]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/ps3/system-bus.c
[POWERPC] ps3: add ps3 platform system bus support
[linux-2.6-omap-h63xx.git] / drivers / ps3 / system-bus.c
1 /*
2  *  PS3 system bus driver.
3  *
4  *  Copyright (C) 2006 Sony Computer Entertainment Inc.
5  *  Copyright 2006 Sony Corp.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; version 2 of the License.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <linux/kernel.h>
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/err.h>
26
27 #include <asm/udbg.h>
28 #include <asm/ps3.h>
29 #include <asm/lv1call.h>
30
31 #define dump_mmio_region(_a) _dump_mmio_region(_a, __func__, __LINE__)
32 static void _dump_mmio_region(const struct ps3_mmio_region* r,
33         const char* func, int line)
34 {
35         pr_debug("%s:%d: dev       %u:%u\n", func, line, r->did.bus_id,
36                 r->did.dev_id);
37         pr_debug("%s:%d: bus_addr  %lxh\n", func, line, r->bus_addr);
38         pr_debug("%s:%d: len       %lxh\n", func, line, r->len);
39         pr_debug("%s:%d: lpar_addr %lxh\n", func, line, r->lpar_addr);
40 }
41
42 int ps3_mmio_region_create(struct ps3_mmio_region *r)
43 {
44         int result;
45
46         result = lv1_map_device_mmio_region(r->did.bus_id, r->did.dev_id,
47                 r->bus_addr, r->len, r->page_size, &r->lpar_addr);
48
49         if (result) {
50                 pr_debug("%s:%d: lv1_map_device_mmio_region failed: %s\n",
51                         __func__, __LINE__, ps3_result(result));
52                 r->lpar_addr = r->len = r->bus_addr = 0;
53         }
54
55         dump_mmio_region(r);
56         return result;
57 }
58
59 int ps3_free_mmio_region(struct ps3_mmio_region *r)
60 {
61         int result;
62
63         result = lv1_unmap_device_mmio_region(r->did.bus_id, r->did.dev_id,
64                 r->bus_addr);
65
66         if (result)
67                 pr_debug("%s:%d: lv1_unmap_device_mmio_region failed: %s\n",
68                         __func__, __LINE__, ps3_result(result));
69
70         r->lpar_addr = r->len = r->bus_addr = 0;
71         return result;
72 }
73
74 static int ps3_system_bus_match(struct device *_dev,
75         struct device_driver *_drv)
76 {
77         int result;
78         struct ps3_system_bus_driver *drv = to_ps3_system_bus_driver(_drv);
79         struct ps3_system_bus_device *dev = to_ps3_system_bus_device(_dev);
80
81         result = dev->match_id == drv->match_id;
82
83         pr_info("%s:%d: dev=%u(%s), drv=%u(%s): %s\n", __func__, __LINE__,
84                 dev->match_id, dev->core.bus_id, drv->match_id, drv->core.name,
85                 (result ? "match" : "miss"));
86         return result;
87 }
88
89 static int ps3_system_bus_probe(struct device *_dev)
90 {
91         int result;
92         struct ps3_system_bus_device *dev = to_ps3_system_bus_device(_dev);
93         struct ps3_system_bus_driver *drv =
94                 to_ps3_system_bus_driver(_dev->driver);
95
96         result = lv1_open_device(dev->did.bus_id, dev->did.dev_id, 0);
97
98         if (result) {
99                 pr_debug("%s:%d: lv1_open_device failed (%d)\n",
100                         __func__, __LINE__, result);
101                 result = -EACCES;
102                 goto clean_none;
103         }
104
105         if (dev->d_region->did.bus_id) {
106                 result = ps3_dma_region_create(dev->d_region);
107
108                 if (result) {
109                         pr_debug("%s:%d: ps3_dma_region_create failed (%d)\n",
110                                 __func__, __LINE__, result);
111                         BUG_ON("check region type");
112                         result = -EINVAL;
113                         goto clean_device;
114                 }
115         }
116
117         BUG_ON(!drv);
118
119         if (drv->probe)
120                 result = drv->probe(dev);
121         else
122                 pr_info("%s:%d: %s no probe method\n", __func__, __LINE__,
123                         dev->core.bus_id);
124
125         if (result) {
126                 pr_debug("%s:%d: drv->probe failed\n", __func__, __LINE__);
127                 goto clean_dma;
128         }
129
130         return result;
131
132 clean_dma:
133         ps3_dma_region_free(dev->d_region);
134 clean_device:
135         lv1_close_device(dev->did.bus_id, dev->did.dev_id);
136 clean_none:
137         return result;
138 }
139
140 static int ps3_system_bus_remove(struct device *_dev)
141 {
142         struct ps3_system_bus_device *dev = to_ps3_system_bus_device(_dev);
143         struct ps3_system_bus_driver *drv =
144                 to_ps3_system_bus_driver(_dev->driver);
145
146         if (drv->remove)
147                 drv->remove(dev);
148         else
149                 pr_info("%s:%d: %s no remove method\n", __func__, __LINE__,
150                         dev->core.bus_id);
151
152         ps3_dma_region_free(dev->d_region);
153         ps3_free_mmio_region(dev->m_region);
154         lv1_close_device(dev->did.bus_id, dev->did.dev_id);
155
156         return 0;
157 }
158
159 struct bus_type ps3_system_bus_type = {
160         .name = "ps3_system_bus",
161         .match = ps3_system_bus_match,
162         .probe = ps3_system_bus_probe,
163         .remove = ps3_system_bus_remove,
164 };
165
166 int __init ps3_system_bus_init(void)
167 {
168         int result;
169
170         result = bus_register(&ps3_system_bus_type);
171         BUG_ON(result);
172         return result;
173 }
174
175 core_initcall(ps3_system_bus_init);
176
177 /* Allocates a contiguous real buffer and creates mappings over it.
178  * Returns the virtual address of the buffer and sets dma_handle
179  * to the dma address (mapping) of the first page.
180  */
181
182 static void * ps3_alloc_coherent(struct device *_dev, size_t size,
183         dma_addr_t *dma_handle, gfp_t flag)
184 {
185         int result;
186         struct ps3_system_bus_device *dev = to_ps3_system_bus_device(_dev);
187         unsigned long virt_addr;
188
189         BUG_ON(!dev->d_region->bus_addr);
190
191         flag &= ~(__GFP_DMA | __GFP_HIGHMEM);
192         flag |= __GFP_ZERO;
193
194         virt_addr = __get_free_pages(flag, get_order(size));
195
196         if (!virt_addr) {
197                 pr_debug("%s:%d: get_free_pages failed\n", __func__, __LINE__);
198                 goto clean_none;
199         }
200
201         result = ps3_dma_map(dev->d_region, virt_addr, size, dma_handle);
202
203         if (result) {
204                 pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
205                         __func__, __LINE__, result);
206                 BUG_ON("check region type");
207                 goto clean_alloc;
208         }
209
210         return (void*)virt_addr;
211
212 clean_alloc:
213         free_pages(virt_addr, get_order(size));
214 clean_none:
215         dma_handle = NULL;
216         return NULL;
217 }
218
219 static void ps3_free_coherent(struct device *_dev, size_t size, void *vaddr,
220         dma_addr_t dma_handle)
221 {
222         struct ps3_system_bus_device *dev = to_ps3_system_bus_device(_dev);
223
224         ps3_dma_unmap(dev->d_region, dma_handle, size);
225         free_pages((unsigned long)vaddr, get_order(size));
226 }
227
228 /* Creates TCEs for a user provided buffer.  The user buffer must be
229  * contiguous real kernel storage (not vmalloc).  The address of the buffer
230  * passed here is the kernel (virtual) address of the buffer.  The buffer
231  * need not be page aligned, the dma_addr_t returned will point to the same
232  * byte within the page as vaddr.
233  */
234
235 static dma_addr_t ps3_map_single(struct device *_dev, void *ptr, size_t size,
236         enum dma_data_direction direction)
237 {
238         struct ps3_system_bus_device *dev = to_ps3_system_bus_device(_dev);
239         int result;
240         unsigned long bus_addr;
241
242         result = ps3_dma_map(dev->d_region, (unsigned long)ptr, size,
243                 &bus_addr);
244
245         if (result) {
246                 pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
247                         __func__, __LINE__, result);
248         }
249
250         return bus_addr;
251 }
252
253 static void ps3_unmap_single(struct device *_dev, dma_addr_t dma_addr,
254         size_t size, enum dma_data_direction direction)
255 {
256         struct ps3_system_bus_device *dev = to_ps3_system_bus_device(_dev);
257         int result;
258
259         result = ps3_dma_unmap(dev->d_region, dma_addr, size);
260
261         if (result) {
262                 pr_debug("%s:%d: ps3_dma_unmap failed (%d)\n",
263                         __func__, __LINE__, result);
264         }
265 }
266
267 static int ps3_map_sg(struct device *_dev, struct scatterlist *sg, int nents,
268         enum dma_data_direction direction)
269 {
270 #if defined(CONFIG_PS3_DYNAMIC_DMA)
271         BUG_ON("do");
272 #endif
273         return 0;
274 }
275
276 static void ps3_unmap_sg(struct device *_dev, struct scatterlist *sg,
277         int nents, enum dma_data_direction direction)
278 {
279 #if defined(CONFIG_PS3_DYNAMIC_DMA)
280         BUG_ON("do");
281 #endif
282 }
283
284 static int ps3_dma_supported(struct device *_dev, u64 mask)
285 {
286         return 1;
287 }
288
289 static struct dma_mapping_ops ps3_dma_ops = {
290         .alloc_coherent = ps3_alloc_coherent,
291         .free_coherent = ps3_free_coherent,
292         .map_single = ps3_map_single,
293         .unmap_single = ps3_unmap_single,
294         .map_sg = ps3_map_sg,
295         .unmap_sg = ps3_unmap_sg,
296         .dma_supported = ps3_dma_supported
297 };
298
299 /**
300  * ps3_system_bus_release_device - remove a device from the system bus
301  */
302
303 static void ps3_system_bus_release_device(struct device *_dev)
304 {
305         struct ps3_system_bus_device *dev = to_ps3_system_bus_device(_dev);
306         kfree(dev);
307 }
308
309 /**
310  * ps3_system_bus_device_register - add a device to the system bus
311  *
312  * ps3_system_bus_device_register() expects the dev object to be allocated
313  * dynamically by the caller.  The system bus takes ownership of the dev
314  * object and frees the object in ps3_system_bus_release_device().
315  */
316
317 int ps3_system_bus_device_register(struct ps3_system_bus_device *dev)
318 {
319         int result;
320         static unsigned int dev_count = 1;
321
322         dev->core.parent = NULL;
323         dev->core.bus = &ps3_system_bus_type;
324         dev->core.release = ps3_system_bus_release_device;
325
326         dev->core.archdata.of_node = NULL;
327         dev->core.archdata.dma_ops = &ps3_dma_ops;
328         dev->core.archdata.numa_node = 0;
329
330         snprintf(dev->core.bus_id, sizeof(dev->core.bus_id), "sb_%02x",
331                 dev_count++);
332
333         pr_debug("%s:%d add %s\n", __func__, __LINE__, dev->core.bus_id);
334
335         result = device_register(&dev->core);
336         return result;
337 }
338
339 EXPORT_SYMBOL_GPL(ps3_system_bus_device_register);
340
341 int ps3_system_bus_driver_register(struct ps3_system_bus_driver *drv)
342 {
343         int result;
344
345         drv->core.bus = &ps3_system_bus_type;
346
347         result = driver_register(&drv->core);
348         return result;
349 }
350
351 EXPORT_SYMBOL_GPL(ps3_system_bus_driver_register);
352
353 void ps3_system_bus_driver_unregister(struct ps3_system_bus_driver *drv)
354 {
355         driver_unregister(&drv->core);
356 }
357
358 EXPORT_SYMBOL_GPL(ps3_system_bus_driver_unregister);