2 * (C) Copyright 2009 Industrie Dial Face S.p.A.
3 * Luigi 'Comio' Mantellini <luigi.mantellini@idf-hit.com>
6 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
8 * SPDX-License-Identifier: GPL-2.0+
12 * This provides a bit-banged interface to the ethernet MII management
18 #include <ppc_asm.tmpl>
21 #define BB_MII_RELOCATE(v,off) (v += (v?off:0))
23 DECLARE_GLOBAL_DATA_PTR;
25 #ifndef CONFIG_BITBANGMII_MULTI
28 * If CONFIG_BITBANGMII_MULTI is not defined we use a
29 * compatibility layer with the previous miiphybb implementation
30 * based on macros usage.
33 static int bb_mii_init_wrap(struct bb_miiphy_bus *bus)
41 static int bb_mdio_active_wrap(struct bb_miiphy_bus *bus)
50 static int bb_mdio_tristate_wrap(struct bb_miiphy_bus *bus)
59 static int bb_set_mdio_wrap(struct bb_miiphy_bus *bus, int v)
68 static int bb_get_mdio_wrap(struct bb_miiphy_bus *bus, int *v)
77 static int bb_set_mdc_wrap(struct bb_miiphy_bus *bus, int v)
86 static int bb_delay_wrap(struct bb_miiphy_bus *bus)
92 struct bb_miiphy_bus bb_miiphy_buses[] = {
94 .name = BB_MII_DEVNAME,
95 .init = bb_mii_init_wrap,
96 .mdio_active = bb_mdio_active_wrap,
97 .mdio_tristate = bb_mdio_tristate_wrap,
98 .set_mdio = bb_set_mdio_wrap,
99 .get_mdio = bb_get_mdio_wrap,
100 .set_mdc = bb_set_mdc_wrap,
101 .delay = bb_delay_wrap,
105 int bb_miiphy_buses_num = sizeof(bb_miiphy_buses) /
106 sizeof(bb_miiphy_buses[0]);
109 void bb_miiphy_init(void)
113 for (i = 0; i < bb_miiphy_buses_num; i++) {
114 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
115 /* Relocate the hook pointers*/
116 BB_MII_RELOCATE(bb_miiphy_buses[i].init, gd->reloc_off);
117 BB_MII_RELOCATE(bb_miiphy_buses[i].mdio_active, gd->reloc_off);
118 BB_MII_RELOCATE(bb_miiphy_buses[i].mdio_tristate, gd->reloc_off);
119 BB_MII_RELOCATE(bb_miiphy_buses[i].set_mdio, gd->reloc_off);
120 BB_MII_RELOCATE(bb_miiphy_buses[i].get_mdio, gd->reloc_off);
121 BB_MII_RELOCATE(bb_miiphy_buses[i].set_mdc, gd->reloc_off);
122 BB_MII_RELOCATE(bb_miiphy_buses[i].delay, gd->reloc_off);
124 if (bb_miiphy_buses[i].init != NULL) {
125 bb_miiphy_buses[i].init(&bb_miiphy_buses[i]);
130 static inline struct bb_miiphy_bus *bb_miiphy_getbus(const char *devname)
132 #ifdef CONFIG_BITBANGMII_MULTI
135 /* Search the correct bus */
136 for (i = 0; i < bb_miiphy_buses_num; i++) {
137 if (!strcmp(bb_miiphy_buses[i].name, devname)) {
138 return &bb_miiphy_buses[i];
143 /* We have just one bitbanging bus */
144 return &bb_miiphy_buses[0];
148 /*****************************************************************************
150 * Utility to send the preamble, address, and register (common to read
153 static void miiphy_pre(struct bb_miiphy_bus *bus, char read,
154 unsigned char addr, unsigned char reg)
159 * Send a 32 bit preamble ('1's) with an extra '1' bit for good measure.
160 * The IEEE spec says this is a PHY optional requirement. The AMD
161 * 79C874 requires one after power up and one after a MII communications
162 * error. This means that we are doing more preambles than we need,
163 * but it is safer and will be much more robust.
166 bus->mdio_active(bus);
167 bus->set_mdio(bus, 1);
168 for (j = 0; j < 32; j++) {
169 bus->set_mdc(bus, 0);
171 bus->set_mdc(bus, 1);
175 /* send the start bit (01) and the read opcode (10) or write (10) */
176 bus->set_mdc(bus, 0);
177 bus->set_mdio(bus, 0);
179 bus->set_mdc(bus, 1);
181 bus->set_mdc(bus, 0);
182 bus->set_mdio(bus, 1);
184 bus->set_mdc(bus, 1);
186 bus->set_mdc(bus, 0);
187 bus->set_mdio(bus, read);
189 bus->set_mdc(bus, 1);
191 bus->set_mdc(bus, 0);
192 bus->set_mdio(bus, !read);
194 bus->set_mdc(bus, 1);
197 /* send the PHY address */
198 for (j = 0; j < 5; j++) {
199 bus->set_mdc(bus, 0);
200 if ((addr & 0x10) == 0) {
201 bus->set_mdio(bus, 0);
203 bus->set_mdio(bus, 1);
206 bus->set_mdc(bus, 1);
211 /* send the register address */
212 for (j = 0; j < 5; j++) {
213 bus->set_mdc(bus, 0);
214 if ((reg & 0x10) == 0) {
215 bus->set_mdio(bus, 0);
217 bus->set_mdio(bus, 1);
220 bus->set_mdc(bus, 1);
226 /*****************************************************************************
228 * Read a MII PHY register.
233 int bb_miiphy_read(const char *devname, unsigned char addr,
234 unsigned char reg, unsigned short *value)
236 short rdreg; /* register working value */
239 struct bb_miiphy_bus *bus;
241 bus = bb_miiphy_getbus(devname);
247 puts("NULL value pointer\n");
251 miiphy_pre (bus, 1, addr, reg);
253 /* tri-state our MDIO I/O pin so we can read */
254 bus->set_mdc(bus, 0);
255 bus->mdio_tristate(bus);
257 bus->set_mdc(bus, 1);
260 /* check the turnaround bit: the PHY should be driving it to zero */
261 bus->get_mdio(bus, &v);
263 /* puts ("PHY didn't drive TA low\n"); */
264 for (j = 0; j < 32; j++) {
265 bus->set_mdc(bus, 0);
267 bus->set_mdc(bus, 1);
270 /* There is no PHY, set value to 0xFFFF and return */
275 bus->set_mdc(bus, 0);
278 /* read 16 bits of register data, MSB first */
280 for (j = 0; j < 16; j++) {
281 bus->set_mdc(bus, 1);
284 bus->get_mdio(bus, &v);
286 bus->set_mdc(bus, 0);
290 bus->set_mdc(bus, 1);
292 bus->set_mdc(bus, 0);
294 bus->set_mdc(bus, 1);
300 printf ("miiphy_read(0x%x) @ 0x%x = 0x%04x\n", reg, addr, *value);
307 /*****************************************************************************
309 * Write a MII PHY register.
314 int bb_miiphy_write (const char *devname, unsigned char addr,
315 unsigned char reg, unsigned short value)
317 struct bb_miiphy_bus *bus;
320 bus = bb_miiphy_getbus(devname);
326 miiphy_pre (bus, 0, addr, reg);
328 /* send the turnaround (10) */
329 bus->set_mdc(bus, 0);
330 bus->set_mdio(bus, 1);
332 bus->set_mdc(bus, 1);
334 bus->set_mdc(bus, 0);
335 bus->set_mdio(bus, 0);
337 bus->set_mdc(bus, 1);
340 /* write 16 bits of register data, MSB first */
341 for (j = 0; j < 16; j++) {
342 bus->set_mdc(bus, 0);
343 if ((value & 0x00008000) == 0) {
344 bus->set_mdio(bus, 0);
346 bus->set_mdio(bus, 1);
349 bus->set_mdc(bus, 1);
355 * Tri-state the MDIO line.
357 bus->mdio_tristate(bus);
358 bus->set_mdc(bus, 0);
360 bus->set_mdc(bus, 1);