gcscide.c revision 1.1
11.1Sxtraeme/*	$NetBSD: gcscide.c,v 1.1 2007/06/27 23:02:53 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.1Sxtraeme * Driver for the National Semiconductor/AMD CS5535 Companion Controller.
411.1Sxtraeme * Usually this comes with an AMD Geode GX CPU.
421.1Sxtraeme *
431.1Sxtraeme * Datasheet at:
441.1Sxtraeme *
451.1Sxtraeme * http://www.amd.com/files/connectivitysolutions/geode/geode_gx/31506_cs5535_databook.pdf
461.1Sxtraeme */
471.1Sxtraeme
481.1Sxtraeme#include <sys/cdefs.h>
491.1Sxtraeme__KERNEL_RCSID(0, "$NetBSD: gcscide.c,v 1.1 2007/06/27 23:02:53 xtraeme Exp $");
501.1Sxtraeme
511.1Sxtraeme#include <sys/param.h>
521.1Sxtraeme#include <sys/systm.h>
531.1Sxtraeme
541.1Sxtraeme#include <dev/pci/pcivar.h>
551.1Sxtraeme#include <dev/pci/pcidevs.h>
561.1Sxtraeme#include <dev/pci/pciidereg.h>
571.1Sxtraeme#include <dev/pci/pciidevar.h>
581.1Sxtraeme
591.1Sxtraeme#include <machine/cpufunc.h>
601.1Sxtraeme
611.1Sxtraeme#define GCSCIDE_MSR_ATAC_BASE 		0x51300000
621.1Sxtraeme#define GCSCIDE_ATAC_GLD_MSR_CAP 	(GCSCIDE_MSR_ATAC_BASE + 0)
631.1Sxtraeme#define GCSCIDE_ATAC_GLD_MSR_CONFIG 	(GCSCIDE_MSR_ATAC_BASE + 0x01)
641.1Sxtraeme#define GCSCIDE_ATAC_GLD_MSR_SMI 	(GCSCIDE_MSR_ATAC_BASE + 0x02)
651.1Sxtraeme#define GCSCIDE_ATAC_GLD_MSR_ERROR 	(GCSCIDE_MSR_ATAC_BASE + 0x03)
661.1Sxtraeme#define GCSCIDE_ATAC_GLD_MSR_PM 	(GCSCIDE_MSR_ATAC_BASE + 0x04)
671.1Sxtraeme#define GCSCIDE_ATAC_GLD_MSR_DIAG 	(GCSCIDE_MSR_ATAC_BASE + 0x05)
681.1Sxtraeme#define GCSCIDE_ATAC_IO_BAR 		(GCSCIDE_MSR_ATAC_BASE + 0x08)
691.1Sxtraeme#define GCSCIDE_ATAC_RESET 		(GCSCIDE_MSR_ATAC_BASE + 0x10)
701.1Sxtraeme#define GCSCIDE_ATAC_CH0D0_PIO 		(GCSCIDE_MSR_ATAC_BASE + 0x20)
711.1Sxtraeme#define GCSCIDE_ATAC_CH0D0_DMA 		(GCSCIDE_MSR_ATAC_BASE + 0x21)
721.1Sxtraeme#define GCSCIDE_ATAC_CH0D1_PIO 		(GCSCIDE_MSR_ATAC_BASE + 0x22)
731.1Sxtraeme#define GCSCIDE_ATAC_CH0D1_DMA 		(GCSCIDE_MSR_ATAC_BASE + 0x23)
741.1Sxtraeme#define GCSCIDE_ATAC_PCI_ABRTERR 	(GCSCIDE_MSR_ATAC_BASE + 0x24)
751.1Sxtraeme#define GCSCIDE_ATAC_BM0_CMD_PRIM 	0x00
761.1Sxtraeme#define GCSCIDE_ATAC_BM0_STS_PRIM 	0x02
771.1Sxtraeme#define GCSCIDE_ATAC_BM0_PRD 		0x04
781.1Sxtraeme#define GCSCIDE_PIO_FORMAT		0x80000000UL
791.1Sxtraeme
801.1Sxtraemestatic int	gcscide_match(struct device *, struct cfdata *, void *);
811.1Sxtraemestatic void	gcscide_attach(struct device *, struct device *, void *);
821.1Sxtraeme
831.1Sxtraemestatic void	gcscide_chip_map(struct pciide_softc *, struct pci_attach_args *);
841.1Sxtraemestatic void	gcscide_setup_channel(struct ata_channel *);
851.1Sxtraeme
861.1Sxtraeme/* PIO Format 1 timings */
871.1Sxtraemestatic const uint32_t gcscide_pio_timings[] = {
881.1Sxtraeme	0xf7f4f7f4,	/* PIO Mode 0 */
891.1Sxtraeme	0x53f3f173,	/* PIO Mode 1 */
901.1Sxtraeme	0x13f18141,	/* PIO Mode 2 */
911.1Sxtraeme	0x51315131,	/* PIO Mode 3 */
921.1Sxtraeme	0x11311131	/* PIO Mode 4 */
931.1Sxtraeme};
941.1Sxtraeme
951.1Sxtraemestatic const uint32_t gcscide_mdma_timings[] = {
961.1Sxtraeme	0x7f0ffff3,	/* MDMA Mode 0 */
971.1Sxtraeme	0x7f035352,	/* MDMA Mode 1 */
981.1Sxtraeme	0x7f024241 	/* MDMA Mode 2 */
991.1Sxtraeme};
1001.1Sxtraeme
1011.1Sxtraemestatic const uint32_t gcscide_udma_timings[] = {
1021.1Sxtraeme	0x7f7436a1,	/* Ultra DMA Mode 0 */
1031.1Sxtraeme	0x7f733481,	/* Ultra DMA Mode 1 */
1041.1Sxtraeme	0x7f723261,	/* Ultra DMA Mode 2 */
1051.1Sxtraeme	0x7f713161,	/* Ultra DMA Mode 3 */
1061.1Sxtraeme	0x7f703061	/* Ultra DMA Mode 4 */
1071.1Sxtraeme};
1081.1Sxtraeme
1091.1SxtraemeCFATTACH_DECL(gcscide, sizeof(struct pciide_softc),
1101.1Sxtraeme    gcscide_match, gcscide_attach, NULL, NULL);
1111.1Sxtraeme
1121.1Sxtraemestatic const struct pciide_product_desc pciide_gcscide_products[] = {
1131.1Sxtraeme	{
1141.1Sxtraeme		PCI_PRODUCT_NS_CS5535_IDE,
1151.1Sxtraeme		0,
1161.1Sxtraeme		"National Semiconductor/AMD CS5535 IDE Controller",
1171.1Sxtraeme		gcscide_chip_map
1181.1Sxtraeme	},
1191.1Sxtraeme	{ 0, 0, NULL, NULL }
1201.1Sxtraeme};
1211.1Sxtraeme
1221.1Sxtraemestatic int
1231.1Sxtraemegcscide_match(struct device *parent, struct cfdata *cfdata, void *aux)
1241.1Sxtraeme{
1251.1Sxtraeme	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
1261.1Sxtraeme
1271.1Sxtraeme	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_NS &&
1281.1Sxtraeme	    PCI_CLASS(pa->pa_class) == PCI_CLASS_MASS_STORAGE &&
1291.1Sxtraeme	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_MASS_STORAGE_IDE &&
1301.1Sxtraeme	    pciide_lookup_product(pa->pa_id, pciide_gcscide_products))
1311.1Sxtraeme		return 2;
1321.1Sxtraeme	return 0;
1331.1Sxtraeme}
1341.1Sxtraeme
1351.1Sxtraemestatic void
1361.1Sxtraemegcscide_attach(struct device *parent, struct device *self, void *aux)
1371.1Sxtraeme{
1381.1Sxtraeme	struct pci_attach_args *pa = aux;
1391.1Sxtraeme	struct pciide_softc *sc = (struct pciide_softc *)self;
1401.1Sxtraeme
1411.1Sxtraeme	pciide_common_attach(sc, pa,
1421.1Sxtraeme	    pciide_lookup_product(pa->pa_id, pciide_gcscide_products));
1431.1Sxtraeme}
1441.1Sxtraeme
1451.1Sxtraemestatic void
1461.1Sxtraemegcscide_chip_map(struct pciide_softc *sc, struct pci_attach_args *pa)
1471.1Sxtraeme{
1481.1Sxtraeme	pcireg_t interface;
1491.1Sxtraeme	bus_size_t cmdsize, ctlsize;
1501.1Sxtraeme
1511.1Sxtraeme	if (pciide_chipen(sc, pa) == 0)
1521.1Sxtraeme		return;
1531.1Sxtraeme
1541.1Sxtraeme	aprint_verbose("%s: bus-master DMA support present",
1551.1Sxtraeme	    sc->sc_wdcdev.sc_atac.atac_dev.dv_xname);
1561.1Sxtraeme	pciide_mapreg_dma(sc, pa);
1571.1Sxtraeme	aprint_verbose("\n");
1581.1Sxtraeme
1591.1Sxtraeme	sc->sc_wdcdev.sc_atac.atac_cap = ATAC_CAP_DATA16 | ATAC_CAP_DATA32;
1601.1Sxtraeme	if (sc->sc_dma_ok) {
1611.1Sxtraeme		sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DMA | ATAC_CAP_UDMA;
1621.1Sxtraeme		sc->sc_wdcdev.irqack = pciide_irqack;
1631.1Sxtraeme	}
1641.1Sxtraeme
1651.1Sxtraeme	sc->sc_wdcdev.sc_atac.atac_pio_cap = 4;
1661.1Sxtraeme	sc->sc_wdcdev.sc_atac.atac_dma_cap = 2;
1671.1Sxtraeme	sc->sc_wdcdev.sc_atac.atac_udma_cap = 4;
1681.1Sxtraeme	sc->sc_wdcdev.sc_atac.atac_set_modes = gcscide_setup_channel;
1691.1Sxtraeme	sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanarray;
1701.1Sxtraeme	sc->sc_wdcdev.sc_atac.atac_nchannels = 1;
1711.1Sxtraeme
1721.1Sxtraeme	interface = PCI_INTERFACE(pa->pa_class);
1731.1Sxtraeme
1741.1Sxtraeme	wdc_allocate_regs(&sc->sc_wdcdev);
1751.1Sxtraeme
1761.1Sxtraeme	if (pciide_chansetup(sc, 0, interface) == 0)
1771.1Sxtraeme		return;
1781.1Sxtraeme
1791.1Sxtraeme	pciide_mapchan(pa, &sc->pciide_channels[0], interface,
1801.1Sxtraeme	    &cmdsize, &ctlsize, pciide_pci_intr);
1811.1Sxtraeme}
1821.1Sxtraeme
1831.1Sxtraemestatic void
1841.1Sxtraemegcscide_setup_channel(struct ata_channel *chp)
1851.1Sxtraeme{
1861.1Sxtraeme	struct pciide_channel *cp = CHAN_TO_PCHAN(chp);
1871.1Sxtraeme	struct ata_drive_datas *drvp;
1881.1Sxtraeme	uint64_t reg = 0;
1891.1Sxtraeme	int drive, s;
1901.1Sxtraeme
1911.1Sxtraeme	pciide_channel_dma_setup(cp);
1921.1Sxtraeme
1931.1Sxtraeme	for (drive = 0; drive < 2; drive++) {
1941.1Sxtraeme		drvp = &chp->ch_drive[drive];
1951.1Sxtraeme		if ((drvp->drive_flags & DRIVE) == 0)
1961.1Sxtraeme			continue;
1971.1Sxtraeme		if ((drvp->drive_flags & DRIVE_UDMA) ||
1981.1Sxtraeme		    (drvp->drive_flags & DRIVE_DMA)) {
1991.1Sxtraeme			reg = rdmsr(drive ? GCSCIDE_ATAC_CH0D1_DMA :
2001.1Sxtraeme			    GCSCIDE_ATAC_CH0D0_DMA);
2011.1Sxtraeme
2021.1Sxtraeme			/* high 32 bits */
2031.1Sxtraeme			reg = (reg << 32);
2041.1Sxtraeme			/* Preserve PIO Format bit */
2051.1Sxtraeme			reg &= GCSCIDE_PIO_FORMAT;
2061.1Sxtraeme		}
2071.1Sxtraeme
2081.1Sxtraeme		if (drvp->drive_flags & DRIVE_UDMA) {
2091.1Sxtraeme			s = splbio();
2101.1Sxtraeme			drvp->drive_flags &= ~DRIVE_DMA;
2111.1Sxtraeme			splx(s);
2121.1Sxtraeme			/* Set UDMA and MDMA timings */
2131.1Sxtraeme			reg |= gcscide_udma_timings[drvp->UDMA_mode];
2141.1Sxtraeme			reg |= gcscide_mdma_timings[drvp->DMA_mode];
2151.1Sxtraeme
2161.1Sxtraeme			wrmsr(drive ? GCSCIDE_ATAC_CH0D1_DMA :
2171.1Sxtraeme			    GCSCIDE_ATAC_CH0D0_DMA, reg);
2181.1Sxtraeme
2191.1Sxtraeme		} else if (drvp->drive_flags & DRIVE_DMA) {
2201.1Sxtraeme			/*
2211.1Sxtraeme			 * Disable Ultra DMA and set a MDMA mode.
2221.1Sxtraeme			 */
2231.1Sxtraeme			if (reg & gcscide_udma_timings[drvp->UDMA_mode])
2241.1Sxtraeme				reg &= ~gcscide_udma_timings[drvp->UDMA_mode];
2251.1Sxtraeme
2261.1Sxtraeme			reg |= gcscide_mdma_timings[drvp->DMA_mode];
2271.1Sxtraeme
2281.1Sxtraeme			wrmsr(drive ? GCSCIDE_ATAC_CH0D1_DMA :
2291.1Sxtraeme			    GCSCIDE_ATAC_CH0D0_DMA, reg);
2301.1Sxtraeme		} else {
2311.1Sxtraeme			/*
2321.1Sxtraeme			 * Set PIO Format 1 timings.
2331.1Sxtraeme			 */
2341.1Sxtraeme			reg = rdmsr(drive ? GCSCIDE_ATAC_CH0D1_DMA :
2351.1Sxtraeme		    	    GCSCIDE_ATAC_CH0D0_DMA);
2361.1Sxtraeme			reg = (reg << 32);
2371.1Sxtraeme			wrmsr(drive ? GCSCIDE_ATAC_CH0D1_DMA :
2381.1Sxtraeme		    	    GCSCIDE_ATAC_CH0D0_DMA, reg | GCSCIDE_PIO_FORMAT);
2391.1Sxtraeme		}
2401.1Sxtraeme		/* Set PIO mode and timing */
2411.1Sxtraeme		wrmsr(drive ? GCSCIDE_ATAC_CH0D1_PIO : GCSCIDE_ATAC_CH0D1_PIO,
2421.1Sxtraeme		    gcscide_pio_timings[drvp->PIO_mode]);
2431.1Sxtraeme	}
2441.1Sxtraeme}
245