iteide.c revision 1.10 1 1.10 cegger /* $NetBSD: iteide.c,v 1.10 2008/04/10 19:13:37 cegger Exp $ */
2 1.1 grant
3 1.1 grant /*
4 1.1 grant * Copyright (c) 2004 The NetBSD Foundation, Inc.
5 1.1 grant *
6 1.1 grant * This code is derived from software contributed to The NetBSD Foundation
7 1.1 grant * by Grant Beattie.
8 1.1 grant *
9 1.1 grant * Redistribution and use in source and binary forms, with or without
10 1.1 grant * modification, are permitted provided that the following conditions
11 1.1 grant * are met:
12 1.1 grant * 1. Redistributions of source code must retain the above copyright
13 1.1 grant * notice, this list of conditions and the following disclaimer.
14 1.1 grant * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 grant * notice, this list of conditions and the following disclaimer in the
16 1.1 grant * documentation and/or other materials provided with the distribution.
17 1.1 grant * 3. The name of the author may not be used to endorse or promote products
18 1.1 grant * derived from this software without specific prior written permission.
19 1.1 grant *
20 1.1 grant * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 1.1 grant * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 1.1 grant * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 1.2 perry * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 1.1 grant * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 1.1 grant * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 1.1 grant * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 1.1 grant * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 1.1 grant * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 1.1 grant * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 1.1 grant *
31 1.1 grant */
32 1.1 grant
33 1.3 lukem #include <sys/cdefs.h>
34 1.10 cegger __KERNEL_RCSID(0, "$NetBSD: iteide.c,v 1.10 2008/04/10 19:13:37 cegger Exp $");
35 1.3 lukem
36 1.1 grant #include <sys/param.h>
37 1.1 grant #include <sys/systm.h>
38 1.1 grant
39 1.1 grant #include <dev/pci/pcivar.h>
40 1.1 grant #include <dev/pci/pcidevs.h>
41 1.1 grant #include <dev/pci/pciidereg.h>
42 1.1 grant #include <dev/pci/pciidevar.h>
43 1.1 grant #include <dev/pci/pciide_ite_reg.h>
44 1.1 grant
45 1.1 grant static void ite_chip_map(struct pciide_softc*, struct pci_attach_args*);
46 1.1 grant static void ite_setup_channel(struct ata_channel*);
47 1.1 grant
48 1.9 cube static int iteide_match(device_t, cfdata_t, void *);
49 1.9 cube static void iteide_attach(device_t, device_t, void *);
50 1.1 grant
51 1.9 cube CFATTACH_DECL_NEW(iteide, sizeof(struct pciide_softc),
52 1.1 grant iteide_match, iteide_attach, NULL, NULL);
53 1.1 grant
54 1.1 grant static const struct pciide_product_desc pciide_ite_products[] = {
55 1.5 xtraeme { PCI_PRODUCT_ITE_IT8211,
56 1.5 xtraeme 0,
57 1.5 xtraeme "Integrated Technology Express IDE controller",
58 1.5 xtraeme ite_chip_map,
59 1.5 xtraeme },
60 1.1 grant { PCI_PRODUCT_ITE_IT8212,
61 1.1 grant 0,
62 1.1 grant "Integrated Technology Express IDE controller",
63 1.1 grant ite_chip_map,
64 1.1 grant },
65 1.1 grant { 0,
66 1.1 grant 0,
67 1.1 grant NULL,
68 1.1 grant NULL
69 1.1 grant }
70 1.1 grant };
71 1.1 grant
72 1.1 grant static int
73 1.9 cube iteide_match(device_t parent, cfdata_t match, void *aux)
74 1.1 grant {
75 1.1 grant struct pci_attach_args *pa = aux;
76 1.1 grant if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ITE &&
77 1.1 grant PCI_CLASS(pa->pa_class) == PCI_CLASS_MASS_STORAGE) {
78 1.1 grant if (pciide_lookup_product(pa->pa_id, pciide_ite_products))
79 1.1 grant return (2);
80 1.1 grant }
81 1.1 grant return (0);
82 1.1 grant }
83 1.1 grant
84 1.1 grant static void
85 1.9 cube iteide_attach(device_t parent, device_t self, void *aux)
86 1.1 grant {
87 1.1 grant struct pci_attach_args *pa = aux;
88 1.9 cube struct pciide_softc *sc = device_private(self);
89 1.9 cube
90 1.9 cube sc->sc_wdcdev.sc_atac.atac_dev = self;
91 1.1 grant
92 1.1 grant pciide_common_attach(sc, pa,
93 1.1 grant pciide_lookup_product(pa->pa_id, pciide_ite_products));
94 1.1 grant }
95 1.1 grant
96 1.1 grant static void
97 1.1 grant ite_chip_map(struct pciide_softc *sc, struct pci_attach_args *pa)
98 1.1 grant {
99 1.1 grant struct pciide_channel *cp;
100 1.1 grant int channel;
101 1.1 grant pcireg_t interface;
102 1.1 grant bus_size_t cmdsize, ctlsize;
103 1.1 grant pcireg_t cfg, modectl;
104 1.1 grant
105 1.1 grant /* fake interface since IT8212 claims to be a RAID device */
106 1.1 grant interface = PCIIDE_INTERFACE_BUS_MASTER_DMA |
107 1.1 grant PCIIDE_INTERFACE_PCI(0) | PCIIDE_INTERFACE_PCI(1);
108 1.1 grant
109 1.1 grant cfg = pci_conf_read(sc->sc_pc, sc->sc_tag, IT_CFG);
110 1.1 grant modectl = pci_conf_read(sc->sc_pc, sc->sc_tag, IT_MODE);
111 1.1 grant ATADEBUG_PRINT(("%s: cfg=0x%x, modectl=0x%x\n",
112 1.9 cube device_xname(sc->sc_wdcdev.sc_atac.atac_dev), cfg & IT_CFG_MASK,
113 1.1 grant modectl & IT_MODE_MASK), DEBUG_PROBE);
114 1.1 grant
115 1.1 grant if (pciide_chipen(sc, pa) == 0)
116 1.1 grant return;
117 1.1 grant
118 1.9 cube aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
119 1.9 cube "bus-master DMA support present");
120 1.1 grant pciide_mapreg_dma(sc, pa);
121 1.8 ad aprint_verbose("\n");
122 1.1 grant
123 1.1 grant sc->sc_wdcdev.sc_atac.atac_cap = ATAC_CAP_DATA16 | ATAC_CAP_DATA32;
124 1.1 grant
125 1.1 grant if (sc->sc_dma_ok) {
126 1.1 grant sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DMA | ATAC_CAP_UDMA;
127 1.1 grant sc->sc_wdcdev.irqack = pciide_irqack;
128 1.1 grant }
129 1.1 grant sc->sc_wdcdev.sc_atac.atac_pio_cap = 4;
130 1.1 grant sc->sc_wdcdev.sc_atac.atac_dma_cap = 2;
131 1.1 grant sc->sc_wdcdev.sc_atac.atac_udma_cap = 6;
132 1.1 grant
133 1.1 grant sc->sc_wdcdev.sc_atac.atac_set_modes = ite_setup_channel;
134 1.1 grant sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanarray;
135 1.1 grant sc->sc_wdcdev.sc_atac.atac_nchannels = PCIIDE_NUM_CHANNELS;
136 1.1 grant
137 1.1 grant wdc_allocate_regs(&sc->sc_wdcdev);
138 1.1 grant
139 1.1 grant /* Disable RAID */
140 1.1 grant modectl &= ~IT_MODE_RAID1;
141 1.1 grant /* Disable CPU firmware mode */
142 1.1 grant modectl &= ~IT_MODE_CPU;
143 1.1 grant
144 1.1 grant pci_conf_write(sc->sc_pc, sc->sc_tag, IT_MODE, modectl);
145 1.1 grant
146 1.1 grant for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels; channel++) {
147 1.1 grant cp = &sc->pciide_channels[channel];
148 1.1 grant
149 1.1 grant if (pciide_chansetup(sc, channel, interface) == 0)
150 1.1 grant continue;
151 1.1 grant
152 1.1 grant pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize,
153 1.1 grant pciide_pci_intr);
154 1.1 grant }
155 1.1 grant /* Re-read configuration registers after channels setup */
156 1.1 grant cfg = pci_conf_read(sc->sc_pc, sc->sc_tag, IT_CFG);
157 1.1 grant modectl = pci_conf_read(sc->sc_pc, sc->sc_tag, IT_MODE);
158 1.1 grant ATADEBUG_PRINT(("%s: cfg=0x%x, modectl=0x%x\n",
159 1.9 cube device_xname(sc->sc_wdcdev.sc_atac.atac_dev), cfg & IT_CFG_MASK,
160 1.1 grant modectl & IT_MODE_MASK), DEBUG_PROBE);
161 1.1 grant }
162 1.1 grant
163 1.1 grant static void
164 1.1 grant ite_setup_channel(struct ata_channel *chp)
165 1.1 grant {
166 1.1 grant struct ata_drive_datas *drvp;
167 1.1 grant int drive, mode = 0;
168 1.1 grant u_int32_t idedma_ctl;
169 1.1 grant struct pciide_channel *cp = CHAN_TO_PCHAN(chp);
170 1.1 grant struct pciide_softc *sc = CHAN_TO_PCIIDE(chp);
171 1.1 grant int channel = chp->ch_channel;
172 1.1 grant pcireg_t cfg, modectl;
173 1.1 grant pcireg_t tim;
174 1.1 grant
175 1.1 grant cfg = pci_conf_read(sc->sc_pc, sc->sc_tag, IT_CFG);
176 1.1 grant modectl = pci_conf_read(sc->sc_pc, sc->sc_tag, IT_MODE);
177 1.1 grant tim = pci_conf_read(sc->sc_pc, sc->sc_tag, IT_TIM(channel));
178 1.1 grant ATADEBUG_PRINT(("%s:%d: tim=0x%x\n",
179 1.9 cube device_xname(sc->sc_wdcdev.sc_atac.atac_dev),
180 1.1 grant channel, tim), DEBUG_PROBE);
181 1.1 grant
182 1.1 grant /* Setup DMA if needed */
183 1.1 grant pciide_channel_dma_setup(cp);
184 1.1 grant
185 1.1 grant /* Clear all bits for this channel */
186 1.1 grant idedma_ctl = 0;
187 1.1 grant
188 1.1 grant /* Per channel settings */
189 1.1 grant for (drive = 0; drive < 2; drive++) {
190 1.1 grant drvp = &chp->ch_drive[drive];
191 1.1 grant
192 1.1 grant /* If no drive, skip */
193 1.1 grant if ((drvp->drive_flags & DRIVE) == 0)
194 1.1 grant continue;
195 1.1 grant
196 1.1 grant if ((chp->ch_atac->atac_cap & ATAC_CAP_UDMA) != 0 &&
197 1.1 grant (drvp->drive_flags & DRIVE_UDMA) != 0) {
198 1.1 grant /* Setup UltraDMA mode */
199 1.1 grant drvp->drive_flags &= ~DRIVE_DMA;
200 1.1 grant modectl &= ~IT_MODE_DMA(channel, drive);
201 1.1 grant
202 1.1 grant #if 0
203 1.1 grant /* Check cable, only works in CPU firmware mode */
204 1.1 grant if (drvp->UDMA_mode > 2 &&
205 1.1 grant (cfg & IT_CFG_CABLE(channel, drive)) == 0) {
206 1.1 grant ATADEBUG_PRINT(("(%s:%d:%d): "
207 1.1 grant "80-wire cable not detected\n",
208 1.10 cegger device_xname(&sc->sc_wdcdev.sc_atac.atac_dev),
209 1.1 grant channel, drive), DEBUG_PROBE);
210 1.1 grant drvp->UDMA_mode = 2;
211 1.1 grant }
212 1.1 grant #endif
213 1.1 grant
214 1.1 grant if (drvp->UDMA_mode >= 5)
215 1.1 grant tim |= IT_TIM_UDMA5(drive);
216 1.1 grant else
217 1.1 grant tim &= ~IT_TIM_UDMA5(drive);
218 1.1 grant
219 1.1 grant mode = drvp->PIO_mode;
220 1.1 grant } else if ((chp->ch_atac->atac_cap & ATAC_CAP_DMA) != 0 &&
221 1.1 grant (drvp->drive_flags & DRIVE_DMA) != 0) {
222 1.1 grant /* Setup multiword DMA mode */
223 1.1 grant drvp->drive_flags &= ~DRIVE_UDMA;
224 1.1 grant modectl |= IT_MODE_DMA(channel, drive);
225 1.1 grant
226 1.1 grant /* mode = min(pio, dma + 2) */
227 1.1 grant if (drvp->PIO_mode <= (drvp->DMA_mode + 2))
228 1.1 grant mode = drvp->PIO_mode;
229 1.1 grant else
230 1.1 grant mode = drvp->DMA_mode + 2;
231 1.1 grant } else {
232 1.1 grant goto pio;
233 1.1 grant }
234 1.1 grant idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
235 1.1 grant
236 1.1 grant pio:
237 1.1 grant /* Setup PIO mode */
238 1.1 grant if (mode <= 2) {
239 1.1 grant drvp->DMA_mode = 0;
240 1.1 grant drvp->PIO_mode = 0;
241 1.1 grant mode = 0;
242 1.1 grant } else {
243 1.1 grant drvp->PIO_mode = mode;
244 1.1 grant drvp->DMA_mode = mode - 2;
245 1.1 grant }
246 1.1 grant
247 1.1 grant /* Enable IORDY if PIO mode >= 3 */
248 1.1 grant if (drvp->PIO_mode >= 3)
249 1.1 grant cfg |= IT_CFG_IORDY(channel);
250 1.1 grant }
251 1.1 grant
252 1.1 grant ATADEBUG_PRINT(("%s: tim=0x%x\n",
253 1.9 cube device_xname(sc->sc_wdcdev.sc_atac.atac_dev), tim), DEBUG_PROBE);
254 1.1 grant
255 1.1 grant pci_conf_write(sc->sc_pc, sc->sc_tag, IT_CFG, cfg);
256 1.1 grant pci_conf_write(sc->sc_pc, sc->sc_tag, IT_MODE, modectl);
257 1.1 grant pci_conf_write(sc->sc_pc, sc->sc_tag, IT_TIM(channel), tim);
258 1.1 grant
259 1.1 grant if (idedma_ctl != 0) {
260 1.1 grant /* Add software bits in status register */
261 1.1 grant bus_space_write_1(sc->sc_dma_iot,
262 1.1 grant cp->dma_iohs[IDEDMA_CTL], 0, idedma_ctl);
263 1.1 grant }
264 1.1 grant }
265