xref: /freebsd/sys/dev/ata/chipsets/ata-cyrix.c (revision fdafd315)
113014ca0SSøren Schmidt /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
49a14aa01SUlrich Spörlein  * Copyright (c) 1998 - 2008 Søren Schmidt <sos@FreeBSD.org>
513014ca0SSøren Schmidt  * All rights reserved.
613014ca0SSøren Schmidt  *
713014ca0SSøren Schmidt  * Redistribution and use in source and binary forms, with or without
813014ca0SSøren Schmidt  * modification, are permitted provided that the following conditions
913014ca0SSøren Schmidt  * are met:
1013014ca0SSøren Schmidt  * 1. Redistributions of source code must retain the above copyright
1113014ca0SSøren Schmidt  *    notice, this list of conditions and the following disclaimer,
1213014ca0SSøren Schmidt  *    without modification, immediately at the beginning of the file.
1313014ca0SSøren Schmidt  * 2. Redistributions in binary form must reproduce the above copyright
1413014ca0SSøren Schmidt  *    notice, this list of conditions and the following disclaimer in the
1513014ca0SSøren Schmidt  *    documentation and/or other materials provided with the distribution.
1613014ca0SSøren Schmidt  *
1713014ca0SSøren Schmidt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1813014ca0SSøren Schmidt  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1913014ca0SSøren Schmidt  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2013014ca0SSøren Schmidt  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2113014ca0SSøren Schmidt  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2213014ca0SSøren Schmidt  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2313014ca0SSøren Schmidt  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2413014ca0SSøren Schmidt  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2513014ca0SSøren Schmidt  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2613014ca0SSøren Schmidt  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2713014ca0SSøren Schmidt  */
2813014ca0SSøren Schmidt 
2913014ca0SSøren Schmidt #include <sys/param.h>
3013014ca0SSøren Schmidt #include <sys/module.h>
3113014ca0SSøren Schmidt #include <sys/systm.h>
3213014ca0SSøren Schmidt #include <sys/kernel.h>
3313014ca0SSøren Schmidt #include <sys/ata.h>
3413014ca0SSøren Schmidt #include <sys/bus.h>
3513014ca0SSøren Schmidt #include <sys/endian.h>
3613014ca0SSøren Schmidt #include <sys/malloc.h>
3713014ca0SSøren Schmidt #include <sys/lock.h>
3813014ca0SSøren Schmidt #include <sys/mutex.h>
3913014ca0SSøren Schmidt #include <sys/sema.h>
4013014ca0SSøren Schmidt #include <sys/taskqueue.h>
4113014ca0SSøren Schmidt #include <vm/uma.h>
4213014ca0SSøren Schmidt #include <machine/stdarg.h>
4313014ca0SSøren Schmidt #include <machine/resource.h>
4413014ca0SSøren Schmidt #include <machine/bus.h>
4513014ca0SSøren Schmidt #include <sys/rman.h>
4613014ca0SSøren Schmidt #include <dev/pci/pcivar.h>
4713014ca0SSøren Schmidt #include <dev/pci/pcireg.h>
4813014ca0SSøren Schmidt #include <dev/ata/ata-all.h>
4913014ca0SSøren Schmidt #include <dev/ata/ata-pci.h>
5013014ca0SSøren Schmidt #include <ata_if.h>
5113014ca0SSøren Schmidt 
5213014ca0SSøren Schmidt /* local prototypes */
5313014ca0SSøren Schmidt static int ata_cyrix_chipinit(device_t dev);
54066f913aSAlexander Motin static int ata_cyrix_ch_attach(device_t dev);
55066f913aSAlexander Motin static int ata_cyrix_setmode(device_t dev, int target, int mode);
5613014ca0SSøren Schmidt 
5713014ca0SSøren Schmidt /*
5813014ca0SSøren Schmidt  * Cyrix chipset support functions
5913014ca0SSøren Schmidt  */
6013014ca0SSøren Schmidt static int
ata_cyrix_probe(device_t dev)6113014ca0SSøren Schmidt ata_cyrix_probe(device_t dev)
6213014ca0SSøren Schmidt {
6313014ca0SSøren Schmidt     struct ata_pci_controller *ctlr = device_get_softc(dev);
6413014ca0SSøren Schmidt 
6513014ca0SSøren Schmidt     if (pci_get_devid(dev) == ATA_CYRIX_5530) {
6613014ca0SSøren Schmidt 	device_set_desc(dev, "Cyrix 5530 ATA33 controller");
6713014ca0SSøren Schmidt 	ctlr->chipinit = ata_cyrix_chipinit;
683036de3cSAlexander Motin 	return (BUS_PROBE_LOW_PRIORITY);
6913014ca0SSøren Schmidt     }
7013014ca0SSøren Schmidt     return ENXIO;
7113014ca0SSøren Schmidt }
7213014ca0SSøren Schmidt 
7313014ca0SSøren Schmidt static int
ata_cyrix_chipinit(device_t dev)7413014ca0SSøren Schmidt ata_cyrix_chipinit(device_t dev)
7513014ca0SSøren Schmidt {
7613014ca0SSøren Schmidt     struct ata_pci_controller *ctlr = device_get_softc(dev);
7713014ca0SSøren Schmidt 
7813014ca0SSøren Schmidt     if (ata_setup_interrupt(dev, ata_generic_intr))
7913014ca0SSøren Schmidt 	return ENXIO;
80066f913aSAlexander Motin     ctlr->ch_attach = ata_cyrix_ch_attach;
8113014ca0SSøren Schmidt     ctlr->setmode = ata_cyrix_setmode;
8213014ca0SSøren Schmidt     return 0;
8313014ca0SSøren Schmidt }
8413014ca0SSøren Schmidt 
85066f913aSAlexander Motin static int
ata_cyrix_ch_attach(device_t dev)86066f913aSAlexander Motin ata_cyrix_ch_attach(device_t dev)
8713014ca0SSøren Schmidt {
88066f913aSAlexander Motin 	struct ata_channel *ch = device_get_softc(dev);
89066f913aSAlexander Motin 
90066f913aSAlexander Motin 	ch->dma.alignment = 16;
91066f913aSAlexander Motin 	ch->dma.max_iosize = 64 * DEV_BSIZE;
921510a2b0SMarius Strobl 	return (ata_pci_ch_attach(dev));
93066f913aSAlexander Motin }
94066f913aSAlexander Motin 
95066f913aSAlexander Motin static int
ata_cyrix_setmode(device_t dev,int target,int mode)96066f913aSAlexander Motin ata_cyrix_setmode(device_t dev, int target, int mode)
97066f913aSAlexander Motin {
98066f913aSAlexander Motin 	struct ata_pci_controller *ctlr = device_get_softc(device_get_parent(dev));
99066f913aSAlexander Motin 	struct ata_channel *ch = device_get_softc(dev);
100066f913aSAlexander Motin 	int devno = (ch->unit << 1) + target;
101066f913aSAlexander Motin 	int piomode;
1025187458fSMarius Strobl 	static const uint32_t piotiming[] =
10313014ca0SSøren Schmidt 	    { 0x00009172, 0x00012171, 0x00020080, 0x00032010, 0x00040010 };
1045187458fSMarius Strobl 	static const uint32_t dmatiming[] =
1055187458fSMarius Strobl 	    { 0x00077771, 0x00012121, 0x00002020 };
1065187458fSMarius Strobl 	static const uint32_t udmatiming[] =
1075187458fSMarius Strobl 	    { 0x00921250, 0x00911140, 0x00911030 };
10813014ca0SSøren Schmidt 
109066f913aSAlexander Motin 	mode = min(mode, ATA_UDMA2);
11013014ca0SSøren Schmidt 	/* dont try to set the mode if we dont have the resource */
11113014ca0SSøren Schmidt 	if (ctlr->r_res1) {
11213014ca0SSøren Schmidt 		if (mode >= ATA_UDMA0) {
113066f913aSAlexander Motin 			/* Set UDMA timings, and PIO4. */
11413014ca0SSøren Schmidt 			ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
11513014ca0SSøren Schmidt 			    0x24 + (devno << 3), udmatiming[mode & ATA_MODE_MASK]);
116066f913aSAlexander Motin 			piomode = ATA_PIO4;
117066f913aSAlexander Motin 		} else if (mode >= ATA_WDMA0) {
118066f913aSAlexander Motin 			/* Set WDMA timings, and respective PIO mode. */
11913014ca0SSøren Schmidt 			ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
12013014ca0SSøren Schmidt 			    0x24 + (devno << 3), dmatiming[mode & ATA_MODE_MASK]);
121066f913aSAlexander Motin 		        piomode = (mode == ATA_WDMA0) ? ATA_PIO0 :
122066f913aSAlexander Motin 			    (mode == ATA_WDMA1) ? ATA_PIO3 : ATA_PIO4;
123066f913aSAlexander Motin 		} else
124066f913aSAlexander Motin 			piomode = mode;
125066f913aSAlexander Motin 		/* Set PIO mode calculated above. */
12613014ca0SSøren Schmidt 		ATA_OUTL(ch->r_io[ATA_BMCMD_PORT].res,
127066f913aSAlexander Motin 		    0x20 + (devno << 3), piotiming[ata_mode2idx(piomode)]);
12813014ca0SSøren Schmidt 	}
129066f913aSAlexander Motin 	return (mode);
13013014ca0SSøren Schmidt }
13113014ca0SSøren Schmidt 
13213014ca0SSøren Schmidt ATA_DECLARE_DRIVER(ata_cyrix);
133