tifm_7xx1.c 10 KB

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