us2e_cpufreq.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /* us2e_cpufreq.c: UltraSPARC-IIe cpu frequency support
  2. *
  3. * Copyright (C) 2003 David S. Miller (davem@redhat.com)
  4. *
  5. * Many thanks to Dominik Brodowski for fixing up the cpufreq
  6. * infrastructure in order to make this driver easier to implement.
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/sched.h>
  11. #include <linux/smp.h>
  12. #include <linux/cpufreq.h>
  13. #include <linux/threads.h>
  14. #include <linux/slab.h>
  15. #include <linux/delay.h>
  16. #include <linux/init.h>
  17. #include <asm/asi.h>
  18. #include <asm/timer.h>
  19. static struct cpufreq_driver *cpufreq_us2e_driver;
  20. struct us2e_freq_percpu_info {
  21. struct cpufreq_frequency_table table[6];
  22. };
  23. /* Indexed by cpu number. */
  24. static struct us2e_freq_percpu_info *us2e_freq_table;
  25. #define HBIRD_MEM_CNTL0_ADDR 0x1fe0000f010UL
  26. #define HBIRD_ESTAR_MODE_ADDR 0x1fe0000f080UL
  27. /* UltraSPARC-IIe has five dividers: 1, 2, 4, 6, and 8. These are controlled
  28. * in the ESTAR mode control register.
  29. */
  30. #define ESTAR_MODE_DIV_1 0x0000000000000000UL
  31. #define ESTAR_MODE_DIV_2 0x0000000000000001UL
  32. #define ESTAR_MODE_DIV_4 0x0000000000000003UL
  33. #define ESTAR_MODE_DIV_6 0x0000000000000002UL
  34. #define ESTAR_MODE_DIV_8 0x0000000000000004UL
  35. #define ESTAR_MODE_DIV_MASK 0x0000000000000007UL
  36. #define MCTRL0_SREFRESH_ENAB 0x0000000000010000UL
  37. #define MCTRL0_REFR_COUNT_MASK 0x0000000000007f00UL
  38. #define MCTRL0_REFR_COUNT_SHIFT 8
  39. #define MCTRL0_REFR_INTERVAL 7800
  40. #define MCTRL0_REFR_CLKS_P_CNT 64
  41. static unsigned long read_hbreg(unsigned long addr)
  42. {
  43. unsigned long ret;
  44. __asm__ __volatile__("ldxa [%1] %2, %0"
  45. : "=&r" (ret)
  46. : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E));
  47. return ret;
  48. }
  49. static void write_hbreg(unsigned long addr, unsigned long val)
  50. {
  51. __asm__ __volatile__("stxa %0, [%1] %2\n\t"
  52. "membar #Sync"
  53. : /* no outputs */
  54. : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E)
  55. : "memory");
  56. if (addr == HBIRD_ESTAR_MODE_ADDR) {
  57. /* Need to wait 16 clock cycles for the PLL to lock. */
  58. udelay(1);
  59. }
  60. }
  61. static void self_refresh_ctl(int enable)
  62. {
  63. unsigned long mctrl = read_hbreg(HBIRD_MEM_CNTL0_ADDR);
  64. if (enable)
  65. mctrl |= MCTRL0_SREFRESH_ENAB;
  66. else
  67. mctrl &= ~MCTRL0_SREFRESH_ENAB;
  68. write_hbreg(HBIRD_MEM_CNTL0_ADDR, mctrl);
  69. (void) read_hbreg(HBIRD_MEM_CNTL0_ADDR);
  70. }
  71. static void frob_mem_refresh(int cpu_slowing_down,
  72. unsigned long clock_tick,
  73. unsigned long old_divisor, unsigned long divisor)
  74. {
  75. unsigned long old_refr_count, refr_count, mctrl;
  76. refr_count = (clock_tick * MCTRL0_REFR_INTERVAL);
  77. refr_count /= (MCTRL0_REFR_CLKS_P_CNT * divisor * 1000000000UL);
  78. mctrl = read_hbreg(HBIRD_MEM_CNTL0_ADDR);
  79. old_refr_count = (mctrl & MCTRL0_REFR_COUNT_MASK)
  80. >> MCTRL0_REFR_COUNT_SHIFT;
  81. mctrl &= ~MCTRL0_REFR_COUNT_MASK;
  82. mctrl |= refr_count << MCTRL0_REFR_COUNT_SHIFT;
  83. write_hbreg(HBIRD_MEM_CNTL0_ADDR, mctrl);
  84. mctrl = read_hbreg(HBIRD_MEM_CNTL0_ADDR);
  85. if (cpu_slowing_down && !(mctrl & MCTRL0_SREFRESH_ENAB)) {
  86. unsigned long usecs;
  87. /* We have to wait for both refresh counts (old
  88. * and new) to go to zero.
  89. */
  90. usecs = (MCTRL0_REFR_CLKS_P_CNT *
  91. (refr_count + old_refr_count) *
  92. 1000000UL *
  93. old_divisor) / clock_tick;
  94. udelay(usecs + 1UL);
  95. }
  96. }
  97. static void us2e_transition(unsigned long estar, unsigned long new_bits,
  98. unsigned long clock_tick,
  99. unsigned long old_divisor, unsigned long divisor)
  100. {
  101. unsigned long flags;
  102. local_irq_save(flags);
  103. estar &= ~ESTAR_MODE_DIV_MASK;
  104. /* This is based upon the state transition diagram in the IIe manual. */
  105. if (old_divisor == 2 && divisor == 1) {
  106. self_refresh_ctl(0);
  107. write_hbreg(HBIRD_ESTAR_MODE_ADDR, estar | new_bits);
  108. frob_mem_refresh(0, clock_tick, old_divisor, divisor);
  109. } else if (old_divisor == 1 && divisor == 2) {
  110. frob_mem_refresh(1, clock_tick, old_divisor, divisor);
  111. write_hbreg(HBIRD_ESTAR_MODE_ADDR, estar | new_bits);
  112. self_refresh_ctl(1);
  113. } else if (old_divisor == 1 && divisor > 2) {
  114. us2e_transition(estar, ESTAR_MODE_DIV_2, clock_tick,
  115. 1, 2);
  116. us2e_transition(estar, new_bits, clock_tick,
  117. 2, divisor);
  118. } else if (old_divisor > 2 && divisor == 1) {
  119. us2e_transition(estar, ESTAR_MODE_DIV_2, clock_tick,
  120. old_divisor, 2);
  121. us2e_transition(estar, new_bits, clock_tick,
  122. 2, divisor);
  123. } else if (old_divisor < divisor) {
  124. frob_mem_refresh(0, clock_tick, old_divisor, divisor);
  125. write_hbreg(HBIRD_ESTAR_MODE_ADDR, estar | new_bits);
  126. } else if (old_divisor > divisor) {
  127. write_hbreg(HBIRD_ESTAR_MODE_ADDR, estar | new_bits);
  128. frob_mem_refresh(1, clock_tick, old_divisor, divisor);
  129. } else {
  130. BUG();
  131. }
  132. local_irq_restore(flags);
  133. }
  134. static unsigned long index_to_estar_mode(unsigned int index)
  135. {
  136. switch (index) {
  137. case 0:
  138. return ESTAR_MODE_DIV_1;
  139. case 1:
  140. return ESTAR_MODE_DIV_2;
  141. case 2:
  142. return ESTAR_MODE_DIV_4;
  143. case 3:
  144. return ESTAR_MODE_DIV_6;
  145. case 4:
  146. return ESTAR_MODE_DIV_8;
  147. default:
  148. BUG();
  149. }
  150. }
  151. static unsigned long index_to_divisor(unsigned int index)
  152. {
  153. switch (index) {
  154. case 0:
  155. return 1;
  156. case 1:
  157. return 2;
  158. case 2:
  159. return 4;
  160. case 3:
  161. return 6;
  162. case 4:
  163. return 8;
  164. default:
  165. BUG();
  166. }
  167. }
  168. static unsigned long estar_to_divisor(unsigned long estar)
  169. {
  170. unsigned long ret;
  171. switch (estar & ESTAR_MODE_DIV_MASK) {
  172. case ESTAR_MODE_DIV_1:
  173. ret = 1;
  174. break;
  175. case ESTAR_MODE_DIV_2:
  176. ret = 2;
  177. break;
  178. case ESTAR_MODE_DIV_4:
  179. ret = 4;
  180. break;
  181. case ESTAR_MODE_DIV_6:
  182. ret = 6;
  183. break;
  184. case ESTAR_MODE_DIV_8:
  185. ret = 8;
  186. break;
  187. default:
  188. BUG();
  189. }
  190. return ret;
  191. }
  192. static unsigned int us2e_freq_get(unsigned int cpu)
  193. {
  194. cpumask_t cpus_allowed;
  195. unsigned long clock_tick, estar;
  196. if (!cpu_online(cpu))
  197. return 0;
  198. cpumask_copy(&cpus_allowed, tsk_cpus_allowed(current));
  199. set_cpus_allowed_ptr(current, cpumask_of(cpu));
  200. clock_tick = sparc64_get_clock_tick(cpu) / 1000;
  201. estar = read_hbreg(HBIRD_ESTAR_MODE_ADDR);
  202. set_cpus_allowed_ptr(current, &cpus_allowed);
  203. return clock_tick / estar_to_divisor(estar);
  204. }
  205. static void us2e_set_cpu_divider_index(unsigned int cpu, unsigned int index)
  206. {
  207. unsigned long new_bits, new_freq;
  208. unsigned long clock_tick, divisor, old_divisor, estar;
  209. cpumask_t cpus_allowed;
  210. struct cpufreq_freqs freqs;
  211. if (!cpu_online(cpu))
  212. return;
  213. cpumask_copy(&cpus_allowed, tsk_cpus_allowed(current));
  214. set_cpus_allowed_ptr(current, cpumask_of(cpu));
  215. new_freq = clock_tick = sparc64_get_clock_tick(cpu) / 1000;
  216. new_bits = index_to_estar_mode(index);
  217. divisor = index_to_divisor(index);
  218. new_freq /= divisor;
  219. estar = read_hbreg(HBIRD_ESTAR_MODE_ADDR);
  220. old_divisor = estar_to_divisor(estar);
  221. freqs.old = clock_tick / old_divisor;
  222. freqs.new = new_freq;
  223. freqs.cpu = cpu;
  224. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  225. if (old_divisor != divisor)
  226. us2e_transition(estar, new_bits, clock_tick * 1000,
  227. old_divisor, divisor);
  228. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  229. set_cpus_allowed_ptr(current, &cpus_allowed);
  230. }
  231. static int us2e_freq_target(struct cpufreq_policy *policy,
  232. unsigned int target_freq,
  233. unsigned int relation)
  234. {
  235. unsigned int new_index = 0;
  236. if (cpufreq_frequency_table_target(policy,
  237. &us2e_freq_table[policy->cpu].table[0],
  238. target_freq, relation, &new_index))
  239. return -EINVAL;
  240. us2e_set_cpu_divider_index(policy->cpu, new_index);
  241. return 0;
  242. }
  243. static int us2e_freq_verify(struct cpufreq_policy *policy)
  244. {
  245. return cpufreq_frequency_table_verify(policy,
  246. &us2e_freq_table[policy->cpu].table[0]);
  247. }
  248. static int __init us2e_freq_cpu_init(struct cpufreq_policy *policy)
  249. {
  250. unsigned int cpu = policy->cpu;
  251. unsigned long clock_tick = sparc64_get_clock_tick(cpu) / 1000;
  252. struct cpufreq_frequency_table *table =
  253. &us2e_freq_table[cpu].table[0];
  254. table[0].index = 0;
  255. table[0].frequency = clock_tick / 1;
  256. table[1].index = 1;
  257. table[1].frequency = clock_tick / 2;
  258. table[2].index = 2;
  259. table[2].frequency = clock_tick / 4;
  260. table[2].index = 3;
  261. table[2].frequency = clock_tick / 6;
  262. table[2].index = 4;
  263. table[2].frequency = clock_tick / 8;
  264. table[2].index = 5;
  265. table[3].frequency = CPUFREQ_TABLE_END;
  266. policy->cpuinfo.transition_latency = 0;
  267. policy->cur = clock_tick;
  268. return cpufreq_frequency_table_cpuinfo(policy, table);
  269. }
  270. static int us2e_freq_cpu_exit(struct cpufreq_policy *policy)
  271. {
  272. if (cpufreq_us2e_driver)
  273. us2e_set_cpu_divider_index(policy->cpu, 0);
  274. return 0;
  275. }
  276. static int __init us2e_freq_init(void)
  277. {
  278. unsigned long manuf, impl, ver;
  279. int ret;
  280. if (tlb_type != spitfire)
  281. return -ENODEV;
  282. __asm__("rdpr %%ver, %0" : "=r" (ver));
  283. manuf = ((ver >> 48) & 0xffff);
  284. impl = ((ver >> 32) & 0xffff);
  285. if (manuf == 0x17 && impl == 0x13) {
  286. struct cpufreq_driver *driver;
  287. ret = -ENOMEM;
  288. driver = kzalloc(sizeof(struct cpufreq_driver), GFP_KERNEL);
  289. if (!driver)
  290. goto err_out;
  291. us2e_freq_table = kzalloc(
  292. (NR_CPUS * sizeof(struct us2e_freq_percpu_info)),
  293. GFP_KERNEL);
  294. if (!us2e_freq_table)
  295. goto err_out;
  296. driver->init = us2e_freq_cpu_init;
  297. driver->verify = us2e_freq_verify;
  298. driver->target = us2e_freq_target;
  299. driver->get = us2e_freq_get;
  300. driver->exit = us2e_freq_cpu_exit;
  301. driver->owner = THIS_MODULE,
  302. strcpy(driver->name, "UltraSPARC-IIe");
  303. cpufreq_us2e_driver = driver;
  304. ret = cpufreq_register_driver(driver);
  305. if (ret)
  306. goto err_out;
  307. return 0;
  308. err_out:
  309. if (driver) {
  310. kfree(driver);
  311. cpufreq_us2e_driver = NULL;
  312. }
  313. kfree(us2e_freq_table);
  314. us2e_freq_table = NULL;
  315. return ret;
  316. }
  317. return -ENODEV;
  318. }
  319. static void __exit us2e_freq_exit(void)
  320. {
  321. if (cpufreq_us2e_driver) {
  322. cpufreq_unregister_driver(cpufreq_us2e_driver);
  323. kfree(cpufreq_us2e_driver);
  324. cpufreq_us2e_driver = NULL;
  325. kfree(us2e_freq_table);
  326. us2e_freq_table = NULL;
  327. }
  328. }
  329. MODULE_AUTHOR("David S. Miller <davem@redhat.com>");
  330. MODULE_DESCRIPTION("cpufreq driver for UltraSPARC-IIe");
  331. MODULE_LICENSE("GPL");
  332. module_init(us2e_freq_init);
  333. module_exit(us2e_freq_exit);