ramips: mt7620: fix bad indent
[openwrt/openwrt.git] / target / linux / ramips / files-4.14 / drivers / net / ethernet / mediatek / gsw_mt7620.c
1 /* This program is free software; you can redistribute it and/or modify
2 * it under the terms of the GNU General Public License as published by
3 * the Free Software Foundation; version 2 of the License
4 *
5 * This program is distributed in the hope that it will be useful,
6 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8 * GNU General Public License for more details.
9 *
10 * Copyright (C) 2009-2015 John Crispin <blogic@openwrt.org>
11 * Copyright (C) 2009-2015 Felix Fietkau <nbd@nbd.name>
12 * Copyright (C) 2013-2015 Michael Lee <igvtee@gmail.com>
13 */
14
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/types.h>
18 #include <linux/platform_device.h>
19 #include <linux/of_device.h>
20 #include <linux/of_irq.h>
21
22 #include <ralink_regs.h>
23
24 #include "mtk_eth_soc.h"
25 #include "gsw_mt7620.h"
26
27 void mtk_switch_w32(struct mt7620_gsw *gsw, u32 val, unsigned reg)
28 {
29 iowrite32(val, gsw->base + reg);
30 }
31
32 u32 mtk_switch_r32(struct mt7620_gsw *gsw, unsigned reg)
33 {
34 return ioread32(gsw->base + reg);
35 }
36
37 static irqreturn_t gsw_interrupt_mt7620(int irq, void *_priv)
38 {
39 struct fe_priv *priv = (struct fe_priv *)_priv;
40 struct mt7620_gsw *gsw = (struct mt7620_gsw *)priv->soc->swpriv;
41 u32 status;
42 int i, max = (gsw->port4 == PORT4_EPHY) ? (4) : (3);
43
44 status = mtk_switch_r32(gsw, GSW_REG_ISR);
45 if (status & PORT_IRQ_ST_CHG)
46 for (i = 0; i <= max; i++) {
47 u32 status = mtk_switch_r32(gsw, GSW_REG_PORT_STATUS(i));
48 int link = status & 0x1;
49
50 if (link != priv->link[i])
51 mt7620_print_link_state(priv, i, link,
52 (status >> 2) & 3,
53 (status & 0x2));
54
55 priv->link[i] = link;
56 }
57 mt7620_handle_carrier(priv);
58 mtk_switch_w32(gsw, status, GSW_REG_ISR);
59
60 return IRQ_HANDLED;
61 }
62
63 static int mt7620_mdio_mode(struct device_node *eth_node)
64 {
65 struct device_node *phy_node, *mdiobus_node;
66 const __be32 *id;
67 int ret = 0;
68
69 mdiobus_node = of_get_child_by_name(eth_node, "mdio-bus");
70
71 if (mdiobus_node) {
72 for_each_child_of_node(mdiobus_node, phy_node) {
73 id = of_get_property(phy_node, "reg", NULL);
74 if (id && (be32_to_cpu(*id) == 0x1f))
75 ret = 1;
76 }
77
78 of_node_put(mdiobus_node);
79 }
80
81 return ret;
82 }
83
84 static void mt7620_hw_init(struct mt7620_gsw *gsw, int mdio_mode)
85 {
86 u32 i;
87 u32 val;
88 u32 is_BGA = (rt_sysc_r32(0x0c) >> 16) & 1;
89
90 rt_sysc_w32(rt_sysc_r32(SYSC_REG_CFG1) | BIT(8), SYSC_REG_CFG1);
91 mtk_switch_w32(gsw, mtk_switch_r32(gsw, GSW_REG_CKGCR) & ~(0x3 << 4), GSW_REG_CKGCR);
92
93 /* Enable MIB stats */
94 mtk_switch_w32(gsw, mtk_switch_r32(gsw, GSW_REG_MIB_CNT_EN) | (1 << 1), GSW_REG_MIB_CNT_EN);
95
96 if (mdio_mode) {
97 u32 val;
98
99 /* turn off ephy and set phy base addr to 12 */
100 mtk_switch_w32(gsw, mtk_switch_r32(gsw, GSW_REG_GPC1) |
101 (0x1f << 24) | (0xc << 16),
102 GSW_REG_GPC1);
103
104 /* set MT7530 central align */
105 val = mt7530_mdio_r32(gsw, 0x7830);
106 val &= ~BIT(0);
107 val |= BIT(1);
108 mt7530_mdio_w32(gsw, 0x7830, val);
109
110 val = mt7530_mdio_r32(gsw, 0x7a40);
111 val &= ~BIT(30);
112 mt7530_mdio_w32(gsw, 0x7a40, val);
113
114 mt7530_mdio_w32(gsw, 0x7a78, 0x855);
115 } else {
116 /* global page 4 */
117 _mt7620_mii_write(gsw, 1, 31, 0x4000);
118
119 _mt7620_mii_write(gsw, 1, 17, 0x7444);
120 if (is_BGA)
121 _mt7620_mii_write(gsw, 1, 19, 0x0114);
122 else
123 _mt7620_mii_write(gsw, 1, 19, 0x0117);
124
125 _mt7620_mii_write(gsw, 1, 22, 0x10cf);
126 _mt7620_mii_write(gsw, 1, 25, 0x6212);
127 _mt7620_mii_write(gsw, 1, 26, 0x0777);
128 _mt7620_mii_write(gsw, 1, 29, 0x4000);
129 _mt7620_mii_write(gsw, 1, 28, 0xc077);
130 _mt7620_mii_write(gsw, 1, 24, 0x0000);
131
132 /* global page 3 */
133 _mt7620_mii_write(gsw, 1, 31, 0x3000);
134 _mt7620_mii_write(gsw, 1, 17, 0x4838);
135
136 /* global page 2 */
137 _mt7620_mii_write(gsw, 1, 31, 0x2000);
138 if (is_BGA) {
139 _mt7620_mii_write(gsw, 1, 21, 0x0515);
140 _mt7620_mii_write(gsw, 1, 22, 0x0053);
141 _mt7620_mii_write(gsw, 1, 23, 0x00bf);
142 _mt7620_mii_write(gsw, 1, 24, 0x0aaf);
143 _mt7620_mii_write(gsw, 1, 25, 0x0fad);
144 _mt7620_mii_write(gsw, 1, 26, 0x0fc1);
145 } else {
146 _mt7620_mii_write(gsw, 1, 21, 0x0517);
147 _mt7620_mii_write(gsw, 1, 22, 0x0fd2);
148 _mt7620_mii_write(gsw, 1, 23, 0x00bf);
149 _mt7620_mii_write(gsw, 1, 24, 0x0aab);
150 _mt7620_mii_write(gsw, 1, 25, 0x00ae);
151 _mt7620_mii_write(gsw, 1, 26, 0x0fff);
152 }
153 /* global page 1 */
154 _mt7620_mii_write(gsw, 1, 31, 0x1000);
155 _mt7620_mii_write(gsw, 1, 17, 0xe7f8);
156
157 /* turn on all PHYs */
158 for (i = 0; i <= 4; i++) {
159 val = _mt7620_mii_read(gsw, i, 0);
160 val &= ~BIT(11);
161 _mt7620_mii_write(gsw, i, 0, val);
162 }
163 }
164
165 /* global page 0 */
166 _mt7620_mii_write(gsw, 1, 31, 0x8000);
167 _mt7620_mii_write(gsw, 0, 30, 0xa000);
168 _mt7620_mii_write(gsw, 1, 30, 0xa000);
169 _mt7620_mii_write(gsw, 2, 30, 0xa000);
170 _mt7620_mii_write(gsw, 3, 30, 0xa000);
171
172 _mt7620_mii_write(gsw, 0, 4, 0x05e1);
173 _mt7620_mii_write(gsw, 1, 4, 0x05e1);
174 _mt7620_mii_write(gsw, 2, 4, 0x05e1);
175 _mt7620_mii_write(gsw, 3, 4, 0x05e1);
176
177 /* global page 2 */
178 _mt7620_mii_write(gsw, 1, 31, 0xa000);
179 _mt7620_mii_write(gsw, 0, 16, 0x1111);
180 _mt7620_mii_write(gsw, 1, 16, 0x1010);
181 _mt7620_mii_write(gsw, 2, 16, 0x1515);
182 _mt7620_mii_write(gsw, 3, 16, 0x0f0f);
183
184 /* CPU Port6 Force Link 1G, FC ON */
185 mtk_switch_w32(gsw, 0x5e33b, GSW_REG_PORT_PMCR(6));
186
187 /* Set Port 6 as CPU Port */
188 mtk_switch_w32(gsw, 0x7f7f7fe0, 0x0010);
189
190 /* setup port 4 */
191 if (gsw->port4 == PORT4_EPHY) {
192 u32 val = rt_sysc_r32(SYSC_REG_CFG1);
193
194 val |= 3 << 14;
195 rt_sysc_w32(val, SYSC_REG_CFG1);
196 _mt7620_mii_write(gsw, 4, 30, 0xa000);
197 _mt7620_mii_write(gsw, 4, 4, 0x05e1);
198 _mt7620_mii_write(gsw, 4, 16, 0x1313);
199 pr_info("gsw: setting port4 to ephy mode\n");
200 } else if (!mdio_mode) {
201 u32 val = rt_sysc_r32(SYSC_REG_CFG1);
202
203 val &= ~(3 << 14);
204 rt_sysc_w32(val, SYSC_REG_CFG1);
205 pr_info("gsw: setting port4 to gmac mode\n");
206 }
207 }
208
209 static const struct of_device_id mediatek_gsw_match[] = {
210 { .compatible = "mediatek,mt7620-gsw" },
211 {},
212 };
213 MODULE_DEVICE_TABLE(of, mediatek_gsw_match);
214
215 int mtk_gsw_init(struct fe_priv *priv)
216 {
217 struct device_node *np = priv->switch_np;
218 struct platform_device *pdev = of_find_device_by_node(np);
219 struct mt7620_gsw *gsw;
220
221 if (!pdev)
222 return -ENODEV;
223
224 if (!of_device_is_compatible(np, mediatek_gsw_match->compatible))
225 return -EINVAL;
226
227 gsw = platform_get_drvdata(pdev);
228 priv->soc->swpriv = gsw;
229
230 mt7620_hw_init(gsw, mt7620_mdio_mode(priv->dev->of_node));
231
232 if (gsw->irq) {
233 request_irq(gsw->irq, gsw_interrupt_mt7620, 0,
234 "gsw", priv);
235 mtk_switch_w32(gsw, ~PORT_IRQ_ST_CHG, GSW_REG_IMR);
236 }
237
238 return 0;
239 }
240
241 static int mt7620_gsw_probe(struct platform_device *pdev)
242 {
243 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
244 const char *port4 = NULL;
245 struct mt7620_gsw *gsw;
246 struct device_node *np = pdev->dev.of_node;
247
248 gsw = devm_kzalloc(&pdev->dev, sizeof(struct mt7620_gsw), GFP_KERNEL);
249 if (!gsw)
250 return -ENOMEM;
251
252 gsw->base = devm_ioremap_resource(&pdev->dev, res);
253 if (IS_ERR(gsw->base))
254 return PTR_ERR(gsw->base);
255
256 gsw->dev = &pdev->dev;
257
258 of_property_read_string(np, "mediatek,port4", &port4);
259 if (port4 && !strcmp(port4, "ephy"))
260 gsw->port4 = PORT4_EPHY;
261 else if (port4 && !strcmp(port4, "gmac"))
262 gsw->port4 = PORT4_EXT;
263 else
264 gsw->port4 = PORT4_EPHY;
265
266 gsw->irq = platform_get_irq(pdev, 0);
267
268 platform_set_drvdata(pdev, gsw);
269
270 return 0;
271 }
272
273 static int mt7620_gsw_remove(struct platform_device *pdev)
274 {
275 platform_set_drvdata(pdev, NULL);
276
277 return 0;
278 }
279
280 static struct platform_driver gsw_driver = {
281 .probe = mt7620_gsw_probe,
282 .remove = mt7620_gsw_remove,
283 .driver = {
284 .name = "mt7620-gsw",
285 .owner = THIS_MODULE,
286 .of_match_table = mediatek_gsw_match,
287 },
288 };
289
290 module_platform_driver(gsw_driver);
291
292 MODULE_LICENSE("GPL");
293 MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
294 MODULE_DESCRIPTION("GBit switch driver for Mediatek MT7620 SoC");
295 MODULE_VERSION(MTK_FE_DRV_VERSION);