Guenter Roeck | 3ad50cc | 2014-10-29 10:44:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * net/dsa/mv88e6352.c - Marvell 88e6352 switch chip support |
| 3 | * |
| 4 | * Copyright (c) 2014 Guenter Roeck |
| 5 | * |
| 6 | * Derived from mv88e6123_61_65.c |
| 7 | * Copyright (c) 2008-2009 Marvell Semiconductor |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/delay.h> |
| 16 | #include <linux/jiffies.h> |
| 17 | #include <linux/list.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/netdevice.h> |
| 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/phy.h> |
| 22 | #include <net/dsa.h> |
| 23 | #include "mv88e6xxx.h" |
| 24 | |
Guenter Roeck | 3ad50cc | 2014-10-29 10:44:56 -0700 | [diff] [blame] | 25 | static char *mv88e6352_probe(struct device *host_dev, int sw_addr) |
| 26 | { |
| 27 | struct mii_bus *bus = dsa_host_dev_to_mii_bus(host_dev); |
| 28 | int ret; |
| 29 | |
| 30 | if (bus == NULL) |
| 31 | return NULL; |
| 32 | |
| 33 | ret = __mv88e6xxx_reg_read(bus, sw_addr, REG_PORT(0), 0x03); |
| 34 | if (ret >= 0) { |
Guenter Roeck | 2716777 | 2014-10-29 10:44:57 -0700 | [diff] [blame] | 35 | if ((ret & 0xfff0) == 0x1760) |
| 36 | return "Marvell 88E6176"; |
Guenter Roeck | 3ad50cc | 2014-10-29 10:44:56 -0700 | [diff] [blame] | 37 | if (ret == 0x3521) |
| 38 | return "Marvell 88E6352 (A0)"; |
| 39 | if (ret == 0x3522) |
| 40 | return "Marvell 88E6352 (A1)"; |
| 41 | if ((ret & 0xfff0) == 0x3520) |
| 42 | return "Marvell 88E6352"; |
| 43 | } |
| 44 | |
| 45 | return NULL; |
| 46 | } |
| 47 | |
| 48 | static int mv88e6352_switch_reset(struct dsa_switch *ds) |
| 49 | { |
| 50 | unsigned long timeout; |
| 51 | int ret; |
| 52 | int i; |
| 53 | |
| 54 | /* Set all ports to the disabled state. */ |
| 55 | for (i = 0; i < 7; i++) { |
| 56 | ret = REG_READ(REG_PORT(i), 0x04); |
| 57 | REG_WRITE(REG_PORT(i), 0x04, ret & 0xfffc); |
| 58 | } |
| 59 | |
| 60 | /* Wait for transmit queues to drain. */ |
| 61 | usleep_range(2000, 4000); |
| 62 | |
| 63 | /* Reset the switch. Keep PPU active (bit 14, undocumented). |
| 64 | * The PPU needs to be active to support indirect phy register |
| 65 | * accesses through global registers 0x18 and 0x19. |
| 66 | */ |
| 67 | REG_WRITE(REG_GLOBAL, 0x04, 0xc000); |
| 68 | |
| 69 | /* Wait up to one second for reset to complete. */ |
| 70 | timeout = jiffies + 1 * HZ; |
| 71 | while (time_before(jiffies, timeout)) { |
| 72 | ret = REG_READ(REG_GLOBAL, 0x00); |
| 73 | if ((ret & 0x8800) == 0x8800) |
| 74 | break; |
| 75 | usleep_range(1000, 2000); |
| 76 | } |
| 77 | if (time_after(jiffies, timeout)) |
| 78 | return -ETIMEDOUT; |
| 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | static int mv88e6352_setup_global(struct dsa_switch *ds) |
| 84 | { |
| 85 | int ret; |
| 86 | int i; |
| 87 | |
| 88 | /* Discard packets with excessive collisions, |
| 89 | * mask all interrupt sources, enable PPU (bit 14, undocumented). |
| 90 | */ |
| 91 | REG_WRITE(REG_GLOBAL, 0x04, 0x6000); |
| 92 | |
| 93 | /* Set the default address aging time to 5 minutes, and |
| 94 | * enable address learn messages to be sent to all message |
| 95 | * ports. |
| 96 | */ |
| 97 | REG_WRITE(REG_GLOBAL, 0x0a, 0x0148); |
| 98 | |
| 99 | /* Configure the priority mapping registers. */ |
| 100 | ret = mv88e6xxx_config_prio(ds); |
| 101 | if (ret < 0) |
| 102 | return ret; |
| 103 | |
| 104 | /* Configure the upstream port, and configure the upstream |
| 105 | * port as the port to which ingress and egress monitor frames |
| 106 | * are to be sent. |
| 107 | */ |
| 108 | REG_WRITE(REG_GLOBAL, 0x1a, (dsa_upstream_port(ds) * 0x1110)); |
| 109 | |
| 110 | /* Disable remote management for now, and set the switch's |
| 111 | * DSA device number. |
| 112 | */ |
| 113 | REG_WRITE(REG_GLOBAL, 0x1c, ds->index & 0x1f); |
| 114 | |
| 115 | /* Send all frames with destination addresses matching |
| 116 | * 01:80:c2:00:00:2x to the CPU port. |
| 117 | */ |
| 118 | REG_WRITE(REG_GLOBAL2, 0x02, 0xffff); |
| 119 | |
| 120 | /* Send all frames with destination addresses matching |
| 121 | * 01:80:c2:00:00:0x to the CPU port. |
| 122 | */ |
| 123 | REG_WRITE(REG_GLOBAL2, 0x03, 0xffff); |
| 124 | |
| 125 | /* Disable the loopback filter, disable flow control |
| 126 | * messages, disable flood broadcast override, disable |
| 127 | * removing of provider tags, disable ATU age violation |
| 128 | * interrupts, disable tag flow control, force flow |
| 129 | * control priority to the highest, and send all special |
| 130 | * multicast frames to the CPU at the highest priority. |
| 131 | */ |
| 132 | REG_WRITE(REG_GLOBAL2, 0x05, 0x00ff); |
| 133 | |
| 134 | /* Program the DSA routing table. */ |
| 135 | for (i = 0; i < 32; i++) { |
| 136 | int nexthop = 0x1f; |
| 137 | |
| 138 | if (i != ds->index && i < ds->dst->pd->nr_chips) |
| 139 | nexthop = ds->pd->rtable[i] & 0x1f; |
| 140 | |
| 141 | REG_WRITE(REG_GLOBAL2, 0x06, 0x8000 | (i << 8) | nexthop); |
| 142 | } |
| 143 | |
| 144 | /* Clear all trunk masks. */ |
| 145 | for (i = 0; i < 8; i++) |
| 146 | REG_WRITE(REG_GLOBAL2, 0x07, 0x8000 | (i << 12) | 0x7f); |
| 147 | |
| 148 | /* Clear all trunk mappings. */ |
| 149 | for (i = 0; i < 16; i++) |
| 150 | REG_WRITE(REG_GLOBAL2, 0x08, 0x8000 | (i << 11)); |
| 151 | |
| 152 | /* Disable ingress rate limiting by resetting all ingress |
| 153 | * rate limit registers to their initial state. |
| 154 | */ |
| 155 | for (i = 0; i < 7; i++) |
| 156 | REG_WRITE(REG_GLOBAL2, 0x09, 0x9000 | (i << 8)); |
| 157 | |
| 158 | /* Initialise cross-chip port VLAN table to reset defaults. */ |
| 159 | REG_WRITE(REG_GLOBAL2, 0x0b, 0x9000); |
| 160 | |
| 161 | /* Clear the priority override table. */ |
| 162 | for (i = 0; i < 16; i++) |
| 163 | REG_WRITE(REG_GLOBAL2, 0x0f, 0x8000 | (i << 8)); |
| 164 | |
| 165 | /* @@@ initialise AVB (22/23) watchdog (27) sdet (29) registers */ |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | static int mv88e6352_setup_port(struct dsa_switch *ds, int p) |
| 171 | { |
| 172 | int addr = REG_PORT(p); |
| 173 | u16 val; |
| 174 | |
| 175 | /* MAC Forcing register: don't force link, speed, duplex |
| 176 | * or flow control state to any particular values on physical |
| 177 | * ports, but force the CPU port and all DSA ports to 1000 Mb/s |
| 178 | * full duplex. |
| 179 | */ |
| 180 | if (dsa_is_cpu_port(ds, p) || ds->dsa_port_mask & (1 << p)) |
| 181 | REG_WRITE(addr, 0x01, 0x003e); |
| 182 | else |
| 183 | REG_WRITE(addr, 0x01, 0x0003); |
| 184 | |
| 185 | /* Do not limit the period of time that this port can be |
| 186 | * paused for by the remote end or the period of time that |
| 187 | * this port can pause the remote end. |
| 188 | */ |
| 189 | REG_WRITE(addr, 0x02, 0x0000); |
| 190 | |
| 191 | /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock, |
| 192 | * disable Header mode, enable IGMP/MLD snooping, disable VLAN |
| 193 | * tunneling, determine priority by looking at 802.1p and IP |
| 194 | * priority fields (IP prio has precedence), and set STP state |
| 195 | * to Forwarding. |
| 196 | * |
| 197 | * If this is the CPU link, use DSA or EDSA tagging depending |
| 198 | * on which tagging mode was configured. |
| 199 | * |
| 200 | * If this is a link to another switch, use DSA tagging mode. |
| 201 | * |
| 202 | * If this is the upstream port for this switch, enable |
| 203 | * forwarding of unknown unicasts and multicasts. |
| 204 | */ |
| 205 | val = 0x0433; |
| 206 | if (dsa_is_cpu_port(ds, p)) { |
| 207 | if (ds->dst->tag_protocol == DSA_TAG_PROTO_EDSA) |
| 208 | val |= 0x3300; |
| 209 | else |
| 210 | val |= 0x0100; |
| 211 | } |
| 212 | if (ds->dsa_port_mask & (1 << p)) |
| 213 | val |= 0x0100; |
| 214 | if (p == dsa_upstream_port(ds)) |
| 215 | val |= 0x000c; |
| 216 | REG_WRITE(addr, 0x04, val); |
| 217 | |
Guenter Roeck | 3ad50cc | 2014-10-29 10:44:56 -0700 | [diff] [blame] | 218 | /* Port Control 2: don't force a good FCS, set the maximum |
| 219 | * frame size to 10240 bytes, don't let the switch add or |
| 220 | * strip 802.1q tags, don't discard tagged or untagged frames |
| 221 | * on this port, do a destination address lookup on all |
| 222 | * received packets as usual, disable ARP mirroring and don't |
| 223 | * send a copy of all transmitted/received frames on this port |
| 224 | * to the CPU. |
| 225 | */ |
| 226 | REG_WRITE(addr, 0x08, 0x2080); |
| 227 | |
| 228 | /* Egress rate control: disable egress rate control. */ |
| 229 | REG_WRITE(addr, 0x09, 0x0001); |
| 230 | |
| 231 | /* Egress rate control 2: disable egress rate control. */ |
| 232 | REG_WRITE(addr, 0x0a, 0x0000); |
| 233 | |
| 234 | /* Port Association Vector: when learning source addresses |
| 235 | * of packets, add the address to the address database using |
| 236 | * a port bitmap that has only the bit for this port set and |
| 237 | * the other bits clear. |
| 238 | */ |
| 239 | REG_WRITE(addr, 0x0b, 1 << p); |
| 240 | |
| 241 | /* Port ATU control: disable limiting the number of address |
| 242 | * database entries that this port is allowed to use. |
| 243 | */ |
| 244 | REG_WRITE(addr, 0x0c, 0x0000); |
| 245 | |
| 246 | /* Priority Override: disable DA, SA and VTU priority override. */ |
| 247 | REG_WRITE(addr, 0x0d, 0x0000); |
| 248 | |
| 249 | /* Port Ethertype: use the Ethertype DSA Ethertype value. */ |
| 250 | REG_WRITE(addr, 0x0f, ETH_P_EDSA); |
| 251 | |
| 252 | /* Tag Remap: use an identity 802.1p prio -> switch prio |
| 253 | * mapping. |
| 254 | */ |
| 255 | REG_WRITE(addr, 0x18, 0x3210); |
| 256 | |
| 257 | /* Tag Remap 2: use an identity 802.1p prio -> switch prio |
| 258 | * mapping. |
| 259 | */ |
| 260 | REG_WRITE(addr, 0x19, 0x7654); |
| 261 | |
Guenter Roeck | 2089052 | 2015-03-26 18:36:32 -0700 | [diff] [blame^] | 262 | return mv88e6xxx_setup_port_common(ds, p); |
Guenter Roeck | 3ad50cc | 2014-10-29 10:44:56 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Guenter Roeck | 276db3b | 2014-10-29 10:44:59 -0700 | [diff] [blame] | 265 | #ifdef CONFIG_NET_DSA_HWMON |
| 266 | |
| 267 | static int mv88e6352_phy_page_read(struct dsa_switch *ds, |
| 268 | int port, int page, int reg) |
| 269 | { |
| 270 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 271 | int ret; |
| 272 | |
| 273 | mutex_lock(&ps->phy_mutex); |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 274 | ret = mv88e6xxx_phy_write_indirect(ds, port, 0x16, page); |
Guenter Roeck | 276db3b | 2014-10-29 10:44:59 -0700 | [diff] [blame] | 275 | if (ret < 0) |
| 276 | goto error; |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 277 | ret = mv88e6xxx_phy_read_indirect(ds, port, reg); |
Guenter Roeck | 276db3b | 2014-10-29 10:44:59 -0700 | [diff] [blame] | 278 | error: |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 279 | mv88e6xxx_phy_write_indirect(ds, port, 0x16, 0x0); |
Guenter Roeck | 276db3b | 2014-10-29 10:44:59 -0700 | [diff] [blame] | 280 | mutex_unlock(&ps->phy_mutex); |
| 281 | return ret; |
| 282 | } |
| 283 | |
| 284 | static int mv88e6352_phy_page_write(struct dsa_switch *ds, |
| 285 | int port, int page, int reg, int val) |
| 286 | { |
| 287 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 288 | int ret; |
| 289 | |
| 290 | mutex_lock(&ps->phy_mutex); |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 291 | ret = mv88e6xxx_phy_write_indirect(ds, port, 0x16, page); |
Guenter Roeck | 276db3b | 2014-10-29 10:44:59 -0700 | [diff] [blame] | 292 | if (ret < 0) |
| 293 | goto error; |
| 294 | |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 295 | ret = mv88e6xxx_phy_write_indirect(ds, port, reg, val); |
Guenter Roeck | 276db3b | 2014-10-29 10:44:59 -0700 | [diff] [blame] | 296 | error: |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 297 | mv88e6xxx_phy_write_indirect(ds, port, 0x16, 0x0); |
Guenter Roeck | 276db3b | 2014-10-29 10:44:59 -0700 | [diff] [blame] | 298 | mutex_unlock(&ps->phy_mutex); |
| 299 | return ret; |
| 300 | } |
| 301 | |
| 302 | static int mv88e6352_get_temp(struct dsa_switch *ds, int *temp) |
| 303 | { |
| 304 | int ret; |
| 305 | |
| 306 | *temp = 0; |
| 307 | |
| 308 | ret = mv88e6352_phy_page_read(ds, 0, 6, 27); |
| 309 | if (ret < 0) |
| 310 | return ret; |
| 311 | |
| 312 | *temp = (ret & 0xff) - 25; |
| 313 | |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | static int mv88e6352_get_temp_limit(struct dsa_switch *ds, int *temp) |
| 318 | { |
| 319 | int ret; |
| 320 | |
| 321 | *temp = 0; |
| 322 | |
| 323 | ret = mv88e6352_phy_page_read(ds, 0, 6, 26); |
| 324 | if (ret < 0) |
| 325 | return ret; |
| 326 | |
| 327 | *temp = (((ret >> 8) & 0x1f) * 5) - 25; |
| 328 | |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | static int mv88e6352_set_temp_limit(struct dsa_switch *ds, int temp) |
| 333 | { |
| 334 | int ret; |
| 335 | |
| 336 | ret = mv88e6352_phy_page_read(ds, 0, 6, 26); |
| 337 | if (ret < 0) |
| 338 | return ret; |
| 339 | temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f); |
| 340 | return mv88e6352_phy_page_write(ds, 0, 6, 26, |
| 341 | (ret & 0xe0ff) | (temp << 8)); |
| 342 | } |
| 343 | |
| 344 | static int mv88e6352_get_temp_alarm(struct dsa_switch *ds, bool *alarm) |
| 345 | { |
| 346 | int ret; |
| 347 | |
| 348 | *alarm = false; |
| 349 | |
| 350 | ret = mv88e6352_phy_page_read(ds, 0, 6, 26); |
| 351 | if (ret < 0) |
| 352 | return ret; |
| 353 | |
| 354 | *alarm = !!(ret & 0x40); |
| 355 | |
| 356 | return 0; |
| 357 | } |
| 358 | #endif /* CONFIG_NET_DSA_HWMON */ |
| 359 | |
Guenter Roeck | 3ad50cc | 2014-10-29 10:44:56 -0700 | [diff] [blame] | 360 | static int mv88e6352_setup(struct dsa_switch *ds) |
| 361 | { |
| 362 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 363 | int ret; |
| 364 | int i; |
| 365 | |
Guenter Roeck | acdaffc | 2015-03-26 18:36:28 -0700 | [diff] [blame] | 366 | ret = mv88e6xxx_setup_common(ds); |
| 367 | if (ret < 0) |
| 368 | return ret; |
| 369 | |
Guenter Roeck | 33b43df | 2014-10-29 10:45:03 -0700 | [diff] [blame] | 370 | mutex_init(&ps->eeprom_mutex); |
Guenter Roeck | 3ad50cc | 2014-10-29 10:44:56 -0700 | [diff] [blame] | 371 | |
| 372 | ps->id = REG_READ(REG_PORT(0), 0x03) & 0xfff0; |
| 373 | |
| 374 | ret = mv88e6352_switch_reset(ds); |
| 375 | if (ret < 0) |
| 376 | return ret; |
| 377 | |
| 378 | /* @@@ initialise vtu and atu */ |
| 379 | |
| 380 | ret = mv88e6352_setup_global(ds); |
| 381 | if (ret < 0) |
| 382 | return ret; |
| 383 | |
| 384 | for (i = 0; i < 7; i++) { |
| 385 | ret = mv88e6352_setup_port(ds, i); |
| 386 | if (ret < 0) |
| 387 | return ret; |
| 388 | } |
| 389 | |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | static int mv88e6352_port_to_phy_addr(int port) |
| 394 | { |
| 395 | if (port >= 0 && port <= 4) |
| 396 | return port; |
| 397 | return -EINVAL; |
| 398 | } |
| 399 | |
| 400 | static int |
| 401 | mv88e6352_phy_read(struct dsa_switch *ds, int port, int regnum) |
| 402 | { |
| 403 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 404 | int addr = mv88e6352_port_to_phy_addr(port); |
| 405 | int ret; |
| 406 | |
| 407 | if (addr < 0) |
| 408 | return addr; |
| 409 | |
| 410 | mutex_lock(&ps->phy_mutex); |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 411 | ret = mv88e6xxx_phy_read_indirect(ds, addr, regnum); |
Guenter Roeck | 3ad50cc | 2014-10-29 10:44:56 -0700 | [diff] [blame] | 412 | mutex_unlock(&ps->phy_mutex); |
| 413 | |
| 414 | return ret; |
| 415 | } |
| 416 | |
| 417 | static int |
| 418 | mv88e6352_phy_write(struct dsa_switch *ds, int port, int regnum, u16 val) |
| 419 | { |
| 420 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 421 | int addr = mv88e6352_port_to_phy_addr(port); |
| 422 | int ret; |
| 423 | |
| 424 | if (addr < 0) |
| 425 | return addr; |
| 426 | |
| 427 | mutex_lock(&ps->phy_mutex); |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 428 | ret = mv88e6xxx_phy_write_indirect(ds, addr, regnum, val); |
Guenter Roeck | 3ad50cc | 2014-10-29 10:44:56 -0700 | [diff] [blame] | 429 | mutex_unlock(&ps->phy_mutex); |
| 430 | |
| 431 | return ret; |
| 432 | } |
| 433 | |
| 434 | static struct mv88e6xxx_hw_stat mv88e6352_hw_stats[] = { |
| 435 | { "in_good_octets", 8, 0x00, }, |
| 436 | { "in_bad_octets", 4, 0x02, }, |
| 437 | { "in_unicast", 4, 0x04, }, |
| 438 | { "in_broadcasts", 4, 0x06, }, |
| 439 | { "in_multicasts", 4, 0x07, }, |
| 440 | { "in_pause", 4, 0x16, }, |
| 441 | { "in_undersize", 4, 0x18, }, |
| 442 | { "in_fragments", 4, 0x19, }, |
| 443 | { "in_oversize", 4, 0x1a, }, |
| 444 | { "in_jabber", 4, 0x1b, }, |
| 445 | { "in_rx_error", 4, 0x1c, }, |
| 446 | { "in_fcs_error", 4, 0x1d, }, |
| 447 | { "out_octets", 8, 0x0e, }, |
| 448 | { "out_unicast", 4, 0x10, }, |
| 449 | { "out_broadcasts", 4, 0x13, }, |
| 450 | { "out_multicasts", 4, 0x12, }, |
| 451 | { "out_pause", 4, 0x15, }, |
| 452 | { "excessive", 4, 0x11, }, |
| 453 | { "collisions", 4, 0x1e, }, |
| 454 | { "deferred", 4, 0x05, }, |
| 455 | { "single", 4, 0x14, }, |
| 456 | { "multiple", 4, 0x17, }, |
| 457 | { "out_fcs_error", 4, 0x03, }, |
| 458 | { "late", 4, 0x1f, }, |
| 459 | { "hist_64bytes", 4, 0x08, }, |
| 460 | { "hist_65_127bytes", 4, 0x09, }, |
| 461 | { "hist_128_255bytes", 4, 0x0a, }, |
| 462 | { "hist_256_511bytes", 4, 0x0b, }, |
| 463 | { "hist_512_1023bytes", 4, 0x0c, }, |
| 464 | { "hist_1024_max_bytes", 4, 0x0d, }, |
Guenter Roeck | 17ee3e0 | 2014-10-29 10:45:07 -0700 | [diff] [blame] | 465 | { "sw_in_discards", 4, 0x110, }, |
| 466 | { "sw_in_filtered", 2, 0x112, }, |
| 467 | { "sw_out_filtered", 2, 0x113, }, |
Guenter Roeck | 3ad50cc | 2014-10-29 10:44:56 -0700 | [diff] [blame] | 468 | }; |
| 469 | |
Guenter Roeck | 33b43df | 2014-10-29 10:45:03 -0700 | [diff] [blame] | 470 | static int mv88e6352_read_eeprom_word(struct dsa_switch *ds, int addr) |
| 471 | { |
| 472 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 473 | int ret; |
| 474 | |
| 475 | mutex_lock(&ps->eeprom_mutex); |
| 476 | |
| 477 | ret = mv88e6xxx_reg_write(ds, REG_GLOBAL2, 0x14, |
| 478 | 0xc000 | (addr & 0xff)); |
| 479 | if (ret < 0) |
| 480 | goto error; |
| 481 | |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 482 | ret = mv88e6xxx_eeprom_busy_wait(ds); |
Guenter Roeck | 33b43df | 2014-10-29 10:45:03 -0700 | [diff] [blame] | 483 | if (ret < 0) |
| 484 | goto error; |
| 485 | |
| 486 | ret = mv88e6xxx_reg_read(ds, REG_GLOBAL2, 0x15); |
| 487 | error: |
| 488 | mutex_unlock(&ps->eeprom_mutex); |
| 489 | return ret; |
| 490 | } |
| 491 | |
| 492 | static int mv88e6352_get_eeprom(struct dsa_switch *ds, |
| 493 | struct ethtool_eeprom *eeprom, u8 *data) |
| 494 | { |
| 495 | int offset; |
| 496 | int len; |
| 497 | int ret; |
| 498 | |
| 499 | offset = eeprom->offset; |
| 500 | len = eeprom->len; |
| 501 | eeprom->len = 0; |
| 502 | |
| 503 | eeprom->magic = 0xc3ec4951; |
| 504 | |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 505 | ret = mv88e6xxx_eeprom_load_wait(ds); |
Guenter Roeck | 33b43df | 2014-10-29 10:45:03 -0700 | [diff] [blame] | 506 | if (ret < 0) |
| 507 | return ret; |
| 508 | |
| 509 | if (offset & 1) { |
| 510 | int word; |
| 511 | |
| 512 | word = mv88e6352_read_eeprom_word(ds, offset >> 1); |
| 513 | if (word < 0) |
| 514 | return word; |
| 515 | |
| 516 | *data++ = (word >> 8) & 0xff; |
| 517 | |
| 518 | offset++; |
| 519 | len--; |
| 520 | eeprom->len++; |
| 521 | } |
| 522 | |
| 523 | while (len >= 2) { |
| 524 | int word; |
| 525 | |
| 526 | word = mv88e6352_read_eeprom_word(ds, offset >> 1); |
| 527 | if (word < 0) |
| 528 | return word; |
| 529 | |
| 530 | *data++ = word & 0xff; |
| 531 | *data++ = (word >> 8) & 0xff; |
| 532 | |
| 533 | offset += 2; |
| 534 | len -= 2; |
| 535 | eeprom->len += 2; |
| 536 | } |
| 537 | |
| 538 | if (len) { |
| 539 | int word; |
| 540 | |
| 541 | word = mv88e6352_read_eeprom_word(ds, offset >> 1); |
| 542 | if (word < 0) |
| 543 | return word; |
| 544 | |
| 545 | *data++ = word & 0xff; |
| 546 | |
| 547 | offset++; |
| 548 | len--; |
| 549 | eeprom->len++; |
| 550 | } |
| 551 | |
| 552 | return 0; |
| 553 | } |
| 554 | |
| 555 | static int mv88e6352_eeprom_is_readonly(struct dsa_switch *ds) |
| 556 | { |
| 557 | int ret; |
| 558 | |
| 559 | ret = mv88e6xxx_reg_read(ds, REG_GLOBAL2, 0x14); |
| 560 | if (ret < 0) |
| 561 | return ret; |
| 562 | |
| 563 | if (!(ret & 0x0400)) |
| 564 | return -EROFS; |
| 565 | |
| 566 | return 0; |
| 567 | } |
| 568 | |
| 569 | static int mv88e6352_write_eeprom_word(struct dsa_switch *ds, int addr, |
| 570 | u16 data) |
| 571 | { |
| 572 | struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); |
| 573 | int ret; |
| 574 | |
| 575 | mutex_lock(&ps->eeprom_mutex); |
| 576 | |
| 577 | ret = mv88e6xxx_reg_write(ds, REG_GLOBAL2, 0x15, data); |
| 578 | if (ret < 0) |
| 579 | goto error; |
| 580 | |
| 581 | ret = mv88e6xxx_reg_write(ds, REG_GLOBAL2, 0x14, |
| 582 | 0xb000 | (addr & 0xff)); |
| 583 | if (ret < 0) |
| 584 | goto error; |
| 585 | |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 586 | ret = mv88e6xxx_eeprom_busy_wait(ds); |
Guenter Roeck | 33b43df | 2014-10-29 10:45:03 -0700 | [diff] [blame] | 587 | error: |
| 588 | mutex_unlock(&ps->eeprom_mutex); |
| 589 | return ret; |
| 590 | } |
| 591 | |
| 592 | static int mv88e6352_set_eeprom(struct dsa_switch *ds, |
| 593 | struct ethtool_eeprom *eeprom, u8 *data) |
| 594 | { |
| 595 | int offset; |
| 596 | int ret; |
| 597 | int len; |
| 598 | |
| 599 | if (eeprom->magic != 0xc3ec4951) |
| 600 | return -EINVAL; |
| 601 | |
| 602 | ret = mv88e6352_eeprom_is_readonly(ds); |
| 603 | if (ret) |
| 604 | return ret; |
| 605 | |
| 606 | offset = eeprom->offset; |
| 607 | len = eeprom->len; |
| 608 | eeprom->len = 0; |
| 609 | |
Andrew Lunn | f304468 | 2015-02-14 19:17:50 +0100 | [diff] [blame] | 610 | ret = mv88e6xxx_eeprom_load_wait(ds); |
Guenter Roeck | 33b43df | 2014-10-29 10:45:03 -0700 | [diff] [blame] | 611 | if (ret < 0) |
| 612 | return ret; |
| 613 | |
| 614 | if (offset & 1) { |
| 615 | int word; |
| 616 | |
| 617 | word = mv88e6352_read_eeprom_word(ds, offset >> 1); |
| 618 | if (word < 0) |
| 619 | return word; |
| 620 | |
| 621 | word = (*data++ << 8) | (word & 0xff); |
| 622 | |
| 623 | ret = mv88e6352_write_eeprom_word(ds, offset >> 1, word); |
| 624 | if (ret < 0) |
| 625 | return ret; |
| 626 | |
| 627 | offset++; |
| 628 | len--; |
| 629 | eeprom->len++; |
| 630 | } |
| 631 | |
| 632 | while (len >= 2) { |
| 633 | int word; |
| 634 | |
| 635 | word = *data++; |
| 636 | word |= *data++ << 8; |
| 637 | |
| 638 | ret = mv88e6352_write_eeprom_word(ds, offset >> 1, word); |
| 639 | if (ret < 0) |
| 640 | return ret; |
| 641 | |
| 642 | offset += 2; |
| 643 | len -= 2; |
| 644 | eeprom->len += 2; |
| 645 | } |
| 646 | |
| 647 | if (len) { |
| 648 | int word; |
| 649 | |
| 650 | word = mv88e6352_read_eeprom_word(ds, offset >> 1); |
| 651 | if (word < 0) |
| 652 | return word; |
| 653 | |
| 654 | word = (word & 0xff00) | *data++; |
| 655 | |
| 656 | ret = mv88e6352_write_eeprom_word(ds, offset >> 1, word); |
| 657 | if (ret < 0) |
| 658 | return ret; |
| 659 | |
| 660 | offset++; |
| 661 | len--; |
| 662 | eeprom->len++; |
| 663 | } |
| 664 | |
| 665 | return 0; |
| 666 | } |
| 667 | |
Guenter Roeck | 3ad50cc | 2014-10-29 10:44:56 -0700 | [diff] [blame] | 668 | static void |
| 669 | mv88e6352_get_strings(struct dsa_switch *ds, int port, uint8_t *data) |
| 670 | { |
| 671 | mv88e6xxx_get_strings(ds, ARRAY_SIZE(mv88e6352_hw_stats), |
| 672 | mv88e6352_hw_stats, port, data); |
| 673 | } |
| 674 | |
| 675 | static void |
| 676 | mv88e6352_get_ethtool_stats(struct dsa_switch *ds, int port, uint64_t *data) |
| 677 | { |
| 678 | mv88e6xxx_get_ethtool_stats(ds, ARRAY_SIZE(mv88e6352_hw_stats), |
| 679 | mv88e6352_hw_stats, port, data); |
| 680 | } |
| 681 | |
| 682 | static int mv88e6352_get_sset_count(struct dsa_switch *ds) |
| 683 | { |
| 684 | return ARRAY_SIZE(mv88e6352_hw_stats); |
| 685 | } |
| 686 | |
| 687 | struct dsa_switch_driver mv88e6352_switch_driver = { |
| 688 | .tag_protocol = DSA_TAG_PROTO_EDSA, |
| 689 | .priv_size = sizeof(struct mv88e6xxx_priv_state), |
| 690 | .probe = mv88e6352_probe, |
| 691 | .setup = mv88e6352_setup, |
| 692 | .set_addr = mv88e6xxx_set_addr_indirect, |
| 693 | .phy_read = mv88e6352_phy_read, |
| 694 | .phy_write = mv88e6352_phy_write, |
| 695 | .poll_link = mv88e6xxx_poll_link, |
| 696 | .get_strings = mv88e6352_get_strings, |
| 697 | .get_ethtool_stats = mv88e6352_get_ethtool_stats, |
| 698 | .get_sset_count = mv88e6352_get_sset_count, |
Guenter Roeck | 04b0a80 | 2015-03-06 22:23:52 -0800 | [diff] [blame] | 699 | .set_eee = mv88e6xxx_set_eee, |
| 700 | .get_eee = mv88e6xxx_get_eee, |
Guenter Roeck | 276db3b | 2014-10-29 10:44:59 -0700 | [diff] [blame] | 701 | #ifdef CONFIG_NET_DSA_HWMON |
| 702 | .get_temp = mv88e6352_get_temp, |
| 703 | .get_temp_limit = mv88e6352_get_temp_limit, |
| 704 | .set_temp_limit = mv88e6352_set_temp_limit, |
| 705 | .get_temp_alarm = mv88e6352_get_temp_alarm, |
| 706 | #endif |
Guenter Roeck | 33b43df | 2014-10-29 10:45:03 -0700 | [diff] [blame] | 707 | .get_eeprom = mv88e6352_get_eeprom, |
| 708 | .set_eeprom = mv88e6352_set_eeprom, |
Guenter Roeck | 95d08b5 | 2014-10-29 10:45:06 -0700 | [diff] [blame] | 709 | .get_regs_len = mv88e6xxx_get_regs_len, |
| 710 | .get_regs = mv88e6xxx_get_regs, |
Guenter Roeck | 3ad50cc | 2014-10-29 10:44:56 -0700 | [diff] [blame] | 711 | }; |
| 712 | |
| 713 | MODULE_ALIAS("platform:mv88e6352"); |