ata-card.c revision 116485
1/*-
2 * Copyright (c) 1998 - 2003 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 * 3. The name of the author may not be used to endorse or promote products
15 *    derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * $FreeBSD: head/sys/dev/ata/ata-card.c 116485 2003-06-17 12:33:53Z imp $
29 */
30
31#include <sys/param.h>
32#include <sys/systm.h>
33#include <sys/ata.h>
34#include <sys/kernel.h>
35#include <sys/module.h>
36#include <sys/bus.h>
37#include <sys/malloc.h>
38#include <machine/stdarg.h>
39#include <machine/resource.h>
40#include <machine/bus.h>
41#include <sys/rman.h>
42#include <dev/ata/ata-all.h>
43#include <dev/pccard/pccardreg.h>
44#include <dev/pccard/pccardvar.h>
45#include <dev/pccard/pccarddevs.h>
46
47static const struct pccard_product ata_pccard_products[] = {
48	/* NetBSD has a few others that need to migrate into pccarddevs */
49	/* XXX */
50	PCMCIA_CARD(FREECOM, PCCARDIDE, 0),
51	PCMCIA_CARD(EXP, EXPMULTIMEDIA, 0),
52	PCMCIA_CARD(IODATA, CBIDE2, 0),
53	PCMCIA_CARD(OEM2, CDROM1, 0),
54	PCMCIA_CARD(OEM2, IDE, 0),
55	PCMCIA_CARD(PANASONIC, KXLC005, 0),
56	PCMCIA_CARD(TEAC, IDECARDII, 0),
57	{NULL}
58};
59
60static int
61ata_pccard_match(device_t dev)
62{
63    const struct pccard_product *pp;
64    u_int32_t fcn = PCCARD_FUNCTION_UNSPEC;
65    int error = 0;
66
67    error = pccard_get_function(dev, &fcn);
68    if (error != 0)
69	return (error);
70
71    /* if it says its a disk we should register it */
72    if (fcn == PCCARD_FUNCTION_DISK)
73	return (0);
74
75    /* Match other devices here, primarily cdrom/dvd rom */
76    if ((pp = pccard_product_lookup(dev, ata_pccard_products,
77      sizeof(ata_pccard_products[0]), NULL)) != NULL) {
78	if (pp->pp_name)
79	    device_set_desc(dev, pp->pp_name);
80	return (0);
81    }
82    return(ENXIO);
83}
84
85static void
86ata_pccard_locknoop(struct ata_channel *ch, int type)
87{
88}
89
90static void
91ata_pccard_setmode(struct ata_device *atadev, int mode)
92{
93    atadev->mode = ata_limit_mode(atadev, mode, ATA_PIO_MAX);
94}
95
96static int
97ata_pccard_probe(device_t dev)
98{
99    struct ata_channel *ch = device_get_softc(dev);
100    struct resource *io, *altio;
101    int i, rid, len, start, end;
102    u_long tmp;
103
104    /* allocate the io range to get start and length */
105    rid = ATA_IOADDR_RID;
106    len = bus_get_resource_count(dev, SYS_RES_IOPORT, rid);
107    io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
108			    ATA_IOSIZE, RF_ACTIVE);
109    if (!io)
110	return ENXIO;
111
112    /* reallocate the io address to only cover the io ports */
113    start = rman_get_start(io);
114    end = start + ATA_IOSIZE - 1;
115    bus_release_resource(dev, SYS_RES_IOPORT, rid, io);
116    io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
117			    start, end, ATA_IOSIZE, RF_ACTIVE);
118
119    /*
120     * if we got more than the default ATA_IOSIZE ports, this is likely
121     * a pccard system where the altio ports are located at offset 14
122     * otherwise its the normal altio offset
123     */
124    if (bus_get_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, &tmp, &tmp)) {
125	if (len > ATA_IOSIZE) {
126	    bus_set_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID,
127			     start + ATA_PCCARD_ALTOFFSET, ATA_ALTIOSIZE);
128	}
129	else {
130	    bus_set_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID,
131			     start + ATA_ALTOFFSET, ATA_ALTIOSIZE);
132	}
133    }
134    else {
135	bus_release_resource(dev, SYS_RES_IOPORT, rid, io);
136	return ENXIO;
137    }
138
139    /* allocate the altport range */
140    rid = ATA_ALTADDR_RID;
141    altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
142			       ATA_ALTIOSIZE, RF_ACTIVE);
143    if (!altio) {
144	bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
145	return ENXIO;
146    }
147
148    /* setup the resource vectors */
149    for (i = ATA_DATA; i <= ATA_STATUS; i++) {
150	ch->r_io[i].res = io;
151	ch->r_io[i].offset = i;
152    }
153    ch->r_io[ATA_ALTSTAT].res = altio;
154    ch->r_io[ATA_ALTSTAT].offset = 0;
155
156    /* initialize softc for this channel */
157    ch->unit = 0;
158    ch->flags |= (ATA_USE_16BIT | ATA_NO_SLAVE);
159    ch->locking = ata_pccard_locknoop;
160    ch->device[MASTER].setmode = ata_pccard_setmode;
161    ch->device[SLAVE].setmode = ata_pccard_setmode;
162    return ata_probe(dev);
163}
164
165static int
166ata_pccard_detach(device_t dev)
167{
168    struct ata_channel *ch = device_get_softc(dev);
169    int i;
170
171    ata_detach(dev);
172    bus_release_resource(dev, SYS_RES_IOPORT,
173			 ATA_ALTADDR_RID, ch->r_io[ATA_ALTSTAT].res);
174    bus_release_resource(dev, SYS_RES_IOPORT,
175			 ATA_IOADDR_RID, ch->r_io[ATA_DATA].res);
176    for (i = ATA_DATA; i < ATA_MAX_RES; i++)
177	ch->r_io[i].res = NULL;
178    return 0;
179}
180
181static device_method_t ata_pccard_methods[] = {
182    /* device interface */
183    DEVMETHOD(device_probe,	pccard_compat_probe),
184    DEVMETHOD(device_attach,	pccard_compat_attach),
185    DEVMETHOD(device_detach,	ata_pccard_detach),
186
187    /* Card interface */
188    DEVMETHOD(card_compat_match,	ata_pccard_match),
189    DEVMETHOD(card_compat_probe,	ata_pccard_probe),
190    DEVMETHOD(card_compat_attach,	ata_attach),
191    { 0, 0 }
192};
193
194static driver_t ata_pccard_driver = {
195    "ata",
196    ata_pccard_methods,
197    sizeof(struct ata_channel),
198};
199
200DRIVER_MODULE(ata, pccard, ata_pccard_driver, ata_devclass, 0, 0);
201