blob: ba97a54c0676fd159a236254c38b9ec6bbce6340 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenkaffae2b2002-08-17 09:36:01 +00002/*
Luigi 'Comio' Mantellini4ba31ab2009-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' Mantellini4ba31ab2009-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 Denk49467752009-10-28 00:49:47 +0100105 sizeof(bb_miiphy_buses[0]);
Luigi 'Comio' Mantellini4ba31ab2009-10-10 12:42:20 +0200106#endif
107
108void bb_miiphy_init(void)
109{
110 int i;
111
112 for (i = 0; i < bb_miiphy_buses_num; i++) {
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200113#if defined(CONFIG_NEEDS_MANUAL_RELOC)
Luigi 'Comio' Mantellini4ba31ab2009-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 }
127}
128
Ben Warrend7fb9bc2010-07-29 12:56:11 -0700129static inline struct bb_miiphy_bus *bb_miiphy_getbus(const char *devname)
Luigi 'Comio' Mantellini4ba31ab2009-10-10 12:42:20 +0200130{
131#ifdef CONFIG_BITBANGMII_MULTI
132 int i;
133
134 /* Search the correct bus */
135 for (i = 0; i < bb_miiphy_buses_num; i++) {
136 if (!strcmp(bb_miiphy_buses[i].name, devname)) {
137 return &bb_miiphy_buses[i];
138 }
139 }
140 return NULL;
141#else
142 /* We have just one bitbanging bus */
143 return &bb_miiphy_buses[0];
144#endif
145}
wdenkaffae2b2002-08-17 09:36:01 +0000146
wdenkaffae2b2002-08-17 09:36:01 +0000147/*****************************************************************************
148 *
149 * Utility to send the preamble, address, and register (common to read
150 * and write).
151 */
Luigi 'Comio' Mantellini4ba31ab2009-10-10 12:42:20 +0200152static void miiphy_pre(struct bb_miiphy_bus *bus, char read,
Wolfgang Denk49467752009-10-28 00:49:47 +0100153 unsigned char addr, unsigned char reg)
wdenkaffae2b2002-08-17 09:36:01 +0000154{
Luigi 'Comio' Mantellini4ba31ab2009-10-10 12:42:20 +0200155 int j;
wdenkaffae2b2002-08-17 09:36:01 +0000156
Wolfgang Denk2afbe4e2005-08-13 02:04:37 +0200157 /*
158 * Send a 32 bit preamble ('1's) with an extra '1' bit for good measure.
159 * The IEEE spec says this is a PHY optional requirement. The AMD
160 * 79C874 requires one after power up and one after a MII communications
161 * error. This means that we are doing more preambles than we need,
162 * but it is safer and will be much more robust.
163 */
wdenkaffae2b2002-08-17 09:36:01 +0000164
Luigi 'Comio' Mantellini4ba31ab2009-10-10 12:42:20 +0200165 bus->mdio_active(bus);
166 bus->set_mdio(bus, 1);
Wolfgang Denk2afbe4e2005-08-13 02:04:37 +0200167 for (j = 0; j < 32; j++) {
Luigi 'Comio' Mantellini4ba31ab2009-10-10 12:42:20 +0200168 bus->set_mdc(bus, 0);
169 bus->delay(bus);
170 bus->set_mdc(bus, 1);
171 bus->delay(bus);
Wolfgang Denk2afbe4e2005-08-13 02:04:37 +0200172 }
wdenkaffae2b2002-08-17 09:36:01 +0000173
Wolfgang Denk2afbe4e2005-08-13 02:04:37 +0200174 /* send the start bit (01) and the read opcode (10) or write (10) */
Luigi 'Comio' Mantellini4ba31ab2009-10-10 12:42:20 +0200175 bus->set_mdc(bus, 0);
176 bus->set_mdio(bus, 0);
177 bus->delay(bus);
178 bus->set_mdc(bus, 1);
179 bus->delay(bus);
180 bus->set_mdc(bus, 0);
181 bus->set_mdio(bus, 1);
182 bus->delay(bus);
183 bus->set_mdc(bus, 1);
184 bus->delay(bus);
185 bus->set_mdc(bus, 0);
186 bus->set_mdio(bus, read);
187 bus->delay(bus);
188 bus->set_mdc(bus, 1);
189 bus->delay(bus);
190 bus->set_mdc(bus, 0);
191 bus->set_mdio(bus, !read);
192 bus->delay(bus);
193 bus->set_mdc(bus, 1);
194 bus->delay(bus);
wdenkaffae2b2002-08-17 09:36:01 +0000195
Wolfgang Denk2afbe4e2005-08-13 02:04:37 +0200196 /* send the PHY address */
197 for (j = 0; j < 5; j++) {
Luigi 'Comio' Mantellini4ba31ab2009-10-10 12:42:20 +0200198 bus->set_mdc(bus, 0);
Wolfgang Denk2afbe4e2005-08-13 02:04:37 +0200199 if ((addr & 0x10) == 0) {
Luigi 'Comio' Mantellini4ba31ab2009-10-10 12:42:20 +0200200 bus->set_mdio(bus, 0);
Wolfgang Denk2afbe4e2005-08-13 02:04:37 +0200201 } else {
Luigi 'Comio' Mantellini4ba31ab2009-10-10 12:42:20 +0200202 bus->set_mdio(bus, 1);
Wolfgang Denk2afbe4e2005-08-13 02:04:37 +0200203 }
Luigi 'Comio' Mantellini4ba31ab2009-10-10 12:42:20 +0200204 bus->delay(bus);
205 bus->set_mdc(bus, 1);
206 bus->delay(bus);
Wolfgang Denk2afbe4e2005-08-13 02:04:37 +0200207 addr <<= 1;
208 }
wdenkaffae2b2002-08-17 09:36:01 +0000209
Wolfgang Denk2afbe4e2005-08-13 02:04:37 +0200210 /* send the register address */
211 for (j = 0; j < 5; j++) {
Luigi 'Comio' Mantellini4ba31ab2009-10-10 12:42:20 +0200212 bus->set_mdc(bus, 0);
Wolfgang Denk2afbe4e2005-08-13 02:04:37 +0200213 if ((reg & 0x10) == 0) {
Luigi 'Comio' Mantellini4ba31ab2009-10-10 12:42:20 +0200214 bus->set_mdio(bus, 0);
Wolfgang Denk2afbe4e2005-08-13 02:04:37 +0200215 } else {
Luigi 'Comio' Mantellini4ba31ab2009-10-10 12:42:20 +0200216 bus->set_mdio(bus, 1);
Wolfgang Denk2afbe4e2005-08-13 02:04:37 +0200217 }
Luigi 'Comio' Mantellini4ba31ab2009-10-10 12:42:20 +0200218 bus->delay(bus);
219 bus->set_mdc(bus, 1);
220 bus->delay(bus);
Wolfgang Denk2afbe4e2005-08-13 02:04:37 +0200221 reg <<= 1;
222 }
wdenkaffae2b2002-08-17 09:36:01 +0000223}
224
wdenkaffae2b2002-08-17 09:36:01 +0000225/*****************************************************************************
226 *
227 * Read a MII PHY register.
228 *
229 * Returns:
230 * 0 on success
231 */
Joe Hershbergerdfcc4962016-08-08 11:28:39 -0500232int bb_miiphy_read(struct mii_dev *miidev, int addr, int devad, int reg)
wdenkaffae2b2002-08-17 09:36:01 +0000233{
Chris Brandt33bab102017-11-03 08:30:13 -0500234 unsigned short rdreg; /* register working value */
Luigi 'Comio' Mantellini4ba31ab2009-10-10 12:42:20 +0200235 int v;
236 int j; /* counter */
237 struct bb_miiphy_bus *bus;
238
Joe Hershbergerdfcc4962016-08-08 11:28:39 -0500239 bus = bb_miiphy_getbus(miidev->name);
Luigi 'Comio' Mantellini4ba31ab2009-10-10 12:42:20 +0200240 if (bus == NULL) {
241 return -1;
242 }
wdenkaffae2b2002-08-17 09:36:01 +0000243
Luigi 'Comio' Mantellini4ba31ab2009-10-10 12:42:20 +0200244 miiphy_pre (bus, 1, addr, reg);
Wolfgang Denk2afbe4e2005-08-13 02:04:37 +0200245
246 /* tri-state our MDIO I/O pin so we can read */
Luigi 'Comio' Mantellini4ba31ab2009-10-10 12:42:20 +0200247 bus->set_mdc(bus, 0);
248 bus->mdio_tristate(bus);
249 bus->delay(bus);
250 bus->set_mdc(bus, 1);
251 bus->delay(bus);
Wolfgang Denk2afbe4e2005-08-13 02:04:37 +0200252
253 /* check the turnaround bit: the PHY should be driving it to zero */
Luigi 'Comio' Mantellini4ba31ab2009-10-10 12:42:20 +0200254 bus->get_mdio(bus, &v);
255 if (v != 0) {
Wolfgang Denk2afbe4e2005-08-13 02:04:37 +0200256 /* puts ("PHY didn't drive TA low\n"); */
257 for (j = 0; j < 32; j++) {
Luigi 'Comio' Mantellini4ba31ab2009-10-10 12:42:20 +0200258 bus->set_mdc(bus, 0);
259 bus->delay(bus);
260 bus->set_mdc(bus, 1);
261 bus->delay(bus);
Wolfgang Denk2afbe4e2005-08-13 02:04:37 +0200262 }
Joe Hershbergerdfcc4962016-08-08 11:28:39 -0500263 /* There is no PHY, return */
Luigi 'Comio' Mantellini4ba31ab2009-10-10 12:42:20 +0200264 return -1;
Wolfgang Denk2afbe4e2005-08-13 02:04:37 +0200265 }
266
Luigi 'Comio' Mantellini4ba31ab2009-10-10 12:42:20 +0200267 bus->set_mdc(bus, 0);
268 bus->delay(bus);
Wolfgang Denk2afbe4e2005-08-13 02:04:37 +0200269
270 /* read 16 bits of register data, MSB first */
271 rdreg = 0;
272 for (j = 0; j < 16; j++) {
Luigi 'Comio' Mantellini4ba31ab2009-10-10 12:42:20 +0200273 bus->set_mdc(bus, 1);
274 bus->delay(bus);
Wolfgang Denk2afbe4e2005-08-13 02:04:37 +0200275 rdreg <<= 1;
Luigi 'Comio' Mantellini4ba31ab2009-10-10 12:42:20 +0200276 bus->get_mdio(bus, &v);
277 rdreg |= (v & 0x1);
278 bus->set_mdc(bus, 0);
279 bus->delay(bus);
Wolfgang Denk2afbe4e2005-08-13 02:04:37 +0200280 }
281
Luigi 'Comio' Mantellini4ba31ab2009-10-10 12:42:20 +0200282 bus->set_mdc(bus, 1);
283 bus->delay(bus);
284 bus->set_mdc(bus, 0);
285 bus->delay(bus);
286 bus->set_mdc(bus, 1);
287 bus->delay(bus);
Wolfgang Denk2afbe4e2005-08-13 02:04:37 +0200288
Wolfgang Denk2afbe4e2005-08-13 02:04:37 +0200289#ifdef DEBUG
Joe Hershbergerdfcc4962016-08-08 11:28:39 -0500290 printf("miiphy_read(0x%x) @ 0x%x = 0x%04x\n", reg, addr, rdreg);
Wolfgang Denk2afbe4e2005-08-13 02:04:37 +0200291#endif
292
Joe Hershbergerdfcc4962016-08-08 11:28:39 -0500293 return rdreg;
wdenkaffae2b2002-08-17 09:36:01 +0000294}
295
296
297/*****************************************************************************
298 *
299 * Write a MII PHY register.
300 *
301 * Returns:
302 * 0 on success
303 */
Joe Hershbergerdfcc4962016-08-08 11:28:39 -0500304int bb_miiphy_write(struct mii_dev *miidev, int addr, int devad, int reg,
305 u16 value)
wdenkaffae2b2002-08-17 09:36:01 +0000306{
Luigi 'Comio' Mantellini4ba31ab2009-10-10 12:42:20 +0200307 struct bb_miiphy_bus *bus;
Wolfgang Denk2afbe4e2005-08-13 02:04:37 +0200308 int j; /* counter */
wdenkaffae2b2002-08-17 09:36:01 +0000309
Joe Hershbergerdfcc4962016-08-08 11:28:39 -0500310 bus = bb_miiphy_getbus(miidev->name);
Luigi 'Comio' Mantellini4ba31ab2009-10-10 12:42:20 +0200311 if (bus == NULL) {
312 /* Bus not found! */
313 return -1;
314 }
315
316 miiphy_pre (bus, 0, addr, reg);
wdenkaffae2b2002-08-17 09:36:01 +0000317
Wolfgang Denk2afbe4e2005-08-13 02:04:37 +0200318 /* send the turnaround (10) */
Luigi 'Comio' Mantellini4ba31ab2009-10-10 12:42:20 +0200319 bus->set_mdc(bus, 0);
320 bus->set_mdio(bus, 1);
321 bus->delay(bus);
322 bus->set_mdc(bus, 1);
323 bus->delay(bus);
324 bus->set_mdc(bus, 0);
325 bus->set_mdio(bus, 0);
326 bus->delay(bus);
327 bus->set_mdc(bus, 1);
328 bus->delay(bus);
wdenkaffae2b2002-08-17 09:36:01 +0000329
Wolfgang Denk2afbe4e2005-08-13 02:04:37 +0200330 /* write 16 bits of register data, MSB first */
331 for (j = 0; j < 16; j++) {
Luigi 'Comio' Mantellini4ba31ab2009-10-10 12:42:20 +0200332 bus->set_mdc(bus, 0);
Wolfgang Denk2afbe4e2005-08-13 02:04:37 +0200333 if ((value & 0x00008000) == 0) {
Luigi 'Comio' Mantellini4ba31ab2009-10-10 12:42:20 +0200334 bus->set_mdio(bus, 0);
Wolfgang Denk2afbe4e2005-08-13 02:04:37 +0200335 } else {
Luigi 'Comio' Mantellini4ba31ab2009-10-10 12:42:20 +0200336 bus->set_mdio(bus, 1);
Wolfgang Denk2afbe4e2005-08-13 02:04:37 +0200337 }
Luigi 'Comio' Mantellini4ba31ab2009-10-10 12:42:20 +0200338 bus->delay(bus);
339 bus->set_mdc(bus, 1);
340 bus->delay(bus);
Wolfgang Denk2afbe4e2005-08-13 02:04:37 +0200341 value <<= 1;
342 }
wdenkaffae2b2002-08-17 09:36:01 +0000343
Wolfgang Denk2afbe4e2005-08-13 02:04:37 +0200344 /*
345 * Tri-state the MDIO line.
346 */
Luigi 'Comio' Mantellini4ba31ab2009-10-10 12:42:20 +0200347 bus->mdio_tristate(bus);
348 bus->set_mdc(bus, 0);
349 bus->delay(bus);
350 bus->set_mdc(bus, 1);
351 bus->delay(bus);
wdenkaffae2b2002-08-17 09:36:01 +0000352
Wolfgang Denk2afbe4e2005-08-13 02:04:37 +0200353 return 0;
Wolfgang Denk16d1c102009-10-25 23:00:09 +0100354}