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