walnux/drivers/clk/clk_multiplier.c

258 lines
7 KiB
C
Raw Normal View History

/****************************************************************************
* drivers/clk/clk_multiplier.c
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/clk/clk.h>
#include <nuttx/clk/clk_provider.h>
#include <debug.h>
#include <stdlib.h>
#include "clk.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define to_clk_multiplier(_clk) (FAR struct clk_multiplier_s *) \
(_clk->private_data)
/****************************************************************************
* Private Functions
****************************************************************************/
static uint32_t _get_maxmult(FAR struct clk_multiplier_s *multiplier)
{
if (multiplier->flags & CLK_MULT_ONE_BASED)
{
return MASK(multiplier->width);
}
if (multiplier->flags & CLK_MULT_MAX_HALF)
{
return 1 << (multiplier->width - 1);
}
return MASK(multiplier->width) + 1;
}
static uint32_t _get_mult(FAR struct clk_multiplier_s *multiplier,
uint32_t val)
{
if (multiplier->flags & CLK_MULT_ONE_BASED)
{
return val;
}
return val + 1;
}
static uint32_t _get_val(FAR struct clk_multiplier_s *multiplier,
uint32_t mult)
{
if (multiplier->flags & CLK_MULT_ONE_BASED)
{
return mult;
}
return mult - 1;
}
static uint32_t clk_multiplier_recalc_rate(FAR struct clk_s *clk,
uint32_t parent_rate)
{
FAR struct clk_multiplier_s *multiplier = to_clk_multiplier(clk);
uint32_t mult;
uint32_t val;
val = clk_read(multiplier->reg) >> multiplier->shift;
val &= MASK(multiplier->width);
mult = _get_mult(multiplier, val);
if (!mult)
{
if (!(multiplier->flags & CLK_MULT_ALLOW_ZERO))
{
return -EINVAL;
}
return parent_rate;
}
return parent_rate * mult;
}
static bool __is_best_rate(uint32_t rate, uint32_t new,
uint32_t best, uint16_t flags)
{
if (flags & CLK_MULT_ROUND_CLOSEST)
{
MacOs: fix the sim compile warning in MacOS CC: clk/clk_fixed_rate.c clk/clk_divider.c:177:14: warning: taking the absolute value of unsigned type 'unsigned int' has no effect [-Wabsolute-value] return abs(rate - now) < abs(rate - best); ^ clk/clk_divider.c:177:14: note: remove the call to 'abs' since unsigned values cannot be negative return abs(rate - now) < abs(rate - best); ^~~ clk/clk_divider.c:177:32: warning: taking the absolute value of unsigned type 'unsigned int' has no effect [-Wabsolute-value] return abs(rate - now) < abs(rate - best); ^ clk/clk_divider.c:177:32: note: remove the call to 'abs' since unsigned values cannot be negative return abs(rate - now) < abs(rate - best); ^~~ CC: mm_heap/mm_initialize.c 2 warnings generated. clk/clk.c:1324:11: warning: variable 'irqflags' is used uninitialized whenever 'if' condition is true [-Wsometimes-uninitialized] if (clk->parents == NULL) ^~~~~~~~~~~~~~~~~~~~ clk/clk.c:1341:19: note: uninitialized use occurs here clk_list_unlock(irqflags); ^~~~~~~~ clk/clk.c:1324:7: note: remove the 'if' if its condition is always false if (clk->parents == NULL) ^~~~~~~~~~~~~~~~~~~~~~~~~ clk/clk.c:1255:22: note: initialize the variable 'irqflags' to silence this warning irqstate_t irqflags; ^ = 0 CC: clk/clk_mux.c clk/clk_multiplier.c:110:14: warning: taking the absolute value of unsigned type 'unsigned int' has no effect [-Wabsolute-value] return abs(rate - new) < abs(rate - best); ^ clk/clk_multiplier.c:110:14: note: remove the call to 'abs' since unsigned values cannot be negative return abs(rate - new) < abs(rate - best); ^~~ clk/clk_multiplier.c:110:32: warning: taking the absolute value of unsigned type 'unsigned int' has no effect [-Wabsolute-value] return abs(rate - new) < abs(rate - best); ^ clk/clk_multiplier.c:110:32: note: remove the call to 'abs' since unsigned values cannot be negative return abs(rate - new) < abs(rate - best); ^~~ clk/clk_mux.c:47:14: warning: taking the absolute value of unsigned type 'unsigned int' has no effect [-Wabsolute-value] return abs(now - rate) < abs(best - rate); ^ clk/clk_mux.c:47:14: note: remove the call to 'abs' since unsigned values cannot be negative return abs(now - rate) < abs(best - rate); ^~~ clk/clk_mux.c:47:32: warning: taking the absolute value of unsigned type 'unsigned int' has no effect [-Wabsolute-value] return abs(now - rate) < abs(best - rate); ^ clk/clk_mux.c:47:32: note: remove the call to 'abs' since unsigned values cannot be negative return abs(now - rate) < abs(best - rate); ^~~ AS: sim/sim_fork_x86.S 2 warnings generated. 1 warning2 warnings generated. generated. iperf.c:325:14: warning: format specifies type 'uintmax_t' (aka 'unsigned long long') but the argument has type 'unsigned long' [-Wformat] now_len -last_len, ^~~~~~~~~~~~~~~~~ iperf.c:340:14: warning: format specifies type 'uintmax_t' (aka 'unsigned long long') but the argument has type 'uintmax_t' (aka 'unsigned long') [-Wformat] now_len, ^~~~~~~ CC: misc/rpmsgblk_server.c 2 warnings generated. :28: warning: format specifies type 'uintmax_t' (aka 'unsigned long long') but the argument has type 'uintmax_t' (aka 'unsigned long') [-Wformat] (uintmax_t)skip); ^~~~~~~~~~~~~~~ nsh_dbgcmds.c:473:20: warning: format specifies type 'uintmax_t' (aka 'unsigned long long') but the argument has type 'uintmax_t' (aka 'unsigned long') [-Wformat] (uintmax_t)position); ^~~~~~~~~~~~~~~~~~~ CC: nsh_main.c 2 warnings generated. return INTMAX_MIN; ~~~~~~ ^~~~~~~~~~ CC: nsh_system.c note: expanded from macro 'INTMAX_MIN' ^~~~~~~~~ /Users/vela/work/nuttx/include/stdint.h:65:41: note: expanded from macro 'INT64_MIN' ~~~~~~~~~~~^~~ inttypes/lib_strtoimax.c:103:37: warning: implicit conversion from 'long long' to 'intmax_t' (aka 'long') changes value from -9223372036854775808 to 0 [-Wconstant-conversion] return (accum == limit) ? INTMAX_MIN : -(intmax_t)accum; ~~~~~~ ^~~~~~~~~~ /Users/vela/work/nuttx/include/stdint.h:136:29: note: expanded from macro 'INTMAX_MIN' ^~~~~~~~~ /Users/vela/work/nuttx/include/stdint.h:65:41: note: expanded from macro 'INT64_MIN' ~~~~~~~~~~~^~~ inttypes/lib_strtoimax.c:106:17: warning: result of comparison of constant 9223372036854775807 with expression of type 'uintmax_t' (aka 'unsigned long') is always false [-Wtautological-constant-out-of-range-compare] if (accum > INTMAX_MAX) ~~~~~ ^ ~~~~~~~~~~ inttypes/lib_strtoimax.c:109:18: warning: implicit conversion from 'long long' to 'intmax_t' (aka 'long') changes value from 9223372036854775807 to -1 [-Wconstant-conversion] return INTMAX_MAX; ~~~~~~ ^~~~~~~~~~ /Users/vela/work/nuttx/include/stdint.h:137:29: note: expanded from macro 'INTMAX_MAX' ^~~~~~~~~ /Users/vela/work/nuttx/include/stdint.h:66:29: note: expanded from macro 'INT64_MAX' ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ /Users/vela/work/nuttx/include/arch/inttypes.h:35:23: note: expanded from macro 'INT64_C' ^~~~~~~ <scratch space>:12:1: note: expanded from here 9223372036854775807ll ^~~~~~~~~~~~~~~~~~~~~ syslog/vsyslog.c:212:32: warning: format specifies type 'uintmax_t' (aka 'unsigned long long') but the argument has type 'uintmax_t' (aka 'unsigned long') [-Wformat] , (uintmax_t)ts.tv_sec ^~~~~~~~~~~~~~~~~~~~ CC: iob/iob_free.c 4 warnings generated. 1 warning generated. CC: mqueue/mq_msgqalloc.c inttypes/lib_strtoumax.c:91:23: warning: implicit conversion from 'unsigned long long' to 'uintmax_t' (aka 'unsigned long') changes value from 18446744073709551615 to 4294967295 [-Wconstant-conversion] accum = UINTMAX_MAX; ~ ^~~~~~~~~~~ /Users/vela/work/nuttx/include/stdint.h:140:29: note: expanded from macro 'UINTMAX_MAX' ^~~~~~~~~~ /Users/vela/work/nuttx/include/stdint.h:67:29: note: expanded from macro 'UINT64_MAX' ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /Users/vela/work/nuttx/include/arch/inttypes.h:36:23: note: expanded from macro 'UINT64_C' ^~~~~~~~ <scratch space>:8:1: note: expanded from here 18446744073709551615ull ^~~~~~~~~~~~~~~~~~~~~~~ CC: icmp/icmp_ioctl.c 1 warning generated. time/lib_strftime.c:584:52: warning: format specifies type 'uintmax_t' (aka 'unsigned long long') but the argument has type 'uintmax_t' (aka 'unsigned long') [-Wformat] len = snprintf(dest, chleft, "%ju", (uintmax_t)mktime(&tmp)); ~~~ ^~~~~~~~~~~~~~~~~~~~~~~ %ju Signed-off-by: Bowen Wang <wangbowen6@xiaomi.com>
2024-10-17 17:06:11 +08:00
return clk_is_best_rate_closest(rate, new, best);
}
return new >= rate && new < best;
}
static uint32_t clk_multiplier_bestmult(FAR struct clk_s *clk, uint32_t rate,
FAR uint32_t *best_parent_rate)
{
FAR struct clk_multiplier_s *multiplier = to_clk_multiplier(clk);
uint32_t i;
uint32_t bestmult = 0;
uint32_t parent_rate;
uint32_t best = 0;
uint32_t now;
uint32_t maxmult;
uint32_t parent_rate_saved = *best_parent_rate;
if (!rate)
{
rate = 1;
}
maxmult = _get_maxmult(multiplier);
if (!(clk->flags & CLK_SET_RATE_PARENT))
{
parent_rate = *best_parent_rate;
bestmult = rate / parent_rate;
bestmult = bestmult == 0 ? 1 : bestmult;
bestmult = bestmult > maxmult ? maxmult : bestmult;
return bestmult;
}
for (i = maxmult; i >= 1; i--)
{
if (rate == parent_rate_saved * i)
{
*best_parent_rate = parent_rate_saved;
return i;
}
parent_rate = clk_round_rate(clk_get_parent(clk),
rate / i);
now = parent_rate * i;
if (__is_best_rate(rate, now, best, multiplier->flags))
{
bestmult = i;
best = now;
*best_parent_rate = parent_rate;
}
}
if (!bestmult)
{
bestmult = 1;
*best_parent_rate = clk_round_rate(clk_get_parent(clk), 1);
}
return bestmult;
}
static uint32_t clk_multiplier_round_rate(FAR struct clk_s *clk,
uint32_t rate, FAR uint32_t *prate)
{
return *prate * clk_multiplier_bestmult(clk, rate, prate);
}
static int clk_multiplier_set_rate(FAR struct clk_s *clk, uint32_t rate,
uint32_t parent_rate)
{
FAR struct clk_multiplier_s *multiplier = to_clk_multiplier(clk);
uint32_t mult;
uint32_t value;
uint32_t val;
mult = rate / parent_rate;
value = _get_val(multiplier, mult);
if (value > MASK(multiplier->width))
{
value = MASK(multiplier->width);
}
if (multiplier->flags & CLK_MULT_HIWORD_MASK)
{
val = MASK(multiplier->width) << (multiplier->shift + 16);
}
else
{
val = clk_read(multiplier->reg);
val &= ~(MASK(multiplier->width) << multiplier->shift);
}
val |= value << multiplier->shift;
clk_write(multiplier->reg, val);
return 0;
}
/****************************************************************************
* Public Data
****************************************************************************/
const struct clk_ops_s g_clk_multiplier_ops =
{
.recalc_rate = clk_multiplier_recalc_rate,
.round_rate = clk_multiplier_round_rate,
.set_rate = clk_multiplier_set_rate,
};
/****************************************************************************
* Public Functions
****************************************************************************/
FAR struct clk_s *clk_register_multiplier(FAR const char *name,
FAR const char *parent_name,
uint8_t flags, uint32_t reg,
uint8_t shift,
uint8_t width,
uint8_t clk_multiplier_flags)
{
struct clk_multiplier_s mult;
FAR const char **parent_names;
uint8_t num_parents;
if (clk_multiplier_flags & CLK_MULT_HIWORD_MASK)
{
if (width + shift > 16)
{
return NULL;
}
}
parent_names = parent_name ? &parent_name : NULL;
num_parents = parent_name ? 1 : 0;
mult.reg = reg;
mult.shift = shift;
mult.width = width;
mult.flags = clk_multiplier_flags;
return clk_register(name, parent_names, num_parents, flags,
&g_clk_multiplier_ops, &mult, sizeof(mult));
}