1 /*
2 * Copyright (c) 2016, Fuzhou Rockchip Electronics Co., Ltd
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 */
13
14 #include <linux/devfreq-event.h>
15 #include <linux/io.h>
16 #include <linux/kernel.h>
17 #include <linux/mfd/syscon.h>
18 #include <linux/module.h>
19 #include <linux/of.h>
20 #include <linux/platform_device.h>
21
22 #define EVENT_BYTE 0x08
23 #define EVENT_CHAIN 0x10
24
25 #define START_EN BIT(3)
26 #define GLOBAL_EN BIT(0)
27 #define START_GO BIT(0)
28
29 #define PROBE_MAINCTL 0x0008
30 #define PROBE_CFGCTL 0x000c
31 #define PROBE_STATPERIOD 0x0024
32 #define PROBE_STATGO 0x0028
33 #define PROBE_COUNTERS_0_SRC 0x0138
34 #define PROBE_COUNTERS_0_VAL 0x013c
35 #define PROBE_COUNTERS_1_SRC 0x014c
36 #define PROBE_COUNTERS_1_VAL 0x0150
37
38 struct rockchip_nocp {
39 void __iomem *reg_base;
40 struct device *dev;
41 struct devfreq_event_dev *edev;
42 struct devfreq_event_desc *desc;
43 ktime_t time;
44 };
45
rockchip_nocp_enable(struct devfreq_event_dev * edev)46 static int rockchip_nocp_enable(struct devfreq_event_dev *edev)
47 {
48 struct rockchip_nocp *nocp = devfreq_event_get_drvdata(edev);
49 void __iomem *reg_base = nocp->reg_base;
50
51 writel_relaxed(GLOBAL_EN, reg_base + PROBE_CFGCTL);
52 writel_relaxed(START_EN, reg_base + PROBE_MAINCTL);
53 writel_relaxed(0, reg_base + PROBE_STATPERIOD);
54 writel_relaxed(EVENT_BYTE, reg_base + PROBE_COUNTERS_0_SRC);
55 writel_relaxed(EVENT_CHAIN, reg_base + PROBE_COUNTERS_1_SRC);
56 writel_relaxed(START_GO, reg_base + PROBE_STATGO);
57
58 nocp->time = ktime_get();
59
60 return 0;
61 }
62
rockchip_nocp_disable(struct devfreq_event_dev * edev)63 static int rockchip_nocp_disable(struct devfreq_event_dev *edev)
64 {
65 struct rockchip_nocp *nocp = devfreq_event_get_drvdata(edev);
66 void __iomem *reg_base = nocp->reg_base;
67
68 writel_relaxed(0, reg_base + PROBE_STATGO);
69 writel_relaxed(0, reg_base + PROBE_MAINCTL);
70 writel_relaxed(0, reg_base + PROBE_CFGCTL);
71 writel_relaxed(0, reg_base + PROBE_COUNTERS_0_SRC);
72 writel_relaxed(0, reg_base + PROBE_COUNTERS_1_SRC);
73
74 return 0;
75 }
76
rockchip_nocp_get_event(struct devfreq_event_dev * edev,struct devfreq_event_data * edata)77 static int rockchip_nocp_get_event(struct devfreq_event_dev *edev, struct devfreq_event_data *edata)
78 {
79 struct rockchip_nocp *nocp = devfreq_event_get_drvdata(edev);
80 void __iomem *reg_base = nocp->reg_base;
81 u32 counter = 0, counter0 = 0, counter1 = 0;
82 int time_ms = 0;
83
84 time_ms = ktime_to_ms(ktime_sub(ktime_get(), nocp->time));
85
86 counter0 = readl_relaxed(reg_base + PROBE_COUNTERS_0_VAL);
87 counter1 = readl_relaxed(reg_base + PROBE_COUNTERS_1_VAL);
88 counter = (counter0 & 0xffff) | ((counter1 & 0xffff) << 0x10);
89 counter = counter / 0xf4240;
90 if (time_ms > 0) {
91 edata->load_count = (counter * 0x3e8) / time_ms;
92 }
93
94 writel_relaxed(START_GO, reg_base + PROBE_STATGO);
95 nocp->time = ktime_get();
96
97 return 0;
98 }
99
rockchip_nocp_set_event(struct devfreq_event_dev * edev)100 static int rockchip_nocp_set_event(struct devfreq_event_dev *edev)
101 {
102 return 0;
103 }
104
105 static const struct devfreq_event_ops rockchip_nocp_ops = {
106 .disable = rockchip_nocp_disable,
107 .enable = rockchip_nocp_enable,
108 .get_event = rockchip_nocp_get_event,
109 .set_event = rockchip_nocp_set_event,
110 };
111
112 static const struct of_device_id rockchip_nocp_id_match[] = {
113 {.compatible = "rockchip,rk3288-nocp"},
114 {.compatible = "rockchip,rk3368-nocp"},
115 {.compatible = "rockchip,rk3399-nocp"},
116 {},
117 };
118
rockchip_nocp_probe(struct platform_device * pdev)119 static int rockchip_nocp_probe(struct platform_device *pdev)
120 {
121 struct resource *res;
122 struct rockchip_nocp *nocp;
123 struct devfreq_event_desc *desc;
124 struct device_node *np = pdev->dev.of_node;
125
126 nocp = devm_kzalloc(&pdev->dev, sizeof(*nocp), GFP_KERNEL);
127 if (!nocp) {
128 return -ENOMEM;
129 }
130
131 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
132 nocp->reg_base = devm_ioremap_resource(&pdev->dev, res);
133 if (IS_ERR(nocp->reg_base)) {
134 return PTR_ERR(nocp->reg_base);
135 }
136
137 desc = devm_kzalloc(&pdev->dev, sizeof(*desc), GFP_KERNEL);
138 if (!desc) {
139 return -ENOMEM;
140 }
141
142 desc->ops = &rockchip_nocp_ops;
143 desc->driver_data = nocp;
144 desc->name = np->name;
145 nocp->desc = desc;
146 nocp->dev = &pdev->dev;
147 nocp->edev = devm_devfreq_event_add_edev(&pdev->dev, desc);
148 if (IS_ERR(nocp->edev)) {
149 dev_err(&pdev->dev, "failed to add devfreq-event device\n");
150 return PTR_ERR(nocp->edev);
151 }
152
153 platform_set_drvdata(pdev, nocp);
154
155 return 0;
156 }
157
158 static struct platform_driver rockchip_nocp_driver = {
159 .probe = rockchip_nocp_probe,
160 .driver =
161 {
162 .name = "rockchip-nocp",
163 .of_match_table = rockchip_nocp_id_match,
164 },
165 };
166 module_platform_driver(rockchip_nocp_driver);
167
168 MODULE_DESCRIPTION("Rockchip NoC (Network on Chip) Probe driver");
169 MODULE_AUTHOR("Finley Xiao <finley.xiao@rock-chips.com>");
170 MODULE_LICENSE("GPL v2");
171