generic: rtl8366: introduce rtl8366_smi_rmwr function
[openwrt.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 #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_smi_mii_init(struct rtl8366_smi *smi)
285 {
286 int ret;
287 int i;
288
289 smi->mii_bus = mdiobus_alloc();
290 if (smi->mii_bus == NULL) {
291 ret = -ENOMEM;
292 goto err;
293 }
294
295 smi->mii_bus->priv = (void *) smi;
296 smi->mii_bus->name = dev_name(smi->parent);
297 smi->mii_bus->read = smi->ops->mii_read;
298 smi->mii_bus->write = smi->ops->mii_write;
299 snprintf(smi->mii_bus->id, MII_BUS_ID_SIZE, "%s",
300 dev_name(smi->parent));
301 smi->mii_bus->parent = smi->parent;
302 smi->mii_bus->phy_mask = ~(0x1f);
303 smi->mii_bus->irq = smi->mii_irq;
304 for (i = 0; i < PHY_MAX_ADDR; i++)
305 smi->mii_irq[i] = PHY_POLL;
306
307 ret = mdiobus_register(smi->mii_bus);
308 if (ret)
309 goto err_free;
310
311 return 0;
312
313 err_free:
314 mdiobus_free(smi->mii_bus);
315 err:
316 return ret;
317 }
318
319 static void rtl8366_smi_mii_cleanup(struct rtl8366_smi *smi)
320 {
321 mdiobus_unregister(smi->mii_bus);
322 mdiobus_free(smi->mii_bus);
323 }
324
325 int rtl8366_smi_init(struct rtl8366_smi *smi)
326 {
327 int err;
328
329 if (!smi->parent)
330 return -EINVAL;
331
332 if (!smi->ops)
333 return -EINVAL;
334
335 err = gpio_request(smi->gpio_sda, dev_name(smi->parent));
336 if (err) {
337 dev_err(smi->parent, "gpio_request failed for %u, err=%d\n",
338 smi->gpio_sda, err);
339 goto err_out;
340 }
341
342 err = gpio_request(smi->gpio_sck, dev_name(smi->parent));
343 if (err) {
344 dev_err(smi->parent, "gpio_request failed for %u, err=%d\n",
345 smi->gpio_sck, err);
346 goto err_free_sda;
347 }
348
349 spin_lock_init(&smi->lock);
350
351 dev_info(smi->parent, "using GPIO pins %u (SDA) and %u (SCK)\n",
352 smi->gpio_sda, smi->gpio_sck);
353
354 err = smi->ops->detect(smi);
355 if (err) {
356 dev_err(smi->parent, "chip detection failed, err=%d\n", err);
357 goto err_free_sck;
358 }
359
360 err = rtl8366_smi_mii_init(smi);
361 if (err)
362 goto err_free_sck;
363
364 return 0;
365
366 err_free_sck:
367 gpio_free(smi->gpio_sck);
368 err_free_sda:
369 gpio_free(smi->gpio_sda);
370 err_out:
371 return err;
372 }
373 EXPORT_SYMBOL_GPL(rtl8366_smi_init);
374
375 void rtl8366_smi_cleanup(struct rtl8366_smi *smi)
376 {
377 rtl8366_smi_mii_cleanup(smi);
378 gpio_free(smi->gpio_sck);
379 gpio_free(smi->gpio_sda);
380 }
381 EXPORT_SYMBOL_GPL(rtl8366_smi_cleanup);
382
383 MODULE_DESCRIPTION("Realtek RTL8366 SMI interface driver");
384 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
385 MODULE_LICENSE("GPL v2");
This page took 0.059943 seconds and 5 git commands to generate.