s3c2410-iotiming.c 12 KB

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