stpcide.c revision 1.6 1 1.6 thorpej /* $NetBSD: stpcide.c,v 1.6 2004/08/14 15:08:06 thorpej Exp $ */
2 1.1 nisimura
3 1.1 nisimura /*
4 1.1 nisimura * Copyright (c) 2003 Toru Nishimura
5 1.1 nisimura *
6 1.1 nisimura * Redistribution and use in source and binary forms, with or without
7 1.1 nisimura * modification, are permitted provided that the following conditions
8 1.1 nisimura * are met:
9 1.1 nisimura * 1. Redistributions of source code must retain the above copyright
10 1.1 nisimura * notice, this list of conditions and the following disclaimer.
11 1.1 nisimura * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 nisimura * notice, this list of conditions and the following disclaimer in the
13 1.1 nisimura * documentation and/or other materials provided with the distribution.
14 1.1 nisimura * 3. All advertising materials mentioning features or use of this software
15 1.1 nisimura * must display the following acknowledgement:
16 1.1 nisimura * This product includes software developed by Toru Nishimura.
17 1.1 nisimura * 4. The name of the author may not be used to endorse or promote products
18 1.1 nisimura * derived from this software without specific prior written permission.
19 1.1 nisimura *
20 1.1 nisimura * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 1.1 nisimura * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 1.1 nisimura * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 1.1 nisimura * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 1.1 nisimura * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 1.1 nisimura * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 1.1 nisimura * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 1.1 nisimura * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 1.1 nisimura * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 1.1 nisimura * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 1.1 nisimura */
31 1.1 nisimura
32 1.1 nisimura #include <sys/param.h>
33 1.1 nisimura #include <sys/systm.h>
34 1.1 nisimura
35 1.1 nisimura #include <dev/pci/pcivar.h>
36 1.1 nisimura #include <dev/pci/pcidevs.h>
37 1.1 nisimura #include <dev/pci/pciidereg.h>
38 1.1 nisimura #include <dev/pci/pciidevar.h>
39 1.1 nisimura
40 1.1 nisimura static void stpc_chip_map(struct pciide_softc *, struct pci_attach_args *);
41 1.6 thorpej static void stpc_setup_channel(struct ata_channel *);
42 1.1 nisimura
43 1.1 nisimura static int stpcide_match(struct device *, struct cfdata *, void *);
44 1.1 nisimura static void stpcide_attach(struct device *, struct device *, void *);
45 1.1 nisimura
46 1.1 nisimura const struct pciide_product_desc pciide_stpc_products[] = {
47 1.1 nisimura { 0x0228,
48 1.1 nisimura 0,
49 1.1 nisimura "STMicroelectronics STPC IDE Controller",
50 1.1 nisimura stpc_chip_map,
51 1.1 nisimura },
52 1.1 nisimura { 0, 0, NULL, NULL },
53 1.1 nisimura };
54 1.1 nisimura
55 1.1 nisimura CFATTACH_DECL(stpcide, sizeof(struct pciide_softc),
56 1.1 nisimura stpcide_match, stpcide_attach, NULL, NULL);
57 1.1 nisimura
58 1.1 nisimura static int
59 1.1 nisimura stpcide_match(struct device *parent, struct cfdata *match, void *aux)
60 1.1 nisimura {
61 1.1 nisimura struct pci_attach_args *pa = aux;
62 1.1 nisimura
63 1.1 nisimura if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SGSTHOMSON) {
64 1.1 nisimura if (pciide_lookup_product(pa->pa_id, pciide_stpc_products))
65 1.1 nisimura return (2);
66 1.1 nisimura }
67 1.1 nisimura return (0);
68 1.1 nisimura }
69 1.1 nisimura
70 1.1 nisimura static void
71 1.1 nisimura stpcide_attach(struct device *parent, struct device *self, void *aux)
72 1.1 nisimura {
73 1.1 nisimura struct pci_attach_args *pa = aux;
74 1.1 nisimura struct pciide_softc *sc = (struct pciide_softc *)self;
75 1.1 nisimura
76 1.1 nisimura pciide_common_attach(sc, pa,
77 1.1 nisimura pciide_lookup_product(pa->pa_id, pciide_stpc_products));
78 1.1 nisimura
79 1.1 nisimura }
80 1.1 nisimura
81 1.1 nisimura static void
82 1.1 nisimura stpc_chip_map(struct pciide_softc *sc, struct pci_attach_args *pa)
83 1.1 nisimura {
84 1.1 nisimura struct pciide_channel *cp;
85 1.1 nisimura int channel;
86 1.1 nisimura pcireg_t interface = PCI_INTERFACE(pa->pa_class);
87 1.1 nisimura bus_size_t cmdsize, ctlsize;
88 1.1 nisimura
89 1.1 nisimura if (pciide_chipen(sc, pa) == 0)
90 1.1 nisimura return;
91 1.1 nisimura
92 1.1 nisimura aprint_normal("%s: bus-master DMA support present",
93 1.1 nisimura sc->sc_wdcdev.sc_dev.dv_xname);
94 1.1 nisimura pciide_mapreg_dma(sc, pa);
95 1.1 nisimura aprint_normal("\n");
96 1.5 thorpej sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_DATA32;
97 1.1 nisimura if (sc->sc_dma_ok) {
98 1.5 thorpej sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA;
99 1.1 nisimura sc->sc_wdcdev.irqack = pciide_irqack;
100 1.1 nisimura }
101 1.1 nisimura sc->sc_wdcdev.PIO_cap = 4;
102 1.1 nisimura sc->sc_wdcdev.DMA_cap = 2;
103 1.1 nisimura sc->sc_wdcdev.UDMA_cap = 0;
104 1.1 nisimura sc->sc_wdcdev.set_modes = stpc_setup_channel;
105 1.1 nisimura sc->sc_wdcdev.channels = sc->wdc_chanarray;
106 1.1 nisimura sc->sc_wdcdev.nchannels = PCIIDE_NUM_CHANNELS;
107 1.1 nisimura
108 1.6 thorpej wdc_allocate_regs(&sc->sc_wdcdev);
109 1.6 thorpej
110 1.1 nisimura for (channel = 0; channel < sc->sc_wdcdev.nchannels; channel++) {
111 1.1 nisimura cp = &sc->pciide_channels[channel];
112 1.1 nisimura if (pciide_chansetup(sc, channel, interface) == 0)
113 1.1 nisimura continue;
114 1.1 nisimura pciide_mapchan(pa, cp, interface, &cmdsize, &ctlsize,
115 1.1 nisimura pciide_pci_intr);
116 1.1 nisimura }
117 1.1 nisimura }
118 1.1 nisimura
119 1.1 nisimura /*
120 1.1 nisimura * IDE timing register (0x40, 0x42, 0x44, and 0x46) assignment.
121 1.1 nisimura * 33MHz PCI system will have;
122 1.1 nisimura * DMA0 01-11-11
123 1.1 nisimura * DMA1 00-01-10
124 1.1 nisimura * DMA2 00-00-10
125 1.1 nisimura * PIO0 111-100
126 1.1 nisimura * PIO1 100-011
127 1.1 nisimura * PIO2 011-010
128 1.1 nisimura * PIO3 010-001
129 1.1 nisimura * PIO4 000-001
130 1.1 nisimura * MISC XYZW
131 1.1 nisimura */
132 1.1 nisimura static const u_int16_t dmatbl[] = { 0x7C00, 0x1800, 0x0800 };
133 1.1 nisimura static const u_int16_t piotbl[] = { 0x03C0, 0x0230, 0x01A0, 0x0110, 0x0010 };
134 1.1 nisimura
135 1.1 nisimura static void
136 1.6 thorpej stpc_setup_channel(struct ata_channel *chp)
137 1.1 nisimura {
138 1.1 nisimura struct pciide_channel *cp = (struct pciide_channel *)chp;
139 1.6 thorpej struct pciide_softc *sc = (struct pciide_softc *)cp->ata_channel.ch_wdc;
140 1.4 thorpej struct wdc_softc *wdc = &sc->sc_wdcdev;
141 1.4 thorpej int channel = chp->ch_channel;
142 1.1 nisimura struct ata_drive_datas *drvp;
143 1.1 nisimura u_int32_t idedma_ctl, idetim;
144 1.1 nisimura int drive, bits[2];
145 1.1 nisimura
146 1.1 nisimura /* setup DMA if needed */
147 1.1 nisimura pciide_channel_dma_setup(cp);
148 1.1 nisimura
149 1.1 nisimura idedma_ctl = 0;
150 1.1 nisimura bits[0] = bits[1] = 0x7F60; /* assume PIO2/DMA0 */
151 1.1 nisimura
152 1.1 nisimura /* Per drive settings */
153 1.1 nisimura for (drive = 0; drive < 2; drive++) {
154 1.1 nisimura drvp = &chp->ch_drive[drive];
155 1.1 nisimura /* If no drive, skip */
156 1.1 nisimura if ((drvp->drive_flags & DRIVE) == 0)
157 1.1 nisimura continue;
158 1.1 nisimura /* add timing values, setup DMA if needed */
159 1.4 thorpej if ((wdc->cap & WDC_CAPABILITY_DMA) &&
160 1.1 nisimura (drvp->drive_flags & DRIVE_DMA)) {
161 1.1 nisimura /* use Multiword DMA */
162 1.1 nisimura drvp->drive_flags &= ~DRIVE_UDMA;
163 1.1 nisimura idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
164 1.1 nisimura bits[drive] = 0xe; /* IOCHRDY,wr/post,rd/prefetch */
165 1.1 nisimura }
166 1.1 nisimura else {
167 1.1 nisimura /* PIO only */
168 1.1 nisimura drvp->drive_flags &= ~(DRIVE_UDMA | DRIVE_DMA);
169 1.1 nisimura bits[drive] = 0x8; /* IOCHRDY */
170 1.1 nisimura }
171 1.1 nisimura bits[drive] |= dmatbl[drvp->DMA_mode] | piotbl[drvp->PIO_mode];
172 1.1 nisimura }
173 1.1 nisimura #if 0
174 1.1 nisimura idetim = pci_conf_read(sc->sc_pc, sc->sc_tag,
175 1.1 nisimura (channel == 0) ? 0x40 : 0x44);
176 1.1 nisimura aprint_normal("wdc%d: IDETIM %08x -> %08x\n",
177 1.1 nisimura channel, idetim, (bits[1] << 16) | bits[0]);
178 1.1 nisimura #endif
179 1.1 nisimura idetim = (bits[1] << 16) | bits[0];
180 1.1 nisimura pci_conf_write(sc->sc_pc, sc->sc_tag,
181 1.1 nisimura (channel == 0) ? 0x40 : 0x44, idetim);
182 1.1 nisimura
183 1.1 nisimura if (idedma_ctl != 0) {
184 1.1 nisimura /* Add software bits in status register */
185 1.2 fvdl bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0,
186 1.2 fvdl idedma_ctl);
187 1.1 nisimura }
188 1.1 nisimura }
189