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/list.h>
12 #include <linux/netdevice.h>
13 #include <linux/phy.h>
15 #include "mv88e6xxx.h"
18 * If the switch's ADDR[4:0] strap pins are strapped to zero, it will
19 * use all 32 SMI bus addresses on its SMI bus, and all switch registers
20 * will be directly accessible on some {device address,register address}
21 * pair. If the ADDR[4:0] pins are not strapped to zero, the switch
22 * will only respond to SMI transactions to that specific address, and
23 * an indirect addressing mechanism needs to be used to access its
26 static int mv88e6xxx_reg_wait_ready(struct mii_bus
*bus
, int sw_addr
)
31 for (i
= 0; i
< 16; i
++) {
32 ret
= mdiobus_read(bus
, sw_addr
, 0);
36 if ((ret
& 0x8000) == 0)
43 int __mv88e6xxx_reg_read(struct mii_bus
*bus
, int sw_addr
, int addr
, int reg
)
48 return mdiobus_read(bus
, addr
, reg
);
51 * Wait for the bus to become free.
53 ret
= mv88e6xxx_reg_wait_ready(bus
, sw_addr
);
58 * Transmit the read command.
60 ret
= mdiobus_write(bus
, sw_addr
, 0, 0x9800 | (addr
<< 5) | reg
);
65 * Wait for the read command to complete.
67 ret
= mv88e6xxx_reg_wait_ready(bus
, sw_addr
);
74 ret
= mdiobus_read(bus
, sw_addr
, 1);
81 int mv88e6xxx_reg_read(struct dsa_switch
*ds
, int addr
, int reg
)
83 struct mv88e6xxx_priv_state
*ps
= (void *)(ds
+ 1);
86 mutex_lock(&ps
->smi_mutex
);
87 ret
= __mv88e6xxx_reg_read(ds
->master_mii_bus
,
88 ds
->pd
->sw_addr
, addr
, reg
);
89 mutex_unlock(&ps
->smi_mutex
);
94 int __mv88e6xxx_reg_write(struct mii_bus
*bus
, int sw_addr
, int addr
,
100 return mdiobus_write(bus
, addr
, reg
, val
);
103 * Wait for the bus to become free.
105 ret
= mv88e6xxx_reg_wait_ready(bus
, sw_addr
);
110 * Transmit the data to write.
112 ret
= mdiobus_write(bus
, sw_addr
, 1, val
);
117 * Transmit the write command.
119 ret
= mdiobus_write(bus
, sw_addr
, 0, 0x9400 | (addr
<< 5) | reg
);
124 * Wait for the write command to complete.
126 ret
= mv88e6xxx_reg_wait_ready(bus
, sw_addr
);
133 int mv88e6xxx_reg_write(struct dsa_switch
*ds
, int addr
, int reg
, u16 val
)
135 struct mv88e6xxx_priv_state
*ps
= (void *)(ds
+ 1);
138 mutex_lock(&ps
->smi_mutex
);
139 ret
= __mv88e6xxx_reg_write(ds
->master_mii_bus
,
140 ds
->pd
->sw_addr
, addr
, reg
, val
);
141 mutex_unlock(&ps
->smi_mutex
);
146 int mv88e6xxx_config_prio(struct dsa_switch
*ds
)
149 * Configure the IP ToS mapping registers.
151 REG_WRITE(REG_GLOBAL
, 0x10, 0x0000);
152 REG_WRITE(REG_GLOBAL
, 0x11, 0x0000);
153 REG_WRITE(REG_GLOBAL
, 0x12, 0x5555);
154 REG_WRITE(REG_GLOBAL
, 0x13, 0x5555);
155 REG_WRITE(REG_GLOBAL
, 0x14, 0xaaaa);
156 REG_WRITE(REG_GLOBAL
, 0x15, 0xaaaa);
157 REG_WRITE(REG_GLOBAL
, 0x16, 0xffff);
158 REG_WRITE(REG_GLOBAL
, 0x17, 0xffff);
161 * Configure the IEEE 802.1p priority mapping register.
163 REG_WRITE(REG_GLOBAL
, 0x18, 0xfa41);
168 int mv88e6xxx_set_addr_direct(struct dsa_switch
*ds
, u8
*addr
)
170 REG_WRITE(REG_GLOBAL
, 0x01, (addr
[0] << 8) | addr
[1]);
171 REG_WRITE(REG_GLOBAL
, 0x02, (addr
[2] << 8) | addr
[3]);
172 REG_WRITE(REG_GLOBAL
, 0x03, (addr
[4] << 8) | addr
[5]);
177 int mv88e6xxx_set_addr_indirect(struct dsa_switch
*ds
, u8
*addr
)
182 for (i
= 0; i
< 6; i
++) {
186 * Write the MAC address byte.
188 REG_WRITE(REG_GLOBAL2
, 0x0d, 0x8000 | (i
<< 8) | addr
[i
]);
191 * Wait for the write to complete.
193 for (j
= 0; j
< 16; j
++) {
194 ret
= REG_READ(REG_GLOBAL2
, 0x0d);
195 if ((ret
& 0x8000) == 0)
205 int mv88e6xxx_phy_read(struct dsa_switch
*ds
, int addr
, int regnum
)
208 return mv88e6xxx_reg_read(ds
, addr
, regnum
);
212 int mv88e6xxx_phy_write(struct dsa_switch
*ds
, int addr
, int regnum
, u16 val
)
215 return mv88e6xxx_reg_write(ds
, addr
, regnum
, val
);
219 #ifdef CONFIG_NET_DSA_MV88E6XXX_NEED_PPU
220 static int mv88e6xxx_ppu_disable(struct dsa_switch
*ds
)
225 ret
= REG_READ(REG_GLOBAL
, 0x04);
226 REG_WRITE(REG_GLOBAL
, 0x04, ret
& ~0x4000);
228 for (i
= 0; i
< 1000; i
++) {
229 ret
= REG_READ(REG_GLOBAL
, 0x00);
231 if ((ret
& 0xc000) != 0xc000)
238 static int mv88e6xxx_ppu_enable(struct dsa_switch
*ds
)
243 ret
= REG_READ(REG_GLOBAL
, 0x04);
244 REG_WRITE(REG_GLOBAL
, 0x04, ret
| 0x4000);
246 for (i
= 0; i
< 1000; i
++) {
247 ret
= REG_READ(REG_GLOBAL
, 0x00);
249 if ((ret
& 0xc000) == 0xc000)
256 static void mv88e6xxx_ppu_reenable_work(struct work_struct
*ugly
)
258 struct mv88e6xxx_priv_state
*ps
;
260 ps
= container_of(ugly
, struct mv88e6xxx_priv_state
, ppu_work
);
261 if (mutex_trylock(&ps
->ppu_mutex
)) {
262 struct dsa_switch
*ds
= ((struct dsa_switch
*)ps
) - 1;
264 if (mv88e6xxx_ppu_enable(ds
) == 0)
265 ps
->ppu_disabled
= 0;
266 mutex_unlock(&ps
->ppu_mutex
);
270 static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps
)
272 struct mv88e6xxx_priv_state
*ps
= (void *)_ps
;
274 schedule_work(&ps
->ppu_work
);
277 static int mv88e6xxx_ppu_access_get(struct dsa_switch
*ds
)
279 struct mv88e6xxx_priv_state
*ps
= (void *)(ds
+ 1);
282 mutex_lock(&ps
->ppu_mutex
);
285 * If the PHY polling unit is enabled, disable it so that
286 * we can access the PHY registers. If it was already
287 * disabled, cancel the timer that is going to re-enable
290 if (!ps
->ppu_disabled
) {
291 ret
= mv88e6xxx_ppu_disable(ds
);
293 mutex_unlock(&ps
->ppu_mutex
);
296 ps
->ppu_disabled
= 1;
298 del_timer(&ps
->ppu_timer
);
305 static void mv88e6xxx_ppu_access_put(struct dsa_switch
*ds
)
307 struct mv88e6xxx_priv_state
*ps
= (void *)(ds
+ 1);
310 * Schedule a timer to re-enable the PHY polling unit.
312 mod_timer(&ps
->ppu_timer
, jiffies
+ msecs_to_jiffies(10));
313 mutex_unlock(&ps
->ppu_mutex
);
316 void mv88e6xxx_ppu_state_init(struct dsa_switch
*ds
)
318 struct mv88e6xxx_priv_state
*ps
= (void *)(ds
+ 1);
320 mutex_init(&ps
->ppu_mutex
);
321 INIT_WORK(&ps
->ppu_work
, mv88e6xxx_ppu_reenable_work
);
322 init_timer(&ps
->ppu_timer
);
323 ps
->ppu_timer
.data
= (unsigned long)ps
;
324 ps
->ppu_timer
.function
= mv88e6xxx_ppu_reenable_timer
;
327 int mv88e6xxx_phy_read_ppu(struct dsa_switch
*ds
, int addr
, int regnum
)
331 ret
= mv88e6xxx_ppu_access_get(ds
);
333 ret
= mv88e6xxx_reg_read(ds
, addr
, regnum
);
334 mv88e6xxx_ppu_access_put(ds
);
340 int mv88e6xxx_phy_write_ppu(struct dsa_switch
*ds
, int addr
,
345 ret
= mv88e6xxx_ppu_access_get(ds
);
347 ret
= mv88e6xxx_reg_write(ds
, addr
, regnum
, val
);
348 mv88e6xxx_ppu_access_put(ds
);
355 void mv88e6xxx_poll_link(struct dsa_switch
*ds
)
359 for (i
= 0; i
< DSA_MAX_PORTS
; i
++) {
360 struct net_device
*dev
;
361 int uninitialized_var(port_status
);
372 if (dev
->flags
& IFF_UP
) {
373 port_status
= mv88e6xxx_reg_read(ds
, REG_PORT(i
), 0x00);
377 link
= !!(port_status
& 0x0800);
381 if (netif_carrier_ok(dev
)) {
382 printk(KERN_INFO
"%s: link down\n", dev
->name
);
383 netif_carrier_off(dev
);
388 switch (port_status
& 0x0300) {
402 duplex
= (port_status
& 0x0400) ? 1 : 0;
403 fc
= (port_status
& 0x8000) ? 1 : 0;
405 if (!netif_carrier_ok(dev
)) {
406 printk(KERN_INFO
"%s: link up, %d Mb/s, %s duplex, "
407 "flow control %sabled\n", dev
->name
,
408 speed
, duplex
? "full" : "half",
410 netif_carrier_on(dev
);
415 static int mv88e6xxx_stats_wait(struct dsa_switch
*ds
)
420 for (i
= 0; i
< 10; i
++) {
421 ret
= REG_READ(REG_GLOBAL
, 0x1d);
422 if ((ret
& 0x8000) == 0)
429 static int mv88e6xxx_stats_snapshot(struct dsa_switch
*ds
, int port
)
434 * Snapshot the hardware statistics counters for this port.
436 REG_WRITE(REG_GLOBAL
, 0x1d, 0xdc00 | port
);
439 * Wait for the snapshotting to complete.
441 ret
= mv88e6xxx_stats_wait(ds
);
448 static void mv88e6xxx_stats_read(struct dsa_switch
*ds
, int stat
, u32
*val
)
455 ret
= mv88e6xxx_reg_write(ds
, REG_GLOBAL
, 0x1d, 0xcc00 | stat
);
459 ret
= mv88e6xxx_stats_wait(ds
);
463 ret
= mv88e6xxx_reg_read(ds
, REG_GLOBAL
, 0x1e);
469 ret
= mv88e6xxx_reg_read(ds
, REG_GLOBAL
, 0x1f);
476 void mv88e6xxx_get_strings(struct dsa_switch
*ds
,
477 int nr_stats
, struct mv88e6xxx_hw_stat
*stats
,
478 int port
, uint8_t *data
)
482 for (i
= 0; i
< nr_stats
; i
++) {
483 memcpy(data
+ i
* ETH_GSTRING_LEN
,
484 stats
[i
].string
, ETH_GSTRING_LEN
);
488 void mv88e6xxx_get_ethtool_stats(struct dsa_switch
*ds
,
489 int nr_stats
, struct mv88e6xxx_hw_stat
*stats
,
490 int port
, uint64_t *data
)
492 struct mv88e6xxx_priv_state
*ps
= (void *)(ds
+ 1);
496 mutex_lock(&ps
->stats_mutex
);
498 ret
= mv88e6xxx_stats_snapshot(ds
, port
);
500 mutex_unlock(&ps
->stats_mutex
);
505 * Read each of the counters.
507 for (i
= 0; i
< nr_stats
; i
++) {
508 struct mv88e6xxx_hw_stat
*s
= stats
+ i
;
512 mv88e6xxx_stats_read(ds
, s
->reg
, &low
);
513 if (s
->sizeof_stat
== 8)
514 mv88e6xxx_stats_read(ds
, s
->reg
+ 1, &high
);
518 data
[i
] = (((u64
)high
) << 32) | low
;
521 mutex_unlock(&ps
->stats_mutex
);