tifm_7xx1.c 10 KB

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