mii_bitbang.c revision 1.3 1 1.3 thorpej /* $NetBSD: mii_bitbang.c,v 1.3 2001/08/25 18:04:01 thorpej Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.1 thorpej * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.1 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.1 thorpej * NASA Ames Research Center.
10 1.1 thorpej *
11 1.1 thorpej * Redistribution and use in source and binary forms, with or without
12 1.1 thorpej * modification, are permitted provided that the following conditions
13 1.1 thorpej * are met:
14 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
15 1.1 thorpej * notice, this list of conditions and the following disclaimer.
16 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
18 1.1 thorpej * documentation and/or other materials provided with the distribution.
19 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
20 1.1 thorpej * must display the following acknowledgement:
21 1.1 thorpej * This product includes software developed by the NetBSD
22 1.1 thorpej * Foundation, Inc. and its contributors.
23 1.1 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.1 thorpej * contributors may be used to endorse or promote products derived
25 1.1 thorpej * from this software without specific prior written permission.
26 1.1 thorpej *
27 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
38 1.1 thorpej */
39 1.1 thorpej
40 1.1 thorpej /*
41 1.1 thorpej * Common module for bit-bang'ing the MII.
42 1.1 thorpej */
43 1.1 thorpej
44 1.1 thorpej #include <sys/param.h>
45 1.1 thorpej #include <sys/device.h>
46 1.1 thorpej
47 1.1 thorpej #include <dev/mii/mii.h>
48 1.1 thorpej #include <dev/mii/mii_bitbang.h>
49 1.1 thorpej
50 1.3 thorpej void mii_bitbang_sync(struct device *, mii_bitbang_ops_t);
51 1.3 thorpej void mii_bitbang_sendbits(struct device *, mii_bitbang_ops_t,
52 1.3 thorpej u_int32_t, int);
53 1.1 thorpej
54 1.1 thorpej #define WRITE(x) \
55 1.1 thorpej do { \
56 1.1 thorpej ops->mbo_write(sc, (x)); \
57 1.1 thorpej delay(1); \
58 1.2 lukem } while (/* CONSTCOND */ 0)
59 1.1 thorpej
60 1.1 thorpej #define READ ops->mbo_read(sc)
61 1.1 thorpej
62 1.1 thorpej #define MDO ops->mbo_bits[MII_BIT_MDO]
63 1.1 thorpej #define MDI ops->mbo_bits[MII_BIT_MDI]
64 1.1 thorpej #define MDC ops->mbo_bits[MII_BIT_MDC]
65 1.1 thorpej #define MDIRPHY ops->mbo_bits[MII_BIT_DIR_HOST_PHY]
66 1.1 thorpej #define MDIRHOST ops->mbo_bits[MII_BIT_DIR_PHY_HOST]
67 1.1 thorpej
68 1.1 thorpej /*
69 1.1 thorpej * mii_bitbang_sync:
70 1.1 thorpej *
71 1.1 thorpej * Synchronize the MII.
72 1.1 thorpej */
73 1.1 thorpej void
74 1.3 thorpej mii_bitbang_sync(struct device *sc, mii_bitbang_ops_t ops)
75 1.1 thorpej {
76 1.1 thorpej int i, v;
77 1.1 thorpej
78 1.1 thorpej v = MDIRPHY | MDO;
79 1.1 thorpej
80 1.1 thorpej WRITE(v);
81 1.1 thorpej for (i = 0; i < 32; i++) {
82 1.1 thorpej WRITE(v | MDC);
83 1.1 thorpej WRITE(v);
84 1.1 thorpej }
85 1.1 thorpej }
86 1.1 thorpej
87 1.1 thorpej /*
88 1.1 thorpej * mii_bitbang_sendbits:
89 1.1 thorpej *
90 1.1 thorpej * Send a series of bits to the MII.
91 1.1 thorpej */
92 1.1 thorpej void
93 1.3 thorpej mii_bitbang_sendbits(struct device *sc, mii_bitbang_ops_t ops, uint32_t data,
94 1.3 thorpej int nbits)
95 1.1 thorpej {
96 1.1 thorpej int i, v;
97 1.1 thorpej
98 1.1 thorpej v = MDIRPHY;
99 1.1 thorpej WRITE(v);
100 1.1 thorpej
101 1.1 thorpej for (i = 1 << (nbits - 1); i != 0; i >>= 1) {
102 1.1 thorpej if (data & i)
103 1.1 thorpej v |= MDO;
104 1.1 thorpej else
105 1.1 thorpej v &= ~MDO;
106 1.1 thorpej WRITE(v);
107 1.1 thorpej WRITE(v | MDC);
108 1.1 thorpej WRITE(v);
109 1.1 thorpej }
110 1.1 thorpej }
111 1.1 thorpej
112 1.1 thorpej /*
113 1.1 thorpej * mii_bitbang_readreg:
114 1.1 thorpej *
115 1.1 thorpej * Read a PHY register by bit-bang'ing the MII.
116 1.1 thorpej */
117 1.1 thorpej int
118 1.3 thorpej mii_bitbang_readreg(struct device *sc, mii_bitbang_ops_t ops, int phy, int reg)
119 1.1 thorpej {
120 1.1 thorpej int val = 0, err = 0, i;
121 1.1 thorpej
122 1.1 thorpej mii_bitbang_sync(sc, ops);
123 1.1 thorpej
124 1.1 thorpej mii_bitbang_sendbits(sc, ops, MII_COMMAND_START, 2);
125 1.1 thorpej mii_bitbang_sendbits(sc, ops, MII_COMMAND_READ, 2);
126 1.1 thorpej mii_bitbang_sendbits(sc, ops, phy, 5);
127 1.1 thorpej mii_bitbang_sendbits(sc, ops, reg, 5);
128 1.1 thorpej
129 1.1 thorpej /* Switch direction to PHY->host, without a clock transition. */
130 1.1 thorpej WRITE(MDIRHOST);
131 1.1 thorpej
132 1.1 thorpej /* Turnaround clock. */
133 1.1 thorpej WRITE(MDIRHOST | MDC);
134 1.1 thorpej WRITE(MDIRHOST);
135 1.1 thorpej
136 1.1 thorpej /* Check for error. */
137 1.1 thorpej err = READ & MDI;
138 1.1 thorpej
139 1.1 thorpej /* Idle clock. */
140 1.1 thorpej WRITE(MDIRHOST | MDC);
141 1.1 thorpej WRITE(MDIRHOST);
142 1.1 thorpej
143 1.1 thorpej for (i = 0; i < 16; i++) {
144 1.1 thorpej val <<= 1;
145 1.1 thorpej /* Read data prior to clock low-high transition. */
146 1.1 thorpej if (err == 0 && (READ & MDI) != 0)
147 1.1 thorpej val |= 1;
148 1.1 thorpej
149 1.1 thorpej WRITE(MDIRHOST | MDC);
150 1.1 thorpej WRITE(MDIRHOST);
151 1.1 thorpej }
152 1.1 thorpej
153 1.1 thorpej /* Set direction to host->PHY, without a clock transition. */
154 1.1 thorpej WRITE(MDIRPHY);
155 1.1 thorpej
156 1.1 thorpej return (err ? 0 : val);
157 1.1 thorpej }
158 1.1 thorpej
159 1.1 thorpej /*
160 1.1 thorpej * mii_bitbang_writereg:
161 1.1 thorpej *
162 1.1 thorpej * Write a PHY register by bit-bang'ing the MII.
163 1.1 thorpej */
164 1.1 thorpej void
165 1.3 thorpej mii_bitbang_writereg(struct device *sc, mii_bitbang_ops_t ops, int phy,
166 1.3 thorpej int reg, int val)
167 1.1 thorpej {
168 1.1 thorpej
169 1.1 thorpej mii_bitbang_sync(sc, ops);
170 1.1 thorpej
171 1.1 thorpej mii_bitbang_sendbits(sc, ops, MII_COMMAND_START, 2);
172 1.1 thorpej mii_bitbang_sendbits(sc, ops, MII_COMMAND_WRITE, 2);
173 1.1 thorpej mii_bitbang_sendbits(sc, ops, phy, 5);
174 1.1 thorpej mii_bitbang_sendbits(sc, ops, reg, 5);
175 1.1 thorpej mii_bitbang_sendbits(sc, ops, MII_COMMAND_ACK, 2);
176 1.1 thorpej mii_bitbang_sendbits(sc, ops, val, 16);
177 1.1 thorpej
178 1.1 thorpej WRITE(MDIRPHY);
179 1.1 thorpej }
180