• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,
78 				   struct devfreq_event_data *edata)
79 {
80 	struct rockchip_nocp *nocp = devfreq_event_get_drvdata(edev);
81 	void __iomem *reg_base = nocp->reg_base;
82 	u32 counter = 0, counter0 = 0, counter1 = 0;
83 	int time_ms = 0;
84 
85 	time_ms = ktime_to_ms(ktime_sub(ktime_get(), nocp->time));
86 
87 	counter0 = readl_relaxed(reg_base + PROBE_COUNTERS_0_VAL);
88 	counter1 = readl_relaxed(reg_base + PROBE_COUNTERS_1_VAL);
89 	counter = (counter0 & 0xffff) | ((counter1 & 0xffff) << 16);
90 	counter = counter / 1000000;
91 	if (time_ms > 0)
92 		edata->load_count = (counter * 1000) / time_ms;
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 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
131 	nocp->reg_base = devm_ioremap_resource(&pdev->dev, res);
132 	if (IS_ERR(nocp->reg_base))
133 		return PTR_ERR(nocp->reg_base);
134 
135 	desc = devm_kzalloc(&pdev->dev, sizeof(*desc), GFP_KERNEL);
136 	if (!desc)
137 		return -ENOMEM;
138 
139 	desc->ops = &rockchip_nocp_ops;
140 	desc->driver_data = nocp;
141 	desc->name = np->name;
142 	nocp->desc = desc;
143 	nocp->dev = &pdev->dev;
144 	nocp->edev = devm_devfreq_event_add_edev(&pdev->dev, desc);
145 	if (IS_ERR(nocp->edev)) {
146 		dev_err(&pdev->dev, "failed to add devfreq-event device\n");
147 		return PTR_ERR(nocp->edev);
148 	}
149 
150 	platform_set_drvdata(pdev, nocp);
151 
152 	return 0;
153 }
154 
155 static struct platform_driver rockchip_nocp_driver = {
156 	.probe = rockchip_nocp_probe,
157 	.driver = {
158 		.name = "rockchip-nocp",
159 		.of_match_table = rockchip_nocp_id_match,
160 	},
161 };
162 module_platform_driver(rockchip_nocp_driver);
163 
164 MODULE_DESCRIPTION("Rockchip NoC (Network on Chip) Probe driver");
165 MODULE_AUTHOR("Finley Xiao <finley.xiao@rock-chips.com>");
166 MODULE_LICENSE("GPL v2");
167