blob: e951c8fab778b0e72e9b98e453d80b498781438c [file] [log] [blame]
Romain Perier6eacf312014-09-08 17:14:47 +00001/**
2 * emac-rockchip.c - Rockchip EMAC specific glue layer
3 *
4 * Copyright (C) 2014 Romain Perier <romain.perier@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17#include <linux/etherdevice.h>
18#include <linux/mfd/syscon.h>
19#include <linux/module.h>
20#include <linux/of_net.h>
21#include <linux/platform_device.h>
22#include <linux/regmap.h>
23#include <linux/regulator/consumer.h>
24
25#include "emac.h"
26
27#define DRV_NAME "rockchip_emac"
Xing Zhengf4c9d3e2016-01-08 09:35:01 +080028#define DRV_VERSION "1.1"
Romain Perier6eacf312014-09-08 17:14:47 +000029
30struct emac_rockchip_soc_data {
Xing Zhengf4c9d3e2016-01-08 09:35:01 +080031 unsigned int grf_offset;
32 unsigned int grf_mode_offset;
33 unsigned int grf_speed_offset;
34 bool need_div_macclk;
Romain Perier6eacf312014-09-08 17:14:47 +000035};
36
37struct rockchip_priv_data {
38 struct arc_emac_priv emac;
39 struct regmap *grf;
40 const struct emac_rockchip_soc_data *soc_data;
41 struct regulator *regulator;
42 struct clk *refclk;
Xing Zhengf4c9d3e2016-01-08 09:35:01 +080043 struct clk *macclk;
Romain Perier6eacf312014-09-08 17:14:47 +000044};
45
46static void emac_rockchip_set_mac_speed(void *priv, unsigned int speed)
47{
48 struct rockchip_priv_data *emac = priv;
Xing Zhengf4c9d3e2016-01-08 09:35:01 +080049 u32 speed_offset = emac->soc_data->grf_speed_offset;
Romain Perier6eacf312014-09-08 17:14:47 +000050 u32 data;
51 int err = 0;
52
Romain Perier6eacf312014-09-08 17:14:47 +000053 switch(speed) {
54 case 10:
Xing Zhengf4c9d3e2016-01-08 09:35:01 +080055 data = (1 << (speed_offset + 16)) | (0 << speed_offset);
Romain Perier6eacf312014-09-08 17:14:47 +000056 break;
57 case 100:
Xing Zhengf4c9d3e2016-01-08 09:35:01 +080058 data = (1 << (speed_offset + 16)) | (1 << speed_offset);
Romain Perier6eacf312014-09-08 17:14:47 +000059 break;
60 default:
61 pr_err("speed %u not supported\n", speed);
62 return;
63 }
64
65 err = regmap_write(emac->grf, emac->soc_data->grf_offset, data);
66 if (err)
67 pr_err("unable to apply speed %u to grf (%d)\n", speed, err);
68}
69
Xing Zhengf4c9d3e2016-01-08 09:35:01 +080070static const struct emac_rockchip_soc_data emac_rk3066_emac_data = {
71 .grf_offset = 0x154, .grf_mode_offset = 0,
72 .grf_speed_offset = 1, .need_div_macclk = 0,
73};
74
75static const struct emac_rockchip_soc_data emac_rk3188_emac_data = {
76 .grf_offset = 0x0a4, .grf_mode_offset = 0,
77 .grf_speed_offset = 1, .need_div_macclk = 0,
Romain Perier6eacf312014-09-08 17:14:47 +000078};
79
80static const struct of_device_id emac_rockchip_dt_ids[] = {
Xing Zhengf4c9d3e2016-01-08 09:35:01 +080081 { .compatible = "rockchip,rk3066-emac", .data = &emac_rk3066_emac_data },
82 { .compatible = "rockchip,rk3188-emac", .data = &emac_rk3188_emac_data },
Romain Perier6eacf312014-09-08 17:14:47 +000083 { /* Sentinel */ }
84};
85
86MODULE_DEVICE_TABLE(of, emac_rockchip_dt_ids);
87
88static int emac_rockchip_probe(struct platform_device *pdev)
89{
90 struct device *dev = &pdev->dev;
91 struct net_device *ndev;
92 struct rockchip_priv_data *priv;
93 const struct of_device_id *match;
94 u32 data;
95 int err, interface;
96
97 if (!pdev->dev.of_node)
98 return -ENODEV;
99
100 ndev = alloc_etherdev(sizeof(struct rockchip_priv_data));
101 if (!ndev)
102 return -ENOMEM;
103 platform_set_drvdata(pdev, ndev);
104 SET_NETDEV_DEV(ndev, dev);
105
106 priv = netdev_priv(ndev);
107 priv->emac.drv_name = DRV_NAME;
108 priv->emac.drv_version = DRV_VERSION;
109 priv->emac.set_mac_speed = emac_rockchip_set_mac_speed;
110
111 interface = of_get_phy_mode(dev->of_node);
112
113 /* RK3066 and RK3188 SoCs only support RMII */
114 if (interface != PHY_INTERFACE_MODE_RMII) {
115 dev_err(dev, "unsupported phy interface mode %d\n", interface);
116 err = -ENOTSUPP;
117 goto out_netdev;
118 }
119
120 priv->grf = syscon_regmap_lookup_by_phandle(dev->of_node, "rockchip,grf");
121 if (IS_ERR(priv->grf)) {
122 dev_err(dev, "failed to retrieve global register file (%ld)\n", PTR_ERR(priv->grf));
123 err = PTR_ERR(priv->grf);
124 goto out_netdev;
125 }
126
127 match = of_match_node(emac_rockchip_dt_ids, dev->of_node);
128 priv->soc_data = match->data;
129
130 priv->emac.clk = devm_clk_get(dev, "hclk");
131 if (IS_ERR(priv->emac.clk)) {
132 dev_err(dev, "failed to retrieve host clock (%ld)\n", PTR_ERR(priv->emac.clk));
133 err = PTR_ERR(priv->emac.clk);
134 goto out_netdev;
135 }
136
137 priv->refclk = devm_clk_get(dev, "macref");
138 if (IS_ERR(priv->refclk)) {
139 dev_err(dev, "failed to retrieve reference clock (%ld)\n", PTR_ERR(priv->refclk));
140 err = PTR_ERR(priv->refclk);
141 goto out_netdev;
142 }
143
144 err = clk_prepare_enable(priv->refclk);
145 if (err) {
146 dev_err(dev, "failed to enable reference clock (%d)\n", err);
147 goto out_netdev;
148 }
149
150 /* Optional regulator for PHY */
151 priv->regulator = devm_regulator_get_optional(dev, "phy");
152 if (IS_ERR(priv->regulator)) {
153 if (PTR_ERR(priv->regulator) == -EPROBE_DEFER)
154 return -EPROBE_DEFER;
155 dev_err(dev, "no regulator found\n");
156 priv->regulator = NULL;
157 }
158
159 if (priv->regulator) {
160 err = regulator_enable(priv->regulator);
161 if (err) {
162 dev_err(dev, "failed to enable phy-supply (%d)\n", err);
163 goto out_clk_disable;
164 }
165 }
166
Xing Zhengf4c9d3e2016-01-08 09:35:01 +0800167 /* Set speed 100M */
168 data = (1 << (priv->soc_data->grf_speed_offset + 16)) |
169 (1 << priv->soc_data->grf_speed_offset);
170 /* Set RMII mode */
171 data |= (1 << (priv->soc_data->grf_mode_offset + 16)) |
172 (0 << priv->soc_data->grf_mode_offset);
Romain Perier6eacf312014-09-08 17:14:47 +0000173
174 err = regmap_write(priv->grf, priv->soc_data->grf_offset, data);
175 if (err) {
176 dev_err(dev, "unable to apply initial settings to grf (%d)\n", err);
177 goto out_regulator_disable;
178 }
179
180 /* RMII interface needs always a rate of 50MHz */
181 err = clk_set_rate(priv->refclk, 50000000);
182 if (err)
183 dev_err(dev, "failed to change reference clock rate (%d)\n", err);
Xing Zhengc9bca2f2016-01-08 09:35:00 +0800184
Xing Zhengf4c9d3e2016-01-08 09:35:01 +0800185 if (priv->soc_data->need_div_macclk) {
186 priv->macclk = devm_clk_get(dev, "macclk");
187 if (IS_ERR(priv->macclk)) {
188 dev_err(dev, "failed to retrieve mac clock (%ld)\n", PTR_ERR(priv->macclk));
189 err = PTR_ERR(priv->macclk);
190 goto out_regulator_disable;
191 }
192
193 err = clk_prepare_enable(priv->macclk);
194 if (err) {
195 dev_err(dev, "failed to enable mac clock (%d)\n", err);
196 goto out_regulator_disable;
197 }
198
199 /* RMII TX/RX needs always a rate of 25MHz */
200 err = clk_set_rate(priv->macclk, 25000000);
201 if (err)
202 dev_err(dev, "failed to change mac clock rate (%d)\n", err);
203 }
204
Xing Zhengc9bca2f2016-01-08 09:35:00 +0800205 err = arc_emac_probe(ndev, interface);
206 if (err) {
207 dev_err(dev, "failed to probe arc emac (%d)\n", err);
208 goto out_regulator_disable;
209 }
210
Romain Perier6eacf312014-09-08 17:14:47 +0000211 return 0;
212
213out_regulator_disable:
214 if (priv->regulator)
215 regulator_disable(priv->regulator);
216out_clk_disable:
217 clk_disable_unprepare(priv->refclk);
218out_netdev:
219 free_netdev(ndev);
220 return err;
221}
222
223static int emac_rockchip_remove(struct platform_device *pdev)
224{
225 struct net_device *ndev = platform_get_drvdata(pdev);
226 struct rockchip_priv_data *priv = netdev_priv(ndev);
227 int err;
228
Romain Periercf98192d2014-09-10 07:51:13 +0000229 err = arc_emac_remove(ndev);
230
Romain Perier6eacf312014-09-08 17:14:47 +0000231 clk_disable_unprepare(priv->refclk);
232
233 if (priv->regulator)
234 regulator_disable(priv->regulator);
235
Romain Perier6eacf312014-09-08 17:14:47 +0000236 free_netdev(ndev);
237 return err;
238}
239
240static struct platform_driver emac_rockchip_driver = {
241 .probe = emac_rockchip_probe,
242 .remove = emac_rockchip_remove,
243 .driver = {
244 .name = DRV_NAME,
245 .of_match_table = emac_rockchip_dt_ids,
246 },
247};
248
249module_platform_driver(emac_rockchip_driver);
250
251MODULE_AUTHOR("Romain Perier <romain.perier@gmail.com>");
252MODULE_DESCRIPTION("Rockchip EMAC platform driver");
253MODULE_LICENSE("GPL");