hsmc.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Static Memory Controller for AT32 chips
  3. *
  4. * Copyright (C) 2006 Atmel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/err.h>
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <asm/io.h>
  16. #include <mach/smc.h>
  17. #include "hsmc.h"
  18. #define NR_CHIP_SELECTS 6
  19. struct hsmc {
  20. void __iomem *regs;
  21. struct clk *pclk;
  22. struct clk *mck;
  23. };
  24. static struct hsmc *hsmc;
  25. void smc_set_timing(struct smc_config *config,
  26. const struct smc_timing *timing)
  27. {
  28. int recover;
  29. int cycle;
  30. unsigned long mul;
  31. /* Reset all SMC timings */
  32. config->ncs_read_setup = 0;
  33. config->nrd_setup = 0;
  34. config->ncs_write_setup = 0;
  35. config->nwe_setup = 0;
  36. config->ncs_read_pulse = 0;
  37. config->nrd_pulse = 0;
  38. config->ncs_write_pulse = 0;
  39. config->nwe_pulse = 0;
  40. config->read_cycle = 0;
  41. config->write_cycle = 0;
  42. /*
  43. * cycles = x / T = x * f
  44. * = ((x * 1000000000) * ((f * 65536) / 1000000000)) / 65536
  45. * = ((x * 1000000000) * (((f / 10000) * 65536) / 100000)) / 65536
  46. */
  47. mul = (clk_get_rate(hsmc->mck) / 10000) << 16;
  48. mul /= 100000;
  49. #define ns2cyc(x) ((((x) * mul) + 65535) >> 16)
  50. if (timing->ncs_read_setup > 0)
  51. config->ncs_read_setup = ns2cyc(timing->ncs_read_setup);
  52. if (timing->nrd_setup > 0)
  53. config->nrd_setup = ns2cyc(timing->nrd_setup);
  54. if (timing->ncs_write_setup > 0)
  55. config->ncs_write_setup = ns2cyc(timing->ncs_write_setup);
  56. if (timing->nwe_setup > 0)
  57. config->nwe_setup = ns2cyc(timing->nwe_setup);
  58. if (timing->ncs_read_pulse > 0)
  59. config->ncs_read_pulse = ns2cyc(timing->ncs_read_pulse);
  60. if (timing->nrd_pulse > 0)
  61. config->nrd_pulse = ns2cyc(timing->nrd_pulse);
  62. if (timing->ncs_write_pulse > 0)
  63. config->ncs_write_pulse = ns2cyc(timing->ncs_write_pulse);
  64. if (timing->nwe_pulse > 0)
  65. config->nwe_pulse = ns2cyc(timing->nwe_pulse);
  66. if (timing->read_cycle > 0)
  67. config->read_cycle = ns2cyc(timing->read_cycle);
  68. if (timing->write_cycle > 0)
  69. config->write_cycle = ns2cyc(timing->write_cycle);
  70. /* Extend read cycle in needed */
  71. if (timing->ncs_read_recover > 0)
  72. recover = ns2cyc(timing->ncs_read_recover);
  73. else
  74. recover = 1;
  75. cycle = config->ncs_read_setup + config->ncs_read_pulse + recover;
  76. if (config->read_cycle < cycle)
  77. config->read_cycle = cycle;
  78. /* Extend read cycle in needed */
  79. if (timing->nrd_recover > 0)
  80. recover = ns2cyc(timing->nrd_recover);
  81. else
  82. recover = 1;
  83. cycle = config->nrd_setup + config->nrd_pulse + recover;
  84. if (config->read_cycle < cycle)
  85. config->read_cycle = cycle;
  86. /* Extend write cycle in needed */
  87. if (timing->ncs_write_recover > 0)
  88. recover = ns2cyc(timing->ncs_write_recover);
  89. else
  90. recover = 1;
  91. cycle = config->ncs_write_setup + config->ncs_write_pulse + recover;
  92. if (config->write_cycle < cycle)
  93. config->write_cycle = cycle;
  94. /* Extend write cycle in needed */
  95. if (timing->nwe_recover > 0)
  96. recover = ns2cyc(timing->nwe_recover);
  97. else
  98. recover = 1;
  99. cycle = config->nwe_setup + config->nwe_pulse + recover;
  100. if (config->write_cycle < cycle)
  101. config->write_cycle = cycle;
  102. }
  103. EXPORT_SYMBOL(smc_set_timing);
  104. int smc_set_configuration(int cs, const struct smc_config *config)
  105. {
  106. unsigned long offset;
  107. u32 setup, pulse, cycle, mode;
  108. if (!hsmc)
  109. return -ENODEV;
  110. if (cs >= NR_CHIP_SELECTS)
  111. return -EINVAL;
  112. setup = (HSMC_BF(NWE_SETUP, config->nwe_setup)
  113. | HSMC_BF(NCS_WR_SETUP, config->ncs_write_setup)
  114. | HSMC_BF(NRD_SETUP, config->nrd_setup)
  115. | HSMC_BF(NCS_RD_SETUP, config->ncs_read_setup));
  116. pulse = (HSMC_BF(NWE_PULSE, config->nwe_pulse)
  117. | HSMC_BF(NCS_WR_PULSE, config->ncs_write_pulse)
  118. | HSMC_BF(NRD_PULSE, config->nrd_pulse)
  119. | HSMC_BF(NCS_RD_PULSE, config->ncs_read_pulse));
  120. cycle = (HSMC_BF(NWE_CYCLE, config->write_cycle)
  121. | HSMC_BF(NRD_CYCLE, config->read_cycle));
  122. switch (config->bus_width) {
  123. case 1:
  124. mode = HSMC_BF(DBW, HSMC_DBW_8_BITS);
  125. break;
  126. case 2:
  127. mode = HSMC_BF(DBW, HSMC_DBW_16_BITS);
  128. break;
  129. case 4:
  130. mode = HSMC_BF(DBW, HSMC_DBW_32_BITS);
  131. break;
  132. default:
  133. return -EINVAL;
  134. }
  135. switch (config->nwait_mode) {
  136. case 0:
  137. mode |= HSMC_BF(EXNW_MODE, HSMC_EXNW_MODE_DISABLED);
  138. break;
  139. case 1:
  140. mode |= HSMC_BF(EXNW_MODE, HSMC_EXNW_MODE_RESERVED);
  141. break;
  142. case 2:
  143. mode |= HSMC_BF(EXNW_MODE, HSMC_EXNW_MODE_FROZEN);
  144. break;
  145. case 3:
  146. mode |= HSMC_BF(EXNW_MODE, HSMC_EXNW_MODE_READY);
  147. break;
  148. default:
  149. return -EINVAL;
  150. }
  151. if (config->tdf_cycles) {
  152. mode |= HSMC_BF(TDF_CYCLES, config->tdf_cycles);
  153. }
  154. if (config->nrd_controlled)
  155. mode |= HSMC_BIT(READ_MODE);
  156. if (config->nwe_controlled)
  157. mode |= HSMC_BIT(WRITE_MODE);
  158. if (config->byte_write)
  159. mode |= HSMC_BIT(BAT);
  160. if (config->tdf_mode)
  161. mode |= HSMC_BIT(TDF_MODE);
  162. pr_debug("smc cs%d: setup/%08x pulse/%08x cycle/%08x mode/%08x\n",
  163. cs, setup, pulse, cycle, mode);
  164. offset = cs * 0x10;
  165. hsmc_writel(hsmc, SETUP0 + offset, setup);
  166. hsmc_writel(hsmc, PULSE0 + offset, pulse);
  167. hsmc_writel(hsmc, CYCLE0 + offset, cycle);
  168. hsmc_writel(hsmc, MODE0 + offset, mode);
  169. hsmc_readl(hsmc, MODE0); /* I/O barrier */
  170. return 0;
  171. }
  172. EXPORT_SYMBOL(smc_set_configuration);
  173. static int hsmc_probe(struct platform_device *pdev)
  174. {
  175. struct resource *regs;
  176. struct clk *pclk, *mck;
  177. int ret;
  178. if (hsmc)
  179. return -EBUSY;
  180. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  181. if (!regs)
  182. return -ENXIO;
  183. pclk = clk_get(&pdev->dev, "pclk");
  184. if (IS_ERR(pclk))
  185. return PTR_ERR(pclk);
  186. mck = clk_get(&pdev->dev, "mck");
  187. if (IS_ERR(mck)) {
  188. ret = PTR_ERR(mck);
  189. goto out_put_pclk;
  190. }
  191. ret = -ENOMEM;
  192. hsmc = kzalloc(sizeof(struct hsmc), GFP_KERNEL);
  193. if (!hsmc)
  194. goto out_put_clocks;
  195. clk_enable(pclk);
  196. clk_enable(mck);
  197. hsmc->pclk = pclk;
  198. hsmc->mck = mck;
  199. hsmc->regs = ioremap(regs->start, regs->end - regs->start + 1);
  200. if (!hsmc->regs)
  201. goto out_disable_clocks;
  202. dev_info(&pdev->dev, "Atmel Static Memory Controller at 0x%08lx\n",
  203. (unsigned long)regs->start);
  204. platform_set_drvdata(pdev, hsmc);
  205. return 0;
  206. out_disable_clocks:
  207. clk_disable(mck);
  208. clk_disable(pclk);
  209. kfree(hsmc);
  210. out_put_clocks:
  211. clk_put(mck);
  212. out_put_pclk:
  213. clk_put(pclk);
  214. hsmc = NULL;
  215. return ret;
  216. }
  217. static struct platform_driver hsmc_driver = {
  218. .probe = hsmc_probe,
  219. .driver = {
  220. .name = "smc",
  221. },
  222. };
  223. static int __init hsmc_init(void)
  224. {
  225. return platform_driver_register(&hsmc_driver);
  226. }
  227. core_initcall(hsmc_init);