blob: 56887c6877a7b519c2bba19f9c15c273d6035eb2 [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 Lindgren0eecc632017-10-10 14:23:43 -070010#include <linux/module.h>
11#include <linux/platform_device.h>
Tony Lindgrena885f0f2018-02-22 14:03:48 -080012#include <linux/pm_domain.h>
Tony Lindgren0eecc632017-10-10 14:23:43 -070013#include <linux/pm_runtime.h>
Tony Lindgren50622362018-04-16 10:20:27 -070014#include <linux/reset.h>
Tony Lindgren0eecc632017-10-10 14:23:43 -070015#include <linux/of_address.h>
16#include <linux/of_platform.h>
Tony Lindgren2c355ff2018-02-22 13:58:03 -080017#include <linux/slab.h>
Faiz Abbas596e7952018-07-09 22:18:39 +053018#include <linux/iopoll.h>
Tony Lindgren2c355ff2018-02-22 13:58:03 -080019
Tony Lindgren70a65242017-12-15 09:41:09 -080020#include <linux/platform_data/ti-sysc.h>
21
22#include <dt-bindings/bus/ti-sysc.h>
Tony Lindgren0eecc632017-10-10 14:23:43 -070023
Faiz Abbas596e7952018-07-09 22:18:39 +053024#define MAX_MODULE_SOFTRESET_WAIT 10000
25
Tony Lindgren0eecc632017-10-10 14:23:43 -070026static const char * const reg_names[] = { "rev", "sysc", "syss", };
27
28enum sysc_clocks {
29 SYSC_FCK,
30 SYSC_ICK,
Tony Lindgren09dfe582018-04-16 10:25:52 -070031 SYSC_OPTFCK0,
32 SYSC_OPTFCK1,
33 SYSC_OPTFCK2,
34 SYSC_OPTFCK3,
35 SYSC_OPTFCK4,
36 SYSC_OPTFCK5,
37 SYSC_OPTFCK6,
38 SYSC_OPTFCK7,
Tony Lindgren0eecc632017-10-10 14:23:43 -070039 SYSC_MAX_CLOCKS,
40};
41
Tony Lindgrena54275f2019-03-21 11:00:21 -070042static const char * const clock_names[SYSC_MAX_CLOCKS] = {
43 "fck", "ick", "opt0", "opt1", "opt2", "opt3", "opt4",
44 "opt5", "opt6", "opt7",
45};
Tony Lindgren0eecc632017-10-10 14:23:43 -070046
Tony Lindgrenc5a2de92017-12-15 09:41:23 -080047#define SYSC_IDLEMODE_MASK 3
48#define SYSC_CLOCKACTIVITY_MASK 3
49
Tony Lindgren0eecc632017-10-10 14:23:43 -070050/**
51 * struct sysc - TI sysc interconnect target module registers and capabilities
52 * @dev: struct device pointer
53 * @module_pa: physical address of the interconnect target module
54 * @module_size: size of the interconnect target module
55 * @module_va: virtual address of the interconnect target module
56 * @offsets: register offsets from module base
Suman Annab58056d2019-08-13 04:13:32 -070057 * @mdata: ti-sysc to hwmod translation data for a module
Tony Lindgren0eecc632017-10-10 14:23:43 -070058 * @clocks: clocks used by the interconnect target module
Tony Lindgren09dfe582018-04-16 10:25:52 -070059 * @clock_roles: clock role names for the found clocks
60 * @nr_clocks: number of clocks used by the interconnect target module
Suman Annab58056d2019-08-13 04:13:32 -070061 * @rsts: resets used by the interconnect target module
Tony Lindgren0eecc632017-10-10 14:23:43 -070062 * @legacy_mode: configured for legacy mode if set
Tony Lindgren70a65242017-12-15 09:41:09 -080063 * @cap: interconnect target module capabilities
64 * @cfg: interconnect target module configuration
Suman Annab58056d2019-08-13 04:13:32 -070065 * @cookie: data used by legacy platform callbacks
Tony Lindgren566a9b052017-12-15 09:41:19 -080066 * @name: name if available
67 * @revision: interconnect target module revision
Suman Annab58056d2019-08-13 04:13:32 -070068 * @enabled: sysc runtime enabled status
Tony Lindgren62020f22018-02-22 13:59:44 -080069 * @needs_resume: runtime resume needed on resume from suspend
Suman Annab58056d2019-08-13 04:13:32 -070070 * @child_needs_resume: runtime resume needed for child on resume from suspend
71 * @disable_on_idle: status flag used for disabling modules with resets
72 * @idle_work: work structure used to perform delayed idle on a module
Tony Lindgren4e23be42019-06-10 04:48:05 -070073 * @clk_enable_quirk: module specific clock enable quirk
74 * @clk_disable_quirk: module specific clock disable quirk
75 * @reset_done_quirk: module specific reset done quirk
Tony Lindgrend7f563d2019-08-14 05:18:16 -070076 * @module_enable_quirk: module specific enable quirk
Tony Lindgrenc7d86692019-10-17 11:21:44 -070077 * @module_disable_quirk: module specific disable quirk
Tony Lindgren0eecc632017-10-10 14:23:43 -070078 */
79struct sysc {
80 struct device *dev;
81 u64 module_pa;
82 u32 module_size;
83 void __iomem *module_va;
84 int offsets[SYSC_MAX_REGS];
Tony Lindgrena3e92e72019-03-21 11:00:21 -070085 struct ti_sysc_module_data *mdata;
Tony Lindgren09dfe582018-04-16 10:25:52 -070086 struct clk **clocks;
87 const char **clock_roles;
88 int nr_clocks;
Tony Lindgren50622362018-04-16 10:20:27 -070089 struct reset_control *rsts;
Tony Lindgren0eecc632017-10-10 14:23:43 -070090 const char *legacy_mode;
Tony Lindgren70a65242017-12-15 09:41:09 -080091 const struct sysc_capabilities *cap;
92 struct sysc_config cfg;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -080093 struct ti_sysc_cookie cookie;
Tony Lindgren566a9b052017-12-15 09:41:19 -080094 const char *name;
95 u32 revision;
Tony Lindgren8383e252019-05-27 04:51:56 -070096 unsigned int enabled:1;
97 unsigned int needs_resume:1;
98 unsigned int child_needs_resume:1;
Tony Lindgren76f0f772018-02-23 08:28:45 -080099 struct delayed_work idle_work;
Tony Lindgren4e23be42019-06-10 04:48:05 -0700100 void (*clk_enable_quirk)(struct sysc *sysc);
101 void (*clk_disable_quirk)(struct sysc *sysc);
102 void (*reset_done_quirk)(struct sysc *sysc);
Tony Lindgrend7f563d2019-08-14 05:18:16 -0700103 void (*module_enable_quirk)(struct sysc *sysc);
Tony Lindgrenc7d86692019-10-17 11:21:44 -0700104 void (*module_disable_quirk)(struct sysc *sysc);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700105};
106
Tony Lindgren4014c082018-12-10 14:11:26 -0800107static void sysc_parse_dts_quirks(struct sysc *ddata, struct device_node *np,
108 bool is_child);
109
Tony Lindgrenb7182b42019-03-21 11:00:21 -0700110static void sysc_write(struct sysc *ddata, int offset, u32 value)
Faiz Abbas596e7952018-07-09 22:18:39 +0530111{
Tony Lindgren5aa91292019-05-27 04:51:53 -0700112 if (ddata->cfg.quirks & SYSC_QUIRK_16BIT) {
113 writew_relaxed(value & 0xffff, ddata->module_va + offset);
114
115 /* Only i2c revision has LO and HI register with stride of 4 */
116 if (ddata->offsets[SYSC_REVISION] >= 0 &&
117 offset == ddata->offsets[SYSC_REVISION]) {
118 u16 hi = value >> 16;
119
120 writew_relaxed(hi, ddata->module_va + offset + 4);
121 }
122
123 return;
124 }
125
Faiz Abbas596e7952018-07-09 22:18:39 +0530126 writel_relaxed(value, ddata->module_va + offset);
127}
128
Tony Lindgren566a9b052017-12-15 09:41:19 -0800129static u32 sysc_read(struct sysc *ddata, int offset)
130{
131 if (ddata->cfg.quirks & SYSC_QUIRK_16BIT) {
132 u32 val;
133
134 val = readw_relaxed(ddata->module_va + offset);
Tony Lindgren5aa91292019-05-27 04:51:53 -0700135
136 /* Only i2c revision has LO and HI register with stride of 4 */
137 if (ddata->offsets[SYSC_REVISION] >= 0 &&
138 offset == ddata->offsets[SYSC_REVISION]) {
139 u16 tmp = readw_relaxed(ddata->module_va + offset + 4);
140
141 val |= tmp << 16;
142 }
Tony Lindgren566a9b052017-12-15 09:41:19 -0800143
144 return val;
145 }
146
147 return readl_relaxed(ddata->module_va + offset);
148}
149
Tony Lindgren09dfe582018-04-16 10:25:52 -0700150static bool sysc_opt_clks_needed(struct sysc *ddata)
151{
152 return !!(ddata->cfg.quirks & SYSC_QUIRK_OPT_CLKS_NEEDED);
153}
154
Tony Lindgren0eecc632017-10-10 14:23:43 -0700155static u32 sysc_read_revision(struct sysc *ddata)
156{
Tony Lindgren566a9b052017-12-15 09:41:19 -0800157 int offset = ddata->offsets[SYSC_REVISION];
158
159 if (offset < 0)
160 return 0;
161
162 return sysc_read(ddata, offset);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700163}
164
Tony Lindgrene0db94f2019-05-27 04:51:53 -0700165static u32 sysc_read_sysconfig(struct sysc *ddata)
166{
167 int offset = ddata->offsets[SYSC_SYSCONFIG];
168
169 if (offset < 0)
170 return 0;
171
172 return sysc_read(ddata, offset);
173}
174
175static u32 sysc_read_sysstatus(struct sysc *ddata)
176{
177 int offset = ddata->offsets[SYSC_SYSSTATUS];
178
179 if (offset < 0)
180 return 0;
181
182 return sysc_read(ddata, offset);
183}
184
Tony Lindgrena54275f2019-03-21 11:00:21 -0700185static int sysc_add_named_clock_from_child(struct sysc *ddata,
186 const char *name,
187 const char *optfck_name)
188{
189 struct device_node *np = ddata->dev->of_node;
190 struct device_node *child;
191 struct clk_lookup *cl;
192 struct clk *clock;
193 const char *n;
194
195 if (name)
196 n = name;
197 else
198 n = optfck_name;
199
200 /* Does the clock alias already exist? */
201 clock = of_clk_get_by_name(np, n);
202 if (!IS_ERR(clock)) {
203 clk_put(clock);
204
205 return 0;
206 }
207
208 child = of_get_next_available_child(np, NULL);
209 if (!child)
210 return -ENODEV;
211
212 clock = devm_get_clk_from_child(ddata->dev, child, name);
213 if (IS_ERR(clock))
214 return PTR_ERR(clock);
215
216 /*
217 * Use clkdev_add() instead of clkdev_alloc() to avoid the MAX_DEV_ID
218 * limit for clk_get(). If cl ever needs to be freed, it should be done
219 * with clkdev_drop().
220 */
221 cl = kcalloc(1, sizeof(*cl), GFP_KERNEL);
222 if (!cl)
223 return -ENOMEM;
224
225 cl->con_id = n;
226 cl->dev_id = dev_name(ddata->dev);
227 cl->clk = clock;
228 clkdev_add(cl);
229
230 clk_put(clock);
231
232 return 0;
233}
234
235static int sysc_init_ext_opt_clock(struct sysc *ddata, const char *name)
236{
237 const char *optfck_name;
238 int error, index;
239
240 if (ddata->nr_clocks < SYSC_OPTFCK0)
241 index = SYSC_OPTFCK0;
242 else
243 index = ddata->nr_clocks;
244
245 if (name)
246 optfck_name = name;
247 else
248 optfck_name = clock_names[index];
249
250 error = sysc_add_named_clock_from_child(ddata, name, optfck_name);
251 if (error)
252 return error;
253
254 ddata->clock_roles[index] = optfck_name;
255 ddata->nr_clocks++;
256
257 return 0;
258}
259
Tony Lindgren09dfe582018-04-16 10:25:52 -0700260static int sysc_get_one_clock(struct sysc *ddata, const char *name)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700261{
Tony Lindgren09dfe582018-04-16 10:25:52 -0700262 int error, i, index = -ENODEV;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700263
Tony Lindgren09dfe582018-04-16 10:25:52 -0700264 if (!strncmp(clock_names[SYSC_FCK], name, 3))
265 index = SYSC_FCK;
266 else if (!strncmp(clock_names[SYSC_ICK], name, 3))
267 index = SYSC_ICK;
268
269 if (index < 0) {
270 for (i = SYSC_OPTFCK0; i < SYSC_MAX_CLOCKS; i++) {
Tony Lindgrenc97c8622018-05-18 07:54:44 -0700271 if (!ddata->clocks[i]) {
Tony Lindgren09dfe582018-04-16 10:25:52 -0700272 index = i;
273 break;
274 }
275 }
Tony Lindgren0eecc632017-10-10 14:23:43 -0700276 }
Tony Lindgren09dfe582018-04-16 10:25:52 -0700277
278 if (index < 0) {
279 dev_err(ddata->dev, "clock %s not added\n", name);
280 return index;
281 }
Tony Lindgren0eecc632017-10-10 14:23:43 -0700282
283 ddata->clocks[index] = devm_clk_get(ddata->dev, name);
284 if (IS_ERR(ddata->clocks[index])) {
Tony Lindgren0eecc632017-10-10 14:23:43 -0700285 dev_err(ddata->dev, "clock get error for %s: %li\n",
286 name, PTR_ERR(ddata->clocks[index]));
287
288 return PTR_ERR(ddata->clocks[index]);
289 }
290
291 error = clk_prepare(ddata->clocks[index]);
292 if (error) {
293 dev_err(ddata->dev, "clock prepare error for %s: %i\n",
294 name, error);
295
296 return error;
297 }
298
299 return 0;
300}
301
302static int sysc_get_clocks(struct sysc *ddata)
303{
Tony Lindgren09dfe582018-04-16 10:25:52 -0700304 struct device_node *np = ddata->dev->of_node;
305 struct property *prop;
306 const char *name;
307 int nr_fck = 0, nr_ick = 0, i, error = 0;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700308
Kees Cook20749052018-06-18 10:45:49 -0700309 ddata->clock_roles = devm_kcalloc(ddata->dev,
Tony Lindgren09dfe582018-04-16 10:25:52 -0700310 SYSC_MAX_CLOCKS,
Kees Cook20749052018-06-18 10:45:49 -0700311 sizeof(*ddata->clock_roles),
Tony Lindgren09dfe582018-04-16 10:25:52 -0700312 GFP_KERNEL);
313 if (!ddata->clock_roles)
314 return -ENOMEM;
315
316 of_property_for_each_string(np, "clock-names", prop, name) {
317 if (!strncmp(clock_names[SYSC_FCK], name, 3))
318 nr_fck++;
319 if (!strncmp(clock_names[SYSC_ICK], name, 3))
320 nr_ick++;
321 ddata->clock_roles[ddata->nr_clocks] = name;
322 ddata->nr_clocks++;
323 }
324
325 if (ddata->nr_clocks < 1)
326 return 0;
327
Tony Lindgrena54275f2019-03-21 11:00:21 -0700328 if ((ddata->cfg.quirks & SYSC_QUIRK_EXT_OPT_CLOCK)) {
329 error = sysc_init_ext_opt_clock(ddata, NULL);
330 if (error)
331 return error;
332 }
333
Tony Lindgren09dfe582018-04-16 10:25:52 -0700334 if (ddata->nr_clocks > SYSC_MAX_CLOCKS) {
335 dev_err(ddata->dev, "too many clocks for %pOF\n", np);
336
337 return -EINVAL;
338 }
339
340 if (nr_fck > 1 || nr_ick > 1) {
341 dev_err(ddata->dev, "max one fck and ick for %pOF\n", np);
342
343 return -EINVAL;
344 }
345
Kees Cook20749052018-06-18 10:45:49 -0700346 ddata->clocks = devm_kcalloc(ddata->dev,
347 ddata->nr_clocks, sizeof(*ddata->clocks),
Tony Lindgren09dfe582018-04-16 10:25:52 -0700348 GFP_KERNEL);
349 if (!ddata->clocks)
350 return -ENOMEM;
351
Tony Lindgren7b4f8ac2018-11-15 14:46:52 -0800352 for (i = 0; i < SYSC_MAX_CLOCKS; i++) {
353 const char *name = ddata->clock_roles[i];
354
355 if (!name)
356 continue;
357
358 error = sysc_get_one_clock(ddata, name);
Tony Lindgren2783d062019-09-05 13:01:29 -0700359 if (error)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700360 return error;
361 }
362
363 return 0;
364}
365
Tony Lindgrend8789702019-03-21 11:00:21 -0700366static int sysc_enable_main_clocks(struct sysc *ddata)
367{
368 struct clk *clock;
369 int i, error;
370
371 if (!ddata->clocks)
372 return 0;
373
374 for (i = 0; i < SYSC_OPTFCK0; i++) {
375 clock = ddata->clocks[i];
376
377 /* Main clocks may not have ick */
378 if (IS_ERR_OR_NULL(clock))
379 continue;
380
381 error = clk_enable(clock);
382 if (error)
383 goto err_disable;
384 }
385
386 return 0;
387
388err_disable:
389 for (i--; i >= 0; i--) {
390 clock = ddata->clocks[i];
391
392 /* Main clocks may not have ick */
393 if (IS_ERR_OR_NULL(clock))
394 continue;
395
396 clk_disable(clock);
397 }
398
399 return error;
400}
401
402static void sysc_disable_main_clocks(struct sysc *ddata)
403{
404 struct clk *clock;
405 int i;
406
407 if (!ddata->clocks)
408 return;
409
410 for (i = 0; i < SYSC_OPTFCK0; i++) {
411 clock = ddata->clocks[i];
412 if (IS_ERR_OR_NULL(clock))
413 continue;
414
415 clk_disable(clock);
416 }
417}
418
419static int sysc_enable_opt_clocks(struct sysc *ddata)
420{
421 struct clk *clock;
422 int i, error;
423
424 if (!ddata->clocks)
425 return 0;
426
427 for (i = SYSC_OPTFCK0; i < SYSC_MAX_CLOCKS; i++) {
428 clock = ddata->clocks[i];
429
430 /* Assume no holes for opt clocks */
431 if (IS_ERR_OR_NULL(clock))
432 return 0;
433
434 error = clk_enable(clock);
435 if (error)
436 goto err_disable;
437 }
438
439 return 0;
440
441err_disable:
442 for (i--; i >= 0; i--) {
443 clock = ddata->clocks[i];
444 if (IS_ERR_OR_NULL(clock))
445 continue;
446
447 clk_disable(clock);
448 }
449
450 return error;
451}
452
453static void sysc_disable_opt_clocks(struct sysc *ddata)
454{
455 struct clk *clock;
456 int i;
457
458 if (!ddata->clocks)
459 return;
460
461 for (i = SYSC_OPTFCK0; i < SYSC_MAX_CLOCKS; i++) {
462 clock = ddata->clocks[i];
463
464 /* Assume no holes for opt clocks */
465 if (IS_ERR_OR_NULL(clock))
466 return;
467
468 clk_disable(clock);
469 }
470}
471
Tony Lindgren2b2f7de2019-05-27 04:51:53 -0700472static void sysc_clkdm_deny_idle(struct sysc *ddata)
473{
474 struct ti_sysc_platform_data *pdata;
475
476 if (ddata->legacy_mode)
477 return;
478
479 pdata = dev_get_platdata(ddata->dev);
480 if (pdata && pdata->clkdm_deny_idle)
481 pdata->clkdm_deny_idle(ddata->dev, &ddata->cookie);
482}
483
484static void sysc_clkdm_allow_idle(struct sysc *ddata)
485{
486 struct ti_sysc_platform_data *pdata;
487
488 if (ddata->legacy_mode)
489 return;
490
491 pdata = dev_get_platdata(ddata->dev);
492 if (pdata && pdata->clkdm_allow_idle)
493 pdata->clkdm_allow_idle(ddata->dev, &ddata->cookie);
494}
495
Tony Lindgren0eecc632017-10-10 14:23:43 -0700496/**
Tony Lindgrenb11c1ea2019-03-21 11:00:21 -0700497 * sysc_init_resets - init rstctrl reset line if configured
Tony Lindgren50622362018-04-16 10:20:27 -0700498 * @ddata: device driver data
499 *
Tony Lindgrenb11c1ea2019-03-21 11:00:21 -0700500 * See sysc_rstctrl_reset_deassert().
Tony Lindgren50622362018-04-16 10:20:27 -0700501 */
502static int sysc_init_resets(struct sysc *ddata)
503{
Tony Lindgren50622362018-04-16 10:20:27 -0700504 ddata->rsts =
Tero Kristobb88b862019-08-07 15:46:05 +0300505 devm_reset_control_get_optional_shared(ddata->dev, "rstctrl");
Tony Lindgren50622362018-04-16 10:20:27 -0700506 if (IS_ERR(ddata->rsts))
507 return PTR_ERR(ddata->rsts);
508
Tony Lindgren50622362018-04-16 10:20:27 -0700509 return 0;
510}
511
512/**
Tony Lindgren0eecc632017-10-10 14:23:43 -0700513 * sysc_parse_and_check_child_range - parses module IO region from ranges
514 * @ddata: device driver data
515 *
516 * In general we only need rev, syss, and sysc registers and not the whole
517 * module range. But we do want the offsets for these registers from the
518 * module base. This allows us to check them against the legacy hwmod
519 * platform data. Let's also check the ranges are configured properly.
520 */
521static int sysc_parse_and_check_child_range(struct sysc *ddata)
522{
523 struct device_node *np = ddata->dev->of_node;
524 const __be32 *ranges;
525 u32 nr_addr, nr_size;
526 int len, error;
527
528 ranges = of_get_property(np, "ranges", &len);
529 if (!ranges) {
530 dev_err(ddata->dev, "missing ranges for %pOF\n", np);
531
532 return -ENOENT;
533 }
534
535 len /= sizeof(*ranges);
536
537 if (len < 3) {
538 dev_err(ddata->dev, "incomplete ranges for %pOF\n", np);
539
540 return -EINVAL;
541 }
542
543 error = of_property_read_u32(np, "#address-cells", &nr_addr);
544 if (error)
545 return -ENOENT;
546
547 error = of_property_read_u32(np, "#size-cells", &nr_size);
548 if (error)
549 return -ENOENT;
550
551 if (nr_addr != 1 || nr_size != 1) {
552 dev_err(ddata->dev, "invalid ranges for %pOF\n", np);
553
554 return -EINVAL;
555 }
556
557 ranges++;
558 ddata->module_pa = of_translate_address(np, ranges++);
559 ddata->module_size = be32_to_cpup(ranges);
560
Tony Lindgren0eecc632017-10-10 14:23:43 -0700561 return 0;
562}
563
Tony Lindgren3bb37c82018-02-22 14:05:14 -0800564static struct device_node *stdout_path;
565
566static void sysc_init_stdout_path(struct sysc *ddata)
567{
568 struct device_node *np = NULL;
569 const char *uart;
570
571 if (IS_ERR(stdout_path))
572 return;
573
574 if (stdout_path)
575 return;
576
577 np = of_find_node_by_path("/chosen");
578 if (!np)
579 goto err;
580
581 uart = of_get_property(np, "stdout-path", NULL);
582 if (!uart)
583 goto err;
584
585 np = of_find_node_by_path(uart);
586 if (!np)
587 goto err;
588
589 stdout_path = np;
590
591 return;
592
593err:
594 stdout_path = ERR_PTR(-ENODEV);
595}
596
597static void sysc_check_quirk_stdout(struct sysc *ddata,
598 struct device_node *np)
599{
600 sysc_init_stdout_path(ddata);
601 if (np != stdout_path)
602 return;
603
604 ddata->cfg.quirks |= SYSC_QUIRK_NO_IDLE_ON_INIT |
605 SYSC_QUIRK_NO_RESET_ON_INIT;
606}
607
Tony Lindgren0eecc632017-10-10 14:23:43 -0700608/**
609 * sysc_check_one_child - check child configuration
610 * @ddata: device driver data
611 * @np: child device node
612 *
613 * Let's avoid messy situations where we have new interconnect target
614 * node but children have "ti,hwmods". These belong to the interconnect
615 * target node and are managed by this driver.
616 */
Nishka Dasguptac6e78d72019-08-15 11:16:47 +0530617static void sysc_check_one_child(struct sysc *ddata,
618 struct device_node *np)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700619{
620 const char *name;
621
622 name = of_get_property(np, "ti,hwmods", NULL);
623 if (name)
624 dev_warn(ddata->dev, "really a child ti,hwmods property?");
625
Tony Lindgren3bb37c82018-02-22 14:05:14 -0800626 sysc_check_quirk_stdout(ddata, np);
Tony Lindgren4014c082018-12-10 14:11:26 -0800627 sysc_parse_dts_quirks(ddata, np, true);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700628}
629
Nishka Dasguptac6e78d72019-08-15 11:16:47 +0530630static void sysc_check_children(struct sysc *ddata)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700631{
632 struct device_node *child;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700633
Nishka Dasguptac6e78d72019-08-15 11:16:47 +0530634 for_each_child_of_node(ddata->dev->of_node, child)
635 sysc_check_one_child(ddata, child);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700636}
637
Tony Lindgrena7199e22017-12-15 09:41:14 -0800638/*
639 * So far only I2C uses 16-bit read access with clockactivity with revision
640 * in two registers with stride of 4. We can detect this based on the rev
641 * register size to configure things far enough to be able to properly read
642 * the revision register.
643 */
644static void sysc_check_quirk_16bit(struct sysc *ddata, struct resource *res)
645{
Tony Lindgrendd57ac12018-02-22 14:09:57 -0800646 if (resource_size(res) == 8)
Tony Lindgrena7199e22017-12-15 09:41:14 -0800647 ddata->cfg.quirks |= SYSC_QUIRK_16BIT | SYSC_QUIRK_USE_CLOCKACT;
Tony Lindgrena7199e22017-12-15 09:41:14 -0800648}
649
Tony Lindgren0eecc632017-10-10 14:23:43 -0700650/**
651 * sysc_parse_one - parses the interconnect target module registers
652 * @ddata: device driver data
653 * @reg: register to parse
654 */
655static int sysc_parse_one(struct sysc *ddata, enum sysc_registers reg)
656{
657 struct resource *res;
658 const char *name;
659
660 switch (reg) {
661 case SYSC_REVISION:
662 case SYSC_SYSCONFIG:
663 case SYSC_SYSSTATUS:
664 name = reg_names[reg];
665 break;
666 default:
667 return -EINVAL;
668 }
669
670 res = platform_get_resource_byname(to_platform_device(ddata->dev),
671 IORESOURCE_MEM, name);
672 if (!res) {
Tony Lindgren0eecc632017-10-10 14:23:43 -0700673 ddata->offsets[reg] = -ENODEV;
674
675 return 0;
676 }
677
678 ddata->offsets[reg] = res->start - ddata->module_pa;
Tony Lindgrena7199e22017-12-15 09:41:14 -0800679 if (reg == SYSC_REVISION)
680 sysc_check_quirk_16bit(ddata, res);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700681
682 return 0;
683}
684
685static int sysc_parse_registers(struct sysc *ddata)
686{
687 int i, error;
688
689 for (i = 0; i < SYSC_MAX_REGS; i++) {
690 error = sysc_parse_one(ddata, i);
691 if (error)
692 return error;
693 }
694
695 return 0;
696}
697
698/**
699 * sysc_check_registers - check for misconfigured register overlaps
700 * @ddata: device driver data
701 */
702static int sysc_check_registers(struct sysc *ddata)
703{
704 int i, j, nr_regs = 0, nr_matches = 0;
705
706 for (i = 0; i < SYSC_MAX_REGS; i++) {
707 if (ddata->offsets[i] < 0)
708 continue;
709
710 if (ddata->offsets[i] > (ddata->module_size - 4)) {
711 dev_err(ddata->dev, "register outside module range");
712
713 return -EINVAL;
714 }
715
716 for (j = 0; j < SYSC_MAX_REGS; j++) {
717 if (ddata->offsets[j] < 0)
718 continue;
719
720 if (ddata->offsets[i] == ddata->offsets[j])
721 nr_matches++;
722 }
723 nr_regs++;
724 }
725
Tony Lindgren0eecc632017-10-10 14:23:43 -0700726 if (nr_matches > nr_regs) {
727 dev_err(ddata->dev, "overlapping registers: (%i/%i)",
728 nr_regs, nr_matches);
729
730 return -EINVAL;
731 }
732
733 return 0;
734}
735
736/**
737 * syc_ioremap - ioremap register space for the interconnect target module
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700738 * @ddata: device driver data
Tony Lindgren0eecc632017-10-10 14:23:43 -0700739 *
740 * Note that the interconnect target module registers can be anywhere
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700741 * within the interconnect target module range. For example, SGX has
742 * them at offset 0x1fc00 in the 32MB module address space. And cpsw
743 * has them at offset 0x1200 in the CPSW_WR child. Usually the
744 * the interconnect target module registers are at the beginning of
745 * the module range though.
Tony Lindgren0eecc632017-10-10 14:23:43 -0700746 */
747static int sysc_ioremap(struct sysc *ddata)
748{
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700749 int size;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700750
Tony Lindgrene4f50c82019-05-01 14:24:57 -0700751 if (ddata->offsets[SYSC_REVISION] < 0 &&
752 ddata->offsets[SYSC_SYSCONFIG] < 0 &&
753 ddata->offsets[SYSC_SYSSTATUS] < 0) {
754 size = ddata->module_size;
755 } else {
756 size = max3(ddata->offsets[SYSC_REVISION],
757 ddata->offsets[SYSC_SYSCONFIG],
758 ddata->offsets[SYSC_SYSSTATUS]);
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700759
Tony Lindgren4e23be42019-06-10 04:48:05 -0700760 if (size < SZ_1K)
761 size = SZ_1K;
762
Tony Lindgrene4f50c82019-05-01 14:24:57 -0700763 if ((size + sizeof(u32)) > ddata->module_size)
Tony Lindgren4e23be42019-06-10 04:48:05 -0700764 size = ddata->module_size;
Tony Lindgrene4f50c82019-05-01 14:24:57 -0700765 }
Tony Lindgren0eecc632017-10-10 14:23:43 -0700766
Tony Lindgren0eecc632017-10-10 14:23:43 -0700767 ddata->module_va = devm_ioremap(ddata->dev,
768 ddata->module_pa,
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700769 size + sizeof(u32));
Tony Lindgren0eecc632017-10-10 14:23:43 -0700770 if (!ddata->module_va)
771 return -EIO;
772
773 return 0;
774}
775
776/**
777 * sysc_map_and_check_registers - ioremap and check device registers
778 * @ddata: device driver data
779 */
780static int sysc_map_and_check_registers(struct sysc *ddata)
781{
782 int error;
783
784 error = sysc_parse_and_check_child_range(ddata);
785 if (error)
786 return error;
787
Nishka Dasguptac6e78d72019-08-15 11:16:47 +0530788 sysc_check_children(ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700789
790 error = sysc_parse_registers(ddata);
791 if (error)
792 return error;
793
794 error = sysc_ioremap(ddata);
795 if (error)
796 return error;
797
798 error = sysc_check_registers(ddata);
799 if (error)
800 return error;
801
802 return 0;
803}
804
805/**
806 * sysc_show_rev - read and show interconnect target module revision
807 * @bufp: buffer to print the information to
808 * @ddata: device driver data
809 */
810static int sysc_show_rev(char *bufp, struct sysc *ddata)
811{
Tony Lindgren566a9b052017-12-15 09:41:19 -0800812 int len;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700813
814 if (ddata->offsets[SYSC_REVISION] < 0)
815 return sprintf(bufp, ":NA");
816
Tony Lindgren566a9b052017-12-15 09:41:19 -0800817 len = sprintf(bufp, ":%08x", ddata->revision);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700818
819 return len;
820}
821
822static int sysc_show_reg(struct sysc *ddata,
823 char *bufp, enum sysc_registers reg)
824{
825 if (ddata->offsets[reg] < 0)
826 return sprintf(bufp, ":NA");
827
828 return sprintf(bufp, ":%x", ddata->offsets[reg]);
829}
830
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800831static int sysc_show_name(char *bufp, struct sysc *ddata)
832{
833 if (!ddata->name)
834 return 0;
835
836 return sprintf(bufp, ":%s", ddata->name);
837}
838
Tony Lindgren0eecc632017-10-10 14:23:43 -0700839/**
840 * sysc_show_registers - show information about interconnect target module
841 * @ddata: device driver data
842 */
843static void sysc_show_registers(struct sysc *ddata)
844{
845 char buf[128];
846 char *bufp = buf;
847 int i;
848
849 for (i = 0; i < SYSC_MAX_REGS; i++)
850 bufp += sysc_show_reg(ddata, bufp, i);
851
852 bufp += sysc_show_rev(bufp, ddata);
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800853 bufp += sysc_show_name(bufp, ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700854
855 dev_dbg(ddata->dev, "%llx:%x%s\n",
856 ddata->module_pa, ddata->module_size,
857 buf);
858}
859
Roger Quadrosd59b6052019-04-08 12:52:39 +0300860#define SYSC_IDLE_MASK (SYSC_NR_IDLEMODES - 1)
Tony Lindgrenae9ae122019-05-27 04:51:54 -0700861#define SYSC_CLOCACT_ICK 2
Roger Quadrosd59b6052019-04-08 12:52:39 +0300862
Tony Lindgren2b2f7de2019-05-27 04:51:53 -0700863/* Caller needs to manage sysc_clkdm_deny_idle() and sysc_clkdm_allow_idle() */
Roger Quadrosd59b6052019-04-08 12:52:39 +0300864static int sysc_enable_module(struct device *dev)
865{
866 struct sysc *ddata;
867 const struct sysc_regbits *regbits;
868 u32 reg, idlemodes, best_mode;
869
870 ddata = dev_get_drvdata(dev);
871 if (ddata->offsets[SYSC_SYSCONFIG] == -ENODEV)
872 return 0;
873
Roger Quadrosd59b6052019-04-08 12:52:39 +0300874 regbits = ddata->cap->regbits;
875 reg = sysc_read(ddata, ddata->offsets[SYSC_SYSCONFIG]);
876
Tony Lindgrenae9ae122019-05-27 04:51:54 -0700877 /* Set CLOCKACTIVITY, we only use it for ick */
878 if (regbits->clkact_shift >= 0 &&
879 (ddata->cfg.quirks & SYSC_QUIRK_USE_CLOCKACT ||
880 ddata->cfg.sysc_val & BIT(regbits->clkact_shift)))
881 reg |= SYSC_CLOCACT_ICK << regbits->clkact_shift;
882
Roger Quadrosd59b6052019-04-08 12:52:39 +0300883 /* Set SIDLE mode */
884 idlemodes = ddata->cfg.sidlemodes;
885 if (!idlemodes || regbits->sidle_shift < 0)
886 goto set_midle;
887
Tony Lindgrenfb685f12019-05-27 04:51:55 -0700888 if (ddata->cfg.quirks & (SYSC_QUIRK_SWSUP_SIDLE |
889 SYSC_QUIRK_SWSUP_SIDLE_ACT)) {
890 best_mode = SYSC_IDLE_NO;
891 } else {
892 best_mode = fls(ddata->cfg.sidlemodes) - 1;
893 if (best_mode > SYSC_IDLE_MASK) {
894 dev_err(dev, "%s: invalid sidlemode\n", __func__);
895 return -EINVAL;
896 }
Tony Lindgren6e09f492019-05-27 04:51:55 -0700897
898 /* Set WAKEUP */
899 if (regbits->enwkup_shift >= 0 &&
900 ddata->cfg.sysc_val & BIT(regbits->enwkup_shift))
901 reg |= BIT(regbits->enwkup_shift);
Roger Quadrosd59b6052019-04-08 12:52:39 +0300902 }
903
904 reg &= ~(SYSC_IDLE_MASK << regbits->sidle_shift);
905 reg |= best_mode << regbits->sidle_shift;
906 sysc_write(ddata, ddata->offsets[SYSC_SYSCONFIG], reg);
907
908set_midle:
909 /* Set MIDLE mode */
910 idlemodes = ddata->cfg.midlemodes;
911 if (!idlemodes || regbits->midle_shift < 0)
Tony Lindgreneec26552019-05-27 04:51:54 -0700912 goto set_autoidle;
Roger Quadrosd59b6052019-04-08 12:52:39 +0300913
914 best_mode = fls(ddata->cfg.midlemodes) - 1;
915 if (best_mode > SYSC_IDLE_MASK) {
916 dev_err(dev, "%s: invalid midlemode\n", __func__);
917 return -EINVAL;
918 }
919
Tony Lindgren03856e92019-10-21 13:36:09 -0700920 if (ddata->cfg.quirks & SYSC_QUIRK_SWSUP_MSTANDBY)
921 best_mode = SYSC_IDLE_NO;
922
Roger Quadrosd59b6052019-04-08 12:52:39 +0300923 reg &= ~(SYSC_IDLE_MASK << regbits->midle_shift);
924 reg |= best_mode << regbits->midle_shift;
925 sysc_write(ddata, ddata->offsets[SYSC_SYSCONFIG], reg);
926
Tony Lindgreneec26552019-05-27 04:51:54 -0700927set_autoidle:
928 /* Autoidle bit must enabled separately if available */
929 if (regbits->autoidle_shift >= 0 &&
930 ddata->cfg.sysc_val & BIT(regbits->autoidle_shift)) {
931 reg |= 1 << regbits->autoidle_shift;
932 sysc_write(ddata, ddata->offsets[SYSC_SYSCONFIG], reg);
933 }
934
Tony Lindgrend7f563d2019-08-14 05:18:16 -0700935 if (ddata->module_enable_quirk)
936 ddata->module_enable_quirk(ddata);
937
Roger Quadrosd59b6052019-04-08 12:52:39 +0300938 return 0;
939}
940
941static int sysc_best_idle_mode(u32 idlemodes, u32 *best_mode)
942{
943 if (idlemodes & BIT(SYSC_IDLE_SMART_WKUP))
944 *best_mode = SYSC_IDLE_SMART_WKUP;
945 else if (idlemodes & BIT(SYSC_IDLE_SMART))
946 *best_mode = SYSC_IDLE_SMART;
Tony Lindgren6ee82412019-07-22 03:44:42 -0700947 else if (idlemodes & BIT(SYSC_IDLE_FORCE))
Roger Quadrosd59b6052019-04-08 12:52:39 +0300948 *best_mode = SYSC_IDLE_FORCE;
949 else
950 return -EINVAL;
951
952 return 0;
953}
954
Tony Lindgren2b2f7de2019-05-27 04:51:53 -0700955/* Caller needs to manage sysc_clkdm_deny_idle() and sysc_clkdm_allow_idle() */
Roger Quadrosd59b6052019-04-08 12:52:39 +0300956static int sysc_disable_module(struct device *dev)
957{
958 struct sysc *ddata;
959 const struct sysc_regbits *regbits;
960 u32 reg, idlemodes, best_mode;
961 int ret;
962
963 ddata = dev_get_drvdata(dev);
964 if (ddata->offsets[SYSC_SYSCONFIG] == -ENODEV)
965 return 0;
966
Tony Lindgrenc7d86692019-10-17 11:21:44 -0700967 if (ddata->module_disable_quirk)
968 ddata->module_disable_quirk(ddata);
969
Roger Quadrosd59b6052019-04-08 12:52:39 +0300970 regbits = ddata->cap->regbits;
971 reg = sysc_read(ddata, ddata->offsets[SYSC_SYSCONFIG]);
972
973 /* Set MIDLE mode */
974 idlemodes = ddata->cfg.midlemodes;
975 if (!idlemodes || regbits->midle_shift < 0)
976 goto set_sidle;
977
978 ret = sysc_best_idle_mode(idlemodes, &best_mode);
979 if (ret) {
980 dev_err(dev, "%s: invalid midlemode\n", __func__);
981 return ret;
982 }
983
Tony Lindgren03856e92019-10-21 13:36:09 -0700984 if (ddata->cfg.quirks & SYSC_QUIRK_SWSUP_MSTANDBY)
985 best_mode = SYSC_IDLE_FORCE;
986
Roger Quadrosd59b6052019-04-08 12:52:39 +0300987 reg &= ~(SYSC_IDLE_MASK << regbits->midle_shift);
988 reg |= best_mode << regbits->midle_shift;
989 sysc_write(ddata, ddata->offsets[SYSC_SYSCONFIG], reg);
990
991set_sidle:
992 /* Set SIDLE mode */
993 idlemodes = ddata->cfg.sidlemodes;
994 if (!idlemodes || regbits->sidle_shift < 0)
995 return 0;
996
Tony Lindgrenfb685f12019-05-27 04:51:55 -0700997 if (ddata->cfg.quirks & SYSC_QUIRK_SWSUP_SIDLE) {
998 best_mode = SYSC_IDLE_FORCE;
999 } else {
1000 ret = sysc_best_idle_mode(idlemodes, &best_mode);
1001 if (ret) {
1002 dev_err(dev, "%s: invalid sidlemode\n", __func__);
1003 return ret;
1004 }
Roger Quadrosd59b6052019-04-08 12:52:39 +03001005 }
1006
1007 reg &= ~(SYSC_IDLE_MASK << regbits->sidle_shift);
1008 reg |= best_mode << regbits->sidle_shift;
Tony Lindgreneec26552019-05-27 04:51:54 -07001009 if (regbits->autoidle_shift >= 0 &&
1010 ddata->cfg.sysc_val & BIT(regbits->autoidle_shift))
1011 reg |= 1 << regbits->autoidle_shift;
Roger Quadrosd59b6052019-04-08 12:52:39 +03001012 sysc_write(ddata, ddata->offsets[SYSC_SYSCONFIG], reg);
1013
1014 return 0;
1015}
1016
Tony Lindgrenff437282019-03-21 11:00:21 -07001017static int __maybe_unused sysc_runtime_suspend_legacy(struct device *dev,
1018 struct sysc *ddata)
Tony Lindgren0eecc632017-10-10 14:23:43 -07001019{
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001020 struct ti_sysc_platform_data *pdata;
Tony Lindgrenff437282019-03-21 11:00:21 -07001021 int error;
1022
1023 pdata = dev_get_platdata(ddata->dev);
1024 if (!pdata)
1025 return 0;
1026
1027 if (!pdata->idle_module)
1028 return -ENODEV;
1029
1030 error = pdata->idle_module(dev, &ddata->cookie);
1031 if (error)
1032 dev_err(dev, "%s: could not idle: %i\n",
1033 __func__, error);
1034
Tero Kristo4345f0d2019-08-07 15:46:04 +03001035 reset_control_assert(ddata->rsts);
Tony Lindgren8383e252019-05-27 04:51:56 -07001036
Tony Lindgrenff437282019-03-21 11:00:21 -07001037 return 0;
1038}
1039
1040static int __maybe_unused sysc_runtime_resume_legacy(struct device *dev,
1041 struct sysc *ddata)
1042{
1043 struct ti_sysc_platform_data *pdata;
1044 int error;
1045
1046 pdata = dev_get_platdata(ddata->dev);
1047 if (!pdata)
1048 return 0;
1049
1050 if (!pdata->enable_module)
1051 return -ENODEV;
1052
1053 error = pdata->enable_module(dev, &ddata->cookie);
1054 if (error)
1055 dev_err(dev, "%s: could not enable: %i\n",
1056 __func__, error);
1057
Tero Kristobf59ebb2019-10-07 15:29:29 +03001058 reset_control_deassert(ddata->rsts);
1059
Tony Lindgrenff437282019-03-21 11:00:21 -07001060 return 0;
1061}
1062
1063static int __maybe_unused sysc_runtime_suspend(struct device *dev)
1064{
Tony Lindgren0eecc632017-10-10 14:23:43 -07001065 struct sysc *ddata;
Tony Lindgrend8789702019-03-21 11:00:21 -07001066 int error = 0;
Tony Lindgren0eecc632017-10-10 14:23:43 -07001067
1068 ddata = dev_get_drvdata(dev);
1069
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001070 if (!ddata->enabled)
Tony Lindgren0eecc632017-10-10 14:23:43 -07001071 return 0;
1072
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001073 sysc_clkdm_deny_idle(ddata);
1074
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001075 if (ddata->legacy_mode) {
Tony Lindgrenff437282019-03-21 11:00:21 -07001076 error = sysc_runtime_suspend_legacy(dev, ddata);
Tony Lindgren93de83a2019-03-21 11:00:21 -07001077 if (error)
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001078 goto err_allow_idle;
Roger Quadrosd59b6052019-04-08 12:52:39 +03001079 } else {
1080 error = sysc_disable_module(dev);
1081 if (error)
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001082 goto err_allow_idle;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001083 }
1084
Tony Lindgrend8789702019-03-21 11:00:21 -07001085 sysc_disable_main_clocks(ddata);
Tony Lindgren09dfe582018-04-16 10:25:52 -07001086
Tony Lindgrend8789702019-03-21 11:00:21 -07001087 if (sysc_opt_clks_needed(ddata))
1088 sysc_disable_opt_clocks(ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001089
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001090 ddata->enabled = false;
1091
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001092err_allow_idle:
Tero Kristo4345f0d2019-08-07 15:46:04 +03001093 reset_control_assert(ddata->rsts);
Tony Lindgren8383e252019-05-27 04:51:56 -07001094
Tero Kristob6036312019-08-07 15:46:03 +03001095 sysc_clkdm_allow_idle(ddata);
1096
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001097 return error;
Tony Lindgren0eecc632017-10-10 14:23:43 -07001098}
1099
Arnd Bergmanna4a5d492017-10-13 11:25:32 +02001100static int __maybe_unused sysc_runtime_resume(struct device *dev)
Tony Lindgren0eecc632017-10-10 14:23:43 -07001101{
1102 struct sysc *ddata;
Tony Lindgrend8789702019-03-21 11:00:21 -07001103 int error = 0;
Tony Lindgren0eecc632017-10-10 14:23:43 -07001104
1105 ddata = dev_get_drvdata(dev);
1106
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001107 if (ddata->enabled)
Tony Lindgren0eecc632017-10-10 14:23:43 -07001108 return 0;
1109
Tony Lindgren8383e252019-05-27 04:51:56 -07001110
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001111 sysc_clkdm_deny_idle(ddata);
1112
Tony Lindgrend8789702019-03-21 11:00:21 -07001113 if (sysc_opt_clks_needed(ddata)) {
1114 error = sysc_enable_opt_clocks(ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001115 if (error)
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001116 goto err_allow_idle;
Tony Lindgren0eecc632017-10-10 14:23:43 -07001117 }
1118
Tony Lindgrend8789702019-03-21 11:00:21 -07001119 error = sysc_enable_main_clocks(ddata);
1120 if (error)
Tony Lindgren93de83a2019-03-21 11:00:21 -07001121 goto err_opt_clocks;
1122
Tero Kristobf59ebb2019-10-07 15:29:29 +03001123 reset_control_deassert(ddata->rsts);
1124
Tony Lindgren93de83a2019-03-21 11:00:21 -07001125 if (ddata->legacy_mode) {
1126 error = sysc_runtime_resume_legacy(dev, ddata);
1127 if (error)
1128 goto err_main_clocks;
Roger Quadrosd59b6052019-04-08 12:52:39 +03001129 } else {
1130 error = sysc_enable_module(dev);
1131 if (error)
1132 goto err_main_clocks;
Tony Lindgren93de83a2019-03-21 11:00:21 -07001133 }
Tony Lindgrend8789702019-03-21 11:00:21 -07001134
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001135 ddata->enabled = true;
1136
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001137 sysc_clkdm_allow_idle(ddata);
1138
Tony Lindgrend8789702019-03-21 11:00:21 -07001139 return 0;
1140
1141err_main_clocks:
Tony Lindgren93de83a2019-03-21 11:00:21 -07001142 sysc_disable_main_clocks(ddata);
1143err_opt_clocks:
Tony Lindgrend8789702019-03-21 11:00:21 -07001144 if (sysc_opt_clks_needed(ddata))
1145 sysc_disable_opt_clocks(ddata);
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001146err_allow_idle:
1147 sysc_clkdm_allow_idle(ddata);
Tony Lindgrend8789702019-03-21 11:00:21 -07001148
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001149 return error;
Tony Lindgren0eecc632017-10-10 14:23:43 -07001150}
1151
Tony Lindgrenf5e802032018-10-01 09:33:28 -07001152static int __maybe_unused sysc_noirq_suspend(struct device *dev)
Tony Lindgren62020f22018-02-22 13:59:44 -08001153{
1154 struct sysc *ddata;
1155
1156 ddata = dev_get_drvdata(dev);
1157
Tony Lindgren40d9f912018-09-24 12:16:54 -07001158 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
Tony Lindgrene7420c22018-04-16 10:26:46 -07001159 return 0;
1160
Tony Lindgrenf5e802032018-10-01 09:33:28 -07001161 return pm_runtime_force_suspend(dev);
Tony Lindgren62020f22018-02-22 13:59:44 -08001162}
1163
Tony Lindgrenf5e802032018-10-01 09:33:28 -07001164static int __maybe_unused sysc_noirq_resume(struct device *dev)
Tony Lindgren62020f22018-02-22 13:59:44 -08001165{
1166 struct sysc *ddata;
1167
1168 ddata = dev_get_drvdata(dev);
Tony Lindgrene7420c22018-04-16 10:26:46 -07001169
Tony Lindgren40d9f912018-09-24 12:16:54 -07001170 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
Tony Lindgrene7420c22018-04-16 10:26:46 -07001171 return 0;
1172
Tony Lindgrenf5e802032018-10-01 09:33:28 -07001173 return pm_runtime_force_resume(dev);
Tony Lindgrene7420c22018-04-16 10:26:46 -07001174}
1175
Tony Lindgren0eecc632017-10-10 14:23:43 -07001176static const struct dev_pm_ops sysc_pm_ops = {
Tony Lindgrene7420c22018-04-16 10:26:46 -07001177 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(sysc_noirq_suspend, sysc_noirq_resume)
Tony Lindgren0eecc632017-10-10 14:23:43 -07001178 SET_RUNTIME_PM_OPS(sysc_runtime_suspend,
1179 sysc_runtime_resume,
1180 NULL)
1181};
1182
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001183/* Module revision register based quirks */
1184struct sysc_revision_quirk {
1185 const char *name;
1186 u32 base;
1187 int rev_offset;
1188 int sysc_offset;
1189 int syss_offset;
1190 u32 revision;
1191 u32 revision_mask;
1192 u32 quirks;
1193};
1194
1195#define SYSC_QUIRK(optname, optbase, optrev, optsysc, optsyss, \
1196 optrev_val, optrevmask, optquirkmask) \
1197 { \
1198 .name = (optname), \
1199 .base = (optbase), \
1200 .rev_offset = (optrev), \
1201 .sysc_offset = (optsysc), \
1202 .syss_offset = (optsyss), \
1203 .revision = (optrev_val), \
1204 .revision_mask = (optrevmask), \
1205 .quirks = (optquirkmask), \
1206 }
1207
1208static const struct sysc_revision_quirk sysc_revision_quirks[] = {
1209 /* These drivers need to be fixed to not use pm_runtime_irq_safe() */
Tony Lindgren3a3d8022018-09-27 13:33:58 -07001210 SYSC_QUIRK("gpio", 0, 0, 0x10, 0x114, 0x50600801, 0xffff00ff,
Tony Lindgren09dfe582018-04-16 10:25:52 -07001211 SYSC_QUIRK_LEGACY_IDLE | SYSC_QUIRK_OPT_CLKS_IN_RESET),
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001212 SYSC_QUIRK("mmu", 0, 0, 0x10, 0x14, 0x00000020, 0xffffffff,
1213 SYSC_QUIRK_LEGACY_IDLE),
1214 SYSC_QUIRK("mmu", 0, 0, 0x10, 0x14, 0x00000030, 0xffffffff,
1215 SYSC_QUIRK_LEGACY_IDLE),
1216 SYSC_QUIRK("sham", 0, 0x100, 0x110, 0x114, 0x40000c03, 0xffffffff,
1217 SYSC_QUIRK_LEGACY_IDLE),
1218 SYSC_QUIRK("smartreflex", 0, -1, 0x24, -1, 0x00000000, 0xffffffff,
1219 SYSC_QUIRK_LEGACY_IDLE),
1220 SYSC_QUIRK("smartreflex", 0, -1, 0x38, -1, 0x00000000, 0xffffffff,
1221 SYSC_QUIRK_LEGACY_IDLE),
1222 SYSC_QUIRK("timer", 0, 0, 0x10, 0x14, 0x00000015, 0xffffffff,
Tony Lindgren9bd34c62019-01-22 09:03:08 -08001223 0),
Tony Lindgren8cde5d52018-04-16 10:25:15 -07001224 /* Some timers on omap4 and later */
Tony Lindgren3a3d8022018-09-27 13:33:58 -07001225 SYSC_QUIRK("timer", 0, 0, 0x10, -1, 0x50002100, 0xffffffff,
Tony Lindgren072167d2019-01-29 07:53:47 -08001226 0),
Tony Lindgren3a3d8022018-09-27 13:33:58 -07001227 SYSC_QUIRK("timer", 0, 0, 0x10, -1, 0x4fff1301, 0xffff00ff,
Tony Lindgren9bd34c62019-01-22 09:03:08 -08001228 0),
Tony Lindgrenb6a53c42019-05-27 04:51:57 -07001229 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x00000046, 0xffffffff,
1230 SYSC_QUIRK_SWSUP_SIDLE | SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001231 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x00000052, 0xffffffff,
Tony Lindgrenb6a53c42019-05-27 04:51:57 -07001232 SYSC_QUIRK_SWSUP_SIDLE | SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgrend708bb12018-04-16 10:25:32 -07001233 /* Uarts on omap4 and later */
Tony Lindgrenb82beef2018-09-24 12:16:59 -07001234 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x50411e03, 0xffff00ff,
Tony Lindgrenb4a9a7a2019-03-21 13:27:08 -07001235 SYSC_QUIRK_SWSUP_SIDLE_ACT | SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgrenb82beef2018-09-24 12:16:59 -07001236 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x47422e03, 0xffffffff,
Tony Lindgrenb4a9a7a2019-03-21 13:27:08 -07001237 SYSC_QUIRK_SWSUP_SIDLE_ACT | SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgren7e27e5d2018-04-20 14:54:16 -07001238
Tony Lindgrena54275f2019-03-21 11:00:21 -07001239 /* Quirks that need to be set based on the module address */
1240 SYSC_QUIRK("mcpdm", 0x40132000, 0, 0x10, -1, 0x50000800, 0xffffffff,
1241 SYSC_QUIRK_EXT_OPT_CLOCK | SYSC_QUIRK_NO_RESET_ON_INIT |
1242 SYSC_QUIRK_SWSUP_SIDLE),
1243
Tony Lindgren4e23be42019-06-10 04:48:05 -07001244 /* Quirks that need to be set based on detected module */
Tony Lindgren020003f2019-11-14 08:25:28 -08001245 SYSC_QUIRK("aess", 0, 0, 0x10, -1, 0x40000000, 0xffffffff,
1246 SYSC_MODULE_QUIRK_AESS),
Tony Lindgren4e23be42019-06-10 04:48:05 -07001247 SYSC_QUIRK("hdq1w", 0, 0, 0x14, 0x18, 0x00000006, 0xffffffff,
1248 SYSC_MODULE_QUIRK_HDQ1W),
1249 SYSC_QUIRK("hdq1w", 0, 0, 0x14, 0x18, 0x0000000a, 0xffffffff,
1250 SYSC_MODULE_QUIRK_HDQ1W),
1251 SYSC_QUIRK("i2c", 0, 0, 0x20, 0x10, 0x00000036, 0x000000ff,
1252 SYSC_MODULE_QUIRK_I2C),
1253 SYSC_QUIRK("i2c", 0, 0, 0x20, 0x10, 0x0000003c, 0x000000ff,
1254 SYSC_MODULE_QUIRK_I2C),
1255 SYSC_QUIRK("i2c", 0, 0, 0x20, 0x10, 0x00000040, 0x000000ff,
1256 SYSC_MODULE_QUIRK_I2C),
1257 SYSC_QUIRK("i2c", 0, 0, 0x10, 0x90, 0x5040000a, 0xfffff0f0,
1258 SYSC_MODULE_QUIRK_I2C),
Tony Lindgrend7f563d2019-08-14 05:18:16 -07001259 SYSC_QUIRK("gpu", 0x50000000, 0x14, -1, -1, 0x00010201, 0xffffffff, 0),
1260 SYSC_QUIRK("gpu", 0x50000000, 0xfe00, 0xfe10, -1, 0x40000000 , 0xffffffff,
1261 SYSC_MODULE_QUIRK_SGX),
Tony Lindgren03856e92019-10-21 13:36:09 -07001262 SYSC_QUIRK("usb_otg_hs", 0, 0x400, 0x404, 0x408, 0x00000050,
1263 0xffffffff, SYSC_QUIRK_SWSUP_SIDLE | SYSC_QUIRK_SWSUP_MSTANDBY),
Tony Lindgren1819ef22019-10-21 14:15:55 -07001264 SYSC_QUIRK("usb_otg_hs", 0, 0, 0x10, -1, 0x4ea2080d, 0xffffffff,
1265 SYSC_QUIRK_SWSUP_SIDLE | SYSC_QUIRK_SWSUP_MSTANDBY),
Tony Lindgren4e23be42019-06-10 04:48:05 -07001266 SYSC_QUIRK("wdt", 0, 0, 0x10, 0x14, 0x502a0500, 0xfffff0f0,
1267 SYSC_MODULE_QUIRK_WDT),
Tony Lindgrenc7d86692019-10-17 11:21:44 -07001268 /* Watchdog on am3 and am4 */
1269 SYSC_QUIRK("wdt", 0x44e35000, 0, 0x10, 0x14, 0x502a0500, 0xfffff0f0,
1270 SYSC_MODULE_QUIRK_WDT | SYSC_QUIRK_SWSUP_SIDLE),
Tony Lindgren4e23be42019-06-10 04:48:05 -07001271
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001272#ifdef DEBUG
Tony Lindgren1ba30692018-09-24 12:17:05 -07001273 SYSC_QUIRK("adc", 0, 0, 0x10, -1, 0x47300001, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -07001274 SYSC_QUIRK("atl", 0, 0, -1, -1, 0x0a070100, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -07001275 SYSC_QUIRK("cm", 0, 0, -1, -1, 0x40000301, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -07001276 SYSC_QUIRK("control", 0, 0, 0x10, -1, 0x40000900, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001277 SYSC_QUIRK("cpgmac", 0, 0x1200, 0x1208, 0x1204, 0x4edb1902,
Tony Lindgren23731ea2018-09-24 12:17:09 -07001278 0xffff00f0, 0),
Tony Lindgren89bbc6f2019-07-22 03:44:42 -07001279 SYSC_QUIRK("dcan", 0, 0x20, -1, -1, 0xa3170504, 0xffffffff, 0),
1280 SYSC_QUIRK("dcan", 0, 0x20, -1, -1, 0x4edb1902, 0xffffffff, 0),
Tony Lindgren13aad512019-03-21 11:00:21 -07001281 SYSC_QUIRK("dmic", 0, 0, 0x10, -1, 0x50010000, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001282 SYSC_QUIRK("dwc3", 0, 0, 0x10, -1, 0x500a0200, 0xffffffff, 0),
Tony Lindgren7edd00f2019-08-14 05:18:16 -07001283 SYSC_QUIRK("d2d", 0x4a0b6000, 0, 0x10, 0x14, 0x00000010, 0xffffffff, 0),
1284 SYSC_QUIRK("d2d", 0x4a0cd000, 0, 0x10, 0x14, 0x00000010, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001285 SYSC_QUIRK("epwmss", 0, 0, 0x4, -1, 0x47400001, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001286 SYSC_QUIRK("gpu", 0, 0x1fc00, 0x1fc10, -1, 0, 0, 0),
Tony Lindgrend7f563d2019-08-14 05:18:16 -07001287 SYSC_QUIRK("gpu", 0, 0xfe00, 0xfe10, -1, 0x40000000 , 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001288 SYSC_QUIRK("hsi", 0, 0, 0x10, 0x14, 0x50043101, 0xffffffff, 0),
1289 SYSC_QUIRK("iss", 0, 0, 0x10, -1, 0x40000101, 0xffffffff, 0),
Tony Lindgren23731ea2018-09-24 12:17:09 -07001290 SYSC_QUIRK("lcdc", 0, 0, 0x54, -1, 0x4f201000, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001291 SYSC_QUIRK("mcasp", 0, 0, 0x4, -1, 0x44306302, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001292 SYSC_QUIRK("mcasp", 0, 0, 0x4, -1, 0x44307b02, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001293 SYSC_QUIRK("mcbsp", 0, -1, 0x8c, -1, 0, 0, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -07001294 SYSC_QUIRK("mcspi", 0, 0, 0x10, -1, 0x40300a0b, 0xffff00ff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001295 SYSC_QUIRK("mcspi", 0, 0, 0x110, 0x114, 0x40300a0b, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001296 SYSC_QUIRK("mailbox", 0, 0, 0x10, -1, 0x00000400, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001297 SYSC_QUIRK("m3", 0, 0, -1, -1, 0x5f580105, 0x0fff0f00, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -07001298 SYSC_QUIRK("ocp2scp", 0, 0, 0x10, 0x14, 0x50060005, 0xfffffff0, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001299 SYSC_QUIRK("ocp2scp", 0, 0, -1, -1, 0x50060007, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -07001300 SYSC_QUIRK("padconf", 0, 0, 0x10, -1, 0x4fff0800, 0xffffffff, 0),
Tony Lindgrenf0106702018-11-15 14:46:53 -08001301 SYSC_QUIRK("padconf", 0, 0, -1, -1, 0x40001100, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -07001302 SYSC_QUIRK("prcm", 0, 0, -1, -1, 0x40000100, 0xffffffff, 0),
Tony Lindgren23731ea2018-09-24 12:17:09 -07001303 SYSC_QUIRK("prcm", 0, 0, -1, -1, 0x00004102, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001304 SYSC_QUIRK("prcm", 0, 0, -1, -1, 0x40000400, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -07001305 SYSC_QUIRK("scm", 0, 0, 0x10, -1, 0x40000900, 0xffffffff, 0),
Tony Lindgren23731ea2018-09-24 12:17:09 -07001306 SYSC_QUIRK("scm", 0, 0, -1, -1, 0x4e8b0100, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001307 SYSC_QUIRK("scm", 0, 0, -1, -1, 0x4f000100, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -07001308 SYSC_QUIRK("scm", 0, 0, -1, -1, 0x40000900, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -07001309 SYSC_QUIRK("scrm", 0, 0, -1, -1, 0x00000010, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -07001310 SYSC_QUIRK("sdio", 0, 0, 0x10, -1, 0x40202301, 0xffff0ff0, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001311 SYSC_QUIRK("sdio", 0, 0x2fc, 0x110, 0x114, 0x31010000, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -07001312 SYSC_QUIRK("sdma", 0, 0, 0x2c, 0x28, 0x00010900, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001313 SYSC_QUIRK("slimbus", 0, 0, 0x10, -1, 0x40000902, 0xffffffff, 0),
1314 SYSC_QUIRK("slimbus", 0, 0, 0x10, -1, 0x40002903, 0xffffffff, 0),
1315 SYSC_QUIRK("spinlock", 0, 0, 0x10, -1, 0x50020000, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001316 SYSC_QUIRK("rng", 0, 0x1fe0, 0x1fe4, -1, 0x00000020, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -07001317 SYSC_QUIRK("rtc", 0, 0x74, 0x78, -1, 0x4eb01908, 0xffff00f0, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001318 SYSC_QUIRK("timer32k", 0, 0, 0x4, -1, 0x00000060, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001319 SYSC_QUIRK("usbhstll", 0, 0, 0x10, 0x14, 0x00000004, 0xffffffff, 0),
Tony Lindgrenf0106702018-11-15 14:46:53 -08001320 SYSC_QUIRK("usbhstll", 0, 0, 0x10, 0x14, 0x00000008, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001321 SYSC_QUIRK("usb_host_hs", 0, 0, 0x10, 0x14, 0x50700100, 0xffffffff, 0),
Tony Lindgrenf0106702018-11-15 14:46:53 -08001322 SYSC_QUIRK("usb_host_hs", 0, 0, 0x10, -1, 0x50700101, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001323 SYSC_QUIRK("vfpe", 0, 0, 0x104, -1, 0x4d001200, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001324#endif
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001325};
1326
Tony Lindgren42b9c5c2019-03-21 11:00:21 -07001327/*
1328 * Early quirks based on module base and register offsets only that are
1329 * needed before the module revision can be read
1330 */
1331static void sysc_init_early_quirks(struct sysc *ddata)
1332{
1333 const struct sysc_revision_quirk *q;
1334 int i;
1335
1336 for (i = 0; i < ARRAY_SIZE(sysc_revision_quirks); i++) {
1337 q = &sysc_revision_quirks[i];
1338
1339 if (!q->base)
1340 continue;
1341
1342 if (q->base != ddata->module_pa)
1343 continue;
1344
1345 if (q->rev_offset >= 0 &&
1346 q->rev_offset != ddata->offsets[SYSC_REVISION])
1347 continue;
1348
1349 if (q->sysc_offset >= 0 &&
1350 q->sysc_offset != ddata->offsets[SYSC_SYSCONFIG])
1351 continue;
1352
1353 if (q->syss_offset >= 0 &&
1354 q->syss_offset != ddata->offsets[SYSC_SYSSTATUS])
1355 continue;
1356
1357 ddata->name = q->name;
1358 ddata->cfg.quirks |= q->quirks;
1359 }
1360}
1361
1362/* Quirks that also consider the revision register value */
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001363static void sysc_init_revision_quirks(struct sysc *ddata)
1364{
1365 const struct sysc_revision_quirk *q;
1366 int i;
1367
1368 for (i = 0; i < ARRAY_SIZE(sysc_revision_quirks); i++) {
1369 q = &sysc_revision_quirks[i];
1370
1371 if (q->base && q->base != ddata->module_pa)
1372 continue;
1373
1374 if (q->rev_offset >= 0 &&
1375 q->rev_offset != ddata->offsets[SYSC_REVISION])
1376 continue;
1377
1378 if (q->sysc_offset >= 0 &&
1379 q->sysc_offset != ddata->offsets[SYSC_SYSCONFIG])
1380 continue;
1381
1382 if (q->syss_offset >= 0 &&
1383 q->syss_offset != ddata->offsets[SYSC_SYSSTATUS])
1384 continue;
1385
1386 if (q->revision == ddata->revision ||
1387 (q->revision & q->revision_mask) ==
1388 (ddata->revision & q->revision_mask)) {
1389 ddata->name = q->name;
1390 ddata->cfg.quirks |= q->quirks;
1391 }
1392 }
1393}
1394
Tony Lindgren4e23be42019-06-10 04:48:05 -07001395/* 1-wire needs module's internal clocks enabled for reset */
1396static void sysc_clk_enable_quirk_hdq1w(struct sysc *ddata)
1397{
1398 int offset = 0x0c; /* HDQ_CTRL_STATUS */
1399 u16 val;
1400
1401 val = sysc_read(ddata, offset);
1402 val |= BIT(5);
1403 sysc_write(ddata, offset, val);
1404}
1405
Tony Lindgren020003f2019-11-14 08:25:28 -08001406/* AESS (Audio Engine SubSystem) needs autogating set after enable */
1407static void sysc_module_enable_quirk_aess(struct sysc *ddata)
1408{
1409 int offset = 0x7c; /* AESS_AUTO_GATING_ENABLE */
1410
1411 sysc_write(ddata, offset, 1);
1412}
1413
Tony Lindgren4e23be42019-06-10 04:48:05 -07001414/* I2C needs extra enable bit toggling for reset */
1415static void sysc_clk_quirk_i2c(struct sysc *ddata, bool enable)
1416{
1417 int offset;
1418 u16 val;
1419
1420 /* I2C_CON, omap2/3 is different from omap4 and later */
1421 if ((ddata->revision & 0xffffff00) == 0x001f0000)
1422 offset = 0x24;
1423 else
1424 offset = 0xa4;
1425
1426 /* I2C_EN */
1427 val = sysc_read(ddata, offset);
1428 if (enable)
1429 val |= BIT(15);
1430 else
1431 val &= ~BIT(15);
1432 sysc_write(ddata, offset, val);
1433}
1434
1435static void sysc_clk_enable_quirk_i2c(struct sysc *ddata)
1436{
1437 sysc_clk_quirk_i2c(ddata, true);
1438}
1439
1440static void sysc_clk_disable_quirk_i2c(struct sysc *ddata)
1441{
1442 sysc_clk_quirk_i2c(ddata, false);
1443}
1444
Tony Lindgrend7f563d2019-08-14 05:18:16 -07001445/* 36xx SGX needs a quirk for to bypass OCP IPG interrupt logic */
1446static void sysc_module_enable_quirk_sgx(struct sysc *ddata)
1447{
1448 int offset = 0xff08; /* OCP_DEBUG_CONFIG */
1449 u32 val = BIT(31); /* THALIA_INT_BYPASS */
1450
1451 sysc_write(ddata, offset, val);
1452}
1453
Tony Lindgren4e23be42019-06-10 04:48:05 -07001454/* Watchdog timer needs a disable sequence after reset */
1455static void sysc_reset_done_quirk_wdt(struct sysc *ddata)
1456{
1457 int wps, spr, error;
1458 u32 val;
1459
1460 wps = 0x34;
1461 spr = 0x48;
1462
1463 sysc_write(ddata, spr, 0xaaaa);
1464 error = readl_poll_timeout(ddata->module_va + wps, val,
1465 !(val & 0x10), 100,
1466 MAX_MODULE_SOFTRESET_WAIT);
1467 if (error)
Tony Lindgrenc7d86692019-10-17 11:21:44 -07001468 dev_warn(ddata->dev, "wdt disable step1 failed\n");
Tony Lindgren4e23be42019-06-10 04:48:05 -07001469
Tony Lindgrenc7d86692019-10-17 11:21:44 -07001470 sysc_write(ddata, spr, 0x5555);
Tony Lindgren4e23be42019-06-10 04:48:05 -07001471 error = readl_poll_timeout(ddata->module_va + wps, val,
1472 !(val & 0x10), 100,
1473 MAX_MODULE_SOFTRESET_WAIT);
1474 if (error)
Tony Lindgrenc7d86692019-10-17 11:21:44 -07001475 dev_warn(ddata->dev, "wdt disable step2 failed\n");
Tony Lindgren4e23be42019-06-10 04:48:05 -07001476}
1477
1478static void sysc_init_module_quirks(struct sysc *ddata)
1479{
1480 if (ddata->legacy_mode || !ddata->name)
1481 return;
1482
1483 if (ddata->cfg.quirks & SYSC_MODULE_QUIRK_HDQ1W) {
1484 ddata->clk_enable_quirk = sysc_clk_enable_quirk_hdq1w;
1485
1486 return;
1487 }
1488
1489 if (ddata->cfg.quirks & SYSC_MODULE_QUIRK_I2C) {
1490 ddata->clk_enable_quirk = sysc_clk_enable_quirk_i2c;
1491 ddata->clk_disable_quirk = sysc_clk_disable_quirk_i2c;
1492
1493 return;
1494 }
1495
Tony Lindgren020003f2019-11-14 08:25:28 -08001496 if (ddata->cfg.quirks & SYSC_MODULE_QUIRK_AESS)
1497 ddata->module_enable_quirk = sysc_module_enable_quirk_aess;
1498
Tony Lindgrend7f563d2019-08-14 05:18:16 -07001499 if (ddata->cfg.quirks & SYSC_MODULE_QUIRK_SGX)
1500 ddata->module_enable_quirk = sysc_module_enable_quirk_sgx;
1501
Tony Lindgrenc7d86692019-10-17 11:21:44 -07001502 if (ddata->cfg.quirks & SYSC_MODULE_QUIRK_WDT) {
Tony Lindgren4e23be42019-06-10 04:48:05 -07001503 ddata->reset_done_quirk = sysc_reset_done_quirk_wdt;
Tony Lindgrenc7d86692019-10-17 11:21:44 -07001504 ddata->module_disable_quirk = sysc_reset_done_quirk_wdt;
1505 }
Tony Lindgren4e23be42019-06-10 04:48:05 -07001506}
1507
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001508static int sysc_clockdomain_init(struct sysc *ddata)
1509{
1510 struct ti_sysc_platform_data *pdata = dev_get_platdata(ddata->dev);
1511 struct clk *fck = NULL, *ick = NULL;
1512 int error;
1513
1514 if (!pdata || !pdata->init_clockdomain)
1515 return 0;
1516
1517 switch (ddata->nr_clocks) {
1518 case 2:
1519 ick = ddata->clocks[SYSC_ICK];
1520 /* fallthrough */
1521 case 1:
1522 fck = ddata->clocks[SYSC_FCK];
1523 break;
1524 case 0:
1525 return 0;
1526 }
1527
1528 error = pdata->init_clockdomain(ddata->dev, fck, ick, &ddata->cookie);
1529 if (!error || error == -ENODEV)
1530 return 0;
1531
1532 return error;
1533}
1534
Tony Lindgrena3e92e72019-03-21 11:00:21 -07001535/*
1536 * Note that pdata->init_module() typically does a reset first. After
1537 * pdata->init_module() is done, PM runtime can be used for the interconnect
1538 * target module.
1539 */
1540static int sysc_legacy_init(struct sysc *ddata)
1541{
1542 struct ti_sysc_platform_data *pdata = dev_get_platdata(ddata->dev);
1543 int error;
1544
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001545 if (!pdata || !pdata->init_module)
Tony Lindgrena3e92e72019-03-21 11:00:21 -07001546 return 0;
1547
1548 error = pdata->init_module(ddata->dev, ddata->mdata, &ddata->cookie);
1549 if (error == -EEXIST)
1550 error = 0;
1551
1552 return error;
1553}
1554
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001555/*
1556 * Note that the caller must ensure the interconnect target module is enabled
1557 * before calling reset. Otherwise reset will not complete.
1558 */
Faiz Abbas596e7952018-07-09 22:18:39 +05301559static int sysc_reset(struct sysc *ddata)
1560{
YueHaibingc8a738f2019-06-28 12:10:54 +08001561 int sysc_offset, syss_offset, sysc_val, rstval, error = 0;
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001562 u32 sysc_mask, syss_done;
Faiz Abbas596e7952018-07-09 22:18:39 +05301563
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001564 sysc_offset = ddata->offsets[SYSC_SYSCONFIG];
1565 syss_offset = ddata->offsets[SYSC_SYSSTATUS];
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001566
1567 if (ddata->legacy_mode || sysc_offset < 0 ||
1568 ddata->cap->regbits->srst_shift < 0 ||
Faiz Abbas596e7952018-07-09 22:18:39 +05301569 ddata->cfg.quirks & SYSC_QUIRK_NO_RESET_ON_INIT)
1570 return 0;
1571
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001572 sysc_mask = BIT(ddata->cap->regbits->srst_shift);
Faiz Abbas596e7952018-07-09 22:18:39 +05301573
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001574 if (ddata->cfg.quirks & SYSS_QUIRK_RESETDONE_INVERTED)
1575 syss_done = 0;
1576 else
1577 syss_done = ddata->cfg.syss_mask;
1578
Tony Lindgren4e23be42019-06-10 04:48:05 -07001579 if (ddata->clk_disable_quirk)
1580 ddata->clk_disable_quirk(ddata);
1581
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001582 sysc_val = sysc_read_sysconfig(ddata);
1583 sysc_val |= sysc_mask;
1584 sysc_write(ddata, sysc_offset, sysc_val);
Faiz Abbas596e7952018-07-09 22:18:39 +05301585
Tony Lindgren4e23be42019-06-10 04:48:05 -07001586 if (ddata->clk_enable_quirk)
1587 ddata->clk_enable_quirk(ddata);
1588
Faiz Abbas596e7952018-07-09 22:18:39 +05301589 /* Poll on reset status */
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001590 if (syss_offset >= 0) {
1591 error = readx_poll_timeout(sysc_read_sysstatus, ddata, rstval,
1592 (rstval & ddata->cfg.syss_mask) ==
1593 syss_done,
1594 100, MAX_MODULE_SOFTRESET_WAIT);
Faiz Abbas596e7952018-07-09 22:18:39 +05301595
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001596 } else if (ddata->cfg.quirks & SYSC_QUIRK_RESET_STATUS) {
1597 error = readx_poll_timeout(sysc_read_sysconfig, ddata, rstval,
1598 !(rstval & sysc_mask),
1599 100, MAX_MODULE_SOFTRESET_WAIT);
1600 }
1601
Tony Lindgren4e23be42019-06-10 04:48:05 -07001602 if (ddata->reset_done_quirk)
1603 ddata->reset_done_quirk(ddata);
1604
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001605 return error;
Faiz Abbas596e7952018-07-09 22:18:39 +05301606}
1607
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001608/*
1609 * At this point the module is configured enough to read the revision but
1610 * module may not be completely configured yet to use PM runtime. Enable
1611 * all clocks directly during init to configure the quirks needed for PM
1612 * runtime based on the revision register.
1613 */
Tony Lindgren566a9b052017-12-15 09:41:19 -08001614static int sysc_init_module(struct sysc *ddata)
1615{
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001616 int error = 0;
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001617
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001618 error = sysc_clockdomain_init(ddata);
1619 if (error)
1620 return error;
1621
Tony Lindgrend0989132019-09-05 07:37:22 -07001622 sysc_clkdm_deny_idle(ddata);
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001623
Tony Lindgrend0989132019-09-05 07:37:22 -07001624 /*
1625 * Always enable clocks. The bootloader may or may not have enabled
1626 * the related clocks.
1627 */
1628 error = sysc_enable_opt_clocks(ddata);
1629 if (error)
1630 return error;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001631
Tony Lindgrend0989132019-09-05 07:37:22 -07001632 error = sysc_enable_main_clocks(ddata);
1633 if (error)
1634 goto err_opt_clocks;
Faiz Abbas596e7952018-07-09 22:18:39 +05301635
Tony Lindgrenea5a2e42019-05-27 04:51:56 -07001636 if (!(ddata->cfg.quirks & SYSC_QUIRK_NO_RESET_ON_INIT)) {
Tero Kristodf4f3452019-10-07 15:29:30 +03001637 error = reset_control_deassert(ddata->rsts);
Tony Lindgrenea5a2e42019-05-27 04:51:56 -07001638 if (error)
1639 goto err_main_clocks;
1640 }
1641
Tony Lindgren566a9b052017-12-15 09:41:19 -08001642 ddata->revision = sysc_read_revision(ddata);
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001643 sysc_init_revision_quirks(ddata);
Tony Lindgren4e23be42019-06-10 04:48:05 -07001644 sysc_init_module_quirks(ddata);
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001645
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001646 if (ddata->legacy_mode) {
1647 error = sysc_legacy_init(ddata);
1648 if (error)
Tero Kristocdc56c12019-10-07 15:29:31 +03001649 goto err_reset;
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001650 }
1651
Tony Lindgrend0989132019-09-05 07:37:22 -07001652 if (!ddata->legacy_mode) {
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001653 error = sysc_enable_module(ddata->dev);
1654 if (error)
Tero Kristocdc56c12019-10-07 15:29:31 +03001655 goto err_reset;
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001656 }
Tony Lindgrena3e92e72019-03-21 11:00:21 -07001657
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001658 error = sysc_reset(ddata);
1659 if (error)
1660 dev_err(ddata->dev, "Reset failed with %d\n", error);
1661
Tero Kristocdc56c12019-10-07 15:29:31 +03001662 if (error && !ddata->legacy_mode)
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001663 sysc_disable_module(ddata->dev);
1664
Tero Kristocdc56c12019-10-07 15:29:31 +03001665err_reset:
1666 if (error && !(ddata->cfg.quirks & SYSC_QUIRK_NO_RESET_ON_INIT))
1667 reset_control_assert(ddata->rsts);
1668
Tony Lindgrena3e92e72019-03-21 11:00:21 -07001669err_main_clocks:
Tero Kristocdc56c12019-10-07 15:29:31 +03001670 if (error)
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001671 sysc_disable_main_clocks(ddata);
1672err_opt_clocks:
Tony Lindgrend0989132019-09-05 07:37:22 -07001673 /* No re-enable of clockdomain autoidle to prevent module autoidle */
Tero Kristocdc56c12019-10-07 15:29:31 +03001674 if (error) {
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001675 sysc_disable_opt_clocks(ddata);
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001676 sysc_clkdm_allow_idle(ddata);
1677 }
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001678
1679 return error;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001680}
1681
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08001682static int sysc_init_sysc_mask(struct sysc *ddata)
1683{
1684 struct device_node *np = ddata->dev->of_node;
1685 int error;
1686 u32 val;
1687
1688 error = of_property_read_u32(np, "ti,sysc-mask", &val);
1689 if (error)
1690 return 0;
1691
Tony Lindgrene212abd2019-07-22 03:44:52 -07001692 ddata->cfg.sysc_val = val & ddata->cap->sysc_mask;
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08001693
1694 return 0;
1695}
1696
1697static int sysc_init_idlemode(struct sysc *ddata, u8 *idlemodes,
1698 const char *name)
1699{
1700 struct device_node *np = ddata->dev->of_node;
1701 struct property *prop;
1702 const __be32 *p;
1703 u32 val;
1704
1705 of_property_for_each_u32(np, name, prop, p, val) {
1706 if (val >= SYSC_NR_IDLEMODES) {
1707 dev_err(ddata->dev, "invalid idlemode: %i\n", val);
1708 return -EINVAL;
1709 }
1710 *idlemodes |= (1 << val);
1711 }
1712
1713 return 0;
1714}
1715
1716static int sysc_init_idlemodes(struct sysc *ddata)
1717{
1718 int error;
1719
1720 error = sysc_init_idlemode(ddata, &ddata->cfg.midlemodes,
1721 "ti,sysc-midle");
1722 if (error)
1723 return error;
1724
1725 error = sysc_init_idlemode(ddata, &ddata->cfg.sidlemodes,
1726 "ti,sysc-sidle");
1727 if (error)
1728 return error;
1729
1730 return 0;
1731}
1732
1733/*
1734 * Only some devices on omap4 and later have SYSCONFIG reset done
1735 * bit. We can detect this if there is no SYSSTATUS at all, or the
1736 * SYSTATUS bit 0 is not used. Note that some SYSSTATUS registers
1737 * have multiple bits for the child devices like OHCI and EHCI.
1738 * Depends on SYSC being parsed first.
1739 */
1740static int sysc_init_syss_mask(struct sysc *ddata)
1741{
1742 struct device_node *np = ddata->dev->of_node;
1743 int error;
1744 u32 val;
1745
1746 error = of_property_read_u32(np, "ti,syss-mask", &val);
1747 if (error) {
1748 if ((ddata->cap->type == TI_SYSC_OMAP4 ||
1749 ddata->cap->type == TI_SYSC_OMAP4_TIMER) &&
1750 (ddata->cfg.sysc_val & SYSC_OMAP4_SOFTRESET))
1751 ddata->cfg.quirks |= SYSC_QUIRK_RESET_STATUS;
1752
1753 return 0;
1754 }
1755
1756 if (!(val & 1) && (ddata->cfg.sysc_val & SYSC_OMAP4_SOFTRESET))
1757 ddata->cfg.quirks |= SYSC_QUIRK_RESET_STATUS;
1758
1759 ddata->cfg.syss_mask = val;
1760
1761 return 0;
1762}
1763
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001764/*
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001765 * Many child device drivers need to have fck and opt clocks available
1766 * to get the clock rate for device internal configuration etc.
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001767 */
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001768static int sysc_child_add_named_clock(struct sysc *ddata,
1769 struct device *child,
1770 const char *name)
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001771{
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001772 struct clk *clk;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001773 struct clk_lookup *l;
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001774 int error = 0;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001775
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001776 if (!name)
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001777 return 0;
1778
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001779 clk = clk_get(child, name);
1780 if (!IS_ERR(clk)) {
Markus Elfringcb6cfe22019-11-06 19:12:30 +01001781 error = -EEXIST;
1782 goto put_clk;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001783 }
1784
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001785 clk = clk_get(ddata->dev, name);
1786 if (IS_ERR(clk))
1787 return -ENODEV;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001788
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001789 l = clkdev_create(clk, name, dev_name(child));
1790 if (!l)
1791 error = -ENOMEM;
Markus Elfringcb6cfe22019-11-06 19:12:30 +01001792put_clk:
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001793 clk_put(clk);
1794
1795 return error;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001796}
1797
Tony Lindgren09dfe582018-04-16 10:25:52 -07001798static int sysc_child_add_clocks(struct sysc *ddata,
1799 struct device *child)
1800{
1801 int i, error;
1802
1803 for (i = 0; i < ddata->nr_clocks; i++) {
1804 error = sysc_child_add_named_clock(ddata,
1805 child,
1806 ddata->clock_roles[i]);
1807 if (error && error != -EEXIST) {
1808 dev_err(ddata->dev, "could not add child clock %s: %i\n",
1809 ddata->clock_roles[i], error);
1810
1811 return error;
1812 }
1813 }
1814
1815 return 0;
1816}
1817
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001818static struct device_type sysc_device_type = {
1819};
1820
1821static struct sysc *sysc_child_to_parent(struct device *dev)
1822{
1823 struct device *parent = dev->parent;
1824
1825 if (!parent || parent->type != &sysc_device_type)
1826 return NULL;
1827
1828 return dev_get_drvdata(parent);
1829}
1830
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001831static int __maybe_unused sysc_child_runtime_suspend(struct device *dev)
1832{
1833 struct sysc *ddata;
1834 int error;
1835
1836 ddata = sysc_child_to_parent(dev);
1837
1838 error = pm_generic_runtime_suspend(dev);
1839 if (error)
1840 return error;
1841
1842 if (!ddata->enabled)
1843 return 0;
1844
1845 return sysc_runtime_suspend(ddata->dev);
1846}
1847
1848static int __maybe_unused sysc_child_runtime_resume(struct device *dev)
1849{
1850 struct sysc *ddata;
1851 int error;
1852
1853 ddata = sysc_child_to_parent(dev);
1854
1855 if (!ddata->enabled) {
1856 error = sysc_runtime_resume(ddata->dev);
1857 if (error < 0)
1858 dev_err(ddata->dev,
1859 "%s error: %i\n", __func__, error);
1860 }
1861
1862 return pm_generic_runtime_resume(dev);
1863}
1864
1865#ifdef CONFIG_PM_SLEEP
1866static int sysc_child_suspend_noirq(struct device *dev)
1867{
1868 struct sysc *ddata;
1869 int error;
1870
1871 ddata = sysc_child_to_parent(dev);
1872
Tony Lindgrenef55f822018-04-16 10:27:15 -07001873 dev_dbg(ddata->dev, "%s %s\n", __func__,
1874 ddata->name ? ddata->name : "");
1875
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001876 error = pm_generic_suspend_noirq(dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001877 if (error) {
1878 dev_err(dev, "%s error at %i: %i\n",
1879 __func__, __LINE__, error);
1880
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001881 return error;
Tony Lindgrenef55f822018-04-16 10:27:15 -07001882 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001883
1884 if (!pm_runtime_status_suspended(dev)) {
1885 error = pm_generic_runtime_suspend(dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001886 if (error) {
Tony Lindgrenf9490782018-09-28 15:21:50 -07001887 dev_dbg(dev, "%s busy at %i: %i\n",
1888 __func__, __LINE__, error);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001889
Tony Lindgren4f3530f2018-08-08 01:07:06 -07001890 return 0;
Tony Lindgrenef55f822018-04-16 10:27:15 -07001891 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001892
1893 error = sysc_runtime_suspend(ddata->dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001894 if (error) {
1895 dev_err(dev, "%s error at %i: %i\n",
1896 __func__, __LINE__, error);
1897
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001898 return error;
Tony Lindgrenef55f822018-04-16 10:27:15 -07001899 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001900
1901 ddata->child_needs_resume = true;
1902 }
1903
1904 return 0;
1905}
1906
1907static int sysc_child_resume_noirq(struct device *dev)
1908{
1909 struct sysc *ddata;
1910 int error;
1911
1912 ddata = sysc_child_to_parent(dev);
1913
Tony Lindgrenef55f822018-04-16 10:27:15 -07001914 dev_dbg(ddata->dev, "%s %s\n", __func__,
1915 ddata->name ? ddata->name : "");
1916
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001917 if (ddata->child_needs_resume) {
1918 ddata->child_needs_resume = false;
1919
1920 error = sysc_runtime_resume(ddata->dev);
1921 if (error)
1922 dev_err(ddata->dev,
1923 "%s runtime resume error: %i\n",
1924 __func__, error);
1925
1926 error = pm_generic_runtime_resume(dev);
1927 if (error)
1928 dev_err(ddata->dev,
1929 "%s generic runtime resume: %i\n",
1930 __func__, error);
1931 }
1932
1933 return pm_generic_resume_noirq(dev);
1934}
1935#endif
1936
Tony Lindgrenb7182b42019-03-21 11:00:21 -07001937static struct dev_pm_domain sysc_child_pm_domain = {
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001938 .ops = {
1939 SET_RUNTIME_PM_OPS(sysc_child_runtime_suspend,
1940 sysc_child_runtime_resume,
1941 NULL)
1942 USE_PLATFORM_PM_SLEEP_OPS
1943 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(sysc_child_suspend_noirq,
1944 sysc_child_resume_noirq)
1945 }
1946};
1947
1948/**
1949 * sysc_legacy_idle_quirk - handle children in omap_device compatible way
1950 * @ddata: device driver data
1951 * @child: child device driver
1952 *
1953 * Allow idle for child devices as done with _od_runtime_suspend().
1954 * Otherwise many child devices will not idle because of the permanent
1955 * parent usecount set in pm_runtime_irq_safe().
1956 *
1957 * Note that the long term solution is to just modify the child device
1958 * drivers to not set pm_runtime_irq_safe() and then this can be just
1959 * dropped.
1960 */
1961static void sysc_legacy_idle_quirk(struct sysc *ddata, struct device *child)
1962{
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001963 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
1964 dev_pm_domain_set(child, &sysc_child_pm_domain);
1965}
1966
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001967static int sysc_notifier_call(struct notifier_block *nb,
1968 unsigned long event, void *device)
1969{
1970 struct device *dev = device;
1971 struct sysc *ddata;
1972 int error;
1973
1974 ddata = sysc_child_to_parent(dev);
1975 if (!ddata)
1976 return NOTIFY_DONE;
1977
1978 switch (event) {
1979 case BUS_NOTIFY_ADD_DEVICE:
Tony Lindgren09dfe582018-04-16 10:25:52 -07001980 error = sysc_child_add_clocks(ddata, dev);
1981 if (error)
1982 return error;
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001983 sysc_legacy_idle_quirk(ddata, dev);
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001984 break;
1985 default:
1986 break;
1987 }
1988
1989 return NOTIFY_DONE;
1990}
1991
1992static struct notifier_block sysc_nb = {
1993 .notifier_call = sysc_notifier_call,
1994};
1995
Tony Lindgren566a9b052017-12-15 09:41:19 -08001996/* Device tree configured quirks */
1997struct sysc_dts_quirk {
1998 const char *name;
1999 u32 mask;
2000};
2001
2002static const struct sysc_dts_quirk sysc_dts_quirks[] = {
2003 { .name = "ti,no-idle-on-init",
2004 .mask = SYSC_QUIRK_NO_IDLE_ON_INIT, },
2005 { .name = "ti,no-reset-on-init",
2006 .mask = SYSC_QUIRK_NO_RESET_ON_INIT, },
Tony Lindgren386cb762019-03-22 07:49:30 -07002007 { .name = "ti,no-idle",
2008 .mask = SYSC_QUIRK_NO_IDLE, },
Tony Lindgren566a9b052017-12-15 09:41:19 -08002009};
2010
Tony Lindgren4014c082018-12-10 14:11:26 -08002011static void sysc_parse_dts_quirks(struct sysc *ddata, struct device_node *np,
2012 bool is_child)
Tony Lindgren566a9b052017-12-15 09:41:19 -08002013{
Tony Lindgren566a9b052017-12-15 09:41:19 -08002014 const struct property *prop;
Tony Lindgren4014c082018-12-10 14:11:26 -08002015 int i, len;
Tony Lindgren566a9b052017-12-15 09:41:19 -08002016
2017 for (i = 0; i < ARRAY_SIZE(sysc_dts_quirks); i++) {
Tony Lindgren4014c082018-12-10 14:11:26 -08002018 const char *name = sysc_dts_quirks[i].name;
2019
2020 prop = of_get_property(np, name, &len);
Tony Lindgren566a9b052017-12-15 09:41:19 -08002021 if (!prop)
Tony Lindgrend39b6ea2018-02-15 09:18:55 -08002022 continue;
Tony Lindgren566a9b052017-12-15 09:41:19 -08002023
2024 ddata->cfg.quirks |= sysc_dts_quirks[i].mask;
Tony Lindgren4014c082018-12-10 14:11:26 -08002025 if (is_child) {
2026 dev_warn(ddata->dev,
2027 "dts flag should be at module level for %s\n",
2028 name);
2029 }
Tony Lindgren566a9b052017-12-15 09:41:19 -08002030 }
Tony Lindgren4014c082018-12-10 14:11:26 -08002031}
Tony Lindgren566a9b052017-12-15 09:41:19 -08002032
Tony Lindgren4014c082018-12-10 14:11:26 -08002033static int sysc_init_dts_quirks(struct sysc *ddata)
2034{
2035 struct device_node *np = ddata->dev->of_node;
2036 int error;
2037 u32 val;
2038
2039 ddata->legacy_mode = of_get_property(np, "ti,hwmods", NULL);
2040
2041 sysc_parse_dts_quirks(ddata, np, false);
Tony Lindgren566a9b052017-12-15 09:41:19 -08002042 error = of_property_read_u32(np, "ti,sysc-delay-us", &val);
2043 if (!error) {
2044 if (val > 255) {
2045 dev_warn(ddata->dev, "bad ti,sysc-delay-us: %i\n",
2046 val);
2047 }
2048
2049 ddata->cfg.srst_udelay = (u8)val;
2050 }
2051
2052 return 0;
2053}
2054
Tony Lindgren0eecc632017-10-10 14:23:43 -07002055static void sysc_unprepare(struct sysc *ddata)
2056{
2057 int i;
2058
Tony Lindgrenaaa29bb2019-03-21 11:00:21 -07002059 if (!ddata->clocks)
2060 return;
2061
Tony Lindgren0eecc632017-10-10 14:23:43 -07002062 for (i = 0; i < SYSC_MAX_CLOCKS; i++) {
2063 if (!IS_ERR_OR_NULL(ddata->clocks[i]))
2064 clk_unprepare(ddata->clocks[i]);
2065 }
2066}
2067
Tony Lindgren70a65242017-12-15 09:41:09 -08002068/*
2069 * Common sysc register bits found on omap2, also known as type1
2070 */
2071static const struct sysc_regbits sysc_regbits_omap2 = {
2072 .dmadisable_shift = -ENODEV,
2073 .midle_shift = 12,
2074 .sidle_shift = 3,
2075 .clkact_shift = 8,
2076 .emufree_shift = 5,
2077 .enwkup_shift = 2,
2078 .srst_shift = 1,
2079 .autoidle_shift = 0,
2080};
2081
2082static const struct sysc_capabilities sysc_omap2 = {
2083 .type = TI_SYSC_OMAP2,
2084 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY | SYSC_OMAP2_EMUFREE |
2085 SYSC_OMAP2_ENAWAKEUP | SYSC_OMAP2_SOFTRESET |
2086 SYSC_OMAP2_AUTOIDLE,
2087 .regbits = &sysc_regbits_omap2,
2088};
2089
2090/* All omap2 and 3 timers, and timers 1, 2 & 10 on omap 4 and 5 */
2091static const struct sysc_capabilities sysc_omap2_timer = {
2092 .type = TI_SYSC_OMAP2_TIMER,
2093 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY | SYSC_OMAP2_EMUFREE |
2094 SYSC_OMAP2_ENAWAKEUP | SYSC_OMAP2_SOFTRESET |
2095 SYSC_OMAP2_AUTOIDLE,
2096 .regbits = &sysc_regbits_omap2,
2097 .mod_quirks = SYSC_QUIRK_USE_CLOCKACT,
2098};
2099
2100/*
2101 * SHAM2 (SHA1/MD5) sysc found on omap3, a variant of sysc_regbits_omap2
2102 * with different sidle position
2103 */
2104static const struct sysc_regbits sysc_regbits_omap3_sham = {
2105 .dmadisable_shift = -ENODEV,
2106 .midle_shift = -ENODEV,
2107 .sidle_shift = 4,
2108 .clkact_shift = -ENODEV,
2109 .enwkup_shift = -ENODEV,
2110 .srst_shift = 1,
2111 .autoidle_shift = 0,
2112 .emufree_shift = -ENODEV,
2113};
2114
2115static const struct sysc_capabilities sysc_omap3_sham = {
2116 .type = TI_SYSC_OMAP3_SHAM,
2117 .sysc_mask = SYSC_OMAP2_SOFTRESET | SYSC_OMAP2_AUTOIDLE,
2118 .regbits = &sysc_regbits_omap3_sham,
2119};
2120
2121/*
2122 * AES register bits found on omap3 and later, a variant of
2123 * sysc_regbits_omap2 with different sidle position
2124 */
2125static const struct sysc_regbits sysc_regbits_omap3_aes = {
2126 .dmadisable_shift = -ENODEV,
2127 .midle_shift = -ENODEV,
2128 .sidle_shift = 6,
2129 .clkact_shift = -ENODEV,
2130 .enwkup_shift = -ENODEV,
2131 .srst_shift = 1,
2132 .autoidle_shift = 0,
2133 .emufree_shift = -ENODEV,
2134};
2135
2136static const struct sysc_capabilities sysc_omap3_aes = {
2137 .type = TI_SYSC_OMAP3_AES,
2138 .sysc_mask = SYSC_OMAP2_SOFTRESET | SYSC_OMAP2_AUTOIDLE,
2139 .regbits = &sysc_regbits_omap3_aes,
2140};
2141
2142/*
2143 * Common sysc register bits found on omap4, also known as type2
2144 */
2145static const struct sysc_regbits sysc_regbits_omap4 = {
2146 .dmadisable_shift = 16,
2147 .midle_shift = 4,
2148 .sidle_shift = 2,
2149 .clkact_shift = -ENODEV,
2150 .enwkup_shift = -ENODEV,
2151 .emufree_shift = 1,
2152 .srst_shift = 0,
2153 .autoidle_shift = -ENODEV,
2154};
2155
2156static const struct sysc_capabilities sysc_omap4 = {
2157 .type = TI_SYSC_OMAP4,
2158 .sysc_mask = SYSC_OMAP4_DMADISABLE | SYSC_OMAP4_FREEEMU |
2159 SYSC_OMAP4_SOFTRESET,
2160 .regbits = &sysc_regbits_omap4,
2161};
2162
2163static const struct sysc_capabilities sysc_omap4_timer = {
2164 .type = TI_SYSC_OMAP4_TIMER,
2165 .sysc_mask = SYSC_OMAP4_DMADISABLE | SYSC_OMAP4_FREEEMU |
2166 SYSC_OMAP4_SOFTRESET,
2167 .regbits = &sysc_regbits_omap4,
2168};
2169
2170/*
2171 * Common sysc register bits found on omap4, also known as type3
2172 */
2173static const struct sysc_regbits sysc_regbits_omap4_simple = {
2174 .dmadisable_shift = -ENODEV,
2175 .midle_shift = 2,
2176 .sidle_shift = 0,
2177 .clkact_shift = -ENODEV,
2178 .enwkup_shift = -ENODEV,
2179 .srst_shift = -ENODEV,
2180 .emufree_shift = -ENODEV,
2181 .autoidle_shift = -ENODEV,
2182};
2183
2184static const struct sysc_capabilities sysc_omap4_simple = {
2185 .type = TI_SYSC_OMAP4_SIMPLE,
2186 .regbits = &sysc_regbits_omap4_simple,
2187};
2188
2189/*
2190 * SmartReflex sysc found on omap34xx
2191 */
2192static const struct sysc_regbits sysc_regbits_omap34xx_sr = {
2193 .dmadisable_shift = -ENODEV,
2194 .midle_shift = -ENODEV,
2195 .sidle_shift = -ENODEV,
2196 .clkact_shift = 20,
2197 .enwkup_shift = -ENODEV,
2198 .srst_shift = -ENODEV,
2199 .emufree_shift = -ENODEV,
2200 .autoidle_shift = -ENODEV,
2201};
2202
2203static const struct sysc_capabilities sysc_34xx_sr = {
2204 .type = TI_SYSC_OMAP34XX_SR,
2205 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY,
2206 .regbits = &sysc_regbits_omap34xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002207 .mod_quirks = SYSC_QUIRK_USE_CLOCKACT | SYSC_QUIRK_UNCACHED |
2208 SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08002209};
2210
2211/*
2212 * SmartReflex sysc found on omap36xx and later
2213 */
2214static const struct sysc_regbits sysc_regbits_omap36xx_sr = {
2215 .dmadisable_shift = -ENODEV,
2216 .midle_shift = -ENODEV,
2217 .sidle_shift = 24,
2218 .clkact_shift = -ENODEV,
2219 .enwkup_shift = 26,
2220 .srst_shift = -ENODEV,
2221 .emufree_shift = -ENODEV,
2222 .autoidle_shift = -ENODEV,
2223};
2224
2225static const struct sysc_capabilities sysc_36xx_sr = {
2226 .type = TI_SYSC_OMAP36XX_SR,
Tony Lindgren3267c082018-01-22 09:32:53 -08002227 .sysc_mask = SYSC_OMAP3_SR_ENAWAKEUP,
Tony Lindgren70a65242017-12-15 09:41:09 -08002228 .regbits = &sysc_regbits_omap36xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002229 .mod_quirks = SYSC_QUIRK_UNCACHED | SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08002230};
2231
2232static const struct sysc_capabilities sysc_omap4_sr = {
2233 .type = TI_SYSC_OMAP4_SR,
2234 .regbits = &sysc_regbits_omap36xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002235 .mod_quirks = SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08002236};
2237
2238/*
2239 * McASP register bits found on omap4 and later
2240 */
2241static const struct sysc_regbits sysc_regbits_omap4_mcasp = {
2242 .dmadisable_shift = -ENODEV,
2243 .midle_shift = -ENODEV,
2244 .sidle_shift = 0,
2245 .clkact_shift = -ENODEV,
2246 .enwkup_shift = -ENODEV,
2247 .srst_shift = -ENODEV,
2248 .emufree_shift = -ENODEV,
2249 .autoidle_shift = -ENODEV,
2250};
2251
2252static const struct sysc_capabilities sysc_omap4_mcasp = {
2253 .type = TI_SYSC_OMAP4_MCASP,
2254 .regbits = &sysc_regbits_omap4_mcasp,
Tony Lindgren2c63a832018-11-15 14:46:53 -08002255 .mod_quirks = SYSC_QUIRK_OPT_CLKS_NEEDED,
2256};
2257
2258/*
2259 * McASP found on dra7 and later
2260 */
2261static const struct sysc_capabilities sysc_dra7_mcasp = {
2262 .type = TI_SYSC_OMAP4_SIMPLE,
2263 .regbits = &sysc_regbits_omap4_simple,
2264 .mod_quirks = SYSC_QUIRK_OPT_CLKS_NEEDED,
Tony Lindgren70a65242017-12-15 09:41:09 -08002265};
2266
2267/*
2268 * FS USB host found on omap4 and later
2269 */
2270static const struct sysc_regbits sysc_regbits_omap4_usb_host_fs = {
2271 .dmadisable_shift = -ENODEV,
2272 .midle_shift = -ENODEV,
2273 .sidle_shift = 24,
2274 .clkact_shift = -ENODEV,
2275 .enwkup_shift = 26,
2276 .srst_shift = -ENODEV,
2277 .emufree_shift = -ENODEV,
2278 .autoidle_shift = -ENODEV,
2279};
2280
2281static const struct sysc_capabilities sysc_omap4_usb_host_fs = {
2282 .type = TI_SYSC_OMAP4_USB_HOST_FS,
2283 .sysc_mask = SYSC_OMAP2_ENAWAKEUP,
2284 .regbits = &sysc_regbits_omap4_usb_host_fs,
2285};
2286
Faiz Abbas7f35e632018-07-09 22:18:38 +05302287static const struct sysc_regbits sysc_regbits_dra7_mcan = {
2288 .dmadisable_shift = -ENODEV,
2289 .midle_shift = -ENODEV,
2290 .sidle_shift = -ENODEV,
2291 .clkact_shift = -ENODEV,
2292 .enwkup_shift = 4,
2293 .srst_shift = 0,
2294 .emufree_shift = -ENODEV,
2295 .autoidle_shift = -ENODEV,
2296};
2297
2298static const struct sysc_capabilities sysc_dra7_mcan = {
2299 .type = TI_SYSC_DRA7_MCAN,
2300 .sysc_mask = SYSC_DRA7_MCAN_ENAWAKEUP | SYSC_OMAP4_SOFTRESET,
2301 .regbits = &sysc_regbits_dra7_mcan,
Tony Lindgrene0db94f2019-05-27 04:51:53 -07002302 .mod_quirks = SYSS_QUIRK_RESETDONE_INVERTED,
Faiz Abbas7f35e632018-07-09 22:18:38 +05302303};
2304
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002305static int sysc_init_pdata(struct sysc *ddata)
2306{
2307 struct ti_sysc_platform_data *pdata = dev_get_platdata(ddata->dev);
Tony Lindgrena3e92e72019-03-21 11:00:21 -07002308 struct ti_sysc_module_data *mdata;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002309
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07002310 if (!pdata)
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002311 return 0;
2312
Tony Lindgrena3e92e72019-03-21 11:00:21 -07002313 mdata = devm_kzalloc(ddata->dev, sizeof(*mdata), GFP_KERNEL);
2314 if (!mdata)
2315 return -ENOMEM;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002316
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07002317 if (ddata->legacy_mode) {
2318 mdata->name = ddata->legacy_mode;
2319 mdata->module_pa = ddata->module_pa;
2320 mdata->module_size = ddata->module_size;
2321 mdata->offsets = ddata->offsets;
2322 mdata->nr_offsets = SYSC_MAX_REGS;
2323 mdata->cap = ddata->cap;
2324 mdata->cfg = &ddata->cfg;
2325 }
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002326
Tony Lindgrena3e92e72019-03-21 11:00:21 -07002327 ddata->mdata = mdata;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002328
Tony Lindgrena3e92e72019-03-21 11:00:21 -07002329 return 0;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002330}
2331
Tony Lindgren70a65242017-12-15 09:41:09 -08002332static int sysc_init_match(struct sysc *ddata)
2333{
2334 const struct sysc_capabilities *cap;
2335
2336 cap = of_device_get_match_data(ddata->dev);
2337 if (!cap)
2338 return -EINVAL;
2339
2340 ddata->cap = cap;
2341 if (ddata->cap)
2342 ddata->cfg.quirks |= ddata->cap->mod_quirks;
2343
2344 return 0;
2345}
2346
Tony Lindgren76f0f772018-02-23 08:28:45 -08002347static void ti_sysc_idle(struct work_struct *work)
2348{
2349 struct sysc *ddata;
2350
2351 ddata = container_of(work, struct sysc, idle_work.work);
2352
Tony Lindgrend0989132019-09-05 07:37:22 -07002353 /*
2354 * One time decrement of clock usage counts if left on from init.
2355 * Note that we disable opt clocks unconditionally in this case
2356 * as they are enabled unconditionally during init without
2357 * considering sysc_opt_clks_needed() at that point.
2358 */
2359 if (ddata->cfg.quirks & (SYSC_QUIRK_NO_IDLE |
2360 SYSC_QUIRK_NO_IDLE_ON_INIT)) {
Tony Lindgrend0989132019-09-05 07:37:22 -07002361 sysc_disable_main_clocks(ddata);
2362 sysc_disable_opt_clocks(ddata);
2363 sysc_clkdm_allow_idle(ddata);
2364 }
2365
2366 /* Keep permanent PM runtime usage count for SYSC_QUIRK_NO_IDLE */
2367 if (ddata->cfg.quirks & SYSC_QUIRK_NO_IDLE)
2368 return;
2369
2370 /*
2371 * Decrement PM runtime usage count for SYSC_QUIRK_NO_IDLE_ON_INIT
2372 * and SYSC_QUIRK_NO_RESET_ON_INIT
2373 */
Tony Lindgren76f0f772018-02-23 08:28:45 -08002374 if (pm_runtime_active(ddata->dev))
2375 pm_runtime_put_sync(ddata->dev);
2376}
2377
Tony Lindgrenc4bebea2018-04-16 10:24:54 -07002378static const struct of_device_id sysc_match_table[] = {
2379 { .compatible = "simple-bus", },
2380 { /* sentinel */ },
2381};
2382
Tony Lindgren0eecc632017-10-10 14:23:43 -07002383static int sysc_probe(struct platform_device *pdev)
2384{
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002385 struct ti_sysc_platform_data *pdata = dev_get_platdata(&pdev->dev);
Tony Lindgren0eecc632017-10-10 14:23:43 -07002386 struct sysc *ddata;
2387 int error;
2388
2389 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
2390 if (!ddata)
2391 return -ENOMEM;
2392
2393 ddata->dev = &pdev->dev;
Tony Lindgren566a9b052017-12-15 09:41:19 -08002394 platform_set_drvdata(pdev, ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -07002395
Tony Lindgren70a65242017-12-15 09:41:09 -08002396 error = sysc_init_match(ddata);
2397 if (error)
2398 return error;
2399
Tony Lindgren566a9b052017-12-15 09:41:19 -08002400 error = sysc_init_dts_quirks(ddata);
2401 if (error)
Suman Annaa304f482019-06-25 18:33:15 -05002402 return error;
Tony Lindgren566a9b052017-12-15 09:41:19 -08002403
Tony Lindgren0eecc632017-10-10 14:23:43 -07002404 error = sysc_map_and_check_registers(ddata);
2405 if (error)
Suman Annaa304f482019-06-25 18:33:15 -05002406 return error;
Tony Lindgren0eecc632017-10-10 14:23:43 -07002407
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08002408 error = sysc_init_sysc_mask(ddata);
2409 if (error)
Suman Annaa304f482019-06-25 18:33:15 -05002410 return error;
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08002411
2412 error = sysc_init_idlemodes(ddata);
2413 if (error)
Suman Annaa304f482019-06-25 18:33:15 -05002414 return error;
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08002415
2416 error = sysc_init_syss_mask(ddata);
2417 if (error)
Suman Annaa304f482019-06-25 18:33:15 -05002418 return error;
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08002419
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002420 error = sysc_init_pdata(ddata);
2421 if (error)
Suman Annaa304f482019-06-25 18:33:15 -05002422 return error;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002423
Tony Lindgren42b9c5c2019-03-21 11:00:21 -07002424 sysc_init_early_quirks(ddata);
2425
2426 error = sysc_get_clocks(ddata);
2427 if (error)
2428 return error;
2429
Tony Lindgren50622362018-04-16 10:20:27 -07002430 error = sysc_init_resets(ddata);
2431 if (error)
Suman Annaa304f482019-06-25 18:33:15 -05002432 goto unprepare;
Tony Lindgren566a9b052017-12-15 09:41:19 -08002433
2434 error = sysc_init_module(ddata);
2435 if (error)
2436 goto unprepare;
2437
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07002438 pm_runtime_enable(ddata->dev);
Tony Lindgren0eecc632017-10-10 14:23:43 -07002439 error = pm_runtime_get_sync(ddata->dev);
2440 if (error < 0) {
2441 pm_runtime_put_noidle(ddata->dev);
2442 pm_runtime_disable(ddata->dev);
2443 goto unprepare;
2444 }
2445
Tero Kristocdc56c12019-10-07 15:29:31 +03002446 /* Balance use counts as PM runtime should have enabled these all */
2447 if (!(ddata->cfg.quirks & SYSC_QUIRK_NO_RESET_ON_INIT))
Tero Kristobb88b862019-08-07 15:46:05 +03002448 reset_control_assert(ddata->rsts);
2449
Tero Kristocdc56c12019-10-07 15:29:31 +03002450 if (!(ddata->cfg.quirks &
2451 (SYSC_QUIRK_NO_IDLE | SYSC_QUIRK_NO_IDLE_ON_INIT))) {
2452 sysc_disable_main_clocks(ddata);
2453 sysc_disable_opt_clocks(ddata);
2454 sysc_clkdm_allow_idle(ddata);
2455 }
2456
Tony Lindgren0eecc632017-10-10 14:23:43 -07002457 sysc_show_registers(ddata);
2458
Tony Lindgren2c355ff2018-02-22 13:58:03 -08002459 ddata->dev->type = &sysc_device_type;
Tony Lindgrenc4bebea2018-04-16 10:24:54 -07002460 error = of_platform_populate(ddata->dev->of_node, sysc_match_table,
2461 pdata ? pdata->auxdata : NULL,
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002462 ddata->dev);
Tony Lindgren0eecc632017-10-10 14:23:43 -07002463 if (error)
2464 goto err;
2465
Tony Lindgren76f0f772018-02-23 08:28:45 -08002466 INIT_DELAYED_WORK(&ddata->idle_work, ti_sysc_idle);
2467
2468 /* At least earlycon won't survive without deferred idle */
Tony Lindgrend0989132019-09-05 07:37:22 -07002469 if (ddata->cfg.quirks & (SYSC_QUIRK_NO_IDLE |
2470 SYSC_QUIRK_NO_IDLE_ON_INIT |
Tony Lindgren76f0f772018-02-23 08:28:45 -08002471 SYSC_QUIRK_NO_RESET_ON_INIT)) {
2472 schedule_delayed_work(&ddata->idle_work, 3000);
2473 } else {
2474 pm_runtime_put(&pdev->dev);
2475 }
Tony Lindgren0eecc632017-10-10 14:23:43 -07002476
2477 return 0;
2478
2479err:
Tony Lindgren0eecc632017-10-10 14:23:43 -07002480 pm_runtime_put_sync(&pdev->dev);
2481 pm_runtime_disable(&pdev->dev);
2482unprepare:
2483 sysc_unprepare(ddata);
2484
2485 return error;
2486}
2487
Tony Lindgren684be5a2017-10-13 10:48:40 -07002488static int sysc_remove(struct platform_device *pdev)
2489{
2490 struct sysc *ddata = platform_get_drvdata(pdev);
2491 int error;
2492
Tony Lindgren76f0f772018-02-23 08:28:45 -08002493 cancel_delayed_work_sync(&ddata->idle_work);
2494
Tony Lindgren684be5a2017-10-13 10:48:40 -07002495 error = pm_runtime_get_sync(ddata->dev);
2496 if (error < 0) {
2497 pm_runtime_put_noidle(ddata->dev);
2498 pm_runtime_disable(ddata->dev);
2499 goto unprepare;
2500 }
2501
2502 of_platform_depopulate(&pdev->dev);
2503
Tony Lindgren684be5a2017-10-13 10:48:40 -07002504 pm_runtime_put_sync(&pdev->dev);
2505 pm_runtime_disable(&pdev->dev);
Tony Lindgren50622362018-04-16 10:20:27 -07002506 reset_control_assert(ddata->rsts);
Tony Lindgren684be5a2017-10-13 10:48:40 -07002507
2508unprepare:
2509 sysc_unprepare(ddata);
2510
2511 return 0;
2512}
2513
Tony Lindgren0eecc632017-10-10 14:23:43 -07002514static const struct of_device_id sysc_match[] = {
Tony Lindgren70a65242017-12-15 09:41:09 -08002515 { .compatible = "ti,sysc-omap2", .data = &sysc_omap2, },
2516 { .compatible = "ti,sysc-omap2-timer", .data = &sysc_omap2_timer, },
2517 { .compatible = "ti,sysc-omap4", .data = &sysc_omap4, },
2518 { .compatible = "ti,sysc-omap4-timer", .data = &sysc_omap4_timer, },
2519 { .compatible = "ti,sysc-omap4-simple", .data = &sysc_omap4_simple, },
2520 { .compatible = "ti,sysc-omap3430-sr", .data = &sysc_34xx_sr, },
2521 { .compatible = "ti,sysc-omap3630-sr", .data = &sysc_36xx_sr, },
2522 { .compatible = "ti,sysc-omap4-sr", .data = &sysc_omap4_sr, },
2523 { .compatible = "ti,sysc-omap3-sham", .data = &sysc_omap3_sham, },
2524 { .compatible = "ti,sysc-omap-aes", .data = &sysc_omap3_aes, },
2525 { .compatible = "ti,sysc-mcasp", .data = &sysc_omap4_mcasp, },
Tony Lindgren2c63a832018-11-15 14:46:53 -08002526 { .compatible = "ti,sysc-dra7-mcasp", .data = &sysc_dra7_mcasp, },
Tony Lindgren70a65242017-12-15 09:41:09 -08002527 { .compatible = "ti,sysc-usb-host-fs",
2528 .data = &sysc_omap4_usb_host_fs, },
Faiz Abbas7f35e632018-07-09 22:18:38 +05302529 { .compatible = "ti,sysc-dra7-mcan", .data = &sysc_dra7_mcan, },
Tony Lindgren0eecc632017-10-10 14:23:43 -07002530 { },
2531};
2532MODULE_DEVICE_TABLE(of, sysc_match);
2533
2534static struct platform_driver sysc_driver = {
2535 .probe = sysc_probe,
Tony Lindgren684be5a2017-10-13 10:48:40 -07002536 .remove = sysc_remove,
Tony Lindgren0eecc632017-10-10 14:23:43 -07002537 .driver = {
2538 .name = "ti-sysc",
2539 .of_match_table = sysc_match,
2540 .pm = &sysc_pm_ops,
2541 },
2542};
Tony Lindgren2c355ff2018-02-22 13:58:03 -08002543
2544static int __init sysc_init(void)
2545{
2546 bus_register_notifier(&platform_bus_type, &sysc_nb);
2547
2548 return platform_driver_register(&sysc_driver);
2549}
2550module_init(sysc_init);
2551
2552static void __exit sysc_exit(void)
2553{
2554 bus_unregister_notifier(&platform_bus_type, &sysc_nb);
2555 platform_driver_unregister(&sysc_driver);
2556}
2557module_exit(sysc_exit);
Tony Lindgren0eecc632017-10-10 14:23:43 -07002558
2559MODULE_DESCRIPTION("TI sysc interconnect target driver");
2560MODULE_LICENSE("GPL v2");