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