generic: rtl8366: add debugfs file for VLAN4K table
[openwrt.git] / target / linux / generic / files / drivers / net / phy / rtl8366_smi.c
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 #define RTL8366_VLAN4K_PAGE_SIZE 64
565 #define RTL8366_VLAN4K_NUM_PAGES (4096 / RTL8366_VLAN4K_PAGE_SIZE)
566
567 static ssize_t rtl8366_read_debugfs_vlan_4k(struct file *file,
568 char __user *user_buf,
569 size_t count, loff_t *ppos)
570 {
571 struct rtl8366_smi *smi = (struct rtl8366_smi *)file->private_data;
572 int i, len = 0;
573 int offset;
574 char *buf = smi->buf;
575
576 if (smi->dbg_vlan_4k_page >= RTL8366_VLAN4K_NUM_PAGES) {
577 len += snprintf(buf + len, sizeof(smi->buf) - len,
578 "invalid page: %u\n", smi->dbg_vlan_4k_page);
579 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
580 }
581
582 len += snprintf(buf + len, sizeof(smi->buf) - len,
583 "%4s %6s %6s %3s\n",
584 "vid", "member", "untag", "fid");
585
586 offset = RTL8366_VLAN4K_PAGE_SIZE * smi->dbg_vlan_4k_page;
587 for (i = 0; i < RTL8366_VLAN4K_PAGE_SIZE; i++) {
588 struct rtl8366_vlan_4k vlan4k;
589
590 smi->ops->get_vlan_4k(smi, offset + i, &vlan4k);
591
592 len += snprintf(buf + len, sizeof(smi->buf) - len,
593 "%4d 0x%04x 0x%04x %3d\n",
594 vlan4k.vid, vlan4k.member,
595 vlan4k.untag, vlan4k.fid);
596 }
597
598 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
599 }
600
601 static ssize_t rtl8366_read_debugfs_pvid(struct file *file,
602 char __user *user_buf,
603 size_t count, loff_t *ppos)
604 {
605 struct rtl8366_smi *smi = (struct rtl8366_smi *)file->private_data;
606 char *buf = smi->buf;
607 int len = 0;
608 int i;
609
610 len += snprintf(buf + len, sizeof(smi->buf) - len, "%4s %4s\n",
611 "port", "pvid");
612
613 for (i = 0; i < smi->num_ports; i++) {
614 int pvid;
615 int err;
616
617 err = rtl8366_get_pvid(smi, i, &pvid);
618 if (err)
619 len += snprintf(buf + len, sizeof(smi->buf) - len,
620 "%4d error\n", i);
621 else
622 len += snprintf(buf + len, sizeof(smi->buf) - len,
623 "%4d %4d\n", i, pvid);
624 }
625
626 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
627 }
628
629 static ssize_t rtl8366_read_debugfs_reg(struct file *file,
630 char __user *user_buf,
631 size_t count, loff_t *ppos)
632 {
633 struct rtl8366_smi *smi = (struct rtl8366_smi *)file->private_data;
634 u32 t, reg = smi->dbg_reg;
635 int err, len = 0;
636 char *buf = smi->buf;
637
638 memset(buf, '\0', sizeof(smi->buf));
639
640 err = rtl8366_smi_read_reg(smi, reg, &t);
641 if (err) {
642 len += snprintf(buf, sizeof(smi->buf),
643 "Read failed (reg: 0x%04x)\n", reg);
644 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
645 }
646
647 len += snprintf(buf, sizeof(smi->buf), "reg = 0x%04x, val = 0x%04x\n",
648 reg, t);
649
650 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
651 }
652
653 static ssize_t rtl8366_write_debugfs_reg(struct file *file,
654 const char __user *user_buf,
655 size_t count, loff_t *ppos)
656 {
657 struct rtl8366_smi *smi = (struct rtl8366_smi *)file->private_data;
658 unsigned long data;
659 u32 reg = smi->dbg_reg;
660 int err;
661 size_t len;
662 char *buf = smi->buf;
663
664 len = min(count, sizeof(smi->buf) - 1);
665 if (copy_from_user(buf, user_buf, len)) {
666 dev_err(smi->parent, "copy from user failed\n");
667 return -EFAULT;
668 }
669
670 buf[len] = '\0';
671 if (len > 0 && buf[len - 1] == '\n')
672 buf[len - 1] = '\0';
673
674
675 if (strict_strtoul(buf, 16, &data)) {
676 dev_err(smi->parent, "Invalid reg value %s\n", buf);
677 } else {
678 err = rtl8366_smi_write_reg(smi, reg, data);
679 if (err) {
680 dev_err(smi->parent,
681 "writing reg 0x%04x val 0x%04lx failed\n",
682 reg, data);
683 }
684 }
685
686 return count;
687 }
688
689 static ssize_t rtl8366_read_debugfs_mibs(struct file *file,
690 char __user *user_buf,
691 size_t count, loff_t *ppos)
692 {
693 struct rtl8366_smi *smi = file->private_data;
694 int i, j, len = 0;
695 char *buf = smi->buf;
696
697 len += snprintf(buf + len, sizeof(smi->buf) - len, "%-36s",
698 "Counter");
699
700 for (i = 0; i < smi->num_ports; i++) {
701 char port_buf[10];
702
703 snprintf(port_buf, sizeof(port_buf), "Port %d", i);
704 len += snprintf(buf + len, sizeof(smi->buf) - len, " %12s",
705 port_buf);
706 }
707 len += snprintf(buf + len, sizeof(smi->buf) - len, "\n");
708
709 for (i = 0; i < smi->num_mib_counters; i++) {
710 len += snprintf(buf + len, sizeof(smi->buf) - len, "%-36s ",
711 smi->mib_counters[i].name);
712 for (j = 0; j < smi->num_ports; j++) {
713 unsigned long long counter = 0;
714
715 if (!smi->ops->get_mib_counter(smi, i, j, &counter))
716 len += snprintf(buf + len,
717 sizeof(smi->buf) - len,
718 "%12llu ", counter);
719 else
720 len += snprintf(buf + len,
721 sizeof(smi->buf) - len,
722 "%12s ", "error");
723 }
724 len += snprintf(buf + len, sizeof(smi->buf) - len, "\n");
725 }
726
727 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
728 }
729
730 static const struct file_operations fops_rtl8366_regs = {
731 .read = rtl8366_read_debugfs_reg,
732 .write = rtl8366_write_debugfs_reg,
733 .open = rtl8366_debugfs_open,
734 .owner = THIS_MODULE
735 };
736
737 static const struct file_operations fops_rtl8366_vlan_mc = {
738 .read = rtl8366_read_debugfs_vlan_mc,
739 .open = rtl8366_debugfs_open,
740 .owner = THIS_MODULE
741 };
742
743 static const struct file_operations fops_rtl8366_vlan_4k = {
744 .read = rtl8366_read_debugfs_vlan_4k,
745 .open = rtl8366_debugfs_open,
746 .owner = THIS_MODULE
747 };
748
749 static const struct file_operations fops_rtl8366_pvid = {
750 .read = rtl8366_read_debugfs_pvid,
751 .open = rtl8366_debugfs_open,
752 .owner = THIS_MODULE
753 };
754
755 static const struct file_operations fops_rtl8366_mibs = {
756 .read = rtl8366_read_debugfs_mibs,
757 .open = rtl8366_debugfs_open,
758 .owner = THIS_MODULE
759 };
760
761 static void rtl8366_debugfs_init(struct rtl8366_smi *smi)
762 {
763 struct dentry *node;
764 struct dentry *root;
765
766 if (!smi->debugfs_root)
767 smi->debugfs_root = debugfs_create_dir(dev_name(smi->parent),
768 NULL);
769
770 if (!smi->debugfs_root) {
771 dev_err(smi->parent, "Unable to create debugfs dir\n");
772 return;
773 }
774 root = smi->debugfs_root;
775
776 node = debugfs_create_x16("reg", S_IRUGO | S_IWUSR, root,
777 &smi->dbg_reg);
778 if (!node) {
779 dev_err(smi->parent, "Creating debugfs file '%s' failed\n",
780 "reg");
781 return;
782 }
783
784 node = debugfs_create_file("val", S_IRUGO | S_IWUSR, root, smi,
785 &fops_rtl8366_regs);
786 if (!node) {
787 dev_err(smi->parent, "Creating debugfs file '%s' failed\n",
788 "val");
789 return;
790 }
791
792 node = debugfs_create_file("vlan_mc", S_IRUSR, root, smi,
793 &fops_rtl8366_vlan_mc);
794 if (!node) {
795 dev_err(smi->parent, "Creating debugfs file '%s' failed\n",
796 "vlan_mc");
797 return;
798 }
799
800 node = debugfs_create_u8("vlan_4k_page", S_IRUGO | S_IWUSR, root,
801 &smi->dbg_vlan_4k_page);
802 if (!node) {
803 dev_err(smi->parent, "Creating debugfs file '%s' failed\n",
804 "vlan_4k_page");
805 return;
806 }
807
808 node = debugfs_create_file("vlan_4k", S_IRUSR, root, smi,
809 &fops_rtl8366_vlan_4k);
810 if (!node) {
811 dev_err(smi->parent, "Creating debugfs file '%s' failed\n",
812 "vlan_4k");
813 return;
814 }
815
816 node = debugfs_create_file("pvid", S_IRUSR, root, smi,
817 &fops_rtl8366_pvid);
818 if (!node) {
819 dev_err(smi->parent, "Creating debugfs file '%s' failed\n",
820 "pvid");
821 return;
822 }
823
824 node = debugfs_create_file("mibs", S_IRUSR, smi->debugfs_root, smi,
825 &fops_rtl8366_mibs);
826 if (!node)
827 dev_err(smi->parent, "Creating debugfs file '%s' failed\n",
828 "mibs");
829 }
830
831 static void rtl8366_debugfs_remove(struct rtl8366_smi *smi)
832 {
833 if (smi->debugfs_root) {
834 debugfs_remove_recursive(smi->debugfs_root);
835 smi->debugfs_root = NULL;
836 }
837 }
838 #else
839 static inline void rtl8366_debugfs_init(struct rtl8366_smi *smi) {}
840 static inline void rtl8366_debugfs_remove(struct rtl8366_smi *smi) {}
841 #endif /* CONFIG_RTL8366S_PHY_DEBUG_FS */
842
843 static int rtl8366_smi_mii_init(struct rtl8366_smi *smi)
844 {
845 int ret;
846 int i;
847
848 smi->mii_bus = mdiobus_alloc();
849 if (smi->mii_bus == NULL) {
850 ret = -ENOMEM;
851 goto err;
852 }
853
854 smi->mii_bus->priv = (void *) smi;
855 smi->mii_bus->name = dev_name(smi->parent);
856 smi->mii_bus->read = smi->ops->mii_read;
857 smi->mii_bus->write = smi->ops->mii_write;
858 snprintf(smi->mii_bus->id, MII_BUS_ID_SIZE, "%s",
859 dev_name(smi->parent));
860 smi->mii_bus->parent = smi->parent;
861 smi->mii_bus->phy_mask = ~(0x1f);
862 smi->mii_bus->irq = smi->mii_irq;
863 for (i = 0; i < PHY_MAX_ADDR; i++)
864 smi->mii_irq[i] = PHY_POLL;
865
866 ret = mdiobus_register(smi->mii_bus);
867 if (ret)
868 goto err_free;
869
870 return 0;
871
872 err_free:
873 mdiobus_free(smi->mii_bus);
874 err:
875 return ret;
876 }
877
878 static void rtl8366_smi_mii_cleanup(struct rtl8366_smi *smi)
879 {
880 mdiobus_unregister(smi->mii_bus);
881 mdiobus_free(smi->mii_bus);
882 }
883
884 int rtl8366_sw_get_port_pvid(struct switch_dev *dev, int port, int *val)
885 {
886 struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
887 return rtl8366_get_pvid(smi, port, val);
888 }
889 EXPORT_SYMBOL_GPL(rtl8366_sw_get_port_pvid);
890
891 int rtl8366_sw_set_port_pvid(struct switch_dev *dev, int port, int val)
892 {
893 struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
894 return rtl8366_set_pvid(smi, port, val);
895 }
896 EXPORT_SYMBOL_GPL(rtl8366_sw_set_port_pvid);
897
898 int rtl8366_sw_get_port_mib(struct switch_dev *dev,
899 const struct switch_attr *attr,
900 struct switch_val *val)
901 {
902 struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
903 int i, len = 0;
904 unsigned long long counter = 0;
905 char *buf = smi->buf;
906
907 if (val->port_vlan >= smi->num_ports)
908 return -EINVAL;
909
910 len += snprintf(buf + len, sizeof(smi->buf) - len,
911 "Port %d MIB counters\n",
912 val->port_vlan);
913
914 for (i = 0; i < smi->num_mib_counters; ++i) {
915 len += snprintf(buf + len, sizeof(smi->buf) - len,
916 "%-36s: ", smi->mib_counters[i].name);
917 if (!smi->ops->get_mib_counter(smi, i, val->port_vlan,
918 &counter))
919 len += snprintf(buf + len, sizeof(smi->buf) - len,
920 "%llu\n", counter);
921 else
922 len += snprintf(buf + len, sizeof(smi->buf) - len,
923 "%s\n", "error");
924 }
925
926 val->value.s = buf;
927 val->len = len;
928 return 0;
929 }
930 EXPORT_SYMBOL_GPL(rtl8366_sw_get_port_mib);
931
932 int rtl8366_sw_get_vlan_info(struct switch_dev *dev,
933 const struct switch_attr *attr,
934 struct switch_val *val)
935 {
936 int i;
937 u32 len = 0;
938 struct rtl8366_vlan_4k vlan4k;
939 struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
940 char *buf = smi->buf;
941 int err;
942
943 if (!smi->ops->is_vlan_valid(smi, val->port_vlan))
944 return -EINVAL;
945
946 memset(buf, '\0', sizeof(smi->buf));
947
948 err = smi->ops->get_vlan_4k(smi, val->port_vlan, &vlan4k);
949 if (err)
950 return err;
951
952 len += snprintf(buf + len, sizeof(smi->buf) - len,
953 "VLAN %d: Ports: '", vlan4k.vid);
954
955 for (i = 0; i < smi->num_ports; i++) {
956 if (!(vlan4k.member & (1 << i)))
957 continue;
958
959 len += snprintf(buf + len, sizeof(smi->buf) - len, "%d%s", i,
960 (vlan4k.untag & (1 << i)) ? "" : "t");
961 }
962
963 len += snprintf(buf + len, sizeof(smi->buf) - len,
964 "', members=%04x, untag=%04x, fid=%u",
965 vlan4k.member, vlan4k.untag, vlan4k.fid);
966
967 val->value.s = buf;
968 val->len = len;
969
970 return 0;
971 }
972 EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_info);
973
974 int rtl8366_sw_get_vlan_ports(struct switch_dev *dev, struct switch_val *val)
975 {
976 struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
977 struct switch_port *port;
978 struct rtl8366_vlan_4k vlan4k;
979 int i;
980
981 if (!smi->ops->is_vlan_valid(smi, val->port_vlan))
982 return -EINVAL;
983
984 smi->ops->get_vlan_4k(smi, val->port_vlan, &vlan4k);
985
986 port = &val->value.ports[0];
987 val->len = 0;
988 for (i = 0; i < smi->num_ports; i++) {
989 if (!(vlan4k.member & BIT(i)))
990 continue;
991
992 port->id = i;
993 port->flags = (vlan4k.untag & BIT(i)) ?
994 0 : BIT(SWITCH_PORT_FLAG_TAGGED);
995 val->len++;
996 port++;
997 }
998 return 0;
999 }
1000 EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_ports);
1001
1002 int rtl8366_sw_set_vlan_ports(struct switch_dev *dev, struct switch_val *val)
1003 {
1004 struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
1005 struct switch_port *port;
1006 u32 member = 0;
1007 u32 untag = 0;
1008 int err;
1009 int i;
1010
1011 if (!smi->ops->is_vlan_valid(smi, val->port_vlan))
1012 return -EINVAL;
1013
1014 port = &val->value.ports[0];
1015 for (i = 0; i < val->len; i++, port++) {
1016 member |= BIT(port->id);
1017
1018 if (!(port->flags & BIT(SWITCH_PORT_FLAG_TAGGED)))
1019 untag |= BIT(port->id);
1020
1021 /*
1022 * To ensure that we have a valid MC entry for this VLAN,
1023 * initialize the port VLAN ID here.
1024 */
1025 err = rtl8366_set_pvid(smi, port->id, val->port_vlan);
1026 if (err < 0)
1027 return err;
1028 }
1029
1030 return rtl8366_set_vlan(smi, val->port_vlan, member, untag, 0);
1031 }
1032 EXPORT_SYMBOL_GPL(rtl8366_sw_set_vlan_ports);
1033
1034 int rtl8366_sw_get_vlan_fid(struct switch_dev *dev,
1035 const struct switch_attr *attr,
1036 struct switch_val *val)
1037 {
1038 struct rtl8366_vlan_4k vlan4k;
1039 struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
1040 int err;
1041
1042 if (!smi->ops->is_vlan_valid(smi, val->port_vlan))
1043 return -EINVAL;
1044
1045 err = smi->ops->get_vlan_4k(smi, val->port_vlan, &vlan4k);
1046 if (err)
1047 return err;
1048
1049 val->value.i = vlan4k.fid;
1050
1051 return 0;
1052 }
1053 EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_fid);
1054
1055 int rtl8366_sw_set_vlan_fid(struct switch_dev *dev,
1056 const struct switch_attr *attr,
1057 struct switch_val *val)
1058 {
1059 struct rtl8366_vlan_4k vlan4k;
1060 struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
1061 int err;
1062
1063 if (!smi->ops->is_vlan_valid(smi, val->port_vlan))
1064 return -EINVAL;
1065
1066 if (val->value.i < 0 || val->value.i > attr->max)
1067 return -EINVAL;
1068
1069 err = smi->ops->get_vlan_4k(smi, val->port_vlan, &vlan4k);
1070 if (err)
1071 return err;
1072
1073 return rtl8366_set_vlan(smi, val->port_vlan,
1074 vlan4k.member,
1075 vlan4k.untag,
1076 val->value.i);
1077 }
1078 EXPORT_SYMBOL_GPL(rtl8366_sw_set_vlan_fid);
1079
1080 int rtl8366_sw_get_vlan_enable(struct switch_dev *dev,
1081 const struct switch_attr *attr,
1082 struct switch_val *val)
1083 {
1084 struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
1085
1086 if (attr->ofs > 2)
1087 return -EINVAL;
1088
1089 if (attr->ofs == 1)
1090 val->value.i = smi->vlan_enabled;
1091 else
1092 val->value.i = smi->vlan4k_enabled;
1093
1094 return 0;
1095 }
1096 EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_enable);
1097
1098 int rtl8366_sw_set_vlan_enable(struct switch_dev *dev,
1099 const struct switch_attr *attr,
1100 struct switch_val *val)
1101 {
1102 struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
1103 int err;
1104
1105 if (attr->ofs > 2)
1106 return -EINVAL;
1107
1108 if (attr->ofs == 1)
1109 err = rtl8366_enable_vlan(smi, val->value.i);
1110 else
1111 err = rtl8366_enable_vlan4k(smi, val->value.i);
1112
1113 return err;
1114 }
1115 EXPORT_SYMBOL_GPL(rtl8366_sw_set_vlan_enable);
1116
1117 struct rtl8366_smi *rtl8366_smi_alloc(struct device *parent)
1118 {
1119 struct rtl8366_smi *smi;
1120
1121 BUG_ON(!parent);
1122
1123 smi = kzalloc(sizeof(*smi), GFP_KERNEL);
1124 if (!smi) {
1125 dev_err(parent, "no memory for private data\n");
1126 return NULL;
1127 }
1128
1129 smi->parent = parent;
1130 return smi;
1131 }
1132 EXPORT_SYMBOL_GPL(rtl8366_smi_alloc);
1133
1134 int rtl8366_smi_init(struct rtl8366_smi *smi)
1135 {
1136 int err;
1137
1138 if (!smi->ops)
1139 return -EINVAL;
1140
1141 err = gpio_request(smi->gpio_sda, dev_name(smi->parent));
1142 if (err) {
1143 dev_err(smi->parent, "gpio_request failed for %u, err=%d\n",
1144 smi->gpio_sda, err);
1145 goto err_out;
1146 }
1147
1148 err = gpio_request(smi->gpio_sck, dev_name(smi->parent));
1149 if (err) {
1150 dev_err(smi->parent, "gpio_request failed for %u, err=%d\n",
1151 smi->gpio_sck, err);
1152 goto err_free_sda;
1153 }
1154
1155 spin_lock_init(&smi->lock);
1156
1157 dev_info(smi->parent, "using GPIO pins %u (SDA) and %u (SCK)\n",
1158 smi->gpio_sda, smi->gpio_sck);
1159
1160 err = smi->ops->detect(smi);
1161 if (err) {
1162 dev_err(smi->parent, "chip detection failed, err=%d\n", err);
1163 goto err_free_sck;
1164 }
1165
1166 err = smi->ops->setup(smi);
1167 if (err) {
1168 dev_err(smi->parent, "chip setup failed, err=%d\n", err);
1169 goto err_free_sck;
1170 }
1171
1172 err = rtl8366_smi_mii_init(smi);
1173 if (err)
1174 goto err_free_sck;
1175
1176 rtl8366_debugfs_init(smi);
1177
1178 return 0;
1179
1180 err_free_sck:
1181 gpio_free(smi->gpio_sck);
1182 err_free_sda:
1183 gpio_free(smi->gpio_sda);
1184 err_out:
1185 return err;
1186 }
1187 EXPORT_SYMBOL_GPL(rtl8366_smi_init);
1188
1189 void rtl8366_smi_cleanup(struct rtl8366_smi *smi)
1190 {
1191 rtl8366_debugfs_remove(smi);
1192 rtl8366_smi_mii_cleanup(smi);
1193 gpio_free(smi->gpio_sck);
1194 gpio_free(smi->gpio_sda);
1195 }
1196 EXPORT_SYMBOL_GPL(rtl8366_smi_cleanup);
1197
1198 MODULE_DESCRIPTION("Realtek RTL8366 SMI interface driver");
1199 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
1200 MODULE_LICENSE("GPL v2");
This page took 0.11248 seconds and 5 git commands to generate.