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