blob: 0b8601f8536e5edfc6dd01a7147e4d86d1013ea9 [file] [log] [blame]
Vivien Didelotec561272016-09-02 14:45:33 -04001/*
Andrew Lunndc30c352016-10-16 19:56:49 +02002 * Marvell 88E6xxx Switch Global 2 Registers support (device address
3 * 0x1C)
Vivien Didelotec561272016-09-02 14:45:33 -04004 *
5 * Copyright (c) 2008 Marvell Semiconductor
6 *
Vivien Didelot4333d612017-03-28 15:10:36 -04007 * Copyright (c) 2016-2017 Savoir-faire Linux Inc.
8 * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
Vivien Didelotec561272016-09-02 14:45:33 -04009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 */
15
Andrew Lunndc30c352016-10-16 19:56:49 +020016#include <linux/irqdomain.h>
Vivien Didelotec561272016-09-02 14:45:33 -040017#include "mv88e6xxx.h"
18#include "global2.h"
19
Vivien Didelot9fe850f2016-09-29 12:21:54 -040020#define ADDR_GLOBAL2 0x1c
21
22static int mv88e6xxx_g2_read(struct mv88e6xxx_chip *chip, int reg, u16 *val)
23{
24 return mv88e6xxx_read(chip, ADDR_GLOBAL2, reg, val);
25}
26
27static int mv88e6xxx_g2_write(struct mv88e6xxx_chip *chip, int reg, u16 val)
28{
29 return mv88e6xxx_write(chip, ADDR_GLOBAL2, reg, val);
30}
31
32static int mv88e6xxx_g2_update(struct mv88e6xxx_chip *chip, int reg, u16 update)
33{
34 return mv88e6xxx_update(chip, ADDR_GLOBAL2, reg, update);
35}
36
37static int mv88e6xxx_g2_wait(struct mv88e6xxx_chip *chip, int reg, u16 mask)
38{
39 return mv88e6xxx_wait(chip, ADDR_GLOBAL2, reg, mask);
40}
41
Andrew Lunn6e55f692016-12-03 04:45:16 +010042/* Offset 0x02: Management Enable 2x */
43/* Offset 0x03: Management Enable 0x */
44
45int mv88e6095_g2_mgmt_rsvd2cpu(struct mv88e6xxx_chip *chip)
46{
47 int err;
48
49 /* Consider the frames with reserved multicast destination
50 * addresses matching 01:80:c2:00:00:2x as MGMT.
51 */
52 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_2X)) {
53 err = mv88e6xxx_g2_write(chip, GLOBAL2_MGMT_EN_2X, 0xffff);
54 if (err)
55 return err;
56 }
57
58 /* Consider the frames with reserved multicast destination
59 * addresses matching 01:80:c2:00:00:0x as MGMT.
60 */
61 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_0X))
62 return mv88e6xxx_g2_write(chip, GLOBAL2_MGMT_EN_0X, 0xffff);
63
64 return 0;
65}
66
Vivien Didelotec561272016-09-02 14:45:33 -040067/* Offset 0x06: Device Mapping Table register */
68
69static int mv88e6xxx_g2_device_mapping_write(struct mv88e6xxx_chip *chip,
70 int target, int port)
71{
72 u16 val = (target << 8) | (port & 0xf);
73
Vivien Didelot9fe850f2016-09-29 12:21:54 -040074 return mv88e6xxx_g2_update(chip, GLOBAL2_DEVICE_MAPPING, val);
Vivien Didelotec561272016-09-02 14:45:33 -040075}
76
77static int mv88e6xxx_g2_set_device_mapping(struct mv88e6xxx_chip *chip)
78{
79 int target, port;
80 int err;
81
82 /* Initialize the routing port to the 32 possible target devices */
83 for (target = 0; target < 32; ++target) {
84 port = 0xf;
85
86 if (target < DSA_MAX_SWITCHES) {
87 port = chip->ds->rtable[target];
88 if (port == DSA_RTABLE_NONE)
89 port = 0xf;
90 }
91
92 err = mv88e6xxx_g2_device_mapping_write(chip, target, port);
93 if (err)
94 break;
95 }
96
97 return err;
98}
99
100/* Offset 0x07: Trunk Mask Table register */
101
102static int mv88e6xxx_g2_trunk_mask_write(struct mv88e6xxx_chip *chip, int num,
103 bool hask, u16 mask)
104{
Vivien Didelot370b4ff2016-09-29 12:21:57 -0400105 const u16 port_mask = BIT(mv88e6xxx_num_ports(chip)) - 1;
Vivien Didelotec561272016-09-02 14:45:33 -0400106 u16 val = (num << 12) | (mask & port_mask);
107
108 if (hask)
109 val |= GLOBAL2_TRUNK_MASK_HASK;
110
Vivien Didelot9fe850f2016-09-29 12:21:54 -0400111 return mv88e6xxx_g2_update(chip, GLOBAL2_TRUNK_MASK, val);
Vivien Didelotec561272016-09-02 14:45:33 -0400112}
113
114/* Offset 0x08: Trunk Mapping Table register */
115
116static int mv88e6xxx_g2_trunk_mapping_write(struct mv88e6xxx_chip *chip, int id,
117 u16 map)
118{
Vivien Didelot370b4ff2016-09-29 12:21:57 -0400119 const u16 port_mask = BIT(mv88e6xxx_num_ports(chip)) - 1;
Vivien Didelotec561272016-09-02 14:45:33 -0400120 u16 val = (id << 11) | (map & port_mask);
121
Vivien Didelot9fe850f2016-09-29 12:21:54 -0400122 return mv88e6xxx_g2_update(chip, GLOBAL2_TRUNK_MAPPING, val);
Vivien Didelotec561272016-09-02 14:45:33 -0400123}
124
125static int mv88e6xxx_g2_clear_trunk(struct mv88e6xxx_chip *chip)
126{
Vivien Didelot370b4ff2016-09-29 12:21:57 -0400127 const u16 port_mask = BIT(mv88e6xxx_num_ports(chip)) - 1;
Vivien Didelotec561272016-09-02 14:45:33 -0400128 int i, err;
129
130 /* Clear all eight possible Trunk Mask vectors */
131 for (i = 0; i < 8; ++i) {
132 err = mv88e6xxx_g2_trunk_mask_write(chip, i, false, port_mask);
133 if (err)
134 return err;
135 }
136
137 /* Clear all sixteen possible Trunk ID routing vectors */
138 for (i = 0; i < 16; ++i) {
139 err = mv88e6xxx_g2_trunk_mapping_write(chip, i, 0);
140 if (err)
141 return err;
142 }
143
144 return 0;
145}
146
147/* Offset 0x09: Ingress Rate Command register
148 * Offset 0x0A: Ingress Rate Data register
149 */
150
151static int mv88e6xxx_g2_clear_irl(struct mv88e6xxx_chip *chip)
152{
153 int port, err;
154
155 /* Init all Ingress Rate Limit resources of all ports */
Vivien Didelot370b4ff2016-09-29 12:21:57 -0400156 for (port = 0; port < mv88e6xxx_num_ports(chip); ++port) {
Vivien Didelotec561272016-09-02 14:45:33 -0400157 /* XXX newer chips (like 88E6390) have different 2-bit ops */
Vivien Didelot9fe850f2016-09-29 12:21:54 -0400158 err = mv88e6xxx_g2_write(chip, GLOBAL2_IRL_CMD,
159 GLOBAL2_IRL_CMD_OP_INIT_ALL |
160 (port << 8));
Vivien Didelotec561272016-09-02 14:45:33 -0400161 if (err)
162 break;
163
164 /* Wait for the operation to complete */
Vivien Didelot9fe850f2016-09-29 12:21:54 -0400165 err = mv88e6xxx_g2_wait(chip, GLOBAL2_IRL_CMD,
166 GLOBAL2_IRL_CMD_BUSY);
Vivien Didelotec561272016-09-02 14:45:33 -0400167 if (err)
168 break;
169 }
170
171 return err;
172}
173
174/* Offset 0x0D: Switch MAC/WoL/WoF register */
175
176static int mv88e6xxx_g2_switch_mac_write(struct mv88e6xxx_chip *chip,
177 unsigned int pointer, u8 data)
178{
179 u16 val = (pointer << 8) | data;
180
Vivien Didelot9fe850f2016-09-29 12:21:54 -0400181 return mv88e6xxx_g2_update(chip, GLOBAL2_SWITCH_MAC, val);
Vivien Didelotec561272016-09-02 14:45:33 -0400182}
183
184int mv88e6xxx_g2_set_switch_mac(struct mv88e6xxx_chip *chip, u8 *addr)
185{
186 int i, err;
187
188 for (i = 0; i < 6; i++) {
189 err = mv88e6xxx_g2_switch_mac_write(chip, i, addr[i]);
190 if (err)
191 break;
192 }
193
194 return err;
195}
196
197/* Offset 0x0F: Priority Override Table */
198
199static int mv88e6xxx_g2_pot_write(struct mv88e6xxx_chip *chip, int pointer,
200 u8 data)
201{
202 u16 val = (pointer << 8) | (data & 0x7);
203
Vivien Didelot9fe850f2016-09-29 12:21:54 -0400204 return mv88e6xxx_g2_update(chip, GLOBAL2_PRIO_OVERRIDE, val);
Vivien Didelotec561272016-09-02 14:45:33 -0400205}
206
207static int mv88e6xxx_g2_clear_pot(struct mv88e6xxx_chip *chip)
208{
209 int i, err;
210
211 /* Clear all sixteen possible Priority Override entries */
212 for (i = 0; i < 16; i++) {
213 err = mv88e6xxx_g2_pot_write(chip, i, 0);
214 if (err)
215 break;
216 }
217
218 return err;
219}
220
221/* Offset 0x14: EEPROM Command
Vivien Didelot98fc3c62017-01-12 18:07:16 -0500222 * Offset 0x15: EEPROM Data (for 16-bit data access)
223 * Offset 0x15: EEPROM Addr (for 8-bit data access)
Vivien Didelotec561272016-09-02 14:45:33 -0400224 */
225
226static int mv88e6xxx_g2_eeprom_wait(struct mv88e6xxx_chip *chip)
227{
Vivien Didelot9fe850f2016-09-29 12:21:54 -0400228 return mv88e6xxx_g2_wait(chip, GLOBAL2_EEPROM_CMD,
229 GLOBAL2_EEPROM_CMD_BUSY |
230 GLOBAL2_EEPROM_CMD_RUNNING);
Vivien Didelotec561272016-09-02 14:45:33 -0400231}
232
233static int mv88e6xxx_g2_eeprom_cmd(struct mv88e6xxx_chip *chip, u16 cmd)
234{
235 int err;
236
Vivien Didelot9fe850f2016-09-29 12:21:54 -0400237 err = mv88e6xxx_g2_write(chip, GLOBAL2_EEPROM_CMD, cmd);
Vivien Didelotec561272016-09-02 14:45:33 -0400238 if (err)
239 return err;
240
241 return mv88e6xxx_g2_eeprom_wait(chip);
242}
243
Vivien Didelot98fc3c62017-01-12 18:07:16 -0500244static int mv88e6xxx_g2_eeprom_read8(struct mv88e6xxx_chip *chip,
245 u16 addr, u8 *data)
246{
247 u16 cmd = GLOBAL2_EEPROM_CMD_OP_READ;
248 int err;
249
250 err = mv88e6xxx_g2_eeprom_wait(chip);
251 if (err)
252 return err;
253
254 err = mv88e6xxx_g2_write(chip, GLOBAL2_EEPROM_ADDR, addr);
255 if (err)
256 return err;
257
258 err = mv88e6xxx_g2_eeprom_cmd(chip, cmd);
259 if (err)
260 return err;
261
262 err = mv88e6xxx_g2_read(chip, GLOBAL2_EEPROM_CMD, &cmd);
263 if (err)
264 return err;
265
266 *data = cmd & 0xff;
267
268 return 0;
269}
270
271static int mv88e6xxx_g2_eeprom_write8(struct mv88e6xxx_chip *chip,
272 u16 addr, u8 data)
273{
274 u16 cmd = GLOBAL2_EEPROM_CMD_OP_WRITE | GLOBAL2_EEPROM_CMD_WRITE_EN;
275 int err;
276
277 err = mv88e6xxx_g2_eeprom_wait(chip);
278 if (err)
279 return err;
280
281 err = mv88e6xxx_g2_write(chip, GLOBAL2_EEPROM_ADDR, addr);
282 if (err)
283 return err;
284
285 return mv88e6xxx_g2_eeprom_cmd(chip, cmd | data);
286}
287
Vivien Didelotec561272016-09-02 14:45:33 -0400288static int mv88e6xxx_g2_eeprom_read16(struct mv88e6xxx_chip *chip,
289 u8 addr, u16 *data)
290{
291 u16 cmd = GLOBAL2_EEPROM_CMD_OP_READ | addr;
292 int err;
293
294 err = mv88e6xxx_g2_eeprom_wait(chip);
295 if (err)
296 return err;
297
298 err = mv88e6xxx_g2_eeprom_cmd(chip, cmd);
299 if (err)
300 return err;
301
Vivien Didelot9fe850f2016-09-29 12:21:54 -0400302 return mv88e6xxx_g2_read(chip, GLOBAL2_EEPROM_DATA, data);
Vivien Didelotec561272016-09-02 14:45:33 -0400303}
304
305static int mv88e6xxx_g2_eeprom_write16(struct mv88e6xxx_chip *chip,
306 u8 addr, u16 data)
307{
308 u16 cmd = GLOBAL2_EEPROM_CMD_OP_WRITE | addr;
309 int err;
310
311 err = mv88e6xxx_g2_eeprom_wait(chip);
312 if (err)
313 return err;
314
Vivien Didelot9fe850f2016-09-29 12:21:54 -0400315 err = mv88e6xxx_g2_write(chip, GLOBAL2_EEPROM_DATA, data);
Vivien Didelotec561272016-09-02 14:45:33 -0400316 if (err)
317 return err;
318
319 return mv88e6xxx_g2_eeprom_cmd(chip, cmd);
320}
321
Vivien Didelot98fc3c62017-01-12 18:07:16 -0500322int mv88e6xxx_g2_get_eeprom8(struct mv88e6xxx_chip *chip,
323 struct ethtool_eeprom *eeprom, u8 *data)
324{
325 unsigned int offset = eeprom->offset;
326 unsigned int len = eeprom->len;
327 int err;
328
329 eeprom->len = 0;
330
331 while (len) {
332 err = mv88e6xxx_g2_eeprom_read8(chip, offset, data);
333 if (err)
334 return err;
335
336 eeprom->len++;
337 offset++;
338 data++;
339 len--;
340 }
341
342 return 0;
343}
344
345int mv88e6xxx_g2_set_eeprom8(struct mv88e6xxx_chip *chip,
346 struct ethtool_eeprom *eeprom, u8 *data)
347{
348 unsigned int offset = eeprom->offset;
349 unsigned int len = eeprom->len;
350 int err;
351
352 eeprom->len = 0;
353
354 while (len) {
355 err = mv88e6xxx_g2_eeprom_write8(chip, offset, *data);
356 if (err)
357 return err;
358
359 eeprom->len++;
360 offset++;
361 data++;
362 len--;
363 }
364
365 return 0;
366}
367
Vivien Didelotec561272016-09-02 14:45:33 -0400368int mv88e6xxx_g2_get_eeprom16(struct mv88e6xxx_chip *chip,
369 struct ethtool_eeprom *eeprom, u8 *data)
370{
371 unsigned int offset = eeprom->offset;
372 unsigned int len = eeprom->len;
373 u16 val;
374 int err;
375
376 eeprom->len = 0;
377
378 if (offset & 1) {
379 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
380 if (err)
381 return err;
382
383 *data++ = (val >> 8) & 0xff;
384
385 offset++;
386 len--;
387 eeprom->len++;
388 }
389
390 while (len >= 2) {
391 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
392 if (err)
393 return err;
394
395 *data++ = val & 0xff;
396 *data++ = (val >> 8) & 0xff;
397
398 offset += 2;
399 len -= 2;
400 eeprom->len += 2;
401 }
402
403 if (len) {
404 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
405 if (err)
406 return err;
407
408 *data++ = val & 0xff;
409
410 offset++;
411 len--;
412 eeprom->len++;
413 }
414
415 return 0;
416}
417
418int mv88e6xxx_g2_set_eeprom16(struct mv88e6xxx_chip *chip,
419 struct ethtool_eeprom *eeprom, u8 *data)
420{
421 unsigned int offset = eeprom->offset;
422 unsigned int len = eeprom->len;
423 u16 val;
424 int err;
425
426 /* Ensure the RO WriteEn bit is set */
Vivien Didelot9fe850f2016-09-29 12:21:54 -0400427 err = mv88e6xxx_g2_read(chip, GLOBAL2_EEPROM_CMD, &val);
Vivien Didelotec561272016-09-02 14:45:33 -0400428 if (err)
429 return err;
430
431 if (!(val & GLOBAL2_EEPROM_CMD_WRITE_EN))
432 return -EROFS;
433
434 eeprom->len = 0;
435
436 if (offset & 1) {
437 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
438 if (err)
439 return err;
440
441 val = (*data++ << 8) | (val & 0xff);
442
443 err = mv88e6xxx_g2_eeprom_write16(chip, offset >> 1, val);
444 if (err)
445 return err;
446
447 offset++;
448 len--;
449 eeprom->len++;
450 }
451
452 while (len >= 2) {
453 val = *data++;
454 val |= *data++ << 8;
455
456 err = mv88e6xxx_g2_eeprom_write16(chip, offset >> 1, val);
457 if (err)
458 return err;
459
460 offset += 2;
461 len -= 2;
462 eeprom->len += 2;
463 }
464
465 if (len) {
466 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
467 if (err)
468 return err;
469
470 val = (val & 0xff00) | *data++;
471
472 err = mv88e6xxx_g2_eeprom_write16(chip, offset >> 1, val);
473 if (err)
474 return err;
475
476 offset++;
477 len--;
478 eeprom->len++;
479 }
480
481 return 0;
482}
483
484/* Offset 0x18: SMI PHY Command Register
485 * Offset 0x19: SMI PHY Data Register
486 */
487
488static int mv88e6xxx_g2_smi_phy_wait(struct mv88e6xxx_chip *chip)
489{
Vivien Didelot9fe850f2016-09-29 12:21:54 -0400490 return mv88e6xxx_g2_wait(chip, GLOBAL2_SMI_PHY_CMD,
491 GLOBAL2_SMI_PHY_CMD_BUSY);
Vivien Didelotec561272016-09-02 14:45:33 -0400492}
493
494static int mv88e6xxx_g2_smi_phy_cmd(struct mv88e6xxx_chip *chip, u16 cmd)
495{
496 int err;
497
Vivien Didelot9fe850f2016-09-29 12:21:54 -0400498 err = mv88e6xxx_g2_write(chip, GLOBAL2_SMI_PHY_CMD, cmd);
Vivien Didelotec561272016-09-02 14:45:33 -0400499 if (err)
500 return err;
501
502 return mv88e6xxx_g2_smi_phy_wait(chip);
503}
504
Andrew Lunncf3e80d2017-02-04 20:12:24 +0100505static int mv88e6xxx_g2_smi_phy_write_addr(struct mv88e6xxx_chip *chip,
506 int addr, int device, int reg,
507 bool external)
Vivien Didelotec561272016-09-02 14:45:33 -0400508{
Andrew Lunncf3e80d2017-02-04 20:12:24 +0100509 int cmd = SMI_CMD_OP_45_WRITE_ADDR | (addr << 5) | device;
Vivien Didelotec561272016-09-02 14:45:33 -0400510 int err;
511
Andrew Lunncf3e80d2017-02-04 20:12:24 +0100512 if (external)
513 cmd |= GLOBAL2_SMI_PHY_CMD_EXTERNAL;
514
515 err = mv88e6xxx_g2_smi_phy_wait(chip);
516 if (err)
517 return err;
518
519 err = mv88e6xxx_g2_write(chip, GLOBAL2_SMI_PHY_DATA, reg);
520 if (err)
521 return err;
522
523 return mv88e6xxx_g2_smi_phy_cmd(chip, cmd);
524}
525
526int mv88e6xxx_g2_smi_phy_read_c45(struct mv88e6xxx_chip *chip, int addr,
527 int reg_c45, u16 *val, bool external)
528{
529 int device = (reg_c45 >> 16) & 0x1f;
530 int reg = reg_c45 & 0xffff;
531 int err;
532 u16 cmd;
533
534 err = mv88e6xxx_g2_smi_phy_write_addr(chip, addr, device, reg,
535 external);
536 if (err)
537 return err;
538
539 cmd = GLOBAL2_SMI_PHY_CMD_OP_45_READ_DATA | (addr << 5) | device;
540
541 if (external)
542 cmd |= GLOBAL2_SMI_PHY_CMD_EXTERNAL;
543
544 err = mv88e6xxx_g2_smi_phy_cmd(chip, cmd);
545 if (err)
546 return err;
547
548 err = mv88e6xxx_g2_read(chip, GLOBAL2_SMI_PHY_DATA, val);
549 if (err)
550 return err;
551
552 err = *val;
553
554 return 0;
555}
556
557int mv88e6xxx_g2_smi_phy_read_c22(struct mv88e6xxx_chip *chip, int addr,
558 int reg, u16 *val, bool external)
559{
560 u16 cmd = GLOBAL2_SMI_PHY_CMD_OP_22_READ_DATA | (addr << 5) | reg;
561 int err;
562
563 if (external)
Andrew Lunnc61a6a72017-01-24 14:53:51 +0100564 cmd |= GLOBAL2_SMI_PHY_CMD_EXTERNAL;
565
Vivien Didelotec561272016-09-02 14:45:33 -0400566 err = mv88e6xxx_g2_smi_phy_wait(chip);
567 if (err)
568 return err;
569
570 err = mv88e6xxx_g2_smi_phy_cmd(chip, cmd);
571 if (err)
572 return err;
573
Vivien Didelot9fe850f2016-09-29 12:21:54 -0400574 return mv88e6xxx_g2_read(chip, GLOBAL2_SMI_PHY_DATA, val);
Vivien Didelotec561272016-09-02 14:45:33 -0400575}
576
Andrew Lunncf3e80d2017-02-04 20:12:24 +0100577int mv88e6xxx_g2_smi_phy_read(struct mv88e6xxx_chip *chip,
578 struct mii_bus *bus,
579 int addr, int reg, u16 *val)
580{
581 struct mv88e6xxx_mdio_bus *mdio_bus = bus->priv;
582 bool external = mdio_bus->external;
583
584 if (reg & MII_ADDR_C45)
585 return mv88e6xxx_g2_smi_phy_read_c45(chip, addr, reg, val,
586 external);
587 return mv88e6xxx_g2_smi_phy_read_c22(chip, addr, reg, val, external);
588}
589
590int mv88e6xxx_g2_smi_phy_write_c45(struct mv88e6xxx_chip *chip, int addr,
591 int reg_c45, u16 val, bool external)
592{
593 int device = (reg_c45 >> 16) & 0x1f;
594 int reg = reg_c45 & 0xffff;
595 int err;
596 u16 cmd;
597
598 err = mv88e6xxx_g2_smi_phy_write_addr(chip, addr, device, reg,
599 external);
600 if (err)
601 return err;
602
603 cmd = GLOBAL2_SMI_PHY_CMD_OP_45_WRITE_DATA | (addr << 5) | device;
604
605 if (external)
606 cmd |= GLOBAL2_SMI_PHY_CMD_EXTERNAL;
607
608 err = mv88e6xxx_g2_write(chip, GLOBAL2_SMI_PHY_DATA, val);
609 if (err)
610 return err;
611
612 err = mv88e6xxx_g2_smi_phy_cmd(chip, cmd);
613 if (err)
614 return err;
615
616 return 0;
617}
618
619int mv88e6xxx_g2_smi_phy_write_c22(struct mv88e6xxx_chip *chip, int addr,
620 int reg, u16 val, bool external)
Vivien Didelotec561272016-09-02 14:45:33 -0400621{
622 u16 cmd = GLOBAL2_SMI_PHY_CMD_OP_22_WRITE_DATA | (addr << 5) | reg;
623 int err;
624
Andrew Lunncf3e80d2017-02-04 20:12:24 +0100625 if (external)
Andrew Lunnc61a6a72017-01-24 14:53:51 +0100626 cmd |= GLOBAL2_SMI_PHY_CMD_EXTERNAL;
627
Vivien Didelotec561272016-09-02 14:45:33 -0400628 err = mv88e6xxx_g2_smi_phy_wait(chip);
629 if (err)
630 return err;
631
Vivien Didelot9fe850f2016-09-29 12:21:54 -0400632 err = mv88e6xxx_g2_write(chip, GLOBAL2_SMI_PHY_DATA, val);
Vivien Didelotec561272016-09-02 14:45:33 -0400633 if (err)
634 return err;
635
636 return mv88e6xxx_g2_smi_phy_cmd(chip, cmd);
637}
638
Andrew Lunncf3e80d2017-02-04 20:12:24 +0100639int mv88e6xxx_g2_smi_phy_write(struct mv88e6xxx_chip *chip,
640 struct mii_bus *bus,
641 int addr, int reg, u16 val)
642{
643 struct mv88e6xxx_mdio_bus *mdio_bus = bus->priv;
644 bool external = mdio_bus->external;
645
646 if (reg & MII_ADDR_C45)
647 return mv88e6xxx_g2_smi_phy_write_c45(chip, addr, reg, val,
648 external);
649
650 return mv88e6xxx_g2_smi_phy_write_c22(chip, addr, reg, val, external);
651}
652
Andrew Lunnfcd25162017-02-09 00:03:42 +0100653static int mv88e6097_watchdog_action(struct mv88e6xxx_chip *chip, int irq)
654{
655 u16 reg;
656
657 mv88e6xxx_g2_read(chip, GLOBAL2_WDOG_CONTROL, &reg);
658
659 dev_info(chip->dev, "Watchdog event: 0x%04x", reg);
660
661 return IRQ_HANDLED;
662}
663
664static void mv88e6097_watchdog_free(struct mv88e6xxx_chip *chip)
665{
666 u16 reg;
667
668 mv88e6xxx_g2_read(chip, GLOBAL2_WDOG_CONTROL, &reg);
669
670 reg &= ~(GLOBAL2_WDOG_CONTROL_EGRESS_ENABLE |
671 GLOBAL2_WDOG_CONTROL_QC_ENABLE);
672
673 mv88e6xxx_g2_write(chip, GLOBAL2_WDOG_CONTROL, reg);
674}
675
676static int mv88e6097_watchdog_setup(struct mv88e6xxx_chip *chip)
677{
678 return mv88e6xxx_g2_write(chip, GLOBAL2_WDOG_CONTROL,
679 GLOBAL2_WDOG_CONTROL_EGRESS_ENABLE |
680 GLOBAL2_WDOG_CONTROL_QC_ENABLE |
681 GLOBAL2_WDOG_CONTROL_SWRESET);
682}
683
684const struct mv88e6xxx_irq_ops mv88e6097_watchdog_ops = {
685 .irq_action = mv88e6097_watchdog_action,
686 .irq_setup = mv88e6097_watchdog_setup,
687 .irq_free = mv88e6097_watchdog_free,
688};
689
Andrew Lunn61303732017-02-09 00:03:43 +0100690static int mv88e6390_watchdog_setup(struct mv88e6xxx_chip *chip)
691{
692 return mv88e6xxx_g2_update(chip, GLOBAL2_WDOG_CONTROL,
693 GLOBAL2_WDOG_INT_ENABLE |
694 GLOBAL2_WDOG_CUT_THROUGH |
695 GLOBAL2_WDOG_QUEUE_CONTROLLER |
696 GLOBAL2_WDOG_EGRESS |
697 GLOBAL2_WDOG_FORCE_IRQ);
698}
699
700static int mv88e6390_watchdog_action(struct mv88e6xxx_chip *chip, int irq)
701{
702 int err;
703 u16 reg;
704
705 mv88e6xxx_g2_write(chip, GLOBAL2_WDOG_CONTROL, GLOBAL2_WDOG_EVENT);
706 err = mv88e6xxx_g2_read(chip, GLOBAL2_WDOG_CONTROL, &reg);
707
708 dev_info(chip->dev, "Watchdog event: 0x%04x",
709 reg & GLOBAL2_WDOG_DATA_MASK);
710
711 mv88e6xxx_g2_write(chip, GLOBAL2_WDOG_CONTROL, GLOBAL2_WDOG_HISTORY);
712 err = mv88e6xxx_g2_read(chip, GLOBAL2_WDOG_CONTROL, &reg);
713
714 dev_info(chip->dev, "Watchdog history: 0x%04x",
715 reg & GLOBAL2_WDOG_DATA_MASK);
716
717 /* Trigger a software reset to try to recover the switch */
718 if (chip->info->ops->reset)
719 chip->info->ops->reset(chip);
720
721 mv88e6390_watchdog_setup(chip);
722
723 return IRQ_HANDLED;
724}
725
726static void mv88e6390_watchdog_free(struct mv88e6xxx_chip *chip)
727{
728 mv88e6xxx_g2_update(chip, GLOBAL2_WDOG_CONTROL,
729 GLOBAL2_WDOG_INT_ENABLE);
730}
731
732const struct mv88e6xxx_irq_ops mv88e6390_watchdog_ops = {
733 .irq_action = mv88e6390_watchdog_action,
734 .irq_setup = mv88e6390_watchdog_setup,
735 .irq_free = mv88e6390_watchdog_free,
736};
737
Andrew Lunnfcd25162017-02-09 00:03:42 +0100738static irqreturn_t mv88e6xxx_g2_watchdog_thread_fn(int irq, void *dev_id)
739{
740 struct mv88e6xxx_chip *chip = dev_id;
741 irqreturn_t ret = IRQ_NONE;
742
743 mutex_lock(&chip->reg_lock);
744 if (chip->info->ops->watchdog_ops->irq_action)
745 ret = chip->info->ops->watchdog_ops->irq_action(chip, irq);
746 mutex_unlock(&chip->reg_lock);
747
748 return ret;
749}
750
751static void mv88e6xxx_g2_watchdog_free(struct mv88e6xxx_chip *chip)
752{
753 mutex_lock(&chip->reg_lock);
754 if (chip->info->ops->watchdog_ops->irq_free)
755 chip->info->ops->watchdog_ops->irq_free(chip);
756 mutex_unlock(&chip->reg_lock);
757
758 free_irq(chip->watchdog_irq, chip);
759 irq_dispose_mapping(chip->watchdog_irq);
760}
761
762static int mv88e6xxx_g2_watchdog_setup(struct mv88e6xxx_chip *chip)
763{
764 int err;
765
766 chip->watchdog_irq = irq_find_mapping(chip->g2_irq.domain,
767 GLOBAL2_INT_SOURCE_WATCHDOG);
768 if (chip->watchdog_irq < 0)
769 return chip->watchdog_irq;
770
771 err = request_threaded_irq(chip->watchdog_irq, NULL,
772 mv88e6xxx_g2_watchdog_thread_fn,
773 IRQF_ONESHOT | IRQF_TRIGGER_FALLING,
774 "mv88e6xxx-watchdog", chip);
775 if (err)
776 return err;
777
778 mutex_lock(&chip->reg_lock);
779 if (chip->info->ops->watchdog_ops->irq_setup)
780 err = chip->info->ops->watchdog_ops->irq_setup(chip);
781 mutex_unlock(&chip->reg_lock);
782
783 return err;
784}
785
Andrew Lunndc30c352016-10-16 19:56:49 +0200786static void mv88e6xxx_g2_irq_mask(struct irq_data *d)
787{
788 struct mv88e6xxx_chip *chip = irq_data_get_irq_chip_data(d);
789 unsigned int n = d->hwirq;
790
791 chip->g2_irq.masked |= (1 << n);
792}
793
794static void mv88e6xxx_g2_irq_unmask(struct irq_data *d)
795{
796 struct mv88e6xxx_chip *chip = irq_data_get_irq_chip_data(d);
797 unsigned int n = d->hwirq;
798
799 chip->g2_irq.masked &= ~(1 << n);
800}
801
802static irqreturn_t mv88e6xxx_g2_irq_thread_fn(int irq, void *dev_id)
803{
804 struct mv88e6xxx_chip *chip = dev_id;
805 unsigned int nhandled = 0;
806 unsigned int sub_irq;
807 unsigned int n;
808 int err;
809 u16 reg;
810
811 mutex_lock(&chip->reg_lock);
812 err = mv88e6xxx_g2_read(chip, GLOBAL2_INT_SOURCE, &reg);
813 mutex_unlock(&chip->reg_lock);
814 if (err)
815 goto out;
816
817 for (n = 0; n < 16; ++n) {
818 if (reg & (1 << n)) {
819 sub_irq = irq_find_mapping(chip->g2_irq.domain, n);
820 handle_nested_irq(sub_irq);
821 ++nhandled;
822 }
823 }
824out:
825 return (nhandled > 0 ? IRQ_HANDLED : IRQ_NONE);
826}
827
828static void mv88e6xxx_g2_irq_bus_lock(struct irq_data *d)
829{
830 struct mv88e6xxx_chip *chip = irq_data_get_irq_chip_data(d);
831
832 mutex_lock(&chip->reg_lock);
833}
834
835static void mv88e6xxx_g2_irq_bus_sync_unlock(struct irq_data *d)
836{
837 struct mv88e6xxx_chip *chip = irq_data_get_irq_chip_data(d);
838
839 mv88e6xxx_g2_write(chip, GLOBAL2_INT_MASK, ~chip->g2_irq.masked);
840
841 mutex_unlock(&chip->reg_lock);
842}
843
844static struct irq_chip mv88e6xxx_g2_irq_chip = {
845 .name = "mv88e6xxx-g2",
846 .irq_mask = mv88e6xxx_g2_irq_mask,
847 .irq_unmask = mv88e6xxx_g2_irq_unmask,
848 .irq_bus_lock = mv88e6xxx_g2_irq_bus_lock,
849 .irq_bus_sync_unlock = mv88e6xxx_g2_irq_bus_sync_unlock,
850};
851
852static int mv88e6xxx_g2_irq_domain_map(struct irq_domain *d,
853 unsigned int irq,
854 irq_hw_number_t hwirq)
855{
856 struct mv88e6xxx_chip *chip = d->host_data;
857
858 irq_set_chip_data(irq, d->host_data);
859 irq_set_chip_and_handler(irq, &chip->g2_irq.chip, handle_level_irq);
860 irq_set_noprobe(irq);
861
862 return 0;
863}
864
865static const struct irq_domain_ops mv88e6xxx_g2_irq_domain_ops = {
866 .map = mv88e6xxx_g2_irq_domain_map,
867 .xlate = irq_domain_xlate_twocell,
868};
869
870void mv88e6xxx_g2_irq_free(struct mv88e6xxx_chip *chip)
871{
872 int irq, virq;
873
Andrew Lunnfcd25162017-02-09 00:03:42 +0100874 mv88e6xxx_g2_watchdog_free(chip);
875
Andrew Lunn8e757eb2016-11-20 20:14:18 +0100876 free_irq(chip->device_irq, chip);
877 irq_dispose_mapping(chip->device_irq);
878
Andrew Lunndc30c352016-10-16 19:56:49 +0200879 for (irq = 0; irq < 16; irq++) {
880 virq = irq_find_mapping(chip->g2_irq.domain, irq);
881 irq_dispose_mapping(virq);
882 }
883
884 irq_domain_remove(chip->g2_irq.domain);
885}
886
887int mv88e6xxx_g2_irq_setup(struct mv88e6xxx_chip *chip)
888{
Andrew Lunn8e757eb2016-11-20 20:14:18 +0100889 int err, irq, virq;
Andrew Lunndc30c352016-10-16 19:56:49 +0200890
891 if (!chip->dev->of_node)
892 return -EINVAL;
893
894 chip->g2_irq.domain = irq_domain_add_simple(
895 chip->dev->of_node, 16, 0, &mv88e6xxx_g2_irq_domain_ops, chip);
896 if (!chip->g2_irq.domain)
897 return -ENOMEM;
898
899 for (irq = 0; irq < 16; irq++)
900 irq_create_mapping(chip->g2_irq.domain, irq);
901
902 chip->g2_irq.chip = mv88e6xxx_g2_irq_chip;
903 chip->g2_irq.masked = ~0;
904
Andrew Lunn8e757eb2016-11-20 20:14:18 +0100905 chip->device_irq = irq_find_mapping(chip->g1_irq.domain,
906 GLOBAL_STATUS_IRQ_DEVICE);
907 if (chip->device_irq < 0) {
908 err = chip->device_irq;
Andrew Lunndc30c352016-10-16 19:56:49 +0200909 goto out;
910 }
911
Andrew Lunn8e757eb2016-11-20 20:14:18 +0100912 err = request_threaded_irq(chip->device_irq, NULL,
913 mv88e6xxx_g2_irq_thread_fn,
914 IRQF_ONESHOT, "mv88e6xxx-g1", chip);
Andrew Lunndc30c352016-10-16 19:56:49 +0200915 if (err)
916 goto out;
917
Andrew Lunnfcd25162017-02-09 00:03:42 +0100918 return mv88e6xxx_g2_watchdog_setup(chip);
Andrew Lunn8e757eb2016-11-20 20:14:18 +0100919
Andrew Lunndc30c352016-10-16 19:56:49 +0200920out:
Andrew Lunn8e757eb2016-11-20 20:14:18 +0100921 for (irq = 0; irq < 16; irq++) {
922 virq = irq_find_mapping(chip->g2_irq.domain, irq);
923 irq_dispose_mapping(virq);
924 }
925
926 irq_domain_remove(chip->g2_irq.domain);
Andrew Lunndc30c352016-10-16 19:56:49 +0200927
928 return err;
929}
930
Vivien Didelotec561272016-09-02 14:45:33 -0400931int mv88e6xxx_g2_setup(struct mv88e6xxx_chip *chip)
932{
933 u16 reg;
934 int err;
935
Vivien Didelotec561272016-09-02 14:45:33 -0400936 /* Ignore removed tag data on doubly tagged packets, disable
937 * flow control messages, force flow control priority to the
938 * highest, and send all special multicast frames to the CPU
939 * port at the highest priority.
940 */
941 reg = GLOBAL2_SWITCH_MGMT_FORCE_FLOW_CTRL_PRI | (0x7 << 4);
942 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_0X) ||
943 mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_2X))
944 reg |= GLOBAL2_SWITCH_MGMT_RSVD2CPU | 0x7;
Vivien Didelot9fe850f2016-09-29 12:21:54 -0400945 err = mv88e6xxx_g2_write(chip, GLOBAL2_SWITCH_MGMT, reg);
Vivien Didelotec561272016-09-02 14:45:33 -0400946 if (err)
947 return err;
948
949 /* Program the DSA routing table. */
950 err = mv88e6xxx_g2_set_device_mapping(chip);
951 if (err)
952 return err;
953
954 /* Clear all trunk masks and mapping. */
955 err = mv88e6xxx_g2_clear_trunk(chip);
956 if (err)
957 return err;
958
959 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_IRL)) {
960 /* Disable ingress rate limiting by resetting all per port
961 * ingress rate limit resources to their initial state.
962 */
963 err = mv88e6xxx_g2_clear_irl(chip);
964 if (err)
965 return err;
966 }
967
968 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_PVT)) {
969 /* Initialize Cross-chip Port VLAN Table to reset defaults */
Vivien Didelot9fe850f2016-09-29 12:21:54 -0400970 err = mv88e6xxx_g2_write(chip, GLOBAL2_PVT_ADDR,
971 GLOBAL2_PVT_ADDR_OP_INIT_ONES);
Vivien Didelotec561272016-09-02 14:45:33 -0400972 if (err)
973 return err;
974 }
975
976 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_POT)) {
977 /* Clear the priority override table. */
978 err = mv88e6xxx_g2_clear_pot(chip);
979 if (err)
980 return err;
981 }
982
983 return 0;
984}