aceride.c revision 1.1 1 1.1 bouyer /* $NetBSD: aceride.c,v 1.1 2003/10/08 11:51:59 bouyer Exp $ */
2 1.1 bouyer
3 1.1 bouyer
4 1.1 bouyer /*
5 1.1 bouyer * Copyright (c) 1999, 2000, 2001 Manuel Bouyer.
6 1.1 bouyer *
7 1.1 bouyer * Redistribution and use in source and binary forms, with or without
8 1.1 bouyer * modification, are permitted provided that the following conditions
9 1.1 bouyer * are met:
10 1.1 bouyer * 1. Redistributions of source code must retain the above copyright
11 1.1 bouyer * notice, this list of conditions and the following disclaimer.
12 1.1 bouyer * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 bouyer * notice, this list of conditions and the following disclaimer in the
14 1.1 bouyer * documentation and/or other materials provided with the distribution.
15 1.1 bouyer * 3. All advertising materials mentioning features or use of this software
16 1.1 bouyer * must display the following acknowledgement:
17 1.1 bouyer * This product includes software developed by Manuel Bouyer.
18 1.1 bouyer * 4. The name of the author may not be used to endorse or promote products
19 1.1 bouyer * derived from this software without specific prior written permission.
20 1.1 bouyer *
21 1.1 bouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 bouyer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 bouyer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 bouyer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 bouyer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 bouyer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 bouyer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 bouyer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 bouyer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.1 bouyer * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 bouyer *
32 1.1 bouyer */
33 1.1 bouyer
34 1.1 bouyer
35 1.1 bouyer #include <sys/param.h>
36 1.1 bouyer #include <sys/systm.h>
37 1.1 bouyer
38 1.1 bouyer #include <dev/pci/pcivar.h>
39 1.1 bouyer #include <dev/pci/pcidevs.h>
40 1.1 bouyer #include <dev/pci/pciidereg.h>
41 1.1 bouyer #include <dev/pci/pciidevar.h>
42 1.1 bouyer #include <dev/pci/pciide_acer_reg.h>
43 1.1 bouyer
44 1.1 bouyer void acer_chip_map __P((struct pciide_softc*, struct pci_attach_args*));
45 1.1 bouyer void acer_setup_channel __P((struct channel_softc*));
46 1.1 bouyer int acer_pci_intr __P((void *));
47 1.1 bouyer
48 1.1 bouyer int aceride_match __P((struct device *, struct cfdata *, void *));
49 1.1 bouyer void aceride_attach __P((struct device *, struct device *, void *));
50 1.1 bouyer
51 1.1 bouyer CFATTACH_DECL(aceride, sizeof(struct pciide_softc),
52 1.1 bouyer aceride_match, aceride_attach, NULL, NULL);
53 1.1 bouyer
54 1.1 bouyer const struct pciide_product_desc pciide_acer_products[] = {
55 1.1 bouyer { PCI_PRODUCT_ALI_M5229,
56 1.1 bouyer 0,
57 1.1 bouyer "Acer Labs M5229 UDMA IDE Controller",
58 1.1 bouyer acer_chip_map,
59 1.1 bouyer },
60 1.1 bouyer { 0,
61 1.1 bouyer 0,
62 1.1 bouyer NULL,
63 1.1 bouyer NULL
64 1.1 bouyer }
65 1.1 bouyer };
66 1.1 bouyer
67 1.1 bouyer int
68 1.1 bouyer aceride_match(parent, match, aux)
69 1.1 bouyer struct device *parent;
70 1.1 bouyer struct cfdata *match;
71 1.1 bouyer void *aux;
72 1.1 bouyer {
73 1.1 bouyer struct pci_attach_args *pa = aux;
74 1.1 bouyer
75 1.1 bouyer if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ALI) {
76 1.1 bouyer if (pciide_lookup_product(pa->pa_id, pciide_acer_products))
77 1.1 bouyer return (2);
78 1.1 bouyer }
79 1.1 bouyer return (0);
80 1.1 bouyer }
81 1.1 bouyer
82 1.1 bouyer void
83 1.1 bouyer aceride_attach(parent, self, aux)
84 1.1 bouyer struct device *parent, *self;
85 1.1 bouyer void *aux;
86 1.1 bouyer {
87 1.1 bouyer struct pci_attach_args *pa = aux;
88 1.1 bouyer struct pciide_softc *sc = (struct pciide_softc *)self;
89 1.1 bouyer
90 1.1 bouyer pciide_common_attach(sc, pa,
91 1.1 bouyer pciide_lookup_product(pa->pa_id, pciide_acer_products));
92 1.1 bouyer
93 1.1 bouyer }
94 1.1 bouyer
95 1.1 bouyer void
96 1.1 bouyer acer_chip_map(sc, pa)
97 1.1 bouyer struct pciide_softc *sc;
98 1.1 bouyer struct pci_attach_args *pa;
99 1.1 bouyer {
100 1.1 bouyer struct pciide_channel *cp;
101 1.1 bouyer int channel;
102 1.1 bouyer pcireg_t cr, interface;
103 1.1 bouyer bus_size_t cmdsize, ctlsize;
104 1.1 bouyer pcireg_t rev = PCI_REVISION(pa->pa_class);
105 1.1 bouyer
106 1.1 bouyer if (pciide_chipen(sc, pa) == 0)
107 1.1 bouyer return;
108 1.1 bouyer
109 1.1 bouyer aprint_normal("%s: bus-master DMA support present",
110 1.1 bouyer sc->sc_wdcdev.sc_dev.dv_xname);
111 1.1 bouyer pciide_mapreg_dma(sc, pa);
112 1.1 bouyer aprint_normal("\n");
113 1.1 bouyer sc->sc_wdcdev.cap = WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32 |
114 1.1 bouyer WDC_CAPABILITY_MODE;
115 1.1 bouyer if (sc->sc_dma_ok) {
116 1.1 bouyer sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA;
117 1.1 bouyer if (rev >= 0x20) {
118 1.1 bouyer sc->sc_wdcdev.cap |= WDC_CAPABILITY_UDMA;
119 1.1 bouyer if (rev >= 0xC4)
120 1.1 bouyer sc->sc_wdcdev.UDMA_cap = 5;
121 1.1 bouyer else if (rev >= 0xC2)
122 1.1 bouyer sc->sc_wdcdev.UDMA_cap = 4;
123 1.1 bouyer else
124 1.1 bouyer sc->sc_wdcdev.UDMA_cap = 2;
125 1.1 bouyer }
126 1.1 bouyer sc->sc_wdcdev.cap |= WDC_CAPABILITY_IRQACK;
127 1.1 bouyer sc->sc_wdcdev.irqack = pciide_irqack;
128 1.1 bouyer }
129 1.1 bouyer
130 1.1 bouyer sc->sc_wdcdev.PIO_cap = 4;
131 1.1 bouyer sc->sc_wdcdev.DMA_cap = 2;
132 1.1 bouyer sc->sc_wdcdev.set_modes = acer_setup_channel;
133 1.1 bouyer sc->sc_wdcdev.channels = sc->wdc_chanarray;
134 1.1 bouyer sc->sc_wdcdev.nchannels = PCIIDE_NUM_CHANNELS;
135 1.1 bouyer
136 1.1 bouyer pciide_pci_write(sc->sc_pc, sc->sc_tag, ACER_CDRC,
137 1.1 bouyer (pciide_pci_read(sc->sc_pc, sc->sc_tag, ACER_CDRC) |
138 1.1 bouyer ACER_CDRC_DMA_EN) & ~ACER_CDRC_FIFO_DISABLE);
139 1.1 bouyer
140 1.1 bouyer /* Enable "microsoft register bits" R/W. */
141 1.1 bouyer pciide_pci_write(sc->sc_pc, sc->sc_tag, ACER_CCAR3,
142 1.1 bouyer pciide_pci_read(sc->sc_pc, sc->sc_tag, ACER_CCAR3) | ACER_CCAR3_PI);
143 1.1 bouyer pciide_pci_write(sc->sc_pc, sc->sc_tag, ACER_CCAR1,
144 1.1 bouyer pciide_pci_read(sc->sc_pc, sc->sc_tag, ACER_CCAR1) &
145 1.1 bouyer ~(ACER_CHANSTATUS_RO|PCIIDE_CHAN_RO(0)|PCIIDE_CHAN_RO(1)));
146 1.1 bouyer pciide_pci_write(sc->sc_pc, sc->sc_tag, ACER_CCAR2,
147 1.1 bouyer pciide_pci_read(sc->sc_pc, sc->sc_tag, ACER_CCAR2) &
148 1.1 bouyer ~ACER_CHANSTATUSREGS_RO);
149 1.1 bouyer cr = pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG);
150 1.1 bouyer cr |= (PCIIDE_CHANSTATUS_EN << PCI_INTERFACE_SHIFT);
151 1.1 bouyer pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG, cr);
152 1.1 bouyer /* Don't use cr, re-read the real register content instead */
153 1.1 bouyer interface = PCI_INTERFACE(pci_conf_read(sc->sc_pc, sc->sc_tag,
154 1.1 bouyer PCI_CLASS_REG));
155 1.1 bouyer
156 1.1 bouyer /* From linux: enable "Cable Detection" */
157 1.1 bouyer if (rev >= 0xC2) {
158 1.1 bouyer pciide_pci_write(sc->sc_pc, sc->sc_tag, ACER_0x4B,
159 1.1 bouyer pciide_pci_read(sc->sc_pc, sc->sc_tag, ACER_0x4B)
160 1.1 bouyer | ACER_0x4B_CDETECT);
161 1.1 bouyer }
162 1.1 bouyer
163 1.1 bouyer for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
164 1.1 bouyer cp = &sc->pciide_channels[channel];
165 1.1 bouyer if (pciide_chansetup(sc, channel, interface) == 0)
166 1.1 bouyer continue;
167 1.1 bouyer if ((interface & PCIIDE_CHAN_EN(channel)) == 0) {
168 1.1 bouyer aprint_normal("%s: %s channel ignored (disabled)\n",
169 1.1 bouyer sc->sc_wdcdev.sc_dev.dv_xname, cp->name);
170 1.1 bouyer cp->wdc_channel.ch_flags |= WDCF_DISABLED;
171 1.1 bouyer continue;
172 1.1 bouyer }
173 1.1 bouyer /* newer controllers seems to lack the ACER_CHIDS. Sigh */
174 1.1 bouyer pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize,
175 1.1 bouyer (rev >= 0xC2) ? pciide_pci_intr : acer_pci_intr);
176 1.1 bouyer }
177 1.1 bouyer }
178 1.1 bouyer
179 1.1 bouyer void
180 1.1 bouyer acer_setup_channel(chp)
181 1.1 bouyer struct channel_softc *chp;
182 1.1 bouyer {
183 1.1 bouyer struct ata_drive_datas *drvp;
184 1.1 bouyer int drive;
185 1.1 bouyer u_int32_t acer_fifo_udma;
186 1.1 bouyer u_int32_t idedma_ctl;
187 1.1 bouyer struct pciide_channel *cp = (struct pciide_channel*)chp;
188 1.1 bouyer struct pciide_softc *sc = (struct pciide_softc *)cp->wdc_channel.wdc;
189 1.1 bouyer
190 1.1 bouyer idedma_ctl = 0;
191 1.1 bouyer acer_fifo_udma = pci_conf_read(sc->sc_pc, sc->sc_tag, ACER_FTH_UDMA);
192 1.1 bouyer WDCDEBUG_PRINT(("acer_setup_channel: old fifo/udma reg 0x%x\n",
193 1.1 bouyer acer_fifo_udma), DEBUG_PROBE);
194 1.1 bouyer /* setup DMA if needed */
195 1.1 bouyer pciide_channel_dma_setup(cp);
196 1.1 bouyer
197 1.1 bouyer if ((chp->ch_drive[0].drive_flags | chp->ch_drive[1].drive_flags) &
198 1.1 bouyer DRIVE_UDMA) { /* check 80 pins cable */
199 1.1 bouyer if (pciide_pci_read(sc->sc_pc, sc->sc_tag, ACER_0x4A) &
200 1.1 bouyer ACER_0x4A_80PIN(chp->channel)) {
201 1.1 bouyer if (chp->ch_drive[0].UDMA_mode > 2)
202 1.1 bouyer chp->ch_drive[0].UDMA_mode = 2;
203 1.1 bouyer if (chp->ch_drive[1].UDMA_mode > 2)
204 1.1 bouyer chp->ch_drive[1].UDMA_mode = 2;
205 1.1 bouyer }
206 1.1 bouyer }
207 1.1 bouyer
208 1.1 bouyer for (drive = 0; drive < 2; drive++) {
209 1.1 bouyer drvp = &chp->ch_drive[drive];
210 1.1 bouyer /* If no drive, skip */
211 1.1 bouyer if ((drvp->drive_flags & DRIVE) == 0)
212 1.1 bouyer continue;
213 1.1 bouyer WDCDEBUG_PRINT(("acer_setup_channel: old timings reg for "
214 1.1 bouyer "channel %d drive %d 0x%x\n", chp->channel, drive,
215 1.1 bouyer pciide_pci_read(sc->sc_pc, sc->sc_tag,
216 1.1 bouyer ACER_IDETIM(chp->channel, drive))), DEBUG_PROBE);
217 1.1 bouyer /* clear FIFO/DMA mode */
218 1.1 bouyer acer_fifo_udma &= ~(ACER_FTH_OPL(chp->channel, drive, 0x3) |
219 1.1 bouyer ACER_UDMA_EN(chp->channel, drive) |
220 1.1 bouyer ACER_UDMA_TIM(chp->channel, drive, 0x7));
221 1.1 bouyer
222 1.1 bouyer /* add timing values, setup DMA if needed */
223 1.1 bouyer if ((drvp->drive_flags & DRIVE_DMA) == 0 &&
224 1.1 bouyer (drvp->drive_flags & DRIVE_UDMA) == 0) {
225 1.1 bouyer acer_fifo_udma |=
226 1.1 bouyer ACER_FTH_OPL(chp->channel, drive, 0x1);
227 1.1 bouyer goto pio;
228 1.1 bouyer }
229 1.1 bouyer
230 1.1 bouyer acer_fifo_udma |= ACER_FTH_OPL(chp->channel, drive, 0x2);
231 1.1 bouyer if (drvp->drive_flags & DRIVE_UDMA) {
232 1.1 bouyer /* use Ultra/DMA */
233 1.1 bouyer drvp->drive_flags &= ~DRIVE_DMA;
234 1.1 bouyer acer_fifo_udma |= ACER_UDMA_EN(chp->channel, drive);
235 1.1 bouyer acer_fifo_udma |=
236 1.1 bouyer ACER_UDMA_TIM(chp->channel, drive,
237 1.1 bouyer acer_udma[drvp->UDMA_mode]);
238 1.1 bouyer /* XXX disable if one drive < UDMA3 ? */
239 1.1 bouyer if (drvp->UDMA_mode >= 3) {
240 1.1 bouyer pciide_pci_write(sc->sc_pc, sc->sc_tag,
241 1.1 bouyer ACER_0x4B,
242 1.1 bouyer pciide_pci_read(sc->sc_pc, sc->sc_tag,
243 1.1 bouyer ACER_0x4B) | ACER_0x4B_UDMA66);
244 1.1 bouyer }
245 1.1 bouyer } else {
246 1.1 bouyer /*
247 1.1 bouyer * use Multiword DMA
248 1.1 bouyer * Timings will be used for both PIO and DMA,
249 1.1 bouyer * so adjust DMA mode if needed
250 1.1 bouyer */
251 1.1 bouyer if (drvp->PIO_mode > (drvp->DMA_mode + 2))
252 1.1 bouyer drvp->PIO_mode = drvp->DMA_mode + 2;
253 1.1 bouyer if (drvp->DMA_mode + 2 > (drvp->PIO_mode))
254 1.1 bouyer drvp->DMA_mode = (drvp->PIO_mode > 2) ?
255 1.1 bouyer drvp->PIO_mode - 2 : 0;
256 1.1 bouyer if (drvp->DMA_mode == 0)
257 1.1 bouyer drvp->PIO_mode = 0;
258 1.1 bouyer }
259 1.1 bouyer idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
260 1.1 bouyer pio: pciide_pci_write(sc->sc_pc, sc->sc_tag,
261 1.1 bouyer ACER_IDETIM(chp->channel, drive),
262 1.1 bouyer acer_pio[drvp->PIO_mode]);
263 1.1 bouyer }
264 1.1 bouyer WDCDEBUG_PRINT(("acer_setup_channel: new fifo/udma reg 0x%x\n",
265 1.1 bouyer acer_fifo_udma), DEBUG_PROBE);
266 1.1 bouyer pci_conf_write(sc->sc_pc, sc->sc_tag, ACER_FTH_UDMA, acer_fifo_udma);
267 1.1 bouyer if (idedma_ctl != 0) {
268 1.1 bouyer /* Add software bits in status register */
269 1.1 bouyer bus_space_write_1(sc->sc_dma_iot, sc->sc_dma_ioh,
270 1.1 bouyer IDEDMA_CTL + (IDEDMA_SCH_OFFSET * chp->channel),
271 1.1 bouyer idedma_ctl);
272 1.1 bouyer }
273 1.1 bouyer }
274 1.1 bouyer
275 1.1 bouyer int
276 1.1 bouyer acer_pci_intr(arg)
277 1.1 bouyer void *arg;
278 1.1 bouyer {
279 1.1 bouyer struct pciide_softc *sc = arg;
280 1.1 bouyer struct pciide_channel *cp;
281 1.1 bouyer struct channel_softc *wdc_cp;
282 1.1 bouyer int i, rv, crv;
283 1.1 bouyer u_int32_t chids;
284 1.1 bouyer
285 1.1 bouyer rv = 0;
286 1.1 bouyer chids = pciide_pci_read(sc->sc_pc, sc->sc_tag, ACER_CHIDS);
287 1.1 bouyer for (i = 0; i < sc->sc_wdcdev.nchannels; i++) {
288 1.1 bouyer cp = &sc->pciide_channels[i];
289 1.1 bouyer wdc_cp = &cp->wdc_channel;
290 1.1 bouyer /* If a compat channel skip. */
291 1.1 bouyer if (cp->compat)
292 1.1 bouyer continue;
293 1.1 bouyer if (chids & ACER_CHIDS_INT(i)) {
294 1.1 bouyer crv = wdcintr(wdc_cp);
295 1.1 bouyer if (crv == 0)
296 1.1 bouyer printf("%s:%d: bogus intr\n",
297 1.1 bouyer sc->sc_wdcdev.sc_dev.dv_xname, i);
298 1.1 bouyer else
299 1.1 bouyer rv = 1;
300 1.1 bouyer }
301 1.1 bouyer }
302 1.1 bouyer return rv;
303 1.1 bouyer }
304