blob: a3a2c269e9ad74ed537b7a5a7c0a7ea2dd4d5842 [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>
Faiz Abbas596e7952018-07-09 22:18:39 +053020#include <linux/iopoll.h>
Tony Lindgren2c355ff2018-02-22 13:58:03 -080021
Tony Lindgren70a65242017-12-15 09:41:09 -080022#include <linux/platform_data/ti-sysc.h>
23
24#include <dt-bindings/bus/ti-sysc.h>
Tony Lindgren0eecc632017-10-10 14:23:43 -070025
Tony Lindgrenfeaa8ba2020-02-24 12:58:03 -080026#define DIS_ISP BIT(2)
27#define DIS_IVA BIT(1)
28#define DIS_SGX BIT(0)
29
30#define SOC_FLAG(match, flag) { .machine = match, .data = (void *)(flag), }
31
Faiz Abbas596e7952018-07-09 22:18:39 +053032#define MAX_MODULE_SOFTRESET_WAIT 10000
33
Tony Lindgrenfeaa8ba2020-02-24 12:58:03 -080034enum sysc_soc {
35 SOC_UNKNOWN,
36 SOC_2420,
37 SOC_2430,
38 SOC_3430,
39 SOC_3630,
40 SOC_4430,
41 SOC_4460,
42 SOC_4470,
43 SOC_5430,
44 SOC_AM3,
45 SOC_AM4,
46 SOC_DRA7,
47};
48
49struct sysc_address {
50 unsigned long base;
51 struct list_head node;
52};
53
54struct sysc_soc_info {
55 unsigned long general_purpose:1;
56 enum sysc_soc soc;
57 struct mutex list_lock; /* disabled modules list lock */
58 struct list_head disabled_modules;
59};
Tony Lindgren0eecc632017-10-10 14:23:43 -070060
61enum sysc_clocks {
62 SYSC_FCK,
63 SYSC_ICK,
Tony Lindgren09dfe582018-04-16 10:25:52 -070064 SYSC_OPTFCK0,
65 SYSC_OPTFCK1,
66 SYSC_OPTFCK2,
67 SYSC_OPTFCK3,
68 SYSC_OPTFCK4,
69 SYSC_OPTFCK5,
70 SYSC_OPTFCK6,
71 SYSC_OPTFCK7,
Tony Lindgren0eecc632017-10-10 14:23:43 -070072 SYSC_MAX_CLOCKS,
73};
74
Tony Lindgrenfeaa8ba2020-02-24 12:58:03 -080075static struct sysc_soc_info *sysc_soc;
76static const char * const reg_names[] = { "rev", "sysc", "syss", };
Tony Lindgrena54275f2019-03-21 11:00:21 -070077static const char * const clock_names[SYSC_MAX_CLOCKS] = {
78 "fck", "ick", "opt0", "opt1", "opt2", "opt3", "opt4",
79 "opt5", "opt6", "opt7",
80};
Tony Lindgren0eecc632017-10-10 14:23:43 -070081
Tony Lindgrenc5a2de92017-12-15 09:41:23 -080082#define SYSC_IDLEMODE_MASK 3
83#define SYSC_CLOCKACTIVITY_MASK 3
84
Tony Lindgren0eecc632017-10-10 14:23:43 -070085/**
86 * struct sysc - TI sysc interconnect target module registers and capabilities
87 * @dev: struct device pointer
88 * @module_pa: physical address of the interconnect target module
89 * @module_size: size of the interconnect target module
90 * @module_va: virtual address of the interconnect target module
91 * @offsets: register offsets from module base
Suman Annab58056d2019-08-13 04:13:32 -070092 * @mdata: ti-sysc to hwmod translation data for a module
Tony Lindgren0eecc632017-10-10 14:23:43 -070093 * @clocks: clocks used by the interconnect target module
Tony Lindgren09dfe582018-04-16 10:25:52 -070094 * @clock_roles: clock role names for the found clocks
95 * @nr_clocks: number of clocks used by the interconnect target module
Suman Annab58056d2019-08-13 04:13:32 -070096 * @rsts: resets used by the interconnect target module
Tony Lindgren0eecc632017-10-10 14:23:43 -070097 * @legacy_mode: configured for legacy mode if set
Tony Lindgren70a65242017-12-15 09:41:09 -080098 * @cap: interconnect target module capabilities
99 * @cfg: interconnect target module configuration
Suman Annab58056d2019-08-13 04:13:32 -0700100 * @cookie: data used by legacy platform callbacks
Tony Lindgren566a9b052017-12-15 09:41:19 -0800101 * @name: name if available
102 * @revision: interconnect target module revision
Suman Annab58056d2019-08-13 04:13:32 -0700103 * @enabled: sysc runtime enabled status
Tony Lindgren62020f22018-02-22 13:59:44 -0800104 * @needs_resume: runtime resume needed on resume from suspend
Suman Annab58056d2019-08-13 04:13:32 -0700105 * @child_needs_resume: runtime resume needed for child on resume from suspend
106 * @disable_on_idle: status flag used for disabling modules with resets
107 * @idle_work: work structure used to perform delayed idle on a module
Tony Lindgrene64c0212020-02-24 12:58:03 -0800108 * @pre_reset_quirk: module specific pre-reset quirk
109 * @post_reset_quirk: module specific post-reset quirk
Tony Lindgren4e23be42019-06-10 04:48:05 -0700110 * @reset_done_quirk: module specific reset done quirk
Tony Lindgrend7f563d2019-08-14 05:18:16 -0700111 * @module_enable_quirk: module specific enable quirk
Tony Lindgrenc7d86692019-10-17 11:21:44 -0700112 * @module_disable_quirk: module specific disable quirk
Tony Lindgrene8639e12020-02-24 12:58:03 -0800113 * @module_unlock_quirk: module specific sysconfig unlock quirk
114 * @module_lock_quirk: module specific sysconfig lock quirk
Tony Lindgren0eecc632017-10-10 14:23:43 -0700115 */
116struct sysc {
117 struct device *dev;
118 u64 module_pa;
119 u32 module_size;
120 void __iomem *module_va;
121 int offsets[SYSC_MAX_REGS];
Tony Lindgrena3e92e72019-03-21 11:00:21 -0700122 struct ti_sysc_module_data *mdata;
Tony Lindgren09dfe582018-04-16 10:25:52 -0700123 struct clk **clocks;
124 const char **clock_roles;
125 int nr_clocks;
Tony Lindgren50622362018-04-16 10:20:27 -0700126 struct reset_control *rsts;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700127 const char *legacy_mode;
Tony Lindgren70a65242017-12-15 09:41:09 -0800128 const struct sysc_capabilities *cap;
129 struct sysc_config cfg;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800130 struct ti_sysc_cookie cookie;
Tony Lindgren566a9b052017-12-15 09:41:19 -0800131 const char *name;
132 u32 revision;
Tony Lindgren8383e252019-05-27 04:51:56 -0700133 unsigned int enabled:1;
134 unsigned int needs_resume:1;
135 unsigned int child_needs_resume:1;
Tony Lindgren76f0f772018-02-23 08:28:45 -0800136 struct delayed_work idle_work;
Tony Lindgrene64c0212020-02-24 12:58:03 -0800137 void (*pre_reset_quirk)(struct sysc *sysc);
138 void (*post_reset_quirk)(struct sysc *sysc);
Tony Lindgren4e23be42019-06-10 04:48:05 -0700139 void (*reset_done_quirk)(struct sysc *sysc);
Tony Lindgrend7f563d2019-08-14 05:18:16 -0700140 void (*module_enable_quirk)(struct sysc *sysc);
Tony Lindgrenc7d86692019-10-17 11:21:44 -0700141 void (*module_disable_quirk)(struct sysc *sysc);
Tony Lindgrene8639e12020-02-24 12:58:03 -0800142 void (*module_unlock_quirk)(struct sysc *sysc);
143 void (*module_lock_quirk)(struct sysc *sysc);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700144};
145
Tony Lindgren4014c082018-12-10 14:11:26 -0800146static void sysc_parse_dts_quirks(struct sysc *ddata, struct device_node *np,
147 bool is_child);
148
Tony Lindgrenb7182b42019-03-21 11:00:21 -0700149static void sysc_write(struct sysc *ddata, int offset, u32 value)
Faiz Abbas596e7952018-07-09 22:18:39 +0530150{
Tony Lindgren5aa91292019-05-27 04:51:53 -0700151 if (ddata->cfg.quirks & SYSC_QUIRK_16BIT) {
152 writew_relaxed(value & 0xffff, ddata->module_va + offset);
153
154 /* Only i2c revision has LO and HI register with stride of 4 */
155 if (ddata->offsets[SYSC_REVISION] >= 0 &&
156 offset == ddata->offsets[SYSC_REVISION]) {
157 u16 hi = value >> 16;
158
159 writew_relaxed(hi, ddata->module_va + offset + 4);
160 }
161
162 return;
163 }
164
Faiz Abbas596e7952018-07-09 22:18:39 +0530165 writel_relaxed(value, ddata->module_va + offset);
166}
167
Tony Lindgren566a9b052017-12-15 09:41:19 -0800168static u32 sysc_read(struct sysc *ddata, int offset)
169{
170 if (ddata->cfg.quirks & SYSC_QUIRK_16BIT) {
171 u32 val;
172
173 val = readw_relaxed(ddata->module_va + offset);
Tony Lindgren5aa91292019-05-27 04:51:53 -0700174
175 /* Only i2c revision has LO and HI register with stride of 4 */
176 if (ddata->offsets[SYSC_REVISION] >= 0 &&
177 offset == ddata->offsets[SYSC_REVISION]) {
178 u16 tmp = readw_relaxed(ddata->module_va + offset + 4);
179
180 val |= tmp << 16;
181 }
Tony Lindgren566a9b052017-12-15 09:41:19 -0800182
183 return val;
184 }
185
186 return readl_relaxed(ddata->module_va + offset);
187}
188
Tony Lindgren09dfe582018-04-16 10:25:52 -0700189static bool sysc_opt_clks_needed(struct sysc *ddata)
190{
191 return !!(ddata->cfg.quirks & SYSC_QUIRK_OPT_CLKS_NEEDED);
192}
193
Tony Lindgren0eecc632017-10-10 14:23:43 -0700194static u32 sysc_read_revision(struct sysc *ddata)
195{
Tony Lindgren566a9b052017-12-15 09:41:19 -0800196 int offset = ddata->offsets[SYSC_REVISION];
197
198 if (offset < 0)
199 return 0;
200
201 return sysc_read(ddata, offset);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700202}
203
Tony Lindgrene0db94f2019-05-27 04:51:53 -0700204static u32 sysc_read_sysconfig(struct sysc *ddata)
205{
206 int offset = ddata->offsets[SYSC_SYSCONFIG];
207
208 if (offset < 0)
209 return 0;
210
211 return sysc_read(ddata, offset);
212}
213
214static u32 sysc_read_sysstatus(struct sysc *ddata)
215{
216 int offset = ddata->offsets[SYSC_SYSSTATUS];
217
218 if (offset < 0)
219 return 0;
220
221 return sysc_read(ddata, offset);
222}
223
Tony Lindgrend46f9fb2020-05-31 12:37:54 -0700224/* Poll on reset status */
225static int sysc_wait_softreset(struct sysc *ddata)
226{
227 u32 sysc_mask, syss_done, rstval;
228 int syss_offset, error = 0;
229
230 syss_offset = ddata->offsets[SYSC_SYSSTATUS];
231 sysc_mask = BIT(ddata->cap->regbits->srst_shift);
232
233 if (ddata->cfg.quirks & SYSS_QUIRK_RESETDONE_INVERTED)
234 syss_done = 0;
235 else
236 syss_done = ddata->cfg.syss_mask;
237
238 if (syss_offset >= 0) {
239 error = readx_poll_timeout(sysc_read_sysstatus, ddata, rstval,
240 (rstval & ddata->cfg.syss_mask) ==
241 syss_done,
242 100, MAX_MODULE_SOFTRESET_WAIT);
243
244 } else if (ddata->cfg.quirks & SYSC_QUIRK_RESET_STATUS) {
245 error = readx_poll_timeout(sysc_read_sysconfig, ddata, rstval,
246 !(rstval & sysc_mask),
247 100, MAX_MODULE_SOFTRESET_WAIT);
248 }
249
250 return error;
251}
252
Tony Lindgrena54275f2019-03-21 11:00:21 -0700253static int sysc_add_named_clock_from_child(struct sysc *ddata,
254 const char *name,
255 const char *optfck_name)
256{
257 struct device_node *np = ddata->dev->of_node;
258 struct device_node *child;
259 struct clk_lookup *cl;
260 struct clk *clock;
261 const char *n;
262
263 if (name)
264 n = name;
265 else
266 n = optfck_name;
267
268 /* Does the clock alias already exist? */
269 clock = of_clk_get_by_name(np, n);
270 if (!IS_ERR(clock)) {
271 clk_put(clock);
272
273 return 0;
274 }
275
276 child = of_get_next_available_child(np, NULL);
277 if (!child)
278 return -ENODEV;
279
280 clock = devm_get_clk_from_child(ddata->dev, child, name);
281 if (IS_ERR(clock))
282 return PTR_ERR(clock);
283
284 /*
285 * Use clkdev_add() instead of clkdev_alloc() to avoid the MAX_DEV_ID
286 * limit for clk_get(). If cl ever needs to be freed, it should be done
287 * with clkdev_drop().
288 */
289 cl = kcalloc(1, sizeof(*cl), GFP_KERNEL);
290 if (!cl)
291 return -ENOMEM;
292
293 cl->con_id = n;
294 cl->dev_id = dev_name(ddata->dev);
295 cl->clk = clock;
296 clkdev_add(cl);
297
298 clk_put(clock);
299
300 return 0;
301}
302
303static int sysc_init_ext_opt_clock(struct sysc *ddata, const char *name)
304{
305 const char *optfck_name;
306 int error, index;
307
308 if (ddata->nr_clocks < SYSC_OPTFCK0)
309 index = SYSC_OPTFCK0;
310 else
311 index = ddata->nr_clocks;
312
313 if (name)
314 optfck_name = name;
315 else
316 optfck_name = clock_names[index];
317
318 error = sysc_add_named_clock_from_child(ddata, name, optfck_name);
319 if (error)
320 return error;
321
322 ddata->clock_roles[index] = optfck_name;
323 ddata->nr_clocks++;
324
325 return 0;
326}
327
Tony Lindgren09dfe582018-04-16 10:25:52 -0700328static int sysc_get_one_clock(struct sysc *ddata, const char *name)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700329{
Tony Lindgren09dfe582018-04-16 10:25:52 -0700330 int error, i, index = -ENODEV;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700331
Tony Lindgren09dfe582018-04-16 10:25:52 -0700332 if (!strncmp(clock_names[SYSC_FCK], name, 3))
333 index = SYSC_FCK;
334 else if (!strncmp(clock_names[SYSC_ICK], name, 3))
335 index = SYSC_ICK;
336
337 if (index < 0) {
338 for (i = SYSC_OPTFCK0; i < SYSC_MAX_CLOCKS; i++) {
Tony Lindgrenc97c8622018-05-18 07:54:44 -0700339 if (!ddata->clocks[i]) {
Tony Lindgren09dfe582018-04-16 10:25:52 -0700340 index = i;
341 break;
342 }
343 }
Tony Lindgren0eecc632017-10-10 14:23:43 -0700344 }
Tony Lindgren09dfe582018-04-16 10:25:52 -0700345
346 if (index < 0) {
347 dev_err(ddata->dev, "clock %s not added\n", name);
348 return index;
349 }
Tony Lindgren0eecc632017-10-10 14:23:43 -0700350
351 ddata->clocks[index] = devm_clk_get(ddata->dev, name);
352 if (IS_ERR(ddata->clocks[index])) {
Tony Lindgren0eecc632017-10-10 14:23:43 -0700353 dev_err(ddata->dev, "clock get error for %s: %li\n",
354 name, PTR_ERR(ddata->clocks[index]));
355
356 return PTR_ERR(ddata->clocks[index]);
357 }
358
359 error = clk_prepare(ddata->clocks[index]);
360 if (error) {
361 dev_err(ddata->dev, "clock prepare error for %s: %i\n",
362 name, error);
363
364 return error;
365 }
366
367 return 0;
368}
369
370static int sysc_get_clocks(struct sysc *ddata)
371{
Tony Lindgren09dfe582018-04-16 10:25:52 -0700372 struct device_node *np = ddata->dev->of_node;
373 struct property *prop;
374 const char *name;
375 int nr_fck = 0, nr_ick = 0, i, error = 0;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700376
Kees Cook20749052018-06-18 10:45:49 -0700377 ddata->clock_roles = devm_kcalloc(ddata->dev,
Tony Lindgren09dfe582018-04-16 10:25:52 -0700378 SYSC_MAX_CLOCKS,
Kees Cook20749052018-06-18 10:45:49 -0700379 sizeof(*ddata->clock_roles),
Tony Lindgren09dfe582018-04-16 10:25:52 -0700380 GFP_KERNEL);
381 if (!ddata->clock_roles)
382 return -ENOMEM;
383
384 of_property_for_each_string(np, "clock-names", prop, name) {
385 if (!strncmp(clock_names[SYSC_FCK], name, 3))
386 nr_fck++;
387 if (!strncmp(clock_names[SYSC_ICK], name, 3))
388 nr_ick++;
389 ddata->clock_roles[ddata->nr_clocks] = name;
390 ddata->nr_clocks++;
391 }
392
393 if (ddata->nr_clocks < 1)
394 return 0;
395
Tony Lindgrena54275f2019-03-21 11:00:21 -0700396 if ((ddata->cfg.quirks & SYSC_QUIRK_EXT_OPT_CLOCK)) {
397 error = sysc_init_ext_opt_clock(ddata, NULL);
398 if (error)
399 return error;
400 }
401
Tony Lindgren09dfe582018-04-16 10:25:52 -0700402 if (ddata->nr_clocks > SYSC_MAX_CLOCKS) {
403 dev_err(ddata->dev, "too many clocks for %pOF\n", np);
404
405 return -EINVAL;
406 }
407
408 if (nr_fck > 1 || nr_ick > 1) {
409 dev_err(ddata->dev, "max one fck and ick for %pOF\n", np);
410
411 return -EINVAL;
412 }
413
Tony Lindgren2c81f0f2019-12-16 14:41:53 -0800414 /* Always add a slot for main clocks fck and ick even if unused */
415 if (!nr_fck)
416 ddata->nr_clocks++;
417 if (!nr_ick)
418 ddata->nr_clocks++;
419
Kees Cook20749052018-06-18 10:45:49 -0700420 ddata->clocks = devm_kcalloc(ddata->dev,
421 ddata->nr_clocks, sizeof(*ddata->clocks),
Tony Lindgren09dfe582018-04-16 10:25:52 -0700422 GFP_KERNEL);
423 if (!ddata->clocks)
424 return -ENOMEM;
425
Tony Lindgren7b4f8ac2018-11-15 14:46:52 -0800426 for (i = 0; i < SYSC_MAX_CLOCKS; i++) {
427 const char *name = ddata->clock_roles[i];
428
429 if (!name)
430 continue;
431
432 error = sysc_get_one_clock(ddata, name);
Tony Lindgren2783d062019-09-05 13:01:29 -0700433 if (error)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700434 return error;
435 }
436
437 return 0;
438}
439
Tony Lindgrend8789702019-03-21 11:00:21 -0700440static int sysc_enable_main_clocks(struct sysc *ddata)
441{
442 struct clk *clock;
443 int i, error;
444
445 if (!ddata->clocks)
446 return 0;
447
448 for (i = 0; i < SYSC_OPTFCK0; i++) {
449 clock = ddata->clocks[i];
450
451 /* Main clocks may not have ick */
452 if (IS_ERR_OR_NULL(clock))
453 continue;
454
455 error = clk_enable(clock);
456 if (error)
457 goto err_disable;
458 }
459
460 return 0;
461
462err_disable:
463 for (i--; i >= 0; i--) {
464 clock = ddata->clocks[i];
465
466 /* Main clocks may not have ick */
467 if (IS_ERR_OR_NULL(clock))
468 continue;
469
470 clk_disable(clock);
471 }
472
473 return error;
474}
475
476static void sysc_disable_main_clocks(struct sysc *ddata)
477{
478 struct clk *clock;
479 int i;
480
481 if (!ddata->clocks)
482 return;
483
484 for (i = 0; i < SYSC_OPTFCK0; i++) {
485 clock = ddata->clocks[i];
486 if (IS_ERR_OR_NULL(clock))
487 continue;
488
489 clk_disable(clock);
490 }
491}
492
493static int sysc_enable_opt_clocks(struct sysc *ddata)
494{
495 struct clk *clock;
496 int i, error;
497
Tony Lindgren2c81f0f2019-12-16 14:41:53 -0800498 if (!ddata->clocks || ddata->nr_clocks < SYSC_OPTFCK0 + 1)
Tony Lindgrend8789702019-03-21 11:00:21 -0700499 return 0;
500
501 for (i = SYSC_OPTFCK0; i < SYSC_MAX_CLOCKS; i++) {
502 clock = ddata->clocks[i];
503
504 /* Assume no holes for opt clocks */
505 if (IS_ERR_OR_NULL(clock))
506 return 0;
507
508 error = clk_enable(clock);
509 if (error)
510 goto err_disable;
511 }
512
513 return 0;
514
515err_disable:
516 for (i--; i >= 0; i--) {
517 clock = ddata->clocks[i];
518 if (IS_ERR_OR_NULL(clock))
519 continue;
520
521 clk_disable(clock);
522 }
523
524 return error;
525}
526
527static void sysc_disable_opt_clocks(struct sysc *ddata)
528{
529 struct clk *clock;
530 int i;
531
Tony Lindgren2c81f0f2019-12-16 14:41:53 -0800532 if (!ddata->clocks || ddata->nr_clocks < SYSC_OPTFCK0 + 1)
Tony Lindgrend8789702019-03-21 11:00:21 -0700533 return;
534
535 for (i = SYSC_OPTFCK0; i < SYSC_MAX_CLOCKS; i++) {
536 clock = ddata->clocks[i];
537
538 /* Assume no holes for opt clocks */
539 if (IS_ERR_OR_NULL(clock))
540 return;
541
542 clk_disable(clock);
543 }
544}
545
Tony Lindgren2b2f7de2019-05-27 04:51:53 -0700546static void sysc_clkdm_deny_idle(struct sysc *ddata)
547{
548 struct ti_sysc_platform_data *pdata;
549
Tony Lindgren94f63452019-12-12 08:57:58 -0800550 if (ddata->legacy_mode || (ddata->cfg.quirks & SYSC_QUIRK_CLKDM_NOAUTO))
Tony Lindgren2b2f7de2019-05-27 04:51:53 -0700551 return;
552
553 pdata = dev_get_platdata(ddata->dev);
554 if (pdata && pdata->clkdm_deny_idle)
555 pdata->clkdm_deny_idle(ddata->dev, &ddata->cookie);
556}
557
558static void sysc_clkdm_allow_idle(struct sysc *ddata)
559{
560 struct ti_sysc_platform_data *pdata;
561
Tony Lindgren94f63452019-12-12 08:57:58 -0800562 if (ddata->legacy_mode || (ddata->cfg.quirks & SYSC_QUIRK_CLKDM_NOAUTO))
Tony Lindgren2b2f7de2019-05-27 04:51:53 -0700563 return;
564
565 pdata = dev_get_platdata(ddata->dev);
566 if (pdata && pdata->clkdm_allow_idle)
567 pdata->clkdm_allow_idle(ddata->dev, &ddata->cookie);
568}
569
Tony Lindgren0eecc632017-10-10 14:23:43 -0700570/**
Tony Lindgrenb11c1ea2019-03-21 11:00:21 -0700571 * sysc_init_resets - init rstctrl reset line if configured
Tony Lindgren50622362018-04-16 10:20:27 -0700572 * @ddata: device driver data
573 *
Tony Lindgrenb11c1ea2019-03-21 11:00:21 -0700574 * See sysc_rstctrl_reset_deassert().
Tony Lindgren50622362018-04-16 10:20:27 -0700575 */
576static int sysc_init_resets(struct sysc *ddata)
577{
Tony Lindgren50622362018-04-16 10:20:27 -0700578 ddata->rsts =
Tero Kristobb88b862019-08-07 15:46:05 +0300579 devm_reset_control_get_optional_shared(ddata->dev, "rstctrl");
Tony Lindgren50622362018-04-16 10:20:27 -0700580
zhengbin3f2c4202019-11-18 18:37:32 +0800581 return PTR_ERR_OR_ZERO(ddata->rsts);
Tony Lindgren50622362018-04-16 10:20:27 -0700582}
583
584/**
Tony Lindgren0eecc632017-10-10 14:23:43 -0700585 * sysc_parse_and_check_child_range - parses module IO region from ranges
586 * @ddata: device driver data
587 *
588 * In general we only need rev, syss, and sysc registers and not the whole
589 * module range. But we do want the offsets for these registers from the
590 * module base. This allows us to check them against the legacy hwmod
591 * platform data. Let's also check the ranges are configured properly.
592 */
593static int sysc_parse_and_check_child_range(struct sysc *ddata)
594{
595 struct device_node *np = ddata->dev->of_node;
596 const __be32 *ranges;
597 u32 nr_addr, nr_size;
598 int len, error;
599
600 ranges = of_get_property(np, "ranges", &len);
601 if (!ranges) {
602 dev_err(ddata->dev, "missing ranges for %pOF\n", np);
603
604 return -ENOENT;
605 }
606
607 len /= sizeof(*ranges);
608
609 if (len < 3) {
610 dev_err(ddata->dev, "incomplete ranges for %pOF\n", np);
611
612 return -EINVAL;
613 }
614
615 error = of_property_read_u32(np, "#address-cells", &nr_addr);
616 if (error)
617 return -ENOENT;
618
619 error = of_property_read_u32(np, "#size-cells", &nr_size);
620 if (error)
621 return -ENOENT;
622
623 if (nr_addr != 1 || nr_size != 1) {
624 dev_err(ddata->dev, "invalid ranges for %pOF\n", np);
625
626 return -EINVAL;
627 }
628
629 ranges++;
630 ddata->module_pa = of_translate_address(np, ranges++);
631 ddata->module_size = be32_to_cpup(ranges);
632
Tony Lindgren0eecc632017-10-10 14:23:43 -0700633 return 0;
634}
635
Tony Lindgren3bb37c82018-02-22 14:05:14 -0800636static struct device_node *stdout_path;
637
638static void sysc_init_stdout_path(struct sysc *ddata)
639{
640 struct device_node *np = NULL;
641 const char *uart;
642
643 if (IS_ERR(stdout_path))
644 return;
645
646 if (stdout_path)
647 return;
648
649 np = of_find_node_by_path("/chosen");
650 if (!np)
651 goto err;
652
653 uart = of_get_property(np, "stdout-path", NULL);
654 if (!uart)
655 goto err;
656
657 np = of_find_node_by_path(uart);
658 if (!np)
659 goto err;
660
661 stdout_path = np;
662
663 return;
664
665err:
666 stdout_path = ERR_PTR(-ENODEV);
667}
668
669static void sysc_check_quirk_stdout(struct sysc *ddata,
670 struct device_node *np)
671{
672 sysc_init_stdout_path(ddata);
673 if (np != stdout_path)
674 return;
675
676 ddata->cfg.quirks |= SYSC_QUIRK_NO_IDLE_ON_INIT |
677 SYSC_QUIRK_NO_RESET_ON_INIT;
678}
679
Tony Lindgren0eecc632017-10-10 14:23:43 -0700680/**
681 * sysc_check_one_child - check child configuration
682 * @ddata: device driver data
683 * @np: child device node
684 *
685 * Let's avoid messy situations where we have new interconnect target
686 * node but children have "ti,hwmods". These belong to the interconnect
687 * target node and are managed by this driver.
688 */
Nishka Dasguptac6e78d72019-08-15 11:16:47 +0530689static void sysc_check_one_child(struct sysc *ddata,
690 struct device_node *np)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700691{
692 const char *name;
693
694 name = of_get_property(np, "ti,hwmods", NULL);
Tony Lindgren7320fd32020-02-24 12:58:03 -0800695 if (name && !of_device_is_compatible(np, "ti,sysc"))
Tony Lindgren0eecc632017-10-10 14:23:43 -0700696 dev_warn(ddata->dev, "really a child ti,hwmods property?");
697
Tony Lindgren3bb37c82018-02-22 14:05:14 -0800698 sysc_check_quirk_stdout(ddata, np);
Tony Lindgren4014c082018-12-10 14:11:26 -0800699 sysc_parse_dts_quirks(ddata, np, true);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700700}
701
Nishka Dasguptac6e78d72019-08-15 11:16:47 +0530702static void sysc_check_children(struct sysc *ddata)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700703{
704 struct device_node *child;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700705
Nishka Dasguptac6e78d72019-08-15 11:16:47 +0530706 for_each_child_of_node(ddata->dev->of_node, child)
707 sysc_check_one_child(ddata, child);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700708}
709
Tony Lindgrena7199e22017-12-15 09:41:14 -0800710/*
711 * So far only I2C uses 16-bit read access with clockactivity with revision
712 * in two registers with stride of 4. We can detect this based on the rev
713 * register size to configure things far enough to be able to properly read
714 * the revision register.
715 */
716static void sysc_check_quirk_16bit(struct sysc *ddata, struct resource *res)
717{
Tony Lindgrendd57ac12018-02-22 14:09:57 -0800718 if (resource_size(res) == 8)
Tony Lindgrena7199e22017-12-15 09:41:14 -0800719 ddata->cfg.quirks |= SYSC_QUIRK_16BIT | SYSC_QUIRK_USE_CLOCKACT;
Tony Lindgrena7199e22017-12-15 09:41:14 -0800720}
721
Tony Lindgren0eecc632017-10-10 14:23:43 -0700722/**
723 * sysc_parse_one - parses the interconnect target module registers
724 * @ddata: device driver data
725 * @reg: register to parse
726 */
727static int sysc_parse_one(struct sysc *ddata, enum sysc_registers reg)
728{
729 struct resource *res;
730 const char *name;
731
732 switch (reg) {
733 case SYSC_REVISION:
734 case SYSC_SYSCONFIG:
735 case SYSC_SYSSTATUS:
736 name = reg_names[reg];
737 break;
738 default:
739 return -EINVAL;
740 }
741
742 res = platform_get_resource_byname(to_platform_device(ddata->dev),
743 IORESOURCE_MEM, name);
744 if (!res) {
Tony Lindgren0eecc632017-10-10 14:23:43 -0700745 ddata->offsets[reg] = -ENODEV;
746
747 return 0;
748 }
749
750 ddata->offsets[reg] = res->start - ddata->module_pa;
Tony Lindgrena7199e22017-12-15 09:41:14 -0800751 if (reg == SYSC_REVISION)
752 sysc_check_quirk_16bit(ddata, res);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700753
754 return 0;
755}
756
757static int sysc_parse_registers(struct sysc *ddata)
758{
759 int i, error;
760
761 for (i = 0; i < SYSC_MAX_REGS; i++) {
762 error = sysc_parse_one(ddata, i);
763 if (error)
764 return error;
765 }
766
767 return 0;
768}
769
770/**
771 * sysc_check_registers - check for misconfigured register overlaps
772 * @ddata: device driver data
773 */
774static int sysc_check_registers(struct sysc *ddata)
775{
776 int i, j, nr_regs = 0, nr_matches = 0;
777
778 for (i = 0; i < SYSC_MAX_REGS; i++) {
779 if (ddata->offsets[i] < 0)
780 continue;
781
782 if (ddata->offsets[i] > (ddata->module_size - 4)) {
783 dev_err(ddata->dev, "register outside module range");
784
785 return -EINVAL;
786 }
787
788 for (j = 0; j < SYSC_MAX_REGS; j++) {
789 if (ddata->offsets[j] < 0)
790 continue;
791
792 if (ddata->offsets[i] == ddata->offsets[j])
793 nr_matches++;
794 }
795 nr_regs++;
796 }
797
Tony Lindgren0eecc632017-10-10 14:23:43 -0700798 if (nr_matches > nr_regs) {
799 dev_err(ddata->dev, "overlapping registers: (%i/%i)",
800 nr_regs, nr_matches);
801
802 return -EINVAL;
803 }
804
805 return 0;
806}
807
808/**
809 * syc_ioremap - ioremap register space for the interconnect target module
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700810 * @ddata: device driver data
Tony Lindgren0eecc632017-10-10 14:23:43 -0700811 *
812 * Note that the interconnect target module registers can be anywhere
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700813 * within the interconnect target module range. For example, SGX has
814 * them at offset 0x1fc00 in the 32MB module address space. And cpsw
815 * has them at offset 0x1200 in the CPSW_WR child. Usually the
816 * the interconnect target module registers are at the beginning of
817 * the module range though.
Tony Lindgren0eecc632017-10-10 14:23:43 -0700818 */
819static int sysc_ioremap(struct sysc *ddata)
820{
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700821 int size;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700822
Tony Lindgrene4f50c82019-05-01 14:24:57 -0700823 if (ddata->offsets[SYSC_REVISION] < 0 &&
824 ddata->offsets[SYSC_SYSCONFIG] < 0 &&
825 ddata->offsets[SYSC_SYSSTATUS] < 0) {
826 size = ddata->module_size;
827 } else {
828 size = max3(ddata->offsets[SYSC_REVISION],
829 ddata->offsets[SYSC_SYSCONFIG],
830 ddata->offsets[SYSC_SYSSTATUS]);
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700831
Tony Lindgren4e23be42019-06-10 04:48:05 -0700832 if (size < SZ_1K)
833 size = SZ_1K;
834
Tony Lindgrene4f50c82019-05-01 14:24:57 -0700835 if ((size + sizeof(u32)) > ddata->module_size)
Tony Lindgren4e23be42019-06-10 04:48:05 -0700836 size = ddata->module_size;
Tony Lindgrene4f50c82019-05-01 14:24:57 -0700837 }
Tony Lindgren0eecc632017-10-10 14:23:43 -0700838
Tony Lindgren0eecc632017-10-10 14:23:43 -0700839 ddata->module_va = devm_ioremap(ddata->dev,
840 ddata->module_pa,
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700841 size + sizeof(u32));
Tony Lindgren0eecc632017-10-10 14:23:43 -0700842 if (!ddata->module_va)
843 return -EIO;
844
845 return 0;
846}
847
848/**
849 * sysc_map_and_check_registers - ioremap and check device registers
850 * @ddata: device driver data
851 */
852static int sysc_map_and_check_registers(struct sysc *ddata)
853{
854 int error;
855
856 error = sysc_parse_and_check_child_range(ddata);
857 if (error)
858 return error;
859
Nishka Dasguptac6e78d72019-08-15 11:16:47 +0530860 sysc_check_children(ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700861
862 error = sysc_parse_registers(ddata);
863 if (error)
864 return error;
865
866 error = sysc_ioremap(ddata);
867 if (error)
868 return error;
869
870 error = sysc_check_registers(ddata);
871 if (error)
872 return error;
873
874 return 0;
875}
876
877/**
878 * sysc_show_rev - read and show interconnect target module revision
879 * @bufp: buffer to print the information to
880 * @ddata: device driver data
881 */
882static int sysc_show_rev(char *bufp, struct sysc *ddata)
883{
Tony Lindgren566a9b052017-12-15 09:41:19 -0800884 int len;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700885
886 if (ddata->offsets[SYSC_REVISION] < 0)
887 return sprintf(bufp, ":NA");
888
Tony Lindgren566a9b052017-12-15 09:41:19 -0800889 len = sprintf(bufp, ":%08x", ddata->revision);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700890
891 return len;
892}
893
894static int sysc_show_reg(struct sysc *ddata,
895 char *bufp, enum sysc_registers reg)
896{
897 if (ddata->offsets[reg] < 0)
898 return sprintf(bufp, ":NA");
899
900 return sprintf(bufp, ":%x", ddata->offsets[reg]);
901}
902
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800903static int sysc_show_name(char *bufp, struct sysc *ddata)
904{
905 if (!ddata->name)
906 return 0;
907
908 return sprintf(bufp, ":%s", ddata->name);
909}
910
Tony Lindgren0eecc632017-10-10 14:23:43 -0700911/**
912 * sysc_show_registers - show information about interconnect target module
913 * @ddata: device driver data
914 */
915static void sysc_show_registers(struct sysc *ddata)
916{
917 char buf[128];
918 char *bufp = buf;
919 int i;
920
921 for (i = 0; i < SYSC_MAX_REGS; i++)
922 bufp += sysc_show_reg(ddata, bufp, i);
923
924 bufp += sysc_show_rev(bufp, ddata);
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800925 bufp += sysc_show_name(bufp, ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700926
927 dev_dbg(ddata->dev, "%llx:%x%s\n",
928 ddata->module_pa, ddata->module_size,
929 buf);
930}
931
Tony Lindgrene8639e12020-02-24 12:58:03 -0800932/**
933 * sysc_write_sysconfig - handle sysconfig quirks for register write
934 * @ddata: device driver data
935 * @value: register value
936 */
937static void sysc_write_sysconfig(struct sysc *ddata, u32 value)
938{
939 if (ddata->module_unlock_quirk)
940 ddata->module_unlock_quirk(ddata);
941
942 sysc_write(ddata, ddata->offsets[SYSC_SYSCONFIG], value);
943
944 if (ddata->module_lock_quirk)
945 ddata->module_lock_quirk(ddata);
946}
947
Roger Quadrosd59b6052019-04-08 12:52:39 +0300948#define SYSC_IDLE_MASK (SYSC_NR_IDLEMODES - 1)
Tony Lindgrenae9ae122019-05-27 04:51:54 -0700949#define SYSC_CLOCACT_ICK 2
Roger Quadrosd59b6052019-04-08 12:52:39 +0300950
Tony Lindgren2b2f7de2019-05-27 04:51:53 -0700951/* Caller needs to manage sysc_clkdm_deny_idle() and sysc_clkdm_allow_idle() */
Roger Quadrosd59b6052019-04-08 12:52:39 +0300952static int sysc_enable_module(struct device *dev)
953{
954 struct sysc *ddata;
955 const struct sysc_regbits *regbits;
956 u32 reg, idlemodes, best_mode;
Tony Lindgrend46f9fb2020-05-31 12:37:54 -0700957 int error;
Roger Quadrosd59b6052019-04-08 12:52:39 +0300958
959 ddata = dev_get_drvdata(dev);
Tony Lindgrend46f9fb2020-05-31 12:37:54 -0700960
961 /*
962 * Some modules like DSS reset automatically on idle. Enable optional
963 * reset clocks and wait for OCP softreset to complete.
964 */
965 if (ddata->cfg.quirks & SYSC_QUIRK_OPT_CLKS_IN_RESET) {
966 error = sysc_enable_opt_clocks(ddata);
967 if (error) {
968 dev_err(ddata->dev,
969 "Optional clocks failed for enable: %i\n",
970 error);
971 return error;
972 }
973 }
974 error = sysc_wait_softreset(ddata);
975 if (error)
976 dev_warn(ddata->dev, "OCP softreset timed out\n");
977 if (ddata->cfg.quirks & SYSC_QUIRK_OPT_CLKS_IN_RESET)
978 sysc_disable_opt_clocks(ddata);
979
980 /*
981 * Some subsystem private interconnects, like DSS top level module,
982 * need only the automatic OCP softreset handling with no sysconfig
983 * register bits to configure.
984 */
Roger Quadrosd59b6052019-04-08 12:52:39 +0300985 if (ddata->offsets[SYSC_SYSCONFIG] == -ENODEV)
986 return 0;
987
Roger Quadrosd59b6052019-04-08 12:52:39 +0300988 regbits = ddata->cap->regbits;
989 reg = sysc_read(ddata, ddata->offsets[SYSC_SYSCONFIG]);
990
Tony Lindgrenae9ae122019-05-27 04:51:54 -0700991 /* Set CLOCKACTIVITY, we only use it for ick */
992 if (regbits->clkact_shift >= 0 &&
993 (ddata->cfg.quirks & SYSC_QUIRK_USE_CLOCKACT ||
994 ddata->cfg.sysc_val & BIT(regbits->clkact_shift)))
995 reg |= SYSC_CLOCACT_ICK << regbits->clkact_shift;
996
Roger Quadrosd59b6052019-04-08 12:52:39 +0300997 /* Set SIDLE mode */
998 idlemodes = ddata->cfg.sidlemodes;
999 if (!idlemodes || regbits->sidle_shift < 0)
1000 goto set_midle;
1001
Tony Lindgrenfb685f12019-05-27 04:51:55 -07001002 if (ddata->cfg.quirks & (SYSC_QUIRK_SWSUP_SIDLE |
1003 SYSC_QUIRK_SWSUP_SIDLE_ACT)) {
1004 best_mode = SYSC_IDLE_NO;
1005 } else {
1006 best_mode = fls(ddata->cfg.sidlemodes) - 1;
1007 if (best_mode > SYSC_IDLE_MASK) {
1008 dev_err(dev, "%s: invalid sidlemode\n", __func__);
1009 return -EINVAL;
1010 }
Tony Lindgren6e09f492019-05-27 04:51:55 -07001011
1012 /* Set WAKEUP */
1013 if (regbits->enwkup_shift >= 0 &&
1014 ddata->cfg.sysc_val & BIT(regbits->enwkup_shift))
1015 reg |= BIT(regbits->enwkup_shift);
Roger Quadrosd59b6052019-04-08 12:52:39 +03001016 }
1017
1018 reg &= ~(SYSC_IDLE_MASK << regbits->sidle_shift);
1019 reg |= best_mode << regbits->sidle_shift;
Tony Lindgrene8639e12020-02-24 12:58:03 -08001020 sysc_write_sysconfig(ddata, reg);
Roger Quadrosd59b6052019-04-08 12:52:39 +03001021
1022set_midle:
1023 /* Set MIDLE mode */
1024 idlemodes = ddata->cfg.midlemodes;
1025 if (!idlemodes || regbits->midle_shift < 0)
Tony Lindgreneec26552019-05-27 04:51:54 -07001026 goto set_autoidle;
Roger Quadrosd59b6052019-04-08 12:52:39 +03001027
1028 best_mode = fls(ddata->cfg.midlemodes) - 1;
1029 if (best_mode > SYSC_IDLE_MASK) {
1030 dev_err(dev, "%s: invalid midlemode\n", __func__);
1031 return -EINVAL;
1032 }
1033
Tony Lindgren03856e92019-10-21 13:36:09 -07001034 if (ddata->cfg.quirks & SYSC_QUIRK_SWSUP_MSTANDBY)
1035 best_mode = SYSC_IDLE_NO;
1036
Roger Quadrosd59b6052019-04-08 12:52:39 +03001037 reg &= ~(SYSC_IDLE_MASK << regbits->midle_shift);
1038 reg |= best_mode << regbits->midle_shift;
Tony Lindgrene8639e12020-02-24 12:58:03 -08001039 sysc_write_sysconfig(ddata, reg);
Roger Quadrosd59b6052019-04-08 12:52:39 +03001040
Tony Lindgreneec26552019-05-27 04:51:54 -07001041set_autoidle:
1042 /* Autoidle bit must enabled separately if available */
1043 if (regbits->autoidle_shift >= 0 &&
1044 ddata->cfg.sysc_val & BIT(regbits->autoidle_shift)) {
1045 reg |= 1 << regbits->autoidle_shift;
Tony Lindgrene8639e12020-02-24 12:58:03 -08001046 sysc_write_sysconfig(ddata, reg);
Tony Lindgreneec26552019-05-27 04:51:54 -07001047 }
1048
Tony Lindgren5ce8aee2020-05-27 06:49:29 -07001049 /* Flush posted write */
1050 sysc_read(ddata, ddata->offsets[SYSC_SYSCONFIG]);
1051
Tony Lindgrend7f563d2019-08-14 05:18:16 -07001052 if (ddata->module_enable_quirk)
1053 ddata->module_enable_quirk(ddata);
1054
Roger Quadrosd59b6052019-04-08 12:52:39 +03001055 return 0;
1056}
1057
1058static int sysc_best_idle_mode(u32 idlemodes, u32 *best_mode)
1059{
1060 if (idlemodes & BIT(SYSC_IDLE_SMART_WKUP))
1061 *best_mode = SYSC_IDLE_SMART_WKUP;
1062 else if (idlemodes & BIT(SYSC_IDLE_SMART))
1063 *best_mode = SYSC_IDLE_SMART;
Tony Lindgren6ee82412019-07-22 03:44:42 -07001064 else if (idlemodes & BIT(SYSC_IDLE_FORCE))
Roger Quadrosd59b6052019-04-08 12:52:39 +03001065 *best_mode = SYSC_IDLE_FORCE;
1066 else
1067 return -EINVAL;
1068
1069 return 0;
1070}
1071
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001072/* Caller needs to manage sysc_clkdm_deny_idle() and sysc_clkdm_allow_idle() */
Roger Quadrosd59b6052019-04-08 12:52:39 +03001073static int sysc_disable_module(struct device *dev)
1074{
1075 struct sysc *ddata;
1076 const struct sysc_regbits *regbits;
1077 u32 reg, idlemodes, best_mode;
1078 int ret;
1079
1080 ddata = dev_get_drvdata(dev);
1081 if (ddata->offsets[SYSC_SYSCONFIG] == -ENODEV)
1082 return 0;
1083
Tony Lindgrenc7d86692019-10-17 11:21:44 -07001084 if (ddata->module_disable_quirk)
1085 ddata->module_disable_quirk(ddata);
1086
Roger Quadrosd59b6052019-04-08 12:52:39 +03001087 regbits = ddata->cap->regbits;
1088 reg = sysc_read(ddata, ddata->offsets[SYSC_SYSCONFIG]);
1089
1090 /* Set MIDLE mode */
1091 idlemodes = ddata->cfg.midlemodes;
1092 if (!idlemodes || regbits->midle_shift < 0)
1093 goto set_sidle;
1094
1095 ret = sysc_best_idle_mode(idlemodes, &best_mode);
1096 if (ret) {
1097 dev_err(dev, "%s: invalid midlemode\n", __func__);
1098 return ret;
1099 }
1100
Tony Lindgren93c60482019-12-09 13:55:15 -08001101 if (ddata->cfg.quirks & (SYSC_QUIRK_SWSUP_MSTANDBY) ||
1102 ddata->cfg.quirks & (SYSC_QUIRK_FORCE_MSTANDBY))
Tony Lindgren03856e92019-10-21 13:36:09 -07001103 best_mode = SYSC_IDLE_FORCE;
1104
Roger Quadrosd59b6052019-04-08 12:52:39 +03001105 reg &= ~(SYSC_IDLE_MASK << regbits->midle_shift);
1106 reg |= best_mode << regbits->midle_shift;
Tony Lindgrene8639e12020-02-24 12:58:03 -08001107 sysc_write_sysconfig(ddata, reg);
Roger Quadrosd59b6052019-04-08 12:52:39 +03001108
1109set_sidle:
1110 /* Set SIDLE mode */
1111 idlemodes = ddata->cfg.sidlemodes;
1112 if (!idlemodes || regbits->sidle_shift < 0)
1113 return 0;
1114
Tony Lindgrenfb685f12019-05-27 04:51:55 -07001115 if (ddata->cfg.quirks & SYSC_QUIRK_SWSUP_SIDLE) {
1116 best_mode = SYSC_IDLE_FORCE;
1117 } else {
1118 ret = sysc_best_idle_mode(idlemodes, &best_mode);
1119 if (ret) {
1120 dev_err(dev, "%s: invalid sidlemode\n", __func__);
1121 return ret;
1122 }
Roger Quadrosd59b6052019-04-08 12:52:39 +03001123 }
1124
1125 reg &= ~(SYSC_IDLE_MASK << regbits->sidle_shift);
1126 reg |= best_mode << regbits->sidle_shift;
Tony Lindgreneec26552019-05-27 04:51:54 -07001127 if (regbits->autoidle_shift >= 0 &&
1128 ddata->cfg.sysc_val & BIT(regbits->autoidle_shift))
1129 reg |= 1 << regbits->autoidle_shift;
Tony Lindgrene8639e12020-02-24 12:58:03 -08001130 sysc_write_sysconfig(ddata, reg);
Roger Quadrosd59b6052019-04-08 12:52:39 +03001131
Tony Lindgren5ce8aee2020-05-27 06:49:29 -07001132 /* Flush posted write */
1133 sysc_read(ddata, ddata->offsets[SYSC_SYSCONFIG]);
1134
Roger Quadrosd59b6052019-04-08 12:52:39 +03001135 return 0;
1136}
1137
Tony Lindgrenff437282019-03-21 11:00:21 -07001138static int __maybe_unused sysc_runtime_suspend_legacy(struct device *dev,
1139 struct sysc *ddata)
Tony Lindgren0eecc632017-10-10 14:23:43 -07001140{
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001141 struct ti_sysc_platform_data *pdata;
Tony Lindgrenff437282019-03-21 11:00:21 -07001142 int error;
1143
1144 pdata = dev_get_platdata(ddata->dev);
1145 if (!pdata)
1146 return 0;
1147
1148 if (!pdata->idle_module)
1149 return -ENODEV;
1150
1151 error = pdata->idle_module(dev, &ddata->cookie);
1152 if (error)
1153 dev_err(dev, "%s: could not idle: %i\n",
1154 __func__, error);
1155
Tero Kristo4345f0d2019-08-07 15:46:04 +03001156 reset_control_assert(ddata->rsts);
Tony Lindgren8383e252019-05-27 04:51:56 -07001157
Tony Lindgrenff437282019-03-21 11:00:21 -07001158 return 0;
1159}
1160
1161static int __maybe_unused sysc_runtime_resume_legacy(struct device *dev,
1162 struct sysc *ddata)
1163{
1164 struct ti_sysc_platform_data *pdata;
1165 int error;
1166
1167 pdata = dev_get_platdata(ddata->dev);
1168 if (!pdata)
1169 return 0;
1170
1171 if (!pdata->enable_module)
1172 return -ENODEV;
1173
1174 error = pdata->enable_module(dev, &ddata->cookie);
1175 if (error)
1176 dev_err(dev, "%s: could not enable: %i\n",
1177 __func__, error);
1178
Tero Kristobf59ebb2019-10-07 15:29:29 +03001179 reset_control_deassert(ddata->rsts);
1180
Tony Lindgrenff437282019-03-21 11:00:21 -07001181 return 0;
1182}
1183
1184static int __maybe_unused sysc_runtime_suspend(struct device *dev)
1185{
Tony Lindgren0eecc632017-10-10 14:23:43 -07001186 struct sysc *ddata;
Tony Lindgrend8789702019-03-21 11:00:21 -07001187 int error = 0;
Tony Lindgren0eecc632017-10-10 14:23:43 -07001188
1189 ddata = dev_get_drvdata(dev);
1190
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001191 if (!ddata->enabled)
Tony Lindgren0eecc632017-10-10 14:23:43 -07001192 return 0;
1193
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001194 sysc_clkdm_deny_idle(ddata);
1195
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001196 if (ddata->legacy_mode) {
Tony Lindgrenff437282019-03-21 11:00:21 -07001197 error = sysc_runtime_suspend_legacy(dev, ddata);
Tony Lindgren93de83a2019-03-21 11:00:21 -07001198 if (error)
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001199 goto err_allow_idle;
Roger Quadrosd59b6052019-04-08 12:52:39 +03001200 } else {
1201 error = sysc_disable_module(dev);
1202 if (error)
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001203 goto err_allow_idle;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001204 }
1205
Tony Lindgrend8789702019-03-21 11:00:21 -07001206 sysc_disable_main_clocks(ddata);
Tony Lindgren09dfe582018-04-16 10:25:52 -07001207
Tony Lindgrend8789702019-03-21 11:00:21 -07001208 if (sysc_opt_clks_needed(ddata))
1209 sysc_disable_opt_clocks(ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001210
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001211 ddata->enabled = false;
1212
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001213err_allow_idle:
Tero Kristo4345f0d2019-08-07 15:46:04 +03001214 reset_control_assert(ddata->rsts);
Tony Lindgren8383e252019-05-27 04:51:56 -07001215
Tero Kristob6036312019-08-07 15:46:03 +03001216 sysc_clkdm_allow_idle(ddata);
1217
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001218 return error;
Tony Lindgren0eecc632017-10-10 14:23:43 -07001219}
1220
Arnd Bergmanna4a5d492017-10-13 11:25:32 +02001221static int __maybe_unused sysc_runtime_resume(struct device *dev)
Tony Lindgren0eecc632017-10-10 14:23:43 -07001222{
1223 struct sysc *ddata;
Tony Lindgrend8789702019-03-21 11:00:21 -07001224 int error = 0;
Tony Lindgren0eecc632017-10-10 14:23:43 -07001225
1226 ddata = dev_get_drvdata(dev);
1227
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001228 if (ddata->enabled)
Tony Lindgren0eecc632017-10-10 14:23:43 -07001229 return 0;
1230
Tony Lindgren8383e252019-05-27 04:51:56 -07001231
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001232 sysc_clkdm_deny_idle(ddata);
1233
Tony Lindgrend8789702019-03-21 11:00:21 -07001234 if (sysc_opt_clks_needed(ddata)) {
1235 error = sysc_enable_opt_clocks(ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001236 if (error)
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001237 goto err_allow_idle;
Tony Lindgren0eecc632017-10-10 14:23:43 -07001238 }
1239
Tony Lindgrend8789702019-03-21 11:00:21 -07001240 error = sysc_enable_main_clocks(ddata);
1241 if (error)
Tony Lindgren93de83a2019-03-21 11:00:21 -07001242 goto err_opt_clocks;
1243
Tero Kristobf59ebb2019-10-07 15:29:29 +03001244 reset_control_deassert(ddata->rsts);
1245
Tony Lindgren93de83a2019-03-21 11:00:21 -07001246 if (ddata->legacy_mode) {
1247 error = sysc_runtime_resume_legacy(dev, ddata);
1248 if (error)
1249 goto err_main_clocks;
Roger Quadrosd59b6052019-04-08 12:52:39 +03001250 } else {
1251 error = sysc_enable_module(dev);
1252 if (error)
1253 goto err_main_clocks;
Tony Lindgren93de83a2019-03-21 11:00:21 -07001254 }
Tony Lindgrend8789702019-03-21 11:00:21 -07001255
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001256 ddata->enabled = true;
1257
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001258 sysc_clkdm_allow_idle(ddata);
1259
Tony Lindgrend8789702019-03-21 11:00:21 -07001260 return 0;
1261
1262err_main_clocks:
Tony Lindgren93de83a2019-03-21 11:00:21 -07001263 sysc_disable_main_clocks(ddata);
1264err_opt_clocks:
Tony Lindgrend8789702019-03-21 11:00:21 -07001265 if (sysc_opt_clks_needed(ddata))
1266 sysc_disable_opt_clocks(ddata);
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001267err_allow_idle:
1268 sysc_clkdm_allow_idle(ddata);
Tony Lindgrend8789702019-03-21 11:00:21 -07001269
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001270 return error;
Tony Lindgren0eecc632017-10-10 14:23:43 -07001271}
1272
Tony Lindgrenf5e802032018-10-01 09:33:28 -07001273static int __maybe_unused sysc_noirq_suspend(struct device *dev)
Tony Lindgren62020f22018-02-22 13:59:44 -08001274{
1275 struct sysc *ddata;
1276
1277 ddata = dev_get_drvdata(dev);
1278
Tony Lindgren40d9f912018-09-24 12:16:54 -07001279 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
Tony Lindgrene7420c22018-04-16 10:26:46 -07001280 return 0;
1281
Tony Lindgrenf5e802032018-10-01 09:33:28 -07001282 return pm_runtime_force_suspend(dev);
Tony Lindgren62020f22018-02-22 13:59:44 -08001283}
1284
Tony Lindgrenf5e802032018-10-01 09:33:28 -07001285static int __maybe_unused sysc_noirq_resume(struct device *dev)
Tony Lindgren62020f22018-02-22 13:59:44 -08001286{
1287 struct sysc *ddata;
1288
1289 ddata = dev_get_drvdata(dev);
Tony Lindgrene7420c22018-04-16 10:26:46 -07001290
Tony Lindgren40d9f912018-09-24 12:16:54 -07001291 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
Tony Lindgrene7420c22018-04-16 10:26:46 -07001292 return 0;
1293
Tony Lindgrenf5e802032018-10-01 09:33:28 -07001294 return pm_runtime_force_resume(dev);
Tony Lindgrene7420c22018-04-16 10:26:46 -07001295}
1296
Tony Lindgren0eecc632017-10-10 14:23:43 -07001297static const struct dev_pm_ops sysc_pm_ops = {
Tony Lindgrene7420c22018-04-16 10:26:46 -07001298 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(sysc_noirq_suspend, sysc_noirq_resume)
Tony Lindgren0eecc632017-10-10 14:23:43 -07001299 SET_RUNTIME_PM_OPS(sysc_runtime_suspend,
1300 sysc_runtime_resume,
1301 NULL)
1302};
1303
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001304/* Module revision register based quirks */
1305struct sysc_revision_quirk {
1306 const char *name;
1307 u32 base;
1308 int rev_offset;
1309 int sysc_offset;
1310 int syss_offset;
1311 u32 revision;
1312 u32 revision_mask;
1313 u32 quirks;
1314};
1315
1316#define SYSC_QUIRK(optname, optbase, optrev, optsysc, optsyss, \
1317 optrev_val, optrevmask, optquirkmask) \
1318 { \
1319 .name = (optname), \
1320 .base = (optbase), \
1321 .rev_offset = (optrev), \
1322 .sysc_offset = (optsysc), \
1323 .syss_offset = (optsyss), \
1324 .revision = (optrev_val), \
1325 .revision_mask = (optrevmask), \
1326 .quirks = (optquirkmask), \
1327 }
1328
1329static const struct sysc_revision_quirk sysc_revision_quirks[] = {
1330 /* These drivers need to be fixed to not use pm_runtime_irq_safe() */
Tony Lindgren3a3d8022018-09-27 13:33:58 -07001331 SYSC_QUIRK("gpio", 0, 0, 0x10, 0x114, 0x50600801, 0xffff00ff,
Tony Lindgren09dfe582018-04-16 10:25:52 -07001332 SYSC_QUIRK_LEGACY_IDLE | SYSC_QUIRK_OPT_CLKS_IN_RESET),
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001333 SYSC_QUIRK("sham", 0, 0x100, 0x110, 0x114, 0x40000c03, 0xffffffff,
1334 SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001335 SYSC_QUIRK("smartreflex", 0, -ENODEV, 0x24, -ENODEV, 0x00000000, 0xffffffff,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001336 SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001337 SYSC_QUIRK("smartreflex", 0, -ENODEV, 0x38, -ENODEV, 0x00000000, 0xffffffff,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001338 SYSC_QUIRK_LEGACY_IDLE),
1339 SYSC_QUIRK("timer", 0, 0, 0x10, 0x14, 0x00000015, 0xffffffff,
Tony Lindgren9bd34c62019-01-22 09:03:08 -08001340 0),
Tony Lindgren8cde5d52018-04-16 10:25:15 -07001341 /* Some timers on omap4 and later */
Tony Lindgren590e15c2020-02-24 12:58:03 -08001342 SYSC_QUIRK("timer", 0, 0, 0x10, -ENODEV, 0x50002100, 0xffffffff,
Tony Lindgren072167d2019-01-29 07:53:47 -08001343 0),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001344 SYSC_QUIRK("timer", 0, 0, 0x10, -ENODEV, 0x4fff1301, 0xffff00ff,
Tony Lindgren9bd34c62019-01-22 09:03:08 -08001345 0),
Tony Lindgrenb6a53c42019-05-27 04:51:57 -07001346 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x00000046, 0xffffffff,
1347 SYSC_QUIRK_SWSUP_SIDLE | SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001348 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x00000052, 0xffffffff,
Tony Lindgrenb6a53c42019-05-27 04:51:57 -07001349 SYSC_QUIRK_SWSUP_SIDLE | SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgrend708bb12018-04-16 10:25:32 -07001350 /* Uarts on omap4 and later */
Tony Lindgrenb82beef2018-09-24 12:16:59 -07001351 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x50411e03, 0xffff00ff,
Tony Lindgrenb4a9a7a2019-03-21 13:27:08 -07001352 SYSC_QUIRK_SWSUP_SIDLE_ACT | SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgrenb82beef2018-09-24 12:16:59 -07001353 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x47422e03, 0xffffffff,
Tony Lindgrenb4a9a7a2019-03-21 13:27:08 -07001354 SYSC_QUIRK_SWSUP_SIDLE_ACT | SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgren7e27e5d2018-04-20 14:54:16 -07001355
Tony Lindgrena54275f2019-03-21 11:00:21 -07001356 /* Quirks that need to be set based on the module address */
Tony Lindgren590e15c2020-02-24 12:58:03 -08001357 SYSC_QUIRK("mcpdm", 0x40132000, 0, 0x10, -ENODEV, 0x50000800, 0xffffffff,
Tony Lindgrena54275f2019-03-21 11:00:21 -07001358 SYSC_QUIRK_EXT_OPT_CLOCK | SYSC_QUIRK_NO_RESET_ON_INIT |
1359 SYSC_QUIRK_SWSUP_SIDLE),
1360
Tony Lindgren4e23be42019-06-10 04:48:05 -07001361 /* Quirks that need to be set based on detected module */
Tony Lindgren590e15c2020-02-24 12:58:03 -08001362 SYSC_QUIRK("aess", 0, 0, 0x10, -ENODEV, 0x40000000, 0xffffffff,
Tony Lindgren020003f2019-11-14 08:25:28 -08001363 SYSC_MODULE_QUIRK_AESS),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001364 SYSC_QUIRK("dcan", 0x48480000, 0x20, -ENODEV, -ENODEV, 0xa3170504, 0xffffffff,
Tony Lindgren94f63452019-12-12 08:57:58 -08001365 SYSC_QUIRK_CLKDM_NOAUTO),
Tony Lindgren77dfece2020-02-24 12:58:03 -08001366 SYSC_QUIRK("dss", 0x4832a000, 0, 0x10, 0x14, 0x00000020, 0xffffffff,
Tony Lindgren7324a7a2020-02-24 12:58:03 -08001367 SYSC_QUIRK_OPT_CLKS_IN_RESET | SYSC_MODULE_QUIRK_DSS_RESET),
Tony Lindgren77dfece2020-02-24 12:58:03 -08001368 SYSC_QUIRK("dss", 0x58000000, 0, -ENODEV, 0x14, 0x00000040, 0xffffffff,
Tony Lindgren7324a7a2020-02-24 12:58:03 -08001369 SYSC_QUIRK_OPT_CLKS_IN_RESET | SYSC_MODULE_QUIRK_DSS_RESET),
Tony Lindgren77dfece2020-02-24 12:58:03 -08001370 SYSC_QUIRK("dss", 0x58000000, 0, -ENODEV, 0x14, 0x00000061, 0xffffffff,
Tony Lindgren7324a7a2020-02-24 12:58:03 -08001371 SYSC_QUIRK_OPT_CLKS_IN_RESET | SYSC_MODULE_QUIRK_DSS_RESET),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001372 SYSC_QUIRK("dwc3", 0x48880000, 0, 0x10, -ENODEV, 0x500a0200, 0xffffffff,
Tony Lindgren94f63452019-12-12 08:57:58 -08001373 SYSC_QUIRK_CLKDM_NOAUTO),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001374 SYSC_QUIRK("dwc3", 0x488c0000, 0, 0x10, -ENODEV, 0x500a0200, 0xffffffff,
Tony Lindgren94f63452019-12-12 08:57:58 -08001375 SYSC_QUIRK_CLKDM_NOAUTO),
Tony Lindgren77dfece2020-02-24 12:58:03 -08001376 SYSC_QUIRK("hdmi", 0, 0, 0x10, -ENODEV, 0x50030200, 0xffffffff,
1377 SYSC_QUIRK_OPT_CLKS_NEEDED),
Tony Lindgren4e23be42019-06-10 04:48:05 -07001378 SYSC_QUIRK("hdq1w", 0, 0, 0x14, 0x18, 0x00000006, 0xffffffff,
1379 SYSC_MODULE_QUIRK_HDQ1W),
1380 SYSC_QUIRK("hdq1w", 0, 0, 0x14, 0x18, 0x0000000a, 0xffffffff,
1381 SYSC_MODULE_QUIRK_HDQ1W),
1382 SYSC_QUIRK("i2c", 0, 0, 0x20, 0x10, 0x00000036, 0x000000ff,
1383 SYSC_MODULE_QUIRK_I2C),
1384 SYSC_QUIRK("i2c", 0, 0, 0x20, 0x10, 0x0000003c, 0x000000ff,
1385 SYSC_MODULE_QUIRK_I2C),
1386 SYSC_QUIRK("i2c", 0, 0, 0x20, 0x10, 0x00000040, 0x000000ff,
1387 SYSC_MODULE_QUIRK_I2C),
1388 SYSC_QUIRK("i2c", 0, 0, 0x10, 0x90, 0x5040000a, 0xfffff0f0,
1389 SYSC_MODULE_QUIRK_I2C),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001390 SYSC_QUIRK("gpu", 0x50000000, 0x14, -ENODEV, -ENODEV, 0x00010201, 0xffffffff, 0),
1391 SYSC_QUIRK("gpu", 0x50000000, 0xfe00, 0xfe10, -ENODEV, 0x40000000 , 0xffffffff,
Tony Lindgrend7f563d2019-08-14 05:18:16 -07001392 SYSC_MODULE_QUIRK_SGX),
Tony Lindgrenaef067e2020-03-10 09:06:02 -07001393 SYSC_QUIRK("lcdc", 0, 0, 0x54, -ENODEV, 0x4f201000, 0xffffffff,
1394 SYSC_QUIRK_SWSUP_SIDLE | SYSC_QUIRK_SWSUP_MSTANDBY),
Tony Lindgrene8639e12020-02-24 12:58:03 -08001395 SYSC_QUIRK("rtc", 0, 0x74, 0x78, -ENODEV, 0x4eb01908, 0xffff00f0,
1396 SYSC_MODULE_QUIRK_RTC_UNLOCK),
Tony Lindgren25bfaaa2020-03-03 09:31:00 -08001397 SYSC_QUIRK("tptc", 0, 0, 0x10, -ENODEV, 0x40006c00, 0xffffefff,
1398 SYSC_QUIRK_SWSUP_SIDLE | SYSC_QUIRK_SWSUP_MSTANDBY),
1399 SYSC_QUIRK("tptc", 0, 0, -ENODEV, -ENODEV, 0x40007c00, 0xffffffff,
1400 SYSC_QUIRK_SWSUP_SIDLE | SYSC_QUIRK_SWSUP_MSTANDBY),
Tony Lindgren03856e92019-10-21 13:36:09 -07001401 SYSC_QUIRK("usb_otg_hs", 0, 0x400, 0x404, 0x408, 0x00000050,
1402 0xffffffff, SYSC_QUIRK_SWSUP_SIDLE | SYSC_QUIRK_SWSUP_MSTANDBY),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001403 SYSC_QUIRK("usb_otg_hs", 0, 0, 0x10, -ENODEV, 0x4ea2080d, 0xffffffff,
Tony Lindgren1819ef22019-10-21 14:15:55 -07001404 SYSC_QUIRK_SWSUP_SIDLE | SYSC_QUIRK_SWSUP_MSTANDBY),
Tony Lindgren4e23be42019-06-10 04:48:05 -07001405 SYSC_QUIRK("wdt", 0, 0, 0x10, 0x14, 0x502a0500, 0xfffff0f0,
1406 SYSC_MODULE_QUIRK_WDT),
Suman Annab2745d92020-02-27 16:28:34 -06001407 /* PRUSS on am3, am4 and am5 */
1408 SYSC_QUIRK("pruss", 0, 0x26000, 0x26004, -ENODEV, 0x47000000, 0xff000000,
1409 SYSC_MODULE_QUIRK_PRUSS),
Tony Lindgrenc7d86692019-10-17 11:21:44 -07001410 /* Watchdog on am3 and am4 */
1411 SYSC_QUIRK("wdt", 0x44e35000, 0, 0x10, 0x14, 0x502a0500, 0xfffff0f0,
1412 SYSC_MODULE_QUIRK_WDT | SYSC_QUIRK_SWSUP_SIDLE),
Tony Lindgren4e23be42019-06-10 04:48:05 -07001413
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001414#ifdef DEBUG
Tony Lindgren590e15c2020-02-24 12:58:03 -08001415 SYSC_QUIRK("adc", 0, 0, 0x10, -ENODEV, 0x47300001, 0xffffffff, 0),
1416 SYSC_QUIRK("atl", 0, 0, -ENODEV, -ENODEV, 0x0a070100, 0xffffffff, 0),
1417 SYSC_QUIRK("cm", 0, 0, -ENODEV, -ENODEV, 0x40000301, 0xffffffff, 0),
1418 SYSC_QUIRK("control", 0, 0, 0x10, -ENODEV, 0x40000900, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001419 SYSC_QUIRK("cpgmac", 0, 0x1200, 0x1208, 0x1204, 0x4edb1902,
Tony Lindgren23731ea2018-09-24 12:17:09 -07001420 0xffff00f0, 0),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001421 SYSC_QUIRK("dcan", 0, 0x20, -ENODEV, -ENODEV, 0xa3170504, 0xffffffff, 0),
1422 SYSC_QUIRK("dcan", 0, 0x20, -ENODEV, -ENODEV, 0x4edb1902, 0xffffffff, 0),
Tony Lindgren77dfece2020-02-24 12:58:03 -08001423 SYSC_QUIRK("dispc", 0x4832a400, 0, 0x10, 0x14, 0x00000030, 0xffffffff, 0),
1424 SYSC_QUIRK("dispc", 0x58001000, 0, 0x10, 0x14, 0x00000040, 0xffffffff, 0),
1425 SYSC_QUIRK("dispc", 0x58001000, 0, 0x10, 0x14, 0x00000051, 0xffffffff, 0),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001426 SYSC_QUIRK("dmic", 0, 0, 0x10, -ENODEV, 0x50010000, 0xffffffff, 0),
Tony Lindgren77dfece2020-02-24 12:58:03 -08001427 SYSC_QUIRK("dsi", 0x58004000, 0, 0x10, 0x14, 0x00000030, 0xffffffff, 0),
1428 SYSC_QUIRK("dsi", 0x58005000, 0, 0x10, 0x14, 0x00000030, 0xffffffff, 0),
1429 SYSC_QUIRK("dsi", 0x58005000, 0, 0x10, 0x14, 0x00000040, 0xffffffff, 0),
1430 SYSC_QUIRK("dsi", 0x58009000, 0, 0x10, 0x14, 0x00000040, 0xffffffff, 0),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001431 SYSC_QUIRK("dwc3", 0, 0, 0x10, -ENODEV, 0x500a0200, 0xffffffff, 0),
Tony Lindgren7edd00f2019-08-14 05:18:16 -07001432 SYSC_QUIRK("d2d", 0x4a0b6000, 0, 0x10, 0x14, 0x00000010, 0xffffffff, 0),
1433 SYSC_QUIRK("d2d", 0x4a0cd000, 0, 0x10, 0x14, 0x00000010, 0xffffffff, 0),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001434 SYSC_QUIRK("epwmss", 0, 0, 0x4, -ENODEV, 0x47400001, 0xffffffff, 0),
1435 SYSC_QUIRK("gpu", 0, 0x1fc00, 0x1fc10, -ENODEV, 0, 0, 0),
1436 SYSC_QUIRK("gpu", 0, 0xfe00, 0xfe10, -ENODEV, 0x40000000 , 0xffffffff, 0),
Tony Lindgren77dfece2020-02-24 12:58:03 -08001437 SYSC_QUIRK("hdmi", 0, 0, 0x10, -ENODEV, 0x50031d00, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001438 SYSC_QUIRK("hsi", 0, 0, 0x10, 0x14, 0x50043101, 0xffffffff, 0),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001439 SYSC_QUIRK("iss", 0, 0, 0x10, -ENODEV, 0x40000101, 0xffffffff, 0),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001440 SYSC_QUIRK("mcasp", 0, 0, 0x4, -ENODEV, 0x44306302, 0xffffffff, 0),
1441 SYSC_QUIRK("mcasp", 0, 0, 0x4, -ENODEV, 0x44307b02, 0xffffffff, 0),
1442 SYSC_QUIRK("mcbsp", 0, -ENODEV, 0x8c, -ENODEV, 0, 0, 0),
1443 SYSC_QUIRK("mcspi", 0, 0, 0x10, -ENODEV, 0x40300a0b, 0xffff00ff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001444 SYSC_QUIRK("mcspi", 0, 0, 0x110, 0x114, 0x40300a0b, 0xffffffff, 0),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001445 SYSC_QUIRK("mailbox", 0, 0, 0x10, -ENODEV, 0x00000400, 0xffffffff, 0),
1446 SYSC_QUIRK("m3", 0, 0, -ENODEV, -ENODEV, 0x5f580105, 0x0fff0f00, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -07001447 SYSC_QUIRK("ocp2scp", 0, 0, 0x10, 0x14, 0x50060005, 0xfffffff0, 0),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001448 SYSC_QUIRK("ocp2scp", 0, 0, -ENODEV, -ENODEV, 0x50060007, 0xffffffff, 0),
1449 SYSC_QUIRK("padconf", 0, 0, 0x10, -ENODEV, 0x4fff0800, 0xffffffff, 0),
1450 SYSC_QUIRK("padconf", 0, 0, -ENODEV, -ENODEV, 0x40001100, 0xffffffff, 0),
1451 SYSC_QUIRK("prcm", 0, 0, -ENODEV, -ENODEV, 0x40000100, 0xffffffff, 0),
1452 SYSC_QUIRK("prcm", 0, 0, -ENODEV, -ENODEV, 0x00004102, 0xffffffff, 0),
1453 SYSC_QUIRK("prcm", 0, 0, -ENODEV, -ENODEV, 0x40000400, 0xffffffff, 0),
Tony Lindgren77dfece2020-02-24 12:58:03 -08001454 SYSC_QUIRK("rfbi", 0x4832a800, 0, 0x10, 0x14, 0x00000010, 0xffffffff, 0),
1455 SYSC_QUIRK("rfbi", 0x58002000, 0, 0x10, 0x14, 0x00000010, 0xffffffff, 0),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001456 SYSC_QUIRK("scm", 0, 0, 0x10, -ENODEV, 0x40000900, 0xffffffff, 0),
1457 SYSC_QUIRK("scm", 0, 0, -ENODEV, -ENODEV, 0x4e8b0100, 0xffffffff, 0),
1458 SYSC_QUIRK("scm", 0, 0, -ENODEV, -ENODEV, 0x4f000100, 0xffffffff, 0),
1459 SYSC_QUIRK("scm", 0, 0, -ENODEV, -ENODEV, 0x40000900, 0xffffffff, 0),
1460 SYSC_QUIRK("scrm", 0, 0, -ENODEV, -ENODEV, 0x00000010, 0xffffffff, 0),
1461 SYSC_QUIRK("sdio", 0, 0, 0x10, -ENODEV, 0x40202301, 0xffff0ff0, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001462 SYSC_QUIRK("sdio", 0, 0x2fc, 0x110, 0x114, 0x31010000, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -07001463 SYSC_QUIRK("sdma", 0, 0, 0x2c, 0x28, 0x00010900, 0xffffffff, 0),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001464 SYSC_QUIRK("slimbus", 0, 0, 0x10, -ENODEV, 0x40000902, 0xffffffff, 0),
1465 SYSC_QUIRK("slimbus", 0, 0, 0x10, -ENODEV, 0x40002903, 0xffffffff, 0),
1466 SYSC_QUIRK("spinlock", 0, 0, 0x10, -ENODEV, 0x50020000, 0xffffffff, 0),
1467 SYSC_QUIRK("rng", 0, 0x1fe0, 0x1fe4, -ENODEV, 0x00000020, 0xffffffff, 0),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001468 SYSC_QUIRK("timer32k", 0, 0, 0x4, -ENODEV, 0x00000060, 0xffffffff, 0),
Tony Lindgren25bfaaa2020-03-03 09:31:00 -08001469 SYSC_QUIRK("tpcc", 0, 0, -ENODEV, -ENODEV, 0x40014c00, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001470 SYSC_QUIRK("usbhstll", 0, 0, 0x10, 0x14, 0x00000004, 0xffffffff, 0),
Tony Lindgrenf0106702018-11-15 14:46:53 -08001471 SYSC_QUIRK("usbhstll", 0, 0, 0x10, 0x14, 0x00000008, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001472 SYSC_QUIRK("usb_host_hs", 0, 0, 0x10, 0x14, 0x50700100, 0xffffffff, 0),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001473 SYSC_QUIRK("usb_host_hs", 0, 0, 0x10, -ENODEV, 0x50700101, 0xffffffff, 0),
Tony Lindgren77dfece2020-02-24 12:58:03 -08001474 SYSC_QUIRK("venc", 0x58003000, 0, -ENODEV, -ENODEV, 0x00000002, 0xffffffff, 0),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001475 SYSC_QUIRK("vfpe", 0, 0, 0x104, -ENODEV, 0x4d001200, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001476#endif
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001477};
1478
Tony Lindgren42b9c5c2019-03-21 11:00:21 -07001479/*
1480 * Early quirks based on module base and register offsets only that are
1481 * needed before the module revision can be read
1482 */
1483static void sysc_init_early_quirks(struct sysc *ddata)
1484{
1485 const struct sysc_revision_quirk *q;
1486 int i;
1487
1488 for (i = 0; i < ARRAY_SIZE(sysc_revision_quirks); i++) {
1489 q = &sysc_revision_quirks[i];
1490
1491 if (!q->base)
1492 continue;
1493
1494 if (q->base != ddata->module_pa)
1495 continue;
1496
Tony Lindgren590e15c2020-02-24 12:58:03 -08001497 if (q->rev_offset != ddata->offsets[SYSC_REVISION])
Tony Lindgren42b9c5c2019-03-21 11:00:21 -07001498 continue;
1499
Tony Lindgren590e15c2020-02-24 12:58:03 -08001500 if (q->sysc_offset != ddata->offsets[SYSC_SYSCONFIG])
Tony Lindgren42b9c5c2019-03-21 11:00:21 -07001501 continue;
1502
Tony Lindgren590e15c2020-02-24 12:58:03 -08001503 if (q->syss_offset != ddata->offsets[SYSC_SYSSTATUS])
Tony Lindgren42b9c5c2019-03-21 11:00:21 -07001504 continue;
1505
1506 ddata->name = q->name;
1507 ddata->cfg.quirks |= q->quirks;
1508 }
1509}
1510
1511/* Quirks that also consider the revision register value */
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001512static void sysc_init_revision_quirks(struct sysc *ddata)
1513{
1514 const struct sysc_revision_quirk *q;
1515 int i;
1516
1517 for (i = 0; i < ARRAY_SIZE(sysc_revision_quirks); i++) {
1518 q = &sysc_revision_quirks[i];
1519
1520 if (q->base && q->base != ddata->module_pa)
1521 continue;
1522
Tony Lindgren590e15c2020-02-24 12:58:03 -08001523 if (q->rev_offset != ddata->offsets[SYSC_REVISION])
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001524 continue;
1525
Tony Lindgren590e15c2020-02-24 12:58:03 -08001526 if (q->sysc_offset != ddata->offsets[SYSC_SYSCONFIG])
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001527 continue;
1528
Tony Lindgren590e15c2020-02-24 12:58:03 -08001529 if (q->syss_offset != ddata->offsets[SYSC_SYSSTATUS])
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001530 continue;
1531
1532 if (q->revision == ddata->revision ||
1533 (q->revision & q->revision_mask) ==
1534 (ddata->revision & q->revision_mask)) {
1535 ddata->name = q->name;
1536 ddata->cfg.quirks |= q->quirks;
1537 }
1538 }
1539}
1540
Tony Lindgren7324a7a2020-02-24 12:58:03 -08001541/*
1542 * DSS needs dispc outputs disabled to reset modules. Returns mask of
1543 * enabled DSS interrupts. Eventually we may be able to do this on
1544 * dispc init rather than top-level DSS init.
1545 */
1546static u32 sysc_quirk_dispc(struct sysc *ddata, int dispc_offset,
1547 bool disable)
1548{
1549 bool lcd_en, digit_en, lcd2_en = false, lcd3_en = false;
1550 const int lcd_en_mask = BIT(0), digit_en_mask = BIT(1);
1551 int manager_count;
1552 bool framedonetv_irq;
1553 u32 val, irq_mask = 0;
1554
1555 switch (sysc_soc->soc) {
1556 case SOC_2420 ... SOC_3630:
1557 manager_count = 2;
1558 framedonetv_irq = false;
1559 break;
1560 case SOC_4430 ... SOC_4470:
1561 manager_count = 3;
1562 break;
1563 case SOC_5430:
1564 case SOC_DRA7:
1565 manager_count = 4;
1566 break;
1567 case SOC_AM4:
1568 manager_count = 1;
1569 break;
1570 case SOC_UNKNOWN:
1571 default:
1572 return 0;
1573 };
1574
1575 /* Remap the whole module range to be able to reset dispc outputs */
1576 devm_iounmap(ddata->dev, ddata->module_va);
1577 ddata->module_va = devm_ioremap(ddata->dev,
1578 ddata->module_pa,
1579 ddata->module_size);
1580 if (!ddata->module_va)
1581 return -EIO;
1582
1583 /* DISP_CONTROL */
1584 val = sysc_read(ddata, dispc_offset + 0x40);
1585 lcd_en = val & lcd_en_mask;
1586 digit_en = val & digit_en_mask;
1587 if (lcd_en)
1588 irq_mask |= BIT(0); /* FRAMEDONE */
1589 if (digit_en) {
1590 if (framedonetv_irq)
1591 irq_mask |= BIT(24); /* FRAMEDONETV */
1592 else
1593 irq_mask |= BIT(2) | BIT(3); /* EVSYNC bits */
1594 }
1595 if (disable & (lcd_en | digit_en))
1596 sysc_write(ddata, dispc_offset + 0x40,
1597 val & ~(lcd_en_mask | digit_en_mask));
1598
1599 if (manager_count <= 2)
1600 return irq_mask;
1601
1602 /* DISPC_CONTROL2 */
1603 val = sysc_read(ddata, dispc_offset + 0x238);
1604 lcd2_en = val & lcd_en_mask;
1605 if (lcd2_en)
1606 irq_mask |= BIT(22); /* FRAMEDONE2 */
1607 if (disable && lcd2_en)
1608 sysc_write(ddata, dispc_offset + 0x238,
1609 val & ~lcd_en_mask);
1610
1611 if (manager_count <= 3)
1612 return irq_mask;
1613
1614 /* DISPC_CONTROL3 */
1615 val = sysc_read(ddata, dispc_offset + 0x848);
1616 lcd3_en = val & lcd_en_mask;
1617 if (lcd3_en)
1618 irq_mask |= BIT(30); /* FRAMEDONE3 */
1619 if (disable && lcd3_en)
1620 sysc_write(ddata, dispc_offset + 0x848,
1621 val & ~lcd_en_mask);
1622
1623 return irq_mask;
1624}
1625
1626/* DSS needs child outputs disabled and SDI registers cleared for reset */
1627static void sysc_pre_reset_quirk_dss(struct sysc *ddata)
1628{
1629 const int dispc_offset = 0x1000;
1630 int error;
1631 u32 irq_mask, val;
1632
1633 /* Get enabled outputs */
1634 irq_mask = sysc_quirk_dispc(ddata, dispc_offset, false);
1635 if (!irq_mask)
1636 return;
1637
1638 /* Clear IRQSTATUS */
Tony Lindgren69e60902020-03-03 07:17:43 -08001639 sysc_write(ddata, dispc_offset + 0x18, irq_mask);
Tony Lindgren7324a7a2020-02-24 12:58:03 -08001640
1641 /* Disable outputs */
1642 val = sysc_quirk_dispc(ddata, dispc_offset, true);
1643
1644 /* Poll IRQSTATUS */
1645 error = readl_poll_timeout(ddata->module_va + dispc_offset + 0x18,
1646 val, val != irq_mask, 100, 50);
1647 if (error)
1648 dev_warn(ddata->dev, "%s: timed out %08x !+ %08x\n",
1649 __func__, val, irq_mask);
1650
1651 if (sysc_soc->soc == SOC_3430) {
1652 /* Clear DSS_SDI_CONTROL */
Tony Lindgren69e60902020-03-03 07:17:43 -08001653 sysc_write(ddata, 0x44, 0);
Tony Lindgren7324a7a2020-02-24 12:58:03 -08001654
1655 /* Clear DSS_PLL_CONTROL */
Tony Lindgren69e60902020-03-03 07:17:43 -08001656 sysc_write(ddata, 0x48, 0);
Tony Lindgren7324a7a2020-02-24 12:58:03 -08001657 }
1658
1659 /* Clear DSS_CONTROL to switch DSS clock sources to PRCM if not */
Tony Lindgren69e60902020-03-03 07:17:43 -08001660 sysc_write(ddata, 0x40, 0);
Tony Lindgren7324a7a2020-02-24 12:58:03 -08001661}
1662
Tony Lindgren4e23be42019-06-10 04:48:05 -07001663/* 1-wire needs module's internal clocks enabled for reset */
Tony Lindgrenaec551c2020-02-24 12:58:03 -08001664static void sysc_pre_reset_quirk_hdq1w(struct sysc *ddata)
Tony Lindgren4e23be42019-06-10 04:48:05 -07001665{
1666 int offset = 0x0c; /* HDQ_CTRL_STATUS */
1667 u16 val;
1668
1669 val = sysc_read(ddata, offset);
1670 val |= BIT(5);
1671 sysc_write(ddata, offset, val);
1672}
1673
Tony Lindgren020003f2019-11-14 08:25:28 -08001674/* AESS (Audio Engine SubSystem) needs autogating set after enable */
1675static void sysc_module_enable_quirk_aess(struct sysc *ddata)
1676{
1677 int offset = 0x7c; /* AESS_AUTO_GATING_ENABLE */
1678
1679 sysc_write(ddata, offset, 1);
1680}
1681
Tony Lindgrene64c0212020-02-24 12:58:03 -08001682/* I2C needs to be disabled for reset */
Tony Lindgren4e23be42019-06-10 04:48:05 -07001683static void sysc_clk_quirk_i2c(struct sysc *ddata, bool enable)
1684{
1685 int offset;
1686 u16 val;
1687
1688 /* I2C_CON, omap2/3 is different from omap4 and later */
1689 if ((ddata->revision & 0xffffff00) == 0x001f0000)
1690 offset = 0x24;
1691 else
1692 offset = 0xa4;
1693
1694 /* I2C_EN */
1695 val = sysc_read(ddata, offset);
1696 if (enable)
1697 val |= BIT(15);
1698 else
1699 val &= ~BIT(15);
1700 sysc_write(ddata, offset, val);
1701}
1702
Tony Lindgrene64c0212020-02-24 12:58:03 -08001703static void sysc_pre_reset_quirk_i2c(struct sysc *ddata)
Tony Lindgren4e23be42019-06-10 04:48:05 -07001704{
1705 sysc_clk_quirk_i2c(ddata, false);
1706}
1707
Tony Lindgrene64c0212020-02-24 12:58:03 -08001708static void sysc_post_reset_quirk_i2c(struct sysc *ddata)
1709{
1710 sysc_clk_quirk_i2c(ddata, true);
1711}
1712
Tony Lindgrene8639e12020-02-24 12:58:03 -08001713/* RTC on am3 and 4 needs to be unlocked and locked for sysconfig */
1714static void sysc_quirk_rtc(struct sysc *ddata, bool lock)
1715{
1716 u32 val, kick0_val = 0, kick1_val = 0;
1717 unsigned long flags;
1718 int error;
1719
1720 if (!lock) {
1721 kick0_val = 0x83e70b13;
1722 kick1_val = 0x95a4f1e0;
1723 }
1724
1725 local_irq_save(flags);
1726 /* RTC_STATUS BUSY bit may stay active for 1/32768 seconds (~30 usec) */
1727 error = readl_poll_timeout(ddata->module_va + 0x44, val,
1728 !(val & BIT(0)), 100, 50);
1729 if (error)
1730 dev_warn(ddata->dev, "rtc busy timeout\n");
1731 /* Now we have ~15 microseconds to read/write various registers */
1732 sysc_write(ddata, 0x6c, kick0_val);
1733 sysc_write(ddata, 0x70, kick1_val);
1734 local_irq_restore(flags);
1735}
1736
1737static void sysc_module_unlock_quirk_rtc(struct sysc *ddata)
1738{
1739 sysc_quirk_rtc(ddata, false);
1740}
1741
1742static void sysc_module_lock_quirk_rtc(struct sysc *ddata)
1743{
1744 sysc_quirk_rtc(ddata, true);
1745}
1746
Tony Lindgrend7f563d2019-08-14 05:18:16 -07001747/* 36xx SGX needs a quirk for to bypass OCP IPG interrupt logic */
1748static void sysc_module_enable_quirk_sgx(struct sysc *ddata)
1749{
1750 int offset = 0xff08; /* OCP_DEBUG_CONFIG */
1751 u32 val = BIT(31); /* THALIA_INT_BYPASS */
1752
1753 sysc_write(ddata, offset, val);
1754}
1755
Tony Lindgren4e23be42019-06-10 04:48:05 -07001756/* Watchdog timer needs a disable sequence after reset */
1757static void sysc_reset_done_quirk_wdt(struct sysc *ddata)
1758{
1759 int wps, spr, error;
1760 u32 val;
1761
1762 wps = 0x34;
1763 spr = 0x48;
1764
1765 sysc_write(ddata, spr, 0xaaaa);
1766 error = readl_poll_timeout(ddata->module_va + wps, val,
1767 !(val & 0x10), 100,
1768 MAX_MODULE_SOFTRESET_WAIT);
1769 if (error)
Tony Lindgrenc7d86692019-10-17 11:21:44 -07001770 dev_warn(ddata->dev, "wdt disable step1 failed\n");
Tony Lindgren4e23be42019-06-10 04:48:05 -07001771
Tony Lindgrenc7d86692019-10-17 11:21:44 -07001772 sysc_write(ddata, spr, 0x5555);
Tony Lindgren4e23be42019-06-10 04:48:05 -07001773 error = readl_poll_timeout(ddata->module_va + wps, val,
1774 !(val & 0x10), 100,
1775 MAX_MODULE_SOFTRESET_WAIT);
1776 if (error)
Tony Lindgrenc7d86692019-10-17 11:21:44 -07001777 dev_warn(ddata->dev, "wdt disable step2 failed\n");
Tony Lindgren4e23be42019-06-10 04:48:05 -07001778}
1779
Suman Annab2745d92020-02-27 16:28:34 -06001780/* PRUSS needs to set MSTANDBY_INIT inorder to idle properly */
1781static void sysc_module_disable_quirk_pruss(struct sysc *ddata)
1782{
1783 u32 reg;
1784
1785 reg = sysc_read(ddata, ddata->offsets[SYSC_SYSCONFIG]);
1786 reg |= SYSC_PRUSS_STANDBY_INIT;
1787 sysc_write(ddata, ddata->offsets[SYSC_SYSCONFIG], reg);
1788}
1789
Tony Lindgren4e23be42019-06-10 04:48:05 -07001790static void sysc_init_module_quirks(struct sysc *ddata)
1791{
1792 if (ddata->legacy_mode || !ddata->name)
1793 return;
1794
1795 if (ddata->cfg.quirks & SYSC_MODULE_QUIRK_HDQ1W) {
Tony Lindgrene64c0212020-02-24 12:58:03 -08001796 ddata->pre_reset_quirk = sysc_pre_reset_quirk_hdq1w;
Tony Lindgren4e23be42019-06-10 04:48:05 -07001797
1798 return;
1799 }
1800
1801 if (ddata->cfg.quirks & SYSC_MODULE_QUIRK_I2C) {
Tony Lindgrene64c0212020-02-24 12:58:03 -08001802 ddata->pre_reset_quirk = sysc_pre_reset_quirk_i2c;
1803 ddata->post_reset_quirk = sysc_post_reset_quirk_i2c;
Tony Lindgren4e23be42019-06-10 04:48:05 -07001804
1805 return;
1806 }
1807
Tony Lindgren020003f2019-11-14 08:25:28 -08001808 if (ddata->cfg.quirks & SYSC_MODULE_QUIRK_AESS)
1809 ddata->module_enable_quirk = sysc_module_enable_quirk_aess;
1810
Tony Lindgren7324a7a2020-02-24 12:58:03 -08001811 if (ddata->cfg.quirks & SYSC_MODULE_QUIRK_DSS_RESET)
1812 ddata->pre_reset_quirk = sysc_pre_reset_quirk_dss;
1813
Tony Lindgrene8639e12020-02-24 12:58:03 -08001814 if (ddata->cfg.quirks & SYSC_MODULE_QUIRK_RTC_UNLOCK) {
1815 ddata->module_unlock_quirk = sysc_module_unlock_quirk_rtc;
1816 ddata->module_lock_quirk = sysc_module_lock_quirk_rtc;
1817
1818 return;
1819 }
1820
Tony Lindgrend7f563d2019-08-14 05:18:16 -07001821 if (ddata->cfg.quirks & SYSC_MODULE_QUIRK_SGX)
1822 ddata->module_enable_quirk = sysc_module_enable_quirk_sgx;
1823
Tony Lindgrenc7d86692019-10-17 11:21:44 -07001824 if (ddata->cfg.quirks & SYSC_MODULE_QUIRK_WDT) {
Tony Lindgren4e23be42019-06-10 04:48:05 -07001825 ddata->reset_done_quirk = sysc_reset_done_quirk_wdt;
Tony Lindgrenc7d86692019-10-17 11:21:44 -07001826 ddata->module_disable_quirk = sysc_reset_done_quirk_wdt;
1827 }
Suman Annab2745d92020-02-27 16:28:34 -06001828
1829 if (ddata->cfg.quirks & SYSC_MODULE_QUIRK_PRUSS)
1830 ddata->module_disable_quirk = sysc_module_disable_quirk_pruss;
Tony Lindgren4e23be42019-06-10 04:48:05 -07001831}
1832
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001833static int sysc_clockdomain_init(struct sysc *ddata)
1834{
1835 struct ti_sysc_platform_data *pdata = dev_get_platdata(ddata->dev);
1836 struct clk *fck = NULL, *ick = NULL;
1837 int error;
1838
1839 if (!pdata || !pdata->init_clockdomain)
1840 return 0;
1841
1842 switch (ddata->nr_clocks) {
1843 case 2:
1844 ick = ddata->clocks[SYSC_ICK];
1845 /* fallthrough */
1846 case 1:
1847 fck = ddata->clocks[SYSC_FCK];
1848 break;
1849 case 0:
1850 return 0;
1851 }
1852
1853 error = pdata->init_clockdomain(ddata->dev, fck, ick, &ddata->cookie);
1854 if (!error || error == -ENODEV)
1855 return 0;
1856
1857 return error;
1858}
1859
Tony Lindgrena3e92e72019-03-21 11:00:21 -07001860/*
1861 * Note that pdata->init_module() typically does a reset first. After
1862 * pdata->init_module() is done, PM runtime can be used for the interconnect
1863 * target module.
1864 */
1865static int sysc_legacy_init(struct sysc *ddata)
1866{
1867 struct ti_sysc_platform_data *pdata = dev_get_platdata(ddata->dev);
1868 int error;
1869
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001870 if (!pdata || !pdata->init_module)
Tony Lindgrena3e92e72019-03-21 11:00:21 -07001871 return 0;
1872
1873 error = pdata->init_module(ddata->dev, ddata->mdata, &ddata->cookie);
1874 if (error == -EEXIST)
1875 error = 0;
1876
1877 return error;
1878}
1879
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001880/*
1881 * Note that the caller must ensure the interconnect target module is enabled
1882 * before calling reset. Otherwise reset will not complete.
1883 */
Faiz Abbas596e7952018-07-09 22:18:39 +05301884static int sysc_reset(struct sysc *ddata)
1885{
Tony Lindgrend46f9fb2020-05-31 12:37:54 -07001886 int sysc_offset, sysc_val, error;
1887 u32 sysc_mask;
Faiz Abbas596e7952018-07-09 22:18:39 +05301888
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001889 sysc_offset = ddata->offsets[SYSC_SYSCONFIG];
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001890
Tony Lindgrenab4d309d2020-02-24 12:58:03 -08001891 if (ddata->legacy_mode ||
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001892 ddata->cap->regbits->srst_shift < 0 ||
Faiz Abbas596e7952018-07-09 22:18:39 +05301893 ddata->cfg.quirks & SYSC_QUIRK_NO_RESET_ON_INIT)
1894 return 0;
1895
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001896 sysc_mask = BIT(ddata->cap->regbits->srst_shift);
Faiz Abbas596e7952018-07-09 22:18:39 +05301897
Tony Lindgrene64c0212020-02-24 12:58:03 -08001898 if (ddata->pre_reset_quirk)
1899 ddata->pre_reset_quirk(ddata);
Tony Lindgren4e23be42019-06-10 04:48:05 -07001900
Tony Lindgrenab4d309d2020-02-24 12:58:03 -08001901 if (sysc_offset >= 0) {
1902 sysc_val = sysc_read_sysconfig(ddata);
1903 sysc_val |= sysc_mask;
1904 sysc_write(ddata, sysc_offset, sysc_val);
1905 }
Faiz Abbas596e7952018-07-09 22:18:39 +05301906
Tony Lindgrene709ed72019-12-12 08:20:10 -08001907 if (ddata->cfg.srst_udelay)
1908 usleep_range(ddata->cfg.srst_udelay,
1909 ddata->cfg.srst_udelay * 2);
1910
Tony Lindgrene64c0212020-02-24 12:58:03 -08001911 if (ddata->post_reset_quirk)
1912 ddata->post_reset_quirk(ddata);
Tony Lindgren4e23be42019-06-10 04:48:05 -07001913
Tony Lindgrend46f9fb2020-05-31 12:37:54 -07001914 error = sysc_wait_softreset(ddata);
1915 if (error)
1916 dev_warn(ddata->dev, "OCP softreset timed out\n");
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001917
Tony Lindgren4e23be42019-06-10 04:48:05 -07001918 if (ddata->reset_done_quirk)
1919 ddata->reset_done_quirk(ddata);
1920
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001921 return error;
Faiz Abbas596e7952018-07-09 22:18:39 +05301922}
1923
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001924/*
1925 * At this point the module is configured enough to read the revision but
1926 * module may not be completely configured yet to use PM runtime. Enable
1927 * all clocks directly during init to configure the quirks needed for PM
1928 * runtime based on the revision register.
1929 */
Tony Lindgren566a9b052017-12-15 09:41:19 -08001930static int sysc_init_module(struct sysc *ddata)
1931{
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001932 int error = 0;
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001933
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001934 error = sysc_clockdomain_init(ddata);
1935 if (error)
1936 return error;
1937
Tony Lindgrend0989132019-09-05 07:37:22 -07001938 sysc_clkdm_deny_idle(ddata);
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001939
Tony Lindgrend0989132019-09-05 07:37:22 -07001940 /*
1941 * Always enable clocks. The bootloader may or may not have enabled
1942 * the related clocks.
1943 */
1944 error = sysc_enable_opt_clocks(ddata);
1945 if (error)
1946 return error;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001947
Tony Lindgrend0989132019-09-05 07:37:22 -07001948 error = sysc_enable_main_clocks(ddata);
1949 if (error)
1950 goto err_opt_clocks;
Faiz Abbas596e7952018-07-09 22:18:39 +05301951
Tony Lindgrenea5a2e42019-05-27 04:51:56 -07001952 if (!(ddata->cfg.quirks & SYSC_QUIRK_NO_RESET_ON_INIT)) {
Tero Kristodf4f3452019-10-07 15:29:30 +03001953 error = reset_control_deassert(ddata->rsts);
Tony Lindgrenea5a2e42019-05-27 04:51:56 -07001954 if (error)
1955 goto err_main_clocks;
1956 }
1957
Tony Lindgren566a9b052017-12-15 09:41:19 -08001958 ddata->revision = sysc_read_revision(ddata);
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001959 sysc_init_revision_quirks(ddata);
Tony Lindgren4e23be42019-06-10 04:48:05 -07001960 sysc_init_module_quirks(ddata);
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001961
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001962 if (ddata->legacy_mode) {
1963 error = sysc_legacy_init(ddata);
1964 if (error)
Tero Kristocdc56c12019-10-07 15:29:31 +03001965 goto err_reset;
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001966 }
1967
Tony Lindgrend0989132019-09-05 07:37:22 -07001968 if (!ddata->legacy_mode) {
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001969 error = sysc_enable_module(ddata->dev);
1970 if (error)
Tero Kristocdc56c12019-10-07 15:29:31 +03001971 goto err_reset;
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001972 }
Tony Lindgrena3e92e72019-03-21 11:00:21 -07001973
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001974 error = sysc_reset(ddata);
1975 if (error)
1976 dev_err(ddata->dev, "Reset failed with %d\n", error);
1977
Tero Kristocdc56c12019-10-07 15:29:31 +03001978 if (error && !ddata->legacy_mode)
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001979 sysc_disable_module(ddata->dev);
1980
Tero Kristocdc56c12019-10-07 15:29:31 +03001981err_reset:
1982 if (error && !(ddata->cfg.quirks & SYSC_QUIRK_NO_RESET_ON_INIT))
1983 reset_control_assert(ddata->rsts);
1984
Tony Lindgrena3e92e72019-03-21 11:00:21 -07001985err_main_clocks:
Tero Kristocdc56c12019-10-07 15:29:31 +03001986 if (error)
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001987 sysc_disable_main_clocks(ddata);
1988err_opt_clocks:
Tony Lindgrend0989132019-09-05 07:37:22 -07001989 /* No re-enable of clockdomain autoidle to prevent module autoidle */
Tero Kristocdc56c12019-10-07 15:29:31 +03001990 if (error) {
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001991 sysc_disable_opt_clocks(ddata);
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001992 sysc_clkdm_allow_idle(ddata);
1993 }
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001994
1995 return error;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001996}
1997
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08001998static int sysc_init_sysc_mask(struct sysc *ddata)
1999{
2000 struct device_node *np = ddata->dev->of_node;
2001 int error;
2002 u32 val;
2003
2004 error = of_property_read_u32(np, "ti,sysc-mask", &val);
2005 if (error)
2006 return 0;
2007
Tony Lindgrene212abd2019-07-22 03:44:52 -07002008 ddata->cfg.sysc_val = val & ddata->cap->sysc_mask;
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08002009
2010 return 0;
2011}
2012
2013static int sysc_init_idlemode(struct sysc *ddata, u8 *idlemodes,
2014 const char *name)
2015{
2016 struct device_node *np = ddata->dev->of_node;
2017 struct property *prop;
2018 const __be32 *p;
2019 u32 val;
2020
2021 of_property_for_each_u32(np, name, prop, p, val) {
2022 if (val >= SYSC_NR_IDLEMODES) {
2023 dev_err(ddata->dev, "invalid idlemode: %i\n", val);
2024 return -EINVAL;
2025 }
2026 *idlemodes |= (1 << val);
2027 }
2028
2029 return 0;
2030}
2031
2032static int sysc_init_idlemodes(struct sysc *ddata)
2033{
2034 int error;
2035
2036 error = sysc_init_idlemode(ddata, &ddata->cfg.midlemodes,
2037 "ti,sysc-midle");
2038 if (error)
2039 return error;
2040
2041 error = sysc_init_idlemode(ddata, &ddata->cfg.sidlemodes,
2042 "ti,sysc-sidle");
2043 if (error)
2044 return error;
2045
2046 return 0;
2047}
2048
2049/*
2050 * Only some devices on omap4 and later have SYSCONFIG reset done
2051 * bit. We can detect this if there is no SYSSTATUS at all, or the
2052 * SYSTATUS bit 0 is not used. Note that some SYSSTATUS registers
2053 * have multiple bits for the child devices like OHCI and EHCI.
2054 * Depends on SYSC being parsed first.
2055 */
2056static int sysc_init_syss_mask(struct sysc *ddata)
2057{
2058 struct device_node *np = ddata->dev->of_node;
2059 int error;
2060 u32 val;
2061
2062 error = of_property_read_u32(np, "ti,syss-mask", &val);
2063 if (error) {
2064 if ((ddata->cap->type == TI_SYSC_OMAP4 ||
2065 ddata->cap->type == TI_SYSC_OMAP4_TIMER) &&
2066 (ddata->cfg.sysc_val & SYSC_OMAP4_SOFTRESET))
2067 ddata->cfg.quirks |= SYSC_QUIRK_RESET_STATUS;
2068
2069 return 0;
2070 }
2071
2072 if (!(val & 1) && (ddata->cfg.sysc_val & SYSC_OMAP4_SOFTRESET))
2073 ddata->cfg.quirks |= SYSC_QUIRK_RESET_STATUS;
2074
2075 ddata->cfg.syss_mask = val;
2076
2077 return 0;
2078}
2079
Tony Lindgren2c355ff2018-02-22 13:58:03 -08002080/*
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07002081 * Many child device drivers need to have fck and opt clocks available
2082 * to get the clock rate for device internal configuration etc.
Tony Lindgren2c355ff2018-02-22 13:58:03 -08002083 */
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07002084static int sysc_child_add_named_clock(struct sysc *ddata,
2085 struct device *child,
2086 const char *name)
Tony Lindgren2c355ff2018-02-22 13:58:03 -08002087{
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07002088 struct clk *clk;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08002089 struct clk_lookup *l;
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07002090 int error = 0;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08002091
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07002092 if (!name)
Tony Lindgren2c355ff2018-02-22 13:58:03 -08002093 return 0;
2094
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07002095 clk = clk_get(child, name);
2096 if (!IS_ERR(clk)) {
Markus Elfringcb6cfe22019-11-06 19:12:30 +01002097 error = -EEXIST;
2098 goto put_clk;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08002099 }
2100
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07002101 clk = clk_get(ddata->dev, name);
2102 if (IS_ERR(clk))
2103 return -ENODEV;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08002104
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07002105 l = clkdev_create(clk, name, dev_name(child));
2106 if (!l)
2107 error = -ENOMEM;
Markus Elfringcb6cfe22019-11-06 19:12:30 +01002108put_clk:
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07002109 clk_put(clk);
2110
2111 return error;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08002112}
2113
Tony Lindgren09dfe582018-04-16 10:25:52 -07002114static int sysc_child_add_clocks(struct sysc *ddata,
2115 struct device *child)
2116{
2117 int i, error;
2118
2119 for (i = 0; i < ddata->nr_clocks; i++) {
2120 error = sysc_child_add_named_clock(ddata,
2121 child,
2122 ddata->clock_roles[i]);
2123 if (error && error != -EEXIST) {
2124 dev_err(ddata->dev, "could not add child clock %s: %i\n",
2125 ddata->clock_roles[i], error);
2126
2127 return error;
2128 }
2129 }
2130
2131 return 0;
2132}
2133
Tony Lindgren2c355ff2018-02-22 13:58:03 -08002134static struct device_type sysc_device_type = {
2135};
2136
2137static struct sysc *sysc_child_to_parent(struct device *dev)
2138{
2139 struct device *parent = dev->parent;
2140
2141 if (!parent || parent->type != &sysc_device_type)
2142 return NULL;
2143
2144 return dev_get_drvdata(parent);
2145}
2146
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002147static int __maybe_unused sysc_child_runtime_suspend(struct device *dev)
2148{
2149 struct sysc *ddata;
2150 int error;
2151
2152 ddata = sysc_child_to_parent(dev);
2153
2154 error = pm_generic_runtime_suspend(dev);
2155 if (error)
2156 return error;
2157
2158 if (!ddata->enabled)
2159 return 0;
2160
2161 return sysc_runtime_suspend(ddata->dev);
2162}
2163
2164static int __maybe_unused sysc_child_runtime_resume(struct device *dev)
2165{
2166 struct sysc *ddata;
2167 int error;
2168
2169 ddata = sysc_child_to_parent(dev);
2170
2171 if (!ddata->enabled) {
2172 error = sysc_runtime_resume(ddata->dev);
2173 if (error < 0)
2174 dev_err(ddata->dev,
2175 "%s error: %i\n", __func__, error);
2176 }
2177
2178 return pm_generic_runtime_resume(dev);
2179}
2180
2181#ifdef CONFIG_PM_SLEEP
2182static int sysc_child_suspend_noirq(struct device *dev)
2183{
2184 struct sysc *ddata;
2185 int error;
2186
2187 ddata = sysc_child_to_parent(dev);
2188
Tony Lindgrenef55f822018-04-16 10:27:15 -07002189 dev_dbg(ddata->dev, "%s %s\n", __func__,
2190 ddata->name ? ddata->name : "");
2191
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002192 error = pm_generic_suspend_noirq(dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07002193 if (error) {
2194 dev_err(dev, "%s error at %i: %i\n",
2195 __func__, __LINE__, error);
2196
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002197 return error;
Tony Lindgrenef55f822018-04-16 10:27:15 -07002198 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002199
2200 if (!pm_runtime_status_suspended(dev)) {
2201 error = pm_generic_runtime_suspend(dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07002202 if (error) {
Tony Lindgrenf9490782018-09-28 15:21:50 -07002203 dev_dbg(dev, "%s busy at %i: %i\n",
2204 __func__, __LINE__, error);
Tony Lindgrenef55f822018-04-16 10:27:15 -07002205
Tony Lindgren4f3530f2018-08-08 01:07:06 -07002206 return 0;
Tony Lindgrenef55f822018-04-16 10:27:15 -07002207 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002208
2209 error = sysc_runtime_suspend(ddata->dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07002210 if (error) {
2211 dev_err(dev, "%s error at %i: %i\n",
2212 __func__, __LINE__, error);
2213
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002214 return error;
Tony Lindgrenef55f822018-04-16 10:27:15 -07002215 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002216
2217 ddata->child_needs_resume = true;
2218 }
2219
2220 return 0;
2221}
2222
2223static int sysc_child_resume_noirq(struct device *dev)
2224{
2225 struct sysc *ddata;
2226 int error;
2227
2228 ddata = sysc_child_to_parent(dev);
2229
Tony Lindgrenef55f822018-04-16 10:27:15 -07002230 dev_dbg(ddata->dev, "%s %s\n", __func__,
2231 ddata->name ? ddata->name : "");
2232
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002233 if (ddata->child_needs_resume) {
2234 ddata->child_needs_resume = false;
2235
2236 error = sysc_runtime_resume(ddata->dev);
2237 if (error)
2238 dev_err(ddata->dev,
2239 "%s runtime resume error: %i\n",
2240 __func__, error);
2241
2242 error = pm_generic_runtime_resume(dev);
2243 if (error)
2244 dev_err(ddata->dev,
2245 "%s generic runtime resume: %i\n",
2246 __func__, error);
2247 }
2248
2249 return pm_generic_resume_noirq(dev);
2250}
2251#endif
2252
Tony Lindgrenb7182b42019-03-21 11:00:21 -07002253static struct dev_pm_domain sysc_child_pm_domain = {
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002254 .ops = {
2255 SET_RUNTIME_PM_OPS(sysc_child_runtime_suspend,
2256 sysc_child_runtime_resume,
2257 NULL)
2258 USE_PLATFORM_PM_SLEEP_OPS
2259 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(sysc_child_suspend_noirq,
2260 sysc_child_resume_noirq)
2261 }
2262};
2263
2264/**
2265 * sysc_legacy_idle_quirk - handle children in omap_device compatible way
2266 * @ddata: device driver data
2267 * @child: child device driver
2268 *
2269 * Allow idle for child devices as done with _od_runtime_suspend().
2270 * Otherwise many child devices will not idle because of the permanent
2271 * parent usecount set in pm_runtime_irq_safe().
2272 *
2273 * Note that the long term solution is to just modify the child device
2274 * drivers to not set pm_runtime_irq_safe() and then this can be just
2275 * dropped.
2276 */
2277static void sysc_legacy_idle_quirk(struct sysc *ddata, struct device *child)
2278{
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002279 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
2280 dev_pm_domain_set(child, &sysc_child_pm_domain);
2281}
2282
Tony Lindgren2c355ff2018-02-22 13:58:03 -08002283static int sysc_notifier_call(struct notifier_block *nb,
2284 unsigned long event, void *device)
2285{
2286 struct device *dev = device;
2287 struct sysc *ddata;
2288 int error;
2289
2290 ddata = sysc_child_to_parent(dev);
2291 if (!ddata)
2292 return NOTIFY_DONE;
2293
2294 switch (event) {
2295 case BUS_NOTIFY_ADD_DEVICE:
Tony Lindgren09dfe582018-04-16 10:25:52 -07002296 error = sysc_child_add_clocks(ddata, dev);
2297 if (error)
2298 return error;
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002299 sysc_legacy_idle_quirk(ddata, dev);
Tony Lindgren2c355ff2018-02-22 13:58:03 -08002300 break;
2301 default:
2302 break;
2303 }
2304
2305 return NOTIFY_DONE;
2306}
2307
2308static struct notifier_block sysc_nb = {
2309 .notifier_call = sysc_notifier_call,
2310};
2311
Tony Lindgren566a9b052017-12-15 09:41:19 -08002312/* Device tree configured quirks */
2313struct sysc_dts_quirk {
2314 const char *name;
2315 u32 mask;
2316};
2317
2318static const struct sysc_dts_quirk sysc_dts_quirks[] = {
2319 { .name = "ti,no-idle-on-init",
2320 .mask = SYSC_QUIRK_NO_IDLE_ON_INIT, },
2321 { .name = "ti,no-reset-on-init",
2322 .mask = SYSC_QUIRK_NO_RESET_ON_INIT, },
Tony Lindgren386cb762019-03-22 07:49:30 -07002323 { .name = "ti,no-idle",
2324 .mask = SYSC_QUIRK_NO_IDLE, },
Tony Lindgren566a9b052017-12-15 09:41:19 -08002325};
2326
Tony Lindgren4014c082018-12-10 14:11:26 -08002327static void sysc_parse_dts_quirks(struct sysc *ddata, struct device_node *np,
2328 bool is_child)
Tony Lindgren566a9b052017-12-15 09:41:19 -08002329{
Tony Lindgren566a9b052017-12-15 09:41:19 -08002330 const struct property *prop;
Tony Lindgren4014c082018-12-10 14:11:26 -08002331 int i, len;
Tony Lindgren566a9b052017-12-15 09:41:19 -08002332
2333 for (i = 0; i < ARRAY_SIZE(sysc_dts_quirks); i++) {
Tony Lindgren4014c082018-12-10 14:11:26 -08002334 const char *name = sysc_dts_quirks[i].name;
2335
2336 prop = of_get_property(np, name, &len);
Tony Lindgren566a9b052017-12-15 09:41:19 -08002337 if (!prop)
Tony Lindgrend39b6ea2018-02-15 09:18:55 -08002338 continue;
Tony Lindgren566a9b052017-12-15 09:41:19 -08002339
2340 ddata->cfg.quirks |= sysc_dts_quirks[i].mask;
Tony Lindgren4014c082018-12-10 14:11:26 -08002341 if (is_child) {
2342 dev_warn(ddata->dev,
2343 "dts flag should be at module level for %s\n",
2344 name);
2345 }
Tony Lindgren566a9b052017-12-15 09:41:19 -08002346 }
Tony Lindgren4014c082018-12-10 14:11:26 -08002347}
Tony Lindgren566a9b052017-12-15 09:41:19 -08002348
Tony Lindgren4014c082018-12-10 14:11:26 -08002349static int sysc_init_dts_quirks(struct sysc *ddata)
2350{
2351 struct device_node *np = ddata->dev->of_node;
2352 int error;
2353 u32 val;
2354
2355 ddata->legacy_mode = of_get_property(np, "ti,hwmods", NULL);
2356
2357 sysc_parse_dts_quirks(ddata, np, false);
Tony Lindgren566a9b052017-12-15 09:41:19 -08002358 error = of_property_read_u32(np, "ti,sysc-delay-us", &val);
2359 if (!error) {
2360 if (val > 255) {
2361 dev_warn(ddata->dev, "bad ti,sysc-delay-us: %i\n",
2362 val);
2363 }
2364
2365 ddata->cfg.srst_udelay = (u8)val;
2366 }
2367
2368 return 0;
2369}
2370
Tony Lindgren0eecc632017-10-10 14:23:43 -07002371static void sysc_unprepare(struct sysc *ddata)
2372{
2373 int i;
2374
Tony Lindgrenaaa29bb2019-03-21 11:00:21 -07002375 if (!ddata->clocks)
2376 return;
2377
Tony Lindgren0eecc632017-10-10 14:23:43 -07002378 for (i = 0; i < SYSC_MAX_CLOCKS; i++) {
2379 if (!IS_ERR_OR_NULL(ddata->clocks[i]))
2380 clk_unprepare(ddata->clocks[i]);
2381 }
2382}
2383
Tony Lindgren70a65242017-12-15 09:41:09 -08002384/*
2385 * Common sysc register bits found on omap2, also known as type1
2386 */
2387static const struct sysc_regbits sysc_regbits_omap2 = {
2388 .dmadisable_shift = -ENODEV,
2389 .midle_shift = 12,
2390 .sidle_shift = 3,
2391 .clkact_shift = 8,
2392 .emufree_shift = 5,
2393 .enwkup_shift = 2,
2394 .srst_shift = 1,
2395 .autoidle_shift = 0,
2396};
2397
2398static const struct sysc_capabilities sysc_omap2 = {
2399 .type = TI_SYSC_OMAP2,
2400 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY | SYSC_OMAP2_EMUFREE |
2401 SYSC_OMAP2_ENAWAKEUP | SYSC_OMAP2_SOFTRESET |
2402 SYSC_OMAP2_AUTOIDLE,
2403 .regbits = &sysc_regbits_omap2,
2404};
2405
2406/* All omap2 and 3 timers, and timers 1, 2 & 10 on omap 4 and 5 */
2407static const struct sysc_capabilities sysc_omap2_timer = {
2408 .type = TI_SYSC_OMAP2_TIMER,
2409 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY | SYSC_OMAP2_EMUFREE |
2410 SYSC_OMAP2_ENAWAKEUP | SYSC_OMAP2_SOFTRESET |
2411 SYSC_OMAP2_AUTOIDLE,
2412 .regbits = &sysc_regbits_omap2,
2413 .mod_quirks = SYSC_QUIRK_USE_CLOCKACT,
2414};
2415
2416/*
2417 * SHAM2 (SHA1/MD5) sysc found on omap3, a variant of sysc_regbits_omap2
2418 * with different sidle position
2419 */
2420static const struct sysc_regbits sysc_regbits_omap3_sham = {
2421 .dmadisable_shift = -ENODEV,
2422 .midle_shift = -ENODEV,
2423 .sidle_shift = 4,
2424 .clkact_shift = -ENODEV,
2425 .enwkup_shift = -ENODEV,
2426 .srst_shift = 1,
2427 .autoidle_shift = 0,
2428 .emufree_shift = -ENODEV,
2429};
2430
2431static const struct sysc_capabilities sysc_omap3_sham = {
2432 .type = TI_SYSC_OMAP3_SHAM,
2433 .sysc_mask = SYSC_OMAP2_SOFTRESET | SYSC_OMAP2_AUTOIDLE,
2434 .regbits = &sysc_regbits_omap3_sham,
2435};
2436
2437/*
2438 * AES register bits found on omap3 and later, a variant of
2439 * sysc_regbits_omap2 with different sidle position
2440 */
2441static const struct sysc_regbits sysc_regbits_omap3_aes = {
2442 .dmadisable_shift = -ENODEV,
2443 .midle_shift = -ENODEV,
2444 .sidle_shift = 6,
2445 .clkact_shift = -ENODEV,
2446 .enwkup_shift = -ENODEV,
2447 .srst_shift = 1,
2448 .autoidle_shift = 0,
2449 .emufree_shift = -ENODEV,
2450};
2451
2452static const struct sysc_capabilities sysc_omap3_aes = {
2453 .type = TI_SYSC_OMAP3_AES,
2454 .sysc_mask = SYSC_OMAP2_SOFTRESET | SYSC_OMAP2_AUTOIDLE,
2455 .regbits = &sysc_regbits_omap3_aes,
2456};
2457
2458/*
2459 * Common sysc register bits found on omap4, also known as type2
2460 */
2461static const struct sysc_regbits sysc_regbits_omap4 = {
2462 .dmadisable_shift = 16,
2463 .midle_shift = 4,
2464 .sidle_shift = 2,
2465 .clkact_shift = -ENODEV,
2466 .enwkup_shift = -ENODEV,
2467 .emufree_shift = 1,
2468 .srst_shift = 0,
2469 .autoidle_shift = -ENODEV,
2470};
2471
2472static const struct sysc_capabilities sysc_omap4 = {
2473 .type = TI_SYSC_OMAP4,
2474 .sysc_mask = SYSC_OMAP4_DMADISABLE | SYSC_OMAP4_FREEEMU |
2475 SYSC_OMAP4_SOFTRESET,
2476 .regbits = &sysc_regbits_omap4,
2477};
2478
2479static const struct sysc_capabilities sysc_omap4_timer = {
2480 .type = TI_SYSC_OMAP4_TIMER,
2481 .sysc_mask = SYSC_OMAP4_DMADISABLE | SYSC_OMAP4_FREEEMU |
2482 SYSC_OMAP4_SOFTRESET,
2483 .regbits = &sysc_regbits_omap4,
2484};
2485
2486/*
2487 * Common sysc register bits found on omap4, also known as type3
2488 */
2489static const struct sysc_regbits sysc_regbits_omap4_simple = {
2490 .dmadisable_shift = -ENODEV,
2491 .midle_shift = 2,
2492 .sidle_shift = 0,
2493 .clkact_shift = -ENODEV,
2494 .enwkup_shift = -ENODEV,
2495 .srst_shift = -ENODEV,
2496 .emufree_shift = -ENODEV,
2497 .autoidle_shift = -ENODEV,
2498};
2499
2500static const struct sysc_capabilities sysc_omap4_simple = {
2501 .type = TI_SYSC_OMAP4_SIMPLE,
2502 .regbits = &sysc_regbits_omap4_simple,
2503};
2504
2505/*
2506 * SmartReflex sysc found on omap34xx
2507 */
2508static const struct sysc_regbits sysc_regbits_omap34xx_sr = {
2509 .dmadisable_shift = -ENODEV,
2510 .midle_shift = -ENODEV,
2511 .sidle_shift = -ENODEV,
2512 .clkact_shift = 20,
2513 .enwkup_shift = -ENODEV,
2514 .srst_shift = -ENODEV,
2515 .emufree_shift = -ENODEV,
2516 .autoidle_shift = -ENODEV,
2517};
2518
2519static const struct sysc_capabilities sysc_34xx_sr = {
2520 .type = TI_SYSC_OMAP34XX_SR,
2521 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY,
2522 .regbits = &sysc_regbits_omap34xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002523 .mod_quirks = SYSC_QUIRK_USE_CLOCKACT | SYSC_QUIRK_UNCACHED |
2524 SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08002525};
2526
2527/*
2528 * SmartReflex sysc found on omap36xx and later
2529 */
2530static const struct sysc_regbits sysc_regbits_omap36xx_sr = {
2531 .dmadisable_shift = -ENODEV,
2532 .midle_shift = -ENODEV,
2533 .sidle_shift = 24,
2534 .clkact_shift = -ENODEV,
2535 .enwkup_shift = 26,
2536 .srst_shift = -ENODEV,
2537 .emufree_shift = -ENODEV,
2538 .autoidle_shift = -ENODEV,
2539};
2540
2541static const struct sysc_capabilities sysc_36xx_sr = {
2542 .type = TI_SYSC_OMAP36XX_SR,
Tony Lindgren3267c082018-01-22 09:32:53 -08002543 .sysc_mask = SYSC_OMAP3_SR_ENAWAKEUP,
Tony Lindgren70a65242017-12-15 09:41:09 -08002544 .regbits = &sysc_regbits_omap36xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002545 .mod_quirks = SYSC_QUIRK_UNCACHED | SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08002546};
2547
2548static const struct sysc_capabilities sysc_omap4_sr = {
2549 .type = TI_SYSC_OMAP4_SR,
2550 .regbits = &sysc_regbits_omap36xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002551 .mod_quirks = SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08002552};
2553
2554/*
2555 * McASP register bits found on omap4 and later
2556 */
2557static const struct sysc_regbits sysc_regbits_omap4_mcasp = {
2558 .dmadisable_shift = -ENODEV,
2559 .midle_shift = -ENODEV,
2560 .sidle_shift = 0,
2561 .clkact_shift = -ENODEV,
2562 .enwkup_shift = -ENODEV,
2563 .srst_shift = -ENODEV,
2564 .emufree_shift = -ENODEV,
2565 .autoidle_shift = -ENODEV,
2566};
2567
2568static const struct sysc_capabilities sysc_omap4_mcasp = {
2569 .type = TI_SYSC_OMAP4_MCASP,
2570 .regbits = &sysc_regbits_omap4_mcasp,
Tony Lindgren2c63a832018-11-15 14:46:53 -08002571 .mod_quirks = SYSC_QUIRK_OPT_CLKS_NEEDED,
2572};
2573
2574/*
2575 * McASP found on dra7 and later
2576 */
2577static const struct sysc_capabilities sysc_dra7_mcasp = {
2578 .type = TI_SYSC_OMAP4_SIMPLE,
2579 .regbits = &sysc_regbits_omap4_simple,
2580 .mod_quirks = SYSC_QUIRK_OPT_CLKS_NEEDED,
Tony Lindgren70a65242017-12-15 09:41:09 -08002581};
2582
2583/*
2584 * FS USB host found on omap4 and later
2585 */
2586static const struct sysc_regbits sysc_regbits_omap4_usb_host_fs = {
2587 .dmadisable_shift = -ENODEV,
2588 .midle_shift = -ENODEV,
2589 .sidle_shift = 24,
2590 .clkact_shift = -ENODEV,
2591 .enwkup_shift = 26,
2592 .srst_shift = -ENODEV,
2593 .emufree_shift = -ENODEV,
2594 .autoidle_shift = -ENODEV,
2595};
2596
2597static const struct sysc_capabilities sysc_omap4_usb_host_fs = {
2598 .type = TI_SYSC_OMAP4_USB_HOST_FS,
2599 .sysc_mask = SYSC_OMAP2_ENAWAKEUP,
2600 .regbits = &sysc_regbits_omap4_usb_host_fs,
2601};
2602
Faiz Abbas7f35e632018-07-09 22:18:38 +05302603static const struct sysc_regbits sysc_regbits_dra7_mcan = {
2604 .dmadisable_shift = -ENODEV,
2605 .midle_shift = -ENODEV,
2606 .sidle_shift = -ENODEV,
2607 .clkact_shift = -ENODEV,
2608 .enwkup_shift = 4,
2609 .srst_shift = 0,
2610 .emufree_shift = -ENODEV,
2611 .autoidle_shift = -ENODEV,
2612};
2613
2614static const struct sysc_capabilities sysc_dra7_mcan = {
2615 .type = TI_SYSC_DRA7_MCAN,
2616 .sysc_mask = SYSC_DRA7_MCAN_ENAWAKEUP | SYSC_OMAP4_SOFTRESET,
2617 .regbits = &sysc_regbits_dra7_mcan,
Tony Lindgrene0db94f2019-05-27 04:51:53 -07002618 .mod_quirks = SYSS_QUIRK_RESETDONE_INVERTED,
Faiz Abbas7f35e632018-07-09 22:18:38 +05302619};
2620
Suman Annab2745d92020-02-27 16:28:34 -06002621/*
2622 * PRUSS found on some AM33xx, AM437x and AM57xx SoCs
2623 */
2624static const struct sysc_capabilities sysc_pruss = {
2625 .type = TI_SYSC_PRUSS,
2626 .sysc_mask = SYSC_PRUSS_STANDBY_INIT | SYSC_PRUSS_SUB_MWAIT,
2627 .regbits = &sysc_regbits_omap4_simple,
2628 .mod_quirks = SYSC_MODULE_QUIRK_PRUSS,
2629};
2630
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002631static int sysc_init_pdata(struct sysc *ddata)
2632{
2633 struct ti_sysc_platform_data *pdata = dev_get_platdata(ddata->dev);
Tony Lindgrena3e92e72019-03-21 11:00:21 -07002634 struct ti_sysc_module_data *mdata;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002635
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07002636 if (!pdata)
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002637 return 0;
2638
Tony Lindgrena3e92e72019-03-21 11:00:21 -07002639 mdata = devm_kzalloc(ddata->dev, sizeof(*mdata), GFP_KERNEL);
2640 if (!mdata)
2641 return -ENOMEM;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002642
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07002643 if (ddata->legacy_mode) {
2644 mdata->name = ddata->legacy_mode;
2645 mdata->module_pa = ddata->module_pa;
2646 mdata->module_size = ddata->module_size;
2647 mdata->offsets = ddata->offsets;
2648 mdata->nr_offsets = SYSC_MAX_REGS;
2649 mdata->cap = ddata->cap;
2650 mdata->cfg = &ddata->cfg;
2651 }
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002652
Tony Lindgrena3e92e72019-03-21 11:00:21 -07002653 ddata->mdata = mdata;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002654
Tony Lindgrena3e92e72019-03-21 11:00:21 -07002655 return 0;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002656}
2657
Tony Lindgren70a65242017-12-15 09:41:09 -08002658static int sysc_init_match(struct sysc *ddata)
2659{
2660 const struct sysc_capabilities *cap;
2661
2662 cap = of_device_get_match_data(ddata->dev);
2663 if (!cap)
2664 return -EINVAL;
2665
2666 ddata->cap = cap;
2667 if (ddata->cap)
2668 ddata->cfg.quirks |= ddata->cap->mod_quirks;
2669
2670 return 0;
2671}
2672
Tony Lindgren76f0f772018-02-23 08:28:45 -08002673static void ti_sysc_idle(struct work_struct *work)
2674{
2675 struct sysc *ddata;
2676
2677 ddata = container_of(work, struct sysc, idle_work.work);
2678
Tony Lindgrend0989132019-09-05 07:37:22 -07002679 /*
2680 * One time decrement of clock usage counts if left on from init.
2681 * Note that we disable opt clocks unconditionally in this case
2682 * as they are enabled unconditionally during init without
2683 * considering sysc_opt_clks_needed() at that point.
2684 */
2685 if (ddata->cfg.quirks & (SYSC_QUIRK_NO_IDLE |
2686 SYSC_QUIRK_NO_IDLE_ON_INIT)) {
Tony Lindgrend0989132019-09-05 07:37:22 -07002687 sysc_disable_main_clocks(ddata);
2688 sysc_disable_opt_clocks(ddata);
2689 sysc_clkdm_allow_idle(ddata);
2690 }
2691
2692 /* Keep permanent PM runtime usage count for SYSC_QUIRK_NO_IDLE */
2693 if (ddata->cfg.quirks & SYSC_QUIRK_NO_IDLE)
2694 return;
2695
2696 /*
2697 * Decrement PM runtime usage count for SYSC_QUIRK_NO_IDLE_ON_INIT
2698 * and SYSC_QUIRK_NO_RESET_ON_INIT
2699 */
Tony Lindgren76f0f772018-02-23 08:28:45 -08002700 if (pm_runtime_active(ddata->dev))
2701 pm_runtime_put_sync(ddata->dev);
2702}
2703
Tony Lindgrenfeaa8ba2020-02-24 12:58:03 -08002704/*
2705 * SoC model and features detection. Only needed for SoCs that need
2706 * special handling for quirks, no need to list others.
2707 */
2708static const struct soc_device_attribute sysc_soc_match[] = {
2709 SOC_FLAG("OMAP242*", SOC_2420),
2710 SOC_FLAG("OMAP243*", SOC_2430),
2711 SOC_FLAG("OMAP3[45]*", SOC_3430),
2712 SOC_FLAG("OMAP3[67]*", SOC_3630),
2713 SOC_FLAG("OMAP443*", SOC_4430),
2714 SOC_FLAG("OMAP446*", SOC_4460),
2715 SOC_FLAG("OMAP447*", SOC_4470),
2716 SOC_FLAG("OMAP54*", SOC_5430),
2717 SOC_FLAG("AM433", SOC_AM3),
2718 SOC_FLAG("AM43*", SOC_AM4),
2719 SOC_FLAG("DRA7*", SOC_DRA7),
2720
2721 { /* sentinel */ },
2722};
2723
2724/*
2725 * List of SoCs variants with disabled features. By default we assume all
2726 * devices in the device tree are available so no need to list those SoCs.
2727 */
2728static const struct soc_device_attribute sysc_soc_feat_match[] = {
2729 /* OMAP3430/3530 and AM3517 variants with some accelerators disabled */
2730 SOC_FLAG("AM3505", DIS_SGX),
2731 SOC_FLAG("OMAP3525", DIS_SGX),
2732 SOC_FLAG("OMAP3515", DIS_IVA | DIS_SGX),
2733 SOC_FLAG("OMAP3503", DIS_ISP | DIS_IVA | DIS_SGX),
2734
2735 /* OMAP3630/DM3730 variants with some accelerators disabled */
2736 SOC_FLAG("AM3703", DIS_IVA | DIS_SGX),
2737 SOC_FLAG("DM3725", DIS_SGX),
2738 SOC_FLAG("OMAP3611", DIS_ISP | DIS_IVA | DIS_SGX),
2739 SOC_FLAG("OMAP3615/AM3715", DIS_IVA),
2740 SOC_FLAG("OMAP3621", DIS_ISP),
2741
2742 { /* sentinel */ },
2743};
2744
2745static int sysc_add_disabled(unsigned long base)
2746{
2747 struct sysc_address *disabled_module;
2748
2749 disabled_module = kzalloc(sizeof(*disabled_module), GFP_KERNEL);
2750 if (!disabled_module)
2751 return -ENOMEM;
2752
2753 disabled_module->base = base;
2754
2755 mutex_lock(&sysc_soc->list_lock);
2756 list_add(&disabled_module->node, &sysc_soc->disabled_modules);
2757 mutex_unlock(&sysc_soc->list_lock);
2758
2759 return 0;
2760}
2761
2762/*
2763 * One time init to detect the booted SoC and disable unavailable features.
2764 * Note that we initialize static data shared across all ti-sysc instances
2765 * so ddata is only used for SoC type. This can be called from module_init
2766 * once we no longer need to rely on platform data.
2767 */
2768static int sysc_init_soc(struct sysc *ddata)
2769{
2770 const struct soc_device_attribute *match;
2771 struct ti_sysc_platform_data *pdata;
2772 unsigned long features = 0;
2773
2774 if (sysc_soc)
2775 return 0;
2776
2777 sysc_soc = kzalloc(sizeof(*sysc_soc), GFP_KERNEL);
2778 if (!sysc_soc)
2779 return -ENOMEM;
2780
2781 mutex_init(&sysc_soc->list_lock);
2782 INIT_LIST_HEAD(&sysc_soc->disabled_modules);
2783 sysc_soc->general_purpose = true;
2784
2785 pdata = dev_get_platdata(ddata->dev);
2786 if (pdata && pdata->soc_type_gp)
2787 sysc_soc->general_purpose = pdata->soc_type_gp();
2788
2789 match = soc_device_match(sysc_soc_match);
2790 if (match && match->data)
2791 sysc_soc->soc = (int)match->data;
2792
2793 match = soc_device_match(sysc_soc_feat_match);
2794 if (!match)
2795 return 0;
2796
2797 if (match->data)
2798 features = (unsigned long)match->data;
2799
2800 /*
2801 * Add disabled devices to the list based on the module base.
2802 * Note that this must be done before we attempt to access the
2803 * device and have module revision checks working.
2804 */
2805 if (features & DIS_ISP)
2806 sysc_add_disabled(0x480bd400);
2807 if (features & DIS_IVA)
2808 sysc_add_disabled(0x5d000000);
2809 if (features & DIS_SGX)
2810 sysc_add_disabled(0x50000000);
2811
2812 return 0;
2813}
2814
2815static void sysc_cleanup_soc(void)
2816{
2817 struct sysc_address *disabled_module;
2818 struct list_head *pos, *tmp;
2819
2820 if (!sysc_soc)
2821 return;
2822
2823 mutex_lock(&sysc_soc->list_lock);
2824 list_for_each_safe(pos, tmp, &sysc_soc->disabled_modules) {
2825 disabled_module = list_entry(pos, struct sysc_address, node);
2826 list_del(pos);
2827 kfree(disabled_module);
2828 }
2829 mutex_unlock(&sysc_soc->list_lock);
2830}
2831
2832static int sysc_check_disabled_devices(struct sysc *ddata)
2833{
2834 struct sysc_address *disabled_module;
2835 struct list_head *pos;
2836 int error = 0;
2837
2838 mutex_lock(&sysc_soc->list_lock);
2839 list_for_each(pos, &sysc_soc->disabled_modules) {
2840 disabled_module = list_entry(pos, struct sysc_address, node);
2841 if (ddata->module_pa == disabled_module->base) {
2842 dev_dbg(ddata->dev, "module disabled for this SoC\n");
2843 error = -ENODEV;
2844 break;
2845 }
2846 }
2847 mutex_unlock(&sysc_soc->list_lock);
2848
2849 return error;
2850}
2851
Tony Lindgrenc4bebea2018-04-16 10:24:54 -07002852static const struct of_device_id sysc_match_table[] = {
2853 { .compatible = "simple-bus", },
2854 { /* sentinel */ },
2855};
2856
Tony Lindgren0eecc632017-10-10 14:23:43 -07002857static int sysc_probe(struct platform_device *pdev)
2858{
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002859 struct ti_sysc_platform_data *pdata = dev_get_platdata(&pdev->dev);
Tony Lindgren0eecc632017-10-10 14:23:43 -07002860 struct sysc *ddata;
2861 int error;
2862
2863 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
2864 if (!ddata)
2865 return -ENOMEM;
2866
2867 ddata->dev = &pdev->dev;
Tony Lindgren566a9b052017-12-15 09:41:19 -08002868 platform_set_drvdata(pdev, ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -07002869
Tony Lindgrenfeaa8ba2020-02-24 12:58:03 -08002870 error = sysc_init_soc(ddata);
2871 if (error)
2872 return error;
2873
Tony Lindgren70a65242017-12-15 09:41:09 -08002874 error = sysc_init_match(ddata);
2875 if (error)
2876 return error;
2877
Tony Lindgren566a9b052017-12-15 09:41:19 -08002878 error = sysc_init_dts_quirks(ddata);
2879 if (error)
Suman Annaa304f482019-06-25 18:33:15 -05002880 return error;
Tony Lindgren566a9b052017-12-15 09:41:19 -08002881
Tony Lindgren0eecc632017-10-10 14:23:43 -07002882 error = sysc_map_and_check_registers(ddata);
2883 if (error)
Suman Annaa304f482019-06-25 18:33:15 -05002884 return error;
Tony Lindgren0eecc632017-10-10 14:23:43 -07002885
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08002886 error = sysc_init_sysc_mask(ddata);
2887 if (error)
Suman Annaa304f482019-06-25 18:33:15 -05002888 return error;
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08002889
2890 error = sysc_init_idlemodes(ddata);
2891 if (error)
Suman Annaa304f482019-06-25 18:33:15 -05002892 return error;
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08002893
2894 error = sysc_init_syss_mask(ddata);
2895 if (error)
Suman Annaa304f482019-06-25 18:33:15 -05002896 return error;
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08002897
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002898 error = sysc_init_pdata(ddata);
2899 if (error)
Suman Annaa304f482019-06-25 18:33:15 -05002900 return error;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002901
Tony Lindgren42b9c5c2019-03-21 11:00:21 -07002902 sysc_init_early_quirks(ddata);
2903
Tony Lindgrenfeaa8ba2020-02-24 12:58:03 -08002904 error = sysc_check_disabled_devices(ddata);
2905 if (error)
2906 return error;
2907
Tony Lindgren42b9c5c2019-03-21 11:00:21 -07002908 error = sysc_get_clocks(ddata);
2909 if (error)
2910 return error;
2911
Tony Lindgren50622362018-04-16 10:20:27 -07002912 error = sysc_init_resets(ddata);
2913 if (error)
Suman Annaa304f482019-06-25 18:33:15 -05002914 goto unprepare;
Tony Lindgren566a9b052017-12-15 09:41:19 -08002915
2916 error = sysc_init_module(ddata);
2917 if (error)
2918 goto unprepare;
2919
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07002920 pm_runtime_enable(ddata->dev);
Tony Lindgren0eecc632017-10-10 14:23:43 -07002921 error = pm_runtime_get_sync(ddata->dev);
2922 if (error < 0) {
2923 pm_runtime_put_noidle(ddata->dev);
2924 pm_runtime_disable(ddata->dev);
2925 goto unprepare;
2926 }
2927
Tero Kristocdc56c12019-10-07 15:29:31 +03002928 /* Balance use counts as PM runtime should have enabled these all */
2929 if (!(ddata->cfg.quirks & SYSC_QUIRK_NO_RESET_ON_INIT))
Tero Kristobb88b862019-08-07 15:46:05 +03002930 reset_control_assert(ddata->rsts);
2931
Tero Kristocdc56c12019-10-07 15:29:31 +03002932 if (!(ddata->cfg.quirks &
2933 (SYSC_QUIRK_NO_IDLE | SYSC_QUIRK_NO_IDLE_ON_INIT))) {
2934 sysc_disable_main_clocks(ddata);
2935 sysc_disable_opt_clocks(ddata);
2936 sysc_clkdm_allow_idle(ddata);
2937 }
2938
Tony Lindgren0eecc632017-10-10 14:23:43 -07002939 sysc_show_registers(ddata);
2940
Tony Lindgren2c355ff2018-02-22 13:58:03 -08002941 ddata->dev->type = &sysc_device_type;
Tony Lindgrenc4bebea2018-04-16 10:24:54 -07002942 error = of_platform_populate(ddata->dev->of_node, sysc_match_table,
2943 pdata ? pdata->auxdata : NULL,
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002944 ddata->dev);
Tony Lindgren0eecc632017-10-10 14:23:43 -07002945 if (error)
2946 goto err;
2947
Tony Lindgren76f0f772018-02-23 08:28:45 -08002948 INIT_DELAYED_WORK(&ddata->idle_work, ti_sysc_idle);
2949
2950 /* At least earlycon won't survive without deferred idle */
Tony Lindgrend0989132019-09-05 07:37:22 -07002951 if (ddata->cfg.quirks & (SYSC_QUIRK_NO_IDLE |
2952 SYSC_QUIRK_NO_IDLE_ON_INIT |
Tony Lindgren76f0f772018-02-23 08:28:45 -08002953 SYSC_QUIRK_NO_RESET_ON_INIT)) {
2954 schedule_delayed_work(&ddata->idle_work, 3000);
2955 } else {
2956 pm_runtime_put(&pdev->dev);
2957 }
Tony Lindgren0eecc632017-10-10 14:23:43 -07002958
2959 return 0;
2960
2961err:
Tony Lindgren0eecc632017-10-10 14:23:43 -07002962 pm_runtime_put_sync(&pdev->dev);
2963 pm_runtime_disable(&pdev->dev);
2964unprepare:
2965 sysc_unprepare(ddata);
2966
2967 return error;
2968}
2969
Tony Lindgren684be5a2017-10-13 10:48:40 -07002970static int sysc_remove(struct platform_device *pdev)
2971{
2972 struct sysc *ddata = platform_get_drvdata(pdev);
2973 int error;
2974
Tony Lindgren76f0f772018-02-23 08:28:45 -08002975 cancel_delayed_work_sync(&ddata->idle_work);
2976
Tony Lindgren684be5a2017-10-13 10:48:40 -07002977 error = pm_runtime_get_sync(ddata->dev);
2978 if (error < 0) {
2979 pm_runtime_put_noidle(ddata->dev);
2980 pm_runtime_disable(ddata->dev);
2981 goto unprepare;
2982 }
2983
2984 of_platform_depopulate(&pdev->dev);
2985
Tony Lindgren684be5a2017-10-13 10:48:40 -07002986 pm_runtime_put_sync(&pdev->dev);
2987 pm_runtime_disable(&pdev->dev);
Tony Lindgren50622362018-04-16 10:20:27 -07002988 reset_control_assert(ddata->rsts);
Tony Lindgren684be5a2017-10-13 10:48:40 -07002989
2990unprepare:
2991 sysc_unprepare(ddata);
2992
2993 return 0;
2994}
2995
Tony Lindgren0eecc632017-10-10 14:23:43 -07002996static const struct of_device_id sysc_match[] = {
Tony Lindgren70a65242017-12-15 09:41:09 -08002997 { .compatible = "ti,sysc-omap2", .data = &sysc_omap2, },
2998 { .compatible = "ti,sysc-omap2-timer", .data = &sysc_omap2_timer, },
2999 { .compatible = "ti,sysc-omap4", .data = &sysc_omap4, },
3000 { .compatible = "ti,sysc-omap4-timer", .data = &sysc_omap4_timer, },
3001 { .compatible = "ti,sysc-omap4-simple", .data = &sysc_omap4_simple, },
3002 { .compatible = "ti,sysc-omap3430-sr", .data = &sysc_34xx_sr, },
3003 { .compatible = "ti,sysc-omap3630-sr", .data = &sysc_36xx_sr, },
3004 { .compatible = "ti,sysc-omap4-sr", .data = &sysc_omap4_sr, },
3005 { .compatible = "ti,sysc-omap3-sham", .data = &sysc_omap3_sham, },
3006 { .compatible = "ti,sysc-omap-aes", .data = &sysc_omap3_aes, },
3007 { .compatible = "ti,sysc-mcasp", .data = &sysc_omap4_mcasp, },
Tony Lindgren2c63a832018-11-15 14:46:53 -08003008 { .compatible = "ti,sysc-dra7-mcasp", .data = &sysc_dra7_mcasp, },
Tony Lindgren70a65242017-12-15 09:41:09 -08003009 { .compatible = "ti,sysc-usb-host-fs",
3010 .data = &sysc_omap4_usb_host_fs, },
Faiz Abbas7f35e632018-07-09 22:18:38 +05303011 { .compatible = "ti,sysc-dra7-mcan", .data = &sysc_dra7_mcan, },
Suman Annab2745d92020-02-27 16:28:34 -06003012 { .compatible = "ti,sysc-pruss", .data = &sysc_pruss, },
Tony Lindgren0eecc632017-10-10 14:23:43 -07003013 { },
3014};
3015MODULE_DEVICE_TABLE(of, sysc_match);
3016
3017static struct platform_driver sysc_driver = {
3018 .probe = sysc_probe,
Tony Lindgren684be5a2017-10-13 10:48:40 -07003019 .remove = sysc_remove,
Tony Lindgren0eecc632017-10-10 14:23:43 -07003020 .driver = {
3021 .name = "ti-sysc",
3022 .of_match_table = sysc_match,
3023 .pm = &sysc_pm_ops,
3024 },
3025};
Tony Lindgren2c355ff2018-02-22 13:58:03 -08003026
3027static int __init sysc_init(void)
3028{
3029 bus_register_notifier(&platform_bus_type, &sysc_nb);
3030
3031 return platform_driver_register(&sysc_driver);
3032}
3033module_init(sysc_init);
3034
3035static void __exit sysc_exit(void)
3036{
3037 bus_unregister_notifier(&platform_bus_type, &sysc_nb);
3038 platform_driver_unregister(&sysc_driver);
Tony Lindgrenfeaa8ba2020-02-24 12:58:03 -08003039 sysc_cleanup_soc();
Tony Lindgren2c355ff2018-02-22 13:58:03 -08003040}
3041module_exit(sysc_exit);
Tony Lindgren0eecc632017-10-10 14:23:43 -07003042
3043MODULE_DESCRIPTION("TI sysc interconnect target driver");
3044MODULE_LICENSE("GPL v2");