xref: /netbsd-src/sys/dev/pci/toshide.c (revision 1b9578b8c2c1f848eeb16dabbfd7d1f0d9fdefbd)
1 /*	$NetBSD: toshide.c,v 1.4 2011/04/04 20:37:56 dyoung Exp $	*/
2 
3 /*-
4  * Copyright (c) 2009 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: toshide.c,v 1.4 2011/04/04 20:37:56 dyoung Exp $");
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_piccolo_reg.h>
40 
41 static void piccolo_chip_map(struct pciide_softc *,
42     const struct pci_attach_args *);
43 static void piccolo_setup_channel(struct ata_channel *);
44 
45 static int  piccolo_match(device_t, cfdata_t, void *);
46 static void piccolo_attach(device_t, device_t, void *);
47 
48 CFATTACH_DECL_NEW(toshide, sizeof(struct pciide_softc),
49     piccolo_match, piccolo_attach, NULL, NULL);
50 
51 static const struct pciide_product_desc pciide_toshiba2_products[] = {
52 	{
53 		PCI_PRODUCT_TOSHIBA2_PICCOLO,
54 		0,
55 		"Toshiba Piccolo IDE controller",
56 		piccolo_chip_map,
57 	},
58 	{
59 		PCI_PRODUCT_TOSHIBA2_PICCOLO2,
60 		0,
61 		"Toshiba Piccolo 2 IDE controller",
62 		piccolo_chip_map,
63 	},
64 	{
65 		PCI_PRODUCT_TOSHIBA2_PICCOLO3,
66 		0,
67 		"Toshiba Piccolo 3 IDE controller",
68 		piccolo_chip_map,
69 	},
70 	{
71 		PCI_PRODUCT_TOSHIBA2_PICCOLO5,
72 		0,
73 		"Toshiba Piccolo 5 IDE controller",
74 		piccolo_chip_map,
75 	},
76 	{
77 		0,
78 		0,
79 		NULL,
80 		NULL,
81 	}
82 };
83 
84 static int
85 piccolo_match(device_t parent, cfdata_t match, void *aux)
86 {
87 	struct pci_attach_args *pa = aux;
88 
89 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_TOSHIBA2) {
90 		if (pciide_lookup_product(pa->pa_id, pciide_toshiba2_products))
91 			return 2;
92 	}
93 	return 0;
94 }
95 
96 static void
97 piccolo_attach(device_t parent, device_t self, void *aux)
98 {
99 	struct pci_attach_args *pa = aux;
100 	struct pciide_softc *sc = device_private(self);
101 	const struct pciide_product_desc *pp;
102 
103 	sc->sc_wdcdev.sc_atac.atac_dev = self;
104 
105 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_TOSHIBA2)
106 		pp = pciide_lookup_product(pa->pa_id, pciide_toshiba2_products);
107 	else
108 		pp = NULL;
109 	if (pp == NULL)
110 		panic("toshide_attach");
111 	pciide_common_attach(sc, pa, pp);
112 }
113 
114 static void
115 piccolo_chip_map(struct pciide_softc *sc, const struct pci_attach_args *pa)
116 {
117 	struct pciide_channel *cp;
118 	pcireg_t interface;
119 	int channel;
120 
121 	if (pciide_chipen(sc, pa) == 0)
122 		return;
123 
124 	aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
125 	    "bus-master DMA support present");
126 
127 	pciide_mapreg_dma(sc, pa);
128 	aprint_verbose("\n");
129 
130 	sc->sc_wdcdev.sc_atac.atac_cap = ATAC_CAP_DATA32 | ATAC_CAP_DATA16;
131 	sc->sc_wdcdev.sc_atac.atac_pio_cap = 5;
132 
133 	if (sc->sc_dma_ok) {
134 		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DMA | ATAC_CAP_UDMA;
135 		sc->sc_wdcdev.irqack = pciide_irqack;
136 		sc->sc_wdcdev.sc_atac.atac_dma_cap = 3;
137 		sc->sc_wdcdev.sc_atac.atac_udma_cap = 2;
138 	}
139 
140 	sc->sc_wdcdev.sc_atac.atac_set_modes = piccolo_setup_channel;
141 
142 	sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanarray;
143 	sc->sc_wdcdev.sc_atac.atac_nchannels = 1;
144 	/*
145 	 * XXX one for now. We'll figure out how to talk to the second channel
146 	 * later, hopefully! Second interface config is via the
147 	 * "alternate PCI Configuration Space" whatever that is!
148 	 */
149 
150 	interface = PCI_INTERFACE(pa->pa_class);
151 
152 	wdc_allocate_regs(&sc->sc_wdcdev);
153 
154 	for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels;
155 	     channel++) {
156 		cp = &sc->pciide_channels[channel];
157 		if (pciide_chansetup(sc, channel, interface) == 0)
158 			continue;
159 
160 		pciide_mapchan(pa, cp, interface, pciide_pci_intr);
161 	}
162 }
163 
164 static void
165 piccolo_setup_channel(struct ata_channel *chp)
166 {
167 	struct ata_drive_datas *drvp;
168 	struct pciide_channel *cp = CHAN_TO_PCHAN(chp);
169 	struct pciide_softc *sc = CHAN_TO_PCIIDE(chp);
170 	u_int32_t idedma_ctl;
171 	int drive, s;
172 	pcireg_t pxdx;
173 #ifdef TOSHIDE_DEBUG
174 	pcireg_t pxdx_prime;
175 #endif
176 
177 	idedma_ctl = 0;
178 
179 	/* Set up DMA if needed. */
180 	pciide_channel_dma_setup(cp);
181 
182 	for (drive = 0; drive < 2; drive++) {
183 
184 		drvp = &chp->ch_drive[drive];
185 		/* If no drive, skip */
186 		if ((drvp->drive_flags & DRIVE) == 0)
187 			continue;
188 
189 		if (drvp->drive_flags & DRIVE_UDMA) {
190 			/* use Ultra/DMA */
191 			s = splbio();
192 			drvp->drive_flags &= ~DRIVE_DMA;
193 			splx(s);
194 
195 			/*
196 			 * Use UDMA - we can go up to mode 2 so no need to
197 			 * check anything since nearly all drives with UDMA
198 			 * are mode 2 or faster
199 			 */
200 			pxdx = pci_conf_read(sc->sc_pc, sc->sc_tag,
201 			    PICCOLO_DMA_TIMING);
202  		        pxdx &= PICCOLO_UDMA_MASK;
203  		        pxdx |= piccolo_udma_times[2];
204  		        pci_conf_write(sc->sc_pc, sc->sc_tag,
205 			    PICCOLO_DMA_TIMING, pxdx);
206 #ifdef TOSHIDE_DEBUG
207 			/* XXX sanity check */
208  			pxdx_prime = pci_conf_read(sc->sc_pc, sc->sc_tag,
209 			    PICCOLO_DMA_TIMING);
210 			aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
211 			    "UDMA want %x, set %x, got %x\n",
212 			    piccolo_udma_times[2], pxdx, pxdx_prime);
213 #endif
214 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
215 
216 		}
217 		else if (drvp->drive_flags & DRIVE_DMA) {
218 			/*
219 			 * Use Multiword DMA
220 			 */
221 			if (drvp->PIO_mode > (drvp->DMA_mode + 2))
222 				drvp->PIO_mode = drvp->DMA_mode + 2;
223 			if (drvp->DMA_mode + 2 > (drvp->PIO_mode))
224 				drvp->DMA_mode = (drvp->PIO_mode > 2) ?
225 				    drvp->PIO_mode - 2 : 0;
226 
227 			pxdx = pci_conf_read(sc->sc_pc, sc->sc_tag,
228 			    PICCOLO_DMA_TIMING);
229  		        pxdx &= PICCOLO_DMA_MASK;
230  			pxdx |= piccolo_mw_dma_times[drvp->DMA_mode];
231  		        pci_conf_write(sc->sc_pc, sc->sc_tag,
232 			    PICCOLO_DMA_TIMING, pxdx);
233 #ifdef TOSHIDE_DEBUG
234 			/* XXX sanity check */
235  			pxdx_prime = pci_conf_read(sc->sc_pc, sc->sc_tag,
236 			    PICCOLO_DMA_TIMING);
237 			aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
238 			    "DMA %d want %x, set %x, got %x\n",
239 			    drvp->DMA_mode,
240 			    piccolo_mw_dma_times[drvp->DMA_mode], pxdx,
241 			    pxdx_prime);
242 #endif
243 			idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
244 
245 		}
246 		else {
247 			pxdx = pci_conf_read(sc->sc_pc, sc->sc_tag,
248 			    PICCOLO_PIO_TIMING);
249  		        pxdx &= PICCOLO_PIO_MASK;
250 			pxdx |= piccolo_pio_times[drvp->PIO_mode];
251  		        pci_conf_write(sc->sc_pc, sc->sc_tag,
252 			    PICCOLO_PIO_TIMING, pxdx);
253 #ifdef TOSHIDE_DEBUG
254 			/* XXX sanity check */
255  			pxdx_prime = pci_conf_read(sc->sc_pc, sc->sc_tag,
256 			    PICCOLO_PIO_TIMING);
257 			aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
258 			    "PIO %d want %x, set %x, got %x\n", drvp->PIO_mode,
259 			    piccolo_pio_times[drvp->PIO_mode], pxdx,
260 			    pxdx_prime);
261 #endif
262 		}
263 
264 	}
265 	if (idedma_ctl != 0) {
266 		/* Add software bits in status register */
267 		bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0,
268 		    idedma_ctl);
269 	}
270 }
271