| 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 | |
| 19 | #ifdef CONFIG_RTL8366S_PHY_DEBUG_FS |
| 20 | #include <linux/debugfs.h> |
| 21 | #endif |
| 22 | |
| 23 | #include "rtl8366_smi.h" |
| 24 | |
| 25 | #define RTL8366_SMI_ACK_RETRY_COUNT 5 |
| 26 | #define RTL8366_SMI_CLK_DELAY 10 /* nsec */ |
| 27 | |
| 28 | static inline void rtl8366_smi_clk_delay(struct rtl8366_smi *smi) |
| 29 | { |
| 30 | ndelay(RTL8366_SMI_CLK_DELAY); |
| 31 | } |
| 32 | |
| 33 | static void rtl8366_smi_start(struct rtl8366_smi *smi) |
| 34 | { |
| 35 | unsigned int sda = smi->gpio_sda; |
| 36 | unsigned int sck = smi->gpio_sck; |
| 37 | |
| 38 | /* |
| 39 | * Set GPIO pins to output mode, with initial state: |
| 40 | * SCK = 0, SDA = 1 |
| 41 | */ |
| 42 | gpio_direction_output(sck, 0); |
| 43 | gpio_direction_output(sda, 1); |
| 44 | rtl8366_smi_clk_delay(smi); |
| 45 | |
| 46 | /* CLK 1: 0 -> 1, 1 -> 0 */ |
| 47 | gpio_set_value(sck, 1); |
| 48 | rtl8366_smi_clk_delay(smi); |
| 49 | gpio_set_value(sck, 0); |
| 50 | rtl8366_smi_clk_delay(smi); |
| 51 | |
| 52 | /* CLK 2: */ |
| 53 | gpio_set_value(sck, 1); |
| 54 | rtl8366_smi_clk_delay(smi); |
| 55 | gpio_set_value(sda, 0); |
| 56 | rtl8366_smi_clk_delay(smi); |
| 57 | gpio_set_value(sck, 0); |
| 58 | rtl8366_smi_clk_delay(smi); |
| 59 | gpio_set_value(sda, 1); |
| 60 | } |
| 61 | |
| 62 | static void rtl8366_smi_stop(struct rtl8366_smi *smi) |
| 63 | { |
| 64 | unsigned int sda = smi->gpio_sda; |
| 65 | unsigned int sck = smi->gpio_sck; |
| 66 | |
| 67 | rtl8366_smi_clk_delay(smi); |
| 68 | gpio_set_value(sda, 0); |
| 69 | gpio_set_value(sck, 1); |
| 70 | rtl8366_smi_clk_delay(smi); |
| 71 | gpio_set_value(sda, 1); |
| 72 | rtl8366_smi_clk_delay(smi); |
| 73 | gpio_set_value(sck, 1); |
| 74 | rtl8366_smi_clk_delay(smi); |
| 75 | gpio_set_value(sck, 0); |
| 76 | rtl8366_smi_clk_delay(smi); |
| 77 | gpio_set_value(sck, 1); |
| 78 | |
| 79 | /* add a click */ |
| 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 | /* set GPIO pins to input mode */ |
| 86 | gpio_direction_input(sda); |
| 87 | gpio_direction_input(sck); |
| 88 | } |
| 89 | |
| 90 | static void rtl8366_smi_write_bits(struct rtl8366_smi *smi, u32 data, u32 len) |
| 91 | { |
| 92 | unsigned int sda = smi->gpio_sda; |
| 93 | unsigned int sck = smi->gpio_sck; |
| 94 | |
| 95 | for (; len > 0; len--) { |
| 96 | rtl8366_smi_clk_delay(smi); |
| 97 | |
| 98 | /* prepare data */ |
| 99 | gpio_set_value(sda, !!(data & ( 1 << (len - 1)))); |
| 100 | rtl8366_smi_clk_delay(smi); |
| 101 | |
| 102 | /* clocking */ |
| 103 | gpio_set_value(sck, 1); |
| 104 | rtl8366_smi_clk_delay(smi); |
| 105 | gpio_set_value(sck, 0); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | static void rtl8366_smi_read_bits(struct rtl8366_smi *smi, u32 len, u32 *data) |
| 110 | { |
| 111 | unsigned int sda = smi->gpio_sda; |
| 112 | unsigned int sck = smi->gpio_sck; |
| 113 | |
| 114 | gpio_direction_input(sda); |
| 115 | |
| 116 | for (*data = 0; len > 0; len--) { |
| 117 | u32 u; |
| 118 | |
| 119 | rtl8366_smi_clk_delay(smi); |
| 120 | |
| 121 | /* clocking */ |
| 122 | gpio_set_value(sck, 1); |
| 123 | rtl8366_smi_clk_delay(smi); |
| 124 | u = !!gpio_get_value(sda); |
| 125 | gpio_set_value(sck, 0); |
| 126 | |
| 127 | *data |= (u << (len - 1)); |
| 128 | } |
| 129 | |
| 130 | gpio_direction_output(sda, 0); |
| 131 | } |
| 132 | |
| 133 | static int rtl8366_smi_wait_for_ack(struct rtl8366_smi *smi) |
| 134 | { |
| 135 | int retry_cnt; |
| 136 | |
| 137 | retry_cnt = 0; |
| 138 | do { |
| 139 | u32 ack; |
| 140 | |
| 141 | rtl8366_smi_read_bits(smi, 1, &ack); |
| 142 | if (ack == 0) |
| 143 | break; |
| 144 | |
| 145 | if (++retry_cnt > RTL8366_SMI_ACK_RETRY_COUNT) |
| 146 | return -EIO; |
| 147 | } while (1); |
| 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | static int rtl8366_smi_write_byte(struct rtl8366_smi *smi, u8 data) |
| 153 | { |
| 154 | rtl8366_smi_write_bits(smi, data, 8); |
| 155 | return rtl8366_smi_wait_for_ack(smi); |
| 156 | } |
| 157 | |
| 158 | static int rtl8366_smi_read_byte0(struct rtl8366_smi *smi, u8 *data) |
| 159 | { |
| 160 | u32 t; |
| 161 | |
| 162 | /* read data */ |
| 163 | rtl8366_smi_read_bits(smi, 8, &t); |
| 164 | *data = (t & 0xff); |
| 165 | |
| 166 | /* send an ACK */ |
| 167 | rtl8366_smi_write_bits(smi, 0x00, 1); |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | static int rtl8366_smi_read_byte1(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, 0x01, 1); |
| 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | int rtl8366_smi_read_reg(struct rtl8366_smi *smi, u32 addr, u32 *data) |
| 187 | { |
| 188 | unsigned long flags; |
| 189 | u8 lo = 0; |
| 190 | u8 hi = 0; |
| 191 | int ret; |
| 192 | |
| 193 | spin_lock_irqsave(&smi->lock, flags); |
| 194 | |
| 195 | rtl8366_smi_start(smi); |
| 196 | |
| 197 | /* send READ command */ |
| 198 | ret = rtl8366_smi_write_byte(smi, 0x0a << 4 | 0x04 << 1 | 0x01); |
| 199 | if (ret) |
| 200 | goto out; |
| 201 | |
| 202 | /* set ADDR[7:0] */ |
| 203 | ret = rtl8366_smi_write_byte(smi, addr & 0xff); |
| 204 | if (ret) |
| 205 | goto out; |
| 206 | |
| 207 | /* set ADDR[15:8] */ |
| 208 | ret = rtl8366_smi_write_byte(smi, addr >> 8); |
| 209 | if (ret) |
| 210 | goto out; |
| 211 | |
| 212 | /* read DATA[7:0] */ |
| 213 | rtl8366_smi_read_byte0(smi, &lo); |
| 214 | /* read DATA[15:8] */ |
| 215 | rtl8366_smi_read_byte1(smi, &hi); |
| 216 | |
| 217 | *data = ((u32) lo) | (((u32) hi) << 8); |
| 218 | |
| 219 | ret = 0; |
| 220 | |
| 221 | out: |
| 222 | rtl8366_smi_stop(smi); |
| 223 | spin_unlock_irqrestore(&smi->lock, flags); |
| 224 | |
| 225 | return ret; |
| 226 | } |
| 227 | EXPORT_SYMBOL_GPL(rtl8366_smi_read_reg); |
| 228 | |
| 229 | int rtl8366_smi_write_reg(struct rtl8366_smi *smi, u32 addr, u32 data) |
| 230 | { |
| 231 | unsigned long flags; |
| 232 | int ret; |
| 233 | |
| 234 | spin_lock_irqsave(&smi->lock, flags); |
| 235 | |
| 236 | rtl8366_smi_start(smi); |
| 237 | |
| 238 | /* send WRITE command */ |
| 239 | ret = rtl8366_smi_write_byte(smi, 0x0a << 4 | 0x04 << 1 | 0x00); |
| 240 | if (ret) |
| 241 | goto out; |
| 242 | |
| 243 | /* set ADDR[7:0] */ |
| 244 | ret = rtl8366_smi_write_byte(smi, addr & 0xff); |
| 245 | if (ret) |
| 246 | goto out; |
| 247 | |
| 248 | /* set ADDR[15:8] */ |
| 249 | ret = rtl8366_smi_write_byte(smi, addr >> 8); |
| 250 | if (ret) |
| 251 | goto out; |
| 252 | |
| 253 | /* write DATA[7:0] */ |
| 254 | ret = rtl8366_smi_write_byte(smi, data & 0xff); |
| 255 | if (ret) |
| 256 | goto out; |
| 257 | |
| 258 | /* write DATA[15:8] */ |
| 259 | ret = rtl8366_smi_write_byte(smi, data >> 8); |
| 260 | if (ret) |
| 261 | goto out; |
| 262 | |
| 263 | ret = 0; |
| 264 | |
| 265 | out: |
| 266 | rtl8366_smi_stop(smi); |
| 267 | spin_unlock_irqrestore(&smi->lock, flags); |
| 268 | |
| 269 | return ret; |
| 270 | } |
| 271 | EXPORT_SYMBOL_GPL(rtl8366_smi_write_reg); |
| 272 | |
| 273 | int rtl8366_smi_rmwr(struct rtl8366_smi *smi, u32 addr, u32 mask, u32 data) |
| 274 | { |
| 275 | u32 t; |
| 276 | int err; |
| 277 | |
| 278 | err = rtl8366_smi_read_reg(smi, addr, &t); |
| 279 | if (err) |
| 280 | return err; |
| 281 | |
| 282 | err = rtl8366_smi_write_reg(smi, addr, (t & ~mask) | data); |
| 283 | return err; |
| 284 | |
| 285 | } |
| 286 | EXPORT_SYMBOL_GPL(rtl8366_smi_rmwr); |
| 287 | |
| 288 | static int rtl8366_mc_is_used(struct rtl8366_smi *smi, int mc_index, int *used) |
| 289 | { |
| 290 | int err; |
| 291 | int i; |
| 292 | |
| 293 | *used = 0; |
| 294 | for (i = 0; i < smi->num_ports; i++) { |
| 295 | int index = 0; |
| 296 | |
| 297 | err = smi->ops->get_mc_index(smi, i, &index); |
| 298 | if (err) |
| 299 | return err; |
| 300 | |
| 301 | if (mc_index == index) { |
| 302 | *used = 1; |
| 303 | break; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | static int rtl8366_set_vlan(struct rtl8366_smi *smi, int vid, u32 member, |
| 311 | u32 untag, u32 fid) |
| 312 | { |
| 313 | struct rtl8366_vlan_4k vlan4k; |
| 314 | int err; |
| 315 | int i; |
| 316 | |
| 317 | /* Update the 4K table */ |
| 318 | err = smi->ops->get_vlan_4k(smi, vid, &vlan4k); |
| 319 | if (err) |
| 320 | return err; |
| 321 | |
| 322 | vlan4k.member = member; |
| 323 | vlan4k.untag = untag; |
| 324 | vlan4k.fid = fid; |
| 325 | err = smi->ops->set_vlan_4k(smi, &vlan4k); |
| 326 | if (err) |
| 327 | return err; |
| 328 | |
| 329 | /* Try to find an existing MC entry for this VID */ |
| 330 | for (i = 0; i < smi->num_vlan_mc; i++) { |
| 331 | struct rtl8366_vlan_mc vlanmc; |
| 332 | |
| 333 | err = smi->ops->get_vlan_mc(smi, i, &vlanmc); |
| 334 | if (err) |
| 335 | return err; |
| 336 | |
| 337 | if (vid == vlanmc.vid) { |
| 338 | /* update the MC entry */ |
| 339 | vlanmc.member = member; |
| 340 | vlanmc.untag = untag; |
| 341 | vlanmc.fid = fid; |
| 342 | |
| 343 | err = smi->ops->set_vlan_mc(smi, i, &vlanmc); |
| 344 | break; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | return err; |
| 349 | } |
| 350 | |
| 351 | static int rtl8366_get_pvid(struct rtl8366_smi *smi, int port, int *val) |
| 352 | { |
| 353 | struct rtl8366_vlan_mc vlanmc; |
| 354 | int err; |
| 355 | int index; |
| 356 | |
| 357 | err = smi->ops->get_mc_index(smi, port, &index); |
| 358 | if (err) |
| 359 | return err; |
| 360 | |
| 361 | err = smi->ops->get_vlan_mc(smi, index, &vlanmc); |
| 362 | if (err) |
| 363 | return err; |
| 364 | |
| 365 | *val = vlanmc.vid; |
| 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | static int rtl8366_set_pvid(struct rtl8366_smi *smi, unsigned port, |
| 370 | unsigned vid) |
| 371 | { |
| 372 | struct rtl8366_vlan_mc vlanmc; |
| 373 | struct rtl8366_vlan_4k vlan4k; |
| 374 | int err; |
| 375 | int i; |
| 376 | |
| 377 | /* Try to find an existing MC entry for this VID */ |
| 378 | for (i = 0; i < smi->num_vlan_mc; i++) { |
| 379 | err = smi->ops->get_vlan_mc(smi, i, &vlanmc); |
| 380 | if (err) |
| 381 | return err; |
| 382 | |
| 383 | if (vid == vlanmc.vid) { |
| 384 | err = smi->ops->set_vlan_mc(smi, i, &vlanmc); |
| 385 | if (err) |
| 386 | return err; |
| 387 | |
| 388 | err = smi->ops->set_mc_index(smi, port, i); |
| 389 | return err; |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | /* We have no MC entry for this VID, try to find an empty one */ |
| 394 | for (i = 0; i < smi->num_vlan_mc; i++) { |
| 395 | err = smi->ops->get_vlan_mc(smi, i, &vlanmc); |
| 396 | if (err) |
| 397 | return err; |
| 398 | |
| 399 | if (vlanmc.vid == 0 && vlanmc.member == 0) { |
| 400 | /* Update the entry from the 4K table */ |
| 401 | err = smi->ops->get_vlan_4k(smi, vid, &vlan4k); |
| 402 | if (err) |
| 403 | return err; |
| 404 | |
| 405 | vlanmc.vid = vid; |
| 406 | vlanmc.member = vlan4k.member; |
| 407 | vlanmc.untag = vlan4k.untag; |
| 408 | vlanmc.fid = vlan4k.fid; |
| 409 | err = smi->ops->set_vlan_mc(smi, i, &vlanmc); |
| 410 | if (err) |
| 411 | return err; |
| 412 | |
| 413 | err = smi->ops->set_mc_index(smi, port, i); |
| 414 | return err; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | /* MC table is full, try to find an unused entry and replace it */ |
| 419 | for (i = 0; i < smi->num_vlan_mc; i++) { |
| 420 | int used; |
| 421 | |
| 422 | err = rtl8366_mc_is_used(smi, i, &used); |
| 423 | if (err) |
| 424 | return err; |
| 425 | |
| 426 | if (!used) { |
| 427 | /* Update the entry from the 4K table */ |
| 428 | err = smi->ops->get_vlan_4k(smi, vid, &vlan4k); |
| 429 | if (err) |
| 430 | return err; |
| 431 | |
| 432 | vlanmc.vid = vid; |
| 433 | vlanmc.member = vlan4k.member; |
| 434 | vlanmc.untag = vlan4k.untag; |
| 435 | vlanmc.fid = vlan4k.fid; |
| 436 | err = smi->ops->set_vlan_mc(smi, i, &vlanmc); |
| 437 | if (err) |
| 438 | return err; |
| 439 | |
| 440 | err = smi->ops->set_mc_index(smi, port, i); |
| 441 | return err; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | dev_err(smi->parent, |
| 446 | "all VLAN member configurations are in use\n"); |
| 447 | |
| 448 | return -ENOSPC; |
| 449 | } |
| 450 | |
| 451 | static int rtl8366_enable_vlan(struct rtl8366_smi *smi, int enable) |
| 452 | { |
| 453 | int err; |
| 454 | |
| 455 | err = smi->ops->enable_vlan(smi, enable); |
| 456 | if (err) |
| 457 | return err; |
| 458 | |
| 459 | smi->vlan_enabled = enable; |
| 460 | |
| 461 | if (!enable) { |
| 462 | smi->vlan4k_enabled = 0; |
| 463 | err = smi->ops->enable_vlan4k(smi, enable); |
| 464 | } |
| 465 | |
| 466 | return err; |
| 467 | } |
| 468 | |
| 469 | static int rtl8366_enable_vlan4k(struct rtl8366_smi *smi, int enable) |
| 470 | { |
| 471 | int err; |
| 472 | |
| 473 | if (enable) { |
| 474 | err = smi->ops->enable_vlan(smi, enable); |
| 475 | if (err) |
| 476 | return err; |
| 477 | |
| 478 | smi->vlan_enabled = enable; |
| 479 | } |
| 480 | |
| 481 | err = smi->ops->enable_vlan4k(smi, enable); |
| 482 | if (err) |
| 483 | return err; |
| 484 | |
| 485 | smi->vlan4k_enabled = enable; |
| 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | int rtl8366_reset_vlan(struct rtl8366_smi *smi) |
| 490 | { |
| 491 | struct rtl8366_vlan_mc vlanmc; |
| 492 | int err; |
| 493 | int i; |
| 494 | |
| 495 | rtl8366_enable_vlan(smi, 0); |
| 496 | rtl8366_enable_vlan4k(smi, 0); |
| 497 | |
| 498 | /* clear VLAN member configurations */ |
| 499 | vlanmc.vid = 0; |
| 500 | vlanmc.priority = 0; |
| 501 | vlanmc.member = 0; |
| 502 | vlanmc.untag = 0; |
| 503 | vlanmc.fid = 0; |
| 504 | for (i = 0; i < smi->num_vlan_mc; i++) { |
| 505 | err = smi->ops->set_vlan_mc(smi, i, &vlanmc); |
| 506 | if (err) |
| 507 | return err; |
| 508 | } |
| 509 | |
| 510 | for (i = 0; i < smi->num_ports; i++) { |
| 511 | if (i == smi->cpu_port) |
| 512 | continue; |
| 513 | |
| 514 | err = rtl8366_set_vlan(smi, (i + 1), |
| 515 | (1 << i) | (1 << smi->cpu_port), |
| 516 | (1 << i) | (1 << smi->cpu_port), |
| 517 | 0); |
| 518 | if (err) |
| 519 | return err; |
| 520 | |
| 521 | err = rtl8366_set_pvid(smi, i, (i + 1)); |
| 522 | if (err) |
| 523 | return err; |
| 524 | } |
| 525 | |
| 526 | return 0; |
| 527 | } |
| 528 | EXPORT_SYMBOL_GPL(rtl8366_reset_vlan); |
| 529 | |
| 530 | #ifdef CONFIG_RTL8366S_PHY_DEBUG_FS |
| 531 | int rtl8366_debugfs_open(struct inode *inode, struct file *file) |
| 532 | { |
| 533 | file->private_data = inode->i_private; |
| 534 | return 0; |
| 535 | } |
| 536 | EXPORT_SYMBOL_GPL(rtl8366_debugfs_open); |
| 537 | |
| 538 | static ssize_t rtl8366_read_debugfs_vlan_mc(struct file *file, |
| 539 | char __user *user_buf, |
| 540 | size_t count, loff_t *ppos) |
| 541 | { |
| 542 | struct rtl8366_smi *smi = (struct rtl8366_smi *)file->private_data; |
| 543 | int i, len = 0; |
| 544 | char *buf = smi->buf; |
| 545 | |
| 546 | len += snprintf(buf + len, sizeof(smi->buf) - len, |
| 547 | "%2s %6s %4s %6s %6s %3s\n", |
| 548 | "id", "vid","prio", "member", "untag", "fid"); |
| 549 | |
| 550 | for (i = 0; i < smi->num_vlan_mc; ++i) { |
| 551 | struct rtl8366_vlan_mc vlanmc; |
| 552 | |
| 553 | smi->ops->get_vlan_mc(smi, i, &vlanmc); |
| 554 | |
| 555 | len += snprintf(buf + len, sizeof(smi->buf) - len, |
| 556 | "%2d %6d %4d 0x%04x 0x%04x %3d\n", |
| 557 | i, vlanmc.vid, vlanmc.priority, |
| 558 | vlanmc.member, vlanmc.untag, vlanmc.fid); |
| 559 | } |
| 560 | |
| 561 | return simple_read_from_buffer(user_buf, count, ppos, buf, len); |
| 562 | } |
| 563 | |
| 564 | static ssize_t rtl8366_read_debugfs_pvid(struct file *file, |
| 565 | char __user *user_buf, |
| 566 | size_t count, loff_t *ppos) |
| 567 | { |
| 568 | struct rtl8366_smi *smi = (struct rtl8366_smi *)file->private_data; |
| 569 | char *buf = smi->buf; |
| 570 | int len = 0; |
| 571 | int i; |
| 572 | |
| 573 | len += snprintf(buf + len, sizeof(smi->buf) - len, "%4s %4s\n", |
| 574 | "port", "pvid"); |
| 575 | |
| 576 | for (i = 0; i < smi->num_ports; i++) { |
| 577 | int pvid; |
| 578 | int err; |
| 579 | |
| 580 | err = rtl8366_get_pvid(smi, i, &pvid); |
| 581 | if (err) |
| 582 | len += snprintf(buf + len, sizeof(smi->buf) - len, |
| 583 | "%4d error\n", i); |
| 584 | else |
| 585 | len += snprintf(buf + len, sizeof(smi->buf) - len, |
| 586 | "%4d %4d\n", i, pvid); |
| 587 | } |
| 588 | |
| 589 | return simple_read_from_buffer(user_buf, count, ppos, buf, len); |
| 590 | } |
| 591 | |
| 592 | static ssize_t rtl8366_read_debugfs_reg(struct file *file, |
| 593 | char __user *user_buf, |
| 594 | size_t count, loff_t *ppos) |
| 595 | { |
| 596 | struct rtl8366_smi *smi = (struct rtl8366_smi *)file->private_data; |
| 597 | u32 t, reg = smi->dbg_reg; |
| 598 | int err, len = 0; |
| 599 | char *buf = smi->buf; |
| 600 | |
| 601 | memset(buf, '\0', sizeof(smi->buf)); |
| 602 | |
| 603 | err = rtl8366_smi_read_reg(smi, reg, &t); |
| 604 | if (err) { |
| 605 | len += snprintf(buf, sizeof(smi->buf), |
| 606 | "Read failed (reg: 0x%04x)\n", reg); |
| 607 | return simple_read_from_buffer(user_buf, count, ppos, buf, len); |
| 608 | } |
| 609 | |
| 610 | len += snprintf(buf, sizeof(smi->buf), "reg = 0x%04x, val = 0x%04x\n", |
| 611 | reg, t); |
| 612 | |
| 613 | return simple_read_from_buffer(user_buf, count, ppos, buf, len); |
| 614 | } |
| 615 | |
| 616 | static ssize_t rtl8366_write_debugfs_reg(struct file *file, |
| 617 | const char __user *user_buf, |
| 618 | size_t count, loff_t *ppos) |
| 619 | { |
| 620 | struct rtl8366_smi *smi = (struct rtl8366_smi *)file->private_data; |
| 621 | unsigned long data; |
| 622 | u32 reg = smi->dbg_reg; |
| 623 | int err; |
| 624 | size_t len; |
| 625 | char *buf = smi->buf; |
| 626 | |
| 627 | len = min(count, sizeof(smi->buf) - 1); |
| 628 | if (copy_from_user(buf, user_buf, len)) { |
| 629 | dev_err(smi->parent, "copy from user failed\n"); |
| 630 | return -EFAULT; |
| 631 | } |
| 632 | |
| 633 | buf[len] = '\0'; |
| 634 | if (len > 0 && buf[len - 1] == '\n') |
| 635 | buf[len - 1] = '\0'; |
| 636 | |
| 637 | |
| 638 | if (strict_strtoul(buf, 16, &data)) { |
| 639 | dev_err(smi->parent, "Invalid reg value %s\n", buf); |
| 640 | } else { |
| 641 | err = rtl8366_smi_write_reg(smi, reg, data); |
| 642 | if (err) { |
| 643 | dev_err(smi->parent, |
| 644 | "writing reg 0x%04x val 0x%04lx failed\n", |
| 645 | reg, data); |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | return count; |
| 650 | } |
| 651 | |
| 652 | static ssize_t rtl8366_read_debugfs_mibs(struct file *file, |
| 653 | char __user *user_buf, |
| 654 | size_t count, loff_t *ppos) |
| 655 | { |
| 656 | struct rtl8366_smi *smi = file->private_data; |
| 657 | int i, j, len = 0; |
| 658 | char *buf = smi->buf; |
| 659 | |
| 660 | len += snprintf(buf + len, sizeof(smi->buf) - len, "%-36s", |
| 661 | "Counter"); |
| 662 | |
| 663 | for (i = 0; i < smi->num_ports; i++) { |
| 664 | char port_buf[10]; |
| 665 | |
| 666 | snprintf(port_buf, sizeof(port_buf), "Port %d", i); |
| 667 | len += snprintf(buf + len, sizeof(smi->buf) - len, " %12s", |
| 668 | port_buf); |
| 669 | } |
| 670 | len += snprintf(buf + len, sizeof(smi->buf) - len, "\n"); |
| 671 | |
| 672 | for (i = 0; i < smi->num_mib_counters; i++) { |
| 673 | len += snprintf(buf + len, sizeof(smi->buf) - len, "%-36s ", |
| 674 | smi->mib_counters[i].name); |
| 675 | for (j = 0; j < smi->num_ports; j++) { |
| 676 | unsigned long long counter = 0; |
| 677 | |
| 678 | if (!smi->ops->get_mib_counter(smi, i, j, &counter)) |
| 679 | len += snprintf(buf + len, |
| 680 | sizeof(smi->buf) - len, |
| 681 | "%12llu ", counter); |
| 682 | else |
| 683 | len += snprintf(buf + len, |
| 684 | sizeof(smi->buf) - len, |
| 685 | "%12s ", "error"); |
| 686 | } |
| 687 | len += snprintf(buf + len, sizeof(smi->buf) - len, "\n"); |
| 688 | } |
| 689 | |
| 690 | return simple_read_from_buffer(user_buf, count, ppos, buf, len); |
| 691 | } |
| 692 | |
| 693 | static const struct file_operations fops_rtl8366_regs = { |
| 694 | .read = rtl8366_read_debugfs_reg, |
| 695 | .write = rtl8366_write_debugfs_reg, |
| 696 | .open = rtl8366_debugfs_open, |
| 697 | .owner = THIS_MODULE |
| 698 | }; |
| 699 | |
| 700 | static const struct file_operations fops_rtl8366_vlan_mc = { |
| 701 | .read = rtl8366_read_debugfs_vlan_mc, |
| 702 | .open = rtl8366_debugfs_open, |
| 703 | .owner = THIS_MODULE |
| 704 | }; |
| 705 | |
| 706 | static const struct file_operations fops_rtl8366_pvid = { |
| 707 | .read = rtl8366_read_debugfs_pvid, |
| 708 | .open = rtl8366_debugfs_open, |
| 709 | .owner = THIS_MODULE |
| 710 | }; |
| 711 | |
| 712 | static const struct file_operations fops_rtl8366_mibs = { |
| 713 | .read = rtl8366_read_debugfs_mibs, |
| 714 | .open = rtl8366_debugfs_open, |
| 715 | .owner = THIS_MODULE |
| 716 | }; |
| 717 | |
| 718 | static void rtl8366_debugfs_init(struct rtl8366_smi *smi) |
| 719 | { |
| 720 | struct dentry *node; |
| 721 | struct dentry *root; |
| 722 | |
| 723 | if (!smi->debugfs_root) |
| 724 | smi->debugfs_root = debugfs_create_dir(dev_name(smi->parent), |
| 725 | NULL); |
| 726 | |
| 727 | if (!smi->debugfs_root) { |
| 728 | dev_err(smi->parent, "Unable to create debugfs dir\n"); |
| 729 | return; |
| 730 | } |
| 731 | root = smi->debugfs_root; |
| 732 | |
| 733 | node = debugfs_create_x16("reg", S_IRUGO | S_IWUSR, root, |
| 734 | &smi->dbg_reg); |
| 735 | if (!node) { |
| 736 | dev_err(smi->parent, "Creating debugfs file '%s' failed\n", |
| 737 | "reg"); |
| 738 | return; |
| 739 | } |
| 740 | |
| 741 | node = debugfs_create_file("val", S_IRUGO | S_IWUSR, root, smi, |
| 742 | &fops_rtl8366_regs); |
| 743 | if (!node) { |
| 744 | dev_err(smi->parent, "Creating debugfs file '%s' failed\n", |
| 745 | "val"); |
| 746 | return; |
| 747 | } |
| 748 | |
| 749 | node = debugfs_create_file("vlan_mc", S_IRUSR, root, smi, |
| 750 | &fops_rtl8366_vlan_mc); |
| 751 | if (!node) { |
| 752 | dev_err(smi->parent, "Creating debugfs file '%s' failed\n", |
| 753 | "vlan_mc"); |
| 754 | return; |
| 755 | } |
| 756 | |
| 757 | node = debugfs_create_file("pvid", S_IRUSR, root, smi, |
| 758 | &fops_rtl8366_pvid); |
| 759 | if (!node) { |
| 760 | dev_err(smi->parent, "Creating debugfs file '%s' failed\n", |
| 761 | "pvid"); |
| 762 | return; |
| 763 | } |
| 764 | |
| 765 | node = debugfs_create_file("mibs", S_IRUSR, smi->debugfs_root, smi, |
| 766 | &fops_rtl8366_mibs); |
| 767 | if (!node) |
| 768 | dev_err(smi->parent, "Creating debugfs file '%s' failed\n", |
| 769 | "mibs"); |
| 770 | } |
| 771 | |
| 772 | static void rtl8366_debugfs_remove(struct rtl8366_smi *smi) |
| 773 | { |
| 774 | if (smi->debugfs_root) { |
| 775 | debugfs_remove_recursive(smi->debugfs_root); |
| 776 | smi->debugfs_root = NULL; |
| 777 | } |
| 778 | } |
| 779 | #else |
| 780 | static inline void rtl8366_debugfs_init(struct rtl8366_smi *smi) {} |
| 781 | static inline void rtl8366_debugfs_remove(struct rtl8366_smi *smi) {} |
| 782 | #endif /* CONFIG_RTL8366S_PHY_DEBUG_FS */ |
| 783 | |
| 784 | static int rtl8366_smi_mii_init(struct rtl8366_smi *smi) |
| 785 | { |
| 786 | int ret; |
| 787 | int i; |
| 788 | |
| 789 | smi->mii_bus = mdiobus_alloc(); |
| 790 | if (smi->mii_bus == NULL) { |
| 791 | ret = -ENOMEM; |
| 792 | goto err; |
| 793 | } |
| 794 | |
| 795 | smi->mii_bus->priv = (void *) smi; |
| 796 | smi->mii_bus->name = dev_name(smi->parent); |
| 797 | smi->mii_bus->read = smi->ops->mii_read; |
| 798 | smi->mii_bus->write = smi->ops->mii_write; |
| 799 | snprintf(smi->mii_bus->id, MII_BUS_ID_SIZE, "%s", |
| 800 | dev_name(smi->parent)); |
| 801 | smi->mii_bus->parent = smi->parent; |
| 802 | smi->mii_bus->phy_mask = ~(0x1f); |
| 803 | smi->mii_bus->irq = smi->mii_irq; |
| 804 | for (i = 0; i < PHY_MAX_ADDR; i++) |
| 805 | smi->mii_irq[i] = PHY_POLL; |
| 806 | |
| 807 | ret = mdiobus_register(smi->mii_bus); |
| 808 | if (ret) |
| 809 | goto err_free; |
| 810 | |
| 811 | return 0; |
| 812 | |
| 813 | err_free: |
| 814 | mdiobus_free(smi->mii_bus); |
| 815 | err: |
| 816 | return ret; |
| 817 | } |
| 818 | |
| 819 | static void rtl8366_smi_mii_cleanup(struct rtl8366_smi *smi) |
| 820 | { |
| 821 | mdiobus_unregister(smi->mii_bus); |
| 822 | mdiobus_free(smi->mii_bus); |
| 823 | } |
| 824 | |
| 825 | int rtl8366_sw_get_port_pvid(struct switch_dev *dev, int port, int *val) |
| 826 | { |
| 827 | struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev); |
| 828 | return rtl8366_get_pvid(smi, port, val); |
| 829 | } |
| 830 | EXPORT_SYMBOL_GPL(rtl8366_sw_get_port_pvid); |
| 831 | |
| 832 | int rtl8366_sw_set_port_pvid(struct switch_dev *dev, int port, int val) |
| 833 | { |
| 834 | struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev); |
| 835 | return rtl8366_set_pvid(smi, port, val); |
| 836 | } |
| 837 | EXPORT_SYMBOL_GPL(rtl8366_sw_set_port_pvid); |
| 838 | |
| 839 | int rtl8366_sw_get_port_mib(struct switch_dev *dev, |
| 840 | const struct switch_attr *attr, |
| 841 | struct switch_val *val) |
| 842 | { |
| 843 | struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev); |
| 844 | int i, len = 0; |
| 845 | unsigned long long counter = 0; |
| 846 | char *buf = smi->buf; |
| 847 | |
| 848 | if (val->port_vlan >= smi->num_ports) |
| 849 | return -EINVAL; |
| 850 | |
| 851 | len += snprintf(buf + len, sizeof(smi->buf) - len, |
| 852 | "Port %d MIB counters\n", |
| 853 | val->port_vlan); |
| 854 | |
| 855 | for (i = 0; i < smi->num_mib_counters; ++i) { |
| 856 | len += snprintf(buf + len, sizeof(smi->buf) - len, |
| 857 | "%-36s: ", smi->mib_counters[i].name); |
| 858 | if (!smi->ops->get_mib_counter(smi, i, val->port_vlan, |
| 859 | &counter)) |
| 860 | len += snprintf(buf + len, sizeof(smi->buf) - len, |
| 861 | "%llu\n", counter); |
| 862 | else |
| 863 | len += snprintf(buf + len, sizeof(smi->buf) - len, |
| 864 | "%s\n", "error"); |
| 865 | } |
| 866 | |
| 867 | val->value.s = buf; |
| 868 | val->len = len; |
| 869 | return 0; |
| 870 | } |
| 871 | EXPORT_SYMBOL_GPL(rtl8366_sw_get_port_mib); |
| 872 | |
| 873 | int rtl8366_sw_get_vlan_info(struct switch_dev *dev, |
| 874 | const struct switch_attr *attr, |
| 875 | struct switch_val *val) |
| 876 | { |
| 877 | int i; |
| 878 | u32 len = 0; |
| 879 | struct rtl8366_vlan_4k vlan4k; |
| 880 | struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev); |
| 881 | char *buf = smi->buf; |
| 882 | int err; |
| 883 | |
| 884 | if (!smi->ops->is_vlan_valid(smi, val->port_vlan)) |
| 885 | return -EINVAL; |
| 886 | |
| 887 | memset(buf, '\0', sizeof(smi->buf)); |
| 888 | |
| 889 | err = smi->ops->get_vlan_4k(smi, val->port_vlan, &vlan4k); |
| 890 | if (err) |
| 891 | return err; |
| 892 | |
| 893 | len += snprintf(buf + len, sizeof(smi->buf) - len, |
| 894 | "VLAN %d: Ports: '", vlan4k.vid); |
| 895 | |
| 896 | for (i = 0; i < smi->num_ports; i++) { |
| 897 | if (!(vlan4k.member & (1 << i))) |
| 898 | continue; |
| 899 | |
| 900 | len += snprintf(buf + len, sizeof(smi->buf) - len, "%d%s", i, |
| 901 | (vlan4k.untag & (1 << i)) ? "" : "t"); |
| 902 | } |
| 903 | |
| 904 | len += snprintf(buf + len, sizeof(smi->buf) - len, |
| 905 | "', members=%04x, untag=%04x, fid=%u", |
| 906 | vlan4k.member, vlan4k.untag, vlan4k.fid); |
| 907 | |
| 908 | val->value.s = buf; |
| 909 | val->len = len; |
| 910 | |
| 911 | return 0; |
| 912 | } |
| 913 | EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_info); |
| 914 | |
| 915 | int rtl8366_sw_get_vlan_ports(struct switch_dev *dev, struct switch_val *val) |
| 916 | { |
| 917 | struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev); |
| 918 | struct switch_port *port; |
| 919 | struct rtl8366_vlan_4k vlan4k; |
| 920 | int i; |
| 921 | |
| 922 | if (!smi->ops->is_vlan_valid(smi, val->port_vlan)) |
| 923 | return -EINVAL; |
| 924 | |
| 925 | smi->ops->get_vlan_4k(smi, val->port_vlan, &vlan4k); |
| 926 | |
| 927 | port = &val->value.ports[0]; |
| 928 | val->len = 0; |
| 929 | for (i = 0; i < smi->num_ports; i++) { |
| 930 | if (!(vlan4k.member & BIT(i))) |
| 931 | continue; |
| 932 | |
| 933 | port->id = i; |
| 934 | port->flags = (vlan4k.untag & BIT(i)) ? |
| 935 | 0 : BIT(SWITCH_PORT_FLAG_TAGGED); |
| 936 | val->len++; |
| 937 | port++; |
| 938 | } |
| 939 | return 0; |
| 940 | } |
| 941 | EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_ports); |
| 942 | |
| 943 | int rtl8366_sw_set_vlan_ports(struct switch_dev *dev, struct switch_val *val) |
| 944 | { |
| 945 | struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev); |
| 946 | struct switch_port *port; |
| 947 | u32 member = 0; |
| 948 | u32 untag = 0; |
| 949 | int err; |
| 950 | int i; |
| 951 | |
| 952 | if (!smi->ops->is_vlan_valid(smi, val->port_vlan)) |
| 953 | return -EINVAL; |
| 954 | |
| 955 | port = &val->value.ports[0]; |
| 956 | for (i = 0; i < val->len; i++, port++) { |
| 957 | member |= BIT(port->id); |
| 958 | |
| 959 | if (!(port->flags & BIT(SWITCH_PORT_FLAG_TAGGED))) |
| 960 | untag |= BIT(port->id); |
| 961 | |
| 962 | /* |
| 963 | * To ensure that we have a valid MC entry for this VLAN, |
| 964 | * initialize the port VLAN ID here. |
| 965 | */ |
| 966 | err = rtl8366_set_pvid(smi, port->id, val->port_vlan); |
| 967 | if (err < 0) |
| 968 | return err; |
| 969 | } |
| 970 | |
| 971 | return rtl8366_set_vlan(smi, val->port_vlan, member, untag, 0); |
| 972 | } |
| 973 | EXPORT_SYMBOL_GPL(rtl8366_sw_set_vlan_ports); |
| 974 | |
| 975 | int rtl8366_sw_get_vlan_enable(struct switch_dev *dev, |
| 976 | const struct switch_attr *attr, |
| 977 | struct switch_val *val) |
| 978 | { |
| 979 | struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev); |
| 980 | |
| 981 | if (attr->ofs > 2) |
| 982 | return -EINVAL; |
| 983 | |
| 984 | if (attr->ofs == 1) |
| 985 | val->value.i = smi->vlan_enabled; |
| 986 | else |
| 987 | val->value.i = smi->vlan4k_enabled; |
| 988 | |
| 989 | return 0; |
| 990 | } |
| 991 | EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_enable); |
| 992 | |
| 993 | int rtl8366_sw_set_vlan_enable(struct switch_dev *dev, |
| 994 | const struct switch_attr *attr, |
| 995 | struct switch_val *val) |
| 996 | { |
| 997 | struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev); |
| 998 | int err; |
| 999 | |
| 1000 | if (attr->ofs > 2) |
| 1001 | return -EINVAL; |
| 1002 | |
| 1003 | if (attr->ofs == 1) |
| 1004 | err = rtl8366_enable_vlan(smi, val->value.i); |
| 1005 | else |
| 1006 | err = rtl8366_enable_vlan4k(smi, val->value.i); |
| 1007 | |
| 1008 | return err; |
| 1009 | } |
| 1010 | EXPORT_SYMBOL_GPL(rtl8366_sw_set_vlan_enable); |
| 1011 | |
| 1012 | struct rtl8366_smi *rtl8366_smi_alloc(struct device *parent) |
| 1013 | { |
| 1014 | struct rtl8366_smi *smi; |
| 1015 | |
| 1016 | BUG_ON(!parent); |
| 1017 | |
| 1018 | smi = kzalloc(sizeof(*smi), GFP_KERNEL); |
| 1019 | if (!smi) { |
| 1020 | dev_err(parent, "no memory for private data\n"); |
| 1021 | return NULL; |
| 1022 | } |
| 1023 | |
| 1024 | smi->parent = parent; |
| 1025 | return smi; |
| 1026 | } |
| 1027 | EXPORT_SYMBOL_GPL(rtl8366_smi_alloc); |
| 1028 | |
| 1029 | int rtl8366_smi_init(struct rtl8366_smi *smi) |
| 1030 | { |
| 1031 | int err; |
| 1032 | |
| 1033 | if (!smi->ops) |
| 1034 | return -EINVAL; |
| 1035 | |
| 1036 | err = gpio_request(smi->gpio_sda, dev_name(smi->parent)); |
| 1037 | if (err) { |
| 1038 | dev_err(smi->parent, "gpio_request failed for %u, err=%d\n", |
| 1039 | smi->gpio_sda, err); |
| 1040 | goto err_out; |
| 1041 | } |
| 1042 | |
| 1043 | err = gpio_request(smi->gpio_sck, dev_name(smi->parent)); |
| 1044 | if (err) { |
| 1045 | dev_err(smi->parent, "gpio_request failed for %u, err=%d\n", |
| 1046 | smi->gpio_sck, err); |
| 1047 | goto err_free_sda; |
| 1048 | } |
| 1049 | |
| 1050 | spin_lock_init(&smi->lock); |
| 1051 | |
| 1052 | dev_info(smi->parent, "using GPIO pins %u (SDA) and %u (SCK)\n", |
| 1053 | smi->gpio_sda, smi->gpio_sck); |
| 1054 | |
| 1055 | err = smi->ops->detect(smi); |
| 1056 | if (err) { |
| 1057 | dev_err(smi->parent, "chip detection failed, err=%d\n", err); |
| 1058 | goto err_free_sck; |
| 1059 | } |
| 1060 | |
| 1061 | err = smi->ops->setup(smi); |
| 1062 | if (err) { |
| 1063 | dev_err(smi->parent, "chip setup failed, err=%d\n", err); |
| 1064 | goto err_free_sck; |
| 1065 | } |
| 1066 | |
| 1067 | err = rtl8366_smi_mii_init(smi); |
| 1068 | if (err) |
| 1069 | goto err_free_sck; |
| 1070 | |
| 1071 | rtl8366_debugfs_init(smi); |
| 1072 | |
| 1073 | return 0; |
| 1074 | |
| 1075 | err_free_sck: |
| 1076 | gpio_free(smi->gpio_sck); |
| 1077 | err_free_sda: |
| 1078 | gpio_free(smi->gpio_sda); |
| 1079 | err_out: |
| 1080 | return err; |
| 1081 | } |
| 1082 | EXPORT_SYMBOL_GPL(rtl8366_smi_init); |
| 1083 | |
| 1084 | void rtl8366_smi_cleanup(struct rtl8366_smi *smi) |
| 1085 | { |
| 1086 | rtl8366_debugfs_remove(smi); |
| 1087 | rtl8366_smi_mii_cleanup(smi); |
| 1088 | gpio_free(smi->gpio_sck); |
| 1089 | gpio_free(smi->gpio_sda); |
| 1090 | } |
| 1091 | EXPORT_SYMBOL_GPL(rtl8366_smi_cleanup); |
| 1092 | |
| 1093 | MODULE_DESCRIPTION("Realtek RTL8366 SMI interface driver"); |
| 1094 | MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>"); |
| 1095 | MODULE_LICENSE("GPL v2"); |
| 1096 | |