iotiming-s3c2410.c 11 KB

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