blob: 9e958343a719cdc9f45b8e004ef186b7474a0d7b [file] [log] [blame]
Suman Anna54d66222019-08-13 04:13:32 -07001// SPDX-License-Identifier: GPL-2.0
Tony Lindgren0eecc632017-10-10 14:23:43 -07002/*
3 * ti-sysc.c - Texas Instruments sysc interconnect target driver
Tony Lindgren0eecc632017-10-10 14:23:43 -07004 */
5
6#include <linux/io.h>
7#include <linux/clk.h>
Tony Lindgren2c355ff2018-02-22 13:58:03 -08008#include <linux/clkdev.h>
Tony Lindgrena885f0f2018-02-22 14:03:48 -08009#include <linux/delay.h>
Tony Lindgrenfeaa8ba2020-02-24 12:58:03 -080010#include <linux/list.h>
Tony Lindgren0eecc632017-10-10 14:23:43 -070011#include <linux/module.h>
12#include <linux/platform_device.h>
Tony Lindgrena885f0f2018-02-22 14:03:48 -080013#include <linux/pm_domain.h>
Tony Lindgren0eecc632017-10-10 14:23:43 -070014#include <linux/pm_runtime.h>
Tony Lindgren50622362018-04-16 10:20:27 -070015#include <linux/reset.h>
Tony Lindgren0eecc632017-10-10 14:23:43 -070016#include <linux/of_address.h>
17#include <linux/of_platform.h>
Tony Lindgren2c355ff2018-02-22 13:58:03 -080018#include <linux/slab.h>
Tony Lindgrenfeaa8ba2020-02-24 12:58:03 -080019#include <linux/sys_soc.h>
Tony Lindgrenb3e94312021-09-21 12:42:25 +030020#include <linux/timekeeping.h>
Faiz Abbas596e7952018-07-09 22:18:39 +053021#include <linux/iopoll.h>
Tony Lindgren2c355ff2018-02-22 13:58:03 -080022
Tony Lindgren70a65242017-12-15 09:41:09 -080023#include <linux/platform_data/ti-sysc.h>
24
25#include <dt-bindings/bus/ti-sysc.h>
Tony Lindgren0eecc632017-10-10 14:23:43 -070026
Tony Lindgrenfeaa8ba2020-02-24 12:58:03 -080027#define DIS_ISP BIT(2)
28#define DIS_IVA BIT(1)
29#define DIS_SGX BIT(0)
30
31#define SOC_FLAG(match, flag) { .machine = match, .data = (void *)(flag), }
32
Tony Lindgrene4a8fc02020-06-10 15:31:54 -070033#define MAX_MODULE_SOFTRESET_WAIT 10000
Faiz Abbas596e7952018-07-09 22:18:39 +053034
Tony Lindgrenfeaa8ba2020-02-24 12:58:03 -080035enum sysc_soc {
36 SOC_UNKNOWN,
37 SOC_2420,
38 SOC_2430,
39 SOC_3430,
40 SOC_3630,
41 SOC_4430,
42 SOC_4460,
43 SOC_4470,
44 SOC_5430,
45 SOC_AM3,
46 SOC_AM4,
47 SOC_DRA7,
48};
49
50struct sysc_address {
51 unsigned long base;
52 struct list_head node;
53};
54
55struct sysc_soc_info {
56 unsigned long general_purpose:1;
57 enum sysc_soc soc;
58 struct mutex list_lock; /* disabled modules list lock */
59 struct list_head disabled_modules;
60};
Tony Lindgren0eecc632017-10-10 14:23:43 -070061
62enum sysc_clocks {
63 SYSC_FCK,
64 SYSC_ICK,
Tony Lindgren09dfe582018-04-16 10:25:52 -070065 SYSC_OPTFCK0,
66 SYSC_OPTFCK1,
67 SYSC_OPTFCK2,
68 SYSC_OPTFCK3,
69 SYSC_OPTFCK4,
70 SYSC_OPTFCK5,
71 SYSC_OPTFCK6,
72 SYSC_OPTFCK7,
Tony Lindgren0eecc632017-10-10 14:23:43 -070073 SYSC_MAX_CLOCKS,
74};
75
Tony Lindgrenfeaa8ba2020-02-24 12:58:03 -080076static struct sysc_soc_info *sysc_soc;
77static const char * const reg_names[] = { "rev", "sysc", "syss", };
Tony Lindgrena54275f2019-03-21 11:00:21 -070078static const char * const clock_names[SYSC_MAX_CLOCKS] = {
79 "fck", "ick", "opt0", "opt1", "opt2", "opt3", "opt4",
80 "opt5", "opt6", "opt7",
81};
Tony Lindgren0eecc632017-10-10 14:23:43 -070082
Tony Lindgrenc5a2de92017-12-15 09:41:23 -080083#define SYSC_IDLEMODE_MASK 3
84#define SYSC_CLOCKACTIVITY_MASK 3
85
Tony Lindgren0eecc632017-10-10 14:23:43 -070086/**
87 * struct sysc - TI sysc interconnect target module registers and capabilities
88 * @dev: struct device pointer
89 * @module_pa: physical address of the interconnect target module
90 * @module_size: size of the interconnect target module
91 * @module_va: virtual address of the interconnect target module
92 * @offsets: register offsets from module base
Suman Annab58056d2019-08-13 04:13:32 -070093 * @mdata: ti-sysc to hwmod translation data for a module
Tony Lindgren0eecc632017-10-10 14:23:43 -070094 * @clocks: clocks used by the interconnect target module
Tony Lindgren09dfe582018-04-16 10:25:52 -070095 * @clock_roles: clock role names for the found clocks
96 * @nr_clocks: number of clocks used by the interconnect target module
Suman Annab58056d2019-08-13 04:13:32 -070097 * @rsts: resets used by the interconnect target module
Tony Lindgren0eecc632017-10-10 14:23:43 -070098 * @legacy_mode: configured for legacy mode if set
Tony Lindgren70a65242017-12-15 09:41:09 -080099 * @cap: interconnect target module capabilities
100 * @cfg: interconnect target module configuration
Suman Annab58056d2019-08-13 04:13:32 -0700101 * @cookie: data used by legacy platform callbacks
Tony Lindgren566a9b052017-12-15 09:41:19 -0800102 * @name: name if available
103 * @revision: interconnect target module revision
Tony Lindgren3ff340e2021-06-11 08:42:50 +0300104 * @reserved: target module is reserved and already in use
Suman Annab58056d2019-08-13 04:13:32 -0700105 * @enabled: sysc runtime enabled status
Tony Lindgren62020f22018-02-22 13:59:44 -0800106 * @needs_resume: runtime resume needed on resume from suspend
Suman Annab58056d2019-08-13 04:13:32 -0700107 * @child_needs_resume: runtime resume needed for child on resume from suspend
108 * @disable_on_idle: status flag used for disabling modules with resets
109 * @idle_work: work structure used to perform delayed idle on a module
Tony Lindgrene64c0212020-02-24 12:58:03 -0800110 * @pre_reset_quirk: module specific pre-reset quirk
111 * @post_reset_quirk: module specific post-reset quirk
Tony Lindgren4e23be42019-06-10 04:48:05 -0700112 * @reset_done_quirk: module specific reset done quirk
Tony Lindgrend7f563d2019-08-14 05:18:16 -0700113 * @module_enable_quirk: module specific enable quirk
Tony Lindgrenc7d86692019-10-17 11:21:44 -0700114 * @module_disable_quirk: module specific disable quirk
Tony Lindgrene8639e12020-02-24 12:58:03 -0800115 * @module_unlock_quirk: module specific sysconfig unlock quirk
116 * @module_lock_quirk: module specific sysconfig lock quirk
Tony Lindgren0eecc632017-10-10 14:23:43 -0700117 */
118struct sysc {
119 struct device *dev;
120 u64 module_pa;
121 u32 module_size;
122 void __iomem *module_va;
123 int offsets[SYSC_MAX_REGS];
Tony Lindgrena3e92e72019-03-21 11:00:21 -0700124 struct ti_sysc_module_data *mdata;
Tony Lindgren09dfe582018-04-16 10:25:52 -0700125 struct clk **clocks;
126 const char **clock_roles;
127 int nr_clocks;
Tony Lindgren50622362018-04-16 10:20:27 -0700128 struct reset_control *rsts;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700129 const char *legacy_mode;
Tony Lindgren70a65242017-12-15 09:41:09 -0800130 const struct sysc_capabilities *cap;
131 struct sysc_config cfg;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800132 struct ti_sysc_cookie cookie;
Tony Lindgren566a9b052017-12-15 09:41:19 -0800133 const char *name;
134 u32 revision;
Tony Lindgren95ec14fa2021-09-21 12:42:25 +0300135 u32 sysconfig;
Tony Lindgren3ff340e2021-06-11 08:42:50 +0300136 unsigned int reserved:1;
Tony Lindgren8383e252019-05-27 04:51:56 -0700137 unsigned int enabled:1;
138 unsigned int needs_resume:1;
139 unsigned int child_needs_resume:1;
Tony Lindgren76f0f772018-02-23 08:28:45 -0800140 struct delayed_work idle_work;
Tony Lindgrene64c0212020-02-24 12:58:03 -0800141 void (*pre_reset_quirk)(struct sysc *sysc);
142 void (*post_reset_quirk)(struct sysc *sysc);
Tony Lindgren4e23be42019-06-10 04:48:05 -0700143 void (*reset_done_quirk)(struct sysc *sysc);
Tony Lindgrend7f563d2019-08-14 05:18:16 -0700144 void (*module_enable_quirk)(struct sysc *sysc);
Tony Lindgrenc7d86692019-10-17 11:21:44 -0700145 void (*module_disable_quirk)(struct sysc *sysc);
Tony Lindgrene8639e12020-02-24 12:58:03 -0800146 void (*module_unlock_quirk)(struct sysc *sysc);
147 void (*module_lock_quirk)(struct sysc *sysc);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700148};
149
Tony Lindgren4014c082018-12-10 14:11:26 -0800150static void sysc_parse_dts_quirks(struct sysc *ddata, struct device_node *np,
151 bool is_child);
152
Tony Lindgrenb7182b42019-03-21 11:00:21 -0700153static void sysc_write(struct sysc *ddata, int offset, u32 value)
Faiz Abbas596e7952018-07-09 22:18:39 +0530154{
Tony Lindgren5aa91292019-05-27 04:51:53 -0700155 if (ddata->cfg.quirks & SYSC_QUIRK_16BIT) {
156 writew_relaxed(value & 0xffff, ddata->module_va + offset);
157
158 /* Only i2c revision has LO and HI register with stride of 4 */
159 if (ddata->offsets[SYSC_REVISION] >= 0 &&
160 offset == ddata->offsets[SYSC_REVISION]) {
161 u16 hi = value >> 16;
162
163 writew_relaxed(hi, ddata->module_va + offset + 4);
164 }
165
166 return;
167 }
168
Faiz Abbas596e7952018-07-09 22:18:39 +0530169 writel_relaxed(value, ddata->module_va + offset);
170}
171
Tony Lindgren566a9b052017-12-15 09:41:19 -0800172static u32 sysc_read(struct sysc *ddata, int offset)
173{
174 if (ddata->cfg.quirks & SYSC_QUIRK_16BIT) {
175 u32 val;
176
177 val = readw_relaxed(ddata->module_va + offset);
Tony Lindgren5aa91292019-05-27 04:51:53 -0700178
179 /* Only i2c revision has LO and HI register with stride of 4 */
180 if (ddata->offsets[SYSC_REVISION] >= 0 &&
181 offset == ddata->offsets[SYSC_REVISION]) {
182 u16 tmp = readw_relaxed(ddata->module_va + offset + 4);
183
184 val |= tmp << 16;
185 }
Tony Lindgren566a9b052017-12-15 09:41:19 -0800186
187 return val;
188 }
189
190 return readl_relaxed(ddata->module_va + offset);
191}
192
Tony Lindgren09dfe582018-04-16 10:25:52 -0700193static bool sysc_opt_clks_needed(struct sysc *ddata)
194{
195 return !!(ddata->cfg.quirks & SYSC_QUIRK_OPT_CLKS_NEEDED);
196}
197
Tony Lindgren0eecc632017-10-10 14:23:43 -0700198static u32 sysc_read_revision(struct sysc *ddata)
199{
Tony Lindgren566a9b052017-12-15 09:41:19 -0800200 int offset = ddata->offsets[SYSC_REVISION];
201
202 if (offset < 0)
203 return 0;
204
205 return sysc_read(ddata, offset);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700206}
207
Tony Lindgrene0db94f2019-05-27 04:51:53 -0700208static u32 sysc_read_sysconfig(struct sysc *ddata)
209{
210 int offset = ddata->offsets[SYSC_SYSCONFIG];
211
212 if (offset < 0)
213 return 0;
214
215 return sysc_read(ddata, offset);
216}
217
218static u32 sysc_read_sysstatus(struct sysc *ddata)
219{
220 int offset = ddata->offsets[SYSC_SYSSTATUS];
221
222 if (offset < 0)
223 return 0;
224
225 return sysc_read(ddata, offset);
226}
227
Tony Lindgrenb3e94312021-09-21 12:42:25 +0300228static int sysc_poll_reset_sysstatus(struct sysc *ddata)
Tony Lindgrend46f9fb2020-05-31 12:37:54 -0700229{
Tony Lindgrenb3e94312021-09-21 12:42:25 +0300230 int error, retries;
231 u32 syss_done, rstval;
Tony Lindgrend46f9fb2020-05-31 12:37:54 -0700232
233 if (ddata->cfg.quirks & SYSS_QUIRK_RESETDONE_INVERTED)
234 syss_done = 0;
235 else
236 syss_done = ddata->cfg.syss_mask;
237
Tony Lindgrenb3e94312021-09-21 12:42:25 +0300238 if (likely(!timekeeping_suspended)) {
Tony Lindgren9f911392020-07-02 10:44:20 -0700239 error = readx_poll_timeout_atomic(sysc_read_sysstatus, ddata,
240 rstval, (rstval & ddata->cfg.syss_mask) ==
241 syss_done, 100, MAX_MODULE_SOFTRESET_WAIT);
Tony Lindgrenb3e94312021-09-21 12:42:25 +0300242 } else {
243 retries = MAX_MODULE_SOFTRESET_WAIT;
244 while (retries--) {
245 rstval = sysc_read_sysstatus(ddata);
246 if ((rstval & ddata->cfg.syss_mask) == syss_done)
247 return 0;
248 udelay(2); /* Account for udelay flakeyness */
249 }
250 error = -ETIMEDOUT;
251 }
Tony Lindgrend46f9fb2020-05-31 12:37:54 -0700252
Tony Lindgrenb3e94312021-09-21 12:42:25 +0300253 return error;
254}
255
256static int sysc_poll_reset_sysconfig(struct sysc *ddata)
257{
258 int error, retries;
259 u32 sysc_mask, rstval;
260
261 sysc_mask = BIT(ddata->cap->regbits->srst_shift);
262
263 if (likely(!timekeeping_suspended)) {
Tony Lindgren9f911392020-07-02 10:44:20 -0700264 error = readx_poll_timeout_atomic(sysc_read_sysconfig, ddata,
265 rstval, !(rstval & sysc_mask),
266 100, MAX_MODULE_SOFTRESET_WAIT);
Tony Lindgrenb3e94312021-09-21 12:42:25 +0300267 } else {
268 retries = MAX_MODULE_SOFTRESET_WAIT;
269 while (retries--) {
270 rstval = sysc_read_sysconfig(ddata);
271 if (!(rstval & sysc_mask))
272 return 0;
273 udelay(2); /* Account for udelay flakeyness */
274 }
275 error = -ETIMEDOUT;
Tony Lindgrend46f9fb2020-05-31 12:37:54 -0700276 }
277
278 return error;
279}
280
Tony Lindgrenb3e94312021-09-21 12:42:25 +0300281/* Poll on reset status */
282static int sysc_wait_softreset(struct sysc *ddata)
283{
284 int syss_offset, error = 0;
285
286 if (ddata->cap->regbits->srst_shift < 0)
287 return 0;
288
289 syss_offset = ddata->offsets[SYSC_SYSSTATUS];
290
291 if (syss_offset >= 0)
292 error = sysc_poll_reset_sysstatus(ddata);
293 else if (ddata->cfg.quirks & SYSC_QUIRK_RESET_STATUS)
294 error = sysc_poll_reset_sysconfig(ddata);
295
296 return error;
297}
298
Tony Lindgrena54275f2019-03-21 11:00:21 -0700299static int sysc_add_named_clock_from_child(struct sysc *ddata,
300 const char *name,
301 const char *optfck_name)
302{
303 struct device_node *np = ddata->dev->of_node;
304 struct device_node *child;
305 struct clk_lookup *cl;
306 struct clk *clock;
307 const char *n;
308
309 if (name)
310 n = name;
311 else
312 n = optfck_name;
313
314 /* Does the clock alias already exist? */
315 clock = of_clk_get_by_name(np, n);
316 if (!IS_ERR(clock)) {
317 clk_put(clock);
318
319 return 0;
320 }
321
322 child = of_get_next_available_child(np, NULL);
323 if (!child)
324 return -ENODEV;
325
326 clock = devm_get_clk_from_child(ddata->dev, child, name);
327 if (IS_ERR(clock))
328 return PTR_ERR(clock);
329
330 /*
331 * Use clkdev_add() instead of clkdev_alloc() to avoid the MAX_DEV_ID
332 * limit for clk_get(). If cl ever needs to be freed, it should be done
333 * with clkdev_drop().
334 */
Zheng Yongjund995d3d2020-12-29 21:51:47 +0800335 cl = kzalloc(sizeof(*cl), GFP_KERNEL);
Tony Lindgrena54275f2019-03-21 11:00:21 -0700336 if (!cl)
337 return -ENOMEM;
338
339 cl->con_id = n;
340 cl->dev_id = dev_name(ddata->dev);
341 cl->clk = clock;
342 clkdev_add(cl);
343
344 clk_put(clock);
345
346 return 0;
347}
348
349static int sysc_init_ext_opt_clock(struct sysc *ddata, const char *name)
350{
351 const char *optfck_name;
352 int error, index;
353
354 if (ddata->nr_clocks < SYSC_OPTFCK0)
355 index = SYSC_OPTFCK0;
356 else
357 index = ddata->nr_clocks;
358
359 if (name)
360 optfck_name = name;
361 else
362 optfck_name = clock_names[index];
363
364 error = sysc_add_named_clock_from_child(ddata, name, optfck_name);
365 if (error)
366 return error;
367
368 ddata->clock_roles[index] = optfck_name;
369 ddata->nr_clocks++;
370
371 return 0;
372}
373
Tony Lindgren09dfe582018-04-16 10:25:52 -0700374static int sysc_get_one_clock(struct sysc *ddata, const char *name)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700375{
Tony Lindgren09dfe582018-04-16 10:25:52 -0700376 int error, i, index = -ENODEV;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700377
Tony Lindgren09dfe582018-04-16 10:25:52 -0700378 if (!strncmp(clock_names[SYSC_FCK], name, 3))
379 index = SYSC_FCK;
380 else if (!strncmp(clock_names[SYSC_ICK], name, 3))
381 index = SYSC_ICK;
382
383 if (index < 0) {
384 for (i = SYSC_OPTFCK0; i < SYSC_MAX_CLOCKS; i++) {
Tony Lindgrenc97c8622018-05-18 07:54:44 -0700385 if (!ddata->clocks[i]) {
Tony Lindgren09dfe582018-04-16 10:25:52 -0700386 index = i;
387 break;
388 }
389 }
Tony Lindgren0eecc632017-10-10 14:23:43 -0700390 }
Tony Lindgren09dfe582018-04-16 10:25:52 -0700391
392 if (index < 0) {
393 dev_err(ddata->dev, "clock %s not added\n", name);
394 return index;
395 }
Tony Lindgren0eecc632017-10-10 14:23:43 -0700396
397 ddata->clocks[index] = devm_clk_get(ddata->dev, name);
398 if (IS_ERR(ddata->clocks[index])) {
Tony Lindgren0eecc632017-10-10 14:23:43 -0700399 dev_err(ddata->dev, "clock get error for %s: %li\n",
400 name, PTR_ERR(ddata->clocks[index]));
401
402 return PTR_ERR(ddata->clocks[index]);
403 }
404
405 error = clk_prepare(ddata->clocks[index]);
406 if (error) {
407 dev_err(ddata->dev, "clock prepare error for %s: %i\n",
408 name, error);
409
410 return error;
411 }
412
413 return 0;
414}
415
416static int sysc_get_clocks(struct sysc *ddata)
417{
Tony Lindgren09dfe582018-04-16 10:25:52 -0700418 struct device_node *np = ddata->dev->of_node;
419 struct property *prop;
420 const char *name;
421 int nr_fck = 0, nr_ick = 0, i, error = 0;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700422
Kees Cook20749052018-06-18 10:45:49 -0700423 ddata->clock_roles = devm_kcalloc(ddata->dev,
Tony Lindgren09dfe582018-04-16 10:25:52 -0700424 SYSC_MAX_CLOCKS,
Kees Cook20749052018-06-18 10:45:49 -0700425 sizeof(*ddata->clock_roles),
Tony Lindgren09dfe582018-04-16 10:25:52 -0700426 GFP_KERNEL);
427 if (!ddata->clock_roles)
428 return -ENOMEM;
429
430 of_property_for_each_string(np, "clock-names", prop, name) {
431 if (!strncmp(clock_names[SYSC_FCK], name, 3))
432 nr_fck++;
433 if (!strncmp(clock_names[SYSC_ICK], name, 3))
434 nr_ick++;
435 ddata->clock_roles[ddata->nr_clocks] = name;
436 ddata->nr_clocks++;
437 }
438
439 if (ddata->nr_clocks < 1)
440 return 0;
441
Tony Lindgrena54275f2019-03-21 11:00:21 -0700442 if ((ddata->cfg.quirks & SYSC_QUIRK_EXT_OPT_CLOCK)) {
443 error = sysc_init_ext_opt_clock(ddata, NULL);
444 if (error)
445 return error;
446 }
447
Tony Lindgren09dfe582018-04-16 10:25:52 -0700448 if (ddata->nr_clocks > SYSC_MAX_CLOCKS) {
449 dev_err(ddata->dev, "too many clocks for %pOF\n", np);
450
451 return -EINVAL;
452 }
453
454 if (nr_fck > 1 || nr_ick > 1) {
455 dev_err(ddata->dev, "max one fck and ick for %pOF\n", np);
456
457 return -EINVAL;
458 }
459
Tony Lindgren2c81f0f2019-12-16 14:41:53 -0800460 /* Always add a slot for main clocks fck and ick even if unused */
461 if (!nr_fck)
462 ddata->nr_clocks++;
463 if (!nr_ick)
464 ddata->nr_clocks++;
465
Kees Cook20749052018-06-18 10:45:49 -0700466 ddata->clocks = devm_kcalloc(ddata->dev,
467 ddata->nr_clocks, sizeof(*ddata->clocks),
Tony Lindgren09dfe582018-04-16 10:25:52 -0700468 GFP_KERNEL);
469 if (!ddata->clocks)
470 return -ENOMEM;
471
Tony Lindgren7b4f8ac2018-11-15 14:46:52 -0800472 for (i = 0; i < SYSC_MAX_CLOCKS; i++) {
473 const char *name = ddata->clock_roles[i];
474
475 if (!name)
476 continue;
477
478 error = sysc_get_one_clock(ddata, name);
Tony Lindgren2783d062019-09-05 13:01:29 -0700479 if (error)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700480 return error;
481 }
482
483 return 0;
484}
485
Tony Lindgrend8789702019-03-21 11:00:21 -0700486static int sysc_enable_main_clocks(struct sysc *ddata)
487{
488 struct clk *clock;
489 int i, error;
490
491 if (!ddata->clocks)
492 return 0;
493
494 for (i = 0; i < SYSC_OPTFCK0; i++) {
495 clock = ddata->clocks[i];
496
497 /* Main clocks may not have ick */
498 if (IS_ERR_OR_NULL(clock))
499 continue;
500
501 error = clk_enable(clock);
502 if (error)
503 goto err_disable;
504 }
505
506 return 0;
507
508err_disable:
509 for (i--; i >= 0; i--) {
510 clock = ddata->clocks[i];
511
512 /* Main clocks may not have ick */
513 if (IS_ERR_OR_NULL(clock))
514 continue;
515
516 clk_disable(clock);
517 }
518
519 return error;
520}
521
522static void sysc_disable_main_clocks(struct sysc *ddata)
523{
524 struct clk *clock;
525 int i;
526
527 if (!ddata->clocks)
528 return;
529
530 for (i = 0; i < SYSC_OPTFCK0; i++) {
531 clock = ddata->clocks[i];
532 if (IS_ERR_OR_NULL(clock))
533 continue;
534
535 clk_disable(clock);
536 }
537}
538
539static int sysc_enable_opt_clocks(struct sysc *ddata)
540{
541 struct clk *clock;
542 int i, error;
543
Tony Lindgren2c81f0f2019-12-16 14:41:53 -0800544 if (!ddata->clocks || ddata->nr_clocks < SYSC_OPTFCK0 + 1)
Tony Lindgrend8789702019-03-21 11:00:21 -0700545 return 0;
546
547 for (i = SYSC_OPTFCK0; i < SYSC_MAX_CLOCKS; i++) {
548 clock = ddata->clocks[i];
549
550 /* Assume no holes for opt clocks */
551 if (IS_ERR_OR_NULL(clock))
552 return 0;
553
554 error = clk_enable(clock);
555 if (error)
556 goto err_disable;
557 }
558
559 return 0;
560
561err_disable:
562 for (i--; i >= 0; i--) {
563 clock = ddata->clocks[i];
564 if (IS_ERR_OR_NULL(clock))
565 continue;
566
567 clk_disable(clock);
568 }
569
570 return error;
571}
572
573static void sysc_disable_opt_clocks(struct sysc *ddata)
574{
575 struct clk *clock;
576 int i;
577
Tony Lindgren2c81f0f2019-12-16 14:41:53 -0800578 if (!ddata->clocks || ddata->nr_clocks < SYSC_OPTFCK0 + 1)
Tony Lindgrend8789702019-03-21 11:00:21 -0700579 return;
580
581 for (i = SYSC_OPTFCK0; i < SYSC_MAX_CLOCKS; i++) {
582 clock = ddata->clocks[i];
583
584 /* Assume no holes for opt clocks */
585 if (IS_ERR_OR_NULL(clock))
586 return;
587
588 clk_disable(clock);
589 }
590}
591
Tony Lindgren2b2f7de2019-05-27 04:51:53 -0700592static void sysc_clkdm_deny_idle(struct sysc *ddata)
593{
594 struct ti_sysc_platform_data *pdata;
595
Tony Lindgren94f63452019-12-12 08:57:58 -0800596 if (ddata->legacy_mode || (ddata->cfg.quirks & SYSC_QUIRK_CLKDM_NOAUTO))
Tony Lindgren2b2f7de2019-05-27 04:51:53 -0700597 return;
598
599 pdata = dev_get_platdata(ddata->dev);
600 if (pdata && pdata->clkdm_deny_idle)
601 pdata->clkdm_deny_idle(ddata->dev, &ddata->cookie);
602}
603
604static void sysc_clkdm_allow_idle(struct sysc *ddata)
605{
606 struct ti_sysc_platform_data *pdata;
607
Tony Lindgren94f63452019-12-12 08:57:58 -0800608 if (ddata->legacy_mode || (ddata->cfg.quirks & SYSC_QUIRK_CLKDM_NOAUTO))
Tony Lindgren2b2f7de2019-05-27 04:51:53 -0700609 return;
610
611 pdata = dev_get_platdata(ddata->dev);
612 if (pdata && pdata->clkdm_allow_idle)
613 pdata->clkdm_allow_idle(ddata->dev, &ddata->cookie);
614}
615
Tony Lindgren0eecc632017-10-10 14:23:43 -0700616/**
Tony Lindgrenb11c1ea2019-03-21 11:00:21 -0700617 * sysc_init_resets - init rstctrl reset line if configured
Tony Lindgren50622362018-04-16 10:20:27 -0700618 * @ddata: device driver data
619 *
Tony Lindgrenb11c1ea2019-03-21 11:00:21 -0700620 * See sysc_rstctrl_reset_deassert().
Tony Lindgren50622362018-04-16 10:20:27 -0700621 */
622static int sysc_init_resets(struct sysc *ddata)
623{
Tony Lindgren50622362018-04-16 10:20:27 -0700624 ddata->rsts =
Tero Kristobb88b862019-08-07 15:46:05 +0300625 devm_reset_control_get_optional_shared(ddata->dev, "rstctrl");
Tony Lindgren50622362018-04-16 10:20:27 -0700626
zhengbin3f2c4202019-11-18 18:37:32 +0800627 return PTR_ERR_OR_ZERO(ddata->rsts);
Tony Lindgren50622362018-04-16 10:20:27 -0700628}
629
630/**
Tony Lindgren0eecc632017-10-10 14:23:43 -0700631 * sysc_parse_and_check_child_range - parses module IO region from ranges
632 * @ddata: device driver data
633 *
634 * In general we only need rev, syss, and sysc registers and not the whole
635 * module range. But we do want the offsets for these registers from the
636 * module base. This allows us to check them against the legacy hwmod
637 * platform data. Let's also check the ranges are configured properly.
638 */
639static int sysc_parse_and_check_child_range(struct sysc *ddata)
640{
641 struct device_node *np = ddata->dev->of_node;
642 const __be32 *ranges;
643 u32 nr_addr, nr_size;
644 int len, error;
645
646 ranges = of_get_property(np, "ranges", &len);
647 if (!ranges) {
648 dev_err(ddata->dev, "missing ranges for %pOF\n", np);
649
650 return -ENOENT;
651 }
652
653 len /= sizeof(*ranges);
654
655 if (len < 3) {
656 dev_err(ddata->dev, "incomplete ranges for %pOF\n", np);
657
658 return -EINVAL;
659 }
660
661 error = of_property_read_u32(np, "#address-cells", &nr_addr);
662 if (error)
663 return -ENOENT;
664
665 error = of_property_read_u32(np, "#size-cells", &nr_size);
666 if (error)
667 return -ENOENT;
668
669 if (nr_addr != 1 || nr_size != 1) {
670 dev_err(ddata->dev, "invalid ranges for %pOF\n", np);
671
672 return -EINVAL;
673 }
674
675 ranges++;
676 ddata->module_pa = of_translate_address(np, ranges++);
677 ddata->module_size = be32_to_cpup(ranges);
678
Tony Lindgren0eecc632017-10-10 14:23:43 -0700679 return 0;
680}
681
Tony Lindgren4700a002021-03-08 11:35:07 +0200682/* Interconnect instances to probe before l4_per instances */
683static struct resource early_bus_ranges[] = {
684 /* am3/4 l4_wkup */
685 { .start = 0x44c00000, .end = 0x44c00000 + 0x300000, },
686 /* omap4/5 and dra7 l4_cfg */
687 { .start = 0x4a000000, .end = 0x4a000000 + 0x300000, },
688 /* omap4 l4_wkup */
689 { .start = 0x4a300000, .end = 0x4a300000 + 0x30000, },
690 /* omap5 and dra7 l4_wkup without dra7 dcan segment */
691 { .start = 0x4ae00000, .end = 0x4ae00000 + 0x30000, },
692};
693
694static atomic_t sysc_defer = ATOMIC_INIT(10);
695
696/**
697 * sysc_defer_non_critical - defer non_critical interconnect probing
698 * @ddata: device driver data
699 *
700 * We want to probe l4_cfg and l4_wkup interconnect instances before any
701 * l4_per instances as l4_per instances depend on resources on l4_cfg and
702 * l4_wkup interconnects.
703 */
704static int sysc_defer_non_critical(struct sysc *ddata)
705{
706 struct resource *res;
707 int i;
708
709 if (!atomic_read(&sysc_defer))
710 return 0;
711
712 for (i = 0; i < ARRAY_SIZE(early_bus_ranges); i++) {
713 res = &early_bus_ranges[i];
714 if (ddata->module_pa >= res->start &&
715 ddata->module_pa <= res->end) {
716 atomic_set(&sysc_defer, 0);
717
718 return 0;
719 }
720 }
721
722 atomic_dec_if_positive(&sysc_defer);
723
724 return -EPROBE_DEFER;
725}
726
Tony Lindgren3bb37c82018-02-22 14:05:14 -0800727static struct device_node *stdout_path;
728
729static void sysc_init_stdout_path(struct sysc *ddata)
730{
731 struct device_node *np = NULL;
732 const char *uart;
733
734 if (IS_ERR(stdout_path))
735 return;
736
737 if (stdout_path)
738 return;
739
740 np = of_find_node_by_path("/chosen");
741 if (!np)
742 goto err;
743
744 uart = of_get_property(np, "stdout-path", NULL);
745 if (!uart)
746 goto err;
747
748 np = of_find_node_by_path(uart);
749 if (!np)
750 goto err;
751
752 stdout_path = np;
753
754 return;
755
756err:
757 stdout_path = ERR_PTR(-ENODEV);
758}
759
760static void sysc_check_quirk_stdout(struct sysc *ddata,
761 struct device_node *np)
762{
763 sysc_init_stdout_path(ddata);
764 if (np != stdout_path)
765 return;
766
767 ddata->cfg.quirks |= SYSC_QUIRK_NO_IDLE_ON_INIT |
768 SYSC_QUIRK_NO_RESET_ON_INIT;
769}
770
Tony Lindgren0eecc632017-10-10 14:23:43 -0700771/**
772 * sysc_check_one_child - check child configuration
773 * @ddata: device driver data
774 * @np: child device node
775 *
776 * Let's avoid messy situations where we have new interconnect target
777 * node but children have "ti,hwmods". These belong to the interconnect
778 * target node and are managed by this driver.
779 */
Nishka Dasguptac6e78d72019-08-15 11:16:47 +0530780static void sysc_check_one_child(struct sysc *ddata,
781 struct device_node *np)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700782{
783 const char *name;
784
785 name = of_get_property(np, "ti,hwmods", NULL);
Tony Lindgren7320fd32020-02-24 12:58:03 -0800786 if (name && !of_device_is_compatible(np, "ti,sysc"))
Tony Lindgren0eecc632017-10-10 14:23:43 -0700787 dev_warn(ddata->dev, "really a child ti,hwmods property?");
788
Tony Lindgren3bb37c82018-02-22 14:05:14 -0800789 sysc_check_quirk_stdout(ddata, np);
Tony Lindgren4014c082018-12-10 14:11:26 -0800790 sysc_parse_dts_quirks(ddata, np, true);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700791}
792
Nishka Dasguptac6e78d72019-08-15 11:16:47 +0530793static void sysc_check_children(struct sysc *ddata)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700794{
795 struct device_node *child;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700796
Nishka Dasguptac6e78d72019-08-15 11:16:47 +0530797 for_each_child_of_node(ddata->dev->of_node, child)
798 sysc_check_one_child(ddata, child);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700799}
800
Tony Lindgrena7199e22017-12-15 09:41:14 -0800801/*
802 * So far only I2C uses 16-bit read access with clockactivity with revision
803 * in two registers with stride of 4. We can detect this based on the rev
804 * register size to configure things far enough to be able to properly read
805 * the revision register.
806 */
807static void sysc_check_quirk_16bit(struct sysc *ddata, struct resource *res)
808{
Tony Lindgrendd57ac12018-02-22 14:09:57 -0800809 if (resource_size(res) == 8)
Tony Lindgrena7199e22017-12-15 09:41:14 -0800810 ddata->cfg.quirks |= SYSC_QUIRK_16BIT | SYSC_QUIRK_USE_CLOCKACT;
Tony Lindgrena7199e22017-12-15 09:41:14 -0800811}
812
Tony Lindgren0eecc632017-10-10 14:23:43 -0700813/**
814 * sysc_parse_one - parses the interconnect target module registers
815 * @ddata: device driver data
816 * @reg: register to parse
817 */
818static int sysc_parse_one(struct sysc *ddata, enum sysc_registers reg)
819{
820 struct resource *res;
821 const char *name;
822
823 switch (reg) {
824 case SYSC_REVISION:
825 case SYSC_SYSCONFIG:
826 case SYSC_SYSSTATUS:
827 name = reg_names[reg];
828 break;
829 default:
830 return -EINVAL;
831 }
832
833 res = platform_get_resource_byname(to_platform_device(ddata->dev),
834 IORESOURCE_MEM, name);
835 if (!res) {
Tony Lindgren0eecc632017-10-10 14:23:43 -0700836 ddata->offsets[reg] = -ENODEV;
837
838 return 0;
839 }
840
841 ddata->offsets[reg] = res->start - ddata->module_pa;
Tony Lindgrena7199e22017-12-15 09:41:14 -0800842 if (reg == SYSC_REVISION)
843 sysc_check_quirk_16bit(ddata, res);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700844
845 return 0;
846}
847
848static int sysc_parse_registers(struct sysc *ddata)
849{
850 int i, error;
851
852 for (i = 0; i < SYSC_MAX_REGS; i++) {
853 error = sysc_parse_one(ddata, i);
854 if (error)
855 return error;
856 }
857
858 return 0;
859}
860
861/**
862 * sysc_check_registers - check for misconfigured register overlaps
863 * @ddata: device driver data
864 */
865static int sysc_check_registers(struct sysc *ddata)
866{
867 int i, j, nr_regs = 0, nr_matches = 0;
868
869 for (i = 0; i < SYSC_MAX_REGS; i++) {
870 if (ddata->offsets[i] < 0)
871 continue;
872
873 if (ddata->offsets[i] > (ddata->module_size - 4)) {
874 dev_err(ddata->dev, "register outside module range");
875
876 return -EINVAL;
877 }
878
879 for (j = 0; j < SYSC_MAX_REGS; j++) {
880 if (ddata->offsets[j] < 0)
881 continue;
882
883 if (ddata->offsets[i] == ddata->offsets[j])
884 nr_matches++;
885 }
886 nr_regs++;
887 }
888
Tony Lindgren0eecc632017-10-10 14:23:43 -0700889 if (nr_matches > nr_regs) {
890 dev_err(ddata->dev, "overlapping registers: (%i/%i)",
891 nr_regs, nr_matches);
892
893 return -EINVAL;
894 }
895
896 return 0;
897}
898
899/**
Lee Jones4e001852021-05-26 09:10:37 +0100900 * sysc_ioremap - ioremap register space for the interconnect target module
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700901 * @ddata: device driver data
Tony Lindgren0eecc632017-10-10 14:23:43 -0700902 *
903 * Note that the interconnect target module registers can be anywhere
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700904 * within the interconnect target module range. For example, SGX has
905 * them at offset 0x1fc00 in the 32MB module address space. And cpsw
906 * has them at offset 0x1200 in the CPSW_WR child. Usually the
907 * the interconnect target module registers are at the beginning of
908 * the module range though.
Tony Lindgren0eecc632017-10-10 14:23:43 -0700909 */
910static int sysc_ioremap(struct sysc *ddata)
911{
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700912 int size;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700913
Tony Lindgrene4f50c82019-05-01 14:24:57 -0700914 if (ddata->offsets[SYSC_REVISION] < 0 &&
915 ddata->offsets[SYSC_SYSCONFIG] < 0 &&
916 ddata->offsets[SYSC_SYSSTATUS] < 0) {
917 size = ddata->module_size;
918 } else {
919 size = max3(ddata->offsets[SYSC_REVISION],
920 ddata->offsets[SYSC_SYSCONFIG],
921 ddata->offsets[SYSC_SYSSTATUS]);
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700922
Tony Lindgren4e23be42019-06-10 04:48:05 -0700923 if (size < SZ_1K)
924 size = SZ_1K;
925
Tony Lindgrene4f50c82019-05-01 14:24:57 -0700926 if ((size + sizeof(u32)) > ddata->module_size)
Tony Lindgren4e23be42019-06-10 04:48:05 -0700927 size = ddata->module_size;
Tony Lindgrene4f50c82019-05-01 14:24:57 -0700928 }
Tony Lindgren0eecc632017-10-10 14:23:43 -0700929
Tony Lindgren0eecc632017-10-10 14:23:43 -0700930 ddata->module_va = devm_ioremap(ddata->dev,
931 ddata->module_pa,
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700932 size + sizeof(u32));
Tony Lindgren0eecc632017-10-10 14:23:43 -0700933 if (!ddata->module_va)
934 return -EIO;
935
936 return 0;
937}
938
939/**
940 * sysc_map_and_check_registers - ioremap and check device registers
941 * @ddata: device driver data
942 */
943static int sysc_map_and_check_registers(struct sysc *ddata)
944{
Tony Lindgren29281352020-11-16 12:57:13 +0200945 struct device_node *np = ddata->dev->of_node;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700946 int error;
947
948 error = sysc_parse_and_check_child_range(ddata);
949 if (error)
950 return error;
951
Tony Lindgren4700a002021-03-08 11:35:07 +0200952 error = sysc_defer_non_critical(ddata);
953 if (error)
954 return error;
955
Nishka Dasguptac6e78d72019-08-15 11:16:47 +0530956 sysc_check_children(ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700957
Tony Lindgren7bad5af2021-03-08 11:35:06 +0200958 if (!of_get_property(np, "reg", NULL))
959 return 0;
960
Tony Lindgren0eecc632017-10-10 14:23:43 -0700961 error = sysc_parse_registers(ddata);
962 if (error)
963 return error;
964
965 error = sysc_ioremap(ddata);
966 if (error)
967 return error;
968
969 error = sysc_check_registers(ddata);
970 if (error)
971 return error;
972
973 return 0;
974}
975
976/**
977 * sysc_show_rev - read and show interconnect target module revision
978 * @bufp: buffer to print the information to
979 * @ddata: device driver data
980 */
981static int sysc_show_rev(char *bufp, struct sysc *ddata)
982{
Tony Lindgren566a9b052017-12-15 09:41:19 -0800983 int len;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700984
985 if (ddata->offsets[SYSC_REVISION] < 0)
986 return sprintf(bufp, ":NA");
987
Tony Lindgren566a9b052017-12-15 09:41:19 -0800988 len = sprintf(bufp, ":%08x", ddata->revision);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700989
990 return len;
991}
992
993static int sysc_show_reg(struct sysc *ddata,
994 char *bufp, enum sysc_registers reg)
995{
996 if (ddata->offsets[reg] < 0)
997 return sprintf(bufp, ":NA");
998
999 return sprintf(bufp, ":%x", ddata->offsets[reg]);
1000}
1001
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001002static int sysc_show_name(char *bufp, struct sysc *ddata)
1003{
1004 if (!ddata->name)
1005 return 0;
1006
1007 return sprintf(bufp, ":%s", ddata->name);
1008}
1009
Tony Lindgren0eecc632017-10-10 14:23:43 -07001010/**
1011 * sysc_show_registers - show information about interconnect target module
1012 * @ddata: device driver data
1013 */
1014static void sysc_show_registers(struct sysc *ddata)
1015{
1016 char buf[128];
1017 char *bufp = buf;
1018 int i;
1019
1020 for (i = 0; i < SYSC_MAX_REGS; i++)
1021 bufp += sysc_show_reg(ddata, bufp, i);
1022
1023 bufp += sysc_show_rev(bufp, ddata);
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001024 bufp += sysc_show_name(bufp, ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001025
1026 dev_dbg(ddata->dev, "%llx:%x%s\n",
1027 ddata->module_pa, ddata->module_size,
1028 buf);
1029}
1030
Tony Lindgrene8639e12020-02-24 12:58:03 -08001031/**
1032 * sysc_write_sysconfig - handle sysconfig quirks for register write
1033 * @ddata: device driver data
1034 * @value: register value
1035 */
1036static void sysc_write_sysconfig(struct sysc *ddata, u32 value)
1037{
1038 if (ddata->module_unlock_quirk)
1039 ddata->module_unlock_quirk(ddata);
1040
1041 sysc_write(ddata, ddata->offsets[SYSC_SYSCONFIG], value);
1042
1043 if (ddata->module_lock_quirk)
1044 ddata->module_lock_quirk(ddata);
1045}
1046
Roger Quadrosd59b6052019-04-08 12:52:39 +03001047#define SYSC_IDLE_MASK (SYSC_NR_IDLEMODES - 1)
Tony Lindgrenae9ae122019-05-27 04:51:54 -07001048#define SYSC_CLOCACT_ICK 2
Roger Quadrosd59b6052019-04-08 12:52:39 +03001049
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001050/* Caller needs to manage sysc_clkdm_deny_idle() and sysc_clkdm_allow_idle() */
Roger Quadrosd59b6052019-04-08 12:52:39 +03001051static int sysc_enable_module(struct device *dev)
1052{
1053 struct sysc *ddata;
1054 const struct sysc_regbits *regbits;
1055 u32 reg, idlemodes, best_mode;
Tony Lindgrend46f9fb2020-05-31 12:37:54 -07001056 int error;
Roger Quadrosd59b6052019-04-08 12:52:39 +03001057
1058 ddata = dev_get_drvdata(dev);
Tony Lindgrend46f9fb2020-05-31 12:37:54 -07001059
1060 /*
1061 * Some modules like DSS reset automatically on idle. Enable optional
1062 * reset clocks and wait for OCP softreset to complete.
1063 */
1064 if (ddata->cfg.quirks & SYSC_QUIRK_OPT_CLKS_IN_RESET) {
1065 error = sysc_enable_opt_clocks(ddata);
1066 if (error) {
1067 dev_err(ddata->dev,
1068 "Optional clocks failed for enable: %i\n",
1069 error);
1070 return error;
1071 }
1072 }
Tony Lindgrene275d212020-10-26 10:08:47 +02001073 /*
1074 * Some modules like i2c and hdq1w have unusable reset status unless
1075 * the module reset quirk is enabled. Skip status check on enable.
1076 */
1077 if (!(ddata->cfg.quirks & SYSC_MODULE_QUIRK_ENA_RESETDONE)) {
1078 error = sysc_wait_softreset(ddata);
1079 if (error)
1080 dev_warn(ddata->dev, "OCP softreset timed out\n");
1081 }
Tony Lindgrend46f9fb2020-05-31 12:37:54 -07001082 if (ddata->cfg.quirks & SYSC_QUIRK_OPT_CLKS_IN_RESET)
1083 sysc_disable_opt_clocks(ddata);
1084
1085 /*
1086 * Some subsystem private interconnects, like DSS top level module,
1087 * need only the automatic OCP softreset handling with no sysconfig
1088 * register bits to configure.
1089 */
Roger Quadrosd59b6052019-04-08 12:52:39 +03001090 if (ddata->offsets[SYSC_SYSCONFIG] == -ENODEV)
1091 return 0;
1092
Roger Quadrosd59b6052019-04-08 12:52:39 +03001093 regbits = ddata->cap->regbits;
1094 reg = sysc_read(ddata, ddata->offsets[SYSC_SYSCONFIG]);
1095
Tony Lindgren08b91dd2020-05-31 12:37:54 -07001096 /*
1097 * Set CLOCKACTIVITY, we only use it for ick. And we only configure it
1098 * based on the SYSC_QUIRK_USE_CLOCKACT flag, not based on the hardware
1099 * capabilities. See the old HWMOD_SET_DEFAULT_CLOCKACT flag.
1100 */
Tony Lindgrenae9ae122019-05-27 04:51:54 -07001101 if (regbits->clkact_shift >= 0 &&
Tony Lindgren08b91dd2020-05-31 12:37:54 -07001102 (ddata->cfg.quirks & SYSC_QUIRK_USE_CLOCKACT))
Tony Lindgrenae9ae122019-05-27 04:51:54 -07001103 reg |= SYSC_CLOCACT_ICK << regbits->clkact_shift;
1104
Roger Quadrosd59b6052019-04-08 12:52:39 +03001105 /* Set SIDLE mode */
1106 idlemodes = ddata->cfg.sidlemodes;
1107 if (!idlemodes || regbits->sidle_shift < 0)
1108 goto set_midle;
1109
Tony Lindgrenfb685f12019-05-27 04:51:55 -07001110 if (ddata->cfg.quirks & (SYSC_QUIRK_SWSUP_SIDLE |
1111 SYSC_QUIRK_SWSUP_SIDLE_ACT)) {
1112 best_mode = SYSC_IDLE_NO;
1113 } else {
1114 best_mode = fls(ddata->cfg.sidlemodes) - 1;
1115 if (best_mode > SYSC_IDLE_MASK) {
1116 dev_err(dev, "%s: invalid sidlemode\n", __func__);
1117 return -EINVAL;
1118 }
Tony Lindgren6e09f492019-05-27 04:51:55 -07001119
1120 /* Set WAKEUP */
1121 if (regbits->enwkup_shift >= 0 &&
1122 ddata->cfg.sysc_val & BIT(regbits->enwkup_shift))
1123 reg |= BIT(regbits->enwkup_shift);
Roger Quadrosd59b6052019-04-08 12:52:39 +03001124 }
1125
1126 reg &= ~(SYSC_IDLE_MASK << regbits->sidle_shift);
1127 reg |= best_mode << regbits->sidle_shift;
Tony Lindgrene8639e12020-02-24 12:58:03 -08001128 sysc_write_sysconfig(ddata, reg);
Roger Quadrosd59b6052019-04-08 12:52:39 +03001129
1130set_midle:
1131 /* Set MIDLE mode */
1132 idlemodes = ddata->cfg.midlemodes;
1133 if (!idlemodes || regbits->midle_shift < 0)
Tony Lindgreneec26552019-05-27 04:51:54 -07001134 goto set_autoidle;
Roger Quadrosd59b6052019-04-08 12:52:39 +03001135
1136 best_mode = fls(ddata->cfg.midlemodes) - 1;
1137 if (best_mode > SYSC_IDLE_MASK) {
1138 dev_err(dev, "%s: invalid midlemode\n", __func__);
Tony Lindgren95ec14fa2021-09-21 12:42:25 +03001139 error = -EINVAL;
1140 goto save_context;
Roger Quadrosd59b6052019-04-08 12:52:39 +03001141 }
1142
Tony Lindgren03856e92019-10-21 13:36:09 -07001143 if (ddata->cfg.quirks & SYSC_QUIRK_SWSUP_MSTANDBY)
1144 best_mode = SYSC_IDLE_NO;
1145
Roger Quadrosd59b6052019-04-08 12:52:39 +03001146 reg &= ~(SYSC_IDLE_MASK << regbits->midle_shift);
1147 reg |= best_mode << regbits->midle_shift;
Tony Lindgrene8639e12020-02-24 12:58:03 -08001148 sysc_write_sysconfig(ddata, reg);
Roger Quadrosd59b6052019-04-08 12:52:39 +03001149
Tony Lindgreneec26552019-05-27 04:51:54 -07001150set_autoidle:
1151 /* Autoidle bit must enabled separately if available */
1152 if (regbits->autoidle_shift >= 0 &&
1153 ddata->cfg.sysc_val & BIT(regbits->autoidle_shift)) {
1154 reg |= 1 << regbits->autoidle_shift;
Tony Lindgrene8639e12020-02-24 12:58:03 -08001155 sysc_write_sysconfig(ddata, reg);
Tony Lindgreneec26552019-05-27 04:51:54 -07001156 }
1157
Tony Lindgren95ec14fa2021-09-21 12:42:25 +03001158 error = 0;
1159
1160save_context:
1161 /* Save context and flush posted write */
1162 ddata->sysconfig = sysc_read(ddata, ddata->offsets[SYSC_SYSCONFIG]);
Tony Lindgren5ce8aee2020-05-27 06:49:29 -07001163
Tony Lindgrend7f563d2019-08-14 05:18:16 -07001164 if (ddata->module_enable_quirk)
1165 ddata->module_enable_quirk(ddata);
1166
Tony Lindgren95ec14fa2021-09-21 12:42:25 +03001167 return error;
Roger Quadrosd59b6052019-04-08 12:52:39 +03001168}
1169
1170static int sysc_best_idle_mode(u32 idlemodes, u32 *best_mode)
1171{
1172 if (idlemodes & BIT(SYSC_IDLE_SMART_WKUP))
1173 *best_mode = SYSC_IDLE_SMART_WKUP;
1174 else if (idlemodes & BIT(SYSC_IDLE_SMART))
1175 *best_mode = SYSC_IDLE_SMART;
Tony Lindgren6ee82412019-07-22 03:44:42 -07001176 else if (idlemodes & BIT(SYSC_IDLE_FORCE))
Roger Quadrosd59b6052019-04-08 12:52:39 +03001177 *best_mode = SYSC_IDLE_FORCE;
1178 else
1179 return -EINVAL;
1180
1181 return 0;
1182}
1183
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001184/* Caller needs to manage sysc_clkdm_deny_idle() and sysc_clkdm_allow_idle() */
Roger Quadrosd59b6052019-04-08 12:52:39 +03001185static int sysc_disable_module(struct device *dev)
1186{
1187 struct sysc *ddata;
1188 const struct sysc_regbits *regbits;
1189 u32 reg, idlemodes, best_mode;
1190 int ret;
1191
1192 ddata = dev_get_drvdata(dev);
1193 if (ddata->offsets[SYSC_SYSCONFIG] == -ENODEV)
1194 return 0;
1195
Tony Lindgrenc7d86692019-10-17 11:21:44 -07001196 if (ddata->module_disable_quirk)
1197 ddata->module_disable_quirk(ddata);
1198
Roger Quadrosd59b6052019-04-08 12:52:39 +03001199 regbits = ddata->cap->regbits;
1200 reg = sysc_read(ddata, ddata->offsets[SYSC_SYSCONFIG]);
1201
1202 /* Set MIDLE mode */
1203 idlemodes = ddata->cfg.midlemodes;
1204 if (!idlemodes || regbits->midle_shift < 0)
1205 goto set_sidle;
1206
1207 ret = sysc_best_idle_mode(idlemodes, &best_mode);
1208 if (ret) {
1209 dev_err(dev, "%s: invalid midlemode\n", __func__);
1210 return ret;
1211 }
1212
Tony Lindgren93c60482019-12-09 13:55:15 -08001213 if (ddata->cfg.quirks & (SYSC_QUIRK_SWSUP_MSTANDBY) ||
1214 ddata->cfg.quirks & (SYSC_QUIRK_FORCE_MSTANDBY))
Tony Lindgren03856e92019-10-21 13:36:09 -07001215 best_mode = SYSC_IDLE_FORCE;
1216
Roger Quadrosd59b6052019-04-08 12:52:39 +03001217 reg &= ~(SYSC_IDLE_MASK << regbits->midle_shift);
1218 reg |= best_mode << regbits->midle_shift;
Tony Lindgrene8639e12020-02-24 12:58:03 -08001219 sysc_write_sysconfig(ddata, reg);
Roger Quadrosd59b6052019-04-08 12:52:39 +03001220
1221set_sidle:
1222 /* Set SIDLE mode */
1223 idlemodes = ddata->cfg.sidlemodes;
Tony Lindgren95ec14fa2021-09-21 12:42:25 +03001224 if (!idlemodes || regbits->sidle_shift < 0) {
1225 ret = 0;
1226 goto save_context;
1227 }
Roger Quadrosd59b6052019-04-08 12:52:39 +03001228
Tony Lindgrenfb685f12019-05-27 04:51:55 -07001229 if (ddata->cfg.quirks & SYSC_QUIRK_SWSUP_SIDLE) {
1230 best_mode = SYSC_IDLE_FORCE;
1231 } else {
1232 ret = sysc_best_idle_mode(idlemodes, &best_mode);
1233 if (ret) {
1234 dev_err(dev, "%s: invalid sidlemode\n", __func__);
Tony Lindgren95ec14fa2021-09-21 12:42:25 +03001235 ret = -EINVAL;
1236 goto save_context;
Tony Lindgrenfb685f12019-05-27 04:51:55 -07001237 }
Roger Quadrosd59b6052019-04-08 12:52:39 +03001238 }
1239
1240 reg &= ~(SYSC_IDLE_MASK << regbits->sidle_shift);
1241 reg |= best_mode << regbits->sidle_shift;
Tony Lindgreneec26552019-05-27 04:51:54 -07001242 if (regbits->autoidle_shift >= 0 &&
1243 ddata->cfg.sysc_val & BIT(regbits->autoidle_shift))
1244 reg |= 1 << regbits->autoidle_shift;
Tony Lindgrene8639e12020-02-24 12:58:03 -08001245 sysc_write_sysconfig(ddata, reg);
Roger Quadrosd59b6052019-04-08 12:52:39 +03001246
Tony Lindgren95ec14fa2021-09-21 12:42:25 +03001247 ret = 0;
Tony Lindgren5ce8aee2020-05-27 06:49:29 -07001248
Tony Lindgren95ec14fa2021-09-21 12:42:25 +03001249save_context:
1250 /* Save context and flush posted write */
1251 ddata->sysconfig = sysc_read(ddata, ddata->offsets[SYSC_SYSCONFIG]);
1252
1253 return ret;
Roger Quadrosd59b6052019-04-08 12:52:39 +03001254}
1255
Tony Lindgrenff437282019-03-21 11:00:21 -07001256static int __maybe_unused sysc_runtime_suspend_legacy(struct device *dev,
1257 struct sysc *ddata)
Tony Lindgren0eecc632017-10-10 14:23:43 -07001258{
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001259 struct ti_sysc_platform_data *pdata;
Tony Lindgrenff437282019-03-21 11:00:21 -07001260 int error;
1261
1262 pdata = dev_get_platdata(ddata->dev);
1263 if (!pdata)
1264 return 0;
1265
1266 if (!pdata->idle_module)
1267 return -ENODEV;
1268
1269 error = pdata->idle_module(dev, &ddata->cookie);
1270 if (error)
1271 dev_err(dev, "%s: could not idle: %i\n",
1272 __func__, error);
1273
Tero Kristo4345f0d2019-08-07 15:46:04 +03001274 reset_control_assert(ddata->rsts);
Tony Lindgren8383e252019-05-27 04:51:56 -07001275
Tony Lindgrenff437282019-03-21 11:00:21 -07001276 return 0;
1277}
1278
1279static int __maybe_unused sysc_runtime_resume_legacy(struct device *dev,
1280 struct sysc *ddata)
1281{
1282 struct ti_sysc_platform_data *pdata;
1283 int error;
1284
1285 pdata = dev_get_platdata(ddata->dev);
1286 if (!pdata)
1287 return 0;
1288
1289 if (!pdata->enable_module)
1290 return -ENODEV;
1291
1292 error = pdata->enable_module(dev, &ddata->cookie);
1293 if (error)
1294 dev_err(dev, "%s: could not enable: %i\n",
1295 __func__, error);
1296
Tero Kristobf59ebb2019-10-07 15:29:29 +03001297 reset_control_deassert(ddata->rsts);
1298
Tony Lindgrenff437282019-03-21 11:00:21 -07001299 return 0;
1300}
1301
1302static int __maybe_unused sysc_runtime_suspend(struct device *dev)
1303{
Tony Lindgren0eecc632017-10-10 14:23:43 -07001304 struct sysc *ddata;
Tony Lindgrend8789702019-03-21 11:00:21 -07001305 int error = 0;
Tony Lindgren0eecc632017-10-10 14:23:43 -07001306
1307 ddata = dev_get_drvdata(dev);
1308
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001309 if (!ddata->enabled)
Tony Lindgren0eecc632017-10-10 14:23:43 -07001310 return 0;
1311
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001312 sysc_clkdm_deny_idle(ddata);
1313
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001314 if (ddata->legacy_mode) {
Tony Lindgrenff437282019-03-21 11:00:21 -07001315 error = sysc_runtime_suspend_legacy(dev, ddata);
Tony Lindgren93de83a2019-03-21 11:00:21 -07001316 if (error)
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001317 goto err_allow_idle;
Roger Quadrosd59b6052019-04-08 12:52:39 +03001318 } else {
1319 error = sysc_disable_module(dev);
1320 if (error)
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001321 goto err_allow_idle;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001322 }
1323
Tony Lindgrend8789702019-03-21 11:00:21 -07001324 sysc_disable_main_clocks(ddata);
Tony Lindgren09dfe582018-04-16 10:25:52 -07001325
Tony Lindgrend8789702019-03-21 11:00:21 -07001326 if (sysc_opt_clks_needed(ddata))
1327 sysc_disable_opt_clocks(ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001328
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001329 ddata->enabled = false;
1330
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001331err_allow_idle:
Tero Kristob6036312019-08-07 15:46:03 +03001332 sysc_clkdm_allow_idle(ddata);
1333
Tony Lindgren4097c9a2020-11-16 12:57:13 +02001334 reset_control_assert(ddata->rsts);
1335
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001336 return error;
Tony Lindgren0eecc632017-10-10 14:23:43 -07001337}
1338
Arnd Bergmanna4a5d492017-10-13 11:25:32 +02001339static int __maybe_unused sysc_runtime_resume(struct device *dev)
Tony Lindgren0eecc632017-10-10 14:23:43 -07001340{
1341 struct sysc *ddata;
Tony Lindgrend8789702019-03-21 11:00:21 -07001342 int error = 0;
Tony Lindgren0eecc632017-10-10 14:23:43 -07001343
1344 ddata = dev_get_drvdata(dev);
1345
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001346 if (ddata->enabled)
Tony Lindgren0eecc632017-10-10 14:23:43 -07001347 return 0;
1348
Tony Lindgren8383e252019-05-27 04:51:56 -07001349
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001350 sysc_clkdm_deny_idle(ddata);
1351
Tony Lindgrend8789702019-03-21 11:00:21 -07001352 if (sysc_opt_clks_needed(ddata)) {
1353 error = sysc_enable_opt_clocks(ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001354 if (error)
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001355 goto err_allow_idle;
Tony Lindgren0eecc632017-10-10 14:23:43 -07001356 }
1357
Tony Lindgrend8789702019-03-21 11:00:21 -07001358 error = sysc_enable_main_clocks(ddata);
1359 if (error)
Tony Lindgren93de83a2019-03-21 11:00:21 -07001360 goto err_opt_clocks;
1361
Tero Kristobf59ebb2019-10-07 15:29:29 +03001362 reset_control_deassert(ddata->rsts);
1363
Tony Lindgren93de83a2019-03-21 11:00:21 -07001364 if (ddata->legacy_mode) {
1365 error = sysc_runtime_resume_legacy(dev, ddata);
1366 if (error)
1367 goto err_main_clocks;
Roger Quadrosd59b6052019-04-08 12:52:39 +03001368 } else {
1369 error = sysc_enable_module(dev);
1370 if (error)
1371 goto err_main_clocks;
Tony Lindgren93de83a2019-03-21 11:00:21 -07001372 }
Tony Lindgrend8789702019-03-21 11:00:21 -07001373
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001374 ddata->enabled = true;
1375
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001376 sysc_clkdm_allow_idle(ddata);
1377
Tony Lindgrend8789702019-03-21 11:00:21 -07001378 return 0;
1379
1380err_main_clocks:
Tony Lindgren93de83a2019-03-21 11:00:21 -07001381 sysc_disable_main_clocks(ddata);
1382err_opt_clocks:
Tony Lindgrend8789702019-03-21 11:00:21 -07001383 if (sysc_opt_clks_needed(ddata))
1384 sysc_disable_opt_clocks(ddata);
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001385err_allow_idle:
1386 sysc_clkdm_allow_idle(ddata);
Tony Lindgrend8789702019-03-21 11:00:21 -07001387
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001388 return error;
Tony Lindgren0eecc632017-10-10 14:23:43 -07001389}
1390
Tony Lindgren95ec14fa2021-09-21 12:42:25 +03001391/*
1392 * Checks if device context was lost. Assumes the sysconfig register value
1393 * after lost context is different from the configured value. Only works for
1394 * enabled devices.
1395 *
1396 * Eventually we may want to also add support to using the context lost
1397 * registers that some SoCs have.
1398 */
1399static int sysc_check_context(struct sysc *ddata)
1400{
1401 u32 reg;
1402
1403 if (!ddata->enabled)
1404 return -ENODATA;
1405
1406 reg = sysc_read(ddata, ddata->offsets[SYSC_SYSCONFIG]);
1407 if (reg == ddata->sysconfig)
1408 return 0;
1409
1410 return -EACCES;
1411}
1412
Tony Lindgren4d7b3242021-05-18 09:47:23 +03001413static int sysc_reinit_module(struct sysc *ddata, bool leave_enabled)
1414{
1415 struct device *dev = ddata->dev;
1416 int error;
1417
Tony Lindgren4d7b3242021-05-18 09:47:23 +03001418 if (ddata->enabled) {
Tony Lindgren95ec14fa2021-09-21 12:42:25 +03001419 /* Nothing to do if enabled and context not lost */
1420 error = sysc_check_context(ddata);
1421 if (!error)
1422 return 0;
1423
1424 /* Disable target module if it is enabled */
Tony Lindgren4d7b3242021-05-18 09:47:23 +03001425 error = sysc_runtime_suspend(dev);
1426 if (error)
1427 dev_warn(dev, "reinit suspend failed: %i\n", error);
1428 }
1429
1430 /* Enable target module */
1431 error = sysc_runtime_resume(dev);
1432 if (error)
1433 dev_warn(dev, "reinit resume failed: %i\n", error);
1434
1435 if (leave_enabled)
1436 return error;
1437
1438 /* Disable target module if no leave_enabled was set */
1439 error = sysc_runtime_suspend(dev);
1440 if (error)
1441 dev_warn(dev, "reinit suspend failed: %i\n", error);
1442
1443 return error;
1444}
1445
Tony Lindgrenf5e802032018-10-01 09:33:28 -07001446static int __maybe_unused sysc_noirq_suspend(struct device *dev)
Tony Lindgren62020f22018-02-22 13:59:44 -08001447{
1448 struct sysc *ddata;
1449
1450 ddata = dev_get_drvdata(dev);
1451
Tony Lindgrena55de412020-07-02 13:57:14 -07001452 if (ddata->cfg.quirks &
1453 (SYSC_QUIRK_LEGACY_IDLE | SYSC_QUIRK_NO_IDLE))
Tony Lindgrene7420c22018-04-16 10:26:46 -07001454 return 0;
1455
Tony Lindgren4d7b3242021-05-18 09:47:23 +03001456 if (!ddata->enabled)
1457 return 0;
1458
1459 ddata->needs_resume = 1;
1460
1461 return sysc_runtime_suspend(dev);
Tony Lindgren62020f22018-02-22 13:59:44 -08001462}
1463
Tony Lindgrenf5e802032018-10-01 09:33:28 -07001464static int __maybe_unused sysc_noirq_resume(struct device *dev)
Tony Lindgren62020f22018-02-22 13:59:44 -08001465{
1466 struct sysc *ddata;
Tony Lindgren4d7b3242021-05-18 09:47:23 +03001467 int error = 0;
Tony Lindgren62020f22018-02-22 13:59:44 -08001468
1469 ddata = dev_get_drvdata(dev);
Tony Lindgrene7420c22018-04-16 10:26:46 -07001470
Tony Lindgrena55de412020-07-02 13:57:14 -07001471 if (ddata->cfg.quirks &
1472 (SYSC_QUIRK_LEGACY_IDLE | SYSC_QUIRK_NO_IDLE))
Tony Lindgrene7420c22018-04-16 10:26:46 -07001473 return 0;
1474
Tony Lindgren4d7b3242021-05-18 09:47:23 +03001475 if (ddata->cfg.quirks & SYSC_QUIRK_REINIT_ON_RESUME) {
1476 error = sysc_reinit_module(ddata, ddata->needs_resume);
1477 if (error)
1478 dev_warn(dev, "noirq_resume failed: %i\n", error);
1479 } else if (ddata->needs_resume) {
1480 error = sysc_runtime_resume(dev);
1481 if (error)
1482 dev_warn(dev, "noirq_resume failed: %i\n", error);
1483 }
1484
1485 ddata->needs_resume = 0;
1486
1487 return error;
Tony Lindgrene7420c22018-04-16 10:26:46 -07001488}
1489
Tony Lindgren0eecc632017-10-10 14:23:43 -07001490static const struct dev_pm_ops sysc_pm_ops = {
Tony Lindgrene7420c22018-04-16 10:26:46 -07001491 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(sysc_noirq_suspend, sysc_noirq_resume)
Tony Lindgren0eecc632017-10-10 14:23:43 -07001492 SET_RUNTIME_PM_OPS(sysc_runtime_suspend,
1493 sysc_runtime_resume,
1494 NULL)
1495};
1496
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001497/* Module revision register based quirks */
1498struct sysc_revision_quirk {
1499 const char *name;
1500 u32 base;
1501 int rev_offset;
1502 int sysc_offset;
1503 int syss_offset;
1504 u32 revision;
1505 u32 revision_mask;
1506 u32 quirks;
1507};
1508
1509#define SYSC_QUIRK(optname, optbase, optrev, optsysc, optsyss, \
1510 optrev_val, optrevmask, optquirkmask) \
1511 { \
1512 .name = (optname), \
1513 .base = (optbase), \
1514 .rev_offset = (optrev), \
1515 .sysc_offset = (optsysc), \
1516 .syss_offset = (optsyss), \
1517 .revision = (optrev_val), \
1518 .revision_mask = (optrevmask), \
1519 .quirks = (optquirkmask), \
1520 }
1521
1522static const struct sysc_revision_quirk sysc_revision_quirks[] = {
1523 /* These drivers need to be fixed to not use pm_runtime_irq_safe() */
Tony Lindgren3a3d8022018-09-27 13:33:58 -07001524 SYSC_QUIRK("gpio", 0, 0, 0x10, 0x114, 0x50600801, 0xffff00ff,
Tony Lindgren09dfe582018-04-16 10:25:52 -07001525 SYSC_QUIRK_LEGACY_IDLE | SYSC_QUIRK_OPT_CLKS_IN_RESET),
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001526 SYSC_QUIRK("sham", 0, 0x100, 0x110, 0x114, 0x40000c03, 0xffffffff,
1527 SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgrenb6a53c42019-05-27 04:51:57 -07001528 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x00000046, 0xffffffff,
1529 SYSC_QUIRK_SWSUP_SIDLE | SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001530 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x00000052, 0xffffffff,
Tony Lindgrenb6a53c42019-05-27 04:51:57 -07001531 SYSC_QUIRK_SWSUP_SIDLE | SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgrend708bb12018-04-16 10:25:32 -07001532 /* Uarts on omap4 and later */
Tony Lindgrenb82beef2018-09-24 12:16:59 -07001533 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x50411e03, 0xffff00ff,
Tony Lindgrenc8692ad2021-05-25 09:08:23 +03001534 SYSC_QUIRK_SWSUP_SIDLE | SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgrenb82beef2018-09-24 12:16:59 -07001535 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x47422e03, 0xffffffff,
Tony Lindgrenc8692ad2021-05-25 09:08:23 +03001536 SYSC_QUIRK_SWSUP_SIDLE | SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgren7e27e5d2018-04-20 14:54:16 -07001537
Tony Lindgrena54275f2019-03-21 11:00:21 -07001538 /* Quirks that need to be set based on the module address */
Tony Lindgren590e15c2020-02-24 12:58:03 -08001539 SYSC_QUIRK("mcpdm", 0x40132000, 0, 0x10, -ENODEV, 0x50000800, 0xffffffff,
Tony Lindgrena54275f2019-03-21 11:00:21 -07001540 SYSC_QUIRK_EXT_OPT_CLOCK | SYSC_QUIRK_NO_RESET_ON_INIT |
1541 SYSC_QUIRK_SWSUP_SIDLE),
1542
Tony Lindgren4e23be42019-06-10 04:48:05 -07001543 /* Quirks that need to be set based on detected module */
Tony Lindgren590e15c2020-02-24 12:58:03 -08001544 SYSC_QUIRK("aess", 0, 0, 0x10, -ENODEV, 0x40000000, 0xffffffff,
Tony Lindgren020003f2019-11-14 08:25:28 -08001545 SYSC_MODULE_QUIRK_AESS),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001546 SYSC_QUIRK("dcan", 0x48480000, 0x20, -ENODEV, -ENODEV, 0xa3170504, 0xffffffff,
Tony Lindgren94f63452019-12-12 08:57:58 -08001547 SYSC_QUIRK_CLKDM_NOAUTO),
Tony Lindgren77dfece2020-02-24 12:58:03 -08001548 SYSC_QUIRK("dss", 0x4832a000, 0, 0x10, 0x14, 0x00000020, 0xffffffff,
Tony Lindgren7324a7a2020-02-24 12:58:03 -08001549 SYSC_QUIRK_OPT_CLKS_IN_RESET | SYSC_MODULE_QUIRK_DSS_RESET),
Tony Lindgren77dfece2020-02-24 12:58:03 -08001550 SYSC_QUIRK("dss", 0x58000000, 0, -ENODEV, 0x14, 0x00000040, 0xffffffff,
Tony Lindgren7324a7a2020-02-24 12:58:03 -08001551 SYSC_QUIRK_OPT_CLKS_IN_RESET | SYSC_MODULE_QUIRK_DSS_RESET),
Tony Lindgren77dfece2020-02-24 12:58:03 -08001552 SYSC_QUIRK("dss", 0x58000000, 0, -ENODEV, 0x14, 0x00000061, 0xffffffff,
Tony Lindgren7324a7a2020-02-24 12:58:03 -08001553 SYSC_QUIRK_OPT_CLKS_IN_RESET | SYSC_MODULE_QUIRK_DSS_RESET),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001554 SYSC_QUIRK("dwc3", 0x48880000, 0, 0x10, -ENODEV, 0x500a0200, 0xffffffff,
Tony Lindgren94f63452019-12-12 08:57:58 -08001555 SYSC_QUIRK_CLKDM_NOAUTO),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001556 SYSC_QUIRK("dwc3", 0x488c0000, 0, 0x10, -ENODEV, 0x500a0200, 0xffffffff,
Tony Lindgren94f63452019-12-12 08:57:58 -08001557 SYSC_QUIRK_CLKDM_NOAUTO),
Tony Lindgrencfeeea62020-11-16 12:57:13 +02001558 SYSC_QUIRK("gpmc", 0, 0, 0x10, 0x14, 0x00000060, 0xffffffff,
1559 SYSC_QUIRK_GPMC_DEBUG),
Tony Lindgren77dfece2020-02-24 12:58:03 -08001560 SYSC_QUIRK("hdmi", 0, 0, 0x10, -ENODEV, 0x50030200, 0xffffffff,
1561 SYSC_QUIRK_OPT_CLKS_NEEDED),
Tony Lindgren4e23be42019-06-10 04:48:05 -07001562 SYSC_QUIRK("hdq1w", 0, 0, 0x14, 0x18, 0x00000006, 0xffffffff,
Tony Lindgrene275d212020-10-26 10:08:47 +02001563 SYSC_MODULE_QUIRK_HDQ1W | SYSC_MODULE_QUIRK_ENA_RESETDONE),
Tony Lindgren4e23be42019-06-10 04:48:05 -07001564 SYSC_QUIRK("hdq1w", 0, 0, 0x14, 0x18, 0x0000000a, 0xffffffff,
Tony Lindgrene275d212020-10-26 10:08:47 +02001565 SYSC_MODULE_QUIRK_HDQ1W | SYSC_MODULE_QUIRK_ENA_RESETDONE),
Tony Lindgren4e23be42019-06-10 04:48:05 -07001566 SYSC_QUIRK("i2c", 0, 0, 0x20, 0x10, 0x00000036, 0x000000ff,
Tony Lindgrene275d212020-10-26 10:08:47 +02001567 SYSC_MODULE_QUIRK_I2C | SYSC_MODULE_QUIRK_ENA_RESETDONE),
Tony Lindgren4e23be42019-06-10 04:48:05 -07001568 SYSC_QUIRK("i2c", 0, 0, 0x20, 0x10, 0x0000003c, 0x000000ff,
Tony Lindgrene275d212020-10-26 10:08:47 +02001569 SYSC_MODULE_QUIRK_I2C | SYSC_MODULE_QUIRK_ENA_RESETDONE),
Tony Lindgren4e23be42019-06-10 04:48:05 -07001570 SYSC_QUIRK("i2c", 0, 0, 0x20, 0x10, 0x00000040, 0x000000ff,
Tony Lindgrene275d212020-10-26 10:08:47 +02001571 SYSC_MODULE_QUIRK_I2C | SYSC_MODULE_QUIRK_ENA_RESETDONE),
Tony Lindgren4e23be42019-06-10 04:48:05 -07001572 SYSC_QUIRK("i2c", 0, 0, 0x10, 0x90, 0x5040000a, 0xfffff0f0,
Tony Lindgrene275d212020-10-26 10:08:47 +02001573 SYSC_MODULE_QUIRK_I2C | SYSC_MODULE_QUIRK_ENA_RESETDONE),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001574 SYSC_QUIRK("gpu", 0x50000000, 0x14, -ENODEV, -ENODEV, 0x00010201, 0xffffffff, 0),
1575 SYSC_QUIRK("gpu", 0x50000000, 0xfe00, 0xfe10, -ENODEV, 0x40000000 , 0xffffffff,
Tony Lindgrend7f563d2019-08-14 05:18:16 -07001576 SYSC_MODULE_QUIRK_SGX),
Tony Lindgrenaef067e2020-03-10 09:06:02 -07001577 SYSC_QUIRK("lcdc", 0, 0, 0x54, -ENODEV, 0x4f201000, 0xffffffff,
1578 SYSC_QUIRK_SWSUP_SIDLE | SYSC_QUIRK_SWSUP_MSTANDBY),
Peter Ujfalusi8122dc52021-07-17 15:09:24 +03001579 SYSC_QUIRK("mcasp", 0, 0, 0x4, -ENODEV, 0x44306302, 0xffffffff,
1580 SYSC_QUIRK_SWSUP_SIDLE),
Tony Lindgrene8639e12020-02-24 12:58:03 -08001581 SYSC_QUIRK("rtc", 0, 0x74, 0x78, -ENODEV, 0x4eb01908, 0xffff00f0,
1582 SYSC_MODULE_QUIRK_RTC_UNLOCK),
Tony Lindgren25bfaaa2020-03-03 09:31:00 -08001583 SYSC_QUIRK("tptc", 0, 0, 0x10, -ENODEV, 0x40006c00, 0xffffefff,
1584 SYSC_QUIRK_SWSUP_SIDLE | SYSC_QUIRK_SWSUP_MSTANDBY),
1585 SYSC_QUIRK("tptc", 0, 0, -ENODEV, -ENODEV, 0x40007c00, 0xffffffff,
1586 SYSC_QUIRK_SWSUP_SIDLE | SYSC_QUIRK_SWSUP_MSTANDBY),
Tony Lindgrendb8e7122021-05-07 12:09:03 +03001587 SYSC_QUIRK("sata", 0, 0xfc, 0x1100, -ENODEV, 0x5e412000, 0xffffffff,
1588 SYSC_QUIRK_SWSUP_SIDLE | SYSC_QUIRK_SWSUP_MSTANDBY),
Tony Lindgren42546322020-07-13 09:59:47 -07001589 SYSC_QUIRK("usb_host_hs", 0, 0, 0x10, 0x14, 0x50700100, 0xffffffff,
1590 SYSC_QUIRK_SWSUP_SIDLE | SYSC_QUIRK_SWSUP_MSTANDBY),
1591 SYSC_QUIRK("usb_host_hs", 0, 0, 0x10, -ENODEV, 0x50700101, 0xffffffff,
1592 SYSC_QUIRK_SWSUP_SIDLE | SYSC_QUIRK_SWSUP_MSTANDBY),
Tony Lindgren03856e92019-10-21 13:36:09 -07001593 SYSC_QUIRK("usb_otg_hs", 0, 0x400, 0x404, 0x408, 0x00000050,
1594 0xffffffff, SYSC_QUIRK_SWSUP_SIDLE | SYSC_QUIRK_SWSUP_MSTANDBY),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001595 SYSC_QUIRK("usb_otg_hs", 0, 0, 0x10, -ENODEV, 0x4ea2080d, 0xffffffff,
Tony Lindgren4d7b3242021-05-18 09:47:23 +03001596 SYSC_QUIRK_SWSUP_SIDLE | SYSC_QUIRK_SWSUP_MSTANDBY |
1597 SYSC_QUIRK_REINIT_ON_RESUME),
Tony Lindgren4e23be42019-06-10 04:48:05 -07001598 SYSC_QUIRK("wdt", 0, 0, 0x10, 0x14, 0x502a0500, 0xfffff0f0,
1599 SYSC_MODULE_QUIRK_WDT),
Suman Annab2745d92020-02-27 16:28:34 -06001600 /* PRUSS on am3, am4 and am5 */
1601 SYSC_QUIRK("pruss", 0, 0x26000, 0x26004, -ENODEV, 0x47000000, 0xff000000,
1602 SYSC_MODULE_QUIRK_PRUSS),
Tony Lindgrenc7d86692019-10-17 11:21:44 -07001603 /* Watchdog on am3 and am4 */
1604 SYSC_QUIRK("wdt", 0x44e35000, 0, 0x10, 0x14, 0x502a0500, 0xfffff0f0,
1605 SYSC_MODULE_QUIRK_WDT | SYSC_QUIRK_SWSUP_SIDLE),
Tony Lindgren4e23be42019-06-10 04:48:05 -07001606
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001607#ifdef DEBUG
Tony Lindgren590e15c2020-02-24 12:58:03 -08001608 SYSC_QUIRK("adc", 0, 0, 0x10, -ENODEV, 0x47300001, 0xffffffff, 0),
1609 SYSC_QUIRK("atl", 0, 0, -ENODEV, -ENODEV, 0x0a070100, 0xffffffff, 0),
1610 SYSC_QUIRK("cm", 0, 0, -ENODEV, -ENODEV, 0x40000301, 0xffffffff, 0),
1611 SYSC_QUIRK("control", 0, 0, 0x10, -ENODEV, 0x40000900, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001612 SYSC_QUIRK("cpgmac", 0, 0x1200, 0x1208, 0x1204, 0x4edb1902,
Tony Lindgren23731ea2018-09-24 12:17:09 -07001613 0xffff00f0, 0),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001614 SYSC_QUIRK("dcan", 0, 0x20, -ENODEV, -ENODEV, 0xa3170504, 0xffffffff, 0),
1615 SYSC_QUIRK("dcan", 0, 0x20, -ENODEV, -ENODEV, 0x4edb1902, 0xffffffff, 0),
Tony Lindgren77dfece2020-02-24 12:58:03 -08001616 SYSC_QUIRK("dispc", 0x4832a400, 0, 0x10, 0x14, 0x00000030, 0xffffffff, 0),
1617 SYSC_QUIRK("dispc", 0x58001000, 0, 0x10, 0x14, 0x00000040, 0xffffffff, 0),
1618 SYSC_QUIRK("dispc", 0x58001000, 0, 0x10, 0x14, 0x00000051, 0xffffffff, 0),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001619 SYSC_QUIRK("dmic", 0, 0, 0x10, -ENODEV, 0x50010000, 0xffffffff, 0),
Tony Lindgren77dfece2020-02-24 12:58:03 -08001620 SYSC_QUIRK("dsi", 0x58004000, 0, 0x10, 0x14, 0x00000030, 0xffffffff, 0),
1621 SYSC_QUIRK("dsi", 0x58005000, 0, 0x10, 0x14, 0x00000030, 0xffffffff, 0),
1622 SYSC_QUIRK("dsi", 0x58005000, 0, 0x10, 0x14, 0x00000040, 0xffffffff, 0),
1623 SYSC_QUIRK("dsi", 0x58009000, 0, 0x10, 0x14, 0x00000040, 0xffffffff, 0),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001624 SYSC_QUIRK("dwc3", 0, 0, 0x10, -ENODEV, 0x500a0200, 0xffffffff, 0),
Tony Lindgren7edd00f2019-08-14 05:18:16 -07001625 SYSC_QUIRK("d2d", 0x4a0b6000, 0, 0x10, 0x14, 0x00000010, 0xffffffff, 0),
1626 SYSC_QUIRK("d2d", 0x4a0cd000, 0, 0x10, 0x14, 0x00000010, 0xffffffff, 0),
Tony Lindgrenf2dc0752021-03-08 11:35:07 +02001627 SYSC_QUIRK("elm", 0x48080000, 0, 0x10, 0x14, 0x00000020, 0xffffffff, 0),
1628 SYSC_QUIRK("emif", 0, 0, -ENODEV, -ENODEV, 0x40441403, 0xffff0fff, 0),
1629 SYSC_QUIRK("emif", 0, 0, -ENODEV, -ENODEV, 0x50440500, 0xffffffff, 0),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001630 SYSC_QUIRK("epwmss", 0, 0, 0x4, -ENODEV, 0x47400001, 0xffffffff, 0),
1631 SYSC_QUIRK("gpu", 0, 0x1fc00, 0x1fc10, -ENODEV, 0, 0, 0),
1632 SYSC_QUIRK("gpu", 0, 0xfe00, 0xfe10, -ENODEV, 0x40000000 , 0xffffffff, 0),
Tony Lindgren77dfece2020-02-24 12:58:03 -08001633 SYSC_QUIRK("hdmi", 0, 0, 0x10, -ENODEV, 0x50031d00, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001634 SYSC_QUIRK("hsi", 0, 0, 0x10, 0x14, 0x50043101, 0xffffffff, 0),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001635 SYSC_QUIRK("iss", 0, 0, 0x10, -ENODEV, 0x40000101, 0xffffffff, 0),
Tony Lindgrenf2dc0752021-03-08 11:35:07 +02001636 SYSC_QUIRK("keypad", 0x4a31c000, 0, 0x10, 0x14, 0x00000020, 0xffffffff, 0),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001637 SYSC_QUIRK("mcasp", 0, 0, 0x4, -ENODEV, 0x44307b02, 0xffffffff, 0),
1638 SYSC_QUIRK("mcbsp", 0, -ENODEV, 0x8c, -ENODEV, 0, 0, 0),
1639 SYSC_QUIRK("mcspi", 0, 0, 0x10, -ENODEV, 0x40300a0b, 0xffff00ff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001640 SYSC_QUIRK("mcspi", 0, 0, 0x110, 0x114, 0x40300a0b, 0xffffffff, 0),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001641 SYSC_QUIRK("mailbox", 0, 0, 0x10, -ENODEV, 0x00000400, 0xffffffff, 0),
1642 SYSC_QUIRK("m3", 0, 0, -ENODEV, -ENODEV, 0x5f580105, 0x0fff0f00, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -07001643 SYSC_QUIRK("ocp2scp", 0, 0, 0x10, 0x14, 0x50060005, 0xfffffff0, 0),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001644 SYSC_QUIRK("ocp2scp", 0, 0, -ENODEV, -ENODEV, 0x50060007, 0xffffffff, 0),
1645 SYSC_QUIRK("padconf", 0, 0, 0x10, -ENODEV, 0x4fff0800, 0xffffffff, 0),
1646 SYSC_QUIRK("padconf", 0, 0, -ENODEV, -ENODEV, 0x40001100, 0xffffffff, 0),
Tony Lindgrenf2dc0752021-03-08 11:35:07 +02001647 SYSC_QUIRK("pcie", 0x51000000, -ENODEV, -ENODEV, -ENODEV, 0, 0, 0),
1648 SYSC_QUIRK("pcie", 0x51800000, -ENODEV, -ENODEV, -ENODEV, 0, 0, 0),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001649 SYSC_QUIRK("prcm", 0, 0, -ENODEV, -ENODEV, 0x40000100, 0xffffffff, 0),
1650 SYSC_QUIRK("prcm", 0, 0, -ENODEV, -ENODEV, 0x00004102, 0xffffffff, 0),
1651 SYSC_QUIRK("prcm", 0, 0, -ENODEV, -ENODEV, 0x40000400, 0xffffffff, 0),
Tony Lindgren77dfece2020-02-24 12:58:03 -08001652 SYSC_QUIRK("rfbi", 0x4832a800, 0, 0x10, 0x14, 0x00000010, 0xffffffff, 0),
1653 SYSC_QUIRK("rfbi", 0x58002000, 0, 0x10, 0x14, 0x00000010, 0xffffffff, 0),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001654 SYSC_QUIRK("scm", 0, 0, 0x10, -ENODEV, 0x40000900, 0xffffffff, 0),
1655 SYSC_QUIRK("scm", 0, 0, -ENODEV, -ENODEV, 0x4e8b0100, 0xffffffff, 0),
1656 SYSC_QUIRK("scm", 0, 0, -ENODEV, -ENODEV, 0x4f000100, 0xffffffff, 0),
1657 SYSC_QUIRK("scm", 0, 0, -ENODEV, -ENODEV, 0x40000900, 0xffffffff, 0),
1658 SYSC_QUIRK("scrm", 0, 0, -ENODEV, -ENODEV, 0x00000010, 0xffffffff, 0),
1659 SYSC_QUIRK("sdio", 0, 0, 0x10, -ENODEV, 0x40202301, 0xffff0ff0, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001660 SYSC_QUIRK("sdio", 0, 0x2fc, 0x110, 0x114, 0x31010000, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -07001661 SYSC_QUIRK("sdma", 0, 0, 0x2c, 0x28, 0x00010900, 0xffffffff, 0),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001662 SYSC_QUIRK("slimbus", 0, 0, 0x10, -ENODEV, 0x40000902, 0xffffffff, 0),
1663 SYSC_QUIRK("slimbus", 0, 0, 0x10, -ENODEV, 0x40002903, 0xffffffff, 0),
Tony Lindgrened4520d2021-08-09 11:27:47 -05001664 SYSC_QUIRK("smartreflex", 0, -ENODEV, 0x24, -ENODEV, 0x00000000, 0xffffffff, 0),
1665 SYSC_QUIRK("smartreflex", 0, -ENODEV, 0x38, -ENODEV, 0x00000000, 0xffffffff, 0),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001666 SYSC_QUIRK("spinlock", 0, 0, 0x10, -ENODEV, 0x50020000, 0xffffffff, 0),
1667 SYSC_QUIRK("rng", 0, 0x1fe0, 0x1fe4, -ENODEV, 0x00000020, 0xffffffff, 0),
Tony Lindgren1a542812020-05-07 09:43:08 -07001668 SYSC_QUIRK("timer", 0, 0, 0x10, 0x14, 0x00000013, 0xffffffff, 0),
1669 SYSC_QUIRK("timer", 0, 0, 0x10, 0x14, 0x00000015, 0xffffffff, 0),
1670 /* Some timers on omap4 and later */
1671 SYSC_QUIRK("timer", 0, 0, 0x10, -ENODEV, 0x50002100, 0xffffffff, 0),
1672 SYSC_QUIRK("timer", 0, 0, 0x10, -ENODEV, 0x4fff1301, 0xffff00ff, 0),
1673 SYSC_QUIRK("timer32k", 0, 0, 0x4, -ENODEV, 0x00000040, 0xffffffff, 0),
1674 SYSC_QUIRK("timer32k", 0, 0, 0x4, -ENODEV, 0x00000011, 0xffffffff, 0),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001675 SYSC_QUIRK("timer32k", 0, 0, 0x4, -ENODEV, 0x00000060, 0xffffffff, 0),
Tony Lindgren25bfaaa2020-03-03 09:31:00 -08001676 SYSC_QUIRK("tpcc", 0, 0, -ENODEV, -ENODEV, 0x40014c00, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001677 SYSC_QUIRK("usbhstll", 0, 0, 0x10, 0x14, 0x00000004, 0xffffffff, 0),
Tony Lindgrenf0106702018-11-15 14:46:53 -08001678 SYSC_QUIRK("usbhstll", 0, 0, 0x10, 0x14, 0x00000008, 0xffffffff, 0),
Tony Lindgren77dfece2020-02-24 12:58:03 -08001679 SYSC_QUIRK("venc", 0x58003000, 0, -ENODEV, -ENODEV, 0x00000002, 0xffffffff, 0),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001680 SYSC_QUIRK("vfpe", 0, 0, 0x104, -ENODEV, 0x4d001200, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001681#endif
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001682};
1683
Tony Lindgren42b9c5c2019-03-21 11:00:21 -07001684/*
1685 * Early quirks based on module base and register offsets only that are
1686 * needed before the module revision can be read
1687 */
1688static void sysc_init_early_quirks(struct sysc *ddata)
1689{
1690 const struct sysc_revision_quirk *q;
1691 int i;
1692
1693 for (i = 0; i < ARRAY_SIZE(sysc_revision_quirks); i++) {
1694 q = &sysc_revision_quirks[i];
1695
1696 if (!q->base)
1697 continue;
1698
1699 if (q->base != ddata->module_pa)
1700 continue;
1701
Tony Lindgren590e15c2020-02-24 12:58:03 -08001702 if (q->rev_offset != ddata->offsets[SYSC_REVISION])
Tony Lindgren42b9c5c2019-03-21 11:00:21 -07001703 continue;
1704
Tony Lindgren590e15c2020-02-24 12:58:03 -08001705 if (q->sysc_offset != ddata->offsets[SYSC_SYSCONFIG])
Tony Lindgren42b9c5c2019-03-21 11:00:21 -07001706 continue;
1707
Tony Lindgren590e15c2020-02-24 12:58:03 -08001708 if (q->syss_offset != ddata->offsets[SYSC_SYSSTATUS])
Tony Lindgren42b9c5c2019-03-21 11:00:21 -07001709 continue;
1710
1711 ddata->name = q->name;
1712 ddata->cfg.quirks |= q->quirks;
1713 }
1714}
1715
1716/* Quirks that also consider the revision register value */
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001717static void sysc_init_revision_quirks(struct sysc *ddata)
1718{
1719 const struct sysc_revision_quirk *q;
1720 int i;
1721
1722 for (i = 0; i < ARRAY_SIZE(sysc_revision_quirks); i++) {
1723 q = &sysc_revision_quirks[i];
1724
1725 if (q->base && q->base != ddata->module_pa)
1726 continue;
1727
Tony Lindgren590e15c2020-02-24 12:58:03 -08001728 if (q->rev_offset != ddata->offsets[SYSC_REVISION])
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001729 continue;
1730
Tony Lindgren590e15c2020-02-24 12:58:03 -08001731 if (q->sysc_offset != ddata->offsets[SYSC_SYSCONFIG])
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001732 continue;
1733
Tony Lindgren590e15c2020-02-24 12:58:03 -08001734 if (q->syss_offset != ddata->offsets[SYSC_SYSSTATUS])
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001735 continue;
1736
1737 if (q->revision == ddata->revision ||
1738 (q->revision & q->revision_mask) ==
1739 (ddata->revision & q->revision_mask)) {
1740 ddata->name = q->name;
1741 ddata->cfg.quirks |= q->quirks;
1742 }
1743 }
1744}
1745
Tony Lindgren7324a7a2020-02-24 12:58:03 -08001746/*
1747 * DSS needs dispc outputs disabled to reset modules. Returns mask of
1748 * enabled DSS interrupts. Eventually we may be able to do this on
1749 * dispc init rather than top-level DSS init.
1750 */
1751static u32 sysc_quirk_dispc(struct sysc *ddata, int dispc_offset,
1752 bool disable)
1753{
1754 bool lcd_en, digit_en, lcd2_en = false, lcd3_en = false;
1755 const int lcd_en_mask = BIT(0), digit_en_mask = BIT(1);
1756 int manager_count;
Tony Lindgren085bc0e2020-05-31 12:37:54 -07001757 bool framedonetv_irq = true;
Tony Lindgren7324a7a2020-02-24 12:58:03 -08001758 u32 val, irq_mask = 0;
1759
1760 switch (sysc_soc->soc) {
1761 case SOC_2420 ... SOC_3630:
1762 manager_count = 2;
1763 framedonetv_irq = false;
1764 break;
1765 case SOC_4430 ... SOC_4470:
1766 manager_count = 3;
1767 break;
1768 case SOC_5430:
1769 case SOC_DRA7:
1770 manager_count = 4;
1771 break;
1772 case SOC_AM4:
1773 manager_count = 1;
Tony Lindgren085bc0e2020-05-31 12:37:54 -07001774 framedonetv_irq = false;
Tony Lindgren7324a7a2020-02-24 12:58:03 -08001775 break;
1776 case SOC_UNKNOWN:
1777 default:
1778 return 0;
Yang Li52fbb5a2021-02-02 15:00:05 +08001779 }
Tony Lindgren7324a7a2020-02-24 12:58:03 -08001780
1781 /* Remap the whole module range to be able to reset dispc outputs */
1782 devm_iounmap(ddata->dev, ddata->module_va);
1783 ddata->module_va = devm_ioremap(ddata->dev,
1784 ddata->module_pa,
1785 ddata->module_size);
1786 if (!ddata->module_va)
1787 return -EIO;
1788
1789 /* DISP_CONTROL */
1790 val = sysc_read(ddata, dispc_offset + 0x40);
1791 lcd_en = val & lcd_en_mask;
1792 digit_en = val & digit_en_mask;
1793 if (lcd_en)
1794 irq_mask |= BIT(0); /* FRAMEDONE */
1795 if (digit_en) {
1796 if (framedonetv_irq)
1797 irq_mask |= BIT(24); /* FRAMEDONETV */
1798 else
1799 irq_mask |= BIT(2) | BIT(3); /* EVSYNC bits */
1800 }
1801 if (disable & (lcd_en | digit_en))
1802 sysc_write(ddata, dispc_offset + 0x40,
1803 val & ~(lcd_en_mask | digit_en_mask));
1804
1805 if (manager_count <= 2)
1806 return irq_mask;
1807
1808 /* DISPC_CONTROL2 */
1809 val = sysc_read(ddata, dispc_offset + 0x238);
1810 lcd2_en = val & lcd_en_mask;
1811 if (lcd2_en)
1812 irq_mask |= BIT(22); /* FRAMEDONE2 */
1813 if (disable && lcd2_en)
1814 sysc_write(ddata, dispc_offset + 0x238,
1815 val & ~lcd_en_mask);
1816
1817 if (manager_count <= 3)
1818 return irq_mask;
1819
1820 /* DISPC_CONTROL3 */
1821 val = sysc_read(ddata, dispc_offset + 0x848);
1822 lcd3_en = val & lcd_en_mask;
1823 if (lcd3_en)
1824 irq_mask |= BIT(30); /* FRAMEDONE3 */
1825 if (disable && lcd3_en)
1826 sysc_write(ddata, dispc_offset + 0x848,
1827 val & ~lcd_en_mask);
1828
1829 return irq_mask;
1830}
1831
1832/* DSS needs child outputs disabled and SDI registers cleared for reset */
1833static void sysc_pre_reset_quirk_dss(struct sysc *ddata)
1834{
1835 const int dispc_offset = 0x1000;
1836 int error;
1837 u32 irq_mask, val;
1838
1839 /* Get enabled outputs */
1840 irq_mask = sysc_quirk_dispc(ddata, dispc_offset, false);
1841 if (!irq_mask)
1842 return;
1843
1844 /* Clear IRQSTATUS */
Tony Lindgren69e60902020-03-03 07:17:43 -08001845 sysc_write(ddata, dispc_offset + 0x18, irq_mask);
Tony Lindgren7324a7a2020-02-24 12:58:03 -08001846
1847 /* Disable outputs */
1848 val = sysc_quirk_dispc(ddata, dispc_offset, true);
1849
1850 /* Poll IRQSTATUS */
1851 error = readl_poll_timeout(ddata->module_va + dispc_offset + 0x18,
1852 val, val != irq_mask, 100, 50);
1853 if (error)
1854 dev_warn(ddata->dev, "%s: timed out %08x !+ %08x\n",
1855 __func__, val, irq_mask);
1856
1857 if (sysc_soc->soc == SOC_3430) {
1858 /* Clear DSS_SDI_CONTROL */
Tony Lindgren69e60902020-03-03 07:17:43 -08001859 sysc_write(ddata, 0x44, 0);
Tony Lindgren7324a7a2020-02-24 12:58:03 -08001860
1861 /* Clear DSS_PLL_CONTROL */
Tony Lindgren69e60902020-03-03 07:17:43 -08001862 sysc_write(ddata, 0x48, 0);
Tony Lindgren7324a7a2020-02-24 12:58:03 -08001863 }
1864
1865 /* Clear DSS_CONTROL to switch DSS clock sources to PRCM if not */
Tony Lindgren69e60902020-03-03 07:17:43 -08001866 sysc_write(ddata, 0x40, 0);
Tony Lindgren7324a7a2020-02-24 12:58:03 -08001867}
1868
Tony Lindgren4e23be42019-06-10 04:48:05 -07001869/* 1-wire needs module's internal clocks enabled for reset */
Tony Lindgrenaec551c2020-02-24 12:58:03 -08001870static void sysc_pre_reset_quirk_hdq1w(struct sysc *ddata)
Tony Lindgren4e23be42019-06-10 04:48:05 -07001871{
1872 int offset = 0x0c; /* HDQ_CTRL_STATUS */
1873 u16 val;
1874
1875 val = sysc_read(ddata, offset);
1876 val |= BIT(5);
1877 sysc_write(ddata, offset, val);
1878}
1879
Tony Lindgren020003f2019-11-14 08:25:28 -08001880/* AESS (Audio Engine SubSystem) needs autogating set after enable */
1881static void sysc_module_enable_quirk_aess(struct sysc *ddata)
1882{
1883 int offset = 0x7c; /* AESS_AUTO_GATING_ENABLE */
1884
1885 sysc_write(ddata, offset, 1);
1886}
1887
Tony Lindgrene64c0212020-02-24 12:58:03 -08001888/* I2C needs to be disabled for reset */
Tony Lindgren4e23be42019-06-10 04:48:05 -07001889static void sysc_clk_quirk_i2c(struct sysc *ddata, bool enable)
1890{
1891 int offset;
1892 u16 val;
1893
1894 /* I2C_CON, omap2/3 is different from omap4 and later */
1895 if ((ddata->revision & 0xffffff00) == 0x001f0000)
1896 offset = 0x24;
1897 else
1898 offset = 0xa4;
1899
1900 /* I2C_EN */
1901 val = sysc_read(ddata, offset);
1902 if (enable)
1903 val |= BIT(15);
1904 else
1905 val &= ~BIT(15);
1906 sysc_write(ddata, offset, val);
1907}
1908
Tony Lindgrene64c0212020-02-24 12:58:03 -08001909static void sysc_pre_reset_quirk_i2c(struct sysc *ddata)
Tony Lindgren4e23be42019-06-10 04:48:05 -07001910{
1911 sysc_clk_quirk_i2c(ddata, false);
1912}
1913
Tony Lindgrene64c0212020-02-24 12:58:03 -08001914static void sysc_post_reset_quirk_i2c(struct sysc *ddata)
1915{
1916 sysc_clk_quirk_i2c(ddata, true);
1917}
1918
Tony Lindgrene8639e12020-02-24 12:58:03 -08001919/* RTC on am3 and 4 needs to be unlocked and locked for sysconfig */
1920static void sysc_quirk_rtc(struct sysc *ddata, bool lock)
1921{
1922 u32 val, kick0_val = 0, kick1_val = 0;
1923 unsigned long flags;
1924 int error;
1925
1926 if (!lock) {
1927 kick0_val = 0x83e70b13;
1928 kick1_val = 0x95a4f1e0;
1929 }
1930
1931 local_irq_save(flags);
1932 /* RTC_STATUS BUSY bit may stay active for 1/32768 seconds (~30 usec) */
Tony Lindgrenafe6f1e2020-07-02 10:41:02 -07001933 error = readl_poll_timeout_atomic(ddata->module_va + 0x44, val,
1934 !(val & BIT(0)), 100, 50);
Tony Lindgrene8639e12020-02-24 12:58:03 -08001935 if (error)
1936 dev_warn(ddata->dev, "rtc busy timeout\n");
1937 /* Now we have ~15 microseconds to read/write various registers */
1938 sysc_write(ddata, 0x6c, kick0_val);
1939 sysc_write(ddata, 0x70, kick1_val);
1940 local_irq_restore(flags);
1941}
1942
1943static void sysc_module_unlock_quirk_rtc(struct sysc *ddata)
1944{
1945 sysc_quirk_rtc(ddata, false);
1946}
1947
1948static void sysc_module_lock_quirk_rtc(struct sysc *ddata)
1949{
1950 sysc_quirk_rtc(ddata, true);
1951}
1952
Tony Lindgrend7f563d2019-08-14 05:18:16 -07001953/* 36xx SGX needs a quirk for to bypass OCP IPG interrupt logic */
1954static void sysc_module_enable_quirk_sgx(struct sysc *ddata)
1955{
1956 int offset = 0xff08; /* OCP_DEBUG_CONFIG */
1957 u32 val = BIT(31); /* THALIA_INT_BYPASS */
1958
1959 sysc_write(ddata, offset, val);
1960}
1961
Tony Lindgren4e23be42019-06-10 04:48:05 -07001962/* Watchdog timer needs a disable sequence after reset */
1963static void sysc_reset_done_quirk_wdt(struct sysc *ddata)
1964{
1965 int wps, spr, error;
1966 u32 val;
1967
1968 wps = 0x34;
1969 spr = 0x48;
1970
1971 sysc_write(ddata, spr, 0xaaaa);
1972 error = readl_poll_timeout(ddata->module_va + wps, val,
1973 !(val & 0x10), 100,
1974 MAX_MODULE_SOFTRESET_WAIT);
1975 if (error)
Tony Lindgrenc7d86692019-10-17 11:21:44 -07001976 dev_warn(ddata->dev, "wdt disable step1 failed\n");
Tony Lindgren4e23be42019-06-10 04:48:05 -07001977
Tony Lindgrenc7d86692019-10-17 11:21:44 -07001978 sysc_write(ddata, spr, 0x5555);
Tony Lindgren4e23be42019-06-10 04:48:05 -07001979 error = readl_poll_timeout(ddata->module_va + wps, val,
1980 !(val & 0x10), 100,
1981 MAX_MODULE_SOFTRESET_WAIT);
1982 if (error)
Tony Lindgrenc7d86692019-10-17 11:21:44 -07001983 dev_warn(ddata->dev, "wdt disable step2 failed\n");
Tony Lindgren4e23be42019-06-10 04:48:05 -07001984}
1985
Suman Annab2745d92020-02-27 16:28:34 -06001986/* PRUSS needs to set MSTANDBY_INIT inorder to idle properly */
1987static void sysc_module_disable_quirk_pruss(struct sysc *ddata)
1988{
1989 u32 reg;
1990
1991 reg = sysc_read(ddata, ddata->offsets[SYSC_SYSCONFIG]);
1992 reg |= SYSC_PRUSS_STANDBY_INIT;
1993 sysc_write(ddata, ddata->offsets[SYSC_SYSCONFIG], reg);
1994}
1995
Tony Lindgren4e23be42019-06-10 04:48:05 -07001996static void sysc_init_module_quirks(struct sysc *ddata)
1997{
1998 if (ddata->legacy_mode || !ddata->name)
1999 return;
2000
2001 if (ddata->cfg.quirks & SYSC_MODULE_QUIRK_HDQ1W) {
Tony Lindgrene64c0212020-02-24 12:58:03 -08002002 ddata->pre_reset_quirk = sysc_pre_reset_quirk_hdq1w;
Tony Lindgren4e23be42019-06-10 04:48:05 -07002003
2004 return;
2005 }
2006
Tony Lindgrencfeeea62020-11-16 12:57:13 +02002007#ifdef CONFIG_OMAP_GPMC_DEBUG
2008 if (ddata->cfg.quirks & SYSC_QUIRK_GPMC_DEBUG) {
2009 ddata->cfg.quirks |= SYSC_QUIRK_NO_RESET_ON_INIT;
2010
2011 return;
2012 }
2013#endif
2014
Tony Lindgren4e23be42019-06-10 04:48:05 -07002015 if (ddata->cfg.quirks & SYSC_MODULE_QUIRK_I2C) {
Tony Lindgrene64c0212020-02-24 12:58:03 -08002016 ddata->pre_reset_quirk = sysc_pre_reset_quirk_i2c;
2017 ddata->post_reset_quirk = sysc_post_reset_quirk_i2c;
Tony Lindgren4e23be42019-06-10 04:48:05 -07002018
2019 return;
2020 }
2021
Tony Lindgren020003f2019-11-14 08:25:28 -08002022 if (ddata->cfg.quirks & SYSC_MODULE_QUIRK_AESS)
2023 ddata->module_enable_quirk = sysc_module_enable_quirk_aess;
2024
Tony Lindgren7324a7a2020-02-24 12:58:03 -08002025 if (ddata->cfg.quirks & SYSC_MODULE_QUIRK_DSS_RESET)
2026 ddata->pre_reset_quirk = sysc_pre_reset_quirk_dss;
2027
Tony Lindgrene8639e12020-02-24 12:58:03 -08002028 if (ddata->cfg.quirks & SYSC_MODULE_QUIRK_RTC_UNLOCK) {
2029 ddata->module_unlock_quirk = sysc_module_unlock_quirk_rtc;
2030 ddata->module_lock_quirk = sysc_module_lock_quirk_rtc;
2031
2032 return;
2033 }
2034
Tony Lindgrend7f563d2019-08-14 05:18:16 -07002035 if (ddata->cfg.quirks & SYSC_MODULE_QUIRK_SGX)
2036 ddata->module_enable_quirk = sysc_module_enable_quirk_sgx;
2037
Tony Lindgrenc7d86692019-10-17 11:21:44 -07002038 if (ddata->cfg.quirks & SYSC_MODULE_QUIRK_WDT) {
Tony Lindgren4e23be42019-06-10 04:48:05 -07002039 ddata->reset_done_quirk = sysc_reset_done_quirk_wdt;
Tony Lindgrenc7d86692019-10-17 11:21:44 -07002040 ddata->module_disable_quirk = sysc_reset_done_quirk_wdt;
2041 }
Suman Annab2745d92020-02-27 16:28:34 -06002042
2043 if (ddata->cfg.quirks & SYSC_MODULE_QUIRK_PRUSS)
2044 ddata->module_disable_quirk = sysc_module_disable_quirk_pruss;
Tony Lindgren4e23be42019-06-10 04:48:05 -07002045}
2046
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07002047static int sysc_clockdomain_init(struct sysc *ddata)
2048{
2049 struct ti_sysc_platform_data *pdata = dev_get_platdata(ddata->dev);
2050 struct clk *fck = NULL, *ick = NULL;
2051 int error;
2052
2053 if (!pdata || !pdata->init_clockdomain)
2054 return 0;
2055
2056 switch (ddata->nr_clocks) {
2057 case 2:
2058 ick = ddata->clocks[SYSC_ICK];
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002059 fallthrough;
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07002060 case 1:
2061 fck = ddata->clocks[SYSC_FCK];
2062 break;
2063 case 0:
2064 return 0;
2065 }
2066
2067 error = pdata->init_clockdomain(ddata->dev, fck, ick, &ddata->cookie);
2068 if (!error || error == -ENODEV)
2069 return 0;
2070
2071 return error;
2072}
2073
Tony Lindgrena3e92e72019-03-21 11:00:21 -07002074/*
2075 * Note that pdata->init_module() typically does a reset first. After
2076 * pdata->init_module() is done, PM runtime can be used for the interconnect
2077 * target module.
2078 */
2079static int sysc_legacy_init(struct sysc *ddata)
2080{
2081 struct ti_sysc_platform_data *pdata = dev_get_platdata(ddata->dev);
2082 int error;
2083
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07002084 if (!pdata || !pdata->init_module)
Tony Lindgrena3e92e72019-03-21 11:00:21 -07002085 return 0;
2086
2087 error = pdata->init_module(ddata->dev, ddata->mdata, &ddata->cookie);
2088 if (error == -EEXIST)
2089 error = 0;
2090
2091 return error;
2092}
2093
Tony Lindgrene0db94f2019-05-27 04:51:53 -07002094/*
2095 * Note that the caller must ensure the interconnect target module is enabled
2096 * before calling reset. Otherwise reset will not complete.
2097 */
Faiz Abbas596e7952018-07-09 22:18:39 +05302098static int sysc_reset(struct sysc *ddata)
2099{
Tony Lindgrend46f9fb2020-05-31 12:37:54 -07002100 int sysc_offset, sysc_val, error;
2101 u32 sysc_mask;
Faiz Abbas596e7952018-07-09 22:18:39 +05302102
Tony Lindgrene0db94f2019-05-27 04:51:53 -07002103 sysc_offset = ddata->offsets[SYSC_SYSCONFIG];
Tony Lindgrene0db94f2019-05-27 04:51:53 -07002104
Tony Lindgrenab4d309d2020-02-24 12:58:03 -08002105 if (ddata->legacy_mode ||
Tony Lindgrene0db94f2019-05-27 04:51:53 -07002106 ddata->cap->regbits->srst_shift < 0 ||
Faiz Abbas596e7952018-07-09 22:18:39 +05302107 ddata->cfg.quirks & SYSC_QUIRK_NO_RESET_ON_INIT)
2108 return 0;
2109
Tony Lindgrene0db94f2019-05-27 04:51:53 -07002110 sysc_mask = BIT(ddata->cap->regbits->srst_shift);
Faiz Abbas596e7952018-07-09 22:18:39 +05302111
Tony Lindgrene64c0212020-02-24 12:58:03 -08002112 if (ddata->pre_reset_quirk)
2113 ddata->pre_reset_quirk(ddata);
Tony Lindgren4e23be42019-06-10 04:48:05 -07002114
Tony Lindgrenab4d309d2020-02-24 12:58:03 -08002115 if (sysc_offset >= 0) {
2116 sysc_val = sysc_read_sysconfig(ddata);
2117 sysc_val |= sysc_mask;
2118 sysc_write(ddata, sysc_offset, sysc_val);
2119 }
Faiz Abbas596e7952018-07-09 22:18:39 +05302120
Tony Lindgrene709ed72019-12-12 08:20:10 -08002121 if (ddata->cfg.srst_udelay)
2122 usleep_range(ddata->cfg.srst_udelay,
2123 ddata->cfg.srst_udelay * 2);
2124
Tony Lindgrene64c0212020-02-24 12:58:03 -08002125 if (ddata->post_reset_quirk)
2126 ddata->post_reset_quirk(ddata);
Tony Lindgren4e23be42019-06-10 04:48:05 -07002127
Tony Lindgrend46f9fb2020-05-31 12:37:54 -07002128 error = sysc_wait_softreset(ddata);
2129 if (error)
2130 dev_warn(ddata->dev, "OCP softreset timed out\n");
Tony Lindgrene0db94f2019-05-27 04:51:53 -07002131
Tony Lindgren4e23be42019-06-10 04:48:05 -07002132 if (ddata->reset_done_quirk)
2133 ddata->reset_done_quirk(ddata);
2134
Tony Lindgrene0db94f2019-05-27 04:51:53 -07002135 return error;
Faiz Abbas596e7952018-07-09 22:18:39 +05302136}
2137
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07002138/*
2139 * At this point the module is configured enough to read the revision but
2140 * module may not be completely configured yet to use PM runtime. Enable
2141 * all clocks directly during init to configure the quirks needed for PM
2142 * runtime based on the revision register.
2143 */
Tony Lindgren566a9b052017-12-15 09:41:19 -08002144static int sysc_init_module(struct sysc *ddata)
2145{
Tony Lindgren4097c9a2020-11-16 12:57:13 +02002146 bool rstctrl_deasserted = false;
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07002147 int error = 0;
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002148
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07002149 error = sysc_clockdomain_init(ddata);
2150 if (error)
2151 return error;
2152
Tony Lindgrend0989132019-09-05 07:37:22 -07002153 sysc_clkdm_deny_idle(ddata);
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07002154
Tony Lindgrend0989132019-09-05 07:37:22 -07002155 /*
2156 * Always enable clocks. The bootloader may or may not have enabled
2157 * the related clocks.
2158 */
2159 error = sysc_enable_opt_clocks(ddata);
2160 if (error)
2161 return error;
Tony Lindgren566a9b052017-12-15 09:41:19 -08002162
Tony Lindgrend0989132019-09-05 07:37:22 -07002163 error = sysc_enable_main_clocks(ddata);
2164 if (error)
2165 goto err_opt_clocks;
Faiz Abbas596e7952018-07-09 22:18:39 +05302166
Tony Lindgrenea5a2e42019-05-27 04:51:56 -07002167 if (!(ddata->cfg.quirks & SYSC_QUIRK_NO_RESET_ON_INIT)) {
Tero Kristodf4f3452019-10-07 15:29:30 +03002168 error = reset_control_deassert(ddata->rsts);
Tony Lindgrenea5a2e42019-05-27 04:51:56 -07002169 if (error)
2170 goto err_main_clocks;
Tony Lindgren4097c9a2020-11-16 12:57:13 +02002171 rstctrl_deasserted = true;
Tony Lindgrenea5a2e42019-05-27 04:51:56 -07002172 }
2173
Tony Lindgren566a9b052017-12-15 09:41:19 -08002174 ddata->revision = sysc_read_revision(ddata);
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002175 sysc_init_revision_quirks(ddata);
Tony Lindgren4e23be42019-06-10 04:48:05 -07002176 sysc_init_module_quirks(ddata);
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002177
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07002178 if (ddata->legacy_mode) {
2179 error = sysc_legacy_init(ddata);
2180 if (error)
Tony Lindgren4097c9a2020-11-16 12:57:13 +02002181 goto err_main_clocks;
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07002182 }
2183
Tony Lindgrend0989132019-09-05 07:37:22 -07002184 if (!ddata->legacy_mode) {
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07002185 error = sysc_enable_module(ddata->dev);
2186 if (error)
Tony Lindgren4097c9a2020-11-16 12:57:13 +02002187 goto err_main_clocks;
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07002188 }
Tony Lindgrena3e92e72019-03-21 11:00:21 -07002189
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07002190 error = sysc_reset(ddata);
2191 if (error)
2192 dev_err(ddata->dev, "Reset failed with %d\n", error);
2193
Tero Kristocdc56c12019-10-07 15:29:31 +03002194 if (error && !ddata->legacy_mode)
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07002195 sysc_disable_module(ddata->dev);
2196
Tony Lindgrena3e92e72019-03-21 11:00:21 -07002197err_main_clocks:
Tero Kristocdc56c12019-10-07 15:29:31 +03002198 if (error)
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07002199 sysc_disable_main_clocks(ddata);
2200err_opt_clocks:
Tony Lindgrend0989132019-09-05 07:37:22 -07002201 /* No re-enable of clockdomain autoidle to prevent module autoidle */
Tero Kristocdc56c12019-10-07 15:29:31 +03002202 if (error) {
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07002203 sysc_disable_opt_clocks(ddata);
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07002204 sysc_clkdm_allow_idle(ddata);
2205 }
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07002206
Tony Lindgren4097c9a2020-11-16 12:57:13 +02002207 if (error && rstctrl_deasserted &&
2208 !(ddata->cfg.quirks & SYSC_QUIRK_NO_RESET_ON_INIT))
2209 reset_control_assert(ddata->rsts);
2210
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07002211 return error;
Tony Lindgren566a9b052017-12-15 09:41:19 -08002212}
2213
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08002214static int sysc_init_sysc_mask(struct sysc *ddata)
2215{
2216 struct device_node *np = ddata->dev->of_node;
2217 int error;
2218 u32 val;
2219
2220 error = of_property_read_u32(np, "ti,sysc-mask", &val);
2221 if (error)
2222 return 0;
2223
Tony Lindgrene212abd2019-07-22 03:44:52 -07002224 ddata->cfg.sysc_val = val & ddata->cap->sysc_mask;
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08002225
2226 return 0;
2227}
2228
2229static int sysc_init_idlemode(struct sysc *ddata, u8 *idlemodes,
2230 const char *name)
2231{
2232 struct device_node *np = ddata->dev->of_node;
2233 struct property *prop;
2234 const __be32 *p;
2235 u32 val;
2236
2237 of_property_for_each_u32(np, name, prop, p, val) {
2238 if (val >= SYSC_NR_IDLEMODES) {
2239 dev_err(ddata->dev, "invalid idlemode: %i\n", val);
2240 return -EINVAL;
2241 }
2242 *idlemodes |= (1 << val);
2243 }
2244
2245 return 0;
2246}
2247
2248static int sysc_init_idlemodes(struct sysc *ddata)
2249{
2250 int error;
2251
2252 error = sysc_init_idlemode(ddata, &ddata->cfg.midlemodes,
2253 "ti,sysc-midle");
2254 if (error)
2255 return error;
2256
2257 error = sysc_init_idlemode(ddata, &ddata->cfg.sidlemodes,
2258 "ti,sysc-sidle");
2259 if (error)
2260 return error;
2261
2262 return 0;
2263}
2264
2265/*
2266 * Only some devices on omap4 and later have SYSCONFIG reset done
2267 * bit. We can detect this if there is no SYSSTATUS at all, or the
2268 * SYSTATUS bit 0 is not used. Note that some SYSSTATUS registers
2269 * have multiple bits for the child devices like OHCI and EHCI.
2270 * Depends on SYSC being parsed first.
2271 */
2272static int sysc_init_syss_mask(struct sysc *ddata)
2273{
2274 struct device_node *np = ddata->dev->of_node;
2275 int error;
2276 u32 val;
2277
2278 error = of_property_read_u32(np, "ti,syss-mask", &val);
2279 if (error) {
2280 if ((ddata->cap->type == TI_SYSC_OMAP4 ||
2281 ddata->cap->type == TI_SYSC_OMAP4_TIMER) &&
2282 (ddata->cfg.sysc_val & SYSC_OMAP4_SOFTRESET))
2283 ddata->cfg.quirks |= SYSC_QUIRK_RESET_STATUS;
2284
2285 return 0;
2286 }
2287
2288 if (!(val & 1) && (ddata->cfg.sysc_val & SYSC_OMAP4_SOFTRESET))
2289 ddata->cfg.quirks |= SYSC_QUIRK_RESET_STATUS;
2290
2291 ddata->cfg.syss_mask = val;
2292
2293 return 0;
2294}
2295
Tony Lindgren2c355ff2018-02-22 13:58:03 -08002296/*
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07002297 * Many child device drivers need to have fck and opt clocks available
2298 * to get the clock rate for device internal configuration etc.
Tony Lindgren2c355ff2018-02-22 13:58:03 -08002299 */
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07002300static int sysc_child_add_named_clock(struct sysc *ddata,
2301 struct device *child,
2302 const char *name)
Tony Lindgren2c355ff2018-02-22 13:58:03 -08002303{
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07002304 struct clk *clk;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08002305 struct clk_lookup *l;
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07002306 int error = 0;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08002307
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07002308 if (!name)
Tony Lindgren2c355ff2018-02-22 13:58:03 -08002309 return 0;
2310
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07002311 clk = clk_get(child, name);
2312 if (!IS_ERR(clk)) {
Markus Elfringcb6cfe22019-11-06 19:12:30 +01002313 error = -EEXIST;
2314 goto put_clk;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08002315 }
2316
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07002317 clk = clk_get(ddata->dev, name);
2318 if (IS_ERR(clk))
2319 return -ENODEV;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08002320
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07002321 l = clkdev_create(clk, name, dev_name(child));
2322 if (!l)
2323 error = -ENOMEM;
Markus Elfringcb6cfe22019-11-06 19:12:30 +01002324put_clk:
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07002325 clk_put(clk);
2326
2327 return error;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08002328}
2329
Tony Lindgren09dfe582018-04-16 10:25:52 -07002330static int sysc_child_add_clocks(struct sysc *ddata,
2331 struct device *child)
2332{
2333 int i, error;
2334
2335 for (i = 0; i < ddata->nr_clocks; i++) {
2336 error = sysc_child_add_named_clock(ddata,
2337 child,
2338 ddata->clock_roles[i]);
2339 if (error && error != -EEXIST) {
2340 dev_err(ddata->dev, "could not add child clock %s: %i\n",
2341 ddata->clock_roles[i], error);
2342
2343 return error;
2344 }
2345 }
2346
2347 return 0;
2348}
2349
Tony Lindgren2c355ff2018-02-22 13:58:03 -08002350static struct device_type sysc_device_type = {
2351};
2352
2353static struct sysc *sysc_child_to_parent(struct device *dev)
2354{
2355 struct device *parent = dev->parent;
2356
2357 if (!parent || parent->type != &sysc_device_type)
2358 return NULL;
2359
2360 return dev_get_drvdata(parent);
2361}
2362
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002363static int __maybe_unused sysc_child_runtime_suspend(struct device *dev)
2364{
2365 struct sysc *ddata;
2366 int error;
2367
2368 ddata = sysc_child_to_parent(dev);
2369
2370 error = pm_generic_runtime_suspend(dev);
2371 if (error)
2372 return error;
2373
2374 if (!ddata->enabled)
2375 return 0;
2376
2377 return sysc_runtime_suspend(ddata->dev);
2378}
2379
2380static int __maybe_unused sysc_child_runtime_resume(struct device *dev)
2381{
2382 struct sysc *ddata;
2383 int error;
2384
2385 ddata = sysc_child_to_parent(dev);
2386
2387 if (!ddata->enabled) {
2388 error = sysc_runtime_resume(ddata->dev);
2389 if (error < 0)
2390 dev_err(ddata->dev,
2391 "%s error: %i\n", __func__, error);
2392 }
2393
2394 return pm_generic_runtime_resume(dev);
2395}
2396
2397#ifdef CONFIG_PM_SLEEP
2398static int sysc_child_suspend_noirq(struct device *dev)
2399{
2400 struct sysc *ddata;
2401 int error;
2402
2403 ddata = sysc_child_to_parent(dev);
2404
Tony Lindgrenef55f822018-04-16 10:27:15 -07002405 dev_dbg(ddata->dev, "%s %s\n", __func__,
2406 ddata->name ? ddata->name : "");
2407
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002408 error = pm_generic_suspend_noirq(dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07002409 if (error) {
2410 dev_err(dev, "%s error at %i: %i\n",
2411 __func__, __LINE__, error);
2412
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002413 return error;
Tony Lindgrenef55f822018-04-16 10:27:15 -07002414 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002415
2416 if (!pm_runtime_status_suspended(dev)) {
2417 error = pm_generic_runtime_suspend(dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07002418 if (error) {
Tony Lindgrenf9490782018-09-28 15:21:50 -07002419 dev_dbg(dev, "%s busy at %i: %i\n",
2420 __func__, __LINE__, error);
Tony Lindgrenef55f822018-04-16 10:27:15 -07002421
Tony Lindgren4f3530f2018-08-08 01:07:06 -07002422 return 0;
Tony Lindgrenef55f822018-04-16 10:27:15 -07002423 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002424
2425 error = sysc_runtime_suspend(ddata->dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07002426 if (error) {
2427 dev_err(dev, "%s error at %i: %i\n",
2428 __func__, __LINE__, error);
2429
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002430 return error;
Tony Lindgrenef55f822018-04-16 10:27:15 -07002431 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002432
2433 ddata->child_needs_resume = true;
2434 }
2435
2436 return 0;
2437}
2438
2439static int sysc_child_resume_noirq(struct device *dev)
2440{
2441 struct sysc *ddata;
2442 int error;
2443
2444 ddata = sysc_child_to_parent(dev);
2445
Tony Lindgrenef55f822018-04-16 10:27:15 -07002446 dev_dbg(ddata->dev, "%s %s\n", __func__,
2447 ddata->name ? ddata->name : "");
2448
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002449 if (ddata->child_needs_resume) {
2450 ddata->child_needs_resume = false;
2451
2452 error = sysc_runtime_resume(ddata->dev);
2453 if (error)
2454 dev_err(ddata->dev,
2455 "%s runtime resume error: %i\n",
2456 __func__, error);
2457
2458 error = pm_generic_runtime_resume(dev);
2459 if (error)
2460 dev_err(ddata->dev,
2461 "%s generic runtime resume: %i\n",
2462 __func__, error);
2463 }
2464
2465 return pm_generic_resume_noirq(dev);
2466}
2467#endif
2468
Tony Lindgrenb7182b42019-03-21 11:00:21 -07002469static struct dev_pm_domain sysc_child_pm_domain = {
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002470 .ops = {
2471 SET_RUNTIME_PM_OPS(sysc_child_runtime_suspend,
2472 sysc_child_runtime_resume,
2473 NULL)
2474 USE_PLATFORM_PM_SLEEP_OPS
2475 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(sysc_child_suspend_noirq,
2476 sysc_child_resume_noirq)
2477 }
2478};
2479
2480/**
2481 * sysc_legacy_idle_quirk - handle children in omap_device compatible way
2482 * @ddata: device driver data
2483 * @child: child device driver
2484 *
2485 * Allow idle for child devices as done with _od_runtime_suspend().
2486 * Otherwise many child devices will not idle because of the permanent
2487 * parent usecount set in pm_runtime_irq_safe().
2488 *
2489 * Note that the long term solution is to just modify the child device
2490 * drivers to not set pm_runtime_irq_safe() and then this can be just
2491 * dropped.
2492 */
2493static void sysc_legacy_idle_quirk(struct sysc *ddata, struct device *child)
2494{
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002495 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
2496 dev_pm_domain_set(child, &sysc_child_pm_domain);
2497}
2498
Tony Lindgren2c355ff2018-02-22 13:58:03 -08002499static int sysc_notifier_call(struct notifier_block *nb,
2500 unsigned long event, void *device)
2501{
2502 struct device *dev = device;
2503 struct sysc *ddata;
2504 int error;
2505
2506 ddata = sysc_child_to_parent(dev);
2507 if (!ddata)
2508 return NOTIFY_DONE;
2509
2510 switch (event) {
2511 case BUS_NOTIFY_ADD_DEVICE:
Tony Lindgren09dfe582018-04-16 10:25:52 -07002512 error = sysc_child_add_clocks(ddata, dev);
2513 if (error)
2514 return error;
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002515 sysc_legacy_idle_quirk(ddata, dev);
Tony Lindgren2c355ff2018-02-22 13:58:03 -08002516 break;
2517 default:
2518 break;
2519 }
2520
2521 return NOTIFY_DONE;
2522}
2523
2524static struct notifier_block sysc_nb = {
2525 .notifier_call = sysc_notifier_call,
2526};
2527
Tony Lindgren566a9b052017-12-15 09:41:19 -08002528/* Device tree configured quirks */
2529struct sysc_dts_quirk {
2530 const char *name;
2531 u32 mask;
2532};
2533
2534static const struct sysc_dts_quirk sysc_dts_quirks[] = {
2535 { .name = "ti,no-idle-on-init",
2536 .mask = SYSC_QUIRK_NO_IDLE_ON_INIT, },
2537 { .name = "ti,no-reset-on-init",
2538 .mask = SYSC_QUIRK_NO_RESET_ON_INIT, },
Tony Lindgren386cb762019-03-22 07:49:30 -07002539 { .name = "ti,no-idle",
2540 .mask = SYSC_QUIRK_NO_IDLE, },
Tony Lindgren566a9b052017-12-15 09:41:19 -08002541};
2542
Tony Lindgren4014c082018-12-10 14:11:26 -08002543static void sysc_parse_dts_quirks(struct sysc *ddata, struct device_node *np,
2544 bool is_child)
Tony Lindgren566a9b052017-12-15 09:41:19 -08002545{
Tony Lindgren566a9b052017-12-15 09:41:19 -08002546 const struct property *prop;
Tony Lindgren4014c082018-12-10 14:11:26 -08002547 int i, len;
Tony Lindgren566a9b052017-12-15 09:41:19 -08002548
2549 for (i = 0; i < ARRAY_SIZE(sysc_dts_quirks); i++) {
Tony Lindgren4014c082018-12-10 14:11:26 -08002550 const char *name = sysc_dts_quirks[i].name;
2551
2552 prop = of_get_property(np, name, &len);
Tony Lindgren566a9b052017-12-15 09:41:19 -08002553 if (!prop)
Tony Lindgrend39b6ea2018-02-15 09:18:55 -08002554 continue;
Tony Lindgren566a9b052017-12-15 09:41:19 -08002555
2556 ddata->cfg.quirks |= sysc_dts_quirks[i].mask;
Tony Lindgren4014c082018-12-10 14:11:26 -08002557 if (is_child) {
2558 dev_warn(ddata->dev,
2559 "dts flag should be at module level for %s\n",
2560 name);
2561 }
Tony Lindgren566a9b052017-12-15 09:41:19 -08002562 }
Tony Lindgren4014c082018-12-10 14:11:26 -08002563}
Tony Lindgren566a9b052017-12-15 09:41:19 -08002564
Tony Lindgren4014c082018-12-10 14:11:26 -08002565static int sysc_init_dts_quirks(struct sysc *ddata)
2566{
2567 struct device_node *np = ddata->dev->of_node;
2568 int error;
2569 u32 val;
2570
2571 ddata->legacy_mode = of_get_property(np, "ti,hwmods", NULL);
2572
2573 sysc_parse_dts_quirks(ddata, np, false);
Tony Lindgren566a9b052017-12-15 09:41:19 -08002574 error = of_property_read_u32(np, "ti,sysc-delay-us", &val);
2575 if (!error) {
2576 if (val > 255) {
2577 dev_warn(ddata->dev, "bad ti,sysc-delay-us: %i\n",
2578 val);
2579 }
2580
2581 ddata->cfg.srst_udelay = (u8)val;
2582 }
2583
2584 return 0;
2585}
2586
Tony Lindgren0eecc632017-10-10 14:23:43 -07002587static void sysc_unprepare(struct sysc *ddata)
2588{
2589 int i;
2590
Tony Lindgrenaaa29bb2019-03-21 11:00:21 -07002591 if (!ddata->clocks)
2592 return;
2593
Tony Lindgren0eecc632017-10-10 14:23:43 -07002594 for (i = 0; i < SYSC_MAX_CLOCKS; i++) {
2595 if (!IS_ERR_OR_NULL(ddata->clocks[i]))
2596 clk_unprepare(ddata->clocks[i]);
2597 }
2598}
2599
Tony Lindgren70a65242017-12-15 09:41:09 -08002600/*
2601 * Common sysc register bits found on omap2, also known as type1
2602 */
2603static const struct sysc_regbits sysc_regbits_omap2 = {
2604 .dmadisable_shift = -ENODEV,
2605 .midle_shift = 12,
2606 .sidle_shift = 3,
2607 .clkact_shift = 8,
2608 .emufree_shift = 5,
2609 .enwkup_shift = 2,
2610 .srst_shift = 1,
2611 .autoidle_shift = 0,
2612};
2613
2614static const struct sysc_capabilities sysc_omap2 = {
2615 .type = TI_SYSC_OMAP2,
2616 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY | SYSC_OMAP2_EMUFREE |
2617 SYSC_OMAP2_ENAWAKEUP | SYSC_OMAP2_SOFTRESET |
2618 SYSC_OMAP2_AUTOIDLE,
2619 .regbits = &sysc_regbits_omap2,
2620};
2621
2622/* All omap2 and 3 timers, and timers 1, 2 & 10 on omap 4 and 5 */
2623static const struct sysc_capabilities sysc_omap2_timer = {
2624 .type = TI_SYSC_OMAP2_TIMER,
2625 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY | SYSC_OMAP2_EMUFREE |
2626 SYSC_OMAP2_ENAWAKEUP | SYSC_OMAP2_SOFTRESET |
2627 SYSC_OMAP2_AUTOIDLE,
2628 .regbits = &sysc_regbits_omap2,
2629 .mod_quirks = SYSC_QUIRK_USE_CLOCKACT,
2630};
2631
2632/*
2633 * SHAM2 (SHA1/MD5) sysc found on omap3, a variant of sysc_regbits_omap2
2634 * with different sidle position
2635 */
2636static const struct sysc_regbits sysc_regbits_omap3_sham = {
2637 .dmadisable_shift = -ENODEV,
2638 .midle_shift = -ENODEV,
2639 .sidle_shift = 4,
2640 .clkact_shift = -ENODEV,
2641 .enwkup_shift = -ENODEV,
2642 .srst_shift = 1,
2643 .autoidle_shift = 0,
2644 .emufree_shift = -ENODEV,
2645};
2646
2647static const struct sysc_capabilities sysc_omap3_sham = {
2648 .type = TI_SYSC_OMAP3_SHAM,
2649 .sysc_mask = SYSC_OMAP2_SOFTRESET | SYSC_OMAP2_AUTOIDLE,
2650 .regbits = &sysc_regbits_omap3_sham,
2651};
2652
2653/*
2654 * AES register bits found on omap3 and later, a variant of
2655 * sysc_regbits_omap2 with different sidle position
2656 */
2657static const struct sysc_regbits sysc_regbits_omap3_aes = {
2658 .dmadisable_shift = -ENODEV,
2659 .midle_shift = -ENODEV,
2660 .sidle_shift = 6,
2661 .clkact_shift = -ENODEV,
2662 .enwkup_shift = -ENODEV,
2663 .srst_shift = 1,
2664 .autoidle_shift = 0,
2665 .emufree_shift = -ENODEV,
2666};
2667
2668static const struct sysc_capabilities sysc_omap3_aes = {
2669 .type = TI_SYSC_OMAP3_AES,
2670 .sysc_mask = SYSC_OMAP2_SOFTRESET | SYSC_OMAP2_AUTOIDLE,
2671 .regbits = &sysc_regbits_omap3_aes,
2672};
2673
2674/*
2675 * Common sysc register bits found on omap4, also known as type2
2676 */
2677static const struct sysc_regbits sysc_regbits_omap4 = {
2678 .dmadisable_shift = 16,
2679 .midle_shift = 4,
2680 .sidle_shift = 2,
2681 .clkact_shift = -ENODEV,
2682 .enwkup_shift = -ENODEV,
2683 .emufree_shift = 1,
2684 .srst_shift = 0,
2685 .autoidle_shift = -ENODEV,
2686};
2687
2688static const struct sysc_capabilities sysc_omap4 = {
2689 .type = TI_SYSC_OMAP4,
2690 .sysc_mask = SYSC_OMAP4_DMADISABLE | SYSC_OMAP4_FREEEMU |
2691 SYSC_OMAP4_SOFTRESET,
2692 .regbits = &sysc_regbits_omap4,
2693};
2694
2695static const struct sysc_capabilities sysc_omap4_timer = {
2696 .type = TI_SYSC_OMAP4_TIMER,
2697 .sysc_mask = SYSC_OMAP4_DMADISABLE | SYSC_OMAP4_FREEEMU |
2698 SYSC_OMAP4_SOFTRESET,
2699 .regbits = &sysc_regbits_omap4,
2700};
2701
2702/*
2703 * Common sysc register bits found on omap4, also known as type3
2704 */
2705static const struct sysc_regbits sysc_regbits_omap4_simple = {
2706 .dmadisable_shift = -ENODEV,
2707 .midle_shift = 2,
2708 .sidle_shift = 0,
2709 .clkact_shift = -ENODEV,
2710 .enwkup_shift = -ENODEV,
2711 .srst_shift = -ENODEV,
2712 .emufree_shift = -ENODEV,
2713 .autoidle_shift = -ENODEV,
2714};
2715
2716static const struct sysc_capabilities sysc_omap4_simple = {
2717 .type = TI_SYSC_OMAP4_SIMPLE,
2718 .regbits = &sysc_regbits_omap4_simple,
2719};
2720
2721/*
2722 * SmartReflex sysc found on omap34xx
2723 */
2724static const struct sysc_regbits sysc_regbits_omap34xx_sr = {
2725 .dmadisable_shift = -ENODEV,
2726 .midle_shift = -ENODEV,
2727 .sidle_shift = -ENODEV,
2728 .clkact_shift = 20,
2729 .enwkup_shift = -ENODEV,
2730 .srst_shift = -ENODEV,
2731 .emufree_shift = -ENODEV,
2732 .autoidle_shift = -ENODEV,
2733};
2734
2735static const struct sysc_capabilities sysc_34xx_sr = {
2736 .type = TI_SYSC_OMAP34XX_SR,
2737 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY,
2738 .regbits = &sysc_regbits_omap34xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002739 .mod_quirks = SYSC_QUIRK_USE_CLOCKACT | SYSC_QUIRK_UNCACHED |
2740 SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08002741};
2742
2743/*
2744 * SmartReflex sysc found on omap36xx and later
2745 */
2746static const struct sysc_regbits sysc_regbits_omap36xx_sr = {
2747 .dmadisable_shift = -ENODEV,
2748 .midle_shift = -ENODEV,
2749 .sidle_shift = 24,
2750 .clkact_shift = -ENODEV,
2751 .enwkup_shift = 26,
2752 .srst_shift = -ENODEV,
2753 .emufree_shift = -ENODEV,
2754 .autoidle_shift = -ENODEV,
2755};
2756
2757static const struct sysc_capabilities sysc_36xx_sr = {
2758 .type = TI_SYSC_OMAP36XX_SR,
Tony Lindgren3267c082018-01-22 09:32:53 -08002759 .sysc_mask = SYSC_OMAP3_SR_ENAWAKEUP,
Tony Lindgren70a65242017-12-15 09:41:09 -08002760 .regbits = &sysc_regbits_omap36xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002761 .mod_quirks = SYSC_QUIRK_UNCACHED | SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08002762};
2763
2764static const struct sysc_capabilities sysc_omap4_sr = {
2765 .type = TI_SYSC_OMAP4_SR,
2766 .regbits = &sysc_regbits_omap36xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002767 .mod_quirks = SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08002768};
2769
2770/*
2771 * McASP register bits found on omap4 and later
2772 */
2773static const struct sysc_regbits sysc_regbits_omap4_mcasp = {
2774 .dmadisable_shift = -ENODEV,
2775 .midle_shift = -ENODEV,
2776 .sidle_shift = 0,
2777 .clkact_shift = -ENODEV,
2778 .enwkup_shift = -ENODEV,
2779 .srst_shift = -ENODEV,
2780 .emufree_shift = -ENODEV,
2781 .autoidle_shift = -ENODEV,
2782};
2783
2784static const struct sysc_capabilities sysc_omap4_mcasp = {
2785 .type = TI_SYSC_OMAP4_MCASP,
2786 .regbits = &sysc_regbits_omap4_mcasp,
Tony Lindgren2c63a832018-11-15 14:46:53 -08002787 .mod_quirks = SYSC_QUIRK_OPT_CLKS_NEEDED,
2788};
2789
2790/*
2791 * McASP found on dra7 and later
2792 */
2793static const struct sysc_capabilities sysc_dra7_mcasp = {
2794 .type = TI_SYSC_OMAP4_SIMPLE,
2795 .regbits = &sysc_regbits_omap4_simple,
2796 .mod_quirks = SYSC_QUIRK_OPT_CLKS_NEEDED,
Tony Lindgren70a65242017-12-15 09:41:09 -08002797};
2798
2799/*
2800 * FS USB host found on omap4 and later
2801 */
2802static const struct sysc_regbits sysc_regbits_omap4_usb_host_fs = {
2803 .dmadisable_shift = -ENODEV,
2804 .midle_shift = -ENODEV,
2805 .sidle_shift = 24,
2806 .clkact_shift = -ENODEV,
2807 .enwkup_shift = 26,
2808 .srst_shift = -ENODEV,
2809 .emufree_shift = -ENODEV,
2810 .autoidle_shift = -ENODEV,
2811};
2812
2813static const struct sysc_capabilities sysc_omap4_usb_host_fs = {
2814 .type = TI_SYSC_OMAP4_USB_HOST_FS,
2815 .sysc_mask = SYSC_OMAP2_ENAWAKEUP,
2816 .regbits = &sysc_regbits_omap4_usb_host_fs,
2817};
2818
Faiz Abbas7f35e632018-07-09 22:18:38 +05302819static const struct sysc_regbits sysc_regbits_dra7_mcan = {
2820 .dmadisable_shift = -ENODEV,
2821 .midle_shift = -ENODEV,
2822 .sidle_shift = -ENODEV,
2823 .clkact_shift = -ENODEV,
2824 .enwkup_shift = 4,
2825 .srst_shift = 0,
2826 .emufree_shift = -ENODEV,
2827 .autoidle_shift = -ENODEV,
2828};
2829
2830static const struct sysc_capabilities sysc_dra7_mcan = {
2831 .type = TI_SYSC_DRA7_MCAN,
2832 .sysc_mask = SYSC_DRA7_MCAN_ENAWAKEUP | SYSC_OMAP4_SOFTRESET,
2833 .regbits = &sysc_regbits_dra7_mcan,
Tony Lindgrene0db94f2019-05-27 04:51:53 -07002834 .mod_quirks = SYSS_QUIRK_RESETDONE_INVERTED,
Faiz Abbas7f35e632018-07-09 22:18:38 +05302835};
2836
Suman Annab2745d92020-02-27 16:28:34 -06002837/*
2838 * PRUSS found on some AM33xx, AM437x and AM57xx SoCs
2839 */
2840static const struct sysc_capabilities sysc_pruss = {
2841 .type = TI_SYSC_PRUSS,
2842 .sysc_mask = SYSC_PRUSS_STANDBY_INIT | SYSC_PRUSS_SUB_MWAIT,
2843 .regbits = &sysc_regbits_omap4_simple,
2844 .mod_quirks = SYSC_MODULE_QUIRK_PRUSS,
2845};
2846
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002847static int sysc_init_pdata(struct sysc *ddata)
2848{
2849 struct ti_sysc_platform_data *pdata = dev_get_platdata(ddata->dev);
Tony Lindgrena3e92e72019-03-21 11:00:21 -07002850 struct ti_sysc_module_data *mdata;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002851
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07002852 if (!pdata)
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002853 return 0;
2854
Tony Lindgrena3e92e72019-03-21 11:00:21 -07002855 mdata = devm_kzalloc(ddata->dev, sizeof(*mdata), GFP_KERNEL);
2856 if (!mdata)
2857 return -ENOMEM;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002858
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07002859 if (ddata->legacy_mode) {
2860 mdata->name = ddata->legacy_mode;
2861 mdata->module_pa = ddata->module_pa;
2862 mdata->module_size = ddata->module_size;
2863 mdata->offsets = ddata->offsets;
2864 mdata->nr_offsets = SYSC_MAX_REGS;
2865 mdata->cap = ddata->cap;
2866 mdata->cfg = &ddata->cfg;
2867 }
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002868
Tony Lindgrena3e92e72019-03-21 11:00:21 -07002869 ddata->mdata = mdata;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002870
Tony Lindgrena3e92e72019-03-21 11:00:21 -07002871 return 0;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002872}
2873
Tony Lindgren70a65242017-12-15 09:41:09 -08002874static int sysc_init_match(struct sysc *ddata)
2875{
2876 const struct sysc_capabilities *cap;
2877
2878 cap = of_device_get_match_data(ddata->dev);
2879 if (!cap)
2880 return -EINVAL;
2881
2882 ddata->cap = cap;
2883 if (ddata->cap)
2884 ddata->cfg.quirks |= ddata->cap->mod_quirks;
2885
2886 return 0;
2887}
2888
Tony Lindgren76f0f772018-02-23 08:28:45 -08002889static void ti_sysc_idle(struct work_struct *work)
2890{
2891 struct sysc *ddata;
2892
2893 ddata = container_of(work, struct sysc, idle_work.work);
2894
Tony Lindgrend0989132019-09-05 07:37:22 -07002895 /*
2896 * One time decrement of clock usage counts if left on from init.
2897 * Note that we disable opt clocks unconditionally in this case
2898 * as they are enabled unconditionally during init without
2899 * considering sysc_opt_clks_needed() at that point.
2900 */
2901 if (ddata->cfg.quirks & (SYSC_QUIRK_NO_IDLE |
2902 SYSC_QUIRK_NO_IDLE_ON_INIT)) {
Tony Lindgrend0989132019-09-05 07:37:22 -07002903 sysc_disable_main_clocks(ddata);
2904 sysc_disable_opt_clocks(ddata);
2905 sysc_clkdm_allow_idle(ddata);
2906 }
2907
2908 /* Keep permanent PM runtime usage count for SYSC_QUIRK_NO_IDLE */
2909 if (ddata->cfg.quirks & SYSC_QUIRK_NO_IDLE)
2910 return;
2911
2912 /*
2913 * Decrement PM runtime usage count for SYSC_QUIRK_NO_IDLE_ON_INIT
2914 * and SYSC_QUIRK_NO_RESET_ON_INIT
2915 */
Tony Lindgren76f0f772018-02-23 08:28:45 -08002916 if (pm_runtime_active(ddata->dev))
2917 pm_runtime_put_sync(ddata->dev);
2918}
2919
Tony Lindgrenfeaa8ba2020-02-24 12:58:03 -08002920/*
2921 * SoC model and features detection. Only needed for SoCs that need
2922 * special handling for quirks, no need to list others.
2923 */
2924static const struct soc_device_attribute sysc_soc_match[] = {
2925 SOC_FLAG("OMAP242*", SOC_2420),
2926 SOC_FLAG("OMAP243*", SOC_2430),
2927 SOC_FLAG("OMAP3[45]*", SOC_3430),
2928 SOC_FLAG("OMAP3[67]*", SOC_3630),
2929 SOC_FLAG("OMAP443*", SOC_4430),
2930 SOC_FLAG("OMAP446*", SOC_4460),
2931 SOC_FLAG("OMAP447*", SOC_4470),
2932 SOC_FLAG("OMAP54*", SOC_5430),
2933 SOC_FLAG("AM433", SOC_AM3),
2934 SOC_FLAG("AM43*", SOC_AM4),
2935 SOC_FLAG("DRA7*", SOC_DRA7),
2936
2937 { /* sentinel */ },
2938};
2939
2940/*
2941 * List of SoCs variants with disabled features. By default we assume all
2942 * devices in the device tree are available so no need to list those SoCs.
2943 */
2944static const struct soc_device_attribute sysc_soc_feat_match[] = {
2945 /* OMAP3430/3530 and AM3517 variants with some accelerators disabled */
2946 SOC_FLAG("AM3505", DIS_SGX),
2947 SOC_FLAG("OMAP3525", DIS_SGX),
2948 SOC_FLAG("OMAP3515", DIS_IVA | DIS_SGX),
2949 SOC_FLAG("OMAP3503", DIS_ISP | DIS_IVA | DIS_SGX),
2950
2951 /* OMAP3630/DM3730 variants with some accelerators disabled */
2952 SOC_FLAG("AM3703", DIS_IVA | DIS_SGX),
2953 SOC_FLAG("DM3725", DIS_SGX),
2954 SOC_FLAG("OMAP3611", DIS_ISP | DIS_IVA | DIS_SGX),
2955 SOC_FLAG("OMAP3615/AM3715", DIS_IVA),
2956 SOC_FLAG("OMAP3621", DIS_ISP),
2957
2958 { /* sentinel */ },
2959};
2960
2961static int sysc_add_disabled(unsigned long base)
2962{
2963 struct sysc_address *disabled_module;
2964
2965 disabled_module = kzalloc(sizeof(*disabled_module), GFP_KERNEL);
2966 if (!disabled_module)
2967 return -ENOMEM;
2968
2969 disabled_module->base = base;
2970
2971 mutex_lock(&sysc_soc->list_lock);
2972 list_add(&disabled_module->node, &sysc_soc->disabled_modules);
2973 mutex_unlock(&sysc_soc->list_lock);
2974
2975 return 0;
2976}
2977
2978/*
2979 * One time init to detect the booted SoC and disable unavailable features.
2980 * Note that we initialize static data shared across all ti-sysc instances
2981 * so ddata is only used for SoC type. This can be called from module_init
2982 * once we no longer need to rely on platform data.
2983 */
2984static int sysc_init_soc(struct sysc *ddata)
2985{
2986 const struct soc_device_attribute *match;
2987 struct ti_sysc_platform_data *pdata;
2988 unsigned long features = 0;
Tony Lindgren5f7259a2021-03-08 11:35:09 +02002989 struct device_node *np;
Tony Lindgrenfeaa8ba2020-02-24 12:58:03 -08002990
2991 if (sysc_soc)
2992 return 0;
2993
2994 sysc_soc = kzalloc(sizeof(*sysc_soc), GFP_KERNEL);
2995 if (!sysc_soc)
2996 return -ENOMEM;
2997
2998 mutex_init(&sysc_soc->list_lock);
2999 INIT_LIST_HEAD(&sysc_soc->disabled_modules);
3000 sysc_soc->general_purpose = true;
3001
3002 pdata = dev_get_platdata(ddata->dev);
3003 if (pdata && pdata->soc_type_gp)
3004 sysc_soc->general_purpose = pdata->soc_type_gp();
3005
3006 match = soc_device_match(sysc_soc_match);
3007 if (match && match->data)
3008 sysc_soc->soc = (int)match->data;
3009
Tony Lindgren5f7259a2021-03-08 11:35:09 +02003010 /*
3011 * Check and warn about possible old incomplete dtb. We now want to see
3012 * simple-pm-bus instead of simple-bus in the dtb for genpd using SoCs.
3013 */
3014 switch (sysc_soc->soc) {
3015 case SOC_AM3:
3016 case SOC_AM4:
Tony Lindgren4adcf4c2021-03-12 10:23:48 +02003017 case SOC_4430 ... SOC_4470:
3018 case SOC_5430:
3019 case SOC_DRA7:
Tony Lindgren5f7259a2021-03-08 11:35:09 +02003020 np = of_find_node_by_path("/ocp");
3021 WARN_ONCE(np && of_device_is_compatible(np, "simple-bus"),
3022 "ti-sysc: Incomplete old dtb, please update\n");
3023 break;
3024 default:
3025 break;
3026 }
3027
Tony Lindgren4bba9bf2020-05-07 09:59:31 -07003028 /* Ignore devices that are not available on HS and EMU SoCs */
3029 if (!sysc_soc->general_purpose) {
3030 switch (sysc_soc->soc) {
3031 case SOC_3430 ... SOC_3630:
3032 sysc_add_disabled(0x48304000); /* timer12 */
3033 break;
Kevin Hilmana6d90e92021-07-20 11:27:16 -07003034 case SOC_AM3:
3035 sysc_add_disabled(0x48310000); /* rng */
Tony Lindgren4bba9bf2020-05-07 09:59:31 -07003036 default:
3037 break;
Yang Li52fbb5a2021-02-02 15:00:05 +08003038 }
Tony Lindgren4bba9bf2020-05-07 09:59:31 -07003039 }
3040
Tony Lindgrenfeaa8ba2020-02-24 12:58:03 -08003041 match = soc_device_match(sysc_soc_feat_match);
3042 if (!match)
3043 return 0;
3044
3045 if (match->data)
3046 features = (unsigned long)match->data;
3047
3048 /*
3049 * Add disabled devices to the list based on the module base.
3050 * Note that this must be done before we attempt to access the
3051 * device and have module revision checks working.
3052 */
3053 if (features & DIS_ISP)
3054 sysc_add_disabled(0x480bd400);
3055 if (features & DIS_IVA)
3056 sysc_add_disabled(0x5d000000);
3057 if (features & DIS_SGX)
3058 sysc_add_disabled(0x50000000);
3059
3060 return 0;
3061}
3062
3063static void sysc_cleanup_soc(void)
3064{
3065 struct sysc_address *disabled_module;
3066 struct list_head *pos, *tmp;
3067
3068 if (!sysc_soc)
3069 return;
3070
3071 mutex_lock(&sysc_soc->list_lock);
3072 list_for_each_safe(pos, tmp, &sysc_soc->disabled_modules) {
3073 disabled_module = list_entry(pos, struct sysc_address, node);
3074 list_del(pos);
3075 kfree(disabled_module);
3076 }
3077 mutex_unlock(&sysc_soc->list_lock);
3078}
3079
3080static int sysc_check_disabled_devices(struct sysc *ddata)
3081{
3082 struct sysc_address *disabled_module;
3083 struct list_head *pos;
3084 int error = 0;
3085
3086 mutex_lock(&sysc_soc->list_lock);
3087 list_for_each(pos, &sysc_soc->disabled_modules) {
3088 disabled_module = list_entry(pos, struct sysc_address, node);
3089 if (ddata->module_pa == disabled_module->base) {
3090 dev_dbg(ddata->dev, "module disabled for this SoC\n");
3091 error = -ENODEV;
3092 break;
3093 }
3094 }
3095 mutex_unlock(&sysc_soc->list_lock);
3096
3097 return error;
3098}
3099
Tony Lindgren6cfcd552020-07-13 09:26:01 -07003100/*
3101 * Ignore timers tagged with no-reset and no-idle. These are likely in use,
3102 * for example by drivers/clocksource/timer-ti-dm-systimer.c. If more checks
3103 * are needed, we could also look at the timer register configuration.
3104 */
3105static int sysc_check_active_timer(struct sysc *ddata)
3106{
3107 if (ddata->cap->type != TI_SYSC_OMAP2_TIMER &&
3108 ddata->cap->type != TI_SYSC_OMAP4_TIMER)
3109 return 0;
3110
3111 if ((ddata->cfg.quirks & SYSC_QUIRK_NO_RESET_ON_INIT) &&
3112 (ddata->cfg.quirks & SYSC_QUIRK_NO_IDLE))
Grygorii Strashko65fb7362020-11-18 16:19:00 +02003113 return -ENXIO;
Tony Lindgren6cfcd552020-07-13 09:26:01 -07003114
3115 return 0;
3116}
3117
Tony Lindgrenc4bebea2018-04-16 10:24:54 -07003118static const struct of_device_id sysc_match_table[] = {
3119 { .compatible = "simple-bus", },
3120 { /* sentinel */ },
3121};
3122
Tony Lindgren0eecc632017-10-10 14:23:43 -07003123static int sysc_probe(struct platform_device *pdev)
3124{
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08003125 struct ti_sysc_platform_data *pdata = dev_get_platdata(&pdev->dev);
Tony Lindgren0eecc632017-10-10 14:23:43 -07003126 struct sysc *ddata;
3127 int error;
3128
3129 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
3130 if (!ddata)
3131 return -ENOMEM;
3132
Tony Lindgren29281352020-11-16 12:57:13 +02003133 ddata->offsets[SYSC_REVISION] = -ENODEV;
3134 ddata->offsets[SYSC_SYSCONFIG] = -ENODEV;
3135 ddata->offsets[SYSC_SYSSTATUS] = -ENODEV;
Tony Lindgren0eecc632017-10-10 14:23:43 -07003136 ddata->dev = &pdev->dev;
Tony Lindgren566a9b052017-12-15 09:41:19 -08003137 platform_set_drvdata(pdev, ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -07003138
Tony Lindgrenfeaa8ba2020-02-24 12:58:03 -08003139 error = sysc_init_soc(ddata);
3140 if (error)
3141 return error;
3142
Tony Lindgren70a65242017-12-15 09:41:09 -08003143 error = sysc_init_match(ddata);
3144 if (error)
3145 return error;
3146
Tony Lindgren566a9b052017-12-15 09:41:19 -08003147 error = sysc_init_dts_quirks(ddata);
3148 if (error)
Suman Annaa304f482019-06-25 18:33:15 -05003149 return error;
Tony Lindgren566a9b052017-12-15 09:41:19 -08003150
Tony Lindgren0eecc632017-10-10 14:23:43 -07003151 error = sysc_map_and_check_registers(ddata);
3152 if (error)
Suman Annaa304f482019-06-25 18:33:15 -05003153 return error;
Tony Lindgren0eecc632017-10-10 14:23:43 -07003154
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08003155 error = sysc_init_sysc_mask(ddata);
3156 if (error)
Suman Annaa304f482019-06-25 18:33:15 -05003157 return error;
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08003158
3159 error = sysc_init_idlemodes(ddata);
3160 if (error)
Suman Annaa304f482019-06-25 18:33:15 -05003161 return error;
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08003162
3163 error = sysc_init_syss_mask(ddata);
3164 if (error)
Suman Annaa304f482019-06-25 18:33:15 -05003165 return error;
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08003166
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08003167 error = sysc_init_pdata(ddata);
3168 if (error)
Suman Annaa304f482019-06-25 18:33:15 -05003169 return error;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08003170
Tony Lindgren42b9c5c2019-03-21 11:00:21 -07003171 sysc_init_early_quirks(ddata);
3172
Tony Lindgrenfeaa8ba2020-02-24 12:58:03 -08003173 error = sysc_check_disabled_devices(ddata);
3174 if (error)
3175 return error;
3176
Tony Lindgren6cfcd552020-07-13 09:26:01 -07003177 error = sysc_check_active_timer(ddata);
Tony Lindgren06a089e2021-08-11 08:34:46 +03003178 if (error == -ENXIO)
Tony Lindgren3ff340e2021-06-11 08:42:50 +03003179 ddata->reserved = true;
Tony Lindgren06a089e2021-08-11 08:34:46 +03003180 else if (error)
3181 return error;
Tony Lindgren6cfcd552020-07-13 09:26:01 -07003182
Tony Lindgren42b9c5c2019-03-21 11:00:21 -07003183 error = sysc_get_clocks(ddata);
3184 if (error)
3185 return error;
3186
Tony Lindgren50622362018-04-16 10:20:27 -07003187 error = sysc_init_resets(ddata);
3188 if (error)
Suman Annaa304f482019-06-25 18:33:15 -05003189 goto unprepare;
Tony Lindgren566a9b052017-12-15 09:41:19 -08003190
3191 error = sysc_init_module(ddata);
3192 if (error)
3193 goto unprepare;
3194
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07003195 pm_runtime_enable(ddata->dev);
Zhang Qilongcea08162021-05-31 22:25:42 +08003196 error = pm_runtime_resume_and_get(ddata->dev);
Tony Lindgren0eecc632017-10-10 14:23:43 -07003197 if (error < 0) {
Tony Lindgren0eecc632017-10-10 14:23:43 -07003198 pm_runtime_disable(ddata->dev);
3199 goto unprepare;
3200 }
3201
Tero Kristocdc56c12019-10-07 15:29:31 +03003202 /* Balance use counts as PM runtime should have enabled these all */
Tero Kristocdc56c12019-10-07 15:29:31 +03003203 if (!(ddata->cfg.quirks &
3204 (SYSC_QUIRK_NO_IDLE | SYSC_QUIRK_NO_IDLE_ON_INIT))) {
3205 sysc_disable_main_clocks(ddata);
3206 sysc_disable_opt_clocks(ddata);
3207 sysc_clkdm_allow_idle(ddata);
3208 }
3209
Tony Lindgren4097c9a2020-11-16 12:57:13 +02003210 if (!(ddata->cfg.quirks & SYSC_QUIRK_NO_RESET_ON_INIT))
3211 reset_control_assert(ddata->rsts);
3212
Tony Lindgren0eecc632017-10-10 14:23:43 -07003213 sysc_show_registers(ddata);
3214
Tony Lindgren2c355ff2018-02-22 13:58:03 -08003215 ddata->dev->type = &sysc_device_type;
Tony Lindgren3ff340e2021-06-11 08:42:50 +03003216
3217 if (!ddata->reserved) {
3218 error = of_platform_populate(ddata->dev->of_node,
3219 sysc_match_table,
3220 pdata ? pdata->auxdata : NULL,
3221 ddata->dev);
3222 if (error)
3223 goto err;
3224 }
Tony Lindgren0eecc632017-10-10 14:23:43 -07003225
Tony Lindgren76f0f772018-02-23 08:28:45 -08003226 INIT_DELAYED_WORK(&ddata->idle_work, ti_sysc_idle);
3227
3228 /* At least earlycon won't survive without deferred idle */
Tony Lindgrend0989132019-09-05 07:37:22 -07003229 if (ddata->cfg.quirks & (SYSC_QUIRK_NO_IDLE |
3230 SYSC_QUIRK_NO_IDLE_ON_INIT |
Tony Lindgren76f0f772018-02-23 08:28:45 -08003231 SYSC_QUIRK_NO_RESET_ON_INIT)) {
3232 schedule_delayed_work(&ddata->idle_work, 3000);
3233 } else {
3234 pm_runtime_put(&pdev->dev);
3235 }
Tony Lindgren0eecc632017-10-10 14:23:43 -07003236
3237 return 0;
3238
3239err:
Tony Lindgren0eecc632017-10-10 14:23:43 -07003240 pm_runtime_put_sync(&pdev->dev);
3241 pm_runtime_disable(&pdev->dev);
3242unprepare:
3243 sysc_unprepare(ddata);
3244
3245 return error;
3246}
3247
Tony Lindgren684be5a2017-10-13 10:48:40 -07003248static int sysc_remove(struct platform_device *pdev)
3249{
3250 struct sysc *ddata = platform_get_drvdata(pdev);
3251 int error;
3252
Tony Lindgren76f0f772018-02-23 08:28:45 -08003253 cancel_delayed_work_sync(&ddata->idle_work);
3254
Zhang Qilongcea08162021-05-31 22:25:42 +08003255 error = pm_runtime_resume_and_get(ddata->dev);
Tony Lindgren684be5a2017-10-13 10:48:40 -07003256 if (error < 0) {
Tony Lindgren684be5a2017-10-13 10:48:40 -07003257 pm_runtime_disable(ddata->dev);
3258 goto unprepare;
3259 }
3260
3261 of_platform_depopulate(&pdev->dev);
3262
Tony Lindgren684be5a2017-10-13 10:48:40 -07003263 pm_runtime_put_sync(&pdev->dev);
3264 pm_runtime_disable(&pdev->dev);
Tony Lindgrena7b5d7c2021-02-18 13:06:57 +02003265
3266 if (!reset_control_status(ddata->rsts))
3267 reset_control_assert(ddata->rsts);
Tony Lindgren684be5a2017-10-13 10:48:40 -07003268
3269unprepare:
3270 sysc_unprepare(ddata);
3271
3272 return 0;
3273}
3274
Tony Lindgren0eecc632017-10-10 14:23:43 -07003275static const struct of_device_id sysc_match[] = {
Tony Lindgren70a65242017-12-15 09:41:09 -08003276 { .compatible = "ti,sysc-omap2", .data = &sysc_omap2, },
3277 { .compatible = "ti,sysc-omap2-timer", .data = &sysc_omap2_timer, },
3278 { .compatible = "ti,sysc-omap4", .data = &sysc_omap4, },
3279 { .compatible = "ti,sysc-omap4-timer", .data = &sysc_omap4_timer, },
3280 { .compatible = "ti,sysc-omap4-simple", .data = &sysc_omap4_simple, },
3281 { .compatible = "ti,sysc-omap3430-sr", .data = &sysc_34xx_sr, },
3282 { .compatible = "ti,sysc-omap3630-sr", .data = &sysc_36xx_sr, },
3283 { .compatible = "ti,sysc-omap4-sr", .data = &sysc_omap4_sr, },
3284 { .compatible = "ti,sysc-omap3-sham", .data = &sysc_omap3_sham, },
3285 { .compatible = "ti,sysc-omap-aes", .data = &sysc_omap3_aes, },
3286 { .compatible = "ti,sysc-mcasp", .data = &sysc_omap4_mcasp, },
Tony Lindgren2c63a832018-11-15 14:46:53 -08003287 { .compatible = "ti,sysc-dra7-mcasp", .data = &sysc_dra7_mcasp, },
Tony Lindgren70a65242017-12-15 09:41:09 -08003288 { .compatible = "ti,sysc-usb-host-fs",
3289 .data = &sysc_omap4_usb_host_fs, },
Faiz Abbas7f35e632018-07-09 22:18:38 +05303290 { .compatible = "ti,sysc-dra7-mcan", .data = &sysc_dra7_mcan, },
Suman Annab2745d92020-02-27 16:28:34 -06003291 { .compatible = "ti,sysc-pruss", .data = &sysc_pruss, },
Tony Lindgren0eecc632017-10-10 14:23:43 -07003292 { },
3293};
3294MODULE_DEVICE_TABLE(of, sysc_match);
3295
3296static struct platform_driver sysc_driver = {
3297 .probe = sysc_probe,
Tony Lindgren684be5a2017-10-13 10:48:40 -07003298 .remove = sysc_remove,
Tony Lindgren0eecc632017-10-10 14:23:43 -07003299 .driver = {
3300 .name = "ti-sysc",
3301 .of_match_table = sysc_match,
3302 .pm = &sysc_pm_ops,
3303 },
3304};
Tony Lindgren2c355ff2018-02-22 13:58:03 -08003305
3306static int __init sysc_init(void)
3307{
3308 bus_register_notifier(&platform_bus_type, &sysc_nb);
3309
3310 return platform_driver_register(&sysc_driver);
3311}
3312module_init(sysc_init);
3313
3314static void __exit sysc_exit(void)
3315{
3316 bus_unregister_notifier(&platform_bus_type, &sysc_nb);
3317 platform_driver_unregister(&sysc_driver);
Tony Lindgrenfeaa8ba2020-02-24 12:58:03 -08003318 sysc_cleanup_soc();
Tony Lindgren2c355ff2018-02-22 13:58:03 -08003319}
3320module_exit(sysc_exit);
Tony Lindgren0eecc632017-10-10 14:23:43 -07003321
3322MODULE_DESCRIPTION("TI sysc interconnect target driver");
3323MODULE_LICENSE("GPL v2");