1 From f8c8f4bd2a13e0cc060c93812377373d436f7f02 Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Wed, 18 Nov 2015 03:13:05 +0100
4 Subject: [PATCH 505/513] net-next: mediatek: add support for rt2880
6 rt2880 is the oldest SoC with this core. It has a single gBit port that will
7 normally be attached to an external phy of switch. The patch also adds the
8 code required to drive the mdio bus.
10 Signed-off-by: John Crispin <blogic@openwrt.org>
11 Signed-off-by: Felix Fietkau <nbd@openwrt.org>
12 Signed-off-by: Michael Lee <igvtee@gmail.com>
14 drivers/net/ethernet/mediatek/mdio_rt2880.c | 222 +++++++++++++++++++++++++++
15 drivers/net/ethernet/mediatek/mdio_rt2880.h | 23 +++
16 drivers/net/ethernet/mediatek/soc_rt2880.c | 76 +++++++++
17 3 files changed, 321 insertions(+)
18 create mode 100644 drivers/net/ethernet/mediatek/mdio_rt2880.c
19 create mode 100644 drivers/net/ethernet/mediatek/mdio_rt2880.h
20 create mode 100644 drivers/net/ethernet/mediatek/soc_rt2880.c
22 diff --git a/drivers/net/ethernet/mediatek/mdio_rt2880.c b/drivers/net/ethernet/mediatek/mdio_rt2880.c
24 index 0000000..eb9df6e
26 +++ b/drivers/net/ethernet/mediatek/mdio_rt2880.c
28 +/* This program is free software; you can redistribute it and/or modify
29 + * it under the terms of the GNU General Public License as published by
30 + * the Free Software Foundation; version 2 of the License
32 + * This program is distributed in the hope that it will be useful,
33 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 + * GNU General Public License for more details.
37 + * Copyright (C) 2009-2015 John Crispin <blogic@openwrt.org>
38 + * Copyright (C) 2009-2015 Felix Fietkau <nbd@openwrt.org>
39 + * Copyright (C) 2013-2015 Michael Lee <igvtee@gmail.com>
42 +#include <linux/module.h>
43 +#include <linux/kernel.h>
44 +#include <linux/types.h>
45 +#include <linux/of_net.h>
46 +#include <linux/of_mdio.h>
48 +#include "mtk_eth_soc.h"
49 +#include "mdio_rt2880.h"
52 +#define FE_MDIO_RETRY 1000
54 +static unsigned char *rt2880_speed_str(struct fe_priv *priv)
56 + switch (priv->phy->speed[0]) {
68 +void rt2880_mdio_link_adjust(struct fe_priv *priv, int port)
72 + if (!priv->link[0]) {
73 + netif_carrier_off(priv->netdev);
74 + netdev_info(priv->netdev, "link down\n");
78 + mdio_cfg = FE_MDIO_CFG_TX_CLK_SKEW_200 |
79 + FE_MDIO_CFG_RX_CLK_SKEW_200 |
80 + FE_MDIO_CFG_GP1_FRC_EN;
82 + if (priv->phy->duplex[0] == DUPLEX_FULL)
83 + mdio_cfg |= FE_MDIO_CFG_GP1_DUPLEX;
85 + if (priv->phy->tx_fc[0])
86 + mdio_cfg |= FE_MDIO_CFG_GP1_FC_TX;
88 + if (priv->phy->rx_fc[0])
89 + mdio_cfg |= FE_MDIO_CFG_GP1_FC_RX;
91 + switch (priv->phy->speed[0]) {
93 + mdio_cfg |= FE_MDIO_CFG_GP1_SPEED_10;
96 + mdio_cfg |= FE_MDIO_CFG_GP1_SPEED_100;
99 + mdio_cfg |= FE_MDIO_CFG_GP1_SPEED_1000;
105 + fe_w32(mdio_cfg, FE_MDIO_CFG);
107 + netif_carrier_on(priv->netdev);
108 + netdev_info(priv->netdev, "link up (%sMbps/%s duplex)\n",
109 + rt2880_speed_str(priv),
110 + (priv->phy->duplex[0] == DUPLEX_FULL) ? "Full" : "Half");
113 +static int rt2880_mdio_wait_ready(struct fe_priv *priv)
117 + retries = FE_MDIO_RETRY;
121 + t = fe_r32(FE_MDIO_ACCESS);
122 + if ((t & BIT(31)) == 0)
125 + if (retries-- == 0)
131 + dev_err(priv->device, "MDIO operation timed out\n");
135 +int rt2880_mdio_read(struct mii_bus *bus, int phy_addr, int phy_reg)
137 + struct fe_priv *priv = bus->priv;
141 + err = rt2880_mdio_wait_ready(priv);
145 + t = (phy_addr << 24) | (phy_reg << 16);
146 + fe_w32(t, FE_MDIO_ACCESS);
148 + fe_w32(t, FE_MDIO_ACCESS);
150 + err = rt2880_mdio_wait_ready(priv);
154 + pr_debug("%s: addr=%04x, reg=%04x, value=%04x\n", __func__,
155 + phy_addr, phy_reg, fe_r32(FE_MDIO_ACCESS) & 0xffff);
157 + return fe_r32(FE_MDIO_ACCESS) & 0xffff;
160 +int rt2880_mdio_write(struct mii_bus *bus, int phy_addr, int phy_reg, u16 val)
162 + struct fe_priv *priv = bus->priv;
166 + pr_debug("%s: addr=%04x, reg=%04x, value=%04x\n", __func__,
167 + phy_addr, phy_reg, fe_r32(FE_MDIO_ACCESS) & 0xffff);
169 + err = rt2880_mdio_wait_ready(priv);
173 + t = (1 << 30) | (phy_addr << 24) | (phy_reg << 16) | val;
174 + fe_w32(t, FE_MDIO_ACCESS);
176 + fe_w32(t, FE_MDIO_ACCESS);
178 + return rt2880_mdio_wait_ready(priv);
181 +void rt2880_port_init(struct fe_priv *priv, struct device_node *np)
183 + const __be32 *id = of_get_property(np, "reg", NULL);
184 + const __be32 *link;
188 + if (!id || (be32_to_cpu(*id) != 0)) {
189 + pr_err("%s: invalid port id\n", np->name);
193 + priv->phy->phy_fixed[0] = of_get_property(np,
194 + "mediatek,fixed-link", &size);
195 + if (priv->phy->phy_fixed[0] &&
196 + (size != (4 * sizeof(*priv->phy->phy_fixed[0])))) {
197 + pr_err("%s: invalid fixed link property\n", np->name);
198 + priv->phy->phy_fixed[0] = NULL;
202 + phy_mode = of_get_phy_mode(np);
203 + switch (phy_mode) {
204 + case PHY_INTERFACE_MODE_RGMII:
206 + case PHY_INTERFACE_MODE_MII:
208 + case PHY_INTERFACE_MODE_RMII:
211 + if (!priv->phy->phy_fixed[0])
212 + dev_err(priv->device, "port %d - invalid phy mode\n",
213 + priv->phy->speed[0]);
217 + priv->phy->phy_node[0] = of_parse_phandle(np, "phy-handle", 0);
218 + if (!priv->phy->phy_node[0] && !priv->phy->phy_fixed[0])
221 + if (priv->phy->phy_fixed[0]) {
222 + link = priv->phy->phy_fixed[0];
223 + priv->phy->speed[0] = be32_to_cpup(link++);
224 + priv->phy->duplex[0] = be32_to_cpup(link++);
225 + priv->phy->tx_fc[0] = be32_to_cpup(link++);
226 + priv->phy->rx_fc[0] = be32_to_cpup(link++);
229 + switch (priv->phy->speed[0]) {
237 + dev_err(priv->device, "invalid link speed: %d\n",
238 + priv->phy->speed[0]);
239 + priv->phy->phy_fixed[0] = 0;
242 + dev_info(priv->device, "using fixed link parameters\n");
243 + rt2880_mdio_link_adjust(priv, 0);
247 + if (priv->phy->phy_node[0] && priv->mii_bus->phy_map[0])
248 + fe_connect_phy_node(priv, priv->phy->phy_node[0]);
250 diff --git a/drivers/net/ethernet/mediatek/mdio_rt2880.h b/drivers/net/ethernet/mediatek/mdio_rt2880.h
252 index 0000000..6884894
254 +++ b/drivers/net/ethernet/mediatek/mdio_rt2880.h
256 +/* This program is free software; you can redistribute it and/or modify
257 + * it under the terms of the GNU General Public License as published by
258 + * the Free Software Foundation; version 2 of the License
260 + * This program is distributed in the hope that it will be useful,
261 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
262 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
263 + * GNU General Public License for more details.
265 + * Copyright (C) 2009-2015 John Crispin <blogic@openwrt.org>
266 + * Copyright (C) 2009-2015 Felix Fietkau <nbd@openwrt.org>
267 + * Copyright (C) 2013-2015 Michael Lee <igvtee@gmail.com>
270 +#ifndef _RALINK_MDIO_RT2880_H__
271 +#define _RALINK_MDIO_RT2880_H__
273 +void rt2880_mdio_link_adjust(struct fe_priv *priv, int port);
274 +int rt2880_mdio_read(struct mii_bus *bus, int phy_addr, int phy_reg);
275 +int rt2880_mdio_write(struct mii_bus *bus, int phy_addr, int phy_reg, u16 val);
276 +void rt2880_port_init(struct fe_priv *priv, struct device_node *np);
279 diff --git a/drivers/net/ethernet/mediatek/soc_rt2880.c b/drivers/net/ethernet/mediatek/soc_rt2880.c
281 index 0000000..0792dab
283 +++ b/drivers/net/ethernet/mediatek/soc_rt2880.c
285 +/* This program is free software; you can redistribute it and/or modify
286 + * it under the terms of the GNU General Public License as published by
287 + * the Free Software Foundation; version 2 of the License
289 + * This program is distributed in the hope that it will be useful,
290 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
291 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
292 + * GNU General Public License for more details.
294 + * Copyright (C) 2009-2015 John Crispin <blogic@openwrt.org>
295 + * Copyright (C) 2009-2015 Felix Fietkau <nbd@openwrt.org>
296 + * Copyright (C) 2013-2015 Michael Lee <igvtee@gmail.com>
299 +#include <linux/module.h>
301 +#include <asm/mach-ralink/ralink_regs.h>
303 +#include "mtk_eth_soc.h"
304 +#include "mdio_rt2880.h"
306 +#define RT2880_RESET_FE BIT(18)
308 +static void rt2880_init_data(struct fe_soc_data *data,
309 + struct net_device *netdev)
311 + struct fe_priv *priv = netdev_priv(netdev);
313 + priv->flags = FE_FLAG_PADDING_64B | FE_FLAG_PADDING_BUG |
314 + FE_FLAG_JUMBO_FRAME | FE_FLAG_CALIBRATE_CLK;
315 + netdev->hw_features = NETIF_F_SG | NETIF_F_HW_VLAN_CTAG_TX;
316 + /* this should work according to the datasheet but actually does not*/
317 + /* netdev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_RXCSUM; */
320 +void rt2880_fe_reset(void)
322 + fe_reset(RT2880_RESET_FE);
325 +static int rt2880_fwd_config(struct fe_priv *priv)
329 + ret = fe_set_clock_cycle(priv);
333 + fe_fwd_config(priv);
334 + fe_w32(FE_PSE_FQFC_CFG_INIT, FE_PSE_FQ_CFG);
335 + fe_csum_config(priv);
340 +struct fe_soc_data rt2880_data = {
341 + .init_data = rt2880_init_data,
342 + .reset_fe = rt2880_fe_reset,
343 + .fwd_config = rt2880_fwd_config,
344 + .pdma_glo_cfg = FE_PDMA_SIZE_8DWORDS,
345 + .checksum_bit = RX_DMA_L4VALID,
346 + .rx_int = FE_RX_DONE_INT,
347 + .tx_int = FE_TX_DONE_INT,
348 + .status_int = FE_CNT_GDM_AF,
349 + .mdio_read = rt2880_mdio_read,
350 + .mdio_write = rt2880_mdio_write,
351 + .mdio_adjust_link = rt2880_mdio_link_adjust,
352 + .port_init = rt2880_port_init,
355 +const struct of_device_id of_fe_match[] = {
356 + { .compatible = "ralink,rt2880-eth", .data = &rt2880_data },
360 +MODULE_DEVICE_TABLE(of, of_fe_match);