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_cyrix_chipinit(device_t dev);
55200171Smavstatic int ata_cyrix_ch_attach(device_t dev);
56200171Smavstatic int ata_cyrix_setmode(device_t dev, int target, int mode);
57183724Ssos
58183724Ssos/*
59183724Ssos * Cyrix chipset support functions
60183724Ssos */
61183724Ssosstatic int
62183724Ssosata_cyrix_probe(device_t dev)
63183724Ssos{
64183724Ssos    struct ata_pci_controller *ctlr = device_get_softc(dev);
65183724Ssos
66183724Ssos    if (pci_get_devid(dev) == ATA_CYRIX_5530) {
67183724Ssos	device_set_desc(dev, "Cyrix 5530 ATA33 controller");
68183724Ssos	ctlr->chipinit = ata_cyrix_chipinit;
69281140Smav	return (BUS_PROBE_LOW_PRIORITY);
70183724Ssos    }
71183724Ssos    return ENXIO;
72183724Ssos}
73183724Ssos
74183724Ssosstatic int
75183724Ssosata_cyrix_chipinit(device_t dev)
76183724Ssos{
77183724Ssos    struct ata_pci_controller *ctlr = device_get_softc(dev);
78183724Ssos
79183724Ssos    if (ata_setup_interrupt(dev, ata_generic_intr))
80183724Ssos	return ENXIO;
81200171Smav    ctlr->ch_attach = ata_cyrix_ch_attach;
82183724Ssos    ctlr->setmode = ata_cyrix_setmode;
83183724Ssos    return 0;
84183724Ssos}
85183724Ssos
86200171Smavstatic int
87200171Smavata_cyrix_ch_attach(device_t dev)
88183724Ssos{
89200171Smav	struct ata_channel *ch = device_get_softc(dev);
90200171Smav
91200171Smav	ch->dma.alignment = 16;
92200171Smav	ch->dma.max_iosize = 64 * DEV_BSIZE;
93216013Smarius	return (ata_pci_ch_attach(dev));
94200171Smav}
95183724Ssos
96200171Smavstatic int
97200171Smavata_cyrix_setmode(device_t dev, int target, int mode)
98200171Smav{
99200171Smav	struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
100200171Smav	struct ata_channel *ch = device_get_softc(dev);
101200171Smav	int devno = (ch->unit << 1) + target;
102200171Smav	int piomode;
103233282Smarius	static const uint32_t piotiming[] =
104200171Smav	    { 0x00009172, 0x00012171, 0x00020080, 0x00032010, 0x00040010 };
105233282Smarius	static const uint32_t dmatiming[] =
106233282Smarius	    { 0x00077771, 0x00012121, 0x00002020 };
107233282Smarius	static const uint32_t udmatiming[] =
108233282Smarius	    { 0x00921250, 0x00911140, 0x00911030 };
109183724Ssos
110200171Smav	mode = min(mode, ATA_UDMA2);
111183724Ssos	/* dont try to set the mode if we dont have the resource */
112183724Ssos	if (ctlr->r_res1) {
113200171Smav		if (mode >= ATA_UDMA0) {
114200171Smav			/* Set UDMA timings, and PIO4. */
115200171Smav			ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
116200171Smav			    0x24 + (devno << 3), udmatiming[mode & ATA_MODE_MASK]);
117200171Smav			piomode = ATA_PIO4;
118200171Smav		} else if (mode >= ATA_WDMA0) {
119200171Smav			/* Set WDMA timings, and respective PIO mode. */
120200171Smav			ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
121200171Smav			    0x24 + (devno << 3), dmatiming[mode & ATA_MODE_MASK]);
122200171Smav		        piomode = (mode == ATA_WDMA0) ? ATA_PIO0 :
123200171Smav			    (mode == ATA_WDMA1) ? ATA_PIO3 : ATA_PIO4;
124200171Smav		} else
125200171Smav			piomode = mode;
126200171Smav		/* Set PIO mode calculated above. */
127183724Ssos		ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
128200171Smav		    0x20 + (devno << 3), piotiming[ata_mode2idx(piomode)]);
129183724Ssos	}
130200171Smav	return (mode);
131183724Ssos}
132183724Ssos
133183724SsosATA_DECLARE_DRIVER(ata_cyrix);
134