Home | History | Annotate | Line # | Download | only in ti
      1 /* $NetBSD: ti_cpufreq.c,v 1.5 2025/09/06 21:24:05 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2019 Jared McNeill <jmcneill (at) 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.5 2025/09/06 21:24:05 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/fdt_opp.h>
     42 #include <dev/fdt/syscon.h>
     43 
     44 static bool		ti_opp_probed = false;
     45 static bool		(*ti_opp_supportedfn)(const int, const int);
     46 static struct syscon	*ti_opp_syscon;
     47 
     48 #ifdef SOC_AM33XX
     49 
     50 #define	AM33XX_REV_OFFSET	0x0600
     51 #define	AM33XX_REV_MASK		0xf0000000
     52 #define	AM33XX_EFUSE_OFFSET	0x07fc
     53 #define	AM33XX_EFUSE_MASK	0x00001fff
     54 #define	AM33XX_EFUSE_DEFAULT	0x1e2f
     55 
     56 static const struct device_compatible_entry am33xx_compat_data[] = {
     57 	{ .compat = "ti,am33xx" },
     58 	DEVICE_COMPAT_EOL
     59 };
     60 
     61 static bool
     62 am33xx_opp_supported(const int opp_table, const int opp_node)
     63 {
     64 	const u_int *supported_hw;
     65 	uint32_t efuse, rev;
     66 	int len;
     67 
     68 	syscon_lock(ti_opp_syscon);
     69 	rev = __BIT(__SHIFTOUT(syscon_read_4(ti_opp_syscon, AM33XX_REV_OFFSET), AM33XX_REV_MASK));
     70 	efuse = __SHIFTOUT(syscon_read_4(ti_opp_syscon, AM33XX_EFUSE_OFFSET), AM33XX_EFUSE_MASK);
     71 	syscon_unlock(ti_opp_syscon);
     72 
     73 	if (efuse == 0)
     74 		efuse = AM33XX_EFUSE_DEFAULT;
     75 	efuse = ~efuse;
     76 
     77 	supported_hw = fdtbus_get_prop(opp_node, "opp-supported-hw", &len);
     78 	if (len != 8)
     79 		return false;
     80 
     81 	if ((rev & be32toh(supported_hw[0])) == 0)
     82 		return false;
     83 
     84 	if ((efuse & be32toh(supported_hw[1])) == 0)
     85 		return false;
     86 
     87 	return true;
     88 }
     89 #endif
     90 
     91 static void
     92 ti_opp_probe(const int opp_table)
     93 {
     94 	if (ti_opp_probed)
     95 		return;
     96 	ti_opp_probed = true;
     97 
     98 	ti_opp_syscon = fdtbus_syscon_acquire(opp_table, "syscon");
     99 
    100 #ifdef SOC_AM33XX
    101 	if (ti_opp_syscon &&
    102 	    of_compatible_match(OF_finddevice("/"), am33xx_compat_data))
    103 		ti_opp_supportedfn = am33xx_opp_supported;
    104 #endif
    105 }
    106 
    107 static bool
    108 ti_opp_supported(const int opp_table, const int opp_node)
    109 {
    110 	ti_opp_probe(opp_table);
    111 
    112 	if (!of_hasprop(opp_node, "opp-supported-hw"))
    113 		return true;
    114 
    115 	return ti_opp_supportedfn ? ti_opp_supportedfn(opp_table, opp_node) : false;
    116 }
    117 
    118 FDT_OPP(ti, "operating-points-v2-ti-cpu", ti_opp_supported);
    119