maple-cpufreq.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright (C) 2011 Dmitry Eremin-Solenikov
  3. * Copyright (C) 2002 - 2005 Benjamin Herrenschmidt <benh@kernel.crashing.org>
  4. * and Markus Demleitner <msdemlei@cl.uni-heidelberg.de>
  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. * This driver adds basic cpufreq support for SMU & 970FX based G5 Macs,
  11. * that is iMac G5 and latest single CPU desktop.
  12. */
  13. #undef DEBUG
  14. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  15. #include <linux/module.h>
  16. #include <linux/types.h>
  17. #include <linux/errno.h>
  18. #include <linux/kernel.h>
  19. #include <linux/delay.h>
  20. #include <linux/sched.h>
  21. #include <linux/cpufreq.h>
  22. #include <linux/init.h>
  23. #include <linux/completion.h>
  24. #include <linux/mutex.h>
  25. #include <linux/time.h>
  26. #include <linux/of_device.h>
  27. #define DBG(fmt...) pr_debug(fmt)
  28. /* see 970FX user manual */
  29. #define SCOM_PCR 0x0aa001 /* PCR scom addr */
  30. #define PCR_HILO_SELECT 0x80000000U /* 1 = PCR, 0 = PCRH */
  31. #define PCR_SPEED_FULL 0x00000000U /* 1:1 speed value */
  32. #define PCR_SPEED_HALF 0x00020000U /* 1:2 speed value */
  33. #define PCR_SPEED_QUARTER 0x00040000U /* 1:4 speed value */
  34. #define PCR_SPEED_MASK 0x000e0000U /* speed mask */
  35. #define PCR_SPEED_SHIFT 17
  36. #define PCR_FREQ_REQ_VALID 0x00010000U /* freq request valid */
  37. #define PCR_VOLT_REQ_VALID 0x00008000U /* volt request valid */
  38. #define PCR_TARGET_TIME_MASK 0x00006000U /* target time */
  39. #define PCR_STATLAT_MASK 0x00001f00U /* STATLAT value */
  40. #define PCR_SNOOPLAT_MASK 0x000000f0U /* SNOOPLAT value */
  41. #define PCR_SNOOPACC_MASK 0x0000000fU /* SNOOPACC value */
  42. #define SCOM_PSR 0x408001 /* PSR scom addr */
  43. /* warning: PSR is a 64 bits register */
  44. #define PSR_CMD_RECEIVED 0x2000000000000000U /* command received */
  45. #define PSR_CMD_COMPLETED 0x1000000000000000U /* command completed */
  46. #define PSR_CUR_SPEED_MASK 0x0300000000000000U /* current speed */
  47. #define PSR_CUR_SPEED_SHIFT (56)
  48. /*
  49. * The G5 only supports two frequencies (Quarter speed is not supported)
  50. */
  51. #define CPUFREQ_HIGH 0
  52. #define CPUFREQ_LOW 1
  53. static struct cpufreq_frequency_table maple_cpu_freqs[] = {
  54. {0, CPUFREQ_HIGH, 0},
  55. {0, CPUFREQ_LOW, 0},
  56. {0, 0, CPUFREQ_TABLE_END},
  57. };
  58. /* Power mode data is an array of the 32 bits PCR values to use for
  59. * the various frequencies, retrieved from the device-tree
  60. */
  61. static int maple_pmode_cur;
  62. static const u32 *maple_pmode_data;
  63. static int maple_pmode_max;
  64. /*
  65. * SCOM based frequency switching for 970FX rev3
  66. */
  67. static int maple_scom_switch_freq(int speed_mode)
  68. {
  69. unsigned long flags;
  70. int to;
  71. local_irq_save(flags);
  72. /* Clear PCR high */
  73. scom970_write(SCOM_PCR, 0);
  74. /* Clear PCR low */
  75. scom970_write(SCOM_PCR, PCR_HILO_SELECT | 0);
  76. /* Set PCR low */
  77. scom970_write(SCOM_PCR, PCR_HILO_SELECT |
  78. maple_pmode_data[speed_mode]);
  79. /* Wait for completion */
  80. for (to = 0; to < 10; to++) {
  81. unsigned long psr = scom970_read(SCOM_PSR);
  82. if ((psr & PSR_CMD_RECEIVED) == 0 &&
  83. (((psr >> PSR_CUR_SPEED_SHIFT) ^
  84. (maple_pmode_data[speed_mode] >> PCR_SPEED_SHIFT)) & 0x3)
  85. == 0)
  86. break;
  87. if (psr & PSR_CMD_COMPLETED)
  88. break;
  89. udelay(100);
  90. }
  91. local_irq_restore(flags);
  92. maple_pmode_cur = speed_mode;
  93. ppc_proc_freq = maple_cpu_freqs[speed_mode].frequency * 1000ul;
  94. return 0;
  95. }
  96. static int maple_scom_query_freq(void)
  97. {
  98. unsigned long psr = scom970_read(SCOM_PSR);
  99. int i;
  100. for (i = 0; i <= maple_pmode_max; i++)
  101. if ((((psr >> PSR_CUR_SPEED_SHIFT) ^
  102. (maple_pmode_data[i] >> PCR_SPEED_SHIFT)) & 0x3) == 0)
  103. break;
  104. return i;
  105. }
  106. /*
  107. * Common interface to the cpufreq core
  108. */
  109. static int maple_cpufreq_target(struct cpufreq_policy *policy,
  110. unsigned int index)
  111. {
  112. return maple_scom_switch_freq(index);
  113. }
  114. static unsigned int maple_cpufreq_get_speed(unsigned int cpu)
  115. {
  116. return maple_cpu_freqs[maple_pmode_cur].frequency;
  117. }
  118. static int maple_cpufreq_cpu_init(struct cpufreq_policy *policy)
  119. {
  120. return cpufreq_generic_init(policy, maple_cpu_freqs, 12000);
  121. }
  122. static struct cpufreq_driver maple_cpufreq_driver = {
  123. .name = "maple",
  124. .flags = CPUFREQ_CONST_LOOPS,
  125. .init = maple_cpufreq_cpu_init,
  126. .verify = cpufreq_generic_frequency_table_verify,
  127. .target_index = maple_cpufreq_target,
  128. .get = maple_cpufreq_get_speed,
  129. .attr = cpufreq_generic_attr,
  130. };
  131. static int __init maple_cpufreq_init(void)
  132. {
  133. struct device_node *cpunode;
  134. unsigned int psize;
  135. unsigned long max_freq;
  136. const u32 *valp;
  137. u32 pvr_hi;
  138. int rc = -ENODEV;
  139. /*
  140. * Behave here like powermac driver which checks machine compatibility
  141. * to ease merging of two drivers in future.
  142. */
  143. if (!of_machine_is_compatible("Momentum,Maple") &&
  144. !of_machine_is_compatible("Momentum,Apache"))
  145. return 0;
  146. /* Get first CPU node */
  147. cpunode = of_cpu_device_node_get(0);
  148. if (cpunode == NULL) {
  149. pr_err("Can't find any CPU 0 node\n");
  150. goto bail_noprops;
  151. }
  152. /* Check 970FX for now */
  153. /* we actually don't care on which CPU to access PVR */
  154. pvr_hi = PVR_VER(mfspr(SPRN_PVR));
  155. if (pvr_hi != 0x3c && pvr_hi != 0x44) {
  156. pr_err("Unsupported CPU version (%x)\n", pvr_hi);
  157. goto bail_noprops;
  158. }
  159. /* Look for the powertune data in the device-tree */
  160. /*
  161. * On Maple this property is provided by PIBS in dual-processor config,
  162. * not provided by PIBS in CPU0 config and also not provided by SLOF,
  163. * so YMMV
  164. */
  165. maple_pmode_data = of_get_property(cpunode, "power-mode-data", &psize);
  166. if (!maple_pmode_data) {
  167. DBG("No power-mode-data !\n");
  168. goto bail_noprops;
  169. }
  170. maple_pmode_max = psize / sizeof(u32) - 1;
  171. /*
  172. * From what I see, clock-frequency is always the maximal frequency.
  173. * The current driver can not slew sysclk yet, so we really only deal
  174. * with powertune steps for now. We also only implement full freq and
  175. * half freq in this version. So far, I haven't yet seen a machine
  176. * supporting anything else.
  177. */
  178. valp = of_get_property(cpunode, "clock-frequency", NULL);
  179. if (!valp)
  180. return -ENODEV;
  181. max_freq = (*valp)/1000;
  182. maple_cpu_freqs[0].frequency = max_freq;
  183. maple_cpu_freqs[1].frequency = max_freq/2;
  184. /* Force apply current frequency to make sure everything is in
  185. * sync (voltage is right for example). Firmware may leave us with
  186. * a strange setting ...
  187. */
  188. msleep(10);
  189. maple_pmode_cur = -1;
  190. maple_scom_switch_freq(maple_scom_query_freq());
  191. pr_info("Registering Maple CPU frequency driver\n");
  192. pr_info("Low: %d Mhz, High: %d Mhz, Cur: %d MHz\n",
  193. maple_cpu_freqs[1].frequency/1000,
  194. maple_cpu_freqs[0].frequency/1000,
  195. maple_cpu_freqs[maple_pmode_cur].frequency/1000);
  196. rc = cpufreq_register_driver(&maple_cpufreq_driver);
  197. of_node_put(cpunode);
  198. return rc;
  199. bail_noprops:
  200. of_node_put(cpunode);
  201. return rc;
  202. }
  203. module_init(maple_cpufreq_init);
  204. MODULE_LICENSE("GPL");