xref: /netbsd/sys/dev/pci/hptide.c (revision 6550d01e)
1 /*	$NetBSD: hptide.c,v 1.27 2010/11/05 18:07:24 jakllsch 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  *
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 __KERNEL_RCSID(0, "$NetBSD: hptide.c,v 1.27 2010/11/05 18:07:24 jakllsch Exp $");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 
33 #include <dev/pci/pcivar.h>
34 #include <dev/pci/pcidevs.h>
35 #include <dev/pci/pciidereg.h>
36 #include <dev/pci/pciidevar.h>
37 #include <dev/pci/pciide_hpt_reg.h>
38 
39 static void hpt_chip_map(struct pciide_softc*, struct pci_attach_args*);
40 static void hpt_setup_channel(struct ata_channel*);
41 static int  hpt_pci_intr(void *);
42 
43 static int  hptide_match(device_t, cfdata_t, void *);
44 static void hptide_attach(device_t, device_t, void *);
45 
46 CFATTACH_DECL_NEW(hptide, sizeof(struct pciide_softc),
47     hptide_match, hptide_attach, NULL, NULL);
48 
49 static const struct pciide_product_desc pciide_triones_products[] =  {
50 	{ PCI_PRODUCT_TRIONES_HPT302,
51 	  0,
52 	  NULL,
53 	  hpt_chip_map
54 	},
55 	{ PCI_PRODUCT_TRIONES_HPT366,
56 	  0,
57 	  NULL,
58 	  hpt_chip_map,
59 	},
60 	{ PCI_PRODUCT_TRIONES_HPT371,
61 	  0,
62 	  NULL,
63 	  hpt_chip_map,
64 	},
65 	{ PCI_PRODUCT_TRIONES_HPT372A,
66 	  0,
67 	  NULL,
68 	  hpt_chip_map
69 	},
70 	{ PCI_PRODUCT_TRIONES_HPT374,
71 	  0,
72 	  NULL,
73 	  hpt_chip_map
74 	},
75 	{ 0,
76 	  0,
77 	  NULL,
78 	  NULL
79 	}
80 };
81 
82 static int
83 hptide_match(device_t parent, cfdata_t match, void *aux)
84 {
85 	struct pci_attach_args *pa = aux;
86 
87 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_TRIONES) {
88 		if (pciide_lookup_product(pa->pa_id, pciide_triones_products))
89 			return (2);
90 	}
91 	return (0);
92 }
93 
94 static void
95 hptide_attach(device_t parent, device_t self, void *aux)
96 {
97 	struct pci_attach_args *pa = aux;
98 	struct pciide_softc *sc = device_private(self);
99 
100 	sc->sc_wdcdev.sc_atac.atac_dev = self;
101 
102 	pciide_common_attach(sc, pa,
103 	    pciide_lookup_product(pa->pa_id, pciide_triones_products));
104 
105 }
106 
107 static void
108 hpt_chip_map(struct pciide_softc *sc, struct pci_attach_args *pa)
109 {
110 	struct pciide_channel *cp;
111 	int i, compatchan, revision;
112 	pcireg_t interface;
113 
114 	if (pciide_chipen(sc, pa) == 0)
115 		return;
116 
117 	revision = PCI_REVISION(pa->pa_class);
118 	aprint_normal_dev(sc->sc_wdcdev.sc_atac.atac_dev,
119 	    "Triones/Highpoint ");
120 	switch (sc->sc_pp->ide_product) {
121 	case PCI_PRODUCT_TRIONES_HPT302:
122 		aprint_normal("HPT302 IDE Controller\n");
123 		break;
124 	case PCI_PRODUCT_TRIONES_HPT371:
125 		aprint_normal("HPT371 IDE Controller\n");
126 		break;
127 	case PCI_PRODUCT_TRIONES_HPT374:
128 		aprint_normal("HPT374 IDE Controller\n");
129 		break;
130 	case PCI_PRODUCT_TRIONES_HPT372A:
131 		aprint_normal("HPT372A IDE Controller\n");
132 		break;
133 	case PCI_PRODUCT_TRIONES_HPT366:
134 		if (revision == HPT372_REV)
135 			aprint_normal("HPT372 IDE Controller\n");
136 		else if (revision == HPT370_REV)
137 			aprint_normal("HPT370 IDE Controller\n");
138 		else if (revision == HPT370A_REV)
139 			aprint_normal("HPT370A IDE Controller\n");
140 		else if (revision == HPT368_REV)
141 			aprint_normal("HPT368 IDE Controller\n");
142 		else if (revision == HPT366_REV)
143 			aprint_normal("HPT366 IDE Controller\n");
144 		else
145 			aprint_normal("unknown HPT IDE controller rev %d\n",
146 			    revision);
147 		break;
148 	default:
149 		aprint_normal("unknown HPT IDE controller 0x%x\n",
150 		    sc->sc_pp->ide_product);
151 	}
152 
153 	/*
154 	 * when the chip is in native mode it identifies itself as a
155 	 * 'misc mass storage'. Fake interface in this case.
156 	 */
157 	if (PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_IDE) {
158 		interface = PCI_INTERFACE(pa->pa_class);
159 	} else {
160 		interface = PCIIDE_INTERFACE_BUS_MASTER_DMA |
161 		    PCIIDE_INTERFACE_PCI(0);
162 		if ((sc->sc_pp->ide_product == PCI_PRODUCT_TRIONES_HPT366 &&
163 		    (revision == HPT368_REV || revision == HPT370_REV || revision == HPT370A_REV ||
164 		     revision == HPT372_REV)) ||
165 		    sc->sc_pp->ide_product == PCI_PRODUCT_TRIONES_HPT302 ||
166 		    sc->sc_pp->ide_product == PCI_PRODUCT_TRIONES_HPT371 ||
167 		    sc->sc_pp->ide_product == PCI_PRODUCT_TRIONES_HPT372A ||
168 		    sc->sc_pp->ide_product == PCI_PRODUCT_TRIONES_HPT374)
169 			interface |= PCIIDE_INTERFACE_PCI(1);
170 	}
171 
172 	aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
173 	    "bus-master DMA support present");
174 	pciide_mapreg_dma(sc, pa);
175 	aprint_verbose("\n");
176 	sc->sc_wdcdev.sc_atac.atac_cap = ATAC_CAP_DATA16 | ATAC_CAP_DATA32;
177 	if (sc->sc_dma_ok) {
178 		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DMA | ATAC_CAP_UDMA;
179 		sc->sc_wdcdev.irqack = pciide_irqack;
180 	}
181 	sc->sc_wdcdev.sc_atac.atac_pio_cap = 4;
182 	sc->sc_wdcdev.sc_atac.atac_dma_cap = 2;
183 
184 	sc->sc_wdcdev.sc_atac.atac_set_modes = hpt_setup_channel;
185 	sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanarray;
186 	if (sc->sc_pp->ide_product == PCI_PRODUCT_TRIONES_HPT366 &&
187 	    (revision == HPT366_REV || revision == HPT368_REV)) {
188 		sc->sc_wdcdev.sc_atac.atac_nchannels = 1;
189 		sc->sc_wdcdev.sc_atac.atac_udma_cap = 4;
190 	} else {
191 		sc->sc_wdcdev.sc_atac.atac_nchannels = 2;
192 		if (sc->sc_pp->ide_product == PCI_PRODUCT_TRIONES_HPT374 ||
193 		    sc->sc_pp->ide_product == PCI_PRODUCT_TRIONES_HPT372A ||
194 		    sc->sc_pp->ide_product == PCI_PRODUCT_TRIONES_HPT371 ||
195 		    sc->sc_pp->ide_product == PCI_PRODUCT_TRIONES_HPT302 ||
196 		    (sc->sc_pp->ide_product == PCI_PRODUCT_TRIONES_HPT366 &&
197 		    revision == HPT372_REV))
198 			sc->sc_wdcdev.sc_atac.atac_udma_cap = 6;
199 		else
200 			sc->sc_wdcdev.sc_atac.atac_udma_cap = 5;
201 	}
202 
203 	wdc_allocate_regs(&sc->sc_wdcdev);
204 
205 	for (i = 0; i < sc->sc_wdcdev.sc_atac.atac_nchannels; i++) {
206 		cp = &sc->pciide_channels[i];
207 		if (sc->sc_wdcdev.sc_atac.atac_nchannels > 1) {
208 			compatchan = i;
209 			if((pciide_pci_read(sc->sc_pc, sc->sc_tag,
210 			   HPT370_CTRL1(i)) & HPT370_CTRL1_EN) == 0) {
211 				aprint_normal(
212 				    "%s: %s channel ignored (disabled)\n",
213 				    device_xname(
214 				      sc->sc_wdcdev.sc_atac.atac_dev),
215 				    cp->name);
216 				cp->ata_channel.ch_flags |= ATACH_DISABLED;
217 				continue;
218 			}
219 		} else {
220 			/*
221 			 * The 366 has 2 PCI IDE functions, one for primary and
222 			 * one for secondary. So we need to call
223 			 * pciide_mapregs_compat() with the real channel.
224 			 */
225 			if (pa->pa_function == 0)
226 				compatchan = 0;
227 			else if (pa->pa_function == 1)
228 				compatchan = 1;
229 			else {
230 				aprint_error_dev(
231 				    sc->sc_wdcdev.sc_atac.atac_dev,
232 				    "unexpected PCI function %d\n",
233 				    pa->pa_function);
234 				return;
235 			}
236 		}
237 		if (pciide_chansetup(sc, i, interface) == 0)
238 			continue;
239 		if (interface & PCIIDE_INTERFACE_PCI(i)) {
240 			pciide_mapregs_native(pa, cp, hpt_pci_intr);
241 		} else {
242 			pciide_mapregs_compat(pa, cp, compatchan);
243 			if ((cp->ata_channel.ch_flags & ATACH_DISABLED) == 0)
244 				pciide_map_compat_intr(pa, cp,
245 				    sc->sc_cy_compatchan);
246 		}
247 		wdcattach(&cp->ata_channel);
248 	}
249 	if ((sc->sc_pp->ide_product == PCI_PRODUCT_TRIONES_HPT366 &&
250 	    (revision == HPT368_REV || revision == HPT370_REV || revision == HPT370A_REV ||
251 	     revision == HPT372_REV)) ||
252 	    sc->sc_pp->ide_product == PCI_PRODUCT_TRIONES_HPT302 ||
253 	    sc->sc_pp->ide_product == PCI_PRODUCT_TRIONES_HPT371 ||
254 	    sc->sc_pp->ide_product == PCI_PRODUCT_TRIONES_HPT372A ||
255 	    sc->sc_pp->ide_product == PCI_PRODUCT_TRIONES_HPT374) {
256 		/*
257 		 * HPT370_REV and highter has a bit to disable interrupts,
258 		 * make sure to clear it
259 		 */
260 		pciide_pci_write(sc->sc_pc, sc->sc_tag, HPT_CSEL,
261 		    pciide_pci_read(sc->sc_pc, sc->sc_tag, HPT_CSEL) &
262 		    ~HPT_CSEL_IRQDIS);
263 	}
264 	/* set clocks, etc (mandatory on 372/4, optional otherwise) */
265 	if ((sc->sc_pp->ide_product == PCI_PRODUCT_TRIONES_HPT366 &&
266 	     revision == HPT372_REV ) ||
267 	    sc->sc_pp->ide_product == PCI_PRODUCT_TRIONES_HPT302 ||
268 	    sc->sc_pp->ide_product == PCI_PRODUCT_TRIONES_HPT371 ||
269 	    sc->sc_pp->ide_product == PCI_PRODUCT_TRIONES_HPT372A ||
270 	    sc->sc_pp->ide_product == PCI_PRODUCT_TRIONES_HPT374)
271 		pciide_pci_write(sc->sc_pc, sc->sc_tag, HPT_SC2,
272 		    (pciide_pci_read(sc->sc_pc, sc->sc_tag, HPT_SC2) &
273 		     HPT_SC2_MAEN) | HPT_SC2_OSC_EN);
274 	return;
275 }
276 
277 static void
278 hpt_setup_channel(struct ata_channel *chp)
279 {
280 	struct ata_drive_datas *drvp;
281 	int drive, s;
282 	int cable;
283 	u_int32_t before, after;
284 	u_int32_t idedma_ctl;
285 	struct pciide_channel *cp = CHAN_TO_PCHAN(chp);
286 	struct pciide_softc *sc = CHAN_TO_PCIIDE(chp);
287 	int revision =
288 	     PCI_REVISION(pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_CLASS_REG));
289 	const u_int32_t *tim_pio, *tim_dma, *tim_udma;
290 
291 	cable = pciide_pci_read(sc->sc_pc, sc->sc_tag, HPT_CSEL);
292 
293 	/* setup DMA if needed */
294 	pciide_channel_dma_setup(cp);
295 
296 	idedma_ctl = 0;
297 
298 	/* select the timing arrays for the chip */
299 	switch (sc->sc_pp->ide_product) {
300 	case PCI_PRODUCT_TRIONES_HPT374:
301 		tim_udma = hpt374_udma;
302 		tim_dma = hpt374_dma;
303 		tim_pio = hpt374_pio;
304 		break;
305 	case PCI_PRODUCT_TRIONES_HPT302:
306 	case PCI_PRODUCT_TRIONES_HPT371:
307 	case PCI_PRODUCT_TRIONES_HPT372A:
308 		tim_udma = hpt372_udma;
309 		tim_dma = hpt372_dma;
310 		tim_pio = hpt372_pio;
311 		break;
312 	case PCI_PRODUCT_TRIONES_HPT366:
313 	default:
314 		switch (revision) {
315 		case HPT372_REV:
316 			tim_udma = hpt372_udma;
317 			tim_dma = hpt372_dma;
318 			tim_pio = hpt372_pio;
319 			break;
320 		case HPT370_REV:
321 		case HPT370A_REV:
322 			tim_udma = hpt370_udma;
323 			tim_dma = hpt370_dma;
324 			tim_pio = hpt370_pio;
325 			break;
326 		case HPT368_REV:
327 		case HPT366_REV:
328 		default:
329 			tim_udma = hpt366_udma;
330 			tim_dma = hpt366_dma;
331 			tim_pio = hpt366_pio;
332 			break;
333 		}
334 	}
335 
336 	/* Per drive settings */
337 	for (drive = 0; drive < chp->ch_ndrive; drive++) {
338 		drvp = &chp->ch_drive[drive];
339 		/* If no drive, skip */
340 		if ((drvp->drive_flags & DRIVE) == 0)
341 			continue;
342 		before = pci_conf_read(sc->sc_pc, sc->sc_tag,
343 					HPT_IDETIM(chp->ch_channel, drive));
344 
345 		/* add timing values, setup DMA if needed */
346 		if (drvp->drive_flags & DRIVE_UDMA) {
347 			/* use Ultra/DMA */
348 			s = splbio();
349 			drvp->drive_flags &= ~DRIVE_DMA;
350 			splx(s);
351 			if ((cable & HPT_CSEL_CBLID(chp->ch_channel)) != 0 &&
352 			    drvp->UDMA_mode > 2)
353 				drvp->UDMA_mode = 2;
354 			after = tim_udma[drvp->UDMA_mode];
355 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
356 		} else if (drvp->drive_flags & DRIVE_DMA) {
357 			/*
358 			 * use Multiword DMA.
359 			 * Timings will be used for both PIO and DMA, so adjust
360 			 * DMA mode if needed
361 			 */
362 			if (drvp->PIO_mode >= 3 &&
363 			    (drvp->DMA_mode + 2) > drvp->PIO_mode) {
364 				drvp->DMA_mode = drvp->PIO_mode - 2;
365 			}
366 			after = tim_dma[drvp->DMA_mode];
367 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
368 		} else {
369 			/* PIO only */
370 			after = tim_pio[drvp->PIO_mode];
371 		}
372 		pci_conf_write(sc->sc_pc, sc->sc_tag,
373 		    HPT_IDETIM(chp->ch_channel, drive), after);
374 		ATADEBUG_PRINT(("%s: bus speed register set to 0x%08x "
375 		    "(BIOS 0x%08x)\n", device_xname(drvp->drv_softc),
376 		    after, before), DEBUG_PROBE);
377 	}
378 	if (idedma_ctl != 0) {
379 		/* Add software bits in status register */
380 		bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0,
381 		    idedma_ctl);
382 	}
383 }
384 
385 static int
386 hpt_pci_intr(void *arg)
387 {
388 	struct pciide_softc *sc = arg;
389 	struct pciide_channel *cp;
390 	struct ata_channel *wdc_cp;
391 	int rv = 0;
392 	int dmastat, i, crv;
393 
394 	for (i = 0; i < sc->sc_wdcdev.sc_atac.atac_nchannels; i++) {
395 		cp = &sc->pciide_channels[i];
396 		dmastat = bus_space_read_1(sc->sc_dma_iot,
397 		    cp->dma_iohs[IDEDMA_CTL], 0);
398 		if((dmastat & ( IDEDMA_CTL_ACT | IDEDMA_CTL_INTR)) !=
399 		    IDEDMA_CTL_INTR)
400 			continue;
401 		wdc_cp = &cp->ata_channel;
402 		crv = wdcintr(wdc_cp);
403 		if (crv == 0) {
404 			aprint_error("%s:%d: bogus intr\n",
405 			    device_xname(sc->sc_wdcdev.sc_atac.atac_dev), i);
406 			bus_space_write_1(sc->sc_dma_iot,
407 			    cp->dma_iohs[IDEDMA_CTL], 0, dmastat);
408 		} else
409 			rv = 1;
410 	}
411 	return rv;
412 }
413