cpu-sa1110.c 8.6 KB

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