1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
|
/*
* Exynos Generic power domain support.
*
* Copyright (c) 2013 Samsung Electronics Co., Ltd.
* http://www.samsung.com
*
* Implementation of Exynos specific power domain control which is used in
* conjunction with runtime-pm. Support for both device-tree and non-device-tree
* based power domain support is included.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <mach/pm_domains.h>
/* Sub-domain does not have power on/off features.
* dummy power on/off function is required.
*/
static LIST_HEAD(epd_list);
static DEFINE_MUTEX(epd_list_lock);
static inline int exynos_pd_power_dummy(struct exynos_pm_domain *pd,
int power_flags)
{
DEBUG_PRINT_INFO("%s: dummy power %s\n", pd->genpd.name, power_flags ? "on":"off");
pd->status = power_flags;
return 0;
}
struct exynos_pm_domain *exynos_pd_lookup_name(const char *domain_name)
{
struct exynos_pm_domain *exypd = NULL, *epd;
if (IS_ERR_OR_NULL(domain_name))
return NULL;
mutex_lock(&epd_list_lock);
list_for_each_entry(epd, &epd_list, epd_list_node) {
if (!strcmp(epd->name, domain_name)) {
exypd = epd;
break;
}
}
mutex_unlock(&epd_list_lock);
return exypd;
}
static int exynos_pd_status(struct exynos_pm_domain *pd)
{
if (unlikely(!pd))
return -EINVAL;
mutex_lock(&pd->access_lock);
if (likely(pd->base)) {
/* check STATUS register */
pd->status = (__raw_readl(pd->base+0x4) & LOCAL_PWR_CFG);
}
mutex_unlock(&pd->access_lock);
return pd->status;
}
static int exynos_pd_power(struct exynos_pm_domain *pd, int power_flags)
{
unsigned long timeout;
if (unlikely(!pd))
return -EINVAL;
mutex_lock(&pd->access_lock);
#if defined(CONFIG_SOC_EXYNOS7420)
timeout = pd->pd_timeout * 100;
#endif
if (likely(pd->base)) {
/* sc_feedback to OPTION register */
__raw_writel(pd->pd_option, pd->base+0x8);
#if defined(CONFIG_SOC_EXYNOS7420)
if (!strcmp(pd->name, "pd-g3d"))
__raw_writel(0xFFFF00C0, pd->base+0x10);
#endif
/* on/off value to CONFIGURATION register */
__raw_writel(power_flags, pd->base);
#if defined(CONFIG_SOC_EXYNOS5433) || defined(CONFIG_SOC_EXYNOS7580)
/* Wait max 1ms */
timeout = 100;
#endif
/* check STATUS register */
while ((__raw_readl(pd->base+0x4) & LOCAL_PWR_CFG) != power_flags) {
if (timeout == 0) {
pr_err("%s@%p: %08x, %08x, %08x\n",
pd->genpd.name,
pd->base,
__raw_readl(pd->base),
__raw_readl(pd->base+4),
__raw_readl(pd->base+8));
pr_err(PM_DOMAIN_PREFIX "%s can't control power, timeout\n", pd->name);
mutex_unlock(&pd->access_lock);
#if defined(CONFIG_SOC_EXYNOS5422)
pr_info("\e[1;33m ######### cmu register dump on timeout ######### \e[0m\n");
pr_info("$$$$$$$$$ %s : %08X\n","EXYNOS5_EPLL_CON0", readl(EXYNOS5_EPLL_CON0));
pr_info("$$$$$$$$$ %s : %08X\n","EXYNOS5_DPLL_CON0", readl(EXYNOS5_DPLL_CON0));
pr_info("$$$$$$$$$ %s : %08X\n","EXYNOS5_MPLL_CON0", readl(EXYNOS5_MPLL_CON0));
pr_info("$$$$$$$$$ %s : %08X\n","EXYNOS5_SPLL_CON0", readl(EXYNOS5_SPLL_CON0));
pr_info("$$$$$$$$$ %s : %08X\n","EXYNOS5_CLK_SRC_TOP6", readl(EXYNOS5_CLK_SRC_TOP6));
pr_info("$$$$$$$$$ %s : %08X\n","EXYNOS5_CLK_MUX_STAT_TOP6", readl(EXYNOS5_CLK_MUX_STAT_TOP6));
pr_info("$$$$$$$$$ %s : %08X\n","EXYNOS5_CLK_SRC_TOP7", readl(EXYNOS5_CLK_SRC_TOP7));
pr_info("$$$$$$$$$ %s : %08X\n","EXYNOS5_CLK_SRC_MASK_TOP7", readl(EXYNOS5_CLK_SRC_MASK_TOP7));
pr_info("$$$$$$$$$ %s : %08X\n","EXYNOS5_CLK_MUX_STAT_TOP7", readl(EXYNOS5_CLK_MUX_STAT_TOP7));
pr_info("$$$$$$$$$ %s : %08X\n","EXYNOS5_CLK_SRC_TOP9", readl(EXYNOS5_CLK_SRC_TOP9));
pr_info("$$$$$$$$$ %s : %08X\n","EXYNOS5_CLK_MUX_STAT_TOP9", readl(EXYNOS5_CLK_MUX_STAT_TOP9));
pr_info("$$$$$$$$$ %s : %08X\n","EXYNOS5_CLK_GATE_BUS_TOP", readl(EXYNOS5_CLK_GATE_BUS_TOP));
#endif
return -EBUSY;
}
--timeout;
cpu_relax();
usleep_range(8, 10);
}
if (unlikely(timeout < 50)) {
pr_warn(PM_DOMAIN_PREFIX "long delay found during %s is %s\n", pd->name, power_flags ? "on":"off");
pr_warn("%s@%p: %08x, %08x, %08x\n",
pd->name,
pd->base,
__raw_readl(pd->base),
__raw_readl(pd->base+4),
__raw_readl(pd->base+8));
}
}
pd->status = power_flags;
mutex_unlock(&pd->access_lock);
DEBUG_PRINT_INFO("%s@%p: %08x, %08x, %08x\n",
pd->genpd.name, pd->base,
__raw_readl(pd->base),
__raw_readl(pd->base+4),
__raw_readl(pd->base+8));
return 0;
}
/* exynos_genpd_power_on - power-on callback function for genpd.
* @genpd: generic power domain.
*
* main function of power on sequence.
* 1. clock on
* 2. reconfiguration of clock
* 3. pd power on
* 4. set bts configuration
* 5. restore clock sources.
*/
static int exynos_genpd_power_on(struct generic_pm_domain *genpd)
{
struct exynos_pm_domain *pd = container_of(genpd, struct exynos_pm_domain, genpd);
int ret;
if (unlikely(!pd->on)) {
pr_err(PM_DOMAIN_PREFIX "%s cannot support power on\n", pd->name);
return -EINVAL;
}
/* clock enable before pd on */
if (pd->cb && pd->cb->on_pre)
pd->cb->on_pre(pd);
/* power domain on */
ret = pd->on(pd, LOCAL_PWR_CFG);
if (unlikely(ret)) {
pr_err(PM_DOMAIN_PREFIX "%s makes error at power on!\n", genpd->name);
return ret;
}
if (pd->cb && pd->cb->on_post)
pd->cb->on_post(pd);
#if defined(CONFIG_EXYNOS5430_BTS) || defined(CONFIG_EXYNOS5422_BTS) \
|| defined(CONFIG_EXYNOS5433_BTS) || defined(CONFIG_EXYNOS7420_BTS)
/* enable bts features if exists */
if (pd->bts)
bts_initialize(pd->name, true);
#endif
return 0;
}
/* exynos_genpd_power_off - power-off callback function for genpd.
* @genpd: generic power domain.
*
* main function of power off sequence.
* 1. clock on
* 2. reset IPs when necessary
* 3. pd power off
* 4. change clock sources to OSC.
*/
static int exynos_genpd_power_off(struct generic_pm_domain *genpd)
{
struct exynos_pm_domain *pd = container_of(genpd, struct exynos_pm_domain, genpd);
int ret;
if (unlikely(!pd->off)) {
pr_err(PM_DOMAIN_PREFIX "%s can't support power off!\n", genpd->name);
return -EINVAL;
}
#if defined(CONFIG_EXYNOS5430_BTS) || defined(CONFIG_EXYNOS5422_BTS) \
|| defined(CONFIG_EXYNOS5433_BTS) || defined(CONFIG_EXYNOS7420_BTS)
/* disable bts features if exists */
if (pd->bts)
bts_initialize(pd->name, false);
#endif
if (pd->cb && pd->cb->off_pre)
pd->cb->off_pre(pd);
ret = pd->off(pd, 0);
if (unlikely(ret)) {
if (ret == -EBUSY) {
pr_err(PM_DOMAIN_PREFIX "Timed out during %s power off! -> forced power off\n", genpd->name);
if (pd->cb && pd->cb->forced_off_pre)
pd->cb->forced_off_pre(pd);
ret = pd->off(pd, 0);
if (unlikely(ret)) {
pr_err(PM_DOMAIN_PREFIX "%s occur error at power off!\n", genpd->name);
return ret;
}
} else {
pr_err(PM_DOMAIN_PREFIX "%s occur error at power off!\n", genpd->name);
return ret;
}
}
if (pd->cb && pd->cb->off_post)
pd->cb->off_post(pd);
return 0;
}
#ifdef CONFIG_OF
#if defined(CONFIG_EXYNOS5430_BTS) || defined(CONFIG_EXYNOS5422_BTS) \
|| defined(CONFIG_EXYNOS5433_BTS) || defined(CONFIG_EXYNOS7420_BTS)
/**
* of_device_bts_is_available - check if bts feature is enabled or not
*
* @device: Node to check for availability, with locks already held
*
* Returns 1 if the status property is "enabled" or "ok",
* 0 otherwise
*/
static int of_device_bts_is_available(const struct device_node *device)
{
const char *status;
int statlen;
status = of_get_property(device, "bts-status", &statlen);
if (status == NULL)
return 0;
if (statlen > 0) {
if (!strcmp(status, "enabled") || !strcmp(status, "ok"))
return 1;
}
return 0;
}
#endif
static void exynos_pm_powerdomain_init(struct exynos_pm_domain *pd)
{
pd->genpd.power_off = exynos_genpd_power_off;
pd->genpd.power_on = exynos_genpd_power_on;
/* pd power on/off latency is less than 1ms */
pd->genpd.power_on_latency_ns = 1000000;
pd->genpd.power_off_latency_ns = 1000000;
pd->status = true;
pd->check_status = exynos_pd_status;
#if defined(CONFIG_EXYNOS5430_BTS) || defined(CONFIG_EXYNOS5422_BTS) \
|| defined(CONFIG_EXYNOS5433_BTS) || defined(CONFIG_EXYNOS7420_BTS)
do {
int ret;
/* bts feature is enabled if exists */
ret = of_device_bts_is_available(pd->genpd.of_node);
if (ret) {
pd->bts = 1;
bts_initialize(pd->name, true);
DEBUG_PRINT_INFO("%s - bts feature is enabled\n", pd->name);
}
} while(0);
#endif
mutex_init(&pd->access_lock);
pm_genpd_init(&pd->genpd, NULL, !pd->status);
}
/* show_power_domain - show current power domain status.
*
* read the status of power domain and show it.
*/
static void show_power_domain(void)
{
struct device_node *np;
for_each_compatible_node(np, NULL, "samsung,exynos-pd") {
struct platform_device *pdev;
struct exynos_pm_domain *pd;
if (of_device_is_available(np)) {
pdev = of_find_device_by_node(np);
if (!pdev)
continue;
pd = platform_get_drvdata(pdev);
pr_info(" %-9s - %-3s\n", pd->genpd.name,
pd->check_status(pd) ? "on" : "off");
} else
pr_info(" %-9s - %s\n", np->name, "on, always");
}
return;
}
/* when latency is over pre-defined value warning message will be shown.
* default values are:
* start/stop latency - 50us
* state save/restore latency - 500us
*/
static struct gpd_timing_data gpd_td = {
.stop_latency_ns = 50000,
.start_latency_ns = 50000,
.save_state_latency_ns = 500000,
.restore_state_latency_ns = 500000,
};
static void exynos_add_device_to_pd(struct exynos_pm_domain *pd,
struct device *dev)
{
int ret;
if (unlikely(!pd)) {
pr_err(PM_DOMAIN_PREFIX "can't add device, domain is empty\n");
return;
}
if (unlikely(!dev)) {
pr_err(PM_DOMAIN_PREFIX "can't add device, device is empty\n");
return;
}
DEBUG_PRINT_INFO("adding %s to power domain %s\n", dev_name(dev), pd->genpd.name);
while (1) {
ret = __pm_genpd_add_device(&pd->genpd, dev, &gpd_td);
if (ret != -EAGAIN)
break;
cond_resched();
}
if (!ret) {
pm_genpd_dev_need_restore(dev, true);
pr_info(PM_DOMAIN_PREFIX "%s, Device : %s Registered\n", pd->genpd.name, dev_name(dev));
} else
pr_err(PM_DOMAIN_PREFIX "%s can't add device %s\n", pd->genpd.name, dev_name(dev));
}
static void exynos_remove_device_from_pd(struct device *dev)
{
struct generic_pm_domain *genpd = dev_to_genpd(dev);
int ret;
DEBUG_PRINT_INFO("removing %s from power domain %s\n", dev_name(dev), genpd->name);
while (1) {
ret = pm_genpd_remove_device(genpd, dev);
if (ret != -EAGAIN)
break;
cond_resched();
}
}
static void exynos_read_domain_from_dt(struct device *dev)
{
struct platform_device *pd_pdev;
struct exynos_pm_domain *pd;
struct device_node *node;
/* add platform device to power domain
* 1. get power domain node for given platform device
* 2. get power domain device from power domain node
* 3. get power domain structure from power domain device
* 4. add given platform device to power domain structure.
*/
node = of_parse_phandle(dev->of_node, "samsung,power-domain", 0);
if (!node)
return;
pd_pdev = of_find_device_by_node(node);
if (!pd_pdev)
return;
pd = platform_get_drvdata(pd_pdev);
exynos_add_device_to_pd(pd, dev);
}
static int exynos_pm_notifier_call(struct notifier_block *nb,
unsigned long event, void *data)
{
struct device *dev = data;
switch (event) {
case BUS_NOTIFY_BIND_DRIVER:
if (dev->of_node)
exynos_read_domain_from_dt(dev);
break;
case BUS_NOTIFY_UNBOUND_DRIVER:
exynos_remove_device_from_pd(dev);
break;
}
return NOTIFY_DONE;
}
static struct notifier_block platform_nb = {
.notifier_call = exynos_pm_notifier_call,
};
static __init int exynos_pm_dt_parse_domains(void)
{
struct platform_device *pdev = NULL;
struct device_node *np = NULL;
for_each_compatible_node(np, NULL, "samsung,exynos-pd") {
struct exynos_pm_domain *pd;
struct device_node *children;
int ret, val;
/* skip unmanaged power domain */
if (!of_device_is_available(np))
continue;
pdev = of_find_device_by_node(np);
pd = kzalloc(sizeof(*pd), GFP_KERNEL);
if (!pd) {
pr_err(PM_DOMAIN_PREFIX "%s: failed to allocate memory for domain\n",
__func__);
return -ENOMEM;
}
pd->genpd.name = kstrdup(np->name, GFP_KERNEL);
pd->name = pd->genpd.name;
pd->genpd.of_node = np;
pd->base = of_iomap(np, 0);
pd->on = exynos_pd_power;
pd->off = exynos_pd_power;
pd->cb = exynos_pd_find_callback(pd);
#if defined(CONFIG_SOC_EXYNOS7420) || defined(CONFIG_SOC_EXYNOS7580)
pd->pd_data = (void *)exynos_pd_find_data(pd);
#endif
ret = exynos_pd_clk_get(pd);
if (ret) {
pr_err(PM_DOMAIN_PREFIX "%s: failed to get pd clk\n", __func__);
return ret;
}
ret = of_property_read_u32_index(np, "pd-option", 0, &val);
if (ret)
pd->pd_option = 0x0102;
else
pd->pd_option = val;
#if defined(CONFIG_SOC_EXYNOS7420)
ret = of_property_read_u32_index(np, "pd-timeout", 0, &val);
if (ret)
pd->pd_timeout = 1;
else
pd->pd_timeout = val;
#endif
if (pd->cb) {
if (pd->cb->on)
pd->on = pd->cb->on;
if (pd->cb->off)
pd->off = pd->cb->off;
}
platform_set_drvdata(pdev, pd);
if (pd->cb && pd->cb->init) {
ret = pd->cb->init(pd);
if (ret) {
iounmap(pd->base);
kfree(pd->genpd.name);
kfree(pd);
pr_err(PM_DOMAIN_PREFIX
"%s: Failed to init domain %s\n",
__func__, np->name);
return ret;
}
}
exynos_pm_powerdomain_init(pd);
mutex_lock(&epd_list_lock);
list_add(&pd->epd_list_node, &epd_list);
mutex_unlock(&epd_list_lock);
/* add LOGICAL sub-domain
* It is not assumed that there is REAL sub-domain.
* Power on/off functions are not defined here.
*/
for_each_child_of_node(np, children) {
struct exynos_pm_domain *sub_pd;
struct platform_device *sub_pdev;
sub_pd = kzalloc(sizeof(*sub_pd), GFP_KERNEL);
if (!sub_pd) {
pr_err("%s: failed to allocate memory for power domain\n",
__func__);
kfree(pd->genpd.name);
kfree(pd);
return -ENOMEM;
}
sub_pd->genpd.name = kstrdup(children->name, GFP_KERNEL);
sub_pd->name = sub_pd->genpd.name;
sub_pd->genpd.of_node = children;
sub_pd->on = exynos_pd_power_dummy;
sub_pd->off = exynos_pd_power_dummy;
sub_pd->cb = NULL;
/* kernel does not create sub-domain pdev. */
sub_pdev = of_find_device_by_node(children);
if (!sub_pdev)
sub_pdev = of_platform_device_create(children, NULL, &pdev->dev);
if (!sub_pdev) {
pr_err(PM_DOMAIN_PREFIX "sub domain allocation failed: %s\n",
kstrdup(children->name, GFP_KERNEL));
continue;
}
platform_set_drvdata(sub_pdev, sub_pd);
exynos_pm_powerdomain_init(sub_pd);
if (pm_genpd_add_subdomain(&pd->genpd, &sub_pd->genpd))
pr_err("PM DOMAIN: %s can't add subdomain %s\n", pd->genpd.name, sub_pd->genpd.name);
}
}
/* EXCEPTION: add physical sub-pd to master pd using device tree */
for_each_compatible_node(np, NULL, "samsung,exynos-pd") {
struct exynos_pm_domain *parent_pd, *child_pd;
struct device_node *parent;
struct platform_device *parent_pd_pdev, *child_pd_pdev;
int i;
/* skip unmanaged power domain */
if (!of_device_is_available(np))
continue;
/* child_pd_pdev should have value. */
child_pd_pdev = of_find_device_by_node(np);
child_pd = platform_get_drvdata(child_pd_pdev);
/* search parents in device tree */
for (i = 0; i < MAX_PARENT_POWER_DOMAIN; i++) {
/* find parent node if available */
parent = of_parse_phandle(np, "parent", i);
if (!parent)
break;
/* display error when parent is unmanaged. */
if (!of_device_is_available(parent)) {
pr_err(PM_DOMAIN_PREFIX "%s is not managed by runtime pm.\n",
kstrdup(parent->name, GFP_KERNEL));
continue;
}
/* parent_pd_pdev should have value. */
parent_pd_pdev = of_find_device_by_node(parent);
parent_pd = platform_get_drvdata(parent_pd_pdev);
if (pm_genpd_add_subdomain(&parent_pd->genpd, &child_pd->genpd))
pr_err(PM_DOMAIN_PREFIX "%s cannot add subdomain %s\n",
parent_pd->name, child_pd->name);
else
pr_info(PM_DOMAIN_PREFIX "%s has a new child %s.\n",
parent_pd->name, child_pd->name);
}
}
bus_register_notifier(&platform_bus_type, &platform_nb);
pr_info("EXYNOS5: PM Domain Initialize\n");
/* show information of power domain registration */
show_power_domain();
return 0;
}
#endif
static int __init exynos5_pm_domain_init(void)
{
#ifdef CONFIG_OF
if (of_have_populated_dt())
return exynos_pm_dt_parse_domains();
#endif
pr_err(PM_DOMAIN_PREFIX "PM Domain works along with Device Tree\n");
return -EPERM;
}
subsys_initcall(exynos5_pm_domain_init);
static __init int exynos_pm_domain_idle(void)
{
pr_info(PM_DOMAIN_PREFIX "Power off unused power domains.\n");
pm_genpd_poweroff_unused();
return 0;
}
late_initcall_sync(exynos_pm_domain_idle);
|