1 /*        $NetBSD: i82365_isa.c,v 1.36 2022/09/25 17:09:36 thorpej 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.36 2022/09/25 17:09:36 thorpej 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 
42 #include <sys/bus.h>
43 #include <sys/intr.h>
44 
45 #include <dev/isa/isareg.h>
46 #include <dev/isa/isavar.h>
47 
48 #include <dev/pcmcia/pcmciareg.h>
49 #include <dev/pcmcia/pcmciavar.h>
50 #include <dev/pcmcia/pcmciachip.h>
51 
52 #include <dev/ic/i82365reg.h>
53 #include <dev/ic/i82365var.h>
54 #include <dev/isa/i82365_isavar.h>
55 
56 #ifdef PCICISADEBUG
57 int       pcicisa_debug = 0;
58 #define   DPRINTF(arg) if (pcicisa_debug) printf arg;
59 #else
60 #define   DPRINTF(arg)
61 #endif
62 
63 int       pcic_isa_probe(device_t, cfdata_t, void *);
64 void      pcic_isa_attach(device_t, device_t, void *);
65 
66 CFATTACH_DECL_NEW(pcic_isa, sizeof(struct pcic_isa_softc),
67     pcic_isa_probe, pcic_isa_attach, NULL, NULL);
68 
69 static const struct pcmcia_chip_functions pcic_isa_functions = {
70           pcic_chip_mem_alloc,
71           pcic_chip_mem_free,
72           pcic_chip_mem_map,
73           pcic_chip_mem_unmap,
74 
75           pcic_chip_io_alloc,
76           pcic_chip_io_free,
77           pcic_chip_io_map,
78           pcic_chip_io_unmap,
79 
80           pcic_isa_chip_intr_establish,
81           pcic_isa_chip_intr_disestablish,
82 
83           pcic_chip_socket_enable,
84           pcic_chip_socket_disable,
85           pcic_chip_socket_settype,
86           NULL,
87 };
88 
89 int
pcic_isa_probe(device_t parent,cfdata_t match,void * aux)90 pcic_isa_probe(device_t parent, cfdata_t match, void *aux)
91 {
92           struct isa_attach_args *ia = aux;
93           bus_space_tag_t iot = ia->ia_iot;
94           bus_space_handle_t ioh, memh;
95           int val, found, msize;
96 
97           if (ia->ia_nio < 1)
98                     return 0;
99           if (ia->ia_niomem < 1)
100                     return 0;
101 
102           if (ISA_DIRECT_CONFIG(ia))
103                     return 0;
104 
105           /* Disallow wildcarded i/o address. */
106           if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
107                     return 0;
108           if (ia->ia_iomem[0].ir_addr == ISA_UNKNOWN_IOMEM)
109                     return 0;
110 
111           if (bus_space_map(iot, ia->ia_io[0].ir_addr, PCIC_IOSIZE, 0, &ioh))
112                     return 0;
113 
114           if (ia->ia_iomem[0].ir_size == ISA_UNKNOWN_IOSIZ)
115                     msize = PCIC_MEMSIZE;
116           else
117                     msize = ia->ia_iomem[0].ir_size;
118 
119           if (bus_space_map(ia->ia_memt, ia->ia_iomem[0].ir_addr,
120               msize, 0, &memh)) {
121                     bus_space_unmap(iot, ioh, PCIC_IOSIZE);
122                     return 0;
123           }
124 
125           found = 0;
126 
127           /*
128            * this could be done with a loop, but it would violate the
129            * abstraction
130            */
131 
132           bus_space_write_1(iot, ioh, PCIC_REG_INDEX, C0SA + PCIC_IDENT);
133 
134           val = bus_space_read_1(iot, ioh, PCIC_REG_DATA);
135 
136           if (pcic_ident_ok(val))
137                     found++;
138 
139 
140           bus_space_write_1(iot, ioh, PCIC_REG_INDEX, C0SB + PCIC_IDENT);
141 
142           val = bus_space_read_1(iot, ioh, PCIC_REG_DATA);
143 
144           if (pcic_ident_ok(val))
145                     found++;
146 
147 
148           bus_space_write_1(iot, ioh, PCIC_REG_INDEX, C1SA + PCIC_IDENT);
149 
150           val = bus_space_read_1(iot, ioh, PCIC_REG_DATA);
151 
152           if (pcic_ident_ok(val))
153                     found++;
154 
155 
156           bus_space_write_1(iot, ioh, PCIC_REG_INDEX, C1SB + PCIC_IDENT);
157 
158           val = bus_space_read_1(iot, ioh, PCIC_REG_DATA);
159 
160           if (pcic_ident_ok(val))
161                     found++;
162 
163 
164           bus_space_unmap(iot, ioh, PCIC_IOSIZE);
165           bus_space_unmap(ia->ia_memt, memh, msize);
166 
167           if (!found)
168                     return 0;
169 
170           ia->ia_nio = 1;
171           ia->ia_io[0].ir_size = PCIC_IOSIZE;
172 
173           ia->ia_niomem = 1;
174           ia->ia_iomem[0].ir_size = msize;
175 
176           /* IRQ is special. */
177 
178           ia->ia_ndrq = 0;
179 
180           return 1;
181 }
182 
183 void
pcic_isa_attach(device_t parent,device_t self,void * aux)184 pcic_isa_attach(device_t parent, device_t self, void *aux)
185 {
186           struct pcic_isa_softc *isc = device_private(self);
187           struct pcic_softc *sc = &isc->sc_pcic;
188           struct isa_attach_args *ia = aux;
189           isa_chipset_tag_t ic = ia->ia_ic;
190           bus_space_tag_t iot = ia->ia_iot;
191           bus_space_tag_t memt = ia->ia_memt;
192           bus_space_handle_t ioh;
193           bus_space_handle_t memh;
194 
195           aprint_naive("\n");
196           sc->dev = self;
197 
198           /* Map i/o space. */
199           if (bus_space_map(iot, ia->ia_io[0].ir_addr, PCIC_IOSIZE, 0, &ioh)) {
200                     aprint_error(": can't map i/o space\n");
201                     return;
202           }
203 
204           /* Map mem space. */
205           if (bus_space_map(memt, ia->ia_iomem[0].ir_addr,
206               ia->ia_iomem[0].ir_size, 0, &memh)) {
207                     aprint_error(": can't map mem space\n");
208                     return;
209           }
210 
211           sc->membase = ia->ia_iomem[0].ir_addr;
212           sc->subregionmask =
213               (1 << (ia->ia_iomem[0].ir_size / PCIC_MEM_PAGESIZE)) - 1;
214 
215           isc->sc_ic = ic;
216           sc->pct = &pcic_isa_functions;
217 
218           sc->iot = iot;
219           sc->ioh = ioh;
220           sc->memt = memt;
221           sc->memh = memh;
222           if (ia->ia_nirq > 0)
223                     sc->irq = ia->ia_irq[0].ir_irq;
224           else
225                     sc->irq = ISA_UNKNOWN_IRQ;
226 
227           aprint_normal("\n");
228 
229           pcic_attach(sc);
230           pcic_isa_bus_width_probe(sc, iot, ioh, ia->ia_io[0].ir_addr,
231               PCIC_IOSIZE);
232           pcic_attach_sockets(sc);
233 
234           config_interrupts(self, pcic_isa_config_interrupts);
235 }
236