blob: 8bf153d821381101afe5d18f510538c541e13a00 [file] [log] [blame]
Tony Lindgren0eecc632017-10-10 14:23:43 -07001/*
2 * ti-sysc.c - Texas Instruments sysc interconnect target driver
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
9 * kind, whether express or implied; without even the implied warranty
10 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#include <linux/io.h>
15#include <linux/clk.h>
Tony Lindgren2c355ff2018-02-22 13:58:03 -080016#include <linux/clkdev.h>
Tony Lindgrena885f0f2018-02-22 14:03:48 -080017#include <linux/delay.h>
Tony Lindgren0eecc632017-10-10 14:23:43 -070018#include <linux/module.h>
19#include <linux/platform_device.h>
Tony Lindgrena885f0f2018-02-22 14:03:48 -080020#include <linux/pm_domain.h>
Tony Lindgren0eecc632017-10-10 14:23:43 -070021#include <linux/pm_runtime.h>
Tony Lindgren50622362018-04-16 10:20:27 -070022#include <linux/reset.h>
Tony Lindgren0eecc632017-10-10 14:23:43 -070023#include <linux/of_address.h>
24#include <linux/of_platform.h>
Tony Lindgren2c355ff2018-02-22 13:58:03 -080025#include <linux/slab.h>
Faiz Abbas596e7952018-07-09 22:18:39 +053026#include <linux/iopoll.h>
Tony Lindgren2c355ff2018-02-22 13:58:03 -080027
Tony Lindgren70a65242017-12-15 09:41:09 -080028#include <linux/platform_data/ti-sysc.h>
29
30#include <dt-bindings/bus/ti-sysc.h>
Tony Lindgren0eecc632017-10-10 14:23:43 -070031
Faiz Abbas596e7952018-07-09 22:18:39 +053032#define MAX_MODULE_SOFTRESET_WAIT 10000
33
Tony Lindgren0eecc632017-10-10 14:23:43 -070034static const char * const reg_names[] = { "rev", "sysc", "syss", };
35
36enum sysc_clocks {
37 SYSC_FCK,
38 SYSC_ICK,
Tony Lindgren09dfe582018-04-16 10:25:52 -070039 SYSC_OPTFCK0,
40 SYSC_OPTFCK1,
41 SYSC_OPTFCK2,
42 SYSC_OPTFCK3,
43 SYSC_OPTFCK4,
44 SYSC_OPTFCK5,
45 SYSC_OPTFCK6,
46 SYSC_OPTFCK7,
Tony Lindgren0eecc632017-10-10 14:23:43 -070047 SYSC_MAX_CLOCKS,
48};
49
Tony Lindgren09dfe582018-04-16 10:25:52 -070050static const char * const clock_names[SYSC_ICK + 1] = { "fck", "ick", };
Tony Lindgren0eecc632017-10-10 14:23:43 -070051
Tony Lindgrenc5a2de92017-12-15 09:41:23 -080052#define SYSC_IDLEMODE_MASK 3
53#define SYSC_CLOCKACTIVITY_MASK 3
54
Tony Lindgren0eecc632017-10-10 14:23:43 -070055/**
56 * struct sysc - TI sysc interconnect target module registers and capabilities
57 * @dev: struct device pointer
58 * @module_pa: physical address of the interconnect target module
59 * @module_size: size of the interconnect target module
60 * @module_va: virtual address of the interconnect target module
61 * @offsets: register offsets from module base
62 * @clocks: clocks used by the interconnect target module
Tony Lindgren09dfe582018-04-16 10:25:52 -070063 * @clock_roles: clock role names for the found clocks
64 * @nr_clocks: number of clocks used by the interconnect target module
Tony Lindgren0eecc632017-10-10 14:23:43 -070065 * @legacy_mode: configured for legacy mode if set
Tony Lindgren70a65242017-12-15 09:41:09 -080066 * @cap: interconnect target module capabilities
67 * @cfg: interconnect target module configuration
Tony Lindgren566a9b052017-12-15 09:41:19 -080068 * @name: name if available
69 * @revision: interconnect target module revision
Tony Lindgren62020f22018-02-22 13:59:44 -080070 * @needs_resume: runtime resume needed on resume from suspend
Tony Lindgren0eecc632017-10-10 14:23:43 -070071 */
72struct sysc {
73 struct device *dev;
74 u64 module_pa;
75 u32 module_size;
76 void __iomem *module_va;
77 int offsets[SYSC_MAX_REGS];
Tony Lindgrena3e92e72019-03-21 11:00:21 -070078 struct ti_sysc_module_data *mdata;
Tony Lindgren09dfe582018-04-16 10:25:52 -070079 struct clk **clocks;
80 const char **clock_roles;
81 int nr_clocks;
Tony Lindgren50622362018-04-16 10:20:27 -070082 struct reset_control *rsts;
Tony Lindgren0eecc632017-10-10 14:23:43 -070083 const char *legacy_mode;
Tony Lindgren70a65242017-12-15 09:41:09 -080084 const struct sysc_capabilities *cap;
85 struct sysc_config cfg;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -080086 struct ti_sysc_cookie cookie;
Tony Lindgren566a9b052017-12-15 09:41:19 -080087 const char *name;
88 u32 revision;
Tony Lindgren62020f22018-02-22 13:59:44 -080089 bool enabled;
90 bool needs_resume;
Tony Lindgrena885f0f2018-02-22 14:03:48 -080091 bool child_needs_resume;
Tony Lindgren76f0f772018-02-23 08:28:45 -080092 struct delayed_work idle_work;
Tony Lindgren0eecc632017-10-10 14:23:43 -070093};
94
Tony Lindgren4014c082018-12-10 14:11:26 -080095static void sysc_parse_dts_quirks(struct sysc *ddata, struct device_node *np,
96 bool is_child);
97
Tony Lindgrenb7182b42019-03-21 11:00:21 -070098static void sysc_write(struct sysc *ddata, int offset, u32 value)
Faiz Abbas596e7952018-07-09 22:18:39 +053099{
100 writel_relaxed(value, ddata->module_va + offset);
101}
102
Tony Lindgren566a9b052017-12-15 09:41:19 -0800103static u32 sysc_read(struct sysc *ddata, int offset)
104{
105 if (ddata->cfg.quirks & SYSC_QUIRK_16BIT) {
106 u32 val;
107
108 val = readw_relaxed(ddata->module_va + offset);
109 val |= (readw_relaxed(ddata->module_va + offset + 4) << 16);
110
111 return val;
112 }
113
114 return readl_relaxed(ddata->module_va + offset);
115}
116
Tony Lindgren09dfe582018-04-16 10:25:52 -0700117static bool sysc_opt_clks_needed(struct sysc *ddata)
118{
119 return !!(ddata->cfg.quirks & SYSC_QUIRK_OPT_CLKS_NEEDED);
120}
121
Tony Lindgren0eecc632017-10-10 14:23:43 -0700122static u32 sysc_read_revision(struct sysc *ddata)
123{
Tony Lindgren566a9b052017-12-15 09:41:19 -0800124 int offset = ddata->offsets[SYSC_REVISION];
125
126 if (offset < 0)
127 return 0;
128
129 return sysc_read(ddata, offset);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700130}
131
Tony Lindgren09dfe582018-04-16 10:25:52 -0700132static int sysc_get_one_clock(struct sysc *ddata, const char *name)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700133{
Tony Lindgren09dfe582018-04-16 10:25:52 -0700134 int error, i, index = -ENODEV;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700135
Tony Lindgren09dfe582018-04-16 10:25:52 -0700136 if (!strncmp(clock_names[SYSC_FCK], name, 3))
137 index = SYSC_FCK;
138 else if (!strncmp(clock_names[SYSC_ICK], name, 3))
139 index = SYSC_ICK;
140
141 if (index < 0) {
142 for (i = SYSC_OPTFCK0; i < SYSC_MAX_CLOCKS; i++) {
Tony Lindgrenc97c8622018-05-18 07:54:44 -0700143 if (!ddata->clocks[i]) {
Tony Lindgren09dfe582018-04-16 10:25:52 -0700144 index = i;
145 break;
146 }
147 }
Tony Lindgren0eecc632017-10-10 14:23:43 -0700148 }
Tony Lindgren09dfe582018-04-16 10:25:52 -0700149
150 if (index < 0) {
151 dev_err(ddata->dev, "clock %s not added\n", name);
152 return index;
153 }
Tony Lindgren0eecc632017-10-10 14:23:43 -0700154
155 ddata->clocks[index] = devm_clk_get(ddata->dev, name);
156 if (IS_ERR(ddata->clocks[index])) {
157 if (PTR_ERR(ddata->clocks[index]) == -ENOENT)
158 return 0;
159
160 dev_err(ddata->dev, "clock get error for %s: %li\n",
161 name, PTR_ERR(ddata->clocks[index]));
162
163 return PTR_ERR(ddata->clocks[index]);
164 }
165
166 error = clk_prepare(ddata->clocks[index]);
167 if (error) {
168 dev_err(ddata->dev, "clock prepare error for %s: %i\n",
169 name, error);
170
171 return error;
172 }
173
174 return 0;
175}
176
177static int sysc_get_clocks(struct sysc *ddata)
178{
Tony Lindgren09dfe582018-04-16 10:25:52 -0700179 struct device_node *np = ddata->dev->of_node;
180 struct property *prop;
181 const char *name;
182 int nr_fck = 0, nr_ick = 0, i, error = 0;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700183
Kees Cook20749052018-06-18 10:45:49 -0700184 ddata->clock_roles = devm_kcalloc(ddata->dev,
Tony Lindgren09dfe582018-04-16 10:25:52 -0700185 SYSC_MAX_CLOCKS,
Kees Cook20749052018-06-18 10:45:49 -0700186 sizeof(*ddata->clock_roles),
Tony Lindgren09dfe582018-04-16 10:25:52 -0700187 GFP_KERNEL);
188 if (!ddata->clock_roles)
189 return -ENOMEM;
190
191 of_property_for_each_string(np, "clock-names", prop, name) {
192 if (!strncmp(clock_names[SYSC_FCK], name, 3))
193 nr_fck++;
194 if (!strncmp(clock_names[SYSC_ICK], name, 3))
195 nr_ick++;
196 ddata->clock_roles[ddata->nr_clocks] = name;
197 ddata->nr_clocks++;
198 }
199
200 if (ddata->nr_clocks < 1)
201 return 0;
202
203 if (ddata->nr_clocks > SYSC_MAX_CLOCKS) {
204 dev_err(ddata->dev, "too many clocks for %pOF\n", np);
205
206 return -EINVAL;
207 }
208
209 if (nr_fck > 1 || nr_ick > 1) {
210 dev_err(ddata->dev, "max one fck and ick for %pOF\n", np);
211
212 return -EINVAL;
213 }
214
Kees Cook20749052018-06-18 10:45:49 -0700215 ddata->clocks = devm_kcalloc(ddata->dev,
216 ddata->nr_clocks, sizeof(*ddata->clocks),
Tony Lindgren09dfe582018-04-16 10:25:52 -0700217 GFP_KERNEL);
218 if (!ddata->clocks)
219 return -ENOMEM;
220
Tony Lindgren7b4f8ac2018-11-15 14:46:52 -0800221 for (i = 0; i < SYSC_MAX_CLOCKS; i++) {
222 const char *name = ddata->clock_roles[i];
223
224 if (!name)
225 continue;
226
227 error = sysc_get_one_clock(ddata, name);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700228 if (error && error != -ENOENT)
229 return error;
230 }
231
232 return 0;
233}
234
Tony Lindgrend8789702019-03-21 11:00:21 -0700235static int sysc_enable_main_clocks(struct sysc *ddata)
236{
237 struct clk *clock;
238 int i, error;
239
240 if (!ddata->clocks)
241 return 0;
242
243 for (i = 0; i < SYSC_OPTFCK0; i++) {
244 clock = ddata->clocks[i];
245
246 /* Main clocks may not have ick */
247 if (IS_ERR_OR_NULL(clock))
248 continue;
249
250 error = clk_enable(clock);
251 if (error)
252 goto err_disable;
253 }
254
255 return 0;
256
257err_disable:
258 for (i--; i >= 0; i--) {
259 clock = ddata->clocks[i];
260
261 /* Main clocks may not have ick */
262 if (IS_ERR_OR_NULL(clock))
263 continue;
264
265 clk_disable(clock);
266 }
267
268 return error;
269}
270
271static void sysc_disable_main_clocks(struct sysc *ddata)
272{
273 struct clk *clock;
274 int i;
275
276 if (!ddata->clocks)
277 return;
278
279 for (i = 0; i < SYSC_OPTFCK0; i++) {
280 clock = ddata->clocks[i];
281 if (IS_ERR_OR_NULL(clock))
282 continue;
283
284 clk_disable(clock);
285 }
286}
287
288static int sysc_enable_opt_clocks(struct sysc *ddata)
289{
290 struct clk *clock;
291 int i, error;
292
293 if (!ddata->clocks)
294 return 0;
295
296 for (i = SYSC_OPTFCK0; i < SYSC_MAX_CLOCKS; i++) {
297 clock = ddata->clocks[i];
298
299 /* Assume no holes for opt clocks */
300 if (IS_ERR_OR_NULL(clock))
301 return 0;
302
303 error = clk_enable(clock);
304 if (error)
305 goto err_disable;
306 }
307
308 return 0;
309
310err_disable:
311 for (i--; i >= 0; i--) {
312 clock = ddata->clocks[i];
313 if (IS_ERR_OR_NULL(clock))
314 continue;
315
316 clk_disable(clock);
317 }
318
319 return error;
320}
321
322static void sysc_disable_opt_clocks(struct sysc *ddata)
323{
324 struct clk *clock;
325 int i;
326
327 if (!ddata->clocks)
328 return;
329
330 for (i = SYSC_OPTFCK0; i < SYSC_MAX_CLOCKS; i++) {
331 clock = ddata->clocks[i];
332
333 /* Assume no holes for opt clocks */
334 if (IS_ERR_OR_NULL(clock))
335 return;
336
337 clk_disable(clock);
338 }
339}
340
Tony Lindgren0eecc632017-10-10 14:23:43 -0700341/**
Tony Lindgrenb11c1ea2019-03-21 11:00:21 -0700342 * sysc_init_resets - init rstctrl reset line if configured
Tony Lindgren50622362018-04-16 10:20:27 -0700343 * @ddata: device driver data
344 *
Tony Lindgrenb11c1ea2019-03-21 11:00:21 -0700345 * See sysc_rstctrl_reset_deassert().
Tony Lindgren50622362018-04-16 10:20:27 -0700346 */
347static int sysc_init_resets(struct sysc *ddata)
348{
Tony Lindgren50622362018-04-16 10:20:27 -0700349 ddata->rsts =
350 devm_reset_control_array_get_optional_exclusive(ddata->dev);
351 if (IS_ERR(ddata->rsts))
352 return PTR_ERR(ddata->rsts);
353
Tony Lindgren50622362018-04-16 10:20:27 -0700354 return 0;
355}
356
357/**
Tony Lindgren0eecc632017-10-10 14:23:43 -0700358 * sysc_parse_and_check_child_range - parses module IO region from ranges
359 * @ddata: device driver data
360 *
361 * In general we only need rev, syss, and sysc registers and not the whole
362 * module range. But we do want the offsets for these registers from the
363 * module base. This allows us to check them against the legacy hwmod
364 * platform data. Let's also check the ranges are configured properly.
365 */
366static int sysc_parse_and_check_child_range(struct sysc *ddata)
367{
368 struct device_node *np = ddata->dev->of_node;
369 const __be32 *ranges;
370 u32 nr_addr, nr_size;
371 int len, error;
372
373 ranges = of_get_property(np, "ranges", &len);
374 if (!ranges) {
375 dev_err(ddata->dev, "missing ranges for %pOF\n", np);
376
377 return -ENOENT;
378 }
379
380 len /= sizeof(*ranges);
381
382 if (len < 3) {
383 dev_err(ddata->dev, "incomplete ranges for %pOF\n", np);
384
385 return -EINVAL;
386 }
387
388 error = of_property_read_u32(np, "#address-cells", &nr_addr);
389 if (error)
390 return -ENOENT;
391
392 error = of_property_read_u32(np, "#size-cells", &nr_size);
393 if (error)
394 return -ENOENT;
395
396 if (nr_addr != 1 || nr_size != 1) {
397 dev_err(ddata->dev, "invalid ranges for %pOF\n", np);
398
399 return -EINVAL;
400 }
401
402 ranges++;
403 ddata->module_pa = of_translate_address(np, ranges++);
404 ddata->module_size = be32_to_cpup(ranges);
405
Tony Lindgren0eecc632017-10-10 14:23:43 -0700406 return 0;
407}
408
Tony Lindgren3bb37c82018-02-22 14:05:14 -0800409static struct device_node *stdout_path;
410
411static void sysc_init_stdout_path(struct sysc *ddata)
412{
413 struct device_node *np = NULL;
414 const char *uart;
415
416 if (IS_ERR(stdout_path))
417 return;
418
419 if (stdout_path)
420 return;
421
422 np = of_find_node_by_path("/chosen");
423 if (!np)
424 goto err;
425
426 uart = of_get_property(np, "stdout-path", NULL);
427 if (!uart)
428 goto err;
429
430 np = of_find_node_by_path(uart);
431 if (!np)
432 goto err;
433
434 stdout_path = np;
435
436 return;
437
438err:
439 stdout_path = ERR_PTR(-ENODEV);
440}
441
442static void sysc_check_quirk_stdout(struct sysc *ddata,
443 struct device_node *np)
444{
445 sysc_init_stdout_path(ddata);
446 if (np != stdout_path)
447 return;
448
449 ddata->cfg.quirks |= SYSC_QUIRK_NO_IDLE_ON_INIT |
450 SYSC_QUIRK_NO_RESET_ON_INIT;
451}
452
Tony Lindgren0eecc632017-10-10 14:23:43 -0700453/**
454 * sysc_check_one_child - check child configuration
455 * @ddata: device driver data
456 * @np: child device node
457 *
458 * Let's avoid messy situations where we have new interconnect target
459 * node but children have "ti,hwmods". These belong to the interconnect
460 * target node and are managed by this driver.
461 */
462static int sysc_check_one_child(struct sysc *ddata,
463 struct device_node *np)
464{
465 const char *name;
466
467 name = of_get_property(np, "ti,hwmods", NULL);
468 if (name)
469 dev_warn(ddata->dev, "really a child ti,hwmods property?");
470
Tony Lindgren3bb37c82018-02-22 14:05:14 -0800471 sysc_check_quirk_stdout(ddata, np);
Tony Lindgren4014c082018-12-10 14:11:26 -0800472 sysc_parse_dts_quirks(ddata, np, true);
Tony Lindgren3bb37c82018-02-22 14:05:14 -0800473
Tony Lindgren0eecc632017-10-10 14:23:43 -0700474 return 0;
475}
476
477static int sysc_check_children(struct sysc *ddata)
478{
479 struct device_node *child;
480 int error;
481
482 for_each_child_of_node(ddata->dev->of_node, child) {
483 error = sysc_check_one_child(ddata, child);
484 if (error)
485 return error;
486 }
487
488 return 0;
489}
490
Tony Lindgrena7199e22017-12-15 09:41:14 -0800491/*
492 * So far only I2C uses 16-bit read access with clockactivity with revision
493 * in two registers with stride of 4. We can detect this based on the rev
494 * register size to configure things far enough to be able to properly read
495 * the revision register.
496 */
497static void sysc_check_quirk_16bit(struct sysc *ddata, struct resource *res)
498{
Tony Lindgrendd57ac12018-02-22 14:09:57 -0800499 if (resource_size(res) == 8)
Tony Lindgrena7199e22017-12-15 09:41:14 -0800500 ddata->cfg.quirks |= SYSC_QUIRK_16BIT | SYSC_QUIRK_USE_CLOCKACT;
Tony Lindgrena7199e22017-12-15 09:41:14 -0800501}
502
Tony Lindgren0eecc632017-10-10 14:23:43 -0700503/**
504 * sysc_parse_one - parses the interconnect target module registers
505 * @ddata: device driver data
506 * @reg: register to parse
507 */
508static int sysc_parse_one(struct sysc *ddata, enum sysc_registers reg)
509{
510 struct resource *res;
511 const char *name;
512
513 switch (reg) {
514 case SYSC_REVISION:
515 case SYSC_SYSCONFIG:
516 case SYSC_SYSSTATUS:
517 name = reg_names[reg];
518 break;
519 default:
520 return -EINVAL;
521 }
522
523 res = platform_get_resource_byname(to_platform_device(ddata->dev),
524 IORESOURCE_MEM, name);
525 if (!res) {
Tony Lindgren0eecc632017-10-10 14:23:43 -0700526 ddata->offsets[reg] = -ENODEV;
527
528 return 0;
529 }
530
531 ddata->offsets[reg] = res->start - ddata->module_pa;
Tony Lindgrena7199e22017-12-15 09:41:14 -0800532 if (reg == SYSC_REVISION)
533 sysc_check_quirk_16bit(ddata, res);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700534
535 return 0;
536}
537
538static int sysc_parse_registers(struct sysc *ddata)
539{
540 int i, error;
541
542 for (i = 0; i < SYSC_MAX_REGS; i++) {
543 error = sysc_parse_one(ddata, i);
544 if (error)
545 return error;
546 }
547
548 return 0;
549}
550
551/**
552 * sysc_check_registers - check for misconfigured register overlaps
553 * @ddata: device driver data
554 */
555static int sysc_check_registers(struct sysc *ddata)
556{
557 int i, j, nr_regs = 0, nr_matches = 0;
558
559 for (i = 0; i < SYSC_MAX_REGS; i++) {
560 if (ddata->offsets[i] < 0)
561 continue;
562
563 if (ddata->offsets[i] > (ddata->module_size - 4)) {
564 dev_err(ddata->dev, "register outside module range");
565
566 return -EINVAL;
567 }
568
569 for (j = 0; j < SYSC_MAX_REGS; j++) {
570 if (ddata->offsets[j] < 0)
571 continue;
572
573 if (ddata->offsets[i] == ddata->offsets[j])
574 nr_matches++;
575 }
576 nr_regs++;
577 }
578
579 if (nr_regs < 1) {
580 dev_err(ddata->dev, "missing registers\n");
581
582 return -EINVAL;
583 }
584
585 if (nr_matches > nr_regs) {
586 dev_err(ddata->dev, "overlapping registers: (%i/%i)",
587 nr_regs, nr_matches);
588
589 return -EINVAL;
590 }
591
592 return 0;
593}
594
595/**
596 * syc_ioremap - ioremap register space for the interconnect target module
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700597 * @ddata: device driver data
Tony Lindgren0eecc632017-10-10 14:23:43 -0700598 *
599 * Note that the interconnect target module registers can be anywhere
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700600 * within the interconnect target module range. For example, SGX has
601 * them at offset 0x1fc00 in the 32MB module address space. And cpsw
602 * has them at offset 0x1200 in the CPSW_WR child. Usually the
603 * the interconnect target module registers are at the beginning of
604 * the module range though.
Tony Lindgren0eecc632017-10-10 14:23:43 -0700605 */
606static int sysc_ioremap(struct sysc *ddata)
607{
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700608 int size;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700609
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700610 size = max3(ddata->offsets[SYSC_REVISION],
611 ddata->offsets[SYSC_SYSCONFIG],
612 ddata->offsets[SYSC_SYSSTATUS]);
613
614 if (size < 0 || (size + sizeof(u32)) > ddata->module_size)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700615 return -EINVAL;
616
Tony Lindgren0eecc632017-10-10 14:23:43 -0700617 ddata->module_va = devm_ioremap(ddata->dev,
618 ddata->module_pa,
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700619 size + sizeof(u32));
Tony Lindgren0eecc632017-10-10 14:23:43 -0700620 if (!ddata->module_va)
621 return -EIO;
622
623 return 0;
624}
625
626/**
627 * sysc_map_and_check_registers - ioremap and check device registers
628 * @ddata: device driver data
629 */
630static int sysc_map_and_check_registers(struct sysc *ddata)
631{
632 int error;
633
634 error = sysc_parse_and_check_child_range(ddata);
635 if (error)
636 return error;
637
638 error = sysc_check_children(ddata);
639 if (error)
640 return error;
641
642 error = sysc_parse_registers(ddata);
643 if (error)
644 return error;
645
646 error = sysc_ioremap(ddata);
647 if (error)
648 return error;
649
650 error = sysc_check_registers(ddata);
651 if (error)
652 return error;
653
654 return 0;
655}
656
657/**
658 * sysc_show_rev - read and show interconnect target module revision
659 * @bufp: buffer to print the information to
660 * @ddata: device driver data
661 */
662static int sysc_show_rev(char *bufp, struct sysc *ddata)
663{
Tony Lindgren566a9b052017-12-15 09:41:19 -0800664 int len;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700665
666 if (ddata->offsets[SYSC_REVISION] < 0)
667 return sprintf(bufp, ":NA");
668
Tony Lindgren566a9b052017-12-15 09:41:19 -0800669 len = sprintf(bufp, ":%08x", ddata->revision);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700670
671 return len;
672}
673
674static int sysc_show_reg(struct sysc *ddata,
675 char *bufp, enum sysc_registers reg)
676{
677 if (ddata->offsets[reg] < 0)
678 return sprintf(bufp, ":NA");
679
680 return sprintf(bufp, ":%x", ddata->offsets[reg]);
681}
682
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800683static int sysc_show_name(char *bufp, struct sysc *ddata)
684{
685 if (!ddata->name)
686 return 0;
687
688 return sprintf(bufp, ":%s", ddata->name);
689}
690
Tony Lindgren0eecc632017-10-10 14:23:43 -0700691/**
692 * sysc_show_registers - show information about interconnect target module
693 * @ddata: device driver data
694 */
695static void sysc_show_registers(struct sysc *ddata)
696{
697 char buf[128];
698 char *bufp = buf;
699 int i;
700
701 for (i = 0; i < SYSC_MAX_REGS; i++)
702 bufp += sysc_show_reg(ddata, bufp, i);
703
704 bufp += sysc_show_rev(bufp, ddata);
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800705 bufp += sysc_show_name(bufp, ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700706
707 dev_dbg(ddata->dev, "%llx:%x%s\n",
708 ddata->module_pa, ddata->module_size,
709 buf);
710}
711
Tony Lindgrenff437282019-03-21 11:00:21 -0700712static int __maybe_unused sysc_runtime_suspend_legacy(struct device *dev,
713 struct sysc *ddata)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700714{
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800715 struct ti_sysc_platform_data *pdata;
Tony Lindgrenff437282019-03-21 11:00:21 -0700716 int error;
717
718 pdata = dev_get_platdata(ddata->dev);
719 if (!pdata)
720 return 0;
721
722 if (!pdata->idle_module)
723 return -ENODEV;
724
725 error = pdata->idle_module(dev, &ddata->cookie);
726 if (error)
727 dev_err(dev, "%s: could not idle: %i\n",
728 __func__, error);
729
730 return 0;
731}
732
733static int __maybe_unused sysc_runtime_resume_legacy(struct device *dev,
734 struct sysc *ddata)
735{
736 struct ti_sysc_platform_data *pdata;
737 int error;
738
739 pdata = dev_get_platdata(ddata->dev);
740 if (!pdata)
741 return 0;
742
743 if (!pdata->enable_module)
744 return -ENODEV;
745
746 error = pdata->enable_module(dev, &ddata->cookie);
747 if (error)
748 dev_err(dev, "%s: could not enable: %i\n",
749 __func__, error);
750
751 return 0;
752}
753
754static int __maybe_unused sysc_runtime_suspend(struct device *dev)
755{
Tony Lindgren0eecc632017-10-10 14:23:43 -0700756 struct sysc *ddata;
Tony Lindgrend8789702019-03-21 11:00:21 -0700757 int error = 0;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700758
759 ddata = dev_get_drvdata(dev);
760
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800761 if (!ddata->enabled)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700762 return 0;
763
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800764 if (ddata->legacy_mode) {
Tony Lindgrenff437282019-03-21 11:00:21 -0700765 error = sysc_runtime_suspend_legacy(dev, ddata);
Tony Lindgren93de83a2019-03-21 11:00:21 -0700766 if (error)
767 return error;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800768 }
769
Tony Lindgrend8789702019-03-21 11:00:21 -0700770 sysc_disable_main_clocks(ddata);
Tony Lindgren09dfe582018-04-16 10:25:52 -0700771
Tony Lindgrend8789702019-03-21 11:00:21 -0700772 if (sysc_opt_clks_needed(ddata))
773 sysc_disable_opt_clocks(ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700774
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800775 ddata->enabled = false;
776
777 return error;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700778}
779
Arnd Bergmanna4a5d492017-10-13 11:25:32 +0200780static int __maybe_unused sysc_runtime_resume(struct device *dev)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700781{
782 struct sysc *ddata;
Tony Lindgrend8789702019-03-21 11:00:21 -0700783 int error = 0;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700784
785 ddata = dev_get_drvdata(dev);
786
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800787 if (ddata->enabled)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700788 return 0;
789
Tony Lindgrend8789702019-03-21 11:00:21 -0700790 if (sysc_opt_clks_needed(ddata)) {
791 error = sysc_enable_opt_clocks(ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700792 if (error)
793 return error;
794 }
795
Tony Lindgrend8789702019-03-21 11:00:21 -0700796 error = sysc_enable_main_clocks(ddata);
797 if (error)
Tony Lindgren93de83a2019-03-21 11:00:21 -0700798 goto err_opt_clocks;
799
800 if (ddata->legacy_mode) {
801 error = sysc_runtime_resume_legacy(dev, ddata);
802 if (error)
803 goto err_main_clocks;
804 }
Tony Lindgrend8789702019-03-21 11:00:21 -0700805
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800806 ddata->enabled = true;
807
Tony Lindgrend8789702019-03-21 11:00:21 -0700808 return 0;
809
810err_main_clocks:
Tony Lindgren93de83a2019-03-21 11:00:21 -0700811 sysc_disable_main_clocks(ddata);
812err_opt_clocks:
Tony Lindgrend8789702019-03-21 11:00:21 -0700813 if (sysc_opt_clks_needed(ddata))
814 sysc_disable_opt_clocks(ddata);
815
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800816 return error;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700817}
818
Tony Lindgrenf5e802032018-10-01 09:33:28 -0700819static int __maybe_unused sysc_noirq_suspend(struct device *dev)
Tony Lindgren62020f22018-02-22 13:59:44 -0800820{
821 struct sysc *ddata;
822
823 ddata = dev_get_drvdata(dev);
824
Tony Lindgren40d9f912018-09-24 12:16:54 -0700825 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
Tony Lindgrene7420c22018-04-16 10:26:46 -0700826 return 0;
827
Tony Lindgrenf5e802032018-10-01 09:33:28 -0700828 return pm_runtime_force_suspend(dev);
Tony Lindgren62020f22018-02-22 13:59:44 -0800829}
830
Tony Lindgrenf5e802032018-10-01 09:33:28 -0700831static int __maybe_unused sysc_noirq_resume(struct device *dev)
Tony Lindgren62020f22018-02-22 13:59:44 -0800832{
833 struct sysc *ddata;
834
835 ddata = dev_get_drvdata(dev);
Tony Lindgrene7420c22018-04-16 10:26:46 -0700836
Tony Lindgren40d9f912018-09-24 12:16:54 -0700837 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
Tony Lindgrene7420c22018-04-16 10:26:46 -0700838 return 0;
839
Tony Lindgrenf5e802032018-10-01 09:33:28 -0700840 return pm_runtime_force_resume(dev);
Tony Lindgrene7420c22018-04-16 10:26:46 -0700841}
842
Tony Lindgren0eecc632017-10-10 14:23:43 -0700843static const struct dev_pm_ops sysc_pm_ops = {
Tony Lindgrene7420c22018-04-16 10:26:46 -0700844 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(sysc_noirq_suspend, sysc_noirq_resume)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700845 SET_RUNTIME_PM_OPS(sysc_runtime_suspend,
846 sysc_runtime_resume,
847 NULL)
848};
849
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800850/* Module revision register based quirks */
851struct sysc_revision_quirk {
852 const char *name;
853 u32 base;
854 int rev_offset;
855 int sysc_offset;
856 int syss_offset;
857 u32 revision;
858 u32 revision_mask;
859 u32 quirks;
860};
861
862#define SYSC_QUIRK(optname, optbase, optrev, optsysc, optsyss, \
863 optrev_val, optrevmask, optquirkmask) \
864 { \
865 .name = (optname), \
866 .base = (optbase), \
867 .rev_offset = (optrev), \
868 .sysc_offset = (optsysc), \
869 .syss_offset = (optsyss), \
870 .revision = (optrev_val), \
871 .revision_mask = (optrevmask), \
872 .quirks = (optquirkmask), \
873 }
874
875static const struct sysc_revision_quirk sysc_revision_quirks[] = {
876 /* These drivers need to be fixed to not use pm_runtime_irq_safe() */
Tony Lindgren3a3d8022018-09-27 13:33:58 -0700877 SYSC_QUIRK("gpio", 0, 0, 0x10, 0x114, 0x50600801, 0xffff00ff,
Tony Lindgren09dfe582018-04-16 10:25:52 -0700878 SYSC_QUIRK_LEGACY_IDLE | SYSC_QUIRK_OPT_CLKS_IN_RESET),
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800879 SYSC_QUIRK("mmu", 0, 0, 0x10, 0x14, 0x00000020, 0xffffffff,
880 SYSC_QUIRK_LEGACY_IDLE),
881 SYSC_QUIRK("mmu", 0, 0, 0x10, 0x14, 0x00000030, 0xffffffff,
882 SYSC_QUIRK_LEGACY_IDLE),
883 SYSC_QUIRK("sham", 0, 0x100, 0x110, 0x114, 0x40000c03, 0xffffffff,
884 SYSC_QUIRK_LEGACY_IDLE),
885 SYSC_QUIRK("smartreflex", 0, -1, 0x24, -1, 0x00000000, 0xffffffff,
886 SYSC_QUIRK_LEGACY_IDLE),
887 SYSC_QUIRK("smartreflex", 0, -1, 0x38, -1, 0x00000000, 0xffffffff,
888 SYSC_QUIRK_LEGACY_IDLE),
889 SYSC_QUIRK("timer", 0, 0, 0x10, 0x14, 0x00000015, 0xffffffff,
Tony Lindgren9bd34c62019-01-22 09:03:08 -0800890 0),
Tony Lindgren8cde5d52018-04-16 10:25:15 -0700891 /* Some timers on omap4 and later */
Tony Lindgren3a3d8022018-09-27 13:33:58 -0700892 SYSC_QUIRK("timer", 0, 0, 0x10, -1, 0x50002100, 0xffffffff,
Tony Lindgren072167d2019-01-29 07:53:47 -0800893 0),
Tony Lindgren3a3d8022018-09-27 13:33:58 -0700894 SYSC_QUIRK("timer", 0, 0, 0x10, -1, 0x4fff1301, 0xffff00ff,
Tony Lindgren9bd34c62019-01-22 09:03:08 -0800895 0),
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800896 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x00000052, 0xffffffff,
897 SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgrend708bb12018-04-16 10:25:32 -0700898 /* Uarts on omap4 and later */
Tony Lindgrenb82beef2018-09-24 12:16:59 -0700899 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x50411e03, 0xffff00ff,
900 SYSC_QUIRK_LEGACY_IDLE),
901 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x47422e03, 0xffffffff,
Tony Lindgrend708bb12018-04-16 10:25:32 -0700902 SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgren7e27e5d2018-04-20 14:54:16 -0700903
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700904#ifdef DEBUG
Tony Lindgren1ba30692018-09-24 12:17:05 -0700905 SYSC_QUIRK("adc", 0, 0, 0x10, -1, 0x47300001, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -0700906 SYSC_QUIRK("atl", 0, 0, -1, -1, 0x0a070100, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700907 SYSC_QUIRK("aess", 0, 0, 0x10, -1, 0x40000000, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -0700908 SYSC_QUIRK("cm", 0, 0, -1, -1, 0x40000301, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -0700909 SYSC_QUIRK("control", 0, 0, 0x10, -1, 0x40000900, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700910 SYSC_QUIRK("cpgmac", 0, 0x1200, 0x1208, 0x1204, 0x4edb1902,
Tony Lindgren23731ea2018-09-24 12:17:09 -0700911 0xffff00f0, 0),
912 SYSC_QUIRK("dcan", 0, 0, -1, -1, 0xffffffff, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700913 SYSC_QUIRK("dcan", 0, 0, -1, -1, 0x00001401, 0xffffffff, 0),
914 SYSC_QUIRK("dwc3", 0, 0, 0x10, -1, 0x500a0200, 0xffffffff, 0),
915 SYSC_QUIRK("epwmss", 0, 0, 0x4, -1, 0x47400001, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700916 SYSC_QUIRK("gpu", 0, 0x1fc00, 0x1fc10, -1, 0, 0, 0),
917 SYSC_QUIRK("hdq1w", 0, 0, 0x14, 0x18, 0x00000006, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700918 SYSC_QUIRK("hdq1w", 0, 0, 0x14, 0x18, 0x0000000a, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700919 SYSC_QUIRK("hsi", 0, 0, 0x10, 0x14, 0x50043101, 0xffffffff, 0),
920 SYSC_QUIRK("iss", 0, 0, 0x10, -1, 0x40000101, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -0700921 SYSC_QUIRK("i2c", 0, 0, 0x10, 0x90, 0x5040000a, 0xfffff0f0, 0),
Tony Lindgren23731ea2018-09-24 12:17:09 -0700922 SYSC_QUIRK("lcdc", 0, 0, 0x54, -1, 0x4f201000, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700923 SYSC_QUIRK("mcasp", 0, 0, 0x4, -1, 0x44306302, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700924 SYSC_QUIRK("mcasp", 0, 0, 0x4, -1, 0x44307b02, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700925 SYSC_QUIRK("mcbsp", 0, -1, 0x8c, -1, 0, 0, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -0700926 SYSC_QUIRK("mcspi", 0, 0, 0x10, -1, 0x40300a0b, 0xffff00ff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700927 SYSC_QUIRK("mcspi", 0, 0, 0x110, 0x114, 0x40300a0b, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700928 SYSC_QUIRK("mailbox", 0, 0, 0x10, -1, 0x00000400, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700929 SYSC_QUIRK("m3", 0, 0, -1, -1, 0x5f580105, 0x0fff0f00, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -0700930 SYSC_QUIRK("ocp2scp", 0, 0, 0x10, 0x14, 0x50060005, 0xfffffff0, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700931 SYSC_QUIRK("ocp2scp", 0, 0, -1, -1, 0x50060007, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -0700932 SYSC_QUIRK("padconf", 0, 0, 0x10, -1, 0x4fff0800, 0xffffffff, 0),
Tony Lindgrenf0106702018-11-15 14:46:53 -0800933 SYSC_QUIRK("padconf", 0, 0, -1, -1, 0x40001100, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -0700934 SYSC_QUIRK("prcm", 0, 0, -1, -1, 0x40000100, 0xffffffff, 0),
Tony Lindgren23731ea2018-09-24 12:17:09 -0700935 SYSC_QUIRK("prcm", 0, 0, -1, -1, 0x00004102, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700936 SYSC_QUIRK("prcm", 0, 0, -1, -1, 0x40000400, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -0700937 SYSC_QUIRK("scm", 0, 0, 0x10, -1, 0x40000900, 0xffffffff, 0),
Tony Lindgren23731ea2018-09-24 12:17:09 -0700938 SYSC_QUIRK("scm", 0, 0, -1, -1, 0x4e8b0100, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700939 SYSC_QUIRK("scm", 0, 0, -1, -1, 0x4f000100, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -0700940 SYSC_QUIRK("scm", 0, 0, -1, -1, 0x40000900, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -0700941 SYSC_QUIRK("scrm", 0, 0, -1, -1, 0x00000010, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -0700942 SYSC_QUIRK("sdio", 0, 0, 0x10, -1, 0x40202301, 0xffff0ff0, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700943 SYSC_QUIRK("sdio", 0, 0x2fc, 0x110, 0x114, 0x31010000, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -0700944 SYSC_QUIRK("sdma", 0, 0, 0x2c, 0x28, 0x00010900, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700945 SYSC_QUIRK("slimbus", 0, 0, 0x10, -1, 0x40000902, 0xffffffff, 0),
946 SYSC_QUIRK("slimbus", 0, 0, 0x10, -1, 0x40002903, 0xffffffff, 0),
947 SYSC_QUIRK("spinlock", 0, 0, 0x10, -1, 0x50020000, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700948 SYSC_QUIRK("rng", 0, 0x1fe0, 0x1fe4, -1, 0x00000020, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -0700949 SYSC_QUIRK("rtc", 0, 0x74, 0x78, -1, 0x4eb01908, 0xffff00f0, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700950 SYSC_QUIRK("timer32k", 0, 0, 0x4, -1, 0x00000060, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700951 SYSC_QUIRK("usbhstll", 0, 0, 0x10, 0x14, 0x00000004, 0xffffffff, 0),
Tony Lindgrenf0106702018-11-15 14:46:53 -0800952 SYSC_QUIRK("usbhstll", 0, 0, 0x10, 0x14, 0x00000008, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700953 SYSC_QUIRK("usb_host_hs", 0, 0, 0x10, 0x14, 0x50700100, 0xffffffff, 0),
Tony Lindgrenf0106702018-11-15 14:46:53 -0800954 SYSC_QUIRK("usb_host_hs", 0, 0, 0x10, -1, 0x50700101, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700955 SYSC_QUIRK("usb_otg_hs", 0, 0x400, 0x404, 0x408, 0x00000050,
956 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700957 SYSC_QUIRK("wdt", 0, 0, 0x10, 0x14, 0x502a0500, 0xfffff0f0, 0),
958 SYSC_QUIRK("vfpe", 0, 0, 0x104, -1, 0x4d001200, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700959#endif
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800960};
961
962static void sysc_init_revision_quirks(struct sysc *ddata)
963{
964 const struct sysc_revision_quirk *q;
965 int i;
966
967 for (i = 0; i < ARRAY_SIZE(sysc_revision_quirks); i++) {
968 q = &sysc_revision_quirks[i];
969
970 if (q->base && q->base != ddata->module_pa)
971 continue;
972
973 if (q->rev_offset >= 0 &&
974 q->rev_offset != ddata->offsets[SYSC_REVISION])
975 continue;
976
977 if (q->sysc_offset >= 0 &&
978 q->sysc_offset != ddata->offsets[SYSC_SYSCONFIG])
979 continue;
980
981 if (q->syss_offset >= 0 &&
982 q->syss_offset != ddata->offsets[SYSC_SYSSTATUS])
983 continue;
984
985 if (q->revision == ddata->revision ||
986 (q->revision & q->revision_mask) ==
987 (ddata->revision & q->revision_mask)) {
988 ddata->name = q->name;
989 ddata->cfg.quirks |= q->quirks;
990 }
991 }
992}
993
Tony Lindgrena3e92e72019-03-21 11:00:21 -0700994/*
995 * Note that pdata->init_module() typically does a reset first. After
996 * pdata->init_module() is done, PM runtime can be used for the interconnect
997 * target module.
998 */
999static int sysc_legacy_init(struct sysc *ddata)
1000{
1001 struct ti_sysc_platform_data *pdata = dev_get_platdata(ddata->dev);
1002 int error;
1003
1004 if (!ddata->legacy_mode || !pdata || !pdata->init_module)
1005 return 0;
1006
1007 error = pdata->init_module(ddata->dev, ddata->mdata, &ddata->cookie);
1008 if (error == -EEXIST)
1009 error = 0;
1010
1011 return error;
1012}
1013
Tony Lindgrenb11c1ea2019-03-21 11:00:21 -07001014/**
1015 * sysc_rstctrl_reset_deassert - deassert rstctrl reset
1016 * @ddata: device driver data
1017 * @reset: reset before deassert
1018 *
1019 * A module can have both OCP softreset control and external rstctrl.
1020 * If more complicated rstctrl resets are needed, please handle these
1021 * directly from the child device driver and map only the module reset
1022 * for the parent interconnect target module device.
1023 *
1024 * Automatic reset of the module on init can be skipped with the
1025 * "ti,no-reset-on-init" device tree property.
1026 */
1027static int sysc_rstctrl_reset_deassert(struct sysc *ddata, bool reset)
1028{
1029 int error;
1030
1031 if (!ddata->rsts)
1032 return 0;
1033
1034 if (reset) {
1035 error = reset_control_assert(ddata->rsts);
1036 if (error)
1037 return error;
1038 }
1039
1040 return reset_control_deassert(ddata->rsts);
1041}
1042
Faiz Abbas596e7952018-07-09 22:18:39 +05301043static int sysc_reset(struct sysc *ddata)
1044{
1045 int offset = ddata->offsets[SYSC_SYSCONFIG];
1046 int val;
1047
1048 if (ddata->legacy_mode || offset < 0 ||
1049 ddata->cfg.quirks & SYSC_QUIRK_NO_RESET_ON_INIT)
1050 return 0;
1051
1052 /*
1053 * Currently only support reset status in sysstatus.
1054 * Warn and return error in all other cases
1055 */
1056 if (!ddata->cfg.syss_mask) {
1057 dev_err(ddata->dev, "No ti,syss-mask. Reset failed\n");
1058 return -EINVAL;
1059 }
1060
1061 val = sysc_read(ddata, offset);
1062 val |= (0x1 << ddata->cap->regbits->srst_shift);
1063 sysc_write(ddata, offset, val);
1064
1065 /* Poll on reset status */
1066 offset = ddata->offsets[SYSC_SYSSTATUS];
1067
1068 return readl_poll_timeout(ddata->module_va + offset, val,
1069 (val & ddata->cfg.syss_mask) == 0x0,
1070 100, MAX_MODULE_SOFTRESET_WAIT);
1071}
1072
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001073/*
1074 * At this point the module is configured enough to read the revision but
1075 * module may not be completely configured yet to use PM runtime. Enable
1076 * all clocks directly during init to configure the quirks needed for PM
1077 * runtime based on the revision register.
1078 */
Tony Lindgren566a9b052017-12-15 09:41:19 -08001079static int sysc_init_module(struct sysc *ddata)
1080{
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001081 int error = 0;
1082 bool manage_clocks = true;
Tony Lindgrenb11c1ea2019-03-21 11:00:21 -07001083 bool reset = true;
1084
1085 if (ddata->cfg.quirks & SYSC_QUIRK_NO_RESET_ON_INIT)
1086 reset = false;
1087
1088 error = sysc_rstctrl_reset_deassert(ddata, reset);
1089 if (error)
1090 return error;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001091
Tony Lindgren386cb762019-03-22 07:49:30 -07001092 if (ddata->cfg.quirks &
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001093 (SYSC_QUIRK_NO_IDLE | SYSC_QUIRK_NO_IDLE_ON_INIT))
1094 manage_clocks = false;
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001095
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001096 if (manage_clocks) {
1097 error = sysc_enable_opt_clocks(ddata);
1098 if (error)
1099 return error;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001100
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001101 error = sysc_enable_main_clocks(ddata);
1102 if (error)
1103 goto err_opt_clocks;
Faiz Abbas596e7952018-07-09 22:18:39 +05301104 }
1105
Tony Lindgren566a9b052017-12-15 09:41:19 -08001106 ddata->revision = sysc_read_revision(ddata);
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001107 sysc_init_revision_quirks(ddata);
1108
Tony Lindgrena3e92e72019-03-21 11:00:21 -07001109 error = sysc_legacy_init(ddata);
1110 if (error)
1111 goto err_main_clocks;
1112
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001113 error = sysc_reset(ddata);
1114 if (error)
1115 dev_err(ddata->dev, "Reset failed with %d\n", error);
1116
Tony Lindgrena3e92e72019-03-21 11:00:21 -07001117err_main_clocks:
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001118 if (manage_clocks)
1119 sysc_disable_main_clocks(ddata);
1120err_opt_clocks:
1121 if (manage_clocks)
1122 sysc_disable_opt_clocks(ddata);
1123
1124 return error;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001125}
1126
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08001127static int sysc_init_sysc_mask(struct sysc *ddata)
1128{
1129 struct device_node *np = ddata->dev->of_node;
1130 int error;
1131 u32 val;
1132
1133 error = of_property_read_u32(np, "ti,sysc-mask", &val);
1134 if (error)
1135 return 0;
1136
1137 if (val)
1138 ddata->cfg.sysc_val = val & ddata->cap->sysc_mask;
1139 else
1140 ddata->cfg.sysc_val = ddata->cap->sysc_mask;
1141
1142 return 0;
1143}
1144
1145static int sysc_init_idlemode(struct sysc *ddata, u8 *idlemodes,
1146 const char *name)
1147{
1148 struct device_node *np = ddata->dev->of_node;
1149 struct property *prop;
1150 const __be32 *p;
1151 u32 val;
1152
1153 of_property_for_each_u32(np, name, prop, p, val) {
1154 if (val >= SYSC_NR_IDLEMODES) {
1155 dev_err(ddata->dev, "invalid idlemode: %i\n", val);
1156 return -EINVAL;
1157 }
1158 *idlemodes |= (1 << val);
1159 }
1160
1161 return 0;
1162}
1163
1164static int sysc_init_idlemodes(struct sysc *ddata)
1165{
1166 int error;
1167
1168 error = sysc_init_idlemode(ddata, &ddata->cfg.midlemodes,
1169 "ti,sysc-midle");
1170 if (error)
1171 return error;
1172
1173 error = sysc_init_idlemode(ddata, &ddata->cfg.sidlemodes,
1174 "ti,sysc-sidle");
1175 if (error)
1176 return error;
1177
1178 return 0;
1179}
1180
1181/*
1182 * Only some devices on omap4 and later have SYSCONFIG reset done
1183 * bit. We can detect this if there is no SYSSTATUS at all, or the
1184 * SYSTATUS bit 0 is not used. Note that some SYSSTATUS registers
1185 * have multiple bits for the child devices like OHCI and EHCI.
1186 * Depends on SYSC being parsed first.
1187 */
1188static int sysc_init_syss_mask(struct sysc *ddata)
1189{
1190 struct device_node *np = ddata->dev->of_node;
1191 int error;
1192 u32 val;
1193
1194 error = of_property_read_u32(np, "ti,syss-mask", &val);
1195 if (error) {
1196 if ((ddata->cap->type == TI_SYSC_OMAP4 ||
1197 ddata->cap->type == TI_SYSC_OMAP4_TIMER) &&
1198 (ddata->cfg.sysc_val & SYSC_OMAP4_SOFTRESET))
1199 ddata->cfg.quirks |= SYSC_QUIRK_RESET_STATUS;
1200
1201 return 0;
1202 }
1203
1204 if (!(val & 1) && (ddata->cfg.sysc_val & SYSC_OMAP4_SOFTRESET))
1205 ddata->cfg.quirks |= SYSC_QUIRK_RESET_STATUS;
1206
1207 ddata->cfg.syss_mask = val;
1208
1209 return 0;
1210}
1211
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001212/*
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001213 * Many child device drivers need to have fck and opt clocks available
1214 * to get the clock rate for device internal configuration etc.
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001215 */
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001216static int sysc_child_add_named_clock(struct sysc *ddata,
1217 struct device *child,
1218 const char *name)
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001219{
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001220 struct clk *clk;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001221 struct clk_lookup *l;
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001222 int error = 0;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001223
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001224 if (!name)
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001225 return 0;
1226
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001227 clk = clk_get(child, name);
1228 if (!IS_ERR(clk)) {
1229 clk_put(clk);
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001230
1231 return -EEXIST;
1232 }
1233
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001234 clk = clk_get(ddata->dev, name);
1235 if (IS_ERR(clk))
1236 return -ENODEV;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001237
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001238 l = clkdev_create(clk, name, dev_name(child));
1239 if (!l)
1240 error = -ENOMEM;
1241
1242 clk_put(clk);
1243
1244 return error;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001245}
1246
Tony Lindgren09dfe582018-04-16 10:25:52 -07001247static int sysc_child_add_clocks(struct sysc *ddata,
1248 struct device *child)
1249{
1250 int i, error;
1251
1252 for (i = 0; i < ddata->nr_clocks; i++) {
1253 error = sysc_child_add_named_clock(ddata,
1254 child,
1255 ddata->clock_roles[i]);
1256 if (error && error != -EEXIST) {
1257 dev_err(ddata->dev, "could not add child clock %s: %i\n",
1258 ddata->clock_roles[i], error);
1259
1260 return error;
1261 }
1262 }
1263
1264 return 0;
1265}
1266
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001267static struct device_type sysc_device_type = {
1268};
1269
1270static struct sysc *sysc_child_to_parent(struct device *dev)
1271{
1272 struct device *parent = dev->parent;
1273
1274 if (!parent || parent->type != &sysc_device_type)
1275 return NULL;
1276
1277 return dev_get_drvdata(parent);
1278}
1279
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001280static int __maybe_unused sysc_child_runtime_suspend(struct device *dev)
1281{
1282 struct sysc *ddata;
1283 int error;
1284
1285 ddata = sysc_child_to_parent(dev);
1286
1287 error = pm_generic_runtime_suspend(dev);
1288 if (error)
1289 return error;
1290
1291 if (!ddata->enabled)
1292 return 0;
1293
1294 return sysc_runtime_suspend(ddata->dev);
1295}
1296
1297static int __maybe_unused sysc_child_runtime_resume(struct device *dev)
1298{
1299 struct sysc *ddata;
1300 int error;
1301
1302 ddata = sysc_child_to_parent(dev);
1303
1304 if (!ddata->enabled) {
1305 error = sysc_runtime_resume(ddata->dev);
1306 if (error < 0)
1307 dev_err(ddata->dev,
1308 "%s error: %i\n", __func__, error);
1309 }
1310
1311 return pm_generic_runtime_resume(dev);
1312}
1313
1314#ifdef CONFIG_PM_SLEEP
1315static int sysc_child_suspend_noirq(struct device *dev)
1316{
1317 struct sysc *ddata;
1318 int error;
1319
1320 ddata = sysc_child_to_parent(dev);
1321
Tony Lindgrenef55f822018-04-16 10:27:15 -07001322 dev_dbg(ddata->dev, "%s %s\n", __func__,
1323 ddata->name ? ddata->name : "");
1324
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001325 error = pm_generic_suspend_noirq(dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001326 if (error) {
1327 dev_err(dev, "%s error at %i: %i\n",
1328 __func__, __LINE__, error);
1329
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001330 return error;
Tony Lindgrenef55f822018-04-16 10:27:15 -07001331 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001332
1333 if (!pm_runtime_status_suspended(dev)) {
1334 error = pm_generic_runtime_suspend(dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001335 if (error) {
Tony Lindgrenf9490782018-09-28 15:21:50 -07001336 dev_dbg(dev, "%s busy at %i: %i\n",
1337 __func__, __LINE__, error);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001338
Tony Lindgren4f3530f2018-08-08 01:07:06 -07001339 return 0;
Tony Lindgrenef55f822018-04-16 10:27:15 -07001340 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001341
1342 error = sysc_runtime_suspend(ddata->dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001343 if (error) {
1344 dev_err(dev, "%s error at %i: %i\n",
1345 __func__, __LINE__, error);
1346
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001347 return error;
Tony Lindgrenef55f822018-04-16 10:27:15 -07001348 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001349
1350 ddata->child_needs_resume = true;
1351 }
1352
1353 return 0;
1354}
1355
1356static int sysc_child_resume_noirq(struct device *dev)
1357{
1358 struct sysc *ddata;
1359 int error;
1360
1361 ddata = sysc_child_to_parent(dev);
1362
Tony Lindgrenef55f822018-04-16 10:27:15 -07001363 dev_dbg(ddata->dev, "%s %s\n", __func__,
1364 ddata->name ? ddata->name : "");
1365
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001366 if (ddata->child_needs_resume) {
1367 ddata->child_needs_resume = false;
1368
1369 error = sysc_runtime_resume(ddata->dev);
1370 if (error)
1371 dev_err(ddata->dev,
1372 "%s runtime resume error: %i\n",
1373 __func__, error);
1374
1375 error = pm_generic_runtime_resume(dev);
1376 if (error)
1377 dev_err(ddata->dev,
1378 "%s generic runtime resume: %i\n",
1379 __func__, error);
1380 }
1381
1382 return pm_generic_resume_noirq(dev);
1383}
1384#endif
1385
Tony Lindgrenb7182b42019-03-21 11:00:21 -07001386static struct dev_pm_domain sysc_child_pm_domain = {
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001387 .ops = {
1388 SET_RUNTIME_PM_OPS(sysc_child_runtime_suspend,
1389 sysc_child_runtime_resume,
1390 NULL)
1391 USE_PLATFORM_PM_SLEEP_OPS
1392 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(sysc_child_suspend_noirq,
1393 sysc_child_resume_noirq)
1394 }
1395};
1396
1397/**
1398 * sysc_legacy_idle_quirk - handle children in omap_device compatible way
1399 * @ddata: device driver data
1400 * @child: child device driver
1401 *
1402 * Allow idle for child devices as done with _od_runtime_suspend().
1403 * Otherwise many child devices will not idle because of the permanent
1404 * parent usecount set in pm_runtime_irq_safe().
1405 *
1406 * Note that the long term solution is to just modify the child device
1407 * drivers to not set pm_runtime_irq_safe() and then this can be just
1408 * dropped.
1409 */
1410static void sysc_legacy_idle_quirk(struct sysc *ddata, struct device *child)
1411{
1412 if (!ddata->legacy_mode)
1413 return;
1414
1415 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
1416 dev_pm_domain_set(child, &sysc_child_pm_domain);
1417}
1418
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001419static int sysc_notifier_call(struct notifier_block *nb,
1420 unsigned long event, void *device)
1421{
1422 struct device *dev = device;
1423 struct sysc *ddata;
1424 int error;
1425
1426 ddata = sysc_child_to_parent(dev);
1427 if (!ddata)
1428 return NOTIFY_DONE;
1429
1430 switch (event) {
1431 case BUS_NOTIFY_ADD_DEVICE:
Tony Lindgren09dfe582018-04-16 10:25:52 -07001432 error = sysc_child_add_clocks(ddata, dev);
1433 if (error)
1434 return error;
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001435 sysc_legacy_idle_quirk(ddata, dev);
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001436 break;
1437 default:
1438 break;
1439 }
1440
1441 return NOTIFY_DONE;
1442}
1443
1444static struct notifier_block sysc_nb = {
1445 .notifier_call = sysc_notifier_call,
1446};
1447
Tony Lindgren566a9b052017-12-15 09:41:19 -08001448/* Device tree configured quirks */
1449struct sysc_dts_quirk {
1450 const char *name;
1451 u32 mask;
1452};
1453
1454static const struct sysc_dts_quirk sysc_dts_quirks[] = {
1455 { .name = "ti,no-idle-on-init",
1456 .mask = SYSC_QUIRK_NO_IDLE_ON_INIT, },
1457 { .name = "ti,no-reset-on-init",
1458 .mask = SYSC_QUIRK_NO_RESET_ON_INIT, },
Tony Lindgren386cb762019-03-22 07:49:30 -07001459 { .name = "ti,no-idle",
1460 .mask = SYSC_QUIRK_NO_IDLE, },
Tony Lindgren566a9b052017-12-15 09:41:19 -08001461};
1462
Tony Lindgren4014c082018-12-10 14:11:26 -08001463static void sysc_parse_dts_quirks(struct sysc *ddata, struct device_node *np,
1464 bool is_child)
Tony Lindgren566a9b052017-12-15 09:41:19 -08001465{
Tony Lindgren566a9b052017-12-15 09:41:19 -08001466 const struct property *prop;
Tony Lindgren4014c082018-12-10 14:11:26 -08001467 int i, len;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001468
1469 for (i = 0; i < ARRAY_SIZE(sysc_dts_quirks); i++) {
Tony Lindgren4014c082018-12-10 14:11:26 -08001470 const char *name = sysc_dts_quirks[i].name;
1471
1472 prop = of_get_property(np, name, &len);
Tony Lindgren566a9b052017-12-15 09:41:19 -08001473 if (!prop)
Tony Lindgrend39b6ea2018-02-15 09:18:55 -08001474 continue;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001475
1476 ddata->cfg.quirks |= sysc_dts_quirks[i].mask;
Tony Lindgren4014c082018-12-10 14:11:26 -08001477 if (is_child) {
1478 dev_warn(ddata->dev,
1479 "dts flag should be at module level for %s\n",
1480 name);
1481 }
Tony Lindgren566a9b052017-12-15 09:41:19 -08001482 }
Tony Lindgren4014c082018-12-10 14:11:26 -08001483}
Tony Lindgren566a9b052017-12-15 09:41:19 -08001484
Tony Lindgren4014c082018-12-10 14:11:26 -08001485static int sysc_init_dts_quirks(struct sysc *ddata)
1486{
1487 struct device_node *np = ddata->dev->of_node;
1488 int error;
1489 u32 val;
1490
1491 ddata->legacy_mode = of_get_property(np, "ti,hwmods", NULL);
1492
1493 sysc_parse_dts_quirks(ddata, np, false);
Tony Lindgren566a9b052017-12-15 09:41:19 -08001494 error = of_property_read_u32(np, "ti,sysc-delay-us", &val);
1495 if (!error) {
1496 if (val > 255) {
1497 dev_warn(ddata->dev, "bad ti,sysc-delay-us: %i\n",
1498 val);
1499 }
1500
1501 ddata->cfg.srst_udelay = (u8)val;
1502 }
1503
1504 return 0;
1505}
1506
Tony Lindgren0eecc632017-10-10 14:23:43 -07001507static void sysc_unprepare(struct sysc *ddata)
1508{
1509 int i;
1510
Tony Lindgrenaaa29bb2019-03-21 11:00:21 -07001511 if (!ddata->clocks)
1512 return;
1513
Tony Lindgren0eecc632017-10-10 14:23:43 -07001514 for (i = 0; i < SYSC_MAX_CLOCKS; i++) {
1515 if (!IS_ERR_OR_NULL(ddata->clocks[i]))
1516 clk_unprepare(ddata->clocks[i]);
1517 }
1518}
1519
Tony Lindgren70a65242017-12-15 09:41:09 -08001520/*
1521 * Common sysc register bits found on omap2, also known as type1
1522 */
1523static const struct sysc_regbits sysc_regbits_omap2 = {
1524 .dmadisable_shift = -ENODEV,
1525 .midle_shift = 12,
1526 .sidle_shift = 3,
1527 .clkact_shift = 8,
1528 .emufree_shift = 5,
1529 .enwkup_shift = 2,
1530 .srst_shift = 1,
1531 .autoidle_shift = 0,
1532};
1533
1534static const struct sysc_capabilities sysc_omap2 = {
1535 .type = TI_SYSC_OMAP2,
1536 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY | SYSC_OMAP2_EMUFREE |
1537 SYSC_OMAP2_ENAWAKEUP | SYSC_OMAP2_SOFTRESET |
1538 SYSC_OMAP2_AUTOIDLE,
1539 .regbits = &sysc_regbits_omap2,
1540};
1541
1542/* All omap2 and 3 timers, and timers 1, 2 & 10 on omap 4 and 5 */
1543static const struct sysc_capabilities sysc_omap2_timer = {
1544 .type = TI_SYSC_OMAP2_TIMER,
1545 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY | SYSC_OMAP2_EMUFREE |
1546 SYSC_OMAP2_ENAWAKEUP | SYSC_OMAP2_SOFTRESET |
1547 SYSC_OMAP2_AUTOIDLE,
1548 .regbits = &sysc_regbits_omap2,
1549 .mod_quirks = SYSC_QUIRK_USE_CLOCKACT,
1550};
1551
1552/*
1553 * SHAM2 (SHA1/MD5) sysc found on omap3, a variant of sysc_regbits_omap2
1554 * with different sidle position
1555 */
1556static const struct sysc_regbits sysc_regbits_omap3_sham = {
1557 .dmadisable_shift = -ENODEV,
1558 .midle_shift = -ENODEV,
1559 .sidle_shift = 4,
1560 .clkact_shift = -ENODEV,
1561 .enwkup_shift = -ENODEV,
1562 .srst_shift = 1,
1563 .autoidle_shift = 0,
1564 .emufree_shift = -ENODEV,
1565};
1566
1567static const struct sysc_capabilities sysc_omap3_sham = {
1568 .type = TI_SYSC_OMAP3_SHAM,
1569 .sysc_mask = SYSC_OMAP2_SOFTRESET | SYSC_OMAP2_AUTOIDLE,
1570 .regbits = &sysc_regbits_omap3_sham,
1571};
1572
1573/*
1574 * AES register bits found on omap3 and later, a variant of
1575 * sysc_regbits_omap2 with different sidle position
1576 */
1577static const struct sysc_regbits sysc_regbits_omap3_aes = {
1578 .dmadisable_shift = -ENODEV,
1579 .midle_shift = -ENODEV,
1580 .sidle_shift = 6,
1581 .clkact_shift = -ENODEV,
1582 .enwkup_shift = -ENODEV,
1583 .srst_shift = 1,
1584 .autoidle_shift = 0,
1585 .emufree_shift = -ENODEV,
1586};
1587
1588static const struct sysc_capabilities sysc_omap3_aes = {
1589 .type = TI_SYSC_OMAP3_AES,
1590 .sysc_mask = SYSC_OMAP2_SOFTRESET | SYSC_OMAP2_AUTOIDLE,
1591 .regbits = &sysc_regbits_omap3_aes,
1592};
1593
1594/*
1595 * Common sysc register bits found on omap4, also known as type2
1596 */
1597static const struct sysc_regbits sysc_regbits_omap4 = {
1598 .dmadisable_shift = 16,
1599 .midle_shift = 4,
1600 .sidle_shift = 2,
1601 .clkact_shift = -ENODEV,
1602 .enwkup_shift = -ENODEV,
1603 .emufree_shift = 1,
1604 .srst_shift = 0,
1605 .autoidle_shift = -ENODEV,
1606};
1607
1608static const struct sysc_capabilities sysc_omap4 = {
1609 .type = TI_SYSC_OMAP4,
1610 .sysc_mask = SYSC_OMAP4_DMADISABLE | SYSC_OMAP4_FREEEMU |
1611 SYSC_OMAP4_SOFTRESET,
1612 .regbits = &sysc_regbits_omap4,
1613};
1614
1615static const struct sysc_capabilities sysc_omap4_timer = {
1616 .type = TI_SYSC_OMAP4_TIMER,
1617 .sysc_mask = SYSC_OMAP4_DMADISABLE | SYSC_OMAP4_FREEEMU |
1618 SYSC_OMAP4_SOFTRESET,
1619 .regbits = &sysc_regbits_omap4,
1620};
1621
1622/*
1623 * Common sysc register bits found on omap4, also known as type3
1624 */
1625static const struct sysc_regbits sysc_regbits_omap4_simple = {
1626 .dmadisable_shift = -ENODEV,
1627 .midle_shift = 2,
1628 .sidle_shift = 0,
1629 .clkact_shift = -ENODEV,
1630 .enwkup_shift = -ENODEV,
1631 .srst_shift = -ENODEV,
1632 .emufree_shift = -ENODEV,
1633 .autoidle_shift = -ENODEV,
1634};
1635
1636static const struct sysc_capabilities sysc_omap4_simple = {
1637 .type = TI_SYSC_OMAP4_SIMPLE,
1638 .regbits = &sysc_regbits_omap4_simple,
1639};
1640
1641/*
1642 * SmartReflex sysc found on omap34xx
1643 */
1644static const struct sysc_regbits sysc_regbits_omap34xx_sr = {
1645 .dmadisable_shift = -ENODEV,
1646 .midle_shift = -ENODEV,
1647 .sidle_shift = -ENODEV,
1648 .clkact_shift = 20,
1649 .enwkup_shift = -ENODEV,
1650 .srst_shift = -ENODEV,
1651 .emufree_shift = -ENODEV,
1652 .autoidle_shift = -ENODEV,
1653};
1654
1655static const struct sysc_capabilities sysc_34xx_sr = {
1656 .type = TI_SYSC_OMAP34XX_SR,
1657 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY,
1658 .regbits = &sysc_regbits_omap34xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001659 .mod_quirks = SYSC_QUIRK_USE_CLOCKACT | SYSC_QUIRK_UNCACHED |
1660 SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08001661};
1662
1663/*
1664 * SmartReflex sysc found on omap36xx and later
1665 */
1666static const struct sysc_regbits sysc_regbits_omap36xx_sr = {
1667 .dmadisable_shift = -ENODEV,
1668 .midle_shift = -ENODEV,
1669 .sidle_shift = 24,
1670 .clkact_shift = -ENODEV,
1671 .enwkup_shift = 26,
1672 .srst_shift = -ENODEV,
1673 .emufree_shift = -ENODEV,
1674 .autoidle_shift = -ENODEV,
1675};
1676
1677static const struct sysc_capabilities sysc_36xx_sr = {
1678 .type = TI_SYSC_OMAP36XX_SR,
Tony Lindgren3267c082018-01-22 09:32:53 -08001679 .sysc_mask = SYSC_OMAP3_SR_ENAWAKEUP,
Tony Lindgren70a65242017-12-15 09:41:09 -08001680 .regbits = &sysc_regbits_omap36xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001681 .mod_quirks = SYSC_QUIRK_UNCACHED | SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08001682};
1683
1684static const struct sysc_capabilities sysc_omap4_sr = {
1685 .type = TI_SYSC_OMAP4_SR,
1686 .regbits = &sysc_regbits_omap36xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001687 .mod_quirks = SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08001688};
1689
1690/*
1691 * McASP register bits found on omap4 and later
1692 */
1693static const struct sysc_regbits sysc_regbits_omap4_mcasp = {
1694 .dmadisable_shift = -ENODEV,
1695 .midle_shift = -ENODEV,
1696 .sidle_shift = 0,
1697 .clkact_shift = -ENODEV,
1698 .enwkup_shift = -ENODEV,
1699 .srst_shift = -ENODEV,
1700 .emufree_shift = -ENODEV,
1701 .autoidle_shift = -ENODEV,
1702};
1703
1704static const struct sysc_capabilities sysc_omap4_mcasp = {
1705 .type = TI_SYSC_OMAP4_MCASP,
1706 .regbits = &sysc_regbits_omap4_mcasp,
Tony Lindgren2c63a832018-11-15 14:46:53 -08001707 .mod_quirks = SYSC_QUIRK_OPT_CLKS_NEEDED,
1708};
1709
1710/*
1711 * McASP found on dra7 and later
1712 */
1713static const struct sysc_capabilities sysc_dra7_mcasp = {
1714 .type = TI_SYSC_OMAP4_SIMPLE,
1715 .regbits = &sysc_regbits_omap4_simple,
1716 .mod_quirks = SYSC_QUIRK_OPT_CLKS_NEEDED,
Tony Lindgren70a65242017-12-15 09:41:09 -08001717};
1718
1719/*
1720 * FS USB host found on omap4 and later
1721 */
1722static const struct sysc_regbits sysc_regbits_omap4_usb_host_fs = {
1723 .dmadisable_shift = -ENODEV,
1724 .midle_shift = -ENODEV,
1725 .sidle_shift = 24,
1726 .clkact_shift = -ENODEV,
1727 .enwkup_shift = 26,
1728 .srst_shift = -ENODEV,
1729 .emufree_shift = -ENODEV,
1730 .autoidle_shift = -ENODEV,
1731};
1732
1733static const struct sysc_capabilities sysc_omap4_usb_host_fs = {
1734 .type = TI_SYSC_OMAP4_USB_HOST_FS,
1735 .sysc_mask = SYSC_OMAP2_ENAWAKEUP,
1736 .regbits = &sysc_regbits_omap4_usb_host_fs,
1737};
1738
Faiz Abbas7f35e632018-07-09 22:18:38 +05301739static const struct sysc_regbits sysc_regbits_dra7_mcan = {
1740 .dmadisable_shift = -ENODEV,
1741 .midle_shift = -ENODEV,
1742 .sidle_shift = -ENODEV,
1743 .clkact_shift = -ENODEV,
1744 .enwkup_shift = 4,
1745 .srst_shift = 0,
1746 .emufree_shift = -ENODEV,
1747 .autoidle_shift = -ENODEV,
1748};
1749
1750static const struct sysc_capabilities sysc_dra7_mcan = {
1751 .type = TI_SYSC_DRA7_MCAN,
1752 .sysc_mask = SYSC_DRA7_MCAN_ENAWAKEUP | SYSC_OMAP4_SOFTRESET,
1753 .regbits = &sysc_regbits_dra7_mcan,
1754};
1755
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001756static int sysc_init_pdata(struct sysc *ddata)
1757{
1758 struct ti_sysc_platform_data *pdata = dev_get_platdata(ddata->dev);
Tony Lindgrena3e92e72019-03-21 11:00:21 -07001759 struct ti_sysc_module_data *mdata;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001760
1761 if (!pdata || !ddata->legacy_mode)
1762 return 0;
1763
Tony Lindgrena3e92e72019-03-21 11:00:21 -07001764 mdata = devm_kzalloc(ddata->dev, sizeof(*mdata), GFP_KERNEL);
1765 if (!mdata)
1766 return -ENOMEM;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001767
Tony Lindgrena3e92e72019-03-21 11:00:21 -07001768 mdata->name = ddata->legacy_mode;
1769 mdata->module_pa = ddata->module_pa;
1770 mdata->module_size = ddata->module_size;
1771 mdata->offsets = ddata->offsets;
1772 mdata->nr_offsets = SYSC_MAX_REGS;
1773 mdata->cap = ddata->cap;
1774 mdata->cfg = &ddata->cfg;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001775
Tony Lindgrena3e92e72019-03-21 11:00:21 -07001776 ddata->mdata = mdata;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001777
Tony Lindgrena3e92e72019-03-21 11:00:21 -07001778 return 0;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001779}
1780
Tony Lindgren70a65242017-12-15 09:41:09 -08001781static int sysc_init_match(struct sysc *ddata)
1782{
1783 const struct sysc_capabilities *cap;
1784
1785 cap = of_device_get_match_data(ddata->dev);
1786 if (!cap)
1787 return -EINVAL;
1788
1789 ddata->cap = cap;
1790 if (ddata->cap)
1791 ddata->cfg.quirks |= ddata->cap->mod_quirks;
1792
1793 return 0;
1794}
1795
Tony Lindgren76f0f772018-02-23 08:28:45 -08001796static void ti_sysc_idle(struct work_struct *work)
1797{
1798 struct sysc *ddata;
1799
1800 ddata = container_of(work, struct sysc, idle_work.work);
1801
1802 if (pm_runtime_active(ddata->dev))
1803 pm_runtime_put_sync(ddata->dev);
1804}
1805
Tony Lindgrenc4bebea2018-04-16 10:24:54 -07001806static const struct of_device_id sysc_match_table[] = {
1807 { .compatible = "simple-bus", },
1808 { /* sentinel */ },
1809};
1810
Tony Lindgren0eecc632017-10-10 14:23:43 -07001811static int sysc_probe(struct platform_device *pdev)
1812{
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001813 struct ti_sysc_platform_data *pdata = dev_get_platdata(&pdev->dev);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001814 struct sysc *ddata;
1815 int error;
1816
1817 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
1818 if (!ddata)
1819 return -ENOMEM;
1820
1821 ddata->dev = &pdev->dev;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001822 platform_set_drvdata(pdev, ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001823
Tony Lindgren70a65242017-12-15 09:41:09 -08001824 error = sysc_init_match(ddata);
1825 if (error)
1826 return error;
1827
Tony Lindgren566a9b052017-12-15 09:41:19 -08001828 error = sysc_init_dts_quirks(ddata);
1829 if (error)
1830 goto unprepare;
1831
Tony Lindgren0eecc632017-10-10 14:23:43 -07001832 error = sysc_get_clocks(ddata);
1833 if (error)
1834 return error;
1835
1836 error = sysc_map_and_check_registers(ddata);
1837 if (error)
1838 goto unprepare;
1839
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08001840 error = sysc_init_sysc_mask(ddata);
1841 if (error)
1842 goto unprepare;
1843
1844 error = sysc_init_idlemodes(ddata);
1845 if (error)
1846 goto unprepare;
1847
1848 error = sysc_init_syss_mask(ddata);
1849 if (error)
1850 goto unprepare;
1851
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001852 error = sysc_init_pdata(ddata);
1853 if (error)
1854 goto unprepare;
1855
Tony Lindgren50622362018-04-16 10:20:27 -07001856 error = sysc_init_resets(ddata);
1857 if (error)
1858 return error;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001859
1860 error = sysc_init_module(ddata);
1861 if (error)
1862 goto unprepare;
1863
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001864 pm_runtime_enable(ddata->dev);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001865 error = pm_runtime_get_sync(ddata->dev);
1866 if (error < 0) {
1867 pm_runtime_put_noidle(ddata->dev);
1868 pm_runtime_disable(ddata->dev);
1869 goto unprepare;
1870 }
1871
Tony Lindgren0eecc632017-10-10 14:23:43 -07001872 sysc_show_registers(ddata);
1873
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001874 ddata->dev->type = &sysc_device_type;
Tony Lindgrenc4bebea2018-04-16 10:24:54 -07001875 error = of_platform_populate(ddata->dev->of_node, sysc_match_table,
1876 pdata ? pdata->auxdata : NULL,
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001877 ddata->dev);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001878 if (error)
1879 goto err;
1880
Tony Lindgren76f0f772018-02-23 08:28:45 -08001881 INIT_DELAYED_WORK(&ddata->idle_work, ti_sysc_idle);
1882
1883 /* At least earlycon won't survive without deferred idle */
1884 if (ddata->cfg.quirks & (SYSC_QUIRK_NO_IDLE_ON_INIT |
1885 SYSC_QUIRK_NO_RESET_ON_INIT)) {
1886 schedule_delayed_work(&ddata->idle_work, 3000);
1887 } else {
1888 pm_runtime_put(&pdev->dev);
1889 }
Tony Lindgren0eecc632017-10-10 14:23:43 -07001890
Tony Lindgren50622362018-04-16 10:20:27 -07001891 if (!of_get_available_child_count(ddata->dev->of_node))
1892 reset_control_assert(ddata->rsts);
1893
Tony Lindgren0eecc632017-10-10 14:23:43 -07001894 return 0;
1895
1896err:
Tony Lindgren0eecc632017-10-10 14:23:43 -07001897 pm_runtime_put_sync(&pdev->dev);
1898 pm_runtime_disable(&pdev->dev);
1899unprepare:
1900 sysc_unprepare(ddata);
1901
1902 return error;
1903}
1904
Tony Lindgren684be5a2017-10-13 10:48:40 -07001905static int sysc_remove(struct platform_device *pdev)
1906{
1907 struct sysc *ddata = platform_get_drvdata(pdev);
1908 int error;
1909
Tony Lindgren76f0f772018-02-23 08:28:45 -08001910 cancel_delayed_work_sync(&ddata->idle_work);
1911
Tony Lindgren684be5a2017-10-13 10:48:40 -07001912 error = pm_runtime_get_sync(ddata->dev);
1913 if (error < 0) {
1914 pm_runtime_put_noidle(ddata->dev);
1915 pm_runtime_disable(ddata->dev);
1916 goto unprepare;
1917 }
1918
1919 of_platform_depopulate(&pdev->dev);
1920
Tony Lindgren684be5a2017-10-13 10:48:40 -07001921 pm_runtime_put_sync(&pdev->dev);
1922 pm_runtime_disable(&pdev->dev);
Tony Lindgren50622362018-04-16 10:20:27 -07001923 reset_control_assert(ddata->rsts);
Tony Lindgren684be5a2017-10-13 10:48:40 -07001924
1925unprepare:
1926 sysc_unprepare(ddata);
1927
1928 return 0;
1929}
1930
Tony Lindgren0eecc632017-10-10 14:23:43 -07001931static const struct of_device_id sysc_match[] = {
Tony Lindgren70a65242017-12-15 09:41:09 -08001932 { .compatible = "ti,sysc-omap2", .data = &sysc_omap2, },
1933 { .compatible = "ti,sysc-omap2-timer", .data = &sysc_omap2_timer, },
1934 { .compatible = "ti,sysc-omap4", .data = &sysc_omap4, },
1935 { .compatible = "ti,sysc-omap4-timer", .data = &sysc_omap4_timer, },
1936 { .compatible = "ti,sysc-omap4-simple", .data = &sysc_omap4_simple, },
1937 { .compatible = "ti,sysc-omap3430-sr", .data = &sysc_34xx_sr, },
1938 { .compatible = "ti,sysc-omap3630-sr", .data = &sysc_36xx_sr, },
1939 { .compatible = "ti,sysc-omap4-sr", .data = &sysc_omap4_sr, },
1940 { .compatible = "ti,sysc-omap3-sham", .data = &sysc_omap3_sham, },
1941 { .compatible = "ti,sysc-omap-aes", .data = &sysc_omap3_aes, },
1942 { .compatible = "ti,sysc-mcasp", .data = &sysc_omap4_mcasp, },
Tony Lindgren2c63a832018-11-15 14:46:53 -08001943 { .compatible = "ti,sysc-dra7-mcasp", .data = &sysc_dra7_mcasp, },
Tony Lindgren70a65242017-12-15 09:41:09 -08001944 { .compatible = "ti,sysc-usb-host-fs",
1945 .data = &sysc_omap4_usb_host_fs, },
Faiz Abbas7f35e632018-07-09 22:18:38 +05301946 { .compatible = "ti,sysc-dra7-mcan", .data = &sysc_dra7_mcan, },
Tony Lindgren0eecc632017-10-10 14:23:43 -07001947 { },
1948};
1949MODULE_DEVICE_TABLE(of, sysc_match);
1950
1951static struct platform_driver sysc_driver = {
1952 .probe = sysc_probe,
Tony Lindgren684be5a2017-10-13 10:48:40 -07001953 .remove = sysc_remove,
Tony Lindgren0eecc632017-10-10 14:23:43 -07001954 .driver = {
1955 .name = "ti-sysc",
1956 .of_match_table = sysc_match,
1957 .pm = &sysc_pm_ops,
1958 },
1959};
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001960
1961static int __init sysc_init(void)
1962{
1963 bus_register_notifier(&platform_bus_type, &sysc_nb);
1964
1965 return platform_driver_register(&sysc_driver);
1966}
1967module_init(sysc_init);
1968
1969static void __exit sysc_exit(void)
1970{
1971 bus_unregister_notifier(&platform_bus_type, &sysc_nb);
1972 platform_driver_unregister(&sysc_driver);
1973}
1974module_exit(sysc_exit);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001975
1976MODULE_DESCRIPTION("TI sysc interconnect target driver");
1977MODULE_LICENSE("GPL v2");