1 /* $NetBSD: mii_bitbang.c,v 1.11 2008/04/28 20:23:53 martin Exp $ */
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
34 * Common module for bit-bang'ing the MII.
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: mii_bitbang.c,v 1.11 2008/04/28 20:23:53 martin Exp $");
40 #include <sys/param.h>
41 #include <sys/device.h>
43 #include <dev/mii/mii.h>
44 #include <dev/mii/mii_bitbang.h>
48 ops->mbo_write(sc, (x)); \
50 } while (/* CONSTCOND */ 0)
52 #define READ ops->mbo_read(sc)
54 #define MDO ops->mbo_bits[MII_BIT_MDO]
55 #define MDI ops->mbo_bits[MII_BIT_MDI]
56 #define MDC ops->mbo_bits[MII_BIT_MDC]
57 #define MDIRPHY ops->mbo_bits[MII_BIT_DIR_HOST_PHY]
58 #define MDIRHOST ops->mbo_bits[MII_BIT_DIR_PHY_HOST]
63 * Synchronize the MII.
66 mii_bitbang_sync(device_t sc
, mii_bitbang_ops_t ops
)
74 for (i
= 0; i
< 32; i
++) {
81 * mii_bitbang_sendbits:
83 * Send a series of bits to the MII.
86 mii_bitbang_sendbits(device_t sc
, mii_bitbang_ops_t ops
, uint32_t data
,
95 for (i
= 1 << (nbits
- 1); i
!= 0; i
>>= 1) {
107 * mii_bitbang_readreg:
109 * Read a PHY register by bit-bang'ing the MII.
112 mii_bitbang_readreg(device_t sc
, mii_bitbang_ops_t ops
, int phy
, int reg
)
114 int val
= 0, err
= 0, i
;
116 mii_bitbang_sync(sc
, ops
);
118 mii_bitbang_sendbits(sc
, ops
, MII_COMMAND_START
, 2);
119 mii_bitbang_sendbits(sc
, ops
, MII_COMMAND_READ
, 2);
120 mii_bitbang_sendbits(sc
, ops
, phy
, 5);
121 mii_bitbang_sendbits(sc
, ops
, reg
, 5);
123 /* Switch direction to PHY->host, without a clock transition. */
126 /* Turnaround clock. */
127 WRITE(MDIRHOST
| MDC
);
130 /* Check for error. */
134 WRITE(MDIRHOST
| MDC
);
137 for (i
= 0; i
< 16; i
++) {
139 /* Read data prior to clock low-high transition. */
140 if (err
== 0 && (READ
& MDI
) != 0)
143 WRITE(MDIRHOST
| MDC
);
147 /* Set direction to host->PHY, without a clock transition. */
150 return (err
? 0 : val
);
154 * mii_bitbang_writereg:
156 * Write a PHY register by bit-bang'ing the MII.
159 mii_bitbang_writereg(device_t sc
, mii_bitbang_ops_t ops
, int phy
,
163 mii_bitbang_sync(sc
, ops
);
165 mii_bitbang_sendbits(sc
, ops
, MII_COMMAND_START
, 2);
166 mii_bitbang_sendbits(sc
, ops
, MII_COMMAND_WRITE
, 2);
167 mii_bitbang_sendbits(sc
, ops
, phy
, 5);
168 mii_bitbang_sendbits(sc
, ops
, reg
, 5);
169 mii_bitbang_sendbits(sc
, ops
, MII_COMMAND_ACK
, 2);
170 mii_bitbang_sendbits(sc
, ops
, val
, 16);