blob: a366ae548ec9da06a187faccc688256f663bbab6 [file] [log] [blame]
Tony Lindgren0eecc632017-10-10 14:23:43 -07001/*
2 * ti-sysc.c - Texas Instruments sysc interconnect target driver
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
9 * kind, whether express or implied; without even the implied warranty
10 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#include <linux/io.h>
15#include <linux/clk.h>
Tony Lindgren2c355ff2018-02-22 13:58:03 -080016#include <linux/clkdev.h>
Tony Lindgrena885f0f2018-02-22 14:03:48 -080017#include <linux/delay.h>
Tony Lindgren0eecc632017-10-10 14:23:43 -070018#include <linux/module.h>
19#include <linux/platform_device.h>
Tony Lindgrena885f0f2018-02-22 14:03:48 -080020#include <linux/pm_domain.h>
Tony Lindgren0eecc632017-10-10 14:23:43 -070021#include <linux/pm_runtime.h>
Tony Lindgren50622362018-04-16 10:20:27 -070022#include <linux/reset.h>
Tony Lindgren0eecc632017-10-10 14:23:43 -070023#include <linux/of_address.h>
24#include <linux/of_platform.h>
Tony Lindgren2c355ff2018-02-22 13:58:03 -080025#include <linux/slab.h>
Faiz Abbas596e7952018-07-09 22:18:39 +053026#include <linux/iopoll.h>
Tony Lindgren2c355ff2018-02-22 13:58:03 -080027
Tony Lindgren70a65242017-12-15 09:41:09 -080028#include <linux/platform_data/ti-sysc.h>
29
30#include <dt-bindings/bus/ti-sysc.h>
Tony Lindgren0eecc632017-10-10 14:23:43 -070031
Faiz Abbas596e7952018-07-09 22:18:39 +053032#define MAX_MODULE_SOFTRESET_WAIT 10000
33
Tony Lindgren0eecc632017-10-10 14:23:43 -070034static const char * const reg_names[] = { "rev", "sysc", "syss", };
35
36enum sysc_clocks {
37 SYSC_FCK,
38 SYSC_ICK,
Tony Lindgren09dfe582018-04-16 10:25:52 -070039 SYSC_OPTFCK0,
40 SYSC_OPTFCK1,
41 SYSC_OPTFCK2,
42 SYSC_OPTFCK3,
43 SYSC_OPTFCK4,
44 SYSC_OPTFCK5,
45 SYSC_OPTFCK6,
46 SYSC_OPTFCK7,
Tony Lindgren0eecc632017-10-10 14:23:43 -070047 SYSC_MAX_CLOCKS,
48};
49
Tony Lindgrena54275f2019-03-21 11:00:21 -070050static const char * const clock_names[SYSC_MAX_CLOCKS] = {
51 "fck", "ick", "opt0", "opt1", "opt2", "opt3", "opt4",
52 "opt5", "opt6", "opt7",
53};
Tony Lindgren0eecc632017-10-10 14:23:43 -070054
Tony Lindgrenc5a2de92017-12-15 09:41:23 -080055#define SYSC_IDLEMODE_MASK 3
56#define SYSC_CLOCKACTIVITY_MASK 3
57
Tony Lindgren0eecc632017-10-10 14:23:43 -070058/**
59 * struct sysc - TI sysc interconnect target module registers and capabilities
60 * @dev: struct device pointer
61 * @module_pa: physical address of the interconnect target module
62 * @module_size: size of the interconnect target module
63 * @module_va: virtual address of the interconnect target module
64 * @offsets: register offsets from module base
65 * @clocks: clocks used by the interconnect target module
Tony Lindgren09dfe582018-04-16 10:25:52 -070066 * @clock_roles: clock role names for the found clocks
67 * @nr_clocks: number of clocks used by the interconnect target module
Tony Lindgren0eecc632017-10-10 14:23:43 -070068 * @legacy_mode: configured for legacy mode if set
Tony Lindgren70a65242017-12-15 09:41:09 -080069 * @cap: interconnect target module capabilities
70 * @cfg: interconnect target module configuration
Tony Lindgren566a9b052017-12-15 09:41:19 -080071 * @name: name if available
72 * @revision: interconnect target module revision
Tony Lindgren62020f22018-02-22 13:59:44 -080073 * @needs_resume: runtime resume needed on resume from suspend
Tony Lindgren0eecc632017-10-10 14:23:43 -070074 */
75struct sysc {
76 struct device *dev;
77 u64 module_pa;
78 u32 module_size;
79 void __iomem *module_va;
80 int offsets[SYSC_MAX_REGS];
Tony Lindgrena3e92e72019-03-21 11:00:21 -070081 struct ti_sysc_module_data *mdata;
Tony Lindgren09dfe582018-04-16 10:25:52 -070082 struct clk **clocks;
83 const char **clock_roles;
84 int nr_clocks;
Tony Lindgren50622362018-04-16 10:20:27 -070085 struct reset_control *rsts;
Tony Lindgren0eecc632017-10-10 14:23:43 -070086 const char *legacy_mode;
Tony Lindgren70a65242017-12-15 09:41:09 -080087 const struct sysc_capabilities *cap;
88 struct sysc_config cfg;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -080089 struct ti_sysc_cookie cookie;
Tony Lindgren566a9b052017-12-15 09:41:19 -080090 const char *name;
91 u32 revision;
Tony Lindgren8383e252019-05-27 04:51:56 -070092 unsigned int enabled:1;
93 unsigned int needs_resume:1;
94 unsigned int child_needs_resume:1;
95 unsigned int disable_on_idle:1;
Tony Lindgren76f0f772018-02-23 08:28:45 -080096 struct delayed_work idle_work;
Tony Lindgren0eecc632017-10-10 14:23:43 -070097};
98
Tony Lindgren4014c082018-12-10 14:11:26 -080099static void sysc_parse_dts_quirks(struct sysc *ddata, struct device_node *np,
100 bool is_child);
101
Tony Lindgrenb7182b42019-03-21 11:00:21 -0700102static void sysc_write(struct sysc *ddata, int offset, u32 value)
Faiz Abbas596e7952018-07-09 22:18:39 +0530103{
Tony Lindgren5aa91292019-05-27 04:51:53 -0700104 if (ddata->cfg.quirks & SYSC_QUIRK_16BIT) {
105 writew_relaxed(value & 0xffff, ddata->module_va + offset);
106
107 /* Only i2c revision has LO and HI register with stride of 4 */
108 if (ddata->offsets[SYSC_REVISION] >= 0 &&
109 offset == ddata->offsets[SYSC_REVISION]) {
110 u16 hi = value >> 16;
111
112 writew_relaxed(hi, ddata->module_va + offset + 4);
113 }
114
115 return;
116 }
117
Faiz Abbas596e7952018-07-09 22:18:39 +0530118 writel_relaxed(value, ddata->module_va + offset);
119}
120
Tony Lindgren566a9b052017-12-15 09:41:19 -0800121static u32 sysc_read(struct sysc *ddata, int offset)
122{
123 if (ddata->cfg.quirks & SYSC_QUIRK_16BIT) {
124 u32 val;
125
126 val = readw_relaxed(ddata->module_va + offset);
Tony Lindgren5aa91292019-05-27 04:51:53 -0700127
128 /* Only i2c revision has LO and HI register with stride of 4 */
129 if (ddata->offsets[SYSC_REVISION] >= 0 &&
130 offset == ddata->offsets[SYSC_REVISION]) {
131 u16 tmp = readw_relaxed(ddata->module_va + offset + 4);
132
133 val |= tmp << 16;
134 }
Tony Lindgren566a9b052017-12-15 09:41:19 -0800135
136 return val;
137 }
138
139 return readl_relaxed(ddata->module_va + offset);
140}
141
Tony Lindgren09dfe582018-04-16 10:25:52 -0700142static bool sysc_opt_clks_needed(struct sysc *ddata)
143{
144 return !!(ddata->cfg.quirks & SYSC_QUIRK_OPT_CLKS_NEEDED);
145}
146
Tony Lindgren0eecc632017-10-10 14:23:43 -0700147static u32 sysc_read_revision(struct sysc *ddata)
148{
Tony Lindgren566a9b052017-12-15 09:41:19 -0800149 int offset = ddata->offsets[SYSC_REVISION];
150
151 if (offset < 0)
152 return 0;
153
154 return sysc_read(ddata, offset);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700155}
156
Tony Lindgrene0db94f2019-05-27 04:51:53 -0700157static u32 sysc_read_sysconfig(struct sysc *ddata)
158{
159 int offset = ddata->offsets[SYSC_SYSCONFIG];
160
161 if (offset < 0)
162 return 0;
163
164 return sysc_read(ddata, offset);
165}
166
167static u32 sysc_read_sysstatus(struct sysc *ddata)
168{
169 int offset = ddata->offsets[SYSC_SYSSTATUS];
170
171 if (offset < 0)
172 return 0;
173
174 return sysc_read(ddata, offset);
175}
176
Tony Lindgrena54275f2019-03-21 11:00:21 -0700177static int sysc_add_named_clock_from_child(struct sysc *ddata,
178 const char *name,
179 const char *optfck_name)
180{
181 struct device_node *np = ddata->dev->of_node;
182 struct device_node *child;
183 struct clk_lookup *cl;
184 struct clk *clock;
185 const char *n;
186
187 if (name)
188 n = name;
189 else
190 n = optfck_name;
191
192 /* Does the clock alias already exist? */
193 clock = of_clk_get_by_name(np, n);
194 if (!IS_ERR(clock)) {
195 clk_put(clock);
196
197 return 0;
198 }
199
200 child = of_get_next_available_child(np, NULL);
201 if (!child)
202 return -ENODEV;
203
204 clock = devm_get_clk_from_child(ddata->dev, child, name);
205 if (IS_ERR(clock))
206 return PTR_ERR(clock);
207
208 /*
209 * Use clkdev_add() instead of clkdev_alloc() to avoid the MAX_DEV_ID
210 * limit for clk_get(). If cl ever needs to be freed, it should be done
211 * with clkdev_drop().
212 */
213 cl = kcalloc(1, sizeof(*cl), GFP_KERNEL);
214 if (!cl)
215 return -ENOMEM;
216
217 cl->con_id = n;
218 cl->dev_id = dev_name(ddata->dev);
219 cl->clk = clock;
220 clkdev_add(cl);
221
222 clk_put(clock);
223
224 return 0;
225}
226
227static int sysc_init_ext_opt_clock(struct sysc *ddata, const char *name)
228{
229 const char *optfck_name;
230 int error, index;
231
232 if (ddata->nr_clocks < SYSC_OPTFCK0)
233 index = SYSC_OPTFCK0;
234 else
235 index = ddata->nr_clocks;
236
237 if (name)
238 optfck_name = name;
239 else
240 optfck_name = clock_names[index];
241
242 error = sysc_add_named_clock_from_child(ddata, name, optfck_name);
243 if (error)
244 return error;
245
246 ddata->clock_roles[index] = optfck_name;
247 ddata->nr_clocks++;
248
249 return 0;
250}
251
Tony Lindgren09dfe582018-04-16 10:25:52 -0700252static int sysc_get_one_clock(struct sysc *ddata, const char *name)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700253{
Tony Lindgren09dfe582018-04-16 10:25:52 -0700254 int error, i, index = -ENODEV;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700255
Tony Lindgren09dfe582018-04-16 10:25:52 -0700256 if (!strncmp(clock_names[SYSC_FCK], name, 3))
257 index = SYSC_FCK;
258 else if (!strncmp(clock_names[SYSC_ICK], name, 3))
259 index = SYSC_ICK;
260
261 if (index < 0) {
262 for (i = SYSC_OPTFCK0; i < SYSC_MAX_CLOCKS; i++) {
Tony Lindgrenc97c8622018-05-18 07:54:44 -0700263 if (!ddata->clocks[i]) {
Tony Lindgren09dfe582018-04-16 10:25:52 -0700264 index = i;
265 break;
266 }
267 }
Tony Lindgren0eecc632017-10-10 14:23:43 -0700268 }
Tony Lindgren09dfe582018-04-16 10:25:52 -0700269
270 if (index < 0) {
271 dev_err(ddata->dev, "clock %s not added\n", name);
272 return index;
273 }
Tony Lindgren0eecc632017-10-10 14:23:43 -0700274
275 ddata->clocks[index] = devm_clk_get(ddata->dev, name);
276 if (IS_ERR(ddata->clocks[index])) {
277 if (PTR_ERR(ddata->clocks[index]) == -ENOENT)
278 return 0;
279
280 dev_err(ddata->dev, "clock get error for %s: %li\n",
281 name, PTR_ERR(ddata->clocks[index]));
282
283 return PTR_ERR(ddata->clocks[index]);
284 }
285
286 error = clk_prepare(ddata->clocks[index]);
287 if (error) {
288 dev_err(ddata->dev, "clock prepare error for %s: %i\n",
289 name, error);
290
291 return error;
292 }
293
294 return 0;
295}
296
297static int sysc_get_clocks(struct sysc *ddata)
298{
Tony Lindgren09dfe582018-04-16 10:25:52 -0700299 struct device_node *np = ddata->dev->of_node;
300 struct property *prop;
301 const char *name;
302 int nr_fck = 0, nr_ick = 0, i, error = 0;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700303
Kees Cook20749052018-06-18 10:45:49 -0700304 ddata->clock_roles = devm_kcalloc(ddata->dev,
Tony Lindgren09dfe582018-04-16 10:25:52 -0700305 SYSC_MAX_CLOCKS,
Kees Cook20749052018-06-18 10:45:49 -0700306 sizeof(*ddata->clock_roles),
Tony Lindgren09dfe582018-04-16 10:25:52 -0700307 GFP_KERNEL);
308 if (!ddata->clock_roles)
309 return -ENOMEM;
310
311 of_property_for_each_string(np, "clock-names", prop, name) {
312 if (!strncmp(clock_names[SYSC_FCK], name, 3))
313 nr_fck++;
314 if (!strncmp(clock_names[SYSC_ICK], name, 3))
315 nr_ick++;
316 ddata->clock_roles[ddata->nr_clocks] = name;
317 ddata->nr_clocks++;
318 }
319
320 if (ddata->nr_clocks < 1)
321 return 0;
322
Tony Lindgrena54275f2019-03-21 11:00:21 -0700323 if ((ddata->cfg.quirks & SYSC_QUIRK_EXT_OPT_CLOCK)) {
324 error = sysc_init_ext_opt_clock(ddata, NULL);
325 if (error)
326 return error;
327 }
328
Tony Lindgren09dfe582018-04-16 10:25:52 -0700329 if (ddata->nr_clocks > SYSC_MAX_CLOCKS) {
330 dev_err(ddata->dev, "too many clocks for %pOF\n", np);
331
332 return -EINVAL;
333 }
334
335 if (nr_fck > 1 || nr_ick > 1) {
336 dev_err(ddata->dev, "max one fck and ick for %pOF\n", np);
337
338 return -EINVAL;
339 }
340
Kees Cook20749052018-06-18 10:45:49 -0700341 ddata->clocks = devm_kcalloc(ddata->dev,
342 ddata->nr_clocks, sizeof(*ddata->clocks),
Tony Lindgren09dfe582018-04-16 10:25:52 -0700343 GFP_KERNEL);
344 if (!ddata->clocks)
345 return -ENOMEM;
346
Tony Lindgren7b4f8ac2018-11-15 14:46:52 -0800347 for (i = 0; i < SYSC_MAX_CLOCKS; i++) {
348 const char *name = ddata->clock_roles[i];
349
350 if (!name)
351 continue;
352
353 error = sysc_get_one_clock(ddata, name);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700354 if (error && error != -ENOENT)
355 return error;
356 }
357
358 return 0;
359}
360
Tony Lindgrend8789702019-03-21 11:00:21 -0700361static int sysc_enable_main_clocks(struct sysc *ddata)
362{
363 struct clk *clock;
364 int i, error;
365
366 if (!ddata->clocks)
367 return 0;
368
369 for (i = 0; i < SYSC_OPTFCK0; i++) {
370 clock = ddata->clocks[i];
371
372 /* Main clocks may not have ick */
373 if (IS_ERR_OR_NULL(clock))
374 continue;
375
376 error = clk_enable(clock);
377 if (error)
378 goto err_disable;
379 }
380
381 return 0;
382
383err_disable:
384 for (i--; i >= 0; i--) {
385 clock = ddata->clocks[i];
386
387 /* Main clocks may not have ick */
388 if (IS_ERR_OR_NULL(clock))
389 continue;
390
391 clk_disable(clock);
392 }
393
394 return error;
395}
396
397static void sysc_disable_main_clocks(struct sysc *ddata)
398{
399 struct clk *clock;
400 int i;
401
402 if (!ddata->clocks)
403 return;
404
405 for (i = 0; i < SYSC_OPTFCK0; i++) {
406 clock = ddata->clocks[i];
407 if (IS_ERR_OR_NULL(clock))
408 continue;
409
410 clk_disable(clock);
411 }
412}
413
414static int sysc_enable_opt_clocks(struct sysc *ddata)
415{
416 struct clk *clock;
417 int i, error;
418
419 if (!ddata->clocks)
420 return 0;
421
422 for (i = SYSC_OPTFCK0; i < SYSC_MAX_CLOCKS; i++) {
423 clock = ddata->clocks[i];
424
425 /* Assume no holes for opt clocks */
426 if (IS_ERR_OR_NULL(clock))
427 return 0;
428
429 error = clk_enable(clock);
430 if (error)
431 goto err_disable;
432 }
433
434 return 0;
435
436err_disable:
437 for (i--; i >= 0; i--) {
438 clock = ddata->clocks[i];
439 if (IS_ERR_OR_NULL(clock))
440 continue;
441
442 clk_disable(clock);
443 }
444
445 return error;
446}
447
448static void sysc_disable_opt_clocks(struct sysc *ddata)
449{
450 struct clk *clock;
451 int i;
452
453 if (!ddata->clocks)
454 return;
455
456 for (i = SYSC_OPTFCK0; i < SYSC_MAX_CLOCKS; i++) {
457 clock = ddata->clocks[i];
458
459 /* Assume no holes for opt clocks */
460 if (IS_ERR_OR_NULL(clock))
461 return;
462
463 clk_disable(clock);
464 }
465}
466
Tony Lindgren2b2f7de2019-05-27 04:51:53 -0700467static void sysc_clkdm_deny_idle(struct sysc *ddata)
468{
469 struct ti_sysc_platform_data *pdata;
470
471 if (ddata->legacy_mode)
472 return;
473
474 pdata = dev_get_platdata(ddata->dev);
475 if (pdata && pdata->clkdm_deny_idle)
476 pdata->clkdm_deny_idle(ddata->dev, &ddata->cookie);
477}
478
479static void sysc_clkdm_allow_idle(struct sysc *ddata)
480{
481 struct ti_sysc_platform_data *pdata;
482
483 if (ddata->legacy_mode)
484 return;
485
486 pdata = dev_get_platdata(ddata->dev);
487 if (pdata && pdata->clkdm_allow_idle)
488 pdata->clkdm_allow_idle(ddata->dev, &ddata->cookie);
489}
490
Tony Lindgren0eecc632017-10-10 14:23:43 -0700491/**
Tony Lindgrenb11c1ea2019-03-21 11:00:21 -0700492 * sysc_init_resets - init rstctrl reset line if configured
Tony Lindgren50622362018-04-16 10:20:27 -0700493 * @ddata: device driver data
494 *
Tony Lindgrenb11c1ea2019-03-21 11:00:21 -0700495 * See sysc_rstctrl_reset_deassert().
Tony Lindgren50622362018-04-16 10:20:27 -0700496 */
497static int sysc_init_resets(struct sysc *ddata)
498{
Tony Lindgren50622362018-04-16 10:20:27 -0700499 ddata->rsts =
Tony Lindgrenea5a2e42019-05-27 04:51:56 -0700500 devm_reset_control_get_optional(ddata->dev, "rstctrl");
Tony Lindgren50622362018-04-16 10:20:27 -0700501 if (IS_ERR(ddata->rsts))
502 return PTR_ERR(ddata->rsts);
503
Tony Lindgren50622362018-04-16 10:20:27 -0700504 return 0;
505}
506
507/**
Tony Lindgren0eecc632017-10-10 14:23:43 -0700508 * sysc_parse_and_check_child_range - parses module IO region from ranges
509 * @ddata: device driver data
510 *
511 * In general we only need rev, syss, and sysc registers and not the whole
512 * module range. But we do want the offsets for these registers from the
513 * module base. This allows us to check them against the legacy hwmod
514 * platform data. Let's also check the ranges are configured properly.
515 */
516static int sysc_parse_and_check_child_range(struct sysc *ddata)
517{
518 struct device_node *np = ddata->dev->of_node;
519 const __be32 *ranges;
520 u32 nr_addr, nr_size;
521 int len, error;
522
523 ranges = of_get_property(np, "ranges", &len);
524 if (!ranges) {
525 dev_err(ddata->dev, "missing ranges for %pOF\n", np);
526
527 return -ENOENT;
528 }
529
530 len /= sizeof(*ranges);
531
532 if (len < 3) {
533 dev_err(ddata->dev, "incomplete ranges for %pOF\n", np);
534
535 return -EINVAL;
536 }
537
538 error = of_property_read_u32(np, "#address-cells", &nr_addr);
539 if (error)
540 return -ENOENT;
541
542 error = of_property_read_u32(np, "#size-cells", &nr_size);
543 if (error)
544 return -ENOENT;
545
546 if (nr_addr != 1 || nr_size != 1) {
547 dev_err(ddata->dev, "invalid ranges for %pOF\n", np);
548
549 return -EINVAL;
550 }
551
552 ranges++;
553 ddata->module_pa = of_translate_address(np, ranges++);
554 ddata->module_size = be32_to_cpup(ranges);
555
Tony Lindgren0eecc632017-10-10 14:23:43 -0700556 return 0;
557}
558
Tony Lindgren3bb37c82018-02-22 14:05:14 -0800559static struct device_node *stdout_path;
560
561static void sysc_init_stdout_path(struct sysc *ddata)
562{
563 struct device_node *np = NULL;
564 const char *uart;
565
566 if (IS_ERR(stdout_path))
567 return;
568
569 if (stdout_path)
570 return;
571
572 np = of_find_node_by_path("/chosen");
573 if (!np)
574 goto err;
575
576 uart = of_get_property(np, "stdout-path", NULL);
577 if (!uart)
578 goto err;
579
580 np = of_find_node_by_path(uart);
581 if (!np)
582 goto err;
583
584 stdout_path = np;
585
586 return;
587
588err:
589 stdout_path = ERR_PTR(-ENODEV);
590}
591
592static void sysc_check_quirk_stdout(struct sysc *ddata,
593 struct device_node *np)
594{
595 sysc_init_stdout_path(ddata);
596 if (np != stdout_path)
597 return;
598
599 ddata->cfg.quirks |= SYSC_QUIRK_NO_IDLE_ON_INIT |
600 SYSC_QUIRK_NO_RESET_ON_INIT;
601}
602
Tony Lindgren0eecc632017-10-10 14:23:43 -0700603/**
604 * sysc_check_one_child - check child configuration
605 * @ddata: device driver data
606 * @np: child device node
607 *
608 * Let's avoid messy situations where we have new interconnect target
609 * node but children have "ti,hwmods". These belong to the interconnect
610 * target node and are managed by this driver.
611 */
612static int sysc_check_one_child(struct sysc *ddata,
613 struct device_node *np)
614{
615 const char *name;
616
617 name = of_get_property(np, "ti,hwmods", NULL);
618 if (name)
619 dev_warn(ddata->dev, "really a child ti,hwmods property?");
620
Tony Lindgren3bb37c82018-02-22 14:05:14 -0800621 sysc_check_quirk_stdout(ddata, np);
Tony Lindgren4014c082018-12-10 14:11:26 -0800622 sysc_parse_dts_quirks(ddata, np, true);
Tony Lindgren3bb37c82018-02-22 14:05:14 -0800623
Tony Lindgren0eecc632017-10-10 14:23:43 -0700624 return 0;
625}
626
627static int sysc_check_children(struct sysc *ddata)
628{
629 struct device_node *child;
630 int error;
631
632 for_each_child_of_node(ddata->dev->of_node, child) {
633 error = sysc_check_one_child(ddata, child);
634 if (error)
635 return error;
636 }
637
638 return 0;
639}
640
Tony Lindgrena7199e22017-12-15 09:41:14 -0800641/*
642 * So far only I2C uses 16-bit read access with clockactivity with revision
643 * in two registers with stride of 4. We can detect this based on the rev
644 * register size to configure things far enough to be able to properly read
645 * the revision register.
646 */
647static void sysc_check_quirk_16bit(struct sysc *ddata, struct resource *res)
648{
Tony Lindgrendd57ac12018-02-22 14:09:57 -0800649 if (resource_size(res) == 8)
Tony Lindgrena7199e22017-12-15 09:41:14 -0800650 ddata->cfg.quirks |= SYSC_QUIRK_16BIT | SYSC_QUIRK_USE_CLOCKACT;
Tony Lindgrena7199e22017-12-15 09:41:14 -0800651}
652
Tony Lindgren0eecc632017-10-10 14:23:43 -0700653/**
654 * sysc_parse_one - parses the interconnect target module registers
655 * @ddata: device driver data
656 * @reg: register to parse
657 */
658static int sysc_parse_one(struct sysc *ddata, enum sysc_registers reg)
659{
660 struct resource *res;
661 const char *name;
662
663 switch (reg) {
664 case SYSC_REVISION:
665 case SYSC_SYSCONFIG:
666 case SYSC_SYSSTATUS:
667 name = reg_names[reg];
668 break;
669 default:
670 return -EINVAL;
671 }
672
673 res = platform_get_resource_byname(to_platform_device(ddata->dev),
674 IORESOURCE_MEM, name);
675 if (!res) {
Tony Lindgren0eecc632017-10-10 14:23:43 -0700676 ddata->offsets[reg] = -ENODEV;
677
678 return 0;
679 }
680
681 ddata->offsets[reg] = res->start - ddata->module_pa;
Tony Lindgrena7199e22017-12-15 09:41:14 -0800682 if (reg == SYSC_REVISION)
683 sysc_check_quirk_16bit(ddata, res);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700684
685 return 0;
686}
687
688static int sysc_parse_registers(struct sysc *ddata)
689{
690 int i, error;
691
692 for (i = 0; i < SYSC_MAX_REGS; i++) {
693 error = sysc_parse_one(ddata, i);
694 if (error)
695 return error;
696 }
697
698 return 0;
699}
700
701/**
702 * sysc_check_registers - check for misconfigured register overlaps
703 * @ddata: device driver data
704 */
705static int sysc_check_registers(struct sysc *ddata)
706{
707 int i, j, nr_regs = 0, nr_matches = 0;
708
709 for (i = 0; i < SYSC_MAX_REGS; i++) {
710 if (ddata->offsets[i] < 0)
711 continue;
712
713 if (ddata->offsets[i] > (ddata->module_size - 4)) {
714 dev_err(ddata->dev, "register outside module range");
715
716 return -EINVAL;
717 }
718
719 for (j = 0; j < SYSC_MAX_REGS; j++) {
720 if (ddata->offsets[j] < 0)
721 continue;
722
723 if (ddata->offsets[i] == ddata->offsets[j])
724 nr_matches++;
725 }
726 nr_regs++;
727 }
728
Tony Lindgren0eecc632017-10-10 14:23:43 -0700729 if (nr_matches > nr_regs) {
730 dev_err(ddata->dev, "overlapping registers: (%i/%i)",
731 nr_regs, nr_matches);
732
733 return -EINVAL;
734 }
735
736 return 0;
737}
738
739/**
740 * syc_ioremap - ioremap register space for the interconnect target module
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700741 * @ddata: device driver data
Tony Lindgren0eecc632017-10-10 14:23:43 -0700742 *
743 * Note that the interconnect target module registers can be anywhere
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700744 * within the interconnect target module range. For example, SGX has
745 * them at offset 0x1fc00 in the 32MB module address space. And cpsw
746 * has them at offset 0x1200 in the CPSW_WR child. Usually the
747 * the interconnect target module registers are at the beginning of
748 * the module range though.
Tony Lindgren0eecc632017-10-10 14:23:43 -0700749 */
750static int sysc_ioremap(struct sysc *ddata)
751{
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700752 int size;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700753
Tony Lindgrene4f50c82019-05-01 14:24:57 -0700754 if (ddata->offsets[SYSC_REVISION] < 0 &&
755 ddata->offsets[SYSC_SYSCONFIG] < 0 &&
756 ddata->offsets[SYSC_SYSSTATUS] < 0) {
757 size = ddata->module_size;
758 } else {
759 size = max3(ddata->offsets[SYSC_REVISION],
760 ddata->offsets[SYSC_SYSCONFIG],
761 ddata->offsets[SYSC_SYSSTATUS]);
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700762
Tony Lindgrene4f50c82019-05-01 14:24:57 -0700763 if ((size + sizeof(u32)) > ddata->module_size)
764 return -EINVAL;
765 }
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
788 error = sysc_check_children(ddata);
789 if (error)
790 return error;
791
792 error = sysc_parse_registers(ddata);
793 if (error)
794 return error;
795
796 error = sysc_ioremap(ddata);
797 if (error)
798 return error;
799
800 error = sysc_check_registers(ddata);
801 if (error)
802 return error;
803
804 return 0;
805}
806
807/**
808 * sysc_show_rev - read and show interconnect target module revision
809 * @bufp: buffer to print the information to
810 * @ddata: device driver data
811 */
812static int sysc_show_rev(char *bufp, struct sysc *ddata)
813{
Tony Lindgren566a9b052017-12-15 09:41:19 -0800814 int len;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700815
816 if (ddata->offsets[SYSC_REVISION] < 0)
817 return sprintf(bufp, ":NA");
818
Tony Lindgren566a9b052017-12-15 09:41:19 -0800819 len = sprintf(bufp, ":%08x", ddata->revision);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700820
821 return len;
822}
823
824static int sysc_show_reg(struct sysc *ddata,
825 char *bufp, enum sysc_registers reg)
826{
827 if (ddata->offsets[reg] < 0)
828 return sprintf(bufp, ":NA");
829
830 return sprintf(bufp, ":%x", ddata->offsets[reg]);
831}
832
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800833static int sysc_show_name(char *bufp, struct sysc *ddata)
834{
835 if (!ddata->name)
836 return 0;
837
838 return sprintf(bufp, ":%s", ddata->name);
839}
840
Tony Lindgren0eecc632017-10-10 14:23:43 -0700841/**
842 * sysc_show_registers - show information about interconnect target module
843 * @ddata: device driver data
844 */
845static void sysc_show_registers(struct sysc *ddata)
846{
847 char buf[128];
848 char *bufp = buf;
849 int i;
850
851 for (i = 0; i < SYSC_MAX_REGS; i++)
852 bufp += sysc_show_reg(ddata, bufp, i);
853
854 bufp += sysc_show_rev(bufp, ddata);
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800855 bufp += sysc_show_name(bufp, ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700856
857 dev_dbg(ddata->dev, "%llx:%x%s\n",
858 ddata->module_pa, ddata->module_size,
859 buf);
860}
861
Roger Quadrosd59b6052019-04-08 12:52:39 +0300862#define SYSC_IDLE_MASK (SYSC_NR_IDLEMODES - 1)
Tony Lindgrenae9ae122019-05-27 04:51:54 -0700863#define SYSC_CLOCACT_ICK 2
Roger Quadrosd59b6052019-04-08 12:52:39 +0300864
Tony Lindgren2b2f7de2019-05-27 04:51:53 -0700865/* Caller needs to manage sysc_clkdm_deny_idle() and sysc_clkdm_allow_idle() */
Roger Quadrosd59b6052019-04-08 12:52:39 +0300866static int sysc_enable_module(struct device *dev)
867{
868 struct sysc *ddata;
869 const struct sysc_regbits *regbits;
870 u32 reg, idlemodes, best_mode;
871
872 ddata = dev_get_drvdata(dev);
873 if (ddata->offsets[SYSC_SYSCONFIG] == -ENODEV)
874 return 0;
875
Roger Quadrosd59b6052019-04-08 12:52:39 +0300876 regbits = ddata->cap->regbits;
877 reg = sysc_read(ddata, ddata->offsets[SYSC_SYSCONFIG]);
878
Tony Lindgrenae9ae122019-05-27 04:51:54 -0700879 /* Set CLOCKACTIVITY, we only use it for ick */
880 if (regbits->clkact_shift >= 0 &&
881 (ddata->cfg.quirks & SYSC_QUIRK_USE_CLOCKACT ||
882 ddata->cfg.sysc_val & BIT(regbits->clkact_shift)))
883 reg |= SYSC_CLOCACT_ICK << regbits->clkact_shift;
884
Roger Quadrosd59b6052019-04-08 12:52:39 +0300885 /* Set SIDLE mode */
886 idlemodes = ddata->cfg.sidlemodes;
887 if (!idlemodes || regbits->sidle_shift < 0)
888 goto set_midle;
889
Tony Lindgrenfb685f12019-05-27 04:51:55 -0700890 if (ddata->cfg.quirks & (SYSC_QUIRK_SWSUP_SIDLE |
891 SYSC_QUIRK_SWSUP_SIDLE_ACT)) {
892 best_mode = SYSC_IDLE_NO;
893 } else {
894 best_mode = fls(ddata->cfg.sidlemodes) - 1;
895 if (best_mode > SYSC_IDLE_MASK) {
896 dev_err(dev, "%s: invalid sidlemode\n", __func__);
897 return -EINVAL;
898 }
Tony Lindgren6e09f492019-05-27 04:51:55 -0700899
900 /* Set WAKEUP */
901 if (regbits->enwkup_shift >= 0 &&
902 ddata->cfg.sysc_val & BIT(regbits->enwkup_shift))
903 reg |= BIT(regbits->enwkup_shift);
Roger Quadrosd59b6052019-04-08 12:52:39 +0300904 }
905
906 reg &= ~(SYSC_IDLE_MASK << regbits->sidle_shift);
907 reg |= best_mode << regbits->sidle_shift;
908 sysc_write(ddata, ddata->offsets[SYSC_SYSCONFIG], reg);
909
910set_midle:
911 /* Set MIDLE mode */
912 idlemodes = ddata->cfg.midlemodes;
913 if (!idlemodes || regbits->midle_shift < 0)
Tony Lindgreneec26552019-05-27 04:51:54 -0700914 goto set_autoidle;
Roger Quadrosd59b6052019-04-08 12:52:39 +0300915
916 best_mode = fls(ddata->cfg.midlemodes) - 1;
917 if (best_mode > SYSC_IDLE_MASK) {
918 dev_err(dev, "%s: invalid midlemode\n", __func__);
919 return -EINVAL;
920 }
921
922 reg &= ~(SYSC_IDLE_MASK << regbits->midle_shift);
923 reg |= best_mode << regbits->midle_shift;
924 sysc_write(ddata, ddata->offsets[SYSC_SYSCONFIG], reg);
925
Tony Lindgreneec26552019-05-27 04:51:54 -0700926set_autoidle:
927 /* Autoidle bit must enabled separately if available */
928 if (regbits->autoidle_shift >= 0 &&
929 ddata->cfg.sysc_val & BIT(regbits->autoidle_shift)) {
930 reg |= 1 << regbits->autoidle_shift;
931 sysc_write(ddata, ddata->offsets[SYSC_SYSCONFIG], reg);
932 }
933
Roger Quadrosd59b6052019-04-08 12:52:39 +0300934 return 0;
935}
936
937static int sysc_best_idle_mode(u32 idlemodes, u32 *best_mode)
938{
939 if (idlemodes & BIT(SYSC_IDLE_SMART_WKUP))
940 *best_mode = SYSC_IDLE_SMART_WKUP;
941 else if (idlemodes & BIT(SYSC_IDLE_SMART))
942 *best_mode = SYSC_IDLE_SMART;
943 else if (idlemodes & SYSC_IDLE_FORCE)
944 *best_mode = SYSC_IDLE_FORCE;
945 else
946 return -EINVAL;
947
948 return 0;
949}
950
Tony Lindgren2b2f7de2019-05-27 04:51:53 -0700951/* Caller needs to manage sysc_clkdm_deny_idle() and sysc_clkdm_allow_idle() */
Roger Quadrosd59b6052019-04-08 12:52:39 +0300952static int sysc_disable_module(struct device *dev)
953{
954 struct sysc *ddata;
955 const struct sysc_regbits *regbits;
956 u32 reg, idlemodes, best_mode;
957 int ret;
958
959 ddata = dev_get_drvdata(dev);
960 if (ddata->offsets[SYSC_SYSCONFIG] == -ENODEV)
961 return 0;
962
Roger Quadrosd59b6052019-04-08 12:52:39 +0300963 regbits = ddata->cap->regbits;
964 reg = sysc_read(ddata, ddata->offsets[SYSC_SYSCONFIG]);
965
966 /* Set MIDLE mode */
967 idlemodes = ddata->cfg.midlemodes;
968 if (!idlemodes || regbits->midle_shift < 0)
969 goto set_sidle;
970
971 ret = sysc_best_idle_mode(idlemodes, &best_mode);
972 if (ret) {
973 dev_err(dev, "%s: invalid midlemode\n", __func__);
974 return ret;
975 }
976
977 reg &= ~(SYSC_IDLE_MASK << regbits->midle_shift);
978 reg |= best_mode << regbits->midle_shift;
979 sysc_write(ddata, ddata->offsets[SYSC_SYSCONFIG], reg);
980
981set_sidle:
982 /* Set SIDLE mode */
983 idlemodes = ddata->cfg.sidlemodes;
984 if (!idlemodes || regbits->sidle_shift < 0)
985 return 0;
986
Tony Lindgrenfb685f12019-05-27 04:51:55 -0700987 if (ddata->cfg.quirks & SYSC_QUIRK_SWSUP_SIDLE) {
988 best_mode = SYSC_IDLE_FORCE;
989 } else {
990 ret = sysc_best_idle_mode(idlemodes, &best_mode);
991 if (ret) {
992 dev_err(dev, "%s: invalid sidlemode\n", __func__);
993 return ret;
994 }
Roger Quadrosd59b6052019-04-08 12:52:39 +0300995 }
996
997 reg &= ~(SYSC_IDLE_MASK << regbits->sidle_shift);
998 reg |= best_mode << regbits->sidle_shift;
Tony Lindgreneec26552019-05-27 04:51:54 -0700999 if (regbits->autoidle_shift >= 0 &&
1000 ddata->cfg.sysc_val & BIT(regbits->autoidle_shift))
1001 reg |= 1 << regbits->autoidle_shift;
Roger Quadrosd59b6052019-04-08 12:52:39 +03001002 sysc_write(ddata, ddata->offsets[SYSC_SYSCONFIG], reg);
1003
1004 return 0;
1005}
1006
Tony Lindgrenff437282019-03-21 11:00:21 -07001007static int __maybe_unused sysc_runtime_suspend_legacy(struct device *dev,
1008 struct sysc *ddata)
Tony Lindgren0eecc632017-10-10 14:23:43 -07001009{
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001010 struct ti_sysc_platform_data *pdata;
Tony Lindgrenff437282019-03-21 11:00:21 -07001011 int error;
1012
1013 pdata = dev_get_platdata(ddata->dev);
1014 if (!pdata)
1015 return 0;
1016
1017 if (!pdata->idle_module)
1018 return -ENODEV;
1019
1020 error = pdata->idle_module(dev, &ddata->cookie);
1021 if (error)
1022 dev_err(dev, "%s: could not idle: %i\n",
1023 __func__, error);
1024
Tony Lindgren8383e252019-05-27 04:51:56 -07001025 if (ddata->disable_on_idle)
1026 reset_control_assert(ddata->rsts);
1027
Tony Lindgrenff437282019-03-21 11:00:21 -07001028 return 0;
1029}
1030
1031static int __maybe_unused sysc_runtime_resume_legacy(struct device *dev,
1032 struct sysc *ddata)
1033{
1034 struct ti_sysc_platform_data *pdata;
1035 int error;
1036
Tony Lindgren8383e252019-05-27 04:51:56 -07001037 if (ddata->disable_on_idle)
1038 reset_control_deassert(ddata->rsts);
1039
Tony Lindgrenff437282019-03-21 11:00:21 -07001040 pdata = dev_get_platdata(ddata->dev);
1041 if (!pdata)
1042 return 0;
1043
1044 if (!pdata->enable_module)
1045 return -ENODEV;
1046
1047 error = pdata->enable_module(dev, &ddata->cookie);
1048 if (error)
1049 dev_err(dev, "%s: could not enable: %i\n",
1050 __func__, error);
1051
1052 return 0;
1053}
1054
1055static int __maybe_unused sysc_runtime_suspend(struct device *dev)
1056{
Tony Lindgren0eecc632017-10-10 14:23:43 -07001057 struct sysc *ddata;
Tony Lindgrend8789702019-03-21 11:00:21 -07001058 int error = 0;
Tony Lindgren0eecc632017-10-10 14:23:43 -07001059
1060 ddata = dev_get_drvdata(dev);
1061
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001062 if (!ddata->enabled)
Tony Lindgren0eecc632017-10-10 14:23:43 -07001063 return 0;
1064
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001065 sysc_clkdm_deny_idle(ddata);
1066
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001067 if (ddata->legacy_mode) {
Tony Lindgrenff437282019-03-21 11:00:21 -07001068 error = sysc_runtime_suspend_legacy(dev, ddata);
Tony Lindgren93de83a2019-03-21 11:00:21 -07001069 if (error)
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001070 goto err_allow_idle;
Roger Quadrosd59b6052019-04-08 12:52:39 +03001071 } else {
1072 error = sysc_disable_module(dev);
1073 if (error)
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001074 goto err_allow_idle;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001075 }
1076
Tony Lindgrend8789702019-03-21 11:00:21 -07001077 sysc_disable_main_clocks(ddata);
Tony Lindgren09dfe582018-04-16 10:25:52 -07001078
Tony Lindgrend8789702019-03-21 11:00:21 -07001079 if (sysc_opt_clks_needed(ddata))
1080 sysc_disable_opt_clocks(ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001081
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001082 ddata->enabled = false;
1083
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001084err_allow_idle:
1085 sysc_clkdm_allow_idle(ddata);
1086
Tony Lindgren8383e252019-05-27 04:51:56 -07001087 if (ddata->disable_on_idle)
1088 reset_control_assert(ddata->rsts);
1089
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001090 return error;
Tony Lindgren0eecc632017-10-10 14:23:43 -07001091}
1092
Arnd Bergmanna4a5d492017-10-13 11:25:32 +02001093static int __maybe_unused sysc_runtime_resume(struct device *dev)
Tony Lindgren0eecc632017-10-10 14:23:43 -07001094{
1095 struct sysc *ddata;
Tony Lindgrend8789702019-03-21 11:00:21 -07001096 int error = 0;
Tony Lindgren0eecc632017-10-10 14:23:43 -07001097
1098 ddata = dev_get_drvdata(dev);
1099
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001100 if (ddata->enabled)
Tony Lindgren0eecc632017-10-10 14:23:43 -07001101 return 0;
1102
Tony Lindgren8383e252019-05-27 04:51:56 -07001103 if (ddata->disable_on_idle)
1104 reset_control_deassert(ddata->rsts);
1105
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001106 sysc_clkdm_deny_idle(ddata);
1107
Tony Lindgrend8789702019-03-21 11:00:21 -07001108 if (sysc_opt_clks_needed(ddata)) {
1109 error = sysc_enable_opt_clocks(ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001110 if (error)
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001111 goto err_allow_idle;
Tony Lindgren0eecc632017-10-10 14:23:43 -07001112 }
1113
Tony Lindgrend8789702019-03-21 11:00:21 -07001114 error = sysc_enable_main_clocks(ddata);
1115 if (error)
Tony Lindgren93de83a2019-03-21 11:00:21 -07001116 goto err_opt_clocks;
1117
1118 if (ddata->legacy_mode) {
1119 error = sysc_runtime_resume_legacy(dev, ddata);
1120 if (error)
1121 goto err_main_clocks;
Roger Quadrosd59b6052019-04-08 12:52:39 +03001122 } else {
1123 error = sysc_enable_module(dev);
1124 if (error)
1125 goto err_main_clocks;
Tony Lindgren93de83a2019-03-21 11:00:21 -07001126 }
Tony Lindgrend8789702019-03-21 11:00:21 -07001127
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001128 ddata->enabled = true;
1129
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001130 sysc_clkdm_allow_idle(ddata);
1131
Tony Lindgrend8789702019-03-21 11:00:21 -07001132 return 0;
1133
1134err_main_clocks:
Tony Lindgren93de83a2019-03-21 11:00:21 -07001135 sysc_disable_main_clocks(ddata);
1136err_opt_clocks:
Tony Lindgrend8789702019-03-21 11:00:21 -07001137 if (sysc_opt_clks_needed(ddata))
1138 sysc_disable_opt_clocks(ddata);
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001139err_allow_idle:
1140 sysc_clkdm_allow_idle(ddata);
Tony Lindgrend8789702019-03-21 11:00:21 -07001141
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001142 return error;
Tony Lindgren0eecc632017-10-10 14:23:43 -07001143}
1144
Tony Lindgrenf5e802032018-10-01 09:33:28 -07001145static int __maybe_unused sysc_noirq_suspend(struct device *dev)
Tony Lindgren62020f22018-02-22 13:59:44 -08001146{
1147 struct sysc *ddata;
1148
1149 ddata = dev_get_drvdata(dev);
1150
Tony Lindgren40d9f912018-09-24 12:16:54 -07001151 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
Tony Lindgrene7420c22018-04-16 10:26:46 -07001152 return 0;
1153
Tony Lindgrenf5e802032018-10-01 09:33:28 -07001154 return pm_runtime_force_suspend(dev);
Tony Lindgren62020f22018-02-22 13:59:44 -08001155}
1156
Tony Lindgrenf5e802032018-10-01 09:33:28 -07001157static int __maybe_unused sysc_noirq_resume(struct device *dev)
Tony Lindgren62020f22018-02-22 13:59:44 -08001158{
1159 struct sysc *ddata;
1160
1161 ddata = dev_get_drvdata(dev);
Tony Lindgrene7420c22018-04-16 10:26:46 -07001162
Tony Lindgren40d9f912018-09-24 12:16:54 -07001163 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
Tony Lindgrene7420c22018-04-16 10:26:46 -07001164 return 0;
1165
Tony Lindgrenf5e802032018-10-01 09:33:28 -07001166 return pm_runtime_force_resume(dev);
Tony Lindgrene7420c22018-04-16 10:26:46 -07001167}
1168
Tony Lindgren0eecc632017-10-10 14:23:43 -07001169static const struct dev_pm_ops sysc_pm_ops = {
Tony Lindgrene7420c22018-04-16 10:26:46 -07001170 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(sysc_noirq_suspend, sysc_noirq_resume)
Tony Lindgren0eecc632017-10-10 14:23:43 -07001171 SET_RUNTIME_PM_OPS(sysc_runtime_suspend,
1172 sysc_runtime_resume,
1173 NULL)
1174};
1175
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001176/* Module revision register based quirks */
1177struct sysc_revision_quirk {
1178 const char *name;
1179 u32 base;
1180 int rev_offset;
1181 int sysc_offset;
1182 int syss_offset;
1183 u32 revision;
1184 u32 revision_mask;
1185 u32 quirks;
1186};
1187
1188#define SYSC_QUIRK(optname, optbase, optrev, optsysc, optsyss, \
1189 optrev_val, optrevmask, optquirkmask) \
1190 { \
1191 .name = (optname), \
1192 .base = (optbase), \
1193 .rev_offset = (optrev), \
1194 .sysc_offset = (optsysc), \
1195 .syss_offset = (optsyss), \
1196 .revision = (optrev_val), \
1197 .revision_mask = (optrevmask), \
1198 .quirks = (optquirkmask), \
1199 }
1200
1201static const struct sysc_revision_quirk sysc_revision_quirks[] = {
1202 /* These drivers need to be fixed to not use pm_runtime_irq_safe() */
Tony Lindgren3a3d8022018-09-27 13:33:58 -07001203 SYSC_QUIRK("gpio", 0, 0, 0x10, 0x114, 0x50600801, 0xffff00ff,
Tony Lindgren09dfe582018-04-16 10:25:52 -07001204 SYSC_QUIRK_LEGACY_IDLE | SYSC_QUIRK_OPT_CLKS_IN_RESET),
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001205 SYSC_QUIRK("mmu", 0, 0, 0x10, 0x14, 0x00000020, 0xffffffff,
1206 SYSC_QUIRK_LEGACY_IDLE),
1207 SYSC_QUIRK("mmu", 0, 0, 0x10, 0x14, 0x00000030, 0xffffffff,
1208 SYSC_QUIRK_LEGACY_IDLE),
1209 SYSC_QUIRK("sham", 0, 0x100, 0x110, 0x114, 0x40000c03, 0xffffffff,
1210 SYSC_QUIRK_LEGACY_IDLE),
1211 SYSC_QUIRK("smartreflex", 0, -1, 0x24, -1, 0x00000000, 0xffffffff,
1212 SYSC_QUIRK_LEGACY_IDLE),
1213 SYSC_QUIRK("smartreflex", 0, -1, 0x38, -1, 0x00000000, 0xffffffff,
1214 SYSC_QUIRK_LEGACY_IDLE),
1215 SYSC_QUIRK("timer", 0, 0, 0x10, 0x14, 0x00000015, 0xffffffff,
Tony Lindgren9bd34c62019-01-22 09:03:08 -08001216 0),
Tony Lindgren8cde5d52018-04-16 10:25:15 -07001217 /* Some timers on omap4 and later */
Tony Lindgren3a3d8022018-09-27 13:33:58 -07001218 SYSC_QUIRK("timer", 0, 0, 0x10, -1, 0x50002100, 0xffffffff,
Tony Lindgren072167d2019-01-29 07:53:47 -08001219 0),
Tony Lindgren3a3d8022018-09-27 13:33:58 -07001220 SYSC_QUIRK("timer", 0, 0, 0x10, -1, 0x4fff1301, 0xffff00ff,
Tony Lindgren9bd34c62019-01-22 09:03:08 -08001221 0),
Tony Lindgrenb6a53c42019-05-27 04:51:57 -07001222 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x00000046, 0xffffffff,
1223 SYSC_QUIRK_SWSUP_SIDLE | SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001224 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x00000052, 0xffffffff,
Tony Lindgrenb6a53c42019-05-27 04:51:57 -07001225 SYSC_QUIRK_SWSUP_SIDLE | SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgrend708bb12018-04-16 10:25:32 -07001226 /* Uarts on omap4 and later */
Tony Lindgrenb82beef2018-09-24 12:16:59 -07001227 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x50411e03, 0xffff00ff,
Tony Lindgrenb4a9a7a2019-03-21 13:27:08 -07001228 SYSC_QUIRK_SWSUP_SIDLE_ACT | SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgrenb82beef2018-09-24 12:16:59 -07001229 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x47422e03, 0xffffffff,
Tony Lindgrenb4a9a7a2019-03-21 13:27:08 -07001230 SYSC_QUIRK_SWSUP_SIDLE_ACT | SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgren7e27e5d2018-04-20 14:54:16 -07001231
Tony Lindgrena54275f2019-03-21 11:00:21 -07001232 /* Quirks that need to be set based on the module address */
1233 SYSC_QUIRK("mcpdm", 0x40132000, 0, 0x10, -1, 0x50000800, 0xffffffff,
1234 SYSC_QUIRK_EXT_OPT_CLOCK | SYSC_QUIRK_NO_RESET_ON_INIT |
1235 SYSC_QUIRK_SWSUP_SIDLE),
1236
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001237#ifdef DEBUG
Tony Lindgren1ba30692018-09-24 12:17:05 -07001238 SYSC_QUIRK("adc", 0, 0, 0x10, -1, 0x47300001, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -07001239 SYSC_QUIRK("atl", 0, 0, -1, -1, 0x0a070100, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001240 SYSC_QUIRK("aess", 0, 0, 0x10, -1, 0x40000000, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -07001241 SYSC_QUIRK("cm", 0, 0, -1, -1, 0x40000301, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -07001242 SYSC_QUIRK("control", 0, 0, 0x10, -1, 0x40000900, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001243 SYSC_QUIRK("cpgmac", 0, 0x1200, 0x1208, 0x1204, 0x4edb1902,
Tony Lindgren23731ea2018-09-24 12:17:09 -07001244 0xffff00f0, 0),
1245 SYSC_QUIRK("dcan", 0, 0, -1, -1, 0xffffffff, 0xffffffff, 0),
Tony Lindgren13aad512019-03-21 11:00:21 -07001246 SYSC_QUIRK("dmic", 0, 0, 0x10, -1, 0x50010000, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001247 SYSC_QUIRK("dwc3", 0, 0, 0x10, -1, 0x500a0200, 0xffffffff, 0),
1248 SYSC_QUIRK("epwmss", 0, 0, 0x4, -1, 0x47400001, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001249 SYSC_QUIRK("gpu", 0, 0x1fc00, 0x1fc10, -1, 0, 0, 0),
1250 SYSC_QUIRK("hdq1w", 0, 0, 0x14, 0x18, 0x00000006, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001251 SYSC_QUIRK("hdq1w", 0, 0, 0x14, 0x18, 0x0000000a, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001252 SYSC_QUIRK("hsi", 0, 0, 0x10, 0x14, 0x50043101, 0xffffffff, 0),
1253 SYSC_QUIRK("iss", 0, 0, 0x10, -1, 0x40000101, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -07001254 SYSC_QUIRK("i2c", 0, 0, 0x10, 0x90, 0x5040000a, 0xfffff0f0, 0),
Tony Lindgren23731ea2018-09-24 12:17:09 -07001255 SYSC_QUIRK("lcdc", 0, 0, 0x54, -1, 0x4f201000, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001256 SYSC_QUIRK("mcasp", 0, 0, 0x4, -1, 0x44306302, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001257 SYSC_QUIRK("mcasp", 0, 0, 0x4, -1, 0x44307b02, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001258 SYSC_QUIRK("mcbsp", 0, -1, 0x8c, -1, 0, 0, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -07001259 SYSC_QUIRK("mcspi", 0, 0, 0x10, -1, 0x40300a0b, 0xffff00ff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001260 SYSC_QUIRK("mcspi", 0, 0, 0x110, 0x114, 0x40300a0b, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001261 SYSC_QUIRK("mailbox", 0, 0, 0x10, -1, 0x00000400, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001262 SYSC_QUIRK("m3", 0, 0, -1, -1, 0x5f580105, 0x0fff0f00, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -07001263 SYSC_QUIRK("ocp2scp", 0, 0, 0x10, 0x14, 0x50060005, 0xfffffff0, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001264 SYSC_QUIRK("ocp2scp", 0, 0, -1, -1, 0x50060007, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -07001265 SYSC_QUIRK("padconf", 0, 0, 0x10, -1, 0x4fff0800, 0xffffffff, 0),
Tony Lindgrenf0106702018-11-15 14:46:53 -08001266 SYSC_QUIRK("padconf", 0, 0, -1, -1, 0x40001100, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -07001267 SYSC_QUIRK("prcm", 0, 0, -1, -1, 0x40000100, 0xffffffff, 0),
Tony Lindgren23731ea2018-09-24 12:17:09 -07001268 SYSC_QUIRK("prcm", 0, 0, -1, -1, 0x00004102, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001269 SYSC_QUIRK("prcm", 0, 0, -1, -1, 0x40000400, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -07001270 SYSC_QUIRK("scm", 0, 0, 0x10, -1, 0x40000900, 0xffffffff, 0),
Tony Lindgren23731ea2018-09-24 12:17:09 -07001271 SYSC_QUIRK("scm", 0, 0, -1, -1, 0x4e8b0100, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001272 SYSC_QUIRK("scm", 0, 0, -1, -1, 0x4f000100, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -07001273 SYSC_QUIRK("scm", 0, 0, -1, -1, 0x40000900, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -07001274 SYSC_QUIRK("scrm", 0, 0, -1, -1, 0x00000010, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -07001275 SYSC_QUIRK("sdio", 0, 0, 0x10, -1, 0x40202301, 0xffff0ff0, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001276 SYSC_QUIRK("sdio", 0, 0x2fc, 0x110, 0x114, 0x31010000, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -07001277 SYSC_QUIRK("sdma", 0, 0, 0x2c, 0x28, 0x00010900, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001278 SYSC_QUIRK("slimbus", 0, 0, 0x10, -1, 0x40000902, 0xffffffff, 0),
1279 SYSC_QUIRK("slimbus", 0, 0, 0x10, -1, 0x40002903, 0xffffffff, 0),
1280 SYSC_QUIRK("spinlock", 0, 0, 0x10, -1, 0x50020000, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001281 SYSC_QUIRK("rng", 0, 0x1fe0, 0x1fe4, -1, 0x00000020, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -07001282 SYSC_QUIRK("rtc", 0, 0x74, 0x78, -1, 0x4eb01908, 0xffff00f0, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001283 SYSC_QUIRK("timer32k", 0, 0, 0x4, -1, 0x00000060, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001284 SYSC_QUIRK("usbhstll", 0, 0, 0x10, 0x14, 0x00000004, 0xffffffff, 0),
Tony Lindgrenf0106702018-11-15 14:46:53 -08001285 SYSC_QUIRK("usbhstll", 0, 0, 0x10, 0x14, 0x00000008, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001286 SYSC_QUIRK("usb_host_hs", 0, 0, 0x10, 0x14, 0x50700100, 0xffffffff, 0),
Tony Lindgrenf0106702018-11-15 14:46:53 -08001287 SYSC_QUIRK("usb_host_hs", 0, 0, 0x10, -1, 0x50700101, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001288 SYSC_QUIRK("usb_otg_hs", 0, 0x400, 0x404, 0x408, 0x00000050,
1289 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001290 SYSC_QUIRK("wdt", 0, 0, 0x10, 0x14, 0x502a0500, 0xfffff0f0, 0),
1291 SYSC_QUIRK("vfpe", 0, 0, 0x104, -1, 0x4d001200, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001292#endif
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001293};
1294
Tony Lindgren42b9c5c2019-03-21 11:00:21 -07001295/*
1296 * Early quirks based on module base and register offsets only that are
1297 * needed before the module revision can be read
1298 */
1299static void sysc_init_early_quirks(struct sysc *ddata)
1300{
1301 const struct sysc_revision_quirk *q;
1302 int i;
1303
1304 for (i = 0; i < ARRAY_SIZE(sysc_revision_quirks); i++) {
1305 q = &sysc_revision_quirks[i];
1306
1307 if (!q->base)
1308 continue;
1309
1310 if (q->base != ddata->module_pa)
1311 continue;
1312
1313 if (q->rev_offset >= 0 &&
1314 q->rev_offset != ddata->offsets[SYSC_REVISION])
1315 continue;
1316
1317 if (q->sysc_offset >= 0 &&
1318 q->sysc_offset != ddata->offsets[SYSC_SYSCONFIG])
1319 continue;
1320
1321 if (q->syss_offset >= 0 &&
1322 q->syss_offset != ddata->offsets[SYSC_SYSSTATUS])
1323 continue;
1324
1325 ddata->name = q->name;
1326 ddata->cfg.quirks |= q->quirks;
1327 }
1328}
1329
1330/* Quirks that also consider the revision register value */
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001331static void sysc_init_revision_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 && q->base != ddata->module_pa)
1340 continue;
1341
1342 if (q->rev_offset >= 0 &&
1343 q->rev_offset != ddata->offsets[SYSC_REVISION])
1344 continue;
1345
1346 if (q->sysc_offset >= 0 &&
1347 q->sysc_offset != ddata->offsets[SYSC_SYSCONFIG])
1348 continue;
1349
1350 if (q->syss_offset >= 0 &&
1351 q->syss_offset != ddata->offsets[SYSC_SYSSTATUS])
1352 continue;
1353
1354 if (q->revision == ddata->revision ||
1355 (q->revision & q->revision_mask) ==
1356 (ddata->revision & q->revision_mask)) {
1357 ddata->name = q->name;
1358 ddata->cfg.quirks |= q->quirks;
1359 }
1360 }
1361}
1362
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001363static int sysc_clockdomain_init(struct sysc *ddata)
1364{
1365 struct ti_sysc_platform_data *pdata = dev_get_platdata(ddata->dev);
1366 struct clk *fck = NULL, *ick = NULL;
1367 int error;
1368
1369 if (!pdata || !pdata->init_clockdomain)
1370 return 0;
1371
1372 switch (ddata->nr_clocks) {
1373 case 2:
1374 ick = ddata->clocks[SYSC_ICK];
1375 /* fallthrough */
1376 case 1:
1377 fck = ddata->clocks[SYSC_FCK];
1378 break;
1379 case 0:
1380 return 0;
1381 }
1382
1383 error = pdata->init_clockdomain(ddata->dev, fck, ick, &ddata->cookie);
1384 if (!error || error == -ENODEV)
1385 return 0;
1386
1387 return error;
1388}
1389
Tony Lindgrena3e92e72019-03-21 11:00:21 -07001390/*
1391 * Note that pdata->init_module() typically does a reset first. After
1392 * pdata->init_module() is done, PM runtime can be used for the interconnect
1393 * target module.
1394 */
1395static int sysc_legacy_init(struct sysc *ddata)
1396{
1397 struct ti_sysc_platform_data *pdata = dev_get_platdata(ddata->dev);
1398 int error;
1399
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001400 if (!pdata || !pdata->init_module)
Tony Lindgrena3e92e72019-03-21 11:00:21 -07001401 return 0;
1402
1403 error = pdata->init_module(ddata->dev, ddata->mdata, &ddata->cookie);
1404 if (error == -EEXIST)
1405 error = 0;
1406
1407 return error;
1408}
1409
Tony Lindgrenb11c1ea2019-03-21 11:00:21 -07001410/**
1411 * sysc_rstctrl_reset_deassert - deassert rstctrl reset
1412 * @ddata: device driver data
1413 * @reset: reset before deassert
1414 *
1415 * A module can have both OCP softreset control and external rstctrl.
1416 * If more complicated rstctrl resets are needed, please handle these
1417 * directly from the child device driver and map only the module reset
1418 * for the parent interconnect target module device.
1419 *
1420 * Automatic reset of the module on init can be skipped with the
1421 * "ti,no-reset-on-init" device tree property.
1422 */
1423static int sysc_rstctrl_reset_deassert(struct sysc *ddata, bool reset)
1424{
Tony Lindgrenea5a2e42019-05-27 04:51:56 -07001425 int error, val;
Tony Lindgrenb11c1ea2019-03-21 11:00:21 -07001426
1427 if (!ddata->rsts)
1428 return 0;
1429
1430 if (reset) {
1431 error = reset_control_assert(ddata->rsts);
1432 if (error)
1433 return error;
1434 }
1435
Tony Lindgrenea5a2e42019-05-27 04:51:56 -07001436 error = reset_control_deassert(ddata->rsts);
1437 if (error == -EEXIST)
1438 return 0;
1439
1440 error = readx_poll_timeout(reset_control_status, ddata->rsts, val,
1441 val == 0, 100, MAX_MODULE_SOFTRESET_WAIT);
1442
1443 return error;
Tony Lindgrenb11c1ea2019-03-21 11:00:21 -07001444}
1445
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001446/*
1447 * Note that the caller must ensure the interconnect target module is enabled
1448 * before calling reset. Otherwise reset will not complete.
1449 */
Faiz Abbas596e7952018-07-09 22:18:39 +05301450static int sysc_reset(struct sysc *ddata)
1451{
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001452 int sysc_offset, syss_offset, sysc_val, rstval, quirks, error = 0;
1453 u32 sysc_mask, syss_done;
Faiz Abbas596e7952018-07-09 22:18:39 +05301454
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001455 sysc_offset = ddata->offsets[SYSC_SYSCONFIG];
1456 syss_offset = ddata->offsets[SYSC_SYSSTATUS];
1457 quirks = ddata->cfg.quirks;
1458
1459 if (ddata->legacy_mode || sysc_offset < 0 ||
1460 ddata->cap->regbits->srst_shift < 0 ||
Faiz Abbas596e7952018-07-09 22:18:39 +05301461 ddata->cfg.quirks & SYSC_QUIRK_NO_RESET_ON_INIT)
1462 return 0;
1463
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001464 sysc_mask = BIT(ddata->cap->regbits->srst_shift);
Faiz Abbas596e7952018-07-09 22:18:39 +05301465
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001466 if (ddata->cfg.quirks & SYSS_QUIRK_RESETDONE_INVERTED)
1467 syss_done = 0;
1468 else
1469 syss_done = ddata->cfg.syss_mask;
1470
1471 sysc_val = sysc_read_sysconfig(ddata);
1472 sysc_val |= sysc_mask;
1473 sysc_write(ddata, sysc_offset, sysc_val);
Faiz Abbas596e7952018-07-09 22:18:39 +05301474
1475 /* Poll on reset status */
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001476 if (syss_offset >= 0) {
1477 error = readx_poll_timeout(sysc_read_sysstatus, ddata, rstval,
1478 (rstval & ddata->cfg.syss_mask) ==
1479 syss_done,
1480 100, MAX_MODULE_SOFTRESET_WAIT);
Faiz Abbas596e7952018-07-09 22:18:39 +05301481
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001482 } else if (ddata->cfg.quirks & SYSC_QUIRK_RESET_STATUS) {
1483 error = readx_poll_timeout(sysc_read_sysconfig, ddata, rstval,
1484 !(rstval & sysc_mask),
1485 100, MAX_MODULE_SOFTRESET_WAIT);
1486 }
1487
1488 return error;
Faiz Abbas596e7952018-07-09 22:18:39 +05301489}
1490
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001491/*
1492 * At this point the module is configured enough to read the revision but
1493 * module may not be completely configured yet to use PM runtime. Enable
1494 * all clocks directly during init to configure the quirks needed for PM
1495 * runtime based on the revision register.
1496 */
Tony Lindgren566a9b052017-12-15 09:41:19 -08001497static int sysc_init_module(struct sysc *ddata)
1498{
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001499 int error = 0;
1500 bool manage_clocks = true;
Tony Lindgrenb11c1ea2019-03-21 11:00:21 -07001501
Tony Lindgrenea5a2e42019-05-27 04:51:56 -07001502 error = sysc_rstctrl_reset_deassert(ddata, false);
Tony Lindgrenb11c1ea2019-03-21 11:00:21 -07001503 if (error)
1504 return error;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001505
Tony Lindgren386cb762019-03-22 07:49:30 -07001506 if (ddata->cfg.quirks &
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001507 (SYSC_QUIRK_NO_IDLE | SYSC_QUIRK_NO_IDLE_ON_INIT))
1508 manage_clocks = false;
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001509
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001510 error = sysc_clockdomain_init(ddata);
1511 if (error)
1512 return error;
1513
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001514 if (manage_clocks) {
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001515 sysc_clkdm_deny_idle(ddata);
1516
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001517 error = sysc_enable_opt_clocks(ddata);
1518 if (error)
1519 return error;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001520
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001521 error = sysc_enable_main_clocks(ddata);
1522 if (error)
1523 goto err_opt_clocks;
Faiz Abbas596e7952018-07-09 22:18:39 +05301524 }
1525
Tony Lindgrenea5a2e42019-05-27 04:51:56 -07001526 if (!(ddata->cfg.quirks & SYSC_QUIRK_NO_RESET_ON_INIT)) {
1527 error = sysc_rstctrl_reset_deassert(ddata, true);
1528 if (error)
1529 goto err_main_clocks;
1530 }
1531
Tony Lindgren566a9b052017-12-15 09:41:19 -08001532 ddata->revision = sysc_read_revision(ddata);
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001533 sysc_init_revision_quirks(ddata);
1534
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001535 if (ddata->legacy_mode) {
1536 error = sysc_legacy_init(ddata);
1537 if (error)
1538 goto err_main_clocks;
1539 }
1540
1541 if (!ddata->legacy_mode && manage_clocks) {
1542 error = sysc_enable_module(ddata->dev);
1543 if (error)
1544 goto err_main_clocks;
1545 }
Tony Lindgrena3e92e72019-03-21 11:00:21 -07001546
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001547 error = sysc_reset(ddata);
1548 if (error)
1549 dev_err(ddata->dev, "Reset failed with %d\n", error);
1550
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001551 if (!ddata->legacy_mode && manage_clocks)
1552 sysc_disable_module(ddata->dev);
1553
Tony Lindgrena3e92e72019-03-21 11:00:21 -07001554err_main_clocks:
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001555 if (manage_clocks)
1556 sysc_disable_main_clocks(ddata);
1557err_opt_clocks:
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001558 if (manage_clocks) {
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001559 sysc_disable_opt_clocks(ddata);
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001560 sysc_clkdm_allow_idle(ddata);
1561 }
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001562
1563 return error;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001564}
1565
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08001566static int sysc_init_sysc_mask(struct sysc *ddata)
1567{
1568 struct device_node *np = ddata->dev->of_node;
1569 int error;
1570 u32 val;
1571
1572 error = of_property_read_u32(np, "ti,sysc-mask", &val);
1573 if (error)
1574 return 0;
1575
1576 if (val)
1577 ddata->cfg.sysc_val = val & ddata->cap->sysc_mask;
1578 else
1579 ddata->cfg.sysc_val = ddata->cap->sysc_mask;
1580
1581 return 0;
1582}
1583
1584static int sysc_init_idlemode(struct sysc *ddata, u8 *idlemodes,
1585 const char *name)
1586{
1587 struct device_node *np = ddata->dev->of_node;
1588 struct property *prop;
1589 const __be32 *p;
1590 u32 val;
1591
1592 of_property_for_each_u32(np, name, prop, p, val) {
1593 if (val >= SYSC_NR_IDLEMODES) {
1594 dev_err(ddata->dev, "invalid idlemode: %i\n", val);
1595 return -EINVAL;
1596 }
1597 *idlemodes |= (1 << val);
1598 }
1599
1600 return 0;
1601}
1602
1603static int sysc_init_idlemodes(struct sysc *ddata)
1604{
1605 int error;
1606
1607 error = sysc_init_idlemode(ddata, &ddata->cfg.midlemodes,
1608 "ti,sysc-midle");
1609 if (error)
1610 return error;
1611
1612 error = sysc_init_idlemode(ddata, &ddata->cfg.sidlemodes,
1613 "ti,sysc-sidle");
1614 if (error)
1615 return error;
1616
1617 return 0;
1618}
1619
1620/*
1621 * Only some devices on omap4 and later have SYSCONFIG reset done
1622 * bit. We can detect this if there is no SYSSTATUS at all, or the
1623 * SYSTATUS bit 0 is not used. Note that some SYSSTATUS registers
1624 * have multiple bits for the child devices like OHCI and EHCI.
1625 * Depends on SYSC being parsed first.
1626 */
1627static int sysc_init_syss_mask(struct sysc *ddata)
1628{
1629 struct device_node *np = ddata->dev->of_node;
1630 int error;
1631 u32 val;
1632
1633 error = of_property_read_u32(np, "ti,syss-mask", &val);
1634 if (error) {
1635 if ((ddata->cap->type == TI_SYSC_OMAP4 ||
1636 ddata->cap->type == TI_SYSC_OMAP4_TIMER) &&
1637 (ddata->cfg.sysc_val & SYSC_OMAP4_SOFTRESET))
1638 ddata->cfg.quirks |= SYSC_QUIRK_RESET_STATUS;
1639
1640 return 0;
1641 }
1642
1643 if (!(val & 1) && (ddata->cfg.sysc_val & SYSC_OMAP4_SOFTRESET))
1644 ddata->cfg.quirks |= SYSC_QUIRK_RESET_STATUS;
1645
1646 ddata->cfg.syss_mask = val;
1647
1648 return 0;
1649}
1650
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001651/*
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001652 * Many child device drivers need to have fck and opt clocks available
1653 * to get the clock rate for device internal configuration etc.
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001654 */
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001655static int sysc_child_add_named_clock(struct sysc *ddata,
1656 struct device *child,
1657 const char *name)
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001658{
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001659 struct clk *clk;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001660 struct clk_lookup *l;
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001661 int error = 0;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001662
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001663 if (!name)
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001664 return 0;
1665
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001666 clk = clk_get(child, name);
1667 if (!IS_ERR(clk)) {
1668 clk_put(clk);
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001669
1670 return -EEXIST;
1671 }
1672
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001673 clk = clk_get(ddata->dev, name);
1674 if (IS_ERR(clk))
1675 return -ENODEV;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001676
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001677 l = clkdev_create(clk, name, dev_name(child));
1678 if (!l)
1679 error = -ENOMEM;
1680
1681 clk_put(clk);
1682
1683 return error;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001684}
1685
Tony Lindgren09dfe582018-04-16 10:25:52 -07001686static int sysc_child_add_clocks(struct sysc *ddata,
1687 struct device *child)
1688{
1689 int i, error;
1690
1691 for (i = 0; i < ddata->nr_clocks; i++) {
1692 error = sysc_child_add_named_clock(ddata,
1693 child,
1694 ddata->clock_roles[i]);
1695 if (error && error != -EEXIST) {
1696 dev_err(ddata->dev, "could not add child clock %s: %i\n",
1697 ddata->clock_roles[i], error);
1698
1699 return error;
1700 }
1701 }
1702
1703 return 0;
1704}
1705
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001706static struct device_type sysc_device_type = {
1707};
1708
1709static struct sysc *sysc_child_to_parent(struct device *dev)
1710{
1711 struct device *parent = dev->parent;
1712
1713 if (!parent || parent->type != &sysc_device_type)
1714 return NULL;
1715
1716 return dev_get_drvdata(parent);
1717}
1718
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001719static int __maybe_unused sysc_child_runtime_suspend(struct device *dev)
1720{
1721 struct sysc *ddata;
1722 int error;
1723
1724 ddata = sysc_child_to_parent(dev);
1725
1726 error = pm_generic_runtime_suspend(dev);
1727 if (error)
1728 return error;
1729
1730 if (!ddata->enabled)
1731 return 0;
1732
1733 return sysc_runtime_suspend(ddata->dev);
1734}
1735
1736static int __maybe_unused sysc_child_runtime_resume(struct device *dev)
1737{
1738 struct sysc *ddata;
1739 int error;
1740
1741 ddata = sysc_child_to_parent(dev);
1742
1743 if (!ddata->enabled) {
1744 error = sysc_runtime_resume(ddata->dev);
1745 if (error < 0)
1746 dev_err(ddata->dev,
1747 "%s error: %i\n", __func__, error);
1748 }
1749
1750 return pm_generic_runtime_resume(dev);
1751}
1752
1753#ifdef CONFIG_PM_SLEEP
1754static int sysc_child_suspend_noirq(struct device *dev)
1755{
1756 struct sysc *ddata;
1757 int error;
1758
1759 ddata = sysc_child_to_parent(dev);
1760
Tony Lindgrenef55f822018-04-16 10:27:15 -07001761 dev_dbg(ddata->dev, "%s %s\n", __func__,
1762 ddata->name ? ddata->name : "");
1763
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001764 error = pm_generic_suspend_noirq(dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001765 if (error) {
1766 dev_err(dev, "%s error at %i: %i\n",
1767 __func__, __LINE__, error);
1768
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001769 return error;
Tony Lindgrenef55f822018-04-16 10:27:15 -07001770 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001771
1772 if (!pm_runtime_status_suspended(dev)) {
1773 error = pm_generic_runtime_suspend(dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001774 if (error) {
Tony Lindgrenf9490782018-09-28 15:21:50 -07001775 dev_dbg(dev, "%s busy at %i: %i\n",
1776 __func__, __LINE__, error);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001777
Tony Lindgren4f3530f2018-08-08 01:07:06 -07001778 return 0;
Tony Lindgrenef55f822018-04-16 10:27:15 -07001779 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001780
1781 error = sysc_runtime_suspend(ddata->dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001782 if (error) {
1783 dev_err(dev, "%s error at %i: %i\n",
1784 __func__, __LINE__, error);
1785
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001786 return error;
Tony Lindgrenef55f822018-04-16 10:27:15 -07001787 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001788
1789 ddata->child_needs_resume = true;
1790 }
1791
1792 return 0;
1793}
1794
1795static int sysc_child_resume_noirq(struct device *dev)
1796{
1797 struct sysc *ddata;
1798 int error;
1799
1800 ddata = sysc_child_to_parent(dev);
1801
Tony Lindgrenef55f822018-04-16 10:27:15 -07001802 dev_dbg(ddata->dev, "%s %s\n", __func__,
1803 ddata->name ? ddata->name : "");
1804
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001805 if (ddata->child_needs_resume) {
1806 ddata->child_needs_resume = false;
1807
1808 error = sysc_runtime_resume(ddata->dev);
1809 if (error)
1810 dev_err(ddata->dev,
1811 "%s runtime resume error: %i\n",
1812 __func__, error);
1813
1814 error = pm_generic_runtime_resume(dev);
1815 if (error)
1816 dev_err(ddata->dev,
1817 "%s generic runtime resume: %i\n",
1818 __func__, error);
1819 }
1820
1821 return pm_generic_resume_noirq(dev);
1822}
1823#endif
1824
Tony Lindgrenb7182b42019-03-21 11:00:21 -07001825static struct dev_pm_domain sysc_child_pm_domain = {
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001826 .ops = {
1827 SET_RUNTIME_PM_OPS(sysc_child_runtime_suspend,
1828 sysc_child_runtime_resume,
1829 NULL)
1830 USE_PLATFORM_PM_SLEEP_OPS
1831 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(sysc_child_suspend_noirq,
1832 sysc_child_resume_noirq)
1833 }
1834};
1835
1836/**
1837 * sysc_legacy_idle_quirk - handle children in omap_device compatible way
1838 * @ddata: device driver data
1839 * @child: child device driver
1840 *
1841 * Allow idle for child devices as done with _od_runtime_suspend().
1842 * Otherwise many child devices will not idle because of the permanent
1843 * parent usecount set in pm_runtime_irq_safe().
1844 *
1845 * Note that the long term solution is to just modify the child device
1846 * drivers to not set pm_runtime_irq_safe() and then this can be just
1847 * dropped.
1848 */
1849static void sysc_legacy_idle_quirk(struct sysc *ddata, struct device *child)
1850{
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001851 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
1852 dev_pm_domain_set(child, &sysc_child_pm_domain);
1853}
1854
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001855static int sysc_notifier_call(struct notifier_block *nb,
1856 unsigned long event, void *device)
1857{
1858 struct device *dev = device;
1859 struct sysc *ddata;
1860 int error;
1861
1862 ddata = sysc_child_to_parent(dev);
1863 if (!ddata)
1864 return NOTIFY_DONE;
1865
1866 switch (event) {
1867 case BUS_NOTIFY_ADD_DEVICE:
Tony Lindgren09dfe582018-04-16 10:25:52 -07001868 error = sysc_child_add_clocks(ddata, dev);
1869 if (error)
1870 return error;
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001871 sysc_legacy_idle_quirk(ddata, dev);
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001872 break;
1873 default:
1874 break;
1875 }
1876
1877 return NOTIFY_DONE;
1878}
1879
1880static struct notifier_block sysc_nb = {
1881 .notifier_call = sysc_notifier_call,
1882};
1883
Tony Lindgren566a9b052017-12-15 09:41:19 -08001884/* Device tree configured quirks */
1885struct sysc_dts_quirk {
1886 const char *name;
1887 u32 mask;
1888};
1889
1890static const struct sysc_dts_quirk sysc_dts_quirks[] = {
1891 { .name = "ti,no-idle-on-init",
1892 .mask = SYSC_QUIRK_NO_IDLE_ON_INIT, },
1893 { .name = "ti,no-reset-on-init",
1894 .mask = SYSC_QUIRK_NO_RESET_ON_INIT, },
Tony Lindgren386cb762019-03-22 07:49:30 -07001895 { .name = "ti,no-idle",
1896 .mask = SYSC_QUIRK_NO_IDLE, },
Tony Lindgren566a9b052017-12-15 09:41:19 -08001897};
1898
Tony Lindgren4014c082018-12-10 14:11:26 -08001899static void sysc_parse_dts_quirks(struct sysc *ddata, struct device_node *np,
1900 bool is_child)
Tony Lindgren566a9b052017-12-15 09:41:19 -08001901{
Tony Lindgren566a9b052017-12-15 09:41:19 -08001902 const struct property *prop;
Tony Lindgren4014c082018-12-10 14:11:26 -08001903 int i, len;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001904
1905 for (i = 0; i < ARRAY_SIZE(sysc_dts_quirks); i++) {
Tony Lindgren4014c082018-12-10 14:11:26 -08001906 const char *name = sysc_dts_quirks[i].name;
1907
1908 prop = of_get_property(np, name, &len);
Tony Lindgren566a9b052017-12-15 09:41:19 -08001909 if (!prop)
Tony Lindgrend39b6ea2018-02-15 09:18:55 -08001910 continue;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001911
1912 ddata->cfg.quirks |= sysc_dts_quirks[i].mask;
Tony Lindgren4014c082018-12-10 14:11:26 -08001913 if (is_child) {
1914 dev_warn(ddata->dev,
1915 "dts flag should be at module level for %s\n",
1916 name);
1917 }
Tony Lindgren566a9b052017-12-15 09:41:19 -08001918 }
Tony Lindgren4014c082018-12-10 14:11:26 -08001919}
Tony Lindgren566a9b052017-12-15 09:41:19 -08001920
Tony Lindgren4014c082018-12-10 14:11:26 -08001921static int sysc_init_dts_quirks(struct sysc *ddata)
1922{
1923 struct device_node *np = ddata->dev->of_node;
1924 int error;
1925 u32 val;
1926
1927 ddata->legacy_mode = of_get_property(np, "ti,hwmods", NULL);
1928
1929 sysc_parse_dts_quirks(ddata, np, false);
Tony Lindgren566a9b052017-12-15 09:41:19 -08001930 error = of_property_read_u32(np, "ti,sysc-delay-us", &val);
1931 if (!error) {
1932 if (val > 255) {
1933 dev_warn(ddata->dev, "bad ti,sysc-delay-us: %i\n",
1934 val);
1935 }
1936
1937 ddata->cfg.srst_udelay = (u8)val;
1938 }
1939
1940 return 0;
1941}
1942
Tony Lindgren0eecc632017-10-10 14:23:43 -07001943static void sysc_unprepare(struct sysc *ddata)
1944{
1945 int i;
1946
Tony Lindgrenaaa29bb2019-03-21 11:00:21 -07001947 if (!ddata->clocks)
1948 return;
1949
Tony Lindgren0eecc632017-10-10 14:23:43 -07001950 for (i = 0; i < SYSC_MAX_CLOCKS; i++) {
1951 if (!IS_ERR_OR_NULL(ddata->clocks[i]))
1952 clk_unprepare(ddata->clocks[i]);
1953 }
1954}
1955
Tony Lindgren70a65242017-12-15 09:41:09 -08001956/*
1957 * Common sysc register bits found on omap2, also known as type1
1958 */
1959static const struct sysc_regbits sysc_regbits_omap2 = {
1960 .dmadisable_shift = -ENODEV,
1961 .midle_shift = 12,
1962 .sidle_shift = 3,
1963 .clkact_shift = 8,
1964 .emufree_shift = 5,
1965 .enwkup_shift = 2,
1966 .srst_shift = 1,
1967 .autoidle_shift = 0,
1968};
1969
1970static const struct sysc_capabilities sysc_omap2 = {
1971 .type = TI_SYSC_OMAP2,
1972 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY | SYSC_OMAP2_EMUFREE |
1973 SYSC_OMAP2_ENAWAKEUP | SYSC_OMAP2_SOFTRESET |
1974 SYSC_OMAP2_AUTOIDLE,
1975 .regbits = &sysc_regbits_omap2,
1976};
1977
1978/* All omap2 and 3 timers, and timers 1, 2 & 10 on omap 4 and 5 */
1979static const struct sysc_capabilities sysc_omap2_timer = {
1980 .type = TI_SYSC_OMAP2_TIMER,
1981 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY | SYSC_OMAP2_EMUFREE |
1982 SYSC_OMAP2_ENAWAKEUP | SYSC_OMAP2_SOFTRESET |
1983 SYSC_OMAP2_AUTOIDLE,
1984 .regbits = &sysc_regbits_omap2,
1985 .mod_quirks = SYSC_QUIRK_USE_CLOCKACT,
1986};
1987
1988/*
1989 * SHAM2 (SHA1/MD5) sysc found on omap3, a variant of sysc_regbits_omap2
1990 * with different sidle position
1991 */
1992static const struct sysc_regbits sysc_regbits_omap3_sham = {
1993 .dmadisable_shift = -ENODEV,
1994 .midle_shift = -ENODEV,
1995 .sidle_shift = 4,
1996 .clkact_shift = -ENODEV,
1997 .enwkup_shift = -ENODEV,
1998 .srst_shift = 1,
1999 .autoidle_shift = 0,
2000 .emufree_shift = -ENODEV,
2001};
2002
2003static const struct sysc_capabilities sysc_omap3_sham = {
2004 .type = TI_SYSC_OMAP3_SHAM,
2005 .sysc_mask = SYSC_OMAP2_SOFTRESET | SYSC_OMAP2_AUTOIDLE,
2006 .regbits = &sysc_regbits_omap3_sham,
2007};
2008
2009/*
2010 * AES register bits found on omap3 and later, a variant of
2011 * sysc_regbits_omap2 with different sidle position
2012 */
2013static const struct sysc_regbits sysc_regbits_omap3_aes = {
2014 .dmadisable_shift = -ENODEV,
2015 .midle_shift = -ENODEV,
2016 .sidle_shift = 6,
2017 .clkact_shift = -ENODEV,
2018 .enwkup_shift = -ENODEV,
2019 .srst_shift = 1,
2020 .autoidle_shift = 0,
2021 .emufree_shift = -ENODEV,
2022};
2023
2024static const struct sysc_capabilities sysc_omap3_aes = {
2025 .type = TI_SYSC_OMAP3_AES,
2026 .sysc_mask = SYSC_OMAP2_SOFTRESET | SYSC_OMAP2_AUTOIDLE,
2027 .regbits = &sysc_regbits_omap3_aes,
2028};
2029
2030/*
2031 * Common sysc register bits found on omap4, also known as type2
2032 */
2033static const struct sysc_regbits sysc_regbits_omap4 = {
2034 .dmadisable_shift = 16,
2035 .midle_shift = 4,
2036 .sidle_shift = 2,
2037 .clkact_shift = -ENODEV,
2038 .enwkup_shift = -ENODEV,
2039 .emufree_shift = 1,
2040 .srst_shift = 0,
2041 .autoidle_shift = -ENODEV,
2042};
2043
2044static const struct sysc_capabilities sysc_omap4 = {
2045 .type = TI_SYSC_OMAP4,
2046 .sysc_mask = SYSC_OMAP4_DMADISABLE | SYSC_OMAP4_FREEEMU |
2047 SYSC_OMAP4_SOFTRESET,
2048 .regbits = &sysc_regbits_omap4,
2049};
2050
2051static const struct sysc_capabilities sysc_omap4_timer = {
2052 .type = TI_SYSC_OMAP4_TIMER,
2053 .sysc_mask = SYSC_OMAP4_DMADISABLE | SYSC_OMAP4_FREEEMU |
2054 SYSC_OMAP4_SOFTRESET,
2055 .regbits = &sysc_regbits_omap4,
2056};
2057
2058/*
2059 * Common sysc register bits found on omap4, also known as type3
2060 */
2061static const struct sysc_regbits sysc_regbits_omap4_simple = {
2062 .dmadisable_shift = -ENODEV,
2063 .midle_shift = 2,
2064 .sidle_shift = 0,
2065 .clkact_shift = -ENODEV,
2066 .enwkup_shift = -ENODEV,
2067 .srst_shift = -ENODEV,
2068 .emufree_shift = -ENODEV,
2069 .autoidle_shift = -ENODEV,
2070};
2071
2072static const struct sysc_capabilities sysc_omap4_simple = {
2073 .type = TI_SYSC_OMAP4_SIMPLE,
2074 .regbits = &sysc_regbits_omap4_simple,
2075};
2076
2077/*
2078 * SmartReflex sysc found on omap34xx
2079 */
2080static const struct sysc_regbits sysc_regbits_omap34xx_sr = {
2081 .dmadisable_shift = -ENODEV,
2082 .midle_shift = -ENODEV,
2083 .sidle_shift = -ENODEV,
2084 .clkact_shift = 20,
2085 .enwkup_shift = -ENODEV,
2086 .srst_shift = -ENODEV,
2087 .emufree_shift = -ENODEV,
2088 .autoidle_shift = -ENODEV,
2089};
2090
2091static const struct sysc_capabilities sysc_34xx_sr = {
2092 .type = TI_SYSC_OMAP34XX_SR,
2093 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY,
2094 .regbits = &sysc_regbits_omap34xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002095 .mod_quirks = SYSC_QUIRK_USE_CLOCKACT | SYSC_QUIRK_UNCACHED |
2096 SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08002097};
2098
2099/*
2100 * SmartReflex sysc found on omap36xx and later
2101 */
2102static const struct sysc_regbits sysc_regbits_omap36xx_sr = {
2103 .dmadisable_shift = -ENODEV,
2104 .midle_shift = -ENODEV,
2105 .sidle_shift = 24,
2106 .clkact_shift = -ENODEV,
2107 .enwkup_shift = 26,
2108 .srst_shift = -ENODEV,
2109 .emufree_shift = -ENODEV,
2110 .autoidle_shift = -ENODEV,
2111};
2112
2113static const struct sysc_capabilities sysc_36xx_sr = {
2114 .type = TI_SYSC_OMAP36XX_SR,
Tony Lindgren3267c082018-01-22 09:32:53 -08002115 .sysc_mask = SYSC_OMAP3_SR_ENAWAKEUP,
Tony Lindgren70a65242017-12-15 09:41:09 -08002116 .regbits = &sysc_regbits_omap36xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002117 .mod_quirks = SYSC_QUIRK_UNCACHED | SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08002118};
2119
2120static const struct sysc_capabilities sysc_omap4_sr = {
2121 .type = TI_SYSC_OMAP4_SR,
2122 .regbits = &sysc_regbits_omap36xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002123 .mod_quirks = SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08002124};
2125
2126/*
2127 * McASP register bits found on omap4 and later
2128 */
2129static const struct sysc_regbits sysc_regbits_omap4_mcasp = {
2130 .dmadisable_shift = -ENODEV,
2131 .midle_shift = -ENODEV,
2132 .sidle_shift = 0,
2133 .clkact_shift = -ENODEV,
2134 .enwkup_shift = -ENODEV,
2135 .srst_shift = -ENODEV,
2136 .emufree_shift = -ENODEV,
2137 .autoidle_shift = -ENODEV,
2138};
2139
2140static const struct sysc_capabilities sysc_omap4_mcasp = {
2141 .type = TI_SYSC_OMAP4_MCASP,
2142 .regbits = &sysc_regbits_omap4_mcasp,
Tony Lindgren2c63a832018-11-15 14:46:53 -08002143 .mod_quirks = SYSC_QUIRK_OPT_CLKS_NEEDED,
2144};
2145
2146/*
2147 * McASP found on dra7 and later
2148 */
2149static const struct sysc_capabilities sysc_dra7_mcasp = {
2150 .type = TI_SYSC_OMAP4_SIMPLE,
2151 .regbits = &sysc_regbits_omap4_simple,
2152 .mod_quirks = SYSC_QUIRK_OPT_CLKS_NEEDED,
Tony Lindgren70a65242017-12-15 09:41:09 -08002153};
2154
2155/*
2156 * FS USB host found on omap4 and later
2157 */
2158static const struct sysc_regbits sysc_regbits_omap4_usb_host_fs = {
2159 .dmadisable_shift = -ENODEV,
2160 .midle_shift = -ENODEV,
2161 .sidle_shift = 24,
2162 .clkact_shift = -ENODEV,
2163 .enwkup_shift = 26,
2164 .srst_shift = -ENODEV,
2165 .emufree_shift = -ENODEV,
2166 .autoidle_shift = -ENODEV,
2167};
2168
2169static const struct sysc_capabilities sysc_omap4_usb_host_fs = {
2170 .type = TI_SYSC_OMAP4_USB_HOST_FS,
2171 .sysc_mask = SYSC_OMAP2_ENAWAKEUP,
2172 .regbits = &sysc_regbits_omap4_usb_host_fs,
2173};
2174
Faiz Abbas7f35e632018-07-09 22:18:38 +05302175static const struct sysc_regbits sysc_regbits_dra7_mcan = {
2176 .dmadisable_shift = -ENODEV,
2177 .midle_shift = -ENODEV,
2178 .sidle_shift = -ENODEV,
2179 .clkact_shift = -ENODEV,
2180 .enwkup_shift = 4,
2181 .srst_shift = 0,
2182 .emufree_shift = -ENODEV,
2183 .autoidle_shift = -ENODEV,
2184};
2185
2186static const struct sysc_capabilities sysc_dra7_mcan = {
2187 .type = TI_SYSC_DRA7_MCAN,
2188 .sysc_mask = SYSC_DRA7_MCAN_ENAWAKEUP | SYSC_OMAP4_SOFTRESET,
2189 .regbits = &sysc_regbits_dra7_mcan,
Tony Lindgrene0db94f2019-05-27 04:51:53 -07002190 .mod_quirks = SYSS_QUIRK_RESETDONE_INVERTED,
Faiz Abbas7f35e632018-07-09 22:18:38 +05302191};
2192
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002193static int sysc_init_pdata(struct sysc *ddata)
2194{
2195 struct ti_sysc_platform_data *pdata = dev_get_platdata(ddata->dev);
Tony Lindgrena3e92e72019-03-21 11:00:21 -07002196 struct ti_sysc_module_data *mdata;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002197
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07002198 if (!pdata)
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002199 return 0;
2200
Tony Lindgrena3e92e72019-03-21 11:00:21 -07002201 mdata = devm_kzalloc(ddata->dev, sizeof(*mdata), GFP_KERNEL);
2202 if (!mdata)
2203 return -ENOMEM;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002204
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07002205 if (ddata->legacy_mode) {
2206 mdata->name = ddata->legacy_mode;
2207 mdata->module_pa = ddata->module_pa;
2208 mdata->module_size = ddata->module_size;
2209 mdata->offsets = ddata->offsets;
2210 mdata->nr_offsets = SYSC_MAX_REGS;
2211 mdata->cap = ddata->cap;
2212 mdata->cfg = &ddata->cfg;
2213 }
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002214
Tony Lindgrena3e92e72019-03-21 11:00:21 -07002215 ddata->mdata = mdata;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002216
Tony Lindgrena3e92e72019-03-21 11:00:21 -07002217 return 0;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002218}
2219
Tony Lindgren70a65242017-12-15 09:41:09 -08002220static int sysc_init_match(struct sysc *ddata)
2221{
2222 const struct sysc_capabilities *cap;
2223
2224 cap = of_device_get_match_data(ddata->dev);
2225 if (!cap)
2226 return -EINVAL;
2227
2228 ddata->cap = cap;
2229 if (ddata->cap)
2230 ddata->cfg.quirks |= ddata->cap->mod_quirks;
2231
2232 return 0;
2233}
2234
Tony Lindgren76f0f772018-02-23 08:28:45 -08002235static void ti_sysc_idle(struct work_struct *work)
2236{
2237 struct sysc *ddata;
2238
2239 ddata = container_of(work, struct sysc, idle_work.work);
2240
2241 if (pm_runtime_active(ddata->dev))
2242 pm_runtime_put_sync(ddata->dev);
2243}
2244
Tony Lindgrenc4bebea2018-04-16 10:24:54 -07002245static const struct of_device_id sysc_match_table[] = {
2246 { .compatible = "simple-bus", },
2247 { /* sentinel */ },
2248};
2249
Tony Lindgren0eecc632017-10-10 14:23:43 -07002250static int sysc_probe(struct platform_device *pdev)
2251{
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002252 struct ti_sysc_platform_data *pdata = dev_get_platdata(&pdev->dev);
Tony Lindgren0eecc632017-10-10 14:23:43 -07002253 struct sysc *ddata;
2254 int error;
2255
2256 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
2257 if (!ddata)
2258 return -ENOMEM;
2259
2260 ddata->dev = &pdev->dev;
Tony Lindgren566a9b052017-12-15 09:41:19 -08002261 platform_set_drvdata(pdev, ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -07002262
Tony Lindgren70a65242017-12-15 09:41:09 -08002263 error = sysc_init_match(ddata);
2264 if (error)
2265 return error;
2266
Tony Lindgren566a9b052017-12-15 09:41:19 -08002267 error = sysc_init_dts_quirks(ddata);
2268 if (error)
2269 goto unprepare;
2270
Tony Lindgren0eecc632017-10-10 14:23:43 -07002271 error = sysc_map_and_check_registers(ddata);
2272 if (error)
2273 goto unprepare;
2274
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08002275 error = sysc_init_sysc_mask(ddata);
2276 if (error)
2277 goto unprepare;
2278
2279 error = sysc_init_idlemodes(ddata);
2280 if (error)
2281 goto unprepare;
2282
2283 error = sysc_init_syss_mask(ddata);
2284 if (error)
2285 goto unprepare;
2286
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002287 error = sysc_init_pdata(ddata);
2288 if (error)
2289 goto unprepare;
2290
Tony Lindgren42b9c5c2019-03-21 11:00:21 -07002291 sysc_init_early_quirks(ddata);
2292
2293 error = sysc_get_clocks(ddata);
2294 if (error)
2295 return error;
2296
Tony Lindgren50622362018-04-16 10:20:27 -07002297 error = sysc_init_resets(ddata);
2298 if (error)
2299 return error;
Tony Lindgren566a9b052017-12-15 09:41:19 -08002300
2301 error = sysc_init_module(ddata);
2302 if (error)
2303 goto unprepare;
2304
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07002305 pm_runtime_enable(ddata->dev);
Tony Lindgren0eecc632017-10-10 14:23:43 -07002306 error = pm_runtime_get_sync(ddata->dev);
2307 if (error < 0) {
2308 pm_runtime_put_noidle(ddata->dev);
2309 pm_runtime_disable(ddata->dev);
2310 goto unprepare;
2311 }
2312
Tony Lindgren0eecc632017-10-10 14:23:43 -07002313 sysc_show_registers(ddata);
2314
Tony Lindgren2c355ff2018-02-22 13:58:03 -08002315 ddata->dev->type = &sysc_device_type;
Tony Lindgrenc4bebea2018-04-16 10:24:54 -07002316 error = of_platform_populate(ddata->dev->of_node, sysc_match_table,
2317 pdata ? pdata->auxdata : NULL,
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002318 ddata->dev);
Tony Lindgren0eecc632017-10-10 14:23:43 -07002319 if (error)
2320 goto err;
2321
Tony Lindgren76f0f772018-02-23 08:28:45 -08002322 INIT_DELAYED_WORK(&ddata->idle_work, ti_sysc_idle);
2323
2324 /* At least earlycon won't survive without deferred idle */
2325 if (ddata->cfg.quirks & (SYSC_QUIRK_NO_IDLE_ON_INIT |
2326 SYSC_QUIRK_NO_RESET_ON_INIT)) {
2327 schedule_delayed_work(&ddata->idle_work, 3000);
2328 } else {
2329 pm_runtime_put(&pdev->dev);
2330 }
Tony Lindgren0eecc632017-10-10 14:23:43 -07002331
Tony Lindgren50622362018-04-16 10:20:27 -07002332 if (!of_get_available_child_count(ddata->dev->of_node))
Tony Lindgren8383e252019-05-27 04:51:56 -07002333 ddata->disable_on_idle = true;
Tony Lindgren50622362018-04-16 10:20:27 -07002334
Tony Lindgren0eecc632017-10-10 14:23:43 -07002335 return 0;
2336
2337err:
Tony Lindgren0eecc632017-10-10 14:23:43 -07002338 pm_runtime_put_sync(&pdev->dev);
2339 pm_runtime_disable(&pdev->dev);
2340unprepare:
2341 sysc_unprepare(ddata);
2342
2343 return error;
2344}
2345
Tony Lindgren684be5a2017-10-13 10:48:40 -07002346static int sysc_remove(struct platform_device *pdev)
2347{
2348 struct sysc *ddata = platform_get_drvdata(pdev);
2349 int error;
2350
Tony Lindgren76f0f772018-02-23 08:28:45 -08002351 cancel_delayed_work_sync(&ddata->idle_work);
2352
Tony Lindgren684be5a2017-10-13 10:48:40 -07002353 error = pm_runtime_get_sync(ddata->dev);
2354 if (error < 0) {
2355 pm_runtime_put_noidle(ddata->dev);
2356 pm_runtime_disable(ddata->dev);
2357 goto unprepare;
2358 }
2359
2360 of_platform_depopulate(&pdev->dev);
2361
Tony Lindgren684be5a2017-10-13 10:48:40 -07002362 pm_runtime_put_sync(&pdev->dev);
2363 pm_runtime_disable(&pdev->dev);
Tony Lindgren50622362018-04-16 10:20:27 -07002364 reset_control_assert(ddata->rsts);
Tony Lindgren684be5a2017-10-13 10:48:40 -07002365
2366unprepare:
2367 sysc_unprepare(ddata);
2368
2369 return 0;
2370}
2371
Tony Lindgren0eecc632017-10-10 14:23:43 -07002372static const struct of_device_id sysc_match[] = {
Tony Lindgren70a65242017-12-15 09:41:09 -08002373 { .compatible = "ti,sysc-omap2", .data = &sysc_omap2, },
2374 { .compatible = "ti,sysc-omap2-timer", .data = &sysc_omap2_timer, },
2375 { .compatible = "ti,sysc-omap4", .data = &sysc_omap4, },
2376 { .compatible = "ti,sysc-omap4-timer", .data = &sysc_omap4_timer, },
2377 { .compatible = "ti,sysc-omap4-simple", .data = &sysc_omap4_simple, },
2378 { .compatible = "ti,sysc-omap3430-sr", .data = &sysc_34xx_sr, },
2379 { .compatible = "ti,sysc-omap3630-sr", .data = &sysc_36xx_sr, },
2380 { .compatible = "ti,sysc-omap4-sr", .data = &sysc_omap4_sr, },
2381 { .compatible = "ti,sysc-omap3-sham", .data = &sysc_omap3_sham, },
2382 { .compatible = "ti,sysc-omap-aes", .data = &sysc_omap3_aes, },
2383 { .compatible = "ti,sysc-mcasp", .data = &sysc_omap4_mcasp, },
Tony Lindgren2c63a832018-11-15 14:46:53 -08002384 { .compatible = "ti,sysc-dra7-mcasp", .data = &sysc_dra7_mcasp, },
Tony Lindgren70a65242017-12-15 09:41:09 -08002385 { .compatible = "ti,sysc-usb-host-fs",
2386 .data = &sysc_omap4_usb_host_fs, },
Faiz Abbas7f35e632018-07-09 22:18:38 +05302387 { .compatible = "ti,sysc-dra7-mcan", .data = &sysc_dra7_mcan, },
Tony Lindgren0eecc632017-10-10 14:23:43 -07002388 { },
2389};
2390MODULE_DEVICE_TABLE(of, sysc_match);
2391
2392static struct platform_driver sysc_driver = {
2393 .probe = sysc_probe,
Tony Lindgren684be5a2017-10-13 10:48:40 -07002394 .remove = sysc_remove,
Tony Lindgren0eecc632017-10-10 14:23:43 -07002395 .driver = {
2396 .name = "ti-sysc",
2397 .of_match_table = sysc_match,
2398 .pm = &sysc_pm_ops,
2399 },
2400};
Tony Lindgren2c355ff2018-02-22 13:58:03 -08002401
2402static int __init sysc_init(void)
2403{
2404 bus_register_notifier(&platform_bus_type, &sysc_nb);
2405
2406 return platform_driver_register(&sysc_driver);
2407}
2408module_init(sysc_init);
2409
2410static void __exit sysc_exit(void)
2411{
2412 bus_unregister_notifier(&platform_bus_type, &sysc_nb);
2413 platform_driver_unregister(&sysc_driver);
2414}
2415module_exit(sysc_exit);
Tony Lindgren0eecc632017-10-10 14:23:43 -07002416
2417MODULE_DESCRIPTION("TI sysc interconnect target driver");
2418MODULE_LICENSE("GPL v2");