tifm_7xx1.c 11 KB

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