blob: 1f5129c105fb3cce995527104b049ebf6e89abac [file] [log] [blame]
Guenter Roeck3ad50cc2014-10-29 10:44:56 -07001/*
2 * net/dsa/mv88e6352.c - Marvell 88e6352 switch chip support
3 *
4 * Copyright (c) 2014 Guenter Roeck
5 *
6 * Derived from mv88e6123_61_65.c
7 * Copyright (c) 2008-2009 Marvell Semiconductor
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 */
14
15#include <linux/delay.h>
16#include <linux/jiffies.h>
17#include <linux/list.h>
18#include <linux/module.h>
19#include <linux/netdevice.h>
20#include <linux/platform_device.h>
21#include <linux/phy.h>
22#include <net/dsa.h>
23#include "mv88e6xxx.h"
24
Guenter Roeck3ad50cc2014-10-29 10:44:56 -070025static char *mv88e6352_probe(struct device *host_dev, int sw_addr)
26{
27 struct mii_bus *bus = dsa_host_dev_to_mii_bus(host_dev);
28 int ret;
29
30 if (bus == NULL)
31 return NULL;
32
Andrew Lunncca8b132015-04-02 04:06:39 +020033 ret = __mv88e6xxx_reg_read(bus, sw_addr, REG_PORT(0), PORT_SWITCH_ID);
Guenter Roeck3ad50cc2014-10-29 10:44:56 -070034 if (ret >= 0) {
Andrew Lunn1636d882015-05-06 01:09:50 +020035 if ((ret & 0xfff0) == PORT_SWITCH_ID_6172)
36 return "Marvell 88E6172";
Andrew Lunncca8b132015-04-02 04:06:39 +020037 if ((ret & 0xfff0) == PORT_SWITCH_ID_6176)
Guenter Roeck27167772014-10-29 10:44:57 -070038 return "Marvell 88E6176";
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -070039 if (ret == PORT_SWITCH_ID_6320_A1)
40 return "Marvell 88E6320 (A1)";
41 if (ret == PORT_SWITCH_ID_6320_A2)
42 return "Marvell 88e6320 (A2)";
43 if ((ret & 0xfff0) == PORT_SWITCH_ID_6320)
44 return "Marvell 88E6320";
45 if (ret == PORT_SWITCH_ID_6321_A1)
46 return "Marvell 88E6321 (A1)";
47 if (ret == PORT_SWITCH_ID_6321_A2)
48 return "Marvell 88e6321 (A2)";
49 if ((ret & 0xfff0) == PORT_SWITCH_ID_6321)
50 return "Marvell 88E6321";
Andrew Lunncca8b132015-04-02 04:06:39 +020051 if (ret == PORT_SWITCH_ID_6352_A0)
Guenter Roeck3ad50cc2014-10-29 10:44:56 -070052 return "Marvell 88E6352 (A0)";
Andrew Lunncca8b132015-04-02 04:06:39 +020053 if (ret == PORT_SWITCH_ID_6352_A1)
Guenter Roeck3ad50cc2014-10-29 10:44:56 -070054 return "Marvell 88E6352 (A1)";
Andrew Lunncca8b132015-04-02 04:06:39 +020055 if ((ret & 0xfff0) == PORT_SWITCH_ID_6352)
Guenter Roeck3ad50cc2014-10-29 10:44:56 -070056 return "Marvell 88E6352";
57 }
58
59 return NULL;
60}
61
Guenter Roeck3ad50cc2014-10-29 10:44:56 -070062static int mv88e6352_setup_global(struct dsa_switch *ds)
63{
Andrew Lunn15966a22015-05-06 01:09:49 +020064 u32 upstream_port = dsa_upstream_port(ds);
Guenter Roeck3ad50cc2014-10-29 10:44:56 -070065 int ret;
Andrew Lunn15966a22015-05-06 01:09:49 +020066 u32 reg;
Andrew Lunn54d792f2015-05-06 01:09:47 +020067
68 ret = mv88e6xxx_setup_global(ds);
69 if (ret)
70 return ret;
Guenter Roeck3ad50cc2014-10-29 10:44:56 -070071
72 /* Discard packets with excessive collisions,
73 * mask all interrupt sources, enable PPU (bit 14, undocumented).
74 */
Andrew Lunn15966a22015-05-06 01:09:49 +020075 REG_WRITE(REG_GLOBAL, GLOBAL_CONTROL,
76 GLOBAL_CONTROL_PPU_ENABLE | GLOBAL_CONTROL_DISCARD_EXCESS);
Guenter Roeck3ad50cc2014-10-29 10:44:56 -070077
Guenter Roeck3ad50cc2014-10-29 10:44:56 -070078 /* Configure the upstream port, and configure the upstream
79 * port as the port to which ingress and egress monitor frames
80 * are to be sent.
81 */
Andrew Lunn15966a22015-05-06 01:09:49 +020082 reg = upstream_port << GLOBAL_MONITOR_CONTROL_INGRESS_SHIFT |
83 upstream_port << GLOBAL_MONITOR_CONTROL_EGRESS_SHIFT |
84 upstream_port << GLOBAL_MONITOR_CONTROL_ARP_SHIFT;
85 REG_WRITE(REG_GLOBAL, GLOBAL_MONITOR_CONTROL, reg);
Guenter Roeck3ad50cc2014-10-29 10:44:56 -070086
87 /* Disable remote management for now, and set the switch's
88 * DSA device number.
89 */
90 REG_WRITE(REG_GLOBAL, 0x1c, ds->index & 0x1f);
91
Guenter Roeck3ad50cc2014-10-29 10:44:56 -070092 return 0;
93}
94
Guenter Roeck3ad50cc2014-10-29 10:44:56 -070095static int mv88e6352_setup(struct dsa_switch *ds)
96{
97 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
98 int ret;
Guenter Roeck3ad50cc2014-10-29 10:44:56 -070099
Guenter Roeckacdaffc2015-03-26 18:36:28 -0700100 ret = mv88e6xxx_setup_common(ds);
101 if (ret < 0)
102 return ret;
103
Andrew Lunn44e50dd2015-04-02 04:06:33 +0200104 ps->num_ports = 7;
105
Guenter Roeck33b43df2014-10-29 10:45:03 -0700106 mutex_init(&ps->eeprom_mutex);
Guenter Roeck3ad50cc2014-10-29 10:44:56 -0700107
Andrew Lunn143a83072015-04-02 04:06:34 +0200108 ret = mv88e6xxx_switch_reset(ds, true);
Guenter Roeck3ad50cc2014-10-29 10:44:56 -0700109 if (ret < 0)
110 return ret;
111
Guenter Roeck3ad50cc2014-10-29 10:44:56 -0700112 ret = mv88e6352_setup_global(ds);
113 if (ret < 0)
114 return ret;
115
Andrew Lunndbde9e62015-05-06 01:09:48 +0200116 return mv88e6xxx_setup_ports(ds);
Guenter Roeck3ad50cc2014-10-29 10:44:56 -0700117}
118
Guenter Roeck33b43df2014-10-29 10:45:03 -0700119static int mv88e6352_read_eeprom_word(struct dsa_switch *ds, int addr)
120{
121 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
122 int ret;
123
124 mutex_lock(&ps->eeprom_mutex);
125
Andrew Lunn966bce32015-08-08 17:04:50 +0200126 ret = mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
127 GLOBAL2_EEPROM_OP_READ |
128 (addr & GLOBAL2_EEPROM_OP_ADDR_MASK));
Guenter Roeck33b43df2014-10-29 10:45:03 -0700129 if (ret < 0)
130 goto error;
131
Andrew Lunnf3044682015-02-14 19:17:50 +0100132 ret = mv88e6xxx_eeprom_busy_wait(ds);
Guenter Roeck33b43df2014-10-29 10:45:03 -0700133 if (ret < 0)
134 goto error;
135
Andrew Lunn966bce32015-08-08 17:04:50 +0200136 ret = mv88e6xxx_reg_read(ds, REG_GLOBAL2, GLOBAL2_EEPROM_DATA);
Guenter Roeck33b43df2014-10-29 10:45:03 -0700137error:
138 mutex_unlock(&ps->eeprom_mutex);
139 return ret;
140}
141
142static int mv88e6352_get_eeprom(struct dsa_switch *ds,
143 struct ethtool_eeprom *eeprom, u8 *data)
144{
145 int offset;
146 int len;
147 int ret;
148
149 offset = eeprom->offset;
150 len = eeprom->len;
151 eeprom->len = 0;
152
153 eeprom->magic = 0xc3ec4951;
154
Andrew Lunnf3044682015-02-14 19:17:50 +0100155 ret = mv88e6xxx_eeprom_load_wait(ds);
Guenter Roeck33b43df2014-10-29 10:45:03 -0700156 if (ret < 0)
157 return ret;
158
159 if (offset & 1) {
160 int word;
161
162 word = mv88e6352_read_eeprom_word(ds, offset >> 1);
163 if (word < 0)
164 return word;
165
166 *data++ = (word >> 8) & 0xff;
167
168 offset++;
169 len--;
170 eeprom->len++;
171 }
172
173 while (len >= 2) {
174 int word;
175
176 word = mv88e6352_read_eeprom_word(ds, offset >> 1);
177 if (word < 0)
178 return word;
179
180 *data++ = word & 0xff;
181 *data++ = (word >> 8) & 0xff;
182
183 offset += 2;
184 len -= 2;
185 eeprom->len += 2;
186 }
187
188 if (len) {
189 int word;
190
191 word = mv88e6352_read_eeprom_word(ds, offset >> 1);
192 if (word < 0)
193 return word;
194
195 *data++ = word & 0xff;
196
197 offset++;
198 len--;
199 eeprom->len++;
200 }
201
202 return 0;
203}
204
205static int mv88e6352_eeprom_is_readonly(struct dsa_switch *ds)
206{
207 int ret;
208
Andrew Lunn966bce32015-08-08 17:04:50 +0200209 ret = mv88e6xxx_reg_read(ds, REG_GLOBAL2, GLOBAL2_EEPROM_OP);
Guenter Roeck33b43df2014-10-29 10:45:03 -0700210 if (ret < 0)
211 return ret;
212
Andrew Lunn966bce32015-08-08 17:04:50 +0200213 if (!(ret & GLOBAL2_EEPROM_OP_WRITE_EN))
Guenter Roeck33b43df2014-10-29 10:45:03 -0700214 return -EROFS;
215
216 return 0;
217}
218
219static int mv88e6352_write_eeprom_word(struct dsa_switch *ds, int addr,
220 u16 data)
221{
222 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
223 int ret;
224
225 mutex_lock(&ps->eeprom_mutex);
226
Andrew Lunn966bce32015-08-08 17:04:50 +0200227 ret = mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_EEPROM_DATA, data);
Guenter Roeck33b43df2014-10-29 10:45:03 -0700228 if (ret < 0)
229 goto error;
230
Andrew Lunn966bce32015-08-08 17:04:50 +0200231 ret = mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
232 GLOBAL2_EEPROM_OP_WRITE |
233 (addr & GLOBAL2_EEPROM_OP_ADDR_MASK));
Guenter Roeck33b43df2014-10-29 10:45:03 -0700234 if (ret < 0)
235 goto error;
236
Andrew Lunnf3044682015-02-14 19:17:50 +0100237 ret = mv88e6xxx_eeprom_busy_wait(ds);
Guenter Roeck33b43df2014-10-29 10:45:03 -0700238error:
239 mutex_unlock(&ps->eeprom_mutex);
240 return ret;
241}
242
243static int mv88e6352_set_eeprom(struct dsa_switch *ds,
244 struct ethtool_eeprom *eeprom, u8 *data)
245{
246 int offset;
247 int ret;
248 int len;
249
250 if (eeprom->magic != 0xc3ec4951)
251 return -EINVAL;
252
253 ret = mv88e6352_eeprom_is_readonly(ds);
254 if (ret)
255 return ret;
256
257 offset = eeprom->offset;
258 len = eeprom->len;
259 eeprom->len = 0;
260
Andrew Lunnf3044682015-02-14 19:17:50 +0100261 ret = mv88e6xxx_eeprom_load_wait(ds);
Guenter Roeck33b43df2014-10-29 10:45:03 -0700262 if (ret < 0)
263 return ret;
264
265 if (offset & 1) {
266 int word;
267
268 word = mv88e6352_read_eeprom_word(ds, offset >> 1);
269 if (word < 0)
270 return word;
271
272 word = (*data++ << 8) | (word & 0xff);
273
274 ret = mv88e6352_write_eeprom_word(ds, offset >> 1, word);
275 if (ret < 0)
276 return ret;
277
278 offset++;
279 len--;
280 eeprom->len++;
281 }
282
283 while (len >= 2) {
284 int word;
285
286 word = *data++;
287 word |= *data++ << 8;
288
289 ret = mv88e6352_write_eeprom_word(ds, offset >> 1, word);
290 if (ret < 0)
291 return ret;
292
293 offset += 2;
294 len -= 2;
295 eeprom->len += 2;
296 }
297
298 if (len) {
299 int word;
300
301 word = mv88e6352_read_eeprom_word(ds, offset >> 1);
302 if (word < 0)
303 return word;
304
305 word = (word & 0xff00) | *data++;
306
307 ret = mv88e6352_write_eeprom_word(ds, offset >> 1, word);
308 if (ret < 0)
309 return ret;
310
311 offset++;
312 len--;
313 eeprom->len++;
314 }
315
316 return 0;
317}
318
Guenter Roeck3ad50cc2014-10-29 10:44:56 -0700319struct dsa_switch_driver mv88e6352_switch_driver = {
320 .tag_protocol = DSA_TAG_PROTO_EDSA,
321 .priv_size = sizeof(struct mv88e6xxx_priv_state),
322 .probe = mv88e6352_probe,
323 .setup = mv88e6352_setup,
324 .set_addr = mv88e6xxx_set_addr_indirect,
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +0200325 .phy_read = mv88e6xxx_phy_read_indirect,
326 .phy_write = mv88e6xxx_phy_write_indirect,
Guenter Roeck3ad50cc2014-10-29 10:44:56 -0700327 .poll_link = mv88e6xxx_poll_link,
Andrew Lunne413e7e2015-04-02 04:06:38 +0200328 .get_strings = mv88e6xxx_get_strings,
329 .get_ethtool_stats = mv88e6xxx_get_ethtool_stats,
330 .get_sset_count = mv88e6xxx_get_sset_count,
Andrew Lunndea87022015-08-31 15:56:47 +0200331 .adjust_link = mv88e6xxx_adjust_link,
Guenter Roeck04b0a802015-03-06 22:23:52 -0800332 .set_eee = mv88e6xxx_set_eee,
333 .get_eee = mv88e6xxx_get_eee,
Guenter Roeck276db3b2014-10-29 10:44:59 -0700334#ifdef CONFIG_NET_DSA_HWMON
Guenter Roeckc22995c2015-07-25 09:42:28 -0700335 .get_temp = mv88e6xxx_get_temp,
336 .get_temp_limit = mv88e6xxx_get_temp_limit,
337 .set_temp_limit = mv88e6xxx_set_temp_limit,
338 .get_temp_alarm = mv88e6xxx_get_temp_alarm,
Guenter Roeck276db3b2014-10-29 10:44:59 -0700339#endif
Guenter Roeck33b43df2014-10-29 10:45:03 -0700340 .get_eeprom = mv88e6352_get_eeprom,
341 .set_eeprom = mv88e6352_set_eeprom,
Guenter Roeck95d08b52014-10-29 10:45:06 -0700342 .get_regs_len = mv88e6xxx_get_regs_len,
343 .get_regs = mv88e6xxx_get_regs,
Guenter Roeck3f244ab2015-03-26 18:36:36 -0700344 .port_join_bridge = mv88e6xxx_join_bridge,
345 .port_leave_bridge = mv88e6xxx_leave_bridge,
346 .port_stp_update = mv88e6xxx_port_stp_update,
Vivien Didelotb8fee952015-08-13 12:52:19 -0400347 .port_pvid_get = mv88e6xxx_port_pvid_get,
Vivien Didelot0d3b33e2015-08-13 12:52:22 -0400348 .port_pvid_set = mv88e6xxx_port_pvid_set,
349 .port_vlan_add = mv88e6xxx_port_vlan_add,
Vivien Didelot7dad08d2015-08-13 12:52:21 -0400350 .port_vlan_del = mv88e6xxx_port_vlan_del,
Vivien Didelotb8fee952015-08-13 12:52:19 -0400351 .vlan_getnext = mv88e6xxx_vlan_getnext,
Vivien Didelot2a778e12015-08-10 09:09:49 -0400352 .port_fdb_add = mv88e6xxx_port_fdb_add,
353 .port_fdb_del = mv88e6xxx_port_fdb_del,
354 .port_fdb_getnext = mv88e6xxx_port_fdb_getnext,
Guenter Roeck3ad50cc2014-10-29 10:44:56 -0700355};
356
Andrew Lunn1636d882015-05-06 01:09:50 +0200357MODULE_ALIAS("platform:mv88e6172");
Aleksey S. Kazantsev7c3d0d62015-07-07 20:38:15 -0700358MODULE_ALIAS("platform:mv88e6176");
359MODULE_ALIAS("platform:mv88e6320");
360MODULE_ALIAS("platform:mv88e6321");
361MODULE_ALIAS("platform:mv88e6352");