cpufreq_64.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Copyright (C) 2002 - 2005 Benjamin Herrenschmidt <benh@kernel.crashing.org>
  3. * and Markus Demleitner <msdemlei@cl.uni-heidelberg.de>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This driver adds basic cpufreq support for SMU & 970FX based G5 Macs,
  10. * that is iMac G5 and latest single CPU desktop.
  11. */
  12. #include <linux/config.h>
  13. #include <linux/module.h>
  14. #include <linux/types.h>
  15. #include <linux/errno.h>
  16. #include <linux/kernel.h>
  17. #include <linux/delay.h>
  18. #include <linux/sched.h>
  19. #include <linux/slab.h>
  20. #include <linux/cpufreq.h>
  21. #include <linux/init.h>
  22. #include <linux/completion.h>
  23. #include <asm/prom.h>
  24. #include <asm/machdep.h>
  25. #include <asm/irq.h>
  26. #include <asm/sections.h>
  27. #include <asm/cputable.h>
  28. #include <asm/time.h>
  29. #include <asm/smu.h>
  30. #undef DEBUG
  31. #ifdef DEBUG
  32. #define DBG(fmt...) printk(fmt)
  33. #else
  34. #define DBG(fmt...)
  35. #endif
  36. /* see 970FX user manual */
  37. #define SCOM_PCR 0x0aa001 /* PCR scom addr */
  38. #define PCR_HILO_SELECT 0x80000000U /* 1 = PCR, 0 = PCRH */
  39. #define PCR_SPEED_FULL 0x00000000U /* 1:1 speed value */
  40. #define PCR_SPEED_HALF 0x00020000U /* 1:2 speed value */
  41. #define PCR_SPEED_QUARTER 0x00040000U /* 1:4 speed value */
  42. #define PCR_SPEED_MASK 0x000e0000U /* speed mask */
  43. #define PCR_SPEED_SHIFT 17
  44. #define PCR_FREQ_REQ_VALID 0x00010000U /* freq request valid */
  45. #define PCR_VOLT_REQ_VALID 0x00008000U /* volt request valid */
  46. #define PCR_TARGET_TIME_MASK 0x00006000U /* target time */
  47. #define PCR_STATLAT_MASK 0x00001f00U /* STATLAT value */
  48. #define PCR_SNOOPLAT_MASK 0x000000f0U /* SNOOPLAT value */
  49. #define PCR_SNOOPACC_MASK 0x0000000fU /* SNOOPACC value */
  50. #define SCOM_PSR 0x408001 /* PSR scom addr */
  51. /* warning: PSR is a 64 bits register */
  52. #define PSR_CMD_RECEIVED 0x2000000000000000U /* command received */
  53. #define PSR_CMD_COMPLETED 0x1000000000000000U /* command completed */
  54. #define PSR_CUR_SPEED_MASK 0x0300000000000000U /* current speed */
  55. #define PSR_CUR_SPEED_SHIFT (56)
  56. /*
  57. * The G5 only supports two frequencies (Quarter speed is not supported)
  58. */
  59. #define CPUFREQ_HIGH 0
  60. #define CPUFREQ_LOW 1
  61. static struct cpufreq_frequency_table g5_cpu_freqs[] = {
  62. {CPUFREQ_HIGH, 0},
  63. {CPUFREQ_LOW, 0},
  64. {0, CPUFREQ_TABLE_END},
  65. };
  66. static struct freq_attr* g5_cpu_freqs_attr[] = {
  67. &cpufreq_freq_attr_scaling_available_freqs,
  68. NULL,
  69. };
  70. /* Power mode data is an array of the 32 bits PCR values to use for
  71. * the various frequencies, retreived from the device-tree
  72. */
  73. static u32 *g5_pmode_data;
  74. static int g5_pmode_max;
  75. static int g5_pmode_cur;
  76. static DECLARE_MUTEX(g5_switch_mutex);
  77. static struct smu_sdbp_fvt *g5_fvt_table; /* table of op. points */
  78. static int g5_fvt_count; /* number of op. points */
  79. static int g5_fvt_cur; /* current op. point */
  80. /* ----------------- real hardware interface */
  81. static void g5_switch_volt(int speed_mode)
  82. {
  83. struct smu_simple_cmd cmd;
  84. DECLARE_COMPLETION(comp);
  85. smu_queue_simple(&cmd, SMU_CMD_POWER_COMMAND, 8, smu_done_complete,
  86. &comp, 'V', 'S', 'L', 'E', 'W',
  87. 0xff, g5_fvt_cur+1, speed_mode);
  88. wait_for_completion(&comp);
  89. }
  90. static int g5_switch_freq(int speed_mode)
  91. {
  92. struct cpufreq_freqs freqs;
  93. int to;
  94. if (g5_pmode_cur == speed_mode)
  95. return 0;
  96. down(&g5_switch_mutex);
  97. freqs.old = g5_cpu_freqs[g5_pmode_cur].frequency;
  98. freqs.new = g5_cpu_freqs[speed_mode].frequency;
  99. freqs.cpu = 0;
  100. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  101. /* If frequency is going up, first ramp up the voltage */
  102. if (speed_mode < g5_pmode_cur)
  103. g5_switch_volt(speed_mode);
  104. /* Clear PCR high */
  105. scom970_write(SCOM_PCR, 0);
  106. /* Clear PCR low */
  107. scom970_write(SCOM_PCR, PCR_HILO_SELECT | 0);
  108. /* Set PCR low */
  109. scom970_write(SCOM_PCR, PCR_HILO_SELECT |
  110. g5_pmode_data[speed_mode]);
  111. /* Wait for completion */
  112. for (to = 0; to < 10; to++) {
  113. unsigned long psr = scom970_read(SCOM_PSR);
  114. if ((psr & PSR_CMD_RECEIVED) == 0 &&
  115. (((psr >> PSR_CUR_SPEED_SHIFT) ^
  116. (g5_pmode_data[speed_mode] >> PCR_SPEED_SHIFT)) & 0x3)
  117. == 0)
  118. break;
  119. if (psr & PSR_CMD_COMPLETED)
  120. break;
  121. udelay(100);
  122. }
  123. /* If frequency is going down, last ramp the voltage */
  124. if (speed_mode > g5_pmode_cur)
  125. g5_switch_volt(speed_mode);
  126. g5_pmode_cur = speed_mode;
  127. ppc_proc_freq = g5_cpu_freqs[speed_mode].frequency * 1000ul;
  128. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  129. up(&g5_switch_mutex);
  130. return 0;
  131. }
  132. static int g5_query_freq(void)
  133. {
  134. unsigned long psr = scom970_read(SCOM_PSR);
  135. int i;
  136. for (i = 0; i <= g5_pmode_max; i++)
  137. if ((((psr >> PSR_CUR_SPEED_SHIFT) ^
  138. (g5_pmode_data[i] >> PCR_SPEED_SHIFT)) & 0x3) == 0)
  139. break;
  140. return i;
  141. }
  142. /* ----------------- cpufreq bookkeeping */
  143. static int g5_cpufreq_verify(struct cpufreq_policy *policy)
  144. {
  145. return cpufreq_frequency_table_verify(policy, g5_cpu_freqs);
  146. }
  147. static int g5_cpufreq_target(struct cpufreq_policy *policy,
  148. unsigned int target_freq, unsigned int relation)
  149. {
  150. unsigned int newstate = 0;
  151. if (cpufreq_frequency_table_target(policy, g5_cpu_freqs,
  152. target_freq, relation, &newstate))
  153. return -EINVAL;
  154. return g5_switch_freq(newstate);
  155. }
  156. static unsigned int g5_cpufreq_get_speed(unsigned int cpu)
  157. {
  158. return g5_cpu_freqs[g5_pmode_cur].frequency;
  159. }
  160. static int g5_cpufreq_cpu_init(struct cpufreq_policy *policy)
  161. {
  162. if (policy->cpu != 0)
  163. return -ENODEV;
  164. policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
  165. policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
  166. policy->cur = g5_cpu_freqs[g5_query_freq()].frequency;
  167. cpufreq_frequency_table_get_attr(g5_cpu_freqs, policy->cpu);
  168. return cpufreq_frequency_table_cpuinfo(policy,
  169. g5_cpu_freqs);
  170. }
  171. static struct cpufreq_driver g5_cpufreq_driver = {
  172. .name = "powermac",
  173. .owner = THIS_MODULE,
  174. .flags = CPUFREQ_CONST_LOOPS,
  175. .init = g5_cpufreq_cpu_init,
  176. .verify = g5_cpufreq_verify,
  177. .target = g5_cpufreq_target,
  178. .get = g5_cpufreq_get_speed,
  179. .attr = g5_cpu_freqs_attr,
  180. };
  181. static int __init g5_cpufreq_init(void)
  182. {
  183. struct device_node *cpunode;
  184. unsigned int psize, ssize;
  185. struct smu_sdbp_header *shdr;
  186. unsigned long max_freq;
  187. u32 *valp;
  188. int rc = -ENODEV;
  189. /* Look for CPU and SMU nodes */
  190. cpunode = of_find_node_by_type(NULL, "cpu");
  191. if (!cpunode) {
  192. DBG("No CPU node !\n");
  193. return -ENODEV;
  194. }
  195. /* Check 970FX for now */
  196. valp = (u32 *)get_property(cpunode, "cpu-version", NULL);
  197. if (!valp) {
  198. DBG("No cpu-version property !\n");
  199. goto bail_noprops;
  200. }
  201. if (((*valp) >> 16) != 0x3c) {
  202. DBG("Wrong CPU version: %08x\n", *valp);
  203. goto bail_noprops;
  204. }
  205. /* Look for the powertune data in the device-tree */
  206. g5_pmode_data = (u32 *)get_property(cpunode, "power-mode-data",&psize);
  207. if (!g5_pmode_data) {
  208. DBG("No power-mode-data !\n");
  209. goto bail_noprops;
  210. }
  211. g5_pmode_max = psize / sizeof(u32) - 1;
  212. /* Look for the FVT table */
  213. shdr = smu_get_sdb_partition(SMU_SDB_FVT_ID, NULL);
  214. if (!shdr)
  215. goto bail_noprops;
  216. g5_fvt_table = (struct smu_sdbp_fvt *)&shdr[1];
  217. ssize = (shdr->len * sizeof(u32)) - sizeof(struct smu_sdbp_header);
  218. g5_fvt_count = ssize / sizeof(struct smu_sdbp_fvt);
  219. g5_fvt_cur = 0;
  220. /* Sanity checking */
  221. if (g5_fvt_count < 1 || g5_pmode_max < 1)
  222. goto bail_noprops;
  223. /*
  224. * From what I see, clock-frequency is always the maximal frequency.
  225. * The current driver can not slew sysclk yet, so we really only deal
  226. * with powertune steps for now. We also only implement full freq and
  227. * half freq in this version. So far, I haven't yet seen a machine
  228. * supporting anything else.
  229. */
  230. valp = (u32 *)get_property(cpunode, "clock-frequency", NULL);
  231. if (!valp)
  232. return -ENODEV;
  233. max_freq = (*valp)/1000;
  234. g5_cpu_freqs[0].frequency = max_freq;
  235. g5_cpu_freqs[1].frequency = max_freq/2;
  236. /* Check current frequency */
  237. g5_pmode_cur = g5_query_freq();
  238. if (g5_pmode_cur > 1)
  239. /* We don't support anything but 1:1 and 1:2, fixup ... */
  240. g5_pmode_cur = 1;
  241. /* Force apply current frequency to make sure everything is in
  242. * sync (voltage is right for example). Firmware may leave us with
  243. * a strange setting ...
  244. */
  245. g5_switch_freq(g5_pmode_cur);
  246. printk(KERN_INFO "Registering G5 CPU frequency driver\n");
  247. printk(KERN_INFO "Low: %d Mhz, High: %d Mhz, Cur: %d MHz\n",
  248. g5_cpu_freqs[1].frequency/1000,
  249. g5_cpu_freqs[0].frequency/1000,
  250. g5_cpu_freqs[g5_pmode_cur].frequency/1000);
  251. rc = cpufreq_register_driver(&g5_cpufreq_driver);
  252. /* We keep the CPU node on hold... hopefully, Apple G5 don't have
  253. * hotplug CPU with a dynamic device-tree ...
  254. */
  255. return rc;
  256. bail_noprops:
  257. of_node_put(cpunode);
  258. return rc;
  259. }
  260. module_init(g5_cpufreq_init);
  261. MODULE_LICENSE("GPL");