pxa2xx_base.c 9.7 KB

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