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>
28 #include <asm/lv1call.h>
29 #include <asm/firmware.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
,
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
)
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
);
52 pr_debug("%s:%d: lv1_map_device_mmio_region failed: %s\n",
53 __func__
, __LINE__
, ps3_result(result
));
60 EXPORT_SYMBOL_GPL(ps3_mmio_region_create
);
62 int ps3_free_mmio_region(struct ps3_mmio_region
*r
)
66 result
= lv1_unmap_device_mmio_region(r
->did
.bus_id
, r
->did
.dev_id
,
70 pr_debug("%s:%d: lv1_unmap_device_mmio_region failed: %s\n",
71 __func__
, __LINE__
, ps3_result(result
));
76 EXPORT_SYMBOL_GPL(ps3_free_mmio_region
);
78 static int ps3_system_bus_match(struct device
*_dev
,
79 struct device_driver
*_drv
)
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"));
93 static int ps3_system_bus_probe(struct device
*_dev
)
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);
103 pr_debug("%s:%d: lv1_open_device failed (%d)\n",
104 __func__
, __LINE__
, result
);
109 if (dev
->d_region
->did
.bus_id
) {
110 result
= ps3_dma_region_create(dev
->d_region
);
113 pr_debug("%s:%d: ps3_dma_region_create failed (%d)\n",
114 __func__
, __LINE__
, result
);
115 BUG_ON("check region type");
124 result
= drv
->probe(dev
);
126 pr_info("%s:%d: %s no probe method\n", __func__
, __LINE__
,
130 pr_debug("%s:%d: drv->probe failed\n", __func__
, __LINE__
);
137 ps3_dma_region_free(dev
->d_region
);
139 lv1_close_device(dev
->did
.bus_id
, dev
->did
.dev_id
);
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
);
153 pr_info("%s:%d: %s no remove method\n", __func__
, __LINE__
,
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
);
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)
174 if (!firmware_has_feature(FW_FEATURE_PS3_LV1
))
177 result
= bus_register(&ps3_system_bus_type
);
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
)
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
);
201 virt_addr
= __get_free_pages(flag
, get_order(size
));
204 pr_debug("%s:%d: get_free_pages failed\n", __func__
, __LINE__
);
208 result
= ps3_dma_map(dev
->d_region
, virt_addr
, size
, dma_handle
);
211 pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
212 __func__
, __LINE__
, result
);
213 BUG_ON("check region type");
217 return (void*)virt_addr
;
220 free_pages(virt_addr
, get_order(size
));
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
);
247 unsigned long bus_addr
;
249 result
= ps3_dma_map(dev
->d_region
, (unsigned long)ptr
, size
,
253 pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
254 __func__
, __LINE__
, result
);
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
);
266 result
= ps3_dma_unmap(dev
->d_region
, dma_addr
, size
);
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
);
280 #if defined(CONFIG_PS3_DYNAMIC_DMA)
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
,
290 pr_debug("%s:%d: ps3_dma_map failed (%d)\n",
291 __func__
, __LINE__
, result
);
295 sg
->dma_length
= sg
->length
;
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)
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
);
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
)
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",
359 pr_debug("%s:%d add %s\n", __func__
, __LINE__
, dev
->core
.bus_id
);
361 result
= device_register(&dev
->core
);
365 EXPORT_SYMBOL_GPL(ps3_system_bus_device_register
);
367 int ps3_system_bus_driver_register(struct ps3_system_bus_driver
*drv
)
371 drv
->core
.bus
= &ps3_system_bus_type
;
373 result
= driver_register(&drv
->core
);
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
);