s3c2412-iotiming.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /* linux/arch/arm/plat-s3c24xx/s3c2412-iotiming.c
  2. *
  3. * Copyright (c) 2006,2008 Simtec Electronics
  4. * http://armlinux.simtec.co.uk/
  5. * Ben Dooks <ben@simtec.co.uk>
  6. *
  7. * S3C2412/S3C2443 (PL093 based) IO timing support
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/init.h>
  14. #include <linux/module.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/ioport.h>
  17. #include <linux/cpufreq.h>
  18. #include <linux/seq_file.h>
  19. #include <linux/sysdev.h>
  20. #include <linux/delay.h>
  21. #include <linux/clk.h>
  22. #include <linux/err.h>
  23. #include <linux/amba/pl093.h>
  24. #include <asm/mach/arch.h>
  25. #include <asm/mach/map.h>
  26. #include <mach/regs-s3c2412-mem.h>
  27. #include <plat/cpu.h>
  28. #include <plat/cpu-freq-core.h>
  29. #include <plat/clock.h>
  30. #define print_ns(x) ((x) / 10), ((x) % 10)
  31. /**
  32. * s3c2412_print_timing - print timing infromation via printk.
  33. * @pfx: The prefix to print each line with.
  34. * @iot: The IO timing information
  35. */
  36. static void s3c2412_print_timing(const char *pfx, struct s3c_iotimings *iot)
  37. {
  38. struct s3c2412_iobank_timing *bt;
  39. unsigned int bank;
  40. for (bank = 0; bank < MAX_BANKS; bank++) {
  41. bt = iot->bank[bank].io_2412;
  42. if (!bt)
  43. continue;
  44. printk(KERN_DEBUG "%s: %d: idcy=%d.%d wstrd=%d.%d wstwr=%d,%d"
  45. "wstoen=%d.%d wstwen=%d.%d wstbrd=%d.%d\n", pfx, bank,
  46. print_ns(bt->idcy),
  47. print_ns(bt->wstrd),
  48. print_ns(bt->wstwr),
  49. print_ns(bt->wstoen),
  50. print_ns(bt->wstwen),
  51. print_ns(bt->wstbrd));
  52. }
  53. }
  54. /**
  55. * to_div - turn a cycle length into a divisor setting.
  56. * @cyc_tns: The cycle time in 10ths of nanoseconds.
  57. * @clk_tns: The clock period in 10ths of nanoseconds.
  58. */
  59. static inline unsigned int to_div(unsigned int cyc_tns, unsigned int clk_tns)
  60. {
  61. return cyc_tns ? DIV_ROUND_UP(cyc_tns, clk_tns) : 0;
  62. }
  63. /**
  64. * calc_timing - calculate timing divisor value and check in range.
  65. * @hwtm: The hardware timing in 10ths of nanoseconds.
  66. * @clk_tns: The clock period in 10ths of nanoseconds.
  67. * @err: Pointer to err variable to update in event of failure.
  68. */
  69. static unsigned int calc_timing(unsigned int hwtm, unsigned int clk_tns,
  70. unsigned int *err)
  71. {
  72. unsigned int ret = to_div(hwtm, clk_tns);
  73. if (ret > 0xf)
  74. *err = -EINVAL;
  75. return ret;
  76. }
  77. /**
  78. * s3c2412_calc_bank - calculate the bank divisor settings.
  79. * @cfg: The current frequency configuration.
  80. * @bt: The bank timing.
  81. */
  82. static int s3c2412_calc_bank(struct s3c_cpufreq_config *cfg,
  83. struct s3c2412_iobank_timing *bt)
  84. {
  85. unsigned int hclk = cfg->freq.hclk_tns;
  86. int err = 0;
  87. bt->smbidcyr = calc_timing(bt->idcy, hclk, &err);
  88. bt->smbwstrd = calc_timing(bt->wstrd, hclk, &err);
  89. bt->smbwstwr = calc_timing(bt->wstwr, hclk, &err);
  90. bt->smbwstoen = calc_timing(bt->wstoen, hclk, &err);
  91. bt->smbwstwen = calc_timing(bt->wstwen, hclk, &err);
  92. bt->smbwstbrd = calc_timing(bt->wstbrd, hclk, &err);
  93. return err;
  94. }
  95. /**
  96. * s3c2412_iotiming_debugfs - debugfs show io bank timing information
  97. * @seq: The seq_file to write output to using seq_printf().
  98. * @cfg: The current configuration.
  99. * @iob: The IO bank information to decode.
  100. */
  101. void s3c2412_iotiming_debugfs(struct seq_file *seq,
  102. struct s3c_cpufreq_config *cfg,
  103. union s3c_iobank *iob)
  104. {
  105. struct s3c2412_iobank_timing *bt = iob->io_2412;
  106. seq_printf(seq,
  107. "\tRead: idcy=%d.%d wstrd=%d.%d wstwr=%d,%d"
  108. "wstoen=%d.%d wstwen=%d.%d wstbrd=%d.%d\n",
  109. print_ns(bt->idcy),
  110. print_ns(bt->wstrd),
  111. print_ns(bt->wstwr),
  112. print_ns(bt->wstoen),
  113. print_ns(bt->wstwen),
  114. print_ns(bt->wstbrd));
  115. }
  116. /**
  117. * s3c2412_iotiming_calc - calculate all the bank divisor settings.
  118. * @cfg: The current frequency configuration.
  119. * @iot: The bank timing information.
  120. *
  121. * Calculate the timing information for all the banks that are
  122. * configured as IO, using s3c2412_calc_bank().
  123. */
  124. int s3c2412_iotiming_calc(struct s3c_cpufreq_config *cfg,
  125. struct s3c_iotimings *iot)
  126. {
  127. struct s3c2412_iobank_timing *bt;
  128. int bank;
  129. int ret;
  130. for (bank = 0; bank < MAX_BANKS; bank++) {
  131. bt = iot->bank[bank].io_2412;
  132. if (!bt)
  133. continue;
  134. ret = s3c2412_calc_bank(cfg, bt);
  135. if (ret) {
  136. printk(KERN_ERR "%s: cannot calculate bank %d io\n",
  137. __func__, bank);
  138. goto err;
  139. }
  140. }
  141. return 0;
  142. err:
  143. return ret;
  144. }
  145. /**
  146. * s3c2412_iotiming_set - set the timing information
  147. * @cfg: The current frequency configuration.
  148. * @iot: The bank timing information.
  149. *
  150. * Set the IO bank information from the details calculated earlier from
  151. * calling s3c2412_iotiming_calc().
  152. */
  153. void s3c2412_iotiming_set(struct s3c_cpufreq_config *cfg,
  154. struct s3c_iotimings *iot)
  155. {
  156. struct s3c2412_iobank_timing *bt;
  157. void __iomem *regs;
  158. int bank;
  159. /* set the io timings from the specifier */
  160. for (bank = 0; bank < MAX_BANKS; bank++) {
  161. bt = iot->bank[bank].io_2412;
  162. if (!bt)
  163. continue;
  164. regs = S3C2412_SSMC_BANK(bank);
  165. __raw_writel(bt->smbidcyr, regs + SMBIDCYR);
  166. __raw_writel(bt->smbwstrd, regs + SMBWSTRDR);
  167. __raw_writel(bt->smbwstwr, regs + SMBWSTWRR);
  168. __raw_writel(bt->smbwstoen, regs + SMBWSTOENR);
  169. __raw_writel(bt->smbwstwen, regs + SMBWSTWENR);
  170. __raw_writel(bt->smbwstbrd, regs + SMBWSTBRDR);
  171. }
  172. }
  173. static inline unsigned int s3c2412_decode_timing(unsigned int clock, u32 reg)
  174. {
  175. return (reg & 0xf) * clock;
  176. }
  177. static void s3c2412_iotiming_getbank(struct s3c_cpufreq_config *cfg,
  178. struct s3c2412_iobank_timing *bt,
  179. unsigned int bank)
  180. {
  181. unsigned long clk = cfg->freq.hclk_tns; /* ssmc clock??? */
  182. void __iomem *regs = S3C2412_SSMC_BANK(bank);
  183. bt->idcy = s3c2412_decode_timing(clk, __raw_readl(regs + SMBIDCYR));
  184. bt->wstrd = s3c2412_decode_timing(clk, __raw_readl(regs + SMBWSTRDR));
  185. bt->wstoen = s3c2412_decode_timing(clk, __raw_readl(regs + SMBWSTOENR));
  186. bt->wstwen = s3c2412_decode_timing(clk, __raw_readl(regs + SMBWSTWENR));
  187. bt->wstbrd = s3c2412_decode_timing(clk, __raw_readl(regs + SMBWSTBRDR));
  188. }
  189. /**
  190. * bank_is_io - return true if bank is (possibly) IO.
  191. * @bank: The bank number.
  192. * @bankcfg: The value of S3C2412_EBI_BANKCFG.
  193. */
  194. static inline bool bank_is_io(unsigned int bank, u32 bankcfg)
  195. {
  196. if (bank < 2)
  197. return true;
  198. return !(bankcfg & (1 << bank));
  199. }
  200. int s3c2412_iotiming_get(struct s3c_cpufreq_config *cfg,
  201. struct s3c_iotimings *timings)
  202. {
  203. struct s3c2412_iobank_timing *bt;
  204. u32 bankcfg = __raw_readl(S3C2412_EBI_BANKCFG);
  205. unsigned int bank;
  206. /* look through all banks to see what is currently set. */
  207. for (bank = 0; bank < MAX_BANKS; bank++) {
  208. if (!bank_is_io(bank, bankcfg))
  209. continue;
  210. bt = kzalloc(sizeof(struct s3c2412_iobank_timing), GFP_KERNEL);
  211. if (!bt) {
  212. printk(KERN_ERR "%s: no memory for bank\n", __func__);
  213. return -ENOMEM;
  214. }
  215. timings->bank[bank].io_2412 = bt;
  216. s3c2412_iotiming_getbank(cfg, bt, bank);
  217. }
  218. s3c2412_print_timing("get", timings);
  219. return 0;
  220. }
  221. /* this is in here as it is so small, it doesn't currently warrant a file
  222. * to itself. We expect that any s3c24xx needing this is going to also
  223. * need the iotiming support.
  224. */
  225. void s3c2412_cpufreq_setrefresh(struct s3c_cpufreq_config *cfg)
  226. {
  227. struct s3c_cpufreq_board *board = cfg->board;
  228. u32 refresh;
  229. WARN_ON(board == NULL);
  230. /* Reduce both the refresh time (in ns) and the frequency (in MHz)
  231. * down to ensure that we do not overflow 32 bit numbers.
  232. *
  233. * This should work for HCLK up to 133MHz and refresh period up
  234. * to 30usec.
  235. */
  236. refresh = (cfg->freq.hclk / 100) * (board->refresh / 10);
  237. refresh = DIV_ROUND_UP(refresh, (1000 * 1000)); /* apply scale */
  238. refresh &= ((1 << 16) - 1);
  239. s3c_freq_dbg("%s: refresh value %u\n", __func__, (unsigned int)refresh);
  240. __raw_writel(refresh, S3C2412_REFRESH);
  241. }