gcscide.c revision 1.14
11.14Sbouyer/*	$NetBSD: gcscide.c,v 1.14 2012/07/31 15:50:32 bouyer Exp $	*/
21.1Sxtraeme
31.6Sxtraeme/*-
41.6Sxtraeme * Copyright (c) 2007 Juan Romero Pardines.
51.1Sxtraeme * All rights reserved.
61.1Sxtraeme *
71.1Sxtraeme * Redistribution and use in source and binary forms, with or without
81.1Sxtraeme * modification, are permitted provided that the following conditions
91.1Sxtraeme * are met:
101.1Sxtraeme * 1. Redistributions of source code must retain the above copyright
111.1Sxtraeme *    notice, this list of conditions and the following disclaimer.
121.1Sxtraeme * 2. Redistributions in binary form must reproduce the above copyright
131.1Sxtraeme *    notice, this list of conditions and the following disclaimer in the
141.1Sxtraeme *    documentation and/or other materials provided with the distribution.
151.1Sxtraeme *
161.6Sxtraeme * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
171.6Sxtraeme * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
181.6Sxtraeme * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
191.6Sxtraeme * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
201.6Sxtraeme * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
211.6Sxtraeme * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
221.6Sxtraeme * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
231.6Sxtraeme * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
241.6Sxtraeme * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
251.6Sxtraeme * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
261.1Sxtraeme */
271.1Sxtraeme
281.1Sxtraeme/*
291.3Sxtraeme * Driver for the IDE Controller of the National Semiconductor/AMD
301.3Sxtraeme * CS5535 Companion device. Available on systems with an AMD Geode GX2
311.3Sxtraeme * CPU, for example the decTOP.
321.1Sxtraeme *
331.1Sxtraeme * Datasheet at:
341.1Sxtraeme *
351.1Sxtraeme * http://www.amd.com/files/connectivitysolutions/geode/geode_gx/31506_cs5535_databook.pdf
361.1Sxtraeme */
371.1Sxtraeme
381.1Sxtraeme#include <sys/cdefs.h>
391.14Sbouyer__KERNEL_RCSID(0, "$NetBSD: gcscide.c,v 1.14 2012/07/31 15:50:32 bouyer Exp $");
401.1Sxtraeme
411.1Sxtraeme#include <sys/param.h>
421.1Sxtraeme#include <sys/systm.h>
431.1Sxtraeme
441.1Sxtraeme#include <dev/pci/pcivar.h>
451.1Sxtraeme#include <dev/pci/pcidevs.h>
461.1Sxtraeme#include <dev/pci/pciidereg.h>
471.1Sxtraeme#include <dev/pci/pciidevar.h>
481.1Sxtraeme
491.1Sxtraeme#include <machine/cpufunc.h>
501.1Sxtraeme
511.5Sxtraeme/*
521.5Sxtraeme * 6.4 - ATA-5 Controller Register Definitions.
531.5Sxtraeme */
541.1Sxtraeme#define GCSCIDE_MSR_ATAC_BASE 		0x51300000
551.1Sxtraeme#define GCSCIDE_ATAC_GLD_MSR_CAP 	(GCSCIDE_MSR_ATAC_BASE + 0)
561.1Sxtraeme#define GCSCIDE_ATAC_GLD_MSR_CONFIG 	(GCSCIDE_MSR_ATAC_BASE + 0x01)
571.1Sxtraeme#define GCSCIDE_ATAC_GLD_MSR_SMI 	(GCSCIDE_MSR_ATAC_BASE + 0x02)
581.1Sxtraeme#define GCSCIDE_ATAC_GLD_MSR_ERROR 	(GCSCIDE_MSR_ATAC_BASE + 0x03)
591.1Sxtraeme#define GCSCIDE_ATAC_GLD_MSR_PM 	(GCSCIDE_MSR_ATAC_BASE + 0x04)
601.1Sxtraeme#define GCSCIDE_ATAC_GLD_MSR_DIAG 	(GCSCIDE_MSR_ATAC_BASE + 0x05)
611.1Sxtraeme#define GCSCIDE_ATAC_IO_BAR 		(GCSCIDE_MSR_ATAC_BASE + 0x08)
621.1Sxtraeme#define GCSCIDE_ATAC_RESET 		(GCSCIDE_MSR_ATAC_BASE + 0x10)
631.1Sxtraeme#define GCSCIDE_ATAC_CH0D0_PIO 		(GCSCIDE_MSR_ATAC_BASE + 0x20)
641.1Sxtraeme#define GCSCIDE_ATAC_CH0D0_DMA 		(GCSCIDE_MSR_ATAC_BASE + 0x21)
651.1Sxtraeme#define GCSCIDE_ATAC_CH0D1_PIO 		(GCSCIDE_MSR_ATAC_BASE + 0x22)
661.1Sxtraeme#define GCSCIDE_ATAC_CH0D1_DMA 		(GCSCIDE_MSR_ATAC_BASE + 0x23)
671.1Sxtraeme#define GCSCIDE_ATAC_PCI_ABRTERR 	(GCSCIDE_MSR_ATAC_BASE + 0x24)
681.1Sxtraeme#define GCSCIDE_ATAC_BM0_CMD_PRIM 	0x00
691.1Sxtraeme#define GCSCIDE_ATAC_BM0_STS_PRIM 	0x02
701.1Sxtraeme#define GCSCIDE_ATAC_BM0_PRD 		0x04
711.5Sxtraeme/*
721.5Sxtraeme * ATAC_CH0D0_DMA registers:
731.5Sxtraeme *
741.5Sxtraeme * PIO Format (bit 31): Format 1 allows independent control of command
751.5Sxtraeme * and data per drive, while Format 0 selects the slowest speed
761.5Sxtraeme * of the two drives.
771.5Sxtraeme */
781.5Sxtraeme#define GCSCIDE_ATAC_PIO_FORMAT		(1 << 31) /* PIO Mode Format 1 */
791.5Sxtraeme/*
801.5Sxtraeme * DMA_SEL (bit 20): sets Ultra DMA mode (if enabled) or Multi-word
811.5Sxtraeme * DMA mode (if disabled).
821.5Sxtraeme */
831.5Sxtraeme#define GCSCIDE_ATAC_DMA_SEL		(1 << 20)
841.1Sxtraeme
851.7Scubestatic int	gcscide_match(device_t, cfdata_t, void *);
861.7Scubestatic void	gcscide_attach(device_t, device_t, void *);
871.1Sxtraeme
881.9Sdyoungstatic void	gcscide_chip_map(struct pciide_softc *, const struct pci_attach_args *);
891.1Sxtraemestatic void	gcscide_setup_channel(struct ata_channel *);
901.1Sxtraeme
911.5Sxtraeme/* PIO Format 1 settings */
921.1Sxtraemestatic const uint32_t gcscide_pio_timings[] = {
931.1Sxtraeme	0xf7f4f7f4,	/* PIO Mode 0 */
941.1Sxtraeme	0x53f3f173,	/* PIO Mode 1 */
951.1Sxtraeme	0x13f18141,	/* PIO Mode 2 */
961.1Sxtraeme	0x51315131,	/* PIO Mode 3 */
971.1Sxtraeme	0x11311131	/* PIO Mode 4 */
981.1Sxtraeme};
991.1Sxtraeme
1001.1Sxtraemestatic const uint32_t gcscide_mdma_timings[] = {
1011.1Sxtraeme	0x7f0ffff3,	/* MDMA Mode 0 */
1021.1Sxtraeme	0x7f035352,	/* MDMA Mode 1 */
1031.1Sxtraeme	0x7f024241 	/* MDMA Mode 2 */
1041.1Sxtraeme};
1051.1Sxtraeme
1061.1Sxtraemestatic const uint32_t gcscide_udma_timings[] = {
1071.1Sxtraeme	0x7f7436a1,	/* Ultra DMA Mode 0 */
1081.1Sxtraeme	0x7f733481,	/* Ultra DMA Mode 1 */
1091.1Sxtraeme	0x7f723261,	/* Ultra DMA Mode 2 */
1101.1Sxtraeme	0x7f713161,	/* Ultra DMA Mode 3 */
1111.1Sxtraeme	0x7f703061	/* Ultra DMA Mode 4 */
1121.1Sxtraeme};
1131.1Sxtraeme
1141.7ScubeCFATTACH_DECL_NEW(gcscide, sizeof(struct pciide_softc),
1151.1Sxtraeme    gcscide_match, gcscide_attach, NULL, NULL);
1161.1Sxtraeme
1171.1Sxtraemestatic const struct pciide_product_desc pciide_gcscide_products[] = {
1181.1Sxtraeme	{
1191.1Sxtraeme		PCI_PRODUCT_NS_CS5535_IDE,
1201.1Sxtraeme		0,
1211.1Sxtraeme		"National Semiconductor/AMD CS5535 IDE Controller",
1221.1Sxtraeme		gcscide_chip_map
1231.1Sxtraeme	},
1241.1Sxtraeme	{ 0, 0, NULL, NULL }
1251.1Sxtraeme};
1261.1Sxtraeme
1271.1Sxtraemestatic int
1281.7Scubegcscide_match(device_t parent, cfdata_t cfdata, void *aux)
1291.1Sxtraeme{
1301.1Sxtraeme	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
1311.1Sxtraeme
1321.1Sxtraeme	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_NS &&
1331.1Sxtraeme	    PCI_CLASS(pa->pa_class) == PCI_CLASS_MASS_STORAGE &&
1341.1Sxtraeme	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_IDE &&
1351.1Sxtraeme	    pciide_lookup_product(pa->pa_id, pciide_gcscide_products))
1361.1Sxtraeme		return 2;
1371.1Sxtraeme	return 0;
1381.1Sxtraeme}
1391.1Sxtraeme
1401.1Sxtraemestatic void
1411.7Scubegcscide_attach(device_t parent, device_t self, void *aux)
1421.1Sxtraeme{
1431.1Sxtraeme	struct pci_attach_args *pa = aux;
1441.7Scube	struct pciide_softc *sc = device_private(self);
1451.7Scube
1461.7Scube	sc->sc_wdcdev.sc_atac.atac_dev = self;
1471.1Sxtraeme
1481.1Sxtraeme	pciide_common_attach(sc, pa,
1491.1Sxtraeme	    pciide_lookup_product(pa->pa_id, pciide_gcscide_products));
1501.1Sxtraeme}
1511.1Sxtraeme
1521.1Sxtraemestatic void
1531.9Sdyounggcscide_chip_map(struct pciide_softc *sc, const struct pci_attach_args *pa)
1541.1Sxtraeme{
1551.1Sxtraeme	pcireg_t interface;
1561.1Sxtraeme
1571.1Sxtraeme	if (pciide_chipen(sc, pa) == 0)
1581.1Sxtraeme		return;
1591.1Sxtraeme
1601.7Scube	aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
1611.7Scube	    "bus-master DMA support present");
1621.1Sxtraeme	pciide_mapreg_dma(sc, pa);
1631.1Sxtraeme	aprint_verbose("\n");
1641.1Sxtraeme
1651.1Sxtraeme	sc->sc_wdcdev.sc_atac.atac_cap = ATAC_CAP_DATA16 | ATAC_CAP_DATA32;
1661.1Sxtraeme	if (sc->sc_dma_ok) {
1671.1Sxtraeme		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DMA | ATAC_CAP_UDMA;
1681.1Sxtraeme		sc->sc_wdcdev.irqack = pciide_irqack;
1691.1Sxtraeme	}
1701.1Sxtraeme
1711.1Sxtraeme	sc->sc_wdcdev.sc_atac.atac_pio_cap = 4;
1721.1Sxtraeme	sc->sc_wdcdev.sc_atac.atac_dma_cap = 2;
1731.1Sxtraeme	sc->sc_wdcdev.sc_atac.atac_udma_cap = 4;
1741.1Sxtraeme	sc->sc_wdcdev.sc_atac.atac_set_modes = gcscide_setup_channel;
1751.1Sxtraeme	sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanarray;
1761.1Sxtraeme	sc->sc_wdcdev.sc_atac.atac_nchannels = 1;
1771.1Sxtraeme
1781.1Sxtraeme	interface = PCI_INTERFACE(pa->pa_class);
1791.1Sxtraeme
1801.1Sxtraeme	wdc_allocate_regs(&sc->sc_wdcdev);
1811.1Sxtraeme
1821.1Sxtraeme	if (pciide_chansetup(sc, 0, interface) == 0)
1831.1Sxtraeme		return;
1841.1Sxtraeme
1851.1Sxtraeme	pciide_mapchan(pa, &sc->pciide_channels[0], interface,
1861.8Sjakllsch	    pciide_pci_intr);
1871.1Sxtraeme}
1881.1Sxtraeme
1891.1Sxtraemestatic void
1901.1Sxtraemegcscide_setup_channel(struct ata_channel *chp)
1911.1Sxtraeme{
1921.1Sxtraeme	struct pciide_channel *cp = CHAN_TO_PCHAN(chp);
1931.1Sxtraeme	struct ata_drive_datas *drvp;
1941.1Sxtraeme	uint64_t reg = 0;
1951.1Sxtraeme	int drive, s;
1961.1Sxtraeme
1971.1Sxtraeme	pciide_channel_dma_setup(cp);
1981.1Sxtraeme
1991.1Sxtraeme	for (drive = 0; drive < 2; drive++) {
2001.1Sxtraeme		drvp = &chp->ch_drive[drive];
2011.14Sbouyer		if (drvp->drive_type == ATA_DRIVET_NONE)
2021.1Sxtraeme			continue;
2031.5Sxtraeme
2041.5Sxtraeme		reg = rdmsr(drive ? GCSCIDE_ATAC_CH0D1_DMA :
2051.5Sxtraeme		    GCSCIDE_ATAC_CH0D0_DMA);
2061.1Sxtraeme
2071.14Sbouyer		if (drvp->drive_flags & ATA_DRIVE_UDMA) {
2081.1Sxtraeme			s = splbio();
2091.14Sbouyer			drvp->drive_flags &= ~ATA_DRIVE_DMA;
2101.1Sxtraeme			splx(s);
2111.5Sxtraeme			/* Enable the Ultra DMA mode bit */
2121.5Sxtraeme			reg |= GCSCIDE_ATAC_DMA_SEL;
2131.5Sxtraeme			/* set the Ultra DMA mode */
2141.1Sxtraeme			reg |= gcscide_udma_timings[drvp->UDMA_mode];
2151.1Sxtraeme
2161.1Sxtraeme			wrmsr(drive ? GCSCIDE_ATAC_CH0D1_DMA :
2171.1Sxtraeme			    GCSCIDE_ATAC_CH0D0_DMA, reg);
2181.1Sxtraeme
2191.14Sbouyer		} else if (drvp->drive_flags & ATA_DRIVE_DMA) {
2201.5Sxtraeme			/* Enable the Multi-word DMA bit */
2211.5Sxtraeme			reg &= ~GCSCIDE_ATAC_DMA_SEL;
2221.5Sxtraeme			/* set the Multi-word DMA mode */
2231.1Sxtraeme			reg |= gcscide_mdma_timings[drvp->DMA_mode];
2241.1Sxtraeme
2251.1Sxtraeme			wrmsr(drive ? GCSCIDE_ATAC_CH0D1_DMA :
2261.1Sxtraeme			    GCSCIDE_ATAC_CH0D0_DMA, reg);
2271.1Sxtraeme		}
2281.5Sxtraeme
2291.5Sxtraeme		/* Always use PIO Format 1. */
2301.5Sxtraeme		wrmsr(drive ? GCSCIDE_ATAC_CH0D1_DMA :
2311.5Sxtraeme		    GCSCIDE_ATAC_CH0D0_DMA, reg | GCSCIDE_ATAC_PIO_FORMAT);
2321.5Sxtraeme
2331.5Sxtraeme		/* Set PIO mode */
2341.4Sxtraeme		wrmsr(drive ? GCSCIDE_ATAC_CH0D1_PIO : GCSCIDE_ATAC_CH0D0_PIO,
2351.1Sxtraeme		    gcscide_pio_timings[drvp->PIO_mode]);
2361.1Sxtraeme	}
2371.1Sxtraeme}
238