blob: 46a6c528b543c62262bd6039453b4c806f07a06c [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
Faiz Abbas596e7952018-07-09 22:18:39 +053094void sysc_write(struct sysc *ddata, int offset, u32 value)
95{
96 writel_relaxed(value, ddata->module_va + offset);
97}
98
Tony Lindgren566a9b052017-12-15 09:41:19 -080099static u32 sysc_read(struct sysc *ddata, int offset)
100{
101 if (ddata->cfg.quirks & SYSC_QUIRK_16BIT) {
102 u32 val;
103
104 val = readw_relaxed(ddata->module_va + offset);
105 val |= (readw_relaxed(ddata->module_va + offset + 4) << 16);
106
107 return val;
108 }
109
110 return readl_relaxed(ddata->module_va + offset);
111}
112
Tony Lindgren09dfe582018-04-16 10:25:52 -0700113static bool sysc_opt_clks_needed(struct sysc *ddata)
114{
115 return !!(ddata->cfg.quirks & SYSC_QUIRK_OPT_CLKS_NEEDED);
116}
117
Tony Lindgren0eecc632017-10-10 14:23:43 -0700118static u32 sysc_read_revision(struct sysc *ddata)
119{
Tony Lindgren566a9b052017-12-15 09:41:19 -0800120 int offset = ddata->offsets[SYSC_REVISION];
121
122 if (offset < 0)
123 return 0;
124
125 return sysc_read(ddata, offset);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700126}
127
Tony Lindgren09dfe582018-04-16 10:25:52 -0700128static int sysc_get_one_clock(struct sysc *ddata, const char *name)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700129{
Tony Lindgren09dfe582018-04-16 10:25:52 -0700130 int error, i, index = -ENODEV;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700131
Tony Lindgren09dfe582018-04-16 10:25:52 -0700132 if (!strncmp(clock_names[SYSC_FCK], name, 3))
133 index = SYSC_FCK;
134 else if (!strncmp(clock_names[SYSC_ICK], name, 3))
135 index = SYSC_ICK;
136
137 if (index < 0) {
138 for (i = SYSC_OPTFCK0; i < SYSC_MAX_CLOCKS; i++) {
Tony Lindgrenc97c8622018-05-18 07:54:44 -0700139 if (!ddata->clocks[i]) {
Tony Lindgren09dfe582018-04-16 10:25:52 -0700140 index = i;
141 break;
142 }
143 }
Tony Lindgren0eecc632017-10-10 14:23:43 -0700144 }
Tony Lindgren09dfe582018-04-16 10:25:52 -0700145
146 if (index < 0) {
147 dev_err(ddata->dev, "clock %s not added\n", name);
148 return index;
149 }
Tony Lindgren0eecc632017-10-10 14:23:43 -0700150
151 ddata->clocks[index] = devm_clk_get(ddata->dev, name);
152 if (IS_ERR(ddata->clocks[index])) {
153 if (PTR_ERR(ddata->clocks[index]) == -ENOENT)
154 return 0;
155
156 dev_err(ddata->dev, "clock get error for %s: %li\n",
157 name, PTR_ERR(ddata->clocks[index]));
158
159 return PTR_ERR(ddata->clocks[index]);
160 }
161
162 error = clk_prepare(ddata->clocks[index]);
163 if (error) {
164 dev_err(ddata->dev, "clock prepare error for %s: %i\n",
165 name, error);
166
167 return error;
168 }
169
170 return 0;
171}
172
173static int sysc_get_clocks(struct sysc *ddata)
174{
Tony Lindgren09dfe582018-04-16 10:25:52 -0700175 struct device_node *np = ddata->dev->of_node;
176 struct property *prop;
177 const char *name;
178 int nr_fck = 0, nr_ick = 0, i, error = 0;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700179
Kees Cook20749052018-06-18 10:45:49 -0700180 ddata->clock_roles = devm_kcalloc(ddata->dev,
Tony Lindgren09dfe582018-04-16 10:25:52 -0700181 SYSC_MAX_CLOCKS,
Kees Cook20749052018-06-18 10:45:49 -0700182 sizeof(*ddata->clock_roles),
Tony Lindgren09dfe582018-04-16 10:25:52 -0700183 GFP_KERNEL);
184 if (!ddata->clock_roles)
185 return -ENOMEM;
186
187 of_property_for_each_string(np, "clock-names", prop, name) {
188 if (!strncmp(clock_names[SYSC_FCK], name, 3))
189 nr_fck++;
190 if (!strncmp(clock_names[SYSC_ICK], name, 3))
191 nr_ick++;
192 ddata->clock_roles[ddata->nr_clocks] = name;
193 ddata->nr_clocks++;
194 }
195
196 if (ddata->nr_clocks < 1)
197 return 0;
198
199 if (ddata->nr_clocks > SYSC_MAX_CLOCKS) {
200 dev_err(ddata->dev, "too many clocks for %pOF\n", np);
201
202 return -EINVAL;
203 }
204
205 if (nr_fck > 1 || nr_ick > 1) {
206 dev_err(ddata->dev, "max one fck and ick for %pOF\n", np);
207
208 return -EINVAL;
209 }
210
Kees Cook20749052018-06-18 10:45:49 -0700211 ddata->clocks = devm_kcalloc(ddata->dev,
212 ddata->nr_clocks, sizeof(*ddata->clocks),
Tony Lindgren09dfe582018-04-16 10:25:52 -0700213 GFP_KERNEL);
214 if (!ddata->clocks)
215 return -ENOMEM;
216
Tony Lindgren7b4f8ac2018-11-15 14:46:52 -0800217 for (i = 0; i < SYSC_MAX_CLOCKS; i++) {
218 const char *name = ddata->clock_roles[i];
219
220 if (!name)
221 continue;
222
223 error = sysc_get_one_clock(ddata, name);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700224 if (error && error != -ENOENT)
225 return error;
226 }
227
228 return 0;
229}
230
231/**
Tony Lindgren50622362018-04-16 10:20:27 -0700232 * sysc_init_resets - reset module on init
233 * @ddata: device driver data
234 *
235 * A module can have both OCP softreset control and external rstctrl.
236 * If more complicated rstctrl resets are needed, please handle these
237 * directly from the child device driver and map only the module reset
238 * for the parent interconnect target module device.
239 *
240 * Automatic reset of the module on init can be skipped with the
241 * "ti,no-reset-on-init" device tree property.
242 */
243static int sysc_init_resets(struct sysc *ddata)
244{
245 int error;
246
247 ddata->rsts =
248 devm_reset_control_array_get_optional_exclusive(ddata->dev);
249 if (IS_ERR(ddata->rsts))
250 return PTR_ERR(ddata->rsts);
251
252 if (ddata->cfg.quirks & SYSC_QUIRK_NO_RESET_ON_INIT)
253 goto deassert;
254
255 error = reset_control_assert(ddata->rsts);
256 if (error)
257 return error;
258
259deassert:
260 error = reset_control_deassert(ddata->rsts);
261 if (error)
262 return error;
263
264 return 0;
265}
266
267/**
Tony Lindgren0eecc632017-10-10 14:23:43 -0700268 * sysc_parse_and_check_child_range - parses module IO region from ranges
269 * @ddata: device driver data
270 *
271 * In general we only need rev, syss, and sysc registers and not the whole
272 * module range. But we do want the offsets for these registers from the
273 * module base. This allows us to check them against the legacy hwmod
274 * platform data. Let's also check the ranges are configured properly.
275 */
276static int sysc_parse_and_check_child_range(struct sysc *ddata)
277{
278 struct device_node *np = ddata->dev->of_node;
279 const __be32 *ranges;
280 u32 nr_addr, nr_size;
281 int len, error;
282
283 ranges = of_get_property(np, "ranges", &len);
284 if (!ranges) {
285 dev_err(ddata->dev, "missing ranges for %pOF\n", np);
286
287 return -ENOENT;
288 }
289
290 len /= sizeof(*ranges);
291
292 if (len < 3) {
293 dev_err(ddata->dev, "incomplete ranges for %pOF\n", np);
294
295 return -EINVAL;
296 }
297
298 error = of_property_read_u32(np, "#address-cells", &nr_addr);
299 if (error)
300 return -ENOENT;
301
302 error = of_property_read_u32(np, "#size-cells", &nr_size);
303 if (error)
304 return -ENOENT;
305
306 if (nr_addr != 1 || nr_size != 1) {
307 dev_err(ddata->dev, "invalid ranges for %pOF\n", np);
308
309 return -EINVAL;
310 }
311
312 ranges++;
313 ddata->module_pa = of_translate_address(np, ranges++);
314 ddata->module_size = be32_to_cpup(ranges);
315
Tony Lindgren0eecc632017-10-10 14:23:43 -0700316 return 0;
317}
318
Tony Lindgren3bb37c82018-02-22 14:05:14 -0800319static struct device_node *stdout_path;
320
321static void sysc_init_stdout_path(struct sysc *ddata)
322{
323 struct device_node *np = NULL;
324 const char *uart;
325
326 if (IS_ERR(stdout_path))
327 return;
328
329 if (stdout_path)
330 return;
331
332 np = of_find_node_by_path("/chosen");
333 if (!np)
334 goto err;
335
336 uart = of_get_property(np, "stdout-path", NULL);
337 if (!uart)
338 goto err;
339
340 np = of_find_node_by_path(uart);
341 if (!np)
342 goto err;
343
344 stdout_path = np;
345
346 return;
347
348err:
349 stdout_path = ERR_PTR(-ENODEV);
350}
351
352static void sysc_check_quirk_stdout(struct sysc *ddata,
353 struct device_node *np)
354{
355 sysc_init_stdout_path(ddata);
356 if (np != stdout_path)
357 return;
358
359 ddata->cfg.quirks |= SYSC_QUIRK_NO_IDLE_ON_INIT |
360 SYSC_QUIRK_NO_RESET_ON_INIT;
361}
362
Tony Lindgren0eecc632017-10-10 14:23:43 -0700363/**
364 * sysc_check_one_child - check child configuration
365 * @ddata: device driver data
366 * @np: child device node
367 *
368 * Let's avoid messy situations where we have new interconnect target
369 * node but children have "ti,hwmods". These belong to the interconnect
370 * target node and are managed by this driver.
371 */
372static int sysc_check_one_child(struct sysc *ddata,
373 struct device_node *np)
374{
375 const char *name;
376
377 name = of_get_property(np, "ti,hwmods", NULL);
378 if (name)
379 dev_warn(ddata->dev, "really a child ti,hwmods property?");
380
Tony Lindgren3bb37c82018-02-22 14:05:14 -0800381 sysc_check_quirk_stdout(ddata, np);
382
Tony Lindgren0eecc632017-10-10 14:23:43 -0700383 return 0;
384}
385
386static int sysc_check_children(struct sysc *ddata)
387{
388 struct device_node *child;
389 int error;
390
391 for_each_child_of_node(ddata->dev->of_node, child) {
392 error = sysc_check_one_child(ddata, child);
393 if (error)
394 return error;
395 }
396
397 return 0;
398}
399
Tony Lindgrena7199e22017-12-15 09:41:14 -0800400/*
401 * So far only I2C uses 16-bit read access with clockactivity with revision
402 * in two registers with stride of 4. We can detect this based on the rev
403 * register size to configure things far enough to be able to properly read
404 * the revision register.
405 */
406static void sysc_check_quirk_16bit(struct sysc *ddata, struct resource *res)
407{
Tony Lindgrendd57ac12018-02-22 14:09:57 -0800408 if (resource_size(res) == 8)
Tony Lindgrena7199e22017-12-15 09:41:14 -0800409 ddata->cfg.quirks |= SYSC_QUIRK_16BIT | SYSC_QUIRK_USE_CLOCKACT;
Tony Lindgrena7199e22017-12-15 09:41:14 -0800410}
411
Tony Lindgren0eecc632017-10-10 14:23:43 -0700412/**
413 * sysc_parse_one - parses the interconnect target module registers
414 * @ddata: device driver data
415 * @reg: register to parse
416 */
417static int sysc_parse_one(struct sysc *ddata, enum sysc_registers reg)
418{
419 struct resource *res;
420 const char *name;
421
422 switch (reg) {
423 case SYSC_REVISION:
424 case SYSC_SYSCONFIG:
425 case SYSC_SYSSTATUS:
426 name = reg_names[reg];
427 break;
428 default:
429 return -EINVAL;
430 }
431
432 res = platform_get_resource_byname(to_platform_device(ddata->dev),
433 IORESOURCE_MEM, name);
434 if (!res) {
Tony Lindgren0eecc632017-10-10 14:23:43 -0700435 ddata->offsets[reg] = -ENODEV;
436
437 return 0;
438 }
439
440 ddata->offsets[reg] = res->start - ddata->module_pa;
Tony Lindgrena7199e22017-12-15 09:41:14 -0800441 if (reg == SYSC_REVISION)
442 sysc_check_quirk_16bit(ddata, res);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700443
444 return 0;
445}
446
447static int sysc_parse_registers(struct sysc *ddata)
448{
449 int i, error;
450
451 for (i = 0; i < SYSC_MAX_REGS; i++) {
452 error = sysc_parse_one(ddata, i);
453 if (error)
454 return error;
455 }
456
457 return 0;
458}
459
460/**
461 * sysc_check_registers - check for misconfigured register overlaps
462 * @ddata: device driver data
463 */
464static int sysc_check_registers(struct sysc *ddata)
465{
466 int i, j, nr_regs = 0, nr_matches = 0;
467
468 for (i = 0; i < SYSC_MAX_REGS; i++) {
469 if (ddata->offsets[i] < 0)
470 continue;
471
472 if (ddata->offsets[i] > (ddata->module_size - 4)) {
473 dev_err(ddata->dev, "register outside module range");
474
475 return -EINVAL;
476 }
477
478 for (j = 0; j < SYSC_MAX_REGS; j++) {
479 if (ddata->offsets[j] < 0)
480 continue;
481
482 if (ddata->offsets[i] == ddata->offsets[j])
483 nr_matches++;
484 }
485 nr_regs++;
486 }
487
488 if (nr_regs < 1) {
489 dev_err(ddata->dev, "missing registers\n");
490
491 return -EINVAL;
492 }
493
494 if (nr_matches > nr_regs) {
495 dev_err(ddata->dev, "overlapping registers: (%i/%i)",
496 nr_regs, nr_matches);
497
498 return -EINVAL;
499 }
500
501 return 0;
502}
503
504/**
505 * syc_ioremap - ioremap register space for the interconnect target module
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700506 * @ddata: device driver data
Tony Lindgren0eecc632017-10-10 14:23:43 -0700507 *
508 * Note that the interconnect target module registers can be anywhere
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700509 * within the interconnect target module range. For example, SGX has
510 * them at offset 0x1fc00 in the 32MB module address space. And cpsw
511 * has them at offset 0x1200 in the CPSW_WR child. Usually the
512 * the interconnect target module registers are at the beginning of
513 * the module range though.
Tony Lindgren0eecc632017-10-10 14:23:43 -0700514 */
515static int sysc_ioremap(struct sysc *ddata)
516{
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700517 int size;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700518
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700519 size = max3(ddata->offsets[SYSC_REVISION],
520 ddata->offsets[SYSC_SYSCONFIG],
521 ddata->offsets[SYSC_SYSSTATUS]);
522
523 if (size < 0 || (size + sizeof(u32)) > ddata->module_size)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700524 return -EINVAL;
525
Tony Lindgren0eecc632017-10-10 14:23:43 -0700526 ddata->module_va = devm_ioremap(ddata->dev,
527 ddata->module_pa,
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700528 size + sizeof(u32));
Tony Lindgren0eecc632017-10-10 14:23:43 -0700529 if (!ddata->module_va)
530 return -EIO;
531
532 return 0;
533}
534
535/**
536 * sysc_map_and_check_registers - ioremap and check device registers
537 * @ddata: device driver data
538 */
539static int sysc_map_and_check_registers(struct sysc *ddata)
540{
541 int error;
542
543 error = sysc_parse_and_check_child_range(ddata);
544 if (error)
545 return error;
546
547 error = sysc_check_children(ddata);
548 if (error)
549 return error;
550
551 error = sysc_parse_registers(ddata);
552 if (error)
553 return error;
554
555 error = sysc_ioremap(ddata);
556 if (error)
557 return error;
558
559 error = sysc_check_registers(ddata);
560 if (error)
561 return error;
562
563 return 0;
564}
565
566/**
567 * sysc_show_rev - read and show interconnect target module revision
568 * @bufp: buffer to print the information to
569 * @ddata: device driver data
570 */
571static int sysc_show_rev(char *bufp, struct sysc *ddata)
572{
Tony Lindgren566a9b052017-12-15 09:41:19 -0800573 int len;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700574
575 if (ddata->offsets[SYSC_REVISION] < 0)
576 return sprintf(bufp, ":NA");
577
Tony Lindgren566a9b052017-12-15 09:41:19 -0800578 len = sprintf(bufp, ":%08x", ddata->revision);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700579
580 return len;
581}
582
583static int sysc_show_reg(struct sysc *ddata,
584 char *bufp, enum sysc_registers reg)
585{
586 if (ddata->offsets[reg] < 0)
587 return sprintf(bufp, ":NA");
588
589 return sprintf(bufp, ":%x", ddata->offsets[reg]);
590}
591
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800592static int sysc_show_name(char *bufp, struct sysc *ddata)
593{
594 if (!ddata->name)
595 return 0;
596
597 return sprintf(bufp, ":%s", ddata->name);
598}
599
Tony Lindgren0eecc632017-10-10 14:23:43 -0700600/**
601 * sysc_show_registers - show information about interconnect target module
602 * @ddata: device driver data
603 */
604static void sysc_show_registers(struct sysc *ddata)
605{
606 char buf[128];
607 char *bufp = buf;
608 int i;
609
610 for (i = 0; i < SYSC_MAX_REGS; i++)
611 bufp += sysc_show_reg(ddata, bufp, i);
612
613 bufp += sysc_show_rev(bufp, ddata);
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800614 bufp += sysc_show_name(bufp, ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700615
616 dev_dbg(ddata->dev, "%llx:%x%s\n",
617 ddata->module_pa, ddata->module_size,
618 buf);
619}
620
Arnd Bergmanna4a5d492017-10-13 11:25:32 +0200621static int __maybe_unused sysc_runtime_suspend(struct device *dev)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700622{
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800623 struct ti_sysc_platform_data *pdata;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700624 struct sysc *ddata;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800625 int error = 0, i;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700626
627 ddata = dev_get_drvdata(dev);
628
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800629 if (!ddata->enabled)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700630 return 0;
631
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800632 if (ddata->legacy_mode) {
633 pdata = dev_get_platdata(ddata->dev);
634 if (!pdata)
635 return 0;
636
637 if (!pdata->idle_module)
638 return -ENODEV;
639
640 error = pdata->idle_module(dev, &ddata->cookie);
641 if (error)
642 dev_err(dev, "%s: could not idle: %i\n",
643 __func__, error);
644
645 goto idled;
646 }
647
Tony Lindgren09dfe582018-04-16 10:25:52 -0700648 for (i = 0; i < ddata->nr_clocks; i++) {
Tony Lindgren0eecc632017-10-10 14:23:43 -0700649 if (IS_ERR_OR_NULL(ddata->clocks[i]))
650 continue;
Tony Lindgren09dfe582018-04-16 10:25:52 -0700651
652 if (i >= SYSC_OPTFCK0 && !sysc_opt_clks_needed(ddata))
653 break;
654
Tony Lindgren0eecc632017-10-10 14:23:43 -0700655 clk_disable(ddata->clocks[i]);
656 }
657
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800658idled:
659 ddata->enabled = false;
660
661 return error;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700662}
663
Arnd Bergmanna4a5d492017-10-13 11:25:32 +0200664static int __maybe_unused sysc_runtime_resume(struct device *dev)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700665{
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800666 struct ti_sysc_platform_data *pdata;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700667 struct sysc *ddata;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800668 int error = 0, i;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700669
670 ddata = dev_get_drvdata(dev);
671
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800672 if (ddata->enabled)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700673 return 0;
674
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800675 if (ddata->legacy_mode) {
676 pdata = dev_get_platdata(ddata->dev);
677 if (!pdata)
678 return 0;
679
680 if (!pdata->enable_module)
681 return -ENODEV;
682
683 error = pdata->enable_module(dev, &ddata->cookie);
684 if (error)
685 dev_err(dev, "%s: could not enable: %i\n",
686 __func__, error);
687
688 goto awake;
689 }
690
Tony Lindgren09dfe582018-04-16 10:25:52 -0700691 for (i = 0; i < ddata->nr_clocks; i++) {
Tony Lindgren0eecc632017-10-10 14:23:43 -0700692 if (IS_ERR_OR_NULL(ddata->clocks[i]))
693 continue;
Tony Lindgren09dfe582018-04-16 10:25:52 -0700694
695 if (i >= SYSC_OPTFCK0 && !sysc_opt_clks_needed(ddata))
696 break;
697
Tony Lindgren0eecc632017-10-10 14:23:43 -0700698 error = clk_enable(ddata->clocks[i]);
699 if (error)
700 return error;
701 }
702
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800703awake:
704 ddata->enabled = true;
705
706 return error;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700707}
708
Tony Lindgrenf5e802032018-10-01 09:33:28 -0700709static int __maybe_unused sysc_noirq_suspend(struct device *dev)
Tony Lindgren62020f22018-02-22 13:59:44 -0800710{
711 struct sysc *ddata;
712
713 ddata = dev_get_drvdata(dev);
714
Tony Lindgren40d9f912018-09-24 12:16:54 -0700715 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
Tony Lindgrene7420c22018-04-16 10:26:46 -0700716 return 0;
717
Tony Lindgrenf5e802032018-10-01 09:33:28 -0700718 return pm_runtime_force_suspend(dev);
Tony Lindgren62020f22018-02-22 13:59:44 -0800719}
720
Tony Lindgrenf5e802032018-10-01 09:33:28 -0700721static int __maybe_unused sysc_noirq_resume(struct device *dev)
Tony Lindgren62020f22018-02-22 13:59:44 -0800722{
723 struct sysc *ddata;
724
725 ddata = dev_get_drvdata(dev);
Tony Lindgrene7420c22018-04-16 10:26:46 -0700726
Tony Lindgren40d9f912018-09-24 12:16:54 -0700727 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
Tony Lindgrene7420c22018-04-16 10:26:46 -0700728 return 0;
729
Tony Lindgrenf5e802032018-10-01 09:33:28 -0700730 return pm_runtime_force_resume(dev);
Tony Lindgrene7420c22018-04-16 10:26:46 -0700731}
732
Tony Lindgren0eecc632017-10-10 14:23:43 -0700733static const struct dev_pm_ops sysc_pm_ops = {
Tony Lindgrene7420c22018-04-16 10:26:46 -0700734 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(sysc_noirq_suspend, sysc_noirq_resume)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700735 SET_RUNTIME_PM_OPS(sysc_runtime_suspend,
736 sysc_runtime_resume,
737 NULL)
738};
739
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800740/* Module revision register based quirks */
741struct sysc_revision_quirk {
742 const char *name;
743 u32 base;
744 int rev_offset;
745 int sysc_offset;
746 int syss_offset;
747 u32 revision;
748 u32 revision_mask;
749 u32 quirks;
750};
751
752#define SYSC_QUIRK(optname, optbase, optrev, optsysc, optsyss, \
753 optrev_val, optrevmask, optquirkmask) \
754 { \
755 .name = (optname), \
756 .base = (optbase), \
757 .rev_offset = (optrev), \
758 .sysc_offset = (optsysc), \
759 .syss_offset = (optsyss), \
760 .revision = (optrev_val), \
761 .revision_mask = (optrevmask), \
762 .quirks = (optquirkmask), \
763 }
764
765static const struct sysc_revision_quirk sysc_revision_quirks[] = {
766 /* These drivers need to be fixed to not use pm_runtime_irq_safe() */
Tony Lindgren3a3d8022018-09-27 13:33:58 -0700767 SYSC_QUIRK("gpio", 0, 0, 0x10, 0x114, 0x50600801, 0xffff00ff,
Tony Lindgren09dfe582018-04-16 10:25:52 -0700768 SYSC_QUIRK_LEGACY_IDLE | SYSC_QUIRK_OPT_CLKS_IN_RESET),
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800769 SYSC_QUIRK("mmu", 0, 0, 0x10, 0x14, 0x00000020, 0xffffffff,
770 SYSC_QUIRK_LEGACY_IDLE),
771 SYSC_QUIRK("mmu", 0, 0, 0x10, 0x14, 0x00000030, 0xffffffff,
772 SYSC_QUIRK_LEGACY_IDLE),
773 SYSC_QUIRK("sham", 0, 0x100, 0x110, 0x114, 0x40000c03, 0xffffffff,
774 SYSC_QUIRK_LEGACY_IDLE),
775 SYSC_QUIRK("smartreflex", 0, -1, 0x24, -1, 0x00000000, 0xffffffff,
776 SYSC_QUIRK_LEGACY_IDLE),
777 SYSC_QUIRK("smartreflex", 0, -1, 0x38, -1, 0x00000000, 0xffffffff,
778 SYSC_QUIRK_LEGACY_IDLE),
779 SYSC_QUIRK("timer", 0, 0, 0x10, 0x14, 0x00000015, 0xffffffff,
780 SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgren8cde5d52018-04-16 10:25:15 -0700781 /* Some timers on omap4 and later */
Tony Lindgren3a3d8022018-09-27 13:33:58 -0700782 SYSC_QUIRK("timer", 0, 0, 0x10, -1, 0x50002100, 0xffffffff,
783 SYSC_QUIRK_LEGACY_IDLE),
784 SYSC_QUIRK("timer", 0, 0, 0x10, -1, 0x4fff1301, 0xffff00ff,
Tony Lindgren8cde5d52018-04-16 10:25:15 -0700785 SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800786 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x00000052, 0xffffffff,
787 SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgrend708bb12018-04-16 10:25:32 -0700788 /* Uarts on omap4 and later */
Tony Lindgrenb82beef2018-09-24 12:16:59 -0700789 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x50411e03, 0xffff00ff,
790 SYSC_QUIRK_LEGACY_IDLE),
791 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x47422e03, 0xffffffff,
Tony Lindgrend708bb12018-04-16 10:25:32 -0700792 SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgren7e27e5d2018-04-20 14:54:16 -0700793
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700794#ifdef DEBUG
Tony Lindgren1ba30692018-09-24 12:17:05 -0700795 SYSC_QUIRK("adc", 0, 0, 0x10, -1, 0x47300001, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -0700796 SYSC_QUIRK("atl", 0, 0, -1, -1, 0x0a070100, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700797 SYSC_QUIRK("aess", 0, 0, 0x10, -1, 0x40000000, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -0700798 SYSC_QUIRK("cm", 0, 0, -1, -1, 0x40000301, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -0700799 SYSC_QUIRK("control", 0, 0, 0x10, -1, 0x40000900, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700800 SYSC_QUIRK("cpgmac", 0, 0x1200, 0x1208, 0x1204, 0x4edb1902,
Tony Lindgren23731ea2018-09-24 12:17:09 -0700801 0xffff00f0, 0),
802 SYSC_QUIRK("dcan", 0, 0, -1, -1, 0xffffffff, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700803 SYSC_QUIRK("dcan", 0, 0, -1, -1, 0x00001401, 0xffffffff, 0),
804 SYSC_QUIRK("dwc3", 0, 0, 0x10, -1, 0x500a0200, 0xffffffff, 0),
805 SYSC_QUIRK("epwmss", 0, 0, 0x4, -1, 0x47400001, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700806 SYSC_QUIRK("gpu", 0, 0x1fc00, 0x1fc10, -1, 0, 0, 0),
807 SYSC_QUIRK("hdq1w", 0, 0, 0x14, 0x18, 0x00000006, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700808 SYSC_QUIRK("hdq1w", 0, 0, 0x14, 0x18, 0x0000000a, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700809 SYSC_QUIRK("hsi", 0, 0, 0x10, 0x14, 0x50043101, 0xffffffff, 0),
810 SYSC_QUIRK("iss", 0, 0, 0x10, -1, 0x40000101, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -0700811 SYSC_QUIRK("i2c", 0, 0, 0x10, 0x90, 0x5040000a, 0xfffff0f0, 0),
Tony Lindgren23731ea2018-09-24 12:17:09 -0700812 SYSC_QUIRK("lcdc", 0, 0, 0x54, -1, 0x4f201000, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700813 SYSC_QUIRK("mcasp", 0, 0, 0x4, -1, 0x44306302, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700814 SYSC_QUIRK("mcasp", 0, 0, 0x4, -1, 0x44307b02, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700815 SYSC_QUIRK("mcbsp", 0, -1, 0x8c, -1, 0, 0, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -0700816 SYSC_QUIRK("mcspi", 0, 0, 0x10, -1, 0x40300a0b, 0xffff00ff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700817 SYSC_QUIRK("mcspi", 0, 0, 0x110, 0x114, 0x40300a0b, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700818 SYSC_QUIRK("mailbox", 0, 0, 0x10, -1, 0x00000400, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700819 SYSC_QUIRK("m3", 0, 0, -1, -1, 0x5f580105, 0x0fff0f00, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -0700820 SYSC_QUIRK("ocp2scp", 0, 0, 0x10, 0x14, 0x50060005, 0xfffffff0, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700821 SYSC_QUIRK("ocp2scp", 0, 0, -1, -1, 0x50060007, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -0700822 SYSC_QUIRK("padconf", 0, 0, 0x10, -1, 0x4fff0800, 0xffffffff, 0),
Tony Lindgrenf0106702018-11-15 14:46:53 -0800823 SYSC_QUIRK("padconf", 0, 0, -1, -1, 0x40001100, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -0700824 SYSC_QUIRK("prcm", 0, 0, -1, -1, 0x40000100, 0xffffffff, 0),
Tony Lindgren23731ea2018-09-24 12:17:09 -0700825 SYSC_QUIRK("prcm", 0, 0, -1, -1, 0x00004102, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700826 SYSC_QUIRK("prcm", 0, 0, -1, -1, 0x40000400, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -0700827 SYSC_QUIRK("scm", 0, 0, 0x10, -1, 0x40000900, 0xffffffff, 0),
Tony Lindgren23731ea2018-09-24 12:17:09 -0700828 SYSC_QUIRK("scm", 0, 0, -1, -1, 0x4e8b0100, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700829 SYSC_QUIRK("scm", 0, 0, -1, -1, 0x4f000100, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -0700830 SYSC_QUIRK("scm", 0, 0, -1, -1, 0x40000900, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -0700831 SYSC_QUIRK("scrm", 0, 0, -1, -1, 0x00000010, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -0700832 SYSC_QUIRK("sdio", 0, 0, 0x10, -1, 0x40202301, 0xffff0ff0, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700833 SYSC_QUIRK("sdio", 0, 0x2fc, 0x110, 0x114, 0x31010000, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -0700834 SYSC_QUIRK("sdma", 0, 0, 0x2c, 0x28, 0x00010900, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700835 SYSC_QUIRK("slimbus", 0, 0, 0x10, -1, 0x40000902, 0xffffffff, 0),
836 SYSC_QUIRK("slimbus", 0, 0, 0x10, -1, 0x40002903, 0xffffffff, 0),
837 SYSC_QUIRK("spinlock", 0, 0, 0x10, -1, 0x50020000, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700838 SYSC_QUIRK("rng", 0, 0x1fe0, 0x1fe4, -1, 0x00000020, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -0700839 SYSC_QUIRK("rtc", 0, 0x74, 0x78, -1, 0x4eb01908, 0xffff00f0, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700840 SYSC_QUIRK("timer32k", 0, 0, 0x4, -1, 0x00000060, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700841 SYSC_QUIRK("usbhstll", 0, 0, 0x10, 0x14, 0x00000004, 0xffffffff, 0),
Tony Lindgrenf0106702018-11-15 14:46:53 -0800842 SYSC_QUIRK("usbhstll", 0, 0, 0x10, 0x14, 0x00000008, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700843 SYSC_QUIRK("usb_host_hs", 0, 0, 0x10, 0x14, 0x50700100, 0xffffffff, 0),
Tony Lindgrenf0106702018-11-15 14:46:53 -0800844 SYSC_QUIRK("usb_host_hs", 0, 0, 0x10, -1, 0x50700101, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700845 SYSC_QUIRK("usb_otg_hs", 0, 0x400, 0x404, 0x408, 0x00000050,
846 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700847 SYSC_QUIRK("wdt", 0, 0, 0x10, 0x14, 0x502a0500, 0xfffff0f0, 0),
848 SYSC_QUIRK("vfpe", 0, 0, 0x104, -1, 0x4d001200, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700849#endif
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800850};
851
852static void sysc_init_revision_quirks(struct sysc *ddata)
853{
854 const struct sysc_revision_quirk *q;
855 int i;
856
857 for (i = 0; i < ARRAY_SIZE(sysc_revision_quirks); i++) {
858 q = &sysc_revision_quirks[i];
859
860 if (q->base && q->base != ddata->module_pa)
861 continue;
862
863 if (q->rev_offset >= 0 &&
864 q->rev_offset != ddata->offsets[SYSC_REVISION])
865 continue;
866
867 if (q->sysc_offset >= 0 &&
868 q->sysc_offset != ddata->offsets[SYSC_SYSCONFIG])
869 continue;
870
871 if (q->syss_offset >= 0 &&
872 q->syss_offset != ddata->offsets[SYSC_SYSSTATUS])
873 continue;
874
875 if (q->revision == ddata->revision ||
876 (q->revision & q->revision_mask) ==
877 (ddata->revision & q->revision_mask)) {
878 ddata->name = q->name;
879 ddata->cfg.quirks |= q->quirks;
880 }
881 }
882}
883
Faiz Abbas596e7952018-07-09 22:18:39 +0530884static int sysc_reset(struct sysc *ddata)
885{
886 int offset = ddata->offsets[SYSC_SYSCONFIG];
887 int val;
888
889 if (ddata->legacy_mode || offset < 0 ||
890 ddata->cfg.quirks & SYSC_QUIRK_NO_RESET_ON_INIT)
891 return 0;
892
893 /*
894 * Currently only support reset status in sysstatus.
895 * Warn and return error in all other cases
896 */
897 if (!ddata->cfg.syss_mask) {
898 dev_err(ddata->dev, "No ti,syss-mask. Reset failed\n");
899 return -EINVAL;
900 }
901
902 val = sysc_read(ddata, offset);
903 val |= (0x1 << ddata->cap->regbits->srst_shift);
904 sysc_write(ddata, offset, val);
905
906 /* Poll on reset status */
907 offset = ddata->offsets[SYSC_SYSSTATUS];
908
909 return readl_poll_timeout(ddata->module_va + offset, val,
910 (val & ddata->cfg.syss_mask) == 0x0,
911 100, MAX_MODULE_SOFTRESET_WAIT);
912}
913
Tony Lindgren566a9b052017-12-15 09:41:19 -0800914/* At this point the module is configured enough to read the revision */
915static int sysc_init_module(struct sysc *ddata)
916{
917 int error;
918
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800919 if (ddata->cfg.quirks & SYSC_QUIRK_NO_IDLE_ON_INIT) {
920 ddata->revision = sysc_read_revision(ddata);
921 goto rev_quirks;
922 }
923
Tony Lindgren566a9b052017-12-15 09:41:19 -0800924 error = pm_runtime_get_sync(ddata->dev);
925 if (error < 0) {
926 pm_runtime_put_noidle(ddata->dev);
927
928 return 0;
929 }
Tony Lindgren50622362018-04-16 10:20:27 -0700930
Faiz Abbas596e7952018-07-09 22:18:39 +0530931 error = sysc_reset(ddata);
932 if (error) {
933 dev_err(ddata->dev, "Reset failed with %d\n", error);
934 pm_runtime_put_sync(ddata->dev);
935
936 return error;
937 }
938
Tony Lindgren566a9b052017-12-15 09:41:19 -0800939 ddata->revision = sysc_read_revision(ddata);
940 pm_runtime_put_sync(ddata->dev);
941
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800942rev_quirks:
943 sysc_init_revision_quirks(ddata);
944
Tony Lindgren566a9b052017-12-15 09:41:19 -0800945 return 0;
946}
947
Tony Lindgrenc5a2de92017-12-15 09:41:23 -0800948static int sysc_init_sysc_mask(struct sysc *ddata)
949{
950 struct device_node *np = ddata->dev->of_node;
951 int error;
952 u32 val;
953
954 error = of_property_read_u32(np, "ti,sysc-mask", &val);
955 if (error)
956 return 0;
957
958 if (val)
959 ddata->cfg.sysc_val = val & ddata->cap->sysc_mask;
960 else
961 ddata->cfg.sysc_val = ddata->cap->sysc_mask;
962
963 return 0;
964}
965
966static int sysc_init_idlemode(struct sysc *ddata, u8 *idlemodes,
967 const char *name)
968{
969 struct device_node *np = ddata->dev->of_node;
970 struct property *prop;
971 const __be32 *p;
972 u32 val;
973
974 of_property_for_each_u32(np, name, prop, p, val) {
975 if (val >= SYSC_NR_IDLEMODES) {
976 dev_err(ddata->dev, "invalid idlemode: %i\n", val);
977 return -EINVAL;
978 }
979 *idlemodes |= (1 << val);
980 }
981
982 return 0;
983}
984
985static int sysc_init_idlemodes(struct sysc *ddata)
986{
987 int error;
988
989 error = sysc_init_idlemode(ddata, &ddata->cfg.midlemodes,
990 "ti,sysc-midle");
991 if (error)
992 return error;
993
994 error = sysc_init_idlemode(ddata, &ddata->cfg.sidlemodes,
995 "ti,sysc-sidle");
996 if (error)
997 return error;
998
999 return 0;
1000}
1001
1002/*
1003 * Only some devices on omap4 and later have SYSCONFIG reset done
1004 * bit. We can detect this if there is no SYSSTATUS at all, or the
1005 * SYSTATUS bit 0 is not used. Note that some SYSSTATUS registers
1006 * have multiple bits for the child devices like OHCI and EHCI.
1007 * Depends on SYSC being parsed first.
1008 */
1009static int sysc_init_syss_mask(struct sysc *ddata)
1010{
1011 struct device_node *np = ddata->dev->of_node;
1012 int error;
1013 u32 val;
1014
1015 error = of_property_read_u32(np, "ti,syss-mask", &val);
1016 if (error) {
1017 if ((ddata->cap->type == TI_SYSC_OMAP4 ||
1018 ddata->cap->type == TI_SYSC_OMAP4_TIMER) &&
1019 (ddata->cfg.sysc_val & SYSC_OMAP4_SOFTRESET))
1020 ddata->cfg.quirks |= SYSC_QUIRK_RESET_STATUS;
1021
1022 return 0;
1023 }
1024
1025 if (!(val & 1) && (ddata->cfg.sysc_val & SYSC_OMAP4_SOFTRESET))
1026 ddata->cfg.quirks |= SYSC_QUIRK_RESET_STATUS;
1027
1028 ddata->cfg.syss_mask = val;
1029
1030 return 0;
1031}
1032
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001033/*
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001034 * Many child device drivers need to have fck and opt clocks available
1035 * to get the clock rate for device internal configuration etc.
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001036 */
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001037static int sysc_child_add_named_clock(struct sysc *ddata,
1038 struct device *child,
1039 const char *name)
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001040{
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001041 struct clk *clk;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001042 struct clk_lookup *l;
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001043 int error = 0;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001044
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001045 if (!name)
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001046 return 0;
1047
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001048 clk = clk_get(child, name);
1049 if (!IS_ERR(clk)) {
1050 clk_put(clk);
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001051
1052 return -EEXIST;
1053 }
1054
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001055 clk = clk_get(ddata->dev, name);
1056 if (IS_ERR(clk))
1057 return -ENODEV;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001058
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001059 l = clkdev_create(clk, name, dev_name(child));
1060 if (!l)
1061 error = -ENOMEM;
1062
1063 clk_put(clk);
1064
1065 return error;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001066}
1067
Tony Lindgren09dfe582018-04-16 10:25:52 -07001068static int sysc_child_add_clocks(struct sysc *ddata,
1069 struct device *child)
1070{
1071 int i, error;
1072
1073 for (i = 0; i < ddata->nr_clocks; i++) {
1074 error = sysc_child_add_named_clock(ddata,
1075 child,
1076 ddata->clock_roles[i]);
1077 if (error && error != -EEXIST) {
1078 dev_err(ddata->dev, "could not add child clock %s: %i\n",
1079 ddata->clock_roles[i], error);
1080
1081 return error;
1082 }
1083 }
1084
1085 return 0;
1086}
1087
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001088static struct device_type sysc_device_type = {
1089};
1090
1091static struct sysc *sysc_child_to_parent(struct device *dev)
1092{
1093 struct device *parent = dev->parent;
1094
1095 if (!parent || parent->type != &sysc_device_type)
1096 return NULL;
1097
1098 return dev_get_drvdata(parent);
1099}
1100
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001101static int __maybe_unused sysc_child_runtime_suspend(struct device *dev)
1102{
1103 struct sysc *ddata;
1104 int error;
1105
1106 ddata = sysc_child_to_parent(dev);
1107
1108 error = pm_generic_runtime_suspend(dev);
1109 if (error)
1110 return error;
1111
1112 if (!ddata->enabled)
1113 return 0;
1114
1115 return sysc_runtime_suspend(ddata->dev);
1116}
1117
1118static int __maybe_unused sysc_child_runtime_resume(struct device *dev)
1119{
1120 struct sysc *ddata;
1121 int error;
1122
1123 ddata = sysc_child_to_parent(dev);
1124
1125 if (!ddata->enabled) {
1126 error = sysc_runtime_resume(ddata->dev);
1127 if (error < 0)
1128 dev_err(ddata->dev,
1129 "%s error: %i\n", __func__, error);
1130 }
1131
1132 return pm_generic_runtime_resume(dev);
1133}
1134
1135#ifdef CONFIG_PM_SLEEP
1136static int sysc_child_suspend_noirq(struct device *dev)
1137{
1138 struct sysc *ddata;
1139 int error;
1140
1141 ddata = sysc_child_to_parent(dev);
1142
Tony Lindgrenef55f822018-04-16 10:27:15 -07001143 dev_dbg(ddata->dev, "%s %s\n", __func__,
1144 ddata->name ? ddata->name : "");
1145
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001146 error = pm_generic_suspend_noirq(dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001147 if (error) {
1148 dev_err(dev, "%s error at %i: %i\n",
1149 __func__, __LINE__, error);
1150
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001151 return error;
Tony Lindgrenef55f822018-04-16 10:27:15 -07001152 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001153
1154 if (!pm_runtime_status_suspended(dev)) {
1155 error = pm_generic_runtime_suspend(dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001156 if (error) {
Tony Lindgrenf9490782018-09-28 15:21:50 -07001157 dev_dbg(dev, "%s busy at %i: %i\n",
1158 __func__, __LINE__, error);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001159
Tony Lindgren4f3530f2018-08-08 01:07:06 -07001160 return 0;
Tony Lindgrenef55f822018-04-16 10:27:15 -07001161 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001162
1163 error = sysc_runtime_suspend(ddata->dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001164 if (error) {
1165 dev_err(dev, "%s error at %i: %i\n",
1166 __func__, __LINE__, error);
1167
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001168 return error;
Tony Lindgrenef55f822018-04-16 10:27:15 -07001169 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001170
1171 ddata->child_needs_resume = true;
1172 }
1173
1174 return 0;
1175}
1176
1177static int sysc_child_resume_noirq(struct device *dev)
1178{
1179 struct sysc *ddata;
1180 int error;
1181
1182 ddata = sysc_child_to_parent(dev);
1183
Tony Lindgrenef55f822018-04-16 10:27:15 -07001184 dev_dbg(ddata->dev, "%s %s\n", __func__,
1185 ddata->name ? ddata->name : "");
1186
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001187 if (ddata->child_needs_resume) {
1188 ddata->child_needs_resume = false;
1189
1190 error = sysc_runtime_resume(ddata->dev);
1191 if (error)
1192 dev_err(ddata->dev,
1193 "%s runtime resume error: %i\n",
1194 __func__, error);
1195
1196 error = pm_generic_runtime_resume(dev);
1197 if (error)
1198 dev_err(ddata->dev,
1199 "%s generic runtime resume: %i\n",
1200 __func__, error);
1201 }
1202
1203 return pm_generic_resume_noirq(dev);
1204}
1205#endif
1206
1207struct dev_pm_domain sysc_child_pm_domain = {
1208 .ops = {
1209 SET_RUNTIME_PM_OPS(sysc_child_runtime_suspend,
1210 sysc_child_runtime_resume,
1211 NULL)
1212 USE_PLATFORM_PM_SLEEP_OPS
1213 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(sysc_child_suspend_noirq,
1214 sysc_child_resume_noirq)
1215 }
1216};
1217
1218/**
1219 * sysc_legacy_idle_quirk - handle children in omap_device compatible way
1220 * @ddata: device driver data
1221 * @child: child device driver
1222 *
1223 * Allow idle for child devices as done with _od_runtime_suspend().
1224 * Otherwise many child devices will not idle because of the permanent
1225 * parent usecount set in pm_runtime_irq_safe().
1226 *
1227 * Note that the long term solution is to just modify the child device
1228 * drivers to not set pm_runtime_irq_safe() and then this can be just
1229 * dropped.
1230 */
1231static void sysc_legacy_idle_quirk(struct sysc *ddata, struct device *child)
1232{
1233 if (!ddata->legacy_mode)
1234 return;
1235
1236 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
1237 dev_pm_domain_set(child, &sysc_child_pm_domain);
1238}
1239
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001240static int sysc_notifier_call(struct notifier_block *nb,
1241 unsigned long event, void *device)
1242{
1243 struct device *dev = device;
1244 struct sysc *ddata;
1245 int error;
1246
1247 ddata = sysc_child_to_parent(dev);
1248 if (!ddata)
1249 return NOTIFY_DONE;
1250
1251 switch (event) {
1252 case BUS_NOTIFY_ADD_DEVICE:
Tony Lindgren09dfe582018-04-16 10:25:52 -07001253 error = sysc_child_add_clocks(ddata, dev);
1254 if (error)
1255 return error;
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001256 sysc_legacy_idle_quirk(ddata, dev);
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001257 break;
1258 default:
1259 break;
1260 }
1261
1262 return NOTIFY_DONE;
1263}
1264
1265static struct notifier_block sysc_nb = {
1266 .notifier_call = sysc_notifier_call,
1267};
1268
Tony Lindgren566a9b052017-12-15 09:41:19 -08001269/* Device tree configured quirks */
1270struct sysc_dts_quirk {
1271 const char *name;
1272 u32 mask;
1273};
1274
1275static const struct sysc_dts_quirk sysc_dts_quirks[] = {
1276 { .name = "ti,no-idle-on-init",
1277 .mask = SYSC_QUIRK_NO_IDLE_ON_INIT, },
1278 { .name = "ti,no-reset-on-init",
1279 .mask = SYSC_QUIRK_NO_RESET_ON_INIT, },
1280};
1281
1282static int sysc_init_dts_quirks(struct sysc *ddata)
1283{
1284 struct device_node *np = ddata->dev->of_node;
1285 const struct property *prop;
1286 int i, len, error;
1287 u32 val;
1288
1289 ddata->legacy_mode = of_get_property(np, "ti,hwmods", NULL);
1290
1291 for (i = 0; i < ARRAY_SIZE(sysc_dts_quirks); i++) {
1292 prop = of_get_property(np, sysc_dts_quirks[i].name, &len);
1293 if (!prop)
Tony Lindgrend39b6ea2018-02-15 09:18:55 -08001294 continue;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001295
1296 ddata->cfg.quirks |= sysc_dts_quirks[i].mask;
1297 }
1298
1299 error = of_property_read_u32(np, "ti,sysc-delay-us", &val);
1300 if (!error) {
1301 if (val > 255) {
1302 dev_warn(ddata->dev, "bad ti,sysc-delay-us: %i\n",
1303 val);
1304 }
1305
1306 ddata->cfg.srst_udelay = (u8)val;
1307 }
1308
1309 return 0;
1310}
1311
Tony Lindgren0eecc632017-10-10 14:23:43 -07001312static void sysc_unprepare(struct sysc *ddata)
1313{
1314 int i;
1315
1316 for (i = 0; i < SYSC_MAX_CLOCKS; i++) {
1317 if (!IS_ERR_OR_NULL(ddata->clocks[i]))
1318 clk_unprepare(ddata->clocks[i]);
1319 }
1320}
1321
Tony Lindgren70a65242017-12-15 09:41:09 -08001322/*
1323 * Common sysc register bits found on omap2, also known as type1
1324 */
1325static const struct sysc_regbits sysc_regbits_omap2 = {
1326 .dmadisable_shift = -ENODEV,
1327 .midle_shift = 12,
1328 .sidle_shift = 3,
1329 .clkact_shift = 8,
1330 .emufree_shift = 5,
1331 .enwkup_shift = 2,
1332 .srst_shift = 1,
1333 .autoidle_shift = 0,
1334};
1335
1336static const struct sysc_capabilities sysc_omap2 = {
1337 .type = TI_SYSC_OMAP2,
1338 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY | SYSC_OMAP2_EMUFREE |
1339 SYSC_OMAP2_ENAWAKEUP | SYSC_OMAP2_SOFTRESET |
1340 SYSC_OMAP2_AUTOIDLE,
1341 .regbits = &sysc_regbits_omap2,
1342};
1343
1344/* All omap2 and 3 timers, and timers 1, 2 & 10 on omap 4 and 5 */
1345static const struct sysc_capabilities sysc_omap2_timer = {
1346 .type = TI_SYSC_OMAP2_TIMER,
1347 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY | SYSC_OMAP2_EMUFREE |
1348 SYSC_OMAP2_ENAWAKEUP | SYSC_OMAP2_SOFTRESET |
1349 SYSC_OMAP2_AUTOIDLE,
1350 .regbits = &sysc_regbits_omap2,
1351 .mod_quirks = SYSC_QUIRK_USE_CLOCKACT,
1352};
1353
1354/*
1355 * SHAM2 (SHA1/MD5) sysc found on omap3, a variant of sysc_regbits_omap2
1356 * with different sidle position
1357 */
1358static const struct sysc_regbits sysc_regbits_omap3_sham = {
1359 .dmadisable_shift = -ENODEV,
1360 .midle_shift = -ENODEV,
1361 .sidle_shift = 4,
1362 .clkact_shift = -ENODEV,
1363 .enwkup_shift = -ENODEV,
1364 .srst_shift = 1,
1365 .autoidle_shift = 0,
1366 .emufree_shift = -ENODEV,
1367};
1368
1369static const struct sysc_capabilities sysc_omap3_sham = {
1370 .type = TI_SYSC_OMAP3_SHAM,
1371 .sysc_mask = SYSC_OMAP2_SOFTRESET | SYSC_OMAP2_AUTOIDLE,
1372 .regbits = &sysc_regbits_omap3_sham,
1373};
1374
1375/*
1376 * AES register bits found on omap3 and later, a variant of
1377 * sysc_regbits_omap2 with different sidle position
1378 */
1379static const struct sysc_regbits sysc_regbits_omap3_aes = {
1380 .dmadisable_shift = -ENODEV,
1381 .midle_shift = -ENODEV,
1382 .sidle_shift = 6,
1383 .clkact_shift = -ENODEV,
1384 .enwkup_shift = -ENODEV,
1385 .srst_shift = 1,
1386 .autoidle_shift = 0,
1387 .emufree_shift = -ENODEV,
1388};
1389
1390static const struct sysc_capabilities sysc_omap3_aes = {
1391 .type = TI_SYSC_OMAP3_AES,
1392 .sysc_mask = SYSC_OMAP2_SOFTRESET | SYSC_OMAP2_AUTOIDLE,
1393 .regbits = &sysc_regbits_omap3_aes,
1394};
1395
1396/*
1397 * Common sysc register bits found on omap4, also known as type2
1398 */
1399static const struct sysc_regbits sysc_regbits_omap4 = {
1400 .dmadisable_shift = 16,
1401 .midle_shift = 4,
1402 .sidle_shift = 2,
1403 .clkact_shift = -ENODEV,
1404 .enwkup_shift = -ENODEV,
1405 .emufree_shift = 1,
1406 .srst_shift = 0,
1407 .autoidle_shift = -ENODEV,
1408};
1409
1410static const struct sysc_capabilities sysc_omap4 = {
1411 .type = TI_SYSC_OMAP4,
1412 .sysc_mask = SYSC_OMAP4_DMADISABLE | SYSC_OMAP4_FREEEMU |
1413 SYSC_OMAP4_SOFTRESET,
1414 .regbits = &sysc_regbits_omap4,
1415};
1416
1417static const struct sysc_capabilities sysc_omap4_timer = {
1418 .type = TI_SYSC_OMAP4_TIMER,
1419 .sysc_mask = SYSC_OMAP4_DMADISABLE | SYSC_OMAP4_FREEEMU |
1420 SYSC_OMAP4_SOFTRESET,
1421 .regbits = &sysc_regbits_omap4,
1422};
1423
1424/*
1425 * Common sysc register bits found on omap4, also known as type3
1426 */
1427static const struct sysc_regbits sysc_regbits_omap4_simple = {
1428 .dmadisable_shift = -ENODEV,
1429 .midle_shift = 2,
1430 .sidle_shift = 0,
1431 .clkact_shift = -ENODEV,
1432 .enwkup_shift = -ENODEV,
1433 .srst_shift = -ENODEV,
1434 .emufree_shift = -ENODEV,
1435 .autoidle_shift = -ENODEV,
1436};
1437
1438static const struct sysc_capabilities sysc_omap4_simple = {
1439 .type = TI_SYSC_OMAP4_SIMPLE,
1440 .regbits = &sysc_regbits_omap4_simple,
1441};
1442
1443/*
1444 * SmartReflex sysc found on omap34xx
1445 */
1446static const struct sysc_regbits sysc_regbits_omap34xx_sr = {
1447 .dmadisable_shift = -ENODEV,
1448 .midle_shift = -ENODEV,
1449 .sidle_shift = -ENODEV,
1450 .clkact_shift = 20,
1451 .enwkup_shift = -ENODEV,
1452 .srst_shift = -ENODEV,
1453 .emufree_shift = -ENODEV,
1454 .autoidle_shift = -ENODEV,
1455};
1456
1457static const struct sysc_capabilities sysc_34xx_sr = {
1458 .type = TI_SYSC_OMAP34XX_SR,
1459 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY,
1460 .regbits = &sysc_regbits_omap34xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001461 .mod_quirks = SYSC_QUIRK_USE_CLOCKACT | SYSC_QUIRK_UNCACHED |
1462 SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08001463};
1464
1465/*
1466 * SmartReflex sysc found on omap36xx and later
1467 */
1468static const struct sysc_regbits sysc_regbits_omap36xx_sr = {
1469 .dmadisable_shift = -ENODEV,
1470 .midle_shift = -ENODEV,
1471 .sidle_shift = 24,
1472 .clkact_shift = -ENODEV,
1473 .enwkup_shift = 26,
1474 .srst_shift = -ENODEV,
1475 .emufree_shift = -ENODEV,
1476 .autoidle_shift = -ENODEV,
1477};
1478
1479static const struct sysc_capabilities sysc_36xx_sr = {
1480 .type = TI_SYSC_OMAP36XX_SR,
Tony Lindgren3267c082018-01-22 09:32:53 -08001481 .sysc_mask = SYSC_OMAP3_SR_ENAWAKEUP,
Tony Lindgren70a65242017-12-15 09:41:09 -08001482 .regbits = &sysc_regbits_omap36xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001483 .mod_quirks = SYSC_QUIRK_UNCACHED | SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08001484};
1485
1486static const struct sysc_capabilities sysc_omap4_sr = {
1487 .type = TI_SYSC_OMAP4_SR,
1488 .regbits = &sysc_regbits_omap36xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001489 .mod_quirks = SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08001490};
1491
1492/*
1493 * McASP register bits found on omap4 and later
1494 */
1495static const struct sysc_regbits sysc_regbits_omap4_mcasp = {
1496 .dmadisable_shift = -ENODEV,
1497 .midle_shift = -ENODEV,
1498 .sidle_shift = 0,
1499 .clkact_shift = -ENODEV,
1500 .enwkup_shift = -ENODEV,
1501 .srst_shift = -ENODEV,
1502 .emufree_shift = -ENODEV,
1503 .autoidle_shift = -ENODEV,
1504};
1505
1506static const struct sysc_capabilities sysc_omap4_mcasp = {
1507 .type = TI_SYSC_OMAP4_MCASP,
1508 .regbits = &sysc_regbits_omap4_mcasp,
Tony Lindgren2c63a832018-11-15 14:46:53 -08001509 .mod_quirks = SYSC_QUIRK_OPT_CLKS_NEEDED,
1510};
1511
1512/*
1513 * McASP found on dra7 and later
1514 */
1515static const struct sysc_capabilities sysc_dra7_mcasp = {
1516 .type = TI_SYSC_OMAP4_SIMPLE,
1517 .regbits = &sysc_regbits_omap4_simple,
1518 .mod_quirks = SYSC_QUIRK_OPT_CLKS_NEEDED,
Tony Lindgren70a65242017-12-15 09:41:09 -08001519};
1520
1521/*
1522 * FS USB host found on omap4 and later
1523 */
1524static const struct sysc_regbits sysc_regbits_omap4_usb_host_fs = {
1525 .dmadisable_shift = -ENODEV,
1526 .midle_shift = -ENODEV,
1527 .sidle_shift = 24,
1528 .clkact_shift = -ENODEV,
1529 .enwkup_shift = 26,
1530 .srst_shift = -ENODEV,
1531 .emufree_shift = -ENODEV,
1532 .autoidle_shift = -ENODEV,
1533};
1534
1535static const struct sysc_capabilities sysc_omap4_usb_host_fs = {
1536 .type = TI_SYSC_OMAP4_USB_HOST_FS,
1537 .sysc_mask = SYSC_OMAP2_ENAWAKEUP,
1538 .regbits = &sysc_regbits_omap4_usb_host_fs,
1539};
1540
Faiz Abbas7f35e632018-07-09 22:18:38 +05301541static const struct sysc_regbits sysc_regbits_dra7_mcan = {
1542 .dmadisable_shift = -ENODEV,
1543 .midle_shift = -ENODEV,
1544 .sidle_shift = -ENODEV,
1545 .clkact_shift = -ENODEV,
1546 .enwkup_shift = 4,
1547 .srst_shift = 0,
1548 .emufree_shift = -ENODEV,
1549 .autoidle_shift = -ENODEV,
1550};
1551
1552static const struct sysc_capabilities sysc_dra7_mcan = {
1553 .type = TI_SYSC_DRA7_MCAN,
1554 .sysc_mask = SYSC_DRA7_MCAN_ENAWAKEUP | SYSC_OMAP4_SOFTRESET,
1555 .regbits = &sysc_regbits_dra7_mcan,
1556};
1557
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001558static int sysc_init_pdata(struct sysc *ddata)
1559{
1560 struct ti_sysc_platform_data *pdata = dev_get_platdata(ddata->dev);
1561 struct ti_sysc_module_data mdata;
1562 int error = 0;
1563
1564 if (!pdata || !ddata->legacy_mode)
1565 return 0;
1566
1567 mdata.name = ddata->legacy_mode;
1568 mdata.module_pa = ddata->module_pa;
1569 mdata.module_size = ddata->module_size;
1570 mdata.offsets = ddata->offsets;
1571 mdata.nr_offsets = SYSC_MAX_REGS;
1572 mdata.cap = ddata->cap;
1573 mdata.cfg = &ddata->cfg;
1574
1575 if (!pdata->init_module)
1576 return -ENODEV;
1577
1578 error = pdata->init_module(ddata->dev, &mdata, &ddata->cookie);
1579 if (error == -EEXIST)
1580 error = 0;
1581
1582 return error;
1583}
1584
Tony Lindgren70a65242017-12-15 09:41:09 -08001585static int sysc_init_match(struct sysc *ddata)
1586{
1587 const struct sysc_capabilities *cap;
1588
1589 cap = of_device_get_match_data(ddata->dev);
1590 if (!cap)
1591 return -EINVAL;
1592
1593 ddata->cap = cap;
1594 if (ddata->cap)
1595 ddata->cfg.quirks |= ddata->cap->mod_quirks;
1596
1597 return 0;
1598}
1599
Tony Lindgren76f0f772018-02-23 08:28:45 -08001600static void ti_sysc_idle(struct work_struct *work)
1601{
1602 struct sysc *ddata;
1603
1604 ddata = container_of(work, struct sysc, idle_work.work);
1605
1606 if (pm_runtime_active(ddata->dev))
1607 pm_runtime_put_sync(ddata->dev);
1608}
1609
Tony Lindgrenc4bebea2018-04-16 10:24:54 -07001610static const struct of_device_id sysc_match_table[] = {
1611 { .compatible = "simple-bus", },
1612 { /* sentinel */ },
1613};
1614
Tony Lindgren0eecc632017-10-10 14:23:43 -07001615static int sysc_probe(struct platform_device *pdev)
1616{
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001617 struct ti_sysc_platform_data *pdata = dev_get_platdata(&pdev->dev);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001618 struct sysc *ddata;
1619 int error;
1620
1621 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
1622 if (!ddata)
1623 return -ENOMEM;
1624
1625 ddata->dev = &pdev->dev;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001626 platform_set_drvdata(pdev, ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001627
Tony Lindgren70a65242017-12-15 09:41:09 -08001628 error = sysc_init_match(ddata);
1629 if (error)
1630 return error;
1631
Tony Lindgren566a9b052017-12-15 09:41:19 -08001632 error = sysc_init_dts_quirks(ddata);
1633 if (error)
1634 goto unprepare;
1635
Tony Lindgren0eecc632017-10-10 14:23:43 -07001636 error = sysc_get_clocks(ddata);
1637 if (error)
1638 return error;
1639
1640 error = sysc_map_and_check_registers(ddata);
1641 if (error)
1642 goto unprepare;
1643
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08001644 error = sysc_init_sysc_mask(ddata);
1645 if (error)
1646 goto unprepare;
1647
1648 error = sysc_init_idlemodes(ddata);
1649 if (error)
1650 goto unprepare;
1651
1652 error = sysc_init_syss_mask(ddata);
1653 if (error)
1654 goto unprepare;
1655
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001656 error = sysc_init_pdata(ddata);
1657 if (error)
1658 goto unprepare;
1659
Tony Lindgren50622362018-04-16 10:20:27 -07001660 error = sysc_init_resets(ddata);
1661 if (error)
1662 return error;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001663
Tony Lindgren50622362018-04-16 10:20:27 -07001664 pm_runtime_enable(ddata->dev);
Tony Lindgren566a9b052017-12-15 09:41:19 -08001665 error = sysc_init_module(ddata);
1666 if (error)
1667 goto unprepare;
1668
Tony Lindgren0eecc632017-10-10 14:23:43 -07001669 error = pm_runtime_get_sync(ddata->dev);
1670 if (error < 0) {
1671 pm_runtime_put_noidle(ddata->dev);
1672 pm_runtime_disable(ddata->dev);
1673 goto unprepare;
1674 }
1675
Tony Lindgren0eecc632017-10-10 14:23:43 -07001676 sysc_show_registers(ddata);
1677
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001678 ddata->dev->type = &sysc_device_type;
Tony Lindgrenc4bebea2018-04-16 10:24:54 -07001679 error = of_platform_populate(ddata->dev->of_node, sysc_match_table,
1680 pdata ? pdata->auxdata : NULL,
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001681 ddata->dev);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001682 if (error)
1683 goto err;
1684
Tony Lindgren76f0f772018-02-23 08:28:45 -08001685 INIT_DELAYED_WORK(&ddata->idle_work, ti_sysc_idle);
1686
1687 /* At least earlycon won't survive without deferred idle */
1688 if (ddata->cfg.quirks & (SYSC_QUIRK_NO_IDLE_ON_INIT |
1689 SYSC_QUIRK_NO_RESET_ON_INIT)) {
1690 schedule_delayed_work(&ddata->idle_work, 3000);
1691 } else {
1692 pm_runtime_put(&pdev->dev);
1693 }
Tony Lindgren0eecc632017-10-10 14:23:43 -07001694
Tony Lindgren50622362018-04-16 10:20:27 -07001695 if (!of_get_available_child_count(ddata->dev->of_node))
1696 reset_control_assert(ddata->rsts);
1697
Tony Lindgren0eecc632017-10-10 14:23:43 -07001698 return 0;
1699
1700err:
Tony Lindgren0eecc632017-10-10 14:23:43 -07001701 pm_runtime_put_sync(&pdev->dev);
1702 pm_runtime_disable(&pdev->dev);
1703unprepare:
1704 sysc_unprepare(ddata);
1705
1706 return error;
1707}
1708
Tony Lindgren684be5a2017-10-13 10:48:40 -07001709static int sysc_remove(struct platform_device *pdev)
1710{
1711 struct sysc *ddata = platform_get_drvdata(pdev);
1712 int error;
1713
Tony Lindgren76f0f772018-02-23 08:28:45 -08001714 cancel_delayed_work_sync(&ddata->idle_work);
1715
Tony Lindgren684be5a2017-10-13 10:48:40 -07001716 error = pm_runtime_get_sync(ddata->dev);
1717 if (error < 0) {
1718 pm_runtime_put_noidle(ddata->dev);
1719 pm_runtime_disable(ddata->dev);
1720 goto unprepare;
1721 }
1722
1723 of_platform_depopulate(&pdev->dev);
1724
Tony Lindgren684be5a2017-10-13 10:48:40 -07001725 pm_runtime_put_sync(&pdev->dev);
1726 pm_runtime_disable(&pdev->dev);
Tony Lindgren50622362018-04-16 10:20:27 -07001727 reset_control_assert(ddata->rsts);
Tony Lindgren684be5a2017-10-13 10:48:40 -07001728
1729unprepare:
1730 sysc_unprepare(ddata);
1731
1732 return 0;
1733}
1734
Tony Lindgren0eecc632017-10-10 14:23:43 -07001735static const struct of_device_id sysc_match[] = {
Tony Lindgren70a65242017-12-15 09:41:09 -08001736 { .compatible = "ti,sysc-omap2", .data = &sysc_omap2, },
1737 { .compatible = "ti,sysc-omap2-timer", .data = &sysc_omap2_timer, },
1738 { .compatible = "ti,sysc-omap4", .data = &sysc_omap4, },
1739 { .compatible = "ti,sysc-omap4-timer", .data = &sysc_omap4_timer, },
1740 { .compatible = "ti,sysc-omap4-simple", .data = &sysc_omap4_simple, },
1741 { .compatible = "ti,sysc-omap3430-sr", .data = &sysc_34xx_sr, },
1742 { .compatible = "ti,sysc-omap3630-sr", .data = &sysc_36xx_sr, },
1743 { .compatible = "ti,sysc-omap4-sr", .data = &sysc_omap4_sr, },
1744 { .compatible = "ti,sysc-omap3-sham", .data = &sysc_omap3_sham, },
1745 { .compatible = "ti,sysc-omap-aes", .data = &sysc_omap3_aes, },
1746 { .compatible = "ti,sysc-mcasp", .data = &sysc_omap4_mcasp, },
Tony Lindgren2c63a832018-11-15 14:46:53 -08001747 { .compatible = "ti,sysc-dra7-mcasp", .data = &sysc_dra7_mcasp, },
Tony Lindgren70a65242017-12-15 09:41:09 -08001748 { .compatible = "ti,sysc-usb-host-fs",
1749 .data = &sysc_omap4_usb_host_fs, },
Faiz Abbas7f35e632018-07-09 22:18:38 +05301750 { .compatible = "ti,sysc-dra7-mcan", .data = &sysc_dra7_mcan, },
Tony Lindgren0eecc632017-10-10 14:23:43 -07001751 { },
1752};
1753MODULE_DEVICE_TABLE(of, sysc_match);
1754
1755static struct platform_driver sysc_driver = {
1756 .probe = sysc_probe,
Tony Lindgren684be5a2017-10-13 10:48:40 -07001757 .remove = sysc_remove,
Tony Lindgren0eecc632017-10-10 14:23:43 -07001758 .driver = {
1759 .name = "ti-sysc",
1760 .of_match_table = sysc_match,
1761 .pm = &sysc_pm_ops,
1762 },
1763};
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001764
1765static int __init sysc_init(void)
1766{
1767 bus_register_notifier(&platform_bus_type, &sysc_nb);
1768
1769 return platform_driver_register(&sysc_driver);
1770}
1771module_init(sysc_init);
1772
1773static void __exit sysc_exit(void)
1774{
1775 bus_unregister_notifier(&platform_bus_type, &sysc_nb);
1776 platform_driver_unregister(&sysc_driver);
1777}
1778module_exit(sysc_exit);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001779
1780MODULE_DESCRIPTION("TI sysc interconnect target driver");
1781MODULE_LICENSE("GPL v2");