blob: 647caccacee684c374630784d5a50cb01b2bc68f [file] [log] [blame]
Tony Lindgren0eecc632017-10-10 14:23:43 -07001/*
2 * ti-sysc.c - Texas Instruments sysc interconnect target driver
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
9 * kind, whether express or implied; without even the implied warranty
10 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#include <linux/io.h>
15#include <linux/clk.h>
Tony Lindgren2c355ff2018-02-22 13:58:03 -080016#include <linux/clkdev.h>
Tony Lindgrena885f0f2018-02-22 14:03:48 -080017#include <linux/delay.h>
Tony Lindgren0eecc632017-10-10 14:23:43 -070018#include <linux/module.h>
19#include <linux/platform_device.h>
Tony Lindgrena885f0f2018-02-22 14:03:48 -080020#include <linux/pm_domain.h>
Tony Lindgren0eecc632017-10-10 14:23:43 -070021#include <linux/pm_runtime.h>
Tony Lindgren50622362018-04-16 10:20:27 -070022#include <linux/reset.h>
Tony Lindgren0eecc632017-10-10 14:23:43 -070023#include <linux/of_address.h>
24#include <linux/of_platform.h>
Tony Lindgren2c355ff2018-02-22 13:58:03 -080025#include <linux/slab.h>
Faiz Abbas596e7952018-07-09 22:18:39 +053026#include <linux/iopoll.h>
Tony Lindgren2c355ff2018-02-22 13:58:03 -080027
Tony Lindgren70a65242017-12-15 09:41:09 -080028#include <linux/platform_data/ti-sysc.h>
29
30#include <dt-bindings/bus/ti-sysc.h>
Tony Lindgren0eecc632017-10-10 14:23:43 -070031
Faiz Abbas596e7952018-07-09 22:18:39 +053032#define MAX_MODULE_SOFTRESET_WAIT 10000
33
Tony Lindgren0eecc632017-10-10 14:23:43 -070034static const char * const reg_names[] = { "rev", "sysc", "syss", };
35
36enum sysc_clocks {
37 SYSC_FCK,
38 SYSC_ICK,
Tony Lindgren09dfe582018-04-16 10:25:52 -070039 SYSC_OPTFCK0,
40 SYSC_OPTFCK1,
41 SYSC_OPTFCK2,
42 SYSC_OPTFCK3,
43 SYSC_OPTFCK4,
44 SYSC_OPTFCK5,
45 SYSC_OPTFCK6,
46 SYSC_OPTFCK7,
Tony Lindgren0eecc632017-10-10 14:23:43 -070047 SYSC_MAX_CLOCKS,
48};
49
Tony Lindgren09dfe582018-04-16 10:25:52 -070050static const char * const clock_names[SYSC_ICK + 1] = { "fck", "ick", };
Tony Lindgren0eecc632017-10-10 14:23:43 -070051
Tony Lindgrenc5a2de92017-12-15 09:41:23 -080052#define SYSC_IDLEMODE_MASK 3
53#define SYSC_CLOCKACTIVITY_MASK 3
54
Tony Lindgren0eecc632017-10-10 14:23:43 -070055/**
56 * struct sysc - TI sysc interconnect target module registers and capabilities
57 * @dev: struct device pointer
58 * @module_pa: physical address of the interconnect target module
59 * @module_size: size of the interconnect target module
60 * @module_va: virtual address of the interconnect target module
61 * @offsets: register offsets from module base
62 * @clocks: clocks used by the interconnect target module
Tony Lindgren09dfe582018-04-16 10:25:52 -070063 * @clock_roles: clock role names for the found clocks
64 * @nr_clocks: number of clocks used by the interconnect target module
Tony Lindgren0eecc632017-10-10 14:23:43 -070065 * @legacy_mode: configured for legacy mode if set
Tony Lindgren70a65242017-12-15 09:41:09 -080066 * @cap: interconnect target module capabilities
67 * @cfg: interconnect target module configuration
Tony Lindgren566a9b052017-12-15 09:41:19 -080068 * @name: name if available
69 * @revision: interconnect target module revision
Tony Lindgren62020f22018-02-22 13:59:44 -080070 * @needs_resume: runtime resume needed on resume from suspend
Tony Lindgren0eecc632017-10-10 14:23:43 -070071 */
72struct sysc {
73 struct device *dev;
74 u64 module_pa;
75 u32 module_size;
76 void __iomem *module_va;
77 int offsets[SYSC_MAX_REGS];
Tony Lindgren09dfe582018-04-16 10:25:52 -070078 struct clk **clocks;
79 const char **clock_roles;
80 int nr_clocks;
Tony Lindgren50622362018-04-16 10:20:27 -070081 struct reset_control *rsts;
Tony Lindgren0eecc632017-10-10 14:23:43 -070082 const char *legacy_mode;
Tony Lindgren70a65242017-12-15 09:41:09 -080083 const struct sysc_capabilities *cap;
84 struct sysc_config cfg;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -080085 struct ti_sysc_cookie cookie;
Tony Lindgren566a9b052017-12-15 09:41:19 -080086 const char *name;
87 u32 revision;
Tony Lindgren62020f22018-02-22 13:59:44 -080088 bool enabled;
89 bool needs_resume;
Tony Lindgrena885f0f2018-02-22 14:03:48 -080090 bool child_needs_resume;
Tony Lindgren76f0f772018-02-23 08:28:45 -080091 struct delayed_work idle_work;
Tony Lindgren0eecc632017-10-10 14:23:43 -070092};
93
Tony Lindgren4014c082018-12-10 14:11:26 -080094static void sysc_parse_dts_quirks(struct sysc *ddata, struct device_node *np,
95 bool is_child);
96
Faiz Abbas596e7952018-07-09 22:18:39 +053097void sysc_write(struct sysc *ddata, int offset, u32 value)
98{
99 writel_relaxed(value, ddata->module_va + offset);
100}
101
Tony Lindgren566a9b052017-12-15 09:41:19 -0800102static u32 sysc_read(struct sysc *ddata, int offset)
103{
104 if (ddata->cfg.quirks & SYSC_QUIRK_16BIT) {
105 u32 val;
106
107 val = readw_relaxed(ddata->module_va + offset);
108 val |= (readw_relaxed(ddata->module_va + offset + 4) << 16);
109
110 return val;
111 }
112
113 return readl_relaxed(ddata->module_va + offset);
114}
115
Tony Lindgren09dfe582018-04-16 10:25:52 -0700116static bool sysc_opt_clks_needed(struct sysc *ddata)
117{
118 return !!(ddata->cfg.quirks & SYSC_QUIRK_OPT_CLKS_NEEDED);
119}
120
Tony Lindgren0eecc632017-10-10 14:23:43 -0700121static u32 sysc_read_revision(struct sysc *ddata)
122{
Tony Lindgren566a9b052017-12-15 09:41:19 -0800123 int offset = ddata->offsets[SYSC_REVISION];
124
125 if (offset < 0)
126 return 0;
127
128 return sysc_read(ddata, offset);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700129}
130
Tony Lindgren09dfe582018-04-16 10:25:52 -0700131static int sysc_get_one_clock(struct sysc *ddata, const char *name)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700132{
Tony Lindgren09dfe582018-04-16 10:25:52 -0700133 int error, i, index = -ENODEV;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700134
Tony Lindgren09dfe582018-04-16 10:25:52 -0700135 if (!strncmp(clock_names[SYSC_FCK], name, 3))
136 index = SYSC_FCK;
137 else if (!strncmp(clock_names[SYSC_ICK], name, 3))
138 index = SYSC_ICK;
139
140 if (index < 0) {
141 for (i = SYSC_OPTFCK0; i < SYSC_MAX_CLOCKS; i++) {
Tony Lindgrenc97c8622018-05-18 07:54:44 -0700142 if (!ddata->clocks[i]) {
Tony Lindgren09dfe582018-04-16 10:25:52 -0700143 index = i;
144 break;
145 }
146 }
Tony Lindgren0eecc632017-10-10 14:23:43 -0700147 }
Tony Lindgren09dfe582018-04-16 10:25:52 -0700148
149 if (index < 0) {
150 dev_err(ddata->dev, "clock %s not added\n", name);
151 return index;
152 }
Tony Lindgren0eecc632017-10-10 14:23:43 -0700153
154 ddata->clocks[index] = devm_clk_get(ddata->dev, name);
155 if (IS_ERR(ddata->clocks[index])) {
156 if (PTR_ERR(ddata->clocks[index]) == -ENOENT)
157 return 0;
158
159 dev_err(ddata->dev, "clock get error for %s: %li\n",
160 name, PTR_ERR(ddata->clocks[index]));
161
162 return PTR_ERR(ddata->clocks[index]);
163 }
164
165 error = clk_prepare(ddata->clocks[index]);
166 if (error) {
167 dev_err(ddata->dev, "clock prepare error for %s: %i\n",
168 name, error);
169
170 return error;
171 }
172
173 return 0;
174}
175
176static int sysc_get_clocks(struct sysc *ddata)
177{
Tony Lindgren09dfe582018-04-16 10:25:52 -0700178 struct device_node *np = ddata->dev->of_node;
179 struct property *prop;
180 const char *name;
181 int nr_fck = 0, nr_ick = 0, i, error = 0;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700182
Kees Cook20749052018-06-18 10:45:49 -0700183 ddata->clock_roles = devm_kcalloc(ddata->dev,
Tony Lindgren09dfe582018-04-16 10:25:52 -0700184 SYSC_MAX_CLOCKS,
Kees Cook20749052018-06-18 10:45:49 -0700185 sizeof(*ddata->clock_roles),
Tony Lindgren09dfe582018-04-16 10:25:52 -0700186 GFP_KERNEL);
187 if (!ddata->clock_roles)
188 return -ENOMEM;
189
190 of_property_for_each_string(np, "clock-names", prop, name) {
191 if (!strncmp(clock_names[SYSC_FCK], name, 3))
192 nr_fck++;
193 if (!strncmp(clock_names[SYSC_ICK], name, 3))
194 nr_ick++;
195 ddata->clock_roles[ddata->nr_clocks] = name;
196 ddata->nr_clocks++;
197 }
198
199 if (ddata->nr_clocks < 1)
200 return 0;
201
202 if (ddata->nr_clocks > SYSC_MAX_CLOCKS) {
203 dev_err(ddata->dev, "too many clocks for %pOF\n", np);
204
205 return -EINVAL;
206 }
207
208 if (nr_fck > 1 || nr_ick > 1) {
209 dev_err(ddata->dev, "max one fck and ick for %pOF\n", np);
210
211 return -EINVAL;
212 }
213
Kees Cook20749052018-06-18 10:45:49 -0700214 ddata->clocks = devm_kcalloc(ddata->dev,
215 ddata->nr_clocks, sizeof(*ddata->clocks),
Tony Lindgren09dfe582018-04-16 10:25:52 -0700216 GFP_KERNEL);
217 if (!ddata->clocks)
218 return -ENOMEM;
219
Tony Lindgren7b4f8ac2018-11-15 14:46:52 -0800220 for (i = 0; i < SYSC_MAX_CLOCKS; i++) {
221 const char *name = ddata->clock_roles[i];
222
223 if (!name)
224 continue;
225
226 error = sysc_get_one_clock(ddata, name);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700227 if (error && error != -ENOENT)
228 return error;
229 }
230
231 return 0;
232}
233
234/**
Tony Lindgren50622362018-04-16 10:20:27 -0700235 * sysc_init_resets - reset module on init
236 * @ddata: device driver data
237 *
238 * A module can have both OCP softreset control and external rstctrl.
239 * If more complicated rstctrl resets are needed, please handle these
240 * directly from the child device driver and map only the module reset
241 * for the parent interconnect target module device.
242 *
243 * Automatic reset of the module on init can be skipped with the
244 * "ti,no-reset-on-init" device tree property.
245 */
246static int sysc_init_resets(struct sysc *ddata)
247{
248 int error;
249
250 ddata->rsts =
251 devm_reset_control_array_get_optional_exclusive(ddata->dev);
252 if (IS_ERR(ddata->rsts))
253 return PTR_ERR(ddata->rsts);
254
255 if (ddata->cfg.quirks & SYSC_QUIRK_NO_RESET_ON_INIT)
256 goto deassert;
257
258 error = reset_control_assert(ddata->rsts);
259 if (error)
260 return error;
261
262deassert:
263 error = reset_control_deassert(ddata->rsts);
264 if (error)
265 return error;
266
267 return 0;
268}
269
270/**
Tony Lindgren0eecc632017-10-10 14:23:43 -0700271 * sysc_parse_and_check_child_range - parses module IO region from ranges
272 * @ddata: device driver data
273 *
274 * In general we only need rev, syss, and sysc registers and not the whole
275 * module range. But we do want the offsets for these registers from the
276 * module base. This allows us to check them against the legacy hwmod
277 * platform data. Let's also check the ranges are configured properly.
278 */
279static int sysc_parse_and_check_child_range(struct sysc *ddata)
280{
281 struct device_node *np = ddata->dev->of_node;
282 const __be32 *ranges;
283 u32 nr_addr, nr_size;
284 int len, error;
285
286 ranges = of_get_property(np, "ranges", &len);
287 if (!ranges) {
288 dev_err(ddata->dev, "missing ranges for %pOF\n", np);
289
290 return -ENOENT;
291 }
292
293 len /= sizeof(*ranges);
294
295 if (len < 3) {
296 dev_err(ddata->dev, "incomplete ranges for %pOF\n", np);
297
298 return -EINVAL;
299 }
300
301 error = of_property_read_u32(np, "#address-cells", &nr_addr);
302 if (error)
303 return -ENOENT;
304
305 error = of_property_read_u32(np, "#size-cells", &nr_size);
306 if (error)
307 return -ENOENT;
308
309 if (nr_addr != 1 || nr_size != 1) {
310 dev_err(ddata->dev, "invalid ranges for %pOF\n", np);
311
312 return -EINVAL;
313 }
314
315 ranges++;
316 ddata->module_pa = of_translate_address(np, ranges++);
317 ddata->module_size = be32_to_cpup(ranges);
318
Tony Lindgren0eecc632017-10-10 14:23:43 -0700319 return 0;
320}
321
Tony Lindgren3bb37c82018-02-22 14:05:14 -0800322static struct device_node *stdout_path;
323
324static void sysc_init_stdout_path(struct sysc *ddata)
325{
326 struct device_node *np = NULL;
327 const char *uart;
328
329 if (IS_ERR(stdout_path))
330 return;
331
332 if (stdout_path)
333 return;
334
335 np = of_find_node_by_path("/chosen");
336 if (!np)
337 goto err;
338
339 uart = of_get_property(np, "stdout-path", NULL);
340 if (!uart)
341 goto err;
342
343 np = of_find_node_by_path(uart);
344 if (!np)
345 goto err;
346
347 stdout_path = np;
348
349 return;
350
351err:
352 stdout_path = ERR_PTR(-ENODEV);
353}
354
355static void sysc_check_quirk_stdout(struct sysc *ddata,
356 struct device_node *np)
357{
358 sysc_init_stdout_path(ddata);
359 if (np != stdout_path)
360 return;
361
362 ddata->cfg.quirks |= SYSC_QUIRK_NO_IDLE_ON_INIT |
363 SYSC_QUIRK_NO_RESET_ON_INIT;
364}
365
Tony Lindgren0eecc632017-10-10 14:23:43 -0700366/**
367 * sysc_check_one_child - check child configuration
368 * @ddata: device driver data
369 * @np: child device node
370 *
371 * Let's avoid messy situations where we have new interconnect target
372 * node but children have "ti,hwmods". These belong to the interconnect
373 * target node and are managed by this driver.
374 */
375static int sysc_check_one_child(struct sysc *ddata,
376 struct device_node *np)
377{
378 const char *name;
379
380 name = of_get_property(np, "ti,hwmods", NULL);
381 if (name)
382 dev_warn(ddata->dev, "really a child ti,hwmods property?");
383
Tony Lindgren3bb37c82018-02-22 14:05:14 -0800384 sysc_check_quirk_stdout(ddata, np);
Tony Lindgren4014c082018-12-10 14:11:26 -0800385 sysc_parse_dts_quirks(ddata, np, true);
Tony Lindgren3bb37c82018-02-22 14:05:14 -0800386
Tony Lindgren0eecc632017-10-10 14:23:43 -0700387 return 0;
388}
389
390static int sysc_check_children(struct sysc *ddata)
391{
392 struct device_node *child;
393 int error;
394
395 for_each_child_of_node(ddata->dev->of_node, child) {
396 error = sysc_check_one_child(ddata, child);
397 if (error)
398 return error;
399 }
400
401 return 0;
402}
403
Tony Lindgrena7199e22017-12-15 09:41:14 -0800404/*
405 * So far only I2C uses 16-bit read access with clockactivity with revision
406 * in two registers with stride of 4. We can detect this based on the rev
407 * register size to configure things far enough to be able to properly read
408 * the revision register.
409 */
410static void sysc_check_quirk_16bit(struct sysc *ddata, struct resource *res)
411{
Tony Lindgrendd57ac12018-02-22 14:09:57 -0800412 if (resource_size(res) == 8)
Tony Lindgrena7199e22017-12-15 09:41:14 -0800413 ddata->cfg.quirks |= SYSC_QUIRK_16BIT | SYSC_QUIRK_USE_CLOCKACT;
Tony Lindgrena7199e22017-12-15 09:41:14 -0800414}
415
Tony Lindgren0eecc632017-10-10 14:23:43 -0700416/**
417 * sysc_parse_one - parses the interconnect target module registers
418 * @ddata: device driver data
419 * @reg: register to parse
420 */
421static int sysc_parse_one(struct sysc *ddata, enum sysc_registers reg)
422{
423 struct resource *res;
424 const char *name;
425
426 switch (reg) {
427 case SYSC_REVISION:
428 case SYSC_SYSCONFIG:
429 case SYSC_SYSSTATUS:
430 name = reg_names[reg];
431 break;
432 default:
433 return -EINVAL;
434 }
435
436 res = platform_get_resource_byname(to_platform_device(ddata->dev),
437 IORESOURCE_MEM, name);
438 if (!res) {
Tony Lindgren0eecc632017-10-10 14:23:43 -0700439 ddata->offsets[reg] = -ENODEV;
440
441 return 0;
442 }
443
444 ddata->offsets[reg] = res->start - ddata->module_pa;
Tony Lindgrena7199e22017-12-15 09:41:14 -0800445 if (reg == SYSC_REVISION)
446 sysc_check_quirk_16bit(ddata, res);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700447
448 return 0;
449}
450
451static int sysc_parse_registers(struct sysc *ddata)
452{
453 int i, error;
454
455 for (i = 0; i < SYSC_MAX_REGS; i++) {
456 error = sysc_parse_one(ddata, i);
457 if (error)
458 return error;
459 }
460
461 return 0;
462}
463
464/**
465 * sysc_check_registers - check for misconfigured register overlaps
466 * @ddata: device driver data
467 */
468static int sysc_check_registers(struct sysc *ddata)
469{
470 int i, j, nr_regs = 0, nr_matches = 0;
471
472 for (i = 0; i < SYSC_MAX_REGS; i++) {
473 if (ddata->offsets[i] < 0)
474 continue;
475
476 if (ddata->offsets[i] > (ddata->module_size - 4)) {
477 dev_err(ddata->dev, "register outside module range");
478
479 return -EINVAL;
480 }
481
482 for (j = 0; j < SYSC_MAX_REGS; j++) {
483 if (ddata->offsets[j] < 0)
484 continue;
485
486 if (ddata->offsets[i] == ddata->offsets[j])
487 nr_matches++;
488 }
489 nr_regs++;
490 }
491
492 if (nr_regs < 1) {
493 dev_err(ddata->dev, "missing registers\n");
494
495 return -EINVAL;
496 }
497
498 if (nr_matches > nr_regs) {
499 dev_err(ddata->dev, "overlapping registers: (%i/%i)",
500 nr_regs, nr_matches);
501
502 return -EINVAL;
503 }
504
505 return 0;
506}
507
508/**
509 * syc_ioremap - ioremap register space for the interconnect target module
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700510 * @ddata: device driver data
Tony Lindgren0eecc632017-10-10 14:23:43 -0700511 *
512 * Note that the interconnect target module registers can be anywhere
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700513 * within the interconnect target module range. For example, SGX has
514 * them at offset 0x1fc00 in the 32MB module address space. And cpsw
515 * has them at offset 0x1200 in the CPSW_WR child. Usually the
516 * the interconnect target module registers are at the beginning of
517 * the module range though.
Tony Lindgren0eecc632017-10-10 14:23:43 -0700518 */
519static int sysc_ioremap(struct sysc *ddata)
520{
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700521 int size;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700522
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700523 size = max3(ddata->offsets[SYSC_REVISION],
524 ddata->offsets[SYSC_SYSCONFIG],
525 ddata->offsets[SYSC_SYSSTATUS]);
526
527 if (size < 0 || (size + sizeof(u32)) > ddata->module_size)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700528 return -EINVAL;
529
Tony Lindgren0eecc632017-10-10 14:23:43 -0700530 ddata->module_va = devm_ioremap(ddata->dev,
531 ddata->module_pa,
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700532 size + sizeof(u32));
Tony Lindgren0eecc632017-10-10 14:23:43 -0700533 if (!ddata->module_va)
534 return -EIO;
535
536 return 0;
537}
538
539/**
540 * sysc_map_and_check_registers - ioremap and check device registers
541 * @ddata: device driver data
542 */
543static int sysc_map_and_check_registers(struct sysc *ddata)
544{
545 int error;
546
547 error = sysc_parse_and_check_child_range(ddata);
548 if (error)
549 return error;
550
551 error = sysc_check_children(ddata);
552 if (error)
553 return error;
554
555 error = sysc_parse_registers(ddata);
556 if (error)
557 return error;
558
559 error = sysc_ioremap(ddata);
560 if (error)
561 return error;
562
563 error = sysc_check_registers(ddata);
564 if (error)
565 return error;
566
567 return 0;
568}
569
570/**
571 * sysc_show_rev - read and show interconnect target module revision
572 * @bufp: buffer to print the information to
573 * @ddata: device driver data
574 */
575static int sysc_show_rev(char *bufp, struct sysc *ddata)
576{
Tony Lindgren566a9b052017-12-15 09:41:19 -0800577 int len;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700578
579 if (ddata->offsets[SYSC_REVISION] < 0)
580 return sprintf(bufp, ":NA");
581
Tony Lindgren566a9b052017-12-15 09:41:19 -0800582 len = sprintf(bufp, ":%08x", ddata->revision);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700583
584 return len;
585}
586
587static int sysc_show_reg(struct sysc *ddata,
588 char *bufp, enum sysc_registers reg)
589{
590 if (ddata->offsets[reg] < 0)
591 return sprintf(bufp, ":NA");
592
593 return sprintf(bufp, ":%x", ddata->offsets[reg]);
594}
595
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800596static int sysc_show_name(char *bufp, struct sysc *ddata)
597{
598 if (!ddata->name)
599 return 0;
600
601 return sprintf(bufp, ":%s", ddata->name);
602}
603
Tony Lindgren0eecc632017-10-10 14:23:43 -0700604/**
605 * sysc_show_registers - show information about interconnect target module
606 * @ddata: device driver data
607 */
608static void sysc_show_registers(struct sysc *ddata)
609{
610 char buf[128];
611 char *bufp = buf;
612 int i;
613
614 for (i = 0; i < SYSC_MAX_REGS; i++)
615 bufp += sysc_show_reg(ddata, bufp, i);
616
617 bufp += sysc_show_rev(bufp, ddata);
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800618 bufp += sysc_show_name(bufp, ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700619
620 dev_dbg(ddata->dev, "%llx:%x%s\n",
621 ddata->module_pa, ddata->module_size,
622 buf);
623}
624
Arnd Bergmanna4a5d492017-10-13 11:25:32 +0200625static int __maybe_unused sysc_runtime_suspend(struct device *dev)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700626{
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800627 struct ti_sysc_platform_data *pdata;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700628 struct sysc *ddata;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800629 int error = 0, i;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700630
631 ddata = dev_get_drvdata(dev);
632
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800633 if (!ddata->enabled)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700634 return 0;
635
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800636 if (ddata->legacy_mode) {
637 pdata = dev_get_platdata(ddata->dev);
638 if (!pdata)
639 return 0;
640
641 if (!pdata->idle_module)
642 return -ENODEV;
643
644 error = pdata->idle_module(dev, &ddata->cookie);
645 if (error)
646 dev_err(dev, "%s: could not idle: %i\n",
647 __func__, error);
648
649 goto idled;
650 }
651
Tony Lindgren09dfe582018-04-16 10:25:52 -0700652 for (i = 0; i < ddata->nr_clocks; i++) {
Tony Lindgren0eecc632017-10-10 14:23:43 -0700653 if (IS_ERR_OR_NULL(ddata->clocks[i]))
654 continue;
Tony Lindgren09dfe582018-04-16 10:25:52 -0700655
656 if (i >= SYSC_OPTFCK0 && !sysc_opt_clks_needed(ddata))
657 break;
658
Tony Lindgren0eecc632017-10-10 14:23:43 -0700659 clk_disable(ddata->clocks[i]);
660 }
661
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800662idled:
663 ddata->enabled = false;
664
665 return error;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700666}
667
Arnd Bergmanna4a5d492017-10-13 11:25:32 +0200668static int __maybe_unused sysc_runtime_resume(struct device *dev)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700669{
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800670 struct ti_sysc_platform_data *pdata;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700671 struct sysc *ddata;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800672 int error = 0, i;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700673
674 ddata = dev_get_drvdata(dev);
675
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800676 if (ddata->enabled)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700677 return 0;
678
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800679 if (ddata->legacy_mode) {
680 pdata = dev_get_platdata(ddata->dev);
681 if (!pdata)
682 return 0;
683
684 if (!pdata->enable_module)
685 return -ENODEV;
686
687 error = pdata->enable_module(dev, &ddata->cookie);
688 if (error)
689 dev_err(dev, "%s: could not enable: %i\n",
690 __func__, error);
691
692 goto awake;
693 }
694
Tony Lindgren09dfe582018-04-16 10:25:52 -0700695 for (i = 0; i < ddata->nr_clocks; i++) {
Tony Lindgren0eecc632017-10-10 14:23:43 -0700696 if (IS_ERR_OR_NULL(ddata->clocks[i]))
697 continue;
Tony Lindgren09dfe582018-04-16 10:25:52 -0700698
699 if (i >= SYSC_OPTFCK0 && !sysc_opt_clks_needed(ddata))
700 break;
701
Tony Lindgren0eecc632017-10-10 14:23:43 -0700702 error = clk_enable(ddata->clocks[i]);
703 if (error)
704 return error;
705 }
706
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800707awake:
708 ddata->enabled = true;
709
710 return error;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700711}
712
Tony Lindgrenf5e802032018-10-01 09:33:28 -0700713static int __maybe_unused sysc_noirq_suspend(struct device *dev)
Tony Lindgren62020f22018-02-22 13:59:44 -0800714{
715 struct sysc *ddata;
716
717 ddata = dev_get_drvdata(dev);
718
Tony Lindgren40d9f912018-09-24 12:16:54 -0700719 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
Tony Lindgrene7420c22018-04-16 10:26:46 -0700720 return 0;
721
Tony Lindgrenf5e802032018-10-01 09:33:28 -0700722 return pm_runtime_force_suspend(dev);
Tony Lindgren62020f22018-02-22 13:59:44 -0800723}
724
Tony Lindgrenf5e802032018-10-01 09:33:28 -0700725static int __maybe_unused sysc_noirq_resume(struct device *dev)
Tony Lindgren62020f22018-02-22 13:59:44 -0800726{
727 struct sysc *ddata;
728
729 ddata = dev_get_drvdata(dev);
Tony Lindgrene7420c22018-04-16 10:26:46 -0700730
Tony Lindgren40d9f912018-09-24 12:16:54 -0700731 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
Tony Lindgrene7420c22018-04-16 10:26:46 -0700732 return 0;
733
Tony Lindgrenf5e802032018-10-01 09:33:28 -0700734 return pm_runtime_force_resume(dev);
Tony Lindgrene7420c22018-04-16 10:26:46 -0700735}
736
Tony Lindgren0eecc632017-10-10 14:23:43 -0700737static const struct dev_pm_ops sysc_pm_ops = {
Tony Lindgrene7420c22018-04-16 10:26:46 -0700738 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(sysc_noirq_suspend, sysc_noirq_resume)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700739 SET_RUNTIME_PM_OPS(sysc_runtime_suspend,
740 sysc_runtime_resume,
741 NULL)
742};
743
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800744/* Module revision register based quirks */
745struct sysc_revision_quirk {
746 const char *name;
747 u32 base;
748 int rev_offset;
749 int sysc_offset;
750 int syss_offset;
751 u32 revision;
752 u32 revision_mask;
753 u32 quirks;
754};
755
756#define SYSC_QUIRK(optname, optbase, optrev, optsysc, optsyss, \
757 optrev_val, optrevmask, optquirkmask) \
758 { \
759 .name = (optname), \
760 .base = (optbase), \
761 .rev_offset = (optrev), \
762 .sysc_offset = (optsysc), \
763 .syss_offset = (optsyss), \
764 .revision = (optrev_val), \
765 .revision_mask = (optrevmask), \
766 .quirks = (optquirkmask), \
767 }
768
769static const struct sysc_revision_quirk sysc_revision_quirks[] = {
770 /* These drivers need to be fixed to not use pm_runtime_irq_safe() */
Tony Lindgren3a3d8022018-09-27 13:33:58 -0700771 SYSC_QUIRK("gpio", 0, 0, 0x10, 0x114, 0x50600801, 0xffff00ff,
Tony Lindgren09dfe582018-04-16 10:25:52 -0700772 SYSC_QUIRK_LEGACY_IDLE | SYSC_QUIRK_OPT_CLKS_IN_RESET),
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800773 SYSC_QUIRK("mmu", 0, 0, 0x10, 0x14, 0x00000020, 0xffffffff,
774 SYSC_QUIRK_LEGACY_IDLE),
775 SYSC_QUIRK("mmu", 0, 0, 0x10, 0x14, 0x00000030, 0xffffffff,
776 SYSC_QUIRK_LEGACY_IDLE),
777 SYSC_QUIRK("sham", 0, 0x100, 0x110, 0x114, 0x40000c03, 0xffffffff,
778 SYSC_QUIRK_LEGACY_IDLE),
779 SYSC_QUIRK("smartreflex", 0, -1, 0x24, -1, 0x00000000, 0xffffffff,
780 SYSC_QUIRK_LEGACY_IDLE),
781 SYSC_QUIRK("smartreflex", 0, -1, 0x38, -1, 0x00000000, 0xffffffff,
782 SYSC_QUIRK_LEGACY_IDLE),
783 SYSC_QUIRK("timer", 0, 0, 0x10, 0x14, 0x00000015, 0xffffffff,
Tony Lindgren9bd34c62019-01-22 09:03:08 -0800784 0),
Tony Lindgren8cde5d52018-04-16 10:25:15 -0700785 /* Some timers on omap4 and later */
Tony Lindgren3a3d8022018-09-27 13:33:58 -0700786 SYSC_QUIRK("timer", 0, 0, 0x10, -1, 0x50002100, 0xffffffff,
Tony Lindgren072167d2019-01-29 07:53:47 -0800787 0),
Tony Lindgren3a3d8022018-09-27 13:33:58 -0700788 SYSC_QUIRK("timer", 0, 0, 0x10, -1, 0x4fff1301, 0xffff00ff,
Tony Lindgren9bd34c62019-01-22 09:03:08 -0800789 0),
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800790 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x00000052, 0xffffffff,
791 SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgrend708bb12018-04-16 10:25:32 -0700792 /* Uarts on omap4 and later */
Tony Lindgrenb82beef2018-09-24 12:16:59 -0700793 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x50411e03, 0xffff00ff,
794 SYSC_QUIRK_LEGACY_IDLE),
795 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x47422e03, 0xffffffff,
Tony Lindgrend708bb12018-04-16 10:25:32 -0700796 SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgren7e27e5d2018-04-20 14:54:16 -0700797
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700798#ifdef DEBUG
Tony Lindgren1ba30692018-09-24 12:17:05 -0700799 SYSC_QUIRK("adc", 0, 0, 0x10, -1, 0x47300001, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -0700800 SYSC_QUIRK("atl", 0, 0, -1, -1, 0x0a070100, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700801 SYSC_QUIRK("aess", 0, 0, 0x10, -1, 0x40000000, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -0700802 SYSC_QUIRK("cm", 0, 0, -1, -1, 0x40000301, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -0700803 SYSC_QUIRK("control", 0, 0, 0x10, -1, 0x40000900, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700804 SYSC_QUIRK("cpgmac", 0, 0x1200, 0x1208, 0x1204, 0x4edb1902,
Tony Lindgren23731ea2018-09-24 12:17:09 -0700805 0xffff00f0, 0),
806 SYSC_QUIRK("dcan", 0, 0, -1, -1, 0xffffffff, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700807 SYSC_QUIRK("dcan", 0, 0, -1, -1, 0x00001401, 0xffffffff, 0),
808 SYSC_QUIRK("dwc3", 0, 0, 0x10, -1, 0x500a0200, 0xffffffff, 0),
809 SYSC_QUIRK("epwmss", 0, 0, 0x4, -1, 0x47400001, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700810 SYSC_QUIRK("gpu", 0, 0x1fc00, 0x1fc10, -1, 0, 0, 0),
811 SYSC_QUIRK("hdq1w", 0, 0, 0x14, 0x18, 0x00000006, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700812 SYSC_QUIRK("hdq1w", 0, 0, 0x14, 0x18, 0x0000000a, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700813 SYSC_QUIRK("hsi", 0, 0, 0x10, 0x14, 0x50043101, 0xffffffff, 0),
814 SYSC_QUIRK("iss", 0, 0, 0x10, -1, 0x40000101, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -0700815 SYSC_QUIRK("i2c", 0, 0, 0x10, 0x90, 0x5040000a, 0xfffff0f0, 0),
Tony Lindgren23731ea2018-09-24 12:17:09 -0700816 SYSC_QUIRK("lcdc", 0, 0, 0x54, -1, 0x4f201000, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700817 SYSC_QUIRK("mcasp", 0, 0, 0x4, -1, 0x44306302, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700818 SYSC_QUIRK("mcasp", 0, 0, 0x4, -1, 0x44307b02, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700819 SYSC_QUIRK("mcbsp", 0, -1, 0x8c, -1, 0, 0, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -0700820 SYSC_QUIRK("mcspi", 0, 0, 0x10, -1, 0x40300a0b, 0xffff00ff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700821 SYSC_QUIRK("mcspi", 0, 0, 0x110, 0x114, 0x40300a0b, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700822 SYSC_QUIRK("mailbox", 0, 0, 0x10, -1, 0x00000400, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700823 SYSC_QUIRK("m3", 0, 0, -1, -1, 0x5f580105, 0x0fff0f00, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -0700824 SYSC_QUIRK("ocp2scp", 0, 0, 0x10, 0x14, 0x50060005, 0xfffffff0, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700825 SYSC_QUIRK("ocp2scp", 0, 0, -1, -1, 0x50060007, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -0700826 SYSC_QUIRK("padconf", 0, 0, 0x10, -1, 0x4fff0800, 0xffffffff, 0),
Tony Lindgrenf0106702018-11-15 14:46:53 -0800827 SYSC_QUIRK("padconf", 0, 0, -1, -1, 0x40001100, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -0700828 SYSC_QUIRK("prcm", 0, 0, -1, -1, 0x40000100, 0xffffffff, 0),
Tony Lindgren23731ea2018-09-24 12:17:09 -0700829 SYSC_QUIRK("prcm", 0, 0, -1, -1, 0x00004102, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700830 SYSC_QUIRK("prcm", 0, 0, -1, -1, 0x40000400, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -0700831 SYSC_QUIRK("scm", 0, 0, 0x10, -1, 0x40000900, 0xffffffff, 0),
Tony Lindgren23731ea2018-09-24 12:17:09 -0700832 SYSC_QUIRK("scm", 0, 0, -1, -1, 0x4e8b0100, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700833 SYSC_QUIRK("scm", 0, 0, -1, -1, 0x4f000100, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -0700834 SYSC_QUIRK("scm", 0, 0, -1, -1, 0x40000900, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -0700835 SYSC_QUIRK("scrm", 0, 0, -1, -1, 0x00000010, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -0700836 SYSC_QUIRK("sdio", 0, 0, 0x10, -1, 0x40202301, 0xffff0ff0, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700837 SYSC_QUIRK("sdio", 0, 0x2fc, 0x110, 0x114, 0x31010000, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -0700838 SYSC_QUIRK("sdma", 0, 0, 0x2c, 0x28, 0x00010900, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700839 SYSC_QUIRK("slimbus", 0, 0, 0x10, -1, 0x40000902, 0xffffffff, 0),
840 SYSC_QUIRK("slimbus", 0, 0, 0x10, -1, 0x40002903, 0xffffffff, 0),
841 SYSC_QUIRK("spinlock", 0, 0, 0x10, -1, 0x50020000, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700842 SYSC_QUIRK("rng", 0, 0x1fe0, 0x1fe4, -1, 0x00000020, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -0700843 SYSC_QUIRK("rtc", 0, 0x74, 0x78, -1, 0x4eb01908, 0xffff00f0, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700844 SYSC_QUIRK("timer32k", 0, 0, 0x4, -1, 0x00000060, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700845 SYSC_QUIRK("usbhstll", 0, 0, 0x10, 0x14, 0x00000004, 0xffffffff, 0),
Tony Lindgrenf0106702018-11-15 14:46:53 -0800846 SYSC_QUIRK("usbhstll", 0, 0, 0x10, 0x14, 0x00000008, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700847 SYSC_QUIRK("usb_host_hs", 0, 0, 0x10, 0x14, 0x50700100, 0xffffffff, 0),
Tony Lindgrenf0106702018-11-15 14:46:53 -0800848 SYSC_QUIRK("usb_host_hs", 0, 0, 0x10, -1, 0x50700101, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700849 SYSC_QUIRK("usb_otg_hs", 0, 0x400, 0x404, 0x408, 0x00000050,
850 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -0700851 SYSC_QUIRK("wdt", 0, 0, 0x10, 0x14, 0x502a0500, 0xfffff0f0, 0),
852 SYSC_QUIRK("vfpe", 0, 0, 0x104, -1, 0x4d001200, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -0700853#endif
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800854};
855
856static void sysc_init_revision_quirks(struct sysc *ddata)
857{
858 const struct sysc_revision_quirk *q;
859 int i;
860
861 for (i = 0; i < ARRAY_SIZE(sysc_revision_quirks); i++) {
862 q = &sysc_revision_quirks[i];
863
864 if (q->base && q->base != ddata->module_pa)
865 continue;
866
867 if (q->rev_offset >= 0 &&
868 q->rev_offset != ddata->offsets[SYSC_REVISION])
869 continue;
870
871 if (q->sysc_offset >= 0 &&
872 q->sysc_offset != ddata->offsets[SYSC_SYSCONFIG])
873 continue;
874
875 if (q->syss_offset >= 0 &&
876 q->syss_offset != ddata->offsets[SYSC_SYSSTATUS])
877 continue;
878
879 if (q->revision == ddata->revision ||
880 (q->revision & q->revision_mask) ==
881 (ddata->revision & q->revision_mask)) {
882 ddata->name = q->name;
883 ddata->cfg.quirks |= q->quirks;
884 }
885 }
886}
887
Faiz Abbas596e7952018-07-09 22:18:39 +0530888static int sysc_reset(struct sysc *ddata)
889{
890 int offset = ddata->offsets[SYSC_SYSCONFIG];
891 int val;
892
893 if (ddata->legacy_mode || offset < 0 ||
894 ddata->cfg.quirks & SYSC_QUIRK_NO_RESET_ON_INIT)
895 return 0;
896
897 /*
898 * Currently only support reset status in sysstatus.
899 * Warn and return error in all other cases
900 */
901 if (!ddata->cfg.syss_mask) {
902 dev_err(ddata->dev, "No ti,syss-mask. Reset failed\n");
903 return -EINVAL;
904 }
905
906 val = sysc_read(ddata, offset);
907 val |= (0x1 << ddata->cap->regbits->srst_shift);
908 sysc_write(ddata, offset, val);
909
910 /* Poll on reset status */
911 offset = ddata->offsets[SYSC_SYSSTATUS];
912
913 return readl_poll_timeout(ddata->module_va + offset, val,
914 (val & ddata->cfg.syss_mask) == 0x0,
915 100, MAX_MODULE_SOFTRESET_WAIT);
916}
917
Tony Lindgren566a9b052017-12-15 09:41:19 -0800918/* At this point the module is configured enough to read the revision */
919static int sysc_init_module(struct sysc *ddata)
920{
921 int error;
922
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800923 if (ddata->cfg.quirks & SYSC_QUIRK_NO_IDLE_ON_INIT) {
924 ddata->revision = sysc_read_revision(ddata);
925 goto rev_quirks;
926 }
927
Tony Lindgren566a9b052017-12-15 09:41:19 -0800928 error = pm_runtime_get_sync(ddata->dev);
929 if (error < 0) {
930 pm_runtime_put_noidle(ddata->dev);
931
932 return 0;
933 }
Tony Lindgren50622362018-04-16 10:20:27 -0700934
Faiz Abbas596e7952018-07-09 22:18:39 +0530935 error = sysc_reset(ddata);
936 if (error) {
937 dev_err(ddata->dev, "Reset failed with %d\n", error);
938 pm_runtime_put_sync(ddata->dev);
939
940 return error;
941 }
942
Tony Lindgren566a9b052017-12-15 09:41:19 -0800943 ddata->revision = sysc_read_revision(ddata);
944 pm_runtime_put_sync(ddata->dev);
945
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800946rev_quirks:
947 sysc_init_revision_quirks(ddata);
948
Tony Lindgren566a9b052017-12-15 09:41:19 -0800949 return 0;
950}
951
Tony Lindgrenc5a2de92017-12-15 09:41:23 -0800952static int sysc_init_sysc_mask(struct sysc *ddata)
953{
954 struct device_node *np = ddata->dev->of_node;
955 int error;
956 u32 val;
957
958 error = of_property_read_u32(np, "ti,sysc-mask", &val);
959 if (error)
960 return 0;
961
962 if (val)
963 ddata->cfg.sysc_val = val & ddata->cap->sysc_mask;
964 else
965 ddata->cfg.sysc_val = ddata->cap->sysc_mask;
966
967 return 0;
968}
969
970static int sysc_init_idlemode(struct sysc *ddata, u8 *idlemodes,
971 const char *name)
972{
973 struct device_node *np = ddata->dev->of_node;
974 struct property *prop;
975 const __be32 *p;
976 u32 val;
977
978 of_property_for_each_u32(np, name, prop, p, val) {
979 if (val >= SYSC_NR_IDLEMODES) {
980 dev_err(ddata->dev, "invalid idlemode: %i\n", val);
981 return -EINVAL;
982 }
983 *idlemodes |= (1 << val);
984 }
985
986 return 0;
987}
988
989static int sysc_init_idlemodes(struct sysc *ddata)
990{
991 int error;
992
993 error = sysc_init_idlemode(ddata, &ddata->cfg.midlemodes,
994 "ti,sysc-midle");
995 if (error)
996 return error;
997
998 error = sysc_init_idlemode(ddata, &ddata->cfg.sidlemodes,
999 "ti,sysc-sidle");
1000 if (error)
1001 return error;
1002
1003 return 0;
1004}
1005
1006/*
1007 * Only some devices on omap4 and later have SYSCONFIG reset done
1008 * bit. We can detect this if there is no SYSSTATUS at all, or the
1009 * SYSTATUS bit 0 is not used. Note that some SYSSTATUS registers
1010 * have multiple bits for the child devices like OHCI and EHCI.
1011 * Depends on SYSC being parsed first.
1012 */
1013static int sysc_init_syss_mask(struct sysc *ddata)
1014{
1015 struct device_node *np = ddata->dev->of_node;
1016 int error;
1017 u32 val;
1018
1019 error = of_property_read_u32(np, "ti,syss-mask", &val);
1020 if (error) {
1021 if ((ddata->cap->type == TI_SYSC_OMAP4 ||
1022 ddata->cap->type == TI_SYSC_OMAP4_TIMER) &&
1023 (ddata->cfg.sysc_val & SYSC_OMAP4_SOFTRESET))
1024 ddata->cfg.quirks |= SYSC_QUIRK_RESET_STATUS;
1025
1026 return 0;
1027 }
1028
1029 if (!(val & 1) && (ddata->cfg.sysc_val & SYSC_OMAP4_SOFTRESET))
1030 ddata->cfg.quirks |= SYSC_QUIRK_RESET_STATUS;
1031
1032 ddata->cfg.syss_mask = val;
1033
1034 return 0;
1035}
1036
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001037/*
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001038 * Many child device drivers need to have fck and opt clocks available
1039 * to get the clock rate for device internal configuration etc.
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001040 */
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001041static int sysc_child_add_named_clock(struct sysc *ddata,
1042 struct device *child,
1043 const char *name)
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001044{
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001045 struct clk *clk;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001046 struct clk_lookup *l;
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001047 int error = 0;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001048
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001049 if (!name)
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001050 return 0;
1051
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001052 clk = clk_get(child, name);
1053 if (!IS_ERR(clk)) {
1054 clk_put(clk);
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001055
1056 return -EEXIST;
1057 }
1058
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001059 clk = clk_get(ddata->dev, name);
1060 if (IS_ERR(clk))
1061 return -ENODEV;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001062
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001063 l = clkdev_create(clk, name, dev_name(child));
1064 if (!l)
1065 error = -ENOMEM;
1066
1067 clk_put(clk);
1068
1069 return error;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001070}
1071
Tony Lindgren09dfe582018-04-16 10:25:52 -07001072static int sysc_child_add_clocks(struct sysc *ddata,
1073 struct device *child)
1074{
1075 int i, error;
1076
1077 for (i = 0; i < ddata->nr_clocks; i++) {
1078 error = sysc_child_add_named_clock(ddata,
1079 child,
1080 ddata->clock_roles[i]);
1081 if (error && error != -EEXIST) {
1082 dev_err(ddata->dev, "could not add child clock %s: %i\n",
1083 ddata->clock_roles[i], error);
1084
1085 return error;
1086 }
1087 }
1088
1089 return 0;
1090}
1091
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001092static struct device_type sysc_device_type = {
1093};
1094
1095static struct sysc *sysc_child_to_parent(struct device *dev)
1096{
1097 struct device *parent = dev->parent;
1098
1099 if (!parent || parent->type != &sysc_device_type)
1100 return NULL;
1101
1102 return dev_get_drvdata(parent);
1103}
1104
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001105static int __maybe_unused sysc_child_runtime_suspend(struct device *dev)
1106{
1107 struct sysc *ddata;
1108 int error;
1109
1110 ddata = sysc_child_to_parent(dev);
1111
1112 error = pm_generic_runtime_suspend(dev);
1113 if (error)
1114 return error;
1115
1116 if (!ddata->enabled)
1117 return 0;
1118
1119 return sysc_runtime_suspend(ddata->dev);
1120}
1121
1122static int __maybe_unused sysc_child_runtime_resume(struct device *dev)
1123{
1124 struct sysc *ddata;
1125 int error;
1126
1127 ddata = sysc_child_to_parent(dev);
1128
1129 if (!ddata->enabled) {
1130 error = sysc_runtime_resume(ddata->dev);
1131 if (error < 0)
1132 dev_err(ddata->dev,
1133 "%s error: %i\n", __func__, error);
1134 }
1135
1136 return pm_generic_runtime_resume(dev);
1137}
1138
1139#ifdef CONFIG_PM_SLEEP
1140static int sysc_child_suspend_noirq(struct device *dev)
1141{
1142 struct sysc *ddata;
1143 int error;
1144
1145 ddata = sysc_child_to_parent(dev);
1146
Tony Lindgrenef55f822018-04-16 10:27:15 -07001147 dev_dbg(ddata->dev, "%s %s\n", __func__,
1148 ddata->name ? ddata->name : "");
1149
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001150 error = pm_generic_suspend_noirq(dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001151 if (error) {
1152 dev_err(dev, "%s error at %i: %i\n",
1153 __func__, __LINE__, error);
1154
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001155 return error;
Tony Lindgrenef55f822018-04-16 10:27:15 -07001156 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001157
1158 if (!pm_runtime_status_suspended(dev)) {
1159 error = pm_generic_runtime_suspend(dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001160 if (error) {
Tony Lindgrenf9490782018-09-28 15:21:50 -07001161 dev_dbg(dev, "%s busy at %i: %i\n",
1162 __func__, __LINE__, error);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001163
Tony Lindgren4f3530f2018-08-08 01:07:06 -07001164 return 0;
Tony Lindgrenef55f822018-04-16 10:27:15 -07001165 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001166
1167 error = sysc_runtime_suspend(ddata->dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001168 if (error) {
1169 dev_err(dev, "%s error at %i: %i\n",
1170 __func__, __LINE__, error);
1171
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001172 return error;
Tony Lindgrenef55f822018-04-16 10:27:15 -07001173 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001174
1175 ddata->child_needs_resume = true;
1176 }
1177
1178 return 0;
1179}
1180
1181static int sysc_child_resume_noirq(struct device *dev)
1182{
1183 struct sysc *ddata;
1184 int error;
1185
1186 ddata = sysc_child_to_parent(dev);
1187
Tony Lindgrenef55f822018-04-16 10:27:15 -07001188 dev_dbg(ddata->dev, "%s %s\n", __func__,
1189 ddata->name ? ddata->name : "");
1190
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001191 if (ddata->child_needs_resume) {
1192 ddata->child_needs_resume = false;
1193
1194 error = sysc_runtime_resume(ddata->dev);
1195 if (error)
1196 dev_err(ddata->dev,
1197 "%s runtime resume error: %i\n",
1198 __func__, error);
1199
1200 error = pm_generic_runtime_resume(dev);
1201 if (error)
1202 dev_err(ddata->dev,
1203 "%s generic runtime resume: %i\n",
1204 __func__, error);
1205 }
1206
1207 return pm_generic_resume_noirq(dev);
1208}
1209#endif
1210
1211struct dev_pm_domain sysc_child_pm_domain = {
1212 .ops = {
1213 SET_RUNTIME_PM_OPS(sysc_child_runtime_suspend,
1214 sysc_child_runtime_resume,
1215 NULL)
1216 USE_PLATFORM_PM_SLEEP_OPS
1217 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(sysc_child_suspend_noirq,
1218 sysc_child_resume_noirq)
1219 }
1220};
1221
1222/**
1223 * sysc_legacy_idle_quirk - handle children in omap_device compatible way
1224 * @ddata: device driver data
1225 * @child: child device driver
1226 *
1227 * Allow idle for child devices as done with _od_runtime_suspend().
1228 * Otherwise many child devices will not idle because of the permanent
1229 * parent usecount set in pm_runtime_irq_safe().
1230 *
1231 * Note that the long term solution is to just modify the child device
1232 * drivers to not set pm_runtime_irq_safe() and then this can be just
1233 * dropped.
1234 */
1235static void sysc_legacy_idle_quirk(struct sysc *ddata, struct device *child)
1236{
1237 if (!ddata->legacy_mode)
1238 return;
1239
1240 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
1241 dev_pm_domain_set(child, &sysc_child_pm_domain);
1242}
1243
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001244static int sysc_notifier_call(struct notifier_block *nb,
1245 unsigned long event, void *device)
1246{
1247 struct device *dev = device;
1248 struct sysc *ddata;
1249 int error;
1250
1251 ddata = sysc_child_to_parent(dev);
1252 if (!ddata)
1253 return NOTIFY_DONE;
1254
1255 switch (event) {
1256 case BUS_NOTIFY_ADD_DEVICE:
Tony Lindgren09dfe582018-04-16 10:25:52 -07001257 error = sysc_child_add_clocks(ddata, dev);
1258 if (error)
1259 return error;
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001260 sysc_legacy_idle_quirk(ddata, dev);
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001261 break;
1262 default:
1263 break;
1264 }
1265
1266 return NOTIFY_DONE;
1267}
1268
1269static struct notifier_block sysc_nb = {
1270 .notifier_call = sysc_notifier_call,
1271};
1272
Tony Lindgren566a9b052017-12-15 09:41:19 -08001273/* Device tree configured quirks */
1274struct sysc_dts_quirk {
1275 const char *name;
1276 u32 mask;
1277};
1278
1279static const struct sysc_dts_quirk sysc_dts_quirks[] = {
1280 { .name = "ti,no-idle-on-init",
1281 .mask = SYSC_QUIRK_NO_IDLE_ON_INIT, },
1282 { .name = "ti,no-reset-on-init",
1283 .mask = SYSC_QUIRK_NO_RESET_ON_INIT, },
1284};
1285
Tony Lindgren4014c082018-12-10 14:11:26 -08001286static void sysc_parse_dts_quirks(struct sysc *ddata, struct device_node *np,
1287 bool is_child)
Tony Lindgren566a9b052017-12-15 09:41:19 -08001288{
Tony Lindgren566a9b052017-12-15 09:41:19 -08001289 const struct property *prop;
Tony Lindgren4014c082018-12-10 14:11:26 -08001290 int i, len;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001291
1292 for (i = 0; i < ARRAY_SIZE(sysc_dts_quirks); i++) {
Tony Lindgren4014c082018-12-10 14:11:26 -08001293 const char *name = sysc_dts_quirks[i].name;
1294
1295 prop = of_get_property(np, name, &len);
Tony Lindgren566a9b052017-12-15 09:41:19 -08001296 if (!prop)
Tony Lindgrend39b6ea2018-02-15 09:18:55 -08001297 continue;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001298
1299 ddata->cfg.quirks |= sysc_dts_quirks[i].mask;
Tony Lindgren4014c082018-12-10 14:11:26 -08001300 if (is_child) {
1301 dev_warn(ddata->dev,
1302 "dts flag should be at module level for %s\n",
1303 name);
1304 }
Tony Lindgren566a9b052017-12-15 09:41:19 -08001305 }
Tony Lindgren4014c082018-12-10 14:11:26 -08001306}
Tony Lindgren566a9b052017-12-15 09:41:19 -08001307
Tony Lindgren4014c082018-12-10 14:11:26 -08001308static int sysc_init_dts_quirks(struct sysc *ddata)
1309{
1310 struct device_node *np = ddata->dev->of_node;
1311 int error;
1312 u32 val;
1313
1314 ddata->legacy_mode = of_get_property(np, "ti,hwmods", NULL);
1315
1316 sysc_parse_dts_quirks(ddata, np, false);
Tony Lindgren566a9b052017-12-15 09:41:19 -08001317 error = of_property_read_u32(np, "ti,sysc-delay-us", &val);
1318 if (!error) {
1319 if (val > 255) {
1320 dev_warn(ddata->dev, "bad ti,sysc-delay-us: %i\n",
1321 val);
1322 }
1323
1324 ddata->cfg.srst_udelay = (u8)val;
1325 }
1326
1327 return 0;
1328}
1329
Tony Lindgren0eecc632017-10-10 14:23:43 -07001330static void sysc_unprepare(struct sysc *ddata)
1331{
1332 int i;
1333
Tony Lindgrenaaa29bb2019-03-21 11:00:21 -07001334 if (!ddata->clocks)
1335 return;
1336
Tony Lindgren0eecc632017-10-10 14:23:43 -07001337 for (i = 0; i < SYSC_MAX_CLOCKS; i++) {
1338 if (!IS_ERR_OR_NULL(ddata->clocks[i]))
1339 clk_unprepare(ddata->clocks[i]);
1340 }
1341}
1342
Tony Lindgren70a65242017-12-15 09:41:09 -08001343/*
1344 * Common sysc register bits found on omap2, also known as type1
1345 */
1346static const struct sysc_regbits sysc_regbits_omap2 = {
1347 .dmadisable_shift = -ENODEV,
1348 .midle_shift = 12,
1349 .sidle_shift = 3,
1350 .clkact_shift = 8,
1351 .emufree_shift = 5,
1352 .enwkup_shift = 2,
1353 .srst_shift = 1,
1354 .autoidle_shift = 0,
1355};
1356
1357static const struct sysc_capabilities sysc_omap2 = {
1358 .type = TI_SYSC_OMAP2,
1359 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY | SYSC_OMAP2_EMUFREE |
1360 SYSC_OMAP2_ENAWAKEUP | SYSC_OMAP2_SOFTRESET |
1361 SYSC_OMAP2_AUTOIDLE,
1362 .regbits = &sysc_regbits_omap2,
1363};
1364
1365/* All omap2 and 3 timers, and timers 1, 2 & 10 on omap 4 and 5 */
1366static const struct sysc_capabilities sysc_omap2_timer = {
1367 .type = TI_SYSC_OMAP2_TIMER,
1368 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY | SYSC_OMAP2_EMUFREE |
1369 SYSC_OMAP2_ENAWAKEUP | SYSC_OMAP2_SOFTRESET |
1370 SYSC_OMAP2_AUTOIDLE,
1371 .regbits = &sysc_regbits_omap2,
1372 .mod_quirks = SYSC_QUIRK_USE_CLOCKACT,
1373};
1374
1375/*
1376 * SHAM2 (SHA1/MD5) sysc found on omap3, a variant of sysc_regbits_omap2
1377 * with different sidle position
1378 */
1379static const struct sysc_regbits sysc_regbits_omap3_sham = {
1380 .dmadisable_shift = -ENODEV,
1381 .midle_shift = -ENODEV,
1382 .sidle_shift = 4,
1383 .clkact_shift = -ENODEV,
1384 .enwkup_shift = -ENODEV,
1385 .srst_shift = 1,
1386 .autoidle_shift = 0,
1387 .emufree_shift = -ENODEV,
1388};
1389
1390static const struct sysc_capabilities sysc_omap3_sham = {
1391 .type = TI_SYSC_OMAP3_SHAM,
1392 .sysc_mask = SYSC_OMAP2_SOFTRESET | SYSC_OMAP2_AUTOIDLE,
1393 .regbits = &sysc_regbits_omap3_sham,
1394};
1395
1396/*
1397 * AES register bits found on omap3 and later, a variant of
1398 * sysc_regbits_omap2 with different sidle position
1399 */
1400static const struct sysc_regbits sysc_regbits_omap3_aes = {
1401 .dmadisable_shift = -ENODEV,
1402 .midle_shift = -ENODEV,
1403 .sidle_shift = 6,
1404 .clkact_shift = -ENODEV,
1405 .enwkup_shift = -ENODEV,
1406 .srst_shift = 1,
1407 .autoidle_shift = 0,
1408 .emufree_shift = -ENODEV,
1409};
1410
1411static const struct sysc_capabilities sysc_omap3_aes = {
1412 .type = TI_SYSC_OMAP3_AES,
1413 .sysc_mask = SYSC_OMAP2_SOFTRESET | SYSC_OMAP2_AUTOIDLE,
1414 .regbits = &sysc_regbits_omap3_aes,
1415};
1416
1417/*
1418 * Common sysc register bits found on omap4, also known as type2
1419 */
1420static const struct sysc_regbits sysc_regbits_omap4 = {
1421 .dmadisable_shift = 16,
1422 .midle_shift = 4,
1423 .sidle_shift = 2,
1424 .clkact_shift = -ENODEV,
1425 .enwkup_shift = -ENODEV,
1426 .emufree_shift = 1,
1427 .srst_shift = 0,
1428 .autoidle_shift = -ENODEV,
1429};
1430
1431static const struct sysc_capabilities sysc_omap4 = {
1432 .type = TI_SYSC_OMAP4,
1433 .sysc_mask = SYSC_OMAP4_DMADISABLE | SYSC_OMAP4_FREEEMU |
1434 SYSC_OMAP4_SOFTRESET,
1435 .regbits = &sysc_regbits_omap4,
1436};
1437
1438static const struct sysc_capabilities sysc_omap4_timer = {
1439 .type = TI_SYSC_OMAP4_TIMER,
1440 .sysc_mask = SYSC_OMAP4_DMADISABLE | SYSC_OMAP4_FREEEMU |
1441 SYSC_OMAP4_SOFTRESET,
1442 .regbits = &sysc_regbits_omap4,
1443};
1444
1445/*
1446 * Common sysc register bits found on omap4, also known as type3
1447 */
1448static const struct sysc_regbits sysc_regbits_omap4_simple = {
1449 .dmadisable_shift = -ENODEV,
1450 .midle_shift = 2,
1451 .sidle_shift = 0,
1452 .clkact_shift = -ENODEV,
1453 .enwkup_shift = -ENODEV,
1454 .srst_shift = -ENODEV,
1455 .emufree_shift = -ENODEV,
1456 .autoidle_shift = -ENODEV,
1457};
1458
1459static const struct sysc_capabilities sysc_omap4_simple = {
1460 .type = TI_SYSC_OMAP4_SIMPLE,
1461 .regbits = &sysc_regbits_omap4_simple,
1462};
1463
1464/*
1465 * SmartReflex sysc found on omap34xx
1466 */
1467static const struct sysc_regbits sysc_regbits_omap34xx_sr = {
1468 .dmadisable_shift = -ENODEV,
1469 .midle_shift = -ENODEV,
1470 .sidle_shift = -ENODEV,
1471 .clkact_shift = 20,
1472 .enwkup_shift = -ENODEV,
1473 .srst_shift = -ENODEV,
1474 .emufree_shift = -ENODEV,
1475 .autoidle_shift = -ENODEV,
1476};
1477
1478static const struct sysc_capabilities sysc_34xx_sr = {
1479 .type = TI_SYSC_OMAP34XX_SR,
1480 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY,
1481 .regbits = &sysc_regbits_omap34xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001482 .mod_quirks = SYSC_QUIRK_USE_CLOCKACT | SYSC_QUIRK_UNCACHED |
1483 SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08001484};
1485
1486/*
1487 * SmartReflex sysc found on omap36xx and later
1488 */
1489static const struct sysc_regbits sysc_regbits_omap36xx_sr = {
1490 .dmadisable_shift = -ENODEV,
1491 .midle_shift = -ENODEV,
1492 .sidle_shift = 24,
1493 .clkact_shift = -ENODEV,
1494 .enwkup_shift = 26,
1495 .srst_shift = -ENODEV,
1496 .emufree_shift = -ENODEV,
1497 .autoidle_shift = -ENODEV,
1498};
1499
1500static const struct sysc_capabilities sysc_36xx_sr = {
1501 .type = TI_SYSC_OMAP36XX_SR,
Tony Lindgren3267c082018-01-22 09:32:53 -08001502 .sysc_mask = SYSC_OMAP3_SR_ENAWAKEUP,
Tony Lindgren70a65242017-12-15 09:41:09 -08001503 .regbits = &sysc_regbits_omap36xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001504 .mod_quirks = SYSC_QUIRK_UNCACHED | SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08001505};
1506
1507static const struct sysc_capabilities sysc_omap4_sr = {
1508 .type = TI_SYSC_OMAP4_SR,
1509 .regbits = &sysc_regbits_omap36xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001510 .mod_quirks = SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08001511};
1512
1513/*
1514 * McASP register bits found on omap4 and later
1515 */
1516static const struct sysc_regbits sysc_regbits_omap4_mcasp = {
1517 .dmadisable_shift = -ENODEV,
1518 .midle_shift = -ENODEV,
1519 .sidle_shift = 0,
1520 .clkact_shift = -ENODEV,
1521 .enwkup_shift = -ENODEV,
1522 .srst_shift = -ENODEV,
1523 .emufree_shift = -ENODEV,
1524 .autoidle_shift = -ENODEV,
1525};
1526
1527static const struct sysc_capabilities sysc_omap4_mcasp = {
1528 .type = TI_SYSC_OMAP4_MCASP,
1529 .regbits = &sysc_regbits_omap4_mcasp,
Tony Lindgren2c63a832018-11-15 14:46:53 -08001530 .mod_quirks = SYSC_QUIRK_OPT_CLKS_NEEDED,
1531};
1532
1533/*
1534 * McASP found on dra7 and later
1535 */
1536static const struct sysc_capabilities sysc_dra7_mcasp = {
1537 .type = TI_SYSC_OMAP4_SIMPLE,
1538 .regbits = &sysc_regbits_omap4_simple,
1539 .mod_quirks = SYSC_QUIRK_OPT_CLKS_NEEDED,
Tony Lindgren70a65242017-12-15 09:41:09 -08001540};
1541
1542/*
1543 * FS USB host found on omap4 and later
1544 */
1545static const struct sysc_regbits sysc_regbits_omap4_usb_host_fs = {
1546 .dmadisable_shift = -ENODEV,
1547 .midle_shift = -ENODEV,
1548 .sidle_shift = 24,
1549 .clkact_shift = -ENODEV,
1550 .enwkup_shift = 26,
1551 .srst_shift = -ENODEV,
1552 .emufree_shift = -ENODEV,
1553 .autoidle_shift = -ENODEV,
1554};
1555
1556static const struct sysc_capabilities sysc_omap4_usb_host_fs = {
1557 .type = TI_SYSC_OMAP4_USB_HOST_FS,
1558 .sysc_mask = SYSC_OMAP2_ENAWAKEUP,
1559 .regbits = &sysc_regbits_omap4_usb_host_fs,
1560};
1561
Faiz Abbas7f35e632018-07-09 22:18:38 +05301562static const struct sysc_regbits sysc_regbits_dra7_mcan = {
1563 .dmadisable_shift = -ENODEV,
1564 .midle_shift = -ENODEV,
1565 .sidle_shift = -ENODEV,
1566 .clkact_shift = -ENODEV,
1567 .enwkup_shift = 4,
1568 .srst_shift = 0,
1569 .emufree_shift = -ENODEV,
1570 .autoidle_shift = -ENODEV,
1571};
1572
1573static const struct sysc_capabilities sysc_dra7_mcan = {
1574 .type = TI_SYSC_DRA7_MCAN,
1575 .sysc_mask = SYSC_DRA7_MCAN_ENAWAKEUP | SYSC_OMAP4_SOFTRESET,
1576 .regbits = &sysc_regbits_dra7_mcan,
1577};
1578
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001579static int sysc_init_pdata(struct sysc *ddata)
1580{
1581 struct ti_sysc_platform_data *pdata = dev_get_platdata(ddata->dev);
1582 struct ti_sysc_module_data mdata;
1583 int error = 0;
1584
1585 if (!pdata || !ddata->legacy_mode)
1586 return 0;
1587
1588 mdata.name = ddata->legacy_mode;
1589 mdata.module_pa = ddata->module_pa;
1590 mdata.module_size = ddata->module_size;
1591 mdata.offsets = ddata->offsets;
1592 mdata.nr_offsets = SYSC_MAX_REGS;
1593 mdata.cap = ddata->cap;
1594 mdata.cfg = &ddata->cfg;
1595
1596 if (!pdata->init_module)
1597 return -ENODEV;
1598
1599 error = pdata->init_module(ddata->dev, &mdata, &ddata->cookie);
1600 if (error == -EEXIST)
1601 error = 0;
1602
1603 return error;
1604}
1605
Tony Lindgren70a65242017-12-15 09:41:09 -08001606static int sysc_init_match(struct sysc *ddata)
1607{
1608 const struct sysc_capabilities *cap;
1609
1610 cap = of_device_get_match_data(ddata->dev);
1611 if (!cap)
1612 return -EINVAL;
1613
1614 ddata->cap = cap;
1615 if (ddata->cap)
1616 ddata->cfg.quirks |= ddata->cap->mod_quirks;
1617
1618 return 0;
1619}
1620
Tony Lindgren76f0f772018-02-23 08:28:45 -08001621static void ti_sysc_idle(struct work_struct *work)
1622{
1623 struct sysc *ddata;
1624
1625 ddata = container_of(work, struct sysc, idle_work.work);
1626
1627 if (pm_runtime_active(ddata->dev))
1628 pm_runtime_put_sync(ddata->dev);
1629}
1630
Tony Lindgrenc4bebea2018-04-16 10:24:54 -07001631static const struct of_device_id sysc_match_table[] = {
1632 { .compatible = "simple-bus", },
1633 { /* sentinel */ },
1634};
1635
Tony Lindgren0eecc632017-10-10 14:23:43 -07001636static int sysc_probe(struct platform_device *pdev)
1637{
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001638 struct ti_sysc_platform_data *pdata = dev_get_platdata(&pdev->dev);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001639 struct sysc *ddata;
1640 int error;
1641
1642 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
1643 if (!ddata)
1644 return -ENOMEM;
1645
1646 ddata->dev = &pdev->dev;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001647 platform_set_drvdata(pdev, ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001648
Tony Lindgren70a65242017-12-15 09:41:09 -08001649 error = sysc_init_match(ddata);
1650 if (error)
1651 return error;
1652
Tony Lindgren566a9b052017-12-15 09:41:19 -08001653 error = sysc_init_dts_quirks(ddata);
1654 if (error)
1655 goto unprepare;
1656
Tony Lindgren0eecc632017-10-10 14:23:43 -07001657 error = sysc_get_clocks(ddata);
1658 if (error)
1659 return error;
1660
1661 error = sysc_map_and_check_registers(ddata);
1662 if (error)
1663 goto unprepare;
1664
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08001665 error = sysc_init_sysc_mask(ddata);
1666 if (error)
1667 goto unprepare;
1668
1669 error = sysc_init_idlemodes(ddata);
1670 if (error)
1671 goto unprepare;
1672
1673 error = sysc_init_syss_mask(ddata);
1674 if (error)
1675 goto unprepare;
1676
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001677 error = sysc_init_pdata(ddata);
1678 if (error)
1679 goto unprepare;
1680
Tony Lindgren50622362018-04-16 10:20:27 -07001681 error = sysc_init_resets(ddata);
1682 if (error)
1683 return error;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001684
Tony Lindgren50622362018-04-16 10:20:27 -07001685 pm_runtime_enable(ddata->dev);
Tony Lindgren566a9b052017-12-15 09:41:19 -08001686 error = sysc_init_module(ddata);
1687 if (error)
1688 goto unprepare;
1689
Tony Lindgren0eecc632017-10-10 14:23:43 -07001690 error = pm_runtime_get_sync(ddata->dev);
1691 if (error < 0) {
1692 pm_runtime_put_noidle(ddata->dev);
1693 pm_runtime_disable(ddata->dev);
1694 goto unprepare;
1695 }
1696
Tony Lindgren0eecc632017-10-10 14:23:43 -07001697 sysc_show_registers(ddata);
1698
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001699 ddata->dev->type = &sysc_device_type;
Tony Lindgrenc4bebea2018-04-16 10:24:54 -07001700 error = of_platform_populate(ddata->dev->of_node, sysc_match_table,
1701 pdata ? pdata->auxdata : NULL,
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001702 ddata->dev);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001703 if (error)
1704 goto err;
1705
Tony Lindgren76f0f772018-02-23 08:28:45 -08001706 INIT_DELAYED_WORK(&ddata->idle_work, ti_sysc_idle);
1707
1708 /* At least earlycon won't survive without deferred idle */
1709 if (ddata->cfg.quirks & (SYSC_QUIRK_NO_IDLE_ON_INIT |
1710 SYSC_QUIRK_NO_RESET_ON_INIT)) {
1711 schedule_delayed_work(&ddata->idle_work, 3000);
1712 } else {
1713 pm_runtime_put(&pdev->dev);
1714 }
Tony Lindgren0eecc632017-10-10 14:23:43 -07001715
Tony Lindgren50622362018-04-16 10:20:27 -07001716 if (!of_get_available_child_count(ddata->dev->of_node))
1717 reset_control_assert(ddata->rsts);
1718
Tony Lindgren0eecc632017-10-10 14:23:43 -07001719 return 0;
1720
1721err:
Tony Lindgren0eecc632017-10-10 14:23:43 -07001722 pm_runtime_put_sync(&pdev->dev);
1723 pm_runtime_disable(&pdev->dev);
1724unprepare:
1725 sysc_unprepare(ddata);
1726
1727 return error;
1728}
1729
Tony Lindgren684be5a2017-10-13 10:48:40 -07001730static int sysc_remove(struct platform_device *pdev)
1731{
1732 struct sysc *ddata = platform_get_drvdata(pdev);
1733 int error;
1734
Tony Lindgren76f0f772018-02-23 08:28:45 -08001735 cancel_delayed_work_sync(&ddata->idle_work);
1736
Tony Lindgren684be5a2017-10-13 10:48:40 -07001737 error = pm_runtime_get_sync(ddata->dev);
1738 if (error < 0) {
1739 pm_runtime_put_noidle(ddata->dev);
1740 pm_runtime_disable(ddata->dev);
1741 goto unprepare;
1742 }
1743
1744 of_platform_depopulate(&pdev->dev);
1745
Tony Lindgren684be5a2017-10-13 10:48:40 -07001746 pm_runtime_put_sync(&pdev->dev);
1747 pm_runtime_disable(&pdev->dev);
Tony Lindgren50622362018-04-16 10:20:27 -07001748 reset_control_assert(ddata->rsts);
Tony Lindgren684be5a2017-10-13 10:48:40 -07001749
1750unprepare:
1751 sysc_unprepare(ddata);
1752
1753 return 0;
1754}
1755
Tony Lindgren0eecc632017-10-10 14:23:43 -07001756static const struct of_device_id sysc_match[] = {
Tony Lindgren70a65242017-12-15 09:41:09 -08001757 { .compatible = "ti,sysc-omap2", .data = &sysc_omap2, },
1758 { .compatible = "ti,sysc-omap2-timer", .data = &sysc_omap2_timer, },
1759 { .compatible = "ti,sysc-omap4", .data = &sysc_omap4, },
1760 { .compatible = "ti,sysc-omap4-timer", .data = &sysc_omap4_timer, },
1761 { .compatible = "ti,sysc-omap4-simple", .data = &sysc_omap4_simple, },
1762 { .compatible = "ti,sysc-omap3430-sr", .data = &sysc_34xx_sr, },
1763 { .compatible = "ti,sysc-omap3630-sr", .data = &sysc_36xx_sr, },
1764 { .compatible = "ti,sysc-omap4-sr", .data = &sysc_omap4_sr, },
1765 { .compatible = "ti,sysc-omap3-sham", .data = &sysc_omap3_sham, },
1766 { .compatible = "ti,sysc-omap-aes", .data = &sysc_omap3_aes, },
1767 { .compatible = "ti,sysc-mcasp", .data = &sysc_omap4_mcasp, },
Tony Lindgren2c63a832018-11-15 14:46:53 -08001768 { .compatible = "ti,sysc-dra7-mcasp", .data = &sysc_dra7_mcasp, },
Tony Lindgren70a65242017-12-15 09:41:09 -08001769 { .compatible = "ti,sysc-usb-host-fs",
1770 .data = &sysc_omap4_usb_host_fs, },
Faiz Abbas7f35e632018-07-09 22:18:38 +05301771 { .compatible = "ti,sysc-dra7-mcan", .data = &sysc_dra7_mcan, },
Tony Lindgren0eecc632017-10-10 14:23:43 -07001772 { },
1773};
1774MODULE_DEVICE_TABLE(of, sysc_match);
1775
1776static struct platform_driver sysc_driver = {
1777 .probe = sysc_probe,
Tony Lindgren684be5a2017-10-13 10:48:40 -07001778 .remove = sysc_remove,
Tony Lindgren0eecc632017-10-10 14:23:43 -07001779 .driver = {
1780 .name = "ti-sysc",
1781 .of_match_table = sysc_match,
1782 .pm = &sysc_pm_ops,
1783 },
1784};
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001785
1786static int __init sysc_init(void)
1787{
1788 bus_register_notifier(&platform_bus_type, &sysc_nb);
1789
1790 return platform_driver_register(&sysc_driver);
1791}
1792module_init(sysc_init);
1793
1794static void __exit sysc_exit(void)
1795{
1796 bus_unregister_notifier(&platform_bus_type, &sysc_nb);
1797 platform_driver_unregister(&sysc_driver);
1798}
1799module_exit(sysc_exit);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001800
1801MODULE_DESCRIPTION("TI sysc interconnect target driver");
1802MODULE_LICENSE("GPL v2");