xref: /freebsd/sys/dev/ata/chipsets/ata-acard.c (revision c243e4902be8df1e643c76b5f18b68bb77cc5268)
1 /*-
2  * Copyright (c) 1998 - 2008 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  *
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.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include "opt_ata.h"
31 #include <sys/param.h>
32 #include <sys/module.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/ata.h>
36 #include <sys/bus.h>
37 #include <sys/endian.h>
38 #include <sys/malloc.h>
39 #include <sys/lock.h>
40 #include <sys/mutex.h>
41 #include <sys/sema.h>
42 #include <sys/taskqueue.h>
43 #include <vm/uma.h>
44 #include <machine/stdarg.h>
45 #include <machine/resource.h>
46 #include <machine/bus.h>
47 #include <sys/rman.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>
52 #include <ata_if.h>
53 
54 #ifndef ATA_CAM
55 struct ata_serialize {
56     struct mtx  locked_mtx;
57     int         locked_ch;
58     int         restart_ch;
59 };
60 #endif
61 
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);
68 #ifndef ATA_CAM
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);
72 #endif
73 
74 /* misc defines */
75 #define ATP_OLD		1
76 
77 /*
78  * Acard chipset support functions
79  */
80 static int
81 ata_acard_probe(device_t dev)
82 {
83     struct ata_pci_controller *ctlr = device_get_softc(dev);
84     static const struct ata_chip_id const 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" },
90      { 0, 0, 0, 0, 0, 0}};
91 
92     if (pci_get_vendor(dev) != ATA_ACARD_ID)
93 	return ENXIO;
94 
95     if (!(ctlr->chip = ata_match_chip(dev, ids)))
96 	return ENXIO;
97 
98     ata_set_desc(dev);
99     ctlr->chipinit = ata_acard_chipinit;
100 #ifndef ATA_CAM
101     ctlr->chipdeinit = ata_acard_chipdeinit;
102 #endif
103     return (BUS_PROBE_DEFAULT);
104 }
105 
106 static int
107 ata_acard_chipinit(device_t dev)
108 {
109     struct ata_pci_controller *ctlr = device_get_softc(dev);
110 #ifndef ATA_CAM
111     struct ata_serialize *serial;
112 #endif
113 
114     if (ata_setup_interrupt(dev, ata_generic_intr))
115 	return ENXIO;
116 
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;
121 #ifndef ATA_CAM
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;
127 #endif
128     }
129     else
130 	ctlr->setmode = ata_acard_86X_setmode;
131     return 0;
132 }
133 
134 #ifndef ATA_CAM
135 static int
136 ata_acard_chipdeinit(device_t dev)
137 {
138 	struct ata_pci_controller *ctlr = device_get_softc(dev);
139 	struct ata_serialize *serial;
140 
141 	if (ctlr->chip->cfg1 == ATP_OLD) {
142 		serial = ctlr->chipset_data;
143 		mtx_destroy(&serial->locked_mtx);
144 		free(serial, M_ATAPCI);
145 		ctlr->chipset_data = NULL;
146 	}
147 	return (0);
148 }
149 #endif
150 
151 static int
152 ata_acard_ch_attach(device_t dev)
153 {
154     struct ata_channel *ch = device_get_softc(dev);
155 
156     /* setup the usual register normal pci style */
157     if (ata_pci_ch_attach(dev))
158 	return ENXIO;
159 
160     ch->hw.status = ata_acard_status;
161     ch->flags |= ATA_NO_ATAPI_DMA;
162     return 0;
163 }
164 
165 static int
166 ata_acard_status(device_t dev)
167 {
168     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
169     struct ata_channel *ch = device_get_softc(dev);
170 
171     if (ctlr->chip->cfg1 == ATP_OLD &&
172 	ATA_LOCKING(dev, ATA_LF_WHICH) != ch->unit)
173 	    return 0;
174     if (ch->dma.flags & ATA_DMA_ACTIVE) {
175 	int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
176 
177 	if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
178 	    ATA_BMSTAT_INTERRUPT)
179 	    return 0;
180 	ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
181 	DELAY(1);
182 	ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
183 		     ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
184 	DELAY(1);
185     }
186     if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) {
187 	DELAY(100);
188 	if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY)
189 	    return 0;
190     }
191     return 1;
192 }
193 
194 static int
195 ata_acard_850_setmode(device_t dev, int target, int mode)
196 {
197     device_t parent = device_get_parent(dev);
198     struct ata_pci_controller *ctlr = device_get_softc(parent);
199     struct ata_channel *ch = device_get_softc(dev);
200     int devno = (ch->unit << 1) + target;
201 
202     mode = min(mode, ctlr->chip->max_dma);
203     /* XXX SOS missing WDMA0+1 + PIO modes */
204     if (mode >= ATA_WDMA2) {
205 	    u_int8_t reg54 = pci_read_config(parent, 0x54, 1);
206 
207 	    reg54 &= ~(0x03 << (devno << 1));
208 	    if (mode >= ATA_UDMA0)
209 		reg54 |= (((mode & ATA_MODE_MASK) + 1) << (devno << 1));
210 	    pci_write_config(parent, 0x54, reg54, 1);
211 	    pci_write_config(parent, 0x4a, 0xa6, 1);
212 	    pci_write_config(parent, 0x40 + (devno << 1), 0x0301, 2);
213     }
214     /* we could set PIO mode timings, but we assume the BIOS did that */
215     return (mode);
216 }
217 
218 static int
219 ata_acard_86X_setmode(device_t dev, int target, int mode)
220 {
221 	device_t parent = device_get_parent(dev);
222 	struct ata_pci_controller *ctlr = device_get_softc(parent);
223 	struct ata_channel *ch = device_get_softc(dev);
224 	int devno = (ch->unit << 1) + target;
225 
226 	mode = min(mode, ctlr->chip->max_dma);
227 	/* XXX SOS missing WDMA0+1 + PIO modes */
228 	if (mode >= ATA_WDMA2) {
229 		u_int16_t reg44 = pci_read_config(parent, 0x44, 2);
230 
231 		reg44 &= ~(0x000f << (devno << 2));
232 		if (mode >= ATA_UDMA0)
233 			reg44 |= (((mode & ATA_MODE_MASK) + 1) << (devno << 2));
234 		pci_write_config(parent, 0x44, reg44, 2);
235 		pci_write_config(parent, 0x4a, 0xa6, 1);
236 		pci_write_config(parent, 0x40 + devno, 0x31, 1);
237 	}
238 	/* we could set PIO mode timings, but we assume the BIOS did that */
239 	return (mode);
240 }
241 
242 #ifndef ATA_CAM
243 static void
244 ata_serialize_init(struct ata_serialize *serial)
245 {
246 
247     mtx_init(&serial->locked_mtx, "ATA serialize lock", NULL, MTX_DEF);
248     serial->locked_ch = -1;
249     serial->restart_ch = -1;
250 }
251 
252 static int
253 ata_serialize(device_t dev, int flags)
254 {
255     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
256     struct ata_channel *ch = device_get_softc(dev);
257     struct ata_serialize *serial;
258     int res;
259 
260     serial = ctlr->chipset_data;
261 
262     mtx_lock(&serial->locked_mtx);
263     switch (flags) {
264     case ATA_LF_LOCK:
265 	if (serial->locked_ch == -1)
266 	    serial->locked_ch = ch->unit;
267 	if (serial->locked_ch != ch->unit)
268 	    serial->restart_ch = ch->unit;
269 	break;
270 
271     case ATA_LF_UNLOCK:
272 	if (serial->locked_ch == ch->unit) {
273 	    serial->locked_ch = -1;
274 	    if (serial->restart_ch != -1) {
275 		if ((ch = ctlr->interrupt[serial->restart_ch].argument)) {
276 		    serial->restart_ch = -1;
277 		    mtx_unlock(&serial->locked_mtx);
278 		    ata_start(dev);
279 		    return -1;
280 		}
281 	    }
282 	}
283 	break;
284 
285     case ATA_LF_WHICH:
286 	break;
287     }
288     res = serial->locked_ch;
289     mtx_unlock(&serial->locked_mtx);
290     return res;
291 }
292 #endif
293 
294 ATA_DECLARE_DRIVER(ata_acard);
295