1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Marvell 88E6xxx Switch Global (1) Registers support
5 * Copyright (c) 2008 Marvell Semiconductor
7 * Copyright (c) 2016-2017 Savoir-faire Linux Inc.
8 * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
11 #include <linux/bitfield.h>
16 int mv88e6xxx_g1_read(struct mv88e6xxx_chip *chip, int reg, u16 *val)
18 int addr = chip->info->global1_addr;
20 return mv88e6xxx_read(chip, addr, reg, val);
23 int mv88e6xxx_g1_write(struct mv88e6xxx_chip *chip, int reg, u16 val)
25 int addr = chip->info->global1_addr;
27 return mv88e6xxx_write(chip, addr, reg, val);
30 int mv88e6xxx_g1_wait_bit(struct mv88e6xxx_chip *chip, int reg, int
33 return mv88e6xxx_wait_bit(chip, chip->info->global1_addr, reg,
37 int mv88e6xxx_g1_wait_mask(struct mv88e6xxx_chip *chip, int reg,
40 return mv88e6xxx_wait_mask(chip, chip->info->global1_addr, reg,
44 /* Offset 0x00: Switch Global Status Register */
46 static int mv88e6185_g1_wait_ppu_disabled(struct mv88e6xxx_chip *chip)
48 return mv88e6xxx_g1_wait_mask(chip, MV88E6XXX_G1_STS,
49 MV88E6185_G1_STS_PPU_STATE_MASK,
50 MV88E6185_G1_STS_PPU_STATE_DISABLED);
53 static int mv88e6185_g1_wait_ppu_polling(struct mv88e6xxx_chip *chip)
55 return mv88e6xxx_g1_wait_mask(chip, MV88E6XXX_G1_STS,
56 MV88E6185_G1_STS_PPU_STATE_MASK,
57 MV88E6185_G1_STS_PPU_STATE_POLLING);
60 static int mv88e6352_g1_wait_ppu_polling(struct mv88e6xxx_chip *chip)
62 int bit = __bf_shf(MV88E6352_G1_STS_PPU_STATE);
64 return mv88e6xxx_g1_wait_bit(chip, MV88E6XXX_G1_STS, bit, 1);
67 static int mv88e6xxx_g1_wait_init_ready(struct mv88e6xxx_chip *chip)
69 int bit = __bf_shf(MV88E6XXX_G1_STS_INIT_READY);
71 /* Wait up to 1 second for the switch to be ready. The InitReady bit 11
72 * is set to a one when all units inside the device (ATU, VTU, etc.)
73 * have finished their initialization and are ready to accept frames.
75 return mv88e6xxx_g1_wait_bit(chip, MV88E6XXX_G1_STS, bit, 1);
78 void mv88e6xxx_g1_wait_eeprom_done(struct mv88e6xxx_chip *chip)
80 const unsigned long timeout = jiffies + 1 * HZ;
84 /* Wait up to 1 second for the switch to finish reading the
87 while (time_before(jiffies, timeout)) {
88 err = mv88e6xxx_g1_read(chip, MV88E6XXX_G1_STS, &val);
90 dev_err(chip->dev, "Error reading status");
94 /* If the switch is still resetting, it may not
95 * respond on the bus, and so MDIO read returns
96 * 0xffff. Differentiate between that, and waiting for
97 * the EEPROM to be done by bit 0 being set.
100 val & BIT(MV88E6XXX_G1_STS_IRQ_EEPROM_DONE))
103 usleep_range(1000, 2000);
106 dev_err(chip->dev, "Timeout waiting for EEPROM done");
109 /* Offset 0x01: Switch MAC Address Register Bytes 0 & 1
110 * Offset 0x02: Switch MAC Address Register Bytes 2 & 3
111 * Offset 0x03: Switch MAC Address Register Bytes 4 & 5
113 int mv88e6xxx_g1_set_switch_mac(struct mv88e6xxx_chip *chip, u8 *addr)
118 reg = (addr[0] << 8) | addr[1];
119 err = mv88e6xxx_g1_write(chip, MV88E6XXX_G1_MAC_01, reg);
123 reg = (addr[2] << 8) | addr[3];
124 err = mv88e6xxx_g1_write(chip, MV88E6XXX_G1_MAC_23, reg);
128 reg = (addr[4] << 8) | addr[5];
129 err = mv88e6xxx_g1_write(chip, MV88E6XXX_G1_MAC_45, reg);
136 /* Offset 0x04: Switch Global Control Register */
138 int mv88e6185_g1_reset(struct mv88e6xxx_chip *chip)
143 /* Set the SWReset bit 15 along with the PPUEn bit 14, to also restart
144 * the PPU, including re-doing PHY detection and initialization
146 err = mv88e6xxx_g1_read(chip, MV88E6XXX_G1_CTL1, &val);
150 val |= MV88E6XXX_G1_CTL1_SW_RESET;
151 val |= MV88E6XXX_G1_CTL1_PPU_ENABLE;
153 err = mv88e6xxx_g1_write(chip, MV88E6XXX_G1_CTL1, val);
157 err = mv88e6xxx_g1_wait_init_ready(chip);
161 return mv88e6185_g1_wait_ppu_polling(chip);
164 int mv88e6250_g1_reset(struct mv88e6xxx_chip *chip)
169 /* Set the SWReset bit 15 */
170 err = mv88e6xxx_g1_read(chip, MV88E6XXX_G1_CTL1, &val);
174 val |= MV88E6XXX_G1_CTL1_SW_RESET;
176 err = mv88e6xxx_g1_write(chip, MV88E6XXX_G1_CTL1, val);
180 return mv88e6xxx_g1_wait_init_ready(chip);
183 int mv88e6352_g1_reset(struct mv88e6xxx_chip *chip)
187 err = mv88e6250_g1_reset(chip);
191 return mv88e6352_g1_wait_ppu_polling(chip);
194 int mv88e6185_g1_ppu_enable(struct mv88e6xxx_chip *chip)
199 err = mv88e6xxx_g1_read(chip, MV88E6XXX_G1_CTL1, &val);
203 val |= MV88E6XXX_G1_CTL1_PPU_ENABLE;
205 err = mv88e6xxx_g1_write(chip, MV88E6XXX_G1_CTL1, val);
209 return mv88e6185_g1_wait_ppu_polling(chip);
212 int mv88e6185_g1_ppu_disable(struct mv88e6xxx_chip *chip)
217 err = mv88e6xxx_g1_read(chip, MV88E6XXX_G1_CTL1, &val);
221 val &= ~MV88E6XXX_G1_CTL1_PPU_ENABLE;
223 err = mv88e6xxx_g1_write(chip, MV88E6XXX_G1_CTL1, val);
227 return mv88e6185_g1_wait_ppu_disabled(chip);
230 int mv88e6185_g1_set_max_frame_size(struct mv88e6xxx_chip *chip, int mtu)
235 mtu += ETH_HLEN + ETH_FCS_LEN;
237 err = mv88e6xxx_g1_read(chip, MV88E6XXX_G1_CTL1, &val);
241 val &= ~MV88E6185_G1_CTL1_MAX_FRAME_1632;
244 val |= MV88E6185_G1_CTL1_MAX_FRAME_1632;
246 return mv88e6xxx_g1_write(chip, MV88E6XXX_G1_CTL1, val);
249 /* Offset 0x10: IP-PRI Mapping Register 0
250 * Offset 0x11: IP-PRI Mapping Register 1
251 * Offset 0x12: IP-PRI Mapping Register 2
252 * Offset 0x13: IP-PRI Mapping Register 3
253 * Offset 0x14: IP-PRI Mapping Register 4
254 * Offset 0x15: IP-PRI Mapping Register 5
255 * Offset 0x16: IP-PRI Mapping Register 6
256 * Offset 0x17: IP-PRI Mapping Register 7
259 int mv88e6085_g1_ip_pri_map(struct mv88e6xxx_chip *chip)
263 /* Reset the IP TOS/DiffServ/Traffic priorities to defaults */
264 err = mv88e6xxx_g1_write(chip, MV88E6XXX_G1_IP_PRI_0, 0x0000);
268 err = mv88e6xxx_g1_write(chip, MV88E6XXX_G1_IP_PRI_1, 0x0000);
272 err = mv88e6xxx_g1_write(chip, MV88E6XXX_G1_IP_PRI_2, 0x5555);
276 err = mv88e6xxx_g1_write(chip, MV88E6XXX_G1_IP_PRI_3, 0x5555);
280 err = mv88e6xxx_g1_write(chip, MV88E6XXX_G1_IP_PRI_4, 0xaaaa);
284 err = mv88e6xxx_g1_write(chip, MV88E6XXX_G1_IP_PRI_5, 0xaaaa);
288 err = mv88e6xxx_g1_write(chip, MV88E6XXX_G1_IP_PRI_6, 0xffff);
292 err = mv88e6xxx_g1_write(chip, MV88E6XXX_G1_IP_PRI_7, 0xffff);
299 /* Offset 0x18: IEEE-PRI Register */
301 int mv88e6085_g1_ieee_pri_map(struct mv88e6xxx_chip *chip)
303 /* Reset the IEEE Tag priorities to defaults */
304 return mv88e6xxx_g1_write(chip, MV88E6XXX_G1_IEEE_PRI, 0xfa41);
307 int mv88e6250_g1_ieee_pri_map(struct mv88e6xxx_chip *chip)
309 /* Reset the IEEE Tag priorities to defaults */
310 return mv88e6xxx_g1_write(chip, MV88E6XXX_G1_IEEE_PRI, 0xfa50);
313 /* Offset 0x1a: Monitor Control */
314 /* Offset 0x1a: Monitor & MGMT Control on some devices */
316 int mv88e6095_g1_set_egress_port(struct mv88e6xxx_chip *chip,
317 enum mv88e6xxx_egress_direction direction,
323 err = mv88e6xxx_g1_read(chip, MV88E6185_G1_MONITOR_CTL, ®);
328 case MV88E6XXX_EGRESS_DIR_INGRESS:
329 reg &= ~MV88E6185_G1_MONITOR_CTL_INGRESS_DEST_MASK;
331 __bf_shf(MV88E6185_G1_MONITOR_CTL_INGRESS_DEST_MASK);
333 case MV88E6XXX_EGRESS_DIR_EGRESS:
334 reg &= ~MV88E6185_G1_MONITOR_CTL_EGRESS_DEST_MASK;
336 __bf_shf(MV88E6185_G1_MONITOR_CTL_EGRESS_DEST_MASK);
342 return mv88e6xxx_g1_write(chip, MV88E6185_G1_MONITOR_CTL, reg);
345 /* Older generations also call this the ARP destination. It has been
346 * generalized in more modern devices such that more than ARP can
349 int mv88e6095_g1_set_cpu_port(struct mv88e6xxx_chip *chip, int port)
354 err = mv88e6xxx_g1_read(chip, MV88E6185_G1_MONITOR_CTL, ®);
358 reg &= ~MV88E6185_G1_MONITOR_CTL_ARP_DEST_MASK;
359 reg |= port << __bf_shf(MV88E6185_G1_MONITOR_CTL_ARP_DEST_MASK);
361 return mv88e6xxx_g1_write(chip, MV88E6185_G1_MONITOR_CTL, reg);
364 static int mv88e6390_g1_monitor_write(struct mv88e6xxx_chip *chip,
365 u16 pointer, u8 data)
369 reg = MV88E6390_G1_MONITOR_MGMT_CTL_UPDATE | pointer | data;
371 return mv88e6xxx_g1_write(chip, MV88E6390_G1_MONITOR_MGMT_CTL, reg);
374 int mv88e6390_g1_set_egress_port(struct mv88e6xxx_chip *chip,
375 enum mv88e6xxx_egress_direction direction,
381 case MV88E6XXX_EGRESS_DIR_INGRESS:
382 ptr = MV88E6390_G1_MONITOR_MGMT_CTL_PTR_INGRESS_DEST;
384 case MV88E6XXX_EGRESS_DIR_EGRESS:
385 ptr = MV88E6390_G1_MONITOR_MGMT_CTL_PTR_EGRESS_DEST;
391 return mv88e6390_g1_monitor_write(chip, ptr, port);
394 int mv88e6390_g1_set_cpu_port(struct mv88e6xxx_chip *chip, int port)
396 u16 ptr = MV88E6390_G1_MONITOR_MGMT_CTL_PTR_CPU_DEST;
398 /* Use the default high priority for management frames sent to
401 port |= MV88E6390_G1_MONITOR_MGMT_CTL_PTR_CPU_DEST_MGMTPRI;
403 return mv88e6390_g1_monitor_write(chip, ptr, port);
406 int mv88e6390_g1_set_ptp_cpu_port(struct mv88e6xxx_chip *chip, int port)
408 u16 ptr = MV88E6390_G1_MONITOR_MGMT_CTL_PTR_PTP_CPU_DEST;
410 /* Use the default high priority for PTP frames sent to
413 port |= MV88E6390_G1_MONITOR_MGMT_CTL_PTR_CPU_DEST_MGMTPRI;
415 return mv88e6390_g1_monitor_write(chip, ptr, port);
418 int mv88e6390_g1_mgmt_rsvd2cpu(struct mv88e6xxx_chip *chip)
423 /* 01:80:c2:00:00:00-01:80:c2:00:00:07 are Management */
424 ptr = MV88E6390_G1_MONITOR_MGMT_CTL_PTR_0180C200000XLO;
425 err = mv88e6390_g1_monitor_write(chip, ptr, 0xff);
429 /* 01:80:c2:00:00:08-01:80:c2:00:00:0f are Management */
430 ptr = MV88E6390_G1_MONITOR_MGMT_CTL_PTR_0180C200000XHI;
431 err = mv88e6390_g1_monitor_write(chip, ptr, 0xff);
435 /* 01:80:c2:00:00:20-01:80:c2:00:00:27 are Management */
436 ptr = MV88E6390_G1_MONITOR_MGMT_CTL_PTR_0180C200002XLO;
437 err = mv88e6390_g1_monitor_write(chip, ptr, 0xff);
441 /* 01:80:c2:00:00:28-01:80:c2:00:00:2f are Management */
442 ptr = MV88E6390_G1_MONITOR_MGMT_CTL_PTR_0180C200002XHI;
443 err = mv88e6390_g1_monitor_write(chip, ptr, 0xff);
450 /* Offset 0x1c: Global Control 2 */
452 static int mv88e6xxx_g1_ctl2_mask(struct mv88e6xxx_chip *chip, u16 mask,
458 err = mv88e6xxx_g1_read(chip, MV88E6XXX_G1_CTL2, ®);
465 return mv88e6xxx_g1_write(chip, MV88E6XXX_G1_CTL2, reg);
468 int mv88e6185_g1_set_cascade_port(struct mv88e6xxx_chip *chip, int port)
470 const u16 mask = MV88E6185_G1_CTL2_CASCADE_PORT_MASK;
472 return mv88e6xxx_g1_ctl2_mask(chip, mask, port << __bf_shf(mask));
475 int mv88e6085_g1_rmu_disable(struct mv88e6xxx_chip *chip)
477 return mv88e6xxx_g1_ctl2_mask(chip, MV88E6085_G1_CTL2_P10RM |
478 MV88E6085_G1_CTL2_RM_ENABLE, 0);
481 int mv88e6352_g1_rmu_disable(struct mv88e6xxx_chip *chip)
483 return mv88e6xxx_g1_ctl2_mask(chip, MV88E6352_G1_CTL2_RMU_MODE_MASK,
484 MV88E6352_G1_CTL2_RMU_MODE_DISABLED);
487 int mv88e6390_g1_rmu_disable(struct mv88e6xxx_chip *chip)
489 return mv88e6xxx_g1_ctl2_mask(chip, MV88E6390_G1_CTL2_RMU_MODE_MASK,
490 MV88E6390_G1_CTL2_RMU_MODE_DISABLED);
493 int mv88e6390_g1_stats_set_histogram(struct mv88e6xxx_chip *chip)
495 return mv88e6xxx_g1_ctl2_mask(chip, MV88E6390_G1_CTL2_HIST_MODE_MASK,
496 MV88E6390_G1_CTL2_HIST_MODE_RX |
497 MV88E6390_G1_CTL2_HIST_MODE_TX);
500 int mv88e6xxx_g1_set_device_number(struct mv88e6xxx_chip *chip, int index)
502 return mv88e6xxx_g1_ctl2_mask(chip,
503 MV88E6XXX_G1_CTL2_DEVICE_NUMBER_MASK,
507 /* Offset 0x1d: Statistics Operation 2 */
509 static int mv88e6xxx_g1_stats_wait(struct mv88e6xxx_chip *chip)
511 int bit = __bf_shf(MV88E6XXX_G1_STATS_OP_BUSY);
513 return mv88e6xxx_g1_wait_bit(chip, MV88E6XXX_G1_STATS_OP, bit, 0);
516 int mv88e6095_g1_stats_set_histogram(struct mv88e6xxx_chip *chip)
521 err = mv88e6xxx_g1_read(chip, MV88E6XXX_G1_STATS_OP, &val);
525 val |= MV88E6XXX_G1_STATS_OP_HIST_RX_TX;
527 err = mv88e6xxx_g1_write(chip, MV88E6XXX_G1_STATS_OP, val);
532 int mv88e6xxx_g1_stats_snapshot(struct mv88e6xxx_chip *chip, int port)
536 /* Snapshot the hardware statistics counters for this port. */
537 err = mv88e6xxx_g1_write(chip, MV88E6XXX_G1_STATS_OP,
538 MV88E6XXX_G1_STATS_OP_BUSY |
539 MV88E6XXX_G1_STATS_OP_CAPTURE_PORT |
540 MV88E6XXX_G1_STATS_OP_HIST_RX_TX | port);
544 /* Wait for the snapshotting to complete. */
545 return mv88e6xxx_g1_stats_wait(chip);
548 int mv88e6320_g1_stats_snapshot(struct mv88e6xxx_chip *chip, int port)
550 port = (port + 1) << 5;
552 return mv88e6xxx_g1_stats_snapshot(chip, port);
555 int mv88e6390_g1_stats_snapshot(struct mv88e6xxx_chip *chip, int port)
559 port = (port + 1) << 5;
561 /* Snapshot the hardware statistics counters for this port. */
562 err = mv88e6xxx_g1_write(chip, MV88E6XXX_G1_STATS_OP,
563 MV88E6XXX_G1_STATS_OP_BUSY |
564 MV88E6XXX_G1_STATS_OP_CAPTURE_PORT | port);
568 /* Wait for the snapshotting to complete. */
569 return mv88e6xxx_g1_stats_wait(chip);
572 void mv88e6xxx_g1_stats_read(struct mv88e6xxx_chip *chip, int stat, u32 *val)
580 err = mv88e6xxx_g1_write(chip, MV88E6XXX_G1_STATS_OP,
581 MV88E6XXX_G1_STATS_OP_BUSY |
582 MV88E6XXX_G1_STATS_OP_READ_CAPTURED | stat);
586 err = mv88e6xxx_g1_stats_wait(chip);
590 err = mv88e6xxx_g1_read(chip, MV88E6XXX_G1_STATS_COUNTER_32, ®);
596 err = mv88e6xxx_g1_read(chip, MV88E6XXX_G1_STATS_COUNTER_01, ®);
603 int mv88e6xxx_g1_stats_clear(struct mv88e6xxx_chip *chip)
608 err = mv88e6xxx_g1_read(chip, MV88E6XXX_G1_STATS_OP, &val);
612 /* Keep the histogram mode bits */
613 val &= MV88E6XXX_G1_STATS_OP_HIST_RX_TX;
614 val |= MV88E6XXX_G1_STATS_OP_BUSY | MV88E6XXX_G1_STATS_OP_FLUSH_ALL;
616 err = mv88e6xxx_g1_write(chip, MV88E6XXX_G1_STATS_OP, val);
620 /* Wait for the flush to complete. */
621 return mv88e6xxx_g1_stats_wait(chip);