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/of_device.h>
21 #include <linux/platform_device.h>
22
23 #define EVENT_BYTE 0x08
24 #define EVENT_CHAIN 0x10
25
26 #define START_EN BIT(3)
27 #define GLOBAL_EN BIT(0)
28 #define START_GO BIT(0)
29
30 #define PROBE_MAINCTL 0x0008
31 #define PROBE_CFGCTL 0x000c
32 #define PROBE_STATPERIOD 0x0024
33 #define PROBE_STATGO 0x0028
34
35 struct nocp_info {
36 u32 counter0_src;
37 u32 counter0_val;
38 u32 counter1_src;
39 u32 counter1_val;
40 };
41
42 struct rockchip_nocp {
43 void __iomem *reg_base;
44 struct device *dev;
45 struct devfreq_event_dev *edev;
46 struct devfreq_event_desc *desc;
47 const struct nocp_info *info;
48 ktime_t time;
49 };
50
rockchip_nocp_enable(struct devfreq_event_dev * edev)51 static int rockchip_nocp_enable(struct devfreq_event_dev *edev)
52 {
53 struct rockchip_nocp *nocp = devfreq_event_get_drvdata(edev);
54 const struct nocp_info *info = nocp->info;
55 void __iomem *reg_base = nocp->reg_base;
56
57 writel_relaxed(GLOBAL_EN, reg_base + PROBE_CFGCTL);
58 writel_relaxed(START_EN, reg_base + PROBE_MAINCTL);
59 writel_relaxed(0, reg_base + PROBE_STATPERIOD);
60 writel_relaxed(EVENT_BYTE, reg_base + info->counter0_src);
61 writel_relaxed(EVENT_CHAIN, reg_base + info->counter1_src);
62 writel_relaxed(START_GO, reg_base + PROBE_STATGO);
63
64 nocp->time = ktime_get();
65
66 return 0;
67 }
68
rockchip_nocp_disable(struct devfreq_event_dev * edev)69 static int rockchip_nocp_disable(struct devfreq_event_dev *edev)
70 {
71 struct rockchip_nocp *nocp = devfreq_event_get_drvdata(edev);
72 const struct nocp_info *info = nocp->info;
73 void __iomem *reg_base = nocp->reg_base;
74
75 writel_relaxed(0, reg_base + PROBE_STATGO);
76 writel_relaxed(0, reg_base + PROBE_MAINCTL);
77 writel_relaxed(0, reg_base + PROBE_CFGCTL);
78 writel_relaxed(0, reg_base + info->counter0_src);
79 writel_relaxed(0, reg_base + info->counter1_src);
80
81 return 0;
82 }
83
rockchip_nocp_get_event(struct devfreq_event_dev * edev,struct devfreq_event_data * edata)84 static int rockchip_nocp_get_event(struct devfreq_event_dev *edev,
85 struct devfreq_event_data *edata)
86 {
87 struct rockchip_nocp *nocp = devfreq_event_get_drvdata(edev);
88 const struct nocp_info *info = nocp->info;
89 void __iomem *reg_base = nocp->reg_base;
90 u32 counter = 0, counter0 = 0, counter1 = 0;
91 int time_ms = 0;
92
93 time_ms = ktime_to_ms(ktime_sub(ktime_get(), nocp->time));
94
95 counter0 = readl_relaxed(reg_base + info->counter0_val);
96 counter1 = readl_relaxed(reg_base + info->counter1_val);
97 counter = (counter0 & 0xffff) | ((counter1 & 0xffff) << 16);
98 counter = counter / 1000000;
99 if (time_ms > 0)
100 edata->load_count = (counter * 1000) / time_ms;
101
102 writel_relaxed(START_GO, reg_base + PROBE_STATGO);
103 nocp->time = ktime_get();
104
105 return 0;
106 }
107
rockchip_nocp_set_event(struct devfreq_event_dev * edev)108 static int rockchip_nocp_set_event(struct devfreq_event_dev *edev)
109 {
110 return 0;
111 }
112
113 static const struct devfreq_event_ops rockchip_nocp_ops = {
114 .disable = rockchip_nocp_disable,
115 .enable = rockchip_nocp_enable,
116 .get_event = rockchip_nocp_get_event,
117 .set_event = rockchip_nocp_set_event,
118 };
119
120 static const struct nocp_info rk3288_nocp = {
121 .counter0_src = 0x138,
122 .counter0_val = 0x13c,
123 .counter1_src = 0x14c,
124 .counter1_val = 0x150,
125 };
126
127 static const struct nocp_info rk3568_nocp = {
128 .counter0_src = 0x204,
129 .counter0_val = 0x20c,
130 .counter1_src = 0x214,
131 .counter1_val = 0x21c,
132 };
133
134 static const struct of_device_id rockchip_nocp_id_match[] = {
135 {
136 .compatible = "rockchip,rk3288-nocp",
137 .data = (void *)&rk3288_nocp,
138 },
139 {
140 .compatible = "rockchip,rk3368-nocp",
141 .data = (void *)&rk3288_nocp,
142 },
143 {
144 .compatible = "rockchip,rk3399-nocp",
145 .data = (void *)&rk3288_nocp,
146 },
147 {
148 .compatible = "rockchip,rk3568-nocp",
149 .data = (void *)&rk3568_nocp,
150 },
151 { },
152 };
153
rockchip_nocp_probe(struct platform_device * pdev)154 static int rockchip_nocp_probe(struct platform_device *pdev)
155 {
156 struct resource *res;
157 struct rockchip_nocp *nocp;
158 struct devfreq_event_desc *desc;
159 struct device_node *np = pdev->dev.of_node;
160 const struct of_device_id *match;
161
162 match = of_match_device(rockchip_nocp_id_match, &pdev->dev);
163 if (!match || !match->data) {
164 dev_err(&pdev->dev, "missing nocp data\n");
165 return -ENODEV;
166 }
167
168 nocp = devm_kzalloc(&pdev->dev, sizeof(*nocp), GFP_KERNEL);
169 if (!nocp)
170 return -ENOMEM;
171
172 nocp->info = match->data;
173
174 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
175 nocp->reg_base = devm_ioremap_resource(&pdev->dev, res);
176 if (IS_ERR(nocp->reg_base))
177 return PTR_ERR(nocp->reg_base);
178
179 desc = devm_kzalloc(&pdev->dev, sizeof(*desc), GFP_KERNEL);
180 if (!desc)
181 return -ENOMEM;
182
183 desc->ops = &rockchip_nocp_ops;
184 desc->driver_data = nocp;
185 desc->name = np->name;
186 nocp->desc = desc;
187 nocp->dev = &pdev->dev;
188 nocp->edev = devm_devfreq_event_add_edev(&pdev->dev, desc);
189 if (IS_ERR(nocp->edev)) {
190 dev_err(&pdev->dev, "failed to add devfreq-event device\n");
191 return PTR_ERR(nocp->edev);
192 }
193
194 platform_set_drvdata(pdev, nocp);
195
196 return 0;
197 }
198
199 static struct platform_driver rockchip_nocp_driver = {
200 .probe = rockchip_nocp_probe,
201 .driver = {
202 .name = "rockchip-nocp",
203 .of_match_table = rockchip_nocp_id_match,
204 },
205 };
206 module_platform_driver(rockchip_nocp_driver);
207
208 MODULE_DESCRIPTION("Rockchip NoC (Network on Chip) Probe driver");
209 MODULE_AUTHOR("Finley Xiao <finley.xiao@rock-chips.com>");
210 MODULE_LICENSE("GPL v2");
211