| 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 | int rtl8366_set_vlan(struct rtl8366_smi *smi, int vid, u32 member, u32 untag, |
| 311 | 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 | EXPORT_SYMBOL_GPL(rtl8366_set_vlan); |
| 351 | |
| 352 | int rtl8366_reset_vlan(struct rtl8366_smi *smi) |
| 353 | { |
| 354 | struct rtl8366_vlan_mc vlanmc; |
| 355 | int err; |
| 356 | int i; |
| 357 | |
| 358 | /* clear VLAN member configurations */ |
| 359 | vlanmc.vid = 0; |
| 360 | vlanmc.priority = 0; |
| 361 | vlanmc.member = 0; |
| 362 | vlanmc.untag = 0; |
| 363 | vlanmc.fid = 0; |
| 364 | for (i = 0; i < smi->num_vlan_mc; i++) { |
| 365 | err = smi->ops->set_vlan_mc(smi, i, &vlanmc); |
| 366 | if (err) |
| 367 | return err; |
| 368 | } |
| 369 | |
| 370 | for (i = 0; i < smi->num_ports; i++) { |
| 371 | if (i == smi->cpu_port) |
| 372 | continue; |
| 373 | |
| 374 | err = rtl8366_set_vlan(smi, (i + 1), |
| 375 | (1 << i) | (1 << smi->cpu_port), |
| 376 | (1 << i) | (1 << smi->cpu_port), |
| 377 | 0); |
| 378 | if (err) |
| 379 | return err; |
| 380 | |
| 381 | err = rtl8366_set_pvid(smi, i, (i + 1)); |
| 382 | if (err) |
| 383 | return err; |
| 384 | } |
| 385 | |
| 386 | return 0; |
| 387 | } |
| 388 | EXPORT_SYMBOL_GPL(rtl8366_reset_vlan); |
| 389 | |
| 390 | int rtl8366_get_pvid(struct rtl8366_smi *smi, int port, int *val) |
| 391 | { |
| 392 | struct rtl8366_vlan_mc vlanmc; |
| 393 | int err; |
| 394 | int index; |
| 395 | |
| 396 | err = smi->ops->get_mc_index(smi, port, &index); |
| 397 | if (err) |
| 398 | return err; |
| 399 | |
| 400 | err = smi->ops->get_vlan_mc(smi, index, &vlanmc); |
| 401 | if (err) |
| 402 | return err; |
| 403 | |
| 404 | *val = vlanmc.vid; |
| 405 | return 0; |
| 406 | } |
| 407 | EXPORT_SYMBOL_GPL(rtl8366_get_pvid); |
| 408 | |
| 409 | int rtl8366_set_pvid(struct rtl8366_smi *smi, unsigned port, unsigned vid) |
| 410 | { |
| 411 | struct rtl8366_vlan_mc vlanmc; |
| 412 | struct rtl8366_vlan_4k vlan4k; |
| 413 | int err; |
| 414 | int i; |
| 415 | |
| 416 | /* Try to find an existing MC entry for this VID */ |
| 417 | for (i = 0; i < smi->num_vlan_mc; i++) { |
| 418 | err = smi->ops->get_vlan_mc(smi, i, &vlanmc); |
| 419 | if (err) |
| 420 | return err; |
| 421 | |
| 422 | if (vid == vlanmc.vid) { |
| 423 | err = smi->ops->set_vlan_mc(smi, i, &vlanmc); |
| 424 | if (err) |
| 425 | return err; |
| 426 | |
| 427 | err = smi->ops->set_mc_index(smi, port, i); |
| 428 | return err; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | /* We have no MC entry for this VID, try to find an empty one */ |
| 433 | for (i = 0; i < smi->num_vlan_mc; i++) { |
| 434 | err = smi->ops->get_vlan_mc(smi, i, &vlanmc); |
| 435 | if (err) |
| 436 | return err; |
| 437 | |
| 438 | if (vlanmc.vid == 0 && vlanmc.member == 0) { |
| 439 | /* Update the entry from the 4K table */ |
| 440 | err = smi->ops->get_vlan_4k(smi, vid, &vlan4k); |
| 441 | if (err) |
| 442 | return err; |
| 443 | |
| 444 | vlanmc.vid = vid; |
| 445 | vlanmc.member = vlan4k.member; |
| 446 | vlanmc.untag = vlan4k.untag; |
| 447 | vlanmc.fid = vlan4k.fid; |
| 448 | err = smi->ops->set_vlan_mc(smi, i, &vlanmc); |
| 449 | if (err) |
| 450 | return err; |
| 451 | |
| 452 | err = smi->ops->set_mc_index(smi, port, i); |
| 453 | return err; |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | /* MC table is full, try to find an unused entry and replace it */ |
| 458 | for (i = 0; i < smi->num_vlan_mc; i++) { |
| 459 | int used; |
| 460 | |
| 461 | err = rtl8366_mc_is_used(smi, i, &used); |
| 462 | if (err) |
| 463 | return err; |
| 464 | |
| 465 | if (!used) { |
| 466 | /* Update the entry from the 4K table */ |
| 467 | err = smi->ops->get_vlan_4k(smi, vid, &vlan4k); |
| 468 | if (err) |
| 469 | return err; |
| 470 | |
| 471 | vlanmc.vid = vid; |
| 472 | vlanmc.member = vlan4k.member; |
| 473 | vlanmc.untag = vlan4k.untag; |
| 474 | vlanmc.fid = vlan4k.fid; |
| 475 | err = smi->ops->set_vlan_mc(smi, i, &vlanmc); |
| 476 | if (err) |
| 477 | return err; |
| 478 | |
| 479 | err = smi->ops->set_mc_index(smi, port, i); |
| 480 | return err; |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | dev_err(smi->parent, |
| 485 | "all VLAN member configurations are in use\n"); |
| 486 | |
| 487 | return -ENOSPC; |
| 488 | } |
| 489 | EXPORT_SYMBOL_GPL(rtl8366_set_pvid); |
| 490 | |
| 491 | #ifdef CONFIG_RTL8366S_PHY_DEBUG_FS |
| 492 | int rtl8366_debugfs_open(struct inode *inode, struct file *file) |
| 493 | { |
| 494 | file->private_data = inode->i_private; |
| 495 | return 0; |
| 496 | } |
| 497 | EXPORT_SYMBOL_GPL(rtl8366_debugfs_open); |
| 498 | |
| 499 | static ssize_t rtl8366_read_debugfs_vlan_mc(struct file *file, |
| 500 | char __user *user_buf, |
| 501 | size_t count, loff_t *ppos) |
| 502 | { |
| 503 | struct rtl8366_smi *smi = (struct rtl8366_smi *)file->private_data; |
| 504 | int i, len = 0; |
| 505 | char *buf = smi->buf; |
| 506 | |
| 507 | len += snprintf(buf + len, sizeof(smi->buf) - len, |
| 508 | "%2s %6s %4s %6s %6s %3s\n", |
| 509 | "id", "vid","prio", "member", "untag", "fid"); |
| 510 | |
| 511 | for (i = 0; i < smi->num_vlan_mc; ++i) { |
| 512 | struct rtl8366_vlan_mc vlanmc; |
| 513 | |
| 514 | smi->ops->get_vlan_mc(smi, i, &vlanmc); |
| 515 | |
| 516 | len += snprintf(buf + len, sizeof(smi->buf) - len, |
| 517 | "%2d %6d %4d 0x%04x 0x%04x %3d\n", |
| 518 | i, vlanmc.vid, vlanmc.priority, |
| 519 | vlanmc.member, vlanmc.untag, vlanmc.fid); |
| 520 | } |
| 521 | |
| 522 | return simple_read_from_buffer(user_buf, count, ppos, buf, len); |
| 523 | } |
| 524 | |
| 525 | static ssize_t rtl8366_read_debugfs_reg(struct file *file, |
| 526 | char __user *user_buf, |
| 527 | size_t count, loff_t *ppos) |
| 528 | { |
| 529 | struct rtl8366_smi *smi = (struct rtl8366_smi *)file->private_data; |
| 530 | u32 t, reg = smi->dbg_reg; |
| 531 | int err, len = 0; |
| 532 | char *buf = smi->buf; |
| 533 | |
| 534 | memset(buf, '\0', sizeof(smi->buf)); |
| 535 | |
| 536 | err = rtl8366_smi_read_reg(smi, reg, &t); |
| 537 | if (err) { |
| 538 | len += snprintf(buf, sizeof(smi->buf), |
| 539 | "Read failed (reg: 0x%04x)\n", reg); |
| 540 | return simple_read_from_buffer(user_buf, count, ppos, buf, len); |
| 541 | } |
| 542 | |
| 543 | len += snprintf(buf, sizeof(smi->buf), "reg = 0x%04x, val = 0x%04x\n", |
| 544 | reg, t); |
| 545 | |
| 546 | return simple_read_from_buffer(user_buf, count, ppos, buf, len); |
| 547 | } |
| 548 | |
| 549 | static ssize_t rtl8366_write_debugfs_reg(struct file *file, |
| 550 | const char __user *user_buf, |
| 551 | size_t count, loff_t *ppos) |
| 552 | { |
| 553 | struct rtl8366_smi *smi = (struct rtl8366_smi *)file->private_data; |
| 554 | unsigned long data; |
| 555 | u32 reg = smi->dbg_reg; |
| 556 | int err; |
| 557 | size_t len; |
| 558 | char *buf = smi->buf; |
| 559 | |
| 560 | len = min(count, sizeof(smi->buf) - 1); |
| 561 | if (copy_from_user(buf, user_buf, len)) { |
| 562 | dev_err(smi->parent, "copy from user failed\n"); |
| 563 | return -EFAULT; |
| 564 | } |
| 565 | |
| 566 | buf[len] = '\0'; |
| 567 | if (len > 0 && buf[len - 1] == '\n') |
| 568 | buf[len - 1] = '\0'; |
| 569 | |
| 570 | |
| 571 | if (strict_strtoul(buf, 16, &data)) { |
| 572 | dev_err(smi->parent, "Invalid reg value %s\n", buf); |
| 573 | } else { |
| 574 | err = rtl8366_smi_write_reg(smi, reg, data); |
| 575 | if (err) { |
| 576 | dev_err(smi->parent, |
| 577 | "writing reg 0x%04x val 0x%04lx failed\n", |
| 578 | reg, data); |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | return count; |
| 583 | } |
| 584 | |
| 585 | static ssize_t rtl8366_read_debugfs_mibs(struct file *file, |
| 586 | char __user *user_buf, |
| 587 | size_t count, loff_t *ppos) |
| 588 | { |
| 589 | struct rtl8366_smi *smi = file->private_data; |
| 590 | int i, j, len = 0; |
| 591 | char *buf = smi->buf; |
| 592 | |
| 593 | len += snprintf(buf + len, sizeof(smi->buf) - len, "%-36s", |
| 594 | "Counter"); |
| 595 | |
| 596 | for (i = 0; i < smi->num_ports; i++) { |
| 597 | char port_buf[10]; |
| 598 | |
| 599 | snprintf(port_buf, sizeof(port_buf), "Port %d", i); |
| 600 | len += snprintf(buf + len, sizeof(smi->buf) - len, " %12s", |
| 601 | port_buf); |
| 602 | } |
| 603 | len += snprintf(buf + len, sizeof(smi->buf) - len, "\n"); |
| 604 | |
| 605 | for (i = 0; i < smi->num_mib_counters; i++) { |
| 606 | len += snprintf(buf + len, sizeof(smi->buf) - len, "%-36s ", |
| 607 | smi->mib_counters[i].name); |
| 608 | for (j = 0; j < smi->num_ports; j++) { |
| 609 | unsigned long long counter = 0; |
| 610 | |
| 611 | if (!smi->ops->get_mib_counter(smi, i, j, &counter)) |
| 612 | len += snprintf(buf + len, |
| 613 | sizeof(smi->buf) - len, |
| 614 | "%12llu ", counter); |
| 615 | else |
| 616 | len += snprintf(buf + len, |
| 617 | sizeof(smi->buf) - len, |
| 618 | "%12s ", "error"); |
| 619 | } |
| 620 | len += snprintf(buf + len, sizeof(smi->buf) - len, "\n"); |
| 621 | } |
| 622 | |
| 623 | return simple_read_from_buffer(user_buf, count, ppos, buf, len); |
| 624 | } |
| 625 | |
| 626 | static const struct file_operations fops_rtl8366_regs = { |
| 627 | .read = rtl8366_read_debugfs_reg, |
| 628 | .write = rtl8366_write_debugfs_reg, |
| 629 | .open = rtl8366_debugfs_open, |
| 630 | .owner = THIS_MODULE |
| 631 | }; |
| 632 | |
| 633 | static const struct file_operations fops_rtl8366_vlan_mc = { |
| 634 | .read = rtl8366_read_debugfs_vlan_mc, |
| 635 | .open = rtl8366_debugfs_open, |
| 636 | .owner = THIS_MODULE |
| 637 | }; |
| 638 | |
| 639 | static const struct file_operations fops_rtl8366_mibs = { |
| 640 | .read = rtl8366_read_debugfs_mibs, |
| 641 | .open = rtl8366_debugfs_open, |
| 642 | .owner = THIS_MODULE |
| 643 | }; |
| 644 | |
| 645 | static void rtl8366_debugfs_init(struct rtl8366_smi *smi) |
| 646 | { |
| 647 | struct dentry *node; |
| 648 | struct dentry *root; |
| 649 | |
| 650 | if (!smi->debugfs_root) |
| 651 | smi->debugfs_root = debugfs_create_dir(dev_name(smi->parent), |
| 652 | NULL); |
| 653 | |
| 654 | if (!smi->debugfs_root) { |
| 655 | dev_err(smi->parent, "Unable to create debugfs dir\n"); |
| 656 | return; |
| 657 | } |
| 658 | root = smi->debugfs_root; |
| 659 | |
| 660 | node = debugfs_create_x16("reg", S_IRUGO | S_IWUSR, root, |
| 661 | &smi->dbg_reg); |
| 662 | if (!node) { |
| 663 | dev_err(smi->parent, "Creating debugfs file '%s' failed\n", |
| 664 | "reg"); |
| 665 | return; |
| 666 | } |
| 667 | |
| 668 | node = debugfs_create_file("val", S_IRUGO | S_IWUSR, root, smi, |
| 669 | &fops_rtl8366_regs); |
| 670 | if (!node) { |
| 671 | dev_err(smi->parent, "Creating debugfs file '%s' failed\n", |
| 672 | "val"); |
| 673 | return; |
| 674 | } |
| 675 | |
| 676 | node = debugfs_create_file("vlan_mc", S_IRUSR, root, smi, |
| 677 | &fops_rtl8366_vlan_mc); |
| 678 | if (!node) { |
| 679 | dev_err(smi->parent, "Creating debugfs file '%s' failed\n", |
| 680 | "vlan_mc"); |
| 681 | return; |
| 682 | } |
| 683 | |
| 684 | node = debugfs_create_file("mibs", S_IRUSR, smi->debugfs_root, smi, |
| 685 | &fops_rtl8366_mibs); |
| 686 | if (!node) |
| 687 | dev_err(smi->parent, "Creating debugfs file '%s' failed\n", |
| 688 | "mibs"); |
| 689 | } |
| 690 | |
| 691 | static void rtl8366_debugfs_remove(struct rtl8366_smi *smi) |
| 692 | { |
| 693 | if (smi->debugfs_root) { |
| 694 | debugfs_remove_recursive(smi->debugfs_root); |
| 695 | smi->debugfs_root = NULL; |
| 696 | } |
| 697 | } |
| 698 | #else |
| 699 | static inline void rtl8366_debugfs_init(struct rtl8366_smi *smi) {} |
| 700 | static inline void rtl8366_debugfs_remove(struct rtl8366_smi *smi) {} |
| 701 | #endif /* CONFIG_RTL8366S_PHY_DEBUG_FS */ |
| 702 | |
| 703 | static int rtl8366_smi_mii_init(struct rtl8366_smi *smi) |
| 704 | { |
| 705 | int ret; |
| 706 | int i; |
| 707 | |
| 708 | smi->mii_bus = mdiobus_alloc(); |
| 709 | if (smi->mii_bus == NULL) { |
| 710 | ret = -ENOMEM; |
| 711 | goto err; |
| 712 | } |
| 713 | |
| 714 | smi->mii_bus->priv = (void *) smi; |
| 715 | smi->mii_bus->name = dev_name(smi->parent); |
| 716 | smi->mii_bus->read = smi->ops->mii_read; |
| 717 | smi->mii_bus->write = smi->ops->mii_write; |
| 718 | snprintf(smi->mii_bus->id, MII_BUS_ID_SIZE, "%s", |
| 719 | dev_name(smi->parent)); |
| 720 | smi->mii_bus->parent = smi->parent; |
| 721 | smi->mii_bus->phy_mask = ~(0x1f); |
| 722 | smi->mii_bus->irq = smi->mii_irq; |
| 723 | for (i = 0; i < PHY_MAX_ADDR; i++) |
| 724 | smi->mii_irq[i] = PHY_POLL; |
| 725 | |
| 726 | ret = mdiobus_register(smi->mii_bus); |
| 727 | if (ret) |
| 728 | goto err_free; |
| 729 | |
| 730 | return 0; |
| 731 | |
| 732 | err_free: |
| 733 | mdiobus_free(smi->mii_bus); |
| 734 | err: |
| 735 | return ret; |
| 736 | } |
| 737 | |
| 738 | static void rtl8366_smi_mii_cleanup(struct rtl8366_smi *smi) |
| 739 | { |
| 740 | mdiobus_unregister(smi->mii_bus); |
| 741 | mdiobus_free(smi->mii_bus); |
| 742 | } |
| 743 | |
| 744 | int rtl8366_smi_init(struct rtl8366_smi *smi) |
| 745 | { |
| 746 | int err; |
| 747 | |
| 748 | if (!smi->parent) |
| 749 | return -EINVAL; |
| 750 | |
| 751 | if (!smi->ops) |
| 752 | return -EINVAL; |
| 753 | |
| 754 | err = gpio_request(smi->gpio_sda, dev_name(smi->parent)); |
| 755 | if (err) { |
| 756 | dev_err(smi->parent, "gpio_request failed for %u, err=%d\n", |
| 757 | smi->gpio_sda, err); |
| 758 | goto err_out; |
| 759 | } |
| 760 | |
| 761 | err = gpio_request(smi->gpio_sck, dev_name(smi->parent)); |
| 762 | if (err) { |
| 763 | dev_err(smi->parent, "gpio_request failed for %u, err=%d\n", |
| 764 | smi->gpio_sck, err); |
| 765 | goto err_free_sda; |
| 766 | } |
| 767 | |
| 768 | spin_lock_init(&smi->lock); |
| 769 | |
| 770 | dev_info(smi->parent, "using GPIO pins %u (SDA) and %u (SCK)\n", |
| 771 | smi->gpio_sda, smi->gpio_sck); |
| 772 | |
| 773 | err = smi->ops->detect(smi); |
| 774 | if (err) { |
| 775 | dev_err(smi->parent, "chip detection failed, err=%d\n", err); |
| 776 | goto err_free_sck; |
| 777 | } |
| 778 | |
| 779 | err = rtl8366_smi_mii_init(smi); |
| 780 | if (err) |
| 781 | goto err_free_sck; |
| 782 | |
| 783 | rtl8366_debugfs_init(smi); |
| 784 | |
| 785 | return 0; |
| 786 | |
| 787 | err_free_sck: |
| 788 | gpio_free(smi->gpio_sck); |
| 789 | err_free_sda: |
| 790 | gpio_free(smi->gpio_sda); |
| 791 | err_out: |
| 792 | return err; |
| 793 | } |
| 794 | EXPORT_SYMBOL_GPL(rtl8366_smi_init); |
| 795 | |
| 796 | void rtl8366_smi_cleanup(struct rtl8366_smi *smi) |
| 797 | { |
| 798 | rtl8366_debugfs_remove(smi); |
| 799 | rtl8366_smi_mii_cleanup(smi); |
| 800 | gpio_free(smi->gpio_sck); |
| 801 | gpio_free(smi->gpio_sda); |
| 802 | } |
| 803 | EXPORT_SYMBOL_GPL(rtl8366_smi_cleanup); |
| 804 | |
| 805 | MODULE_DESCRIPTION("Realtek RTL8366 SMI interface driver"); |
| 806 | MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>"); |
| 807 | MODULE_LICENSE("GPL v2"); |
| 808 | |