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/kernel.h> 35 #include <sys/module.h> 36 #include <sys/mutex.h> 37 #include <sys/rman.h> 38 #include <machine/bus.h> 39 40 #include <dev/iicbus/iiconf.h> 41 #include <dev/iicbus/iicbus.h> 42 43 #include <dev/ofw/ofw_bus.h> 44 #include <dev/ofw/ofw_bus_subr.h> 45 46 #include <dev/iicbus/pmic/rockchip/rk805reg.h> 47 #include <dev/iicbus/pmic/rockchip/rk808reg.h> 48 #include <dev/iicbus/pmic/rockchip/rk8xx.h> 49 50 #include "clock_if.h" 51 #include "regdev_if.h" 52 53 int 54 rk8xx_read(device_t dev, uint8_t reg, uint8_t *data, uint8_t size) 55 { 56 int err; 57 58 err = iicdev_readfrom(dev, reg, data, size, IIC_INTRWAIT); 59 return (err); 60 } 61 62 int 63 rk8xx_write(device_t dev, uint8_t reg, uint8_t *data, uint8_t size) 64 { 65 66 return (iicdev_writeto(dev, reg, data, size, IIC_INTRWAIT)); 67 } 68 69 static void 70 rk8xx_start(void *pdev) 71 { 72 struct rk8xx_softc *sc; 73 device_t dev; 74 uint8_t data[2]; 75 int err; 76 77 dev = pdev; 78 sc = device_get_softc(dev); 79 sc->dev = dev; 80 81 /* No version register in RK808 */ 82 if (bootverbose && sc->type == RK805) { 83 err = rk8xx_read(dev, RK805_CHIP_NAME, data, 1); 84 if (err != 0) { 85 device_printf(dev, "Cannot read chip name reg\n"); 86 return; 87 } 88 err = rk8xx_read(dev, RK805_CHIP_VER, data + 1, 1); 89 if (err != 0) { 90 device_printf(dev, "Cannot read chip version reg\n"); 91 return; 92 } 93 device_printf(dev, "Chip Name: %x\n", 94 data[0] << 4 | ((data[1] >> 4) & 0xf)); 95 device_printf(dev, "Chip Version: %x\n", data[1] & 0xf); 96 } 97 98 /* Register this as a 1Hz clock */ 99 clock_register(dev, 1000000); 100 101 config_intrhook_disestablish(&sc->intr_hook); 102 } 103 104 int 105 rk8xx_attach(struct rk8xx_softc *sc) 106 { 107 int error; 108 109 error = rk8xx_attach_clocks(sc); 110 if (error != 0) 111 return (error); 112 113 sc->intr_hook.ich_func = rk8xx_start; 114 sc->intr_hook.ich_arg = sc->dev; 115 if (config_intrhook_establish(&sc->intr_hook) != 0) 116 return (ENOMEM); 117 118 rk8xx_attach_regulators(sc); 119 120 return (0); 121 } 122 123 static int 124 rk8xx_detach(device_t dev) 125 { 126 127 /* We cannot detach regulators */ 128 return (EBUSY); 129 } 130 131 static device_method_t rk8xx_methods[] = { 132 DEVMETHOD(device_detach, rk8xx_detach), 133 134 /* regdev interface */ 135 DEVMETHOD(regdev_map, rk8xx_map), 136 137 /* Clock interface */ 138 DEVMETHOD(clock_gettime, rk8xx_gettime), 139 DEVMETHOD(clock_settime, rk8xx_settime), 140 141 DEVMETHOD_END 142 }; 143 144 DEFINE_CLASS_0(rk8xx, rk8xx_driver, rk8xx_methods, 145 sizeof(struct rk8xx_softc)); 146