xref: /netbsd/sys/dev/isa/i82365_isa.c (revision 6550d01e)
1 /*	$NetBSD: i82365_isa.c,v 1.33 2009/09/17 18:14:41 tsutsui Exp $	*/
2 
3 /*
4  * Copyright (c) 1997 Marc Horowitz.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by Marc Horowitz.
17  * 4. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: i82365_isa.c,v 1.33 2009/09/17 18:14:41 tsutsui Exp $");
34 
35 #define	PCICISADEBUG
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/device.h>
40 #include <sys/extent.h>
41 #include <sys/malloc.h>
42 
43 #include <sys/bus.h>
44 #include <sys/intr.h>
45 
46 #include <dev/isa/isareg.h>
47 #include <dev/isa/isavar.h>
48 
49 #include <dev/pcmcia/pcmciareg.h>
50 #include <dev/pcmcia/pcmciavar.h>
51 #include <dev/pcmcia/pcmciachip.h>
52 
53 #include <dev/ic/i82365reg.h>
54 #include <dev/ic/i82365var.h>
55 #include <dev/isa/i82365_isavar.h>
56 
57 #ifdef PCICISADEBUG
58 int	pcicisa_debug = 0;
59 #define	DPRINTF(arg) if (pcicisa_debug) printf arg;
60 #else
61 #define	DPRINTF(arg)
62 #endif
63 
64 int	pcic_isa_probe(device_t, cfdata_t, void *);
65 void	pcic_isa_attach(device_t, device_t, void *);
66 
67 CFATTACH_DECL(pcic_isa, sizeof(struct pcic_isa_softc),
68     pcic_isa_probe, pcic_isa_attach, NULL, NULL);
69 
70 static const struct pcmcia_chip_functions pcic_isa_functions = {
71 	pcic_chip_mem_alloc,
72 	pcic_chip_mem_free,
73 	pcic_chip_mem_map,
74 	pcic_chip_mem_unmap,
75 
76 	pcic_chip_io_alloc,
77 	pcic_chip_io_free,
78 	pcic_chip_io_map,
79 	pcic_chip_io_unmap,
80 
81 	pcic_isa_chip_intr_establish,
82 	pcic_isa_chip_intr_disestablish,
83 
84 	pcic_chip_socket_enable,
85 	pcic_chip_socket_disable,
86 	pcic_chip_socket_settype,
87 	NULL,
88 };
89 
90 int
91 pcic_isa_probe(device_t parent, cfdata_t match, void *aux)
92 {
93 	struct isa_attach_args *ia = aux;
94 	bus_space_tag_t iot = ia->ia_iot;
95 	bus_space_handle_t ioh, memh;
96 	int val, found, msize;
97 
98 	if (ia->ia_nio < 1)
99 		return 0;
100 	if (ia->ia_niomem < 1)
101 		return 0;
102 
103 	if (ISA_DIRECT_CONFIG(ia))
104 		return 0;
105 
106 	/* Disallow wildcarded i/o address. */
107 	if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
108 		return 0;
109 	if (ia->ia_iomem[0].ir_addr == ISA_UNKNOWN_IOMEM)
110 		return 0;
111 
112 	if (bus_space_map(iot, ia->ia_io[0].ir_addr, PCIC_IOSIZE, 0, &ioh))
113 		return 0;
114 
115 	if (ia->ia_iomem[0].ir_size == ISA_UNKNOWN_IOSIZ)
116 		msize = PCIC_MEMSIZE;
117 	else
118 		msize = ia->ia_iomem[0].ir_size;
119 
120 	if (bus_space_map(ia->ia_memt, ia->ia_iomem[0].ir_addr,
121 	    msize, 0, &memh)) {
122 		bus_space_unmap(iot, ioh, PCIC_IOSIZE);
123 		return 0;
124 	}
125 
126 	found = 0;
127 
128 	/*
129 	 * this could be done with a loop, but it would violate the
130 	 * abstraction
131 	 */
132 
133 	bus_space_write_1(iot, ioh, PCIC_REG_INDEX, C0SA + PCIC_IDENT);
134 
135 	val = bus_space_read_1(iot, ioh, PCIC_REG_DATA);
136 
137 	if (pcic_ident_ok(val))
138 		found++;
139 
140 
141 	bus_space_write_1(iot, ioh, PCIC_REG_INDEX, C0SB + PCIC_IDENT);
142 
143 	val = bus_space_read_1(iot, ioh, PCIC_REG_DATA);
144 
145 	if (pcic_ident_ok(val))
146 		found++;
147 
148 
149 	bus_space_write_1(iot, ioh, PCIC_REG_INDEX, C1SA + PCIC_IDENT);
150 
151 	val = bus_space_read_1(iot, ioh, PCIC_REG_DATA);
152 
153 	if (pcic_ident_ok(val))
154 		found++;
155 
156 
157 	bus_space_write_1(iot, ioh, PCIC_REG_INDEX, C1SB + PCIC_IDENT);
158 
159 	val = bus_space_read_1(iot, ioh, PCIC_REG_DATA);
160 
161 	if (pcic_ident_ok(val))
162 		found++;
163 
164 
165 	bus_space_unmap(iot, ioh, PCIC_IOSIZE);
166 	bus_space_unmap(ia->ia_memt, memh, msize);
167 
168 	if (!found)
169 		return 0;
170 
171 	ia->ia_nio = 1;
172 	ia->ia_io[0].ir_size = PCIC_IOSIZE;
173 
174 	ia->ia_niomem = 1;
175 	ia->ia_iomem[0].ir_size = msize;
176 
177 	/* IRQ is special. */
178 
179 	ia->ia_ndrq = 0;
180 
181 	return 1;
182 }
183 
184 void
185 pcic_isa_attach(device_t parent, device_t self, void *aux)
186 {
187 	struct pcic_isa_softc *isc = device_private(self);
188 	struct pcic_softc *sc = &isc->sc_pcic;
189 	struct isa_attach_args *ia = aux;
190 	isa_chipset_tag_t ic = ia->ia_ic;
191 	bus_space_tag_t iot = ia->ia_iot;
192 	bus_space_tag_t memt = ia->ia_memt;
193 	bus_space_handle_t ioh;
194 	bus_space_handle_t memh;
195 
196 	/* Map i/o space. */
197 	if (bus_space_map(iot, ia->ia_io[0].ir_addr, PCIC_IOSIZE, 0, &ioh)) {
198 		printf(": can't map i/o space\n");
199 		return;
200 	}
201 
202 	/* Map mem space. */
203 	if (bus_space_map(memt, ia->ia_iomem[0].ir_addr,
204 	    ia->ia_iomem[0].ir_size, 0, &memh)) {
205 		printf(": can't map mem space\n");
206 		return;
207 	}
208 
209 	sc->membase = ia->ia_iomem[0].ir_addr;
210 	sc->subregionmask =
211 	    (1 << (ia->ia_iomem[0].ir_size / PCIC_MEM_PAGESIZE)) - 1;
212 
213 	isc->sc_ic = ic;
214 	sc->pct = &pcic_isa_functions;
215 
216 	sc->iot = iot;
217 	sc->ioh = ioh;
218 	sc->memt = memt;
219 	sc->memh = memh;
220 	if (ia->ia_nirq > 0)
221 		sc->irq = ia->ia_irq[0].ir_irq;
222 	else
223 		sc->irq = ISA_UNKNOWN_IRQ;
224 
225 	printf("\n");
226 
227 	pcic_attach(sc);
228 	pcic_isa_bus_width_probe(sc, iot, ioh, ia->ia_io[0].ir_addr,
229 	    PCIC_IOSIZE);
230 	pcic_attach_sockets(sc);
231 
232 	config_interrupts(self, pcic_isa_config_interrupts);
233 }
234