b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Realtek RTL8366 SMI interface driver |
| 3 | * |
| 4 | * Copyright (C) 2009-2010 Gabor Juhos <juhosg@openwrt.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License version 2 as published |
| 8 | * by the Free Software Foundation. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/device.h> |
| 14 | #include <linux/delay.h> |
| 15 | #include <linux/gpio.h> |
| 16 | #include <linux/spinlock.h> |
| 17 | #include <linux/skbuff.h> |
| 18 | #include <linux/of.h> |
| 19 | #include <linux/of_platform.h> |
| 20 | #include <linux/of_gpio.h> |
| 21 | #include <linux/rtl8366.h> |
| 22 | #include <linux/version.h> |
| 23 | #include <linux/of_mdio.h> |
| 24 | |
| 25 | #ifdef CONFIG_RTL8366_SMI_DEBUG_FS |
| 26 | #include <linux/debugfs.h> |
| 27 | #endif |
| 28 | |
| 29 | #include "rtl8366_smi.h" |
| 30 | |
| 31 | #define RTL8366_SMI_ACK_RETRY_COUNT 5 |
| 32 | |
| 33 | #define RTL8366_SMI_HW_STOP_DELAY 25 /* msecs */ |
| 34 | #define RTL8366_SMI_HW_START_DELAY 100 /* msecs */ |
| 35 | |
| 36 | static inline void rtl8366_smi_clk_delay(struct rtl8366_smi *smi) |
| 37 | { |
| 38 | ndelay(smi->clk_delay); |
| 39 | } |
| 40 | |
| 41 | static void rtl8366_smi_start(struct rtl8366_smi *smi) |
| 42 | { |
| 43 | unsigned int sda = smi->gpio_sda; |
| 44 | unsigned int sck = smi->gpio_sck; |
| 45 | |
| 46 | /* |
| 47 | * Set GPIO pins to output mode, with initial state: |
| 48 | * SCK = 0, SDA = 1 |
| 49 | */ |
| 50 | gpio_direction_output(sck, 0); |
| 51 | gpio_direction_output(sda, 1); |
| 52 | rtl8366_smi_clk_delay(smi); |
| 53 | |
| 54 | /* CLK 1: 0 -> 1, 1 -> 0 */ |
| 55 | gpio_set_value(sck, 1); |
| 56 | rtl8366_smi_clk_delay(smi); |
| 57 | gpio_set_value(sck, 0); |
| 58 | rtl8366_smi_clk_delay(smi); |
| 59 | |
| 60 | /* CLK 2: */ |
| 61 | gpio_set_value(sck, 1); |
| 62 | rtl8366_smi_clk_delay(smi); |
| 63 | gpio_set_value(sda, 0); |
| 64 | rtl8366_smi_clk_delay(smi); |
| 65 | gpio_set_value(sck, 0); |
| 66 | rtl8366_smi_clk_delay(smi); |
| 67 | gpio_set_value(sda, 1); |
| 68 | } |
| 69 | |
| 70 | static void rtl8366_smi_stop(struct rtl8366_smi *smi) |
| 71 | { |
| 72 | unsigned int sda = smi->gpio_sda; |
| 73 | unsigned int sck = smi->gpio_sck; |
| 74 | |
| 75 | rtl8366_smi_clk_delay(smi); |
| 76 | gpio_set_value(sda, 0); |
| 77 | gpio_set_value(sck, 1); |
| 78 | rtl8366_smi_clk_delay(smi); |
| 79 | gpio_set_value(sda, 1); |
| 80 | rtl8366_smi_clk_delay(smi); |
| 81 | gpio_set_value(sck, 1); |
| 82 | rtl8366_smi_clk_delay(smi); |
| 83 | gpio_set_value(sck, 0); |
| 84 | rtl8366_smi_clk_delay(smi); |
| 85 | gpio_set_value(sck, 1); |
| 86 | |
| 87 | /* add a click */ |
| 88 | rtl8366_smi_clk_delay(smi); |
| 89 | gpio_set_value(sck, 0); |
| 90 | rtl8366_smi_clk_delay(smi); |
| 91 | gpio_set_value(sck, 1); |
| 92 | |
| 93 | /* set GPIO pins to input mode */ |
| 94 | gpio_direction_input(sda); |
| 95 | gpio_direction_input(sck); |
| 96 | } |
| 97 | |
| 98 | static void rtl8366_smi_write_bits(struct rtl8366_smi *smi, u32 data, u32 len) |
| 99 | { |
| 100 | unsigned int sda = smi->gpio_sda; |
| 101 | unsigned int sck = smi->gpio_sck; |
| 102 | |
| 103 | for (; len > 0; len--) { |
| 104 | rtl8366_smi_clk_delay(smi); |
| 105 | |
| 106 | /* prepare data */ |
| 107 | gpio_set_value(sda, !!(data & ( 1 << (len - 1)))); |
| 108 | rtl8366_smi_clk_delay(smi); |
| 109 | |
| 110 | /* clocking */ |
| 111 | gpio_set_value(sck, 1); |
| 112 | rtl8366_smi_clk_delay(smi); |
| 113 | gpio_set_value(sck, 0); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | static void rtl8366_smi_read_bits(struct rtl8366_smi *smi, u32 len, u32 *data) |
| 118 | { |
| 119 | unsigned int sda = smi->gpio_sda; |
| 120 | unsigned int sck = smi->gpio_sck; |
| 121 | |
| 122 | gpio_direction_input(sda); |
| 123 | |
| 124 | for (*data = 0; len > 0; len--) { |
| 125 | u32 u; |
| 126 | |
| 127 | rtl8366_smi_clk_delay(smi); |
| 128 | |
| 129 | /* clocking */ |
| 130 | gpio_set_value(sck, 1); |
| 131 | rtl8366_smi_clk_delay(smi); |
| 132 | u = !!gpio_get_value(sda); |
| 133 | gpio_set_value(sck, 0); |
| 134 | |
| 135 | *data |= (u << (len - 1)); |
| 136 | } |
| 137 | |
| 138 | gpio_direction_output(sda, 0); |
| 139 | } |
| 140 | |
| 141 | static int rtl8366_smi_wait_for_ack(struct rtl8366_smi *smi) |
| 142 | { |
| 143 | int retry_cnt; |
| 144 | |
| 145 | retry_cnt = 0; |
| 146 | do { |
| 147 | u32 ack; |
| 148 | |
| 149 | rtl8366_smi_read_bits(smi, 1, &ack); |
| 150 | if (ack == 0) |
| 151 | break; |
| 152 | |
| 153 | if (++retry_cnt > RTL8366_SMI_ACK_RETRY_COUNT) { |
| 154 | dev_err(smi->parent, "ACK timeout\n"); |
| 155 | return -ETIMEDOUT; |
| 156 | } |
| 157 | } while (1); |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | static int rtl8366_smi_write_byte(struct rtl8366_smi *smi, u8 data) |
| 163 | { |
| 164 | rtl8366_smi_write_bits(smi, data, 8); |
| 165 | return rtl8366_smi_wait_for_ack(smi); |
| 166 | } |
| 167 | |
| 168 | static int rtl8366_smi_write_byte_noack(struct rtl8366_smi *smi, u8 data) |
| 169 | { |
| 170 | rtl8366_smi_write_bits(smi, data, 8); |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | static int rtl8366_smi_read_byte0(struct rtl8366_smi *smi, u8 *data) |
| 175 | { |
| 176 | u32 t; |
| 177 | |
| 178 | /* read data */ |
| 179 | rtl8366_smi_read_bits(smi, 8, &t); |
| 180 | *data = (t & 0xff); |
| 181 | |
| 182 | /* send an ACK */ |
| 183 | rtl8366_smi_write_bits(smi, 0x00, 1); |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | static int rtl8366_smi_read_byte1(struct rtl8366_smi *smi, u8 *data) |
| 189 | { |
| 190 | u32 t; |
| 191 | |
| 192 | /* read data */ |
| 193 | rtl8366_smi_read_bits(smi, 8, &t); |
| 194 | *data = (t & 0xff); |
| 195 | |
| 196 | /* send an ACK */ |
| 197 | rtl8366_smi_write_bits(smi, 0x01, 1); |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | static int __rtl8366_smi_read_reg(struct rtl8366_smi *smi, u32 addr, u32 *data) |
| 203 | { |
| 204 | unsigned long flags; |
| 205 | u8 lo = 0; |
| 206 | u8 hi = 0; |
| 207 | int ret; |
| 208 | |
| 209 | spin_lock_irqsave(&smi->lock, flags); |
| 210 | |
| 211 | rtl8366_smi_start(smi); |
| 212 | |
| 213 | /* send READ command */ |
| 214 | ret = rtl8366_smi_write_byte(smi, smi->cmd_read); |
| 215 | if (ret) |
| 216 | goto out; |
| 217 | |
| 218 | /* set ADDR[7:0] */ |
| 219 | ret = rtl8366_smi_write_byte(smi, addr & 0xff); |
| 220 | if (ret) |
| 221 | goto out; |
| 222 | |
| 223 | /* set ADDR[15:8] */ |
| 224 | ret = rtl8366_smi_write_byte(smi, addr >> 8); |
| 225 | if (ret) |
| 226 | goto out; |
| 227 | |
| 228 | /* read DATA[7:0] */ |
| 229 | rtl8366_smi_read_byte0(smi, &lo); |
| 230 | /* read DATA[15:8] */ |
| 231 | rtl8366_smi_read_byte1(smi, &hi); |
| 232 | |
| 233 | *data = ((u32) lo) | (((u32) hi) << 8); |
| 234 | |
| 235 | ret = 0; |
| 236 | |
| 237 | out: |
| 238 | rtl8366_smi_stop(smi); |
| 239 | spin_unlock_irqrestore(&smi->lock, flags); |
| 240 | |
| 241 | return ret; |
| 242 | } |
| 243 | /* Read/write via mdiobus */ |
| 244 | #define MDC_MDIO_CTRL0_REG 31 |
| 245 | #define MDC_MDIO_START_REG 29 |
| 246 | #define MDC_MDIO_CTRL1_REG 21 |
| 247 | #define MDC_MDIO_ADDRESS_REG 23 |
| 248 | #define MDC_MDIO_DATA_WRITE_REG 24 |
| 249 | #define MDC_MDIO_DATA_READ_REG 25 |
| 250 | |
| 251 | #define MDC_MDIO_START_OP 0xFFFF |
| 252 | #define MDC_MDIO_ADDR_OP 0x000E |
| 253 | #define MDC_MDIO_READ_OP 0x0001 |
| 254 | #define MDC_MDIO_WRITE_OP 0x0003 |
| 255 | #define MDC_REALTEK_PHY_ADDR 0x0 |
| 256 | |
| 257 | int __rtl8366_mdio_read_reg(struct rtl8366_smi *smi, u32 addr, u32 *data) |
| 258 | { |
| 259 | u32 phy_id = MDC_REALTEK_PHY_ADDR; |
| 260 | struct mii_bus *mbus = smi->ext_mbus; |
| 261 | |
| 262 | BUG_ON(in_interrupt()); |
| 263 | |
| 264 | mutex_lock(&mbus->mdio_lock); |
| 265 | /* Write Start command to register 29 */ |
| 266 | mbus->write(mbus, phy_id, MDC_MDIO_START_REG, MDC_MDIO_START_OP); |
| 267 | |
| 268 | /* Write address control code to register 31 */ |
| 269 | mbus->write(mbus, phy_id, MDC_MDIO_CTRL0_REG, MDC_MDIO_ADDR_OP); |
| 270 | |
| 271 | /* Write Start command to register 29 */ |
| 272 | mbus->write(mbus, phy_id, MDC_MDIO_START_REG, MDC_MDIO_START_OP); |
| 273 | |
| 274 | /* Write address to register 23 */ |
| 275 | mbus->write(mbus, phy_id, MDC_MDIO_ADDRESS_REG, addr); |
| 276 | |
| 277 | /* Write Start command to register 29 */ |
| 278 | mbus->write(mbus, phy_id, MDC_MDIO_START_REG, MDC_MDIO_START_OP); |
| 279 | |
| 280 | /* Write read control code to register 21 */ |
| 281 | mbus->write(mbus, phy_id, MDC_MDIO_CTRL1_REG, MDC_MDIO_READ_OP); |
| 282 | |
| 283 | /* Write Start command to register 29 */ |
| 284 | mbus->write(smi->ext_mbus, phy_id, MDC_MDIO_START_REG, MDC_MDIO_START_OP); |
| 285 | |
| 286 | /* Read data from register 25 */ |
| 287 | *data = mbus->read(mbus, phy_id, MDC_MDIO_DATA_READ_REG); |
| 288 | |
| 289 | mutex_unlock(&mbus->mdio_lock); |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | static int __rtl8366_mdio_write_reg(struct rtl8366_smi *smi, u32 addr, u32 data) |
| 295 | { |
| 296 | u32 phy_id = MDC_REALTEK_PHY_ADDR; |
| 297 | struct mii_bus *mbus = smi->ext_mbus; |
| 298 | |
| 299 | BUG_ON(in_interrupt()); |
| 300 | |
| 301 | mutex_lock(&mbus->mdio_lock); |
| 302 | |
| 303 | /* Write Start command to register 29 */ |
| 304 | mbus->write(mbus, phy_id, MDC_MDIO_START_REG, MDC_MDIO_START_OP); |
| 305 | |
| 306 | /* Write address control code to register 31 */ |
| 307 | mbus->write(mbus, phy_id, MDC_MDIO_CTRL0_REG, MDC_MDIO_ADDR_OP); |
| 308 | |
| 309 | /* Write Start command to register 29 */ |
| 310 | mbus->write(mbus, phy_id, MDC_MDIO_START_REG, MDC_MDIO_START_OP); |
| 311 | |
| 312 | /* Write address to register 23 */ |
| 313 | mbus->write(mbus, phy_id, MDC_MDIO_ADDRESS_REG, addr); |
| 314 | |
| 315 | /* Write Start command to register 29 */ |
| 316 | mbus->write(mbus, phy_id, MDC_MDIO_START_REG, MDC_MDIO_START_OP); |
| 317 | |
| 318 | /* Write data to register 24 */ |
| 319 | mbus->write(mbus, phy_id, MDC_MDIO_DATA_WRITE_REG, data); |
| 320 | |
| 321 | /* Write Start command to register 29 */ |
| 322 | mbus->write(mbus, phy_id, MDC_MDIO_START_REG, MDC_MDIO_START_OP); |
| 323 | |
| 324 | /* Write data control code to register 21 */ |
| 325 | mbus->write(mbus, phy_id, MDC_MDIO_CTRL1_REG, MDC_MDIO_WRITE_OP); |
| 326 | |
| 327 | mutex_unlock(&mbus->mdio_lock); |
| 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | int rtl8366_smi_read_reg(struct rtl8366_smi *smi, u32 addr, u32 *data) |
| 332 | { |
| 333 | if (smi->ext_mbus) |
| 334 | return __rtl8366_mdio_read_reg(smi, addr, data); |
| 335 | else |
| 336 | return __rtl8366_smi_read_reg(smi, addr, data); |
| 337 | } |
| 338 | EXPORT_SYMBOL_GPL(rtl8366_smi_read_reg); |
| 339 | |
| 340 | static int __rtl8366_smi_write_reg(struct rtl8366_smi *smi, |
| 341 | u32 addr, u32 data, bool ack) |
| 342 | { |
| 343 | unsigned long flags; |
| 344 | int ret; |
| 345 | |
| 346 | spin_lock_irqsave(&smi->lock, flags); |
| 347 | |
| 348 | rtl8366_smi_start(smi); |
| 349 | |
| 350 | /* send WRITE command */ |
| 351 | ret = rtl8366_smi_write_byte(smi, smi->cmd_write); |
| 352 | if (ret) |
| 353 | goto out; |
| 354 | |
| 355 | /* set ADDR[7:0] */ |
| 356 | ret = rtl8366_smi_write_byte(smi, addr & 0xff); |
| 357 | if (ret) |
| 358 | goto out; |
| 359 | |
| 360 | /* set ADDR[15:8] */ |
| 361 | ret = rtl8366_smi_write_byte(smi, addr >> 8); |
| 362 | if (ret) |
| 363 | goto out; |
| 364 | |
| 365 | /* write DATA[7:0] */ |
| 366 | ret = rtl8366_smi_write_byte(smi, data & 0xff); |
| 367 | if (ret) |
| 368 | goto out; |
| 369 | |
| 370 | /* write DATA[15:8] */ |
| 371 | if (ack) |
| 372 | ret = rtl8366_smi_write_byte(smi, data >> 8); |
| 373 | else |
| 374 | ret = rtl8366_smi_write_byte_noack(smi, data >> 8); |
| 375 | if (ret) |
| 376 | goto out; |
| 377 | |
| 378 | ret = 0; |
| 379 | |
| 380 | out: |
| 381 | rtl8366_smi_stop(smi); |
| 382 | spin_unlock_irqrestore(&smi->lock, flags); |
| 383 | |
| 384 | return ret; |
| 385 | } |
| 386 | |
| 387 | int rtl8366_smi_write_reg(struct rtl8366_smi *smi, u32 addr, u32 data) |
| 388 | { |
| 389 | if (smi->ext_mbus) |
| 390 | return __rtl8366_mdio_write_reg(smi, addr, data); |
| 391 | else |
| 392 | return __rtl8366_smi_write_reg(smi, addr, data, true); |
| 393 | } |
| 394 | EXPORT_SYMBOL_GPL(rtl8366_smi_write_reg); |
| 395 | |
| 396 | int rtl8366_smi_write_reg_noack(struct rtl8366_smi *smi, u32 addr, u32 data) |
| 397 | { |
| 398 | return __rtl8366_smi_write_reg(smi, addr, data, false); |
| 399 | } |
| 400 | EXPORT_SYMBOL_GPL(rtl8366_smi_write_reg_noack); |
| 401 | |
| 402 | int rtl8366_smi_rmwr(struct rtl8366_smi *smi, u32 addr, u32 mask, u32 data) |
| 403 | { |
| 404 | u32 t; |
| 405 | int err; |
| 406 | |
| 407 | err = rtl8366_smi_read_reg(smi, addr, &t); |
| 408 | if (err) |
| 409 | return err; |
| 410 | |
| 411 | err = rtl8366_smi_write_reg(smi, addr, (t & ~mask) | data); |
| 412 | return err; |
| 413 | |
| 414 | } |
| 415 | EXPORT_SYMBOL_GPL(rtl8366_smi_rmwr); |
| 416 | |
| 417 | static int rtl8366_reset(struct rtl8366_smi *smi) |
| 418 | { |
| 419 | if (smi->hw_reset) { |
| 420 | smi->hw_reset(smi, true); |
| 421 | msleep(RTL8366_SMI_HW_STOP_DELAY); |
| 422 | smi->hw_reset(smi, false); |
| 423 | msleep(RTL8366_SMI_HW_START_DELAY); |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | return smi->ops->reset_chip(smi); |
| 428 | } |
| 429 | |
| 430 | static int rtl8366_mc_is_used(struct rtl8366_smi *smi, int mc_index, int *used) |
| 431 | { |
| 432 | int err; |
| 433 | int i; |
| 434 | |
| 435 | *used = 0; |
| 436 | for (i = 0; i < smi->num_ports; i++) { |
| 437 | int index = 0; |
| 438 | |
| 439 | err = smi->ops->get_mc_index(smi, i, &index); |
| 440 | if (err) |
| 441 | return err; |
| 442 | |
| 443 | if (mc_index == index) { |
| 444 | *used = 1; |
| 445 | break; |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | return 0; |
| 450 | } |
| 451 | |
| 452 | static int rtl8366_set_vlan(struct rtl8366_smi *smi, int vid, u32 member, |
| 453 | u32 untag, u32 fid) |
| 454 | { |
| 455 | struct rtl8366_vlan_4k vlan4k; |
| 456 | int err; |
| 457 | int i; |
| 458 | |
| 459 | /* Update the 4K table */ |
| 460 | err = smi->ops->get_vlan_4k(smi, vid, &vlan4k); |
| 461 | if (err) |
| 462 | return err; |
| 463 | |
| 464 | vlan4k.member = member; |
| 465 | vlan4k.untag = untag; |
| 466 | vlan4k.fid = fid; |
| 467 | err = smi->ops->set_vlan_4k(smi, &vlan4k); |
| 468 | if (err) |
| 469 | return err; |
| 470 | |
| 471 | /* Try to find an existing MC entry for this VID */ |
| 472 | for (i = 0; i < smi->num_vlan_mc; i++) { |
| 473 | struct rtl8366_vlan_mc vlanmc; |
| 474 | |
| 475 | err = smi->ops->get_vlan_mc(smi, i, &vlanmc); |
| 476 | if (err) |
| 477 | return err; |
| 478 | |
| 479 | if (vid == vlanmc.vid) { |
| 480 | /* update the MC entry */ |
| 481 | vlanmc.member = member; |
| 482 | vlanmc.untag = untag; |
| 483 | vlanmc.fid = fid; |
| 484 | |
| 485 | err = smi->ops->set_vlan_mc(smi, i, &vlanmc); |
| 486 | break; |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | return err; |
| 491 | } |
| 492 | |
| 493 | static int rtl8366_get_pvid(struct rtl8366_smi *smi, int port, int *val) |
| 494 | { |
| 495 | struct rtl8366_vlan_mc vlanmc; |
| 496 | int err; |
| 497 | int index; |
| 498 | |
| 499 | err = smi->ops->get_mc_index(smi, port, &index); |
| 500 | if (err) |
| 501 | return err; |
| 502 | |
| 503 | err = smi->ops->get_vlan_mc(smi, index, &vlanmc); |
| 504 | if (err) |
| 505 | return err; |
| 506 | |
| 507 | *val = vlanmc.vid; |
| 508 | return 0; |
| 509 | } |
| 510 | |
| 511 | static int rtl8366_set_pvid(struct rtl8366_smi *smi, unsigned port, |
| 512 | unsigned vid) |
| 513 | { |
| 514 | struct rtl8366_vlan_mc vlanmc; |
| 515 | struct rtl8366_vlan_4k vlan4k; |
| 516 | int err; |
| 517 | int i; |
| 518 | |
| 519 | /* Try to find an existing MC entry for this VID */ |
| 520 | for (i = 0; i < smi->num_vlan_mc; i++) { |
| 521 | err = smi->ops->get_vlan_mc(smi, i, &vlanmc); |
| 522 | if (err) |
| 523 | return err; |
| 524 | |
| 525 | if (vid == vlanmc.vid) { |
| 526 | err = smi->ops->set_vlan_mc(smi, i, &vlanmc); |
| 527 | if (err) |
| 528 | return err; |
| 529 | |
| 530 | err = smi->ops->set_mc_index(smi, port, i); |
| 531 | return err; |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | /* We have no MC entry for this VID, try to find an empty one */ |
| 536 | for (i = 0; i < smi->num_vlan_mc; i++) { |
| 537 | err = smi->ops->get_vlan_mc(smi, i, &vlanmc); |
| 538 | if (err) |
| 539 | return err; |
| 540 | |
| 541 | if (vlanmc.vid == 0 && vlanmc.member == 0) { |
| 542 | /* Update the entry from the 4K table */ |
| 543 | err = smi->ops->get_vlan_4k(smi, vid, &vlan4k); |
| 544 | if (err) |
| 545 | return err; |
| 546 | |
| 547 | vlanmc.vid = vid; |
| 548 | vlanmc.member = vlan4k.member; |
| 549 | vlanmc.untag = vlan4k.untag; |
| 550 | vlanmc.fid = vlan4k.fid; |
| 551 | err = smi->ops->set_vlan_mc(smi, i, &vlanmc); |
| 552 | if (err) |
| 553 | return err; |
| 554 | |
| 555 | err = smi->ops->set_mc_index(smi, port, i); |
| 556 | return err; |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | /* MC table is full, try to find an unused entry and replace it */ |
| 561 | for (i = 0; i < smi->num_vlan_mc; i++) { |
| 562 | int used; |
| 563 | |
| 564 | err = rtl8366_mc_is_used(smi, i, &used); |
| 565 | if (err) |
| 566 | return err; |
| 567 | |
| 568 | if (!used) { |
| 569 | /* Update the entry from the 4K table */ |
| 570 | err = smi->ops->get_vlan_4k(smi, vid, &vlan4k); |
| 571 | if (err) |
| 572 | return err; |
| 573 | |
| 574 | vlanmc.vid = vid; |
| 575 | vlanmc.member = vlan4k.member; |
| 576 | vlanmc.untag = vlan4k.untag; |
| 577 | vlanmc.fid = vlan4k.fid; |
| 578 | err = smi->ops->set_vlan_mc(smi, i, &vlanmc); |
| 579 | if (err) |
| 580 | return err; |
| 581 | |
| 582 | err = smi->ops->set_mc_index(smi, port, i); |
| 583 | return err; |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | dev_err(smi->parent, |
| 588 | "all VLAN member configurations are in use\n"); |
| 589 | |
| 590 | return -ENOSPC; |
| 591 | } |
| 592 | |
| 593 | int rtl8366_enable_vlan(struct rtl8366_smi *smi, int enable) |
| 594 | { |
| 595 | int err; |
| 596 | |
| 597 | err = smi->ops->enable_vlan(smi, enable); |
| 598 | if (err) |
| 599 | return err; |
| 600 | |
| 601 | smi->vlan_enabled = enable; |
| 602 | |
| 603 | if (!enable) { |
| 604 | smi->vlan4k_enabled = 0; |
| 605 | err = smi->ops->enable_vlan4k(smi, enable); |
| 606 | } |
| 607 | |
| 608 | return err; |
| 609 | } |
| 610 | EXPORT_SYMBOL_GPL(rtl8366_enable_vlan); |
| 611 | |
| 612 | static int rtl8366_enable_vlan4k(struct rtl8366_smi *smi, int enable) |
| 613 | { |
| 614 | int err; |
| 615 | |
| 616 | if (enable) { |
| 617 | err = smi->ops->enable_vlan(smi, enable); |
| 618 | if (err) |
| 619 | return err; |
| 620 | |
| 621 | smi->vlan_enabled = enable; |
| 622 | } |
| 623 | |
| 624 | err = smi->ops->enable_vlan4k(smi, enable); |
| 625 | if (err) |
| 626 | return err; |
| 627 | |
| 628 | smi->vlan4k_enabled = enable; |
| 629 | return 0; |
| 630 | } |
| 631 | |
| 632 | int rtl8366_enable_all_ports(struct rtl8366_smi *smi, int enable) |
| 633 | { |
| 634 | int port; |
| 635 | int err; |
| 636 | |
| 637 | for (port = 0; port < smi->num_ports; port++) { |
| 638 | err = smi->ops->enable_port(smi, port, enable); |
| 639 | if (err) |
| 640 | return err; |
| 641 | } |
| 642 | |
| 643 | return 0; |
| 644 | } |
| 645 | EXPORT_SYMBOL_GPL(rtl8366_enable_all_ports); |
| 646 | |
| 647 | int rtl8366_reset_vlan(struct rtl8366_smi *smi) |
| 648 | { |
| 649 | struct rtl8366_vlan_mc vlanmc; |
| 650 | int err; |
| 651 | int i; |
| 652 | |
| 653 | rtl8366_enable_vlan(smi, 0); |
| 654 | rtl8366_enable_vlan4k(smi, 0); |
| 655 | |
| 656 | /* clear VLAN member configurations */ |
| 657 | vlanmc.vid = 0; |
| 658 | vlanmc.priority = 0; |
| 659 | vlanmc.member = 0; |
| 660 | vlanmc.untag = 0; |
| 661 | vlanmc.fid = 0; |
| 662 | for (i = 0; i < smi->num_vlan_mc; i++) { |
| 663 | err = smi->ops->set_vlan_mc(smi, i, &vlanmc); |
| 664 | if (err) |
| 665 | return err; |
| 666 | } |
| 667 | |
| 668 | return 0; |
| 669 | } |
| 670 | EXPORT_SYMBOL_GPL(rtl8366_reset_vlan); |
| 671 | |
| 672 | static int rtl8366_init_vlan(struct rtl8366_smi *smi) |
| 673 | { |
| 674 | int port; |
| 675 | int err; |
| 676 | |
| 677 | err = rtl8366_reset_vlan(smi); |
| 678 | if (err) |
| 679 | return err; |
| 680 | |
| 681 | for (port = 0; port < smi->num_ports; port++) { |
| 682 | u32 mask; |
| 683 | |
| 684 | if (port == smi->cpu_port) |
| 685 | mask = (1 << smi->num_ports) - 1; |
| 686 | else |
| 687 | mask = (1 << port) | (1 << smi->cpu_port); |
| 688 | |
| 689 | err = rtl8366_set_vlan(smi, (port + 1), mask, mask, 0); |
| 690 | if (err) |
| 691 | return err; |
| 692 | |
| 693 | err = rtl8366_set_pvid(smi, port, (port + 1)); |
| 694 | if (err) |
| 695 | return err; |
| 696 | } |
| 697 | |
| 698 | return rtl8366_enable_vlan(smi, 1); |
| 699 | } |
| 700 | |
| 701 | #ifdef CONFIG_RTL8366_SMI_DEBUG_FS |
| 702 | int rtl8366_debugfs_open(struct inode *inode, struct file *file) |
| 703 | { |
| 704 | file->private_data = inode->i_private; |
| 705 | return 0; |
| 706 | } |
| 707 | EXPORT_SYMBOL_GPL(rtl8366_debugfs_open); |
| 708 | |
| 709 | static ssize_t rtl8366_read_debugfs_vlan_mc(struct file *file, |
| 710 | char __user *user_buf, |
| 711 | size_t count, loff_t *ppos) |
| 712 | { |
| 713 | struct rtl8366_smi *smi = (struct rtl8366_smi *)file->private_data; |
| 714 | int i, len = 0; |
| 715 | char *buf = smi->buf; |
| 716 | |
| 717 | len += snprintf(buf + len, sizeof(smi->buf) - len, |
| 718 | "%2s %6s %4s %6s %6s %3s\n", |
| 719 | "id", "vid","prio", "member", "untag", "fid"); |
| 720 | |
| 721 | for (i = 0; i < smi->num_vlan_mc; ++i) { |
| 722 | struct rtl8366_vlan_mc vlanmc; |
| 723 | |
| 724 | smi->ops->get_vlan_mc(smi, i, &vlanmc); |
| 725 | |
| 726 | len += snprintf(buf + len, sizeof(smi->buf) - len, |
| 727 | "%2d %6d %4d 0x%04x 0x%04x %3d\n", |
| 728 | i, vlanmc.vid, vlanmc.priority, |
| 729 | vlanmc.member, vlanmc.untag, vlanmc.fid); |
| 730 | } |
| 731 | |
| 732 | return simple_read_from_buffer(user_buf, count, ppos, buf, len); |
| 733 | } |
| 734 | |
| 735 | #define RTL8366_VLAN4K_PAGE_SIZE 64 |
| 736 | #define RTL8366_VLAN4K_NUM_PAGES (4096 / RTL8366_VLAN4K_PAGE_SIZE) |
| 737 | |
| 738 | static ssize_t rtl8366_read_debugfs_vlan_4k(struct file *file, |
| 739 | char __user *user_buf, |
| 740 | size_t count, loff_t *ppos) |
| 741 | { |
| 742 | struct rtl8366_smi *smi = (struct rtl8366_smi *)file->private_data; |
| 743 | int i, len = 0; |
| 744 | int offset; |
| 745 | char *buf = smi->buf; |
| 746 | |
| 747 | if (smi->dbg_vlan_4k_page >= RTL8366_VLAN4K_NUM_PAGES) { |
| 748 | len += snprintf(buf + len, sizeof(smi->buf) - len, |
| 749 | "invalid page: %u\n", smi->dbg_vlan_4k_page); |
| 750 | return simple_read_from_buffer(user_buf, count, ppos, buf, len); |
| 751 | } |
| 752 | |
| 753 | len += snprintf(buf + len, sizeof(smi->buf) - len, |
| 754 | "%4s %6s %6s %3s\n", |
| 755 | "vid", "member", "untag", "fid"); |
| 756 | |
| 757 | offset = RTL8366_VLAN4K_PAGE_SIZE * smi->dbg_vlan_4k_page; |
| 758 | for (i = 0; i < RTL8366_VLAN4K_PAGE_SIZE; i++) { |
| 759 | struct rtl8366_vlan_4k vlan4k; |
| 760 | |
| 761 | smi->ops->get_vlan_4k(smi, offset + i, &vlan4k); |
| 762 | |
| 763 | len += snprintf(buf + len, sizeof(smi->buf) - len, |
| 764 | "%4d 0x%04x 0x%04x %3d\n", |
| 765 | vlan4k.vid, vlan4k.member, |
| 766 | vlan4k.untag, vlan4k.fid); |
| 767 | } |
| 768 | |
| 769 | return simple_read_from_buffer(user_buf, count, ppos, buf, len); |
| 770 | } |
| 771 | |
| 772 | static ssize_t rtl8366_read_debugfs_pvid(struct file *file, |
| 773 | char __user *user_buf, |
| 774 | size_t count, loff_t *ppos) |
| 775 | { |
| 776 | struct rtl8366_smi *smi = (struct rtl8366_smi *)file->private_data; |
| 777 | char *buf = smi->buf; |
| 778 | int len = 0; |
| 779 | int i; |
| 780 | |
| 781 | len += snprintf(buf + len, sizeof(smi->buf) - len, "%4s %4s\n", |
| 782 | "port", "pvid"); |
| 783 | |
| 784 | for (i = 0; i < smi->num_ports; i++) { |
| 785 | int pvid; |
| 786 | int err; |
| 787 | |
| 788 | err = rtl8366_get_pvid(smi, i, &pvid); |
| 789 | if (err) |
| 790 | len += snprintf(buf + len, sizeof(smi->buf) - len, |
| 791 | "%4d error\n", i); |
| 792 | else |
| 793 | len += snprintf(buf + len, sizeof(smi->buf) - len, |
| 794 | "%4d %4d\n", i, pvid); |
| 795 | } |
| 796 | |
| 797 | return simple_read_from_buffer(user_buf, count, ppos, buf, len); |
| 798 | } |
| 799 | |
| 800 | static ssize_t rtl8366_read_debugfs_reg(struct file *file, |
| 801 | char __user *user_buf, |
| 802 | size_t count, loff_t *ppos) |
| 803 | { |
| 804 | struct rtl8366_smi *smi = (struct rtl8366_smi *)file->private_data; |
| 805 | u32 t, reg = smi->dbg_reg; |
| 806 | int err, len = 0; |
| 807 | char *buf = smi->buf; |
| 808 | |
| 809 | memset(buf, '\0', sizeof(smi->buf)); |
| 810 | |
| 811 | err = rtl8366_smi_read_reg(smi, reg, &t); |
| 812 | if (err) { |
| 813 | len += snprintf(buf, sizeof(smi->buf), |
| 814 | "Read failed (reg: 0x%04x)\n", reg); |
| 815 | return simple_read_from_buffer(user_buf, count, ppos, buf, len); |
| 816 | } |
| 817 | |
| 818 | len += snprintf(buf, sizeof(smi->buf), "reg = 0x%04x, val = 0x%04x\n", |
| 819 | reg, t); |
| 820 | |
| 821 | return simple_read_from_buffer(user_buf, count, ppos, buf, len); |
| 822 | } |
| 823 | |
| 824 | static ssize_t rtl8366_write_debugfs_reg(struct file *file, |
| 825 | const char __user *user_buf, |
| 826 | size_t count, loff_t *ppos) |
| 827 | { |
| 828 | struct rtl8366_smi *smi = (struct rtl8366_smi *)file->private_data; |
| 829 | unsigned long data; |
| 830 | u32 reg = smi->dbg_reg; |
| 831 | int err; |
| 832 | size_t len; |
| 833 | char *buf = smi->buf; |
| 834 | |
| 835 | len = min(count, sizeof(smi->buf) - 1); |
| 836 | if (copy_from_user(buf, user_buf, len)) { |
| 837 | dev_err(smi->parent, "copy from user failed\n"); |
| 838 | return -EFAULT; |
| 839 | } |
| 840 | |
| 841 | buf[len] = '\0'; |
| 842 | if (len > 0 && buf[len - 1] == '\n') |
| 843 | buf[len - 1] = '\0'; |
| 844 | |
| 845 | |
| 846 | if (kstrtoul(buf, 16, &data)) { |
| 847 | dev_err(smi->parent, "Invalid reg value %s\n", buf); |
| 848 | } else { |
| 849 | err = rtl8366_smi_write_reg(smi, reg, data); |
| 850 | if (err) { |
| 851 | dev_err(smi->parent, |
| 852 | "writing reg 0x%04x val 0x%04lx failed\n", |
| 853 | reg, data); |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | return count; |
| 858 | } |
| 859 | |
| 860 | static ssize_t rtl8366_read_debugfs_mibs(struct file *file, |
| 861 | char __user *user_buf, |
| 862 | size_t count, loff_t *ppos) |
| 863 | { |
| 864 | struct rtl8366_smi *smi = file->private_data; |
| 865 | int i, j, len = 0; |
| 866 | char *buf = smi->buf; |
| 867 | |
| 868 | len += snprintf(buf + len, sizeof(smi->buf) - len, "%-36s", |
| 869 | "Counter"); |
| 870 | |
| 871 | for (i = 0; i < smi->num_ports; i++) { |
| 872 | char port_buf[10]; |
| 873 | |
| 874 | snprintf(port_buf, sizeof(port_buf), "Port %d", i); |
| 875 | len += snprintf(buf + len, sizeof(smi->buf) - len, " %12s", |
| 876 | port_buf); |
| 877 | } |
| 878 | len += snprintf(buf + len, sizeof(smi->buf) - len, "\n"); |
| 879 | |
| 880 | for (i = 0; i < smi->num_mib_counters; i++) { |
| 881 | len += snprintf(buf + len, sizeof(smi->buf) - len, "%-36s ", |
| 882 | smi->mib_counters[i].name); |
| 883 | for (j = 0; j < smi->num_ports; j++) { |
| 884 | unsigned long long counter = 0; |
| 885 | |
| 886 | if (!smi->ops->get_mib_counter(smi, i, j, &counter)) |
| 887 | len += snprintf(buf + len, |
| 888 | sizeof(smi->buf) - len, |
| 889 | "%12llu ", counter); |
| 890 | else |
| 891 | len += snprintf(buf + len, |
| 892 | sizeof(smi->buf) - len, |
| 893 | "%12s ", "error"); |
| 894 | } |
| 895 | len += snprintf(buf + len, sizeof(smi->buf) - len, "\n"); |
| 896 | } |
| 897 | |
| 898 | return simple_read_from_buffer(user_buf, count, ppos, buf, len); |
| 899 | } |
| 900 | |
| 901 | static const struct file_operations fops_rtl8366_regs = { |
| 902 | .read = rtl8366_read_debugfs_reg, |
| 903 | .write = rtl8366_write_debugfs_reg, |
| 904 | .open = rtl8366_debugfs_open, |
| 905 | .owner = THIS_MODULE |
| 906 | }; |
| 907 | |
| 908 | static const struct file_operations fops_rtl8366_vlan_mc = { |
| 909 | .read = rtl8366_read_debugfs_vlan_mc, |
| 910 | .open = rtl8366_debugfs_open, |
| 911 | .owner = THIS_MODULE |
| 912 | }; |
| 913 | |
| 914 | static const struct file_operations fops_rtl8366_vlan_4k = { |
| 915 | .read = rtl8366_read_debugfs_vlan_4k, |
| 916 | .open = rtl8366_debugfs_open, |
| 917 | .owner = THIS_MODULE |
| 918 | }; |
| 919 | |
| 920 | static const struct file_operations fops_rtl8366_pvid = { |
| 921 | .read = rtl8366_read_debugfs_pvid, |
| 922 | .open = rtl8366_debugfs_open, |
| 923 | .owner = THIS_MODULE |
| 924 | }; |
| 925 | |
| 926 | static const struct file_operations fops_rtl8366_mibs = { |
| 927 | .read = rtl8366_read_debugfs_mibs, |
| 928 | .open = rtl8366_debugfs_open, |
| 929 | .owner = THIS_MODULE |
| 930 | }; |
| 931 | |
| 932 | static void rtl8366_debugfs_init(struct rtl8366_smi *smi) |
| 933 | { |
| 934 | struct dentry *node; |
| 935 | struct dentry *root; |
| 936 | |
| 937 | if (!smi->debugfs_root) |
| 938 | smi->debugfs_root = debugfs_create_dir(dev_name(smi->parent), |
| 939 | NULL); |
| 940 | |
| 941 | if (!smi->debugfs_root) { |
| 942 | dev_err(smi->parent, "Unable to create debugfs dir\n"); |
| 943 | return; |
| 944 | } |
| 945 | root = smi->debugfs_root; |
| 946 | |
| 947 | node = debugfs_create_x16("reg", S_IRUGO | S_IWUSR, root, |
| 948 | &smi->dbg_reg); |
| 949 | if (!node) { |
| 950 | dev_err(smi->parent, "Creating debugfs file '%s' failed\n", |
| 951 | "reg"); |
| 952 | return; |
| 953 | } |
| 954 | |
| 955 | node = debugfs_create_file("val", S_IRUGO | S_IWUSR, root, smi, |
| 956 | &fops_rtl8366_regs); |
| 957 | if (!node) { |
| 958 | dev_err(smi->parent, "Creating debugfs file '%s' failed\n", |
| 959 | "val"); |
| 960 | return; |
| 961 | } |
| 962 | |
| 963 | node = debugfs_create_file("vlan_mc", S_IRUSR, root, smi, |
| 964 | &fops_rtl8366_vlan_mc); |
| 965 | if (!node) { |
| 966 | dev_err(smi->parent, "Creating debugfs file '%s' failed\n", |
| 967 | "vlan_mc"); |
| 968 | return; |
| 969 | } |
| 970 | |
| 971 | node = debugfs_create_u8("vlan_4k_page", S_IRUGO | S_IWUSR, root, |
| 972 | &smi->dbg_vlan_4k_page); |
| 973 | if (!node) { |
| 974 | dev_err(smi->parent, "Creating debugfs file '%s' failed\n", |
| 975 | "vlan_4k_page"); |
| 976 | return; |
| 977 | } |
| 978 | |
| 979 | node = debugfs_create_file("vlan_4k", S_IRUSR, root, smi, |
| 980 | &fops_rtl8366_vlan_4k); |
| 981 | if (!node) { |
| 982 | dev_err(smi->parent, "Creating debugfs file '%s' failed\n", |
| 983 | "vlan_4k"); |
| 984 | return; |
| 985 | } |
| 986 | |
| 987 | node = debugfs_create_file("pvid", S_IRUSR, root, smi, |
| 988 | &fops_rtl8366_pvid); |
| 989 | if (!node) { |
| 990 | dev_err(smi->parent, "Creating debugfs file '%s' failed\n", |
| 991 | "pvid"); |
| 992 | return; |
| 993 | } |
| 994 | |
| 995 | node = debugfs_create_file("mibs", S_IRUSR, smi->debugfs_root, smi, |
| 996 | &fops_rtl8366_mibs); |
| 997 | if (!node) |
| 998 | dev_err(smi->parent, "Creating debugfs file '%s' failed\n", |
| 999 | "mibs"); |
| 1000 | } |
| 1001 | |
| 1002 | static void rtl8366_debugfs_remove(struct rtl8366_smi *smi) |
| 1003 | { |
| 1004 | if (smi->debugfs_root) { |
| 1005 | debugfs_remove_recursive(smi->debugfs_root); |
| 1006 | smi->debugfs_root = NULL; |
| 1007 | } |
| 1008 | } |
| 1009 | #else |
| 1010 | static inline void rtl8366_debugfs_init(struct rtl8366_smi *smi) {} |
| 1011 | static inline void rtl8366_debugfs_remove(struct rtl8366_smi *smi) {} |
| 1012 | #endif /* CONFIG_RTL8366_SMI_DEBUG_FS */ |
| 1013 | |
| 1014 | static int rtl8366_smi_mii_init(struct rtl8366_smi *smi) |
| 1015 | { |
| 1016 | int ret; |
| 1017 | |
| 1018 | #ifdef CONFIG_OF |
| 1019 | struct device_node *np = NULL; |
| 1020 | |
| 1021 | np = of_get_child_by_name(smi->parent->of_node, "mdio-bus"); |
| 1022 | #endif |
| 1023 | |
| 1024 | smi->mii_bus = mdiobus_alloc(); |
| 1025 | if (smi->mii_bus == NULL) { |
| 1026 | ret = -ENOMEM; |
| 1027 | goto err; |
| 1028 | } |
| 1029 | |
| 1030 | smi->mii_bus->priv = (void *) smi; |
| 1031 | smi->mii_bus->name = dev_name(smi->parent); |
| 1032 | smi->mii_bus->read = smi->ops->mii_read; |
| 1033 | smi->mii_bus->write = smi->ops->mii_write; |
| 1034 | snprintf(smi->mii_bus->id, MII_BUS_ID_SIZE, "%s", |
| 1035 | dev_name(smi->parent)); |
| 1036 | smi->mii_bus->parent = smi->parent; |
| 1037 | smi->mii_bus->phy_mask = ~(0x1f); |
| 1038 | |
| 1039 | #ifdef CONFIG_OF |
| 1040 | if (np) |
| 1041 | ret = of_mdiobus_register(smi->mii_bus, np); |
| 1042 | else |
| 1043 | #endif |
| 1044 | ret = mdiobus_register(smi->mii_bus); |
| 1045 | |
| 1046 | if (ret) |
| 1047 | goto err_free; |
| 1048 | |
| 1049 | return 0; |
| 1050 | |
| 1051 | err_free: |
| 1052 | mdiobus_free(smi->mii_bus); |
| 1053 | err: |
| 1054 | return ret; |
| 1055 | } |
| 1056 | |
| 1057 | static void rtl8366_smi_mii_cleanup(struct rtl8366_smi *smi) |
| 1058 | { |
| 1059 | mdiobus_unregister(smi->mii_bus); |
| 1060 | mdiobus_free(smi->mii_bus); |
| 1061 | } |
| 1062 | |
| 1063 | int rtl8366_sw_reset_switch(struct switch_dev *dev) |
| 1064 | { |
| 1065 | struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev); |
| 1066 | int err; |
| 1067 | |
| 1068 | err = rtl8366_reset(smi); |
| 1069 | if (err) |
| 1070 | return err; |
| 1071 | |
| 1072 | err = smi->ops->setup(smi); |
| 1073 | if (err) |
| 1074 | return err; |
| 1075 | |
| 1076 | err = rtl8366_reset_vlan(smi); |
| 1077 | if (err) |
| 1078 | return err; |
| 1079 | |
| 1080 | err = rtl8366_enable_vlan(smi, 1); |
| 1081 | if (err) |
| 1082 | return err; |
| 1083 | |
| 1084 | return rtl8366_enable_all_ports(smi, 1); |
| 1085 | } |
| 1086 | EXPORT_SYMBOL_GPL(rtl8366_sw_reset_switch); |
| 1087 | |
| 1088 | int rtl8366_sw_get_port_pvid(struct switch_dev *dev, int port, int *val) |
| 1089 | { |
| 1090 | struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev); |
| 1091 | return rtl8366_get_pvid(smi, port, val); |
| 1092 | } |
| 1093 | EXPORT_SYMBOL_GPL(rtl8366_sw_get_port_pvid); |
| 1094 | |
| 1095 | int rtl8366_sw_set_port_pvid(struct switch_dev *dev, int port, int val) |
| 1096 | { |
| 1097 | struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev); |
| 1098 | return rtl8366_set_pvid(smi, port, val); |
| 1099 | } |
| 1100 | EXPORT_SYMBOL_GPL(rtl8366_sw_set_port_pvid); |
| 1101 | |
| 1102 | int rtl8366_sw_get_port_mib(struct switch_dev *dev, |
| 1103 | const struct switch_attr *attr, |
| 1104 | struct switch_val *val) |
| 1105 | { |
| 1106 | struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev); |
| 1107 | int i, len = 0; |
| 1108 | unsigned long long counter = 0; |
| 1109 | char *buf = smi->buf; |
| 1110 | |
| 1111 | if (val->port_vlan >= smi->num_ports) |
| 1112 | return -EINVAL; |
| 1113 | |
| 1114 | len += snprintf(buf + len, sizeof(smi->buf) - len, |
| 1115 | "Port %d MIB counters\n", |
| 1116 | val->port_vlan); |
| 1117 | |
| 1118 | for (i = 0; i < smi->num_mib_counters; ++i) { |
| 1119 | len += snprintf(buf + len, sizeof(smi->buf) - len, |
| 1120 | "%-36s: ", smi->mib_counters[i].name); |
| 1121 | if (!smi->ops->get_mib_counter(smi, i, val->port_vlan, |
| 1122 | &counter)) |
| 1123 | len += snprintf(buf + len, sizeof(smi->buf) - len, |
| 1124 | "%llu\n", counter); |
| 1125 | else |
| 1126 | len += snprintf(buf + len, sizeof(smi->buf) - len, |
| 1127 | "%s\n", "error"); |
| 1128 | } |
| 1129 | |
| 1130 | val->value.s = buf; |
| 1131 | val->len = len; |
| 1132 | return 0; |
| 1133 | } |
| 1134 | EXPORT_SYMBOL_GPL(rtl8366_sw_get_port_mib); |
| 1135 | |
| 1136 | int rtl8366_sw_get_port_stats(struct switch_dev *dev, int port, |
| 1137 | struct switch_port_stats *stats, |
| 1138 | int txb_id, int rxb_id) |
| 1139 | { |
| 1140 | struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev); |
| 1141 | unsigned long long counter = 0; |
| 1142 | int ret; |
| 1143 | |
| 1144 | if (port >= smi->num_ports) |
| 1145 | return -EINVAL; |
| 1146 | |
| 1147 | ret = smi->ops->get_mib_counter(smi, txb_id, port, &counter); |
| 1148 | if (ret) |
| 1149 | return ret; |
| 1150 | |
| 1151 | stats->tx_bytes = counter; |
| 1152 | |
| 1153 | ret = smi->ops->get_mib_counter(smi, rxb_id, port, &counter); |
| 1154 | if (ret) |
| 1155 | return ret; |
| 1156 | |
| 1157 | stats->rx_bytes = counter; |
| 1158 | |
| 1159 | return 0; |
| 1160 | } |
| 1161 | EXPORT_SYMBOL_GPL(rtl8366_sw_get_port_stats); |
| 1162 | |
| 1163 | int rtl8366_sw_get_vlan_info(struct switch_dev *dev, |
| 1164 | const struct switch_attr *attr, |
| 1165 | struct switch_val *val) |
| 1166 | { |
| 1167 | int i; |
| 1168 | u32 len = 0; |
| 1169 | struct rtl8366_vlan_4k vlan4k; |
| 1170 | struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev); |
| 1171 | char *buf = smi->buf; |
| 1172 | int err; |
| 1173 | |
| 1174 | if (!smi->ops->is_vlan_valid(smi, val->port_vlan)) |
| 1175 | return -EINVAL; |
| 1176 | |
| 1177 | memset(buf, '\0', sizeof(smi->buf)); |
| 1178 | |
| 1179 | err = smi->ops->get_vlan_4k(smi, val->port_vlan, &vlan4k); |
| 1180 | if (err) |
| 1181 | return err; |
| 1182 | |
| 1183 | len += snprintf(buf + len, sizeof(smi->buf) - len, |
| 1184 | "VLAN %d: Ports: '", vlan4k.vid); |
| 1185 | |
| 1186 | for (i = 0; i < smi->num_ports; i++) { |
| 1187 | if (!(vlan4k.member & (1 << i))) |
| 1188 | continue; |
| 1189 | |
| 1190 | len += snprintf(buf + len, sizeof(smi->buf) - len, "%d%s", i, |
| 1191 | (vlan4k.untag & (1 << i)) ? "" : "t"); |
| 1192 | } |
| 1193 | |
| 1194 | len += snprintf(buf + len, sizeof(smi->buf) - len, |
| 1195 | "', members=%04x, untag=%04x, fid=%u", |
| 1196 | vlan4k.member, vlan4k.untag, vlan4k.fid); |
| 1197 | |
| 1198 | val->value.s = buf; |
| 1199 | val->len = len; |
| 1200 | |
| 1201 | return 0; |
| 1202 | } |
| 1203 | EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_info); |
| 1204 | |
| 1205 | int rtl8366_sw_get_vlan_ports(struct switch_dev *dev, struct switch_val *val) |
| 1206 | { |
| 1207 | struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev); |
| 1208 | struct switch_port *port; |
| 1209 | struct rtl8366_vlan_4k vlan4k; |
| 1210 | int i; |
| 1211 | |
| 1212 | if (!smi->ops->is_vlan_valid(smi, val->port_vlan)) |
| 1213 | return -EINVAL; |
| 1214 | |
| 1215 | smi->ops->get_vlan_4k(smi, val->port_vlan, &vlan4k); |
| 1216 | |
| 1217 | port = &val->value.ports[0]; |
| 1218 | val->len = 0; |
| 1219 | for (i = 0; i < smi->num_ports; i++) { |
| 1220 | if (!(vlan4k.member & BIT(i))) |
| 1221 | continue; |
| 1222 | |
| 1223 | port->id = i; |
| 1224 | port->flags = (vlan4k.untag & BIT(i)) ? |
| 1225 | 0 : BIT(SWITCH_PORT_FLAG_TAGGED); |
| 1226 | val->len++; |
| 1227 | port++; |
| 1228 | } |
| 1229 | return 0; |
| 1230 | } |
| 1231 | EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_ports); |
| 1232 | |
| 1233 | int rtl8366_sw_set_vlan_ports(struct switch_dev *dev, struct switch_val *val) |
| 1234 | { |
| 1235 | struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev); |
| 1236 | struct switch_port *port; |
| 1237 | u32 member = 0; |
| 1238 | u32 untag = 0; |
| 1239 | int err; |
| 1240 | int i; |
| 1241 | |
| 1242 | if (!smi->ops->is_vlan_valid(smi, val->port_vlan)) |
| 1243 | return -EINVAL; |
| 1244 | |
| 1245 | port = &val->value.ports[0]; |
| 1246 | for (i = 0; i < val->len; i++, port++) { |
| 1247 | int pvid = 0; |
| 1248 | member |= BIT(port->id); |
| 1249 | |
| 1250 | if (!(port->flags & BIT(SWITCH_PORT_FLAG_TAGGED))) |
| 1251 | untag |= BIT(port->id); |
| 1252 | |
| 1253 | /* |
| 1254 | * To ensure that we have a valid MC entry for this VLAN, |
| 1255 | * initialize the port VLAN ID here. |
| 1256 | */ |
| 1257 | err = rtl8366_get_pvid(smi, port->id, &pvid); |
| 1258 | if (err < 0) |
| 1259 | return err; |
| 1260 | if (pvid == 0) { |
| 1261 | err = rtl8366_set_pvid(smi, port->id, val->port_vlan); |
| 1262 | if (err < 0) |
| 1263 | return err; |
| 1264 | } |
| 1265 | } |
| 1266 | |
| 1267 | return rtl8366_set_vlan(smi, val->port_vlan, member, untag, 0); |
| 1268 | } |
| 1269 | EXPORT_SYMBOL_GPL(rtl8366_sw_set_vlan_ports); |
| 1270 | |
| 1271 | int rtl8366_sw_get_vlan_fid(struct switch_dev *dev, |
| 1272 | const struct switch_attr *attr, |
| 1273 | struct switch_val *val) |
| 1274 | { |
| 1275 | struct rtl8366_vlan_4k vlan4k; |
| 1276 | struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev); |
| 1277 | int err; |
| 1278 | |
| 1279 | if (!smi->ops->is_vlan_valid(smi, val->port_vlan)) |
| 1280 | return -EINVAL; |
| 1281 | |
| 1282 | err = smi->ops->get_vlan_4k(smi, val->port_vlan, &vlan4k); |
| 1283 | if (err) |
| 1284 | return err; |
| 1285 | |
| 1286 | val->value.i = vlan4k.fid; |
| 1287 | |
| 1288 | return 0; |
| 1289 | } |
| 1290 | EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_fid); |
| 1291 | |
| 1292 | int rtl8366_sw_set_vlan_fid(struct switch_dev *dev, |
| 1293 | const struct switch_attr *attr, |
| 1294 | struct switch_val *val) |
| 1295 | { |
| 1296 | struct rtl8366_vlan_4k vlan4k; |
| 1297 | struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev); |
| 1298 | int err; |
| 1299 | |
| 1300 | if (!smi->ops->is_vlan_valid(smi, val->port_vlan)) |
| 1301 | return -EINVAL; |
| 1302 | |
| 1303 | if (val->value.i < 0 || val->value.i > attr->max) |
| 1304 | return -EINVAL; |
| 1305 | |
| 1306 | err = smi->ops->get_vlan_4k(smi, val->port_vlan, &vlan4k); |
| 1307 | if (err) |
| 1308 | return err; |
| 1309 | |
| 1310 | return rtl8366_set_vlan(smi, val->port_vlan, |
| 1311 | vlan4k.member, |
| 1312 | vlan4k.untag, |
| 1313 | val->value.i); |
| 1314 | } |
| 1315 | EXPORT_SYMBOL_GPL(rtl8366_sw_set_vlan_fid); |
| 1316 | |
| 1317 | int rtl8366_sw_get_vlan_enable(struct switch_dev *dev, |
| 1318 | const struct switch_attr *attr, |
| 1319 | struct switch_val *val) |
| 1320 | { |
| 1321 | struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev); |
| 1322 | |
| 1323 | if (attr->ofs > 2) |
| 1324 | return -EINVAL; |
| 1325 | |
| 1326 | if (attr->ofs == 1) |
| 1327 | val->value.i = smi->vlan_enabled; |
| 1328 | else |
| 1329 | val->value.i = smi->vlan4k_enabled; |
| 1330 | |
| 1331 | return 0; |
| 1332 | } |
| 1333 | EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_enable); |
| 1334 | |
| 1335 | int rtl8366_sw_set_vlan_enable(struct switch_dev *dev, |
| 1336 | const struct switch_attr *attr, |
| 1337 | struct switch_val *val) |
| 1338 | { |
| 1339 | struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev); |
| 1340 | int err; |
| 1341 | |
| 1342 | if (attr->ofs > 2) |
| 1343 | return -EINVAL; |
| 1344 | |
| 1345 | if (attr->ofs == 1) |
| 1346 | err = rtl8366_enable_vlan(smi, val->value.i); |
| 1347 | else |
| 1348 | err = rtl8366_enable_vlan4k(smi, val->value.i); |
| 1349 | |
| 1350 | return err; |
| 1351 | } |
| 1352 | EXPORT_SYMBOL_GPL(rtl8366_sw_set_vlan_enable); |
| 1353 | |
| 1354 | struct rtl8366_smi *rtl8366_smi_alloc(struct device *parent) |
| 1355 | { |
| 1356 | struct rtl8366_smi *smi; |
| 1357 | |
| 1358 | BUG_ON(!parent); |
| 1359 | |
| 1360 | smi = kzalloc(sizeof(*smi), GFP_KERNEL); |
| 1361 | if (!smi) { |
| 1362 | dev_err(parent, "no memory for private data\n"); |
| 1363 | return NULL; |
| 1364 | } |
| 1365 | |
| 1366 | smi->parent = parent; |
| 1367 | return smi; |
| 1368 | } |
| 1369 | EXPORT_SYMBOL_GPL(rtl8366_smi_alloc); |
| 1370 | |
| 1371 | static int __rtl8366_smi_init(struct rtl8366_smi *smi, const char *name) |
| 1372 | { |
| 1373 | int err; |
| 1374 | |
| 1375 | if (!smi->ext_mbus) { |
| 1376 | err = gpio_request(smi->gpio_sda, name); |
| 1377 | if (err) { |
| 1378 | printk(KERN_ERR "rtl8366_smi: gpio_request failed for %u, err=%d\n", |
| 1379 | smi->gpio_sda, err); |
| 1380 | goto err_out; |
| 1381 | } |
| 1382 | |
| 1383 | err = gpio_request(smi->gpio_sck, name); |
| 1384 | if (err) { |
| 1385 | printk(KERN_ERR "rtl8366_smi: gpio_request failed for %u, err=%d\n", |
| 1386 | smi->gpio_sck, err); |
| 1387 | goto err_free_sda; |
| 1388 | } |
| 1389 | } |
| 1390 | |
| 1391 | spin_lock_init(&smi->lock); |
| 1392 | |
| 1393 | /* start the switch */ |
| 1394 | if (smi->hw_reset) { |
| 1395 | smi->hw_reset(smi, false); |
| 1396 | msleep(RTL8366_SMI_HW_START_DELAY); |
| 1397 | } |
| 1398 | |
| 1399 | return 0; |
| 1400 | |
| 1401 | err_free_sda: |
| 1402 | gpio_free(smi->gpio_sda); |
| 1403 | err_out: |
| 1404 | return err; |
| 1405 | } |
| 1406 | |
| 1407 | static void __rtl8366_smi_cleanup(struct rtl8366_smi *smi) |
| 1408 | { |
| 1409 | if (smi->hw_reset) |
| 1410 | smi->hw_reset(smi, true); |
| 1411 | |
| 1412 | if (!smi->ext_mbus) { |
| 1413 | gpio_free(smi->gpio_sck); |
| 1414 | gpio_free(smi->gpio_sda); |
| 1415 | } |
| 1416 | } |
| 1417 | |
| 1418 | enum rtl8366_type rtl8366_smi_detect(struct rtl8366_platform_data *pdata) |
| 1419 | { |
| 1420 | static struct rtl8366_smi smi; |
| 1421 | enum rtl8366_type type = RTL8366_TYPE_UNKNOWN; |
| 1422 | u32 reg = 0; |
| 1423 | |
| 1424 | memset(&smi, 0, sizeof(smi)); |
| 1425 | smi.gpio_sda = pdata->gpio_sda; |
| 1426 | smi.gpio_sck = pdata->gpio_sck; |
| 1427 | smi.clk_delay = 10; |
| 1428 | smi.cmd_read = 0xa9; |
| 1429 | smi.cmd_write = 0xa8; |
| 1430 | |
| 1431 | if (__rtl8366_smi_init(&smi, "rtl8366")) |
| 1432 | goto out; |
| 1433 | |
| 1434 | if (rtl8366_smi_read_reg(&smi, 0x5c, ®)) |
| 1435 | goto cleanup; |
| 1436 | |
| 1437 | switch(reg) { |
| 1438 | case 0x6027: |
| 1439 | printk("Found an RTL8366S switch\n"); |
| 1440 | type = RTL8366_TYPE_S; |
| 1441 | break; |
| 1442 | case 0x5937: |
| 1443 | printk("Found an RTL8366RB switch\n"); |
| 1444 | type = RTL8366_TYPE_RB; |
| 1445 | break; |
| 1446 | default: |
| 1447 | printk("Found an Unknown RTL8366 switch (id=0x%04x)\n", reg); |
| 1448 | break; |
| 1449 | } |
| 1450 | |
| 1451 | cleanup: |
| 1452 | __rtl8366_smi_cleanup(&smi); |
| 1453 | out: |
| 1454 | return type; |
| 1455 | } |
| 1456 | |
| 1457 | int rtl8366_smi_init(struct rtl8366_smi *smi) |
| 1458 | { |
| 1459 | int err; |
| 1460 | |
| 1461 | if (!smi->ops) |
| 1462 | return -EINVAL; |
| 1463 | |
| 1464 | err = __rtl8366_smi_init(smi, dev_name(smi->parent)); |
| 1465 | if (err) |
| 1466 | goto err_out; |
| 1467 | |
| 1468 | if (!smi->ext_mbus) |
| 1469 | dev_info(smi->parent, "using GPIO pins %u (SDA) and %u (SCK)\n", |
| 1470 | smi->gpio_sda, smi->gpio_sck); |
| 1471 | else |
| 1472 | dev_info(smi->parent, "using MDIO bus '%s'\n", smi->ext_mbus->name); |
| 1473 | |
| 1474 | err = smi->ops->detect(smi); |
| 1475 | if (err) { |
| 1476 | dev_err(smi->parent, "chip detection failed, err=%d\n", err); |
| 1477 | goto err_free_sck; |
| 1478 | } |
| 1479 | |
| 1480 | err = rtl8366_reset(smi); |
| 1481 | if (err) |
| 1482 | goto err_free_sck; |
| 1483 | |
| 1484 | err = smi->ops->setup(smi); |
| 1485 | if (err) { |
| 1486 | dev_err(smi->parent, "chip setup failed, err=%d\n", err); |
| 1487 | goto err_free_sck; |
| 1488 | } |
| 1489 | |
| 1490 | err = rtl8366_init_vlan(smi); |
| 1491 | if (err) { |
| 1492 | dev_err(smi->parent, "VLAN initialization failed, err=%d\n", |
| 1493 | err); |
| 1494 | goto err_free_sck; |
| 1495 | } |
| 1496 | |
| 1497 | err = rtl8366_enable_all_ports(smi, 1); |
| 1498 | if (err) |
| 1499 | goto err_free_sck; |
| 1500 | |
| 1501 | err = rtl8366_smi_mii_init(smi); |
| 1502 | if (err) |
| 1503 | goto err_free_sck; |
| 1504 | |
| 1505 | rtl8366_debugfs_init(smi); |
| 1506 | |
| 1507 | return 0; |
| 1508 | |
| 1509 | err_free_sck: |
| 1510 | __rtl8366_smi_cleanup(smi); |
| 1511 | err_out: |
| 1512 | return err; |
| 1513 | } |
| 1514 | EXPORT_SYMBOL_GPL(rtl8366_smi_init); |
| 1515 | |
| 1516 | void rtl8366_smi_cleanup(struct rtl8366_smi *smi) |
| 1517 | { |
| 1518 | rtl8366_debugfs_remove(smi); |
| 1519 | rtl8366_smi_mii_cleanup(smi); |
| 1520 | __rtl8366_smi_cleanup(smi); |
| 1521 | } |
| 1522 | EXPORT_SYMBOL_GPL(rtl8366_smi_cleanup); |
| 1523 | |
| 1524 | #ifdef CONFIG_OF |
| 1525 | static void rtl8366_smi_reset(struct rtl8366_smi *smi, bool active) |
| 1526 | { |
| 1527 | if (active) |
| 1528 | reset_control_assert(smi->reset); |
| 1529 | else |
| 1530 | reset_control_deassert(smi->reset); |
| 1531 | } |
| 1532 | |
| 1533 | int rtl8366_smi_probe_of(struct platform_device *pdev, struct rtl8366_smi *smi) |
| 1534 | { |
| 1535 | int sck = of_get_named_gpio(pdev->dev.of_node, "gpio-sck", 0); |
| 1536 | int sda = of_get_named_gpio(pdev->dev.of_node, "gpio-sda", 0); |
| 1537 | struct device_node *np = pdev->dev.of_node; |
| 1538 | struct device_node *mdio_node; |
| 1539 | |
| 1540 | mdio_node = of_parse_phandle(np, "mii-bus", 0); |
| 1541 | if (!mdio_node) { |
| 1542 | dev_err(&pdev->dev, "cannot find mdio node phandle"); |
| 1543 | goto try_gpio; |
| 1544 | } |
| 1545 | |
| 1546 | smi->ext_mbus = of_mdio_find_bus(mdio_node); |
| 1547 | if (!smi->ext_mbus) { |
| 1548 | dev_info(&pdev->dev, |
| 1549 | "cannot find mdio bus from bus handle (yet)"); |
| 1550 | goto try_gpio; |
| 1551 | } |
| 1552 | |
| 1553 | return 0; |
| 1554 | |
| 1555 | try_gpio: |
| 1556 | if (!gpio_is_valid(sck) || !gpio_is_valid(sda)) { |
| 1557 | if (!mdio_node) { |
| 1558 | dev_err(&pdev->dev, "gpios missing in devictree\n"); |
| 1559 | return -EINVAL; |
| 1560 | } else { |
| 1561 | return -EPROBE_DEFER; |
| 1562 | } |
| 1563 | } |
| 1564 | |
| 1565 | smi->gpio_sda = sda; |
| 1566 | smi->gpio_sck = sck; |
| 1567 | smi->reset = devm_reset_control_get(&pdev->dev, "switch"); |
| 1568 | if (!IS_ERR(smi->reset)) |
| 1569 | smi->hw_reset = rtl8366_smi_reset; |
| 1570 | |
| 1571 | return 0; |
| 1572 | } |
| 1573 | #else |
| 1574 | static inline int rtl8366_smi_probe_of(struct platform_device *pdev, struct rtl8366_smi *smi) |
| 1575 | { |
| 1576 | return -ENODEV; |
| 1577 | } |
| 1578 | #endif |
| 1579 | |
| 1580 | int rtl8366_smi_probe_plat(struct platform_device *pdev, struct rtl8366_smi *smi) |
| 1581 | { |
| 1582 | struct rtl8366_platform_data *pdata = pdev->dev.platform_data; |
| 1583 | |
| 1584 | if (!pdev->dev.platform_data) { |
| 1585 | dev_err(&pdev->dev, "no platform data specified\n"); |
| 1586 | return -EINVAL; |
| 1587 | } |
| 1588 | |
| 1589 | smi->gpio_sda = pdata->gpio_sda; |
| 1590 | smi->gpio_sck = pdata->gpio_sck; |
| 1591 | smi->hw_reset = pdata->hw_reset; |
| 1592 | |
| 1593 | return 0; |
| 1594 | } |
| 1595 | |
| 1596 | |
| 1597 | struct rtl8366_smi *rtl8366_smi_probe(struct platform_device *pdev) |
| 1598 | { |
| 1599 | struct rtl8366_smi *smi; |
| 1600 | int err; |
| 1601 | |
| 1602 | smi = rtl8366_smi_alloc(&pdev->dev); |
| 1603 | if (!smi) |
| 1604 | return NULL; |
| 1605 | |
| 1606 | if (pdev->dev.of_node) |
| 1607 | err = rtl8366_smi_probe_of(pdev, smi); |
| 1608 | else |
| 1609 | err = rtl8366_smi_probe_plat(pdev, smi); |
| 1610 | |
| 1611 | if (err) |
| 1612 | goto free_smi; |
| 1613 | |
| 1614 | return smi; |
| 1615 | |
| 1616 | free_smi: |
| 1617 | kfree(smi); |
| 1618 | return ERR_PTR(err); |
| 1619 | } |
| 1620 | EXPORT_SYMBOL_GPL(rtl8366_smi_probe); |
| 1621 | |
| 1622 | MODULE_DESCRIPTION("Realtek RTL8366 SMI interface driver"); |
| 1623 | MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>"); |
| 1624 | MODULE_LICENSE("GPL v2"); |