2 * Copyright (c) 1998 - 2008 Søren Schmidt <sos@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/module.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
37 #include <sys/endian.h>
38 #include <sys/malloc.h>
40 #include <sys/mutex.h>
42 #include <sys/taskqueue.h>
44 #include <machine/stdarg.h>
45 #include <machine/resource.h>
46 #include <machine/bus.h>
48 #include <dev/pci/pcivar.h>
49 #include <dev/pci/pcireg.h>
50 #include <dev/ata/ata-all.h>
51 #include <dev/ata/ata-pci.h>
54 struct ata_serialize {
55 struct mtx locked_mtx;
60 /* local prototypes */
61 static int ata_acard_chipinit(device_t dev);
62 static int ata_acard_chipdeinit(device_t dev);
63 static int ata_acard_ch_attach(device_t dev);
64 static int ata_acard_status(device_t dev);
65 static int ata_acard_850_setmode(device_t dev, int target, int mode);
66 static int ata_acard_86X_setmode(device_t dev, int target, int mode);
67 static int ata_serialize(device_t dev, int flags);
68 static void ata_serialize_init(struct ata_serialize *serial);
75 * Acard chipset support functions
78 ata_acard_probe(device_t dev)
80 struct ata_pci_controller *ctlr = device_get_softc(dev);
81 static struct ata_chip_id ids[] =
82 {{ ATA_ATP850R, 0, ATP_OLD, 0x00, ATA_UDMA2, "ATP850" },
83 { ATA_ATP860A, 0, 0, 0x00, ATA_UDMA4, "ATP860A" },
84 { ATA_ATP860R, 0, 0, 0x00, ATA_UDMA4, "ATP860R" },
85 { ATA_ATP865A, 0, 0, 0x00, ATA_UDMA6, "ATP865A" },
86 { ATA_ATP865R, 0, 0, 0x00, ATA_UDMA6, "ATP865R" },
89 if (pci_get_vendor(dev) != ATA_ACARD_ID)
92 if (!(ctlr->chip = ata_match_chip(dev, ids)))
96 ctlr->chipinit = ata_acard_chipinit;
97 ctlr->chipdeinit = ata_acard_chipdeinit;
98 return (BUS_PROBE_DEFAULT);
102 ata_acard_chipinit(device_t dev)
104 struct ata_pci_controller *ctlr = device_get_softc(dev);
105 struct ata_serialize *serial;
107 if (ata_setup_interrupt(dev, ata_generic_intr))
110 ctlr->ch_attach = ata_acard_ch_attach;
111 ctlr->ch_detach = ata_pci_ch_detach;
112 if (ctlr->chip->cfg1 == ATP_OLD) {
113 ctlr->setmode = ata_acard_850_setmode;
114 ctlr->locking = ata_serialize;
115 serial = malloc(sizeof(struct ata_serialize),
116 M_ATAPCI, M_WAITOK | M_ZERO);
117 ata_serialize_init(serial);
118 ctlr->chipset_data = serial;
121 ctlr->setmode = ata_acard_86X_setmode;
126 ata_acard_chipdeinit(device_t dev)
128 struct ata_pci_controller *ctlr = device_get_softc(dev);
129 struct ata_serialize *serial;
131 if (ctlr->chip->cfg1 == ATP_OLD) {
132 serial = ctlr->chipset_data;
133 mtx_destroy(&serial->locked_mtx);
134 free(serial, M_ATAPCI);
135 ctlr->chipset_data = NULL;
141 ata_acard_ch_attach(device_t dev)
143 struct ata_channel *ch = device_get_softc(dev);
145 /* setup the usual register normal pci style */
146 if (ata_pci_ch_attach(dev))
149 ch->hw.status = ata_acard_status;
150 ch->flags |= ATA_NO_ATAPI_DMA;
155 ata_acard_status(device_t dev)
157 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
158 struct ata_channel *ch = device_get_softc(dev);
160 if (ctlr->chip->cfg1 == ATP_OLD &&
161 ATA_LOCKING(dev, ATA_LF_WHICH) != ch->unit)
163 if (ch->dma.flags & ATA_DMA_ACTIVE) {
164 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
166 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
167 ATA_BMSTAT_INTERRUPT)
169 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
171 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
172 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
175 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) {
177 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY)
184 ata_acard_850_setmode(device_t dev, int target, int mode)
186 device_t parent = device_get_parent(dev);
187 struct ata_pci_controller *ctlr = device_get_softc(parent);
188 struct ata_channel *ch = device_get_softc(dev);
189 int devno = (ch->unit << 1) + target;
191 mode = min(mode, ctlr->chip->max_dma);
192 /* XXX SOS missing WDMA0+1 + PIO modes */
193 if (mode >= ATA_WDMA2) {
194 u_int8_t reg54 = pci_read_config(parent, 0x54, 1);
196 reg54 &= ~(0x03 << (devno << 1));
197 if (mode >= ATA_UDMA0)
198 reg54 |= (((mode & ATA_MODE_MASK) + 1) << (devno << 1));
199 pci_write_config(parent, 0x54, reg54, 1);
200 pci_write_config(parent, 0x4a, 0xa6, 1);
201 pci_write_config(parent, 0x40 + (devno << 1), 0x0301, 2);
203 /* we could set PIO mode timings, but we assume the BIOS did that */
208 ata_acard_86X_setmode(device_t dev, int target, int mode)
210 device_t parent = device_get_parent(dev);
211 struct ata_pci_controller *ctlr = device_get_softc(parent);
212 struct ata_channel *ch = device_get_softc(dev);
213 int devno = (ch->unit << 1) + target;
215 mode = min(mode, ctlr->chip->max_dma);
216 /* XXX SOS missing WDMA0+1 + PIO modes */
217 if (mode >= ATA_WDMA2) {
218 u_int16_t reg44 = pci_read_config(parent, 0x44, 2);
220 reg44 &= ~(0x000f << (devno << 2));
221 if (mode >= ATA_UDMA0)
222 reg44 |= (((mode & ATA_MODE_MASK) + 1) << (devno << 2));
223 pci_write_config(parent, 0x44, reg44, 2);
224 pci_write_config(parent, 0x4a, 0xa6, 1);
225 pci_write_config(parent, 0x40 + devno, 0x31, 1);
227 /* we could set PIO mode timings, but we assume the BIOS did that */
232 ata_serialize_init(struct ata_serialize *serial)
235 mtx_init(&serial->locked_mtx, "ATA serialize lock", NULL, MTX_DEF);
236 serial->locked_ch = -1;
237 serial->restart_ch = -1;
241 ata_serialize(device_t dev, int flags)
243 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
244 struct ata_channel *ch = device_get_softc(dev);
245 struct ata_serialize *serial;
248 serial = ctlr->chipset_data;
250 mtx_lock(&serial->locked_mtx);
253 if (serial->locked_ch == -1)
254 serial->locked_ch = ch->unit;
255 if (serial->locked_ch != ch->unit)
256 serial->restart_ch = ch->unit;
260 if (serial->locked_ch == ch->unit) {
261 serial->locked_ch = -1;
262 if (serial->restart_ch != -1) {
263 if ((ch = ctlr->interrupt[serial->restart_ch].argument)) {
264 serial->restart_ch = -1;
265 mtx_unlock(&serial->locked_mtx);
276 res = serial->locked_ch;
277 mtx_unlock(&serial->locked_mtx);
281 ATA_DECLARE_DRIVER(ata_acard);