blob: 1f90b91dbfae6e6890559cb831f4e126ab639ac4 [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;
665
666 ddata = dev_get_drvdata(dev);
667
Tony Lindgrene7420c22018-04-16 10:26:46 -0700668 if (ddata->cfg.quirks & (SYSC_QUIRK_RESOURCE_PROVIDER |
669 SYSC_QUIRK_LEGACY_IDLE))
670 return 0;
671
Tony Lindgren62020f22018-02-22 13:59:44 -0800672 if (!ddata->enabled)
673 return 0;
674
675 ddata->needs_resume = true;
676
677 return sysc_runtime_suspend(dev);
678}
679
680static int sysc_resume(struct device *dev)
681{
682 struct sysc *ddata;
683
684 ddata = dev_get_drvdata(dev);
Tony Lindgrene7420c22018-04-16 10:26:46 -0700685
686 if (ddata->cfg.quirks & (SYSC_QUIRK_RESOURCE_PROVIDER |
687 SYSC_QUIRK_LEGACY_IDLE))
688 return 0;
689
690 if (ddata->needs_resume) {
691 dev_dbg(ddata->dev, "%s %s\n", __func__,
692 ddata->name ? ddata->name : "");
693
694 ddata->needs_resume = false;
695
696 return sysc_runtime_resume(dev);
697 }
698
699 return 0;
700}
701
702static int sysc_noirq_suspend(struct device *dev)
703{
704 struct sysc *ddata;
705
706 ddata = dev_get_drvdata(dev);
707
708 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
709 return 0;
710
711 if (!(ddata->cfg.quirks & SYSC_QUIRK_RESOURCE_PROVIDER))
712 return 0;
713
714 if (!ddata->enabled)
715 return 0;
716
717 dev_dbg(ddata->dev, "%s %s\n", __func__,
718 ddata->name ? ddata->name : "");
719
720 ddata->needs_resume = true;
721
722 return sysc_runtime_suspend(dev);
723}
724
725static int sysc_noirq_resume(struct device *dev)
726{
727 struct sysc *ddata;
728
729 ddata = dev_get_drvdata(dev);
730
731 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
732 return 0;
733
734 if (!(ddata->cfg.quirks & SYSC_QUIRK_RESOURCE_PROVIDER))
735 return 0;
736
Tony Lindgren62020f22018-02-22 13:59:44 -0800737 if (ddata->needs_resume) {
738 ddata->needs_resume = false;
739
740 return sysc_runtime_resume(dev);
741 }
742
743 return 0;
744}
745#endif
746
Tony Lindgren0eecc632017-10-10 14:23:43 -0700747static const struct dev_pm_ops sysc_pm_ops = {
Tony Lindgren62020f22018-02-22 13:59:44 -0800748 SET_SYSTEM_SLEEP_PM_OPS(sysc_suspend, sysc_resume)
Tony Lindgrene7420c22018-04-16 10:26:46 -0700749 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(sysc_noirq_suspend, sysc_noirq_resume)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700750 SET_RUNTIME_PM_OPS(sysc_runtime_suspend,
751 sysc_runtime_resume,
752 NULL)
753};
754
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800755/* Module revision register based quirks */
756struct sysc_revision_quirk {
757 const char *name;
758 u32 base;
759 int rev_offset;
760 int sysc_offset;
761 int syss_offset;
762 u32 revision;
763 u32 revision_mask;
764 u32 quirks;
765};
766
767#define SYSC_QUIRK(optname, optbase, optrev, optsysc, optsyss, \
768 optrev_val, optrevmask, optquirkmask) \
769 { \
770 .name = (optname), \
771 .base = (optbase), \
772 .rev_offset = (optrev), \
773 .sysc_offset = (optsysc), \
774 .syss_offset = (optsyss), \
775 .revision = (optrev_val), \
776 .revision_mask = (optrevmask), \
777 .quirks = (optquirkmask), \
778 }
779
780static const struct sysc_revision_quirk sysc_revision_quirks[] = {
Tony Lindgrene7420c22018-04-16 10:26:46 -0700781 /* These need to use noirq_suspend */
782 SYSC_QUIRK("control", 0, 0, 0x10, -1, 0x40000900, 0xffffffff,
783 SYSC_QUIRK_RESOURCE_PROVIDER),
784 SYSC_QUIRK("i2c", 0, 0, 0x10, 0x90, 0x5040000a, 0xffffffff,
785 SYSC_QUIRK_RESOURCE_PROVIDER),
786 SYSC_QUIRK("mcspi", 0, 0, 0x10, -1, 0x40300a0b, 0xffffffff,
787 SYSC_QUIRK_RESOURCE_PROVIDER),
788 SYSC_QUIRK("prcm", 0, 0, -1, -1, 0x40000100, 0xffffffff,
789 SYSC_QUIRK_RESOURCE_PROVIDER),
790 SYSC_QUIRK("ocp2scp", 0, 0, 0x10, 0x14, 0x50060005, 0xffffffff,
791 SYSC_QUIRK_RESOURCE_PROVIDER),
792 SYSC_QUIRK("padconf", 0, 0, 0x10, -1, 0x4fff0800, 0xffffffff,
793 SYSC_QUIRK_RESOURCE_PROVIDER),
794 SYSC_QUIRK("scm", 0, 0, 0x10, -1, 0x40000900, 0xffffffff,
795 SYSC_QUIRK_RESOURCE_PROVIDER),
796 SYSC_QUIRK("scrm", 0, 0, -1, -1, 0x00000010, 0xffffffff,
797 SYSC_QUIRK_RESOURCE_PROVIDER),
798 SYSC_QUIRK("sdma", 0, 0, 0x2c, 0x28, 0x00010900, 0xffffffff,
799 SYSC_QUIRK_RESOURCE_PROVIDER),
800
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800801 /* These drivers need to be fixed to not use pm_runtime_irq_safe() */
802 SYSC_QUIRK("gpio", 0, 0, 0x10, 0x114, 0x50600801, 0xffffffff,
Tony Lindgren09dfe582018-04-16 10:25:52 -0700803 SYSC_QUIRK_LEGACY_IDLE | SYSC_QUIRK_OPT_CLKS_IN_RESET),
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800804 SYSC_QUIRK("mmu", 0, 0, 0x10, 0x14, 0x00000020, 0xffffffff,
805 SYSC_QUIRK_LEGACY_IDLE),
806 SYSC_QUIRK("mmu", 0, 0, 0x10, 0x14, 0x00000030, 0xffffffff,
807 SYSC_QUIRK_LEGACY_IDLE),
808 SYSC_QUIRK("sham", 0, 0x100, 0x110, 0x114, 0x40000c03, 0xffffffff,
809 SYSC_QUIRK_LEGACY_IDLE),
810 SYSC_QUIRK("smartreflex", 0, -1, 0x24, -1, 0x00000000, 0xffffffff,
811 SYSC_QUIRK_LEGACY_IDLE),
812 SYSC_QUIRK("smartreflex", 0, -1, 0x38, -1, 0x00000000, 0xffffffff,
813 SYSC_QUIRK_LEGACY_IDLE),
814 SYSC_QUIRK("timer", 0, 0, 0x10, 0x14, 0x00000015, 0xffffffff,
815 SYSC_QUIRK_LEGACY_IDLE),
816 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x00000052, 0xffffffff,
817 SYSC_QUIRK_LEGACY_IDLE),
818};
819
820static void sysc_init_revision_quirks(struct sysc *ddata)
821{
822 const struct sysc_revision_quirk *q;
823 int i;
824
825 for (i = 0; i < ARRAY_SIZE(sysc_revision_quirks); i++) {
826 q = &sysc_revision_quirks[i];
827
828 if (q->base && q->base != ddata->module_pa)
829 continue;
830
831 if (q->rev_offset >= 0 &&
832 q->rev_offset != ddata->offsets[SYSC_REVISION])
833 continue;
834
835 if (q->sysc_offset >= 0 &&
836 q->sysc_offset != ddata->offsets[SYSC_SYSCONFIG])
837 continue;
838
839 if (q->syss_offset >= 0 &&
840 q->syss_offset != ddata->offsets[SYSC_SYSSTATUS])
841 continue;
842
843 if (q->revision == ddata->revision ||
844 (q->revision & q->revision_mask) ==
845 (ddata->revision & q->revision_mask)) {
846 ddata->name = q->name;
847 ddata->cfg.quirks |= q->quirks;
848 }
849 }
850}
851
Tony Lindgren566a9b052017-12-15 09:41:19 -0800852/* At this point the module is configured enough to read the revision */
853static int sysc_init_module(struct sysc *ddata)
854{
855 int error;
856
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800857 if (ddata->cfg.quirks & SYSC_QUIRK_NO_IDLE_ON_INIT) {
858 ddata->revision = sysc_read_revision(ddata);
859 goto rev_quirks;
860 }
861
Tony Lindgren566a9b052017-12-15 09:41:19 -0800862 error = pm_runtime_get_sync(ddata->dev);
863 if (error < 0) {
864 pm_runtime_put_noidle(ddata->dev);
865
866 return 0;
867 }
868 ddata->revision = sysc_read_revision(ddata);
869 pm_runtime_put_sync(ddata->dev);
870
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800871rev_quirks:
872 sysc_init_revision_quirks(ddata);
873
Tony Lindgren566a9b052017-12-15 09:41:19 -0800874 return 0;
875}
876
Tony Lindgrenc5a2de92017-12-15 09:41:23 -0800877static int sysc_init_sysc_mask(struct sysc *ddata)
878{
879 struct device_node *np = ddata->dev->of_node;
880 int error;
881 u32 val;
882
883 error = of_property_read_u32(np, "ti,sysc-mask", &val);
884 if (error)
885 return 0;
886
887 if (val)
888 ddata->cfg.sysc_val = val & ddata->cap->sysc_mask;
889 else
890 ddata->cfg.sysc_val = ddata->cap->sysc_mask;
891
892 return 0;
893}
894
895static int sysc_init_idlemode(struct sysc *ddata, u8 *idlemodes,
896 const char *name)
897{
898 struct device_node *np = ddata->dev->of_node;
899 struct property *prop;
900 const __be32 *p;
901 u32 val;
902
903 of_property_for_each_u32(np, name, prop, p, val) {
904 if (val >= SYSC_NR_IDLEMODES) {
905 dev_err(ddata->dev, "invalid idlemode: %i\n", val);
906 return -EINVAL;
907 }
908 *idlemodes |= (1 << val);
909 }
910
911 return 0;
912}
913
914static int sysc_init_idlemodes(struct sysc *ddata)
915{
916 int error;
917
918 error = sysc_init_idlemode(ddata, &ddata->cfg.midlemodes,
919 "ti,sysc-midle");
920 if (error)
921 return error;
922
923 error = sysc_init_idlemode(ddata, &ddata->cfg.sidlemodes,
924 "ti,sysc-sidle");
925 if (error)
926 return error;
927
928 return 0;
929}
930
931/*
932 * Only some devices on omap4 and later have SYSCONFIG reset done
933 * bit. We can detect this if there is no SYSSTATUS at all, or the
934 * SYSTATUS bit 0 is not used. Note that some SYSSTATUS registers
935 * have multiple bits for the child devices like OHCI and EHCI.
936 * Depends on SYSC being parsed first.
937 */
938static int sysc_init_syss_mask(struct sysc *ddata)
939{
940 struct device_node *np = ddata->dev->of_node;
941 int error;
942 u32 val;
943
944 error = of_property_read_u32(np, "ti,syss-mask", &val);
945 if (error) {
946 if ((ddata->cap->type == TI_SYSC_OMAP4 ||
947 ddata->cap->type == TI_SYSC_OMAP4_TIMER) &&
948 (ddata->cfg.sysc_val & SYSC_OMAP4_SOFTRESET))
949 ddata->cfg.quirks |= SYSC_QUIRK_RESET_STATUS;
950
951 return 0;
952 }
953
954 if (!(val & 1) && (ddata->cfg.sysc_val & SYSC_OMAP4_SOFTRESET))
955 ddata->cfg.quirks |= SYSC_QUIRK_RESET_STATUS;
956
957 ddata->cfg.syss_mask = val;
958
959 return 0;
960}
961
Tony Lindgren2c355ff2018-02-22 13:58:03 -0800962/*
Tony Lindgren8b2830ba2018-04-16 10:26:09 -0700963 * Many child device drivers need to have fck and opt clocks available
964 * to get the clock rate for device internal configuration etc.
Tony Lindgren2c355ff2018-02-22 13:58:03 -0800965 */
Tony Lindgren8b2830ba2018-04-16 10:26:09 -0700966static int sysc_child_add_named_clock(struct sysc *ddata,
967 struct device *child,
968 const char *name)
Tony Lindgren2c355ff2018-02-22 13:58:03 -0800969{
Tony Lindgren8b2830ba2018-04-16 10:26:09 -0700970 struct clk *clk;
Tony Lindgren2c355ff2018-02-22 13:58:03 -0800971 struct clk_lookup *l;
Tony Lindgren8b2830ba2018-04-16 10:26:09 -0700972 int error = 0;
Tony Lindgren2c355ff2018-02-22 13:58:03 -0800973
Tony Lindgren8b2830ba2018-04-16 10:26:09 -0700974 if (!name)
Tony Lindgren2c355ff2018-02-22 13:58:03 -0800975 return 0;
976
Tony Lindgren8b2830ba2018-04-16 10:26:09 -0700977 clk = clk_get(child, name);
978 if (!IS_ERR(clk)) {
979 clk_put(clk);
Tony Lindgren2c355ff2018-02-22 13:58:03 -0800980
981 return -EEXIST;
982 }
983
Tony Lindgren8b2830ba2018-04-16 10:26:09 -0700984 clk = clk_get(ddata->dev, name);
985 if (IS_ERR(clk))
986 return -ENODEV;
Tony Lindgren2c355ff2018-02-22 13:58:03 -0800987
Tony Lindgren8b2830ba2018-04-16 10:26:09 -0700988 l = clkdev_create(clk, name, dev_name(child));
989 if (!l)
990 error = -ENOMEM;
991
992 clk_put(clk);
993
994 return error;
Tony Lindgren2c355ff2018-02-22 13:58:03 -0800995}
996
Tony Lindgren09dfe582018-04-16 10:25:52 -0700997static int sysc_child_add_clocks(struct sysc *ddata,
998 struct device *child)
999{
1000 int i, error;
1001
1002 for (i = 0; i < ddata->nr_clocks; i++) {
1003 error = sysc_child_add_named_clock(ddata,
1004 child,
1005 ddata->clock_roles[i]);
1006 if (error && error != -EEXIST) {
1007 dev_err(ddata->dev, "could not add child clock %s: %i\n",
1008 ddata->clock_roles[i], error);
1009
1010 return error;
1011 }
1012 }
1013
1014 return 0;
1015}
1016
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001017static struct device_type sysc_device_type = {
1018};
1019
1020static struct sysc *sysc_child_to_parent(struct device *dev)
1021{
1022 struct device *parent = dev->parent;
1023
1024 if (!parent || parent->type != &sysc_device_type)
1025 return NULL;
1026
1027 return dev_get_drvdata(parent);
1028}
1029
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001030static int __maybe_unused sysc_child_runtime_suspend(struct device *dev)
1031{
1032 struct sysc *ddata;
1033 int error;
1034
1035 ddata = sysc_child_to_parent(dev);
1036
1037 error = pm_generic_runtime_suspend(dev);
1038 if (error)
1039 return error;
1040
1041 if (!ddata->enabled)
1042 return 0;
1043
1044 return sysc_runtime_suspend(ddata->dev);
1045}
1046
1047static int __maybe_unused sysc_child_runtime_resume(struct device *dev)
1048{
1049 struct sysc *ddata;
1050 int error;
1051
1052 ddata = sysc_child_to_parent(dev);
1053
1054 if (!ddata->enabled) {
1055 error = sysc_runtime_resume(ddata->dev);
1056 if (error < 0)
1057 dev_err(ddata->dev,
1058 "%s error: %i\n", __func__, error);
1059 }
1060
1061 return pm_generic_runtime_resume(dev);
1062}
1063
1064#ifdef CONFIG_PM_SLEEP
1065static int sysc_child_suspend_noirq(struct device *dev)
1066{
1067 struct sysc *ddata;
1068 int error;
1069
1070 ddata = sysc_child_to_parent(dev);
1071
1072 error = pm_generic_suspend_noirq(dev);
1073 if (error)
1074 return error;
1075
1076 if (!pm_runtime_status_suspended(dev)) {
1077 error = pm_generic_runtime_suspend(dev);
1078 if (error)
1079 return error;
1080
1081 error = sysc_runtime_suspend(ddata->dev);
1082 if (error)
1083 return error;
1084
1085 ddata->child_needs_resume = true;
1086 }
1087
1088 return 0;
1089}
1090
1091static int sysc_child_resume_noirq(struct device *dev)
1092{
1093 struct sysc *ddata;
1094 int error;
1095
1096 ddata = sysc_child_to_parent(dev);
1097
1098 if (ddata->child_needs_resume) {
1099 ddata->child_needs_resume = false;
1100
1101 error = sysc_runtime_resume(ddata->dev);
1102 if (error)
1103 dev_err(ddata->dev,
1104 "%s runtime resume error: %i\n",
1105 __func__, error);
1106
1107 error = pm_generic_runtime_resume(dev);
1108 if (error)
1109 dev_err(ddata->dev,
1110 "%s generic runtime resume: %i\n",
1111 __func__, error);
1112 }
1113
1114 return pm_generic_resume_noirq(dev);
1115}
1116#endif
1117
1118struct dev_pm_domain sysc_child_pm_domain = {
1119 .ops = {
1120 SET_RUNTIME_PM_OPS(sysc_child_runtime_suspend,
1121 sysc_child_runtime_resume,
1122 NULL)
1123 USE_PLATFORM_PM_SLEEP_OPS
1124 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(sysc_child_suspend_noirq,
1125 sysc_child_resume_noirq)
1126 }
1127};
1128
1129/**
1130 * sysc_legacy_idle_quirk - handle children in omap_device compatible way
1131 * @ddata: device driver data
1132 * @child: child device driver
1133 *
1134 * Allow idle for child devices as done with _od_runtime_suspend().
1135 * Otherwise many child devices will not idle because of the permanent
1136 * parent usecount set in pm_runtime_irq_safe().
1137 *
1138 * Note that the long term solution is to just modify the child device
1139 * drivers to not set pm_runtime_irq_safe() and then this can be just
1140 * dropped.
1141 */
1142static void sysc_legacy_idle_quirk(struct sysc *ddata, struct device *child)
1143{
1144 if (!ddata->legacy_mode)
1145 return;
1146
1147 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
1148 dev_pm_domain_set(child, &sysc_child_pm_domain);
1149}
1150
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001151static int sysc_notifier_call(struct notifier_block *nb,
1152 unsigned long event, void *device)
1153{
1154 struct device *dev = device;
1155 struct sysc *ddata;
1156 int error;
1157
1158 ddata = sysc_child_to_parent(dev);
1159 if (!ddata)
1160 return NOTIFY_DONE;
1161
1162 switch (event) {
1163 case BUS_NOTIFY_ADD_DEVICE:
Tony Lindgren09dfe582018-04-16 10:25:52 -07001164 error = sysc_child_add_clocks(ddata, dev);
1165 if (error)
1166 return error;
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001167 sysc_legacy_idle_quirk(ddata, dev);
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001168 break;
1169 default:
1170 break;
1171 }
1172
1173 return NOTIFY_DONE;
1174}
1175
1176static struct notifier_block sysc_nb = {
1177 .notifier_call = sysc_notifier_call,
1178};
1179
Tony Lindgren566a9b052017-12-15 09:41:19 -08001180/* Device tree configured quirks */
1181struct sysc_dts_quirk {
1182 const char *name;
1183 u32 mask;
1184};
1185
1186static const struct sysc_dts_quirk sysc_dts_quirks[] = {
1187 { .name = "ti,no-idle-on-init",
1188 .mask = SYSC_QUIRK_NO_IDLE_ON_INIT, },
1189 { .name = "ti,no-reset-on-init",
1190 .mask = SYSC_QUIRK_NO_RESET_ON_INIT, },
1191};
1192
1193static int sysc_init_dts_quirks(struct sysc *ddata)
1194{
1195 struct device_node *np = ddata->dev->of_node;
1196 const struct property *prop;
1197 int i, len, error;
1198 u32 val;
1199
1200 ddata->legacy_mode = of_get_property(np, "ti,hwmods", NULL);
1201
1202 for (i = 0; i < ARRAY_SIZE(sysc_dts_quirks); i++) {
1203 prop = of_get_property(np, sysc_dts_quirks[i].name, &len);
1204 if (!prop)
Tony Lindgrend39b6ea2018-02-15 09:18:55 -08001205 continue;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001206
1207 ddata->cfg.quirks |= sysc_dts_quirks[i].mask;
1208 }
1209
1210 error = of_property_read_u32(np, "ti,sysc-delay-us", &val);
1211 if (!error) {
1212 if (val > 255) {
1213 dev_warn(ddata->dev, "bad ti,sysc-delay-us: %i\n",
1214 val);
1215 }
1216
1217 ddata->cfg.srst_udelay = (u8)val;
1218 }
1219
1220 return 0;
1221}
1222
Tony Lindgren0eecc632017-10-10 14:23:43 -07001223static void sysc_unprepare(struct sysc *ddata)
1224{
1225 int i;
1226
1227 for (i = 0; i < SYSC_MAX_CLOCKS; i++) {
1228 if (!IS_ERR_OR_NULL(ddata->clocks[i]))
1229 clk_unprepare(ddata->clocks[i]);
1230 }
1231}
1232
Tony Lindgren70a65242017-12-15 09:41:09 -08001233/*
1234 * Common sysc register bits found on omap2, also known as type1
1235 */
1236static const struct sysc_regbits sysc_regbits_omap2 = {
1237 .dmadisable_shift = -ENODEV,
1238 .midle_shift = 12,
1239 .sidle_shift = 3,
1240 .clkact_shift = 8,
1241 .emufree_shift = 5,
1242 .enwkup_shift = 2,
1243 .srst_shift = 1,
1244 .autoidle_shift = 0,
1245};
1246
1247static const struct sysc_capabilities sysc_omap2 = {
1248 .type = TI_SYSC_OMAP2,
1249 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY | SYSC_OMAP2_EMUFREE |
1250 SYSC_OMAP2_ENAWAKEUP | SYSC_OMAP2_SOFTRESET |
1251 SYSC_OMAP2_AUTOIDLE,
1252 .regbits = &sysc_regbits_omap2,
1253};
1254
1255/* All omap2 and 3 timers, and timers 1, 2 & 10 on omap 4 and 5 */
1256static const struct sysc_capabilities sysc_omap2_timer = {
1257 .type = TI_SYSC_OMAP2_TIMER,
1258 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY | SYSC_OMAP2_EMUFREE |
1259 SYSC_OMAP2_ENAWAKEUP | SYSC_OMAP2_SOFTRESET |
1260 SYSC_OMAP2_AUTOIDLE,
1261 .regbits = &sysc_regbits_omap2,
1262 .mod_quirks = SYSC_QUIRK_USE_CLOCKACT,
1263};
1264
1265/*
1266 * SHAM2 (SHA1/MD5) sysc found on omap3, a variant of sysc_regbits_omap2
1267 * with different sidle position
1268 */
1269static const struct sysc_regbits sysc_regbits_omap3_sham = {
1270 .dmadisable_shift = -ENODEV,
1271 .midle_shift = -ENODEV,
1272 .sidle_shift = 4,
1273 .clkact_shift = -ENODEV,
1274 .enwkup_shift = -ENODEV,
1275 .srst_shift = 1,
1276 .autoidle_shift = 0,
1277 .emufree_shift = -ENODEV,
1278};
1279
1280static const struct sysc_capabilities sysc_omap3_sham = {
1281 .type = TI_SYSC_OMAP3_SHAM,
1282 .sysc_mask = SYSC_OMAP2_SOFTRESET | SYSC_OMAP2_AUTOIDLE,
1283 .regbits = &sysc_regbits_omap3_sham,
1284};
1285
1286/*
1287 * AES register bits found on omap3 and later, a variant of
1288 * sysc_regbits_omap2 with different sidle position
1289 */
1290static const struct sysc_regbits sysc_regbits_omap3_aes = {
1291 .dmadisable_shift = -ENODEV,
1292 .midle_shift = -ENODEV,
1293 .sidle_shift = 6,
1294 .clkact_shift = -ENODEV,
1295 .enwkup_shift = -ENODEV,
1296 .srst_shift = 1,
1297 .autoidle_shift = 0,
1298 .emufree_shift = -ENODEV,
1299};
1300
1301static const struct sysc_capabilities sysc_omap3_aes = {
1302 .type = TI_SYSC_OMAP3_AES,
1303 .sysc_mask = SYSC_OMAP2_SOFTRESET | SYSC_OMAP2_AUTOIDLE,
1304 .regbits = &sysc_regbits_omap3_aes,
1305};
1306
1307/*
1308 * Common sysc register bits found on omap4, also known as type2
1309 */
1310static const struct sysc_regbits sysc_regbits_omap4 = {
1311 .dmadisable_shift = 16,
1312 .midle_shift = 4,
1313 .sidle_shift = 2,
1314 .clkact_shift = -ENODEV,
1315 .enwkup_shift = -ENODEV,
1316 .emufree_shift = 1,
1317 .srst_shift = 0,
1318 .autoidle_shift = -ENODEV,
1319};
1320
1321static const struct sysc_capabilities sysc_omap4 = {
1322 .type = TI_SYSC_OMAP4,
1323 .sysc_mask = SYSC_OMAP4_DMADISABLE | SYSC_OMAP4_FREEEMU |
1324 SYSC_OMAP4_SOFTRESET,
1325 .regbits = &sysc_regbits_omap4,
1326};
1327
1328static const struct sysc_capabilities sysc_omap4_timer = {
1329 .type = TI_SYSC_OMAP4_TIMER,
1330 .sysc_mask = SYSC_OMAP4_DMADISABLE | SYSC_OMAP4_FREEEMU |
1331 SYSC_OMAP4_SOFTRESET,
1332 .regbits = &sysc_regbits_omap4,
1333};
1334
1335/*
1336 * Common sysc register bits found on omap4, also known as type3
1337 */
1338static const struct sysc_regbits sysc_regbits_omap4_simple = {
1339 .dmadisable_shift = -ENODEV,
1340 .midle_shift = 2,
1341 .sidle_shift = 0,
1342 .clkact_shift = -ENODEV,
1343 .enwkup_shift = -ENODEV,
1344 .srst_shift = -ENODEV,
1345 .emufree_shift = -ENODEV,
1346 .autoidle_shift = -ENODEV,
1347};
1348
1349static const struct sysc_capabilities sysc_omap4_simple = {
1350 .type = TI_SYSC_OMAP4_SIMPLE,
1351 .regbits = &sysc_regbits_omap4_simple,
1352};
1353
1354/*
1355 * SmartReflex sysc found on omap34xx
1356 */
1357static const struct sysc_regbits sysc_regbits_omap34xx_sr = {
1358 .dmadisable_shift = -ENODEV,
1359 .midle_shift = -ENODEV,
1360 .sidle_shift = -ENODEV,
1361 .clkact_shift = 20,
1362 .enwkup_shift = -ENODEV,
1363 .srst_shift = -ENODEV,
1364 .emufree_shift = -ENODEV,
1365 .autoidle_shift = -ENODEV,
1366};
1367
1368static const struct sysc_capabilities sysc_34xx_sr = {
1369 .type = TI_SYSC_OMAP34XX_SR,
1370 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY,
1371 .regbits = &sysc_regbits_omap34xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001372 .mod_quirks = SYSC_QUIRK_USE_CLOCKACT | SYSC_QUIRK_UNCACHED |
1373 SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08001374};
1375
1376/*
1377 * SmartReflex sysc found on omap36xx and later
1378 */
1379static const struct sysc_regbits sysc_regbits_omap36xx_sr = {
1380 .dmadisable_shift = -ENODEV,
1381 .midle_shift = -ENODEV,
1382 .sidle_shift = 24,
1383 .clkact_shift = -ENODEV,
1384 .enwkup_shift = 26,
1385 .srst_shift = -ENODEV,
1386 .emufree_shift = -ENODEV,
1387 .autoidle_shift = -ENODEV,
1388};
1389
1390static const struct sysc_capabilities sysc_36xx_sr = {
1391 .type = TI_SYSC_OMAP36XX_SR,
Tony Lindgren3267c082018-01-22 09:32:53 -08001392 .sysc_mask = SYSC_OMAP3_SR_ENAWAKEUP,
Tony Lindgren70a65242017-12-15 09:41:09 -08001393 .regbits = &sysc_regbits_omap36xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001394 .mod_quirks = SYSC_QUIRK_UNCACHED | SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08001395};
1396
1397static const struct sysc_capabilities sysc_omap4_sr = {
1398 .type = TI_SYSC_OMAP4_SR,
1399 .regbits = &sysc_regbits_omap36xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001400 .mod_quirks = SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08001401};
1402
1403/*
1404 * McASP register bits found on omap4 and later
1405 */
1406static const struct sysc_regbits sysc_regbits_omap4_mcasp = {
1407 .dmadisable_shift = -ENODEV,
1408 .midle_shift = -ENODEV,
1409 .sidle_shift = 0,
1410 .clkact_shift = -ENODEV,
1411 .enwkup_shift = -ENODEV,
1412 .srst_shift = -ENODEV,
1413 .emufree_shift = -ENODEV,
1414 .autoidle_shift = -ENODEV,
1415};
1416
1417static const struct sysc_capabilities sysc_omap4_mcasp = {
1418 .type = TI_SYSC_OMAP4_MCASP,
1419 .regbits = &sysc_regbits_omap4_mcasp,
1420};
1421
1422/*
1423 * FS USB host found on omap4 and later
1424 */
1425static const struct sysc_regbits sysc_regbits_omap4_usb_host_fs = {
1426 .dmadisable_shift = -ENODEV,
1427 .midle_shift = -ENODEV,
1428 .sidle_shift = 24,
1429 .clkact_shift = -ENODEV,
1430 .enwkup_shift = 26,
1431 .srst_shift = -ENODEV,
1432 .emufree_shift = -ENODEV,
1433 .autoidle_shift = -ENODEV,
1434};
1435
1436static const struct sysc_capabilities sysc_omap4_usb_host_fs = {
1437 .type = TI_SYSC_OMAP4_USB_HOST_FS,
1438 .sysc_mask = SYSC_OMAP2_ENAWAKEUP,
1439 .regbits = &sysc_regbits_omap4_usb_host_fs,
1440};
1441
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001442static int sysc_init_pdata(struct sysc *ddata)
1443{
1444 struct ti_sysc_platform_data *pdata = dev_get_platdata(ddata->dev);
1445 struct ti_sysc_module_data mdata;
1446 int error = 0;
1447
1448 if (!pdata || !ddata->legacy_mode)
1449 return 0;
1450
1451 mdata.name = ddata->legacy_mode;
1452 mdata.module_pa = ddata->module_pa;
1453 mdata.module_size = ddata->module_size;
1454 mdata.offsets = ddata->offsets;
1455 mdata.nr_offsets = SYSC_MAX_REGS;
1456 mdata.cap = ddata->cap;
1457 mdata.cfg = &ddata->cfg;
1458
1459 if (!pdata->init_module)
1460 return -ENODEV;
1461
1462 error = pdata->init_module(ddata->dev, &mdata, &ddata->cookie);
1463 if (error == -EEXIST)
1464 error = 0;
1465
1466 return error;
1467}
1468
Tony Lindgren70a65242017-12-15 09:41:09 -08001469static int sysc_init_match(struct sysc *ddata)
1470{
1471 const struct sysc_capabilities *cap;
1472
1473 cap = of_device_get_match_data(ddata->dev);
1474 if (!cap)
1475 return -EINVAL;
1476
1477 ddata->cap = cap;
1478 if (ddata->cap)
1479 ddata->cfg.quirks |= ddata->cap->mod_quirks;
1480
1481 return 0;
1482}
1483
Tony Lindgren76f0f772018-02-23 08:28:45 -08001484static void ti_sysc_idle(struct work_struct *work)
1485{
1486 struct sysc *ddata;
1487
1488 ddata = container_of(work, struct sysc, idle_work.work);
1489
1490 if (pm_runtime_active(ddata->dev))
1491 pm_runtime_put_sync(ddata->dev);
1492}
1493
Tony Lindgrenc4bebea2018-04-16 10:24:54 -07001494static const struct of_device_id sysc_match_table[] = {
1495 { .compatible = "simple-bus", },
1496 { /* sentinel */ },
1497};
1498
Tony Lindgren0eecc632017-10-10 14:23:43 -07001499static int sysc_probe(struct platform_device *pdev)
1500{
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001501 struct ti_sysc_platform_data *pdata = dev_get_platdata(&pdev->dev);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001502 struct sysc *ddata;
1503 int error;
1504
1505 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
1506 if (!ddata)
1507 return -ENOMEM;
1508
1509 ddata->dev = &pdev->dev;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001510 platform_set_drvdata(pdev, ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001511
Tony Lindgren70a65242017-12-15 09:41:09 -08001512 error = sysc_init_match(ddata);
1513 if (error)
1514 return error;
1515
Tony Lindgren566a9b052017-12-15 09:41:19 -08001516 error = sysc_init_dts_quirks(ddata);
1517 if (error)
1518 goto unprepare;
1519
Tony Lindgren0eecc632017-10-10 14:23:43 -07001520 error = sysc_get_clocks(ddata);
1521 if (error)
1522 return error;
1523
1524 error = sysc_map_and_check_registers(ddata);
1525 if (error)
1526 goto unprepare;
1527
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08001528 error = sysc_init_sysc_mask(ddata);
1529 if (error)
1530 goto unprepare;
1531
1532 error = sysc_init_idlemodes(ddata);
1533 if (error)
1534 goto unprepare;
1535
1536 error = sysc_init_syss_mask(ddata);
1537 if (error)
1538 goto unprepare;
1539
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001540 error = sysc_init_pdata(ddata);
1541 if (error)
1542 goto unprepare;
1543
Tony Lindgren0eecc632017-10-10 14:23:43 -07001544 pm_runtime_enable(ddata->dev);
Tony Lindgren566a9b052017-12-15 09:41:19 -08001545
1546 error = sysc_init_module(ddata);
1547 if (error)
1548 goto unprepare;
1549
Tony Lindgren0eecc632017-10-10 14:23:43 -07001550 error = pm_runtime_get_sync(ddata->dev);
1551 if (error < 0) {
1552 pm_runtime_put_noidle(ddata->dev);
1553 pm_runtime_disable(ddata->dev);
1554 goto unprepare;
1555 }
1556
Tony Lindgren0eecc632017-10-10 14:23:43 -07001557 sysc_show_registers(ddata);
1558
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001559 ddata->dev->type = &sysc_device_type;
Tony Lindgrenc4bebea2018-04-16 10:24:54 -07001560 error = of_platform_populate(ddata->dev->of_node, sysc_match_table,
1561 pdata ? pdata->auxdata : NULL,
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001562 ddata->dev);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001563 if (error)
1564 goto err;
1565
Tony Lindgren76f0f772018-02-23 08:28:45 -08001566 INIT_DELAYED_WORK(&ddata->idle_work, ti_sysc_idle);
1567
1568 /* At least earlycon won't survive without deferred idle */
1569 if (ddata->cfg.quirks & (SYSC_QUIRK_NO_IDLE_ON_INIT |
1570 SYSC_QUIRK_NO_RESET_ON_INIT)) {
1571 schedule_delayed_work(&ddata->idle_work, 3000);
1572 } else {
1573 pm_runtime_put(&pdev->dev);
1574 }
Tony Lindgren0eecc632017-10-10 14:23:43 -07001575
1576 return 0;
1577
1578err:
Tony Lindgren0eecc632017-10-10 14:23:43 -07001579 pm_runtime_put_sync(&pdev->dev);
1580 pm_runtime_disable(&pdev->dev);
1581unprepare:
1582 sysc_unprepare(ddata);
1583
1584 return error;
1585}
1586
Tony Lindgren684be5a2017-10-13 10:48:40 -07001587static int sysc_remove(struct platform_device *pdev)
1588{
1589 struct sysc *ddata = platform_get_drvdata(pdev);
1590 int error;
1591
Tony Lindgren76f0f772018-02-23 08:28:45 -08001592 cancel_delayed_work_sync(&ddata->idle_work);
1593
Tony Lindgren684be5a2017-10-13 10:48:40 -07001594 error = pm_runtime_get_sync(ddata->dev);
1595 if (error < 0) {
1596 pm_runtime_put_noidle(ddata->dev);
1597 pm_runtime_disable(ddata->dev);
1598 goto unprepare;
1599 }
1600
1601 of_platform_depopulate(&pdev->dev);
1602
Tony Lindgren684be5a2017-10-13 10:48:40 -07001603 pm_runtime_put_sync(&pdev->dev);
1604 pm_runtime_disable(&pdev->dev);
1605
1606unprepare:
1607 sysc_unprepare(ddata);
1608
1609 return 0;
1610}
1611
Tony Lindgren0eecc632017-10-10 14:23:43 -07001612static const struct of_device_id sysc_match[] = {
Tony Lindgren70a65242017-12-15 09:41:09 -08001613 { .compatible = "ti,sysc-omap2", .data = &sysc_omap2, },
1614 { .compatible = "ti,sysc-omap2-timer", .data = &sysc_omap2_timer, },
1615 { .compatible = "ti,sysc-omap4", .data = &sysc_omap4, },
1616 { .compatible = "ti,sysc-omap4-timer", .data = &sysc_omap4_timer, },
1617 { .compatible = "ti,sysc-omap4-simple", .data = &sysc_omap4_simple, },
1618 { .compatible = "ti,sysc-omap3430-sr", .data = &sysc_34xx_sr, },
1619 { .compatible = "ti,sysc-omap3630-sr", .data = &sysc_36xx_sr, },
1620 { .compatible = "ti,sysc-omap4-sr", .data = &sysc_omap4_sr, },
1621 { .compatible = "ti,sysc-omap3-sham", .data = &sysc_omap3_sham, },
1622 { .compatible = "ti,sysc-omap-aes", .data = &sysc_omap3_aes, },
1623 { .compatible = "ti,sysc-mcasp", .data = &sysc_omap4_mcasp, },
1624 { .compatible = "ti,sysc-usb-host-fs",
1625 .data = &sysc_omap4_usb_host_fs, },
Tony Lindgren0eecc632017-10-10 14:23:43 -07001626 { },
1627};
1628MODULE_DEVICE_TABLE(of, sysc_match);
1629
1630static struct platform_driver sysc_driver = {
1631 .probe = sysc_probe,
Tony Lindgren684be5a2017-10-13 10:48:40 -07001632 .remove = sysc_remove,
Tony Lindgren0eecc632017-10-10 14:23:43 -07001633 .driver = {
1634 .name = "ti-sysc",
1635 .of_match_table = sysc_match,
1636 .pm = &sysc_pm_ops,
1637 },
1638};
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001639
1640static int __init sysc_init(void)
1641{
1642 bus_register_notifier(&platform_bus_type, &sysc_nb);
1643
1644 return platform_driver_register(&sysc_driver);
1645}
1646module_init(sysc_init);
1647
1648static void __exit sysc_exit(void)
1649{
1650 bus_unregister_notifier(&platform_bus_type, &sysc_nb);
1651 platform_driver_unregister(&sysc_driver);
1652}
1653module_exit(sysc_exit);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001654
1655MODULE_DESCRIPTION("TI sysc interconnect target driver");
1656MODULE_LICENSE("GPL v2");