sa1110-cpufreq.c 8.9 KB

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