iotiming-s3c2410.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*
  2. * Copyright (c) 2006-2009 Simtec Electronics
  3. * http://armlinux.simtec.co.uk/
  4. * Ben Dooks <ben@simtec.co.uk>
  5. *
  6. * S3C24XX CPU Frequency scaling - IO timing for S3C2410/S3C2440/S3C2442
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/errno.h>
  15. #include <linux/cpufreq.h>
  16. #include <linux/seq_file.h>
  17. #include <linux/io.h>
  18. #include <linux/slab.h>
  19. #include <mach/map.h>
  20. #include <mach/regs-clock.h>
  21. #include <plat/cpu-freq-core.h>
  22. #include "regs-mem.h"
  23. #define print_ns(x) ((x) / 10), ((x) % 10)
  24. /**
  25. * s3c2410_print_timing - print bank timing data for debug purposes
  26. * @pfx: The prefix to put on the output
  27. * @timings: The timing inforamtion to print.
  28. */
  29. static void s3c2410_print_timing(const char *pfx,
  30. struct s3c_iotimings *timings)
  31. {
  32. struct s3c2410_iobank_timing *bt;
  33. int bank;
  34. for (bank = 0; bank < MAX_BANKS; bank++) {
  35. bt = timings->bank[bank].io_2410;
  36. if (!bt)
  37. continue;
  38. printk(KERN_DEBUG "%s %d: Tacs=%d.%d, Tcos=%d.%d, Tacc=%d.%d, "
  39. "Tcoh=%d.%d, Tcah=%d.%d\n", pfx, bank,
  40. print_ns(bt->tacs),
  41. print_ns(bt->tcos),
  42. print_ns(bt->tacc),
  43. print_ns(bt->tcoh),
  44. print_ns(bt->tcah));
  45. }
  46. }
  47. /**
  48. * bank_reg - convert bank number to pointer to the control register.
  49. * @bank: The IO bank number.
  50. */
  51. static inline void __iomem *bank_reg(unsigned int bank)
  52. {
  53. return S3C2410_BANKCON0 + (bank << 2);
  54. }
  55. /**
  56. * bank_is_io - test whether bank is used for IO
  57. * @bankcon: The bank control register.
  58. *
  59. * This is a simplistic test to see if any BANKCON[x] is not an IO
  60. * bank. It currently does not take into account whether BWSCON has
  61. * an illegal width-setting in it, or if the pin connected to nCS[x]
  62. * is actually being handled as a chip-select.
  63. */
  64. static inline int bank_is_io(unsigned long bankcon)
  65. {
  66. return !(bankcon & S3C2410_BANKCON_SDRAM);
  67. }
  68. /**
  69. * to_div - convert cycle time to divisor
  70. * @cyc: The cycle time, in 10ths of nanoseconds.
  71. * @hclk_tns: The cycle time for HCLK, in 10ths of nanoseconds.
  72. *
  73. * Convert the given cycle time into the divisor to use to obtain it from
  74. * HCLK.
  75. */
  76. static inline unsigned int to_div(unsigned int cyc, unsigned int hclk_tns)
  77. {
  78. if (cyc == 0)
  79. return 0;
  80. return DIV_ROUND_UP(cyc, hclk_tns);
  81. }
  82. /**
  83. * calc_0124 - calculate divisor control for divisors that do /0, /1. /2 and /4
  84. * @cyc: The cycle time, in 10ths of nanoseconds.
  85. * @hclk_tns: The cycle time for HCLK, in 10ths of nanoseconds.
  86. * @v: Pointer to register to alter.
  87. * @shift: The shift to get to the control bits.
  88. *
  89. * Calculate the divisor, and turn it into the correct control bits to
  90. * set in the result, @v.
  91. */
  92. static unsigned int calc_0124(unsigned int cyc, unsigned long hclk_tns,
  93. unsigned long *v, int shift)
  94. {
  95. unsigned int div = to_div(cyc, hclk_tns);
  96. unsigned long val;
  97. s3c_freq_iodbg("%s: cyc=%d, hclk=%lu, shift=%d => div %d\n",
  98. __func__, cyc, hclk_tns, shift, div);
  99. switch (div) {
  100. case 0:
  101. val = 0;
  102. break;
  103. case 1:
  104. val = 1;
  105. break;
  106. case 2:
  107. val = 2;
  108. break;
  109. case 3:
  110. case 4:
  111. val = 3;
  112. break;
  113. default:
  114. return -1;
  115. }
  116. *v |= val << shift;
  117. return 0;
  118. }
  119. int calc_tacp(unsigned int cyc, unsigned long hclk, unsigned long *v)
  120. {
  121. /* Currently no support for Tacp calculations. */
  122. return 0;
  123. }
  124. /**
  125. * calc_tacc - calculate divisor control for tacc.
  126. * @cyc: The cycle time, in 10ths of nanoseconds.
  127. * @nwait_en: IS nWAIT enabled for this bank.
  128. * @hclk_tns: The cycle time for HCLK, in 10ths of nanoseconds.
  129. * @v: Pointer to register to alter.
  130. *
  131. * Calculate the divisor control for tACC, taking into account whether
  132. * the bank has nWAIT enabled. The result is used to modify the value
  133. * pointed to by @v.
  134. */
  135. static int calc_tacc(unsigned int cyc, int nwait_en,
  136. unsigned long hclk_tns, unsigned long *v)
  137. {
  138. unsigned int div = to_div(cyc, hclk_tns);
  139. unsigned long val;
  140. s3c_freq_iodbg("%s: cyc=%u, nwait=%d, hclk=%lu => div=%u\n",
  141. __func__, cyc, nwait_en, hclk_tns, div);
  142. /* if nWait enabled on an bank, Tacc must be at-least 4 cycles. */
  143. if (nwait_en && div < 4)
  144. div = 4;
  145. switch (div) {
  146. case 0:
  147. val = 0;
  148. break;
  149. case 1:
  150. case 2:
  151. case 3:
  152. case 4:
  153. val = div - 1;
  154. break;
  155. case 5:
  156. case 6:
  157. val = 4;
  158. break;
  159. case 7:
  160. case 8:
  161. val = 5;
  162. break;
  163. case 9:
  164. case 10:
  165. val = 6;
  166. break;
  167. case 11:
  168. case 12:
  169. case 13:
  170. case 14:
  171. val = 7;
  172. break;
  173. default:
  174. return -1;
  175. }
  176. *v |= val << 8;
  177. return 0;
  178. }
  179. /**
  180. * s3c2410_calc_bank - calculate bank timing infromation
  181. * @cfg: The configuration we need to calculate for.
  182. * @bt: The bank timing information.
  183. *
  184. * Given the cycle timine for a bank @bt, calculate the new BANKCON
  185. * setting for the @cfg timing. This updates the timing information
  186. * ready for the cpu frequency change.
  187. */
  188. static int s3c2410_calc_bank(struct s3c_cpufreq_config *cfg,
  189. struct s3c2410_iobank_timing *bt)
  190. {
  191. unsigned long hclk = cfg->freq.hclk_tns;
  192. unsigned long res;
  193. int ret;
  194. res = bt->bankcon;
  195. res &= (S3C2410_BANKCON_SDRAM | S3C2410_BANKCON_PMC16);
  196. /* tacp: 2,3,4,5 */
  197. /* tcah: 0,1,2,4 */
  198. /* tcoh: 0,1,2,4 */
  199. /* tacc: 1,2,3,4,6,7,10,14 (>4 for nwait) */
  200. /* tcos: 0,1,2,4 */
  201. /* tacs: 0,1,2,4 */
  202. ret = calc_0124(bt->tacs, hclk, &res, S3C2410_BANKCON_Tacs_SHIFT);
  203. ret |= calc_0124(bt->tcos, hclk, &res, S3C2410_BANKCON_Tcos_SHIFT);
  204. ret |= calc_0124(bt->tcah, hclk, &res, S3C2410_BANKCON_Tcah_SHIFT);
  205. ret |= calc_0124(bt->tcoh, hclk, &res, S3C2410_BANKCON_Tcoh_SHIFT);
  206. if (ret)
  207. return -EINVAL;
  208. ret |= calc_tacp(bt->tacp, hclk, &res);
  209. ret |= calc_tacc(bt->tacc, bt->nwait_en, hclk, &res);
  210. if (ret)
  211. return -EINVAL;
  212. bt->bankcon = res;
  213. return 0;
  214. }
  215. static unsigned int tacc_tab[] = {
  216. [0] = 1,
  217. [1] = 2,
  218. [2] = 3,
  219. [3] = 4,
  220. [4] = 6,
  221. [5] = 9,
  222. [6] = 10,
  223. [7] = 14,
  224. };
  225. /**
  226. * get_tacc - turn tACC value into cycle time
  227. * @hclk_tns: The cycle time for HCLK, in 10ths of nanoseconds.
  228. * @val: The bank timing register value, shifed down.
  229. */
  230. static unsigned int get_tacc(unsigned long hclk_tns,
  231. unsigned long val)
  232. {
  233. val &= 7;
  234. return hclk_tns * tacc_tab[val];
  235. }
  236. /**
  237. * get_0124 - turn 0/1/2/4 divider into cycle time
  238. * @hclk_tns: The cycle time for HCLK, in 10ths of nanoseconds.
  239. * @val: The bank timing register value, shifed down.
  240. */
  241. static unsigned int get_0124(unsigned long hclk_tns,
  242. unsigned long val)
  243. {
  244. val &= 3;
  245. return hclk_tns * ((val == 3) ? 4 : val);
  246. }
  247. /**
  248. * s3c2410_iotiming_getbank - turn BANKCON into cycle time information
  249. * @cfg: The frequency configuration
  250. * @bt: The bank timing to fill in (uses cached BANKCON)
  251. *
  252. * Given the BANKCON setting in @bt and the current frequency settings
  253. * in @cfg, update the cycle timing information.
  254. */
  255. void s3c2410_iotiming_getbank(struct s3c_cpufreq_config *cfg,
  256. struct s3c2410_iobank_timing *bt)
  257. {
  258. unsigned long bankcon = bt->bankcon;
  259. unsigned long hclk = cfg->freq.hclk_tns;
  260. bt->tcah = get_0124(hclk, bankcon >> S3C2410_BANKCON_Tcah_SHIFT);
  261. bt->tcoh = get_0124(hclk, bankcon >> S3C2410_BANKCON_Tcoh_SHIFT);
  262. bt->tcos = get_0124(hclk, bankcon >> S3C2410_BANKCON_Tcos_SHIFT);
  263. bt->tacs = get_0124(hclk, bankcon >> S3C2410_BANKCON_Tacs_SHIFT);
  264. bt->tacc = get_tacc(hclk, bankcon >> S3C2410_BANKCON_Tacc_SHIFT);
  265. }
  266. /**
  267. * s3c2410_iotiming_debugfs - debugfs show io bank timing information
  268. * @seq: The seq_file to write output to using seq_printf().
  269. * @cfg: The current configuration.
  270. * @iob: The IO bank information to decode.
  271. */
  272. void s3c2410_iotiming_debugfs(struct seq_file *seq,
  273. struct s3c_cpufreq_config *cfg,
  274. union s3c_iobank *iob)
  275. {
  276. struct s3c2410_iobank_timing *bt = iob->io_2410;
  277. unsigned long bankcon = bt->bankcon;
  278. unsigned long hclk = cfg->freq.hclk_tns;
  279. unsigned int tacs;
  280. unsigned int tcos;
  281. unsigned int tacc;
  282. unsigned int tcoh;
  283. unsigned int tcah;
  284. seq_printf(seq, "BANKCON=0x%08lx\n", bankcon);
  285. tcah = get_0124(hclk, bankcon >> S3C2410_BANKCON_Tcah_SHIFT);
  286. tcoh = get_0124(hclk, bankcon >> S3C2410_BANKCON_Tcoh_SHIFT);
  287. tcos = get_0124(hclk, bankcon >> S3C2410_BANKCON_Tcos_SHIFT);
  288. tacs = get_0124(hclk, bankcon >> S3C2410_BANKCON_Tacs_SHIFT);
  289. tacc = get_tacc(hclk, bankcon >> S3C2410_BANKCON_Tacc_SHIFT);
  290. seq_printf(seq,
  291. "\tRead: Tacs=%d.%d, Tcos=%d.%d, Tacc=%d.%d, Tcoh=%d.%d, Tcah=%d.%d\n",
  292. print_ns(bt->tacs),
  293. print_ns(bt->tcos),
  294. print_ns(bt->tacc),
  295. print_ns(bt->tcoh),
  296. print_ns(bt->tcah));
  297. seq_printf(seq,
  298. "\t Set: Tacs=%d.%d, Tcos=%d.%d, Tacc=%d.%d, Tcoh=%d.%d, Tcah=%d.%d\n",
  299. print_ns(tacs),
  300. print_ns(tcos),
  301. print_ns(tacc),
  302. print_ns(tcoh),
  303. print_ns(tcah));
  304. }
  305. /**
  306. * s3c2410_iotiming_calc - Calculate bank timing for frequency change.
  307. * @cfg: The frequency configuration
  308. * @iot: The IO timing information to fill out.
  309. *
  310. * Calculate the new values for the banks in @iot based on the new
  311. * frequency information in @cfg. This is then used by s3c2410_iotiming_set()
  312. * to update the timing when necessary.
  313. */
  314. int s3c2410_iotiming_calc(struct s3c_cpufreq_config *cfg,
  315. struct s3c_iotimings *iot)
  316. {
  317. struct s3c2410_iobank_timing *bt;
  318. unsigned long bankcon;
  319. int bank;
  320. int ret;
  321. for (bank = 0; bank < MAX_BANKS; bank++) {
  322. bankcon = __raw_readl(bank_reg(bank));
  323. bt = iot->bank[bank].io_2410;
  324. if (!bt)
  325. continue;
  326. bt->bankcon = bankcon;
  327. ret = s3c2410_calc_bank(cfg, bt);
  328. if (ret) {
  329. printk(KERN_ERR "%s: cannot calculate bank %d io\n",
  330. __func__, bank);
  331. goto err;
  332. }
  333. s3c_freq_iodbg("%s: bank %d: con=%08lx\n",
  334. __func__, bank, bt->bankcon);
  335. }
  336. return 0;
  337. err:
  338. return ret;
  339. }
  340. /**
  341. * s3c2410_iotiming_set - set the IO timings from the given setup.
  342. * @cfg: The frequency configuration
  343. * @iot: The IO timing information to use.
  344. *
  345. * Set all the currently used IO bank timing information generated
  346. * by s3c2410_iotiming_calc() once the core has validated that all
  347. * the new values are within permitted bounds.
  348. */
  349. void s3c2410_iotiming_set(struct s3c_cpufreq_config *cfg,
  350. struct s3c_iotimings *iot)
  351. {
  352. struct s3c2410_iobank_timing *bt;
  353. int bank;
  354. /* set the io timings from the specifier */
  355. for (bank = 0; bank < MAX_BANKS; bank++) {
  356. bt = iot->bank[bank].io_2410;
  357. if (!bt)
  358. continue;
  359. __raw_writel(bt->bankcon, bank_reg(bank));
  360. }
  361. }
  362. /**
  363. * s3c2410_iotiming_get - Get the timing information from current registers.
  364. * @cfg: The frequency configuration
  365. * @timings: The IO timing information to fill out.
  366. *
  367. * Calculate the @timings timing information from the current frequency
  368. * information in @cfg, and the new frequency configuration
  369. * through all the IO banks, reading the state and then updating @iot
  370. * as necessary.
  371. *
  372. * This is used at the moment on initialisation to get the current
  373. * configuration so that boards do not have to carry their own setup
  374. * if the timings are correct on initialisation.
  375. */
  376. int s3c2410_iotiming_get(struct s3c_cpufreq_config *cfg,
  377. struct s3c_iotimings *timings)
  378. {
  379. struct s3c2410_iobank_timing *bt;
  380. unsigned long bankcon;
  381. unsigned long bwscon;
  382. int bank;
  383. bwscon = __raw_readl(S3C2410_BWSCON);
  384. /* look through all banks to see what is currently set. */
  385. for (bank = 0; bank < MAX_BANKS; bank++) {
  386. bankcon = __raw_readl(bank_reg(bank));
  387. if (!bank_is_io(bankcon))
  388. continue;
  389. s3c_freq_iodbg("%s: bank %d: con %08lx\n",
  390. __func__, bank, bankcon);
  391. bt = kzalloc(sizeof(struct s3c2410_iobank_timing), GFP_KERNEL);
  392. if (!bt) {
  393. printk(KERN_ERR "%s: no memory for bank\n", __func__);
  394. return -ENOMEM;
  395. }
  396. /* find out in nWait is enabled for bank. */
  397. if (bank != 0) {
  398. unsigned long tmp = S3C2410_BWSCON_GET(bwscon, bank);
  399. if (tmp & S3C2410_BWSCON_WS)
  400. bt->nwait_en = 1;
  401. }
  402. timings->bank[bank].io_2410 = bt;
  403. bt->bankcon = bankcon;
  404. s3c2410_iotiming_getbank(cfg, bt);
  405. }
  406. s3c2410_print_timing("get", timings);
  407. return 0;
  408. }