2 * net/dsa/mv88e6xxx.c - Marvell 88e6xxx switch chip support
3 * Copyright (c) 2008 Marvell Semiconductor
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
11 #include <linux/delay.h>
12 #include <linux/jiffies.h>
13 #include <linux/list.h>
14 #include <linux/module.h>
15 #include <linux/netdevice.h>
16 #include <linux/phy.h>
18 #include "mv88e6xxx.h"
20 /* If the switch's ADDR[4:0] strap pins are strapped to zero, it will
21 * use all 32 SMI bus addresses on its SMI bus, and all switch registers
22 * will be directly accessible on some {device address,register address}
23 * pair. If the ADDR[4:0] pins are not strapped to zero, the switch
24 * will only respond to SMI transactions to that specific address, and
25 * an indirect addressing mechanism needs to be used to access its
28 static int mv88e6xxx_reg_wait_ready(struct mii_bus *bus, int sw_addr)
33 for (i = 0; i < 16; i++) {
34 ret = mdiobus_read(bus, sw_addr, 0);
38 if ((ret & 0x8000) == 0)
45 int __mv88e6xxx_reg_read(struct mii_bus *bus, int sw_addr, int addr, int reg)
50 return mdiobus_read(bus, addr, reg);
52 /* Wait for the bus to become free. */
53 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
57 /* Transmit the read command. */
58 ret = mdiobus_write(bus, sw_addr, 0, 0x9800 | (addr << 5) | reg);
62 /* Wait for the read command to complete. */
63 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
68 ret = mdiobus_read(bus, sw_addr, 1);
75 int mv88e6xxx_reg_read(struct dsa_switch *ds, int addr, int reg)
77 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
78 struct mii_bus *bus = dsa_host_dev_to_mii_bus(ds->master_dev);
84 mutex_lock(&ps->smi_mutex);
85 ret = __mv88e6xxx_reg_read(bus, ds->pd->sw_addr, addr, reg);
86 mutex_unlock(&ps->smi_mutex);
91 int __mv88e6xxx_reg_write(struct mii_bus *bus, int sw_addr, int addr,
97 return mdiobus_write(bus, addr, reg, val);
99 /* Wait for the bus to become free. */
100 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
104 /* Transmit the data to write. */
105 ret = mdiobus_write(bus, sw_addr, 1, val);
109 /* Transmit the write command. */
110 ret = mdiobus_write(bus, sw_addr, 0, 0x9400 | (addr << 5) | reg);
114 /* Wait for the write command to complete. */
115 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
122 int mv88e6xxx_reg_write(struct dsa_switch *ds, int addr, int reg, u16 val)
124 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
125 struct mii_bus *bus = dsa_host_dev_to_mii_bus(ds->master_dev);
131 mutex_lock(&ps->smi_mutex);
132 ret = __mv88e6xxx_reg_write(bus, ds->pd->sw_addr, addr, reg, val);
133 mutex_unlock(&ps->smi_mutex);
138 int mv88e6xxx_config_prio(struct dsa_switch *ds)
140 /* Configure the IP ToS mapping registers. */
141 REG_WRITE(REG_GLOBAL, 0x10, 0x0000);
142 REG_WRITE(REG_GLOBAL, 0x11, 0x0000);
143 REG_WRITE(REG_GLOBAL, 0x12, 0x5555);
144 REG_WRITE(REG_GLOBAL, 0x13, 0x5555);
145 REG_WRITE(REG_GLOBAL, 0x14, 0xaaaa);
146 REG_WRITE(REG_GLOBAL, 0x15, 0xaaaa);
147 REG_WRITE(REG_GLOBAL, 0x16, 0xffff);
148 REG_WRITE(REG_GLOBAL, 0x17, 0xffff);
150 /* Configure the IEEE 802.1p priority mapping register. */
151 REG_WRITE(REG_GLOBAL, 0x18, 0xfa41);
156 int mv88e6xxx_set_addr_direct(struct dsa_switch *ds, u8 *addr)
158 REG_WRITE(REG_GLOBAL, 0x01, (addr[0] << 8) | addr[1]);
159 REG_WRITE(REG_GLOBAL, 0x02, (addr[2] << 8) | addr[3]);
160 REG_WRITE(REG_GLOBAL, 0x03, (addr[4] << 8) | addr[5]);
165 int mv88e6xxx_set_addr_indirect(struct dsa_switch *ds, u8 *addr)
170 for (i = 0; i < 6; i++) {
173 /* Write the MAC address byte. */
174 REG_WRITE(REG_GLOBAL2, 0x0d, 0x8000 | (i << 8) | addr[i]);
176 /* Wait for the write to complete. */
177 for (j = 0; j < 16; j++) {
178 ret = REG_READ(REG_GLOBAL2, 0x0d);
179 if ((ret & 0x8000) == 0)
189 int mv88e6xxx_phy_read(struct dsa_switch *ds, int addr, int regnum)
192 return mv88e6xxx_reg_read(ds, addr, regnum);
196 int mv88e6xxx_phy_write(struct dsa_switch *ds, int addr, int regnum, u16 val)
199 return mv88e6xxx_reg_write(ds, addr, regnum, val);
203 #ifdef CONFIG_NET_DSA_MV88E6XXX_NEED_PPU
204 static int mv88e6xxx_ppu_disable(struct dsa_switch *ds)
207 unsigned long timeout;
209 ret = REG_READ(REG_GLOBAL, 0x04);
210 REG_WRITE(REG_GLOBAL, 0x04, ret & ~0x4000);
212 timeout = jiffies + 1 * HZ;
213 while (time_before(jiffies, timeout)) {
214 ret = REG_READ(REG_GLOBAL, 0x00);
215 usleep_range(1000, 2000);
216 if ((ret & 0xc000) != 0xc000)
223 static int mv88e6xxx_ppu_enable(struct dsa_switch *ds)
226 unsigned long timeout;
228 ret = REG_READ(REG_GLOBAL, 0x04);
229 REG_WRITE(REG_GLOBAL, 0x04, ret | 0x4000);
231 timeout = jiffies + 1 * HZ;
232 while (time_before(jiffies, timeout)) {
233 ret = REG_READ(REG_GLOBAL, 0x00);
234 usleep_range(1000, 2000);
235 if ((ret & 0xc000) == 0xc000)
242 static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly)
244 struct mv88e6xxx_priv_state *ps;
246 ps = container_of(ugly, struct mv88e6xxx_priv_state, ppu_work);
247 if (mutex_trylock(&ps->ppu_mutex)) {
248 struct dsa_switch *ds = ((struct dsa_switch *)ps) - 1;
250 if (mv88e6xxx_ppu_enable(ds) == 0)
251 ps->ppu_disabled = 0;
252 mutex_unlock(&ps->ppu_mutex);
256 static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps)
258 struct mv88e6xxx_priv_state *ps = (void *)_ps;
260 schedule_work(&ps->ppu_work);
263 static int mv88e6xxx_ppu_access_get(struct dsa_switch *ds)
265 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
268 mutex_lock(&ps->ppu_mutex);
270 /* If the PHY polling unit is enabled, disable it so that
271 * we can access the PHY registers. If it was already
272 * disabled, cancel the timer that is going to re-enable
275 if (!ps->ppu_disabled) {
276 ret = mv88e6xxx_ppu_disable(ds);
278 mutex_unlock(&ps->ppu_mutex);
281 ps->ppu_disabled = 1;
283 del_timer(&ps->ppu_timer);
290 static void mv88e6xxx_ppu_access_put(struct dsa_switch *ds)
292 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
294 /* Schedule a timer to re-enable the PHY polling unit. */
295 mod_timer(&ps->ppu_timer, jiffies + msecs_to_jiffies(10));
296 mutex_unlock(&ps->ppu_mutex);
299 void mv88e6xxx_ppu_state_init(struct dsa_switch *ds)
301 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
303 mutex_init(&ps->ppu_mutex);
304 INIT_WORK(&ps->ppu_work, mv88e6xxx_ppu_reenable_work);
305 init_timer(&ps->ppu_timer);
306 ps->ppu_timer.data = (unsigned long)ps;
307 ps->ppu_timer.function = mv88e6xxx_ppu_reenable_timer;
310 int mv88e6xxx_phy_read_ppu(struct dsa_switch *ds, int addr, int regnum)
314 ret = mv88e6xxx_ppu_access_get(ds);
316 ret = mv88e6xxx_reg_read(ds, addr, regnum);
317 mv88e6xxx_ppu_access_put(ds);
323 int mv88e6xxx_phy_write_ppu(struct dsa_switch *ds, int addr,
328 ret = mv88e6xxx_ppu_access_get(ds);
330 ret = mv88e6xxx_reg_write(ds, addr, regnum, val);
331 mv88e6xxx_ppu_access_put(ds);
338 void mv88e6xxx_poll_link(struct dsa_switch *ds)
342 for (i = 0; i < DSA_MAX_PORTS; i++) {
343 struct net_device *dev;
344 int uninitialized_var(port_status);
355 if (dev->flags & IFF_UP) {
356 port_status = mv88e6xxx_reg_read(ds, REG_PORT(i), 0x00);
360 link = !!(port_status & 0x0800);
364 if (netif_carrier_ok(dev)) {
365 netdev_info(dev, "link down\n");
366 netif_carrier_off(dev);
371 switch (port_status & 0x0300) {
385 duplex = (port_status & 0x0400) ? 1 : 0;
386 fc = (port_status & 0x8000) ? 1 : 0;
388 if (!netif_carrier_ok(dev)) {
390 "link up, %d Mb/s, %s duplex, flow control %sabled\n",
392 duplex ? "full" : "half",
394 netif_carrier_on(dev);
399 static int mv88e6xxx_stats_wait(struct dsa_switch *ds)
404 for (i = 0; i < 10; i++) {
405 ret = REG_READ(REG_GLOBAL, 0x1d);
406 if ((ret & 0x8000) == 0)
413 static int mv88e6xxx_stats_snapshot(struct dsa_switch *ds, int port)
417 /* Snapshot the hardware statistics counters for this port. */
418 REG_WRITE(REG_GLOBAL, 0x1d, 0xdc00 | port);
420 /* Wait for the snapshotting to complete. */
421 ret = mv88e6xxx_stats_wait(ds);
428 static void mv88e6xxx_stats_read(struct dsa_switch *ds, int stat, u32 *val)
435 ret = mv88e6xxx_reg_write(ds, REG_GLOBAL, 0x1d, 0xcc00 | stat);
439 ret = mv88e6xxx_stats_wait(ds);
443 ret = mv88e6xxx_reg_read(ds, REG_GLOBAL, 0x1e);
449 ret = mv88e6xxx_reg_read(ds, REG_GLOBAL, 0x1f);
456 void mv88e6xxx_get_strings(struct dsa_switch *ds,
457 int nr_stats, struct mv88e6xxx_hw_stat *stats,
458 int port, uint8_t *data)
462 for (i = 0; i < nr_stats; i++) {
463 memcpy(data + i * ETH_GSTRING_LEN,
464 stats[i].string, ETH_GSTRING_LEN);
468 void mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds,
469 int nr_stats, struct mv88e6xxx_hw_stat *stats,
470 int port, uint64_t *data)
472 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
476 mutex_lock(&ps->stats_mutex);
478 ret = mv88e6xxx_stats_snapshot(ds, port);
480 mutex_unlock(&ps->stats_mutex);
484 /* Read each of the counters. */
485 for (i = 0; i < nr_stats; i++) {
486 struct mv88e6xxx_hw_stat *s = stats + i;
490 if (s->reg >= 0x100) {
493 ret = mv88e6xxx_reg_read(ds, REG_PORT(port),
498 if (s->sizeof_stat == 4) {
499 ret = mv88e6xxx_reg_read(ds, REG_PORT(port),
505 data[i] = (((u64)high) << 16) | low;
508 mv88e6xxx_stats_read(ds, s->reg, &low);
509 if (s->sizeof_stat == 8)
510 mv88e6xxx_stats_read(ds, s->reg + 1, &high);
512 data[i] = (((u64)high) << 32) | low;
515 mutex_unlock(&ps->stats_mutex);
518 int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
520 return 32 * sizeof(u16);
523 void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
524 struct ethtool_regs *regs, void *_p)
531 memset(p, 0xff, 32 * sizeof(u16));
533 for (i = 0; i < 32; i++) {
536 ret = mv88e6xxx_reg_read(ds, REG_PORT(port), i);
542 #ifdef CONFIG_NET_DSA_HWMON
544 int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
546 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
552 mutex_lock(&ps->phy_mutex);
554 ret = mv88e6xxx_phy_write(ds, 0x0, 0x16, 0x6);
558 /* Enable temperature sensor */
559 ret = mv88e6xxx_phy_read(ds, 0x0, 0x1a);
563 ret = mv88e6xxx_phy_write(ds, 0x0, 0x1a, ret | (1 << 5));
567 /* Wait for temperature to stabilize */
568 usleep_range(10000, 12000);
570 val = mv88e6xxx_phy_read(ds, 0x0, 0x1a);
576 /* Disable temperature sensor */
577 ret = mv88e6xxx_phy_write(ds, 0x0, 0x1a, ret & ~(1 << 5));
581 *temp = ((val & 0x1f) - 5) * 5;
584 mv88e6xxx_phy_write(ds, 0x0, 0x16, 0x0);
585 mutex_unlock(&ps->phy_mutex);
588 #endif /* CONFIG_NET_DSA_HWMON */
590 static int __init mv88e6xxx_init(void)
592 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6131)
593 register_switch_driver(&mv88e6131_switch_driver);
595 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6123_61_65)
596 register_switch_driver(&mv88e6123_61_65_switch_driver);
598 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
599 register_switch_driver(&mv88e6352_switch_driver);
601 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6171)
602 register_switch_driver(&mv88e6171_switch_driver);
606 module_init(mv88e6xxx_init);
608 static void __exit mv88e6xxx_cleanup(void)
610 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6171)
611 unregister_switch_driver(&mv88e6171_switch_driver);
613 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6123_61_65)
614 unregister_switch_driver(&mv88e6123_61_65_switch_driver);
616 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6131)
617 unregister_switch_driver(&mv88e6131_switch_driver);
620 module_exit(mv88e6xxx_cleanup);
623 MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
624 MODULE_LICENSE("GPL");