ata-card.c revision 115756
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 115756 2003-06-03 01:30:55Z 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(EXP, EXPMULTIMEDIA, 0),
51	PCMCIA_CARD(IODATA, CBIDE2, 0),
52	PCMCIA_CARD(OEM2, CDROM1, 0),
53	PCMCIA_CARD(OEM2, IDE, 0),
54	PCMCIA_CARD(PANASONIC, KXLC005, 0),
55	PCMCIA_CARD(TEAC, IDECARDII, 0),
56	{NULL}
57};
58
59static int
60ata_pccard_match(device_t dev)
61{
62    const struct pccard_product *pp;
63    u_int32_t fcn = PCCARD_FUNCTION_UNSPEC;
64    int error = 0;
65
66    error = pccard_get_function(dev, &fcn);
67    if (error != 0)
68	return (error);
69
70    /* if it says its a disk we should register it */
71    if (fcn == PCCARD_FUNCTION_DISK)
72	return (0);
73
74    /* Match other devices here, primarily cdrom/dvd rom */
75    if ((pp = pccard_product_lookup(dev, ata_pccard_products,
76      sizeof(ata_pccard_products[0]), NULL)) != NULL) {
77	if (pp->pp_name)
78	    device_set_desc(dev, pp->pp_name);
79	return (0);
80    }
81    return(ENXIO);
82}
83
84static void
85ata_pccard_locknoop(struct ata_channel *ch, int type)
86{
87}
88
89static void
90ata_pccard_setmode(struct ata_device *atadev, int mode)
91{
92    atadev->mode = ata_limit_mode(atadev, mode, ATA_PIO_MAX);
93}
94
95static int
96ata_pccard_probe(device_t dev)
97{
98    struct ata_channel *ch = device_get_softc(dev);
99    struct resource *io, *altio;
100    int i, rid, len, start, end;
101    u_long tmp;
102
103    /* allocate the io range to get start and length */
104    rid = ATA_IOADDR_RID;
105    len = bus_get_resource_count(dev, SYS_RES_IOPORT, rid);
106    io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
107			    ATA_IOSIZE, RF_ACTIVE);
108    if (!io)
109	return ENXIO;
110
111    /* reallocate the io address to only cover the io ports */
112    start = rman_get_start(io);
113    end = start + ATA_IOSIZE - 1;
114    bus_release_resource(dev, SYS_RES_IOPORT, rid, io);
115    io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
116			    start, end, ATA_IOSIZE, RF_ACTIVE);
117
118    /*
119     * if we got more than the default ATA_IOSIZE ports, this is likely
120     * a pccard system where the altio ports are located at offset 14
121     * otherwise its the normal altio offset
122     */
123    if (bus_get_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID, &tmp, &tmp)) {
124	if (len > ATA_IOSIZE) {
125	    bus_set_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID,
126			     start + ATA_PCCARD_ALTOFFSET, ATA_ALTIOSIZE);
127	}
128	else {
129	    bus_set_resource(dev, SYS_RES_IOPORT, ATA_ALTADDR_RID,
130			     start + ATA_ALTOFFSET, ATA_ALTIOSIZE);
131	}
132    }
133    else {
134	bus_release_resource(dev, SYS_RES_IOPORT, rid, io);
135	return ENXIO;
136    }
137
138    /* allocate the altport range */
139    rid = ATA_ALTADDR_RID;
140    altio = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
141			       ATA_ALTIOSIZE, RF_ACTIVE);
142    if (!altio) {
143	bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
144	return ENXIO;
145    }
146
147    /* setup the resource vectors */
148    for (i = ATA_DATA; i <= ATA_STATUS; i++) {
149	ch->r_io[i].res = io;
150	ch->r_io[i].offset = i;
151    }
152    ch->r_io[ATA_ALTSTAT].res = altio;
153    ch->r_io[ATA_ALTSTAT].offset = 0;
154
155    /* initialize softc for this channel */
156    ch->unit = 0;
157    ch->flags |= (ATA_USE_16BIT | ATA_NO_SLAVE);
158    ch->locking = ata_pccard_locknoop;
159    ch->device[MASTER].setmode = ata_pccard_setmode;
160    ch->device[SLAVE].setmode = ata_pccard_setmode;
161    return ata_probe(dev);
162}
163
164static int
165ata_pccard_detach(device_t dev)
166{
167    struct ata_channel *ch = device_get_softc(dev);
168    int i;
169
170    ata_detach(dev);
171    bus_release_resource(dev, SYS_RES_IOPORT,
172			 ATA_ALTADDR_RID, ch->r_io[ATA_ALTSTAT].res);
173    bus_release_resource(dev, SYS_RES_IOPORT,
174			 ATA_IOADDR_RID, ch->r_io[ATA_DATA].res);
175    for (i = ATA_DATA; i < ATA_MAX_RES; i++)
176	ch->r_io[i].res = NULL;
177    return 0;
178}
179
180static device_method_t ata_pccard_methods[] = {
181    /* device interface */
182    DEVMETHOD(device_probe,	pccard_compat_probe),
183    DEVMETHOD(device_attach,	pccard_compat_attach),
184    DEVMETHOD(device_detach,	ata_pccard_detach),
185
186    /* Card interface */
187    DEVMETHOD(card_compat_match,	ata_pccard_match),
188    DEVMETHOD(card_compat_probe,	ata_pccard_probe),
189    DEVMETHOD(card_compat_attach,	ata_attach),
190    { 0, 0 }
191};
192
193static driver_t ata_pccard_driver = {
194    "ata",
195    ata_pccard_methods,
196    sizeof(struct ata_channel),
197};
198
199DRIVER_MODULE(ata, pccard, ata_pccard_driver, ata_devclass, 0, 0);
200