blob: 2e7a6c490c887fd79f8add6204e211159a43b024 [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 Lindgren09dfe582018-04-16 10:25:52 -070050static const char * const clock_names[SYSC_ICK + 1] = { "fck", "ick", };
Tony Lindgren0eecc632017-10-10 14:23:43 -070051
Tony Lindgrenc5a2de92017-12-15 09:41:23 -080052#define SYSC_IDLEMODE_MASK 3
53#define SYSC_CLOCKACTIVITY_MASK 3
54
Tony Lindgren0eecc632017-10-10 14:23:43 -070055/**
56 * struct sysc - TI sysc interconnect target module registers and capabilities
57 * @dev: struct device pointer
58 * @module_pa: physical address of the interconnect target module
59 * @module_size: size of the interconnect target module
60 * @module_va: virtual address of the interconnect target module
61 * @offsets: register offsets from module base
62 * @clocks: clocks used by the interconnect target module
Tony Lindgren09dfe582018-04-16 10:25:52 -070063 * @clock_roles: clock role names for the found clocks
64 * @nr_clocks: number of clocks used by the interconnect target module
Tony Lindgren0eecc632017-10-10 14:23:43 -070065 * @legacy_mode: configured for legacy mode if set
Tony Lindgren70a65242017-12-15 09:41:09 -080066 * @cap: interconnect target module capabilities
67 * @cfg: interconnect target module configuration
Tony Lindgren566a9b052017-12-15 09:41:19 -080068 * @name: name if available
69 * @revision: interconnect target module revision
Tony Lindgren62020f22018-02-22 13:59:44 -080070 * @needs_resume: runtime resume needed on resume from suspend
Tony Lindgren0eecc632017-10-10 14:23:43 -070071 */
72struct sysc {
73 struct device *dev;
74 u64 module_pa;
75 u32 module_size;
76 void __iomem *module_va;
77 int offsets[SYSC_MAX_REGS];
Tony Lindgren09dfe582018-04-16 10:25:52 -070078 struct clk **clocks;
79 const char **clock_roles;
80 int nr_clocks;
Tony Lindgren50622362018-04-16 10:20:27 -070081 struct reset_control *rsts;
Tony Lindgren0eecc632017-10-10 14:23:43 -070082 const char *legacy_mode;
Tony Lindgren70a65242017-12-15 09:41:09 -080083 const struct sysc_capabilities *cap;
84 struct sysc_config cfg;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -080085 struct ti_sysc_cookie cookie;
Tony Lindgren566a9b052017-12-15 09:41:19 -080086 const char *name;
87 u32 revision;
Tony Lindgren62020f22018-02-22 13:59:44 -080088 bool enabled;
89 bool needs_resume;
Tony Lindgrena885f0f2018-02-22 14:03:48 -080090 bool child_needs_resume;
Tony Lindgren76f0f772018-02-23 08:28:45 -080091 struct delayed_work idle_work;
Tony Lindgren0eecc632017-10-10 14:23:43 -070092};
93
Tony Lindgren4014c082018-12-10 14:11:26 -080094static void sysc_parse_dts_quirks(struct sysc *ddata, struct device_node *np,
95 bool is_child);
96
Tony Lindgrenb7182b42019-03-21 11:00:21 -070097static void sysc_write(struct sysc *ddata, int offset, u32 value)
Faiz Abbas596e7952018-07-09 22:18:39 +053098{
99 writel_relaxed(value, ddata->module_va + offset);
100}
101
Tony Lindgren566a9b052017-12-15 09:41:19 -0800102static u32 sysc_read(struct sysc *ddata, int offset)
103{
104 if (ddata->cfg.quirks & SYSC_QUIRK_16BIT) {
105 u32 val;
106
107 val = readw_relaxed(ddata->module_va + offset);
108 val |= (readw_relaxed(ddata->module_va + offset + 4) << 16);
109
110 return val;
111 }
112
113 return readl_relaxed(ddata->module_va + offset);
114}
115
Tony Lindgren09dfe582018-04-16 10:25:52 -0700116static bool sysc_opt_clks_needed(struct sysc *ddata)
117{
118 return !!(ddata->cfg.quirks & SYSC_QUIRK_OPT_CLKS_NEEDED);
119}
120
Tony Lindgren0eecc632017-10-10 14:23:43 -0700121static u32 sysc_read_revision(struct sysc *ddata)
122{
Tony Lindgren566a9b052017-12-15 09:41:19 -0800123 int offset = ddata->offsets[SYSC_REVISION];
124
125 if (offset < 0)
126 return 0;
127
128 return sysc_read(ddata, offset);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700129}
130
Tony Lindgren09dfe582018-04-16 10:25:52 -0700131static int sysc_get_one_clock(struct sysc *ddata, const char *name)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700132{
Tony Lindgren09dfe582018-04-16 10:25:52 -0700133 int error, i, index = -ENODEV;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700134
Tony Lindgren09dfe582018-04-16 10:25:52 -0700135 if (!strncmp(clock_names[SYSC_FCK], name, 3))
136 index = SYSC_FCK;
137 else if (!strncmp(clock_names[SYSC_ICK], name, 3))
138 index = SYSC_ICK;
139
140 if (index < 0) {
141 for (i = SYSC_OPTFCK0; i < SYSC_MAX_CLOCKS; i++) {
Tony Lindgrenc97c8622018-05-18 07:54:44 -0700142 if (!ddata->clocks[i]) {
Tony Lindgren09dfe582018-04-16 10:25:52 -0700143 index = i;
144 break;
145 }
146 }
Tony Lindgren0eecc632017-10-10 14:23:43 -0700147 }
Tony Lindgren09dfe582018-04-16 10:25:52 -0700148
149 if (index < 0) {
150 dev_err(ddata->dev, "clock %s not added\n", name);
151 return index;
152 }
Tony Lindgren0eecc632017-10-10 14:23:43 -0700153
154 ddata->clocks[index] = devm_clk_get(ddata->dev, name);
155 if (IS_ERR(ddata->clocks[index])) {
156 if (PTR_ERR(ddata->clocks[index]) == -ENOENT)
157 return 0;
158
159 dev_err(ddata->dev, "clock get error for %s: %li\n",
160 name, PTR_ERR(ddata->clocks[index]));
161
162 return PTR_ERR(ddata->clocks[index]);
163 }
164
165 error = clk_prepare(ddata->clocks[index]);
166 if (error) {
167 dev_err(ddata->dev, "clock prepare error for %s: %i\n",
168 name, error);
169
170 return error;
171 }
172
173 return 0;
174}
175
176static int sysc_get_clocks(struct sysc *ddata)
177{
Tony Lindgren09dfe582018-04-16 10:25:52 -0700178 struct device_node *np = ddata->dev->of_node;
179 struct property *prop;
180 const char *name;
181 int nr_fck = 0, nr_ick = 0, i, error = 0;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700182
Kees Cook20749052018-06-18 10:45:49 -0700183 ddata->clock_roles = devm_kcalloc(ddata->dev,
Tony Lindgren09dfe582018-04-16 10:25:52 -0700184 SYSC_MAX_CLOCKS,
Kees Cook20749052018-06-18 10:45:49 -0700185 sizeof(*ddata->clock_roles),
Tony Lindgren09dfe582018-04-16 10:25:52 -0700186 GFP_KERNEL);
187 if (!ddata->clock_roles)
188 return -ENOMEM;
189
190 of_property_for_each_string(np, "clock-names", prop, name) {
191 if (!strncmp(clock_names[SYSC_FCK], name, 3))
192 nr_fck++;
193 if (!strncmp(clock_names[SYSC_ICK], name, 3))
194 nr_ick++;
195 ddata->clock_roles[ddata->nr_clocks] = name;
196 ddata->nr_clocks++;
197 }
198
199 if (ddata->nr_clocks < 1)
200 return 0;
201
202 if (ddata->nr_clocks > SYSC_MAX_CLOCKS) {
203 dev_err(ddata->dev, "too many clocks for %pOF\n", np);
204
205 return -EINVAL;
206 }
207
208 if (nr_fck > 1 || nr_ick > 1) {
209 dev_err(ddata->dev, "max one fck and ick for %pOF\n", np);
210
211 return -EINVAL;
212 }
213
Kees Cook20749052018-06-18 10:45:49 -0700214 ddata->clocks = devm_kcalloc(ddata->dev,
215 ddata->nr_clocks, sizeof(*ddata->clocks),
Tony Lindgren09dfe582018-04-16 10:25:52 -0700216 GFP_KERNEL);
217 if (!ddata->clocks)
218 return -ENOMEM;
219
Tony Lindgren7b4f8ac2018-11-15 14:46:52 -0800220 for (i = 0; i < SYSC_MAX_CLOCKS; i++) {
221 const char *name = ddata->clock_roles[i];
222
223 if (!name)
224 continue;
225
226 error = sysc_get_one_clock(ddata, name);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700227 if (error && error != -ENOENT)
228 return error;
229 }
230
231 return 0;
232}
233
Tony Lindgrend8789702019-03-21 11:00:21 -0700234static int sysc_enable_main_clocks(struct sysc *ddata)
235{
236 struct clk *clock;
237 int i, error;
238
239 if (!ddata->clocks)
240 return 0;
241
242 for (i = 0; i < SYSC_OPTFCK0; i++) {
243 clock = ddata->clocks[i];
244
245 /* Main clocks may not have ick */
246 if (IS_ERR_OR_NULL(clock))
247 continue;
248
249 error = clk_enable(clock);
250 if (error)
251 goto err_disable;
252 }
253
254 return 0;
255
256err_disable:
257 for (i--; i >= 0; i--) {
258 clock = ddata->clocks[i];
259
260 /* Main clocks may not have ick */
261 if (IS_ERR_OR_NULL(clock))
262 continue;
263
264 clk_disable(clock);
265 }
266
267 return error;
268}
269
270static void sysc_disable_main_clocks(struct sysc *ddata)
271{
272 struct clk *clock;
273 int i;
274
275 if (!ddata->clocks)
276 return;
277
278 for (i = 0; i < SYSC_OPTFCK0; i++) {
279 clock = ddata->clocks[i];
280 if (IS_ERR_OR_NULL(clock))
281 continue;
282
283 clk_disable(clock);
284 }
285}
286
287static int sysc_enable_opt_clocks(struct sysc *ddata)
288{
289 struct clk *clock;
290 int i, error;
291
292 if (!ddata->clocks)
293 return 0;
294
295 for (i = SYSC_OPTFCK0; i < SYSC_MAX_CLOCKS; i++) {
296 clock = ddata->clocks[i];
297
298 /* Assume no holes for opt clocks */
299 if (IS_ERR_OR_NULL(clock))
300 return 0;
301
302 error = clk_enable(clock);
303 if (error)
304 goto err_disable;
305 }
306
307 return 0;
308
309err_disable:
310 for (i--; i >= 0; i--) {
311 clock = ddata->clocks[i];
312 if (IS_ERR_OR_NULL(clock))
313 continue;
314
315 clk_disable(clock);
316 }
317
318 return error;
319}
320
321static void sysc_disable_opt_clocks(struct sysc *ddata)
322{
323 struct clk *clock;
324 int i;
325
326 if (!ddata->clocks)
327 return;
328
329 for (i = SYSC_OPTFCK0; i < SYSC_MAX_CLOCKS; i++) {
330 clock = ddata->clocks[i];
331
332 /* Assume no holes for opt clocks */
333 if (IS_ERR_OR_NULL(clock))
334 return;
335
336 clk_disable(clock);
337 }
338}
339
Tony Lindgren0eecc632017-10-10 14:23:43 -0700340/**
Tony Lindgren50622362018-04-16 10:20:27 -0700341 * sysc_init_resets - reset module on init
342 * @ddata: device driver data
343 *
344 * A module can have both OCP softreset control and external rstctrl.
345 * If more complicated rstctrl resets are needed, please handle these
346 * directly from the child device driver and map only the module reset
347 * for the parent interconnect target module device.
348 *
349 * Automatic reset of the module on init can be skipped with the
350 * "ti,no-reset-on-init" device tree property.
351 */
352static int sysc_init_resets(struct sysc *ddata)
353{
354 int error;
355
356 ddata->rsts =
357 devm_reset_control_array_get_optional_exclusive(ddata->dev);
358 if (IS_ERR(ddata->rsts))
359 return PTR_ERR(ddata->rsts);
360
361 if (ddata->cfg.quirks & SYSC_QUIRK_NO_RESET_ON_INIT)
362 goto deassert;
363
364 error = reset_control_assert(ddata->rsts);
365 if (error)
366 return error;
367
368deassert:
369 error = reset_control_deassert(ddata->rsts);
370 if (error)
371 return error;
372
373 return 0;
374}
375
376/**
Tony Lindgren0eecc632017-10-10 14:23:43 -0700377 * sysc_parse_and_check_child_range - parses module IO region from ranges
378 * @ddata: device driver data
379 *
380 * In general we only need rev, syss, and sysc registers and not the whole
381 * module range. But we do want the offsets for these registers from the
382 * module base. This allows us to check them against the legacy hwmod
383 * platform data. Let's also check the ranges are configured properly.
384 */
385static int sysc_parse_and_check_child_range(struct sysc *ddata)
386{
387 struct device_node *np = ddata->dev->of_node;
388 const __be32 *ranges;
389 u32 nr_addr, nr_size;
390 int len, error;
391
392 ranges = of_get_property(np, "ranges", &len);
393 if (!ranges) {
394 dev_err(ddata->dev, "missing ranges for %pOF\n", np);
395
396 return -ENOENT;
397 }
398
399 len /= sizeof(*ranges);
400
401 if (len < 3) {
402 dev_err(ddata->dev, "incomplete ranges for %pOF\n", np);
403
404 return -EINVAL;
405 }
406
407 error = of_property_read_u32(np, "#address-cells", &nr_addr);
408 if (error)
409 return -ENOENT;
410
411 error = of_property_read_u32(np, "#size-cells", &nr_size);
412 if (error)
413 return -ENOENT;
414
415 if (nr_addr != 1 || nr_size != 1) {
416 dev_err(ddata->dev, "invalid ranges for %pOF\n", np);
417
418 return -EINVAL;
419 }
420
421 ranges++;
422 ddata->module_pa = of_translate_address(np, ranges++);
423 ddata->module_size = be32_to_cpup(ranges);
424
Tony Lindgren0eecc632017-10-10 14:23:43 -0700425 return 0;
426}
427
Tony Lindgren3bb37c82018-02-22 14:05:14 -0800428static struct device_node *stdout_path;
429
430static void sysc_init_stdout_path(struct sysc *ddata)
431{
432 struct device_node *np = NULL;
433 const char *uart;
434
435 if (IS_ERR(stdout_path))
436 return;
437
438 if (stdout_path)
439 return;
440
441 np = of_find_node_by_path("/chosen");
442 if (!np)
443 goto err;
444
445 uart = of_get_property(np, "stdout-path", NULL);
446 if (!uart)
447 goto err;
448
449 np = of_find_node_by_path(uart);
450 if (!np)
451 goto err;
452
453 stdout_path = np;
454
455 return;
456
457err:
458 stdout_path = ERR_PTR(-ENODEV);
459}
460
461static void sysc_check_quirk_stdout(struct sysc *ddata,
462 struct device_node *np)
463{
464 sysc_init_stdout_path(ddata);
465 if (np != stdout_path)
466 return;
467
468 ddata->cfg.quirks |= SYSC_QUIRK_NO_IDLE_ON_INIT |
469 SYSC_QUIRK_NO_RESET_ON_INIT;
470}
471
Tony Lindgren0eecc632017-10-10 14:23:43 -0700472/**
473 * sysc_check_one_child - check child configuration
474 * @ddata: device driver data
475 * @np: child device node
476 *
477 * Let's avoid messy situations where we have new interconnect target
478 * node but children have "ti,hwmods". These belong to the interconnect
479 * target node and are managed by this driver.
480 */
481static int sysc_check_one_child(struct sysc *ddata,
482 struct device_node *np)
483{
484 const char *name;
485
486 name = of_get_property(np, "ti,hwmods", NULL);
487 if (name)
488 dev_warn(ddata->dev, "really a child ti,hwmods property?");
489
Tony Lindgren3bb37c82018-02-22 14:05:14 -0800490 sysc_check_quirk_stdout(ddata, np);
Tony Lindgren4014c082018-12-10 14:11:26 -0800491 sysc_parse_dts_quirks(ddata, np, true);
Tony Lindgren3bb37c82018-02-22 14:05:14 -0800492
Tony Lindgren0eecc632017-10-10 14:23:43 -0700493 return 0;
494}
495
496static int sysc_check_children(struct sysc *ddata)
497{
498 struct device_node *child;
499 int error;
500
501 for_each_child_of_node(ddata->dev->of_node, child) {
502 error = sysc_check_one_child(ddata, child);
503 if (error)
504 return error;
505 }
506
507 return 0;
508}
509
Tony Lindgrena7199e22017-12-15 09:41:14 -0800510/*
511 * So far only I2C uses 16-bit read access with clockactivity with revision
512 * in two registers with stride of 4. We can detect this based on the rev
513 * register size to configure things far enough to be able to properly read
514 * the revision register.
515 */
516static void sysc_check_quirk_16bit(struct sysc *ddata, struct resource *res)
517{
Tony Lindgrendd57ac12018-02-22 14:09:57 -0800518 if (resource_size(res) == 8)
Tony Lindgrena7199e22017-12-15 09:41:14 -0800519 ddata->cfg.quirks |= SYSC_QUIRK_16BIT | SYSC_QUIRK_USE_CLOCKACT;
Tony Lindgrena7199e22017-12-15 09:41:14 -0800520}
521
Tony Lindgren0eecc632017-10-10 14:23:43 -0700522/**
523 * sysc_parse_one - parses the interconnect target module registers
524 * @ddata: device driver data
525 * @reg: register to parse
526 */
527static int sysc_parse_one(struct sysc *ddata, enum sysc_registers reg)
528{
529 struct resource *res;
530 const char *name;
531
532 switch (reg) {
533 case SYSC_REVISION:
534 case SYSC_SYSCONFIG:
535 case SYSC_SYSSTATUS:
536 name = reg_names[reg];
537 break;
538 default:
539 return -EINVAL;
540 }
541
542 res = platform_get_resource_byname(to_platform_device(ddata->dev),
543 IORESOURCE_MEM, name);
544 if (!res) {
Tony Lindgren0eecc632017-10-10 14:23:43 -0700545 ddata->offsets[reg] = -ENODEV;
546
547 return 0;
548 }
549
550 ddata->offsets[reg] = res->start - ddata->module_pa;
Tony Lindgrena7199e22017-12-15 09:41:14 -0800551 if (reg == SYSC_REVISION)
552 sysc_check_quirk_16bit(ddata, res);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700553
554 return 0;
555}
556
557static int sysc_parse_registers(struct sysc *ddata)
558{
559 int i, error;
560
561 for (i = 0; i < SYSC_MAX_REGS; i++) {
562 error = sysc_parse_one(ddata, i);
563 if (error)
564 return error;
565 }
566
567 return 0;
568}
569
570/**
571 * sysc_check_registers - check for misconfigured register overlaps
572 * @ddata: device driver data
573 */
574static int sysc_check_registers(struct sysc *ddata)
575{
576 int i, j, nr_regs = 0, nr_matches = 0;
577
578 for (i = 0; i < SYSC_MAX_REGS; i++) {
579 if (ddata->offsets[i] < 0)
580 continue;
581
582 if (ddata->offsets[i] > (ddata->module_size - 4)) {
583 dev_err(ddata->dev, "register outside module range");
584
585 return -EINVAL;
586 }
587
588 for (j = 0; j < SYSC_MAX_REGS; j++) {
589 if (ddata->offsets[j] < 0)
590 continue;
591
592 if (ddata->offsets[i] == ddata->offsets[j])
593 nr_matches++;
594 }
595 nr_regs++;
596 }
597
598 if (nr_regs < 1) {
599 dev_err(ddata->dev, "missing registers\n");
600
601 return -EINVAL;
602 }
603
604 if (nr_matches > nr_regs) {
605 dev_err(ddata->dev, "overlapping registers: (%i/%i)",
606 nr_regs, nr_matches);
607
608 return -EINVAL;
609 }
610
611 return 0;
612}
613
614/**
615 * syc_ioremap - ioremap register space for the interconnect target module
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700616 * @ddata: device driver data
Tony Lindgren0eecc632017-10-10 14:23:43 -0700617 *
618 * Note that the interconnect target module registers can be anywhere
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700619 * within the interconnect target module range. For example, SGX has
620 * them at offset 0x1fc00 in the 32MB module address space. And cpsw
621 * has them at offset 0x1200 in the CPSW_WR child. Usually the
622 * the interconnect target module registers are at the beginning of
623 * the module range though.
Tony Lindgren0eecc632017-10-10 14:23:43 -0700624 */
625static int sysc_ioremap(struct sysc *ddata)
626{
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700627 int size;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700628
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700629 size = max3(ddata->offsets[SYSC_REVISION],
630 ddata->offsets[SYSC_SYSCONFIG],
631 ddata->offsets[SYSC_SYSSTATUS]);
632
633 if (size < 0 || (size + sizeof(u32)) > ddata->module_size)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700634 return -EINVAL;
635
Tony Lindgren0eecc632017-10-10 14:23:43 -0700636 ddata->module_va = devm_ioremap(ddata->dev,
637 ddata->module_pa,
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700638 size + sizeof(u32));
Tony Lindgren0eecc632017-10-10 14:23:43 -0700639 if (!ddata->module_va)
640 return -EIO;
641
642 return 0;
643}
644
645/**
646 * sysc_map_and_check_registers - ioremap and check device registers
647 * @ddata: device driver data
648 */
649static int sysc_map_and_check_registers(struct sysc *ddata)
650{
651 int error;
652
653 error = sysc_parse_and_check_child_range(ddata);
654 if (error)
655 return error;
656
657 error = sysc_check_children(ddata);
658 if (error)
659 return error;
660
661 error = sysc_parse_registers(ddata);
662 if (error)
663 return error;
664
665 error = sysc_ioremap(ddata);
666 if (error)
667 return error;
668
669 error = sysc_check_registers(ddata);
670 if (error)
671 return error;
672
673 return 0;
674}
675
676/**
677 * sysc_show_rev - read and show interconnect target module revision
678 * @bufp: buffer to print the information to
679 * @ddata: device driver data
680 */
681static int sysc_show_rev(char *bufp, struct sysc *ddata)
682{
Tony Lindgren566a9b052017-12-15 09:41:19 -0800683 int len;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700684
685 if (ddata->offsets[SYSC_REVISION] < 0)
686 return sprintf(bufp, ":NA");
687
Tony Lindgren566a9b052017-12-15 09:41:19 -0800688 len = sprintf(bufp, ":%08x", ddata->revision);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700689
690 return len;
691}
692
693static int sysc_show_reg(struct sysc *ddata,
694 char *bufp, enum sysc_registers reg)
695{
696 if (ddata->offsets[reg] < 0)
697 return sprintf(bufp, ":NA");
698
699 return sprintf(bufp, ":%x", ddata->offsets[reg]);
700}
701
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800702static int sysc_show_name(char *bufp, struct sysc *ddata)
703{
704 if (!ddata->name)
705 return 0;
706
707 return sprintf(bufp, ":%s", ddata->name);
708}
709
Tony Lindgren0eecc632017-10-10 14:23:43 -0700710/**
711 * sysc_show_registers - show information about interconnect target module
712 * @ddata: device driver data
713 */
714static void sysc_show_registers(struct sysc *ddata)
715{
716 char buf[128];
717 char *bufp = buf;
718 int i;
719
720 for (i = 0; i < SYSC_MAX_REGS; i++)
721 bufp += sysc_show_reg(ddata, bufp, i);
722
723 bufp += sysc_show_rev(bufp, ddata);
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800724 bufp += sysc_show_name(bufp, ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700725
726 dev_dbg(ddata->dev, "%llx:%x%s\n",
727 ddata->module_pa, ddata->module_size,
728 buf);
729}
730
Tony Lindgrenff437282019-03-21 11:00:21 -0700731static int __maybe_unused sysc_runtime_suspend_legacy(struct device *dev,
732 struct sysc *ddata)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700733{
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800734 struct ti_sysc_platform_data *pdata;
Tony Lindgrenff437282019-03-21 11:00:21 -0700735 int error;
736
737 pdata = dev_get_platdata(ddata->dev);
738 if (!pdata)
739 return 0;
740
741 if (!pdata->idle_module)
742 return -ENODEV;
743
744 error = pdata->idle_module(dev, &ddata->cookie);
745 if (error)
746 dev_err(dev, "%s: could not idle: %i\n",
747 __func__, error);
748
749 return 0;
750}
751
752static int __maybe_unused sysc_runtime_resume_legacy(struct device *dev,
753 struct sysc *ddata)
754{
755 struct ti_sysc_platform_data *pdata;
756 int error;
757
758 pdata = dev_get_platdata(ddata->dev);
759 if (!pdata)
760 return 0;
761
762 if (!pdata->enable_module)
763 return -ENODEV;
764
765 error = pdata->enable_module(dev, &ddata->cookie);
766 if (error)
767 dev_err(dev, "%s: could not enable: %i\n",
768 __func__, error);
769
770 return 0;
771}
772
773static int __maybe_unused sysc_runtime_suspend(struct device *dev)
774{
Tony Lindgren0eecc632017-10-10 14:23:43 -0700775 struct sysc *ddata;
Tony Lindgrend8789702019-03-21 11:00:21 -0700776 int error = 0;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700777
778 ddata = dev_get_drvdata(dev);
779
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800780 if (!ddata->enabled)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700781 return 0;
782
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800783 if (ddata->legacy_mode) {
Tony Lindgrenff437282019-03-21 11:00:21 -0700784 error = sysc_runtime_suspend_legacy(dev, ddata);
785 if (!error)
786 ddata->enabled = false;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800787
Tony Lindgrenff437282019-03-21 11:00:21 -0700788 return error;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800789 }
790
Tony Lindgrend8789702019-03-21 11:00:21 -0700791 sysc_disable_main_clocks(ddata);
Tony Lindgren09dfe582018-04-16 10:25:52 -0700792
Tony Lindgrend8789702019-03-21 11:00:21 -0700793 if (sysc_opt_clks_needed(ddata))
794 sysc_disable_opt_clocks(ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700795
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800796 ddata->enabled = false;
797
798 return error;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700799}
800
Arnd Bergmanna4a5d492017-10-13 11:25:32 +0200801static int __maybe_unused sysc_runtime_resume(struct device *dev)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700802{
803 struct sysc *ddata;
Tony Lindgrend8789702019-03-21 11:00:21 -0700804 int error = 0;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700805
806 ddata = dev_get_drvdata(dev);
807
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800808 if (ddata->enabled)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700809 return 0;
810
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800811 if (ddata->legacy_mode) {
Tony Lindgrenff437282019-03-21 11:00:21 -0700812 error = sysc_runtime_resume_legacy(dev, ddata);
813 if (!error)
814 ddata->enabled = true;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800815
Tony Lindgrenff437282019-03-21 11:00:21 -0700816 return error;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800817 }
818
Tony Lindgrend8789702019-03-21 11:00:21 -0700819 if (sysc_opt_clks_needed(ddata)) {
820 error = sysc_enable_opt_clocks(ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700821 if (error)
822 return error;
823 }
824
Tony Lindgrend8789702019-03-21 11:00:21 -0700825 error = sysc_enable_main_clocks(ddata);
826 if (error)
827 goto err_main_clocks;
828
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800829 ddata->enabled = true;
830
Tony Lindgrend8789702019-03-21 11:00:21 -0700831 return 0;
832
833err_main_clocks:
834 if (sysc_opt_clks_needed(ddata))
835 sysc_disable_opt_clocks(ddata);
836
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800837 return error;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700838}
839
Tony Lindgrenf5e802032018-10-01 09:33:28 -0700840static int __maybe_unused sysc_noirq_suspend(struct device *dev)
Tony Lindgren62020f22018-02-22 13:59:44 -0800841{
842 struct sysc *ddata;
843
844 ddata = dev_get_drvdata(dev);
845
Tony Lindgren40d9f912018-09-24 12:16:54 -0700846 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
Tony Lindgrene7420c22018-04-16 10:26:46 -0700847 return 0;
848
Tony Lindgrenf5e802032018-10-01 09:33:28 -0700849 return pm_runtime_force_suspend(dev);
Tony Lindgren62020f22018-02-22 13:59:44 -0800850}
851
Tony Lindgrenf5e802032018-10-01 09:33:28 -0700852static int __maybe_unused sysc_noirq_resume(struct device *dev)
Tony Lindgren62020f22018-02-22 13:59:44 -0800853{
854 struct sysc *ddata;
855
856 ddata = dev_get_drvdata(dev);
Tony Lindgrene7420c22018-04-16 10:26:46 -0700857
Tony Lindgren40d9f912018-09-24 12:16:54 -0700858 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
Tony Lindgrene7420c22018-04-16 10:26:46 -0700859 return 0;
860
Tony Lindgrenf5e802032018-10-01 09:33:28 -0700861 return pm_runtime_force_resume(dev);
Tony Lindgrene7420c22018-04-16 10:26:46 -0700862}
863
Tony Lindgren0eecc632017-10-10 14:23:43 -0700864static const struct dev_pm_ops sysc_pm_ops = {
Tony Lindgrene7420c22018-04-16 10:26:46 -0700865 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(sysc_noirq_suspend, sysc_noirq_resume)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700866 SET_RUNTIME_PM_OPS(sysc_runtime_suspend,
867 sysc_runtime_resume,
868 NULL)
869};
870
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800871/* Module revision register based quirks */
872struct sysc_revision_quirk {
873 const char *name;
874 u32 base;
875 int rev_offset;
876 int sysc_offset;
877 int syss_offset;
878 u32 revision;
879 u32 revision_mask;
880 u32 quirks;
881};
882
883#define SYSC_QUIRK(optname, optbase, optrev, optsysc, optsyss, \
884 optrev_val, optrevmask, optquirkmask) \
885 { \
886 .name = (optname), \
887 .base = (optbase), \
888 .rev_offset = (optrev), \
889 .sysc_offset = (optsysc), \
890 .syss_offset = (optsyss), \
891 .revision = (optrev_val), \
892 .revision_mask = (optrevmask), \
893 .quirks = (optquirkmask), \
894 }
895
896static const struct sysc_revision_quirk sysc_revision_quirks[] = {
897 /* These drivers need to be fixed to not use pm_runtime_irq_safe() */
Tony Lindgren3a3d8022018-09-27 13:33:58 -0700898 SYSC_QUIRK("gpio", 0, 0, 0x10, 0x114, 0x50600801, 0xffff00ff,
Tony Lindgren09dfe582018-04-16 10:25:52 -0700899 SYSC_QUIRK_LEGACY_IDLE | SYSC_QUIRK_OPT_CLKS_IN_RESET),
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800900 SYSC_QUIRK("mmu", 0, 0, 0x10, 0x14, 0x00000020, 0xffffffff,
901 SYSC_QUIRK_LEGACY_IDLE),
902 SYSC_QUIRK("mmu", 0, 0, 0x10, 0x14, 0x00000030, 0xffffffff,
903 SYSC_QUIRK_LEGACY_IDLE),
904 SYSC_QUIRK("sham", 0, 0x100, 0x110, 0x114, 0x40000c03, 0xffffffff,
905 SYSC_QUIRK_LEGACY_IDLE),
906 SYSC_QUIRK("smartreflex", 0, -1, 0x24, -1, 0x00000000, 0xffffffff,
907 SYSC_QUIRK_LEGACY_IDLE),
908 SYSC_QUIRK("smartreflex", 0, -1, 0x38, -1, 0x00000000, 0xffffffff,
909 SYSC_QUIRK_LEGACY_IDLE),
910 SYSC_QUIRK("timer", 0, 0, 0x10, 0x14, 0x00000015, 0xffffffff,
Tony Lindgren9bd34c62019-01-22 09:03:08 -0800911 0),
Tony Lindgren8cde5d52018-04-16 10:25:15 -0700912 /* Some timers on omap4 and later */
Tony Lindgren3a3d8022018-09-27 13:33:58 -0700913 SYSC_QUIRK("timer", 0, 0, 0x10, -1, 0x50002100, 0xffffffff,
Tony Lindgren072167d2019-01-29 07:53:47 -0800914 0),
Tony Lindgren3a3d8022018-09-27 13:33:58 -0700915 SYSC_QUIRK("timer", 0, 0, 0x10, -1, 0x4fff1301, 0xffff00ff,
Tony Lindgren9bd34c62019-01-22 09:03:08 -0800916 0),
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800917 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x00000052, 0xffffffff,
918 SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgrend708bb12018-04-16 10:25:32 -0700919 /* Uarts on omap4 and later */
Tony Lindgrenb82beef2018-09-24 12:16:59 -0700920 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x50411e03, 0xffff00ff,
921 SYSC_QUIRK_LEGACY_IDLE),
922 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x47422e03, 0xffffffff,
Tony Lindgrend708bb12018-04-16 10:25:32 -0700923 SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgren7e27e5d2018-04-20 14:54:16 -0700924
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700925#ifdef DEBUG
Tony Lindgren1ba30692018-09-24 12:17:05 -0700926 SYSC_QUIRK("adc", 0, 0, 0x10, -1, 0x47300001, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -0700927 SYSC_QUIRK("atl", 0, 0, -1, -1, 0x0a070100, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700928 SYSC_QUIRK("aess", 0, 0, 0x10, -1, 0x40000000, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -0700929 SYSC_QUIRK("cm", 0, 0, -1, -1, 0x40000301, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -0700930 SYSC_QUIRK("control", 0, 0, 0x10, -1, 0x40000900, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700931 SYSC_QUIRK("cpgmac", 0, 0x1200, 0x1208, 0x1204, 0x4edb1902,
Tony Lindgren23731ea2018-09-24 12:17:09 -0700932 0xffff00f0, 0),
933 SYSC_QUIRK("dcan", 0, 0, -1, -1, 0xffffffff, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700934 SYSC_QUIRK("dcan", 0, 0, -1, -1, 0x00001401, 0xffffffff, 0),
935 SYSC_QUIRK("dwc3", 0, 0, 0x10, -1, 0x500a0200, 0xffffffff, 0),
936 SYSC_QUIRK("epwmss", 0, 0, 0x4, -1, 0x47400001, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700937 SYSC_QUIRK("gpu", 0, 0x1fc00, 0x1fc10, -1, 0, 0, 0),
938 SYSC_QUIRK("hdq1w", 0, 0, 0x14, 0x18, 0x00000006, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700939 SYSC_QUIRK("hdq1w", 0, 0, 0x14, 0x18, 0x0000000a, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700940 SYSC_QUIRK("hsi", 0, 0, 0x10, 0x14, 0x50043101, 0xffffffff, 0),
941 SYSC_QUIRK("iss", 0, 0, 0x10, -1, 0x40000101, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -0700942 SYSC_QUIRK("i2c", 0, 0, 0x10, 0x90, 0x5040000a, 0xfffff0f0, 0),
Tony Lindgren23731ea2018-09-24 12:17:09 -0700943 SYSC_QUIRK("lcdc", 0, 0, 0x54, -1, 0x4f201000, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700944 SYSC_QUIRK("mcasp", 0, 0, 0x4, -1, 0x44306302, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700945 SYSC_QUIRK("mcasp", 0, 0, 0x4, -1, 0x44307b02, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700946 SYSC_QUIRK("mcbsp", 0, -1, 0x8c, -1, 0, 0, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -0700947 SYSC_QUIRK("mcspi", 0, 0, 0x10, -1, 0x40300a0b, 0xffff00ff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700948 SYSC_QUIRK("mcspi", 0, 0, 0x110, 0x114, 0x40300a0b, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700949 SYSC_QUIRK("mailbox", 0, 0, 0x10, -1, 0x00000400, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700950 SYSC_QUIRK("m3", 0, 0, -1, -1, 0x5f580105, 0x0fff0f00, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -0700951 SYSC_QUIRK("ocp2scp", 0, 0, 0x10, 0x14, 0x50060005, 0xfffffff0, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700952 SYSC_QUIRK("ocp2scp", 0, 0, -1, -1, 0x50060007, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -0700953 SYSC_QUIRK("padconf", 0, 0, 0x10, -1, 0x4fff0800, 0xffffffff, 0),
Tony Lindgrenf0106702018-11-15 14:46:53 -0800954 SYSC_QUIRK("padconf", 0, 0, -1, -1, 0x40001100, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -0700955 SYSC_QUIRK("prcm", 0, 0, -1, -1, 0x40000100, 0xffffffff, 0),
Tony Lindgren23731ea2018-09-24 12:17:09 -0700956 SYSC_QUIRK("prcm", 0, 0, -1, -1, 0x00004102, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700957 SYSC_QUIRK("prcm", 0, 0, -1, -1, 0x40000400, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -0700958 SYSC_QUIRK("scm", 0, 0, 0x10, -1, 0x40000900, 0xffffffff, 0),
Tony Lindgren23731ea2018-09-24 12:17:09 -0700959 SYSC_QUIRK("scm", 0, 0, -1, -1, 0x4e8b0100, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700960 SYSC_QUIRK("scm", 0, 0, -1, -1, 0x4f000100, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -0700961 SYSC_QUIRK("scm", 0, 0, -1, -1, 0x40000900, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -0700962 SYSC_QUIRK("scrm", 0, 0, -1, -1, 0x00000010, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -0700963 SYSC_QUIRK("sdio", 0, 0, 0x10, -1, 0x40202301, 0xffff0ff0, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700964 SYSC_QUIRK("sdio", 0, 0x2fc, 0x110, 0x114, 0x31010000, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -0700965 SYSC_QUIRK("sdma", 0, 0, 0x2c, 0x28, 0x00010900, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700966 SYSC_QUIRK("slimbus", 0, 0, 0x10, -1, 0x40000902, 0xffffffff, 0),
967 SYSC_QUIRK("slimbus", 0, 0, 0x10, -1, 0x40002903, 0xffffffff, 0),
968 SYSC_QUIRK("spinlock", 0, 0, 0x10, -1, 0x50020000, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700969 SYSC_QUIRK("rng", 0, 0x1fe0, 0x1fe4, -1, 0x00000020, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -0700970 SYSC_QUIRK("rtc", 0, 0x74, 0x78, -1, 0x4eb01908, 0xffff00f0, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700971 SYSC_QUIRK("timer32k", 0, 0, 0x4, -1, 0x00000060, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700972 SYSC_QUIRK("usbhstll", 0, 0, 0x10, 0x14, 0x00000004, 0xffffffff, 0),
Tony Lindgrenf0106702018-11-15 14:46:53 -0800973 SYSC_QUIRK("usbhstll", 0, 0, 0x10, 0x14, 0x00000008, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700974 SYSC_QUIRK("usb_host_hs", 0, 0, 0x10, 0x14, 0x50700100, 0xffffffff, 0),
Tony Lindgrenf0106702018-11-15 14:46:53 -0800975 SYSC_QUIRK("usb_host_hs", 0, 0, 0x10, -1, 0x50700101, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700976 SYSC_QUIRK("usb_otg_hs", 0, 0x400, 0x404, 0x408, 0x00000050,
977 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700978 SYSC_QUIRK("wdt", 0, 0, 0x10, 0x14, 0x502a0500, 0xfffff0f0, 0),
979 SYSC_QUIRK("vfpe", 0, 0, 0x104, -1, 0x4d001200, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700980#endif
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800981};
982
983static void sysc_init_revision_quirks(struct sysc *ddata)
984{
985 const struct sysc_revision_quirk *q;
986 int i;
987
988 for (i = 0; i < ARRAY_SIZE(sysc_revision_quirks); i++) {
989 q = &sysc_revision_quirks[i];
990
991 if (q->base && q->base != ddata->module_pa)
992 continue;
993
994 if (q->rev_offset >= 0 &&
995 q->rev_offset != ddata->offsets[SYSC_REVISION])
996 continue;
997
998 if (q->sysc_offset >= 0 &&
999 q->sysc_offset != ddata->offsets[SYSC_SYSCONFIG])
1000 continue;
1001
1002 if (q->syss_offset >= 0 &&
1003 q->syss_offset != ddata->offsets[SYSC_SYSSTATUS])
1004 continue;
1005
1006 if (q->revision == ddata->revision ||
1007 (q->revision & q->revision_mask) ==
1008 (ddata->revision & q->revision_mask)) {
1009 ddata->name = q->name;
1010 ddata->cfg.quirks |= q->quirks;
1011 }
1012 }
1013}
1014
Faiz Abbas596e7952018-07-09 22:18:39 +05301015static int sysc_reset(struct sysc *ddata)
1016{
1017 int offset = ddata->offsets[SYSC_SYSCONFIG];
1018 int val;
1019
1020 if (ddata->legacy_mode || offset < 0 ||
1021 ddata->cfg.quirks & SYSC_QUIRK_NO_RESET_ON_INIT)
1022 return 0;
1023
1024 /*
1025 * Currently only support reset status in sysstatus.
1026 * Warn and return error in all other cases
1027 */
1028 if (!ddata->cfg.syss_mask) {
1029 dev_err(ddata->dev, "No ti,syss-mask. Reset failed\n");
1030 return -EINVAL;
1031 }
1032
1033 val = sysc_read(ddata, offset);
1034 val |= (0x1 << ddata->cap->regbits->srst_shift);
1035 sysc_write(ddata, offset, val);
1036
1037 /* Poll on reset status */
1038 offset = ddata->offsets[SYSC_SYSSTATUS];
1039
1040 return readl_poll_timeout(ddata->module_va + offset, val,
1041 (val & ddata->cfg.syss_mask) == 0x0,
1042 100, MAX_MODULE_SOFTRESET_WAIT);
1043}
1044
Tony Lindgren566a9b052017-12-15 09:41:19 -08001045/* At this point the module is configured enough to read the revision */
1046static int sysc_init_module(struct sysc *ddata)
1047{
1048 int error;
1049
Tony Lindgren386cb762019-03-22 07:49:30 -07001050 if (ddata->cfg.quirks &
1051 (SYSC_QUIRK_NO_IDLE | SYSC_QUIRK_NO_IDLE_ON_INIT)) {
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001052 ddata->revision = sysc_read_revision(ddata);
1053 goto rev_quirks;
1054 }
1055
Tony Lindgren566a9b052017-12-15 09:41:19 -08001056 error = pm_runtime_get_sync(ddata->dev);
1057 if (error < 0) {
1058 pm_runtime_put_noidle(ddata->dev);
1059
1060 return 0;
1061 }
Tony Lindgren50622362018-04-16 10:20:27 -07001062
Faiz Abbas596e7952018-07-09 22:18:39 +05301063 error = sysc_reset(ddata);
1064 if (error) {
1065 dev_err(ddata->dev, "Reset failed with %d\n", error);
1066 pm_runtime_put_sync(ddata->dev);
1067
1068 return error;
1069 }
1070
Tony Lindgren566a9b052017-12-15 09:41:19 -08001071 ddata->revision = sysc_read_revision(ddata);
1072 pm_runtime_put_sync(ddata->dev);
1073
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001074rev_quirks:
1075 sysc_init_revision_quirks(ddata);
1076
Tony Lindgren566a9b052017-12-15 09:41:19 -08001077 return 0;
1078}
1079
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08001080static int sysc_init_sysc_mask(struct sysc *ddata)
1081{
1082 struct device_node *np = ddata->dev->of_node;
1083 int error;
1084 u32 val;
1085
1086 error = of_property_read_u32(np, "ti,sysc-mask", &val);
1087 if (error)
1088 return 0;
1089
1090 if (val)
1091 ddata->cfg.sysc_val = val & ddata->cap->sysc_mask;
1092 else
1093 ddata->cfg.sysc_val = ddata->cap->sysc_mask;
1094
1095 return 0;
1096}
1097
1098static int sysc_init_idlemode(struct sysc *ddata, u8 *idlemodes,
1099 const char *name)
1100{
1101 struct device_node *np = ddata->dev->of_node;
1102 struct property *prop;
1103 const __be32 *p;
1104 u32 val;
1105
1106 of_property_for_each_u32(np, name, prop, p, val) {
1107 if (val >= SYSC_NR_IDLEMODES) {
1108 dev_err(ddata->dev, "invalid idlemode: %i\n", val);
1109 return -EINVAL;
1110 }
1111 *idlemodes |= (1 << val);
1112 }
1113
1114 return 0;
1115}
1116
1117static int sysc_init_idlemodes(struct sysc *ddata)
1118{
1119 int error;
1120
1121 error = sysc_init_idlemode(ddata, &ddata->cfg.midlemodes,
1122 "ti,sysc-midle");
1123 if (error)
1124 return error;
1125
1126 error = sysc_init_idlemode(ddata, &ddata->cfg.sidlemodes,
1127 "ti,sysc-sidle");
1128 if (error)
1129 return error;
1130
1131 return 0;
1132}
1133
1134/*
1135 * Only some devices on omap4 and later have SYSCONFIG reset done
1136 * bit. We can detect this if there is no SYSSTATUS at all, or the
1137 * SYSTATUS bit 0 is not used. Note that some SYSSTATUS registers
1138 * have multiple bits for the child devices like OHCI and EHCI.
1139 * Depends on SYSC being parsed first.
1140 */
1141static int sysc_init_syss_mask(struct sysc *ddata)
1142{
1143 struct device_node *np = ddata->dev->of_node;
1144 int error;
1145 u32 val;
1146
1147 error = of_property_read_u32(np, "ti,syss-mask", &val);
1148 if (error) {
1149 if ((ddata->cap->type == TI_SYSC_OMAP4 ||
1150 ddata->cap->type == TI_SYSC_OMAP4_TIMER) &&
1151 (ddata->cfg.sysc_val & SYSC_OMAP4_SOFTRESET))
1152 ddata->cfg.quirks |= SYSC_QUIRK_RESET_STATUS;
1153
1154 return 0;
1155 }
1156
1157 if (!(val & 1) && (ddata->cfg.sysc_val & SYSC_OMAP4_SOFTRESET))
1158 ddata->cfg.quirks |= SYSC_QUIRK_RESET_STATUS;
1159
1160 ddata->cfg.syss_mask = val;
1161
1162 return 0;
1163}
1164
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001165/*
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001166 * Many child device drivers need to have fck and opt clocks available
1167 * to get the clock rate for device internal configuration etc.
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001168 */
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001169static int sysc_child_add_named_clock(struct sysc *ddata,
1170 struct device *child,
1171 const char *name)
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001172{
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001173 struct clk *clk;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001174 struct clk_lookup *l;
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001175 int error = 0;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001176
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001177 if (!name)
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001178 return 0;
1179
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001180 clk = clk_get(child, name);
1181 if (!IS_ERR(clk)) {
1182 clk_put(clk);
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001183
1184 return -EEXIST;
1185 }
1186
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001187 clk = clk_get(ddata->dev, name);
1188 if (IS_ERR(clk))
1189 return -ENODEV;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001190
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001191 l = clkdev_create(clk, name, dev_name(child));
1192 if (!l)
1193 error = -ENOMEM;
1194
1195 clk_put(clk);
1196
1197 return error;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001198}
1199
Tony Lindgren09dfe582018-04-16 10:25:52 -07001200static int sysc_child_add_clocks(struct sysc *ddata,
1201 struct device *child)
1202{
1203 int i, error;
1204
1205 for (i = 0; i < ddata->nr_clocks; i++) {
1206 error = sysc_child_add_named_clock(ddata,
1207 child,
1208 ddata->clock_roles[i]);
1209 if (error && error != -EEXIST) {
1210 dev_err(ddata->dev, "could not add child clock %s: %i\n",
1211 ddata->clock_roles[i], error);
1212
1213 return error;
1214 }
1215 }
1216
1217 return 0;
1218}
1219
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001220static struct device_type sysc_device_type = {
1221};
1222
1223static struct sysc *sysc_child_to_parent(struct device *dev)
1224{
1225 struct device *parent = dev->parent;
1226
1227 if (!parent || parent->type != &sysc_device_type)
1228 return NULL;
1229
1230 return dev_get_drvdata(parent);
1231}
1232
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001233static int __maybe_unused sysc_child_runtime_suspend(struct device *dev)
1234{
1235 struct sysc *ddata;
1236 int error;
1237
1238 ddata = sysc_child_to_parent(dev);
1239
1240 error = pm_generic_runtime_suspend(dev);
1241 if (error)
1242 return error;
1243
1244 if (!ddata->enabled)
1245 return 0;
1246
1247 return sysc_runtime_suspend(ddata->dev);
1248}
1249
1250static int __maybe_unused sysc_child_runtime_resume(struct device *dev)
1251{
1252 struct sysc *ddata;
1253 int error;
1254
1255 ddata = sysc_child_to_parent(dev);
1256
1257 if (!ddata->enabled) {
1258 error = sysc_runtime_resume(ddata->dev);
1259 if (error < 0)
1260 dev_err(ddata->dev,
1261 "%s error: %i\n", __func__, error);
1262 }
1263
1264 return pm_generic_runtime_resume(dev);
1265}
1266
1267#ifdef CONFIG_PM_SLEEP
1268static int sysc_child_suspend_noirq(struct device *dev)
1269{
1270 struct sysc *ddata;
1271 int error;
1272
1273 ddata = sysc_child_to_parent(dev);
1274
Tony Lindgrenef55f822018-04-16 10:27:15 -07001275 dev_dbg(ddata->dev, "%s %s\n", __func__,
1276 ddata->name ? ddata->name : "");
1277
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001278 error = pm_generic_suspend_noirq(dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001279 if (error) {
1280 dev_err(dev, "%s error at %i: %i\n",
1281 __func__, __LINE__, error);
1282
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001283 return error;
Tony Lindgrenef55f822018-04-16 10:27:15 -07001284 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001285
1286 if (!pm_runtime_status_suspended(dev)) {
1287 error = pm_generic_runtime_suspend(dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001288 if (error) {
Tony Lindgrenf9490782018-09-28 15:21:50 -07001289 dev_dbg(dev, "%s busy at %i: %i\n",
1290 __func__, __LINE__, error);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001291
Tony Lindgren4f3530f2018-08-08 01:07:06 -07001292 return 0;
Tony Lindgrenef55f822018-04-16 10:27:15 -07001293 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001294
1295 error = sysc_runtime_suspend(ddata->dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001296 if (error) {
1297 dev_err(dev, "%s error at %i: %i\n",
1298 __func__, __LINE__, error);
1299
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001300 return error;
Tony Lindgrenef55f822018-04-16 10:27:15 -07001301 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001302
1303 ddata->child_needs_resume = true;
1304 }
1305
1306 return 0;
1307}
1308
1309static int sysc_child_resume_noirq(struct device *dev)
1310{
1311 struct sysc *ddata;
1312 int error;
1313
1314 ddata = sysc_child_to_parent(dev);
1315
Tony Lindgrenef55f822018-04-16 10:27:15 -07001316 dev_dbg(ddata->dev, "%s %s\n", __func__,
1317 ddata->name ? ddata->name : "");
1318
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001319 if (ddata->child_needs_resume) {
1320 ddata->child_needs_resume = false;
1321
1322 error = sysc_runtime_resume(ddata->dev);
1323 if (error)
1324 dev_err(ddata->dev,
1325 "%s runtime resume error: %i\n",
1326 __func__, error);
1327
1328 error = pm_generic_runtime_resume(dev);
1329 if (error)
1330 dev_err(ddata->dev,
1331 "%s generic runtime resume: %i\n",
1332 __func__, error);
1333 }
1334
1335 return pm_generic_resume_noirq(dev);
1336}
1337#endif
1338
Tony Lindgrenb7182b42019-03-21 11:00:21 -07001339static struct dev_pm_domain sysc_child_pm_domain = {
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001340 .ops = {
1341 SET_RUNTIME_PM_OPS(sysc_child_runtime_suspend,
1342 sysc_child_runtime_resume,
1343 NULL)
1344 USE_PLATFORM_PM_SLEEP_OPS
1345 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(sysc_child_suspend_noirq,
1346 sysc_child_resume_noirq)
1347 }
1348};
1349
1350/**
1351 * sysc_legacy_idle_quirk - handle children in omap_device compatible way
1352 * @ddata: device driver data
1353 * @child: child device driver
1354 *
1355 * Allow idle for child devices as done with _od_runtime_suspend().
1356 * Otherwise many child devices will not idle because of the permanent
1357 * parent usecount set in pm_runtime_irq_safe().
1358 *
1359 * Note that the long term solution is to just modify the child device
1360 * drivers to not set pm_runtime_irq_safe() and then this can be just
1361 * dropped.
1362 */
1363static void sysc_legacy_idle_quirk(struct sysc *ddata, struct device *child)
1364{
1365 if (!ddata->legacy_mode)
1366 return;
1367
1368 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
1369 dev_pm_domain_set(child, &sysc_child_pm_domain);
1370}
1371
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001372static int sysc_notifier_call(struct notifier_block *nb,
1373 unsigned long event, void *device)
1374{
1375 struct device *dev = device;
1376 struct sysc *ddata;
1377 int error;
1378
1379 ddata = sysc_child_to_parent(dev);
1380 if (!ddata)
1381 return NOTIFY_DONE;
1382
1383 switch (event) {
1384 case BUS_NOTIFY_ADD_DEVICE:
Tony Lindgren09dfe582018-04-16 10:25:52 -07001385 error = sysc_child_add_clocks(ddata, dev);
1386 if (error)
1387 return error;
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001388 sysc_legacy_idle_quirk(ddata, dev);
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001389 break;
1390 default:
1391 break;
1392 }
1393
1394 return NOTIFY_DONE;
1395}
1396
1397static struct notifier_block sysc_nb = {
1398 .notifier_call = sysc_notifier_call,
1399};
1400
Tony Lindgren566a9b052017-12-15 09:41:19 -08001401/* Device tree configured quirks */
1402struct sysc_dts_quirk {
1403 const char *name;
1404 u32 mask;
1405};
1406
1407static const struct sysc_dts_quirk sysc_dts_quirks[] = {
1408 { .name = "ti,no-idle-on-init",
1409 .mask = SYSC_QUIRK_NO_IDLE_ON_INIT, },
1410 { .name = "ti,no-reset-on-init",
1411 .mask = SYSC_QUIRK_NO_RESET_ON_INIT, },
Tony Lindgren386cb762019-03-22 07:49:30 -07001412 { .name = "ti,no-idle",
1413 .mask = SYSC_QUIRK_NO_IDLE, },
Tony Lindgren566a9b052017-12-15 09:41:19 -08001414};
1415
Tony Lindgren4014c082018-12-10 14:11:26 -08001416static void sysc_parse_dts_quirks(struct sysc *ddata, struct device_node *np,
1417 bool is_child)
Tony Lindgren566a9b052017-12-15 09:41:19 -08001418{
Tony Lindgren566a9b052017-12-15 09:41:19 -08001419 const struct property *prop;
Tony Lindgren4014c082018-12-10 14:11:26 -08001420 int i, len;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001421
1422 for (i = 0; i < ARRAY_SIZE(sysc_dts_quirks); i++) {
Tony Lindgren4014c082018-12-10 14:11:26 -08001423 const char *name = sysc_dts_quirks[i].name;
1424
1425 prop = of_get_property(np, name, &len);
Tony Lindgren566a9b052017-12-15 09:41:19 -08001426 if (!prop)
Tony Lindgrend39b6ea2018-02-15 09:18:55 -08001427 continue;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001428
1429 ddata->cfg.quirks |= sysc_dts_quirks[i].mask;
Tony Lindgren4014c082018-12-10 14:11:26 -08001430 if (is_child) {
1431 dev_warn(ddata->dev,
1432 "dts flag should be at module level for %s\n",
1433 name);
1434 }
Tony Lindgren566a9b052017-12-15 09:41:19 -08001435 }
Tony Lindgren4014c082018-12-10 14:11:26 -08001436}
Tony Lindgren566a9b052017-12-15 09:41:19 -08001437
Tony Lindgren4014c082018-12-10 14:11:26 -08001438static int sysc_init_dts_quirks(struct sysc *ddata)
1439{
1440 struct device_node *np = ddata->dev->of_node;
1441 int error;
1442 u32 val;
1443
1444 ddata->legacy_mode = of_get_property(np, "ti,hwmods", NULL);
1445
1446 sysc_parse_dts_quirks(ddata, np, false);
Tony Lindgren566a9b052017-12-15 09:41:19 -08001447 error = of_property_read_u32(np, "ti,sysc-delay-us", &val);
1448 if (!error) {
1449 if (val > 255) {
1450 dev_warn(ddata->dev, "bad ti,sysc-delay-us: %i\n",
1451 val);
1452 }
1453
1454 ddata->cfg.srst_udelay = (u8)val;
1455 }
1456
1457 return 0;
1458}
1459
Tony Lindgren0eecc632017-10-10 14:23:43 -07001460static void sysc_unprepare(struct sysc *ddata)
1461{
1462 int i;
1463
Tony Lindgrenaaa29bb2019-03-21 11:00:21 -07001464 if (!ddata->clocks)
1465 return;
1466
Tony Lindgren0eecc632017-10-10 14:23:43 -07001467 for (i = 0; i < SYSC_MAX_CLOCKS; i++) {
1468 if (!IS_ERR_OR_NULL(ddata->clocks[i]))
1469 clk_unprepare(ddata->clocks[i]);
1470 }
1471}
1472
Tony Lindgren70a65242017-12-15 09:41:09 -08001473/*
1474 * Common sysc register bits found on omap2, also known as type1
1475 */
1476static const struct sysc_regbits sysc_regbits_omap2 = {
1477 .dmadisable_shift = -ENODEV,
1478 .midle_shift = 12,
1479 .sidle_shift = 3,
1480 .clkact_shift = 8,
1481 .emufree_shift = 5,
1482 .enwkup_shift = 2,
1483 .srst_shift = 1,
1484 .autoidle_shift = 0,
1485};
1486
1487static const struct sysc_capabilities sysc_omap2 = {
1488 .type = TI_SYSC_OMAP2,
1489 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY | SYSC_OMAP2_EMUFREE |
1490 SYSC_OMAP2_ENAWAKEUP | SYSC_OMAP2_SOFTRESET |
1491 SYSC_OMAP2_AUTOIDLE,
1492 .regbits = &sysc_regbits_omap2,
1493};
1494
1495/* All omap2 and 3 timers, and timers 1, 2 & 10 on omap 4 and 5 */
1496static const struct sysc_capabilities sysc_omap2_timer = {
1497 .type = TI_SYSC_OMAP2_TIMER,
1498 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY | SYSC_OMAP2_EMUFREE |
1499 SYSC_OMAP2_ENAWAKEUP | SYSC_OMAP2_SOFTRESET |
1500 SYSC_OMAP2_AUTOIDLE,
1501 .regbits = &sysc_regbits_omap2,
1502 .mod_quirks = SYSC_QUIRK_USE_CLOCKACT,
1503};
1504
1505/*
1506 * SHAM2 (SHA1/MD5) sysc found on omap3, a variant of sysc_regbits_omap2
1507 * with different sidle position
1508 */
1509static const struct sysc_regbits sysc_regbits_omap3_sham = {
1510 .dmadisable_shift = -ENODEV,
1511 .midle_shift = -ENODEV,
1512 .sidle_shift = 4,
1513 .clkact_shift = -ENODEV,
1514 .enwkup_shift = -ENODEV,
1515 .srst_shift = 1,
1516 .autoidle_shift = 0,
1517 .emufree_shift = -ENODEV,
1518};
1519
1520static const struct sysc_capabilities sysc_omap3_sham = {
1521 .type = TI_SYSC_OMAP3_SHAM,
1522 .sysc_mask = SYSC_OMAP2_SOFTRESET | SYSC_OMAP2_AUTOIDLE,
1523 .regbits = &sysc_regbits_omap3_sham,
1524};
1525
1526/*
1527 * AES register bits found on omap3 and later, a variant of
1528 * sysc_regbits_omap2 with different sidle position
1529 */
1530static const struct sysc_regbits sysc_regbits_omap3_aes = {
1531 .dmadisable_shift = -ENODEV,
1532 .midle_shift = -ENODEV,
1533 .sidle_shift = 6,
1534 .clkact_shift = -ENODEV,
1535 .enwkup_shift = -ENODEV,
1536 .srst_shift = 1,
1537 .autoidle_shift = 0,
1538 .emufree_shift = -ENODEV,
1539};
1540
1541static const struct sysc_capabilities sysc_omap3_aes = {
1542 .type = TI_SYSC_OMAP3_AES,
1543 .sysc_mask = SYSC_OMAP2_SOFTRESET | SYSC_OMAP2_AUTOIDLE,
1544 .regbits = &sysc_regbits_omap3_aes,
1545};
1546
1547/*
1548 * Common sysc register bits found on omap4, also known as type2
1549 */
1550static const struct sysc_regbits sysc_regbits_omap4 = {
1551 .dmadisable_shift = 16,
1552 .midle_shift = 4,
1553 .sidle_shift = 2,
1554 .clkact_shift = -ENODEV,
1555 .enwkup_shift = -ENODEV,
1556 .emufree_shift = 1,
1557 .srst_shift = 0,
1558 .autoidle_shift = -ENODEV,
1559};
1560
1561static const struct sysc_capabilities sysc_omap4 = {
1562 .type = TI_SYSC_OMAP4,
1563 .sysc_mask = SYSC_OMAP4_DMADISABLE | SYSC_OMAP4_FREEEMU |
1564 SYSC_OMAP4_SOFTRESET,
1565 .regbits = &sysc_regbits_omap4,
1566};
1567
1568static const struct sysc_capabilities sysc_omap4_timer = {
1569 .type = TI_SYSC_OMAP4_TIMER,
1570 .sysc_mask = SYSC_OMAP4_DMADISABLE | SYSC_OMAP4_FREEEMU |
1571 SYSC_OMAP4_SOFTRESET,
1572 .regbits = &sysc_regbits_omap4,
1573};
1574
1575/*
1576 * Common sysc register bits found on omap4, also known as type3
1577 */
1578static const struct sysc_regbits sysc_regbits_omap4_simple = {
1579 .dmadisable_shift = -ENODEV,
1580 .midle_shift = 2,
1581 .sidle_shift = 0,
1582 .clkact_shift = -ENODEV,
1583 .enwkup_shift = -ENODEV,
1584 .srst_shift = -ENODEV,
1585 .emufree_shift = -ENODEV,
1586 .autoidle_shift = -ENODEV,
1587};
1588
1589static const struct sysc_capabilities sysc_omap4_simple = {
1590 .type = TI_SYSC_OMAP4_SIMPLE,
1591 .regbits = &sysc_regbits_omap4_simple,
1592};
1593
1594/*
1595 * SmartReflex sysc found on omap34xx
1596 */
1597static const struct sysc_regbits sysc_regbits_omap34xx_sr = {
1598 .dmadisable_shift = -ENODEV,
1599 .midle_shift = -ENODEV,
1600 .sidle_shift = -ENODEV,
1601 .clkact_shift = 20,
1602 .enwkup_shift = -ENODEV,
1603 .srst_shift = -ENODEV,
1604 .emufree_shift = -ENODEV,
1605 .autoidle_shift = -ENODEV,
1606};
1607
1608static const struct sysc_capabilities sysc_34xx_sr = {
1609 .type = TI_SYSC_OMAP34XX_SR,
1610 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY,
1611 .regbits = &sysc_regbits_omap34xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001612 .mod_quirks = SYSC_QUIRK_USE_CLOCKACT | SYSC_QUIRK_UNCACHED |
1613 SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08001614};
1615
1616/*
1617 * SmartReflex sysc found on omap36xx and later
1618 */
1619static const struct sysc_regbits sysc_regbits_omap36xx_sr = {
1620 .dmadisable_shift = -ENODEV,
1621 .midle_shift = -ENODEV,
1622 .sidle_shift = 24,
1623 .clkact_shift = -ENODEV,
1624 .enwkup_shift = 26,
1625 .srst_shift = -ENODEV,
1626 .emufree_shift = -ENODEV,
1627 .autoidle_shift = -ENODEV,
1628};
1629
1630static const struct sysc_capabilities sysc_36xx_sr = {
1631 .type = TI_SYSC_OMAP36XX_SR,
Tony Lindgren3267c082018-01-22 09:32:53 -08001632 .sysc_mask = SYSC_OMAP3_SR_ENAWAKEUP,
Tony Lindgren70a65242017-12-15 09:41:09 -08001633 .regbits = &sysc_regbits_omap36xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001634 .mod_quirks = SYSC_QUIRK_UNCACHED | SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08001635};
1636
1637static const struct sysc_capabilities sysc_omap4_sr = {
1638 .type = TI_SYSC_OMAP4_SR,
1639 .regbits = &sysc_regbits_omap36xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001640 .mod_quirks = SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08001641};
1642
1643/*
1644 * McASP register bits found on omap4 and later
1645 */
1646static const struct sysc_regbits sysc_regbits_omap4_mcasp = {
1647 .dmadisable_shift = -ENODEV,
1648 .midle_shift = -ENODEV,
1649 .sidle_shift = 0,
1650 .clkact_shift = -ENODEV,
1651 .enwkup_shift = -ENODEV,
1652 .srst_shift = -ENODEV,
1653 .emufree_shift = -ENODEV,
1654 .autoidle_shift = -ENODEV,
1655};
1656
1657static const struct sysc_capabilities sysc_omap4_mcasp = {
1658 .type = TI_SYSC_OMAP4_MCASP,
1659 .regbits = &sysc_regbits_omap4_mcasp,
Tony Lindgren2c63a832018-11-15 14:46:53 -08001660 .mod_quirks = SYSC_QUIRK_OPT_CLKS_NEEDED,
1661};
1662
1663/*
1664 * McASP found on dra7 and later
1665 */
1666static const struct sysc_capabilities sysc_dra7_mcasp = {
1667 .type = TI_SYSC_OMAP4_SIMPLE,
1668 .regbits = &sysc_regbits_omap4_simple,
1669 .mod_quirks = SYSC_QUIRK_OPT_CLKS_NEEDED,
Tony Lindgren70a65242017-12-15 09:41:09 -08001670};
1671
1672/*
1673 * FS USB host found on omap4 and later
1674 */
1675static const struct sysc_regbits sysc_regbits_omap4_usb_host_fs = {
1676 .dmadisable_shift = -ENODEV,
1677 .midle_shift = -ENODEV,
1678 .sidle_shift = 24,
1679 .clkact_shift = -ENODEV,
1680 .enwkup_shift = 26,
1681 .srst_shift = -ENODEV,
1682 .emufree_shift = -ENODEV,
1683 .autoidle_shift = -ENODEV,
1684};
1685
1686static const struct sysc_capabilities sysc_omap4_usb_host_fs = {
1687 .type = TI_SYSC_OMAP4_USB_HOST_FS,
1688 .sysc_mask = SYSC_OMAP2_ENAWAKEUP,
1689 .regbits = &sysc_regbits_omap4_usb_host_fs,
1690};
1691
Faiz Abbas7f35e632018-07-09 22:18:38 +05301692static const struct sysc_regbits sysc_regbits_dra7_mcan = {
1693 .dmadisable_shift = -ENODEV,
1694 .midle_shift = -ENODEV,
1695 .sidle_shift = -ENODEV,
1696 .clkact_shift = -ENODEV,
1697 .enwkup_shift = 4,
1698 .srst_shift = 0,
1699 .emufree_shift = -ENODEV,
1700 .autoidle_shift = -ENODEV,
1701};
1702
1703static const struct sysc_capabilities sysc_dra7_mcan = {
1704 .type = TI_SYSC_DRA7_MCAN,
1705 .sysc_mask = SYSC_DRA7_MCAN_ENAWAKEUP | SYSC_OMAP4_SOFTRESET,
1706 .regbits = &sysc_regbits_dra7_mcan,
1707};
1708
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001709static int sysc_init_pdata(struct sysc *ddata)
1710{
1711 struct ti_sysc_platform_data *pdata = dev_get_platdata(ddata->dev);
1712 struct ti_sysc_module_data mdata;
1713 int error = 0;
1714
1715 if (!pdata || !ddata->legacy_mode)
1716 return 0;
1717
1718 mdata.name = ddata->legacy_mode;
1719 mdata.module_pa = ddata->module_pa;
1720 mdata.module_size = ddata->module_size;
1721 mdata.offsets = ddata->offsets;
1722 mdata.nr_offsets = SYSC_MAX_REGS;
1723 mdata.cap = ddata->cap;
1724 mdata.cfg = &ddata->cfg;
1725
1726 if (!pdata->init_module)
1727 return -ENODEV;
1728
1729 error = pdata->init_module(ddata->dev, &mdata, &ddata->cookie);
1730 if (error == -EEXIST)
1731 error = 0;
1732
1733 return error;
1734}
1735
Tony Lindgren70a65242017-12-15 09:41:09 -08001736static int sysc_init_match(struct sysc *ddata)
1737{
1738 const struct sysc_capabilities *cap;
1739
1740 cap = of_device_get_match_data(ddata->dev);
1741 if (!cap)
1742 return -EINVAL;
1743
1744 ddata->cap = cap;
1745 if (ddata->cap)
1746 ddata->cfg.quirks |= ddata->cap->mod_quirks;
1747
1748 return 0;
1749}
1750
Tony Lindgren76f0f772018-02-23 08:28:45 -08001751static void ti_sysc_idle(struct work_struct *work)
1752{
1753 struct sysc *ddata;
1754
1755 ddata = container_of(work, struct sysc, idle_work.work);
1756
1757 if (pm_runtime_active(ddata->dev))
1758 pm_runtime_put_sync(ddata->dev);
1759}
1760
Tony Lindgrenc4bebea2018-04-16 10:24:54 -07001761static const struct of_device_id sysc_match_table[] = {
1762 { .compatible = "simple-bus", },
1763 { /* sentinel */ },
1764};
1765
Tony Lindgren0eecc632017-10-10 14:23:43 -07001766static int sysc_probe(struct platform_device *pdev)
1767{
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001768 struct ti_sysc_platform_data *pdata = dev_get_platdata(&pdev->dev);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001769 struct sysc *ddata;
1770 int error;
1771
1772 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
1773 if (!ddata)
1774 return -ENOMEM;
1775
1776 ddata->dev = &pdev->dev;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001777 platform_set_drvdata(pdev, ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001778
Tony Lindgren70a65242017-12-15 09:41:09 -08001779 error = sysc_init_match(ddata);
1780 if (error)
1781 return error;
1782
Tony Lindgren566a9b052017-12-15 09:41:19 -08001783 error = sysc_init_dts_quirks(ddata);
1784 if (error)
1785 goto unprepare;
1786
Tony Lindgren0eecc632017-10-10 14:23:43 -07001787 error = sysc_get_clocks(ddata);
1788 if (error)
1789 return error;
1790
1791 error = sysc_map_and_check_registers(ddata);
1792 if (error)
1793 goto unprepare;
1794
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08001795 error = sysc_init_sysc_mask(ddata);
1796 if (error)
1797 goto unprepare;
1798
1799 error = sysc_init_idlemodes(ddata);
1800 if (error)
1801 goto unprepare;
1802
1803 error = sysc_init_syss_mask(ddata);
1804 if (error)
1805 goto unprepare;
1806
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001807 error = sysc_init_pdata(ddata);
1808 if (error)
1809 goto unprepare;
1810
Tony Lindgren50622362018-04-16 10:20:27 -07001811 error = sysc_init_resets(ddata);
1812 if (error)
1813 return error;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001814
Tony Lindgren50622362018-04-16 10:20:27 -07001815 pm_runtime_enable(ddata->dev);
Tony Lindgren566a9b052017-12-15 09:41:19 -08001816 error = sysc_init_module(ddata);
1817 if (error)
1818 goto unprepare;
1819
Tony Lindgren0eecc632017-10-10 14:23:43 -07001820 error = pm_runtime_get_sync(ddata->dev);
1821 if (error < 0) {
1822 pm_runtime_put_noidle(ddata->dev);
1823 pm_runtime_disable(ddata->dev);
1824 goto unprepare;
1825 }
1826
Tony Lindgren0eecc632017-10-10 14:23:43 -07001827 sysc_show_registers(ddata);
1828
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001829 ddata->dev->type = &sysc_device_type;
Tony Lindgrenc4bebea2018-04-16 10:24:54 -07001830 error = of_platform_populate(ddata->dev->of_node, sysc_match_table,
1831 pdata ? pdata->auxdata : NULL,
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001832 ddata->dev);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001833 if (error)
1834 goto err;
1835
Tony Lindgren76f0f772018-02-23 08:28:45 -08001836 INIT_DELAYED_WORK(&ddata->idle_work, ti_sysc_idle);
1837
1838 /* At least earlycon won't survive without deferred idle */
1839 if (ddata->cfg.quirks & (SYSC_QUIRK_NO_IDLE_ON_INIT |
1840 SYSC_QUIRK_NO_RESET_ON_INIT)) {
1841 schedule_delayed_work(&ddata->idle_work, 3000);
1842 } else {
1843 pm_runtime_put(&pdev->dev);
1844 }
Tony Lindgren0eecc632017-10-10 14:23:43 -07001845
Tony Lindgren50622362018-04-16 10:20:27 -07001846 if (!of_get_available_child_count(ddata->dev->of_node))
1847 reset_control_assert(ddata->rsts);
1848
Tony Lindgren0eecc632017-10-10 14:23:43 -07001849 return 0;
1850
1851err:
Tony Lindgren0eecc632017-10-10 14:23:43 -07001852 pm_runtime_put_sync(&pdev->dev);
1853 pm_runtime_disable(&pdev->dev);
1854unprepare:
1855 sysc_unprepare(ddata);
1856
1857 return error;
1858}
1859
Tony Lindgren684be5a2017-10-13 10:48:40 -07001860static int sysc_remove(struct platform_device *pdev)
1861{
1862 struct sysc *ddata = platform_get_drvdata(pdev);
1863 int error;
1864
Tony Lindgren76f0f772018-02-23 08:28:45 -08001865 cancel_delayed_work_sync(&ddata->idle_work);
1866
Tony Lindgren684be5a2017-10-13 10:48:40 -07001867 error = pm_runtime_get_sync(ddata->dev);
1868 if (error < 0) {
1869 pm_runtime_put_noidle(ddata->dev);
1870 pm_runtime_disable(ddata->dev);
1871 goto unprepare;
1872 }
1873
1874 of_platform_depopulate(&pdev->dev);
1875
Tony Lindgren684be5a2017-10-13 10:48:40 -07001876 pm_runtime_put_sync(&pdev->dev);
1877 pm_runtime_disable(&pdev->dev);
Tony Lindgren50622362018-04-16 10:20:27 -07001878 reset_control_assert(ddata->rsts);
Tony Lindgren684be5a2017-10-13 10:48:40 -07001879
1880unprepare:
1881 sysc_unprepare(ddata);
1882
1883 return 0;
1884}
1885
Tony Lindgren0eecc632017-10-10 14:23:43 -07001886static const struct of_device_id sysc_match[] = {
Tony Lindgren70a65242017-12-15 09:41:09 -08001887 { .compatible = "ti,sysc-omap2", .data = &sysc_omap2, },
1888 { .compatible = "ti,sysc-omap2-timer", .data = &sysc_omap2_timer, },
1889 { .compatible = "ti,sysc-omap4", .data = &sysc_omap4, },
1890 { .compatible = "ti,sysc-omap4-timer", .data = &sysc_omap4_timer, },
1891 { .compatible = "ti,sysc-omap4-simple", .data = &sysc_omap4_simple, },
1892 { .compatible = "ti,sysc-omap3430-sr", .data = &sysc_34xx_sr, },
1893 { .compatible = "ti,sysc-omap3630-sr", .data = &sysc_36xx_sr, },
1894 { .compatible = "ti,sysc-omap4-sr", .data = &sysc_omap4_sr, },
1895 { .compatible = "ti,sysc-omap3-sham", .data = &sysc_omap3_sham, },
1896 { .compatible = "ti,sysc-omap-aes", .data = &sysc_omap3_aes, },
1897 { .compatible = "ti,sysc-mcasp", .data = &sysc_omap4_mcasp, },
Tony Lindgren2c63a832018-11-15 14:46:53 -08001898 { .compatible = "ti,sysc-dra7-mcasp", .data = &sysc_dra7_mcasp, },
Tony Lindgren70a65242017-12-15 09:41:09 -08001899 { .compatible = "ti,sysc-usb-host-fs",
1900 .data = &sysc_omap4_usb_host_fs, },
Faiz Abbas7f35e632018-07-09 22:18:38 +05301901 { .compatible = "ti,sysc-dra7-mcan", .data = &sysc_dra7_mcan, },
Tony Lindgren0eecc632017-10-10 14:23:43 -07001902 { },
1903};
1904MODULE_DEVICE_TABLE(of, sysc_match);
1905
1906static struct platform_driver sysc_driver = {
1907 .probe = sysc_probe,
Tony Lindgren684be5a2017-10-13 10:48:40 -07001908 .remove = sysc_remove,
Tony Lindgren0eecc632017-10-10 14:23:43 -07001909 .driver = {
1910 .name = "ti-sysc",
1911 .of_match_table = sysc_match,
1912 .pm = &sysc_pm_ops,
1913 },
1914};
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001915
1916static int __init sysc_init(void)
1917{
1918 bus_register_notifier(&platform_bus_type, &sysc_nb);
1919
1920 return platform_driver_register(&sysc_driver);
1921}
1922module_init(sysc_init);
1923
1924static void __exit sysc_exit(void)
1925{
1926 bus_unregister_notifier(&platform_bus_type, &sysc_nb);
1927 platform_driver_unregister(&sysc_driver);
1928}
1929module_exit(sysc_exit);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001930
1931MODULE_DESCRIPTION("TI sysc interconnect target driver");
1932MODULE_LICENSE("GPL v2");