maple.c 18 KB

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