blob: f4a048430cd161f240757368bf0a90ee89a7b002 [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 Lindgren62020f22018-02-22 13:59:44 -080092 bool enabled;
93 bool needs_resume;
Tony Lindgrena885f0f2018-02-22 14:03:48 -080094 bool child_needs_resume;
Tony Lindgren76f0f772018-02-23 08:28:45 -080095 struct delayed_work idle_work;
Tony Lindgren0eecc632017-10-10 14:23:43 -070096};
97
Tony Lindgren4014c082018-12-10 14:11:26 -080098static void sysc_parse_dts_quirks(struct sysc *ddata, struct device_node *np,
99 bool is_child);
100
Tony Lindgrenb7182b42019-03-21 11:00:21 -0700101static void sysc_write(struct sysc *ddata, int offset, u32 value)
Faiz Abbas596e7952018-07-09 22:18:39 +0530102{
Tony Lindgren5aa91292019-05-27 04:51:53 -0700103 if (ddata->cfg.quirks & SYSC_QUIRK_16BIT) {
104 writew_relaxed(value & 0xffff, ddata->module_va + offset);
105
106 /* Only i2c revision has LO and HI register with stride of 4 */
107 if (ddata->offsets[SYSC_REVISION] >= 0 &&
108 offset == ddata->offsets[SYSC_REVISION]) {
109 u16 hi = value >> 16;
110
111 writew_relaxed(hi, ddata->module_va + offset + 4);
112 }
113
114 return;
115 }
116
Faiz Abbas596e7952018-07-09 22:18:39 +0530117 writel_relaxed(value, ddata->module_va + offset);
118}
119
Tony Lindgren566a9b052017-12-15 09:41:19 -0800120static u32 sysc_read(struct sysc *ddata, int offset)
121{
122 if (ddata->cfg.quirks & SYSC_QUIRK_16BIT) {
123 u32 val;
124
125 val = readw_relaxed(ddata->module_va + offset);
Tony Lindgren5aa91292019-05-27 04:51:53 -0700126
127 /* Only i2c revision has LO and HI register with stride of 4 */
128 if (ddata->offsets[SYSC_REVISION] >= 0 &&
129 offset == ddata->offsets[SYSC_REVISION]) {
130 u16 tmp = readw_relaxed(ddata->module_va + offset + 4);
131
132 val |= tmp << 16;
133 }
Tony Lindgren566a9b052017-12-15 09:41:19 -0800134
135 return val;
136 }
137
138 return readl_relaxed(ddata->module_va + offset);
139}
140
Tony Lindgren09dfe582018-04-16 10:25:52 -0700141static bool sysc_opt_clks_needed(struct sysc *ddata)
142{
143 return !!(ddata->cfg.quirks & SYSC_QUIRK_OPT_CLKS_NEEDED);
144}
145
Tony Lindgren0eecc632017-10-10 14:23:43 -0700146static u32 sysc_read_revision(struct sysc *ddata)
147{
Tony Lindgren566a9b052017-12-15 09:41:19 -0800148 int offset = ddata->offsets[SYSC_REVISION];
149
150 if (offset < 0)
151 return 0;
152
153 return sysc_read(ddata, offset);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700154}
155
Tony Lindgrene0db94f2019-05-27 04:51:53 -0700156static u32 sysc_read_sysconfig(struct sysc *ddata)
157{
158 int offset = ddata->offsets[SYSC_SYSCONFIG];
159
160 if (offset < 0)
161 return 0;
162
163 return sysc_read(ddata, offset);
164}
165
166static u32 sysc_read_sysstatus(struct sysc *ddata)
167{
168 int offset = ddata->offsets[SYSC_SYSSTATUS];
169
170 if (offset < 0)
171 return 0;
172
173 return sysc_read(ddata, offset);
174}
175
Tony Lindgrena54275f2019-03-21 11:00:21 -0700176static int sysc_add_named_clock_from_child(struct sysc *ddata,
177 const char *name,
178 const char *optfck_name)
179{
180 struct device_node *np = ddata->dev->of_node;
181 struct device_node *child;
182 struct clk_lookup *cl;
183 struct clk *clock;
184 const char *n;
185
186 if (name)
187 n = name;
188 else
189 n = optfck_name;
190
191 /* Does the clock alias already exist? */
192 clock = of_clk_get_by_name(np, n);
193 if (!IS_ERR(clock)) {
194 clk_put(clock);
195
196 return 0;
197 }
198
199 child = of_get_next_available_child(np, NULL);
200 if (!child)
201 return -ENODEV;
202
203 clock = devm_get_clk_from_child(ddata->dev, child, name);
204 if (IS_ERR(clock))
205 return PTR_ERR(clock);
206
207 /*
208 * Use clkdev_add() instead of clkdev_alloc() to avoid the MAX_DEV_ID
209 * limit for clk_get(). If cl ever needs to be freed, it should be done
210 * with clkdev_drop().
211 */
212 cl = kcalloc(1, sizeof(*cl), GFP_KERNEL);
213 if (!cl)
214 return -ENOMEM;
215
216 cl->con_id = n;
217 cl->dev_id = dev_name(ddata->dev);
218 cl->clk = clock;
219 clkdev_add(cl);
220
221 clk_put(clock);
222
223 return 0;
224}
225
226static int sysc_init_ext_opt_clock(struct sysc *ddata, const char *name)
227{
228 const char *optfck_name;
229 int error, index;
230
231 if (ddata->nr_clocks < SYSC_OPTFCK0)
232 index = SYSC_OPTFCK0;
233 else
234 index = ddata->nr_clocks;
235
236 if (name)
237 optfck_name = name;
238 else
239 optfck_name = clock_names[index];
240
241 error = sysc_add_named_clock_from_child(ddata, name, optfck_name);
242 if (error)
243 return error;
244
245 ddata->clock_roles[index] = optfck_name;
246 ddata->nr_clocks++;
247
248 return 0;
249}
250
Tony Lindgren09dfe582018-04-16 10:25:52 -0700251static int sysc_get_one_clock(struct sysc *ddata, const char *name)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700252{
Tony Lindgren09dfe582018-04-16 10:25:52 -0700253 int error, i, index = -ENODEV;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700254
Tony Lindgren09dfe582018-04-16 10:25:52 -0700255 if (!strncmp(clock_names[SYSC_FCK], name, 3))
256 index = SYSC_FCK;
257 else if (!strncmp(clock_names[SYSC_ICK], name, 3))
258 index = SYSC_ICK;
259
260 if (index < 0) {
261 for (i = SYSC_OPTFCK0; i < SYSC_MAX_CLOCKS; i++) {
Tony Lindgrenc97c8622018-05-18 07:54:44 -0700262 if (!ddata->clocks[i]) {
Tony Lindgren09dfe582018-04-16 10:25:52 -0700263 index = i;
264 break;
265 }
266 }
Tony Lindgren0eecc632017-10-10 14:23:43 -0700267 }
Tony Lindgren09dfe582018-04-16 10:25:52 -0700268
269 if (index < 0) {
270 dev_err(ddata->dev, "clock %s not added\n", name);
271 return index;
272 }
Tony Lindgren0eecc632017-10-10 14:23:43 -0700273
274 ddata->clocks[index] = devm_clk_get(ddata->dev, name);
275 if (IS_ERR(ddata->clocks[index])) {
276 if (PTR_ERR(ddata->clocks[index]) == -ENOENT)
277 return 0;
278
279 dev_err(ddata->dev, "clock get error for %s: %li\n",
280 name, PTR_ERR(ddata->clocks[index]));
281
282 return PTR_ERR(ddata->clocks[index]);
283 }
284
285 error = clk_prepare(ddata->clocks[index]);
286 if (error) {
287 dev_err(ddata->dev, "clock prepare error for %s: %i\n",
288 name, error);
289
290 return error;
291 }
292
293 return 0;
294}
295
296static int sysc_get_clocks(struct sysc *ddata)
297{
Tony Lindgren09dfe582018-04-16 10:25:52 -0700298 struct device_node *np = ddata->dev->of_node;
299 struct property *prop;
300 const char *name;
301 int nr_fck = 0, nr_ick = 0, i, error = 0;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700302
Kees Cook20749052018-06-18 10:45:49 -0700303 ddata->clock_roles = devm_kcalloc(ddata->dev,
Tony Lindgren09dfe582018-04-16 10:25:52 -0700304 SYSC_MAX_CLOCKS,
Kees Cook20749052018-06-18 10:45:49 -0700305 sizeof(*ddata->clock_roles),
Tony Lindgren09dfe582018-04-16 10:25:52 -0700306 GFP_KERNEL);
307 if (!ddata->clock_roles)
308 return -ENOMEM;
309
310 of_property_for_each_string(np, "clock-names", prop, name) {
311 if (!strncmp(clock_names[SYSC_FCK], name, 3))
312 nr_fck++;
313 if (!strncmp(clock_names[SYSC_ICK], name, 3))
314 nr_ick++;
315 ddata->clock_roles[ddata->nr_clocks] = name;
316 ddata->nr_clocks++;
317 }
318
319 if (ddata->nr_clocks < 1)
320 return 0;
321
Tony Lindgrena54275f2019-03-21 11:00:21 -0700322 if ((ddata->cfg.quirks & SYSC_QUIRK_EXT_OPT_CLOCK)) {
323 error = sysc_init_ext_opt_clock(ddata, NULL);
324 if (error)
325 return error;
326 }
327
Tony Lindgren09dfe582018-04-16 10:25:52 -0700328 if (ddata->nr_clocks > SYSC_MAX_CLOCKS) {
329 dev_err(ddata->dev, "too many clocks for %pOF\n", np);
330
331 return -EINVAL;
332 }
333
334 if (nr_fck > 1 || nr_ick > 1) {
335 dev_err(ddata->dev, "max one fck and ick for %pOF\n", np);
336
337 return -EINVAL;
338 }
339
Kees Cook20749052018-06-18 10:45:49 -0700340 ddata->clocks = devm_kcalloc(ddata->dev,
341 ddata->nr_clocks, sizeof(*ddata->clocks),
Tony Lindgren09dfe582018-04-16 10:25:52 -0700342 GFP_KERNEL);
343 if (!ddata->clocks)
344 return -ENOMEM;
345
Tony Lindgren7b4f8ac2018-11-15 14:46:52 -0800346 for (i = 0; i < SYSC_MAX_CLOCKS; i++) {
347 const char *name = ddata->clock_roles[i];
348
349 if (!name)
350 continue;
351
352 error = sysc_get_one_clock(ddata, name);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700353 if (error && error != -ENOENT)
354 return error;
355 }
356
357 return 0;
358}
359
Tony Lindgrend8789702019-03-21 11:00:21 -0700360static int sysc_enable_main_clocks(struct sysc *ddata)
361{
362 struct clk *clock;
363 int i, error;
364
365 if (!ddata->clocks)
366 return 0;
367
368 for (i = 0; i < SYSC_OPTFCK0; i++) {
369 clock = ddata->clocks[i];
370
371 /* Main clocks may not have ick */
372 if (IS_ERR_OR_NULL(clock))
373 continue;
374
375 error = clk_enable(clock);
376 if (error)
377 goto err_disable;
378 }
379
380 return 0;
381
382err_disable:
383 for (i--; i >= 0; i--) {
384 clock = ddata->clocks[i];
385
386 /* Main clocks may not have ick */
387 if (IS_ERR_OR_NULL(clock))
388 continue;
389
390 clk_disable(clock);
391 }
392
393 return error;
394}
395
396static void sysc_disable_main_clocks(struct sysc *ddata)
397{
398 struct clk *clock;
399 int i;
400
401 if (!ddata->clocks)
402 return;
403
404 for (i = 0; i < SYSC_OPTFCK0; i++) {
405 clock = ddata->clocks[i];
406 if (IS_ERR_OR_NULL(clock))
407 continue;
408
409 clk_disable(clock);
410 }
411}
412
413static int sysc_enable_opt_clocks(struct sysc *ddata)
414{
415 struct clk *clock;
416 int i, error;
417
418 if (!ddata->clocks)
419 return 0;
420
421 for (i = SYSC_OPTFCK0; i < SYSC_MAX_CLOCKS; i++) {
422 clock = ddata->clocks[i];
423
424 /* Assume no holes for opt clocks */
425 if (IS_ERR_OR_NULL(clock))
426 return 0;
427
428 error = clk_enable(clock);
429 if (error)
430 goto err_disable;
431 }
432
433 return 0;
434
435err_disable:
436 for (i--; i >= 0; i--) {
437 clock = ddata->clocks[i];
438 if (IS_ERR_OR_NULL(clock))
439 continue;
440
441 clk_disable(clock);
442 }
443
444 return error;
445}
446
447static void sysc_disable_opt_clocks(struct sysc *ddata)
448{
449 struct clk *clock;
450 int i;
451
452 if (!ddata->clocks)
453 return;
454
455 for (i = SYSC_OPTFCK0; i < SYSC_MAX_CLOCKS; i++) {
456 clock = ddata->clocks[i];
457
458 /* Assume no holes for opt clocks */
459 if (IS_ERR_OR_NULL(clock))
460 return;
461
462 clk_disable(clock);
463 }
464}
465
Tony Lindgren2b2f7de2019-05-27 04:51:53 -0700466static void sysc_clkdm_deny_idle(struct sysc *ddata)
467{
468 struct ti_sysc_platform_data *pdata;
469
470 if (ddata->legacy_mode)
471 return;
472
473 pdata = dev_get_platdata(ddata->dev);
474 if (pdata && pdata->clkdm_deny_idle)
475 pdata->clkdm_deny_idle(ddata->dev, &ddata->cookie);
476}
477
478static void sysc_clkdm_allow_idle(struct sysc *ddata)
479{
480 struct ti_sysc_platform_data *pdata;
481
482 if (ddata->legacy_mode)
483 return;
484
485 pdata = dev_get_platdata(ddata->dev);
486 if (pdata && pdata->clkdm_allow_idle)
487 pdata->clkdm_allow_idle(ddata->dev, &ddata->cookie);
488}
489
Tony Lindgren0eecc632017-10-10 14:23:43 -0700490/**
Tony Lindgrenb11c1ea2019-03-21 11:00:21 -0700491 * sysc_init_resets - init rstctrl reset line if configured
Tony Lindgren50622362018-04-16 10:20:27 -0700492 * @ddata: device driver data
493 *
Tony Lindgrenb11c1ea2019-03-21 11:00:21 -0700494 * See sysc_rstctrl_reset_deassert().
Tony Lindgren50622362018-04-16 10:20:27 -0700495 */
496static int sysc_init_resets(struct sysc *ddata)
497{
Tony Lindgren50622362018-04-16 10:20:27 -0700498 ddata->rsts =
499 devm_reset_control_array_get_optional_exclusive(ddata->dev);
500 if (IS_ERR(ddata->rsts))
501 return PTR_ERR(ddata->rsts);
502
Tony Lindgren50622362018-04-16 10:20:27 -0700503 return 0;
504}
505
506/**
Tony Lindgren0eecc632017-10-10 14:23:43 -0700507 * sysc_parse_and_check_child_range - parses module IO region from ranges
508 * @ddata: device driver data
509 *
510 * In general we only need rev, syss, and sysc registers and not the whole
511 * module range. But we do want the offsets for these registers from the
512 * module base. This allows us to check them against the legacy hwmod
513 * platform data. Let's also check the ranges are configured properly.
514 */
515static int sysc_parse_and_check_child_range(struct sysc *ddata)
516{
517 struct device_node *np = ddata->dev->of_node;
518 const __be32 *ranges;
519 u32 nr_addr, nr_size;
520 int len, error;
521
522 ranges = of_get_property(np, "ranges", &len);
523 if (!ranges) {
524 dev_err(ddata->dev, "missing ranges for %pOF\n", np);
525
526 return -ENOENT;
527 }
528
529 len /= sizeof(*ranges);
530
531 if (len < 3) {
532 dev_err(ddata->dev, "incomplete ranges for %pOF\n", np);
533
534 return -EINVAL;
535 }
536
537 error = of_property_read_u32(np, "#address-cells", &nr_addr);
538 if (error)
539 return -ENOENT;
540
541 error = of_property_read_u32(np, "#size-cells", &nr_size);
542 if (error)
543 return -ENOENT;
544
545 if (nr_addr != 1 || nr_size != 1) {
546 dev_err(ddata->dev, "invalid ranges for %pOF\n", np);
547
548 return -EINVAL;
549 }
550
551 ranges++;
552 ddata->module_pa = of_translate_address(np, ranges++);
553 ddata->module_size = be32_to_cpup(ranges);
554
Tony Lindgren0eecc632017-10-10 14:23:43 -0700555 return 0;
556}
557
Tony Lindgren3bb37c82018-02-22 14:05:14 -0800558static struct device_node *stdout_path;
559
560static void sysc_init_stdout_path(struct sysc *ddata)
561{
562 struct device_node *np = NULL;
563 const char *uart;
564
565 if (IS_ERR(stdout_path))
566 return;
567
568 if (stdout_path)
569 return;
570
571 np = of_find_node_by_path("/chosen");
572 if (!np)
573 goto err;
574
575 uart = of_get_property(np, "stdout-path", NULL);
576 if (!uart)
577 goto err;
578
579 np = of_find_node_by_path(uart);
580 if (!np)
581 goto err;
582
583 stdout_path = np;
584
585 return;
586
587err:
588 stdout_path = ERR_PTR(-ENODEV);
589}
590
591static void sysc_check_quirk_stdout(struct sysc *ddata,
592 struct device_node *np)
593{
594 sysc_init_stdout_path(ddata);
595 if (np != stdout_path)
596 return;
597
598 ddata->cfg.quirks |= SYSC_QUIRK_NO_IDLE_ON_INIT |
599 SYSC_QUIRK_NO_RESET_ON_INIT;
600}
601
Tony Lindgren0eecc632017-10-10 14:23:43 -0700602/**
603 * sysc_check_one_child - check child configuration
604 * @ddata: device driver data
605 * @np: child device node
606 *
607 * Let's avoid messy situations where we have new interconnect target
608 * node but children have "ti,hwmods". These belong to the interconnect
609 * target node and are managed by this driver.
610 */
611static int sysc_check_one_child(struct sysc *ddata,
612 struct device_node *np)
613{
614 const char *name;
615
616 name = of_get_property(np, "ti,hwmods", NULL);
617 if (name)
618 dev_warn(ddata->dev, "really a child ti,hwmods property?");
619
Tony Lindgren3bb37c82018-02-22 14:05:14 -0800620 sysc_check_quirk_stdout(ddata, np);
Tony Lindgren4014c082018-12-10 14:11:26 -0800621 sysc_parse_dts_quirks(ddata, np, true);
Tony Lindgren3bb37c82018-02-22 14:05:14 -0800622
Tony Lindgren0eecc632017-10-10 14:23:43 -0700623 return 0;
624}
625
626static int sysc_check_children(struct sysc *ddata)
627{
628 struct device_node *child;
629 int error;
630
631 for_each_child_of_node(ddata->dev->of_node, child) {
632 error = sysc_check_one_child(ddata, child);
633 if (error)
634 return error;
635 }
636
637 return 0;
638}
639
Tony Lindgrena7199e22017-12-15 09:41:14 -0800640/*
641 * So far only I2C uses 16-bit read access with clockactivity with revision
642 * in two registers with stride of 4. We can detect this based on the rev
643 * register size to configure things far enough to be able to properly read
644 * the revision register.
645 */
646static void sysc_check_quirk_16bit(struct sysc *ddata, struct resource *res)
647{
Tony Lindgrendd57ac12018-02-22 14:09:57 -0800648 if (resource_size(res) == 8)
Tony Lindgrena7199e22017-12-15 09:41:14 -0800649 ddata->cfg.quirks |= SYSC_QUIRK_16BIT | SYSC_QUIRK_USE_CLOCKACT;
Tony Lindgrena7199e22017-12-15 09:41:14 -0800650}
651
Tony Lindgren0eecc632017-10-10 14:23:43 -0700652/**
653 * sysc_parse_one - parses the interconnect target module registers
654 * @ddata: device driver data
655 * @reg: register to parse
656 */
657static int sysc_parse_one(struct sysc *ddata, enum sysc_registers reg)
658{
659 struct resource *res;
660 const char *name;
661
662 switch (reg) {
663 case SYSC_REVISION:
664 case SYSC_SYSCONFIG:
665 case SYSC_SYSSTATUS:
666 name = reg_names[reg];
667 break;
668 default:
669 return -EINVAL;
670 }
671
672 res = platform_get_resource_byname(to_platform_device(ddata->dev),
673 IORESOURCE_MEM, name);
674 if (!res) {
Tony Lindgren0eecc632017-10-10 14:23:43 -0700675 ddata->offsets[reg] = -ENODEV;
676
677 return 0;
678 }
679
680 ddata->offsets[reg] = res->start - ddata->module_pa;
Tony Lindgrena7199e22017-12-15 09:41:14 -0800681 if (reg == SYSC_REVISION)
682 sysc_check_quirk_16bit(ddata, res);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700683
684 return 0;
685}
686
687static int sysc_parse_registers(struct sysc *ddata)
688{
689 int i, error;
690
691 for (i = 0; i < SYSC_MAX_REGS; i++) {
692 error = sysc_parse_one(ddata, i);
693 if (error)
694 return error;
695 }
696
697 return 0;
698}
699
700/**
701 * sysc_check_registers - check for misconfigured register overlaps
702 * @ddata: device driver data
703 */
704static int sysc_check_registers(struct sysc *ddata)
705{
706 int i, j, nr_regs = 0, nr_matches = 0;
707
708 for (i = 0; i < SYSC_MAX_REGS; i++) {
709 if (ddata->offsets[i] < 0)
710 continue;
711
712 if (ddata->offsets[i] > (ddata->module_size - 4)) {
713 dev_err(ddata->dev, "register outside module range");
714
715 return -EINVAL;
716 }
717
718 for (j = 0; j < SYSC_MAX_REGS; j++) {
719 if (ddata->offsets[j] < 0)
720 continue;
721
722 if (ddata->offsets[i] == ddata->offsets[j])
723 nr_matches++;
724 }
725 nr_regs++;
726 }
727
Tony Lindgren0eecc632017-10-10 14:23:43 -0700728 if (nr_matches > nr_regs) {
729 dev_err(ddata->dev, "overlapping registers: (%i/%i)",
730 nr_regs, nr_matches);
731
732 return -EINVAL;
733 }
734
735 return 0;
736}
737
738/**
739 * syc_ioremap - ioremap register space for the interconnect target module
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700740 * @ddata: device driver data
Tony Lindgren0eecc632017-10-10 14:23:43 -0700741 *
742 * Note that the interconnect target module registers can be anywhere
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700743 * within the interconnect target module range. For example, SGX has
744 * them at offset 0x1fc00 in the 32MB module address space. And cpsw
745 * has them at offset 0x1200 in the CPSW_WR child. Usually the
746 * the interconnect target module registers are at the beginning of
747 * the module range though.
Tony Lindgren0eecc632017-10-10 14:23:43 -0700748 */
749static int sysc_ioremap(struct sysc *ddata)
750{
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700751 int size;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700752
Tony Lindgrene4f50c82019-05-01 14:24:57 -0700753 if (ddata->offsets[SYSC_REVISION] < 0 &&
754 ddata->offsets[SYSC_SYSCONFIG] < 0 &&
755 ddata->offsets[SYSC_SYSSTATUS] < 0) {
756 size = ddata->module_size;
757 } else {
758 size = max3(ddata->offsets[SYSC_REVISION],
759 ddata->offsets[SYSC_SYSCONFIG],
760 ddata->offsets[SYSC_SYSSTATUS]);
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700761
Tony Lindgrene4f50c82019-05-01 14:24:57 -0700762 if ((size + sizeof(u32)) > ddata->module_size)
763 return -EINVAL;
764 }
Tony Lindgren0eecc632017-10-10 14:23:43 -0700765
Tony Lindgren0eecc632017-10-10 14:23:43 -0700766 ddata->module_va = devm_ioremap(ddata->dev,
767 ddata->module_pa,
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700768 size + sizeof(u32));
Tony Lindgren0eecc632017-10-10 14:23:43 -0700769 if (!ddata->module_va)
770 return -EIO;
771
772 return 0;
773}
774
775/**
776 * sysc_map_and_check_registers - ioremap and check device registers
777 * @ddata: device driver data
778 */
779static int sysc_map_and_check_registers(struct sysc *ddata)
780{
781 int error;
782
783 error = sysc_parse_and_check_child_range(ddata);
784 if (error)
785 return error;
786
787 error = sysc_check_children(ddata);
788 if (error)
789 return error;
790
791 error = sysc_parse_registers(ddata);
792 if (error)
793 return error;
794
795 error = sysc_ioremap(ddata);
796 if (error)
797 return error;
798
799 error = sysc_check_registers(ddata);
800 if (error)
801 return error;
802
803 return 0;
804}
805
806/**
807 * sysc_show_rev - read and show interconnect target module revision
808 * @bufp: buffer to print the information to
809 * @ddata: device driver data
810 */
811static int sysc_show_rev(char *bufp, struct sysc *ddata)
812{
Tony Lindgren566a9b052017-12-15 09:41:19 -0800813 int len;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700814
815 if (ddata->offsets[SYSC_REVISION] < 0)
816 return sprintf(bufp, ":NA");
817
Tony Lindgren566a9b052017-12-15 09:41:19 -0800818 len = sprintf(bufp, ":%08x", ddata->revision);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700819
820 return len;
821}
822
823static int sysc_show_reg(struct sysc *ddata,
824 char *bufp, enum sysc_registers reg)
825{
826 if (ddata->offsets[reg] < 0)
827 return sprintf(bufp, ":NA");
828
829 return sprintf(bufp, ":%x", ddata->offsets[reg]);
830}
831
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800832static int sysc_show_name(char *bufp, struct sysc *ddata)
833{
834 if (!ddata->name)
835 return 0;
836
837 return sprintf(bufp, ":%s", ddata->name);
838}
839
Tony Lindgren0eecc632017-10-10 14:23:43 -0700840/**
841 * sysc_show_registers - show information about interconnect target module
842 * @ddata: device driver data
843 */
844static void sysc_show_registers(struct sysc *ddata)
845{
846 char buf[128];
847 char *bufp = buf;
848 int i;
849
850 for (i = 0; i < SYSC_MAX_REGS; i++)
851 bufp += sysc_show_reg(ddata, bufp, i);
852
853 bufp += sysc_show_rev(bufp, ddata);
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800854 bufp += sysc_show_name(bufp, ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700855
856 dev_dbg(ddata->dev, "%llx:%x%s\n",
857 ddata->module_pa, ddata->module_size,
858 buf);
859}
860
Roger Quadrosd59b6052019-04-08 12:52:39 +0300861#define SYSC_IDLE_MASK (SYSC_NR_IDLEMODES - 1)
862
Tony Lindgren2b2f7de2019-05-27 04:51:53 -0700863/* Caller needs to manage sysc_clkdm_deny_idle() and sysc_clkdm_allow_idle() */
Roger Quadrosd59b6052019-04-08 12:52:39 +0300864static int sysc_enable_module(struct device *dev)
865{
866 struct sysc *ddata;
867 const struct sysc_regbits *regbits;
868 u32 reg, idlemodes, best_mode;
869
870 ddata = dev_get_drvdata(dev);
871 if (ddata->offsets[SYSC_SYSCONFIG] == -ENODEV)
872 return 0;
873
Roger Quadrosd59b6052019-04-08 12:52:39 +0300874 regbits = ddata->cap->regbits;
875 reg = sysc_read(ddata, ddata->offsets[SYSC_SYSCONFIG]);
876
877 /* Set SIDLE mode */
878 idlemodes = ddata->cfg.sidlemodes;
879 if (!idlemodes || regbits->sidle_shift < 0)
880 goto set_midle;
881
882 best_mode = fls(ddata->cfg.sidlemodes) - 1;
883 if (best_mode > SYSC_IDLE_MASK) {
884 dev_err(dev, "%s: invalid sidlemode\n", __func__);
885 return -EINVAL;
886 }
887
888 reg &= ~(SYSC_IDLE_MASK << regbits->sidle_shift);
889 reg |= best_mode << regbits->sidle_shift;
890 sysc_write(ddata, ddata->offsets[SYSC_SYSCONFIG], reg);
891
892set_midle:
893 /* Set MIDLE mode */
894 idlemodes = ddata->cfg.midlemodes;
895 if (!idlemodes || regbits->midle_shift < 0)
896 return 0;
897
898 best_mode = fls(ddata->cfg.midlemodes) - 1;
899 if (best_mode > SYSC_IDLE_MASK) {
900 dev_err(dev, "%s: invalid midlemode\n", __func__);
901 return -EINVAL;
902 }
903
904 reg &= ~(SYSC_IDLE_MASK << regbits->midle_shift);
905 reg |= best_mode << regbits->midle_shift;
906 sysc_write(ddata, ddata->offsets[SYSC_SYSCONFIG], reg);
907
908 return 0;
909}
910
911static int sysc_best_idle_mode(u32 idlemodes, u32 *best_mode)
912{
913 if (idlemodes & BIT(SYSC_IDLE_SMART_WKUP))
914 *best_mode = SYSC_IDLE_SMART_WKUP;
915 else if (idlemodes & BIT(SYSC_IDLE_SMART))
916 *best_mode = SYSC_IDLE_SMART;
917 else if (idlemodes & SYSC_IDLE_FORCE)
918 *best_mode = SYSC_IDLE_FORCE;
919 else
920 return -EINVAL;
921
922 return 0;
923}
924
Tony Lindgren2b2f7de2019-05-27 04:51:53 -0700925/* Caller needs to manage sysc_clkdm_deny_idle() and sysc_clkdm_allow_idle() */
Roger Quadrosd59b6052019-04-08 12:52:39 +0300926static int sysc_disable_module(struct device *dev)
927{
928 struct sysc *ddata;
929 const struct sysc_regbits *regbits;
930 u32 reg, idlemodes, best_mode;
931 int ret;
932
933 ddata = dev_get_drvdata(dev);
934 if (ddata->offsets[SYSC_SYSCONFIG] == -ENODEV)
935 return 0;
936
Roger Quadrosd59b6052019-04-08 12:52:39 +0300937 regbits = ddata->cap->regbits;
938 reg = sysc_read(ddata, ddata->offsets[SYSC_SYSCONFIG]);
939
940 /* Set MIDLE mode */
941 idlemodes = ddata->cfg.midlemodes;
942 if (!idlemodes || regbits->midle_shift < 0)
943 goto set_sidle;
944
945 ret = sysc_best_idle_mode(idlemodes, &best_mode);
946 if (ret) {
947 dev_err(dev, "%s: invalid midlemode\n", __func__);
948 return ret;
949 }
950
951 reg &= ~(SYSC_IDLE_MASK << regbits->midle_shift);
952 reg |= best_mode << regbits->midle_shift;
953 sysc_write(ddata, ddata->offsets[SYSC_SYSCONFIG], reg);
954
955set_sidle:
956 /* Set SIDLE mode */
957 idlemodes = ddata->cfg.sidlemodes;
958 if (!idlemodes || regbits->sidle_shift < 0)
959 return 0;
960
961 ret = sysc_best_idle_mode(idlemodes, &best_mode);
962 if (ret) {
963 dev_err(dev, "%s: invalid sidlemode\n", __func__);
964 return ret;
965 }
966
967 reg &= ~(SYSC_IDLE_MASK << regbits->sidle_shift);
968 reg |= best_mode << regbits->sidle_shift;
969 sysc_write(ddata, ddata->offsets[SYSC_SYSCONFIG], reg);
970
971 return 0;
972}
973
Tony Lindgrenff437282019-03-21 11:00:21 -0700974static int __maybe_unused sysc_runtime_suspend_legacy(struct device *dev,
975 struct sysc *ddata)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700976{
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800977 struct ti_sysc_platform_data *pdata;
Tony Lindgrenff437282019-03-21 11:00:21 -0700978 int error;
979
980 pdata = dev_get_platdata(ddata->dev);
981 if (!pdata)
982 return 0;
983
984 if (!pdata->idle_module)
985 return -ENODEV;
986
987 error = pdata->idle_module(dev, &ddata->cookie);
988 if (error)
989 dev_err(dev, "%s: could not idle: %i\n",
990 __func__, error);
991
992 return 0;
993}
994
995static int __maybe_unused sysc_runtime_resume_legacy(struct device *dev,
996 struct sysc *ddata)
997{
998 struct ti_sysc_platform_data *pdata;
999 int error;
1000
1001 pdata = dev_get_platdata(ddata->dev);
1002 if (!pdata)
1003 return 0;
1004
1005 if (!pdata->enable_module)
1006 return -ENODEV;
1007
1008 error = pdata->enable_module(dev, &ddata->cookie);
1009 if (error)
1010 dev_err(dev, "%s: could not enable: %i\n",
1011 __func__, error);
1012
1013 return 0;
1014}
1015
1016static int __maybe_unused sysc_runtime_suspend(struct device *dev)
1017{
Tony Lindgren0eecc632017-10-10 14:23:43 -07001018 struct sysc *ddata;
Tony Lindgrend8789702019-03-21 11:00:21 -07001019 int error = 0;
Tony Lindgren0eecc632017-10-10 14:23:43 -07001020
1021 ddata = dev_get_drvdata(dev);
1022
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001023 if (!ddata->enabled)
Tony Lindgren0eecc632017-10-10 14:23:43 -07001024 return 0;
1025
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001026 sysc_clkdm_deny_idle(ddata);
1027
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001028 if (ddata->legacy_mode) {
Tony Lindgrenff437282019-03-21 11:00:21 -07001029 error = sysc_runtime_suspend_legacy(dev, ddata);
Tony Lindgren93de83a2019-03-21 11:00:21 -07001030 if (error)
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001031 goto err_allow_idle;
Roger Quadrosd59b6052019-04-08 12:52:39 +03001032 } else {
1033 error = sysc_disable_module(dev);
1034 if (error)
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001035 goto err_allow_idle;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001036 }
1037
Tony Lindgrend8789702019-03-21 11:00:21 -07001038 sysc_disable_main_clocks(ddata);
Tony Lindgren09dfe582018-04-16 10:25:52 -07001039
Tony Lindgrend8789702019-03-21 11:00:21 -07001040 if (sysc_opt_clks_needed(ddata))
1041 sysc_disable_opt_clocks(ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001042
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001043 ddata->enabled = false;
1044
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001045err_allow_idle:
1046 sysc_clkdm_allow_idle(ddata);
1047
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001048 return error;
Tony Lindgren0eecc632017-10-10 14:23:43 -07001049}
1050
Arnd Bergmanna4a5d492017-10-13 11:25:32 +02001051static int __maybe_unused sysc_runtime_resume(struct device *dev)
Tony Lindgren0eecc632017-10-10 14:23:43 -07001052{
1053 struct sysc *ddata;
Tony Lindgrend8789702019-03-21 11:00:21 -07001054 int error = 0;
Tony Lindgren0eecc632017-10-10 14:23:43 -07001055
1056 ddata = dev_get_drvdata(dev);
1057
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001058 if (ddata->enabled)
Tony Lindgren0eecc632017-10-10 14:23:43 -07001059 return 0;
1060
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001061 sysc_clkdm_deny_idle(ddata);
1062
Tony Lindgrend8789702019-03-21 11:00:21 -07001063 if (sysc_opt_clks_needed(ddata)) {
1064 error = sysc_enable_opt_clocks(ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001065 if (error)
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001066 goto err_allow_idle;
Tony Lindgren0eecc632017-10-10 14:23:43 -07001067 }
1068
Tony Lindgrend8789702019-03-21 11:00:21 -07001069 error = sysc_enable_main_clocks(ddata);
1070 if (error)
Tony Lindgren93de83a2019-03-21 11:00:21 -07001071 goto err_opt_clocks;
1072
1073 if (ddata->legacy_mode) {
1074 error = sysc_runtime_resume_legacy(dev, ddata);
1075 if (error)
1076 goto err_main_clocks;
Roger Quadrosd59b6052019-04-08 12:52:39 +03001077 } else {
1078 error = sysc_enable_module(dev);
1079 if (error)
1080 goto err_main_clocks;
Tony Lindgren93de83a2019-03-21 11:00:21 -07001081 }
Tony Lindgrend8789702019-03-21 11:00:21 -07001082
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001083 ddata->enabled = true;
1084
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001085 sysc_clkdm_allow_idle(ddata);
1086
Tony Lindgrend8789702019-03-21 11:00:21 -07001087 return 0;
1088
1089err_main_clocks:
Tony Lindgren93de83a2019-03-21 11:00:21 -07001090 sysc_disable_main_clocks(ddata);
1091err_opt_clocks:
Tony Lindgrend8789702019-03-21 11:00:21 -07001092 if (sysc_opt_clks_needed(ddata))
1093 sysc_disable_opt_clocks(ddata);
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001094err_allow_idle:
1095 sysc_clkdm_allow_idle(ddata);
Tony Lindgrend8789702019-03-21 11:00:21 -07001096
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001097 return error;
Tony Lindgren0eecc632017-10-10 14:23:43 -07001098}
1099
Tony Lindgrenf5e802032018-10-01 09:33:28 -07001100static int __maybe_unused sysc_noirq_suspend(struct device *dev)
Tony Lindgren62020f22018-02-22 13:59:44 -08001101{
1102 struct sysc *ddata;
1103
1104 ddata = dev_get_drvdata(dev);
1105
Tony Lindgren40d9f912018-09-24 12:16:54 -07001106 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
Tony Lindgrene7420c22018-04-16 10:26:46 -07001107 return 0;
1108
Tony Lindgrenf5e802032018-10-01 09:33:28 -07001109 return pm_runtime_force_suspend(dev);
Tony Lindgren62020f22018-02-22 13:59:44 -08001110}
1111
Tony Lindgrenf5e802032018-10-01 09:33:28 -07001112static int __maybe_unused sysc_noirq_resume(struct device *dev)
Tony Lindgren62020f22018-02-22 13:59:44 -08001113{
1114 struct sysc *ddata;
1115
1116 ddata = dev_get_drvdata(dev);
Tony Lindgrene7420c22018-04-16 10:26:46 -07001117
Tony Lindgren40d9f912018-09-24 12:16:54 -07001118 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
Tony Lindgrene7420c22018-04-16 10:26:46 -07001119 return 0;
1120
Tony Lindgrenf5e802032018-10-01 09:33:28 -07001121 return pm_runtime_force_resume(dev);
Tony Lindgrene7420c22018-04-16 10:26:46 -07001122}
1123
Tony Lindgren0eecc632017-10-10 14:23:43 -07001124static const struct dev_pm_ops sysc_pm_ops = {
Tony Lindgrene7420c22018-04-16 10:26:46 -07001125 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(sysc_noirq_suspend, sysc_noirq_resume)
Tony Lindgren0eecc632017-10-10 14:23:43 -07001126 SET_RUNTIME_PM_OPS(sysc_runtime_suspend,
1127 sysc_runtime_resume,
1128 NULL)
1129};
1130
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001131/* Module revision register based quirks */
1132struct sysc_revision_quirk {
1133 const char *name;
1134 u32 base;
1135 int rev_offset;
1136 int sysc_offset;
1137 int syss_offset;
1138 u32 revision;
1139 u32 revision_mask;
1140 u32 quirks;
1141};
1142
1143#define SYSC_QUIRK(optname, optbase, optrev, optsysc, optsyss, \
1144 optrev_val, optrevmask, optquirkmask) \
1145 { \
1146 .name = (optname), \
1147 .base = (optbase), \
1148 .rev_offset = (optrev), \
1149 .sysc_offset = (optsysc), \
1150 .syss_offset = (optsyss), \
1151 .revision = (optrev_val), \
1152 .revision_mask = (optrevmask), \
1153 .quirks = (optquirkmask), \
1154 }
1155
1156static const struct sysc_revision_quirk sysc_revision_quirks[] = {
1157 /* These drivers need to be fixed to not use pm_runtime_irq_safe() */
Tony Lindgren3a3d8022018-09-27 13:33:58 -07001158 SYSC_QUIRK("gpio", 0, 0, 0x10, 0x114, 0x50600801, 0xffff00ff,
Tony Lindgren09dfe582018-04-16 10:25:52 -07001159 SYSC_QUIRK_LEGACY_IDLE | SYSC_QUIRK_OPT_CLKS_IN_RESET),
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001160 SYSC_QUIRK("mmu", 0, 0, 0x10, 0x14, 0x00000020, 0xffffffff,
1161 SYSC_QUIRK_LEGACY_IDLE),
1162 SYSC_QUIRK("mmu", 0, 0, 0x10, 0x14, 0x00000030, 0xffffffff,
1163 SYSC_QUIRK_LEGACY_IDLE),
1164 SYSC_QUIRK("sham", 0, 0x100, 0x110, 0x114, 0x40000c03, 0xffffffff,
1165 SYSC_QUIRK_LEGACY_IDLE),
1166 SYSC_QUIRK("smartreflex", 0, -1, 0x24, -1, 0x00000000, 0xffffffff,
1167 SYSC_QUIRK_LEGACY_IDLE),
1168 SYSC_QUIRK("smartreflex", 0, -1, 0x38, -1, 0x00000000, 0xffffffff,
1169 SYSC_QUIRK_LEGACY_IDLE),
1170 SYSC_QUIRK("timer", 0, 0, 0x10, 0x14, 0x00000015, 0xffffffff,
Tony Lindgren9bd34c62019-01-22 09:03:08 -08001171 0),
Tony Lindgren8cde5d52018-04-16 10:25:15 -07001172 /* Some timers on omap4 and later */
Tony Lindgren3a3d8022018-09-27 13:33:58 -07001173 SYSC_QUIRK("timer", 0, 0, 0x10, -1, 0x50002100, 0xffffffff,
Tony Lindgren072167d2019-01-29 07:53:47 -08001174 0),
Tony Lindgren3a3d8022018-09-27 13:33:58 -07001175 SYSC_QUIRK("timer", 0, 0, 0x10, -1, 0x4fff1301, 0xffff00ff,
Tony Lindgren9bd34c62019-01-22 09:03:08 -08001176 0),
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001177 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x00000052, 0xffffffff,
Tony Lindgrenb4a9a7a2019-03-21 13:27:08 -07001178 SYSC_QUIRK_SWSUP_SIDLE_ACT | SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgrend708bb12018-04-16 10:25:32 -07001179 /* Uarts on omap4 and later */
Tony Lindgrenb82beef2018-09-24 12:16:59 -07001180 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x50411e03, 0xffff00ff,
Tony Lindgrenb4a9a7a2019-03-21 13:27:08 -07001181 SYSC_QUIRK_SWSUP_SIDLE_ACT | SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgrenb82beef2018-09-24 12:16:59 -07001182 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x47422e03, 0xffffffff,
Tony Lindgrenb4a9a7a2019-03-21 13:27:08 -07001183 SYSC_QUIRK_SWSUP_SIDLE_ACT | SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgren7e27e5d2018-04-20 14:54:16 -07001184
Tony Lindgrena54275f2019-03-21 11:00:21 -07001185 /* Quirks that need to be set based on the module address */
1186 SYSC_QUIRK("mcpdm", 0x40132000, 0, 0x10, -1, 0x50000800, 0xffffffff,
1187 SYSC_QUIRK_EXT_OPT_CLOCK | SYSC_QUIRK_NO_RESET_ON_INIT |
1188 SYSC_QUIRK_SWSUP_SIDLE),
1189
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001190#ifdef DEBUG
Tony Lindgren1ba30692018-09-24 12:17:05 -07001191 SYSC_QUIRK("adc", 0, 0, 0x10, -1, 0x47300001, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -07001192 SYSC_QUIRK("atl", 0, 0, -1, -1, 0x0a070100, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001193 SYSC_QUIRK("aess", 0, 0, 0x10, -1, 0x40000000, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -07001194 SYSC_QUIRK("cm", 0, 0, -1, -1, 0x40000301, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -07001195 SYSC_QUIRK("control", 0, 0, 0x10, -1, 0x40000900, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001196 SYSC_QUIRK("cpgmac", 0, 0x1200, 0x1208, 0x1204, 0x4edb1902,
Tony Lindgren23731ea2018-09-24 12:17:09 -07001197 0xffff00f0, 0),
1198 SYSC_QUIRK("dcan", 0, 0, -1, -1, 0xffffffff, 0xffffffff, 0),
Tony Lindgren13aad512019-03-21 11:00:21 -07001199 SYSC_QUIRK("dmic", 0, 0, 0x10, -1, 0x50010000, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001200 SYSC_QUIRK("dwc3", 0, 0, 0x10, -1, 0x500a0200, 0xffffffff, 0),
1201 SYSC_QUIRK("epwmss", 0, 0, 0x4, -1, 0x47400001, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001202 SYSC_QUIRK("gpu", 0, 0x1fc00, 0x1fc10, -1, 0, 0, 0),
1203 SYSC_QUIRK("hdq1w", 0, 0, 0x14, 0x18, 0x00000006, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001204 SYSC_QUIRK("hdq1w", 0, 0, 0x14, 0x18, 0x0000000a, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001205 SYSC_QUIRK("hsi", 0, 0, 0x10, 0x14, 0x50043101, 0xffffffff, 0),
1206 SYSC_QUIRK("iss", 0, 0, 0x10, -1, 0x40000101, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -07001207 SYSC_QUIRK("i2c", 0, 0, 0x10, 0x90, 0x5040000a, 0xfffff0f0, 0),
Tony Lindgren23731ea2018-09-24 12:17:09 -07001208 SYSC_QUIRK("lcdc", 0, 0, 0x54, -1, 0x4f201000, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001209 SYSC_QUIRK("mcasp", 0, 0, 0x4, -1, 0x44306302, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001210 SYSC_QUIRK("mcasp", 0, 0, 0x4, -1, 0x44307b02, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001211 SYSC_QUIRK("mcbsp", 0, -1, 0x8c, -1, 0, 0, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -07001212 SYSC_QUIRK("mcspi", 0, 0, 0x10, -1, 0x40300a0b, 0xffff00ff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001213 SYSC_QUIRK("mcspi", 0, 0, 0x110, 0x114, 0x40300a0b, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001214 SYSC_QUIRK("mailbox", 0, 0, 0x10, -1, 0x00000400, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001215 SYSC_QUIRK("m3", 0, 0, -1, -1, 0x5f580105, 0x0fff0f00, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -07001216 SYSC_QUIRK("ocp2scp", 0, 0, 0x10, 0x14, 0x50060005, 0xfffffff0, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001217 SYSC_QUIRK("ocp2scp", 0, 0, -1, -1, 0x50060007, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -07001218 SYSC_QUIRK("padconf", 0, 0, 0x10, -1, 0x4fff0800, 0xffffffff, 0),
Tony Lindgrenf0106702018-11-15 14:46:53 -08001219 SYSC_QUIRK("padconf", 0, 0, -1, -1, 0x40001100, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -07001220 SYSC_QUIRK("prcm", 0, 0, -1, -1, 0x40000100, 0xffffffff, 0),
Tony Lindgren23731ea2018-09-24 12:17:09 -07001221 SYSC_QUIRK("prcm", 0, 0, -1, -1, 0x00004102, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001222 SYSC_QUIRK("prcm", 0, 0, -1, -1, 0x40000400, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -07001223 SYSC_QUIRK("scm", 0, 0, 0x10, -1, 0x40000900, 0xffffffff, 0),
Tony Lindgren23731ea2018-09-24 12:17:09 -07001224 SYSC_QUIRK("scm", 0, 0, -1, -1, 0x4e8b0100, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001225 SYSC_QUIRK("scm", 0, 0, -1, -1, 0x4f000100, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -07001226 SYSC_QUIRK("scm", 0, 0, -1, -1, 0x40000900, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -07001227 SYSC_QUIRK("scrm", 0, 0, -1, -1, 0x00000010, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -07001228 SYSC_QUIRK("sdio", 0, 0, 0x10, -1, 0x40202301, 0xffff0ff0, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001229 SYSC_QUIRK("sdio", 0, 0x2fc, 0x110, 0x114, 0x31010000, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -07001230 SYSC_QUIRK("sdma", 0, 0, 0x2c, 0x28, 0x00010900, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001231 SYSC_QUIRK("slimbus", 0, 0, 0x10, -1, 0x40000902, 0xffffffff, 0),
1232 SYSC_QUIRK("slimbus", 0, 0, 0x10, -1, 0x40002903, 0xffffffff, 0),
1233 SYSC_QUIRK("spinlock", 0, 0, 0x10, -1, 0x50020000, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001234 SYSC_QUIRK("rng", 0, 0x1fe0, 0x1fe4, -1, 0x00000020, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -07001235 SYSC_QUIRK("rtc", 0, 0x74, 0x78, -1, 0x4eb01908, 0xffff00f0, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001236 SYSC_QUIRK("timer32k", 0, 0, 0x4, -1, 0x00000060, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001237 SYSC_QUIRK("usbhstll", 0, 0, 0x10, 0x14, 0x00000004, 0xffffffff, 0),
Tony Lindgrenf0106702018-11-15 14:46:53 -08001238 SYSC_QUIRK("usbhstll", 0, 0, 0x10, 0x14, 0x00000008, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001239 SYSC_QUIRK("usb_host_hs", 0, 0, 0x10, 0x14, 0x50700100, 0xffffffff, 0),
Tony Lindgrenf0106702018-11-15 14:46:53 -08001240 SYSC_QUIRK("usb_host_hs", 0, 0, 0x10, -1, 0x50700101, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001241 SYSC_QUIRK("usb_otg_hs", 0, 0x400, 0x404, 0x408, 0x00000050,
1242 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001243 SYSC_QUIRK("wdt", 0, 0, 0x10, 0x14, 0x502a0500, 0xfffff0f0, 0),
1244 SYSC_QUIRK("vfpe", 0, 0, 0x104, -1, 0x4d001200, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001245#endif
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001246};
1247
Tony Lindgren42b9c5c2019-03-21 11:00:21 -07001248/*
1249 * Early quirks based on module base and register offsets only that are
1250 * needed before the module revision can be read
1251 */
1252static void sysc_init_early_quirks(struct sysc *ddata)
1253{
1254 const struct sysc_revision_quirk *q;
1255 int i;
1256
1257 for (i = 0; i < ARRAY_SIZE(sysc_revision_quirks); i++) {
1258 q = &sysc_revision_quirks[i];
1259
1260 if (!q->base)
1261 continue;
1262
1263 if (q->base != ddata->module_pa)
1264 continue;
1265
1266 if (q->rev_offset >= 0 &&
1267 q->rev_offset != ddata->offsets[SYSC_REVISION])
1268 continue;
1269
1270 if (q->sysc_offset >= 0 &&
1271 q->sysc_offset != ddata->offsets[SYSC_SYSCONFIG])
1272 continue;
1273
1274 if (q->syss_offset >= 0 &&
1275 q->syss_offset != ddata->offsets[SYSC_SYSSTATUS])
1276 continue;
1277
1278 ddata->name = q->name;
1279 ddata->cfg.quirks |= q->quirks;
1280 }
1281}
1282
1283/* Quirks that also consider the revision register value */
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001284static void sysc_init_revision_quirks(struct sysc *ddata)
1285{
1286 const struct sysc_revision_quirk *q;
1287 int i;
1288
1289 for (i = 0; i < ARRAY_SIZE(sysc_revision_quirks); i++) {
1290 q = &sysc_revision_quirks[i];
1291
1292 if (q->base && q->base != ddata->module_pa)
1293 continue;
1294
1295 if (q->rev_offset >= 0 &&
1296 q->rev_offset != ddata->offsets[SYSC_REVISION])
1297 continue;
1298
1299 if (q->sysc_offset >= 0 &&
1300 q->sysc_offset != ddata->offsets[SYSC_SYSCONFIG])
1301 continue;
1302
1303 if (q->syss_offset >= 0 &&
1304 q->syss_offset != ddata->offsets[SYSC_SYSSTATUS])
1305 continue;
1306
1307 if (q->revision == ddata->revision ||
1308 (q->revision & q->revision_mask) ==
1309 (ddata->revision & q->revision_mask)) {
1310 ddata->name = q->name;
1311 ddata->cfg.quirks |= q->quirks;
1312 }
1313 }
1314}
1315
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001316static int sysc_clockdomain_init(struct sysc *ddata)
1317{
1318 struct ti_sysc_platform_data *pdata = dev_get_platdata(ddata->dev);
1319 struct clk *fck = NULL, *ick = NULL;
1320 int error;
1321
1322 if (!pdata || !pdata->init_clockdomain)
1323 return 0;
1324
1325 switch (ddata->nr_clocks) {
1326 case 2:
1327 ick = ddata->clocks[SYSC_ICK];
1328 /* fallthrough */
1329 case 1:
1330 fck = ddata->clocks[SYSC_FCK];
1331 break;
1332 case 0:
1333 return 0;
1334 }
1335
1336 error = pdata->init_clockdomain(ddata->dev, fck, ick, &ddata->cookie);
1337 if (!error || error == -ENODEV)
1338 return 0;
1339
1340 return error;
1341}
1342
Tony Lindgrena3e92e72019-03-21 11:00:21 -07001343/*
1344 * Note that pdata->init_module() typically does a reset first. After
1345 * pdata->init_module() is done, PM runtime can be used for the interconnect
1346 * target module.
1347 */
1348static int sysc_legacy_init(struct sysc *ddata)
1349{
1350 struct ti_sysc_platform_data *pdata = dev_get_platdata(ddata->dev);
1351 int error;
1352
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001353 if (!pdata || !pdata->init_module)
Tony Lindgrena3e92e72019-03-21 11:00:21 -07001354 return 0;
1355
1356 error = pdata->init_module(ddata->dev, ddata->mdata, &ddata->cookie);
1357 if (error == -EEXIST)
1358 error = 0;
1359
1360 return error;
1361}
1362
Tony Lindgrenb11c1ea2019-03-21 11:00:21 -07001363/**
1364 * sysc_rstctrl_reset_deassert - deassert rstctrl reset
1365 * @ddata: device driver data
1366 * @reset: reset before deassert
1367 *
1368 * A module can have both OCP softreset control and external rstctrl.
1369 * If more complicated rstctrl resets are needed, please handle these
1370 * directly from the child device driver and map only the module reset
1371 * for the parent interconnect target module device.
1372 *
1373 * Automatic reset of the module on init can be skipped with the
1374 * "ti,no-reset-on-init" device tree property.
1375 */
1376static int sysc_rstctrl_reset_deassert(struct sysc *ddata, bool reset)
1377{
1378 int error;
1379
1380 if (!ddata->rsts)
1381 return 0;
1382
1383 if (reset) {
1384 error = reset_control_assert(ddata->rsts);
1385 if (error)
1386 return error;
1387 }
1388
1389 return reset_control_deassert(ddata->rsts);
1390}
1391
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001392/*
1393 * Note that the caller must ensure the interconnect target module is enabled
1394 * before calling reset. Otherwise reset will not complete.
1395 */
Faiz Abbas596e7952018-07-09 22:18:39 +05301396static int sysc_reset(struct sysc *ddata)
1397{
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001398 int sysc_offset, syss_offset, sysc_val, rstval, quirks, error = 0;
1399 u32 sysc_mask, syss_done;
Faiz Abbas596e7952018-07-09 22:18:39 +05301400
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001401 sysc_offset = ddata->offsets[SYSC_SYSCONFIG];
1402 syss_offset = ddata->offsets[SYSC_SYSSTATUS];
1403 quirks = ddata->cfg.quirks;
1404
1405 if (ddata->legacy_mode || sysc_offset < 0 ||
1406 ddata->cap->regbits->srst_shift < 0 ||
Faiz Abbas596e7952018-07-09 22:18:39 +05301407 ddata->cfg.quirks & SYSC_QUIRK_NO_RESET_ON_INIT)
1408 return 0;
1409
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001410 sysc_mask = BIT(ddata->cap->regbits->srst_shift);
Faiz Abbas596e7952018-07-09 22:18:39 +05301411
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001412 if (ddata->cfg.quirks & SYSS_QUIRK_RESETDONE_INVERTED)
1413 syss_done = 0;
1414 else
1415 syss_done = ddata->cfg.syss_mask;
1416
1417 sysc_val = sysc_read_sysconfig(ddata);
1418 sysc_val |= sysc_mask;
1419 sysc_write(ddata, sysc_offset, sysc_val);
Faiz Abbas596e7952018-07-09 22:18:39 +05301420
1421 /* Poll on reset status */
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001422 if (syss_offset >= 0) {
1423 error = readx_poll_timeout(sysc_read_sysstatus, ddata, rstval,
1424 (rstval & ddata->cfg.syss_mask) ==
1425 syss_done,
1426 100, MAX_MODULE_SOFTRESET_WAIT);
Faiz Abbas596e7952018-07-09 22:18:39 +05301427
Tony Lindgrene0db94f2019-05-27 04:51:53 -07001428 } else if (ddata->cfg.quirks & SYSC_QUIRK_RESET_STATUS) {
1429 error = readx_poll_timeout(sysc_read_sysconfig, ddata, rstval,
1430 !(rstval & sysc_mask),
1431 100, MAX_MODULE_SOFTRESET_WAIT);
1432 }
1433
1434 return error;
Faiz Abbas596e7952018-07-09 22:18:39 +05301435}
1436
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001437/*
1438 * At this point the module is configured enough to read the revision but
1439 * module may not be completely configured yet to use PM runtime. Enable
1440 * all clocks directly during init to configure the quirks needed for PM
1441 * runtime based on the revision register.
1442 */
Tony Lindgren566a9b052017-12-15 09:41:19 -08001443static int sysc_init_module(struct sysc *ddata)
1444{
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001445 int error = 0;
1446 bool manage_clocks = true;
Tony Lindgrenb11c1ea2019-03-21 11:00:21 -07001447 bool reset = true;
1448
1449 if (ddata->cfg.quirks & SYSC_QUIRK_NO_RESET_ON_INIT)
1450 reset = false;
1451
1452 error = sysc_rstctrl_reset_deassert(ddata, reset);
1453 if (error)
1454 return error;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001455
Tony Lindgren386cb762019-03-22 07:49:30 -07001456 if (ddata->cfg.quirks &
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001457 (SYSC_QUIRK_NO_IDLE | SYSC_QUIRK_NO_IDLE_ON_INIT))
1458 manage_clocks = false;
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001459
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001460 error = sysc_clockdomain_init(ddata);
1461 if (error)
1462 return error;
1463
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001464 if (manage_clocks) {
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001465 sysc_clkdm_deny_idle(ddata);
1466
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001467 error = sysc_enable_opt_clocks(ddata);
1468 if (error)
1469 return error;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001470
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001471 error = sysc_enable_main_clocks(ddata);
1472 if (error)
1473 goto err_opt_clocks;
Faiz Abbas596e7952018-07-09 22:18:39 +05301474 }
1475
Tony Lindgren566a9b052017-12-15 09:41:19 -08001476 ddata->revision = sysc_read_revision(ddata);
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001477 sysc_init_revision_quirks(ddata);
1478
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001479 if (ddata->legacy_mode) {
1480 error = sysc_legacy_init(ddata);
1481 if (error)
1482 goto err_main_clocks;
1483 }
1484
1485 if (!ddata->legacy_mode && manage_clocks) {
1486 error = sysc_enable_module(ddata->dev);
1487 if (error)
1488 goto err_main_clocks;
1489 }
Tony Lindgrena3e92e72019-03-21 11:00:21 -07001490
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001491 error = sysc_reset(ddata);
1492 if (error)
1493 dev_err(ddata->dev, "Reset failed with %d\n", error);
1494
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001495 if (!ddata->legacy_mode && manage_clocks)
1496 sysc_disable_module(ddata->dev);
1497
Tony Lindgrena3e92e72019-03-21 11:00:21 -07001498err_main_clocks:
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001499 if (manage_clocks)
1500 sysc_disable_main_clocks(ddata);
1501err_opt_clocks:
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001502 if (manage_clocks) {
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001503 sysc_disable_opt_clocks(ddata);
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001504 sysc_clkdm_allow_idle(ddata);
1505 }
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001506
1507 return error;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001508}
1509
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08001510static int sysc_init_sysc_mask(struct sysc *ddata)
1511{
1512 struct device_node *np = ddata->dev->of_node;
1513 int error;
1514 u32 val;
1515
1516 error = of_property_read_u32(np, "ti,sysc-mask", &val);
1517 if (error)
1518 return 0;
1519
1520 if (val)
1521 ddata->cfg.sysc_val = val & ddata->cap->sysc_mask;
1522 else
1523 ddata->cfg.sysc_val = ddata->cap->sysc_mask;
1524
1525 return 0;
1526}
1527
1528static int sysc_init_idlemode(struct sysc *ddata, u8 *idlemodes,
1529 const char *name)
1530{
1531 struct device_node *np = ddata->dev->of_node;
1532 struct property *prop;
1533 const __be32 *p;
1534 u32 val;
1535
1536 of_property_for_each_u32(np, name, prop, p, val) {
1537 if (val >= SYSC_NR_IDLEMODES) {
1538 dev_err(ddata->dev, "invalid idlemode: %i\n", val);
1539 return -EINVAL;
1540 }
1541 *idlemodes |= (1 << val);
1542 }
1543
1544 return 0;
1545}
1546
1547static int sysc_init_idlemodes(struct sysc *ddata)
1548{
1549 int error;
1550
1551 error = sysc_init_idlemode(ddata, &ddata->cfg.midlemodes,
1552 "ti,sysc-midle");
1553 if (error)
1554 return error;
1555
1556 error = sysc_init_idlemode(ddata, &ddata->cfg.sidlemodes,
1557 "ti,sysc-sidle");
1558 if (error)
1559 return error;
1560
1561 return 0;
1562}
1563
1564/*
1565 * Only some devices on omap4 and later have SYSCONFIG reset done
1566 * bit. We can detect this if there is no SYSSTATUS at all, or the
1567 * SYSTATUS bit 0 is not used. Note that some SYSSTATUS registers
1568 * have multiple bits for the child devices like OHCI and EHCI.
1569 * Depends on SYSC being parsed first.
1570 */
1571static int sysc_init_syss_mask(struct sysc *ddata)
1572{
1573 struct device_node *np = ddata->dev->of_node;
1574 int error;
1575 u32 val;
1576
1577 error = of_property_read_u32(np, "ti,syss-mask", &val);
1578 if (error) {
1579 if ((ddata->cap->type == TI_SYSC_OMAP4 ||
1580 ddata->cap->type == TI_SYSC_OMAP4_TIMER) &&
1581 (ddata->cfg.sysc_val & SYSC_OMAP4_SOFTRESET))
1582 ddata->cfg.quirks |= SYSC_QUIRK_RESET_STATUS;
1583
1584 return 0;
1585 }
1586
1587 if (!(val & 1) && (ddata->cfg.sysc_val & SYSC_OMAP4_SOFTRESET))
1588 ddata->cfg.quirks |= SYSC_QUIRK_RESET_STATUS;
1589
1590 ddata->cfg.syss_mask = val;
1591
1592 return 0;
1593}
1594
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001595/*
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001596 * Many child device drivers need to have fck and opt clocks available
1597 * to get the clock rate for device internal configuration etc.
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001598 */
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001599static int sysc_child_add_named_clock(struct sysc *ddata,
1600 struct device *child,
1601 const char *name)
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001602{
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001603 struct clk *clk;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001604 struct clk_lookup *l;
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001605 int error = 0;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001606
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001607 if (!name)
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001608 return 0;
1609
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001610 clk = clk_get(child, name);
1611 if (!IS_ERR(clk)) {
1612 clk_put(clk);
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001613
1614 return -EEXIST;
1615 }
1616
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001617 clk = clk_get(ddata->dev, name);
1618 if (IS_ERR(clk))
1619 return -ENODEV;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001620
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001621 l = clkdev_create(clk, name, dev_name(child));
1622 if (!l)
1623 error = -ENOMEM;
1624
1625 clk_put(clk);
1626
1627 return error;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001628}
1629
Tony Lindgren09dfe582018-04-16 10:25:52 -07001630static int sysc_child_add_clocks(struct sysc *ddata,
1631 struct device *child)
1632{
1633 int i, error;
1634
1635 for (i = 0; i < ddata->nr_clocks; i++) {
1636 error = sysc_child_add_named_clock(ddata,
1637 child,
1638 ddata->clock_roles[i]);
1639 if (error && error != -EEXIST) {
1640 dev_err(ddata->dev, "could not add child clock %s: %i\n",
1641 ddata->clock_roles[i], error);
1642
1643 return error;
1644 }
1645 }
1646
1647 return 0;
1648}
1649
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001650static struct device_type sysc_device_type = {
1651};
1652
1653static struct sysc *sysc_child_to_parent(struct device *dev)
1654{
1655 struct device *parent = dev->parent;
1656
1657 if (!parent || parent->type != &sysc_device_type)
1658 return NULL;
1659
1660 return dev_get_drvdata(parent);
1661}
1662
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001663static int __maybe_unused sysc_child_runtime_suspend(struct device *dev)
1664{
1665 struct sysc *ddata;
1666 int error;
1667
1668 ddata = sysc_child_to_parent(dev);
1669
1670 error = pm_generic_runtime_suspend(dev);
1671 if (error)
1672 return error;
1673
1674 if (!ddata->enabled)
1675 return 0;
1676
1677 return sysc_runtime_suspend(ddata->dev);
1678}
1679
1680static int __maybe_unused sysc_child_runtime_resume(struct device *dev)
1681{
1682 struct sysc *ddata;
1683 int error;
1684
1685 ddata = sysc_child_to_parent(dev);
1686
1687 if (!ddata->enabled) {
1688 error = sysc_runtime_resume(ddata->dev);
1689 if (error < 0)
1690 dev_err(ddata->dev,
1691 "%s error: %i\n", __func__, error);
1692 }
1693
1694 return pm_generic_runtime_resume(dev);
1695}
1696
1697#ifdef CONFIG_PM_SLEEP
1698static int sysc_child_suspend_noirq(struct device *dev)
1699{
1700 struct sysc *ddata;
1701 int error;
1702
1703 ddata = sysc_child_to_parent(dev);
1704
Tony Lindgrenef55f822018-04-16 10:27:15 -07001705 dev_dbg(ddata->dev, "%s %s\n", __func__,
1706 ddata->name ? ddata->name : "");
1707
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001708 error = pm_generic_suspend_noirq(dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001709 if (error) {
1710 dev_err(dev, "%s error at %i: %i\n",
1711 __func__, __LINE__, error);
1712
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001713 return error;
Tony Lindgrenef55f822018-04-16 10:27:15 -07001714 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001715
1716 if (!pm_runtime_status_suspended(dev)) {
1717 error = pm_generic_runtime_suspend(dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001718 if (error) {
Tony Lindgrenf9490782018-09-28 15:21:50 -07001719 dev_dbg(dev, "%s busy at %i: %i\n",
1720 __func__, __LINE__, error);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001721
Tony Lindgren4f3530f2018-08-08 01:07:06 -07001722 return 0;
Tony Lindgrenef55f822018-04-16 10:27:15 -07001723 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001724
1725 error = sysc_runtime_suspend(ddata->dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001726 if (error) {
1727 dev_err(dev, "%s error at %i: %i\n",
1728 __func__, __LINE__, error);
1729
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001730 return error;
Tony Lindgrenef55f822018-04-16 10:27:15 -07001731 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001732
1733 ddata->child_needs_resume = true;
1734 }
1735
1736 return 0;
1737}
1738
1739static int sysc_child_resume_noirq(struct device *dev)
1740{
1741 struct sysc *ddata;
1742 int error;
1743
1744 ddata = sysc_child_to_parent(dev);
1745
Tony Lindgrenef55f822018-04-16 10:27:15 -07001746 dev_dbg(ddata->dev, "%s %s\n", __func__,
1747 ddata->name ? ddata->name : "");
1748
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001749 if (ddata->child_needs_resume) {
1750 ddata->child_needs_resume = false;
1751
1752 error = sysc_runtime_resume(ddata->dev);
1753 if (error)
1754 dev_err(ddata->dev,
1755 "%s runtime resume error: %i\n",
1756 __func__, error);
1757
1758 error = pm_generic_runtime_resume(dev);
1759 if (error)
1760 dev_err(ddata->dev,
1761 "%s generic runtime resume: %i\n",
1762 __func__, error);
1763 }
1764
1765 return pm_generic_resume_noirq(dev);
1766}
1767#endif
1768
Tony Lindgrenb7182b42019-03-21 11:00:21 -07001769static struct dev_pm_domain sysc_child_pm_domain = {
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001770 .ops = {
1771 SET_RUNTIME_PM_OPS(sysc_child_runtime_suspend,
1772 sysc_child_runtime_resume,
1773 NULL)
1774 USE_PLATFORM_PM_SLEEP_OPS
1775 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(sysc_child_suspend_noirq,
1776 sysc_child_resume_noirq)
1777 }
1778};
1779
1780/**
1781 * sysc_legacy_idle_quirk - handle children in omap_device compatible way
1782 * @ddata: device driver data
1783 * @child: child device driver
1784 *
1785 * Allow idle for child devices as done with _od_runtime_suspend().
1786 * Otherwise many child devices will not idle because of the permanent
1787 * parent usecount set in pm_runtime_irq_safe().
1788 *
1789 * Note that the long term solution is to just modify the child device
1790 * drivers to not set pm_runtime_irq_safe() and then this can be just
1791 * dropped.
1792 */
1793static void sysc_legacy_idle_quirk(struct sysc *ddata, struct device *child)
1794{
1795 if (!ddata->legacy_mode)
1796 return;
1797
1798 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
1799 dev_pm_domain_set(child, &sysc_child_pm_domain);
1800}
1801
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001802static int sysc_notifier_call(struct notifier_block *nb,
1803 unsigned long event, void *device)
1804{
1805 struct device *dev = device;
1806 struct sysc *ddata;
1807 int error;
1808
1809 ddata = sysc_child_to_parent(dev);
1810 if (!ddata)
1811 return NOTIFY_DONE;
1812
1813 switch (event) {
1814 case BUS_NOTIFY_ADD_DEVICE:
Tony Lindgren09dfe582018-04-16 10:25:52 -07001815 error = sysc_child_add_clocks(ddata, dev);
1816 if (error)
1817 return error;
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001818 sysc_legacy_idle_quirk(ddata, dev);
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001819 break;
1820 default:
1821 break;
1822 }
1823
1824 return NOTIFY_DONE;
1825}
1826
1827static struct notifier_block sysc_nb = {
1828 .notifier_call = sysc_notifier_call,
1829};
1830
Tony Lindgren566a9b052017-12-15 09:41:19 -08001831/* Device tree configured quirks */
1832struct sysc_dts_quirk {
1833 const char *name;
1834 u32 mask;
1835};
1836
1837static const struct sysc_dts_quirk sysc_dts_quirks[] = {
1838 { .name = "ti,no-idle-on-init",
1839 .mask = SYSC_QUIRK_NO_IDLE_ON_INIT, },
1840 { .name = "ti,no-reset-on-init",
1841 .mask = SYSC_QUIRK_NO_RESET_ON_INIT, },
Tony Lindgren386cb762019-03-22 07:49:30 -07001842 { .name = "ti,no-idle",
1843 .mask = SYSC_QUIRK_NO_IDLE, },
Tony Lindgren566a9b052017-12-15 09:41:19 -08001844};
1845
Tony Lindgren4014c082018-12-10 14:11:26 -08001846static void sysc_parse_dts_quirks(struct sysc *ddata, struct device_node *np,
1847 bool is_child)
Tony Lindgren566a9b052017-12-15 09:41:19 -08001848{
Tony Lindgren566a9b052017-12-15 09:41:19 -08001849 const struct property *prop;
Tony Lindgren4014c082018-12-10 14:11:26 -08001850 int i, len;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001851
1852 for (i = 0; i < ARRAY_SIZE(sysc_dts_quirks); i++) {
Tony Lindgren4014c082018-12-10 14:11:26 -08001853 const char *name = sysc_dts_quirks[i].name;
1854
1855 prop = of_get_property(np, name, &len);
Tony Lindgren566a9b052017-12-15 09:41:19 -08001856 if (!prop)
Tony Lindgrend39b6ea2018-02-15 09:18:55 -08001857 continue;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001858
1859 ddata->cfg.quirks |= sysc_dts_quirks[i].mask;
Tony Lindgren4014c082018-12-10 14:11:26 -08001860 if (is_child) {
1861 dev_warn(ddata->dev,
1862 "dts flag should be at module level for %s\n",
1863 name);
1864 }
Tony Lindgren566a9b052017-12-15 09:41:19 -08001865 }
Tony Lindgren4014c082018-12-10 14:11:26 -08001866}
Tony Lindgren566a9b052017-12-15 09:41:19 -08001867
Tony Lindgren4014c082018-12-10 14:11:26 -08001868static int sysc_init_dts_quirks(struct sysc *ddata)
1869{
1870 struct device_node *np = ddata->dev->of_node;
1871 int error;
1872 u32 val;
1873
1874 ddata->legacy_mode = of_get_property(np, "ti,hwmods", NULL);
1875
1876 sysc_parse_dts_quirks(ddata, np, false);
Tony Lindgren566a9b052017-12-15 09:41:19 -08001877 error = of_property_read_u32(np, "ti,sysc-delay-us", &val);
1878 if (!error) {
1879 if (val > 255) {
1880 dev_warn(ddata->dev, "bad ti,sysc-delay-us: %i\n",
1881 val);
1882 }
1883
1884 ddata->cfg.srst_udelay = (u8)val;
1885 }
1886
1887 return 0;
1888}
1889
Tony Lindgren0eecc632017-10-10 14:23:43 -07001890static void sysc_unprepare(struct sysc *ddata)
1891{
1892 int i;
1893
Tony Lindgrenaaa29bb2019-03-21 11:00:21 -07001894 if (!ddata->clocks)
1895 return;
1896
Tony Lindgren0eecc632017-10-10 14:23:43 -07001897 for (i = 0; i < SYSC_MAX_CLOCKS; i++) {
1898 if (!IS_ERR_OR_NULL(ddata->clocks[i]))
1899 clk_unprepare(ddata->clocks[i]);
1900 }
1901}
1902
Tony Lindgren70a65242017-12-15 09:41:09 -08001903/*
1904 * Common sysc register bits found on omap2, also known as type1
1905 */
1906static const struct sysc_regbits sysc_regbits_omap2 = {
1907 .dmadisable_shift = -ENODEV,
1908 .midle_shift = 12,
1909 .sidle_shift = 3,
1910 .clkact_shift = 8,
1911 .emufree_shift = 5,
1912 .enwkup_shift = 2,
1913 .srst_shift = 1,
1914 .autoidle_shift = 0,
1915};
1916
1917static const struct sysc_capabilities sysc_omap2 = {
1918 .type = TI_SYSC_OMAP2,
1919 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY | SYSC_OMAP2_EMUFREE |
1920 SYSC_OMAP2_ENAWAKEUP | SYSC_OMAP2_SOFTRESET |
1921 SYSC_OMAP2_AUTOIDLE,
1922 .regbits = &sysc_regbits_omap2,
1923};
1924
1925/* All omap2 and 3 timers, and timers 1, 2 & 10 on omap 4 and 5 */
1926static const struct sysc_capabilities sysc_omap2_timer = {
1927 .type = TI_SYSC_OMAP2_TIMER,
1928 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY | SYSC_OMAP2_EMUFREE |
1929 SYSC_OMAP2_ENAWAKEUP | SYSC_OMAP2_SOFTRESET |
1930 SYSC_OMAP2_AUTOIDLE,
1931 .regbits = &sysc_regbits_omap2,
1932 .mod_quirks = SYSC_QUIRK_USE_CLOCKACT,
1933};
1934
1935/*
1936 * SHAM2 (SHA1/MD5) sysc found on omap3, a variant of sysc_regbits_omap2
1937 * with different sidle position
1938 */
1939static const struct sysc_regbits sysc_regbits_omap3_sham = {
1940 .dmadisable_shift = -ENODEV,
1941 .midle_shift = -ENODEV,
1942 .sidle_shift = 4,
1943 .clkact_shift = -ENODEV,
1944 .enwkup_shift = -ENODEV,
1945 .srst_shift = 1,
1946 .autoidle_shift = 0,
1947 .emufree_shift = -ENODEV,
1948};
1949
1950static const struct sysc_capabilities sysc_omap3_sham = {
1951 .type = TI_SYSC_OMAP3_SHAM,
1952 .sysc_mask = SYSC_OMAP2_SOFTRESET | SYSC_OMAP2_AUTOIDLE,
1953 .regbits = &sysc_regbits_omap3_sham,
1954};
1955
1956/*
1957 * AES register bits found on omap3 and later, a variant of
1958 * sysc_regbits_omap2 with different sidle position
1959 */
1960static const struct sysc_regbits sysc_regbits_omap3_aes = {
1961 .dmadisable_shift = -ENODEV,
1962 .midle_shift = -ENODEV,
1963 .sidle_shift = 6,
1964 .clkact_shift = -ENODEV,
1965 .enwkup_shift = -ENODEV,
1966 .srst_shift = 1,
1967 .autoidle_shift = 0,
1968 .emufree_shift = -ENODEV,
1969};
1970
1971static const struct sysc_capabilities sysc_omap3_aes = {
1972 .type = TI_SYSC_OMAP3_AES,
1973 .sysc_mask = SYSC_OMAP2_SOFTRESET | SYSC_OMAP2_AUTOIDLE,
1974 .regbits = &sysc_regbits_omap3_aes,
1975};
1976
1977/*
1978 * Common sysc register bits found on omap4, also known as type2
1979 */
1980static const struct sysc_regbits sysc_regbits_omap4 = {
1981 .dmadisable_shift = 16,
1982 .midle_shift = 4,
1983 .sidle_shift = 2,
1984 .clkact_shift = -ENODEV,
1985 .enwkup_shift = -ENODEV,
1986 .emufree_shift = 1,
1987 .srst_shift = 0,
1988 .autoidle_shift = -ENODEV,
1989};
1990
1991static const struct sysc_capabilities sysc_omap4 = {
1992 .type = TI_SYSC_OMAP4,
1993 .sysc_mask = SYSC_OMAP4_DMADISABLE | SYSC_OMAP4_FREEEMU |
1994 SYSC_OMAP4_SOFTRESET,
1995 .regbits = &sysc_regbits_omap4,
1996};
1997
1998static const struct sysc_capabilities sysc_omap4_timer = {
1999 .type = TI_SYSC_OMAP4_TIMER,
2000 .sysc_mask = SYSC_OMAP4_DMADISABLE | SYSC_OMAP4_FREEEMU |
2001 SYSC_OMAP4_SOFTRESET,
2002 .regbits = &sysc_regbits_omap4,
2003};
2004
2005/*
2006 * Common sysc register bits found on omap4, also known as type3
2007 */
2008static const struct sysc_regbits sysc_regbits_omap4_simple = {
2009 .dmadisable_shift = -ENODEV,
2010 .midle_shift = 2,
2011 .sidle_shift = 0,
2012 .clkact_shift = -ENODEV,
2013 .enwkup_shift = -ENODEV,
2014 .srst_shift = -ENODEV,
2015 .emufree_shift = -ENODEV,
2016 .autoidle_shift = -ENODEV,
2017};
2018
2019static const struct sysc_capabilities sysc_omap4_simple = {
2020 .type = TI_SYSC_OMAP4_SIMPLE,
2021 .regbits = &sysc_regbits_omap4_simple,
2022};
2023
2024/*
2025 * SmartReflex sysc found on omap34xx
2026 */
2027static const struct sysc_regbits sysc_regbits_omap34xx_sr = {
2028 .dmadisable_shift = -ENODEV,
2029 .midle_shift = -ENODEV,
2030 .sidle_shift = -ENODEV,
2031 .clkact_shift = 20,
2032 .enwkup_shift = -ENODEV,
2033 .srst_shift = -ENODEV,
2034 .emufree_shift = -ENODEV,
2035 .autoidle_shift = -ENODEV,
2036};
2037
2038static const struct sysc_capabilities sysc_34xx_sr = {
2039 .type = TI_SYSC_OMAP34XX_SR,
2040 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY,
2041 .regbits = &sysc_regbits_omap34xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002042 .mod_quirks = SYSC_QUIRK_USE_CLOCKACT | SYSC_QUIRK_UNCACHED |
2043 SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08002044};
2045
2046/*
2047 * SmartReflex sysc found on omap36xx and later
2048 */
2049static const struct sysc_regbits sysc_regbits_omap36xx_sr = {
2050 .dmadisable_shift = -ENODEV,
2051 .midle_shift = -ENODEV,
2052 .sidle_shift = 24,
2053 .clkact_shift = -ENODEV,
2054 .enwkup_shift = 26,
2055 .srst_shift = -ENODEV,
2056 .emufree_shift = -ENODEV,
2057 .autoidle_shift = -ENODEV,
2058};
2059
2060static const struct sysc_capabilities sysc_36xx_sr = {
2061 .type = TI_SYSC_OMAP36XX_SR,
Tony Lindgren3267c082018-01-22 09:32:53 -08002062 .sysc_mask = SYSC_OMAP3_SR_ENAWAKEUP,
Tony Lindgren70a65242017-12-15 09:41:09 -08002063 .regbits = &sysc_regbits_omap36xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002064 .mod_quirks = SYSC_QUIRK_UNCACHED | SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08002065};
2066
2067static const struct sysc_capabilities sysc_omap4_sr = {
2068 .type = TI_SYSC_OMAP4_SR,
2069 .regbits = &sysc_regbits_omap36xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002070 .mod_quirks = SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08002071};
2072
2073/*
2074 * McASP register bits found on omap4 and later
2075 */
2076static const struct sysc_regbits sysc_regbits_omap4_mcasp = {
2077 .dmadisable_shift = -ENODEV,
2078 .midle_shift = -ENODEV,
2079 .sidle_shift = 0,
2080 .clkact_shift = -ENODEV,
2081 .enwkup_shift = -ENODEV,
2082 .srst_shift = -ENODEV,
2083 .emufree_shift = -ENODEV,
2084 .autoidle_shift = -ENODEV,
2085};
2086
2087static const struct sysc_capabilities sysc_omap4_mcasp = {
2088 .type = TI_SYSC_OMAP4_MCASP,
2089 .regbits = &sysc_regbits_omap4_mcasp,
Tony Lindgren2c63a832018-11-15 14:46:53 -08002090 .mod_quirks = SYSC_QUIRK_OPT_CLKS_NEEDED,
2091};
2092
2093/*
2094 * McASP found on dra7 and later
2095 */
2096static const struct sysc_capabilities sysc_dra7_mcasp = {
2097 .type = TI_SYSC_OMAP4_SIMPLE,
2098 .regbits = &sysc_regbits_omap4_simple,
2099 .mod_quirks = SYSC_QUIRK_OPT_CLKS_NEEDED,
Tony Lindgren70a65242017-12-15 09:41:09 -08002100};
2101
2102/*
2103 * FS USB host found on omap4 and later
2104 */
2105static const struct sysc_regbits sysc_regbits_omap4_usb_host_fs = {
2106 .dmadisable_shift = -ENODEV,
2107 .midle_shift = -ENODEV,
2108 .sidle_shift = 24,
2109 .clkact_shift = -ENODEV,
2110 .enwkup_shift = 26,
2111 .srst_shift = -ENODEV,
2112 .emufree_shift = -ENODEV,
2113 .autoidle_shift = -ENODEV,
2114};
2115
2116static const struct sysc_capabilities sysc_omap4_usb_host_fs = {
2117 .type = TI_SYSC_OMAP4_USB_HOST_FS,
2118 .sysc_mask = SYSC_OMAP2_ENAWAKEUP,
2119 .regbits = &sysc_regbits_omap4_usb_host_fs,
2120};
2121
Faiz Abbas7f35e632018-07-09 22:18:38 +05302122static const struct sysc_regbits sysc_regbits_dra7_mcan = {
2123 .dmadisable_shift = -ENODEV,
2124 .midle_shift = -ENODEV,
2125 .sidle_shift = -ENODEV,
2126 .clkact_shift = -ENODEV,
2127 .enwkup_shift = 4,
2128 .srst_shift = 0,
2129 .emufree_shift = -ENODEV,
2130 .autoidle_shift = -ENODEV,
2131};
2132
2133static const struct sysc_capabilities sysc_dra7_mcan = {
2134 .type = TI_SYSC_DRA7_MCAN,
2135 .sysc_mask = SYSC_DRA7_MCAN_ENAWAKEUP | SYSC_OMAP4_SOFTRESET,
2136 .regbits = &sysc_regbits_dra7_mcan,
Tony Lindgrene0db94f2019-05-27 04:51:53 -07002137 .mod_quirks = SYSS_QUIRK_RESETDONE_INVERTED,
Faiz Abbas7f35e632018-07-09 22:18:38 +05302138};
2139
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002140static int sysc_init_pdata(struct sysc *ddata)
2141{
2142 struct ti_sysc_platform_data *pdata = dev_get_platdata(ddata->dev);
Tony Lindgrena3e92e72019-03-21 11:00:21 -07002143 struct ti_sysc_module_data *mdata;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002144
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07002145 if (!pdata)
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002146 return 0;
2147
Tony Lindgrena3e92e72019-03-21 11:00:21 -07002148 mdata = devm_kzalloc(ddata->dev, sizeof(*mdata), GFP_KERNEL);
2149 if (!mdata)
2150 return -ENOMEM;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002151
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07002152 if (ddata->legacy_mode) {
2153 mdata->name = ddata->legacy_mode;
2154 mdata->module_pa = ddata->module_pa;
2155 mdata->module_size = ddata->module_size;
2156 mdata->offsets = ddata->offsets;
2157 mdata->nr_offsets = SYSC_MAX_REGS;
2158 mdata->cap = ddata->cap;
2159 mdata->cfg = &ddata->cfg;
2160 }
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002161
Tony Lindgrena3e92e72019-03-21 11:00:21 -07002162 ddata->mdata = mdata;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002163
Tony Lindgrena3e92e72019-03-21 11:00:21 -07002164 return 0;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002165}
2166
Tony Lindgren70a65242017-12-15 09:41:09 -08002167static int sysc_init_match(struct sysc *ddata)
2168{
2169 const struct sysc_capabilities *cap;
2170
2171 cap = of_device_get_match_data(ddata->dev);
2172 if (!cap)
2173 return -EINVAL;
2174
2175 ddata->cap = cap;
2176 if (ddata->cap)
2177 ddata->cfg.quirks |= ddata->cap->mod_quirks;
2178
2179 return 0;
2180}
2181
Tony Lindgren76f0f772018-02-23 08:28:45 -08002182static void ti_sysc_idle(struct work_struct *work)
2183{
2184 struct sysc *ddata;
2185
2186 ddata = container_of(work, struct sysc, idle_work.work);
2187
2188 if (pm_runtime_active(ddata->dev))
2189 pm_runtime_put_sync(ddata->dev);
2190}
2191
Tony Lindgrenc4bebea2018-04-16 10:24:54 -07002192static const struct of_device_id sysc_match_table[] = {
2193 { .compatible = "simple-bus", },
2194 { /* sentinel */ },
2195};
2196
Tony Lindgren0eecc632017-10-10 14:23:43 -07002197static int sysc_probe(struct platform_device *pdev)
2198{
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002199 struct ti_sysc_platform_data *pdata = dev_get_platdata(&pdev->dev);
Tony Lindgren0eecc632017-10-10 14:23:43 -07002200 struct sysc *ddata;
2201 int error;
2202
2203 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
2204 if (!ddata)
2205 return -ENOMEM;
2206
2207 ddata->dev = &pdev->dev;
Tony Lindgren566a9b052017-12-15 09:41:19 -08002208 platform_set_drvdata(pdev, ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -07002209
Tony Lindgren70a65242017-12-15 09:41:09 -08002210 error = sysc_init_match(ddata);
2211 if (error)
2212 return error;
2213
Tony Lindgren566a9b052017-12-15 09:41:19 -08002214 error = sysc_init_dts_quirks(ddata);
2215 if (error)
2216 goto unprepare;
2217
Tony Lindgren0eecc632017-10-10 14:23:43 -07002218 error = sysc_map_and_check_registers(ddata);
2219 if (error)
2220 goto unprepare;
2221
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08002222 error = sysc_init_sysc_mask(ddata);
2223 if (error)
2224 goto unprepare;
2225
2226 error = sysc_init_idlemodes(ddata);
2227 if (error)
2228 goto unprepare;
2229
2230 error = sysc_init_syss_mask(ddata);
2231 if (error)
2232 goto unprepare;
2233
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002234 error = sysc_init_pdata(ddata);
2235 if (error)
2236 goto unprepare;
2237
Tony Lindgren42b9c5c2019-03-21 11:00:21 -07002238 sysc_init_early_quirks(ddata);
2239
2240 error = sysc_get_clocks(ddata);
2241 if (error)
2242 return error;
2243
Tony Lindgren50622362018-04-16 10:20:27 -07002244 error = sysc_init_resets(ddata);
2245 if (error)
2246 return error;
Tony Lindgren566a9b052017-12-15 09:41:19 -08002247
2248 error = sysc_init_module(ddata);
2249 if (error)
2250 goto unprepare;
2251
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07002252 pm_runtime_enable(ddata->dev);
Tony Lindgren0eecc632017-10-10 14:23:43 -07002253 error = pm_runtime_get_sync(ddata->dev);
2254 if (error < 0) {
2255 pm_runtime_put_noidle(ddata->dev);
2256 pm_runtime_disable(ddata->dev);
2257 goto unprepare;
2258 }
2259
Tony Lindgren0eecc632017-10-10 14:23:43 -07002260 sysc_show_registers(ddata);
2261
Tony Lindgren2c355ff2018-02-22 13:58:03 -08002262 ddata->dev->type = &sysc_device_type;
Tony Lindgrenc4bebea2018-04-16 10:24:54 -07002263 error = of_platform_populate(ddata->dev->of_node, sysc_match_table,
2264 pdata ? pdata->auxdata : NULL,
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002265 ddata->dev);
Tony Lindgren0eecc632017-10-10 14:23:43 -07002266 if (error)
2267 goto err;
2268
Tony Lindgren76f0f772018-02-23 08:28:45 -08002269 INIT_DELAYED_WORK(&ddata->idle_work, ti_sysc_idle);
2270
2271 /* At least earlycon won't survive without deferred idle */
2272 if (ddata->cfg.quirks & (SYSC_QUIRK_NO_IDLE_ON_INIT |
2273 SYSC_QUIRK_NO_RESET_ON_INIT)) {
2274 schedule_delayed_work(&ddata->idle_work, 3000);
2275 } else {
2276 pm_runtime_put(&pdev->dev);
2277 }
Tony Lindgren0eecc632017-10-10 14:23:43 -07002278
Tony Lindgren50622362018-04-16 10:20:27 -07002279 if (!of_get_available_child_count(ddata->dev->of_node))
2280 reset_control_assert(ddata->rsts);
2281
Tony Lindgren0eecc632017-10-10 14:23:43 -07002282 return 0;
2283
2284err:
Tony Lindgren0eecc632017-10-10 14:23:43 -07002285 pm_runtime_put_sync(&pdev->dev);
2286 pm_runtime_disable(&pdev->dev);
2287unprepare:
2288 sysc_unprepare(ddata);
2289
2290 return error;
2291}
2292
Tony Lindgren684be5a2017-10-13 10:48:40 -07002293static int sysc_remove(struct platform_device *pdev)
2294{
2295 struct sysc *ddata = platform_get_drvdata(pdev);
2296 int error;
2297
Tony Lindgren76f0f772018-02-23 08:28:45 -08002298 cancel_delayed_work_sync(&ddata->idle_work);
2299
Tony Lindgren684be5a2017-10-13 10:48:40 -07002300 error = pm_runtime_get_sync(ddata->dev);
2301 if (error < 0) {
2302 pm_runtime_put_noidle(ddata->dev);
2303 pm_runtime_disable(ddata->dev);
2304 goto unprepare;
2305 }
2306
2307 of_platform_depopulate(&pdev->dev);
2308
Tony Lindgren684be5a2017-10-13 10:48:40 -07002309 pm_runtime_put_sync(&pdev->dev);
2310 pm_runtime_disable(&pdev->dev);
Tony Lindgren50622362018-04-16 10:20:27 -07002311 reset_control_assert(ddata->rsts);
Tony Lindgren684be5a2017-10-13 10:48:40 -07002312
2313unprepare:
2314 sysc_unprepare(ddata);
2315
2316 return 0;
2317}
2318
Tony Lindgren0eecc632017-10-10 14:23:43 -07002319static const struct of_device_id sysc_match[] = {
Tony Lindgren70a65242017-12-15 09:41:09 -08002320 { .compatible = "ti,sysc-omap2", .data = &sysc_omap2, },
2321 { .compatible = "ti,sysc-omap2-timer", .data = &sysc_omap2_timer, },
2322 { .compatible = "ti,sysc-omap4", .data = &sysc_omap4, },
2323 { .compatible = "ti,sysc-omap4-timer", .data = &sysc_omap4_timer, },
2324 { .compatible = "ti,sysc-omap4-simple", .data = &sysc_omap4_simple, },
2325 { .compatible = "ti,sysc-omap3430-sr", .data = &sysc_34xx_sr, },
2326 { .compatible = "ti,sysc-omap3630-sr", .data = &sysc_36xx_sr, },
2327 { .compatible = "ti,sysc-omap4-sr", .data = &sysc_omap4_sr, },
2328 { .compatible = "ti,sysc-omap3-sham", .data = &sysc_omap3_sham, },
2329 { .compatible = "ti,sysc-omap-aes", .data = &sysc_omap3_aes, },
2330 { .compatible = "ti,sysc-mcasp", .data = &sysc_omap4_mcasp, },
Tony Lindgren2c63a832018-11-15 14:46:53 -08002331 { .compatible = "ti,sysc-dra7-mcasp", .data = &sysc_dra7_mcasp, },
Tony Lindgren70a65242017-12-15 09:41:09 -08002332 { .compatible = "ti,sysc-usb-host-fs",
2333 .data = &sysc_omap4_usb_host_fs, },
Faiz Abbas7f35e632018-07-09 22:18:38 +05302334 { .compatible = "ti,sysc-dra7-mcan", .data = &sysc_dra7_mcan, },
Tony Lindgren0eecc632017-10-10 14:23:43 -07002335 { },
2336};
2337MODULE_DEVICE_TABLE(of, sysc_match);
2338
2339static struct platform_driver sysc_driver = {
2340 .probe = sysc_probe,
Tony Lindgren684be5a2017-10-13 10:48:40 -07002341 .remove = sysc_remove,
Tony Lindgren0eecc632017-10-10 14:23:43 -07002342 .driver = {
2343 .name = "ti-sysc",
2344 .of_match_table = sysc_match,
2345 .pm = &sysc_pm_ops,
2346 },
2347};
Tony Lindgren2c355ff2018-02-22 13:58:03 -08002348
2349static int __init sysc_init(void)
2350{
2351 bus_register_notifier(&platform_bus_type, &sysc_nb);
2352
2353 return platform_driver_register(&sysc_driver);
2354}
2355module_init(sysc_init);
2356
2357static void __exit sysc_exit(void)
2358{
2359 bus_unregister_notifier(&platform_bus_type, &sysc_nb);
2360 platform_driver_unregister(&sysc_driver);
2361}
2362module_exit(sysc_exit);
Tony Lindgren0eecc632017-10-10 14:23:43 -07002363
2364MODULE_DESCRIPTION("TI sysc interconnect target driver");
2365MODULE_LICENSE("GPL v2");