blob: 132559d46b953bf967670050ad5c033d23650cef [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
Florian Westphal282ccf62017-03-29 17:17:31 +020016#include <linux/interrupt.h>
Andrew Lunndc30c352016-10-16 19:56:49 +020017#include <linux/irqdomain.h>
Vivien Didelotec561272016-09-02 14:45:33 -040018#include "mv88e6xxx.h"
19#include "global2.h"
20
Vivien Didelot9fe850f2016-09-29 12:21:54 -040021#define ADDR_GLOBAL2 0x1c
22
23static int mv88e6xxx_g2_read(struct mv88e6xxx_chip *chip, int reg, u16 *val)
24{
25 return mv88e6xxx_read(chip, ADDR_GLOBAL2, reg, val);
26}
27
28static int mv88e6xxx_g2_write(struct mv88e6xxx_chip *chip, int reg, u16 val)
29{
30 return mv88e6xxx_write(chip, ADDR_GLOBAL2, reg, val);
31}
32
33static int mv88e6xxx_g2_update(struct mv88e6xxx_chip *chip, int reg, u16 update)
34{
35 return mv88e6xxx_update(chip, ADDR_GLOBAL2, reg, update);
36}
37
38static int mv88e6xxx_g2_wait(struct mv88e6xxx_chip *chip, int reg, u16 mask)
39{
40 return mv88e6xxx_wait(chip, ADDR_GLOBAL2, reg, mask);
41}
42
Andrew Lunn6e55f692016-12-03 04:45:16 +010043/* Offset 0x02: Management Enable 2x */
44/* Offset 0x03: Management Enable 0x */
45
46int mv88e6095_g2_mgmt_rsvd2cpu(struct mv88e6xxx_chip *chip)
47{
48 int err;
49
50 /* Consider the frames with reserved multicast destination
51 * addresses matching 01:80:c2:00:00:2x as MGMT.
52 */
53 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_2X)) {
54 err = mv88e6xxx_g2_write(chip, GLOBAL2_MGMT_EN_2X, 0xffff);
55 if (err)
56 return err;
57 }
58
59 /* Consider the frames with reserved multicast destination
60 * addresses matching 01:80:c2:00:00:0x as MGMT.
61 */
62 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_0X))
63 return mv88e6xxx_g2_write(chip, GLOBAL2_MGMT_EN_0X, 0xffff);
64
65 return 0;
66}
67
Vivien Didelotec561272016-09-02 14:45:33 -040068/* Offset 0x06: Device Mapping Table register */
69
70static int mv88e6xxx_g2_device_mapping_write(struct mv88e6xxx_chip *chip,
71 int target, int port)
72{
73 u16 val = (target << 8) | (port & 0xf);
74
Vivien Didelot9fe850f2016-09-29 12:21:54 -040075 return mv88e6xxx_g2_update(chip, GLOBAL2_DEVICE_MAPPING, val);
Vivien Didelotec561272016-09-02 14:45:33 -040076}
77
78static int mv88e6xxx_g2_set_device_mapping(struct mv88e6xxx_chip *chip)
79{
80 int target, port;
81 int err;
82
83 /* Initialize the routing port to the 32 possible target devices */
84 for (target = 0; target < 32; ++target) {
85 port = 0xf;
86
87 if (target < DSA_MAX_SWITCHES) {
88 port = chip->ds->rtable[target];
89 if (port == DSA_RTABLE_NONE)
90 port = 0xf;
91 }
92
93 err = mv88e6xxx_g2_device_mapping_write(chip, target, port);
94 if (err)
95 break;
96 }
97
98 return err;
99}
100
101/* Offset 0x07: Trunk Mask Table register */
102
103static int mv88e6xxx_g2_trunk_mask_write(struct mv88e6xxx_chip *chip, int num,
104 bool hask, u16 mask)
105{
Vivien Didelot370b4ff2016-09-29 12:21:57 -0400106 const u16 port_mask = BIT(mv88e6xxx_num_ports(chip)) - 1;
Vivien Didelotec561272016-09-02 14:45:33 -0400107 u16 val = (num << 12) | (mask & port_mask);
108
109 if (hask)
110 val |= GLOBAL2_TRUNK_MASK_HASK;
111
Vivien Didelot9fe850f2016-09-29 12:21:54 -0400112 return mv88e6xxx_g2_update(chip, GLOBAL2_TRUNK_MASK, val);
Vivien Didelotec561272016-09-02 14:45:33 -0400113}
114
115/* Offset 0x08: Trunk Mapping Table register */
116
117static int mv88e6xxx_g2_trunk_mapping_write(struct mv88e6xxx_chip *chip, int id,
118 u16 map)
119{
Vivien Didelot370b4ff2016-09-29 12:21:57 -0400120 const u16 port_mask = BIT(mv88e6xxx_num_ports(chip)) - 1;
Vivien Didelotec561272016-09-02 14:45:33 -0400121 u16 val = (id << 11) | (map & port_mask);
122
Vivien Didelot9fe850f2016-09-29 12:21:54 -0400123 return mv88e6xxx_g2_update(chip, GLOBAL2_TRUNK_MAPPING, val);
Vivien Didelotec561272016-09-02 14:45:33 -0400124}
125
126static int mv88e6xxx_g2_clear_trunk(struct mv88e6xxx_chip *chip)
127{
Vivien Didelot370b4ff2016-09-29 12:21:57 -0400128 const u16 port_mask = BIT(mv88e6xxx_num_ports(chip)) - 1;
Vivien Didelotec561272016-09-02 14:45:33 -0400129 int i, err;
130
131 /* Clear all eight possible Trunk Mask vectors */
132 for (i = 0; i < 8; ++i) {
133 err = mv88e6xxx_g2_trunk_mask_write(chip, i, false, port_mask);
134 if (err)
135 return err;
136 }
137
138 /* Clear all sixteen possible Trunk ID routing vectors */
139 for (i = 0; i < 16; ++i) {
140 err = mv88e6xxx_g2_trunk_mapping_write(chip, i, 0);
141 if (err)
142 return err;
143 }
144
145 return 0;
146}
147
148/* Offset 0x09: Ingress Rate Command register
149 * Offset 0x0A: Ingress Rate Data register
150 */
151
152static int mv88e6xxx_g2_clear_irl(struct mv88e6xxx_chip *chip)
153{
154 int port, err;
155
156 /* Init all Ingress Rate Limit resources of all ports */
Vivien Didelot370b4ff2016-09-29 12:21:57 -0400157 for (port = 0; port < mv88e6xxx_num_ports(chip); ++port) {
Vivien Didelotec561272016-09-02 14:45:33 -0400158 /* XXX newer chips (like 88E6390) have different 2-bit ops */
Vivien Didelot9fe850f2016-09-29 12:21:54 -0400159 err = mv88e6xxx_g2_write(chip, GLOBAL2_IRL_CMD,
160 GLOBAL2_IRL_CMD_OP_INIT_ALL |
161 (port << 8));
Vivien Didelotec561272016-09-02 14:45:33 -0400162 if (err)
163 break;
164
165 /* Wait for the operation to complete */
Vivien Didelot9fe850f2016-09-29 12:21:54 -0400166 err = mv88e6xxx_g2_wait(chip, GLOBAL2_IRL_CMD,
167 GLOBAL2_IRL_CMD_BUSY);
Vivien Didelotec561272016-09-02 14:45:33 -0400168 if (err)
169 break;
170 }
171
172 return err;
173}
174
175/* Offset 0x0D: Switch MAC/WoL/WoF register */
176
177static int mv88e6xxx_g2_switch_mac_write(struct mv88e6xxx_chip *chip,
178 unsigned int pointer, u8 data)
179{
180 u16 val = (pointer << 8) | data;
181
Vivien Didelot9fe850f2016-09-29 12:21:54 -0400182 return mv88e6xxx_g2_update(chip, GLOBAL2_SWITCH_MAC, val);
Vivien Didelotec561272016-09-02 14:45:33 -0400183}
184
185int mv88e6xxx_g2_set_switch_mac(struct mv88e6xxx_chip *chip, u8 *addr)
186{
187 int i, err;
188
189 for (i = 0; i < 6; i++) {
190 err = mv88e6xxx_g2_switch_mac_write(chip, i, addr[i]);
191 if (err)
192 break;
193 }
194
195 return err;
196}
197
198/* Offset 0x0F: Priority Override Table */
199
200static int mv88e6xxx_g2_pot_write(struct mv88e6xxx_chip *chip, int pointer,
201 u8 data)
202{
203 u16 val = (pointer << 8) | (data & 0x7);
204
Vivien Didelot9fe850f2016-09-29 12:21:54 -0400205 return mv88e6xxx_g2_update(chip, GLOBAL2_PRIO_OVERRIDE, val);
Vivien Didelotec561272016-09-02 14:45:33 -0400206}
207
208static int mv88e6xxx_g2_clear_pot(struct mv88e6xxx_chip *chip)
209{
210 int i, err;
211
212 /* Clear all sixteen possible Priority Override entries */
213 for (i = 0; i < 16; i++) {
214 err = mv88e6xxx_g2_pot_write(chip, i, 0);
215 if (err)
216 break;
217 }
218
219 return err;
220}
221
222/* Offset 0x14: EEPROM Command
Vivien Didelot98fc3c62017-01-12 18:07:16 -0500223 * Offset 0x15: EEPROM Data (for 16-bit data access)
224 * Offset 0x15: EEPROM Addr (for 8-bit data access)
Vivien Didelotec561272016-09-02 14:45:33 -0400225 */
226
227static int mv88e6xxx_g2_eeprom_wait(struct mv88e6xxx_chip *chip)
228{
Vivien Didelot9fe850f2016-09-29 12:21:54 -0400229 return mv88e6xxx_g2_wait(chip, GLOBAL2_EEPROM_CMD,
230 GLOBAL2_EEPROM_CMD_BUSY |
231 GLOBAL2_EEPROM_CMD_RUNNING);
Vivien Didelotec561272016-09-02 14:45:33 -0400232}
233
234static int mv88e6xxx_g2_eeprom_cmd(struct mv88e6xxx_chip *chip, u16 cmd)
235{
236 int err;
237
Vivien Didelot9fe850f2016-09-29 12:21:54 -0400238 err = mv88e6xxx_g2_write(chip, GLOBAL2_EEPROM_CMD, cmd);
Vivien Didelotec561272016-09-02 14:45:33 -0400239 if (err)
240 return err;
241
242 return mv88e6xxx_g2_eeprom_wait(chip);
243}
244
Vivien Didelot98fc3c62017-01-12 18:07:16 -0500245static int mv88e6xxx_g2_eeprom_read8(struct mv88e6xxx_chip *chip,
246 u16 addr, u8 *data)
247{
248 u16 cmd = GLOBAL2_EEPROM_CMD_OP_READ;
249 int err;
250
251 err = mv88e6xxx_g2_eeprom_wait(chip);
252 if (err)
253 return err;
254
255 err = mv88e6xxx_g2_write(chip, GLOBAL2_EEPROM_ADDR, addr);
256 if (err)
257 return err;
258
259 err = mv88e6xxx_g2_eeprom_cmd(chip, cmd);
260 if (err)
261 return err;
262
263 err = mv88e6xxx_g2_read(chip, GLOBAL2_EEPROM_CMD, &cmd);
264 if (err)
265 return err;
266
267 *data = cmd & 0xff;
268
269 return 0;
270}
271
272static int mv88e6xxx_g2_eeprom_write8(struct mv88e6xxx_chip *chip,
273 u16 addr, u8 data)
274{
275 u16 cmd = GLOBAL2_EEPROM_CMD_OP_WRITE | GLOBAL2_EEPROM_CMD_WRITE_EN;
276 int err;
277
278 err = mv88e6xxx_g2_eeprom_wait(chip);
279 if (err)
280 return err;
281
282 err = mv88e6xxx_g2_write(chip, GLOBAL2_EEPROM_ADDR, addr);
283 if (err)
284 return err;
285
286 return mv88e6xxx_g2_eeprom_cmd(chip, cmd | data);
287}
288
Vivien Didelotec561272016-09-02 14:45:33 -0400289static int mv88e6xxx_g2_eeprom_read16(struct mv88e6xxx_chip *chip,
290 u8 addr, u16 *data)
291{
292 u16 cmd = GLOBAL2_EEPROM_CMD_OP_READ | addr;
293 int err;
294
295 err = mv88e6xxx_g2_eeprom_wait(chip);
296 if (err)
297 return err;
298
299 err = mv88e6xxx_g2_eeprom_cmd(chip, cmd);
300 if (err)
301 return err;
302
Vivien Didelot9fe850f2016-09-29 12:21:54 -0400303 return mv88e6xxx_g2_read(chip, GLOBAL2_EEPROM_DATA, data);
Vivien Didelotec561272016-09-02 14:45:33 -0400304}
305
306static int mv88e6xxx_g2_eeprom_write16(struct mv88e6xxx_chip *chip,
307 u8 addr, u16 data)
308{
309 u16 cmd = GLOBAL2_EEPROM_CMD_OP_WRITE | addr;
310 int err;
311
312 err = mv88e6xxx_g2_eeprom_wait(chip);
313 if (err)
314 return err;
315
Vivien Didelot9fe850f2016-09-29 12:21:54 -0400316 err = mv88e6xxx_g2_write(chip, GLOBAL2_EEPROM_DATA, data);
Vivien Didelotec561272016-09-02 14:45:33 -0400317 if (err)
318 return err;
319
320 return mv88e6xxx_g2_eeprom_cmd(chip, cmd);
321}
322
Vivien Didelot98fc3c62017-01-12 18:07:16 -0500323int mv88e6xxx_g2_get_eeprom8(struct mv88e6xxx_chip *chip,
324 struct ethtool_eeprom *eeprom, u8 *data)
325{
326 unsigned int offset = eeprom->offset;
327 unsigned int len = eeprom->len;
328 int err;
329
330 eeprom->len = 0;
331
332 while (len) {
333 err = mv88e6xxx_g2_eeprom_read8(chip, offset, data);
334 if (err)
335 return err;
336
337 eeprom->len++;
338 offset++;
339 data++;
340 len--;
341 }
342
343 return 0;
344}
345
346int mv88e6xxx_g2_set_eeprom8(struct mv88e6xxx_chip *chip,
347 struct ethtool_eeprom *eeprom, u8 *data)
348{
349 unsigned int offset = eeprom->offset;
350 unsigned int len = eeprom->len;
351 int err;
352
353 eeprom->len = 0;
354
355 while (len) {
356 err = mv88e6xxx_g2_eeprom_write8(chip, offset, *data);
357 if (err)
358 return err;
359
360 eeprom->len++;
361 offset++;
362 data++;
363 len--;
364 }
365
366 return 0;
367}
368
Vivien Didelotec561272016-09-02 14:45:33 -0400369int mv88e6xxx_g2_get_eeprom16(struct mv88e6xxx_chip *chip,
370 struct ethtool_eeprom *eeprom, u8 *data)
371{
372 unsigned int offset = eeprom->offset;
373 unsigned int len = eeprom->len;
374 u16 val;
375 int err;
376
377 eeprom->len = 0;
378
379 if (offset & 1) {
380 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
381 if (err)
382 return err;
383
384 *data++ = (val >> 8) & 0xff;
385
386 offset++;
387 len--;
388 eeprom->len++;
389 }
390
391 while (len >= 2) {
392 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
393 if (err)
394 return err;
395
396 *data++ = val & 0xff;
397 *data++ = (val >> 8) & 0xff;
398
399 offset += 2;
400 len -= 2;
401 eeprom->len += 2;
402 }
403
404 if (len) {
405 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
406 if (err)
407 return err;
408
409 *data++ = val & 0xff;
410
411 offset++;
412 len--;
413 eeprom->len++;
414 }
415
416 return 0;
417}
418
419int mv88e6xxx_g2_set_eeprom16(struct mv88e6xxx_chip *chip,
420 struct ethtool_eeprom *eeprom, u8 *data)
421{
422 unsigned int offset = eeprom->offset;
423 unsigned int len = eeprom->len;
424 u16 val;
425 int err;
426
427 /* Ensure the RO WriteEn bit is set */
Vivien Didelot9fe850f2016-09-29 12:21:54 -0400428 err = mv88e6xxx_g2_read(chip, GLOBAL2_EEPROM_CMD, &val);
Vivien Didelotec561272016-09-02 14:45:33 -0400429 if (err)
430 return err;
431
432 if (!(val & GLOBAL2_EEPROM_CMD_WRITE_EN))
433 return -EROFS;
434
435 eeprom->len = 0;
436
437 if (offset & 1) {
438 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
439 if (err)
440 return err;
441
442 val = (*data++ << 8) | (val & 0xff);
443
444 err = mv88e6xxx_g2_eeprom_write16(chip, offset >> 1, val);
445 if (err)
446 return err;
447
448 offset++;
449 len--;
450 eeprom->len++;
451 }
452
453 while (len >= 2) {
454 val = *data++;
455 val |= *data++ << 8;
456
457 err = mv88e6xxx_g2_eeprom_write16(chip, offset >> 1, val);
458 if (err)
459 return err;
460
461 offset += 2;
462 len -= 2;
463 eeprom->len += 2;
464 }
465
466 if (len) {
467 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
468 if (err)
469 return err;
470
471 val = (val & 0xff00) | *data++;
472
473 err = mv88e6xxx_g2_eeprom_write16(chip, offset >> 1, val);
474 if (err)
475 return err;
476
477 offset++;
478 len--;
479 eeprom->len++;
480 }
481
482 return 0;
483}
484
485/* Offset 0x18: SMI PHY Command Register
486 * Offset 0x19: SMI PHY Data Register
487 */
488
489static int mv88e6xxx_g2_smi_phy_wait(struct mv88e6xxx_chip *chip)
490{
Vivien Didelot9fe850f2016-09-29 12:21:54 -0400491 return mv88e6xxx_g2_wait(chip, GLOBAL2_SMI_PHY_CMD,
492 GLOBAL2_SMI_PHY_CMD_BUSY);
Vivien Didelotec561272016-09-02 14:45:33 -0400493}
494
495static int mv88e6xxx_g2_smi_phy_cmd(struct mv88e6xxx_chip *chip, u16 cmd)
496{
497 int err;
498
Vivien Didelot9fe850f2016-09-29 12:21:54 -0400499 err = mv88e6xxx_g2_write(chip, GLOBAL2_SMI_PHY_CMD, cmd);
Vivien Didelotec561272016-09-02 14:45:33 -0400500 if (err)
501 return err;
502
503 return mv88e6xxx_g2_smi_phy_wait(chip);
504}
505
Andrew Lunncf3e80d2017-02-04 20:12:24 +0100506static int mv88e6xxx_g2_smi_phy_write_addr(struct mv88e6xxx_chip *chip,
507 int addr, int device, int reg,
508 bool external)
Vivien Didelotec561272016-09-02 14:45:33 -0400509{
Andrew Lunncf3e80d2017-02-04 20:12:24 +0100510 int cmd = SMI_CMD_OP_45_WRITE_ADDR | (addr << 5) | device;
Vivien Didelotec561272016-09-02 14:45:33 -0400511 int err;
512
Andrew Lunncf3e80d2017-02-04 20:12:24 +0100513 if (external)
514 cmd |= GLOBAL2_SMI_PHY_CMD_EXTERNAL;
515
516 err = mv88e6xxx_g2_smi_phy_wait(chip);
517 if (err)
518 return err;
519
520 err = mv88e6xxx_g2_write(chip, GLOBAL2_SMI_PHY_DATA, reg);
521 if (err)
522 return err;
523
524 return mv88e6xxx_g2_smi_phy_cmd(chip, cmd);
525}
526
527int mv88e6xxx_g2_smi_phy_read_c45(struct mv88e6xxx_chip *chip, int addr,
528 int reg_c45, u16 *val, bool external)
529{
530 int device = (reg_c45 >> 16) & 0x1f;
531 int reg = reg_c45 & 0xffff;
532 int err;
533 u16 cmd;
534
535 err = mv88e6xxx_g2_smi_phy_write_addr(chip, addr, device, reg,
536 external);
537 if (err)
538 return err;
539
540 cmd = GLOBAL2_SMI_PHY_CMD_OP_45_READ_DATA | (addr << 5) | device;
541
542 if (external)
543 cmd |= GLOBAL2_SMI_PHY_CMD_EXTERNAL;
544
545 err = mv88e6xxx_g2_smi_phy_cmd(chip, cmd);
546 if (err)
547 return err;
548
549 err = mv88e6xxx_g2_read(chip, GLOBAL2_SMI_PHY_DATA, val);
550 if (err)
551 return err;
552
553 err = *val;
554
555 return 0;
556}
557
558int mv88e6xxx_g2_smi_phy_read_c22(struct mv88e6xxx_chip *chip, int addr,
559 int reg, u16 *val, bool external)
560{
561 u16 cmd = GLOBAL2_SMI_PHY_CMD_OP_22_READ_DATA | (addr << 5) | reg;
562 int err;
563
564 if (external)
Andrew Lunnc61a6a72017-01-24 14:53:51 +0100565 cmd |= GLOBAL2_SMI_PHY_CMD_EXTERNAL;
566
Vivien Didelotec561272016-09-02 14:45:33 -0400567 err = mv88e6xxx_g2_smi_phy_wait(chip);
568 if (err)
569 return err;
570
571 err = mv88e6xxx_g2_smi_phy_cmd(chip, cmd);
572 if (err)
573 return err;
574
Vivien Didelot9fe850f2016-09-29 12:21:54 -0400575 return mv88e6xxx_g2_read(chip, GLOBAL2_SMI_PHY_DATA, val);
Vivien Didelotec561272016-09-02 14:45:33 -0400576}
577
Andrew Lunncf3e80d2017-02-04 20:12:24 +0100578int mv88e6xxx_g2_smi_phy_read(struct mv88e6xxx_chip *chip,
579 struct mii_bus *bus,
580 int addr, int reg, u16 *val)
581{
582 struct mv88e6xxx_mdio_bus *mdio_bus = bus->priv;
583 bool external = mdio_bus->external;
584
585 if (reg & MII_ADDR_C45)
586 return mv88e6xxx_g2_smi_phy_read_c45(chip, addr, reg, val,
587 external);
588 return mv88e6xxx_g2_smi_phy_read_c22(chip, addr, reg, val, external);
589}
590
591int mv88e6xxx_g2_smi_phy_write_c45(struct mv88e6xxx_chip *chip, int addr,
592 int reg_c45, u16 val, bool external)
593{
594 int device = (reg_c45 >> 16) & 0x1f;
595 int reg = reg_c45 & 0xffff;
596 int err;
597 u16 cmd;
598
599 err = mv88e6xxx_g2_smi_phy_write_addr(chip, addr, device, reg,
600 external);
601 if (err)
602 return err;
603
604 cmd = GLOBAL2_SMI_PHY_CMD_OP_45_WRITE_DATA | (addr << 5) | device;
605
606 if (external)
607 cmd |= GLOBAL2_SMI_PHY_CMD_EXTERNAL;
608
609 err = mv88e6xxx_g2_write(chip, GLOBAL2_SMI_PHY_DATA, val);
610 if (err)
611 return err;
612
613 err = mv88e6xxx_g2_smi_phy_cmd(chip, cmd);
614 if (err)
615 return err;
616
617 return 0;
618}
619
620int mv88e6xxx_g2_smi_phy_write_c22(struct mv88e6xxx_chip *chip, int addr,
621 int reg, u16 val, bool external)
Vivien Didelotec561272016-09-02 14:45:33 -0400622{
623 u16 cmd = GLOBAL2_SMI_PHY_CMD_OP_22_WRITE_DATA | (addr << 5) | reg;
624 int err;
625
Andrew Lunncf3e80d2017-02-04 20:12:24 +0100626 if (external)
Andrew Lunnc61a6a72017-01-24 14:53:51 +0100627 cmd |= GLOBAL2_SMI_PHY_CMD_EXTERNAL;
628
Vivien Didelotec561272016-09-02 14:45:33 -0400629 err = mv88e6xxx_g2_smi_phy_wait(chip);
630 if (err)
631 return err;
632
Vivien Didelot9fe850f2016-09-29 12:21:54 -0400633 err = mv88e6xxx_g2_write(chip, GLOBAL2_SMI_PHY_DATA, val);
Vivien Didelotec561272016-09-02 14:45:33 -0400634 if (err)
635 return err;
636
637 return mv88e6xxx_g2_smi_phy_cmd(chip, cmd);
638}
639
Andrew Lunncf3e80d2017-02-04 20:12:24 +0100640int mv88e6xxx_g2_smi_phy_write(struct mv88e6xxx_chip *chip,
641 struct mii_bus *bus,
642 int addr, int reg, u16 val)
643{
644 struct mv88e6xxx_mdio_bus *mdio_bus = bus->priv;
645 bool external = mdio_bus->external;
646
647 if (reg & MII_ADDR_C45)
648 return mv88e6xxx_g2_smi_phy_write_c45(chip, addr, reg, val,
649 external);
650
651 return mv88e6xxx_g2_smi_phy_write_c22(chip, addr, reg, val, external);
652}
653
Andrew Lunnfcd25162017-02-09 00:03:42 +0100654static int mv88e6097_watchdog_action(struct mv88e6xxx_chip *chip, int irq)
655{
656 u16 reg;
657
658 mv88e6xxx_g2_read(chip, GLOBAL2_WDOG_CONTROL, &reg);
659
660 dev_info(chip->dev, "Watchdog event: 0x%04x", reg);
661
662 return IRQ_HANDLED;
663}
664
665static void mv88e6097_watchdog_free(struct mv88e6xxx_chip *chip)
666{
667 u16 reg;
668
669 mv88e6xxx_g2_read(chip, GLOBAL2_WDOG_CONTROL, &reg);
670
671 reg &= ~(GLOBAL2_WDOG_CONTROL_EGRESS_ENABLE |
672 GLOBAL2_WDOG_CONTROL_QC_ENABLE);
673
674 mv88e6xxx_g2_write(chip, GLOBAL2_WDOG_CONTROL, reg);
675}
676
677static int mv88e6097_watchdog_setup(struct mv88e6xxx_chip *chip)
678{
679 return mv88e6xxx_g2_write(chip, GLOBAL2_WDOG_CONTROL,
680 GLOBAL2_WDOG_CONTROL_EGRESS_ENABLE |
681 GLOBAL2_WDOG_CONTROL_QC_ENABLE |
682 GLOBAL2_WDOG_CONTROL_SWRESET);
683}
684
685const struct mv88e6xxx_irq_ops mv88e6097_watchdog_ops = {
686 .irq_action = mv88e6097_watchdog_action,
687 .irq_setup = mv88e6097_watchdog_setup,
688 .irq_free = mv88e6097_watchdog_free,
689};
690
Andrew Lunn61303732017-02-09 00:03:43 +0100691static int mv88e6390_watchdog_setup(struct mv88e6xxx_chip *chip)
692{
693 return mv88e6xxx_g2_update(chip, GLOBAL2_WDOG_CONTROL,
694 GLOBAL2_WDOG_INT_ENABLE |
695 GLOBAL2_WDOG_CUT_THROUGH |
696 GLOBAL2_WDOG_QUEUE_CONTROLLER |
697 GLOBAL2_WDOG_EGRESS |
698 GLOBAL2_WDOG_FORCE_IRQ);
699}
700
701static int mv88e6390_watchdog_action(struct mv88e6xxx_chip *chip, int irq)
702{
703 int err;
704 u16 reg;
705
706 mv88e6xxx_g2_write(chip, GLOBAL2_WDOG_CONTROL, GLOBAL2_WDOG_EVENT);
707 err = mv88e6xxx_g2_read(chip, GLOBAL2_WDOG_CONTROL, &reg);
708
709 dev_info(chip->dev, "Watchdog event: 0x%04x",
710 reg & GLOBAL2_WDOG_DATA_MASK);
711
712 mv88e6xxx_g2_write(chip, GLOBAL2_WDOG_CONTROL, GLOBAL2_WDOG_HISTORY);
713 err = mv88e6xxx_g2_read(chip, GLOBAL2_WDOG_CONTROL, &reg);
714
715 dev_info(chip->dev, "Watchdog history: 0x%04x",
716 reg & GLOBAL2_WDOG_DATA_MASK);
717
718 /* Trigger a software reset to try to recover the switch */
719 if (chip->info->ops->reset)
720 chip->info->ops->reset(chip);
721
722 mv88e6390_watchdog_setup(chip);
723
724 return IRQ_HANDLED;
725}
726
727static void mv88e6390_watchdog_free(struct mv88e6xxx_chip *chip)
728{
729 mv88e6xxx_g2_update(chip, GLOBAL2_WDOG_CONTROL,
730 GLOBAL2_WDOG_INT_ENABLE);
731}
732
733const struct mv88e6xxx_irq_ops mv88e6390_watchdog_ops = {
734 .irq_action = mv88e6390_watchdog_action,
735 .irq_setup = mv88e6390_watchdog_setup,
736 .irq_free = mv88e6390_watchdog_free,
737};
738
Andrew Lunnfcd25162017-02-09 00:03:42 +0100739static irqreturn_t mv88e6xxx_g2_watchdog_thread_fn(int irq, void *dev_id)
740{
741 struct mv88e6xxx_chip *chip = dev_id;
742 irqreturn_t ret = IRQ_NONE;
743
744 mutex_lock(&chip->reg_lock);
745 if (chip->info->ops->watchdog_ops->irq_action)
746 ret = chip->info->ops->watchdog_ops->irq_action(chip, irq);
747 mutex_unlock(&chip->reg_lock);
748
749 return ret;
750}
751
752static void mv88e6xxx_g2_watchdog_free(struct mv88e6xxx_chip *chip)
753{
754 mutex_lock(&chip->reg_lock);
755 if (chip->info->ops->watchdog_ops->irq_free)
756 chip->info->ops->watchdog_ops->irq_free(chip);
757 mutex_unlock(&chip->reg_lock);
758
759 free_irq(chip->watchdog_irq, chip);
760 irq_dispose_mapping(chip->watchdog_irq);
761}
762
763static int mv88e6xxx_g2_watchdog_setup(struct mv88e6xxx_chip *chip)
764{
765 int err;
766
767 chip->watchdog_irq = irq_find_mapping(chip->g2_irq.domain,
768 GLOBAL2_INT_SOURCE_WATCHDOG);
769 if (chip->watchdog_irq < 0)
770 return chip->watchdog_irq;
771
772 err = request_threaded_irq(chip->watchdog_irq, NULL,
773 mv88e6xxx_g2_watchdog_thread_fn,
774 IRQF_ONESHOT | IRQF_TRIGGER_FALLING,
775 "mv88e6xxx-watchdog", chip);
776 if (err)
777 return err;
778
779 mutex_lock(&chip->reg_lock);
780 if (chip->info->ops->watchdog_ops->irq_setup)
781 err = chip->info->ops->watchdog_ops->irq_setup(chip);
782 mutex_unlock(&chip->reg_lock);
783
784 return err;
785}
786
Andrew Lunndc30c352016-10-16 19:56:49 +0200787static void mv88e6xxx_g2_irq_mask(struct irq_data *d)
788{
789 struct mv88e6xxx_chip *chip = irq_data_get_irq_chip_data(d);
790 unsigned int n = d->hwirq;
791
792 chip->g2_irq.masked |= (1 << n);
793}
794
795static void mv88e6xxx_g2_irq_unmask(struct irq_data *d)
796{
797 struct mv88e6xxx_chip *chip = irq_data_get_irq_chip_data(d);
798 unsigned int n = d->hwirq;
799
800 chip->g2_irq.masked &= ~(1 << n);
801}
802
803static irqreturn_t mv88e6xxx_g2_irq_thread_fn(int irq, void *dev_id)
804{
805 struct mv88e6xxx_chip *chip = dev_id;
806 unsigned int nhandled = 0;
807 unsigned int sub_irq;
808 unsigned int n;
809 int err;
810 u16 reg;
811
812 mutex_lock(&chip->reg_lock);
813 err = mv88e6xxx_g2_read(chip, GLOBAL2_INT_SOURCE, &reg);
814 mutex_unlock(&chip->reg_lock);
815 if (err)
816 goto out;
817
818 for (n = 0; n < 16; ++n) {
819 if (reg & (1 << n)) {
820 sub_irq = irq_find_mapping(chip->g2_irq.domain, n);
821 handle_nested_irq(sub_irq);
822 ++nhandled;
823 }
824 }
825out:
826 return (nhandled > 0 ? IRQ_HANDLED : IRQ_NONE);
827}
828
829static void mv88e6xxx_g2_irq_bus_lock(struct irq_data *d)
830{
831 struct mv88e6xxx_chip *chip = irq_data_get_irq_chip_data(d);
832
833 mutex_lock(&chip->reg_lock);
834}
835
836static void mv88e6xxx_g2_irq_bus_sync_unlock(struct irq_data *d)
837{
838 struct mv88e6xxx_chip *chip = irq_data_get_irq_chip_data(d);
839
840 mv88e6xxx_g2_write(chip, GLOBAL2_INT_MASK, ~chip->g2_irq.masked);
841
842 mutex_unlock(&chip->reg_lock);
843}
844
845static struct irq_chip mv88e6xxx_g2_irq_chip = {
846 .name = "mv88e6xxx-g2",
847 .irq_mask = mv88e6xxx_g2_irq_mask,
848 .irq_unmask = mv88e6xxx_g2_irq_unmask,
849 .irq_bus_lock = mv88e6xxx_g2_irq_bus_lock,
850 .irq_bus_sync_unlock = mv88e6xxx_g2_irq_bus_sync_unlock,
851};
852
853static int mv88e6xxx_g2_irq_domain_map(struct irq_domain *d,
854 unsigned int irq,
855 irq_hw_number_t hwirq)
856{
857 struct mv88e6xxx_chip *chip = d->host_data;
858
859 irq_set_chip_data(irq, d->host_data);
860 irq_set_chip_and_handler(irq, &chip->g2_irq.chip, handle_level_irq);
861 irq_set_noprobe(irq);
862
863 return 0;
864}
865
866static const struct irq_domain_ops mv88e6xxx_g2_irq_domain_ops = {
867 .map = mv88e6xxx_g2_irq_domain_map,
868 .xlate = irq_domain_xlate_twocell,
869};
870
871void mv88e6xxx_g2_irq_free(struct mv88e6xxx_chip *chip)
872{
873 int irq, virq;
874
Andrew Lunnfcd25162017-02-09 00:03:42 +0100875 mv88e6xxx_g2_watchdog_free(chip);
876
Andrew Lunn8e757eb2016-11-20 20:14:18 +0100877 free_irq(chip->device_irq, chip);
878 irq_dispose_mapping(chip->device_irq);
879
Andrew Lunndc30c352016-10-16 19:56:49 +0200880 for (irq = 0; irq < 16; irq++) {
881 virq = irq_find_mapping(chip->g2_irq.domain, irq);
882 irq_dispose_mapping(virq);
883 }
884
885 irq_domain_remove(chip->g2_irq.domain);
886}
887
888int mv88e6xxx_g2_irq_setup(struct mv88e6xxx_chip *chip)
889{
Andrew Lunn8e757eb2016-11-20 20:14:18 +0100890 int err, irq, virq;
Andrew Lunndc30c352016-10-16 19:56:49 +0200891
892 if (!chip->dev->of_node)
893 return -EINVAL;
894
895 chip->g2_irq.domain = irq_domain_add_simple(
896 chip->dev->of_node, 16, 0, &mv88e6xxx_g2_irq_domain_ops, chip);
897 if (!chip->g2_irq.domain)
898 return -ENOMEM;
899
900 for (irq = 0; irq < 16; irq++)
901 irq_create_mapping(chip->g2_irq.domain, irq);
902
903 chip->g2_irq.chip = mv88e6xxx_g2_irq_chip;
904 chip->g2_irq.masked = ~0;
905
Andrew Lunn8e757eb2016-11-20 20:14:18 +0100906 chip->device_irq = irq_find_mapping(chip->g1_irq.domain,
907 GLOBAL_STATUS_IRQ_DEVICE);
908 if (chip->device_irq < 0) {
909 err = chip->device_irq;
Andrew Lunndc30c352016-10-16 19:56:49 +0200910 goto out;
911 }
912
Andrew Lunn8e757eb2016-11-20 20:14:18 +0100913 err = request_threaded_irq(chip->device_irq, NULL,
914 mv88e6xxx_g2_irq_thread_fn,
915 IRQF_ONESHOT, "mv88e6xxx-g1", chip);
Andrew Lunndc30c352016-10-16 19:56:49 +0200916 if (err)
917 goto out;
918
Andrew Lunnfcd25162017-02-09 00:03:42 +0100919 return mv88e6xxx_g2_watchdog_setup(chip);
Andrew Lunn8e757eb2016-11-20 20:14:18 +0100920
Andrew Lunndc30c352016-10-16 19:56:49 +0200921out:
Andrew Lunn8e757eb2016-11-20 20:14:18 +0100922 for (irq = 0; irq < 16; irq++) {
923 virq = irq_find_mapping(chip->g2_irq.domain, irq);
924 irq_dispose_mapping(virq);
925 }
926
927 irq_domain_remove(chip->g2_irq.domain);
Andrew Lunndc30c352016-10-16 19:56:49 +0200928
929 return err;
930}
931
Vivien Didelotec561272016-09-02 14:45:33 -0400932int mv88e6xxx_g2_setup(struct mv88e6xxx_chip *chip)
933{
934 u16 reg;
935 int err;
936
Vivien Didelotec561272016-09-02 14:45:33 -0400937 /* Ignore removed tag data on doubly tagged packets, disable
938 * flow control messages, force flow control priority to the
939 * highest, and send all special multicast frames to the CPU
940 * port at the highest priority.
941 */
942 reg = GLOBAL2_SWITCH_MGMT_FORCE_FLOW_CTRL_PRI | (0x7 << 4);
943 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_0X) ||
944 mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_2X))
945 reg |= GLOBAL2_SWITCH_MGMT_RSVD2CPU | 0x7;
Vivien Didelot9fe850f2016-09-29 12:21:54 -0400946 err = mv88e6xxx_g2_write(chip, GLOBAL2_SWITCH_MGMT, reg);
Vivien Didelotec561272016-09-02 14:45:33 -0400947 if (err)
948 return err;
949
950 /* Program the DSA routing table. */
951 err = mv88e6xxx_g2_set_device_mapping(chip);
952 if (err)
953 return err;
954
955 /* Clear all trunk masks and mapping. */
956 err = mv88e6xxx_g2_clear_trunk(chip);
957 if (err)
958 return err;
959
960 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_IRL)) {
961 /* Disable ingress rate limiting by resetting all per port
962 * ingress rate limit resources to their initial state.
963 */
964 err = mv88e6xxx_g2_clear_irl(chip);
965 if (err)
966 return err;
967 }
968
969 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_PVT)) {
970 /* Initialize Cross-chip Port VLAN Table to reset defaults */
Vivien Didelot9fe850f2016-09-29 12:21:54 -0400971 err = mv88e6xxx_g2_write(chip, GLOBAL2_PVT_ADDR,
972 GLOBAL2_PVT_ADDR_OP_INIT_ONES);
Vivien Didelotec561272016-09-02 14:45:33 -0400973 if (err)
974 return err;
975 }
976
977 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_POT)) {
978 /* Clear the priority override table. */
979 err = mv88e6xxx_g2_clear_pot(chip);
980 if (err)
981 return err;
982 }
983
984 return 0;
985}