if_tlp_ap.c revision 1.2.2.2 1 1.2.2.2 nathanw /* $NetBSD: if_tlp_ap.c,v 1.2.2.2 2002/10/18 02:39:10 nathanw Exp $ */
2 1.2.2.2 nathanw
3 1.2.2.2 nathanw /*
4 1.2.2.2 nathanw * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 1.2.2.2 nathanw * All rights reserved.
6 1.2.2.2 nathanw *
7 1.2.2.2 nathanw * This code is derived from software contributed to The NetBSD Foundation
8 1.2.2.2 nathanw * by Atsushi Onoe.
9 1.2.2.2 nathanw *
10 1.2.2.2 nathanw * Redistribution and use in source and binary forms, with or without
11 1.2.2.2 nathanw * modification, are permitted provided that the following conditions
12 1.2.2.2 nathanw * are met:
13 1.2.2.2 nathanw * 1. Redistributions of source code must retain the above copyright
14 1.2.2.2 nathanw * notice, this list of conditions and the following disclaimer.
15 1.2.2.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
16 1.2.2.2 nathanw * notice, this list of conditions and the following disclaimer in the
17 1.2.2.2 nathanw * documentation and/or other materials provided with the distribution.
18 1.2.2.2 nathanw * 3. All advertising materials mentioning features or use of this software
19 1.2.2.2 nathanw * must display the following acknowledgement:
20 1.2.2.2 nathanw * This product includes software developed by the NetBSD
21 1.2.2.2 nathanw * Foundation, Inc. and its contributors.
22 1.2.2.2 nathanw * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.2.2.2 nathanw * contributors may be used to endorse or promote products derived
24 1.2.2.2 nathanw * from this software without specific prior written permission.
25 1.2.2.2 nathanw *
26 1.2.2.2 nathanw * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.2.2.2 nathanw * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.2.2.2 nathanw * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.2.2.2 nathanw * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.2.2.2 nathanw * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.2.2.2 nathanw * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.2.2.2 nathanw * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.2.2.2 nathanw * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.2.2.2 nathanw * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.2.2.2 nathanw * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.2.2.2 nathanw * POSSIBILITY OF SUCH DAMAGE.
37 1.2.2.2 nathanw */
38 1.2.2.2 nathanw
39 1.2.2.2 nathanw #include <sys/param.h>
40 1.2.2.2 nathanw #include <sys/device.h>
41 1.2.2.2 nathanw #include <sys/errno.h>
42 1.2.2.2 nathanw #include <sys/ioctl.h>
43 1.2.2.2 nathanw #include <sys/malloc.h>
44 1.2.2.2 nathanw #include <sys/socket.h>
45 1.2.2.2 nathanw #include <sys/syslog.h>
46 1.2.2.2 nathanw #include <sys/systm.h>
47 1.2.2.2 nathanw
48 1.2.2.2 nathanw #include <net/if.h>
49 1.2.2.2 nathanw #include <net/if_dl.h>
50 1.2.2.2 nathanw #include <net/if_media.h>
51 1.2.2.2 nathanw #include <net/if_ether.h>
52 1.2.2.2 nathanw
53 1.2.2.2 nathanw #include <machine/bus.h>
54 1.2.2.2 nathanw #include <machine/intr.h>
55 1.2.2.2 nathanw
56 1.2.2.2 nathanw #include <mips/cache.h>
57 1.2.2.2 nathanw
58 1.2.2.2 nathanw #include <dev/mii/miivar.h>
59 1.2.2.2 nathanw #include <dev/mii/mii_bitbang.h>
60 1.2.2.2 nathanw
61 1.2.2.2 nathanw #include <dev/ic/tulipreg.h>
62 1.2.2.2 nathanw #include <dev/ic/tulipvar.h>
63 1.2.2.2 nathanw
64 1.2.2.2 nathanw #include <newsmips/apbus/apbusvar.h>
65 1.2.2.2 nathanw
66 1.2.2.2 nathanw #define TLP_AP_ROM 0x00000000 /* AProm entry address */
67 1.2.2.2 nathanw #define TLP_AP_CFG 0x00040000 /* PCI configuration registers */
68 1.2.2.2 nathanw #define TLP_AP_CFG_CFID 0x00 /* Identification */
69 1.2.2.2 nathanw #define TLP_AP_CFG_CFCS 0x04 /* Command and status */
70 1.2.2.2 nathanw #define TLP_AP_CFG_CFRV 0x08 /* Revision */
71 1.2.2.2 nathanw #define TLP_AP_CFG_CFLT 0x0c /* Latency timer */
72 1.2.2.2 nathanw #define TLP_AP_CFG_CBIO 0x10 /* Base I/O address */
73 1.2.2.2 nathanw #define TLP_AP_CFG_CBMA 0x14 /* Base memory address */
74 1.2.2.2 nathanw #define TLP_AP_CFG_CFIT 0x3c /* Interrupt */
75 1.2.2.2 nathanw #define TLP_AP_CSR 0x00080000 /* CSR base address */
76 1.2.2.2 nathanw #define TLP_AP_RST 0x00100000 /* Board Reset */
77 1.2.2.2 nathanw
78 1.2.2.2 nathanw
79 1.2.2.2 nathanw extern void tlp_idle __P((struct tulip_softc *, u_int32_t));
80 1.2.2.2 nathanw
81 1.2.2.2 nathanw struct tulip_ap_softc {
82 1.2.2.2 nathanw struct tulip_softc sc_tulip; /* real Tulip softc */
83 1.2.2.2 nathanw bus_space_tag_t sc_cfst;
84 1.2.2.2 nathanw bus_space_handle_t sc_cfsh;
85 1.2.2.2 nathanw };
86 1.2.2.2 nathanw
87 1.2.2.2 nathanw static int tlp_ap_match __P((struct device *, struct cfdata *, void *));
88 1.2.2.2 nathanw static void tlp_ap_attach __P((struct device *, struct device *, void *));
89 1.2.2.2 nathanw
90 1.2.2.2 nathanw CFATTACH_DECL(tlp_ap, sizeof(struct tulip_ap_softc),
91 1.2.2.2 nathanw tlp_ap_match, tlp_ap_attach, NULL, NULL);
92 1.2.2.2 nathanw
93 1.2.2.2 nathanw static void tlp_ap_preinit __P((struct tulip_softc *));
94 1.2.2.2 nathanw static void tlp_ap_tmsw_init __P((struct tulip_softc *));
95 1.2.2.2 nathanw static void tlp_ap_getmedia __P((struct tulip_softc *, struct ifmediareq *));
96 1.2.2.2 nathanw static int tlp_ap_setmedia __P((struct tulip_softc *));
97 1.2.2.2 nathanw
98 1.2.2.2 nathanw const struct tulip_mediasw tlp_ap_mediasw = {
99 1.2.2.2 nathanw tlp_ap_tmsw_init, tlp_ap_getmedia, tlp_ap_setmedia
100 1.2.2.2 nathanw };
101 1.2.2.2 nathanw
102 1.2.2.2 nathanw static int
103 1.2.2.2 nathanw tlp_ap_match(parent, cf, aux)
104 1.2.2.2 nathanw struct device *parent;
105 1.2.2.2 nathanw struct cfdata *cf;
106 1.2.2.2 nathanw void *aux;
107 1.2.2.2 nathanw {
108 1.2.2.2 nathanw struct apbus_attach_args *apa = aux;
109 1.2.2.2 nathanw
110 1.2.2.2 nathanw if (strcmp(apa->apa_name, "cbasetx") != 0)
111 1.2.2.2 nathanw return 0;
112 1.2.2.2 nathanw
113 1.2.2.2 nathanw return 1;
114 1.2.2.2 nathanw }
115 1.2.2.2 nathanw
116 1.2.2.2 nathanw /*
117 1.2.2.2 nathanw * Install interface into kernel networking data structures
118 1.2.2.2 nathanw */
119 1.2.2.2 nathanw static void
120 1.2.2.2 nathanw tlp_ap_attach(parent, self, aux)
121 1.2.2.2 nathanw struct device *parent, *self;
122 1.2.2.2 nathanw void *aux;
123 1.2.2.2 nathanw {
124 1.2.2.2 nathanw struct tulip_ap_softc *psc = (void *) self;
125 1.2.2.2 nathanw struct tulip_softc *sc = &psc->sc_tulip;
126 1.2.2.2 nathanw struct apbus_attach_args *apa = aux;
127 1.2.2.2 nathanw u_int8_t enaddr[ETHER_ADDR_LEN];
128 1.2.2.2 nathanw u_int intrmask;
129 1.2.2.2 nathanw int i;
130 1.2.2.2 nathanw
131 1.2.2.2 nathanw printf(" slot%d addr 0x%lx", apa->apa_slotno, apa->apa_hwbase);
132 1.2.2.2 nathanw
133 1.2.2.2 nathanw /* PCI configuration register */
134 1.2.2.2 nathanw psc->sc_cfst = 0;
135 1.2.2.2 nathanw psc->sc_cfsh = apa->apa_hwbase + TLP_AP_CFG;
136 1.2.2.2 nathanw sc->sc_devno = apa->apa_slotno;
137 1.2.2.2 nathanw sc->sc_rev = bus_space_read_4(psc->sc_cfst, psc->sc_cfsh,
138 1.2.2.2 nathanw TLP_AP_CFG_CFRV);
139 1.2.2.2 nathanw switch (bus_space_read_4(psc->sc_cfst, psc->sc_cfsh, TLP_AP_CFG_CFID)) {
140 1.2.2.2 nathanw case 0x00091011:
141 1.2.2.2 nathanw if (sc->sc_rev >= 0x20)
142 1.2.2.2 nathanw sc->sc_chip = TULIP_CHIP_21140A;
143 1.2.2.2 nathanw else
144 1.2.2.2 nathanw sc->sc_chip = TULIP_CHIP_21140;
145 1.2.2.2 nathanw break;
146 1.2.2.2 nathanw default:
147 1.2.2.2 nathanw printf(": unable to handle your board\n");
148 1.2.2.2 nathanw return;
149 1.2.2.2 nathanw }
150 1.2.2.2 nathanw
151 1.2.2.2 nathanw printf(": %s Ethernet, pass %d.%d\n",
152 1.2.2.2 nathanw tlp_chip_names[sc->sc_chip],
153 1.2.2.2 nathanw (sc->sc_rev >> 4) & 0xf, sc->sc_rev & 0xf);
154 1.2.2.2 nathanw
155 1.2.2.2 nathanw /* CSR */
156 1.2.2.2 nathanw sc->sc_st = 0;
157 1.2.2.2 nathanw sc->sc_sh = apa->apa_hwbase + TLP_AP_CSR;
158 1.2.2.2 nathanw sc->sc_dmat = apbus_dmatag_init(apa);
159 1.2.2.2 nathanw if (sc->sc_dmat == NULL) {
160 1.2.2.2 nathanw printf("%s: cannot allocate memory\n", sc->sc_dev.dv_xname);
161 1.2.2.2 nathanw return;
162 1.2.2.2 nathanw }
163 1.2.2.2 nathanw
164 1.2.2.2 nathanw /*
165 1.2.2.2 nathanw * Initialize bus specific parameters.
166 1.2.2.2 nathanw */
167 1.2.2.2 nathanw if (mips_sdcache_line_size > 0)
168 1.2.2.2 nathanw sc->sc_cacheline = mips_sdcache_line_size / 4;
169 1.2.2.2 nathanw else if (mips_pdcache_line_size > 0)
170 1.2.2.2 nathanw sc->sc_cacheline = mips_pdcache_line_size / 4;
171 1.2.2.2 nathanw else
172 1.2.2.2 nathanw sc->sc_cacheline = 4;
173 1.2.2.2 nathanw sc->sc_maxburst = sc->sc_cacheline; /* XXX */
174 1.2.2.2 nathanw sc->sc_regshift = 3;
175 1.2.2.2 nathanw sc->sc_flags |= TULIPF_DBO | TULIPF_BLE; /* Big Endian BUS */
176 1.2.2.2 nathanw sc->sc_flags |= TULIPF_ENABLED; /* No Power Mgmt */
177 1.2.2.2 nathanw
178 1.2.2.2 nathanw /*
179 1.2.2.2 nathanw * Reset hardware.
180 1.2.2.2 nathanw */
181 1.2.2.2 nathanw bus_space_write_4(0, apa->apa_hwbase + TLP_AP_RST, 0, 1);
182 1.2.2.2 nathanw delay(100);
183 1.2.2.2 nathanw
184 1.2.2.2 nathanw /*
185 1.2.2.2 nathanw * Initialize PCI configuration register
186 1.2.2.2 nathanw */
187 1.2.2.2 nathanw bus_space_write_4(psc->sc_cfst, psc->sc_cfsh,
188 1.2.2.2 nathanw TLP_AP_CFG_CFCS, 0x00000005); /* Master, IO */
189 1.2.2.2 nathanw bus_space_write_4(psc->sc_cfst, psc->sc_cfsh,
190 1.2.2.2 nathanw TLP_AP_CFG_CFLT, 0x00000100);
191 1.2.2.2 nathanw bus_space_write_4(psc->sc_cfst, psc->sc_cfsh,
192 1.2.2.2 nathanw TLP_AP_CFG_CBIO, MIPS_KSEG1_TO_PHYS(sc->sc_sh));
193 1.2.2.2 nathanw bus_space_write_4(psc->sc_cfst, psc->sc_cfsh,
194 1.2.2.2 nathanw TLP_AP_CFG_CFIT, 0x00000101);
195 1.2.2.2 nathanw
196 1.2.2.2 nathanw /*
197 1.2.2.2 nathanw * Initialize general purpose port register.
198 1.2.2.2 nathanw */
199 1.2.2.2 nathanw TULIP_WRITE(sc, CSR_GPP, GPP_GPC | 0xc0); /* read */
200 1.2.2.2 nathanw TULIP_WRITE(sc, CSR_GPP, 0x40); /* dipsw port on */
201 1.2.2.2 nathanw i = TULIP_READ(sc, CSR_GPP) & GPP_MD; /* dipsw contents */
202 1.2.2.2 nathanw TULIP_WRITE(sc, CSR_GPP, 0xc0); /* dipsw port off */
203 1.2.2.2 nathanw TULIP_WRITE(sc, CSR_GPP, GPP_GPC | 0xcf); /* read write */
204 1.2.2.2 nathanw if (sc->sc_maxburst == 16) {
205 1.2.2.2 nathanw TULIP_WRITE(sc, CSR_GPP, 0x8f); /* 16word burst */
206 1.2.2.2 nathanw TULIP_WRITE(sc, CSR_GPP, 0xcf);
207 1.2.2.2 nathanw } else {
208 1.2.2.2 nathanw TULIP_WRITE(sc, CSR_GPP, 0x8e); /* 8word burst */
209 1.2.2.2 nathanw TULIP_WRITE(sc, CSR_GPP, 0xce);
210 1.2.2.2 nathanw }
211 1.2.2.2 nathanw TULIP_WRITE(sc, CSR_GPP, GPP_GPC | 0xcf); /* read write */
212 1.2.2.2 nathanw TULIP_WRITE(sc, CSR_GPP, 0xc3); /* mask abort/dma err */
213 1.2.2.2 nathanw TULIP_WRITE(sc, CSR_GPP, 0xcf); /* mask abort/dma err */
214 1.2.2.2 nathanw
215 1.2.2.2 nathanw if (tlp_read_srom(sc) == 0) {
216 1.2.2.2 nathanw printf("%s: srom read failed\n", sc->sc_dev.dv_xname);
217 1.2.2.2 nathanw free(sc->sc_dmat, M_DEVBUF);
218 1.2.2.2 nathanw return;
219 1.2.2.2 nathanw }
220 1.2.2.2 nathanw for (i = 0; i < ETHER_ADDR_LEN; i++)
221 1.2.2.2 nathanw enaddr[i] = sc->sc_srom[0x11 + i * 2];
222 1.2.2.2 nathanw
223 1.2.2.2 nathanw sc->sc_mediasw = &tlp_ap_mediasw;
224 1.2.2.2 nathanw
225 1.2.2.2 nathanw /*
226 1.2.2.2 nathanw * Finish off the attach.
227 1.2.2.2 nathanw */
228 1.2.2.2 nathanw tlp_attach(sc, enaddr);
229 1.2.2.2 nathanw
230 1.2.2.2 nathanw intrmask = SLOTTOMASK(apa->apa_slotno);
231 1.2.2.2 nathanw apbus_intr_establish(0, /* interrupt level (0 or 1) */
232 1.2.2.2 nathanw intrmask,
233 1.2.2.2 nathanw 0, /* priority */
234 1.2.2.2 nathanw tlp_intr, sc, apa->apa_name, apa->apa_ctlnum);
235 1.2.2.2 nathanw }
236 1.2.2.2 nathanw
237 1.2.2.2 nathanw static void
238 1.2.2.2 nathanw tlp_ap_preinit(sc)
239 1.2.2.2 nathanw struct tulip_softc *sc;
240 1.2.2.2 nathanw {
241 1.2.2.2 nathanw
242 1.2.2.2 nathanw sc->sc_opmode |= OPMODE_MBO | OPMODE_SCR | OPMODE_PCS | OPMODE_HBD |
243 1.2.2.2 nathanw OPMODE_PS;
244 1.2.2.2 nathanw TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
245 1.2.2.2 nathanw }
246 1.2.2.2 nathanw
247 1.2.2.2 nathanw static void
248 1.2.2.2 nathanw tlp_ap_tmsw_init(sc)
249 1.2.2.2 nathanw struct tulip_softc *sc;
250 1.2.2.2 nathanw {
251 1.2.2.2 nathanw struct ifnet *ifp = &sc->sc_ethercom.ec_if;
252 1.2.2.2 nathanw
253 1.2.2.2 nathanw sc->sc_preinit = tlp_ap_preinit;
254 1.2.2.2 nathanw
255 1.2.2.2 nathanw sc->sc_mii.mii_ifp = ifp;
256 1.2.2.2 nathanw sc->sc_mii.mii_readreg = NULL;
257 1.2.2.2 nathanw sc->sc_mii.mii_writereg = NULL;
258 1.2.2.2 nathanw sc->sc_mii.mii_statchg = sc->sc_statchg;
259 1.2.2.2 nathanw ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
260 1.2.2.2 nathanw tlp_mediastatus);
261 1.2.2.2 nathanw ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_100_TX, 0, NULL);
262 1.2.2.2 nathanw ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_100_TX|IFM_FDX, 0,
263 1.2.2.2 nathanw NULL);
264 1.2.2.2 nathanw ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_100_TX);
265 1.2.2.2 nathanw }
266 1.2.2.2 nathanw
267 1.2.2.2 nathanw static void
268 1.2.2.2 nathanw tlp_ap_getmedia(sc, ifmr)
269 1.2.2.2 nathanw struct tulip_softc *sc;
270 1.2.2.2 nathanw struct ifmediareq *ifmr;
271 1.2.2.2 nathanw {
272 1.2.2.2 nathanw struct ifnet *ifp = &sc->sc_ethercom.ec_if;
273 1.2.2.2 nathanw
274 1.2.2.2 nathanw ifmr->ifm_status = IFM_AVALID;
275 1.2.2.2 nathanw if (ifp->if_flags & IFF_RUNNING)
276 1.2.2.2 nathanw ifmr->ifm_status |= IFM_ACTIVE;
277 1.2.2.2 nathanw ifmr->ifm_active = sc->sc_mii.mii_media_active;
278 1.2.2.2 nathanw }
279 1.2.2.2 nathanw
280 1.2.2.2 nathanw static int
281 1.2.2.2 nathanw tlp_ap_setmedia(sc)
282 1.2.2.2 nathanw struct tulip_softc *sc;
283 1.2.2.2 nathanw {
284 1.2.2.2 nathanw struct ifnet *ifp = &sc->sc_ethercom.ec_if;
285 1.2.2.2 nathanw
286 1.2.2.2 nathanw sc->sc_mii.mii_media_active = sc->sc_mii.mii_media.ifm_cur->ifm_media;
287 1.2.2.2 nathanw
288 1.2.2.2 nathanw if (ifp->if_flags & IFF_UP)
289 1.2.2.2 nathanw tlp_idle(sc, OPMODE_ST|OPMODE_SR);
290 1.2.2.2 nathanw if (sc->sc_mii.mii_media_active & IFM_FDX)
291 1.2.2.2 nathanw sc->sc_opmode |= OPMODE_FD;
292 1.2.2.2 nathanw else
293 1.2.2.2 nathanw sc->sc_opmode &= ~OPMODE_FD;
294 1.2.2.2 nathanw if (ifp->if_flags & IFF_UP)
295 1.2.2.2 nathanw TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
296 1.2.2.2 nathanw return (0);
297 1.2.2.2 nathanw }
298