blob: 9cda6994920a0fd11b87b6342fa1fc9b42583705 [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 Lindgren40d9f912018-09-24 12:16:54 -070090 unsigned int noirq_suspend:1;
Tony Lindgrena885f0f2018-02-22 14:03:48 -080091 bool child_needs_resume;
Tony Lindgren76f0f772018-02-23 08:28:45 -080092 struct delayed_work idle_work;
Tony Lindgren0eecc632017-10-10 14:23:43 -070093};
94
Faiz Abbas596e7952018-07-09 22:18:39 +053095void sysc_write(struct sysc *ddata, int offset, u32 value)
96{
97 writel_relaxed(value, ddata->module_va + offset);
98}
99
Tony Lindgren566a9b052017-12-15 09:41:19 -0800100static u32 sysc_read(struct sysc *ddata, int offset)
101{
102 if (ddata->cfg.quirks & SYSC_QUIRK_16BIT) {
103 u32 val;
104
105 val = readw_relaxed(ddata->module_va + offset);
106 val |= (readw_relaxed(ddata->module_va + offset + 4) << 16);
107
108 return val;
109 }
110
111 return readl_relaxed(ddata->module_va + offset);
112}
113
Tony Lindgren09dfe582018-04-16 10:25:52 -0700114static bool sysc_opt_clks_needed(struct sysc *ddata)
115{
116 return !!(ddata->cfg.quirks & SYSC_QUIRK_OPT_CLKS_NEEDED);
117}
118
Tony Lindgren0eecc632017-10-10 14:23:43 -0700119static u32 sysc_read_revision(struct sysc *ddata)
120{
Tony Lindgren566a9b052017-12-15 09:41:19 -0800121 int offset = ddata->offsets[SYSC_REVISION];
122
123 if (offset < 0)
124 return 0;
125
126 return sysc_read(ddata, offset);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700127}
128
Tony Lindgren09dfe582018-04-16 10:25:52 -0700129static int sysc_get_one_clock(struct sysc *ddata, const char *name)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700130{
Tony Lindgren09dfe582018-04-16 10:25:52 -0700131 int error, i, index = -ENODEV;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700132
Tony Lindgren09dfe582018-04-16 10:25:52 -0700133 if (!strncmp(clock_names[SYSC_FCK], name, 3))
134 index = SYSC_FCK;
135 else if (!strncmp(clock_names[SYSC_ICK], name, 3))
136 index = SYSC_ICK;
137
138 if (index < 0) {
139 for (i = SYSC_OPTFCK0; i < SYSC_MAX_CLOCKS; i++) {
Tony Lindgrenc97c8622018-05-18 07:54:44 -0700140 if (!ddata->clocks[i]) {
Tony Lindgren09dfe582018-04-16 10:25:52 -0700141 index = i;
142 break;
143 }
144 }
Tony Lindgren0eecc632017-10-10 14:23:43 -0700145 }
Tony Lindgren09dfe582018-04-16 10:25:52 -0700146
147 if (index < 0) {
148 dev_err(ddata->dev, "clock %s not added\n", name);
149 return index;
150 }
Tony Lindgren0eecc632017-10-10 14:23:43 -0700151
152 ddata->clocks[index] = devm_clk_get(ddata->dev, name);
153 if (IS_ERR(ddata->clocks[index])) {
154 if (PTR_ERR(ddata->clocks[index]) == -ENOENT)
155 return 0;
156
157 dev_err(ddata->dev, "clock get error for %s: %li\n",
158 name, PTR_ERR(ddata->clocks[index]));
159
160 return PTR_ERR(ddata->clocks[index]);
161 }
162
163 error = clk_prepare(ddata->clocks[index]);
164 if (error) {
165 dev_err(ddata->dev, "clock prepare error for %s: %i\n",
166 name, error);
167
168 return error;
169 }
170
171 return 0;
172}
173
174static int sysc_get_clocks(struct sysc *ddata)
175{
Tony Lindgren09dfe582018-04-16 10:25:52 -0700176 struct device_node *np = ddata->dev->of_node;
177 struct property *prop;
178 const char *name;
179 int nr_fck = 0, nr_ick = 0, i, error = 0;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700180
Kees Cook20749052018-06-18 10:45:49 -0700181 ddata->clock_roles = devm_kcalloc(ddata->dev,
Tony Lindgren09dfe582018-04-16 10:25:52 -0700182 SYSC_MAX_CLOCKS,
Kees Cook20749052018-06-18 10:45:49 -0700183 sizeof(*ddata->clock_roles),
Tony Lindgren09dfe582018-04-16 10:25:52 -0700184 GFP_KERNEL);
185 if (!ddata->clock_roles)
186 return -ENOMEM;
187
188 of_property_for_each_string(np, "clock-names", prop, name) {
189 if (!strncmp(clock_names[SYSC_FCK], name, 3))
190 nr_fck++;
191 if (!strncmp(clock_names[SYSC_ICK], name, 3))
192 nr_ick++;
193 ddata->clock_roles[ddata->nr_clocks] = name;
194 ddata->nr_clocks++;
195 }
196
197 if (ddata->nr_clocks < 1)
198 return 0;
199
200 if (ddata->nr_clocks > SYSC_MAX_CLOCKS) {
201 dev_err(ddata->dev, "too many clocks for %pOF\n", np);
202
203 return -EINVAL;
204 }
205
206 if (nr_fck > 1 || nr_ick > 1) {
207 dev_err(ddata->dev, "max one fck and ick for %pOF\n", np);
208
209 return -EINVAL;
210 }
211
Kees Cook20749052018-06-18 10:45:49 -0700212 ddata->clocks = devm_kcalloc(ddata->dev,
213 ddata->nr_clocks, sizeof(*ddata->clocks),
Tony Lindgren09dfe582018-04-16 10:25:52 -0700214 GFP_KERNEL);
215 if (!ddata->clocks)
216 return -ENOMEM;
217
218 for (i = 0; i < ddata->nr_clocks; i++) {
219 error = sysc_get_one_clock(ddata, ddata->clock_roles[i]);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700220 if (error && error != -ENOENT)
221 return error;
222 }
223
224 return 0;
225}
226
227/**
Tony Lindgren50622362018-04-16 10:20:27 -0700228 * sysc_init_resets - reset module on init
229 * @ddata: device driver data
230 *
231 * A module can have both OCP softreset control and external rstctrl.
232 * If more complicated rstctrl resets are needed, please handle these
233 * directly from the child device driver and map only the module reset
234 * for the parent interconnect target module device.
235 *
236 * Automatic reset of the module on init can be skipped with the
237 * "ti,no-reset-on-init" device tree property.
238 */
239static int sysc_init_resets(struct sysc *ddata)
240{
241 int error;
242
243 ddata->rsts =
244 devm_reset_control_array_get_optional_exclusive(ddata->dev);
245 if (IS_ERR(ddata->rsts))
246 return PTR_ERR(ddata->rsts);
247
248 if (ddata->cfg.quirks & SYSC_QUIRK_NO_RESET_ON_INIT)
249 goto deassert;
250
251 error = reset_control_assert(ddata->rsts);
252 if (error)
253 return error;
254
255deassert:
256 error = reset_control_deassert(ddata->rsts);
257 if (error)
258 return error;
259
260 return 0;
261}
262
263/**
Tony Lindgren0eecc632017-10-10 14:23:43 -0700264 * sysc_parse_and_check_child_range - parses module IO region from ranges
265 * @ddata: device driver data
266 *
267 * In general we only need rev, syss, and sysc registers and not the whole
268 * module range. But we do want the offsets for these registers from the
269 * module base. This allows us to check them against the legacy hwmod
270 * platform data. Let's also check the ranges are configured properly.
271 */
272static int sysc_parse_and_check_child_range(struct sysc *ddata)
273{
274 struct device_node *np = ddata->dev->of_node;
275 const __be32 *ranges;
276 u32 nr_addr, nr_size;
277 int len, error;
278
279 ranges = of_get_property(np, "ranges", &len);
280 if (!ranges) {
281 dev_err(ddata->dev, "missing ranges for %pOF\n", np);
282
283 return -ENOENT;
284 }
285
286 len /= sizeof(*ranges);
287
288 if (len < 3) {
289 dev_err(ddata->dev, "incomplete ranges for %pOF\n", np);
290
291 return -EINVAL;
292 }
293
294 error = of_property_read_u32(np, "#address-cells", &nr_addr);
295 if (error)
296 return -ENOENT;
297
298 error = of_property_read_u32(np, "#size-cells", &nr_size);
299 if (error)
300 return -ENOENT;
301
302 if (nr_addr != 1 || nr_size != 1) {
303 dev_err(ddata->dev, "invalid ranges for %pOF\n", np);
304
305 return -EINVAL;
306 }
307
308 ranges++;
309 ddata->module_pa = of_translate_address(np, ranges++);
310 ddata->module_size = be32_to_cpup(ranges);
311
Tony Lindgren0eecc632017-10-10 14:23:43 -0700312 return 0;
313}
314
Tony Lindgren3bb37c82018-02-22 14:05:14 -0800315static struct device_node *stdout_path;
316
317static void sysc_init_stdout_path(struct sysc *ddata)
318{
319 struct device_node *np = NULL;
320 const char *uart;
321
322 if (IS_ERR(stdout_path))
323 return;
324
325 if (stdout_path)
326 return;
327
328 np = of_find_node_by_path("/chosen");
329 if (!np)
330 goto err;
331
332 uart = of_get_property(np, "stdout-path", NULL);
333 if (!uart)
334 goto err;
335
336 np = of_find_node_by_path(uart);
337 if (!np)
338 goto err;
339
340 stdout_path = np;
341
342 return;
343
344err:
345 stdout_path = ERR_PTR(-ENODEV);
346}
347
348static void sysc_check_quirk_stdout(struct sysc *ddata,
349 struct device_node *np)
350{
351 sysc_init_stdout_path(ddata);
352 if (np != stdout_path)
353 return;
354
355 ddata->cfg.quirks |= SYSC_QUIRK_NO_IDLE_ON_INIT |
356 SYSC_QUIRK_NO_RESET_ON_INIT;
357}
358
Tony Lindgren0eecc632017-10-10 14:23:43 -0700359/**
360 * sysc_check_one_child - check child configuration
361 * @ddata: device driver data
362 * @np: child device node
363 *
364 * Let's avoid messy situations where we have new interconnect target
365 * node but children have "ti,hwmods". These belong to the interconnect
366 * target node and are managed by this driver.
367 */
368static int sysc_check_one_child(struct sysc *ddata,
369 struct device_node *np)
370{
371 const char *name;
372
373 name = of_get_property(np, "ti,hwmods", NULL);
374 if (name)
375 dev_warn(ddata->dev, "really a child ti,hwmods property?");
376
Tony Lindgren3bb37c82018-02-22 14:05:14 -0800377 sysc_check_quirk_stdout(ddata, np);
378
Tony Lindgren0eecc632017-10-10 14:23:43 -0700379 return 0;
380}
381
382static int sysc_check_children(struct sysc *ddata)
383{
384 struct device_node *child;
385 int error;
386
387 for_each_child_of_node(ddata->dev->of_node, child) {
388 error = sysc_check_one_child(ddata, child);
389 if (error)
390 return error;
391 }
392
393 return 0;
394}
395
Tony Lindgrena7199e22017-12-15 09:41:14 -0800396/*
397 * So far only I2C uses 16-bit read access with clockactivity with revision
398 * in two registers with stride of 4. We can detect this based on the rev
399 * register size to configure things far enough to be able to properly read
400 * the revision register.
401 */
402static void sysc_check_quirk_16bit(struct sysc *ddata, struct resource *res)
403{
Tony Lindgrendd57ac12018-02-22 14:09:57 -0800404 if (resource_size(res) == 8)
Tony Lindgrena7199e22017-12-15 09:41:14 -0800405 ddata->cfg.quirks |= SYSC_QUIRK_16BIT | SYSC_QUIRK_USE_CLOCKACT;
Tony Lindgrena7199e22017-12-15 09:41:14 -0800406}
407
Tony Lindgren0eecc632017-10-10 14:23:43 -0700408/**
409 * sysc_parse_one - parses the interconnect target module registers
410 * @ddata: device driver data
411 * @reg: register to parse
412 */
413static int sysc_parse_one(struct sysc *ddata, enum sysc_registers reg)
414{
415 struct resource *res;
416 const char *name;
417
418 switch (reg) {
419 case SYSC_REVISION:
420 case SYSC_SYSCONFIG:
421 case SYSC_SYSSTATUS:
422 name = reg_names[reg];
423 break;
424 default:
425 return -EINVAL;
426 }
427
428 res = platform_get_resource_byname(to_platform_device(ddata->dev),
429 IORESOURCE_MEM, name);
430 if (!res) {
Tony Lindgren0eecc632017-10-10 14:23:43 -0700431 ddata->offsets[reg] = -ENODEV;
432
433 return 0;
434 }
435
436 ddata->offsets[reg] = res->start - ddata->module_pa;
Tony Lindgrena7199e22017-12-15 09:41:14 -0800437 if (reg == SYSC_REVISION)
438 sysc_check_quirk_16bit(ddata, res);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700439
440 return 0;
441}
442
443static int sysc_parse_registers(struct sysc *ddata)
444{
445 int i, error;
446
447 for (i = 0; i < SYSC_MAX_REGS; i++) {
448 error = sysc_parse_one(ddata, i);
449 if (error)
450 return error;
451 }
452
453 return 0;
454}
455
456/**
457 * sysc_check_registers - check for misconfigured register overlaps
458 * @ddata: device driver data
459 */
460static int sysc_check_registers(struct sysc *ddata)
461{
462 int i, j, nr_regs = 0, nr_matches = 0;
463
464 for (i = 0; i < SYSC_MAX_REGS; i++) {
465 if (ddata->offsets[i] < 0)
466 continue;
467
468 if (ddata->offsets[i] > (ddata->module_size - 4)) {
469 dev_err(ddata->dev, "register outside module range");
470
471 return -EINVAL;
472 }
473
474 for (j = 0; j < SYSC_MAX_REGS; j++) {
475 if (ddata->offsets[j] < 0)
476 continue;
477
478 if (ddata->offsets[i] == ddata->offsets[j])
479 nr_matches++;
480 }
481 nr_regs++;
482 }
483
484 if (nr_regs < 1) {
485 dev_err(ddata->dev, "missing registers\n");
486
487 return -EINVAL;
488 }
489
490 if (nr_matches > nr_regs) {
491 dev_err(ddata->dev, "overlapping registers: (%i/%i)",
492 nr_regs, nr_matches);
493
494 return -EINVAL;
495 }
496
497 return 0;
498}
499
500/**
501 * syc_ioremap - ioremap register space for the interconnect target module
502 * @ddata: deviec driver data
503 *
504 * Note that the interconnect target module registers can be anywhere
505 * within the first child device address space. For example, SGX has
506 * them at offset 0x1fc00 in the 32MB module address space. We just
507 * what we need around the interconnect target module registers.
508 */
509static int sysc_ioremap(struct sysc *ddata)
510{
511 u32 size = 0;
512
513 if (ddata->offsets[SYSC_SYSSTATUS] >= 0)
514 size = ddata->offsets[SYSC_SYSSTATUS];
515 else if (ddata->offsets[SYSC_SYSCONFIG] >= 0)
516 size = ddata->offsets[SYSC_SYSCONFIG];
517 else if (ddata->offsets[SYSC_REVISION] >= 0)
518 size = ddata->offsets[SYSC_REVISION];
519 else
520 return -EINVAL;
521
522 size &= 0xfff00;
523 size += SZ_256;
524
525 ddata->module_va = devm_ioremap(ddata->dev,
526 ddata->module_pa,
527 size);
528 if (!ddata->module_va)
529 return -EIO;
530
531 return 0;
532}
533
534/**
535 * sysc_map_and_check_registers - ioremap and check device registers
536 * @ddata: device driver data
537 */
538static int sysc_map_and_check_registers(struct sysc *ddata)
539{
540 int error;
541
542 error = sysc_parse_and_check_child_range(ddata);
543 if (error)
544 return error;
545
546 error = sysc_check_children(ddata);
547 if (error)
548 return error;
549
550 error = sysc_parse_registers(ddata);
551 if (error)
552 return error;
553
554 error = sysc_ioremap(ddata);
555 if (error)
556 return error;
557
558 error = sysc_check_registers(ddata);
559 if (error)
560 return error;
561
562 return 0;
563}
564
565/**
566 * sysc_show_rev - read and show interconnect target module revision
567 * @bufp: buffer to print the information to
568 * @ddata: device driver data
569 */
570static int sysc_show_rev(char *bufp, struct sysc *ddata)
571{
Tony Lindgren566a9b052017-12-15 09:41:19 -0800572 int len;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700573
574 if (ddata->offsets[SYSC_REVISION] < 0)
575 return sprintf(bufp, ":NA");
576
Tony Lindgren566a9b052017-12-15 09:41:19 -0800577 len = sprintf(bufp, ":%08x", ddata->revision);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700578
579 return len;
580}
581
582static int sysc_show_reg(struct sysc *ddata,
583 char *bufp, enum sysc_registers reg)
584{
585 if (ddata->offsets[reg] < 0)
586 return sprintf(bufp, ":NA");
587
588 return sprintf(bufp, ":%x", ddata->offsets[reg]);
589}
590
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800591static int sysc_show_name(char *bufp, struct sysc *ddata)
592{
593 if (!ddata->name)
594 return 0;
595
596 return sprintf(bufp, ":%s", ddata->name);
597}
598
Tony Lindgren0eecc632017-10-10 14:23:43 -0700599/**
600 * sysc_show_registers - show information about interconnect target module
601 * @ddata: device driver data
602 */
603static void sysc_show_registers(struct sysc *ddata)
604{
605 char buf[128];
606 char *bufp = buf;
607 int i;
608
609 for (i = 0; i < SYSC_MAX_REGS; i++)
610 bufp += sysc_show_reg(ddata, bufp, i);
611
612 bufp += sysc_show_rev(bufp, ddata);
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800613 bufp += sysc_show_name(bufp, ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700614
615 dev_dbg(ddata->dev, "%llx:%x%s\n",
616 ddata->module_pa, ddata->module_size,
617 buf);
618}
619
Arnd Bergmanna4a5d492017-10-13 11:25:32 +0200620static int __maybe_unused sysc_runtime_suspend(struct device *dev)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700621{
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800622 struct ti_sysc_platform_data *pdata;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700623 struct sysc *ddata;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800624 int error = 0, i;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700625
626 ddata = dev_get_drvdata(dev);
627
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800628 if (!ddata->enabled)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700629 return 0;
630
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800631 if (ddata->legacy_mode) {
632 pdata = dev_get_platdata(ddata->dev);
633 if (!pdata)
634 return 0;
635
636 if (!pdata->idle_module)
637 return -ENODEV;
638
639 error = pdata->idle_module(dev, &ddata->cookie);
640 if (error)
641 dev_err(dev, "%s: could not idle: %i\n",
642 __func__, error);
643
644 goto idled;
645 }
646
Tony Lindgren09dfe582018-04-16 10:25:52 -0700647 for (i = 0; i < ddata->nr_clocks; i++) {
Tony Lindgren0eecc632017-10-10 14:23:43 -0700648 if (IS_ERR_OR_NULL(ddata->clocks[i]))
649 continue;
Tony Lindgren09dfe582018-04-16 10:25:52 -0700650
651 if (i >= SYSC_OPTFCK0 && !sysc_opt_clks_needed(ddata))
652 break;
653
Tony Lindgren0eecc632017-10-10 14:23:43 -0700654 clk_disable(ddata->clocks[i]);
655 }
656
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800657idled:
658 ddata->enabled = false;
659
660 return error;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700661}
662
Arnd Bergmanna4a5d492017-10-13 11:25:32 +0200663static int __maybe_unused sysc_runtime_resume(struct device *dev)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700664{
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800665 struct ti_sysc_platform_data *pdata;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700666 struct sysc *ddata;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800667 int error = 0, i;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700668
669 ddata = dev_get_drvdata(dev);
670
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800671 if (ddata->enabled)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700672 return 0;
673
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800674 if (ddata->legacy_mode) {
675 pdata = dev_get_platdata(ddata->dev);
676 if (!pdata)
677 return 0;
678
679 if (!pdata->enable_module)
680 return -ENODEV;
681
682 error = pdata->enable_module(dev, &ddata->cookie);
683 if (error)
684 dev_err(dev, "%s: could not enable: %i\n",
685 __func__, error);
686
687 goto awake;
688 }
689
Tony Lindgren09dfe582018-04-16 10:25:52 -0700690 for (i = 0; i < ddata->nr_clocks; i++) {
Tony Lindgren0eecc632017-10-10 14:23:43 -0700691 if (IS_ERR_OR_NULL(ddata->clocks[i]))
692 continue;
Tony Lindgren09dfe582018-04-16 10:25:52 -0700693
694 if (i >= SYSC_OPTFCK0 && !sysc_opt_clks_needed(ddata))
695 break;
696
Tony Lindgren0eecc632017-10-10 14:23:43 -0700697 error = clk_enable(ddata->clocks[i]);
698 if (error)
699 return error;
700 }
701
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800702awake:
703 ddata->enabled = true;
704
705 return error;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700706}
707
Tony Lindgren62020f22018-02-22 13:59:44 -0800708#ifdef CONFIG_PM_SLEEP
709static int sysc_suspend(struct device *dev)
710{
711 struct sysc *ddata;
Tony Lindgrenef55f822018-04-16 10:27:15 -0700712 int error;
Tony Lindgren62020f22018-02-22 13:59:44 -0800713
714 ddata = dev_get_drvdata(dev);
715
Tony Lindgren40d9f912018-09-24 12:16:54 -0700716 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
Tony Lindgrene7420c22018-04-16 10:26:46 -0700717 return 0;
718
Tony Lindgren40d9f912018-09-24 12:16:54 -0700719 if (!ddata->enabled || ddata->noirq_suspend)
Tony Lindgren62020f22018-02-22 13:59:44 -0800720 return 0;
721
Tony Lindgrenef55f822018-04-16 10:27:15 -0700722 dev_dbg(ddata->dev, "%s %s\n", __func__,
723 ddata->name ? ddata->name : "");
724
725 error = pm_runtime_put_sync_suspend(dev);
Tony Lindgren40d9f912018-09-24 12:16:54 -0700726 if (error == -EBUSY) {
727 dev_dbg(ddata->dev, "%s busy, tagging for noirq suspend %s\n",
728 __func__, ddata->name ? ddata->name : "");
729
730 ddata->noirq_suspend = true;
731
732 return 0;
733 } else if (error < 0) {
734 dev_warn(ddata->dev, "%s cannot suspend %i %s\n",
Tony Lindgrenef55f822018-04-16 10:27:15 -0700735 __func__, error,
736 ddata->name ? ddata->name : "");
737
738 return 0;
739 }
740
Tony Lindgren62020f22018-02-22 13:59:44 -0800741 ddata->needs_resume = true;
742
Tony Lindgrenef55f822018-04-16 10:27:15 -0700743 return 0;
Tony Lindgren62020f22018-02-22 13:59:44 -0800744}
745
746static int sysc_resume(struct device *dev)
747{
748 struct sysc *ddata;
Tony Lindgrenef55f822018-04-16 10:27:15 -0700749 int error;
Tony Lindgren62020f22018-02-22 13:59:44 -0800750
751 ddata = dev_get_drvdata(dev);
Tony Lindgrene7420c22018-04-16 10:26:46 -0700752
Tony Lindgren40d9f912018-09-24 12:16:54 -0700753 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
Tony Lindgrene7420c22018-04-16 10:26:46 -0700754 return 0;
755
Tony Lindgren40d9f912018-09-24 12:16:54 -0700756 if (!ddata->needs_resume || ddata->noirq_suspend)
757 return 0;
758
759 dev_dbg(ddata->dev, "%s %s\n", __func__,
760 ddata->name ? ddata->name : "");
761
762 error = pm_runtime_get_sync(dev);
763 if (error < 0) {
764 dev_err(ddata->dev, "%s error %i %s\n",
765 __func__, error,
Tony Lindgrene7420c22018-04-16 10:26:46 -0700766 ddata->name ? ddata->name : "");
767
Tony Lindgren40d9f912018-09-24 12:16:54 -0700768 return error;
Tony Lindgrene7420c22018-04-16 10:26:46 -0700769 }
770
Tony Lindgren40d9f912018-09-24 12:16:54 -0700771 ddata->needs_resume = false;
772
Tony Lindgrene7420c22018-04-16 10:26:46 -0700773 return 0;
774}
775
776static int sysc_noirq_suspend(struct device *dev)
777{
778 struct sysc *ddata;
Tony Lindgren40d9f912018-09-24 12:16:54 -0700779 int error;
Tony Lindgrene7420c22018-04-16 10:26:46 -0700780
781 ddata = dev_get_drvdata(dev);
782
783 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
784 return 0;
785
Tony Lindgren40d9f912018-09-24 12:16:54 -0700786 if (!ddata->enabled || !ddata->noirq_suspend)
Tony Lindgrene7420c22018-04-16 10:26:46 -0700787 return 0;
788
789 dev_dbg(ddata->dev, "%s %s\n", __func__,
790 ddata->name ? ddata->name : "");
791
Tony Lindgren40d9f912018-09-24 12:16:54 -0700792 error = sysc_runtime_suspend(dev);
793 if (error) {
794 dev_warn(ddata->dev, "%s busy %i %s\n",
795 __func__, error, ddata->name ? ddata->name : "");
796
797 return 0;
798 }
799
Tony Lindgrene7420c22018-04-16 10:26:46 -0700800 ddata->needs_resume = true;
801
Tony Lindgren40d9f912018-09-24 12:16:54 -0700802 return 0;
Tony Lindgrene7420c22018-04-16 10:26:46 -0700803}
804
805static int sysc_noirq_resume(struct device *dev)
806{
807 struct sysc *ddata;
Tony Lindgren40d9f912018-09-24 12:16:54 -0700808 int error;
Tony Lindgrene7420c22018-04-16 10:26:46 -0700809
810 ddata = dev_get_drvdata(dev);
811
812 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
813 return 0;
814
Tony Lindgren40d9f912018-09-24 12:16:54 -0700815 if (!ddata->needs_resume || !ddata->noirq_suspend)
Tony Lindgrene7420c22018-04-16 10:26:46 -0700816 return 0;
817
Tony Lindgren40d9f912018-09-24 12:16:54 -0700818 dev_dbg(ddata->dev, "%s %s\n", __func__,
819 ddata->name ? ddata->name : "");
Tony Lindgrenef55f822018-04-16 10:27:15 -0700820
Tony Lindgren40d9f912018-09-24 12:16:54 -0700821 error = sysc_runtime_resume(dev);
822 if (error) {
823 dev_warn(ddata->dev, "%s cannot resume %i %s\n",
824 __func__, error,
825 ddata->name ? ddata->name : "");
Tony Lindgren62020f22018-02-22 13:59:44 -0800826
Tony Lindgren40d9f912018-09-24 12:16:54 -0700827 return error;
Tony Lindgren62020f22018-02-22 13:59:44 -0800828 }
829
Tony Lindgren40d9f912018-09-24 12:16:54 -0700830 /* Maybe also reconsider clearing noirq_suspend at some point */
831 ddata->needs_resume = false;
832
Tony Lindgren62020f22018-02-22 13:59:44 -0800833 return 0;
834}
835#endif
836
Tony Lindgren0eecc632017-10-10 14:23:43 -0700837static const struct dev_pm_ops sysc_pm_ops = {
Tony Lindgren62020f22018-02-22 13:59:44 -0800838 SET_SYSTEM_SLEEP_PM_OPS(sysc_suspend, sysc_resume)
Tony Lindgrene7420c22018-04-16 10:26:46 -0700839 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(sysc_noirq_suspend, sysc_noirq_resume)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700840 SET_RUNTIME_PM_OPS(sysc_runtime_suspend,
841 sysc_runtime_resume,
842 NULL)
843};
844
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800845/* Module revision register based quirks */
846struct sysc_revision_quirk {
847 const char *name;
848 u32 base;
849 int rev_offset;
850 int sysc_offset;
851 int syss_offset;
852 u32 revision;
853 u32 revision_mask;
854 u32 quirks;
855};
856
857#define SYSC_QUIRK(optname, optbase, optrev, optsysc, optsyss, \
858 optrev_val, optrevmask, optquirkmask) \
859 { \
860 .name = (optname), \
861 .base = (optbase), \
862 .rev_offset = (optrev), \
863 .sysc_offset = (optsysc), \
864 .syss_offset = (optsyss), \
865 .revision = (optrev_val), \
866 .revision_mask = (optrevmask), \
867 .quirks = (optquirkmask), \
868 }
869
870static const struct sysc_revision_quirk sysc_revision_quirks[] = {
871 /* These drivers need to be fixed to not use pm_runtime_irq_safe() */
Tony Lindgrenb82beef2018-09-24 12:16:59 -0700872 SYSC_QUIRK("gpio", 0, 0, 0x10, 0x114, 0x50600801, 0xffff0fff,
Tony Lindgren09dfe582018-04-16 10:25:52 -0700873 SYSC_QUIRK_LEGACY_IDLE | SYSC_QUIRK_OPT_CLKS_IN_RESET),
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800874 SYSC_QUIRK("mmu", 0, 0, 0x10, 0x14, 0x00000020, 0xffffffff,
875 SYSC_QUIRK_LEGACY_IDLE),
876 SYSC_QUIRK("mmu", 0, 0, 0x10, 0x14, 0x00000030, 0xffffffff,
877 SYSC_QUIRK_LEGACY_IDLE),
878 SYSC_QUIRK("sham", 0, 0x100, 0x110, 0x114, 0x40000c03, 0xffffffff,
879 SYSC_QUIRK_LEGACY_IDLE),
880 SYSC_QUIRK("smartreflex", 0, -1, 0x24, -1, 0x00000000, 0xffffffff,
881 SYSC_QUIRK_LEGACY_IDLE),
882 SYSC_QUIRK("smartreflex", 0, -1, 0x38, -1, 0x00000000, 0xffffffff,
883 SYSC_QUIRK_LEGACY_IDLE),
884 SYSC_QUIRK("timer", 0, 0, 0x10, 0x14, 0x00000015, 0xffffffff,
885 SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgren8cde5d52018-04-16 10:25:15 -0700886 /* Some timers on omap4 and later */
887 SYSC_QUIRK("timer", 0, 0, 0x10, -1, 0x4fff1301, 0xffffffff,
888 SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800889 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x00000052, 0xffffffff,
890 SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgrend708bb12018-04-16 10:25:32 -0700891 /* Uarts on omap4 and later */
Tony Lindgrenb82beef2018-09-24 12:16:59 -0700892 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x50411e03, 0xffff00ff,
893 SYSC_QUIRK_LEGACY_IDLE),
894 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x47422e03, 0xffffffff,
Tony Lindgrend708bb12018-04-16 10:25:32 -0700895 SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgren7e27e5d2018-04-20 14:54:16 -0700896
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700897#ifdef DEBUG
Tony Lindgren1ba30692018-09-24 12:17:05 -0700898 SYSC_QUIRK("adc", 0, 0, 0x10, -1, 0x47300001, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700899 SYSC_QUIRK("aess", 0, 0, 0x10, -1, 0x40000000, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -0700900 SYSC_QUIRK("control", 0, 0, 0x10, -1, 0x40000900, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700901 SYSC_QUIRK("cpgmac", 0, 0x1200, 0x1208, 0x1204, 0x4edb1902,
902 0xffffffff, 0),
903 SYSC_QUIRK("dcan", 0, 0, -1, -1, 0x00001401, 0xffffffff, 0),
904 SYSC_QUIRK("dwc3", 0, 0, 0x10, -1, 0x500a0200, 0xffffffff, 0),
905 SYSC_QUIRK("epwmss", 0, 0, 0x4, -1, 0x47400001, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700906 SYSC_QUIRK("gpu", 0, 0x1fc00, 0x1fc10, -1, 0, 0, 0),
907 SYSC_QUIRK("hdq1w", 0, 0, 0x14, 0x18, 0x00000006, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700908 SYSC_QUIRK("hdq1w", 0, 0, 0x14, 0x18, 0x0000000a, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700909 SYSC_QUIRK("hsi", 0, 0, 0x10, 0x14, 0x50043101, 0xffffffff, 0),
910 SYSC_QUIRK("iss", 0, 0, 0x10, -1, 0x40000101, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700911 SYSC_QUIRK("i2c", 0, 0, 0x10, 0x90, 0x5040000a, 0xfffffff0, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700912 SYSC_QUIRK("mcasp", 0, 0, 0x4, -1, 0x44306302, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700913 SYSC_QUIRK("mcasp", 0, 0, 0x4, -1, 0x44307b02, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700914 SYSC_QUIRK("mcbsp", 0, -1, 0x8c, -1, 0, 0, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -0700915 SYSC_QUIRK("mcspi", 0, 0, 0x10, -1, 0x40300a0b, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700916 SYSC_QUIRK("mcspi", 0, 0, 0x110, 0x114, 0x40300a0b, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700917 SYSC_QUIRK("mailbox", 0, 0, 0x10, -1, 0x00000400, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700918 SYSC_QUIRK("m3", 0, 0, -1, -1, 0x5f580105, 0x0fff0f00, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -0700919 SYSC_QUIRK("ocp2scp", 0, 0, 0x10, 0x14, 0x50060005, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700920 SYSC_QUIRK("ocp2scp", 0, 0, -1, -1, 0x50060007, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -0700921 SYSC_QUIRK("padconf", 0, 0, 0x10, -1, 0x4fff0800, 0xffffffff, 0),
922 SYSC_QUIRK("prcm", 0, 0, -1, -1, 0x40000100, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700923 SYSC_QUIRK("prcm", 0, 0, -1, -1, 0x40000400, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -0700924 SYSC_QUIRK("scm", 0, 0, 0x10, -1, 0x40000900, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700925 SYSC_QUIRK("scm", 0, 0, -1, -1, 0x4f000100, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -0700926 SYSC_QUIRK("scrm", 0, 0, -1, -1, 0x00000010, 0xffffffff, 0),
927 SYSC_QUIRK("sdio", 0, 0, 0x10, -1, 0x40202301, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700928 SYSC_QUIRK("sdio", 0, 0x2fc, 0x110, 0x114, 0x31010000, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -0700929 SYSC_QUIRK("sdma", 0, 0, 0x2c, 0x28, 0x00010900, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700930 SYSC_QUIRK("slimbus", 0, 0, 0x10, -1, 0x40000902, 0xffffffff, 0),
931 SYSC_QUIRK("slimbus", 0, 0, 0x10, -1, 0x40002903, 0xffffffff, 0),
932 SYSC_QUIRK("spinlock", 0, 0, 0x10, -1, 0x50020000, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700933 SYSC_QUIRK("rng", 0, 0x1fe0, 0x1fe4, -1, 0x00000020, 0xffffffff, 0),
934 SYSC_QUIRK("rtc", 0, 0x74, 0x78, -1, 0x4eb01908, 0xffffffff, 0),
935 SYSC_QUIRK("timer32k", 0, 0, 0x4, -1, 0x00000060, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700936 SYSC_QUIRK("usbhstll", 0, 0, 0x10, 0x14, 0x00000004, 0xffffffff, 0),
937 SYSC_QUIRK("usb_host_hs", 0, 0, 0x10, 0x14, 0x50700100, 0xffffffff, 0),
938 SYSC_QUIRK("usb_otg_hs", 0, 0x400, 0x404, 0x408, 0x00000050,
939 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700940 SYSC_QUIRK("wdt", 0, 0, 0x10, 0x14, 0x502a0500, 0xfffff0f0, 0),
941 SYSC_QUIRK("vfpe", 0, 0, 0x104, -1, 0x4d001200, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700942#endif
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800943};
944
945static void sysc_init_revision_quirks(struct sysc *ddata)
946{
947 const struct sysc_revision_quirk *q;
948 int i;
949
950 for (i = 0; i < ARRAY_SIZE(sysc_revision_quirks); i++) {
951 q = &sysc_revision_quirks[i];
952
953 if (q->base && q->base != ddata->module_pa)
954 continue;
955
956 if (q->rev_offset >= 0 &&
957 q->rev_offset != ddata->offsets[SYSC_REVISION])
958 continue;
959
960 if (q->sysc_offset >= 0 &&
961 q->sysc_offset != ddata->offsets[SYSC_SYSCONFIG])
962 continue;
963
964 if (q->syss_offset >= 0 &&
965 q->syss_offset != ddata->offsets[SYSC_SYSSTATUS])
966 continue;
967
968 if (q->revision == ddata->revision ||
969 (q->revision & q->revision_mask) ==
970 (ddata->revision & q->revision_mask)) {
971 ddata->name = q->name;
972 ddata->cfg.quirks |= q->quirks;
973 }
974 }
975}
976
Faiz Abbas596e7952018-07-09 22:18:39 +0530977static int sysc_reset(struct sysc *ddata)
978{
979 int offset = ddata->offsets[SYSC_SYSCONFIG];
980 int val;
981
982 if (ddata->legacy_mode || offset < 0 ||
983 ddata->cfg.quirks & SYSC_QUIRK_NO_RESET_ON_INIT)
984 return 0;
985
986 /*
987 * Currently only support reset status in sysstatus.
988 * Warn and return error in all other cases
989 */
990 if (!ddata->cfg.syss_mask) {
991 dev_err(ddata->dev, "No ti,syss-mask. Reset failed\n");
992 return -EINVAL;
993 }
994
995 val = sysc_read(ddata, offset);
996 val |= (0x1 << ddata->cap->regbits->srst_shift);
997 sysc_write(ddata, offset, val);
998
999 /* Poll on reset status */
1000 offset = ddata->offsets[SYSC_SYSSTATUS];
1001
1002 return readl_poll_timeout(ddata->module_va + offset, val,
1003 (val & ddata->cfg.syss_mask) == 0x0,
1004 100, MAX_MODULE_SOFTRESET_WAIT);
1005}
1006
Tony Lindgren566a9b052017-12-15 09:41:19 -08001007/* At this point the module is configured enough to read the revision */
1008static int sysc_init_module(struct sysc *ddata)
1009{
1010 int error;
1011
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001012 if (ddata->cfg.quirks & SYSC_QUIRK_NO_IDLE_ON_INIT) {
1013 ddata->revision = sysc_read_revision(ddata);
1014 goto rev_quirks;
1015 }
1016
Tony Lindgren566a9b052017-12-15 09:41:19 -08001017 error = pm_runtime_get_sync(ddata->dev);
1018 if (error < 0) {
1019 pm_runtime_put_noidle(ddata->dev);
1020
1021 return 0;
1022 }
Tony Lindgren50622362018-04-16 10:20:27 -07001023
Faiz Abbas596e7952018-07-09 22:18:39 +05301024 error = sysc_reset(ddata);
1025 if (error) {
1026 dev_err(ddata->dev, "Reset failed with %d\n", error);
1027 pm_runtime_put_sync(ddata->dev);
1028
1029 return error;
1030 }
1031
Tony Lindgren566a9b052017-12-15 09:41:19 -08001032 ddata->revision = sysc_read_revision(ddata);
1033 pm_runtime_put_sync(ddata->dev);
1034
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001035rev_quirks:
1036 sysc_init_revision_quirks(ddata);
1037
Tony Lindgren566a9b052017-12-15 09:41:19 -08001038 return 0;
1039}
1040
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08001041static int sysc_init_sysc_mask(struct sysc *ddata)
1042{
1043 struct device_node *np = ddata->dev->of_node;
1044 int error;
1045 u32 val;
1046
1047 error = of_property_read_u32(np, "ti,sysc-mask", &val);
1048 if (error)
1049 return 0;
1050
1051 if (val)
1052 ddata->cfg.sysc_val = val & ddata->cap->sysc_mask;
1053 else
1054 ddata->cfg.sysc_val = ddata->cap->sysc_mask;
1055
1056 return 0;
1057}
1058
1059static int sysc_init_idlemode(struct sysc *ddata, u8 *idlemodes,
1060 const char *name)
1061{
1062 struct device_node *np = ddata->dev->of_node;
1063 struct property *prop;
1064 const __be32 *p;
1065 u32 val;
1066
1067 of_property_for_each_u32(np, name, prop, p, val) {
1068 if (val >= SYSC_NR_IDLEMODES) {
1069 dev_err(ddata->dev, "invalid idlemode: %i\n", val);
1070 return -EINVAL;
1071 }
1072 *idlemodes |= (1 << val);
1073 }
1074
1075 return 0;
1076}
1077
1078static int sysc_init_idlemodes(struct sysc *ddata)
1079{
1080 int error;
1081
1082 error = sysc_init_idlemode(ddata, &ddata->cfg.midlemodes,
1083 "ti,sysc-midle");
1084 if (error)
1085 return error;
1086
1087 error = sysc_init_idlemode(ddata, &ddata->cfg.sidlemodes,
1088 "ti,sysc-sidle");
1089 if (error)
1090 return error;
1091
1092 return 0;
1093}
1094
1095/*
1096 * Only some devices on omap4 and later have SYSCONFIG reset done
1097 * bit. We can detect this if there is no SYSSTATUS at all, or the
1098 * SYSTATUS bit 0 is not used. Note that some SYSSTATUS registers
1099 * have multiple bits for the child devices like OHCI and EHCI.
1100 * Depends on SYSC being parsed first.
1101 */
1102static int sysc_init_syss_mask(struct sysc *ddata)
1103{
1104 struct device_node *np = ddata->dev->of_node;
1105 int error;
1106 u32 val;
1107
1108 error = of_property_read_u32(np, "ti,syss-mask", &val);
1109 if (error) {
1110 if ((ddata->cap->type == TI_SYSC_OMAP4 ||
1111 ddata->cap->type == TI_SYSC_OMAP4_TIMER) &&
1112 (ddata->cfg.sysc_val & SYSC_OMAP4_SOFTRESET))
1113 ddata->cfg.quirks |= SYSC_QUIRK_RESET_STATUS;
1114
1115 return 0;
1116 }
1117
1118 if (!(val & 1) && (ddata->cfg.sysc_val & SYSC_OMAP4_SOFTRESET))
1119 ddata->cfg.quirks |= SYSC_QUIRK_RESET_STATUS;
1120
1121 ddata->cfg.syss_mask = val;
1122
1123 return 0;
1124}
1125
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001126/*
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001127 * Many child device drivers need to have fck and opt clocks available
1128 * to get the clock rate for device internal configuration etc.
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001129 */
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001130static int sysc_child_add_named_clock(struct sysc *ddata,
1131 struct device *child,
1132 const char *name)
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001133{
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001134 struct clk *clk;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001135 struct clk_lookup *l;
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001136 int error = 0;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001137
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001138 if (!name)
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001139 return 0;
1140
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001141 clk = clk_get(child, name);
1142 if (!IS_ERR(clk)) {
1143 clk_put(clk);
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001144
1145 return -EEXIST;
1146 }
1147
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001148 clk = clk_get(ddata->dev, name);
1149 if (IS_ERR(clk))
1150 return -ENODEV;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001151
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001152 l = clkdev_create(clk, name, dev_name(child));
1153 if (!l)
1154 error = -ENOMEM;
1155
1156 clk_put(clk);
1157
1158 return error;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001159}
1160
Tony Lindgren09dfe582018-04-16 10:25:52 -07001161static int sysc_child_add_clocks(struct sysc *ddata,
1162 struct device *child)
1163{
1164 int i, error;
1165
1166 for (i = 0; i < ddata->nr_clocks; i++) {
1167 error = sysc_child_add_named_clock(ddata,
1168 child,
1169 ddata->clock_roles[i]);
1170 if (error && error != -EEXIST) {
1171 dev_err(ddata->dev, "could not add child clock %s: %i\n",
1172 ddata->clock_roles[i], error);
1173
1174 return error;
1175 }
1176 }
1177
1178 return 0;
1179}
1180
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001181static struct device_type sysc_device_type = {
1182};
1183
1184static struct sysc *sysc_child_to_parent(struct device *dev)
1185{
1186 struct device *parent = dev->parent;
1187
1188 if (!parent || parent->type != &sysc_device_type)
1189 return NULL;
1190
1191 return dev_get_drvdata(parent);
1192}
1193
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001194static int __maybe_unused sysc_child_runtime_suspend(struct device *dev)
1195{
1196 struct sysc *ddata;
1197 int error;
1198
1199 ddata = sysc_child_to_parent(dev);
1200
1201 error = pm_generic_runtime_suspend(dev);
1202 if (error)
1203 return error;
1204
1205 if (!ddata->enabled)
1206 return 0;
1207
1208 return sysc_runtime_suspend(ddata->dev);
1209}
1210
1211static int __maybe_unused sysc_child_runtime_resume(struct device *dev)
1212{
1213 struct sysc *ddata;
1214 int error;
1215
1216 ddata = sysc_child_to_parent(dev);
1217
1218 if (!ddata->enabled) {
1219 error = sysc_runtime_resume(ddata->dev);
1220 if (error < 0)
1221 dev_err(ddata->dev,
1222 "%s error: %i\n", __func__, error);
1223 }
1224
1225 return pm_generic_runtime_resume(dev);
1226}
1227
1228#ifdef CONFIG_PM_SLEEP
1229static int sysc_child_suspend_noirq(struct device *dev)
1230{
1231 struct sysc *ddata;
1232 int error;
1233
1234 ddata = sysc_child_to_parent(dev);
1235
Tony Lindgrenef55f822018-04-16 10:27:15 -07001236 dev_dbg(ddata->dev, "%s %s\n", __func__,
1237 ddata->name ? ddata->name : "");
1238
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001239 error = pm_generic_suspend_noirq(dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001240 if (error) {
1241 dev_err(dev, "%s error at %i: %i\n",
1242 __func__, __LINE__, error);
1243
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001244 return error;
Tony Lindgrenef55f822018-04-16 10:27:15 -07001245 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001246
1247 if (!pm_runtime_status_suspended(dev)) {
1248 error = pm_generic_runtime_suspend(dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001249 if (error) {
1250 dev_err(dev, "%s error at %i: %i\n",
1251 __func__, __LINE__, error);
1252
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001253 return error;
Tony Lindgrenef55f822018-04-16 10:27:15 -07001254 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001255
1256 error = sysc_runtime_suspend(ddata->dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001257 if (error) {
1258 dev_err(dev, "%s error at %i: %i\n",
1259 __func__, __LINE__, error);
1260
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001261 return error;
Tony Lindgrenef55f822018-04-16 10:27:15 -07001262 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001263
1264 ddata->child_needs_resume = true;
1265 }
1266
1267 return 0;
1268}
1269
1270static int sysc_child_resume_noirq(struct device *dev)
1271{
1272 struct sysc *ddata;
1273 int error;
1274
1275 ddata = sysc_child_to_parent(dev);
1276
Tony Lindgrenef55f822018-04-16 10:27:15 -07001277 dev_dbg(ddata->dev, "%s %s\n", __func__,
1278 ddata->name ? ddata->name : "");
1279
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001280 if (ddata->child_needs_resume) {
1281 ddata->child_needs_resume = false;
1282
1283 error = sysc_runtime_resume(ddata->dev);
1284 if (error)
1285 dev_err(ddata->dev,
1286 "%s runtime resume error: %i\n",
1287 __func__, error);
1288
1289 error = pm_generic_runtime_resume(dev);
1290 if (error)
1291 dev_err(ddata->dev,
1292 "%s generic runtime resume: %i\n",
1293 __func__, error);
1294 }
1295
1296 return pm_generic_resume_noirq(dev);
1297}
1298#endif
1299
1300struct dev_pm_domain sysc_child_pm_domain = {
1301 .ops = {
1302 SET_RUNTIME_PM_OPS(sysc_child_runtime_suspend,
1303 sysc_child_runtime_resume,
1304 NULL)
1305 USE_PLATFORM_PM_SLEEP_OPS
1306 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(sysc_child_suspend_noirq,
1307 sysc_child_resume_noirq)
1308 }
1309};
1310
1311/**
1312 * sysc_legacy_idle_quirk - handle children in omap_device compatible way
1313 * @ddata: device driver data
1314 * @child: child device driver
1315 *
1316 * Allow idle for child devices as done with _od_runtime_suspend().
1317 * Otherwise many child devices will not idle because of the permanent
1318 * parent usecount set in pm_runtime_irq_safe().
1319 *
1320 * Note that the long term solution is to just modify the child device
1321 * drivers to not set pm_runtime_irq_safe() and then this can be just
1322 * dropped.
1323 */
1324static void sysc_legacy_idle_quirk(struct sysc *ddata, struct device *child)
1325{
1326 if (!ddata->legacy_mode)
1327 return;
1328
1329 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
1330 dev_pm_domain_set(child, &sysc_child_pm_domain);
1331}
1332
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001333static int sysc_notifier_call(struct notifier_block *nb,
1334 unsigned long event, void *device)
1335{
1336 struct device *dev = device;
1337 struct sysc *ddata;
1338 int error;
1339
1340 ddata = sysc_child_to_parent(dev);
1341 if (!ddata)
1342 return NOTIFY_DONE;
1343
1344 switch (event) {
1345 case BUS_NOTIFY_ADD_DEVICE:
Tony Lindgren09dfe582018-04-16 10:25:52 -07001346 error = sysc_child_add_clocks(ddata, dev);
1347 if (error)
1348 return error;
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001349 sysc_legacy_idle_quirk(ddata, dev);
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001350 break;
1351 default:
1352 break;
1353 }
1354
1355 return NOTIFY_DONE;
1356}
1357
1358static struct notifier_block sysc_nb = {
1359 .notifier_call = sysc_notifier_call,
1360};
1361
Tony Lindgren566a9b052017-12-15 09:41:19 -08001362/* Device tree configured quirks */
1363struct sysc_dts_quirk {
1364 const char *name;
1365 u32 mask;
1366};
1367
1368static const struct sysc_dts_quirk sysc_dts_quirks[] = {
1369 { .name = "ti,no-idle-on-init",
1370 .mask = SYSC_QUIRK_NO_IDLE_ON_INIT, },
1371 { .name = "ti,no-reset-on-init",
1372 .mask = SYSC_QUIRK_NO_RESET_ON_INIT, },
1373};
1374
1375static int sysc_init_dts_quirks(struct sysc *ddata)
1376{
1377 struct device_node *np = ddata->dev->of_node;
1378 const struct property *prop;
1379 int i, len, error;
1380 u32 val;
1381
1382 ddata->legacy_mode = of_get_property(np, "ti,hwmods", NULL);
1383
1384 for (i = 0; i < ARRAY_SIZE(sysc_dts_quirks); i++) {
1385 prop = of_get_property(np, sysc_dts_quirks[i].name, &len);
1386 if (!prop)
Tony Lindgrend39b6ea2018-02-15 09:18:55 -08001387 continue;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001388
1389 ddata->cfg.quirks |= sysc_dts_quirks[i].mask;
1390 }
1391
1392 error = of_property_read_u32(np, "ti,sysc-delay-us", &val);
1393 if (!error) {
1394 if (val > 255) {
1395 dev_warn(ddata->dev, "bad ti,sysc-delay-us: %i\n",
1396 val);
1397 }
1398
1399 ddata->cfg.srst_udelay = (u8)val;
1400 }
1401
1402 return 0;
1403}
1404
Tony Lindgren0eecc632017-10-10 14:23:43 -07001405static void sysc_unprepare(struct sysc *ddata)
1406{
1407 int i;
1408
1409 for (i = 0; i < SYSC_MAX_CLOCKS; i++) {
1410 if (!IS_ERR_OR_NULL(ddata->clocks[i]))
1411 clk_unprepare(ddata->clocks[i]);
1412 }
1413}
1414
Tony Lindgren70a65242017-12-15 09:41:09 -08001415/*
1416 * Common sysc register bits found on omap2, also known as type1
1417 */
1418static const struct sysc_regbits sysc_regbits_omap2 = {
1419 .dmadisable_shift = -ENODEV,
1420 .midle_shift = 12,
1421 .sidle_shift = 3,
1422 .clkact_shift = 8,
1423 .emufree_shift = 5,
1424 .enwkup_shift = 2,
1425 .srst_shift = 1,
1426 .autoidle_shift = 0,
1427};
1428
1429static const struct sysc_capabilities sysc_omap2 = {
1430 .type = TI_SYSC_OMAP2,
1431 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY | SYSC_OMAP2_EMUFREE |
1432 SYSC_OMAP2_ENAWAKEUP | SYSC_OMAP2_SOFTRESET |
1433 SYSC_OMAP2_AUTOIDLE,
1434 .regbits = &sysc_regbits_omap2,
1435};
1436
1437/* All omap2 and 3 timers, and timers 1, 2 & 10 on omap 4 and 5 */
1438static const struct sysc_capabilities sysc_omap2_timer = {
1439 .type = TI_SYSC_OMAP2_TIMER,
1440 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY | SYSC_OMAP2_EMUFREE |
1441 SYSC_OMAP2_ENAWAKEUP | SYSC_OMAP2_SOFTRESET |
1442 SYSC_OMAP2_AUTOIDLE,
1443 .regbits = &sysc_regbits_omap2,
1444 .mod_quirks = SYSC_QUIRK_USE_CLOCKACT,
1445};
1446
1447/*
1448 * SHAM2 (SHA1/MD5) sysc found on omap3, a variant of sysc_regbits_omap2
1449 * with different sidle position
1450 */
1451static const struct sysc_regbits sysc_regbits_omap3_sham = {
1452 .dmadisable_shift = -ENODEV,
1453 .midle_shift = -ENODEV,
1454 .sidle_shift = 4,
1455 .clkact_shift = -ENODEV,
1456 .enwkup_shift = -ENODEV,
1457 .srst_shift = 1,
1458 .autoidle_shift = 0,
1459 .emufree_shift = -ENODEV,
1460};
1461
1462static const struct sysc_capabilities sysc_omap3_sham = {
1463 .type = TI_SYSC_OMAP3_SHAM,
1464 .sysc_mask = SYSC_OMAP2_SOFTRESET | SYSC_OMAP2_AUTOIDLE,
1465 .regbits = &sysc_regbits_omap3_sham,
1466};
1467
1468/*
1469 * AES register bits found on omap3 and later, a variant of
1470 * sysc_regbits_omap2 with different sidle position
1471 */
1472static const struct sysc_regbits sysc_regbits_omap3_aes = {
1473 .dmadisable_shift = -ENODEV,
1474 .midle_shift = -ENODEV,
1475 .sidle_shift = 6,
1476 .clkact_shift = -ENODEV,
1477 .enwkup_shift = -ENODEV,
1478 .srst_shift = 1,
1479 .autoidle_shift = 0,
1480 .emufree_shift = -ENODEV,
1481};
1482
1483static const struct sysc_capabilities sysc_omap3_aes = {
1484 .type = TI_SYSC_OMAP3_AES,
1485 .sysc_mask = SYSC_OMAP2_SOFTRESET | SYSC_OMAP2_AUTOIDLE,
1486 .regbits = &sysc_regbits_omap3_aes,
1487};
1488
1489/*
1490 * Common sysc register bits found on omap4, also known as type2
1491 */
1492static const struct sysc_regbits sysc_regbits_omap4 = {
1493 .dmadisable_shift = 16,
1494 .midle_shift = 4,
1495 .sidle_shift = 2,
1496 .clkact_shift = -ENODEV,
1497 .enwkup_shift = -ENODEV,
1498 .emufree_shift = 1,
1499 .srst_shift = 0,
1500 .autoidle_shift = -ENODEV,
1501};
1502
1503static const struct sysc_capabilities sysc_omap4 = {
1504 .type = TI_SYSC_OMAP4,
1505 .sysc_mask = SYSC_OMAP4_DMADISABLE | SYSC_OMAP4_FREEEMU |
1506 SYSC_OMAP4_SOFTRESET,
1507 .regbits = &sysc_regbits_omap4,
1508};
1509
1510static const struct sysc_capabilities sysc_omap4_timer = {
1511 .type = TI_SYSC_OMAP4_TIMER,
1512 .sysc_mask = SYSC_OMAP4_DMADISABLE | SYSC_OMAP4_FREEEMU |
1513 SYSC_OMAP4_SOFTRESET,
1514 .regbits = &sysc_regbits_omap4,
1515};
1516
1517/*
1518 * Common sysc register bits found on omap4, also known as type3
1519 */
1520static const struct sysc_regbits sysc_regbits_omap4_simple = {
1521 .dmadisable_shift = -ENODEV,
1522 .midle_shift = 2,
1523 .sidle_shift = 0,
1524 .clkact_shift = -ENODEV,
1525 .enwkup_shift = -ENODEV,
1526 .srst_shift = -ENODEV,
1527 .emufree_shift = -ENODEV,
1528 .autoidle_shift = -ENODEV,
1529};
1530
1531static const struct sysc_capabilities sysc_omap4_simple = {
1532 .type = TI_SYSC_OMAP4_SIMPLE,
1533 .regbits = &sysc_regbits_omap4_simple,
1534};
1535
1536/*
1537 * SmartReflex sysc found on omap34xx
1538 */
1539static const struct sysc_regbits sysc_regbits_omap34xx_sr = {
1540 .dmadisable_shift = -ENODEV,
1541 .midle_shift = -ENODEV,
1542 .sidle_shift = -ENODEV,
1543 .clkact_shift = 20,
1544 .enwkup_shift = -ENODEV,
1545 .srst_shift = -ENODEV,
1546 .emufree_shift = -ENODEV,
1547 .autoidle_shift = -ENODEV,
1548};
1549
1550static const struct sysc_capabilities sysc_34xx_sr = {
1551 .type = TI_SYSC_OMAP34XX_SR,
1552 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY,
1553 .regbits = &sysc_regbits_omap34xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001554 .mod_quirks = SYSC_QUIRK_USE_CLOCKACT | SYSC_QUIRK_UNCACHED |
1555 SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08001556};
1557
1558/*
1559 * SmartReflex sysc found on omap36xx and later
1560 */
1561static const struct sysc_regbits sysc_regbits_omap36xx_sr = {
1562 .dmadisable_shift = -ENODEV,
1563 .midle_shift = -ENODEV,
1564 .sidle_shift = 24,
1565 .clkact_shift = -ENODEV,
1566 .enwkup_shift = 26,
1567 .srst_shift = -ENODEV,
1568 .emufree_shift = -ENODEV,
1569 .autoidle_shift = -ENODEV,
1570};
1571
1572static const struct sysc_capabilities sysc_36xx_sr = {
1573 .type = TI_SYSC_OMAP36XX_SR,
Tony Lindgren3267c082018-01-22 09:32:53 -08001574 .sysc_mask = SYSC_OMAP3_SR_ENAWAKEUP,
Tony Lindgren70a65242017-12-15 09:41:09 -08001575 .regbits = &sysc_regbits_omap36xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001576 .mod_quirks = SYSC_QUIRK_UNCACHED | SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08001577};
1578
1579static const struct sysc_capabilities sysc_omap4_sr = {
1580 .type = TI_SYSC_OMAP4_SR,
1581 .regbits = &sysc_regbits_omap36xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001582 .mod_quirks = SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08001583};
1584
1585/*
1586 * McASP register bits found on omap4 and later
1587 */
1588static const struct sysc_regbits sysc_regbits_omap4_mcasp = {
1589 .dmadisable_shift = -ENODEV,
1590 .midle_shift = -ENODEV,
1591 .sidle_shift = 0,
1592 .clkact_shift = -ENODEV,
1593 .enwkup_shift = -ENODEV,
1594 .srst_shift = -ENODEV,
1595 .emufree_shift = -ENODEV,
1596 .autoidle_shift = -ENODEV,
1597};
1598
1599static const struct sysc_capabilities sysc_omap4_mcasp = {
1600 .type = TI_SYSC_OMAP4_MCASP,
1601 .regbits = &sysc_regbits_omap4_mcasp,
1602};
1603
1604/*
1605 * FS USB host found on omap4 and later
1606 */
1607static const struct sysc_regbits sysc_regbits_omap4_usb_host_fs = {
1608 .dmadisable_shift = -ENODEV,
1609 .midle_shift = -ENODEV,
1610 .sidle_shift = 24,
1611 .clkact_shift = -ENODEV,
1612 .enwkup_shift = 26,
1613 .srst_shift = -ENODEV,
1614 .emufree_shift = -ENODEV,
1615 .autoidle_shift = -ENODEV,
1616};
1617
1618static const struct sysc_capabilities sysc_omap4_usb_host_fs = {
1619 .type = TI_SYSC_OMAP4_USB_HOST_FS,
1620 .sysc_mask = SYSC_OMAP2_ENAWAKEUP,
1621 .regbits = &sysc_regbits_omap4_usb_host_fs,
1622};
1623
Faiz Abbas7f35e632018-07-09 22:18:38 +05301624static const struct sysc_regbits sysc_regbits_dra7_mcan = {
1625 .dmadisable_shift = -ENODEV,
1626 .midle_shift = -ENODEV,
1627 .sidle_shift = -ENODEV,
1628 .clkact_shift = -ENODEV,
1629 .enwkup_shift = 4,
1630 .srst_shift = 0,
1631 .emufree_shift = -ENODEV,
1632 .autoidle_shift = -ENODEV,
1633};
1634
1635static const struct sysc_capabilities sysc_dra7_mcan = {
1636 .type = TI_SYSC_DRA7_MCAN,
1637 .sysc_mask = SYSC_DRA7_MCAN_ENAWAKEUP | SYSC_OMAP4_SOFTRESET,
1638 .regbits = &sysc_regbits_dra7_mcan,
1639};
1640
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001641static int sysc_init_pdata(struct sysc *ddata)
1642{
1643 struct ti_sysc_platform_data *pdata = dev_get_platdata(ddata->dev);
1644 struct ti_sysc_module_data mdata;
1645 int error = 0;
1646
1647 if (!pdata || !ddata->legacy_mode)
1648 return 0;
1649
1650 mdata.name = ddata->legacy_mode;
1651 mdata.module_pa = ddata->module_pa;
1652 mdata.module_size = ddata->module_size;
1653 mdata.offsets = ddata->offsets;
1654 mdata.nr_offsets = SYSC_MAX_REGS;
1655 mdata.cap = ddata->cap;
1656 mdata.cfg = &ddata->cfg;
1657
1658 if (!pdata->init_module)
1659 return -ENODEV;
1660
1661 error = pdata->init_module(ddata->dev, &mdata, &ddata->cookie);
1662 if (error == -EEXIST)
1663 error = 0;
1664
1665 return error;
1666}
1667
Tony Lindgren70a65242017-12-15 09:41:09 -08001668static int sysc_init_match(struct sysc *ddata)
1669{
1670 const struct sysc_capabilities *cap;
1671
1672 cap = of_device_get_match_data(ddata->dev);
1673 if (!cap)
1674 return -EINVAL;
1675
1676 ddata->cap = cap;
1677 if (ddata->cap)
1678 ddata->cfg.quirks |= ddata->cap->mod_quirks;
1679
1680 return 0;
1681}
1682
Tony Lindgren76f0f772018-02-23 08:28:45 -08001683static void ti_sysc_idle(struct work_struct *work)
1684{
1685 struct sysc *ddata;
1686
1687 ddata = container_of(work, struct sysc, idle_work.work);
1688
1689 if (pm_runtime_active(ddata->dev))
1690 pm_runtime_put_sync(ddata->dev);
1691}
1692
Tony Lindgrenc4bebea2018-04-16 10:24:54 -07001693static const struct of_device_id sysc_match_table[] = {
1694 { .compatible = "simple-bus", },
1695 { /* sentinel */ },
1696};
1697
Tony Lindgren0eecc632017-10-10 14:23:43 -07001698static int sysc_probe(struct platform_device *pdev)
1699{
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001700 struct ti_sysc_platform_data *pdata = dev_get_platdata(&pdev->dev);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001701 struct sysc *ddata;
1702 int error;
1703
1704 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
1705 if (!ddata)
1706 return -ENOMEM;
1707
1708 ddata->dev = &pdev->dev;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001709 platform_set_drvdata(pdev, ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001710
Tony Lindgren70a65242017-12-15 09:41:09 -08001711 error = sysc_init_match(ddata);
1712 if (error)
1713 return error;
1714
Tony Lindgren566a9b052017-12-15 09:41:19 -08001715 error = sysc_init_dts_quirks(ddata);
1716 if (error)
1717 goto unprepare;
1718
Tony Lindgren0eecc632017-10-10 14:23:43 -07001719 error = sysc_get_clocks(ddata);
1720 if (error)
1721 return error;
1722
1723 error = sysc_map_and_check_registers(ddata);
1724 if (error)
1725 goto unprepare;
1726
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08001727 error = sysc_init_sysc_mask(ddata);
1728 if (error)
1729 goto unprepare;
1730
1731 error = sysc_init_idlemodes(ddata);
1732 if (error)
1733 goto unprepare;
1734
1735 error = sysc_init_syss_mask(ddata);
1736 if (error)
1737 goto unprepare;
1738
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001739 error = sysc_init_pdata(ddata);
1740 if (error)
1741 goto unprepare;
1742
Tony Lindgren50622362018-04-16 10:20:27 -07001743 error = sysc_init_resets(ddata);
1744 if (error)
1745 return error;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001746
Tony Lindgren50622362018-04-16 10:20:27 -07001747 pm_runtime_enable(ddata->dev);
Tony Lindgren566a9b052017-12-15 09:41:19 -08001748 error = sysc_init_module(ddata);
1749 if (error)
1750 goto unprepare;
1751
Tony Lindgren0eecc632017-10-10 14:23:43 -07001752 error = pm_runtime_get_sync(ddata->dev);
1753 if (error < 0) {
1754 pm_runtime_put_noidle(ddata->dev);
1755 pm_runtime_disable(ddata->dev);
1756 goto unprepare;
1757 }
1758
Tony Lindgren0eecc632017-10-10 14:23:43 -07001759 sysc_show_registers(ddata);
1760
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001761 ddata->dev->type = &sysc_device_type;
Tony Lindgrenc4bebea2018-04-16 10:24:54 -07001762 error = of_platform_populate(ddata->dev->of_node, sysc_match_table,
1763 pdata ? pdata->auxdata : NULL,
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001764 ddata->dev);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001765 if (error)
1766 goto err;
1767
Tony Lindgren76f0f772018-02-23 08:28:45 -08001768 INIT_DELAYED_WORK(&ddata->idle_work, ti_sysc_idle);
1769
1770 /* At least earlycon won't survive without deferred idle */
1771 if (ddata->cfg.quirks & (SYSC_QUIRK_NO_IDLE_ON_INIT |
1772 SYSC_QUIRK_NO_RESET_ON_INIT)) {
1773 schedule_delayed_work(&ddata->idle_work, 3000);
1774 } else {
1775 pm_runtime_put(&pdev->dev);
1776 }
Tony Lindgren0eecc632017-10-10 14:23:43 -07001777
Tony Lindgren50622362018-04-16 10:20:27 -07001778 if (!of_get_available_child_count(ddata->dev->of_node))
1779 reset_control_assert(ddata->rsts);
1780
Tony Lindgren0eecc632017-10-10 14:23:43 -07001781 return 0;
1782
1783err:
Tony Lindgren0eecc632017-10-10 14:23:43 -07001784 pm_runtime_put_sync(&pdev->dev);
1785 pm_runtime_disable(&pdev->dev);
1786unprepare:
1787 sysc_unprepare(ddata);
1788
1789 return error;
1790}
1791
Tony Lindgren684be5a2017-10-13 10:48:40 -07001792static int sysc_remove(struct platform_device *pdev)
1793{
1794 struct sysc *ddata = platform_get_drvdata(pdev);
1795 int error;
1796
Tony Lindgren76f0f772018-02-23 08:28:45 -08001797 cancel_delayed_work_sync(&ddata->idle_work);
1798
Tony Lindgren684be5a2017-10-13 10:48:40 -07001799 error = pm_runtime_get_sync(ddata->dev);
1800 if (error < 0) {
1801 pm_runtime_put_noidle(ddata->dev);
1802 pm_runtime_disable(ddata->dev);
1803 goto unprepare;
1804 }
1805
1806 of_platform_depopulate(&pdev->dev);
1807
Tony Lindgren684be5a2017-10-13 10:48:40 -07001808 pm_runtime_put_sync(&pdev->dev);
1809 pm_runtime_disable(&pdev->dev);
Tony Lindgren50622362018-04-16 10:20:27 -07001810 reset_control_assert(ddata->rsts);
Tony Lindgren684be5a2017-10-13 10:48:40 -07001811
1812unprepare:
1813 sysc_unprepare(ddata);
1814
1815 return 0;
1816}
1817
Tony Lindgren0eecc632017-10-10 14:23:43 -07001818static const struct of_device_id sysc_match[] = {
Tony Lindgren70a65242017-12-15 09:41:09 -08001819 { .compatible = "ti,sysc-omap2", .data = &sysc_omap2, },
1820 { .compatible = "ti,sysc-omap2-timer", .data = &sysc_omap2_timer, },
1821 { .compatible = "ti,sysc-omap4", .data = &sysc_omap4, },
1822 { .compatible = "ti,sysc-omap4-timer", .data = &sysc_omap4_timer, },
1823 { .compatible = "ti,sysc-omap4-simple", .data = &sysc_omap4_simple, },
1824 { .compatible = "ti,sysc-omap3430-sr", .data = &sysc_34xx_sr, },
1825 { .compatible = "ti,sysc-omap3630-sr", .data = &sysc_36xx_sr, },
1826 { .compatible = "ti,sysc-omap4-sr", .data = &sysc_omap4_sr, },
1827 { .compatible = "ti,sysc-omap3-sham", .data = &sysc_omap3_sham, },
1828 { .compatible = "ti,sysc-omap-aes", .data = &sysc_omap3_aes, },
1829 { .compatible = "ti,sysc-mcasp", .data = &sysc_omap4_mcasp, },
1830 { .compatible = "ti,sysc-usb-host-fs",
1831 .data = &sysc_omap4_usb_host_fs, },
Faiz Abbas7f35e632018-07-09 22:18:38 +05301832 { .compatible = "ti,sysc-dra7-mcan", .data = &sysc_dra7_mcan, },
Tony Lindgren0eecc632017-10-10 14:23:43 -07001833 { },
1834};
1835MODULE_DEVICE_TABLE(of, sysc_match);
1836
1837static struct platform_driver sysc_driver = {
1838 .probe = sysc_probe,
Tony Lindgren684be5a2017-10-13 10:48:40 -07001839 .remove = sysc_remove,
Tony Lindgren0eecc632017-10-10 14:23:43 -07001840 .driver = {
1841 .name = "ti-sysc",
1842 .of_match_table = sysc_match,
1843 .pm = &sysc_pm_ops,
1844 },
1845};
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001846
1847static int __init sysc_init(void)
1848{
1849 bus_register_notifier(&platform_bus_type, &sysc_nb);
1850
1851 return platform_driver_register(&sysc_driver);
1852}
1853module_init(sysc_init);
1854
1855static void __exit sysc_exit(void)
1856{
1857 bus_unregister_notifier(&platform_bus_type, &sysc_nb);
1858 platform_driver_unregister(&sysc_driver);
1859}
1860module_exit(sysc_exit);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001861
1862MODULE_DESCRIPTION("TI sysc interconnect target driver");
1863MODULE_LICENSE("GPL v2");