ti_cpufreq.c revision 1.3
1/* $NetBSD: ti_cpufreq.c,v 1.3 2020/06/03 18:02:03 jmcneill Exp $ */
2
3/*-
4 * Copyright (c) 2019 Jared McNeill <jmcneill@invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include "opt_soc.h"
30
31#include <sys/cdefs.h>
32__KERNEL_RCSID(0, "$NetBSD: ti_cpufreq.c,v 1.3 2020/06/03 18:02:03 jmcneill Exp $");
33
34#include <sys/param.h>
35#include <sys/systm.h>
36#include <sys/device.h>
37#include <sys/kmem.h>
38#include <sys/bus.h>
39
40#include <dev/fdt/fdtvar.h>
41#include <dev/fdt/syscon.h>
42
43static bool		ti_opp_probed = false;
44static bool		(*ti_opp_supportedfn)(const int, const int);
45static struct syscon	*ti_opp_syscon;
46
47#ifdef SOC_AM33XX
48
49#define	AM33XX_REV_OFFSET	0x0600
50#define	AM33XX_REV_MASK		0xf0000000
51#define	AM33XX_EFUSE_OFFSET	0x07fc
52#define	AM33XX_EFUSE_MASK	0x00001fff
53#define	AM33XX_EFUSE_DEFAULT	0x1e2f
54
55static const char * const am33xx_compatible[] = { "ti,am33xx", NULL };
56
57static bool
58am33xx_opp_supported(const int opp_table, const int opp_node)
59{
60	const u_int *supported_hw;
61	uint32_t efuse, rev;
62	int len;
63
64	syscon_lock(ti_opp_syscon);
65	rev = __BIT(__SHIFTOUT(syscon_read_4(ti_opp_syscon, AM33XX_REV_OFFSET), AM33XX_REV_MASK));
66	efuse = __SHIFTOUT(syscon_read_4(ti_opp_syscon, AM33XX_EFUSE_OFFSET), AM33XX_EFUSE_MASK);
67	syscon_unlock(ti_opp_syscon);
68
69	if (efuse == 0)
70		efuse = AM33XX_EFUSE_DEFAULT;
71	efuse = ~efuse;
72
73	supported_hw = fdtbus_get_prop(opp_node, "opp-supported-hw", &len);
74	if (len != 8)
75		return false;
76
77	if ((rev & be32toh(supported_hw[0])) == 0)
78		return false;
79
80	if ((efuse & be32toh(supported_hw[1])) == 0)
81		return false;
82
83	return true;
84}
85#endif
86
87static void
88ti_opp_probe(const int opp_table)
89{
90	if (ti_opp_probed)
91		return;
92	ti_opp_probed = true;
93
94	ti_opp_syscon = fdtbus_syscon_acquire(opp_table, "syscon");
95
96#ifdef SOC_AM33XX
97	if (ti_opp_syscon && of_match_compatible(OF_finddevice("/"), am33xx_compatible))
98		ti_opp_supportedfn = am33xx_opp_supported;
99#endif
100}
101
102static bool
103ti_opp_supported(const int opp_table, const int opp_node)
104{
105	ti_opp_probe(opp_table);
106
107	if (!of_hasprop(opp_node, "opp-supported-hw"))
108		return true;
109
110	return ti_opp_supportedfn ? ti_opp_supportedfn(opp_table, opp_node) : false;
111}
112
113FDT_OPP(ti, "operating-points-v2-ti-cpu", ti_opp_supported);
114