clock.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2007 Felix Fietkau <[email protected]>
  4. * Copyright (C) 2007 Eugene Konev <[email protected]>
  5. * Copyright (C) 2009 Florian Fainelli <[email protected]>
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/init.h>
  9. #include <linux/types.h>
  10. #include <linux/export.h>
  11. #include <linux/delay.h>
  12. #include <linux/gcd.h>
  13. #include <linux/io.h>
  14. #include <linux/err.h>
  15. #include <linux/clkdev.h>
  16. #include <linux/clk.h>
  17. #include <linux/clk-provider.h>
  18. #include <asm/addrspace.h>
  19. #include <asm/mach-ar7/ar7.h>
  20. #define BOOT_PLL_SOURCE_MASK 0x3
  21. #define CPU_PLL_SOURCE_SHIFT 16
  22. #define BUS_PLL_SOURCE_SHIFT 14
  23. #define USB_PLL_SOURCE_SHIFT 18
  24. #define DSP_PLL_SOURCE_SHIFT 22
  25. #define BOOT_PLL_SOURCE_AFE 0
  26. #define BOOT_PLL_SOURCE_BUS 0
  27. #define BOOT_PLL_SOURCE_REF 1
  28. #define BOOT_PLL_SOURCE_XTAL 2
  29. #define BOOT_PLL_SOURCE_CPU 3
  30. #define BOOT_PLL_BYPASS 0x00000020
  31. #define BOOT_PLL_ASYNC_MODE 0x02000000
  32. #define BOOT_PLL_2TO1_MODE 0x00008000
  33. #define TNETD7200_CLOCK_ID_CPU 0
  34. #define TNETD7200_CLOCK_ID_DSP 1
  35. #define TNETD7200_CLOCK_ID_USB 2
  36. #define TNETD7200_DEF_CPU_CLK 211000000
  37. #define TNETD7200_DEF_DSP_CLK 125000000
  38. #define TNETD7200_DEF_USB_CLK 48000000
  39. struct tnetd7300_clock {
  40. u32 ctrl;
  41. #define PREDIV_MASK 0x001f0000
  42. #define PREDIV_SHIFT 16
  43. #define POSTDIV_MASK 0x0000001f
  44. u32 unused1[3];
  45. u32 pll;
  46. #define MUL_MASK 0x0000f000
  47. #define MUL_SHIFT 12
  48. #define PLL_MODE_MASK 0x00000001
  49. #define PLL_NDIV 0x00000800
  50. #define PLL_DIV 0x00000002
  51. #define PLL_STATUS 0x00000001
  52. u32 unused2[3];
  53. };
  54. struct tnetd7300_clocks {
  55. struct tnetd7300_clock bus;
  56. struct tnetd7300_clock cpu;
  57. struct tnetd7300_clock usb;
  58. struct tnetd7300_clock dsp;
  59. };
  60. struct tnetd7200_clock {
  61. u32 ctrl;
  62. u32 unused1[3];
  63. #define DIVISOR_ENABLE_MASK 0x00008000
  64. u32 mul;
  65. u32 prediv;
  66. u32 postdiv;
  67. u32 postdiv2;
  68. u32 unused2[6];
  69. u32 cmd;
  70. u32 status;
  71. u32 cmden;
  72. u32 padding[15];
  73. };
  74. struct tnetd7200_clocks {
  75. struct tnetd7200_clock cpu;
  76. struct tnetd7200_clock dsp;
  77. struct tnetd7200_clock usb;
  78. };
  79. struct clk_rate {
  80. u32 rate;
  81. };
  82. static struct clk_rate bus_clk = {
  83. .rate = 125000000,
  84. };
  85. static struct clk_rate cpu_clk = {
  86. .rate = 150000000,
  87. };
  88. static void approximate(int base, int target, int *prediv,
  89. int *postdiv, int *mul)
  90. {
  91. int i, j, k, freq, res = target;
  92. for (i = 1; i <= 16; i++)
  93. for (j = 1; j <= 32; j++)
  94. for (k = 1; k <= 32; k++) {
  95. freq = abs(base / j * i / k - target);
  96. if (freq < res) {
  97. res = freq;
  98. *mul = i;
  99. *prediv = j;
  100. *postdiv = k;
  101. }
  102. }
  103. }
  104. static void calculate(int base, int target, int *prediv, int *postdiv,
  105. int *mul)
  106. {
  107. int tmp_gcd, tmp_base, tmp_freq;
  108. for (*prediv = 1; *prediv <= 32; (*prediv)++) {
  109. tmp_base = base / *prediv;
  110. tmp_gcd = gcd(target, tmp_base);
  111. *mul = target / tmp_gcd;
  112. *postdiv = tmp_base / tmp_gcd;
  113. if ((*mul < 1) || (*mul >= 16))
  114. continue;
  115. if ((*postdiv > 0) & (*postdiv <= 32))
  116. break;
  117. }
  118. if (base / *prediv * *mul / *postdiv != target) {
  119. approximate(base, target, prediv, postdiv, mul);
  120. tmp_freq = base / *prediv * *mul / *postdiv;
  121. printk(KERN_WARNING
  122. "Adjusted requested frequency %d to %d\n",
  123. target, tmp_freq);
  124. }
  125. printk(KERN_DEBUG "Clocks: prediv: %d, postdiv: %d, mul: %d\n",
  126. *prediv, *postdiv, *mul);
  127. }
  128. static int tnetd7300_dsp_clock(void)
  129. {
  130. u32 didr1, didr2;
  131. u8 rev = ar7_chip_rev();
  132. didr1 = readl((void *)KSEG1ADDR(AR7_REGS_GPIO + 0x18));
  133. didr2 = readl((void *)KSEG1ADDR(AR7_REGS_GPIO + 0x1c));
  134. if (didr2 & (1 << 23))
  135. return 0;
  136. if ((rev >= 0x23) && (rev != 0x57))
  137. return 250000000;
  138. if ((((didr2 & 0x1fff) << 10) | ((didr1 & 0xffc00000) >> 22))
  139. > 4208000)
  140. return 250000000;
  141. return 0;
  142. }
  143. static int tnetd7300_get_clock(u32 shift, struct tnetd7300_clock *clock,
  144. u32 *bootcr, u32 bus_clock)
  145. {
  146. int product;
  147. int base_clock = AR7_REF_CLOCK;
  148. u32 ctrl = readl(&clock->ctrl);
  149. u32 pll = readl(&clock->pll);
  150. int prediv = ((ctrl & PREDIV_MASK) >> PREDIV_SHIFT) + 1;
  151. int postdiv = (ctrl & POSTDIV_MASK) + 1;
  152. int divisor = prediv * postdiv;
  153. int mul = ((pll & MUL_MASK) >> MUL_SHIFT) + 1;
  154. switch ((*bootcr & (BOOT_PLL_SOURCE_MASK << shift)) >> shift) {
  155. case BOOT_PLL_SOURCE_BUS:
  156. base_clock = bus_clock;
  157. break;
  158. case BOOT_PLL_SOURCE_REF:
  159. base_clock = AR7_REF_CLOCK;
  160. break;
  161. case BOOT_PLL_SOURCE_XTAL:
  162. base_clock = AR7_XTAL_CLOCK;
  163. break;
  164. case BOOT_PLL_SOURCE_CPU:
  165. base_clock = cpu_clk.rate;
  166. break;
  167. }
  168. if (*bootcr & BOOT_PLL_BYPASS)
  169. return base_clock / divisor;
  170. if ((pll & PLL_MODE_MASK) == 0)
  171. return (base_clock >> (mul / 16 + 1)) / divisor;
  172. if ((pll & (PLL_NDIV | PLL_DIV)) == (PLL_NDIV | PLL_DIV)) {
  173. product = (mul & 1) ?
  174. (base_clock * mul) >> 1 :
  175. (base_clock * (mul - 1)) >> 2;
  176. return product / divisor;
  177. }
  178. if (mul == 16)
  179. return base_clock / divisor;
  180. return base_clock * mul / divisor;
  181. }
  182. static void tnetd7300_set_clock(u32 shift, struct tnetd7300_clock *clock,
  183. u32 *bootcr, u32 frequency)
  184. {
  185. int prediv, postdiv, mul;
  186. int base_clock = bus_clk.rate;
  187. switch ((*bootcr & (BOOT_PLL_SOURCE_MASK << shift)) >> shift) {
  188. case BOOT_PLL_SOURCE_BUS:
  189. base_clock = bus_clk.rate;
  190. break;
  191. case BOOT_PLL_SOURCE_REF:
  192. base_clock = AR7_REF_CLOCK;
  193. break;
  194. case BOOT_PLL_SOURCE_XTAL:
  195. base_clock = AR7_XTAL_CLOCK;
  196. break;
  197. case BOOT_PLL_SOURCE_CPU:
  198. base_clock = cpu_clk.rate;
  199. break;
  200. }
  201. calculate(base_clock, frequency, &prediv, &postdiv, &mul);
  202. writel(((prediv - 1) << PREDIV_SHIFT) | (postdiv - 1), &clock->ctrl);
  203. mdelay(1);
  204. writel(4, &clock->pll);
  205. while (readl(&clock->pll) & PLL_STATUS)
  206. ;
  207. writel(((mul - 1) << MUL_SHIFT) | (0xff << 3) | 0x0e, &clock->pll);
  208. mdelay(75);
  209. }
  210. static void __init tnetd7300_init_clocks(void)
  211. {
  212. u32 *bootcr = (u32 *)ioremap(AR7_REGS_DCL, 4);
  213. struct tnetd7300_clocks *clocks =
  214. ioremap(UR8_REGS_CLOCKS,
  215. sizeof(struct tnetd7300_clocks));
  216. u32 dsp_clk;
  217. struct clk *clk;
  218. bus_clk.rate = tnetd7300_get_clock(BUS_PLL_SOURCE_SHIFT,
  219. &clocks->bus, bootcr, AR7_AFE_CLOCK);
  220. if (*bootcr & BOOT_PLL_ASYNC_MODE)
  221. cpu_clk.rate = tnetd7300_get_clock(CPU_PLL_SOURCE_SHIFT,
  222. &clocks->cpu, bootcr, AR7_AFE_CLOCK);
  223. else
  224. cpu_clk.rate = bus_clk.rate;
  225. dsp_clk = tnetd7300_dsp_clock();
  226. if (dsp_clk == 250000000)
  227. tnetd7300_set_clock(DSP_PLL_SOURCE_SHIFT, &clocks->dsp,
  228. bootcr, dsp_clk);
  229. iounmap(clocks);
  230. iounmap(bootcr);
  231. clk = clk_register_fixed_rate(NULL, "cpu", NULL, 0, cpu_clk.rate);
  232. clkdev_create(clk, "cpu", NULL);
  233. clk = clk_register_fixed_rate(NULL, "dsp", NULL, 0, dsp_clk);
  234. clkdev_create(clk, "dsp", NULL);
  235. }
  236. static void tnetd7200_set_clock(int base, struct tnetd7200_clock *clock,
  237. int prediv, int postdiv, int postdiv2, int mul, u32 frequency)
  238. {
  239. printk(KERN_INFO
  240. "Clocks: base = %d, frequency = %u, prediv = %d, "
  241. "postdiv = %d, postdiv2 = %d, mul = %d\n",
  242. base, frequency, prediv, postdiv, postdiv2, mul);
  243. writel(0, &clock->ctrl);
  244. writel(DIVISOR_ENABLE_MASK | ((prediv - 1) & 0x1F), &clock->prediv);
  245. writel((mul - 1) & 0xF, &clock->mul);
  246. while (readl(&clock->status) & 0x1)
  247. ; /* nop */
  248. writel(DIVISOR_ENABLE_MASK | ((postdiv - 1) & 0x1F), &clock->postdiv);
  249. writel(readl(&clock->cmden) | 1, &clock->cmden);
  250. writel(readl(&clock->cmd) | 1, &clock->cmd);
  251. while (readl(&clock->status) & 0x1)
  252. ; /* nop */
  253. writel(DIVISOR_ENABLE_MASK | ((postdiv2 - 1) & 0x1F), &clock->postdiv2);
  254. writel(readl(&clock->cmden) | 1, &clock->cmden);
  255. writel(readl(&clock->cmd) | 1, &clock->cmd);
  256. while (readl(&clock->status) & 0x1)
  257. ; /* nop */
  258. writel(readl(&clock->ctrl) | 1, &clock->ctrl);
  259. }
  260. static int tnetd7200_get_clock_base(int clock_id, u32 *bootcr)
  261. {
  262. if (*bootcr & BOOT_PLL_ASYNC_MODE)
  263. /* Async */
  264. switch (clock_id) {
  265. case TNETD7200_CLOCK_ID_DSP:
  266. return AR7_REF_CLOCK;
  267. default:
  268. return AR7_AFE_CLOCK;
  269. }
  270. else
  271. /* Sync */
  272. if (*bootcr & BOOT_PLL_2TO1_MODE)
  273. /* 2:1 */
  274. switch (clock_id) {
  275. case TNETD7200_CLOCK_ID_DSP:
  276. return AR7_REF_CLOCK;
  277. default:
  278. return AR7_AFE_CLOCK;
  279. }
  280. else
  281. /* 1:1 */
  282. return AR7_REF_CLOCK;
  283. }
  284. static void __init tnetd7200_init_clocks(void)
  285. {
  286. u32 *bootcr = (u32 *)ioremap(AR7_REGS_DCL, 4);
  287. struct tnetd7200_clocks *clocks =
  288. ioremap(AR7_REGS_CLOCKS,
  289. sizeof(struct tnetd7200_clocks));
  290. int cpu_base, cpu_mul, cpu_prediv, cpu_postdiv;
  291. int dsp_base, dsp_mul, dsp_prediv, dsp_postdiv;
  292. int usb_base, usb_mul, usb_prediv, usb_postdiv;
  293. struct clk *clk;
  294. cpu_base = tnetd7200_get_clock_base(TNETD7200_CLOCK_ID_CPU, bootcr);
  295. dsp_base = tnetd7200_get_clock_base(TNETD7200_CLOCK_ID_DSP, bootcr);
  296. if (*bootcr & BOOT_PLL_ASYNC_MODE) {
  297. printk(KERN_INFO "Clocks: Async mode\n");
  298. printk(KERN_INFO "Clocks: Setting DSP clock\n");
  299. calculate(dsp_base, TNETD7200_DEF_DSP_CLK,
  300. &dsp_prediv, &dsp_postdiv, &dsp_mul);
  301. bus_clk.rate =
  302. ((dsp_base / dsp_prediv) * dsp_mul) / dsp_postdiv;
  303. tnetd7200_set_clock(dsp_base, &clocks->dsp,
  304. dsp_prediv, dsp_postdiv * 2, dsp_postdiv, dsp_mul * 2,
  305. bus_clk.rate);
  306. printk(KERN_INFO "Clocks: Setting CPU clock\n");
  307. calculate(cpu_base, TNETD7200_DEF_CPU_CLK, &cpu_prediv,
  308. &cpu_postdiv, &cpu_mul);
  309. cpu_clk.rate =
  310. ((cpu_base / cpu_prediv) * cpu_mul) / cpu_postdiv;
  311. tnetd7200_set_clock(cpu_base, &clocks->cpu,
  312. cpu_prediv, cpu_postdiv, -1, cpu_mul,
  313. cpu_clk.rate);
  314. } else
  315. if (*bootcr & BOOT_PLL_2TO1_MODE) {
  316. printk(KERN_INFO "Clocks: Sync 2:1 mode\n");
  317. printk(KERN_INFO "Clocks: Setting CPU clock\n");
  318. calculate(cpu_base, TNETD7200_DEF_CPU_CLK, &cpu_prediv,
  319. &cpu_postdiv, &cpu_mul);
  320. cpu_clk.rate = ((cpu_base / cpu_prediv) * cpu_mul)
  321. / cpu_postdiv;
  322. tnetd7200_set_clock(cpu_base, &clocks->cpu,
  323. cpu_prediv, cpu_postdiv, -1, cpu_mul,
  324. cpu_clk.rate);
  325. printk(KERN_INFO "Clocks: Setting DSP clock\n");
  326. calculate(dsp_base, TNETD7200_DEF_DSP_CLK, &dsp_prediv,
  327. &dsp_postdiv, &dsp_mul);
  328. bus_clk.rate = cpu_clk.rate / 2;
  329. tnetd7200_set_clock(dsp_base, &clocks->dsp,
  330. dsp_prediv, dsp_postdiv * 2, dsp_postdiv,
  331. dsp_mul * 2, bus_clk.rate);
  332. } else {
  333. printk(KERN_INFO "Clocks: Sync 1:1 mode\n");
  334. printk(KERN_INFO "Clocks: Setting DSP clock\n");
  335. calculate(dsp_base, TNETD7200_DEF_DSP_CLK, &dsp_prediv,
  336. &dsp_postdiv, &dsp_mul);
  337. bus_clk.rate = ((dsp_base / dsp_prediv) * dsp_mul)
  338. / dsp_postdiv;
  339. tnetd7200_set_clock(dsp_base, &clocks->dsp,
  340. dsp_prediv, dsp_postdiv * 2, dsp_postdiv,
  341. dsp_mul * 2, bus_clk.rate);
  342. cpu_clk.rate = bus_clk.rate;
  343. }
  344. printk(KERN_INFO "Clocks: Setting USB clock\n");
  345. usb_base = bus_clk.rate;
  346. calculate(usb_base, TNETD7200_DEF_USB_CLK, &usb_prediv,
  347. &usb_postdiv, &usb_mul);
  348. tnetd7200_set_clock(usb_base, &clocks->usb,
  349. usb_prediv, usb_postdiv, -1, usb_mul,
  350. TNETD7200_DEF_USB_CLK);
  351. iounmap(clocks);
  352. iounmap(bootcr);
  353. clk = clk_register_fixed_rate(NULL, "cpu", NULL, 0, cpu_clk.rate);
  354. clkdev_create(clk, "cpu", NULL);
  355. clkdev_create(clk, "dsp", NULL);
  356. }
  357. void __init ar7_init_clocks(void)
  358. {
  359. struct clk *clk;
  360. switch (ar7_chip_id()) {
  361. case AR7_CHIP_7100:
  362. case AR7_CHIP_7200:
  363. tnetd7200_init_clocks();
  364. break;
  365. case AR7_CHIP_7300:
  366. tnetd7300_init_clocks();
  367. break;
  368. default:
  369. break;
  370. }
  371. clk = clk_register_fixed_rate(NULL, "bus", NULL, 0, bus_clk.rate);
  372. clkdev_create(clk, "bus", NULL);
  373. /* adjust vbus clock rate */
  374. clk = clk_register_fixed_factor(NULL, "vbus", "bus", 0, 1, 2);
  375. clkdev_create(clk, "vbus", NULL);
  376. clkdev_create(clk, "cpmac", "cpmac.1");
  377. clkdev_create(clk, "cpmac", "cpmac.1");
  378. }