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