blob: e86f7850206afe2c7f8887a3cbdc627054550cb8 [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 Lindgrena54275f2019-03-21 11:00:21 -070050static const char * const clock_names[SYSC_MAX_CLOCKS] = {
51 "fck", "ick", "opt0", "opt1", "opt2", "opt3", "opt4",
52 "opt5", "opt6", "opt7",
53};
Tony Lindgren0eecc632017-10-10 14:23:43 -070054
Tony Lindgrenc5a2de92017-12-15 09:41:23 -080055#define SYSC_IDLEMODE_MASK 3
56#define SYSC_CLOCKACTIVITY_MASK 3
57
Tony Lindgren0eecc632017-10-10 14:23:43 -070058/**
59 * struct sysc - TI sysc interconnect target module registers and capabilities
60 * @dev: struct device pointer
61 * @module_pa: physical address of the interconnect target module
62 * @module_size: size of the interconnect target module
63 * @module_va: virtual address of the interconnect target module
64 * @offsets: register offsets from module base
65 * @clocks: clocks used by the interconnect target module
Tony Lindgren09dfe582018-04-16 10:25:52 -070066 * @clock_roles: clock role names for the found clocks
67 * @nr_clocks: number of clocks used by the interconnect target module
Tony Lindgren0eecc632017-10-10 14:23:43 -070068 * @legacy_mode: configured for legacy mode if set
Tony Lindgren70a65242017-12-15 09:41:09 -080069 * @cap: interconnect target module capabilities
70 * @cfg: interconnect target module configuration
Tony Lindgren566a9b052017-12-15 09:41:19 -080071 * @name: name if available
72 * @revision: interconnect target module revision
Tony Lindgren62020f22018-02-22 13:59:44 -080073 * @needs_resume: runtime resume needed on resume from suspend
Tony Lindgren0eecc632017-10-10 14:23:43 -070074 */
75struct sysc {
76 struct device *dev;
77 u64 module_pa;
78 u32 module_size;
79 void __iomem *module_va;
80 int offsets[SYSC_MAX_REGS];
Tony Lindgrena3e92e72019-03-21 11:00:21 -070081 struct ti_sysc_module_data *mdata;
Tony Lindgren09dfe582018-04-16 10:25:52 -070082 struct clk **clocks;
83 const char **clock_roles;
84 int nr_clocks;
Tony Lindgren50622362018-04-16 10:20:27 -070085 struct reset_control *rsts;
Tony Lindgren0eecc632017-10-10 14:23:43 -070086 const char *legacy_mode;
Tony Lindgren70a65242017-12-15 09:41:09 -080087 const struct sysc_capabilities *cap;
88 struct sysc_config cfg;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -080089 struct ti_sysc_cookie cookie;
Tony Lindgren566a9b052017-12-15 09:41:19 -080090 const char *name;
91 u32 revision;
Tony Lindgren62020f22018-02-22 13:59:44 -080092 bool enabled;
93 bool needs_resume;
Tony Lindgrena885f0f2018-02-22 14:03:48 -080094 bool child_needs_resume;
Tony Lindgren76f0f772018-02-23 08:28:45 -080095 struct delayed_work idle_work;
Tony Lindgren0eecc632017-10-10 14:23:43 -070096};
97
Tony Lindgren4014c082018-12-10 14:11:26 -080098static void sysc_parse_dts_quirks(struct sysc *ddata, struct device_node *np,
99 bool is_child);
100
Tony Lindgrenb7182b42019-03-21 11:00:21 -0700101static void sysc_write(struct sysc *ddata, int offset, u32 value)
Faiz Abbas596e7952018-07-09 22:18:39 +0530102{
103 writel_relaxed(value, ddata->module_va + offset);
104}
105
Tony Lindgren566a9b052017-12-15 09:41:19 -0800106static u32 sysc_read(struct sysc *ddata, int offset)
107{
108 if (ddata->cfg.quirks & SYSC_QUIRK_16BIT) {
109 u32 val;
110
111 val = readw_relaxed(ddata->module_va + offset);
112 val |= (readw_relaxed(ddata->module_va + offset + 4) << 16);
113
114 return val;
115 }
116
117 return readl_relaxed(ddata->module_va + offset);
118}
119
Tony Lindgren09dfe582018-04-16 10:25:52 -0700120static bool sysc_opt_clks_needed(struct sysc *ddata)
121{
122 return !!(ddata->cfg.quirks & SYSC_QUIRK_OPT_CLKS_NEEDED);
123}
124
Tony Lindgren0eecc632017-10-10 14:23:43 -0700125static u32 sysc_read_revision(struct sysc *ddata)
126{
Tony Lindgren566a9b052017-12-15 09:41:19 -0800127 int offset = ddata->offsets[SYSC_REVISION];
128
129 if (offset < 0)
130 return 0;
131
132 return sysc_read(ddata, offset);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700133}
134
Tony Lindgrena54275f2019-03-21 11:00:21 -0700135static int sysc_add_named_clock_from_child(struct sysc *ddata,
136 const char *name,
137 const char *optfck_name)
138{
139 struct device_node *np = ddata->dev->of_node;
140 struct device_node *child;
141 struct clk_lookup *cl;
142 struct clk *clock;
143 const char *n;
144
145 if (name)
146 n = name;
147 else
148 n = optfck_name;
149
150 /* Does the clock alias already exist? */
151 clock = of_clk_get_by_name(np, n);
152 if (!IS_ERR(clock)) {
153 clk_put(clock);
154
155 return 0;
156 }
157
158 child = of_get_next_available_child(np, NULL);
159 if (!child)
160 return -ENODEV;
161
162 clock = devm_get_clk_from_child(ddata->dev, child, name);
163 if (IS_ERR(clock))
164 return PTR_ERR(clock);
165
166 /*
167 * Use clkdev_add() instead of clkdev_alloc() to avoid the MAX_DEV_ID
168 * limit for clk_get(). If cl ever needs to be freed, it should be done
169 * with clkdev_drop().
170 */
171 cl = kcalloc(1, sizeof(*cl), GFP_KERNEL);
172 if (!cl)
173 return -ENOMEM;
174
175 cl->con_id = n;
176 cl->dev_id = dev_name(ddata->dev);
177 cl->clk = clock;
178 clkdev_add(cl);
179
180 clk_put(clock);
181
182 return 0;
183}
184
185static int sysc_init_ext_opt_clock(struct sysc *ddata, const char *name)
186{
187 const char *optfck_name;
188 int error, index;
189
190 if (ddata->nr_clocks < SYSC_OPTFCK0)
191 index = SYSC_OPTFCK0;
192 else
193 index = ddata->nr_clocks;
194
195 if (name)
196 optfck_name = name;
197 else
198 optfck_name = clock_names[index];
199
200 error = sysc_add_named_clock_from_child(ddata, name, optfck_name);
201 if (error)
202 return error;
203
204 ddata->clock_roles[index] = optfck_name;
205 ddata->nr_clocks++;
206
207 return 0;
208}
209
Tony Lindgren09dfe582018-04-16 10:25:52 -0700210static int sysc_get_one_clock(struct sysc *ddata, const char *name)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700211{
Tony Lindgren09dfe582018-04-16 10:25:52 -0700212 int error, i, index = -ENODEV;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700213
Tony Lindgren09dfe582018-04-16 10:25:52 -0700214 if (!strncmp(clock_names[SYSC_FCK], name, 3))
215 index = SYSC_FCK;
216 else if (!strncmp(clock_names[SYSC_ICK], name, 3))
217 index = SYSC_ICK;
218
219 if (index < 0) {
220 for (i = SYSC_OPTFCK0; i < SYSC_MAX_CLOCKS; i++) {
Tony Lindgrenc97c8622018-05-18 07:54:44 -0700221 if (!ddata->clocks[i]) {
Tony Lindgren09dfe582018-04-16 10:25:52 -0700222 index = i;
223 break;
224 }
225 }
Tony Lindgren0eecc632017-10-10 14:23:43 -0700226 }
Tony Lindgren09dfe582018-04-16 10:25:52 -0700227
228 if (index < 0) {
229 dev_err(ddata->dev, "clock %s not added\n", name);
230 return index;
231 }
Tony Lindgren0eecc632017-10-10 14:23:43 -0700232
233 ddata->clocks[index] = devm_clk_get(ddata->dev, name);
234 if (IS_ERR(ddata->clocks[index])) {
235 if (PTR_ERR(ddata->clocks[index]) == -ENOENT)
236 return 0;
237
238 dev_err(ddata->dev, "clock get error for %s: %li\n",
239 name, PTR_ERR(ddata->clocks[index]));
240
241 return PTR_ERR(ddata->clocks[index]);
242 }
243
244 error = clk_prepare(ddata->clocks[index]);
245 if (error) {
246 dev_err(ddata->dev, "clock prepare error for %s: %i\n",
247 name, error);
248
249 return error;
250 }
251
252 return 0;
253}
254
255static int sysc_get_clocks(struct sysc *ddata)
256{
Tony Lindgren09dfe582018-04-16 10:25:52 -0700257 struct device_node *np = ddata->dev->of_node;
258 struct property *prop;
259 const char *name;
260 int nr_fck = 0, nr_ick = 0, i, error = 0;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700261
Kees Cook20749052018-06-18 10:45:49 -0700262 ddata->clock_roles = devm_kcalloc(ddata->dev,
Tony Lindgren09dfe582018-04-16 10:25:52 -0700263 SYSC_MAX_CLOCKS,
Kees Cook20749052018-06-18 10:45:49 -0700264 sizeof(*ddata->clock_roles),
Tony Lindgren09dfe582018-04-16 10:25:52 -0700265 GFP_KERNEL);
266 if (!ddata->clock_roles)
267 return -ENOMEM;
268
269 of_property_for_each_string(np, "clock-names", prop, name) {
270 if (!strncmp(clock_names[SYSC_FCK], name, 3))
271 nr_fck++;
272 if (!strncmp(clock_names[SYSC_ICK], name, 3))
273 nr_ick++;
274 ddata->clock_roles[ddata->nr_clocks] = name;
275 ddata->nr_clocks++;
276 }
277
278 if (ddata->nr_clocks < 1)
279 return 0;
280
Tony Lindgrena54275f2019-03-21 11:00:21 -0700281 if ((ddata->cfg.quirks & SYSC_QUIRK_EXT_OPT_CLOCK)) {
282 error = sysc_init_ext_opt_clock(ddata, NULL);
283 if (error)
284 return error;
285 }
286
Tony Lindgren09dfe582018-04-16 10:25:52 -0700287 if (ddata->nr_clocks > SYSC_MAX_CLOCKS) {
288 dev_err(ddata->dev, "too many clocks for %pOF\n", np);
289
290 return -EINVAL;
291 }
292
293 if (nr_fck > 1 || nr_ick > 1) {
294 dev_err(ddata->dev, "max one fck and ick for %pOF\n", np);
295
296 return -EINVAL;
297 }
298
Kees Cook20749052018-06-18 10:45:49 -0700299 ddata->clocks = devm_kcalloc(ddata->dev,
300 ddata->nr_clocks, sizeof(*ddata->clocks),
Tony Lindgren09dfe582018-04-16 10:25:52 -0700301 GFP_KERNEL);
302 if (!ddata->clocks)
303 return -ENOMEM;
304
Tony Lindgren7b4f8ac2018-11-15 14:46:52 -0800305 for (i = 0; i < SYSC_MAX_CLOCKS; i++) {
306 const char *name = ddata->clock_roles[i];
307
308 if (!name)
309 continue;
310
311 error = sysc_get_one_clock(ddata, name);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700312 if (error && error != -ENOENT)
313 return error;
314 }
315
316 return 0;
317}
318
Tony Lindgrend8789702019-03-21 11:00:21 -0700319static int sysc_enable_main_clocks(struct sysc *ddata)
320{
321 struct clk *clock;
322 int i, error;
323
324 if (!ddata->clocks)
325 return 0;
326
327 for (i = 0; i < SYSC_OPTFCK0; i++) {
328 clock = ddata->clocks[i];
329
330 /* Main clocks may not have ick */
331 if (IS_ERR_OR_NULL(clock))
332 continue;
333
334 error = clk_enable(clock);
335 if (error)
336 goto err_disable;
337 }
338
339 return 0;
340
341err_disable:
342 for (i--; i >= 0; i--) {
343 clock = ddata->clocks[i];
344
345 /* Main clocks may not have ick */
346 if (IS_ERR_OR_NULL(clock))
347 continue;
348
349 clk_disable(clock);
350 }
351
352 return error;
353}
354
355static void sysc_disable_main_clocks(struct sysc *ddata)
356{
357 struct clk *clock;
358 int i;
359
360 if (!ddata->clocks)
361 return;
362
363 for (i = 0; i < SYSC_OPTFCK0; i++) {
364 clock = ddata->clocks[i];
365 if (IS_ERR_OR_NULL(clock))
366 continue;
367
368 clk_disable(clock);
369 }
370}
371
372static int sysc_enable_opt_clocks(struct sysc *ddata)
373{
374 struct clk *clock;
375 int i, error;
376
377 if (!ddata->clocks)
378 return 0;
379
380 for (i = SYSC_OPTFCK0; i < SYSC_MAX_CLOCKS; i++) {
381 clock = ddata->clocks[i];
382
383 /* Assume no holes for opt clocks */
384 if (IS_ERR_OR_NULL(clock))
385 return 0;
386
387 error = clk_enable(clock);
388 if (error)
389 goto err_disable;
390 }
391
392 return 0;
393
394err_disable:
395 for (i--; i >= 0; i--) {
396 clock = ddata->clocks[i];
397 if (IS_ERR_OR_NULL(clock))
398 continue;
399
400 clk_disable(clock);
401 }
402
403 return error;
404}
405
406static void sysc_disable_opt_clocks(struct sysc *ddata)
407{
408 struct clk *clock;
409 int i;
410
411 if (!ddata->clocks)
412 return;
413
414 for (i = SYSC_OPTFCK0; i < SYSC_MAX_CLOCKS; i++) {
415 clock = ddata->clocks[i];
416
417 /* Assume no holes for opt clocks */
418 if (IS_ERR_OR_NULL(clock))
419 return;
420
421 clk_disable(clock);
422 }
423}
424
Tony Lindgren2b2f7de2019-05-27 04:51:53 -0700425static void sysc_clkdm_deny_idle(struct sysc *ddata)
426{
427 struct ti_sysc_platform_data *pdata;
428
429 if (ddata->legacy_mode)
430 return;
431
432 pdata = dev_get_platdata(ddata->dev);
433 if (pdata && pdata->clkdm_deny_idle)
434 pdata->clkdm_deny_idle(ddata->dev, &ddata->cookie);
435}
436
437static void sysc_clkdm_allow_idle(struct sysc *ddata)
438{
439 struct ti_sysc_platform_data *pdata;
440
441 if (ddata->legacy_mode)
442 return;
443
444 pdata = dev_get_platdata(ddata->dev);
445 if (pdata && pdata->clkdm_allow_idle)
446 pdata->clkdm_allow_idle(ddata->dev, &ddata->cookie);
447}
448
Tony Lindgren0eecc632017-10-10 14:23:43 -0700449/**
Tony Lindgrenb11c1ea2019-03-21 11:00:21 -0700450 * sysc_init_resets - init rstctrl reset line if configured
Tony Lindgren50622362018-04-16 10:20:27 -0700451 * @ddata: device driver data
452 *
Tony Lindgrenb11c1ea2019-03-21 11:00:21 -0700453 * See sysc_rstctrl_reset_deassert().
Tony Lindgren50622362018-04-16 10:20:27 -0700454 */
455static int sysc_init_resets(struct sysc *ddata)
456{
Tony Lindgren50622362018-04-16 10:20:27 -0700457 ddata->rsts =
458 devm_reset_control_array_get_optional_exclusive(ddata->dev);
459 if (IS_ERR(ddata->rsts))
460 return PTR_ERR(ddata->rsts);
461
Tony Lindgren50622362018-04-16 10:20:27 -0700462 return 0;
463}
464
465/**
Tony Lindgren0eecc632017-10-10 14:23:43 -0700466 * sysc_parse_and_check_child_range - parses module IO region from ranges
467 * @ddata: device driver data
468 *
469 * In general we only need rev, syss, and sysc registers and not the whole
470 * module range. But we do want the offsets for these registers from the
471 * module base. This allows us to check them against the legacy hwmod
472 * platform data. Let's also check the ranges are configured properly.
473 */
474static int sysc_parse_and_check_child_range(struct sysc *ddata)
475{
476 struct device_node *np = ddata->dev->of_node;
477 const __be32 *ranges;
478 u32 nr_addr, nr_size;
479 int len, error;
480
481 ranges = of_get_property(np, "ranges", &len);
482 if (!ranges) {
483 dev_err(ddata->dev, "missing ranges for %pOF\n", np);
484
485 return -ENOENT;
486 }
487
488 len /= sizeof(*ranges);
489
490 if (len < 3) {
491 dev_err(ddata->dev, "incomplete ranges for %pOF\n", np);
492
493 return -EINVAL;
494 }
495
496 error = of_property_read_u32(np, "#address-cells", &nr_addr);
497 if (error)
498 return -ENOENT;
499
500 error = of_property_read_u32(np, "#size-cells", &nr_size);
501 if (error)
502 return -ENOENT;
503
504 if (nr_addr != 1 || nr_size != 1) {
505 dev_err(ddata->dev, "invalid ranges for %pOF\n", np);
506
507 return -EINVAL;
508 }
509
510 ranges++;
511 ddata->module_pa = of_translate_address(np, ranges++);
512 ddata->module_size = be32_to_cpup(ranges);
513
Tony Lindgren0eecc632017-10-10 14:23:43 -0700514 return 0;
515}
516
Tony Lindgren3bb37c82018-02-22 14:05:14 -0800517static struct device_node *stdout_path;
518
519static void sysc_init_stdout_path(struct sysc *ddata)
520{
521 struct device_node *np = NULL;
522 const char *uart;
523
524 if (IS_ERR(stdout_path))
525 return;
526
527 if (stdout_path)
528 return;
529
530 np = of_find_node_by_path("/chosen");
531 if (!np)
532 goto err;
533
534 uart = of_get_property(np, "stdout-path", NULL);
535 if (!uart)
536 goto err;
537
538 np = of_find_node_by_path(uart);
539 if (!np)
540 goto err;
541
542 stdout_path = np;
543
544 return;
545
546err:
547 stdout_path = ERR_PTR(-ENODEV);
548}
549
550static void sysc_check_quirk_stdout(struct sysc *ddata,
551 struct device_node *np)
552{
553 sysc_init_stdout_path(ddata);
554 if (np != stdout_path)
555 return;
556
557 ddata->cfg.quirks |= SYSC_QUIRK_NO_IDLE_ON_INIT |
558 SYSC_QUIRK_NO_RESET_ON_INIT;
559}
560
Tony Lindgren0eecc632017-10-10 14:23:43 -0700561/**
562 * sysc_check_one_child - check child configuration
563 * @ddata: device driver data
564 * @np: child device node
565 *
566 * Let's avoid messy situations where we have new interconnect target
567 * node but children have "ti,hwmods". These belong to the interconnect
568 * target node and are managed by this driver.
569 */
570static int sysc_check_one_child(struct sysc *ddata,
571 struct device_node *np)
572{
573 const char *name;
574
575 name = of_get_property(np, "ti,hwmods", NULL);
576 if (name)
577 dev_warn(ddata->dev, "really a child ti,hwmods property?");
578
Tony Lindgren3bb37c82018-02-22 14:05:14 -0800579 sysc_check_quirk_stdout(ddata, np);
Tony Lindgren4014c082018-12-10 14:11:26 -0800580 sysc_parse_dts_quirks(ddata, np, true);
Tony Lindgren3bb37c82018-02-22 14:05:14 -0800581
Tony Lindgren0eecc632017-10-10 14:23:43 -0700582 return 0;
583}
584
585static int sysc_check_children(struct sysc *ddata)
586{
587 struct device_node *child;
588 int error;
589
590 for_each_child_of_node(ddata->dev->of_node, child) {
591 error = sysc_check_one_child(ddata, child);
592 if (error)
593 return error;
594 }
595
596 return 0;
597}
598
Tony Lindgrena7199e22017-12-15 09:41:14 -0800599/*
600 * So far only I2C uses 16-bit read access with clockactivity with revision
601 * in two registers with stride of 4. We can detect this based on the rev
602 * register size to configure things far enough to be able to properly read
603 * the revision register.
604 */
605static void sysc_check_quirk_16bit(struct sysc *ddata, struct resource *res)
606{
Tony Lindgrendd57ac12018-02-22 14:09:57 -0800607 if (resource_size(res) == 8)
Tony Lindgrena7199e22017-12-15 09:41:14 -0800608 ddata->cfg.quirks |= SYSC_QUIRK_16BIT | SYSC_QUIRK_USE_CLOCKACT;
Tony Lindgrena7199e22017-12-15 09:41:14 -0800609}
610
Tony Lindgren0eecc632017-10-10 14:23:43 -0700611/**
612 * sysc_parse_one - parses the interconnect target module registers
613 * @ddata: device driver data
614 * @reg: register to parse
615 */
616static int sysc_parse_one(struct sysc *ddata, enum sysc_registers reg)
617{
618 struct resource *res;
619 const char *name;
620
621 switch (reg) {
622 case SYSC_REVISION:
623 case SYSC_SYSCONFIG:
624 case SYSC_SYSSTATUS:
625 name = reg_names[reg];
626 break;
627 default:
628 return -EINVAL;
629 }
630
631 res = platform_get_resource_byname(to_platform_device(ddata->dev),
632 IORESOURCE_MEM, name);
633 if (!res) {
Tony Lindgren0eecc632017-10-10 14:23:43 -0700634 ddata->offsets[reg] = -ENODEV;
635
636 return 0;
637 }
638
639 ddata->offsets[reg] = res->start - ddata->module_pa;
Tony Lindgrena7199e22017-12-15 09:41:14 -0800640 if (reg == SYSC_REVISION)
641 sysc_check_quirk_16bit(ddata, res);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700642
643 return 0;
644}
645
646static int sysc_parse_registers(struct sysc *ddata)
647{
648 int i, error;
649
650 for (i = 0; i < SYSC_MAX_REGS; i++) {
651 error = sysc_parse_one(ddata, i);
652 if (error)
653 return error;
654 }
655
656 return 0;
657}
658
659/**
660 * sysc_check_registers - check for misconfigured register overlaps
661 * @ddata: device driver data
662 */
663static int sysc_check_registers(struct sysc *ddata)
664{
665 int i, j, nr_regs = 0, nr_matches = 0;
666
667 for (i = 0; i < SYSC_MAX_REGS; i++) {
668 if (ddata->offsets[i] < 0)
669 continue;
670
671 if (ddata->offsets[i] > (ddata->module_size - 4)) {
672 dev_err(ddata->dev, "register outside module range");
673
674 return -EINVAL;
675 }
676
677 for (j = 0; j < SYSC_MAX_REGS; j++) {
678 if (ddata->offsets[j] < 0)
679 continue;
680
681 if (ddata->offsets[i] == ddata->offsets[j])
682 nr_matches++;
683 }
684 nr_regs++;
685 }
686
Tony Lindgren0eecc632017-10-10 14:23:43 -0700687 if (nr_matches > nr_regs) {
688 dev_err(ddata->dev, "overlapping registers: (%i/%i)",
689 nr_regs, nr_matches);
690
691 return -EINVAL;
692 }
693
694 return 0;
695}
696
697/**
698 * syc_ioremap - ioremap register space for the interconnect target module
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700699 * @ddata: device driver data
Tony Lindgren0eecc632017-10-10 14:23:43 -0700700 *
701 * Note that the interconnect target module registers can be anywhere
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700702 * within the interconnect target module range. For example, SGX has
703 * them at offset 0x1fc00 in the 32MB module address space. And cpsw
704 * has them at offset 0x1200 in the CPSW_WR child. Usually the
705 * the interconnect target module registers are at the beginning of
706 * the module range though.
Tony Lindgren0eecc632017-10-10 14:23:43 -0700707 */
708static int sysc_ioremap(struct sysc *ddata)
709{
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700710 int size;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700711
Tony Lindgrene4f50c82019-05-01 14:24:57 -0700712 if (ddata->offsets[SYSC_REVISION] < 0 &&
713 ddata->offsets[SYSC_SYSCONFIG] < 0 &&
714 ddata->offsets[SYSC_SYSSTATUS] < 0) {
715 size = ddata->module_size;
716 } else {
717 size = max3(ddata->offsets[SYSC_REVISION],
718 ddata->offsets[SYSC_SYSCONFIG],
719 ddata->offsets[SYSC_SYSSTATUS]);
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700720
Tony Lindgrene4f50c82019-05-01 14:24:57 -0700721 if ((size + sizeof(u32)) > ddata->module_size)
722 return -EINVAL;
723 }
Tony Lindgren0eecc632017-10-10 14:23:43 -0700724
Tony Lindgren0eecc632017-10-10 14:23:43 -0700725 ddata->module_va = devm_ioremap(ddata->dev,
726 ddata->module_pa,
Tony Lindgren0ef8e3b2018-08-08 01:07:05 -0700727 size + sizeof(u32));
Tony Lindgren0eecc632017-10-10 14:23:43 -0700728 if (!ddata->module_va)
729 return -EIO;
730
731 return 0;
732}
733
734/**
735 * sysc_map_and_check_registers - ioremap and check device registers
736 * @ddata: device driver data
737 */
738static int sysc_map_and_check_registers(struct sysc *ddata)
739{
740 int error;
741
742 error = sysc_parse_and_check_child_range(ddata);
743 if (error)
744 return error;
745
746 error = sysc_check_children(ddata);
747 if (error)
748 return error;
749
750 error = sysc_parse_registers(ddata);
751 if (error)
752 return error;
753
754 error = sysc_ioremap(ddata);
755 if (error)
756 return error;
757
758 error = sysc_check_registers(ddata);
759 if (error)
760 return error;
761
762 return 0;
763}
764
765/**
766 * sysc_show_rev - read and show interconnect target module revision
767 * @bufp: buffer to print the information to
768 * @ddata: device driver data
769 */
770static int sysc_show_rev(char *bufp, struct sysc *ddata)
771{
Tony Lindgren566a9b052017-12-15 09:41:19 -0800772 int len;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700773
774 if (ddata->offsets[SYSC_REVISION] < 0)
775 return sprintf(bufp, ":NA");
776
Tony Lindgren566a9b052017-12-15 09:41:19 -0800777 len = sprintf(bufp, ":%08x", ddata->revision);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700778
779 return len;
780}
781
782static int sysc_show_reg(struct sysc *ddata,
783 char *bufp, enum sysc_registers reg)
784{
785 if (ddata->offsets[reg] < 0)
786 return sprintf(bufp, ":NA");
787
788 return sprintf(bufp, ":%x", ddata->offsets[reg]);
789}
790
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800791static int sysc_show_name(char *bufp, struct sysc *ddata)
792{
793 if (!ddata->name)
794 return 0;
795
796 return sprintf(bufp, ":%s", ddata->name);
797}
798
Tony Lindgren0eecc632017-10-10 14:23:43 -0700799/**
800 * sysc_show_registers - show information about interconnect target module
801 * @ddata: device driver data
802 */
803static void sysc_show_registers(struct sysc *ddata)
804{
805 char buf[128];
806 char *bufp = buf;
807 int i;
808
809 for (i = 0; i < SYSC_MAX_REGS; i++)
810 bufp += sysc_show_reg(ddata, bufp, i);
811
812 bufp += sysc_show_rev(bufp, ddata);
Tony Lindgrena885f0f2018-02-22 14:03:48 -0800813 bufp += sysc_show_name(bufp, ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -0700814
815 dev_dbg(ddata->dev, "%llx:%x%s\n",
816 ddata->module_pa, ddata->module_size,
817 buf);
818}
819
Roger Quadrosd59b6052019-04-08 12:52:39 +0300820#define SYSC_IDLE_MASK (SYSC_NR_IDLEMODES - 1)
821
Tony Lindgren2b2f7de2019-05-27 04:51:53 -0700822/* Caller needs to manage sysc_clkdm_deny_idle() and sysc_clkdm_allow_idle() */
Roger Quadrosd59b6052019-04-08 12:52:39 +0300823static int sysc_enable_module(struct device *dev)
824{
825 struct sysc *ddata;
826 const struct sysc_regbits *regbits;
827 u32 reg, idlemodes, best_mode;
828
829 ddata = dev_get_drvdata(dev);
830 if (ddata->offsets[SYSC_SYSCONFIG] == -ENODEV)
831 return 0;
832
Roger Quadrosd59b6052019-04-08 12:52:39 +0300833 regbits = ddata->cap->regbits;
834 reg = sysc_read(ddata, ddata->offsets[SYSC_SYSCONFIG]);
835
836 /* Set SIDLE mode */
837 idlemodes = ddata->cfg.sidlemodes;
838 if (!idlemodes || regbits->sidle_shift < 0)
839 goto set_midle;
840
841 best_mode = fls(ddata->cfg.sidlemodes) - 1;
842 if (best_mode > SYSC_IDLE_MASK) {
843 dev_err(dev, "%s: invalid sidlemode\n", __func__);
844 return -EINVAL;
845 }
846
847 reg &= ~(SYSC_IDLE_MASK << regbits->sidle_shift);
848 reg |= best_mode << regbits->sidle_shift;
849 sysc_write(ddata, ddata->offsets[SYSC_SYSCONFIG], reg);
850
851set_midle:
852 /* Set MIDLE mode */
853 idlemodes = ddata->cfg.midlemodes;
854 if (!idlemodes || regbits->midle_shift < 0)
855 return 0;
856
857 best_mode = fls(ddata->cfg.midlemodes) - 1;
858 if (best_mode > SYSC_IDLE_MASK) {
859 dev_err(dev, "%s: invalid midlemode\n", __func__);
860 return -EINVAL;
861 }
862
863 reg &= ~(SYSC_IDLE_MASK << regbits->midle_shift);
864 reg |= best_mode << regbits->midle_shift;
865 sysc_write(ddata, ddata->offsets[SYSC_SYSCONFIG], reg);
866
867 return 0;
868}
869
870static int sysc_best_idle_mode(u32 idlemodes, u32 *best_mode)
871{
872 if (idlemodes & BIT(SYSC_IDLE_SMART_WKUP))
873 *best_mode = SYSC_IDLE_SMART_WKUP;
874 else if (idlemodes & BIT(SYSC_IDLE_SMART))
875 *best_mode = SYSC_IDLE_SMART;
876 else if (idlemodes & SYSC_IDLE_FORCE)
877 *best_mode = SYSC_IDLE_FORCE;
878 else
879 return -EINVAL;
880
881 return 0;
882}
883
Tony Lindgren2b2f7de2019-05-27 04:51:53 -0700884/* Caller needs to manage sysc_clkdm_deny_idle() and sysc_clkdm_allow_idle() */
Roger Quadrosd59b6052019-04-08 12:52:39 +0300885static int sysc_disable_module(struct device *dev)
886{
887 struct sysc *ddata;
888 const struct sysc_regbits *regbits;
889 u32 reg, idlemodes, best_mode;
890 int ret;
891
892 ddata = dev_get_drvdata(dev);
893 if (ddata->offsets[SYSC_SYSCONFIG] == -ENODEV)
894 return 0;
895
Roger Quadrosd59b6052019-04-08 12:52:39 +0300896 regbits = ddata->cap->regbits;
897 reg = sysc_read(ddata, ddata->offsets[SYSC_SYSCONFIG]);
898
899 /* Set MIDLE mode */
900 idlemodes = ddata->cfg.midlemodes;
901 if (!idlemodes || regbits->midle_shift < 0)
902 goto set_sidle;
903
904 ret = sysc_best_idle_mode(idlemodes, &best_mode);
905 if (ret) {
906 dev_err(dev, "%s: invalid midlemode\n", __func__);
907 return ret;
908 }
909
910 reg &= ~(SYSC_IDLE_MASK << regbits->midle_shift);
911 reg |= best_mode << regbits->midle_shift;
912 sysc_write(ddata, ddata->offsets[SYSC_SYSCONFIG], reg);
913
914set_sidle:
915 /* Set SIDLE mode */
916 idlemodes = ddata->cfg.sidlemodes;
917 if (!idlemodes || regbits->sidle_shift < 0)
918 return 0;
919
920 ret = sysc_best_idle_mode(idlemodes, &best_mode);
921 if (ret) {
922 dev_err(dev, "%s: invalid sidlemode\n", __func__);
923 return ret;
924 }
925
926 reg &= ~(SYSC_IDLE_MASK << regbits->sidle_shift);
927 reg |= best_mode << regbits->sidle_shift;
928 sysc_write(ddata, ddata->offsets[SYSC_SYSCONFIG], reg);
929
930 return 0;
931}
932
Tony Lindgrenff437282019-03-21 11:00:21 -0700933static int __maybe_unused sysc_runtime_suspend_legacy(struct device *dev,
934 struct sysc *ddata)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700935{
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800936 struct ti_sysc_platform_data *pdata;
Tony Lindgrenff437282019-03-21 11:00:21 -0700937 int error;
938
939 pdata = dev_get_platdata(ddata->dev);
940 if (!pdata)
941 return 0;
942
943 if (!pdata->idle_module)
944 return -ENODEV;
945
946 error = pdata->idle_module(dev, &ddata->cookie);
947 if (error)
948 dev_err(dev, "%s: could not idle: %i\n",
949 __func__, error);
950
951 return 0;
952}
953
954static int __maybe_unused sysc_runtime_resume_legacy(struct device *dev,
955 struct sysc *ddata)
956{
957 struct ti_sysc_platform_data *pdata;
958 int error;
959
960 pdata = dev_get_platdata(ddata->dev);
961 if (!pdata)
962 return 0;
963
964 if (!pdata->enable_module)
965 return -ENODEV;
966
967 error = pdata->enable_module(dev, &ddata->cookie);
968 if (error)
969 dev_err(dev, "%s: could not enable: %i\n",
970 __func__, error);
971
972 return 0;
973}
974
975static int __maybe_unused sysc_runtime_suspend(struct device *dev)
976{
Tony Lindgren0eecc632017-10-10 14:23:43 -0700977 struct sysc *ddata;
Tony Lindgrend8789702019-03-21 11:00:21 -0700978 int error = 0;
Tony Lindgren0eecc632017-10-10 14:23:43 -0700979
980 ddata = dev_get_drvdata(dev);
981
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800982 if (!ddata->enabled)
Tony Lindgren0eecc632017-10-10 14:23:43 -0700983 return 0;
984
Tony Lindgren2b2f7de2019-05-27 04:51:53 -0700985 sysc_clkdm_deny_idle(ddata);
986
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800987 if (ddata->legacy_mode) {
Tony Lindgrenff437282019-03-21 11:00:21 -0700988 error = sysc_runtime_suspend_legacy(dev, ddata);
Tony Lindgren93de83a2019-03-21 11:00:21 -0700989 if (error)
Tony Lindgren2b2f7de2019-05-27 04:51:53 -0700990 goto err_allow_idle;
Roger Quadrosd59b6052019-04-08 12:52:39 +0300991 } else {
992 error = sysc_disable_module(dev);
993 if (error)
Tony Lindgren2b2f7de2019-05-27 04:51:53 -0700994 goto err_allow_idle;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -0800995 }
996
Tony Lindgrend8789702019-03-21 11:00:21 -0700997 sysc_disable_main_clocks(ddata);
Tony Lindgren09dfe582018-04-16 10:25:52 -0700998
Tony Lindgrend8789702019-03-21 11:00:21 -0700999 if (sysc_opt_clks_needed(ddata))
1000 sysc_disable_opt_clocks(ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001001
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001002 ddata->enabled = false;
1003
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001004err_allow_idle:
1005 sysc_clkdm_allow_idle(ddata);
1006
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001007 return error;
Tony Lindgren0eecc632017-10-10 14:23:43 -07001008}
1009
Arnd Bergmanna4a5d492017-10-13 11:25:32 +02001010static int __maybe_unused sysc_runtime_resume(struct device *dev)
Tony Lindgren0eecc632017-10-10 14:23:43 -07001011{
1012 struct sysc *ddata;
Tony Lindgrend8789702019-03-21 11:00:21 -07001013 int error = 0;
Tony Lindgren0eecc632017-10-10 14:23:43 -07001014
1015 ddata = dev_get_drvdata(dev);
1016
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001017 if (ddata->enabled)
Tony Lindgren0eecc632017-10-10 14:23:43 -07001018 return 0;
1019
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001020 sysc_clkdm_deny_idle(ddata);
1021
Tony Lindgrend8789702019-03-21 11:00:21 -07001022 if (sysc_opt_clks_needed(ddata)) {
1023 error = sysc_enable_opt_clocks(ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -07001024 if (error)
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001025 goto err_allow_idle;
Tony Lindgren0eecc632017-10-10 14:23:43 -07001026 }
1027
Tony Lindgrend8789702019-03-21 11:00:21 -07001028 error = sysc_enable_main_clocks(ddata);
1029 if (error)
Tony Lindgren93de83a2019-03-21 11:00:21 -07001030 goto err_opt_clocks;
1031
1032 if (ddata->legacy_mode) {
1033 error = sysc_runtime_resume_legacy(dev, ddata);
1034 if (error)
1035 goto err_main_clocks;
Roger Quadrosd59b6052019-04-08 12:52:39 +03001036 } else {
1037 error = sysc_enable_module(dev);
1038 if (error)
1039 goto err_main_clocks;
Tony Lindgren93de83a2019-03-21 11:00:21 -07001040 }
Tony Lindgrend8789702019-03-21 11:00:21 -07001041
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001042 ddata->enabled = true;
1043
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001044 sysc_clkdm_allow_idle(ddata);
1045
Tony Lindgrend8789702019-03-21 11:00:21 -07001046 return 0;
1047
1048err_main_clocks:
Tony Lindgren93de83a2019-03-21 11:00:21 -07001049 sysc_disable_main_clocks(ddata);
1050err_opt_clocks:
Tony Lindgrend8789702019-03-21 11:00:21 -07001051 if (sysc_opt_clks_needed(ddata))
1052 sysc_disable_opt_clocks(ddata);
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001053err_allow_idle:
1054 sysc_clkdm_allow_idle(ddata);
Tony Lindgrend8789702019-03-21 11:00:21 -07001055
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08001056 return error;
Tony Lindgren0eecc632017-10-10 14:23:43 -07001057}
1058
Tony Lindgrenf5e802032018-10-01 09:33:28 -07001059static int __maybe_unused sysc_noirq_suspend(struct device *dev)
Tony Lindgren62020f22018-02-22 13:59:44 -08001060{
1061 struct sysc *ddata;
1062
1063 ddata = dev_get_drvdata(dev);
1064
Tony Lindgren40d9f912018-09-24 12:16:54 -07001065 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
Tony Lindgrene7420c22018-04-16 10:26:46 -07001066 return 0;
1067
Tony Lindgrenf5e802032018-10-01 09:33:28 -07001068 return pm_runtime_force_suspend(dev);
Tony Lindgren62020f22018-02-22 13:59:44 -08001069}
1070
Tony Lindgrenf5e802032018-10-01 09:33:28 -07001071static int __maybe_unused sysc_noirq_resume(struct device *dev)
Tony Lindgren62020f22018-02-22 13:59:44 -08001072{
1073 struct sysc *ddata;
1074
1075 ddata = dev_get_drvdata(dev);
Tony Lindgrene7420c22018-04-16 10:26:46 -07001076
Tony Lindgren40d9f912018-09-24 12:16:54 -07001077 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
Tony Lindgrene7420c22018-04-16 10:26:46 -07001078 return 0;
1079
Tony Lindgrenf5e802032018-10-01 09:33:28 -07001080 return pm_runtime_force_resume(dev);
Tony Lindgrene7420c22018-04-16 10:26:46 -07001081}
1082
Tony Lindgren0eecc632017-10-10 14:23:43 -07001083static const struct dev_pm_ops sysc_pm_ops = {
Tony Lindgrene7420c22018-04-16 10:26:46 -07001084 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(sysc_noirq_suspend, sysc_noirq_resume)
Tony Lindgren0eecc632017-10-10 14:23:43 -07001085 SET_RUNTIME_PM_OPS(sysc_runtime_suspend,
1086 sysc_runtime_resume,
1087 NULL)
1088};
1089
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001090/* Module revision register based quirks */
1091struct sysc_revision_quirk {
1092 const char *name;
1093 u32 base;
1094 int rev_offset;
1095 int sysc_offset;
1096 int syss_offset;
1097 u32 revision;
1098 u32 revision_mask;
1099 u32 quirks;
1100};
1101
1102#define SYSC_QUIRK(optname, optbase, optrev, optsysc, optsyss, \
1103 optrev_val, optrevmask, optquirkmask) \
1104 { \
1105 .name = (optname), \
1106 .base = (optbase), \
1107 .rev_offset = (optrev), \
1108 .sysc_offset = (optsysc), \
1109 .syss_offset = (optsyss), \
1110 .revision = (optrev_val), \
1111 .revision_mask = (optrevmask), \
1112 .quirks = (optquirkmask), \
1113 }
1114
1115static const struct sysc_revision_quirk sysc_revision_quirks[] = {
1116 /* These drivers need to be fixed to not use pm_runtime_irq_safe() */
Tony Lindgren3a3d8022018-09-27 13:33:58 -07001117 SYSC_QUIRK("gpio", 0, 0, 0x10, 0x114, 0x50600801, 0xffff00ff,
Tony Lindgren09dfe582018-04-16 10:25:52 -07001118 SYSC_QUIRK_LEGACY_IDLE | SYSC_QUIRK_OPT_CLKS_IN_RESET),
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001119 SYSC_QUIRK("mmu", 0, 0, 0x10, 0x14, 0x00000020, 0xffffffff,
1120 SYSC_QUIRK_LEGACY_IDLE),
1121 SYSC_QUIRK("mmu", 0, 0, 0x10, 0x14, 0x00000030, 0xffffffff,
1122 SYSC_QUIRK_LEGACY_IDLE),
1123 SYSC_QUIRK("sham", 0, 0x100, 0x110, 0x114, 0x40000c03, 0xffffffff,
1124 SYSC_QUIRK_LEGACY_IDLE),
1125 SYSC_QUIRK("smartreflex", 0, -1, 0x24, -1, 0x00000000, 0xffffffff,
1126 SYSC_QUIRK_LEGACY_IDLE),
1127 SYSC_QUIRK("smartreflex", 0, -1, 0x38, -1, 0x00000000, 0xffffffff,
1128 SYSC_QUIRK_LEGACY_IDLE),
1129 SYSC_QUIRK("timer", 0, 0, 0x10, 0x14, 0x00000015, 0xffffffff,
Tony Lindgren9bd34c62019-01-22 09:03:08 -08001130 0),
Tony Lindgren8cde5d52018-04-16 10:25:15 -07001131 /* Some timers on omap4 and later */
Tony Lindgren3a3d8022018-09-27 13:33:58 -07001132 SYSC_QUIRK("timer", 0, 0, 0x10, -1, 0x50002100, 0xffffffff,
Tony Lindgren072167d2019-01-29 07:53:47 -08001133 0),
Tony Lindgren3a3d8022018-09-27 13:33:58 -07001134 SYSC_QUIRK("timer", 0, 0, 0x10, -1, 0x4fff1301, 0xffff00ff,
Tony Lindgren9bd34c62019-01-22 09:03:08 -08001135 0),
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001136 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x00000052, 0xffffffff,
Tony Lindgrenb4a9a7a2019-03-21 13:27:08 -07001137 SYSC_QUIRK_SWSUP_SIDLE_ACT | SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgrend708bb12018-04-16 10:25:32 -07001138 /* Uarts on omap4 and later */
Tony Lindgrenb82beef2018-09-24 12:16:59 -07001139 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x50411e03, 0xffff00ff,
Tony Lindgrenb4a9a7a2019-03-21 13:27:08 -07001140 SYSC_QUIRK_SWSUP_SIDLE_ACT | SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgrenb82beef2018-09-24 12:16:59 -07001141 SYSC_QUIRK("uart", 0, 0x50, 0x54, 0x58, 0x47422e03, 0xffffffff,
Tony Lindgrenb4a9a7a2019-03-21 13:27:08 -07001142 SYSC_QUIRK_SWSUP_SIDLE_ACT | SYSC_QUIRK_LEGACY_IDLE),
Tony Lindgren7e27e5d2018-04-20 14:54:16 -07001143
Tony Lindgrena54275f2019-03-21 11:00:21 -07001144 /* Quirks that need to be set based on the module address */
1145 SYSC_QUIRK("mcpdm", 0x40132000, 0, 0x10, -1, 0x50000800, 0xffffffff,
1146 SYSC_QUIRK_EXT_OPT_CLOCK | SYSC_QUIRK_NO_RESET_ON_INIT |
1147 SYSC_QUIRK_SWSUP_SIDLE),
1148
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001149#ifdef DEBUG
Tony Lindgren1ba30692018-09-24 12:17:05 -07001150 SYSC_QUIRK("adc", 0, 0, 0x10, -1, 0x47300001, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -07001151 SYSC_QUIRK("atl", 0, 0, -1, -1, 0x0a070100, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001152 SYSC_QUIRK("aess", 0, 0, 0x10, -1, 0x40000000, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -07001153 SYSC_QUIRK("cm", 0, 0, -1, -1, 0x40000301, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -07001154 SYSC_QUIRK("control", 0, 0, 0x10, -1, 0x40000900, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001155 SYSC_QUIRK("cpgmac", 0, 0x1200, 0x1208, 0x1204, 0x4edb1902,
Tony Lindgren23731ea2018-09-24 12:17:09 -07001156 0xffff00f0, 0),
1157 SYSC_QUIRK("dcan", 0, 0, -1, -1, 0xffffffff, 0xffffffff, 0),
Tony Lindgren13aad512019-03-21 11:00:21 -07001158 SYSC_QUIRK("dmic", 0, 0, 0x10, -1, 0x50010000, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001159 SYSC_QUIRK("dwc3", 0, 0, 0x10, -1, 0x500a0200, 0xffffffff, 0),
1160 SYSC_QUIRK("epwmss", 0, 0, 0x4, -1, 0x47400001, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001161 SYSC_QUIRK("gpu", 0, 0x1fc00, 0x1fc10, -1, 0, 0, 0),
1162 SYSC_QUIRK("hdq1w", 0, 0, 0x14, 0x18, 0x00000006, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001163 SYSC_QUIRK("hdq1w", 0, 0, 0x14, 0x18, 0x0000000a, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001164 SYSC_QUIRK("hsi", 0, 0, 0x10, 0x14, 0x50043101, 0xffffffff, 0),
1165 SYSC_QUIRK("iss", 0, 0, 0x10, -1, 0x40000101, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -07001166 SYSC_QUIRK("i2c", 0, 0, 0x10, 0x90, 0x5040000a, 0xfffff0f0, 0),
Tony Lindgren23731ea2018-09-24 12:17:09 -07001167 SYSC_QUIRK("lcdc", 0, 0, 0x54, -1, 0x4f201000, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001168 SYSC_QUIRK("mcasp", 0, 0, 0x4, -1, 0x44306302, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001169 SYSC_QUIRK("mcasp", 0, 0, 0x4, -1, 0x44307b02, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001170 SYSC_QUIRK("mcbsp", 0, -1, 0x8c, -1, 0, 0, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -07001171 SYSC_QUIRK("mcspi", 0, 0, 0x10, -1, 0x40300a0b, 0xffff00ff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001172 SYSC_QUIRK("mcspi", 0, 0, 0x110, 0x114, 0x40300a0b, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001173 SYSC_QUIRK("mailbox", 0, 0, 0x10, -1, 0x00000400, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001174 SYSC_QUIRK("m3", 0, 0, -1, -1, 0x5f580105, 0x0fff0f00, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -07001175 SYSC_QUIRK("ocp2scp", 0, 0, 0x10, 0x14, 0x50060005, 0xfffffff0, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001176 SYSC_QUIRK("ocp2scp", 0, 0, -1, -1, 0x50060007, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -07001177 SYSC_QUIRK("padconf", 0, 0, 0x10, -1, 0x4fff0800, 0xffffffff, 0),
Tony Lindgrenf0106702018-11-15 14:46:53 -08001178 SYSC_QUIRK("padconf", 0, 0, -1, -1, 0x40001100, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -07001179 SYSC_QUIRK("prcm", 0, 0, -1, -1, 0x40000100, 0xffffffff, 0),
Tony Lindgren23731ea2018-09-24 12:17:09 -07001180 SYSC_QUIRK("prcm", 0, 0, -1, -1, 0x00004102, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001181 SYSC_QUIRK("prcm", 0, 0, -1, -1, 0x40000400, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -07001182 SYSC_QUIRK("scm", 0, 0, 0x10, -1, 0x40000900, 0xffffffff, 0),
Tony Lindgren23731ea2018-09-24 12:17:09 -07001183 SYSC_QUIRK("scm", 0, 0, -1, -1, 0x4e8b0100, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001184 SYSC_QUIRK("scm", 0, 0, -1, -1, 0x4f000100, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -07001185 SYSC_QUIRK("scm", 0, 0, -1, -1, 0x40000900, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -07001186 SYSC_QUIRK("scrm", 0, 0, -1, -1, 0x00000010, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -07001187 SYSC_QUIRK("sdio", 0, 0, 0x10, -1, 0x40202301, 0xffff0ff0, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001188 SYSC_QUIRK("sdio", 0, 0x2fc, 0x110, 0x114, 0x31010000, 0xffffffff, 0),
Tony Lindgren40d9f912018-09-24 12:16:54 -07001189 SYSC_QUIRK("sdma", 0, 0, 0x2c, 0x28, 0x00010900, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001190 SYSC_QUIRK("slimbus", 0, 0, 0x10, -1, 0x40000902, 0xffffffff, 0),
1191 SYSC_QUIRK("slimbus", 0, 0, 0x10, -1, 0x40002903, 0xffffffff, 0),
1192 SYSC_QUIRK("spinlock", 0, 0, 0x10, -1, 0x50020000, 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001193 SYSC_QUIRK("rng", 0, 0x1fe0, 0x1fe4, -1, 0x00000020, 0xffffffff, 0),
Tony Lindgrenc6eb4af2018-09-27 13:34:27 -07001194 SYSC_QUIRK("rtc", 0, 0x74, 0x78, -1, 0x4eb01908, 0xffff00f0, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001195 SYSC_QUIRK("timer32k", 0, 0, 0x4, -1, 0x00000060, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001196 SYSC_QUIRK("usbhstll", 0, 0, 0x10, 0x14, 0x00000004, 0xffffffff, 0),
Tony Lindgrenf0106702018-11-15 14:46:53 -08001197 SYSC_QUIRK("usbhstll", 0, 0, 0x10, 0x14, 0x00000008, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001198 SYSC_QUIRK("usb_host_hs", 0, 0, 0x10, 0x14, 0x50700100, 0xffffffff, 0),
Tony Lindgrenf0106702018-11-15 14:46:53 -08001199 SYSC_QUIRK("usb_host_hs", 0, 0, 0x10, -1, 0x50700101, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001200 SYSC_QUIRK("usb_otg_hs", 0, 0x400, 0x404, 0x408, 0x00000050,
1201 0xffffffff, 0),
Tony Lindgren1ba30692018-09-24 12:17:05 -07001202 SYSC_QUIRK("wdt", 0, 0, 0x10, 0x14, 0x502a0500, 0xfffff0f0, 0),
1203 SYSC_QUIRK("vfpe", 0, 0, 0x104, -1, 0x4d001200, 0xffffffff, 0),
Tony Lindgrendc4c85e2018-04-16 10:26:25 -07001204#endif
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001205};
1206
Tony Lindgren42b9c5c2019-03-21 11:00:21 -07001207/*
1208 * Early quirks based on module base and register offsets only that are
1209 * needed before the module revision can be read
1210 */
1211static void sysc_init_early_quirks(struct sysc *ddata)
1212{
1213 const struct sysc_revision_quirk *q;
1214 int i;
1215
1216 for (i = 0; i < ARRAY_SIZE(sysc_revision_quirks); i++) {
1217 q = &sysc_revision_quirks[i];
1218
1219 if (!q->base)
1220 continue;
1221
1222 if (q->base != ddata->module_pa)
1223 continue;
1224
1225 if (q->rev_offset >= 0 &&
1226 q->rev_offset != ddata->offsets[SYSC_REVISION])
1227 continue;
1228
1229 if (q->sysc_offset >= 0 &&
1230 q->sysc_offset != ddata->offsets[SYSC_SYSCONFIG])
1231 continue;
1232
1233 if (q->syss_offset >= 0 &&
1234 q->syss_offset != ddata->offsets[SYSC_SYSSTATUS])
1235 continue;
1236
1237 ddata->name = q->name;
1238 ddata->cfg.quirks |= q->quirks;
1239 }
1240}
1241
1242/* Quirks that also consider the revision register value */
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001243static void sysc_init_revision_quirks(struct sysc *ddata)
1244{
1245 const struct sysc_revision_quirk *q;
1246 int i;
1247
1248 for (i = 0; i < ARRAY_SIZE(sysc_revision_quirks); i++) {
1249 q = &sysc_revision_quirks[i];
1250
1251 if (q->base && q->base != ddata->module_pa)
1252 continue;
1253
1254 if (q->rev_offset >= 0 &&
1255 q->rev_offset != ddata->offsets[SYSC_REVISION])
1256 continue;
1257
1258 if (q->sysc_offset >= 0 &&
1259 q->sysc_offset != ddata->offsets[SYSC_SYSCONFIG])
1260 continue;
1261
1262 if (q->syss_offset >= 0 &&
1263 q->syss_offset != ddata->offsets[SYSC_SYSSTATUS])
1264 continue;
1265
1266 if (q->revision == ddata->revision ||
1267 (q->revision & q->revision_mask) ==
1268 (ddata->revision & q->revision_mask)) {
1269 ddata->name = q->name;
1270 ddata->cfg.quirks |= q->quirks;
1271 }
1272 }
1273}
1274
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001275static int sysc_clockdomain_init(struct sysc *ddata)
1276{
1277 struct ti_sysc_platform_data *pdata = dev_get_platdata(ddata->dev);
1278 struct clk *fck = NULL, *ick = NULL;
1279 int error;
1280
1281 if (!pdata || !pdata->init_clockdomain)
1282 return 0;
1283
1284 switch (ddata->nr_clocks) {
1285 case 2:
1286 ick = ddata->clocks[SYSC_ICK];
1287 /* fallthrough */
1288 case 1:
1289 fck = ddata->clocks[SYSC_FCK];
1290 break;
1291 case 0:
1292 return 0;
1293 }
1294
1295 error = pdata->init_clockdomain(ddata->dev, fck, ick, &ddata->cookie);
1296 if (!error || error == -ENODEV)
1297 return 0;
1298
1299 return error;
1300}
1301
Tony Lindgrena3e92e72019-03-21 11:00:21 -07001302/*
1303 * Note that pdata->init_module() typically does a reset first. After
1304 * pdata->init_module() is done, PM runtime can be used for the interconnect
1305 * target module.
1306 */
1307static int sysc_legacy_init(struct sysc *ddata)
1308{
1309 struct ti_sysc_platform_data *pdata = dev_get_platdata(ddata->dev);
1310 int error;
1311
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001312 if (!pdata || !pdata->init_module)
Tony Lindgrena3e92e72019-03-21 11:00:21 -07001313 return 0;
1314
1315 error = pdata->init_module(ddata->dev, ddata->mdata, &ddata->cookie);
1316 if (error == -EEXIST)
1317 error = 0;
1318
1319 return error;
1320}
1321
Tony Lindgrenb11c1ea2019-03-21 11:00:21 -07001322/**
1323 * sysc_rstctrl_reset_deassert - deassert rstctrl reset
1324 * @ddata: device driver data
1325 * @reset: reset before deassert
1326 *
1327 * A module can have both OCP softreset control and external rstctrl.
1328 * If more complicated rstctrl resets are needed, please handle these
1329 * directly from the child device driver and map only the module reset
1330 * for the parent interconnect target module device.
1331 *
1332 * Automatic reset of the module on init can be skipped with the
1333 * "ti,no-reset-on-init" device tree property.
1334 */
1335static int sysc_rstctrl_reset_deassert(struct sysc *ddata, bool reset)
1336{
1337 int error;
1338
1339 if (!ddata->rsts)
1340 return 0;
1341
1342 if (reset) {
1343 error = reset_control_assert(ddata->rsts);
1344 if (error)
1345 return error;
1346 }
1347
1348 return reset_control_deassert(ddata->rsts);
1349}
1350
Faiz Abbas596e7952018-07-09 22:18:39 +05301351static int sysc_reset(struct sysc *ddata)
1352{
1353 int offset = ddata->offsets[SYSC_SYSCONFIG];
1354 int val;
1355
1356 if (ddata->legacy_mode || offset < 0 ||
1357 ddata->cfg.quirks & SYSC_QUIRK_NO_RESET_ON_INIT)
1358 return 0;
1359
1360 /*
1361 * Currently only support reset status in sysstatus.
1362 * Warn and return error in all other cases
1363 */
1364 if (!ddata->cfg.syss_mask) {
1365 dev_err(ddata->dev, "No ti,syss-mask. Reset failed\n");
1366 return -EINVAL;
1367 }
1368
1369 val = sysc_read(ddata, offset);
1370 val |= (0x1 << ddata->cap->regbits->srst_shift);
1371 sysc_write(ddata, offset, val);
1372
1373 /* Poll on reset status */
1374 offset = ddata->offsets[SYSC_SYSSTATUS];
1375
1376 return readl_poll_timeout(ddata->module_va + offset, val,
1377 (val & ddata->cfg.syss_mask) == 0x0,
1378 100, MAX_MODULE_SOFTRESET_WAIT);
1379}
1380
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001381/*
1382 * At this point the module is configured enough to read the revision but
1383 * module may not be completely configured yet to use PM runtime. Enable
1384 * all clocks directly during init to configure the quirks needed for PM
1385 * runtime based on the revision register.
1386 */
Tony Lindgren566a9b052017-12-15 09:41:19 -08001387static int sysc_init_module(struct sysc *ddata)
1388{
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001389 int error = 0;
1390 bool manage_clocks = true;
Tony Lindgrenb11c1ea2019-03-21 11:00:21 -07001391 bool reset = true;
1392
1393 if (ddata->cfg.quirks & SYSC_QUIRK_NO_RESET_ON_INIT)
1394 reset = false;
1395
1396 error = sysc_rstctrl_reset_deassert(ddata, reset);
1397 if (error)
1398 return error;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001399
Tony Lindgren386cb762019-03-22 07:49:30 -07001400 if (ddata->cfg.quirks &
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001401 (SYSC_QUIRK_NO_IDLE | SYSC_QUIRK_NO_IDLE_ON_INIT))
1402 manage_clocks = false;
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001403
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001404 error = sysc_clockdomain_init(ddata);
1405 if (error)
1406 return error;
1407
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001408 if (manage_clocks) {
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001409 sysc_clkdm_deny_idle(ddata);
1410
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001411 error = sysc_enable_opt_clocks(ddata);
1412 if (error)
1413 return error;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001414
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001415 error = sysc_enable_main_clocks(ddata);
1416 if (error)
1417 goto err_opt_clocks;
Faiz Abbas596e7952018-07-09 22:18:39 +05301418 }
1419
Tony Lindgren566a9b052017-12-15 09:41:19 -08001420 ddata->revision = sysc_read_revision(ddata);
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001421 sysc_init_revision_quirks(ddata);
1422
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001423 if (ddata->legacy_mode) {
1424 error = sysc_legacy_init(ddata);
1425 if (error)
1426 goto err_main_clocks;
1427 }
1428
1429 if (!ddata->legacy_mode && manage_clocks) {
1430 error = sysc_enable_module(ddata->dev);
1431 if (error)
1432 goto err_main_clocks;
1433 }
Tony Lindgrena3e92e72019-03-21 11:00:21 -07001434
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001435 error = sysc_reset(ddata);
1436 if (error)
1437 dev_err(ddata->dev, "Reset failed with %d\n", error);
1438
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001439 if (!ddata->legacy_mode && manage_clocks)
1440 sysc_disable_module(ddata->dev);
1441
Tony Lindgrena3e92e72019-03-21 11:00:21 -07001442err_main_clocks:
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001443 if (manage_clocks)
1444 sysc_disable_main_clocks(ddata);
1445err_opt_clocks:
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001446 if (manage_clocks) {
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001447 sysc_disable_opt_clocks(ddata);
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07001448 sysc_clkdm_allow_idle(ddata);
1449 }
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07001450
1451 return error;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001452}
1453
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08001454static int sysc_init_sysc_mask(struct sysc *ddata)
1455{
1456 struct device_node *np = ddata->dev->of_node;
1457 int error;
1458 u32 val;
1459
1460 error = of_property_read_u32(np, "ti,sysc-mask", &val);
1461 if (error)
1462 return 0;
1463
1464 if (val)
1465 ddata->cfg.sysc_val = val & ddata->cap->sysc_mask;
1466 else
1467 ddata->cfg.sysc_val = ddata->cap->sysc_mask;
1468
1469 return 0;
1470}
1471
1472static int sysc_init_idlemode(struct sysc *ddata, u8 *idlemodes,
1473 const char *name)
1474{
1475 struct device_node *np = ddata->dev->of_node;
1476 struct property *prop;
1477 const __be32 *p;
1478 u32 val;
1479
1480 of_property_for_each_u32(np, name, prop, p, val) {
1481 if (val >= SYSC_NR_IDLEMODES) {
1482 dev_err(ddata->dev, "invalid idlemode: %i\n", val);
1483 return -EINVAL;
1484 }
1485 *idlemodes |= (1 << val);
1486 }
1487
1488 return 0;
1489}
1490
1491static int sysc_init_idlemodes(struct sysc *ddata)
1492{
1493 int error;
1494
1495 error = sysc_init_idlemode(ddata, &ddata->cfg.midlemodes,
1496 "ti,sysc-midle");
1497 if (error)
1498 return error;
1499
1500 error = sysc_init_idlemode(ddata, &ddata->cfg.sidlemodes,
1501 "ti,sysc-sidle");
1502 if (error)
1503 return error;
1504
1505 return 0;
1506}
1507
1508/*
1509 * Only some devices on omap4 and later have SYSCONFIG reset done
1510 * bit. We can detect this if there is no SYSSTATUS at all, or the
1511 * SYSTATUS bit 0 is not used. Note that some SYSSTATUS registers
1512 * have multiple bits for the child devices like OHCI and EHCI.
1513 * Depends on SYSC being parsed first.
1514 */
1515static int sysc_init_syss_mask(struct sysc *ddata)
1516{
1517 struct device_node *np = ddata->dev->of_node;
1518 int error;
1519 u32 val;
1520
1521 error = of_property_read_u32(np, "ti,syss-mask", &val);
1522 if (error) {
1523 if ((ddata->cap->type == TI_SYSC_OMAP4 ||
1524 ddata->cap->type == TI_SYSC_OMAP4_TIMER) &&
1525 (ddata->cfg.sysc_val & SYSC_OMAP4_SOFTRESET))
1526 ddata->cfg.quirks |= SYSC_QUIRK_RESET_STATUS;
1527
1528 return 0;
1529 }
1530
1531 if (!(val & 1) && (ddata->cfg.sysc_val & SYSC_OMAP4_SOFTRESET))
1532 ddata->cfg.quirks |= SYSC_QUIRK_RESET_STATUS;
1533
1534 ddata->cfg.syss_mask = val;
1535
1536 return 0;
1537}
1538
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001539/*
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001540 * Many child device drivers need to have fck and opt clocks available
1541 * to get the clock rate for device internal configuration etc.
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001542 */
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001543static int sysc_child_add_named_clock(struct sysc *ddata,
1544 struct device *child,
1545 const char *name)
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001546{
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001547 struct clk *clk;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001548 struct clk_lookup *l;
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001549 int error = 0;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001550
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001551 if (!name)
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001552 return 0;
1553
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001554 clk = clk_get(child, name);
1555 if (!IS_ERR(clk)) {
1556 clk_put(clk);
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001557
1558 return -EEXIST;
1559 }
1560
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001561 clk = clk_get(ddata->dev, name);
1562 if (IS_ERR(clk))
1563 return -ENODEV;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001564
Tony Lindgren8b2830ba2018-04-16 10:26:09 -07001565 l = clkdev_create(clk, name, dev_name(child));
1566 if (!l)
1567 error = -ENOMEM;
1568
1569 clk_put(clk);
1570
1571 return error;
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001572}
1573
Tony Lindgren09dfe582018-04-16 10:25:52 -07001574static int sysc_child_add_clocks(struct sysc *ddata,
1575 struct device *child)
1576{
1577 int i, error;
1578
1579 for (i = 0; i < ddata->nr_clocks; i++) {
1580 error = sysc_child_add_named_clock(ddata,
1581 child,
1582 ddata->clock_roles[i]);
1583 if (error && error != -EEXIST) {
1584 dev_err(ddata->dev, "could not add child clock %s: %i\n",
1585 ddata->clock_roles[i], error);
1586
1587 return error;
1588 }
1589 }
1590
1591 return 0;
1592}
1593
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001594static struct device_type sysc_device_type = {
1595};
1596
1597static struct sysc *sysc_child_to_parent(struct device *dev)
1598{
1599 struct device *parent = dev->parent;
1600
1601 if (!parent || parent->type != &sysc_device_type)
1602 return NULL;
1603
1604 return dev_get_drvdata(parent);
1605}
1606
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001607static int __maybe_unused sysc_child_runtime_suspend(struct device *dev)
1608{
1609 struct sysc *ddata;
1610 int error;
1611
1612 ddata = sysc_child_to_parent(dev);
1613
1614 error = pm_generic_runtime_suspend(dev);
1615 if (error)
1616 return error;
1617
1618 if (!ddata->enabled)
1619 return 0;
1620
1621 return sysc_runtime_suspend(ddata->dev);
1622}
1623
1624static int __maybe_unused sysc_child_runtime_resume(struct device *dev)
1625{
1626 struct sysc *ddata;
1627 int error;
1628
1629 ddata = sysc_child_to_parent(dev);
1630
1631 if (!ddata->enabled) {
1632 error = sysc_runtime_resume(ddata->dev);
1633 if (error < 0)
1634 dev_err(ddata->dev,
1635 "%s error: %i\n", __func__, error);
1636 }
1637
1638 return pm_generic_runtime_resume(dev);
1639}
1640
1641#ifdef CONFIG_PM_SLEEP
1642static int sysc_child_suspend_noirq(struct device *dev)
1643{
1644 struct sysc *ddata;
1645 int error;
1646
1647 ddata = sysc_child_to_parent(dev);
1648
Tony Lindgrenef55f822018-04-16 10:27:15 -07001649 dev_dbg(ddata->dev, "%s %s\n", __func__,
1650 ddata->name ? ddata->name : "");
1651
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001652 error = pm_generic_suspend_noirq(dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001653 if (error) {
1654 dev_err(dev, "%s error at %i: %i\n",
1655 __func__, __LINE__, error);
1656
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001657 return error;
Tony Lindgrenef55f822018-04-16 10:27:15 -07001658 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001659
1660 if (!pm_runtime_status_suspended(dev)) {
1661 error = pm_generic_runtime_suspend(dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001662 if (error) {
Tony Lindgrenf9490782018-09-28 15:21:50 -07001663 dev_dbg(dev, "%s busy at %i: %i\n",
1664 __func__, __LINE__, error);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001665
Tony Lindgren4f3530f2018-08-08 01:07:06 -07001666 return 0;
Tony Lindgrenef55f822018-04-16 10:27:15 -07001667 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001668
1669 error = sysc_runtime_suspend(ddata->dev);
Tony Lindgrenef55f822018-04-16 10:27:15 -07001670 if (error) {
1671 dev_err(dev, "%s error at %i: %i\n",
1672 __func__, __LINE__, error);
1673
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001674 return error;
Tony Lindgrenef55f822018-04-16 10:27:15 -07001675 }
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001676
1677 ddata->child_needs_resume = true;
1678 }
1679
1680 return 0;
1681}
1682
1683static int sysc_child_resume_noirq(struct device *dev)
1684{
1685 struct sysc *ddata;
1686 int error;
1687
1688 ddata = sysc_child_to_parent(dev);
1689
Tony Lindgrenef55f822018-04-16 10:27:15 -07001690 dev_dbg(ddata->dev, "%s %s\n", __func__,
1691 ddata->name ? ddata->name : "");
1692
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001693 if (ddata->child_needs_resume) {
1694 ddata->child_needs_resume = false;
1695
1696 error = sysc_runtime_resume(ddata->dev);
1697 if (error)
1698 dev_err(ddata->dev,
1699 "%s runtime resume error: %i\n",
1700 __func__, error);
1701
1702 error = pm_generic_runtime_resume(dev);
1703 if (error)
1704 dev_err(ddata->dev,
1705 "%s generic runtime resume: %i\n",
1706 __func__, error);
1707 }
1708
1709 return pm_generic_resume_noirq(dev);
1710}
1711#endif
1712
Tony Lindgrenb7182b42019-03-21 11:00:21 -07001713static struct dev_pm_domain sysc_child_pm_domain = {
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001714 .ops = {
1715 SET_RUNTIME_PM_OPS(sysc_child_runtime_suspend,
1716 sysc_child_runtime_resume,
1717 NULL)
1718 USE_PLATFORM_PM_SLEEP_OPS
1719 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(sysc_child_suspend_noirq,
1720 sysc_child_resume_noirq)
1721 }
1722};
1723
1724/**
1725 * sysc_legacy_idle_quirk - handle children in omap_device compatible way
1726 * @ddata: device driver data
1727 * @child: child device driver
1728 *
1729 * Allow idle for child devices as done with _od_runtime_suspend().
1730 * Otherwise many child devices will not idle because of the permanent
1731 * parent usecount set in pm_runtime_irq_safe().
1732 *
1733 * Note that the long term solution is to just modify the child device
1734 * drivers to not set pm_runtime_irq_safe() and then this can be just
1735 * dropped.
1736 */
1737static void sysc_legacy_idle_quirk(struct sysc *ddata, struct device *child)
1738{
1739 if (!ddata->legacy_mode)
1740 return;
1741
1742 if (ddata->cfg.quirks & SYSC_QUIRK_LEGACY_IDLE)
1743 dev_pm_domain_set(child, &sysc_child_pm_domain);
1744}
1745
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001746static int sysc_notifier_call(struct notifier_block *nb,
1747 unsigned long event, void *device)
1748{
1749 struct device *dev = device;
1750 struct sysc *ddata;
1751 int error;
1752
1753 ddata = sysc_child_to_parent(dev);
1754 if (!ddata)
1755 return NOTIFY_DONE;
1756
1757 switch (event) {
1758 case BUS_NOTIFY_ADD_DEVICE:
Tony Lindgren09dfe582018-04-16 10:25:52 -07001759 error = sysc_child_add_clocks(ddata, dev);
1760 if (error)
1761 return error;
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001762 sysc_legacy_idle_quirk(ddata, dev);
Tony Lindgren2c355ff2018-02-22 13:58:03 -08001763 break;
1764 default:
1765 break;
1766 }
1767
1768 return NOTIFY_DONE;
1769}
1770
1771static struct notifier_block sysc_nb = {
1772 .notifier_call = sysc_notifier_call,
1773};
1774
Tony Lindgren566a9b052017-12-15 09:41:19 -08001775/* Device tree configured quirks */
1776struct sysc_dts_quirk {
1777 const char *name;
1778 u32 mask;
1779};
1780
1781static const struct sysc_dts_quirk sysc_dts_quirks[] = {
1782 { .name = "ti,no-idle-on-init",
1783 .mask = SYSC_QUIRK_NO_IDLE_ON_INIT, },
1784 { .name = "ti,no-reset-on-init",
1785 .mask = SYSC_QUIRK_NO_RESET_ON_INIT, },
Tony Lindgren386cb762019-03-22 07:49:30 -07001786 { .name = "ti,no-idle",
1787 .mask = SYSC_QUIRK_NO_IDLE, },
Tony Lindgren566a9b052017-12-15 09:41:19 -08001788};
1789
Tony Lindgren4014c082018-12-10 14:11:26 -08001790static void sysc_parse_dts_quirks(struct sysc *ddata, struct device_node *np,
1791 bool is_child)
Tony Lindgren566a9b052017-12-15 09:41:19 -08001792{
Tony Lindgren566a9b052017-12-15 09:41:19 -08001793 const struct property *prop;
Tony Lindgren4014c082018-12-10 14:11:26 -08001794 int i, len;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001795
1796 for (i = 0; i < ARRAY_SIZE(sysc_dts_quirks); i++) {
Tony Lindgren4014c082018-12-10 14:11:26 -08001797 const char *name = sysc_dts_quirks[i].name;
1798
1799 prop = of_get_property(np, name, &len);
Tony Lindgren566a9b052017-12-15 09:41:19 -08001800 if (!prop)
Tony Lindgrend39b6ea2018-02-15 09:18:55 -08001801 continue;
Tony Lindgren566a9b052017-12-15 09:41:19 -08001802
1803 ddata->cfg.quirks |= sysc_dts_quirks[i].mask;
Tony Lindgren4014c082018-12-10 14:11:26 -08001804 if (is_child) {
1805 dev_warn(ddata->dev,
1806 "dts flag should be at module level for %s\n",
1807 name);
1808 }
Tony Lindgren566a9b052017-12-15 09:41:19 -08001809 }
Tony Lindgren4014c082018-12-10 14:11:26 -08001810}
Tony Lindgren566a9b052017-12-15 09:41:19 -08001811
Tony Lindgren4014c082018-12-10 14:11:26 -08001812static int sysc_init_dts_quirks(struct sysc *ddata)
1813{
1814 struct device_node *np = ddata->dev->of_node;
1815 int error;
1816 u32 val;
1817
1818 ddata->legacy_mode = of_get_property(np, "ti,hwmods", NULL);
1819
1820 sysc_parse_dts_quirks(ddata, np, false);
Tony Lindgren566a9b052017-12-15 09:41:19 -08001821 error = of_property_read_u32(np, "ti,sysc-delay-us", &val);
1822 if (!error) {
1823 if (val > 255) {
1824 dev_warn(ddata->dev, "bad ti,sysc-delay-us: %i\n",
1825 val);
1826 }
1827
1828 ddata->cfg.srst_udelay = (u8)val;
1829 }
1830
1831 return 0;
1832}
1833
Tony Lindgren0eecc632017-10-10 14:23:43 -07001834static void sysc_unprepare(struct sysc *ddata)
1835{
1836 int i;
1837
Tony Lindgrenaaa29bb2019-03-21 11:00:21 -07001838 if (!ddata->clocks)
1839 return;
1840
Tony Lindgren0eecc632017-10-10 14:23:43 -07001841 for (i = 0; i < SYSC_MAX_CLOCKS; i++) {
1842 if (!IS_ERR_OR_NULL(ddata->clocks[i]))
1843 clk_unprepare(ddata->clocks[i]);
1844 }
1845}
1846
Tony Lindgren70a65242017-12-15 09:41:09 -08001847/*
1848 * Common sysc register bits found on omap2, also known as type1
1849 */
1850static const struct sysc_regbits sysc_regbits_omap2 = {
1851 .dmadisable_shift = -ENODEV,
1852 .midle_shift = 12,
1853 .sidle_shift = 3,
1854 .clkact_shift = 8,
1855 .emufree_shift = 5,
1856 .enwkup_shift = 2,
1857 .srst_shift = 1,
1858 .autoidle_shift = 0,
1859};
1860
1861static const struct sysc_capabilities sysc_omap2 = {
1862 .type = TI_SYSC_OMAP2,
1863 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY | SYSC_OMAP2_EMUFREE |
1864 SYSC_OMAP2_ENAWAKEUP | SYSC_OMAP2_SOFTRESET |
1865 SYSC_OMAP2_AUTOIDLE,
1866 .regbits = &sysc_regbits_omap2,
1867};
1868
1869/* All omap2 and 3 timers, and timers 1, 2 & 10 on omap 4 and 5 */
1870static const struct sysc_capabilities sysc_omap2_timer = {
1871 .type = TI_SYSC_OMAP2_TIMER,
1872 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY | SYSC_OMAP2_EMUFREE |
1873 SYSC_OMAP2_ENAWAKEUP | SYSC_OMAP2_SOFTRESET |
1874 SYSC_OMAP2_AUTOIDLE,
1875 .regbits = &sysc_regbits_omap2,
1876 .mod_quirks = SYSC_QUIRK_USE_CLOCKACT,
1877};
1878
1879/*
1880 * SHAM2 (SHA1/MD5) sysc found on omap3, a variant of sysc_regbits_omap2
1881 * with different sidle position
1882 */
1883static const struct sysc_regbits sysc_regbits_omap3_sham = {
1884 .dmadisable_shift = -ENODEV,
1885 .midle_shift = -ENODEV,
1886 .sidle_shift = 4,
1887 .clkact_shift = -ENODEV,
1888 .enwkup_shift = -ENODEV,
1889 .srst_shift = 1,
1890 .autoidle_shift = 0,
1891 .emufree_shift = -ENODEV,
1892};
1893
1894static const struct sysc_capabilities sysc_omap3_sham = {
1895 .type = TI_SYSC_OMAP3_SHAM,
1896 .sysc_mask = SYSC_OMAP2_SOFTRESET | SYSC_OMAP2_AUTOIDLE,
1897 .regbits = &sysc_regbits_omap3_sham,
1898};
1899
1900/*
1901 * AES register bits found on omap3 and later, a variant of
1902 * sysc_regbits_omap2 with different sidle position
1903 */
1904static const struct sysc_regbits sysc_regbits_omap3_aes = {
1905 .dmadisable_shift = -ENODEV,
1906 .midle_shift = -ENODEV,
1907 .sidle_shift = 6,
1908 .clkact_shift = -ENODEV,
1909 .enwkup_shift = -ENODEV,
1910 .srst_shift = 1,
1911 .autoidle_shift = 0,
1912 .emufree_shift = -ENODEV,
1913};
1914
1915static const struct sysc_capabilities sysc_omap3_aes = {
1916 .type = TI_SYSC_OMAP3_AES,
1917 .sysc_mask = SYSC_OMAP2_SOFTRESET | SYSC_OMAP2_AUTOIDLE,
1918 .regbits = &sysc_regbits_omap3_aes,
1919};
1920
1921/*
1922 * Common sysc register bits found on omap4, also known as type2
1923 */
1924static const struct sysc_regbits sysc_regbits_omap4 = {
1925 .dmadisable_shift = 16,
1926 .midle_shift = 4,
1927 .sidle_shift = 2,
1928 .clkact_shift = -ENODEV,
1929 .enwkup_shift = -ENODEV,
1930 .emufree_shift = 1,
1931 .srst_shift = 0,
1932 .autoidle_shift = -ENODEV,
1933};
1934
1935static const struct sysc_capabilities sysc_omap4 = {
1936 .type = TI_SYSC_OMAP4,
1937 .sysc_mask = SYSC_OMAP4_DMADISABLE | SYSC_OMAP4_FREEEMU |
1938 SYSC_OMAP4_SOFTRESET,
1939 .regbits = &sysc_regbits_omap4,
1940};
1941
1942static const struct sysc_capabilities sysc_omap4_timer = {
1943 .type = TI_SYSC_OMAP4_TIMER,
1944 .sysc_mask = SYSC_OMAP4_DMADISABLE | SYSC_OMAP4_FREEEMU |
1945 SYSC_OMAP4_SOFTRESET,
1946 .regbits = &sysc_regbits_omap4,
1947};
1948
1949/*
1950 * Common sysc register bits found on omap4, also known as type3
1951 */
1952static const struct sysc_regbits sysc_regbits_omap4_simple = {
1953 .dmadisable_shift = -ENODEV,
1954 .midle_shift = 2,
1955 .sidle_shift = 0,
1956 .clkact_shift = -ENODEV,
1957 .enwkup_shift = -ENODEV,
1958 .srst_shift = -ENODEV,
1959 .emufree_shift = -ENODEV,
1960 .autoidle_shift = -ENODEV,
1961};
1962
1963static const struct sysc_capabilities sysc_omap4_simple = {
1964 .type = TI_SYSC_OMAP4_SIMPLE,
1965 .regbits = &sysc_regbits_omap4_simple,
1966};
1967
1968/*
1969 * SmartReflex sysc found on omap34xx
1970 */
1971static const struct sysc_regbits sysc_regbits_omap34xx_sr = {
1972 .dmadisable_shift = -ENODEV,
1973 .midle_shift = -ENODEV,
1974 .sidle_shift = -ENODEV,
1975 .clkact_shift = 20,
1976 .enwkup_shift = -ENODEV,
1977 .srst_shift = -ENODEV,
1978 .emufree_shift = -ENODEV,
1979 .autoidle_shift = -ENODEV,
1980};
1981
1982static const struct sysc_capabilities sysc_34xx_sr = {
1983 .type = TI_SYSC_OMAP34XX_SR,
1984 .sysc_mask = SYSC_OMAP2_CLOCKACTIVITY,
1985 .regbits = &sysc_regbits_omap34xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08001986 .mod_quirks = SYSC_QUIRK_USE_CLOCKACT | SYSC_QUIRK_UNCACHED |
1987 SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08001988};
1989
1990/*
1991 * SmartReflex sysc found on omap36xx and later
1992 */
1993static const struct sysc_regbits sysc_regbits_omap36xx_sr = {
1994 .dmadisable_shift = -ENODEV,
1995 .midle_shift = -ENODEV,
1996 .sidle_shift = 24,
1997 .clkact_shift = -ENODEV,
1998 .enwkup_shift = 26,
1999 .srst_shift = -ENODEV,
2000 .emufree_shift = -ENODEV,
2001 .autoidle_shift = -ENODEV,
2002};
2003
2004static const struct sysc_capabilities sysc_36xx_sr = {
2005 .type = TI_SYSC_OMAP36XX_SR,
Tony Lindgren3267c082018-01-22 09:32:53 -08002006 .sysc_mask = SYSC_OMAP3_SR_ENAWAKEUP,
Tony Lindgren70a65242017-12-15 09:41:09 -08002007 .regbits = &sysc_regbits_omap36xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002008 .mod_quirks = SYSC_QUIRK_UNCACHED | SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08002009};
2010
2011static const struct sysc_capabilities sysc_omap4_sr = {
2012 .type = TI_SYSC_OMAP4_SR,
2013 .regbits = &sysc_regbits_omap36xx_sr,
Tony Lindgrena885f0f2018-02-22 14:03:48 -08002014 .mod_quirks = SYSC_QUIRK_LEGACY_IDLE,
Tony Lindgren70a65242017-12-15 09:41:09 -08002015};
2016
2017/*
2018 * McASP register bits found on omap4 and later
2019 */
2020static const struct sysc_regbits sysc_regbits_omap4_mcasp = {
2021 .dmadisable_shift = -ENODEV,
2022 .midle_shift = -ENODEV,
2023 .sidle_shift = 0,
2024 .clkact_shift = -ENODEV,
2025 .enwkup_shift = -ENODEV,
2026 .srst_shift = -ENODEV,
2027 .emufree_shift = -ENODEV,
2028 .autoidle_shift = -ENODEV,
2029};
2030
2031static const struct sysc_capabilities sysc_omap4_mcasp = {
2032 .type = TI_SYSC_OMAP4_MCASP,
2033 .regbits = &sysc_regbits_omap4_mcasp,
Tony Lindgren2c63a832018-11-15 14:46:53 -08002034 .mod_quirks = SYSC_QUIRK_OPT_CLKS_NEEDED,
2035};
2036
2037/*
2038 * McASP found on dra7 and later
2039 */
2040static const struct sysc_capabilities sysc_dra7_mcasp = {
2041 .type = TI_SYSC_OMAP4_SIMPLE,
2042 .regbits = &sysc_regbits_omap4_simple,
2043 .mod_quirks = SYSC_QUIRK_OPT_CLKS_NEEDED,
Tony Lindgren70a65242017-12-15 09:41:09 -08002044};
2045
2046/*
2047 * FS USB host found on omap4 and later
2048 */
2049static const struct sysc_regbits sysc_regbits_omap4_usb_host_fs = {
2050 .dmadisable_shift = -ENODEV,
2051 .midle_shift = -ENODEV,
2052 .sidle_shift = 24,
2053 .clkact_shift = -ENODEV,
2054 .enwkup_shift = 26,
2055 .srst_shift = -ENODEV,
2056 .emufree_shift = -ENODEV,
2057 .autoidle_shift = -ENODEV,
2058};
2059
2060static const struct sysc_capabilities sysc_omap4_usb_host_fs = {
2061 .type = TI_SYSC_OMAP4_USB_HOST_FS,
2062 .sysc_mask = SYSC_OMAP2_ENAWAKEUP,
2063 .regbits = &sysc_regbits_omap4_usb_host_fs,
2064};
2065
Faiz Abbas7f35e632018-07-09 22:18:38 +05302066static const struct sysc_regbits sysc_regbits_dra7_mcan = {
2067 .dmadisable_shift = -ENODEV,
2068 .midle_shift = -ENODEV,
2069 .sidle_shift = -ENODEV,
2070 .clkact_shift = -ENODEV,
2071 .enwkup_shift = 4,
2072 .srst_shift = 0,
2073 .emufree_shift = -ENODEV,
2074 .autoidle_shift = -ENODEV,
2075};
2076
2077static const struct sysc_capabilities sysc_dra7_mcan = {
2078 .type = TI_SYSC_DRA7_MCAN,
2079 .sysc_mask = SYSC_DRA7_MCAN_ENAWAKEUP | SYSC_OMAP4_SOFTRESET,
2080 .regbits = &sysc_regbits_dra7_mcan,
2081};
2082
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002083static int sysc_init_pdata(struct sysc *ddata)
2084{
2085 struct ti_sysc_platform_data *pdata = dev_get_platdata(ddata->dev);
Tony Lindgrena3e92e72019-03-21 11:00:21 -07002086 struct ti_sysc_module_data *mdata;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002087
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07002088 if (!pdata)
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002089 return 0;
2090
Tony Lindgrena3e92e72019-03-21 11:00:21 -07002091 mdata = devm_kzalloc(ddata->dev, sizeof(*mdata), GFP_KERNEL);
2092 if (!mdata)
2093 return -ENOMEM;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002094
Tony Lindgren2b2f7de2019-05-27 04:51:53 -07002095 if (ddata->legacy_mode) {
2096 mdata->name = ddata->legacy_mode;
2097 mdata->module_pa = ddata->module_pa;
2098 mdata->module_size = ddata->module_size;
2099 mdata->offsets = ddata->offsets;
2100 mdata->nr_offsets = SYSC_MAX_REGS;
2101 mdata->cap = ddata->cap;
2102 mdata->cfg = &ddata->cfg;
2103 }
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002104
Tony Lindgrena3e92e72019-03-21 11:00:21 -07002105 ddata->mdata = mdata;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002106
Tony Lindgrena3e92e72019-03-21 11:00:21 -07002107 return 0;
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002108}
2109
Tony Lindgren70a65242017-12-15 09:41:09 -08002110static int sysc_init_match(struct sysc *ddata)
2111{
2112 const struct sysc_capabilities *cap;
2113
2114 cap = of_device_get_match_data(ddata->dev);
2115 if (!cap)
2116 return -EINVAL;
2117
2118 ddata->cap = cap;
2119 if (ddata->cap)
2120 ddata->cfg.quirks |= ddata->cap->mod_quirks;
2121
2122 return 0;
2123}
2124
Tony Lindgren76f0f772018-02-23 08:28:45 -08002125static void ti_sysc_idle(struct work_struct *work)
2126{
2127 struct sysc *ddata;
2128
2129 ddata = container_of(work, struct sysc, idle_work.work);
2130
2131 if (pm_runtime_active(ddata->dev))
2132 pm_runtime_put_sync(ddata->dev);
2133}
2134
Tony Lindgrenc4bebea2018-04-16 10:24:54 -07002135static const struct of_device_id sysc_match_table[] = {
2136 { .compatible = "simple-bus", },
2137 { /* sentinel */ },
2138};
2139
Tony Lindgren0eecc632017-10-10 14:23:43 -07002140static int sysc_probe(struct platform_device *pdev)
2141{
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002142 struct ti_sysc_platform_data *pdata = dev_get_platdata(&pdev->dev);
Tony Lindgren0eecc632017-10-10 14:23:43 -07002143 struct sysc *ddata;
2144 int error;
2145
2146 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
2147 if (!ddata)
2148 return -ENOMEM;
2149
2150 ddata->dev = &pdev->dev;
Tony Lindgren566a9b052017-12-15 09:41:19 -08002151 platform_set_drvdata(pdev, ddata);
Tony Lindgren0eecc632017-10-10 14:23:43 -07002152
Tony Lindgren70a65242017-12-15 09:41:09 -08002153 error = sysc_init_match(ddata);
2154 if (error)
2155 return error;
2156
Tony Lindgren566a9b052017-12-15 09:41:19 -08002157 error = sysc_init_dts_quirks(ddata);
2158 if (error)
2159 goto unprepare;
2160
Tony Lindgren0eecc632017-10-10 14:23:43 -07002161 error = sysc_map_and_check_registers(ddata);
2162 if (error)
2163 goto unprepare;
2164
Tony Lindgrenc5a2de92017-12-15 09:41:23 -08002165 error = sysc_init_sysc_mask(ddata);
2166 if (error)
2167 goto unprepare;
2168
2169 error = sysc_init_idlemodes(ddata);
2170 if (error)
2171 goto unprepare;
2172
2173 error = sysc_init_syss_mask(ddata);
2174 if (error)
2175 goto unprepare;
2176
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002177 error = sysc_init_pdata(ddata);
2178 if (error)
2179 goto unprepare;
2180
Tony Lindgren42b9c5c2019-03-21 11:00:21 -07002181 sysc_init_early_quirks(ddata);
2182
2183 error = sysc_get_clocks(ddata);
2184 if (error)
2185 return error;
2186
Tony Lindgren50622362018-04-16 10:20:27 -07002187 error = sysc_init_resets(ddata);
2188 if (error)
2189 return error;
Tony Lindgren566a9b052017-12-15 09:41:19 -08002190
2191 error = sysc_init_module(ddata);
2192 if (error)
2193 goto unprepare;
2194
Tony Lindgren1a5cd7c2019-03-21 11:00:21 -07002195 pm_runtime_enable(ddata->dev);
Tony Lindgren0eecc632017-10-10 14:23:43 -07002196 error = pm_runtime_get_sync(ddata->dev);
2197 if (error < 0) {
2198 pm_runtime_put_noidle(ddata->dev);
2199 pm_runtime_disable(ddata->dev);
2200 goto unprepare;
2201 }
2202
Tony Lindgren0eecc632017-10-10 14:23:43 -07002203 sysc_show_registers(ddata);
2204
Tony Lindgren2c355ff2018-02-22 13:58:03 -08002205 ddata->dev->type = &sysc_device_type;
Tony Lindgrenc4bebea2018-04-16 10:24:54 -07002206 error = of_platform_populate(ddata->dev->of_node, sysc_match_table,
2207 pdata ? pdata->auxdata : NULL,
Tony Lindgrenef70b0b2018-02-22 14:00:25 -08002208 ddata->dev);
Tony Lindgren0eecc632017-10-10 14:23:43 -07002209 if (error)
2210 goto err;
2211
Tony Lindgren76f0f772018-02-23 08:28:45 -08002212 INIT_DELAYED_WORK(&ddata->idle_work, ti_sysc_idle);
2213
2214 /* At least earlycon won't survive without deferred idle */
2215 if (ddata->cfg.quirks & (SYSC_QUIRK_NO_IDLE_ON_INIT |
2216 SYSC_QUIRK_NO_RESET_ON_INIT)) {
2217 schedule_delayed_work(&ddata->idle_work, 3000);
2218 } else {
2219 pm_runtime_put(&pdev->dev);
2220 }
Tony Lindgren0eecc632017-10-10 14:23:43 -07002221
Tony Lindgren50622362018-04-16 10:20:27 -07002222 if (!of_get_available_child_count(ddata->dev->of_node))
2223 reset_control_assert(ddata->rsts);
2224
Tony Lindgren0eecc632017-10-10 14:23:43 -07002225 return 0;
2226
2227err:
Tony Lindgren0eecc632017-10-10 14:23:43 -07002228 pm_runtime_put_sync(&pdev->dev);
2229 pm_runtime_disable(&pdev->dev);
2230unprepare:
2231 sysc_unprepare(ddata);
2232
2233 return error;
2234}
2235
Tony Lindgren684be5a2017-10-13 10:48:40 -07002236static int sysc_remove(struct platform_device *pdev)
2237{
2238 struct sysc *ddata = platform_get_drvdata(pdev);
2239 int error;
2240
Tony Lindgren76f0f772018-02-23 08:28:45 -08002241 cancel_delayed_work_sync(&ddata->idle_work);
2242
Tony Lindgren684be5a2017-10-13 10:48:40 -07002243 error = pm_runtime_get_sync(ddata->dev);
2244 if (error < 0) {
2245 pm_runtime_put_noidle(ddata->dev);
2246 pm_runtime_disable(ddata->dev);
2247 goto unprepare;
2248 }
2249
2250 of_platform_depopulate(&pdev->dev);
2251
Tony Lindgren684be5a2017-10-13 10:48:40 -07002252 pm_runtime_put_sync(&pdev->dev);
2253 pm_runtime_disable(&pdev->dev);
Tony Lindgren50622362018-04-16 10:20:27 -07002254 reset_control_assert(ddata->rsts);
Tony Lindgren684be5a2017-10-13 10:48:40 -07002255
2256unprepare:
2257 sysc_unprepare(ddata);
2258
2259 return 0;
2260}
2261
Tony Lindgren0eecc632017-10-10 14:23:43 -07002262static const struct of_device_id sysc_match[] = {
Tony Lindgren70a65242017-12-15 09:41:09 -08002263 { .compatible = "ti,sysc-omap2", .data = &sysc_omap2, },
2264 { .compatible = "ti,sysc-omap2-timer", .data = &sysc_omap2_timer, },
2265 { .compatible = "ti,sysc-omap4", .data = &sysc_omap4, },
2266 { .compatible = "ti,sysc-omap4-timer", .data = &sysc_omap4_timer, },
2267 { .compatible = "ti,sysc-omap4-simple", .data = &sysc_omap4_simple, },
2268 { .compatible = "ti,sysc-omap3430-sr", .data = &sysc_34xx_sr, },
2269 { .compatible = "ti,sysc-omap3630-sr", .data = &sysc_36xx_sr, },
2270 { .compatible = "ti,sysc-omap4-sr", .data = &sysc_omap4_sr, },
2271 { .compatible = "ti,sysc-omap3-sham", .data = &sysc_omap3_sham, },
2272 { .compatible = "ti,sysc-omap-aes", .data = &sysc_omap3_aes, },
2273 { .compatible = "ti,sysc-mcasp", .data = &sysc_omap4_mcasp, },
Tony Lindgren2c63a832018-11-15 14:46:53 -08002274 { .compatible = "ti,sysc-dra7-mcasp", .data = &sysc_dra7_mcasp, },
Tony Lindgren70a65242017-12-15 09:41:09 -08002275 { .compatible = "ti,sysc-usb-host-fs",
2276 .data = &sysc_omap4_usb_host_fs, },
Faiz Abbas7f35e632018-07-09 22:18:38 +05302277 { .compatible = "ti,sysc-dra7-mcan", .data = &sysc_dra7_mcan, },
Tony Lindgren0eecc632017-10-10 14:23:43 -07002278 { },
2279};
2280MODULE_DEVICE_TABLE(of, sysc_match);
2281
2282static struct platform_driver sysc_driver = {
2283 .probe = sysc_probe,
Tony Lindgren684be5a2017-10-13 10:48:40 -07002284 .remove = sysc_remove,
Tony Lindgren0eecc632017-10-10 14:23:43 -07002285 .driver = {
2286 .name = "ti-sysc",
2287 .of_match_table = sysc_match,
2288 .pm = &sysc_pm_ops,
2289 },
2290};
Tony Lindgren2c355ff2018-02-22 13:58:03 -08002291
2292static int __init sysc_init(void)
2293{
2294 bus_register_notifier(&platform_bus_type, &sysc_nb);
2295
2296 return platform_driver_register(&sysc_driver);
2297}
2298module_init(sysc_init);
2299
2300static void __exit sysc_exit(void)
2301{
2302 bus_unregister_notifier(&platform_bus_type, &sysc_nb);
2303 platform_driver_unregister(&sysc_driver);
2304}
2305module_exit(sysc_exit);
Tony Lindgren0eecc632017-10-10 14:23:43 -07002306
2307MODULE_DESCRIPTION("TI sysc interconnect target driver");
2308MODULE_LICENSE("GPL v2");