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 val |= sc->dev_ctrl.pwr_off_mask; 119 error = rk8xx_write(sc->dev, sc->dev_ctrl.dev_ctrl_reg, 120 &val, 1); 121 122 /* Wait a bit for the command to take effect. */ 123 if (error == 0) 124 DELAY(100); 125 } 126 device_printf(sc->dev, "Power off failed\n"); 127 } 128 129 int 130 rk8xx_attach(struct rk8xx_softc *sc) 131 { 132 int error; 133 134 error = rk8xx_attach_clocks(sc); 135 if (error != 0) 136 return (error); 137 138 sc->intr_hook.ich_func = rk8xx_start; 139 sc->intr_hook.ich_arg = sc->dev; 140 if (config_intrhook_establish(&sc->intr_hook) != 0) 141 return (ENOMEM); 142 143 rk8xx_attach_regulators(sc); 144 145 if (OF_hasprop(ofw_bus_get_node(sc->dev), 146 "rockchip,system-power-controller")) { 147 /* 148 * The priority is chosen to override PSCI and EFI shutdown 149 * methods as those two just hang without powering off on Rock64 150 * at least. 151 */ 152 EVENTHANDLER_REGISTER(shutdown_final, rk8xx_poweroff, sc, 153 SHUTDOWN_PRI_LAST - 2); 154 } 155 156 return (0); 157 } 158 159 static int 160 rk8xx_detach(device_t dev) 161 { 162 163 /* We cannot detach regulators */ 164 return (EBUSY); 165 } 166 167 static device_method_t rk8xx_methods[] = { 168 DEVMETHOD(device_detach, rk8xx_detach), 169 170 /* regdev interface */ 171 DEVMETHOD(regdev_map, rk8xx_map), 172 173 /* Clock interface */ 174 DEVMETHOD(clock_gettime, rk8xx_gettime), 175 DEVMETHOD(clock_settime, rk8xx_settime), 176 177 DEVMETHOD_END 178 }; 179 180 DEFINE_CLASS_0(rk8xx, rk8xx_driver, rk8xx_methods, 181 sizeof(struct rk8xx_softc)); 182