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/mii.h>
17 #include <linux/kernel.h>
18 #include <linux/types.h>
19 #include <linux/platform_device.h>
20 #include <linux/of_device.h>
21 #include <linux/of_irq.h>
23 #include <ralink_regs.h>
25 #include "mtk_eth_soc.h"
26 #include "gsw_mt7620.h"
28 void mtk_switch_w32(struct mt7620_gsw
*gsw
, u32 val
, unsigned reg
)
30 iowrite32(val
, gsw
->base
+ reg
);
33 u32
mtk_switch_r32(struct mt7620_gsw
*gsw
, unsigned reg
)
35 return ioread32(gsw
->base
+ reg
);
38 static irqreturn_t
gsw_interrupt_mt7620(int irq
, void *_priv
)
40 struct fe_priv
*priv
= (struct fe_priv
*)_priv
;
41 struct mt7620_gsw
*gsw
= (struct mt7620_gsw
*)priv
->soc
->swpriv
;
43 int i
, max
= (gsw
->port4_ephy
) ? (4) : (3);
45 status
= mtk_switch_r32(gsw
, GSW_REG_ISR
);
46 if (status
& PORT_IRQ_ST_CHG
)
47 for (i
= 0; i
<= max
; i
++) {
48 u32 status
= mtk_switch_r32(gsw
, GSW_REG_PORT_STATUS(i
));
49 int link
= status
& 0x1;
51 if (link
!= priv
->link
[i
])
52 mt7620_print_link_state(priv
, i
, link
,
58 mt7620_handle_carrier(priv
);
59 mtk_switch_w32(gsw
, status
, GSW_REG_ISR
);
64 static void mt7620_ephy_init(struct mt7620_gsw
*gsw
)
68 u32 is_BGA
= (rt_sysc_r32(SYSC_REG_CHIP_REV_ID
) >> 16) & 1;
70 if (gsw
->ephy_disable
) {
71 mtk_switch_w32(gsw
, mtk_switch_r32(gsw
, GSW_REG_GPC1
) |
72 (gsw
->ephy_base
<< 16) | (0x1f << 24),
75 pr_info("gsw: internal ephy disabled\n");
78 } else if (gsw
->ephy_base
) {
79 mtk_switch_w32(gsw
, mtk_switch_r32(gsw
, GSW_REG_GPC1
) |
80 (gsw
->ephy_base
<< 16),
82 fe_reset(MT7620A_RESET_EPHY
);
84 pr_info("gsw: ephy base address: %d\n", gsw
->ephy_base
);
88 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 31, 0x4000);
90 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 17, 0x7444);
92 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 19, 0x0114);
94 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 19, 0x0117);
96 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 22, 0x10cf);
97 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 25, 0x6212);
98 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 26, 0x0777);
99 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 29, 0x4000);
100 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 28, 0xc077);
101 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 24, 0x0000);
104 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 31, 0x3000);
105 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 17, 0x4838);
108 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 31, 0x2000);
110 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 21, 0x0515);
111 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 22, 0x0053);
112 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 23, 0x00bf);
113 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 24, 0x0aaf);
114 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 25, 0x0fad);
115 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 26, 0x0fc1);
117 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 21, 0x0517);
118 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 22, 0x0fd2);
119 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 23, 0x00bf);
120 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 24, 0x0aab);
121 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 25, 0x00ae);
122 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 26, 0x0fff);
125 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 31, 0x1000);
126 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 17, 0xe7f8);
128 /* turn on all PHYs */
129 for (i
= 0; i
<= 4; i
++) {
130 val
= _mt7620_mii_read(gsw
, gsw
->ephy_base
+ i
, MII_BMCR
);
132 val
|= BMCR_ANRESTART
| BMCR_ANENABLE
| BMCR_SPEED100
;
133 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ i
, MII_BMCR
, val
);
137 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 31, 0x8000);
138 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 0, 30, 0xa000);
139 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 30, 0xa000);
140 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 2, 30, 0xa000);
141 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 3, 30, 0xa000);
143 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 0, 4, 0x05e1);
144 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 4, 0x05e1);
145 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 2, 4, 0x05e1);
146 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 3, 4, 0x05e1);
149 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 31, 0xa000);
150 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 0, 16, 0x1111);
151 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 1, 16, 0x1010);
152 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 2, 16, 0x1515);
153 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 3, 16, 0x0f0f);
156 if (gsw
->port4_ephy
) {
157 val
= rt_sysc_r32(SYSC_REG_CFG1
);
160 rt_sysc_w32(val
, SYSC_REG_CFG1
);
161 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 4, 30, 0xa000);
162 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 4, 4, 0x05e1);
163 _mt7620_mii_write(gsw
, gsw
->ephy_base
+ 4, 16, 0x1313);
164 pr_info("gsw: setting port4 to ephy mode\n");
168 static void mt7620_mac_init(struct mt7620_gsw
*gsw
)
170 /* Internal ethernet requires PCIe RC mode */
171 rt_sysc_w32(rt_sysc_r32(SYSC_REG_CFG1
) | PCIE_RC_MODE
, SYSC_REG_CFG1
);
173 /* Keep Global Clocks on Idle traffic */
174 mtk_switch_w32(gsw
, mtk_switch_r32(gsw
, GSW_REG_CKGCR
) & ~(0x3 << 4), GSW_REG_CKGCR
);
176 /* Set Port 6 to Force Link 1G, Flow Control ON */
177 mtk_switch_w32(gsw
, 0x5e33b, GSW_REG_PORT_PMCR(6));
179 /* Set Port 6 as CPU Port */
180 mtk_switch_w32(gsw
, 0x7f7f7fe0, 0x0010);
182 /* Enable MIB stats */
183 mtk_switch_w32(gsw
, mtk_switch_r32(gsw
, GSW_REG_MIB_CNT_EN
) | (1 << 1), GSW_REG_MIB_CNT_EN
);
186 static const struct of_device_id mediatek_gsw_match
[] = {
187 { .compatible
= "mediatek,mt7620-gsw" },
190 MODULE_DEVICE_TABLE(of
, mediatek_gsw_match
);
192 int mtk_gsw_init(struct fe_priv
*priv
)
194 struct device_node
*eth_node
= priv
->dev
->of_node
;
195 struct device_node
*phy_node
, *mdiobus_node
;
196 struct device_node
*np
= priv
->switch_np
;
197 struct platform_device
*pdev
= of_find_device_by_node(np
);
198 struct mt7620_gsw
*gsw
;
205 if (!of_device_is_compatible(np
, mediatek_gsw_match
->compatible
))
208 gsw
= platform_get_drvdata(pdev
);
209 priv
->soc
->swpriv
= gsw
;
211 gsw
->ephy_disable
= of_property_read_bool(np
, "mediatek,ephy-disable");
213 mdiobus_node
= of_get_child_by_name(eth_node
, "mdio-bus");
215 for_each_child_of_node(mdiobus_node
, phy_node
) {
216 id
= of_get_property(phy_node
, "reg", NULL
);
217 if (id
&& (be32_to_cpu(*id
) == 0x1f))
218 gsw
->ephy_disable
= true;
221 of_node_put(mdiobus_node
);
224 gsw
->port4_ephy
= !of_property_read_bool(np
, "mediatek,port4-gmac");
226 if (of_property_read_u8(np
, "mediatek,ephy-base", &val
) == 0)
227 gsw
->ephy_base
= val
;
231 mt7620_mac_init(gsw
);
233 mt7620_ephy_init(gsw
);
236 request_irq(gsw
->irq
, gsw_interrupt_mt7620
, 0,
238 mtk_switch_w32(gsw
, ~PORT_IRQ_ST_CHG
, GSW_REG_IMR
);
244 static int mt7620_gsw_probe(struct platform_device
*pdev
)
246 struct resource
*res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
247 struct mt7620_gsw
*gsw
;
249 gsw
= devm_kzalloc(&pdev
->dev
, sizeof(struct mt7620_gsw
), GFP_KERNEL
);
253 gsw
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
254 if (IS_ERR(gsw
->base
))
255 return PTR_ERR(gsw
->base
);
257 gsw
->dev
= &pdev
->dev
;
259 gsw
->irq
= platform_get_irq(pdev
, 0);
261 platform_set_drvdata(pdev
, gsw
);
266 static int mt7620_gsw_remove(struct platform_device
*pdev
)
268 platform_set_drvdata(pdev
, NULL
);
273 static struct platform_driver gsw_driver
= {
274 .probe
= mt7620_gsw_probe
,
275 .remove
= mt7620_gsw_remove
,
277 .name
= "mt7620-gsw",
278 .owner
= THIS_MODULE
,
279 .of_match_table
= mediatek_gsw_match
,
283 module_platform_driver(gsw_driver
);
285 MODULE_LICENSE("GPL");
286 MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
287 MODULE_DESCRIPTION("GBit switch driver for Mediatek MT7620 SoC");
288 MODULE_VERSION(MTK_FE_DRV_VERSION
);