xref: /openbsd/sys/dev/fdt/amlsm.c (revision d89ec533)
1 /*	$OpenBSD: amlsm.c,v 1.3 2021/10/24 17:52:26 mpi Exp $	*/
2 /*
3  * Copyright (c) 2020 Mark Kettenis <kettenis@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <sys/param.h>
19 #include <sys/systm.h>
20 #include <sys/device.h>
21 #include <sys/timeout.h>
22 
23 #include <machine/bus.h>
24 #include <machine/fdt.h>
25 
26 #include <dev/ofw/openfirm.h>
27 #include <dev/ofw/fdt.h>
28 #include <dev/ofw/ofw_misc.h>
29 
30 extern register_t smc_call(register_t, register_t, register_t, register_t);
31 
32 /* Calls */
33 #define AML_SM_GET_SHARE_MEM_INPUT_BASE		0x82000020
34 #define AML_SM_GET_SHARE_MEM_OUTPUT_BASE	0x82000021
35 #define AML_SM_EFUSE_READ			0x82000030
36 #define AML_SM_EFUSE_USER_MAX			0x82000033
37 #define AML_SM_JTAG_ON				0x82000040
38 #define AML_SM_JTAG_OFF				0x82000041
39 #define AML_SM_GET_CHIP_ID			0x82000044
40 
41 struct aml_cpu_info {
42 	uint32_t	version;
43 	uint32_t	chip_id[4];
44 };
45 
46 struct amlsm_softc {
47 	struct device		sc_dev;
48 	bus_space_tag_t		sc_iot;
49 	bus_space_handle_t	sc_in_ioh;
50 	bus_space_handle_t	sc_out_ioh;
51 };
52 
53 int	amlsm_match(struct device *, void *, void *);
54 void	amlsm_attach(struct device *, struct device *, void *);
55 
56 const struct cfattach	amlsm_ca = {
57 	sizeof (struct amlsm_softc), amlsm_match, amlsm_attach
58 };
59 
60 struct cfdriver amlsm_cd = {
61 	NULL, "amlsm", DV_DULL
62 };
63 
64 int
65 amlsm_match(struct device *parent, void *match, void *aux)
66 {
67 	struct fdt_attach_args *faa = aux;
68 
69 	return OF_is_compatible(faa->fa_node, "amlogic,meson-gxbb-sm");
70 }
71 
72 void
73 amlsm_attach(struct device *parent, struct device *self, void *aux)
74 {
75 	struct amlsm_softc *sc = (struct amlsm_softc *)self;
76 	struct fdt_attach_args *faa = aux;
77 	struct aml_cpu_info *info;
78 	bus_addr_t addr;
79 	int32_t ret;
80 	int i;
81 
82 	sc->sc_iot = faa->fa_iot;
83 
84 	addr = smc_call(AML_SM_GET_SHARE_MEM_INPUT_BASE, 0, 0, 0);
85 	if (addr == (bus_addr_t)-1 ||
86 	    bus_space_map(sc->sc_iot, addr, PAGE_SIZE,
87 	    BUS_SPACE_MAP_CACHEABLE, &sc->sc_in_ioh)) {
88 		printf(": can't map shared memory\n");
89 		return;
90 	}
91 	addr = smc_call(AML_SM_GET_SHARE_MEM_OUTPUT_BASE, 0, 0, 0);
92 	if (addr == (bus_addr_t)-1 ||
93 	    bus_space_map(sc->sc_iot, addr, PAGE_SIZE,
94 	    BUS_SPACE_MAP_CACHEABLE, &sc->sc_out_ioh)) {
95 		bus_space_unmap(sc->sc_iot, sc->sc_in_ioh, PAGE_SIZE);
96 		printf(": can't map shared memory\n");
97 		return;
98 	}
99 
100 	/*
101 	 * Version 2 gives us a 16-byte chip ID.  If that fails, fall
102 	 * back on version 0/1 which gives us a 12-byte chipt ID.
103 	 */
104 	info = bus_space_vaddr(sc->sc_iot, sc->sc_out_ioh);
105 	memset(info, 0, sizeof(struct aml_cpu_info));
106 	ret = smc_call(AML_SM_GET_CHIP_ID, 2, 0, 0);
107 	if (ret == -1)
108 		ret = smc_call(AML_SM_GET_CHIP_ID, 0, 0, 0);
109 
110 	printf(": ver %u\n", info->version);
111 
112 	if (ret != -1) {
113 		for (i = 0; i < nitems(info->chip_id); i++)
114 			enqueue_randomness(info->chip_id[i]);
115 	}
116 }
117