Linux 2.6.21
[linux/fpc-iii.git] / arch / powerpc / platforms / ps3 / system-bus.c
blob3c48cce98a5c0bba2ff7a063702ac7d49330d949
1 /*
2 * PS3 system bus driver.
4 * Copyright (C) 2006 Sony Computer Entertainment Inc.
5 * Copyright 2006 Sony Corp.
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.
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.
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
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>
27 #include <asm/udbg.h>
28 #include <asm/lv1call.h>
29 #include <asm/firmware.h>
31 #include "platform.h"
33 #define dump_mmio_region(_a) _dump_mmio_region(_a, __func__, __LINE__)
34 static void _dump_mmio_region(const struct ps3_mmio_region* r,
35 const char* func, int line)
37 pr_debug("%s:%d: dev %u:%u\n", func, line, r->did.bus_id,
38 r->did.dev_id);
39 pr_debug("%s:%d: bus_addr %lxh\n", func, line, r->bus_addr);
40 pr_debug("%s:%d: len %lxh\n", func, line, r->len);
41 pr_debug("%s:%d: lpar_addr %lxh\n", func, line, r->lpar_addr);
44 int ps3_mmio_region_create(struct ps3_mmio_region *r)
46 int result;
48 result = lv1_map_device_mmio_region(r->did.bus_id, r->did.dev_id,
49 r->bus_addr, r->len, r->page_size, &r->lpar_addr);
51 if (result) {
52 pr_debug("%s:%d: lv1_map_device_mmio_region failed: %s\n",
53 __func__, __LINE__, ps3_result(result));
54 r->lpar_addr = 0;
57 dump_mmio_region(r);
58 return result;
60 EXPORT_SYMBOL_GPL(ps3_mmio_region_create);
62 int ps3_free_mmio_region(struct ps3_mmio_region *r)
64 int result;
66 result = lv1_unmap_device_mmio_region(r->did.bus_id, r->did.dev_id,
67 r->lpar_addr);
69 if (result)
70 pr_debug("%s:%d: lv1_unmap_device_mmio_region failed: %s\n",
71 __func__, __LINE__, ps3_result(result));
73 r->lpar_addr = 0;
74 return result;
76 EXPORT_SYMBOL_GPL(ps3_free_mmio_region);
78 static int ps3_system_bus_match(struct device *_dev,
79 struct device_driver *_drv)
81 int result;
82 struct ps3_system_bus_driver *drv = to_ps3_system_bus_driver(_drv);
83 struct ps3_system_bus_device *dev = to_ps3_system_bus_device(_dev);
85 result = dev->match_id == drv->match_id;
87 pr_info("%s:%d: dev=%u(%s), drv=%u(%s): %s\n", __func__, __LINE__,
88 dev->match_id, dev->core.bus_id, drv->match_id, drv->core.name,
89 (result ? "match" : "miss"));
90 return result;
93 static int ps3_system_bus_probe(struct device *_dev)
95 int result;
96 struct ps3_system_bus_device *dev = to_ps3_system_bus_device(_dev);
97 struct ps3_system_bus_driver *drv =
98 to_ps3_system_bus_driver(_dev->driver);
100 result = lv1_open_device(dev->did.bus_id, dev->did.dev_id, 0);
102 if (result) {
103 pr_debug("%s:%d: lv1_open_device failed (%d)\n",
104 __func__, __LINE__, result);
105 result = -EACCES;
106 goto clean_none;
109 if (dev->d_region->did.bus_id) {
110 result = ps3_dma_region_create(dev->d_region);
112 if (result) {
113 pr_debug("%s:%d: ps3_dma_region_create failed (%d)\n",
114 __func__, __LINE__, result);
115 BUG_ON("check region type");
116 result = -EINVAL;
117 goto clean_device;
121 BUG_ON(!drv);
123 if (drv->probe)
124 result = drv->probe(dev);
125 else
126 pr_info("%s:%d: %s no probe method\n", __func__, __LINE__,
127 dev->core.bus_id);
129 if (result) {
130 pr_debug("%s:%d: drv->probe failed\n", __func__, __LINE__);
131 goto clean_dma;
134 return result;
136 clean_dma:
137 ps3_dma_region_free(dev->d_region);
138 clean_device:
139 lv1_close_device(dev->did.bus_id, dev->did.dev_id);
140 clean_none:
141 return result;
144 static int ps3_system_bus_remove(struct device *_dev)
146 struct ps3_system_bus_device *dev = to_ps3_system_bus_device(_dev);
147 struct ps3_system_bus_driver *drv =
148 to_ps3_system_bus_driver(_dev->driver);
150 if (drv->remove)
151 drv->remove(dev);
152 else
153 pr_info("%s:%d: %s no remove method\n", __func__, __LINE__,
154 dev->core.bus_id);
156 ps3_dma_region_free(dev->d_region);
157 ps3_free_mmio_region(dev->m_region);
158 lv1_close_device(dev->did.bus_id, dev->did.dev_id);
160 return 0;
163 struct bus_type ps3_system_bus_type = {
164 .name = "ps3_system_bus",
165 .match = ps3_system_bus_match,
166 .probe = ps3_system_bus_probe,
167 .remove = ps3_system_bus_remove,
170 int __init ps3_system_bus_init(void)
172 int result;
174 if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
175 return -ENODEV;
177 result = bus_register(&ps3_system_bus_type);
178 BUG_ON(result);
179 return result;
182 core_initcall(ps3_system_bus_init);
184 /* Allocates a contiguous real buffer and creates mappings over it.
185 * Returns the virtual address of the buffer and sets dma_handle
186 * to the dma address (mapping) of the first page.
189 static void * ps3_alloc_coherent(struct device *_dev, size_t size,
190 dma_addr_t *dma_handle, gfp_t flag)
192 int result;
193 struct ps3_system_bus_device *dev = to_ps3_system_bus_device(_dev);
194 unsigned long virt_addr;
196 BUG_ON(!dev->d_region->bus_addr);
198 flag &= ~(__GFP_DMA | __GFP_HIGHMEM);
199 flag |= __GFP_ZERO;
201 virt_addr = __get_free_pages(flag, get_order(size));
203 if (!virt_addr) {
204 pr_debug("%s:%d: get_free_pages failed\n", __func__, __LINE__);
205 goto clean_none;
208 result = ps3_dma_map(dev->d_region, virt_addr, size, dma_handle);
210 if (result) {
211 pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
212 __func__, __LINE__, result);
213 BUG_ON("check region type");
214 goto clean_alloc;
217 return (void*)virt_addr;
219 clean_alloc:
220 free_pages(virt_addr, get_order(size));
221 clean_none:
222 dma_handle = NULL;
223 return NULL;
226 static void ps3_free_coherent(struct device *_dev, size_t size, void *vaddr,
227 dma_addr_t dma_handle)
229 struct ps3_system_bus_device *dev = to_ps3_system_bus_device(_dev);
231 ps3_dma_unmap(dev->d_region, dma_handle, size);
232 free_pages((unsigned long)vaddr, get_order(size));
235 /* Creates TCEs for a user provided buffer. The user buffer must be
236 * contiguous real kernel storage (not vmalloc). The address of the buffer
237 * passed here is the kernel (virtual) address of the buffer. The buffer
238 * need not be page aligned, the dma_addr_t returned will point to the same
239 * byte within the page as vaddr.
242 static dma_addr_t ps3_map_single(struct device *_dev, void *ptr, size_t size,
243 enum dma_data_direction direction)
245 struct ps3_system_bus_device *dev = to_ps3_system_bus_device(_dev);
246 int result;
247 unsigned long bus_addr;
249 result = ps3_dma_map(dev->d_region, (unsigned long)ptr, size,
250 &bus_addr);
252 if (result) {
253 pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
254 __func__, __LINE__, result);
257 return bus_addr;
260 static void ps3_unmap_single(struct device *_dev, dma_addr_t dma_addr,
261 size_t size, enum dma_data_direction direction)
263 struct ps3_system_bus_device *dev = to_ps3_system_bus_device(_dev);
264 int result;
266 result = ps3_dma_unmap(dev->d_region, dma_addr, size);
268 if (result) {
269 pr_debug("%s:%d: ps3_dma_unmap failed (%d)\n",
270 __func__, __LINE__, result);
274 static int ps3_map_sg(struct device *_dev, struct scatterlist *sg, int nents,
275 enum dma_data_direction direction)
277 struct ps3_system_bus_device *dev = to_ps3_system_bus_device(_dev);
278 int i;
280 #if defined(CONFIG_PS3_DYNAMIC_DMA)
281 BUG_ON("do");
282 return -EPERM;
283 #else
284 for (i = 0; i < nents; i++, sg++) {
285 int result = ps3_dma_map(dev->d_region,
286 page_to_phys(sg->page) + sg->offset, sg->length,
287 &sg->dma_address);
289 if (result) {
290 pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
291 __func__, __LINE__, result);
292 return -EINVAL;
295 sg->dma_length = sg->length;
298 return nents;
299 #endif
302 static void ps3_unmap_sg(struct device *_dev, struct scatterlist *sg,
303 int nents, enum dma_data_direction direction)
305 #if defined(CONFIG_PS3_DYNAMIC_DMA)
306 BUG_ON("do");
307 #endif
310 static int ps3_dma_supported(struct device *_dev, u64 mask)
312 return mask >= DMA_32BIT_MASK;
315 static struct dma_mapping_ops ps3_dma_ops = {
316 .alloc_coherent = ps3_alloc_coherent,
317 .free_coherent = ps3_free_coherent,
318 .map_single = ps3_map_single,
319 .unmap_single = ps3_unmap_single,
320 .map_sg = ps3_map_sg,
321 .unmap_sg = ps3_unmap_sg,
322 .dma_supported = ps3_dma_supported
326 * ps3_system_bus_release_device - remove a device from the system bus
329 static void ps3_system_bus_release_device(struct device *_dev)
331 struct ps3_system_bus_device *dev = to_ps3_system_bus_device(_dev);
332 kfree(dev);
336 * ps3_system_bus_device_register - add a device to the system bus
338 * ps3_system_bus_device_register() expects the dev object to be allocated
339 * dynamically by the caller. The system bus takes ownership of the dev
340 * object and frees the object in ps3_system_bus_release_device().
343 int ps3_system_bus_device_register(struct ps3_system_bus_device *dev)
345 int result;
346 static unsigned int dev_count = 1;
348 dev->core.parent = NULL;
349 dev->core.bus = &ps3_system_bus_type;
350 dev->core.release = ps3_system_bus_release_device;
352 dev->core.archdata.of_node = NULL;
353 dev->core.archdata.dma_ops = &ps3_dma_ops;
354 dev->core.archdata.numa_node = 0;
356 snprintf(dev->core.bus_id, sizeof(dev->core.bus_id), "sb_%02x",
357 dev_count++);
359 pr_debug("%s:%d add %s\n", __func__, __LINE__, dev->core.bus_id);
361 result = device_register(&dev->core);
362 return result;
365 EXPORT_SYMBOL_GPL(ps3_system_bus_device_register);
367 int ps3_system_bus_driver_register(struct ps3_system_bus_driver *drv)
369 int result;
371 drv->core.bus = &ps3_system_bus_type;
373 result = driver_register(&drv->core);
374 return result;
377 EXPORT_SYMBOL_GPL(ps3_system_bus_driver_register);
379 void ps3_system_bus_driver_unregister(struct ps3_system_bus_driver *drv)
381 driver_unregister(&drv->core);
384 EXPORT_SYMBOL_GPL(ps3_system_bus_driver_unregister);