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