sparc-us2e-cpufreq.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. estar &= ~ESTAR_MODE_DIV_MASK;
  102. /* This is based upon the state transition diagram in the IIe manual. */
  103. if (old_divisor == 2 && divisor == 1) {
  104. self_refresh_ctl(0);
  105. write_hbreg(HBIRD_ESTAR_MODE_ADDR, estar | new_bits);
  106. frob_mem_refresh(0, clock_tick, old_divisor, divisor);
  107. } else if (old_divisor == 1 && divisor == 2) {
  108. frob_mem_refresh(1, clock_tick, old_divisor, divisor);
  109. write_hbreg(HBIRD_ESTAR_MODE_ADDR, estar | new_bits);
  110. self_refresh_ctl(1);
  111. } else if (old_divisor == 1 && divisor > 2) {
  112. us2e_transition(estar, ESTAR_MODE_DIV_2, clock_tick,
  113. 1, 2);
  114. us2e_transition(estar, new_bits, clock_tick,
  115. 2, divisor);
  116. } else if (old_divisor > 2 && divisor == 1) {
  117. us2e_transition(estar, ESTAR_MODE_DIV_2, clock_tick,
  118. old_divisor, 2);
  119. us2e_transition(estar, new_bits, clock_tick,
  120. 2, divisor);
  121. } else if (old_divisor < divisor) {
  122. frob_mem_refresh(0, clock_tick, old_divisor, divisor);
  123. write_hbreg(HBIRD_ESTAR_MODE_ADDR, estar | new_bits);
  124. } else if (old_divisor > divisor) {
  125. write_hbreg(HBIRD_ESTAR_MODE_ADDR, estar | new_bits);
  126. frob_mem_refresh(1, clock_tick, old_divisor, divisor);
  127. } else {
  128. BUG();
  129. }
  130. }
  131. static unsigned long index_to_estar_mode(unsigned int index)
  132. {
  133. switch (index) {
  134. case 0:
  135. return ESTAR_MODE_DIV_1;
  136. case 1:
  137. return ESTAR_MODE_DIV_2;
  138. case 2:
  139. return ESTAR_MODE_DIV_4;
  140. case 3:
  141. return ESTAR_MODE_DIV_6;
  142. case 4:
  143. return ESTAR_MODE_DIV_8;
  144. default:
  145. BUG();
  146. }
  147. }
  148. static unsigned long index_to_divisor(unsigned int index)
  149. {
  150. switch (index) {
  151. case 0:
  152. return 1;
  153. case 1:
  154. return 2;
  155. case 2:
  156. return 4;
  157. case 3:
  158. return 6;
  159. case 4:
  160. return 8;
  161. default:
  162. BUG();
  163. }
  164. }
  165. static unsigned long estar_to_divisor(unsigned long estar)
  166. {
  167. unsigned long ret;
  168. switch (estar & ESTAR_MODE_DIV_MASK) {
  169. case ESTAR_MODE_DIV_1:
  170. ret = 1;
  171. break;
  172. case ESTAR_MODE_DIV_2:
  173. ret = 2;
  174. break;
  175. case ESTAR_MODE_DIV_4:
  176. ret = 4;
  177. break;
  178. case ESTAR_MODE_DIV_6:
  179. ret = 6;
  180. break;
  181. case ESTAR_MODE_DIV_8:
  182. ret = 8;
  183. break;
  184. default:
  185. BUG();
  186. }
  187. return ret;
  188. }
  189. static void __us2e_freq_get(void *arg)
  190. {
  191. unsigned long *estar = arg;
  192. *estar = read_hbreg(HBIRD_ESTAR_MODE_ADDR);
  193. }
  194. static unsigned int us2e_freq_get(unsigned int cpu)
  195. {
  196. unsigned long clock_tick, estar;
  197. clock_tick = sparc64_get_clock_tick(cpu) / 1000;
  198. if (smp_call_function_single(cpu, __us2e_freq_get, &estar, 1))
  199. return 0;
  200. return clock_tick / estar_to_divisor(estar);
  201. }
  202. static void __us2e_freq_target(void *arg)
  203. {
  204. unsigned int cpu = smp_processor_id();
  205. unsigned int *index = arg;
  206. unsigned long new_bits, new_freq;
  207. unsigned long clock_tick, divisor, old_divisor, estar;
  208. new_freq = clock_tick = sparc64_get_clock_tick(cpu) / 1000;
  209. new_bits = index_to_estar_mode(*index);
  210. divisor = index_to_divisor(*index);
  211. new_freq /= divisor;
  212. estar = read_hbreg(HBIRD_ESTAR_MODE_ADDR);
  213. old_divisor = estar_to_divisor(estar);
  214. if (old_divisor != divisor) {
  215. us2e_transition(estar, new_bits, clock_tick * 1000,
  216. old_divisor, divisor);
  217. }
  218. }
  219. static int us2e_freq_target(struct cpufreq_policy *policy, unsigned int index)
  220. {
  221. unsigned int cpu = policy->cpu;
  222. return smp_call_function_single(cpu, __us2e_freq_target, &index, 1);
  223. }
  224. static int __init us2e_freq_cpu_init(struct cpufreq_policy *policy)
  225. {
  226. unsigned int cpu = policy->cpu;
  227. unsigned long clock_tick = sparc64_get_clock_tick(cpu) / 1000;
  228. struct cpufreq_frequency_table *table =
  229. &us2e_freq_table[cpu].table[0];
  230. table[0].driver_data = 0;
  231. table[0].frequency = clock_tick / 1;
  232. table[1].driver_data = 1;
  233. table[1].frequency = clock_tick / 2;
  234. table[2].driver_data = 2;
  235. table[2].frequency = clock_tick / 4;
  236. table[2].driver_data = 3;
  237. table[2].frequency = clock_tick / 6;
  238. table[2].driver_data = 4;
  239. table[2].frequency = clock_tick / 8;
  240. table[2].driver_data = 5;
  241. table[3].frequency = CPUFREQ_TABLE_END;
  242. policy->cpuinfo.transition_latency = 0;
  243. policy->cur = clock_tick;
  244. return cpufreq_table_validate_and_show(policy, table);
  245. }
  246. static int us2e_freq_cpu_exit(struct cpufreq_policy *policy)
  247. {
  248. if (cpufreq_us2e_driver)
  249. us2e_freq_target(policy, 0);
  250. return 0;
  251. }
  252. static int __init us2e_freq_init(void)
  253. {
  254. unsigned long manuf, impl, ver;
  255. int ret;
  256. if (tlb_type != spitfire)
  257. return -ENODEV;
  258. __asm__("rdpr %%ver, %0" : "=r" (ver));
  259. manuf = ((ver >> 48) & 0xffff);
  260. impl = ((ver >> 32) & 0xffff);
  261. if (manuf == 0x17 && impl == 0x13) {
  262. struct cpufreq_driver *driver;
  263. ret = -ENOMEM;
  264. driver = kzalloc(sizeof(*driver), GFP_KERNEL);
  265. if (!driver)
  266. goto err_out;
  267. us2e_freq_table = kzalloc((NR_CPUS * sizeof(*us2e_freq_table)),
  268. GFP_KERNEL);
  269. if (!us2e_freq_table)
  270. goto err_out;
  271. driver->init = us2e_freq_cpu_init;
  272. driver->verify = cpufreq_generic_frequency_table_verify;
  273. driver->target_index = us2e_freq_target;
  274. driver->get = us2e_freq_get;
  275. driver->exit = us2e_freq_cpu_exit;
  276. strcpy(driver->name, "UltraSPARC-IIe");
  277. cpufreq_us2e_driver = driver;
  278. ret = cpufreq_register_driver(driver);
  279. if (ret)
  280. goto err_out;
  281. return 0;
  282. err_out:
  283. if (driver) {
  284. kfree(driver);
  285. cpufreq_us2e_driver = NULL;
  286. }
  287. kfree(us2e_freq_table);
  288. us2e_freq_table = NULL;
  289. return ret;
  290. }
  291. return -ENODEV;
  292. }
  293. static void __exit us2e_freq_exit(void)
  294. {
  295. if (cpufreq_us2e_driver) {
  296. cpufreq_unregister_driver(cpufreq_us2e_driver);
  297. kfree(cpufreq_us2e_driver);
  298. cpufreq_us2e_driver = NULL;
  299. kfree(us2e_freq_table);
  300. us2e_freq_table = NULL;
  301. }
  302. }
  303. MODULE_AUTHOR("David S. Miller <davem@redhat.com>");
  304. MODULE_DESCRIPTION("cpufreq driver for UltraSPARC-IIe");
  305. MODULE_LICENSE("GPL");
  306. module_init(us2e_freq_init);
  307. module_exit(us2e_freq_exit);