1 1.5 andvar /* $NetBSD: cycv_rstmgr.c,v 1.5 2022/01/01 13:47:19 andvar Exp $ */ 2 1.1 aymeric 3 1.1 aymeric /* This file is in the public domain. */ 4 1.1 aymeric 5 1.1 aymeric #include <sys/cdefs.h> 6 1.5 andvar __KERNEL_RCSID(0, "$NetBSD: cycv_rstmgr.c,v 1.5 2022/01/01 13:47:19 andvar Exp $"); 7 1.1 aymeric 8 1.1 aymeric #include <sys/param.h> 9 1.1 aymeric #include <sys/bus.h> 10 1.1 aymeric #include <sys/device.h> 11 1.1 aymeric #include <sys/intr.h> 12 1.1 aymeric #include <sys/systm.h> 13 1.1 aymeric #include <sys/kernel.h> 14 1.1 aymeric #include <sys/atomic.h> 15 1.1 aymeric #include <sys/kmem.h> 16 1.1 aymeric 17 1.1 aymeric #include <arm/altera/cycv_reg.h> 18 1.1 aymeric #include <arm/altera/cycv_var.h> 19 1.1 aymeric 20 1.1 aymeric #include <dev/fdt/fdtvar.h> 21 1.1 aymeric 22 1.1 aymeric static int cycv_rstmgr_match(device_t, cfdata_t, void *); 23 1.1 aymeric static void cycv_rstmgr_attach(device_t, device_t, void *); 24 1.1 aymeric 25 1.1 aymeric static void *cycv_rst_acquire(device_t, const void *, size_t); 26 1.1 aymeric static void cycv_rst_release(device_t, void *); 27 1.1 aymeric static int cycv_rst_reset_assert(device_t, void *); 28 1.1 aymeric static int cycv_rst_reset_deassert(device_t, void *); 29 1.1 aymeric 30 1.1 aymeric static const struct fdtbus_reset_controller_func cycv_rstmgr_funcs = { 31 1.1 aymeric cycv_rst_acquire, cycv_rst_release, 32 1.1 aymeric cycv_rst_reset_assert, cycv_rst_reset_deassert 33 1.1 aymeric }; 34 1.1 aymeric 35 1.1 aymeric struct cycv_rstmgr_softc { 36 1.1 aymeric device_t sc_dev; 37 1.1 aymeric 38 1.1 aymeric bus_space_tag_t sc_bst; 39 1.1 aymeric bus_space_handle_t sc_bsh; 40 1.1 aymeric }; 41 1.1 aymeric 42 1.1 aymeric struct cycv_reset { 43 1.1 aymeric bus_addr_t address; 44 1.1 aymeric uint32_t mask; 45 1.1 aymeric }; 46 1.1 aymeric 47 1.1 aymeric CFATTACH_DECL_NEW(cycvrstmgr, sizeof (struct cycv_rstmgr_softc), 48 1.1 aymeric cycv_rstmgr_match, cycv_rstmgr_attach, NULL, NULL); 49 1.1 aymeric 50 1.1 aymeric static struct cycv_rstmgr_softc *cycv_rstmgr_sc; 51 1.1 aymeric 52 1.4 thorpej static const struct device_compatible_entry compat_data[] = { 53 1.4 thorpej { .compat = "altr,rst-mgr" }, 54 1.4 thorpej DEVICE_COMPAT_EOL 55 1.4 thorpej }; 56 1.4 thorpej 57 1.1 aymeric static int 58 1.1 aymeric cycv_rstmgr_match(device_t parent, cfdata_t cf, void *aux) 59 1.1 aymeric { 60 1.1 aymeric struct fdt_attach_args *faa = aux; 61 1.1 aymeric 62 1.4 thorpej return of_compatible_match(faa->faa_phandle, compat_data); 63 1.1 aymeric } 64 1.1 aymeric 65 1.1 aymeric static void 66 1.1 aymeric cycv_rstmgr_attach(device_t parent, device_t self, void *aux) 67 1.1 aymeric { 68 1.1 aymeric struct cycv_rstmgr_softc *sc = device_private(self); 69 1.1 aymeric struct fdt_attach_args *faa = aux; 70 1.1 aymeric int phandle = faa->faa_phandle; 71 1.1 aymeric bus_addr_t addr; 72 1.1 aymeric bus_size_t size; 73 1.1 aymeric int error; 74 1.1 aymeric 75 1.1 aymeric if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 76 1.1 aymeric aprint_error(": couldn't get registers\n"); 77 1.1 aymeric return; 78 1.1 aymeric } 79 1.1 aymeric 80 1.1 aymeric sc->sc_dev = self; 81 1.1 aymeric sc->sc_bst = faa->faa_bst; 82 1.1 aymeric error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh); 83 1.1 aymeric if (error) { 84 1.3 skrll aprint_error(": couldn't map %#" PRIxBUSADDR ": %d", 85 1.3 skrll addr, error); 86 1.1 aymeric return; 87 1.1 aymeric } 88 1.1 aymeric 89 1.1 aymeric aprint_normal(": reset manager\n"); 90 1.1 aymeric 91 1.1 aymeric fdtbus_register_reset_controller(self, phandle, &cycv_rstmgr_funcs); 92 1.1 aymeric cycv_rstmgr_sc = sc; 93 1.1 aymeric } 94 1.1 aymeric 95 1.1 aymeric static void * 96 1.1 aymeric cycv_rst_acquire(device_t dev, const void *data, size_t len) { 97 1.1 aymeric struct cycv_reset *reset = NULL; 98 1.1 aymeric uint32_t value; 99 1.1 aymeric 100 1.1 aymeric if (len != sizeof value) 101 1.1 aymeric goto err_decode; 102 1.1 aymeric 103 1.1 aymeric value = of_decode_int(data); 104 1.1 aymeric if (value < 0 || value > 105 1.2 aymeric (CYCV_RSTMGR_MISCMODRST - CYCV_RSTMGR_MPUMODRST + 4) / 4 * 32) 106 1.1 aymeric goto err_decode; 107 1.1 aymeric 108 1.1 aymeric reset = kmem_alloc(sizeof *reset, KM_SLEEP); 109 1.2 aymeric reset->address = CYCV_RSTMGR_MPUMODRST + value / 32 * 4; 110 1.1 aymeric reset->mask = 1 << (value % 32); 111 1.1 aymeric 112 1.1 aymeric if (0) { 113 1.1 aymeric err_decode: 114 1.5 andvar aprint_debug_dev(dev, "couldn't decode reset\n"); 115 1.1 aymeric } 116 1.1 aymeric return reset; 117 1.1 aymeric } 118 1.1 aymeric 119 1.1 aymeric static void 120 1.1 aymeric cycv_rst_release(device_t dev, void *r) { 121 1.1 aymeric kmem_free(r, sizeof (struct cycv_reset)); 122 1.1 aymeric } 123 1.1 aymeric 124 1.1 aymeric static void 125 1.1 aymeric cycv_rst_reset_set(device_t dev, struct cycv_reset *reset, int set) { 126 1.1 aymeric struct cycv_rstmgr_softc *sc = device_private(dev); 127 1.1 aymeric uint32_t val = bus_space_read_4(sc->sc_bst, sc->sc_bsh, reset->address); 128 1.1 aymeric 129 1.1 aymeric if (set) 130 1.1 aymeric val |= reset->mask; 131 1.1 aymeric else 132 1.1 aymeric val &= ~reset->mask; 133 1.1 aymeric 134 1.1 aymeric bus_space_write_4(sc->sc_bst, sc->sc_bsh, reset->address, val); 135 1.1 aymeric } 136 1.1 aymeric 137 1.1 aymeric static int 138 1.1 aymeric cycv_rst_reset_assert(device_t dev, void *r) { 139 1.1 aymeric 140 1.1 aymeric cycv_rst_reset_set(dev, r, 1); 141 1.1 aymeric 142 1.1 aymeric return 0; 143 1.1 aymeric } 144 1.1 aymeric 145 1.1 aymeric static int 146 1.1 aymeric cycv_rst_reset_deassert(device_t dev, void *r) { 147 1.1 aymeric 148 1.1 aymeric cycv_rst_reset_set(dev, r, 0); 149 1.1 aymeric 150 1.1 aymeric return 0; 151 1.1 aymeric } 152