i82365.c revision 1.1.2.13 1 1.1.2.13 enami /* $NetBSD: i82365.c,v 1.1.2.13 1997/10/16 09:18:09 enami Exp $ */
2 1.1.2.12 thorpej
3 1.1.2.12 thorpej #define PCICDEBUG
4 1.1.2.1 marc
5 1.1.2.1 marc #include <sys/types.h>
6 1.1.2.1 marc #include <sys/param.h>
7 1.1.2.1 marc #include <sys/systm.h>
8 1.1.2.1 marc #include <sys/device.h>
9 1.1.2.1 marc #include <sys/extent.h>
10 1.1.2.1 marc #include <sys/malloc.h>
11 1.1.2.1 marc
12 1.1.2.1 marc #include <vm/vm.h>
13 1.1.2.1 marc
14 1.1.2.1 marc #include <machine/bus.h>
15 1.1.2.1 marc #include <machine/intr.h>
16 1.1.2.1 marc
17 1.1.2.1 marc #include <dev/pcmcia/pcmciareg.h>
18 1.1.2.4 thorpej #include <dev/pcmcia/pcmciavar.h>
19 1.1.2.1 marc
20 1.1.2.1 marc #include <dev/ic/i82365reg.h>
21 1.1.2.8 marc #include <dev/ic/i82365var.h>
22 1.1.2.1 marc
23 1.1.2.1 marc #ifdef PCICDEBUG
24 1.1.2.2 thorpej int pcic_debug = 0;
25 1.1.2.12 thorpej #define DPRINTF(arg) if (pcic_debug) printf arg;
26 1.1.2.1 marc #else
27 1.1.2.12 thorpej #define DPRINTF(arg)
28 1.1.2.1 marc #endif
29 1.1.2.1 marc
30 1.1.2.12 thorpej #define PCIC_VENDOR_UNKNOWN 0
31 1.1.2.12 thorpej #define PCIC_VENDOR_I82365SLR0 1
32 1.1.2.12 thorpej #define PCIC_VENDOR_I82365SLR1 2
33 1.1.2.12 thorpej #define PCIC_VENDOR_CIRRUS_PD6710 3
34 1.1.2.12 thorpej #define PCIC_VENDOR_CIRRUS_PD672X 4
35 1.1.2.12 thorpej
36 1.1.2.12 thorpej /*
37 1.1.2.12 thorpej * Individual drivers will allocate their own memory and io regions. Memory
38 1.1.2.12 thorpej * regions must be a multiple of 4k, aligned on a 4k boundary.
39 1.1.2.12 thorpej */
40 1.1.2.1 marc
41 1.1.2.12 thorpej #define PCIC_MEM_ALIGN PCIC_MEM_PAGESIZE
42 1.1.2.1 marc
43 1.1.2.12 thorpej void pcic_attach_socket __P((struct pcic_handle *));
44 1.1.2.12 thorpej void pcic_init_socket __P((struct pcic_handle *));
45 1.1.2.1 marc
46 1.1.2.1 marc #ifdef __BROKEN_INDIRECT_CONFIG
47 1.1.2.12 thorpej int pcic_submatch __P((struct device *, void *, void *));
48 1.1.2.1 marc #else
49 1.1.2.12 thorpej int pcic_submatch __P((struct device *, struct cfdata *, void *));
50 1.1.2.1 marc #endif
51 1.1.2.12 thorpej int pcic_print __P((void *arg, const char *pnp));
52 1.1.2.12 thorpej int pcic_intr_socket __P((struct pcic_handle *));
53 1.1.2.1 marc
54 1.1.2.12 thorpej void pcic_attach_card __P((struct pcic_handle *));
55 1.1.2.12 thorpej void pcic_detach_card __P((struct pcic_handle *));
56 1.1.2.10 marc
57 1.1.2.12 thorpej void pcic_chip_do_mem_map __P((struct pcic_handle *, int));
58 1.1.2.12 thorpej void pcic_chip_do_io_map __P((struct pcic_handle *, int));
59 1.1.2.1 marc
60 1.1.2.1 marc struct cfdriver pcic_cd = {
61 1.1.2.1 marc NULL, "pcic", DV_DULL
62 1.1.2.1 marc };
63 1.1.2.1 marc
64 1.1.2.1 marc int
65 1.1.2.1 marc pcic_ident_ok(ident)
66 1.1.2.12 thorpej int ident;
67 1.1.2.1 marc {
68 1.1.2.12 thorpej /* this is very empirical and heuristic */
69 1.1.2.1 marc
70 1.1.2.12 thorpej if ((ident == 0) || (ident == 0xff) || (ident & PCIC_IDENT_ZERO))
71 1.1.2.12 thorpej return (0);
72 1.1.2.1 marc
73 1.1.2.12 thorpej if ((ident & PCIC_IDENT_IFTYPE_MASK) != PCIC_IDENT_IFTYPE_MEM_AND_IO) {
74 1.1.2.1 marc #ifdef DIAGNOSTIC
75 1.1.2.12 thorpej printf("pcic: does not support memory and I/O cards, "
76 1.1.2.12 thorpej "ignored (ident=%0x)\n", ident);
77 1.1.2.1 marc #endif
78 1.1.2.12 thorpej return (0);
79 1.1.2.12 thorpej }
80 1.1.2.12 thorpej return (1);
81 1.1.2.1 marc }
82 1.1.2.1 marc
83 1.1.2.1 marc int
84 1.1.2.1 marc pcic_vendor(h)
85 1.1.2.12 thorpej struct pcic_handle *h;
86 1.1.2.1 marc {
87 1.1.2.12 thorpej int reg;
88 1.1.2.1 marc
89 1.1.2.12 thorpej /*
90 1.1.2.12 thorpej * the chip_id of the cirrus toggles between 11 and 00 after a write.
91 1.1.2.12 thorpej * weird.
92 1.1.2.12 thorpej */
93 1.1.2.1 marc
94 1.1.2.12 thorpej pcic_write(h, PCIC_CIRRUS_CHIP_INFO, 0);
95 1.1.2.1 marc reg = pcic_read(h, -1);
96 1.1.2.1 marc
97 1.1.2.12 thorpej if ((reg & PCIC_CIRRUS_CHIP_INFO_CHIP_ID) ==
98 1.1.2.12 thorpej PCIC_CIRRUS_CHIP_INFO_CHIP_ID) {
99 1.1.2.12 thorpej reg = pcic_read(h, -1);
100 1.1.2.12 thorpej if ((reg & PCIC_CIRRUS_CHIP_INFO_CHIP_ID) == 0) {
101 1.1.2.12 thorpej if (reg & PCIC_CIRRUS_CHIP_INFO_SLOTS)
102 1.1.2.12 thorpej return (PCIC_VENDOR_CIRRUS_PD672X);
103 1.1.2.12 thorpej else
104 1.1.2.12 thorpej return (PCIC_VENDOR_CIRRUS_PD6710);
105 1.1.2.12 thorpej }
106 1.1.2.12 thorpej }
107 1.1.2.12 thorpej /* XXX how do I identify the GD6729? */
108 1.1.2.8 marc
109 1.1.2.12 thorpej reg = pcic_read(h, PCIC_IDENT);
110 1.1.2.1 marc
111 1.1.2.12 thorpej if ((reg & PCIC_IDENT_REV_MASK) == PCIC_IDENT_REV_I82365SLR0)
112 1.1.2.12 thorpej return (PCIC_VENDOR_I82365SLR0);
113 1.1.2.12 thorpej else
114 1.1.2.12 thorpej return (PCIC_VENDOR_I82365SLR1);
115 1.1.2.1 marc
116 1.1.2.12 thorpej return (PCIC_VENDOR_UNKNOWN);
117 1.1.2.1 marc }
118 1.1.2.1 marc
119 1.1.2.1 marc char *
120 1.1.2.1 marc pcic_vendor_to_string(vendor)
121 1.1.2.12 thorpej int vendor;
122 1.1.2.1 marc {
123 1.1.2.12 thorpej switch (vendor) {
124 1.1.2.12 thorpej case PCIC_VENDOR_I82365SLR0:
125 1.1.2.12 thorpej return ("Intel 82365SL Revision 0");
126 1.1.2.12 thorpej case PCIC_VENDOR_I82365SLR1:
127 1.1.2.12 thorpej return ("Intel 82365SL Revision 1");
128 1.1.2.12 thorpej case PCIC_VENDOR_CIRRUS_PD6710:
129 1.1.2.12 thorpej return ("Cirrus PD6710");
130 1.1.2.12 thorpej case PCIC_VENDOR_CIRRUS_PD672X:
131 1.1.2.12 thorpej return ("Cirrus PD672X");
132 1.1.2.12 thorpej }
133 1.1.2.1 marc
134 1.1.2.12 thorpej return ("Unknown controller");
135 1.1.2.1 marc }
136 1.1.2.1 marc
137 1.1.2.1 marc void
138 1.1.2.8 marc pcic_attach(sc)
139 1.1.2.12 thorpej struct pcic_softc *sc;
140 1.1.2.1 marc {
141 1.1.2.12 thorpej int vendor, count, i, reg;
142 1.1.2.1 marc
143 1.1.2.12 thorpej /* now check for each controller/socket */
144 1.1.2.1 marc
145 1.1.2.12 thorpej /*
146 1.1.2.12 thorpej * this could be done with a loop, but it would violate the
147 1.1.2.12 thorpej * abstraction
148 1.1.2.12 thorpej */
149 1.1.2.1 marc
150 1.1.2.12 thorpej count = 0;
151 1.1.2.1 marc
152 1.1.2.12 thorpej DPRINTF(("pcic ident regs:"));
153 1.1.2.10 marc
154 1.1.2.12 thorpej sc->handle[0].sc = sc;
155 1.1.2.12 thorpej sc->handle[0].sock = C0SA;
156 1.1.2.12 thorpej if (pcic_ident_ok(reg = pcic_read(&sc->handle[0], PCIC_IDENT))) {
157 1.1.2.12 thorpej sc->handle[0].flags = PCIC_FLAG_SOCKETP;
158 1.1.2.12 thorpej count++;
159 1.1.2.12 thorpej } else {
160 1.1.2.12 thorpej sc->handle[0].flags = 0;
161 1.1.2.12 thorpej }
162 1.1.2.1 marc
163 1.1.2.12 thorpej DPRINTF((" 0x%02x", reg));
164 1.1.2.10 marc
165 1.1.2.12 thorpej sc->handle[1].sc = sc;
166 1.1.2.12 thorpej sc->handle[1].sock = C0SB;
167 1.1.2.12 thorpej if (pcic_ident_ok(reg = pcic_read(&sc->handle[1], PCIC_IDENT))) {
168 1.1.2.12 thorpej sc->handle[1].flags = PCIC_FLAG_SOCKETP;
169 1.1.2.12 thorpej count++;
170 1.1.2.12 thorpej } else {
171 1.1.2.12 thorpej sc->handle[1].flags = 0;
172 1.1.2.12 thorpej }
173 1.1.2.1 marc
174 1.1.2.12 thorpej DPRINTF((" 0x%02x", reg));
175 1.1.2.10 marc
176 1.1.2.12 thorpej sc->handle[2].sc = sc;
177 1.1.2.12 thorpej sc->handle[2].sock = C1SA;
178 1.1.2.12 thorpej if (pcic_ident_ok(reg = pcic_read(&sc->handle[2], PCIC_IDENT))) {
179 1.1.2.12 thorpej sc->handle[2].flags = PCIC_FLAG_SOCKETP;
180 1.1.2.12 thorpej count++;
181 1.1.2.12 thorpej } else {
182 1.1.2.12 thorpej sc->handle[2].flags = 0;
183 1.1.2.12 thorpej }
184 1.1.2.1 marc
185 1.1.2.12 thorpej DPRINTF((" 0x%02x", reg));
186 1.1.2.10 marc
187 1.1.2.12 thorpej sc->handle[3].sc = sc;
188 1.1.2.12 thorpej sc->handle[3].sock = C1SB;
189 1.1.2.12 thorpej if (pcic_ident_ok(reg = pcic_read(&sc->handle[3], PCIC_IDENT))) {
190 1.1.2.12 thorpej sc->handle[3].flags = PCIC_FLAG_SOCKETP;
191 1.1.2.12 thorpej count++;
192 1.1.2.12 thorpej } else {
193 1.1.2.12 thorpej sc->handle[3].flags = 0;
194 1.1.2.12 thorpej }
195 1.1.2.1 marc
196 1.1.2.12 thorpej DPRINTF((" 0x%02x\n", reg));
197 1.1.2.10 marc
198 1.1.2.12 thorpej if (count == 0)
199 1.1.2.12 thorpej panic("pcic_attach: attach found no sockets");
200 1.1.2.1 marc
201 1.1.2.12 thorpej /* establish the interrupt */
202 1.1.2.1 marc
203 1.1.2.12 thorpej /* XXX block interrupts? */
204 1.1.2.1 marc
205 1.1.2.12 thorpej for (i = 0; i < PCIC_NSLOTS; i++) {
206 1.1.2.10 marc #if 0
207 1.1.2.12 thorpej /*
208 1.1.2.12 thorpej * this should work, but w/o it, setting tty flags hangs at
209 1.1.2.12 thorpej * boot time.
210 1.1.2.12 thorpej */
211 1.1.2.12 thorpej if (sc->handle[i].flags & PCIC_FLAG_SOCKETP)
212 1.1.2.10 marc #endif
213 1.1.2.12 thorpej {
214 1.1.2.12 thorpej pcic_write(&sc->handle[i], PCIC_CSC_INTR, 0);
215 1.1.2.12 thorpej pcic_read(&sc->handle[i], PCIC_CSC);
216 1.1.2.12 thorpej }
217 1.1.2.12 thorpej }
218 1.1.2.1 marc
219 1.1.2.12 thorpej if ((sc->handle[0].flags & PCIC_FLAG_SOCKETP) ||
220 1.1.2.12 thorpej (sc->handle[1].flags & PCIC_FLAG_SOCKETP)) {
221 1.1.2.12 thorpej vendor = pcic_vendor(&sc->handle[0]);
222 1.1.2.12 thorpej
223 1.1.2.12 thorpej printf("%s: controller 0 (%s) has ", sc->dev.dv_xname,
224 1.1.2.12 thorpej pcic_vendor_to_string(vendor));
225 1.1.2.12 thorpej
226 1.1.2.12 thorpej if ((sc->handle[0].flags & PCIC_FLAG_SOCKETP) &&
227 1.1.2.12 thorpej (sc->handle[1].flags & PCIC_FLAG_SOCKETP))
228 1.1.2.12 thorpej printf("sockets A and B\n");
229 1.1.2.12 thorpej else if (sc->handle[0].flags & PCIC_FLAG_SOCKETP)
230 1.1.2.12 thorpej printf("socket A only\n");
231 1.1.2.12 thorpej else
232 1.1.2.12 thorpej printf("socket B only\n");
233 1.1.2.12 thorpej
234 1.1.2.12 thorpej if (sc->handle[0].flags & PCIC_FLAG_SOCKETP)
235 1.1.2.12 thorpej sc->handle[0].vendor = vendor;
236 1.1.2.12 thorpej if (sc->handle[1].flags & PCIC_FLAG_SOCKETP)
237 1.1.2.12 thorpej sc->handle[1].vendor = vendor;
238 1.1.2.12 thorpej }
239 1.1.2.12 thorpej if ((sc->handle[2].flags & PCIC_FLAG_SOCKETP) ||
240 1.1.2.12 thorpej (sc->handle[3].flags & PCIC_FLAG_SOCKETP)) {
241 1.1.2.12 thorpej vendor = pcic_vendor(&sc->handle[2]);
242 1.1.2.12 thorpej
243 1.1.2.12 thorpej printf("%s: controller 1 (%s) has ", sc->dev.dv_xname,
244 1.1.2.12 thorpej pcic_vendor_to_string(vendor));
245 1.1.2.12 thorpej
246 1.1.2.12 thorpej if ((sc->handle[2].flags & PCIC_FLAG_SOCKETP) &&
247 1.1.2.12 thorpej (sc->handle[3].flags & PCIC_FLAG_SOCKETP))
248 1.1.2.12 thorpej printf("sockets A and B\n");
249 1.1.2.12 thorpej else if (sc->handle[2].flags & PCIC_FLAG_SOCKETP)
250 1.1.2.12 thorpej printf("socket A only\n");
251 1.1.2.12 thorpej else
252 1.1.2.12 thorpej printf("socket B only\n");
253 1.1.2.12 thorpej
254 1.1.2.12 thorpej if (sc->handle[2].flags & PCIC_FLAG_SOCKETP)
255 1.1.2.12 thorpej sc->handle[2].vendor = vendor;
256 1.1.2.12 thorpej if (sc->handle[3].flags & PCIC_FLAG_SOCKETP)
257 1.1.2.12 thorpej sc->handle[3].vendor = vendor;
258 1.1.2.12 thorpej }
259 1.1.2.1 marc }
260 1.1.2.1 marc
261 1.1.2.1 marc void
262 1.1.2.8 marc pcic_attach_sockets(sc)
263 1.1.2.12 thorpej struct pcic_softc *sc;
264 1.1.2.8 marc {
265 1.1.2.12 thorpej int i;
266 1.1.2.8 marc
267 1.1.2.12 thorpej for (i = 0; i < PCIC_NSLOTS; i++)
268 1.1.2.12 thorpej if (sc->handle[i].flags & PCIC_FLAG_SOCKETP)
269 1.1.2.12 thorpej pcic_attach_socket(&sc->handle[i]);
270 1.1.2.8 marc }
271 1.1.2.8 marc
272 1.1.2.8 marc void
273 1.1.2.1 marc pcic_attach_socket(h)
274 1.1.2.12 thorpej struct pcic_handle *h;
275 1.1.2.1 marc {
276 1.1.2.12 thorpej struct pcmciabus_attach_args paa;
277 1.1.2.1 marc
278 1.1.2.12 thorpej /* initialize the rest of the handle */
279 1.1.2.1 marc
280 1.1.2.12 thorpej h->memalloc = 0;
281 1.1.2.12 thorpej h->ioalloc = 0;
282 1.1.2.12 thorpej h->ih_irq = 0;
283 1.1.2.1 marc
284 1.1.2.12 thorpej /* now, config one pcmcia device per socket */
285 1.1.2.1 marc
286 1.1.2.12 thorpej paa.pct = (pcmcia_chipset_tag_t) h->sc->pct;
287 1.1.2.12 thorpej paa.pch = (pcmcia_chipset_handle_t) h;
288 1.1.2.1 marc
289 1.1.2.12 thorpej h->pcmcia = config_found_sm(&h->sc->dev, &paa, pcic_print,
290 1.1.2.12 thorpej pcic_submatch);
291 1.1.2.1 marc
292 1.1.2.12 thorpej /* if there's actually a pcmcia device attached, initialize the slot */
293 1.1.2.1 marc
294 1.1.2.12 thorpej if (h->pcmcia)
295 1.1.2.12 thorpej pcic_init_socket(h);
296 1.1.2.1 marc }
297 1.1.2.1 marc
298 1.1.2.1 marc void
299 1.1.2.1 marc pcic_init_socket(h)
300 1.1.2.12 thorpej struct pcic_handle *h;
301 1.1.2.1 marc {
302 1.1.2.12 thorpej int reg;
303 1.1.2.1 marc
304 1.1.2.12 thorpej /* set up the card to interrupt on card detect */
305 1.1.2.1 marc
306 1.1.2.12 thorpej pcic_write(h, PCIC_CSC_INTR, (h->sc->irq << PCIC_CSC_INTR_IRQ_SHIFT) |
307 1.1.2.12 thorpej PCIC_CSC_INTR_CD_ENABLE);
308 1.1.2.12 thorpej pcic_write(h, PCIC_INTR, 0);
309 1.1.2.12 thorpej pcic_read(h, PCIC_CSC);
310 1.1.2.12 thorpej
311 1.1.2.12 thorpej /* unsleep the cirrus controller */
312 1.1.2.12 thorpej
313 1.1.2.12 thorpej if ((h->vendor == PCIC_VENDOR_CIRRUS_PD6710) ||
314 1.1.2.12 thorpej (h->vendor == PCIC_VENDOR_CIRRUS_PD672X)) {
315 1.1.2.12 thorpej reg = pcic_read(h, PCIC_CIRRUS_MISC_CTL_2);
316 1.1.2.12 thorpej if (reg & PCIC_CIRRUS_MISC_CTL_2_SUSPEND) {
317 1.1.2.12 thorpej DPRINTF(("%s: socket %02x was suspended\n",
318 1.1.2.12 thorpej h->sc->dev.dv_xname, h->sock));
319 1.1.2.12 thorpej reg &= ~PCIC_CIRRUS_MISC_CTL_2_SUSPEND;
320 1.1.2.12 thorpej pcic_write(h, PCIC_CIRRUS_MISC_CTL_2, reg);
321 1.1.2.12 thorpej }
322 1.1.2.1 marc }
323 1.1.2.12 thorpej /* if there's a card there, then attach it. */
324 1.1.2.1 marc
325 1.1.2.12 thorpej reg = pcic_read(h, PCIC_IF_STATUS);
326 1.1.2.1 marc
327 1.1.2.12 thorpej if ((reg & PCIC_IF_STATUS_CARDDETECT_MASK) ==
328 1.1.2.12 thorpej PCIC_IF_STATUS_CARDDETECT_PRESENT)
329 1.1.2.12 thorpej pcic_attach_card(h);
330 1.1.2.1 marc }
331 1.1.2.1 marc
332 1.1.2.1 marc int
333 1.1.2.1 marc #ifdef __BROKEN_INDIRECT_CONFIG
334 1.1.2.1 marc pcic_submatch(parent, match, aux)
335 1.1.2.1 marc #else
336 1.1.2.1 marc pcic_submatch(parent, cf, aux)
337 1.1.2.1 marc #endif
338 1.1.2.12 thorpej struct device *parent;
339 1.1.2.1 marc #ifdef __BROKEN_INDIRECT_CONFIG
340 1.1.2.12 thorpej void *match;
341 1.1.2.1 marc #else
342 1.1.2.12 thorpej struct cfdata *cf;
343 1.1.2.1 marc #endif
344 1.1.2.12 thorpej void *aux;
345 1.1.2.1 marc {
346 1.1.2.1 marc #ifdef __BROKEN_INDIRECT_CONFIG
347 1.1.2.12 thorpej struct cfdata *cf = match;
348 1.1.2.1 marc #endif
349 1.1.2.1 marc
350 1.1.2.12 thorpej struct pcmciabus_attach_args *paa =
351 1.1.2.12 thorpej (struct pcmciabus_attach_args *) aux;
352 1.1.2.12 thorpej struct pcic_handle *h = (struct pcic_handle *) paa->pch;
353 1.1.2.12 thorpej
354 1.1.2.12 thorpej switch (h->sock) {
355 1.1.2.12 thorpej case C0SA:
356 1.1.2.12 thorpej if (cf->cf_loc[0] != -1 && cf->cf_loc[0] != 0)
357 1.1.2.12 thorpej return 0;
358 1.1.2.12 thorpej if (cf->cf_loc[1] != -1 && cf->cf_loc[1] != 0)
359 1.1.2.12 thorpej return 0;
360 1.1.2.12 thorpej
361 1.1.2.12 thorpej break;
362 1.1.2.12 thorpej case C0SB:
363 1.1.2.12 thorpej if (cf->cf_loc[0] != -1 && cf->cf_loc[0] != 0)
364 1.1.2.12 thorpej return 0;
365 1.1.2.12 thorpej if (cf->cf_loc[1] != -1 && cf->cf_loc[1] != 1)
366 1.1.2.12 thorpej return 0;
367 1.1.2.12 thorpej
368 1.1.2.12 thorpej break;
369 1.1.2.12 thorpej case C1SA:
370 1.1.2.12 thorpej if (cf->cf_loc[0] != -1 && cf->cf_loc[0] != 1)
371 1.1.2.12 thorpej return 0;
372 1.1.2.12 thorpej if (cf->cf_loc[1] != -1 && cf->cf_loc[1] != 0)
373 1.1.2.12 thorpej return 0;
374 1.1.2.12 thorpej
375 1.1.2.12 thorpej break;
376 1.1.2.12 thorpej case C1SB:
377 1.1.2.12 thorpej if (cf->cf_loc[0] != -1 && cf->cf_loc[0] != 1)
378 1.1.2.12 thorpej return 0;
379 1.1.2.12 thorpej if (cf->cf_loc[1] != -1 && cf->cf_loc[1] != 1)
380 1.1.2.12 thorpej return 0;
381 1.1.2.12 thorpej
382 1.1.2.12 thorpej break;
383 1.1.2.12 thorpej default:
384 1.1.2.12 thorpej panic("unknown pcic socket");
385 1.1.2.12 thorpej }
386 1.1.2.1 marc
387 1.1.2.12 thorpej return ((*cf->cf_attach->ca_match)(parent, cf, aux));
388 1.1.2.1 marc }
389 1.1.2.1 marc
390 1.1.2.1 marc int
391 1.1.2.1 marc pcic_print(arg, pnp)
392 1.1.2.12 thorpej void *arg;
393 1.1.2.12 thorpej const char *pnp;
394 1.1.2.1 marc {
395 1.1.2.12 thorpej struct pcmciabus_attach_args *paa =
396 1.1.2.12 thorpej (struct pcmciabus_attach_args *) arg;
397 1.1.2.12 thorpej struct pcic_handle *h = (struct pcic_handle *) paa->pch;
398 1.1.2.12 thorpej
399 1.1.2.12 thorpej /* Only "pcmcia"s can attach to "pcic"s... easy. */
400 1.1.2.12 thorpej if (pnp)
401 1.1.2.12 thorpej printf("pcmcia at %s", pnp);
402 1.1.2.12 thorpej
403 1.1.2.12 thorpej switch (h->sock) {
404 1.1.2.12 thorpej case C0SA:
405 1.1.2.12 thorpej printf(" controller 0 socket 0");
406 1.1.2.12 thorpej break;
407 1.1.2.12 thorpej case C0SB:
408 1.1.2.12 thorpej printf(" controller 0 socket 1");
409 1.1.2.12 thorpej break;
410 1.1.2.12 thorpej case C1SA:
411 1.1.2.12 thorpej printf(" controller 1 socket 0");
412 1.1.2.12 thorpej break;
413 1.1.2.12 thorpej case C1SB:
414 1.1.2.12 thorpej printf(" controller 1 socket 1");
415 1.1.2.12 thorpej break;
416 1.1.2.12 thorpej default:
417 1.1.2.12 thorpej panic("unknown pcic socket");
418 1.1.2.12 thorpej }
419 1.1.2.1 marc
420 1.1.2.12 thorpej return (UNCONF);
421 1.1.2.1 marc }
422 1.1.2.1 marc
423 1.1.2.1 marc int
424 1.1.2.1 marc pcic_intr(arg)
425 1.1.2.12 thorpej void *arg;
426 1.1.2.1 marc {
427 1.1.2.12 thorpej struct pcic_softc *sc = (struct pcic_softc *) arg;
428 1.1.2.12 thorpej int i, ret = 0;
429 1.1.2.1 marc
430 1.1.2.12 thorpej DPRINTF(("%s: intr\n", sc->dev.dv_xname));
431 1.1.2.1 marc
432 1.1.2.12 thorpej for (i = 0; i < PCIC_NSLOTS; i++)
433 1.1.2.12 thorpej if (sc->handle[i].flags & PCIC_FLAG_SOCKETP)
434 1.1.2.12 thorpej ret += pcic_intr_socket(&sc->handle[i]);
435 1.1.2.1 marc
436 1.1.2.12 thorpej return (ret ? 1 : 0);
437 1.1.2.1 marc }
438 1.1.2.1 marc
439 1.1.2.1 marc int
440 1.1.2.1 marc pcic_intr_socket(h)
441 1.1.2.12 thorpej struct pcic_handle *h;
442 1.1.2.1 marc {
443 1.1.2.12 thorpej int cscreg;
444 1.1.2.1 marc
445 1.1.2.12 thorpej cscreg = pcic_read(h, PCIC_CSC);
446 1.1.2.1 marc
447 1.1.2.12 thorpej cscreg &= (PCIC_CSC_GPI |
448 1.1.2.12 thorpej PCIC_CSC_CD |
449 1.1.2.12 thorpej PCIC_CSC_READY |
450 1.1.2.12 thorpej PCIC_CSC_BATTWARN |
451 1.1.2.12 thorpej PCIC_CSC_BATTDEAD);
452 1.1.2.1 marc
453 1.1.2.12 thorpej if (cscreg & PCIC_CSC_GPI) {
454 1.1.2.12 thorpej DPRINTF(("%s: %02x GPI\n", h->sc->dev.dv_xname, h->sock));
455 1.1.2.12 thorpej }
456 1.1.2.12 thorpej if (cscreg & PCIC_CSC_CD) {
457 1.1.2.12 thorpej int statreg;
458 1.1.2.1 marc
459 1.1.2.12 thorpej statreg = pcic_read(h, PCIC_IF_STATUS);
460 1.1.2.1 marc
461 1.1.2.12 thorpej DPRINTF(("%s: %02x CD %x\n", h->sc->dev.dv_xname, h->sock,
462 1.1.2.12 thorpej statreg));
463 1.1.2.8 marc
464 1.1.2.12 thorpej /*
465 1.1.2.12 thorpej * XXX This should probably schedule something to happen
466 1.1.2.12 thorpej * after the interrupt handler completes
467 1.1.2.12 thorpej */
468 1.1.2.12 thorpej
469 1.1.2.12 thorpej if ((statreg & PCIC_IF_STATUS_CARDDETECT_MASK) ==
470 1.1.2.12 thorpej PCIC_IF_STATUS_CARDDETECT_PRESENT) {
471 1.1.2.12 thorpej if (!(h->flags & PCIC_FLAG_CARDP))
472 1.1.2.12 thorpej pcic_attach_card(h);
473 1.1.2.12 thorpej } else {
474 1.1.2.12 thorpej if (h->flags & PCIC_FLAG_CARDP)
475 1.1.2.12 thorpej pcic_detach_card(h);
476 1.1.2.12 thorpej }
477 1.1.2.1 marc }
478 1.1.2.12 thorpej if (cscreg & PCIC_CSC_READY) {
479 1.1.2.12 thorpej DPRINTF(("%s: %02x READY\n", h->sc->dev.dv_xname, h->sock));
480 1.1.2.12 thorpej /* shouldn't happen */
481 1.1.2.12 thorpej }
482 1.1.2.12 thorpej if (cscreg & PCIC_CSC_BATTWARN) {
483 1.1.2.12 thorpej DPRINTF(("%s: %02x BATTWARN\n", h->sc->dev.dv_xname, h->sock));
484 1.1.2.12 thorpej }
485 1.1.2.12 thorpej if (cscreg & PCIC_CSC_BATTDEAD) {
486 1.1.2.12 thorpej DPRINTF(("%s: %02x BATTDEAD\n", h->sc->dev.dv_xname, h->sock));
487 1.1.2.12 thorpej }
488 1.1.2.12 thorpej return (cscreg ? 1 : 0);
489 1.1.2.1 marc }
490 1.1.2.1 marc
491 1.1.2.1 marc void
492 1.1.2.1 marc pcic_attach_card(h)
493 1.1.2.12 thorpej struct pcic_handle *h;
494 1.1.2.1 marc {
495 1.1.2.12 thorpej if (h->flags & PCIC_FLAG_CARDP)
496 1.1.2.12 thorpej panic("pcic_attach_card: already attached");
497 1.1.2.1 marc
498 1.1.2.12 thorpej /* call the MI attach function */
499 1.1.2.1 marc
500 1.1.2.12 thorpej pcmcia_card_attach(h->pcmcia);
501 1.1.2.1 marc
502 1.1.2.12 thorpej h->flags |= PCIC_FLAG_CARDP;
503 1.1.2.1 marc }
504 1.1.2.1 marc
505 1.1.2.1 marc void
506 1.1.2.1 marc pcic_detach_card(h)
507 1.1.2.12 thorpej struct pcic_handle *h;
508 1.1.2.1 marc {
509 1.1.2.12 thorpej if (!(h->flags & PCIC_FLAG_CARDP))
510 1.1.2.12 thorpej panic("pcic_attach_card: already detached");
511 1.1.2.1 marc
512 1.1.2.12 thorpej h->flags &= ~PCIC_FLAG_CARDP;
513 1.1.2.1 marc
514 1.1.2.12 thorpej /* call the MI attach function */
515 1.1.2.1 marc
516 1.1.2.12 thorpej pcmcia_card_detach(h->pcmcia);
517 1.1.2.1 marc
518 1.1.2.12 thorpej /* disable card detect resume and configuration reset */
519 1.1.2.1 marc
520 1.1.2.12 thorpej /* power down the socket */
521 1.1.2.1 marc
522 1.1.2.12 thorpej pcic_write(h, PCIC_PWRCTL, 0);
523 1.1.2.1 marc
524 1.1.2.12 thorpej /* reset the card */
525 1.1.2.1 marc
526 1.1.2.12 thorpej pcic_write(h, PCIC_INTR, 0);
527 1.1.2.1 marc }
528 1.1.2.1 marc
529 1.1.2.12 thorpej int
530 1.1.2.12 thorpej pcic_chip_mem_alloc(pch, size, pcmhp)
531 1.1.2.12 thorpej pcmcia_chipset_handle_t pch;
532 1.1.2.12 thorpej bus_size_t size;
533 1.1.2.12 thorpej struct pcmcia_mem_handle *pcmhp;
534 1.1.2.1 marc {
535 1.1.2.12 thorpej struct pcic_handle *h = (struct pcic_handle *) pch;
536 1.1.2.12 thorpej bus_space_handle_t memh;
537 1.1.2.12 thorpej bus_addr_t addr;
538 1.1.2.12 thorpej bus_size_t sizepg;
539 1.1.2.12 thorpej int i, mask, mhandle;
540 1.1.2.1 marc
541 1.1.2.12 thorpej /* out of sc->memh, allocate as many pages as necessary */
542 1.1.2.1 marc
543 1.1.2.12 thorpej /* convert size to PCIC pages */
544 1.1.2.12 thorpej sizepg = (size + (PCIC_MEM_ALIGN - 1)) / PCIC_MEM_ALIGN;
545 1.1.2.1 marc
546 1.1.2.12 thorpej mask = (1 << sizepg) - 1;
547 1.1.2.1 marc
548 1.1.2.12 thorpej addr = 0; /* XXX gcc -Wuninitialized */
549 1.1.2.12 thorpej mhandle = 0; /* XXX gcc -Wuninitialized */
550 1.1.2.8 marc
551 1.1.2.12 thorpej for (i = 0; i < (PCIC_MEM_PAGES + 1 - sizepg); i++) {
552 1.1.2.12 thorpej if ((h->sc->subregionmask & (mask << i)) == (mask << i)) {
553 1.1.2.12 thorpej if (bus_space_subregion(h->sc->memt, h->sc->memh,
554 1.1.2.12 thorpej i * PCIC_MEM_PAGESIZE,
555 1.1.2.12 thorpej sizepg * PCIC_MEM_PAGESIZE, &memh))
556 1.1.2.12 thorpej return (1);
557 1.1.2.12 thorpej mhandle = mask << i;
558 1.1.2.12 thorpej addr = h->sc->membase + (i * PCIC_MEM_PAGESIZE);
559 1.1.2.12 thorpej h->sc->subregionmask &= ~(mhandle);
560 1.1.2.12 thorpej break;
561 1.1.2.12 thorpej }
562 1.1.2.1 marc }
563 1.1.2.1 marc
564 1.1.2.12 thorpej if (i == (PCIC_MEM_PAGES + 1 - size))
565 1.1.2.12 thorpej return (1);
566 1.1.2.1 marc
567 1.1.2.12 thorpej DPRINTF(("pcic_chip_mem_alloc bus addr 0x%lx+0x%lx\n", (u_long) addr,
568 1.1.2.12 thorpej (u_long) size));
569 1.1.2.4 thorpej
570 1.1.2.12 thorpej pcmhp->memt = h->sc->memt;
571 1.1.2.12 thorpej pcmhp->memh = memh;
572 1.1.2.12 thorpej pcmhp->addr = addr;
573 1.1.2.12 thorpej pcmhp->size = size;
574 1.1.2.12 thorpej pcmhp->mhandle = mhandle;
575 1.1.2.12 thorpej pcmhp->realsize = sizepg * PCIC_MEM_PAGESIZE;
576 1.1.2.1 marc
577 1.1.2.12 thorpej return (0);
578 1.1.2.1 marc }
579 1.1.2.1 marc
580 1.1.2.12 thorpej void
581 1.1.2.12 thorpej pcic_chip_mem_free(pch, pcmhp)
582 1.1.2.12 thorpej pcmcia_chipset_handle_t pch;
583 1.1.2.12 thorpej struct pcmcia_mem_handle *pcmhp;
584 1.1.2.1 marc {
585 1.1.2.12 thorpej struct pcic_handle *h = (struct pcic_handle *) pch;
586 1.1.2.1 marc
587 1.1.2.12 thorpej h->sc->subregionmask |= pcmhp->mhandle;
588 1.1.2.1 marc }
589 1.1.2.1 marc
590 1.1.2.1 marc static struct mem_map_index_st {
591 1.1.2.12 thorpej int sysmem_start_lsb;
592 1.1.2.12 thorpej int sysmem_start_msb;
593 1.1.2.12 thorpej int sysmem_stop_lsb;
594 1.1.2.12 thorpej int sysmem_stop_msb;
595 1.1.2.12 thorpej int cardmem_lsb;
596 1.1.2.12 thorpej int cardmem_msb;
597 1.1.2.12 thorpej int memenable;
598 1.1.2.1 marc } mem_map_index[] = {
599 1.1.2.12 thorpej {
600 1.1.2.12 thorpej PCIC_SYSMEM_ADDR0_START_LSB,
601 1.1.2.12 thorpej PCIC_SYSMEM_ADDR0_START_MSB,
602 1.1.2.12 thorpej PCIC_SYSMEM_ADDR0_STOP_LSB,
603 1.1.2.12 thorpej PCIC_SYSMEM_ADDR0_STOP_MSB,
604 1.1.2.12 thorpej PCIC_CARDMEM_ADDR0_LSB,
605 1.1.2.12 thorpej PCIC_CARDMEM_ADDR0_MSB,
606 1.1.2.12 thorpej PCIC_ADDRWIN_ENABLE_MEM0,
607 1.1.2.12 thorpej },
608 1.1.2.12 thorpej {
609 1.1.2.12 thorpej PCIC_SYSMEM_ADDR1_START_LSB,
610 1.1.2.12 thorpej PCIC_SYSMEM_ADDR1_START_MSB,
611 1.1.2.12 thorpej PCIC_SYSMEM_ADDR1_STOP_LSB,
612 1.1.2.12 thorpej PCIC_SYSMEM_ADDR1_STOP_MSB,
613 1.1.2.12 thorpej PCIC_CARDMEM_ADDR1_LSB,
614 1.1.2.12 thorpej PCIC_CARDMEM_ADDR1_MSB,
615 1.1.2.12 thorpej PCIC_ADDRWIN_ENABLE_MEM1,
616 1.1.2.12 thorpej },
617 1.1.2.12 thorpej {
618 1.1.2.12 thorpej PCIC_SYSMEM_ADDR2_START_LSB,
619 1.1.2.12 thorpej PCIC_SYSMEM_ADDR2_START_MSB,
620 1.1.2.12 thorpej PCIC_SYSMEM_ADDR2_STOP_LSB,
621 1.1.2.12 thorpej PCIC_SYSMEM_ADDR2_STOP_MSB,
622 1.1.2.12 thorpej PCIC_CARDMEM_ADDR2_LSB,
623 1.1.2.12 thorpej PCIC_CARDMEM_ADDR2_MSB,
624 1.1.2.12 thorpej PCIC_ADDRWIN_ENABLE_MEM2,
625 1.1.2.12 thorpej },
626 1.1.2.12 thorpej {
627 1.1.2.12 thorpej PCIC_SYSMEM_ADDR3_START_LSB,
628 1.1.2.12 thorpej PCIC_SYSMEM_ADDR3_START_MSB,
629 1.1.2.12 thorpej PCIC_SYSMEM_ADDR3_STOP_LSB,
630 1.1.2.12 thorpej PCIC_SYSMEM_ADDR3_STOP_MSB,
631 1.1.2.12 thorpej PCIC_CARDMEM_ADDR3_LSB,
632 1.1.2.12 thorpej PCIC_CARDMEM_ADDR3_MSB,
633 1.1.2.12 thorpej PCIC_ADDRWIN_ENABLE_MEM3,
634 1.1.2.12 thorpej },
635 1.1.2.12 thorpej {
636 1.1.2.12 thorpej PCIC_SYSMEM_ADDR4_START_LSB,
637 1.1.2.12 thorpej PCIC_SYSMEM_ADDR4_START_MSB,
638 1.1.2.12 thorpej PCIC_SYSMEM_ADDR4_STOP_LSB,
639 1.1.2.12 thorpej PCIC_SYSMEM_ADDR4_STOP_MSB,
640 1.1.2.12 thorpej PCIC_CARDMEM_ADDR4_LSB,
641 1.1.2.12 thorpej PCIC_CARDMEM_ADDR4_MSB,
642 1.1.2.12 thorpej PCIC_ADDRWIN_ENABLE_MEM4,
643 1.1.2.12 thorpej },
644 1.1.2.1 marc };
645 1.1.2.1 marc
646 1.1.2.12 thorpej void
647 1.1.2.12 thorpej pcic_chip_do_mem_map(h, win)
648 1.1.2.12 thorpej struct pcic_handle *h;
649 1.1.2.12 thorpej int win;
650 1.1.2.12 thorpej {
651 1.1.2.12 thorpej int reg;
652 1.1.2.12 thorpej
653 1.1.2.12 thorpej pcic_write(h, mem_map_index[win].sysmem_start_lsb,
654 1.1.2.12 thorpej (h->mem[win].addr >> PCIC_SYSMEM_ADDRX_SHIFT) & 0xff);
655 1.1.2.12 thorpej pcic_write(h, mem_map_index[win].sysmem_start_msb,
656 1.1.2.12 thorpej ((h->mem[win].addr >> (PCIC_SYSMEM_ADDRX_SHIFT + 8)) &
657 1.1.2.12 thorpej PCIC_SYSMEM_ADDRX_START_MSB_ADDR_MASK));
658 1.1.2.10 marc
659 1.1.2.10 marc #if 0
660 1.1.2.12 thorpej /* XXX do I want 16 bit all the time? */
661 1.1.2.12 thorpej PCIC_SYSMEM_ADDRX_START_MSB_DATASIZE_16BIT;
662 1.1.2.10 marc #endif
663 1.1.2.10 marc
664 1.1.2.12 thorpej pcic_write(h, mem_map_index[win].sysmem_stop_lsb,
665 1.1.2.12 thorpej ((h->mem[win].addr + h->mem[win].size) >>
666 1.1.2.12 thorpej PCIC_SYSMEM_ADDRX_SHIFT) & 0xff);
667 1.1.2.12 thorpej pcic_write(h, mem_map_index[win].sysmem_stop_msb,
668 1.1.2.12 thorpej (((h->mem[win].addr + h->mem[win].size) >>
669 1.1.2.12 thorpej (PCIC_SYSMEM_ADDRX_SHIFT + 8)) &
670 1.1.2.12 thorpej PCIC_SYSMEM_ADDRX_STOP_MSB_ADDR_MASK) |
671 1.1.2.12 thorpej PCIC_SYSMEM_ADDRX_STOP_MSB_WAIT2);
672 1.1.2.12 thorpej
673 1.1.2.12 thorpej pcic_write(h, mem_map_index[win].cardmem_lsb,
674 1.1.2.12 thorpej (h->mem[win].offset >> PCIC_CARDMEM_ADDRX_SHIFT) & 0xff);
675 1.1.2.12 thorpej pcic_write(h, mem_map_index[win].cardmem_msb,
676 1.1.2.12 thorpej ((h->mem[win].offset >> (PCIC_CARDMEM_ADDRX_SHIFT + 8)) &
677 1.1.2.12 thorpej PCIC_CARDMEM_ADDRX_MSB_ADDR_MASK) |
678 1.1.2.12 thorpej ((h->mem[win].kind == PCMCIA_MEM_ATTR) ?
679 1.1.2.12 thorpej PCIC_CARDMEM_ADDRX_MSB_REGACTIVE_ATTR : 0));
680 1.1.2.12 thorpej
681 1.1.2.12 thorpej reg = pcic_read(h, PCIC_ADDRWIN_ENABLE);
682 1.1.2.12 thorpej reg |= (mem_map_index[win].memenable | PCIC_ADDRWIN_ENABLE_MEMCS16);
683 1.1.2.12 thorpej pcic_write(h, PCIC_ADDRWIN_ENABLE, reg);
684 1.1.2.10 marc
685 1.1.2.10 marc #ifdef PCICDEBUG
686 1.1.2.12 thorpej {
687 1.1.2.12 thorpej int r1, r2, r3, r4, r5, r6;
688 1.1.2.12 thorpej
689 1.1.2.12 thorpej r1 = pcic_read(h, mem_map_index[win].sysmem_start_msb);
690 1.1.2.12 thorpej r2 = pcic_read(h, mem_map_index[win].sysmem_start_lsb);
691 1.1.2.12 thorpej r3 = pcic_read(h, mem_map_index[win].sysmem_stop_msb);
692 1.1.2.12 thorpej r4 = pcic_read(h, mem_map_index[win].sysmem_stop_lsb);
693 1.1.2.12 thorpej r5 = pcic_read(h, mem_map_index[win].cardmem_msb);
694 1.1.2.12 thorpej r6 = pcic_read(h, mem_map_index[win].cardmem_lsb);
695 1.1.2.10 marc
696 1.1.2.12 thorpej DPRINTF(("pcic_chip_do_mem_map window %d: %02x%02x %02x%02x "
697 1.1.2.12 thorpej "%02x%02x\n", win, r1, r2, r3, r4, r5, r6));
698 1.1.2.12 thorpej }
699 1.1.2.10 marc #endif
700 1.1.2.10 marc }
701 1.1.2.10 marc
702 1.1.2.12 thorpej int
703 1.1.2.12 thorpej pcic_chip_mem_map(pch, kind, card_addr, size, pcmhp, offsetp, windowp)
704 1.1.2.12 thorpej pcmcia_chipset_handle_t pch;
705 1.1.2.12 thorpej int kind;
706 1.1.2.12 thorpej bus_addr_t card_addr;
707 1.1.2.12 thorpej bus_size_t size;
708 1.1.2.12 thorpej struct pcmcia_mem_handle *pcmhp;
709 1.1.2.12 thorpej bus_addr_t *offsetp;
710 1.1.2.12 thorpej int *windowp;
711 1.1.2.12 thorpej {
712 1.1.2.12 thorpej struct pcic_handle *h = (struct pcic_handle *) pch;
713 1.1.2.12 thorpej bus_addr_t busaddr;
714 1.1.2.12 thorpej long card_offset;
715 1.1.2.12 thorpej int i, win;
716 1.1.2.12 thorpej
717 1.1.2.12 thorpej win = -1;
718 1.1.2.12 thorpej for (i = 0; i < (sizeof(mem_map_index) / sizeof(mem_map_index[0]));
719 1.1.2.12 thorpej i++) {
720 1.1.2.12 thorpej if ((h->memalloc & (1 << i)) == 0) {
721 1.1.2.12 thorpej win = i;
722 1.1.2.12 thorpej h->memalloc |= (1 << i);
723 1.1.2.12 thorpej break;
724 1.1.2.12 thorpej }
725 1.1.2.1 marc }
726 1.1.2.1 marc
727 1.1.2.12 thorpej if (win == -1)
728 1.1.2.12 thorpej return (1);
729 1.1.2.1 marc
730 1.1.2.12 thorpej *windowp = win;
731 1.1.2.1 marc
732 1.1.2.12 thorpej /* XXX this is pretty gross */
733 1.1.2.1 marc
734 1.1.2.12 thorpej if (h->sc->memt != pcmhp->memt)
735 1.1.2.12 thorpej panic("pcic_chip_mem_map memt is bogus");
736 1.1.2.1 marc
737 1.1.2.12 thorpej busaddr = pcmhp->addr;
738 1.1.2.1 marc
739 1.1.2.12 thorpej /*
740 1.1.2.12 thorpej * compute the address offset to the pcmcia address space for the
741 1.1.2.12 thorpej * pcic. this is intentionally signed. The masks and shifts below
742 1.1.2.12 thorpej * will cause TRT to happen in the pcic registers. Deal with making
743 1.1.2.12 thorpej * sure the address is aligned, and return the alignment offset.
744 1.1.2.12 thorpej */
745 1.1.2.1 marc
746 1.1.2.12 thorpej *offsetp = card_addr % PCIC_MEM_ALIGN;
747 1.1.2.12 thorpej card_addr -= *offsetp;
748 1.1.2.1 marc
749 1.1.2.12 thorpej DPRINTF(("pcic_chip_mem_map window %d bus %lx+%lx+%lx at card addr "
750 1.1.2.12 thorpej "%lx\n", win, (u_long) busaddr, (u_long) * offsetp, (u_long) size,
751 1.1.2.12 thorpej (u_long) card_addr));
752 1.1.2.1 marc
753 1.1.2.12 thorpej /*
754 1.1.2.12 thorpej * include the offset in the size, and decrement size by one, since
755 1.1.2.12 thorpej * the hw wants start/stop
756 1.1.2.12 thorpej */
757 1.1.2.12 thorpej size += *offsetp - 1;
758 1.1.2.1 marc
759 1.1.2.12 thorpej card_offset = (((long) card_addr) - ((long) busaddr));
760 1.1.2.1 marc
761 1.1.2.12 thorpej h->mem[win].addr = busaddr;
762 1.1.2.12 thorpej h->mem[win].size = size;
763 1.1.2.12 thorpej h->mem[win].offset = card_offset;
764 1.1.2.12 thorpej h->mem[win].kind = kind;
765 1.1.2.1 marc
766 1.1.2.12 thorpej pcic_chip_do_mem_map(h, win);
767 1.1.2.1 marc
768 1.1.2.12 thorpej return (0);
769 1.1.2.1 marc }
770 1.1.2.1 marc
771 1.1.2.12 thorpej void
772 1.1.2.12 thorpej pcic_chip_mem_unmap(pch, window)
773 1.1.2.12 thorpej pcmcia_chipset_handle_t pch;
774 1.1.2.12 thorpej int window;
775 1.1.2.1 marc {
776 1.1.2.12 thorpej struct pcic_handle *h = (struct pcic_handle *) pch;
777 1.1.2.12 thorpej int reg;
778 1.1.2.12 thorpej
779 1.1.2.12 thorpej if (window >= (sizeof(mem_map_index) / sizeof(mem_map_index[0])))
780 1.1.2.12 thorpej panic("pcic_chip_mem_unmap: window out of range");
781 1.1.2.1 marc
782 1.1.2.12 thorpej reg = pcic_read(h, PCIC_ADDRWIN_ENABLE);
783 1.1.2.12 thorpej reg &= ~mem_map_index[window].memenable;
784 1.1.2.12 thorpej pcic_write(h, PCIC_ADDRWIN_ENABLE, reg);
785 1.1.2.1 marc
786 1.1.2.12 thorpej h->memalloc &= ~(1 << window);
787 1.1.2.1 marc }
788 1.1.2.1 marc
789 1.1.2.12 thorpej int
790 1.1.2.12 thorpej pcic_chip_io_alloc(pch, start, size, align, pcihp)
791 1.1.2.12 thorpej pcmcia_chipset_handle_t pch;
792 1.1.2.12 thorpej bus_addr_t start;
793 1.1.2.12 thorpej bus_size_t size;
794 1.1.2.12 thorpej bus_size_t align;
795 1.1.2.12 thorpej struct pcmcia_io_handle *pcihp;
796 1.1.2.1 marc {
797 1.1.2.12 thorpej struct pcic_handle *h = (struct pcic_handle *) pch;
798 1.1.2.12 thorpej bus_space_tag_t iot;
799 1.1.2.12 thorpej bus_space_handle_t ioh;
800 1.1.2.12 thorpej bus_addr_t ioaddr;
801 1.1.2.12 thorpej int flags = 0;
802 1.1.2.1 marc
803 1.1.2.12 thorpej /*
804 1.1.2.12 thorpej * Allocate some arbitrary I/O space.
805 1.1.2.12 thorpej */
806 1.1.2.1 marc
807 1.1.2.12 thorpej iot = h->sc->iot;
808 1.1.2.4 thorpej
809 1.1.2.12 thorpej if (start) {
810 1.1.2.12 thorpej ioaddr = start;
811 1.1.2.12 thorpej if (bus_space_map(iot, start, size, 0, &ioh))
812 1.1.2.12 thorpej return (1);
813 1.1.2.12 thorpej DPRINTF(("pcic_chip_io_alloc map port %lx+%lx\n",
814 1.1.2.12 thorpej (u_long) ioaddr, (u_long) size));
815 1.1.2.12 thorpej } else {
816 1.1.2.12 thorpej flags |= PCMCIA_IO_ALLOCATED;
817 1.1.2.12 thorpej if (bus_space_alloc(iot, h->sc->iobase,
818 1.1.2.12 thorpej h->sc->iobase + h->sc->iosize, size, align, 0, 0,
819 1.1.2.12 thorpej &ioaddr, &ioh))
820 1.1.2.12 thorpej return (1);
821 1.1.2.12 thorpej DPRINTF(("pcic_chip_io_alloc alloc port %lx+%lx\n",
822 1.1.2.12 thorpej (u_long) ioaddr, (u_long) size));
823 1.1.2.12 thorpej }
824 1.1.2.1 marc
825 1.1.2.12 thorpej pcihp->iot = iot;
826 1.1.2.12 thorpej pcihp->ioh = ioh;
827 1.1.2.12 thorpej pcihp->addr = ioaddr;
828 1.1.2.12 thorpej pcihp->size = size;
829 1.1.2.12 thorpej pcihp->flags = flags;
830 1.1.2.12 thorpej
831 1.1.2.12 thorpej return (0);
832 1.1.2.12 thorpej }
833 1.1.2.12 thorpej
834 1.1.2.12 thorpej void
835 1.1.2.12 thorpej pcic_chip_io_free(pch, pcihp)
836 1.1.2.12 thorpej pcmcia_chipset_handle_t pch;
837 1.1.2.12 thorpej struct pcmcia_io_handle *pcihp;
838 1.1.2.12 thorpej {
839 1.1.2.12 thorpej bus_space_tag_t iot = pcihp->iot;
840 1.1.2.12 thorpej bus_space_handle_t ioh = pcihp->ioh;
841 1.1.2.12 thorpej bus_size_t size = pcihp->size;
842 1.1.2.4 thorpej
843 1.1.2.12 thorpej if (pcihp->flags & PCMCIA_IO_ALLOCATED)
844 1.1.2.12 thorpej bus_space_free(iot, ioh, size);
845 1.1.2.12 thorpej else
846 1.1.2.12 thorpej bus_space_unmap(iot, ioh, size);
847 1.1.2.1 marc }
848 1.1.2.1 marc
849 1.1.2.1 marc
850 1.1.2.1 marc static struct io_map_index_st {
851 1.1.2.12 thorpej int start_lsb;
852 1.1.2.12 thorpej int start_msb;
853 1.1.2.12 thorpej int stop_lsb;
854 1.1.2.12 thorpej int stop_msb;
855 1.1.2.12 thorpej int ioenable;
856 1.1.2.12 thorpej int ioctlmask;
857 1.1.2.13 enami int ioctlbits[3]; /* indexed by PCMCIA_WIDTH_* */
858 1.1.2.12 thorpej } io_map_index[] = {
859 1.1.2.12 thorpej {
860 1.1.2.12 thorpej PCIC_IOADDR0_START_LSB,
861 1.1.2.12 thorpej PCIC_IOADDR0_START_MSB,
862 1.1.2.12 thorpej PCIC_IOADDR0_STOP_LSB,
863 1.1.2.12 thorpej PCIC_IOADDR0_STOP_MSB,
864 1.1.2.12 thorpej PCIC_ADDRWIN_ENABLE_IO0,
865 1.1.2.12 thorpej PCIC_IOCTL_IO0_WAITSTATE | PCIC_IOCTL_IO0_ZEROWAIT |
866 1.1.2.12 thorpej PCIC_IOCTL_IO0_IOCS16SRC_MASK | PCIC_IOCTL_IO0_DATASIZE_MASK,
867 1.1.2.13 enami {
868 1.1.2.13 enami PCIC_IOCTL_IO0_IOCS16SRC_CARD,
869 1.1.2.13 enami PCIC_IOCTL_IO0_IOCS16SRC_DATASIZE
870 1.1.2.13 enami | PCIC_IOCTL_IO0_DATASIZE_8BIT,
871 1.1.2.13 enami PCIC_IOCTL_IO0_IOCS16SRC_DATASIZE
872 1.1.2.13 enami | PCIC_IOCTL_IO0_DATASIZE_16BIT,
873 1.1.2.13 enami },
874 1.1.2.12 thorpej },
875 1.1.2.12 thorpej {
876 1.1.2.12 thorpej PCIC_IOADDR1_START_LSB,
877 1.1.2.12 thorpej PCIC_IOADDR1_START_MSB,
878 1.1.2.12 thorpej PCIC_IOADDR1_STOP_LSB,
879 1.1.2.12 thorpej PCIC_IOADDR1_STOP_MSB,
880 1.1.2.12 thorpej PCIC_ADDRWIN_ENABLE_IO1,
881 1.1.2.12 thorpej PCIC_IOCTL_IO1_WAITSTATE | PCIC_IOCTL_IO1_ZEROWAIT |
882 1.1.2.12 thorpej PCIC_IOCTL_IO1_IOCS16SRC_MASK | PCIC_IOCTL_IO1_DATASIZE_MASK,
883 1.1.2.13 enami {
884 1.1.2.13 enami PCIC_IOCTL_IO1_IOCS16SRC_CARD,
885 1.1.2.13 enami PCIC_IOCTL_IO1_IOCS16SRC_DATASIZE |
886 1.1.2.13 enami PCIC_IOCTL_IO1_DATASIZE_8BIT,
887 1.1.2.13 enami PCIC_IOCTL_IO1_IOCS16SRC_DATASIZE |
888 1.1.2.13 enami PCIC_IOCTL_IO1_DATASIZE_16BIT,
889 1.1.2.13 enami },
890 1.1.2.12 thorpej },
891 1.1.2.1 marc };
892 1.1.2.1 marc
893 1.1.2.12 thorpej void
894 1.1.2.12 thorpej pcic_chip_do_io_map(h, win)
895 1.1.2.12 thorpej struct pcic_handle *h;
896 1.1.2.12 thorpej int win;
897 1.1.2.12 thorpej {
898 1.1.2.12 thorpej int reg;
899 1.1.2.12 thorpej
900 1.1.2.12 thorpej DPRINTF(("pcic_chip_do_io_map win %d addr %lx size %lx width %d\n",
901 1.1.2.12 thorpej win, (long) h->io[win].addr, (long) h->io[win].size,
902 1.1.2.12 thorpej h->io[win].width * 8));
903 1.1.2.12 thorpej
904 1.1.2.12 thorpej pcic_write(h, io_map_index[win].start_lsb, h->io[win].addr & 0xff);
905 1.1.2.12 thorpej pcic_write(h, io_map_index[win].start_msb,
906 1.1.2.12 thorpej (h->io[win].addr >> 8) & 0xff);
907 1.1.2.12 thorpej
908 1.1.2.12 thorpej pcic_write(h, io_map_index[win].stop_lsb,
909 1.1.2.12 thorpej (h->io[win].addr + h->io[win].size - 1) & 0xff);
910 1.1.2.12 thorpej pcic_write(h, io_map_index[win].stop_msb,
911 1.1.2.12 thorpej ((h->io[win].addr + h->io[win].size - 1) >> 8) & 0xff);
912 1.1.2.12 thorpej
913 1.1.2.12 thorpej reg = pcic_read(h, PCIC_IOCTL);
914 1.1.2.12 thorpej reg &= ~io_map_index[win].ioctlmask;
915 1.1.2.13 enami reg |= io_map_index[win].ioctlbits[h->io[win].width];
916 1.1.2.12 thorpej pcic_write(h, PCIC_IOCTL, reg);
917 1.1.2.4 thorpej
918 1.1.2.12 thorpej reg = pcic_read(h, PCIC_ADDRWIN_ENABLE);
919 1.1.2.12 thorpej reg |= io_map_index[win].ioenable;
920 1.1.2.12 thorpej pcic_write(h, PCIC_ADDRWIN_ENABLE, reg);
921 1.1.2.12 thorpej }
922 1.1.2.12 thorpej
923 1.1.2.12 thorpej int
924 1.1.2.12 thorpej pcic_chip_io_map(pch, width, offset, size, pcihp, windowp)
925 1.1.2.12 thorpej pcmcia_chipset_handle_t pch;
926 1.1.2.12 thorpej int width;
927 1.1.2.12 thorpej bus_addr_t offset;
928 1.1.2.12 thorpej bus_size_t size;
929 1.1.2.12 thorpej struct pcmcia_io_handle *pcihp;
930 1.1.2.12 thorpej int *windowp;
931 1.1.2.12 thorpej {
932 1.1.2.12 thorpej struct pcic_handle *h = (struct pcic_handle *) pch;
933 1.1.2.12 thorpej bus_addr_t ioaddr = pcihp->addr + offset;
934 1.1.2.13 enami static char *width_names[] = { "auto", "io8", "io16" };
935 1.1.2.12 thorpej int i, win;
936 1.1.2.12 thorpej
937 1.1.2.12 thorpej /* XXX Sanity check offset/size. */
938 1.1.2.12 thorpej
939 1.1.2.12 thorpej win = -1;
940 1.1.2.12 thorpej for (i = 0; i < (sizeof(io_map_index) / sizeof(io_map_index[0])); i++) {
941 1.1.2.12 thorpej if ((h->ioalloc & (1 << i)) == 0) {
942 1.1.2.12 thorpej win = i;
943 1.1.2.12 thorpej h->ioalloc |= (1 << i);
944 1.1.2.12 thorpej break;
945 1.1.2.12 thorpej }
946 1.1.2.1 marc }
947 1.1.2.1 marc
948 1.1.2.12 thorpej if (win == -1)
949 1.1.2.12 thorpej return (1);
950 1.1.2.1 marc
951 1.1.2.12 thorpej *windowp = win;
952 1.1.2.1 marc
953 1.1.2.12 thorpej /* XXX this is pretty gross */
954 1.1.2.1 marc
955 1.1.2.12 thorpej if (h->sc->iot != pcihp->iot)
956 1.1.2.12 thorpej panic("pcic_chip_io_map iot is bogus");
957 1.1.2.1 marc
958 1.1.2.12 thorpej DPRINTF(("pcic_chip_io_map window %d %s port %lx+%lx\n",
959 1.1.2.13 enami win, width_names[width], (u_long) ioaddr, (u_long) size));
960 1.1.2.1 marc
961 1.1.2.12 thorpej /* XXX wtf is this doing here? */
962 1.1.2.10 marc
963 1.1.2.12 thorpej printf(" port 0x%lx", (u_long) ioaddr);
964 1.1.2.12 thorpej if (size > 1)
965 1.1.2.12 thorpej printf("-0x%lx", (u_long) ioaddr + (u_long) size - 1);
966 1.1.2.6 jtk
967 1.1.2.12 thorpej h->io[win].addr = ioaddr;
968 1.1.2.12 thorpej h->io[win].size = size;
969 1.1.2.12 thorpej h->io[win].width = width;
970 1.1.2.1 marc
971 1.1.2.12 thorpej pcic_chip_do_io_map(h, win);
972 1.1.2.1 marc
973 1.1.2.12 thorpej return (0);
974 1.1.2.1 marc }
975 1.1.2.1 marc
976 1.1.2.12 thorpej void
977 1.1.2.12 thorpej pcic_chip_io_unmap(pch, window)
978 1.1.2.12 thorpej pcmcia_chipset_handle_t pch;
979 1.1.2.12 thorpej int window;
980 1.1.2.1 marc {
981 1.1.2.12 thorpej struct pcic_handle *h = (struct pcic_handle *) pch;
982 1.1.2.12 thorpej int reg;
983 1.1.2.12 thorpej
984 1.1.2.12 thorpej if (window >= (sizeof(io_map_index) / sizeof(io_map_index[0])))
985 1.1.2.12 thorpej panic("pcic_chip_io_unmap: window out of range");
986 1.1.2.1 marc
987 1.1.2.12 thorpej reg = pcic_read(h, PCIC_ADDRWIN_ENABLE);
988 1.1.2.12 thorpej reg &= ~io_map_index[window].ioenable;
989 1.1.2.12 thorpej pcic_write(h, PCIC_ADDRWIN_ENABLE, reg);
990 1.1.2.1 marc
991 1.1.2.12 thorpej h->ioalloc &= ~(1 << window);
992 1.1.2.7 thorpej }
993 1.1.2.7 thorpej
994 1.1.2.7 thorpej void
995 1.1.2.7 thorpej pcic_chip_socket_enable(pch)
996 1.1.2.12 thorpej pcmcia_chipset_handle_t pch;
997 1.1.2.7 thorpej {
998 1.1.2.12 thorpej struct pcic_handle *h = (struct pcic_handle *) pch;
999 1.1.2.12 thorpej int cardtype, reg, win;
1000 1.1.2.10 marc
1001 1.1.2.12 thorpej /* this bit is mostly stolen from pcic_attach_card */
1002 1.1.2.10 marc
1003 1.1.2.12 thorpej /* power down the socket to reset it, clear the card reset pin */
1004 1.1.2.10 marc
1005 1.1.2.12 thorpej pcic_write(h, PCIC_PWRCTL, 0);
1006 1.1.2.10 marc
1007 1.1.2.12 thorpej /* power up the socket */
1008 1.1.2.10 marc
1009 1.1.2.12 thorpej pcic_write(h, PCIC_PWRCTL, PCIC_PWRCTL_PWR_ENABLE);
1010 1.1.2.12 thorpej delay(10000);
1011 1.1.2.12 thorpej pcic_write(h, PCIC_PWRCTL, PCIC_PWRCTL_PWR_ENABLE | PCIC_PWRCTL_OE);
1012 1.1.2.10 marc
1013 1.1.2.12 thorpej /* clear the reset flag */
1014 1.1.2.10 marc
1015 1.1.2.12 thorpej pcic_write(h, PCIC_INTR, PCIC_INTR_RESET);
1016 1.1.2.10 marc
1017 1.1.2.12 thorpej /* wait 20ms as per pc card standard (r2.01) section 4.3.6 */
1018 1.1.2.10 marc
1019 1.1.2.12 thorpej delay(20000);
1020 1.1.2.10 marc
1021 1.1.2.12 thorpej /* wait for the chip to finish initializing */
1022 1.1.2.10 marc
1023 1.1.2.12 thorpej pcic_wait_ready(h);
1024 1.1.2.10 marc
1025 1.1.2.12 thorpej /* zero out the address windows */
1026 1.1.2.10 marc
1027 1.1.2.12 thorpej pcic_write(h, PCIC_ADDRWIN_ENABLE, 0);
1028 1.1.2.10 marc
1029 1.1.2.12 thorpej /* set the card type */
1030 1.1.2.10 marc
1031 1.1.2.12 thorpej cardtype = pcmcia_card_gettype(h->pcmcia);
1032 1.1.2.10 marc
1033 1.1.2.12 thorpej reg = pcic_read(h, PCIC_INTR);
1034 1.1.2.12 thorpej reg &= ~PCIC_INTR_CARDTYPE_MASK;
1035 1.1.2.12 thorpej reg |= ((cardtype == PCMCIA_IFTYPE_IO) ?
1036 1.1.2.12 thorpej PCIC_INTR_CARDTYPE_IO :
1037 1.1.2.12 thorpej PCIC_INTR_CARDTYPE_MEM);
1038 1.1.2.12 thorpej reg |= h->ih_irq;
1039 1.1.2.12 thorpej pcic_write(h, PCIC_INTR, reg);
1040 1.1.2.10 marc
1041 1.1.2.12 thorpej DPRINTF(("%s: pcic_chip_socket_enable %02x cardtype %s %02x\n",
1042 1.1.2.12 thorpej h->sc->dev.dv_xname, h->sock,
1043 1.1.2.12 thorpej ((cardtype == PCMCIA_IFTYPE_IO) ? "io" : "mem"), reg));
1044 1.1.2.10 marc
1045 1.1.2.12 thorpej /* reinstall all the memory and io mappings */
1046 1.1.2.10 marc
1047 1.1.2.12 thorpej for (win = 0; win < PCIC_MEM_WINS; win++)
1048 1.1.2.12 thorpej if (h->memalloc & (1 << win))
1049 1.1.2.12 thorpej pcic_chip_do_mem_map(h, win);
1050 1.1.2.10 marc
1051 1.1.2.12 thorpej for (win = 0; win < PCIC_IO_WINS; win++)
1052 1.1.2.12 thorpej if (h->ioalloc & (1 << win))
1053 1.1.2.12 thorpej pcic_chip_do_io_map(h, win);
1054 1.1.2.7 thorpej }
1055 1.1.2.7 thorpej
1056 1.1.2.7 thorpej void
1057 1.1.2.7 thorpej pcic_chip_socket_disable(pch)
1058 1.1.2.12 thorpej pcmcia_chipset_handle_t pch;
1059 1.1.2.7 thorpej {
1060 1.1.2.12 thorpej struct pcic_handle *h = (struct pcic_handle *) pch;
1061 1.1.2.10 marc
1062 1.1.2.12 thorpej DPRINTF(("pcic_chip_socket_disable\n"));
1063 1.1.2.10 marc
1064 1.1.2.12 thorpej /* power down the socket */
1065 1.1.2.10 marc
1066 1.1.2.12 thorpej pcic_write(h, PCIC_PWRCTL, 0);
1067 1.1.2.1 marc }
1068