1896d3e43SSøren Schmidt /*-
290737b63SGanbold Tsagaankhuu * SPDX-License-Identifier: BSD-2-Clause
37e2600eaSGanbold Tsagaankhuu *
4896d3e43SSøren Schmidt * Copyright (c) 2022 Soren Schmidt <sos@deepcore.dk>
5896d3e43SSøren Schmidt *
6896d3e43SSøren Schmidt * Redistribution and use in source and binary forms, with or without
7896d3e43SSøren Schmidt * modification, are permitted provided that the following conditions
8896d3e43SSøren Schmidt * are met:
9896d3e43SSøren Schmidt * 1. Redistributions of source code must retain the above copyright
10896d3e43SSøren Schmidt * notice, this list of conditions and the following disclaimer.
11896d3e43SSøren Schmidt * 2. Redistributions in binary form must reproduce the above copyright
12896d3e43SSøren Schmidt * notice, this list of conditions and the following disclaimer in the
13896d3e43SSøren Schmidt * documentation and/or other materials provided with the distribution.
14896d3e43SSøren Schmidt *
15896d3e43SSøren Schmidt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16896d3e43SSøren Schmidt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17896d3e43SSøren Schmidt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18896d3e43SSøren Schmidt * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19896d3e43SSøren Schmidt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20896d3e43SSøren Schmidt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21896d3e43SSøren Schmidt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22896d3e43SSøren Schmidt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23896d3e43SSøren Schmidt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24896d3e43SSøren Schmidt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25896d3e43SSøren Schmidt * SUCH DAMAGE.
26896d3e43SSøren Schmidt */
27896d3e43SSøren Schmidt
28896d3e43SSøren Schmidt #include <sys/param.h>
29896d3e43SSøren Schmidt #include <sys/bus.h>
30896d3e43SSøren Schmidt #include <sys/kernel.h>
31896d3e43SSøren Schmidt #include <sys/module.h>
32896d3e43SSøren Schmidt #include <sys/mutex.h>
33896d3e43SSøren Schmidt #include <sys/rman.h>
34896d3e43SSøren Schmidt #include <machine/bus.h>
35896d3e43SSøren Schmidt
36896d3e43SSøren Schmidt #include <dev/ofw/openfirm.h>
37896d3e43SSøren Schmidt #include <dev/ofw/ofw_bus.h>
38896d3e43SSøren Schmidt #include <dev/ofw/ofw_bus_subr.h>
39896d3e43SSøren Schmidt
40*62e8ccc3SEmmanuel Vadot #include <dev/syscon/syscon.h>
41896d3e43SSøren Schmidt #include <dev/fdt/simple_mfd.h>
42896d3e43SSøren Schmidt
43896d3e43SSøren Schmidt #include "rk_otp.h"
44896d3e43SSøren Schmidt #include "rk_otp_if.h"
45896d3e43SSøren Schmidt
46896d3e43SSøren Schmidt #define OTPC_SBPI_CTRL 0x0020
47896d3e43SSøren Schmidt #define SBPI_ENABLE_MASK 0x00010000
48896d3e43SSøren Schmidt #define SBPI_ENABLE 1
49896d3e43SSøren Schmidt #define SBPI_DAP_ADDR_MASK 0xff000000
50896d3e43SSøren Schmidt #define SBPI_DAP_ADDR 0x02
51896d3e43SSøren Schmidt #define SBPI_DAP_ADDR_SHIFT 8
52896d3e43SSøren Schmidt #define OTPC_SBPI_CMD_VALID_PRE 0x0024
53896d3e43SSøren Schmidt #define SBPI_CMD_VALID_MASK 0xffff0000
54896d3e43SSøren Schmidt #define OTPC_SBPI_INT_STATUS 0x0304
55896d3e43SSøren Schmidt #define OTPC_SBPI_DONE 2
56896d3e43SSøren Schmidt #define OTPC_USER_DONE 4
57896d3e43SSøren Schmidt #define OTPC_USER_CTRL 0x0100
58896d3e43SSøren Schmidt #define OTPC_USER_MASK 0xffff0000
59896d3e43SSøren Schmidt #define OTPC_USER 1
60896d3e43SSøren Schmidt #define OTPC_USER_ADDR 0x0104
61896d3e43SSøren Schmidt #define OTPC_USER_ADDR_MASK 0xffff0000
62896d3e43SSøren Schmidt #define OTPC_USER_ENABLE 0x0108
63896d3e43SSøren Schmidt #define OTPC_USER_FSM_ENABLE_MASK 0xffff0000
64896d3e43SSøren Schmidt #define OTPC_USER_FSM_ENABLE 1
65896d3e43SSøren Schmidt #define OTPC_USER_Q 0x0124
66896d3e43SSøren Schmidt #define OTPC_SBPI_CMD0_OFFSET 0x1000
67896d3e43SSøren Schmidt #define SBPI_DAP_CMD_WRF 0xc0
68896d3e43SSøren Schmidt #define SBPI_DAP_REG_ECC 0x3a
69896d3e43SSøren Schmidt #define OTPC_SBPI_CMD1_OFFSET 0x1004
70896d3e43SSøren Schmidt #define SBPI_ECC_ENABLE 0x00
71896d3e43SSøren Schmidt #define SBPI_ECC_DISABLE 0x09
72896d3e43SSøren Schmidt
73896d3e43SSøren Schmidt
74896d3e43SSøren Schmidt static struct ofw_compat_data compat_data[] = {
75896d3e43SSøren Schmidt {"rockchip,rk3568-otp", 1},
76896d3e43SSøren Schmidt {NULL, 0}
77896d3e43SSøren Schmidt };
78896d3e43SSøren Schmidt
79896d3e43SSøren Schmidt static struct rk_otp_softc {
80896d3e43SSøren Schmidt struct resource *mem;
81896d3e43SSøren Schmidt } rk_otp_sc;
82896d3e43SSøren Schmidt
83896d3e43SSøren Schmidt
84896d3e43SSøren Schmidt static int
rk_otp_wait(struct rk_otp_softc * sc,uint32_t status)85896d3e43SSøren Schmidt rk_otp_wait(struct rk_otp_softc *sc, uint32_t status)
86896d3e43SSøren Schmidt {
87896d3e43SSøren Schmidt int retry = 10000;
88896d3e43SSøren Schmidt
89896d3e43SSøren Schmidt while (!(bus_read_4(sc->mem, OTPC_SBPI_INT_STATUS) & status)) {
90896d3e43SSøren Schmidt DELAY(10);
91896d3e43SSøren Schmidt if (--retry == 0)
92896d3e43SSøren Schmidt return (ETIMEDOUT);
93896d3e43SSøren Schmidt }
94896d3e43SSøren Schmidt
95896d3e43SSøren Schmidt /* clear status */
96896d3e43SSøren Schmidt bus_write_4(sc->mem, OTPC_SBPI_INT_STATUS, status);
97896d3e43SSøren Schmidt
98896d3e43SSøren Schmidt return (0);
99896d3e43SSøren Schmidt }
100896d3e43SSøren Schmidt
101896d3e43SSøren Schmidt static int
rk_otp_ecc(struct rk_otp_softc * sc,int enable)102896d3e43SSøren Schmidt rk_otp_ecc(struct rk_otp_softc *sc, int enable)
103896d3e43SSøren Schmidt {
104896d3e43SSøren Schmidt bus_write_4(sc->mem, OTPC_SBPI_CTRL,
105896d3e43SSøren Schmidt SBPI_DAP_ADDR_MASK | (SBPI_DAP_ADDR << SBPI_DAP_ADDR_SHIFT));
106896d3e43SSøren Schmidt bus_write_4(sc->mem, OTPC_SBPI_CMD_VALID_PRE,
107896d3e43SSøren Schmidt SBPI_CMD_VALID_MASK | 0x1);
108896d3e43SSøren Schmidt bus_write_4(sc->mem, OTPC_SBPI_CMD0_OFFSET,
109896d3e43SSøren Schmidt SBPI_DAP_CMD_WRF | SBPI_DAP_REG_ECC);
110896d3e43SSøren Schmidt if (enable)
111896d3e43SSøren Schmidt bus_write_4(sc->mem, OTPC_SBPI_CMD1_OFFSET, SBPI_ECC_ENABLE);
112896d3e43SSøren Schmidt else
113896d3e43SSøren Schmidt bus_write_4(sc->mem, OTPC_SBPI_CMD1_OFFSET, SBPI_ECC_DISABLE);
114896d3e43SSøren Schmidt bus_write_4(sc->mem, OTPC_SBPI_CTRL, SBPI_ENABLE_MASK | SBPI_ENABLE);
115896d3e43SSøren Schmidt
116896d3e43SSøren Schmidt return (rk_otp_wait(sc, OTPC_SBPI_DONE));
117896d3e43SSøren Schmidt }
118896d3e43SSøren Schmidt
119896d3e43SSøren Schmidt int
rk_otp_read(device_t dev,uint8_t * buffer,int offset,int size)120896d3e43SSøren Schmidt rk_otp_read(device_t dev, uint8_t *buffer, int offset, int size)
121896d3e43SSøren Schmidt {
122896d3e43SSøren Schmidt struct rk_otp_softc *sc = &rk_otp_sc;
123896d3e43SSøren Schmidt int error;
124896d3e43SSøren Schmidt
125896d3e43SSøren Schmidt /* if not initialized just error out */
126896d3e43SSøren Schmidt if (!sc->mem)
127896d3e43SSøren Schmidt return (ENXIO);
128896d3e43SSøren Schmidt
129896d3e43SSøren Schmidt if ((error = rk_otp_ecc(sc, 1))) {
130896d3e43SSøren Schmidt device_printf(dev, "timeout waiting for OTP ECC status\n");
131896d3e43SSøren Schmidt return (error);
132896d3e43SSøren Schmidt }
133896d3e43SSøren Schmidt
134896d3e43SSøren Schmidt bus_write_4(sc->mem, OTPC_USER_CTRL, OTPC_USER | OTPC_USER_MASK);
135896d3e43SSøren Schmidt DELAY(5);
136896d3e43SSøren Schmidt while (size--) {
137896d3e43SSøren Schmidt bus_write_4(sc->mem, OTPC_USER_ADDR,
138896d3e43SSøren Schmidt offset++ | OTPC_USER_ADDR_MASK);
139896d3e43SSøren Schmidt bus_write_4(sc->mem, OTPC_USER_ENABLE,
140896d3e43SSøren Schmidt OTPC_USER_FSM_ENABLE | OTPC_USER_FSM_ENABLE_MASK);
141896d3e43SSøren Schmidt
142896d3e43SSøren Schmidt if ((error = rk_otp_wait(sc, OTPC_USER_DONE))) {
143896d3e43SSøren Schmidt device_printf(dev, "timeout waiting for OTP data\n");
144896d3e43SSøren Schmidt break;
145896d3e43SSøren Schmidt }
146896d3e43SSøren Schmidt *buffer++ = bus_read_4(sc->mem, OTPC_USER_Q);
147896d3e43SSøren Schmidt }
148896d3e43SSøren Schmidt bus_write_4(sc->mem, OTPC_USER_CTRL, OTPC_USER_MASK);
149896d3e43SSøren Schmidt
150896d3e43SSøren Schmidt return (error);
151896d3e43SSøren Schmidt }
152896d3e43SSøren Schmidt
153896d3e43SSøren Schmidt static int
rk_otp_probe(device_t dev)154896d3e43SSøren Schmidt rk_otp_probe(device_t dev)
155896d3e43SSøren Schmidt {
156896d3e43SSøren Schmidt
157896d3e43SSøren Schmidt if (!ofw_bus_status_okay(dev))
158896d3e43SSøren Schmidt return (ENXIO);
159896d3e43SSøren Schmidt if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
160896d3e43SSøren Schmidt return (ENXIO);
161896d3e43SSøren Schmidt device_set_desc(dev, "RockChip OTP");
162896d3e43SSøren Schmidt return (BUS_PROBE_DEFAULT);
163896d3e43SSøren Schmidt }
164896d3e43SSøren Schmidt
165896d3e43SSøren Schmidt static int
rk_otp_attach(device_t dev)166896d3e43SSøren Schmidt rk_otp_attach(device_t dev)
167896d3e43SSøren Schmidt {
168896d3e43SSøren Schmidt struct rk_otp_softc *sc = &rk_otp_sc;
169896d3e43SSøren Schmidt int rid = 0;
170896d3e43SSøren Schmidt
171896d3e43SSøren Schmidt sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
172896d3e43SSøren Schmidt if (!sc->mem) {
173896d3e43SSøren Schmidt device_printf(dev, "Cannot allocate memory resources\n");
174896d3e43SSøren Schmidt return (ENXIO);
175896d3e43SSøren Schmidt }
176896d3e43SSøren Schmidt return (0);
177896d3e43SSøren Schmidt }
178896d3e43SSøren Schmidt
179896d3e43SSøren Schmidt
180896d3e43SSøren Schmidt static device_method_t rk_otp_methods[] = {
181896d3e43SSøren Schmidt /* Device interface */
182896d3e43SSøren Schmidt DEVMETHOD(device_probe, rk_otp_probe),
183896d3e43SSøren Schmidt DEVMETHOD(device_attach, rk_otp_attach),
184896d3e43SSøren Schmidt
185896d3e43SSøren Schmidt DEVMETHOD_END
186896d3e43SSøren Schmidt };
187896d3e43SSøren Schmidt
188896d3e43SSøren Schmidt DEFINE_CLASS_1(rk_otp, rk_otp_driver, rk_otp_methods,
189896d3e43SSøren Schmidt sizeof(struct simple_mfd_softc), simple_mfd_driver);
190896d3e43SSøren Schmidt EARLY_DRIVER_MODULE(rk_otp, simplebus, rk_otp_driver, 0, 0,
191896d3e43SSøren Schmidt BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
192