Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[firefly-linux-kernel-4.4.55.git] / drivers / net / dsa / mv88e6352.c
1 /*
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
25 static 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
33         ret = __mv88e6xxx_reg_read(bus, sw_addr, REG_PORT(0), PORT_SWITCH_ID);
34         if (ret >= 0) {
35                 if ((ret & 0xfff0) == PORT_SWITCH_ID_6172)
36                         return "Marvell 88E6172";
37                 if ((ret & 0xfff0) == PORT_SWITCH_ID_6176)
38                         return "Marvell 88E6176";
39                 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";
51                 if (ret == PORT_SWITCH_ID_6352_A0)
52                         return "Marvell 88E6352 (A0)";
53                 if (ret == PORT_SWITCH_ID_6352_A1)
54                         return "Marvell 88E6352 (A1)";
55                 if ((ret & 0xfff0) == PORT_SWITCH_ID_6352)
56                         return "Marvell 88E6352";
57         }
58
59         return NULL;
60 }
61
62 static int mv88e6352_setup_global(struct dsa_switch *ds)
63 {
64         u32 upstream_port = dsa_upstream_port(ds);
65         int ret;
66         u32 reg;
67
68         ret = mv88e6xxx_setup_global(ds);
69         if (ret)
70                 return ret;
71
72         /* Discard packets with excessive collisions,
73          * mask all interrupt sources, enable PPU (bit 14, undocumented).
74          */
75         REG_WRITE(REG_GLOBAL, GLOBAL_CONTROL,
76                   GLOBAL_CONTROL_PPU_ENABLE | GLOBAL_CONTROL_DISCARD_EXCESS);
77
78         /* 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          */
82         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);
86
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
92         return 0;
93 }
94
95 #ifdef CONFIG_NET_DSA_HWMON
96
97 static int mv88e6352_get_temp(struct dsa_switch *ds, int *temp)
98 {
99         int phy = mv88e6xxx_6320_family(ds) ? 3 : 0;
100         int ret;
101
102         *temp = 0;
103
104         ret = mv88e6xxx_phy_page_read(ds, phy, 6, 27);
105         if (ret < 0)
106                 return ret;
107
108         *temp = (ret & 0xff) - 25;
109
110         return 0;
111 }
112
113 static int mv88e6352_get_temp_limit(struct dsa_switch *ds, int *temp)
114 {
115         int phy = mv88e6xxx_6320_family(ds) ? 3 : 0;
116         int ret;
117
118         *temp = 0;
119
120         ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26);
121         if (ret < 0)
122                 return ret;
123
124         *temp = (((ret >> 8) & 0x1f) * 5) - 25;
125
126         return 0;
127 }
128
129 static int mv88e6352_set_temp_limit(struct dsa_switch *ds, int temp)
130 {
131         int phy = mv88e6xxx_6320_family(ds) ? 3 : 0;
132         int ret;
133
134         ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26);
135         if (ret < 0)
136                 return ret;
137         temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
138         return mv88e6xxx_phy_page_write(ds, phy, 6, 26,
139                                         (ret & 0xe0ff) | (temp << 8));
140 }
141
142 static int mv88e6352_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
143 {
144         int phy = mv88e6xxx_6320_family(ds) ? 3 : 0;
145         int ret;
146
147         *alarm = false;
148
149         ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26);
150         if (ret < 0)
151                 return ret;
152
153         *alarm = !!(ret & 0x40);
154
155         return 0;
156 }
157 #endif /* CONFIG_NET_DSA_HWMON */
158
159 static int mv88e6352_setup(struct dsa_switch *ds)
160 {
161         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
162         int ret;
163
164         ret = mv88e6xxx_setup_common(ds);
165         if (ret < 0)
166                 return ret;
167
168         ps->num_ports = 7;
169
170         mutex_init(&ps->eeprom_mutex);
171
172         ret = mv88e6xxx_switch_reset(ds, true);
173         if (ret < 0)
174                 return ret;
175
176         ret = mv88e6352_setup_global(ds);
177         if (ret < 0)
178                 return ret;
179
180         return mv88e6xxx_setup_ports(ds);
181 }
182
183 static int mv88e6352_read_eeprom_word(struct dsa_switch *ds, int addr)
184 {
185         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
186         int ret;
187
188         mutex_lock(&ps->eeprom_mutex);
189
190         ret = mv88e6xxx_reg_write(ds, REG_GLOBAL2, 0x14,
191                                   0xc000 | (addr & 0xff));
192         if (ret < 0)
193                 goto error;
194
195         ret = mv88e6xxx_eeprom_busy_wait(ds);
196         if (ret < 0)
197                 goto error;
198
199         ret = mv88e6xxx_reg_read(ds, REG_GLOBAL2, 0x15);
200 error:
201         mutex_unlock(&ps->eeprom_mutex);
202         return ret;
203 }
204
205 static int mv88e6352_get_eeprom(struct dsa_switch *ds,
206                                 struct ethtool_eeprom *eeprom, u8 *data)
207 {
208         int offset;
209         int len;
210         int ret;
211
212         offset = eeprom->offset;
213         len = eeprom->len;
214         eeprom->len = 0;
215
216         eeprom->magic = 0xc3ec4951;
217
218         ret = mv88e6xxx_eeprom_load_wait(ds);
219         if (ret < 0)
220                 return ret;
221
222         if (offset & 1) {
223                 int word;
224
225                 word = mv88e6352_read_eeprom_word(ds, offset >> 1);
226                 if (word < 0)
227                         return word;
228
229                 *data++ = (word >> 8) & 0xff;
230
231                 offset++;
232                 len--;
233                 eeprom->len++;
234         }
235
236         while (len >= 2) {
237                 int word;
238
239                 word = mv88e6352_read_eeprom_word(ds, offset >> 1);
240                 if (word < 0)
241                         return word;
242
243                 *data++ = word & 0xff;
244                 *data++ = (word >> 8) & 0xff;
245
246                 offset += 2;
247                 len -= 2;
248                 eeprom->len += 2;
249         }
250
251         if (len) {
252                 int word;
253
254                 word = mv88e6352_read_eeprom_word(ds, offset >> 1);
255                 if (word < 0)
256                         return word;
257
258                 *data++ = word & 0xff;
259
260                 offset++;
261                 len--;
262                 eeprom->len++;
263         }
264
265         return 0;
266 }
267
268 static int mv88e6352_eeprom_is_readonly(struct dsa_switch *ds)
269 {
270         int ret;
271
272         ret = mv88e6xxx_reg_read(ds, REG_GLOBAL2, 0x14);
273         if (ret < 0)
274                 return ret;
275
276         if (!(ret & 0x0400))
277                 return -EROFS;
278
279         return 0;
280 }
281
282 static int mv88e6352_write_eeprom_word(struct dsa_switch *ds, int addr,
283                                        u16 data)
284 {
285         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
286         int ret;
287
288         mutex_lock(&ps->eeprom_mutex);
289
290         ret = mv88e6xxx_reg_write(ds, REG_GLOBAL2, 0x15, data);
291         if (ret < 0)
292                 goto error;
293
294         ret = mv88e6xxx_reg_write(ds, REG_GLOBAL2, 0x14,
295                                   0xb000 | (addr & 0xff));
296         if (ret < 0)
297                 goto error;
298
299         ret = mv88e6xxx_eeprom_busy_wait(ds);
300 error:
301         mutex_unlock(&ps->eeprom_mutex);
302         return ret;
303 }
304
305 static int mv88e6352_set_eeprom(struct dsa_switch *ds,
306                                 struct ethtool_eeprom *eeprom, u8 *data)
307 {
308         int offset;
309         int ret;
310         int len;
311
312         if (eeprom->magic != 0xc3ec4951)
313                 return -EINVAL;
314
315         ret = mv88e6352_eeprom_is_readonly(ds);
316         if (ret)
317                 return ret;
318
319         offset = eeprom->offset;
320         len = eeprom->len;
321         eeprom->len = 0;
322
323         ret = mv88e6xxx_eeprom_load_wait(ds);
324         if (ret < 0)
325                 return ret;
326
327         if (offset & 1) {
328                 int word;
329
330                 word = mv88e6352_read_eeprom_word(ds, offset >> 1);
331                 if (word < 0)
332                         return word;
333
334                 word = (*data++ << 8) | (word & 0xff);
335
336                 ret = mv88e6352_write_eeprom_word(ds, offset >> 1, word);
337                 if (ret < 0)
338                         return ret;
339
340                 offset++;
341                 len--;
342                 eeprom->len++;
343         }
344
345         while (len >= 2) {
346                 int word;
347
348                 word = *data++;
349                 word |= *data++ << 8;
350
351                 ret = mv88e6352_write_eeprom_word(ds, offset >> 1, word);
352                 if (ret < 0)
353                         return ret;
354
355                 offset += 2;
356                 len -= 2;
357                 eeprom->len += 2;
358         }
359
360         if (len) {
361                 int word;
362
363                 word = mv88e6352_read_eeprom_word(ds, offset >> 1);
364                 if (word < 0)
365                         return word;
366
367                 word = (word & 0xff00) | *data++;
368
369                 ret = mv88e6352_write_eeprom_word(ds, offset >> 1, word);
370                 if (ret < 0)
371                         return ret;
372
373                 offset++;
374                 len--;
375                 eeprom->len++;
376         }
377
378         return 0;
379 }
380
381 struct dsa_switch_driver mv88e6352_switch_driver = {
382         .tag_protocol           = DSA_TAG_PROTO_EDSA,
383         .priv_size              = sizeof(struct mv88e6xxx_priv_state),
384         .probe                  = mv88e6352_probe,
385         .setup                  = mv88e6352_setup,
386         .set_addr               = mv88e6xxx_set_addr_indirect,
387         .phy_read               = mv88e6xxx_phy_read_indirect,
388         .phy_write              = mv88e6xxx_phy_write_indirect,
389         .poll_link              = mv88e6xxx_poll_link,
390         .get_strings            = mv88e6xxx_get_strings,
391         .get_ethtool_stats      = mv88e6xxx_get_ethtool_stats,
392         .get_sset_count         = mv88e6xxx_get_sset_count,
393         .set_eee                = mv88e6xxx_set_eee,
394         .get_eee                = mv88e6xxx_get_eee,
395 #ifdef CONFIG_NET_DSA_HWMON
396         .get_temp               = mv88e6352_get_temp,
397         .get_temp_limit         = mv88e6352_get_temp_limit,
398         .set_temp_limit         = mv88e6352_set_temp_limit,
399         .get_temp_alarm         = mv88e6352_get_temp_alarm,
400 #endif
401         .get_eeprom             = mv88e6352_get_eeprom,
402         .set_eeprom             = mv88e6352_set_eeprom,
403         .get_regs_len           = mv88e6xxx_get_regs_len,
404         .get_regs               = mv88e6xxx_get_regs,
405         .port_join_bridge       = mv88e6xxx_join_bridge,
406         .port_leave_bridge      = mv88e6xxx_leave_bridge,
407         .port_stp_update        = mv88e6xxx_port_stp_update,
408         .fdb_add                = mv88e6xxx_port_fdb_add,
409         .fdb_del                = mv88e6xxx_port_fdb_del,
410         .fdb_getnext            = mv88e6xxx_port_fdb_getnext,
411 };
412
413 MODULE_ALIAS("platform:mv88e6172");
414 MODULE_ALIAS("platform:mv88e6176");
415 MODULE_ALIAS("platform:mv88e6320");
416 MODULE_ALIAS("platform:mv88e6321");
417 MODULE_ALIAS("platform:mv88e6352");