xref: /netbsd-src/sys/arch/arm/nxp/imx6_ocotp.c (revision 90313c06e62e910bf0d1bb24faa9d17dcefd0ab6)
1*90313c06Smsaitoh /*	$NetBSD: imx6_ocotp.c,v 1.3 2024/02/07 04:20:27 msaitoh Exp $	*/
28644267aSskrll 
38644267aSskrll /*
4*90313c06Smsaitoh  * Copyright (c) 2014 Ryo Shimizu
58644267aSskrll  * All rights reserved.
68644267aSskrll  *
78644267aSskrll  * Redistribution and use in source and binary forms, with or without
88644267aSskrll  * modification, are permitted provided that the following conditions
98644267aSskrll  * are met:
108644267aSskrll  * 1. Redistributions of source code must retain the above copyright
118644267aSskrll  *    notice, this list of conditions and the following disclaimer.
128644267aSskrll  * 2. Redistributions in binary form must reproduce the above copyright
138644267aSskrll  *    notice, this list of conditions and the following disclaimer in the
148644267aSskrll  *    documentation and/or other materials provided with the distribution.
158644267aSskrll  *
168644267aSskrll  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
178644267aSskrll  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
188644267aSskrll  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
198644267aSskrll  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
208644267aSskrll  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
218644267aSskrll  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
228644267aSskrll  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
238644267aSskrll  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
248644267aSskrll  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
258644267aSskrll  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
268644267aSskrll  * POSSIBILITY OF SUCH DAMAGE.
278644267aSskrll  */
288644267aSskrll 
298644267aSskrll /*
308644267aSskrll  * i.MX6 On-Chip OTP Controller
318644267aSskrll  */
328644267aSskrll 
338644267aSskrll #include <sys/cdefs.h>
34*90313c06Smsaitoh __KERNEL_RCSID(0, "$NetBSD: imx6_ocotp.c,v 1.3 2024/02/07 04:20:27 msaitoh Exp $");
358644267aSskrll 
368644267aSskrll #include <sys/bus.h>
378644267aSskrll #include <sys/device.h>
388644267aSskrll #include <sys/param.h>
398644267aSskrll 
408644267aSskrll #include <dev/fdt/fdtvar.h>
418644267aSskrll 
428644267aSskrll #include <arm/nxp/imx6_ocotpreg.h>
438644267aSskrll #include <arm/nxp/imx6_ocotpvar.h>
448644267aSskrll 
458644267aSskrll struct imxocotp_softc {
468644267aSskrll 	device_t sc_dev;
478644267aSskrll 
488644267aSskrll 	bus_addr_t sc_addr;
498644267aSskrll 	bus_space_tag_t sc_iot;
508644267aSskrll 	bus_space_handle_t sc_ioh;
518644267aSskrll 	bus_size_t sc_ios;
528644267aSskrll 
538644267aSskrll 	struct clk *sc_clk;
548644267aSskrll 
558644267aSskrll };
568644267aSskrll 
578644267aSskrll static struct imxocotp_softc *ocotp_softc;
588644267aSskrll 
598644267aSskrll static int imxocotp_match(device_t, struct cfdata *, void *);
608644267aSskrll static void imxocotp_attach(device_t, device_t, void *);
618644267aSskrll 
628644267aSskrll CFATTACH_DECL_NEW(imxocotp, sizeof(struct imxocotp_softc),
638644267aSskrll     imxocotp_match, imxocotp_attach, NULL, NULL);
648644267aSskrll 
656e54367aSthorpej static const struct device_compatible_entry compat_data[] = {
666e54367aSthorpej 	{ .compat = "fsl,imx6q-ocotp" },
676e54367aSthorpej 	{ .compat = "fsl,imx6sl-ocotp" },
686e54367aSthorpej 	{ .compat = "fsl,imx6sll-ocotp" },
696e54367aSthorpej 	{ .compat = "fsl,imx6sx-ocotp" },
706e54367aSthorpej 	{ .compat = "fsl,imx6ul-ocotp" },
716e54367aSthorpej 	{ .compat = "fsl,imx6ull-ocotp" },
726e54367aSthorpej 	DEVICE_COMPAT_EOL
736e54367aSthorpej };
746e54367aSthorpej 
758644267aSskrll int
imxocotp_match(device_t parent,cfdata_t cf,void * aux)768644267aSskrll imxocotp_match(device_t parent, cfdata_t cf, void *aux)
778644267aSskrll {
788644267aSskrll 	struct fdt_attach_args * const faa = aux;
798644267aSskrll 
808644267aSskrll 	if (ocotp_softc != NULL)
818644267aSskrll 		return 0;
828644267aSskrll 
836e54367aSthorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
848644267aSskrll }
858644267aSskrll 
868644267aSskrll /* ARGSUSED */
878644267aSskrll static void
imxocotp_attach(device_t parent,device_t self,void * aux)888644267aSskrll imxocotp_attach(device_t parent, device_t self, void *aux)
898644267aSskrll {
908644267aSskrll 	struct imxocotp_softc *sc = device_private(self);
918644267aSskrll 	struct fdt_attach_args * const faa = aux;
928644267aSskrll 	const int phandle = faa->faa_phandle;
938644267aSskrll 	bus_addr_t addr;
948644267aSskrll 	bus_size_t size;
958644267aSskrll 	int error;
968644267aSskrll 
978644267aSskrll 	sc->sc_iot = faa->faa_bst;
988644267aSskrll 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
998644267aSskrll 		aprint_error(": couldn't get registers\n");
1008644267aSskrll 		return;
1018644267aSskrll 	}
1028644267aSskrll 
1038644267aSskrll 	sc->sc_clk = fdtbus_clock_get_index(phandle, 0);
1048644267aSskrll 	if (sc->sc_clk == NULL) {
1058644267aSskrll 		aprint_error(": couldn't get clock\n");
1068644267aSskrll 		return;
1078644267aSskrll 	}
1088644267aSskrll 
1098644267aSskrll 	error = clk_enable(sc->sc_clk);
1108644267aSskrll 	if (error) {
1118644267aSskrll 		aprint_error_dev(sc->sc_dev, "couldn't enable: %d\n", error);
1128644267aSskrll 		return;
1138644267aSskrll 	}
1148644267aSskrll 
1158644267aSskrll 	aprint_naive("\n");
1168644267aSskrll 	aprint_normal(": On-Chip OTP Controller\n");
1178644267aSskrll 	if (bus_space_map(sc->sc_iot, addr, size, 0, &sc->sc_ioh)) {
1188644267aSskrll 		aprint_error_dev(self, "Cannot map registers\n");
1198644267aSskrll 		return;
1208644267aSskrll 	}
1218644267aSskrll 	ocotp_softc = sc;
1228644267aSskrll 	sc->sc_ios = size;
1238644267aSskrll 
1248644267aSskrll 	uint32_t v = imxocotp_read(OCOTP_VERSION);
1258644267aSskrll 	aprint_normal_dev(self, "OCOTP_VERSION %d.%d.%d\n", (v >> 24) & 0xff, (v >> 16) & 0xff, v & 0xffff);
1268644267aSskrll 
1278644267aSskrll 	return;
1288644267aSskrll }
1298644267aSskrll 
1308644267aSskrll uint32_t
imxocotp_read(uint32_t addr)1318644267aSskrll imxocotp_read(uint32_t addr)
1328644267aSskrll {
1338644267aSskrll 	struct imxocotp_softc *sc = ocotp_softc;
1348644267aSskrll 
1358644267aSskrll 	if (sc == NULL)
1368644267aSskrll 		return 0;
1378644267aSskrll 
1388644267aSskrll 	if (addr > sc->sc_ios)
1398644267aSskrll 		return 0;
1408644267aSskrll 
1418644267aSskrll 	return bus_space_read_4(sc->sc_iot, sc->sc_ioh, addr);
1428644267aSskrll }
143