generic: rtl8366: move buf to struct rtl8366_smi
[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
19 #include "rtl8366_smi.h"
20
21 #define RTL8366_SMI_ACK_RETRY_COUNT 5
22 #define RTL8366_SMI_CLK_DELAY 10 /* nsec */
23
24 static inline void rtl8366_smi_clk_delay(struct rtl8366_smi *smi)
25 {
26 ndelay(RTL8366_SMI_CLK_DELAY);
27 }
28
29 static void rtl8366_smi_start(struct rtl8366_smi *smi)
30 {
31 unsigned int sda = smi->gpio_sda;
32 unsigned int sck = smi->gpio_sck;
33
34 /*
35 * Set GPIO pins to output mode, with initial state:
36 * SCK = 0, SDA = 1
37 */
38 gpio_direction_output(sck, 0);
39 gpio_direction_output(sda, 1);
40 rtl8366_smi_clk_delay(smi);
41
42 /* CLK 1: 0 -> 1, 1 -> 0 */
43 gpio_set_value(sck, 1);
44 rtl8366_smi_clk_delay(smi);
45 gpio_set_value(sck, 0);
46 rtl8366_smi_clk_delay(smi);
47
48 /* CLK 2: */
49 gpio_set_value(sck, 1);
50 rtl8366_smi_clk_delay(smi);
51 gpio_set_value(sda, 0);
52 rtl8366_smi_clk_delay(smi);
53 gpio_set_value(sck, 0);
54 rtl8366_smi_clk_delay(smi);
55 gpio_set_value(sda, 1);
56 }
57
58 static void rtl8366_smi_stop(struct rtl8366_smi *smi)
59 {
60 unsigned int sda = smi->gpio_sda;
61 unsigned int sck = smi->gpio_sck;
62
63 rtl8366_smi_clk_delay(smi);
64 gpio_set_value(sda, 0);
65 gpio_set_value(sck, 1);
66 rtl8366_smi_clk_delay(smi);
67 gpio_set_value(sda, 1);
68 rtl8366_smi_clk_delay(smi);
69 gpio_set_value(sck, 1);
70 rtl8366_smi_clk_delay(smi);
71 gpio_set_value(sck, 0);
72 rtl8366_smi_clk_delay(smi);
73 gpio_set_value(sck, 1);
74
75 /* add a click */
76 rtl8366_smi_clk_delay(smi);
77 gpio_set_value(sck, 0);
78 rtl8366_smi_clk_delay(smi);
79 gpio_set_value(sck, 1);
80
81 /* set GPIO pins to input mode */
82 gpio_direction_input(sda);
83 gpio_direction_input(sck);
84 }
85
86 static void rtl8366_smi_write_bits(struct rtl8366_smi *smi, u32 data, u32 len)
87 {
88 unsigned int sda = smi->gpio_sda;
89 unsigned int sck = smi->gpio_sck;
90
91 for (; len > 0; len--) {
92 rtl8366_smi_clk_delay(smi);
93
94 /* prepare data */
95 gpio_set_value(sda, !!(data & ( 1 << (len - 1))));
96 rtl8366_smi_clk_delay(smi);
97
98 /* clocking */
99 gpio_set_value(sck, 1);
100 rtl8366_smi_clk_delay(smi);
101 gpio_set_value(sck, 0);
102 }
103 }
104
105 static void rtl8366_smi_read_bits(struct rtl8366_smi *smi, u32 len, u32 *data)
106 {
107 unsigned int sda = smi->gpio_sda;
108 unsigned int sck = smi->gpio_sck;
109
110 gpio_direction_input(sda);
111
112 for (*data = 0; len > 0; len--) {
113 u32 u;
114
115 rtl8366_smi_clk_delay(smi);
116
117 /* clocking */
118 gpio_set_value(sck, 1);
119 rtl8366_smi_clk_delay(smi);
120 u = !!gpio_get_value(sda);
121 gpio_set_value(sck, 0);
122
123 *data |= (u << (len - 1));
124 }
125
126 gpio_direction_output(sda, 0);
127 }
128
129 static int rtl8366_smi_wait_for_ack(struct rtl8366_smi *smi)
130 {
131 int retry_cnt;
132
133 retry_cnt = 0;
134 do {
135 u32 ack;
136
137 rtl8366_smi_read_bits(smi, 1, &ack);
138 if (ack == 0)
139 break;
140
141 if (++retry_cnt > RTL8366_SMI_ACK_RETRY_COUNT)
142 return -EIO;
143 } while (1);
144
145 return 0;
146 }
147
148 static int rtl8366_smi_write_byte(struct rtl8366_smi *smi, u8 data)
149 {
150 rtl8366_smi_write_bits(smi, data, 8);
151 return rtl8366_smi_wait_for_ack(smi);
152 }
153
154 static int rtl8366_smi_read_byte0(struct rtl8366_smi *smi, u8 *data)
155 {
156 u32 t;
157
158 /* read data */
159 rtl8366_smi_read_bits(smi, 8, &t);
160 *data = (t & 0xff);
161
162 /* send an ACK */
163 rtl8366_smi_write_bits(smi, 0x00, 1);
164
165 return 0;
166 }
167
168 static int rtl8366_smi_read_byte1(struct rtl8366_smi *smi, u8 *data)
169 {
170 u32 t;
171
172 /* read data */
173 rtl8366_smi_read_bits(smi, 8, &t);
174 *data = (t & 0xff);
175
176 /* send an ACK */
177 rtl8366_smi_write_bits(smi, 0x01, 1);
178
179 return 0;
180 }
181
182 int rtl8366_smi_read_reg(struct rtl8366_smi *smi, u32 addr, u32 *data)
183 {
184 unsigned long flags;
185 u8 lo = 0;
186 u8 hi = 0;
187 int ret;
188
189 spin_lock_irqsave(&smi->lock, flags);
190
191 rtl8366_smi_start(smi);
192
193 /* send READ command */
194 ret = rtl8366_smi_write_byte(smi, 0x0a << 4 | 0x04 << 1 | 0x01);
195 if (ret)
196 goto out;
197
198 /* set ADDR[7:0] */
199 ret = rtl8366_smi_write_byte(smi, addr & 0xff);
200 if (ret)
201 goto out;
202
203 /* set ADDR[15:8] */
204 ret = rtl8366_smi_write_byte(smi, addr >> 8);
205 if (ret)
206 goto out;
207
208 /* read DATA[7:0] */
209 rtl8366_smi_read_byte0(smi, &lo);
210 /* read DATA[15:8] */
211 rtl8366_smi_read_byte1(smi, &hi);
212
213 *data = ((u32) lo) | (((u32) hi) << 8);
214
215 ret = 0;
216
217 out:
218 rtl8366_smi_stop(smi);
219 spin_unlock_irqrestore(&smi->lock, flags);
220
221 return ret;
222 }
223 EXPORT_SYMBOL_GPL(rtl8366_smi_read_reg);
224
225 int rtl8366_smi_write_reg(struct rtl8366_smi *smi, u32 addr, u32 data)
226 {
227 unsigned long flags;
228 int ret;
229
230 spin_lock_irqsave(&smi->lock, flags);
231
232 rtl8366_smi_start(smi);
233
234 /* send WRITE command */
235 ret = rtl8366_smi_write_byte(smi, 0x0a << 4 | 0x04 << 1 | 0x00);
236 if (ret)
237 goto out;
238
239 /* set ADDR[7:0] */
240 ret = rtl8366_smi_write_byte(smi, addr & 0xff);
241 if (ret)
242 goto out;
243
244 /* set ADDR[15:8] */
245 ret = rtl8366_smi_write_byte(smi, addr >> 8);
246 if (ret)
247 goto out;
248
249 /* write DATA[7:0] */
250 ret = rtl8366_smi_write_byte(smi, data & 0xff);
251 if (ret)
252 goto out;
253
254 /* write DATA[15:8] */
255 ret = rtl8366_smi_write_byte(smi, data >> 8);
256 if (ret)
257 goto out;
258
259 ret = 0;
260
261 out:
262 rtl8366_smi_stop(smi);
263 spin_unlock_irqrestore(&smi->lock, flags);
264
265 return ret;
266 }
267 EXPORT_SYMBOL_GPL(rtl8366_smi_write_reg);
268
269 int rtl8366_smi_rmwr(struct rtl8366_smi *smi, u32 addr, u32 mask, u32 data)
270 {
271 u32 t;
272 int err;
273
274 err = rtl8366_smi_read_reg(smi, addr, &t);
275 if (err)
276 return err;
277
278 err = rtl8366_smi_write_reg(smi, addr, (t & ~mask) | data);
279 return err;
280
281 }
282 EXPORT_SYMBOL_GPL(rtl8366_smi_rmwr);
283
284 static int rtl8366_mc_is_used(struct rtl8366_smi *smi, int mc_index, int *used)
285 {
286 int err;
287 int i;
288
289 *used = 0;
290 for (i = 0; i < smi->num_ports; i++) {
291 int index = 0;
292
293 err = smi->ops->get_mc_index(smi, i, &index);
294 if (err)
295 return err;
296
297 if (mc_index == index) {
298 *used = 1;
299 break;
300 }
301 }
302
303 return 0;
304 }
305
306 int rtl8366_set_vlan(struct rtl8366_smi *smi, int vid, u32 member, u32 untag,
307 u32 fid)
308 {
309 struct rtl8366_vlan_4k vlan4k;
310 int err;
311 int i;
312
313 /* Update the 4K table */
314 err = smi->ops->get_vlan_4k(smi, vid, &vlan4k);
315 if (err)
316 return err;
317
318 vlan4k.member = member;
319 vlan4k.untag = untag;
320 vlan4k.fid = fid;
321 err = smi->ops->set_vlan_4k(smi, &vlan4k);
322 if (err)
323 return err;
324
325 /* Try to find an existing MC entry for this VID */
326 for (i = 0; i < smi->num_vlan_mc; i++) {
327 struct rtl8366_vlan_mc vlanmc;
328
329 err = smi->ops->get_vlan_mc(smi, i, &vlanmc);
330 if (err)
331 return err;
332
333 if (vid == vlanmc.vid) {
334 /* update the MC entry */
335 vlanmc.member = member;
336 vlanmc.untag = untag;
337 vlanmc.fid = fid;
338
339 err = smi->ops->set_vlan_mc(smi, i, &vlanmc);
340 break;
341 }
342 }
343
344 return err;
345 }
346 EXPORT_SYMBOL_GPL(rtl8366_set_vlan);
347
348 int rtl8366_reset_vlan(struct rtl8366_smi *smi)
349 {
350 struct rtl8366_vlan_mc vlanmc;
351 int err;
352 int i;
353
354 /* clear VLAN member configurations */
355 vlanmc.vid = 0;
356 vlanmc.priority = 0;
357 vlanmc.member = 0;
358 vlanmc.untag = 0;
359 vlanmc.fid = 0;
360 for (i = 0; i < smi->num_vlan_mc; i++) {
361 err = smi->ops->set_vlan_mc(smi, i, &vlanmc);
362 if (err)
363 return err;
364 }
365
366 for (i = 0; i < smi->num_ports; i++) {
367 if (i == smi->cpu_port)
368 continue;
369
370 err = rtl8366_set_vlan(smi, (i + 1),
371 (1 << i) | (1 << smi->cpu_port),
372 (1 << i) | (1 << smi->cpu_port),
373 0);
374 if (err)
375 return err;
376
377 err = rtl8366_set_pvid(smi, i, (i + 1));
378 if (err)
379 return err;
380 }
381
382 return 0;
383 }
384 EXPORT_SYMBOL_GPL(rtl8366_reset_vlan);
385
386 int rtl8366_get_pvid(struct rtl8366_smi *smi, int port, int *val)
387 {
388 struct rtl8366_vlan_mc vlanmc;
389 int err;
390 int index;
391
392 err = smi->ops->get_mc_index(smi, port, &index);
393 if (err)
394 return err;
395
396 err = smi->ops->get_vlan_mc(smi, index, &vlanmc);
397 if (err)
398 return err;
399
400 *val = vlanmc.vid;
401 return 0;
402 }
403 EXPORT_SYMBOL_GPL(rtl8366_get_pvid);
404
405 int rtl8366_set_pvid(struct rtl8366_smi *smi, unsigned port, unsigned vid)
406 {
407 struct rtl8366_vlan_mc vlanmc;
408 struct rtl8366_vlan_4k vlan4k;
409 int err;
410 int i;
411
412 /* Try to find an existing MC entry for this VID */
413 for (i = 0; i < smi->num_vlan_mc; i++) {
414 err = smi->ops->get_vlan_mc(smi, i, &vlanmc);
415 if (err)
416 return err;
417
418 if (vid == vlanmc.vid) {
419 err = smi->ops->set_vlan_mc(smi, i, &vlanmc);
420 if (err)
421 return err;
422
423 err = smi->ops->set_mc_index(smi, port, i);
424 return err;
425 }
426 }
427
428 /* We have no MC entry for this VID, try to find an empty one */
429 for (i = 0; i < smi->num_vlan_mc; i++) {
430 err = smi->ops->get_vlan_mc(smi, i, &vlanmc);
431 if (err)
432 return err;
433
434 if (vlanmc.vid == 0 && vlanmc.member == 0) {
435 /* Update the entry from the 4K table */
436 err = smi->ops->get_vlan_4k(smi, vid, &vlan4k);
437 if (err)
438 return err;
439
440 vlanmc.vid = vid;
441 vlanmc.member = vlan4k.member;
442 vlanmc.untag = vlan4k.untag;
443 vlanmc.fid = vlan4k.fid;
444 err = smi->ops->set_vlan_mc(smi, i, &vlanmc);
445 if (err)
446 return err;
447
448 err = smi->ops->set_mc_index(smi, port, i);
449 return err;
450 }
451 }
452
453 /* MC table is full, try to find an unused entry and replace it */
454 for (i = 0; i < smi->num_vlan_mc; i++) {
455 int used;
456
457 err = rtl8366_mc_is_used(smi, i, &used);
458 if (err)
459 return err;
460
461 if (!used) {
462 /* Update the entry from the 4K table */
463 err = smi->ops->get_vlan_4k(smi, vid, &vlan4k);
464 if (err)
465 return err;
466
467 vlanmc.vid = vid;
468 vlanmc.member = vlan4k.member;
469 vlanmc.untag = vlan4k.untag;
470 vlanmc.fid = vlan4k.fid;
471 err = smi->ops->set_vlan_mc(smi, i, &vlanmc);
472 if (err)
473 return err;
474
475 err = smi->ops->set_mc_index(smi, port, i);
476 return err;
477 }
478 }
479
480 dev_err(smi->parent,
481 "all VLAN member configurations are in use\n");
482
483 return -ENOSPC;
484 }
485 EXPORT_SYMBOL_GPL(rtl8366_set_pvid);
486
487 static int rtl8366_smi_mii_init(struct rtl8366_smi *smi)
488 {
489 int ret;
490 int i;
491
492 smi->mii_bus = mdiobus_alloc();
493 if (smi->mii_bus == NULL) {
494 ret = -ENOMEM;
495 goto err;
496 }
497
498 smi->mii_bus->priv = (void *) smi;
499 smi->mii_bus->name = dev_name(smi->parent);
500 smi->mii_bus->read = smi->ops->mii_read;
501 smi->mii_bus->write = smi->ops->mii_write;
502 snprintf(smi->mii_bus->id, MII_BUS_ID_SIZE, "%s",
503 dev_name(smi->parent));
504 smi->mii_bus->parent = smi->parent;
505 smi->mii_bus->phy_mask = ~(0x1f);
506 smi->mii_bus->irq = smi->mii_irq;
507 for (i = 0; i < PHY_MAX_ADDR; i++)
508 smi->mii_irq[i] = PHY_POLL;
509
510 ret = mdiobus_register(smi->mii_bus);
511 if (ret)
512 goto err_free;
513
514 return 0;
515
516 err_free:
517 mdiobus_free(smi->mii_bus);
518 err:
519 return ret;
520 }
521
522 static void rtl8366_smi_mii_cleanup(struct rtl8366_smi *smi)
523 {
524 mdiobus_unregister(smi->mii_bus);
525 mdiobus_free(smi->mii_bus);
526 }
527
528 int rtl8366_smi_init(struct rtl8366_smi *smi)
529 {
530 int err;
531
532 if (!smi->parent)
533 return -EINVAL;
534
535 if (!smi->ops)
536 return -EINVAL;
537
538 err = gpio_request(smi->gpio_sda, dev_name(smi->parent));
539 if (err) {
540 dev_err(smi->parent, "gpio_request failed for %u, err=%d\n",
541 smi->gpio_sda, err);
542 goto err_out;
543 }
544
545 err = gpio_request(smi->gpio_sck, dev_name(smi->parent));
546 if (err) {
547 dev_err(smi->parent, "gpio_request failed for %u, err=%d\n",
548 smi->gpio_sck, err);
549 goto err_free_sda;
550 }
551
552 spin_lock_init(&smi->lock);
553
554 dev_info(smi->parent, "using GPIO pins %u (SDA) and %u (SCK)\n",
555 smi->gpio_sda, smi->gpio_sck);
556
557 err = smi->ops->detect(smi);
558 if (err) {
559 dev_err(smi->parent, "chip detection failed, err=%d\n", err);
560 goto err_free_sck;
561 }
562
563 err = rtl8366_smi_mii_init(smi);
564 if (err)
565 goto err_free_sck;
566
567 return 0;
568
569 err_free_sck:
570 gpio_free(smi->gpio_sck);
571 err_free_sda:
572 gpio_free(smi->gpio_sda);
573 err_out:
574 return err;
575 }
576 EXPORT_SYMBOL_GPL(rtl8366_smi_init);
577
578 void rtl8366_smi_cleanup(struct rtl8366_smi *smi)
579 {
580 rtl8366_smi_mii_cleanup(smi);
581 gpio_free(smi->gpio_sck);
582 gpio_free(smi->gpio_sda);
583 }
584 EXPORT_SYMBOL_GPL(rtl8366_smi_cleanup);
585
586 MODULE_DESCRIPTION("Realtek RTL8366 SMI interface driver");
587 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
588 MODULE_LICENSE("GPL v2");
This page took 0.084764 seconds and 5 git commands to generate.