cpu-sa1110.c 9.4 KB

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