1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
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 "opt_platform.h"
29
30 #include <sys/param.h>
31 #include <sys/bus.h>
32 #include <sys/clock.h>
33 #include <sys/eventhandler.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/reboot.h>
37 #include <sys/mutex.h>
38 #include <sys/rman.h>
39 #include <machine/bus.h>
40
41 #include <dev/iicbus/iiconf.h>
42 #include <dev/iicbus/iicbus.h>
43
44 #include <dev/ofw/ofw_bus.h>
45 #include <dev/ofw/ofw_bus_subr.h>
46
47 #include <dev/iicbus/pmic/rockchip/rk805reg.h>
48 #include <dev/iicbus/pmic/rockchip/rk808reg.h>
49 #include <dev/iicbus/pmic/rockchip/rk8xx.h>
50
51 #include "clock_if.h"
52 #include "regdev_if.h"
53
54 int
rk8xx_read(device_t dev,uint8_t reg,uint8_t * data,uint8_t size)55 rk8xx_read(device_t dev, uint8_t reg, uint8_t *data, uint8_t size)
56 {
57 int err;
58
59 err = iicdev_readfrom(dev, reg, data, size, IIC_INTRWAIT);
60 return (err);
61 }
62
63 int
rk8xx_write(device_t dev,uint8_t reg,uint8_t * data,uint8_t size)64 rk8xx_write(device_t dev, uint8_t reg, uint8_t *data, uint8_t size)
65 {
66
67 return (iicdev_writeto(dev, reg, data, size, IIC_INTRWAIT));
68 }
69
70 static void
rk8xx_start(void * pdev)71 rk8xx_start(void *pdev)
72 {
73 struct rk8xx_softc *sc;
74 device_t dev;
75 uint8_t data[2];
76 int err;
77
78 dev = pdev;
79 sc = device_get_softc(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 static void
rk8xx_poweroff(void * arg,int howto)105 rk8xx_poweroff(void *arg, int howto)
106 {
107 struct rk8xx_softc *sc = arg;
108 int error;
109 uint8_t val;
110
111 if ((howto & (RB_POWEROFF | RB_POWERCYCLE)) == 0)
112 return;
113
114 device_printf(sc->dev, "Powering off...\n");
115 error = rk8xx_read(sc->dev, sc->dev_ctrl.dev_ctrl_reg, &val, 1);
116 if (error == 0) {
117 if (howto & RB_POWEROFF)
118 val |= sc->dev_ctrl.pwr_off_mask;
119 else if (howto & RB_POWERCYCLE) {
120 if (sc->type == RK809 || sc->type == RK817) {
121 if (bootverbose) {
122 device_printf(sc->dev,
123 "Powercycle PMIC\n");
124 }
125 val |= sc->dev_ctrl.pwr_rst_mask;
126 } else {
127 /* Poweroff PMIC that can't powercycle */
128 val |= sc->dev_ctrl.pwr_off_mask;
129 }
130 }
131 error = rk8xx_write(sc->dev, sc->dev_ctrl.dev_ctrl_reg,
132 &val, 1);
133
134 /* Wait a bit for the command to take effect. */
135 if (error == 0)
136 DELAY(100);
137 }
138 device_printf(sc->dev, "Power off failed\n");
139 }
140
141 int
rk8xx_attach(struct rk8xx_softc * sc)142 rk8xx_attach(struct rk8xx_softc *sc)
143 {
144 int error;
145
146 error = rk8xx_attach_clocks(sc);
147 if (error != 0)
148 return (error);
149
150 sc->intr_hook.ich_func = rk8xx_start;
151 sc->intr_hook.ich_arg = sc->dev;
152 if (config_intrhook_establish(&sc->intr_hook) != 0)
153 return (ENOMEM);
154
155 rk8xx_attach_regulators(sc);
156
157 if (OF_hasprop(ofw_bus_get_node(sc->dev),
158 "rockchip,system-power-controller")) {
159 /*
160 * The priority is chosen to override PSCI and EFI shutdown
161 * methods as those two just hang without powering off on Rock64
162 * at least.
163 */
164 EVENTHANDLER_REGISTER(shutdown_final, rk8xx_poweroff, sc,
165 SHUTDOWN_PRI_LAST - 2);
166 }
167
168 return (0);
169 }
170
171 static int
rk8xx_detach(device_t dev)172 rk8xx_detach(device_t dev)
173 {
174
175 /* We cannot detach regulators */
176 return (EBUSY);
177 }
178
179 static device_method_t rk8xx_methods[] = {
180 DEVMETHOD(device_detach, rk8xx_detach),
181
182 /* regdev interface */
183 DEVMETHOD(regdev_map, rk8xx_map),
184
185 /* Clock interface */
186 DEVMETHOD(clock_gettime, rk8xx_gettime),
187 DEVMETHOD(clock_settime, rk8xx_settime),
188
189 DEVMETHOD_END
190 };
191
192 DEFINE_CLASS_0(rk8xx, rk8xx_driver, rk8xx_methods,
193 sizeof(struct rk8xx_softc));
194