nside.c revision 1.1.6.2 1 1.1.6.2 rmind /* $NetBSD: nside.c,v 1.1.6.2 2011/03/05 20:53:47 rmind Exp $ */
2 1.1.6.2 rmind
3 1.1.6.2 rmind /*
4 1.1.6.2 rmind * Copyright (c) 1999, 2000, 2001 Manuel Bouyer.
5 1.1.6.2 rmind *
6 1.1.6.2 rmind * Redistribution and use in source and binary forms, with or without
7 1.1.6.2 rmind * modification, are permitted provided that the following conditions
8 1.1.6.2 rmind * are met:
9 1.1.6.2 rmind * 1. Redistributions of source code must retain the above copyright
10 1.1.6.2 rmind * notice, this list of conditions and the following disclaimer.
11 1.1.6.2 rmind * 2. Redistributions in binary form must reproduce the above copyright
12 1.1.6.2 rmind * notice, this list of conditions and the following disclaimer in the
13 1.1.6.2 rmind * documentation and/or other materials provided with the distribution.
14 1.1.6.2 rmind *
15 1.1.6.2 rmind * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1.6.2 rmind * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 1.1.6.2 rmind * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 1.1.6.2 rmind * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 1.1.6.2 rmind * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 1.1.6.2 rmind * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 1.1.6.2 rmind * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 1.1.6.2 rmind * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 1.1.6.2 rmind * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 1.1.6.2 rmind * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 1.1.6.2 rmind */
26 1.1.6.2 rmind
27 1.1.6.2 rmind #include <sys/cdefs.h>
28 1.1.6.2 rmind __KERNEL_RCSID(0, "$NetBSD: nside.c,v 1.1.6.2 2011/03/05 20:53:47 rmind Exp $");
29 1.1.6.2 rmind
30 1.1.6.2 rmind #include <sys/param.h>
31 1.1.6.2 rmind #include <sys/systm.h>
32 1.1.6.2 rmind
33 1.1.6.2 rmind #include <dev/pci/pcivar.h>
34 1.1.6.2 rmind #include <dev/pci/pcidevs.h>
35 1.1.6.2 rmind #include <dev/pci/pciidereg.h>
36 1.1.6.2 rmind #include <dev/pci/pciidevar.h>
37 1.1.6.2 rmind #include <dev/pci/pciide_natsemi_reg.h>
38 1.1.6.2 rmind
39 1.1.6.2 rmind static void natsemi_chip_map(struct pciide_softc*, struct pci_attach_args*);
40 1.1.6.2 rmind static void natsemi_setup_channel(struct ata_channel*);
41 1.1.6.2 rmind static int natsemi_pci_intr(void *);
42 1.1.6.2 rmind static void natsemi_irqack(struct ata_channel *);
43 1.1.6.2 rmind
44 1.1.6.2 rmind static int nside_match(device_t, cfdata_t, void *);
45 1.1.6.2 rmind static void nside_attach(device_t, device_t, void *);
46 1.1.6.2 rmind
47 1.1.6.2 rmind struct nside_softc {
48 1.1.6.2 rmind struct pciide_softc pciide_sc;
49 1.1.6.2 rmind struct pci_attach_args pcib_pa;
50 1.1.6.2 rmind };
51 1.1.6.2 rmind
52 1.1.6.2 rmind CFATTACH_DECL_NEW(nside, sizeof(struct nside_softc),
53 1.1.6.2 rmind nside_match, nside_attach, NULL, NULL);
54 1.1.6.2 rmind
55 1.1.6.2 rmind static const struct pciide_product_desc pciide_natsemi_products[] = {
56 1.1.6.2 rmind { PCI_PRODUCT_NS_PC87415, /* National Semi PC87415 IDE */
57 1.1.6.2 rmind 0,
58 1.1.6.2 rmind "National Semiconductor PC87415 IDE Controller",
59 1.1.6.2 rmind natsemi_chip_map,
60 1.1.6.2 rmind },
61 1.1.6.2 rmind { 0,
62 1.1.6.2 rmind 0,
63 1.1.6.2 rmind NULL,
64 1.1.6.2 rmind NULL
65 1.1.6.2 rmind }
66 1.1.6.2 rmind };
67 1.1.6.2 rmind
68 1.1.6.2 rmind static int
69 1.1.6.2 rmind nside_match(device_t parent, cfdata_t match, void *aux)
70 1.1.6.2 rmind {
71 1.1.6.2 rmind struct pci_attach_args *pa = aux;
72 1.1.6.2 rmind
73 1.1.6.2 rmind if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_NS &&
74 1.1.6.2 rmind PCI_CLASS(pa->pa_class) == PCI_CLASS_MASS_STORAGE &&
75 1.1.6.2 rmind PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_IDE) {
76 1.1.6.2 rmind if (pciide_lookup_product(pa->pa_id, pciide_natsemi_products))
77 1.1.6.2 rmind return 2;
78 1.1.6.2 rmind }
79 1.1.6.2 rmind return 0;
80 1.1.6.2 rmind }
81 1.1.6.2 rmind
82 1.1.6.2 rmind static void
83 1.1.6.2 rmind nside_attach(device_t parent, device_t self, void *aux)
84 1.1.6.2 rmind {
85 1.1.6.2 rmind struct pci_attach_args *pa = aux;
86 1.1.6.2 rmind struct pciide_softc *sc = device_private(self);
87 1.1.6.2 rmind
88 1.1.6.2 rmind sc->sc_wdcdev.sc_atac.atac_dev = self;
89 1.1.6.2 rmind
90 1.1.6.2 rmind pciide_common_attach(sc, pa,
91 1.1.6.2 rmind pciide_lookup_product(pa->pa_id, pciide_natsemi_products));
92 1.1.6.2 rmind }
93 1.1.6.2 rmind
94 1.1.6.2 rmind static void
95 1.1.6.2 rmind natsemi_chip_map(struct pciide_softc *sc, struct pci_attach_args *pa)
96 1.1.6.2 rmind {
97 1.1.6.2 rmind struct pciide_channel *cp;
98 1.1.6.2 rmind int channel;
99 1.1.6.2 rmind pcireg_t interface, ctl;
100 1.1.6.2 rmind
101 1.1.6.2 rmind if (pciide_chipen(sc, pa) == 0)
102 1.1.6.2 rmind return;
103 1.1.6.2 rmind
104 1.1.6.2 rmind aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
105 1.1.6.2 rmind "bus-master DMA support present");
106 1.1.6.2 rmind pciide_mapreg_dma(sc, pa);
107 1.1.6.2 rmind aprint_verbose("\n");
108 1.1.6.2 rmind
109 1.1.6.2 rmind sc->sc_wdcdev.sc_atac.atac_cap = ATAC_CAP_DATA16;
110 1.1.6.2 rmind
111 1.1.6.2 rmind if (sc->sc_dma_ok) {
112 1.1.6.2 rmind sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DMA;
113 1.1.6.2 rmind sc->sc_wdcdev.irqack = natsemi_irqack;
114 1.1.6.2 rmind }
115 1.1.6.2 rmind
116 1.1.6.2 rmind pciide_pci_write(sc->sc_pc, sc->sc_tag, NATSEMI_CCBT, 0xb7);
117 1.1.6.2 rmind
118 1.1.6.2 rmind /*
119 1.1.6.2 rmind * Mask off interrupts from both channels, appropriate channel(s)
120 1.1.6.2 rmind * will be unmasked later.
121 1.1.6.2 rmind */
122 1.1.6.2 rmind pciide_pci_write(sc->sc_pc, sc->sc_tag, NATSEMI_CTRL2,
123 1.1.6.2 rmind pciide_pci_read(sc->sc_pc, sc->sc_tag, NATSEMI_CTRL2) |
124 1.1.6.2 rmind NATSEMI_CHMASK(0) | NATSEMI_CHMASK(1));
125 1.1.6.2 rmind
126 1.1.6.2 rmind sc->sc_wdcdev.sc_atac.atac_pio_cap = 4;
127 1.1.6.2 rmind sc->sc_wdcdev.sc_atac.atac_dma_cap = 2;
128 1.1.6.2 rmind sc->sc_wdcdev.sc_atac.atac_set_modes = natsemi_setup_channel;
129 1.1.6.2 rmind sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanarray;
130 1.1.6.2 rmind sc->sc_wdcdev.sc_atac.atac_nchannels = PCIIDE_NUM_CHANNELS;
131 1.1.6.2 rmind
132 1.1.6.2 rmind interface = PCI_INTERFACE(pa->pa_class);
133 1.1.6.2 rmind interface &= ~PCIIDE_CHANSTATUS_EN; /* Reserved on PC87415 */
134 1.1.6.2 rmind
135 1.1.6.2 rmind /* If we're in PCIIDE mode, unmask INTA, otherwise mask it. */
136 1.1.6.2 rmind ctl = pciide_pci_read(sc->sc_pc, sc->sc_tag, NATSEMI_CTRL1);
137 1.1.6.2 rmind if (interface & (PCIIDE_INTERFACE_PCI(0) | PCIIDE_INTERFACE_PCI(1)))
138 1.1.6.2 rmind ctl &= ~NATSEMI_CTRL1_INTAMASK;
139 1.1.6.2 rmind else
140 1.1.6.2 rmind ctl |= NATSEMI_CTRL1_INTAMASK;
141 1.1.6.2 rmind pciide_pci_write(sc->sc_pc, sc->sc_tag, NATSEMI_CTRL1, ctl);
142 1.1.6.2 rmind
143 1.1.6.2 rmind wdc_allocate_regs(&sc->sc_wdcdev);
144 1.1.6.2 rmind
145 1.1.6.2 rmind for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels; channel++) {
146 1.1.6.2 rmind cp = &sc->pciide_channels[channel];
147 1.1.6.2 rmind if (pciide_chansetup(sc, channel, interface) == 0)
148 1.1.6.2 rmind continue;
149 1.1.6.2 rmind
150 1.1.6.2 rmind pciide_mapchan(pa, cp, interface, natsemi_pci_intr);
151 1.1.6.2 rmind
152 1.1.6.2 rmind pciide_pci_write(sc->sc_pc, sc->sc_tag, NATSEMI_CTRL2,
153 1.1.6.2 rmind pciide_pci_read(sc->sc_pc, sc->sc_tag, NATSEMI_CTRL2) &
154 1.1.6.2 rmind ~(NATSEMI_CHMASK(channel)));
155 1.1.6.2 rmind }
156 1.1.6.2 rmind }
157 1.1.6.2 rmind
158 1.1.6.2 rmind void
159 1.1.6.2 rmind natsemi_setup_channel(struct ata_channel *chp)
160 1.1.6.2 rmind {
161 1.1.6.2 rmind struct ata_drive_datas *drvp;
162 1.1.6.2 rmind int drive, ndrives = 0;
163 1.1.6.2 rmind uint32_t idedma_ctl = 0;
164 1.1.6.2 rmind struct pciide_channel *cp = CHAN_TO_PCHAN(chp);
165 1.1.6.2 rmind struct pciide_softc *sc = CHAN_TO_PCIIDE(chp);
166 1.1.6.2 rmind uint8_t tim;
167 1.1.6.2 rmind
168 1.1.6.2 rmind /* setup DMA if needed */
169 1.1.6.2 rmind pciide_channel_dma_setup(cp);
170 1.1.6.2 rmind
171 1.1.6.2 rmind for (drive = 0; drive < 2; drive++) {
172 1.1.6.2 rmind drvp = &chp->ch_drive[drive];
173 1.1.6.2 rmind /* If no drive, skip */
174 1.1.6.2 rmind if ((drvp->drive_flags & DRIVE) == 0)
175 1.1.6.2 rmind continue;
176 1.1.6.2 rmind
177 1.1.6.2 rmind ndrives++;
178 1.1.6.2 rmind /* add timing values, setup DMA if needed */
179 1.1.6.2 rmind if ((drvp->drive_flags & DRIVE_DMA) == 0) {
180 1.1.6.2 rmind tim = natsemi_pio_pulse[drvp->PIO_mode] |
181 1.1.6.2 rmind (natsemi_pio_recover[drvp->PIO_mode] << 4);
182 1.1.6.2 rmind } else {
183 1.1.6.2 rmind /*
184 1.1.6.2 rmind * use Multiword DMA
185 1.1.6.2 rmind * Timings will be used for both PIO and DMA,
186 1.1.6.2 rmind * so adjust DMA mode if needed
187 1.1.6.2 rmind */
188 1.1.6.2 rmind if (drvp->PIO_mode >= 3 &&
189 1.1.6.2 rmind (drvp->DMA_mode + 2) > drvp->PIO_mode) {
190 1.1.6.2 rmind drvp->DMA_mode = drvp->PIO_mode - 2;
191 1.1.6.2 rmind }
192 1.1.6.2 rmind idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
193 1.1.6.2 rmind tim = natsemi_dma_pulse[drvp->DMA_mode] |
194 1.1.6.2 rmind (natsemi_dma_recover[drvp->DMA_mode] << 4);
195 1.1.6.2 rmind
196 1.1.6.2 rmind }
197 1.1.6.2 rmind
198 1.1.6.2 rmind pciide_pci_write(sc->sc_pc, sc->sc_tag,
199 1.1.6.2 rmind NATSEMI_RTREG(chp->ch_channel, drive), tim);
200 1.1.6.2 rmind pciide_pci_write(sc->sc_pc, sc->sc_tag,
201 1.1.6.2 rmind NATSEMI_WTREG(chp->ch_channel, drive), tim);
202 1.1.6.2 rmind }
203 1.1.6.2 rmind
204 1.1.6.2 rmind if (idedma_ctl != 0) {
205 1.1.6.2 rmind /* Add software bits in status register */
206 1.1.6.2 rmind bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0,
207 1.1.6.2 rmind idedma_ctl);
208 1.1.6.2 rmind
209 1.1.6.2 rmind }
210 1.1.6.2 rmind /* Go ahead and ack interrupts generated during probe. */
211 1.1.6.2 rmind natsemi_irqack(chp);
212 1.1.6.2 rmind }
213 1.1.6.2 rmind
214 1.1.6.2 rmind void
215 1.1.6.2 rmind natsemi_irqack(struct ata_channel *chp)
216 1.1.6.2 rmind {
217 1.1.6.2 rmind struct pciide_channel *cp = CHAN_TO_PCHAN(chp);
218 1.1.6.2 rmind struct pciide_softc *sc = CHAN_TO_PCIIDE(chp);
219 1.1.6.2 rmind uint8_t clr;
220 1.1.6.2 rmind
221 1.1.6.2 rmind /* Errata: The "clear" bits are in the wrong register *sigh* */
222 1.1.6.2 rmind clr = bus_space_read_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD], 0);
223 1.1.6.2 rmind clr |= bus_space_read_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0) &
224 1.1.6.2 rmind (IDEDMA_CTL_ERR | IDEDMA_CTL_INTR);
225 1.1.6.2 rmind bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD], 0, clr);
226 1.1.6.2 rmind }
227 1.1.6.2 rmind
228 1.1.6.2 rmind int
229 1.1.6.2 rmind natsemi_pci_intr(void *arg)
230 1.1.6.2 rmind {
231 1.1.6.2 rmind struct pciide_softc *sc = arg;
232 1.1.6.2 rmind struct pciide_channel *cp;
233 1.1.6.2 rmind struct ata_channel *wdc_cp;
234 1.1.6.2 rmind int i, rv, crv;
235 1.1.6.2 rmind uint8_t msk;
236 1.1.6.2 rmind
237 1.1.6.2 rmind rv = 0;
238 1.1.6.2 rmind msk = pciide_pci_read(sc->sc_pc, sc->sc_tag, NATSEMI_CTRL2);
239 1.1.6.2 rmind for (i = 0; i < sc->sc_wdcdev.sc_atac.atac_nchannels; i++) {
240 1.1.6.2 rmind cp = &sc->pciide_channels[i];
241 1.1.6.2 rmind wdc_cp = &cp->ata_channel;
242 1.1.6.2 rmind
243 1.1.6.2 rmind /* If a compat channel skip. */
244 1.1.6.2 rmind if (cp->compat)
245 1.1.6.2 rmind continue;
246 1.1.6.2 rmind
247 1.1.6.2 rmind /* If this channel is masked, skip it. */
248 1.1.6.2 rmind if (msk & NATSEMI_CHMASK(i))
249 1.1.6.2 rmind continue;
250 1.1.6.2 rmind
251 1.1.6.2 rmind crv = wdcintr(wdc_cp);
252 1.1.6.2 rmind if (crv == 0)
253 1.1.6.2 rmind ; /* leave alone */
254 1.1.6.2 rmind else if (crv == 1)
255 1.1.6.2 rmind rv = 1; /* claim the intr */
256 1.1.6.2 rmind else if (rv == 0) /* crv should be -1 in this case */
257 1.1.6.2 rmind rv = crv; /* if we've done no better, take it */
258 1.1.6.2 rmind }
259 1.1.6.2 rmind return (rv);
260 1.1.6.2 rmind }
261