2 * Core maple bus functionality
4 * Copyright (C) 2007, 2008 Adrian McMenamin
6 * Based on 2.4 code by:
8 * Copyright (C) 2000-2001 YAEGASHI Takeshi
9 * Copyright (C) 2001 M. R. Brown
10 * Copyright (C) 2001 Paul Mundt
14 * This file is subject to the terms and conditions of the GNU General Public
15 * License. See the file "COPYING" in the main directory of this archive
18 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <linux/device.h>
21 #include <linux/interrupt.h>
22 #include <linux/list.h>
24 #include <linux/slab.h>
25 #include <linux/maple.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/delay.h>
28 #include <asm/cacheflush.h>
32 #include <mach/sysasic.h>
34 MODULE_AUTHOR("Yaegshi Takeshi, Paul Mundt, M.R. Brown, Adrian McMenamin");
35 MODULE_DESCRIPTION("Maple bus driver for Dreamcast");
36 MODULE_LICENSE("GPL v2");
37 MODULE_SUPPORTED_DEVICE("{{SEGA, Dreamcast/Maple}}");
39 static void maple_dma_handler(struct work_struct *work);
40 static void maple_vblank_handler(struct work_struct *work);
42 static DECLARE_WORK(maple_dma_process, maple_dma_handler);
43 static DECLARE_WORK(maple_vblank_process, maple_vblank_handler);
45 static LIST_HEAD(maple_waitq);
46 static LIST_HEAD(maple_sentq);
48 /* mutex to protect queue of waiting packets */
49 static DEFINE_MUTEX(maple_wlist_lock);
51 static struct maple_driver maple_dummy_driver;
52 static struct device maple_bus;
53 static int subdevice_map[MAPLE_PORTS];
54 static unsigned long *maple_sendbuf, *maple_sendptr, *maple_lastptr;
55 static unsigned long maple_pnp_time;
56 static int started, scanning, fullscan;
57 static struct kmem_cache *maple_queue_cache;
59 struct maple_device_specify {
64 static bool checked[4];
65 static struct maple_device *baseunits[4];
68 * maple_driver_register - register a device driver
69 * automatically makes the driver bus a maple bus
70 * @drv: the driver to be registered
72 int maple_driver_register(struct device_driver *drv)
76 drv->bus = &maple_bus_type;
77 return driver_register(drv);
79 EXPORT_SYMBOL_GPL(maple_driver_register);
81 /* set hardware registers to enable next round of dma */
82 static void maplebus_dma_reset(void)
84 ctrl_outl(MAPLE_MAGIC, MAPLE_RESET);
85 /* set trig type to 0 for software trigger, 1 for hardware (VBLANK) */
86 ctrl_outl(1, MAPLE_TRIGTYPE);
87 ctrl_outl(MAPLE_2MBPS | MAPLE_TIMEOUT(50000), MAPLE_SPEED);
88 ctrl_outl(PHYSADDR(maple_sendbuf), MAPLE_DMAADDR);
89 ctrl_outl(1, MAPLE_ENABLE);
93 * maple_getcond_callback - setup handling MAPLE_COMMAND_GETCOND
94 * @dev: device responding
95 * @callback: handler callback
96 * @interval: interval in jiffies between callbacks
97 * @function: the function code for the device
99 void maple_getcond_callback(struct maple_device *dev,
100 void (*callback) (struct mapleq *mq),
101 unsigned long interval, unsigned long function)
103 dev->callback = callback;
104 dev->interval = interval;
105 dev->function = cpu_to_be32(function);
108 EXPORT_SYMBOL_GPL(maple_getcond_callback);
110 static int maple_dma_done(void)
112 return (ctrl_inl(MAPLE_STATE) & 1) == 0;
115 static void maple_release_device(struct device *dev)
117 struct maple_device *mdev;
121 mdev = to_maple_dev(dev);
125 kmem_cache_free(maple_queue_cache, mq->recvbufdcsp);
133 * maple_add_packet - add a single instruction to the queue
134 * @mdev - maple device
135 * @function - function on device being queried
136 * @command - maple command to add
137 * @length - length of command string (in 32 bit words)
138 * @data - remainder of command string
140 int maple_add_packet(struct maple_device *mdev, u32 function, u32 command,
141 size_t length, void *data)
143 int locking, ret = 0;
144 void *sendbuf = NULL;
146 mutex_lock(&maple_wlist_lock);
147 /* bounce if device already locked */
148 locking = mutex_is_locked(&mdev->mq->mutex);
154 mutex_lock(&mdev->mq->mutex);
157 sendbuf = kmalloc(length * 4, GFP_KERNEL);
159 mutex_unlock(&mdev->mq->mutex);
163 ((__be32 *)sendbuf)[0] = cpu_to_be32(function);
166 mdev->mq->command = command;
167 mdev->mq->length = length;
169 memcpy(sendbuf + 4, data, (length - 1) * 4);
170 mdev->mq->sendbuf = sendbuf;
172 list_add(&mdev->mq->list, &maple_waitq);
174 mutex_unlock(&maple_wlist_lock);
177 EXPORT_SYMBOL_GPL(maple_add_packet);
180 * maple_add_packet_sleeps - add a single instruction to the queue
181 * - waits for lock to be free
182 * @mdev - maple device
183 * @function - function on device being queried
184 * @command - maple command to add
185 * @length - length of command string (in 32 bit words)
186 * @data - remainder of command string
188 int maple_add_packet_sleeps(struct maple_device *mdev, u32 function,
189 u32 command, size_t length, void *data)
191 int locking, ret = 0;
192 void *sendbuf = NULL;
194 locking = mutex_lock_interruptible(&mdev->mq->mutex);
201 sendbuf = kmalloc(length * 4, GFP_KERNEL);
203 mutex_unlock(&mdev->mq->mutex);
207 ((__be32 *)sendbuf)[0] = cpu_to_be32(function);
210 mdev->mq->command = command;
211 mdev->mq->length = length;
213 memcpy(sendbuf + 4, data, (length - 1) * 4);
214 mdev->mq->sendbuf = sendbuf;
216 mutex_lock(&maple_wlist_lock);
217 list_add(&mdev->mq->list, &maple_waitq);
218 mutex_unlock(&maple_wlist_lock);
222 EXPORT_SYMBOL_GPL(maple_add_packet_sleeps);
224 static struct mapleq *maple_allocq(struct maple_device *mdev)
228 mq = kmalloc(sizeof(*mq), GFP_KERNEL);
233 mq->recvbufdcsp = kmem_cache_zalloc(maple_queue_cache, GFP_KERNEL);
234 mq->recvbuf = (void *) P2SEGADDR(mq->recvbufdcsp);
238 * most devices do not need the mutex - but
239 * anything that injects block reads or writes
242 mutex_init(&mq->mutex);
252 static struct maple_device *maple_alloc_dev(int port, int unit)
254 struct maple_device *mdev;
256 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
262 mdev->mq = maple_allocq(mdev);
268 mdev->dev.bus = &maple_bus_type;
269 mdev->dev.parent = &maple_bus;
273 static void maple_free_dev(struct maple_device *mdev)
278 if (mdev->mq->recvbufdcsp)
279 kmem_cache_free(maple_queue_cache,
280 mdev->mq->recvbufdcsp);
286 /* process the command queue into a maple command block
287 * terminating command has bit 32 of first long set to 0
289 static void maple_build_block(struct mapleq *mq)
291 int port, unit, from, to, len;
292 unsigned long *lsendbuf = mq->sendbuf;
294 port = mq->dev->port & 3;
295 unit = mq->dev->unit;
298 to = (port << 6) | (unit > 0 ? (1 << (unit - 1)) & 0x1f : 0x20);
300 *maple_lastptr &= 0x7fffffff;
301 maple_lastptr = maple_sendptr;
303 *maple_sendptr++ = (port << 16) | len | 0x80000000;
304 *maple_sendptr++ = PHYSADDR(mq->recvbuf);
306 mq->command | (to << 8) | (from << 16) | (len << 24);
308 *maple_sendptr++ = *lsendbuf++;
311 /* build up command queue */
312 static void maple_send(void)
314 int i, maple_packets = 0;
315 struct mapleq *mq, *nmq;
317 if (!list_empty(&maple_sentq))
319 mutex_lock(&maple_wlist_lock);
320 if (list_empty(&maple_waitq) || !maple_dma_done()) {
321 mutex_unlock(&maple_wlist_lock);
324 mutex_unlock(&maple_wlist_lock);
325 maple_lastptr = maple_sendbuf;
326 maple_sendptr = maple_sendbuf;
327 mutex_lock(&maple_wlist_lock);
328 list_for_each_entry_safe(mq, nmq, &maple_waitq, list) {
329 maple_build_block(mq);
330 list_move(&mq->list, &maple_sentq);
331 if (maple_packets++ > MAPLE_MAXPACKETS)
334 mutex_unlock(&maple_wlist_lock);
335 if (maple_packets > 0) {
336 for (i = 0; i < (1 << MAPLE_DMA_PAGES); i++)
337 dma_cache_sync(0, maple_sendbuf + i * PAGE_SIZE,
338 PAGE_SIZE, DMA_BIDIRECTIONAL);
342 /* check if there is a driver registered likely to match this device */
343 static int check_matching_maple_driver(struct device_driver *driver,
346 struct maple_driver *maple_drv;
347 struct maple_device *mdev;
350 maple_drv = to_maple_driver(driver);
351 if (mdev->devinfo.function & cpu_to_be32(maple_drv->function))
356 static void maple_detach_driver(struct maple_device *mdev)
360 device_unregister(&mdev->dev);
364 /* process initial MAPLE_COMMAND_DEVINFO for each device or port */
365 static void maple_attach_driver(struct maple_device *mdev)
368 unsigned long function;
371 recvbuf = mdev->mq->recvbuf;
372 /* copy the data as individual elements in
373 * case of memory optimisation */
374 memcpy(&mdev->devinfo.function, recvbuf + 4, 4);
375 memcpy(&mdev->devinfo.function_data[0], recvbuf + 8, 12);
376 memcpy(&mdev->devinfo.area_code, recvbuf + 20, 1);
377 memcpy(&mdev->devinfo.connector_direction, recvbuf + 21, 1);
378 memcpy(&mdev->devinfo.product_name[0], recvbuf + 22, 30);
379 memcpy(&mdev->devinfo.product_licence[0], recvbuf + 52, 60);
380 memcpy(&mdev->devinfo.standby_power, recvbuf + 112, 2);
381 memcpy(&mdev->devinfo.max_power, recvbuf + 114, 2);
382 memcpy(mdev->product_name, mdev->devinfo.product_name, 30);
383 mdev->product_name[30] = '\0';
384 memcpy(mdev->product_licence, mdev->devinfo.product_licence, 60);
385 mdev->product_licence[60] = '\0';
387 for (p = mdev->product_name + 29; mdev->product_name <= p; p--)
392 for (p = mdev->product_licence + 59; mdev->product_licence <= p; p--)
398 printk(KERN_INFO "Maple device detected: %s\n",
400 printk(KERN_INFO "Maple device: %s\n", mdev->product_licence);
402 function = be32_to_cpu(mdev->devinfo.function);
404 if (function > 0x200) {
405 /* Do this silently - as not a real device */
407 mdev->driver = &maple_dummy_driver;
408 sprintf(mdev->dev.bus_id, "%d:0.port", mdev->port);
411 "Maple bus at (%d, %d): Function 0x%lX\n",
412 mdev->port, mdev->unit, function);
415 bus_for_each_drv(&maple_bus_type, NULL, mdev,
416 check_matching_maple_driver);
419 /* Driver does not exist yet */
421 "No maple driver found.\n");
422 mdev->driver = &maple_dummy_driver;
424 sprintf(mdev->dev.bus_id, "%d:0%d.%lX", mdev->port,
425 mdev->unit, function);
427 mdev->function = function;
428 mdev->dev.release = &maple_release_device;
429 retval = device_register(&mdev->dev);
432 "Maple bus: Attempt to register device"
433 " (%x, %x) failed.\n",
434 mdev->port, mdev->unit);
435 maple_free_dev(mdev);
442 * if device has been registered for the given
443 * port and unit then return 1 - allows identification
444 * of which devices need to be attached or detached
446 static int detach_maple_device(struct device *device, void *portptr)
448 struct maple_device_specify *ds;
449 struct maple_device *mdev;
452 mdev = to_maple_dev(device);
453 if (mdev->port == ds->port && mdev->unit == ds->unit)
458 static int setup_maple_commands(struct device *device, void *ignored)
461 struct maple_device *maple_dev = to_maple_dev(device);
463 if ((maple_dev->interval > 0)
464 && time_after(jiffies, maple_dev->when)) {
465 /* bounce if we cannot lock */
466 add = maple_add_packet(maple_dev,
467 be32_to_cpu(maple_dev->devinfo.function),
468 MAPLE_COMMAND_GETCOND, 1, NULL);
470 maple_dev->when = jiffies + maple_dev->interval;
472 if (time_after(jiffies, maple_pnp_time))
473 /* This will also bounce */
474 maple_add_packet(maple_dev, 0,
475 MAPLE_COMMAND_DEVINFO, 0, NULL);
480 /* VBLANK bottom half - implemented via workqueue */
481 static void maple_vblank_handler(struct work_struct *work)
483 if (!list_empty(&maple_sentq) || !maple_dma_done())
486 ctrl_outl(0, MAPLE_ENABLE);
488 bus_for_each_dev(&maple_bus_type, NULL, NULL,
489 setup_maple_commands);
491 if (time_after(jiffies, maple_pnp_time))
492 maple_pnp_time = jiffies + MAPLE_PNP_INTERVAL;
494 mutex_lock(&maple_wlist_lock);
495 if (!list_empty(&maple_waitq) && list_empty(&maple_sentq)) {
496 mutex_unlock(&maple_wlist_lock);
499 mutex_unlock(&maple_wlist_lock);
502 maplebus_dma_reset();
505 /* handle devices added via hotplugs - placing them on queue for DEVINFO*/
506 static void maple_map_subunits(struct maple_device *mdev, int submask)
508 int retval, k, devcheck;
509 struct maple_device *mdev_add;
510 struct maple_device_specify ds;
512 ds.port = mdev->port;
513 for (k = 0; k < 5; k++) {
516 bus_for_each_dev(&maple_bus_type, NULL, &ds,
517 detach_maple_device);
519 submask = submask >> 1;
522 devcheck = submask & 0x01;
524 mdev_add = maple_alloc_dev(mdev->port, k + 1);
527 maple_add_packet(mdev_add, 0, MAPLE_COMMAND_DEVINFO,
529 /* mark that we are checking sub devices */
532 submask = submask >> 1;
536 /* mark a device as removed */
537 static void maple_clean_submap(struct maple_device *mdev)
541 killbit = (mdev->unit > 0 ? (1 << (mdev->unit - 1)) & 0x1f : 0x20);
544 subdevice_map[mdev->port] = subdevice_map[mdev->port] & killbit;
547 /* handle empty port or hotplug removal */
548 static void maple_response_none(struct maple_device *mdev,
551 if (mdev->unit != 0) {
553 maple_clean_submap(mdev);
555 "Maple bus device detaching at (%d, %d)\n",
556 mdev->port, mdev->unit);
557 maple_detach_driver(mdev);
560 if (!started || !fullscan) {
561 if (checked[mdev->port] == false) {
562 checked[mdev->port] = true;
563 printk(KERN_INFO "No maple devices attached"
564 " to port %d\n", mdev->port);
568 maple_clean_submap(mdev);
571 /* preprocess hotplugs or scans */
572 static void maple_response_devinfo(struct maple_device *mdev,
576 if (!started || (scanning == 2) || !fullscan) {
577 if ((mdev->unit == 0) && (checked[mdev->port] == false)) {
578 checked[mdev->port] = true;
579 maple_attach_driver(mdev);
582 maple_attach_driver(mdev);
586 if (mdev->unit == 0) {
587 submask = recvbuf[2] & 0x1F;
588 if (submask ^ subdevice_map[mdev->port]) {
589 maple_map_subunits(mdev, submask);
590 subdevice_map[mdev->port] = submask;
595 static void maple_port_rescan(void)
598 struct maple_device *mdev;
601 for (i = 0; i < MAPLE_PORTS; i++) {
602 if (checked[i] == false) {
606 * test lock in case scan has failed
607 * but device is still locked
609 if (mutex_is_locked(&mdev->mq->mutex))
610 mutex_unlock(&mdev->mq->mutex);
611 maple_add_packet(mdev, 0, MAPLE_COMMAND_DEVINFO,
617 /* maple dma end bottom half - implemented via workqueue */
618 static void maple_dma_handler(struct work_struct *work)
620 struct mapleq *mq, *nmq;
621 struct maple_device *dev;
623 enum maple_code code;
625 if (!maple_dma_done())
627 ctrl_outl(0, MAPLE_ENABLE);
628 if (!list_empty(&maple_sentq)) {
629 list_for_each_entry_safe(mq, nmq, &maple_sentq, list) {
630 recvbuf = mq->recvbuf;
634 mutex_unlock(&mq->mutex);
635 list_del_init(&mq->list);
638 case MAPLE_RESPONSE_NONE:
639 maple_response_none(dev, mq);
642 case MAPLE_RESPONSE_DEVINFO:
643 maple_response_devinfo(dev, recvbuf);
646 case MAPLE_RESPONSE_DATATRF:
651 case MAPLE_RESPONSE_FILEERR:
652 case MAPLE_RESPONSE_AGAIN:
653 case MAPLE_RESPONSE_BADCMD:
654 case MAPLE_RESPONSE_BADFUNC:
656 "Maple non-fatal error 0x%X\n",
660 case MAPLE_RESPONSE_ALLINFO:
662 "Maple - extended device information"
666 case MAPLE_RESPONSE_OK:
673 /* if scanning is 1 then we have subdevices to check */
679 /*check if we have actually tested all ports yet */
682 /* mark that we have been through the first scan */
686 maplebus_dma_reset();
689 static irqreturn_t maplebus_dma_interrupt(int irq, void *dev_id)
691 /* Load everything into the bottom half */
692 schedule_work(&maple_dma_process);
696 static irqreturn_t maplebus_vblank_interrupt(int irq, void *dev_id)
698 schedule_work(&maple_vblank_process);
702 static int maple_set_dma_interrupt_handler(void)
704 return request_irq(HW_EVENT_MAPLE_DMA, maplebus_dma_interrupt,
705 IRQF_SHARED, "maple bus DMA", &maple_dummy_driver);
708 static int maple_set_vblank_interrupt_handler(void)
710 return request_irq(HW_EVENT_VSYNC, maplebus_vblank_interrupt,
711 IRQF_SHARED, "maple bus VBLANK", &maple_dummy_driver);
714 static int maple_get_dma_buffer(void)
717 (void *) __get_free_pages(GFP_KERNEL | __GFP_ZERO,
724 static int match_maple_bus_driver(struct device *devptr,
725 struct device_driver *drvptr)
727 struct maple_driver *maple_drv;
728 struct maple_device *maple_dev;
730 maple_drv = container_of(drvptr, struct maple_driver, drv);
731 maple_dev = container_of(devptr, struct maple_device, dev);
732 /* Trap empty port case */
733 if (maple_dev->devinfo.function == 0xFFFFFFFF)
735 else if (maple_dev->devinfo.function &
736 cpu_to_be32(maple_drv->function))
741 static int maple_bus_uevent(struct device *dev,
742 struct kobj_uevent_env *env)
747 static void maple_bus_release(struct device *dev)
751 static struct maple_driver maple_dummy_driver = {
753 .name = "maple_dummy_driver",
754 .bus = &maple_bus_type,
758 struct bus_type maple_bus_type = {
760 .match = match_maple_bus_driver,
761 .uevent = maple_bus_uevent,
763 EXPORT_SYMBOL_GPL(maple_bus_type);
765 static struct device maple_bus = {
767 .release = maple_bus_release,
770 static int __init maple_bus_init(void)
773 struct maple_device *mdev[MAPLE_PORTS];
774 ctrl_outl(0, MAPLE_STATE);
776 retval = device_register(&maple_bus);
780 retval = bus_register(&maple_bus_type);
784 retval = driver_register(&maple_dummy_driver.drv);
788 /* allocate memory for maple bus dma */
789 retval = maple_get_dma_buffer();
792 "Maple bus: Failed to allocate Maple DMA buffers\n");
796 /* set up DMA interrupt handler */
797 retval = maple_set_dma_interrupt_handler();
800 "Maple bus: Failed to grab maple DMA IRQ\n");
804 /* set up VBLANK interrupt handler */
805 retval = maple_set_vblank_interrupt_handler();
807 printk(KERN_INFO "Maple bus: Failed to grab VBLANK IRQ\n");
812 kmem_cache_create("maple_queue_cache", 0x400, 0,
813 SLAB_POISON|SLAB_HWCACHE_ALIGN, NULL);
815 if (!maple_queue_cache)
816 goto cleanup_bothirqs;
818 INIT_LIST_HEAD(&maple_waitq);
819 INIT_LIST_HEAD(&maple_sentq);
821 /* setup maple ports */
822 for (i = 0; i < MAPLE_PORTS; i++) {
824 mdev[i] = maple_alloc_dev(i, 0);
825 baseunits[i] = mdev[i];
828 maple_free_dev(mdev[i]);
831 maple_add_packet(mdev[i], 0, MAPLE_COMMAND_DEVINFO, 0, NULL);
832 subdevice_map[i] = 0;
835 /* setup maplebus hardware */
836 maplebus_dma_reset();
837 /* initial detection */
839 maple_pnp_time = jiffies;
840 printk(KERN_INFO "Maple bus core now registered.\n");
845 kmem_cache_destroy(maple_queue_cache);
848 free_irq(HW_EVENT_VSYNC, 0);
851 free_irq(HW_EVENT_MAPLE_DMA, 0);
854 free_pages((unsigned long) maple_sendbuf, MAPLE_DMA_PAGES);
857 driver_unregister(&maple_dummy_driver.drv);
860 bus_unregister(&maple_bus_type);
863 device_unregister(&maple_bus);
866 printk(KERN_INFO "Maple bus registration failed\n");
869 /* Push init to later to ensure hardware gets detected */
870 fs_initcall(maple_bus_init);