cpu-sa1110.c 9.1 KB

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