au1000_generic.c 14 KB

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