hsmc.c 6.3 KB

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