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
= (void *)(ds
+ 1);
80 mutex_lock(&ps
->smi_mutex
);
81 ret
= __mv88e6xxx_reg_read(ds
->master_mii_bus
,
82 ds
->pd
->sw_addr
, addr
, reg
);
83 mutex_unlock(&ps
->smi_mutex
);
88 int __mv88e6xxx_reg_write(struct mii_bus
*bus
, int sw_addr
, int addr
,
94 return mdiobus_write(bus
, addr
, reg
, val
);
96 /* Wait for the bus to become free. */
97 ret
= mv88e6xxx_reg_wait_ready(bus
, sw_addr
);
101 /* Transmit the data to write. */
102 ret
= mdiobus_write(bus
, sw_addr
, 1, val
);
106 /* Transmit the write command. */
107 ret
= mdiobus_write(bus
, sw_addr
, 0, 0x9400 | (addr
<< 5) | reg
);
111 /* Wait for the write command to complete. */
112 ret
= mv88e6xxx_reg_wait_ready(bus
, sw_addr
);
119 int mv88e6xxx_reg_write(struct dsa_switch
*ds
, int addr
, int reg
, u16 val
)
121 struct mv88e6xxx_priv_state
*ps
= (void *)(ds
+ 1);
124 mutex_lock(&ps
->smi_mutex
);
125 ret
= __mv88e6xxx_reg_write(ds
->master_mii_bus
,
126 ds
->pd
->sw_addr
, addr
, reg
, val
);
127 mutex_unlock(&ps
->smi_mutex
);
132 int mv88e6xxx_config_prio(struct dsa_switch
*ds
)
134 /* Configure the IP ToS mapping registers. */
135 REG_WRITE(REG_GLOBAL
, 0x10, 0x0000);
136 REG_WRITE(REG_GLOBAL
, 0x11, 0x0000);
137 REG_WRITE(REG_GLOBAL
, 0x12, 0x5555);
138 REG_WRITE(REG_GLOBAL
, 0x13, 0x5555);
139 REG_WRITE(REG_GLOBAL
, 0x14, 0xaaaa);
140 REG_WRITE(REG_GLOBAL
, 0x15, 0xaaaa);
141 REG_WRITE(REG_GLOBAL
, 0x16, 0xffff);
142 REG_WRITE(REG_GLOBAL
, 0x17, 0xffff);
144 /* Configure the IEEE 802.1p priority mapping register. */
145 REG_WRITE(REG_GLOBAL
, 0x18, 0xfa41);
150 int mv88e6xxx_set_addr_direct(struct dsa_switch
*ds
, u8
*addr
)
152 REG_WRITE(REG_GLOBAL
, 0x01, (addr
[0] << 8) | addr
[1]);
153 REG_WRITE(REG_GLOBAL
, 0x02, (addr
[2] << 8) | addr
[3]);
154 REG_WRITE(REG_GLOBAL
, 0x03, (addr
[4] << 8) | addr
[5]);
159 int mv88e6xxx_set_addr_indirect(struct dsa_switch
*ds
, u8
*addr
)
164 for (i
= 0; i
< 6; i
++) {
167 /* Write the MAC address byte. */
168 REG_WRITE(REG_GLOBAL2
, 0x0d, 0x8000 | (i
<< 8) | addr
[i
]);
170 /* Wait for the write to complete. */
171 for (j
= 0; j
< 16; j
++) {
172 ret
= REG_READ(REG_GLOBAL2
, 0x0d);
173 if ((ret
& 0x8000) == 0)
183 int mv88e6xxx_phy_read(struct dsa_switch
*ds
, int addr
, int regnum
)
186 return mv88e6xxx_reg_read(ds
, addr
, regnum
);
190 int mv88e6xxx_phy_write(struct dsa_switch
*ds
, int addr
, int regnum
, u16 val
)
193 return mv88e6xxx_reg_write(ds
, addr
, regnum
, val
);
197 #ifdef CONFIG_NET_DSA_MV88E6XXX_NEED_PPU
198 static int mv88e6xxx_ppu_disable(struct dsa_switch
*ds
)
201 unsigned long timeout
;
203 ret
= REG_READ(REG_GLOBAL
, 0x04);
204 REG_WRITE(REG_GLOBAL
, 0x04, ret
& ~0x4000);
206 timeout
= jiffies
+ 1 * HZ
;
207 while (time_before(jiffies
, timeout
)) {
208 ret
= REG_READ(REG_GLOBAL
, 0x00);
209 usleep_range(1000, 2000);
210 if ((ret
& 0xc000) != 0xc000)
217 static int mv88e6xxx_ppu_enable(struct dsa_switch
*ds
)
220 unsigned long timeout
;
222 ret
= REG_READ(REG_GLOBAL
, 0x04);
223 REG_WRITE(REG_GLOBAL
, 0x04, ret
| 0x4000);
225 timeout
= jiffies
+ 1 * HZ
;
226 while (time_before(jiffies
, timeout
)) {
227 ret
= REG_READ(REG_GLOBAL
, 0x00);
228 usleep_range(1000, 2000);
229 if ((ret
& 0xc000) == 0xc000)
236 static void mv88e6xxx_ppu_reenable_work(struct work_struct
*ugly
)
238 struct mv88e6xxx_priv_state
*ps
;
240 ps
= container_of(ugly
, struct mv88e6xxx_priv_state
, ppu_work
);
241 if (mutex_trylock(&ps
->ppu_mutex
)) {
242 struct dsa_switch
*ds
= ((struct dsa_switch
*)ps
) - 1;
244 if (mv88e6xxx_ppu_enable(ds
) == 0)
245 ps
->ppu_disabled
= 0;
246 mutex_unlock(&ps
->ppu_mutex
);
250 static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps
)
252 struct mv88e6xxx_priv_state
*ps
= (void *)_ps
;
254 schedule_work(&ps
->ppu_work
);
257 static int mv88e6xxx_ppu_access_get(struct dsa_switch
*ds
)
259 struct mv88e6xxx_priv_state
*ps
= (void *)(ds
+ 1);
262 mutex_lock(&ps
->ppu_mutex
);
264 /* If the PHY polling unit is enabled, disable it so that
265 * we can access the PHY registers. If it was already
266 * disabled, cancel the timer that is going to re-enable
269 if (!ps
->ppu_disabled
) {
270 ret
= mv88e6xxx_ppu_disable(ds
);
272 mutex_unlock(&ps
->ppu_mutex
);
275 ps
->ppu_disabled
= 1;
277 del_timer(&ps
->ppu_timer
);
284 static void mv88e6xxx_ppu_access_put(struct dsa_switch
*ds
)
286 struct mv88e6xxx_priv_state
*ps
= (void *)(ds
+ 1);
288 /* Schedule a timer to re-enable the PHY polling unit. */
289 mod_timer(&ps
->ppu_timer
, jiffies
+ msecs_to_jiffies(10));
290 mutex_unlock(&ps
->ppu_mutex
);
293 void mv88e6xxx_ppu_state_init(struct dsa_switch
*ds
)
295 struct mv88e6xxx_priv_state
*ps
= (void *)(ds
+ 1);
297 mutex_init(&ps
->ppu_mutex
);
298 INIT_WORK(&ps
->ppu_work
, mv88e6xxx_ppu_reenable_work
);
299 init_timer(&ps
->ppu_timer
);
300 ps
->ppu_timer
.data
= (unsigned long)ps
;
301 ps
->ppu_timer
.function
= mv88e6xxx_ppu_reenable_timer
;
304 int mv88e6xxx_phy_read_ppu(struct dsa_switch
*ds
, int addr
, int regnum
)
308 ret
= mv88e6xxx_ppu_access_get(ds
);
310 ret
= mv88e6xxx_reg_read(ds
, addr
, regnum
);
311 mv88e6xxx_ppu_access_put(ds
);
317 int mv88e6xxx_phy_write_ppu(struct dsa_switch
*ds
, int addr
,
322 ret
= mv88e6xxx_ppu_access_get(ds
);
324 ret
= mv88e6xxx_reg_write(ds
, addr
, regnum
, val
);
325 mv88e6xxx_ppu_access_put(ds
);
332 void mv88e6xxx_poll_link(struct dsa_switch
*ds
)
336 for (i
= 0; i
< DSA_MAX_PORTS
; i
++) {
337 struct net_device
*dev
;
338 int uninitialized_var(port_status
);
349 if (dev
->flags
& IFF_UP
) {
350 port_status
= mv88e6xxx_reg_read(ds
, REG_PORT(i
), 0x00);
354 link
= !!(port_status
& 0x0800);
358 if (netif_carrier_ok(dev
)) {
359 netdev_info(dev
, "link down\n");
360 netif_carrier_off(dev
);
365 switch (port_status
& 0x0300) {
379 duplex
= (port_status
& 0x0400) ? 1 : 0;
380 fc
= (port_status
& 0x8000) ? 1 : 0;
382 if (!netif_carrier_ok(dev
)) {
384 "link up, %d Mb/s, %s duplex, flow control %sabled\n",
386 duplex
? "full" : "half",
388 netif_carrier_on(dev
);
393 static int mv88e6xxx_stats_wait(struct dsa_switch
*ds
)
398 for (i
= 0; i
< 10; i
++) {
399 ret
= REG_READ(REG_GLOBAL
, 0x1d);
400 if ((ret
& 0x8000) == 0)
407 static int mv88e6xxx_stats_snapshot(struct dsa_switch
*ds
, int port
)
411 /* Snapshot the hardware statistics counters for this port. */
412 REG_WRITE(REG_GLOBAL
, 0x1d, 0xdc00 | port
);
414 /* Wait for the snapshotting to complete. */
415 ret
= mv88e6xxx_stats_wait(ds
);
422 static void mv88e6xxx_stats_read(struct dsa_switch
*ds
, int stat
, u32
*val
)
429 ret
= mv88e6xxx_reg_write(ds
, REG_GLOBAL
, 0x1d, 0xcc00 | stat
);
433 ret
= mv88e6xxx_stats_wait(ds
);
437 ret
= mv88e6xxx_reg_read(ds
, REG_GLOBAL
, 0x1e);
443 ret
= mv88e6xxx_reg_read(ds
, REG_GLOBAL
, 0x1f);
450 void mv88e6xxx_get_strings(struct dsa_switch
*ds
,
451 int nr_stats
, struct mv88e6xxx_hw_stat
*stats
,
452 int port
, uint8_t *data
)
456 for (i
= 0; i
< nr_stats
; i
++) {
457 memcpy(data
+ i
* ETH_GSTRING_LEN
,
458 stats
[i
].string
, ETH_GSTRING_LEN
);
462 void mv88e6xxx_get_ethtool_stats(struct dsa_switch
*ds
,
463 int nr_stats
, struct mv88e6xxx_hw_stat
*stats
,
464 int port
, uint64_t *data
)
466 struct mv88e6xxx_priv_state
*ps
= (void *)(ds
+ 1);
470 mutex_lock(&ps
->stats_mutex
);
472 ret
= mv88e6xxx_stats_snapshot(ds
, port
);
474 mutex_unlock(&ps
->stats_mutex
);
478 /* Read each of the counters. */
479 for (i
= 0; i
< nr_stats
; i
++) {
480 struct mv88e6xxx_hw_stat
*s
= stats
+ i
;
484 mv88e6xxx_stats_read(ds
, s
->reg
, &low
);
485 if (s
->sizeof_stat
== 8)
486 mv88e6xxx_stats_read(ds
, s
->reg
+ 1, &high
);
490 data
[i
] = (((u64
)high
) << 32) | low
;
493 mutex_unlock(&ps
->stats_mutex
);
496 static int __init
mv88e6xxx_init(void)
498 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6131)
499 register_switch_driver(&mv88e6131_switch_driver
);
501 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6123_61_65)
502 register_switch_driver(&mv88e6123_61_65_switch_driver
);
506 module_init(mv88e6xxx_init
);
508 static void __exit
mv88e6xxx_cleanup(void)
510 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6123_61_65)
511 unregister_switch_driver(&mv88e6123_61_65_switch_driver
);
513 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6131)
514 unregister_switch_driver(&mv88e6131_switch_driver
);
517 module_exit(mv88e6xxx_cleanup
);
519 MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
520 MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
521 MODULE_LICENSE("GPL");