tifm_7xx1.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. * tifm_7xx1.c - TI FlashMedia driver
  3. *
  4. * Copyright (C) 2006 Alex Dubov <oakad@yahoo.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. */
  11. #include <linux/tifm.h>
  12. #include <linux/dma-mapping.h>
  13. #define DRIVER_NAME "tifm_7xx1"
  14. #define DRIVER_VERSION "0.7"
  15. static void tifm_7xx1_eject(struct tifm_adapter *fm, struct tifm_dev *sock)
  16. {
  17. unsigned long flags;
  18. spin_lock_irqsave(&fm->lock, flags);
  19. if (!fm->inhibit_new_cards) {
  20. fm->remove_mask |= 1 << sock->socket_id;
  21. queue_work(fm->wq, &fm->media_remover);
  22. }
  23. spin_unlock_irqrestore(&fm->lock, flags);
  24. }
  25. static void tifm_7xx1_remove_media(struct work_struct *work)
  26. {
  27. struct tifm_adapter *fm =
  28. container_of(work, struct tifm_adapter, media_remover);
  29. unsigned long flags;
  30. int cnt;
  31. struct tifm_dev *sock;
  32. if (!class_device_get(&fm->cdev))
  33. return;
  34. spin_lock_irqsave(&fm->lock, flags);
  35. for (cnt = 0; cnt < fm->max_sockets; cnt++) {
  36. if (fm->sockets[cnt] && (fm->remove_mask & (1 << cnt))) {
  37. printk(KERN_INFO DRIVER_NAME
  38. ": demand removing card from socket %d\n", cnt);
  39. sock = fm->sockets[cnt];
  40. fm->sockets[cnt] = NULL;
  41. fm->remove_mask &= ~(1 << cnt);
  42. writel(0x0e00, sock->addr + SOCK_CONTROL);
  43. writel((TIFM_IRQ_FIFOMASK | TIFM_IRQ_CARDMASK) << cnt,
  44. fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
  45. writel((TIFM_IRQ_FIFOMASK | TIFM_IRQ_CARDMASK) << cnt,
  46. fm->addr + FM_SET_INTERRUPT_ENABLE);
  47. spin_unlock_irqrestore(&fm->lock, flags);
  48. device_unregister(&sock->dev);
  49. spin_lock_irqsave(&fm->lock, flags);
  50. }
  51. }
  52. spin_unlock_irqrestore(&fm->lock, flags);
  53. class_device_put(&fm->cdev);
  54. }
  55. static irqreturn_t tifm_7xx1_isr(int irq, void *dev_id)
  56. {
  57. struct tifm_adapter *fm = dev_id;
  58. struct tifm_dev *sock;
  59. unsigned int irq_status;
  60. unsigned int sock_irq_status, cnt;
  61. spin_lock(&fm->lock);
  62. irq_status = readl(fm->addr + FM_INTERRUPT_STATUS);
  63. if (irq_status == 0 || irq_status == (~0)) {
  64. spin_unlock(&fm->lock);
  65. return IRQ_NONE;
  66. }
  67. if (irq_status & TIFM_IRQ_ENABLE) {
  68. writel(TIFM_IRQ_ENABLE, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
  69. for (cnt = 0; cnt < fm->max_sockets; cnt++) {
  70. sock = fm->sockets[cnt];
  71. sock_irq_status = (irq_status >> cnt) &
  72. (TIFM_IRQ_FIFOMASK | TIFM_IRQ_CARDMASK);
  73. if (sock) {
  74. if (sock_irq_status)
  75. sock->signal_irq(sock, sock_irq_status);
  76. if (irq_status & (1 << cnt))
  77. fm->remove_mask |= 1 << cnt;
  78. } else {
  79. if (irq_status & (1 << cnt))
  80. fm->insert_mask |= 1 << cnt;
  81. }
  82. }
  83. }
  84. writel(irq_status, fm->addr + FM_INTERRUPT_STATUS);
  85. if (!fm->inhibit_new_cards) {
  86. if (!fm->remove_mask && !fm->insert_mask) {
  87. writel(TIFM_IRQ_ENABLE,
  88. fm->addr + FM_SET_INTERRUPT_ENABLE);
  89. } else {
  90. queue_work(fm->wq, &fm->media_remover);
  91. queue_work(fm->wq, &fm->media_inserter);
  92. }
  93. }
  94. spin_unlock(&fm->lock);
  95. return IRQ_HANDLED;
  96. }
  97. static tifm_media_id tifm_7xx1_toggle_sock_power(char __iomem *sock_addr, int is_x2)
  98. {
  99. unsigned int s_state;
  100. int cnt;
  101. writel(0x0e00, sock_addr + SOCK_CONTROL);
  102. for (cnt = 0; cnt < 100; cnt++) {
  103. if (!(TIFM_SOCK_STATE_POWERED &
  104. readl(sock_addr + SOCK_PRESENT_STATE)))
  105. break;
  106. msleep(10);
  107. }
  108. s_state = readl(sock_addr + SOCK_PRESENT_STATE);
  109. if (!(TIFM_SOCK_STATE_OCCUPIED & s_state))
  110. return FM_NULL;
  111. if (is_x2) {
  112. writel((s_state & 7) | 0x0c00, sock_addr + SOCK_CONTROL);
  113. } else {
  114. // SmartMedia cards need extra 40 msec
  115. if (((readl(sock_addr + SOCK_PRESENT_STATE) >> 4) & 7) == 1)
  116. msleep(40);
  117. writel(readl(sock_addr + SOCK_CONTROL) | TIFM_CTRL_LED,
  118. sock_addr + SOCK_CONTROL);
  119. msleep(10);
  120. writel((s_state & 0x7) | 0x0c00 | TIFM_CTRL_LED,
  121. sock_addr + SOCK_CONTROL);
  122. }
  123. for (cnt = 0; cnt < 100; cnt++) {
  124. if ((TIFM_SOCK_STATE_POWERED &
  125. readl(sock_addr + SOCK_PRESENT_STATE)))
  126. break;
  127. msleep(10);
  128. }
  129. if (!is_x2)
  130. writel(readl(sock_addr + SOCK_CONTROL) & (~TIFM_CTRL_LED),
  131. sock_addr + SOCK_CONTROL);
  132. return (readl(sock_addr + SOCK_PRESENT_STATE) >> 4) & 7;
  133. }
  134. inline static char __iomem *
  135. tifm_7xx1_sock_addr(char __iomem *base_addr, unsigned int sock_num)
  136. {
  137. return base_addr + ((sock_num + 1) << 10);
  138. }
  139. static void tifm_7xx1_insert_media(struct work_struct *work)
  140. {
  141. struct tifm_adapter *fm =
  142. container_of(work, struct tifm_adapter, media_inserter);
  143. unsigned long flags;
  144. tifm_media_id media_id;
  145. char *card_name = "xx";
  146. int cnt, ok_to_register;
  147. unsigned int insert_mask;
  148. struct tifm_dev *new_sock = NULL;
  149. if (!class_device_get(&fm->cdev))
  150. return;
  151. spin_lock_irqsave(&fm->lock, flags);
  152. insert_mask = fm->insert_mask;
  153. fm->insert_mask = 0;
  154. if (fm->inhibit_new_cards) {
  155. spin_unlock_irqrestore(&fm->lock, flags);
  156. class_device_put(&fm->cdev);
  157. return;
  158. }
  159. spin_unlock_irqrestore(&fm->lock, flags);
  160. for (cnt = 0; cnt < fm->max_sockets; cnt++) {
  161. if (!(insert_mask & (1 << cnt)))
  162. continue;
  163. media_id = tifm_7xx1_toggle_sock_power(tifm_7xx1_sock_addr(fm->addr, cnt),
  164. fm->max_sockets == 2);
  165. if (media_id) {
  166. ok_to_register = 0;
  167. new_sock = tifm_alloc_device(fm);
  168. if (new_sock) {
  169. new_sock->addr = tifm_7xx1_sock_addr(fm->addr,
  170. cnt);
  171. new_sock->media_id = media_id;
  172. new_sock->socket_id = cnt;
  173. switch (media_id) {
  174. case 1:
  175. card_name = "xd";
  176. break;
  177. case 2:
  178. card_name = "ms";
  179. break;
  180. case 3:
  181. card_name = "sd";
  182. break;
  183. default:
  184. break;
  185. }
  186. snprintf(new_sock->dev.bus_id, BUS_ID_SIZE,
  187. "tifm_%s%u:%u", card_name, fm->id, cnt);
  188. printk(KERN_INFO DRIVER_NAME
  189. ": %s card detected in socket %d\n",
  190. card_name, cnt);
  191. spin_lock_irqsave(&fm->lock, flags);
  192. if (!fm->sockets[cnt]) {
  193. fm->sockets[cnt] = new_sock;
  194. ok_to_register = 1;
  195. }
  196. spin_unlock_irqrestore(&fm->lock, flags);
  197. if (!ok_to_register ||
  198. device_register(&new_sock->dev)) {
  199. spin_lock_irqsave(&fm->lock, flags);
  200. fm->sockets[cnt] = NULL;
  201. spin_unlock_irqrestore(&fm->lock,
  202. flags);
  203. tifm_free_device(&new_sock->dev);
  204. }
  205. }
  206. }
  207. writel((TIFM_IRQ_FIFOMASK | TIFM_IRQ_CARDMASK) << cnt,
  208. fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
  209. writel((TIFM_IRQ_FIFOMASK | TIFM_IRQ_CARDMASK) << cnt,
  210. fm->addr + FM_SET_INTERRUPT_ENABLE);
  211. }
  212. writel(TIFM_IRQ_ENABLE, fm->addr + FM_SET_INTERRUPT_ENABLE);
  213. class_device_put(&fm->cdev);
  214. }
  215. static int tifm_7xx1_suspend(struct pci_dev *dev, pm_message_t state)
  216. {
  217. struct tifm_adapter *fm = pci_get_drvdata(dev);
  218. unsigned long flags;
  219. spin_lock_irqsave(&fm->lock, flags);
  220. fm->inhibit_new_cards = 1;
  221. fm->remove_mask = 0xf;
  222. fm->insert_mask = 0;
  223. writel(TIFM_IRQ_ENABLE, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
  224. spin_unlock_irqrestore(&fm->lock, flags);
  225. flush_workqueue(fm->wq);
  226. tifm_7xx1_remove_media(&fm->media_remover);
  227. pci_set_power_state(dev, PCI_D3hot);
  228. pci_disable_device(dev);
  229. pci_save_state(dev);
  230. return 0;
  231. }
  232. static int tifm_7xx1_resume(struct pci_dev *dev)
  233. {
  234. struct tifm_adapter *fm = pci_get_drvdata(dev);
  235. unsigned long flags;
  236. pci_restore_state(dev);
  237. pci_enable_device(dev);
  238. pci_set_power_state(dev, PCI_D0);
  239. pci_set_master(dev);
  240. spin_lock_irqsave(&fm->lock, flags);
  241. fm->inhibit_new_cards = 0;
  242. writel(TIFM_IRQ_SETALL, fm->addr + FM_INTERRUPT_STATUS);
  243. writel(TIFM_IRQ_SETALL, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
  244. writel(TIFM_IRQ_ENABLE | TIFM_IRQ_SETALLSOCK,
  245. fm->addr + FM_SET_INTERRUPT_ENABLE);
  246. fm->insert_mask = 0xf;
  247. spin_unlock_irqrestore(&fm->lock, flags);
  248. return 0;
  249. }
  250. static int tifm_7xx1_probe(struct pci_dev *dev,
  251. const struct pci_device_id *dev_id)
  252. {
  253. struct tifm_adapter *fm;
  254. int pci_dev_busy = 0;
  255. int rc;
  256. rc = pci_set_dma_mask(dev, DMA_32BIT_MASK);
  257. if (rc)
  258. return rc;
  259. rc = pci_enable_device(dev);
  260. if (rc)
  261. return rc;
  262. pci_set_master(dev);
  263. rc = pci_request_regions(dev, DRIVER_NAME);
  264. if (rc) {
  265. pci_dev_busy = 1;
  266. goto err_out;
  267. }
  268. pci_intx(dev, 1);
  269. fm = tifm_alloc_adapter();
  270. if (!fm) {
  271. rc = -ENOMEM;
  272. goto err_out_int;
  273. }
  274. fm->dev = &dev->dev;
  275. fm->max_sockets = (dev->device == 0x803B) ? 2 : 4;
  276. fm->sockets = kzalloc(sizeof(struct tifm_dev*) * fm->max_sockets,
  277. GFP_KERNEL);
  278. if (!fm->sockets)
  279. goto err_out_free;
  280. INIT_WORK(&fm->media_inserter, tifm_7xx1_insert_media);
  281. INIT_WORK(&fm->media_remover, tifm_7xx1_remove_media);
  282. fm->eject = tifm_7xx1_eject;
  283. pci_set_drvdata(dev, fm);
  284. fm->addr = ioremap(pci_resource_start(dev, 0),
  285. pci_resource_len(dev, 0));
  286. if (!fm->addr)
  287. goto err_out_free;
  288. rc = request_irq(dev->irq, tifm_7xx1_isr, SA_SHIRQ, DRIVER_NAME, fm);
  289. if (rc)
  290. goto err_out_unmap;
  291. rc = tifm_add_adapter(fm);
  292. if (rc)
  293. goto err_out_irq;
  294. writel(TIFM_IRQ_SETALL, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
  295. writel(TIFM_IRQ_ENABLE | TIFM_IRQ_SETALLSOCK,
  296. fm->addr + FM_SET_INTERRUPT_ENABLE);
  297. fm->insert_mask = 0xf;
  298. return 0;
  299. err_out_irq:
  300. free_irq(dev->irq, fm);
  301. err_out_unmap:
  302. iounmap(fm->addr);
  303. err_out_free:
  304. pci_set_drvdata(dev, NULL);
  305. tifm_free_adapter(fm);
  306. err_out_int:
  307. pci_intx(dev, 0);
  308. pci_release_regions(dev);
  309. err_out:
  310. if (!pci_dev_busy)
  311. pci_disable_device(dev);
  312. return rc;
  313. }
  314. static void tifm_7xx1_remove(struct pci_dev *dev)
  315. {
  316. struct tifm_adapter *fm = pci_get_drvdata(dev);
  317. unsigned long flags;
  318. spin_lock_irqsave(&fm->lock, flags);
  319. fm->inhibit_new_cards = 1;
  320. fm->remove_mask = 0xf;
  321. fm->insert_mask = 0;
  322. writel(TIFM_IRQ_ENABLE, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
  323. spin_unlock_irqrestore(&fm->lock, flags);
  324. flush_workqueue(fm->wq);
  325. tifm_7xx1_remove_media(&fm->media_remover);
  326. writel(TIFM_IRQ_SETALL, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
  327. free_irq(dev->irq, fm);
  328. tifm_remove_adapter(fm);
  329. pci_set_drvdata(dev, NULL);
  330. iounmap(fm->addr);
  331. pci_intx(dev, 0);
  332. pci_release_regions(dev);
  333. pci_disable_device(dev);
  334. tifm_free_adapter(fm);
  335. }
  336. static struct pci_device_id tifm_7xx1_pci_tbl [] = {
  337. { PCI_VENDOR_ID_TI, 0x8033, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  338. 0 }, /* xx21 - the one I have */
  339. { PCI_VENDOR_ID_TI, 0x803B, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  340. 0 }, /* xx12 - should be also supported */
  341. { }
  342. };
  343. static struct pci_driver tifm_7xx1_driver = {
  344. .name = DRIVER_NAME,
  345. .id_table = tifm_7xx1_pci_tbl,
  346. .probe = tifm_7xx1_probe,
  347. .remove = tifm_7xx1_remove,
  348. .suspend = tifm_7xx1_suspend,
  349. .resume = tifm_7xx1_resume,
  350. };
  351. static int __init tifm_7xx1_init(void)
  352. {
  353. return pci_register_driver(&tifm_7xx1_driver);
  354. }
  355. static void __exit tifm_7xx1_exit(void)
  356. {
  357. pci_unregister_driver(&tifm_7xx1_driver);
  358. }
  359. MODULE_AUTHOR("Alex Dubov");
  360. MODULE_DESCRIPTION("TI FlashMedia host driver");
  361. MODULE_LICENSE("GPL");
  362. MODULE_DEVICE_TABLE(pci, tifm_7xx1_pci_tbl);
  363. MODULE_VERSION(DRIVER_VERSION);
  364. module_init(tifm_7xx1_init);
  365. module_exit(tifm_7xx1_exit);