dm9000.c revision 1.1 1 1.1 ahoka /* $NetBSD: dm9000.c,v 1.1 2010/09/08 22:01:29 ahoka Exp $ */
2 1.1 ahoka
3 1.1 ahoka /*
4 1.1 ahoka * Copyright (c) 2009 Paul Fleischer
5 1.1 ahoka * All rights reserved.
6 1.1 ahoka *
7 1.1 ahoka * 1. Redistributions of source code must retain the above copyright
8 1.1 ahoka * notice, this list of conditions and the following disclaimer.
9 1.1 ahoka * 2. Redistributions in binary form must reproduce the above copyright
10 1.1 ahoka * notice, this list of conditions and the following disclaimer in the
11 1.1 ahoka * documentation and/or other materials provided with the distribution.
12 1.1 ahoka * 3. The name of the company nor the name of the author may be used to
13 1.1 ahoka * endorse or promote products derived from this software without specific
14 1.1 ahoka * prior written permission.
15 1.1 ahoka *
16 1.1 ahoka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 1.1 ahoka * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 1.1 ahoka * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 ahoka * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
20 1.1 ahoka * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 1.1 ahoka * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 1.1 ahoka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 ahoka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 ahoka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 ahoka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 ahoka * SUCH DAMAGE.
27 1.1 ahoka */
28 1.1 ahoka
29 1.1 ahoka /* based on sys/dev/ic/cs89x0.c */
30 1.1 ahoka /*
31 1.1 ahoka * Copyright (c) 2004 Christopher Gilbert
32 1.1 ahoka * All rights reserved.
33 1.1 ahoka *
34 1.1 ahoka * 1. Redistributions of source code must retain the above copyright
35 1.1 ahoka * notice, this list of conditions and the following disclaimer.
36 1.1 ahoka * 2. Redistributions in binary form must reproduce the above copyright
37 1.1 ahoka * notice, this list of conditions and the following disclaimer in the
38 1.1 ahoka * documentation and/or other materials provided with the distribution.
39 1.1 ahoka * 3. The name of the company nor the name of the author may be used to
40 1.1 ahoka * endorse or promote products derived from this software without specific
41 1.1 ahoka * prior written permission.
42 1.1 ahoka *
43 1.1 ahoka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
44 1.1 ahoka * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
45 1.1 ahoka * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
46 1.1 ahoka * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
47 1.1 ahoka * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
48 1.1 ahoka * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
49 1.1 ahoka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 1.1 ahoka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 1.1 ahoka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 1.1 ahoka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 1.1 ahoka * SUCH DAMAGE.
54 1.1 ahoka */
55 1.1 ahoka
56 1.1 ahoka /*
57 1.1 ahoka * Copyright 1997
58 1.1 ahoka * Digital Equipment Corporation. All rights reserved.
59 1.1 ahoka *
60 1.1 ahoka * This software is furnished under license and may be used and
61 1.1 ahoka * copied only in accordance with the following terms and conditions.
62 1.1 ahoka * Subject to these conditions, you may download, copy, install,
63 1.1 ahoka * use, modify and distribute this software in source and/or binary
64 1.1 ahoka * form. No title or ownership is transferred hereby.
65 1.1 ahoka *
66 1.1 ahoka * 1) Any source code used, modified or distributed must reproduce
67 1.1 ahoka * and retain this copyright notice and list of conditions as
68 1.1 ahoka * they appear in the source file.
69 1.1 ahoka *
70 1.1 ahoka * 2) No right is granted to use any trade name, trademark, or logo of
71 1.1 ahoka * Digital Equipment Corporation. Neither the "Digital Equipment
72 1.1 ahoka * Corporation" name nor any trademark or logo of Digital Equipment
73 1.1 ahoka * Corporation may be used to endorse or promote products derived
74 1.1 ahoka * from this software without the prior written permission of
75 1.1 ahoka * Digital Equipment Corporation.
76 1.1 ahoka *
77 1.1 ahoka * 3) This software is provided "AS-IS" and any express or implied
78 1.1 ahoka * warranties, including but not limited to, any implied warranties
79 1.1 ahoka * of merchantability, fitness for a particular purpose, or
80 1.1 ahoka * non-infringement are disclaimed. In no event shall DIGITAL be
81 1.1 ahoka * liable for any damages whatsoever, and in particular, DIGITAL
82 1.1 ahoka * shall not be liable for special, indirect, consequential, or
83 1.1 ahoka * incidental damages or damages for lost profits, loss of
84 1.1 ahoka * revenue or loss of use, whether such damages arise in contract,
85 1.1 ahoka * negligence, tort, under statute, in equity, at law or otherwise,
86 1.1 ahoka * even if advised of the possibility of such damage.
87 1.1 ahoka */
88 1.1 ahoka
89 1.1 ahoka #include <sys/cdefs.h>
90 1.1 ahoka
91 1.1 ahoka #include <sys/param.h>
92 1.1 ahoka #include <sys/systm.h>
93 1.1 ahoka #include <sys/mbuf.h>
94 1.1 ahoka #include <sys/syslog.h>
95 1.1 ahoka #include <sys/socket.h>
96 1.1 ahoka #include <sys/device.h>
97 1.1 ahoka #include <sys/malloc.h>
98 1.1 ahoka #include <sys/ioctl.h>
99 1.1 ahoka #include <sys/errno.h>
100 1.1 ahoka
101 1.1 ahoka #include <net/if.h>
102 1.1 ahoka #include <net/if_ether.h>
103 1.1 ahoka #include <net/if_media.h>
104 1.1 ahoka #ifdef INET
105 1.1 ahoka #include <netinet/in.h>
106 1.1 ahoka #include <netinet/if_inarp.h>
107 1.1 ahoka #endif
108 1.1 ahoka
109 1.1 ahoka #include <net/bpf.h>
110 1.1 ahoka #include <net/bpfdesc.h>
111 1.1 ahoka
112 1.1 ahoka #include <sys/bus.h>
113 1.1 ahoka #include <sys/intr.h>
114 1.1 ahoka
115 1.1 ahoka #include <dev/ic/dm9000var.h>
116 1.1 ahoka #include <dev/ic/dm9000reg.h>
117 1.1 ahoka
118 1.1 ahoka #if 1
119 1.1 ahoka #undef DM9000_DEBUG
120 1.1 ahoka #undef DM9000_TX_DEBUG
121 1.1 ahoka #undef DM9000_TX_DATA_DEBUG
122 1.1 ahoka #undef DM9000_RX_DEBUG
123 1.1 ahoka #undef DM9000_RX_DATA_DEBUG
124 1.1 ahoka #else
125 1.1 ahoka #define DM9000_DEBUG
126 1.1 ahoka #define DM9000_TX_DEBUG
127 1.1 ahoka #define DM9000_TX_DATA_DEBUG
128 1.1 ahoka #define DM9000_RX_DEBUG
129 1.1 ahoka #define DM9000_RX_DATA_DEBUG
130 1.1 ahoka #endif
131 1.1 ahoka
132 1.1 ahoka #ifdef DM9000_DEBUG
133 1.1 ahoka #define DPRINTF(s) do {printf s; } while (/*CONSTCOND*/0)
134 1.1 ahoka #else
135 1.1 ahoka #define DPRINTF(s) do {} while (/*CONSTCOND*/0)
136 1.1 ahoka #endif
137 1.1 ahoka
138 1.1 ahoka #ifdef DM9000_TX_DEBUG
139 1.1 ahoka #define TX_DPRINTF(s) do {printf s; } while (/*CONSTCOND*/0)
140 1.1 ahoka #else
141 1.1 ahoka #define TX_DPRINTF(s) do {} while (/*CONSTCOND*/0)
142 1.1 ahoka #endif
143 1.1 ahoka
144 1.1 ahoka #ifdef DM9000_RX_DEBUG
145 1.1 ahoka #define RX_DPRINTF(s) do {printf s; } while (/*CONSTCOND*/0)
146 1.1 ahoka #else
147 1.1 ahoka #define RX_DPRINTF(s) do {} while (/*CONSTCOND*/0)
148 1.1 ahoka #endif
149 1.1 ahoka
150 1.1 ahoka #ifdef DM9000_RX_DATA_DEBUG
151 1.1 ahoka #define RX_DATA_DPRINTF(s) do {printf s; } while (/*CONSTCOND*/0)
152 1.1 ahoka #else
153 1.1 ahoka #define RX_DATA_DPRINTF(s) do {} while (/*CONSTCOND*/0)
154 1.1 ahoka #endif
155 1.1 ahoka
156 1.1 ahoka #ifdef DM9000_TX_DATA_DEBUG
157 1.1 ahoka #define TX_DATA_DPRINTF(s) do {printf s; } while (/*CONSTCOND*/0)
158 1.1 ahoka #else
159 1.1 ahoka #define TX_DATA_DPRINTF(s) do {} while (/*CONSTCOND*/0)
160 1.1 ahoka #endif
161 1.1 ahoka
162 1.1 ahoka
163 1.1 ahoka uint16_t dme_phy_read(struct dme_softc *sc, int reg);
164 1.1 ahoka void dme_phy_write(struct dme_softc *sc, int reg, uint16_t value);
165 1.1 ahoka
166 1.1 ahoka /*** Methods registered in struct ifnet ***/
167 1.1 ahoka void dme_start_output(struct ifnet *ifp);
168 1.1 ahoka int dme_init(struct ifnet *ifp);
169 1.1 ahoka int dme_ioctl(struct ifnet *ifp, u_long cmd, void *data);
170 1.1 ahoka void dme_stop(struct ifnet *ifp, int disable);
171 1.1 ahoka
172 1.1 ahoka int dme_mediachange(struct ifnet *ifp);
173 1.1 ahoka void dme_mediastatus(struct ifnet *ufp, struct ifmediareq *ifmr);
174 1.1 ahoka
175 1.1 ahoka /*** Internal methods ***/
176 1.1 ahoka
177 1.1 ahoka /* Prepare data to be transmitted (i.e. dequeue and load it into the DM9000) */
178 1.1 ahoka void dme_prepare(struct dme_softc *sc, struct ifnet *ifp);
179 1.1 ahoka
180 1.1 ahoka /* Transmit prepared data */
181 1.1 ahoka void dme_transmit(struct dme_softc *sc);
182 1.1 ahoka
183 1.1 ahoka /* Receive data */
184 1.1 ahoka void dme_receive(struct dme_softc *sc, struct ifnet *ifp);
185 1.1 ahoka
186 1.1 ahoka /* Software Initialize/Reset of the DM9000 */
187 1.1 ahoka void dme_reset(struct dme_softc *sc);
188 1.1 ahoka
189 1.1 ahoka uint16_t
190 1.1 ahoka dme_phy_read(struct dme_softc *sc, int reg)
191 1.1 ahoka {
192 1.1 ahoka uint16_t val;
193 1.1 ahoka /* Select Register to read*/
194 1.1 ahoka dme_write(sc, DM9000_EPAR, DM9000_EPAR_INT_PHY +
195 1.1 ahoka (reg & DM9000_EPAR_EROA_MASK));
196 1.1 ahoka /* Select read operation (DM9000_EPCR_ERPRR) from the PHY */
197 1.1 ahoka dme_write(sc, DM9000_EPCR, DM9000_EPCR_ERPRR + DM9000_EPCR_EPOS_PHY);
198 1.1 ahoka
199 1.1 ahoka /* Wait until access to PHY has completed */
200 1.1 ahoka while (dme_read(sc, DM9000_EPCR) & DM9000_EPCR_ERRE);
201 1.1 ahoka
202 1.1 ahoka /* XXX: The delay is probably not necessary as we just busy-waited */
203 1.1 ahoka delay(200);
204 1.1 ahoka
205 1.1 ahoka /* Reset ERPRR-bit */
206 1.1 ahoka dme_write(sc, DM9000_EPCR, DM9000_EPCR_EPOS_PHY);
207 1.1 ahoka
208 1.1 ahoka val = dme_read(sc, DM9000_EPDRL);
209 1.1 ahoka val += dme_read(sc, DM9000_EPDRH) << 8;
210 1.1 ahoka
211 1.1 ahoka return val;
212 1.1 ahoka }
213 1.1 ahoka
214 1.1 ahoka void
215 1.1 ahoka dme_phy_write(struct dme_softc *sc, int reg, uint16_t value)
216 1.1 ahoka {
217 1.1 ahoka /* Select Register to write*/
218 1.1 ahoka dme_write(sc, DM9000_EPAR, DM9000_EPAR_INT_PHY +
219 1.1 ahoka (reg & DM9000_EPAR_EROA_MASK));
220 1.1 ahoka
221 1.1 ahoka /* Write data to the two data registers */
222 1.1 ahoka dme_write(sc, DM9000_EPDRL, value & 0xFF);
223 1.1 ahoka dme_write(sc, DM9000_EPDRH, (value >> 8) & 0xFF);
224 1.1 ahoka
225 1.1 ahoka /* Select write operation (DM9000_EPCR_ERPRW) from the PHY */
226 1.1 ahoka dme_write(sc, DM9000_EPCR, DM9000_EPCR_ERPRW + DM9000_EPCR_EPOS_PHY);
227 1.1 ahoka
228 1.1 ahoka /* Wait until access to PHY has completed */
229 1.1 ahoka while(dme_read(sc, DM9000_EPCR) & DM9000_EPCR_ERRE);
230 1.1 ahoka
231 1.1 ahoka
232 1.1 ahoka /* XXX: The delay is probably not necessary as we just busy-waited */
233 1.1 ahoka delay(200);
234 1.1 ahoka
235 1.1 ahoka /* Reset ERPRR-bit */
236 1.1 ahoka dme_write(sc, DM9000_EPCR, DM9000_EPCR_EPOS_PHY);
237 1.1 ahoka }
238 1.1 ahoka
239 1.1 ahoka int
240 1.1 ahoka dme_attach(struct dme_softc *sc, uint8_t *enaddr)
241 1.1 ahoka {
242 1.1 ahoka struct ifnet *ifp = &sc->sc_ethercom.ec_if;
243 1.1 ahoka uint8_t b[2];
244 1.1 ahoka
245 1.1 ahoka dme_read_c(sc, DM9000_VID0, b, 2);
246 1.1 ahoka #if BYTE_ORDER == BIG_ENDIAN
247 1.1 ahoka sc->sc_vendor_id = (b[0] << 8) | b[1];
248 1.1 ahoka #else
249 1.1 ahoka sc->sc_vendor_id = b[0] | (b[1] << 8);
250 1.1 ahoka #endif
251 1.1 ahoka dme_read_c(sc, DM9000_PID0, b, 2);
252 1.1 ahoka #if BYTE_ORDER == BIG_ENDIAN
253 1.1 ahoka sc->sc_product_id = (b[0] << 8) | b[1];
254 1.1 ahoka #else
255 1.1 ahoka sc->sc_product_id = b[0] | (b[1] << 8);
256 1.1 ahoka #endif
257 1.1 ahoka /* TODO: Check the vendor ID as well */
258 1.1 ahoka if (sc->sc_product_id != 0x9000) {
259 1.1 ahoka panic("dme_attach: product id mismatch (0x%hx != 0x9000)",
260 1.1 ahoka sc->sc_product_id);
261 1.1 ahoka }
262 1.1 ahoka
263 1.1 ahoka #if 0
264 1.1 ahoka {
265 1.1 ahoka /* Force 10Mbps to test dme_phy_write */
266 1.1 ahoka uint16_t bmcr;
267 1.1 ahoka bmcr = dme_phy_read(sc, DM9000_PHY_BMCR);
268 1.1 ahoka bmcr &= ~DM9000_PHY_BMCR_AUTO_NEG_EN;
269 1.1 ahoka bmcr &= ~DM9000_PHY_BMCR_SPEED_SELECT; /* select 100Mbps */
270 1.1 ahoka dme_phy_write(sc, DM9000_PHY_BMCR, bmcr);
271 1.1 ahoka }
272 1.1 ahoka #endif
273 1.1 ahoka /* Initialize ifnet structure. */
274 1.1 ahoka strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
275 1.1 ahoka ifp->if_softc = sc;
276 1.1 ahoka ifp->if_start = dme_start_output;
277 1.1 ahoka ifp->if_init = dme_init;
278 1.1 ahoka ifp->if_ioctl = dme_ioctl;
279 1.1 ahoka ifp->if_stop = dme_stop;
280 1.1 ahoka ifp->if_watchdog = NULL; /* no watchdog at this stage */
281 1.1 ahoka ifp->if_flags = IFF_SIMPLEX | IFF_NOTRAILERS |
282 1.1 ahoka IFF_BROADCAST; /* No multicast support for now */
283 1.1 ahoka IFQ_SET_READY(&ifp->if_snd);
284 1.1 ahoka
285 1.1 ahoka /* Initialize ifmedia structures. */
286 1.1 ahoka ifmedia_init(&sc->sc_media, 0, dme_mediachange, dme_mediastatus);
287 1.1 ahoka ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_10_T|IFM_100_TX, 0, NULL);
288 1.1 ahoka ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_10_T|IFM_100_TX);
289 1.1 ahoka
290 1.1 ahoka if (enaddr != NULL)
291 1.1 ahoka memcpy(sc->sc_enaddr, enaddr, sizeof(sc->sc_enaddr));
292 1.1 ahoka
293 1.1 ahoka /* Configure DM9000 with the MAC address */
294 1.1 ahoka dme_write_c(sc, DM9000_PAB0, sc->sc_enaddr, 6);
295 1.1 ahoka
296 1.1 ahoka #ifdef DM9000_DEBUG
297 1.1 ahoka {
298 1.1 ahoka uint8_t macAddr[6];
299 1.1 ahoka dme_read_c(sc, DM9000_PAB0, macAddr, 6);
300 1.1 ahoka printf("DM9000 configured with MAC address: ");
301 1.1 ahoka for (int i = 0; i < 6; i++) {
302 1.1 ahoka printf("%02X:", macAddr[i]);
303 1.1 ahoka }
304 1.1 ahoka printf("\n");
305 1.1 ahoka }
306 1.1 ahoka #endif
307 1.1 ahoka
308 1.1 ahoka if_attach(ifp);
309 1.1 ahoka ether_ifattach(ifp, sc->sc_enaddr);
310 1.1 ahoka
311 1.1 ahoka #ifdef DM9000_DEBUG
312 1.1 ahoka {
313 1.1 ahoka uint8_t network_state;
314 1.1 ahoka network_state = dme_read(sc, DM9000_NSR);
315 1.1 ahoka printf("DM9000 Link status: ");
316 1.1 ahoka if (network_state & DM9000_NSR_LINKST) {
317 1.1 ahoka if (network_state & DM9000_NSR_SPEED)
318 1.1 ahoka printf("10Mbps");
319 1.1 ahoka else
320 1.1 ahoka printf("100Mbps");
321 1.1 ahoka } else {
322 1.1 ahoka printf("Down");
323 1.1 ahoka }
324 1.1 ahoka printf("\n");
325 1.1 ahoka }
326 1.1 ahoka #endif
327 1.1 ahoka
328 1.1 ahoka sc->io_mode = (dme_read(sc, DM9000_ISR) &
329 1.1 ahoka DM9000_IOMODE_MASK) >> DM9000_IOMODE_SHIFT;
330 1.1 ahoka if (sc->io_mode != DM9000_MODE_16BIT )
331 1.1 ahoka panic("DM9000: Only 16-bit mode is supported!\n");
332 1.1 ahoka #ifdef DM9000_DEBUG
333 1.1 ahoka printf("DM9000 Operation Mode: ");
334 1.1 ahoka switch( sc->io_mode) {
335 1.1 ahoka case DM9000_MODE_16BIT:
336 1.1 ahoka printf("16-bit mode");
337 1.1 ahoka break;
338 1.1 ahoka case DM9000_MODE_32BIT:
339 1.1 ahoka printf("32-bit mode");
340 1.1 ahoka break;
341 1.1 ahoka case DM9000_MODE_8BIT:
342 1.1 ahoka printf("8-bit mode");
343 1.1 ahoka break;
344 1.1 ahoka case 3:
345 1.1 ahoka printf("Invalid mode");
346 1.1 ahoka break;
347 1.1 ahoka }
348 1.1 ahoka printf("\n");
349 1.1 ahoka #endif
350 1.1 ahoka
351 1.1 ahoka return 0;
352 1.1 ahoka }
353 1.1 ahoka
354 1.1 ahoka int dme_intr(void *arg)
355 1.1 ahoka {
356 1.1 ahoka struct dme_softc *sc = arg;
357 1.1 ahoka struct ifnet *ifp = &sc->sc_ethercom.ec_if;
358 1.1 ahoka uint8_t status;
359 1.1 ahoka
360 1.1 ahoka /* Disable interrupts */
361 1.1 ahoka dme_write(sc, DM9000_IMR, DM9000_IMR_PAR );
362 1.1 ahoka
363 1.1 ahoka status = dme_read(sc, DM9000_ISR);
364 1.1 ahoka dme_write(sc, DM9000_ISR, status);
365 1.1 ahoka
366 1.1 ahoka if (status & DM9000_ISR_PRS) {
367 1.1 ahoka if (ifp->if_flags & IFF_RUNNING )
368 1.1 ahoka dme_receive(sc, ifp);
369 1.1 ahoka }
370 1.1 ahoka if (status & DM9000_ISR_PTS) {
371 1.1 ahoka uint8_t nsr;
372 1.1 ahoka uint8_t tx_status = 0x01; /* Initialize to an error value */
373 1.1 ahoka
374 1.1 ahoka /* A packet has been transmitted */
375 1.1 ahoka sc->txbusy = 0;
376 1.1 ahoka
377 1.1 ahoka nsr = dme_read(sc, DM9000_NSR);
378 1.1 ahoka
379 1.1 ahoka if (nsr & DM9000_NSR_TX1END) {
380 1.1 ahoka tx_status = dme_read(sc, DM9000_TSR1);
381 1.1 ahoka TX_DPRINTF(("dme_intr: Sent using channel 0\n"));
382 1.1 ahoka } else if (nsr & DM9000_NSR_TX2END) {
383 1.1 ahoka tx_status = dme_read(sc, DM9000_TSR2);
384 1.1 ahoka TX_DPRINTF(("dme_intr: Sent using channel 1\n"));
385 1.1 ahoka }
386 1.1 ahoka
387 1.1 ahoka if (tx_status == 0x0) {
388 1.1 ahoka /* Frame successfully sent */
389 1.1 ahoka ifp->if_opackets++;
390 1.1 ahoka } else {
391 1.1 ahoka ifp->if_oerrors++;
392 1.1 ahoka }
393 1.1 ahoka
394 1.1 ahoka /* If we have nothing ready to transmit, prepare something */
395 1.1 ahoka if (!sc->txready) {
396 1.1 ahoka dme_prepare(sc, ifp);
397 1.1 ahoka }
398 1.1 ahoka
399 1.1 ahoka if (sc->txready)
400 1.1 ahoka dme_transmit(sc);
401 1.1 ahoka
402 1.1 ahoka /* Prepare the next frame */
403 1.1 ahoka dme_prepare(sc, ifp);
404 1.1 ahoka
405 1.1 ahoka }
406 1.1 ahoka #ifdef notyet
407 1.1 ahoka if (status & DM9000_ISR_LNKCHNG) {
408 1.1 ahoka }
409 1.1 ahoka #endif
410 1.1 ahoka
411 1.1 ahoka #ifdef DIAGNOSTIC
412 1.1 ahoka sc->sc_inside_interrupt = false;
413 1.1 ahoka #endif
414 1.1 ahoka
415 1.1 ahoka /* Enable interrupts again */
416 1.1 ahoka dme_write(sc, DM9000_IMR, DM9000_IMR_PAR | DM9000_IMR_PRM |
417 1.1 ahoka DM9000_IMR_PTM);
418 1.1 ahoka
419 1.1 ahoka return 1;
420 1.1 ahoka }
421 1.1 ahoka
422 1.1 ahoka void
423 1.1 ahoka dme_start_output(struct ifnet *ifp)
424 1.1 ahoka {
425 1.1 ahoka struct dme_softc *sc;
426 1.1 ahoka
427 1.1 ahoka sc = ifp->if_softc;
428 1.1 ahoka
429 1.1 ahoka if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING) {
430 1.1 ahoka printf("No output\n");
431 1.1 ahoka return;
432 1.1 ahoka }
433 1.1 ahoka
434 1.1 ahoka if (sc->txbusy && sc->txready) {
435 1.1 ahoka panic("DM9000: Internal error, trying to send without"
436 1.1 ahoka " any empty queue\n");
437 1.1 ahoka }
438 1.1 ahoka
439 1.1 ahoka dme_prepare(sc, ifp);
440 1.1 ahoka
441 1.1 ahoka if (sc->txbusy == 0) {
442 1.1 ahoka /* We are ready to transmit right away */
443 1.1 ahoka dme_transmit(sc);
444 1.1 ahoka dme_prepare(sc, ifp); /* Prepare next one */
445 1.1 ahoka } else {
446 1.1 ahoka /* We need to wait until the current packet has
447 1.1 ahoka * been transmitted.
448 1.1 ahoka */
449 1.1 ahoka ifp->if_flags |= IFF_OACTIVE;
450 1.1 ahoka }
451 1.1 ahoka }
452 1.1 ahoka
453 1.1 ahoka void
454 1.1 ahoka dme_prepare(struct dme_softc *sc, struct ifnet *ifp)
455 1.1 ahoka {
456 1.1 ahoka struct mbuf *buf;
457 1.1 ahoka struct mbuf *bufChain;
458 1.1 ahoka uint16_t length;
459 1.1 ahoka uint8_t *write_ptr;
460 1.1 ahoka
461 1.1 ahoka TX_DPRINTF(("dme_prepare: Entering\n"));
462 1.1 ahoka
463 1.1 ahoka if (sc->txready)
464 1.1 ahoka panic("dme_prepare: Someone called us with txready set\n");
465 1.1 ahoka
466 1.1 ahoka IFQ_DEQUEUE(&ifp->if_snd, bufChain);
467 1.1 ahoka if (bufChain == NULL) {
468 1.1 ahoka TX_DPRINTF(("dme_prepare: Nothing to transmit\n"));
469 1.1 ahoka ifp->if_flags &= ~IFF_OACTIVE; /* Clear OACTIVE bit */
470 1.1 ahoka return; /* Nothing to transmit */
471 1.1 ahoka }
472 1.1 ahoka
473 1.1 ahoka /* Element has now been removed from the queue, so we better send it */
474 1.1 ahoka
475 1.1 ahoka if (ifp->if_bpf)
476 1.1 ahoka bpf_mtap(ifp, bufChain);
477 1.1 ahoka
478 1.1 ahoka
479 1.1 ahoka length = 0;
480 1.1 ahoka
481 1.1 ahoka /* XXX: This support 16-bit I/O mode only. */
482 1.1 ahoka /* XXX: This code must be factored out, such that architecture
483 1.1 ahoka dependant versions can be supplied */
484 1.1 ahoka
485 1.1 ahoka int left_over_count = 0; /* Number of bytes from previous mbuf, which
486 1.1 ahoka need to be written with the next.*/
487 1.1 ahoka uint16_t left_over_buf = 0;
488 1.1 ahoka
489 1.1 ahoka /* Setup the DM9000 to accept the writes, and then write each buf in
490 1.1 ahoka the chain. */
491 1.1 ahoka
492 1.1 ahoka TX_DATA_DPRINTF(("dme_prepare: Writing data: "));
493 1.1 ahoka bus_space_write_1(sc->sc_iot, sc->sc_ioh, sc->dme_io, DM9000_MWCMD);
494 1.1 ahoka for (buf = bufChain; buf != NULL; buf = buf->m_next) {
495 1.1 ahoka int to_write = buf->m_len;
496 1.1 ahoka
497 1.1 ahoka length += to_write;
498 1.1 ahoka
499 1.1 ahoka write_ptr = buf->m_data;
500 1.1 ahoka while (to_write > 0 ||
501 1.1 ahoka (buf->m_next == NULL && left_over_count > 0)
502 1.1 ahoka ) {
503 1.1 ahoka if (left_over_count > 0) {
504 1.1 ahoka uint8_t b = 0;
505 1.1 ahoka DPRINTF(("dme_prepare: "
506 1.1 ahoka "Writing left over byte\n"));
507 1.1 ahoka
508 1.1 ahoka if (to_write > 0) {
509 1.1 ahoka b = *write_ptr;
510 1.1 ahoka to_write--;
511 1.1 ahoka write_ptr++;
512 1.1 ahoka
513 1.1 ahoka DPRINTF(("Took single byte\n"));
514 1.1 ahoka } else {
515 1.1 ahoka DPRINTF(("Leftover in last run\n"));
516 1.1 ahoka length++;
517 1.1 ahoka }
518 1.1 ahoka
519 1.1 ahoka /* Does shift direction depend on endianess? */
520 1.1 ahoka left_over_buf = left_over_buf | (b << 8);
521 1.1 ahoka
522 1.1 ahoka bus_space_write_2(sc->sc_iot, sc->sc_ioh,
523 1.1 ahoka sc->dme_data, left_over_buf);
524 1.1 ahoka TX_DATA_DPRINTF(("%02X ", left_over_buf));
525 1.1 ahoka left_over_count = 0;
526 1.1 ahoka } else if ((long)write_ptr % 2 != 0) {
527 1.1 ahoka /* Misaligned data */
528 1.1 ahoka DPRINTF(("dme_prepare: "
529 1.1 ahoka "Detected misaligned data\n"));
530 1.1 ahoka left_over_buf = *write_ptr;
531 1.1 ahoka left_over_count = 1;
532 1.1 ahoka write_ptr++;
533 1.1 ahoka to_write--;
534 1.1 ahoka } else {
535 1.1 ahoka int i;
536 1.1 ahoka uint16_t *dptr = (uint16_t*)write_ptr;
537 1.1 ahoka
538 1.1 ahoka /* A block of aligned data. */
539 1.1 ahoka for(i = 0; i < to_write/2; i++) {
540 1.1 ahoka /* buf will be half-word aligned
541 1.1 ahoka * all the time
542 1.1 ahoka */
543 1.1 ahoka bus_space_write_2(sc->sc_iot,
544 1.1 ahoka sc->sc_ioh, sc->dme_data, *dptr);
545 1.1 ahoka TX_DATA_DPRINTF(("%02X %02X ",
546 1.1 ahoka *dptr & 0xFF, (*dptr>>8) & 0xFF));
547 1.1 ahoka dptr++;
548 1.1 ahoka }
549 1.1 ahoka
550 1.1 ahoka write_ptr += i*2;
551 1.1 ahoka if (to_write % 2 != 0) {
552 1.1 ahoka DPRINTF(("dme_prepare: "
553 1.1 ahoka "to_write %% 2: %d\n",
554 1.1 ahoka to_write % 2));
555 1.1 ahoka left_over_count = 1;
556 1.1 ahoka /* XXX: Does this depend on
557 1.1 ahoka * the endianess?
558 1.1 ahoka */
559 1.1 ahoka left_over_buf = *write_ptr;
560 1.1 ahoka
561 1.1 ahoka write_ptr++;
562 1.1 ahoka to_write--;
563 1.1 ahoka DPRINTF(("dme_prepare: "
564 1.1 ahoka "to_write (after): %d\n",
565 1.1 ahoka to_write));
566 1.1 ahoka DPRINTF(("dme_prepare: i*2: %d\n",
567 1.1 ahoka i*2));
568 1.1 ahoka }
569 1.1 ahoka to_write -= i*2;
570 1.1 ahoka }
571 1.1 ahoka } /* while(...) */
572 1.1 ahoka } /* for(...) */
573 1.1 ahoka
574 1.1 ahoka TX_DATA_DPRINTF(("\n"));
575 1.1 ahoka
576 1.1 ahoka if (length % 2 == 1) {
577 1.1 ahoka panic("dme_prepare: length is not a word-length");
578 1.1 ahoka }
579 1.1 ahoka
580 1.1 ahoka sc->txready_length = length;
581 1.1 ahoka sc->txready = 1;
582 1.1 ahoka
583 1.1 ahoka TX_DPRINTF(("dme_prepare: txbusy: %d\ndme_prepare: "
584 1.1 ahoka "txready: %d, txready_length: %d\n",
585 1.1 ahoka sc->txbusy, sc->txready, sc->txready_length));
586 1.1 ahoka
587 1.1 ahoka m_freem(bufChain);
588 1.1 ahoka
589 1.1 ahoka TX_DPRINTF(("dme_prepare: Leaving\n"));
590 1.1 ahoka }
591 1.1 ahoka
592 1.1 ahoka int
593 1.1 ahoka dme_init(struct ifnet *ifp)
594 1.1 ahoka {
595 1.1 ahoka int s;
596 1.1 ahoka struct dme_softc *sc = ifp->if_softc;
597 1.1 ahoka
598 1.1 ahoka dme_stop(ifp, 0);
599 1.1 ahoka
600 1.1 ahoka s = splnet();
601 1.1 ahoka
602 1.1 ahoka dme_reset(sc);
603 1.1 ahoka
604 1.1 ahoka sc->sc_ethercom.ec_if.if_flags |= IFF_RUNNING;
605 1.1 ahoka sc->sc_ethercom.ec_if.if_flags &= ~IFF_OACTIVE;
606 1.1 ahoka sc->sc_ethercom.ec_if.if_timer = 0;
607 1.1 ahoka
608 1.1 ahoka splx(s);
609 1.1 ahoka
610 1.1 ahoka return 0;
611 1.1 ahoka }
612 1.1 ahoka
613 1.1 ahoka int
614 1.1 ahoka dme_ioctl(struct ifnet *ifp, u_long cmd, void *data)
615 1.1 ahoka {
616 1.1 ahoka struct dme_softc *sc = ifp->if_softc;
617 1.1 ahoka struct ifreq *ifr = data;
618 1.1 ahoka int s, error = 0;
619 1.1 ahoka
620 1.1 ahoka s = splnet();
621 1.1 ahoka
622 1.1 ahoka switch(cmd) {
623 1.1 ahoka case SIOCGIFMEDIA:
624 1.1 ahoka case SIOCSIFMEDIA:
625 1.1 ahoka error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
626 1.1 ahoka break;
627 1.1 ahoka default:
628 1.1 ahoka error = ether_ioctl(ifp, cmd, data);
629 1.1 ahoka break;
630 1.1 ahoka }
631 1.1 ahoka
632 1.1 ahoka splx(s);
633 1.1 ahoka return error;
634 1.1 ahoka }
635 1.1 ahoka
636 1.1 ahoka void
637 1.1 ahoka dme_stop(struct ifnet *ifp, int disable)
638 1.1 ahoka {
639 1.1 ahoka struct dme_softc *sc = ifp->if_softc;
640 1.1 ahoka
641 1.1 ahoka /* Not quite sure what to do when called with disable == 0 */
642 1.1 ahoka if (disable) {
643 1.1 ahoka /* Disable RX */
644 1.1 ahoka dme_write(sc, DM9000_RCR, 0x0);
645 1.1 ahoka }
646 1.1 ahoka
647 1.1 ahoka ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
648 1.1 ahoka ifp->if_timer = 0;
649 1.1 ahoka }
650 1.1 ahoka
651 1.1 ahoka int
652 1.1 ahoka dme_mediachange(struct ifnet *ifp)
653 1.1 ahoka {
654 1.1 ahoka /* TODO: Make this function do something useful. */
655 1.1 ahoka return 0;
656 1.1 ahoka }
657 1.1 ahoka
658 1.1 ahoka void
659 1.1 ahoka dme_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
660 1.1 ahoka {
661 1.1 ahoka /* TODO: Make this function do something useful. */
662 1.1 ahoka struct dme_softc *sc = ifp->if_softc;
663 1.1 ahoka ifmr->ifm_active = sc->sc_media.ifm_cur->ifm_media;
664 1.1 ahoka
665 1.1 ahoka if (ifp->if_flags & IFF_UP) {
666 1.1 ahoka ifmr->ifm_status = IFM_AVALID | IFM_ACTIVE;
667 1.1 ahoka } else {
668 1.1 ahoka ifmr->ifm_status = 0;
669 1.1 ahoka }
670 1.1 ahoka }
671 1.1 ahoka
672 1.1 ahoka void
673 1.1 ahoka dme_transmit(struct dme_softc *sc)
674 1.1 ahoka {
675 1.1 ahoka uint8_t status;
676 1.1 ahoka
677 1.1 ahoka TX_DPRINTF(("dme_transmit: PRE: txready: %d, txbusy: %d\n",
678 1.1 ahoka sc->txready, sc->txbusy));
679 1.1 ahoka
680 1.1 ahoka dme_write(sc, DM9000_TXPLL, sc->txready_length & 0xff);
681 1.1 ahoka dme_write(sc, DM9000_TXPLH, (sc->txready_length >> 8) & 0xff );
682 1.1 ahoka
683 1.1 ahoka /* Request to send the packet */
684 1.1 ahoka status = dme_read(sc, DM9000_ISR);
685 1.1 ahoka
686 1.1 ahoka dme_write(sc, DM9000_TCR, DM9000_TCR_TXREQ);
687 1.1 ahoka
688 1.1 ahoka sc->txready = 0;
689 1.1 ahoka sc->txbusy = 1;
690 1.1 ahoka sc->txready_length = 0;
691 1.1 ahoka }
692 1.1 ahoka
693 1.1 ahoka void
694 1.1 ahoka dme_receive(struct dme_softc *sc, struct ifnet *ifp)
695 1.1 ahoka {
696 1.1 ahoka uint8_t ready = 0x01;
697 1.1 ahoka
698 1.1 ahoka DPRINTF(("inside dme_receive\n"));
699 1.1 ahoka
700 1.1 ahoka while (ready == 0x01) {
701 1.1 ahoka /* Packet received, retrieve it */
702 1.1 ahoka
703 1.1 ahoka /* Read without address increment to get the ready byte without moving past it. */
704 1.1 ahoka bus_space_write_1(sc->sc_iot, sc->sc_ioh,
705 1.1 ahoka sc->dme_io, DM9000_MRCMDX);
706 1.1 ahoka /* Dummy ready */
707 1.1 ahoka ready = bus_space_read_1(sc->sc_iot, sc->sc_ioh, sc->dme_data);
708 1.1 ahoka ready = bus_space_read_1(sc->sc_iot, sc->sc_ioh, sc->dme_data);
709 1.1 ahoka ready &= 0x03; /* we only want bits 1:0 */
710 1.1 ahoka if (ready == 0x01) {
711 1.1 ahoka uint8_t rx_status;
712 1.1 ahoka
713 1.1 ahoka uint16_t data;
714 1.1 ahoka uint16_t frame_length;
715 1.1 ahoka uint16_t i;
716 1.1 ahoka struct mbuf *m;
717 1.1 ahoka uint16_t *buf;
718 1.1 ahoka int pad;
719 1.1 ahoka
720 1.1 ahoka /* TODO: Add support for 8-bit and
721 1.1 ahoka * 32-bit transfer modes.
722 1.1 ahoka */
723 1.1 ahoka
724 1.1 ahoka /* Read with address increment. */
725 1.1 ahoka bus_space_write_1(sc->sc_iot, sc->sc_ioh,
726 1.1 ahoka sc->dme_io, DM9000_MRCMD);
727 1.1 ahoka data = bus_space_read_2(sc->sc_iot, sc->sc_ioh,
728 1.1 ahoka sc->dme_data);
729 1.1 ahoka
730 1.1 ahoka rx_status = data & 0xFF;
731 1.1 ahoka frame_length = bus_space_read_2(sc->sc_iot,
732 1.1 ahoka sc->sc_ioh, sc->dme_data);
733 1.1 ahoka
734 1.1 ahoka RX_DPRINTF(("dme_receive: "
735 1.1 ahoka "rx_statux: 0x%x, frame_length: %d\n",
736 1.1 ahoka rx_status, frame_length));
737 1.1 ahoka
738 1.1 ahoka
739 1.1 ahoka MGETHDR(m, M_DONTWAIT, MT_DATA);
740 1.1 ahoka m->m_pkthdr.rcvif = ifp;
741 1.1 ahoka /* Ensure that we always allocate an even number of
742 1.1 ahoka * bytes in order to avoid writing beyond the buffer
743 1.1 ahoka */
744 1.1 ahoka m->m_pkthdr.len = frame_length + (frame_length % 2);
745 1.1 ahoka pad = ALIGN(sizeof(struct ether_header)) -
746 1.1 ahoka sizeof(struct ether_header);
747 1.1 ahoka /* All our frames have the CRC attached */
748 1.1 ahoka m->m_flags |= M_HASFCS;
749 1.1 ahoka if (m->m_pkthdr.len + pad > MHLEN )
750 1.1 ahoka MCLGET(m, M_DONTWAIT);
751 1.1 ahoka
752 1.1 ahoka m->m_data += pad;
753 1.1 ahoka m->m_len = frame_length + (frame_length % 2);
754 1.1 ahoka buf = mtod(m, uint16_t*);
755 1.1 ahoka
756 1.1 ahoka RX_DPRINTF(("dme_receive: "));
757 1.1 ahoka
758 1.1 ahoka for(i=0; i< frame_length; i+=2 ) {
759 1.1 ahoka data = bus_space_read_2(sc->sc_iot,
760 1.1 ahoka sc->sc_ioh, sc->dme_data);
761 1.1 ahoka if ( (frame_length % 2 != 0) &&
762 1.1 ahoka (i == frame_length-1) ) {
763 1.1 ahoka data = data & 0xff;
764 1.1 ahoka RX_DPRINTF((" L "));
765 1.1 ahoka }
766 1.1 ahoka *buf = data;
767 1.1 ahoka buf++;
768 1.1 ahoka RX_DATA_DPRINTF(("%02X %02X ", data & 0xff,
769 1.1 ahoka (data>>8) & 0xff));
770 1.1 ahoka }
771 1.1 ahoka
772 1.1 ahoka RX_DATA_DPRINTF(("\n"));
773 1.1 ahoka RX_DPRINTF(("Read %d bytes\n", i));
774 1.1 ahoka
775 1.1 ahoka if (rx_status & (DM9000_RSR_CE | DM9000_RSR_PLE)) {
776 1.1 ahoka /* Error while receiving the packet,
777 1.1 ahoka * discard it and keep track of counters
778 1.1 ahoka */
779 1.1 ahoka ifp->if_ierrors++;
780 1.1 ahoka RX_DPRINTF(("dme_receive: "
781 1.1 ahoka "Error reciving packet\n"));
782 1.1 ahoka } else if (rx_status & DM9000_RSR_LCS) {
783 1.1 ahoka ifp->if_collisions++;
784 1.1 ahoka } else {
785 1.1 ahoka if (ifp->if_bpf)
786 1.1 ahoka bpf_mtap(ifp, m);
787 1.1 ahoka ifp->if_ipackets++;
788 1.1 ahoka (*ifp->if_input)(ifp, m);
789 1.1 ahoka }
790 1.1 ahoka
791 1.1 ahoka } else if (ready != 0x00) {
792 1.1 ahoka /* Should this be logged somehow? */
793 1.1 ahoka DPRINTF(("DM9000: Resetting chip\n"));
794 1.1 ahoka dme_reset(sc);
795 1.1 ahoka }
796 1.1 ahoka }
797 1.1 ahoka }
798 1.1 ahoka
799 1.1 ahoka void
800 1.1 ahoka dme_reset(struct dme_softc *sc)
801 1.1 ahoka {
802 1.1 ahoka uint8_t var;
803 1.1 ahoka
804 1.1 ahoka /* Enable PHY */
805 1.1 ahoka var = dme_read(sc, DM9000_GPCR);
806 1.1 ahoka dme_write(sc, DM9000_GPCR, var | DM9000_GPCR_GPIO0_OUT);
807 1.1 ahoka var = dme_read(sc, DM9000_GPR);
808 1.1 ahoka dme_write(sc, DM9000_GPR, var & ~DM9000_GPR_PHY_PWROFF);
809 1.1 ahoka
810 1.1 ahoka /* Reset the DM9000 twice, as describe din section 5.2 of the
811 1.1 ahoka * Application Notes
812 1.1 ahoka */
813 1.1 ahoka dme_write(sc, DM9000_NCR,
814 1.1 ahoka DM9000_NCR_RST | DM9000_NCR_LBK_MAC_INTERNAL);
815 1.1 ahoka
816 1.1 ahoka delay(20);
817 1.1 ahoka dme_write(sc, DM9000_NCR, 0x0);
818 1.1 ahoka dme_write(sc, DM9000_NCR,
819 1.1 ahoka DM9000_NCR_RST | DM9000_NCR_LBK_MAC_INTERNAL);
820 1.1 ahoka
821 1.1 ahoka delay(20);
822 1.1 ahoka dme_write(sc, DM9000_NCR, 0x0);
823 1.1 ahoka
824 1.1 ahoka /* Select internal PHY, no wakeup event, no collosion mode,
825 1.1 ahoka * normal loopback mode, and no full duplex mode
826 1.1 ahoka */
827 1.1 ahoka dme_write(sc, DM9000_NCR, DM9000_NCR_LBK_NORMAL );
828 1.1 ahoka
829 1.1 ahoka /* Will clear TX1END, TX2END, and WAKEST fields by reading DM9000_NSR*/
830 1.1 ahoka dme_read(sc, DM9000_NSR);
831 1.1 ahoka
832 1.1 ahoka /* Enable wraparound of read/write pointer, packet received latch,
833 1.1 ahoka * and packet transmitted latch.
834 1.1 ahoka */
835 1.1 ahoka dme_write(sc, DM9000_IMR,
836 1.1 ahoka DM9000_IMR_PAR | DM9000_IMR_PRM | DM9000_IMR_PTM);
837 1.1 ahoka
838 1.1 ahoka /* Enable RX without watchdog */
839 1.1 ahoka dme_write(sc, DM9000_RCR, DM9000_RCR_RXEN | DM9000_RCR_WTDIS);
840 1.1 ahoka
841 1.1 ahoka sc->txbusy = 0;
842 1.1 ahoka sc->txready = 0;
843 1.1 ahoka }
844