gcscide.c revision 1.4
11.4Sxtraeme/*	$NetBSD: gcscide.c,v 1.4 2007/06/28 10:22:52 xtraeme Exp $	*/
21.1Sxtraeme
31.1Sxtraeme/*
41.1Sxtraeme * Copyright (c) 2007 The NetBSD Foundation.
51.1Sxtraeme * All rights reserved.
61.1Sxtraeme *
71.1Sxtraeme * This code is derived from software contributed to the NetBSD Foundation
81.1Sxtraeme * by Juan Romero Pardines.
91.1Sxtraeme *
101.1Sxtraeme * Redistribution and use in source and binary forms, with or without
111.1Sxtraeme * modification, are permitted provided that the following conditions
121.1Sxtraeme * are met:
131.1Sxtraeme * 1. Redistributions of source code must retain the above copyright
141.1Sxtraeme *    notice, this list of conditions and the following disclaimer.
151.1Sxtraeme * 2. Redistributions in binary form must reproduce the above copyright
161.1Sxtraeme *    notice, this list of conditions and the following disclaimer in the
171.1Sxtraeme *    documentation and/or other materials provided with the distribution.
181.1Sxtraeme * 3. All advertising materials mentioning features or use of this software
191.1Sxtraeme *    must display the following acknowledgement:
201.1Sxtraeme *        This product includes software developed by Juan Romero Pardines
211.1Sxtraeme *        for the NetBSD Foundation, Inc. and its contributors.
221.1Sxtraeme * 4. Neither the name of The NetBSD Foundation nor the names of its
231.1Sxtraeme *    contributors may be used to endorse or promote products derived
241.1Sxtraeme *    from this software without specific prior written permission.
251.1Sxtraeme *
261.1Sxtraeme * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
271.1Sxtraeme * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
281.1Sxtraeme * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
291.1Sxtraeme * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
301.1Sxtraeme * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
311.1Sxtraeme * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
321.1Sxtraeme * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
331.1Sxtraeme * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
341.1Sxtraeme * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
351.1Sxtraeme * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
361.1Sxtraeme * POSSIBILITY OF SUCH DAMAGE.
371.1Sxtraeme */
381.1Sxtraeme
391.1Sxtraeme/*
401.3Sxtraeme * Driver for the IDE Controller of the National Semiconductor/AMD
411.3Sxtraeme * CS5535 Companion device. Available on systems with an AMD Geode GX2
421.3Sxtraeme * CPU, for example the decTOP.
431.1Sxtraeme *
441.1Sxtraeme * Datasheet at:
451.1Sxtraeme *
461.1Sxtraeme * http://www.amd.com/files/connectivitysolutions/geode/geode_gx/31506_cs5535_databook.pdf
471.1Sxtraeme */
481.1Sxtraeme
491.1Sxtraeme#include <sys/cdefs.h>
501.4Sxtraeme__KERNEL_RCSID(0, "$NetBSD: gcscide.c,v 1.4 2007/06/28 10:22:52 xtraeme Exp $");
511.1Sxtraeme
521.1Sxtraeme#include <sys/param.h>
531.1Sxtraeme#include <sys/systm.h>
541.1Sxtraeme
551.1Sxtraeme#include <dev/pci/pcivar.h>
561.1Sxtraeme#include <dev/pci/pcidevs.h>
571.1Sxtraeme#include <dev/pci/pciidereg.h>
581.1Sxtraeme#include <dev/pci/pciidevar.h>
591.1Sxtraeme
601.1Sxtraeme#include <machine/cpufunc.h>
611.1Sxtraeme
621.1Sxtraeme#define GCSCIDE_MSR_ATAC_BASE 		0x51300000
631.1Sxtraeme#define GCSCIDE_ATAC_GLD_MSR_CAP 	(GCSCIDE_MSR_ATAC_BASE + 0)
641.1Sxtraeme#define GCSCIDE_ATAC_GLD_MSR_CONFIG 	(GCSCIDE_MSR_ATAC_BASE + 0x01)
651.1Sxtraeme#define GCSCIDE_ATAC_GLD_MSR_SMI 	(GCSCIDE_MSR_ATAC_BASE + 0x02)
661.1Sxtraeme#define GCSCIDE_ATAC_GLD_MSR_ERROR 	(GCSCIDE_MSR_ATAC_BASE + 0x03)
671.1Sxtraeme#define GCSCIDE_ATAC_GLD_MSR_PM 	(GCSCIDE_MSR_ATAC_BASE + 0x04)
681.1Sxtraeme#define GCSCIDE_ATAC_GLD_MSR_DIAG 	(GCSCIDE_MSR_ATAC_BASE + 0x05)
691.1Sxtraeme#define GCSCIDE_ATAC_IO_BAR 		(GCSCIDE_MSR_ATAC_BASE + 0x08)
701.1Sxtraeme#define GCSCIDE_ATAC_RESET 		(GCSCIDE_MSR_ATAC_BASE + 0x10)
711.1Sxtraeme#define GCSCIDE_ATAC_CH0D0_PIO 		(GCSCIDE_MSR_ATAC_BASE + 0x20)
721.1Sxtraeme#define GCSCIDE_ATAC_CH0D0_DMA 		(GCSCIDE_MSR_ATAC_BASE + 0x21)
731.1Sxtraeme#define GCSCIDE_ATAC_CH0D1_PIO 		(GCSCIDE_MSR_ATAC_BASE + 0x22)
741.1Sxtraeme#define GCSCIDE_ATAC_CH0D1_DMA 		(GCSCIDE_MSR_ATAC_BASE + 0x23)
751.1Sxtraeme#define GCSCIDE_ATAC_PCI_ABRTERR 	(GCSCIDE_MSR_ATAC_BASE + 0x24)
761.1Sxtraeme#define GCSCIDE_ATAC_BM0_CMD_PRIM 	0x00
771.1Sxtraeme#define GCSCIDE_ATAC_BM0_STS_PRIM 	0x02
781.1Sxtraeme#define GCSCIDE_ATAC_BM0_PRD 		0x04
791.1Sxtraeme#define GCSCIDE_PIO_FORMAT		0x80000000UL
801.1Sxtraeme
811.1Sxtraemestatic int	gcscide_match(struct device *, struct cfdata *, void *);
821.1Sxtraemestatic void	gcscide_attach(struct device *, struct device *, void *);
831.1Sxtraeme
841.1Sxtraemestatic void	gcscide_chip_map(struct pciide_softc *, struct pci_attach_args *);
851.1Sxtraemestatic void	gcscide_setup_channel(struct ata_channel *);
861.1Sxtraeme
871.1Sxtraeme/* PIO Format 1 timings */
881.1Sxtraemestatic const uint32_t gcscide_pio_timings[] = {
891.1Sxtraeme	0xf7f4f7f4,	/* PIO Mode 0 */
901.1Sxtraeme	0x53f3f173,	/* PIO Mode 1 */
911.1Sxtraeme	0x13f18141,	/* PIO Mode 2 */
921.1Sxtraeme	0x51315131,	/* PIO Mode 3 */
931.1Sxtraeme	0x11311131	/* PIO Mode 4 */
941.1Sxtraeme};
951.1Sxtraeme
961.1Sxtraemestatic const uint32_t gcscide_mdma_timings[] = {
971.1Sxtraeme	0x7f0ffff3,	/* MDMA Mode 0 */
981.1Sxtraeme	0x7f035352,	/* MDMA Mode 1 */
991.1Sxtraeme	0x7f024241 	/* MDMA Mode 2 */
1001.1Sxtraeme};
1011.1Sxtraeme
1021.1Sxtraemestatic const uint32_t gcscide_udma_timings[] = {
1031.1Sxtraeme	0x7f7436a1,	/* Ultra DMA Mode 0 */
1041.1Sxtraeme	0x7f733481,	/* Ultra DMA Mode 1 */
1051.1Sxtraeme	0x7f723261,	/* Ultra DMA Mode 2 */
1061.1Sxtraeme	0x7f713161,	/* Ultra DMA Mode 3 */
1071.1Sxtraeme	0x7f703061	/* Ultra DMA Mode 4 */
1081.1Sxtraeme};
1091.1Sxtraeme
1101.1SxtraemeCFATTACH_DECL(gcscide, sizeof(struct pciide_softc),
1111.1Sxtraeme    gcscide_match, gcscide_attach, NULL, NULL);
1121.1Sxtraeme
1131.1Sxtraemestatic const struct pciide_product_desc pciide_gcscide_products[] = {
1141.1Sxtraeme	{
1151.1Sxtraeme		PCI_PRODUCT_NS_CS5535_IDE,
1161.1Sxtraeme		0,
1171.1Sxtraeme		"National Semiconductor/AMD CS5535 IDE Controller",
1181.1Sxtraeme		gcscide_chip_map
1191.1Sxtraeme	},
1201.1Sxtraeme	{ 0, 0, NULL, NULL }
1211.1Sxtraeme};
1221.1Sxtraeme
1231.1Sxtraemestatic int
1241.1Sxtraemegcscide_match(struct device *parent, struct cfdata *cfdata, void *aux)
1251.1Sxtraeme{
1261.1Sxtraeme	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
1271.1Sxtraeme
1281.1Sxtraeme	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_NS &&
1291.1Sxtraeme	    PCI_CLASS(pa->pa_class) == PCI_CLASS_MASS_STORAGE &&
1301.1Sxtraeme	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_IDE &&
1311.1Sxtraeme	    pciide_lookup_product(pa->pa_id, pciide_gcscide_products))
1321.1Sxtraeme		return 2;
1331.1Sxtraeme	return 0;
1341.1Sxtraeme}
1351.1Sxtraeme
1361.1Sxtraemestatic void
1371.1Sxtraemegcscide_attach(struct device *parent, struct device *self, void *aux)
1381.1Sxtraeme{
1391.1Sxtraeme	struct pci_attach_args *pa = aux;
1401.1Sxtraeme	struct pciide_softc *sc = (struct pciide_softc *)self;
1411.1Sxtraeme
1421.1Sxtraeme	pciide_common_attach(sc, pa,
1431.1Sxtraeme	    pciide_lookup_product(pa->pa_id, pciide_gcscide_products));
1441.1Sxtraeme}
1451.1Sxtraeme
1461.1Sxtraemestatic void
1471.1Sxtraemegcscide_chip_map(struct pciide_softc *sc, struct pci_attach_args *pa)
1481.1Sxtraeme{
1491.1Sxtraeme	pcireg_t interface;
1501.1Sxtraeme	bus_size_t cmdsize, ctlsize;
1511.1Sxtraeme
1521.1Sxtraeme	if (pciide_chipen(sc, pa) == 0)
1531.1Sxtraeme		return;
1541.1Sxtraeme
1551.1Sxtraeme	aprint_verbose("%s: bus-master DMA support present",
1561.1Sxtraeme	    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname);
1571.1Sxtraeme	pciide_mapreg_dma(sc, pa);
1581.1Sxtraeme	aprint_verbose("\n");
1591.1Sxtraeme
1601.1Sxtraeme	sc->sc_wdcdev.sc_atac.atac_cap = ATAC_CAP_DATA16 | ATAC_CAP_DATA32;
1611.1Sxtraeme	if (sc->sc_dma_ok) {
1621.1Sxtraeme		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DMA | ATAC_CAP_UDMA;
1631.1Sxtraeme		sc->sc_wdcdev.irqack = pciide_irqack;
1641.1Sxtraeme	}
1651.1Sxtraeme
1661.1Sxtraeme	sc->sc_wdcdev.sc_atac.atac_pio_cap = 4;
1671.1Sxtraeme	sc->sc_wdcdev.sc_atac.atac_dma_cap = 2;
1681.1Sxtraeme	sc->sc_wdcdev.sc_atac.atac_udma_cap = 4;
1691.1Sxtraeme	sc->sc_wdcdev.sc_atac.atac_set_modes = gcscide_setup_channel;
1701.1Sxtraeme	sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanarray;
1711.1Sxtraeme	sc->sc_wdcdev.sc_atac.atac_nchannels = 1;
1721.1Sxtraeme
1731.1Sxtraeme	interface = PCI_INTERFACE(pa->pa_class);
1741.1Sxtraeme
1751.1Sxtraeme	wdc_allocate_regs(&sc->sc_wdcdev);
1761.1Sxtraeme
1771.1Sxtraeme	if (pciide_chansetup(sc, 0, interface) == 0)
1781.1Sxtraeme		return;
1791.1Sxtraeme
1801.1Sxtraeme	pciide_mapchan(pa, &sc->pciide_channels[0], interface,
1811.1Sxtraeme	    &cmdsize, &ctlsize, pciide_pci_intr);
1821.1Sxtraeme}
1831.1Sxtraeme
1841.1Sxtraemestatic void
1851.1Sxtraemegcscide_setup_channel(struct ata_channel *chp)
1861.1Sxtraeme{
1871.1Sxtraeme	struct pciide_channel *cp = CHAN_TO_PCHAN(chp);
1881.1Sxtraeme	struct ata_drive_datas *drvp;
1891.1Sxtraeme	uint64_t reg = 0;
1901.1Sxtraeme	int drive, s;
1911.1Sxtraeme
1921.1Sxtraeme	pciide_channel_dma_setup(cp);
1931.1Sxtraeme
1941.1Sxtraeme	for (drive = 0; drive < 2; drive++) {
1951.1Sxtraeme		drvp = &chp->ch_drive[drive];
1961.1Sxtraeme		if ((drvp->drive_flags & DRIVE) == 0)
1971.1Sxtraeme			continue;
1981.1Sxtraeme		if ((drvp->drive_flags & DRIVE_UDMA) ||
1991.1Sxtraeme		    (drvp->drive_flags & DRIVE_DMA)) {
2001.1Sxtraeme			reg = rdmsr(drive ? GCSCIDE_ATAC_CH0D1_DMA :
2011.1Sxtraeme			    GCSCIDE_ATAC_CH0D0_DMA);
2021.1Sxtraeme			/* Preserve PIO Format bit */
2031.2Sxtraeme			reg &= ~GCSCIDE_PIO_FORMAT;
2041.1Sxtraeme		}
2051.1Sxtraeme
2061.1Sxtraeme		if (drvp->drive_flags & DRIVE_UDMA) {
2071.1Sxtraeme			s = splbio();
2081.1Sxtraeme			drvp->drive_flags &= ~DRIVE_DMA;
2091.1Sxtraeme			splx(s);
2101.1Sxtraeme			/* Set UDMA and MDMA timings */
2111.1Sxtraeme			reg |= gcscide_udma_timings[drvp->UDMA_mode];
2121.1Sxtraeme			reg |= gcscide_mdma_timings[drvp->DMA_mode];
2131.1Sxtraeme
2141.1Sxtraeme			wrmsr(drive ? GCSCIDE_ATAC_CH0D1_DMA :
2151.1Sxtraeme			    GCSCIDE_ATAC_CH0D0_DMA, reg);
2161.1Sxtraeme
2171.1Sxtraeme		} else if (drvp->drive_flags & DRIVE_DMA) {
2181.1Sxtraeme			/*
2191.1Sxtraeme			 * Disable Ultra DMA and set a MDMA mode.
2201.1Sxtraeme			 */
2211.1Sxtraeme			if (reg & gcscide_udma_timings[drvp->UDMA_mode])
2221.1Sxtraeme				reg &= ~gcscide_udma_timings[drvp->UDMA_mode];
2231.1Sxtraeme
2241.1Sxtraeme			reg |= gcscide_mdma_timings[drvp->DMA_mode];
2251.1Sxtraeme
2261.1Sxtraeme			wrmsr(drive ? GCSCIDE_ATAC_CH0D1_DMA :
2271.1Sxtraeme			    GCSCIDE_ATAC_CH0D0_DMA, reg);
2281.1Sxtraeme		} else {
2291.1Sxtraeme			/*
2301.1Sxtraeme			 * Set PIO Format 1 timings.
2311.1Sxtraeme			 */
2321.1Sxtraeme			reg = rdmsr(drive ? GCSCIDE_ATAC_CH0D1_DMA :
2331.1Sxtraeme		    	    GCSCIDE_ATAC_CH0D0_DMA);
2341.1Sxtraeme			wrmsr(drive ? GCSCIDE_ATAC_CH0D1_DMA :
2351.1Sxtraeme		    	    GCSCIDE_ATAC_CH0D0_DMA, reg | GCSCIDE_PIO_FORMAT);
2361.1Sxtraeme		}
2371.1Sxtraeme		/* Set PIO mode and timing */
2381.4Sxtraeme		wrmsr(drive ? GCSCIDE_ATAC_CH0D1_PIO : GCSCIDE_ATAC_CH0D0_PIO,
2391.1Sxtraeme		    gcscide_pio_timings[drvp->PIO_mode]);
2401.1Sxtraeme	}
2411.1Sxtraeme}
242