cypide.c revision 1.29.2.2 1 1.29.2.2 tls /* $NetBSD: cypide.c,v 1.29.2.2 2014/08/20 00:03:42 tls Exp $ */
2 1.1 bouyer
3 1.1 bouyer /*
4 1.1 bouyer * Copyright (c) 1999, 2000, 2001 Manuel Bouyer.
5 1.1 bouyer *
6 1.1 bouyer * Redistribution and use in source and binary forms, with or without
7 1.1 bouyer * modification, are permitted provided that the following conditions
8 1.1 bouyer * are met:
9 1.1 bouyer * 1. Redistributions of source code must retain the above copyright
10 1.1 bouyer * notice, this list of conditions and the following disclaimer.
11 1.1 bouyer * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 bouyer * notice, this list of conditions and the following disclaimer in the
13 1.1 bouyer * documentation and/or other materials provided with the distribution.
14 1.1 bouyer *
15 1.1 bouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1 bouyer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 1.1 bouyer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 1.14 perry * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 1.1 bouyer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 1.1 bouyer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 1.1 bouyer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 1.1 bouyer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 1.1 bouyer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 1.1 bouyer * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 1.1 bouyer *
26 1.1 bouyer */
27 1.1 bouyer
28 1.15 lukem #include <sys/cdefs.h>
29 1.29.2.2 tls __KERNEL_RCSID(0, "$NetBSD: cypide.c,v 1.29.2.2 2014/08/20 00:03:42 tls Exp $");
30 1.15 lukem
31 1.1 bouyer #include <sys/param.h>
32 1.1 bouyer #include <sys/systm.h>
33 1.1 bouyer #include <sys/malloc.h>
34 1.1 bouyer
35 1.1 bouyer #include <dev/pci/pcivar.h>
36 1.1 bouyer #include <dev/pci/pcidevs.h>
37 1.1 bouyer #include <dev/pci/pciidereg.h>
38 1.1 bouyer #include <dev/pci/pciidevar.h>
39 1.1 bouyer #include <dev/pci/pciide_cy693_reg.h>
40 1.1 bouyer #include <dev/pci/cy82c693var.h>
41 1.1 bouyer
42 1.24 dyoung static void cy693_chip_map(struct pciide_softc*, const struct pci_attach_args*);
43 1.11 thorpej static void cy693_setup_channel(struct ata_channel*);
44 1.1 bouyer
45 1.21 cube static int cypide_match(device_t, cfdata_t, void *);
46 1.21 cube static void cypide_attach(device_t, device_t, void *);
47 1.1 bouyer
48 1.21 cube CFATTACH_DECL_NEW(cypide, sizeof(struct pciide_softc),
49 1.29.2.2 tls cypide_match, cypide_attach, pciide_detach, NULL);
50 1.1 bouyer
51 1.2 thorpej static const struct pciide_product_desc pciide_cypress_products[] = {
52 1.1 bouyer { PCI_PRODUCT_CONTAQ_82C693,
53 1.1 bouyer IDE_16BIT_IOSPACE,
54 1.1 bouyer "Cypress 82C693 IDE Controller",
55 1.1 bouyer cy693_chip_map,
56 1.1 bouyer },
57 1.1 bouyer { 0,
58 1.1 bouyer 0,
59 1.1 bouyer NULL,
60 1.1 bouyer NULL
61 1.1 bouyer }
62 1.1 bouyer };
63 1.1 bouyer
64 1.2 thorpej static int
65 1.21 cube cypide_match(device_t parent, cfdata_t match, void *aux)
66 1.1 bouyer {
67 1.1 bouyer struct pci_attach_args *pa = aux;
68 1.1 bouyer
69 1.3 mycroft if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_CONTAQ &&
70 1.3 mycroft PCI_CLASS(pa->pa_class) == PCI_CLASS_MASS_STORAGE &&
71 1.3 mycroft PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_IDE) {
72 1.1 bouyer if (pciide_lookup_product(pa->pa_id, pciide_cypress_products))
73 1.1 bouyer return (2);
74 1.1 bouyer }
75 1.1 bouyer return (0);
76 1.1 bouyer }
77 1.1 bouyer
78 1.2 thorpej static void
79 1.21 cube cypide_attach(device_t parent, device_t self, void *aux)
80 1.1 bouyer {
81 1.1 bouyer struct pci_attach_args *pa = aux;
82 1.21 cube struct pciide_softc *sc = device_private(self);
83 1.21 cube
84 1.29.2.1 bouyer self->dv_maxphys = MIN(parent->dv_maxphys, MACHINE_MAXPHYS);
85 1.29.2.1 bouyer
86 1.21 cube sc->sc_wdcdev.sc_atac.atac_dev = self;
87 1.1 bouyer
88 1.1 bouyer pciide_common_attach(sc, pa,
89 1.1 bouyer pciide_lookup_product(pa->pa_id, pciide_cypress_products));
90 1.1 bouyer
91 1.1 bouyer }
92 1.1 bouyer
93 1.2 thorpej static void
94 1.24 dyoung cy693_chip_map(struct pciide_softc *sc, const struct pci_attach_args *pa)
95 1.14 perry {
96 1.1 bouyer struct pciide_channel *cp;
97 1.1 bouyer pcireg_t interface = PCI_INTERFACE(pa->pa_class);
98 1.1 bouyer
99 1.1 bouyer if (pciide_chipen(sc, pa) == 0)
100 1.1 bouyer return;
101 1.1 bouyer
102 1.1 bouyer /*
103 1.1 bouyer * this chip has 2 PCI IDE functions, one for primary and one for
104 1.1 bouyer * secondary. So we need to call pciide_mapregs_compat() with
105 1.1 bouyer * the real channel
106 1.1 bouyer */
107 1.1 bouyer if (pa->pa_function == 1) {
108 1.1 bouyer sc->sc_cy_compatchan = 0;
109 1.1 bouyer } else if (pa->pa_function == 2) {
110 1.1 bouyer sc->sc_cy_compatchan = 1;
111 1.1 bouyer } else {
112 1.21 cube aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
113 1.21 cube "unexpected PCI function %d\n", pa->pa_function);
114 1.1 bouyer return;
115 1.1 bouyer }
116 1.1 bouyer if (interface & PCIIDE_INTERFACE_BUS_MASTER_DMA) {
117 1.21 cube aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
118 1.21 cube "bus-master DMA support present\n");
119 1.1 bouyer pciide_mapreg_dma(sc, pa);
120 1.1 bouyer } else {
121 1.21 cube aprint_normal_dev(sc->sc_wdcdev.sc_atac.atac_dev,
122 1.21 cube "hardware does not support DMA\n");
123 1.1 bouyer sc->sc_dma_ok = 0;
124 1.1 bouyer }
125 1.1 bouyer
126 1.1 bouyer sc->sc_cy_handle = cy82c693_init(pa->pa_iot);
127 1.1 bouyer if (sc->sc_cy_handle == NULL) {
128 1.21 cube aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
129 1.21 cube "unable to map hyperCache control registers\n");
130 1.1 bouyer sc->sc_dma_ok = 0;
131 1.1 bouyer }
132 1.1 bouyer
133 1.13 thorpej sc->sc_wdcdev.sc_atac.atac_cap = ATAC_CAP_DATA16 | ATAC_CAP_DATA32;
134 1.1 bouyer if (sc->sc_dma_ok) {
135 1.13 thorpej sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DMA;
136 1.1 bouyer sc->sc_wdcdev.irqack = pciide_irqack;
137 1.1 bouyer }
138 1.13 thorpej sc->sc_wdcdev.sc_atac.atac_pio_cap = 4;
139 1.13 thorpej sc->sc_wdcdev.sc_atac.atac_dma_cap = 2;
140 1.13 thorpej sc->sc_wdcdev.sc_atac.atac_set_modes = cy693_setup_channel;
141 1.1 bouyer
142 1.13 thorpej sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanarray;
143 1.13 thorpej sc->sc_wdcdev.sc_atac.atac_nchannels = 1;
144 1.29 bouyer sc->sc_wdcdev.wdc_maxdrives = 2;
145 1.1 bouyer
146 1.11 thorpej wdc_allocate_regs(&sc->sc_wdcdev);
147 1.11 thorpej
148 1.1 bouyer /* Only one channel for this chip; if we are here it's enabled */
149 1.1 bouyer cp = &sc->pciide_channels[0];
150 1.11 thorpej sc->wdc_chanarray[0] = &cp->ata_channel;
151 1.1 bouyer cp->name = PCIIDE_CHANNEL_NAME(0);
152 1.11 thorpej cp->ata_channel.ch_channel = 0;
153 1.13 thorpej cp->ata_channel.ch_atac = &sc->sc_wdcdev.sc_atac;
154 1.11 thorpej cp->ata_channel.ch_queue =
155 1.5 thorpej malloc(sizeof(struct ata_queue), M_DEVBUF, M_NOWAIT);
156 1.11 thorpej if (cp->ata_channel.ch_queue == NULL) {
157 1.1 bouyer aprint_error("%s primary channel: "
158 1.1 bouyer "can't allocate memory for command queue",
159 1.21 cube device_xname(sc->sc_wdcdev.sc_atac.atac_dev));
160 1.1 bouyer return;
161 1.1 bouyer }
162 1.21 cube aprint_normal_dev(sc->sc_wdcdev.sc_atac.atac_dev,
163 1.21 cube "primary channel %s to ",
164 1.1 bouyer (interface & PCIIDE_INTERFACE_SETTABLE(0)) ?
165 1.1 bouyer "configured" : "wired");
166 1.1 bouyer if (interface & PCIIDE_INTERFACE_PCI(0)) {
167 1.1 bouyer aprint_normal("native-PCI mode\n");
168 1.23 jakllsch pciide_mapregs_native(pa, cp, pciide_pci_intr);
169 1.1 bouyer } else {
170 1.1 bouyer aprint_normal("compatibility mode\n");
171 1.23 jakllsch pciide_mapregs_compat(pa, cp, sc->sc_cy_compatchan);
172 1.11 thorpej if ((cp->ata_channel.ch_flags & ATACH_DISABLED) == 0)
173 1.8 bouyer pciide_map_compat_intr(pa, cp, sc->sc_cy_compatchan);
174 1.1 bouyer }
175 1.11 thorpej wdcattach(&cp->ata_channel);
176 1.1 bouyer }
177 1.1 bouyer
178 1.2 thorpej static void
179 1.11 thorpej cy693_setup_channel(struct ata_channel *chp)
180 1.1 bouyer {
181 1.1 bouyer struct ata_drive_datas *drvp;
182 1.1 bouyer int drive;
183 1.1 bouyer u_int32_t cy_cmd_ctrl;
184 1.1 bouyer u_int32_t idedma_ctl;
185 1.12 thorpej struct pciide_channel *cp = CHAN_TO_PCHAN(chp);
186 1.12 thorpej struct pciide_softc *sc = CHAN_TO_PCIIDE(chp);
187 1.1 bouyer int dma_mode = -1;
188 1.1 bouyer
189 1.10 thorpej ATADEBUG_PRINT(("cy693_chip_map: old timings reg 0x%x\n",
190 1.1 bouyer pci_conf_read(sc->sc_pc, sc->sc_tag, CY_CMD_CTRL)),DEBUG_PROBE);
191 1.1 bouyer
192 1.1 bouyer cy_cmd_ctrl = idedma_ctl = 0;
193 1.1 bouyer
194 1.1 bouyer /* setup DMA if needed */
195 1.1 bouyer pciide_channel_dma_setup(cp);
196 1.1 bouyer
197 1.1 bouyer for (drive = 0; drive < 2; drive++) {
198 1.1 bouyer drvp = &chp->ch_drive[drive];
199 1.1 bouyer /* If no drive, skip */
200 1.29 bouyer if (drvp->drive_type == ATA_DRIVET_NONE)
201 1.1 bouyer continue;
202 1.1 bouyer /* add timing values, setup DMA if needed */
203 1.29 bouyer if (drvp->drive_flags & ATA_DRIVE_DMA) {
204 1.1 bouyer idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
205 1.1 bouyer /* use Multiword DMA */
206 1.1 bouyer if (dma_mode == -1 || dma_mode > drvp->DMA_mode)
207 1.1 bouyer dma_mode = drvp->DMA_mode;
208 1.1 bouyer }
209 1.1 bouyer cy_cmd_ctrl |= (cy_pio_pulse[drvp->PIO_mode] <<
210 1.1 bouyer CY_CMD_CTRL_IOW_PULSE_OFF(drive));
211 1.1 bouyer cy_cmd_ctrl |= (cy_pio_rec[drvp->PIO_mode] <<
212 1.1 bouyer CY_CMD_CTRL_IOW_REC_OFF(drive));
213 1.1 bouyer cy_cmd_ctrl |= (cy_pio_pulse[drvp->PIO_mode] <<
214 1.1 bouyer CY_CMD_CTRL_IOR_PULSE_OFF(drive));
215 1.1 bouyer cy_cmd_ctrl |= (cy_pio_rec[drvp->PIO_mode] <<
216 1.1 bouyer CY_CMD_CTRL_IOR_REC_OFF(drive));
217 1.1 bouyer }
218 1.1 bouyer pci_conf_write(sc->sc_pc, sc->sc_tag, CY_CMD_CTRL, cy_cmd_ctrl);
219 1.1 bouyer chp->ch_drive[0].DMA_mode = dma_mode;
220 1.1 bouyer chp->ch_drive[1].DMA_mode = dma_mode;
221 1.1 bouyer
222 1.1 bouyer if (dma_mode == -1)
223 1.1 bouyer dma_mode = 0;
224 1.1 bouyer
225 1.1 bouyer if (sc->sc_cy_handle != NULL) {
226 1.1 bouyer /* Note: `multiple' is implied. */
227 1.1 bouyer cy82c693_write(sc->sc_cy_handle,
228 1.1 bouyer (sc->sc_cy_compatchan == 0) ?
229 1.1 bouyer CY_DMA_IDX_PRIMARY : CY_DMA_IDX_SECONDARY, dma_mode);
230 1.1 bouyer }
231 1.1 bouyer
232 1.1 bouyer if (idedma_ctl != 0) {
233 1.1 bouyer /* Add software bits in status register */
234 1.4 fvdl bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0,
235 1.4 fvdl idedma_ctl);
236 1.1 bouyer }
237 1.10 thorpej ATADEBUG_PRINT(("cy693_chip_map: new timings reg 0x%x\n",
238 1.1 bouyer pci_conf_read(sc->sc_pc, sc->sc_tag, CY_CMD_CTRL)), DEBUG_PROBE);
239 1.1 bouyer }
240