ramips: add v4.9 support
[librecmc/librecmc.git] / target / linux / ramips / files-4.9 / drivers / net / ethernet / mtk / soc_rt3050.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
17 #include <asm/mach-ralink/ralink_regs.h>
18
19 #include "mtk_eth_soc.h"
20 #include "mdio_rt2880.h"
21
22 #define RT305X_RESET_FE         BIT(21)
23 #define RT305X_RESET_ESW        BIT(23)
24
25 static const u16 rt5350_reg_table[FE_REG_COUNT] = {
26         [FE_REG_PDMA_GLO_CFG] = RT5350_PDMA_GLO_CFG,
27         [FE_REG_PDMA_RST_CFG] = RT5350_PDMA_RST_CFG,
28         [FE_REG_DLY_INT_CFG] = RT5350_DLY_INT_CFG,
29         [FE_REG_TX_BASE_PTR0] = RT5350_TX_BASE_PTR0,
30         [FE_REG_TX_MAX_CNT0] = RT5350_TX_MAX_CNT0,
31         [FE_REG_TX_CTX_IDX0] = RT5350_TX_CTX_IDX0,
32         [FE_REG_TX_DTX_IDX0] = RT5350_TX_DTX_IDX0,
33         [FE_REG_RX_BASE_PTR0] = RT5350_RX_BASE_PTR0,
34         [FE_REG_RX_MAX_CNT0] = RT5350_RX_MAX_CNT0,
35         [FE_REG_RX_CALC_IDX0] = RT5350_RX_CALC_IDX0,
36         [FE_REG_RX_DRX_IDX0] = RT5350_RX_DRX_IDX0,
37         [FE_REG_FE_INT_ENABLE] = RT5350_FE_INT_ENABLE,
38         [FE_REG_FE_INT_STATUS] = RT5350_FE_INT_STATUS,
39         [FE_REG_FE_RST_GL] = 0,
40         [FE_REG_FE_DMA_VID_BASE] = 0,
41 };
42
43 static void rt305x_init_data(struct fe_soc_data *data,
44                              struct net_device *netdev)
45 {
46         struct fe_priv *priv = netdev_priv(netdev);
47
48         priv->flags = FE_FLAG_PADDING_64B | FE_FLAG_PADDING_BUG |
49                 FE_FLAG_CALIBRATE_CLK | FE_FLAG_HAS_SWITCH;
50         netdev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM |
51                 NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_TX;
52 }
53
54 static int rt3050_fwd_config(struct fe_priv *priv)
55 {
56         int ret;
57
58         if (ralink_soc != RT305X_SOC_RT3052) {
59                 ret = fe_set_clock_cycle(priv);
60                 if (ret)
61                         return ret;
62         }
63
64         fe_fwd_config(priv);
65         if (ralink_soc != RT305X_SOC_RT3352)
66                 fe_w32(FE_PSE_FQFC_CFG_INIT, FE_PSE_FQ_CFG);
67         fe_csum_config(priv);
68
69         return 0;
70 }
71
72 static void rt305x_fe_reset(void)
73 {
74         fe_reset(RT305X_RESET_FE);
75 }
76
77 static void rt5350_init_data(struct fe_soc_data *data,
78                              struct net_device *netdev)
79 {
80         struct fe_priv *priv = netdev_priv(netdev);
81
82         priv->flags = FE_FLAG_HAS_SWITCH;
83         netdev->hw_features = NETIF_F_SG | NETIF_F_RXCSUM;
84 }
85
86 static void rt5350_set_mac(struct fe_priv *priv, unsigned char *mac)
87 {
88         unsigned long flags;
89
90         spin_lock_irqsave(&priv->page_lock, flags);
91         fe_w32((mac[0] << 8) | mac[1], RT5350_SDM_MAC_ADRH);
92         fe_w32((mac[2] << 24) | (mac[3] << 16) | (mac[4] << 8) | mac[5],
93                RT5350_SDM_MAC_ADRL);
94         spin_unlock_irqrestore(&priv->page_lock, flags);
95 }
96
97 static void rt5350_rxcsum_config(bool enable)
98 {
99         if (enable)
100                 fe_w32(fe_r32(RT5350_SDM_CFG) | (RT5350_SDM_ICS_EN |
101                                 RT5350_SDM_TCS_EN | RT5350_SDM_UCS_EN),
102                                 RT5350_SDM_CFG);
103         else
104                 fe_w32(fe_r32(RT5350_SDM_CFG) & ~(RT5350_SDM_ICS_EN |
105                                 RT5350_SDM_TCS_EN | RT5350_SDM_UCS_EN),
106                                 RT5350_SDM_CFG);
107 }
108
109 static int rt5350_fwd_config(struct fe_priv *priv)
110 {
111         struct net_device *dev = priv_netdev(priv);
112
113         rt5350_rxcsum_config((dev->features & NETIF_F_RXCSUM));
114
115         return 0;
116 }
117
118 static void rt5350_tx_dma(struct fe_tx_dma *txd)
119 {
120         txd->txd4 = 0;
121 }
122
123 static void rt5350_fe_reset(void)
124 {
125         fe_reset(RT305X_RESET_FE | RT305X_RESET_ESW);
126 }
127
128 static struct fe_soc_data rt3050_data = {
129         .init_data = rt305x_init_data,
130         .reset_fe = rt305x_fe_reset,
131         .fwd_config = rt3050_fwd_config,
132         .pdma_glo_cfg = FE_PDMA_SIZE_8DWORDS,
133         .checksum_bit = RX_DMA_L4VALID,
134         .rx_int = FE_RX_DONE_INT,
135         .tx_int = FE_TX_DONE_INT,
136         .status_int = FE_CNT_GDM_AF,
137 };
138
139 static struct fe_soc_data rt5350_data = {
140         .init_data = rt5350_init_data,
141         .reg_table = rt5350_reg_table,
142         .reset_fe = rt5350_fe_reset,
143         .set_mac = rt5350_set_mac,
144         .fwd_config = rt5350_fwd_config,
145         .tx_dma = rt5350_tx_dma,
146         .pdma_glo_cfg = FE_PDMA_SIZE_8DWORDS,
147         .checksum_bit = RX_DMA_L4VALID,
148         .rx_int = RT5350_RX_DONE_INT,
149         .tx_int = RT5350_TX_DONE_INT,
150 };
151
152 const struct of_device_id of_fe_match[] = {
153         { .compatible = "ralink,rt3050-eth", .data = &rt3050_data },
154         { .compatible = "ralink,rt5350-eth", .data = &rt5350_data },
155         {},
156 };
157
158 MODULE_DEVICE_TABLE(of, of_fe_match);