2 * ti-sysc.c - Texas Instruments sysc interconnect target driver
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.
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.
15 #include <linux/clk.h>
16 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/of_address.h>
20 #include <linux/of_platform.h>
29 static const char * const reg_names
[] = { "rev", "sysc", "syss", };
37 static const char * const clock_names
[] = { "fck", "ick", };
40 * struct sysc - TI sysc interconnect target module registers and capabilities
41 * @dev: struct device pointer
42 * @module_pa: physical address of the interconnect target module
43 * @module_size: size of the interconnect target module
44 * @module_va: virtual address of the interconnect target module
45 * @offsets: register offsets from module base
46 * @clocks: clocks used by the interconnect target module
47 * @legacy_mode: configured for legacy mode if set
53 void __iomem
*module_va
;
54 int offsets
[SYSC_MAX_REGS
];
55 struct clk
*clocks
[SYSC_MAX_CLOCKS
];
56 const char *legacy_mode
;
59 static u32
sysc_read_revision(struct sysc
*ddata
)
61 return readl_relaxed(ddata
->module_va
+
62 ddata
->offsets
[SYSC_REVISION
]);
65 static int sysc_get_one_clock(struct sysc
*ddata
,
66 enum sysc_clocks index
)
79 name
= clock_names
[index
];
81 ddata
->clocks
[index
] = devm_clk_get(ddata
->dev
, name
);
82 if (IS_ERR(ddata
->clocks
[index
])) {
83 if (PTR_ERR(ddata
->clocks
[index
]) == -ENOENT
)
86 dev_err(ddata
->dev
, "clock get error for %s: %li\n",
87 name
, PTR_ERR(ddata
->clocks
[index
]));
89 return PTR_ERR(ddata
->clocks
[index
]);
92 error
= clk_prepare(ddata
->clocks
[index
]);
94 dev_err(ddata
->dev
, "clock prepare error for %s: %i\n",
103 static int sysc_get_clocks(struct sysc
*ddata
)
107 if (ddata
->legacy_mode
)
110 for (i
= 0; i
< SYSC_MAX_CLOCKS
; i
++) {
111 error
= sysc_get_one_clock(ddata
, i
);
112 if (error
&& error
!= -ENOENT
)
120 * sysc_parse_and_check_child_range - parses module IO region from ranges
121 * @ddata: device driver data
123 * In general we only need rev, syss, and sysc registers and not the whole
124 * module range. But we do want the offsets for these registers from the
125 * module base. This allows us to check them against the legacy hwmod
126 * platform data. Let's also check the ranges are configured properly.
128 static int sysc_parse_and_check_child_range(struct sysc
*ddata
)
130 struct device_node
*np
= ddata
->dev
->of_node
;
131 const __be32
*ranges
;
132 u32 nr_addr
, nr_size
;
135 ranges
= of_get_property(np
, "ranges", &len
);
137 dev_err(ddata
->dev
, "missing ranges for %pOF\n", np
);
142 len
/= sizeof(*ranges
);
145 dev_err(ddata
->dev
, "incomplete ranges for %pOF\n", np
);
150 error
= of_property_read_u32(np
, "#address-cells", &nr_addr
);
154 error
= of_property_read_u32(np
, "#size-cells", &nr_size
);
158 if (nr_addr
!= 1 || nr_size
!= 1) {
159 dev_err(ddata
->dev
, "invalid ranges for %pOF\n", np
);
165 ddata
->module_pa
= of_translate_address(np
, ranges
++);
166 ddata
->module_size
= be32_to_cpup(ranges
);
168 dev_dbg(ddata
->dev
, "interconnect target 0x%llx size 0x%x for %pOF\n",
169 ddata
->module_pa
, ddata
->module_size
, np
);
175 * sysc_check_one_child - check child configuration
176 * @ddata: device driver data
177 * @np: child device node
179 * Let's avoid messy situations where we have new interconnect target
180 * node but children have "ti,hwmods". These belong to the interconnect
181 * target node and are managed by this driver.
183 static int sysc_check_one_child(struct sysc
*ddata
,
184 struct device_node
*np
)
188 name
= of_get_property(np
, "ti,hwmods", NULL
);
190 dev_warn(ddata
->dev
, "really a child ti,hwmods property?");
195 static int sysc_check_children(struct sysc
*ddata
)
197 struct device_node
*child
;
200 for_each_child_of_node(ddata
->dev
->of_node
, child
) {
201 error
= sysc_check_one_child(ddata
, child
);
210 * sysc_parse_one - parses the interconnect target module registers
211 * @ddata: device driver data
212 * @reg: register to parse
214 static int sysc_parse_one(struct sysc
*ddata
, enum sysc_registers reg
)
216 struct resource
*res
;
223 name
= reg_names
[reg
];
229 res
= platform_get_resource_byname(to_platform_device(ddata
->dev
),
230 IORESOURCE_MEM
, name
);
232 dev_dbg(ddata
->dev
, "has no %s register\n", name
);
233 ddata
->offsets
[reg
] = -ENODEV
;
238 ddata
->offsets
[reg
] = res
->start
- ddata
->module_pa
;
243 static int sysc_parse_registers(struct sysc
*ddata
)
247 for (i
= 0; i
< SYSC_MAX_REGS
; i
++) {
248 error
= sysc_parse_one(ddata
, i
);
257 * sysc_check_registers - check for misconfigured register overlaps
258 * @ddata: device driver data
260 static int sysc_check_registers(struct sysc
*ddata
)
262 int i
, j
, nr_regs
= 0, nr_matches
= 0;
264 for (i
= 0; i
< SYSC_MAX_REGS
; i
++) {
265 if (ddata
->offsets
[i
] < 0)
268 if (ddata
->offsets
[i
] > (ddata
->module_size
- 4)) {
269 dev_err(ddata
->dev
, "register outside module range");
274 for (j
= 0; j
< SYSC_MAX_REGS
; j
++) {
275 if (ddata
->offsets
[j
] < 0)
278 if (ddata
->offsets
[i
] == ddata
->offsets
[j
])
285 dev_err(ddata
->dev
, "missing registers\n");
290 if (nr_matches
> nr_regs
) {
291 dev_err(ddata
->dev
, "overlapping registers: (%i/%i)",
292 nr_regs
, nr_matches
);
301 * syc_ioremap - ioremap register space for the interconnect target module
302 * @ddata: deviec driver data
304 * Note that the interconnect target module registers can be anywhere
305 * within the first child device address space. For example, SGX has
306 * them at offset 0x1fc00 in the 32MB module address space. We just
307 * what we need around the interconnect target module registers.
309 static int sysc_ioremap(struct sysc
*ddata
)
313 if (ddata
->offsets
[SYSC_SYSSTATUS
] >= 0)
314 size
= ddata
->offsets
[SYSC_SYSSTATUS
];
315 else if (ddata
->offsets
[SYSC_SYSCONFIG
] >= 0)
316 size
= ddata
->offsets
[SYSC_SYSCONFIG
];
317 else if (ddata
->offsets
[SYSC_REVISION
] >= 0)
318 size
= ddata
->offsets
[SYSC_REVISION
];
325 ddata
->module_va
= devm_ioremap(ddata
->dev
,
328 if (!ddata
->module_va
)
335 * sysc_map_and_check_registers - ioremap and check device registers
336 * @ddata: device driver data
338 static int sysc_map_and_check_registers(struct sysc
*ddata
)
342 error
= sysc_parse_and_check_child_range(ddata
);
346 error
= sysc_check_children(ddata
);
350 error
= sysc_parse_registers(ddata
);
354 error
= sysc_ioremap(ddata
);
358 error
= sysc_check_registers(ddata
);
366 * sysc_show_rev - read and show interconnect target module revision
367 * @bufp: buffer to print the information to
368 * @ddata: device driver data
370 static int sysc_show_rev(char *bufp
, struct sysc
*ddata
)
374 if (ddata
->offsets
[SYSC_REVISION
] < 0)
375 return sprintf(bufp
, ":NA");
377 error
= pm_runtime_get_sync(ddata
->dev
);
379 pm_runtime_put_noidle(ddata
->dev
);
384 len
= sprintf(bufp
, ":%08x", sysc_read_revision(ddata
));
386 pm_runtime_mark_last_busy(ddata
->dev
);
387 pm_runtime_put_autosuspend(ddata
->dev
);
392 static int sysc_show_reg(struct sysc
*ddata
,
393 char *bufp
, enum sysc_registers reg
)
395 if (ddata
->offsets
[reg
] < 0)
396 return sprintf(bufp
, ":NA");
398 return sprintf(bufp
, ":%x", ddata
->offsets
[reg
]);
402 * sysc_show_registers - show information about interconnect target module
403 * @ddata: device driver data
405 static void sysc_show_registers(struct sysc
*ddata
)
411 for (i
= 0; i
< SYSC_MAX_REGS
; i
++)
412 bufp
+= sysc_show_reg(ddata
, bufp
, i
);
414 bufp
+= sysc_show_rev(bufp
, ddata
);
416 dev_dbg(ddata
->dev
, "%llx:%x%s\n",
417 ddata
->module_pa
, ddata
->module_size
,
421 static int __maybe_unused
sysc_runtime_suspend(struct device
*dev
)
426 ddata
= dev_get_drvdata(dev
);
428 if (ddata
->legacy_mode
)
431 for (i
= 0; i
< SYSC_MAX_CLOCKS
; i
++) {
432 if (IS_ERR_OR_NULL(ddata
->clocks
[i
]))
434 clk_disable(ddata
->clocks
[i
]);
440 static int __maybe_unused
sysc_runtime_resume(struct device
*dev
)
445 ddata
= dev_get_drvdata(dev
);
447 if (ddata
->legacy_mode
)
450 for (i
= 0; i
< SYSC_MAX_CLOCKS
; i
++) {
451 if (IS_ERR_OR_NULL(ddata
->clocks
[i
]))
453 error
= clk_enable(ddata
->clocks
[i
]);
461 static const struct dev_pm_ops sysc_pm_ops
= {
462 SET_RUNTIME_PM_OPS(sysc_runtime_suspend
,
467 static void sysc_unprepare(struct sysc
*ddata
)
471 for (i
= 0; i
< SYSC_MAX_CLOCKS
; i
++) {
472 if (!IS_ERR_OR_NULL(ddata
->clocks
[i
]))
473 clk_unprepare(ddata
->clocks
[i
]);
477 static int sysc_probe(struct platform_device
*pdev
)
479 struct device_node
*np
= pdev
->dev
.of_node
;
483 ddata
= devm_kzalloc(&pdev
->dev
, sizeof(*ddata
), GFP_KERNEL
);
487 ddata
->dev
= &pdev
->dev
;
488 ddata
->legacy_mode
= of_get_property(np
, "ti,hwmods", NULL
);
490 error
= sysc_get_clocks(ddata
);
494 error
= sysc_map_and_check_registers(ddata
);
498 platform_set_drvdata(pdev
, ddata
);
500 pm_runtime_enable(ddata
->dev
);
501 error
= pm_runtime_get_sync(ddata
->dev
);
503 pm_runtime_put_noidle(ddata
->dev
);
504 pm_runtime_disable(ddata
->dev
);
508 pm_runtime_use_autosuspend(ddata
->dev
);
510 sysc_show_registers(ddata
);
512 error
= of_platform_populate(ddata
->dev
->of_node
,
513 NULL
, NULL
, ddata
->dev
);
517 pm_runtime_mark_last_busy(ddata
->dev
);
518 pm_runtime_put_autosuspend(ddata
->dev
);
523 pm_runtime_dont_use_autosuspend(&pdev
->dev
);
524 pm_runtime_put_sync(&pdev
->dev
);
525 pm_runtime_disable(&pdev
->dev
);
527 sysc_unprepare(ddata
);
532 static int sysc_remove(struct platform_device
*pdev
)
534 struct sysc
*ddata
= platform_get_drvdata(pdev
);
537 error
= pm_runtime_get_sync(ddata
->dev
);
539 pm_runtime_put_noidle(ddata
->dev
);
540 pm_runtime_disable(ddata
->dev
);
544 of_platform_depopulate(&pdev
->dev
);
546 pm_runtime_dont_use_autosuspend(&pdev
->dev
);
547 pm_runtime_put_sync(&pdev
->dev
);
548 pm_runtime_disable(&pdev
->dev
);
551 sysc_unprepare(ddata
);
556 static const struct of_device_id sysc_match
[] = {
557 { .compatible
= "ti,sysc-omap2" },
558 { .compatible
= "ti,sysc-omap4" },
559 { .compatible
= "ti,sysc-omap4-simple" },
560 { .compatible
= "ti,sysc-omap3430-sr" },
561 { .compatible
= "ti,sysc-omap3630-sr" },
562 { .compatible
= "ti,sysc-omap4-sr" },
563 { .compatible
= "ti,sysc-omap3-sham" },
564 { .compatible
= "ti,sysc-omap-aes" },
565 { .compatible
= "ti,sysc-mcasp" },
566 { .compatible
= "ti,sysc-usb-host-fs" },
569 MODULE_DEVICE_TABLE(of
, sysc_match
);
571 static struct platform_driver sysc_driver
= {
573 .remove
= sysc_remove
,
576 .of_match_table
= sysc_match
,
580 module_platform_driver(sysc_driver
);
582 MODULE_DESCRIPTION("TI sysc interconnect target driver");
583 MODULE_LICENSE("GPL v2");