11.22Sthorpej/* $NetBSD: if_mtd_pci.c,v 1.22 2021/05/08 00:27:02 thorpej 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.22Sthorpej__KERNEL_RCSID(0, "$NetBSD: if_mtd_pci.c,v 1.22 2021/05/08 00:27:02 thorpej 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.16Sdyoung#define PCI_IO_MAP_REG PCI_BAR(0)
581.16Sdyoung#define PCI_MEM_MAP_REG PCI_BAR(1)
591.1Smartin
601.22Sthorpejstatic const struct device_compatible_entry compat_data[] = {
611.22Sthorpej	{ .id = PCI_ID_CODE(PCI_VENDOR_MYSON, PCI_PRODUCT_MYSON_MTD803) },
621.1Smartin
631.22Sthorpej	PCI_COMPAT_EOL
641.1Smartin};
651.1Smartin
661.14Sceggerstatic int	mtd_pci_match(device_t, cfdata_t, void *);
671.11Sdyoungstatic void	mtd_pci_attach(device_t, device_t, void *);
681.1Smartin
691.20SmsaitohCFATTACH_DECL_NEW(mtd_pci, sizeof(struct mtd_softc), mtd_pci_match,
701.20Smsaitoh    mtd_pci_attach, NULL, NULL);
711.1Smartin
721.5Sthorpejstatic int
731.14Sceggermtd_pci_match(device_t parent, cfdata_t match, void *aux)
741.1Smartin{
751.1Smartin	struct pci_attach_args *pa = aux;
761.1Smartin
771.22Sthorpej	return pci_compatible_match(pa, compat_data);
781.1Smartin}
791.1Smartin
801.5Sthorpejstatic void
811.11Sdyoungmtd_pci_attach(device_t parent, device_t self, void *aux)
821.1Smartin{
831.1Smartin	struct pci_attach_args * const pa = aux;
841.11Sdyoung	struct mtd_softc * const sc = device_private(self);
851.1Smartin	pci_intr_handle_t ih;
861.1Smartin	const char *intrstring = NULL;
871.1Smartin	bus_space_tag_t iot, memt;
881.1Smartin	bus_space_handle_t ioh, memh;
891.1Smartin	int io_valid, mem_valid;
901.19Schristos	char intrbuf[PCI_INTRSTR_LEN];
911.1Smartin
921.18Schs	sc->dev = self;
931.17Sdrochner	pci_aprint_devinfo(pa, NULL);
941.1Smartin
951.1Smartin	io_valid = (pci_mapreg_map(pa, PCI_IO_MAP_REG, PCI_MAPREG_TYPE_IO,
961.3Ssimonb			0, &iot, &ioh, NULL, NULL) == 0);
971.1Smartin	mem_valid = (pci_mapreg_map(pa, PCI_MEM_MAP_REG, PCI_MAPREG_TYPE_MEM
981.6Sperry			| PCI_MAPREG_MEM_TYPE_32BIT, 0, &memt, &memh,
991.1Smartin			NULL, NULL) == 0);
1001.1Smartin
1011.1Smartin	if (mem_valid) {
1021.1Smartin		sc->bus_tag = memt;
1031.1Smartin		sc->bus_handle = memh;
1041.1Smartin	} else if (io_valid) {
1051.1Smartin		sc->bus_tag = iot;
1061.1Smartin		sc->bus_handle = ioh;
1071.1Smartin	} else {
1081.20Smsaitoh		aprint_error_dev(sc->dev,
1091.20Smsaitoh		    "could not map memory or i/o space\n");
1101.1Smartin		return;
1111.1Smartin	}
1121.1Smartin	sc->dma_tag = pa->pa_dmat;
1131.1Smartin
1141.1Smartin	/* Do generic attach. Seems this must be done before setting IRQ */
1151.1Smartin	mtd_config(sc);
1161.1Smartin
1171.1Smartin	if (pci_intr_map(pa, &ih)) {
1181.18Schs		aprint_error_dev(sc->dev, "could not map interrupt\n");
1191.1Smartin		return;
1201.1Smartin	}
1211.19Schristos	intrstring = pci_intr_string(pa->pa_pc, ih, intrbuf, sizeof(intrbuf));
1221.1Smartin
1231.21Sjdolecek	if (pci_intr_establish_xname(pa->pa_pc, ih, IPL_NET, mtd_irq_h, sc,
1241.21Sjdolecek	    device_xname(self)) == NULL) {
1251.18Schs		aprint_error_dev(sc->dev, "could not establish interrupt");
1261.1Smartin		if (intrstring != NULL)
1271.15Snjoly			aprint_error(" at %s", intrstring);
1281.15Snjoly		aprint_error("\n");
1291.1Smartin		return;
1301.1Smartin	} else {
1311.18Schs		aprint_normal_dev(sc->dev, "using %s for interrupt\n",
1321.1Smartin			intrstring ? intrstring : "unknown interrupt");
1331.1Smartin	}
1341.1Smartin}
135