]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/dev/ata/chipsets/ata-acard.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.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 int ata_acard_850_setmode(device_t dev, int target, int mode);
65 static int ata_acard_86X_setmode(device_t dev, int target, 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     ch->flags |= ATA_NO_ATAPI_DMA;
134     return 0;
135 }
136
137 static int
138 ata_acard_status(device_t dev)
139 {
140     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
141     struct ata_channel *ch = device_get_softc(dev);
142
143     if (ctlr->chip->cfg1 == ATP_OLD &&
144         ATA_LOCKING(dev, ATA_LF_WHICH) != ch->unit)
145             return 0;
146     if (ch->dma.flags & ATA_DMA_ACTIVE) {
147         int bmstat = ATA_IDX_INB(ch, ATA_BMSTAT_PORT) & ATA_BMSTAT_MASK;
148
149         if ((bmstat & (ATA_BMSTAT_ACTIVE | ATA_BMSTAT_INTERRUPT)) !=
150             ATA_BMSTAT_INTERRUPT)
151             return 0;
152         ATA_IDX_OUTB(ch, ATA_BMSTAT_PORT, bmstat & ~ATA_BMSTAT_ERROR);
153         DELAY(1);
154         ATA_IDX_OUTB(ch, ATA_BMCMD_PORT,
155                      ATA_IDX_INB(ch, ATA_BMCMD_PORT) & ~ATA_BMCMD_START_STOP);
156         DELAY(1);
157     }
158     if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY) {
159         DELAY(100);
160         if (ATA_IDX_INB(ch, ATA_ALTSTAT) & ATA_S_BUSY)
161             return 0;
162     }
163     return 1;
164 }
165
166 static int
167 ata_acard_850_setmode(device_t dev, int target, int mode)
168 {
169     device_t parent = device_get_parent(dev);
170     struct ata_pci_controller *ctlr = device_get_softc(parent);
171     struct ata_channel *ch = device_get_softc(dev);
172     int devno = (ch->unit << 1) + target;
173
174     mode = min(mode, ctlr->chip->max_dma);
175     /* XXX SOS missing WDMA0+1 + PIO modes */
176     if (mode >= ATA_WDMA2) {
177             u_int8_t reg54 = pci_read_config(parent, 0x54, 1);
178             
179             reg54 &= ~(0x03 << (devno << 1));
180             if (mode >= ATA_UDMA0)
181                 reg54 |= (((mode & ATA_MODE_MASK) + 1) << (devno << 1));
182             pci_write_config(parent, 0x54, reg54, 1);
183             pci_write_config(parent, 0x4a, 0xa6, 1);
184             pci_write_config(parent, 0x40 + (devno << 1), 0x0301, 2);
185     }
186     /* we could set PIO mode timings, but we assume the BIOS did that */
187     return (mode);
188 }
189
190 static int
191 ata_acard_86X_setmode(device_t dev, int target, int mode)
192 {
193         device_t parent = device_get_parent(dev);
194         struct ata_pci_controller *ctlr = device_get_softc(parent);
195         struct ata_channel *ch = device_get_softc(dev);
196         int devno = (ch->unit << 1) + target;
197
198         mode = min(mode, ctlr->chip->max_dma);
199         /* XXX SOS missing WDMA0+1 + PIO modes */
200         if (mode >= ATA_WDMA2) {
201                 u_int16_t reg44 = pci_read_config(parent, 0x44, 2);
202             
203                 reg44 &= ~(0x000f << (devno << 2));
204                 if (mode >= ATA_UDMA0)
205                         reg44 |= (((mode & ATA_MODE_MASK) + 1) << (devno << 2));
206                 pci_write_config(parent, 0x44, reg44, 2);
207                 pci_write_config(parent, 0x4a, 0xa6, 1);
208                 pci_write_config(parent, 0x40 + devno, 0x31, 1);
209         }
210         /* we could set PIO mode timings, but we assume the BIOS did that */
211         return (mode);
212 }
213
214 static void
215 ata_serialize_init(struct ata_serialize *serial)
216 {
217
218     mtx_init(&serial->locked_mtx, "ATA serialize lock", NULL, MTX_DEF); 
219     serial->locked_ch = -1;
220     serial->restart_ch = -1;
221 }
222
223 static int
224 ata_serialize(device_t dev, int flags)
225 {
226     struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
227     struct ata_channel *ch = device_get_softc(dev);
228     struct ata_serialize *serial;
229     int res;
230
231     serial = ctlr->chipset_data;
232
233     mtx_lock(&serial->locked_mtx);
234     switch (flags) {
235     case ATA_LF_LOCK:
236         if (serial->locked_ch == -1)
237             serial->locked_ch = ch->unit;
238         if (serial->locked_ch != ch->unit)
239             serial->restart_ch = ch->unit;
240         break;
241
242     case ATA_LF_UNLOCK:
243         if (serial->locked_ch == ch->unit) {
244             serial->locked_ch = -1;
245             if (serial->restart_ch != -1) {
246                 if ((ch = ctlr->interrupt[serial->restart_ch].argument)) {
247                     serial->restart_ch = -1;
248                     mtx_unlock(&serial->locked_mtx);
249                     ata_start(dev);
250                     return -1;
251                 }
252             }
253         }
254         break;
255
256     case ATA_LF_WHICH:
257         break;
258     }
259     res = serial->locked_ch;
260     mtx_unlock(&serial->locked_mtx);
261     return res;
262 }
263
264 ATA_DECLARE_DRIVER(ata_acard);