db1xxx_ss.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. /*
  2. * PCMCIA socket code for the Alchemy Db1xxx/Pb1xxx boards.
  3. *
  4. * Copyright (c) 2009 Manuel Lauss <manuel.lauss@gmail.com>
  5. *
  6. */
  7. /* This is a fairly generic PCMCIA socket driver suitable for the
  8. * following Alchemy Development boards:
  9. * Db1000, Db/Pb1500, Db/Pb1100, Db/Pb1550, Db/Pb1200.
  10. *
  11. * The Db1000 is used as a reference: Per-socket card-, carddetect- and
  12. * statuschange IRQs connected to SoC GPIOs, control and status register
  13. * bits arranged in per-socket groups in an external PLD. All boards
  14. * listed here use this layout, including bit positions and meanings.
  15. * Of course there are exceptions in later boards:
  16. *
  17. * - Pb1100/Pb1500: single socket only; voltage key bits VS are
  18. * at STATUS[5:4] (instead of STATUS[1:0]).
  19. * - Au1200-based: additional card-eject irqs, irqs not gpios!
  20. */
  21. #include <linux/delay.h>
  22. #include <linux/gpio.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/pm.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/resource.h>
  27. #include <linux/slab.h>
  28. #include <linux/spinlock.h>
  29. #include <pcmcia/ss.h>
  30. #include <asm/mach-au1x00/au1000.h>
  31. #include <asm/mach-db1x00/bcsr.h>
  32. #define MEM_MAP_SIZE 0x400000
  33. #define IO_MAP_SIZE 0x1000
  34. struct db1x_pcmcia_sock {
  35. struct pcmcia_socket socket;
  36. int nr; /* socket number */
  37. void *virt_io;
  38. phys_addr_t phys_io;
  39. phys_addr_t phys_attr;
  40. phys_addr_t phys_mem;
  41. /* previous flags for set_socket() */
  42. unsigned int old_flags;
  43. /* interrupt sources: linux irq numbers! */
  44. int insert_irq; /* default carddetect irq */
  45. int stschg_irq; /* card-status-change irq */
  46. int card_irq; /* card irq */
  47. int eject_irq; /* db1200/pb1200 have these */
  48. #define BOARD_TYPE_DEFAULT 0 /* most boards */
  49. #define BOARD_TYPE_DB1200 1 /* IRQs aren't gpios */
  50. #define BOARD_TYPE_PB1100 2 /* VS bits slightly different */
  51. int board_type;
  52. };
  53. #define to_db1x_socket(x) container_of(x, struct db1x_pcmcia_sock, socket)
  54. /* DB/PB1200: check CPLD SIGSTATUS register bit 10/12 */
  55. static int db1200_card_inserted(struct db1x_pcmcia_sock *sock)
  56. {
  57. unsigned short sigstat;
  58. sigstat = bcsr_read(BCSR_SIGSTAT);
  59. return sigstat & 1 << (8 + 2 * sock->nr);
  60. }
  61. /* carddetect gpio: low-active */
  62. static int db1000_card_inserted(struct db1x_pcmcia_sock *sock)
  63. {
  64. return !gpio_get_value(irq_to_gpio(sock->insert_irq));
  65. }
  66. static int db1x_card_inserted(struct db1x_pcmcia_sock *sock)
  67. {
  68. switch (sock->board_type) {
  69. case BOARD_TYPE_DB1200:
  70. return db1200_card_inserted(sock);
  71. default:
  72. return db1000_card_inserted(sock);
  73. }
  74. }
  75. /* STSCHG tends to bounce heavily when cards are inserted/ejected.
  76. * To avoid this, the interrupt is normally disabled and only enabled
  77. * after reset to a card has been de-asserted.
  78. */
  79. static inline void set_stschg(struct db1x_pcmcia_sock *sock, int en)
  80. {
  81. if (sock->stschg_irq != -1) {
  82. if (en)
  83. enable_irq(sock->stschg_irq);
  84. else
  85. disable_irq(sock->stschg_irq);
  86. }
  87. }
  88. static irqreturn_t db1000_pcmcia_cdirq(int irq, void *data)
  89. {
  90. struct db1x_pcmcia_sock *sock = data;
  91. pcmcia_parse_events(&sock->socket, SS_DETECT);
  92. return IRQ_HANDLED;
  93. }
  94. static irqreturn_t db1000_pcmcia_stschgirq(int irq, void *data)
  95. {
  96. struct db1x_pcmcia_sock *sock = data;
  97. pcmcia_parse_events(&sock->socket, SS_STSCHG);
  98. return IRQ_HANDLED;
  99. }
  100. static irqreturn_t db1200_pcmcia_cdirq(int irq, void *data)
  101. {
  102. struct db1x_pcmcia_sock *sock = data;
  103. /* Db/Pb1200 have separate per-socket insertion and ejection
  104. * interrupts which stay asserted as long as the card is
  105. * inserted/missing. The one which caused us to be called
  106. * needs to be disabled and the other one enabled.
  107. */
  108. if (irq == sock->insert_irq) {
  109. disable_irq_nosync(sock->insert_irq);
  110. enable_irq(sock->eject_irq);
  111. } else {
  112. disable_irq_nosync(sock->eject_irq);
  113. enable_irq(sock->insert_irq);
  114. }
  115. pcmcia_parse_events(&sock->socket, SS_DETECT);
  116. return IRQ_HANDLED;
  117. }
  118. static int db1x_pcmcia_setup_irqs(struct db1x_pcmcia_sock *sock)
  119. {
  120. int ret;
  121. if (sock->stschg_irq != -1) {
  122. ret = request_irq(sock->stschg_irq, db1000_pcmcia_stschgirq,
  123. 0, "pcmcia_stschg", sock);
  124. if (ret)
  125. return ret;
  126. }
  127. /* Db/Pb1200 have separate per-socket insertion and ejection
  128. * interrupts, which should show edge behaviour but don't.
  129. * So interrupts are disabled until both insertion and
  130. * ejection handler have been registered and the currently
  131. * active one disabled.
  132. */
  133. if (sock->board_type == BOARD_TYPE_DB1200) {
  134. ret = request_irq(sock->insert_irq, db1200_pcmcia_cdirq,
  135. IRQF_DISABLED, "pcmcia_insert", sock);
  136. if (ret)
  137. goto out1;
  138. ret = request_irq(sock->eject_irq, db1200_pcmcia_cdirq,
  139. IRQF_DISABLED, "pcmcia_eject", sock);
  140. if (ret) {
  141. free_irq(sock->insert_irq, sock);
  142. goto out1;
  143. }
  144. /* enable the currently silent one */
  145. if (db1200_card_inserted(sock))
  146. enable_irq(sock->eject_irq);
  147. else
  148. enable_irq(sock->insert_irq);
  149. } else {
  150. /* all other (older) Db1x00 boards use a GPIO to show
  151. * card detection status: use both-edge triggers.
  152. */
  153. irq_set_irq_type(sock->insert_irq, IRQ_TYPE_EDGE_BOTH);
  154. ret = request_irq(sock->insert_irq, db1000_pcmcia_cdirq,
  155. 0, "pcmcia_carddetect", sock);
  156. if (ret)
  157. goto out1;
  158. }
  159. return 0; /* all done */
  160. out1:
  161. if (sock->stschg_irq != -1)
  162. free_irq(sock->stschg_irq, sock);
  163. return ret;
  164. }
  165. static void db1x_pcmcia_free_irqs(struct db1x_pcmcia_sock *sock)
  166. {
  167. if (sock->stschg_irq != -1)
  168. free_irq(sock->stschg_irq, sock);
  169. free_irq(sock->insert_irq, sock);
  170. if (sock->eject_irq != -1)
  171. free_irq(sock->eject_irq, sock);
  172. }
  173. /*
  174. * configure a PCMCIA socket on the Db1x00 series of boards (and
  175. * compatibles).
  176. *
  177. * 2 external registers are involved:
  178. * pcmcia_status (offset 0x04): bits [0:1/2:3]: read card voltage id
  179. * pcmcia_control(offset 0x10):
  180. * bits[0:1] set vcc for card
  181. * bits[2:3] set vpp for card
  182. * bit 4: enable data buffers
  183. * bit 7: reset# for card
  184. * add 8 for second socket.
  185. */
  186. static int db1x_pcmcia_configure(struct pcmcia_socket *skt,
  187. struct socket_state_t *state)
  188. {
  189. struct db1x_pcmcia_sock *sock = to_db1x_socket(skt);
  190. unsigned short cr_clr, cr_set;
  191. unsigned int changed;
  192. int v, p, ret;
  193. /* card voltage setup */
  194. cr_clr = (0xf << (sock->nr * 8)); /* clear voltage settings */
  195. cr_set = 0;
  196. v = p = ret = 0;
  197. switch (state->Vcc) {
  198. case 50:
  199. ++v;
  200. case 33:
  201. ++v;
  202. case 0:
  203. break;
  204. default:
  205. printk(KERN_INFO "pcmcia%d unsupported Vcc %d\n",
  206. sock->nr, state->Vcc);
  207. }
  208. switch (state->Vpp) {
  209. case 12:
  210. ++p;
  211. case 33:
  212. case 50:
  213. ++p;
  214. case 0:
  215. break;
  216. default:
  217. printk(KERN_INFO "pcmcia%d unsupported Vpp %d\n",
  218. sock->nr, state->Vpp);
  219. }
  220. /* sanity check: Vpp must be 0, 12, or Vcc */
  221. if (((state->Vcc == 33) && (state->Vpp == 50)) ||
  222. ((state->Vcc == 50) && (state->Vpp == 33))) {
  223. printk(KERN_INFO "pcmcia%d bad Vcc/Vpp combo (%d %d)\n",
  224. sock->nr, state->Vcc, state->Vpp);
  225. v = p = 0;
  226. ret = -EINVAL;
  227. }
  228. /* create new voltage code */
  229. cr_set |= ((v << 2) | p) << (sock->nr * 8);
  230. changed = state->flags ^ sock->old_flags;
  231. if (changed & SS_RESET) {
  232. if (state->flags & SS_RESET) {
  233. set_stschg(sock, 0);
  234. /* assert reset, disable io buffers */
  235. cr_clr |= (1 << (7 + (sock->nr * 8)));
  236. cr_clr |= (1 << (4 + (sock->nr * 8)));
  237. } else {
  238. /* de-assert reset, enable io buffers */
  239. cr_set |= 1 << (7 + (sock->nr * 8));
  240. cr_set |= 1 << (4 + (sock->nr * 8));
  241. }
  242. }
  243. /* update PCMCIA configuration */
  244. bcsr_mod(BCSR_PCMCIA, cr_clr, cr_set);
  245. sock->old_flags = state->flags;
  246. /* reset was taken away: give card time to initialize properly */
  247. if ((changed & SS_RESET) && !(state->flags & SS_RESET)) {
  248. msleep(500);
  249. set_stschg(sock, 1);
  250. }
  251. return ret;
  252. }
  253. /* VCC bits at [3:2]/[11:10] */
  254. #define GET_VCC(cr, socknr) \
  255. ((((cr) >> 2) >> ((socknr) * 8)) & 3)
  256. /* VS bits at [0:1]/[3:2] */
  257. #define GET_VS(sr, socknr) \
  258. (((sr) >> (2 * (socknr))) & 3)
  259. /* reset bits at [7]/[15] */
  260. #define GET_RESET(cr, socknr) \
  261. ((cr) & (1 << (7 + (8 * (socknr)))))
  262. static int db1x_pcmcia_get_status(struct pcmcia_socket *skt,
  263. unsigned int *value)
  264. {
  265. struct db1x_pcmcia_sock *sock = to_db1x_socket(skt);
  266. unsigned short cr, sr;
  267. unsigned int status;
  268. status = db1x_card_inserted(sock) ? SS_DETECT : 0;
  269. cr = bcsr_read(BCSR_PCMCIA);
  270. sr = bcsr_read(BCSR_STATUS);
  271. /* PB1100/PB1500: voltage key bits are at [5:4] */
  272. if (sock->board_type == BOARD_TYPE_PB1100)
  273. sr >>= 4;
  274. /* determine card type */
  275. switch (GET_VS(sr, sock->nr)) {
  276. case 0:
  277. case 2:
  278. status |= SS_3VCARD; /* 3V card */
  279. case 3:
  280. break; /* 5V card: set nothing */
  281. default:
  282. status |= SS_XVCARD; /* treated as unsupported in core */
  283. }
  284. /* if Vcc is not zero, we have applied power to a card */
  285. status |= GET_VCC(cr, sock->nr) ? SS_POWERON : 0;
  286. /* reset de-asserted? then we're ready */
  287. status |= (GET_RESET(cr, sock->nr)) ? SS_READY : SS_RESET;
  288. *value = status;
  289. return 0;
  290. }
  291. static int db1x_pcmcia_sock_init(struct pcmcia_socket *skt)
  292. {
  293. return 0;
  294. }
  295. static int db1x_pcmcia_sock_suspend(struct pcmcia_socket *skt)
  296. {
  297. return 0;
  298. }
  299. static int au1x00_pcmcia_set_io_map(struct pcmcia_socket *skt,
  300. struct pccard_io_map *map)
  301. {
  302. struct db1x_pcmcia_sock *sock = to_db1x_socket(skt);
  303. map->start = (u32)sock->virt_io;
  304. map->stop = map->start + IO_MAP_SIZE;
  305. return 0;
  306. }
  307. static int au1x00_pcmcia_set_mem_map(struct pcmcia_socket *skt,
  308. struct pccard_mem_map *map)
  309. {
  310. struct db1x_pcmcia_sock *sock = to_db1x_socket(skt);
  311. if (map->flags & MAP_ATTRIB)
  312. map->static_start = sock->phys_attr + map->card_start;
  313. else
  314. map->static_start = sock->phys_mem + map->card_start;
  315. return 0;
  316. }
  317. static struct pccard_operations db1x_pcmcia_operations = {
  318. .init = db1x_pcmcia_sock_init,
  319. .suspend = db1x_pcmcia_sock_suspend,
  320. .get_status = db1x_pcmcia_get_status,
  321. .set_socket = db1x_pcmcia_configure,
  322. .set_io_map = au1x00_pcmcia_set_io_map,
  323. .set_mem_map = au1x00_pcmcia_set_mem_map,
  324. };
  325. static int __devinit db1x_pcmcia_socket_probe(struct platform_device *pdev)
  326. {
  327. struct db1x_pcmcia_sock *sock;
  328. struct resource *r;
  329. int ret, bid;
  330. sock = kzalloc(sizeof(struct db1x_pcmcia_sock), GFP_KERNEL);
  331. if (!sock)
  332. return -ENOMEM;
  333. sock->nr = pdev->id;
  334. bid = BCSR_WHOAMI_BOARD(bcsr_read(BCSR_WHOAMI));
  335. switch (bid) {
  336. case BCSR_WHOAMI_PB1500:
  337. case BCSR_WHOAMI_PB1500R2:
  338. case BCSR_WHOAMI_PB1100:
  339. sock->board_type = BOARD_TYPE_PB1100;
  340. break;
  341. case BCSR_WHOAMI_DB1000 ... BCSR_WHOAMI_PB1550_SDR:
  342. sock->board_type = BOARD_TYPE_DEFAULT;
  343. break;
  344. case BCSR_WHOAMI_PB1200 ... BCSR_WHOAMI_DB1200:
  345. sock->board_type = BOARD_TYPE_DB1200;
  346. break;
  347. default:
  348. printk(KERN_INFO "db1xxx-ss: unknown board %d!\n", bid);
  349. ret = -ENODEV;
  350. goto out0;
  351. };
  352. /*
  353. * gather resources necessary and optional nice-to-haves to
  354. * operate a socket:
  355. * This includes IRQs for Carddetection/ejection, the card
  356. * itself and optional status change detection.
  357. * Also, the memory areas covered by a socket. For these
  358. * we require the real 36bit addresses (see the au1000.h
  359. * header for more information).
  360. */
  361. /* card: irq assigned to the card itself. */
  362. r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "card");
  363. sock->card_irq = r ? r->start : 0;
  364. /* insert: irq which triggers on card insertion/ejection */
  365. r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "insert");
  366. sock->insert_irq = r ? r->start : -1;
  367. /* stschg: irq which trigger on card status change (optional) */
  368. r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "stschg");
  369. sock->stschg_irq = r ? r->start : -1;
  370. /* eject: irq which triggers on ejection (DB1200/PB1200 only) */
  371. r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "eject");
  372. sock->eject_irq = r ? r->start : -1;
  373. ret = -ENODEV;
  374. /* 36bit PCMCIA Attribute area address */
  375. r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pcmcia-attr");
  376. if (!r) {
  377. printk(KERN_ERR "pcmcia%d has no 'pseudo-attr' resource!\n",
  378. sock->nr);
  379. goto out0;
  380. }
  381. sock->phys_attr = r->start;
  382. /* 36bit PCMCIA Memory area address */
  383. r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pcmcia-mem");
  384. if (!r) {
  385. printk(KERN_ERR "pcmcia%d has no 'pseudo-mem' resource!\n",
  386. sock->nr);
  387. goto out0;
  388. }
  389. sock->phys_mem = r->start;
  390. /* 36bit PCMCIA IO area address */
  391. r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pcmcia-io");
  392. if (!r) {
  393. printk(KERN_ERR "pcmcia%d has no 'pseudo-io' resource!\n",
  394. sock->nr);
  395. goto out0;
  396. }
  397. sock->phys_io = r->start;
  398. /*
  399. * PCMCIA client drivers use the inb/outb macros to access
  400. * the IO registers. Since mips_io_port_base is added
  401. * to the access address of the mips implementation of
  402. * inb/outb, we need to subtract it here because we want
  403. * to access the I/O or MEM address directly, without
  404. * going through this "mips_io_port_base" mechanism.
  405. */
  406. sock->virt_io = (void *)(ioremap(sock->phys_io, IO_MAP_SIZE) -
  407. mips_io_port_base);
  408. if (!sock->virt_io) {
  409. printk(KERN_ERR "pcmcia%d: cannot remap IO area\n",
  410. sock->nr);
  411. ret = -ENOMEM;
  412. goto out0;
  413. }
  414. sock->socket.ops = &db1x_pcmcia_operations;
  415. sock->socket.owner = THIS_MODULE;
  416. sock->socket.pci_irq = sock->card_irq;
  417. sock->socket.features = SS_CAP_STATIC_MAP | SS_CAP_PCCARD;
  418. sock->socket.map_size = MEM_MAP_SIZE;
  419. sock->socket.io_offset = (unsigned long)sock->virt_io;
  420. sock->socket.dev.parent = &pdev->dev;
  421. sock->socket.resource_ops = &pccard_static_ops;
  422. platform_set_drvdata(pdev, sock);
  423. ret = db1x_pcmcia_setup_irqs(sock);
  424. if (ret) {
  425. printk(KERN_ERR "pcmcia%d cannot setup interrupts\n",
  426. sock->nr);
  427. goto out1;
  428. }
  429. set_stschg(sock, 0);
  430. ret = pcmcia_register_socket(&sock->socket);
  431. if (ret) {
  432. printk(KERN_ERR "pcmcia%d failed to register\n", sock->nr);
  433. goto out2;
  434. }
  435. printk(KERN_INFO "Alchemy Db/Pb1xxx pcmcia%d @ io/attr/mem %09llx"
  436. "(%p) %09llx %09llx card/insert/stschg/eject irqs @ %d "
  437. "%d %d %d\n", sock->nr, sock->phys_io, sock->virt_io,
  438. sock->phys_attr, sock->phys_mem, sock->card_irq,
  439. sock->insert_irq, sock->stschg_irq, sock->eject_irq);
  440. return 0;
  441. out2:
  442. db1x_pcmcia_free_irqs(sock);
  443. out1:
  444. iounmap((void *)(sock->virt_io + (u32)mips_io_port_base));
  445. out0:
  446. kfree(sock);
  447. return ret;
  448. }
  449. static int __devexit db1x_pcmcia_socket_remove(struct platform_device *pdev)
  450. {
  451. struct db1x_pcmcia_sock *sock = platform_get_drvdata(pdev);
  452. db1x_pcmcia_free_irqs(sock);
  453. pcmcia_unregister_socket(&sock->socket);
  454. iounmap((void *)(sock->virt_io + (u32)mips_io_port_base));
  455. kfree(sock);
  456. return 0;
  457. }
  458. static struct platform_driver db1x_pcmcia_socket_driver = {
  459. .driver = {
  460. .name = "db1xxx_pcmcia",
  461. .owner = THIS_MODULE,
  462. },
  463. .probe = db1x_pcmcia_socket_probe,
  464. .remove = __devexit_p(db1x_pcmcia_socket_remove),
  465. };
  466. int __init db1x_pcmcia_socket_load(void)
  467. {
  468. return platform_driver_register(&db1x_pcmcia_socket_driver);
  469. }
  470. void __exit db1x_pcmcia_socket_unload(void)
  471. {
  472. platform_driver_unregister(&db1x_pcmcia_socket_driver);
  473. }
  474. module_init(db1x_pcmcia_socket_load);
  475. module_exit(db1x_pcmcia_socket_unload);
  476. MODULE_LICENSE("GPL");
  477. MODULE_DESCRIPTION("PCMCIA Socket Services for Alchemy Db/Pb1x00 boards");
  478. MODULE_AUTHOR("Manuel Lauss");