cy_pci.c revision 1.18
1/*	$NetBSD: cy_pci.c,v 1.18 2005/06/28 00:28:41 thorpej Exp $	*/
2
3/*
4 * cy_pci.c
5 *
6 * Driver for Cyclades Cyclom-8/16/32 multiport serial cards
7 * (currently not tested with Cyclom-32 cards)
8 *
9 * Timo Rossi, 1996
10 */
11
12#include <sys/cdefs.h>
13__KERNEL_RCSID(0, "$NetBSD: cy_pci.c,v 1.18 2005/06/28 00:28:41 thorpej Exp $");
14
15#include <sys/param.h>
16#include <sys/systm.h>
17#include <sys/device.h>
18
19#include <machine/bus.h>
20#include <machine/intr.h>
21
22#include <dev/pci/pcivar.h>
23#include <dev/pci/pcireg.h>
24#include <dev/pci/pcidevs.h>
25
26#include <dev/ic/cd1400reg.h>
27#include <dev/ic/cyreg.h>
28#include <dev/ic/cyvar.h>
29
30struct cy_pci_softc {
31	struct cy_softc sc_cy;		/* real cy softc */
32
33	bus_space_tag_t sc_iot;		/* PLX runtime i/o tag */
34	bus_space_handle_t sc_ioh;	/* PLX runtime i/o handle */
35};
36
37static const struct cy_pci_product {
38	pci_product_id_t cp_product;	/* product ID */
39	pcireg_t cp_memtype;		/* memory type */
40} cy_pci_products[] = {
41	{ PCI_PRODUCT_CYCLADES_CYCLOMY_1,
42	  PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT_1M },
43	{ PCI_PRODUCT_CYCLADES_CYCLOM4Y_1,
44	  PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT_1M },
45	{ PCI_PRODUCT_CYCLADES_CYCLOM8Y_1,
46	  PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT_1M },
47
48	{ PCI_PRODUCT_CYCLADES_CYCLOMY_2,
49	  PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT },
50	{ PCI_PRODUCT_CYCLADES_CYCLOM4Y_2,
51	  PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT },
52	{ PCI_PRODUCT_CYCLADES_CYCLOM8Y_2,
53	  PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT },
54
55	{ 0,
56	  0 },
57};
58static const int cy_pci_nproducts =
59    sizeof(cy_pci_products) / sizeof(cy_pci_products[0]);
60
61static const struct cy_pci_product *
62cy_pci_lookup(const struct pci_attach_args *pa)
63{
64	const struct cy_pci_product *cp;
65	int i;
66
67	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_CYCLADES)
68		return (NULL);
69
70	for (i = 0; i < cy_pci_nproducts; i++) {
71		cp = &cy_pci_products[i];
72		if (PCI_PRODUCT(pa->pa_id) == cp->cp_product)
73			return (cp);
74	}
75	return (NULL);
76}
77
78static int
79cy_pci_match(struct device *parent, struct cfdata *match, void *aux)
80{
81	struct pci_attach_args *pa = aux;
82
83	return (cy_pci_lookup(pa) != NULL);
84}
85
86static void
87cy_pci_attach(struct device *parent, struct device *self, void *aux)
88{
89	struct cy_pci_softc *psc = (void *) self;
90	struct cy_softc *sc = (void *) &psc->sc_cy;
91	struct pci_attach_args *pa = aux;
92	pci_intr_handle_t ih;
93	const struct cy_pci_product *cp;
94	const char *intrstr;
95	int plx_ver;
96
97	aprint_naive(": Multi-port serial controller\n");
98
99	sc->sc_bustype = CY_BUSTYPE_PCI;
100
101	cp = cy_pci_lookup(pa);
102	if (cp == NULL)
103		panic("cy_pci_attach: impossible");
104
105	aprint_normal(": Cyclades-Y multiport serial\n");
106
107	if (pci_mapreg_map(pa, 0x14, PCI_MAPREG_TYPE_IO, 0,
108	    &psc->sc_iot, &psc->sc_ioh, NULL, NULL) != 0) {
109		aprint_error("%s: unable to map PLX registers\n",
110		    sc->sc_dev.dv_xname);
111		return;
112	}
113
114	if (pci_mapreg_map(pa, 0x18, cp->cp_memtype, 0,
115	    &sc->sc_memt, &sc->sc_bsh, NULL, NULL) != 0) {
116		aprint_error("%s: unable to map device registers\n",
117		    sc->sc_dev.dv_xname);
118		return;
119	}
120
121	if (cy_find(sc) == 0) {
122		aprint_error("%s: unable to find CD1400s\n",
123		    sc->sc_dev.dv_xname);
124		return;
125	}
126
127	/*
128	 * XXX Like the Cyclades-Z, we should really check the EEPROM to
129	 * determine the "poll or interrupt" setting.  For now, we always
130	 * map the interrupt and enable it in the PLX.
131	 */
132
133	/* Map and establish the interrupt. */
134	if (pci_intr_map(pa, &ih) != 0) {
135		aprint_error(": unable to map interrupt\n");
136		return;
137	}
138	intrstr = pci_intr_string(pa->pa_pc, ih);
139	sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_TTY, cy_intr, sc);
140	if (sc->sc_ih == NULL) {
141		aprint_error(": unable to establish interrupt");
142		if (intrstr != NULL)
143			aprint_normal(" at %s", intrstr);
144		aprint_normal("\n");
145		return;
146	}
147	aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
148
149	cy_attach(sc);
150
151	plx_ver = bus_space_read_1(sc->sc_memt, sc->sc_bsh, CY_PLX_VER) & 0x0f;
152
153	/* Enable PCI card interrupts */
154	switch (plx_ver) {
155	case CY_PLX_9050:
156		bus_space_write_2(psc->sc_iot, psc->sc_ioh, CY_PCI_INTENA_9050,
157		    bus_space_read_2(psc->sc_iot, psc->sc_ioh,
158		    CY_PCI_INTENA_9050) | 0x40);
159		break;
160
161	case CY_PLX_9060:
162	case CY_PLX_9080:
163	default:
164		bus_space_write_2(psc->sc_iot, psc->sc_ioh, CY_PCI_INTENA,
165		    bus_space_read_2(psc->sc_iot, psc->sc_ioh,
166		    CY_PCI_INTENA) | 0x900);
167	}
168}
169
170CFATTACH_DECL(cy_pci, sizeof(struct cy_pci_softc),
171    cy_pci_match, cy_pci_attach, NULL, NULL);
172