sysctrl.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * This program is free software; you can redistribute it and/or modify it
  3. * under the terms of the GNU General Public License version 2 as published
  4. * by the Free Software Foundation.
  5. *
  6. * Copyright (C) 2011 Thomas Langer <thomas.langer@lantiq.com>
  7. * Copyright (C) 2011 John Crispin <john@phrozen.org>
  8. */
  9. #include <linux/ioport.h>
  10. #include <linux/export.h>
  11. #include <linux/clkdev.h>
  12. #include <linux/of_address.h>
  13. #include <asm/delay.h>
  14. #include <lantiq_soc.h>
  15. #include "../clk.h"
  16. /* infrastructure control register */
  17. #define SYS1_INFRAC 0x00bc
  18. /* Configuration fuses for drivers and pll */
  19. #define STATUS_CONFIG 0x0040
  20. /* GPE frequency selection */
  21. #define GPPC_OFFSET 24
  22. #define GPEFREQ_MASK 0x0000C00
  23. #define GPEFREQ_OFFSET 10
  24. /* Clock status register */
  25. #define SYSCTL_CLKS 0x0000
  26. /* Clock enable register */
  27. #define SYSCTL_CLKEN 0x0004
  28. /* Clock clear register */
  29. #define SYSCTL_CLKCLR 0x0008
  30. /* Activation Status Register */
  31. #define SYSCTL_ACTS 0x0020
  32. /* Activation Register */
  33. #define SYSCTL_ACT 0x0024
  34. /* Deactivation Register */
  35. #define SYSCTL_DEACT 0x0028
  36. /* reboot Register */
  37. #define SYSCTL_RBT 0x002c
  38. /* CPU0 Clock Control Register */
  39. #define SYS1_CPU0CC 0x0040
  40. /* HRST_OUT_N Control Register */
  41. #define SYS1_HRSTOUTC 0x00c0
  42. /* clock divider bit */
  43. #define CPU0CC_CPUDIV 0x0001
  44. /* Activation Status Register */
  45. #define ACTS_ASC0_ACT 0x00001000
  46. #define ACTS_SSC0 0x00002000
  47. #define ACTS_ASC1_ACT 0x00000800
  48. #define ACTS_I2C_ACT 0x00004000
  49. #define ACTS_P0 0x00010000
  50. #define ACTS_P1 0x00010000
  51. #define ACTS_P2 0x00020000
  52. #define ACTS_P3 0x00020000
  53. #define ACTS_P4 0x00040000
  54. #define ACTS_PADCTRL0 0x00100000
  55. #define ACTS_PADCTRL1 0x00100000
  56. #define ACTS_PADCTRL2 0x00200000
  57. #define ACTS_PADCTRL3 0x00200000
  58. #define ACTS_PADCTRL4 0x00400000
  59. #define sysctl_w32(m, x, y) ltq_w32((x), sysctl_membase[m] + (y))
  60. #define sysctl_r32(m, x) ltq_r32(sysctl_membase[m] + (x))
  61. #define sysctl_w32_mask(m, clear, set, reg) \
  62. sysctl_w32(m, (sysctl_r32(m, reg) & ~(clear)) | (set), reg)
  63. #define status_w32(x, y) ltq_w32((x), status_membase + (y))
  64. #define status_r32(x) ltq_r32(status_membase + (x))
  65. static void __iomem *sysctl_membase[3], *status_membase;
  66. void __iomem *ltq_sys1_membase, *ltq_ebu_membase;
  67. void falcon_trigger_hrst(int level)
  68. {
  69. sysctl_w32(SYSCTL_SYS1, level & 1, SYS1_HRSTOUTC);
  70. }
  71. static inline void sysctl_wait(struct clk *clk,
  72. unsigned int test, unsigned int reg)
  73. {
  74. int err = 1000000;
  75. do {} while (--err && ((sysctl_r32(clk->module, reg)
  76. & clk->bits) != test));
  77. if (!err)
  78. pr_err("module de/activation failed %d %08X %08X %08X\n",
  79. clk->module, clk->bits, test,
  80. sysctl_r32(clk->module, reg) & clk->bits);
  81. }
  82. static int sysctl_activate(struct clk *clk)
  83. {
  84. sysctl_w32(clk->module, clk->bits, SYSCTL_CLKEN);
  85. sysctl_w32(clk->module, clk->bits, SYSCTL_ACT);
  86. sysctl_wait(clk, clk->bits, SYSCTL_ACTS);
  87. return 0;
  88. }
  89. static void sysctl_deactivate(struct clk *clk)
  90. {
  91. sysctl_w32(clk->module, clk->bits, SYSCTL_CLKCLR);
  92. sysctl_w32(clk->module, clk->bits, SYSCTL_DEACT);
  93. sysctl_wait(clk, 0, SYSCTL_ACTS);
  94. }
  95. static int sysctl_clken(struct clk *clk)
  96. {
  97. sysctl_w32(clk->module, clk->bits, SYSCTL_CLKEN);
  98. sysctl_w32(clk->module, clk->bits, SYSCTL_ACT);
  99. sysctl_wait(clk, clk->bits, SYSCTL_CLKS);
  100. return 0;
  101. }
  102. static void sysctl_clkdis(struct clk *clk)
  103. {
  104. sysctl_w32(clk->module, clk->bits, SYSCTL_CLKCLR);
  105. sysctl_wait(clk, 0, SYSCTL_CLKS);
  106. }
  107. static void sysctl_reboot(struct clk *clk)
  108. {
  109. unsigned int act;
  110. unsigned int bits;
  111. act = sysctl_r32(clk->module, SYSCTL_ACT);
  112. bits = ~act & clk->bits;
  113. if (bits != 0) {
  114. sysctl_w32(clk->module, bits, SYSCTL_CLKEN);
  115. sysctl_w32(clk->module, bits, SYSCTL_ACT);
  116. sysctl_wait(clk, bits, SYSCTL_ACTS);
  117. }
  118. sysctl_w32(clk->module, act & clk->bits, SYSCTL_RBT);
  119. sysctl_wait(clk, clk->bits, SYSCTL_ACTS);
  120. }
  121. /* enable the ONU core */
  122. static void falcon_gpe_enable(void)
  123. {
  124. unsigned int freq;
  125. unsigned int status;
  126. /* if if the clock is already enabled */
  127. status = sysctl_r32(SYSCTL_SYS1, SYS1_INFRAC);
  128. if (status & (1 << (GPPC_OFFSET + 1)))
  129. return;
  130. freq = (status_r32(STATUS_CONFIG) &
  131. GPEFREQ_MASK) >>
  132. GPEFREQ_OFFSET;
  133. if (freq == 0)
  134. freq = 1; /* use 625MHz on unfused chip */
  135. /* apply new frequency */
  136. sysctl_w32_mask(SYSCTL_SYS1, 7 << (GPPC_OFFSET + 1),
  137. freq << (GPPC_OFFSET + 2) , SYS1_INFRAC);
  138. udelay(1);
  139. /* enable new frequency */
  140. sysctl_w32_mask(SYSCTL_SYS1, 0, 1 << (GPPC_OFFSET + 1), SYS1_INFRAC);
  141. udelay(1);
  142. }
  143. static inline void clkdev_add_sys(const char *dev, unsigned int module,
  144. unsigned int bits)
  145. {
  146. struct clk *clk = kzalloc(sizeof(struct clk), GFP_KERNEL);
  147. clk->cl.dev_id = dev;
  148. clk->cl.con_id = NULL;
  149. clk->cl.clk = clk;
  150. clk->module = module;
  151. clk->bits = bits;
  152. clk->activate = sysctl_activate;
  153. clk->deactivate = sysctl_deactivate;
  154. clk->enable = sysctl_clken;
  155. clk->disable = sysctl_clkdis;
  156. clk->reboot = sysctl_reboot;
  157. clkdev_add(&clk->cl);
  158. }
  159. void __init ltq_soc_init(void)
  160. {
  161. struct device_node *np_status =
  162. of_find_compatible_node(NULL, NULL, "lantiq,status-falcon");
  163. struct device_node *np_ebu =
  164. of_find_compatible_node(NULL, NULL, "lantiq,ebu-falcon");
  165. struct device_node *np_sys1 =
  166. of_find_compatible_node(NULL, NULL, "lantiq,sys1-falcon");
  167. struct device_node *np_syseth =
  168. of_find_compatible_node(NULL, NULL, "lantiq,syseth-falcon");
  169. struct device_node *np_sysgpe =
  170. of_find_compatible_node(NULL, NULL, "lantiq,sysgpe-falcon");
  171. struct resource res_status, res_ebu, res_sys[3];
  172. int i;
  173. /* check if all the core register ranges are available */
  174. if (!np_status || !np_ebu || !np_sys1 || !np_syseth || !np_sysgpe)
  175. panic("Failed to load core nodes from devicetree");
  176. if (of_address_to_resource(np_status, 0, &res_status) ||
  177. of_address_to_resource(np_ebu, 0, &res_ebu) ||
  178. of_address_to_resource(np_sys1, 0, &res_sys[0]) ||
  179. of_address_to_resource(np_syseth, 0, &res_sys[1]) ||
  180. of_address_to_resource(np_sysgpe, 0, &res_sys[2]))
  181. panic("Failed to get core resources");
  182. if ((request_mem_region(res_status.start, resource_size(&res_status),
  183. res_status.name) < 0) ||
  184. (request_mem_region(res_ebu.start, resource_size(&res_ebu),
  185. res_ebu.name) < 0) ||
  186. (request_mem_region(res_sys[0].start,
  187. resource_size(&res_sys[0]),
  188. res_sys[0].name) < 0) ||
  189. (request_mem_region(res_sys[1].start,
  190. resource_size(&res_sys[1]),
  191. res_sys[1].name) < 0) ||
  192. (request_mem_region(res_sys[2].start,
  193. resource_size(&res_sys[2]),
  194. res_sys[2].name) < 0))
  195. pr_err("Failed to request core resources");
  196. status_membase = ioremap_nocache(res_status.start,
  197. resource_size(&res_status));
  198. ltq_ebu_membase = ioremap_nocache(res_ebu.start,
  199. resource_size(&res_ebu));
  200. if (!status_membase || !ltq_ebu_membase)
  201. panic("Failed to remap core resources");
  202. for (i = 0; i < 3; i++) {
  203. sysctl_membase[i] = ioremap_nocache(res_sys[i].start,
  204. resource_size(&res_sys[i]));
  205. if (!sysctl_membase[i])
  206. panic("Failed to remap sysctrl resources");
  207. }
  208. ltq_sys1_membase = sysctl_membase[0];
  209. falcon_gpe_enable();
  210. /* get our 3 static rates for cpu, fpi and io clocks */
  211. if (ltq_sys1_r32(SYS1_CPU0CC) & CPU0CC_CPUDIV)
  212. clkdev_add_static(CLOCK_200M, CLOCK_100M, CLOCK_200M, 0);
  213. else
  214. clkdev_add_static(CLOCK_400M, CLOCK_100M, CLOCK_200M, 0);
  215. /* add our clock domains */
  216. clkdev_add_sys("1d810000.gpio", SYSCTL_SYSETH, ACTS_P0);
  217. clkdev_add_sys("1d810100.gpio", SYSCTL_SYSETH, ACTS_P2);
  218. clkdev_add_sys("1e800100.gpio", SYSCTL_SYS1, ACTS_P1);
  219. clkdev_add_sys("1e800200.gpio", SYSCTL_SYS1, ACTS_P3);
  220. clkdev_add_sys("1e800300.gpio", SYSCTL_SYS1, ACTS_P4);
  221. clkdev_add_sys("1db01000.pad", SYSCTL_SYSETH, ACTS_PADCTRL0);
  222. clkdev_add_sys("1db02000.pad", SYSCTL_SYSETH, ACTS_PADCTRL2);
  223. clkdev_add_sys("1e800400.pad", SYSCTL_SYS1, ACTS_PADCTRL1);
  224. clkdev_add_sys("1e800500.pad", SYSCTL_SYS1, ACTS_PADCTRL3);
  225. clkdev_add_sys("1e800600.pad", SYSCTL_SYS1, ACTS_PADCTRL4);
  226. clkdev_add_sys("1e100b00.serial", SYSCTL_SYS1, ACTS_ASC1_ACT);
  227. clkdev_add_sys("1e100c00.serial", SYSCTL_SYS1, ACTS_ASC0_ACT);
  228. clkdev_add_sys("1e100d00.spi", SYSCTL_SYS1, ACTS_SSC0);
  229. clkdev_add_sys("1e200000.i2c", SYSCTL_SYS1, ACTS_I2C_ACT);
  230. }