blob: 41d113749878a85bf8796575cff51611576e2250 [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{
50 int ret;
Andrew Lunn54d792f2015-05-06 01:09:47 +020051
52 ret = mv88e6xxx_setup_global(ds);
53 if (ret)
54 return ret;
Guenter Roeck3ad50cc2014-10-29 10:44:56 -070055
56 /* Discard packets with excessive collisions,
57 * mask all interrupt sources, enable PPU (bit 14, undocumented).
58 */
59 REG_WRITE(REG_GLOBAL, 0x04, 0x6000);
60
Guenter Roeck3ad50cc2014-10-29 10:44:56 -070061 /* Configure the upstream port, and configure the upstream
62 * port as the port to which ingress and egress monitor frames
63 * are to be sent.
64 */
65 REG_WRITE(REG_GLOBAL, 0x1a, (dsa_upstream_port(ds) * 0x1110));
66
67 /* Disable remote management for now, and set the switch's
68 * DSA device number.
69 */
70 REG_WRITE(REG_GLOBAL, 0x1c, ds->index & 0x1f);
71
Guenter Roeck3ad50cc2014-10-29 10:44:56 -070072 return 0;
73}
74
Guenter Roeck276db3b2014-10-29 10:44:59 -070075#ifdef CONFIG_NET_DSA_HWMON
76
Guenter Roeck276db3b2014-10-29 10:44:59 -070077static int mv88e6352_get_temp(struct dsa_switch *ds, int *temp)
78{
79 int ret;
80
81 *temp = 0;
82
Andrew Lunn491435852015-04-02 04:06:35 +020083 ret = mv88e6xxx_phy_page_read(ds, 0, 6, 27);
Guenter Roeck276db3b2014-10-29 10:44:59 -070084 if (ret < 0)
85 return ret;
86
87 *temp = (ret & 0xff) - 25;
88
89 return 0;
90}
91
92static int mv88e6352_get_temp_limit(struct dsa_switch *ds, int *temp)
93{
94 int ret;
95
96 *temp = 0;
97
Andrew Lunn491435852015-04-02 04:06:35 +020098 ret = mv88e6xxx_phy_page_read(ds, 0, 6, 26);
Guenter Roeck276db3b2014-10-29 10:44:59 -070099 if (ret < 0)
100 return ret;
101
102 *temp = (((ret >> 8) & 0x1f) * 5) - 25;
103
104 return 0;
105}
106
107static int mv88e6352_set_temp_limit(struct dsa_switch *ds, int temp)
108{
109 int ret;
110
Andrew Lunn491435852015-04-02 04:06:35 +0200111 ret = mv88e6xxx_phy_page_read(ds, 0, 6, 26);
Guenter Roeck276db3b2014-10-29 10:44:59 -0700112 if (ret < 0)
113 return ret;
114 temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
Andrew Lunn491435852015-04-02 04:06:35 +0200115 return mv88e6xxx_phy_page_write(ds, 0, 6, 26,
Guenter Roeck276db3b2014-10-29 10:44:59 -0700116 (ret & 0xe0ff) | (temp << 8));
117}
118
119static int mv88e6352_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
120{
121 int ret;
122
123 *alarm = false;
124
Andrew Lunn491435852015-04-02 04:06:35 +0200125 ret = mv88e6xxx_phy_page_read(ds, 0, 6, 26);
Guenter Roeck276db3b2014-10-29 10:44:59 -0700126 if (ret < 0)
127 return ret;
128
129 *alarm = !!(ret & 0x40);
130
131 return 0;
132}
133#endif /* CONFIG_NET_DSA_HWMON */
134
Guenter Roeck3ad50cc2014-10-29 10:44:56 -0700135static int mv88e6352_setup(struct dsa_switch *ds)
136{
137 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
138 int ret;
Guenter Roeck3ad50cc2014-10-29 10:44:56 -0700139
Guenter Roeckacdaffc2015-03-26 18:36:28 -0700140 ret = mv88e6xxx_setup_common(ds);
141 if (ret < 0)
142 return ret;
143
Andrew Lunn44e50dd2015-04-02 04:06:33 +0200144 ps->num_ports = 7;
145
Guenter Roeck33b43df2014-10-29 10:45:03 -0700146 mutex_init(&ps->eeprom_mutex);
Guenter Roeck3ad50cc2014-10-29 10:44:56 -0700147
Andrew Lunn143a8302015-04-02 04:06:34 +0200148 ret = mv88e6xxx_switch_reset(ds, true);
Guenter Roeck3ad50cc2014-10-29 10:44:56 -0700149 if (ret < 0)
150 return ret;
151
Guenter Roeck3ad50cc2014-10-29 10:44:56 -0700152 ret = mv88e6352_setup_global(ds);
153 if (ret < 0)
154 return ret;
155
Andrew Lunndbde9e62015-05-06 01:09:48 +0200156 return mv88e6xxx_setup_ports(ds);
Guenter Roeck3ad50cc2014-10-29 10:44:56 -0700157}
158
Guenter Roeck33b43df2014-10-29 10:45:03 -0700159static int mv88e6352_read_eeprom_word(struct dsa_switch *ds, int addr)
160{
161 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
162 int ret;
163
164 mutex_lock(&ps->eeprom_mutex);
165
166 ret = mv88e6xxx_reg_write(ds, REG_GLOBAL2, 0x14,
167 0xc000 | (addr & 0xff));
168 if (ret < 0)
169 goto error;
170
Andrew Lunnf3044682015-02-14 19:17:50 +0100171 ret = mv88e6xxx_eeprom_busy_wait(ds);
Guenter Roeck33b43df2014-10-29 10:45:03 -0700172 if (ret < 0)
173 goto error;
174
175 ret = mv88e6xxx_reg_read(ds, REG_GLOBAL2, 0x15);
176error:
177 mutex_unlock(&ps->eeprom_mutex);
178 return ret;
179}
180
181static int mv88e6352_get_eeprom(struct dsa_switch *ds,
182 struct ethtool_eeprom *eeprom, u8 *data)
183{
184 int offset;
185 int len;
186 int ret;
187
188 offset = eeprom->offset;
189 len = eeprom->len;
190 eeprom->len = 0;
191
192 eeprom->magic = 0xc3ec4951;
193
Andrew Lunnf3044682015-02-14 19:17:50 +0100194 ret = mv88e6xxx_eeprom_load_wait(ds);
Guenter Roeck33b43df2014-10-29 10:45:03 -0700195 if (ret < 0)
196 return ret;
197
198 if (offset & 1) {
199 int word;
200
201 word = mv88e6352_read_eeprom_word(ds, offset >> 1);
202 if (word < 0)
203 return word;
204
205 *data++ = (word >> 8) & 0xff;
206
207 offset++;
208 len--;
209 eeprom->len++;
210 }
211
212 while (len >= 2) {
213 int word;
214
215 word = mv88e6352_read_eeprom_word(ds, offset >> 1);
216 if (word < 0)
217 return word;
218
219 *data++ = word & 0xff;
220 *data++ = (word >> 8) & 0xff;
221
222 offset += 2;
223 len -= 2;
224 eeprom->len += 2;
225 }
226
227 if (len) {
228 int word;
229
230 word = mv88e6352_read_eeprom_word(ds, offset >> 1);
231 if (word < 0)
232 return word;
233
234 *data++ = word & 0xff;
235
236 offset++;
237 len--;
238 eeprom->len++;
239 }
240
241 return 0;
242}
243
244static int mv88e6352_eeprom_is_readonly(struct dsa_switch *ds)
245{
246 int ret;
247
248 ret = mv88e6xxx_reg_read(ds, REG_GLOBAL2, 0x14);
249 if (ret < 0)
250 return ret;
251
252 if (!(ret & 0x0400))
253 return -EROFS;
254
255 return 0;
256}
257
258static int mv88e6352_write_eeprom_word(struct dsa_switch *ds, int addr,
259 u16 data)
260{
261 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
262 int ret;
263
264 mutex_lock(&ps->eeprom_mutex);
265
266 ret = mv88e6xxx_reg_write(ds, REG_GLOBAL2, 0x15, data);
267 if (ret < 0)
268 goto error;
269
270 ret = mv88e6xxx_reg_write(ds, REG_GLOBAL2, 0x14,
271 0xb000 | (addr & 0xff));
272 if (ret < 0)
273 goto error;
274
Andrew Lunnf3044682015-02-14 19:17:50 +0100275 ret = mv88e6xxx_eeprom_busy_wait(ds);
Guenter Roeck33b43df2014-10-29 10:45:03 -0700276error:
277 mutex_unlock(&ps->eeprom_mutex);
278 return ret;
279}
280
281static int mv88e6352_set_eeprom(struct dsa_switch *ds,
282 struct ethtool_eeprom *eeprom, u8 *data)
283{
284 int offset;
285 int ret;
286 int len;
287
288 if (eeprom->magic != 0xc3ec4951)
289 return -EINVAL;
290
291 ret = mv88e6352_eeprom_is_readonly(ds);
292 if (ret)
293 return ret;
294
295 offset = eeprom->offset;
296 len = eeprom->len;
297 eeprom->len = 0;
298
Andrew Lunnf3044682015-02-14 19:17:50 +0100299 ret = mv88e6xxx_eeprom_load_wait(ds);
Guenter Roeck33b43df2014-10-29 10:45:03 -0700300 if (ret < 0)
301 return ret;
302
303 if (offset & 1) {
304 int word;
305
306 word = mv88e6352_read_eeprom_word(ds, offset >> 1);
307 if (word < 0)
308 return word;
309
310 word = (*data++ << 8) | (word & 0xff);
311
312 ret = mv88e6352_write_eeprom_word(ds, offset >> 1, word);
313 if (ret < 0)
314 return ret;
315
316 offset++;
317 len--;
318 eeprom->len++;
319 }
320
321 while (len >= 2) {
322 int word;
323
324 word = *data++;
325 word |= *data++ << 8;
326
327 ret = mv88e6352_write_eeprom_word(ds, offset >> 1, word);
328 if (ret < 0)
329 return ret;
330
331 offset += 2;
332 len -= 2;
333 eeprom->len += 2;
334 }
335
336 if (len) {
337 int word;
338
339 word = mv88e6352_read_eeprom_word(ds, offset >> 1);
340 if (word < 0)
341 return word;
342
343 word = (word & 0xff00) | *data++;
344
345 ret = mv88e6352_write_eeprom_word(ds, offset >> 1, word);
346 if (ret < 0)
347 return ret;
348
349 offset++;
350 len--;
351 eeprom->len++;
352 }
353
354 return 0;
355}
356
Guenter Roeck3ad50cc2014-10-29 10:44:56 -0700357struct dsa_switch_driver mv88e6352_switch_driver = {
358 .tag_protocol = DSA_TAG_PROTO_EDSA,
359 .priv_size = sizeof(struct mv88e6xxx_priv_state),
360 .probe = mv88e6352_probe,
361 .setup = mv88e6352_setup,
362 .set_addr = mv88e6xxx_set_addr_indirect,
Andrew Lunnfd3a0ee2015-04-02 04:06:36 +0200363 .phy_read = mv88e6xxx_phy_read_indirect,
364 .phy_write = mv88e6xxx_phy_write_indirect,
Guenter Roeck3ad50cc2014-10-29 10:44:56 -0700365 .poll_link = mv88e6xxx_poll_link,
Andrew Lunne413e7e2015-04-02 04:06:38 +0200366 .get_strings = mv88e6xxx_get_strings,
367 .get_ethtool_stats = mv88e6xxx_get_ethtool_stats,
368 .get_sset_count = mv88e6xxx_get_sset_count,
Guenter Roeck04b0a802015-03-06 22:23:52 -0800369 .set_eee = mv88e6xxx_set_eee,
370 .get_eee = mv88e6xxx_get_eee,
Guenter Roeck276db3b2014-10-29 10:44:59 -0700371#ifdef CONFIG_NET_DSA_HWMON
372 .get_temp = mv88e6352_get_temp,
373 .get_temp_limit = mv88e6352_get_temp_limit,
374 .set_temp_limit = mv88e6352_set_temp_limit,
375 .get_temp_alarm = mv88e6352_get_temp_alarm,
376#endif
Guenter Roeck33b43df2014-10-29 10:45:03 -0700377 .get_eeprom = mv88e6352_get_eeprom,
378 .set_eeprom = mv88e6352_set_eeprom,
Guenter Roeck95d08b52014-10-29 10:45:06 -0700379 .get_regs_len = mv88e6xxx_get_regs_len,
380 .get_regs = mv88e6xxx_get_regs,
Guenter Roeck3f244ab2015-03-26 18:36:36 -0700381 .port_join_bridge = mv88e6xxx_join_bridge,
382 .port_leave_bridge = mv88e6xxx_leave_bridge,
383 .port_stp_update = mv88e6xxx_port_stp_update,
Guenter Roeck4f431e52015-03-26 18:36:39 -0700384 .fdb_add = mv88e6xxx_port_fdb_add,
385 .fdb_del = mv88e6xxx_port_fdb_del,
386 .fdb_getnext = mv88e6xxx_port_fdb_getnext,
Guenter Roeck3ad50cc2014-10-29 10:44:56 -0700387};
388
389MODULE_ALIAS("platform:mv88e6352");