rccide.c revision 1.7.4.4 1 1.7.4.4 skrll /* $NetBSD: rccide.c,v 1.7.4.4 2004/09/18 14:49:06 skrll Exp $ */
2 1.7.4.2 skrll
3 1.7.4.2 skrll /*
4 1.7.4.2 skrll * Copyright (c) 2003 By Noon Software, Inc. All rights reserved.
5 1.7.4.2 skrll *
6 1.7.4.2 skrll * Redistribution and use in source and binary forms, with or without
7 1.7.4.2 skrll * modification, are permitted provided that the following conditions
8 1.7.4.2 skrll * are met:
9 1.7.4.2 skrll * 1. Redistributions of source code must retain the above copyright
10 1.7.4.2 skrll * notice, this list of conditions and the following disclaimer.
11 1.7.4.2 skrll * 2. Redistributions in binary form must reproduce the above copyright
12 1.7.4.2 skrll * notice, this list of conditions and the following disclaimer in the
13 1.7.4.2 skrll * documentation and/or other materials provided with the distribution.
14 1.7.4.2 skrll * 3. The names of the authors may not be used to endorse or promote products
15 1.7.4.2 skrll * derived from this software without specific prior written permission.
16 1.7.4.2 skrll *
17 1.7.4.2 skrll * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
18 1.7.4.2 skrll * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 1.7.4.2 skrll * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 1.7.4.2 skrll * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 1.7.4.2 skrll * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 1.7.4.2 skrll * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 1.7.4.2 skrll * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 1.7.4.2 skrll * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 1.7.4.2 skrll * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 1.7.4.2 skrll * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 1.7.4.2 skrll */
28 1.7.4.2 skrll
29 1.7.4.2 skrll #include <sys/cdefs.h>
30 1.7.4.4 skrll __KERNEL_RCSID(0, "$NetBSD: rccide.c,v 1.7.4.4 2004/09/18 14:49:06 skrll Exp $");
31 1.7.4.2 skrll
32 1.7.4.2 skrll #include <sys/param.h>
33 1.7.4.2 skrll #include <sys/systm.h>
34 1.7.4.2 skrll
35 1.7.4.2 skrll #include <dev/pci/pcivar.h>
36 1.7.4.2 skrll #include <dev/pci/pcidevs.h>
37 1.7.4.2 skrll #include <dev/pci/pciidereg.h>
38 1.7.4.2 skrll #include <dev/pci/pciidevar.h>
39 1.7.4.2 skrll
40 1.7.4.2 skrll static void serverworks_chip_map(struct pciide_softc *,
41 1.7.4.2 skrll struct pci_attach_args *);
42 1.7.4.3 skrll static void serverworks_setup_channel(struct ata_channel *);
43 1.7.4.2 skrll static int serverworks_pci_intr(void *);
44 1.7.4.2 skrll static int serverworkscsb6_pci_intr(void *);
45 1.7.4.2 skrll
46 1.7.4.2 skrll static int rccide_match(struct device *, struct cfdata *, void *);
47 1.7.4.2 skrll static void rccide_attach(struct device *, struct device *, void *);
48 1.7.4.2 skrll
49 1.7.4.2 skrll CFATTACH_DECL(rccide, sizeof(struct pciide_softc),
50 1.7.4.2 skrll rccide_match, rccide_attach, NULL, NULL);
51 1.7.4.2 skrll
52 1.7.4.2 skrll static const struct pciide_product_desc pciide_serverworks_products[] = {
53 1.7.4.2 skrll { PCI_PRODUCT_SERVERWORKS_OSB4_IDE,
54 1.7.4.2 skrll 0,
55 1.7.4.2 skrll "ServerWorks OSB4 IDE Controller",
56 1.7.4.2 skrll serverworks_chip_map,
57 1.7.4.2 skrll },
58 1.7.4.2 skrll { PCI_PRODUCT_SERVERWORKS_CSB5_IDE,
59 1.7.4.2 skrll 0,
60 1.7.4.2 skrll "ServerWorks CSB5 IDE Controller",
61 1.7.4.2 skrll serverworks_chip_map,
62 1.7.4.2 skrll },
63 1.7.4.2 skrll { PCI_PRODUCT_SERVERWORKS_CSB6_IDE,
64 1.7.4.2 skrll 0,
65 1.7.4.2 skrll "ServerWorks CSB6 RAID/IDE Controller",
66 1.7.4.2 skrll serverworks_chip_map,
67 1.7.4.2 skrll },
68 1.7.4.2 skrll { PCI_PRODUCT_SERVERWORKS_CSB6_RAID,
69 1.7.4.2 skrll 0,
70 1.7.4.2 skrll "ServerWorks CSB6 RAID/IDE Controller",
71 1.7.4.2 skrll serverworks_chip_map,
72 1.7.4.2 skrll },
73 1.7.4.2 skrll { 0,
74 1.7.4.2 skrll 0,
75 1.7.4.2 skrll NULL,
76 1.7.4.2 skrll NULL,
77 1.7.4.2 skrll }
78 1.7.4.2 skrll };
79 1.7.4.2 skrll
80 1.7.4.2 skrll static int
81 1.7.4.2 skrll rccide_match(struct device *parent, struct cfdata *match, void *aux)
82 1.7.4.2 skrll {
83 1.7.4.2 skrll struct pci_attach_args *pa = aux;
84 1.7.4.2 skrll
85 1.7.4.2 skrll if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SERVERWORKS &&
86 1.7.4.2 skrll PCI_CLASS(pa->pa_class) == PCI_CLASS_MASS_STORAGE &&
87 1.7.4.2 skrll PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_IDE) {
88 1.7.4.2 skrll if (pciide_lookup_product(pa->pa_id,
89 1.7.4.2 skrll pciide_serverworks_products))
90 1.7.4.2 skrll return (2);
91 1.7.4.2 skrll }
92 1.7.4.2 skrll return (0);
93 1.7.4.2 skrll }
94 1.7.4.2 skrll
95 1.7.4.2 skrll static void
96 1.7.4.2 skrll rccide_attach(struct device *parent, struct device *self, void *aux)
97 1.7.4.2 skrll {
98 1.7.4.2 skrll struct pci_attach_args *pa = aux;
99 1.7.4.2 skrll struct pciide_softc *sc = (void *)self;
100 1.7.4.2 skrll
101 1.7.4.2 skrll pciide_common_attach(sc, pa,
102 1.7.4.2 skrll pciide_lookup_product(pa->pa_id, pciide_serverworks_products));
103 1.7.4.2 skrll }
104 1.7.4.2 skrll
105 1.7.4.2 skrll static void
106 1.7.4.2 skrll serverworks_chip_map(struct pciide_softc *sc, struct pci_attach_args *pa)
107 1.7.4.2 skrll {
108 1.7.4.2 skrll struct pciide_channel *cp;
109 1.7.4.2 skrll pcireg_t interface = PCI_INTERFACE(pa->pa_class);
110 1.7.4.2 skrll pcitag_t pcib_tag;
111 1.7.4.2 skrll int channel;
112 1.7.4.2 skrll bus_size_t cmdsize, ctlsize;
113 1.7.4.2 skrll
114 1.7.4.2 skrll if (pciide_chipen(sc, pa) == 0)
115 1.7.4.2 skrll return;
116 1.7.4.2 skrll
117 1.7.4.2 skrll aprint_normal("%s: bus-master DMA support present",
118 1.7.4.3 skrll sc->sc_wdcdev.sc_atac.atac_dev.dv_xname);
119 1.7.4.2 skrll pciide_mapreg_dma(sc, pa);
120 1.7.4.2 skrll aprint_normal("\n");
121 1.7.4.3 skrll sc->sc_wdcdev.sc_atac.atac_cap = ATAC_CAP_DATA16 | ATAC_CAP_DATA32;
122 1.7.4.2 skrll
123 1.7.4.2 skrll if (sc->sc_dma_ok) {
124 1.7.4.3 skrll sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DMA | ATAC_CAP_UDMA;
125 1.7.4.2 skrll sc->sc_wdcdev.irqack = pciide_irqack;
126 1.7.4.2 skrll }
127 1.7.4.3 skrll sc->sc_wdcdev.sc_atac.atac_pio_cap = 4;
128 1.7.4.3 skrll sc->sc_wdcdev.sc_atac.atac_dma_cap = 2;
129 1.7.4.2 skrll switch (sc->sc_pp->ide_product) {
130 1.7.4.2 skrll case PCI_PRODUCT_SERVERWORKS_OSB4_IDE:
131 1.7.4.3 skrll sc->sc_wdcdev.sc_atac.atac_udma_cap = 2;
132 1.7.4.2 skrll break;
133 1.7.4.2 skrll case PCI_PRODUCT_SERVERWORKS_CSB5_IDE:
134 1.7.4.2 skrll if (PCI_REVISION(pa->pa_class) < 0x92)
135 1.7.4.3 skrll sc->sc_wdcdev.sc_atac.atac_udma_cap = 4;
136 1.7.4.2 skrll else
137 1.7.4.3 skrll sc->sc_wdcdev.sc_atac.atac_udma_cap = 5;
138 1.7.4.2 skrll break;
139 1.7.4.2 skrll case PCI_PRODUCT_SERVERWORKS_CSB6_IDE:
140 1.7.4.2 skrll case PCI_PRODUCT_SERVERWORKS_CSB6_RAID:
141 1.7.4.3 skrll sc->sc_wdcdev.sc_atac.atac_udma_cap = 5;
142 1.7.4.2 skrll break;
143 1.7.4.2 skrll }
144 1.7.4.2 skrll
145 1.7.4.3 skrll sc->sc_wdcdev.sc_atac.atac_set_modes = serverworks_setup_channel;
146 1.7.4.3 skrll sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanarray;
147 1.7.4.3 skrll sc->sc_wdcdev.sc_atac.atac_nchannels = 2;
148 1.7.4.2 skrll
149 1.7.4.3 skrll wdc_allocate_regs(&sc->sc_wdcdev);
150 1.7.4.3 skrll
151 1.7.4.3 skrll for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels;
152 1.7.4.3 skrll channel++) {
153 1.7.4.2 skrll cp = &sc->pciide_channels[channel];
154 1.7.4.2 skrll if (pciide_chansetup(sc, channel, interface) == 0)
155 1.7.4.2 skrll continue;
156 1.7.4.2 skrll switch (sc->sc_pp->ide_product) {
157 1.7.4.2 skrll case PCI_PRODUCT_SERVERWORKS_CSB6_IDE:
158 1.7.4.2 skrll case PCI_PRODUCT_SERVERWORKS_CSB6_RAID:
159 1.7.4.2 skrll pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize,
160 1.7.4.2 skrll serverworkscsb6_pci_intr);
161 1.7.4.2 skrll break;
162 1.7.4.2 skrll default:
163 1.7.4.2 skrll pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize,
164 1.7.4.2 skrll serverworks_pci_intr);
165 1.7.4.2 skrll }
166 1.7.4.2 skrll }
167 1.7.4.2 skrll
168 1.7.4.2 skrll pcib_tag = pci_make_tag(pa->pa_pc, pa->pa_bus, pa->pa_device, 0);
169 1.7.4.2 skrll pci_conf_write(pa->pa_pc, pcib_tag, 0x64,
170 1.7.4.2 skrll (pci_conf_read(pa->pa_pc, pcib_tag, 0x64) & ~0x2000) | 0x4000);
171 1.7.4.2 skrll }
172 1.7.4.2 skrll
173 1.7.4.2 skrll static void
174 1.7.4.3 skrll serverworks_setup_channel(struct ata_channel *chp)
175 1.7.4.2 skrll {
176 1.7.4.2 skrll struct ata_drive_datas *drvp;
177 1.7.4.3 skrll struct atac_softc *atac = chp->ch_atac;
178 1.7.4.3 skrll struct pciide_channel *cp = CHAN_TO_PCHAN(chp);
179 1.7.4.3 skrll struct pciide_softc *sc = CHAN_TO_PCIIDE(chp);
180 1.7.4.2 skrll int channel = chp->ch_channel;
181 1.7.4.3 skrll int drive, unit, s;
182 1.7.4.2 skrll u_int32_t pio_time, dma_time, pio_mode, udma_mode;
183 1.7.4.2 skrll u_int32_t idedma_ctl;
184 1.7.4.2 skrll static const u_int8_t pio_modes[5] = {0x5d, 0x47, 0x34, 0x22, 0x20};
185 1.7.4.2 skrll static const u_int8_t dma_modes[3] = {0x77, 0x21, 0x20};
186 1.7.4.2 skrll
187 1.7.4.2 skrll /* setup DMA if needed */
188 1.7.4.2 skrll pciide_channel_dma_setup(cp);
189 1.7.4.2 skrll
190 1.7.4.2 skrll pio_time = pci_conf_read(sc->sc_pc, sc->sc_tag, 0x40);
191 1.7.4.2 skrll dma_time = pci_conf_read(sc->sc_pc, sc->sc_tag, 0x44);
192 1.7.4.2 skrll pio_mode = pci_conf_read(sc->sc_pc, sc->sc_tag, 0x48);
193 1.7.4.2 skrll udma_mode = pci_conf_read(sc->sc_pc, sc->sc_tag, 0x54);
194 1.7.4.2 skrll
195 1.7.4.2 skrll pio_time &= ~(0xffff << (16 * channel));
196 1.7.4.2 skrll dma_time &= ~(0xffff << (16 * channel));
197 1.7.4.2 skrll pio_mode &= ~(0xff << (8 * channel + 16));
198 1.7.4.2 skrll udma_mode &= ~(0xff << (8 * channel + 16));
199 1.7.4.2 skrll udma_mode &= ~(3 << (2 * channel));
200 1.7.4.2 skrll
201 1.7.4.2 skrll idedma_ctl = 0;
202 1.7.4.2 skrll
203 1.7.4.2 skrll /* Per drive settings */
204 1.7.4.2 skrll for (drive = 0; drive < 2; drive++) {
205 1.7.4.2 skrll drvp = &chp->ch_drive[drive];
206 1.7.4.2 skrll /* If no drive, skip */
207 1.7.4.2 skrll if ((drvp->drive_flags & DRIVE) == 0)
208 1.7.4.2 skrll continue;
209 1.7.4.2 skrll unit = drive + 2 * channel;
210 1.7.4.2 skrll /* add timing values, setup DMA if needed */
211 1.7.4.2 skrll pio_time |= pio_modes[drvp->PIO_mode] << (8 * (unit^1));
212 1.7.4.2 skrll pio_mode |= drvp->PIO_mode << (4 * unit + 16);
213 1.7.4.3 skrll if ((atac->atac_cap & ATAC_CAP_UDMA) &&
214 1.7.4.2 skrll (drvp->drive_flags & DRIVE_UDMA)) {
215 1.7.4.2 skrll /* use Ultra/DMA, check for 80-pin cable */
216 1.7.4.2 skrll if (drvp->UDMA_mode > 2 &&
217 1.7.4.2 skrll (PCI_PRODUCT(pci_conf_read(sc->sc_pc, sc->sc_tag,
218 1.7.4.2 skrll PCI_SUBSYS_ID_REG))
219 1.7.4.2 skrll & (1 << (14 + channel))) == 0)
220 1.7.4.2 skrll drvp->UDMA_mode = 2;
221 1.7.4.2 skrll dma_time |= dma_modes[drvp->DMA_mode] << (8 * (unit^1));
222 1.7.4.2 skrll udma_mode |= drvp->UDMA_mode << (4 * unit + 16);
223 1.7.4.2 skrll udma_mode |= 1 << unit;
224 1.7.4.2 skrll idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
225 1.7.4.3 skrll } else if ((atac->atac_cap & ATAC_CAP_DMA) &&
226 1.7.4.2 skrll (drvp->drive_flags & DRIVE_DMA)) {
227 1.7.4.2 skrll /* use Multiword DMA */
228 1.7.4.3 skrll s = splbio();
229 1.7.4.2 skrll drvp->drive_flags &= ~DRIVE_UDMA;
230 1.7.4.3 skrll splx(s);
231 1.7.4.2 skrll dma_time |= dma_modes[drvp->DMA_mode] << (8 * (unit^1));
232 1.7.4.2 skrll idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
233 1.7.4.2 skrll } else {
234 1.7.4.2 skrll /* PIO only */
235 1.7.4.3 skrll s = splbio();
236 1.7.4.2 skrll drvp->drive_flags &= ~(DRIVE_UDMA | DRIVE_DMA);
237 1.7.4.3 skrll splx(s);
238 1.7.4.2 skrll }
239 1.7.4.2 skrll }
240 1.7.4.2 skrll
241 1.7.4.2 skrll pci_conf_write(sc->sc_pc, sc->sc_tag, 0x40, pio_time);
242 1.7.4.2 skrll pci_conf_write(sc->sc_pc, sc->sc_tag, 0x44, dma_time);
243 1.7.4.2 skrll if (sc->sc_pp->ide_product != PCI_PRODUCT_SERVERWORKS_OSB4_IDE)
244 1.7.4.2 skrll pci_conf_write(sc->sc_pc, sc->sc_tag, 0x48, pio_mode);
245 1.7.4.2 skrll pci_conf_write(sc->sc_pc, sc->sc_tag, 0x54, udma_mode);
246 1.7.4.2 skrll
247 1.7.4.2 skrll if (idedma_ctl != 0) {
248 1.7.4.2 skrll /* Add software bits in status register */
249 1.7.4.2 skrll bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0,
250 1.7.4.2 skrll idedma_ctl);
251 1.7.4.2 skrll }
252 1.7.4.2 skrll }
253 1.7.4.2 skrll
254 1.7.4.2 skrll static int
255 1.7.4.2 skrll serverworks_pci_intr(arg)
256 1.7.4.2 skrll void *arg;
257 1.7.4.2 skrll {
258 1.7.4.2 skrll struct pciide_softc *sc = arg;
259 1.7.4.2 skrll struct pciide_channel *cp;
260 1.7.4.3 skrll struct ata_channel *wdc_cp;
261 1.7.4.2 skrll int rv = 0;
262 1.7.4.2 skrll int dmastat, i, crv;
263 1.7.4.2 skrll
264 1.7.4.3 skrll for (i = 0; i < sc->sc_wdcdev.sc_atac.atac_nchannels; i++) {
265 1.7.4.2 skrll cp = &sc->pciide_channels[i];
266 1.7.4.2 skrll dmastat = bus_space_read_1(sc->sc_dma_iot,
267 1.7.4.2 skrll cp->dma_iohs[IDEDMA_CTL], 0);
268 1.7.4.2 skrll if ((dmastat & (IDEDMA_CTL_ACT | IDEDMA_CTL_INTR)) !=
269 1.7.4.2 skrll IDEDMA_CTL_INTR)
270 1.7.4.2 skrll continue;
271 1.7.4.3 skrll wdc_cp = &cp->ata_channel;
272 1.7.4.2 skrll crv = wdcintr(wdc_cp);
273 1.7.4.2 skrll if (crv == 0) {
274 1.7.4.2 skrll printf("%s:%d: bogus intr\n",
275 1.7.4.3 skrll sc->sc_wdcdev.sc_atac.atac_dev.dv_xname, i);
276 1.7.4.2 skrll bus_space_write_1(sc->sc_dma_iot,
277 1.7.4.2 skrll cp->dma_iohs[IDEDMA_CTL], 0, dmastat);
278 1.7.4.2 skrll } else
279 1.7.4.2 skrll rv = 1;
280 1.7.4.2 skrll }
281 1.7.4.2 skrll return rv;
282 1.7.4.2 skrll }
283 1.7.4.2 skrll
284 1.7.4.2 skrll static int
285 1.7.4.2 skrll serverworkscsb6_pci_intr(arg)
286 1.7.4.2 skrll void *arg;
287 1.7.4.2 skrll {
288 1.7.4.2 skrll struct pciide_softc *sc = arg;
289 1.7.4.2 skrll struct pciide_channel *cp;
290 1.7.4.3 skrll struct ata_channel *wdc_cp;
291 1.7.4.2 skrll int rv = 0;
292 1.7.4.2 skrll int i, crv;
293 1.7.4.2 skrll
294 1.7.4.3 skrll for (i = 0; i < sc->sc_wdcdev.sc_atac.atac_nchannels; i++) {
295 1.7.4.2 skrll cp = &sc->pciide_channels[i];
296 1.7.4.3 skrll wdc_cp = &cp->ata_channel;
297 1.7.4.2 skrll /*
298 1.7.4.2 skrll * The CSB6 doesn't assert IDEDMA_CTL_INTR for non-DMA commands.
299 1.7.4.2 skrll * Until we find a way to know if the controller posted an
300 1.7.4.2 skrll * interrupt, always call wdcintr(), which will try to guess
301 1.7.4.2 skrll * if the interrupt was for us or not (and checks
302 1.7.4.2 skrll * IDEDMA_CTL_INTR for DMA commands only).
303 1.7.4.2 skrll */
304 1.7.4.2 skrll crv = wdcintr(wdc_cp);
305 1.7.4.2 skrll if (crv != 0)
306 1.7.4.2 skrll rv = 1;
307 1.7.4.2 skrll }
308 1.7.4.2 skrll return rv;
309 1.7.4.2 skrll }
310