maple.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. /*
  2. * Core maple bus functionality
  3. *
  4. * Copyright (C) 2007 - 2009 Adrian McMenamin
  5. * Copyright (C) 2001 - 2008 Paul Mundt
  6. * Copyright (C) 2000 - 2001 YAEGASHI Takeshi
  7. * Copyright (C) 2001 M. R. Brown
  8. *
  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>
  16. #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>
  22. #include <linux/delay.h>
  23. #include <linux/module.h>
  24. #include <asm/cacheflush.h>
  25. #include <asm/dma.h>
  26. #include <asm/io.h>
  27. #include <mach/dma.h>
  28. #include <mach/sysasic.h>
  29. MODULE_AUTHOR("Adrian McMenamin <adrian@mcmen.demon.co.uk>");
  30. MODULE_DESCRIPTION("Maple bus driver for Dreamcast");
  31. MODULE_LICENSE("GPL v2");
  32. MODULE_SUPPORTED_DEVICE("{{SEGA, Dreamcast/Maple}}");
  33. static void maple_dma_handler(struct work_struct *work);
  34. static void maple_vblank_handler(struct work_struct *work);
  35. static DECLARE_WORK(maple_dma_process, maple_dma_handler);
  36. static DECLARE_WORK(maple_vblank_process, maple_vblank_handler);
  37. static LIST_HEAD(maple_waitq);
  38. static LIST_HEAD(maple_sentq);
  39. /* mutex to protect queue of waiting packets */
  40. static DEFINE_MUTEX(maple_wlist_lock);
  41. static struct maple_driver maple_unsupported_device;
  42. static struct device maple_bus;
  43. static int subdevice_map[MAPLE_PORTS];
  44. static unsigned long *maple_sendbuf, *maple_sendptr, *maple_lastptr;
  45. static unsigned long maple_pnp_time;
  46. static int started, scanning, fullscan;
  47. static struct kmem_cache *maple_queue_cache;
  48. struct maple_device_specify {
  49. int port;
  50. int unit;
  51. };
  52. static bool checked[MAPLE_PORTS];
  53. static bool empty[MAPLE_PORTS];
  54. static struct maple_device *baseunits[MAPLE_PORTS];
  55. /**
  56. * maple_driver_register - register a maple driver
  57. * @drv: maple driver to be registered.
  58. *
  59. * Registers the passed in @drv, while updating the bus type.
  60. * Devices with matching function IDs will be automatically probed.
  61. */
  62. int maple_driver_register(struct maple_driver *drv)
  63. {
  64. if (!drv)
  65. return -EINVAL;
  66. drv->drv.bus = &maple_bus_type;
  67. return driver_register(&drv->drv);
  68. }
  69. EXPORT_SYMBOL_GPL(maple_driver_register);
  70. /**
  71. * maple_driver_unregister - unregister a maple driver.
  72. * @drv: maple driver to unregister.
  73. *
  74. * Cleans up after maple_driver_register(). To be invoked in the exit
  75. * path of any module drivers.
  76. */
  77. void maple_driver_unregister(struct maple_driver *drv)
  78. {
  79. driver_unregister(&drv->drv);
  80. }
  81. EXPORT_SYMBOL_GPL(maple_driver_unregister);
  82. /* set hardware registers to enable next round of dma */
  83. static void maple_dma_reset(void)
  84. {
  85. __raw_writel(MAPLE_MAGIC, MAPLE_RESET);
  86. /* set trig type to 0 for software trigger, 1 for hardware (VBLANK) */
  87. __raw_writel(1, MAPLE_TRIGTYPE);
  88. /*
  89. * Maple system register
  90. * bits 31 - 16 timeout in units of 20nsec
  91. * bit 12 hard trigger - set 0 to keep responding to VBLANK
  92. * bits 9 - 8 set 00 for 2 Mbps, 01 for 1 Mbps
  93. * bits 3 - 0 delay (in 1.3ms) between VBLANK and start of DMA
  94. * max delay is 11
  95. */
  96. __raw_writel(MAPLE_2MBPS | MAPLE_TIMEOUT(0xFFFF), MAPLE_SPEED);
  97. __raw_writel(virt_to_phys(maple_sendbuf), MAPLE_DMAADDR);
  98. __raw_writel(1, MAPLE_ENABLE);
  99. }
  100. /**
  101. * maple_getcond_callback - setup handling MAPLE_COMMAND_GETCOND
  102. * @dev: device responding
  103. * @callback: handler callback
  104. * @interval: interval in jiffies between callbacks
  105. * @function: the function code for the device
  106. */
  107. void maple_getcond_callback(struct maple_device *dev,
  108. void (*callback) (struct mapleq *mq),
  109. unsigned long interval, unsigned long function)
  110. {
  111. dev->callback = callback;
  112. dev->interval = interval;
  113. dev->function = cpu_to_be32(function);
  114. dev->when = jiffies;
  115. }
  116. EXPORT_SYMBOL_GPL(maple_getcond_callback);
  117. static int maple_dma_done(void)
  118. {
  119. return (__raw_readl(MAPLE_STATE) & 1) == 0;
  120. }
  121. static void maple_release_device(struct device *dev)
  122. {
  123. struct maple_device *mdev;
  124. struct mapleq *mq;
  125. mdev = to_maple_dev(dev);
  126. mq = mdev->mq;
  127. kmem_cache_free(maple_queue_cache, mq->recvbuf);
  128. kfree(mq);
  129. kfree(mdev);
  130. }
  131. /**
  132. * maple_add_packet - add a single instruction to the maple bus queue
  133. * @mdev: maple device
  134. * @function: function on device being queried
  135. * @command: maple command to add
  136. * @length: length of command string (in 32 bit words)
  137. * @data: remainder of command string
  138. */
  139. int maple_add_packet(struct maple_device *mdev, u32 function, u32 command,
  140. size_t length, void *data)
  141. {
  142. int ret = 0;
  143. void *sendbuf = NULL;
  144. if (length) {
  145. sendbuf = kcalloc(length, 4, GFP_KERNEL);
  146. if (!sendbuf) {
  147. ret = -ENOMEM;
  148. goto out;
  149. }
  150. ((__be32 *)sendbuf)[0] = cpu_to_be32(function);
  151. }
  152. mdev->mq->command = command;
  153. mdev->mq->length = length;
  154. if (length > 1)
  155. memcpy(sendbuf + 4, data, (length - 1) * 4);
  156. mdev->mq->sendbuf = sendbuf;
  157. mutex_lock(&maple_wlist_lock);
  158. list_add_tail(&mdev->mq->list, &maple_waitq);
  159. mutex_unlock(&maple_wlist_lock);
  160. out:
  161. return ret;
  162. }
  163. EXPORT_SYMBOL_GPL(maple_add_packet);
  164. static struct mapleq *maple_allocq(struct maple_device *mdev)
  165. {
  166. struct mapleq *mq;
  167. mq = kzalloc(sizeof(*mq), GFP_KERNEL);
  168. if (!mq)
  169. goto failed_nomem;
  170. INIT_LIST_HEAD(&mq->list);
  171. mq->dev = mdev;
  172. mq->recvbuf = kmem_cache_zalloc(maple_queue_cache, GFP_KERNEL);
  173. if (!mq->recvbuf)
  174. goto failed_p2;
  175. mq->recvbuf->buf = &((mq->recvbuf->bufx)[0]);
  176. return mq;
  177. failed_p2:
  178. kfree(mq);
  179. failed_nomem:
  180. dev_err(&mdev->dev, "could not allocate memory for device (%d, %d)\n",
  181. mdev->port, mdev->unit);
  182. return NULL;
  183. }
  184. static struct maple_device *maple_alloc_dev(int port, int unit)
  185. {
  186. struct maple_device *mdev;
  187. /* zero this out to avoid kobj subsystem
  188. * thinking it has already been registered */
  189. mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
  190. if (!mdev)
  191. return NULL;
  192. mdev->port = port;
  193. mdev->unit = unit;
  194. mdev->mq = maple_allocq(mdev);
  195. if (!mdev->mq) {
  196. kfree(mdev);
  197. return NULL;
  198. }
  199. mdev->dev.bus = &maple_bus_type;
  200. mdev->dev.parent = &maple_bus;
  201. init_waitqueue_head(&mdev->maple_wait);
  202. return mdev;
  203. }
  204. static void maple_free_dev(struct maple_device *mdev)
  205. {
  206. kmem_cache_free(maple_queue_cache, mdev->mq->recvbuf);
  207. kfree(mdev->mq);
  208. kfree(mdev);
  209. }
  210. /* process the command queue into a maple command block
  211. * terminating command has bit 32 of first long set to 0
  212. */
  213. static void maple_build_block(struct mapleq *mq)
  214. {
  215. int port, unit, from, to, len;
  216. unsigned long *lsendbuf = mq->sendbuf;
  217. port = mq->dev->port & 3;
  218. unit = mq->dev->unit;
  219. len = mq->length;
  220. from = port << 6;
  221. to = (port << 6) | (unit > 0 ? (1 << (unit - 1)) & 0x1f : 0x20);
  222. *maple_lastptr &= 0x7fffffff;
  223. maple_lastptr = maple_sendptr;
  224. *maple_sendptr++ = (port << 16) | len | 0x80000000;
  225. *maple_sendptr++ = virt_to_phys(mq->recvbuf->buf);
  226. *maple_sendptr++ =
  227. mq->command | (to << 8) | (from << 16) | (len << 24);
  228. while (len-- > 0)
  229. *maple_sendptr++ = *lsendbuf++;
  230. }
  231. /* build up command queue */
  232. static void maple_send(void)
  233. {
  234. int i, maple_packets = 0;
  235. struct mapleq *mq, *nmq;
  236. if (!maple_dma_done())
  237. return;
  238. /* disable DMA */
  239. __raw_writel(0, MAPLE_ENABLE);
  240. if (!list_empty(&maple_sentq))
  241. goto finish;
  242. mutex_lock(&maple_wlist_lock);
  243. if (list_empty(&maple_waitq)) {
  244. mutex_unlock(&maple_wlist_lock);
  245. goto finish;
  246. }
  247. maple_lastptr = maple_sendbuf;
  248. maple_sendptr = maple_sendbuf;
  249. list_for_each_entry_safe(mq, nmq, &maple_waitq, list) {
  250. maple_build_block(mq);
  251. list_del_init(&mq->list);
  252. list_add_tail(&mq->list, &maple_sentq);
  253. if (maple_packets++ > MAPLE_MAXPACKETS)
  254. break;
  255. }
  256. mutex_unlock(&maple_wlist_lock);
  257. if (maple_packets > 0) {
  258. for (i = 0; i < (1 << MAPLE_DMA_PAGES); i++)
  259. __flush_purge_region(maple_sendbuf + i * PAGE_SIZE,
  260. PAGE_SIZE);
  261. }
  262. finish:
  263. maple_dma_reset();
  264. }
  265. /* check if there is a driver registered likely to match this device */
  266. static int maple_check_matching_driver(struct device_driver *driver,
  267. void *devptr)
  268. {
  269. struct maple_driver *maple_drv;
  270. struct maple_device *mdev;
  271. mdev = devptr;
  272. maple_drv = to_maple_driver(driver);
  273. if (mdev->devinfo.function & cpu_to_be32(maple_drv->function))
  274. return 1;
  275. return 0;
  276. }
  277. static void maple_detach_driver(struct maple_device *mdev)
  278. {
  279. device_unregister(&mdev->dev);
  280. }
  281. /* process initial MAPLE_COMMAND_DEVINFO for each device or port */
  282. static void maple_attach_driver(struct maple_device *mdev)
  283. {
  284. char *p, *recvbuf;
  285. unsigned long function;
  286. int matched, error;
  287. recvbuf = mdev->mq->recvbuf->buf;
  288. /* copy the data as individual elements in
  289. * case of memory optimisation */
  290. memcpy(&mdev->devinfo.function, recvbuf + 4, 4);
  291. memcpy(&mdev->devinfo.function_data[0], recvbuf + 8, 12);
  292. memcpy(&mdev->devinfo.area_code, recvbuf + 20, 1);
  293. memcpy(&mdev->devinfo.connector_direction, recvbuf + 21, 1);
  294. memcpy(&mdev->devinfo.product_name[0], recvbuf + 22, 30);
  295. memcpy(&mdev->devinfo.standby_power, recvbuf + 112, 2);
  296. memcpy(&mdev->devinfo.max_power, recvbuf + 114, 2);
  297. memcpy(mdev->product_name, mdev->devinfo.product_name, 30);
  298. mdev->product_name[30] = '\0';
  299. memcpy(mdev->product_licence, mdev->devinfo.product_licence, 60);
  300. mdev->product_licence[60] = '\0';
  301. for (p = mdev->product_name + 29; mdev->product_name <= p; p--)
  302. if (*p == ' ')
  303. *p = '\0';
  304. else
  305. break;
  306. for (p = mdev->product_licence + 59; mdev->product_licence <= p; p--)
  307. if (*p == ' ')
  308. *p = '\0';
  309. else
  310. break;
  311. function = be32_to_cpu(mdev->devinfo.function);
  312. dev_info(&mdev->dev, "detected %s: function 0x%lX: at (%d, %d)\n",
  313. mdev->product_name, function, mdev->port, mdev->unit);
  314. if (function > 0x200) {
  315. /* Do this silently - as not a real device */
  316. function = 0;
  317. mdev->driver = &maple_unsupported_device;
  318. dev_set_name(&mdev->dev, "%d:0.port", mdev->port);
  319. } else {
  320. matched =
  321. bus_for_each_drv(&maple_bus_type, NULL, mdev,
  322. maple_check_matching_driver);
  323. if (matched == 0) {
  324. /* Driver does not exist yet */
  325. dev_info(&mdev->dev, "no driver found\n");
  326. mdev->driver = &maple_unsupported_device;
  327. }
  328. dev_set_name(&mdev->dev, "%d:0%d.%lX", mdev->port,
  329. mdev->unit, function);
  330. }
  331. mdev->function = function;
  332. mdev->dev.release = &maple_release_device;
  333. atomic_set(&mdev->busy, 0);
  334. error = device_register(&mdev->dev);
  335. if (error) {
  336. dev_warn(&mdev->dev, "could not register device at"
  337. " (%d, %d), with error 0x%X\n", mdev->unit,
  338. mdev->port, error);
  339. maple_free_dev(mdev);
  340. mdev = NULL;
  341. return;
  342. }
  343. }
  344. /*
  345. * if device has been registered for the given
  346. * port and unit then return 1 - allows identification
  347. * of which devices need to be attached or detached
  348. */
  349. static int check_maple_device(struct device *device, void *portptr)
  350. {
  351. struct maple_device_specify *ds;
  352. struct maple_device *mdev;
  353. ds = portptr;
  354. mdev = to_maple_dev(device);
  355. if (mdev->port == ds->port && mdev->unit == ds->unit)
  356. return 1;
  357. return 0;
  358. }
  359. static int setup_maple_commands(struct device *device, void *ignored)
  360. {
  361. int add;
  362. struct maple_device *mdev = to_maple_dev(device);
  363. if (mdev->interval > 0 && atomic_read(&mdev->busy) == 0 &&
  364. time_after(jiffies, mdev->when)) {
  365. /* bounce if we cannot add */
  366. add = maple_add_packet(mdev,
  367. be32_to_cpu(mdev->devinfo.function),
  368. MAPLE_COMMAND_GETCOND, 1, NULL);
  369. if (!add)
  370. mdev->when = jiffies + mdev->interval;
  371. } else {
  372. if (time_after(jiffies, maple_pnp_time))
  373. /* Ensure we don't have block reads and devinfo
  374. * calls interfering with one another - so flag the
  375. * device as busy */
  376. if (atomic_read(&mdev->busy) == 0) {
  377. atomic_set(&mdev->busy, 1);
  378. maple_add_packet(mdev, 0,
  379. MAPLE_COMMAND_DEVINFO, 0, NULL);
  380. }
  381. }
  382. return 0;
  383. }
  384. /* VBLANK bottom half - implemented via workqueue */
  385. static void maple_vblank_handler(struct work_struct *work)
  386. {
  387. int x, locking;
  388. struct maple_device *mdev;
  389. if (!maple_dma_done())
  390. return;
  391. __raw_writel(0, MAPLE_ENABLE);
  392. if (!list_empty(&maple_sentq))
  393. goto finish;
  394. /*
  395. * Set up essential commands - to fetch data and
  396. * check devices are still present
  397. */
  398. bus_for_each_dev(&maple_bus_type, NULL, NULL,
  399. setup_maple_commands);
  400. if (time_after(jiffies, maple_pnp_time)) {
  401. /*
  402. * Scan the empty ports - bus is flakey and may have
  403. * mis-reported emptyness
  404. */
  405. for (x = 0; x < MAPLE_PORTS; x++) {
  406. if (checked[x] && empty[x]) {
  407. mdev = baseunits[x];
  408. if (!mdev)
  409. break;
  410. atomic_set(&mdev->busy, 1);
  411. locking = maple_add_packet(mdev, 0,
  412. MAPLE_COMMAND_DEVINFO, 0, NULL);
  413. if (!locking)
  414. break;
  415. }
  416. }
  417. maple_pnp_time = jiffies + MAPLE_PNP_INTERVAL;
  418. }
  419. finish:
  420. maple_send();
  421. }
  422. /* handle devices added via hotplugs - placing them on queue for DEVINFO */
  423. static void maple_map_subunits(struct maple_device *mdev, int submask)
  424. {
  425. int retval, k, devcheck;
  426. struct maple_device *mdev_add;
  427. struct maple_device_specify ds;
  428. ds.port = mdev->port;
  429. for (k = 0; k < 5; k++) {
  430. ds.unit = k + 1;
  431. retval =
  432. bus_for_each_dev(&maple_bus_type, NULL, &ds,
  433. check_maple_device);
  434. if (retval) {
  435. submask = submask >> 1;
  436. continue;
  437. }
  438. devcheck = submask & 0x01;
  439. if (devcheck) {
  440. mdev_add = maple_alloc_dev(mdev->port, k + 1);
  441. if (!mdev_add)
  442. return;
  443. atomic_set(&mdev_add->busy, 1);
  444. maple_add_packet(mdev_add, 0, MAPLE_COMMAND_DEVINFO,
  445. 0, NULL);
  446. /* mark that we are checking sub devices */
  447. scanning = 1;
  448. }
  449. submask = submask >> 1;
  450. }
  451. }
  452. /* mark a device as removed */
  453. static void maple_clean_submap(struct maple_device *mdev)
  454. {
  455. int killbit;
  456. killbit = (mdev->unit > 0 ? (1 << (mdev->unit - 1)) & 0x1f : 0x20);
  457. killbit = ~killbit;
  458. killbit &= 0xFF;
  459. subdevice_map[mdev->port] = subdevice_map[mdev->port] & killbit;
  460. }
  461. /* handle empty port or hotplug removal */
  462. static void maple_response_none(struct maple_device *mdev)
  463. {
  464. maple_clean_submap(mdev);
  465. if (likely(mdev->unit != 0)) {
  466. /*
  467. * Block devices play up
  468. * and give the impression they have
  469. * been removed even when still in place or
  470. * trip the mtd layer when they have
  471. * really gone - this code traps that eventuality
  472. * and ensures we aren't overloaded with useless
  473. * error messages
  474. */
  475. if (mdev->can_unload) {
  476. if (!mdev->can_unload(mdev)) {
  477. atomic_set(&mdev->busy, 2);
  478. wake_up(&mdev->maple_wait);
  479. return;
  480. }
  481. }
  482. dev_info(&mdev->dev, "detaching device at (%d, %d)\n",
  483. mdev->port, mdev->unit);
  484. maple_detach_driver(mdev);
  485. return;
  486. } else {
  487. if (!started || !fullscan) {
  488. if (checked[mdev->port] == false) {
  489. checked[mdev->port] = true;
  490. empty[mdev->port] = true;
  491. dev_info(&mdev->dev, "no devices"
  492. " to port %d\n", mdev->port);
  493. }
  494. return;
  495. }
  496. }
  497. /* Some hardware devices generate false detach messages on unit 0 */
  498. atomic_set(&mdev->busy, 0);
  499. }
  500. /* preprocess hotplugs or scans */
  501. static void maple_response_devinfo(struct maple_device *mdev,
  502. char *recvbuf)
  503. {
  504. char submask;
  505. if (!started || (scanning == 2) || !fullscan) {
  506. if ((mdev->unit == 0) && (checked[mdev->port] == false)) {
  507. checked[mdev->port] = true;
  508. maple_attach_driver(mdev);
  509. } else {
  510. if (mdev->unit != 0)
  511. maple_attach_driver(mdev);
  512. if (mdev->unit == 0) {
  513. empty[mdev->port] = false;
  514. maple_attach_driver(mdev);
  515. }
  516. }
  517. }
  518. if (mdev->unit == 0) {
  519. submask = recvbuf[2] & 0x1F;
  520. if (submask ^ subdevice_map[mdev->port]) {
  521. maple_map_subunits(mdev, submask);
  522. subdevice_map[mdev->port] = submask;
  523. }
  524. }
  525. }
  526. static void maple_response_fileerr(struct maple_device *mdev, void *recvbuf)
  527. {
  528. if (mdev->fileerr_handler) {
  529. mdev->fileerr_handler(mdev, recvbuf);
  530. return;
  531. } else
  532. dev_warn(&mdev->dev, "device at (%d, %d) reports"
  533. "file error 0x%X\n", mdev->port, mdev->unit,
  534. ((int *)recvbuf)[1]);
  535. }
  536. static void maple_port_rescan(void)
  537. {
  538. int i;
  539. struct maple_device *mdev;
  540. fullscan = 1;
  541. for (i = 0; i < MAPLE_PORTS; i++) {
  542. if (checked[i] == false) {
  543. fullscan = 0;
  544. mdev = baseunits[i];
  545. maple_add_packet(mdev, 0, MAPLE_COMMAND_DEVINFO,
  546. 0, NULL);
  547. }
  548. }
  549. }
  550. /* maple dma end bottom half - implemented via workqueue */
  551. static void maple_dma_handler(struct work_struct *work)
  552. {
  553. struct mapleq *mq, *nmq;
  554. struct maple_device *mdev;
  555. char *recvbuf;
  556. enum maple_code code;
  557. if (!maple_dma_done())
  558. return;
  559. __raw_writel(0, MAPLE_ENABLE);
  560. if (!list_empty(&maple_sentq)) {
  561. list_for_each_entry_safe(mq, nmq, &maple_sentq, list) {
  562. mdev = mq->dev;
  563. recvbuf = mq->recvbuf->buf;
  564. __flush_invalidate_region(sh_cacheop_vaddr(recvbuf),
  565. 0x400);
  566. code = recvbuf[0];
  567. kfree(mq->sendbuf);
  568. list_del_init(&mq->list);
  569. switch (code) {
  570. case MAPLE_RESPONSE_NONE:
  571. maple_response_none(mdev);
  572. break;
  573. case MAPLE_RESPONSE_DEVINFO:
  574. maple_response_devinfo(mdev, recvbuf);
  575. atomic_set(&mdev->busy, 0);
  576. break;
  577. case MAPLE_RESPONSE_DATATRF:
  578. if (mdev->callback)
  579. mdev->callback(mq);
  580. atomic_set(&mdev->busy, 0);
  581. wake_up(&mdev->maple_wait);
  582. break;
  583. case MAPLE_RESPONSE_FILEERR:
  584. maple_response_fileerr(mdev, recvbuf);
  585. atomic_set(&mdev->busy, 0);
  586. wake_up(&mdev->maple_wait);
  587. break;
  588. case MAPLE_RESPONSE_AGAIN:
  589. case MAPLE_RESPONSE_BADCMD:
  590. case MAPLE_RESPONSE_BADFUNC:
  591. dev_warn(&mdev->dev, "non-fatal error"
  592. " 0x%X at (%d, %d)\n", code,
  593. mdev->port, mdev->unit);
  594. atomic_set(&mdev->busy, 0);
  595. break;
  596. case MAPLE_RESPONSE_ALLINFO:
  597. dev_notice(&mdev->dev, "extended"
  598. " device information request for (%d, %d)"
  599. " but call is not supported\n", mdev->port,
  600. mdev->unit);
  601. atomic_set(&mdev->busy, 0);
  602. break;
  603. case MAPLE_RESPONSE_OK:
  604. atomic_set(&mdev->busy, 0);
  605. wake_up(&mdev->maple_wait);
  606. break;
  607. default:
  608. break;
  609. }
  610. }
  611. /* if scanning is 1 then we have subdevices to check */
  612. if (scanning == 1) {
  613. maple_send();
  614. scanning = 2;
  615. } else
  616. scanning = 0;
  617. /*check if we have actually tested all ports yet */
  618. if (!fullscan)
  619. maple_port_rescan();
  620. /* mark that we have been through the first scan */
  621. started = 1;
  622. }
  623. maple_send();
  624. }
  625. static irqreturn_t maple_dma_interrupt(int irq, void *dev_id)
  626. {
  627. /* Load everything into the bottom half */
  628. schedule_work(&maple_dma_process);
  629. return IRQ_HANDLED;
  630. }
  631. static irqreturn_t maple_vblank_interrupt(int irq, void *dev_id)
  632. {
  633. schedule_work(&maple_vblank_process);
  634. return IRQ_HANDLED;
  635. }
  636. static int maple_set_dma_interrupt_handler(void)
  637. {
  638. return request_irq(HW_EVENT_MAPLE_DMA, maple_dma_interrupt,
  639. IRQF_SHARED, "maple bus DMA", &maple_unsupported_device);
  640. }
  641. static int maple_set_vblank_interrupt_handler(void)
  642. {
  643. return request_irq(HW_EVENT_VSYNC, maple_vblank_interrupt,
  644. IRQF_SHARED, "maple bus VBLANK", &maple_unsupported_device);
  645. }
  646. static int maple_get_dma_buffer(void)
  647. {
  648. maple_sendbuf =
  649. (void *) __get_free_pages(GFP_KERNEL | __GFP_ZERO,
  650. MAPLE_DMA_PAGES);
  651. if (!maple_sendbuf)
  652. return -ENOMEM;
  653. return 0;
  654. }
  655. static int maple_match_bus_driver(struct device *devptr,
  656. struct device_driver *drvptr)
  657. {
  658. struct maple_driver *maple_drv = to_maple_driver(drvptr);
  659. struct maple_device *maple_dev = to_maple_dev(devptr);
  660. /* Trap empty port case */
  661. if (maple_dev->devinfo.function == 0xFFFFFFFF)
  662. return 0;
  663. else if (maple_dev->devinfo.function &
  664. cpu_to_be32(maple_drv->function))
  665. return 1;
  666. return 0;
  667. }
  668. static int maple_bus_uevent(struct device *dev,
  669. struct kobj_uevent_env *env)
  670. {
  671. return 0;
  672. }
  673. static void maple_bus_release(struct device *dev)
  674. {
  675. }
  676. static struct maple_driver maple_unsupported_device = {
  677. .drv = {
  678. .name = "maple_unsupported_device",
  679. .bus = &maple_bus_type,
  680. },
  681. };
  682. /*
  683. * maple_bus_type - core maple bus structure
  684. */
  685. struct bus_type maple_bus_type = {
  686. .name = "maple",
  687. .match = maple_match_bus_driver,
  688. .uevent = maple_bus_uevent,
  689. };
  690. EXPORT_SYMBOL_GPL(maple_bus_type);
  691. static struct device maple_bus = {
  692. .init_name = "maple",
  693. .release = maple_bus_release,
  694. };
  695. static int __init maple_bus_init(void)
  696. {
  697. int retval, i;
  698. struct maple_device *mdev[MAPLE_PORTS];
  699. __raw_writel(0, MAPLE_ENABLE);
  700. retval = device_register(&maple_bus);
  701. if (retval)
  702. goto cleanup;
  703. retval = bus_register(&maple_bus_type);
  704. if (retval)
  705. goto cleanup_device;
  706. retval = driver_register(&maple_unsupported_device.drv);
  707. if (retval)
  708. goto cleanup_bus;
  709. /* allocate memory for maple bus dma */
  710. retval = maple_get_dma_buffer();
  711. if (retval) {
  712. dev_err(&maple_bus, "failed to allocate DMA buffers\n");
  713. goto cleanup_basic;
  714. }
  715. /* set up DMA interrupt handler */
  716. retval = maple_set_dma_interrupt_handler();
  717. if (retval) {
  718. dev_err(&maple_bus, "bus failed to grab maple "
  719. "DMA IRQ\n");
  720. goto cleanup_dma;
  721. }
  722. /* set up VBLANK interrupt handler */
  723. retval = maple_set_vblank_interrupt_handler();
  724. if (retval) {
  725. dev_err(&maple_bus, "bus failed to grab VBLANK IRQ\n");
  726. goto cleanup_irq;
  727. }
  728. maple_queue_cache = KMEM_CACHE(maple_buffer, SLAB_HWCACHE_ALIGN);
  729. if (!maple_queue_cache) {
  730. retval = -ENOMEM;
  731. goto cleanup_bothirqs;
  732. }
  733. INIT_LIST_HEAD(&maple_waitq);
  734. INIT_LIST_HEAD(&maple_sentq);
  735. /* setup maple ports */
  736. for (i = 0; i < MAPLE_PORTS; i++) {
  737. checked[i] = false;
  738. empty[i] = false;
  739. mdev[i] = maple_alloc_dev(i, 0);
  740. if (!mdev[i]) {
  741. while (i-- > 0)
  742. maple_free_dev(mdev[i]);
  743. retval = -ENOMEM;
  744. goto cleanup_cache;
  745. }
  746. baseunits[i] = mdev[i];
  747. atomic_set(&mdev[i]->busy, 1);
  748. maple_add_packet(mdev[i], 0, MAPLE_COMMAND_DEVINFO, 0, NULL);
  749. subdevice_map[i] = 0;
  750. }
  751. maple_pnp_time = jiffies + HZ;
  752. /* prepare initial queue */
  753. maple_send();
  754. dev_info(&maple_bus, "bus core now registered\n");
  755. return 0;
  756. cleanup_cache:
  757. kmem_cache_destroy(maple_queue_cache);
  758. cleanup_bothirqs:
  759. free_irq(HW_EVENT_VSYNC, 0);
  760. cleanup_irq:
  761. free_irq(HW_EVENT_MAPLE_DMA, 0);
  762. cleanup_dma:
  763. free_pages((unsigned long) maple_sendbuf, MAPLE_DMA_PAGES);
  764. cleanup_basic:
  765. driver_unregister(&maple_unsupported_device.drv);
  766. cleanup_bus:
  767. bus_unregister(&maple_bus_type);
  768. cleanup_device:
  769. device_unregister(&maple_bus);
  770. cleanup:
  771. printk(KERN_ERR "Maple bus registration failed\n");
  772. return retval;
  773. }
  774. /* Push init to later to ensure hardware gets detected */
  775. fs_initcall(maple_bus_init);