au1000_generic.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /*
  2. *
  3. * Alchemy Semi Au1000 pcmcia driver
  4. *
  5. * Copyright 2001-2003 MontaVista Software Inc.
  6. * Author: MontaVista Software, Inc.
  7. * ppopov@embeddedalley.com or source@mvista.com
  8. *
  9. * Copyright 2004 Pete Popov, Embedded Alley Solutions, Inc.
  10. * Updated the driver to 2.6. Followed the sa11xx API and largely
  11. * copied many of the hardware independent functions.
  12. *
  13. * ########################################################################
  14. *
  15. * This program is free software; you can distribute it and/or modify it
  16. * under the terms of the GNU General Public License (Version 2) as
  17. * published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope it will be useful, but WITHOUT
  20. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  21. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  22. * for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License along
  25. * with this program; if not, write to the Free Software Foundation, Inc.,
  26. * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
  27. *
  28. * ########################################################################
  29. *
  30. *
  31. */
  32. #include <linux/module.h>
  33. #include <linux/moduleparam.h>
  34. #include <linux/init.h>
  35. #include <linux/cpufreq.h>
  36. #include <linux/ioport.h>
  37. #include <linux/kernel.h>
  38. #include <linux/timer.h>
  39. #include <linux/mm.h>
  40. #include <linux/notifier.h>
  41. #include <linux/interrupt.h>
  42. #include <linux/spinlock.h>
  43. #include <linux/mutex.h>
  44. #include <linux/platform_device.h>
  45. #include <linux/slab.h>
  46. #include <asm/io.h>
  47. #include <asm/irq.h>
  48. #include <asm/system.h>
  49. #include <asm/mach-au1x00/au1000.h>
  50. #include "au1000_generic.h"
  51. MODULE_LICENSE("GPL");
  52. MODULE_AUTHOR("Pete Popov <ppopov@embeddedalley.com>");
  53. MODULE_DESCRIPTION("Linux PCMCIA Card Services: Au1x00 Socket Controller");
  54. #if 0
  55. #define debug(x,args...) printk(KERN_DEBUG "%s: " x, __func__ , ##args)
  56. #else
  57. #define debug(x,args...)
  58. #endif
  59. #define MAP_SIZE 0x100000
  60. extern struct au1000_pcmcia_socket au1000_pcmcia_socket[];
  61. #define PCMCIA_SOCKET(x) (au1000_pcmcia_socket + (x))
  62. #define to_au1000_socket(x) container_of(x, struct au1000_pcmcia_socket, socket)
  63. /* Some boards like to support CF cards as IDE root devices, so they
  64. * grab pcmcia sockets directly.
  65. */
  66. u32 *pcmcia_base_vaddrs[2];
  67. extern const unsigned long mips_io_port_base;
  68. static DEFINE_MUTEX(pcmcia_sockets_lock);
  69. static int (*au1x00_pcmcia_hw_init[])(struct device *dev) = {
  70. au1x_board_init,
  71. };
  72. static int
  73. au1x00_pcmcia_skt_state(struct au1000_pcmcia_socket *skt)
  74. {
  75. struct pcmcia_state state;
  76. unsigned int stat;
  77. memset(&state, 0, sizeof(struct pcmcia_state));
  78. skt->ops->socket_state(skt, &state);
  79. stat = state.detect ? SS_DETECT : 0;
  80. stat |= state.ready ? SS_READY : 0;
  81. stat |= state.wrprot ? SS_WRPROT : 0;
  82. stat |= state.vs_3v ? SS_3VCARD : 0;
  83. stat |= state.vs_Xv ? SS_XVCARD : 0;
  84. stat |= skt->cs_state.Vcc ? SS_POWERON : 0;
  85. if (skt->cs_state.flags & SS_IOCARD)
  86. stat |= state.bvd1 ? SS_STSCHG : 0;
  87. else {
  88. if (state.bvd1 == 0)
  89. stat |= SS_BATDEAD;
  90. else if (state.bvd2 == 0)
  91. stat |= SS_BATWARN;
  92. }
  93. return stat;
  94. }
  95. /*
  96. * au100_pcmcia_config_skt
  97. *
  98. * Convert PCMCIA socket state to our socket configure structure.
  99. */
  100. static int
  101. au1x00_pcmcia_config_skt(struct au1000_pcmcia_socket *skt, socket_state_t *state)
  102. {
  103. int ret;
  104. ret = skt->ops->configure_socket(skt, state);
  105. if (ret == 0) {
  106. skt->cs_state = *state;
  107. }
  108. if (ret < 0)
  109. debug("unable to configure socket %d\n", skt->nr);
  110. return ret;
  111. }
  112. /* au1x00_pcmcia_sock_init()
  113. *
  114. * (Re-)Initialise the socket, turning on status interrupts
  115. * and PCMCIA bus. This must wait for power to stabilise
  116. * so that the card status signals report correctly.
  117. *
  118. * Returns: 0
  119. */
  120. static int au1x00_pcmcia_sock_init(struct pcmcia_socket *sock)
  121. {
  122. struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
  123. debug("initializing socket %u\n", skt->nr);
  124. skt->ops->socket_init(skt);
  125. return 0;
  126. }
  127. /*
  128. * au1x00_pcmcia_suspend()
  129. *
  130. * Remove power on the socket, disable IRQs from the card.
  131. * Turn off status interrupts, and disable the PCMCIA bus.
  132. *
  133. * Returns: 0
  134. */
  135. static int au1x00_pcmcia_suspend(struct pcmcia_socket *sock)
  136. {
  137. struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
  138. debug("suspending socket %u\n", skt->nr);
  139. skt->ops->socket_suspend(skt);
  140. return 0;
  141. }
  142. static DEFINE_SPINLOCK(status_lock);
  143. /*
  144. * au1x00_check_status()
  145. */
  146. static void au1x00_check_status(struct au1000_pcmcia_socket *skt)
  147. {
  148. unsigned int events;
  149. debug("entering PCMCIA monitoring thread\n");
  150. do {
  151. unsigned int status;
  152. unsigned long flags;
  153. status = au1x00_pcmcia_skt_state(skt);
  154. spin_lock_irqsave(&status_lock, flags);
  155. events = (status ^ skt->status) & skt->cs_state.csc_mask;
  156. skt->status = status;
  157. spin_unlock_irqrestore(&status_lock, flags);
  158. debug("events: %s%s%s%s%s%s\n",
  159. events == 0 ? "<NONE>" : "",
  160. events & SS_DETECT ? "DETECT " : "",
  161. events & SS_READY ? "READY " : "",
  162. events & SS_BATDEAD ? "BATDEAD " : "",
  163. events & SS_BATWARN ? "BATWARN " : "",
  164. events & SS_STSCHG ? "STSCHG " : "");
  165. if (events)
  166. pcmcia_parse_events(&skt->socket, events);
  167. } while (events);
  168. }
  169. /*
  170. * au1x00_pcmcia_poll_event()
  171. * Let's poll for events in addition to IRQs since IRQ only is unreliable...
  172. */
  173. static void au1x00_pcmcia_poll_event(unsigned long dummy)
  174. {
  175. struct au1000_pcmcia_socket *skt = (struct au1000_pcmcia_socket *)dummy;
  176. debug("polling for events\n");
  177. mod_timer(&skt->poll_timer, jiffies + AU1000_PCMCIA_POLL_PERIOD);
  178. au1x00_check_status(skt);
  179. }
  180. /* au1x00_pcmcia_get_status()
  181. *
  182. * From the sa11xx_core.c:
  183. * Implements the get_status() operation for the in-kernel PCMCIA
  184. * service (formerly SS_GetStatus in Card Services). Essentially just
  185. * fills in bits in `status' according to internal driver state or
  186. * the value of the voltage detect chipselect register.
  187. *
  188. * As a debugging note, during card startup, the PCMCIA core issues
  189. * three set_socket() commands in a row the first with RESET deasserted,
  190. * the second with RESET asserted, and the last with RESET deasserted
  191. * again. Following the third set_socket(), a get_status() command will
  192. * be issued. The kernel is looking for the SS_READY flag (see
  193. * setup_socket(), reset_socket(), and unreset_socket() in cs.c).
  194. *
  195. * Returns: 0
  196. */
  197. static int
  198. au1x00_pcmcia_get_status(struct pcmcia_socket *sock, unsigned int *status)
  199. {
  200. struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
  201. skt->status = au1x00_pcmcia_skt_state(skt);
  202. *status = skt->status;
  203. return 0;
  204. }
  205. /* au1x00_pcmcia_set_socket()
  206. * Implements the set_socket() operation for the in-kernel PCMCIA
  207. * service (formerly SS_SetSocket in Card Services). We more or
  208. * less punt all of this work and let the kernel handle the details
  209. * of power configuration, reset, &c. We also record the value of
  210. * `state' in order to regurgitate it to the PCMCIA core later.
  211. *
  212. * Returns: 0
  213. */
  214. static int
  215. au1x00_pcmcia_set_socket(struct pcmcia_socket *sock, socket_state_t *state)
  216. {
  217. struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
  218. debug("for sock %u\n", skt->nr);
  219. debug("\tmask: %s%s%s%s%s%s\n\tflags: %s%s%s%s%s%s\n",
  220. (state->csc_mask==0)?"<NONE>":"",
  221. (state->csc_mask&SS_DETECT)?"DETECT ":"",
  222. (state->csc_mask&SS_READY)?"READY ":"",
  223. (state->csc_mask&SS_BATDEAD)?"BATDEAD ":"",
  224. (state->csc_mask&SS_BATWARN)?"BATWARN ":"",
  225. (state->csc_mask&SS_STSCHG)?"STSCHG ":"",
  226. (state->flags==0)?"<NONE>":"",
  227. (state->flags&SS_PWR_AUTO)?"PWR_AUTO ":"",
  228. (state->flags&SS_IOCARD)?"IOCARD ":"",
  229. (state->flags&SS_RESET)?"RESET ":"",
  230. (state->flags&SS_SPKR_ENA)?"SPKR_ENA ":"",
  231. (state->flags&SS_OUTPUT_ENA)?"OUTPUT_ENA ":"");
  232. debug("\tVcc %d Vpp %d irq %d\n",
  233. state->Vcc, state->Vpp, state->io_irq);
  234. return au1x00_pcmcia_config_skt(skt, state);
  235. }
  236. int
  237. au1x00_pcmcia_set_io_map(struct pcmcia_socket *sock, struct pccard_io_map *map)
  238. {
  239. struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
  240. unsigned int speed;
  241. if(map->map>=MAX_IO_WIN){
  242. debug("map (%d) out of range\n", map->map);
  243. return -1;
  244. }
  245. if(map->flags&MAP_ACTIVE){
  246. speed=(map->speed>0)?map->speed:AU1000_PCMCIA_IO_SPEED;
  247. skt->spd_io[map->map] = speed;
  248. }
  249. map->start=(unsigned int)(u32)skt->virt_io;
  250. map->stop=map->start+MAP_SIZE;
  251. return 0;
  252. } /* au1x00_pcmcia_set_io_map() */
  253. static int
  254. au1x00_pcmcia_set_mem_map(struct pcmcia_socket *sock, struct pccard_mem_map *map)
  255. {
  256. struct au1000_pcmcia_socket *skt = to_au1000_socket(sock);
  257. unsigned short speed = map->speed;
  258. if(map->map>=MAX_WIN){
  259. debug("map (%d) out of range\n", map->map);
  260. return -1;
  261. }
  262. if (map->flags & MAP_ATTRIB) {
  263. skt->spd_attr[map->map] = speed;
  264. skt->spd_mem[map->map] = 0;
  265. } else {
  266. skt->spd_attr[map->map] = 0;
  267. skt->spd_mem[map->map] = speed;
  268. }
  269. if (map->flags & MAP_ATTRIB) {
  270. map->static_start = skt->phys_attr + map->card_start;
  271. }
  272. else {
  273. map->static_start = skt->phys_mem + map->card_start;
  274. }
  275. debug("set_mem_map %d start %08lx card_start %08x\n",
  276. map->map, map->static_start, map->card_start);
  277. return 0;
  278. } /* au1x00_pcmcia_set_mem_map() */
  279. static struct pccard_operations au1x00_pcmcia_operations = {
  280. .init = au1x00_pcmcia_sock_init,
  281. .suspend = au1x00_pcmcia_suspend,
  282. .get_status = au1x00_pcmcia_get_status,
  283. .set_socket = au1x00_pcmcia_set_socket,
  284. .set_io_map = au1x00_pcmcia_set_io_map,
  285. .set_mem_map = au1x00_pcmcia_set_mem_map,
  286. };
  287. static const char *skt_names[] = {
  288. "PCMCIA socket 0",
  289. "PCMCIA socket 1",
  290. };
  291. struct skt_dev_info {
  292. int nskt;
  293. };
  294. int au1x00_pcmcia_socket_probe(struct device *dev, struct pcmcia_low_level *ops, int first, int nr)
  295. {
  296. struct skt_dev_info *sinfo;
  297. struct au1000_pcmcia_socket *skt;
  298. int ret, i;
  299. sinfo = kzalloc(sizeof(struct skt_dev_info), GFP_KERNEL);
  300. if (!sinfo) {
  301. ret = -ENOMEM;
  302. goto out;
  303. }
  304. sinfo->nskt = nr;
  305. /*
  306. * Initialise the per-socket structure.
  307. */
  308. for (i = 0; i < nr; i++) {
  309. skt = PCMCIA_SOCKET(i);
  310. memset(skt, 0, sizeof(*skt));
  311. skt->socket.resource_ops = &pccard_static_ops;
  312. skt->socket.ops = &au1x00_pcmcia_operations;
  313. skt->socket.owner = ops->owner;
  314. skt->socket.dev.parent = dev;
  315. init_timer(&skt->poll_timer);
  316. skt->poll_timer.function = au1x00_pcmcia_poll_event;
  317. skt->poll_timer.data = (unsigned long)skt;
  318. skt->poll_timer.expires = jiffies + AU1000_PCMCIA_POLL_PERIOD;
  319. skt->nr = first + i;
  320. skt->irq = 255;
  321. skt->dev = dev;
  322. skt->ops = ops;
  323. skt->res_skt.name = skt_names[skt->nr];
  324. skt->res_io.name = "io";
  325. skt->res_io.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  326. skt->res_mem.name = "memory";
  327. skt->res_mem.flags = IORESOURCE_MEM;
  328. skt->res_attr.name = "attribute";
  329. skt->res_attr.flags = IORESOURCE_MEM;
  330. /*
  331. * PCMCIA client drivers use the inb/outb macros to access the
  332. * IO registers. Since mips_io_port_base is added to the
  333. * access address of the mips implementation of inb/outb,
  334. * we need to subtract it here because we want to access the
  335. * I/O or MEM address directly, without going through this
  336. * "mips_io_port_base" mechanism.
  337. */
  338. if (i == 0) {
  339. skt->virt_io = (void *)
  340. (ioremap((phys_t)AU1X_SOCK0_IO, 0x1000) -
  341. (u32)mips_io_port_base);
  342. skt->phys_attr = AU1X_SOCK0_PHYS_ATTR;
  343. skt->phys_mem = AU1X_SOCK0_PHYS_MEM;
  344. }
  345. else {
  346. skt->virt_io = (void *)
  347. (ioremap((phys_t)AU1X_SOCK1_IO, 0x1000) -
  348. (u32)mips_io_port_base);
  349. skt->phys_attr = AU1X_SOCK1_PHYS_ATTR;
  350. skt->phys_mem = AU1X_SOCK1_PHYS_MEM;
  351. }
  352. pcmcia_base_vaddrs[i] = (u32 *)skt->virt_io;
  353. ret = ops->hw_init(skt);
  354. skt->socket.features = SS_CAP_STATIC_MAP|SS_CAP_PCCARD;
  355. skt->socket.irq_mask = 0;
  356. skt->socket.map_size = MAP_SIZE;
  357. skt->socket.pci_irq = skt->irq;
  358. skt->socket.io_offset = (unsigned long)skt->virt_io;
  359. skt->status = au1x00_pcmcia_skt_state(skt);
  360. ret = pcmcia_register_socket(&skt->socket);
  361. if (ret)
  362. goto out_err;
  363. WARN_ON(skt->socket.sock != i);
  364. add_timer(&skt->poll_timer);
  365. }
  366. dev_set_drvdata(dev, sinfo);
  367. return 0;
  368. out_err:
  369. ops->hw_shutdown(skt);
  370. while (i-- > 0) {
  371. skt = PCMCIA_SOCKET(i);
  372. del_timer_sync(&skt->poll_timer);
  373. pcmcia_unregister_socket(&skt->socket);
  374. if (i == 0) {
  375. iounmap(skt->virt_io + (u32)mips_io_port_base);
  376. skt->virt_io = NULL;
  377. }
  378. #ifndef CONFIG_MIPS_XXS1500
  379. else {
  380. iounmap(skt->virt_io + (u32)mips_io_port_base);
  381. skt->virt_io = NULL;
  382. }
  383. #endif
  384. ops->hw_shutdown(skt);
  385. }
  386. kfree(sinfo);
  387. out:
  388. return ret;
  389. }
  390. int au1x00_drv_pcmcia_remove(struct platform_device *dev)
  391. {
  392. struct skt_dev_info *sinfo = platform_get_drvdata(dev);
  393. int i;
  394. mutex_lock(&pcmcia_sockets_lock);
  395. platform_set_drvdata(dev, NULL);
  396. for (i = 0; i < sinfo->nskt; i++) {
  397. struct au1000_pcmcia_socket *skt = PCMCIA_SOCKET(i);
  398. del_timer_sync(&skt->poll_timer);
  399. pcmcia_unregister_socket(&skt->socket);
  400. skt->ops->hw_shutdown(skt);
  401. au1x00_pcmcia_config_skt(skt, &dead_socket);
  402. iounmap(skt->virt_io + (u32)mips_io_port_base);
  403. skt->virt_io = NULL;
  404. }
  405. kfree(sinfo);
  406. mutex_unlock(&pcmcia_sockets_lock);
  407. return 0;
  408. }
  409. /*
  410. * PCMCIA "Driver" API
  411. */
  412. static int au1x00_drv_pcmcia_probe(struct platform_device *dev)
  413. {
  414. int i, ret = -ENODEV;
  415. mutex_lock(&pcmcia_sockets_lock);
  416. for (i=0; i < ARRAY_SIZE(au1x00_pcmcia_hw_init); i++) {
  417. ret = au1x00_pcmcia_hw_init[i](&dev->dev);
  418. if (ret == 0)
  419. break;
  420. }
  421. mutex_unlock(&pcmcia_sockets_lock);
  422. return ret;
  423. }
  424. static struct platform_driver au1x00_pcmcia_driver = {
  425. .driver = {
  426. .name = "au1x00-pcmcia",
  427. .owner = THIS_MODULE,
  428. },
  429. .probe = au1x00_drv_pcmcia_probe,
  430. .remove = au1x00_drv_pcmcia_remove,
  431. };
  432. /* au1x00_pcmcia_init()
  433. *
  434. * This routine performs low-level PCMCIA initialization and then
  435. * registers this socket driver with Card Services.
  436. *
  437. * Returns: 0 on success, -ve error code on failure
  438. */
  439. static int __init au1x00_pcmcia_init(void)
  440. {
  441. int error = 0;
  442. error = platform_driver_register(&au1x00_pcmcia_driver);
  443. return error;
  444. }
  445. /* au1x00_pcmcia_exit()
  446. * Invokes the low-level kernel service to free IRQs associated with this
  447. * socket controller and reset GPIO edge detection.
  448. */
  449. static void __exit au1x00_pcmcia_exit(void)
  450. {
  451. platform_driver_unregister(&au1x00_pcmcia_driver);
  452. }
  453. module_init(au1x00_pcmcia_init);
  454. module_exit(au1x00_pcmcia_exit);