28e1a8505d4fd490c54f9bee8b0850538bd11561
[openwrt-10.03/.git] / target / linux / generic-2.6 / 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 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 EXPORT_SYMBOL_GPL(rtl8366_enable_vlan);
469
470 static int rtl8366_enable_vlan4k(struct rtl8366_smi *smi, int enable)
471 {
472         int err;
473
474         if (enable) {
475                 err = smi->ops->enable_vlan(smi, enable);
476                 if (err)
477                         return err;
478
479                 smi->vlan_enabled = enable;
480         }
481
482         err = smi->ops->enable_vlan4k(smi, enable);
483         if (err)
484                 return err;
485
486         smi->vlan4k_enabled = enable;
487         return 0;
488 }
489
490 int rtl8366_enable_all_ports(struct rtl8366_smi *smi, int enable)
491 {
492         int port;
493         int err;
494
495         for (port = 0; port < smi->num_ports; port++) {
496                 err = smi->ops->enable_port(smi, port, enable);
497                 if (err)
498                         return err;
499         }
500
501         return 0;
502 }
503 EXPORT_SYMBOL_GPL(rtl8366_enable_all_ports);
504
505 int rtl8366_reset_vlan(struct rtl8366_smi *smi)
506 {
507         struct rtl8366_vlan_mc vlanmc;
508         int err;
509         int i;
510
511         rtl8366_enable_vlan(smi, 0);
512         rtl8366_enable_vlan4k(smi, 0);
513
514         /* clear VLAN member configurations */
515         vlanmc.vid = 0;
516         vlanmc.priority = 0;
517         vlanmc.member = 0;
518         vlanmc.untag = 0;
519         vlanmc.fid = 0;
520         for (i = 0; i < smi->num_vlan_mc; i++) {
521                 err = smi->ops->set_vlan_mc(smi, i, &vlanmc);
522                 if (err)
523                         return err;
524         }
525
526         return 0;
527 }
528 EXPORT_SYMBOL_GPL(rtl8366_reset_vlan);
529
530 static int rtl8366_init_vlan(struct rtl8366_smi *smi)
531 {
532         int port;
533         int err;
534
535         err = rtl8366_reset_vlan(smi);
536         if (err)
537                 return err;
538
539         for (port = 0; port < smi->num_ports; port++) {
540                 u32 mask;
541
542                 if (port == smi->cpu_port)
543                         mask = (1 << smi->num_ports) - 1;
544                 else
545                         mask = (1 << port) | (1 << smi->cpu_port);
546
547                 err = rtl8366_set_vlan(smi, (port + 1), mask, mask, 0);
548                 if (err)
549                         return err;
550
551                 err = rtl8366_set_pvid(smi, port, (port + 1));
552                 if (err)
553                         return err;
554         }
555
556         return rtl8366_enable_vlan(smi, 1);
557 }
558
559 #ifdef CONFIG_RTL8366S_PHY_DEBUG_FS
560 int rtl8366_debugfs_open(struct inode *inode, struct file *file)
561 {
562         file->private_data = inode->i_private;
563         return 0;
564 }
565 EXPORT_SYMBOL_GPL(rtl8366_debugfs_open);
566
567 static ssize_t rtl8366_read_debugfs_vlan_mc(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         char *buf = smi->buf;
574
575         len += snprintf(buf + len, sizeof(smi->buf) - len,
576                         "%2s %6s %4s %6s %6s %3s\n",
577                         "id", "vid","prio", "member", "untag", "fid");
578
579         for (i = 0; i < smi->num_vlan_mc; ++i) {
580                 struct rtl8366_vlan_mc vlanmc;
581
582                 smi->ops->get_vlan_mc(smi, i, &vlanmc);
583
584                 len += snprintf(buf + len, sizeof(smi->buf) - len,
585                                 "%2d %6d %4d 0x%04x 0x%04x %3d\n",
586                                 i, vlanmc.vid, vlanmc.priority,
587                                 vlanmc.member, vlanmc.untag, vlanmc.fid);
588         }
589
590         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
591 }
592
593 #define RTL8366_VLAN4K_PAGE_SIZE        64
594 #define RTL8366_VLAN4K_NUM_PAGES        (4096 / RTL8366_VLAN4K_PAGE_SIZE)
595
596 static ssize_t rtl8366_read_debugfs_vlan_4k(struct file *file,
597                                             char __user *user_buf,
598                                             size_t count, loff_t *ppos)
599 {
600         struct rtl8366_smi *smi = (struct rtl8366_smi *)file->private_data;
601         int i, len = 0;
602         int offset;
603         char *buf = smi->buf;
604
605         if (smi->dbg_vlan_4k_page >= RTL8366_VLAN4K_NUM_PAGES) {
606                 len += snprintf(buf + len, sizeof(smi->buf) - len,
607                                 "invalid page: %u\n", smi->dbg_vlan_4k_page);
608                 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
609         }
610
611         len += snprintf(buf + len, sizeof(smi->buf) - len,
612                         "%4s %6s %6s %3s\n",
613                         "vid", "member", "untag", "fid");
614
615         offset = RTL8366_VLAN4K_PAGE_SIZE * smi->dbg_vlan_4k_page;
616         for (i = 0; i < RTL8366_VLAN4K_PAGE_SIZE; i++) {
617                 struct rtl8366_vlan_4k vlan4k;
618
619                 smi->ops->get_vlan_4k(smi, offset + i, &vlan4k);
620
621                 len += snprintf(buf + len, sizeof(smi->buf) - len,
622                                 "%4d 0x%04x 0x%04x %3d\n",
623                                 vlan4k.vid, vlan4k.member,
624                                 vlan4k.untag, vlan4k.fid);
625         }
626
627         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
628 }
629
630 static ssize_t rtl8366_read_debugfs_pvid(struct file *file,
631                                          char __user *user_buf,
632                                          size_t count, loff_t *ppos)
633 {
634         struct rtl8366_smi *smi = (struct rtl8366_smi *)file->private_data;
635         char *buf = smi->buf;
636         int len = 0;
637         int i;
638
639         len += snprintf(buf + len, sizeof(smi->buf) - len, "%4s %4s\n",
640                         "port", "pvid");
641
642         for (i = 0; i < smi->num_ports; i++) {
643                 int pvid;
644                 int err;
645
646                 err = rtl8366_get_pvid(smi, i, &pvid);
647                 if (err)
648                         len += snprintf(buf + len, sizeof(smi->buf) - len,
649                                 "%4d error\n", i);
650                 else
651                         len += snprintf(buf + len, sizeof(smi->buf) - len,
652                                 "%4d %4d\n", i, pvid);
653         }
654
655         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
656 }
657
658 static ssize_t rtl8366_read_debugfs_reg(struct file *file,
659                                          char __user *user_buf,
660                                          size_t count, loff_t *ppos)
661 {
662         struct rtl8366_smi *smi = (struct rtl8366_smi *)file->private_data;
663         u32 t, reg = smi->dbg_reg;
664         int err, len = 0;
665         char *buf = smi->buf;
666
667         memset(buf, '\0', sizeof(smi->buf));
668
669         err = rtl8366_smi_read_reg(smi, reg, &t);
670         if (err) {
671                 len += snprintf(buf, sizeof(smi->buf),
672                                 "Read failed (reg: 0x%04x)\n", reg);
673                 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
674         }
675
676         len += snprintf(buf, sizeof(smi->buf), "reg = 0x%04x, val = 0x%04x\n",
677                         reg, t);
678
679         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
680 }
681
682 static ssize_t rtl8366_write_debugfs_reg(struct file *file,
683                                           const char __user *user_buf,
684                                           size_t count, loff_t *ppos)
685 {
686         struct rtl8366_smi *smi = (struct rtl8366_smi *)file->private_data;
687         unsigned long data;
688         u32 reg = smi->dbg_reg;
689         int err;
690         size_t len;
691         char *buf = smi->buf;
692
693         len = min(count, sizeof(smi->buf) - 1);
694         if (copy_from_user(buf, user_buf, len)) {
695                 dev_err(smi->parent, "copy from user failed\n");
696                 return -EFAULT;
697         }
698
699         buf[len] = '\0';
700         if (len > 0 && buf[len - 1] == '\n')
701                 buf[len - 1] = '\0';
702
703
704         if (strict_strtoul(buf, 16, &data)) {
705                 dev_err(smi->parent, "Invalid reg value %s\n", buf);
706         } else {
707                 err = rtl8366_smi_write_reg(smi, reg, data);
708                 if (err) {
709                         dev_err(smi->parent,
710                                 "writing reg 0x%04x val 0x%04lx failed\n",
711                                 reg, data);
712                 }
713         }
714
715         return count;
716 }
717
718 static ssize_t rtl8366_read_debugfs_mibs(struct file *file,
719                                          char __user *user_buf,
720                                          size_t count, loff_t *ppos)
721 {
722         struct rtl8366_smi *smi = file->private_data;
723         int i, j, len = 0;
724         char *buf = smi->buf;
725
726         len += snprintf(buf + len, sizeof(smi->buf) - len, "%-36s",
727                         "Counter");
728
729         for (i = 0; i < smi->num_ports; i++) {
730                 char port_buf[10];
731
732                 snprintf(port_buf, sizeof(port_buf), "Port %d", i);
733                 len += snprintf(buf + len, sizeof(smi->buf) - len, " %12s",
734                                 port_buf);
735         }
736         len += snprintf(buf + len, sizeof(smi->buf) - len, "\n");
737
738         for (i = 0; i < smi->num_mib_counters; i++) {
739                 len += snprintf(buf + len, sizeof(smi->buf) - len, "%-36s ",
740                                 smi->mib_counters[i].name);
741                 for (j = 0; j < smi->num_ports; j++) {
742                         unsigned long long counter = 0;
743
744                         if (!smi->ops->get_mib_counter(smi, i, j, &counter))
745                                 len += snprintf(buf + len,
746                                                 sizeof(smi->buf) - len,
747                                                 "%12llu ", counter);
748                         else
749                                 len += snprintf(buf + len,
750                                                 sizeof(smi->buf) - len,
751                                                 "%12s ", "error");
752                 }
753                 len += snprintf(buf + len, sizeof(smi->buf) - len, "\n");
754         }
755
756         return simple_read_from_buffer(user_buf, count, ppos, buf, len);
757 }
758
759 static const struct file_operations fops_rtl8366_regs = {
760         .read   = rtl8366_read_debugfs_reg,
761         .write  = rtl8366_write_debugfs_reg,
762         .open   = rtl8366_debugfs_open,
763         .owner  = THIS_MODULE
764 };
765
766 static const struct file_operations fops_rtl8366_vlan_mc = {
767         .read   = rtl8366_read_debugfs_vlan_mc,
768         .open   = rtl8366_debugfs_open,
769         .owner  = THIS_MODULE
770 };
771
772 static const struct file_operations fops_rtl8366_vlan_4k = {
773         .read   = rtl8366_read_debugfs_vlan_4k,
774         .open   = rtl8366_debugfs_open,
775         .owner  = THIS_MODULE
776 };
777
778 static const struct file_operations fops_rtl8366_pvid = {
779         .read   = rtl8366_read_debugfs_pvid,
780         .open   = rtl8366_debugfs_open,
781         .owner  = THIS_MODULE
782 };
783
784 static const struct file_operations fops_rtl8366_mibs = {
785         .read = rtl8366_read_debugfs_mibs,
786         .open = rtl8366_debugfs_open,
787         .owner = THIS_MODULE
788 };
789
790 static void rtl8366_debugfs_init(struct rtl8366_smi *smi)
791 {
792         struct dentry *node;
793         struct dentry *root;
794
795         if (!smi->debugfs_root)
796                 smi->debugfs_root = debugfs_create_dir(dev_name(smi->parent),
797                                                        NULL);
798
799         if (!smi->debugfs_root) {
800                 dev_err(smi->parent, "Unable to create debugfs dir\n");
801                 return;
802         }
803         root = smi->debugfs_root;
804
805         node = debugfs_create_x16("reg", S_IRUGO | S_IWUSR, root,
806                                   &smi->dbg_reg);
807         if (!node) {
808                 dev_err(smi->parent, "Creating debugfs file '%s' failed\n",
809                         "reg");
810                 return;
811         }
812
813         node = debugfs_create_file("val", S_IRUGO | S_IWUSR, root, smi,
814                                    &fops_rtl8366_regs);
815         if (!node) {
816                 dev_err(smi->parent, "Creating debugfs file '%s' failed\n",
817                         "val");
818                 return;
819         }
820
821         node = debugfs_create_file("vlan_mc", S_IRUSR, root, smi,
822                                    &fops_rtl8366_vlan_mc);
823         if (!node) {
824                 dev_err(smi->parent, "Creating debugfs file '%s' failed\n",
825                         "vlan_mc");
826                 return;
827         }
828
829         node = debugfs_create_u8("vlan_4k_page", S_IRUGO | S_IWUSR, root,
830                                   &smi->dbg_vlan_4k_page);
831         if (!node) {
832                 dev_err(smi->parent, "Creating debugfs file '%s' failed\n",
833                         "vlan_4k_page");
834                 return;
835         }
836
837         node = debugfs_create_file("vlan_4k", S_IRUSR, root, smi,
838                                    &fops_rtl8366_vlan_4k);
839         if (!node) {
840                 dev_err(smi->parent, "Creating debugfs file '%s' failed\n",
841                         "vlan_4k");
842                 return;
843         }
844
845         node = debugfs_create_file("pvid", S_IRUSR, root, smi,
846                                    &fops_rtl8366_pvid);
847         if (!node) {
848                 dev_err(smi->parent, "Creating debugfs file '%s' failed\n",
849                         "pvid");
850                 return;
851         }
852
853         node = debugfs_create_file("mibs", S_IRUSR, smi->debugfs_root, smi,
854                                    &fops_rtl8366_mibs);
855         if (!node)
856                 dev_err(smi->parent, "Creating debugfs file '%s' failed\n",
857                         "mibs");
858 }
859
860 static void rtl8366_debugfs_remove(struct rtl8366_smi *smi)
861 {
862         if (smi->debugfs_root) {
863                 debugfs_remove_recursive(smi->debugfs_root);
864                 smi->debugfs_root = NULL;
865         }
866 }
867 #else
868 static inline void rtl8366_debugfs_init(struct rtl8366_smi *smi) {}
869 static inline void rtl8366_debugfs_remove(struct rtl8366_smi *smi) {}
870 #endif /* CONFIG_RTL8366S_PHY_DEBUG_FS */
871
872 static int rtl8366_smi_mii_init(struct rtl8366_smi *smi)
873 {
874         int ret;
875         int i;
876
877         smi->mii_bus = mdiobus_alloc();
878         if (smi->mii_bus == NULL) {
879                 ret = -ENOMEM;
880                 goto err;
881         }
882
883         smi->mii_bus->priv = (void *) smi;
884         smi->mii_bus->name = dev_name(smi->parent);
885         smi->mii_bus->read = smi->ops->mii_read;
886         smi->mii_bus->write = smi->ops->mii_write;
887         snprintf(smi->mii_bus->id, MII_BUS_ID_SIZE, "%s",
888                  dev_name(smi->parent));
889         smi->mii_bus->parent = smi->parent;
890         smi->mii_bus->phy_mask = ~(0x1f);
891         smi->mii_bus->irq = smi->mii_irq;
892         for (i = 0; i < PHY_MAX_ADDR; i++)
893                 smi->mii_irq[i] = PHY_POLL;
894
895         ret = mdiobus_register(smi->mii_bus);
896         if (ret)
897                 goto err_free;
898
899         return 0;
900
901  err_free:
902         mdiobus_free(smi->mii_bus);
903  err:
904         return ret;
905 }
906
907 static void rtl8366_smi_mii_cleanup(struct rtl8366_smi *smi)
908 {
909         mdiobus_unregister(smi->mii_bus);
910         mdiobus_free(smi->mii_bus);
911 }
912
913 int rtl8366_sw_get_port_pvid(struct switch_dev *dev, int port, int *val)
914 {
915         struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
916         return rtl8366_get_pvid(smi, port, val);
917 }
918 EXPORT_SYMBOL_GPL(rtl8366_sw_get_port_pvid);
919
920 int rtl8366_sw_set_port_pvid(struct switch_dev *dev, int port, int val)
921 {
922         struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
923         return rtl8366_set_pvid(smi, port, val);
924 }
925 EXPORT_SYMBOL_GPL(rtl8366_sw_set_port_pvid);
926
927 int rtl8366_sw_get_port_mib(struct switch_dev *dev,
928                             const struct switch_attr *attr,
929                             struct switch_val *val)
930 {
931         struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
932         int i, len = 0;
933         unsigned long long counter = 0;
934         char *buf = smi->buf;
935
936         if (val->port_vlan >= smi->num_ports)
937                 return -EINVAL;
938
939         len += snprintf(buf + len, sizeof(smi->buf) - len,
940                         "Port %d MIB counters\n",
941                         val->port_vlan);
942
943         for (i = 0; i < smi->num_mib_counters; ++i) {
944                 len += snprintf(buf + len, sizeof(smi->buf) - len,
945                                 "%-36s: ", smi->mib_counters[i].name);
946                 if (!smi->ops->get_mib_counter(smi, i, val->port_vlan,
947                                                &counter))
948                         len += snprintf(buf + len, sizeof(smi->buf) - len,
949                                         "%llu\n", counter);
950                 else
951                         len += snprintf(buf + len, sizeof(smi->buf) - len,
952                                         "%s\n", "error");
953         }
954
955         val->value.s = buf;
956         val->len = len;
957         return 0;
958 }
959 EXPORT_SYMBOL_GPL(rtl8366_sw_get_port_mib);
960
961 int rtl8366_sw_get_vlan_info(struct switch_dev *dev,
962                              const struct switch_attr *attr,
963                              struct switch_val *val)
964 {
965         int i;
966         u32 len = 0;
967         struct rtl8366_vlan_4k vlan4k;
968         struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
969         char *buf = smi->buf;
970         int err;
971
972         if (!smi->ops->is_vlan_valid(smi, val->port_vlan))
973                 return -EINVAL;
974
975         memset(buf, '\0', sizeof(smi->buf));
976
977         err = smi->ops->get_vlan_4k(smi, val->port_vlan, &vlan4k);
978         if (err)
979                 return err;
980
981         len += snprintf(buf + len, sizeof(smi->buf) - len,
982                         "VLAN %d: Ports: '", vlan4k.vid);
983
984         for (i = 0; i < smi->num_ports; i++) {
985                 if (!(vlan4k.member & (1 << i)))
986                         continue;
987
988                 len += snprintf(buf + len, sizeof(smi->buf) - len, "%d%s", i,
989                                 (vlan4k.untag & (1 << i)) ? "" : "t");
990         }
991
992         len += snprintf(buf + len, sizeof(smi->buf) - len,
993                         "', members=%04x, untag=%04x, fid=%u",
994                         vlan4k.member, vlan4k.untag, vlan4k.fid);
995
996         val->value.s = buf;
997         val->len = len;
998
999         return 0;
1000 }
1001 EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_info);
1002
1003 int rtl8366_sw_get_vlan_ports(struct switch_dev *dev, struct switch_val *val)
1004 {
1005         struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
1006         struct switch_port *port;
1007         struct rtl8366_vlan_4k vlan4k;
1008         int i;
1009
1010         if (!smi->ops->is_vlan_valid(smi, val->port_vlan))
1011                 return -EINVAL;
1012
1013         smi->ops->get_vlan_4k(smi, val->port_vlan, &vlan4k);
1014
1015         port = &val->value.ports[0];
1016         val->len = 0;
1017         for (i = 0; i < smi->num_ports; i++) {
1018                 if (!(vlan4k.member & BIT(i)))
1019                         continue;
1020
1021                 port->id = i;
1022                 port->flags = (vlan4k.untag & BIT(i)) ?
1023                                         0 : BIT(SWITCH_PORT_FLAG_TAGGED);
1024                 val->len++;
1025                 port++;
1026         }
1027         return 0;
1028 }
1029 EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_ports);
1030
1031 int rtl8366_sw_set_vlan_ports(struct switch_dev *dev, struct switch_val *val)
1032 {
1033         struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
1034         struct switch_port *port;
1035         u32 member = 0;
1036         u32 untag = 0;
1037         int err;
1038         int i;
1039
1040         if (!smi->ops->is_vlan_valid(smi, val->port_vlan))
1041                 return -EINVAL;
1042
1043         port = &val->value.ports[0];
1044         for (i = 0; i < val->len; i++, port++) {
1045                 member |= BIT(port->id);
1046
1047                 if (!(port->flags & BIT(SWITCH_PORT_FLAG_TAGGED)))
1048                         untag |= BIT(port->id);
1049
1050                 /*
1051                  * To ensure that we have a valid MC entry for this VLAN,
1052                  * initialize the port VLAN ID here.
1053                  */
1054                 err = rtl8366_set_pvid(smi, port->id, val->port_vlan);
1055                 if (err < 0)
1056                         return err;
1057         }
1058
1059         return rtl8366_set_vlan(smi, val->port_vlan, member, untag, 0);
1060 }
1061 EXPORT_SYMBOL_GPL(rtl8366_sw_set_vlan_ports);
1062
1063 int rtl8366_sw_get_vlan_fid(struct switch_dev *dev,
1064                             const struct switch_attr *attr,
1065                             struct switch_val *val)
1066 {
1067         struct rtl8366_vlan_4k vlan4k;
1068         struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
1069         int err;
1070
1071         if (!smi->ops->is_vlan_valid(smi, val->port_vlan))
1072                 return -EINVAL;
1073
1074         err = smi->ops->get_vlan_4k(smi, val->port_vlan, &vlan4k);
1075         if (err)
1076                 return err;
1077
1078         val->value.i = vlan4k.fid;
1079
1080         return 0;
1081 }
1082 EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_fid);
1083
1084 int rtl8366_sw_set_vlan_fid(struct switch_dev *dev,
1085                             const struct switch_attr *attr,
1086                             struct switch_val *val)
1087 {
1088         struct rtl8366_vlan_4k vlan4k;
1089         struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
1090         int err;
1091
1092         if (!smi->ops->is_vlan_valid(smi, val->port_vlan))
1093                 return -EINVAL;
1094
1095         if (val->value.i < 0 || val->value.i > attr->max)
1096                 return -EINVAL;
1097
1098         err = smi->ops->get_vlan_4k(smi, val->port_vlan, &vlan4k);
1099         if (err)
1100                 return err;
1101
1102         return rtl8366_set_vlan(smi, val->port_vlan,
1103                                 vlan4k.member,
1104                                 vlan4k.untag,
1105                                 val->value.i);
1106 }
1107 EXPORT_SYMBOL_GPL(rtl8366_sw_set_vlan_fid);
1108
1109 int rtl8366_sw_get_vlan_enable(struct switch_dev *dev,
1110                                const struct switch_attr *attr,
1111                                struct switch_val *val)
1112 {
1113         struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);