cpu-sa1110.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * linux/arch/arm/mach-sa1100/cpu-sa1110.c
  3. *
  4. * Copyright (C) 2001 Russell King
  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. * Note: there are two erratas that apply to the SA1110 here:
  11. * 7 - SDRAM auto-power-up failure (rev A0)
  12. * 13 - Corruption of internal register reads/writes following
  13. * SDRAM reads (rev A0, B0, B1)
  14. *
  15. * We ignore rev. A0 and B0 devices; I don't think they're worth supporting.
  16. *
  17. * The SDRAM type can be passed on the command line as cpu_sa1110.sdram=type
  18. */
  19. #include <linux/cpufreq.h>
  20. #include <linux/delay.h>
  21. #include <linux/init.h>
  22. #include <linux/io.h>
  23. #include <linux/kernel.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/types.h>
  26. #include <asm/cputype.h>
  27. #include <asm/mach-types.h>
  28. #include <mach/hardware.h>
  29. #include "generic.h"
  30. #undef DEBUG
  31. struct sdram_params {
  32. const char name[20];
  33. u_char rows; /* bits */
  34. u_char cas_latency; /* cycles */
  35. u_char tck; /* clock cycle time (ns) */
  36. u_char trcd; /* activate to r/w (ns) */
  37. u_char trp; /* precharge to activate (ns) */
  38. u_char twr; /* write recovery time (ns) */
  39. u_short refresh; /* refresh time for array (us) */
  40. };
  41. struct sdram_info {
  42. u_int mdcnfg;
  43. u_int mdrefr;
  44. u_int mdcas[3];
  45. };
  46. static struct sdram_params sdram_tbl[] __initdata = {
  47. { /* Toshiba TC59SM716 CL2 */
  48. .name = "TC59SM716-CL2",
  49. .rows = 12,
  50. .tck = 10,
  51. .trcd = 20,
  52. .trp = 20,
  53. .twr = 10,
  54. .refresh = 64000,
  55. .cas_latency = 2,
  56. }, { /* Toshiba TC59SM716 CL3 */
  57. .name = "TC59SM716-CL3",
  58. .rows = 12,
  59. .tck = 8,
  60. .trcd = 20,
  61. .trp = 20,
  62. .twr = 8,
  63. .refresh = 64000,
  64. .cas_latency = 3,
  65. }, { /* Samsung K4S641632D TC75 */
  66. .name = "K4S641632D",
  67. .rows = 14,
  68. .tck = 9,
  69. .trcd = 27,
  70. .trp = 20,
  71. .twr = 9,
  72. .refresh = 64000,
  73. .cas_latency = 3,
  74. }, { /* Samsung K4S281632B-1H */
  75. .name = "K4S281632B-1H",
  76. .rows = 12,
  77. .tck = 10,
  78. .trp = 20,
  79. .twr = 10,
  80. .refresh = 64000,
  81. .cas_latency = 3,
  82. }, { /* Samsung KM416S4030CT */
  83. .name = "KM416S4030CT",
  84. .rows = 13,
  85. .tck = 8,
  86. .trcd = 24, /* 3 CLKs */
  87. .trp = 24, /* 3 CLKs */
  88. .twr = 16, /* Trdl: 2 CLKs */
  89. .refresh = 64000,
  90. .cas_latency = 3,
  91. }, { /* Winbond W982516AH75L CL3 */
  92. .name = "W982516AH75L",
  93. .rows = 16,
  94. .tck = 8,
  95. .trcd = 20,
  96. .trp = 20,
  97. .twr = 8,
  98. .refresh = 64000,
  99. .cas_latency = 3,
  100. }, { /* Micron MT48LC8M16A2TG-75 */
  101. .name = "MT48LC8M16A2TG-75",
  102. .rows = 12,
  103. .tck = 8,
  104. .trcd = 20,
  105. .trp = 20,
  106. .twr = 8,
  107. .refresh = 64000,
  108. .cas_latency = 3,
  109. },
  110. };
  111. static struct sdram_params sdram_params;
  112. /*
  113. * Given a period in ns and frequency in khz, calculate the number of
  114. * cycles of frequency in period. Note that we round up to the next
  115. * cycle, even if we are only slightly over.
  116. */
  117. static inline u_int ns_to_cycles(u_int ns, u_int khz)
  118. {
  119. return (ns * khz + 999999) / 1000000;
  120. }
  121. /*
  122. * Create the MDCAS register bit pattern.
  123. */
  124. static inline void set_mdcas(u_int *mdcas, int delayed, u_int rcd)
  125. {
  126. u_int shift;
  127. rcd = 2 * rcd - 1;
  128. shift = delayed + 1 + rcd;
  129. mdcas[0] = (1 << rcd) - 1;
  130. mdcas[0] |= 0x55555555 << shift;
  131. mdcas[1] = mdcas[2] = 0x55555555 << (shift & 1);
  132. }
  133. static void
  134. sdram_calculate_timing(struct sdram_info *sd, u_int cpu_khz,
  135. struct sdram_params *sdram)
  136. {
  137. u_int mem_khz, sd_khz, trp, twr;
  138. mem_khz = cpu_khz / 2;
  139. sd_khz = mem_khz;
  140. /*
  141. * If SDCLK would invalidate the SDRAM timings,
  142. * run SDCLK at half speed.
  143. *
  144. * CPU steppings prior to B2 must either run the memory at
  145. * half speed or use delayed read latching (errata 13).
  146. */
  147. if ((ns_to_cycles(sdram->tck, sd_khz) > 1) ||
  148. (CPU_REVISION < CPU_SA1110_B2 && sd_khz < 62000))
  149. sd_khz /= 2;
  150. sd->mdcnfg = MDCNFG & 0x007f007f;
  151. twr = ns_to_cycles(sdram->twr, mem_khz);
  152. /* trp should always be >1 */
  153. trp = ns_to_cycles(sdram->trp, mem_khz) - 1;
  154. if (trp < 1)
  155. trp = 1;
  156. sd->mdcnfg |= trp << 8;
  157. sd->mdcnfg |= trp << 24;
  158. sd->mdcnfg |= sdram->cas_latency << 12;
  159. sd->mdcnfg |= sdram->cas_latency << 28;
  160. sd->mdcnfg |= twr << 14;
  161. sd->mdcnfg |= twr << 30;
  162. sd->mdrefr = MDREFR & 0xffbffff0;
  163. sd->mdrefr |= 7;
  164. if (sd_khz != mem_khz)
  165. sd->mdrefr |= MDREFR_K1DB2;
  166. /* initial number of '1's in MDCAS + 1 */
  167. set_mdcas(sd->mdcas, sd_khz >= 62000,
  168. ns_to_cycles(sdram->trcd, mem_khz));
  169. #ifdef DEBUG
  170. printk(KERN_DEBUG "MDCNFG: %08x MDREFR: %08x MDCAS0: %08x MDCAS1: %08x MDCAS2: %08x\n",
  171. sd->mdcnfg, sd->mdrefr, sd->mdcas[0], sd->mdcas[1],
  172. sd->mdcas[2]);
  173. #endif
  174. }
  175. /*
  176. * Set the SDRAM refresh rate.
  177. */
  178. static inline void sdram_set_refresh(u_int dri)
  179. {
  180. MDREFR = (MDREFR & 0xffff000f) | (dri << 4);
  181. (void) MDREFR;
  182. }
  183. /*
  184. * Update the refresh period. We do this such that we always refresh
  185. * the SDRAMs within their permissible period. The refresh period is
  186. * always a multiple of the memory clock (fixed at cpu_clock / 2).
  187. *
  188. * FIXME: we don't currently take account of burst accesses here,
  189. * but neither do Intels DM nor Angel.
  190. */
  191. static void
  192. sdram_update_refresh(u_int cpu_khz, struct sdram_params *sdram)
  193. {
  194. u_int ns_row = (sdram->refresh * 1000) >> sdram->rows;
  195. u_int dri = ns_to_cycles(ns_row, cpu_khz / 2) / 32;
  196. #ifdef DEBUG
  197. mdelay(250);
  198. printk(KERN_DEBUG "new dri value = %d\n", dri);
  199. #endif
  200. sdram_set_refresh(dri);
  201. }
  202. /*
  203. * Ok, set the CPU frequency.
  204. */
  205. static int sa1110_target(struct cpufreq_policy *policy,
  206. unsigned int target_freq,
  207. unsigned int relation)
  208. {
  209. struct sdram_params *sdram = &sdram_params;
  210. struct cpufreq_freqs freqs;
  211. struct sdram_info sd;
  212. unsigned long flags;
  213. unsigned int ppcr, unused;
  214. switch (relation) {
  215. case CPUFREQ_RELATION_L:
  216. ppcr = sa11x0_freq_to_ppcr(target_freq);
  217. if (sa11x0_ppcr_to_freq(ppcr) > policy->max)
  218. ppcr--;
  219. break;
  220. case CPUFREQ_RELATION_H:
  221. ppcr = sa11x0_freq_to_ppcr(target_freq);
  222. if (ppcr && (sa11x0_ppcr_to_freq(ppcr) > target_freq) &&
  223. (sa11x0_ppcr_to_freq(ppcr-1) >= policy->min))
  224. ppcr--;
  225. break;
  226. default:
  227. return -EINVAL;
  228. }
  229. freqs.old = sa11x0_getspeed(0);
  230. freqs.new = sa11x0_ppcr_to_freq(ppcr);
  231. freqs.cpu = 0;
  232. sdram_calculate_timing(&sd, freqs.new, sdram);
  233. #if 0
  234. /*
  235. * These values are wrong according to the SA1110 documentation
  236. * and errata, but they seem to work. Need to get a storage
  237. * scope on to the SDRAM signals to work out why.
  238. */
  239. if (policy->max < 147500) {
  240. sd.mdrefr |= MDREFR_K1DB2;
  241. sd.mdcas[0] = 0xaaaaaa7f;
  242. } else {
  243. sd.mdrefr &= ~MDREFR_K1DB2;
  244. sd.mdcas[0] = 0xaaaaaa9f;
  245. }
  246. sd.mdcas[1] = 0xaaaaaaaa;
  247. sd.mdcas[2] = 0xaaaaaaaa;
  248. #endif
  249. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  250. /*
  251. * The clock could be going away for some time. Set the SDRAMs
  252. * to refresh rapidly (every 64 memory clock cycles). To get
  253. * through the whole array, we need to wait 262144 mclk cycles.
  254. * We wait 20ms to be safe.
  255. */
  256. sdram_set_refresh(2);
  257. if (!irqs_disabled())
  258. msleep(20);
  259. else
  260. mdelay(20);
  261. /*
  262. * Reprogram the DRAM timings with interrupts disabled, and
  263. * ensure that we are doing this within a complete cache line.
  264. * This means that we won't access SDRAM for the duration of
  265. * the programming.
  266. */
  267. local_irq_save(flags);
  268. asm("mcr p15, 0, %0, c7, c10, 4" : : "r" (0));
  269. udelay(10);
  270. __asm__ __volatile__("\n\
  271. b 2f \n\
  272. .align 5 \n\
  273. 1: str %3, [%1, #0] @ MDCNFG \n\
  274. str %4, [%1, #28] @ MDREFR \n\
  275. str %5, [%1, #4] @ MDCAS0 \n\
  276. str %6, [%1, #8] @ MDCAS1 \n\
  277. str %7, [%1, #12] @ MDCAS2 \n\
  278. str %8, [%2, #0] @ PPCR \n\
  279. ldr %0, [%1, #0] \n\
  280. b 3f \n\
  281. 2: b 1b \n\
  282. 3: nop \n\
  283. nop"
  284. : "=&r" (unused)
  285. : "r" (&MDCNFG), "r" (&PPCR), "0" (sd.mdcnfg),
  286. "r" (sd.mdrefr), "r" (sd.mdcas[0]),
  287. "r" (sd.mdcas[1]), "r" (sd.mdcas[2]), "r" (ppcr));
  288. local_irq_restore(flags);
  289. /*
  290. * Now, return the SDRAM refresh back to normal.
  291. */
  292. sdram_update_refresh(freqs.new, sdram);
  293. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  294. return 0;
  295. }
  296. static int __init sa1110_cpu_init(struct cpufreq_policy *policy)
  297. {
  298. if (policy->cpu != 0)
  299. return -EINVAL;
  300. policy->cur = policy->min = policy->max = sa11x0_getspeed(0);
  301. policy->cpuinfo.min_freq = 59000;
  302. policy->cpuinfo.max_freq = 287000;
  303. policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
  304. return 0;
  305. }
  306. /* sa1110_driver needs __refdata because it must remain after init registers
  307. * it with cpufreq_register_driver() */
  308. static struct cpufreq_driver sa1110_driver __refdata = {
  309. .flags = CPUFREQ_STICKY,
  310. .verify = sa11x0_verify_speed,
  311. .target = sa1110_target,
  312. .get = sa11x0_getspeed,
  313. .init = sa1110_cpu_init,
  314. .name = "sa1110",
  315. };
  316. static struct sdram_params *sa1110_find_sdram(const char *name)
  317. {
  318. struct sdram_params *sdram;
  319. for (sdram = sdram_tbl; sdram < sdram_tbl + ARRAY_SIZE(sdram_tbl);
  320. sdram++)
  321. if (strcmp(name, sdram->name) == 0)
  322. return sdram;
  323. return NULL;
  324. }
  325. static char sdram_name[16];
  326. static int __init sa1110_clk_init(void)
  327. {
  328. struct sdram_params *sdram;
  329. const char *name = sdram_name;
  330. if (!cpu_is_sa1110())
  331. return -ENODEV;
  332. if (!name[0]) {
  333. if (machine_is_assabet())
  334. name = "TC59SM716-CL3";
  335. if (machine_is_pt_system3())
  336. name = "K4S641632D";
  337. if (machine_is_h3100())
  338. name = "KM416S4030CT";
  339. if (machine_is_jornada720())
  340. name = "K4S281632B-1H";
  341. if (machine_is_nanoengine())
  342. name = "MT48LC8M16A2TG-75";
  343. }
  344. sdram = sa1110_find_sdram(name);
  345. if (sdram) {
  346. printk(KERN_DEBUG "SDRAM: tck: %d trcd: %d trp: %d"
  347. " twr: %d refresh: %d cas_latency: %d\n",
  348. sdram->tck, sdram->trcd, sdram->trp,
  349. sdram->twr, sdram->refresh, sdram->cas_latency);
  350. memcpy(&sdram_params, sdram, sizeof(sdram_params));
  351. return cpufreq_register_driver(&sa1110_driver);
  352. }
  353. return 0;
  354. }
  355. module_param_string(sdram, sdram_name, sizeof(sdram_name), 0);
  356. arch_initcall(sa1110_clk_init);