]> CyberLeo.Net >> Repos - FreeBSD/releng/8.0.git/blob - sys/dev/ata/chipsets/ata-acard.c
Adjust to reflect 8.0-RELEASE.
[FreeBSD/releng/8.0.git] / sys / dev / ata / chipsets / ata-acard.c
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 struct ata_serialize {
55     struct mtx  locked_mtx;
56     int         locked_ch;
57     int         restart_ch;
58 };
59
60 /* local prototypes */
61 static int ata_acard_chipinit(device_t dev);
62 static int ata_acard_ch_attach(device_t dev);
63 static int ata_acard_status(device_t dev);
64 static void ata_acard_850_setmode(device_t dev, int mode);
65 static void ata_acard_86X_setmode(device_t dev, int mode);
66 static int ata_serialize(device_t dev, int flags);
67 static void ata_serialize_init(struct ata_serialize *serial);
68
69 /* misc defines */
70 #define ATP_OLD         1
71
72
73 /*
74  * Acard chipset support functions
75  */
76 static int
77 ata_acard_probe(device_t dev)
78 {
79     struct ata_pci_controller *ctlr = device_get_softc(dev);
80     static struct ata_chip_id ids[] =
81     {{ ATA_ATP850R, 0, ATP_OLD, 0x00, ATA_UDMA2, "ATP850" },
82      { ATA_ATP860A, 0, 0,       0x00, ATA_UDMA4, "ATP860A" },
83      { ATA_ATP860R, 0, 0,       0x00, ATA_UDMA4, "ATP860R" },
84      { ATA_ATP865A, 0, 0,       0x00, ATA_UDMA6, "ATP865A" },
85      { ATA_ATP865R, 0, 0,       0x00, ATA_UDMA6, "ATP865R" },
86      { 0, 0, 0, 0, 0, 0}};
87
88     if (pci_get_vendor(dev) != ATA_ACARD_ID)
89         return ENXIO;
90
91     if (!(ctlr->chip = ata_match_chip(dev, ids)))
92         return ENXIO;
93
94     ata_set_desc(dev);
95     ctlr->chipinit = ata_acard_chipinit;
96     return (BUS_PROBE_DEFAULT);
97 }
98
99 static int
100 ata_acard_chipinit(device_t dev)
101 {
102     struct ata_pci_controller *ctlr = device_get_softc(dev);
103     struct ata_serialize *serial;
104
105     if (ata_setup_interrupt(dev, ata_generic_intr))
106         return ENXIO;
107
108     ctlr->ch_attach = ata_acard_ch_attach;
109     ctlr->ch_detach = ata_pci_ch_detach;
110     if (ctlr->chip->cfg1 == ATP_OLD) {
111         ctlr->setmode = ata_acard_850_setmode;
112         ctlr->locking = ata_serialize;
113         serial = malloc(sizeof(struct ata_serialize),
114                               M_TEMP, M_WAITOK | M_ZERO);
115         ata_serialize_init(serial);
116         ctlr->chipset_data = serial;
117     }
118     else
119         ctlr->setmode = ata_acard_86X_setmode;
120     return 0;
121 }
122
123 static int
124 ata_acard_ch_attach(device_t dev)
125 {
126     struct ata_channel *ch = device_get_softc(dev);
127
128     /* setup the usual register normal pci style */
129     if (ata_pci_ch_attach(dev))
130         return ENXIO;
131
132     ch->hw.status = ata_acard_status;
133     return 0;
134 }
135
136 static int
137 ata_acard_status(device_t dev)
138 {
139     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
140     struct ata_channel *ch = device_get_softc(dev);
141
142     if (ctlr->chip->cfg1 == ATP_OLD &&
143         ATA_LOCKING(dev, ATA_LF_WHICH) != ch->unit)
144             return 0;
145     if (ch->dma.flags & ATA_DMA_ACTIVE) {
146         int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
147
148         if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
149             ATA_BMSTAT_INTERRUPT)
150             return 0;
151         ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
152         DELAY(1);
153         ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
154                      ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
155         DELAY(1);
156     }
157     if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) {
158         DELAY(100);
159         if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY)
160             return 0;
161     }
162     return 1;
163 }
164
165 static void
166 ata_acard_850_setmode(device_t dev, int mode)
167 {
168     device_t gparent = GRANDPARENT(dev);
169     struct ata_pci_controller *ctlr = device_get_softc(gparent);
170     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
171     struct ata_device *atadev = device_get_softc(dev);
172     int devno = (ch->unit << 1) + atadev->unit;
173     int error;
174
175     mode = ata_limit_mode(dev, mode,
176                           ata_atapi(dev) ? ATA_PIO_MAX : ctlr->chip->max_dma);
177
178     /* XXX SOS missing WDMA0+1 + PIO modes */
179     if (mode >= ATA_WDMA2) {
180         error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
181         if (bootverbose)
182             device_printf(dev, "%ssetting %s on %s chip\n",
183                           (error) ? "FAILURE " : "",
184                           ata_mode2str(mode), ctlr->chip->text);
185         if (!error) {
186             u_int8_t reg54 = pci_read_config(gparent, 0x54, 1);
187             
188             reg54 &= ~(0x03 << (devno << 1));
189             if (mode >= ATA_UDMA0)
190                 reg54 |= (((mode & ATA_MODE_MASK) + 1) << (devno << 1));
191             pci_write_config(gparent, 0x54, reg54, 1);
192             pci_write_config(gparent, 0x4a, 0xa6, 1);
193             pci_write_config(gparent, 0x40 + (devno << 1), 0x0301, 2);
194             atadev->mode = mode;
195             return;
196         }
197     }
198     /* we could set PIO mode timings, but we assume the BIOS did that */
199 }
200
201 static void
202 ata_acard_86X_setmode(device_t dev, int mode)
203 {
204     device_t gparent = GRANDPARENT(dev);
205     struct ata_pci_controller *ctlr = device_get_softc(gparent);
206     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
207     struct ata_device *atadev = device_get_softc(dev);
208     int devno = (ch->unit << 1) + atadev->unit;
209     int error;
210
211
212     mode = ata_limit_mode(dev, mode,
213                           ata_atapi(dev) ? ATA_PIO_MAX : ctlr->chip->max_dma);
214
215     mode = ata_check_80pin(dev, mode);
216
217     /* XXX SOS missing WDMA0+1 + PIO modes */
218     if (mode >= ATA_WDMA2) {
219         error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
220         if (bootverbose)
221             device_printf(dev, "%ssetting %s on %s chip\n",
222                           (error) ? "FAILURE " : "",
223                           ata_mode2str(mode), ctlr->chip->text);
224         if (!error) {
225             u_int16_t reg44 = pci_read_config(gparent, 0x44, 2);
226             
227             reg44 &= ~(0x000f << (devno << 2));
228             if (mode >= ATA_UDMA0)
229                 reg44 |= (((mode & ATA_MODE_MASK) + 1) << (devno << 2));
230             pci_write_config(gparent, 0x44, reg44, 2);
231             pci_write_config(gparent, 0x4a, 0xa6, 1);
232             pci_write_config(gparent, 0x40 + devno, 0x31, 1);
233             atadev->mode = mode;
234             return;
235         }
236     }
237     /* we could set PIO mode timings, but we assume the BIOS did that */
238 }
239
240 static void
241 ata_serialize_init(struct ata_serialize *serial)
242 {
243
244     mtx_init(&serial->locked_mtx, "ATA serialize lock", NULL, MTX_DEF); 
245     serial->locked_ch = -1;
246     serial->restart_ch = -1;
247 }
248
249 static int
250 ata_serialize(device_t dev, int flags)
251 {
252     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
253     struct ata_channel *ch = device_get_softc(dev);
254     struct ata_serialize *serial;
255     int res;
256
257     serial = ctlr->chipset_data;
258
259     mtx_lock(&serial->locked_mtx);
260     switch (flags) {
261     case ATA_LF_LOCK:
262         if (serial->locked_ch == -1)
263             serial->locked_ch = ch->unit;
264         if (serial->locked_ch != ch->unit)
265             serial->restart_ch = ch->unit;
266         break;
267
268     case ATA_LF_UNLOCK:
269         if (serial->locked_ch == ch->unit) {
270             serial->locked_ch = -1;
271             if (serial->restart_ch != -1) {
272                 if ((ch = ctlr->interrupt[serial->restart_ch].argument)) {
273                     serial->restart_ch = -1;
274                     mtx_unlock(&serial->locked_mtx);
275                     ata_start(dev);
276                     return -1;
277                 }
278             }
279         }
280         break;
281
282     case ATA_LF_WHICH:
283         break;
284     }
285     res = serial->locked_ch;
286     mtx_unlock(&serial->locked_mtx);
287     return res;
288 }
289
290 ATA_DECLARE_DRIVER(ata_acard);