blob: c71bb4b4ce8476efbaa0e70bca1fccc3ab2666b5 [file] [log] [blame]
Adrian McMenamin17be2d22007-09-21 15:55:55 +09001/*
2 * Core maple bus functionality
3 *
Adrian McMenaminb233b282009-02-27 16:07:32 +09004 * Copyright (C) 2007 - 2009 Adrian McMenamin
Paul Mundt63870292008-08-04 10:39:46 +09005 * Copyright (C) 2001 - 2008 Paul Mundt
Adrian McMenaminb233b282009-02-27 16:07:32 +09006 * Copyright (C) 2000 - 2001 YAEGASHI Takeshi
Adrian McMenamin17be2d22007-09-21 15:55:55 +09007 * Copyright (C) 2001 M. R. Brown
Adrian McMenamin17be2d22007-09-21 15:55:55 +09008 *
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>
Adrian McMenamin17be2d22007-09-21 15:55:55 +090016#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>
Adrian McMenamin1795cf42008-07-29 22:10:56 +090022#include <linux/delay.h>
Adrian McMenamin17be2d22007-09-21 15:55:55 +090023#include <asm/cacheflush.h>
24#include <asm/dma.h>
25#include <asm/io.h>
Adrian McMenamin1795cf42008-07-29 22:10:56 +090026#include <mach/dma.h>
27#include <mach/sysasic.h>
Adrian McMenamin17be2d22007-09-21 15:55:55 +090028
Adrian McMenaminb233b282009-02-27 16:07:32 +090029MODULE_AUTHOR("Adrian McMenamin <adrian@mcmen.demon.co.uk>");
Adrian McMenamin17be2d22007-09-21 15:55:55 +090030MODULE_DESCRIPTION("Maple bus driver for Dreamcast");
31MODULE_LICENSE("GPL v2");
32MODULE_SUPPORTED_DEVICE("{{SEGA, Dreamcast/Maple}}");
33
34static void maple_dma_handler(struct work_struct *work);
35static void maple_vblank_handler(struct work_struct *work);
36
37static DECLARE_WORK(maple_dma_process, maple_dma_handler);
38static DECLARE_WORK(maple_vblank_process, maple_vblank_handler);
39
40static LIST_HEAD(maple_waitq);
41static LIST_HEAD(maple_sentq);
42
Adrian McMenamin1795cf42008-07-29 22:10:56 +090043/* mutex to protect queue of waiting packets */
44static DEFINE_MUTEX(maple_wlist_lock);
Adrian McMenamin17be2d22007-09-21 15:55:55 +090045
Adrian McMenaminb233b282009-02-27 16:07:32 +090046static struct maple_driver maple_unsupported_device;
Adrian McMenamin17be2d22007-09-21 15:55:55 +090047static struct device maple_bus;
48static int subdevice_map[MAPLE_PORTS];
49static unsigned long *maple_sendbuf, *maple_sendptr, *maple_lastptr;
50static unsigned long maple_pnp_time;
Adrian McMenamin1795cf42008-07-29 22:10:56 +090051static int started, scanning, fullscan;
Adrian McMenamin17be2d22007-09-21 15:55:55 +090052static struct kmem_cache *maple_queue_cache;
53
54struct maple_device_specify {
Adrian McMenaminb9482372008-02-06 22:46:21 +000055 int port;
56 int unit;
Adrian McMenamin17be2d22007-09-21 15:55:55 +090057};
58
Adrian McMenaminb233b282009-02-27 16:07:32 +090059static bool checked[MAPLE_PORTS];
60static bool empty[MAPLE_PORTS];
61static struct maple_device *baseunits[MAPLE_PORTS];
Adrian McMenaminbd496662008-02-24 14:30:23 +000062
Adrian McMenamin17be2d22007-09-21 15:55:55 +090063/**
Paul Mundt63870292008-08-04 10:39:46 +090064 * 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.
Adrian McMenamin17be2d22007-09-21 15:55:55 +090069 */
Paul Mundt63870292008-08-04 10:39:46 +090070int maple_driver_register(struct maple_driver *drv)
Adrian McMenamin17be2d22007-09-21 15:55:55 +090071{
Adrian McMenaminb9482372008-02-06 22:46:21 +000072 if (!drv)
73 return -EINVAL;
Paul Mundt63870292008-08-04 10:39:46 +090074
75 drv->drv.bus = &maple_bus_type;
76
77 return driver_register(&drv->drv);
Adrian McMenamin17be2d22007-09-21 15:55:55 +090078}
79EXPORT_SYMBOL_GPL(maple_driver_register);
80
Paul Mundt63870292008-08-04 10:39:46 +090081/**
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 */
88void maple_driver_unregister(struct maple_driver *drv)
89{
90 driver_unregister(&drv->drv);
91}
Paul Mundt61787062008-08-04 10:58:24 +090092EXPORT_SYMBOL_GPL(maple_driver_unregister);
Paul Mundt63870292008-08-04 10:39:46 +090093
Adrian McMenamin17be2d22007-09-21 15:55:55 +090094/* set hardware registers to enable next round of dma */
Adrian McMenaminb233b282009-02-27 16:07:32 +090095static void maple_dma_reset(void)
Adrian McMenamin17be2d22007-09-21 15:55:55 +090096{
Adrian McMenaminb9482372008-02-06 22:46:21 +000097 ctrl_outl(MAPLE_MAGIC, MAPLE_RESET);
98 /* set trig type to 0 for software trigger, 1 for hardware (VBLANK) */
99 ctrl_outl(1, MAPLE_TRIGTYPE);
Adrian McMenaminb233b282009-02-27 16:07:32 +0900100 /*
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 */
108 ctrl_outl(MAPLE_2MBPS | MAPLE_TIMEOUT(0xFFFF), MAPLE_SPEED);
Adrian McMenaminb9482372008-02-06 22:46:21 +0000109 ctrl_outl(PHYSADDR(maple_sendbuf), MAPLE_DMAADDR);
110 ctrl_outl(1, MAPLE_ENABLE);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900111}
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 */
120void maple_getcond_callback(struct maple_device *dev,
Adrian McMenaminb3c69e22008-02-06 23:51:21 +0000121 void (*callback) (struct mapleq *mq),
122 unsigned long interval, unsigned long function)
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900123{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000124 dev->callback = callback;
125 dev->interval = interval;
126 dev->function = cpu_to_be32(function);
127 dev->when = jiffies;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900128}
129EXPORT_SYMBOL_GPL(maple_getcond_callback);
130
131static int maple_dma_done(void)
132{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000133 return (ctrl_inl(MAPLE_STATE) & 1) == 0;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900134}
135
136static void maple_release_device(struct device *dev)
137{
Adrian McMenaminb3c69e22008-02-06 23:51:21 +0000138 struct maple_device *mdev;
139 struct mapleq *mq;
Adrian McMenaminb233b282009-02-27 16:07:32 +0900140
Adrian McMenaminb3c69e22008-02-06 23:51:21 +0000141 mdev = to_maple_dev(dev);
142 mq = mdev->mq;
Adrian McMenaminb233b282009-02-27 16:07:32 +0900143 kmem_cache_free(maple_queue_cache, mq->recvbuf);
144 kfree(mq);
Adrian McMenaminb3c69e22008-02-06 23:51:21 +0000145 kfree(mdev);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900146}
147
Paul Mundt6a9545b2008-08-04 12:51:06 +0900148/**
Adrian McMenaminb233b282009-02-27 16:07:32 +0900149 * maple_add_packet - add a single instruction to the maple bus queue
Paul Mundt6a9545b2008-08-04 12:51:06 +0900150 * @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
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900155 */
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900156int maple_add_packet(struct maple_device *mdev, u32 function, u32 command,
157 size_t length, void *data)
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900158{
Adrian McMenaminb233b282009-02-27 16:07:32 +0900159 int ret = 0;
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900160 void *sendbuf = NULL;
161
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900162 if (length) {
Adrian McMenaminb233b282009-02-27 16:07:32 +0900163 sendbuf = kzalloc(length * 4, GFP_KERNEL);
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900164 if (!sendbuf) {
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900165 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
Adrian McMenaminb233b282009-02-27 16:07:32 +0900177 mutex_lock(&maple_wlist_lock);
178 list_add_tail(&mdev->mq->list, &maple_waitq);
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900179 mutex_unlock(&maple_wlist_lock);
Adrian McMenaminb233b282009-02-27 16:07:32 +0900180out:
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900181 return ret;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900182}
183EXPORT_SYMBOL_GPL(maple_add_packet);
184
Adrian McMenaminb3c69e22008-02-06 23:51:21 +0000185static struct mapleq *maple_allocq(struct maple_device *mdev)
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900186{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000187 struct mapleq *mq;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900188
Adrian McMenaminb233b282009-02-27 16:07:32 +0900189 mq = kzalloc(sizeof(*mq), GFP_KERNEL);
Adrian McMenaminb9482372008-02-06 22:46:21 +0000190 if (!mq)
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900191 goto failed_nomem;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900192
Adrian McMenaminb233b282009-02-27 16:07:32 +0900193 INIT_LIST_HEAD(&mq->list);
Adrian McMenaminb3c69e22008-02-06 23:51:21 +0000194 mq->dev = mdev;
Adrian McMenaminb233b282009-02-27 16:07:32 +0900195 mq->recvbuf = kmem_cache_zalloc(maple_queue_cache, GFP_KERNEL);
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900196 if (!mq->recvbuf)
197 goto failed_p2;
Adrian McMenaminb233b282009-02-27 16:07:32 +0900198 mq->recvbuf->buf = &((mq->recvbuf->bufx)[0]);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900199
Adrian McMenaminb9482372008-02-06 22:46:21 +0000200 return mq;
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900201
202failed_p2:
203 kfree(mq);
204failed_nomem:
Adrian McMenaminb233b282009-02-27 16:07:32 +0900205 dev_err(&mdev->dev, "could not allocate memory for device (%d, %d)\n",
206 mdev->port, mdev->unit);
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900207 return NULL;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900208}
209
210static struct maple_device *maple_alloc_dev(int port, int unit)
211{
Adrian McMenaminb3c69e22008-02-06 23:51:21 +0000212 struct maple_device *mdev;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900213
Adrian McMenaminb233b282009-02-27 16:07:32 +0900214 /* zero this out to avoid kobj subsystem
215 * thinking it has already been registered */
216
Adrian McMenaminb3c69e22008-02-06 23:51:21 +0000217 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
218 if (!mdev)
Adrian McMenaminb9482372008-02-06 22:46:21 +0000219 return NULL;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900220
Adrian McMenaminb3c69e22008-02-06 23:51:21 +0000221 mdev->port = port;
222 mdev->unit = unit;
Adrian McMenaminb233b282009-02-27 16:07:32 +0900223
Adrian McMenaminb3c69e22008-02-06 23:51:21 +0000224 mdev->mq = maple_allocq(mdev);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900225
Adrian McMenaminb3c69e22008-02-06 23:51:21 +0000226 if (!mdev->mq) {
227 kfree(mdev);
Adrian McMenaminb9482372008-02-06 22:46:21 +0000228 return NULL;
229 }
Adrian McMenaminb3c69e22008-02-06 23:51:21 +0000230 mdev->dev.bus = &maple_bus_type;
231 mdev->dev.parent = &maple_bus;
Adrian McMenaminb233b282009-02-27 16:07:32 +0900232 init_waitqueue_head(&mdev->maple_wait);
Adrian McMenaminb3c69e22008-02-06 23:51:21 +0000233 return mdev;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900234}
235
236static void maple_free_dev(struct maple_device *mdev)
237{
Adrian McMenaminb233b282009-02-27 16:07:32 +0900238 kmem_cache_free(maple_queue_cache, mdev->mq->recvbuf);
239 kfree(mdev->mq);
Adrian McMenaminb9482372008-02-06 22:46:21 +0000240 kfree(mdev);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900241}
242
243/* process the command queue into a maple command block
244 * terminating command has bit 32 of first long set to 0
245 */
246static void maple_build_block(struct mapleq *mq)
247{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000248 int port, unit, from, to, len;
249 unsigned long *lsendbuf = mq->sendbuf;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900250
Adrian McMenaminb9482372008-02-06 22:46:21 +0000251 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);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900256
Adrian McMenaminb9482372008-02-06 22:46:21 +0000257 *maple_lastptr &= 0x7fffffff;
258 maple_lastptr = maple_sendptr;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900259
Adrian McMenaminb9482372008-02-06 22:46:21 +0000260 *maple_sendptr++ = (port << 16) | len | 0x80000000;
Adrian McMenaminb233b282009-02-27 16:07:32 +0900261 *maple_sendptr++ = PHYSADDR(mq->recvbuf->buf);
Adrian McMenaminb9482372008-02-06 22:46:21 +0000262 *maple_sendptr++ =
263 mq->command | (to << 8) | (from << 16) | (len << 24);
Adrian McMenaminb9482372008-02-06 22:46:21 +0000264 while (len-- > 0)
265 *maple_sendptr++ = *lsendbuf++;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900266}
267
268/* build up command queue */
269static void maple_send(void)
270{
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900271 int i, maple_packets = 0;
Adrian McMenaminb9482372008-02-06 22:46:21 +0000272 struct mapleq *mq, *nmq;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900273
Adrian McMenaminb233b282009-02-27 16:07:32 +0900274 if (!maple_dma_done())
275 return;
276
277 /* disable DMA */
278 ctrl_outl(0, MAPLE_ENABLE);
279
Adrian McMenaminb9482372008-02-06 22:46:21 +0000280 if (!list_empty(&maple_sentq))
Adrian McMenaminb233b282009-02-27 16:07:32 +0900281 goto finish;
282
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900283 mutex_lock(&maple_wlist_lock);
Adrian McMenaminb233b282009-02-27 16:07:32 +0900284 if (list_empty(&maple_waitq)) {
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900285 mutex_unlock(&maple_wlist_lock);
Adrian McMenaminb233b282009-02-27 16:07:32 +0900286 goto finish;
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900287 }
Adrian McMenaminb233b282009-02-27 16:07:32 +0900288
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900289 maple_lastptr = maple_sendbuf;
290 maple_sendptr = maple_sendbuf;
Adrian McMenaminb233b282009-02-27 16:07:32 +0900291
Adrian McMenaminb9482372008-02-06 22:46:21 +0000292 list_for_each_entry_safe(mq, nmq, &maple_waitq, list) {
293 maple_build_block(mq);
Adrian McMenaminb233b282009-02-27 16:07:32 +0900294 list_del_init(&mq->list);
295 list_add_tail(&mq->list, &maple_sentq);
Adrian McMenaminb9482372008-02-06 22:46:21 +0000296 if (maple_packets++ > MAPLE_MAXPACKETS)
297 break;
298 }
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900299 mutex_unlock(&maple_wlist_lock);
Adrian McMenaminb9482372008-02-06 22:46:21 +0000300 if (maple_packets > 0) {
301 for (i = 0; i < (1 << MAPLE_DMA_PAGES); i++)
302 dma_cache_sync(0, maple_sendbuf + i * PAGE_SIZE,
303 PAGE_SIZE, DMA_BIDIRECTIONAL);
304 }
Adrian McMenaminb233b282009-02-27 16:07:32 +0900305
306finish:
307 maple_dma_reset();
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900308}
309
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900310/* check if there is a driver registered likely to match this device */
Adrian McMenaminb233b282009-02-27 16:07:32 +0900311static int maple_check_matching_driver(struct device_driver *driver,
Adrian McMenaminb9482372008-02-06 22:46:21 +0000312 void *devptr)
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900313{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000314 struct maple_driver *maple_drv;
315 struct maple_device *mdev;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900316
Adrian McMenaminb9482372008-02-06 22:46:21 +0000317 mdev = devptr;
318 maple_drv = to_maple_driver(driver);
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900319 if (mdev->devinfo.function & cpu_to_be32(maple_drv->function))
320 return 1;
Adrian McMenaminb9482372008-02-06 22:46:21 +0000321 return 0;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900322}
323
324static void maple_detach_driver(struct maple_device *mdev)
325{
Adrian McMenaminb3c69e22008-02-06 23:51:21 +0000326 device_unregister(&mdev->dev);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900327}
328
329/* process initial MAPLE_COMMAND_DEVINFO for each device or port */
Adrian McMenaminb3c69e22008-02-06 23:51:21 +0000330static void maple_attach_driver(struct maple_device *mdev)
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900331{
Adrian McMenaminb3c69e22008-02-06 23:51:21 +0000332 char *p, *recvbuf;
Adrian McMenaminb9482372008-02-06 22:46:21 +0000333 unsigned long function;
Adrian McMenaminb233b282009-02-27 16:07:32 +0900334 int matched, error;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900335
Adrian McMenaminb233b282009-02-27 16:07:32 +0900336 recvbuf = mdev->mq->recvbuf->buf;
Adrian McMenaminb3c69e22008-02-06 23:51:21 +0000337 /* 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);
Adrian McMenaminb3c69e22008-02-06 23:51:21 +0000344 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';
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900350
Adrian McMenaminb3c69e22008-02-06 23:51:21 +0000351 for (p = mdev->product_name + 29; mdev->product_name <= p; p--)
352 if (*p == ' ')
353 *p = '\0';
354 else
355 break;
356 for (p = mdev->product_licence + 59; mdev->product_licence <= p; p--)
Adrian McMenaminb9482372008-02-06 22:46:21 +0000357 if (*p == ' ')
358 *p = '\0';
359 else
360 break;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900361
Adrian McMenaminb3c69e22008-02-06 23:51:21 +0000362 function = be32_to_cpu(mdev->devinfo.function);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900363
Adrian McMenaminb233b282009-02-27 16:07:32 +0900364 dev_info(&mdev->dev, "detected %s: function 0x%lX: at (%d, %d)\n",
365 mdev->product_name, function, mdev->port, mdev->unit);
366
Adrian McMenaminb9482372008-02-06 22:46:21 +0000367 if (function > 0x200) {
368 /* Do this silently - as not a real device */
369 function = 0;
Adrian McMenaminb233b282009-02-27 16:07:32 +0900370 mdev->driver = &maple_unsupported_device;
Kay Sievers93fde772009-03-03 12:16:12 +0900371 dev_set_name(&mdev->dev, "%d:0.port", mdev->port);
Adrian McMenaminb9482372008-02-06 22:46:21 +0000372 } else {
Adrian McMenaminb9482372008-02-06 22:46:21 +0000373 matched =
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900374 bus_for_each_drv(&maple_bus_type, NULL, mdev,
Adrian McMenaminb233b282009-02-27 16:07:32 +0900375 maple_check_matching_driver);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900376
Adrian McMenaminb9482372008-02-06 22:46:21 +0000377 if (matched == 0) {
378 /* Driver does not exist yet */
Adrian McMenaminb233b282009-02-27 16:07:32 +0900379 dev_info(&mdev->dev, "no driver found\n");
380 mdev->driver = &maple_unsupported_device;
Adrian McMenaminb9482372008-02-06 22:46:21 +0000381 }
Kay Sievers93fde772009-03-03 12:16:12 +0900382
383 dev_set_name(&mdev->dev, "%d:0%d.%lX", mdev->port,
384 mdev->unit, function);
Adrian McMenaminb9482372008-02-06 22:46:21 +0000385 }
Adrian McMenaminb233b282009-02-27 16:07:32 +0900386
Adrian McMenaminb3c69e22008-02-06 23:51:21 +0000387 mdev->function = function;
388 mdev->dev.release = &maple_release_device;
Adrian McMenaminb233b282009-02-27 16:07:32 +0900389
390 atomic_set(&mdev->busy, 0);
391 error = device_register(&mdev->dev);
392 if (error) {
393 dev_warn(&mdev->dev, "could not register device at"
394 " (%d, %d), with error 0x%X\n", mdev->unit,
395 mdev->port, error);
Adrian McMenaminb3c69e22008-02-06 23:51:21 +0000396 maple_free_dev(mdev);
397 mdev = NULL;
398 return;
Adrian McMenaminb9482372008-02-06 22:46:21 +0000399 }
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900400}
401
402/*
403 * if device has been registered for the given
404 * port and unit then return 1 - allows identification
405 * of which devices need to be attached or detached
406 */
Adrian McMenaminb233b282009-02-27 16:07:32 +0900407static int check_maple_device(struct device *device, void *portptr)
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900408{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000409 struct maple_device_specify *ds;
410 struct maple_device *mdev;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900411
Adrian McMenaminb9482372008-02-06 22:46:21 +0000412 ds = portptr;
413 mdev = to_maple_dev(device);
414 if (mdev->port == ds->port && mdev->unit == ds->unit)
415 return 1;
416 return 0;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900417}
418
419static int setup_maple_commands(struct device *device, void *ignored)
420{
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900421 int add;
Adrian McMenaminb233b282009-02-27 16:07:32 +0900422 struct maple_device *mdev = to_maple_dev(device);
423 if (mdev->interval > 0 && atomic_read(&mdev->busy) == 0 &&
424 time_after(jiffies, mdev->when)) {
425 /* bounce if we cannot add */
426 add = maple_add_packet(mdev,
427 be32_to_cpu(mdev->devinfo.function),
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900428 MAPLE_COMMAND_GETCOND, 1, NULL);
429 if (!add)
Adrian McMenaminb233b282009-02-27 16:07:32 +0900430 mdev->when = jiffies + mdev->interval;
Adrian McMenaminb9482372008-02-06 22:46:21 +0000431 } else {
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900432 if (time_after(jiffies, maple_pnp_time))
Adrian McMenaminb233b282009-02-27 16:07:32 +0900433 /* Ensure we don't have block reads and devinfo
434 * calls interfering with one another - so flag the
435 * device as busy */
436 if (atomic_read(&mdev->busy) == 0) {
437 atomic_set(&mdev->busy, 1);
438 maple_add_packet(mdev, 0,
439 MAPLE_COMMAND_DEVINFO, 0, NULL);
440 }
Adrian McMenaminb9482372008-02-06 22:46:21 +0000441 }
Adrian McMenaminb9482372008-02-06 22:46:21 +0000442 return 0;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900443}
444
445/* VBLANK bottom half - implemented via workqueue */
446static void maple_vblank_handler(struct work_struct *work)
447{
Adrian McMenaminb233b282009-02-27 16:07:32 +0900448 int x, locking;
449 struct maple_device *mdev;
450
451 if (!maple_dma_done())
Adrian McMenaminb9482372008-02-06 22:46:21 +0000452 return;
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900453
Adrian McMenaminb9482372008-02-06 22:46:21 +0000454 ctrl_outl(0, MAPLE_ENABLE);
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900455
Adrian McMenaminb233b282009-02-27 16:07:32 +0900456 if (!list_empty(&maple_sentq))
457 goto finish;
458
459 /*
460 * Set up essential commands - to fetch data and
461 * check devices are still present
462 */
Adrian McMenaminb9482372008-02-06 22:46:21 +0000463 bus_for_each_dev(&maple_bus_type, NULL, NULL,
Adrian McMenaminb233b282009-02-27 16:07:32 +0900464 setup_maple_commands);
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900465
Adrian McMenaminb233b282009-02-27 16:07:32 +0900466 if (time_after(jiffies, maple_pnp_time)) {
467 /*
468 * Scan the empty ports - bus is flakey and may have
469 * mis-reported emptyness
470 */
471 for (x = 0; x < MAPLE_PORTS; x++) {
472 if (checked[x] && empty[x]) {
473 mdev = baseunits[x];
474 if (!mdev)
475 break;
476 atomic_set(&mdev->busy, 1);
477 locking = maple_add_packet(mdev, 0,
478 MAPLE_COMMAND_DEVINFO, 0, NULL);
479 if (!locking)
480 break;
481 }
482 }
483
Adrian McMenaminb9482372008-02-06 22:46:21 +0000484 maple_pnp_time = jiffies + MAPLE_PNP_INTERVAL;
Adrian McMenaminb9482372008-02-06 22:46:21 +0000485 }
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900486
Adrian McMenaminb233b282009-02-27 16:07:32 +0900487finish:
488 maple_send();
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900489}
490
Adrian McMenaminb233b282009-02-27 16:07:32 +0900491/* handle devices added via hotplugs - placing them on queue for DEVINFO */
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900492static void maple_map_subunits(struct maple_device *mdev, int submask)
493{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000494 int retval, k, devcheck;
495 struct maple_device *mdev_add;
496 struct maple_device_specify ds;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900497
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900498 ds.port = mdev->port;
Adrian McMenaminb9482372008-02-06 22:46:21 +0000499 for (k = 0; k < 5; k++) {
Adrian McMenaminb9482372008-02-06 22:46:21 +0000500 ds.unit = k + 1;
501 retval =
502 bus_for_each_dev(&maple_bus_type, NULL, &ds,
Adrian McMenaminb233b282009-02-27 16:07:32 +0900503 check_maple_device);
Adrian McMenaminb9482372008-02-06 22:46:21 +0000504 if (retval) {
505 submask = submask >> 1;
506 continue;
507 }
508 devcheck = submask & 0x01;
509 if (devcheck) {
510 mdev_add = maple_alloc_dev(mdev->port, k + 1);
511 if (!mdev_add)
512 return;
Adrian McMenaminb233b282009-02-27 16:07:32 +0900513 atomic_set(&mdev_add->busy, 1);
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900514 maple_add_packet(mdev_add, 0, MAPLE_COMMAND_DEVINFO,
515 0, NULL);
516 /* mark that we are checking sub devices */
Adrian McMenaminb9482372008-02-06 22:46:21 +0000517 scanning = 1;
518 }
519 submask = submask >> 1;
520 }
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900521}
522
523/* mark a device as removed */
524static void maple_clean_submap(struct maple_device *mdev)
525{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000526 int killbit;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900527
Adrian McMenaminb9482372008-02-06 22:46:21 +0000528 killbit = (mdev->unit > 0 ? (1 << (mdev->unit - 1)) & 0x1f : 0x20);
529 killbit = ~killbit;
530 killbit &= 0xFF;
531 subdevice_map[mdev->port] = subdevice_map[mdev->port] & killbit;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900532}
533
534/* handle empty port or hotplug removal */
Adrian McMenaminb233b282009-02-27 16:07:32 +0900535static void maple_response_none(struct maple_device *mdev)
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900536{
Adrian McMenaminb233b282009-02-27 16:07:32 +0900537 maple_clean_submap(mdev);
538
539 if (likely(mdev->unit != 0)) {
540 /*
541 * Block devices play up
542 * and give the impression they have
543 * been removed even when still in place or
544 * trip the mtd layer when they have
545 * really gone - this code traps that eventuality
546 * and ensures we aren't overloaded with useless
547 * error messages
548 */
549 if (mdev->can_unload) {
550 if (!mdev->can_unload(mdev)) {
551 atomic_set(&mdev->busy, 2);
552 wake_up(&mdev->maple_wait);
553 return;
554 }
555 }
556
557 dev_info(&mdev->dev, "detaching device at (%d, %d)\n",
558 mdev->port, mdev->unit);
Adrian McMenaminb9482372008-02-06 22:46:21 +0000559 maple_detach_driver(mdev);
560 return;
Adrian McMenaminb233b282009-02-27 16:07:32 +0900561 } else {
562 if (!started || !fullscan) {
563 if (checked[mdev->port] == false) {
564 checked[mdev->port] = true;
565 empty[mdev->port] = true;
566 dev_info(&mdev->dev, "no devices"
567 " to port %d\n", mdev->port);
568 }
569 return;
Adrian McMenaminbd496662008-02-24 14:30:23 +0000570 }
Adrian McMenaminb9482372008-02-06 22:46:21 +0000571 }
Adrian McMenaminb233b282009-02-27 16:07:32 +0900572 /* Some hardware devices generate false detach messages on unit 0 */
573 atomic_set(&mdev->busy, 0);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900574}
575
576/* preprocess hotplugs or scans */
577static void maple_response_devinfo(struct maple_device *mdev,
Adrian McMenaminb9482372008-02-06 22:46:21 +0000578 char *recvbuf)
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900579{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000580 char submask;
Adrian McMenaminbd496662008-02-24 14:30:23 +0000581 if (!started || (scanning == 2) || !fullscan) {
582 if ((mdev->unit == 0) && (checked[mdev->port] == false)) {
583 checked[mdev->port] = true;
584 maple_attach_driver(mdev);
585 } else {
586 if (mdev->unit != 0)
587 maple_attach_driver(mdev);
Adrian McMenaminb233b282009-02-27 16:07:32 +0900588 if (mdev->unit == 0) {
589 empty[mdev->port] = false;
590 maple_attach_driver(mdev);
591 }
Adrian McMenaminbd496662008-02-24 14:30:23 +0000592 }
Adrian McMenaminb9482372008-02-06 22:46:21 +0000593 }
594 if (mdev->unit == 0) {
595 submask = recvbuf[2] & 0x1F;
596 if (submask ^ subdevice_map[mdev->port]) {
597 maple_map_subunits(mdev, submask);
598 subdevice_map[mdev->port] = submask;
599 }
600 }
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900601}
602
Adrian McMenaminb233b282009-02-27 16:07:32 +0900603static void maple_response_fileerr(struct maple_device *mdev, void *recvbuf)
604{
605 if (mdev->fileerr_handler) {
606 mdev->fileerr_handler(mdev, recvbuf);
607 return;
608 } else
609 dev_warn(&mdev->dev, "device at (%d, %d) reports"
610 "file error 0x%X\n", mdev->port, mdev->unit,
611 ((int *)recvbuf)[1]);
612}
613
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900614static void maple_port_rescan(void)
615{
616 int i;
617 struct maple_device *mdev;
618
619 fullscan = 1;
620 for (i = 0; i < MAPLE_PORTS; i++) {
621 if (checked[i] == false) {
622 fullscan = 0;
623 mdev = baseunits[i];
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900624 maple_add_packet(mdev, 0, MAPLE_COMMAND_DEVINFO,
625 0, NULL);
626 }
627 }
628}
629
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900630/* maple dma end bottom half - implemented via workqueue */
631static void maple_dma_handler(struct work_struct *work)
632{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000633 struct mapleq *mq, *nmq;
Adrian McMenaminb233b282009-02-27 16:07:32 +0900634 struct maple_device *mdev;
Adrian McMenaminb9482372008-02-06 22:46:21 +0000635 char *recvbuf;
636 enum maple_code code;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900637
Adrian McMenaminb9482372008-02-06 22:46:21 +0000638 if (!maple_dma_done())
639 return;
640 ctrl_outl(0, MAPLE_ENABLE);
641 if (!list_empty(&maple_sentq)) {
642 list_for_each_entry_safe(mq, nmq, &maple_sentq, list) {
Adrian McMenaminb233b282009-02-27 16:07:32 +0900643 mdev = mq->dev;
644 recvbuf = mq->recvbuf->buf;
645 dma_cache_sync(&mdev->dev, recvbuf, 0x400,
646 DMA_FROM_DEVICE);
Adrian McMenaminb9482372008-02-06 22:46:21 +0000647 code = recvbuf[0];
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900648 kfree(mq->sendbuf);
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900649 list_del_init(&mq->list);
Adrian McMenaminb9482372008-02-06 22:46:21 +0000650 switch (code) {
651 case MAPLE_RESPONSE_NONE:
Adrian McMenaminb233b282009-02-27 16:07:32 +0900652 maple_response_none(mdev);
Adrian McMenaminb9482372008-02-06 22:46:21 +0000653 break;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900654
Adrian McMenaminb9482372008-02-06 22:46:21 +0000655 case MAPLE_RESPONSE_DEVINFO:
Adrian McMenaminb233b282009-02-27 16:07:32 +0900656 maple_response_devinfo(mdev, recvbuf);
657 atomic_set(&mdev->busy, 0);
Adrian McMenaminb9482372008-02-06 22:46:21 +0000658 break;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900659
Adrian McMenaminb9482372008-02-06 22:46:21 +0000660 case MAPLE_RESPONSE_DATATRF:
Adrian McMenaminb233b282009-02-27 16:07:32 +0900661 if (mdev->callback)
662 mdev->callback(mq);
663 atomic_set(&mdev->busy, 0);
664 wake_up(&mdev->maple_wait);
Adrian McMenaminb9482372008-02-06 22:46:21 +0000665 break;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900666
Adrian McMenaminb9482372008-02-06 22:46:21 +0000667 case MAPLE_RESPONSE_FILEERR:
Adrian McMenaminb233b282009-02-27 16:07:32 +0900668 maple_response_fileerr(mdev, recvbuf);
669 atomic_set(&mdev->busy, 0);
670 wake_up(&mdev->maple_wait);
671 break;
672
Adrian McMenaminb9482372008-02-06 22:46:21 +0000673 case MAPLE_RESPONSE_AGAIN:
674 case MAPLE_RESPONSE_BADCMD:
675 case MAPLE_RESPONSE_BADFUNC:
Adrian McMenaminb233b282009-02-27 16:07:32 +0900676 dev_warn(&mdev->dev, "non-fatal error"
677 " 0x%X at (%d, %d)\n", code,
678 mdev->port, mdev->unit);
679 atomic_set(&mdev->busy, 0);
Adrian McMenaminb9482372008-02-06 22:46:21 +0000680 break;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900681
Adrian McMenaminb9482372008-02-06 22:46:21 +0000682 case MAPLE_RESPONSE_ALLINFO:
Adrian McMenaminb233b282009-02-27 16:07:32 +0900683 dev_notice(&mdev->dev, "extended"
684 " device information request for (%d, %d)"
685 " but call is not supported\n", mdev->port,
686 mdev->unit);
687 atomic_set(&mdev->busy, 0);
Adrian McMenaminb9482372008-02-06 22:46:21 +0000688 break;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900689
Adrian McMenaminb9482372008-02-06 22:46:21 +0000690 case MAPLE_RESPONSE_OK:
Adrian McMenaminb233b282009-02-27 16:07:32 +0900691 atomic_set(&mdev->busy, 0);
692 wake_up(&mdev->maple_wait);
Adrian McMenaminb9482372008-02-06 22:46:21 +0000693 break;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900694
Adrian McMenaminb9482372008-02-06 22:46:21 +0000695 default:
696 break;
697 }
698 }
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900699 /* if scanning is 1 then we have subdevices to check */
Adrian McMenaminb9482372008-02-06 22:46:21 +0000700 if (scanning == 1) {
701 maple_send();
702 scanning = 2;
703 } else
704 scanning = 0;
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900705 /*check if we have actually tested all ports yet */
706 if (!fullscan)
707 maple_port_rescan();
708 /* mark that we have been through the first scan */
Adrian McMenaminb233b282009-02-27 16:07:32 +0900709 started = 1;
Adrian McMenaminb9482372008-02-06 22:46:21 +0000710 }
Adrian McMenaminb233b282009-02-27 16:07:32 +0900711 maple_send();
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900712}
713
Adrian McMenaminb233b282009-02-27 16:07:32 +0900714static irqreturn_t maple_dma_interrupt(int irq, void *dev_id)
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900715{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000716 /* Load everything into the bottom half */
717 schedule_work(&maple_dma_process);
718 return IRQ_HANDLED;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900719}
720
Adrian McMenaminb233b282009-02-27 16:07:32 +0900721static irqreturn_t maple_vblank_interrupt(int irq, void *dev_id)
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900722{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000723 schedule_work(&maple_vblank_process);
724 return IRQ_HANDLED;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900725}
726
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900727static int maple_set_dma_interrupt_handler(void)
728{
Adrian McMenaminb233b282009-02-27 16:07:32 +0900729 return request_irq(HW_EVENT_MAPLE_DMA, maple_dma_interrupt,
730 IRQF_SHARED, "maple bus DMA", &maple_unsupported_device);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900731}
732
733static int maple_set_vblank_interrupt_handler(void)
734{
Adrian McMenaminb233b282009-02-27 16:07:32 +0900735 return request_irq(HW_EVENT_VSYNC, maple_vblank_interrupt,
736 IRQF_SHARED, "maple bus VBLANK", &maple_unsupported_device);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900737}
738
739static int maple_get_dma_buffer(void)
740{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000741 maple_sendbuf =
742 (void *) __get_free_pages(GFP_KERNEL | __GFP_ZERO,
743 MAPLE_DMA_PAGES);
744 if (!maple_sendbuf)
745 return -ENOMEM;
746 return 0;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900747}
748
Adrian McMenaminb233b282009-02-27 16:07:32 +0900749static int maple_match_bus_driver(struct device *devptr,
Adrian McMenaminb9482372008-02-06 22:46:21 +0000750 struct device_driver *drvptr)
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900751{
Paul Mundt63870292008-08-04 10:39:46 +0900752 struct maple_driver *maple_drv = to_maple_driver(drvptr);
753 struct maple_device *maple_dev = to_maple_dev(devptr);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900754
Adrian McMenaminb9482372008-02-06 22:46:21 +0000755 /* Trap empty port case */
756 if (maple_dev->devinfo.function == 0xFFFFFFFF)
757 return 0;
758 else if (maple_dev->devinfo.function &
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900759 cpu_to_be32(maple_drv->function))
Adrian McMenaminb9482372008-02-06 22:46:21 +0000760 return 1;
761 return 0;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900762}
763
Adrian McMenaminb9482372008-02-06 22:46:21 +0000764static int maple_bus_uevent(struct device *dev,
765 struct kobj_uevent_env *env)
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900766{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000767 return 0;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900768}
769
770static void maple_bus_release(struct device *dev)
771{
772}
773
Adrian McMenaminb233b282009-02-27 16:07:32 +0900774static struct maple_driver maple_unsupported_device = {
Adrian McMenaminb9482372008-02-06 22:46:21 +0000775 .drv = {
Adrian McMenaminb233b282009-02-27 16:07:32 +0900776 .name = "maple_unsupported_device",
Adrian McMenaminb9482372008-02-06 22:46:21 +0000777 .bus = &maple_bus_type,
Adrian McMenaminb3c69e22008-02-06 23:51:21 +0000778 },
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900779};
Adrian McMenaminb233b282009-02-27 16:07:32 +0900780/**
781 * maple_bus_type - core maple bus structure
782 */
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900783struct bus_type maple_bus_type = {
Adrian McMenaminb9482372008-02-06 22:46:21 +0000784 .name = "maple",
Adrian McMenaminb233b282009-02-27 16:07:32 +0900785 .match = maple_match_bus_driver,
Adrian McMenaminb9482372008-02-06 22:46:21 +0000786 .uevent = maple_bus_uevent,
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900787};
788EXPORT_SYMBOL_GPL(maple_bus_type);
789
790static struct device maple_bus = {
Kay Sievers93fde772009-03-03 12:16:12 +0900791 .init_name = "maple",
Adrian McMenaminb9482372008-02-06 22:46:21 +0000792 .release = maple_bus_release,
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900793};
794
795static int __init maple_bus_init(void)
796{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000797 int retval, i;
798 struct maple_device *mdev[MAPLE_PORTS];
Adrian McMenaminb233b282009-02-27 16:07:32 +0900799
800 ctrl_outl(0, MAPLE_ENABLE);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900801
Adrian McMenaminb9482372008-02-06 22:46:21 +0000802 retval = device_register(&maple_bus);
803 if (retval)
804 goto cleanup;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900805
Adrian McMenaminb9482372008-02-06 22:46:21 +0000806 retval = bus_register(&maple_bus_type);
807 if (retval)
808 goto cleanup_device;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900809
Adrian McMenaminb233b282009-02-27 16:07:32 +0900810 retval = driver_register(&maple_unsupported_device.drv);
Adrian McMenaminb9482372008-02-06 22:46:21 +0000811 if (retval)
812 goto cleanup_bus;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900813
Adrian McMenaminb9482372008-02-06 22:46:21 +0000814 /* allocate memory for maple bus dma */
815 retval = maple_get_dma_buffer();
816 if (retval) {
Adrian McMenaminb233b282009-02-27 16:07:32 +0900817 dev_err(&maple_bus, "failed to allocate DMA buffers\n");
Adrian McMenaminb9482372008-02-06 22:46:21 +0000818 goto cleanup_basic;
819 }
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900820
Adrian McMenaminb9482372008-02-06 22:46:21 +0000821 /* set up DMA interrupt handler */
822 retval = maple_set_dma_interrupt_handler();
823 if (retval) {
Adrian McMenaminb233b282009-02-27 16:07:32 +0900824 dev_err(&maple_bus, "bus failed to grab maple "
825 "DMA IRQ\n");
Adrian McMenaminb9482372008-02-06 22:46:21 +0000826 goto cleanup_dma;
827 }
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900828
Adrian McMenaminb9482372008-02-06 22:46:21 +0000829 /* set up VBLANK interrupt handler */
830 retval = maple_set_vblank_interrupt_handler();
831 if (retval) {
Adrian McMenaminb233b282009-02-27 16:07:32 +0900832 dev_err(&maple_bus, "bus failed to grab VBLANK IRQ\n");
Adrian McMenaminb9482372008-02-06 22:46:21 +0000833 goto cleanup_irq;
834 }
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900835
Adrian McMenaminb233b282009-02-27 16:07:32 +0900836 maple_queue_cache = KMEM_CACHE(maple_buffer, SLAB_HWCACHE_ALIGN);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900837
Adrian McMenaminb9482372008-02-06 22:46:21 +0000838 if (!maple_queue_cache)
839 goto cleanup_bothirqs;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900840
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900841 INIT_LIST_HEAD(&maple_waitq);
842 INIT_LIST_HEAD(&maple_sentq);
843
Adrian McMenaminb9482372008-02-06 22:46:21 +0000844 /* setup maple ports */
845 for (i = 0; i < MAPLE_PORTS; i++) {
Adrian McMenaminbd496662008-02-24 14:30:23 +0000846 checked[i] = false;
Adrian McMenaminb233b282009-02-27 16:07:32 +0900847 empty[i] = false;
Adrian McMenaminb9482372008-02-06 22:46:21 +0000848 mdev[i] = maple_alloc_dev(i, 0);
849 if (!mdev[i]) {
850 while (i-- > 0)
851 maple_free_dev(mdev[i]);
852 goto cleanup_cache;
853 }
Adrian McMenaminb233b282009-02-27 16:07:32 +0900854 baseunits[i] = mdev[i];
855 atomic_set(&mdev[i]->busy, 1);
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900856 maple_add_packet(mdev[i], 0, MAPLE_COMMAND_DEVINFO, 0, NULL);
Adrian McMenaminb9482372008-02-06 22:46:21 +0000857 subdevice_map[i] = 0;
858 }
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900859
Adrian McMenaminb233b282009-02-27 16:07:32 +0900860 maple_pnp_time = jiffies + HZ;
861 /* prepare initial queue */
Adrian McMenaminb9482372008-02-06 22:46:21 +0000862 maple_send();
Adrian McMenaminb233b282009-02-27 16:07:32 +0900863 dev_info(&maple_bus, "bus core now registered\n");
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900864
Adrian McMenaminb9482372008-02-06 22:46:21 +0000865 return 0;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900866
Adrian McMenaminb3c69e22008-02-06 23:51:21 +0000867cleanup_cache:
Adrian McMenaminb9482372008-02-06 22:46:21 +0000868 kmem_cache_destroy(maple_queue_cache);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900869
Adrian McMenaminb3c69e22008-02-06 23:51:21 +0000870cleanup_bothirqs:
Adrian McMenaminb9482372008-02-06 22:46:21 +0000871 free_irq(HW_EVENT_VSYNC, 0);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900872
Adrian McMenaminb3c69e22008-02-06 23:51:21 +0000873cleanup_irq:
Adrian McMenaminb9482372008-02-06 22:46:21 +0000874 free_irq(HW_EVENT_MAPLE_DMA, 0);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900875
Adrian McMenaminb3c69e22008-02-06 23:51:21 +0000876cleanup_dma:
Adrian McMenaminb9482372008-02-06 22:46:21 +0000877 free_pages((unsigned long) maple_sendbuf, MAPLE_DMA_PAGES);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900878
Adrian McMenaminb3c69e22008-02-06 23:51:21 +0000879cleanup_basic:
Adrian McMenaminb233b282009-02-27 16:07:32 +0900880 driver_unregister(&maple_unsupported_device.drv);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900881
Adrian McMenaminb3c69e22008-02-06 23:51:21 +0000882cleanup_bus:
Adrian McMenaminb9482372008-02-06 22:46:21 +0000883 bus_unregister(&maple_bus_type);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900884
Adrian McMenaminb3c69e22008-02-06 23:51:21 +0000885cleanup_device:
Adrian McMenaminb9482372008-02-06 22:46:21 +0000886 device_unregister(&maple_bus);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900887
Adrian McMenaminb3c69e22008-02-06 23:51:21 +0000888cleanup:
Adrian McMenaminb233b282009-02-27 16:07:32 +0900889 printk(KERN_ERR "Maple bus registration failed\n");
Adrian McMenaminb9482372008-02-06 22:46:21 +0000890 return retval;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900891}
Adrian McMenaminb3c69e22008-02-06 23:51:21 +0000892/* Push init to later to ensure hardware gets detected */
893fs_initcall(maple_bus_init);