mii_bitbang.c revision 1.6 1 1.6 thorpej /* $NetBSD: mii_bitbang.c,v 1.6 2004/08/23 06:18:39 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.5 lukem
44 1.5 lukem #include <sys/cdefs.h>
45 1.6 thorpej __KERNEL_RCSID(0, "$NetBSD: mii_bitbang.c,v 1.6 2004/08/23 06:18:39 thorpej Exp $");
46 1.1 thorpej
47 1.1 thorpej #include <sys/param.h>
48 1.1 thorpej #include <sys/device.h>
49 1.1 thorpej
50 1.1 thorpej #include <dev/mii/mii.h>
51 1.1 thorpej #include <dev/mii/mii_bitbang.h>
52 1.1 thorpej
53 1.1 thorpej #define WRITE(x) \
54 1.1 thorpej do { \
55 1.1 thorpej ops->mbo_write(sc, (x)); \
56 1.1 thorpej delay(1); \
57 1.2 lukem } while (/* CONSTCOND */ 0)
58 1.1 thorpej
59 1.1 thorpej #define READ ops->mbo_read(sc)
60 1.1 thorpej
61 1.1 thorpej #define MDO ops->mbo_bits[MII_BIT_MDO]
62 1.1 thorpej #define MDI ops->mbo_bits[MII_BIT_MDI]
63 1.1 thorpej #define MDC ops->mbo_bits[MII_BIT_MDC]
64 1.1 thorpej #define MDIRPHY ops->mbo_bits[MII_BIT_DIR_HOST_PHY]
65 1.1 thorpej #define MDIRHOST ops->mbo_bits[MII_BIT_DIR_PHY_HOST]
66 1.1 thorpej
67 1.1 thorpej /*
68 1.1 thorpej * mii_bitbang_sync:
69 1.1 thorpej *
70 1.1 thorpej * Synchronize the MII.
71 1.1 thorpej */
72 1.6 thorpej static void
73 1.3 thorpej mii_bitbang_sync(struct device *sc, mii_bitbang_ops_t ops)
74 1.1 thorpej {
75 1.4 perry int i;
76 1.4 perry u_int32_t 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.6 thorpej static 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.4 perry int i;
97 1.4 perry u_int32_t v;
98 1.1 thorpej
99 1.1 thorpej v = MDIRPHY;
100 1.1 thorpej WRITE(v);
101 1.1 thorpej
102 1.1 thorpej for (i = 1 << (nbits - 1); i != 0; i >>= 1) {
103 1.1 thorpej if (data & i)
104 1.1 thorpej v |= MDO;
105 1.1 thorpej else
106 1.1 thorpej v &= ~MDO;
107 1.1 thorpej WRITE(v);
108 1.1 thorpej WRITE(v | MDC);
109 1.1 thorpej WRITE(v);
110 1.1 thorpej }
111 1.1 thorpej }
112 1.1 thorpej
113 1.1 thorpej /*
114 1.1 thorpej * mii_bitbang_readreg:
115 1.1 thorpej *
116 1.1 thorpej * Read a PHY register by bit-bang'ing the MII.
117 1.1 thorpej */
118 1.1 thorpej int
119 1.3 thorpej mii_bitbang_readreg(struct device *sc, mii_bitbang_ops_t ops, int phy, int reg)
120 1.1 thorpej {
121 1.1 thorpej int val = 0, err = 0, i;
122 1.1 thorpej
123 1.1 thorpej mii_bitbang_sync(sc, ops);
124 1.1 thorpej
125 1.1 thorpej mii_bitbang_sendbits(sc, ops, MII_COMMAND_START, 2);
126 1.1 thorpej mii_bitbang_sendbits(sc, ops, MII_COMMAND_READ, 2);
127 1.1 thorpej mii_bitbang_sendbits(sc, ops, phy, 5);
128 1.1 thorpej mii_bitbang_sendbits(sc, ops, reg, 5);
129 1.1 thorpej
130 1.1 thorpej /* Switch direction to PHY->host, without a clock transition. */
131 1.1 thorpej WRITE(MDIRHOST);
132 1.1 thorpej
133 1.1 thorpej /* Turnaround clock. */
134 1.1 thorpej WRITE(MDIRHOST | MDC);
135 1.1 thorpej WRITE(MDIRHOST);
136 1.1 thorpej
137 1.1 thorpej /* Check for error. */
138 1.1 thorpej err = READ & MDI;
139 1.1 thorpej
140 1.1 thorpej /* Idle clock. */
141 1.1 thorpej WRITE(MDIRHOST | MDC);
142 1.1 thorpej WRITE(MDIRHOST);
143 1.1 thorpej
144 1.1 thorpej for (i = 0; i < 16; i++) {
145 1.1 thorpej val <<= 1;
146 1.1 thorpej /* Read data prior to clock low-high transition. */
147 1.1 thorpej if (err == 0 && (READ & MDI) != 0)
148 1.1 thorpej val |= 1;
149 1.1 thorpej
150 1.1 thorpej WRITE(MDIRHOST | MDC);
151 1.1 thorpej WRITE(MDIRHOST);
152 1.1 thorpej }
153 1.1 thorpej
154 1.1 thorpej /* Set direction to host->PHY, without a clock transition. */
155 1.1 thorpej WRITE(MDIRPHY);
156 1.1 thorpej
157 1.1 thorpej return (err ? 0 : val);
158 1.1 thorpej }
159 1.1 thorpej
160 1.1 thorpej /*
161 1.1 thorpej * mii_bitbang_writereg:
162 1.1 thorpej *
163 1.1 thorpej * Write a PHY register by bit-bang'ing the MII.
164 1.1 thorpej */
165 1.1 thorpej void
166 1.3 thorpej mii_bitbang_writereg(struct device *sc, mii_bitbang_ops_t ops, int phy,
167 1.3 thorpej int reg, int val)
168 1.1 thorpej {
169 1.1 thorpej
170 1.1 thorpej mii_bitbang_sync(sc, ops);
171 1.1 thorpej
172 1.1 thorpej mii_bitbang_sendbits(sc, ops, MII_COMMAND_START, 2);
173 1.1 thorpej mii_bitbang_sendbits(sc, ops, MII_COMMAND_WRITE, 2);
174 1.1 thorpej mii_bitbang_sendbits(sc, ops, phy, 5);
175 1.1 thorpej mii_bitbang_sendbits(sc, ops, reg, 5);
176 1.1 thorpej mii_bitbang_sendbits(sc, ops, MII_COMMAND_ACK, 2);
177 1.1 thorpej mii_bitbang_sendbits(sc, ops, val, 16);
178 1.1 thorpej
179 1.1 thorpej WRITE(MDIRPHY);
180 1.1 thorpej }
181