blob: e9aca7f7945e3482bbcaceca2d2c7ef168afa077 [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 Lunncca8b132015-04-02 04:06:39 +020035 if ((ret & 0xfff0) == PORT_SWITCH_ID_6176)
Guenter Roeck27167772014-10-29 10:44:57 -070036 return "Marvell 88E6176";
Andrew Lunncca8b132015-04-02 04:06:39 +020037 if (ret == PORT_SWITCH_ID_6352_A0)
Guenter Roeck3ad50cc2014-10-29 10:44:56 -070038 return "Marvell 88E6352 (A0)";
Andrew Lunncca8b132015-04-02 04:06:39 +020039 if (ret == PORT_SWITCH_ID_6352_A1)
Guenter Roeck3ad50cc2014-10-29 10:44:56 -070040 return "Marvell 88E6352 (A1)";
Andrew Lunncca8b132015-04-02 04:06:39 +020041 if ((ret & 0xfff0) == PORT_SWITCH_ID_6352)
Guenter Roeck3ad50cc2014-10-29 10:44:56 -070042 return "Marvell 88E6352";
43 }
44
45 return NULL;
46}
47
Guenter Roeck3ad50cc2014-10-29 10:44:56 -070048static int mv88e6352_setup_global(struct dsa_switch *ds)
49{
Andrew Lunn15966a22015-05-06 01:09:49 +020050 u32 upstream_port = dsa_upstream_port(ds);
Guenter Roeck3ad50cc2014-10-29 10:44:56 -070051 int ret;
Andrew Lunn15966a22015-05-06 01:09:49 +020052 u32 reg;
Andrew Lunn54d792f2015-05-06 01:09:47 +020053
54 ret = mv88e6xxx_setup_global(ds);
55 if (ret)
56 return ret;
Guenter Roeck3ad50cc2014-10-29 10:44:56 -070057
58 /* Discard packets with excessive collisions,
59 * mask all interrupt sources, enable PPU (bit 14, undocumented).
60 */
Andrew Lunn15966a22015-05-06 01:09:49 +020061 REG_WRITE(REG_GLOBAL, GLOBAL_CONTROL,
62 GLOBAL_CONTROL_PPU_ENABLE | GLOBAL_CONTROL_DISCARD_EXCESS);
Guenter Roeck3ad50cc2014-10-29 10:44:56 -070063
Guenter Roeck3ad50cc2014-10-29 10:44:56 -070064 /* Configure the upstream port, and configure the upstream
65 * port as the port to which ingress and egress monitor frames
66 * are to be sent.
67 */
Andrew Lunn15966a22015-05-06 01:09:49 +020068 reg = upstream_port << GLOBAL_MONITOR_CONTROL_INGRESS_SHIFT |
69 upstream_port << GLOBAL_MONITOR_CONTROL_EGRESS_SHIFT |
70 upstream_port << GLOBAL_MONITOR_CONTROL_ARP_SHIFT;
71 REG_WRITE(REG_GLOBAL, GLOBAL_MONITOR_CONTROL, reg);
Guenter Roeck3ad50cc2014-10-29 10:44:56 -070072
73 /* Disable remote management for now, and set the switch's
74 * DSA device number.
75 */
76 REG_WRITE(REG_GLOBAL, 0x1c, ds->index & 0x1f);
77
Guenter Roeck3ad50cc2014-10-29 10:44:56 -070078 return 0;
79}
80
Guenter Roeck276db3b2014-10-29 10:44:59 -070081#ifdef CONFIG_NET_DSA_HWMON
82
Guenter Roeck276db3b2014-10-29 10:44:59 -070083static int mv88e6352_get_temp(struct dsa_switch *ds, int *temp)
84{
85 int ret;
86
87 *temp = 0;
88
Andrew Lunn491435852015-04-02 04:06:35 +020089 ret = mv88e6xxx_phy_page_read(ds, 0, 6, 27);
Guenter Roeck276db3b2014-10-29 10:44:59 -070090 if (ret < 0)
91 return ret;
92
93 *temp = (ret & 0xff) - 25;
94
95 return 0;
96}
97
98static int mv88e6352_get_temp_limit(struct dsa_switch *ds, int *temp)
99{
100 int ret;
101
102 *temp = 0;
103
Andrew Lunn491435852015-04-02 04:06:35 +0200104 ret = mv88e6xxx_phy_page_read(ds, 0, 6, 26);
Guenter Roeck276db3b2014-10-29 10:44:59 -0700105 if (ret < 0)
106 return ret;
107
108 *temp = (((ret >> 8) & 0x1f) * 5) - 25;
109
110 return 0;
111}
112
113static int mv88e6352_set_temp_limit(struct dsa_switch *ds, int temp)
114{
115 int ret;
116
Andrew Lunn491435852015-04-02 04:06:35 +0200117 ret = mv88e6xxx_phy_page_read(ds, 0, 6, 26);
Guenter Roeck276db3b2014-10-29 10:44:59 -0700118 if (ret < 0)
119 return ret;
120 temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
Andrew Lunn491435852015-04-02 04:06:35 +0200121 return mv88e6xxx_phy_page_write(ds, 0, 6, 26,
Guenter Roeck276db3b2014-10-29 10:44:59 -0700122 (ret & 0xe0ff) | (temp << 8));
123}
124
125static int mv88e6352_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
126{
127 int ret;
128
129 *alarm = false;
130
Andrew Lunn491435852015-04-02 04:06:35 +0200131 ret = mv88e6xxx_phy_page_read(ds, 0, 6, 26);
Guenter Roeck276db3b2014-10-29 10:44:59 -0700132 if (ret < 0)
133 return ret;
134
135 *alarm = !!(ret & 0x40);
136
137 return 0;
138}
139#endif /* CONFIG_NET_DSA_HWMON */
140
Guenter Roeck3ad50cc2014-10-29 10:44:56 -0700141static int mv88e6352_setup(struct dsa_switch *ds)
142{
143 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
144 int ret;
Guenter Roeck3ad50cc2014-10-29 10:44:56 -0700145
Guenter Roeckacdaffc2015-03-26 18:36:28 -0700146 ret = mv88e6xxx_setup_common(ds);
147 if (ret < 0)
148 return ret;
149
Andrew Lunn44e50dd2015-04-02 04:06:33 +0200150 ps->num_ports = 7;
151
Guenter Roeck33b43df2014-10-29 10:45:03 -0700152 mutex_init(&ps->eeprom_mutex);
Guenter Roeck3ad50cc2014-10-29 10:44:56 -0700153
Andrew Lunn143a8302015-04-02 04:06:34 +0200154 ret = mv88e6xxx_switch_reset(ds, true);
Guenter Roeck3ad50cc2014-10-29 10:44:56 -0700155 if (ret < 0)
156 return ret;
157
Guenter Roeck3ad50cc2014-10-29 10:44:56 -0700158 ret = mv88e6352_setup_global(ds);
159 if (ret < 0)
160 return ret;
161
Andrew Lunndbde9e62015-05-06 01:09:48 +0200162 return mv88e6xxx_setup_ports(ds);
Guenter Roeck3ad50cc2014-10-29 10:44:56 -0700163}
164
Guenter Roeck33b43df2014-10-29 10:45:03 -0700165static int mv88e6352_read_eeprom_word(struct dsa_switch *ds, int addr)
166{
167 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
168 int ret;
169
170 mutex_lock(&ps->eeprom_mutex);
171
172 ret = mv88e6xxx_reg_write(ds, REG_GLOBAL2, 0x14,
173 0xc000 | (addr & 0xff));
174 if (ret < 0)
175 goto error;
176
Andrew Lunnf3044682015-02-14 19:17:50 +0100177 ret = mv88e6xxx_eeprom_busy_wait(ds);
Guenter Roeck33b43df2014-10-29 10:45:03 -0700178 if (ret < 0)
179 goto error;
180
181 ret = mv88e6xxx_reg_read(ds, REG_GLOBAL2, 0x15);
182error:
183 mutex_unlock(&ps->eeprom_mutex);
184 return ret;
185}
186
187static int mv88e6352_get_eeprom(struct dsa_switch *ds,
188 struct ethtool_eeprom *eeprom, u8 *data)
189{
190 int offset;
191 int len;
192 int ret;
193
194 offset = eeprom->offset;
195 len = eeprom->len;
196 eeprom->len = 0;
197
198 eeprom->magic = 0xc3ec4951;
199
Andrew Lunnf3044682015-02-14 19:17:50 +0100200 ret = mv88e6xxx_eeprom_load_wait(ds);
Guenter Roeck33b43df2014-10-29 10:45:03 -0700201 if (ret < 0)
202 return ret;
203
204 if (offset & 1) {
205 int word;
206
207 word = mv88e6352_read_eeprom_word(ds, offset >> 1);
208 if (word < 0)
209 return word;
210
211 *data++ = (word >> 8) & 0xff;
212
213 offset++;
214 len--;
215 eeprom->len++;
216 }
217
218 while (len >= 2) {
219 int word;
220
221 word = mv88e6352_read_eeprom_word(ds, offset >> 1);
222 if (word < 0)
223 return word;
224
225 *data++ = word & 0xff;
226 *data++ = (word >> 8) & 0xff;
227
228 offset += 2;
229 len -= 2;
230 eeprom->len += 2;
231 }
232
233 if (len) {
234 int word;
235
236 word = mv88e6352_read_eeprom_word(ds, offset >> 1);
237 if (word < 0)
238 return word;
239
240 *data++ = word & 0xff;
241
242 offset++;
243 len--;
244 eeprom->len++;
245 }
246
247 return 0;
248}
249
250static int mv88e6352_eeprom_is_readonly(struct dsa_switch *ds)
251{
252 int ret;
253
254 ret = mv88e6xxx_reg_read(ds, REG_GLOBAL2, 0x14);
255 if (ret < 0)
256 return ret;
257
258 if (!(ret & 0x0400))
259 return -EROFS;
260
261 return 0;
262}
263
264static int mv88e6352_write_eeprom_word(struct dsa_switch *ds, int addr,
265 u16 data)
266{
267 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
268 int ret;
269
270 mutex_lock(&ps->eeprom_mutex);
271
272 ret = mv88e6xxx_reg_write(ds, REG_GLOBAL2, 0x15, data);
273 if (ret < 0)
274 goto error;
275
276 ret = mv88e6xxx_reg_write(ds, REG_GLOBAL2, 0x14,
277 0xb000 | (addr & 0xff));
278 if (ret < 0)
279 goto error;
280
Andrew Lunnf3044682015-02-14 19:17:50 +0100281 ret = mv88e6xxx_eeprom_busy_wait(ds);
Guenter Roeck33b43df2014-10-29 10:45:03 -0700282error:
283 mutex_unlock(&ps->eeprom_mutex);
284 return ret;
285}
286
287static int mv88e6352_set_eeprom(struct dsa_switch *ds,
288 struct ethtool_eeprom *eeprom, u8 *data)
289{
290 int offset;
291 int ret;
292 int len;
293
294 if (eeprom->magic != 0xc3ec4951)
295 return -EINVAL;
296
297 ret = mv88e6352_eeprom_is_readonly(ds);
298 if (ret)
299 return ret;
300
301 offset = eeprom->offset;
302 len = eeprom->len;
303 eeprom->len = 0;
304
Andrew Lunnf3044682015-02-14 19:17:50 +0100305 ret = mv88e6xxx_eeprom_load_wait(ds);
Guenter Roeck33b43df2014-10-29 10:45:03 -0700306 if (ret < 0)
307 return ret;
308
309 if (offset & 1) {
310 int word;
311
312 word = mv88e6352_read_eeprom_word(ds, offset >> 1);
313 if (word < 0)
314 return word;
315
316 word = (*data++ << 8) | (word & 0xff);
317
318 ret = mv88e6352_write_eeprom_word(ds, offset >> 1, word);
319 if (ret < 0)
320 return ret;
321
322 offset++;
323 len--;
324 eeprom->len++;
325 }
326
327 while (len >= 2) {
328 int word;
329
330 word = *data++;
331 word |= *data++ << 8;
332
333 ret = mv88e6352_write_eeprom_word(ds, offset >> 1, word);
334 if (ret < 0)
335 return ret;
336
337 offset += 2;
338 len -= 2;
339 eeprom->len += 2;
340 }
341
342 if (len) {
343 int word;
344
345 word = mv88e6352_read_eeprom_word(ds, offset >> 1);
346 if (word < 0)
347 return word;
348
349 word = (word & 0xff00) | *data++;
350
351 ret = mv88e6352_write_eeprom_word(ds, offset >> 1, word);
352 if (ret < 0)
353 return ret;
354
355 offset++;
356 len--;
357 eeprom->len++;
358 }
359
360 return 0;
361}
362
Guenter Roeck3ad50cc2014-10-29 10:44:56 -0700363struct dsa_switch_driver mv88e6352_switch_driver = {
364 .tag_protocol = DSA_TAG_PROTO_EDSA,
365 .priv_size = sizeof(struct mv88e6xxx_priv_state),
366 .probe = mv88e6352_probe,
367 .setup = mv88e6352_setup,
368 .set_addr = mv88e6xxx_set_addr_indirect,
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +0200369 .phy_read = mv88e6xxx_phy_read_indirect,
370 .phy_write = mv88e6xxx_phy_write_indirect,
Guenter Roeck3ad50cc2014-10-29 10:44:56 -0700371 .poll_link = mv88e6xxx_poll_link,
Andrew Lunne413e7e2015-04-02 04:06:38 +0200372 .get_strings = mv88e6xxx_get_strings,
373 .get_ethtool_stats = mv88e6xxx_get_ethtool_stats,
374 .get_sset_count = mv88e6xxx_get_sset_count,
Guenter Roeck04b0a802015-03-06 22:23:52 -0800375 .set_eee = mv88e6xxx_set_eee,
376 .get_eee = mv88e6xxx_get_eee,
Guenter Roeck276db3b2014-10-29 10:44:59 -0700377#ifdef CONFIG_NET_DSA_HWMON
378 .get_temp = mv88e6352_get_temp,
379 .get_temp_limit = mv88e6352_get_temp_limit,
380 .set_temp_limit = mv88e6352_set_temp_limit,
381 .get_temp_alarm = mv88e6352_get_temp_alarm,
382#endif
Guenter Roeck33b43df2014-10-29 10:45:03 -0700383 .get_eeprom = mv88e6352_get_eeprom,
384 .set_eeprom = mv88e6352_set_eeprom,
Guenter Roeck95d08b52014-10-29 10:45:06 -0700385 .get_regs_len = mv88e6xxx_get_regs_len,
386 .get_regs = mv88e6xxx_get_regs,
Guenter Roeck3f244ab2015-03-26 18:36:36 -0700387 .port_join_bridge = mv88e6xxx_join_bridge,
388 .port_leave_bridge = mv88e6xxx_leave_bridge,
389 .port_stp_update = mv88e6xxx_port_stp_update,
Guenter Roeck4f431e52015-03-26 18:36:39 -0700390 .fdb_add = mv88e6xxx_port_fdb_add,
391 .fdb_del = mv88e6xxx_port_fdb_del,
392 .fdb_getnext = mv88e6xxx_port_fdb_getnext,
Guenter Roeck3ad50cc2014-10-29 10:44:56 -0700393};
394
395MODULE_ALIAS("platform:mv88e6352");