1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2018-2021 Emmanuel Vadot <manu@FreeBSD.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/param.h> 32 #include <sys/bus.h> 33 #include <sys/clock.h> 34 #include <sys/eventhandler.h> 35 #include <sys/kernel.h> 36 #include <sys/module.h> 37 #include <sys/reboot.h> 38 #include <sys/mutex.h> 39 #include <sys/rman.h> 40 #include <machine/bus.h> 41 42 #include <dev/iicbus/iiconf.h> 43 #include <dev/iicbus/iicbus.h> 44 45 #include <dev/ofw/ofw_bus.h> 46 #include <dev/ofw/ofw_bus_subr.h> 47 48 #include <dev/iicbus/pmic/rockchip/rk805reg.h> 49 #include <dev/iicbus/pmic/rockchip/rk808reg.h> 50 #include <dev/iicbus/pmic/rockchip/rk8xx.h> 51 52 #include "clock_if.h" 53 #include "regdev_if.h" 54 55 int 56 rk8xx_read(device_t dev, uint8_t reg, uint8_t *data, uint8_t size) 57 { 58 int err; 59 60 err = iicdev_readfrom(dev, reg, data, size, IIC_INTRWAIT); 61 return (err); 62 } 63 64 int 65 rk8xx_write(device_t dev, uint8_t reg, uint8_t *data, uint8_t size) 66 { 67 68 return (iicdev_writeto(dev, reg, data, size, IIC_INTRWAIT)); 69 } 70 71 static void 72 rk8xx_start(void *pdev) 73 { 74 struct rk8xx_softc *sc; 75 device_t dev; 76 uint8_t data[2]; 77 int err; 78 79 dev = pdev; 80 sc = device_get_softc(dev); 81 82 /* No version register in RK808 */ 83 if (bootverbose && sc->type == RK805) { 84 err = rk8xx_read(dev, RK805_CHIP_NAME, data, 1); 85 if (err != 0) { 86 device_printf(dev, "Cannot read chip name reg\n"); 87 return; 88 } 89 err = rk8xx_read(dev, RK805_CHIP_VER, data + 1, 1); 90 if (err != 0) { 91 device_printf(dev, "Cannot read chip version reg\n"); 92 return; 93 } 94 device_printf(dev, "Chip Name: %x\n", 95 data[0] << 4 | ((data[1] >> 4) & 0xf)); 96 device_printf(dev, "Chip Version: %x\n", data[1] & 0xf); 97 } 98 99 /* Register this as a 1Hz clock */ 100 clock_register(dev, 1000000); 101 102 config_intrhook_disestablish(&sc->intr_hook); 103 } 104 105 static void 106 rk8xx_poweroff(void *arg, int howto) 107 { 108 struct rk8xx_softc *sc = arg; 109 int error; 110 uint8_t val; 111 112 if ((howto & RB_POWEROFF) == 0) 113 return; 114 115 device_printf(sc->dev, "Powering off...\n"); 116 error = rk8xx_read(sc->dev, sc->dev_ctrl.dev_ctrl_reg, &val, 1); 117 if (error == 0) { 118 if (howto & RB_POWEROFF) 119 val |= sc->dev_ctrl.pwr_off_mask; 120 else if (howto & RB_POWERCYCLE) { 121 if (sc->type == RK809 || sc->type == RK817) { 122 if (bootverbose) { 123 device_printf(sc->dev, 124 "Powercycle PMIC\n"); 125 } 126 val |= sc->dev_ctrl.pwr_rst_mask;; 127 } else { 128 /* Poweroff PMIC that can't powercycle */ 129 val |= sc->dev_ctrl.pwr_off_mask; 130 } 131 } 132 error = rk8xx_write(sc->dev, sc->dev_ctrl.dev_ctrl_reg, 133 &val, 1); 134 135 /* Wait a bit for the command to take effect. */ 136 if (error == 0) 137 DELAY(100); 138 } 139 device_printf(sc->dev, "Power off failed\n"); 140 } 141 142 int 143 rk8xx_attach(struct rk8xx_softc *sc) 144 { 145 int error; 146 147 error = rk8xx_attach_clocks(sc); 148 if (error != 0) 149 return (error); 150 151 sc->intr_hook.ich_func = rk8xx_start; 152 sc->intr_hook.ich_arg = sc->dev; 153 if (config_intrhook_establish(&sc->intr_hook) != 0) 154 return (ENOMEM); 155 156 rk8xx_attach_regulators(sc); 157 158 if (OF_hasprop(ofw_bus_get_node(sc->dev), 159 "rockchip,system-power-controller")) { 160 /* 161 * The priority is chosen to override PSCI and EFI shutdown 162 * methods as those two just hang without powering off on Rock64 163 * at least. 164 */ 165 EVENTHANDLER_REGISTER(shutdown_final, rk8xx_poweroff, sc, 166 SHUTDOWN_PRI_LAST - 2); 167 } 168 169 return (0); 170 } 171 172 static int 173 rk8xx_detach(device_t dev) 174 { 175 176 /* We cannot detach regulators */ 177 return (EBUSY); 178 } 179 180 static device_method_t rk8xx_methods[] = { 181 DEVMETHOD(device_detach, rk8xx_detach), 182 183 /* regdev interface */ 184 DEVMETHOD(regdev_map, rk8xx_map), 185 186 /* Clock interface */ 187 DEVMETHOD(clock_gettime, rk8xx_gettime), 188 DEVMETHOD(clock_settime, rk8xx_settime), 189 190 DEVMETHOD_END 191 }; 192 193 DEFINE_CLASS_0(rk8xx, rk8xx_driver, rk8xx_methods, 194 sizeof(struct rk8xx_softc)); 195