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