pxa2xx_base.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 <mach/smemc.h>
  22. #include <asm/io.h>
  23. #include <asm/irq.h>
  24. #include <mach/pxa2xx-regs.h>
  25. #include <asm/mach-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. uint32_t val;
  96. val = ((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. __raw_writel(val, MCMEM(sock));
  103. return 0;
  104. }
  105. static int pxa2xx_pcmcia_set_mcio( int sock, int speed, int clock )
  106. {
  107. uint32_t val;
  108. val = ((pxa2xx_mcxx_setup(speed, clock)
  109. & MCXX_SETUP_MASK) << MCXX_SETUP_SHIFT)
  110. | ((pxa2xx_mcxx_asst(speed, clock)
  111. & MCXX_ASST_MASK) << MCXX_ASST_SHIFT)
  112. | ((pxa2xx_mcxx_hold(speed, clock)
  113. & MCXX_HOLD_MASK) << MCXX_HOLD_SHIFT);
  114. __raw_writel(val, MCIO(sock));
  115. return 0;
  116. }
  117. static int pxa2xx_pcmcia_set_mcatt( int sock, int speed, int clock )
  118. {
  119. uint32_t val;
  120. val = ((pxa2xx_mcxx_setup(speed, clock)
  121. & MCXX_SETUP_MASK) << MCXX_SETUP_SHIFT)
  122. | ((pxa2xx_mcxx_asst(speed, clock)
  123. & MCXX_ASST_MASK) << MCXX_ASST_SHIFT)
  124. | ((pxa2xx_mcxx_hold(speed, clock)
  125. & MCXX_HOLD_MASK) << MCXX_HOLD_SHIFT);
  126. __raw_writel(val, MCATT(sock));
  127. return 0;
  128. }
  129. static int pxa2xx_pcmcia_set_mcxx(struct soc_pcmcia_socket *skt, unsigned int clk)
  130. {
  131. struct soc_pcmcia_timing timing;
  132. int sock = skt->nr;
  133. soc_common_pcmcia_get_timing(skt, &timing);
  134. pxa2xx_pcmcia_set_mcmem(sock, timing.mem, clk);
  135. pxa2xx_pcmcia_set_mcatt(sock, timing.attr, clk);
  136. pxa2xx_pcmcia_set_mcio(sock, timing.io, clk);
  137. return 0;
  138. }
  139. static int pxa2xx_pcmcia_set_timing(struct soc_pcmcia_socket *skt)
  140. {
  141. unsigned long clk = clk_get_rate(skt->clk);
  142. return pxa2xx_pcmcia_set_mcxx(skt, clk / 10000);
  143. }
  144. #ifdef CONFIG_CPU_FREQ
  145. static int
  146. pxa2xx_pcmcia_frequency_change(struct soc_pcmcia_socket *skt,
  147. unsigned long val,
  148. struct cpufreq_freqs *freqs)
  149. {
  150. switch (val) {
  151. case CPUFREQ_PRECHANGE:
  152. if (freqs->new > freqs->old) {
  153. debug(skt, 2, "new frequency %u.%uMHz > %u.%uMHz, "
  154. "pre-updating\n",
  155. freqs->new / 1000, (freqs->new / 100) % 10,
  156. freqs->old / 1000, (freqs->old / 100) % 10);
  157. pxa2xx_pcmcia_set_timing(skt);
  158. }
  159. break;
  160. case CPUFREQ_POSTCHANGE:
  161. if (freqs->new < freqs->old) {
  162. debug(skt, 2, "new frequency %u.%uMHz < %u.%uMHz, "
  163. "post-updating\n",
  164. freqs->new / 1000, (freqs->new / 100) % 10,
  165. freqs->old / 1000, (freqs->old / 100) % 10);
  166. pxa2xx_pcmcia_set_timing(skt);
  167. }
  168. break;
  169. }
  170. return 0;
  171. }
  172. #endif
  173. void pxa2xx_configure_sockets(struct device *dev)
  174. {
  175. struct pcmcia_low_level *ops = dev->platform_data;
  176. /*
  177. * We have at least one socket, so set MECR:CIT
  178. * (Card Is There)
  179. */
  180. uint32_t mecr = MECR_CIT;
  181. /* Set MECR:NOS (Number Of Sockets) */
  182. if ((ops->first + ops->nr) > 1 ||
  183. machine_is_viper() || machine_is_arcom_zeus())
  184. mecr |= MECR_NOS;
  185. __raw_writel(mecr, MECR);
  186. }
  187. EXPORT_SYMBOL(pxa2xx_configure_sockets);
  188. static const char *skt_names[] = {
  189. "PCMCIA socket 0",
  190. "PCMCIA socket 1",
  191. };
  192. #define SKT_DEV_INFO_SIZE(n) \
  193. (sizeof(struct skt_dev_info) + (n)*sizeof(struct soc_pcmcia_socket))
  194. int pxa2xx_drv_pcmcia_add_one(struct soc_pcmcia_socket *skt)
  195. {
  196. skt->res_skt.start = _PCMCIA(skt->nr);
  197. skt->res_skt.end = _PCMCIA(skt->nr) + PCMCIASp - 1;
  198. skt->res_skt.name = skt_names[skt->nr];
  199. skt->res_skt.flags = IORESOURCE_MEM;
  200. skt->res_io.start = _PCMCIAIO(skt->nr);
  201. skt->res_io.end = _PCMCIAIO(skt->nr) + PCMCIAIOSp - 1;
  202. skt->res_io.name = "io";
  203. skt->res_io.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  204. skt->res_mem.start = _PCMCIAMem(skt->nr);
  205. skt->res_mem.end = _PCMCIAMem(skt->nr) + PCMCIAMemSp - 1;
  206. skt->res_mem.name = "memory";
  207. skt->res_mem.flags = IORESOURCE_MEM;
  208. skt->res_attr.start = _PCMCIAAttr(skt->nr);
  209. skt->res_attr.end = _PCMCIAAttr(skt->nr) + PCMCIAAttrSp - 1;
  210. skt->res_attr.name = "attribute";
  211. skt->res_attr.flags = IORESOURCE_MEM;
  212. return soc_pcmcia_add_one(skt);
  213. }
  214. EXPORT_SYMBOL(pxa2xx_drv_pcmcia_add_one);
  215. void pxa2xx_drv_pcmcia_ops(struct pcmcia_low_level *ops)
  216. {
  217. /* Provide our PXA2xx specific timing routines. */
  218. ops->set_timing = pxa2xx_pcmcia_set_timing;
  219. #ifdef CONFIG_CPU_FREQ
  220. ops->frequency_change = pxa2xx_pcmcia_frequency_change;
  221. #endif
  222. }
  223. EXPORT_SYMBOL(pxa2xx_drv_pcmcia_ops);
  224. static int pxa2xx_drv_pcmcia_probe(struct platform_device *dev)
  225. {
  226. int i, ret = 0;
  227. struct pcmcia_low_level *ops;
  228. struct skt_dev_info *sinfo;
  229. struct soc_pcmcia_socket *skt;
  230. struct clk *clk;
  231. ops = (struct pcmcia_low_level *)dev->dev.platform_data;
  232. if (!ops) {
  233. ret = -ENODEV;
  234. goto err0;
  235. }
  236. if (cpu_is_pxa320() && ops->nr > 1) {
  237. dev_err(&dev->dev, "pxa320 supports only one pcmcia slot");
  238. ret = -EINVAL;
  239. goto err0;
  240. }
  241. clk = devm_clk_get(&dev->dev, NULL);
  242. if (IS_ERR(clk))
  243. return -ENODEV;
  244. pxa2xx_drv_pcmcia_ops(ops);
  245. sinfo = devm_kzalloc(&dev->dev, SKT_DEV_INFO_SIZE(ops->nr),
  246. GFP_KERNEL);
  247. if (!sinfo)
  248. return -ENOMEM;
  249. sinfo->nskt = ops->nr;
  250. /* Initialize processor specific parameters */
  251. for (i = 0; i < ops->nr; i++) {
  252. skt = &sinfo->skt[i];
  253. skt->nr = ops->first + i;
  254. skt->clk = clk;
  255. soc_pcmcia_init_one(skt, ops, &dev->dev);
  256. ret = pxa2xx_drv_pcmcia_add_one(skt);
  257. if (ret)
  258. goto err1;
  259. }
  260. pxa2xx_configure_sockets(&dev->dev);
  261. dev_set_drvdata(&dev->dev, sinfo);
  262. return 0;
  263. err1:
  264. while (--i >= 0)
  265. soc_pcmcia_remove_one(&sinfo->skt[i]);
  266. err0:
  267. return ret;
  268. }
  269. static int pxa2xx_drv_pcmcia_remove(struct platform_device *dev)
  270. {
  271. struct skt_dev_info *sinfo = platform_get_drvdata(dev);
  272. int i;
  273. for (i = 0; i < sinfo->nskt; i++)
  274. soc_pcmcia_remove_one(&sinfo->skt[i]);
  275. return 0;
  276. }
  277. static int pxa2xx_drv_pcmcia_resume(struct device *dev)
  278. {
  279. pxa2xx_configure_sockets(dev);
  280. return 0;
  281. }
  282. static const struct dev_pm_ops pxa2xx_drv_pcmcia_pm_ops = {
  283. .resume = pxa2xx_drv_pcmcia_resume,
  284. };
  285. static struct platform_driver pxa2xx_pcmcia_driver = {
  286. .probe = pxa2xx_drv_pcmcia_probe,
  287. .remove = pxa2xx_drv_pcmcia_remove,
  288. .driver = {
  289. .name = "pxa2xx-pcmcia",
  290. .pm = &pxa2xx_drv_pcmcia_pm_ops,
  291. },
  292. };
  293. static int __init pxa2xx_pcmcia_init(void)
  294. {
  295. return platform_driver_register(&pxa2xx_pcmcia_driver);
  296. }
  297. static void __exit pxa2xx_pcmcia_exit(void)
  298. {
  299. platform_driver_unregister(&pxa2xx_pcmcia_driver);
  300. }
  301. fs_initcall(pxa2xx_pcmcia_init);
  302. module_exit(pxa2xx_pcmcia_exit);
  303. MODULE_AUTHOR("Stefan Eletzhofer <stefan.eletzhofer@inquant.de> and Ian Molton <spyro@f2s.com>");
  304. MODULE_DESCRIPTION("Linux PCMCIA Card Services: PXA2xx core socket driver");
  305. MODULE_LICENSE("GPL");
  306. MODULE_ALIAS("platform:pxa2xx-pcmcia");