Commit | Line | Data |
---|---|---|
17be2d2b AM |
1 | /* |
2 | * Core maple bus functionality | |
3 | * | |
b233b28e | 4 | * Copyright (C) 2007 - 2009 Adrian McMenamin |
63870295 | 5 | * Copyright (C) 2001 - 2008 Paul Mundt |
b233b28e | 6 | * Copyright (C) 2000 - 2001 YAEGASHI Takeshi |
17be2d2b | 7 | * Copyright (C) 2001 M. R. Brown |
17be2d2b AM |
8 | * |
9 | * This file is subject to the terms and conditions of the GNU General Public | |
10 | * License. See the file "COPYING" in the main directory of this archive | |
11 | * for more details. | |
12 | */ | |
13 | #include <linux/init.h> | |
14 | #include <linux/kernel.h> | |
15 | #include <linux/device.h> | |
17be2d2b AM |
16 | #include <linux/interrupt.h> |
17 | #include <linux/list.h> | |
18 | #include <linux/io.h> | |
19 | #include <linux/slab.h> | |
20 | #include <linux/maple.h> | |
21 | #include <linux/dma-mapping.h> | |
1795cf48 | 22 | #include <linux/delay.h> |
db4e8395 | 23 | #include <linux/module.h> |
17be2d2b AM |
24 | #include <asm/cacheflush.h> |
25 | #include <asm/dma.h> | |
26 | #include <asm/io.h> | |
1795cf48 AM |
27 | #include <mach/dma.h> |
28 | #include <mach/sysasic.h> | |
17be2d2b | 29 | |
b233b28e | 30 | MODULE_AUTHOR("Adrian McMenamin <adrian@mcmen.demon.co.uk>"); |
17be2d2b AM |
31 | MODULE_DESCRIPTION("Maple bus driver for Dreamcast"); |
32 | MODULE_LICENSE("GPL v2"); | |
17be2d2b AM |
33 | |
34 | static void maple_dma_handler(struct work_struct *work); | |
35 | static void maple_vblank_handler(struct work_struct *work); | |
36 | ||
37 | static DECLARE_WORK(maple_dma_process, maple_dma_handler); | |
38 | static DECLARE_WORK(maple_vblank_process, maple_vblank_handler); | |
39 | ||
40 | static LIST_HEAD(maple_waitq); | |
41 | static LIST_HEAD(maple_sentq); | |
42 | ||
1795cf48 AM |
43 | /* mutex to protect queue of waiting packets */ |
44 | static DEFINE_MUTEX(maple_wlist_lock); | |
17be2d2b | 45 | |
b233b28e | 46 | static struct maple_driver maple_unsupported_device; |
17be2d2b AM |
47 | static struct device maple_bus; |
48 | static int subdevice_map[MAPLE_PORTS]; | |
49 | static unsigned long *maple_sendbuf, *maple_sendptr, *maple_lastptr; | |
50 | static unsigned long maple_pnp_time; | |
1795cf48 | 51 | static int started, scanning, fullscan; |
17be2d2b AM |
52 | static struct kmem_cache *maple_queue_cache; |
53 | ||
54 | struct maple_device_specify { | |
b9482378 AM |
55 | int port; |
56 | int unit; | |
17be2d2b AM |
57 | }; |
58 | ||
b233b28e AM |
59 | static bool checked[MAPLE_PORTS]; |
60 | static bool empty[MAPLE_PORTS]; | |
61 | static struct maple_device *baseunits[MAPLE_PORTS]; | |
bd496669 | 62 | |
17be2d2b | 63 | /** |
63870295 PM |
64 | * maple_driver_register - register a maple driver |
65 | * @drv: maple driver to be registered. | |
66 | * | |
67 | * Registers the passed in @drv, while updating the bus type. | |
68 | * Devices with matching function IDs will be automatically probed. | |
17be2d2b | 69 | */ |
63870295 | 70 | int maple_driver_register(struct maple_driver *drv) |
17be2d2b | 71 | { |
b9482378 AM |
72 | if (!drv) |
73 | return -EINVAL; | |
63870295 PM |
74 | |
75 | drv->drv.bus = &maple_bus_type; | |
76 | ||
77 | return driver_register(&drv->drv); | |
17be2d2b AM |
78 | } |
79 | EXPORT_SYMBOL_GPL(maple_driver_register); | |
80 | ||
63870295 PM |
81 | /** |
82 | * maple_driver_unregister - unregister a maple driver. | |
83 | * @drv: maple driver to unregister. | |
84 | * | |
85 | * Cleans up after maple_driver_register(). To be invoked in the exit | |
86 | * path of any module drivers. | |
87 | */ | |
88 | void maple_driver_unregister(struct maple_driver *drv) | |
89 | { | |
90 | driver_unregister(&drv->drv); | |
91 | } | |
61787063 | 92 | EXPORT_SYMBOL_GPL(maple_driver_unregister); |
63870295 | 93 | |
17be2d2b | 94 | /* set hardware registers to enable next round of dma */ |
b233b28e | 95 | static void maple_dma_reset(void) |
17be2d2b | 96 | { |
c0537844 | 97 | __raw_writel(MAPLE_MAGIC, MAPLE_RESET); |
b9482378 | 98 | /* set trig type to 0 for software trigger, 1 for hardware (VBLANK) */ |
c0537844 | 99 | __raw_writel(1, MAPLE_TRIGTYPE); |
b233b28e AM |
100 | /* |
101 | * Maple system register | |
102 | * bits 31 - 16 timeout in units of 20nsec | |
103 | * bit 12 hard trigger - set 0 to keep responding to VBLANK | |
104 | * bits 9 - 8 set 00 for 2 Mbps, 01 for 1 Mbps | |
105 | * bits 3 - 0 delay (in 1.3ms) between VBLANK and start of DMA | |
106 | * max delay is 11 | |
107 | */ | |
c0537844 PM |
108 | __raw_writel(MAPLE_2MBPS | MAPLE_TIMEOUT(0xFFFF), MAPLE_SPEED); |
109 | __raw_writel(virt_to_phys(maple_sendbuf), MAPLE_DMAADDR); | |
110 | __raw_writel(1, MAPLE_ENABLE); | |
17be2d2b AM |
111 | } |
112 | ||
113 | /** | |
114 | * maple_getcond_callback - setup handling MAPLE_COMMAND_GETCOND | |
115 | * @dev: device responding | |
116 | * @callback: handler callback | |
117 | * @interval: interval in jiffies between callbacks | |
118 | * @function: the function code for the device | |
119 | */ | |
120 | void maple_getcond_callback(struct maple_device *dev, | |
b3c69e24 AM |
121 | void (*callback) (struct mapleq *mq), |
122 | unsigned long interval, unsigned long function) | |
17be2d2b | 123 | { |
b9482378 AM |
124 | dev->callback = callback; |
125 | dev->interval = interval; | |
126 | dev->function = cpu_to_be32(function); | |
127 | dev->when = jiffies; | |
17be2d2b AM |
128 | } |
129 | EXPORT_SYMBOL_GPL(maple_getcond_callback); | |
130 | ||
131 | static int maple_dma_done(void) | |
132 | { | |
c0537844 | 133 | return (__raw_readl(MAPLE_STATE) & 1) == 0; |
17be2d2b AM |
134 | } |
135 | ||
136 | static void maple_release_device(struct device *dev) | |
137 | { | |
b3c69e24 AM |
138 | struct maple_device *mdev; |
139 | struct mapleq *mq; | |
b233b28e | 140 | |
b3c69e24 AM |
141 | mdev = to_maple_dev(dev); |
142 | mq = mdev->mq; | |
b233b28e AM |
143 | kmem_cache_free(maple_queue_cache, mq->recvbuf); |
144 | kfree(mq); | |
b3c69e24 | 145 | kfree(mdev); |
17be2d2b AM |
146 | } |
147 | ||
6a9545bd | 148 | /** |
b233b28e | 149 | * maple_add_packet - add a single instruction to the maple bus queue |
6a9545bd PM |
150 | * @mdev: maple device |
151 | * @function: function on device being queried | |
152 | * @command: maple command to add | |
153 | * @length: length of command string (in 32 bit words) | |
154 | * @data: remainder of command string | |
17be2d2b | 155 | */ |
1795cf48 AM |
156 | int maple_add_packet(struct maple_device *mdev, u32 function, u32 command, |
157 | size_t length, void *data) | |
17be2d2b | 158 | { |
b233b28e | 159 | int ret = 0; |
1795cf48 AM |
160 | void *sendbuf = NULL; |
161 | ||
1795cf48 | 162 | if (length) { |
6396bb22 | 163 | sendbuf = kcalloc(length, 4, GFP_KERNEL); |
1795cf48 | 164 | if (!sendbuf) { |
1795cf48 AM |
165 | ret = -ENOMEM; |
166 | goto out; | |
167 | } | |
168 | ((__be32 *)sendbuf)[0] = cpu_to_be32(function); | |
169 | } | |
170 | ||
171 | mdev->mq->command = command; | |
172 | mdev->mq->length = length; | |
173 | if (length > 1) | |
174 | memcpy(sendbuf + 4, data, (length - 1) * 4); | |
175 | mdev->mq->sendbuf = sendbuf; | |
176 | ||
177 | mutex_lock(&maple_wlist_lock); | |
b233b28e | 178 | list_add_tail(&mdev->mq->list, &maple_waitq); |
1795cf48 AM |
179 | mutex_unlock(&maple_wlist_lock); |
180 | out: | |
181 | return ret; | |
182 | } | |
b233b28e | 183 | EXPORT_SYMBOL_GPL(maple_add_packet); |
1795cf48 | 184 | |
b3c69e24 | 185 | static struct mapleq *maple_allocq(struct maple_device *mdev) |
17be2d2b | 186 | { |
b9482378 | 187 | struct mapleq *mq; |
17be2d2b | 188 | |
b233b28e | 189 | mq = kzalloc(sizeof(*mq), GFP_KERNEL); |
b9482378 | 190 | if (!mq) |
1795cf48 | 191 | goto failed_nomem; |
17be2d2b | 192 | |
b233b28e | 193 | INIT_LIST_HEAD(&mq->list); |
b3c69e24 | 194 | mq->dev = mdev; |
b233b28e | 195 | mq->recvbuf = kmem_cache_zalloc(maple_queue_cache, GFP_KERNEL); |
1795cf48 AM |
196 | if (!mq->recvbuf) |
197 | goto failed_p2; | |
b233b28e | 198 | mq->recvbuf->buf = &((mq->recvbuf->bufx)[0]); |
17be2d2b | 199 | |
b9482378 | 200 | return mq; |
1795cf48 AM |
201 | |
202 | failed_p2: | |
203 | kfree(mq); | |
204 | failed_nomem: | |
b233b28e AM |
205 | dev_err(&mdev->dev, "could not allocate memory for device (%d, %d)\n", |
206 | mdev->port, mdev->unit); | |
1795cf48 | 207 | return NULL; |
17be2d2b AM |
208 | } |
209 | ||
210 | static struct maple_device *maple_alloc_dev(int port, int unit) | |
211 | { | |
b3c69e24 | 212 | struct maple_device *mdev; |
17be2d2b | 213 | |
b233b28e AM |
214 | /* zero this out to avoid kobj subsystem |
215 | * thinking it has already been registered */ | |
216 | ||
b3c69e24 AM |
217 | mdev = kzalloc(sizeof(*mdev), GFP_KERNEL); |
218 | if (!mdev) | |
b9482378 | 219 | return NULL; |
17be2d2b | 220 | |
b3c69e24 AM |
221 | mdev->port = port; |
222 | mdev->unit = unit; | |
b233b28e | 223 | |
b3c69e24 | 224 | mdev->mq = maple_allocq(mdev); |
17be2d2b | 225 | |
b3c69e24 AM |
226 | if (!mdev->mq) { |
227 | kfree(mdev); | |
b9482378 AM |
228 | return NULL; |
229 | } | |
b3c69e24 AM |
230 | mdev->dev.bus = &maple_bus_type; |
231 | mdev->dev.parent = &maple_bus; | |
b233b28e | 232 | init_waitqueue_head(&mdev->maple_wait); |
b3c69e24 | 233 | return mdev; |
17be2d2b AM |
234 | } |
235 | ||
236 | static void maple_free_dev(struct maple_device *mdev) | |
237 | { | |
b233b28e AM |
238 | kmem_cache_free(maple_queue_cache, mdev->mq->recvbuf); |
239 | kfree(mdev->mq); | |
b9482378 | 240 | kfree(mdev); |
17be2d2b AM |
241 | } |
242 | ||
243 | /* process the command queue into a maple command block | |
244 | * terminating command has bit 32 of first long set to 0 | |
245 | */ | |
246 | static void maple_build_block(struct mapleq *mq) | |
247 | { | |
b9482378 AM |
248 | int port, unit, from, to, len; |
249 | unsigned long *lsendbuf = mq->sendbuf; | |
17be2d2b | 250 | |
b9482378 AM |
251 | port = mq->dev->port & 3; |
252 | unit = mq->dev->unit; | |
253 | len = mq->length; | |
254 | from = port << 6; | |
255 | to = (port << 6) | (unit > 0 ? (1 << (unit - 1)) & 0x1f : 0x20); | |
17be2d2b | 256 | |
b9482378 AM |
257 | *maple_lastptr &= 0x7fffffff; |
258 | maple_lastptr = maple_sendptr; | |
17be2d2b | 259 | |
b9482378 | 260 | *maple_sendptr++ = (port << 16) | len | 0x80000000; |
913df445 | 261 | *maple_sendptr++ = virt_to_phys(mq->recvbuf->buf); |
b9482378 AM |
262 | *maple_sendptr++ = |
263 | mq->command | (to << 8) | (from << 16) | (len << 24); | |
b9482378 AM |
264 | while (len-- > 0) |
265 | *maple_sendptr++ = *lsendbuf++; | |
17be2d2b AM |
266 | } |
267 | ||
268 | /* build up command queue */ | |
269 | static void maple_send(void) | |
270 | { | |
1795cf48 | 271 | int i, maple_packets = 0; |
b9482378 AM |
272 | struct mapleq *mq, *nmq; |
273 | ||
b233b28e | 274 | if (!maple_dma_done()) |
b9482378 | 275 | return; |
b233b28e AM |
276 | |
277 | /* disable DMA */ | |
c0537844 | 278 | __raw_writel(0, MAPLE_ENABLE); |
b233b28e AM |
279 | |
280 | if (!list_empty(&maple_sentq)) | |
281 | goto finish; | |
282 | ||
1795cf48 | 283 | mutex_lock(&maple_wlist_lock); |
b233b28e | 284 | if (list_empty(&maple_waitq)) { |
1795cf48 | 285 | mutex_unlock(&maple_wlist_lock); |
b233b28e | 286 | goto finish; |
1795cf48 | 287 | } |
b233b28e | 288 | |
1795cf48 AM |
289 | maple_lastptr = maple_sendbuf; |
290 | maple_sendptr = maple_sendbuf; | |
b233b28e | 291 | |
b9482378 AM |
292 | list_for_each_entry_safe(mq, nmq, &maple_waitq, list) { |
293 | maple_build_block(mq); | |
b233b28e AM |
294 | list_del_init(&mq->list); |
295 | list_add_tail(&mq->list, &maple_sentq); | |
b9482378 AM |
296 | if (maple_packets++ > MAPLE_MAXPACKETS) |
297 | break; | |
298 | } | |
1795cf48 | 299 | mutex_unlock(&maple_wlist_lock); |
b9482378 AM |
300 | if (maple_packets > 0) { |
301 | for (i = 0; i < (1 << MAPLE_DMA_PAGES); i++) | |
47fcae0d CH |
302 | __flush_purge_region(maple_sendbuf + i * PAGE_SIZE, |
303 | PAGE_SIZE); | |
b9482378 | 304 | } |
b233b28e AM |
305 | |
306 | finish: | |
307 | maple_dma_reset(); | |
17be2d2b AM |
308 | } |
309 | ||
1795cf48 | 310 | /* check if there is a driver registered likely to match this device */ |
b233b28e | 311 | static int maple_check_matching_driver(struct device_driver *driver, |
b9482378 | 312 | void *devptr) |
17be2d2b | 313 | { |
b9482378 AM |
314 | struct maple_driver *maple_drv; |
315 | struct maple_device *mdev; | |
316 | ||
317 | mdev = devptr; | |
318 | maple_drv = to_maple_driver(driver); | |
1795cf48 AM |
319 | if (mdev->devinfo.function & cpu_to_be32(maple_drv->function)) |
320 | return 1; | |
b9482378 | 321 | return 0; |
17be2d2b AM |
322 | } |
323 | ||
324 | static void maple_detach_driver(struct maple_device *mdev) | |
325 | { | |
b3c69e24 | 326 | device_unregister(&mdev->dev); |
17be2d2b AM |
327 | } |
328 | ||
329 | /* process initial MAPLE_COMMAND_DEVINFO for each device or port */ | |
b3c69e24 | 330 | static void maple_attach_driver(struct maple_device *mdev) |
17be2d2b | 331 | { |
b3c69e24 | 332 | char *p, *recvbuf; |
b9482378 | 333 | unsigned long function; |
b233b28e | 334 | int matched, error; |
b9482378 | 335 | |
b233b28e | 336 | recvbuf = mdev->mq->recvbuf->buf; |
b3c69e24 AM |
337 | /* copy the data as individual elements in |
338 | * case of memory optimisation */ | |
339 | memcpy(&mdev->devinfo.function, recvbuf + 4, 4); | |
340 | memcpy(&mdev->devinfo.function_data[0], recvbuf + 8, 12); | |
341 | memcpy(&mdev->devinfo.area_code, recvbuf + 20, 1); | |
342 | memcpy(&mdev->devinfo.connector_direction, recvbuf + 21, 1); | |
343 | memcpy(&mdev->devinfo.product_name[0], recvbuf + 22, 30); | |
b3c69e24 AM |
344 | memcpy(&mdev->devinfo.standby_power, recvbuf + 112, 2); |
345 | memcpy(&mdev->devinfo.max_power, recvbuf + 114, 2); | |
346 | memcpy(mdev->product_name, mdev->devinfo.product_name, 30); | |
347 | mdev->product_name[30] = '\0'; | |
348 | memcpy(mdev->product_licence, mdev->devinfo.product_licence, 60); | |
349 | mdev->product_licence[60] = '\0'; | |
350 | ||
351 | for (p = mdev->product_name + 29; mdev->product_name <= p; p--) | |
b9482378 AM |
352 | if (*p == ' ') |
353 | *p = '\0'; | |
354 | else | |
355 | break; | |
b3c69e24 | 356 | for (p = mdev->product_licence + 59; mdev->product_licence <= p; p--) |
b9482378 AM |
357 | if (*p == ' ') |
358 | *p = '\0'; | |
359 | else | |
360 | break; | |
361 | ||
b3c69e24 | 362 | function = be32_to_cpu(mdev->devinfo.function); |
b9482378 | 363 | |
b233b28e AM |
364 | dev_info(&mdev->dev, "detected %s: function 0x%lX: at (%d, %d)\n", |
365 | mdev->product_name, function, mdev->port, mdev->unit); | |
366 | ||
b9482378 AM |
367 | if (function > 0x200) { |
368 | /* Do this silently - as not a real device */ | |
369 | function = 0; | |
b233b28e | 370 | mdev->driver = &maple_unsupported_device; |
93fde774 | 371 | dev_set_name(&mdev->dev, "%d:0.port", mdev->port); |
b9482378 | 372 | } else { |
b9482378 | 373 | matched = |
1795cf48 | 374 | bus_for_each_drv(&maple_bus_type, NULL, mdev, |
b233b28e | 375 | maple_check_matching_driver); |
b9482378 AM |
376 | |
377 | if (matched == 0) { | |
378 | /* Driver does not exist yet */ | |
b233b28e AM |
379 | dev_info(&mdev->dev, "no driver found\n"); |
380 | mdev->driver = &maple_unsupported_device; | |
b9482378 | 381 | } |
93fde774 KS |
382 | dev_set_name(&mdev->dev, "%d:0%d.%lX", mdev->port, |
383 | mdev->unit, function); | |
b9482378 | 384 | } |
b233b28e | 385 | |
b3c69e24 AM |
386 | mdev->function = function; |
387 | mdev->dev.release = &maple_release_device; | |
b233b28e AM |
388 | |
389 | atomic_set(&mdev->busy, 0); | |
390 | error = device_register(&mdev->dev); | |
391 | if (error) { | |
392 | dev_warn(&mdev->dev, "could not register device at" | |
393 | " (%d, %d), with error 0x%X\n", mdev->unit, | |
394 | mdev->port, error); | |
b3c69e24 AM |
395 | maple_free_dev(mdev); |
396 | mdev = NULL; | |
397 | return; | |
b9482378 | 398 | } |
17be2d2b AM |
399 | } |
400 | ||
401 | /* | |
402 | * if device has been registered for the given | |
403 | * port and unit then return 1 - allows identification | |
404 | * of which devices need to be attached or detached | |
405 | */ | |
b233b28e | 406 | static int check_maple_device(struct device *device, void *portptr) |
17be2d2b | 407 | { |
b9482378 AM |
408 | struct maple_device_specify *ds; |
409 | struct maple_device *mdev; | |
410 | ||
411 | ds = portptr; | |
412 | mdev = to_maple_dev(device); | |
413 | if (mdev->port == ds->port && mdev->unit == ds->unit) | |
414 | return 1; | |
415 | return 0; | |
17be2d2b AM |
416 | } |
417 | ||
418 | static int setup_maple_commands(struct device *device, void *ignored) | |
419 | { | |
1795cf48 | 420 | int add; |
b233b28e AM |
421 | struct maple_device *mdev = to_maple_dev(device); |
422 | if (mdev->interval > 0 && atomic_read(&mdev->busy) == 0 && | |
423 | time_after(jiffies, mdev->when)) { | |
424 | /* bounce if we cannot add */ | |
425 | add = maple_add_packet(mdev, | |
426 | be32_to_cpu(mdev->devinfo.function), | |
1795cf48 AM |
427 | MAPLE_COMMAND_GETCOND, 1, NULL); |
428 | if (!add) | |
b233b28e | 429 | mdev->when = jiffies + mdev->interval; |
b9482378 | 430 | } else { |
1795cf48 | 431 | if (time_after(jiffies, maple_pnp_time)) |
b233b28e AM |
432 | /* Ensure we don't have block reads and devinfo |
433 | * calls interfering with one another - so flag the | |
434 | * device as busy */ | |
435 | if (atomic_read(&mdev->busy) == 0) { | |
436 | atomic_set(&mdev->busy, 1); | |
437 | maple_add_packet(mdev, 0, | |
438 | MAPLE_COMMAND_DEVINFO, 0, NULL); | |
439 | } | |
b9482378 | 440 | } |
b9482378 | 441 | return 0; |
17be2d2b AM |
442 | } |
443 | ||
444 | /* VBLANK bottom half - implemented via workqueue */ | |
445 | static void maple_vblank_handler(struct work_struct *work) | |
446 | { | |
b233b28e AM |
447 | int x, locking; |
448 | struct maple_device *mdev; | |
449 | ||
450 | if (!maple_dma_done()) | |
b9482378 | 451 | return; |
1795cf48 | 452 | |
c0537844 | 453 | __raw_writel(0, MAPLE_ENABLE); |
1795cf48 | 454 | |
b233b28e AM |
455 | if (!list_empty(&maple_sentq)) |
456 | goto finish; | |
457 | ||
458 | /* | |
459 | * Set up essential commands - to fetch data and | |
460 | * check devices are still present | |
461 | */ | |
b9482378 | 462 | bus_for_each_dev(&maple_bus_type, NULL, NULL, |
b233b28e AM |
463 | setup_maple_commands); |
464 | ||
465 | if (time_after(jiffies, maple_pnp_time)) { | |
466 | /* | |
467 | * Scan the empty ports - bus is flakey and may have | |
468 | * mis-reported emptyness | |
469 | */ | |
470 | for (x = 0; x < MAPLE_PORTS; x++) { | |
471 | if (checked[x] && empty[x]) { | |
472 | mdev = baseunits[x]; | |
473 | if (!mdev) | |
474 | break; | |
475 | atomic_set(&mdev->busy, 1); | |
476 | locking = maple_add_packet(mdev, 0, | |
477 | MAPLE_COMMAND_DEVINFO, 0, NULL); | |
478 | if (!locking) | |
479 | break; | |
480 | } | |
481 | } | |
1795cf48 | 482 | |
b9482378 | 483 | maple_pnp_time = jiffies + MAPLE_PNP_INTERVAL; |
b9482378 | 484 | } |
1795cf48 | 485 | |
b233b28e AM |
486 | finish: |
487 | maple_send(); | |
17be2d2b AM |
488 | } |
489 | ||
b233b28e | 490 | /* handle devices added via hotplugs - placing them on queue for DEVINFO */ |
17be2d2b AM |
491 | static void maple_map_subunits(struct maple_device *mdev, int submask) |
492 | { | |
b9482378 AM |
493 | int retval, k, devcheck; |
494 | struct maple_device *mdev_add; | |
495 | struct maple_device_specify ds; | |
496 | ||
1795cf48 | 497 | ds.port = mdev->port; |
b9482378 | 498 | for (k = 0; k < 5; k++) { |
b9482378 AM |
499 | ds.unit = k + 1; |
500 | retval = | |
501 | bus_for_each_dev(&maple_bus_type, NULL, &ds, | |
b233b28e | 502 | check_maple_device); |
b9482378 AM |
503 | if (retval) { |
504 | submask = submask >> 1; | |
505 | continue; | |
506 | } | |
507 | devcheck = submask & 0x01; | |
508 | if (devcheck) { | |
509 | mdev_add = maple_alloc_dev(mdev->port, k + 1); | |
510 | if (!mdev_add) | |
511 | return; | |
b233b28e | 512 | atomic_set(&mdev_add->busy, 1); |
1795cf48 AM |
513 | maple_add_packet(mdev_add, 0, MAPLE_COMMAND_DEVINFO, |
514 | 0, NULL); | |
515 | /* mark that we are checking sub devices */ | |
b9482378 AM |
516 | scanning = 1; |
517 | } | |
518 | submask = submask >> 1; | |
519 | } | |
17be2d2b AM |
520 | } |
521 | ||
522 | /* mark a device as removed */ | |
523 | static void maple_clean_submap(struct maple_device *mdev) | |
524 | { | |
b9482378 | 525 | int killbit; |
17be2d2b | 526 | |
b9482378 AM |
527 | killbit = (mdev->unit > 0 ? (1 << (mdev->unit - 1)) & 0x1f : 0x20); |
528 | killbit = ~killbit; | |
529 | killbit &= 0xFF; | |
530 | subdevice_map[mdev->port] = subdevice_map[mdev->port] & killbit; | |
17be2d2b AM |
531 | } |
532 | ||
533 | /* handle empty port or hotplug removal */ | |
b233b28e AM |
534 | static void maple_response_none(struct maple_device *mdev) |
535 | { | |
536 | maple_clean_submap(mdev); | |
537 | ||
538 | if (likely(mdev->unit != 0)) { | |
539 | /* | |
540 | * Block devices play up | |
541 | * and give the impression they have | |
542 | * been removed even when still in place or | |
543 | * trip the mtd layer when they have | |
544 | * really gone - this code traps that eventuality | |
545 | * and ensures we aren't overloaded with useless | |
546 | * error messages | |
547 | */ | |
548 | if (mdev->can_unload) { | |
549 | if (!mdev->can_unload(mdev)) { | |
550 | atomic_set(&mdev->busy, 2); | |
551 | wake_up(&mdev->maple_wait); | |
552 | return; | |
553 | } | |
554 | } | |
555 | ||
556 | dev_info(&mdev->dev, "detaching device at (%d, %d)\n", | |
557 | mdev->port, mdev->unit); | |
b9482378 AM |
558 | maple_detach_driver(mdev); |
559 | return; | |
b233b28e AM |
560 | } else { |
561 | if (!started || !fullscan) { | |
562 | if (checked[mdev->port] == false) { | |
563 | checked[mdev->port] = true; | |
564 | empty[mdev->port] = true; | |
565 | dev_info(&mdev->dev, "no devices" | |
566 | " to port %d\n", mdev->port); | |
567 | } | |
568 | return; | |
bd496669 | 569 | } |
b9482378 | 570 | } |
b233b28e AM |
571 | /* Some hardware devices generate false detach messages on unit 0 */ |
572 | atomic_set(&mdev->busy, 0); | |
17be2d2b AM |
573 | } |
574 | ||
575 | /* preprocess hotplugs or scans */ | |
576 | static void maple_response_devinfo(struct maple_device *mdev, | |
b9482378 | 577 | char *recvbuf) |
17be2d2b | 578 | { |
b9482378 | 579 | char submask; |
bd496669 AM |
580 | if (!started || (scanning == 2) || !fullscan) { |
581 | if ((mdev->unit == 0) && (checked[mdev->port] == false)) { | |
582 | checked[mdev->port] = true; | |
583 | maple_attach_driver(mdev); | |
584 | } else { | |
585 | if (mdev->unit != 0) | |
586 | maple_attach_driver(mdev); | |
b233b28e AM |
587 | if (mdev->unit == 0) { |
588 | empty[mdev->port] = false; | |
589 | maple_attach_driver(mdev); | |
590 | } | |
bd496669 | 591 | } |
b9482378 AM |
592 | } |
593 | if (mdev->unit == 0) { | |
594 | submask = recvbuf[2] & 0x1F; | |
595 | if (submask ^ subdevice_map[mdev->port]) { | |
596 | maple_map_subunits(mdev, submask); | |
597 | subdevice_map[mdev->port] = submask; | |
598 | } | |
599 | } | |
17be2d2b AM |
600 | } |
601 | ||
b233b28e AM |
602 | static void maple_response_fileerr(struct maple_device *mdev, void *recvbuf) |
603 | { | |
604 | if (mdev->fileerr_handler) { | |
605 | mdev->fileerr_handler(mdev, recvbuf); | |
606 | return; | |
607 | } else | |
608 | dev_warn(&mdev->dev, "device at (%d, %d) reports" | |
609 | "file error 0x%X\n", mdev->port, mdev->unit, | |
610 | ((int *)recvbuf)[1]); | |
611 | } | |
612 | ||
1795cf48 AM |
613 | static void maple_port_rescan(void) |
614 | { | |
615 | int i; | |
616 | struct maple_device *mdev; | |
617 | ||
618 | fullscan = 1; | |
619 | for (i = 0; i < MAPLE_PORTS; i++) { | |
620 | if (checked[i] == false) { | |
621 | fullscan = 0; | |
622 | mdev = baseunits[i]; | |
1795cf48 AM |
623 | maple_add_packet(mdev, 0, MAPLE_COMMAND_DEVINFO, |
624 | 0, NULL); | |
625 | } | |
626 | } | |
627 | } | |
628 | ||
17be2d2b AM |
629 | /* maple dma end bottom half - implemented via workqueue */ |
630 | static void maple_dma_handler(struct work_struct *work) | |
631 | { | |
b9482378 | 632 | struct mapleq *mq, *nmq; |
b233b28e | 633 | struct maple_device *mdev; |
b9482378 AM |
634 | char *recvbuf; |
635 | enum maple_code code; | |
636 | ||
637 | if (!maple_dma_done()) | |
638 | return; | |
c0537844 | 639 | __raw_writel(0, MAPLE_ENABLE); |
b9482378 AM |
640 | if (!list_empty(&maple_sentq)) { |
641 | list_for_each_entry_safe(mq, nmq, &maple_sentq, list) { | |
b233b28e AM |
642 | mdev = mq->dev; |
643 | recvbuf = mq->recvbuf->buf; | |
47fcae0d CH |
644 | __flush_invalidate_region(sh_cacheop_vaddr(recvbuf), |
645 | 0x400); | |
b9482378 | 646 | code = recvbuf[0]; |
1795cf48 | 647 | kfree(mq->sendbuf); |
1795cf48 | 648 | list_del_init(&mq->list); |
b9482378 AM |
649 | switch (code) { |
650 | case MAPLE_RESPONSE_NONE: | |
b233b28e | 651 | maple_response_none(mdev); |
b9482378 AM |
652 | break; |
653 | ||
654 | case MAPLE_RESPONSE_DEVINFO: | |
b233b28e AM |
655 | maple_response_devinfo(mdev, recvbuf); |
656 | atomic_set(&mdev->busy, 0); | |
b9482378 AM |
657 | break; |
658 | ||
659 | case MAPLE_RESPONSE_DATATRF: | |
b233b28e AM |
660 | if (mdev->callback) |
661 | mdev->callback(mq); | |
662 | atomic_set(&mdev->busy, 0); | |
663 | wake_up(&mdev->maple_wait); | |
b9482378 AM |
664 | break; |
665 | ||
666 | case MAPLE_RESPONSE_FILEERR: | |
b233b28e AM |
667 | maple_response_fileerr(mdev, recvbuf); |
668 | atomic_set(&mdev->busy, 0); | |
669 | wake_up(&mdev->maple_wait); | |
670 | break; | |
671 | ||
b9482378 AM |
672 | case MAPLE_RESPONSE_AGAIN: |
673 | case MAPLE_RESPONSE_BADCMD: | |
674 | case MAPLE_RESPONSE_BADFUNC: | |
b233b28e AM |
675 | dev_warn(&mdev->dev, "non-fatal error" |
676 | " 0x%X at (%d, %d)\n", code, | |
677 | mdev->port, mdev->unit); | |
678 | atomic_set(&mdev->busy, 0); | |
b9482378 AM |
679 | break; |
680 | ||
681 | case MAPLE_RESPONSE_ALLINFO: | |
b233b28e AM |
682 | dev_notice(&mdev->dev, "extended" |
683 | " device information request for (%d, %d)" | |
684 | " but call is not supported\n", mdev->port, | |
685 | mdev->unit); | |
686 | atomic_set(&mdev->busy, 0); | |
b9482378 AM |
687 | break; |
688 | ||
689 | case MAPLE_RESPONSE_OK: | |
b233b28e AM |
690 | atomic_set(&mdev->busy, 0); |
691 | wake_up(&mdev->maple_wait); | |
b9482378 AM |
692 | break; |
693 | ||
694 | default: | |
695 | break; | |
696 | } | |
697 | } | |
1795cf48 | 698 | /* if scanning is 1 then we have subdevices to check */ |
b9482378 AM |
699 | if (scanning == 1) { |
700 | maple_send(); | |
701 | scanning = 2; | |
702 | } else | |
703 | scanning = 0; | |
1795cf48 AM |
704 | /*check if we have actually tested all ports yet */ |
705 | if (!fullscan) | |
706 | maple_port_rescan(); | |
707 | /* mark that we have been through the first scan */ | |
b233b28e | 708 | started = 1; |
b9482378 | 709 | } |
b233b28e | 710 | maple_send(); |
17be2d2b AM |
711 | } |
712 | ||
b233b28e | 713 | static irqreturn_t maple_dma_interrupt(int irq, void *dev_id) |
17be2d2b | 714 | { |
b9482378 AM |
715 | /* Load everything into the bottom half */ |
716 | schedule_work(&maple_dma_process); | |
717 | return IRQ_HANDLED; | |
17be2d2b AM |
718 | } |
719 | ||
b233b28e | 720 | static irqreturn_t maple_vblank_interrupt(int irq, void *dev_id) |
17be2d2b | 721 | { |
b9482378 AM |
722 | schedule_work(&maple_vblank_process); |
723 | return IRQ_HANDLED; | |
17be2d2b AM |
724 | } |
725 | ||
17be2d2b AM |
726 | static int maple_set_dma_interrupt_handler(void) |
727 | { | |
b233b28e AM |
728 | return request_irq(HW_EVENT_MAPLE_DMA, maple_dma_interrupt, |
729 | IRQF_SHARED, "maple bus DMA", &maple_unsupported_device); | |
17be2d2b AM |
730 | } |
731 | ||
732 | static int maple_set_vblank_interrupt_handler(void) | |
733 | { | |
b233b28e AM |
734 | return request_irq(HW_EVENT_VSYNC, maple_vblank_interrupt, |
735 | IRQF_SHARED, "maple bus VBLANK", &maple_unsupported_device); | |
17be2d2b AM |
736 | } |
737 | ||
738 | static int maple_get_dma_buffer(void) | |
739 | { | |
b9482378 AM |
740 | maple_sendbuf = |
741 | (void *) __get_free_pages(GFP_KERNEL | __GFP_ZERO, | |
742 | MAPLE_DMA_PAGES); | |
743 | if (!maple_sendbuf) | |
744 | return -ENOMEM; | |
745 | return 0; | |
17be2d2b AM |
746 | } |
747 | ||
b233b28e | 748 | static int maple_match_bus_driver(struct device *devptr, |
b9482378 | 749 | struct device_driver *drvptr) |
17be2d2b | 750 | { |
63870295 PM |
751 | struct maple_driver *maple_drv = to_maple_driver(drvptr); |
752 | struct maple_device *maple_dev = to_maple_dev(devptr); | |
b9482378 | 753 | |
b9482378 AM |
754 | /* Trap empty port case */ |
755 | if (maple_dev->devinfo.function == 0xFFFFFFFF) | |
756 | return 0; | |
757 | else if (maple_dev->devinfo.function & | |
1795cf48 | 758 | cpu_to_be32(maple_drv->function)) |
b9482378 AM |
759 | return 1; |
760 | return 0; | |
17be2d2b AM |
761 | } |
762 | ||
17be2d2b AM |
763 | static void maple_bus_release(struct device *dev) |
764 | { | |
765 | } | |
766 | ||
b233b28e | 767 | static struct maple_driver maple_unsupported_device = { |
b9482378 | 768 | .drv = { |
b233b28e | 769 | .name = "maple_unsupported_device", |
b9482378 | 770 | .bus = &maple_bus_type, |
b3c69e24 | 771 | }, |
17be2d2b | 772 | }; |
ee665ecc | 773 | /* |
b233b28e AM |
774 | * maple_bus_type - core maple bus structure |
775 | */ | |
17be2d2b | 776 | struct bus_type maple_bus_type = { |
b9482378 | 777 | .name = "maple", |
b233b28e | 778 | .match = maple_match_bus_driver, |
17be2d2b AM |
779 | }; |
780 | EXPORT_SYMBOL_GPL(maple_bus_type); | |
781 | ||
782 | static struct device maple_bus = { | |
93fde774 | 783 | .init_name = "maple", |
b9482378 | 784 | .release = maple_bus_release, |
17be2d2b AM |
785 | }; |
786 | ||
787 | static int __init maple_bus_init(void) | |
788 | { | |
b9482378 AM |
789 | int retval, i; |
790 | struct maple_device *mdev[MAPLE_PORTS]; | |
b233b28e | 791 | |
c0537844 | 792 | __raw_writel(0, MAPLE_ENABLE); |
b9482378 AM |
793 | |
794 | retval = device_register(&maple_bus); | |
795 | if (retval) | |
796 | goto cleanup; | |
797 | ||
798 | retval = bus_register(&maple_bus_type); | |
799 | if (retval) | |
800 | goto cleanup_device; | |
801 | ||
b233b28e | 802 | retval = driver_register(&maple_unsupported_device.drv); |
b9482378 AM |
803 | if (retval) |
804 | goto cleanup_bus; | |
805 | ||
806 | /* allocate memory for maple bus dma */ | |
807 | retval = maple_get_dma_buffer(); | |
808 | if (retval) { | |
b233b28e | 809 | dev_err(&maple_bus, "failed to allocate DMA buffers\n"); |
b9482378 AM |
810 | goto cleanup_basic; |
811 | } | |
812 | ||
813 | /* set up DMA interrupt handler */ | |
814 | retval = maple_set_dma_interrupt_handler(); | |
815 | if (retval) { | |
b233b28e AM |
816 | dev_err(&maple_bus, "bus failed to grab maple " |
817 | "DMA IRQ\n"); | |
b9482378 AM |
818 | goto cleanup_dma; |
819 | } | |
820 | ||
821 | /* set up VBLANK interrupt handler */ | |
822 | retval = maple_set_vblank_interrupt_handler(); | |
823 | if (retval) { | |
b233b28e | 824 | dev_err(&maple_bus, "bus failed to grab VBLANK IRQ\n"); |
b9482378 AM |
825 | goto cleanup_irq; |
826 | } | |
827 | ||
b233b28e | 828 | maple_queue_cache = KMEM_CACHE(maple_buffer, SLAB_HWCACHE_ALIGN); |
b9482378 | 829 | |
bde82ee3 LW |
830 | if (!maple_queue_cache) { |
831 | retval = -ENOMEM; | |
b9482378 | 832 | goto cleanup_bothirqs; |
bde82ee3 | 833 | } |
b9482378 | 834 | |
1795cf48 AM |
835 | INIT_LIST_HEAD(&maple_waitq); |
836 | INIT_LIST_HEAD(&maple_sentq); | |
837 | ||
b9482378 AM |
838 | /* setup maple ports */ |
839 | for (i = 0; i < MAPLE_PORTS; i++) { | |
bd496669 | 840 | checked[i] = false; |
b233b28e | 841 | empty[i] = false; |
b9482378 AM |
842 | mdev[i] = maple_alloc_dev(i, 0); |
843 | if (!mdev[i]) { | |
844 | while (i-- > 0) | |
845 | maple_free_dev(mdev[i]); | |
bde82ee3 | 846 | retval = -ENOMEM; |
b9482378 AM |
847 | goto cleanup_cache; |
848 | } | |
b233b28e AM |
849 | baseunits[i] = mdev[i]; |
850 | atomic_set(&mdev[i]->busy, 1); | |
1795cf48 | 851 | maple_add_packet(mdev[i], 0, MAPLE_COMMAND_DEVINFO, 0, NULL); |
b9482378 AM |
852 | subdevice_map[i] = 0; |
853 | } | |
854 | ||
b233b28e AM |
855 | maple_pnp_time = jiffies + HZ; |
856 | /* prepare initial queue */ | |
b9482378 | 857 | maple_send(); |
b233b28e | 858 | dev_info(&maple_bus, "bus core now registered\n"); |
b9482378 AM |
859 | |
860 | return 0; | |
861 | ||
b3c69e24 | 862 | cleanup_cache: |
b9482378 AM |
863 | kmem_cache_destroy(maple_queue_cache); |
864 | ||
b3c69e24 | 865 | cleanup_bothirqs: |
b9482378 AM |
866 | free_irq(HW_EVENT_VSYNC, 0); |
867 | ||
b3c69e24 | 868 | cleanup_irq: |
b9482378 AM |
869 | free_irq(HW_EVENT_MAPLE_DMA, 0); |
870 | ||
b3c69e24 | 871 | cleanup_dma: |
b9482378 AM |
872 | free_pages((unsigned long) maple_sendbuf, MAPLE_DMA_PAGES); |
873 | ||
b3c69e24 | 874 | cleanup_basic: |
b233b28e | 875 | driver_unregister(&maple_unsupported_device.drv); |
b9482378 | 876 | |
b3c69e24 | 877 | cleanup_bus: |
b9482378 AM |
878 | bus_unregister(&maple_bus_type); |
879 | ||
b3c69e24 | 880 | cleanup_device: |
b9482378 AM |
881 | device_unregister(&maple_bus); |
882 | ||
b3c69e24 | 883 | cleanup: |
b233b28e | 884 | printk(KERN_ERR "Maple bus registration failed\n"); |
b9482378 | 885 | return retval; |
17be2d2b | 886 | } |
b3c69e24 AM |
887 | /* Push init to later to ensure hardware gets detected */ |
888 | fs_initcall(maple_bus_init); |