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>
55 struct ata_serialize {
56 struct mtx locked_mtx;
62 /* local prototypes */
63 static int ata_acard_chipinit(device_t dev);
64 static int ata_acard_ch_attach(device_t dev);
65 static int ata_acard_status(device_t dev);
66 static int ata_acard_850_setmode(device_t dev, int target, int mode);
67 static int ata_acard_86X_setmode(device_t dev, int target, int mode);
69 static int ata_acard_chipdeinit(device_t dev);
70 static int ata_serialize(device_t dev, int flags);
71 static void ata_serialize_init(struct ata_serialize *serial);
78 * Acard chipset support functions
81 ata_acard_probe(device_t dev)
83 struct ata_pci_controller *ctlr = device_get_softc(dev);
84 static const struct ata_chip_id ids[] =
85 {{ ATA_ATP850R, 0, ATP_OLD, 0x00, ATA_UDMA2, "ATP850" },
86 { ATA_ATP860A, 0, 0, 0x00, ATA_UDMA4, "ATP860A" },
87 { ATA_ATP860R, 0, 0, 0x00, ATA_UDMA4, "ATP860R" },
88 { ATA_ATP865A, 0, 0, 0x00, ATA_UDMA6, "ATP865A" },
89 { ATA_ATP865R, 0, 0, 0x00, ATA_UDMA6, "ATP865R" },
92 if (pci_get_vendor(dev) != ATA_ACARD_ID)
95 if (!(ctlr->chip = ata_match_chip(dev, ids)))
99 ctlr->chipinit = ata_acard_chipinit;
101 ctlr->chipdeinit = ata_acard_chipdeinit;
103 return (BUS_PROBE_DEFAULT);
107 ata_acard_chipinit(device_t dev)
109 struct ata_pci_controller *ctlr = device_get_softc(dev);
111 struct ata_serialize *serial;
114 if (ata_setup_interrupt(dev, ata_generic_intr))
117 ctlr->ch_attach = ata_acard_ch_attach;
118 ctlr->ch_detach = ata_pci_ch_detach;
119 if (ctlr->chip->cfg1 == ATP_OLD) {
120 ctlr->setmode = ata_acard_850_setmode;
122 ctlr->locking = ata_serialize;
123 serial = malloc(sizeof(struct ata_serialize),
124 M_ATAPCI, M_WAITOK | M_ZERO);
125 ata_serialize_init(serial);
126 ctlr->chipset_data = serial;
128 /* Work around the lack of channel serialization in ATA_CAM. */
130 device_printf(dev, "second channel ignored\n");
134 ctlr->setmode = ata_acard_86X_setmode;
140 ata_acard_chipdeinit(device_t dev)
142 struct ata_pci_controller *ctlr = device_get_softc(dev);
143 struct ata_serialize *serial;
145 if (ctlr->chip->cfg1 == ATP_OLD) {
146 serial = ctlr->chipset_data;
147 mtx_destroy(&serial->locked_mtx);
148 free(serial, M_ATAPCI);
149 ctlr->chipset_data = NULL;
156 ata_acard_ch_attach(device_t dev)
158 struct ata_channel *ch = device_get_softc(dev);
160 /* setup the usual register normal pci style */
161 if (ata_pci_ch_attach(dev))
164 ch->hw.status = ata_acard_status;
165 ch->flags |= ATA_NO_ATAPI_DMA;
170 ata_acard_status(device_t dev)
172 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
173 struct ata_channel *ch = device_get_softc(dev);
175 if (ctlr->chip->cfg1 == ATP_OLD &&
176 ATA_LOCKING(dev, ATA_LF_WHICH) != ch->unit)
178 if (ch->dma.flags & ATA_DMA_ACTIVE) {
179 int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
181 if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
182 ATA_BMSTAT_INTERRUPT)
184 ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
186 ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
187 ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
190 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) {
192 if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY)
199 ata_acard_850_setmode(device_t dev, int target, int mode)
201 device_t parent = device_get_parent(dev);
202 struct ata_pci_controller *ctlr = device_get_softc(parent);
203 struct ata_channel *ch = device_get_softc(dev);
204 int devno = (ch->unit << 1) + target;
206 mode = min(mode, ctlr->chip->max_dma);
207 /* XXX SOS missing WDMA0+1 + PIO modes */
208 if (mode >= ATA_WDMA2) {
209 u_int8_t reg54 = pci_read_config(parent, 0x54, 1);
211 reg54 &= ~(0x03 << (devno << 1));
212 if (mode >= ATA_UDMA0)
213 reg54 |= (((mode & ATA_MODE_MASK) + 1) << (devno << 1));
214 pci_write_config(parent, 0x54, reg54, 1);
215 pci_write_config(parent, 0x4a, 0xa6, 1);
216 pci_write_config(parent, 0x40 + (devno << 1), 0x0301, 2);
218 /* we could set PIO mode timings, but we assume the BIOS did that */
223 ata_acard_86X_setmode(device_t dev, int target, int mode)
225 device_t parent = device_get_parent(dev);
226 struct ata_pci_controller *ctlr = device_get_softc(parent);
227 struct ata_channel *ch = device_get_softc(dev);
228 int devno = (ch->unit << 1) + target;
230 mode = min(mode, ctlr->chip->max_dma);
231 /* XXX SOS missing WDMA0+1 + PIO modes */
232 if (mode >= ATA_WDMA2) {
233 u_int16_t reg44 = pci_read_config(parent, 0x44, 2);
235 reg44 &= ~(0x000f << (devno << 2));
236 if (mode >= ATA_UDMA0)
237 reg44 |= (((mode & ATA_MODE_MASK) + 1) << (devno << 2));
238 pci_write_config(parent, 0x44, reg44, 2);
239 pci_write_config(parent, 0x4a, 0xa6, 1);
240 pci_write_config(parent, 0x40 + devno, 0x31, 1);
242 /* we could set PIO mode timings, but we assume the BIOS did that */
248 ata_serialize_init(struct ata_serialize *serial)
251 mtx_init(&serial->locked_mtx, "ATA serialize lock", NULL, MTX_DEF);
252 serial->locked_ch = -1;
253 serial->restart_ch = -1;
257 ata_serialize(device_t dev, int flags)
259 struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
260 struct ata_channel *ch = device_get_softc(dev);
261 struct ata_serialize *serial;
264 serial = ctlr->chipset_data;
266 mtx_lock(&serial->locked_mtx);
269 if (serial->locked_ch == -1)
270 serial->locked_ch = ch->unit;
271 if (serial->locked_ch != ch->unit)
272 serial->restart_ch = ch->unit;
276 if (serial->locked_ch == ch->unit) {
277 serial->locked_ch = -1;
278 if (serial->restart_ch != -1) {
279 if ((ch = ctlr->interrupt[serial->restart_ch].argument)) {
280 serial->restart_ch = -1;
281 mtx_unlock(&serial->locked_mtx);
292 res = serial->locked_ch;
293 mtx_unlock(&serial->locked_mtx);
298 ATA_DECLARE_DRIVER(ata_acard);