dsa: mv88e6xxx: Kill the REG_READ and REG_WRITE macros
[cascardo/linux.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 const struct mv88e6xxx_switch_id mv88e6352_table[] = {
26         { PORT_SWITCH_ID_6172, "Marvell 88E6172" },
27         { PORT_SWITCH_ID_6176, "Marvell 88E6176" },
28         { PORT_SWITCH_ID_6240, "Marvell 88E6240" },
29         { PORT_SWITCH_ID_6320, "Marvell 88E6320" },
30         { PORT_SWITCH_ID_6320_A1, "Marvell 88E6320 (A1)" },
31         { PORT_SWITCH_ID_6320_A2, "Marvell 88e6320 (A2)" },
32         { PORT_SWITCH_ID_6321, "Marvell 88E6321" },
33         { PORT_SWITCH_ID_6321_A1, "Marvell 88E6321 (A1)" },
34         { PORT_SWITCH_ID_6321_A2, "Marvell 88e6321 (A2)" },
35         { PORT_SWITCH_ID_6352, "Marvell 88E6352" },
36         { PORT_SWITCH_ID_6352_A0, "Marvell 88E6352 (A0)" },
37         { PORT_SWITCH_ID_6352_A1, "Marvell 88E6352 (A1)" },
38 };
39
40 static char *mv88e6352_drv_probe(struct device *dsa_dev,
41                                  struct device *host_dev,
42                                  int sw_addr, void **priv)
43 {
44         return mv88e6xxx_drv_probe(dsa_dev, host_dev, sw_addr, priv,
45                                    mv88e6352_table,
46                                    ARRAY_SIZE(mv88e6352_table));
47 }
48
49 static int mv88e6352_setup_global(struct dsa_switch *ds)
50 {
51         u32 upstream_port = dsa_upstream_port(ds);
52         int ret;
53         u32 reg;
54
55         ret = mv88e6xxx_setup_global(ds);
56         if (ret)
57                 return ret;
58
59         /* Discard packets with excessive collisions,
60          * mask all interrupt sources, enable PPU (bit 14, undocumented).
61          */
62         ret = mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_CONTROL,
63                                   GLOBAL_CONTROL_PPU_ENABLE |
64                                   GLOBAL_CONTROL_DISCARD_EXCESS);
65         if (ret)
66                 return ret;
67
68         /* Configure the upstream port, and configure the upstream
69          * port as the port to which ingress and egress monitor frames
70          * are to be sent.
71          */
72         reg = upstream_port << GLOBAL_MONITOR_CONTROL_INGRESS_SHIFT |
73                 upstream_port << GLOBAL_MONITOR_CONTROL_EGRESS_SHIFT |
74                 upstream_port << GLOBAL_MONITOR_CONTROL_ARP_SHIFT;
75         ret = mv88e6xxx_reg_write(ds, REG_GLOBAL, GLOBAL_MONITOR_CONTROL, reg);
76         if (ret)
77                 return ret;
78
79         /* Disable remote management for now, and set the switch's
80          * DSA device number.
81          */
82         return mv88e6xxx_reg_write(ds, REG_GLOBAL, 0x1c, ds->index & 0x1f);
83 }
84
85 static int mv88e6352_setup(struct dsa_switch *ds)
86 {
87         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
88         int ret;
89
90         ps->ds = ds;
91
92         ret = mv88e6xxx_setup_common(ds);
93         if (ret < 0)
94                 return ret;
95
96         ps->num_ports = 7;
97
98         mutex_init(&ps->eeprom_mutex);
99
100         ret = mv88e6xxx_switch_reset(ds, true);
101         if (ret < 0)
102                 return ret;
103
104         ret = mv88e6352_setup_global(ds);
105         if (ret < 0)
106                 return ret;
107
108         return mv88e6xxx_setup_ports(ds);
109 }
110
111 static int mv88e6352_read_eeprom_word(struct dsa_switch *ds, int addr)
112 {
113         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
114         int ret;
115
116         mutex_lock(&ps->eeprom_mutex);
117
118         ret = mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
119                                   GLOBAL2_EEPROM_OP_READ |
120                                   (addr & GLOBAL2_EEPROM_OP_ADDR_MASK));
121         if (ret < 0)
122                 goto error;
123
124         ret = mv88e6xxx_eeprom_busy_wait(ds);
125         if (ret < 0)
126                 goto error;
127
128         ret = mv88e6xxx_reg_read(ds, REG_GLOBAL2, GLOBAL2_EEPROM_DATA);
129 error:
130         mutex_unlock(&ps->eeprom_mutex);
131         return ret;
132 }
133
134 static int mv88e6352_get_eeprom(struct dsa_switch *ds,
135                                 struct ethtool_eeprom *eeprom, u8 *data)
136 {
137         int offset;
138         int len;
139         int ret;
140
141         offset = eeprom->offset;
142         len = eeprom->len;
143         eeprom->len = 0;
144
145         eeprom->magic = 0xc3ec4951;
146
147         ret = mv88e6xxx_eeprom_load_wait(ds);
148         if (ret < 0)
149                 return ret;
150
151         if (offset & 1) {
152                 int word;
153
154                 word = mv88e6352_read_eeprom_word(ds, offset >> 1);
155                 if (word < 0)
156                         return word;
157
158                 *data++ = (word >> 8) & 0xff;
159
160                 offset++;
161                 len--;
162                 eeprom->len++;
163         }
164
165         while (len >= 2) {
166                 int word;
167
168                 word = mv88e6352_read_eeprom_word(ds, offset >> 1);
169                 if (word < 0)
170                         return word;
171
172                 *data++ = word & 0xff;
173                 *data++ = (word >> 8) & 0xff;
174
175                 offset += 2;
176                 len -= 2;
177                 eeprom->len += 2;
178         }
179
180         if (len) {
181                 int word;
182
183                 word = mv88e6352_read_eeprom_word(ds, offset >> 1);
184                 if (word < 0)
185                         return word;
186
187                 *data++ = word & 0xff;
188
189                 offset++;
190                 len--;
191                 eeprom->len++;
192         }
193
194         return 0;
195 }
196
197 static int mv88e6352_eeprom_is_readonly(struct dsa_switch *ds)
198 {
199         int ret;
200
201         ret = mv88e6xxx_reg_read(ds, REG_GLOBAL2, GLOBAL2_EEPROM_OP);
202         if (ret < 0)
203                 return ret;
204
205         if (!(ret & GLOBAL2_EEPROM_OP_WRITE_EN))
206                 return -EROFS;
207
208         return 0;
209 }
210
211 static int mv88e6352_write_eeprom_word(struct dsa_switch *ds, int addr,
212                                        u16 data)
213 {
214         struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
215         int ret;
216
217         mutex_lock(&ps->eeprom_mutex);
218
219         ret = mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_EEPROM_DATA, data);
220         if (ret < 0)
221                 goto error;
222
223         ret = mv88e6xxx_reg_write(ds, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
224                                   GLOBAL2_EEPROM_OP_WRITE |
225                                   (addr & GLOBAL2_EEPROM_OP_ADDR_MASK));
226         if (ret < 0)
227                 goto error;
228
229         ret = mv88e6xxx_eeprom_busy_wait(ds);
230 error:
231         mutex_unlock(&ps->eeprom_mutex);
232         return ret;
233 }
234
235 static int mv88e6352_set_eeprom(struct dsa_switch *ds,
236                                 struct ethtool_eeprom *eeprom, u8 *data)
237 {
238         int offset;
239         int ret;
240         int len;
241
242         if (eeprom->magic != 0xc3ec4951)
243                 return -EINVAL;
244
245         ret = mv88e6352_eeprom_is_readonly(ds);
246         if (ret)
247                 return ret;
248
249         offset = eeprom->offset;
250         len = eeprom->len;
251         eeprom->len = 0;
252
253         ret = mv88e6xxx_eeprom_load_wait(ds);
254         if (ret < 0)
255                 return ret;
256
257         if (offset & 1) {
258                 int word;
259
260                 word = mv88e6352_read_eeprom_word(ds, offset >> 1);
261                 if (word < 0)
262                         return word;
263
264                 word = (*data++ << 8) | (word & 0xff);
265
266                 ret = mv88e6352_write_eeprom_word(ds, offset >> 1, word);
267                 if (ret < 0)
268                         return ret;
269
270                 offset++;
271                 len--;
272                 eeprom->len++;
273         }
274
275         while (len >= 2) {
276                 int word;
277
278                 word = *data++;
279                 word |= *data++ << 8;
280
281                 ret = mv88e6352_write_eeprom_word(ds, offset >> 1, word);
282                 if (ret < 0)
283                         return ret;
284
285                 offset += 2;
286                 len -= 2;
287                 eeprom->len += 2;
288         }
289
290         if (len) {
291                 int word;
292
293                 word = mv88e6352_read_eeprom_word(ds, offset >> 1);
294                 if (word < 0)
295                         return word;
296
297                 word = (word & 0xff00) | *data++;
298
299                 ret = mv88e6352_write_eeprom_word(ds, offset >> 1, word);
300                 if (ret < 0)
301                         return ret;
302
303                 offset++;
304                 len--;
305                 eeprom->len++;
306         }
307
308         return 0;
309 }
310
311 struct dsa_switch_driver mv88e6352_switch_driver = {
312         .tag_protocol           = DSA_TAG_PROTO_EDSA,
313         .probe                  = mv88e6352_drv_probe,
314         .setup                  = mv88e6352_setup,
315         .set_addr               = mv88e6xxx_set_addr_indirect,
316         .phy_read               = mv88e6xxx_phy_read_indirect,
317         .phy_write              = mv88e6xxx_phy_write_indirect,
318         .get_strings            = mv88e6xxx_get_strings,
319         .get_ethtool_stats      = mv88e6xxx_get_ethtool_stats,
320         .get_sset_count         = mv88e6xxx_get_sset_count,
321         .adjust_link            = mv88e6xxx_adjust_link,
322         .set_eee                = mv88e6xxx_set_eee,
323         .get_eee                = mv88e6xxx_get_eee,
324 #ifdef CONFIG_NET_DSA_HWMON
325         .get_temp               = mv88e6xxx_get_temp,
326         .get_temp_limit         = mv88e6xxx_get_temp_limit,
327         .set_temp_limit         = mv88e6xxx_set_temp_limit,
328         .get_temp_alarm         = mv88e6xxx_get_temp_alarm,
329 #endif
330         .get_eeprom             = mv88e6352_get_eeprom,
331         .set_eeprom             = mv88e6352_set_eeprom,
332         .get_regs_len           = mv88e6xxx_get_regs_len,
333         .get_regs               = mv88e6xxx_get_regs,
334         .port_bridge_join       = mv88e6xxx_port_bridge_join,
335         .port_bridge_leave      = mv88e6xxx_port_bridge_leave,
336         .port_stp_state_set     = mv88e6xxx_port_stp_state_set,
337         .port_vlan_filtering    = mv88e6xxx_port_vlan_filtering,
338         .port_vlan_prepare      = mv88e6xxx_port_vlan_prepare,
339         .port_vlan_add          = mv88e6xxx_port_vlan_add,
340         .port_vlan_del          = mv88e6xxx_port_vlan_del,
341         .port_vlan_dump         = mv88e6xxx_port_vlan_dump,
342         .port_fdb_prepare       = mv88e6xxx_port_fdb_prepare,
343         .port_fdb_add           = mv88e6xxx_port_fdb_add,
344         .port_fdb_del           = mv88e6xxx_port_fdb_del,
345         .port_fdb_dump          = mv88e6xxx_port_fdb_dump,
346 };
347
348 MODULE_ALIAS("platform:mv88e6172");
349 MODULE_ALIAS("platform:mv88e6176");
350 MODULE_ALIAS("platform:mv88e6320");
351 MODULE_ALIAS("platform:mv88e6321");
352 MODULE_ALIAS("platform:mv88e6352");