blob: 64c71f0f3f595cbb3f9ce903773ac93081296cef [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),
823 SYSC_QUIRK("prcm", 0, 0, -1, -1, 0x40000100, 0xffffffff, 0),
Tony Lindgren23731ea2018-09-24 12:17:09 -0700824 SYSC_QUIRK("prcm", 0, 0, -1, -1, 0x00004102, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700825 SYSC_QUIRK("prcm", 0, 0, -1, -1, 0x40000400, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -0700826 SYSC_QUIRK("scm", 0, 0, 0x10, -1, 0x40000900, 0xffffffff, 0),
Tony Lindgren23731ea2018-09-24 12:17:09 -0700827 SYSC_QUIRK("scm", 0, 0, -1, -1, 0x4e8b0100, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700828 SYSC_QUIRK("scm", 0, 0, -1, -1, 0x4f000100, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -0700829 SYSC_QUIRK("scm", 0, 0, -1, -1, 0x40000900, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -0700830 SYSC_QUIRK("scrm", 0, 0, -1, -1, 0x00000010, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -0700831 SYSC_QUIRK("sdio", 0, 0, 0x10, -1, 0x40202301, 0xffff0ff0, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700832 SYSC_QUIRK("sdio", 0, 0x2fc, 0x110, 0x114, 0x31010000, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -0700833 SYSC_QUIRK("sdma", 0, 0, 0x2c, 0x28, 0x00010900, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700834 SYSC_QUIRK("slimbus", 0, 0, 0x10, -1, 0x40000902, 0xffffffff, 0),
835 SYSC_QUIRK("slimbus", 0, 0, 0x10, -1, 0x40002903, 0xffffffff, 0),
836 SYSC_QUIRK("spinlock", 0, 0, 0x10, -1, 0x50020000, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700837 SYSC_QUIRK("rng", 0, 0x1fe0, 0x1fe4, -1, 0x00000020, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -0700838 SYSC_QUIRK("rtc", 0, 0x74, 0x78, -1, 0x4eb01908, 0xffff00f0, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700839 SYSC_QUIRK("timer32k", 0, 0, 0x4, -1, 0x00000060, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700840 SYSC_QUIRK("usbhstll", 0, 0, 0x10, 0x14, 0x00000004, 0xffffffff, 0),
841 SYSC_QUIRK("usb_host_hs", 0, 0, 0x10, 0x14, 0x50700100, 0xffffffff, 0),
842 SYSC_QUIRK("usb_otg_hs", 0, 0x400, 0x404, 0x408, 0x00000050,
843 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700844 SYSC_QUIRK("wdt", 0, 0, 0x10, 0x14, 0x502a0500, 0xfffff0f0, 0),
845 SYSC_QUIRK("vfpe", 0, 0, 0x104, -1, 0x4d001200, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700846#endif
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800847};
848
849static void sysc_init_revision_quirks(struct sysc *ddata)
850{
851 const struct sysc_revision_quirk *q;
852 int i;
853
854 for (i = 0; i < ARRAY_SIZE(sysc_revision_quirks); i++) {
855 q = &sysc_revision_quirks[i];
856
857 if (q->base && q->base != ddata->module_pa)
858 continue;
859
860 if (q->rev_offset >= 0 &&
861 q->rev_offset != ddata->offsets[SYSC_REVISION])
862 continue;
863
864 if (q->sysc_offset >= 0 &&
865 q->sysc_offset != ddata->offsets[SYSC_SYSCONFIG])
866 continue;
867
868 if (q->syss_offset >= 0 &&
869 q->syss_offset != ddata->offsets[SYSC_SYSSTATUS])
870 continue;
871
872 if (q->revision == ddata->revision ||
873 (q->revision & q->revision_mask) ==
874 (ddata->revision & q->revision_mask)) {
875 ddata->name = q->name;
876 ddata->cfg.quirks |= q->quirks;
877 }
878 }
879}
880
Faiz Abbas596e7952018-07-09 22:18:39 +0530881static int sysc_reset(struct sysc *ddata)
882{
883 int offset = ddata->offsets[SYSC_SYSCONFIG];
884 int val;
885
886 if (ddata->legacy_mode || offset < 0 ||
887 ddata->cfg.quirks & SYSC_QUIRK_NO_RESET_ON_INIT)
888 return 0;
889
890 /*
891 * Currently only support reset status in sysstatus.
892 * Warn and return error in all other cases
893 */
894 if (!ddata->cfg.syss_mask) {
895 dev_err(ddata->dev, "No ti,syss-mask. Reset failed\n");
896 return -EINVAL;
897 }
898
899 val = sysc_read(ddata, offset);
900 val |= (0x1 << ddata->cap->regbits->srst_shift);
901 sysc_write(ddata, offset, val);
902
903 /* Poll on reset status */
904 offset = ddata->offsets[SYSC_SYSSTATUS];
905
906 return readl_poll_timeout(ddata->module_va + offset, val,
907 (val & ddata->cfg.syss_mask) == 0x0,
908 100, MAX_MODULE_SOFTRESET_WAIT);
909}
910
Tony Lindgren566a9b052017-12-15 09:41:19 -0800911/* At this point the module is configured enough to read the revision */
912static int sysc_init_module(struct sysc *ddata)
913{
914 int error;
915
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800916 if (ddata->cfg.quirks & SYSC_QUIRK_NO_IDLE_ON_INIT) {
917 ddata->revision = sysc_read_revision(ddata);
918 goto rev_quirks;
919 }
920
Tony Lindgren566a9b052017-12-15 09:41:19 -0800921 error = pm_runtime_get_sync(ddata->dev);
922 if (error < 0) {
923 pm_runtime_put_noidle(ddata->dev);
924
925 return 0;
926 }
Tony Lindgren50622362018-04-16 10:20:27 -0700927
Faiz Abbas596e7952018-07-09 22:18:39 +0530928 error = sysc_reset(ddata);
929 if (error) {
930 dev_err(ddata->dev, "Reset failed with %d\n", error);
931 pm_runtime_put_sync(ddata->dev);
932
933 return error;
934 }
935
Tony Lindgren566a9b052017-12-15 09:41:19 -0800936 ddata->revision = sysc_read_revision(ddata);
937 pm_runtime_put_sync(ddata->dev);
938
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800939rev_quirks:
940 sysc_init_revision_quirks(ddata);
941
Tony Lindgren566a9b052017-12-15 09:41:19 -0800942 return 0;
943}
944
Tony Lindgrenc5a2de92017-12-15 09:41:23 -0800945static int sysc_init_sysc_mask(struct sysc *ddata)
946{
947 struct device_node *np = ddata->dev->of_node;
948 int error;
949 u32 val;
950
951 error = of_property_read_u32(np, "ti,sysc-mask", &val);
952 if (error)
953 return 0;
954
955 if (val)
956 ddata->cfg.sysc_val = val & ddata->cap->sysc_mask;
957 else
958 ddata->cfg.sysc_val = ddata->cap->sysc_mask;
959
960 return 0;
961}
962
963static int sysc_init_idlemode(struct sysc *ddata, u8 *idlemodes,
964 const char *name)
965{
966 struct device_node *np = ddata->dev->of_node;
967 struct property *prop;
968 const __be32 *p;
969 u32 val;
970
971 of_property_for_each_u32(np, name, prop, p, val) {
972 if (val >= SYSC_NR_IDLEMODES) {
973 dev_err(ddata->dev, "invalid idlemode: %i\n", val);
974 return -EINVAL;
975 }
976 *idlemodes |= (1 << val);
977 }
978
979 return 0;
980}
981
982static int sysc_init_idlemodes(struct sysc *ddata)
983{
984 int error;
985
986 error = sysc_init_idlemode(ddata, &ddata->cfg.midlemodes,
987 "ti,sysc-midle");
988 if (error)
989 return error;
990
991 error = sysc_init_idlemode(ddata, &ddata->cfg.sidlemodes,
992 "ti,sysc-sidle");
993 if (error)
994 return error;
995
996 return 0;
997}
998
999/*
1000 * Only some devices on omap4 and later have SYSCONFIG reset done
1001 * bit. We can detect this if there is no SYSSTATUS at all, or the
1002 * SYSTATUS bit 0 is not used. Note that some SYSSTATUS registers
1003 * have multiple bits for the child devices like OHCI and EHCI.
1004 * Depends on SYSC being parsed first.
1005 */
1006static int sysc_init_syss_mask(struct sysc *ddata)
1007{
1008 struct device_node *np = ddata->dev->of_node;
1009 int error;
1010 u32 val;
1011
1012 error = of_property_read_u32(np, "ti,syss-mask", &val);
1013 if (error) {
1014 if ((ddata->cap->type == TI_SYSC_OMAP4 ||
1015 ddata->cap->type == TI_SYSC_OMAP4_TIMER) &&
1016 (ddata->cfg.sysc_val & SYSC_OMAP4_SOFTRESET))
1017 ddata->cfg.quirks |= SYSC_QUIRK_RESET_STATUS;
1018
1019 return 0;
1020 }
1021
1022 if (!(val & 1) && (ddata->cfg.sysc_val & SYSC_OMAP4_SOFTRESET))
1023 ddata->cfg.quirks |= SYSC_QUIRK_RESET_STATUS;
1024
1025 ddata->cfg.syss_mask = val;
1026
1027 return 0;
1028}
1029
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001030/*
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001031 * Many child device drivers need to have fck and opt clocks available
1032 * to get the clock rate for device internal configuration etc.
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001033 */
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001034static int sysc_child_add_named_clock(struct sysc *ddata,
1035 struct device *child,
1036 const char *name)
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001037{
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001038 struct clk *clk;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001039 struct clk_lookup *l;
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001040 int error = 0;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001041
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001042 if (!name)
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001043 return 0;
1044
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001045 clk = clk_get(child, name);
1046 if (!IS_ERR(clk)) {
1047 clk_put(clk);
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001048
1049 return -EEXIST;
1050 }
1051
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001052 clk = clk_get(ddata->dev, name);
1053 if (IS_ERR(clk))
1054 return -ENODEV;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001055
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001056 l = clkdev_create(clk, name, dev_name(child));
1057 if (!l)
1058 error = -ENOMEM;
1059
1060 clk_put(clk);
1061
1062 return error;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001063}
1064
Tony Lindgren09dfe582018-04-16 10:25:52 -07001065static int sysc_child_add_clocks(struct sysc *ddata,
1066 struct device *child)
1067{
1068 int i, error;
1069
1070 for (i = 0; i < ddata->nr_clocks; i++) {
1071 error = sysc_child_add_named_clock(ddata,
1072 child,
1073 ddata->clock_roles[i]);
1074 if (error && error != -EEXIST) {
1075 dev_err(ddata->dev, "could not add child clock %s: %i\n",
1076 ddata->clock_roles[i], error);
1077
1078 return error;
1079 }
1080 }
1081
1082 return 0;
1083}
1084
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001085static struct device_type sysc_device_type = {
1086};
1087
1088static struct sysc *sysc_child_to_parent(struct device *dev)
1089{
1090 struct device *parent = dev->parent;
1091
1092 if (!parent || parent->type != &sysc_device_type)
1093 return NULL;
1094
1095 return dev_get_drvdata(parent);
1096}
1097
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001098static int __maybe_unused sysc_child_runtime_suspend(struct device *dev)
1099{
1100 struct sysc *ddata;
1101 int error;
1102
1103 ddata = sysc_child_to_parent(dev);
1104
1105 error = pm_generic_runtime_suspend(dev);
1106 if (error)
1107 return error;
1108
1109 if (!ddata->enabled)
1110 return 0;
1111
1112 return sysc_runtime_suspend(ddata->dev);
1113}
1114
1115static int __maybe_unused sysc_child_runtime_resume(struct device *dev)
1116{
1117 struct sysc *ddata;
1118 int error;
1119
1120 ddata = sysc_child_to_parent(dev);
1121
1122 if (!ddata->enabled) {
1123 error = sysc_runtime_resume(ddata->dev);
1124 if (error < 0)
1125 dev_err(ddata->dev,
1126 "%s error: %i\n", __func__, error);
1127 }
1128
1129 return pm_generic_runtime_resume(dev);
1130}
1131
1132#ifdef CONFIG_PM_SLEEP
1133static int sysc_child_suspend_noirq(struct device *dev)
1134{
1135 struct sysc *ddata;
1136 int error;
1137
1138 ddata = sysc_child_to_parent(dev);
1139
Tony Lindgrenef55f822018-04-16 10:27:15 -07001140 dev_dbg(ddata->dev, "%s %s\n", __func__,
1141 ddata->name ? ddata->name : "");
1142
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001143 error = pm_generic_suspend_noirq(dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001144 if (error) {
1145 dev_err(dev, "%s error at %i: %i\n",
1146 __func__, __LINE__, error);
1147
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001148 return error;
Tony Lindgrenef55f822018-04-16 10:27:15 -07001149 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001150
1151 if (!pm_runtime_status_suspended(dev)) {
1152 error = pm_generic_runtime_suspend(dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001153 if (error) {
Tony Lindgrenf9490782018-09-28 15:21:50 -07001154 dev_dbg(dev, "%s busy at %i: %i\n",
1155 __func__, __LINE__, error);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001156
Tony Lindgren4f3530f2018-08-08 01:07:06 -07001157 return 0;
Tony Lindgrenef55f822018-04-16 10:27:15 -07001158 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001159
1160 error = sysc_runtime_suspend(ddata->dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001161 if (error) {
1162 dev_err(dev, "%s error at %i: %i\n",
1163 __func__, __LINE__, error);
1164
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001165 return error;
Tony Lindgrenef55f822018-04-16 10:27:15 -07001166 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001167
1168 ddata->child_needs_resume = true;
1169 }
1170
1171 return 0;
1172}
1173
1174static int sysc_child_resume_noirq(struct device *dev)
1175{
1176 struct sysc *ddata;
1177 int error;
1178
1179 ddata = sysc_child_to_parent(dev);
1180
Tony Lindgrenef55f822018-04-16 10:27:15 -07001181 dev_dbg(ddata->dev, "%s %s\n", __func__,
1182 ddata->name ? ddata->name : "");
1183
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001184 if (ddata->child_needs_resume) {
1185 ddata->child_needs_resume = false;
1186
1187 error = sysc_runtime_resume(ddata->dev);
1188 if (error)
1189 dev_err(ddata->dev,
1190 "%s runtime resume error: %i\n",
1191 __func__, error);
1192
1193 error = pm_generic_runtime_resume(dev);
1194 if (error)
1195 dev_err(ddata->dev,
1196 "%s generic runtime resume: %i\n",
1197 __func__, error);
1198 }
1199
1200 return pm_generic_resume_noirq(dev);
1201}
1202#endif
1203
1204struct dev_pm_domain sysc_child_pm_domain = {
1205 .ops = {
1206 SET_RUNTIME_PM_OPS(sysc_child_runtime_suspend,
1207 sysc_child_runtime_resume,
1208 NULL)
1209 USE_PLATFORM_PM_SLEEP_OPS
1210 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(sysc_child_suspend_noirq,
1211 sysc_child_resume_noirq)
1212 }
1213};
1214
1215/**
1216 * sysc_legacy_idle_quirk - handle children in omap_device compatible way
1217 * @ddata: device driver data
1218 * @child: child device driver
1219 *
1220 * Allow idle for child devices as done with _od_runtime_suspend().
1221 * Otherwise many child devices will not idle because of the permanent
1222 * parent usecount set in pm_runtime_irq_safe().
1223 *
1224 * Note that the long term solution is to just modify the child device
1225 * drivers to not set pm_runtime_irq_safe() and then this can be just
1226 * dropped.
1227 */
1228static void sysc_legacy_idle_quirk(struct sysc *ddata, struct device *child)
1229{
1230 if (!ddata->legacy_mode)
1231 return;
1232
1233 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
1234 dev_pm_domain_set(child, &sysc_child_pm_domain);
1235}
1236
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001237static int sysc_notifier_call(struct notifier_block *nb,
1238 unsigned long event, void *device)
1239{
1240 struct device *dev = device;
1241 struct sysc *ddata;
1242 int error;
1243
1244 ddata = sysc_child_to_parent(dev);
1245 if (!ddata)
1246 return NOTIFY_DONE;
1247
1248 switch (event) {
1249 case BUS_NOTIFY_ADD_DEVICE:
Tony Lindgren09dfe582018-04-16 10:25:52 -07001250 error = sysc_child_add_clocks(ddata, dev);
1251 if (error)
1252 return error;
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001253 sysc_legacy_idle_quirk(ddata, dev);
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001254 break;
1255 default:
1256 break;
1257 }
1258
1259 return NOTIFY_DONE;
1260}
1261
1262static struct notifier_block sysc_nb = {
1263 .notifier_call = sysc_notifier_call,
1264};
1265
Tony Lindgren566a9b052017-12-15 09:41:19 -08001266/* Device tree configured quirks */
1267struct sysc_dts_quirk {
1268 const char *name;
1269 u32 mask;
1270};
1271
1272static const struct sysc_dts_quirk sysc_dts_quirks[] = {
1273 { .name = "ti,no-idle-on-init",
1274 .mask = SYSC_QUIRK_NO_IDLE_ON_INIT, },
1275 { .name = "ti,no-reset-on-init",
1276 .mask = SYSC_QUIRK_NO_RESET_ON_INIT, },
1277};
1278
1279static int sysc_init_dts_quirks(struct sysc *ddata)
1280{
1281 struct device_node *np = ddata->dev->of_node;
1282 const struct property *prop;
1283 int i, len, error;
1284 u32 val;
1285
1286 ddata->legacy_mode = of_get_property(np, "ti,hwmods", NULL);
1287
1288 for (i = 0; i < ARRAY_SIZE(sysc_dts_quirks); i++) {
1289 prop = of_get_property(np, sysc_dts_quirks[i].name, &len);
1290 if (!prop)
Tony Lindgrend39b6ea2018-02-15 09:18:55 -08001291 continue;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001292
1293 ddata->cfg.quirks |= sysc_dts_quirks[i].mask;
1294 }
1295
1296 error = of_property_read_u32(np, "ti,sysc-delay-us", &val);
1297 if (!error) {
1298 if (val > 255) {
1299 dev_warn(ddata->dev, "bad ti,sysc-delay-us: %i\n",
1300 val);
1301 }
1302
1303 ddata->cfg.srst_udelay = (u8)val;
1304 }
1305
1306 return 0;
1307}
1308
Tony Lindgren0eecc632017-10-10 14:23:43 -07001309static void sysc_unprepare(struct sysc *ddata)
1310{
1311 int i;
1312
1313 for (i = 0; i < SYSC_MAX_CLOCKS; i++) {
1314 if (!IS_ERR_OR_NULL(ddata->clocks[i]))
1315 clk_unprepare(ddata->clocks[i]);
1316 }
1317}
1318
Tony Lindgren70a65242017-12-15 09:41:09 -08001319/*
1320 * Common sysc register bits found on omap2, also known as type1
1321 */
1322static const struct sysc_regbits sysc_regbits_omap2 = {
1323 .dmadisable_shift = -ENODEV,
1324 .midle_shift = 12,
1325 .sidle_shift = 3,
1326 .clkact_shift = 8,
1327 .emufree_shift = 5,
1328 .enwkup_shift = 2,
1329 .srst_shift = 1,
1330 .autoidle_shift = 0,
1331};
1332
1333static const struct sysc_capabilities sysc_omap2 = {
1334 .type = TI_SYSC_OMAP2,
1335 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY | SYSC_OMAP2_EMUFREE |
1336 SYSC_OMAP2_ENAWAKEUP | SYSC_OMAP2_SOFTRESET |
1337 SYSC_OMAP2_AUTOIDLE,
1338 .regbits = &sysc_regbits_omap2,
1339};
1340
1341/* All omap2 and 3 timers, and timers 1, 2 & 10 on omap 4 and 5 */
1342static const struct sysc_capabilities sysc_omap2_timer = {
1343 .type = TI_SYSC_OMAP2_TIMER,
1344 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY | SYSC_OMAP2_EMUFREE |
1345 SYSC_OMAP2_ENAWAKEUP | SYSC_OMAP2_SOFTRESET |
1346 SYSC_OMAP2_AUTOIDLE,
1347 .regbits = &sysc_regbits_omap2,
1348 .mod_quirks = SYSC_QUIRK_USE_CLOCKACT,
1349};
1350
1351/*
1352 * SHAM2 (SHA1/MD5) sysc found on omap3, a variant of sysc_regbits_omap2
1353 * with different sidle position
1354 */
1355static const struct sysc_regbits sysc_regbits_omap3_sham = {
1356 .dmadisable_shift = -ENODEV,
1357 .midle_shift = -ENODEV,
1358 .sidle_shift = 4,
1359 .clkact_shift = -ENODEV,
1360 .enwkup_shift = -ENODEV,
1361 .srst_shift = 1,
1362 .autoidle_shift = 0,
1363 .emufree_shift = -ENODEV,
1364};
1365
1366static const struct sysc_capabilities sysc_omap3_sham = {
1367 .type = TI_SYSC_OMAP3_SHAM,
1368 .sysc_mask = SYSC_OMAP2_SOFTRESET | SYSC_OMAP2_AUTOIDLE,
1369 .regbits = &sysc_regbits_omap3_sham,
1370};
1371
1372/*
1373 * AES register bits found on omap3 and later, a variant of
1374 * sysc_regbits_omap2 with different sidle position
1375 */
1376static const struct sysc_regbits sysc_regbits_omap3_aes = {
1377 .dmadisable_shift = -ENODEV,
1378 .midle_shift = -ENODEV,
1379 .sidle_shift = 6,
1380 .clkact_shift = -ENODEV,
1381 .enwkup_shift = -ENODEV,
1382 .srst_shift = 1,
1383 .autoidle_shift = 0,
1384 .emufree_shift = -ENODEV,
1385};
1386
1387static const struct sysc_capabilities sysc_omap3_aes = {
1388 .type = TI_SYSC_OMAP3_AES,
1389 .sysc_mask = SYSC_OMAP2_SOFTRESET | SYSC_OMAP2_AUTOIDLE,
1390 .regbits = &sysc_regbits_omap3_aes,
1391};
1392
1393/*
1394 * Common sysc register bits found on omap4, also known as type2
1395 */
1396static const struct sysc_regbits sysc_regbits_omap4 = {
1397 .dmadisable_shift = 16,
1398 .midle_shift = 4,
1399 .sidle_shift = 2,
1400 .clkact_shift = -ENODEV,
1401 .enwkup_shift = -ENODEV,
1402 .emufree_shift = 1,
1403 .srst_shift = 0,
1404 .autoidle_shift = -ENODEV,
1405};
1406
1407static const struct sysc_capabilities sysc_omap4 = {
1408 .type = TI_SYSC_OMAP4,
1409 .sysc_mask = SYSC_OMAP4_DMADISABLE | SYSC_OMAP4_FREEEMU |
1410 SYSC_OMAP4_SOFTRESET,
1411 .regbits = &sysc_regbits_omap4,
1412};
1413
1414static const struct sysc_capabilities sysc_omap4_timer = {
1415 .type = TI_SYSC_OMAP4_TIMER,
1416 .sysc_mask = SYSC_OMAP4_DMADISABLE | SYSC_OMAP4_FREEEMU |
1417 SYSC_OMAP4_SOFTRESET,
1418 .regbits = &sysc_regbits_omap4,
1419};
1420
1421/*
1422 * Common sysc register bits found on omap4, also known as type3
1423 */
1424static const struct sysc_regbits sysc_regbits_omap4_simple = {
1425 .dmadisable_shift = -ENODEV,
1426 .midle_shift = 2,
1427 .sidle_shift = 0,
1428 .clkact_shift = -ENODEV,
1429 .enwkup_shift = -ENODEV,
1430 .srst_shift = -ENODEV,
1431 .emufree_shift = -ENODEV,
1432 .autoidle_shift = -ENODEV,
1433};
1434
1435static const struct sysc_capabilities sysc_omap4_simple = {
1436 .type = TI_SYSC_OMAP4_SIMPLE,
1437 .regbits = &sysc_regbits_omap4_simple,
1438};
1439
1440/*
1441 * SmartReflex sysc found on omap34xx
1442 */
1443static const struct sysc_regbits sysc_regbits_omap34xx_sr = {
1444 .dmadisable_shift = -ENODEV,
1445 .midle_shift = -ENODEV,
1446 .sidle_shift = -ENODEV,
1447 .clkact_shift = 20,
1448 .enwkup_shift = -ENODEV,
1449 .srst_shift = -ENODEV,
1450 .emufree_shift = -ENODEV,
1451 .autoidle_shift = -ENODEV,
1452};
1453
1454static const struct sysc_capabilities sysc_34xx_sr = {
1455 .type = TI_SYSC_OMAP34XX_SR,
1456 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY,
1457 .regbits = &sysc_regbits_omap34xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001458 .mod_quirks = SYSC_QUIRK_USE_CLOCKACT | SYSC_QUIRK_UNCACHED |
1459 SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08001460};
1461
1462/*
1463 * SmartReflex sysc found on omap36xx and later
1464 */
1465static const struct sysc_regbits sysc_regbits_omap36xx_sr = {
1466 .dmadisable_shift = -ENODEV,
1467 .midle_shift = -ENODEV,
1468 .sidle_shift = 24,
1469 .clkact_shift = -ENODEV,
1470 .enwkup_shift = 26,
1471 .srst_shift = -ENODEV,
1472 .emufree_shift = -ENODEV,
1473 .autoidle_shift = -ENODEV,
1474};
1475
1476static const struct sysc_capabilities sysc_36xx_sr = {
1477 .type = TI_SYSC_OMAP36XX_SR,
Tony Lindgren3267c082018-01-22 09:32:53 -08001478 .sysc_mask = SYSC_OMAP3_SR_ENAWAKEUP,
Tony Lindgren70a65242017-12-15 09:41:09 -08001479 .regbits = &sysc_regbits_omap36xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001480 .mod_quirks = SYSC_QUIRK_UNCACHED | SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08001481};
1482
1483static const struct sysc_capabilities sysc_omap4_sr = {
1484 .type = TI_SYSC_OMAP4_SR,
1485 .regbits = &sysc_regbits_omap36xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001486 .mod_quirks = SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08001487};
1488
1489/*
1490 * McASP register bits found on omap4 and later
1491 */
1492static const struct sysc_regbits sysc_regbits_omap4_mcasp = {
1493 .dmadisable_shift = -ENODEV,
1494 .midle_shift = -ENODEV,
1495 .sidle_shift = 0,
1496 .clkact_shift = -ENODEV,
1497 .enwkup_shift = -ENODEV,
1498 .srst_shift = -ENODEV,
1499 .emufree_shift = -ENODEV,
1500 .autoidle_shift = -ENODEV,
1501};
1502
1503static const struct sysc_capabilities sysc_omap4_mcasp = {
1504 .type = TI_SYSC_OMAP4_MCASP,
1505 .regbits = &sysc_regbits_omap4_mcasp,
Tony Lindgren2c63a832018-11-15 14:46:53 -08001506 .mod_quirks = SYSC_QUIRK_OPT_CLKS_NEEDED,
1507};
1508
1509/*
1510 * McASP found on dra7 and later
1511 */
1512static const struct sysc_capabilities sysc_dra7_mcasp = {
1513 .type = TI_SYSC_OMAP4_SIMPLE,
1514 .regbits = &sysc_regbits_omap4_simple,
1515 .mod_quirks = SYSC_QUIRK_OPT_CLKS_NEEDED,
Tony Lindgren70a65242017-12-15 09:41:09 -08001516};
1517
1518/*
1519 * FS USB host found on omap4 and later
1520 */
1521static const struct sysc_regbits sysc_regbits_omap4_usb_host_fs = {
1522 .dmadisable_shift = -ENODEV,
1523 .midle_shift = -ENODEV,
1524 .sidle_shift = 24,
1525 .clkact_shift = -ENODEV,
1526 .enwkup_shift = 26,
1527 .srst_shift = -ENODEV,
1528 .emufree_shift = -ENODEV,
1529 .autoidle_shift = -ENODEV,
1530};
1531
1532static const struct sysc_capabilities sysc_omap4_usb_host_fs = {
1533 .type = TI_SYSC_OMAP4_USB_HOST_FS,
1534 .sysc_mask = SYSC_OMAP2_ENAWAKEUP,
1535 .regbits = &sysc_regbits_omap4_usb_host_fs,
1536};
1537
Faiz Abbas7f35e632018-07-09 22:18:38 +05301538static const struct sysc_regbits sysc_regbits_dra7_mcan = {
1539 .dmadisable_shift = -ENODEV,
1540 .midle_shift = -ENODEV,
1541 .sidle_shift = -ENODEV,
1542 .clkact_shift = -ENODEV,
1543 .enwkup_shift = 4,
1544 .srst_shift = 0,
1545 .emufree_shift = -ENODEV,
1546 .autoidle_shift = -ENODEV,
1547};
1548
1549static const struct sysc_capabilities sysc_dra7_mcan = {
1550 .type = TI_SYSC_DRA7_MCAN,
1551 .sysc_mask = SYSC_DRA7_MCAN_ENAWAKEUP | SYSC_OMAP4_SOFTRESET,
1552 .regbits = &sysc_regbits_dra7_mcan,
1553};
1554
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001555static int sysc_init_pdata(struct sysc *ddata)
1556{
1557 struct ti_sysc_platform_data *pdata = dev_get_platdata(ddata->dev);
1558 struct ti_sysc_module_data mdata;
1559 int error = 0;
1560
1561 if (!pdata || !ddata->legacy_mode)
1562 return 0;
1563
1564 mdata.name = ddata->legacy_mode;
1565 mdata.module_pa = ddata->module_pa;
1566 mdata.module_size = ddata->module_size;
1567 mdata.offsets = ddata->offsets;
1568 mdata.nr_offsets = SYSC_MAX_REGS;
1569 mdata.cap = ddata->cap;
1570 mdata.cfg = &ddata->cfg;
1571
1572 if (!pdata->init_module)
1573 return -ENODEV;
1574
1575 error = pdata->init_module(ddata->dev, &mdata, &ddata->cookie);
1576 if (error == -EEXIST)
1577 error = 0;
1578
1579 return error;
1580}
1581
Tony Lindgren70a65242017-12-15 09:41:09 -08001582static int sysc_init_match(struct sysc *ddata)
1583{
1584 const struct sysc_capabilities *cap;
1585
1586 cap = of_device_get_match_data(ddata->dev);
1587 if (!cap)
1588 return -EINVAL;
1589
1590 ddata->cap = cap;
1591 if (ddata->cap)
1592 ddata->cfg.quirks |= ddata->cap->mod_quirks;
1593
1594 return 0;
1595}
1596
Tony Lindgren76f0f772018-02-23 08:28:45 -08001597static void ti_sysc_idle(struct work_struct *work)
1598{
1599 struct sysc *ddata;
1600
1601 ddata = container_of(work, struct sysc, idle_work.work);
1602
1603 if (pm_runtime_active(ddata->dev))
1604 pm_runtime_put_sync(ddata->dev);
1605}
1606
Tony Lindgrenc4bebea2018-04-16 10:24:54 -07001607static const struct of_device_id sysc_match_table[] = {
1608 { .compatible = "simple-bus", },
1609 { /* sentinel */ },
1610};
1611
Tony Lindgren0eecc632017-10-10 14:23:43 -07001612static int sysc_probe(struct platform_device *pdev)
1613{
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001614 struct ti_sysc_platform_data *pdata = dev_get_platdata(&pdev->dev);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001615 struct sysc *ddata;
1616 int error;
1617
1618 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
1619 if (!ddata)
1620 return -ENOMEM;
1621
1622 ddata->dev = &pdev->dev;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001623 platform_set_drvdata(pdev, ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001624
Tony Lindgren70a65242017-12-15 09:41:09 -08001625 error = sysc_init_match(ddata);
1626 if (error)
1627 return error;
1628
Tony Lindgren566a9b052017-12-15 09:41:19 -08001629 error = sysc_init_dts_quirks(ddata);
1630 if (error)
1631 goto unprepare;
1632
Tony Lindgren0eecc632017-10-10 14:23:43 -07001633 error = sysc_get_clocks(ddata);
1634 if (error)
1635 return error;
1636
1637 error = sysc_map_and_check_registers(ddata);
1638 if (error)
1639 goto unprepare;
1640
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08001641 error = sysc_init_sysc_mask(ddata);
1642 if (error)
1643 goto unprepare;
1644
1645 error = sysc_init_idlemodes(ddata);
1646 if (error)
1647 goto unprepare;
1648
1649 error = sysc_init_syss_mask(ddata);
1650 if (error)
1651 goto unprepare;
1652
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001653 error = sysc_init_pdata(ddata);
1654 if (error)
1655 goto unprepare;
1656
Tony Lindgren50622362018-04-16 10:20:27 -07001657 error = sysc_init_resets(ddata);
1658 if (error)
1659 return error;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001660
Tony Lindgren50622362018-04-16 10:20:27 -07001661 pm_runtime_enable(ddata->dev);
Tony Lindgren566a9b052017-12-15 09:41:19 -08001662 error = sysc_init_module(ddata);
1663 if (error)
1664 goto unprepare;
1665
Tony Lindgren0eecc632017-10-10 14:23:43 -07001666 error = pm_runtime_get_sync(ddata->dev);
1667 if (error < 0) {
1668 pm_runtime_put_noidle(ddata->dev);
1669 pm_runtime_disable(ddata->dev);
1670 goto unprepare;
1671 }
1672
Tony Lindgren0eecc632017-10-10 14:23:43 -07001673 sysc_show_registers(ddata);
1674
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001675 ddata->dev->type = &sysc_device_type;
Tony Lindgrenc4bebea2018-04-16 10:24:54 -07001676 error = of_platform_populate(ddata->dev->of_node, sysc_match_table,
1677 pdata ? pdata->auxdata : NULL,
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001678 ddata->dev);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001679 if (error)
1680 goto err;
1681
Tony Lindgren76f0f772018-02-23 08:28:45 -08001682 INIT_DELAYED_WORK(&ddata->idle_work, ti_sysc_idle);
1683
1684 /* At least earlycon won't survive without deferred idle */
1685 if (ddata->cfg.quirks & (SYSC_QUIRK_NO_IDLE_ON_INIT |
1686 SYSC_QUIRK_NO_RESET_ON_INIT)) {
1687 schedule_delayed_work(&ddata->idle_work, 3000);
1688 } else {
1689 pm_runtime_put(&pdev->dev);
1690 }
Tony Lindgren0eecc632017-10-10 14:23:43 -07001691
Tony Lindgren50622362018-04-16 10:20:27 -07001692 if (!of_get_available_child_count(ddata->dev->of_node))
1693 reset_control_assert(ddata->rsts);
1694
Tony Lindgren0eecc632017-10-10 14:23:43 -07001695 return 0;
1696
1697err:
Tony Lindgren0eecc632017-10-10 14:23:43 -07001698 pm_runtime_put_sync(&pdev->dev);
1699 pm_runtime_disable(&pdev->dev);
1700unprepare:
1701 sysc_unprepare(ddata);
1702
1703 return error;
1704}
1705
Tony Lindgren684be5a2017-10-13 10:48:40 -07001706static int sysc_remove(struct platform_device *pdev)
1707{
1708 struct sysc *ddata = platform_get_drvdata(pdev);
1709 int error;
1710
Tony Lindgren76f0f772018-02-23 08:28:45 -08001711 cancel_delayed_work_sync(&ddata->idle_work);
1712
Tony Lindgren684be5a2017-10-13 10:48:40 -07001713 error = pm_runtime_get_sync(ddata->dev);
1714 if (error < 0) {
1715 pm_runtime_put_noidle(ddata->dev);
1716 pm_runtime_disable(ddata->dev);
1717 goto unprepare;
1718 }
1719
1720 of_platform_depopulate(&pdev->dev);
1721
Tony Lindgren684be5a2017-10-13 10:48:40 -07001722 pm_runtime_put_sync(&pdev->dev);
1723 pm_runtime_disable(&pdev->dev);
Tony Lindgren50622362018-04-16 10:20:27 -07001724 reset_control_assert(ddata->rsts);
Tony Lindgren684be5a2017-10-13 10:48:40 -07001725
1726unprepare:
1727 sysc_unprepare(ddata);
1728
1729 return 0;
1730}
1731
Tony Lindgren0eecc632017-10-10 14:23:43 -07001732static const struct of_device_id sysc_match[] = {
Tony Lindgren70a65242017-12-15 09:41:09 -08001733 { .compatible = "ti,sysc-omap2", .data = &sysc_omap2, },
1734 { .compatible = "ti,sysc-omap2-timer", .data = &sysc_omap2_timer, },
1735 { .compatible = "ti,sysc-omap4", .data = &sysc_omap4, },
1736 { .compatible = "ti,sysc-omap4-timer", .data = &sysc_omap4_timer, },
1737 { .compatible = "ti,sysc-omap4-simple", .data = &sysc_omap4_simple, },
1738 { .compatible = "ti,sysc-omap3430-sr", .data = &sysc_34xx_sr, },
1739 { .compatible = "ti,sysc-omap3630-sr", .data = &sysc_36xx_sr, },
1740 { .compatible = "ti,sysc-omap4-sr", .data = &sysc_omap4_sr, },
1741 { .compatible = "ti,sysc-omap3-sham", .data = &sysc_omap3_sham, },
1742 { .compatible = "ti,sysc-omap-aes", .data = &sysc_omap3_aes, },
1743 { .compatible = "ti,sysc-mcasp", .data = &sysc_omap4_mcasp, },
Tony Lindgren2c63a832018-11-15 14:46:53 -08001744 { .compatible = "ti,sysc-dra7-mcasp", .data = &sysc_dra7_mcasp, },
Tony Lindgren70a65242017-12-15 09:41:09 -08001745 { .compatible = "ti,sysc-usb-host-fs",
1746 .data = &sysc_omap4_usb_host_fs, },
Faiz Abbas7f35e632018-07-09 22:18:38 +05301747 { .compatible = "ti,sysc-dra7-mcan", .data = &sysc_dra7_mcan, },
Tony Lindgren0eecc632017-10-10 14:23:43 -07001748 { },
1749};
1750MODULE_DEVICE_TABLE(of, sysc_match);
1751
1752static struct platform_driver sysc_driver = {
1753 .probe = sysc_probe,
Tony Lindgren684be5a2017-10-13 10:48:40 -07001754 .remove = sysc_remove,
Tony Lindgren0eecc632017-10-10 14:23:43 -07001755 .driver = {
1756 .name = "ti-sysc",
1757 .of_match_table = sysc_match,
1758 .pm = &sysc_pm_ops,
1759 },
1760};
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001761
1762static int __init sysc_init(void)
1763{
1764 bus_register_notifier(&platform_bus_type, &sysc_nb);
1765
1766 return platform_driver_register(&sysc_driver);
1767}
1768module_init(sysc_init);
1769
1770static void __exit sysc_exit(void)
1771{
1772 bus_unregister_notifier(&platform_bus_type, &sysc_nb);
1773 platform_driver_unregister(&sysc_driver);
1774}
1775module_exit(sysc_exit);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001776
1777MODULE_DESCRIPTION("TI sysc interconnect target driver");
1778MODULE_LICENSE("GPL v2");