amlsm.c revision 1.1
1/*	$OpenBSD: amlsm.c,v 1.1 2020/01/14 20:43:20 kettenis 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#include <dev/rndvar.h>
30
31extern register_t smc_call(register_t, register_t, register_t, register_t);
32
33/* Calls */
34#define AML_SM_GET_SHARE_MEM_INPUT_BASE		0x82000020
35#define AML_SM_GET_SHARE_MEM_OUTPUT_BASE	0x82000021
36#define AML_SM_EFUSE_READ			0x82000030
37#define AML_SM_EFUSE_USER_MAX			0x82000033
38#define AML_SM_JTAG_ON				0x82000040
39#define AML_SM_JTAG_OFF				0x82000041
40#define AML_SM_GET_CHIP_ID			0x82000044
41
42struct aml_cpu_info {
43	uint32_t	version;
44	uint32_t	chip_id[4];
45};
46
47struct amlsm_softc {
48	struct device		sc_dev;
49	bus_space_tag_t		sc_iot;
50	bus_space_handle_t	sc_in_ioh;
51	bus_space_handle_t	sc_out_ioh;
52};
53
54int	amlsm_match(struct device *, void *, void *);
55void	amlsm_attach(struct device *, struct device *, void *);
56
57struct cfattach	amlsm_ca = {
58	sizeof (struct amlsm_softc), amlsm_match, amlsm_attach
59};
60
61struct cfdriver amlsm_cd = {
62	NULL, "amlsm", DV_DULL
63};
64
65int
66amlsm_match(struct device *parent, void *match, void *aux)
67{
68	struct fdt_attach_args *faa = aux;
69
70	return OF_is_compatible(faa->fa_node, "amlogic,meson-gxbb-sm");
71}
72
73void
74amlsm_attach(struct device *parent, struct device *self, void *aux)
75{
76	struct amlsm_softc *sc = (struct amlsm_softc *)self;
77	struct fdt_attach_args *faa = aux;
78	struct aml_cpu_info *info;
79	bus_addr_t addr;
80	int32_t ret;
81	int i;
82
83	sc->sc_iot = faa->fa_iot;
84
85	addr = smc_call(AML_SM_GET_SHARE_MEM_INPUT_BASE, 0, 0, 0);
86	if (addr == (bus_addr_t)-1 ||
87	    bus_space_map(sc->sc_iot, addr, PAGE_SIZE,
88	    BUS_SPACE_MAP_CACHEABLE, &sc->sc_in_ioh)) {
89		printf(": can't map shared memory\n");
90		return;
91	}
92	addr = smc_call(AML_SM_GET_SHARE_MEM_OUTPUT_BASE, 0, 0, 0);
93	if (addr == (bus_addr_t)-1 ||
94	    bus_space_map(sc->sc_iot, addr, PAGE_SIZE,
95	    BUS_SPACE_MAP_CACHEABLE, &sc->sc_out_ioh)) {
96		bus_space_unmap(sc->sc_iot, sc->sc_in_ioh, PAGE_SIZE);
97		printf(": can't map shared memory\n");
98		return;
99	}
100
101	/*
102	 * Version 2 gives us a 16-byte chip ID.  If that fails, fall
103	 * back on version 0/1 which gives us a 12-byte chipt ID.
104	 */
105	info = bus_space_vaddr(sc->sc_iot, sc->sc_out_ioh);
106	memset(info, 0, sizeof(struct aml_cpu_info));
107	ret = smc_call(AML_SM_GET_CHIP_ID, 2, 0, 0);
108	if (ret == -1)
109		ret = smc_call(AML_SM_GET_CHIP_ID, 0, 0, 0);
110
111	printf(": ver %u\n", info->version);
112
113	if (ret != -1) {
114		for (i = 0; i < nitems(info->chip_id); i++)
115			enqueue_randomness(info->chip_id[i]);
116	}
117}
118