Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 1 | /* |
| 2 | * net/dsa/mv88e6xxx.c - Marvell 88e6xxx switch chip support |
| 3 | * Copyright (c) 2008 Marvell Semiconductor |
| 4 | * |
| 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. |
| 9 | */ |
| 10 | |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 11 | #include <linux/delay.h> |
| 12 | #include <linux/jiffies.h> |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 13 | #include <linux/list.h> |
Paul Gortmaker | 2bbba27 | 2012-01-24 10:41:40 +0000 | [diff] [blame] | 14 | #include <linux/module.h> |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 15 | #include <linux/netdevice.h> |
| 16 | #include <linux/phy.h> |
Ben Hutchings | c8f0b86 | 2011-11-27 17:06:08 +0000 | [diff] [blame] | 17 | #include <net/dsa.h> |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 18 | #include "mv88e6xxx.h" |
| 19 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 20 | /* If the switch's ADDR[4:0] strap pins are strapped to zero, it will |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 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 |
| 26 | * registers. |
| 27 | */ |
| 28 | static int mv88e6xxx_reg_wait_ready(struct mii_bus *bus, int sw_addr) |
| 29 | { |
| 30 | int ret; |
| 31 | int i; |
| 32 | |
| 33 | for (i = 0; i < 16; i++) { |
| 34 | ret = mdiobus_read(bus, sw_addr, 0); |
| 35 | if (ret < 0) |
| 36 | return ret; |
| 37 | |
| 38 | if ((ret & 0x8000) == 0) |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | return -ETIMEDOUT; |
| 43 | } |
| 44 | |
| 45 | int __mv88e6xxx_reg_read(struct mii_bus *bus, int sw_addr, int addr, int reg) |
| 46 | { |
| 47 | int ret; |
| 48 | |
| 49 | if (sw_addr == 0) |
| 50 | return mdiobus_read(bus, addr, reg); |
| 51 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 52 | /* Wait for the bus to become free. */ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 53 | ret = mv88e6xxx_reg_wait_ready(bus, sw_addr); |
| 54 | if (ret < 0) |
| 55 | return ret; |
| 56 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 57 | /* Transmit the read command. */ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 58 | ret = mdiobus_write(bus, sw_addr, 0, 0x9800 | (addr << 5) | reg); |
| 59 | if (ret < 0) |
| 60 | return ret; |
| 61 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 62 | /* Wait for the read command to complete. */ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 63 | ret = mv88e6xxx_reg_wait_ready(bus, sw_addr); |
| 64 | if (ret < 0) |
| 65 | return ret; |
| 66 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 67 | /* Read the data. */ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 68 | ret = mdiobus_read(bus, sw_addr, 1); |
| 69 | if (ret < 0) |
| 70 | return ret; |
| 71 | |
| 72 | return ret & 0xffff; |
| 73 | } |
| 74 | |
| 75 | int mv88e6xxx_reg_read(struct dsa_switch *ds, int addr, int reg) |
| 76 | { |
Florian Fainelli | a22adce | 2014-04-28 11:14:28 -0700 | [diff] [blame] | 77 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Guenter Roeck | b184e49 | 2014-10-17 12:30:58 -0700 | [diff] [blame] | 78 | struct mii_bus *bus = dsa_host_dev_to_mii_bus(ds->master_dev); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 79 | int ret; |
| 80 | |
Guenter Roeck | b184e49 | 2014-10-17 12:30:58 -0700 | [diff] [blame] | 81 | if (bus == NULL) |
| 82 | return -EINVAL; |
| 83 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 84 | mutex_lock(&ps->smi_mutex); |
Guenter Roeck | b184e49 | 2014-10-17 12:30:58 -0700 | [diff] [blame] | 85 | ret = __mv88e6xxx_reg_read(bus, ds->pd->sw_addr, addr, reg); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 86 | mutex_unlock(&ps->smi_mutex); |
| 87 | |
Vivien Didelot | bb92ea5 | 2015-01-23 16:10:36 -0500 | [diff] [blame] | 88 | if (ret < 0) |
| 89 | return ret; |
| 90 | |
| 91 | dev_dbg(ds->master_dev, "<- addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n", |
| 92 | addr, reg, ret); |
| 93 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 94 | return ret; |
| 95 | } |
| 96 | |
| 97 | int __mv88e6xxx_reg_write(struct mii_bus *bus, int sw_addr, int addr, |
| 98 | int reg, u16 val) |
| 99 | { |
| 100 | int ret; |
| 101 | |
| 102 | if (sw_addr == 0) |
| 103 | return mdiobus_write(bus, addr, reg, val); |
| 104 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 105 | /* Wait for the bus to become free. */ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 106 | ret = mv88e6xxx_reg_wait_ready(bus, sw_addr); |
| 107 | if (ret < 0) |
| 108 | return ret; |
| 109 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 110 | /* Transmit the data to write. */ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 111 | ret = mdiobus_write(bus, sw_addr, 1, val); |
| 112 | if (ret < 0) |
| 113 | return ret; |
| 114 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 115 | /* Transmit the write command. */ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 116 | ret = mdiobus_write(bus, sw_addr, 0, 0x9400 | (addr << 5) | reg); |
| 117 | if (ret < 0) |
| 118 | return ret; |
| 119 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 120 | /* Wait for the write command to complete. */ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 121 | ret = mv88e6xxx_reg_wait_ready(bus, sw_addr); |
| 122 | if (ret < 0) |
| 123 | return ret; |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | int mv88e6xxx_reg_write(struct dsa_switch *ds, int addr, int reg, u16 val) |
| 129 | { |
Florian Fainelli | a22adce | 2014-04-28 11:14:28 -0700 | [diff] [blame] | 130 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Guenter Roeck | b184e49 | 2014-10-17 12:30:58 -0700 | [diff] [blame] | 131 | struct mii_bus *bus = dsa_host_dev_to_mii_bus(ds->master_dev); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 132 | int ret; |
| 133 | |
Guenter Roeck | b184e49 | 2014-10-17 12:30:58 -0700 | [diff] [blame] | 134 | if (bus == NULL) |
| 135 | return -EINVAL; |
| 136 | |
Vivien Didelot | bb92ea5 | 2015-01-23 16:10:36 -0500 | [diff] [blame] | 137 | dev_dbg(ds->master_dev, "-> addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n", |
| 138 | addr, reg, val); |
| 139 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 140 | mutex_lock(&ps->smi_mutex); |
Guenter Roeck | b184e49 | 2014-10-17 12:30:58 -0700 | [diff] [blame] | 141 | ret = __mv88e6xxx_reg_write(bus, ds->pd->sw_addr, addr, reg, val); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 142 | mutex_unlock(&ps->smi_mutex); |
| 143 | |
| 144 | return ret; |
| 145 | } |
| 146 | |
| 147 | int mv88e6xxx_config_prio(struct dsa_switch *ds) |
| 148 | { |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 149 | /* Configure the IP ToS mapping registers. */ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 150 | REG_WRITE(REG_GLOBAL, 0x10, 0x0000); |
| 151 | REG_WRITE(REG_GLOBAL, 0x11, 0x0000); |
| 152 | REG_WRITE(REG_GLOBAL, 0x12, 0x5555); |
| 153 | REG_WRITE(REG_GLOBAL, 0x13, 0x5555); |
| 154 | REG_WRITE(REG_GLOBAL, 0x14, 0xaaaa); |
| 155 | REG_WRITE(REG_GLOBAL, 0x15, 0xaaaa); |
| 156 | REG_WRITE(REG_GLOBAL, 0x16, 0xffff); |
| 157 | REG_WRITE(REG_GLOBAL, 0x17, 0xffff); |
| 158 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 159 | /* Configure the IEEE 802.1p priority mapping register. */ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 160 | REG_WRITE(REG_GLOBAL, 0x18, 0xfa41); |
| 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 165 | int mv88e6xxx_set_addr_direct(struct dsa_switch *ds, u8 *addr) |
| 166 | { |
| 167 | REG_WRITE(REG_GLOBAL, 0x01, (addr[0] << 8) | addr[1]); |
| 168 | REG_WRITE(REG_GLOBAL, 0x02, (addr[2] << 8) | addr[3]); |
| 169 | REG_WRITE(REG_GLOBAL, 0x03, (addr[4] << 8) | addr[5]); |
| 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 174 | int mv88e6xxx_set_addr_indirect(struct dsa_switch *ds, u8 *addr) |
| 175 | { |
| 176 | int i; |
| 177 | int ret; |
| 178 | |
| 179 | for (i = 0; i < 6; i++) { |
| 180 | int j; |
| 181 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 182 | /* Write the MAC address byte. */ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 183 | REG_WRITE(REG_GLOBAL2, 0x0d, 0x8000 | (i << 8) | addr[i]); |
| 184 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 185 | /* Wait for the write to complete. */ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 186 | for (j = 0; j < 16; j++) { |
| 187 | ret = REG_READ(REG_GLOBAL2, 0x0d); |
| 188 | if ((ret & 0x8000) == 0) |
| 189 | break; |
| 190 | } |
| 191 | if (j == 16) |
| 192 | return -ETIMEDOUT; |
| 193 | } |
| 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | int mv88e6xxx_phy_read(struct dsa_switch *ds, int addr, int regnum) |
| 199 | { |
| 200 | if (addr >= 0) |
| 201 | return mv88e6xxx_reg_read(ds, addr, regnum); |
| 202 | return 0xffff; |
| 203 | } |
| 204 | |
| 205 | int mv88e6xxx_phy_write(struct dsa_switch *ds, int addr, int regnum, u16 val) |
| 206 | { |
| 207 | if (addr >= 0) |
| 208 | return mv88e6xxx_reg_write(ds, addr, regnum, val); |
| 209 | return 0; |
| 210 | } |
| 211 | |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 212 | #ifdef CONFIG_NET_DSA_MV88E6XXX_NEED_PPU |
| 213 | static int mv88e6xxx_ppu_disable(struct dsa_switch *ds) |
| 214 | { |
| 215 | int ret; |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 216 | unsigned long timeout; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 217 | |
| 218 | ret = REG_READ(REG_GLOBAL, 0x04); |
| 219 | REG_WRITE(REG_GLOBAL, 0x04, ret & ~0x4000); |
| 220 | |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 221 | timeout = jiffies + 1 * HZ; |
| 222 | while (time_before(jiffies, timeout)) { |
Barry Grussling | 8568658 | 2013-01-08 16:05:56 +0000 | [diff] [blame] | 223 | ret = REG_READ(REG_GLOBAL, 0x00); |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 224 | usleep_range(1000, 2000); |
Barry Grussling | 8568658 | 2013-01-08 16:05:56 +0000 | [diff] [blame] | 225 | if ((ret & 0xc000) != 0xc000) |
| 226 | return 0; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | return -ETIMEDOUT; |
| 230 | } |
| 231 | |
| 232 | static int mv88e6xxx_ppu_enable(struct dsa_switch *ds) |
| 233 | { |
| 234 | int ret; |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 235 | unsigned long timeout; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 236 | |
| 237 | ret = REG_READ(REG_GLOBAL, 0x04); |
| 238 | REG_WRITE(REG_GLOBAL, 0x04, ret | 0x4000); |
| 239 | |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 240 | timeout = jiffies + 1 * HZ; |
| 241 | while (time_before(jiffies, timeout)) { |
Barry Grussling | 8568658 | 2013-01-08 16:05:56 +0000 | [diff] [blame] | 242 | ret = REG_READ(REG_GLOBAL, 0x00); |
Barry Grussling | 19b2f97 | 2013-01-08 16:05:54 +0000 | [diff] [blame] | 243 | usleep_range(1000, 2000); |
Barry Grussling | 8568658 | 2013-01-08 16:05:56 +0000 | [diff] [blame] | 244 | if ((ret & 0xc000) == 0xc000) |
| 245 | return 0; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | return -ETIMEDOUT; |
| 249 | } |
| 250 | |
| 251 | static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly) |
| 252 | { |
| 253 | struct mv88e6xxx_priv_state *ps; |
| 254 | |
| 255 | ps = container_of(ugly, struct mv88e6xxx_priv_state, ppu_work); |
| 256 | if (mutex_trylock(&ps->ppu_mutex)) { |
Barry Grussling | 8568658 | 2013-01-08 16:05:56 +0000 | [diff] [blame] | 257 | struct dsa_switch *ds = ((struct dsa_switch *)ps) - 1; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 258 | |
Barry Grussling | 8568658 | 2013-01-08 16:05:56 +0000 | [diff] [blame] | 259 | if (mv88e6xxx_ppu_enable(ds) == 0) |
| 260 | ps->ppu_disabled = 0; |
| 261 | mutex_unlock(&ps->ppu_mutex); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | |
| 265 | static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps) |
| 266 | { |
| 267 | struct mv88e6xxx_priv_state *ps = (void *)_ps; |
| 268 | |
| 269 | schedule_work(&ps->ppu_work); |
| 270 | } |
| 271 | |
| 272 | static int mv88e6xxx_ppu_access_get(struct dsa_switch *ds) |
| 273 | { |
Florian Fainelli | a22adce | 2014-04-28 11:14:28 -0700 | [diff] [blame] | 274 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 275 | int ret; |
| 276 | |
| 277 | mutex_lock(&ps->ppu_mutex); |
| 278 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 279 | /* If the PHY polling unit is enabled, disable it so that |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 280 | * we can access the PHY registers. If it was already |
| 281 | * disabled, cancel the timer that is going to re-enable |
| 282 | * it. |
| 283 | */ |
| 284 | if (!ps->ppu_disabled) { |
Barry Grussling | 8568658 | 2013-01-08 16:05:56 +0000 | [diff] [blame] | 285 | ret = mv88e6xxx_ppu_disable(ds); |
| 286 | if (ret < 0) { |
| 287 | mutex_unlock(&ps->ppu_mutex); |
| 288 | return ret; |
| 289 | } |
| 290 | ps->ppu_disabled = 1; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 291 | } else { |
Barry Grussling | 8568658 | 2013-01-08 16:05:56 +0000 | [diff] [blame] | 292 | del_timer(&ps->ppu_timer); |
| 293 | ret = 0; |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | return ret; |
| 297 | } |
| 298 | |
| 299 | static void mv88e6xxx_ppu_access_put(struct dsa_switch *ds) |
| 300 | { |
Florian Fainelli | a22adce | 2014-04-28 11:14:28 -0700 | [diff] [blame] | 301 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 302 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 303 | /* Schedule a timer to re-enable the PHY polling unit. */ |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 304 | mod_timer(&ps->ppu_timer, jiffies + msecs_to_jiffies(10)); |
| 305 | mutex_unlock(&ps->ppu_mutex); |
| 306 | } |
| 307 | |
| 308 | void mv88e6xxx_ppu_state_init(struct dsa_switch *ds) |
| 309 | { |
Florian Fainelli | a22adce | 2014-04-28 11:14:28 -0700 | [diff] [blame] | 310 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 311 | |
| 312 | mutex_init(&ps->ppu_mutex); |
| 313 | INIT_WORK(&ps->ppu_work, mv88e6xxx_ppu_reenable_work); |
| 314 | init_timer(&ps->ppu_timer); |
| 315 | ps->ppu_timer.data = (unsigned long)ps; |
| 316 | ps->ppu_timer.function = mv88e6xxx_ppu_reenable_timer; |
| 317 | } |
| 318 | |
| 319 | int mv88e6xxx_phy_read_ppu(struct dsa_switch *ds, int addr, int regnum) |
| 320 | { |
| 321 | int ret; |
| 322 | |
| 323 | ret = mv88e6xxx_ppu_access_get(ds); |
| 324 | if (ret >= 0) { |
Barry Grussling | 8568658 | 2013-01-08 16:05:56 +0000 | [diff] [blame] | 325 | ret = mv88e6xxx_reg_read(ds, addr, regnum); |
| 326 | mv88e6xxx_ppu_access_put(ds); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | return ret; |
| 330 | } |
| 331 | |
| 332 | int mv88e6xxx_phy_write_ppu(struct dsa_switch *ds, int addr, |
| 333 | int regnum, u16 val) |
| 334 | { |
| 335 | int ret; |
| 336 | |
| 337 | ret = mv88e6xxx_ppu_access_get(ds); |
| 338 | if (ret >= 0) { |
Barry Grussling | 8568658 | 2013-01-08 16:05:56 +0000 | [diff] [blame] | 339 | ret = mv88e6xxx_reg_write(ds, addr, regnum, val); |
| 340 | mv88e6xxx_ppu_access_put(ds); |
Lennert Buytenhek | 2e5f032 | 2008-10-07 13:45:18 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | return ret; |
| 344 | } |
| 345 | #endif |
| 346 | |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 347 | void mv88e6xxx_poll_link(struct dsa_switch *ds) |
| 348 | { |
| 349 | int i; |
| 350 | |
| 351 | for (i = 0; i < DSA_MAX_PORTS; i++) { |
| 352 | struct net_device *dev; |
Ingo Molnar | 2a9e797 | 2008-11-25 16:50:49 -0800 | [diff] [blame] | 353 | int uninitialized_var(port_status); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 354 | int link; |
| 355 | int speed; |
| 356 | int duplex; |
| 357 | int fc; |
| 358 | |
| 359 | dev = ds->ports[i]; |
| 360 | if (dev == NULL) |
| 361 | continue; |
| 362 | |
| 363 | link = 0; |
| 364 | if (dev->flags & IFF_UP) { |
| 365 | port_status = mv88e6xxx_reg_read(ds, REG_PORT(i), 0x00); |
| 366 | if (port_status < 0) |
| 367 | continue; |
| 368 | |
| 369 | link = !!(port_status & 0x0800); |
| 370 | } |
| 371 | |
| 372 | if (!link) { |
| 373 | if (netif_carrier_ok(dev)) { |
Barry Grussling | ab381a9 | 2013-01-08 16:05:55 +0000 | [diff] [blame] | 374 | netdev_info(dev, "link down\n"); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 375 | netif_carrier_off(dev); |
| 376 | } |
| 377 | continue; |
| 378 | } |
| 379 | |
| 380 | switch (port_status & 0x0300) { |
| 381 | case 0x0000: |
| 382 | speed = 10; |
| 383 | break; |
| 384 | case 0x0100: |
| 385 | speed = 100; |
| 386 | break; |
| 387 | case 0x0200: |
| 388 | speed = 1000; |
| 389 | break; |
| 390 | default: |
| 391 | speed = -1; |
| 392 | break; |
| 393 | } |
| 394 | duplex = (port_status & 0x0400) ? 1 : 0; |
| 395 | fc = (port_status & 0x8000) ? 1 : 0; |
| 396 | |
| 397 | if (!netif_carrier_ok(dev)) { |
Barry Grussling | ab381a9 | 2013-01-08 16:05:55 +0000 | [diff] [blame] | 398 | netdev_info(dev, |
| 399 | "link up, %d Mb/s, %s duplex, flow control %sabled\n", |
| 400 | speed, |
| 401 | duplex ? "full" : "half", |
| 402 | fc ? "en" : "dis"); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 403 | netif_carrier_on(dev); |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | static int mv88e6xxx_stats_wait(struct dsa_switch *ds) |
| 409 | { |
| 410 | int ret; |
| 411 | int i; |
| 412 | |
| 413 | for (i = 0; i < 10; i++) { |
Stephane Contri | 1ded3f5 | 2009-07-02 23:26:48 +0000 | [diff] [blame] | 414 | ret = REG_READ(REG_GLOBAL, 0x1d); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 415 | if ((ret & 0x8000) == 0) |
| 416 | return 0; |
| 417 | } |
| 418 | |
| 419 | return -ETIMEDOUT; |
| 420 | } |
| 421 | |
| 422 | static int mv88e6xxx_stats_snapshot(struct dsa_switch *ds, int port) |
| 423 | { |
| 424 | int ret; |
| 425 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 426 | /* Snapshot the hardware statistics counters for this port. */ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 427 | REG_WRITE(REG_GLOBAL, 0x1d, 0xdc00 | port); |
| 428 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 429 | /* Wait for the snapshotting to complete. */ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 430 | ret = mv88e6xxx_stats_wait(ds); |
| 431 | if (ret < 0) |
| 432 | return ret; |
| 433 | |
| 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | static void mv88e6xxx_stats_read(struct dsa_switch *ds, int stat, u32 *val) |
| 438 | { |
| 439 | u32 _val; |
| 440 | int ret; |
| 441 | |
| 442 | *val = 0; |
| 443 | |
| 444 | ret = mv88e6xxx_reg_write(ds, REG_GLOBAL, 0x1d, 0xcc00 | stat); |
| 445 | if (ret < 0) |
| 446 | return; |
| 447 | |
| 448 | ret = mv88e6xxx_stats_wait(ds); |
| 449 | if (ret < 0) |
| 450 | return; |
| 451 | |
| 452 | ret = mv88e6xxx_reg_read(ds, REG_GLOBAL, 0x1e); |
| 453 | if (ret < 0) |
| 454 | return; |
| 455 | |
| 456 | _val = ret << 16; |
| 457 | |
| 458 | ret = mv88e6xxx_reg_read(ds, REG_GLOBAL, 0x1f); |
| 459 | if (ret < 0) |
| 460 | return; |
| 461 | |
| 462 | *val = _val | ret; |
| 463 | } |
| 464 | |
| 465 | void mv88e6xxx_get_strings(struct dsa_switch *ds, |
| 466 | int nr_stats, struct mv88e6xxx_hw_stat *stats, |
| 467 | int port, uint8_t *data) |
| 468 | { |
| 469 | int i; |
| 470 | |
| 471 | for (i = 0; i < nr_stats; i++) { |
| 472 | memcpy(data + i * ETH_GSTRING_LEN, |
| 473 | stats[i].string, ETH_GSTRING_LEN); |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | void mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds, |
| 478 | int nr_stats, struct mv88e6xxx_hw_stat *stats, |
| 479 | int port, uint64_t *data) |
| 480 | { |
Florian Fainelli | a22adce | 2014-04-28 11:14:28 -0700 | [diff] [blame] | 481 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 482 | int ret; |
| 483 | int i; |
| 484 | |
| 485 | mutex_lock(&ps->stats_mutex); |
| 486 | |
| 487 | ret = mv88e6xxx_stats_snapshot(ds, port); |
| 488 | if (ret < 0) { |
| 489 | mutex_unlock(&ps->stats_mutex); |
| 490 | return; |
| 491 | } |
| 492 | |
Barry Grussling | 3675c8d | 2013-01-08 16:05:53 +0000 | [diff] [blame] | 493 | /* Read each of the counters. */ |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 494 | for (i = 0; i < nr_stats; i++) { |
| 495 | struct mv88e6xxx_hw_stat *s = stats + i; |
| 496 | u32 low; |
Guenter Roeck | 17ee3e0 | 2014-10-29 10:45:07 -0700 | [diff] [blame] | 497 | u32 high = 0; |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 498 | |
Guenter Roeck | 17ee3e0 | 2014-10-29 10:45:07 -0700 | [diff] [blame] | 499 | if (s->reg >= 0x100) { |
| 500 | int ret; |
| 501 | |
| 502 | ret = mv88e6xxx_reg_read(ds, REG_PORT(port), |
| 503 | s->reg - 0x100); |
| 504 | if (ret < 0) |
| 505 | goto error; |
| 506 | low = ret; |
| 507 | if (s->sizeof_stat == 4) { |
| 508 | ret = mv88e6xxx_reg_read(ds, REG_PORT(port), |
| 509 | s->reg - 0x100 + 1); |
| 510 | if (ret < 0) |
| 511 | goto error; |
| 512 | high = ret; |
| 513 | } |
| 514 | data[i] = (((u64)high) << 16) | low; |
| 515 | continue; |
| 516 | } |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 517 | mv88e6xxx_stats_read(ds, s->reg, &low); |
| 518 | if (s->sizeof_stat == 8) |
| 519 | mv88e6xxx_stats_read(ds, s->reg + 1, &high); |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 520 | |
| 521 | data[i] = (((u64)high) << 32) | low; |
| 522 | } |
Guenter Roeck | 17ee3e0 | 2014-10-29 10:45:07 -0700 | [diff] [blame] | 523 | error: |
Lennert Buytenhek | 91da11f | 2008-10-07 13:44:02 +0000 | [diff] [blame] | 524 | mutex_unlock(&ps->stats_mutex); |
| 525 | } |
Ben Hutchings | 98e6730 | 2011-11-25 14:36:19 +0000 | [diff] [blame] | 526 | |
Guenter Roeck | a1ab91f | 2014-10-29 10:45:05 -0700 | [diff] [blame] | 527 | int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port) |
| 528 | { |
| 529 | return 32 * sizeof(u16); |
| 530 | } |
| 531 | |
| 532 | void mv88e6xxx_get_regs(struct dsa_switch *ds, int port, |
| 533 | struct ethtool_regs *regs, void *_p) |
| 534 | { |
| 535 | u16 *p = _p; |
| 536 | int i; |
| 537 | |
| 538 | regs->version = 0; |
| 539 | |
| 540 | memset(p, 0xff, 32 * sizeof(u16)); |
| 541 | |
| 542 | for (i = 0; i < 32; i++) { |
| 543 | int ret; |
| 544 | |
| 545 | ret = mv88e6xxx_reg_read(ds, REG_PORT(port), i); |
| 546 | if (ret >= 0) |
| 547 | p[i] = ret; |
| 548 | } |
| 549 | } |
| 550 | |
Andrew Lunn | eaa2376 | 2014-11-15 22:24:51 +0100 | [diff] [blame] | 551 | #ifdef CONFIG_NET_DSA_HWMON |
| 552 | |
| 553 | int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp) |
| 554 | { |
| 555 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 556 | int ret; |
| 557 | int val; |
| 558 | |
| 559 | *temp = 0; |
| 560 | |
| 561 | mutex_lock(&ps->phy_mutex); |
| 562 | |
| 563 | ret = mv88e6xxx_phy_write(ds, 0x0, 0x16, 0x6); |
| 564 | if (ret < 0) |
| 565 | goto error; |
| 566 | |
| 567 | /* Enable temperature sensor */ |
| 568 | ret = mv88e6xxx_phy_read(ds, 0x0, 0x1a); |
| 569 | if (ret < 0) |
| 570 | goto error; |
| 571 | |
| 572 | ret = mv88e6xxx_phy_write(ds, 0x0, 0x1a, ret | (1 << 5)); |
| 573 | if (ret < 0) |
| 574 | goto error; |
| 575 | |
| 576 | /* Wait for temperature to stabilize */ |
| 577 | usleep_range(10000, 12000); |
| 578 | |
| 579 | val = mv88e6xxx_phy_read(ds, 0x0, 0x1a); |
| 580 | if (val < 0) { |
| 581 | ret = val; |
| 582 | goto error; |
| 583 | } |
| 584 | |
| 585 | /* Disable temperature sensor */ |
| 586 | ret = mv88e6xxx_phy_write(ds, 0x0, 0x1a, ret & ~(1 << 5)); |
| 587 | if (ret < 0) |
| 588 | goto error; |
| 589 | |
| 590 | *temp = ((val & 0x1f) - 5) * 5; |
| 591 | |
| 592 | error: |
| 593 | mv88e6xxx_phy_write(ds, 0x0, 0x16, 0x0); |
| 594 | mutex_unlock(&ps->phy_mutex); |
| 595 | return ret; |
| 596 | } |
| 597 | #endif /* CONFIG_NET_DSA_HWMON */ |
| 598 | |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 599 | static int mv88e6xxx_wait(struct dsa_switch *ds, int reg, int offset, u16 mask) |
| 600 | { |
| 601 | unsigned long timeout = jiffies + HZ / 10; |
| 602 | |
| 603 | while (time_before(jiffies, timeout)) { |
| 604 | int ret; |
| 605 | |
| 606 | ret = REG_READ(reg, offset); |
| 607 | if (!(ret & mask)) |
| 608 | return 0; |
| 609 | |
| 610 | usleep_range(1000, 2000); |
| 611 | } |
| 612 | return -ETIMEDOUT; |
| 613 | } |
| 614 | |
| 615 | int mv88e6xxx_phy_wait(struct dsa_switch *ds) |
| 616 | { |
| 617 | return mv88e6xxx_wait(ds, REG_GLOBAL2, 0x18, 0x8000); |
| 618 | } |
| 619 | |
| 620 | int mv88e6xxx_eeprom_load_wait(struct dsa_switch *ds) |
| 621 | { |
| 622 | return mv88e6xxx_wait(ds, REG_GLOBAL2, 0x14, 0x0800); |
| 623 | } |
| 624 | |
| 625 | int mv88e6xxx_eeprom_busy_wait(struct dsa_switch *ds) |
| 626 | { |
| 627 | return mv88e6xxx_wait(ds, REG_GLOBAL2, 0x14, 0x8000); |
| 628 | } |
| 629 | |
| 630 | int mv88e6xxx_phy_read_indirect(struct dsa_switch *ds, int addr, int regnum) |
| 631 | { |
| 632 | int ret; |
| 633 | |
| 634 | REG_WRITE(REG_GLOBAL2, 0x18, 0x9800 | (addr << 5) | regnum); |
| 635 | |
| 636 | ret = mv88e6xxx_phy_wait(ds); |
| 637 | if (ret < 0) |
| 638 | return ret; |
| 639 | |
| 640 | return REG_READ(REG_GLOBAL2, 0x19); |
| 641 | } |
| 642 | |
| 643 | int mv88e6xxx_phy_write_indirect(struct dsa_switch *ds, int addr, int regnum, |
| 644 | u16 val) |
| 645 | { |
| 646 | REG_WRITE(REG_GLOBAL2, 0x19, val); |
| 647 | REG_WRITE(REG_GLOBAL2, 0x18, 0x9400 | (addr << 5) | regnum); |
| 648 | |
| 649 | return mv88e6xxx_phy_wait(ds); |
| 650 | } |
| 651 | |
Guenter Roeck | 11b3b45 | 2015-03-06 22:23:51 -0800 | [diff] [blame] | 652 | int mv88e6xxx_get_eee(struct dsa_switch *ds, int port, struct ethtool_eee *e) |
| 653 | { |
| 654 | int reg; |
| 655 | |
| 656 | reg = mv88e6xxx_phy_read_indirect(ds, port, 16); |
| 657 | if (reg < 0) |
| 658 | return -EOPNOTSUPP; |
| 659 | |
| 660 | e->eee_enabled = !!(reg & 0x0200); |
| 661 | e->tx_lpi_enabled = !!(reg & 0x0100); |
| 662 | |
| 663 | reg = REG_READ(REG_PORT(port), 0); |
| 664 | e->eee_active = !!(reg & 0x0040); |
| 665 | |
| 666 | return 0; |
| 667 | } |
| 668 | |
| 669 | static int mv88e6xxx_eee_enable_set(struct dsa_switch *ds, int port, |
| 670 | bool eee_enabled, bool tx_lpi_enabled) |
| 671 | { |
| 672 | int reg, nreg; |
| 673 | |
| 674 | reg = mv88e6xxx_phy_read_indirect(ds, port, 16); |
| 675 | if (reg < 0) |
| 676 | return reg; |
| 677 | |
| 678 | nreg = reg & ~0x0300; |
| 679 | if (eee_enabled) |
| 680 | nreg |= 0x0200; |
| 681 | if (tx_lpi_enabled) |
| 682 | nreg |= 0x0100; |
| 683 | |
| 684 | if (nreg != reg) |
| 685 | return mv88e6xxx_phy_write_indirect(ds, port, 16, nreg); |
| 686 | |
| 687 | return 0; |
| 688 | } |
| 689 | |
| 690 | int mv88e6xxx_set_eee(struct dsa_switch *ds, int port, |
| 691 | struct phy_device *phydev, struct ethtool_eee *e) |
| 692 | { |
| 693 | int ret; |
| 694 | |
| 695 | ret = mv88e6xxx_eee_enable_set(ds, port, e->eee_enabled, |
| 696 | e->tx_lpi_enabled); |
| 697 | if (ret) |
| 698 | return -EOPNOTSUPP; |
| 699 | |
| 700 | return 0; |
| 701 | } |
| 702 | |
Guenter Roeck | d827e88 | 2015-03-26 18:36:29 -0700 | [diff] [blame^] | 703 | int mv88e6xxx_setup_port_common(struct dsa_switch *ds, int port) |
| 704 | { |
| 705 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 706 | int ret, reg; |
| 707 | |
| 708 | mutex_lock(&ps->smi_mutex); |
| 709 | |
| 710 | /* Port Control 1: disable trunking. Also, if this is the |
| 711 | * CPU port, enable learn messages to be sent to this port. |
| 712 | */ |
| 713 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), 0x05, |
| 714 | dsa_is_cpu_port(ds, port) ? 0x8000 : 0x0000); |
| 715 | if (ret) |
| 716 | goto abort; |
| 717 | |
| 718 | /* Port based VLAN map: give each port its own address |
| 719 | * database, allow the CPU port to talk to each of the 'real' |
| 720 | * ports, and allow each of the 'real' ports to only talk to |
| 721 | * the upstream port. |
| 722 | */ |
| 723 | reg = (port & 0xf) << 12; |
| 724 | if (dsa_is_cpu_port(ds, port)) |
| 725 | reg |= ds->phys_port_mask; |
| 726 | else |
| 727 | reg |= 1 << dsa_upstream_port(ds); |
| 728 | |
| 729 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), 0x06, reg); |
| 730 | if (ret) |
| 731 | goto abort; |
| 732 | |
| 733 | /* Default VLAN ID and priority: don't set a default VLAN |
| 734 | * ID, and set the default packet priority to zero. |
| 735 | */ |
| 736 | ret = _mv88e6xxx_reg_write(ds, REG_PORT(port), 0x07, 0x0000); |
| 737 | abort: |
| 738 | mutex_unlock(&ps->smi_mutex); |
| 739 | return ret; |
| 740 | } |
| 741 | |
Guenter Roeck | acdaffc | 2015-03-26 18:36:28 -0700 | [diff] [blame] | 742 | int mv88e6xxx_setup_common(struct dsa_switch *ds) |
| 743 | { |
| 744 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 745 | |
| 746 | mutex_init(&ps->smi_mutex); |
| 747 | mutex_init(&ps->stats_mutex); |
| 748 | mutex_init(&ps->phy_mutex); |
| 749 | |
| 750 | return 0; |
| 751 | } |
| 752 | |
Ben Hutchings | 98e6730 | 2011-11-25 14:36:19 +0000 | [diff] [blame] | 753 | static int __init mv88e6xxx_init(void) |
| 754 | { |
| 755 | #if IS_ENABLED(CONFIG_NET_DSA_MV88E6131) |
| 756 | register_switch_driver(&mv88e6131_switch_driver); |
| 757 | #endif |
| 758 | #if IS_ENABLED(CONFIG_NET_DSA_MV88E6123_61_65) |
| 759 | register_switch_driver(&mv88e6123_61_65_switch_driver); |
| 760 | #endif |
Guenter Roeck | 3ad50cc | 2014-10-29 10:44:56 -0700 | [diff] [blame] | 761 | #if IS_ENABLED(CONFIG_NET_DSA_MV88E6352) |
| 762 | register_switch_driver(&mv88e6352_switch_driver); |
| 763 | #endif |
Andrew Lunn | 42f2725 | 2014-09-12 23:58:44 +0200 | [diff] [blame] | 764 | #if IS_ENABLED(CONFIG_NET_DSA_MV88E6171) |
| 765 | register_switch_driver(&mv88e6171_switch_driver); |
| 766 | #endif |
Ben Hutchings | 98e6730 | 2011-11-25 14:36:19 +0000 | [diff] [blame] | 767 | return 0; |
| 768 | } |
| 769 | module_init(mv88e6xxx_init); |
| 770 | |
| 771 | static void __exit mv88e6xxx_cleanup(void) |
| 772 | { |
Andrew Lunn | 42f2725 | 2014-09-12 23:58:44 +0200 | [diff] [blame] | 773 | #if IS_ENABLED(CONFIG_NET_DSA_MV88E6171) |
| 774 | unregister_switch_driver(&mv88e6171_switch_driver); |
| 775 | #endif |
Ben Hutchings | 98e6730 | 2011-11-25 14:36:19 +0000 | [diff] [blame] | 776 | #if IS_ENABLED(CONFIG_NET_DSA_MV88E6123_61_65) |
| 777 | unregister_switch_driver(&mv88e6123_61_65_switch_driver); |
| 778 | #endif |
| 779 | #if IS_ENABLED(CONFIG_NET_DSA_MV88E6131) |
| 780 | unregister_switch_driver(&mv88e6131_switch_driver); |
| 781 | #endif |
| 782 | } |
| 783 | module_exit(mv88e6xxx_cleanup); |
Ben Hutchings | 3d825ed | 2011-11-25 14:37:16 +0000 | [diff] [blame] | 784 | |
| 785 | MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>"); |
| 786 | MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips"); |
| 787 | MODULE_LICENSE("GPL"); |