1 // SPDX-License-Identifier: GPL-2.0
3 * Bitbanged MDIO support.
5 * Author: Scott Wood <scottwood@freescale.com>
6 * Copyright (c) 2007 Freescale Semiconductor
8 * Based on CPM2 MDIO code which is:
10 * Copyright (c) 2003 Intracom S.A.
11 * by Pantelis Antoniou <panto@intracom.gr>
13 * 2005 (c) MontaVista Software, Inc.
14 * Vitaly Bordug <vbordug@ru.mvista.com>
17 #include <linux/delay.h>
18 #include <linux/mdio-bitbang.h>
19 #include <linux/module.h>
20 #include <linux/types.h>
25 #define MDIO_C45 (1<<15)
26 #define MDIO_C45_ADDR (MDIO_C45 | 0)
27 #define MDIO_C45_READ (MDIO_C45 | 3)
28 #define MDIO_C45_WRITE (MDIO_C45 | 1)
30 #define MDIO_SETUP_TIME 10
31 #define MDIO_HOLD_TIME 10
33 /* Minimum MDC period is 400 ns, plus some margin for error. MDIO_DELAY
34 * is done twice per period.
36 #define MDIO_DELAY 250
38 /* The PHY may take up to 300 ns to produce data, plus some margin
41 #define MDIO_READ_DELAY 350
43 /* MDIO must already be configured as output. */
44 static void mdiobb_send_bit(struct mdiobb_ctrl
*ctrl
, int val
)
46 const struct mdiobb_ops
*ops
= ctrl
->ops
;
48 ops
->set_mdio_data(ctrl
, val
);
50 ops
->set_mdc(ctrl
, 1);
52 ops
->set_mdc(ctrl
, 0);
55 /* MDIO must already be configured as input. */
56 static int mdiobb_get_bit(struct mdiobb_ctrl
*ctrl
)
58 const struct mdiobb_ops
*ops
= ctrl
->ops
;
61 ops
->set_mdc(ctrl
, 1);
62 ndelay(MDIO_READ_DELAY
);
63 ops
->set_mdc(ctrl
, 0);
65 return ops
->get_mdio_data(ctrl
);
68 /* MDIO must already be configured as output. */
69 static void mdiobb_send_num(struct mdiobb_ctrl
*ctrl
, u16 val
, int bits
)
73 for (i
= bits
- 1; i
>= 0; i
--)
74 mdiobb_send_bit(ctrl
, (val
>> i
) & 1);
77 /* MDIO must already be configured as input. */
78 static u16
mdiobb_get_num(struct mdiobb_ctrl
*ctrl
, int bits
)
83 for (i
= bits
- 1; i
>= 0; i
--) {
85 ret
|= mdiobb_get_bit(ctrl
);
91 /* Utility to send the preamble, address, and
92 * register (common to read and write).
94 static void mdiobb_cmd(struct mdiobb_ctrl
*ctrl
, int op
, u8 phy
, u8 reg
)
96 const struct mdiobb_ops
*ops
= ctrl
->ops
;
99 ops
->set_mdio_dir(ctrl
, 1);
102 * Send a 32 bit preamble ('1's) with an extra '1' bit for good
103 * measure. The IEEE spec says this is a PHY optional
104 * requirement. The AMD 79C874 requires one after power up and
105 * one after a MII communications error. This means that we are
106 * doing more preambles than we need, but it is safer and will be
110 for (i
= 0; i
< 32; i
++)
111 mdiobb_send_bit(ctrl
, 1);
113 /* send the start bit (01) and the read opcode (10) or write (01).
114 Clause 45 operation uses 00 for the start and 11, 10 for
116 mdiobb_send_bit(ctrl
, 0);
118 mdiobb_send_bit(ctrl
, 0);
120 mdiobb_send_bit(ctrl
, 1);
121 mdiobb_send_bit(ctrl
, (op
>> 1) & 1);
122 mdiobb_send_bit(ctrl
, (op
>> 0) & 1);
124 mdiobb_send_num(ctrl
, phy
, 5);
125 mdiobb_send_num(ctrl
, reg
, 5);
128 /* In clause 45 mode all commands are prefixed by MDIO_ADDR to specify the
129 lower 16 bits of the 21 bit address. This transfer is done identically to a
130 MDIO_WRITE except for a different code. Theoretically clause 45 and normal
131 devices can exist on the same bus. Normal devices should ignore the MDIO_ADDR
133 static void mdiobb_cmd_addr(struct mdiobb_ctrl
*ctrl
, int phy
, int dev_addr
,
136 mdiobb_cmd(ctrl
, MDIO_C45_ADDR
, phy
, dev_addr
);
138 /* send the turnaround (10) */
139 mdiobb_send_bit(ctrl
, 1);
140 mdiobb_send_bit(ctrl
, 0);
142 mdiobb_send_num(ctrl
, reg
, 16);
144 ctrl
->ops
->set_mdio_dir(ctrl
, 0);
145 mdiobb_get_bit(ctrl
);
148 static int mdiobb_read_common(struct mii_bus
*bus
, int phy
)
150 struct mdiobb_ctrl
*ctrl
= bus
->priv
;
153 ctrl
->ops
->set_mdio_dir(ctrl
, 0);
155 /* check the turnaround bit: the PHY should be driving it to zero, if this
156 * PHY is listed in phy_ignore_ta_mask as having broken TA, skip that
158 if (mdiobb_get_bit(ctrl
) != 0 &&
159 !(bus
->phy_ignore_ta_mask
& (1 << phy
))) {
160 /* PHY didn't drive TA low -- flush any bits it
161 * may be trying to send.
163 for (i
= 0; i
< 32; i
++)
164 mdiobb_get_bit(ctrl
);
169 ret
= mdiobb_get_num(ctrl
, 16);
170 mdiobb_get_bit(ctrl
);
174 int mdiobb_read_c22(struct mii_bus
*bus
, int phy
, int reg
)
176 struct mdiobb_ctrl
*ctrl
= bus
->priv
;
178 mdiobb_cmd(ctrl
, ctrl
->op_c22_read
, phy
, reg
);
180 return mdiobb_read_common(bus
, phy
);
182 EXPORT_SYMBOL(mdiobb_read_c22
);
184 int mdiobb_read_c45(struct mii_bus
*bus
, int phy
, int devad
, int reg
)
186 struct mdiobb_ctrl
*ctrl
= bus
->priv
;
188 mdiobb_cmd_addr(ctrl
, phy
, devad
, reg
);
189 mdiobb_cmd(ctrl
, MDIO_C45_READ
, phy
, devad
);
191 return mdiobb_read_common(bus
, phy
);
193 EXPORT_SYMBOL(mdiobb_read_c45
);
195 static int mdiobb_write_common(struct mii_bus
*bus
, u16 val
)
197 struct mdiobb_ctrl
*ctrl
= bus
->priv
;
199 /* send the turnaround (10) */
200 mdiobb_send_bit(ctrl
, 1);
201 mdiobb_send_bit(ctrl
, 0);
203 mdiobb_send_num(ctrl
, val
, 16);
205 ctrl
->ops
->set_mdio_dir(ctrl
, 0);
206 mdiobb_get_bit(ctrl
);
210 int mdiobb_write_c22(struct mii_bus
*bus
, int phy
, int reg
, u16 val
)
212 struct mdiobb_ctrl
*ctrl
= bus
->priv
;
214 mdiobb_cmd(ctrl
, ctrl
->op_c22_write
, phy
, reg
);
216 return mdiobb_write_common(bus
, val
);
218 EXPORT_SYMBOL(mdiobb_write_c22
);
220 int mdiobb_write_c45(struct mii_bus
*bus
, int phy
, int devad
, int reg
, u16 val
)
222 struct mdiobb_ctrl
*ctrl
= bus
->priv
;
224 mdiobb_cmd_addr(ctrl
, phy
, devad
, reg
);
225 mdiobb_cmd(ctrl
, MDIO_C45_WRITE
, phy
, devad
);
227 return mdiobb_write_common(bus
, val
);
229 EXPORT_SYMBOL(mdiobb_write_c45
);
231 struct mii_bus
*alloc_mdio_bitbang(struct mdiobb_ctrl
*ctrl
)
235 bus
= mdiobus_alloc();
239 __module_get(ctrl
->ops
->owner
);
241 bus
->read
= mdiobb_read_c22
;
242 bus
->write
= mdiobb_write_c22
;
243 bus
->read_c45
= mdiobb_read_c45
;
244 bus
->write_c45
= mdiobb_write_c45
;
247 if (!ctrl
->override_op_c22
) {
248 ctrl
->op_c22_read
= MDIO_READ
;
249 ctrl
->op_c22_write
= MDIO_WRITE
;
254 EXPORT_SYMBOL(alloc_mdio_bitbang
);
256 void free_mdio_bitbang(struct mii_bus
*bus
)
258 struct mdiobb_ctrl
*ctrl
= bus
->priv
;
260 module_put(ctrl
->ops
->owner
);
263 EXPORT_SYMBOL(free_mdio_bitbang
);
265 MODULE_LICENSE("GPL v2");
266 MODULE_DESCRIPTION("Bitbanged MDIO buses");