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