cpu-sa1110.c 9.3 KB

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