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