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