if_mtd_pci.c revision 1.11
11.11Sdyoung/* $NetBSD: if_mtd_pci.c,v 1.11 2008/03/12 18:26:58 dyoung Exp $ */
21.1Smartin
31.1Smartin/*-
41.1Smartin * Copyright (c) 2002 The NetBSD Foundation, Inc.
51.1Smartin * All rights reserved.
61.1Smartin *
71.1Smartin * This code is derived from software contributed to The NetBSD Foundation
81.1Smartin * by Peter Bex <Peter.Bex@student.kun.nl>.
91.1Smartin *
101.1Smartin * Redistribution and use in source and binary forms, with or without
111.1Smartin * modification, are permitted provided that the following conditions
121.1Smartin * are met:
131.1Smartin * 1. Redistributions of source code must retain the above copyright
141.1Smartin *    notice, this list of conditions and the following disclaimer.
151.1Smartin * 2. Redistributions in binary form must reproduce the above copyright
161.1Smartin *    notice, this list of conditions and the following disclaimer in the
171.1Smartin *    documentation and/or other materials provided with the distribution.
181.1Smartin * 3. All advertising materials mentioning features or use of this software
191.1Smartin *    must display the following acknowledgement:
201.1Smartin *	This product includes software developed by the NetBSD
211.1Smartin *	Foundation, Inc. and its contributors.
221.1Smartin * 4. Neither the name of The NetBSD Foundation nor the names of its
231.1Smartin *    contributors may be used to endorse or promote products derived
241.1Smartin *    from this software without specific prior written permission.
251.1Smartin *
261.1Smartin * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
271.1Smartin * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
281.1Smartin * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
291.1Smartin * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
301.1Smartin * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
311.1Smartin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
321.1Smartin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
331.1Smartin * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
341.1Smartin * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
351.1Smartin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
361.1Smartin * POSSIBILITY OF SUCH DAMAGE.
371.1Smartin */
381.1Smartin
391.1Smartin/*
401.1Smartin * PCI interface for MTD803 cards
411.1Smartin * Written by Peter Bex (peter.bex@student.kun.nl)
421.1Smartin */
431.1Smartin
441.1Smartin/* TODO: Check why in IO space, the MII won't work. Memory mapped works */
451.2Slukem
461.2Slukem#include <sys/cdefs.h>
471.11Sdyoung__KERNEL_RCSID(0, "$NetBSD: if_mtd_pci.c,v 1.11 2008/03/12 18:26:58 dyoung Exp $");
481.1Smartin
491.1Smartin#include <sys/param.h>
501.1Smartin#include <sys/device.h>
511.1Smartin#include <sys/systm.h>
521.1Smartin#include <sys/socket.h>
531.1Smartin#include <net/if.h>
541.1Smartin#include <net/if_ether.h>
551.1Smartin#include <net/if_media.h>
561.10Sad#include <sys/bus.h>
571.1Smartin#include <dev/mii/miivar.h>
581.1Smartin#include <dev/ic/mtd803reg.h>
591.1Smartin#include <dev/ic/mtd803var.h>
601.1Smartin#include <dev/pci/pcidevs.h>
611.1Smartin#include <dev/pci/pcireg.h>
621.1Smartin#include <dev/pci/pcivar.h>
631.1Smartin
641.1Smartin#define PCI_IO_MAP_REG		0x10
651.1Smartin#define PCI_MEM_MAP_REG		0x14
661.1Smartin
671.1Smartinstruct mtd_pci_device_id {
681.1Smartin	pci_vendor_id_t		vendor;		/* PCI vendor ID */
691.1Smartin	pci_product_id_t	product;	/* PCI product ID */
701.1Smartin};
711.1Smartin
721.1Smartinstatic struct mtd_pci_device_id mtd_ids[] = {
731.1Smartin	{ PCI_VENDOR_MYSON, PCI_PRODUCT_MYSON_MTD803 },
741.1Smartin	{ 0, 0 }
751.1Smartin};
761.1Smartin
771.11Sdyoungstatic int	mtd_pci_match(device_t, struct cfdata *, void *);
781.11Sdyoungstatic void	mtd_pci_attach(device_t, device_t, void *);
791.1Smartin
801.1SmartinCFATTACH_DECL(mtd_pci, sizeof(struct mtd_softc), mtd_pci_match, mtd_pci_attach,
811.1Smartin    NULL, NULL);
821.1Smartin
831.5Sthorpejstatic int
841.11Sdyoungmtd_pci_match(device_t parent, struct cfdata *match, void *aux)
851.1Smartin{
861.1Smartin	struct pci_attach_args *pa = aux;
871.1Smartin	struct mtd_pci_device_id *id;
881.1Smartin
891.1Smartin	for (id = mtd_ids; id->vendor != 0; ++id) {
901.1Smartin		if (PCI_VENDOR(pa->pa_id) == id->vendor &&
911.1Smartin		    PCI_PRODUCT(pa->pa_id) == id->product)
921.1Smartin			return (1);
931.1Smartin	}
941.1Smartin	return (0);
951.1Smartin}
961.1Smartin
971.5Sthorpejstatic void
981.11Sdyoungmtd_pci_attach(device_t parent, device_t self, void *aux)
991.1Smartin{
1001.1Smartin	struct pci_attach_args * const pa = aux;
1011.11Sdyoung	struct mtd_softc * const sc = device_private(self);
1021.1Smartin	pci_intr_handle_t ih;
1031.1Smartin	const char *intrstring = NULL;
1041.1Smartin	bus_space_tag_t iot, memt;
1051.1Smartin	bus_space_handle_t ioh, memh;
1061.1Smartin	int io_valid, mem_valid;
1071.1Smartin	char devinfo[256];
1081.1Smartin
1091.4Sitojun	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
1101.1Smartin	printf(": %s (rev. 0x%02x)\n", devinfo, PCI_REVISION(pa->pa_class));
1111.1Smartin
1121.1Smartin	io_valid = (pci_mapreg_map(pa, PCI_IO_MAP_REG, PCI_MAPREG_TYPE_IO,
1131.3Ssimonb			0, &iot, &ioh, NULL, NULL) == 0);
1141.1Smartin	mem_valid = (pci_mapreg_map(pa, PCI_MEM_MAP_REG, PCI_MAPREG_TYPE_MEM
1151.6Sperry			| PCI_MAPREG_MEM_TYPE_32BIT, 0, &memt, &memh,
1161.1Smartin			NULL, NULL) == 0);
1171.1Smartin
1181.1Smartin	if (mem_valid) {
1191.1Smartin		sc->bus_tag = memt;
1201.1Smartin		sc->bus_handle = memh;
1211.1Smartin	} else if (io_valid) {
1221.1Smartin		sc->bus_tag = iot;
1231.1Smartin		sc->bus_handle = ioh;
1241.1Smartin	} else {
1251.1Smartin		printf("%s: could not map memory or i/o space\n",
1261.1Smartin			sc->dev.dv_xname);
1271.1Smartin		return;
1281.1Smartin	}
1291.1Smartin	sc->dma_tag = pa->pa_dmat;
1301.1Smartin
1311.1Smartin	/* Do generic attach. Seems this must be done before setting IRQ */
1321.1Smartin	mtd_config(sc);
1331.1Smartin
1341.1Smartin	if (pci_intr_map(pa, &ih)) {
1351.1Smartin		printf("%s: could not map interrupt\n", sc->dev.dv_xname);
1361.1Smartin		return;
1371.1Smartin	}
1381.1Smartin	intrstring = pci_intr_string(pa->pa_pc, ih);
1391.1Smartin
1401.1Smartin	if (pci_intr_establish(pa->pa_pc, ih, IPL_NET, mtd_irq_h, sc) == NULL) {
1411.1Smartin		printf("%s: could not establish interrupt", sc->dev.dv_xname);
1421.1Smartin		if (intrstring != NULL)
1431.1Smartin			printf(" at %s", intrstring);
1441.1Smartin		printf("\n");
1451.1Smartin		return;
1461.1Smartin	} else {
1471.1Smartin		printf("%s: using %s for interrupt\n",
1481.1Smartin			sc->dev.dv_xname,
1491.1Smartin			intrstring ? intrstring : "unknown interrupt");
1501.1Smartin	}
1511.1Smartin}
152