cpu-sa1110.c 9.3 KB

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