blob: bc4dbaa027cface6630d85eb4442ab50df3b81cb [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 Lindgrena885f0f2018-02-22 14:03:48 -08001222 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x00000052, 0xffffffff,
Tony Lindgrenb4a9a7a2019-03-21 13:27:08 -07001223 SYSC_QUIRK_SWSUP_SIDLE_ACT | SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgrend708bb12018-04-16 10:25:32 -07001224 /* Uarts on omap4 and later */
Tony Lindgrenb82beef2018-09-24 12:16:59 -07001225 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x50411e03, 0xffff00ff,
Tony Lindgrenb4a9a7a2019-03-21 13:27:08 -07001226 SYSC_QUIRK_SWSUP_SIDLE_ACT | SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgrenb82beef2018-09-24 12:16:59 -07001227 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x47422e03, 0xffffffff,
Tony Lindgrenb4a9a7a2019-03-21 13:27:08 -07001228 SYSC_QUIRK_SWSUP_SIDLE_ACT | SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgren7e27e5d2018-04-20 14:54:16 -07001229
Tony Lindgrena54275f2019-03-21 11:00:21 -07001230 /* Quirks that need to be set based on the module address */
1231 SYSC_QUIRK("mcpdm", 0x40132000, 0, 0x10, -1, 0x50000800, 0xffffffff,
1232 SYSC_QUIRK_EXT_OPT_CLOCK | SYSC_QUIRK_NO_RESET_ON_INIT |
1233 SYSC_QUIRK_SWSUP_SIDLE),
1234
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001235#ifdef DEBUG
Tony Lindgren1ba30692018-09-24 12:17:05 -07001236 SYSC_QUIRK("adc", 0, 0, 0x10, -1, 0x47300001, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -07001237 SYSC_QUIRK("atl", 0, 0, -1, -1, 0x0a070100, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001238 SYSC_QUIRK("aess", 0, 0, 0x10, -1, 0x40000000, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -07001239 SYSC_QUIRK("cm", 0, 0, -1, -1, 0x40000301, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -07001240 SYSC_QUIRK("control", 0, 0, 0x10, -1, 0x40000900, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001241 SYSC_QUIRK("cpgmac", 0, 0x1200, 0x1208, 0x1204, 0x4edb1902,
Tony Lindgren23731ea2018-09-24 12:17:09 -07001242 0xffff00f0, 0),
1243 SYSC_QUIRK("dcan", 0, 0, -1, -1, 0xffffffff, 0xffffffff, 0),
Tony Lindgren13aad512019-03-21 11:00:21 -07001244 SYSC_QUIRK("dmic", 0, 0, 0x10, -1, 0x50010000, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001245 SYSC_QUIRK("dwc3", 0, 0, 0x10, -1, 0x500a0200, 0xffffffff, 0),
1246 SYSC_QUIRK("epwmss", 0, 0, 0x4, -1, 0x47400001, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001247 SYSC_QUIRK("gpu", 0, 0x1fc00, 0x1fc10, -1, 0, 0, 0),
1248 SYSC_QUIRK("hdq1w", 0, 0, 0x14, 0x18, 0x00000006, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001249 SYSC_QUIRK("hdq1w", 0, 0, 0x14, 0x18, 0x0000000a, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001250 SYSC_QUIRK("hsi", 0, 0, 0x10, 0x14, 0x50043101, 0xffffffff, 0),
1251 SYSC_QUIRK("iss", 0, 0, 0x10, -1, 0x40000101, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -07001252 SYSC_QUIRK("i2c", 0, 0, 0x10, 0x90, 0x5040000a, 0xfffff0f0, 0),
Tony Lindgren23731ea2018-09-24 12:17:09 -07001253 SYSC_QUIRK("lcdc", 0, 0, 0x54, -1, 0x4f201000, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001254 SYSC_QUIRK("mcasp", 0, 0, 0x4, -1, 0x44306302, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001255 SYSC_QUIRK("mcasp", 0, 0, 0x4, -1, 0x44307b02, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001256 SYSC_QUIRK("mcbsp", 0, -1, 0x8c, -1, 0, 0, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -07001257 SYSC_QUIRK("mcspi", 0, 0, 0x10, -1, 0x40300a0b, 0xffff00ff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001258 SYSC_QUIRK("mcspi", 0, 0, 0x110, 0x114, 0x40300a0b, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001259 SYSC_QUIRK("mailbox", 0, 0, 0x10, -1, 0x00000400, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001260 SYSC_QUIRK("m3", 0, 0, -1, -1, 0x5f580105, 0x0fff0f00, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -07001261 SYSC_QUIRK("ocp2scp", 0, 0, 0x10, 0x14, 0x50060005, 0xfffffff0, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001262 SYSC_QUIRK("ocp2scp", 0, 0, -1, -1, 0x50060007, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -07001263 SYSC_QUIRK("padconf", 0, 0, 0x10, -1, 0x4fff0800, 0xffffffff, 0),
Tony Lindgrenf0106702018-11-15 14:46:53 -08001264 SYSC_QUIRK("padconf", 0, 0, -1, -1, 0x40001100, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -07001265 SYSC_QUIRK("prcm", 0, 0, -1, -1, 0x40000100, 0xffffffff, 0),
Tony Lindgren23731ea2018-09-24 12:17:09 -07001266 SYSC_QUIRK("prcm", 0, 0, -1, -1, 0x00004102, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001267 SYSC_QUIRK("prcm", 0, 0, -1, -1, 0x40000400, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -07001268 SYSC_QUIRK("scm", 0, 0, 0x10, -1, 0x40000900, 0xffffffff, 0),
Tony Lindgren23731ea2018-09-24 12:17:09 -07001269 SYSC_QUIRK("scm", 0, 0, -1, -1, 0x4e8b0100, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001270 SYSC_QUIRK("scm", 0, 0, -1, -1, 0x4f000100, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -07001271 SYSC_QUIRK("scm", 0, 0, -1, -1, 0x40000900, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -07001272 SYSC_QUIRK("scrm", 0, 0, -1, -1, 0x00000010, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -07001273 SYSC_QUIRK("sdio", 0, 0, 0x10, -1, 0x40202301, 0xffff0ff0, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001274 SYSC_QUIRK("sdio", 0, 0x2fc, 0x110, 0x114, 0x31010000, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -07001275 SYSC_QUIRK("sdma", 0, 0, 0x2c, 0x28, 0x00010900, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001276 SYSC_QUIRK("slimbus", 0, 0, 0x10, -1, 0x40000902, 0xffffffff, 0),
1277 SYSC_QUIRK("slimbus", 0, 0, 0x10, -1, 0x40002903, 0xffffffff, 0),
1278 SYSC_QUIRK("spinlock", 0, 0, 0x10, -1, 0x50020000, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001279 SYSC_QUIRK("rng", 0, 0x1fe0, 0x1fe4, -1, 0x00000020, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -07001280 SYSC_QUIRK("rtc", 0, 0x74, 0x78, -1, 0x4eb01908, 0xffff00f0, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001281 SYSC_QUIRK("timer32k", 0, 0, 0x4, -1, 0x00000060, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001282 SYSC_QUIRK("usbhstll", 0, 0, 0x10, 0x14, 0x00000004, 0xffffffff, 0),
Tony Lindgrenf0106702018-11-15 14:46:53 -08001283 SYSC_QUIRK("usbhstll", 0, 0, 0x10, 0x14, 0x00000008, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001284 SYSC_QUIRK("usb_host_hs", 0, 0, 0x10, 0x14, 0x50700100, 0xffffffff, 0),
Tony Lindgrenf0106702018-11-15 14:46:53 -08001285 SYSC_QUIRK("usb_host_hs", 0, 0, 0x10, -1, 0x50700101, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001286 SYSC_QUIRK("usb_otg_hs", 0, 0x400, 0x404, 0x408, 0x00000050,
1287 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001288 SYSC_QUIRK("wdt", 0, 0, 0x10, 0x14, 0x502a0500, 0xfffff0f0, 0),
1289 SYSC_QUIRK("vfpe", 0, 0, 0x104, -1, 0x4d001200, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001290#endif
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001291};
1292
Tony Lindgren42b9c5c2019-03-21 11:00:21 -07001293/*
1294 * Early quirks based on module base and register offsets only that are
1295 * needed before the module revision can be read
1296 */
1297static void sysc_init_early_quirks(struct sysc *ddata)
1298{
1299 const struct sysc_revision_quirk *q;
1300 int i;
1301
1302 for (i = 0; i < ARRAY_SIZE(sysc_revision_quirks); i++) {
1303 q = &sysc_revision_quirks[i];
1304
1305 if (!q->base)
1306 continue;
1307
1308 if (q->base != ddata->module_pa)
1309 continue;
1310
1311 if (q->rev_offset >= 0 &&
1312 q->rev_offset != ddata->offsets[SYSC_REVISION])
1313 continue;
1314
1315 if (q->sysc_offset >= 0 &&
1316 q->sysc_offset != ddata->offsets[SYSC_SYSCONFIG])
1317 continue;
1318
1319 if (q->syss_offset >= 0 &&
1320 q->syss_offset != ddata->offsets[SYSC_SYSSTATUS])
1321 continue;
1322
1323 ddata->name = q->name;
1324 ddata->cfg.quirks |= q->quirks;
1325 }
1326}
1327
1328/* Quirks that also consider the revision register value */
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001329static void sysc_init_revision_quirks(struct sysc *ddata)
1330{
1331 const struct sysc_revision_quirk *q;
1332 int i;
1333
1334 for (i = 0; i < ARRAY_SIZE(sysc_revision_quirks); i++) {
1335 q = &sysc_revision_quirks[i];
1336
1337 if (q->base && q->base != ddata->module_pa)
1338 continue;
1339
1340 if (q->rev_offset >= 0 &&
1341 q->rev_offset != ddata->offsets[SYSC_REVISION])
1342 continue;
1343
1344 if (q->sysc_offset >= 0 &&
1345 q->sysc_offset != ddata->offsets[SYSC_SYSCONFIG])
1346 continue;
1347
1348 if (q->syss_offset >= 0 &&
1349 q->syss_offset != ddata->offsets[SYSC_SYSSTATUS])
1350 continue;
1351
1352 if (q->revision == ddata->revision ||
1353 (q->revision & q->revision_mask) ==
1354 (ddata->revision & q->revision_mask)) {
1355 ddata->name = q->name;
1356 ddata->cfg.quirks |= q->quirks;
1357 }
1358 }
1359}
1360
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001361static int sysc_clockdomain_init(struct sysc *ddata)
1362{
1363 struct ti_sysc_platform_data *pdata = dev_get_platdata(ddata->dev);
1364 struct clk *fck = NULL, *ick = NULL;
1365 int error;
1366
1367 if (!pdata || !pdata->init_clockdomain)
1368 return 0;
1369
1370 switch (ddata->nr_clocks) {
1371 case 2:
1372 ick = ddata->clocks[SYSC_ICK];
1373 /* fallthrough */
1374 case 1:
1375 fck = ddata->clocks[SYSC_FCK];
1376 break;
1377 case 0:
1378 return 0;
1379 }
1380
1381 error = pdata->init_clockdomain(ddata->dev, fck, ick, &ddata->cookie);
1382 if (!error || error == -ENODEV)
1383 return 0;
1384
1385 return error;
1386}
1387
Tony Lindgrena3e92e72019-03-21 11:00:21 -07001388/*
1389 * Note that pdata->init_module() typically does a reset first. After
1390 * pdata->init_module() is done, PM runtime can be used for the interconnect
1391 * target module.
1392 */
1393static int sysc_legacy_init(struct sysc *ddata)
1394{
1395 struct ti_sysc_platform_data *pdata = dev_get_platdata(ddata->dev);
1396 int error;
1397
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001398 if (!pdata || !pdata->init_module)
Tony Lindgrena3e92e72019-03-21 11:00:21 -07001399 return 0;
1400
1401 error = pdata->init_module(ddata->dev, ddata->mdata, &ddata->cookie);
1402 if (error == -EEXIST)
1403 error = 0;
1404
1405 return error;
1406}
1407
Tony Lindgrenb11c1ea2019-03-21 11:00:21 -07001408/**
1409 * sysc_rstctrl_reset_deassert - deassert rstctrl reset
1410 * @ddata: device driver data
1411 * @reset: reset before deassert
1412 *
1413 * A module can have both OCP softreset control and external rstctrl.
1414 * If more complicated rstctrl resets are needed, please handle these
1415 * directly from the child device driver and map only the module reset
1416 * for the parent interconnect target module device.
1417 *
1418 * Automatic reset of the module on init can be skipped with the
1419 * "ti,no-reset-on-init" device tree property.
1420 */
1421static int sysc_rstctrl_reset_deassert(struct sysc *ddata, bool reset)
1422{
Tony Lindgrenea5a2e42019-05-27 04:51:56 -07001423 int error, val;
Tony Lindgrenb11c1ea2019-03-21 11:00:21 -07001424
1425 if (!ddata->rsts)
1426 return 0;
1427
1428 if (reset) {
1429 error = reset_control_assert(ddata->rsts);
1430 if (error)
1431 return error;
1432 }
1433
Tony Lindgrenea5a2e42019-05-27 04:51:56 -07001434 error = reset_control_deassert(ddata->rsts);
1435 if (error == -EEXIST)
1436 return 0;
1437
1438 error = readx_poll_timeout(reset_control_status, ddata->rsts, val,
1439 val == 0, 100, MAX_MODULE_SOFTRESET_WAIT);
1440
1441 return error;
Tony Lindgrenb11c1ea2019-03-21 11:00:21 -07001442}
1443
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001444/*
1445 * Note that the caller must ensure the interconnect target module is enabled
1446 * before calling reset. Otherwise reset will not complete.
1447 */
Faiz Abbas596e7952018-07-09 22:18:39 +05301448static int sysc_reset(struct sysc *ddata)
1449{
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001450 int sysc_offset, syss_offset, sysc_val, rstval, quirks, error = 0;
1451 u32 sysc_mask, syss_done;
Faiz Abbas596e7952018-07-09 22:18:39 +05301452
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001453 sysc_offset = ddata->offsets[SYSC_SYSCONFIG];
1454 syss_offset = ddata->offsets[SYSC_SYSSTATUS];
1455 quirks = ddata->cfg.quirks;
1456
1457 if (ddata->legacy_mode || sysc_offset < 0 ||
1458 ddata->cap->regbits->srst_shift < 0 ||
Faiz Abbas596e7952018-07-09 22:18:39 +05301459 ddata->cfg.quirks & SYSC_QUIRK_NO_RESET_ON_INIT)
1460 return 0;
1461
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001462 sysc_mask = BIT(ddata->cap->regbits->srst_shift);
Faiz Abbas596e7952018-07-09 22:18:39 +05301463
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001464 if (ddata->cfg.quirks & SYSS_QUIRK_RESETDONE_INVERTED)
1465 syss_done = 0;
1466 else
1467 syss_done = ddata->cfg.syss_mask;
1468
1469 sysc_val = sysc_read_sysconfig(ddata);
1470 sysc_val |= sysc_mask;
1471 sysc_write(ddata, sysc_offset, sysc_val);
Faiz Abbas596e7952018-07-09 22:18:39 +05301472
1473 /* Poll on reset status */
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001474 if (syss_offset >= 0) {
1475 error = readx_poll_timeout(sysc_read_sysstatus, ddata, rstval,
1476 (rstval & ddata->cfg.syss_mask) ==
1477 syss_done,
1478 100, MAX_MODULE_SOFTRESET_WAIT);
Faiz Abbas596e7952018-07-09 22:18:39 +05301479
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001480 } else if (ddata->cfg.quirks & SYSC_QUIRK_RESET_STATUS) {
1481 error = readx_poll_timeout(sysc_read_sysconfig, ddata, rstval,
1482 !(rstval & sysc_mask),
1483 100, MAX_MODULE_SOFTRESET_WAIT);
1484 }
1485
1486 return error;
Faiz Abbas596e7952018-07-09 22:18:39 +05301487}
1488
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001489/*
1490 * At this point the module is configured enough to read the revision but
1491 * module may not be completely configured yet to use PM runtime. Enable
1492 * all clocks directly during init to configure the quirks needed for PM
1493 * runtime based on the revision register.
1494 */
Tony Lindgren566a9b052017-12-15 09:41:19 -08001495static int sysc_init_module(struct sysc *ddata)
1496{
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001497 int error = 0;
1498 bool manage_clocks = true;
Tony Lindgrenb11c1ea2019-03-21 11:00:21 -07001499
Tony Lindgrenea5a2e42019-05-27 04:51:56 -07001500 error = sysc_rstctrl_reset_deassert(ddata, false);
Tony Lindgrenb11c1ea2019-03-21 11:00:21 -07001501 if (error)
1502 return error;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001503
Tony Lindgren386cb762019-03-22 07:49:30 -07001504 if (ddata->cfg.quirks &
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001505 (SYSC_QUIRK_NO_IDLE | SYSC_QUIRK_NO_IDLE_ON_INIT))
1506 manage_clocks = false;
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001507
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001508 error = sysc_clockdomain_init(ddata);
1509 if (error)
1510 return error;
1511
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001512 if (manage_clocks) {
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001513 sysc_clkdm_deny_idle(ddata);
1514
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001515 error = sysc_enable_opt_clocks(ddata);
1516 if (error)
1517 return error;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001518
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001519 error = sysc_enable_main_clocks(ddata);
1520 if (error)
1521 goto err_opt_clocks;
Faiz Abbas596e7952018-07-09 22:18:39 +05301522 }
1523
Tony Lindgrenea5a2e42019-05-27 04:51:56 -07001524 if (!(ddata->cfg.quirks & SYSC_QUIRK_NO_RESET_ON_INIT)) {
1525 error = sysc_rstctrl_reset_deassert(ddata, true);
1526 if (error)
1527 goto err_main_clocks;
1528 }
1529
Tony Lindgren566a9b052017-12-15 09:41:19 -08001530 ddata->revision = sysc_read_revision(ddata);
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001531 sysc_init_revision_quirks(ddata);
1532
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001533 if (ddata->legacy_mode) {
1534 error = sysc_legacy_init(ddata);
1535 if (error)
1536 goto err_main_clocks;
1537 }
1538
1539 if (!ddata->legacy_mode && manage_clocks) {
1540 error = sysc_enable_module(ddata->dev);
1541 if (error)
1542 goto err_main_clocks;
1543 }
Tony Lindgrena3e92e72019-03-21 11:00:21 -07001544
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001545 error = sysc_reset(ddata);
1546 if (error)
1547 dev_err(ddata->dev, "Reset failed with %d\n", error);
1548
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001549 if (!ddata->legacy_mode && manage_clocks)
1550 sysc_disable_module(ddata->dev);
1551
Tony Lindgrena3e92e72019-03-21 11:00:21 -07001552err_main_clocks:
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001553 if (manage_clocks)
1554 sysc_disable_main_clocks(ddata);
1555err_opt_clocks:
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001556 if (manage_clocks) {
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001557 sysc_disable_opt_clocks(ddata);
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001558 sysc_clkdm_allow_idle(ddata);
1559 }
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001560
1561 return error;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001562}
1563
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08001564static int sysc_init_sysc_mask(struct sysc *ddata)
1565{
1566 struct device_node *np = ddata->dev->of_node;
1567 int error;
1568 u32 val;
1569
1570 error = of_property_read_u32(np, "ti,sysc-mask", &val);
1571 if (error)
1572 return 0;
1573
1574 if (val)
1575 ddata->cfg.sysc_val = val & ddata->cap->sysc_mask;
1576 else
1577 ddata->cfg.sysc_val = ddata->cap->sysc_mask;
1578
1579 return 0;
1580}
1581
1582static int sysc_init_idlemode(struct sysc *ddata, u8 *idlemodes,
1583 const char *name)
1584{
1585 struct device_node *np = ddata->dev->of_node;
1586 struct property *prop;
1587 const __be32 *p;
1588 u32 val;
1589
1590 of_property_for_each_u32(np, name, prop, p, val) {
1591 if (val >= SYSC_NR_IDLEMODES) {
1592 dev_err(ddata->dev, "invalid idlemode: %i\n", val);
1593 return -EINVAL;
1594 }
1595 *idlemodes |= (1 << val);
1596 }
1597
1598 return 0;
1599}
1600
1601static int sysc_init_idlemodes(struct sysc *ddata)
1602{
1603 int error;
1604
1605 error = sysc_init_idlemode(ddata, &ddata->cfg.midlemodes,
1606 "ti,sysc-midle");
1607 if (error)
1608 return error;
1609
1610 error = sysc_init_idlemode(ddata, &ddata->cfg.sidlemodes,
1611 "ti,sysc-sidle");
1612 if (error)
1613 return error;
1614
1615 return 0;
1616}
1617
1618/*
1619 * Only some devices on omap4 and later have SYSCONFIG reset done
1620 * bit. We can detect this if there is no SYSSTATUS at all, or the
1621 * SYSTATUS bit 0 is not used. Note that some SYSSTATUS registers
1622 * have multiple bits for the child devices like OHCI and EHCI.
1623 * Depends on SYSC being parsed first.
1624 */
1625static int sysc_init_syss_mask(struct sysc *ddata)
1626{
1627 struct device_node *np = ddata->dev->of_node;
1628 int error;
1629 u32 val;
1630
1631 error = of_property_read_u32(np, "ti,syss-mask", &val);
1632 if (error) {
1633 if ((ddata->cap->type == TI_SYSC_OMAP4 ||
1634 ddata->cap->type == TI_SYSC_OMAP4_TIMER) &&
1635 (ddata->cfg.sysc_val & SYSC_OMAP4_SOFTRESET))
1636 ddata->cfg.quirks |= SYSC_QUIRK_RESET_STATUS;
1637
1638 return 0;
1639 }
1640
1641 if (!(val & 1) && (ddata->cfg.sysc_val & SYSC_OMAP4_SOFTRESET))
1642 ddata->cfg.quirks |= SYSC_QUIRK_RESET_STATUS;
1643
1644 ddata->cfg.syss_mask = val;
1645
1646 return 0;
1647}
1648
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001649/*
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001650 * Many child device drivers need to have fck and opt clocks available
1651 * to get the clock rate for device internal configuration etc.
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001652 */
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001653static int sysc_child_add_named_clock(struct sysc *ddata,
1654 struct device *child,
1655 const char *name)
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001656{
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001657 struct clk *clk;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001658 struct clk_lookup *l;
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001659 int error = 0;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001660
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001661 if (!name)
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001662 return 0;
1663
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001664 clk = clk_get(child, name);
1665 if (!IS_ERR(clk)) {
1666 clk_put(clk);
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001667
1668 return -EEXIST;
1669 }
1670
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001671 clk = clk_get(ddata->dev, name);
1672 if (IS_ERR(clk))
1673 return -ENODEV;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001674
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001675 l = clkdev_create(clk, name, dev_name(child));
1676 if (!l)
1677 error = -ENOMEM;
1678
1679 clk_put(clk);
1680
1681 return error;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001682}
1683
Tony Lindgren09dfe582018-04-16 10:25:52 -07001684static int sysc_child_add_clocks(struct sysc *ddata,
1685 struct device *child)
1686{
1687 int i, error;
1688
1689 for (i = 0; i < ddata->nr_clocks; i++) {
1690 error = sysc_child_add_named_clock(ddata,
1691 child,
1692 ddata->clock_roles[i]);
1693 if (error && error != -EEXIST) {
1694 dev_err(ddata->dev, "could not add child clock %s: %i\n",
1695 ddata->clock_roles[i], error);
1696
1697 return error;
1698 }
1699 }
1700
1701 return 0;
1702}
1703
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001704static struct device_type sysc_device_type = {
1705};
1706
1707static struct sysc *sysc_child_to_parent(struct device *dev)
1708{
1709 struct device *parent = dev->parent;
1710
1711 if (!parent || parent->type != &sysc_device_type)
1712 return NULL;
1713
1714 return dev_get_drvdata(parent);
1715}
1716
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001717static int __maybe_unused sysc_child_runtime_suspend(struct device *dev)
1718{
1719 struct sysc *ddata;
1720 int error;
1721
1722 ddata = sysc_child_to_parent(dev);
1723
1724 error = pm_generic_runtime_suspend(dev);
1725 if (error)
1726 return error;
1727
1728 if (!ddata->enabled)
1729 return 0;
1730
1731 return sysc_runtime_suspend(ddata->dev);
1732}
1733
1734static int __maybe_unused sysc_child_runtime_resume(struct device *dev)
1735{
1736 struct sysc *ddata;
1737 int error;
1738
1739 ddata = sysc_child_to_parent(dev);
1740
1741 if (!ddata->enabled) {
1742 error = sysc_runtime_resume(ddata->dev);
1743 if (error < 0)
1744 dev_err(ddata->dev,
1745 "%s error: %i\n", __func__, error);
1746 }
1747
1748 return pm_generic_runtime_resume(dev);
1749}
1750
1751#ifdef CONFIG_PM_SLEEP
1752static int sysc_child_suspend_noirq(struct device *dev)
1753{
1754 struct sysc *ddata;
1755 int error;
1756
1757 ddata = sysc_child_to_parent(dev);
1758
Tony Lindgrenef55f822018-04-16 10:27:15 -07001759 dev_dbg(ddata->dev, "%s %s\n", __func__,
1760 ddata->name ? ddata->name : "");
1761
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001762 error = pm_generic_suspend_noirq(dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001763 if (error) {
1764 dev_err(dev, "%s error at %i: %i\n",
1765 __func__, __LINE__, error);
1766
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001767 return error;
Tony Lindgrenef55f822018-04-16 10:27:15 -07001768 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001769
1770 if (!pm_runtime_status_suspended(dev)) {
1771 error = pm_generic_runtime_suspend(dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001772 if (error) {
Tony Lindgrenf9490782018-09-28 15:21:50 -07001773 dev_dbg(dev, "%s busy at %i: %i\n",
1774 __func__, __LINE__, error);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001775
Tony Lindgren4f3530f2018-08-08 01:07:06 -07001776 return 0;
Tony Lindgrenef55f822018-04-16 10:27:15 -07001777 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001778
1779 error = sysc_runtime_suspend(ddata->dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001780 if (error) {
1781 dev_err(dev, "%s error at %i: %i\n",
1782 __func__, __LINE__, error);
1783
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001784 return error;
Tony Lindgrenef55f822018-04-16 10:27:15 -07001785 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001786
1787 ddata->child_needs_resume = true;
1788 }
1789
1790 return 0;
1791}
1792
1793static int sysc_child_resume_noirq(struct device *dev)
1794{
1795 struct sysc *ddata;
1796 int error;
1797
1798 ddata = sysc_child_to_parent(dev);
1799
Tony Lindgrenef55f822018-04-16 10:27:15 -07001800 dev_dbg(ddata->dev, "%s %s\n", __func__,
1801 ddata->name ? ddata->name : "");
1802
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001803 if (ddata->child_needs_resume) {
1804 ddata->child_needs_resume = false;
1805
1806 error = sysc_runtime_resume(ddata->dev);
1807 if (error)
1808 dev_err(ddata->dev,
1809 "%s runtime resume error: %i\n",
1810 __func__, error);
1811
1812 error = pm_generic_runtime_resume(dev);
1813 if (error)
1814 dev_err(ddata->dev,
1815 "%s generic runtime resume: %i\n",
1816 __func__, error);
1817 }
1818
1819 return pm_generic_resume_noirq(dev);
1820}
1821#endif
1822
Tony Lindgrenb7182b42019-03-21 11:00:21 -07001823static struct dev_pm_domain sysc_child_pm_domain = {
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001824 .ops = {
1825 SET_RUNTIME_PM_OPS(sysc_child_runtime_suspend,
1826 sysc_child_runtime_resume,
1827 NULL)
1828 USE_PLATFORM_PM_SLEEP_OPS
1829 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(sysc_child_suspend_noirq,
1830 sysc_child_resume_noirq)
1831 }
1832};
1833
1834/**
1835 * sysc_legacy_idle_quirk - handle children in omap_device compatible way
1836 * @ddata: device driver data
1837 * @child: child device driver
1838 *
1839 * Allow idle for child devices as done with _od_runtime_suspend().
1840 * Otherwise many child devices will not idle because of the permanent
1841 * parent usecount set in pm_runtime_irq_safe().
1842 *
1843 * Note that the long term solution is to just modify the child device
1844 * drivers to not set pm_runtime_irq_safe() and then this can be just
1845 * dropped.
1846 */
1847static void sysc_legacy_idle_quirk(struct sysc *ddata, struct device *child)
1848{
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001849 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
1850 dev_pm_domain_set(child, &sysc_child_pm_domain);
1851}
1852
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001853static int sysc_notifier_call(struct notifier_block *nb,
1854 unsigned long event, void *device)
1855{
1856 struct device *dev = device;
1857 struct sysc *ddata;
1858 int error;
1859
1860 ddata = sysc_child_to_parent(dev);
1861 if (!ddata)
1862 return NOTIFY_DONE;
1863
1864 switch (event) {
1865 case BUS_NOTIFY_ADD_DEVICE:
Tony Lindgren09dfe582018-04-16 10:25:52 -07001866 error = sysc_child_add_clocks(ddata, dev);
1867 if (error)
1868 return error;
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001869 sysc_legacy_idle_quirk(ddata, dev);
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001870 break;
1871 default:
1872 break;
1873 }
1874
1875 return NOTIFY_DONE;
1876}
1877
1878static struct notifier_block sysc_nb = {
1879 .notifier_call = sysc_notifier_call,
1880};
1881
Tony Lindgren566a9b052017-12-15 09:41:19 -08001882/* Device tree configured quirks */
1883struct sysc_dts_quirk {
1884 const char *name;
1885 u32 mask;
1886};
1887
1888static const struct sysc_dts_quirk sysc_dts_quirks[] = {
1889 { .name = "ti,no-idle-on-init",
1890 .mask = SYSC_QUIRK_NO_IDLE_ON_INIT, },
1891 { .name = "ti,no-reset-on-init",
1892 .mask = SYSC_QUIRK_NO_RESET_ON_INIT, },
Tony Lindgren386cb762019-03-22 07:49:30 -07001893 { .name = "ti,no-idle",
1894 .mask = SYSC_QUIRK_NO_IDLE, },
Tony Lindgren566a9b052017-12-15 09:41:19 -08001895};
1896
Tony Lindgren4014c082018-12-10 14:11:26 -08001897static void sysc_parse_dts_quirks(struct sysc *ddata, struct device_node *np,
1898 bool is_child)
Tony Lindgren566a9b052017-12-15 09:41:19 -08001899{
Tony Lindgren566a9b052017-12-15 09:41:19 -08001900 const struct property *prop;
Tony Lindgren4014c082018-12-10 14:11:26 -08001901 int i, len;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001902
1903 for (i = 0; i < ARRAY_SIZE(sysc_dts_quirks); i++) {
Tony Lindgren4014c082018-12-10 14:11:26 -08001904 const char *name = sysc_dts_quirks[i].name;
1905
1906 prop = of_get_property(np, name, &len);
Tony Lindgren566a9b052017-12-15 09:41:19 -08001907 if (!prop)
Tony Lindgrend39b6ea2018-02-15 09:18:55 -08001908 continue;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001909
1910 ddata->cfg.quirks |= sysc_dts_quirks[i].mask;
Tony Lindgren4014c082018-12-10 14:11:26 -08001911 if (is_child) {
1912 dev_warn(ddata->dev,
1913 "dts flag should be at module level for %s\n",
1914 name);
1915 }
Tony Lindgren566a9b052017-12-15 09:41:19 -08001916 }
Tony Lindgren4014c082018-12-10 14:11:26 -08001917}
Tony Lindgren566a9b052017-12-15 09:41:19 -08001918
Tony Lindgren4014c082018-12-10 14:11:26 -08001919static int sysc_init_dts_quirks(struct sysc *ddata)
1920{
1921 struct device_node *np = ddata->dev->of_node;
1922 int error;
1923 u32 val;
1924
1925 ddata->legacy_mode = of_get_property(np, "ti,hwmods", NULL);
1926
1927 sysc_parse_dts_quirks(ddata, np, false);
Tony Lindgren566a9b052017-12-15 09:41:19 -08001928 error = of_property_read_u32(np, "ti,sysc-delay-us", &val);
1929 if (!error) {
1930 if (val > 255) {
1931 dev_warn(ddata->dev, "bad ti,sysc-delay-us: %i\n",
1932 val);
1933 }
1934
1935 ddata->cfg.srst_udelay = (u8)val;
1936 }
1937
1938 return 0;
1939}
1940
Tony Lindgren0eecc632017-10-10 14:23:43 -07001941static void sysc_unprepare(struct sysc *ddata)
1942{
1943 int i;
1944
Tony Lindgrenaaa29bb2019-03-21 11:00:21 -07001945 if (!ddata->clocks)
1946 return;
1947
Tony Lindgren0eecc632017-10-10 14:23:43 -07001948 for (i = 0; i < SYSC_MAX_CLOCKS; i++) {
1949 if (!IS_ERR_OR_NULL(ddata->clocks[i]))
1950 clk_unprepare(ddata->clocks[i]);
1951 }
1952}
1953
Tony Lindgren70a65242017-12-15 09:41:09 -08001954/*
1955 * Common sysc register bits found on omap2, also known as type1
1956 */
1957static const struct sysc_regbits sysc_regbits_omap2 = {
1958 .dmadisable_shift = -ENODEV,
1959 .midle_shift = 12,
1960 .sidle_shift = 3,
1961 .clkact_shift = 8,
1962 .emufree_shift = 5,
1963 .enwkup_shift = 2,
1964 .srst_shift = 1,
1965 .autoidle_shift = 0,
1966};
1967
1968static const struct sysc_capabilities sysc_omap2 = {
1969 .type = TI_SYSC_OMAP2,
1970 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY | SYSC_OMAP2_EMUFREE |
1971 SYSC_OMAP2_ENAWAKEUP | SYSC_OMAP2_SOFTRESET |
1972 SYSC_OMAP2_AUTOIDLE,
1973 .regbits = &sysc_regbits_omap2,
1974};
1975
1976/* All omap2 and 3 timers, and timers 1, 2 & 10 on omap 4 and 5 */
1977static const struct sysc_capabilities sysc_omap2_timer = {
1978 .type = TI_SYSC_OMAP2_TIMER,
1979 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY | SYSC_OMAP2_EMUFREE |
1980 SYSC_OMAP2_ENAWAKEUP | SYSC_OMAP2_SOFTRESET |
1981 SYSC_OMAP2_AUTOIDLE,
1982 .regbits = &sysc_regbits_omap2,
1983 .mod_quirks = SYSC_QUIRK_USE_CLOCKACT,
1984};
1985
1986/*
1987 * SHAM2 (SHA1/MD5) sysc found on omap3, a variant of sysc_regbits_omap2
1988 * with different sidle position
1989 */
1990static const struct sysc_regbits sysc_regbits_omap3_sham = {
1991 .dmadisable_shift = -ENODEV,
1992 .midle_shift = -ENODEV,
1993 .sidle_shift = 4,
1994 .clkact_shift = -ENODEV,
1995 .enwkup_shift = -ENODEV,
1996 .srst_shift = 1,
1997 .autoidle_shift = 0,
1998 .emufree_shift = -ENODEV,
1999};
2000
2001static const struct sysc_capabilities sysc_omap3_sham = {
2002 .type = TI_SYSC_OMAP3_SHAM,
2003 .sysc_mask = SYSC_OMAP2_SOFTRESET | SYSC_OMAP2_AUTOIDLE,
2004 .regbits = &sysc_regbits_omap3_sham,
2005};
2006
2007/*
2008 * AES register bits found on omap3 and later, a variant of
2009 * sysc_regbits_omap2 with different sidle position
2010 */
2011static const struct sysc_regbits sysc_regbits_omap3_aes = {
2012 .dmadisable_shift = -ENODEV,
2013 .midle_shift = -ENODEV,
2014 .sidle_shift = 6,
2015 .clkact_shift = -ENODEV,
2016 .enwkup_shift = -ENODEV,
2017 .srst_shift = 1,
2018 .autoidle_shift = 0,
2019 .emufree_shift = -ENODEV,
2020};
2021
2022static const struct sysc_capabilities sysc_omap3_aes = {
2023 .type = TI_SYSC_OMAP3_AES,
2024 .sysc_mask = SYSC_OMAP2_SOFTRESET | SYSC_OMAP2_AUTOIDLE,
2025 .regbits = &sysc_regbits_omap3_aes,
2026};
2027
2028/*
2029 * Common sysc register bits found on omap4, also known as type2
2030 */
2031static const struct sysc_regbits sysc_regbits_omap4 = {
2032 .dmadisable_shift = 16,
2033 .midle_shift = 4,
2034 .sidle_shift = 2,
2035 .clkact_shift = -ENODEV,
2036 .enwkup_shift = -ENODEV,
2037 .emufree_shift = 1,
2038 .srst_shift = 0,
2039 .autoidle_shift = -ENODEV,
2040};
2041
2042static const struct sysc_capabilities sysc_omap4 = {
2043 .type = TI_SYSC_OMAP4,
2044 .sysc_mask = SYSC_OMAP4_DMADISABLE | SYSC_OMAP4_FREEEMU |
2045 SYSC_OMAP4_SOFTRESET,
2046 .regbits = &sysc_regbits_omap4,
2047};
2048
2049static const struct sysc_capabilities sysc_omap4_timer = {
2050 .type = TI_SYSC_OMAP4_TIMER,
2051 .sysc_mask = SYSC_OMAP4_DMADISABLE | SYSC_OMAP4_FREEEMU |
2052 SYSC_OMAP4_SOFTRESET,
2053 .regbits = &sysc_regbits_omap4,
2054};
2055
2056/*
2057 * Common sysc register bits found on omap4, also known as type3
2058 */
2059static const struct sysc_regbits sysc_regbits_omap4_simple = {
2060 .dmadisable_shift = -ENODEV,
2061 .midle_shift = 2,
2062 .sidle_shift = 0,
2063 .clkact_shift = -ENODEV,
2064 .enwkup_shift = -ENODEV,
2065 .srst_shift = -ENODEV,
2066 .emufree_shift = -ENODEV,
2067 .autoidle_shift = -ENODEV,
2068};
2069
2070static const struct sysc_capabilities sysc_omap4_simple = {
2071 .type = TI_SYSC_OMAP4_SIMPLE,
2072 .regbits = &sysc_regbits_omap4_simple,
2073};
2074
2075/*
2076 * SmartReflex sysc found on omap34xx
2077 */
2078static const struct sysc_regbits sysc_regbits_omap34xx_sr = {
2079 .dmadisable_shift = -ENODEV,
2080 .midle_shift = -ENODEV,
2081 .sidle_shift = -ENODEV,
2082 .clkact_shift = 20,
2083 .enwkup_shift = -ENODEV,
2084 .srst_shift = -ENODEV,
2085 .emufree_shift = -ENODEV,
2086 .autoidle_shift = -ENODEV,
2087};
2088
2089static const struct sysc_capabilities sysc_34xx_sr = {
2090 .type = TI_SYSC_OMAP34XX_SR,
2091 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY,
2092 .regbits = &sysc_regbits_omap34xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002093 .mod_quirks = SYSC_QUIRK_USE_CLOCKACT | SYSC_QUIRK_UNCACHED |
2094 SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08002095};
2096
2097/*
2098 * SmartReflex sysc found on omap36xx and later
2099 */
2100static const struct sysc_regbits sysc_regbits_omap36xx_sr = {
2101 .dmadisable_shift = -ENODEV,
2102 .midle_shift = -ENODEV,
2103 .sidle_shift = 24,
2104 .clkact_shift = -ENODEV,
2105 .enwkup_shift = 26,
2106 .srst_shift = -ENODEV,
2107 .emufree_shift = -ENODEV,
2108 .autoidle_shift = -ENODEV,
2109};
2110
2111static const struct sysc_capabilities sysc_36xx_sr = {
2112 .type = TI_SYSC_OMAP36XX_SR,
Tony Lindgren3267c082018-01-22 09:32:53 -08002113 .sysc_mask = SYSC_OMAP3_SR_ENAWAKEUP,
Tony Lindgren70a65242017-12-15 09:41:09 -08002114 .regbits = &sysc_regbits_omap36xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002115 .mod_quirks = SYSC_QUIRK_UNCACHED | SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08002116};
2117
2118static const struct sysc_capabilities sysc_omap4_sr = {
2119 .type = TI_SYSC_OMAP4_SR,
2120 .regbits = &sysc_regbits_omap36xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002121 .mod_quirks = SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08002122};
2123
2124/*
2125 * McASP register bits found on omap4 and later
2126 */
2127static const struct sysc_regbits sysc_regbits_omap4_mcasp = {
2128 .dmadisable_shift = -ENODEV,
2129 .midle_shift = -ENODEV,
2130 .sidle_shift = 0,
2131 .clkact_shift = -ENODEV,
2132 .enwkup_shift = -ENODEV,
2133 .srst_shift = -ENODEV,
2134 .emufree_shift = -ENODEV,
2135 .autoidle_shift = -ENODEV,
2136};
2137
2138static const struct sysc_capabilities sysc_omap4_mcasp = {
2139 .type = TI_SYSC_OMAP4_MCASP,
2140 .regbits = &sysc_regbits_omap4_mcasp,
Tony Lindgren2c63a832018-11-15 14:46:53 -08002141 .mod_quirks = SYSC_QUIRK_OPT_CLKS_NEEDED,
2142};
2143
2144/*
2145 * McASP found on dra7 and later
2146 */
2147static const struct sysc_capabilities sysc_dra7_mcasp = {
2148 .type = TI_SYSC_OMAP4_SIMPLE,
2149 .regbits = &sysc_regbits_omap4_simple,
2150 .mod_quirks = SYSC_QUIRK_OPT_CLKS_NEEDED,
Tony Lindgren70a65242017-12-15 09:41:09 -08002151};
2152
2153/*
2154 * FS USB host found on omap4 and later
2155 */
2156static const struct sysc_regbits sysc_regbits_omap4_usb_host_fs = {
2157 .dmadisable_shift = -ENODEV,
2158 .midle_shift = -ENODEV,
2159 .sidle_shift = 24,
2160 .clkact_shift = -ENODEV,
2161 .enwkup_shift = 26,
2162 .srst_shift = -ENODEV,
2163 .emufree_shift = -ENODEV,
2164 .autoidle_shift = -ENODEV,
2165};
2166
2167static const struct sysc_capabilities sysc_omap4_usb_host_fs = {
2168 .type = TI_SYSC_OMAP4_USB_HOST_FS,
2169 .sysc_mask = SYSC_OMAP2_ENAWAKEUP,
2170 .regbits = &sysc_regbits_omap4_usb_host_fs,
2171};
2172
Faiz Abbas7f35e632018-07-09 22:18:38 +05302173static const struct sysc_regbits sysc_regbits_dra7_mcan = {
2174 .dmadisable_shift = -ENODEV,
2175 .midle_shift = -ENODEV,
2176 .sidle_shift = -ENODEV,
2177 .clkact_shift = -ENODEV,
2178 .enwkup_shift = 4,
2179 .srst_shift = 0,
2180 .emufree_shift = -ENODEV,
2181 .autoidle_shift = -ENODEV,
2182};
2183
2184static const struct sysc_capabilities sysc_dra7_mcan = {
2185 .type = TI_SYSC_DRA7_MCAN,
2186 .sysc_mask = SYSC_DRA7_MCAN_ENAWAKEUP | SYSC_OMAP4_SOFTRESET,
2187 .regbits = &sysc_regbits_dra7_mcan,
Tony Lindgrene0db94f2019-05-27 04:51:53 -07002188 .mod_quirks = SYSS_QUIRK_RESETDONE_INVERTED,
Faiz Abbas7f35e632018-07-09 22:18:38 +05302189};
2190
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002191static int sysc_init_pdata(struct sysc *ddata)
2192{
2193 struct ti_sysc_platform_data *pdata = dev_get_platdata(ddata->dev);
Tony Lindgrena3e92e72019-03-21 11:00:21 -07002194 struct ti_sysc_module_data *mdata;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002195
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07002196 if (!pdata)
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002197 return 0;
2198
Tony Lindgrena3e92e72019-03-21 11:00:21 -07002199 mdata = devm_kzalloc(ddata->dev, sizeof(*mdata), GFP_KERNEL);
2200 if (!mdata)
2201 return -ENOMEM;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002202
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07002203 if (ddata->legacy_mode) {
2204 mdata->name = ddata->legacy_mode;
2205 mdata->module_pa = ddata->module_pa;
2206 mdata->module_size = ddata->module_size;
2207 mdata->offsets = ddata->offsets;
2208 mdata->nr_offsets = SYSC_MAX_REGS;
2209 mdata->cap = ddata->cap;
2210 mdata->cfg = &ddata->cfg;
2211 }
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002212
Tony Lindgrena3e92e72019-03-21 11:00:21 -07002213 ddata->mdata = mdata;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002214
Tony Lindgrena3e92e72019-03-21 11:00:21 -07002215 return 0;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002216}
2217
Tony Lindgren70a65242017-12-15 09:41:09 -08002218static int sysc_init_match(struct sysc *ddata)
2219{
2220 const struct sysc_capabilities *cap;
2221
2222 cap = of_device_get_match_data(ddata->dev);
2223 if (!cap)
2224 return -EINVAL;
2225
2226 ddata->cap = cap;
2227 if (ddata->cap)
2228 ddata->cfg.quirks |= ddata->cap->mod_quirks;
2229
2230 return 0;
2231}
2232
Tony Lindgren76f0f772018-02-23 08:28:45 -08002233static void ti_sysc_idle(struct work_struct *work)
2234{
2235 struct sysc *ddata;
2236
2237 ddata = container_of(work, struct sysc, idle_work.work);
2238
2239 if (pm_runtime_active(ddata->dev))
2240 pm_runtime_put_sync(ddata->dev);
2241}
2242
Tony Lindgrenc4bebea2018-04-16 10:24:54 -07002243static const struct of_device_id sysc_match_table[] = {
2244 { .compatible = "simple-bus", },
2245 { /* sentinel */ },
2246};
2247
Tony Lindgren0eecc632017-10-10 14:23:43 -07002248static int sysc_probe(struct platform_device *pdev)
2249{
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002250 struct ti_sysc_platform_data *pdata = dev_get_platdata(&pdev->dev);
Tony Lindgren0eecc632017-10-10 14:23:43 -07002251 struct sysc *ddata;
2252 int error;
2253
2254 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
2255 if (!ddata)
2256 return -ENOMEM;
2257
2258 ddata->dev = &pdev->dev;
Tony Lindgren566a9b052017-12-15 09:41:19 -08002259 platform_set_drvdata(pdev, ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -07002260
Tony Lindgren70a65242017-12-15 09:41:09 -08002261 error = sysc_init_match(ddata);
2262 if (error)
2263 return error;
2264
Tony Lindgren566a9b052017-12-15 09:41:19 -08002265 error = sysc_init_dts_quirks(ddata);
2266 if (error)
2267 goto unprepare;
2268
Tony Lindgren0eecc632017-10-10 14:23:43 -07002269 error = sysc_map_and_check_registers(ddata);
2270 if (error)
2271 goto unprepare;
2272
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08002273 error = sysc_init_sysc_mask(ddata);
2274 if (error)
2275 goto unprepare;
2276
2277 error = sysc_init_idlemodes(ddata);
2278 if (error)
2279 goto unprepare;
2280
2281 error = sysc_init_syss_mask(ddata);
2282 if (error)
2283 goto unprepare;
2284
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002285 error = sysc_init_pdata(ddata);
2286 if (error)
2287 goto unprepare;
2288
Tony Lindgren42b9c5c2019-03-21 11:00:21 -07002289 sysc_init_early_quirks(ddata);
2290
2291 error = sysc_get_clocks(ddata);
2292 if (error)
2293 return error;
2294
Tony Lindgren50622362018-04-16 10:20:27 -07002295 error = sysc_init_resets(ddata);
2296 if (error)
2297 return error;
Tony Lindgren566a9b052017-12-15 09:41:19 -08002298
2299 error = sysc_init_module(ddata);
2300 if (error)
2301 goto unprepare;
2302
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07002303 pm_runtime_enable(ddata->dev);
Tony Lindgren0eecc632017-10-10 14:23:43 -07002304 error = pm_runtime_get_sync(ddata->dev);
2305 if (error < 0) {
2306 pm_runtime_put_noidle(ddata->dev);
2307 pm_runtime_disable(ddata->dev);
2308 goto unprepare;
2309 }
2310
Tony Lindgren0eecc632017-10-10 14:23:43 -07002311 sysc_show_registers(ddata);
2312
Tony Lindgren2c355ff2018-02-22 13:58:03 -08002313 ddata->dev->type = &sysc_device_type;
Tony Lindgrenc4bebea2018-04-16 10:24:54 -07002314 error = of_platform_populate(ddata->dev->of_node, sysc_match_table,
2315 pdata ? pdata->auxdata : NULL,
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002316 ddata->dev);
Tony Lindgren0eecc632017-10-10 14:23:43 -07002317 if (error)
2318 goto err;
2319
Tony Lindgren76f0f772018-02-23 08:28:45 -08002320 INIT_DELAYED_WORK(&ddata->idle_work, ti_sysc_idle);
2321
2322 /* At least earlycon won't survive without deferred idle */
2323 if (ddata->cfg.quirks & (SYSC_QUIRK_NO_IDLE_ON_INIT |
2324 SYSC_QUIRK_NO_RESET_ON_INIT)) {
2325 schedule_delayed_work(&ddata->idle_work, 3000);
2326 } else {
2327 pm_runtime_put(&pdev->dev);
2328 }
Tony Lindgren0eecc632017-10-10 14:23:43 -07002329
Tony Lindgren50622362018-04-16 10:20:27 -07002330 if (!of_get_available_child_count(ddata->dev->of_node))
Tony Lindgren8383e252019-05-27 04:51:56 -07002331 ddata->disable_on_idle = true;
Tony Lindgren50622362018-04-16 10:20:27 -07002332
Tony Lindgren0eecc632017-10-10 14:23:43 -07002333 return 0;
2334
2335err:
Tony Lindgren0eecc632017-10-10 14:23:43 -07002336 pm_runtime_put_sync(&pdev->dev);
2337 pm_runtime_disable(&pdev->dev);
2338unprepare:
2339 sysc_unprepare(ddata);
2340
2341 return error;
2342}
2343
Tony Lindgren684be5a2017-10-13 10:48:40 -07002344static int sysc_remove(struct platform_device *pdev)
2345{
2346 struct sysc *ddata = platform_get_drvdata(pdev);
2347 int error;
2348
Tony Lindgren76f0f772018-02-23 08:28:45 -08002349 cancel_delayed_work_sync(&ddata->idle_work);
2350
Tony Lindgren684be5a2017-10-13 10:48:40 -07002351 error = pm_runtime_get_sync(ddata->dev);
2352 if (error < 0) {
2353 pm_runtime_put_noidle(ddata->dev);
2354 pm_runtime_disable(ddata->dev);
2355 goto unprepare;
2356 }
2357
2358 of_platform_depopulate(&pdev->dev);
2359
Tony Lindgren684be5a2017-10-13 10:48:40 -07002360 pm_runtime_put_sync(&pdev->dev);
2361 pm_runtime_disable(&pdev->dev);
Tony Lindgren50622362018-04-16 10:20:27 -07002362 reset_control_assert(ddata->rsts);
Tony Lindgren684be5a2017-10-13 10:48:40 -07002363
2364unprepare:
2365 sysc_unprepare(ddata);
2366
2367 return 0;
2368}
2369
Tony Lindgren0eecc632017-10-10 14:23:43 -07002370static const struct of_device_id sysc_match[] = {
Tony Lindgren70a65242017-12-15 09:41:09 -08002371 { .compatible = "ti,sysc-omap2", .data = &sysc_omap2, },
2372 { .compatible = "ti,sysc-omap2-timer", .data = &sysc_omap2_timer, },
2373 { .compatible = "ti,sysc-omap4", .data = &sysc_omap4, },
2374 { .compatible = "ti,sysc-omap4-timer", .data = &sysc_omap4_timer, },
2375 { .compatible = "ti,sysc-omap4-simple", .data = &sysc_omap4_simple, },
2376 { .compatible = "ti,sysc-omap3430-sr", .data = &sysc_34xx_sr, },
2377 { .compatible = "ti,sysc-omap3630-sr", .data = &sysc_36xx_sr, },
2378 { .compatible = "ti,sysc-omap4-sr", .data = &sysc_omap4_sr, },
2379 { .compatible = "ti,sysc-omap3-sham", .data = &sysc_omap3_sham, },
2380 { .compatible = "ti,sysc-omap-aes", .data = &sysc_omap3_aes, },
2381 { .compatible = "ti,sysc-mcasp", .data = &sysc_omap4_mcasp, },
Tony Lindgren2c63a832018-11-15 14:46:53 -08002382 { .compatible = "ti,sysc-dra7-mcasp", .data = &sysc_dra7_mcasp, },
Tony Lindgren70a65242017-12-15 09:41:09 -08002383 { .compatible = "ti,sysc-usb-host-fs",
2384 .data = &sysc_omap4_usb_host_fs, },
Faiz Abbas7f35e632018-07-09 22:18:38 +05302385 { .compatible = "ti,sysc-dra7-mcan", .data = &sysc_dra7_mcan, },
Tony Lindgren0eecc632017-10-10 14:23:43 -07002386 { },
2387};
2388MODULE_DEVICE_TABLE(of, sysc_match);
2389
2390static struct platform_driver sysc_driver = {
2391 .probe = sysc_probe,
Tony Lindgren684be5a2017-10-13 10:48:40 -07002392 .remove = sysc_remove,
Tony Lindgren0eecc632017-10-10 14:23:43 -07002393 .driver = {
2394 .name = "ti-sysc",
2395 .of_match_table = sysc_match,
2396 .pm = &sysc_pm_ops,
2397 },
2398};
Tony Lindgren2c355ff2018-02-22 13:58:03 -08002399
2400static int __init sysc_init(void)
2401{
2402 bus_register_notifier(&platform_bus_type, &sysc_nb);
2403
2404 return platform_driver_register(&sysc_driver);
2405}
2406module_init(sysc_init);
2407
2408static void __exit sysc_exit(void)
2409{
2410 bus_unregister_notifier(&platform_bus_type, &sysc_nb);
2411 platform_driver_unregister(&sysc_driver);
2412}
2413module_exit(sysc_exit);
Tony Lindgren0eecc632017-10-10 14:23:43 -07002414
2415MODULE_DESCRIPTION("TI sysc interconnect target driver");
2416MODULE_LICENSE("GPL v2");