blob: 1e20604257af96e0ade525afadb014916a038744 [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{
Paul Mundtc0537842010-10-27 14:34:41 +090097 __raw_writel(MAPLE_MAGIC, MAPLE_RESET);
Adrian McMenaminb9482372008-02-06 22:46:21 +000098 /* set trig type to 0 for software trigger, 1 for hardware (VBLANK) */
Paul Mundtc0537842010-10-27 14:34:41 +090099 __raw_writel(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 */
Paul Mundtc0537842010-10-27 14:34:41 +0900108 __raw_writel(MAPLE_2MBPS | MAPLE_TIMEOUT(0xFFFF), MAPLE_SPEED);
109 __raw_writel(virt_to_phys(maple_sendbuf), MAPLE_DMAADDR);
110 __raw_writel(1, MAPLE_ENABLE);
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{
Paul Mundtc0537842010-10-27 14:34:41 +0900133 return (__raw_readl(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;
Paul Mundt913df442009-10-13 12:35:30 +0900261 *maple_sendptr++ = virt_to_phys(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 */
Paul Mundtc0537842010-10-27 14:34:41 +0900278 __raw_writel(0, MAPLE_ENABLE);
Adrian McMenaminb233b282009-02-27 16:07:32 +0900279
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 dev_set_name(&mdev->dev, "%d:0%d.%lX", mdev->port,
383 mdev->unit, function);
Adrian McMenaminb9482372008-02-06 22:46:21 +0000384 }
Adrian McMenaminb233b282009-02-27 16:07:32 +0900385
Adrian McMenaminb3c69e22008-02-06 23:51:21 +0000386 mdev->function = function;
387 mdev->dev.release = &maple_release_device;
Adrian McMenaminb233b282009-02-27 16:07:32 +0900388
389 atomic_set(&mdev->busy, 0);
390 error = device_register(&mdev->dev);
391 if (error) {
392 dev_warn(&mdev->dev, "could not register device at"
393 " (%d, %d), with error 0x%X\n", mdev->unit,
394 mdev->port, error);
Adrian McMenaminb3c69e22008-02-06 23:51:21 +0000395 maple_free_dev(mdev);
396 mdev = NULL;
397 return;
Adrian McMenaminb9482372008-02-06 22:46:21 +0000398 }
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900399}
400
401/*
402 * if device has been registered for the given
403 * port and unit then return 1 - allows identification
404 * of which devices need to be attached or detached
405 */
Adrian McMenaminb233b282009-02-27 16:07:32 +0900406static int check_maple_device(struct device *device, void *portptr)
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900407{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000408 struct maple_device_specify *ds;
409 struct maple_device *mdev;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900410
Adrian McMenaminb9482372008-02-06 22:46:21 +0000411 ds = portptr;
412 mdev = to_maple_dev(device);
413 if (mdev->port == ds->port && mdev->unit == ds->unit)
414 return 1;
415 return 0;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900416}
417
418static int setup_maple_commands(struct device *device, void *ignored)
419{
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900420 int add;
Adrian McMenaminb233b282009-02-27 16:07:32 +0900421 struct maple_device *mdev = to_maple_dev(device);
422 if (mdev->interval > 0 && atomic_read(&mdev->busy) == 0 &&
423 time_after(jiffies, mdev->when)) {
424 /* bounce if we cannot add */
425 add = maple_add_packet(mdev,
426 be32_to_cpu(mdev->devinfo.function),
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900427 MAPLE_COMMAND_GETCOND, 1, NULL);
428 if (!add)
Adrian McMenaminb233b282009-02-27 16:07:32 +0900429 mdev->when = jiffies + mdev->interval;
Adrian McMenaminb9482372008-02-06 22:46:21 +0000430 } else {
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900431 if (time_after(jiffies, maple_pnp_time))
Adrian McMenaminb233b282009-02-27 16:07:32 +0900432 /* Ensure we don't have block reads and devinfo
433 * calls interfering with one another - so flag the
434 * device as busy */
435 if (atomic_read(&mdev->busy) == 0) {
436 atomic_set(&mdev->busy, 1);
437 maple_add_packet(mdev, 0,
438 MAPLE_COMMAND_DEVINFO, 0, NULL);
439 }
Adrian McMenaminb9482372008-02-06 22:46:21 +0000440 }
Adrian McMenaminb9482372008-02-06 22:46:21 +0000441 return 0;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900442}
443
444/* VBLANK bottom half - implemented via workqueue */
445static void maple_vblank_handler(struct work_struct *work)
446{
Adrian McMenaminb233b282009-02-27 16:07:32 +0900447 int x, locking;
448 struct maple_device *mdev;
449
450 if (!maple_dma_done())
Adrian McMenaminb9482372008-02-06 22:46:21 +0000451 return;
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900452
Paul Mundtc0537842010-10-27 14:34:41 +0900453 __raw_writel(0, MAPLE_ENABLE);
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900454
Adrian McMenaminb233b282009-02-27 16:07:32 +0900455 if (!list_empty(&maple_sentq))
456 goto finish;
457
458 /*
459 * Set up essential commands - to fetch data and
460 * check devices are still present
461 */
Adrian McMenaminb9482372008-02-06 22:46:21 +0000462 bus_for_each_dev(&maple_bus_type, NULL, NULL,
Adrian McMenaminb233b282009-02-27 16:07:32 +0900463 setup_maple_commands);
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900464
Adrian McMenaminb233b282009-02-27 16:07:32 +0900465 if (time_after(jiffies, maple_pnp_time)) {
466 /*
467 * Scan the empty ports - bus is flakey and may have
468 * mis-reported emptyness
469 */
470 for (x = 0; x < MAPLE_PORTS; x++) {
471 if (checked[x] && empty[x]) {
472 mdev = baseunits[x];
473 if (!mdev)
474 break;
475 atomic_set(&mdev->busy, 1);
476 locking = maple_add_packet(mdev, 0,
477 MAPLE_COMMAND_DEVINFO, 0, NULL);
478 if (!locking)
479 break;
480 }
481 }
482
Adrian McMenaminb9482372008-02-06 22:46:21 +0000483 maple_pnp_time = jiffies + MAPLE_PNP_INTERVAL;
Adrian McMenaminb9482372008-02-06 22:46:21 +0000484 }
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900485
Adrian McMenaminb233b282009-02-27 16:07:32 +0900486finish:
487 maple_send();
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900488}
489
Adrian McMenaminb233b282009-02-27 16:07:32 +0900490/* handle devices added via hotplugs - placing them on queue for DEVINFO */
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900491static void maple_map_subunits(struct maple_device *mdev, int submask)
492{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000493 int retval, k, devcheck;
494 struct maple_device *mdev_add;
495 struct maple_device_specify ds;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900496
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900497 ds.port = mdev->port;
Adrian McMenaminb9482372008-02-06 22:46:21 +0000498 for (k = 0; k < 5; k++) {
Adrian McMenaminb9482372008-02-06 22:46:21 +0000499 ds.unit = k + 1;
500 retval =
501 bus_for_each_dev(&maple_bus_type, NULL, &ds,
Adrian McMenaminb233b282009-02-27 16:07:32 +0900502 check_maple_device);
Adrian McMenaminb9482372008-02-06 22:46:21 +0000503 if (retval) {
504 submask = submask >> 1;
505 continue;
506 }
507 devcheck = submask & 0x01;
508 if (devcheck) {
509 mdev_add = maple_alloc_dev(mdev->port, k + 1);
510 if (!mdev_add)
511 return;
Adrian McMenaminb233b282009-02-27 16:07:32 +0900512 atomic_set(&mdev_add->busy, 1);
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900513 maple_add_packet(mdev_add, 0, MAPLE_COMMAND_DEVINFO,
514 0, NULL);
515 /* mark that we are checking sub devices */
Adrian McMenaminb9482372008-02-06 22:46:21 +0000516 scanning = 1;
517 }
518 submask = submask >> 1;
519 }
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900520}
521
522/* mark a device as removed */
523static void maple_clean_submap(struct maple_device *mdev)
524{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000525 int killbit;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900526
Adrian McMenaminb9482372008-02-06 22:46:21 +0000527 killbit = (mdev->unit > 0 ? (1 << (mdev->unit - 1)) & 0x1f : 0x20);
528 killbit = ~killbit;
529 killbit &= 0xFF;
530 subdevice_map[mdev->port] = subdevice_map[mdev->port] & killbit;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900531}
532
533/* handle empty port or hotplug removal */
Adrian McMenaminb233b282009-02-27 16:07:32 +0900534static void maple_response_none(struct maple_device *mdev)
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900535{
Adrian McMenaminb233b282009-02-27 16:07:32 +0900536 maple_clean_submap(mdev);
537
538 if (likely(mdev->unit != 0)) {
539 /*
540 * Block devices play up
541 * and give the impression they have
542 * been removed even when still in place or
543 * trip the mtd layer when they have
544 * really gone - this code traps that eventuality
545 * and ensures we aren't overloaded with useless
546 * error messages
547 */
548 if (mdev->can_unload) {
549 if (!mdev->can_unload(mdev)) {
550 atomic_set(&mdev->busy, 2);
551 wake_up(&mdev->maple_wait);
552 return;
553 }
554 }
555
556 dev_info(&mdev->dev, "detaching device at (%d, %d)\n",
557 mdev->port, mdev->unit);
Adrian McMenaminb9482372008-02-06 22:46:21 +0000558 maple_detach_driver(mdev);
559 return;
Adrian McMenaminb233b282009-02-27 16:07:32 +0900560 } else {
561 if (!started || !fullscan) {
562 if (checked[mdev->port] == false) {
563 checked[mdev->port] = true;
564 empty[mdev->port] = true;
565 dev_info(&mdev->dev, "no devices"
566 " to port %d\n", mdev->port);
567 }
568 return;
Adrian McMenaminbd496662008-02-24 14:30:23 +0000569 }
Adrian McMenaminb9482372008-02-06 22:46:21 +0000570 }
Adrian McMenaminb233b282009-02-27 16:07:32 +0900571 /* Some hardware devices generate false detach messages on unit 0 */
572 atomic_set(&mdev->busy, 0);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900573}
574
575/* preprocess hotplugs or scans */
576static void maple_response_devinfo(struct maple_device *mdev,
Adrian McMenaminb9482372008-02-06 22:46:21 +0000577 char *recvbuf)
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900578{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000579 char submask;
Adrian McMenaminbd496662008-02-24 14:30:23 +0000580 if (!started || (scanning == 2) || !fullscan) {
581 if ((mdev->unit == 0) && (checked[mdev->port] == false)) {
582 checked[mdev->port] = true;
583 maple_attach_driver(mdev);
584 } else {
585 if (mdev->unit != 0)
586 maple_attach_driver(mdev);
Adrian McMenaminb233b282009-02-27 16:07:32 +0900587 if (mdev->unit == 0) {
588 empty[mdev->port] = false;
589 maple_attach_driver(mdev);
590 }
Adrian McMenaminbd496662008-02-24 14:30:23 +0000591 }
Adrian McMenaminb9482372008-02-06 22:46:21 +0000592 }
593 if (mdev->unit == 0) {
594 submask = recvbuf[2] & 0x1F;
595 if (submask ^ subdevice_map[mdev->port]) {
596 maple_map_subunits(mdev, submask);
597 subdevice_map[mdev->port] = submask;
598 }
599 }
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900600}
601
Adrian McMenaminb233b282009-02-27 16:07:32 +0900602static void maple_response_fileerr(struct maple_device *mdev, void *recvbuf)
603{
604 if (mdev->fileerr_handler) {
605 mdev->fileerr_handler(mdev, recvbuf);
606 return;
607 } else
608 dev_warn(&mdev->dev, "device at (%d, %d) reports"
609 "file error 0x%X\n", mdev->port, mdev->unit,
610 ((int *)recvbuf)[1]);
611}
612
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900613static void maple_port_rescan(void)
614{
615 int i;
616 struct maple_device *mdev;
617
618 fullscan = 1;
619 for (i = 0; i < MAPLE_PORTS; i++) {
620 if (checked[i] == false) {
621 fullscan = 0;
622 mdev = baseunits[i];
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900623 maple_add_packet(mdev, 0, MAPLE_COMMAND_DEVINFO,
624 0, NULL);
625 }
626 }
627}
628
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900629/* maple dma end bottom half - implemented via workqueue */
630static void maple_dma_handler(struct work_struct *work)
631{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000632 struct mapleq *mq, *nmq;
Adrian McMenaminb233b282009-02-27 16:07:32 +0900633 struct maple_device *mdev;
Adrian McMenaminb9482372008-02-06 22:46:21 +0000634 char *recvbuf;
635 enum maple_code code;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900636
Adrian McMenaminb9482372008-02-06 22:46:21 +0000637 if (!maple_dma_done())
638 return;
Paul Mundtc0537842010-10-27 14:34:41 +0900639 __raw_writel(0, MAPLE_ENABLE);
Adrian McMenaminb9482372008-02-06 22:46:21 +0000640 if (!list_empty(&maple_sentq)) {
641 list_for_each_entry_safe(mq, nmq, &maple_sentq, list) {
Adrian McMenaminb233b282009-02-27 16:07:32 +0900642 mdev = mq->dev;
643 recvbuf = mq->recvbuf->buf;
644 dma_cache_sync(&mdev->dev, recvbuf, 0x400,
645 DMA_FROM_DEVICE);
Adrian McMenaminb9482372008-02-06 22:46:21 +0000646 code = recvbuf[0];
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900647 kfree(mq->sendbuf);
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900648 list_del_init(&mq->list);
Adrian McMenaminb9482372008-02-06 22:46:21 +0000649 switch (code) {
650 case MAPLE_RESPONSE_NONE:
Adrian McMenaminb233b282009-02-27 16:07:32 +0900651 maple_response_none(mdev);
Adrian McMenaminb9482372008-02-06 22:46:21 +0000652 break;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900653
Adrian McMenaminb9482372008-02-06 22:46:21 +0000654 case MAPLE_RESPONSE_DEVINFO:
Adrian McMenaminb233b282009-02-27 16:07:32 +0900655 maple_response_devinfo(mdev, recvbuf);
656 atomic_set(&mdev->busy, 0);
Adrian McMenaminb9482372008-02-06 22:46:21 +0000657 break;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900658
Adrian McMenaminb9482372008-02-06 22:46:21 +0000659 case MAPLE_RESPONSE_DATATRF:
Adrian McMenaminb233b282009-02-27 16:07:32 +0900660 if (mdev->callback)
661 mdev->callback(mq);
662 atomic_set(&mdev->busy, 0);
663 wake_up(&mdev->maple_wait);
Adrian McMenaminb9482372008-02-06 22:46:21 +0000664 break;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900665
Adrian McMenaminb9482372008-02-06 22:46:21 +0000666 case MAPLE_RESPONSE_FILEERR:
Adrian McMenaminb233b282009-02-27 16:07:32 +0900667 maple_response_fileerr(mdev, recvbuf);
668 atomic_set(&mdev->busy, 0);
669 wake_up(&mdev->maple_wait);
670 break;
671
Adrian McMenaminb9482372008-02-06 22:46:21 +0000672 case MAPLE_RESPONSE_AGAIN:
673 case MAPLE_RESPONSE_BADCMD:
674 case MAPLE_RESPONSE_BADFUNC:
Adrian McMenaminb233b282009-02-27 16:07:32 +0900675 dev_warn(&mdev->dev, "non-fatal error"
676 " 0x%X at (%d, %d)\n", code,
677 mdev->port, mdev->unit);
678 atomic_set(&mdev->busy, 0);
Adrian McMenaminb9482372008-02-06 22:46:21 +0000679 break;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900680
Adrian McMenaminb9482372008-02-06 22:46:21 +0000681 case MAPLE_RESPONSE_ALLINFO:
Adrian McMenaminb233b282009-02-27 16:07:32 +0900682 dev_notice(&mdev->dev, "extended"
683 " device information request for (%d, %d)"
684 " but call is not supported\n", mdev->port,
685 mdev->unit);
686 atomic_set(&mdev->busy, 0);
Adrian McMenaminb9482372008-02-06 22:46:21 +0000687 break;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900688
Adrian McMenaminb9482372008-02-06 22:46:21 +0000689 case MAPLE_RESPONSE_OK:
Adrian McMenaminb233b282009-02-27 16:07:32 +0900690 atomic_set(&mdev->busy, 0);
691 wake_up(&mdev->maple_wait);
Adrian McMenaminb9482372008-02-06 22:46:21 +0000692 break;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900693
Adrian McMenaminb9482372008-02-06 22:46:21 +0000694 default:
695 break;
696 }
697 }
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900698 /* if scanning is 1 then we have subdevices to check */
Adrian McMenaminb9482372008-02-06 22:46:21 +0000699 if (scanning == 1) {
700 maple_send();
701 scanning = 2;
702 } else
703 scanning = 0;
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900704 /*check if we have actually tested all ports yet */
705 if (!fullscan)
706 maple_port_rescan();
707 /* mark that we have been through the first scan */
Adrian McMenaminb233b282009-02-27 16:07:32 +0900708 started = 1;
Adrian McMenaminb9482372008-02-06 22:46:21 +0000709 }
Adrian McMenaminb233b282009-02-27 16:07:32 +0900710 maple_send();
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900711}
712
Adrian McMenaminb233b282009-02-27 16:07:32 +0900713static irqreturn_t maple_dma_interrupt(int irq, void *dev_id)
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900714{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000715 /* Load everything into the bottom half */
716 schedule_work(&maple_dma_process);
717 return IRQ_HANDLED;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900718}
719
Adrian McMenaminb233b282009-02-27 16:07:32 +0900720static irqreturn_t maple_vblank_interrupt(int irq, void *dev_id)
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900721{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000722 schedule_work(&maple_vblank_process);
723 return IRQ_HANDLED;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900724}
725
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900726static int maple_set_dma_interrupt_handler(void)
727{
Adrian McMenaminb233b282009-02-27 16:07:32 +0900728 return request_irq(HW_EVENT_MAPLE_DMA, maple_dma_interrupt,
729 IRQF_SHARED, "maple bus DMA", &maple_unsupported_device);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900730}
731
732static int maple_set_vblank_interrupt_handler(void)
733{
Adrian McMenaminb233b282009-02-27 16:07:32 +0900734 return request_irq(HW_EVENT_VSYNC, maple_vblank_interrupt,
735 IRQF_SHARED, "maple bus VBLANK", &maple_unsupported_device);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900736}
737
738static int maple_get_dma_buffer(void)
739{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000740 maple_sendbuf =
741 (void *) __get_free_pages(GFP_KERNEL | __GFP_ZERO,
742 MAPLE_DMA_PAGES);
743 if (!maple_sendbuf)
744 return -ENOMEM;
745 return 0;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900746}
747
Adrian McMenaminb233b282009-02-27 16:07:32 +0900748static int maple_match_bus_driver(struct device *devptr,
Adrian McMenaminb9482372008-02-06 22:46:21 +0000749 struct device_driver *drvptr)
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900750{
Paul Mundt63870292008-08-04 10:39:46 +0900751 struct maple_driver *maple_drv = to_maple_driver(drvptr);
752 struct maple_device *maple_dev = to_maple_dev(devptr);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900753
Adrian McMenaminb9482372008-02-06 22:46:21 +0000754 /* Trap empty port case */
755 if (maple_dev->devinfo.function == 0xFFFFFFFF)
756 return 0;
757 else if (maple_dev->devinfo.function &
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900758 cpu_to_be32(maple_drv->function))
Adrian McMenaminb9482372008-02-06 22:46:21 +0000759 return 1;
760 return 0;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900761}
762
Adrian McMenaminb9482372008-02-06 22:46:21 +0000763static int maple_bus_uevent(struct device *dev,
764 struct kobj_uevent_env *env)
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900765{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000766 return 0;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900767}
768
769static void maple_bus_release(struct device *dev)
770{
771}
772
Adrian McMenaminb233b282009-02-27 16:07:32 +0900773static struct maple_driver maple_unsupported_device = {
Adrian McMenaminb9482372008-02-06 22:46:21 +0000774 .drv = {
Adrian McMenaminb233b282009-02-27 16:07:32 +0900775 .name = "maple_unsupported_device",
Adrian McMenaminb9482372008-02-06 22:46:21 +0000776 .bus = &maple_bus_type,
Adrian McMenaminb3c69e22008-02-06 23:51:21 +0000777 },
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900778};
Randy Dunlapee665ec2009-03-28 21:50:17 -0700779/*
Adrian McMenaminb233b282009-02-27 16:07:32 +0900780 * maple_bus_type - core maple bus structure
781 */
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900782struct bus_type maple_bus_type = {
Adrian McMenaminb9482372008-02-06 22:46:21 +0000783 .name = "maple",
Adrian McMenaminb233b282009-02-27 16:07:32 +0900784 .match = maple_match_bus_driver,
Adrian McMenaminb9482372008-02-06 22:46:21 +0000785 .uevent = maple_bus_uevent,
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900786};
787EXPORT_SYMBOL_GPL(maple_bus_type);
788
789static struct device maple_bus = {
Kay Sievers93fde772009-03-03 12:16:12 +0900790 .init_name = "maple",
Adrian McMenaminb9482372008-02-06 22:46:21 +0000791 .release = maple_bus_release,
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900792};
793
794static int __init maple_bus_init(void)
795{
Adrian McMenaminb9482372008-02-06 22:46:21 +0000796 int retval, i;
797 struct maple_device *mdev[MAPLE_PORTS];
Adrian McMenaminb233b282009-02-27 16:07:32 +0900798
Paul Mundtc0537842010-10-27 14:34:41 +0900799 __raw_writel(0, MAPLE_ENABLE);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900800
Adrian McMenaminb9482372008-02-06 22:46:21 +0000801 retval = device_register(&maple_bus);
802 if (retval)
803 goto cleanup;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900804
Adrian McMenaminb9482372008-02-06 22:46:21 +0000805 retval = bus_register(&maple_bus_type);
806 if (retval)
807 goto cleanup_device;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900808
Adrian McMenaminb233b282009-02-27 16:07:32 +0900809 retval = driver_register(&maple_unsupported_device.drv);
Adrian McMenaminb9482372008-02-06 22:46:21 +0000810 if (retval)
811 goto cleanup_bus;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900812
Adrian McMenaminb9482372008-02-06 22:46:21 +0000813 /* allocate memory for maple bus dma */
814 retval = maple_get_dma_buffer();
815 if (retval) {
Adrian McMenaminb233b282009-02-27 16:07:32 +0900816 dev_err(&maple_bus, "failed to allocate DMA buffers\n");
Adrian McMenaminb9482372008-02-06 22:46:21 +0000817 goto cleanup_basic;
818 }
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900819
Adrian McMenaminb9482372008-02-06 22:46:21 +0000820 /* set up DMA interrupt handler */
821 retval = maple_set_dma_interrupt_handler();
822 if (retval) {
Adrian McMenaminb233b282009-02-27 16:07:32 +0900823 dev_err(&maple_bus, "bus failed to grab maple "
824 "DMA IRQ\n");
Adrian McMenaminb9482372008-02-06 22:46:21 +0000825 goto cleanup_dma;
826 }
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900827
Adrian McMenaminb9482372008-02-06 22:46:21 +0000828 /* set up VBLANK interrupt handler */
829 retval = maple_set_vblank_interrupt_handler();
830 if (retval) {
Adrian McMenaminb233b282009-02-27 16:07:32 +0900831 dev_err(&maple_bus, "bus failed to grab VBLANK IRQ\n");
Adrian McMenaminb9482372008-02-06 22:46:21 +0000832 goto cleanup_irq;
833 }
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900834
Adrian McMenaminb233b282009-02-27 16:07:32 +0900835 maple_queue_cache = KMEM_CACHE(maple_buffer, SLAB_HWCACHE_ALIGN);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900836
Adrian McMenaminb9482372008-02-06 22:46:21 +0000837 if (!maple_queue_cache)
838 goto cleanup_bothirqs;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900839
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900840 INIT_LIST_HEAD(&maple_waitq);
841 INIT_LIST_HEAD(&maple_sentq);
842
Adrian McMenaminb9482372008-02-06 22:46:21 +0000843 /* setup maple ports */
844 for (i = 0; i < MAPLE_PORTS; i++) {
Adrian McMenaminbd496662008-02-24 14:30:23 +0000845 checked[i] = false;
Adrian McMenaminb233b282009-02-27 16:07:32 +0900846 empty[i] = false;
Adrian McMenaminb9482372008-02-06 22:46:21 +0000847 mdev[i] = maple_alloc_dev(i, 0);
848 if (!mdev[i]) {
849 while (i-- > 0)
850 maple_free_dev(mdev[i]);
851 goto cleanup_cache;
852 }
Adrian McMenaminb233b282009-02-27 16:07:32 +0900853 baseunits[i] = mdev[i];
854 atomic_set(&mdev[i]->busy, 1);
Adrian McMenamin1795cf42008-07-29 22:10:56 +0900855 maple_add_packet(mdev[i], 0, MAPLE_COMMAND_DEVINFO, 0, NULL);
Adrian McMenaminb9482372008-02-06 22:46:21 +0000856 subdevice_map[i] = 0;
857 }
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900858
Adrian McMenaminb233b282009-02-27 16:07:32 +0900859 maple_pnp_time = jiffies + HZ;
860 /* prepare initial queue */
Adrian McMenaminb9482372008-02-06 22:46:21 +0000861 maple_send();
Adrian McMenaminb233b282009-02-27 16:07:32 +0900862 dev_info(&maple_bus, "bus core now registered\n");
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900863
Adrian McMenaminb9482372008-02-06 22:46:21 +0000864 return 0;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900865
Adrian McMenaminb3c69e22008-02-06 23:51:21 +0000866cleanup_cache:
Adrian McMenaminb9482372008-02-06 22:46:21 +0000867 kmem_cache_destroy(maple_queue_cache);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900868
Adrian McMenaminb3c69e22008-02-06 23:51:21 +0000869cleanup_bothirqs:
Adrian McMenaminb9482372008-02-06 22:46:21 +0000870 free_irq(HW_EVENT_VSYNC, 0);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900871
Adrian McMenaminb3c69e22008-02-06 23:51:21 +0000872cleanup_irq:
Adrian McMenaminb9482372008-02-06 22:46:21 +0000873 free_irq(HW_EVENT_MAPLE_DMA, 0);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900874
Adrian McMenaminb3c69e22008-02-06 23:51:21 +0000875cleanup_dma:
Adrian McMenaminb9482372008-02-06 22:46:21 +0000876 free_pages((unsigned long) maple_sendbuf, MAPLE_DMA_PAGES);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900877
Adrian McMenaminb3c69e22008-02-06 23:51:21 +0000878cleanup_basic:
Adrian McMenaminb233b282009-02-27 16:07:32 +0900879 driver_unregister(&maple_unsupported_device.drv);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900880
Adrian McMenaminb3c69e22008-02-06 23:51:21 +0000881cleanup_bus:
Adrian McMenaminb9482372008-02-06 22:46:21 +0000882 bus_unregister(&maple_bus_type);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900883
Adrian McMenaminb3c69e22008-02-06 23:51:21 +0000884cleanup_device:
Adrian McMenaminb9482372008-02-06 22:46:21 +0000885 device_unregister(&maple_bus);
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900886
Adrian McMenaminb3c69e22008-02-06 23:51:21 +0000887cleanup:
Adrian McMenaminb233b282009-02-27 16:07:32 +0900888 printk(KERN_ERR "Maple bus registration failed\n");
Adrian McMenaminb9482372008-02-06 22:46:21 +0000889 return retval;
Adrian McMenamin17be2d22007-09-21 15:55:55 +0900890}
Adrian McMenaminb3c69e22008-02-06 23:51:21 +0000891/* Push init to later to ensure hardware gets detected */
892fs_initcall(maple_bus_init);