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
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.
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>
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>
22 #include <ralink_regs.h>
24 #include "mtk_eth_soc.h"
25 #include "gsw_mt7620.h"
27 void mtk_switch_w32(struct mt7620_gsw
*gsw
, u32 val
, unsigned reg
)
29 iowrite32(val
, gsw
->base
+ reg
);
32 u32
mtk_switch_r32(struct mt7620_gsw
*gsw
, unsigned reg
)
34 return ioread32(gsw
->base
+ reg
);
37 static irqreturn_t
gsw_interrupt_mt7620(int irq
, void *_priv
)
39 struct fe_priv
*priv
= (struct fe_priv
*)_priv
;
40 struct mt7620_gsw
*gsw
= (struct mt7620_gsw
*)priv
->soc
->swpriv
;
42 int i
, max
= (gsw
->port4
== PORT4_EPHY
) ? (4) : (3);
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;
50 if (link
!= priv
->link
[i
])
51 mt7620_print_link_state(priv
, i
, link
,
57 mt7620_handle_carrier(priv
);
58 mtk_switch_w32(gsw
, status
, GSW_REG_ISR
);
63 static int mt7620_mdio_mode(struct device_node
*eth_node
)
65 struct device_node
*phy_node
, *mdiobus_node
;
69 mdiobus_node
= of_get_child_by_name(eth_node
, "mdio-bus");
72 if (of_property_read_bool(mdiobus_node
, "mediatek,mdio-mode"))
75 for_each_child_of_node(mdiobus_node
, phy_node
) {
76 id
= of_get_property(phy_node
, "reg", NULL
);
77 if (id
&& (be32_to_cpu(*id
) == 0x1f))
81 of_node_put(mdiobus_node
);
87 static void mt7620_hw_init(struct mt7620_gsw
*gsw
, int mdio_mode
)
91 u32 is_BGA
= (rt_sysc_r32(0x0c) >> 16) & 1;
93 rt_sysc_w32(rt_sysc_r32(SYSC_REG_CFG1
) | BIT(8), SYSC_REG_CFG1
);
94 mtk_switch_w32(gsw
, mtk_switch_r32(gsw
, GSW_REG_CKGCR
) & ~(0x3 << 4), GSW_REG_CKGCR
);
96 /* Enable MIB stats */
97 mtk_switch_w32(gsw
, mtk_switch_r32(gsw
, GSW_REG_MIB_CNT_EN
) | (1 << 1), GSW_REG_MIB_CNT_EN
);
102 /* turn off ephy and set phy base addr to 12 */
103 mtk_switch_w32(gsw
, mtk_switch_r32(gsw
, GSW_REG_GPC1
) |
104 (0x1f << 24) | (0xc << 16),
107 /* set MT7530 central align */
108 val
= mt7530_mdio_r32(gsw
, 0x7830);
111 mt7530_mdio_w32(gsw
, 0x7830, val
);
113 val
= mt7530_mdio_r32(gsw
, 0x7a40);
115 mt7530_mdio_w32(gsw
, 0x7a40, val
);
117 mt7530_mdio_w32(gsw
, 0x7a78, 0x855);
120 if (gsw
->ephy_base
) {
121 /* set phy base addr to ephy_base */
122 mtk_switch_w32(gsw
, mtk_switch_r32(gsw
, GSW_REG_GPC1
) |
123 (gsw
->ephy_base
<< 16),
125 fe_reset(BIT(24)); /* Resets the Ethernet PHY block. */
129 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 31, 0x4000);
131 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 17, 0x7444);
133 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 19, 0x0114);
135 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 19, 0x0117);
137 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 22, 0x10cf);
138 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 25, 0x6212);
139 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 26, 0x0777);
140 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 29, 0x4000);
141 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 28, 0xc077);
142 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 24, 0x0000);
145 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 31, 0x3000);
146 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 17, 0x4838);
149 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 31, 0x2000);
151 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 21, 0x0515);
152 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 22, 0x0053);
153 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 23, 0x00bf);
154 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 24, 0x0aaf);
155 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 25, 0x0fad);
156 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 26, 0x0fc1);
158 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 21, 0x0517);
159 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 22, 0x0fd2);
160 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 23, 0x00bf);
161 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 24, 0x0aab);
162 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 25, 0x00ae);
163 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 26, 0x0fff);
166 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 31, 0x1000);
167 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 17, 0xe7f8);
169 /* turn on all PHYs */
170 for (i
= 0; i
<= 4; i
++) {
171 val
= _mt7620_mii_read(gsw
, gsw
->ephy_base
+ i
, 0);
173 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ i
, 0, val
);
178 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 31, 0x8000);
179 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 0, 30, 0xa000);
180 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 30, 0xa000);
181 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 2, 30, 0xa000);
182 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 3, 30, 0xa000);
184 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 0, 4, 0x05e1);
185 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 4, 0x05e1);
186 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 2, 4, 0x05e1);
187 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 3, 4, 0x05e1);
190 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 31, 0xa000);
191 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 0, 16, 0x1111);
192 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 16, 0x1010);
193 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 2, 16, 0x1515);
194 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 3, 16, 0x0f0f);
196 /* CPU Port6 Force Link 1G, FC ON */
197 mtk_switch_w32(gsw
, 0x5e33b, GSW_REG_PORT_PMCR(6));
199 /* Set Port 6 as CPU Port */
200 mtk_switch_w32(gsw
, 0x7f7f7fe0, 0x0010);
203 if (gsw
->port4
== PORT4_EPHY
) {
204 u32 val
= rt_sysc_r32(SYSC_REG_CFG1
);
207 rt_sysc_w32(val
, SYSC_REG_CFG1
);
208 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 4, 30, 0xa000);
209 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 4, 4, 0x05e1);
210 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 4, 16, 0x1313);
211 pr_info("gsw: setting port4 to ephy mode\n");
212 } else if (!mdio_mode
) {
213 u32 val
= rt_sysc_r32(SYSC_REG_CFG1
);
216 rt_sysc_w32(val
, SYSC_REG_CFG1
);
217 pr_info("gsw: setting port4 to gmac mode\n");
221 static const struct of_device_id mediatek_gsw_match
[] = {
222 { .compatible
= "mediatek,mt7620-gsw" },
225 MODULE_DEVICE_TABLE(of
, mediatek_gsw_match
);
227 int mtk_gsw_init(struct fe_priv
*priv
)
229 struct device_node
*np
= priv
->switch_np
;
230 struct platform_device
*pdev
= of_find_device_by_node(np
);
231 struct mt7620_gsw
*gsw
;
236 if (!of_device_is_compatible(np
, mediatek_gsw_match
->compatible
))
239 gsw
= platform_get_drvdata(pdev
);
240 priv
->soc
->swpriv
= gsw
;
242 mt7620_hw_init(gsw
, mt7620_mdio_mode(priv
->dev
->of_node
));
245 request_irq(gsw
->irq
, gsw_interrupt_mt7620
, 0,
247 mtk_switch_w32(gsw
, ~PORT_IRQ_ST_CHG
, GSW_REG_IMR
);
253 static int mt7620_gsw_probe(struct platform_device
*pdev
)
255 struct resource
*res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
256 const char *port4
= NULL
;
257 struct mt7620_gsw
*gsw
;
258 struct device_node
*np
= pdev
->dev
.of_node
;
261 gsw
= devm_kzalloc(&pdev
->dev
, sizeof(struct mt7620_gsw
), GFP_KERNEL
);
265 gsw
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
266 if (IS_ERR(gsw
->base
))
267 return PTR_ERR(gsw
->base
);
269 gsw
->dev
= &pdev
->dev
;
271 of_property_read_string(np
, "mediatek,port4", &port4
);
272 if (port4
&& !strcmp(port4
, "ephy"))
273 gsw
->port4
= PORT4_EPHY
;
274 else if (port4
&& !strcmp(port4
, "gmac"))
275 gsw
->port4
= PORT4_EXT
;
277 gsw
->port4
= PORT4_EPHY
;
279 if (of_property_read_u16(np
, "mediatek,ephy-base-address", &val
) == 0)
280 gsw
->ephy_base
= val
;
284 gsw
->irq
= platform_get_irq(pdev
, 0);
286 platform_set_drvdata(pdev
, gsw
);
291 static int mt7620_gsw_remove(struct platform_device
*pdev
)
293 platform_set_drvdata(pdev
, NULL
);
298 static struct platform_driver gsw_driver
= {
299 .probe
= mt7620_gsw_probe
,
300 .remove
= mt7620_gsw_remove
,
302 .name
= "mt7620-gsw",
303 .owner
= THIS_MODULE
,
304 .of_match_table
= mediatek_gsw_match
,
308 module_platform_driver(gsw_driver
);
310 MODULE_LICENSE("GPL");
311 MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
312 MODULE_DESCRIPTION("GBit switch driver for Mediatek MT7620 SoC");
313 MODULE_VERSION(MTK_FE_DRV_VERSION
);