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