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