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