blob: 145dcc0cf48c309cc5d5c28ea235e682838c71f2 [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,
Tony Lindgren09dfe582018-04-16 10:25:52 -070035 SYSC_OPTFCK0,
36 SYSC_OPTFCK1,
37 SYSC_OPTFCK2,
38 SYSC_OPTFCK3,
39 SYSC_OPTFCK4,
40 SYSC_OPTFCK5,
41 SYSC_OPTFCK6,
42 SYSC_OPTFCK7,
Tony Lindgren0eecc632017-10-10 14:23:43 -070043 SYSC_MAX_CLOCKS,
44};
45
Tony Lindgren09dfe582018-04-16 10:25:52 -070046static const char * const clock_names[SYSC_ICK + 1] = { "fck", "ick", };
Tony Lindgren0eecc632017-10-10 14:23:43 -070047
Tony Lindgrenc5a2de92017-12-15 09:41:23 -080048#define SYSC_IDLEMODE_MASK 3
49#define SYSC_CLOCKACTIVITY_MASK 3
50
Tony Lindgren0eecc632017-10-10 14:23:43 -070051/**
52 * struct sysc - TI sysc interconnect target module registers and capabilities
53 * @dev: struct device pointer
54 * @module_pa: physical address of the interconnect target module
55 * @module_size: size of the interconnect target module
56 * @module_va: virtual address of the interconnect target module
57 * @offsets: register offsets from module base
58 * @clocks: clocks used by the interconnect target module
Tony Lindgren09dfe582018-04-16 10:25:52 -070059 * @clock_roles: clock role names for the found clocks
60 * @nr_clocks: number of clocks used by the interconnect target module
Tony Lindgren0eecc632017-10-10 14:23:43 -070061 * @legacy_mode: configured for legacy mode if set
Tony Lindgren70a65242017-12-15 09:41:09 -080062 * @cap: interconnect target module capabilities
63 * @cfg: interconnect target module configuration
Tony Lindgren566a9b052017-12-15 09:41:19 -080064 * @name: name if available
65 * @revision: interconnect target module revision
Tony Lindgren62020f22018-02-22 13:59:44 -080066 * @needs_resume: runtime resume needed on resume from suspend
Tony Lindgren0eecc632017-10-10 14:23:43 -070067 */
68struct sysc {
69 struct device *dev;
70 u64 module_pa;
71 u32 module_size;
72 void __iomem *module_va;
73 int offsets[SYSC_MAX_REGS];
Tony Lindgren09dfe582018-04-16 10:25:52 -070074 struct clk **clocks;
75 const char **clock_roles;
76 int nr_clocks;
Tony Lindgren0eecc632017-10-10 14:23:43 -070077 const char *legacy_mode;
Tony Lindgren70a65242017-12-15 09:41:09 -080078 const struct sysc_capabilities *cap;
79 struct sysc_config cfg;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -080080 struct ti_sysc_cookie cookie;
Tony Lindgren566a9b052017-12-15 09:41:19 -080081 const char *name;
82 u32 revision;
Tony Lindgren62020f22018-02-22 13:59:44 -080083 bool enabled;
84 bool needs_resume;
Tony Lindgrena885f0f2018-02-22 14:03:48 -080085 bool child_needs_resume;
Tony Lindgren76f0f772018-02-23 08:28:45 -080086 struct delayed_work idle_work;
Tony Lindgren0eecc632017-10-10 14:23:43 -070087};
88
Tony Lindgren566a9b052017-12-15 09:41:19 -080089static u32 sysc_read(struct sysc *ddata, int offset)
90{
91 if (ddata->cfg.quirks & SYSC_QUIRK_16BIT) {
92 u32 val;
93
94 val = readw_relaxed(ddata->module_va + offset);
95 val |= (readw_relaxed(ddata->module_va + offset + 4) << 16);
96
97 return val;
98 }
99
100 return readl_relaxed(ddata->module_va + offset);
101}
102
Tony Lindgren09dfe582018-04-16 10:25:52 -0700103static bool sysc_opt_clks_needed(struct sysc *ddata)
104{
105 return !!(ddata->cfg.quirks & SYSC_QUIRK_OPT_CLKS_NEEDED);
106}
107
Tony Lindgren0eecc632017-10-10 14:23:43 -0700108static u32 sysc_read_revision(struct sysc *ddata)
109{
Tony Lindgren566a9b052017-12-15 09:41:19 -0800110 int offset = ddata->offsets[SYSC_REVISION];
111
112 if (offset < 0)
113 return 0;
114
115 return sysc_read(ddata, offset);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700116}
117
Tony Lindgren09dfe582018-04-16 10:25:52 -0700118static int sysc_get_one_clock(struct sysc *ddata, const char *name)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700119{
Tony Lindgren09dfe582018-04-16 10:25:52 -0700120 int error, i, index = -ENODEV;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700121
Tony Lindgren09dfe582018-04-16 10:25:52 -0700122 if (!strncmp(clock_names[SYSC_FCK], name, 3))
123 index = SYSC_FCK;
124 else if (!strncmp(clock_names[SYSC_ICK], name, 3))
125 index = SYSC_ICK;
126
127 if (index < 0) {
128 for (i = SYSC_OPTFCK0; i < SYSC_MAX_CLOCKS; i++) {
129 if (!clock_names[i]) {
130 index = i;
131 break;
132 }
133 }
Tony Lindgren0eecc632017-10-10 14:23:43 -0700134 }
Tony Lindgren09dfe582018-04-16 10:25:52 -0700135
136 if (index < 0) {
137 dev_err(ddata->dev, "clock %s not added\n", name);
138 return index;
139 }
Tony Lindgren0eecc632017-10-10 14:23:43 -0700140
141 ddata->clocks[index] = devm_clk_get(ddata->dev, name);
142 if (IS_ERR(ddata->clocks[index])) {
143 if (PTR_ERR(ddata->clocks[index]) == -ENOENT)
144 return 0;
145
146 dev_err(ddata->dev, "clock get error for %s: %li\n",
147 name, PTR_ERR(ddata->clocks[index]));
148
149 return PTR_ERR(ddata->clocks[index]);
150 }
151
152 error = clk_prepare(ddata->clocks[index]);
153 if (error) {
154 dev_err(ddata->dev, "clock prepare error for %s: %i\n",
155 name, error);
156
157 return error;
158 }
159
160 return 0;
161}
162
163static int sysc_get_clocks(struct sysc *ddata)
164{
Tony Lindgren09dfe582018-04-16 10:25:52 -0700165 struct device_node *np = ddata->dev->of_node;
166 struct property *prop;
167 const char *name;
168 int nr_fck = 0, nr_ick = 0, i, error = 0;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700169
Tony Lindgren09dfe582018-04-16 10:25:52 -0700170 ddata->clock_roles = devm_kzalloc(ddata->dev,
171 sizeof(*ddata->clock_roles) *
172 SYSC_MAX_CLOCKS,
173 GFP_KERNEL);
174 if (!ddata->clock_roles)
175 return -ENOMEM;
176
177 of_property_for_each_string(np, "clock-names", prop, name) {
178 if (!strncmp(clock_names[SYSC_FCK], name, 3))
179 nr_fck++;
180 if (!strncmp(clock_names[SYSC_ICK], name, 3))
181 nr_ick++;
182 ddata->clock_roles[ddata->nr_clocks] = name;
183 ddata->nr_clocks++;
184 }
185
186 if (ddata->nr_clocks < 1)
187 return 0;
188
189 if (ddata->nr_clocks > SYSC_MAX_CLOCKS) {
190 dev_err(ddata->dev, "too many clocks for %pOF\n", np);
191
192 return -EINVAL;
193 }
194
195 if (nr_fck > 1 || nr_ick > 1) {
196 dev_err(ddata->dev, "max one fck and ick for %pOF\n", np);
197
198 return -EINVAL;
199 }
200
201 ddata->clocks = devm_kzalloc(ddata->dev,
202 sizeof(*ddata->clocks) * ddata->nr_clocks,
203 GFP_KERNEL);
204 if (!ddata->clocks)
205 return -ENOMEM;
206
207 for (i = 0; i < ddata->nr_clocks; i++) {
208 error = sysc_get_one_clock(ddata, ddata->clock_roles[i]);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700209 if (error && error != -ENOENT)
210 return error;
211 }
212
213 return 0;
214}
215
216/**
217 * sysc_parse_and_check_child_range - parses module IO region from ranges
218 * @ddata: device driver data
219 *
220 * In general we only need rev, syss, and sysc registers and not the whole
221 * module range. But we do want the offsets for these registers from the
222 * module base. This allows us to check them against the legacy hwmod
223 * platform data. Let's also check the ranges are configured properly.
224 */
225static int sysc_parse_and_check_child_range(struct sysc *ddata)
226{
227 struct device_node *np = ddata->dev->of_node;
228 const __be32 *ranges;
229 u32 nr_addr, nr_size;
230 int len, error;
231
232 ranges = of_get_property(np, "ranges", &len);
233 if (!ranges) {
234 dev_err(ddata->dev, "missing ranges for %pOF\n", np);
235
236 return -ENOENT;
237 }
238
239 len /= sizeof(*ranges);
240
241 if (len < 3) {
242 dev_err(ddata->dev, "incomplete ranges for %pOF\n", np);
243
244 return -EINVAL;
245 }
246
247 error = of_property_read_u32(np, "#address-cells", &nr_addr);
248 if (error)
249 return -ENOENT;
250
251 error = of_property_read_u32(np, "#size-cells", &nr_size);
252 if (error)
253 return -ENOENT;
254
255 if (nr_addr != 1 || nr_size != 1) {
256 dev_err(ddata->dev, "invalid ranges for %pOF\n", np);
257
258 return -EINVAL;
259 }
260
261 ranges++;
262 ddata->module_pa = of_translate_address(np, ranges++);
263 ddata->module_size = be32_to_cpup(ranges);
264
Tony Lindgren0eecc632017-10-10 14:23:43 -0700265 return 0;
266}
267
Tony Lindgren3bb37c82018-02-22 14:05:14 -0800268static struct device_node *stdout_path;
269
270static void sysc_init_stdout_path(struct sysc *ddata)
271{
272 struct device_node *np = NULL;
273 const char *uart;
274
275 if (IS_ERR(stdout_path))
276 return;
277
278 if (stdout_path)
279 return;
280
281 np = of_find_node_by_path("/chosen");
282 if (!np)
283 goto err;
284
285 uart = of_get_property(np, "stdout-path", NULL);
286 if (!uart)
287 goto err;
288
289 np = of_find_node_by_path(uart);
290 if (!np)
291 goto err;
292
293 stdout_path = np;
294
295 return;
296
297err:
298 stdout_path = ERR_PTR(-ENODEV);
299}
300
301static void sysc_check_quirk_stdout(struct sysc *ddata,
302 struct device_node *np)
303{
304 sysc_init_stdout_path(ddata);
305 if (np != stdout_path)
306 return;
307
308 ddata->cfg.quirks |= SYSC_QUIRK_NO_IDLE_ON_INIT |
309 SYSC_QUIRK_NO_RESET_ON_INIT;
310}
311
Tony Lindgren0eecc632017-10-10 14:23:43 -0700312/**
313 * sysc_check_one_child - check child configuration
314 * @ddata: device driver data
315 * @np: child device node
316 *
317 * Let's avoid messy situations where we have new interconnect target
318 * node but children have "ti,hwmods". These belong to the interconnect
319 * target node and are managed by this driver.
320 */
321static int sysc_check_one_child(struct sysc *ddata,
322 struct device_node *np)
323{
324 const char *name;
325
326 name = of_get_property(np, "ti,hwmods", NULL);
327 if (name)
328 dev_warn(ddata->dev, "really a child ti,hwmods property?");
329
Tony Lindgren3bb37c82018-02-22 14:05:14 -0800330 sysc_check_quirk_stdout(ddata, np);
331
Tony Lindgren0eecc632017-10-10 14:23:43 -0700332 return 0;
333}
334
335static int sysc_check_children(struct sysc *ddata)
336{
337 struct device_node *child;
338 int error;
339
340 for_each_child_of_node(ddata->dev->of_node, child) {
341 error = sysc_check_one_child(ddata, child);
342 if (error)
343 return error;
344 }
345
346 return 0;
347}
348
Tony Lindgrena7199e22017-12-15 09:41:14 -0800349/*
350 * So far only I2C uses 16-bit read access with clockactivity with revision
351 * in two registers with stride of 4. We can detect this based on the rev
352 * register size to configure things far enough to be able to properly read
353 * the revision register.
354 */
355static void sysc_check_quirk_16bit(struct sysc *ddata, struct resource *res)
356{
Tony Lindgrendd57ac12018-02-22 14:09:57 -0800357 if (resource_size(res) == 8)
Tony Lindgrena7199e22017-12-15 09:41:14 -0800358 ddata->cfg.quirks |= SYSC_QUIRK_16BIT | SYSC_QUIRK_USE_CLOCKACT;
Tony Lindgrena7199e22017-12-15 09:41:14 -0800359}
360
Tony Lindgren0eecc632017-10-10 14:23:43 -0700361/**
362 * sysc_parse_one - parses the interconnect target module registers
363 * @ddata: device driver data
364 * @reg: register to parse
365 */
366static int sysc_parse_one(struct sysc *ddata, enum sysc_registers reg)
367{
368 struct resource *res;
369 const char *name;
370
371 switch (reg) {
372 case SYSC_REVISION:
373 case SYSC_SYSCONFIG:
374 case SYSC_SYSSTATUS:
375 name = reg_names[reg];
376 break;
377 default:
378 return -EINVAL;
379 }
380
381 res = platform_get_resource_byname(to_platform_device(ddata->dev),
382 IORESOURCE_MEM, name);
383 if (!res) {
Tony Lindgren0eecc632017-10-10 14:23:43 -0700384 ddata->offsets[reg] = -ENODEV;
385
386 return 0;
387 }
388
389 ddata->offsets[reg] = res->start - ddata->module_pa;
Tony Lindgrena7199e22017-12-15 09:41:14 -0800390 if (reg == SYSC_REVISION)
391 sysc_check_quirk_16bit(ddata, res);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700392
393 return 0;
394}
395
396static int sysc_parse_registers(struct sysc *ddata)
397{
398 int i, error;
399
400 for (i = 0; i < SYSC_MAX_REGS; i++) {
401 error = sysc_parse_one(ddata, i);
402 if (error)
403 return error;
404 }
405
406 return 0;
407}
408
409/**
410 * sysc_check_registers - check for misconfigured register overlaps
411 * @ddata: device driver data
412 */
413static int sysc_check_registers(struct sysc *ddata)
414{
415 int i, j, nr_regs = 0, nr_matches = 0;
416
417 for (i = 0; i < SYSC_MAX_REGS; i++) {
418 if (ddata->offsets[i] < 0)
419 continue;
420
421 if (ddata->offsets[i] > (ddata->module_size - 4)) {
422 dev_err(ddata->dev, "register outside module range");
423
424 return -EINVAL;
425 }
426
427 for (j = 0; j < SYSC_MAX_REGS; j++) {
428 if (ddata->offsets[j] < 0)
429 continue;
430
431 if (ddata->offsets[i] == ddata->offsets[j])
432 nr_matches++;
433 }
434 nr_regs++;
435 }
436
437 if (nr_regs < 1) {
438 dev_err(ddata->dev, "missing registers\n");
439
440 return -EINVAL;
441 }
442
443 if (nr_matches > nr_regs) {
444 dev_err(ddata->dev, "overlapping registers: (%i/%i)",
445 nr_regs, nr_matches);
446
447 return -EINVAL;
448 }
449
450 return 0;
451}
452
453/**
454 * syc_ioremap - ioremap register space for the interconnect target module
455 * @ddata: deviec driver data
456 *
457 * Note that the interconnect target module registers can be anywhere
458 * within the first child device address space. For example, SGX has
459 * them at offset 0x1fc00 in the 32MB module address space. We just
460 * what we need around the interconnect target module registers.
461 */
462static int sysc_ioremap(struct sysc *ddata)
463{
464 u32 size = 0;
465
466 if (ddata->offsets[SYSC_SYSSTATUS] >= 0)
467 size = ddata->offsets[SYSC_SYSSTATUS];
468 else if (ddata->offsets[SYSC_SYSCONFIG] >= 0)
469 size = ddata->offsets[SYSC_SYSCONFIG];
470 else if (ddata->offsets[SYSC_REVISION] >= 0)
471 size = ddata->offsets[SYSC_REVISION];
472 else
473 return -EINVAL;
474
475 size &= 0xfff00;
476 size += SZ_256;
477
478 ddata->module_va = devm_ioremap(ddata->dev,
479 ddata->module_pa,
480 size);
481 if (!ddata->module_va)
482 return -EIO;
483
484 return 0;
485}
486
487/**
488 * sysc_map_and_check_registers - ioremap and check device registers
489 * @ddata: device driver data
490 */
491static int sysc_map_and_check_registers(struct sysc *ddata)
492{
493 int error;
494
495 error = sysc_parse_and_check_child_range(ddata);
496 if (error)
497 return error;
498
499 error = sysc_check_children(ddata);
500 if (error)
501 return error;
502
503 error = sysc_parse_registers(ddata);
504 if (error)
505 return error;
506
507 error = sysc_ioremap(ddata);
508 if (error)
509 return error;
510
511 error = sysc_check_registers(ddata);
512 if (error)
513 return error;
514
515 return 0;
516}
517
518/**
519 * sysc_show_rev - read and show interconnect target module revision
520 * @bufp: buffer to print the information to
521 * @ddata: device driver data
522 */
523static int sysc_show_rev(char *bufp, struct sysc *ddata)
524{
Tony Lindgren566a9b052017-12-15 09:41:19 -0800525 int len;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700526
527 if (ddata->offsets[SYSC_REVISION] < 0)
528 return sprintf(bufp, ":NA");
529
Tony Lindgren566a9b052017-12-15 09:41:19 -0800530 len = sprintf(bufp, ":%08x", ddata->revision);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700531
532 return len;
533}
534
535static int sysc_show_reg(struct sysc *ddata,
536 char *bufp, enum sysc_registers reg)
537{
538 if (ddata->offsets[reg] < 0)
539 return sprintf(bufp, ":NA");
540
541 return sprintf(bufp, ":%x", ddata->offsets[reg]);
542}
543
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800544static int sysc_show_name(char *bufp, struct sysc *ddata)
545{
546 if (!ddata->name)
547 return 0;
548
549 return sprintf(bufp, ":%s", ddata->name);
550}
551
Tony Lindgren0eecc632017-10-10 14:23:43 -0700552/**
553 * sysc_show_registers - show information about interconnect target module
554 * @ddata: device driver data
555 */
556static void sysc_show_registers(struct sysc *ddata)
557{
558 char buf[128];
559 char *bufp = buf;
560 int i;
561
562 for (i = 0; i < SYSC_MAX_REGS; i++)
563 bufp += sysc_show_reg(ddata, bufp, i);
564
565 bufp += sysc_show_rev(bufp, ddata);
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800566 bufp += sysc_show_name(bufp, ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700567
568 dev_dbg(ddata->dev, "%llx:%x%s\n",
569 ddata->module_pa, ddata->module_size,
570 buf);
571}
572
Arnd Bergmanna4a5d492017-10-13 11:25:32 +0200573static int __maybe_unused sysc_runtime_suspend(struct device *dev)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700574{
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800575 struct ti_sysc_platform_data *pdata;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700576 struct sysc *ddata;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800577 int error = 0, i;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700578
579 ddata = dev_get_drvdata(dev);
580
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800581 if (!ddata->enabled)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700582 return 0;
583
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800584 if (ddata->legacy_mode) {
585 pdata = dev_get_platdata(ddata->dev);
586 if (!pdata)
587 return 0;
588
589 if (!pdata->idle_module)
590 return -ENODEV;
591
592 error = pdata->idle_module(dev, &ddata->cookie);
593 if (error)
594 dev_err(dev, "%s: could not idle: %i\n",
595 __func__, error);
596
597 goto idled;
598 }
599
Tony Lindgren09dfe582018-04-16 10:25:52 -0700600 for (i = 0; i < ddata->nr_clocks; i++) {
Tony Lindgren0eecc632017-10-10 14:23:43 -0700601 if (IS_ERR_OR_NULL(ddata->clocks[i]))
602 continue;
Tony Lindgren09dfe582018-04-16 10:25:52 -0700603
604 if (i >= SYSC_OPTFCK0 && !sysc_opt_clks_needed(ddata))
605 break;
606
Tony Lindgren0eecc632017-10-10 14:23:43 -0700607 clk_disable(ddata->clocks[i]);
608 }
609
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800610idled:
611 ddata->enabled = false;
612
613 return error;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700614}
615
Arnd Bergmanna4a5d492017-10-13 11:25:32 +0200616static int __maybe_unused sysc_runtime_resume(struct device *dev)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700617{
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800618 struct ti_sysc_platform_data *pdata;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700619 struct sysc *ddata;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800620 int error = 0, i;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700621
622 ddata = dev_get_drvdata(dev);
623
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800624 if (ddata->enabled)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700625 return 0;
626
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800627 if (ddata->legacy_mode) {
628 pdata = dev_get_platdata(ddata->dev);
629 if (!pdata)
630 return 0;
631
632 if (!pdata->enable_module)
633 return -ENODEV;
634
635 error = pdata->enable_module(dev, &ddata->cookie);
636 if (error)
637 dev_err(dev, "%s: could not enable: %i\n",
638 __func__, error);
639
640 goto awake;
641 }
642
Tony Lindgren09dfe582018-04-16 10:25:52 -0700643 for (i = 0; i < ddata->nr_clocks; i++) {
Tony Lindgren0eecc632017-10-10 14:23:43 -0700644 if (IS_ERR_OR_NULL(ddata->clocks[i]))
645 continue;
Tony Lindgren09dfe582018-04-16 10:25:52 -0700646
647 if (i >= SYSC_OPTFCK0 && !sysc_opt_clks_needed(ddata))
648 break;
649
Tony Lindgren0eecc632017-10-10 14:23:43 -0700650 error = clk_enable(ddata->clocks[i]);
651 if (error)
652 return error;
653 }
654
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800655awake:
656 ddata->enabled = true;
657
658 return error;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700659}
660
Tony Lindgren62020f22018-02-22 13:59:44 -0800661#ifdef CONFIG_PM_SLEEP
662static int sysc_suspend(struct device *dev)
663{
664 struct sysc *ddata;
Tony Lindgrenef55f822018-04-16 10:27:15 -0700665 int error;
Tony Lindgren62020f22018-02-22 13:59:44 -0800666
667 ddata = dev_get_drvdata(dev);
668
Tony Lindgrene7420c22018-04-16 10:26:46 -0700669 if (ddata->cfg.quirks & (SYSC_QUIRK_RESOURCE_PROVIDER |
670 SYSC_QUIRK_LEGACY_IDLE))
671 return 0;
672
Tony Lindgren62020f22018-02-22 13:59:44 -0800673 if (!ddata->enabled)
674 return 0;
675
Tony Lindgrenef55f822018-04-16 10:27:15 -0700676 dev_dbg(ddata->dev, "%s %s\n", __func__,
677 ddata->name ? ddata->name : "");
678
679 error = pm_runtime_put_sync_suspend(dev);
680 if (error < 0) {
681 dev_warn(ddata->dev, "%s not idle %i %s\n",
682 __func__, error,
683 ddata->name ? ddata->name : "");
684
685 return 0;
686 }
687
Tony Lindgren62020f22018-02-22 13:59:44 -0800688 ddata->needs_resume = true;
689
Tony Lindgrenef55f822018-04-16 10:27:15 -0700690 return 0;
Tony Lindgren62020f22018-02-22 13:59:44 -0800691}
692
693static int sysc_resume(struct device *dev)
694{
695 struct sysc *ddata;
Tony Lindgrenef55f822018-04-16 10:27:15 -0700696 int error;
Tony Lindgren62020f22018-02-22 13:59:44 -0800697
698 ddata = dev_get_drvdata(dev);
Tony Lindgrene7420c22018-04-16 10:26:46 -0700699
700 if (ddata->cfg.quirks & (SYSC_QUIRK_RESOURCE_PROVIDER |
701 SYSC_QUIRK_LEGACY_IDLE))
702 return 0;
703
704 if (ddata->needs_resume) {
705 dev_dbg(ddata->dev, "%s %s\n", __func__,
706 ddata->name ? ddata->name : "");
707
Tony Lindgrenef55f822018-04-16 10:27:15 -0700708 error = pm_runtime_get_sync(dev);
709 if (error < 0) {
710 dev_err(ddata->dev, "%s error %i %s\n",
711 __func__, error,
712 ddata->name ? ddata->name : "");
Tony Lindgrene7420c22018-04-16 10:26:46 -0700713
Tony Lindgrenef55f822018-04-16 10:27:15 -0700714 return error;
715 }
716
717 ddata->needs_resume = false;
Tony Lindgrene7420c22018-04-16 10:26:46 -0700718 }
719
720 return 0;
721}
722
723static int sysc_noirq_suspend(struct device *dev)
724{
725 struct sysc *ddata;
726
727 ddata = dev_get_drvdata(dev);
728
729 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
730 return 0;
731
732 if (!(ddata->cfg.quirks & SYSC_QUIRK_RESOURCE_PROVIDER))
733 return 0;
734
735 if (!ddata->enabled)
736 return 0;
737
738 dev_dbg(ddata->dev, "%s %s\n", __func__,
739 ddata->name ? ddata->name : "");
740
741 ddata->needs_resume = true;
742
743 return sysc_runtime_suspend(dev);
744}
745
746static int sysc_noirq_resume(struct device *dev)
747{
748 struct sysc *ddata;
749
750 ddata = dev_get_drvdata(dev);
751
752 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
753 return 0;
754
755 if (!(ddata->cfg.quirks & SYSC_QUIRK_RESOURCE_PROVIDER))
756 return 0;
757
Tony Lindgren62020f22018-02-22 13:59:44 -0800758 if (ddata->needs_resume) {
Tony Lindgrenef55f822018-04-16 10:27:15 -0700759 dev_dbg(ddata->dev, "%s %s\n", __func__,
760 ddata->name ? ddata->name : "");
761
Tony Lindgren62020f22018-02-22 13:59:44 -0800762 ddata->needs_resume = false;
763
764 return sysc_runtime_resume(dev);
765 }
766
767 return 0;
768}
769#endif
770
Tony Lindgren0eecc632017-10-10 14:23:43 -0700771static const struct dev_pm_ops sysc_pm_ops = {
Tony Lindgren62020f22018-02-22 13:59:44 -0800772 SET_SYSTEM_SLEEP_PM_OPS(sysc_suspend, sysc_resume)
Tony Lindgrene7420c22018-04-16 10:26:46 -0700773 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(sysc_noirq_suspend, sysc_noirq_resume)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700774 SET_RUNTIME_PM_OPS(sysc_runtime_suspend,
775 sysc_runtime_resume,
776 NULL)
777};
778
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800779/* Module revision register based quirks */
780struct sysc_revision_quirk {
781 const char *name;
782 u32 base;
783 int rev_offset;
784 int sysc_offset;
785 int syss_offset;
786 u32 revision;
787 u32 revision_mask;
788 u32 quirks;
789};
790
791#define SYSC_QUIRK(optname, optbase, optrev, optsysc, optsyss, \
792 optrev_val, optrevmask, optquirkmask) \
793 { \
794 .name = (optname), \
795 .base = (optbase), \
796 .rev_offset = (optrev), \
797 .sysc_offset = (optsysc), \
798 .syss_offset = (optsyss), \
799 .revision = (optrev_val), \
800 .revision_mask = (optrevmask), \
801 .quirks = (optquirkmask), \
802 }
803
804static const struct sysc_revision_quirk sysc_revision_quirks[] = {
Tony Lindgrene7420c22018-04-16 10:26:46 -0700805 /* These need to use noirq_suspend */
806 SYSC_QUIRK("control", 0, 0, 0x10, -1, 0x40000900, 0xffffffff,
807 SYSC_QUIRK_RESOURCE_PROVIDER),
808 SYSC_QUIRK("i2c", 0, 0, 0x10, 0x90, 0x5040000a, 0xffffffff,
809 SYSC_QUIRK_RESOURCE_PROVIDER),
810 SYSC_QUIRK("mcspi", 0, 0, 0x10, -1, 0x40300a0b, 0xffffffff,
811 SYSC_QUIRK_RESOURCE_PROVIDER),
812 SYSC_QUIRK("prcm", 0, 0, -1, -1, 0x40000100, 0xffffffff,
813 SYSC_QUIRK_RESOURCE_PROVIDER),
814 SYSC_QUIRK("ocp2scp", 0, 0, 0x10, 0x14, 0x50060005, 0xffffffff,
815 SYSC_QUIRK_RESOURCE_PROVIDER),
816 SYSC_QUIRK("padconf", 0, 0, 0x10, -1, 0x4fff0800, 0xffffffff,
817 SYSC_QUIRK_RESOURCE_PROVIDER),
818 SYSC_QUIRK("scm", 0, 0, 0x10, -1, 0x40000900, 0xffffffff,
819 SYSC_QUIRK_RESOURCE_PROVIDER),
820 SYSC_QUIRK("scrm", 0, 0, -1, -1, 0x00000010, 0xffffffff,
821 SYSC_QUIRK_RESOURCE_PROVIDER),
822 SYSC_QUIRK("sdma", 0, 0, 0x2c, 0x28, 0x00010900, 0xffffffff,
823 SYSC_QUIRK_RESOURCE_PROVIDER),
824
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800825 /* These drivers need to be fixed to not use pm_runtime_irq_safe() */
826 SYSC_QUIRK("gpio", 0, 0, 0x10, 0x114, 0x50600801, 0xffffffff,
Tony Lindgren09dfe582018-04-16 10:25:52 -0700827 SYSC_QUIRK_LEGACY_IDLE | SYSC_QUIRK_OPT_CLKS_IN_RESET),
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800828 SYSC_QUIRK("mmu", 0, 0, 0x10, 0x14, 0x00000020, 0xffffffff,
829 SYSC_QUIRK_LEGACY_IDLE),
830 SYSC_QUIRK("mmu", 0, 0, 0x10, 0x14, 0x00000030, 0xffffffff,
831 SYSC_QUIRK_LEGACY_IDLE),
832 SYSC_QUIRK("sham", 0, 0x100, 0x110, 0x114, 0x40000c03, 0xffffffff,
833 SYSC_QUIRK_LEGACY_IDLE),
834 SYSC_QUIRK("smartreflex", 0, -1, 0x24, -1, 0x00000000, 0xffffffff,
835 SYSC_QUIRK_LEGACY_IDLE),
836 SYSC_QUIRK("smartreflex", 0, -1, 0x38, -1, 0x00000000, 0xffffffff,
837 SYSC_QUIRK_LEGACY_IDLE),
838 SYSC_QUIRK("timer", 0, 0, 0x10, 0x14, 0x00000015, 0xffffffff,
839 SYSC_QUIRK_LEGACY_IDLE),
840 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x00000052, 0xffffffff,
841 SYSC_QUIRK_LEGACY_IDLE),
842};
843
844static void sysc_init_revision_quirks(struct sysc *ddata)
845{
846 const struct sysc_revision_quirk *q;
847 int i;
848
849 for (i = 0; i < ARRAY_SIZE(sysc_revision_quirks); i++) {
850 q = &sysc_revision_quirks[i];
851
852 if (q->base && q->base != ddata->module_pa)
853 continue;
854
855 if (q->rev_offset >= 0 &&
856 q->rev_offset != ddata->offsets[SYSC_REVISION])
857 continue;
858
859 if (q->sysc_offset >= 0 &&
860 q->sysc_offset != ddata->offsets[SYSC_SYSCONFIG])
861 continue;
862
863 if (q->syss_offset >= 0 &&
864 q->syss_offset != ddata->offsets[SYSC_SYSSTATUS])
865 continue;
866
867 if (q->revision == ddata->revision ||
868 (q->revision & q->revision_mask) ==
869 (ddata->revision & q->revision_mask)) {
870 ddata->name = q->name;
871 ddata->cfg.quirks |= q->quirks;
872 }
873 }
874}
875
Tony Lindgren566a9b052017-12-15 09:41:19 -0800876/* At this point the module is configured enough to read the revision */
877static int sysc_init_module(struct sysc *ddata)
878{
879 int error;
880
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800881 if (ddata->cfg.quirks & SYSC_QUIRK_NO_IDLE_ON_INIT) {
882 ddata->revision = sysc_read_revision(ddata);
883 goto rev_quirks;
884 }
885
Tony Lindgren566a9b052017-12-15 09:41:19 -0800886 error = pm_runtime_get_sync(ddata->dev);
887 if (error < 0) {
888 pm_runtime_put_noidle(ddata->dev);
889
890 return 0;
891 }
892 ddata->revision = sysc_read_revision(ddata);
893 pm_runtime_put_sync(ddata->dev);
894
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800895rev_quirks:
896 sysc_init_revision_quirks(ddata);
897
Tony Lindgren566a9b052017-12-15 09:41:19 -0800898 return 0;
899}
900
Tony Lindgrenc5a2de92017-12-15 09:41:23 -0800901static int sysc_init_sysc_mask(struct sysc *ddata)
902{
903 struct device_node *np = ddata->dev->of_node;
904 int error;
905 u32 val;
906
907 error = of_property_read_u32(np, "ti,sysc-mask", &val);
908 if (error)
909 return 0;
910
911 if (val)
912 ddata->cfg.sysc_val = val & ddata->cap->sysc_mask;
913 else
914 ddata->cfg.sysc_val = ddata->cap->sysc_mask;
915
916 return 0;
917}
918
919static int sysc_init_idlemode(struct sysc *ddata, u8 *idlemodes,
920 const char *name)
921{
922 struct device_node *np = ddata->dev->of_node;
923 struct property *prop;
924 const __be32 *p;
925 u32 val;
926
927 of_property_for_each_u32(np, name, prop, p, val) {
928 if (val >= SYSC_NR_IDLEMODES) {
929 dev_err(ddata->dev, "invalid idlemode: %i\n", val);
930 return -EINVAL;
931 }
932 *idlemodes |= (1 << val);
933 }
934
935 return 0;
936}
937
938static int sysc_init_idlemodes(struct sysc *ddata)
939{
940 int error;
941
942 error = sysc_init_idlemode(ddata, &ddata->cfg.midlemodes,
943 "ti,sysc-midle");
944 if (error)
945 return error;
946
947 error = sysc_init_idlemode(ddata, &ddata->cfg.sidlemodes,
948 "ti,sysc-sidle");
949 if (error)
950 return error;
951
952 return 0;
953}
954
955/*
956 * Only some devices on omap4 and later have SYSCONFIG reset done
957 * bit. We can detect this if there is no SYSSTATUS at all, or the
958 * SYSTATUS bit 0 is not used. Note that some SYSSTATUS registers
959 * have multiple bits for the child devices like OHCI and EHCI.
960 * Depends on SYSC being parsed first.
961 */
962static int sysc_init_syss_mask(struct sysc *ddata)
963{
964 struct device_node *np = ddata->dev->of_node;
965 int error;
966 u32 val;
967
968 error = of_property_read_u32(np, "ti,syss-mask", &val);
969 if (error) {
970 if ((ddata->cap->type == TI_SYSC_OMAP4 ||
971 ddata->cap->type == TI_SYSC_OMAP4_TIMER) &&
972 (ddata->cfg.sysc_val & SYSC_OMAP4_SOFTRESET))
973 ddata->cfg.quirks |= SYSC_QUIRK_RESET_STATUS;
974
975 return 0;
976 }
977
978 if (!(val & 1) && (ddata->cfg.sysc_val & SYSC_OMAP4_SOFTRESET))
979 ddata->cfg.quirks |= SYSC_QUIRK_RESET_STATUS;
980
981 ddata->cfg.syss_mask = val;
982
983 return 0;
984}
985
Tony Lindgren2c355ff2018-02-22 13:58:03 -0800986/*
Tony Lindgren8b2830ba2018-04-16 10:26:09 -0700987 * Many child device drivers need to have fck and opt clocks available
988 * to get the clock rate for device internal configuration etc.
Tony Lindgren2c355ff2018-02-22 13:58:03 -0800989 */
Tony Lindgren8b2830ba2018-04-16 10:26:09 -0700990static int sysc_child_add_named_clock(struct sysc *ddata,
991 struct device *child,
992 const char *name)
Tony Lindgren2c355ff2018-02-22 13:58:03 -0800993{
Tony Lindgren8b2830ba2018-04-16 10:26:09 -0700994 struct clk *clk;
Tony Lindgren2c355ff2018-02-22 13:58:03 -0800995 struct clk_lookup *l;
Tony Lindgren8b2830ba2018-04-16 10:26:09 -0700996 int error = 0;
Tony Lindgren2c355ff2018-02-22 13:58:03 -0800997
Tony Lindgren8b2830ba2018-04-16 10:26:09 -0700998 if (!name)
Tony Lindgren2c355ff2018-02-22 13:58:03 -0800999 return 0;
1000
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001001 clk = clk_get(child, name);
1002 if (!IS_ERR(clk)) {
1003 clk_put(clk);
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001004
1005 return -EEXIST;
1006 }
1007
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001008 clk = clk_get(ddata->dev, name);
1009 if (IS_ERR(clk))
1010 return -ENODEV;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001011
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001012 l = clkdev_create(clk, name, dev_name(child));
1013 if (!l)
1014 error = -ENOMEM;
1015
1016 clk_put(clk);
1017
1018 return error;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001019}
1020
Tony Lindgren09dfe582018-04-16 10:25:52 -07001021static int sysc_child_add_clocks(struct sysc *ddata,
1022 struct device *child)
1023{
1024 int i, error;
1025
1026 for (i = 0; i < ddata->nr_clocks; i++) {
1027 error = sysc_child_add_named_clock(ddata,
1028 child,
1029 ddata->clock_roles[i]);
1030 if (error && error != -EEXIST) {
1031 dev_err(ddata->dev, "could not add child clock %s: %i\n",
1032 ddata->clock_roles[i], error);
1033
1034 return error;
1035 }
1036 }
1037
1038 return 0;
1039}
1040
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001041static struct device_type sysc_device_type = {
1042};
1043
1044static struct sysc *sysc_child_to_parent(struct device *dev)
1045{
1046 struct device *parent = dev->parent;
1047
1048 if (!parent || parent->type != &sysc_device_type)
1049 return NULL;
1050
1051 return dev_get_drvdata(parent);
1052}
1053
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001054static int __maybe_unused sysc_child_runtime_suspend(struct device *dev)
1055{
1056 struct sysc *ddata;
1057 int error;
1058
1059 ddata = sysc_child_to_parent(dev);
1060
1061 error = pm_generic_runtime_suspend(dev);
1062 if (error)
1063 return error;
1064
1065 if (!ddata->enabled)
1066 return 0;
1067
1068 return sysc_runtime_suspend(ddata->dev);
1069}
1070
1071static int __maybe_unused sysc_child_runtime_resume(struct device *dev)
1072{
1073 struct sysc *ddata;
1074 int error;
1075
1076 ddata = sysc_child_to_parent(dev);
1077
1078 if (!ddata->enabled) {
1079 error = sysc_runtime_resume(ddata->dev);
1080 if (error < 0)
1081 dev_err(ddata->dev,
1082 "%s error: %i\n", __func__, error);
1083 }
1084
1085 return pm_generic_runtime_resume(dev);
1086}
1087
1088#ifdef CONFIG_PM_SLEEP
1089static int sysc_child_suspend_noirq(struct device *dev)
1090{
1091 struct sysc *ddata;
1092 int error;
1093
1094 ddata = sysc_child_to_parent(dev);
1095
Tony Lindgrenef55f822018-04-16 10:27:15 -07001096 dev_dbg(ddata->dev, "%s %s\n", __func__,
1097 ddata->name ? ddata->name : "");
1098
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001099 error = pm_generic_suspend_noirq(dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001100 if (error) {
1101 dev_err(dev, "%s error at %i: %i\n",
1102 __func__, __LINE__, error);
1103
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001104 return error;
Tony Lindgrenef55f822018-04-16 10:27:15 -07001105 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001106
1107 if (!pm_runtime_status_suspended(dev)) {
1108 error = pm_generic_runtime_suspend(dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001109 if (error) {
1110 dev_err(dev, "%s error at %i: %i\n",
1111 __func__, __LINE__, error);
1112
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001113 return error;
Tony Lindgrenef55f822018-04-16 10:27:15 -07001114 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001115
1116 error = sysc_runtime_suspend(ddata->dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001117 if (error) {
1118 dev_err(dev, "%s error at %i: %i\n",
1119 __func__, __LINE__, error);
1120
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001121 return error;
Tony Lindgrenef55f822018-04-16 10:27:15 -07001122 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001123
1124 ddata->child_needs_resume = true;
1125 }
1126
1127 return 0;
1128}
1129
1130static int sysc_child_resume_noirq(struct device *dev)
1131{
1132 struct sysc *ddata;
1133 int error;
1134
1135 ddata = sysc_child_to_parent(dev);
1136
Tony Lindgrenef55f822018-04-16 10:27:15 -07001137 dev_dbg(ddata->dev, "%s %s\n", __func__,
1138 ddata->name ? ddata->name : "");
1139
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001140 if (ddata->child_needs_resume) {
1141 ddata->child_needs_resume = false;
1142
1143 error = sysc_runtime_resume(ddata->dev);
1144 if (error)
1145 dev_err(ddata->dev,
1146 "%s runtime resume error: %i\n",
1147 __func__, error);
1148
1149 error = pm_generic_runtime_resume(dev);
1150 if (error)
1151 dev_err(ddata->dev,
1152 "%s generic runtime resume: %i\n",
1153 __func__, error);
1154 }
1155
1156 return pm_generic_resume_noirq(dev);
1157}
1158#endif
1159
1160struct dev_pm_domain sysc_child_pm_domain = {
1161 .ops = {
1162 SET_RUNTIME_PM_OPS(sysc_child_runtime_suspend,
1163 sysc_child_runtime_resume,
1164 NULL)
1165 USE_PLATFORM_PM_SLEEP_OPS
1166 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(sysc_child_suspend_noirq,
1167 sysc_child_resume_noirq)
1168 }
1169};
1170
1171/**
1172 * sysc_legacy_idle_quirk - handle children in omap_device compatible way
1173 * @ddata: device driver data
1174 * @child: child device driver
1175 *
1176 * Allow idle for child devices as done with _od_runtime_suspend().
1177 * Otherwise many child devices will not idle because of the permanent
1178 * parent usecount set in pm_runtime_irq_safe().
1179 *
1180 * Note that the long term solution is to just modify the child device
1181 * drivers to not set pm_runtime_irq_safe() and then this can be just
1182 * dropped.
1183 */
1184static void sysc_legacy_idle_quirk(struct sysc *ddata, struct device *child)
1185{
1186 if (!ddata->legacy_mode)
1187 return;
1188
1189 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
1190 dev_pm_domain_set(child, &sysc_child_pm_domain);
1191}
1192
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001193static int sysc_notifier_call(struct notifier_block *nb,
1194 unsigned long event, void *device)
1195{
1196 struct device *dev = device;
1197 struct sysc *ddata;
1198 int error;
1199
1200 ddata = sysc_child_to_parent(dev);
1201 if (!ddata)
1202 return NOTIFY_DONE;
1203
1204 switch (event) {
1205 case BUS_NOTIFY_ADD_DEVICE:
Tony Lindgren09dfe582018-04-16 10:25:52 -07001206 error = sysc_child_add_clocks(ddata, dev);
1207 if (error)
1208 return error;
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001209 sysc_legacy_idle_quirk(ddata, dev);
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001210 break;
1211 default:
1212 break;
1213 }
1214
1215 return NOTIFY_DONE;
1216}
1217
1218static struct notifier_block sysc_nb = {
1219 .notifier_call = sysc_notifier_call,
1220};
1221
Tony Lindgren566a9b052017-12-15 09:41:19 -08001222/* Device tree configured quirks */
1223struct sysc_dts_quirk {
1224 const char *name;
1225 u32 mask;
1226};
1227
1228static const struct sysc_dts_quirk sysc_dts_quirks[] = {
1229 { .name = "ti,no-idle-on-init",
1230 .mask = SYSC_QUIRK_NO_IDLE_ON_INIT, },
1231 { .name = "ti,no-reset-on-init",
1232 .mask = SYSC_QUIRK_NO_RESET_ON_INIT, },
1233};
1234
1235static int sysc_init_dts_quirks(struct sysc *ddata)
1236{
1237 struct device_node *np = ddata->dev->of_node;
1238 const struct property *prop;
1239 int i, len, error;
1240 u32 val;
1241
1242 ddata->legacy_mode = of_get_property(np, "ti,hwmods", NULL);
1243
1244 for (i = 0; i < ARRAY_SIZE(sysc_dts_quirks); i++) {
1245 prop = of_get_property(np, sysc_dts_quirks[i].name, &len);
1246 if (!prop)
Tony Lindgrend39b6ea2018-02-15 09:18:55 -08001247 continue;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001248
1249 ddata->cfg.quirks |= sysc_dts_quirks[i].mask;
1250 }
1251
1252 error = of_property_read_u32(np, "ti,sysc-delay-us", &val);
1253 if (!error) {
1254 if (val > 255) {
1255 dev_warn(ddata->dev, "bad ti,sysc-delay-us: %i\n",
1256 val);
1257 }
1258
1259 ddata->cfg.srst_udelay = (u8)val;
1260 }
1261
1262 return 0;
1263}
1264
Tony Lindgren0eecc632017-10-10 14:23:43 -07001265static void sysc_unprepare(struct sysc *ddata)
1266{
1267 int i;
1268
1269 for (i = 0; i < SYSC_MAX_CLOCKS; i++) {
1270 if (!IS_ERR_OR_NULL(ddata->clocks[i]))
1271 clk_unprepare(ddata->clocks[i]);
1272 }
1273}
1274
Tony Lindgren70a65242017-12-15 09:41:09 -08001275/*
1276 * Common sysc register bits found on omap2, also known as type1
1277 */
1278static const struct sysc_regbits sysc_regbits_omap2 = {
1279 .dmadisable_shift = -ENODEV,
1280 .midle_shift = 12,
1281 .sidle_shift = 3,
1282 .clkact_shift = 8,
1283 .emufree_shift = 5,
1284 .enwkup_shift = 2,
1285 .srst_shift = 1,
1286 .autoidle_shift = 0,
1287};
1288
1289static const struct sysc_capabilities sysc_omap2 = {
1290 .type = TI_SYSC_OMAP2,
1291 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY | SYSC_OMAP2_EMUFREE |
1292 SYSC_OMAP2_ENAWAKEUP | SYSC_OMAP2_SOFTRESET |
1293 SYSC_OMAP2_AUTOIDLE,
1294 .regbits = &sysc_regbits_omap2,
1295};
1296
1297/* All omap2 and 3 timers, and timers 1, 2 & 10 on omap 4 and 5 */
1298static const struct sysc_capabilities sysc_omap2_timer = {
1299 .type = TI_SYSC_OMAP2_TIMER,
1300 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY | SYSC_OMAP2_EMUFREE |
1301 SYSC_OMAP2_ENAWAKEUP | SYSC_OMAP2_SOFTRESET |
1302 SYSC_OMAP2_AUTOIDLE,
1303 .regbits = &sysc_regbits_omap2,
1304 .mod_quirks = SYSC_QUIRK_USE_CLOCKACT,
1305};
1306
1307/*
1308 * SHAM2 (SHA1/MD5) sysc found on omap3, a variant of sysc_regbits_omap2
1309 * with different sidle position
1310 */
1311static const struct sysc_regbits sysc_regbits_omap3_sham = {
1312 .dmadisable_shift = -ENODEV,
1313 .midle_shift = -ENODEV,
1314 .sidle_shift = 4,
1315 .clkact_shift = -ENODEV,
1316 .enwkup_shift = -ENODEV,
1317 .srst_shift = 1,
1318 .autoidle_shift = 0,
1319 .emufree_shift = -ENODEV,
1320};
1321
1322static const struct sysc_capabilities sysc_omap3_sham = {
1323 .type = TI_SYSC_OMAP3_SHAM,
1324 .sysc_mask = SYSC_OMAP2_SOFTRESET | SYSC_OMAP2_AUTOIDLE,
1325 .regbits = &sysc_regbits_omap3_sham,
1326};
1327
1328/*
1329 * AES register bits found on omap3 and later, a variant of
1330 * sysc_regbits_omap2 with different sidle position
1331 */
1332static const struct sysc_regbits sysc_regbits_omap3_aes = {
1333 .dmadisable_shift = -ENODEV,
1334 .midle_shift = -ENODEV,
1335 .sidle_shift = 6,
1336 .clkact_shift = -ENODEV,
1337 .enwkup_shift = -ENODEV,
1338 .srst_shift = 1,
1339 .autoidle_shift = 0,
1340 .emufree_shift = -ENODEV,
1341};
1342
1343static const struct sysc_capabilities sysc_omap3_aes = {
1344 .type = TI_SYSC_OMAP3_AES,
1345 .sysc_mask = SYSC_OMAP2_SOFTRESET | SYSC_OMAP2_AUTOIDLE,
1346 .regbits = &sysc_regbits_omap3_aes,
1347};
1348
1349/*
1350 * Common sysc register bits found on omap4, also known as type2
1351 */
1352static const struct sysc_regbits sysc_regbits_omap4 = {
1353 .dmadisable_shift = 16,
1354 .midle_shift = 4,
1355 .sidle_shift = 2,
1356 .clkact_shift = -ENODEV,
1357 .enwkup_shift = -ENODEV,
1358 .emufree_shift = 1,
1359 .srst_shift = 0,
1360 .autoidle_shift = -ENODEV,
1361};
1362
1363static const struct sysc_capabilities sysc_omap4 = {
1364 .type = TI_SYSC_OMAP4,
1365 .sysc_mask = SYSC_OMAP4_DMADISABLE | SYSC_OMAP4_FREEEMU |
1366 SYSC_OMAP4_SOFTRESET,
1367 .regbits = &sysc_regbits_omap4,
1368};
1369
1370static const struct sysc_capabilities sysc_omap4_timer = {
1371 .type = TI_SYSC_OMAP4_TIMER,
1372 .sysc_mask = SYSC_OMAP4_DMADISABLE | SYSC_OMAP4_FREEEMU |
1373 SYSC_OMAP4_SOFTRESET,
1374 .regbits = &sysc_regbits_omap4,
1375};
1376
1377/*
1378 * Common sysc register bits found on omap4, also known as type3
1379 */
1380static const struct sysc_regbits sysc_regbits_omap4_simple = {
1381 .dmadisable_shift = -ENODEV,
1382 .midle_shift = 2,
1383 .sidle_shift = 0,
1384 .clkact_shift = -ENODEV,
1385 .enwkup_shift = -ENODEV,
1386 .srst_shift = -ENODEV,
1387 .emufree_shift = -ENODEV,
1388 .autoidle_shift = -ENODEV,
1389};
1390
1391static const struct sysc_capabilities sysc_omap4_simple = {
1392 .type = TI_SYSC_OMAP4_SIMPLE,
1393 .regbits = &sysc_regbits_omap4_simple,
1394};
1395
1396/*
1397 * SmartReflex sysc found on omap34xx
1398 */
1399static const struct sysc_regbits sysc_regbits_omap34xx_sr = {
1400 .dmadisable_shift = -ENODEV,
1401 .midle_shift = -ENODEV,
1402 .sidle_shift = -ENODEV,
1403 .clkact_shift = 20,
1404 .enwkup_shift = -ENODEV,
1405 .srst_shift = -ENODEV,
1406 .emufree_shift = -ENODEV,
1407 .autoidle_shift = -ENODEV,
1408};
1409
1410static const struct sysc_capabilities sysc_34xx_sr = {
1411 .type = TI_SYSC_OMAP34XX_SR,
1412 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY,
1413 .regbits = &sysc_regbits_omap34xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001414 .mod_quirks = SYSC_QUIRK_USE_CLOCKACT | SYSC_QUIRK_UNCACHED |
1415 SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08001416};
1417
1418/*
1419 * SmartReflex sysc found on omap36xx and later
1420 */
1421static const struct sysc_regbits sysc_regbits_omap36xx_sr = {
1422 .dmadisable_shift = -ENODEV,
1423 .midle_shift = -ENODEV,
1424 .sidle_shift = 24,
1425 .clkact_shift = -ENODEV,
1426 .enwkup_shift = 26,
1427 .srst_shift = -ENODEV,
1428 .emufree_shift = -ENODEV,
1429 .autoidle_shift = -ENODEV,
1430};
1431
1432static const struct sysc_capabilities sysc_36xx_sr = {
1433 .type = TI_SYSC_OMAP36XX_SR,
Tony Lindgren3267c082018-01-22 09:32:53 -08001434 .sysc_mask = SYSC_OMAP3_SR_ENAWAKEUP,
Tony Lindgren70a65242017-12-15 09:41:09 -08001435 .regbits = &sysc_regbits_omap36xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001436 .mod_quirks = SYSC_QUIRK_UNCACHED | SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08001437};
1438
1439static const struct sysc_capabilities sysc_omap4_sr = {
1440 .type = TI_SYSC_OMAP4_SR,
1441 .regbits = &sysc_regbits_omap36xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001442 .mod_quirks = SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08001443};
1444
1445/*
1446 * McASP register bits found on omap4 and later
1447 */
1448static const struct sysc_regbits sysc_regbits_omap4_mcasp = {
1449 .dmadisable_shift = -ENODEV,
1450 .midle_shift = -ENODEV,
1451 .sidle_shift = 0,
1452 .clkact_shift = -ENODEV,
1453 .enwkup_shift = -ENODEV,
1454 .srst_shift = -ENODEV,
1455 .emufree_shift = -ENODEV,
1456 .autoidle_shift = -ENODEV,
1457};
1458
1459static const struct sysc_capabilities sysc_omap4_mcasp = {
1460 .type = TI_SYSC_OMAP4_MCASP,
1461 .regbits = &sysc_regbits_omap4_mcasp,
1462};
1463
1464/*
1465 * FS USB host found on omap4 and later
1466 */
1467static const struct sysc_regbits sysc_regbits_omap4_usb_host_fs = {
1468 .dmadisable_shift = -ENODEV,
1469 .midle_shift = -ENODEV,
1470 .sidle_shift = 24,
1471 .clkact_shift = -ENODEV,
1472 .enwkup_shift = 26,
1473 .srst_shift = -ENODEV,
1474 .emufree_shift = -ENODEV,
1475 .autoidle_shift = -ENODEV,
1476};
1477
1478static const struct sysc_capabilities sysc_omap4_usb_host_fs = {
1479 .type = TI_SYSC_OMAP4_USB_HOST_FS,
1480 .sysc_mask = SYSC_OMAP2_ENAWAKEUP,
1481 .regbits = &sysc_regbits_omap4_usb_host_fs,
1482};
1483
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001484static int sysc_init_pdata(struct sysc *ddata)
1485{
1486 struct ti_sysc_platform_data *pdata = dev_get_platdata(ddata->dev);
1487 struct ti_sysc_module_data mdata;
1488 int error = 0;
1489
1490 if (!pdata || !ddata->legacy_mode)
1491 return 0;
1492
1493 mdata.name = ddata->legacy_mode;
1494 mdata.module_pa = ddata->module_pa;
1495 mdata.module_size = ddata->module_size;
1496 mdata.offsets = ddata->offsets;
1497 mdata.nr_offsets = SYSC_MAX_REGS;
1498 mdata.cap = ddata->cap;
1499 mdata.cfg = &ddata->cfg;
1500
1501 if (!pdata->init_module)
1502 return -ENODEV;
1503
1504 error = pdata->init_module(ddata->dev, &mdata, &ddata->cookie);
1505 if (error == -EEXIST)
1506 error = 0;
1507
1508 return error;
1509}
1510
Tony Lindgren70a65242017-12-15 09:41:09 -08001511static int sysc_init_match(struct sysc *ddata)
1512{
1513 const struct sysc_capabilities *cap;
1514
1515 cap = of_device_get_match_data(ddata->dev);
1516 if (!cap)
1517 return -EINVAL;
1518
1519 ddata->cap = cap;
1520 if (ddata->cap)
1521 ddata->cfg.quirks |= ddata->cap->mod_quirks;
1522
1523 return 0;
1524}
1525
Tony Lindgren76f0f772018-02-23 08:28:45 -08001526static void ti_sysc_idle(struct work_struct *work)
1527{
1528 struct sysc *ddata;
1529
1530 ddata = container_of(work, struct sysc, idle_work.work);
1531
1532 if (pm_runtime_active(ddata->dev))
1533 pm_runtime_put_sync(ddata->dev);
1534}
1535
Tony Lindgrenc4bebea2018-04-16 10:24:54 -07001536static const struct of_device_id sysc_match_table[] = {
1537 { .compatible = "simple-bus", },
1538 { /* sentinel */ },
1539};
1540
Tony Lindgren0eecc632017-10-10 14:23:43 -07001541static int sysc_probe(struct platform_device *pdev)
1542{
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001543 struct ti_sysc_platform_data *pdata = dev_get_platdata(&pdev->dev);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001544 struct sysc *ddata;
1545 int error;
1546
1547 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
1548 if (!ddata)
1549 return -ENOMEM;
1550
1551 ddata->dev = &pdev->dev;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001552 platform_set_drvdata(pdev, ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001553
Tony Lindgren70a65242017-12-15 09:41:09 -08001554 error = sysc_init_match(ddata);
1555 if (error)
1556 return error;
1557
Tony Lindgren566a9b052017-12-15 09:41:19 -08001558 error = sysc_init_dts_quirks(ddata);
1559 if (error)
1560 goto unprepare;
1561
Tony Lindgren0eecc632017-10-10 14:23:43 -07001562 error = sysc_get_clocks(ddata);
1563 if (error)
1564 return error;
1565
1566 error = sysc_map_and_check_registers(ddata);
1567 if (error)
1568 goto unprepare;
1569
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08001570 error = sysc_init_sysc_mask(ddata);
1571 if (error)
1572 goto unprepare;
1573
1574 error = sysc_init_idlemodes(ddata);
1575 if (error)
1576 goto unprepare;
1577
1578 error = sysc_init_syss_mask(ddata);
1579 if (error)
1580 goto unprepare;
1581
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001582 error = sysc_init_pdata(ddata);
1583 if (error)
1584 goto unprepare;
1585
Tony Lindgren0eecc632017-10-10 14:23:43 -07001586 pm_runtime_enable(ddata->dev);
Tony Lindgren566a9b052017-12-15 09:41:19 -08001587
1588 error = sysc_init_module(ddata);
1589 if (error)
1590 goto unprepare;
1591
Tony Lindgren0eecc632017-10-10 14:23:43 -07001592 error = pm_runtime_get_sync(ddata->dev);
1593 if (error < 0) {
1594 pm_runtime_put_noidle(ddata->dev);
1595 pm_runtime_disable(ddata->dev);
1596 goto unprepare;
1597 }
1598
Tony Lindgren0eecc632017-10-10 14:23:43 -07001599 sysc_show_registers(ddata);
1600
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001601 ddata->dev->type = &sysc_device_type;
Tony Lindgrenc4bebea2018-04-16 10:24:54 -07001602 error = of_platform_populate(ddata->dev->of_node, sysc_match_table,
1603 pdata ? pdata->auxdata : NULL,
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001604 ddata->dev);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001605 if (error)
1606 goto err;
1607
Tony Lindgren76f0f772018-02-23 08:28:45 -08001608 INIT_DELAYED_WORK(&ddata->idle_work, ti_sysc_idle);
1609
1610 /* At least earlycon won't survive without deferred idle */
1611 if (ddata->cfg.quirks & (SYSC_QUIRK_NO_IDLE_ON_INIT |
1612 SYSC_QUIRK_NO_RESET_ON_INIT)) {
1613 schedule_delayed_work(&ddata->idle_work, 3000);
1614 } else {
1615 pm_runtime_put(&pdev->dev);
1616 }
Tony Lindgren0eecc632017-10-10 14:23:43 -07001617
1618 return 0;
1619
1620err:
Tony Lindgren0eecc632017-10-10 14:23:43 -07001621 pm_runtime_put_sync(&pdev->dev);
1622 pm_runtime_disable(&pdev->dev);
1623unprepare:
1624 sysc_unprepare(ddata);
1625
1626 return error;
1627}
1628
Tony Lindgren684be5a2017-10-13 10:48:40 -07001629static int sysc_remove(struct platform_device *pdev)
1630{
1631 struct sysc *ddata = platform_get_drvdata(pdev);
1632 int error;
1633
Tony Lindgren76f0f772018-02-23 08:28:45 -08001634 cancel_delayed_work_sync(&ddata->idle_work);
1635
Tony Lindgren684be5a2017-10-13 10:48:40 -07001636 error = pm_runtime_get_sync(ddata->dev);
1637 if (error < 0) {
1638 pm_runtime_put_noidle(ddata->dev);
1639 pm_runtime_disable(ddata->dev);
1640 goto unprepare;
1641 }
1642
1643 of_platform_depopulate(&pdev->dev);
1644
Tony Lindgren684be5a2017-10-13 10:48:40 -07001645 pm_runtime_put_sync(&pdev->dev);
1646 pm_runtime_disable(&pdev->dev);
1647
1648unprepare:
1649 sysc_unprepare(ddata);
1650
1651 return 0;
1652}
1653
Tony Lindgren0eecc632017-10-10 14:23:43 -07001654static const struct of_device_id sysc_match[] = {
Tony Lindgren70a65242017-12-15 09:41:09 -08001655 { .compatible = "ti,sysc-omap2", .data = &sysc_omap2, },
1656 { .compatible = "ti,sysc-omap2-timer", .data = &sysc_omap2_timer, },
1657 { .compatible = "ti,sysc-omap4", .data = &sysc_omap4, },
1658 { .compatible = "ti,sysc-omap4-timer", .data = &sysc_omap4_timer, },
1659 { .compatible = "ti,sysc-omap4-simple", .data = &sysc_omap4_simple, },
1660 { .compatible = "ti,sysc-omap3430-sr", .data = &sysc_34xx_sr, },
1661 { .compatible = "ti,sysc-omap3630-sr", .data = &sysc_36xx_sr, },
1662 { .compatible = "ti,sysc-omap4-sr", .data = &sysc_omap4_sr, },
1663 { .compatible = "ti,sysc-omap3-sham", .data = &sysc_omap3_sham, },
1664 { .compatible = "ti,sysc-omap-aes", .data = &sysc_omap3_aes, },
1665 { .compatible = "ti,sysc-mcasp", .data = &sysc_omap4_mcasp, },
1666 { .compatible = "ti,sysc-usb-host-fs",
1667 .data = &sysc_omap4_usb_host_fs, },
Tony Lindgren0eecc632017-10-10 14:23:43 -07001668 { },
1669};
1670MODULE_DEVICE_TABLE(of, sysc_match);
1671
1672static struct platform_driver sysc_driver = {
1673 .probe = sysc_probe,
Tony Lindgren684be5a2017-10-13 10:48:40 -07001674 .remove = sysc_remove,
Tony Lindgren0eecc632017-10-10 14:23:43 -07001675 .driver = {
1676 .name = "ti-sysc",
1677 .of_match_table = sysc_match,
1678 .pm = &sysc_pm_ops,
1679 },
1680};
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001681
1682static int __init sysc_init(void)
1683{
1684 bus_register_notifier(&platform_bus_type, &sysc_nb);
1685
1686 return platform_driver_register(&sysc_driver);
1687}
1688module_init(sysc_init);
1689
1690static void __exit sysc_exit(void)
1691{
1692 bus_unregister_notifier(&platform_bus_type, &sysc_nb);
1693 platform_driver_unregister(&sysc_driver);
1694}
1695module_exit(sysc_exit);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001696
1697MODULE_DESCRIPTION("TI sysc interconnect target driver");
1698MODULE_LICENSE("GPL v2");