1/* $NetBSD: if_en_pci.c,v 1.37 2014/03/29 19:28:24 christos Exp $ */
2
3/*
4 * Copyright (c) 1996 Charles D. Cranor and Washington University.
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 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28/*
29 *
30 * i f _ e n _ p c i . c
31 *
32 * author: Chuck Cranor <chuck@netbsd>
33 * started: spring, 1996.
34 *
35 * PCI glue for the eni155p card.
36 */
37
38#include <sys/cdefs.h>
39__KERNEL_RCSID(0, "$NetBSD: if_en_pci.c,v 1.37 2014/03/29 19:28:24 christos Exp $");
40
41#include <sys/param.h>
42#include <sys/systm.h>
43#include <sys/device.h>
44#include <sys/mbuf.h>
45#include <sys/socket.h>
46#include <sys/socketvar.h>
47
48#include <net/if.h>
49
50#include <dev/pci/pcivar.h>
51#include <dev/pci/pcireg.h>
52#include <dev/pci/pcidevs.h>
53
54#include <dev/ic/midwayreg.h>
55#include <dev/ic/midwayvar.h>
56
57
58/*
59 * local structures
60 */
61
62struct en_pci_softc {
63 /* bus independent stuff */
64 struct en_softc esc;
65
66 /* PCI bus glue */
67 void *sc_ih; /* interrupt handle */
68 pci_chipset_tag_t en_pc; /* for PCI calls */
69
70};
71
72/*
73 * local defines (PCI specific stuff)
74 */
75
76#if !defined(MIDWAY_ENIONLY)
77static void eni_get_macaddr(struct en_pci_softc *, struct pci_attach_args *);
78#endif
79#if !defined(MIDWAY_ADPONLY)
80static void adp_get_macaddr(struct en_pci_softc *, struct pci_attach_args *);
81#endif
82
83/*
84 * address of config base memory address register in PCI config space
85 * (this is card specific)
86 */
87
88#define PCI_CBMA PCI_BAR(0)
89
90/*
91 * tonga (pci bridge). ENI cards only!
92 */
93
94#define EN_TONGA 0x60 /* PCI config addr of tonga reg */
95
96#define TONGA_SWAP_DMA 0x80 /* endian swap control */
97#define TONGA_SWAP_BYTE 0x40
98#define TONGA_SWAP_WORD 0x20
99
100/*
101 * adaptec pci bridge. ADP cards only!
102 */
103
104#define ADP_PCIREG 0x050040 /* PCI control register */
105
106#define ADP_PCIREG_RESET 0x1 /* reset card */
107#define ADP_PCIREG_IENABLE 0x2 /* interrupt enable */
108#define ADP_PCIREG_SWAP_WORD 0x4 /* swap byte on slave access */
109#define ADP_PCIREG_SWAP_DMA 0x8 /* swap bytes on DMA */
110
111/*
112 * prototypes
113 */
114
115static int en_pci_match(device_t, cfdata_t, void *);
116static void en_pci_attach(device_t, device_t, void *);
117
118/*
119 * PCI autoconfig attachments
120 */
121
122CFATTACH_DECL_NEW(en_pci, sizeof(struct en_pci_softc),
123 en_pci_match, en_pci_attach, NULL, NULL);
124
125#if !defined(MIDWAY_ENIONLY)
126
127static void adp_busreset(void *);
128
129/*
130 * bus specific reset function [ADP only!]
131 */
132
133static void adp_busreset(void *v)
134{
135 struct en_softc *sc = (struct en_softc *) v;
136 u_int32_t dummy;
137
138 bus_space_write_4(sc->en_memt, sc->en_base, ADP_PCIREG, ADP_PCIREG_RESET);
139 DELAY(1000); /* let it reset */
140 dummy = bus_space_read_4(sc->en_memt, sc->en_base, ADP_PCIREG);
141 bus_space_write_4(sc->en_memt, sc->en_base, ADP_PCIREG,
142 (ADP_PCIREG_SWAP_WORD|ADP_PCIREG_SWAP_DMA|ADP_PCIREG_IENABLE));
143 dummy = bus_space_read_4(sc->en_memt, sc->en_base, ADP_PCIREG);
144 if ((dummy & (ADP_PCIREG_SWAP_WORD|ADP_PCIREG_SWAP_DMA)) !=
145 (ADP_PCIREG_SWAP_WORD|ADP_PCIREG_SWAP_DMA))
146 printf("adp_busreset: Adaptec ATM did NOT reset!\n");
147
148}
149#endif
150
151/***********************************************************************/
152
153/*
154 * autoconfig stuff
155 */
156
157static int
158en_pci_match(device_t parent, cfdata_t match, void *aux)
159{
160 struct pci_attach_args *pa = (struct pci_attach_args *) aux;
161
162#if !defined(MIDWAY_ADPONLY)
163 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_EFFICIENTNETS &&
164 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_EFFICIENTNETS_ENI155PF ||
165 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_EFFICIENTNETS_ENI155PA))
166 return 1;
167#endif
168
169#if !defined(MIDWAY_ENIONLY)
170 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ADP &&
171 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ADP_AIC5900 ||
172 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ADP_AIC5905))
173 return 1;
174#endif
175
176 return 0;
177}
178
179
180static void
181en_pci_attach(device_t parent, device_t self, void *aux)
182{
183 struct en_pci_softc *scp = device_private(self);
184 struct en_softc *sc = &scp->esc;
185 struct pci_attach_args *pa = aux;
186 pci_intr_handle_t ih;
187 const char *intrstr;
188 int retval;
189 char intrbuf[PCI_INTRSTR_LEN];
190
191 sc->sc_dev = self;
192
193 aprint_naive(": ATM controller\n");
194 aprint_normal("\n");
195
196 sc->is_adaptec = (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ADP) ? 1 : 0;
197 scp->en_pc = pa->pa_pc;
198
199 /*
200 * make sure bus mastering is enabled
201 */
202 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
203 pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
204 PCI_COMMAND_MASTER_ENABLE);
205
206 /*
207 * interrupt map
208 */
209
210 if (pci_intr_map(pa, &ih)) {
211 aprint_error_dev(sc->sc_dev, "couldn't map interrupt\n");
212 return;
213 }
214 intrstr = pci_intr_string(scp->en_pc, ih, intrbuf, sizeof(intrbuf));
215 scp->sc_ih = pci_intr_establish(scp->en_pc, ih, IPL_NET, en_intr, sc);
216 if (scp->sc_ih == NULL) {
217 aprint_error_dev(sc->sc_dev, "couldn't establish interrupt\n");
218 if (intrstr != NULL)
219 aprint_error(" at %s", intrstr);
220 aprint_error("\n");
221 return;
222 }
223 aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", intrstr);
224 sc->ipl = 1; /* XXX */
225
226 /*
227 * memory map
228 */
229
230 retval = pci_mapreg_map(pa, PCI_CBMA,
231 PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 0,
232 &sc->en_memt, &sc->en_base, NULL, &sc->en_obmemsz);
233 if (retval) {
234 aprint_error_dev(sc->sc_dev, "couldn't map memory\n");
235 return;
236 }
237
238 /*
239 * set up pci bridge
240 */
241
242#if !defined(MIDWAY_ENIONLY)
243 if (sc->is_adaptec) {
244 adp_get_macaddr(scp, pa);
245 sc->en_busreset = adp_busreset;
246 adp_busreset(sc);
247 }
248#endif
249
250#if !defined(MIDWAY_ADPONLY)
251 if (!sc->is_adaptec) {
252 eni_get_macaddr(scp, pa);
253 sc->en_busreset = NULL;
254 pci_conf_write(scp->en_pc, pa->pa_tag, EN_TONGA,
255 (TONGA_SWAP_DMA|TONGA_SWAP_WORD));
256 }
257#endif
258
259 /*
260 * done PCI specific stuff
261 */
262
263 en_attach(sc);
264
265}
266
267#if 0
268static void
269en_pci_shutdown(
270 int howto,
271 void *sc)
272{
273 struct en_pci_softc *psc = (struct en_pci_softc *)sc;
274
275 en_reset(&psc->esc);
276 DELAY(10);
277}
278#endif
279
280#if !defined(MIDWAY_ENIONLY)
281
282#if defined(__FreeBSD__)
283#define bus_space_read_1(t, h, o) \
284 ((void)t, (*(volatile u_int8_t *)((h) + (o))))
285#endif
286
287static void
288adp_get_macaddr(struct en_pci_softc *scp, struct pci_attach_args *pa)
289{
290 struct en_softc * sc = (struct en_softc *)scp;
291 int lcv;
292
293 for (lcv = 0; lcv < sizeof(sc->macaddr); lcv++)
294 sc->macaddr[lcv] = bus_space_read_1(sc->en_memt, sc->en_base,
295 MID_ADPMACOFF + lcv);
296}
297
298#endif /* MIDWAY_ENIONLY */
299
300#if !defined(MIDWAY_ADPONLY)
301
302/*
303 * Read station (MAC) address from serial EEPROM.
304 * derived from linux drivers/atm/eni.c by Werner Almesberger, EPFL LRC.
305 */
306#define EN_PROM_MAGIC 0x0c
307#define EN_PROM_DATA 0x02
308#define EN_PROM_CLK 0x01
309#define EN_ESI 64
310
311static void
312eni_get_macaddr(struct en_pci_softc *scp, struct pci_attach_args *pa)
313{
314 struct en_softc *sc = (struct en_softc *)scp;
315 pci_chipset_tag_t id = scp->en_pc;
316 pcitag_t tag = pa->pa_tag;
317 int i, j, address;
318 u_int32_t data, t_data;
319 u_int8_t tmp;
320
321 t_data = pci_conf_read(id, tag, EN_TONGA) & 0xffffff00;
322
323 data = EN_PROM_MAGIC | EN_PROM_DATA | EN_PROM_CLK;
324 pci_conf_write(id, tag, EN_TONGA, data);
325
326 for (i = 0; i < sizeof(sc->macaddr); i ++){
327 /* start operation */
328 data |= EN_PROM_DATA ;
329 pci_conf_write(id, tag, EN_TONGA, data);
330 data |= EN_PROM_CLK ;
331 pci_conf_write(id, tag, EN_TONGA, data);
332 data &= ~EN_PROM_DATA ;
333 pci_conf_write(id, tag, EN_TONGA, data);
334 data &= ~EN_PROM_CLK ;
335 pci_conf_write(id, tag, EN_TONGA, data);
336 /* send address with serial line */
337 address = ((i + EN_ESI) << 1) + 1;
338 for ( j = 7 ; j >= 0 ; j --){
339 data = (address >> j) & 1 ? data | EN_PROM_DATA :
340 data & ~EN_PROM_DATA;
341 pci_conf_write(id, tag, EN_TONGA, data);
342 data |= EN_PROM_CLK ;
343 pci_conf_write(id, tag, EN_TONGA, data);
344 data &= ~EN_PROM_CLK ;
345 pci_conf_write(id, tag, EN_TONGA, data);
346 }
347 /* get ack */
348 data |= EN_PROM_DATA ;
349 pci_conf_write(id, tag, EN_TONGA, data);
350 data |= EN_PROM_CLK ;
351 pci_conf_write(id, tag, EN_TONGA, data);
352 data = pci_conf_read(id, tag, EN_TONGA);
353 data &= ~EN_PROM_CLK ;
354 pci_conf_write(id, tag, EN_TONGA, data);
355 data |= EN_PROM_DATA ;
356 pci_conf_write(id, tag, EN_TONGA, data);
357
358 tmp = 0;
359
360 for ( j = 7 ; j >= 0 ; j --){
361 tmp <<= 1;
362 data |= EN_PROM_DATA ;
363 pci_conf_write(id, tag, EN_TONGA, data);
364 data |= EN_PROM_CLK ;
365 pci_conf_write(id, tag, EN_TONGA, data);
366 data = pci_conf_read(id, tag, EN_TONGA);
367 if(data & EN_PROM_DATA) tmp |= 1;
368 data &= ~EN_PROM_CLK ;
369 pci_conf_write(id, tag, EN_TONGA, data);
370 data |= EN_PROM_DATA ;
371 pci_conf_write(id, tag, EN_TONGA, data);
372 }
373 /* get ack */
374 data |= EN_PROM_DATA ;
375 pci_conf_write(id, tag, EN_TONGA, data);
376 data |= EN_PROM_CLK ;
377 pci_conf_write(id, tag, EN_TONGA, data);
378 data = pci_conf_read(id, tag, EN_TONGA);
379 data &= ~EN_PROM_CLK ;
380 pci_conf_write(id, tag, EN_TONGA, data);
381 data |= EN_PROM_DATA ;
382 pci_conf_write(id, tag, EN_TONGA, data);
383
384 sc->macaddr[i] = tmp;
385 }
386 /* stop operation */
387 data &= ~EN_PROM_DATA;
388 pci_conf_write(id, tag, EN_TONGA, data);
389 data |= EN_PROM_CLK;
390 pci_conf_write(id, tag, EN_TONGA, data);
391 data |= EN_PROM_DATA;
392 pci_conf_write(id, tag, EN_TONGA, data);
393 pci_conf_write(id, tag, EN_TONGA, t_data);
394}
395
396#endif /* !MIDWAY_ADPONLY */
397