if_mtd_pci.c revision 1.15
11.15Snjoly/* $NetBSD: if_mtd_pci.c,v 1.15 2009/11/26 15:17:09 njoly 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 *
191.1Smartin * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201.1Smartin * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211.1Smartin * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221.1Smartin * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231.1Smartin * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241.1Smartin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251.1Smartin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261.1Smartin * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271.1Smartin * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281.1Smartin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291.1Smartin * POSSIBILITY OF SUCH DAMAGE.
301.1Smartin */
311.1Smartin
321.1Smartin/*
331.1Smartin * PCI interface for MTD803 cards
341.1Smartin * Written by Peter Bex (peter.bex@student.kun.nl)
351.1Smartin */
361.1Smartin
371.1Smartin/* TODO: Check why in IO space, the MII won't work. Memory mapped works */
381.2Slukem
391.2Slukem#include <sys/cdefs.h>
401.15Snjoly__KERNEL_RCSID(0, "$NetBSD: if_mtd_pci.c,v 1.15 2009/11/26 15:17:09 njoly Exp $");
411.1Smartin
421.1Smartin#include <sys/param.h>
431.1Smartin#include <sys/device.h>
441.1Smartin#include <sys/systm.h>
451.1Smartin#include <sys/socket.h>
461.1Smartin#include <net/if.h>
471.1Smartin#include <net/if_ether.h>
481.1Smartin#include <net/if_media.h>
491.10Sad#include <sys/bus.h>
501.1Smartin#include <dev/mii/miivar.h>
511.1Smartin#include <dev/ic/mtd803reg.h>
521.1Smartin#include <dev/ic/mtd803var.h>
531.1Smartin#include <dev/pci/pcidevs.h>
541.1Smartin#include <dev/pci/pcireg.h>
551.1Smartin#include <dev/pci/pcivar.h>
561.1Smartin
571.1Smartin#define PCI_IO_MAP_REG		0x10
581.1Smartin#define PCI_MEM_MAP_REG		0x14
591.1Smartin
601.1Smartinstruct mtd_pci_device_id {
611.1Smartin	pci_vendor_id_t		vendor;		/* PCI vendor ID */
621.1Smartin	pci_product_id_t	product;	/* PCI product ID */
631.1Smartin};
641.1Smartin
651.1Smartinstatic struct mtd_pci_device_id mtd_ids[] = {
661.1Smartin	{ PCI_VENDOR_MYSON, PCI_PRODUCT_MYSON_MTD803 },
671.1Smartin	{ 0, 0 }
681.1Smartin};
691.1Smartin
701.14Sceggerstatic int	mtd_pci_match(device_t, cfdata_t, void *);
711.11Sdyoungstatic void	mtd_pci_attach(device_t, device_t, void *);
721.1Smartin
731.1SmartinCFATTACH_DECL(mtd_pci, sizeof(struct mtd_softc), mtd_pci_match, mtd_pci_attach,
741.1Smartin    NULL, NULL);
751.1Smartin
761.5Sthorpejstatic int
771.14Sceggermtd_pci_match(device_t parent, cfdata_t match, void *aux)
781.1Smartin{
791.1Smartin	struct pci_attach_args *pa = aux;
801.1Smartin	struct mtd_pci_device_id *id;
811.1Smartin
821.1Smartin	for (id = mtd_ids; id->vendor != 0; ++id) {
831.1Smartin		if (PCI_VENDOR(pa->pa_id) == id->vendor &&
841.1Smartin		    PCI_PRODUCT(pa->pa_id) == id->product)
851.1Smartin			return (1);
861.1Smartin	}
871.1Smartin	return (0);
881.1Smartin}
891.1Smartin
901.5Sthorpejstatic void
911.11Sdyoungmtd_pci_attach(device_t parent, device_t self, void *aux)
921.1Smartin{
931.1Smartin	struct pci_attach_args * const pa = aux;
941.11Sdyoung	struct mtd_softc * const sc = device_private(self);
951.1Smartin	pci_intr_handle_t ih;
961.1Smartin	const char *intrstring = NULL;
971.1Smartin	bus_space_tag_t iot, memt;
981.1Smartin	bus_space_handle_t ioh, memh;
991.1Smartin	int io_valid, mem_valid;
1001.1Smartin	char devinfo[256];
1011.1Smartin
1021.4Sitojun	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
1031.1Smartin	printf(": %s (rev. 0x%02x)\n", devinfo, PCI_REVISION(pa->pa_class));
1041.1Smartin
1051.1Smartin	io_valid = (pci_mapreg_map(pa, PCI_IO_MAP_REG, PCI_MAPREG_TYPE_IO,
1061.3Ssimonb			0, &iot, &ioh, NULL, NULL) == 0);
1071.1Smartin	mem_valid = (pci_mapreg_map(pa, PCI_MEM_MAP_REG, PCI_MAPREG_TYPE_MEM
1081.6Sperry			| PCI_MAPREG_MEM_TYPE_32BIT, 0, &memt, &memh,
1091.1Smartin			NULL, NULL) == 0);
1101.1Smartin
1111.1Smartin	if (mem_valid) {
1121.1Smartin		sc->bus_tag = memt;
1131.1Smartin		sc->bus_handle = memh;
1141.1Smartin	} else if (io_valid) {
1151.1Smartin		sc->bus_tag = iot;
1161.1Smartin		sc->bus_handle = ioh;
1171.1Smartin	} else {
1181.12Scegger		aprint_error_dev(&sc->dev, "could not map memory or i/o space\n");
1191.1Smartin		return;
1201.1Smartin	}
1211.1Smartin	sc->dma_tag = pa->pa_dmat;
1221.1Smartin
1231.1Smartin	/* Do generic attach. Seems this must be done before setting IRQ */
1241.1Smartin	mtd_config(sc);
1251.1Smartin
1261.1Smartin	if (pci_intr_map(pa, &ih)) {
1271.12Scegger		aprint_error_dev(&sc->dev, "could not map interrupt\n");
1281.1Smartin		return;
1291.1Smartin	}
1301.1Smartin	intrstring = pci_intr_string(pa->pa_pc, ih);
1311.1Smartin
1321.1Smartin	if (pci_intr_establish(pa->pa_pc, ih, IPL_NET, mtd_irq_h, sc) == NULL) {
1331.12Scegger		aprint_error_dev(&sc->dev, "could not establish interrupt");
1341.1Smartin		if (intrstring != NULL)
1351.15Snjoly			aprint_error(" at %s", intrstring);
1361.15Snjoly		aprint_error("\n");
1371.1Smartin		return;
1381.1Smartin	} else {
1391.15Snjoly		aprint_normal_dev(&sc->dev, "using %s for interrupt\n",
1401.1Smartin			intrstring ? intrstring : "unknown interrupt");
1411.1Smartin	}
1421.1Smartin}
143