blob: 087a67617eef0eee14d284d737d0cb1613491701 [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() */
872 SYSC_QUIRK("gpio", 0, 0, 0x10, 0x114, 0x50600801, 0xffffffff,
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 */
892 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x50411e03, 0xffffffff,
893 SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgren7e27e5d2018-04-20 14:54:16 -0700894
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700895#ifdef DEBUG
896 SYSC_QUIRK("aess", 0, 0, 0x10, -1, 0x40000000, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -0700897 SYSC_QUIRK("control", 0, 0, 0x10, -1, 0x40000900, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700898 SYSC_QUIRK("gpu", 0, 0x1fc00, 0x1fc10, -1, 0, 0, 0),
899 SYSC_QUIRK("hdq1w", 0, 0, 0x14, 0x18, 0x00000006, 0xffffffff, 0),
900 SYSC_QUIRK("hsi", 0, 0, 0x10, 0x14, 0x50043101, 0xffffffff, 0),
901 SYSC_QUIRK("iss", 0, 0, 0x10, -1, 0x40000101, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -0700902 SYSC_QUIRK("i2c", 0, 0, 0x10, 0x90, 0x5040000a, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700903 SYSC_QUIRK("mcasp", 0, 0, 0x4, -1, 0x44306302, 0xffffffff, 0),
904 SYSC_QUIRK("mcbsp", 0, -1, 0x8c, -1, 0, 0, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -0700905 SYSC_QUIRK("mcspi", 0, 0, 0x10, -1, 0x40300a0b, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700906 SYSC_QUIRK("mailbox", 0, 0, 0x10, -1, 0x00000400, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -0700907 SYSC_QUIRK("ocp2scp", 0, 0, 0x10, 0x14, 0x50060005, 0xffffffff, 0),
908 SYSC_QUIRK("padconf", 0, 0, 0x10, -1, 0x4fff0800, 0xffffffff, 0),
909 SYSC_QUIRK("prcm", 0, 0, -1, -1, 0x40000100, 0xffffffff, 0),
910 SYSC_QUIRK("scm", 0, 0, 0x10, -1, 0x40000900, 0xffffffff, 0),
911 SYSC_QUIRK("scrm", 0, 0, -1, -1, 0x00000010, 0xffffffff, 0),
912 SYSC_QUIRK("sdio", 0, 0, 0x10, -1, 0x40202301, 0xffffffff, 0),
913 SYSC_QUIRK("sdma", 0, 0, 0x2c, 0x28, 0x00010900, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700914 SYSC_QUIRK("slimbus", 0, 0, 0x10, -1, 0x40000902, 0xffffffff, 0),
915 SYSC_QUIRK("slimbus", 0, 0, 0x10, -1, 0x40002903, 0xffffffff, 0),
916 SYSC_QUIRK("spinlock", 0, 0, 0x10, -1, 0x50020000, 0xffffffff, 0),
917 SYSC_QUIRK("usbhstll", 0, 0, 0x10, 0x14, 0x00000004, 0xffffffff, 0),
918 SYSC_QUIRK("usb_host_hs", 0, 0, 0x10, 0x14, 0x50700100, 0xffffffff, 0),
919 SYSC_QUIRK("usb_otg_hs", 0, 0x400, 0x404, 0x408, 0x00000050,
920 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -0700921 SYSC_QUIRK("wdt", 0, 0, 0x10, 0x14, 0x502a0500, 0xffffffff, 0),
922 SYSC_QUIRK("wdt", 0, 0, 0x10, 0x14, 0x502a0d00, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700923#endif
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800924};
925
926static void sysc_init_revision_quirks(struct sysc *ddata)
927{
928 const struct sysc_revision_quirk *q;
929 int i;
930
931 for (i = 0; i < ARRAY_SIZE(sysc_revision_quirks); i++) {
932 q = &sysc_revision_quirks[i];
933
934 if (q->base && q->base != ddata->module_pa)
935 continue;
936
937 if (q->rev_offset >= 0 &&
938 q->rev_offset != ddata->offsets[SYSC_REVISION])
939 continue;
940
941 if (q->sysc_offset >= 0 &&
942 q->sysc_offset != ddata->offsets[SYSC_SYSCONFIG])
943 continue;
944
945 if (q->syss_offset >= 0 &&
946 q->syss_offset != ddata->offsets[SYSC_SYSSTATUS])
947 continue;
948
949 if (q->revision == ddata->revision ||
950 (q->revision & q->revision_mask) ==
951 (ddata->revision & q->revision_mask)) {
952 ddata->name = q->name;
953 ddata->cfg.quirks |= q->quirks;
954 }
955 }
956}
957
Faiz Abbas596e7952018-07-09 22:18:39 +0530958static int sysc_reset(struct sysc *ddata)
959{
960 int offset = ddata->offsets[SYSC_SYSCONFIG];
961 int val;
962
963 if (ddata->legacy_mode || offset < 0 ||
964 ddata->cfg.quirks & SYSC_QUIRK_NO_RESET_ON_INIT)
965 return 0;
966
967 /*
968 * Currently only support reset status in sysstatus.
969 * Warn and return error in all other cases
970 */
971 if (!ddata->cfg.syss_mask) {
972 dev_err(ddata->dev, "No ti,syss-mask. Reset failed\n");
973 return -EINVAL;
974 }
975
976 val = sysc_read(ddata, offset);
977 val |= (0x1 << ddata->cap->regbits->srst_shift);
978 sysc_write(ddata, offset, val);
979
980 /* Poll on reset status */
981 offset = ddata->offsets[SYSC_SYSSTATUS];
982
983 return readl_poll_timeout(ddata->module_va + offset, val,
984 (val & ddata->cfg.syss_mask) == 0x0,
985 100, MAX_MODULE_SOFTRESET_WAIT);
986}
987
Tony Lindgren566a9b052017-12-15 09:41:19 -0800988/* At this point the module is configured enough to read the revision */
989static int sysc_init_module(struct sysc *ddata)
990{
991 int error;
992
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800993 if (ddata->cfg.quirks & SYSC_QUIRK_NO_IDLE_ON_INIT) {
994 ddata->revision = sysc_read_revision(ddata);
995 goto rev_quirks;
996 }
997
Tony Lindgren566a9b052017-12-15 09:41:19 -0800998 error = pm_runtime_get_sync(ddata->dev);
999 if (error < 0) {
1000 pm_runtime_put_noidle(ddata->dev);
1001
1002 return 0;
1003 }
Tony Lindgren50622362018-04-16 10:20:27 -07001004
Faiz Abbas596e7952018-07-09 22:18:39 +05301005 error = sysc_reset(ddata);
1006 if (error) {
1007 dev_err(ddata->dev, "Reset failed with %d\n", error);
1008 pm_runtime_put_sync(ddata->dev);
1009
1010 return error;
1011 }
1012
Tony Lindgren566a9b052017-12-15 09:41:19 -08001013 ddata->revision = sysc_read_revision(ddata);
1014 pm_runtime_put_sync(ddata->dev);
1015
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001016rev_quirks:
1017 sysc_init_revision_quirks(ddata);
1018
Tony Lindgren566a9b052017-12-15 09:41:19 -08001019 return 0;
1020}
1021
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08001022static int sysc_init_sysc_mask(struct sysc *ddata)
1023{
1024 struct device_node *np = ddata->dev->of_node;
1025 int error;
1026 u32 val;
1027
1028 error = of_property_read_u32(np, "ti,sysc-mask", &val);
1029 if (error)
1030 return 0;
1031
1032 if (val)
1033 ddata->cfg.sysc_val = val & ddata->cap->sysc_mask;
1034 else
1035 ddata->cfg.sysc_val = ddata->cap->sysc_mask;
1036
1037 return 0;
1038}
1039
1040static int sysc_init_idlemode(struct sysc *ddata, u8 *idlemodes,
1041 const char *name)
1042{
1043 struct device_node *np = ddata->dev->of_node;
1044 struct property *prop;
1045 const __be32 *p;
1046 u32 val;
1047
1048 of_property_for_each_u32(np, name, prop, p, val) {
1049 if (val >= SYSC_NR_IDLEMODES) {
1050 dev_err(ddata->dev, "invalid idlemode: %i\n", val);
1051 return -EINVAL;
1052 }
1053 *idlemodes |= (1 << val);
1054 }
1055
1056 return 0;
1057}
1058
1059static int sysc_init_idlemodes(struct sysc *ddata)
1060{
1061 int error;
1062
1063 error = sysc_init_idlemode(ddata, &ddata->cfg.midlemodes,
1064 "ti,sysc-midle");
1065 if (error)
1066 return error;
1067
1068 error = sysc_init_idlemode(ddata, &ddata->cfg.sidlemodes,
1069 "ti,sysc-sidle");
1070 if (error)
1071 return error;
1072
1073 return 0;
1074}
1075
1076/*
1077 * Only some devices on omap4 and later have SYSCONFIG reset done
1078 * bit. We can detect this if there is no SYSSTATUS at all, or the
1079 * SYSTATUS bit 0 is not used. Note that some SYSSTATUS registers
1080 * have multiple bits for the child devices like OHCI and EHCI.
1081 * Depends on SYSC being parsed first.
1082 */
1083static int sysc_init_syss_mask(struct sysc *ddata)
1084{
1085 struct device_node *np = ddata->dev->of_node;
1086 int error;
1087 u32 val;
1088
1089 error = of_property_read_u32(np, "ti,syss-mask", &val);
1090 if (error) {
1091 if ((ddata->cap->type == TI_SYSC_OMAP4 ||
1092 ddata->cap->type == TI_SYSC_OMAP4_TIMER) &&
1093 (ddata->cfg.sysc_val & SYSC_OMAP4_SOFTRESET))
1094 ddata->cfg.quirks |= SYSC_QUIRK_RESET_STATUS;
1095
1096 return 0;
1097 }
1098
1099 if (!(val & 1) && (ddata->cfg.sysc_val & SYSC_OMAP4_SOFTRESET))
1100 ddata->cfg.quirks |= SYSC_QUIRK_RESET_STATUS;
1101
1102 ddata->cfg.syss_mask = val;
1103
1104 return 0;
1105}
1106
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001107/*
Tony Lindgren8b2830b2018-04-16 10:26:09 -07001108 * Many child device drivers need to have fck and opt clocks available
1109 * to get the clock rate for device internal configuration etc.
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001110 */
Tony Lindgren8b2830b2018-04-16 10:26:09 -07001111static int sysc_child_add_named_clock(struct sysc *ddata,
1112 struct device *child,
1113 const char *name)
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001114{
Tony Lindgren8b2830b2018-04-16 10:26:09 -07001115 struct clk *clk;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001116 struct clk_lookup *l;
Tony Lindgren8b2830b2018-04-16 10:26:09 -07001117 int error = 0;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001118
Tony Lindgren8b2830b2018-04-16 10:26:09 -07001119 if (!name)
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001120 return 0;
1121
Tony Lindgren8b2830b2018-04-16 10:26:09 -07001122 clk = clk_get(child, name);
1123 if (!IS_ERR(clk)) {
1124 clk_put(clk);
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001125
1126 return -EEXIST;
1127 }
1128
Tony Lindgren8b2830b2018-04-16 10:26:09 -07001129 clk = clk_get(ddata->dev, name);
1130 if (IS_ERR(clk))
1131 return -ENODEV;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001132
Tony Lindgren8b2830b2018-04-16 10:26:09 -07001133 l = clkdev_create(clk, name, dev_name(child));
1134 if (!l)
1135 error = -ENOMEM;
1136
1137 clk_put(clk);
1138
1139 return error;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001140}
1141
Tony Lindgren09dfe582018-04-16 10:25:52 -07001142static int sysc_child_add_clocks(struct sysc *ddata,
1143 struct device *child)
1144{
1145 int i, error;
1146
1147 for (i = 0; i < ddata->nr_clocks; i++) {
1148 error = sysc_child_add_named_clock(ddata,
1149 child,
1150 ddata->clock_roles[i]);
1151 if (error && error != -EEXIST) {
1152 dev_err(ddata->dev, "could not add child clock %s: %i\n",
1153 ddata->clock_roles[i], error);
1154
1155 return error;
1156 }
1157 }
1158
1159 return 0;
1160}
1161
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001162static struct device_type sysc_device_type = {
1163};
1164
1165static struct sysc *sysc_child_to_parent(struct device *dev)
1166{
1167 struct device *parent = dev->parent;
1168
1169 if (!parent || parent->type != &sysc_device_type)
1170 return NULL;
1171
1172 return dev_get_drvdata(parent);
1173}
1174
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001175static int __maybe_unused sysc_child_runtime_suspend(struct device *dev)
1176{
1177 struct sysc *ddata;
1178 int error;
1179
1180 ddata = sysc_child_to_parent(dev);
1181
1182 error = pm_generic_runtime_suspend(dev);
1183 if (error)
1184 return error;
1185
1186 if (!ddata->enabled)
1187 return 0;
1188
1189 return sysc_runtime_suspend(ddata->dev);
1190}
1191
1192static int __maybe_unused sysc_child_runtime_resume(struct device *dev)
1193{
1194 struct sysc *ddata;
1195 int error;
1196
1197 ddata = sysc_child_to_parent(dev);
1198
1199 if (!ddata->enabled) {
1200 error = sysc_runtime_resume(ddata->dev);
1201 if (error < 0)
1202 dev_err(ddata->dev,
1203 "%s error: %i\n", __func__, error);
1204 }
1205
1206 return pm_generic_runtime_resume(dev);
1207}
1208
1209#ifdef CONFIG_PM_SLEEP
1210static int sysc_child_suspend_noirq(struct device *dev)
1211{
1212 struct sysc *ddata;
1213 int error;
1214
1215 ddata = sysc_child_to_parent(dev);
1216
Tony Lindgrenef55f822018-04-16 10:27:15 -07001217 dev_dbg(ddata->dev, "%s %s\n", __func__,
1218 ddata->name ? ddata->name : "");
1219
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001220 error = pm_generic_suspend_noirq(dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001221 if (error) {
1222 dev_err(dev, "%s error at %i: %i\n",
1223 __func__, __LINE__, error);
1224
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001225 return error;
Tony Lindgrenef55f822018-04-16 10:27:15 -07001226 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001227
1228 if (!pm_runtime_status_suspended(dev)) {
1229 error = pm_generic_runtime_suspend(dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001230 if (error) {
1231 dev_err(dev, "%s error at %i: %i\n",
1232 __func__, __LINE__, error);
1233
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001234 return error;
Tony Lindgrenef55f822018-04-16 10:27:15 -07001235 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001236
1237 error = sysc_runtime_suspend(ddata->dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001238 if (error) {
1239 dev_err(dev, "%s error at %i: %i\n",
1240 __func__, __LINE__, error);
1241
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001242 return error;
Tony Lindgrenef55f822018-04-16 10:27:15 -07001243 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001244
1245 ddata->child_needs_resume = true;
1246 }
1247
1248 return 0;
1249}
1250
1251static int sysc_child_resume_noirq(struct device *dev)
1252{
1253 struct sysc *ddata;
1254 int error;
1255
1256 ddata = sysc_child_to_parent(dev);
1257
Tony Lindgrenef55f822018-04-16 10:27:15 -07001258 dev_dbg(ddata->dev, "%s %s\n", __func__,
1259 ddata->name ? ddata->name : "");
1260
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001261 if (ddata->child_needs_resume) {
1262 ddata->child_needs_resume = false;
1263
1264 error = sysc_runtime_resume(ddata->dev);
1265 if (error)
1266 dev_err(ddata->dev,
1267 "%s runtime resume error: %i\n",
1268 __func__, error);
1269
1270 error = pm_generic_runtime_resume(dev);
1271 if (error)
1272 dev_err(ddata->dev,
1273 "%s generic runtime resume: %i\n",
1274 __func__, error);
1275 }
1276
1277 return pm_generic_resume_noirq(dev);
1278}
1279#endif
1280
1281struct dev_pm_domain sysc_child_pm_domain = {
1282 .ops = {
1283 SET_RUNTIME_PM_OPS(sysc_child_runtime_suspend,
1284 sysc_child_runtime_resume,
1285 NULL)
1286 USE_PLATFORM_PM_SLEEP_OPS
1287 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(sysc_child_suspend_noirq,
1288 sysc_child_resume_noirq)
1289 }
1290};
1291
1292/**
1293 * sysc_legacy_idle_quirk - handle children in omap_device compatible way
1294 * @ddata: device driver data
1295 * @child: child device driver
1296 *
1297 * Allow idle for child devices as done with _od_runtime_suspend().
1298 * Otherwise many child devices will not idle because of the permanent
1299 * parent usecount set in pm_runtime_irq_safe().
1300 *
1301 * Note that the long term solution is to just modify the child device
1302 * drivers to not set pm_runtime_irq_safe() and then this can be just
1303 * dropped.
1304 */
1305static void sysc_legacy_idle_quirk(struct sysc *ddata, struct device *child)
1306{
1307 if (!ddata->legacy_mode)
1308 return;
1309
1310 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
1311 dev_pm_domain_set(child, &sysc_child_pm_domain);
1312}
1313
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001314static int sysc_notifier_call(struct notifier_block *nb,
1315 unsigned long event, void *device)
1316{
1317 struct device *dev = device;
1318 struct sysc *ddata;
1319 int error;
1320
1321 ddata = sysc_child_to_parent(dev);
1322 if (!ddata)
1323 return NOTIFY_DONE;
1324
1325 switch (event) {
1326 case BUS_NOTIFY_ADD_DEVICE:
Tony Lindgren09dfe582018-04-16 10:25:52 -07001327 error = sysc_child_add_clocks(ddata, dev);
1328 if (error)
1329 return error;
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001330 sysc_legacy_idle_quirk(ddata, dev);
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001331 break;
1332 default:
1333 break;
1334 }
1335
1336 return NOTIFY_DONE;
1337}
1338
1339static struct notifier_block sysc_nb = {
1340 .notifier_call = sysc_notifier_call,
1341};
1342
Tony Lindgren566a9b052017-12-15 09:41:19 -08001343/* Device tree configured quirks */
1344struct sysc_dts_quirk {
1345 const char *name;
1346 u32 mask;
1347};
1348
1349static const struct sysc_dts_quirk sysc_dts_quirks[] = {
1350 { .name = "ti,no-idle-on-init",
1351 .mask = SYSC_QUIRK_NO_IDLE_ON_INIT, },
1352 { .name = "ti,no-reset-on-init",
1353 .mask = SYSC_QUIRK_NO_RESET_ON_INIT, },
1354};
1355
1356static int sysc_init_dts_quirks(struct sysc *ddata)
1357{
1358 struct device_node *np = ddata->dev->of_node;
1359 const struct property *prop;
1360 int i, len, error;
1361 u32 val;
1362
1363 ddata->legacy_mode = of_get_property(np, "ti,hwmods", NULL);
1364
1365 for (i = 0; i < ARRAY_SIZE(sysc_dts_quirks); i++) {
1366 prop = of_get_property(np, sysc_dts_quirks[i].name, &len);
1367 if (!prop)
Tony Lindgrend39b6ea2018-02-15 09:18:55 -08001368 continue;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001369
1370 ddata->cfg.quirks |= sysc_dts_quirks[i].mask;
1371 }
1372
1373 error = of_property_read_u32(np, "ti,sysc-delay-us", &val);
1374 if (!error) {
1375 if (val > 255) {
1376 dev_warn(ddata->dev, "bad ti,sysc-delay-us: %i\n",
1377 val);
1378 }
1379
1380 ddata->cfg.srst_udelay = (u8)val;
1381 }
1382
1383 return 0;
1384}
1385
Tony Lindgren0eecc632017-10-10 14:23:43 -07001386static void sysc_unprepare(struct sysc *ddata)
1387{
1388 int i;
1389
1390 for (i = 0; i < SYSC_MAX_CLOCKS; i++) {
1391 if (!IS_ERR_OR_NULL(ddata->clocks[i]))
1392 clk_unprepare(ddata->clocks[i]);
1393 }
1394}
1395
Tony Lindgren70a65242017-12-15 09:41:09 -08001396/*
1397 * Common sysc register bits found on omap2, also known as type1
1398 */
1399static const struct sysc_regbits sysc_regbits_omap2 = {
1400 .dmadisable_shift = -ENODEV,
1401 .midle_shift = 12,
1402 .sidle_shift = 3,
1403 .clkact_shift = 8,
1404 .emufree_shift = 5,
1405 .enwkup_shift = 2,
1406 .srst_shift = 1,
1407 .autoidle_shift = 0,
1408};
1409
1410static const struct sysc_capabilities sysc_omap2 = {
1411 .type = TI_SYSC_OMAP2,
1412 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY | SYSC_OMAP2_EMUFREE |
1413 SYSC_OMAP2_ENAWAKEUP | SYSC_OMAP2_SOFTRESET |
1414 SYSC_OMAP2_AUTOIDLE,
1415 .regbits = &sysc_regbits_omap2,
1416};
1417
1418/* All omap2 and 3 timers, and timers 1, 2 & 10 on omap 4 and 5 */
1419static const struct sysc_capabilities sysc_omap2_timer = {
1420 .type = TI_SYSC_OMAP2_TIMER,
1421 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY | SYSC_OMAP2_EMUFREE |
1422 SYSC_OMAP2_ENAWAKEUP | SYSC_OMAP2_SOFTRESET |
1423 SYSC_OMAP2_AUTOIDLE,
1424 .regbits = &sysc_regbits_omap2,
1425 .mod_quirks = SYSC_QUIRK_USE_CLOCKACT,
1426};
1427
1428/*
1429 * SHAM2 (SHA1/MD5) sysc found on omap3, a variant of sysc_regbits_omap2
1430 * with different sidle position
1431 */
1432static const struct sysc_regbits sysc_regbits_omap3_sham = {
1433 .dmadisable_shift = -ENODEV,
1434 .midle_shift = -ENODEV,
1435 .sidle_shift = 4,
1436 .clkact_shift = -ENODEV,
1437 .enwkup_shift = -ENODEV,
1438 .srst_shift = 1,
1439 .autoidle_shift = 0,
1440 .emufree_shift = -ENODEV,
1441};
1442
1443static const struct sysc_capabilities sysc_omap3_sham = {
1444 .type = TI_SYSC_OMAP3_SHAM,
1445 .sysc_mask = SYSC_OMAP2_SOFTRESET | SYSC_OMAP2_AUTOIDLE,
1446 .regbits = &sysc_regbits_omap3_sham,
1447};
1448
1449/*
1450 * AES register bits found on omap3 and later, a variant of
1451 * sysc_regbits_omap2 with different sidle position
1452 */
1453static const struct sysc_regbits sysc_regbits_omap3_aes = {
1454 .dmadisable_shift = -ENODEV,
1455 .midle_shift = -ENODEV,
1456 .sidle_shift = 6,
1457 .clkact_shift = -ENODEV,
1458 .enwkup_shift = -ENODEV,
1459 .srst_shift = 1,
1460 .autoidle_shift = 0,
1461 .emufree_shift = -ENODEV,
1462};
1463
1464static const struct sysc_capabilities sysc_omap3_aes = {
1465 .type = TI_SYSC_OMAP3_AES,
1466 .sysc_mask = SYSC_OMAP2_SOFTRESET | SYSC_OMAP2_AUTOIDLE,
1467 .regbits = &sysc_regbits_omap3_aes,
1468};
1469
1470/*
1471 * Common sysc register bits found on omap4, also known as type2
1472 */
1473static const struct sysc_regbits sysc_regbits_omap4 = {
1474 .dmadisable_shift = 16,
1475 .midle_shift = 4,
1476 .sidle_shift = 2,
1477 .clkact_shift = -ENODEV,
1478 .enwkup_shift = -ENODEV,
1479 .emufree_shift = 1,
1480 .srst_shift = 0,
1481 .autoidle_shift = -ENODEV,
1482};
1483
1484static const struct sysc_capabilities sysc_omap4 = {
1485 .type = TI_SYSC_OMAP4,
1486 .sysc_mask = SYSC_OMAP4_DMADISABLE | SYSC_OMAP4_FREEEMU |
1487 SYSC_OMAP4_SOFTRESET,
1488 .regbits = &sysc_regbits_omap4,
1489};
1490
1491static const struct sysc_capabilities sysc_omap4_timer = {
1492 .type = TI_SYSC_OMAP4_TIMER,
1493 .sysc_mask = SYSC_OMAP4_DMADISABLE | SYSC_OMAP4_FREEEMU |
1494 SYSC_OMAP4_SOFTRESET,
1495 .regbits = &sysc_regbits_omap4,
1496};
1497
1498/*
1499 * Common sysc register bits found on omap4, also known as type3
1500 */
1501static const struct sysc_regbits sysc_regbits_omap4_simple = {
1502 .dmadisable_shift = -ENODEV,
1503 .midle_shift = 2,
1504 .sidle_shift = 0,
1505 .clkact_shift = -ENODEV,
1506 .enwkup_shift = -ENODEV,
1507 .srst_shift = -ENODEV,
1508 .emufree_shift = -ENODEV,
1509 .autoidle_shift = -ENODEV,
1510};
1511
1512static const struct sysc_capabilities sysc_omap4_simple = {
1513 .type = TI_SYSC_OMAP4_SIMPLE,
1514 .regbits = &sysc_regbits_omap4_simple,
1515};
1516
1517/*
1518 * SmartReflex sysc found on omap34xx
1519 */
1520static const struct sysc_regbits sysc_regbits_omap34xx_sr = {
1521 .dmadisable_shift = -ENODEV,
1522 .midle_shift = -ENODEV,
1523 .sidle_shift = -ENODEV,
1524 .clkact_shift = 20,
1525 .enwkup_shift = -ENODEV,
1526 .srst_shift = -ENODEV,
1527 .emufree_shift = -ENODEV,
1528 .autoidle_shift = -ENODEV,
1529};
1530
1531static const struct sysc_capabilities sysc_34xx_sr = {
1532 .type = TI_SYSC_OMAP34XX_SR,
1533 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY,
1534 .regbits = &sysc_regbits_omap34xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001535 .mod_quirks = SYSC_QUIRK_USE_CLOCKACT | SYSC_QUIRK_UNCACHED |
1536 SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08001537};
1538
1539/*
1540 * SmartReflex sysc found on omap36xx and later
1541 */
1542static const struct sysc_regbits sysc_regbits_omap36xx_sr = {
1543 .dmadisable_shift = -ENODEV,
1544 .midle_shift = -ENODEV,
1545 .sidle_shift = 24,
1546 .clkact_shift = -ENODEV,
1547 .enwkup_shift = 26,
1548 .srst_shift = -ENODEV,
1549 .emufree_shift = -ENODEV,
1550 .autoidle_shift = -ENODEV,
1551};
1552
1553static const struct sysc_capabilities sysc_36xx_sr = {
1554 .type = TI_SYSC_OMAP36XX_SR,
Tony Lindgren3267c082018-01-22 09:32:53 -08001555 .sysc_mask = SYSC_OMAP3_SR_ENAWAKEUP,
Tony Lindgren70a65242017-12-15 09:41:09 -08001556 .regbits = &sysc_regbits_omap36xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001557 .mod_quirks = SYSC_QUIRK_UNCACHED | SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08001558};
1559
1560static const struct sysc_capabilities sysc_omap4_sr = {
1561 .type = TI_SYSC_OMAP4_SR,
1562 .regbits = &sysc_regbits_omap36xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001563 .mod_quirks = SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08001564};
1565
1566/*
1567 * McASP register bits found on omap4 and later
1568 */
1569static const struct sysc_regbits sysc_regbits_omap4_mcasp = {
1570 .dmadisable_shift = -ENODEV,
1571 .midle_shift = -ENODEV,
1572 .sidle_shift = 0,
1573 .clkact_shift = -ENODEV,
1574 .enwkup_shift = -ENODEV,
1575 .srst_shift = -ENODEV,
1576 .emufree_shift = -ENODEV,
1577 .autoidle_shift = -ENODEV,
1578};
1579
1580static const struct sysc_capabilities sysc_omap4_mcasp = {
1581 .type = TI_SYSC_OMAP4_MCASP,
1582 .regbits = &sysc_regbits_omap4_mcasp,
1583};
1584
1585/*
1586 * FS USB host found on omap4 and later
1587 */
1588static const struct sysc_regbits sysc_regbits_omap4_usb_host_fs = {
1589 .dmadisable_shift = -ENODEV,
1590 .midle_shift = -ENODEV,
1591 .sidle_shift = 24,
1592 .clkact_shift = -ENODEV,
1593 .enwkup_shift = 26,
1594 .srst_shift = -ENODEV,
1595 .emufree_shift = -ENODEV,
1596 .autoidle_shift = -ENODEV,
1597};
1598
1599static const struct sysc_capabilities sysc_omap4_usb_host_fs = {
1600 .type = TI_SYSC_OMAP4_USB_HOST_FS,
1601 .sysc_mask = SYSC_OMAP2_ENAWAKEUP,
1602 .regbits = &sysc_regbits_omap4_usb_host_fs,
1603};
1604
Faiz Abbas7f35e632018-07-09 22:18:38 +05301605static const struct sysc_regbits sysc_regbits_dra7_mcan = {
1606 .dmadisable_shift = -ENODEV,
1607 .midle_shift = -ENODEV,
1608 .sidle_shift = -ENODEV,
1609 .clkact_shift = -ENODEV,
1610 .enwkup_shift = 4,
1611 .srst_shift = 0,
1612 .emufree_shift = -ENODEV,
1613 .autoidle_shift = -ENODEV,
1614};
1615
1616static const struct sysc_capabilities sysc_dra7_mcan = {
1617 .type = TI_SYSC_DRA7_MCAN,
1618 .sysc_mask = SYSC_DRA7_MCAN_ENAWAKEUP | SYSC_OMAP4_SOFTRESET,
1619 .regbits = &sysc_regbits_dra7_mcan,
1620};
1621
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001622static int sysc_init_pdata(struct sysc *ddata)
1623{
1624 struct ti_sysc_platform_data *pdata = dev_get_platdata(ddata->dev);
1625 struct ti_sysc_module_data mdata;
1626 int error = 0;
1627
1628 if (!pdata || !ddata->legacy_mode)
1629 return 0;
1630
1631 mdata.name = ddata->legacy_mode;
1632 mdata.module_pa = ddata->module_pa;
1633 mdata.module_size = ddata->module_size;
1634 mdata.offsets = ddata->offsets;
1635 mdata.nr_offsets = SYSC_MAX_REGS;
1636 mdata.cap = ddata->cap;
1637 mdata.cfg = &ddata->cfg;
1638
1639 if (!pdata->init_module)
1640 return -ENODEV;
1641
1642 error = pdata->init_module(ddata->dev, &mdata, &ddata->cookie);
1643 if (error == -EEXIST)
1644 error = 0;
1645
1646 return error;
1647}
1648
Tony Lindgren70a65242017-12-15 09:41:09 -08001649static int sysc_init_match(struct sysc *ddata)
1650{
1651 const struct sysc_capabilities *cap;
1652
1653 cap = of_device_get_match_data(ddata->dev);
1654 if (!cap)
1655 return -EINVAL;
1656
1657 ddata->cap = cap;
1658 if (ddata->cap)
1659 ddata->cfg.quirks |= ddata->cap->mod_quirks;
1660
1661 return 0;
1662}
1663
Tony Lindgren76f0f772018-02-23 08:28:45 -08001664static void ti_sysc_idle(struct work_struct *work)
1665{
1666 struct sysc *ddata;
1667
1668 ddata = container_of(work, struct sysc, idle_work.work);
1669
1670 if (pm_runtime_active(ddata->dev))
1671 pm_runtime_put_sync(ddata->dev);
1672}
1673
Tony Lindgrenc4bebea2018-04-16 10:24:54 -07001674static const struct of_device_id sysc_match_table[] = {
1675 { .compatible = "simple-bus", },
1676 { /* sentinel */ },
1677};
1678
Tony Lindgren0eecc632017-10-10 14:23:43 -07001679static int sysc_probe(struct platform_device *pdev)
1680{
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001681 struct ti_sysc_platform_data *pdata = dev_get_platdata(&pdev->dev);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001682 struct sysc *ddata;
1683 int error;
1684
1685 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
1686 if (!ddata)
1687 return -ENOMEM;
1688
1689 ddata->dev = &pdev->dev;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001690 platform_set_drvdata(pdev, ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001691
Tony Lindgren70a65242017-12-15 09:41:09 -08001692 error = sysc_init_match(ddata);
1693 if (error)
1694 return error;
1695
Tony Lindgren566a9b052017-12-15 09:41:19 -08001696 error = sysc_init_dts_quirks(ddata);
1697 if (error)
1698 goto unprepare;
1699
Tony Lindgren0eecc632017-10-10 14:23:43 -07001700 error = sysc_get_clocks(ddata);
1701 if (error)
1702 return error;
1703
1704 error = sysc_map_and_check_registers(ddata);
1705 if (error)
1706 goto unprepare;
1707
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08001708 error = sysc_init_sysc_mask(ddata);
1709 if (error)
1710 goto unprepare;
1711
1712 error = sysc_init_idlemodes(ddata);
1713 if (error)
1714 goto unprepare;
1715
1716 error = sysc_init_syss_mask(ddata);
1717 if (error)
1718 goto unprepare;
1719
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001720 error = sysc_init_pdata(ddata);
1721 if (error)
1722 goto unprepare;
1723
Tony Lindgren50622362018-04-16 10:20:27 -07001724 error = sysc_init_resets(ddata);
1725 if (error)
1726 return error;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001727
Tony Lindgren50622362018-04-16 10:20:27 -07001728 pm_runtime_enable(ddata->dev);
Tony Lindgren566a9b052017-12-15 09:41:19 -08001729 error = sysc_init_module(ddata);
1730 if (error)
1731 goto unprepare;
1732
Tony Lindgren0eecc632017-10-10 14:23:43 -07001733 error = pm_runtime_get_sync(ddata->dev);
1734 if (error < 0) {
1735 pm_runtime_put_noidle(ddata->dev);
1736 pm_runtime_disable(ddata->dev);
1737 goto unprepare;
1738 }
1739
Tony Lindgren0eecc632017-10-10 14:23:43 -07001740 sysc_show_registers(ddata);
1741
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001742 ddata->dev->type = &sysc_device_type;
Tony Lindgrenc4bebea2018-04-16 10:24:54 -07001743 error = of_platform_populate(ddata->dev->of_node, sysc_match_table,
1744 pdata ? pdata->auxdata : NULL,
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001745 ddata->dev);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001746 if (error)
1747 goto err;
1748
Tony Lindgren76f0f772018-02-23 08:28:45 -08001749 INIT_DELAYED_WORK(&ddata->idle_work, ti_sysc_idle);
1750
1751 /* At least earlycon won't survive without deferred idle */
1752 if (ddata->cfg.quirks & (SYSC_QUIRK_NO_IDLE_ON_INIT |
1753 SYSC_QUIRK_NO_RESET_ON_INIT)) {
1754 schedule_delayed_work(&ddata->idle_work, 3000);
1755 } else {
1756 pm_runtime_put(&pdev->dev);
1757 }
Tony Lindgren0eecc632017-10-10 14:23:43 -07001758
Tony Lindgren50622362018-04-16 10:20:27 -07001759 if (!of_get_available_child_count(ddata->dev->of_node))
1760 reset_control_assert(ddata->rsts);
1761
Tony Lindgren0eecc632017-10-10 14:23:43 -07001762 return 0;
1763
1764err:
Tony Lindgren0eecc632017-10-10 14:23:43 -07001765 pm_runtime_put_sync(&pdev->dev);
1766 pm_runtime_disable(&pdev->dev);
1767unprepare:
1768 sysc_unprepare(ddata);
1769
1770 return error;
1771}
1772
Tony Lindgren684be5a2017-10-13 10:48:40 -07001773static int sysc_remove(struct platform_device *pdev)
1774{
1775 struct sysc *ddata = platform_get_drvdata(pdev);
1776 int error;
1777
Tony Lindgren76f0f772018-02-23 08:28:45 -08001778 cancel_delayed_work_sync(&ddata->idle_work);
1779
Tony Lindgren684be5a2017-10-13 10:48:40 -07001780 error = pm_runtime_get_sync(ddata->dev);
1781 if (error < 0) {
1782 pm_runtime_put_noidle(ddata->dev);
1783 pm_runtime_disable(ddata->dev);
1784 goto unprepare;
1785 }
1786
1787 of_platform_depopulate(&pdev->dev);
1788
Tony Lindgren684be5a2017-10-13 10:48:40 -07001789 pm_runtime_put_sync(&pdev->dev);
1790 pm_runtime_disable(&pdev->dev);
Tony Lindgren50622362018-04-16 10:20:27 -07001791 reset_control_assert(ddata->rsts);
Tony Lindgren684be5a2017-10-13 10:48:40 -07001792
1793unprepare:
1794 sysc_unprepare(ddata);
1795
1796 return 0;
1797}
1798
Tony Lindgren0eecc632017-10-10 14:23:43 -07001799static const struct of_device_id sysc_match[] = {
Tony Lindgren70a65242017-12-15 09:41:09 -08001800 { .compatible = "ti,sysc-omap2", .data = &sysc_omap2, },
1801 { .compatible = "ti,sysc-omap2-timer", .data = &sysc_omap2_timer, },
1802 { .compatible = "ti,sysc-omap4", .data = &sysc_omap4, },
1803 { .compatible = "ti,sysc-omap4-timer", .data = &sysc_omap4_timer, },
1804 { .compatible = "ti,sysc-omap4-simple", .data = &sysc_omap4_simple, },
1805 { .compatible = "ti,sysc-omap3430-sr", .data = &sysc_34xx_sr, },
1806 { .compatible = "ti,sysc-omap3630-sr", .data = &sysc_36xx_sr, },
1807 { .compatible = "ti,sysc-omap4-sr", .data = &sysc_omap4_sr, },
1808 { .compatible = "ti,sysc-omap3-sham", .data = &sysc_omap3_sham, },
1809 { .compatible = "ti,sysc-omap-aes", .data = &sysc_omap3_aes, },
1810 { .compatible = "ti,sysc-mcasp", .data = &sysc_omap4_mcasp, },
1811 { .compatible = "ti,sysc-usb-host-fs",
1812 .data = &sysc_omap4_usb_host_fs, },
Faiz Abbas7f35e632018-07-09 22:18:38 +05301813 { .compatible = "ti,sysc-dra7-mcan", .data = &sysc_dra7_mcan, },
Tony Lindgren0eecc632017-10-10 14:23:43 -07001814 { },
1815};
1816MODULE_DEVICE_TABLE(of, sysc_match);
1817
1818static struct platform_driver sysc_driver = {
1819 .probe = sysc_probe,
Tony Lindgren684be5a2017-10-13 10:48:40 -07001820 .remove = sysc_remove,
Tony Lindgren0eecc632017-10-10 14:23:43 -07001821 .driver = {
1822 .name = "ti-sysc",
1823 .of_match_table = sysc_match,
1824 .pm = &sysc_pm_ops,
1825 },
1826};
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001827
1828static int __init sysc_init(void)
1829{
1830 bus_register_notifier(&platform_bus_type, &sysc_nb);
1831
1832 return platform_driver_register(&sysc_driver);
1833}
1834module_init(sysc_init);
1835
1836static void __exit sysc_exit(void)
1837{
1838 bus_unregister_notifier(&platform_bus_type, &sysc_nb);
1839 platform_driver_unregister(&sysc_driver);
1840}
1841module_exit(sysc_exit);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001842
1843MODULE_DESCRIPTION("TI sysc interconnect target driver");
1844MODULE_LICENSE("GPL v2");