kernel tree reorganization stage 1: Major cvs repository work (not logged as
[dragonfly.git] / sys / bus / pccard / pcic_isa.c
1 /*
2  * Copyright (c) 2001 M. Warner Losh.  All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  * $FreeBSD: src/sys/pccard/pcic_isa.c,v 1.13.2.8 2003/02/26 18:42:00 imp Exp $
25  * $DragonFly: src/sys/bus/pccard/Attic/pcic_isa.c,v 1.3 2003/08/07 21:16:46 dillon Exp $
26  */
27
28 #include <sys/param.h>
29 #include <sys/bus.h>
30 #include <sys/kernel.h>
31 #include <sys/module.h>
32 #include <sys/sysctl.h>
33 #include <sys/systm.h>
34
35 #include "i82365.h"
36 #include "cardinfo.h"
37 #include "slot.h"
38 #include "pcicvar.h"
39
40 /* Get pnp IDs */
41 #include <bus/isa/isavar.h>
42 #include <dev/misc/pcic/i82365reg.h>
43 #include "pccardvar.h"
44 #include "card_if.h"
45
46 static struct isa_pnp_id pcic_ids[] = {
47         {PCIC_PNP_ACTIONTEC,            NULL},          /* AEI0218 */
48         {PCIC_PNP_IBM3765,              NULL},          /* IBM3765 */
49         {PCIC_PNP_82365,                NULL},          /* PNP0E00 */
50         {PCIC_PNP_CL_PD6720,            NULL},          /* PNP0E01 */
51         {PCIC_PNP_VLSI_82C146,          NULL},          /* PNP0E02 */
52         {PCIC_PNP_82365_CARDBUS,        NULL},          /* PNP0E03 */
53         {PCIC_PNP_SCM_SWAPBOX,          NULL},          /* SCM0469 */ 
54         {PCIC_NEC_PC9801_102,           NULL},          /* NEC8091 */
55         {PCIC_NEC_PC9821RA_E01,         NULL},          /* NEC8121 */
56         {0}
57 };
58
59 static struct {
60         const char *name;
61         u_int32_t flags;
62 } bridges[] = {
63         { "Intel i82365SL-A/B", PCIC_AB_POWER},
64         { "IBM PCIC",           PCIC_AB_POWER},
65         { "VLSI 82C146",        PCIC_AB_POWER},
66         { "Cirrus logic 6722",  PCIC_PD_POWER},
67         { "Cirrus logic 6710",  PCIC_PD_POWER},
68         { "Vadem 365",          PCIC_VG_POWER},
69         { "Vadem 465",          PCIC_VG_POWER},
70         { "Vadem 468",          PCIC_VG_POWER},
71         { "Vadem 469",          PCIC_VG_POWER},
72         { "Ricoh RF5C296",      PCIC_RICOH_POWER},
73         { "Ricoh RF5C396",      PCIC_RICOH_POWER},
74         { "IBM KING",           PCIC_KING_POWER},
75         { "Intel i82365SL-DF",  PCIC_DF_POWER}
76 };
77
78 static pcic_intr_way_t pcic_isa_intr_way;
79 static pcic_init_t pcic_isa_init;
80
81 struct pcic_chip pcic_isa_chip = {
82         pcic_isa_intr_way,
83         pcic_isa_intr_way,
84         pcic_isa_mapirq,
85         pcic_isa_init
86 };
87
88 /*
89  *      Look for an Intel PCIC (or compatible).
90  *      For each available slot, allocate a PC-CARD slot.
91  */
92 static int
93 pcic_isa_probe(device_t dev)
94 {
95         int slotnum, validslots = 0;
96         struct pcic_slot *sp;
97         struct pcic_slot *sp0;
98         struct pcic_slot *sp1;
99         struct pcic_slot spsave;
100         unsigned char c;
101         struct resource *r;
102         int rid;
103         struct pcic_softc *sc;
104         int error;
105
106         /* Check isapnp ids */
107         error = ISA_PNP_PROBE(device_get_parent(dev), dev, pcic_ids);
108         if (error == ENXIO)
109                 return (ENXIO);
110
111         if (bus_get_resource_start(dev, SYS_RES_IOPORT, 0) == 0)
112                 bus_set_resource(dev, SYS_RES_IOPORT, 0, PCIC_PORT_0,
113                     PCIC_NPORT);
114         rid = 0;
115         r = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE);
116         if (!r) {
117                 if (bootverbose)
118                         device_printf(dev, "Cannot get I/O range\n");
119                 return (ENOMEM);
120         }
121
122         sc = (struct pcic_softc *) device_get_softc(dev);
123         sc->dev = dev;
124         sp = &sc->slots[0];
125         for (slotnum = 0; slotnum < PCIC_CARD_SLOTS; slotnum++, sp++) {
126                 /*
127                  *      Initialise the PCIC slot table.
128                  */
129                 sp->getb = pcic_getb_io;
130                 sp->putb = pcic_putb_io;
131                 sp->bst = rman_get_bustag(r);
132                 sp->bsh = rman_get_bushandle(r);
133                 sp->offset = slotnum * PCIC_SLOT_SIZE;
134                 sp->controller = -1;
135         }
136
137         /*
138          * Prescan for the broken VLSI chips.
139          *
140          * According to the Linux PCMCIA code from David Hinds,
141          * working chipsets return 0x84 from their (correct) ID ports,
142          * while the broken ones would need to be probed at the new
143          * offset we set after we assume it's broken.
144          *
145          * Note: because of this, we may incorrectly detect a single
146          * slot vlsi chip as an i82365sl step D.  I cannot find a
147          * datasheet for the affected chip, so that's the best we can
148          * do for now.
149          */
150         sp0 = &sc->slots[0];
151         sp1 = &sc->slots[1];
152         if (sp0->getb(sp0, PCIC_ID_REV) == PCIC_VLSI82C146 &&
153             sp1->getb(sp1, PCIC_ID_REV) != PCIC_VLSI82C146) {
154                 spsave = *sp1;
155                 sp1->bsh += 4;
156                 sp1->offset = PCIC_SLOT_SIZE << 1;
157                 if (sp1->getb(sp1, PCIC_ID_REV) != PCIC_VLSI82C146) {
158                         *sp1 = spsave;
159                 } else {
160                         sp0->controller = PCIC_VLSI;
161                         sp1->controller = PCIC_VLSI;
162                 }
163         }
164         
165         /*
166          * Look for normal chipsets here.
167          */
168         sp = &sc->slots[0];
169         for (slotnum = 0; slotnum < PCIC_CARD_SLOTS; slotnum++, sp++) {
170                 /*
171                  * see if there's a PCMCIA controller here
172                  * Intel PCMCIA controllers use 0x82 and 0x83
173                  * IBM clone chips use 0x88 and 0x89, apparently
174                  */
175                 c = sp->getb(sp, PCIC_ID_REV);
176                 sp->revision = -1;
177                 switch(c) {
178                 /*
179                  *      82365 or clones.
180                  */
181                 case PCIC_INTEL0:
182                 case PCIC_INTEL1:
183                         sp->controller = PCIC_I82365;
184                         sp->revision = c & 1;
185                         /*
186                          * Check for Vadem chips by unlocking their extra
187                          * registers and looking for valid ID.  Bit 3 in
188                          * the ID register is normally 0, except when
189                          * PCIC_VADEMREV is set.  Other bridges appear
190                          * to ignore this frobbing.
191                          */
192                         bus_space_write_1(sp->bst, sp->bsh, PCIC_INDEX, 0x0E);
193                         bus_space_write_1(sp->bst, sp->bsh, PCIC_INDEX, 0x37);
194                         pcic_setb(sp, PCIC_VMISC, PCIC_VADEMREV);
195                         c = sp->getb(sp, PCIC_ID_REV);
196                         if (c & 0x08) {
197                                 switch (sp->revision = c & 7) {
198                                 case 1:
199                                         sp->controller = PCIC_VG365;
200                                         break;
201                                 case 2:
202                                         sp->controller = PCIC_VG465;
203                                         break;
204                                 case 3:
205                                         sp->controller = PCIC_VG468;
206                                         break;
207                                 default:
208                                         sp->controller = PCIC_VG469;
209                                         break;
210                                 }
211                                 pcic_clrb(sp, PCIC_VMISC, PCIC_VADEMREV);
212                         }
213
214                         /*
215                          * Check for RICOH RF5C[23]96 PCMCIA Controller
216                          */
217                         c = sp->getb(sp, PCIC_RICOH_ID);
218                         if (c == PCIC_RID_396)
219                                 sp->controller = PCIC_RF5C396;
220                         else if (c == PCIC_RID_296)
221                                 sp->controller = PCIC_RF5C296;
222
223                         break;
224                 /*
225                  *      Intel i82365sl-DF step or maybe a vlsi 82c146
226                  * we detected the vlsi case earlier, so if the controller
227                  * isn't set, we know it is an i82365sl step D.
228                  */
229                 case PCIC_INTEL2:
230                         if (sp->controller == -1)
231                                 sp->controller = PCIC_I82365SL_DF;
232                         break;
233                 case PCIC_IBM1:
234                 case PCIC_IBM2:
235                         sp->controller = PCIC_IBM;
236                         sp->revision = c & 1;
237                         break;
238                 case PCIC_IBM3:
239                         sp->controller = PCIC_IBM_KING;
240                         sp->revision = c & 1;
241                         break;
242                 default:
243                         continue;
244                 }
245                 /*
246                  *      Check for Cirrus logic chips.
247                  */
248                 sp->putb(sp, PCIC_CLCHIP, 0);
249                 c = sp->getb(sp, PCIC_CLCHIP);
250                 if ((c & PCIC_CLC_TOGGLE) == PCIC_CLC_TOGGLE) {
251                         c = sp->getb(sp, PCIC_CLCHIP);
252                         if ((c & PCIC_CLC_TOGGLE) == 0) {
253                                 if (c & PCIC_CLC_DUAL)
254                                         sp->controller = PCIC_PD6722;
255                                 else
256                                         sp->controller = PCIC_PD6710;
257                                 sp->revision = 8 - ((c & 0x1F) >> 2);
258                         }
259                 }
260                 device_set_desc(dev, bridges[(int) sp->controller].name);
261                 sc->flags = bridges[(int) sp->controller].flags;
262                 /*
263                  *      OK it seems we have a PCIC or lookalike.
264                  *      Allocate a slot and initialise the data structures.
265                  */
266                 validslots++;
267                 sp->slt = (struct slot *) 1;
268                 /*
269                  * Modem cards send the speaker audio (dialing noises)
270                  * to the host's speaker.  Cirrus Logic PCIC chips must
271                  * enable this.  There is also a Low Power Dynamic Mode bit
272                  * that claims to reduce power consumption by 30%, so
273                  * enable it and hope for the best.
274                  */
275                 if (sp->controller == PCIC_PD6722) {
276                         pcic_setb(sp, PCIC_MISC1, PCIC_MISC1_SPEAKER);
277                         pcic_setb(sp, PCIC_MISC2, PCIC_LPDM_EN);
278                 }
279         }
280         bus_release_resource(dev, SYS_RES_IOPORT, rid, r);
281         return (validslots ? 0 : ENXIO);
282 }
283
284 static int
285 pcic_isa_attach(device_t dev)
286 {
287         struct pcic_softc *sc;
288         int rid;
289         struct resource *r;
290         int             irq = 0;
291         int error;
292
293         sc = device_get_softc(dev);
294         rid = 0;
295         r = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1, RF_ACTIVE);
296         if (!r) {
297                 pcic_dealloc(dev);
298                 return (ENXIO);
299         }
300         sc->iorid = rid;
301         sc->iores = r;
302         sc->csc_route = pcic_iw_isa;
303         sc->func_route = pcic_iw_isa;
304         sc->chip = &pcic_isa_chip;
305
306         rid = 0;
307         r = NULL;
308         r = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, RF_ACTIVE);
309         if (r == NULL && pcic_override_irq != 0) {
310                 irq = pcic_override_irq;
311                 r = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, irq, irq, 1,
312                     RF_ACTIVE);
313         }
314         if (r && ((1 << (rman_get_start(r))) & PCIC_INT_MASK_ALLOWED) == 0) {
315                 device_printf(dev,
316                     "Hardware does not support irq %d, trying polling.\n",
317                     irq);
318                 bus_release_resource(dev, SYS_RES_IRQ, rid, r);
319                 irq = 0;
320                 r = NULL;
321         }
322         sc->irqrid = rid;
323         sc->irqres = r;
324         irq = 0;
325         if (r != NULL) {
326                 error = bus_setup_intr(dev, r, INTR_TYPE_MISC,
327                     pcic_isa_intr, (void *) sc, &sc->ih);
328                 if (error) {
329                         pcic_dealloc(dev);
330                         return (error);
331                 }
332                 irq = rman_get_start(r);
333                 device_printf(dev, "management irq %d\n", irq);
334         }
335         sc->irq = irq;
336         if (irq == 0) {
337                 sc->slot_poll = pcic_timeout;
338                 sc->timeout_ch = timeout(sc->slot_poll, (void *) sc, hz/2);
339                 device_printf(dev, "Polling mode\n");
340         }
341
342         return (pcic_attach(dev));
343 }
344
345 /*
346  * ISA cards can only do ISA interrupts.  There's no need to do
347  * anything but check args for sanity.
348  */
349 static int
350 pcic_isa_intr_way(struct pcic_slot *sp, enum pcic_intr_way iw)
351 {
352         if (iw != pcic_iw_isa)
353                 return (EINVAL);
354         return (0);
355 }
356
357 /*
358  * No speicial initialization is necesary for ISA cards.
359  */
360 static void
361 pcic_isa_init(device_t dev)
362 {
363 }
364
365 static device_method_t pcic_methods[] = {
366         /* Device interface */
367         DEVMETHOD(device_probe,         pcic_isa_probe),
368         DEVMETHOD(device_attach,        pcic_isa_attach),
369         DEVMETHOD(device_detach,        bus_generic_detach),
370         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
371         DEVMETHOD(device_suspend,       bus_generic_suspend),
372         DEVMETHOD(device_resume,        bus_generic_resume),
373
374         /* Bus interface */
375         DEVMETHOD(bus_print_child,      bus_generic_print_child),
376         DEVMETHOD(bus_alloc_resource,   pcic_alloc_resource),
377         DEVMETHOD(bus_release_resource, bus_generic_release_resource),
378         DEVMETHOD(bus_activate_resource, pcic_activate_resource),
379         DEVMETHOD(bus_deactivate_resource, pcic_deactivate_resource),
380         DEVMETHOD(bus_setup_intr,       pcic_setup_intr),
381         DEVMETHOD(bus_teardown_intr,    pcic_teardown_intr),
382
383         /* Card interface */
384         DEVMETHOD(card_set_res_flags,   pcic_set_res_flags),
385         DEVMETHOD(card_get_res_flags,   pcic_get_res_flags),
386         DEVMETHOD(card_set_memory_offset, pcic_set_memory_offset),
387         DEVMETHOD(card_get_memory_offset, pcic_get_memory_offset),
388
389         { 0, 0 }
390 };
391
392 static driver_t pcic_driver = {
393         "pcic",
394         pcic_methods,
395         sizeof(struct pcic_softc)
396 };
397
398 DRIVER_MODULE(pcic, isa, pcic_driver, pcic_devclass, 0, 0);