if_sn_obio.c revision 1.26 1 /* $NetBSD: if_sn_obio.c,v 1.26 2007/06/10 05:59:43 tsutsui Exp $ */
2
3 /*
4 * Copyright (C) 1997 Allen Briggs
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Allen Briggs
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: if_sn_obio.c,v 1.26 2007/06/10 05:59:43 tsutsui Exp $");
35
36 #include <sys/param.h>
37 #include <sys/device.h>
38 #include <sys/systm.h>
39 #include <sys/ioctl.h>
40
41 #include <net/if.h>
42 #include <net/if_ether.h>
43
44 #include <machine/bus.h>
45 #include <machine/cpu.h>
46 #include <machine/viareg.h>
47
48 #include <dev/ic/dp83932reg.h>
49 #include <dev/ic/dp83932var.h>
50
51 #include <mac68k/obio/obiovar.h>
52 #include <mac68k/dev/if_snvar.h>
53
54 #define SONIC_REG_BASE 0x50F0A000
55 #define SONIC_PROM_BASE 0x50F08000
56 #define SONIC_SLOTNO 9
57
58 static int sn_obio_match(struct device *, struct cfdata *, void *);
59 static void sn_obio_attach(struct device *, struct device *, void *);
60 static int sn_obio_getaddr(struct sonic_softc *, uint8_t *);
61 static int sn_obio_getaddr_kludge(struct sonic_softc *, uint8_t *);
62
63 CFATTACH_DECL(sn_obio, sizeof(struct sonic_softc),
64 sn_obio_match, sn_obio_attach, NULL, NULL);
65
66 static int
67 sn_obio_match(struct device *parent, struct cfdata *cf, void *aux)
68 {
69 struct obio_attach_args *oa = (struct obio_attach_args *)aux;
70 bus_space_handle_t bsh;
71 int found = 0;
72
73 if (!mac68k_machine.sonic)
74 return 0;
75
76 if (bus_space_map(oa->oa_tag,
77 SONIC_REG_BASE, SONIC_NREGS * 4, 0, &bsh))
78 return 0;
79
80 if (mac68k_bus_space_probe(oa->oa_tag, bsh, 0, 4))
81 found = 1;
82
83 bus_space_unmap(oa->oa_tag, bsh, SONIC_NREGS * 4);
84
85 return found;
86 }
87
88 /*
89 * Install interface into kernel networking data structures
90 */
91 static void
92 sn_obio_attach(struct device *parent, struct device *self, void *aux)
93 {
94 struct obio_attach_args *oa = (struct obio_attach_args *)aux;
95 struct sonic_softc *sc = (void *)self;
96 uint8_t myaddr[ETHER_ADDR_LEN];
97 int i;
98
99 sc->sc_st = oa->oa_tag;
100 sc->sc_dmat = oa->oa_dmat;
101
102 if (bus_space_map(sc->sc_st,
103 SONIC_REG_BASE, SONIC_NREGS * 4, 0, &sc->sc_sh)) {
104 printf(": failed to map space for SONIC regs.\n");
105 return;
106 }
107
108 /* regs are addressed as words, big-endian. */
109 for (i = 0; i < SONIC_NREGS; i++) {
110 sc->sc_regmap[i] = (bus_size_t)((i * 4) + 2);
111 }
112
113 sc->sc_bigendian = 1;
114
115 sc->sc_dcr = DCR_BMS | DCR_RFT1 | DCR_TFT0;
116 sc->sc_dcr2 = 0;
117
118 switch (current_mac_model->machineid) {
119 case MACH_MACC610:
120 case MACH_MACC650:
121 case MACH_MACQ610:
122 case MACH_MACQ650:
123 case MACH_MACQ700:
124 case MACH_MACQ800:
125 case MACH_MACQ900:
126 case MACH_MACQ950:
127 sc->sc_dcr |= DCR_EXBUS;
128 sc->sc_32bit = 1;
129 break;
130
131 case MACH_MACLC575:
132 case MACH_MACP580:
133 case MACH_MACQ630:
134 /* Apple Comm Slot cards; assume they are 32 bit */
135 sc->sc_dcr |= DCR_EXBUS | DCR_USR1 | DCR_USR0;
136 sc->sc_32bit = 1;
137 break;
138
139 case MACH_MACPB500:
140 sc->sc_dcr |= DCR_SBUS | DCR_LBR;
141 sc->sc_32bit = 0; /* 16 bit interface */
142 break;
143
144 default:
145 printf(": unsupported machine type\n");
146 return;
147 }
148
149 if (sn_obio_getaddr(sc, myaddr) &&
150 sn_obio_getaddr_kludge(sc, myaddr)) { /* XXX kludge for PB */
151 printf(": failed to get MAC address.\n");
152 bus_space_unmap(sc->sc_st, sc->sc_sh, SONIC_NREGS * 4);
153 return;
154 }
155
156 printf(": integrated SONIC Ethernet adapter\n");
157
158 if (mac68k_machine.aux_interrupts) {
159 intr_establish(sonic_intr, (void *)sc, 3);
160 } else {
161 add_nubus_intr(SONIC_SLOTNO, (void (*)(void *))sonic_intr,
162 (void *)sc);
163 }
164
165 sonic_attach(sc, myaddr);
166 }
167
168 static int
169 sn_obio_getaddr(struct sonic_softc *sc, uint8_t *lladdr)
170 {
171 bus_space_handle_t bsh;
172
173 if (bus_space_map(sc->sc_st, SONIC_PROM_BASE, PAGE_SIZE, 0, &bsh)) {
174 printf(": failed to map space to read SONIC address.\n%s",
175 sc->sc_dev.dv_xname);
176 return (-1);
177 }
178
179 if (!mac68k_bus_space_probe(sc->sc_st, bsh, 0, 1)) {
180 bus_space_unmap(sc->sc_st, bsh, PAGE_SIZE);
181 return (-1);
182 }
183
184 sn_get_enaddr(sc->sc_st, bsh, 0, lladdr);
185
186 bus_space_unmap(sc->sc_st, bsh, PAGE_SIZE);
187
188 return 0;
189 }
190
191 /*
192 * Assume that the SONIC was initialized in MacOS. This should go away
193 * when we can properly get the MAC address on the PBs.
194 */
195 static int
196 sn_obio_getaddr_kludge(struct sonic_softc *sc, u_int8_t *lladdr)
197 {
198 int i, ors = 0;
199
200 /* Shut down NIC */
201 CSR_WRITE(sc, SONIC_CR, CR_RST);
202
203 /* For some reason, Apple fills top first. */
204 CSR_WRITE(sc, SONIC_CEP, 15);
205
206 i = CSR_READ(sc, SONIC_CAP2);
207
208 ors |= i;
209 lladdr[5] = i >> 8;
210 lladdr[4] = i;
211
212 i = CSR_READ(sc, SONIC_CAP1);
213
214 ors |= i;
215 lladdr[3] = i >> 8;
216 lladdr[2] = i;
217
218 i = CSR_READ(sc, SONIC_CAP0);
219
220 ors |= i;
221 lladdr[1] = i >> 8;
222 lladdr[0] = i;
223
224 CSR_WRITE(sc, SONIC_CR, 0);
225
226 if (ors == 0)
227 return -1;
228
229 return 0;
230 }
231