2 * Bitbanged MDIO support.
4 * Author: Scott Wood <scottwood@freescale.com>
5 * Copyright (c) 2007 Freescale Semiconductor
7 * Based on CPM2 MDIO code which is:
9 * Copyright (c) 2003 Intracom S.A.
10 * by Pantelis Antoniou <panto@intracom.gr>
12 * 2005 (c) MontaVista Software, Inc.
13 * Vitaly Bordug <vbordug@ru.mvista.com>
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.
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>
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)
34 #define MDIO_SETUP_TIME 10
35 #define MDIO_HOLD_TIME 10
37 /* Minimum MDC period is 400 ns, plus some margin for error. MDIO_DELAY
38 * is done twice per period.
40 #define MDIO_DELAY 250
42 /* The PHY may take up to 300 ns to produce data, plus some margin
45 #define MDIO_READ_DELAY 350
47 /* MDIO must already be configured as output. */
48 static void mdiobb_send_bit(struct mdiobb_ctrl *ctrl, int val)
50 const struct mdiobb_ops *ops = ctrl->ops;
52 ops->set_mdio_data(ctrl, val);
54 ops->set_mdc(ctrl, 1);
56 ops->set_mdc(ctrl, 0);
59 /* MDIO must already be configured as input. */
60 static int mdiobb_get_bit(struct mdiobb_ctrl *ctrl)
62 const struct mdiobb_ops *ops = ctrl->ops;
65 ops->set_mdc(ctrl, 1);
66 ndelay(MDIO_READ_DELAY);
67 ops->set_mdc(ctrl, 0);
69 return ops->get_mdio_data(ctrl);
72 /* MDIO must already be configured as output. */
73 static void mdiobb_send_num(struct mdiobb_ctrl *ctrl, u16 val, int bits)
77 for (i = bits - 1; i >= 0; i--)
78 mdiobb_send_bit(ctrl, (val >> i) & 1);
81 /* MDIO must already be configured as input. */
82 static u16 mdiobb_get_num(struct mdiobb_ctrl *ctrl, int bits)
87 for (i = bits - 1; i >= 0; i--) {
89 ret |= mdiobb_get_bit(ctrl);
95 /* Utility to send the preamble, address, and
96 * register (common to read and write).
98 static void mdiobb_cmd(struct mdiobb_ctrl *ctrl, int op, u8 phy, u8 reg)
100 const struct mdiobb_ops *ops = ctrl->ops;
103 ops->set_mdio_dir(ctrl, 1);
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
114 for (i = 0; i < 32; i++)
115 mdiobb_send_bit(ctrl, 1);
117 /* 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
120 mdiobb_send_bit(ctrl, 0);
122 mdiobb_send_bit(ctrl, 0);
124 mdiobb_send_bit(ctrl, 1);
125 mdiobb_send_bit(ctrl, (op >> 1) & 1);
126 mdiobb_send_bit(ctrl, (op >> 0) & 1);
128 mdiobb_send_num(ctrl, phy, 5);
129 mdiobb_send_num(ctrl, reg, 5);
132 /* 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
138 static int mdiobb_cmd_addr(struct mdiobb_ctrl *ctrl, int phy, u32 addr)
140 unsigned int dev_addr = (addr >> 16) & 0x1F;
141 unsigned int reg = addr & 0xFFFF;
142 mdiobb_cmd(ctrl, MDIO_C45_ADDR, phy, dev_addr);
144 /* send the turnaround (10) */
145 mdiobb_send_bit(ctrl, 1);
146 mdiobb_send_bit(ctrl, 0);
148 mdiobb_send_num(ctrl, reg, 16);
150 ctrl->ops->set_mdio_dir(ctrl, 0);
151 mdiobb_get_bit(ctrl);
156 static int mdiobb_read(struct mii_bus *bus, int phy, int reg)
158 struct mdiobb_ctrl *ctrl = bus->priv;
161 if (reg & MII_ADDR_C45) {
162 reg = mdiobb_cmd_addr(ctrl, phy, reg);
163 mdiobb_cmd(ctrl, MDIO_C45_READ, phy, reg);
165 mdiobb_cmd(ctrl, MDIO_READ, phy, reg);
167 ctrl->ops->set_mdio_dir(ctrl, 0);
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.
174 for (i = 0; i < 32; i++)
175 mdiobb_get_bit(ctrl);
180 ret = mdiobb_get_num(ctrl, 16);
181 mdiobb_get_bit(ctrl);
185 static int mdiobb_write(struct mii_bus *bus, int phy, int reg, u16 val)
187 struct mdiobb_ctrl *ctrl = bus->priv;
189 if (reg & MII_ADDR_C45) {
190 reg = mdiobb_cmd_addr(ctrl, phy, reg);
191 mdiobb_cmd(ctrl, MDIO_C45_WRITE, phy, reg);
193 mdiobb_cmd(ctrl, MDIO_WRITE, phy, reg);
195 /* send the turnaround (10) */
196 mdiobb_send_bit(ctrl, 1);
197 mdiobb_send_bit(ctrl, 0);
199 mdiobb_send_num(ctrl, val, 16);
201 ctrl->ops->set_mdio_dir(ctrl, 0);
202 mdiobb_get_bit(ctrl);
206 struct mii_bus *alloc_mdio_bitbang(struct mdiobb_ctrl *ctrl)
210 bus = mdiobus_alloc();
214 __module_get(ctrl->ops->owner);
216 bus->read = mdiobb_read;
217 bus->write = mdiobb_write;
222 EXPORT_SYMBOL(alloc_mdio_bitbang);
224 void free_mdio_bitbang(struct mii_bus *bus)
226 struct mdiobb_ctrl *ctrl = bus->priv;
228 module_put(ctrl->ops->owner);
231 EXPORT_SYMBOL(free_mdio_bitbang);
233 MODULE_LICENSE("GPL");