1183724Ssos/*-
2230132Suqs * Copyright (c) 1998 - 2008 S��ren Schmidt <sos@FreeBSD.org>
3183724Ssos * All rights reserved.
4183724Ssos *
5183724Ssos * Redistribution and use in source and binary forms, with or without
6183724Ssos * modification, are permitted provided that the following conditions
7183724Ssos * are met:
8183724Ssos * 1. Redistributions of source code must retain the above copyright
9183724Ssos *    notice, this list of conditions and the following disclaimer,
10183724Ssos *    without modification, immediately at the beginning of the file.
11183724Ssos * 2. Redistributions in binary form must reproduce the above copyright
12183724Ssos *    notice, this list of conditions and the following disclaimer in the
13183724Ssos *    documentation and/or other materials provided with the distribution.
14183724Ssos *
15183724Ssos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16183724Ssos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17183724Ssos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18183724Ssos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19183724Ssos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20183724Ssos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21183724Ssos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22183724Ssos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23183724Ssos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24183724Ssos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25183724Ssos */
26183724Ssos
27183724Ssos#include <sys/cdefs.h>
28183724Ssos__FBSDID("$FreeBSD$");
29183724Ssos
30183724Ssos#include <sys/param.h>
31183724Ssos#include <sys/module.h>
32183724Ssos#include <sys/systm.h>
33183724Ssos#include <sys/kernel.h>
34183724Ssos#include <sys/ata.h>
35183724Ssos#include <sys/bus.h>
36183724Ssos#include <sys/endian.h>
37183724Ssos#include <sys/malloc.h>
38183724Ssos#include <sys/lock.h>
39183724Ssos#include <sys/mutex.h>
40183724Ssos#include <sys/sema.h>
41183724Ssos#include <sys/taskqueue.h>
42183724Ssos#include <vm/uma.h>
43183724Ssos#include <machine/stdarg.h>
44183724Ssos#include <machine/resource.h>
45183724Ssos#include <machine/bus.h>
46183724Ssos#include <sys/rman.h>
47183724Ssos#include <dev/pci/pcivar.h>
48183724Ssos#include <dev/pci/pcireg.h>
49183724Ssos#include <dev/ata/ata-all.h>
50183724Ssos#include <dev/ata/ata-pci.h>
51183724Ssos#include <ata_if.h>
52183724Ssos
53183724Ssos/* local prototypes */
54183724Ssosstatic int ata_ite_chipinit(device_t dev);
55200171Smavstatic int ata_ite_ch_attach(device_t dev);
56200171Smavstatic int ata_ite_821x_setmode(device_t dev, int target, int mode);
57200171Smavstatic int ata_ite_8213_setmode(device_t dev, int target, int mode);
58183724Ssos
59183724Ssos/*
60183724Ssos * Integrated Technology Express Inc. (ITE) chipset support functions
61183724Ssos */
62183724Ssosstatic int
63183724Ssosata_ite_probe(device_t dev)
64183724Ssos{
65183724Ssos    struct ata_pci_controller *ctlr = device_get_softc(dev);
66242625Sdim    static const struct ata_chip_id ids[] =
67183724Ssos    {{ ATA_IT8213F, 0x00, 0x00, 0x00, ATA_UDMA6, "IT8213F" },
68183724Ssos     { ATA_IT8212F, 0x00, 0x00, 0x00, ATA_UDMA6, "IT8212F" },
69183724Ssos     { ATA_IT8211F, 0x00, 0x00, 0x00, ATA_UDMA6, "IT8211F" },
70183724Ssos     { 0, 0, 0, 0, 0, 0}};
71183724Ssos
72183724Ssos    if (pci_get_vendor(dev) != ATA_ITE_ID)
73183724Ssos	return ENXIO;
74183724Ssos
75183724Ssos    if (!(ctlr->chip = ata_match_chip(dev, ids)))
76183724Ssos	return ENXIO;
77183724Ssos
78183724Ssos    ata_set_desc(dev);
79183724Ssos    ctlr->chipinit = ata_ite_chipinit;
80194893Smav    return (BUS_PROBE_DEFAULT);
81183724Ssos}
82183724Ssos
83183724Ssosstatic int
84183724Ssosata_ite_chipinit(device_t dev)
85183724Ssos{
86183724Ssos    struct ata_pci_controller *ctlr = device_get_softc(dev);
87183724Ssos
88183724Ssos    if (ata_setup_interrupt(dev, ata_generic_intr))
89183724Ssos	return ENXIO;
90183724Ssos
91183724Ssos    if (ctlr->chip->chipid == ATA_IT8213F) {
92183724Ssos	/* the ITE 8213F only has one channel */
93183724Ssos	ctlr->channels = 1;
94183724Ssos
95183724Ssos	ctlr->setmode = ata_ite_8213_setmode;
96183724Ssos    }
97183724Ssos    else {
98183724Ssos	/* set PCI mode and 66Mhz reference clock */
99183724Ssos	pci_write_config(dev, 0x50, pci_read_config(dev, 0x50, 1) & ~0x83, 1);
100183724Ssos
101183724Ssos	/* set default active & recover timings */
102183724Ssos	pci_write_config(dev, 0x54, 0x31, 1);
103183724Ssos	pci_write_config(dev, 0x56, 0x31, 1);
104183724Ssos
105183724Ssos	ctlr->setmode = ata_ite_821x_setmode;
106200753Smav	/* No timing restrictions initally. */
107237107Smarius	ctlr->chipset_data = NULL;
108183724Ssos    }
109200171Smav    ctlr->ch_attach = ata_ite_ch_attach;
110237107Smarius    return (0);
111183724Ssos}
112200171Smav
113200171Smavstatic int
114200171Smavata_ite_ch_attach(device_t dev)
115200171Smav{
116200171Smav	struct ata_channel *ch = device_get_softc(dev);
117200171Smav	int error;
118183724Ssos
119200171Smav	error = ata_pci_ch_attach(dev);
120200171Smav	ch->flags |= ATA_CHECKS_CABLE;
121237107Smarius	ch->flags |= ATA_NO_ATAPI_DMA;
122200171Smav	return (error);
123200171Smav}
124200171Smav
125200171Smavstatic int
126200171Smavata_ite_821x_setmode(device_t dev, int target, int mode)
127183724Ssos{
128200171Smav	device_t parent = device_get_parent(dev);
129200171Smav	struct ata_pci_controller *ctlr = device_get_softc(parent);
130200171Smav	struct ata_channel *ch = device_get_softc(dev);
131200171Smav	int devno = (ch->unit << 1) + target;
132200171Smav	int piomode;
133200753Smav	uint8_t *timings = (uint8_t*)(&ctlr->chipset_data);
134233282Smarius	static const uint8_t udmatiming[] =
135200171Smav		{ 0x44, 0x42, 0x31, 0x21, 0x11, 0xa2, 0x91 };
136233282Smarius	static const uint8_t chtiming[] =
137200171Smav		{ 0xaa, 0xa3, 0xa1, 0x33, 0x31, 0x88, 0x32, 0x31 };
138183724Ssos
139200171Smav	mode = min(mode, ctlr->chip->max_dma);
140200171Smav	/* check the CBLID bits for 80 conductor cable detection */
141209872Smav	if (ata_dma_check_80pin && mode > ATA_UDMA2 &&
142209872Smav	    (pci_read_config(parent, 0x40, 2) &
143183724Ssos			     (ch->unit ? (1<<3) : (1<<2)))) {
144200171Smav		ata_print_cable(dev, "controller");
145200171Smav		mode = ATA_UDMA2;
146200171Smav	}
147183724Ssos	if (mode >= ATA_UDMA0) {
148200171Smav		/* enable UDMA mode */
149200171Smav		pci_write_config(parent, 0x50,
150200171Smav			     pci_read_config(parent, 0x50, 1) &
151183724Ssos			     ~(1 << (devno + 3)), 1);
152200171Smav		/* set UDMA timing */
153200171Smav		pci_write_config(parent,
154200171Smav			     0x56 + (ch->unit << 2) + target,
155183724Ssos			     udmatiming[mode & ATA_MODE_MASK], 1);
156200171Smav		piomode = ATA_PIO4;
157200171Smav	} else {
158200171Smav		/* disable UDMA mode */
159200171Smav		pci_write_config(parent, 0x50,
160200171Smav			     pci_read_config(parent, 0x50, 1) |
161183724Ssos			     (1 << (devno + 3)), 1);
162200171Smav		piomode = mode;
163183724Ssos	}
164200753Smav	timings[devno] = chtiming[ata_mode2idx(piomode)];
165200171Smav	/* set active and recover timing (shared between master & slave) */
166200753Smav	pci_write_config(parent, 0x54 + (ch->unit << 2),
167200753Smav	    max(timings[ch->unit << 1], timings[(ch->unit << 1) + 1]), 1);
168200171Smav	return (mode);
169183724Ssos}
170183724Ssos
171200171Smavstatic int
172200171Smavata_ite_8213_setmode(device_t dev, int target, int mode)
173183724Ssos{
174200171Smav	device_t parent = device_get_parent(dev);
175200171Smav	struct ata_pci_controller *ctlr = device_get_softc(parent);
176200171Smav	int piomode;
177200171Smav	u_int16_t reg40 = pci_read_config(parent, 0x40, 2);
178200171Smav	u_int8_t reg44 = pci_read_config(parent, 0x44, 1);
179200171Smav	u_int8_t reg48 = pci_read_config(parent, 0x48, 1);
180200171Smav	u_int16_t reg4a = pci_read_config(parent, 0x4a, 2);
181200171Smav	u_int16_t reg54 = pci_read_config(parent, 0x54, 2);
182200171Smav	u_int16_t mask40 = 0, new40 = 0;
183200171Smav	u_int8_t mask44 = 0, new44 = 0;
184233282Smarius	static const uint8_t timings[] =
185233282Smarius	    { 0x00, 0x00, 0x10, 0x21, 0x23, 0x00, 0x21, 0x23 };
186233282Smarius	static const uint8_t utimings[] =
187233282Smarius	    { 0x00, 0x01, 0x02, 0x01, 0x02, 0x01, 0x02 };
188183724Ssos
189200171Smav	mode = min(mode, ctlr->chip->max_dma);
190183724Ssos
191209872Smav	if (ata_dma_check_80pin && mode > ATA_UDMA2 &&
192209872Smav	    !(reg54 & (0x10 << target))) {
193200171Smav		ata_print_cable(dev, "controller");
194200171Smav		mode = ATA_UDMA2;
195200171Smav	}
196200171Smav	/* Enable/disable UDMA and set timings. */
197183724Ssos	if (mode >= ATA_UDMA0) {
198200171Smav	    pci_write_config(parent, 0x48, reg48 | (0x0001 << target), 2);
199200171Smav	    pci_write_config(parent, 0x4a,
200200171Smav			     (reg4a & ~(0x3 << (target << 2))) |
201200171Smav			     (utimings[mode & ATA_MODE_MASK] << (target<<2)), 2);
202200171Smav	    piomode = ATA_PIO4;
203200171Smav	} else {
204200171Smav	    pci_write_config(parent, 0x48, reg48 & ~(0x0001 << target), 2);
205200171Smav	    pci_write_config(parent, 0x4a, (reg4a & ~(0x3 << (target << 2))),2);
206200171Smav	    piomode = mode;
207183724Ssos	}
208200171Smav	/* Set UDMA reference clock (33/66/133MHz). */
209200171Smav	reg54 &= ~(0x1001 << target);
210183724Ssos	if (mode >= ATA_UDMA5)
211200171Smav	    reg54 |= (0x1000 << target);
212200171Smav	else if (mode >= ATA_UDMA3)
213200171Smav	    reg54 |= (0x1 << target);
214200171Smav	pci_write_config(parent, 0x54, reg54, 2);
215200171Smav	/* Allow PIO/WDMA timing controls. */
216183724Ssos	reg40 &= 0xff00;
217183724Ssos	reg40 |= 0x4033;
218200171Smav	/* Set PIO/WDMA timings. */
219200171Smav	if (target == 0) {
220200459Smarius	    reg40 |= (ata_atapi(dev, target) ? 0x04 : 0x00);
221183724Ssos	    mask40 = 0x3300;
222200171Smav	    new40 = timings[ata_mode2idx(piomode)] << 8;
223183724Ssos	}
224183724Ssos	else {
225200459Smarius	    reg40 |= (ata_atapi(dev, target) ? 0x40 : 0x00);
226183724Ssos	    mask44 = 0x0f;
227200171Smav	    new44 = ((timings[ata_mode2idx(piomode)] & 0x30) >> 2) |
228200171Smav		    (timings[ata_mode2idx(piomode)] & 0x03);
229183724Ssos	}
230200171Smav	pci_write_config(parent, 0x40, (reg40 & ~mask40) | new40, 4);
231200171Smav	pci_write_config(parent, 0x44, (reg44 & ~mask44) | new44, 1);
232200171Smav	return (mode);
233183724Ssos}
234183724Ssos
235183724SsosATA_DECLARE_DRIVER(ata_ite);
236