1 /* $NetBSD: ti_cpufreq.c,v 1.4 2021/01/27 03:10:20 thorpej 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.4 2021/01/27 03:10:20 thorpej 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
43 static bool ti_opp_probed = false;
44 static bool (*ti_opp_supportedfn)(const int, const int);
45 static 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
55 static const struct device_compatible_entry am33xx_compat_data[] = {
56 { .compat = "ti,am33xx" },
57 DEVICE_COMPAT_EOL
58 };
59
60 static bool
am33xx_opp_supported(const int opp_table,const int opp_node)61 am33xx_opp_supported(const int opp_table, const int opp_node)
62 {
63 const u_int *supported_hw;
64 uint32_t efuse, rev;
65 int len;
66
67 syscon_lock(ti_opp_syscon);
68 rev = __BIT(__SHIFTOUT(syscon_read_4(ti_opp_syscon, AM33XX_REV_OFFSET), AM33XX_REV_MASK));
69 efuse = __SHIFTOUT(syscon_read_4(ti_opp_syscon, AM33XX_EFUSE_OFFSET), AM33XX_EFUSE_MASK);
70 syscon_unlock(ti_opp_syscon);
71
72 if (efuse == 0)
73 efuse = AM33XX_EFUSE_DEFAULT;
74 efuse = ~efuse;
75
76 supported_hw = fdtbus_get_prop(opp_node, "opp-supported-hw", &len);
77 if (len != 8)
78 return false;
79
80 if ((rev & be32toh(supported_hw[0])) == 0)
81 return false;
82
83 if ((efuse & be32toh(supported_hw[1])) == 0)
84 return false;
85
86 return true;
87 }
88 #endif
89
90 static void
ti_opp_probe(const int opp_table)91 ti_opp_probe(const int opp_table)
92 {
93 if (ti_opp_probed)
94 return;
95 ti_opp_probed = true;
96
97 ti_opp_syscon = fdtbus_syscon_acquire(opp_table, "syscon");
98
99 #ifdef SOC_AM33XX
100 if (ti_opp_syscon &&
101 of_compatible_match(OF_finddevice("/"), am33xx_compat_data))
102 ti_opp_supportedfn = am33xx_opp_supported;
103 #endif
104 }
105
106 static bool
ti_opp_supported(const int opp_table,const int opp_node)107 ti_opp_supported(const int opp_table, const int opp_node)
108 {
109 ti_opp_probe(opp_table);
110
111 if (!of_hasprop(opp_node, "opp-supported-hw"))
112 return true;
113
114 return ti_opp_supportedfn ? ti_opp_supportedfn(opp_table, opp_node) : false;
115 }
116
117 FDT_OPP(ti, "operating-points-v2-ti-cpu", ti_opp_supported);
118