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