blob: e12d1580f21d8ca05f2daa4401fe5d08b2500eaa [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>
22#include <linux/of_address.h>
23#include <linux/of_platform.h>
Tony Lindgren2c355ff2018-02-22 13:58:03 -080024#include <linux/slab.h>
25
Tony Lindgren70a65242017-12-15 09:41:09 -080026#include <linux/platform_data/ti-sysc.h>
27
28#include <dt-bindings/bus/ti-sysc.h>
Tony Lindgren0eecc632017-10-10 14:23:43 -070029
Tony Lindgren0eecc632017-10-10 14:23:43 -070030static const char * const reg_names[] = { "rev", "sysc", "syss", };
31
32enum sysc_clocks {
33 SYSC_FCK,
34 SYSC_ICK,
35 SYSC_MAX_CLOCKS,
36};
37
38static const char * const clock_names[] = { "fck", "ick", };
39
Tony Lindgrenc5a2de92017-12-15 09:41:23 -080040#define SYSC_IDLEMODE_MASK 3
41#define SYSC_CLOCKACTIVITY_MASK 3
42
Tony Lindgren0eecc632017-10-10 14:23:43 -070043/**
44 * struct sysc - TI sysc interconnect target module registers and capabilities
45 * @dev: struct device pointer
46 * @module_pa: physical address of the interconnect target module
47 * @module_size: size of the interconnect target module
48 * @module_va: virtual address of the interconnect target module
49 * @offsets: register offsets from module base
50 * @clocks: clocks used by the interconnect target module
51 * @legacy_mode: configured for legacy mode if set
Tony Lindgren70a65242017-12-15 09:41:09 -080052 * @cap: interconnect target module capabilities
53 * @cfg: interconnect target module configuration
Tony Lindgren566a9b052017-12-15 09:41:19 -080054 * @name: name if available
55 * @revision: interconnect target module revision
Tony Lindgren62020f22018-02-22 13:59:44 -080056 * @needs_resume: runtime resume needed on resume from suspend
Tony Lindgren0eecc632017-10-10 14:23:43 -070057 */
58struct sysc {
59 struct device *dev;
60 u64 module_pa;
61 u32 module_size;
62 void __iomem *module_va;
63 int offsets[SYSC_MAX_REGS];
64 struct clk *clocks[SYSC_MAX_CLOCKS];
65 const char *legacy_mode;
Tony Lindgren70a65242017-12-15 09:41:09 -080066 const struct sysc_capabilities *cap;
67 struct sysc_config cfg;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -080068 struct ti_sysc_cookie cookie;
Tony Lindgren566a9b052017-12-15 09:41:19 -080069 const char *name;
70 u32 revision;
Tony Lindgren62020f22018-02-22 13:59:44 -080071 bool enabled;
72 bool needs_resume;
Tony Lindgrena885f0f2018-02-22 14:03:48 -080073 bool child_needs_resume;
Tony Lindgren76f0f772018-02-23 08:28:45 -080074 struct delayed_work idle_work;
Tony Lindgren0eecc632017-10-10 14:23:43 -070075};
76
Tony Lindgren566a9b052017-12-15 09:41:19 -080077static u32 sysc_read(struct sysc *ddata, int offset)
78{
79 if (ddata->cfg.quirks & SYSC_QUIRK_16BIT) {
80 u32 val;
81
82 val = readw_relaxed(ddata->module_va + offset);
83 val |= (readw_relaxed(ddata->module_va + offset + 4) << 16);
84
85 return val;
86 }
87
88 return readl_relaxed(ddata->module_va + offset);
89}
90
Tony Lindgren0eecc632017-10-10 14:23:43 -070091static u32 sysc_read_revision(struct sysc *ddata)
92{
Tony Lindgren566a9b052017-12-15 09:41:19 -080093 int offset = ddata->offsets[SYSC_REVISION];
94
95 if (offset < 0)
96 return 0;
97
98 return sysc_read(ddata, offset);
Tony Lindgren0eecc632017-10-10 14:23:43 -070099}
100
101static int sysc_get_one_clock(struct sysc *ddata,
102 enum sysc_clocks index)
103{
104 const char *name;
105 int error;
106
107 switch (index) {
108 case SYSC_FCK:
109 break;
110 case SYSC_ICK:
111 break;
112 default:
113 return -EINVAL;
114 }
115 name = clock_names[index];
116
117 ddata->clocks[index] = devm_clk_get(ddata->dev, name);
118 if (IS_ERR(ddata->clocks[index])) {
119 if (PTR_ERR(ddata->clocks[index]) == -ENOENT)
120 return 0;
121
122 dev_err(ddata->dev, "clock get error for %s: %li\n",
123 name, PTR_ERR(ddata->clocks[index]));
124
125 return PTR_ERR(ddata->clocks[index]);
126 }
127
128 error = clk_prepare(ddata->clocks[index]);
129 if (error) {
130 dev_err(ddata->dev, "clock prepare error for %s: %i\n",
131 name, error);
132
133 return error;
134 }
135
136 return 0;
137}
138
139static int sysc_get_clocks(struct sysc *ddata)
140{
141 int i, error;
142
Tony Lindgren0eecc632017-10-10 14:23:43 -0700143 for (i = 0; i < SYSC_MAX_CLOCKS; i++) {
144 error = sysc_get_one_clock(ddata, i);
145 if (error && error != -ENOENT)
146 return error;
147 }
148
149 return 0;
150}
151
152/**
153 * sysc_parse_and_check_child_range - parses module IO region from ranges
154 * @ddata: device driver data
155 *
156 * In general we only need rev, syss, and sysc registers and not the whole
157 * module range. But we do want the offsets for these registers from the
158 * module base. This allows us to check them against the legacy hwmod
159 * platform data. Let's also check the ranges are configured properly.
160 */
161static int sysc_parse_and_check_child_range(struct sysc *ddata)
162{
163 struct device_node *np = ddata->dev->of_node;
164 const __be32 *ranges;
165 u32 nr_addr, nr_size;
166 int len, error;
167
168 ranges = of_get_property(np, "ranges", &len);
169 if (!ranges) {
170 dev_err(ddata->dev, "missing ranges for %pOF\n", np);
171
172 return -ENOENT;
173 }
174
175 len /= sizeof(*ranges);
176
177 if (len < 3) {
178 dev_err(ddata->dev, "incomplete ranges for %pOF\n", np);
179
180 return -EINVAL;
181 }
182
183 error = of_property_read_u32(np, "#address-cells", &nr_addr);
184 if (error)
185 return -ENOENT;
186
187 error = of_property_read_u32(np, "#size-cells", &nr_size);
188 if (error)
189 return -ENOENT;
190
191 if (nr_addr != 1 || nr_size != 1) {
192 dev_err(ddata->dev, "invalid ranges for %pOF\n", np);
193
194 return -EINVAL;
195 }
196
197 ranges++;
198 ddata->module_pa = of_translate_address(np, ranges++);
199 ddata->module_size = be32_to_cpup(ranges);
200
Tony Lindgren0eecc632017-10-10 14:23:43 -0700201 return 0;
202}
203
Tony Lindgren3bb37c82018-02-22 14:05:14 -0800204static struct device_node *stdout_path;
205
206static void sysc_init_stdout_path(struct sysc *ddata)
207{
208 struct device_node *np = NULL;
209 const char *uart;
210
211 if (IS_ERR(stdout_path))
212 return;
213
214 if (stdout_path)
215 return;
216
217 np = of_find_node_by_path("/chosen");
218 if (!np)
219 goto err;
220
221 uart = of_get_property(np, "stdout-path", NULL);
222 if (!uart)
223 goto err;
224
225 np = of_find_node_by_path(uart);
226 if (!np)
227 goto err;
228
229 stdout_path = np;
230
231 return;
232
233err:
234 stdout_path = ERR_PTR(-ENODEV);
235}
236
237static void sysc_check_quirk_stdout(struct sysc *ddata,
238 struct device_node *np)
239{
240 sysc_init_stdout_path(ddata);
241 if (np != stdout_path)
242 return;
243
244 ddata->cfg.quirks |= SYSC_QUIRK_NO_IDLE_ON_INIT |
245 SYSC_QUIRK_NO_RESET_ON_INIT;
246}
247
Tony Lindgren0eecc632017-10-10 14:23:43 -0700248/**
249 * sysc_check_one_child - check child configuration
250 * @ddata: device driver data
251 * @np: child device node
252 *
253 * Let's avoid messy situations where we have new interconnect target
254 * node but children have "ti,hwmods". These belong to the interconnect
255 * target node and are managed by this driver.
256 */
257static int sysc_check_one_child(struct sysc *ddata,
258 struct device_node *np)
259{
260 const char *name;
261
262 name = of_get_property(np, "ti,hwmods", NULL);
263 if (name)
264 dev_warn(ddata->dev, "really a child ti,hwmods property?");
265
Tony Lindgren3bb37c82018-02-22 14:05:14 -0800266 sysc_check_quirk_stdout(ddata, np);
267
Tony Lindgren0eecc632017-10-10 14:23:43 -0700268 return 0;
269}
270
271static int sysc_check_children(struct sysc *ddata)
272{
273 struct device_node *child;
274 int error;
275
276 for_each_child_of_node(ddata->dev->of_node, child) {
277 error = sysc_check_one_child(ddata, child);
278 if (error)
279 return error;
280 }
281
282 return 0;
283}
284
Tony Lindgrena7199e22017-12-15 09:41:14 -0800285/*
286 * So far only I2C uses 16-bit read access with clockactivity with revision
287 * in two registers with stride of 4. We can detect this based on the rev
288 * register size to configure things far enough to be able to properly read
289 * the revision register.
290 */
291static void sysc_check_quirk_16bit(struct sysc *ddata, struct resource *res)
292{
Tony Lindgrendd57ac12018-02-22 14:09:57 -0800293 if (resource_size(res) == 8)
Tony Lindgrena7199e22017-12-15 09:41:14 -0800294 ddata->cfg.quirks |= SYSC_QUIRK_16BIT | SYSC_QUIRK_USE_CLOCKACT;
Tony Lindgrena7199e22017-12-15 09:41:14 -0800295}
296
Tony Lindgren0eecc632017-10-10 14:23:43 -0700297/**
298 * sysc_parse_one - parses the interconnect target module registers
299 * @ddata: device driver data
300 * @reg: register to parse
301 */
302static int sysc_parse_one(struct sysc *ddata, enum sysc_registers reg)
303{
304 struct resource *res;
305 const char *name;
306
307 switch (reg) {
308 case SYSC_REVISION:
309 case SYSC_SYSCONFIG:
310 case SYSC_SYSSTATUS:
311 name = reg_names[reg];
312 break;
313 default:
314 return -EINVAL;
315 }
316
317 res = platform_get_resource_byname(to_platform_device(ddata->dev),
318 IORESOURCE_MEM, name);
319 if (!res) {
Tony Lindgren0eecc632017-10-10 14:23:43 -0700320 ddata->offsets[reg] = -ENODEV;
321
322 return 0;
323 }
324
325 ddata->offsets[reg] = res->start - ddata->module_pa;
Tony Lindgrena7199e22017-12-15 09:41:14 -0800326 if (reg == SYSC_REVISION)
327 sysc_check_quirk_16bit(ddata, res);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700328
329 return 0;
330}
331
332static int sysc_parse_registers(struct sysc *ddata)
333{
334 int i, error;
335
336 for (i = 0; i < SYSC_MAX_REGS; i++) {
337 error = sysc_parse_one(ddata, i);
338 if (error)
339 return error;
340 }
341
342 return 0;
343}
344
345/**
346 * sysc_check_registers - check for misconfigured register overlaps
347 * @ddata: device driver data
348 */
349static int sysc_check_registers(struct sysc *ddata)
350{
351 int i, j, nr_regs = 0, nr_matches = 0;
352
353 for (i = 0; i < SYSC_MAX_REGS; i++) {
354 if (ddata->offsets[i] < 0)
355 continue;
356
357 if (ddata->offsets[i] > (ddata->module_size - 4)) {
358 dev_err(ddata->dev, "register outside module range");
359
360 return -EINVAL;
361 }
362
363 for (j = 0; j < SYSC_MAX_REGS; j++) {
364 if (ddata->offsets[j] < 0)
365 continue;
366
367 if (ddata->offsets[i] == ddata->offsets[j])
368 nr_matches++;
369 }
370 nr_regs++;
371 }
372
373 if (nr_regs < 1) {
374 dev_err(ddata->dev, "missing registers\n");
375
376 return -EINVAL;
377 }
378
379 if (nr_matches > nr_regs) {
380 dev_err(ddata->dev, "overlapping registers: (%i/%i)",
381 nr_regs, nr_matches);
382
383 return -EINVAL;
384 }
385
386 return 0;
387}
388
389/**
390 * syc_ioremap - ioremap register space for the interconnect target module
391 * @ddata: deviec driver data
392 *
393 * Note that the interconnect target module registers can be anywhere
394 * within the first child device address space. For example, SGX has
395 * them at offset 0x1fc00 in the 32MB module address space. We just
396 * what we need around the interconnect target module registers.
397 */
398static int sysc_ioremap(struct sysc *ddata)
399{
400 u32 size = 0;
401
402 if (ddata->offsets[SYSC_SYSSTATUS] >= 0)
403 size = ddata->offsets[SYSC_SYSSTATUS];
404 else if (ddata->offsets[SYSC_SYSCONFIG] >= 0)
405 size = ddata->offsets[SYSC_SYSCONFIG];
406 else if (ddata->offsets[SYSC_REVISION] >= 0)
407 size = ddata->offsets[SYSC_REVISION];
408 else
409 return -EINVAL;
410
411 size &= 0xfff00;
412 size += SZ_256;
413
414 ddata->module_va = devm_ioremap(ddata->dev,
415 ddata->module_pa,
416 size);
417 if (!ddata->module_va)
418 return -EIO;
419
420 return 0;
421}
422
423/**
424 * sysc_map_and_check_registers - ioremap and check device registers
425 * @ddata: device driver data
426 */
427static int sysc_map_and_check_registers(struct sysc *ddata)
428{
429 int error;
430
431 error = sysc_parse_and_check_child_range(ddata);
432 if (error)
433 return error;
434
435 error = sysc_check_children(ddata);
436 if (error)
437 return error;
438
439 error = sysc_parse_registers(ddata);
440 if (error)
441 return error;
442
443 error = sysc_ioremap(ddata);
444 if (error)
445 return error;
446
447 error = sysc_check_registers(ddata);
448 if (error)
449 return error;
450
451 return 0;
452}
453
454/**
455 * sysc_show_rev - read and show interconnect target module revision
456 * @bufp: buffer to print the information to
457 * @ddata: device driver data
458 */
459static int sysc_show_rev(char *bufp, struct sysc *ddata)
460{
Tony Lindgren566a9b052017-12-15 09:41:19 -0800461 int len;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700462
463 if (ddata->offsets[SYSC_REVISION] < 0)
464 return sprintf(bufp, ":NA");
465
Tony Lindgren566a9b052017-12-15 09:41:19 -0800466 len = sprintf(bufp, ":%08x", ddata->revision);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700467
468 return len;
469}
470
471static int sysc_show_reg(struct sysc *ddata,
472 char *bufp, enum sysc_registers reg)
473{
474 if (ddata->offsets[reg] < 0)
475 return sprintf(bufp, ":NA");
476
477 return sprintf(bufp, ":%x", ddata->offsets[reg]);
478}
479
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800480static int sysc_show_name(char *bufp, struct sysc *ddata)
481{
482 if (!ddata->name)
483 return 0;
484
485 return sprintf(bufp, ":%s", ddata->name);
486}
487
Tony Lindgren0eecc632017-10-10 14:23:43 -0700488/**
489 * sysc_show_registers - show information about interconnect target module
490 * @ddata: device driver data
491 */
492static void sysc_show_registers(struct sysc *ddata)
493{
494 char buf[128];
495 char *bufp = buf;
496 int i;
497
498 for (i = 0; i < SYSC_MAX_REGS; i++)
499 bufp += sysc_show_reg(ddata, bufp, i);
500
501 bufp += sysc_show_rev(bufp, ddata);
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800502 bufp += sysc_show_name(bufp, ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700503
504 dev_dbg(ddata->dev, "%llx:%x%s\n",
505 ddata->module_pa, ddata->module_size,
506 buf);
507}
508
Arnd Bergmanna4a5d492017-10-13 11:25:32 +0200509static int __maybe_unused sysc_runtime_suspend(struct device *dev)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700510{
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800511 struct ti_sysc_platform_data *pdata;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700512 struct sysc *ddata;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800513 int error = 0, i;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700514
515 ddata = dev_get_drvdata(dev);
516
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800517 if (!ddata->enabled)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700518 return 0;
519
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800520 if (ddata->legacy_mode) {
521 pdata = dev_get_platdata(ddata->dev);
522 if (!pdata)
523 return 0;
524
525 if (!pdata->idle_module)
526 return -ENODEV;
527
528 error = pdata->idle_module(dev, &ddata->cookie);
529 if (error)
530 dev_err(dev, "%s: could not idle: %i\n",
531 __func__, error);
532
533 goto idled;
534 }
535
Tony Lindgren0eecc632017-10-10 14:23:43 -0700536 for (i = 0; i < SYSC_MAX_CLOCKS; i++) {
537 if (IS_ERR_OR_NULL(ddata->clocks[i]))
538 continue;
539 clk_disable(ddata->clocks[i]);
540 }
541
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800542idled:
543 ddata->enabled = false;
544
545 return error;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700546}
547
Arnd Bergmanna4a5d492017-10-13 11:25:32 +0200548static int __maybe_unused sysc_runtime_resume(struct device *dev)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700549{
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800550 struct ti_sysc_platform_data *pdata;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700551 struct sysc *ddata;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800552 int error = 0, i;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700553
554 ddata = dev_get_drvdata(dev);
555
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800556 if (ddata->enabled)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700557 return 0;
558
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800559 if (ddata->legacy_mode) {
560 pdata = dev_get_platdata(ddata->dev);
561 if (!pdata)
562 return 0;
563
564 if (!pdata->enable_module)
565 return -ENODEV;
566
567 error = pdata->enable_module(dev, &ddata->cookie);
568 if (error)
569 dev_err(dev, "%s: could not enable: %i\n",
570 __func__, error);
571
572 goto awake;
573 }
574
Tony Lindgren0eecc632017-10-10 14:23:43 -0700575 for (i = 0; i < SYSC_MAX_CLOCKS; i++) {
576 if (IS_ERR_OR_NULL(ddata->clocks[i]))
577 continue;
578 error = clk_enable(ddata->clocks[i]);
579 if (error)
580 return error;
581 }
582
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800583awake:
584 ddata->enabled = true;
585
586 return error;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700587}
588
Tony Lindgren62020f22018-02-22 13:59:44 -0800589#ifdef CONFIG_PM_SLEEP
590static int sysc_suspend(struct device *dev)
591{
592 struct sysc *ddata;
593
594 ddata = dev_get_drvdata(dev);
595
596 if (!ddata->enabled)
597 return 0;
598
599 ddata->needs_resume = true;
600
601 return sysc_runtime_suspend(dev);
602}
603
604static int sysc_resume(struct device *dev)
605{
606 struct sysc *ddata;
607
608 ddata = dev_get_drvdata(dev);
609 if (ddata->needs_resume) {
610 ddata->needs_resume = false;
611
612 return sysc_runtime_resume(dev);
613 }
614
615 return 0;
616}
617#endif
618
Tony Lindgren0eecc632017-10-10 14:23:43 -0700619static const struct dev_pm_ops sysc_pm_ops = {
Tony Lindgren62020f22018-02-22 13:59:44 -0800620 SET_SYSTEM_SLEEP_PM_OPS(sysc_suspend, sysc_resume)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700621 SET_RUNTIME_PM_OPS(sysc_runtime_suspend,
622 sysc_runtime_resume,
623 NULL)
624};
625
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800626/* Module revision register based quirks */
627struct sysc_revision_quirk {
628 const char *name;
629 u32 base;
630 int rev_offset;
631 int sysc_offset;
632 int syss_offset;
633 u32 revision;
634 u32 revision_mask;
635 u32 quirks;
636};
637
638#define SYSC_QUIRK(optname, optbase, optrev, optsysc, optsyss, \
639 optrev_val, optrevmask, optquirkmask) \
640 { \
641 .name = (optname), \
642 .base = (optbase), \
643 .rev_offset = (optrev), \
644 .sysc_offset = (optsysc), \
645 .syss_offset = (optsyss), \
646 .revision = (optrev_val), \
647 .revision_mask = (optrevmask), \
648 .quirks = (optquirkmask), \
649 }
650
651static const struct sysc_revision_quirk sysc_revision_quirks[] = {
652 /* These drivers need to be fixed to not use pm_runtime_irq_safe() */
653 SYSC_QUIRK("gpio", 0, 0, 0x10, 0x114, 0x50600801, 0xffffffff,
654 SYSC_QUIRK_LEGACY_IDLE),
655 SYSC_QUIRK("mmu", 0, 0, 0x10, 0x14, 0x00000020, 0xffffffff,
656 SYSC_QUIRK_LEGACY_IDLE),
657 SYSC_QUIRK("mmu", 0, 0, 0x10, 0x14, 0x00000030, 0xffffffff,
658 SYSC_QUIRK_LEGACY_IDLE),
659 SYSC_QUIRK("sham", 0, 0x100, 0x110, 0x114, 0x40000c03, 0xffffffff,
660 SYSC_QUIRK_LEGACY_IDLE),
661 SYSC_QUIRK("smartreflex", 0, -1, 0x24, -1, 0x00000000, 0xffffffff,
662 SYSC_QUIRK_LEGACY_IDLE),
663 SYSC_QUIRK("smartreflex", 0, -1, 0x38, -1, 0x00000000, 0xffffffff,
664 SYSC_QUIRK_LEGACY_IDLE),
665 SYSC_QUIRK("timer", 0, 0, 0x10, 0x14, 0x00000015, 0xffffffff,
666 SYSC_QUIRK_LEGACY_IDLE),
667 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x00000052, 0xffffffff,
668 SYSC_QUIRK_LEGACY_IDLE),
669};
670
671static void sysc_init_revision_quirks(struct sysc *ddata)
672{
673 const struct sysc_revision_quirk *q;
674 int i;
675
676 for (i = 0; i < ARRAY_SIZE(sysc_revision_quirks); i++) {
677 q = &sysc_revision_quirks[i];
678
679 if (q->base && q->base != ddata->module_pa)
680 continue;
681
682 if (q->rev_offset >= 0 &&
683 q->rev_offset != ddata->offsets[SYSC_REVISION])
684 continue;
685
686 if (q->sysc_offset >= 0 &&
687 q->sysc_offset != ddata->offsets[SYSC_SYSCONFIG])
688 continue;
689
690 if (q->syss_offset >= 0 &&
691 q->syss_offset != ddata->offsets[SYSC_SYSSTATUS])
692 continue;
693
694 if (q->revision == ddata->revision ||
695 (q->revision & q->revision_mask) ==
696 (ddata->revision & q->revision_mask)) {
697 ddata->name = q->name;
698 ddata->cfg.quirks |= q->quirks;
699 }
700 }
701}
702
Tony Lindgren566a9b052017-12-15 09:41:19 -0800703/* At this point the module is configured enough to read the revision */
704static int sysc_init_module(struct sysc *ddata)
705{
706 int error;
707
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800708 if (ddata->cfg.quirks & SYSC_QUIRK_NO_IDLE_ON_INIT) {
709 ddata->revision = sysc_read_revision(ddata);
710 goto rev_quirks;
711 }
712
Tony Lindgren566a9b052017-12-15 09:41:19 -0800713 error = pm_runtime_get_sync(ddata->dev);
714 if (error < 0) {
715 pm_runtime_put_noidle(ddata->dev);
716
717 return 0;
718 }
719 ddata->revision = sysc_read_revision(ddata);
720 pm_runtime_put_sync(ddata->dev);
721
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800722rev_quirks:
723 sysc_init_revision_quirks(ddata);
724
Tony Lindgren566a9b052017-12-15 09:41:19 -0800725 return 0;
726}
727
Tony Lindgrenc5a2de92017-12-15 09:41:23 -0800728static int sysc_init_sysc_mask(struct sysc *ddata)
729{
730 struct device_node *np = ddata->dev->of_node;
731 int error;
732 u32 val;
733
734 error = of_property_read_u32(np, "ti,sysc-mask", &val);
735 if (error)
736 return 0;
737
738 if (val)
739 ddata->cfg.sysc_val = val & ddata->cap->sysc_mask;
740 else
741 ddata->cfg.sysc_val = ddata->cap->sysc_mask;
742
743 return 0;
744}
745
746static int sysc_init_idlemode(struct sysc *ddata, u8 *idlemodes,
747 const char *name)
748{
749 struct device_node *np = ddata->dev->of_node;
750 struct property *prop;
751 const __be32 *p;
752 u32 val;
753
754 of_property_for_each_u32(np, name, prop, p, val) {
755 if (val >= SYSC_NR_IDLEMODES) {
756 dev_err(ddata->dev, "invalid idlemode: %i\n", val);
757 return -EINVAL;
758 }
759 *idlemodes |= (1 << val);
760 }
761
762 return 0;
763}
764
765static int sysc_init_idlemodes(struct sysc *ddata)
766{
767 int error;
768
769 error = sysc_init_idlemode(ddata, &ddata->cfg.midlemodes,
770 "ti,sysc-midle");
771 if (error)
772 return error;
773
774 error = sysc_init_idlemode(ddata, &ddata->cfg.sidlemodes,
775 "ti,sysc-sidle");
776 if (error)
777 return error;
778
779 return 0;
780}
781
782/*
783 * Only some devices on omap4 and later have SYSCONFIG reset done
784 * bit. We can detect this if there is no SYSSTATUS at all, or the
785 * SYSTATUS bit 0 is not used. Note that some SYSSTATUS registers
786 * have multiple bits for the child devices like OHCI and EHCI.
787 * Depends on SYSC being parsed first.
788 */
789static int sysc_init_syss_mask(struct sysc *ddata)
790{
791 struct device_node *np = ddata->dev->of_node;
792 int error;
793 u32 val;
794
795 error = of_property_read_u32(np, "ti,syss-mask", &val);
796 if (error) {
797 if ((ddata->cap->type == TI_SYSC_OMAP4 ||
798 ddata->cap->type == TI_SYSC_OMAP4_TIMER) &&
799 (ddata->cfg.sysc_val & SYSC_OMAP4_SOFTRESET))
800 ddata->cfg.quirks |= SYSC_QUIRK_RESET_STATUS;
801
802 return 0;
803 }
804
805 if (!(val & 1) && (ddata->cfg.sysc_val & SYSC_OMAP4_SOFTRESET))
806 ddata->cfg.quirks |= SYSC_QUIRK_RESET_STATUS;
807
808 ddata->cfg.syss_mask = val;
809
810 return 0;
811}
812
Tony Lindgren2c355ff2018-02-22 13:58:03 -0800813/*
Tony Lindgren8b2830ba2018-04-16 10:26:09 -0700814 * Many child device drivers need to have fck and opt clocks available
815 * to get the clock rate for device internal configuration etc.
Tony Lindgren2c355ff2018-02-22 13:58:03 -0800816 */
Tony Lindgren8b2830ba2018-04-16 10:26:09 -0700817static int sysc_child_add_named_clock(struct sysc *ddata,
818 struct device *child,
819 const char *name)
Tony Lindgren2c355ff2018-02-22 13:58:03 -0800820{
Tony Lindgren8b2830ba2018-04-16 10:26:09 -0700821 struct clk *clk;
Tony Lindgren2c355ff2018-02-22 13:58:03 -0800822 struct clk_lookup *l;
Tony Lindgren8b2830ba2018-04-16 10:26:09 -0700823 int error = 0;
Tony Lindgren2c355ff2018-02-22 13:58:03 -0800824
Tony Lindgren8b2830ba2018-04-16 10:26:09 -0700825 if (!name)
Tony Lindgren2c355ff2018-02-22 13:58:03 -0800826 return 0;
827
Tony Lindgren8b2830ba2018-04-16 10:26:09 -0700828 clk = clk_get(child, name);
829 if (!IS_ERR(clk)) {
830 clk_put(clk);
Tony Lindgren2c355ff2018-02-22 13:58:03 -0800831
832 return -EEXIST;
833 }
834
Tony Lindgren8b2830ba2018-04-16 10:26:09 -0700835 clk = clk_get(ddata->dev, name);
836 if (IS_ERR(clk))
837 return -ENODEV;
Tony Lindgren2c355ff2018-02-22 13:58:03 -0800838
Tony Lindgren8b2830ba2018-04-16 10:26:09 -0700839 l = clkdev_create(clk, name, dev_name(child));
840 if (!l)
841 error = -ENOMEM;
842
843 clk_put(clk);
844
845 return error;
Tony Lindgren2c355ff2018-02-22 13:58:03 -0800846}
847
848static struct device_type sysc_device_type = {
849};
850
851static struct sysc *sysc_child_to_parent(struct device *dev)
852{
853 struct device *parent = dev->parent;
854
855 if (!parent || parent->type != &sysc_device_type)
856 return NULL;
857
858 return dev_get_drvdata(parent);
859}
860
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800861static int __maybe_unused sysc_child_runtime_suspend(struct device *dev)
862{
863 struct sysc *ddata;
864 int error;
865
866 ddata = sysc_child_to_parent(dev);
867
868 error = pm_generic_runtime_suspend(dev);
869 if (error)
870 return error;
871
872 if (!ddata->enabled)
873 return 0;
874
875 return sysc_runtime_suspend(ddata->dev);
876}
877
878static int __maybe_unused sysc_child_runtime_resume(struct device *dev)
879{
880 struct sysc *ddata;
881 int error;
882
883 ddata = sysc_child_to_parent(dev);
884
885 if (!ddata->enabled) {
886 error = sysc_runtime_resume(ddata->dev);
887 if (error < 0)
888 dev_err(ddata->dev,
889 "%s error: %i\n", __func__, error);
890 }
891
892 return pm_generic_runtime_resume(dev);
893}
894
895#ifdef CONFIG_PM_SLEEP
896static int sysc_child_suspend_noirq(struct device *dev)
897{
898 struct sysc *ddata;
899 int error;
900
901 ddata = sysc_child_to_parent(dev);
902
903 error = pm_generic_suspend_noirq(dev);
904 if (error)
905 return error;
906
907 if (!pm_runtime_status_suspended(dev)) {
908 error = pm_generic_runtime_suspend(dev);
909 if (error)
910 return error;
911
912 error = sysc_runtime_suspend(ddata->dev);
913 if (error)
914 return error;
915
916 ddata->child_needs_resume = true;
917 }
918
919 return 0;
920}
921
922static int sysc_child_resume_noirq(struct device *dev)
923{
924 struct sysc *ddata;
925 int error;
926
927 ddata = sysc_child_to_parent(dev);
928
929 if (ddata->child_needs_resume) {
930 ddata->child_needs_resume = false;
931
932 error = sysc_runtime_resume(ddata->dev);
933 if (error)
934 dev_err(ddata->dev,
935 "%s runtime resume error: %i\n",
936 __func__, error);
937
938 error = pm_generic_runtime_resume(dev);
939 if (error)
940 dev_err(ddata->dev,
941 "%s generic runtime resume: %i\n",
942 __func__, error);
943 }
944
945 return pm_generic_resume_noirq(dev);
946}
947#endif
948
949struct dev_pm_domain sysc_child_pm_domain = {
950 .ops = {
951 SET_RUNTIME_PM_OPS(sysc_child_runtime_suspend,
952 sysc_child_runtime_resume,
953 NULL)
954 USE_PLATFORM_PM_SLEEP_OPS
955 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(sysc_child_suspend_noirq,
956 sysc_child_resume_noirq)
957 }
958};
959
960/**
961 * sysc_legacy_idle_quirk - handle children in omap_device compatible way
962 * @ddata: device driver data
963 * @child: child device driver
964 *
965 * Allow idle for child devices as done with _od_runtime_suspend().
966 * Otherwise many child devices will not idle because of the permanent
967 * parent usecount set in pm_runtime_irq_safe().
968 *
969 * Note that the long term solution is to just modify the child device
970 * drivers to not set pm_runtime_irq_safe() and then this can be just
971 * dropped.
972 */
973static void sysc_legacy_idle_quirk(struct sysc *ddata, struct device *child)
974{
975 if (!ddata->legacy_mode)
976 return;
977
978 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
979 dev_pm_domain_set(child, &sysc_child_pm_domain);
980}
981
Tony Lindgren2c355ff2018-02-22 13:58:03 -0800982static int sysc_notifier_call(struct notifier_block *nb,
983 unsigned long event, void *device)
984{
985 struct device *dev = device;
986 struct sysc *ddata;
987 int error;
988
989 ddata = sysc_child_to_parent(dev);
990 if (!ddata)
991 return NOTIFY_DONE;
992
993 switch (event) {
994 case BUS_NOTIFY_ADD_DEVICE:
Tony Lindgren8b2830ba2018-04-16 10:26:09 -0700995 error = sysc_child_add_named_clock(ddata, dev,
996 clock_names[SYSC_FCK]);
Tony Lindgren2c355ff2018-02-22 13:58:03 -0800997 if (error && error != -EEXIST)
998 dev_warn(ddata->dev, "could not add %s fck: %i\n",
999 dev_name(dev), error);
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001000 sysc_legacy_idle_quirk(ddata, dev);
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001001 break;
1002 default:
1003 break;
1004 }
1005
1006 return NOTIFY_DONE;
1007}
1008
1009static struct notifier_block sysc_nb = {
1010 .notifier_call = sysc_notifier_call,
1011};
1012
Tony Lindgren566a9b052017-12-15 09:41:19 -08001013/* Device tree configured quirks */
1014struct sysc_dts_quirk {
1015 const char *name;
1016 u32 mask;
1017};
1018
1019static const struct sysc_dts_quirk sysc_dts_quirks[] = {
1020 { .name = "ti,no-idle-on-init",
1021 .mask = SYSC_QUIRK_NO_IDLE_ON_INIT, },
1022 { .name = "ti,no-reset-on-init",
1023 .mask = SYSC_QUIRK_NO_RESET_ON_INIT, },
1024};
1025
1026static int sysc_init_dts_quirks(struct sysc *ddata)
1027{
1028 struct device_node *np = ddata->dev->of_node;
1029 const struct property *prop;
1030 int i, len, error;
1031 u32 val;
1032
1033 ddata->legacy_mode = of_get_property(np, "ti,hwmods", NULL);
1034
1035 for (i = 0; i < ARRAY_SIZE(sysc_dts_quirks); i++) {
1036 prop = of_get_property(np, sysc_dts_quirks[i].name, &len);
1037 if (!prop)
Tony Lindgrend39b6ea2018-02-15 09:18:55 -08001038 continue;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001039
1040 ddata->cfg.quirks |= sysc_dts_quirks[i].mask;
1041 }
1042
1043 error = of_property_read_u32(np, "ti,sysc-delay-us", &val);
1044 if (!error) {
1045 if (val > 255) {
1046 dev_warn(ddata->dev, "bad ti,sysc-delay-us: %i\n",
1047 val);
1048 }
1049
1050 ddata->cfg.srst_udelay = (u8)val;
1051 }
1052
1053 return 0;
1054}
1055
Tony Lindgren0eecc632017-10-10 14:23:43 -07001056static void sysc_unprepare(struct sysc *ddata)
1057{
1058 int i;
1059
1060 for (i = 0; i < SYSC_MAX_CLOCKS; i++) {
1061 if (!IS_ERR_OR_NULL(ddata->clocks[i]))
1062 clk_unprepare(ddata->clocks[i]);
1063 }
1064}
1065
Tony Lindgren70a65242017-12-15 09:41:09 -08001066/*
1067 * Common sysc register bits found on omap2, also known as type1
1068 */
1069static const struct sysc_regbits sysc_regbits_omap2 = {
1070 .dmadisable_shift = -ENODEV,
1071 .midle_shift = 12,
1072 .sidle_shift = 3,
1073 .clkact_shift = 8,
1074 .emufree_shift = 5,
1075 .enwkup_shift = 2,
1076 .srst_shift = 1,
1077 .autoidle_shift = 0,
1078};
1079
1080static const struct sysc_capabilities sysc_omap2 = {
1081 .type = TI_SYSC_OMAP2,
1082 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY | SYSC_OMAP2_EMUFREE |
1083 SYSC_OMAP2_ENAWAKEUP | SYSC_OMAP2_SOFTRESET |
1084 SYSC_OMAP2_AUTOIDLE,
1085 .regbits = &sysc_regbits_omap2,
1086};
1087
1088/* All omap2 and 3 timers, and timers 1, 2 & 10 on omap 4 and 5 */
1089static const struct sysc_capabilities sysc_omap2_timer = {
1090 .type = TI_SYSC_OMAP2_TIMER,
1091 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY | SYSC_OMAP2_EMUFREE |
1092 SYSC_OMAP2_ENAWAKEUP | SYSC_OMAP2_SOFTRESET |
1093 SYSC_OMAP2_AUTOIDLE,
1094 .regbits = &sysc_regbits_omap2,
1095 .mod_quirks = SYSC_QUIRK_USE_CLOCKACT,
1096};
1097
1098/*
1099 * SHAM2 (SHA1/MD5) sysc found on omap3, a variant of sysc_regbits_omap2
1100 * with different sidle position
1101 */
1102static const struct sysc_regbits sysc_regbits_omap3_sham = {
1103 .dmadisable_shift = -ENODEV,
1104 .midle_shift = -ENODEV,
1105 .sidle_shift = 4,
1106 .clkact_shift = -ENODEV,
1107 .enwkup_shift = -ENODEV,
1108 .srst_shift = 1,
1109 .autoidle_shift = 0,
1110 .emufree_shift = -ENODEV,
1111};
1112
1113static const struct sysc_capabilities sysc_omap3_sham = {
1114 .type = TI_SYSC_OMAP3_SHAM,
1115 .sysc_mask = SYSC_OMAP2_SOFTRESET | SYSC_OMAP2_AUTOIDLE,
1116 .regbits = &sysc_regbits_omap3_sham,
1117};
1118
1119/*
1120 * AES register bits found on omap3 and later, a variant of
1121 * sysc_regbits_omap2 with different sidle position
1122 */
1123static const struct sysc_regbits sysc_regbits_omap3_aes = {
1124 .dmadisable_shift = -ENODEV,
1125 .midle_shift = -ENODEV,
1126 .sidle_shift = 6,
1127 .clkact_shift = -ENODEV,
1128 .enwkup_shift = -ENODEV,
1129 .srst_shift = 1,
1130 .autoidle_shift = 0,
1131 .emufree_shift = -ENODEV,
1132};
1133
1134static const struct sysc_capabilities sysc_omap3_aes = {
1135 .type = TI_SYSC_OMAP3_AES,
1136 .sysc_mask = SYSC_OMAP2_SOFTRESET | SYSC_OMAP2_AUTOIDLE,
1137 .regbits = &sysc_regbits_omap3_aes,
1138};
1139
1140/*
1141 * Common sysc register bits found on omap4, also known as type2
1142 */
1143static const struct sysc_regbits sysc_regbits_omap4 = {
1144 .dmadisable_shift = 16,
1145 .midle_shift = 4,
1146 .sidle_shift = 2,
1147 .clkact_shift = -ENODEV,
1148 .enwkup_shift = -ENODEV,
1149 .emufree_shift = 1,
1150 .srst_shift = 0,
1151 .autoidle_shift = -ENODEV,
1152};
1153
1154static const struct sysc_capabilities sysc_omap4 = {
1155 .type = TI_SYSC_OMAP4,
1156 .sysc_mask = SYSC_OMAP4_DMADISABLE | SYSC_OMAP4_FREEEMU |
1157 SYSC_OMAP4_SOFTRESET,
1158 .regbits = &sysc_regbits_omap4,
1159};
1160
1161static const struct sysc_capabilities sysc_omap4_timer = {
1162 .type = TI_SYSC_OMAP4_TIMER,
1163 .sysc_mask = SYSC_OMAP4_DMADISABLE | SYSC_OMAP4_FREEEMU |
1164 SYSC_OMAP4_SOFTRESET,
1165 .regbits = &sysc_regbits_omap4,
1166};
1167
1168/*
1169 * Common sysc register bits found on omap4, also known as type3
1170 */
1171static const struct sysc_regbits sysc_regbits_omap4_simple = {
1172 .dmadisable_shift = -ENODEV,
1173 .midle_shift = 2,
1174 .sidle_shift = 0,
1175 .clkact_shift = -ENODEV,
1176 .enwkup_shift = -ENODEV,
1177 .srst_shift = -ENODEV,
1178 .emufree_shift = -ENODEV,
1179 .autoidle_shift = -ENODEV,
1180};
1181
1182static const struct sysc_capabilities sysc_omap4_simple = {
1183 .type = TI_SYSC_OMAP4_SIMPLE,
1184 .regbits = &sysc_regbits_omap4_simple,
1185};
1186
1187/*
1188 * SmartReflex sysc found on omap34xx
1189 */
1190static const struct sysc_regbits sysc_regbits_omap34xx_sr = {
1191 .dmadisable_shift = -ENODEV,
1192 .midle_shift = -ENODEV,
1193 .sidle_shift = -ENODEV,
1194 .clkact_shift = 20,
1195 .enwkup_shift = -ENODEV,
1196 .srst_shift = -ENODEV,
1197 .emufree_shift = -ENODEV,
1198 .autoidle_shift = -ENODEV,
1199};
1200
1201static const struct sysc_capabilities sysc_34xx_sr = {
1202 .type = TI_SYSC_OMAP34XX_SR,
1203 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY,
1204 .regbits = &sysc_regbits_omap34xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001205 .mod_quirks = SYSC_QUIRK_USE_CLOCKACT | SYSC_QUIRK_UNCACHED |
1206 SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08001207};
1208
1209/*
1210 * SmartReflex sysc found on omap36xx and later
1211 */
1212static const struct sysc_regbits sysc_regbits_omap36xx_sr = {
1213 .dmadisable_shift = -ENODEV,
1214 .midle_shift = -ENODEV,
1215 .sidle_shift = 24,
1216 .clkact_shift = -ENODEV,
1217 .enwkup_shift = 26,
1218 .srst_shift = -ENODEV,
1219 .emufree_shift = -ENODEV,
1220 .autoidle_shift = -ENODEV,
1221};
1222
1223static const struct sysc_capabilities sysc_36xx_sr = {
1224 .type = TI_SYSC_OMAP36XX_SR,
Tony Lindgren3267c082018-01-22 09:32:53 -08001225 .sysc_mask = SYSC_OMAP3_SR_ENAWAKEUP,
Tony Lindgren70a65242017-12-15 09:41:09 -08001226 .regbits = &sysc_regbits_omap36xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001227 .mod_quirks = SYSC_QUIRK_UNCACHED | SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08001228};
1229
1230static const struct sysc_capabilities sysc_omap4_sr = {
1231 .type = TI_SYSC_OMAP4_SR,
1232 .regbits = &sysc_regbits_omap36xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001233 .mod_quirks = SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08001234};
1235
1236/*
1237 * McASP register bits found on omap4 and later
1238 */
1239static const struct sysc_regbits sysc_regbits_omap4_mcasp = {
1240 .dmadisable_shift = -ENODEV,
1241 .midle_shift = -ENODEV,
1242 .sidle_shift = 0,
1243 .clkact_shift = -ENODEV,
1244 .enwkup_shift = -ENODEV,
1245 .srst_shift = -ENODEV,
1246 .emufree_shift = -ENODEV,
1247 .autoidle_shift = -ENODEV,
1248};
1249
1250static const struct sysc_capabilities sysc_omap4_mcasp = {
1251 .type = TI_SYSC_OMAP4_MCASP,
1252 .regbits = &sysc_regbits_omap4_mcasp,
1253};
1254
1255/*
1256 * FS USB host found on omap4 and later
1257 */
1258static const struct sysc_regbits sysc_regbits_omap4_usb_host_fs = {
1259 .dmadisable_shift = -ENODEV,
1260 .midle_shift = -ENODEV,
1261 .sidle_shift = 24,
1262 .clkact_shift = -ENODEV,
1263 .enwkup_shift = 26,
1264 .srst_shift = -ENODEV,
1265 .emufree_shift = -ENODEV,
1266 .autoidle_shift = -ENODEV,
1267};
1268
1269static const struct sysc_capabilities sysc_omap4_usb_host_fs = {
1270 .type = TI_SYSC_OMAP4_USB_HOST_FS,
1271 .sysc_mask = SYSC_OMAP2_ENAWAKEUP,
1272 .regbits = &sysc_regbits_omap4_usb_host_fs,
1273};
1274
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001275static int sysc_init_pdata(struct sysc *ddata)
1276{
1277 struct ti_sysc_platform_data *pdata = dev_get_platdata(ddata->dev);
1278 struct ti_sysc_module_data mdata;
1279 int error = 0;
1280
1281 if (!pdata || !ddata->legacy_mode)
1282 return 0;
1283
1284 mdata.name = ddata->legacy_mode;
1285 mdata.module_pa = ddata->module_pa;
1286 mdata.module_size = ddata->module_size;
1287 mdata.offsets = ddata->offsets;
1288 mdata.nr_offsets = SYSC_MAX_REGS;
1289 mdata.cap = ddata->cap;
1290 mdata.cfg = &ddata->cfg;
1291
1292 if (!pdata->init_module)
1293 return -ENODEV;
1294
1295 error = pdata->init_module(ddata->dev, &mdata, &ddata->cookie);
1296 if (error == -EEXIST)
1297 error = 0;
1298
1299 return error;
1300}
1301
Tony Lindgren70a65242017-12-15 09:41:09 -08001302static int sysc_init_match(struct sysc *ddata)
1303{
1304 const struct sysc_capabilities *cap;
1305
1306 cap = of_device_get_match_data(ddata->dev);
1307 if (!cap)
1308 return -EINVAL;
1309
1310 ddata->cap = cap;
1311 if (ddata->cap)
1312 ddata->cfg.quirks |= ddata->cap->mod_quirks;
1313
1314 return 0;
1315}
1316
Tony Lindgren76f0f772018-02-23 08:28:45 -08001317static void ti_sysc_idle(struct work_struct *work)
1318{
1319 struct sysc *ddata;
1320
1321 ddata = container_of(work, struct sysc, idle_work.work);
1322
1323 if (pm_runtime_active(ddata->dev))
1324 pm_runtime_put_sync(ddata->dev);
1325}
1326
Tony Lindgrenc4bebea2018-04-16 10:24:54 -07001327static const struct of_device_id sysc_match_table[] = {
1328 { .compatible = "simple-bus", },
1329 { /* sentinel */ },
1330};
1331
Tony Lindgren0eecc632017-10-10 14:23:43 -07001332static int sysc_probe(struct platform_device *pdev)
1333{
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001334 struct ti_sysc_platform_data *pdata = dev_get_platdata(&pdev->dev);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001335 struct sysc *ddata;
1336 int error;
1337
1338 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
1339 if (!ddata)
1340 return -ENOMEM;
1341
1342 ddata->dev = &pdev->dev;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001343 platform_set_drvdata(pdev, ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001344
Tony Lindgren70a65242017-12-15 09:41:09 -08001345 error = sysc_init_match(ddata);
1346 if (error)
1347 return error;
1348
Tony Lindgren566a9b052017-12-15 09:41:19 -08001349 error = sysc_init_dts_quirks(ddata);
1350 if (error)
1351 goto unprepare;
1352
Tony Lindgren0eecc632017-10-10 14:23:43 -07001353 error = sysc_get_clocks(ddata);
1354 if (error)
1355 return error;
1356
1357 error = sysc_map_and_check_registers(ddata);
1358 if (error)
1359 goto unprepare;
1360
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08001361 error = sysc_init_sysc_mask(ddata);
1362 if (error)
1363 goto unprepare;
1364
1365 error = sysc_init_idlemodes(ddata);
1366 if (error)
1367 goto unprepare;
1368
1369 error = sysc_init_syss_mask(ddata);
1370 if (error)
1371 goto unprepare;
1372
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001373 error = sysc_init_pdata(ddata);
1374 if (error)
1375 goto unprepare;
1376
Tony Lindgren0eecc632017-10-10 14:23:43 -07001377 pm_runtime_enable(ddata->dev);
Tony Lindgren566a9b052017-12-15 09:41:19 -08001378
1379 error = sysc_init_module(ddata);
1380 if (error)
1381 goto unprepare;
1382
Tony Lindgren0eecc632017-10-10 14:23:43 -07001383 error = pm_runtime_get_sync(ddata->dev);
1384 if (error < 0) {
1385 pm_runtime_put_noidle(ddata->dev);
1386 pm_runtime_disable(ddata->dev);
1387 goto unprepare;
1388 }
1389
Tony Lindgren0eecc632017-10-10 14:23:43 -07001390 sysc_show_registers(ddata);
1391
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001392 ddata->dev->type = &sysc_device_type;
Tony Lindgrenc4bebea2018-04-16 10:24:54 -07001393 error = of_platform_populate(ddata->dev->of_node, sysc_match_table,
1394 pdata ? pdata->auxdata : NULL,
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001395 ddata->dev);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001396 if (error)
1397 goto err;
1398
Tony Lindgren76f0f772018-02-23 08:28:45 -08001399 INIT_DELAYED_WORK(&ddata->idle_work, ti_sysc_idle);
1400
1401 /* At least earlycon won't survive without deferred idle */
1402 if (ddata->cfg.quirks & (SYSC_QUIRK_NO_IDLE_ON_INIT |
1403 SYSC_QUIRK_NO_RESET_ON_INIT)) {
1404 schedule_delayed_work(&ddata->idle_work, 3000);
1405 } else {
1406 pm_runtime_put(&pdev->dev);
1407 }
Tony Lindgren0eecc632017-10-10 14:23:43 -07001408
1409 return 0;
1410
1411err:
Tony Lindgren0eecc632017-10-10 14:23:43 -07001412 pm_runtime_put_sync(&pdev->dev);
1413 pm_runtime_disable(&pdev->dev);
1414unprepare:
1415 sysc_unprepare(ddata);
1416
1417 return error;
1418}
1419
Tony Lindgren684be5a2017-10-13 10:48:40 -07001420static int sysc_remove(struct platform_device *pdev)
1421{
1422 struct sysc *ddata = platform_get_drvdata(pdev);
1423 int error;
1424
Tony Lindgren76f0f772018-02-23 08:28:45 -08001425 cancel_delayed_work_sync(&ddata->idle_work);
1426
Tony Lindgren684be5a2017-10-13 10:48:40 -07001427 error = pm_runtime_get_sync(ddata->dev);
1428 if (error < 0) {
1429 pm_runtime_put_noidle(ddata->dev);
1430 pm_runtime_disable(ddata->dev);
1431 goto unprepare;
1432 }
1433
1434 of_platform_depopulate(&pdev->dev);
1435
Tony Lindgren684be5a2017-10-13 10:48:40 -07001436 pm_runtime_put_sync(&pdev->dev);
1437 pm_runtime_disable(&pdev->dev);
1438
1439unprepare:
1440 sysc_unprepare(ddata);
1441
1442 return 0;
1443}
1444
Tony Lindgren0eecc632017-10-10 14:23:43 -07001445static const struct of_device_id sysc_match[] = {
Tony Lindgren70a65242017-12-15 09:41:09 -08001446 { .compatible = "ti,sysc-omap2", .data = &sysc_omap2, },
1447 { .compatible = "ti,sysc-omap2-timer", .data = &sysc_omap2_timer, },
1448 { .compatible = "ti,sysc-omap4", .data = &sysc_omap4, },
1449 { .compatible = "ti,sysc-omap4-timer", .data = &sysc_omap4_timer, },
1450 { .compatible = "ti,sysc-omap4-simple", .data = &sysc_omap4_simple, },
1451 { .compatible = "ti,sysc-omap3430-sr", .data = &sysc_34xx_sr, },
1452 { .compatible = "ti,sysc-omap3630-sr", .data = &sysc_36xx_sr, },
1453 { .compatible = "ti,sysc-omap4-sr", .data = &sysc_omap4_sr, },
1454 { .compatible = "ti,sysc-omap3-sham", .data = &sysc_omap3_sham, },
1455 { .compatible = "ti,sysc-omap-aes", .data = &sysc_omap3_aes, },
1456 { .compatible = "ti,sysc-mcasp", .data = &sysc_omap4_mcasp, },
1457 { .compatible = "ti,sysc-usb-host-fs",
1458 .data = &sysc_omap4_usb_host_fs, },
Tony Lindgren0eecc632017-10-10 14:23:43 -07001459 { },
1460};
1461MODULE_DEVICE_TABLE(of, sysc_match);
1462
1463static struct platform_driver sysc_driver = {
1464 .probe = sysc_probe,
Tony Lindgren684be5a2017-10-13 10:48:40 -07001465 .remove = sysc_remove,
Tony Lindgren0eecc632017-10-10 14:23:43 -07001466 .driver = {
1467 .name = "ti-sysc",
1468 .of_match_table = sysc_match,
1469 .pm = &sysc_pm_ops,
1470 },
1471};
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001472
1473static int __init sysc_init(void)
1474{
1475 bus_register_notifier(&platform_bus_type, &sysc_nb);
1476
1477 return platform_driver_register(&sysc_driver);
1478}
1479module_init(sysc_init);
1480
1481static void __exit sysc_exit(void)
1482{
1483 bus_unregister_notifier(&platform_bus_type, &sysc_nb);
1484 platform_driver_unregister(&sysc_driver);
1485}
1486module_exit(sysc_exit);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001487
1488MODULE_DESCRIPTION("TI sysc interconnect target driver");
1489MODULE_LICENSE("GPL v2");