blob: 0ff06617a4ab2a3e7f23926ebd7960ba4e7681cf [file] [log] [blame]
Scott Woode2ec4582007-10-01 14:20:56 -05001/*
2 * Bitbanged MDIO support.
3 *
4 * Author: Scott Wood <scottwood@freescale.com>
5 * Copyright (c) 2007 Freescale Semiconductor
6 *
7 * Based on CPM2 MDIO code which is:
8 *
9 * Copyright (c) 2003 Intracom S.A.
10 * by Pantelis Antoniou <panto@intracom.gr>
11 *
12 * 2005 (c) MontaVista Software, Inc.
13 * Vitaly Bordug <vbordug@ru.mvista.com>
14 *
15 * This file is licensed under the terms of the GNU General Public License
16 * version 2. This program is licensed "as is" without any warranty of any
17 * kind, whether express or implied.
18 */
19
20#include <linux/module.h>
21#include <linux/mdio-bitbang.h>
22#include <linux/slab.h>
23#include <linux/types.h>
24#include <linux/delay.h>
25
Jason Gunthorpeabf35df2010-03-09 09:17:42 +000026#define MDIO_READ 2
27#define MDIO_WRITE 1
28
29#define MDIO_C45 (1<<15)
30#define MDIO_C45_ADDR (MDIO_C45 | 0)
31#define MDIO_C45_READ (MDIO_C45 | 3)
32#define MDIO_C45_WRITE (MDIO_C45 | 1)
Scott Woode2ec4582007-10-01 14:20:56 -050033
34#define MDIO_SETUP_TIME 10
35#define MDIO_HOLD_TIME 10
36
37/* Minimum MDC period is 400 ns, plus some margin for error. MDIO_DELAY
38 * is done twice per period.
39 */
40#define MDIO_DELAY 250
41
42/* The PHY may take up to 300 ns to produce data, plus some margin
43 * for error.
44 */
45#define MDIO_READ_DELAY 350
46
47/* MDIO must already be configured as output. */
48static void mdiobb_send_bit(struct mdiobb_ctrl *ctrl, int val)
49{
50 const struct mdiobb_ops *ops = ctrl->ops;
51
52 ops->set_mdio_data(ctrl, val);
53 ndelay(MDIO_DELAY);
54 ops->set_mdc(ctrl, 1);
55 ndelay(MDIO_DELAY);
56 ops->set_mdc(ctrl, 0);
57}
58
59/* MDIO must already be configured as input. */
60static int mdiobb_get_bit(struct mdiobb_ctrl *ctrl)
61{
62 const struct mdiobb_ops *ops = ctrl->ops;
63
64 ndelay(MDIO_DELAY);
65 ops->set_mdc(ctrl, 1);
66 ndelay(MDIO_READ_DELAY);
67 ops->set_mdc(ctrl, 0);
68
69 return ops->get_mdio_data(ctrl);
70}
71
72/* MDIO must already be configured as output. */
73static void mdiobb_send_num(struct mdiobb_ctrl *ctrl, u16 val, int bits)
74{
75 int i;
76
77 for (i = bits - 1; i >= 0; i--)
78 mdiobb_send_bit(ctrl, (val >> i) & 1);
79}
80
81/* MDIO must already be configured as input. */
82static u16 mdiobb_get_num(struct mdiobb_ctrl *ctrl, int bits)
83{
84 int i;
85 u16 ret = 0;
86
87 for (i = bits - 1; i >= 0; i--) {
88 ret <<= 1;
89 ret |= mdiobb_get_bit(ctrl);
90 }
91
92 return ret;
93}
94
95/* Utility to send the preamble, address, and
96 * register (common to read and write).
97 */
Jason Gunthorpeabf35df2010-03-09 09:17:42 +000098static void mdiobb_cmd(struct mdiobb_ctrl *ctrl, int op, u8 phy, u8 reg)
Scott Woode2ec4582007-10-01 14:20:56 -050099{
100 const struct mdiobb_ops *ops = ctrl->ops;
101 int i;
102
103 ops->set_mdio_dir(ctrl, 1);
104
105 /*
106 * Send a 32 bit preamble ('1's) with an extra '1' bit for good
107 * measure. The IEEE spec says this is a PHY optional
108 * requirement. The AMD 79C874 requires one after power up and
109 * one after a MII communications error. This means that we are
110 * doing more preambles than we need, but it is safer and will be
111 * much more robust.
112 */
113
114 for (i = 0; i < 32; i++)
115 mdiobb_send_bit(ctrl, 1);
116
Jason Gunthorpeabf35df2010-03-09 09:17:42 +0000117 /* send the start bit (01) and the read opcode (10) or write (10).
118 Clause 45 operation uses 00 for the start and 11, 10 for
119 read/write */
Scott Woode2ec4582007-10-01 14:20:56 -0500120 mdiobb_send_bit(ctrl, 0);
Jason Gunthorpeabf35df2010-03-09 09:17:42 +0000121 if (op & MDIO_C45)
122 mdiobb_send_bit(ctrl, 0);
123 else
124 mdiobb_send_bit(ctrl, 1);
125 mdiobb_send_bit(ctrl, (op >> 1) & 1);
126 mdiobb_send_bit(ctrl, (op >> 0) & 1);
Scott Woode2ec4582007-10-01 14:20:56 -0500127
128 mdiobb_send_num(ctrl, phy, 5);
129 mdiobb_send_num(ctrl, reg, 5);
130}
131
Jason Gunthorpeabf35df2010-03-09 09:17:42 +0000132/* In clause 45 mode all commands are prefixed by MDIO_ADDR to specify the
133 lower 16 bits of the 21 bit address. This transfer is done identically to a
134 MDIO_WRITE except for a different code. To enable clause 45 mode or
135 MII_ADDR_C45 into the address. Theoretically clause 45 and normal devices
136 can exist on the same bus. Normal devices should ignore the MDIO_ADDR
137 phase. */
138static int mdiobb_cmd_addr(struct mdiobb_ctrl *ctrl, int phy, u32 addr)
139{
140 unsigned int dev_addr = (addr >> 16) & 0x1F;
141 unsigned int reg = addr & 0xFFFF;
142 mdiobb_cmd(ctrl, MDIO_C45_ADDR, phy, dev_addr);
143
144 /* send the turnaround (10) */
145 mdiobb_send_bit(ctrl, 1);
146 mdiobb_send_bit(ctrl, 0);
147
148 mdiobb_send_num(ctrl, reg, 16);
149
150 ctrl->ops->set_mdio_dir(ctrl, 0);
151 mdiobb_get_bit(ctrl);
152
153 return dev_addr;
154}
Scott Woode2ec4582007-10-01 14:20:56 -0500155
156static int mdiobb_read(struct mii_bus *bus, int phy, int reg)
157{
158 struct mdiobb_ctrl *ctrl = bus->priv;
159 int ret, i;
160
Jason Gunthorpeabf35df2010-03-09 09:17:42 +0000161 if (reg & MII_ADDR_C45) {
162 reg = mdiobb_cmd_addr(ctrl, phy, reg);
163 mdiobb_cmd(ctrl, MDIO_C45_READ, phy, reg);
164 } else
165 mdiobb_cmd(ctrl, MDIO_READ, phy, reg);
166
Scott Woode2ec4582007-10-01 14:20:56 -0500167 ctrl->ops->set_mdio_dir(ctrl, 0);
168
169 /* check the turnaround bit: the PHY should be driving it to zero */
170 if (mdiobb_get_bit(ctrl) != 0) {
171 /* PHY didn't drive TA low -- flush any bits it
172 * may be trying to send.
173 */
174 for (i = 0; i < 32; i++)
175 mdiobb_get_bit(ctrl);
176
177 return 0xffff;
178 }
179
180 ret = mdiobb_get_num(ctrl, 16);
181 mdiobb_get_bit(ctrl);
182 return ret;
183}
184
185static int mdiobb_write(struct mii_bus *bus, int phy, int reg, u16 val)
186{
187 struct mdiobb_ctrl *ctrl = bus->priv;
188
Jason Gunthorpeabf35df2010-03-09 09:17:42 +0000189 if (reg & MII_ADDR_C45) {
190 reg = mdiobb_cmd_addr(ctrl, phy, reg);
191 mdiobb_cmd(ctrl, MDIO_C45_WRITE, phy, reg);
192 } else
193 mdiobb_cmd(ctrl, MDIO_WRITE, phy, reg);
Scott Woode2ec4582007-10-01 14:20:56 -0500194
195 /* send the turnaround (10) */
196 mdiobb_send_bit(ctrl, 1);
197 mdiobb_send_bit(ctrl, 0);
198
199 mdiobb_send_num(ctrl, val, 16);
200
201 ctrl->ops->set_mdio_dir(ctrl, 0);
202 mdiobb_get_bit(ctrl);
203 return 0;
204}
205
206struct mii_bus *alloc_mdio_bitbang(struct mdiobb_ctrl *ctrl)
207{
208 struct mii_bus *bus;
209
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700210 bus = mdiobus_alloc();
Scott Woode2ec4582007-10-01 14:20:56 -0500211 if (!bus)
212 return NULL;
213
214 __module_get(ctrl->ops->owner);
215
216 bus->read = mdiobb_read;
217 bus->write = mdiobb_write;
218 bus->priv = ctrl;
219
220 return bus;
221}
Takashi Iwaie9911c22008-07-07 16:51:45 +0200222EXPORT_SYMBOL(alloc_mdio_bitbang);
Scott Woode2ec4582007-10-01 14:20:56 -0500223
224void free_mdio_bitbang(struct mii_bus *bus)
225{
226 struct mdiobb_ctrl *ctrl = bus->priv;
227
228 module_put(ctrl->ops->owner);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700229 mdiobus_free(bus);
Scott Woode2ec4582007-10-01 14:20:56 -0500230}
Takashi Iwaie9911c22008-07-07 16:51:45 +0200231EXPORT_SYMBOL(free_mdio_bitbang);
Randy Dunlap5a462362007-10-18 12:20:21 -0700232
233MODULE_LICENSE("GPL");