1 /* $NetBSD: ingenic_dme.c,v 1.5 2025/10/04 04:44:20 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 2015 Michael Lorenz 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: ingenic_dme.c,v 1.5 2025/10/04 04:44:20 thorpej Exp $"); 31 32 #include <sys/param.h> 33 #include <sys/intr.h> 34 #include <sys/bus.h> 35 #include <sys/device.h> 36 #include <sys/systm.h> 37 38 #include <mips/ingenic/ingenic_var.h> 39 #include <mips/ingenic/ingenic_regs.h> 40 41 #include <net/if.h> 42 #include <net/if_ether.h> 43 #include <net/if_media.h> 44 #include <dev/mii/miivar.h> 45 46 #include <dev/ic/dm9000var.h> 47 #include <dev/ic/dm9000reg.h> 48 49 #include "opt_ingenic.h" 50 51 static int ingenic_dme_match(device_t, struct cfdata *, void *); 52 static void ingenic_dme_attach(device_t, device_t, void *); 53 static int ingenic_dme_intr(void *); 54 55 CFATTACH_DECL_NEW(ingenic_dme, sizeof(struct dme_softc), 56 ingenic_dme_match, ingenic_dme_attach, NULL, NULL); 57 58 #define GPIO_DME_INT 19 59 #define GPIO_DME_INT_MASK (1 << GPIO_DME_INT) 60 61 /* ARGSUSED */ 62 static int 63 ingenic_dme_match(device_t parent, struct cfdata *match, void *aux) 64 { 65 struct apbus_attach_args *aa = aux; 66 67 if (strcmp(aa->aa_name, "dme") != 0) 68 return 0; 69 70 return 1; 71 } 72 73 /* ARGSUSED */ 74 static void 75 ingenic_dme_attach(device_t parent, device_t self, void *aux) 76 { 77 struct dme_softc *sc = device_private(self); 78 struct apbus_attach_args *aa = aux; 79 void *ih; 80 static uint8_t enaddr[ETHER_ADDR_LEN]; 81 int error; 82 83 sc->sc_dev = self; 84 85 sc->sc_iot = aa->aa_bst; 86 sc->dme_io = JZ_DME_IO; 87 sc->dme_data = JZ_DME_DATA; 88 sc->sc_phy_initialized = 0; 89 90 if (aa->aa_addr == 0) 91 aa->aa_addr = JZ_DME_BASE; 92 93 error = bus_space_map(aa->aa_bst, aa->aa_addr, 4, 0, &sc->sc_ioh); 94 if (error) { 95 aprint_error_dev(self, 96 "can't map registers for %s: %d\n", aa->aa_name, error); 97 return; 98 } 99 100 aprint_naive(": DM9000 Ethernet controller\n"); 101 aprint_normal(": DM9000 Ethernet controller\n"); 102 103 104 /* make sure the chip is powered up and not in reset */ 105 gpio_as_output(1, 25); 106 gpio_set(1, 25, 1); 107 gpio_as_output(5, 12); 108 gpio_set(5, 12, 1); 109 110 /* setup pins to talk to the chip */ 111 gpio_as_dev0(1, 1); 112 gpio_as_dev0(0, 0); 113 gpio_as_dev0(0, 1); 114 gpio_as_dev0(0, 2); 115 gpio_as_dev0(0, 3); 116 gpio_as_dev0(0, 4); 117 gpio_as_dev0(0, 5); 118 gpio_as_dev0(0, 6); 119 gpio_as_dev0(0, 7); 120 121 gpio_as_dev0(0, 16); 122 gpio_as_dev0(0, 17); 123 gpio_as_dev0(0, 26); 124 125 /* DM9000 interrupt is on GPIO E pin 19 */ 126 gpio_as_intr_level(4, GPIO_DME_INT); 127 ih = evbmips_intr_establish(13, ingenic_dme_intr, sc); 128 129 if (ih == NULL) { 130 aprint_error_dev(self, "failed to establish interrupt %d\n", 131 13); 132 goto fail; 133 } 134 #if 0 135 if (! ether_getaddr(self, enaddr)) { 136 /* 137 * XXX 138 * if we don't get the MAC address as a property we hope like 139 * hell that uboot programmed it into the network chip 140 */ 141 aprint_error_dev(self, "reading MAC address from chip\n"); 142 dme_read_c(sc, DM9000_PAB0, enaddr, 6); 143 } 144 #else 145 /* 146 * dme_attach checks dictionary, then previous setting, then roll 147 * a dice to make random MAC address 148 */ 149 #endif 150 dme_attach(sc, enaddr); 151 return; 152 fail: 153 if (ih) { 154 evbmips_intr_disestablish(ih); 155 } 156 bus_space_unmap(sc->sc_iot, sc->sc_ioh, 4); 157 } 158 159 static int 160 ingenic_dme_intr(void *arg) 161 { 162 uint32_t reg; 163 int ret = 0; 164 165 /* see if it's us */ 166 reg = readreg(JZ_GPIO_E_BASE + JZ_GPIO_FLAG); 167 if (reg & GPIO_DME_INT_MASK) { 168 /* yes, it's ours, handle it... */ 169 ret = dme_intr(arg); 170 /* ... and clear it */ 171 writereg(JZ_GPIO_E_BASE + JZ_GPIO_FLAGC, GPIO_DME_INT_MASK); 172 } 173 return ret; 174 } 175