1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2012,2013 Bjoern A. Zeeb 5 * All rights reserved. 6 * 7 * This software was developed by SRI International and the University of 8 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-11-C-0249) 9 * ("MRC2"), as part of the DARPA MRC research programme. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 #include "opt_device_polling.h" 37 38 #include <sys/param.h> 39 #include <sys/kernel.h> 40 #include <sys/bus.h> 41 #include <sys/module.h> 42 #include <sys/rman.h> 43 #include <sys/socket.h> 44 #include <sys/types.h> 45 46 #include <machine/bus.h> 47 #include <machine/resource.h> 48 49 #include <net/ethernet.h> 50 #include <net/if.h> 51 #include <net/if_media.h> 52 #include <net/if_var.h> 53 54 #include <dev/mii/mii.h> 55 #include <dev/mii/miivar.h> 56 57 #include <dev/altera/atse/if_atsereg.h> 58 59 /* "device miibus" required. See GENERIC if you get errors here. */ 60 #include "miibus_if.h" 61 62 MODULE_DEPEND(atse, ether, 1, 1, 1); 63 MODULE_DEPEND(atse, miibus, 1, 1, 1); 64 65 /* 66 * Device routines for interacting with nexus (probe, attach, detach) & helpers. 67 * XXX We should add suspend/resume later. 68 */ 69 static int __unused 70 atse_resource_int(device_t dev, const char *resname, int *v) 71 { 72 int error; 73 74 error = resource_int_value(device_get_name(dev), device_get_unit(dev), 75 resname, v); 76 if (error != 0) { 77 /* If it does not exist, we fail, so not ingoring ENOENT. */ 78 device_printf(dev, "could not fetch '%s' hint\n", resname); 79 return (error); 80 } 81 82 return (0); 83 } 84 85 static int __unused 86 atse_resource_long(device_t dev, const char *resname, long *v) 87 { 88 int error; 89 90 error = resource_long_value(device_get_name(dev), device_get_unit(dev), 91 resname, v); 92 if (error != 0) { 93 /* If it does not exist, we fail, so not ingoring ENOENT. */ 94 device_printf(dev, "could not fetch '%s' hint\n", resname); 95 return (error); 96 } 97 98 return (0); 99 } 100 101 static int 102 atse_probe_nexus(device_t dev) 103 { 104 105 device_set_desc(dev, "Altera Triple-Speed Ethernet MegaCore"); 106 107 return (BUS_PROBE_NOWILDCARD); 108 } 109 110 static int 111 atse_attach_nexus(device_t dev) 112 { 113 struct atse_softc *sc; 114 int error; 115 116 sc = device_get_softc(dev); 117 sc->atse_dev = dev; 118 sc->atse_unit = device_get_unit(dev); 119 120 /* Avalon-MM, atse management register region. */ 121 sc->atse_mem_rid = 0; 122 sc->atse_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 123 &sc->atse_mem_rid, RF_ACTIVE); 124 if (sc->atse_mem_res == NULL) { 125 device_printf(dev, "failed to map memory for ctrl region\n"); 126 return (ENXIO); 127 } 128 129 error = atse_attach(dev); 130 if (error) { 131 /* Cleanup. */ 132 atse_detach_resources(dev); 133 return (error); 134 } 135 136 return (0); 137 } 138 139 static device_method_t atse_methods_nexus[] = { 140 /* Device interface */ 141 DEVMETHOD(device_probe, atse_probe_nexus), 142 DEVMETHOD(device_attach, atse_attach_nexus), 143 DEVMETHOD(device_detach, atse_detach_dev), 144 145 /* MII interface */ 146 DEVMETHOD(miibus_readreg, atse_miibus_readreg), 147 DEVMETHOD(miibus_writereg, atse_miibus_writereg), 148 DEVMETHOD(miibus_statchg, atse_miibus_statchg), 149 150 DEVMETHOD_END 151 }; 152 153 static driver_t atse_driver_nexus = { 154 "atse", 155 atse_methods_nexus, 156 sizeof(struct atse_softc) 157 }; 158 159 DRIVER_MODULE(atse, nexus, atse_driver_nexus, atse_devclass, 0, 0); 160 DRIVER_MODULE(miibus, atse, miibus_driver, miibus_devclass, 0, 0); 161