blob: 9c48ccc44c295bbba25a9ea01f81695d7c592c95 [file] [log] [blame]
Adrian McMenamin17be2d22007-09-21 15:55:55 +09001/*
2 * Core maple bus functionality
3 *
4 * Copyright (C) 2007 Adrian McMenamin
5 *
6 * Based on 2.4 code by:
7 *
8 * Copyright (C) 2000-2001 YAEGASHI Takeshi
9 * Copyright (C) 2001 M. R. Brown
10 * Copyright (C) 2001 Paul Mundt
11 *
12 * and others.
13 *
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
16 * for more details.
17 */
18#include <linux/init.h>
19#include <linux/kernel.h>
20#include <linux/device.h>
21#include <linux/module.h>
22#include <linux/interrupt.h>
23#include <linux/list.h>
24#include <linux/io.h>
25#include <linux/slab.h>
26#include <linux/maple.h>
27#include <linux/dma-mapping.h>
28#include <asm/cacheflush.h>
29#include <asm/dma.h>
30#include <asm/io.h>
31#include <asm/mach/dma.h>
32#include <asm/mach/sysasic.h>
33#include <asm/mach/maple.h>
34
35MODULE_AUTHOR("Yaegshi Takeshi, Paul Mundt, M.R. Brown, Adrian McMenamin");
36MODULE_DESCRIPTION("Maple bus driver for Dreamcast");
37MODULE_LICENSE("GPL v2");
38MODULE_SUPPORTED_DEVICE("{{SEGA, Dreamcast/Maple}}");
39
40static void maple_dma_handler(struct work_struct *work);
41static void maple_vblank_handler(struct work_struct *work);
42
43static DECLARE_WORK(maple_dma_process, maple_dma_handler);
44static DECLARE_WORK(maple_vblank_process, maple_vblank_handler);
45
46static LIST_HEAD(maple_waitq);
47static LIST_HEAD(maple_sentq);
48
49static DEFINE_MUTEX(maple_list_lock);
50
51static struct maple_driver maple_dummy_driver;
52static struct device maple_bus;
53static int subdevice_map[MAPLE_PORTS];
54static unsigned long *maple_sendbuf, *maple_sendptr, *maple_lastptr;
55static unsigned long maple_pnp_time;
56static int started, scanning, liststatus;
57static struct kmem_cache *maple_queue_cache;
58
59struct maple_device_specify {
Adrian McMenaminb9482372008-02-06 22:46:21 +000060 int port;
61 int unit;
Adrian McMenamin17be2d22007-09-21 15:55:55 +090062};
63
64/**
65 * maple_driver_register - register a device driver
66 * automatically makes the driver bus a maple bus
67 * @drv: the driver to be registered
68 */
69int maple_driver_register(struct device_driver *drv)
70{
Adrian McMenaminb9482372008-02-06 22:46:21 +000071 if (!drv)
72 return -EINVAL;
73 drv->bus = &maple_bus_type;
74 return driver_register(drv);
Adrian McMenamin17be2d22007-09-21 15:55:55 +090075}
Adrian McMenaminb9482372008-02-06 22:46:21 +000076
Adrian McMenamin17be2d22007-09-21 15:55:55 +090077EXPORT_SYMBOL_GPL(maple_driver_register);
78
79/* set hardware registers to enable next round of dma */
80static void maplebus_dma_reset(void)
81{
Adrian McMenaminb9482372008-02-06 22:46:21 +000082 ctrl_outl(MAPLE_MAGIC, MAPLE_RESET);
83 /* set trig type to 0 for software trigger, 1 for hardware (VBLANK) */
84 ctrl_outl(1, MAPLE_TRIGTYPE);
85 ctrl_outl(MAPLE_2MBPS | MAPLE_TIMEOUT(50000), MAPLE_SPEED);
86 ctrl_outl(PHYSADDR(maple_sendbuf), MAPLE_DMAADDR);
87 ctrl_outl(1, MAPLE_ENABLE);
Adrian McMenamin17be2d22007-09-21 15:55:55 +090088}
89
90/**
91 * maple_getcond_callback - setup handling MAPLE_COMMAND_GETCOND
92 * @dev: device responding
93 * @callback: handler callback
94 * @interval: interval in jiffies between callbacks
95 * @function: the function code for the device
96 */
97void maple_getcond_callback(struct maple_device *dev,
Adrian McMenaminb9482372008-02-06 22:46:21 +000098 void (*callback) (struct mapleq * mq),
99 unsigned long interval, unsigned long function)
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900100{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000101 dev->callback = callback;
102 dev->interval = interval;
103 dev->function = cpu_to_be32(function);
104 dev->when = jiffies;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900105}
Adrian McMenaminb9482372008-02-06 22:46:21 +0000106
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900107EXPORT_SYMBOL_GPL(maple_getcond_callback);
108
109static int maple_dma_done(void)
110{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000111 return (ctrl_inl(MAPLE_STATE) & 1) == 0;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900112}
113
114static void maple_release_device(struct device *dev)
115{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000116 if (dev->type) {
117 kfree(dev->type->name);
118 kfree(dev->type);
119 }
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900120}
121
122/**
123 * maple_add_packet - add a single instruction to the queue
124 * @mq: instruction to add to waiting queue
125 */
126void maple_add_packet(struct mapleq *mq)
127{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000128 mutex_lock(&maple_list_lock);
129 list_add(&mq->list, &maple_waitq);
130 mutex_unlock(&maple_list_lock);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900131}
Adrian McMenaminb9482372008-02-06 22:46:21 +0000132
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900133EXPORT_SYMBOL_GPL(maple_add_packet);
134
135static struct mapleq *maple_allocq(struct maple_device *dev)
136{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000137 struct mapleq *mq;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900138
Adrian McMenaminb9482372008-02-06 22:46:21 +0000139 mq = kmalloc(sizeof(*mq), GFP_KERNEL);
140 if (!mq)
141 return NULL;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900142
Adrian McMenaminb9482372008-02-06 22:46:21 +0000143 mq->dev = dev;
144 mq->recvbufdcsp = kmem_cache_zalloc(maple_queue_cache, GFP_KERNEL);
145 mq->recvbuf = (void *) P2SEGADDR(mq->recvbufdcsp);
146 if (!mq->recvbuf) {
147 kfree(mq);
148 return NULL;
149 }
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900150
Adrian McMenaminb9482372008-02-06 22:46:21 +0000151 return mq;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900152}
153
154static struct maple_device *maple_alloc_dev(int port, int unit)
155{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000156 struct maple_device *dev;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900157
Adrian McMenaminb9482372008-02-06 22:46:21 +0000158 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
159 if (!dev)
160 return NULL;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900161
Adrian McMenaminb9482372008-02-06 22:46:21 +0000162 dev->port = port;
163 dev->unit = unit;
164 dev->mq = maple_allocq(dev);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900165
Adrian McMenaminb9482372008-02-06 22:46:21 +0000166 if (!dev->mq) {
167 kfree(dev);
168 return NULL;
169 }
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900170
Adrian McMenaminb9482372008-02-06 22:46:21 +0000171 return dev;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900172}
173
174static void maple_free_dev(struct maple_device *mdev)
175{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000176 if (!mdev)
177 return;
178 if (mdev->mq) {
179 kmem_cache_free(maple_queue_cache, mdev->mq->recvbufdcsp);
180 kfree(mdev->mq);
181 }
182 kfree(mdev);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900183}
184
185/* process the command queue into a maple command block
186 * terminating command has bit 32 of first long set to 0
187 */
188static void maple_build_block(struct mapleq *mq)
189{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000190 int port, unit, from, to, len;
191 unsigned long *lsendbuf = mq->sendbuf;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900192
Adrian McMenaminb9482372008-02-06 22:46:21 +0000193 port = mq->dev->port & 3;
194 unit = mq->dev->unit;
195 len = mq->length;
196 from = port << 6;
197 to = (port << 6) | (unit > 0 ? (1 << (unit - 1)) & 0x1f : 0x20);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900198
Adrian McMenaminb9482372008-02-06 22:46:21 +0000199 *maple_lastptr &= 0x7fffffff;
200 maple_lastptr = maple_sendptr;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900201
Adrian McMenaminb9482372008-02-06 22:46:21 +0000202 *maple_sendptr++ = (port << 16) | len | 0x80000000;
203 *maple_sendptr++ = PHYSADDR(mq->recvbuf);
204 *maple_sendptr++ =
205 mq->command | (to << 8) | (from << 16) | (len << 24);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900206
Adrian McMenaminb9482372008-02-06 22:46:21 +0000207 while (len-- > 0)
208 *maple_sendptr++ = *lsendbuf++;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900209}
210
211/* build up command queue */
212static void maple_send(void)
213{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000214 int i;
215 int maple_packets;
216 struct mapleq *mq, *nmq;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900217
Adrian McMenaminb9482372008-02-06 22:46:21 +0000218 if (!list_empty(&maple_sentq))
219 return;
220 if (list_empty(&maple_waitq) || !maple_dma_done())
221 return;
222 maple_packets = 0;
223 maple_sendptr = maple_lastptr = maple_sendbuf;
224 list_for_each_entry_safe(mq, nmq, &maple_waitq, list) {
225 maple_build_block(mq);
226 list_move(&mq->list, &maple_sentq);
227 if (maple_packets++ > MAPLE_MAXPACKETS)
228 break;
229 }
230 if (maple_packets > 0) {
231 for (i = 0; i < (1 << MAPLE_DMA_PAGES); i++)
232 dma_cache_sync(0, maple_sendbuf + i * PAGE_SIZE,
233 PAGE_SIZE, DMA_BIDIRECTIONAL);
234 }
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900235}
236
237static int attach_matching_maple_driver(struct device_driver *driver,
Adrian McMenaminb9482372008-02-06 22:46:21 +0000238 void *devptr)
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900239{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000240 struct maple_driver *maple_drv;
241 struct maple_device *mdev;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900242
Adrian McMenaminb9482372008-02-06 22:46:21 +0000243 mdev = devptr;
244 maple_drv = to_maple_driver(driver);
245 if (mdev->devinfo.function & be32_to_cpu(maple_drv->function)) {
246 if (maple_drv->connect(mdev) == 0) {
247 mdev->driver = maple_drv;
248 return 1;
249 }
250 }
251 return 0;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900252}
253
254static void maple_detach_driver(struct maple_device *mdev)
255{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000256 if (!mdev)
257 return;
258 if (mdev->driver) {
259 if (mdev->driver->disconnect)
260 mdev->driver->disconnect(mdev);
261 }
262 mdev->driver = NULL;
263 if (mdev->registered) {
264 maple_release_device(&mdev->dev);
265 device_unregister(&mdev->dev);
266 }
267 mdev->registered = 0;
268 maple_free_dev(mdev);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900269}
270
271/* process initial MAPLE_COMMAND_DEVINFO for each device or port */
272static void maple_attach_driver(struct maple_device *dev)
273{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000274 char *p;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900275
Adrian McMenaminb9482372008-02-06 22:46:21 +0000276 char *recvbuf;
277 unsigned long function;
278 int matched, retval;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900279
Adrian McMenaminb9482372008-02-06 22:46:21 +0000280 recvbuf = dev->mq->recvbuf;
281 memcpy(&dev->devinfo, recvbuf + 4, sizeof(dev->devinfo));
282 memcpy(dev->product_name, dev->devinfo.product_name, 30);
283 memcpy(dev->product_licence, dev->devinfo.product_licence, 60);
284 dev->product_name[30] = '\0';
285 dev->product_licence[60] = '\0';
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900286
Adrian McMenaminb9482372008-02-06 22:46:21 +0000287 for (p = dev->product_name + 29; dev->product_name <= p; p--)
288 if (*p == ' ')
289 *p = '\0';
290 else
291 break;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900292
Adrian McMenaminb9482372008-02-06 22:46:21 +0000293 for (p = dev->product_licence + 59; dev->product_licence <= p; p--)
294 if (*p == ' ')
295 *p = '\0';
296 else
297 break;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900298
Adrian McMenaminb9482372008-02-06 22:46:21 +0000299 function = be32_to_cpu(dev->devinfo.function);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900300
Adrian McMenaminb9482372008-02-06 22:46:21 +0000301 if (function > 0x200) {
302 /* Do this silently - as not a real device */
303 function = 0;
304 dev->driver = &maple_dummy_driver;
305 sprintf(dev->dev.bus_id, "%d:0.port", dev->port);
306 } else {
307 printk(KERN_INFO
308 "Maple bus at (%d, %d): Connected function 0x%lX\n",
309 dev->port, dev->unit, function);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900310
Adrian McMenaminb9482372008-02-06 22:46:21 +0000311 matched =
312 bus_for_each_drv(&maple_bus_type, NULL, dev,
313 attach_matching_maple_driver);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900314
Adrian McMenaminb9482372008-02-06 22:46:21 +0000315 if (matched == 0) {
316 /* Driver does not exist yet */
317 printk(KERN_INFO
318 "No maple driver found for this device\n");
319 dev->driver = &maple_dummy_driver;
320 }
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900321
Adrian McMenaminb9482372008-02-06 22:46:21 +0000322 sprintf(dev->dev.bus_id, "%d:0%d.%lX", dev->port,
323 dev->unit, function);
324 }
325 dev->function = function;
326 dev->dev.bus = &maple_bus_type;
327 dev->dev.parent = &maple_bus;
328 dev->dev.release = &maple_release_device;
329 retval = device_register(&dev->dev);
330 if (retval) {
331 printk(KERN_INFO
332 "Maple bus: Attempt to register device (%x, %x) failed.\n",
333 dev->port, dev->unit);
334 maple_free_dev(dev);
335 }
336 dev->registered = 1;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900337}
338
339/*
340 * if device has been registered for the given
341 * port and unit then return 1 - allows identification
342 * of which devices need to be attached or detached
343 */
344static int detach_maple_device(struct device *device, void *portptr)
345{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000346 struct maple_device_specify *ds;
347 struct maple_device *mdev;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900348
Adrian McMenaminb9482372008-02-06 22:46:21 +0000349 ds = portptr;
350 mdev = to_maple_dev(device);
351 if (mdev->port == ds->port && mdev->unit == ds->unit)
352 return 1;
353 return 0;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900354}
355
356static int setup_maple_commands(struct device *device, void *ignored)
357{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000358 struct maple_device *maple_dev = to_maple_dev(device);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900359
Adrian McMenaminb9482372008-02-06 22:46:21 +0000360 if ((maple_dev->interval > 0)
361 && time_after(jiffies, maple_dev->when)) {
362 maple_dev->when = jiffies + maple_dev->interval;
363 maple_dev->mq->command = MAPLE_COMMAND_GETCOND;
364 maple_dev->mq->sendbuf = &maple_dev->function;
365 maple_dev->mq->length = 1;
366 maple_add_packet(maple_dev->mq);
367 liststatus++;
368 } else {
369 if (time_after(jiffies, maple_pnp_time)) {
370 maple_dev->mq->command = MAPLE_COMMAND_DEVINFO;
371 maple_dev->mq->length = 0;
372 maple_add_packet(maple_dev->mq);
373 liststatus++;
374 }
375 }
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900376
Adrian McMenaminb9482372008-02-06 22:46:21 +0000377 return 0;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900378}
379
380/* VBLANK bottom half - implemented via workqueue */
381static void maple_vblank_handler(struct work_struct *work)
382{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000383 if (!maple_dma_done())
384 return;
385 if (!list_empty(&maple_sentq))
386 return;
387 ctrl_outl(0, MAPLE_ENABLE);
388 liststatus = 0;
389 bus_for_each_dev(&maple_bus_type, NULL, NULL,
390 setup_maple_commands);
391 if (time_after(jiffies, maple_pnp_time))
392 maple_pnp_time = jiffies + MAPLE_PNP_INTERVAL;
393 if (liststatus && list_empty(&maple_sentq)) {
394 INIT_LIST_HEAD(&maple_sentq);
395 maple_send();
396 }
397 maplebus_dma_reset();
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900398}
399
400/* handle devices added via hotplugs - placing them on queue for DEVINFO*/
401static void maple_map_subunits(struct maple_device *mdev, int submask)
402{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000403 int retval, k, devcheck;
404 struct maple_device *mdev_add;
405 struct maple_device_specify ds;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900406
Adrian McMenaminb9482372008-02-06 22:46:21 +0000407 for (k = 0; k < 5; k++) {
408 ds.port = mdev->port;
409 ds.unit = k + 1;
410 retval =
411 bus_for_each_dev(&maple_bus_type, NULL, &ds,
412 detach_maple_device);
413 if (retval) {
414 submask = submask >> 1;
415 continue;
416 }
417 devcheck = submask & 0x01;
418 if (devcheck) {
419 mdev_add = maple_alloc_dev(mdev->port, k + 1);
420 if (!mdev_add)
421 return;
422 mdev_add->mq->command = MAPLE_COMMAND_DEVINFO;
423 mdev_add->mq->length = 0;
424 maple_add_packet(mdev_add->mq);
425 scanning = 1;
426 }
427 submask = submask >> 1;
428 }
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900429}
430
431/* mark a device as removed */
432static void maple_clean_submap(struct maple_device *mdev)
433{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000434 int killbit;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900435
Adrian McMenaminb9482372008-02-06 22:46:21 +0000436 killbit = (mdev->unit > 0 ? (1 << (mdev->unit - 1)) & 0x1f : 0x20);
437 killbit = ~killbit;
438 killbit &= 0xFF;
439 subdevice_map[mdev->port] = subdevice_map[mdev->port] & killbit;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900440}
441
442/* handle empty port or hotplug removal */
443static void maple_response_none(struct maple_device *mdev,
Adrian McMenaminb9482372008-02-06 22:46:21 +0000444 struct mapleq *mq)
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900445{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000446 if (mdev->unit != 0) {
447 list_del(&mq->list);
448 maple_clean_submap(mdev);
449 printk(KERN_INFO
450 "Maple bus device detaching at (%d, %d)\n",
451 mdev->port, mdev->unit);
452 maple_detach_driver(mdev);
453 return;
454 }
455 if (!started) {
456 printk(KERN_INFO "No maple devices attached to port %d\n",
457 mdev->port);
458 return;
459 }
460 maple_clean_submap(mdev);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900461}
462
463/* preprocess hotplugs or scans */
464static void maple_response_devinfo(struct maple_device *mdev,
Adrian McMenaminb9482372008-02-06 22:46:21 +0000465 char *recvbuf)
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900466{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000467 char submask;
468 if ((!started) || (scanning == 2)) {
469 maple_attach_driver(mdev);
470 return;
471 }
472 if (mdev->unit == 0) {
473 submask = recvbuf[2] & 0x1F;
474 if (submask ^ subdevice_map[mdev->port]) {
475 maple_map_subunits(mdev, submask);
476 subdevice_map[mdev->port] = submask;
477 }
478 }
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900479}
480
481/* maple dma end bottom half - implemented via workqueue */
482static void maple_dma_handler(struct work_struct *work)
483{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000484 struct mapleq *mq, *nmq;
485 struct maple_device *dev;
486 char *recvbuf;
487 enum maple_code code;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900488
Adrian McMenaminb9482372008-02-06 22:46:21 +0000489 if (!maple_dma_done())
490 return;
491 ctrl_outl(0, MAPLE_ENABLE);
492 if (!list_empty(&maple_sentq)) {
493 list_for_each_entry_safe(mq, nmq, &maple_sentq, list) {
494 recvbuf = mq->recvbuf;
495 code = recvbuf[0];
496 dev = mq->dev;
497 switch (code) {
498 case MAPLE_RESPONSE_NONE:
499 maple_response_none(dev, mq);
500 break;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900501
Adrian McMenaminb9482372008-02-06 22:46:21 +0000502 case MAPLE_RESPONSE_DEVINFO:
503 maple_response_devinfo(dev, recvbuf);
504 break;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900505
Adrian McMenaminb9482372008-02-06 22:46:21 +0000506 case MAPLE_RESPONSE_DATATRF:
507 if (dev->callback)
508 dev->callback(mq);
509 break;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900510
Adrian McMenaminb9482372008-02-06 22:46:21 +0000511 case MAPLE_RESPONSE_FILEERR:
512 case MAPLE_RESPONSE_AGAIN:
513 case MAPLE_RESPONSE_BADCMD:
514 case MAPLE_RESPONSE_BADFUNC:
515 printk(KERN_DEBUG
516 "Maple non-fatal error 0x%X\n",
517 code);
518 break;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900519
Adrian McMenaminb9482372008-02-06 22:46:21 +0000520 case MAPLE_RESPONSE_ALLINFO:
521 printk(KERN_DEBUG
522 "Maple - extended device information not supported\n");
523 break;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900524
Adrian McMenaminb9482372008-02-06 22:46:21 +0000525 case MAPLE_RESPONSE_OK:
526 break;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900527
Adrian McMenaminb9482372008-02-06 22:46:21 +0000528 default:
529 break;
530 }
531 }
532 INIT_LIST_HEAD(&maple_sentq);
533 if (scanning == 1) {
534 maple_send();
535 scanning = 2;
536 } else
537 scanning = 0;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900538
Adrian McMenaminb9482372008-02-06 22:46:21 +0000539 if (started == 0)
540 started = 1;
541 }
542 maplebus_dma_reset();
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900543}
544
545static irqreturn_t maplebus_dma_interrupt(int irq, void *dev_id)
546{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000547 /* Load everything into the bottom half */
548 schedule_work(&maple_dma_process);
549 return IRQ_HANDLED;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900550}
551
552static irqreturn_t maplebus_vblank_interrupt(int irq, void *dev_id)
553{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000554 schedule_work(&maple_vblank_process);
555 return IRQ_HANDLED;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900556}
557
558static struct irqaction maple_dma_irq = {
Adrian McMenaminb9482372008-02-06 22:46:21 +0000559 .name = "maple bus DMA handler",
560 .handler = maplebus_dma_interrupt,
561 .flags = IRQF_SHARED,
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900562};
563
564static struct irqaction maple_vblank_irq = {
Adrian McMenaminb9482372008-02-06 22:46:21 +0000565 .name = "maple bus VBLANK handler",
566 .handler = maplebus_vblank_interrupt,
567 .flags = IRQF_SHARED,
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900568};
569
570static int maple_set_dma_interrupt_handler(void)
571{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000572 return setup_irq(HW_EVENT_MAPLE_DMA, &maple_dma_irq);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900573}
574
575static int maple_set_vblank_interrupt_handler(void)
576{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000577 return setup_irq(HW_EVENT_VSYNC, &maple_vblank_irq);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900578}
579
580static int maple_get_dma_buffer(void)
581{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000582 maple_sendbuf =
583 (void *) __get_free_pages(GFP_KERNEL | __GFP_ZERO,
584 MAPLE_DMA_PAGES);
585 if (!maple_sendbuf)
586 return -ENOMEM;
587 return 0;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900588}
589
590static int match_maple_bus_driver(struct device *devptr,
Adrian McMenaminb9482372008-02-06 22:46:21 +0000591 struct device_driver *drvptr)
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900592{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000593 struct maple_driver *maple_drv;
594 struct maple_device *maple_dev;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900595
Adrian McMenaminb9482372008-02-06 22:46:21 +0000596 maple_drv = container_of(drvptr, struct maple_driver, drv);
597 maple_dev = container_of(devptr, struct maple_device, dev);
598 /* Trap empty port case */
599 if (maple_dev->devinfo.function == 0xFFFFFFFF)
600 return 0;
601 else if (maple_dev->devinfo.function &
602 be32_to_cpu(maple_drv->function))
603 return 1;
604 return 0;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900605}
606
Adrian McMenaminb9482372008-02-06 22:46:21 +0000607static int maple_bus_uevent(struct device *dev,
608 struct kobj_uevent_env *env)
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900609{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000610 return 0;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900611}
612
613static void maple_bus_release(struct device *dev)
614{
615}
616
617static struct maple_driver maple_dummy_driver = {
Adrian McMenaminb9482372008-02-06 22:46:21 +0000618 .drv = {
619 .name = "maple_dummy_driver",
620 .bus = &maple_bus_type,
621 },
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900622};
623
624struct bus_type maple_bus_type = {
Adrian McMenaminb9482372008-02-06 22:46:21 +0000625 .name = "maple",
626 .match = match_maple_bus_driver,
627 .uevent = maple_bus_uevent,
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900628};
Adrian McMenaminb9482372008-02-06 22:46:21 +0000629
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900630EXPORT_SYMBOL_GPL(maple_bus_type);
631
632static struct device maple_bus = {
Adrian McMenaminb9482372008-02-06 22:46:21 +0000633 .bus_id = "maple",
634 .release = maple_bus_release,
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900635};
636
637static int __init maple_bus_init(void)
638{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000639 int retval, i;
640 struct maple_device *mdev[MAPLE_PORTS];
641 ctrl_outl(0, MAPLE_STATE);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900642
Adrian McMenaminb9482372008-02-06 22:46:21 +0000643 retval = device_register(&maple_bus);
644 if (retval)
645 goto cleanup;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900646
Adrian McMenaminb9482372008-02-06 22:46:21 +0000647 retval = bus_register(&maple_bus_type);
648 if (retval)
649 goto cleanup_device;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900650
Adrian McMenaminb9482372008-02-06 22:46:21 +0000651 retval = driver_register(&maple_dummy_driver.drv);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900652
Adrian McMenaminb9482372008-02-06 22:46:21 +0000653 if (retval)
654 goto cleanup_bus;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900655
Adrian McMenaminb9482372008-02-06 22:46:21 +0000656 /* allocate memory for maple bus dma */
657 retval = maple_get_dma_buffer();
658 if (retval) {
659 printk(KERN_INFO
660 "Maple bus: Failed to allocate Maple DMA buffers\n");
661 goto cleanup_basic;
662 }
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900663
Adrian McMenaminb9482372008-02-06 22:46:21 +0000664 /* set up DMA interrupt handler */
665 retval = maple_set_dma_interrupt_handler();
666 if (retval) {
667 printk(KERN_INFO
668 "Maple bus: Failed to grab maple DMA IRQ\n");
669 goto cleanup_dma;
670 }
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900671
Adrian McMenaminb9482372008-02-06 22:46:21 +0000672 /* set up VBLANK interrupt handler */
673 retval = maple_set_vblank_interrupt_handler();
674 if (retval) {
675 printk(KERN_INFO "Maple bus: Failed to grab VBLANK IRQ\n");
676 goto cleanup_irq;
677 }
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900678
Adrian McMenaminb9482372008-02-06 22:46:21 +0000679 maple_queue_cache =
680 kmem_cache_create("maple_queue_cache", 0x400, 0,
681 SLAB_HWCACHE_ALIGN, NULL);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900682
Adrian McMenaminb9482372008-02-06 22:46:21 +0000683 if (!maple_queue_cache)
684 goto cleanup_bothirqs;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900685
Adrian McMenaminb9482372008-02-06 22:46:21 +0000686 /* setup maple ports */
687 for (i = 0; i < MAPLE_PORTS; i++) {
688 mdev[i] = maple_alloc_dev(i, 0);
689 if (!mdev[i]) {
690 while (i-- > 0)
691 maple_free_dev(mdev[i]);
692 goto cleanup_cache;
693 }
694 mdev[i]->registered = 0;
695 mdev[i]->mq->command = MAPLE_COMMAND_DEVINFO;
696 mdev[i]->mq->length = 0;
697 maple_attach_driver(mdev[i]);
698 maple_add_packet(mdev[i]->mq);
699 subdevice_map[i] = 0;
700 }
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900701
Adrian McMenaminb9482372008-02-06 22:46:21 +0000702 /* setup maplebus hardware */
703 maplebus_dma_reset();
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900704
Adrian McMenaminb9482372008-02-06 22:46:21 +0000705 /* initial detection */
706 maple_send();
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900707
Adrian McMenaminb9482372008-02-06 22:46:21 +0000708 maple_pnp_time = jiffies;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900709
Adrian McMenaminb9482372008-02-06 22:46:21 +0000710 printk(KERN_INFO "Maple bus core now registered.\n");
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900711
Adrian McMenaminb9482372008-02-06 22:46:21 +0000712 return 0;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900713
Adrian McMenaminb9482372008-02-06 22:46:21 +0000714 cleanup_cache:
715 kmem_cache_destroy(maple_queue_cache);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900716
Adrian McMenaminb9482372008-02-06 22:46:21 +0000717 cleanup_bothirqs:
718 free_irq(HW_EVENT_VSYNC, 0);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900719
Adrian McMenaminb9482372008-02-06 22:46:21 +0000720 cleanup_irq:
721 free_irq(HW_EVENT_MAPLE_DMA, 0);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900722
Adrian McMenaminb9482372008-02-06 22:46:21 +0000723 cleanup_dma:
724 free_pages((unsigned long) maple_sendbuf, MAPLE_DMA_PAGES);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900725
Adrian McMenaminb9482372008-02-06 22:46:21 +0000726 cleanup_basic:
727 driver_unregister(&maple_dummy_driver.drv);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900728
Adrian McMenaminb9482372008-02-06 22:46:21 +0000729 cleanup_bus:
730 bus_unregister(&maple_bus_type);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900731
Adrian McMenaminb9482372008-02-06 22:46:21 +0000732 cleanup_device:
733 device_unregister(&maple_bus);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900734
Adrian McMenaminb9482372008-02-06 22:46:21 +0000735 cleanup:
736 printk(KERN_INFO "Maple bus registration failed\n");
737 return retval;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900738}
Adrian McMenaminb9482372008-02-06 22:46:21 +0000739
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900740subsys_initcall(maple_bus_init);