a80c940ec6d31cbe73397ae445ad96a27fa27aeb
[openwrt.git] / target / linux / ramips / patches-4.4 / 0506-net-next-mediatek-add-support-for-rt3050.patch
1 From a3555658ce5dd97df3dc225289b92800da9d38ba Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Mon, 14 Dec 2015 21:28:51 +0100
4 Subject: [PATCH 506/513] net-next: mediatek: add support for rt3050
5
6 Add support for SoCs from the rt3050 family. This include rt3050, rt3052,
7 rt3352 and rt5350. These all have a builtin 5 port 100mbit switch. This patch
8 includes rudimentary code to power up the switch. There are a lot of magic
9 values that get written to the switch and the internal phys. These values
10 come straight from the SDK driver and we do not know the meaning of most of
11 them.
12
13 Signed-off-by: John Crispin <blogic@openwrt.org>
14 Signed-off-by: Felix Fietkau <nbd@openwrt.org>
15 Signed-off-by: Michael Lee <igvtee@gmail.com>
16 ---
17  drivers/net/ethernet/mediatek/esw_rt3050.c |   18 +---
18  drivers/net/ethernet/mediatek/soc_rt3050.c |  158 ++++++++++++++++++++++++++++
19  2 files changed, 159 insertions(+), 17 deletions(-)
20  create mode 100644 drivers/net/ethernet/mediatek/soc_rt3050.c
21
22 --- a/drivers/net/ethernet/mediatek/esw_rt3050.c
23 +++ b/drivers/net/ethernet/mediatek/esw_rt3050.c
24 @@ -14,27 +14,11 @@
25  
26  #include <linux/module.h>
27  #include <linux/kernel.h>
28 -#include <linux/types.h>
29 -#include <linux/dma-mapping.h>
30 -#include <linux/init.h>
31 -#include <linux/skbuff.h>
32 -#include <linux/etherdevice.h>
33 -#include <linux/ethtool.h>
34  #include <linux/platform_device.h>
35 -#include <linux/of_device.h>
36 -#include <linux/clk.h>
37 -#include <linux/of_net.h>
38 -#include <linux/of_mdio.h>
39 -
40  #include <asm/mach-ralink/ralink_regs.h>
41  
42  #include "mtk_eth_soc.h"
43  
44 -#include <linux/ioport.h>
45 -#include <linux/mii.h>
46 -
47 -#include <ralink_regs.h>
48 -
49  /* HW limitations for this switch:
50   * - No large frame support (PKT_MAX_LEN at most 1536)
51   * - Can't have untagged vlan and tagged vlan on one port at the same time,
52 @@ -559,7 +543,7 @@ static irqreturn_t esw_interrupt(int irq
53  
54  static int esw_probe(struct platform_device *pdev)
55  {
56 -       struct resource *res = platform_get_resource(p, IORESOURCE_MEM, 0);
57 +       struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
58         struct device_node *np = pdev->dev.of_node;
59         const __be32 *port_map, *reg_init;
60         struct rt305x_esw *esw;
61 @@ -629,12 +613,9 @@ static struct platform_driver esw_driver
62         },
63  };
64  
65 -int __init mtk_switch_init(void)
66 -{
67 -       return platform_driver_register(&esw_driver);
68 -}
69 +module_platform_driver(esw_driver);
70  
71 -void mtk_switch_exit(void)
72 -{
73 -       platform_driver_unregister(&esw_driver);
74 -}
75 +MODULE_LICENSE("GPL");
76 +MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
77 +MODULE_DESCRIPTION("Switch driver for RT305X SoC");
78 +MODULE_VERSION(MTK_FE_DRV_VERSION);
79 --- /dev/null
80 +++ b/drivers/net/ethernet/mediatek/soc_rt3050.c
81 @@ -0,0 +1,158 @@
82 +/*   This program is free software; you can redistribute it and/or modify
83 + *   it under the terms of the GNU General Public License as published by
84 + *   the Free Software Foundation; version 2 of the License
85 + *
86 + *   This program is distributed in the hope that it will be useful,
87 + *   but WITHOUT ANY WARRANTY; without even the implied warranty of
88 + *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
89 + *   GNU General Public License for more details.
90 + *
91 + *   Copyright (C) 2009-2015 John Crispin <blogic@openwrt.org>
92 + *   Copyright (C) 2009-2015 Felix Fietkau <nbd@openwrt.org>
93 + *   Copyright (C) 2013-2015 Michael Lee <igvtee@gmail.com>
94 + */
95 +
96 +#include <linux/module.h>
97 +
98 +#include <asm/mach-ralink/ralink_regs.h>
99 +
100 +#include "mtk_eth_soc.h"
101 +#include "mdio_rt2880.h"
102 +
103 +#define RT305X_RESET_FE         BIT(21)
104 +#define RT305X_RESET_ESW        BIT(23)
105 +
106 +static const u16 rt5350_reg_table[FE_REG_COUNT] = {
107 +       [FE_REG_PDMA_GLO_CFG] = RT5350_PDMA_GLO_CFG,
108 +       [FE_REG_PDMA_RST_CFG] = RT5350_PDMA_RST_CFG,
109 +       [FE_REG_DLY_INT_CFG] = RT5350_DLY_INT_CFG,
110 +       [FE_REG_TX_BASE_PTR0] = RT5350_TX_BASE_PTR0,
111 +       [FE_REG_TX_MAX_CNT0] = RT5350_TX_MAX_CNT0,
112 +       [FE_REG_TX_CTX_IDX0] = RT5350_TX_CTX_IDX0,
113 +       [FE_REG_TX_DTX_IDX0] = RT5350_TX_DTX_IDX0,
114 +       [FE_REG_RX_BASE_PTR0] = RT5350_RX_BASE_PTR0,
115 +       [FE_REG_RX_MAX_CNT0] = RT5350_RX_MAX_CNT0,
116 +       [FE_REG_RX_CALC_IDX0] = RT5350_RX_CALC_IDX0,
117 +       [FE_REG_RX_DRX_IDX0] = RT5350_RX_DRX_IDX0,
118 +       [FE_REG_FE_INT_ENABLE] = RT5350_FE_INT_ENABLE,
119 +       [FE_REG_FE_INT_STATUS] = RT5350_FE_INT_STATUS,
120 +       [FE_REG_FE_RST_GL] = 0,
121 +       [FE_REG_FE_DMA_VID_BASE] = 0,
122 +};
123 +
124 +static void rt305x_init_data(struct fe_soc_data *data,
125 +                            struct net_device *netdev)
126 +{
127 +       struct fe_priv *priv = netdev_priv(netdev);
128 +
129 +       priv->flags = FE_FLAG_PADDING_64B | FE_FLAG_PADDING_BUG |
130 +               FE_FLAG_CALIBRATE_CLK | FE_FLAG_HAS_SWITCH;
131 +       netdev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM |
132 +               NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX;
133 +}
134 +
135 +static int rt3050_fwd_config(struct fe_priv *priv)
136 +{
137 +       int ret;
138 +
139 +       if (ralink_soc != RT305X_SOC_RT3052) {
140 +               ret = fe_set_clock_cycle(priv);
141 +               if (ret)
142 +                       return ret;
143 +       }
144 +
145 +       fe_fwd_config(priv);
146 +       if (ralink_soc != RT305X_SOC_RT3352)
147 +               fe_w32(FE_PSE_FQFC_CFG_INIT, FE_PSE_FQ_CFG);
148 +       fe_csum_config(priv);
149 +
150 +       return 0;
151 +}
152 +
153 +static void rt305x_fe_reset(void)
154 +{
155 +       fe_reset(RT305X_RESET_FE);
156 +}
157 +
158 +static void rt5350_init_data(struct fe_soc_data *data,
159 +                            struct net_device *netdev)
160 +{
161 +       struct fe_priv *priv = netdev_priv(netdev);
162 +
163 +       priv->flags = FE_FLAG_HAS_SWITCH;
164 +       netdev->hw_features = NETIF_F_SG | NETIF_F_RXCSUM;
165 +}
166 +
167 +static void rt5350_set_mac(struct fe_priv *priv, unsigned char *mac)
168 +{
169 +       unsigned long flags;
170 +
171 +       spin_lock_irqsave(&priv->page_lock, flags);
172 +       fe_w32((mac[0] << 8) | mac[1], RT5350_SDM_MAC_ADRH);
173 +       fe_w32((mac[2] << 24) | (mac[3] << 16) | (mac[4] << 8) | mac[5],
174 +              RT5350_SDM_MAC_ADRL);
175 +       spin_unlock_irqrestore(&priv->page_lock, flags);
176 +}
177 +
178 +static void rt5350_rxcsum_config(bool enable)
179 +{
180 +       if (enable)
181 +               fe_w32(fe_r32(RT5350_SDM_CFG) | (RT5350_SDM_ICS_EN |
182 +                               RT5350_SDM_TCS_EN | RT5350_SDM_UCS_EN),
183 +                               RT5350_SDM_CFG);
184 +       else
185 +               fe_w32(fe_r32(RT5350_SDM_CFG) & ~(RT5350_SDM_ICS_EN |
186 +                               RT5350_SDM_TCS_EN | RT5350_SDM_UCS_EN),
187 +                               RT5350_SDM_CFG);
188 +}
189 +
190 +static int rt5350_fwd_config(struct fe_priv *priv)
191 +{
192 +       struct net_device *dev = priv_netdev(priv);
193 +
194 +       rt5350_rxcsum_config((dev->features & NETIF_F_RXCSUM));
195 +
196 +       return 0;
197 +}
198 +
199 +static void rt5350_tx_dma(struct fe_tx_dma *txd)
200 +{
201 +       txd->txd4 = 0;
202 +}
203 +
204 +static void rt5350_fe_reset(void)
205 +{
206 +       fe_reset(RT305X_RESET_FE | RT305X_RESET_ESW);
207 +}
208 +
209 +static struct fe_soc_data rt3050_data = {
210 +       .init_data = rt305x_init_data,
211 +       .reset_fe = rt305x_fe_reset,
212 +       .fwd_config = rt3050_fwd_config,
213 +       .pdma_glo_cfg = FE_PDMA_SIZE_8DWORDS,
214 +       .checksum_bit = RX_DMA_L4VALID,
215 +       .rx_int = FE_RX_DONE_INT,
216 +       .tx_int = FE_TX_DONE_INT,
217 +       .status_int = FE_CNT_GDM_AF,
218 +};
219 +
220 +static struct fe_soc_data rt5350_data = {
221 +       .init_data = rt5350_init_data,
222 +       .reg_table = rt5350_reg_table,
223 +       .reset_fe = rt5350_fe_reset,
224 +       .set_mac = rt5350_set_mac,
225 +       .fwd_config = rt5350_fwd_config,
226 +       .tx_dma = rt5350_tx_dma,
227 +       .pdma_glo_cfg = FE_PDMA_SIZE_8DWORDS,
228 +       .checksum_bit = RX_DMA_L4VALID,
229 +       .rx_int = RT5350_RX_DONE_INT,
230 +       .tx_int = RT5350_TX_DONE_INT,
231 +};
232 +
233 +const struct of_device_id of_fe_match[] = {
234 +       { .compatible = "ralink,rt3050-eth", .data = &rt3050_data },
235 +       { .compatible = "ralink,rt5350-eth", .data = &rt5350_data },
236 +       {},
237 +};
238 +
239 +MODULE_DEVICE_TABLE(of, of_fe_match);