if_le_sbdio.c revision 1.3 1 1.3 tsutsui /* $NetBSD: if_le_sbdio.c,v 1.3 2008/03/29 08:14:41 tsutsui Exp $ */
2 1.1 tsutsui
3 1.1 tsutsui /*-
4 1.1 tsutsui * Copyright (c) 1996, 2005 The NetBSD Foundation, Inc.
5 1.1 tsutsui * All rights reserved.
6 1.1 tsutsui *
7 1.1 tsutsui * This code is derived from software contributed to The NetBSD Foundation
8 1.1 tsutsui * by Adam Glass and Gordon W. Ross.
9 1.1 tsutsui *
10 1.1 tsutsui * Redistribution and use in source and binary forms, with or without
11 1.1 tsutsui * modification, are permitted provided that the following conditions
12 1.1 tsutsui * are met:
13 1.1 tsutsui * 1. Redistributions of source code must retain the above copyright
14 1.1 tsutsui * notice, this list of conditions and the following disclaimer.
15 1.1 tsutsui * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 tsutsui * notice, this list of conditions and the following disclaimer in the
17 1.1 tsutsui * documentation and/or other materials provided with the distribution.
18 1.1 tsutsui * 3. All advertising materials mentioning features or use of this software
19 1.1 tsutsui * must display the following acknowledgement:
20 1.1 tsutsui * This product includes software developed by the NetBSD
21 1.1 tsutsui * Foundation, Inc. and its contributors.
22 1.1 tsutsui * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 tsutsui * contributors may be used to endorse or promote products derived
24 1.1 tsutsui * from this software without specific prior written permission.
25 1.1 tsutsui *
26 1.1 tsutsui * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 tsutsui * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 tsutsui * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 tsutsui * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 tsutsui * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 tsutsui * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 tsutsui * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 tsutsui * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 tsutsui * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 tsutsui * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 tsutsui * POSSIBILITY OF SUCH DAMAGE.
37 1.1 tsutsui */
38 1.1 tsutsui
39 1.1 tsutsui #include <sys/cdefs.h>
40 1.3 tsutsui __KERNEL_RCSID(0, "$NetBSD: if_le_sbdio.c,v 1.3 2008/03/29 08:14:41 tsutsui Exp $");
41 1.1 tsutsui
42 1.1 tsutsui #include "opt_inet.h"
43 1.1 tsutsui #include "bpfilter.h"
44 1.1 tsutsui
45 1.1 tsutsui #include <sys/param.h>
46 1.1 tsutsui #include <sys/systm.h>
47 1.1 tsutsui #include <sys/mbuf.h>
48 1.1 tsutsui #include <sys/syslog.h>
49 1.1 tsutsui #include <sys/socket.h>
50 1.1 tsutsui #include <sys/device.h>
51 1.1 tsutsui
52 1.1 tsutsui #include <net/if.h>
53 1.1 tsutsui #include <net/if_ether.h>
54 1.1 tsutsui #include <net/if_media.h>
55 1.1 tsutsui
56 1.1 tsutsui #ifdef INET
57 1.1 tsutsui #include <netinet/in.h>
58 1.1 tsutsui #include <netinet/if_inarp.h>
59 1.1 tsutsui #endif
60 1.1 tsutsui
61 1.1 tsutsui #include <machine/bus.h>
62 1.1 tsutsui #include <machine/intr.h>
63 1.1 tsutsui #include <machine/sbdiovar.h>
64 1.1 tsutsui #include <machine/sbdvar.h> /* for ether_addr() */
65 1.1 tsutsui
66 1.1 tsutsui #include <dev/ic/lancereg.h>
67 1.1 tsutsui #include <dev/ic/lancevar.h>
68 1.1 tsutsui #include <dev/ic/am7990reg.h>
69 1.1 tsutsui #include <dev/ic/am7990var.h>
70 1.1 tsutsui
71 1.1 tsutsui #define LEREG1_RDP 0 /* offset to lance data register */
72 1.1 tsutsui #define LEREG1_RAP 6 /* offset to lance address regsiter */
73 1.1 tsutsui #define LE_MEMSIZE (64 * 1024)
74 1.1 tsutsui
75 1.1 tsutsui struct le_sbdio_softc {
76 1.1 tsutsui struct am7990_softc sc_am7990;
77 1.1 tsutsui
78 1.1 tsutsui bus_space_tag_t sc_bst;
79 1.1 tsutsui bus_space_handle_t sc_bsh;
80 1.1 tsutsui bus_dma_tag_t sc_dmat;
81 1.1 tsutsui bus_dmamap_t sc_dmamap;
82 1.1 tsutsui };
83 1.1 tsutsui
84 1.1 tsutsui int le_sbdio_match(struct device *, struct cfdata *, void *);
85 1.1 tsutsui void le_sbdio_attach(struct device *, struct device *, void *);
86 1.1 tsutsui static void le_sbdio_wrcsr(struct lance_softc *, uint16_t, uint16_t);
87 1.1 tsutsui static uint16_t le_sbdio_rdcsr(struct lance_softc *, uint16_t);
88 1.1 tsutsui
89 1.1 tsutsui CFATTACH_DECL(le_sbdio, sizeof(struct le_sbdio_softc),
90 1.1 tsutsui le_sbdio_match, le_sbdio_attach, NULL, NULL);
91 1.1 tsutsui
92 1.1 tsutsui int
93 1.1 tsutsui le_sbdio_match(struct device *parent, struct cfdata *cf, void *aux)
94 1.1 tsutsui {
95 1.1 tsutsui struct sbdio_attach_args *sa = aux;
96 1.1 tsutsui
97 1.1 tsutsui return strcmp(sa->sa_name, "lance") ? 0 : 1;
98 1.1 tsutsui }
99 1.1 tsutsui
100 1.1 tsutsui void
101 1.1 tsutsui le_sbdio_attach(struct device *parent, struct device *self, void *aux)
102 1.1 tsutsui {
103 1.1 tsutsui struct sbdio_attach_args *sa = aux;
104 1.1 tsutsui struct le_sbdio_softc *lesc = (void *)self;
105 1.1 tsutsui struct lance_softc *sc = &lesc->sc_am7990.lsc;
106 1.1 tsutsui bus_dma_segment_t seg;
107 1.1 tsutsui int rseg;
108 1.1 tsutsui
109 1.1 tsutsui lesc->sc_dmat = sa->sa_dmat;
110 1.1 tsutsui lesc->sc_bst = sa->sa_bust;
111 1.1 tsutsui
112 1.1 tsutsui if (bus_space_map(lesc->sc_bst, sa->sa_addr1, 8 /* XXX */,
113 1.1 tsutsui BUS_SPACE_MAP_LINEAR, &lesc->sc_bsh) != 0) {
114 1.3 tsutsui aprint_error(": cannot map registers\n");
115 1.1 tsutsui return;
116 1.1 tsutsui }
117 1.1 tsutsui
118 1.1 tsutsui /* Allocate DMA memory for the chip. */
119 1.1 tsutsui if (bus_dmamem_alloc(lesc->sc_dmat, LE_MEMSIZE, 0, 0, &seg, 1, &rseg,
120 1.1 tsutsui BUS_DMA_NOWAIT) != 0) {
121 1.3 tsutsui aprint_error(": can't allocate DMA memory\n");
122 1.1 tsutsui return;
123 1.1 tsutsui }
124 1.1 tsutsui if (bus_dmamem_map(lesc->sc_dmat, &seg, rseg, LE_MEMSIZE,
125 1.2 christos (void **)&sc->sc_mem, BUS_DMA_NOWAIT|BUS_DMA_COHERENT) != 0) {
126 1.3 tsutsui aprint_error(": can't map DMA memory\n");
127 1.1 tsutsui return;
128 1.1 tsutsui }
129 1.1 tsutsui if (bus_dmamap_create(lesc->sc_dmat, LE_MEMSIZE, 1, LE_MEMSIZE,
130 1.1 tsutsui 0, BUS_DMA_NOWAIT, &lesc->sc_dmamap) != 0) {
131 1.3 tsutsui aprint_error(": can't create DMA map\n");
132 1.1 tsutsui return;
133 1.1 tsutsui }
134 1.1 tsutsui if (bus_dmamap_load(lesc->sc_dmat, lesc->sc_dmamap, sc->sc_mem,
135 1.1 tsutsui LE_MEMSIZE, NULL, BUS_DMA_NOWAIT) != 0) {
136 1.3 tsutsui aprint_error(": can't load DMA map\n");
137 1.1 tsutsui return;
138 1.1 tsutsui }
139 1.1 tsutsui
140 1.1 tsutsui sc->sc_memsize = LE_MEMSIZE;
141 1.1 tsutsui sc->sc_addr = lesc->sc_dmamap->dm_segs[0].ds_addr;
142 1.1 tsutsui sc->sc_conf3 = LE_C3_BSWP | LE_C3_BCON;
143 1.1 tsutsui (*platform.ether_addr)(sc->sc_enaddr);
144 1.1 tsutsui
145 1.1 tsutsui sc->sc_copytodesc = lance_copytobuf_contig;
146 1.1 tsutsui sc->sc_copyfromdesc = lance_copyfrombuf_contig;
147 1.1 tsutsui sc->sc_copytobuf = lance_copytobuf_contig;
148 1.1 tsutsui sc->sc_copyfrombuf = lance_copyfrombuf_contig;
149 1.1 tsutsui sc->sc_zerobuf = lance_zerobuf_contig;
150 1.1 tsutsui #ifdef LEDEBUG
151 1.1 tsutsui sc->sc_debug = 0xff;
152 1.1 tsutsui #endif
153 1.1 tsutsui sc->sc_rdcsr = le_sbdio_rdcsr;
154 1.1 tsutsui sc->sc_wrcsr = le_sbdio_wrcsr;
155 1.1 tsutsui
156 1.1 tsutsui am7990_config(&lesc->sc_am7990);
157 1.1 tsutsui intr_establish(sa->sa_irq, am7990_intr, self);
158 1.1 tsutsui }
159 1.1 tsutsui
160 1.1 tsutsui static void
161 1.1 tsutsui le_sbdio_wrcsr(struct lance_softc *sc, uint16_t port, uint16_t val)
162 1.1 tsutsui {
163 1.1 tsutsui struct le_sbdio_softc *lesc = (void *)sc;
164 1.1 tsutsui
165 1.1 tsutsui bus_space_write_2(lesc->sc_bst, lesc->sc_bsh, LEREG1_RAP, port);
166 1.1 tsutsui bus_space_write_2(lesc->sc_bst, lesc->sc_bsh, LEREG1_RDP, val);
167 1.1 tsutsui }
168 1.1 tsutsui
169 1.1 tsutsui static uint16_t
170 1.1 tsutsui le_sbdio_rdcsr(struct lance_softc *sc, uint16_t port)
171 1.1 tsutsui {
172 1.1 tsutsui struct le_sbdio_softc *lesc = (void *)sc;
173 1.1 tsutsui
174 1.1 tsutsui bus_space_write_2(lesc->sc_bst, lesc->sc_bsh, LEREG1_RAP, port);
175 1.1 tsutsui return bus_space_read_2(lesc->sc_bst, lesc->sc_bsh, LEREG1_RDP);
176 1.1 tsutsui }
177