blob: 59a32c4913011a9ab93b0be8ecff63a82510a108 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenkaffae2b2002-08-17 09:36:01 +00002/*
Luigi 'Comio' Mantellini466827e2009-10-10 12:42:20 +02003 * (C) Copyright 2009 Industrie Dial Face S.p.A.
4 * Luigi 'Comio' Mantellini <luigi.mantellini@idf-hit.com>
5 *
wdenkaffae2b2002-08-17 09:36:01 +00006 * (C) Copyright 2001
7 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
wdenkaffae2b2002-08-17 09:36:01 +00008 */
9
10/*
11 * This provides a bit-banged interface to the ethernet MII management
12 * channel.
13 */
14
15#include <common.h>
16#include <ioports.h>
17#include <ppc_asm.tmpl>
Luigi 'Comio' Mantellini466827e2009-10-10 12:42:20 +020018#include <miiphy.h>
19
20#define BB_MII_RELOCATE(v,off) (v += (v?off:0))
21
22DECLARE_GLOBAL_DATA_PTR;
23
24#ifndef CONFIG_BITBANGMII_MULTI
25
26/*
27 * If CONFIG_BITBANGMII_MULTI is not defined we use a
28 * compatibility layer with the previous miiphybb implementation
29 * based on macros usage.
30 *
31 */
32static int bb_mii_init_wrap(struct bb_miiphy_bus *bus)
33{
34#ifdef MII_INIT
35 MII_INIT;
36#endif
37 return 0;
38}
39
40static int bb_mdio_active_wrap(struct bb_miiphy_bus *bus)
41{
42#ifdef MDIO_DECLARE
43 MDIO_DECLARE;
44#endif
45 MDIO_ACTIVE;
46 return 0;
47}
48
49static int bb_mdio_tristate_wrap(struct bb_miiphy_bus *bus)
50{
51#ifdef MDIO_DECLARE
52 MDIO_DECLARE;
53#endif
54 MDIO_TRISTATE;
55 return 0;
56}
57
58static int bb_set_mdio_wrap(struct bb_miiphy_bus *bus, int v)
59{
60#ifdef MDIO_DECLARE
61 MDIO_DECLARE;
62#endif
63 MDIO(v);
64 return 0;
65}
66
67static int bb_get_mdio_wrap(struct bb_miiphy_bus *bus, int *v)
68{
69#ifdef MDIO_DECLARE
70 MDIO_DECLARE;
71#endif
72 *v = MDIO_READ;
73 return 0;
74}
75
76static int bb_set_mdc_wrap(struct bb_miiphy_bus *bus, int v)
77{
78#ifdef MDC_DECLARE
79 MDC_DECLARE;
80#endif
81 MDC(v);
82 return 0;
83}
84
85static int bb_delay_wrap(struct bb_miiphy_bus *bus)
86{
87 MIIDELAY;
88 return 0;
89}
90
91struct bb_miiphy_bus bb_miiphy_buses[] = {
92 {
93 .name = BB_MII_DEVNAME,
94 .init = bb_mii_init_wrap,
95 .mdio_active = bb_mdio_active_wrap,
96 .mdio_tristate = bb_mdio_tristate_wrap,
97 .set_mdio = bb_set_mdio_wrap,
98 .get_mdio = bb_get_mdio_wrap,
99 .set_mdc = bb_set_mdc_wrap,
100 .delay = bb_delay_wrap,
101 }
102};
103
104int bb_miiphy_buses_num = sizeof(bb_miiphy_buses) /
Wolfgang Denkd61fbcc2009-10-28 00:49:47 +0100105 sizeof(bb_miiphy_buses[0]);
Luigi 'Comio' Mantellini466827e2009-10-10 12:42:20 +0200106#endif
107
Ovidiu Panait69d1ddb2020-11-28 10:43:17 +0200108int bb_miiphy_init(void)
Luigi 'Comio' Mantellini466827e2009-10-10 12:42:20 +0200109{
110 int i;
111
112 for (i = 0; i < bb_miiphy_buses_num; i++) {
Wolfgang Denkd0813e52010-10-28 20:00:11 +0200113#if defined(CONFIG_NEEDS_MANUAL_RELOC)
Luigi 'Comio' Mantellini466827e2009-10-10 12:42:20 +0200114 /* Relocate the hook pointers*/
115 BB_MII_RELOCATE(bb_miiphy_buses[i].init, gd->reloc_off);
116 BB_MII_RELOCATE(bb_miiphy_buses[i].mdio_active, gd->reloc_off);
117 BB_MII_RELOCATE(bb_miiphy_buses[i].mdio_tristate, gd->reloc_off);
118 BB_MII_RELOCATE(bb_miiphy_buses[i].set_mdio, gd->reloc_off);
119 BB_MII_RELOCATE(bb_miiphy_buses[i].get_mdio, gd->reloc_off);
120 BB_MII_RELOCATE(bb_miiphy_buses[i].set_mdc, gd->reloc_off);
121 BB_MII_RELOCATE(bb_miiphy_buses[i].delay, gd->reloc_off);
122#endif
123 if (bb_miiphy_buses[i].init != NULL) {
124 bb_miiphy_buses[i].init(&bb_miiphy_buses[i]);
125 }
126 }
Ovidiu Panait69d1ddb2020-11-28 10:43:17 +0200127
128 return 0;
Luigi 'Comio' Mantellini466827e2009-10-10 12:42:20 +0200129}
130
Ben Warren97824d62010-07-29 12:56:11 -0700131static inline struct bb_miiphy_bus *bb_miiphy_getbus(const char *devname)
Luigi 'Comio' Mantellini466827e2009-10-10 12:42:20 +0200132{
133#ifdef CONFIG_BITBANGMII_MULTI
134 int i;
135
136 /* Search the correct bus */
137 for (i = 0; i < bb_miiphy_buses_num; i++) {
138 if (!strcmp(bb_miiphy_buses[i].name, devname)) {
139 return &bb_miiphy_buses[i];
140 }
141 }
142 return NULL;
143#else
144 /* We have just one bitbanging bus */
145 return &bb_miiphy_buses[0];
146#endif
147}
wdenkaffae2b2002-08-17 09:36:01 +0000148
wdenkaffae2b2002-08-17 09:36:01 +0000149/*****************************************************************************
150 *
151 * Utility to send the preamble, address, and register (common to read
152 * and write).
153 */
Luigi 'Comio' Mantellini466827e2009-10-10 12:42:20 +0200154static void miiphy_pre(struct bb_miiphy_bus *bus, char read,
Wolfgang Denkd61fbcc2009-10-28 00:49:47 +0100155 unsigned char addr, unsigned char reg)
wdenkaffae2b2002-08-17 09:36:01 +0000156{
Luigi 'Comio' Mantellini466827e2009-10-10 12:42:20 +0200157 int j;
wdenkaffae2b2002-08-17 09:36:01 +0000158
Wolfgang Denk7b4e3472005-08-13 02:04:37 +0200159 /*
160 * Send a 32 bit preamble ('1's) with an extra '1' bit for good measure.
161 * The IEEE spec says this is a PHY optional requirement. The AMD
162 * 79C874 requires one after power up and one after a MII communications
163 * error. This means that we are doing more preambles than we need,
164 * but it is safer and will be much more robust.
165 */
wdenkaffae2b2002-08-17 09:36:01 +0000166
Luigi 'Comio' Mantellini466827e2009-10-10 12:42:20 +0200167 bus->mdio_active(bus);
168 bus->set_mdio(bus, 1);
Wolfgang Denk7b4e3472005-08-13 02:04:37 +0200169 for (j = 0; j < 32; j++) {
Luigi 'Comio' Mantellini466827e2009-10-10 12:42:20 +0200170 bus->set_mdc(bus, 0);
171 bus->delay(bus);
172 bus->set_mdc(bus, 1);
173 bus->delay(bus);
Wolfgang Denk7b4e3472005-08-13 02:04:37 +0200174 }
wdenkaffae2b2002-08-17 09:36:01 +0000175
Wolfgang Denk7b4e3472005-08-13 02:04:37 +0200176 /* send the start bit (01) and the read opcode (10) or write (10) */
Luigi 'Comio' Mantellini466827e2009-10-10 12:42:20 +0200177 bus->set_mdc(bus, 0);
178 bus->set_mdio(bus, 0);
179 bus->delay(bus);
180 bus->set_mdc(bus, 1);
181 bus->delay(bus);
182 bus->set_mdc(bus, 0);
183 bus->set_mdio(bus, 1);
184 bus->delay(bus);
185 bus->set_mdc(bus, 1);
186 bus->delay(bus);
187 bus->set_mdc(bus, 0);
188 bus->set_mdio(bus, read);
189 bus->delay(bus);
190 bus->set_mdc(bus, 1);
191 bus->delay(bus);
192 bus->set_mdc(bus, 0);
193 bus->set_mdio(bus, !read);
194 bus->delay(bus);
195 bus->set_mdc(bus, 1);
196 bus->delay(bus);
wdenkaffae2b2002-08-17 09:36:01 +0000197
Wolfgang Denk7b4e3472005-08-13 02:04:37 +0200198 /* send the PHY address */
199 for (j = 0; j < 5; j++) {
Luigi 'Comio' Mantellini466827e2009-10-10 12:42:20 +0200200 bus->set_mdc(bus, 0);
Wolfgang Denk7b4e3472005-08-13 02:04:37 +0200201 if ((addr & 0x10) == 0) {
Luigi 'Comio' Mantellini466827e2009-10-10 12:42:20 +0200202 bus->set_mdio(bus, 0);
Wolfgang Denk7b4e3472005-08-13 02:04:37 +0200203 } else {
Luigi 'Comio' Mantellini466827e2009-10-10 12:42:20 +0200204 bus->set_mdio(bus, 1);
Wolfgang Denk7b4e3472005-08-13 02:04:37 +0200205 }
Luigi 'Comio' Mantellini466827e2009-10-10 12:42:20 +0200206 bus->delay(bus);
207 bus->set_mdc(bus, 1);
208 bus->delay(bus);
Wolfgang Denk7b4e3472005-08-13 02:04:37 +0200209 addr <<= 1;
210 }
wdenkaffae2b2002-08-17 09:36:01 +0000211
Wolfgang Denk7b4e3472005-08-13 02:04:37 +0200212 /* send the register address */
213 for (j = 0; j < 5; j++) {
Luigi 'Comio' Mantellini466827e2009-10-10 12:42:20 +0200214 bus->set_mdc(bus, 0);
Wolfgang Denk7b4e3472005-08-13 02:04:37 +0200215 if ((reg & 0x10) == 0) {
Luigi 'Comio' Mantellini466827e2009-10-10 12:42:20 +0200216 bus->set_mdio(bus, 0);
Wolfgang Denk7b4e3472005-08-13 02:04:37 +0200217 } else {
Luigi 'Comio' Mantellini466827e2009-10-10 12:42:20 +0200218 bus->set_mdio(bus, 1);
Wolfgang Denk7b4e3472005-08-13 02:04:37 +0200219 }
Luigi 'Comio' Mantellini466827e2009-10-10 12:42:20 +0200220 bus->delay(bus);
221 bus->set_mdc(bus, 1);
222 bus->delay(bus);
Wolfgang Denk7b4e3472005-08-13 02:04:37 +0200223 reg <<= 1;
224 }
wdenkaffae2b2002-08-17 09:36:01 +0000225}
226
wdenkaffae2b2002-08-17 09:36:01 +0000227/*****************************************************************************
228 *
229 * Read a MII PHY register.
230 *
231 * Returns:
232 * 0 on success
233 */
Joe Hershberger0c333192016-08-08 11:28:39 -0500234int bb_miiphy_read(struct mii_dev *miidev, int addr, int devad, int reg)
wdenkaffae2b2002-08-17 09:36:01 +0000235{
Chris Brandt7e4d4d12017-11-03 08:30:13 -0500236 unsigned short rdreg; /* register working value */
Luigi 'Comio' Mantellini466827e2009-10-10 12:42:20 +0200237 int v;
238 int j; /* counter */
239 struct bb_miiphy_bus *bus;
240
Joe Hershberger0c333192016-08-08 11:28:39 -0500241 bus = bb_miiphy_getbus(miidev->name);
Luigi 'Comio' Mantellini466827e2009-10-10 12:42:20 +0200242 if (bus == NULL) {
243 return -1;
244 }
wdenkaffae2b2002-08-17 09:36:01 +0000245
Luigi 'Comio' Mantellini466827e2009-10-10 12:42:20 +0200246 miiphy_pre (bus, 1, addr, reg);
wdenkaffae2b2002-08-17 09:36:01 +0000247
Wolfgang Denk7b4e3472005-08-13 02:04:37 +0200248 /* tri-state our MDIO I/O pin so we can read */
Luigi 'Comio' Mantellini466827e2009-10-10 12:42:20 +0200249 bus->set_mdc(bus, 0);
250 bus->mdio_tristate(bus);
251 bus->delay(bus);
252 bus->set_mdc(bus, 1);
253 bus->delay(bus);
wdenkaffae2b2002-08-17 09:36:01 +0000254
Wolfgang Denk7b4e3472005-08-13 02:04:37 +0200255 /* check the turnaround bit: the PHY should be driving it to zero */
Luigi 'Comio' Mantellini466827e2009-10-10 12:42:20 +0200256 bus->get_mdio(bus, &v);
257 if (v != 0) {
Wolfgang Denk7b4e3472005-08-13 02:04:37 +0200258 /* puts ("PHY didn't drive TA low\n"); */
259 for (j = 0; j < 32; j++) {
Luigi 'Comio' Mantellini466827e2009-10-10 12:42:20 +0200260 bus->set_mdc(bus, 0);
261 bus->delay(bus);
262 bus->set_mdc(bus, 1);
263 bus->delay(bus);
Wolfgang Denk7b4e3472005-08-13 02:04:37 +0200264 }
Joe Hershberger0c333192016-08-08 11:28:39 -0500265 /* There is no PHY, return */
Luigi 'Comio' Mantellini466827e2009-10-10 12:42:20 +0200266 return -1;
Wolfgang Denk7b4e3472005-08-13 02:04:37 +0200267 }
wdenkaffae2b2002-08-17 09:36:01 +0000268
Luigi 'Comio' Mantellini466827e2009-10-10 12:42:20 +0200269 bus->set_mdc(bus, 0);
270 bus->delay(bus);
wdenkaffae2b2002-08-17 09:36:01 +0000271
Wolfgang Denk7b4e3472005-08-13 02:04:37 +0200272 /* read 16 bits of register data, MSB first */
273 rdreg = 0;
274 for (j = 0; j < 16; j++) {
Luigi 'Comio' Mantellini466827e2009-10-10 12:42:20 +0200275 bus->set_mdc(bus, 1);
276 bus->delay(bus);
Wolfgang Denk7b4e3472005-08-13 02:04:37 +0200277 rdreg <<= 1;
Luigi 'Comio' Mantellini466827e2009-10-10 12:42:20 +0200278 bus->get_mdio(bus, &v);
279 rdreg |= (v & 0x1);
280 bus->set_mdc(bus, 0);
281 bus->delay(bus);
Wolfgang Denk7b4e3472005-08-13 02:04:37 +0200282 }
wdenkaffae2b2002-08-17 09:36:01 +0000283
Luigi 'Comio' Mantellini466827e2009-10-10 12:42:20 +0200284 bus->set_mdc(bus, 1);
285 bus->delay(bus);
286 bus->set_mdc(bus, 0);
287 bus->delay(bus);
288 bus->set_mdc(bus, 1);
289 bus->delay(bus);
wdenkaffae2b2002-08-17 09:36:01 +0000290
wdenkaffae2b2002-08-17 09:36:01 +0000291#ifdef DEBUG
Joe Hershberger0c333192016-08-08 11:28:39 -0500292 printf("miiphy_read(0x%x) @ 0x%x = 0x%04x\n", reg, addr, rdreg);
wdenkaffae2b2002-08-17 09:36:01 +0000293#endif
294
Joe Hershberger0c333192016-08-08 11:28:39 -0500295 return rdreg;
wdenkaffae2b2002-08-17 09:36:01 +0000296}
297
298
299/*****************************************************************************
300 *
301 * Write a MII PHY register.
302 *
303 * Returns:
304 * 0 on success
305 */
Joe Hershberger0c333192016-08-08 11:28:39 -0500306int bb_miiphy_write(struct mii_dev *miidev, int addr, int devad, int reg,
307 u16 value)
wdenkaffae2b2002-08-17 09:36:01 +0000308{
Luigi 'Comio' Mantellini466827e2009-10-10 12:42:20 +0200309 struct bb_miiphy_bus *bus;
Wolfgang Denk7b4e3472005-08-13 02:04:37 +0200310 int j; /* counter */
Luigi 'Comio' Mantellini466827e2009-10-10 12:42:20 +0200311
Joe Hershberger0c333192016-08-08 11:28:39 -0500312 bus = bb_miiphy_getbus(miidev->name);
Luigi 'Comio' Mantellini466827e2009-10-10 12:42:20 +0200313 if (bus == NULL) {
314 /* Bus not found! */
315 return -1;
316 }
wdenkaffae2b2002-08-17 09:36:01 +0000317
Luigi 'Comio' Mantellini466827e2009-10-10 12:42:20 +0200318 miiphy_pre (bus, 0, addr, reg);
wdenkaffae2b2002-08-17 09:36:01 +0000319
Wolfgang Denk7b4e3472005-08-13 02:04:37 +0200320 /* send the turnaround (10) */
Luigi 'Comio' Mantellini466827e2009-10-10 12:42:20 +0200321 bus->set_mdc(bus, 0);
322 bus->set_mdio(bus, 1);
323 bus->delay(bus);
324 bus->set_mdc(bus, 1);
325 bus->delay(bus);
326 bus->set_mdc(bus, 0);
327 bus->set_mdio(bus, 0);
328 bus->delay(bus);
329 bus->set_mdc(bus, 1);
330 bus->delay(bus);
wdenkaffae2b2002-08-17 09:36:01 +0000331
Wolfgang Denk7b4e3472005-08-13 02:04:37 +0200332 /* write 16 bits of register data, MSB first */
333 for (j = 0; j < 16; j++) {
Luigi 'Comio' Mantellini466827e2009-10-10 12:42:20 +0200334 bus->set_mdc(bus, 0);
Wolfgang Denk7b4e3472005-08-13 02:04:37 +0200335 if ((value & 0x00008000) == 0) {
Luigi 'Comio' Mantellini466827e2009-10-10 12:42:20 +0200336 bus->set_mdio(bus, 0);
Wolfgang Denk7b4e3472005-08-13 02:04:37 +0200337 } else {
Luigi 'Comio' Mantellini466827e2009-10-10 12:42:20 +0200338 bus->set_mdio(bus, 1);
Wolfgang Denk7b4e3472005-08-13 02:04:37 +0200339 }
Luigi 'Comio' Mantellini466827e2009-10-10 12:42:20 +0200340 bus->delay(bus);
341 bus->set_mdc(bus, 1);
342 bus->delay(bus);
Wolfgang Denk7b4e3472005-08-13 02:04:37 +0200343 value <<= 1;
344 }
wdenkaffae2b2002-08-17 09:36:01 +0000345
Wolfgang Denk7b4e3472005-08-13 02:04:37 +0200346 /*
347 * Tri-state the MDIO line.
348 */
Luigi 'Comio' Mantellini466827e2009-10-10 12:42:20 +0200349 bus->mdio_tristate(bus);
350 bus->set_mdc(bus, 0);
351 bus->delay(bus);
352 bus->set_mdc(bus, 1);
353 bus->delay(bus);
wdenkaffae2b2002-08-17 09:36:01 +0000354
Wolfgang Denk7b4e3472005-08-13 02:04:37 +0200355 return 0;
Wolfgang Denk9235e0c2009-10-25 23:00:09 +0100356}