tifm_7xx1.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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.8"
  15. #define TIFM_IRQ_ENABLE 0x80000000
  16. #define TIFM_IRQ_SOCKMASK(x) (x)
  17. #define TIFM_IRQ_CARDMASK(x) ((x) << 8)
  18. #define TIFM_IRQ_FIFOMASK(x) ((x) << 16)
  19. #define TIFM_IRQ_SETALL 0xffffffff
  20. static void tifm_7xx1_dummy_eject(struct tifm_adapter *fm,
  21. struct tifm_dev *sock)
  22. {
  23. }
  24. static void tifm_7xx1_eject(struct tifm_adapter *fm, struct tifm_dev *sock)
  25. {
  26. unsigned long flags;
  27. spin_lock_irqsave(&fm->lock, flags);
  28. fm->socket_change_set |= 1 << sock->socket_id;
  29. tifm_queue_work(&fm->media_switcher);
  30. spin_unlock_irqrestore(&fm->lock, flags);
  31. }
  32. static irqreturn_t tifm_7xx1_isr(int irq, void *dev_id)
  33. {
  34. struct tifm_adapter *fm = dev_id;
  35. struct tifm_dev *sock;
  36. unsigned int irq_status, cnt;
  37. spin_lock(&fm->lock);
  38. irq_status = readl(fm->addr + FM_INTERRUPT_STATUS);
  39. if (irq_status == 0 || irq_status == (~0)) {
  40. spin_unlock(&fm->lock);
  41. return IRQ_NONE;
  42. }
  43. if (irq_status & TIFM_IRQ_ENABLE) {
  44. writel(TIFM_IRQ_ENABLE, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
  45. for (cnt = 0; cnt < fm->num_sockets; cnt++) {
  46. sock = fm->sockets[cnt];
  47. if (sock) {
  48. if ((irq_status >> cnt) & TIFM_IRQ_FIFOMASK(1))
  49. sock->data_event(sock);
  50. if ((irq_status >> cnt) & TIFM_IRQ_CARDMASK(1))
  51. sock->card_event(sock);
  52. }
  53. }
  54. fm->socket_change_set |= irq_status
  55. & ((1 << fm->num_sockets) - 1);
  56. }
  57. writel(irq_status, fm->addr + FM_INTERRUPT_STATUS);
  58. if (fm->finish_me)
  59. complete_all(fm->finish_me);
  60. else if (!fm->socket_change_set)
  61. writel(TIFM_IRQ_ENABLE, fm->addr + FM_SET_INTERRUPT_ENABLE);
  62. else
  63. tifm_queue_work(&fm->media_switcher);
  64. spin_unlock(&fm->lock);
  65. return IRQ_HANDLED;
  66. }
  67. static unsigned char tifm_7xx1_toggle_sock_power(char __iomem *sock_addr)
  68. {
  69. unsigned int s_state;
  70. int cnt;
  71. writel(0x0e00, sock_addr + SOCK_CONTROL);
  72. for (cnt = 16; cnt <= 256; cnt <<= 1) {
  73. if (!(TIFM_SOCK_STATE_POWERED
  74. & readl(sock_addr + SOCK_PRESENT_STATE)))
  75. break;
  76. msleep(cnt);
  77. }
  78. s_state = readl(sock_addr + SOCK_PRESENT_STATE);
  79. if (!(TIFM_SOCK_STATE_OCCUPIED & s_state))
  80. return 0;
  81. writel(readl(sock_addr + SOCK_CONTROL) | TIFM_CTRL_LED,
  82. sock_addr + SOCK_CONTROL);
  83. /* xd needs some extra time before power on */
  84. if (((readl(sock_addr + SOCK_PRESENT_STATE) >> 4) & 7)
  85. == TIFM_TYPE_XD)
  86. msleep(40);
  87. writel((s_state & TIFM_CTRL_POWER_MASK) | 0x0c00,
  88. sock_addr + SOCK_CONTROL);
  89. /* wait for power to stabilize */
  90. msleep(20);
  91. for (cnt = 16; cnt <= 256; cnt <<= 1) {
  92. if ((TIFM_SOCK_STATE_POWERED
  93. & readl(sock_addr + SOCK_PRESENT_STATE)))
  94. break;
  95. msleep(cnt);
  96. }
  97. writel(readl(sock_addr + SOCK_CONTROL) & (~TIFM_CTRL_LED),
  98. sock_addr + SOCK_CONTROL);
  99. return (readl(sock_addr + SOCK_PRESENT_STATE) >> 4) & 7;
  100. }
  101. inline static void tifm_7xx1_sock_power_off(char __iomem *sock_addr)
  102. {
  103. writel((~TIFM_CTRL_POWER_MASK) & readl(sock_addr + SOCK_CONTROL),
  104. sock_addr + SOCK_CONTROL);
  105. }
  106. inline static char __iomem *
  107. tifm_7xx1_sock_addr(char __iomem *base_addr, unsigned int sock_num)
  108. {
  109. return base_addr + ((sock_num + 1) << 10);
  110. }
  111. static void tifm_7xx1_switch_media(struct work_struct *work)
  112. {
  113. struct tifm_adapter *fm = container_of(work, struct tifm_adapter,
  114. media_switcher);
  115. struct tifm_dev *sock;
  116. char __iomem *sock_addr;
  117. unsigned long flags;
  118. unsigned char media_id;
  119. unsigned int socket_change_set, cnt;
  120. spin_lock_irqsave(&fm->lock, flags);
  121. socket_change_set = fm->socket_change_set;
  122. fm->socket_change_set = 0;
  123. dev_dbg(fm->dev.parent, "checking media set %x\n",
  124. socket_change_set);
  125. if (!socket_change_set) {
  126. spin_unlock_irqrestore(&fm->lock, flags);
  127. return;
  128. }
  129. for (cnt = 0; cnt < fm->num_sockets; cnt++) {
  130. if (!(socket_change_set & (1 << cnt)))
  131. continue;
  132. sock = fm->sockets[cnt];
  133. if (sock) {
  134. printk(KERN_INFO
  135. "%s : demand removing card from socket %u:%u\n",
  136. dev_name(&fm->dev), fm->id, cnt);
  137. fm->sockets[cnt] = NULL;
  138. sock_addr = sock->addr;
  139. spin_unlock_irqrestore(&fm->lock, flags);
  140. device_unregister(&sock->dev);
  141. spin_lock_irqsave(&fm->lock, flags);
  142. tifm_7xx1_sock_power_off(sock_addr);
  143. writel(0x0e00, sock_addr + SOCK_CONTROL);
  144. }
  145. spin_unlock_irqrestore(&fm->lock, flags);
  146. media_id = tifm_7xx1_toggle_sock_power(
  147. tifm_7xx1_sock_addr(fm->addr, cnt));
  148. // tifm_alloc_device will check if media_id is valid
  149. sock = tifm_alloc_device(fm, cnt, media_id);
  150. if (sock) {
  151. sock->addr = tifm_7xx1_sock_addr(fm->addr, cnt);
  152. if (!device_register(&sock->dev)) {
  153. spin_lock_irqsave(&fm->lock, flags);
  154. if (!fm->sockets[cnt]) {
  155. fm->sockets[cnt] = sock;
  156. sock = NULL;
  157. }
  158. spin_unlock_irqrestore(&fm->lock, flags);
  159. }
  160. if (sock)
  161. tifm_free_device(&sock->dev);
  162. }
  163. spin_lock_irqsave(&fm->lock, flags);
  164. }
  165. writel(TIFM_IRQ_FIFOMASK(socket_change_set)
  166. | TIFM_IRQ_CARDMASK(socket_change_set),
  167. fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
  168. writel(TIFM_IRQ_FIFOMASK(socket_change_set)
  169. | TIFM_IRQ_CARDMASK(socket_change_set),
  170. fm->addr + FM_SET_INTERRUPT_ENABLE);
  171. writel(TIFM_IRQ_ENABLE, fm->addr + FM_SET_INTERRUPT_ENABLE);
  172. spin_unlock_irqrestore(&fm->lock, flags);
  173. }
  174. #ifdef CONFIG_PM
  175. static int tifm_7xx1_suspend(struct pci_dev *dev, pm_message_t state)
  176. {
  177. struct tifm_adapter *fm = pci_get_drvdata(dev);
  178. int cnt;
  179. dev_dbg(&dev->dev, "suspending host\n");
  180. for (cnt = 0; cnt < fm->num_sockets; cnt++) {
  181. if (fm->sockets[cnt])
  182. tifm_7xx1_sock_power_off(fm->sockets[cnt]->addr);
  183. }
  184. pci_save_state(dev);
  185. pci_enable_wake(dev, pci_choose_state(dev, state), 0);
  186. pci_disable_device(dev);
  187. pci_set_power_state(dev, pci_choose_state(dev, state));
  188. return 0;
  189. }
  190. static int tifm_7xx1_resume(struct pci_dev *dev)
  191. {
  192. struct tifm_adapter *fm = pci_get_drvdata(dev);
  193. int rc;
  194. unsigned int good_sockets = 0, bad_sockets = 0;
  195. unsigned long flags;
  196. unsigned char new_ids[fm->num_sockets];
  197. DECLARE_COMPLETION_ONSTACK(finish_resume);
  198. pci_set_power_state(dev, PCI_D0);
  199. pci_restore_state(dev);
  200. rc = pci_enable_device(dev);
  201. if (rc)
  202. return rc;
  203. pci_set_master(dev);
  204. dev_dbg(&dev->dev, "resuming host\n");
  205. for (rc = 0; rc < fm->num_sockets; rc++)
  206. new_ids[rc] = tifm_7xx1_toggle_sock_power(
  207. tifm_7xx1_sock_addr(fm->addr, rc));
  208. spin_lock_irqsave(&fm->lock, flags);
  209. for (rc = 0; rc < fm->num_sockets; rc++) {
  210. if (fm->sockets[rc]) {
  211. if (fm->sockets[rc]->type == new_ids[rc])
  212. good_sockets |= 1 << rc;
  213. else
  214. bad_sockets |= 1 << rc;
  215. }
  216. }
  217. writel(TIFM_IRQ_ENABLE | TIFM_IRQ_SOCKMASK((1 << fm->num_sockets) - 1),
  218. fm->addr + FM_SET_INTERRUPT_ENABLE);
  219. dev_dbg(&dev->dev, "change sets on resume: good %x, bad %x\n",
  220. good_sockets, bad_sockets);
  221. fm->socket_change_set = 0;
  222. if (good_sockets) {
  223. fm->finish_me = &finish_resume;
  224. spin_unlock_irqrestore(&fm->lock, flags);
  225. rc = wait_for_completion_timeout(&finish_resume, HZ);
  226. dev_dbg(&dev->dev, "wait returned %d\n", rc);
  227. writel(TIFM_IRQ_FIFOMASK(good_sockets)
  228. | TIFM_IRQ_CARDMASK(good_sockets),
  229. fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
  230. writel(TIFM_IRQ_FIFOMASK(good_sockets)
  231. | TIFM_IRQ_CARDMASK(good_sockets),
  232. fm->addr + FM_SET_INTERRUPT_ENABLE);
  233. spin_lock_irqsave(&fm->lock, flags);
  234. fm->finish_me = NULL;
  235. fm->socket_change_set ^= good_sockets & fm->socket_change_set;
  236. }
  237. fm->socket_change_set |= bad_sockets;
  238. if (fm->socket_change_set)
  239. tifm_queue_work(&fm->media_switcher);
  240. spin_unlock_irqrestore(&fm->lock, flags);
  241. writel(TIFM_IRQ_ENABLE,
  242. fm->addr + FM_SET_INTERRUPT_ENABLE);
  243. return 0;
  244. }
  245. #else
  246. #define tifm_7xx1_suspend NULL
  247. #define tifm_7xx1_resume NULL
  248. #endif /* CONFIG_PM */
  249. static int tifm_7xx1_dummy_has_ms_pif(struct tifm_adapter *fm,
  250. struct tifm_dev *sock)
  251. {
  252. return 0;
  253. }
  254. static int tifm_7xx1_has_ms_pif(struct tifm_adapter *fm, struct tifm_dev *sock)
  255. {
  256. if (((fm->num_sockets == 4) && (sock->socket_id == 2))
  257. || ((fm->num_sockets == 2) && (sock->socket_id == 0)))
  258. return 1;
  259. return 0;
  260. }
  261. static int tifm_7xx1_probe(struct pci_dev *dev,
  262. const struct pci_device_id *dev_id)
  263. {
  264. struct tifm_adapter *fm;
  265. int pci_dev_busy = 0;
  266. int rc;
  267. rc = pci_set_dma_mask(dev, DMA_BIT_MASK(32));
  268. if (rc)
  269. return rc;
  270. rc = pci_enable_device(dev);
  271. if (rc)
  272. return rc;
  273. pci_set_master(dev);
  274. rc = pci_request_regions(dev, DRIVER_NAME);
  275. if (rc) {
  276. pci_dev_busy = 1;
  277. goto err_out;
  278. }
  279. pci_intx(dev, 1);
  280. fm = tifm_alloc_adapter(dev->device == PCI_DEVICE_ID_TI_XX21_XX11_FM
  281. ? 4 : 2, &dev->dev);
  282. if (!fm) {
  283. rc = -ENOMEM;
  284. goto err_out_int;
  285. }
  286. INIT_WORK(&fm->media_switcher, tifm_7xx1_switch_media);
  287. fm->eject = tifm_7xx1_eject;
  288. fm->has_ms_pif = tifm_7xx1_has_ms_pif;
  289. pci_set_drvdata(dev, fm);
  290. fm->addr = pci_ioremap_bar(dev, 0);
  291. if (!fm->addr)
  292. goto err_out_free;
  293. rc = request_irq(dev->irq, tifm_7xx1_isr, IRQF_SHARED, DRIVER_NAME, fm);
  294. if (rc)
  295. goto err_out_unmap;
  296. rc = tifm_add_adapter(fm);
  297. if (rc)
  298. goto err_out_irq;
  299. writel(TIFM_IRQ_ENABLE | TIFM_IRQ_SOCKMASK((1 << fm->num_sockets) - 1),
  300. fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
  301. writel(TIFM_IRQ_ENABLE | TIFM_IRQ_SOCKMASK((1 << fm->num_sockets) - 1),
  302. fm->addr + FM_SET_INTERRUPT_ENABLE);
  303. return 0;
  304. err_out_irq:
  305. free_irq(dev->irq, fm);
  306. err_out_unmap:
  307. iounmap(fm->addr);
  308. err_out_free:
  309. pci_set_drvdata(dev, NULL);
  310. tifm_free_adapter(fm);
  311. err_out_int:
  312. pci_intx(dev, 0);
  313. pci_release_regions(dev);
  314. err_out:
  315. if (!pci_dev_busy)
  316. pci_disable_device(dev);
  317. return rc;
  318. }
  319. static void tifm_7xx1_remove(struct pci_dev *dev)
  320. {
  321. struct tifm_adapter *fm = pci_get_drvdata(dev);
  322. int cnt;
  323. fm->eject = tifm_7xx1_dummy_eject;
  324. fm->has_ms_pif = tifm_7xx1_dummy_has_ms_pif;
  325. writel(TIFM_IRQ_SETALL, fm->addr + FM_CLEAR_INTERRUPT_ENABLE);
  326. mmiowb();
  327. free_irq(dev->irq, fm);
  328. tifm_remove_adapter(fm);
  329. for (cnt = 0; cnt < fm->num_sockets; cnt++)
  330. tifm_7xx1_sock_power_off(tifm_7xx1_sock_addr(fm->addr, cnt));
  331. pci_set_drvdata(dev, NULL);
  332. iounmap(fm->addr);
  333. pci_intx(dev, 0);
  334. pci_release_regions(dev);
  335. pci_disable_device(dev);
  336. tifm_free_adapter(fm);
  337. }
  338. static struct pci_device_id tifm_7xx1_pci_tbl [] = {
  339. { PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_XX21_XX11_FM, PCI_ANY_ID,
  340. PCI_ANY_ID, 0, 0, 0 }, /* xx21 - the one I have */
  341. { PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_XX12_FM, PCI_ANY_ID,
  342. PCI_ANY_ID, 0, 0, 0 },
  343. { PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_XX20_FM, PCI_ANY_ID,
  344. PCI_ANY_ID, 0, 0, 0 },
  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);