5a3f75a02e5e4e4870abcd1de36061670ee7309e
[openwrt.git] / target / linux / ramips / patches-4.3 / 0504-net-next-mediatek-add-switch-driver-for-mt7621.patch
1 From 9fc19d5f7354709298dcb15b3a4c7cd9a18acebf Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Mon, 14 Dec 2015 21:24:46 +0100
4 Subject: [PATCH 504/513] net-next: mediatek: add switch driver for mt7621
5
6 This driver is very basic and only provides basic init and irq support.
7 Switchdev support for this device will follow.
8
9 Signed-off-by: John Crispin <blogic@openwrt.org>
10 ---
11  drivers/net/ethernet/mediatek/gsw_mt7621.c |  284 ++++++++++++++++++++++++++++
12  1 file changed, 284 insertions(+)
13  create mode 100644 drivers/net/ethernet/mediatek/gsw_mt7621.c
14
15 diff --git a/drivers/net/ethernet/mediatek/gsw_mt7621.c b/drivers/net/ethernet/mediatek/gsw_mt7621.c
16 new file mode 100644
17 index 0000000..500841f
18 --- /dev/null
19 +++ b/drivers/net/ethernet/mediatek/gsw_mt7621.c
20 @@ -0,0 +1,284 @@
21 +/*   This program is free software; you can redistribute it and/or modify
22 + *   it under the terms of the GNU General Public License as published by
23 + *   the Free Software Foundation; version 2 of the License
24 + *
25 + *   This program is distributed in the hope that it will be useful,
26 + *   but WITHOUT ANY WARRANTY; without even the implied warranty of
27 + *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28 + *   GNU General Public License for more details.
29 + *
30 + *   Copyright (C) 2009-2015 John Crispin <blogic@openwrt.org>
31 + *   Copyright (C) 2009-2015 Felix Fietkau <nbd@openwrt.org>
32 + *   Copyright (C) 2013-2015 Michael Lee <igvtee@gmail.com>
33 + */
34 +
35 +#include <linux/module.h>
36 +#include <linux/kernel.h>
37 +#include <linux/types.h>
38 +#include <linux/platform_device.h>
39 +#include <linux/of_device.h>
40 +#include <linux/of_irq.h>
41 +
42 +#include <ralink_regs.h>
43 +
44 +#include "mtk_eth_soc.h"
45 +#include "gsw_mt7620.h"
46 +
47 +void mtk_switch_w32(struct mt7620_gsw *gsw, u32 val, unsigned reg)
48 +{
49 +       iowrite32(val, gsw->base + reg);
50 +}
51 +
52 +u32 mtk_switch_r32(struct mt7620_gsw *gsw, unsigned reg)
53 +{
54 +       return ioread32(gsw->base + reg);
55 +}
56 +
57 +static irqreturn_t gsw_interrupt_mt7621(int irq, void *_priv)
58 +{
59 +       struct fe_priv *priv = (struct fe_priv *)_priv;
60 +       struct mt7620_gsw *gsw = (struct mt7620_gsw *)priv->soc->swpriv;
61 +       u32 reg, i;
62 +
63 +       reg = mt7530_mdio_r32(gsw, 0x700c);
64 +
65 +       for (i = 0; i < 5; i++)
66 +               if (reg & BIT(i)) {
67 +                       unsigned int link;
68 +
69 +                       link = mt7530_mdio_r32(gsw,
70 +                                              0x3008 + (i * 0x100)) & 0x1;
71 +
72 +                       if (link != priv->link[i]) {
73 +                               priv->link[i] = link;
74 +                               if (link)
75 +                                       netdev_info(priv->netdev,
76 +                                                   "port %d link up\n", i);
77 +                               else
78 +                                       netdev_info(priv->netdev,
79 +                                                   "port %d link down\n", i);
80 +                       }
81 +               }
82 +
83 +       mt7530_mdio_w32(gsw, 0x700c, 0x1f);
84 +
85 +       return IRQ_HANDLED;
86 +}
87 +
88 +static void mt7621_hw_init(struct mt7620_gsw *gsw, struct device_node *np)
89 +{
90 +       u32 i;
91 +       u32 val;
92 +
93 +       /* wardware reset the switch */
94 +       fe_reset(RST_CTRL_MCM);
95 +       mdelay(10);
96 +
97 +       /* reduce RGMII2 PAD driving strength */
98 +       rt_sysc_m32(3 << 4, 0, SYSC_PAD_RGMII2_MDIO);
99 +
100 +       /* gpio mux - RGMII1=Normal mode */
101 +       rt_sysc_m32(BIT(14), 0, SYSC_GPIO_MODE);
102 +
103 +       /* set GMAC1 RGMII mode */
104 +       rt_sysc_m32(3 << 12, 0, SYSC_REG_CFG1);
105 +
106 +       /* enable MDIO to control MT7530 */
107 +       rt_sysc_m32(3 << 12, 0, SYSC_GPIO_MODE);
108 +
109 +       /* turn off all PHYs */
110 +       for (i = 0; i <= 4; i++) {
111 +               val = _mt7620_mii_read(gsw, i, 0x0);
112 +               val |= BIT(11);
113 +               _mt7620_mii_write(gsw, i, 0x0, val);
114 +       }
115 +
116 +       /* reset the switch */
117 +       mt7530_mdio_w32(gsw, 0x7000, 0x3);
118 +       usleep_range(10, 20);
119 +
120 +       if ((rt_sysc_r32(SYSC_REG_CHIP_REV_ID) & 0xFFFF) == 0x0101) {
121 +               /* (GE1, Force 1000M/FD, FC ON, MAX_RX_LENGTH 1536) */
122 +               mtk_switch_w32(gsw, 0x2105e30b, 0x100);
123 +               mt7530_mdio_w32(gsw, 0x3600, 0x5e30b);
124 +       } else {
125 +               /* (GE1, Force 1000M/FD, FC ON, MAX_RX_LENGTH 1536) */
126 +               mtk_switch_w32(gsw, 0x2105e33b, 0x100);
127 +               mt7530_mdio_w32(gsw, 0x3600, 0x5e33b);
128 +       }
129 +
130 +       /* (GE2, Link down) */
131 +       mtk_switch_w32(gsw, 0x8000, 0x200);
132 +
133 +       /* Enable Port 6, P5 as GMAC5, P5 disable */
134 +       val = mt7530_mdio_r32(gsw, 0x7804);
135 +       val &= ~BIT(8);
136 +       val |= BIT(6) | BIT(13) | BIT(16);
137 +       mt7530_mdio_w32(gsw, 0x7804, val);
138 +
139 +       val = rt_sysc_r32(0x10);
140 +       val = (val >> 6) & 0x7;
141 +       if (val >= 6) {
142 +               /* 25Mhz Xtal - do nothing */
143 +       } else if (val >= 3) {
144 +               /* 40Mhz */
145 +
146 +               /* disable MT7530 core clock */
147 +               _mt7620_mii_write(gsw, 0, 13, 0x1f);
148 +               _mt7620_mii_write(gsw, 0, 14, 0x410);
149 +               _mt7620_mii_write(gsw, 0, 13, 0x401f);
150 +               _mt7620_mii_write(gsw, 0, 14, 0x0);
151 +
152 +               /* disable MT7530 PLL */
153 +               _mt7620_mii_write(gsw, 0, 13, 0x1f);
154 +               _mt7620_mii_write(gsw, 0, 14, 0x40d);
155 +               _mt7620_mii_write(gsw, 0, 13, 0x401f);
156 +               _mt7620_mii_write(gsw, 0, 14, 0x2020);
157 +
158 +               /* for MT7530 core clock = 500Mhz */
159 +               _mt7620_mii_write(gsw, 0, 13, 0x1f);
160 +               _mt7620_mii_write(gsw, 0, 14, 0x40e);
161 +               _mt7620_mii_write(gsw, 0, 13, 0x401f);
162 +               _mt7620_mii_write(gsw, 0, 14, 0x119);
163 +
164 +               /* enable MT7530 PLL */
165 +               _mt7620_mii_write(gsw, 0, 13, 0x1f);
166 +               _mt7620_mii_write(gsw, 0, 14, 0x40d);
167 +               _mt7620_mii_write(gsw, 0, 13, 0x401f);
168 +               _mt7620_mii_write(gsw, 0, 14, 0x2820);
169 +
170 +               usleep_range(20, 40);
171 +
172 +               /* enable MT7530 core clock */
173 +               _mt7620_mii_write(gsw, 0, 13, 0x1f);
174 +               _mt7620_mii_write(gsw, 0, 14, 0x410);
175 +               _mt7620_mii_write(gsw, 0, 13, 0x401f);
176 +       } else {
177 +               /* 20Mhz Xtal - TODO */
178 +       }
179 +
180 +       /* RGMII */
181 +       _mt7620_mii_write(gsw, 0, 14, 0x1);
182 +
183 +       /* set MT7530 central align */
184 +       val = mt7530_mdio_r32(gsw, 0x7830);
185 +       val &= ~BIT(0);
186 +       val |= BIT(1);
187 +       mt7530_mdio_w32(gsw, 0x7830, val);
188 +       val = mt7530_mdio_r32(gsw, 0x7a40);
189 +       val &= ~BIT(30);
190 +       mt7530_mdio_w32(gsw, 0x7a40, val);
191 +       mt7530_mdio_w32(gsw, 0x7a78, 0x855);
192 +
193 +       /* delay setting for 10/1000M */
194 +       mt7530_mdio_w32(gsw, 0x7b00, 0x102);
195 +       mt7530_mdio_w32(gsw, 0x7b04, 0x14);
196 +
197 +       /* lower Tx Driving*/
198 +       mt7530_mdio_w32(gsw, 0x7a54, 0x44);
199 +       mt7530_mdio_w32(gsw, 0x7a5c, 0x44);
200 +       mt7530_mdio_w32(gsw, 0x7a64, 0x44);
201 +       mt7530_mdio_w32(gsw, 0x7a6c, 0x44);
202 +       mt7530_mdio_w32(gsw, 0x7a74, 0x44);
203 +       mt7530_mdio_w32(gsw, 0x7a7c, 0x44);
204 +
205 +       /* turn on all PHYs */
206 +       for (i = 0; i <= 4; i++) {
207 +               val = _mt7620_mii_read(gsw, i, 0);
208 +               val &= ~BIT(11);
209 +               _mt7620_mii_write(gsw, i, 0, val);
210 +       }
211 +
212 +       /* enable irq */
213 +       val = mt7530_mdio_r32(gsw, 0x7808);
214 +       val |= 3 << 16;
215 +       mt7530_mdio_w32(gsw, 0x7808, val);
216 +}
217 +
218 +static const struct of_device_id mediatek_gsw_match[] = {
219 +       { .compatible = "mediatek,mt7621-gsw" },
220 +       {},
221 +};
222 +MODULE_DEVICE_TABLE(of, mediatek_gsw_match);
223 +
224 +int mtk_gsw_init(struct fe_priv *priv)
225 +{
226 +       struct device_node *np = priv->switch_np;
227 +       struct platform_device *pdev = of_find_device_by_node(np);
228 +       struct mt7620_gsw *gsw;
229 +
230 +       if (!pdev)
231 +               return -ENODEV;
232 +
233 +       if (!of_device_is_compatible(np, mediatek_gsw_match->compatible))
234 +               return -EINVAL;
235 +
236 +       gsw = platform_get_drvdata(pdev);
237 +       priv->soc->swpriv = gsw;
238 +
239 +       mt7621_hw_init(gsw, np);
240 +
241 +       if (gsw->irq) {
242 +               request_irq(gsw->irq, gsw_interrupt_mt7621, 0,
243 +                           "gsw", priv);
244 +               mt7530_mdio_w32(gsw, 0x7008, 0x1f);
245 +       }
246 +
247 +       return 0;
248 +}
249 +
250 +static int mt7621_gsw_probe(struct platform_device *pdev)
251 +{
252 +       struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
253 +       const char *port4 = NULL;
254 +       struct mt7620_gsw *gsw;
255 +       struct device_node *np;
256 +
257 +       gsw = devm_kzalloc(&pdev->dev, sizeof(struct mt7620_gsw), GFP_KERNEL);
258 +       if (!gsw)
259 +               return -ENOMEM;
260 +
261 +       gsw->base = devm_ioremap_resource(&pdev->dev, res);
262 +       if (!gsw->base)
263 +               return -EADDRNOTAVAIL;
264 +
265 +       gsw->dev = &pdev->dev;
266 +
267 +       of_property_read_string(np, "mediatek,port4", &port4);
268 +       if (port4 && !strcmp(port4, "ephy"))
269 +               gsw->port4 = PORT4_EPHY;
270 +       else if (port4 && !strcmp(port4, "gmac"))
271 +               gsw->port4 = PORT4_EXT;
272 +       else
273 +               gsw->port4 = PORT4_EPHY;
274 +
275 +       gsw->irq = irq_of_parse_and_map(np, 0);
276 +
277 +       platform_set_drvdata(pdev, gsw);
278 +
279 +       return 0;
280 +}
281 +
282 +static int mt7621_gsw_remove(struct platform_device *pdev)
283 +{
284 +       platform_set_drvdata(pdev, NULL);
285 +
286 +       return 0;
287 +}
288 +
289 +static struct platform_driver gsw_driver = {
290 +       .probe = mt7621_gsw_probe,
291 +       .remove = mt7621_gsw_remove,
292 +       .driver = {
293 +               .name = "mt7621-gsw",
294 +               .owner = THIS_MODULE,
295 +               .of_match_table = mediatek_gsw_match,
296 +       },
297 +};
298 +
299 +module_platform_driver(gsw_driver);
300 +
301 +MODULE_LICENSE("GPL");
302 +MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
303 +MODULE_DESCRIPTION("GBit switch driver for Mediatek MT7621 SoC");
304 +MODULE_VERSION(MTK_FE_DRV_VERSION);
305 -- 
306 1.7.10.4
307