1/*	$NetBSD: if_tlp_cardbus.c,v 1.69 2011/07/26 20:51:23 dyoung Exp $	*/
2
3/*-
4 * Copyright (c) 1999, 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/*
34 * CardBus bus front-end for the Digital Semiconductor ``Tulip'' (21x4x)
35 * Ethernet controller family driver.
36 */
37
38#include <sys/cdefs.h>
39__KERNEL_RCSID(0, "$NetBSD: if_tlp_cardbus.c,v 1.69 2011/07/26 20:51:23 dyoung Exp $");
40
41#include "opt_inet.h"
42
43#include <sys/param.h>
44#include <sys/systm.h>
45#include <sys/mbuf.h>
46#include <sys/malloc.h>
47#include <sys/kernel.h>
48#include <sys/socket.h>
49#include <sys/ioctl.h>
50#include <sys/errno.h>
51#include <sys/device.h>
52
53#include <machine/endian.h>
54
55#include <net/if.h>
56#include <net/if_dl.h>
57#include <net/if_media.h>
58#include <net/if_ether.h>
59
60#ifdef INET
61#include <netinet/in.h>
62#include <netinet/if_inarp.h>
63#endif
64
65
66#include <sys/bus.h>
67#include <sys/intr.h>
68
69#include <dev/mii/miivar.h>
70#include <dev/mii/mii_bitbang.h>
71
72#include <dev/ic/tulipreg.h>
73#include <dev/ic/tulipvar.h>
74
75#include <dev/pci/pcivar.h>
76#include <dev/pci/pcireg.h>
77#include <dev/pci/pcidevs.h>
78
79#include <dev/cardbus/cardbusvar.h>
80#include <dev/pci/pcidevs.h>
81
82/*
83 * PCI configuration space registers used by the Tulip.
84 */
85#define TULIP_PCI_IOBA PCI_BAR(0)	/* i/o mapped base */
86#define TULIP_PCI_MMBA PCI_BAR(1)	/* memory mapped base */
87#define	TULIP_PCI_CFDA		0x40	/* configuration driver area */
88
89#define	CFDA_SLEEP		0x80000000	/* sleep mode */
90#define	CFDA_SNOOZE		0x40000000	/* snooze mode */
91
92struct tulip_cardbus_softc {
93	struct tulip_softc sc_tulip;	/* real Tulip softc */
94
95	/* CardBus-specific goo. */
96	void	*sc_ih;			/* interrupt handle */
97	cardbus_devfunc_t sc_ct;	/* our CardBus devfuncs */
98	pcitag_t sc_tag;		/* our CardBus tag */
99	pcireg_t sc_csr;
100	bus_size_t sc_mapsize;		/* the size of mapped bus space
101					   region */
102
103	int	sc_bar_reg;		/* which BAR to use */
104	pcireg_t sc_bar_val;		/* value of the BAR */
105};
106
107int	tlp_cardbus_match(device_t, cfdata_t, void *);
108void	tlp_cardbus_attach(device_t, device_t, void *);
109int	tlp_cardbus_detach(device_t, int);
110
111CFATTACH_DECL_NEW(tlp_cardbus, sizeof(struct tulip_cardbus_softc),
112    tlp_cardbus_match, tlp_cardbus_attach, tlp_cardbus_detach, tlp_activate);
113
114const struct tulip_cardbus_product {
115	u_int32_t	tcp_vendor;	/* PCI vendor ID */
116	u_int32_t	tcp_product;	/* PCI product ID */
117	tulip_chip_t	tcp_chip;	/* base Tulip chip type */
118} tlp_cardbus_products[] = {
119	{ PCI_VENDOR_DEC,	PCI_PRODUCT_DEC_21142,
120	  TULIP_CHIP_21142 },
121
122	{ PCI_VENDOR_XIRCOM,	PCI_PRODUCT_XIRCOM_X3201_3_21143,
123	  TULIP_CHIP_X3201_3 },
124
125	{ PCI_VENDOR_ADMTEK,	PCI_PRODUCT_ADMTEK_AN983,
126	  TULIP_CHIP_AN985 },
127
128	{ PCI_VENDOR_ACCTON,	PCI_PRODUCT_ACCTON_EN2242,
129	  TULIP_CHIP_AN985 },
130
131	{ PCI_VENDOR_ABOCOM,	PCI_PRODUCT_ABOCOM_FE2500,
132	  TULIP_CHIP_AN985 },
133
134	{ PCI_VENDOR_ABOCOM,	PCI_PRODUCT_ABOCOM_PCM200,
135	  TULIP_CHIP_AN985 },
136
137	{ PCI_VENDOR_ABOCOM,	PCI_PRODUCT_ABOCOM_FE2500MX,
138	  TULIP_CHIP_AN985 },
139
140	{ PCI_VENDOR_HAWKING,	PCI_PRODUCT_HAWKING_PN672TX,
141	  TULIP_CHIP_AN985 },
142
143	{ PCI_VENDOR_ADMTEK,	PCI_PRODUCT_ADMTEK_AN985,
144	  TULIP_CHIP_AN985 },
145
146	{ PCI_VENDOR_MICROSOFT,	PCI_PRODUCT_MICROSOFT_MN120,
147	  TULIP_CHIP_AN985 },
148
149	{ PCI_VENDOR_LINKSYS, PCI_PRODUCT_LINKSYS_PCMPC200,
150	  TULIP_CHIP_AN985 },
151
152	{ 0,				0,
153	  TULIP_CHIP_INVALID },
154};
155
156struct tlp_cardbus_quirks {
157	void		(*tpq_func)(struct tulip_cardbus_softc *,
158			    const u_int8_t *);
159	u_int8_t	tpq_oui[3];
160};
161
162void	tlp_cardbus_lxt_quirks(struct tulip_cardbus_softc *,
163	    const u_int8_t *);
164
165const struct tlp_cardbus_quirks tlp_cardbus_21142_quirks[] = {
166	{ tlp_cardbus_lxt_quirks,	{ 0x00, 0x40, 0x05 } },
167	{ NULL,				{ 0, 0, 0 } }
168};
169
170void	tlp_cardbus_setup(struct tulip_cardbus_softc *);
171
172int	tlp_cardbus_enable(struct tulip_softc *);
173void	tlp_cardbus_disable(struct tulip_softc *);
174void	tlp_cardbus_power(struct tulip_softc *, int);
175
176void	tlp_cardbus_x3201_reset(struct tulip_softc *);
177
178const struct tulip_cardbus_product *tlp_cardbus_lookup
179   (const struct cardbus_attach_args *);
180void tlp_cardbus_get_quirks(struct tulip_cardbus_softc *,
181    const u_int8_t *, const struct tlp_cardbus_quirks *);
182
183const struct tulip_cardbus_product *
184tlp_cardbus_lookup(const struct cardbus_attach_args *ca)
185{
186	const struct tulip_cardbus_product *tcp;
187
188	for (tcp = tlp_cardbus_products; tcp->tcp_chip != TULIP_CHIP_INVALID;
189	     tcp++) {
190		if (PCI_VENDOR(ca->ca_id) == tcp->tcp_vendor &&
191		    PCI_PRODUCT(ca->ca_id) == tcp->tcp_product)
192			return (tcp);
193	}
194	return (NULL);
195}
196
197void
198tlp_cardbus_get_quirks(struct tulip_cardbus_softc *csc, const u_int8_t *enaddr, const struct tlp_cardbus_quirks *tpq)
199{
200
201	for (; tpq->tpq_func != NULL; tpq++) {
202		if (tpq->tpq_oui[0] == enaddr[0] &&
203		    tpq->tpq_oui[1] == enaddr[1] &&
204		    tpq->tpq_oui[2] == enaddr[2]) {
205			(*tpq->tpq_func)(csc, enaddr);
206			return;
207		}
208	}
209}
210
211int
212tlp_cardbus_match(device_t parent, cfdata_t match,
213    void *aux)
214{
215	struct cardbus_attach_args *ca = aux;
216
217	if (tlp_cardbus_lookup(ca) != NULL)
218		return (1);
219
220	return (0);
221}
222
223void
224tlp_cardbus_attach(device_t parent, device_t self,
225    void *aux)
226{
227	struct tulip_cardbus_softc *csc = device_private(self);
228	struct tulip_softc *sc = &csc->sc_tulip;
229	struct cardbus_attach_args *ca = aux;
230	cardbus_devfunc_t ct = ca->ca_ct;
231	const struct tulip_cardbus_product *tcp;
232	u_int8_t enaddr[ETHER_ADDR_LEN];
233	bus_addr_t adr;
234	pcireg_t reg;
235
236	sc->sc_dev = self;
237	sc->sc_devno = 0;
238	sc->sc_dmat = ca->ca_dmat;
239	csc->sc_ct = ct;
240	csc->sc_tag = ca->ca_tag;
241
242	tcp = tlp_cardbus_lookup(ca);
243	if (tcp == NULL) {
244		printf("\n");
245		panic("tlp_cardbus_attach: impossible");
246	}
247	sc->sc_chip = tcp->tcp_chip;
248
249	/*
250	 * By default, Tulip registers are 8 bytes long (4 bytes
251	 * followed by a 4 byte pad).
252	 */
253	sc->sc_regshift = 3;
254
255	/*
256	 * Power management hooks.
257	 */
258	sc->sc_enable = tlp_cardbus_enable;
259	sc->sc_disable = tlp_cardbus_disable;
260	sc->sc_power = tlp_cardbus_power;
261
262	/*
263	 * Get revision info, and set some chip-specific variables.
264	 */
265	sc->sc_rev = PCI_REVISION(ca->ca_class);
266	switch (sc->sc_chip) {
267	case TULIP_CHIP_21142:
268		if (sc->sc_rev >= 0x20)
269			sc->sc_chip = TULIP_CHIP_21143;
270		break;
271
272	case TULIP_CHIP_AN985:
273		/*
274		 * The AN983 and AN985 are very similar, and are
275		 * differentiated by a "signature" register that
276		 * is like, but not identical, to a PCI ID register.
277		 */
278		reg = Cardbus_conf_read(ct, csc->sc_tag, 0x80);
279		switch (reg) {
280		case 0x09811317:
281			sc->sc_chip = TULIP_CHIP_AN985;
282			break;
283
284		case 0x09851317:
285			sc->sc_chip = TULIP_CHIP_AN983;
286			break;
287
288		}
289		break;
290
291	default:
292		/* Nothing. -- to make gcc happy */
293		break;
294	}
295
296	printf(": %s Ethernet, pass %d.%d\n",
297	    tlp_chip_name(sc->sc_chip),
298	    (sc->sc_rev >> 4) & 0xf, sc->sc_rev & 0xf);
299
300	/*
301	 * Map the device.
302	 */
303	csc->sc_csr = PCI_COMMAND_MASTER_ENABLE;
304	if (Cardbus_mapreg_map(ct, TULIP_PCI_MMBA,
305	    PCI_MAPREG_TYPE_MEM, 0, &sc->sc_st, &sc->sc_sh, &adr,
306	    &csc->sc_mapsize) == 0) {
307		csc->sc_csr |= PCI_COMMAND_MEM_ENABLE;
308		csc->sc_bar_reg = TULIP_PCI_MMBA;
309		csc->sc_bar_val = adr | PCI_MAPREG_TYPE_MEM;
310	} else if (Cardbus_mapreg_map(ct, TULIP_PCI_IOBA,
311	    PCI_MAPREG_TYPE_IO, 0, &sc->sc_st, &sc->sc_sh, &adr,
312	    &csc->sc_mapsize) == 0) {
313		csc->sc_csr |= PCI_COMMAND_IO_ENABLE;
314		csc->sc_bar_reg = TULIP_PCI_IOBA;
315		csc->sc_bar_val = adr | PCI_MAPREG_TYPE_IO;
316	} else {
317		aprint_error_dev(self, "unable to map device registers\n");
318		return;
319	}
320
321	/*
322	 * Bring the chip out of powersave mode and initialize the
323	 * configuration registers.
324	 */
325	tlp_cardbus_setup(csc);
326
327	/*
328	 * Read the contents of the Ethernet Address ROM/SROM.
329	 */
330	switch (sc->sc_chip) {
331	case TULIP_CHIP_X3201_3:
332		/*
333		 * No SROM on this chip.
334		 */
335		break;
336
337	default:
338		if (tlp_read_srom(sc) == 0)
339			goto cant_cope;
340		break;
341	}
342
343	/*
344	 * Deal with chip/board quirks.  This includes setting up
345	 * the mediasw, and extracting the Ethernet address from
346	 * the rombuf.
347	 */
348	switch (sc->sc_chip) {
349	case TULIP_CHIP_21142:
350	case TULIP_CHIP_21143:
351		/* Check for new format SROM. */
352		if (tlp_isv_srom_enaddr(sc, enaddr) != 0) {
353			/*
354			 * We start out with the 2114x ISV media switch.
355			 * When we search for quirks, we may change to
356			 * a different switch.
357			 */
358			sc->sc_mediasw = &tlp_2114x_isv_mediasw;
359		} else if (tlp_parse_old_srom(sc, enaddr) == 0) {
360			/*
361			 * Not an ISV SROM, and not in old DEC Address
362			 * ROM format.  Try to snarf it out of the CIS.
363			 */
364			if (ca->ca_cis.funce.network.netid_present == 0)
365				goto cant_cope;
366
367			/* Grab the MAC address from the CIS. */
368			memcpy(enaddr, ca->ca_cis.funce.network.netid,
369			    sizeof(enaddr));
370		}
371
372		/*
373		 * Deal with any quirks this board might have.
374		 */
375		tlp_cardbus_get_quirks(csc, enaddr, tlp_cardbus_21142_quirks);
376
377		/*
378		 * If we don't already have a media switch, default to
379		 * MII-over-SIO, with no special reset routine.
380		 */
381		if (sc->sc_mediasw == NULL) {
382			printf("%s: defaulting to MII-over-SIO; no bets...\n",
383			    device_xname(self));
384			sc->sc_mediasw = &tlp_sio_mii_mediasw;
385		}
386		break;
387
388	case TULIP_CHIP_AN983:
389	case TULIP_CHIP_AN985:
390		/*
391		 * The ADMtek AN985's Ethernet address is located
392		 * at offset 8 of its EEPROM.
393		 */
394		memcpy(enaddr, &sc->sc_srom[8], ETHER_ADDR_LEN);
395
396		/*
397		 * The ADMtek AN985 can be configured in Single-Chip
398		 * mode or MAC-only mode.  Single-Chip uses the built-in
399		 * PHY, MAC-only has an external PHY (usually HomePNA).
400		 * The selection is based on an EEPROM setting, and both
401		 * PHYs are access via MII attached to SIO.
402		 *
403		 * The AN985 "ghosts" the internal PHY onto all
404		 * MII addresses, so we have to use a media init
405		 * routine that limits the search.
406		 * XXX How does this work with MAC-only mode?
407		 */
408		sc->sc_mediasw = &tlp_an985_mediasw;
409		break;
410
411	case TULIP_CHIP_X3201_3:
412		/*
413		 * The X3201 doesn't have an SROM.  Lift the MAC address
414		 * from the CIS.  Also, we have a special media switch:
415		 * MII-on-SIO, plus some special GPIO setup.
416		 */
417		memcpy(enaddr, ca->ca_cis.funce.network.netid, sizeof(enaddr));
418		sc->sc_reset = tlp_cardbus_x3201_reset;
419		sc->sc_mediasw = &tlp_sio_mii_mediasw;
420		break;
421
422	default:
423 cant_cope:
424		printf("%s: sorry, unable to handle your board\n",
425		    device_xname(self));
426		return;
427	}
428
429	/*
430	 * Finish off the attach.
431	 */
432	tlp_attach(sc, enaddr);
433
434	/*
435	 * Power down the socket.
436	 */
437	Cardbus_function_disable(csc->sc_ct);
438}
439
440int
441tlp_cardbus_detach(device_t self, int flags)
442{
443	struct tulip_cardbus_softc *csc = device_private(self);
444	struct tulip_softc *sc = &csc->sc_tulip;
445	struct cardbus_devfunc *ct = csc->sc_ct;
446	int rv;
447
448#if defined(DIAGNOSTIC)
449	if (ct == NULL)
450		panic("%s: data structure lacks", device_xname(self));
451#endif
452
453	rv = tlp_detach(sc);
454	if (rv)
455		return (rv);
456
457	/*
458	 * Unhook the interrupt handler.
459	 */
460	if (csc->sc_ih != NULL)
461		Cardbus_intr_disestablish(ct, csc->sc_ih);
462
463	/*
464	 * Release bus space and close window.
465	 */
466	if (csc->sc_bar_reg != 0)
467		Cardbus_mapreg_unmap(ct, csc->sc_bar_reg,
468		    sc->sc_st, sc->sc_sh, csc->sc_mapsize);
469
470	return (0);
471}
472
473int
474tlp_cardbus_enable(struct tulip_softc *sc)
475{
476	struct tulip_cardbus_softc *csc = (void *) sc;
477	cardbus_devfunc_t ct = csc->sc_ct;
478
479	/*
480	 * Power on the socket.
481	 */
482	Cardbus_function_enable(ct);
483
484	/*
485	 * Set up the PCI configuration registers.
486	 */
487	tlp_cardbus_setup(csc);
488
489	/*
490	 * Map and establish the interrupt.
491	 */
492	csc->sc_ih = Cardbus_intr_establish(ct, IPL_NET, tlp_intr, sc);
493	if (csc->sc_ih == NULL) {
494		aprint_error_dev(sc->sc_dev,
495				 "unable to establish interrupt\n");
496		Cardbus_function_disable(csc->sc_ct);
497		return (1);
498	}
499	return (0);
500}
501
502void
503tlp_cardbus_disable(struct tulip_softc *sc)
504{
505	struct tulip_cardbus_softc *csc = (void *) sc;
506	cardbus_devfunc_t ct = csc->sc_ct;
507
508	/* Unhook the interrupt handler. */
509	Cardbus_intr_disestablish(ct, csc->sc_ih);
510	csc->sc_ih = NULL;
511
512	/* Power down the socket. */
513	Cardbus_function_disable(ct);
514}
515
516void
517tlp_cardbus_power(struct tulip_softc *sc, int why)
518{
519
520	switch (why) {
521	case PWR_RESUME:
522		tlp_cardbus_enable(sc);
523		break;
524	case PWR_SUSPEND:
525		tlp_cardbus_disable(sc);
526		break;
527	}
528}
529
530void
531tlp_cardbus_setup(struct tulip_cardbus_softc *csc)
532{
533	struct tulip_softc *sc = &csc->sc_tulip;
534	cardbus_devfunc_t ct = csc->sc_ct;
535	pcireg_t reg;
536
537	/*
538	 * Check to see if the device is in power-save mode, and
539	 * bring it out if necessary.
540	 */
541	switch (sc->sc_chip) {
542	case TULIP_CHIP_21142:
543	case TULIP_CHIP_21143:
544	case TULIP_CHIP_X3201_3:
545		/*
546		 * Clear the "sleep mode" bit in the CFDA register.
547		 */
548		reg = Cardbus_conf_read(ct, csc->sc_tag, TULIP_PCI_CFDA);
549		if (reg & (CFDA_SLEEP|CFDA_SNOOZE))
550			Cardbus_conf_write(ct, csc->sc_tag, TULIP_PCI_CFDA,
551			    reg & ~(CFDA_SLEEP|CFDA_SNOOZE));
552		break;
553
554	default:
555		/* Nothing. -- to make gcc happy */
556		break;
557	}
558
559	(void)cardbus_set_powerstate(ct, csc->sc_tag, PCI_PWR_D0);
560
561	/* Program the BAR. */
562	Cardbus_conf_write(ct, csc->sc_tag, csc->sc_bar_reg, csc->sc_bar_val);
563
564	/* Enable the appropriate bits in the PCI CSR. */
565	reg = Cardbus_conf_read(ct, csc->sc_tag, PCI_COMMAND_STATUS_REG);
566	reg &= ~(PCI_COMMAND_IO_ENABLE|PCI_COMMAND_MEM_ENABLE);
567	reg |= csc->sc_csr;
568	Cardbus_conf_write(ct, csc->sc_tag, PCI_COMMAND_STATUS_REG, reg);
569
570	/*
571	 * Make sure the latency timer is set to some reasonable
572	 * value.
573	 */
574	reg = Cardbus_conf_read(ct, csc->sc_tag, PCI_BHLC_REG);
575	if (PCI_LATTIMER(reg) < 0x20) {
576		reg &= ~(PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT);
577		reg |= (0x20 << PCI_LATTIMER_SHIFT);
578		Cardbus_conf_write(ct, csc->sc_tag, PCI_BHLC_REG, reg);
579	}
580}
581
582void
583tlp_cardbus_x3201_reset(struct tulip_softc *sc)
584{
585	u_int32_t reg;
586
587	reg = TULIP_READ(sc, CSR_SIAGEN);
588
589	/* make GP[2,0] outputs */
590	TULIP_WRITE(sc, CSR_SIAGEN, (reg & ~SIAGEN_MD) | SIAGEN_CWE |
591	    0x00050000);
592	TULIP_WRITE(sc, CSR_SIAGEN, (reg & ~SIAGEN_CWE) | SIAGEN_MD);
593}
594
595void
596tlp_cardbus_lxt_quirks(struct tulip_cardbus_softc *csc,
597    const u_int8_t *enaddr)
598{
599	struct tulip_softc *sc = &csc->sc_tulip;
600
601	sc->sc_mediasw = &tlp_sio_mii_mediasw;
602}
603