tifm_7xx1.c 11 KB

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