au1000_generic.c 14 KB

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