blob: fd9437f9d6b682022ece558c21bd7b479f912f2f [file] [log] [blame]
Andrew Lunn406a4362019-04-27 19:32:56 +02001// SPDX-License-Identifier: GPL-2.0+
Lennert Buytenhek2e16a772008-10-07 13:46:22 +00002/*
3 * net/dsa/mv88e6060.c - Driver for Marvell 88e6060 switch chips
Lennert Buytenheke84665c2009-03-20 09:52:09 +00004 * Copyright (c) 2008-2009 Marvell Semiconductor
Lennert Buytenhek2e16a772008-10-07 13:46:22 +00005 */
6
Barry Grussling19b2f972013-01-08 16:05:54 +00007#include <linux/delay.h>
Vivien Didelot56c3ff92017-10-13 14:18:07 -04008#include <linux/etherdevice.h>
Barry Grussling19b2f972013-01-08 16:05:54 +00009#include <linux/jiffies.h>
Lennert Buytenhek2e16a772008-10-07 13:46:22 +000010#include <linux/list.h>
Paul Gortmaker2bbba272012-01-24 10:41:40 +000011#include <linux/module.h>
Lennert Buytenhek2e16a772008-10-07 13:46:22 +000012#include <linux/netdevice.h>
13#include <linux/phy.h>
Ben Hutchingsc8f0b862011-11-27 17:06:08 +000014#include <net/dsa.h>
Neil Armstrong6a4b2982015-11-10 16:51:36 +010015#include "mv88e6060.h"
Lennert Buytenhek2e16a772008-10-07 13:46:22 +000016
Andrew Lunn3e8bc1b2019-04-27 19:32:57 +020017static int reg_read(struct mv88e6060_priv *priv, int addr, int reg)
Lennert Buytenhek2e16a772008-10-07 13:46:22 +000018{
Andrew Lunna77d43f2016-04-13 02:40:42 +020019 return mdiobus_read_nested(priv->bus, priv->sw_addr + addr, reg);
Lennert Buytenhek2e16a772008-10-07 13:46:22 +000020}
21
22#define REG_READ(addr, reg) \
23 ({ \
24 int __ret; \
25 \
Andrew Lunn3e8bc1b2019-04-27 19:32:57 +020026 __ret = reg_read(priv, addr, reg); \
Lennert Buytenhek2e16a772008-10-07 13:46:22 +000027 if (__ret < 0) \
28 return __ret; \
29 __ret; \
30 })
31
32
Andrew Lunn3e8bc1b2019-04-27 19:32:57 +020033static int reg_write(struct mv88e6060_priv *priv, int addr, int reg, u16 val)
Lennert Buytenhek2e16a772008-10-07 13:46:22 +000034{
Andrew Lunna77d43f2016-04-13 02:40:42 +020035 return mdiobus_write_nested(priv->bus, priv->sw_addr + addr, reg, val);
Lennert Buytenhek2e16a772008-10-07 13:46:22 +000036}
37
38#define REG_WRITE(addr, reg, val) \
39 ({ \
40 int __ret; \
41 \
Andrew Lunn3e8bc1b2019-04-27 19:32:57 +020042 __ret = reg_write(priv, addr, reg, val); \
Lennert Buytenhek2e16a772008-10-07 13:46:22 +000043 if (__ret < 0) \
44 return __ret; \
45 })
46
Vivien Didelot0209d142016-04-17 13:23:55 -040047static const char *mv88e6060_get_name(struct mii_bus *bus, int sw_addr)
Lennert Buytenhek2e16a772008-10-07 13:46:22 +000048{
49 int ret;
50
Neil Armstrong6a4b2982015-11-10 16:51:36 +010051 ret = mdiobus_read(bus, sw_addr + REG_PORT(0), PORT_SWITCH_ID);
Lennert Buytenhek2e16a772008-10-07 13:46:22 +000052 if (ret >= 0) {
Neil Armstrong6a4b2982015-11-10 16:51:36 +010053 if (ret == PORT_SWITCH_ID_6060)
Guenter Roeck3de6aa4c2014-10-29 10:44:54 -070054 return "Marvell 88E6060 (A0)";
Neil Armstrong6a4b2982015-11-10 16:51:36 +010055 if (ret == PORT_SWITCH_ID_6060_R1 ||
56 ret == PORT_SWITCH_ID_6060_R2)
Guenter Roeck3de6aa4c2014-10-29 10:44:54 -070057 return "Marvell 88E6060 (B0)";
Neil Armstrong6a4b2982015-11-10 16:51:36 +010058 if ((ret & PORT_SWITCH_ID_6060_MASK) == PORT_SWITCH_ID_6060)
Lennert Buytenhek2e16a772008-10-07 13:46:22 +000059 return "Marvell 88E6060";
60 }
61
62 return NULL;
63}
64
Florian Fainelli5ed4e3e2017-11-10 15:22:52 -080065static enum dsa_tag_protocol mv88e6060_get_tag_protocol(struct dsa_switch *ds,
66 int port)
Andrew Lunn7b314362016-08-22 16:01:01 +020067{
68 return DSA_TAG_PROTO_TRAILER;
69}
70
Vivien Didelot0209d142016-04-17 13:23:55 -040071static const char *mv88e6060_drv_probe(struct device *dsa_dev,
72 struct device *host_dev, int sw_addr,
73 void **_priv)
Andrew Lunna77d43f2016-04-13 02:40:42 +020074{
75 struct mii_bus *bus = dsa_host_dev_to_mii_bus(host_dev);
76 struct mv88e6060_priv *priv;
Vivien Didelot0209d142016-04-17 13:23:55 -040077 const char *name;
Andrew Lunna77d43f2016-04-13 02:40:42 +020078
79 name = mv88e6060_get_name(bus, sw_addr);
80 if (name) {
81 priv = devm_kzalloc(dsa_dev, sizeof(*priv), GFP_KERNEL);
82 if (!priv)
83 return NULL;
84 *_priv = priv;
85 priv->bus = bus;
86 priv->sw_addr = sw_addr;
87 }
88
89 return name;
90}
91
Andrew Lunn3e8bc1b2019-04-27 19:32:57 +020092static int mv88e6060_switch_reset(struct mv88e6060_priv *priv)
Lennert Buytenhek2e16a772008-10-07 13:46:22 +000093{
94 int i;
95 int ret;
Barry Grussling19b2f972013-01-08 16:05:54 +000096 unsigned long timeout;
Lennert Buytenhek2e16a772008-10-07 13:46:22 +000097
Barry Grussling3675c8d2013-01-08 16:05:53 +000098 /* Set all ports to the disabled state. */
Neil Armstrong6a4b2982015-11-10 16:51:36 +010099 for (i = 0; i < MV88E6060_PORTS; i++) {
100 ret = REG_READ(REG_PORT(i), PORT_CONTROL);
101 REG_WRITE(REG_PORT(i), PORT_CONTROL,
102 ret & ~PORT_CONTROL_STATE_MASK);
Lennert Buytenhek2e16a772008-10-07 13:46:22 +0000103 }
104
Barry Grussling3675c8d2013-01-08 16:05:53 +0000105 /* Wait for transmit queues to drain. */
Barry Grussling19b2f972013-01-08 16:05:54 +0000106 usleep_range(2000, 4000);
Lennert Buytenhek2e16a772008-10-07 13:46:22 +0000107
Barry Grussling3675c8d2013-01-08 16:05:53 +0000108 /* Reset the switch. */
Neil Armstrong6a4b2982015-11-10 16:51:36 +0100109 REG_WRITE(REG_GLOBAL, GLOBAL_ATU_CONTROL,
110 GLOBAL_ATU_CONTROL_SWRESET |
Anderson Luiz Alvesa7451562018-11-30 21:58:36 -0200111 GLOBAL_ATU_CONTROL_LEARNDIS);
Lennert Buytenhek2e16a772008-10-07 13:46:22 +0000112
Barry Grussling3675c8d2013-01-08 16:05:53 +0000113 /* Wait up to one second for reset to complete. */
Barry Grussling19b2f972013-01-08 16:05:54 +0000114 timeout = jiffies + 1 * HZ;
115 while (time_before(jiffies, timeout)) {
Neil Armstrong6a4b2982015-11-10 16:51:36 +0100116 ret = REG_READ(REG_GLOBAL, GLOBAL_STATUS);
117 if (ret & GLOBAL_STATUS_INIT_READY)
Lennert Buytenhek2e16a772008-10-07 13:46:22 +0000118 break;
119
Barry Grussling19b2f972013-01-08 16:05:54 +0000120 usleep_range(1000, 2000);
Lennert Buytenhek2e16a772008-10-07 13:46:22 +0000121 }
Barry Grussling19b2f972013-01-08 16:05:54 +0000122 if (time_after(jiffies, timeout))
Lennert Buytenhek2e16a772008-10-07 13:46:22 +0000123 return -ETIMEDOUT;
124
125 return 0;
126}
127
Andrew Lunn3e8bc1b2019-04-27 19:32:57 +0200128static int mv88e6060_setup_global(struct mv88e6060_priv *priv)
Lennert Buytenhek2e16a772008-10-07 13:46:22 +0000129{
Barry Grussling3675c8d2013-01-08 16:05:53 +0000130 /* Disable discarding of frames with excessive collisions,
Lennert Buytenhek2e16a772008-10-07 13:46:22 +0000131 * set the maximum frame size to 1536 bytes, and mask all
132 * interrupt sources.
133 */
Neil Armstrong6a4b2982015-11-10 16:51:36 +0100134 REG_WRITE(REG_GLOBAL, GLOBAL_CONTROL, GLOBAL_CONTROL_MAX_FRAME_1536);
Lennert Buytenhek2e16a772008-10-07 13:46:22 +0000135
Anderson Luiz Alvesa7451562018-11-30 21:58:36 -0200136 /* Disable automatic address learning.
Lennert Buytenhek2e16a772008-10-07 13:46:22 +0000137 */
Neil Armstrong6a4b2982015-11-10 16:51:36 +0100138 REG_WRITE(REG_GLOBAL, GLOBAL_ATU_CONTROL,
Anderson Luiz Alvesa7451562018-11-30 21:58:36 -0200139 GLOBAL_ATU_CONTROL_LEARNDIS);
Lennert Buytenhek2e16a772008-10-07 13:46:22 +0000140
141 return 0;
142}
143
Andrew Lunn3e8bc1b2019-04-27 19:32:57 +0200144static int mv88e6060_setup_port(struct mv88e6060_priv *priv, int p)
Lennert Buytenhek2e16a772008-10-07 13:46:22 +0000145{
146 int addr = REG_PORT(p);
147
Barry Grussling3675c8d2013-01-08 16:05:53 +0000148 /* Do not force flow control, disable Ingress and Egress
Lennert Buytenhek2e16a772008-10-07 13:46:22 +0000149 * Header tagging, disable VLAN tunneling, and set the port
150 * state to Forwarding. Additionally, if this is the CPU
151 * port, enable Ingress and Egress Trailer tagging mode.
152 */
Neil Armstrong6a4b2982015-11-10 16:51:36 +0100153 REG_WRITE(addr, PORT_CONTROL,
Andrew Lunn3e8bc1b2019-04-27 19:32:57 +0200154 dsa_is_cpu_port(priv->ds, p) ?
Neil Armstrong6a4b2982015-11-10 16:51:36 +0100155 PORT_CONTROL_TRAILER |
156 PORT_CONTROL_INGRESS_MODE |
157 PORT_CONTROL_STATE_FORWARDING :
158 PORT_CONTROL_STATE_FORWARDING);
Lennert Buytenhek2e16a772008-10-07 13:46:22 +0000159
Barry Grussling3675c8d2013-01-08 16:05:53 +0000160 /* Port based VLAN map: give each port its own address
Lennert Buytenhek2e16a772008-10-07 13:46:22 +0000161 * database, allow the CPU port to talk to each of the 'real'
162 * ports, and allow each of the 'real' ports to only talk to
163 * the CPU port.
164 */
Neil Armstrong6a4b2982015-11-10 16:51:36 +0100165 REG_WRITE(addr, PORT_VLAN_MAP,
166 ((p & 0xf) << PORT_VLAN_MAP_DBNUM_SHIFT) |
Andrew Lunn3e8bc1b2019-04-27 19:32:57 +0200167 (dsa_is_cpu_port(priv->ds, p) ? dsa_user_ports(priv->ds) :
168 BIT(dsa_to_port(priv->ds, p)->cpu_dp->index)));
Lennert Buytenhek2e16a772008-10-07 13:46:22 +0000169
Barry Grussling3675c8d2013-01-08 16:05:53 +0000170 /* Port Association Vector: when learning source addresses
Lennert Buytenhek2e16a772008-10-07 13:46:22 +0000171 * of packets, add the address to the address database using
172 * a port bitmap that has only the bit for this port set and
173 * the other bits clear.
174 */
Neil Armstrong6a4b2982015-11-10 16:51:36 +0100175 REG_WRITE(addr, PORT_ASSOC_VECTOR, BIT(p));
Lennert Buytenhek2e16a772008-10-07 13:46:22 +0000176
177 return 0;
178}
179
Andrew Lunn3e8bc1b2019-04-27 19:32:57 +0200180static int mv88e6060_setup_addr(struct mv88e6060_priv *priv)
Vivien Didelot56c3ff92017-10-13 14:18:07 -0400181{
182 u8 addr[ETH_ALEN];
183 u16 val;
184
185 eth_random_addr(addr);
186
187 val = addr[0] << 8 | addr[1];
188
189 /* The multicast bit is always transmitted as a zero, so the switch uses
190 * bit 8 for "DiffAddr", where 0 means all ports transmit the same SA.
191 */
192 val &= 0xfeff;
193
194 REG_WRITE(REG_GLOBAL, GLOBAL_MAC_01, val);
195 REG_WRITE(REG_GLOBAL, GLOBAL_MAC_23, (addr[2] << 8) | addr[3]);
196 REG_WRITE(REG_GLOBAL, GLOBAL_MAC_45, (addr[4] << 8) | addr[5]);
197
198 return 0;
199}
200
Lennert Buytenhek2e16a772008-10-07 13:46:22 +0000201static int mv88e6060_setup(struct dsa_switch *ds)
202{
Andrew Lunn3e8bc1b2019-04-27 19:32:57 +0200203 struct mv88e6060_priv *priv = ds->priv;
Lennert Buytenhek2e16a772008-10-07 13:46:22 +0000204 int ret;
Andrew Lunna77d43f2016-04-13 02:40:42 +0200205 int i;
Lennert Buytenhek2e16a772008-10-07 13:46:22 +0000206
Andrew Lunn3e8bc1b2019-04-27 19:32:57 +0200207 priv->ds = ds;
208
209 ret = mv88e6060_switch_reset(priv);
Lennert Buytenhek2e16a772008-10-07 13:46:22 +0000210 if (ret < 0)
211 return ret;
212
213 /* @@@ initialise atu */
214
Andrew Lunn3e8bc1b2019-04-27 19:32:57 +0200215 ret = mv88e6060_setup_global(priv);
Lennert Buytenhek2e16a772008-10-07 13:46:22 +0000216 if (ret < 0)
217 return ret;
218
Andrew Lunn3e8bc1b2019-04-27 19:32:57 +0200219 ret = mv88e6060_setup_addr(priv);
Vivien Didelot56c3ff92017-10-13 14:18:07 -0400220 if (ret < 0)
221 return ret;
222
Neil Armstrong6a4b2982015-11-10 16:51:36 +0100223 for (i = 0; i < MV88E6060_PORTS; i++) {
Andrew Lunn3e8bc1b2019-04-27 19:32:57 +0200224 ret = mv88e6060_setup_port(priv, i);
Lennert Buytenhek2e16a772008-10-07 13:46:22 +0000225 if (ret < 0)
226 return ret;
227 }
228
229 return 0;
230}
231
Lennert Buytenhek2e16a772008-10-07 13:46:22 +0000232static int mv88e6060_port_to_phy_addr(int port)
233{
Neil Armstrong6a4b2982015-11-10 16:51:36 +0100234 if (port >= 0 && port < MV88E6060_PORTS)
Lennert Buytenhek2e16a772008-10-07 13:46:22 +0000235 return port;
236 return -1;
237}
238
239static int mv88e6060_phy_read(struct dsa_switch *ds, int port, int regnum)
240{
Andrew Lunn3e8bc1b2019-04-27 19:32:57 +0200241 struct mv88e6060_priv *priv = ds->priv;
Lennert Buytenhek2e16a772008-10-07 13:46:22 +0000242 int addr;
243
244 addr = mv88e6060_port_to_phy_addr(port);
245 if (addr == -1)
246 return 0xffff;
247
Andrew Lunn3e8bc1b2019-04-27 19:32:57 +0200248 return reg_read(priv, addr, regnum);
Lennert Buytenhek2e16a772008-10-07 13:46:22 +0000249}
250
251static int
252mv88e6060_phy_write(struct dsa_switch *ds, int port, int regnum, u16 val)
253{
Andrew Lunn3e8bc1b2019-04-27 19:32:57 +0200254 struct mv88e6060_priv *priv = ds->priv;
Lennert Buytenhek2e16a772008-10-07 13:46:22 +0000255 int addr;
256
257 addr = mv88e6060_port_to_phy_addr(port);
258 if (addr == -1)
259 return 0xffff;
260
Andrew Lunn3e8bc1b2019-04-27 19:32:57 +0200261 return reg_write(priv, addr, regnum, val);
Lennert Buytenhek2e16a772008-10-07 13:46:22 +0000262}
263
Florian Fainellia82f67a2017-01-08 14:52:08 -0800264static const struct dsa_switch_ops mv88e6060_switch_ops = {
Andrew Lunn7b314362016-08-22 16:01:01 +0200265 .get_tag_protocol = mv88e6060_get_tag_protocol,
Andrew Lunne49bad32016-04-13 02:40:43 +0200266 .probe = mv88e6060_drv_probe,
Lennert Buytenhek2e16a772008-10-07 13:46:22 +0000267 .setup = mv88e6060_setup,
Lennert Buytenhek2e16a772008-10-07 13:46:22 +0000268 .phy_read = mv88e6060_phy_read,
269 .phy_write = mv88e6060_phy_write,
Lennert Buytenhek2e16a772008-10-07 13:46:22 +0000270};
271
Florian Fainelliab3d4082017-01-08 14:52:07 -0800272static struct dsa_switch_driver mv88e6060_switch_drv = {
273 .ops = &mv88e6060_switch_ops,
274};
275
Roel Kluin5eaa65b2008-12-10 15:18:31 -0800276static int __init mv88e6060_init(void)
Lennert Buytenhek2e16a772008-10-07 13:46:22 +0000277{
Florian Fainelliab3d4082017-01-08 14:52:07 -0800278 register_switch_driver(&mv88e6060_switch_drv);
Lennert Buytenhek2e16a772008-10-07 13:46:22 +0000279 return 0;
280}
281module_init(mv88e6060_init);
282
Roel Kluin5eaa65b2008-12-10 15:18:31 -0800283static void __exit mv88e6060_cleanup(void)
Lennert Buytenhek2e16a772008-10-07 13:46:22 +0000284{
Florian Fainelliab3d4082017-01-08 14:52:07 -0800285 unregister_switch_driver(&mv88e6060_switch_drv);
Lennert Buytenhek2e16a772008-10-07 13:46:22 +0000286}
287module_exit(mv88e6060_cleanup);
Ben Hutchings3d825ed2011-11-25 14:37:16 +0000288
289MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
290MODULE_DESCRIPTION("Driver for Marvell 88E6060 ethernet switch chip");
291MODULE_LICENSE("GPL");
292MODULE_ALIAS("platform:mv88e6060");