pxa2xx_base.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*======================================================================
  2. Device driver for the PCMCIA control functionality of PXA2xx
  3. microprocessors.
  4. The contents of this file may be used under the
  5. terms of the GNU Public License version 2 (the "GPL")
  6. (c) Ian Molton (spyro@f2s.com) 2003
  7. (c) Stefan Eletzhofer (stefan.eletzhofer@inquant.de) 2003,4
  8. derived from sa11xx_base.c
  9. Portions created by John G. Dorsey are
  10. Copyright (C) 1999 John G. Dorsey.
  11. ======================================================================*/
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/init.h>
  15. #include <linux/cpufreq.h>
  16. #include <linux/ioport.h>
  17. #include <linux/kernel.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/platform_device.h>
  20. #include <mach/hardware.h>
  21. #include <asm/io.h>
  22. #include <asm/irq.h>
  23. #include <asm/system.h>
  24. #include <mach/pxa2xx-regs.h>
  25. #include <asm/mach-types.h>
  26. #include <pcmcia/cs_types.h>
  27. #include <pcmcia/ss.h>
  28. #include <pcmcia/cistpl.h>
  29. #include "soc_common.h"
  30. #include "pxa2xx_base.h"
  31. /*
  32. * Personal Computer Memory Card International Association (PCMCIA) sockets
  33. */
  34. #define PCMCIAPrtSp 0x04000000 /* PCMCIA Partition Space [byte] */
  35. #define PCMCIASp (4*PCMCIAPrtSp) /* PCMCIA Space [byte] */
  36. #define PCMCIAIOSp PCMCIAPrtSp /* PCMCIA I/O Space [byte] */
  37. #define PCMCIAAttrSp PCMCIAPrtSp /* PCMCIA Attribute Space [byte] */
  38. #define PCMCIAMemSp PCMCIAPrtSp /* PCMCIA Memory Space [byte] */
  39. #define PCMCIA0Sp PCMCIASp /* PCMCIA 0 Space [byte] */
  40. #define PCMCIA0IOSp PCMCIAIOSp /* PCMCIA 0 I/O Space [byte] */
  41. #define PCMCIA0AttrSp PCMCIAAttrSp /* PCMCIA 0 Attribute Space [byte] */
  42. #define PCMCIA0MemSp PCMCIAMemSp /* PCMCIA 0 Memory Space [byte] */
  43. #define PCMCIA1Sp PCMCIASp /* PCMCIA 1 Space [byte] */
  44. #define PCMCIA1IOSp PCMCIAIOSp /* PCMCIA 1 I/O Space [byte] */
  45. #define PCMCIA1AttrSp PCMCIAAttrSp /* PCMCIA 1 Attribute Space [byte] */
  46. #define PCMCIA1MemSp PCMCIAMemSp /* PCMCIA 1 Memory Space [byte] */
  47. #define _PCMCIA(Nb) /* PCMCIA [0..1] */ \
  48. (0x20000000 + (Nb) * PCMCIASp)
  49. #define _PCMCIAIO(Nb) _PCMCIA(Nb) /* PCMCIA I/O [0..1] */
  50. #define _PCMCIAAttr(Nb) /* PCMCIA Attribute [0..1] */ \
  51. (_PCMCIA(Nb) + 2 * PCMCIAPrtSp)
  52. #define _PCMCIAMem(Nb) /* PCMCIA Memory [0..1] */ \
  53. (_PCMCIA(Nb) + 3 * PCMCIAPrtSp)
  54. #define _PCMCIA0 _PCMCIA(0) /* PCMCIA 0 */
  55. #define _PCMCIA0IO _PCMCIAIO(0) /* PCMCIA 0 I/O */
  56. #define _PCMCIA0Attr _PCMCIAAttr(0) /* PCMCIA 0 Attribute */
  57. #define _PCMCIA0Mem _PCMCIAMem(0) /* PCMCIA 0 Memory */
  58. #define _PCMCIA1 _PCMCIA(1) /* PCMCIA 1 */
  59. #define _PCMCIA1IO _PCMCIAIO(1) /* PCMCIA 1 I/O */
  60. #define _PCMCIA1Attr _PCMCIAAttr(1) /* PCMCIA 1 Attribute */
  61. #define _PCMCIA1Mem _PCMCIAMem(1) /* PCMCIA 1 Memory */
  62. #define MCXX_SETUP_MASK (0x7f)
  63. #define MCXX_ASST_MASK (0x1f)
  64. #define MCXX_HOLD_MASK (0x3f)
  65. #define MCXX_SETUP_SHIFT (0)
  66. #define MCXX_ASST_SHIFT (7)
  67. #define MCXX_HOLD_SHIFT (14)
  68. static inline u_int pxa2xx_mcxx_hold(u_int pcmcia_cycle_ns,
  69. u_int mem_clk_10khz)
  70. {
  71. u_int code = pcmcia_cycle_ns * mem_clk_10khz;
  72. return (code / 300000) + ((code % 300000) ? 1 : 0) - 1;
  73. }
  74. static inline u_int pxa2xx_mcxx_asst(u_int pcmcia_cycle_ns,
  75. u_int mem_clk_10khz)
  76. {
  77. u_int code = pcmcia_cycle_ns * mem_clk_10khz;
  78. return (code / 300000) + ((code % 300000) ? 1 : 0) + 1;
  79. }
  80. static inline u_int pxa2xx_mcxx_setup(u_int pcmcia_cycle_ns,
  81. u_int mem_clk_10khz)
  82. {
  83. u_int code = pcmcia_cycle_ns * mem_clk_10khz;
  84. return (code / 100000) + ((code % 100000) ? 1 : 0) - 1;
  85. }
  86. /* This function returns the (approximate) command assertion period, in
  87. * nanoseconds, for a given CPU clock frequency and MCXX_ASST value:
  88. */
  89. static inline u_int pxa2xx_pcmcia_cmd_time(u_int mem_clk_10khz,
  90. u_int pcmcia_mcxx_asst)
  91. {
  92. return (300000 * (pcmcia_mcxx_asst + 1) / mem_clk_10khz);
  93. }
  94. static int pxa2xx_pcmcia_set_mcmem( int sock, int speed, int clock )
  95. {
  96. MCMEM(sock) = ((pxa2xx_mcxx_setup(speed, clock)
  97. & MCXX_SETUP_MASK) << MCXX_SETUP_SHIFT)
  98. | ((pxa2xx_mcxx_asst(speed, clock)
  99. & MCXX_ASST_MASK) << MCXX_ASST_SHIFT)
  100. | ((pxa2xx_mcxx_hold(speed, clock)
  101. & MCXX_HOLD_MASK) << MCXX_HOLD_SHIFT);
  102. return 0;
  103. }
  104. static int pxa2xx_pcmcia_set_mcio( int sock, int speed, int clock )
  105. {
  106. MCIO(sock) = ((pxa2xx_mcxx_setup(speed, clock)
  107. & MCXX_SETUP_MASK) << MCXX_SETUP_SHIFT)
  108. | ((pxa2xx_mcxx_asst(speed, clock)
  109. & MCXX_ASST_MASK) << MCXX_ASST_SHIFT)
  110. | ((pxa2xx_mcxx_hold(speed, clock)
  111. & MCXX_HOLD_MASK) << MCXX_HOLD_SHIFT);
  112. return 0;
  113. }
  114. static int pxa2xx_pcmcia_set_mcatt( int sock, int speed, int clock )
  115. {
  116. MCATT(sock) = ((pxa2xx_mcxx_setup(speed, clock)
  117. & MCXX_SETUP_MASK) << MCXX_SETUP_SHIFT)
  118. | ((pxa2xx_mcxx_asst(speed, clock)
  119. & MCXX_ASST_MASK) << MCXX_ASST_SHIFT)
  120. | ((pxa2xx_mcxx_hold(speed, clock)
  121. & MCXX_HOLD_MASK) << MCXX_HOLD_SHIFT);
  122. return 0;
  123. }
  124. static int pxa2xx_pcmcia_set_mcxx(struct soc_pcmcia_socket *skt, unsigned int clk)
  125. {
  126. struct soc_pcmcia_timing timing;
  127. int sock = skt->nr;
  128. soc_common_pcmcia_get_timing(skt, &timing);
  129. pxa2xx_pcmcia_set_mcmem(sock, timing.mem, clk);
  130. pxa2xx_pcmcia_set_mcatt(sock, timing.attr, clk);
  131. pxa2xx_pcmcia_set_mcio(sock, timing.io, clk);
  132. return 0;
  133. }
  134. static int pxa2xx_pcmcia_set_timing(struct soc_pcmcia_socket *skt)
  135. {
  136. unsigned int clk = get_memclk_frequency_10khz();
  137. return pxa2xx_pcmcia_set_mcxx(skt, clk);
  138. }
  139. #ifdef CONFIG_CPU_FREQ
  140. static int
  141. pxa2xx_pcmcia_frequency_change(struct soc_pcmcia_socket *skt,
  142. unsigned long val,
  143. struct cpufreq_freqs *freqs)
  144. {
  145. #warning "it's not clear if this is right since the core CPU (N) clock has no effect on the memory (L) clock"
  146. switch (val) {
  147. case CPUFREQ_PRECHANGE:
  148. if (freqs->new > freqs->old) {
  149. debug(skt, 2, "new frequency %u.%uMHz > %u.%uMHz, "
  150. "pre-updating\n",
  151. freqs->new / 1000, (freqs->new / 100) % 10,
  152. freqs->old / 1000, (freqs->old / 100) % 10);
  153. pxa2xx_pcmcia_set_mcxx(skt, freqs->new);
  154. }
  155. break;
  156. case CPUFREQ_POSTCHANGE:
  157. if (freqs->new < freqs->old) {
  158. debug(skt, 2, "new frequency %u.%uMHz < %u.%uMHz, "
  159. "post-updating\n",
  160. freqs->new / 1000, (freqs->new / 100) % 10,
  161. freqs->old / 1000, (freqs->old / 100) % 10);
  162. pxa2xx_pcmcia_set_mcxx(skt, freqs->new);
  163. }
  164. break;
  165. }
  166. return 0;
  167. }
  168. #endif
  169. static void pxa2xx_configure_sockets(struct device *dev)
  170. {
  171. struct pcmcia_low_level *ops = dev->platform_data;
  172. /*
  173. * We have at least one socket, so set MECR:CIT
  174. * (Card Is There)
  175. */
  176. MECR |= MECR_CIT;
  177. /* Set MECR:NOS (Number Of Sockets) */
  178. if ((ops->first + ops->nr) > 1 ||
  179. machine_is_viper() || machine_is_arcom_zeus())
  180. MECR |= MECR_NOS;
  181. else
  182. MECR &= ~MECR_NOS;
  183. }
  184. static const char *skt_names[] = {
  185. "PCMCIA socket 0",
  186. "PCMCIA socket 1",
  187. };
  188. #define SKT_DEV_INFO_SIZE(n) \
  189. (sizeof(struct skt_dev_info) + (n)*sizeof(struct soc_pcmcia_socket))
  190. int pxa2xx_drv_pcmcia_add_one(struct soc_pcmcia_socket *skt)
  191. {
  192. skt->res_skt.start = _PCMCIA(skt->nr);
  193. skt->res_skt.end = _PCMCIA(skt->nr) + PCMCIASp - 1;
  194. skt->res_skt.name = skt_names[skt->nr];
  195. skt->res_skt.flags = IORESOURCE_MEM;
  196. skt->res_io.start = _PCMCIAIO(skt->nr);
  197. skt->res_io.end = _PCMCIAIO(skt->nr) + PCMCIAIOSp - 1;
  198. skt->res_io.name = "io";
  199. skt->res_io.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  200. skt->res_mem.start = _PCMCIAMem(skt->nr);
  201. skt->res_mem.end = _PCMCIAMem(skt->nr) + PCMCIAMemSp - 1;
  202. skt->res_mem.name = "memory";
  203. skt->res_mem.flags = IORESOURCE_MEM;
  204. skt->res_attr.start = _PCMCIAAttr(skt->nr);
  205. skt->res_attr.end = _PCMCIAAttr(skt->nr) + PCMCIAAttrSp - 1;
  206. skt->res_attr.name = "attribute";
  207. skt->res_attr.flags = IORESOURCE_MEM;
  208. return soc_pcmcia_add_one(skt);
  209. }
  210. EXPORT_SYMBOL(pxa2xx_drv_pcmcia_add_one);
  211. void pxa2xx_drv_pcmcia_ops(struct pcmcia_low_level *ops)
  212. {
  213. /* Provide our PXA2xx specific timing routines. */
  214. ops->set_timing = pxa2xx_pcmcia_set_timing;
  215. #ifdef CONFIG_CPU_FREQ
  216. ops->frequency_change = pxa2xx_pcmcia_frequency_change;
  217. #endif
  218. }
  219. EXPORT_SYMBOL(pxa2xx_drv_pcmcia_ops);
  220. static int pxa2xx_drv_pcmcia_probe(struct platform_device *dev)
  221. {
  222. int i, ret = 0;
  223. struct pcmcia_low_level *ops;
  224. struct skt_dev_info *sinfo;
  225. struct soc_pcmcia_socket *skt;
  226. ops = (struct pcmcia_low_level *)dev->dev.platform_data;
  227. if (!ops)
  228. return -ENODEV;
  229. pxa2xx_drv_pcmcia_ops(ops);
  230. sinfo = kzalloc(SKT_DEV_INFO_SIZE(ops->nr), GFP_KERNEL);
  231. if (!sinfo)
  232. return -ENOMEM;
  233. sinfo->nskt = ops->nr;
  234. /* Initialize processor specific parameters */
  235. for (i = 0; i < ops->nr; i++) {
  236. skt = &sinfo->skt[i];
  237. skt->nr = ops->first + i;
  238. skt->ops = ops;
  239. skt->socket.owner = ops->owner;
  240. skt->socket.dev.parent = &dev->dev;
  241. skt->socket.pci_irq = NO_IRQ;
  242. ret = pxa2xx_drv_pcmcia_add_one(skt);
  243. if (ret)
  244. break;
  245. }
  246. if (ret) {
  247. while (--i >= 0)
  248. soc_pcmcia_remove_one(&sinfo->skt[i]);
  249. kfree(sinfo);
  250. } else {
  251. pxa2xx_configure_sockets(&dev->dev);
  252. dev_set_drvdata(&dev->dev, sinfo);
  253. }
  254. return ret;
  255. }
  256. static int pxa2xx_drv_pcmcia_remove(struct platform_device *dev)
  257. {
  258. struct skt_dev_info *sinfo = platform_get_drvdata(dev);
  259. int i;
  260. platform_set_drvdata(dev, NULL);
  261. for (i = 0; i < sinfo->nskt; i++)
  262. soc_pcmcia_remove_one(&sinfo->skt[i]);
  263. kfree(sinfo);
  264. return 0;
  265. }
  266. static int pxa2xx_drv_pcmcia_resume(struct device *dev)
  267. {
  268. pxa2xx_configure_sockets(dev);
  269. return 0;
  270. }
  271. static const struct dev_pm_ops pxa2xx_drv_pcmcia_pm_ops = {
  272. .resume = pxa2xx_drv_pcmcia_resume,
  273. };
  274. static struct platform_driver pxa2xx_pcmcia_driver = {
  275. .probe = pxa2xx_drv_pcmcia_probe,
  276. .remove = pxa2xx_drv_pcmcia_remove,
  277. .driver = {
  278. .name = "pxa2xx-pcmcia",
  279. .owner = THIS_MODULE,
  280. .pm = &pxa2xx_drv_pcmcia_pm_ops,
  281. },
  282. };
  283. static int __init pxa2xx_pcmcia_init(void)
  284. {
  285. return platform_driver_register(&pxa2xx_pcmcia_driver);
  286. }
  287. static void __exit pxa2xx_pcmcia_exit(void)
  288. {
  289. platform_driver_unregister(&pxa2xx_pcmcia_driver);
  290. }
  291. fs_initcall(pxa2xx_pcmcia_init);
  292. module_exit(pxa2xx_pcmcia_exit);
  293. MODULE_AUTHOR("Stefan Eletzhofer <stefan.eletzhofer@inquant.de> and Ian Molton <spyro@f2s.com>");
  294. MODULE_DESCRIPTION("Linux PCMCIA Card Services: PXA2xx core socket driver");
  295. MODULE_LICENSE("GPL");
  296. MODULE_ALIAS("platform:pxa2xx-pcmcia");