xref: /netbsd-src/sys/dev/pci/cy_pci.c (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1 /*	$NetBSD: cy_pci.c,v 1.7 1997/06/17 05:44:22 cgd 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 #ifdef __BROKEN_INDIRECT_CONFIG
28 static int cy_match_pci __P((struct device *, void *, void *));
29 #else
30 static int cy_match_pci __P((struct device *, struct cfdata *, void *));
31 #endif
32 static void cy_attach_pci  __P((struct device *, struct device *, void *));
33 static int cy_map_pci __P((struct pci_attach_args *, bus_space_tag_t *,
34     bus_space_handle_t *, bus_size_t  *, bus_space_tag_t *,
35     bus_space_handle_t *, bus_size_t *));
36 static void cy_unmap_pci __P((bus_space_tag_t, bus_space_handle_t,
37     bus_size_t, bus_space_tag_t, bus_space_handle_t, bus_size_t));
38 
39 struct cfattach cy_pci_ca = {
40 	sizeof(struct cy_softc), cy_match_pci, cy_attach_pci
41 };
42 
43 static int
44 cy_map_pci(pap, iotp, iohp, iosizep, memtp, memhp, memsizep)
45 	struct pci_attach_args	*pap;
46 	bus_space_tag_t		*iotp, *memtp;
47 	bus_space_handle_t	*iohp, *memhp;
48 	bus_size_t		*iosizep, *memsizep;
49 {
50 	int ioh_valid, memh_valid;
51 	pcireg_t expected;
52 
53 	/* Map I/O (if possible). */
54 	ioh_valid = (pci_mapreg_map(pap, 0x14, PCI_MAPREG_TYPE_IO, 0,
55 	    iotp, iohp, NULL, iosizep) == 0);
56 
57 	/* Map mem (if possible).  Expected mem type depends on board ID.  */
58 	expected = PCI_MAPREG_TYPE_MEM;
59 	if (PCI_PRODUCT(pap->pa_id) == PCI_PRODUCT_CYCLADES_CYCLOMY_1)
60 		expected |= PCI_MAPREG_MEM_TYPE_32BIT_1M;
61 	else
62 		expected |= PCI_MAPREG_MEM_TYPE_32BIT;
63 	memh_valid = (pci_mapreg_map(pap, 0x18, expected, 0,
64 	    memtp, memhp, NULL, memsizep) == 0);
65 
66 	if (ioh_valid && memh_valid)
67 		return (1);
68 
69 	if (ioh_valid)
70 		bus_space_unmap(*iotp, *iohp, *iosizep);
71 	if (memh_valid)
72 		bus_space_unmap(*memtp, *memhp, *memsizep);
73 	return (0);
74 }
75 
76 static void
77 cy_unmap_pci(iot, ioh, iosize, memt, memh, memsize)
78 	bus_space_tag_t iot, memt;
79 	bus_space_handle_t ioh, memh;
80 	bus_size_t iosize, memsize;
81 
82 {
83 	bus_space_unmap(iot, ioh, iosize);
84 	bus_space_unmap(memt, memh, memsize);
85 }
86 
87 static int
88 cy_match_pci(parent, match, aux)
89 	struct device	*parent;
90 #ifdef __BROKEN_INDIRECT_CONFIG
91 	void		*match;
92 #else
93 	struct cfdata	*match;
94 #endif
95 	void		*aux;
96 {
97 	struct pci_attach_args *pap = aux;
98 	bus_space_tag_t iot, memt;
99 	bus_space_handle_t ioh, memh;
100 	bus_size_t iosize, memsize;
101 
102 	if (PCI_VENDOR(pap->pa_id) != PCI_VENDOR_CYCLADES)
103 		return 0;
104 
105 	switch (PCI_PRODUCT(pap->pa_id)) {
106 	case PCI_PRODUCT_CYCLADES_CYCLOMY_1:
107 		break;
108 	case PCI_PRODUCT_CYCLADES_CYCLOMY_2:
109 		break;
110 	default:
111 		return 0;
112 	}
113 
114 #ifdef CY_DEBUG
115 	printf("cy: Found Cyclades PCI device, id = 0x%x\n", pap->pa_id);
116 #endif
117 
118 	if (cy_map_pci(pap, &iot, &ioh, &iosize, &memt, &memh, &memsize) == 0)
119 		return (0);
120 
121 #if 0
122 	XXX probably should do something like this, but this code is just
123 	XXX too broken.
124 
125 #ifdef CY_DEBUG
126 	printf("%s: pci mapped mem 0x%lx (size %d), io 0x%x (size %d)\n",
127 	    sc.sc_dev.dv_xname, memaddr, memsize, iobase, iosize);
128 #endif
129 
130 	if ((rv = cy_find(&sc)) == 0)
131 		printf("%s: PCI Cyclom card with no CD1400s!?\n",
132 		    sc.sc_dev.dv_xname);
133 #endif /* 0 */
134 
135 	cy_unmap_pci(iot, ioh, iosize, memt, memh, memsize);
136 	return (1);
137 }
138 
139 
140 static void
141 cy_attach_pci(parent, self, aux)
142 	struct device  *parent, *self;
143 	void *aux;
144 {
145 	struct cy_softc *sc = (void *) self;
146 	struct pci_attach_args *pap = aux;
147 	pci_intr_handle_t intrhandle;
148 	bus_space_tag_t iot, memt;
149 	bus_space_handle_t ioh, memh;
150 	bus_size_t iosize, memsize;
151 	const char *intrstr;
152 
153 	sc->sc_bustype = CY_BUSTYPE_PCI;
154 
155 	if (cy_map_pci(pap, &iot, &ioh, &iosize, &memt, &memh,
156 	    &memsize) == 0) {
157 		printf(": unable to map device registers\n");
158 		return;
159 	}
160 
161 	sc->sc_memt = memt;
162 	sc->sc_bsh = memh;
163 
164 	if (cy_find(sc) == 0) {
165 		printf(": cannot find CD1400s\n");
166 		return;
167 	}
168 
169 	/* Set up interrupt handler. */
170 	if (pci_intr_map(pap->pa_pc, pap->pa_intrtag, pap->pa_intrpin,
171 	    pap->pa_intrline, &intrhandle) != 0) {
172 		printf(": couldn't map PCI interrupt\n");
173 		return;
174 	}
175 	intrstr = pci_intr_string(pap->pa_pc, intrhandle);
176 	sc->sc_ih = pci_intr_establish(pap->pa_pc, intrhandle, IPL_TTY,
177 	    cy_intr, sc);
178 	if (sc->sc_ih == NULL) {
179 		printf(": couldn't establish interrupt");
180 		if (intrstr != NULL)
181 			printf(" at %s", intrstr);
182 		printf("\n");
183 		return;
184 	}
185 	if (intrstr != NULL)
186 		printf(": interrupting at %s\n%s", intrstr, self->dv_xname);
187 
188 	/* attach the hardware */
189 	cy_attach(parent, self, aux);
190 
191 	/* Enable PCI card interrupts */
192 	bus_space_write_2(iot, ioh, CY_PCI_INTENA,
193 	    bus_space_read_2(iot, ioh, CY_PCI_INTENA) | 0x900);
194 }
195