bcm63xx_pcmcia.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2008 Maxime Bizon <mbizon@freebox.fr>
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/ioport.h>
  11. #include <linux/timer.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/delay.h>
  14. #include <linux/pci.h>
  15. #include <linux/gpio.h>
  16. #include <bcm63xx_regs.h>
  17. #include <bcm63xx_io.h>
  18. #include "bcm63xx_pcmcia.h"
  19. #define PFX "bcm63xx_pcmcia: "
  20. #ifdef CONFIG_CARDBUS
  21. /* if cardbus is used, platform device needs reference to actual pci
  22. * device */
  23. static struct pci_dev *bcm63xx_cb_dev;
  24. #endif
  25. /*
  26. * read/write helper for pcmcia regs
  27. */
  28. static inline u32 pcmcia_readl(struct bcm63xx_pcmcia_socket *skt, u32 off)
  29. {
  30. return bcm_readl(skt->base + off);
  31. }
  32. static inline void pcmcia_writel(struct bcm63xx_pcmcia_socket *skt,
  33. u32 val, u32 off)
  34. {
  35. bcm_writel(val, skt->base + off);
  36. }
  37. /*
  38. * This callback should (re-)initialise the socket, turn on status
  39. * interrupts and PCMCIA bus, and wait for power to stabilise so that
  40. * the card status signals report correctly.
  41. *
  42. * Hardware cannot do that.
  43. */
  44. static int bcm63xx_pcmcia_sock_init(struct pcmcia_socket *sock)
  45. {
  46. return 0;
  47. }
  48. /*
  49. * This callback should remove power on the socket, disable IRQs from
  50. * the card, turn off status interrupts, and disable the PCMCIA bus.
  51. *
  52. * Hardware cannot do that.
  53. */
  54. static int bcm63xx_pcmcia_suspend(struct pcmcia_socket *sock)
  55. {
  56. return 0;
  57. }
  58. /*
  59. * Implements the set_socket() operation for the in-kernel PCMCIA
  60. * service (formerly SS_SetSocket in Card Services). We more or
  61. * less punt all of this work and let the kernel handle the details
  62. * of power configuration, reset, &c. We also record the value of
  63. * `state' in order to regurgitate it to the PCMCIA core later.
  64. */
  65. static int bcm63xx_pcmcia_set_socket(struct pcmcia_socket *sock,
  66. socket_state_t *state)
  67. {
  68. struct bcm63xx_pcmcia_socket *skt;
  69. unsigned long flags;
  70. u32 val;
  71. skt = sock->driver_data;
  72. spin_lock_irqsave(&skt->lock, flags);
  73. /* note: hardware cannot control socket power, so we will
  74. * always report SS_POWERON */
  75. /* apply socket reset */
  76. val = pcmcia_readl(skt, PCMCIA_C1_REG);
  77. if (state->flags & SS_RESET)
  78. val |= PCMCIA_C1_RESET_MASK;
  79. else
  80. val &= ~PCMCIA_C1_RESET_MASK;
  81. /* reverse reset logic for cardbus card */
  82. if (skt->card_detected && (skt->card_type & CARD_CARDBUS))
  83. val ^= PCMCIA_C1_RESET_MASK;
  84. pcmcia_writel(skt, val, PCMCIA_C1_REG);
  85. /* keep requested state for event reporting */
  86. skt->requested_state = *state;
  87. spin_unlock_irqrestore(&skt->lock, flags);
  88. return 0;
  89. }
  90. /*
  91. * identity cardtype from VS[12] input, CD[12] input while only VS2 is
  92. * floating, and CD[12] input while only VS1 is floating
  93. */
  94. enum {
  95. IN_VS1 = (1 << 0),
  96. IN_VS2 = (1 << 1),
  97. IN_CD1_VS2H = (1 << 2),
  98. IN_CD2_VS2H = (1 << 3),
  99. IN_CD1_VS1H = (1 << 4),
  100. IN_CD2_VS1H = (1 << 5),
  101. };
  102. static const u8 vscd_to_cardtype[] = {
  103. /* VS1 float, VS2 float */
  104. [IN_VS1 | IN_VS2] = (CARD_PCCARD | CARD_5V),
  105. /* VS1 grounded, VS2 float */
  106. [IN_VS2] = (CARD_PCCARD | CARD_5V | CARD_3V),
  107. /* VS1 grounded, VS2 grounded */
  108. [0] = (CARD_PCCARD | CARD_5V | CARD_3V | CARD_XV),
  109. /* VS1 tied to CD1, VS2 float */
  110. [IN_VS1 | IN_VS2 | IN_CD1_VS1H] = (CARD_CARDBUS | CARD_3V),
  111. /* VS1 grounded, VS2 tied to CD2 */
  112. [IN_VS2 | IN_CD2_VS2H] = (CARD_CARDBUS | CARD_3V | CARD_XV),
  113. /* VS1 tied to CD2, VS2 grounded */
  114. [IN_VS1 | IN_CD2_VS1H] = (CARD_CARDBUS | CARD_3V | CARD_XV | CARD_YV),
  115. /* VS1 float, VS2 grounded */
  116. [IN_VS1] = (CARD_PCCARD | CARD_XV),
  117. /* VS1 float, VS2 tied to CD2 */
  118. [IN_VS1 | IN_VS2 | IN_CD2_VS2H] = (CARD_CARDBUS | CARD_3V),
  119. /* VS1 float, VS2 tied to CD1 */
  120. [IN_VS1 | IN_VS2 | IN_CD1_VS2H] = (CARD_CARDBUS | CARD_XV | CARD_YV),
  121. /* VS1 tied to CD2, VS2 float */
  122. [IN_VS1 | IN_VS2 | IN_CD2_VS1H] = (CARD_CARDBUS | CARD_YV),
  123. /* VS2 grounded, VS1 is tied to CD1, CD2 is grounded */
  124. [IN_VS1 | IN_CD1_VS1H] = 0, /* ignore cardbay */
  125. };
  126. /*
  127. * poll hardware to check card insertion status
  128. */
  129. static unsigned int __get_socket_status(struct bcm63xx_pcmcia_socket *skt)
  130. {
  131. unsigned int stat;
  132. u32 val;
  133. stat = 0;
  134. /* check CD for card presence */
  135. val = pcmcia_readl(skt, PCMCIA_C1_REG);
  136. if (!(val & PCMCIA_C1_CD1_MASK) && !(val & PCMCIA_C1_CD2_MASK))
  137. stat |= SS_DETECT;
  138. /* if new insertion, detect cardtype */
  139. if ((stat & SS_DETECT) && !skt->card_detected) {
  140. unsigned int stat = 0;
  141. /* float VS1, float VS2 */
  142. val |= PCMCIA_C1_VS1OE_MASK;
  143. val |= PCMCIA_C1_VS2OE_MASK;
  144. pcmcia_writel(skt, val, PCMCIA_C1_REG);
  145. /* wait for output to stabilize and read VS[12] */
  146. udelay(10);
  147. val = pcmcia_readl(skt, PCMCIA_C1_REG);
  148. stat |= (val & PCMCIA_C1_VS1_MASK) ? IN_VS1 : 0;
  149. stat |= (val & PCMCIA_C1_VS2_MASK) ? IN_VS2 : 0;
  150. /* drive VS1 low, float VS2 */
  151. val &= ~PCMCIA_C1_VS1OE_MASK;
  152. val |= PCMCIA_C1_VS2OE_MASK;
  153. pcmcia_writel(skt, val, PCMCIA_C1_REG);
  154. /* wait for output to stabilize and read CD[12] */
  155. udelay(10);
  156. val = pcmcia_readl(skt, PCMCIA_C1_REG);
  157. stat |= (val & PCMCIA_C1_CD1_MASK) ? IN_CD1_VS2H : 0;
  158. stat |= (val & PCMCIA_C1_CD2_MASK) ? IN_CD2_VS2H : 0;
  159. /* float VS1, drive VS2 low */
  160. val |= PCMCIA_C1_VS1OE_MASK;
  161. val &= ~PCMCIA_C1_VS2OE_MASK;
  162. pcmcia_writel(skt, val, PCMCIA_C1_REG);
  163. /* wait for output to stabilize and read CD[12] */
  164. udelay(10);
  165. val = pcmcia_readl(skt, PCMCIA_C1_REG);
  166. stat |= (val & PCMCIA_C1_CD1_MASK) ? IN_CD1_VS1H : 0;
  167. stat |= (val & PCMCIA_C1_CD2_MASK) ? IN_CD2_VS1H : 0;
  168. /* guess cardtype from all this */
  169. skt->card_type = vscd_to_cardtype[stat];
  170. if (!skt->card_type)
  171. dev_err(&skt->socket.dev, "unsupported card type\n");
  172. /* drive both VS pin to 0 again */
  173. val &= ~(PCMCIA_C1_VS1OE_MASK | PCMCIA_C1_VS2OE_MASK);
  174. /* enable correct logic */
  175. val &= ~(PCMCIA_C1_EN_PCMCIA_MASK | PCMCIA_C1_EN_CARDBUS_MASK);
  176. if (skt->card_type & CARD_PCCARD)
  177. val |= PCMCIA_C1_EN_PCMCIA_MASK;
  178. else
  179. val |= PCMCIA_C1_EN_CARDBUS_MASK;
  180. pcmcia_writel(skt, val, PCMCIA_C1_REG);
  181. }
  182. skt->card_detected = (stat & SS_DETECT) ? 1 : 0;
  183. /* report card type/voltage */
  184. if (skt->card_type & CARD_CARDBUS)
  185. stat |= SS_CARDBUS;
  186. if (skt->card_type & CARD_3V)
  187. stat |= SS_3VCARD;
  188. if (skt->card_type & CARD_XV)
  189. stat |= SS_XVCARD;
  190. stat |= SS_POWERON;
  191. if (gpio_get_value(skt->pd->ready_gpio))
  192. stat |= SS_READY;
  193. return stat;
  194. }
  195. /*
  196. * core request to get current socket status
  197. */
  198. static int bcm63xx_pcmcia_get_status(struct pcmcia_socket *sock,
  199. unsigned int *status)
  200. {
  201. struct bcm63xx_pcmcia_socket *skt;
  202. skt = sock->driver_data;
  203. spin_lock_bh(&skt->lock);
  204. *status = __get_socket_status(skt);
  205. spin_unlock_bh(&skt->lock);
  206. return 0;
  207. }
  208. /*
  209. * socket polling timer callback
  210. */
  211. static void bcm63xx_pcmcia_poll(unsigned long data)
  212. {
  213. struct bcm63xx_pcmcia_socket *skt;
  214. unsigned int stat, events;
  215. skt = (struct bcm63xx_pcmcia_socket *)data;
  216. spin_lock_bh(&skt->lock);
  217. stat = __get_socket_status(skt);
  218. /* keep only changed bits, and mask with required one from the
  219. * core */
  220. events = (stat ^ skt->old_status) & skt->requested_state.csc_mask;
  221. skt->old_status = stat;
  222. spin_unlock_bh(&skt->lock);
  223. if (events)
  224. pcmcia_parse_events(&skt->socket, events);
  225. mod_timer(&skt->timer,
  226. jiffies + msecs_to_jiffies(BCM63XX_PCMCIA_POLL_RATE));
  227. }
  228. static int bcm63xx_pcmcia_set_io_map(struct pcmcia_socket *sock,
  229. struct pccard_io_map *map)
  230. {
  231. /* this doesn't seem to be called by pcmcia layer if static
  232. * mapping is used */
  233. return 0;
  234. }
  235. static int bcm63xx_pcmcia_set_mem_map(struct pcmcia_socket *sock,
  236. struct pccard_mem_map *map)
  237. {
  238. struct bcm63xx_pcmcia_socket *skt;
  239. struct resource *res;
  240. skt = sock->driver_data;
  241. if (map->flags & MAP_ATTRIB)
  242. res = skt->attr_res;
  243. else
  244. res = skt->common_res;
  245. map->static_start = res->start + map->card_start;
  246. return 0;
  247. }
  248. static struct pccard_operations bcm63xx_pcmcia_operations = {
  249. .init = bcm63xx_pcmcia_sock_init,
  250. .suspend = bcm63xx_pcmcia_suspend,
  251. .get_status = bcm63xx_pcmcia_get_status,
  252. .set_socket = bcm63xx_pcmcia_set_socket,
  253. .set_io_map = bcm63xx_pcmcia_set_io_map,
  254. .set_mem_map = bcm63xx_pcmcia_set_mem_map,
  255. };
  256. /*
  257. * register pcmcia socket to core
  258. */
  259. static int __devinit bcm63xx_drv_pcmcia_probe(struct platform_device *pdev)
  260. {
  261. struct bcm63xx_pcmcia_socket *skt;
  262. struct pcmcia_socket *sock;
  263. struct resource *res, *irq_res;
  264. unsigned int regmem_size = 0, iomem_size = 0;
  265. u32 val;
  266. int ret;
  267. skt = kzalloc(sizeof(*skt), GFP_KERNEL);
  268. if (!skt)
  269. return -ENOMEM;
  270. spin_lock_init(&skt->lock);
  271. sock = &skt->socket;
  272. sock->driver_data = skt;
  273. /* make sure we have all resources we need */
  274. skt->common_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  275. skt->attr_res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  276. irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  277. skt->pd = pdev->dev.platform_data;
  278. if (!skt->common_res || !skt->attr_res || !irq_res || !skt->pd) {
  279. ret = -EINVAL;
  280. goto err;
  281. }
  282. /* remap pcmcia registers */
  283. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  284. regmem_size = resource_size(res);
  285. if (!request_mem_region(res->start, regmem_size, "bcm63xx_pcmcia")) {
  286. ret = -EINVAL;
  287. goto err;
  288. }
  289. skt->reg_res = res;
  290. skt->base = ioremap(res->start, regmem_size);
  291. if (!skt->base) {
  292. ret = -ENOMEM;
  293. goto err;
  294. }
  295. /* remap io registers */
  296. res = platform_get_resource(pdev, IORESOURCE_MEM, 3);
  297. iomem_size = resource_size(res);
  298. skt->io_base = ioremap(res->start, iomem_size);
  299. if (!skt->io_base) {
  300. ret = -ENOMEM;
  301. goto err;
  302. }
  303. /* resources are static */
  304. sock->resource_ops = &pccard_static_ops;
  305. sock->ops = &bcm63xx_pcmcia_operations;
  306. sock->owner = THIS_MODULE;
  307. sock->dev.parent = &pdev->dev;
  308. sock->features = SS_CAP_STATIC_MAP | SS_CAP_PCCARD;
  309. sock->io_offset = (unsigned long)skt->io_base;
  310. sock->pci_irq = irq_res->start;
  311. #ifdef CONFIG_CARDBUS
  312. sock->cb_dev = bcm63xx_cb_dev;
  313. if (bcm63xx_cb_dev)
  314. sock->features |= SS_CAP_CARDBUS;
  315. #endif
  316. /* assume common & attribute memory have the same size */
  317. sock->map_size = resource_size(skt->common_res);
  318. /* initialize polling timer */
  319. setup_timer(&skt->timer, bcm63xx_pcmcia_poll, (unsigned long)skt);
  320. /* initialize pcmcia control register, drive VS[12] to 0,
  321. * leave CB IDSEL to the old value since it is set by the PCI
  322. * layer */
  323. val = pcmcia_readl(skt, PCMCIA_C1_REG);
  324. val &= PCMCIA_C1_CBIDSEL_MASK;
  325. val |= PCMCIA_C1_EN_PCMCIA_GPIO_MASK;
  326. pcmcia_writel(skt, val, PCMCIA_C1_REG);
  327. /*
  328. * Hardware has only one set of timings registers, not one for
  329. * each memory access type, so we configure them for the
  330. * slowest one: attribute memory.
  331. */
  332. val = PCMCIA_C2_DATA16_MASK;
  333. val |= 10 << PCMCIA_C2_RWCOUNT_SHIFT;
  334. val |= 6 << PCMCIA_C2_INACTIVE_SHIFT;
  335. val |= 3 << PCMCIA_C2_SETUP_SHIFT;
  336. val |= 3 << PCMCIA_C2_HOLD_SHIFT;
  337. pcmcia_writel(skt, val, PCMCIA_C2_REG);
  338. ret = pcmcia_register_socket(sock);
  339. if (ret)
  340. goto err;
  341. /* start polling socket */
  342. mod_timer(&skt->timer,
  343. jiffies + msecs_to_jiffies(BCM63XX_PCMCIA_POLL_RATE));
  344. platform_set_drvdata(pdev, skt);
  345. return 0;
  346. err:
  347. if (skt->io_base)
  348. iounmap(skt->io_base);
  349. if (skt->base)
  350. iounmap(skt->base);
  351. if (skt->reg_res)
  352. release_mem_region(skt->reg_res->start, regmem_size);
  353. kfree(skt);
  354. return ret;
  355. }
  356. static int __devexit bcm63xx_drv_pcmcia_remove(struct platform_device *pdev)
  357. {
  358. struct bcm63xx_pcmcia_socket *skt;
  359. struct resource *res;
  360. skt = platform_get_drvdata(pdev);
  361. del_timer_sync(&skt->timer);
  362. iounmap(skt->base);
  363. iounmap(skt->io_base);
  364. res = skt->reg_res;
  365. release_mem_region(res->start, resource_size(res));
  366. kfree(skt);
  367. return 0;
  368. }
  369. struct platform_driver bcm63xx_pcmcia_driver = {
  370. .probe = bcm63xx_drv_pcmcia_probe,
  371. .remove = __devexit_p(bcm63xx_drv_pcmcia_remove),
  372. .driver = {
  373. .name = "bcm63xx_pcmcia",
  374. .owner = THIS_MODULE,
  375. },
  376. };
  377. #ifdef CONFIG_CARDBUS
  378. static int __devinit bcm63xx_cb_probe(struct pci_dev *dev,
  379. const struct pci_device_id *id)
  380. {
  381. /* keep pci device */
  382. bcm63xx_cb_dev = dev;
  383. return platform_driver_register(&bcm63xx_pcmcia_driver);
  384. }
  385. static void __devexit bcm63xx_cb_exit(struct pci_dev *dev)
  386. {
  387. platform_driver_unregister(&bcm63xx_pcmcia_driver);
  388. bcm63xx_cb_dev = NULL;
  389. }
  390. static struct pci_device_id bcm63xx_cb_table[] = {
  391. {
  392. .vendor = PCI_VENDOR_ID_BROADCOM,
  393. .device = BCM6348_CPU_ID,
  394. .subvendor = PCI_VENDOR_ID_BROADCOM,
  395. .subdevice = PCI_ANY_ID,
  396. .class = PCI_CLASS_BRIDGE_CARDBUS << 8,
  397. .class_mask = ~0,
  398. },
  399. {
  400. .vendor = PCI_VENDOR_ID_BROADCOM,
  401. .device = BCM6358_CPU_ID,
  402. .subvendor = PCI_VENDOR_ID_BROADCOM,
  403. .subdevice = PCI_ANY_ID,
  404. .class = PCI_CLASS_BRIDGE_CARDBUS << 8,
  405. .class_mask = ~0,
  406. },
  407. { },
  408. };
  409. MODULE_DEVICE_TABLE(pci, bcm63xx_cb_table);
  410. static struct pci_driver bcm63xx_cardbus_driver = {
  411. .name = "bcm63xx_cardbus",
  412. .id_table = bcm63xx_cb_table,
  413. .probe = bcm63xx_cb_probe,
  414. .remove = __devexit_p(bcm63xx_cb_exit),
  415. };
  416. #endif
  417. /*
  418. * if cardbus support is enabled, register our platform device after
  419. * our fake cardbus bridge has been registered
  420. */
  421. static int __init bcm63xx_pcmcia_init(void)
  422. {
  423. #ifdef CONFIG_CARDBUS
  424. return pci_register_driver(&bcm63xx_cardbus_driver);
  425. #else
  426. return platform_driver_register(&bcm63xx_pcmcia_driver);
  427. #endif
  428. }
  429. static void __exit bcm63xx_pcmcia_exit(void)
  430. {
  431. #ifdef CONFIG_CARDBUS
  432. return pci_unregister_driver(&bcm63xx_cardbus_driver);
  433. #else
  434. platform_driver_unregister(&bcm63xx_pcmcia_driver);
  435. #endif
  436. }
  437. module_init(bcm63xx_pcmcia_init);
  438. module_exit(bcm63xx_pcmcia_exit);
  439. MODULE_LICENSE("GPL");
  440. MODULE_AUTHOR("Maxime Bizon <mbizon@freebox.fr>");
  441. MODULE_DESCRIPTION("Linux PCMCIA Card Services: bcm63xx Socket Controller");