2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2022 Soren Schmidt <sos@deepcore.dk>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/module.h>
33 #include <sys/mutex.h>
35 #include <machine/bus.h>
37 #include <dev/ofw/openfirm.h>
38 #include <dev/ofw/ofw_bus.h>
39 #include <dev/ofw/ofw_bus_subr.h>
41 #include <dev/extres/syscon/syscon.h>
42 #include <dev/fdt/simple_mfd.h>
45 #include "rk_otp_if.h"
47 #define OTPC_SBPI_CTRL 0x0020
48 #define SBPI_ENABLE_MASK 0x00010000
50 #define SBPI_DAP_ADDR_MASK 0xff000000
51 #define SBPI_DAP_ADDR 0x02
52 #define SBPI_DAP_ADDR_SHIFT 8
53 #define OTPC_SBPI_CMD_VALID_PRE 0x0024
54 #define SBPI_CMD_VALID_MASK 0xffff0000
55 #define OTPC_SBPI_INT_STATUS 0x0304
56 #define OTPC_SBPI_DONE 2
57 #define OTPC_USER_DONE 4
58 #define OTPC_USER_CTRL 0x0100
59 #define OTPC_USER_MASK 0xffff0000
61 #define OTPC_USER_ADDR 0x0104
62 #define OTPC_USER_ADDR_MASK 0xffff0000
63 #define OTPC_USER_ENABLE 0x0108
64 #define OTPC_USER_FSM_ENABLE_MASK 0xffff0000
65 #define OTPC_USER_FSM_ENABLE 1
66 #define OTPC_USER_Q 0x0124
67 #define OTPC_SBPI_CMD0_OFFSET 0x1000
68 #define SBPI_DAP_CMD_WRF 0xc0
69 #define SBPI_DAP_REG_ECC 0x3a
70 #define OTPC_SBPI_CMD1_OFFSET 0x1004
71 #define SBPI_ECC_ENABLE 0x00
72 #define SBPI_ECC_DISABLE 0x09
75 static struct ofw_compat_data compat_data[] = {
76 {"rockchip,rk3568-otp", 1},
80 static struct rk_otp_softc {
86 rk_otp_wait(struct rk_otp_softc *sc, uint32_t status)
90 while (!(bus_read_4(sc->mem, OTPC_SBPI_INT_STATUS) & status)) {
97 bus_write_4(sc->mem, OTPC_SBPI_INT_STATUS, status);
103 rk_otp_ecc(struct rk_otp_softc *sc, int enable)
105 bus_write_4(sc->mem, OTPC_SBPI_CTRL,
106 SBPI_DAP_ADDR_MASK | (SBPI_DAP_ADDR << SBPI_DAP_ADDR_SHIFT));
107 bus_write_4(sc->mem, OTPC_SBPI_CMD_VALID_PRE,
108 SBPI_CMD_VALID_MASK | 0x1);
109 bus_write_4(sc->mem, OTPC_SBPI_CMD0_OFFSET,
110 SBPI_DAP_CMD_WRF | SBPI_DAP_REG_ECC);
112 bus_write_4(sc->mem, OTPC_SBPI_CMD1_OFFSET, SBPI_ECC_ENABLE);
114 bus_write_4(sc->mem, OTPC_SBPI_CMD1_OFFSET, SBPI_ECC_DISABLE);
115 bus_write_4(sc->mem, OTPC_SBPI_CTRL, SBPI_ENABLE_MASK | SBPI_ENABLE);
117 return (rk_otp_wait(sc, OTPC_SBPI_DONE));
121 rk_otp_read(device_t dev, uint8_t *buffer, int offset, int size)
123 struct rk_otp_softc *sc = &rk_otp_sc;
126 /* if not initialized just error out */
130 if ((error = rk_otp_ecc(sc, 1))) {
131 device_printf(dev, "timeout waiting for OTP ECC status\n");
135 bus_write_4(sc->mem, OTPC_USER_CTRL, OTPC_USER | OTPC_USER_MASK);
138 bus_write_4(sc->mem, OTPC_USER_ADDR,
139 offset++ | OTPC_USER_ADDR_MASK);
140 bus_write_4(sc->mem, OTPC_USER_ENABLE,
141 OTPC_USER_FSM_ENABLE | OTPC_USER_FSM_ENABLE_MASK);
143 if ((error = rk_otp_wait(sc, OTPC_USER_DONE))) {
144 device_printf(dev, "timeout waiting for OTP data\n");
147 *buffer++ = bus_read_4(sc->mem, OTPC_USER_Q);
149 bus_write_4(sc->mem, OTPC_USER_CTRL, OTPC_USER_MASK);
155 rk_otp_probe(device_t dev)
158 if (!ofw_bus_status_okay(dev))
160 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
162 device_set_desc(dev, "RockChip OTP");
163 return (BUS_PROBE_DEFAULT);
167 rk_otp_attach(device_t dev)
169 struct rk_otp_softc *sc = &rk_otp_sc;
172 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
174 device_printf(dev, "Cannot allocate memory resources\n");
181 static device_method_t rk_otp_methods[] = {
182 /* Device interface */
183 DEVMETHOD(device_probe, rk_otp_probe),
184 DEVMETHOD(device_attach, rk_otp_attach),
189 DEFINE_CLASS_1(rk_otp, rk_otp_driver, rk_otp_methods,
190 sizeof(struct simple_mfd_softc), simple_mfd_driver);
191 EARLY_DRIVER_MODULE(rk_otp, simplebus, rk_otp_driver, 0, 0,
192 BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);