ramips: don't read RAMIPS_RX_CALC_IDX0 register in the loop
[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_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_RTL8366S_PHY_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_RTL8366S_PHY_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_get_port_pvid(struct switch_dev *dev, int port, int *val)
937 {
938 struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
939 return rtl8366_get_pvid(smi, port, val);
940 }
941 EXPORT_SYMBOL_GPL(rtl8366_sw_get_port_pvid);
942
943 int rtl8366_sw_set_port_pvid(struct switch_dev *dev, int port, int val)
944 {
945 struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
946 return rtl8366_set_pvid(smi, port, val);
947 }
948 EXPORT_SYMBOL_GPL(rtl8366_sw_set_port_pvid);
949
950 int rtl8366_sw_get_port_mib(struct switch_dev *dev,
951 const struct switch_attr *attr,
952 struct switch_val *val)
953 {
954 struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
955 int i, len = 0;
956 unsigned long long counter = 0;
957 char *buf = smi->buf;
958
959 if (val->port_vlan >= smi->num_ports)
960 return -EINVAL;
961
962 len += snprintf(buf + len, sizeof(smi->buf) - len,
963 "Port %d MIB counters\n",
964 val->port_vlan);
965
966 for (i = 0; i < smi->num_mib_counters; ++i) {
967 len += snprintf(buf + len, sizeof(smi->buf) - len,
968 "%-36s: ", smi->mib_counters[i].name);
969 if (!smi->ops->get_mib_counter(smi, i, val->port_vlan,
970 &counter))
971 len += snprintf(buf + len, sizeof(smi->buf) - len,
972 "%llu\n", counter);
973 else
974 len += snprintf(buf + len, sizeof(smi->buf) - len,
975 "%s\n", "error");
976 }
977
978 val->value.s = buf;
979 val->len = len;
980 return 0;
981 }
982 EXPORT_SYMBOL_GPL(rtl8366_sw_get_port_mib);
983
984 int rtl8366_sw_get_vlan_info(struct switch_dev *dev,
985 const struct switch_attr *attr,
986 struct switch_val *val)
987 {
988 int i;
989 u32 len = 0;
990 struct rtl8366_vlan_4k vlan4k;
991 struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
992 char *buf = smi->buf;
993 int err;
994
995 if (!smi->ops->is_vlan_valid(smi, val->port_vlan))
996 return -EINVAL;
997
998 memset(buf, '\0', sizeof(smi->buf));
999
1000 err = smi->ops->get_vlan_4k(smi, val->port_vlan, &vlan4k);
1001 if (err)
1002 return err;
1003
1004 len += snprintf(buf + len, sizeof(smi->buf) - len,
1005 "VLAN %d: Ports: '", vlan4k.vid);
1006
1007 for (i = 0; i < smi->num_ports; i++) {
1008 if (!(vlan4k.member & (1 << i)))
1009 continue;
1010
1011 len += snprintf(buf + len, sizeof(smi->buf) - len, "%d%s", i,
1012 (vlan4k.untag & (1 << i)) ? "" : "t");
1013 }
1014
1015 len += snprintf(buf + len, sizeof(smi->buf) - len,
1016 "', members=%04x, untag=%04x, fid=%u",
1017 vlan4k.member, vlan4k.untag, vlan4k.fid);
1018
1019 val->value.s = buf;
1020 val->len = len;
1021
1022 return 0;
1023 }
1024 EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_info);
1025
1026 int rtl8366_sw_get_vlan_ports(struct switch_dev *dev, struct switch_val *val)
1027 {
1028 struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
1029 struct switch_port *port;
1030 struct rtl8366_vlan_4k vlan4k;
1031 int i;
1032
1033 if (!smi->ops->is_vlan_valid(smi, val->port_vlan))
1034 return -EINVAL;
1035
1036 smi->ops->get_vlan_4k(smi, val->port_vlan, &vlan4k);
1037
1038 port = &val->value.ports[0];
1039 val->len = 0;
1040 for (i = 0; i < smi->num_ports; i++) {
1041 if (!(vlan4k.member & BIT(i)))
1042 continue;
1043
1044 port->id = i;
1045 port->flags = (vlan4k.untag & BIT(i)) ?
1046 0 : BIT(SWITCH_PORT_FLAG_TAGGED);
1047 val->len++;
1048 port++;
1049 }
1050 return 0;
1051 }
1052 EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_ports);
1053
1054 int rtl8366_sw_set_vlan_ports(struct switch_dev *dev, struct switch_val *val)
1055 {
1056 struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
1057 struct switch_port *port;
1058 u32 member = 0;
1059 u32 untag = 0;
1060 int err;
1061 int i;
1062
1063 if (!smi->ops->is_vlan_valid(smi, val->port_vlan))
1064 return -EINVAL;
1065
1066 port = &val->value.ports[0];
1067 for (i = 0; i < val->len; i++, port++) {
1068 member |= BIT(port->id);
1069
1070 if (!(port->flags & BIT(SWITCH_PORT_FLAG_TAGGED)))
1071 untag |= BIT(port->id);
1072
1073 /*
1074 * To ensure that we have a valid MC entry for this VLAN,
1075 * initialize the port VLAN ID here.
1076 */
1077 err = rtl8366_set_pvid(smi, port->id, val->port_vlan);
1078 if (err < 0)
1079 return err;
1080 }
1081
1082 return rtl8366_set_vlan(smi, val->port_vlan, member, untag, 0);
1083 }
1084 EXPORT_SYMBOL_GPL(rtl8366_sw_set_vlan_ports);
1085
1086 int rtl8366_sw_get_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 err = smi->ops->get_vlan_4k(smi, val->port_vlan, &vlan4k);
1098 if (err)
1099 return err;
1100
1101 val->value.i = vlan4k.fid;
1102
1103 return 0;
1104 }
1105 EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_fid);
1106
1107 int rtl8366_sw_set_vlan_fid(struct switch_dev *dev,
1108 const struct switch_attr *attr,
1109 struct switch_val *val)
1110 {
1111 struct rtl8366_vlan_4k vlan4k;
1112 struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
1113 int err;
1114
1115 if (!smi->ops->is_vlan_valid(smi, val->port_vlan))
1116 return -EINVAL;
1117
1118 if (val->value.i < 0 || val->value.i > attr->max)
1119 return -EINVAL;
1120
1121 err = smi->ops->get_vlan_4k(smi, val->port_vlan, &vlan4k);
1122 if (err)
1123 return err;
1124
1125 return rtl8366_set_vlan(smi, val->port_vlan,
1126 vlan4k.member,
1127 vlan4k.untag,
1128 val->value.i);
1129 }
1130 EXPORT_SYMBOL_GPL(rtl8366_sw_set_vlan_fid);
1131
1132 int rtl8366_sw_get_vlan_enable(struct switch_dev *dev,
1133 const struct switch_attr *attr,
1134 struct switch_val *val)
1135 {
1136 struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
1137
1138 if (attr->ofs > 2)
1139 return -EINVAL;
1140
1141 if (attr->ofs == 1)
1142 val->value.i = smi->vlan_enabled;
1143 else
1144 val->value.i = smi->vlan4k_enabled;
1145
1146 return 0;
1147 }
1148 EXPORT_SYMBOL_GPL(rtl8366_sw_get_vlan_enable);
1149
1150 int rtl8366_sw_set_vlan_enable(struct switch_dev *dev,
1151 const struct switch_attr *attr,
1152 struct switch_val *val)
1153 {
1154 struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
1155 int err;
1156
1157 if (attr->ofs > 2)
1158 return -EINVAL;
1159
1160 if (attr->ofs == 1)
1161 err = rtl8366_enable_vlan(smi, val->value.i);
1162 else
1163 err = rtl8366_enable_vlan4k(smi, val->value.i);
1164
1165 return err;
1166 }
1167 EXPORT_SYMBOL_GPL(rtl8366_sw_set_vlan_enable);
1168
1169 struct rtl8366_smi *rtl8366_smi_alloc(struct device *parent)
1170 {
1171 struct rtl8366_smi *smi;
1172
1173 BUG_ON(!parent);
1174
1175 smi = kzalloc(sizeof(*smi), GFP_KERNEL);
1176 if (!smi) {
1177 dev_err(parent, "no memory for private data\n");
1178 return NULL;
1179 }
1180
1181 smi->parent = parent;
1182 return smi;
1183 }
1184 EXPORT_SYMBOL_GPL(rtl8366_smi_alloc);
1185
1186 static int __rtl8366_smi_init(struct rtl8366_smi *smi, const char *name)
1187 {
1188 int err;
1189
1190 err = gpio_request(smi->gpio_sda, name);
1191 if (err) {
1192 printk(KERN_ERR "rtl8366_smi: gpio_request failed for %u, err=%d\n",
1193 smi->gpio_sda, err);
1194 goto err_out;
1195 }
1196
1197 err = gpio_request(smi->gpio_sck, name);
1198 if (err) {
1199 printk(KERN_ERR "rtl8366_smi: gpio_request failed for %u, err=%d\n",
1200 smi->gpio_sck, err);
1201 goto err_free_sda;
1202 }
1203
1204 spin_lock_init(&smi->lock);
1205 return 0;
1206
1207 err_free_sda:
1208 gpio_free(smi->gpio_sda);
1209 err_out:
1210 return err;
1211 }
1212
1213 static void __rtl8366_smi_cleanup(struct rtl8366_smi *smi)
1214 {
1215 gpio_free(smi->gpio_sck);
1216 gpio_free(smi->gpio_sda);
1217 }
1218
1219 enum rtl8366_type rtl8366_smi_detect(struct rtl8366_platform_data *pdata)
1220 {
1221 static struct rtl8366_smi smi;
1222 enum rtl8366_type type = RTL8366_TYPE_UNKNOWN;
1223 u32 reg = 0;
1224
1225 memset(&smi, 0, sizeof(smi));
1226 smi.gpio_sda = pdata->gpio_sda;
1227 smi.gpio_sck = pdata->gpio_sck;
1228 smi.clk_delay = 10;
1229 smi.cmd_read = 0xa9;
1230 smi.cmd_write = 0xa8;
1231
1232 if (__rtl8366_smi_init(&smi, "rtl8366"))
1233 goto out;
1234
1235 if (rtl8366_smi_read_reg(&smi, 0x5c, &reg))
1236 goto cleanup;
1237
1238 switch(reg) {
1239 case 0x6027:
1240 printk("Found an RTL8366S switch\n");
1241 type = RTL8366_TYPE_S;
1242 break;
1243 case 0x5937:
1244 printk("Found an RTL8366RB switch\n");
1245 type = RTL8366_TYPE_RB;
1246 break;
1247 default:
1248 printk("Found an Unknown RTL8366 switch (id=0x%04x)\n", reg);
1249 break;
1250 }
1251
1252 cleanup:
1253 __rtl8366_smi_cleanup(&smi);
1254 out:
1255 return type;
1256 }
1257
1258 int rtl8366_smi_init(struct rtl8366_smi *smi)
1259 {
1260 int err;
1261
1262 if (!smi->ops)
1263 return -EINVAL;
1264
1265 err = __rtl8366_smi_init(smi, dev_name(smi->parent));
1266 if (err)
1267 goto err_out;
1268
1269 spin_lock_init(&smi->lock);
1270
1271 dev_info(smi->parent, "using GPIO pins %u (SDA) and %u (SCK)\n",
1272 smi->gpio_sda, smi->gpio_sck);
1273
1274 err = smi->ops->detect(smi);
1275 if (err) {
1276 dev_err(smi->parent, "chip detection failed, err=%d\n", err);
1277 goto err_free_sck;
1278 }
1279
1280 err = smi->ops->setup(smi);
1281 if (err) {
1282 dev_err(smi->parent, "chip setup failed, err=%d\n", err);
1283 goto err_free_sck;
1284 }
1285
1286 err = rtl8366_init_vlan(smi);
1287 if (err) {
1288 dev_err(smi->parent, "VLAN initialization failed, err=%d\n",
1289 err);
1290 goto err_free_sck;
1291 }
1292
1293 err = rtl8366_enable_all_ports(smi, 1);
1294 if (err)
1295 goto err_free_sck;
1296
1297 err = rtl8366_smi_mii_init(smi);
1298 if (err)
1299 goto err_free_sck;
1300
1301 rtl8366_debugfs_init(smi);
1302
1303 return 0;
1304
1305 err_free_sck:
1306 __rtl8366_smi_cleanup(smi);
1307 err_out:
1308 return err;
1309 }
1310 EXPORT_SYMBOL_GPL(rtl8366_smi_init);
1311
1312 void rtl8366_smi_cleanup(struct rtl8366_smi *smi)
1313 {
1314 rtl8366_debugfs_remove(smi);
1315 rtl8366_smi_mii_cleanup(smi);
1316 gpio_free(smi->gpio_sck);
1317 gpio_free(smi->gpio_sda);
1318 }
1319 EXPORT_SYMBOL_GPL(rtl8366_smi_cleanup);
1320
1321 MODULE_DESCRIPTION("Realtek RTL8366 SMI interface driver");
1322 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
1323 MODULE_LICENSE("GPL v2");
This page took 0.132869 seconds and 5 git commands to generate.