1*71fbb921Smsaitoh /* $NetBSD: if_tra_mca.c,v 1.18 2016/07/11 11:31:51 msaitoh Exp $ */
21c891798Sjdolecek
31c891798Sjdolecek /*-
41c891798Sjdolecek * Copyright (c) 2004 The NetBSD Foundation, Inc.
51c891798Sjdolecek * All rights reserved.
61c891798Sjdolecek *
71c891798Sjdolecek * This code is derived from software contributed to The NetBSD Foundation
81c891798Sjdolecek * by Jaromir Dolecek.
91c891798Sjdolecek *
101c891798Sjdolecek * Redistribution and use in source and binary forms, with or without
111c891798Sjdolecek * modification, are permitted provided that the following conditions
121c891798Sjdolecek * are met:
131c891798Sjdolecek * 1. Redistributions of source code must retain the above copyright
141c891798Sjdolecek * notice, this list of conditions and the following disclaimer.
151c891798Sjdolecek * 2. Redistributions in binary form must reproduce the above copyright
161c891798Sjdolecek * notice, this list of conditions and the following disclaimer in the
171c891798Sjdolecek * documentation and/or other materials provided with the distribution.
181c891798Sjdolecek *
191c891798Sjdolecek * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201c891798Sjdolecek * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211c891798Sjdolecek * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221c891798Sjdolecek * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231c891798Sjdolecek * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241c891798Sjdolecek * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251c891798Sjdolecek * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261c891798Sjdolecek * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271c891798Sjdolecek * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281c891798Sjdolecek * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291c891798Sjdolecek * POSSIBILITY OF SUCH DAMAGE.
301c891798Sjdolecek */
311c891798Sjdolecek
321c891798Sjdolecek /*
338914aa32Sjdolecek * Driver for Tiara LANCard/E II and friends adapted from if_ate_mca.c
348914aa32Sjdolecek * by Dave J. Barnes 2004.
351c891798Sjdolecek */
361c891798Sjdolecek
371c891798Sjdolecek #include <sys/cdefs.h>
38*71fbb921Smsaitoh __KERNEL_RCSID(0, "$NetBSD: if_tra_mca.c,v 1.18 2016/07/11 11:31:51 msaitoh Exp $");
391c891798Sjdolecek
401c891798Sjdolecek #include <sys/param.h>
411c891798Sjdolecek #include <sys/systm.h>
421c891798Sjdolecek #include <sys/device.h>
431c891798Sjdolecek #include <sys/socket.h>
441c891798Sjdolecek #include <sys/syslog.h>
451c891798Sjdolecek
461c891798Sjdolecek #include <net/if.h>
471c891798Sjdolecek #include <net/if_ether.h>
481c891798Sjdolecek #include <net/if_media.h>
491c891798Sjdolecek
50a2a38285Sad #include <sys/bus.h>
51a2a38285Sad #include <sys/intr.h>
521c891798Sjdolecek
531c891798Sjdolecek #include <dev/ic/mb86950reg.h>
541c891798Sjdolecek #include <dev/ic/mb86950var.h>
551c891798Sjdolecek
561c891798Sjdolecek #include <dev/mca/mcavar.h>
571c891798Sjdolecek #include <dev/mca/mcadevs.h>
581c891798Sjdolecek
5999747a80Scegger int tiara_mca_match(device_t, cfdata_t, void *);
6099747a80Scegger void tiara_mca_attach(device_t, device_t, void *);
611c891798Sjdolecek
621c891798Sjdolecek #define TIARA_NPORTS 0x20 /* 32 */
631c891798Sjdolecek #define TIARA_PROM_ID 24 /* offset to mac addr stored in prom */
641c891798Sjdolecek
651c891798Sjdolecek struct tiara_softc {
661c891798Sjdolecek struct mb86950_softc sc_mb86950; /* real "mb86950" softc */
671c891798Sjdolecek
681c891798Sjdolecek /* MCA-specific goo. */
691c891798Sjdolecek void *sc_ih; /* interrupt cookie */
701c891798Sjdolecek };
711c891798Sjdolecek
72cbab9cadSchs CFATTACH_DECL_NEW(tra_mca, sizeof(struct tiara_softc),
731c891798Sjdolecek tiara_mca_match, tiara_mca_attach, NULL, NULL);
741c891798Sjdolecek
751c891798Sjdolecek static const struct tiara_mca_product {
761c891798Sjdolecek u_int32_t tra_prodid; /* MCA product ID */
771c891798Sjdolecek const char *tra_name; /* device name */
781c891798Sjdolecek } tiara_mca_products[] = {
791c891798Sjdolecek { MCA_PRODUCT_TIARA, "Tiara LANCard/E2"},
801c891798Sjdolecek { MCA_PRODUCT_TIARA_TP, "Tiara LANCard/E2 TP"},
811c891798Sjdolecek { MCA_PRODUCT_SMC3016, "SMC 3016/MC"},
829d7a9c40Schristos { 0, NULL },
831c891798Sjdolecek };
841c891798Sjdolecek
8502cdf4d2Sdsl static const struct tiara_mca_product *tiara_mca_lookup(u_int32_t);
861c891798Sjdolecek
871c891798Sjdolecek static const struct tiara_mca_product *
tiara_mca_lookup(u_int32_t id)88454af1c0Sdsl tiara_mca_lookup(u_int32_t id)
891c891798Sjdolecek {
901c891798Sjdolecek const struct tiara_mca_product *tra_p;
911c891798Sjdolecek
921c891798Sjdolecek for (tra_p = tiara_mca_products; tra_p->tra_name != NULL; tra_p++)
931c891798Sjdolecek if (id == tra_p->tra_prodid)
941c891798Sjdolecek return (tra_p);
951c891798Sjdolecek
961c891798Sjdolecek return (NULL);
971c891798Sjdolecek }
981c891798Sjdolecek
991c891798Sjdolecek int
tiara_mca_match(device_t parent,cfdata_t match,void * aux)100cbab9cadSchs tiara_mca_match(device_t parent, cfdata_t match, void *aux)
1011c891798Sjdolecek {
1021c891798Sjdolecek struct mca_attach_args *ma = (struct mca_attach_args *) aux;
1031c891798Sjdolecek
1041c891798Sjdolecek if (tiara_mca_lookup(ma->ma_id) != NULL)
1051c891798Sjdolecek return (1);
1061c891798Sjdolecek
1071c891798Sjdolecek return (0);
1081c891798Sjdolecek }
1091c891798Sjdolecek
1101c891798Sjdolecek /* see POS diagrams below for explanation */
1111c891798Sjdolecek static const int tiara_irq[] = {
1121c891798Sjdolecek 3, 4, 7, 9
1131c891798Sjdolecek };
1141c891798Sjdolecek static const int smc_iobase[] = {
1151c891798Sjdolecek 0x300, 0x340, 0x360, 0x1980, 0x2000, 0x5680, 0x5900, 0x8080
1161c891798Sjdolecek };
1171c891798Sjdolecek static const int smc_irq[] = {
1181c891798Sjdolecek 9, 10, 11, 15, 3, 5, 7, 4
1191c891798Sjdolecek };
1201c891798Sjdolecek
1211c891798Sjdolecek void
tiara_mca_attach(device_t parent,device_t self,void * aux)12299747a80Scegger tiara_mca_attach(device_t parent, device_t self, void *aux)
1231c891798Sjdolecek {
124838ee1e0Sthorpej struct tiara_softc *isc = device_private(self);
1251c891798Sjdolecek struct mb86950_softc *sc = &isc->sc_mb86950;
1261c891798Sjdolecek struct mca_attach_args *ma = aux;
1271c891798Sjdolecek bus_space_tag_t iot = ma->ma_iot;
1281c891798Sjdolecek bus_space_handle_t ioh;
1291c891798Sjdolecek u_int8_t myea[ETHER_ADDR_LEN];
1301c891798Sjdolecek int pos2;
1311c891798Sjdolecek int iobase = 0, irq = 0;
1321c891798Sjdolecek const struct tiara_mca_product *tra_p;
1331c891798Sjdolecek
1341c891798Sjdolecek pos2 = mca_conf_read(ma->ma_mc, ma->ma_slot, 2);
1351c891798Sjdolecek
1361c891798Sjdolecek tra_p = tiara_mca_lookup(ma->ma_id);
1371c891798Sjdolecek
1381c891798Sjdolecek switch (tra_p->tra_prodid) {
1391c891798Sjdolecek
1401c891798Sjdolecek case MCA_PRODUCT_TIARA:
1411c891798Sjdolecek case MCA_PRODUCT_TIARA_TP:
1421c891798Sjdolecek /*
1431c891798Sjdolecek * POS register 2: (adf pos0)
1441c891798Sjdolecek * 7 6 5 4 3 2 1 0
1458914aa32Sjdolecek * \_____/ \_/ \ \__ enable: 0=disabled, 1=enabled
1461c891798Sjdolecek * \ \ \___ boot rom: 0=disabled, 1=enabled
1471c891798Sjdolecek * \ \______ IRQ 00=3 01=4 10=7 11=9
1481c891798Sjdolecek * \_________ Base I/O Port
1491c891798Sjdolecek * 0000=0x1200 0001=0x1220 ... 1110=0x13c0 1111=0x13e0
1501c891798Sjdolecek *
1511c891798Sjdolecek * POS register 3: (adf pos1) not used
1521c891798Sjdolecek * POS register 4: (adf pos2) not used
1531c891798Sjdolecek *
1541c891798Sjdolecek * POS register 5: (adf pos3) ignored
1551c891798Sjdolecek *
1561c891798Sjdolecek * 7 6 5 4 3 2 1 0
1571c891798Sjdolecek * 1 1 0 X \____/
1581c891798Sjdolecek * \____EPROM Address
1591c891798Sjdolecek */
1601c891798Sjdolecek iobase = 0x1200 + ((pos2 & 0xf0) << 1);
1611c891798Sjdolecek irq = tiara_irq[((pos2 & 0x0c) >> 2)];
1621c891798Sjdolecek
1631c891798Sjdolecek /* XXX SWAG for number pkts. */
1641c891798Sjdolecek /* My Tiara LANCard has 128K memory ?!? */
1651c891798Sjdolecek sc->txb_num_pkt = 4;
1661c891798Sjdolecek sc->rxb_num_pkt = (65535 - 8192 - 4) / 64;
1671c891798Sjdolecek /* XXX */
1681c891798Sjdolecek
1691c891798Sjdolecek break;
1701c891798Sjdolecek
1711c891798Sjdolecek case MCA_PRODUCT_SMC3016:
1721c891798Sjdolecek /*
1731c891798Sjdolecek * POS register 2: (adf pos0)
1741c891798Sjdolecek * 7 6 5 4 3 2 1 0
1758914aa32Sjdolecek * \_____/ \___/ \__ enable: 0=disabled, 1=enabled
1761c891798Sjdolecek * \ \_____ I/O Address (see ioaddr table)
1771c891798Sjdolecek * \__________ IRQ (see irq table)
1781c891798Sjdolecek *
1791c891798Sjdolecek * POS register 3: (adf pos1) ignored
1801c891798Sjdolecek * 7 6 5 4 3 2 1 0
1811c891798Sjdolecek * X X X X \____/
1821c891798Sjdolecek * \____EPROM Address (0000 = not used)
1831c891798Sjdolecek */
1841c891798Sjdolecek iobase = smc_iobase[((pos2 & 0x0e) >> 1)];
1858914aa32Sjdolecek if ((pos2 & 0x80) != 0)
1868914aa32Sjdolecek irq = smc_irq[((pos2 & 0x70) >> 4)];
1878914aa32Sjdolecek else {
188cbab9cadSchs aprint_error_dev(self, "unsupported irq selected\n");
1898914aa32Sjdolecek return;
1908914aa32Sjdolecek }
1911c891798Sjdolecek
1921c891798Sjdolecek /* XXX SWAG for number pkts. */
1931c891798Sjdolecek /* The SMC3016 has a 12K rx buffer and a 4k tx buffer */
1941c891798Sjdolecek sc->txb_num_pkt = 2;
1951c891798Sjdolecek sc->rxb_num_pkt = (12288 - 4) / 64;
1961c891798Sjdolecek /* XXX */
1971c891798Sjdolecek
1981c891798Sjdolecek break;
1991c891798Sjdolecek }
2001c891798Sjdolecek
2011c891798Sjdolecek
2021c891798Sjdolecek #ifdef DIAGNOSTIC
2031c891798Sjdolecek tra_p = tiara_mca_lookup(ma->ma_id);
2041c891798Sjdolecek if (tra_p == NULL) {
2051ef783cbScegger aprint_normal("\n");
206cbab9cadSchs aprint_error_dev(self, "where did the card go?\n");
2071c891798Sjdolecek return;
2081c891798Sjdolecek }
2091c891798Sjdolecek #endif
2101c891798Sjdolecek
211*71fbb921Smsaitoh printf(" slot %d ports %#x-%#x irq %d: %s\n", ma->ma_slot + 1,iobase,
212*71fbb921Smsaitoh iobase + TIARA_NPORTS, irq, tra_p->tra_name);
2131c891798Sjdolecek
2141c891798Sjdolecek /* Map i/o space. */
2151c891798Sjdolecek if (bus_space_map(iot, iobase, TIARA_NPORTS, 0, &ioh)) {
216cbab9cadSchs aprint_error_dev(self, "can't map i/o space\n");
2171c891798Sjdolecek return;
2181c891798Sjdolecek }
2191c891798Sjdolecek
220cbab9cadSchs sc->sc_dev = self;
2211c891798Sjdolecek sc->sc_bst = iot;
2221c891798Sjdolecek sc->sc_bsh = ioh;
2231c891798Sjdolecek
2241c891798Sjdolecek /* Get ethernet address from PROM */
2251c891798Sjdolecek bus_space_read_region_1(iot, ioh, TIARA_PROM_ID, myea, ETHER_ADDR_LEN);
2261c891798Sjdolecek
2271c891798Sjdolecek /* This interface is always enabled. */
2281c891798Sjdolecek /* XXX
2291c891798Sjdolecek sc->sc_stat |= NIC_STAT_ENABLED;
2301c891798Sjdolecek */
2318914aa32Sjdolecek
2321c891798Sjdolecek /*
2331c891798Sjdolecek * Do generic MB86950 attach.
2341c891798Sjdolecek */
2351c891798Sjdolecek mb86950_attach(sc, myea);
2361c891798Sjdolecek
2371c891798Sjdolecek
2381c891798Sjdolecek mb86950_config(sc, NULL, 0, 0);
2391c891798Sjdolecek
2401c891798Sjdolecek /* Establish the interrupt handler. */
2411c891798Sjdolecek isc->sc_ih = mca_intr_establish(ma->ma_mc, irq, IPL_NET,
2421c891798Sjdolecek mb86950_intr, sc);
2431c891798Sjdolecek if (isc->sc_ih == NULL) {
244*71fbb921Smsaitoh aprint_error_dev(self,
245*71fbb921Smsaitoh "couldn't establish interrupt handler\n");
2461c891798Sjdolecek return;
2471c891798Sjdolecek }
2481c891798Sjdolecek }
249