maple.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. /*
  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. #include <linux/delay.h>
  35. MODULE_AUTHOR("Yaegshi Takeshi, Paul Mundt, M.R. Brown, Adrian McMenamin");
  36. MODULE_DESCRIPTION("Maple bus driver for Dreamcast");
  37. MODULE_LICENSE("GPL v2");
  38. MODULE_SUPPORTED_DEVICE("{{SEGA, Dreamcast/Maple}}");
  39. static void maple_dma_handler(struct work_struct *work);
  40. static void maple_vblank_handler(struct work_struct *work);
  41. static DECLARE_WORK(maple_dma_process, maple_dma_handler);
  42. static DECLARE_WORK(maple_vblank_process, maple_vblank_handler);
  43. static LIST_HEAD(maple_waitq);
  44. static LIST_HEAD(maple_sentq);
  45. static DEFINE_MUTEX(maple_list_lock);
  46. static struct maple_driver maple_dummy_driver;
  47. static struct device maple_bus;
  48. static int subdevice_map[MAPLE_PORTS];
  49. static unsigned long *maple_sendbuf, *maple_sendptr, *maple_lastptr;
  50. static unsigned long maple_pnp_time;
  51. static int started, scanning, liststatus, realscan;
  52. static struct kmem_cache *maple_queue_cache;
  53. struct maple_device_specify {
  54. int port;
  55. int unit;
  56. };
  57. /**
  58. * maple_driver_register - register a device driver
  59. * automatically makes the driver bus a maple bus
  60. * @drv: the driver to be registered
  61. */
  62. int maple_driver_register(struct device_driver *drv)
  63. {
  64. if (!drv)
  65. return -EINVAL;
  66. drv->bus = &maple_bus_type;
  67. return driver_register(drv);
  68. }
  69. EXPORT_SYMBOL_GPL(maple_driver_register);
  70. /* set hardware registers to enable next round of dma */
  71. static void maplebus_dma_reset(void)
  72. {
  73. ctrl_outl(MAPLE_MAGIC, MAPLE_RESET);
  74. /* set trig type to 0 for software trigger, 1 for hardware (VBLANK) */
  75. ctrl_outl(1, MAPLE_TRIGTYPE);
  76. ctrl_outl(MAPLE_2MBPS | MAPLE_TIMEOUT(50000), MAPLE_SPEED);
  77. ctrl_outl(PHYSADDR(maple_sendbuf), MAPLE_DMAADDR);
  78. ctrl_outl(1, MAPLE_ENABLE);
  79. }
  80. /**
  81. * maple_getcond_callback - setup handling MAPLE_COMMAND_GETCOND
  82. * @dev: device responding
  83. * @callback: handler callback
  84. * @interval: interval in jiffies between callbacks
  85. * @function: the function code for the device
  86. */
  87. void maple_getcond_callback(struct maple_device *dev,
  88. void (*callback) (struct mapleq *mq),
  89. unsigned long interval, unsigned long function)
  90. {
  91. dev->callback = callback;
  92. dev->interval = interval;
  93. dev->function = cpu_to_be32(function);
  94. dev->when = jiffies;
  95. }
  96. EXPORT_SYMBOL_GPL(maple_getcond_callback);
  97. static int maple_dma_done(void)
  98. {
  99. return (ctrl_inl(MAPLE_STATE) & 1) == 0;
  100. }
  101. static void maple_release_device(struct device *dev)
  102. {
  103. struct maple_device *mdev;
  104. struct mapleq *mq;
  105. if (!dev)
  106. return;
  107. mdev = to_maple_dev(dev);
  108. mq = mdev->mq;
  109. if (mq) {
  110. if (mq->recvbufdcsp)
  111. kmem_cache_free(maple_queue_cache, mq->recvbufdcsp);
  112. kfree(mq);
  113. mq = NULL;
  114. }
  115. kfree(mdev);
  116. }
  117. /**
  118. * maple_add_packet - add a single instruction to the queue
  119. * @mq: instruction to add to waiting queue
  120. */
  121. void maple_add_packet(struct mapleq *mq)
  122. {
  123. mutex_lock(&maple_list_lock);
  124. list_add(&mq->list, &maple_waitq);
  125. mutex_unlock(&maple_list_lock);
  126. }
  127. EXPORT_SYMBOL_GPL(maple_add_packet);
  128. static struct mapleq *maple_allocq(struct maple_device *mdev)
  129. {
  130. struct mapleq *mq;
  131. mq = kmalloc(sizeof(*mq), GFP_KERNEL);
  132. if (!mq)
  133. return NULL;
  134. mq->dev = mdev;
  135. mq->recvbufdcsp = kmem_cache_zalloc(maple_queue_cache, GFP_KERNEL);
  136. mq->recvbuf = (void *) P2SEGADDR(mq->recvbufdcsp);
  137. if (!mq->recvbuf) {
  138. kfree(mq);
  139. return NULL;
  140. }
  141. return mq;
  142. }
  143. static struct maple_device *maple_alloc_dev(int port, int unit)
  144. {
  145. struct maple_device *mdev;
  146. mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
  147. if (!mdev)
  148. return NULL;
  149. mdev->port = port;
  150. mdev->unit = unit;
  151. mdev->mq = maple_allocq(mdev);
  152. if (!mdev->mq) {
  153. kfree(mdev);
  154. return NULL;
  155. }
  156. mdev->dev.bus = &maple_bus_type;
  157. mdev->dev.parent = &maple_bus;
  158. mdev->function = 0;
  159. return mdev;
  160. }
  161. static void maple_free_dev(struct maple_device *mdev)
  162. {
  163. if (!mdev)
  164. return;
  165. if (mdev->mq) {
  166. if (mdev->mq->recvbufdcsp)
  167. kmem_cache_free(maple_queue_cache,
  168. mdev->mq->recvbufdcsp);
  169. kfree(mdev->mq);
  170. }
  171. kfree(mdev);
  172. }
  173. /* process the command queue into a maple command block
  174. * terminating command has bit 32 of first long set to 0
  175. */
  176. static void maple_build_block(struct mapleq *mq)
  177. {
  178. int port, unit, from, to, len;
  179. unsigned long *lsendbuf = mq->sendbuf;
  180. port = mq->dev->port & 3;
  181. unit = mq->dev->unit;
  182. len = mq->length;
  183. from = port << 6;
  184. to = (port << 6) | (unit > 0 ? (1 << (unit - 1)) & 0x1f : 0x20);
  185. *maple_lastptr &= 0x7fffffff;
  186. maple_lastptr = maple_sendptr;
  187. *maple_sendptr++ = (port << 16) | len | 0x80000000;
  188. *maple_sendptr++ = PHYSADDR(mq->recvbuf);
  189. *maple_sendptr++ =
  190. mq->command | (to << 8) | (from << 16) | (len << 24);
  191. while (len-- > 0)
  192. *maple_sendptr++ = *lsendbuf++;
  193. }
  194. /* build up command queue */
  195. static void maple_send(void)
  196. {
  197. int i;
  198. int maple_packets;
  199. struct mapleq *mq, *nmq;
  200. if (!list_empty(&maple_sentq))
  201. return;
  202. if (list_empty(&maple_waitq) || !maple_dma_done())
  203. return;
  204. maple_packets = 0;
  205. maple_sendptr = maple_lastptr = maple_sendbuf;
  206. list_for_each_entry_safe(mq, nmq, &maple_waitq, list) {
  207. maple_build_block(mq);
  208. list_move(&mq->list, &maple_sentq);
  209. if (maple_packets++ > MAPLE_MAXPACKETS)
  210. break;
  211. }
  212. if (maple_packets > 0) {
  213. for (i = 0; i < (1 << MAPLE_DMA_PAGES); i++)
  214. dma_cache_sync(0, maple_sendbuf + i * PAGE_SIZE,
  215. PAGE_SIZE, DMA_BIDIRECTIONAL);
  216. }
  217. }
  218. static int attach_matching_maple_driver(struct device_driver *driver,
  219. void *devptr)
  220. {
  221. struct maple_driver *maple_drv;
  222. struct maple_device *mdev;
  223. mdev = devptr;
  224. maple_drv = to_maple_driver(driver);
  225. if (mdev->devinfo.function & be32_to_cpu(maple_drv->function)) {
  226. if (maple_drv->connect(mdev) == 0) {
  227. mdev->driver = maple_drv;
  228. return 1;
  229. }
  230. }
  231. return 0;
  232. }
  233. static void maple_detach_driver(struct maple_device *mdev)
  234. {
  235. if (!mdev)
  236. return;
  237. if (mdev->driver) {
  238. if (mdev->driver->disconnect)
  239. mdev->driver->disconnect(mdev);
  240. }
  241. mdev->driver = NULL;
  242. device_unregister(&mdev->dev);
  243. mdev = NULL;
  244. }
  245. /* process initial MAPLE_COMMAND_DEVINFO for each device or port */
  246. static void maple_attach_driver(struct maple_device *mdev)
  247. {
  248. char *p, *recvbuf;
  249. unsigned long function;
  250. int matched, retval;
  251. recvbuf = mdev->mq->recvbuf;
  252. /* copy the data as individual elements in
  253. * case of memory optimisation */
  254. memcpy(&mdev->devinfo.function, recvbuf + 4, 4);
  255. memcpy(&mdev->devinfo.function_data[0], recvbuf + 8, 12);
  256. memcpy(&mdev->devinfo.area_code, recvbuf + 20, 1);
  257. memcpy(&mdev->devinfo.connector_direction, recvbuf + 21, 1);
  258. memcpy(&mdev->devinfo.product_name[0], recvbuf + 22, 30);
  259. memcpy(&mdev->devinfo.product_licence[0], recvbuf + 52, 60);
  260. memcpy(&mdev->devinfo.standby_power, recvbuf + 112, 2);
  261. memcpy(&mdev->devinfo.max_power, recvbuf + 114, 2);
  262. memcpy(mdev->product_name, mdev->devinfo.product_name, 30);
  263. mdev->product_name[30] = '\0';
  264. memcpy(mdev->product_licence, mdev->devinfo.product_licence, 60);
  265. mdev->product_licence[60] = '\0';
  266. for (p = mdev->product_name + 29; mdev->product_name <= p; p--)
  267. if (*p == ' ')
  268. *p = '\0';
  269. else
  270. break;
  271. for (p = mdev->product_licence + 59; mdev->product_licence <= p; p--)
  272. if (*p == ' ')
  273. *p = '\0';
  274. else
  275. break;
  276. if (realscan) {
  277. printk(KERN_INFO "Maple device detected: %s\n",
  278. mdev->product_name);
  279. printk(KERN_INFO "Maple device: %s\n", mdev->product_licence);
  280. }
  281. function = be32_to_cpu(mdev->devinfo.function);
  282. if (function > 0x200) {
  283. /* Do this silently - as not a real device */
  284. function = 0;
  285. mdev->driver = &maple_dummy_driver;
  286. sprintf(mdev->dev.bus_id, "%d:0.port", mdev->port);
  287. } else {
  288. if (realscan)
  289. printk(KERN_INFO
  290. "Maple bus at (%d, %d): Function 0x%lX\n",
  291. mdev->port, mdev->unit, function);
  292. matched =
  293. bus_for_each_drv(&maple_bus_type, NULL, mdev,
  294. attach_matching_maple_driver);
  295. if (matched == 0) {
  296. /* Driver does not exist yet */
  297. if (realscan)
  298. printk(KERN_INFO
  299. "No maple driver found.\n");
  300. mdev->driver = &maple_dummy_driver;
  301. }
  302. sprintf(mdev->dev.bus_id, "%d:0%d.%lX", mdev->port,
  303. mdev->unit, function);
  304. }
  305. mdev->function = function;
  306. mdev->dev.release = &maple_release_device;
  307. retval = device_register(&mdev->dev);
  308. if (retval) {
  309. printk(KERN_INFO
  310. "Maple bus: Attempt to register device"
  311. " (%x, %x) failed.\n",
  312. mdev->port, mdev->unit);
  313. maple_free_dev(mdev);
  314. mdev = NULL;
  315. return;
  316. }
  317. }
  318. /*
  319. * if device has been registered for the given
  320. * port and unit then return 1 - allows identification
  321. * of which devices need to be attached or detached
  322. */
  323. static int detach_maple_device(struct device *device, void *portptr)
  324. {
  325. struct maple_device_specify *ds;
  326. struct maple_device *mdev;
  327. ds = portptr;
  328. mdev = to_maple_dev(device);
  329. if (mdev->port == ds->port && mdev->unit == ds->unit)
  330. return 1;
  331. return 0;
  332. }
  333. static int setup_maple_commands(struct device *device, void *ignored)
  334. {
  335. struct maple_device *maple_dev = to_maple_dev(device);
  336. if ((maple_dev->interval > 0)
  337. && time_after(jiffies, maple_dev->when)) {
  338. maple_dev->when = jiffies + maple_dev->interval;
  339. maple_dev->mq->command = MAPLE_COMMAND_GETCOND;
  340. maple_dev->mq->sendbuf = &maple_dev->function;
  341. maple_dev->mq->length = 1;
  342. maple_add_packet(maple_dev->mq);
  343. liststatus++;
  344. } else {
  345. if (time_after(jiffies, maple_pnp_time)) {
  346. maple_dev->mq->command = MAPLE_COMMAND_DEVINFO;
  347. maple_dev->mq->length = 0;
  348. maple_add_packet(maple_dev->mq);
  349. liststatus++;
  350. }
  351. }
  352. return 0;
  353. }
  354. /* VBLANK bottom half - implemented via workqueue */
  355. static void maple_vblank_handler(struct work_struct *work)
  356. {
  357. if (!maple_dma_done())
  358. return;
  359. if (!list_empty(&maple_sentq))
  360. return;
  361. ctrl_outl(0, MAPLE_ENABLE);
  362. liststatus = 0;
  363. bus_for_each_dev(&maple_bus_type, NULL, NULL,
  364. setup_maple_commands);
  365. if (time_after(jiffies, maple_pnp_time))
  366. maple_pnp_time = jiffies + MAPLE_PNP_INTERVAL;
  367. if (liststatus && list_empty(&maple_sentq)) {
  368. INIT_LIST_HEAD(&maple_sentq);
  369. maple_send();
  370. }
  371. maplebus_dma_reset();
  372. }
  373. /* handle devices added via hotplugs - placing them on queue for DEVINFO*/
  374. static void maple_map_subunits(struct maple_device *mdev, int submask)
  375. {
  376. int retval, k, devcheck;
  377. struct maple_device *mdev_add;
  378. struct maple_device_specify ds;
  379. for (k = 0; k < 5; k++) {
  380. ds.port = mdev->port;
  381. ds.unit = k + 1;
  382. retval =
  383. bus_for_each_dev(&maple_bus_type, NULL, &ds,
  384. detach_maple_device);
  385. if (retval) {
  386. submask = submask >> 1;
  387. continue;
  388. }
  389. devcheck = submask & 0x01;
  390. if (devcheck) {
  391. mdev_add = maple_alloc_dev(mdev->port, k + 1);
  392. if (!mdev_add)
  393. return;
  394. mdev_add->mq->command = MAPLE_COMMAND_DEVINFO;
  395. mdev_add->mq->length = 0;
  396. maple_add_packet(mdev_add->mq);
  397. scanning = 1;
  398. }
  399. submask = submask >> 1;
  400. }
  401. }
  402. /* mark a device as removed */
  403. static void maple_clean_submap(struct maple_device *mdev)
  404. {
  405. int killbit;
  406. killbit = (mdev->unit > 0 ? (1 << (mdev->unit - 1)) & 0x1f : 0x20);
  407. killbit = ~killbit;
  408. killbit &= 0xFF;
  409. subdevice_map[mdev->port] = subdevice_map[mdev->port] & killbit;
  410. }
  411. /* handle empty port or hotplug removal */
  412. static void maple_response_none(struct maple_device *mdev,
  413. struct mapleq *mq)
  414. {
  415. if (mdev->unit != 0) {
  416. list_del(&mq->list);
  417. maple_clean_submap(mdev);
  418. printk(KERN_INFO
  419. "Maple bus device detaching at (%d, %d)\n",
  420. mdev->port, mdev->unit);
  421. maple_detach_driver(mdev);
  422. return;
  423. }
  424. if (!started) {
  425. printk(KERN_INFO "No maple devices attached to port %d\n",
  426. mdev->port);
  427. return;
  428. }
  429. maple_clean_submap(mdev);
  430. }
  431. /* preprocess hotplugs or scans */
  432. static void maple_response_devinfo(struct maple_device *mdev,
  433. char *recvbuf)
  434. {
  435. char submask;
  436. if ((!started) || (scanning == 2)) {
  437. maple_attach_driver(mdev);
  438. return;
  439. }
  440. if (mdev->unit == 0) {
  441. submask = recvbuf[2] & 0x1F;
  442. if (submask ^ subdevice_map[mdev->port]) {
  443. maple_map_subunits(mdev, submask);
  444. subdevice_map[mdev->port] = submask;
  445. }
  446. }
  447. }
  448. /* maple dma end bottom half - implemented via workqueue */
  449. static void maple_dma_handler(struct work_struct *work)
  450. {
  451. struct mapleq *mq, *nmq;
  452. struct maple_device *dev;
  453. char *recvbuf;
  454. enum maple_code code;
  455. if (!maple_dma_done())
  456. return;
  457. ctrl_outl(0, MAPLE_ENABLE);
  458. if (!list_empty(&maple_sentq)) {
  459. list_for_each_entry_safe(mq, nmq, &maple_sentq, list) {
  460. recvbuf = mq->recvbuf;
  461. code = recvbuf[0];
  462. dev = mq->dev;
  463. switch (code) {
  464. case MAPLE_RESPONSE_NONE:
  465. maple_response_none(dev, mq);
  466. break;
  467. case MAPLE_RESPONSE_DEVINFO:
  468. maple_response_devinfo(dev, recvbuf);
  469. break;
  470. case MAPLE_RESPONSE_DATATRF:
  471. if (dev->callback)
  472. dev->callback(mq);
  473. break;
  474. case MAPLE_RESPONSE_FILEERR:
  475. case MAPLE_RESPONSE_AGAIN:
  476. case MAPLE_RESPONSE_BADCMD:
  477. case MAPLE_RESPONSE_BADFUNC:
  478. printk(KERN_DEBUG
  479. "Maple non-fatal error 0x%X\n",
  480. code);
  481. break;
  482. case MAPLE_RESPONSE_ALLINFO:
  483. printk(KERN_DEBUG
  484. "Maple - extended device information"
  485. " not supported\n");
  486. break;
  487. case MAPLE_RESPONSE_OK:
  488. break;
  489. default:
  490. break;
  491. }
  492. }
  493. INIT_LIST_HEAD(&maple_sentq);
  494. if (scanning == 1) {
  495. maple_send();
  496. scanning = 2;
  497. } else
  498. scanning = 0;
  499. if (started == 0)
  500. started = 1;
  501. }
  502. maplebus_dma_reset();
  503. }
  504. static irqreturn_t maplebus_dma_interrupt(int irq, void *dev_id)
  505. {
  506. /* Load everything into the bottom half */
  507. schedule_work(&maple_dma_process);
  508. return IRQ_HANDLED;
  509. }
  510. static irqreturn_t maplebus_vblank_interrupt(int irq, void *dev_id)
  511. {
  512. schedule_work(&maple_vblank_process);
  513. return IRQ_HANDLED;
  514. }
  515. static int maple_set_dma_interrupt_handler(void)
  516. {
  517. return request_irq(HW_EVENT_MAPLE_DMA, maplebus_dma_interrupt,
  518. IRQF_SHARED, "maple bus DMA", &maple_dummy_driver);
  519. }
  520. static int maple_set_vblank_interrupt_handler(void)
  521. {
  522. return request_irq(HW_EVENT_VSYNC, maplebus_vblank_interrupt,
  523. IRQF_SHARED, "maple bus VBLANK", &maple_dummy_driver);
  524. }
  525. static int maple_get_dma_buffer(void)
  526. {
  527. maple_sendbuf =
  528. (void *) __get_free_pages(GFP_KERNEL | __GFP_ZERO,
  529. MAPLE_DMA_PAGES);
  530. if (!maple_sendbuf)
  531. return -ENOMEM;
  532. return 0;
  533. }
  534. static int match_maple_bus_driver(struct device *devptr,
  535. struct device_driver *drvptr)
  536. {
  537. struct maple_driver *maple_drv;
  538. struct maple_device *maple_dev;
  539. maple_drv = container_of(drvptr, struct maple_driver, drv);
  540. maple_dev = container_of(devptr, struct maple_device, dev);
  541. /* Trap empty port case */
  542. if (maple_dev->devinfo.function == 0xFFFFFFFF)
  543. return 0;
  544. else if (maple_dev->devinfo.function &
  545. be32_to_cpu(maple_drv->function))
  546. return 1;
  547. return 0;
  548. }
  549. static int maple_bus_uevent(struct device *dev,
  550. struct kobj_uevent_env *env)
  551. {
  552. return 0;
  553. }
  554. static void maple_bus_release(struct device *dev)
  555. {
  556. }
  557. static struct maple_driver maple_dummy_driver = {
  558. .drv = {
  559. .name = "maple_dummy_driver",
  560. .bus = &maple_bus_type,
  561. },
  562. };
  563. struct bus_type maple_bus_type = {
  564. .name = "maple",
  565. .match = match_maple_bus_driver,
  566. .uevent = maple_bus_uevent,
  567. };
  568. EXPORT_SYMBOL_GPL(maple_bus_type);
  569. static struct device maple_bus = {
  570. .bus_id = "maple",
  571. .release = maple_bus_release,
  572. };
  573. static int __init maple_bus_init(void)
  574. {
  575. int retval, i;
  576. struct maple_device *mdev[MAPLE_PORTS];
  577. ctrl_outl(0, MAPLE_STATE);
  578. retval = device_register(&maple_bus);
  579. if (retval)
  580. goto cleanup;
  581. retval = bus_register(&maple_bus_type);
  582. if (retval)
  583. goto cleanup_device;
  584. retval = driver_register(&maple_dummy_driver.drv);
  585. if (retval)
  586. goto cleanup_bus;
  587. /* allocate memory for maple bus dma */
  588. retval = maple_get_dma_buffer();
  589. if (retval) {
  590. printk(KERN_INFO
  591. "Maple bus: Failed to allocate Maple DMA buffers\n");
  592. goto cleanup_basic;
  593. }
  594. /* set up DMA interrupt handler */
  595. retval = maple_set_dma_interrupt_handler();
  596. if (retval) {
  597. printk(KERN_INFO
  598. "Maple bus: Failed to grab maple DMA IRQ\n");
  599. goto cleanup_dma;
  600. }
  601. /* set up VBLANK interrupt handler */
  602. retval = maple_set_vblank_interrupt_handler();
  603. if (retval) {
  604. printk(KERN_INFO "Maple bus: Failed to grab VBLANK IRQ\n");
  605. goto cleanup_irq;
  606. }
  607. maple_queue_cache =
  608. kmem_cache_create("maple_queue_cache", 0x400, 0,
  609. SLAB_POISON|SLAB_HWCACHE_ALIGN, NULL);
  610. if (!maple_queue_cache)
  611. goto cleanup_bothirqs;
  612. /* setup maple ports */
  613. for (i = 0; i < MAPLE_PORTS; i++) {
  614. mdev[i] = maple_alloc_dev(i, 0);
  615. if (!mdev[i]) {
  616. while (i-- > 0)
  617. maple_free_dev(mdev[i]);
  618. goto cleanup_cache;
  619. }
  620. mdev[i]->mq->command = MAPLE_COMMAND_DEVINFO;
  621. mdev[i]->mq->length = 0;
  622. maple_add_packet(mdev[i]->mq);
  623. /* delay aids hardware detection */
  624. udelay(20);
  625. subdevice_map[i] = 0;
  626. }
  627. realscan = 1;
  628. /* setup maplebus hardware */
  629. maplebus_dma_reset();
  630. /* initial detection */
  631. maple_send();
  632. maple_pnp_time = jiffies;
  633. printk(KERN_INFO "Maple bus core now registered.\n");
  634. return 0;
  635. cleanup_cache:
  636. kmem_cache_destroy(maple_queue_cache);
  637. cleanup_bothirqs:
  638. free_irq(HW_EVENT_VSYNC, 0);
  639. cleanup_irq:
  640. free_irq(HW_EVENT_MAPLE_DMA, 0);
  641. cleanup_dma:
  642. free_pages((unsigned long) maple_sendbuf, MAPLE_DMA_PAGES);
  643. cleanup_basic:
  644. driver_unregister(&maple_dummy_driver.drv);
  645. cleanup_bus:
  646. bus_unregister(&maple_bus_type);
  647. cleanup_device:
  648. device_unregister(&maple_bus);
  649. cleanup:
  650. printk(KERN_INFO "Maple bus registration failed\n");
  651. return retval;
  652. }
  653. /* Push init to later to ensure hardware gets detected */
  654. fs_initcall(maple_bus_init);