2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/device.h>
18 #include <linux/delay.h>
19 #include <linux/of_mdio.h>
20 #include <linux/of_platform.h>
21 #include <linux/of_gpio.h>
24 #include "./rtl8367c/include/rtk_switch.h"
25 #include "./rtl8367c/include/port.h"
26 #include "./rtl8367c/include/vlan.h"
27 #include "./rtl8367c/include/rtl8367c_asicdrv_port.h"
35 static struct rtk_gsw
*_gsw
;
37 extern int gsw_debug_proc_init(void);
38 extern void gsw_debug_proc_exit(void);
40 #ifdef CONFIG_SWCONFIG
41 extern int rtl8367s_swconfig_init( void (*reset_func
)(void) );
44 /*mii_mgr_read/mii_mgr_write is the callback API for rtl8367 driver*/
45 unsigned int mii_mgr_read(unsigned int phy_addr
,unsigned int phy_register
,unsigned int *read_data
)
47 struct mii_bus
*bus
= _gsw
->bus
;
49 mutex_lock_nested(&bus
->mdio_lock
, MDIO_MUTEX_NESTED
);
51 *read_data
= bus
->read(bus
, phy_addr
, phy_register
);
53 mutex_unlock(&bus
->mdio_lock
);
58 unsigned int mii_mgr_write(unsigned int phy_addr
,unsigned int phy_register
,unsigned int write_data
)
60 struct mii_bus
*bus
= _gsw
->bus
;
62 mutex_lock_nested(&bus
->mdio_lock
, MDIO_MUTEX_NESTED
);
64 bus
->write(bus
, phy_addr
, phy_register
, write_data
);
66 mutex_unlock(&bus
->mdio_lock
);
71 static int rtl8367s_hw_reset(void)
73 struct rtk_gsw
*gsw
= _gsw
;
76 if (gsw
->reset_pin
< 0)
79 ret
= devm_gpio_request(gsw
->dev
, gsw
->reset_pin
, "mediatek,reset-pin");
82 printk("fail to devm_gpio_request\n");
84 gpio_direction_output(gsw
->reset_pin
, 0);
86 usleep_range(1000, 1100);
88 gpio_set_value(gsw
->reset_pin
, 1);
92 devm_gpio_free(gsw
->dev
, gsw
->reset_pin
);
98 static int rtl8367s_vlan_config(int want_at_p0
)
100 rtk_vlan_cfg_t vlan1
, vlan2
;
102 /* Set LAN/WAN VLAN partition */
103 memset(&vlan1
, 0x00, sizeof(rtk_vlan_cfg_t
));
105 RTK_PORTMASK_PORT_SET(vlan1
.mbr
, EXT_PORT0
);
106 RTK_PORTMASK_PORT_SET(vlan1
.mbr
, UTP_PORT1
);
107 RTK_PORTMASK_PORT_SET(vlan1
.mbr
, UTP_PORT2
);
108 RTK_PORTMASK_PORT_SET(vlan1
.mbr
, UTP_PORT3
);
109 RTK_PORTMASK_PORT_SET(vlan1
.untag
, EXT_PORT0
);
110 RTK_PORTMASK_PORT_SET(vlan1
.untag
, UTP_PORT1
);
111 RTK_PORTMASK_PORT_SET(vlan1
.untag
, UTP_PORT2
);
112 RTK_PORTMASK_PORT_SET(vlan1
.untag
, UTP_PORT3
);
115 RTK_PORTMASK_PORT_SET(vlan1
.mbr
, UTP_PORT4
);
116 RTK_PORTMASK_PORT_SET(vlan1
.untag
, UTP_PORT4
);
118 RTK_PORTMASK_PORT_SET(vlan1
.mbr
, UTP_PORT0
);
119 RTK_PORTMASK_PORT_SET(vlan1
.untag
, UTP_PORT0
);
124 rtk_vlan_set(1, &vlan1
);
126 memset(&vlan2
, 0x00, sizeof(rtk_vlan_cfg_t
));
128 RTK_PORTMASK_PORT_SET(vlan2
.mbr
, EXT_PORT1
);
129 RTK_PORTMASK_PORT_SET(vlan2
.untag
, EXT_PORT1
);
132 RTK_PORTMASK_PORT_SET(vlan2
.mbr
, UTP_PORT0
);
133 RTK_PORTMASK_PORT_SET(vlan2
.untag
, UTP_PORT0
);
135 RTK_PORTMASK_PORT_SET(vlan2
.mbr
, UTP_PORT4
);
136 RTK_PORTMASK_PORT_SET(vlan2
.untag
, UTP_PORT4
);
140 rtk_vlan_set(2, &vlan2
);
142 rtk_vlan_portPvid_set(EXT_PORT0
, 1, 0);
143 rtk_vlan_portPvid_set(UTP_PORT1
, 1, 0);
144 rtk_vlan_portPvid_set(UTP_PORT2
, 1, 0);
145 rtk_vlan_portPvid_set(UTP_PORT3
, 1, 0);
146 rtk_vlan_portPvid_set(EXT_PORT1
, 2, 0);
149 rtk_vlan_portPvid_set(UTP_PORT0
, 2, 0);
150 rtk_vlan_portPvid_set(UTP_PORT4
, 1, 0);
152 rtk_vlan_portPvid_set(UTP_PORT0
, 1, 0);
153 rtk_vlan_portPvid_set(UTP_PORT4
, 2, 0);
159 static int rtl8367s_hw_init(void)
164 if(rtk_switch_init())
169 if (rtk_vlan_reset())
178 static void set_rtl8367s_sgmii(void)
180 rtk_port_mac_ability_t mac_cfg
;
183 mode
= MODE_EXT_HSGMII
;
184 mac_cfg
.forcemode
= MAC_FORCE
;
185 mac_cfg
.speed
= PORT_SPEED_2500M
;
186 mac_cfg
.duplex
= PORT_FULL_DUPLEX
;
187 mac_cfg
.link
= PORT_LINKUP
;
188 mac_cfg
.nway
= DISABLED
;
189 mac_cfg
.txpause
= ENABLED
;
190 mac_cfg
.rxpause
= ENABLED
;
191 rtk_port_macForceLinkExt_set(EXT_PORT0
, mode
, &mac_cfg
);
192 rtk_port_sgmiiNway_set(EXT_PORT0
, DISABLED
);
193 rtk_port_phyEnableAll_set(ENABLED
);
197 static void set_rtl8367s_rgmii(void)
199 rtk_port_mac_ability_t mac_cfg
;
202 mode
= MODE_EXT_RGMII
;
203 mac_cfg
.forcemode
= MAC_FORCE
;
204 mac_cfg
.speed
= PORT_SPEED_1000M
;
205 mac_cfg
.duplex
= PORT_FULL_DUPLEX
;
206 mac_cfg
.link
= PORT_LINKUP
;
207 mac_cfg
.nway
= DISABLED
;
208 mac_cfg
.txpause
= ENABLED
;
209 mac_cfg
.rxpause
= ENABLED
;
210 rtk_port_macForceLinkExt_set(EXT_PORT1
, mode
, &mac_cfg
);
211 rtk_port_rgmiiDelayExt_set(EXT_PORT1
, 1, 3);
212 rtk_port_phyEnableAll_set(ENABLED
);
219 set_rtl8367s_sgmii();
220 set_rtl8367s_rgmii();
223 // bleow are platform driver
224 static const struct of_device_id rtk_gsw_match
[] = {
225 { .compatible
= "mediatek,rtk-gsw" },
229 MODULE_DEVICE_TABLE(of
, rtk_gsw_match
);
231 static int rtk_gsw_probe(struct platform_device
*pdev
)
233 struct device_node
*np
= pdev
->dev
.of_node
;
234 struct device_node
*mdio
;
235 struct mii_bus
*mdio_bus
;
239 mdio
= of_parse_phandle(np
, "mediatek,mdio", 0);
244 mdio_bus
= of_mdio_find_bus(mdio
);
247 return -EPROBE_DEFER
;
249 gsw
= devm_kzalloc(&pdev
->dev
, sizeof(struct rtk_gsw
), GFP_KERNEL
);
254 gsw
->dev
= &pdev
->dev
;
258 gsw
->reset_pin
= of_get_named_gpio(np
, "mediatek,reset-pin", 0);
264 //init default vlan or init swocnfig
265 if(!of_property_read_string(pdev
->dev
.of_node
,
266 "mediatek,port_map", &pm
)) {
268 if (!strcasecmp(pm
, "wllll"))
269 rtl8367s_vlan_config(1);
271 rtl8367s_vlan_config(0);
274 #ifdef CONFIG_SWCONFIG
275 rtl8367s_swconfig_init(&init_gsw
);
277 rtl8367s_vlan_config(0);
281 gsw_debug_proc_init();
283 platform_set_drvdata(pdev
, gsw
);
289 static int rtk_gsw_remove(struct platform_device
*pdev
)
291 platform_set_drvdata(pdev
, NULL
);
292 gsw_debug_proc_exit();
297 static struct platform_driver gsw_driver
= {
298 .probe
= rtk_gsw_probe
,
299 .remove
= rtk_gsw_remove
,
302 .owner
= THIS_MODULE
,
303 .of_match_table
= rtk_gsw_match
,
307 module_platform_driver(gsw_driver
);
309 MODULE_LICENSE("GPL");
310 MODULE_AUTHOR("Mark Lee <marklee0201@gmail.com>");
311 MODULE_DESCRIPTION("rtl8367c switch driver for MT7622");