1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 1998 - 2008 S��ren Schmidt <sos@FreeBSD.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer,
12 *    without modification, immediately at the beginning of the file.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
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
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD$");
31
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/ata.h>
35#include <sys/kernel.h>
36#include <sys/module.h>
37#include <sys/bus.h>
38#include <sys/malloc.h>
39#include <sys/sema.h>
40#include <sys/taskqueue.h>
41#include <vm/uma.h>
42#include <machine/stdarg.h>
43#include <machine/resource.h>
44#include <machine/bus.h>
45#include <sys/rman.h>
46#include <dev/ata/ata-all.h>
47#include <dev/pccard/pccard_cis.h>
48#include <dev/pccard/pccardreg.h>
49#include <dev/pccard/pccardvar.h>
50#include <ata_if.h>
51
52#include "pccarddevs.h"
53
54static const struct pccard_product ata_pccard_products[] = {
55	PCMCIA_CARD(FREECOM, PCCARDIDE),
56	PCMCIA_CARD(EXP, EXPMULTIMEDIA),
57	PCMCIA_CARD(IODATA3, CBIDE2),
58	PCMCIA_CARD(OEM2, CDROM1),
59	PCMCIA_CARD(OEM2, IDE),
60	PCMCIA_CARD(PANASONIC, KXLC005),
61	PCMCIA_CARD(TEAC, IDECARDII),
62	{NULL}
63};
64
65static int
66ata_pccard_probe(device_t dev)
67{
68    const struct pccard_product *pp;
69    u_int32_t fcn = PCCARD_FUNCTION_UNSPEC;
70    int error = 0;
71
72    if ((error = pccard_get_function(dev, &fcn)))
73	return error;
74
75    /* if it says its a disk we should register it */
76    if (fcn == PCCARD_FUNCTION_DISK)
77	return 0;
78
79    /* match other devices here, primarily cdrom/dvd rom */
80    if ((pp = pccard_product_lookup(dev, ata_pccard_products,
81				    sizeof(ata_pccard_products[0]), NULL))) {
82	if (pp->pp_name)
83	    device_set_desc(dev, pp->pp_name);
84	return 0;
85    }
86    return ENXIO;
87}
88
89static int
90ata_pccard_attach(device_t dev)
91{
92    struct ata_channel *ch = device_get_softc(dev);
93    struct resource *io, *ctlio;
94    int i, rid, err;
95    uint16_t funce;
96
97    if (ch->attached)
98	return (0);
99    ch->attached = 1;
100
101    /* allocate the io range to get start and length */
102    rid = ATA_IOADDR_RID;
103    if (!(io = bus_alloc_resource_anywhere(dev, SYS_RES_IOPORT, &rid,
104					   ATA_IOSIZE, RF_ACTIVE)))
105	return (ENXIO);
106
107    /* setup the resource vectors */
108    for (i = ATA_DATA; i <= ATA_COMMAND; i++) {
109	ch->r_io[i].res = io;
110	ch->r_io[i].offset = i;
111    }
112    ch->r_io[ATA_IDX_ADDR].res = io;
113
114    /*
115     * if we got more than the default ATA_IOSIZE ports, this is a device
116     * where ctlio is located at offset 14 into "normal" io space.
117     */
118    if (rman_get_size(io) > ATA_IOSIZE) {
119	ch->r_io[ATA_CONTROL].res = io;
120	ch->r_io[ATA_CONTROL].offset = 14;
121    }
122    else {
123	rid = ATA_CTLADDR_RID;
124	if (!(ctlio = bus_alloc_resource_anywhere(dev, SYS_RES_IOPORT, &rid,
125						  ATA_CTLIOSIZE, RF_ACTIVE))) {
126	    bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID, io);
127	    for (i = ATA_DATA; i < ATA_MAX_RES; i++)
128		ch->r_io[i].res = NULL;
129	    return (ENXIO);
130	}
131	ch->r_io[ATA_CONTROL].res = ctlio;
132	ch->r_io[ATA_CONTROL].offset = 0;
133    }
134    ata_default_registers(dev);
135
136    /* initialize softc for this channel */
137    ch->unit = 0;
138    ch->flags |= ATA_USE_16BIT;
139    funce = 0;		/* Default to sane setting of FUNCE */
140    pccard_get_funce_disk(dev, &funce);
141    if (!(funce & PFD_I_D))
142        ch-> flags |= ATA_NO_SLAVE;
143    ata_generic_hw(dev);
144    err = ata_probe(dev);
145    if (err > 0)
146	return (err);
147    gone_in_dev(dev, 13, "pccard removed");
148    return (ata_attach(dev));
149}
150
151static int
152ata_pccard_detach(device_t dev)
153{
154    struct ata_channel *ch = device_get_softc(dev);
155    int i;
156
157    if (!ch->attached)
158	return (0);
159    ch->attached = 0;
160
161    ata_detach(dev);
162    if (ch->r_io[ATA_CONTROL].res != ch->r_io[ATA_DATA].res)
163	bus_release_resource(dev, SYS_RES_IOPORT, ATA_CTLADDR_RID,
164			     ch->r_io[ATA_CONTROL].res);
165    bus_release_resource(dev, SYS_RES_IOPORT, ATA_IOADDR_RID,
166			 ch->r_io[ATA_DATA].res);
167    for (i = ATA_DATA; i < ATA_MAX_RES; i++)
168	ch->r_io[i].res = NULL;
169    return 0;
170}
171
172static device_method_t ata_pccard_methods[] = {
173    /* device interface */
174    DEVMETHOD(device_probe,             ata_pccard_probe),
175    DEVMETHOD(device_attach,            ata_pccard_attach),
176    DEVMETHOD(device_detach,            ata_pccard_detach),
177
178    DEVMETHOD_END
179};
180
181static driver_t ata_pccard_driver = {
182    "ata",
183    ata_pccard_methods,
184    sizeof(struct ata_channel),
185};
186
187DRIVER_MODULE(ata, pccard, ata_pccard_driver, ata_devclass, NULL, NULL);
188MODULE_DEPEND(ata, ata, 1, 1, 1);
189PCCARD_PNP_INFO(ata_pccard_products);
190