blob: 4c377c5765821e147ced6451e972652f50d880b4 [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 Lindgren0eecc632017-10-10 14:23:43 -0700113 */
114struct sysc {
115 struct device *dev;
116 u64 module_pa;
117 u32 module_size;
118 void __iomem *module_va;
119 int offsets[SYSC_MAX_REGS];
Tony Lindgrena3e92e72019-03-21 11:00:21 -0700120 struct ti_sysc_module_data *mdata;
Tony Lindgren09dfe582018-04-16 10:25:52 -0700121 struct clk **clocks;
122 const char **clock_roles;
123 int nr_clocks;
Tony Lindgren50622362018-04-16 10:20:27 -0700124 struct reset_control *rsts;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700125 const char *legacy_mode;
Tony Lindgren70a65242017-12-15 09:41:09 -0800126 const struct sysc_capabilities *cap;
127 struct sysc_config cfg;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800128 struct ti_sysc_cookie cookie;
Tony Lindgren566a9b052017-12-15 09:41:19 -0800129 const char *name;
130 u32 revision;
Tony Lindgren8383e252019-05-27 04:51:56 -0700131 unsigned int enabled:1;
132 unsigned int needs_resume:1;
133 unsigned int child_needs_resume:1;
Tony Lindgren76f0f772018-02-23 08:28:45 -0800134 struct delayed_work idle_work;
Tony Lindgrene64c0212020-02-24 12:58:03 -0800135 void (*pre_reset_quirk)(struct sysc *sysc);
136 void (*post_reset_quirk)(struct sysc *sysc);
Tony Lindgren4e23be42019-06-10 04:48:05 -0700137 void (*reset_done_quirk)(struct sysc *sysc);
Tony Lindgrend7f563d2019-08-14 05:18:16 -0700138 void (*module_enable_quirk)(struct sysc *sysc);
Tony Lindgrenc7d86692019-10-17 11:21:44 -0700139 void (*module_disable_quirk)(struct sysc *sysc);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700140};
141
Tony Lindgren4014c082018-12-10 14:11:26 -0800142static void sysc_parse_dts_quirks(struct sysc *ddata, struct device_node *np,
143 bool is_child);
144
Tony Lindgrenb7182b42019-03-21 11:00:21 -0700145static void sysc_write(struct sysc *ddata, int offset, u32 value)
Faiz Abbas596e7952018-07-09 22:18:39 +0530146{
Tony Lindgren5aa91292019-05-27 04:51:53 -0700147 if (ddata->cfg.quirks & SYSC_QUIRK_16BIT) {
148 writew_relaxed(value & 0xffff, ddata->module_va + offset);
149
150 /* Only i2c revision has LO and HI register with stride of 4 */
151 if (ddata->offsets[SYSC_REVISION] >= 0 &&
152 offset == ddata->offsets[SYSC_REVISION]) {
153 u16 hi = value >> 16;
154
155 writew_relaxed(hi, ddata->module_va + offset + 4);
156 }
157
158 return;
159 }
160
Faiz Abbas596e7952018-07-09 22:18:39 +0530161 writel_relaxed(value, ddata->module_va + offset);
162}
163
Tony Lindgren566a9b052017-12-15 09:41:19 -0800164static u32 sysc_read(struct sysc *ddata, int offset)
165{
166 if (ddata->cfg.quirks & SYSC_QUIRK_16BIT) {
167 u32 val;
168
169 val = readw_relaxed(ddata->module_va + offset);
Tony Lindgren5aa91292019-05-27 04:51:53 -0700170
171 /* Only i2c revision has LO and HI register with stride of 4 */
172 if (ddata->offsets[SYSC_REVISION] >= 0 &&
173 offset == ddata->offsets[SYSC_REVISION]) {
174 u16 tmp = readw_relaxed(ddata->module_va + offset + 4);
175
176 val |= tmp << 16;
177 }
Tony Lindgren566a9b052017-12-15 09:41:19 -0800178
179 return val;
180 }
181
182 return readl_relaxed(ddata->module_va + offset);
183}
184
Tony Lindgren09dfe582018-04-16 10:25:52 -0700185static bool sysc_opt_clks_needed(struct sysc *ddata)
186{
187 return !!(ddata->cfg.quirks & SYSC_QUIRK_OPT_CLKS_NEEDED);
188}
189
Tony Lindgren0eecc632017-10-10 14:23:43 -0700190static u32 sysc_read_revision(struct sysc *ddata)
191{
Tony Lindgren566a9b052017-12-15 09:41:19 -0800192 int offset = ddata->offsets[SYSC_REVISION];
193
194 if (offset < 0)
195 return 0;
196
197 return sysc_read(ddata, offset);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700198}
199
Tony Lindgrene0db94f2019-05-27 04:51:53 -0700200static u32 sysc_read_sysconfig(struct sysc *ddata)
201{
202 int offset = ddata->offsets[SYSC_SYSCONFIG];
203
204 if (offset < 0)
205 return 0;
206
207 return sysc_read(ddata, offset);
208}
209
210static u32 sysc_read_sysstatus(struct sysc *ddata)
211{
212 int offset = ddata->offsets[SYSC_SYSSTATUS];
213
214 if (offset < 0)
215 return 0;
216
217 return sysc_read(ddata, offset);
218}
219
Tony Lindgrena54275f2019-03-21 11:00:21 -0700220static int sysc_add_named_clock_from_child(struct sysc *ddata,
221 const char *name,
222 const char *optfck_name)
223{
224 struct device_node *np = ddata->dev->of_node;
225 struct device_node *child;
226 struct clk_lookup *cl;
227 struct clk *clock;
228 const char *n;
229
230 if (name)
231 n = name;
232 else
233 n = optfck_name;
234
235 /* Does the clock alias already exist? */
236 clock = of_clk_get_by_name(np, n);
237 if (!IS_ERR(clock)) {
238 clk_put(clock);
239
240 return 0;
241 }
242
243 child = of_get_next_available_child(np, NULL);
244 if (!child)
245 return -ENODEV;
246
247 clock = devm_get_clk_from_child(ddata->dev, child, name);
248 if (IS_ERR(clock))
249 return PTR_ERR(clock);
250
251 /*
252 * Use clkdev_add() instead of clkdev_alloc() to avoid the MAX_DEV_ID
253 * limit for clk_get(). If cl ever needs to be freed, it should be done
254 * with clkdev_drop().
255 */
256 cl = kcalloc(1, sizeof(*cl), GFP_KERNEL);
257 if (!cl)
258 return -ENOMEM;
259
260 cl->con_id = n;
261 cl->dev_id = dev_name(ddata->dev);
262 cl->clk = clock;
263 clkdev_add(cl);
264
265 clk_put(clock);
266
267 return 0;
268}
269
270static int sysc_init_ext_opt_clock(struct sysc *ddata, const char *name)
271{
272 const char *optfck_name;
273 int error, index;
274
275 if (ddata->nr_clocks < SYSC_OPTFCK0)
276 index = SYSC_OPTFCK0;
277 else
278 index = ddata->nr_clocks;
279
280 if (name)
281 optfck_name = name;
282 else
283 optfck_name = clock_names[index];
284
285 error = sysc_add_named_clock_from_child(ddata, name, optfck_name);
286 if (error)
287 return error;
288
289 ddata->clock_roles[index] = optfck_name;
290 ddata->nr_clocks++;
291
292 return 0;
293}
294
Tony Lindgren09dfe582018-04-16 10:25:52 -0700295static int sysc_get_one_clock(struct sysc *ddata, const char *name)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700296{
Tony Lindgren09dfe582018-04-16 10:25:52 -0700297 int error, i, index = -ENODEV;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700298
Tony Lindgren09dfe582018-04-16 10:25:52 -0700299 if (!strncmp(clock_names[SYSC_FCK], name, 3))
300 index = SYSC_FCK;
301 else if (!strncmp(clock_names[SYSC_ICK], name, 3))
302 index = SYSC_ICK;
303
304 if (index < 0) {
305 for (i = SYSC_OPTFCK0; i < SYSC_MAX_CLOCKS; i++) {
Tony Lindgrenc97c8622018-05-18 07:54:44 -0700306 if (!ddata->clocks[i]) {
Tony Lindgren09dfe582018-04-16 10:25:52 -0700307 index = i;
308 break;
309 }
310 }
Tony Lindgren0eecc632017-10-10 14:23:43 -0700311 }
Tony Lindgren09dfe582018-04-16 10:25:52 -0700312
313 if (index < 0) {
314 dev_err(ddata->dev, "clock %s not added\n", name);
315 return index;
316 }
Tony Lindgren0eecc632017-10-10 14:23:43 -0700317
318 ddata->clocks[index] = devm_clk_get(ddata->dev, name);
319 if (IS_ERR(ddata->clocks[index])) {
Tony Lindgren0eecc632017-10-10 14:23:43 -0700320 dev_err(ddata->dev, "clock get error for %s: %li\n",
321 name, PTR_ERR(ddata->clocks[index]));
322
323 return PTR_ERR(ddata->clocks[index]);
324 }
325
326 error = clk_prepare(ddata->clocks[index]);
327 if (error) {
328 dev_err(ddata->dev, "clock prepare error for %s: %i\n",
329 name, error);
330
331 return error;
332 }
333
334 return 0;
335}
336
337static int sysc_get_clocks(struct sysc *ddata)
338{
Tony Lindgren09dfe582018-04-16 10:25:52 -0700339 struct device_node *np = ddata->dev->of_node;
340 struct property *prop;
341 const char *name;
342 int nr_fck = 0, nr_ick = 0, i, error = 0;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700343
Kees Cook20749052018-06-18 10:45:49 -0700344 ddata->clock_roles = devm_kcalloc(ddata->dev,
Tony Lindgren09dfe582018-04-16 10:25:52 -0700345 SYSC_MAX_CLOCKS,
Kees Cook20749052018-06-18 10:45:49 -0700346 sizeof(*ddata->clock_roles),
Tony Lindgren09dfe582018-04-16 10:25:52 -0700347 GFP_KERNEL);
348 if (!ddata->clock_roles)
349 return -ENOMEM;
350
351 of_property_for_each_string(np, "clock-names", prop, name) {
352 if (!strncmp(clock_names[SYSC_FCK], name, 3))
353 nr_fck++;
354 if (!strncmp(clock_names[SYSC_ICK], name, 3))
355 nr_ick++;
356 ddata->clock_roles[ddata->nr_clocks] = name;
357 ddata->nr_clocks++;
358 }
359
360 if (ddata->nr_clocks < 1)
361 return 0;
362
Tony Lindgrena54275f2019-03-21 11:00:21 -0700363 if ((ddata->cfg.quirks & SYSC_QUIRK_EXT_OPT_CLOCK)) {
364 error = sysc_init_ext_opt_clock(ddata, NULL);
365 if (error)
366 return error;
367 }
368
Tony Lindgren09dfe582018-04-16 10:25:52 -0700369 if (ddata->nr_clocks > SYSC_MAX_CLOCKS) {
370 dev_err(ddata->dev, "too many clocks for %pOF\n", np);
371
372 return -EINVAL;
373 }
374
375 if (nr_fck > 1 || nr_ick > 1) {
376 dev_err(ddata->dev, "max one fck and ick for %pOF\n", np);
377
378 return -EINVAL;
379 }
380
Tony Lindgren2c81f0f2019-12-16 14:41:53 -0800381 /* Always add a slot for main clocks fck and ick even if unused */
382 if (!nr_fck)
383 ddata->nr_clocks++;
384 if (!nr_ick)
385 ddata->nr_clocks++;
386
Kees Cook20749052018-06-18 10:45:49 -0700387 ddata->clocks = devm_kcalloc(ddata->dev,
388 ddata->nr_clocks, sizeof(*ddata->clocks),
Tony Lindgren09dfe582018-04-16 10:25:52 -0700389 GFP_KERNEL);
390 if (!ddata->clocks)
391 return -ENOMEM;
392
Tony Lindgren7b4f8ac2018-11-15 14:46:52 -0800393 for (i = 0; i < SYSC_MAX_CLOCKS; i++) {
394 const char *name = ddata->clock_roles[i];
395
396 if (!name)
397 continue;
398
399 error = sysc_get_one_clock(ddata, name);
Tony Lindgren2783d062019-09-05 13:01:29 -0700400 if (error)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700401 return error;
402 }
403
404 return 0;
405}
406
Tony Lindgrend8789702019-03-21 11:00:21 -0700407static int sysc_enable_main_clocks(struct sysc *ddata)
408{
409 struct clk *clock;
410 int i, error;
411
412 if (!ddata->clocks)
413 return 0;
414
415 for (i = 0; i < SYSC_OPTFCK0; i++) {
416 clock = ddata->clocks[i];
417
418 /* Main clocks may not have ick */
419 if (IS_ERR_OR_NULL(clock))
420 continue;
421
422 error = clk_enable(clock);
423 if (error)
424 goto err_disable;
425 }
426
427 return 0;
428
429err_disable:
430 for (i--; i >= 0; i--) {
431 clock = ddata->clocks[i];
432
433 /* Main clocks may not have ick */
434 if (IS_ERR_OR_NULL(clock))
435 continue;
436
437 clk_disable(clock);
438 }
439
440 return error;
441}
442
443static void sysc_disable_main_clocks(struct sysc *ddata)
444{
445 struct clk *clock;
446 int i;
447
448 if (!ddata->clocks)
449 return;
450
451 for (i = 0; i < SYSC_OPTFCK0; i++) {
452 clock = ddata->clocks[i];
453 if (IS_ERR_OR_NULL(clock))
454 continue;
455
456 clk_disable(clock);
457 }
458}
459
460static int sysc_enable_opt_clocks(struct sysc *ddata)
461{
462 struct clk *clock;
463 int i, error;
464
Tony Lindgren2c81f0f2019-12-16 14:41:53 -0800465 if (!ddata->clocks || ddata->nr_clocks < SYSC_OPTFCK0 + 1)
Tony Lindgrend8789702019-03-21 11:00:21 -0700466 return 0;
467
468 for (i = SYSC_OPTFCK0; i < SYSC_MAX_CLOCKS; i++) {
469 clock = ddata->clocks[i];
470
471 /* Assume no holes for opt clocks */
472 if (IS_ERR_OR_NULL(clock))
473 return 0;
474
475 error = clk_enable(clock);
476 if (error)
477 goto err_disable;
478 }
479
480 return 0;
481
482err_disable:
483 for (i--; i >= 0; i--) {
484 clock = ddata->clocks[i];
485 if (IS_ERR_OR_NULL(clock))
486 continue;
487
488 clk_disable(clock);
489 }
490
491 return error;
492}
493
494static void sysc_disable_opt_clocks(struct sysc *ddata)
495{
496 struct clk *clock;
497 int i;
498
Tony Lindgren2c81f0f2019-12-16 14:41:53 -0800499 if (!ddata->clocks || ddata->nr_clocks < SYSC_OPTFCK0 + 1)
Tony Lindgrend8789702019-03-21 11:00:21 -0700500 return;
501
502 for (i = SYSC_OPTFCK0; i < SYSC_MAX_CLOCKS; i++) {
503 clock = ddata->clocks[i];
504
505 /* Assume no holes for opt clocks */
506 if (IS_ERR_OR_NULL(clock))
507 return;
508
509 clk_disable(clock);
510 }
511}
512
Tony Lindgren2b2f7de2019-05-27 04:51:53 -0700513static void sysc_clkdm_deny_idle(struct sysc *ddata)
514{
515 struct ti_sysc_platform_data *pdata;
516
Tony Lindgren94f63452019-12-12 08:57:58 -0800517 if (ddata->legacy_mode || (ddata->cfg.quirks & SYSC_QUIRK_CLKDM_NOAUTO))
Tony Lindgren2b2f7de2019-05-27 04:51:53 -0700518 return;
519
520 pdata = dev_get_platdata(ddata->dev);
521 if (pdata && pdata->clkdm_deny_idle)
522 pdata->clkdm_deny_idle(ddata->dev, &ddata->cookie);
523}
524
525static void sysc_clkdm_allow_idle(struct sysc *ddata)
526{
527 struct ti_sysc_platform_data *pdata;
528
Tony Lindgren94f63452019-12-12 08:57:58 -0800529 if (ddata->legacy_mode || (ddata->cfg.quirks & SYSC_QUIRK_CLKDM_NOAUTO))
Tony Lindgren2b2f7de2019-05-27 04:51:53 -0700530 return;
531
532 pdata = dev_get_platdata(ddata->dev);
533 if (pdata && pdata->clkdm_allow_idle)
534 pdata->clkdm_allow_idle(ddata->dev, &ddata->cookie);
535}
536
Tony Lindgren0eecc632017-10-10 14:23:43 -0700537/**
Tony Lindgrenb11c1ea2019-03-21 11:00:21 -0700538 * sysc_init_resets - init rstctrl reset line if configured
Tony Lindgren50622362018-04-16 10:20:27 -0700539 * @ddata: device driver data
540 *
Tony Lindgrenb11c1ea2019-03-21 11:00:21 -0700541 * See sysc_rstctrl_reset_deassert().
Tony Lindgren50622362018-04-16 10:20:27 -0700542 */
543static int sysc_init_resets(struct sysc *ddata)
544{
Tony Lindgren50622362018-04-16 10:20:27 -0700545 ddata->rsts =
Tero Kristobb88b862019-08-07 15:46:05 +0300546 devm_reset_control_get_optional_shared(ddata->dev, "rstctrl");
Tony Lindgren50622362018-04-16 10:20:27 -0700547
zhengbin3f2c4202019-11-18 18:37:32 +0800548 return PTR_ERR_OR_ZERO(ddata->rsts);
Tony Lindgren50622362018-04-16 10:20:27 -0700549}
550
551/**
Tony Lindgren0eecc632017-10-10 14:23:43 -0700552 * sysc_parse_and_check_child_range - parses module IO region from ranges
553 * @ddata: device driver data
554 *
555 * In general we only need rev, syss, and sysc registers and not the whole
556 * module range. But we do want the offsets for these registers from the
557 * module base. This allows us to check them against the legacy hwmod
558 * platform data. Let's also check the ranges are configured properly.
559 */
560static int sysc_parse_and_check_child_range(struct sysc *ddata)
561{
562 struct device_node *np = ddata->dev->of_node;
563 const __be32 *ranges;
564 u32 nr_addr, nr_size;
565 int len, error;
566
567 ranges = of_get_property(np, "ranges", &len);
568 if (!ranges) {
569 dev_err(ddata->dev, "missing ranges for %pOF\n", np);
570
571 return -ENOENT;
572 }
573
574 len /= sizeof(*ranges);
575
576 if (len < 3) {
577 dev_err(ddata->dev, "incomplete ranges for %pOF\n", np);
578
579 return -EINVAL;
580 }
581
582 error = of_property_read_u32(np, "#address-cells", &nr_addr);
583 if (error)
584 return -ENOENT;
585
586 error = of_property_read_u32(np, "#size-cells", &nr_size);
587 if (error)
588 return -ENOENT;
589
590 if (nr_addr != 1 || nr_size != 1) {
591 dev_err(ddata->dev, "invalid ranges for %pOF\n", np);
592
593 return -EINVAL;
594 }
595
596 ranges++;
597 ddata->module_pa = of_translate_address(np, ranges++);
598 ddata->module_size = be32_to_cpup(ranges);
599
Tony Lindgren0eecc632017-10-10 14:23:43 -0700600 return 0;
601}
602
Tony Lindgren3bb37c82018-02-22 14:05:14 -0800603static struct device_node *stdout_path;
604
605static void sysc_init_stdout_path(struct sysc *ddata)
606{
607 struct device_node *np = NULL;
608 const char *uart;
609
610 if (IS_ERR(stdout_path))
611 return;
612
613 if (stdout_path)
614 return;
615
616 np = of_find_node_by_path("/chosen");
617 if (!np)
618 goto err;
619
620 uart = of_get_property(np, "stdout-path", NULL);
621 if (!uart)
622 goto err;
623
624 np = of_find_node_by_path(uart);
625 if (!np)
626 goto err;
627
628 stdout_path = np;
629
630 return;
631
632err:
633 stdout_path = ERR_PTR(-ENODEV);
634}
635
636static void sysc_check_quirk_stdout(struct sysc *ddata,
637 struct device_node *np)
638{
639 sysc_init_stdout_path(ddata);
640 if (np != stdout_path)
641 return;
642
643 ddata->cfg.quirks |= SYSC_QUIRK_NO_IDLE_ON_INIT |
644 SYSC_QUIRK_NO_RESET_ON_INIT;
645}
646
Tony Lindgren0eecc632017-10-10 14:23:43 -0700647/**
648 * sysc_check_one_child - check child configuration
649 * @ddata: device driver data
650 * @np: child device node
651 *
652 * Let's avoid messy situations where we have new interconnect target
653 * node but children have "ti,hwmods". These belong to the interconnect
654 * target node and are managed by this driver.
655 */
Nishka Dasguptac6e78d72019-08-15 11:16:47 +0530656static void sysc_check_one_child(struct sysc *ddata,
657 struct device_node *np)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700658{
659 const char *name;
660
661 name = of_get_property(np, "ti,hwmods", NULL);
Tony Lindgren7320fd32020-02-24 12:58:03 -0800662 if (name && !of_device_is_compatible(np, "ti,sysc"))
Tony Lindgren0eecc632017-10-10 14:23:43 -0700663 dev_warn(ddata->dev, "really a child ti,hwmods property?");
664
Tony Lindgren3bb37c82018-02-22 14:05:14 -0800665 sysc_check_quirk_stdout(ddata, np);
Tony Lindgren4014c082018-12-10 14:11:26 -0800666 sysc_parse_dts_quirks(ddata, np, true);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700667}
668
Nishka Dasguptac6e78d72019-08-15 11:16:47 +0530669static void sysc_check_children(struct sysc *ddata)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700670{
671 struct device_node *child;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700672
Nishka Dasguptac6e78d72019-08-15 11:16:47 +0530673 for_each_child_of_node(ddata->dev->of_node, child)
674 sysc_check_one_child(ddata, child);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700675}
676
Tony Lindgrena7199e22017-12-15 09:41:14 -0800677/*
678 * So far only I2C uses 16-bit read access with clockactivity with revision
679 * in two registers with stride of 4. We can detect this based on the rev
680 * register size to configure things far enough to be able to properly read
681 * the revision register.
682 */
683static void sysc_check_quirk_16bit(struct sysc *ddata, struct resource *res)
684{
Tony Lindgrendd57ac12018-02-22 14:09:57 -0800685 if (resource_size(res) == 8)
Tony Lindgrena7199e22017-12-15 09:41:14 -0800686 ddata->cfg.quirks |= SYSC_QUIRK_16BIT | SYSC_QUIRK_USE_CLOCKACT;
Tony Lindgrena7199e22017-12-15 09:41:14 -0800687}
688
Tony Lindgren0eecc632017-10-10 14:23:43 -0700689/**
690 * sysc_parse_one - parses the interconnect target module registers
691 * @ddata: device driver data
692 * @reg: register to parse
693 */
694static int sysc_parse_one(struct sysc *ddata, enum sysc_registers reg)
695{
696 struct resource *res;
697 const char *name;
698
699 switch (reg) {
700 case SYSC_REVISION:
701 case SYSC_SYSCONFIG:
702 case SYSC_SYSSTATUS:
703 name = reg_names[reg];
704 break;
705 default:
706 return -EINVAL;
707 }
708
709 res = platform_get_resource_byname(to_platform_device(ddata->dev),
710 IORESOURCE_MEM, name);
711 if (!res) {
Tony Lindgren0eecc632017-10-10 14:23:43 -0700712 ddata->offsets[reg] = -ENODEV;
713
714 return 0;
715 }
716
717 ddata->offsets[reg] = res->start - ddata->module_pa;
Tony Lindgrena7199e22017-12-15 09:41:14 -0800718 if (reg == SYSC_REVISION)
719 sysc_check_quirk_16bit(ddata, res);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700720
721 return 0;
722}
723
724static int sysc_parse_registers(struct sysc *ddata)
725{
726 int i, error;
727
728 for (i = 0; i < SYSC_MAX_REGS; i++) {
729 error = sysc_parse_one(ddata, i);
730 if (error)
731 return error;
732 }
733
734 return 0;
735}
736
737/**
738 * sysc_check_registers - check for misconfigured register overlaps
739 * @ddata: device driver data
740 */
741static int sysc_check_registers(struct sysc *ddata)
742{
743 int i, j, nr_regs = 0, nr_matches = 0;
744
745 for (i = 0; i < SYSC_MAX_REGS; i++) {
746 if (ddata->offsets[i] < 0)
747 continue;
748
749 if (ddata->offsets[i] > (ddata->module_size - 4)) {
750 dev_err(ddata->dev, "register outside module range");
751
752 return -EINVAL;
753 }
754
755 for (j = 0; j < SYSC_MAX_REGS; j++) {
756 if (ddata->offsets[j] < 0)
757 continue;
758
759 if (ddata->offsets[i] == ddata->offsets[j])
760 nr_matches++;
761 }
762 nr_regs++;
763 }
764
Tony Lindgren0eecc632017-10-10 14:23:43 -0700765 if (nr_matches > nr_regs) {
766 dev_err(ddata->dev, "overlapping registers: (%i/%i)",
767 nr_regs, nr_matches);
768
769 return -EINVAL;
770 }
771
772 return 0;
773}
774
775/**
776 * syc_ioremap - ioremap register space for the interconnect target module
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700777 * @ddata: device driver data
Tony Lindgren0eecc632017-10-10 14:23:43 -0700778 *
779 * Note that the interconnect target module registers can be anywhere
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700780 * within the interconnect target module range. For example, SGX has
781 * them at offset 0x1fc00 in the 32MB module address space. And cpsw
782 * has them at offset 0x1200 in the CPSW_WR child. Usually the
783 * the interconnect target module registers are at the beginning of
784 * the module range though.
Tony Lindgren0eecc632017-10-10 14:23:43 -0700785 */
786static int sysc_ioremap(struct sysc *ddata)
787{
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700788 int size;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700789
Tony Lindgrene4f50c82019-05-01 14:24:57 -0700790 if (ddata->offsets[SYSC_REVISION] < 0 &&
791 ddata->offsets[SYSC_SYSCONFIG] < 0 &&
792 ddata->offsets[SYSC_SYSSTATUS] < 0) {
793 size = ddata->module_size;
794 } else {
795 size = max3(ddata->offsets[SYSC_REVISION],
796 ddata->offsets[SYSC_SYSCONFIG],
797 ddata->offsets[SYSC_SYSSTATUS]);
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700798
Tony Lindgren4e23be42019-06-10 04:48:05 -0700799 if (size < SZ_1K)
800 size = SZ_1K;
801
Tony Lindgrene4f50c82019-05-01 14:24:57 -0700802 if ((size + sizeof(u32)) > ddata->module_size)
Tony Lindgren4e23be42019-06-10 04:48:05 -0700803 size = ddata->module_size;
Tony Lindgrene4f50c82019-05-01 14:24:57 -0700804 }
Tony Lindgren0eecc632017-10-10 14:23:43 -0700805
Tony Lindgren0eecc632017-10-10 14:23:43 -0700806 ddata->module_va = devm_ioremap(ddata->dev,
807 ddata->module_pa,
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700808 size + sizeof(u32));
Tony Lindgren0eecc632017-10-10 14:23:43 -0700809 if (!ddata->module_va)
810 return -EIO;
811
812 return 0;
813}
814
815/**
816 * sysc_map_and_check_registers - ioremap and check device registers
817 * @ddata: device driver data
818 */
819static int sysc_map_and_check_registers(struct sysc *ddata)
820{
821 int error;
822
823 error = sysc_parse_and_check_child_range(ddata);
824 if (error)
825 return error;
826
Nishka Dasguptac6e78d72019-08-15 11:16:47 +0530827 sysc_check_children(ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700828
829 error = sysc_parse_registers(ddata);
830 if (error)
831 return error;
832
833 error = sysc_ioremap(ddata);
834 if (error)
835 return error;
836
837 error = sysc_check_registers(ddata);
838 if (error)
839 return error;
840
841 return 0;
842}
843
844/**
845 * sysc_show_rev - read and show interconnect target module revision
846 * @bufp: buffer to print the information to
847 * @ddata: device driver data
848 */
849static int sysc_show_rev(char *bufp, struct sysc *ddata)
850{
Tony Lindgren566a9b052017-12-15 09:41:19 -0800851 int len;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700852
853 if (ddata->offsets[SYSC_REVISION] < 0)
854 return sprintf(bufp, ":NA");
855
Tony Lindgren566a9b052017-12-15 09:41:19 -0800856 len = sprintf(bufp, ":%08x", ddata->revision);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700857
858 return len;
859}
860
861static int sysc_show_reg(struct sysc *ddata,
862 char *bufp, enum sysc_registers reg)
863{
864 if (ddata->offsets[reg] < 0)
865 return sprintf(bufp, ":NA");
866
867 return sprintf(bufp, ":%x", ddata->offsets[reg]);
868}
869
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800870static int sysc_show_name(char *bufp, struct sysc *ddata)
871{
872 if (!ddata->name)
873 return 0;
874
875 return sprintf(bufp, ":%s", ddata->name);
876}
877
Tony Lindgren0eecc632017-10-10 14:23:43 -0700878/**
879 * sysc_show_registers - show information about interconnect target module
880 * @ddata: device driver data
881 */
882static void sysc_show_registers(struct sysc *ddata)
883{
884 char buf[128];
885 char *bufp = buf;
886 int i;
887
888 for (i = 0; i < SYSC_MAX_REGS; i++)
889 bufp += sysc_show_reg(ddata, bufp, i);
890
891 bufp += sysc_show_rev(bufp, ddata);
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800892 bufp += sysc_show_name(bufp, ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700893
894 dev_dbg(ddata->dev, "%llx:%x%s\n",
895 ddata->module_pa, ddata->module_size,
896 buf);
897}
898
Roger Quadrosd59b6052019-04-08 12:52:39 +0300899#define SYSC_IDLE_MASK (SYSC_NR_IDLEMODES - 1)
Tony Lindgrenae9ae122019-05-27 04:51:54 -0700900#define SYSC_CLOCACT_ICK 2
Roger Quadrosd59b6052019-04-08 12:52:39 +0300901
Tony Lindgren2b2f7de2019-05-27 04:51:53 -0700902/* Caller needs to manage sysc_clkdm_deny_idle() and sysc_clkdm_allow_idle() */
Roger Quadrosd59b6052019-04-08 12:52:39 +0300903static int sysc_enable_module(struct device *dev)
904{
905 struct sysc *ddata;
906 const struct sysc_regbits *regbits;
907 u32 reg, idlemodes, best_mode;
908
909 ddata = dev_get_drvdata(dev);
910 if (ddata->offsets[SYSC_SYSCONFIG] == -ENODEV)
911 return 0;
912
Roger Quadrosd59b6052019-04-08 12:52:39 +0300913 regbits = ddata->cap->regbits;
914 reg = sysc_read(ddata, ddata->offsets[SYSC_SYSCONFIG]);
915
Tony Lindgrenae9ae122019-05-27 04:51:54 -0700916 /* Set CLOCKACTIVITY, we only use it for ick */
917 if (regbits->clkact_shift >= 0 &&
918 (ddata->cfg.quirks & SYSC_QUIRK_USE_CLOCKACT ||
919 ddata->cfg.sysc_val & BIT(regbits->clkact_shift)))
920 reg |= SYSC_CLOCACT_ICK << regbits->clkact_shift;
921
Roger Quadrosd59b6052019-04-08 12:52:39 +0300922 /* Set SIDLE mode */
923 idlemodes = ddata->cfg.sidlemodes;
924 if (!idlemodes || regbits->sidle_shift < 0)
925 goto set_midle;
926
Tony Lindgrenfb685f12019-05-27 04:51:55 -0700927 if (ddata->cfg.quirks & (SYSC_QUIRK_SWSUP_SIDLE |
928 SYSC_QUIRK_SWSUP_SIDLE_ACT)) {
929 best_mode = SYSC_IDLE_NO;
930 } else {
931 best_mode = fls(ddata->cfg.sidlemodes) - 1;
932 if (best_mode > SYSC_IDLE_MASK) {
933 dev_err(dev, "%s: invalid sidlemode\n", __func__);
934 return -EINVAL;
935 }
Tony Lindgren6e09f492019-05-27 04:51:55 -0700936
937 /* Set WAKEUP */
938 if (regbits->enwkup_shift >= 0 &&
939 ddata->cfg.sysc_val & BIT(regbits->enwkup_shift))
940 reg |= BIT(regbits->enwkup_shift);
Roger Quadrosd59b6052019-04-08 12:52:39 +0300941 }
942
943 reg &= ~(SYSC_IDLE_MASK << regbits->sidle_shift);
944 reg |= best_mode << regbits->sidle_shift;
945 sysc_write(ddata, ddata->offsets[SYSC_SYSCONFIG], reg);
946
947set_midle:
948 /* Set MIDLE mode */
949 idlemodes = ddata->cfg.midlemodes;
950 if (!idlemodes || regbits->midle_shift < 0)
Tony Lindgreneec26552019-05-27 04:51:54 -0700951 goto set_autoidle;
Roger Quadrosd59b6052019-04-08 12:52:39 +0300952
953 best_mode = fls(ddata->cfg.midlemodes) - 1;
954 if (best_mode > SYSC_IDLE_MASK) {
955 dev_err(dev, "%s: invalid midlemode\n", __func__);
956 return -EINVAL;
957 }
958
Tony Lindgren03856e92019-10-21 13:36:09 -0700959 if (ddata->cfg.quirks & SYSC_QUIRK_SWSUP_MSTANDBY)
960 best_mode = SYSC_IDLE_NO;
961
Roger Quadrosd59b6052019-04-08 12:52:39 +0300962 reg &= ~(SYSC_IDLE_MASK << regbits->midle_shift);
963 reg |= best_mode << regbits->midle_shift;
964 sysc_write(ddata, ddata->offsets[SYSC_SYSCONFIG], reg);
965
Tony Lindgreneec26552019-05-27 04:51:54 -0700966set_autoidle:
967 /* Autoidle bit must enabled separately if available */
968 if (regbits->autoidle_shift >= 0 &&
969 ddata->cfg.sysc_val & BIT(regbits->autoidle_shift)) {
970 reg |= 1 << regbits->autoidle_shift;
971 sysc_write(ddata, ddata->offsets[SYSC_SYSCONFIG], reg);
972 }
973
Tony Lindgrend7f563d2019-08-14 05:18:16 -0700974 if (ddata->module_enable_quirk)
975 ddata->module_enable_quirk(ddata);
976
Roger Quadrosd59b6052019-04-08 12:52:39 +0300977 return 0;
978}
979
980static int sysc_best_idle_mode(u32 idlemodes, u32 *best_mode)
981{
982 if (idlemodes & BIT(SYSC_IDLE_SMART_WKUP))
983 *best_mode = SYSC_IDLE_SMART_WKUP;
984 else if (idlemodes & BIT(SYSC_IDLE_SMART))
985 *best_mode = SYSC_IDLE_SMART;
Tony Lindgren6ee82412019-07-22 03:44:42 -0700986 else if (idlemodes & BIT(SYSC_IDLE_FORCE))
Roger Quadrosd59b6052019-04-08 12:52:39 +0300987 *best_mode = SYSC_IDLE_FORCE;
988 else
989 return -EINVAL;
990
991 return 0;
992}
993
Tony Lindgren2b2f7de2019-05-27 04:51:53 -0700994/* Caller needs to manage sysc_clkdm_deny_idle() and sysc_clkdm_allow_idle() */
Roger Quadrosd59b6052019-04-08 12:52:39 +0300995static int sysc_disable_module(struct device *dev)
996{
997 struct sysc *ddata;
998 const struct sysc_regbits *regbits;
999 u32 reg, idlemodes, best_mode;
1000 int ret;
1001
1002 ddata = dev_get_drvdata(dev);
1003 if (ddata->offsets[SYSC_SYSCONFIG] == -ENODEV)
1004 return 0;
1005
Tony Lindgrenc7d86692019-10-17 11:21:44 -07001006 if (ddata->module_disable_quirk)
1007 ddata->module_disable_quirk(ddata);
1008
Roger Quadrosd59b6052019-04-08 12:52:39 +03001009 regbits = ddata->cap->regbits;
1010 reg = sysc_read(ddata, ddata->offsets[SYSC_SYSCONFIG]);
1011
1012 /* Set MIDLE mode */
1013 idlemodes = ddata->cfg.midlemodes;
1014 if (!idlemodes || regbits->midle_shift < 0)
1015 goto set_sidle;
1016
1017 ret = sysc_best_idle_mode(idlemodes, &best_mode);
1018 if (ret) {
1019 dev_err(dev, "%s: invalid midlemode\n", __func__);
1020 return ret;
1021 }
1022
Tony Lindgren93c60482019-12-09 13:55:15 -08001023 if (ddata->cfg.quirks & (SYSC_QUIRK_SWSUP_MSTANDBY) ||
1024 ddata->cfg.quirks & (SYSC_QUIRK_FORCE_MSTANDBY))
Tony Lindgren03856e92019-10-21 13:36:09 -07001025 best_mode = SYSC_IDLE_FORCE;
1026
Roger Quadrosd59b6052019-04-08 12:52:39 +03001027 reg &= ~(SYSC_IDLE_MASK << regbits->midle_shift);
1028 reg |= best_mode << regbits->midle_shift;
1029 sysc_write(ddata, ddata->offsets[SYSC_SYSCONFIG], reg);
1030
1031set_sidle:
1032 /* Set SIDLE mode */
1033 idlemodes = ddata->cfg.sidlemodes;
1034 if (!idlemodes || regbits->sidle_shift < 0)
1035 return 0;
1036
Tony Lindgrenfb685f12019-05-27 04:51:55 -07001037 if (ddata->cfg.quirks & SYSC_QUIRK_SWSUP_SIDLE) {
1038 best_mode = SYSC_IDLE_FORCE;
1039 } else {
1040 ret = sysc_best_idle_mode(idlemodes, &best_mode);
1041 if (ret) {
1042 dev_err(dev, "%s: invalid sidlemode\n", __func__);
1043 return ret;
1044 }
Roger Quadrosd59b6052019-04-08 12:52:39 +03001045 }
1046
1047 reg &= ~(SYSC_IDLE_MASK << regbits->sidle_shift);
1048 reg |= best_mode << regbits->sidle_shift;
Tony Lindgreneec26552019-05-27 04:51:54 -07001049 if (regbits->autoidle_shift >= 0 &&
1050 ddata->cfg.sysc_val & BIT(regbits->autoidle_shift))
1051 reg |= 1 << regbits->autoidle_shift;
Roger Quadrosd59b6052019-04-08 12:52:39 +03001052 sysc_write(ddata, ddata->offsets[SYSC_SYSCONFIG], reg);
1053
1054 return 0;
1055}
1056
Tony Lindgrenff437282019-03-21 11:00:21 -07001057static int __maybe_unused sysc_runtime_suspend_legacy(struct device *dev,
1058 struct sysc *ddata)
Tony Lindgren0eecc632017-10-10 14:23:43 -07001059{
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001060 struct ti_sysc_platform_data *pdata;
Tony Lindgrenff437282019-03-21 11:00:21 -07001061 int error;
1062
1063 pdata = dev_get_platdata(ddata->dev);
1064 if (!pdata)
1065 return 0;
1066
1067 if (!pdata->idle_module)
1068 return -ENODEV;
1069
1070 error = pdata->idle_module(dev, &ddata->cookie);
1071 if (error)
1072 dev_err(dev, "%s: could not idle: %i\n",
1073 __func__, error);
1074
Tero Kristo4345f0d2019-08-07 15:46:04 +03001075 reset_control_assert(ddata->rsts);
Tony Lindgren8383e252019-05-27 04:51:56 -07001076
Tony Lindgrenff437282019-03-21 11:00:21 -07001077 return 0;
1078}
1079
1080static int __maybe_unused sysc_runtime_resume_legacy(struct device *dev,
1081 struct sysc *ddata)
1082{
1083 struct ti_sysc_platform_data *pdata;
1084 int error;
1085
1086 pdata = dev_get_platdata(ddata->dev);
1087 if (!pdata)
1088 return 0;
1089
1090 if (!pdata->enable_module)
1091 return -ENODEV;
1092
1093 error = pdata->enable_module(dev, &ddata->cookie);
1094 if (error)
1095 dev_err(dev, "%s: could not enable: %i\n",
1096 __func__, error);
1097
Tero Kristobf59ebb2019-10-07 15:29:29 +03001098 reset_control_deassert(ddata->rsts);
1099
Tony Lindgrenff437282019-03-21 11:00:21 -07001100 return 0;
1101}
1102
1103static int __maybe_unused sysc_runtime_suspend(struct device *dev)
1104{
Tony Lindgren0eecc632017-10-10 14:23:43 -07001105 struct sysc *ddata;
Tony Lindgrend8789702019-03-21 11:00:21 -07001106 int error = 0;
Tony Lindgren0eecc632017-10-10 14:23:43 -07001107
1108 ddata = dev_get_drvdata(dev);
1109
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001110 if (!ddata->enabled)
Tony Lindgren0eecc632017-10-10 14:23:43 -07001111 return 0;
1112
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001113 sysc_clkdm_deny_idle(ddata);
1114
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001115 if (ddata->legacy_mode) {
Tony Lindgrenff437282019-03-21 11:00:21 -07001116 error = sysc_runtime_suspend_legacy(dev, ddata);
Tony Lindgren93de83a2019-03-21 11:00:21 -07001117 if (error)
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001118 goto err_allow_idle;
Roger Quadrosd59b6052019-04-08 12:52:39 +03001119 } else {
1120 error = sysc_disable_module(dev);
1121 if (error)
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001122 goto err_allow_idle;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001123 }
1124
Tony Lindgrend8789702019-03-21 11:00:21 -07001125 sysc_disable_main_clocks(ddata);
Tony Lindgren09dfe582018-04-16 10:25:52 -07001126
Tony Lindgrend8789702019-03-21 11:00:21 -07001127 if (sysc_opt_clks_needed(ddata))
1128 sysc_disable_opt_clocks(ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001129
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001130 ddata->enabled = false;
1131
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001132err_allow_idle:
Tero Kristo4345f0d2019-08-07 15:46:04 +03001133 reset_control_assert(ddata->rsts);
Tony Lindgren8383e252019-05-27 04:51:56 -07001134
Tero Kristob6036312019-08-07 15:46:03 +03001135 sysc_clkdm_allow_idle(ddata);
1136
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001137 return error;
Tony Lindgren0eecc632017-10-10 14:23:43 -07001138}
1139
Arnd Bergmanna4a5d492017-10-13 11:25:32 +02001140static int __maybe_unused sysc_runtime_resume(struct device *dev)
Tony Lindgren0eecc632017-10-10 14:23:43 -07001141{
1142 struct sysc *ddata;
Tony Lindgrend8789702019-03-21 11:00:21 -07001143 int error = 0;
Tony Lindgren0eecc632017-10-10 14:23:43 -07001144
1145 ddata = dev_get_drvdata(dev);
1146
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001147 if (ddata->enabled)
Tony Lindgren0eecc632017-10-10 14:23:43 -07001148 return 0;
1149
Tony Lindgren8383e252019-05-27 04:51:56 -07001150
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001151 sysc_clkdm_deny_idle(ddata);
1152
Tony Lindgrend8789702019-03-21 11:00:21 -07001153 if (sysc_opt_clks_needed(ddata)) {
1154 error = sysc_enable_opt_clocks(ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001155 if (error)
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001156 goto err_allow_idle;
Tony Lindgren0eecc632017-10-10 14:23:43 -07001157 }
1158
Tony Lindgrend8789702019-03-21 11:00:21 -07001159 error = sysc_enable_main_clocks(ddata);
1160 if (error)
Tony Lindgren93de83a2019-03-21 11:00:21 -07001161 goto err_opt_clocks;
1162
Tero Kristobf59ebb2019-10-07 15:29:29 +03001163 reset_control_deassert(ddata->rsts);
1164
Tony Lindgren93de83a2019-03-21 11:00:21 -07001165 if (ddata->legacy_mode) {
1166 error = sysc_runtime_resume_legacy(dev, ddata);
1167 if (error)
1168 goto err_main_clocks;
Roger Quadrosd59b6052019-04-08 12:52:39 +03001169 } else {
1170 error = sysc_enable_module(dev);
1171 if (error)
1172 goto err_main_clocks;
Tony Lindgren93de83a2019-03-21 11:00:21 -07001173 }
Tony Lindgrend8789702019-03-21 11:00:21 -07001174
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001175 ddata->enabled = true;
1176
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001177 sysc_clkdm_allow_idle(ddata);
1178
Tony Lindgrend8789702019-03-21 11:00:21 -07001179 return 0;
1180
1181err_main_clocks:
Tony Lindgren93de83a2019-03-21 11:00:21 -07001182 sysc_disable_main_clocks(ddata);
1183err_opt_clocks:
Tony Lindgrend8789702019-03-21 11:00:21 -07001184 if (sysc_opt_clks_needed(ddata))
1185 sysc_disable_opt_clocks(ddata);
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001186err_allow_idle:
1187 sysc_clkdm_allow_idle(ddata);
Tony Lindgrend8789702019-03-21 11:00:21 -07001188
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001189 return error;
Tony Lindgren0eecc632017-10-10 14:23:43 -07001190}
1191
Tony Lindgrenf5e802032018-10-01 09:33:28 -07001192static int __maybe_unused sysc_noirq_suspend(struct device *dev)
Tony Lindgren62020f22018-02-22 13:59:44 -08001193{
1194 struct sysc *ddata;
1195
1196 ddata = dev_get_drvdata(dev);
1197
Tony Lindgren40d9f912018-09-24 12:16:54 -07001198 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
Tony Lindgrene7420c22018-04-16 10:26:46 -07001199 return 0;
1200
Tony Lindgrenf5e802032018-10-01 09:33:28 -07001201 return pm_runtime_force_suspend(dev);
Tony Lindgren62020f22018-02-22 13:59:44 -08001202}
1203
Tony Lindgrenf5e802032018-10-01 09:33:28 -07001204static int __maybe_unused sysc_noirq_resume(struct device *dev)
Tony Lindgren62020f22018-02-22 13:59:44 -08001205{
1206 struct sysc *ddata;
1207
1208 ddata = dev_get_drvdata(dev);
Tony Lindgrene7420c22018-04-16 10:26:46 -07001209
Tony Lindgren40d9f912018-09-24 12:16:54 -07001210 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
Tony Lindgrene7420c22018-04-16 10:26:46 -07001211 return 0;
1212
Tony Lindgrenf5e802032018-10-01 09:33:28 -07001213 return pm_runtime_force_resume(dev);
Tony Lindgrene7420c22018-04-16 10:26:46 -07001214}
1215
Tony Lindgren0eecc632017-10-10 14:23:43 -07001216static const struct dev_pm_ops sysc_pm_ops = {
Tony Lindgrene7420c22018-04-16 10:26:46 -07001217 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(sysc_noirq_suspend, sysc_noirq_resume)
Tony Lindgren0eecc632017-10-10 14:23:43 -07001218 SET_RUNTIME_PM_OPS(sysc_runtime_suspend,
1219 sysc_runtime_resume,
1220 NULL)
1221};
1222
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001223/* Module revision register based quirks */
1224struct sysc_revision_quirk {
1225 const char *name;
1226 u32 base;
1227 int rev_offset;
1228 int sysc_offset;
1229 int syss_offset;
1230 u32 revision;
1231 u32 revision_mask;
1232 u32 quirks;
1233};
1234
1235#define SYSC_QUIRK(optname, optbase, optrev, optsysc, optsyss, \
1236 optrev_val, optrevmask, optquirkmask) \
1237 { \
1238 .name = (optname), \
1239 .base = (optbase), \
1240 .rev_offset = (optrev), \
1241 .sysc_offset = (optsysc), \
1242 .syss_offset = (optsyss), \
1243 .revision = (optrev_val), \
1244 .revision_mask = (optrevmask), \
1245 .quirks = (optquirkmask), \
1246 }
1247
1248static const struct sysc_revision_quirk sysc_revision_quirks[] = {
1249 /* These drivers need to be fixed to not use pm_runtime_irq_safe() */
Tony Lindgren3a3d8022018-09-27 13:33:58 -07001250 SYSC_QUIRK("gpio", 0, 0, 0x10, 0x114, 0x50600801, 0xffff00ff,
Tony Lindgren09dfe582018-04-16 10:25:52 -07001251 SYSC_QUIRK_LEGACY_IDLE | SYSC_QUIRK_OPT_CLKS_IN_RESET),
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001252 SYSC_QUIRK("sham", 0, 0x100, 0x110, 0x114, 0x40000c03, 0xffffffff,
1253 SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001254 SYSC_QUIRK("smartreflex", 0, -ENODEV, 0x24, -ENODEV, 0x00000000, 0xffffffff,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001255 SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001256 SYSC_QUIRK("smartreflex", 0, -ENODEV, 0x38, -ENODEV, 0x00000000, 0xffffffff,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001257 SYSC_QUIRK_LEGACY_IDLE),
1258 SYSC_QUIRK("timer", 0, 0, 0x10, 0x14, 0x00000015, 0xffffffff,
Tony Lindgren9bd34c62019-01-22 09:03:08 -08001259 0),
Tony Lindgren8cde5d52018-04-16 10:25:15 -07001260 /* Some timers on omap4 and later */
Tony Lindgren590e15c2020-02-24 12:58:03 -08001261 SYSC_QUIRK("timer", 0, 0, 0x10, -ENODEV, 0x50002100, 0xffffffff,
Tony Lindgren072167d2019-01-29 07:53:47 -08001262 0),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001263 SYSC_QUIRK("timer", 0, 0, 0x10, -ENODEV, 0x4fff1301, 0xffff00ff,
Tony Lindgren9bd34c62019-01-22 09:03:08 -08001264 0),
Tony Lindgrenb6a53c42019-05-27 04:51:57 -07001265 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x00000046, 0xffffffff,
1266 SYSC_QUIRK_SWSUP_SIDLE | SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001267 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x00000052, 0xffffffff,
Tony Lindgrenb6a53c42019-05-27 04:51:57 -07001268 SYSC_QUIRK_SWSUP_SIDLE | SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgrend708bb12018-04-16 10:25:32 -07001269 /* Uarts on omap4 and later */
Tony Lindgrenb82beef2018-09-24 12:16:59 -07001270 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x50411e03, 0xffff00ff,
Tony Lindgrenb4a9a7a2019-03-21 13:27:08 -07001271 SYSC_QUIRK_SWSUP_SIDLE_ACT | SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgrenb82beef2018-09-24 12:16:59 -07001272 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x47422e03, 0xffffffff,
Tony Lindgrenb4a9a7a2019-03-21 13:27:08 -07001273 SYSC_QUIRK_SWSUP_SIDLE_ACT | SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgren7e27e5d2018-04-20 14:54:16 -07001274
Tony Lindgrena54275f2019-03-21 11:00:21 -07001275 /* Quirks that need to be set based on the module address */
Tony Lindgren590e15c2020-02-24 12:58:03 -08001276 SYSC_QUIRK("mcpdm", 0x40132000, 0, 0x10, -ENODEV, 0x50000800, 0xffffffff,
Tony Lindgrena54275f2019-03-21 11:00:21 -07001277 SYSC_QUIRK_EXT_OPT_CLOCK | SYSC_QUIRK_NO_RESET_ON_INIT |
1278 SYSC_QUIRK_SWSUP_SIDLE),
1279
Tony Lindgren4e23be42019-06-10 04:48:05 -07001280 /* Quirks that need to be set based on detected module */
Tony Lindgren590e15c2020-02-24 12:58:03 -08001281 SYSC_QUIRK("aess", 0, 0, 0x10, -ENODEV, 0x40000000, 0xffffffff,
Tony Lindgren020003f2019-11-14 08:25:28 -08001282 SYSC_MODULE_QUIRK_AESS),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001283 SYSC_QUIRK("dcan", 0x48480000, 0x20, -ENODEV, -ENODEV, 0xa3170504, 0xffffffff,
Tony Lindgren94f63452019-12-12 08:57:58 -08001284 SYSC_QUIRK_CLKDM_NOAUTO),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001285 SYSC_QUIRK("dwc3", 0x48880000, 0, 0x10, -ENODEV, 0x500a0200, 0xffffffff,
Tony Lindgren94f63452019-12-12 08:57:58 -08001286 SYSC_QUIRK_CLKDM_NOAUTO),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001287 SYSC_QUIRK("dwc3", 0x488c0000, 0, 0x10, -ENODEV, 0x500a0200, 0xffffffff,
Tony Lindgren94f63452019-12-12 08:57:58 -08001288 SYSC_QUIRK_CLKDM_NOAUTO),
Tony Lindgren4e23be42019-06-10 04:48:05 -07001289 SYSC_QUIRK("hdq1w", 0, 0, 0x14, 0x18, 0x00000006, 0xffffffff,
1290 SYSC_MODULE_QUIRK_HDQ1W),
1291 SYSC_QUIRK("hdq1w", 0, 0, 0x14, 0x18, 0x0000000a, 0xffffffff,
1292 SYSC_MODULE_QUIRK_HDQ1W),
1293 SYSC_QUIRK("i2c", 0, 0, 0x20, 0x10, 0x00000036, 0x000000ff,
1294 SYSC_MODULE_QUIRK_I2C),
1295 SYSC_QUIRK("i2c", 0, 0, 0x20, 0x10, 0x0000003c, 0x000000ff,
1296 SYSC_MODULE_QUIRK_I2C),
1297 SYSC_QUIRK("i2c", 0, 0, 0x20, 0x10, 0x00000040, 0x000000ff,
1298 SYSC_MODULE_QUIRK_I2C),
1299 SYSC_QUIRK("i2c", 0, 0, 0x10, 0x90, 0x5040000a, 0xfffff0f0,
1300 SYSC_MODULE_QUIRK_I2C),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001301 SYSC_QUIRK("gpu", 0x50000000, 0x14, -ENODEV, -ENODEV, 0x00010201, 0xffffffff, 0),
1302 SYSC_QUIRK("gpu", 0x50000000, 0xfe00, 0xfe10, -ENODEV, 0x40000000 , 0xffffffff,
Tony Lindgrend7f563d2019-08-14 05:18:16 -07001303 SYSC_MODULE_QUIRK_SGX),
Tony Lindgren03856e92019-10-21 13:36:09 -07001304 SYSC_QUIRK("usb_otg_hs", 0, 0x400, 0x404, 0x408, 0x00000050,
1305 0xffffffff, SYSC_QUIRK_SWSUP_SIDLE | SYSC_QUIRK_SWSUP_MSTANDBY),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001306 SYSC_QUIRK("usb_otg_hs", 0, 0, 0x10, -ENODEV, 0x4ea2080d, 0xffffffff,
Tony Lindgren1819ef22019-10-21 14:15:55 -07001307 SYSC_QUIRK_SWSUP_SIDLE | SYSC_QUIRK_SWSUP_MSTANDBY),
Tony Lindgren4e23be42019-06-10 04:48:05 -07001308 SYSC_QUIRK("wdt", 0, 0, 0x10, 0x14, 0x502a0500, 0xfffff0f0,
1309 SYSC_MODULE_QUIRK_WDT),
Tony Lindgrenc7d86692019-10-17 11:21:44 -07001310 /* Watchdog on am3 and am4 */
1311 SYSC_QUIRK("wdt", 0x44e35000, 0, 0x10, 0x14, 0x502a0500, 0xfffff0f0,
1312 SYSC_MODULE_QUIRK_WDT | SYSC_QUIRK_SWSUP_SIDLE),
Tony Lindgren4e23be42019-06-10 04:48:05 -07001313
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001314#ifdef DEBUG
Tony Lindgren590e15c2020-02-24 12:58:03 -08001315 SYSC_QUIRK("adc", 0, 0, 0x10, -ENODEV, 0x47300001, 0xffffffff, 0),
1316 SYSC_QUIRK("atl", 0, 0, -ENODEV, -ENODEV, 0x0a070100, 0xffffffff, 0),
1317 SYSC_QUIRK("cm", 0, 0, -ENODEV, -ENODEV, 0x40000301, 0xffffffff, 0),
1318 SYSC_QUIRK("control", 0, 0, 0x10, -ENODEV, 0x40000900, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001319 SYSC_QUIRK("cpgmac", 0, 0x1200, 0x1208, 0x1204, 0x4edb1902,
Tony Lindgren23731ea2018-09-24 12:17:09 -07001320 0xffff00f0, 0),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001321 SYSC_QUIRK("dcan", 0, 0x20, -ENODEV, -ENODEV, 0xa3170504, 0xffffffff, 0),
1322 SYSC_QUIRK("dcan", 0, 0x20, -ENODEV, -ENODEV, 0x4edb1902, 0xffffffff, 0),
1323 SYSC_QUIRK("dmic", 0, 0, 0x10, -ENODEV, 0x50010000, 0xffffffff, 0),
1324 SYSC_QUIRK("dwc3", 0, 0, 0x10, -ENODEV, 0x500a0200, 0xffffffff, 0),
Tony Lindgren7edd00f2019-08-14 05:18:16 -07001325 SYSC_QUIRK("d2d", 0x4a0b6000, 0, 0x10, 0x14, 0x00000010, 0xffffffff, 0),
1326 SYSC_QUIRK("d2d", 0x4a0cd000, 0, 0x10, 0x14, 0x00000010, 0xffffffff, 0),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001327 SYSC_QUIRK("epwmss", 0, 0, 0x4, -ENODEV, 0x47400001, 0xffffffff, 0),
1328 SYSC_QUIRK("gpu", 0, 0x1fc00, 0x1fc10, -ENODEV, 0, 0, 0),
1329 SYSC_QUIRK("gpu", 0, 0xfe00, 0xfe10, -ENODEV, 0x40000000 , 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001330 SYSC_QUIRK("hsi", 0, 0, 0x10, 0x14, 0x50043101, 0xffffffff, 0),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001331 SYSC_QUIRK("iss", 0, 0, 0x10, -ENODEV, 0x40000101, 0xffffffff, 0),
1332 SYSC_QUIRK("lcdc", 0, 0, 0x54, -ENODEV, 0x4f201000, 0xffffffff, 0),
1333 SYSC_QUIRK("mcasp", 0, 0, 0x4, -ENODEV, 0x44306302, 0xffffffff, 0),
1334 SYSC_QUIRK("mcasp", 0, 0, 0x4, -ENODEV, 0x44307b02, 0xffffffff, 0),
1335 SYSC_QUIRK("mcbsp", 0, -ENODEV, 0x8c, -ENODEV, 0, 0, 0),
1336 SYSC_QUIRK("mcspi", 0, 0, 0x10, -ENODEV, 0x40300a0b, 0xffff00ff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001337 SYSC_QUIRK("mcspi", 0, 0, 0x110, 0x114, 0x40300a0b, 0xffffffff, 0),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001338 SYSC_QUIRK("mailbox", 0, 0, 0x10, -ENODEV, 0x00000400, 0xffffffff, 0),
1339 SYSC_QUIRK("m3", 0, 0, -ENODEV, -ENODEV, 0x5f580105, 0x0fff0f00, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -07001340 SYSC_QUIRK("ocp2scp", 0, 0, 0x10, 0x14, 0x50060005, 0xfffffff0, 0),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001341 SYSC_QUIRK("ocp2scp", 0, 0, -ENODEV, -ENODEV, 0x50060007, 0xffffffff, 0),
1342 SYSC_QUIRK("padconf", 0, 0, 0x10, -ENODEV, 0x4fff0800, 0xffffffff, 0),
1343 SYSC_QUIRK("padconf", 0, 0, -ENODEV, -ENODEV, 0x40001100, 0xffffffff, 0),
1344 SYSC_QUIRK("prcm", 0, 0, -ENODEV, -ENODEV, 0x40000100, 0xffffffff, 0),
1345 SYSC_QUIRK("prcm", 0, 0, -ENODEV, -ENODEV, 0x00004102, 0xffffffff, 0),
1346 SYSC_QUIRK("prcm", 0, 0, -ENODEV, -ENODEV, 0x40000400, 0xffffffff, 0),
1347 SYSC_QUIRK("scm", 0, 0, 0x10, -ENODEV, 0x40000900, 0xffffffff, 0),
1348 SYSC_QUIRK("scm", 0, 0, -ENODEV, -ENODEV, 0x4e8b0100, 0xffffffff, 0),
1349 SYSC_QUIRK("scm", 0, 0, -ENODEV, -ENODEV, 0x4f000100, 0xffffffff, 0),
1350 SYSC_QUIRK("scm", 0, 0, -ENODEV, -ENODEV, 0x40000900, 0xffffffff, 0),
1351 SYSC_QUIRK("scrm", 0, 0, -ENODEV, -ENODEV, 0x00000010, 0xffffffff, 0),
1352 SYSC_QUIRK("sdio", 0, 0, 0x10, -ENODEV, 0x40202301, 0xffff0ff0, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001353 SYSC_QUIRK("sdio", 0, 0x2fc, 0x110, 0x114, 0x31010000, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -07001354 SYSC_QUIRK("sdma", 0, 0, 0x2c, 0x28, 0x00010900, 0xffffffff, 0),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001355 SYSC_QUIRK("slimbus", 0, 0, 0x10, -ENODEV, 0x40000902, 0xffffffff, 0),
1356 SYSC_QUIRK("slimbus", 0, 0, 0x10, -ENODEV, 0x40002903, 0xffffffff, 0),
1357 SYSC_QUIRK("spinlock", 0, 0, 0x10, -ENODEV, 0x50020000, 0xffffffff, 0),
1358 SYSC_QUIRK("rng", 0, 0x1fe0, 0x1fe4, -ENODEV, 0x00000020, 0xffffffff, 0),
1359 SYSC_QUIRK("rtc", 0, 0x74, 0x78, -ENODEV, 0x4eb01908, 0xffff00f0, 0),
1360 SYSC_QUIRK("timer32k", 0, 0, 0x4, -ENODEV, 0x00000060, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001361 SYSC_QUIRK("usbhstll", 0, 0, 0x10, 0x14, 0x00000004, 0xffffffff, 0),
Tony Lindgrenf0106702018-11-15 14:46:53 -08001362 SYSC_QUIRK("usbhstll", 0, 0, 0x10, 0x14, 0x00000008, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001363 SYSC_QUIRK("usb_host_hs", 0, 0, 0x10, 0x14, 0x50700100, 0xffffffff, 0),
Tony Lindgren590e15c2020-02-24 12:58:03 -08001364 SYSC_QUIRK("usb_host_hs", 0, 0, 0x10, -ENODEV, 0x50700101, 0xffffffff, 0),
1365 SYSC_QUIRK("vfpe", 0, 0, 0x104, -ENODEV, 0x4d001200, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001366#endif
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001367};
1368
Tony Lindgren42b9c5c2019-03-21 11:00:21 -07001369/*
1370 * Early quirks based on module base and register offsets only that are
1371 * needed before the module revision can be read
1372 */
1373static void sysc_init_early_quirks(struct sysc *ddata)
1374{
1375 const struct sysc_revision_quirk *q;
1376 int i;
1377
1378 for (i = 0; i < ARRAY_SIZE(sysc_revision_quirks); i++) {
1379 q = &sysc_revision_quirks[i];
1380
1381 if (!q->base)
1382 continue;
1383
1384 if (q->base != ddata->module_pa)
1385 continue;
1386
Tony Lindgren590e15c2020-02-24 12:58:03 -08001387 if (q->rev_offset != ddata->offsets[SYSC_REVISION])
Tony Lindgren42b9c5c2019-03-21 11:00:21 -07001388 continue;
1389
Tony Lindgren590e15c2020-02-24 12:58:03 -08001390 if (q->sysc_offset != ddata->offsets[SYSC_SYSCONFIG])
Tony Lindgren42b9c5c2019-03-21 11:00:21 -07001391 continue;
1392
Tony Lindgren590e15c2020-02-24 12:58:03 -08001393 if (q->syss_offset != ddata->offsets[SYSC_SYSSTATUS])
Tony Lindgren42b9c5c2019-03-21 11:00:21 -07001394 continue;
1395
1396 ddata->name = q->name;
1397 ddata->cfg.quirks |= q->quirks;
1398 }
1399}
1400
1401/* Quirks that also consider the revision register value */
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001402static void sysc_init_revision_quirks(struct sysc *ddata)
1403{
1404 const struct sysc_revision_quirk *q;
1405 int i;
1406
1407 for (i = 0; i < ARRAY_SIZE(sysc_revision_quirks); i++) {
1408 q = &sysc_revision_quirks[i];
1409
1410 if (q->base && q->base != ddata->module_pa)
1411 continue;
1412
Tony Lindgren590e15c2020-02-24 12:58:03 -08001413 if (q->rev_offset != ddata->offsets[SYSC_REVISION])
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001414 continue;
1415
Tony Lindgren590e15c2020-02-24 12:58:03 -08001416 if (q->sysc_offset != ddata->offsets[SYSC_SYSCONFIG])
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001417 continue;
1418
Tony Lindgren590e15c2020-02-24 12:58:03 -08001419 if (q->syss_offset != ddata->offsets[SYSC_SYSSTATUS])
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001420 continue;
1421
1422 if (q->revision == ddata->revision ||
1423 (q->revision & q->revision_mask) ==
1424 (ddata->revision & q->revision_mask)) {
1425 ddata->name = q->name;
1426 ddata->cfg.quirks |= q->quirks;
1427 }
1428 }
1429}
1430
Tony Lindgren4e23be42019-06-10 04:48:05 -07001431/* 1-wire needs module's internal clocks enabled for reset */
Tony Lindgrenaec551c2020-02-24 12:58:03 -08001432static void sysc_pre_reset_quirk_hdq1w(struct sysc *ddata)
Tony Lindgren4e23be42019-06-10 04:48:05 -07001433{
1434 int offset = 0x0c; /* HDQ_CTRL_STATUS */
1435 u16 val;
1436
1437 val = sysc_read(ddata, offset);
1438 val |= BIT(5);
1439 sysc_write(ddata, offset, val);
1440}
1441
Tony Lindgren020003f2019-11-14 08:25:28 -08001442/* AESS (Audio Engine SubSystem) needs autogating set after enable */
1443static void sysc_module_enable_quirk_aess(struct sysc *ddata)
1444{
1445 int offset = 0x7c; /* AESS_AUTO_GATING_ENABLE */
1446
1447 sysc_write(ddata, offset, 1);
1448}
1449
Tony Lindgrene64c0212020-02-24 12:58:03 -08001450/* I2C needs to be disabled for reset */
Tony Lindgren4e23be42019-06-10 04:48:05 -07001451static void sysc_clk_quirk_i2c(struct sysc *ddata, bool enable)
1452{
1453 int offset;
1454 u16 val;
1455
1456 /* I2C_CON, omap2/3 is different from omap4 and later */
1457 if ((ddata->revision & 0xffffff00) == 0x001f0000)
1458 offset = 0x24;
1459 else
1460 offset = 0xa4;
1461
1462 /* I2C_EN */
1463 val = sysc_read(ddata, offset);
1464 if (enable)
1465 val |= BIT(15);
1466 else
1467 val &= ~BIT(15);
1468 sysc_write(ddata, offset, val);
1469}
1470
Tony Lindgrene64c0212020-02-24 12:58:03 -08001471static void sysc_pre_reset_quirk_i2c(struct sysc *ddata)
Tony Lindgren4e23be42019-06-10 04:48:05 -07001472{
1473 sysc_clk_quirk_i2c(ddata, false);
1474}
1475
Tony Lindgrene64c0212020-02-24 12:58:03 -08001476static void sysc_post_reset_quirk_i2c(struct sysc *ddata)
1477{
1478 sysc_clk_quirk_i2c(ddata, true);
1479}
1480
Tony Lindgrend7f563d2019-08-14 05:18:16 -07001481/* 36xx SGX needs a quirk for to bypass OCP IPG interrupt logic */
1482static void sysc_module_enable_quirk_sgx(struct sysc *ddata)
1483{
1484 int offset = 0xff08; /* OCP_DEBUG_CONFIG */
1485 u32 val = BIT(31); /* THALIA_INT_BYPASS */
1486
1487 sysc_write(ddata, offset, val);
1488}
1489
Tony Lindgren4e23be42019-06-10 04:48:05 -07001490/* Watchdog timer needs a disable sequence after reset */
1491static void sysc_reset_done_quirk_wdt(struct sysc *ddata)
1492{
1493 int wps, spr, error;
1494 u32 val;
1495
1496 wps = 0x34;
1497 spr = 0x48;
1498
1499 sysc_write(ddata, spr, 0xaaaa);
1500 error = readl_poll_timeout(ddata->module_va + wps, val,
1501 !(val & 0x10), 100,
1502 MAX_MODULE_SOFTRESET_WAIT);
1503 if (error)
Tony Lindgrenc7d86692019-10-17 11:21:44 -07001504 dev_warn(ddata->dev, "wdt disable step1 failed\n");
Tony Lindgren4e23be42019-06-10 04:48:05 -07001505
Tony Lindgrenc7d86692019-10-17 11:21:44 -07001506 sysc_write(ddata, spr, 0x5555);
Tony Lindgren4e23be42019-06-10 04:48:05 -07001507 error = readl_poll_timeout(ddata->module_va + wps, val,
1508 !(val & 0x10), 100,
1509 MAX_MODULE_SOFTRESET_WAIT);
1510 if (error)
Tony Lindgrenc7d86692019-10-17 11:21:44 -07001511 dev_warn(ddata->dev, "wdt disable step2 failed\n");
Tony Lindgren4e23be42019-06-10 04:48:05 -07001512}
1513
1514static void sysc_init_module_quirks(struct sysc *ddata)
1515{
1516 if (ddata->legacy_mode || !ddata->name)
1517 return;
1518
1519 if (ddata->cfg.quirks & SYSC_MODULE_QUIRK_HDQ1W) {
Tony Lindgrene64c0212020-02-24 12:58:03 -08001520 ddata->pre_reset_quirk = sysc_pre_reset_quirk_hdq1w;
Tony Lindgren4e23be42019-06-10 04:48:05 -07001521
1522 return;
1523 }
1524
1525 if (ddata->cfg.quirks & SYSC_MODULE_QUIRK_I2C) {
Tony Lindgrene64c0212020-02-24 12:58:03 -08001526 ddata->pre_reset_quirk = sysc_pre_reset_quirk_i2c;
1527 ddata->post_reset_quirk = sysc_post_reset_quirk_i2c;
Tony Lindgren4e23be42019-06-10 04:48:05 -07001528
1529 return;
1530 }
1531
Tony Lindgren020003f2019-11-14 08:25:28 -08001532 if (ddata->cfg.quirks & SYSC_MODULE_QUIRK_AESS)
1533 ddata->module_enable_quirk = sysc_module_enable_quirk_aess;
1534
Tony Lindgrend7f563d2019-08-14 05:18:16 -07001535 if (ddata->cfg.quirks & SYSC_MODULE_QUIRK_SGX)
1536 ddata->module_enable_quirk = sysc_module_enable_quirk_sgx;
1537
Tony Lindgrenc7d86692019-10-17 11:21:44 -07001538 if (ddata->cfg.quirks & SYSC_MODULE_QUIRK_WDT) {
Tony Lindgren4e23be42019-06-10 04:48:05 -07001539 ddata->reset_done_quirk = sysc_reset_done_quirk_wdt;
Tony Lindgrenc7d86692019-10-17 11:21:44 -07001540 ddata->module_disable_quirk = sysc_reset_done_quirk_wdt;
1541 }
Tony Lindgren4e23be42019-06-10 04:48:05 -07001542}
1543
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001544static int sysc_clockdomain_init(struct sysc *ddata)
1545{
1546 struct ti_sysc_platform_data *pdata = dev_get_platdata(ddata->dev);
1547 struct clk *fck = NULL, *ick = NULL;
1548 int error;
1549
1550 if (!pdata || !pdata->init_clockdomain)
1551 return 0;
1552
1553 switch (ddata->nr_clocks) {
1554 case 2:
1555 ick = ddata->clocks[SYSC_ICK];
1556 /* fallthrough */
1557 case 1:
1558 fck = ddata->clocks[SYSC_FCK];
1559 break;
1560 case 0:
1561 return 0;
1562 }
1563
1564 error = pdata->init_clockdomain(ddata->dev, fck, ick, &ddata->cookie);
1565 if (!error || error == -ENODEV)
1566 return 0;
1567
1568 return error;
1569}
1570
Tony Lindgrena3e92e72019-03-21 11:00:21 -07001571/*
1572 * Note that pdata->init_module() typically does a reset first. After
1573 * pdata->init_module() is done, PM runtime can be used for the interconnect
1574 * target module.
1575 */
1576static int sysc_legacy_init(struct sysc *ddata)
1577{
1578 struct ti_sysc_platform_data *pdata = dev_get_platdata(ddata->dev);
1579 int error;
1580
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001581 if (!pdata || !pdata->init_module)
Tony Lindgrena3e92e72019-03-21 11:00:21 -07001582 return 0;
1583
1584 error = pdata->init_module(ddata->dev, ddata->mdata, &ddata->cookie);
1585 if (error == -EEXIST)
1586 error = 0;
1587
1588 return error;
1589}
1590
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001591/*
1592 * Note that the caller must ensure the interconnect target module is enabled
1593 * before calling reset. Otherwise reset will not complete.
1594 */
Faiz Abbas596e7952018-07-09 22:18:39 +05301595static int sysc_reset(struct sysc *ddata)
1596{
YueHaibingc8a738f2019-06-28 12:10:54 +08001597 int sysc_offset, syss_offset, sysc_val, rstval, error = 0;
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001598 u32 sysc_mask, syss_done;
Faiz Abbas596e7952018-07-09 22:18:39 +05301599
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001600 sysc_offset = ddata->offsets[SYSC_SYSCONFIG];
1601 syss_offset = ddata->offsets[SYSC_SYSSTATUS];
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001602
Tony Lindgrenab4d309d2020-02-24 12:58:03 -08001603 if (ddata->legacy_mode ||
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001604 ddata->cap->regbits->srst_shift < 0 ||
Faiz Abbas596e7952018-07-09 22:18:39 +05301605 ddata->cfg.quirks & SYSC_QUIRK_NO_RESET_ON_INIT)
1606 return 0;
1607
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001608 sysc_mask = BIT(ddata->cap->regbits->srst_shift);
Faiz Abbas596e7952018-07-09 22:18:39 +05301609
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001610 if (ddata->cfg.quirks & SYSS_QUIRK_RESETDONE_INVERTED)
1611 syss_done = 0;
1612 else
1613 syss_done = ddata->cfg.syss_mask;
1614
Tony Lindgrene64c0212020-02-24 12:58:03 -08001615 if (ddata->pre_reset_quirk)
1616 ddata->pre_reset_quirk(ddata);
Tony Lindgren4e23be42019-06-10 04:48:05 -07001617
Tony Lindgrenab4d309d2020-02-24 12:58:03 -08001618 if (sysc_offset >= 0) {
1619 sysc_val = sysc_read_sysconfig(ddata);
1620 sysc_val |= sysc_mask;
1621 sysc_write(ddata, sysc_offset, sysc_val);
1622 }
Faiz Abbas596e7952018-07-09 22:18:39 +05301623
Tony Lindgrene709ed72019-12-12 08:20:10 -08001624 if (ddata->cfg.srst_udelay)
1625 usleep_range(ddata->cfg.srst_udelay,
1626 ddata->cfg.srst_udelay * 2);
1627
Tony Lindgrene64c0212020-02-24 12:58:03 -08001628 if (ddata->post_reset_quirk)
1629 ddata->post_reset_quirk(ddata);
Tony Lindgren4e23be42019-06-10 04:48:05 -07001630
Faiz Abbas596e7952018-07-09 22:18:39 +05301631 /* Poll on reset status */
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001632 if (syss_offset >= 0) {
1633 error = readx_poll_timeout(sysc_read_sysstatus, ddata, rstval,
1634 (rstval & ddata->cfg.syss_mask) ==
1635 syss_done,
1636 100, MAX_MODULE_SOFTRESET_WAIT);
Faiz Abbas596e7952018-07-09 22:18:39 +05301637
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001638 } else if (ddata->cfg.quirks & SYSC_QUIRK_RESET_STATUS) {
1639 error = readx_poll_timeout(sysc_read_sysconfig, ddata, rstval,
1640 !(rstval & sysc_mask),
1641 100, MAX_MODULE_SOFTRESET_WAIT);
1642 }
1643
Tony Lindgren4e23be42019-06-10 04:48:05 -07001644 if (ddata->reset_done_quirk)
1645 ddata->reset_done_quirk(ddata);
1646
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001647 return error;
Faiz Abbas596e7952018-07-09 22:18:39 +05301648}
1649
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001650/*
1651 * At this point the module is configured enough to read the revision but
1652 * module may not be completely configured yet to use PM runtime. Enable
1653 * all clocks directly during init to configure the quirks needed for PM
1654 * runtime based on the revision register.
1655 */
Tony Lindgren566a9b052017-12-15 09:41:19 -08001656static int sysc_init_module(struct sysc *ddata)
1657{
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001658 int error = 0;
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001659
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001660 error = sysc_clockdomain_init(ddata);
1661 if (error)
1662 return error;
1663
Tony Lindgrend0989132019-09-05 07:37:22 -07001664 sysc_clkdm_deny_idle(ddata);
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001665
Tony Lindgrend0989132019-09-05 07:37:22 -07001666 /*
1667 * Always enable clocks. The bootloader may or may not have enabled
1668 * the related clocks.
1669 */
1670 error = sysc_enable_opt_clocks(ddata);
1671 if (error)
1672 return error;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001673
Tony Lindgrend0989132019-09-05 07:37:22 -07001674 error = sysc_enable_main_clocks(ddata);
1675 if (error)
1676 goto err_opt_clocks;
Faiz Abbas596e7952018-07-09 22:18:39 +05301677
Tony Lindgrenea5a2e42019-05-27 04:51:56 -07001678 if (!(ddata->cfg.quirks & SYSC_QUIRK_NO_RESET_ON_INIT)) {
Tero Kristodf4f3452019-10-07 15:29:30 +03001679 error = reset_control_deassert(ddata->rsts);
Tony Lindgrenea5a2e42019-05-27 04:51:56 -07001680 if (error)
1681 goto err_main_clocks;
1682 }
1683
Tony Lindgren566a9b052017-12-15 09:41:19 -08001684 ddata->revision = sysc_read_revision(ddata);
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001685 sysc_init_revision_quirks(ddata);
Tony Lindgren4e23be42019-06-10 04:48:05 -07001686 sysc_init_module_quirks(ddata);
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001687
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001688 if (ddata->legacy_mode) {
1689 error = sysc_legacy_init(ddata);
1690 if (error)
Tero Kristocdc56c12019-10-07 15:29:31 +03001691 goto err_reset;
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001692 }
1693
Tony Lindgrend0989132019-09-05 07:37:22 -07001694 if (!ddata->legacy_mode) {
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001695 error = sysc_enable_module(ddata->dev);
1696 if (error)
Tero Kristocdc56c12019-10-07 15:29:31 +03001697 goto err_reset;
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001698 }
Tony Lindgrena3e92e72019-03-21 11:00:21 -07001699
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001700 error = sysc_reset(ddata);
1701 if (error)
1702 dev_err(ddata->dev, "Reset failed with %d\n", error);
1703
Tero Kristocdc56c12019-10-07 15:29:31 +03001704 if (error && !ddata->legacy_mode)
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001705 sysc_disable_module(ddata->dev);
1706
Tero Kristocdc56c12019-10-07 15:29:31 +03001707err_reset:
1708 if (error && !(ddata->cfg.quirks & SYSC_QUIRK_NO_RESET_ON_INIT))
1709 reset_control_assert(ddata->rsts);
1710
Tony Lindgrena3e92e72019-03-21 11:00:21 -07001711err_main_clocks:
Tero Kristocdc56c12019-10-07 15:29:31 +03001712 if (error)
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001713 sysc_disable_main_clocks(ddata);
1714err_opt_clocks:
Tony Lindgrend0989132019-09-05 07:37:22 -07001715 /* No re-enable of clockdomain autoidle to prevent module autoidle */
Tero Kristocdc56c12019-10-07 15:29:31 +03001716 if (error) {
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001717 sysc_disable_opt_clocks(ddata);
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001718 sysc_clkdm_allow_idle(ddata);
1719 }
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001720
1721 return error;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001722}
1723
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08001724static int sysc_init_sysc_mask(struct sysc *ddata)
1725{
1726 struct device_node *np = ddata->dev->of_node;
1727 int error;
1728 u32 val;
1729
1730 error = of_property_read_u32(np, "ti,sysc-mask", &val);
1731 if (error)
1732 return 0;
1733
Tony Lindgrene212abd2019-07-22 03:44:52 -07001734 ddata->cfg.sysc_val = val & ddata->cap->sysc_mask;
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08001735
1736 return 0;
1737}
1738
1739static int sysc_init_idlemode(struct sysc *ddata, u8 *idlemodes,
1740 const char *name)
1741{
1742 struct device_node *np = ddata->dev->of_node;
1743 struct property *prop;
1744 const __be32 *p;
1745 u32 val;
1746
1747 of_property_for_each_u32(np, name, prop, p, val) {
1748 if (val >= SYSC_NR_IDLEMODES) {
1749 dev_err(ddata->dev, "invalid idlemode: %i\n", val);
1750 return -EINVAL;
1751 }
1752 *idlemodes |= (1 << val);
1753 }
1754
1755 return 0;
1756}
1757
1758static int sysc_init_idlemodes(struct sysc *ddata)
1759{
1760 int error;
1761
1762 error = sysc_init_idlemode(ddata, &ddata->cfg.midlemodes,
1763 "ti,sysc-midle");
1764 if (error)
1765 return error;
1766
1767 error = sysc_init_idlemode(ddata, &ddata->cfg.sidlemodes,
1768 "ti,sysc-sidle");
1769 if (error)
1770 return error;
1771
1772 return 0;
1773}
1774
1775/*
1776 * Only some devices on omap4 and later have SYSCONFIG reset done
1777 * bit. We can detect this if there is no SYSSTATUS at all, or the
1778 * SYSTATUS bit 0 is not used. Note that some SYSSTATUS registers
1779 * have multiple bits for the child devices like OHCI and EHCI.
1780 * Depends on SYSC being parsed first.
1781 */
1782static int sysc_init_syss_mask(struct sysc *ddata)
1783{
1784 struct device_node *np = ddata->dev->of_node;
1785 int error;
1786 u32 val;
1787
1788 error = of_property_read_u32(np, "ti,syss-mask", &val);
1789 if (error) {
1790 if ((ddata->cap->type == TI_SYSC_OMAP4 ||
1791 ddata->cap->type == TI_SYSC_OMAP4_TIMER) &&
1792 (ddata->cfg.sysc_val & SYSC_OMAP4_SOFTRESET))
1793 ddata->cfg.quirks |= SYSC_QUIRK_RESET_STATUS;
1794
1795 return 0;
1796 }
1797
1798 if (!(val & 1) && (ddata->cfg.sysc_val & SYSC_OMAP4_SOFTRESET))
1799 ddata->cfg.quirks |= SYSC_QUIRK_RESET_STATUS;
1800
1801 ddata->cfg.syss_mask = val;
1802
1803 return 0;
1804}
1805
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001806/*
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001807 * Many child device drivers need to have fck and opt clocks available
1808 * to get the clock rate for device internal configuration etc.
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001809 */
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001810static int sysc_child_add_named_clock(struct sysc *ddata,
1811 struct device *child,
1812 const char *name)
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001813{
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001814 struct clk *clk;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001815 struct clk_lookup *l;
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001816 int error = 0;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001817
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001818 if (!name)
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001819 return 0;
1820
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001821 clk = clk_get(child, name);
1822 if (!IS_ERR(clk)) {
Markus Elfringcb6cfe22019-11-06 19:12:30 +01001823 error = -EEXIST;
1824 goto put_clk;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001825 }
1826
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001827 clk = clk_get(ddata->dev, name);
1828 if (IS_ERR(clk))
1829 return -ENODEV;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001830
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001831 l = clkdev_create(clk, name, dev_name(child));
1832 if (!l)
1833 error = -ENOMEM;
Markus Elfringcb6cfe22019-11-06 19:12:30 +01001834put_clk:
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001835 clk_put(clk);
1836
1837 return error;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001838}
1839
Tony Lindgren09dfe582018-04-16 10:25:52 -07001840static int sysc_child_add_clocks(struct sysc *ddata,
1841 struct device *child)
1842{
1843 int i, error;
1844
1845 for (i = 0; i < ddata->nr_clocks; i++) {
1846 error = sysc_child_add_named_clock(ddata,
1847 child,
1848 ddata->clock_roles[i]);
1849 if (error && error != -EEXIST) {
1850 dev_err(ddata->dev, "could not add child clock %s: %i\n",
1851 ddata->clock_roles[i], error);
1852
1853 return error;
1854 }
1855 }
1856
1857 return 0;
1858}
1859
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001860static struct device_type sysc_device_type = {
1861};
1862
1863static struct sysc *sysc_child_to_parent(struct device *dev)
1864{
1865 struct device *parent = dev->parent;
1866
1867 if (!parent || parent->type != &sysc_device_type)
1868 return NULL;
1869
1870 return dev_get_drvdata(parent);
1871}
1872
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001873static int __maybe_unused sysc_child_runtime_suspend(struct device *dev)
1874{
1875 struct sysc *ddata;
1876 int error;
1877
1878 ddata = sysc_child_to_parent(dev);
1879
1880 error = pm_generic_runtime_suspend(dev);
1881 if (error)
1882 return error;
1883
1884 if (!ddata->enabled)
1885 return 0;
1886
1887 return sysc_runtime_suspend(ddata->dev);
1888}
1889
1890static int __maybe_unused sysc_child_runtime_resume(struct device *dev)
1891{
1892 struct sysc *ddata;
1893 int error;
1894
1895 ddata = sysc_child_to_parent(dev);
1896
1897 if (!ddata->enabled) {
1898 error = sysc_runtime_resume(ddata->dev);
1899 if (error < 0)
1900 dev_err(ddata->dev,
1901 "%s error: %i\n", __func__, error);
1902 }
1903
1904 return pm_generic_runtime_resume(dev);
1905}
1906
1907#ifdef CONFIG_PM_SLEEP
1908static int sysc_child_suspend_noirq(struct device *dev)
1909{
1910 struct sysc *ddata;
1911 int error;
1912
1913 ddata = sysc_child_to_parent(dev);
1914
Tony Lindgrenef55f822018-04-16 10:27:15 -07001915 dev_dbg(ddata->dev, "%s %s\n", __func__,
1916 ddata->name ? ddata->name : "");
1917
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001918 error = pm_generic_suspend_noirq(dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001919 if (error) {
1920 dev_err(dev, "%s error at %i: %i\n",
1921 __func__, __LINE__, error);
1922
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001923 return error;
Tony Lindgrenef55f822018-04-16 10:27:15 -07001924 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001925
1926 if (!pm_runtime_status_suspended(dev)) {
1927 error = pm_generic_runtime_suspend(dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001928 if (error) {
Tony Lindgrenf9490782018-09-28 15:21:50 -07001929 dev_dbg(dev, "%s busy at %i: %i\n",
1930 __func__, __LINE__, error);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001931
Tony Lindgren4f3530f2018-08-08 01:07:06 -07001932 return 0;
Tony Lindgrenef55f822018-04-16 10:27:15 -07001933 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001934
1935 error = sysc_runtime_suspend(ddata->dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001936 if (error) {
1937 dev_err(dev, "%s error at %i: %i\n",
1938 __func__, __LINE__, error);
1939
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001940 return error;
Tony Lindgrenef55f822018-04-16 10:27:15 -07001941 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001942
1943 ddata->child_needs_resume = true;
1944 }
1945
1946 return 0;
1947}
1948
1949static int sysc_child_resume_noirq(struct device *dev)
1950{
1951 struct sysc *ddata;
1952 int error;
1953
1954 ddata = sysc_child_to_parent(dev);
1955
Tony Lindgrenef55f822018-04-16 10:27:15 -07001956 dev_dbg(ddata->dev, "%s %s\n", __func__,
1957 ddata->name ? ddata->name : "");
1958
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001959 if (ddata->child_needs_resume) {
1960 ddata->child_needs_resume = false;
1961
1962 error = sysc_runtime_resume(ddata->dev);
1963 if (error)
1964 dev_err(ddata->dev,
1965 "%s runtime resume error: %i\n",
1966 __func__, error);
1967
1968 error = pm_generic_runtime_resume(dev);
1969 if (error)
1970 dev_err(ddata->dev,
1971 "%s generic runtime resume: %i\n",
1972 __func__, error);
1973 }
1974
1975 return pm_generic_resume_noirq(dev);
1976}
1977#endif
1978
Tony Lindgrenb7182b42019-03-21 11:00:21 -07001979static struct dev_pm_domain sysc_child_pm_domain = {
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001980 .ops = {
1981 SET_RUNTIME_PM_OPS(sysc_child_runtime_suspend,
1982 sysc_child_runtime_resume,
1983 NULL)
1984 USE_PLATFORM_PM_SLEEP_OPS
1985 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(sysc_child_suspend_noirq,
1986 sysc_child_resume_noirq)
1987 }
1988};
1989
1990/**
1991 * sysc_legacy_idle_quirk - handle children in omap_device compatible way
1992 * @ddata: device driver data
1993 * @child: child device driver
1994 *
1995 * Allow idle for child devices as done with _od_runtime_suspend().
1996 * Otherwise many child devices will not idle because of the permanent
1997 * parent usecount set in pm_runtime_irq_safe().
1998 *
1999 * Note that the long term solution is to just modify the child device
2000 * drivers to not set pm_runtime_irq_safe() and then this can be just
2001 * dropped.
2002 */
2003static void sysc_legacy_idle_quirk(struct sysc *ddata, struct device *child)
2004{
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002005 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
2006 dev_pm_domain_set(child, &sysc_child_pm_domain);
2007}
2008
Tony Lindgren2c355ff2018-02-22 13:58:03 -08002009static int sysc_notifier_call(struct notifier_block *nb,
2010 unsigned long event, void *device)
2011{
2012 struct device *dev = device;
2013 struct sysc *ddata;
2014 int error;
2015
2016 ddata = sysc_child_to_parent(dev);
2017 if (!ddata)
2018 return NOTIFY_DONE;
2019
2020 switch (event) {
2021 case BUS_NOTIFY_ADD_DEVICE:
Tony Lindgren09dfe582018-04-16 10:25:52 -07002022 error = sysc_child_add_clocks(ddata, dev);
2023 if (error)
2024 return error;
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002025 sysc_legacy_idle_quirk(ddata, dev);
Tony Lindgren2c355ff2018-02-22 13:58:03 -08002026 break;
2027 default:
2028 break;
2029 }
2030
2031 return NOTIFY_DONE;
2032}
2033
2034static struct notifier_block sysc_nb = {
2035 .notifier_call = sysc_notifier_call,
2036};
2037
Tony Lindgren566a9b052017-12-15 09:41:19 -08002038/* Device tree configured quirks */
2039struct sysc_dts_quirk {
2040 const char *name;
2041 u32 mask;
2042};
2043
2044static const struct sysc_dts_quirk sysc_dts_quirks[] = {
2045 { .name = "ti,no-idle-on-init",
2046 .mask = SYSC_QUIRK_NO_IDLE_ON_INIT, },
2047 { .name = "ti,no-reset-on-init",
2048 .mask = SYSC_QUIRK_NO_RESET_ON_INIT, },
Tony Lindgren386cb762019-03-22 07:49:30 -07002049 { .name = "ti,no-idle",
2050 .mask = SYSC_QUIRK_NO_IDLE, },
Tony Lindgren566a9b052017-12-15 09:41:19 -08002051};
2052
Tony Lindgren4014c082018-12-10 14:11:26 -08002053static void sysc_parse_dts_quirks(struct sysc *ddata, struct device_node *np,
2054 bool is_child)
Tony Lindgren566a9b052017-12-15 09:41:19 -08002055{
Tony Lindgren566a9b052017-12-15 09:41:19 -08002056 const struct property *prop;
Tony Lindgren4014c082018-12-10 14:11:26 -08002057 int i, len;
Tony Lindgren566a9b052017-12-15 09:41:19 -08002058
2059 for (i = 0; i < ARRAY_SIZE(sysc_dts_quirks); i++) {
Tony Lindgren4014c082018-12-10 14:11:26 -08002060 const char *name = sysc_dts_quirks[i].name;
2061
2062 prop = of_get_property(np, name, &len);
Tony Lindgren566a9b052017-12-15 09:41:19 -08002063 if (!prop)
Tony Lindgrend39b6ea2018-02-15 09:18:55 -08002064 continue;
Tony Lindgren566a9b052017-12-15 09:41:19 -08002065
2066 ddata->cfg.quirks |= sysc_dts_quirks[i].mask;
Tony Lindgren4014c082018-12-10 14:11:26 -08002067 if (is_child) {
2068 dev_warn(ddata->dev,
2069 "dts flag should be at module level for %s\n",
2070 name);
2071 }
Tony Lindgren566a9b052017-12-15 09:41:19 -08002072 }
Tony Lindgren4014c082018-12-10 14:11:26 -08002073}
Tony Lindgren566a9b052017-12-15 09:41:19 -08002074
Tony Lindgren4014c082018-12-10 14:11:26 -08002075static int sysc_init_dts_quirks(struct sysc *ddata)
2076{
2077 struct device_node *np = ddata->dev->of_node;
2078 int error;
2079 u32 val;
2080
2081 ddata->legacy_mode = of_get_property(np, "ti,hwmods", NULL);
2082
2083 sysc_parse_dts_quirks(ddata, np, false);
Tony Lindgren566a9b052017-12-15 09:41:19 -08002084 error = of_property_read_u32(np, "ti,sysc-delay-us", &val);
2085 if (!error) {
2086 if (val > 255) {
2087 dev_warn(ddata->dev, "bad ti,sysc-delay-us: %i\n",
2088 val);
2089 }
2090
2091 ddata->cfg.srst_udelay = (u8)val;
2092 }
2093
2094 return 0;
2095}
2096
Tony Lindgren0eecc632017-10-10 14:23:43 -07002097static void sysc_unprepare(struct sysc *ddata)
2098{
2099 int i;
2100
Tony Lindgrenaaa29bb2019-03-21 11:00:21 -07002101 if (!ddata->clocks)
2102 return;
2103
Tony Lindgren0eecc632017-10-10 14:23:43 -07002104 for (i = 0; i < SYSC_MAX_CLOCKS; i++) {
2105 if (!IS_ERR_OR_NULL(ddata->clocks[i]))
2106 clk_unprepare(ddata->clocks[i]);
2107 }
2108}
2109
Tony Lindgren70a65242017-12-15 09:41:09 -08002110/*
2111 * Common sysc register bits found on omap2, also known as type1
2112 */
2113static const struct sysc_regbits sysc_regbits_omap2 = {
2114 .dmadisable_shift = -ENODEV,
2115 .midle_shift = 12,
2116 .sidle_shift = 3,
2117 .clkact_shift = 8,
2118 .emufree_shift = 5,
2119 .enwkup_shift = 2,
2120 .srst_shift = 1,
2121 .autoidle_shift = 0,
2122};
2123
2124static const struct sysc_capabilities sysc_omap2 = {
2125 .type = TI_SYSC_OMAP2,
2126 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY | SYSC_OMAP2_EMUFREE |
2127 SYSC_OMAP2_ENAWAKEUP | SYSC_OMAP2_SOFTRESET |
2128 SYSC_OMAP2_AUTOIDLE,
2129 .regbits = &sysc_regbits_omap2,
2130};
2131
2132/* All omap2 and 3 timers, and timers 1, 2 & 10 on omap 4 and 5 */
2133static const struct sysc_capabilities sysc_omap2_timer = {
2134 .type = TI_SYSC_OMAP2_TIMER,
2135 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY | SYSC_OMAP2_EMUFREE |
2136 SYSC_OMAP2_ENAWAKEUP | SYSC_OMAP2_SOFTRESET |
2137 SYSC_OMAP2_AUTOIDLE,
2138 .regbits = &sysc_regbits_omap2,
2139 .mod_quirks = SYSC_QUIRK_USE_CLOCKACT,
2140};
2141
2142/*
2143 * SHAM2 (SHA1/MD5) sysc found on omap3, a variant of sysc_regbits_omap2
2144 * with different sidle position
2145 */
2146static const struct sysc_regbits sysc_regbits_omap3_sham = {
2147 .dmadisable_shift = -ENODEV,
2148 .midle_shift = -ENODEV,
2149 .sidle_shift = 4,
2150 .clkact_shift = -ENODEV,
2151 .enwkup_shift = -ENODEV,
2152 .srst_shift = 1,
2153 .autoidle_shift = 0,
2154 .emufree_shift = -ENODEV,
2155};
2156
2157static const struct sysc_capabilities sysc_omap3_sham = {
2158 .type = TI_SYSC_OMAP3_SHAM,
2159 .sysc_mask = SYSC_OMAP2_SOFTRESET | SYSC_OMAP2_AUTOIDLE,
2160 .regbits = &sysc_regbits_omap3_sham,
2161};
2162
2163/*
2164 * AES register bits found on omap3 and later, a variant of
2165 * sysc_regbits_omap2 with different sidle position
2166 */
2167static const struct sysc_regbits sysc_regbits_omap3_aes = {
2168 .dmadisable_shift = -ENODEV,
2169 .midle_shift = -ENODEV,
2170 .sidle_shift = 6,
2171 .clkact_shift = -ENODEV,
2172 .enwkup_shift = -ENODEV,
2173 .srst_shift = 1,
2174 .autoidle_shift = 0,
2175 .emufree_shift = -ENODEV,
2176};
2177
2178static const struct sysc_capabilities sysc_omap3_aes = {
2179 .type = TI_SYSC_OMAP3_AES,
2180 .sysc_mask = SYSC_OMAP2_SOFTRESET | SYSC_OMAP2_AUTOIDLE,
2181 .regbits = &sysc_regbits_omap3_aes,
2182};
2183
2184/*
2185 * Common sysc register bits found on omap4, also known as type2
2186 */
2187static const struct sysc_regbits sysc_regbits_omap4 = {
2188 .dmadisable_shift = 16,
2189 .midle_shift = 4,
2190 .sidle_shift = 2,
2191 .clkact_shift = -ENODEV,
2192 .enwkup_shift = -ENODEV,
2193 .emufree_shift = 1,
2194 .srst_shift = 0,
2195 .autoidle_shift = -ENODEV,
2196};
2197
2198static const struct sysc_capabilities sysc_omap4 = {
2199 .type = TI_SYSC_OMAP4,
2200 .sysc_mask = SYSC_OMAP4_DMADISABLE | SYSC_OMAP4_FREEEMU |
2201 SYSC_OMAP4_SOFTRESET,
2202 .regbits = &sysc_regbits_omap4,
2203};
2204
2205static const struct sysc_capabilities sysc_omap4_timer = {
2206 .type = TI_SYSC_OMAP4_TIMER,
2207 .sysc_mask = SYSC_OMAP4_DMADISABLE | SYSC_OMAP4_FREEEMU |
2208 SYSC_OMAP4_SOFTRESET,
2209 .regbits = &sysc_regbits_omap4,
2210};
2211
2212/*
2213 * Common sysc register bits found on omap4, also known as type3
2214 */
2215static const struct sysc_regbits sysc_regbits_omap4_simple = {
2216 .dmadisable_shift = -ENODEV,
2217 .midle_shift = 2,
2218 .sidle_shift = 0,
2219 .clkact_shift = -ENODEV,
2220 .enwkup_shift = -ENODEV,
2221 .srst_shift = -ENODEV,
2222 .emufree_shift = -ENODEV,
2223 .autoidle_shift = -ENODEV,
2224};
2225
2226static const struct sysc_capabilities sysc_omap4_simple = {
2227 .type = TI_SYSC_OMAP4_SIMPLE,
2228 .regbits = &sysc_regbits_omap4_simple,
2229};
2230
2231/*
2232 * SmartReflex sysc found on omap34xx
2233 */
2234static const struct sysc_regbits sysc_regbits_omap34xx_sr = {
2235 .dmadisable_shift = -ENODEV,
2236 .midle_shift = -ENODEV,
2237 .sidle_shift = -ENODEV,
2238 .clkact_shift = 20,
2239 .enwkup_shift = -ENODEV,
2240 .srst_shift = -ENODEV,
2241 .emufree_shift = -ENODEV,
2242 .autoidle_shift = -ENODEV,
2243};
2244
2245static const struct sysc_capabilities sysc_34xx_sr = {
2246 .type = TI_SYSC_OMAP34XX_SR,
2247 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY,
2248 .regbits = &sysc_regbits_omap34xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002249 .mod_quirks = SYSC_QUIRK_USE_CLOCKACT | SYSC_QUIRK_UNCACHED |
2250 SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08002251};
2252
2253/*
2254 * SmartReflex sysc found on omap36xx and later
2255 */
2256static const struct sysc_regbits sysc_regbits_omap36xx_sr = {
2257 .dmadisable_shift = -ENODEV,
2258 .midle_shift = -ENODEV,
2259 .sidle_shift = 24,
2260 .clkact_shift = -ENODEV,
2261 .enwkup_shift = 26,
2262 .srst_shift = -ENODEV,
2263 .emufree_shift = -ENODEV,
2264 .autoidle_shift = -ENODEV,
2265};
2266
2267static const struct sysc_capabilities sysc_36xx_sr = {
2268 .type = TI_SYSC_OMAP36XX_SR,
Tony Lindgren3267c082018-01-22 09:32:53 -08002269 .sysc_mask = SYSC_OMAP3_SR_ENAWAKEUP,
Tony Lindgren70a65242017-12-15 09:41:09 -08002270 .regbits = &sysc_regbits_omap36xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002271 .mod_quirks = SYSC_QUIRK_UNCACHED | SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08002272};
2273
2274static const struct sysc_capabilities sysc_omap4_sr = {
2275 .type = TI_SYSC_OMAP4_SR,
2276 .regbits = &sysc_regbits_omap36xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002277 .mod_quirks = SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08002278};
2279
2280/*
2281 * McASP register bits found on omap4 and later
2282 */
2283static const struct sysc_regbits sysc_regbits_omap4_mcasp = {
2284 .dmadisable_shift = -ENODEV,
2285 .midle_shift = -ENODEV,
2286 .sidle_shift = 0,
2287 .clkact_shift = -ENODEV,
2288 .enwkup_shift = -ENODEV,
2289 .srst_shift = -ENODEV,
2290 .emufree_shift = -ENODEV,
2291 .autoidle_shift = -ENODEV,
2292};
2293
2294static const struct sysc_capabilities sysc_omap4_mcasp = {
2295 .type = TI_SYSC_OMAP4_MCASP,
2296 .regbits = &sysc_regbits_omap4_mcasp,
Tony Lindgren2c63a832018-11-15 14:46:53 -08002297 .mod_quirks = SYSC_QUIRK_OPT_CLKS_NEEDED,
2298};
2299
2300/*
2301 * McASP found on dra7 and later
2302 */
2303static const struct sysc_capabilities sysc_dra7_mcasp = {
2304 .type = TI_SYSC_OMAP4_SIMPLE,
2305 .regbits = &sysc_regbits_omap4_simple,
2306 .mod_quirks = SYSC_QUIRK_OPT_CLKS_NEEDED,
Tony Lindgren70a65242017-12-15 09:41:09 -08002307};
2308
2309/*
2310 * FS USB host found on omap4 and later
2311 */
2312static const struct sysc_regbits sysc_regbits_omap4_usb_host_fs = {
2313 .dmadisable_shift = -ENODEV,
2314 .midle_shift = -ENODEV,
2315 .sidle_shift = 24,
2316 .clkact_shift = -ENODEV,
2317 .enwkup_shift = 26,
2318 .srst_shift = -ENODEV,
2319 .emufree_shift = -ENODEV,
2320 .autoidle_shift = -ENODEV,
2321};
2322
2323static const struct sysc_capabilities sysc_omap4_usb_host_fs = {
2324 .type = TI_SYSC_OMAP4_USB_HOST_FS,
2325 .sysc_mask = SYSC_OMAP2_ENAWAKEUP,
2326 .regbits = &sysc_regbits_omap4_usb_host_fs,
2327};
2328
Faiz Abbas7f35e632018-07-09 22:18:38 +05302329static const struct sysc_regbits sysc_regbits_dra7_mcan = {
2330 .dmadisable_shift = -ENODEV,
2331 .midle_shift = -ENODEV,
2332 .sidle_shift = -ENODEV,
2333 .clkact_shift = -ENODEV,
2334 .enwkup_shift = 4,
2335 .srst_shift = 0,
2336 .emufree_shift = -ENODEV,
2337 .autoidle_shift = -ENODEV,
2338};
2339
2340static const struct sysc_capabilities sysc_dra7_mcan = {
2341 .type = TI_SYSC_DRA7_MCAN,
2342 .sysc_mask = SYSC_DRA7_MCAN_ENAWAKEUP | SYSC_OMAP4_SOFTRESET,
2343 .regbits = &sysc_regbits_dra7_mcan,
Tony Lindgrene0db94f2019-05-27 04:51:53 -07002344 .mod_quirks = SYSS_QUIRK_RESETDONE_INVERTED,
Faiz Abbas7f35e632018-07-09 22:18:38 +05302345};
2346
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002347static int sysc_init_pdata(struct sysc *ddata)
2348{
2349 struct ti_sysc_platform_data *pdata = dev_get_platdata(ddata->dev);
Tony Lindgrena3e92e72019-03-21 11:00:21 -07002350 struct ti_sysc_module_data *mdata;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002351
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07002352 if (!pdata)
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002353 return 0;
2354
Tony Lindgrena3e92e72019-03-21 11:00:21 -07002355 mdata = devm_kzalloc(ddata->dev, sizeof(*mdata), GFP_KERNEL);
2356 if (!mdata)
2357 return -ENOMEM;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002358
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07002359 if (ddata->legacy_mode) {
2360 mdata->name = ddata->legacy_mode;
2361 mdata->module_pa = ddata->module_pa;
2362 mdata->module_size = ddata->module_size;
2363 mdata->offsets = ddata->offsets;
2364 mdata->nr_offsets = SYSC_MAX_REGS;
2365 mdata->cap = ddata->cap;
2366 mdata->cfg = &ddata->cfg;
2367 }
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002368
Tony Lindgrena3e92e72019-03-21 11:00:21 -07002369 ddata->mdata = mdata;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002370
Tony Lindgrena3e92e72019-03-21 11:00:21 -07002371 return 0;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002372}
2373
Tony Lindgren70a65242017-12-15 09:41:09 -08002374static int sysc_init_match(struct sysc *ddata)
2375{
2376 const struct sysc_capabilities *cap;
2377
2378 cap = of_device_get_match_data(ddata->dev);
2379 if (!cap)
2380 return -EINVAL;
2381
2382 ddata->cap = cap;
2383 if (ddata->cap)
2384 ddata->cfg.quirks |= ddata->cap->mod_quirks;
2385
2386 return 0;
2387}
2388
Tony Lindgren76f0f772018-02-23 08:28:45 -08002389static void ti_sysc_idle(struct work_struct *work)
2390{
2391 struct sysc *ddata;
2392
2393 ddata = container_of(work, struct sysc, idle_work.work);
2394
Tony Lindgrend0989132019-09-05 07:37:22 -07002395 /*
2396 * One time decrement of clock usage counts if left on from init.
2397 * Note that we disable opt clocks unconditionally in this case
2398 * as they are enabled unconditionally during init without
2399 * considering sysc_opt_clks_needed() at that point.
2400 */
2401 if (ddata->cfg.quirks & (SYSC_QUIRK_NO_IDLE |
2402 SYSC_QUIRK_NO_IDLE_ON_INIT)) {
Tony Lindgrend0989132019-09-05 07:37:22 -07002403 sysc_disable_main_clocks(ddata);
2404 sysc_disable_opt_clocks(ddata);
2405 sysc_clkdm_allow_idle(ddata);
2406 }
2407
2408 /* Keep permanent PM runtime usage count for SYSC_QUIRK_NO_IDLE */
2409 if (ddata->cfg.quirks & SYSC_QUIRK_NO_IDLE)
2410 return;
2411
2412 /*
2413 * Decrement PM runtime usage count for SYSC_QUIRK_NO_IDLE_ON_INIT
2414 * and SYSC_QUIRK_NO_RESET_ON_INIT
2415 */
Tony Lindgren76f0f772018-02-23 08:28:45 -08002416 if (pm_runtime_active(ddata->dev))
2417 pm_runtime_put_sync(ddata->dev);
2418}
2419
Tony Lindgrenfeaa8ba2020-02-24 12:58:03 -08002420/*
2421 * SoC model and features detection. Only needed for SoCs that need
2422 * special handling for quirks, no need to list others.
2423 */
2424static const struct soc_device_attribute sysc_soc_match[] = {
2425 SOC_FLAG("OMAP242*", SOC_2420),
2426 SOC_FLAG("OMAP243*", SOC_2430),
2427 SOC_FLAG("OMAP3[45]*", SOC_3430),
2428 SOC_FLAG("OMAP3[67]*", SOC_3630),
2429 SOC_FLAG("OMAP443*", SOC_4430),
2430 SOC_FLAG("OMAP446*", SOC_4460),
2431 SOC_FLAG("OMAP447*", SOC_4470),
2432 SOC_FLAG("OMAP54*", SOC_5430),
2433 SOC_FLAG("AM433", SOC_AM3),
2434 SOC_FLAG("AM43*", SOC_AM4),
2435 SOC_FLAG("DRA7*", SOC_DRA7),
2436
2437 { /* sentinel */ },
2438};
2439
2440/*
2441 * List of SoCs variants with disabled features. By default we assume all
2442 * devices in the device tree are available so no need to list those SoCs.
2443 */
2444static const struct soc_device_attribute sysc_soc_feat_match[] = {
2445 /* OMAP3430/3530 and AM3517 variants with some accelerators disabled */
2446 SOC_FLAG("AM3505", DIS_SGX),
2447 SOC_FLAG("OMAP3525", DIS_SGX),
2448 SOC_FLAG("OMAP3515", DIS_IVA | DIS_SGX),
2449 SOC_FLAG("OMAP3503", DIS_ISP | DIS_IVA | DIS_SGX),
2450
2451 /* OMAP3630/DM3730 variants with some accelerators disabled */
2452 SOC_FLAG("AM3703", DIS_IVA | DIS_SGX),
2453 SOC_FLAG("DM3725", DIS_SGX),
2454 SOC_FLAG("OMAP3611", DIS_ISP | DIS_IVA | DIS_SGX),
2455 SOC_FLAG("OMAP3615/AM3715", DIS_IVA),
2456 SOC_FLAG("OMAP3621", DIS_ISP),
2457
2458 { /* sentinel */ },
2459};
2460
2461static int sysc_add_disabled(unsigned long base)
2462{
2463 struct sysc_address *disabled_module;
2464
2465 disabled_module = kzalloc(sizeof(*disabled_module), GFP_KERNEL);
2466 if (!disabled_module)
2467 return -ENOMEM;
2468
2469 disabled_module->base = base;
2470
2471 mutex_lock(&sysc_soc->list_lock);
2472 list_add(&disabled_module->node, &sysc_soc->disabled_modules);
2473 mutex_unlock(&sysc_soc->list_lock);
2474
2475 return 0;
2476}
2477
2478/*
2479 * One time init to detect the booted SoC and disable unavailable features.
2480 * Note that we initialize static data shared across all ti-sysc instances
2481 * so ddata is only used for SoC type. This can be called from module_init
2482 * once we no longer need to rely on platform data.
2483 */
2484static int sysc_init_soc(struct sysc *ddata)
2485{
2486 const struct soc_device_attribute *match;
2487 struct ti_sysc_platform_data *pdata;
2488 unsigned long features = 0;
2489
2490 if (sysc_soc)
2491 return 0;
2492
2493 sysc_soc = kzalloc(sizeof(*sysc_soc), GFP_KERNEL);
2494 if (!sysc_soc)
2495 return -ENOMEM;
2496
2497 mutex_init(&sysc_soc->list_lock);
2498 INIT_LIST_HEAD(&sysc_soc->disabled_modules);
2499 sysc_soc->general_purpose = true;
2500
2501 pdata = dev_get_platdata(ddata->dev);
2502 if (pdata && pdata->soc_type_gp)
2503 sysc_soc->general_purpose = pdata->soc_type_gp();
2504
2505 match = soc_device_match(sysc_soc_match);
2506 if (match && match->data)
2507 sysc_soc->soc = (int)match->data;
2508
2509 match = soc_device_match(sysc_soc_feat_match);
2510 if (!match)
2511 return 0;
2512
2513 if (match->data)
2514 features = (unsigned long)match->data;
2515
2516 /*
2517 * Add disabled devices to the list based on the module base.
2518 * Note that this must be done before we attempt to access the
2519 * device and have module revision checks working.
2520 */
2521 if (features & DIS_ISP)
2522 sysc_add_disabled(0x480bd400);
2523 if (features & DIS_IVA)
2524 sysc_add_disabled(0x5d000000);
2525 if (features & DIS_SGX)
2526 sysc_add_disabled(0x50000000);
2527
2528 return 0;
2529}
2530
2531static void sysc_cleanup_soc(void)
2532{
2533 struct sysc_address *disabled_module;
2534 struct list_head *pos, *tmp;
2535
2536 if (!sysc_soc)
2537 return;
2538
2539 mutex_lock(&sysc_soc->list_lock);
2540 list_for_each_safe(pos, tmp, &sysc_soc->disabled_modules) {
2541 disabled_module = list_entry(pos, struct sysc_address, node);
2542 list_del(pos);
2543 kfree(disabled_module);
2544 }
2545 mutex_unlock(&sysc_soc->list_lock);
2546}
2547
2548static int sysc_check_disabled_devices(struct sysc *ddata)
2549{
2550 struct sysc_address *disabled_module;
2551 struct list_head *pos;
2552 int error = 0;
2553
2554 mutex_lock(&sysc_soc->list_lock);
2555 list_for_each(pos, &sysc_soc->disabled_modules) {
2556 disabled_module = list_entry(pos, struct sysc_address, node);
2557 if (ddata->module_pa == disabled_module->base) {
2558 dev_dbg(ddata->dev, "module disabled for this SoC\n");
2559 error = -ENODEV;
2560 break;
2561 }
2562 }
2563 mutex_unlock(&sysc_soc->list_lock);
2564
2565 return error;
2566}
2567
Tony Lindgrenc4bebea2018-04-16 10:24:54 -07002568static const struct of_device_id sysc_match_table[] = {
2569 { .compatible = "simple-bus", },
2570 { /* sentinel */ },
2571};
2572
Tony Lindgren0eecc632017-10-10 14:23:43 -07002573static int sysc_probe(struct platform_device *pdev)
2574{
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002575 struct ti_sysc_platform_data *pdata = dev_get_platdata(&pdev->dev);
Tony Lindgren0eecc632017-10-10 14:23:43 -07002576 struct sysc *ddata;
2577 int error;
2578
2579 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
2580 if (!ddata)
2581 return -ENOMEM;
2582
2583 ddata->dev = &pdev->dev;
Tony Lindgren566a9b052017-12-15 09:41:19 -08002584 platform_set_drvdata(pdev, ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -07002585
Tony Lindgrenfeaa8ba2020-02-24 12:58:03 -08002586 error = sysc_init_soc(ddata);
2587 if (error)
2588 return error;
2589
Tony Lindgren70a65242017-12-15 09:41:09 -08002590 error = sysc_init_match(ddata);
2591 if (error)
2592 return error;
2593
Tony Lindgren566a9b052017-12-15 09:41:19 -08002594 error = sysc_init_dts_quirks(ddata);
2595 if (error)
Suman Annaa304f482019-06-25 18:33:15 -05002596 return error;
Tony Lindgren566a9b052017-12-15 09:41:19 -08002597
Tony Lindgren0eecc632017-10-10 14:23:43 -07002598 error = sysc_map_and_check_registers(ddata);
2599 if (error)
Suman Annaa304f482019-06-25 18:33:15 -05002600 return error;
Tony Lindgren0eecc632017-10-10 14:23:43 -07002601
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08002602 error = sysc_init_sysc_mask(ddata);
2603 if (error)
Suman Annaa304f482019-06-25 18:33:15 -05002604 return error;
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08002605
2606 error = sysc_init_idlemodes(ddata);
2607 if (error)
Suman Annaa304f482019-06-25 18:33:15 -05002608 return error;
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08002609
2610 error = sysc_init_syss_mask(ddata);
2611 if (error)
Suman Annaa304f482019-06-25 18:33:15 -05002612 return error;
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08002613
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002614 error = sysc_init_pdata(ddata);
2615 if (error)
Suman Annaa304f482019-06-25 18:33:15 -05002616 return error;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002617
Tony Lindgren42b9c5c2019-03-21 11:00:21 -07002618 sysc_init_early_quirks(ddata);
2619
Tony Lindgrenfeaa8ba2020-02-24 12:58:03 -08002620 error = sysc_check_disabled_devices(ddata);
2621 if (error)
2622 return error;
2623
Tony Lindgren42b9c5c2019-03-21 11:00:21 -07002624 error = sysc_get_clocks(ddata);
2625 if (error)
2626 return error;
2627
Tony Lindgren50622362018-04-16 10:20:27 -07002628 error = sysc_init_resets(ddata);
2629 if (error)
Suman Annaa304f482019-06-25 18:33:15 -05002630 goto unprepare;
Tony Lindgren566a9b052017-12-15 09:41:19 -08002631
2632 error = sysc_init_module(ddata);
2633 if (error)
2634 goto unprepare;
2635
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07002636 pm_runtime_enable(ddata->dev);
Tony Lindgren0eecc632017-10-10 14:23:43 -07002637 error = pm_runtime_get_sync(ddata->dev);
2638 if (error < 0) {
2639 pm_runtime_put_noidle(ddata->dev);
2640 pm_runtime_disable(ddata->dev);
2641 goto unprepare;
2642 }
2643
Tero Kristocdc56c12019-10-07 15:29:31 +03002644 /* Balance use counts as PM runtime should have enabled these all */
2645 if (!(ddata->cfg.quirks & SYSC_QUIRK_NO_RESET_ON_INIT))
Tero Kristobb88b862019-08-07 15:46:05 +03002646 reset_control_assert(ddata->rsts);
2647
Tero Kristocdc56c12019-10-07 15:29:31 +03002648 if (!(ddata->cfg.quirks &
2649 (SYSC_QUIRK_NO_IDLE | SYSC_QUIRK_NO_IDLE_ON_INIT))) {
2650 sysc_disable_main_clocks(ddata);
2651 sysc_disable_opt_clocks(ddata);
2652 sysc_clkdm_allow_idle(ddata);
2653 }
2654
Tony Lindgren0eecc632017-10-10 14:23:43 -07002655 sysc_show_registers(ddata);
2656
Tony Lindgren2c355ff2018-02-22 13:58:03 -08002657 ddata->dev->type = &sysc_device_type;
Tony Lindgrenc4bebea2018-04-16 10:24:54 -07002658 error = of_platform_populate(ddata->dev->of_node, sysc_match_table,
2659 pdata ? pdata->auxdata : NULL,
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002660 ddata->dev);
Tony Lindgren0eecc632017-10-10 14:23:43 -07002661 if (error)
2662 goto err;
2663
Tony Lindgren76f0f772018-02-23 08:28:45 -08002664 INIT_DELAYED_WORK(&ddata->idle_work, ti_sysc_idle);
2665
2666 /* At least earlycon won't survive without deferred idle */
Tony Lindgrend0989132019-09-05 07:37:22 -07002667 if (ddata->cfg.quirks & (SYSC_QUIRK_NO_IDLE |
2668 SYSC_QUIRK_NO_IDLE_ON_INIT |
Tony Lindgren76f0f772018-02-23 08:28:45 -08002669 SYSC_QUIRK_NO_RESET_ON_INIT)) {
2670 schedule_delayed_work(&ddata->idle_work, 3000);
2671 } else {
2672 pm_runtime_put(&pdev->dev);
2673 }
Tony Lindgren0eecc632017-10-10 14:23:43 -07002674
2675 return 0;
2676
2677err:
Tony Lindgren0eecc632017-10-10 14:23:43 -07002678 pm_runtime_put_sync(&pdev->dev);
2679 pm_runtime_disable(&pdev->dev);
2680unprepare:
2681 sysc_unprepare(ddata);
2682
2683 return error;
2684}
2685
Tony Lindgren684be5a2017-10-13 10:48:40 -07002686static int sysc_remove(struct platform_device *pdev)
2687{
2688 struct sysc *ddata = platform_get_drvdata(pdev);
2689 int error;
2690
Tony Lindgren76f0f772018-02-23 08:28:45 -08002691 cancel_delayed_work_sync(&ddata->idle_work);
2692
Tony Lindgren684be5a2017-10-13 10:48:40 -07002693 error = pm_runtime_get_sync(ddata->dev);
2694 if (error < 0) {
2695 pm_runtime_put_noidle(ddata->dev);
2696 pm_runtime_disable(ddata->dev);
2697 goto unprepare;
2698 }
2699
2700 of_platform_depopulate(&pdev->dev);
2701
Tony Lindgren684be5a2017-10-13 10:48:40 -07002702 pm_runtime_put_sync(&pdev->dev);
2703 pm_runtime_disable(&pdev->dev);
Tony Lindgren50622362018-04-16 10:20:27 -07002704 reset_control_assert(ddata->rsts);
Tony Lindgren684be5a2017-10-13 10:48:40 -07002705
2706unprepare:
2707 sysc_unprepare(ddata);
2708
2709 return 0;
2710}
2711
Tony Lindgren0eecc632017-10-10 14:23:43 -07002712static const struct of_device_id sysc_match[] = {
Tony Lindgren70a65242017-12-15 09:41:09 -08002713 { .compatible = "ti,sysc-omap2", .data = &sysc_omap2, },
2714 { .compatible = "ti,sysc-omap2-timer", .data = &sysc_omap2_timer, },
2715 { .compatible = "ti,sysc-omap4", .data = &sysc_omap4, },
2716 { .compatible = "ti,sysc-omap4-timer", .data = &sysc_omap4_timer, },
2717 { .compatible = "ti,sysc-omap4-simple", .data = &sysc_omap4_simple, },
2718 { .compatible = "ti,sysc-omap3430-sr", .data = &sysc_34xx_sr, },
2719 { .compatible = "ti,sysc-omap3630-sr", .data = &sysc_36xx_sr, },
2720 { .compatible = "ti,sysc-omap4-sr", .data = &sysc_omap4_sr, },
2721 { .compatible = "ti,sysc-omap3-sham", .data = &sysc_omap3_sham, },
2722 { .compatible = "ti,sysc-omap-aes", .data = &sysc_omap3_aes, },
2723 { .compatible = "ti,sysc-mcasp", .data = &sysc_omap4_mcasp, },
Tony Lindgren2c63a832018-11-15 14:46:53 -08002724 { .compatible = "ti,sysc-dra7-mcasp", .data = &sysc_dra7_mcasp, },
Tony Lindgren70a65242017-12-15 09:41:09 -08002725 { .compatible = "ti,sysc-usb-host-fs",
2726 .data = &sysc_omap4_usb_host_fs, },
Faiz Abbas7f35e632018-07-09 22:18:38 +05302727 { .compatible = "ti,sysc-dra7-mcan", .data = &sysc_dra7_mcan, },
Tony Lindgren0eecc632017-10-10 14:23:43 -07002728 { },
2729};
2730MODULE_DEVICE_TABLE(of, sysc_match);
2731
2732static struct platform_driver sysc_driver = {
2733 .probe = sysc_probe,
Tony Lindgren684be5a2017-10-13 10:48:40 -07002734 .remove = sysc_remove,
Tony Lindgren0eecc632017-10-10 14:23:43 -07002735 .driver = {
2736 .name = "ti-sysc",
2737 .of_match_table = sysc_match,
2738 .pm = &sysc_pm_ops,
2739 },
2740};
Tony Lindgren2c355ff2018-02-22 13:58:03 -08002741
2742static int __init sysc_init(void)
2743{
2744 bus_register_notifier(&platform_bus_type, &sysc_nb);
2745
2746 return platform_driver_register(&sysc_driver);
2747}
2748module_init(sysc_init);
2749
2750static void __exit sysc_exit(void)
2751{
2752 bus_unregister_notifier(&platform_bus_type, &sysc_nb);
2753 platform_driver_unregister(&sysc_driver);
Tony Lindgrenfeaa8ba2020-02-24 12:58:03 -08002754 sysc_cleanup_soc();
Tony Lindgren2c355ff2018-02-22 13:58:03 -08002755}
2756module_exit(sysc_exit);
Tony Lindgren0eecc632017-10-10 14:23:43 -07002757
2758MODULE_DESCRIPTION("TI sysc interconnect target driver");
2759MODULE_LICENSE("GPL v2");