summaryrefstree
path: root/kernel/drivers/devfreq/exynos/exynos-ppc.c
blob: e85f2649f9190532e54f2ad28e127aa8aaabc860 (plain)
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
/*
 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
 *		http://www.samsung.com/
 *
 * EXYNOS - PPMU support
 *
 * 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 <linux/kernel.h>
#include <linux/errno.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/types.h>
#include <linux/io.h>
#include <linux/sched/clock.h>
#include <linux/math64.h>

#include "exynos-ppc.h"

#define PMNC			0x0004
#define CNTENS			0x0008
#define CNTENC			0x000C
#define CNTRESET		0x002C
#define CNTAUTO			0x0030
#define PMCNT			0x0034
#define CCNT			0x0048

#define BIT_CHVALUE		((0x1 << 31) | (0x1 << 0))
#define BIT_REGVALUE		(0x1 << 24)
#define BIT_RESETALL		(0x6)
#define BIT_GLBCNTEN		(0x1)

static void exynos_init_ppc(void __iomem *base)
{
	/* Initialize Q-CH, OP Mode */
	__raw_writel(BIT_REGVALUE | BIT_RESETALL, base + PMNC);

	/* Enable CCNT, PMCNTTx Counting*/
	__raw_writel(BIT_CHVALUE, base + CNTENS);
}

static void exynos_start_ppc(void __iomem *base)
{
	__raw_writel(BIT_REGVALUE | BIT_GLBCNTEN, base + PMNC);
}

static void exynos_stop_ppc(void __iomem *base)
{
	__raw_writel(BIT_REGVALUE, base + PMNC);
}

static void exynos_clear_ppc(void __iomem *base)
{
	__raw_writel(BIT_RESETALL, base + PMNC);
}

static void exynos_reset_ppc(void __iomem *base)
{
	__raw_writel(BIT_REGVALUE | BIT_RESETALL, base + PMNC);
}

static void exynos_read_ppc(struct exynos_devfreq_data *data)
{
	int i;
	struct ppc_data ppc = { 0, };
	int index = 0;

	data->um_data.val_ccnt = 0;
	data->um_data.val_pmcnt = 0;

	for (i = 0; i < data->um_data.um_count; i++) {
		ppc.ccnt = __raw_readl(data->um_data.va_base[i] + CCNT);
		ppc.pmcnt = __raw_readl(data->um_data.va_base[i] + PMCNT);

		if (data->um_data.val_pmcnt < ppc.pmcnt) {
			data->um_data.val_ccnt = ppc.ccnt;
			data->um_data.val_pmcnt = ppc.pmcnt;
			index = i;
		}
	}
}

int exynos_devfreq_um_init(struct exynos_devfreq_data *data)
{
	int i = 0;

	for (i = 0; i < data->um_data.um_count; i++)
		exynos_init_ppc(data->um_data.va_base[i]);

	for (i = 0; i < data->um_data.um_count; i++)
		exynos_start_ppc(data->um_data.va_base[i]);

	return 0;
}

void exynos_devfreq_um_exit(struct exynos_devfreq_data *data)
{
	int i = 0;

	for (i = 0; i < data->um_data.um_count; i++)
		exynos_clear_ppc(data->um_data.va_base[i]);
}

static int exynos_devfreq_get_dev_status(struct device *dev, struct devfreq_dev_status *stat)
{
	struct platform_device *pdev = container_of(dev, struct platform_device, dev);
	struct exynos_devfreq_data *data = platform_get_drvdata(pdev);
	u64 cur_time;
	int i;

	if (data->devfreq_disabled)
		return -EAGAIN;

	cur_time = sched_clock();
	data->last_monitor_period = (cur_time - data->last_monitor_time);
	data->last_monitor_time = cur_time;

	for (i = 0; i < data->um_data.um_count; i++)
		exynos_stop_ppc(data->um_data.va_base[i]);

	/* Read current counter */
	exynos_read_ppc(data);

	stat->current_frequency = data->devfreq->previous_freq;
	stat->busy_time = data->um_data.val_pmcnt;
	stat->total_time = data->um_data.val_ccnt;
	stat->delta_time = data->last_monitor_period;

	data->last_um_usage_rate = div64_u64(stat->busy_time * 100, stat->total_time);

	for (i = 0; i < data->um_data.um_count; i++) {
		exynos_reset_ppc(data->um_data.va_base[i]);
		exynos_start_ppc(data->um_data.va_base[i]);
	}

	return 0;
}

void register_get_dev_status(struct exynos_devfreq_data *data)
{
	data->devfreq_profile.get_dev_status = exynos_devfreq_get_dev_status;
}