Update cardbus/pccard support.
[dragonfly.git] / sys / dev / disk / nata / ata-card.c
1 /*-
2  * Copyright (c) 1998 - 2006 Søren Schmidt <sos@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification, immediately at the beginning of the file.
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  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/dev/ata/ata-card.c,v 1.39 2006/01/05 21:27:19 sos Exp $
27  * $DragonFly: src/sys/dev/disk/nata/ata-card.c,v 1.3 2007/07/05 12:08:53 sephe Exp $
28  */
29
30 #include "opt_ata.h"
31
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/module.h>
35 #include <sys/nata.h>
36 #include <sys/rman.h>
37
38 #include <bus/pccard/pccard_cis.h>
39 #include <bus/pccard/pccardreg.h>
40 #include <bus/pccard/pccardvar.h>
41 #include <bus/pccard/pccarddevs.h>
42
43 #include "ata-all.h"
44 #include "ata_if.h"
45
46 static const struct pccard_product ata_pccard_products[] = {
47         PCMCIA_CARD(FREECOM, PCCARDIDE, 0),
48         PCMCIA_CARD(EXP, EXPMULTIMEDIA, 0),
49         PCMCIA_CARD(IODATA3, CBIDE2, 0),
50         PCMCIA_CARD(OEM2, CDROM1, 0),
51         PCMCIA_CARD(OEM2, IDE, 0),
52         PCMCIA_CARD(PANASONIC, KXLC005, 0),
53         PCMCIA_CARD(TEAC, IDECARDII, 0),
54         {NULL}
55 };
56
57 static int
58 ata_pccard_probe(device_t dev)
59 {
60     const struct pccard_product *pp;
61     u_int32_t fcn = PCCARD_FUNCTION_UNSPEC;
62
63     fcn = pccard_get_function_number(dev);
64
65     /* if it says its a disk we should register it */
66     if (fcn == PCCARD_FUNCTION_DISK)
67         return 0;
68
69     /* match other devices here, primarily cdrom/dvd rom */
70     if ((pp = pccard_product_lookup(dev, ata_pccard_products,
71                                     sizeof(ata_pccard_products[0]), NULL))) {
72         if (pp->pp_name)
73             device_set_desc(dev, pp->pp_name);
74         return 0;
75     }
76     return ENXIO;
77 }
78
79 static int
80 ata_pccard_attach(device_t dev)
81 {
82     struct ata_channel *ch = device_get_softc(dev);
83     struct resource *io, *ctlio;
84     int i, rid, err;
85
86     /* allocate the io range to get start and length */
87     rid = ATA_IOADDR_RID;
88     if (!(io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
89                                   ATA_IOSIZE, RF_ACTIVE)))
90         return (ENXIO);
91
92     /* setup the resource vectors */
93     for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
94         ch->r_io[i].res = io;
95         ch->r_io[i].offset = i;
96     }
97     ch->r_io[ATA_IDX_ADDR].res = io;
98
99     /*
100      * if we got more than the default ATA_IOSIZE ports, this is a device
101      * where ctlio is located at offset 14 into "normal" io space.
102      */
103     if (rman_get_size(io) > ATA_IOSIZE) {
104         ch->r_io[ATA_CONTROL].res = io;
105         ch->r_io[ATA_CONTROL].offset = 14;
106     }
107     else {
108         rid = ATA_CTLADDR_RID;
109         if (!(ctlio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
110                                          ATA_CTLIOSIZE, RF_ACTIVE))) {
111             bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
112             for (i = ATA_DATA; i < ATA_MAX_RES; i++)
113                 ch->r_io[i].res = NULL;
114             return (ENXIO);
115         }
116         ch->r_io[ATA_CONTROL].res = ctlio;
117         ch->r_io[ATA_CONTROL].offset = 0;
118     }
119     ata_default_registers(dev);
120
121     /* initialize softc for this channel */
122     ch->unit = 0;
123     ch->flags |= (ATA_USE_16BIT | ATA_NO_SLAVE);
124     ata_generic_hw(dev);
125     err = ata_probe(dev);
126     if (err)
127         return (err);
128     return (ata_attach(dev));
129 }
130
131 static int
132 ata_pccard_detach(device_t dev)
133 {
134     struct ata_channel *ch = device_get_softc(dev);
135     int i;
136
137     ata_detach(dev);
138     if (ch->r_io[ATA_CONTROL].res != ch->r_io[ATA_DATA].res)
139         bus_release_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID,
140                              ch->r_io[ATA_CONTROL].res);
141     bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID,
142                          ch->r_io[ATA_DATA].res);
143     for (i = ATA_DATA; i < ATA_MAX_RES; i++)
144         ch->r_io[i].res = NULL;
145     return 0;
146 }
147
148 static device_method_t ata_pccard_methods[] = {
149     /* device interface */
150     DEVMETHOD(device_probe,             ata_pccard_probe),
151     DEVMETHOD(device_attach,            ata_pccard_attach),
152     DEVMETHOD(device_detach,            ata_pccard_detach),
153
154     { 0, 0 }
155 };
156
157 static driver_t ata_pccard_driver = {
158     "ata",
159     ata_pccard_methods,
160     sizeof(struct ata_channel),
161 };
162
163 DRIVER_MODULE(ata, pccard, ata_pccard_driver, ata_devclass, 0, 0);
164 MODULE_DEPEND(ata, ata, 1, 1, 1);