Remove the INTR_TYPE_* flags. The interrupt type is no longer used to
[dragonfly.git] / sys / dev / serial / si / si_isa.c
1 /*
2  * Device driver for Specialix range (SI/XIO) of serial line multiplexors.
3  *
4  * Copyright (C) 2000, Peter Wemm <peter@netplex.com.au>
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  *    notices, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notices, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY EXPRESS OR IMPLIED
16  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
18  * NO EVENT SHALL THE AUTHORS BE LIABLE.
19  *
20  * $FreeBSD: src/sys/dev/si/si_isa.c,v 1.1 2000/01/24 07:24:01 peter Exp $
21  * $DragonFly: src/sys/dev/serial/si/si_isa.c,v 1.5 2005/10/12 17:35:55 dillon Exp $
22  */
23
24 #include "opt_debug_si.h"
25
26 #include <sys/param.h>
27 #include <sys/systm.h>
28 #include <sys/kernel.h>
29 #include <sys/bus.h>
30 #include <machine/bus.h>
31 #include <sys/rman.h>
32 #include <machine/resource.h>
33
34 #include "sireg.h"
35 #include "sivar.h"
36
37 #include <bus/isa/isavar.h>
38
39 /* Look for a valid board at the given mem addr */
40 static int
41 si_isa_probe(device_t dev)
42 {
43         struct si_softc *sc;
44         int type;
45         u_int i, ramsize;
46         volatile unsigned char was, *ux;
47         volatile unsigned char *maddr;
48         unsigned char *paddr;
49         int unit;
50
51         sc = device_get_softc(dev);
52         unit = device_get_unit(dev);
53
54         sc->sc_mem_rid = 0;
55         sc->sc_mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
56                                             &sc->sc_mem_rid,
57                                             0, ~0, SIPROBEALLOC, RF_ACTIVE);
58         if (!sc->sc_mem_res)
59                 return ENXIO;
60         paddr = (caddr_t)rman_get_start(sc->sc_mem_res);/* physical */
61         maddr = rman_get_virtual(sc->sc_mem_res);       /* in kvm */
62
63         DPRINT((0, DBG_AUTOBOOT, "si%d: probe at virtual=0x%x physical=0x%x\n",
64                 unit, maddr, paddr));
65
66         /*
67          * this is a lie, but it's easier than trying to handle caching
68          * and ram conflicts in the >1M and <16M region.
69          */
70         if ((caddr_t)paddr < (caddr_t)0xA0000 ||
71             (caddr_t)paddr >= (caddr_t)0x100000) {
72                 printf("si%d: iomem (%p) out of range\n",
73                         unit, (void *)paddr);
74                 goto fail;
75         }
76
77         if (((u_int)paddr & 0x7fff) != 0) {
78                 DPRINT((0, DBG_AUTOBOOT|DBG_FAIL,
79                         "si%d: iomem (%x) not on 32k boundary\n", unit, paddr));
80                 goto fail;
81         }
82
83         /* Is there anything out there? (0x17 is just an arbitrary number) */
84         *maddr = 0x17;
85         if (*maddr != 0x17) {
86                 DPRINT((0, DBG_AUTOBOOT|DBG_FAIL,
87                         "si%d: 0x17 check fail at phys 0x%x\n", unit, paddr));
88                 goto fail;
89         }
90         /*
91          * Let's look first for a JET ISA card, since that's pretty easy
92          *
93          * All jet hosts are supposed to have this string in the IDROM,
94          * but it's not worth checking on self-IDing busses like PCI.
95          */
96         {
97                 unsigned char *jet_chk_str = "JET HOST BY KEV#";
98
99                 for (i = 0; i < strlen(jet_chk_str); i++)
100                         if (jet_chk_str[i] != *(maddr + SIJETIDSTR + 2 * i))
101                                 goto try_mk2;
102         }
103         DPRINT((0, DBG_AUTOBOOT|DBG_FAIL, "si%d: JET first check - 0x%x\n",
104                 unit, (*(maddr+SIJETIDBASE))));
105         if (*(maddr+SIJETIDBASE) != (SISPLXID&0xff))
106                 goto try_mk2;
107         DPRINT((0, DBG_AUTOBOOT|DBG_FAIL, "si%d: JET second check - 0x%x\n",
108                 unit, (*(maddr+SIJETIDBASE+2))));
109         if (*(maddr+SIJETIDBASE+2) != ((SISPLXID&0xff00)>>8))
110                 goto try_mk2;
111         /* It must be a Jet ISA or RIO card */
112         DPRINT((0, DBG_AUTOBOOT|DBG_FAIL, "si%d: JET id check - 0x%x\n",
113                 unit, (*(maddr+SIUNIQID))));
114         if ((*(maddr+SIUNIQID) & 0xf0) != 0x20)
115                 goto try_mk2;
116         /* It must be a Jet ISA SI/XIO card */
117         *(maddr + SIJETCONFIG) = 0;
118         type = SIJETISA;
119         ramsize = SIJET_RAMSIZE;
120         goto got_card;
121
122 try_mk2:
123         /*
124          * OK, now to see if whatever responded is really an SI card.
125          * Try for a MK II next (SIHOST2)
126          */
127         for (i = SIPLSIG; i < SIPLSIG + 8; i++)
128                 if ((*(maddr+i) & 7) != (~(unsigned char)i & 7))
129                         goto try_mk1;
130
131         /* It must be an SIHOST2 */
132         *(maddr + SIPLRESET) = 0;
133         *(maddr + SIPLIRQCLR) = 0;
134         *(maddr + SIPLIRQSET) = 0x10;
135         type = SIHOST2;
136         ramsize = SIHOST2_RAMSIZE;
137         goto got_card;
138
139 try_mk1:
140         /*
141          * Its not a MK II, so try for a MK I (SIHOST)
142          */
143         *(maddr+SIRESET) = 0x0;         /* reset the card */
144         *(maddr+SIINTCL) = 0x0;         /* clear int */
145         *(maddr+SIRAM) = 0x17;
146         if (*(maddr+SIRAM) != (unsigned char)0x17)
147                 goto fail;
148         *(maddr+0x7ff8) = 0x17;
149         if (*(maddr+0x7ff8) != (unsigned char)0x17) {
150                 DPRINT((0, DBG_AUTOBOOT|DBG_FAIL,
151                         "si%d: 0x17 check fail at phys 0x%x = 0x%x\n",
152                         unit, paddr+0x77f8, *(maddr+0x77f8)));
153                 goto fail;
154         }
155
156         /* It must be an SIHOST (maybe?) - there must be a better way XXX */
157         type = SIHOST;
158         ramsize = SIHOST_RAMSIZE;
159
160 got_card:
161         DPRINT((0, DBG_AUTOBOOT, "si%d: found type %d card, try memory test\n",
162                 unit, type));
163         /* Try the acid test */
164         ux = maddr + SIRAM;
165         for (i = 0; i < ramsize; i++, ux++)
166                 *ux = (unsigned char)(i&0xff);
167         ux = maddr + SIRAM;
168         for (i = 0; i < ramsize; i++, ux++) {
169                 if ((was = *ux) != (unsigned char)(i&0xff)) {
170                         DPRINT((0, DBG_AUTOBOOT|DBG_FAIL,
171                                 "si%d: match fail at phys 0x%x, was %x should be %x\n",
172                                 unit, paddr + i, was, i&0xff));
173                         goto fail;
174                 }
175         }
176
177         /* clear out the RAM */
178         ux = maddr + SIRAM;
179         for (i = 0; i < ramsize; i++)
180                 *ux++ = 0;
181         ux = maddr + SIRAM;
182         for (i = 0; i < ramsize; i++) {
183                 if ((was = *ux++) != 0) {
184                         DPRINT((0, DBG_AUTOBOOT|DBG_FAIL,
185                                 "si%d: clear fail at phys 0x%x, was %x\n",
186                                 unit, paddr + i, was));
187                         goto fail;
188                 }
189         }
190
191         /*
192          * Success, we've found a valid board, now fill in
193          * the adapter structure.
194          */
195         switch (type) {
196         case SIHOST2:
197                 switch (isa_get_irq(dev)) {
198                 case 11:
199                 case 12:
200                 case 15:
201                         break;
202                 default:
203 bad_irq:
204                         DPRINT((0, DBG_AUTOBOOT|DBG_FAIL,
205                                 "si%d: bad IRQ value - %d\n",
206                                 unit, isa_get_irq(dev)));
207                         goto fail;
208                 }
209                 sc->sc_memsize = SIHOST2_MEMSIZE;
210                 break;
211         case SIHOST:
212                 switch (isa_get_irq(dev)) {
213                 case 11:
214                 case 12:
215                 case 15:
216                         break;
217                 default:
218                         goto bad_irq;
219                 }
220                 sc->sc_memsize = SIHOST_MEMSIZE;
221                 break;
222         case SIJETISA:
223                 switch (isa_get_irq(dev)) {
224                 case 9:
225                 case 10:
226                 case 11:
227                 case 12:
228                 case 15:
229                         break;
230                 default:
231                         goto bad_irq;
232                 }
233                 sc->sc_memsize = SIJETISA_MEMSIZE;
234                 break;
235         case SIMCA:             /* MCA */
236         default:
237                 printf("si%d: card type %d not supported\n", unit, type);
238                 goto fail;
239         }
240         sc->sc_type = type;
241         bus_release_resource(dev, SYS_RES_MEMORY,
242                              sc->sc_mem_rid, sc->sc_mem_res);
243         sc->sc_mem_res = 0;
244         return (0);             /* success! */
245
246 fail:
247         if (sc->sc_mem_res) {
248                 bus_release_resource(dev, SYS_RES_MEMORY,
249                                      sc->sc_mem_rid, sc->sc_mem_res);
250                 sc->sc_mem_res = 0;
251         }
252         return(EINVAL);
253 }
254
255 static int
256 si_isa_attach(device_t dev)
257 {
258         int error;
259         void *ih;
260         struct si_softc *sc;
261
262         error = 0;
263         ih = NULL;
264         sc = device_get_softc(dev);
265
266         sc->sc_mem_rid = 0;
267         sc->sc_mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
268                                             &sc->sc_mem_rid,
269                                             0, ~0, 1, RF_ACTIVE);
270         if (!sc->sc_mem_res) {
271                 device_printf(dev, "couldn't map memory\n");
272                 goto fail;
273         }
274         sc->sc_paddr = (caddr_t)rman_get_start(sc->sc_mem_res);
275         sc->sc_maddr = rman_get_virtual(sc->sc_mem_res);
276
277         sc->sc_irq_rid = 0;
278         sc->sc_irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->sc_irq_rid,
279                                             0, ~0, 1, RF_ACTIVE | RF_SHAREABLE);
280         if (!sc->sc_irq_res) {
281                 device_printf(dev, "couldn't allocate interrupt\n");
282                 goto fail;
283         }
284         sc->sc_irq = rman_get_start(sc->sc_irq_res);
285         error = bus_setup_intr(dev, sc->sc_irq_res, 0, si_intr, sc, &ih, NULL);
286         if (error) {
287                 device_printf(dev, "couldn't activate interrupt\n");
288                 goto fail;
289         }
290
291         error = siattach(dev);
292         if (error)
293                 goto fail;
294         return (0);             /* success */
295
296 fail:
297         if (error == 0)
298                 error = ENXIO;
299         if (sc->sc_irq_res) {
300                 if (ih)
301                         bus_teardown_intr(dev, sc->sc_irq_res, ih);
302                 bus_release_resource(dev, SYS_RES_IRQ,
303                                      sc->sc_irq_rid, sc->sc_irq_res);
304                 sc->sc_irq_res = 0;
305         }
306         if (sc->sc_mem_res) {
307                 bus_release_resource(dev, SYS_RES_MEMORY,
308                                      sc->sc_mem_rid, sc->sc_mem_res);
309                 sc->sc_mem_res = 0;
310         }
311         return (error);
312 }
313
314 static device_method_t si_isa_methods[] = {
315         /* Device interface */
316         DEVMETHOD(device_probe,         si_isa_probe),
317         DEVMETHOD(device_attach,        si_isa_attach),
318
319         { 0, 0 }
320 };
321
322 static driver_t si_isa_driver = {
323         "si",
324         si_isa_methods,
325         sizeof(struct si_softc),
326 };
327
328 DRIVER_MODULE(si, isa, si_isa_driver, si_devclass, 0, 0);