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 <asm/cacheflush.h>
30 #include <asm/mach/dma.h>
31 #include <asm/mach/sysasic.h>
32 #include <asm/mach/maple.h>
33 #include <linux/delay.h>
35 MODULE_AUTHOR("Yaegshi Takeshi, Paul Mundt, M.R. Brown, Adrian McMenamin");
36 MODULE_DESCRIPTION("Maple bus driver for Dreamcast");
37 MODULE_LICENSE("GPL v2");
38 MODULE_SUPPORTED_DEVICE("{{SEGA, Dreamcast/Maple}}");
40 static void maple_dma_handler(struct work_struct
*work
);
41 static void maple_vblank_handler(struct work_struct
*work
);
43 static DECLARE_WORK(maple_dma_process
, maple_dma_handler
);
44 static DECLARE_WORK(maple_vblank_process
, maple_vblank_handler
);
46 static LIST_HEAD(maple_waitq
);
47 static LIST_HEAD(maple_sentq
);
49 static DEFINE_MUTEX(maple_list_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
, liststatus
, 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 * @mq: instruction to add to waiting queue
136 void maple_add_packet(struct mapleq
*mq
)
138 mutex_lock(&maple_list_lock
);
139 list_add(&mq
->list
, &maple_waitq
);
140 mutex_unlock(&maple_list_lock
);
142 EXPORT_SYMBOL_GPL(maple_add_packet
);
144 static struct mapleq
*maple_allocq(struct maple_device
*mdev
)
148 mq
= kmalloc(sizeof(*mq
), GFP_KERNEL
);
153 mq
->recvbufdcsp
= kmem_cache_zalloc(maple_queue_cache
, GFP_KERNEL
);
154 mq
->recvbuf
= (void *) P2SEGADDR(mq
->recvbufdcsp
);
163 static struct maple_device
*maple_alloc_dev(int port
, int unit
)
165 struct maple_device
*mdev
;
167 mdev
= kzalloc(sizeof(*mdev
), GFP_KERNEL
);
173 mdev
->mq
= maple_allocq(mdev
);
179 mdev
->dev
.bus
= &maple_bus_type
;
180 mdev
->dev
.parent
= &maple_bus
;
185 static void maple_free_dev(struct maple_device
*mdev
)
190 if (mdev
->mq
->recvbufdcsp
)
191 kmem_cache_free(maple_queue_cache
,
192 mdev
->mq
->recvbufdcsp
);
198 /* process the command queue into a maple command block
199 * terminating command has bit 32 of first long set to 0
201 static void maple_build_block(struct mapleq
*mq
)
203 int port
, unit
, from
, to
, len
;
204 unsigned long *lsendbuf
= mq
->sendbuf
;
206 port
= mq
->dev
->port
& 3;
207 unit
= mq
->dev
->unit
;
210 to
= (port
<< 6) | (unit
> 0 ? (1 << (unit
- 1)) & 0x1f : 0x20);
212 *maple_lastptr
&= 0x7fffffff;
213 maple_lastptr
= maple_sendptr
;
215 *maple_sendptr
++ = (port
<< 16) | len
| 0x80000000;
216 *maple_sendptr
++ = PHYSADDR(mq
->recvbuf
);
218 mq
->command
| (to
<< 8) | (from
<< 16) | (len
<< 24);
221 *maple_sendptr
++ = *lsendbuf
++;
224 /* build up command queue */
225 static void maple_send(void)
229 struct mapleq
*mq
, *nmq
;
231 if (!list_empty(&maple_sentq
))
233 if (list_empty(&maple_waitq
) || !maple_dma_done())
236 maple_sendptr
= maple_lastptr
= maple_sendbuf
;
237 list_for_each_entry_safe(mq
, nmq
, &maple_waitq
, list
) {
238 maple_build_block(mq
);
239 list_move(&mq
->list
, &maple_sentq
);
240 if (maple_packets
++ > MAPLE_MAXPACKETS
)
243 if (maple_packets
> 0) {
244 for (i
= 0; i
< (1 << MAPLE_DMA_PAGES
); i
++)
245 dma_cache_sync(0, maple_sendbuf
+ i
* PAGE_SIZE
,
246 PAGE_SIZE
, DMA_BIDIRECTIONAL
);
250 static int attach_matching_maple_driver(struct device_driver
*driver
,
253 struct maple_driver
*maple_drv
;
254 struct maple_device
*mdev
;
257 maple_drv
= to_maple_driver(driver
);
258 if (mdev
->devinfo
.function
& be32_to_cpu(maple_drv
->function
)) {
259 if (maple_drv
->connect(mdev
) == 0) {
260 mdev
->driver
= maple_drv
;
267 static void maple_detach_driver(struct maple_device
*mdev
)
272 if (mdev
->driver
->disconnect
)
273 mdev
->driver
->disconnect(mdev
);
276 device_unregister(&mdev
->dev
);
280 /* process initial MAPLE_COMMAND_DEVINFO for each device or port */
281 static void maple_attach_driver(struct maple_device
*mdev
)
284 unsigned long function
;
287 recvbuf
= mdev
->mq
->recvbuf
;
288 /* copy the data as individual elements in
289 * case of memory optimisation */
290 memcpy(&mdev
->devinfo
.function
, recvbuf
+ 4, 4);
291 memcpy(&mdev
->devinfo
.function_data
[0], recvbuf
+ 8, 12);
292 memcpy(&mdev
->devinfo
.area_code
, recvbuf
+ 20, 1);
293 memcpy(&mdev
->devinfo
.connector_direction
, recvbuf
+ 21, 1);
294 memcpy(&mdev
->devinfo
.product_name
[0], recvbuf
+ 22, 30);
295 memcpy(&mdev
->devinfo
.product_licence
[0], recvbuf
+ 52, 60);
296 memcpy(&mdev
->devinfo
.standby_power
, recvbuf
+ 112, 2);
297 memcpy(&mdev
->devinfo
.max_power
, recvbuf
+ 114, 2);
298 memcpy(mdev
->product_name
, mdev
->devinfo
.product_name
, 30);
299 mdev
->product_name
[30] = '\0';
300 memcpy(mdev
->product_licence
, mdev
->devinfo
.product_licence
, 60);
301 mdev
->product_licence
[60] = '\0';
303 for (p
= mdev
->product_name
+ 29; mdev
->product_name
<= p
; p
--)
308 for (p
= mdev
->product_licence
+ 59; mdev
->product_licence
<= p
; p
--)
314 printk(KERN_INFO
"Maple device detected: %s\n",
316 printk(KERN_INFO
"Maple device: %s\n", mdev
->product_licence
);
318 function
= be32_to_cpu(mdev
->devinfo
.function
);
320 if (function
> 0x200) {
321 /* Do this silently - as not a real device */
323 mdev
->driver
= &maple_dummy_driver
;
324 sprintf(mdev
->dev
.bus_id
, "%d:0.port", mdev
->port
);
327 "Maple bus at (%d, %d): Function 0x%lX\n",
328 mdev
->port
, mdev
->unit
, function
);
331 bus_for_each_drv(&maple_bus_type
, NULL
, mdev
,
332 attach_matching_maple_driver
);
335 /* Driver does not exist yet */
337 "No maple driver found.\n");
338 mdev
->driver
= &maple_dummy_driver
;
340 sprintf(mdev
->dev
.bus_id
, "%d:0%d.%lX", mdev
->port
,
341 mdev
->unit
, function
);
343 mdev
->function
= function
;
344 mdev
->dev
.release
= &maple_release_device
;
345 retval
= device_register(&mdev
->dev
);
348 "Maple bus: Attempt to register device"
349 " (%x, %x) failed.\n",
350 mdev
->port
, mdev
->unit
);
351 maple_free_dev(mdev
);
358 * if device has been registered for the given
359 * port and unit then return 1 - allows identification
360 * of which devices need to be attached or detached
362 static int detach_maple_device(struct device
*device
, void *portptr
)
364 struct maple_device_specify
*ds
;
365 struct maple_device
*mdev
;
368 mdev
= to_maple_dev(device
);
369 if (mdev
->port
== ds
->port
&& mdev
->unit
== ds
->unit
)
374 static int setup_maple_commands(struct device
*device
, void *ignored
)
376 struct maple_device
*maple_dev
= to_maple_dev(device
);
378 if ((maple_dev
->interval
> 0)
379 && time_after(jiffies
, maple_dev
->when
)) {
380 maple_dev
->when
= jiffies
+ maple_dev
->interval
;
381 maple_dev
->mq
->command
= MAPLE_COMMAND_GETCOND
;
382 maple_dev
->mq
->sendbuf
= &maple_dev
->function
;
383 maple_dev
->mq
->length
= 1;
384 maple_add_packet(maple_dev
->mq
);
387 if (time_after(jiffies
, maple_pnp_time
)) {
388 maple_dev
->mq
->command
= MAPLE_COMMAND_DEVINFO
;
389 maple_dev
->mq
->length
= 0;
390 maple_add_packet(maple_dev
->mq
);
398 /* VBLANK bottom half - implemented via workqueue */
399 static void maple_vblank_handler(struct work_struct
*work
)
401 if (!maple_dma_done())
403 if (!list_empty(&maple_sentq
))
405 ctrl_outl(0, MAPLE_ENABLE
);
407 bus_for_each_dev(&maple_bus_type
, NULL
, NULL
,
408 setup_maple_commands
);
409 if (time_after(jiffies
, maple_pnp_time
))
410 maple_pnp_time
= jiffies
+ MAPLE_PNP_INTERVAL
;
411 if (liststatus
&& list_empty(&maple_sentq
)) {
412 INIT_LIST_HEAD(&maple_sentq
);
415 maplebus_dma_reset();
418 /* handle devices added via hotplugs - placing them on queue for DEVINFO*/
419 static void maple_map_subunits(struct maple_device
*mdev
, int submask
)
421 int retval
, k
, devcheck
;
422 struct maple_device
*mdev_add
;
423 struct maple_device_specify ds
;
425 for (k
= 0; k
< 5; k
++) {
426 ds
.port
= mdev
->port
;
429 bus_for_each_dev(&maple_bus_type
, NULL
, &ds
,
430 detach_maple_device
);
432 submask
= submask
>> 1;
435 devcheck
= submask
& 0x01;
437 mdev_add
= maple_alloc_dev(mdev
->port
, k
+ 1);
440 mdev_add
->mq
->command
= MAPLE_COMMAND_DEVINFO
;
441 mdev_add
->mq
->length
= 0;
442 maple_add_packet(mdev_add
->mq
);
445 submask
= submask
>> 1;
449 /* mark a device as removed */
450 static void maple_clean_submap(struct maple_device
*mdev
)
454 killbit
= (mdev
->unit
> 0 ? (1 << (mdev
->unit
- 1)) & 0x1f : 0x20);
457 subdevice_map
[mdev
->port
] = subdevice_map
[mdev
->port
] & killbit
;
460 /* handle empty port or hotplug removal */
461 static void maple_response_none(struct maple_device
*mdev
,
464 if (mdev
->unit
!= 0) {
466 maple_clean_submap(mdev
);
468 "Maple bus device detaching at (%d, %d)\n",
469 mdev
->port
, mdev
->unit
);
470 maple_detach_driver(mdev
);
473 if (!started
|| !fullscan
) {
474 if (checked
[mdev
->port
] == false) {
475 checked
[mdev
->port
] = true;
476 printk(KERN_INFO
"No maple devices attached"
477 " to port %d\n", mdev
->port
);
481 maple_clean_submap(mdev
);
484 /* preprocess hotplugs or scans */
485 static void maple_response_devinfo(struct maple_device
*mdev
,
489 if (!started
|| (scanning
== 2) || !fullscan
) {
490 if ((mdev
->unit
== 0) && (checked
[mdev
->port
] == false)) {
491 checked
[mdev
->port
] = true;
492 maple_attach_driver(mdev
);
495 maple_attach_driver(mdev
);
499 if (mdev
->unit
== 0) {
500 submask
= recvbuf
[2] & 0x1F;
501 if (submask
^ subdevice_map
[mdev
->port
]) {
502 maple_map_subunits(mdev
, submask
);
503 subdevice_map
[mdev
->port
] = submask
;
508 /* maple dma end bottom half - implemented via workqueue */
509 static void maple_dma_handler(struct work_struct
*work
)
511 struct mapleq
*mq
, *nmq
;
512 struct maple_device
*dev
;
514 enum maple_code code
;
517 if (!maple_dma_done())
519 ctrl_outl(0, MAPLE_ENABLE
);
520 if (!list_empty(&maple_sentq
)) {
521 list_for_each_entry_safe(mq
, nmq
, &maple_sentq
, list
) {
522 recvbuf
= mq
->recvbuf
;
526 case MAPLE_RESPONSE_NONE
:
527 maple_response_none(dev
, mq
);
530 case MAPLE_RESPONSE_DEVINFO
:
531 maple_response_devinfo(dev
, recvbuf
);
534 case MAPLE_RESPONSE_DATATRF
:
539 case MAPLE_RESPONSE_FILEERR
:
540 case MAPLE_RESPONSE_AGAIN
:
541 case MAPLE_RESPONSE_BADCMD
:
542 case MAPLE_RESPONSE_BADFUNC
:
544 "Maple non-fatal error 0x%X\n",
548 case MAPLE_RESPONSE_ALLINFO
:
550 "Maple - extended device information"
554 case MAPLE_RESPONSE_OK
:
561 INIT_LIST_HEAD(&maple_sentq
);
570 for (i
= 0; i
< MAPLE_PORTS
; i
++) {
571 if (checked
[i
] == false) {
575 MAPLE_COMMAND_DEVINFO
;
577 maple_add_packet(dev
->mq
);
584 maplebus_dma_reset();
587 static irqreturn_t
maplebus_dma_interrupt(int irq
, void *dev_id
)
589 /* Load everything into the bottom half */
590 schedule_work(&maple_dma_process
);
594 static irqreturn_t
maplebus_vblank_interrupt(int irq
, void *dev_id
)
596 schedule_work(&maple_vblank_process
);
600 static int maple_set_dma_interrupt_handler(void)
602 return request_irq(HW_EVENT_MAPLE_DMA
, maplebus_dma_interrupt
,
603 IRQF_SHARED
, "maple bus DMA", &maple_dummy_driver
);
606 static int maple_set_vblank_interrupt_handler(void)
608 return request_irq(HW_EVENT_VSYNC
, maplebus_vblank_interrupt
,
609 IRQF_SHARED
, "maple bus VBLANK", &maple_dummy_driver
);
612 static int maple_get_dma_buffer(void)
615 (void *) __get_free_pages(GFP_KERNEL
| __GFP_ZERO
,
622 static int match_maple_bus_driver(struct device
*devptr
,
623 struct device_driver
*drvptr
)
625 struct maple_driver
*maple_drv
;
626 struct maple_device
*maple_dev
;
628 maple_drv
= container_of(drvptr
, struct maple_driver
, drv
);
629 maple_dev
= container_of(devptr
, struct maple_device
, dev
);
630 /* Trap empty port case */
631 if (maple_dev
->devinfo
.function
== 0xFFFFFFFF)
633 else if (maple_dev
->devinfo
.function
&
634 be32_to_cpu(maple_drv
->function
))
639 static int maple_bus_uevent(struct device
*dev
,
640 struct kobj_uevent_env
*env
)
645 static void maple_bus_release(struct device
*dev
)
649 static struct maple_driver maple_dummy_driver
= {
651 .name
= "maple_dummy_driver",
652 .bus
= &maple_bus_type
,
656 struct bus_type maple_bus_type
= {
658 .match
= match_maple_bus_driver
,
659 .uevent
= maple_bus_uevent
,
661 EXPORT_SYMBOL_GPL(maple_bus_type
);
663 static struct device maple_bus
= {
665 .release
= maple_bus_release
,
668 static int __init
maple_bus_init(void)
671 struct maple_device
*mdev
[MAPLE_PORTS
];
672 ctrl_outl(0, MAPLE_STATE
);
674 retval
= device_register(&maple_bus
);
678 retval
= bus_register(&maple_bus_type
);
682 retval
= driver_register(&maple_dummy_driver
.drv
);
686 /* allocate memory for maple bus dma */
687 retval
= maple_get_dma_buffer();
690 "Maple bus: Failed to allocate Maple DMA buffers\n");
694 /* set up DMA interrupt handler */
695 retval
= maple_set_dma_interrupt_handler();
698 "Maple bus: Failed to grab maple DMA IRQ\n");
702 /* set up VBLANK interrupt handler */
703 retval
= maple_set_vblank_interrupt_handler();
705 printk(KERN_INFO
"Maple bus: Failed to grab VBLANK IRQ\n");
710 kmem_cache_create("maple_queue_cache", 0x400, 0,
711 SLAB_POISON
|SLAB_HWCACHE_ALIGN
, NULL
);
713 if (!maple_queue_cache
)
714 goto cleanup_bothirqs
;
716 /* setup maple ports */
717 for (i
= 0; i
< MAPLE_PORTS
; i
++) {
719 mdev
[i
] = maple_alloc_dev(i
, 0);
720 baseunits
[i
] = mdev
[i
];
723 maple_free_dev(mdev
[i
]);
726 mdev
[i
]->mq
->command
= MAPLE_COMMAND_DEVINFO
;
727 mdev
[i
]->mq
->length
= 0;
728 maple_add_packet(mdev
[i
]->mq
);
729 subdevice_map
[i
] = 0;
732 /* setup maplebus hardware */
733 maplebus_dma_reset();
734 /* initial detection */
736 maple_pnp_time
= jiffies
;
737 printk(KERN_INFO
"Maple bus core now registered.\n");
742 kmem_cache_destroy(maple_queue_cache
);
745 free_irq(HW_EVENT_VSYNC
, 0);
748 free_irq(HW_EVENT_MAPLE_DMA
, 0);
751 free_pages((unsigned long) maple_sendbuf
, MAPLE_DMA_PAGES
);
754 driver_unregister(&maple_dummy_driver
.drv
);
757 bus_unregister(&maple_bus_type
);
760 device_unregister(&maple_bus
);
763 printk(KERN_INFO
"Maple bus registration failed\n");
766 /* Push init to later to ensure hardware gets detected */
767 fs_initcall(maple_bus_init
);