arch: k210: Set CPU clock based on PLL0 settings

This commit is contained in:
Masayuki Ishikawa 2020-05-05 09:14:19 +09:00 committed by Brennan Ashton
parent 6906853f8e
commit 4ca19e7e74
5 changed files with 133 additions and 52 deletions

View File

@ -1,32 +1,24 @@
/****************************************************************************
* arch/risc-v/src/k210/hardware/k210_memorymap.h
*
* Copyright (C) 2019 Masayuki Ishikawa. All rights reserved.
* Author: Masayuki Ishikawa <masayuki.ishikawa@gmail.com>
* Derives from software originally provided by Canaan Inc
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* Copyright 2018 Canaan Inc
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 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
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* 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.
*
****************************************************************************/
@ -49,4 +41,6 @@
#endif
#define K210_GPIO_BASE 0x38001000
#define K210_SYSCTL_BASE 0x50440000
#endif /* __ARCH_RISCV_SRC_K210_HARDWARE_K210_MEMORYMAP_H */

View File

@ -0,0 +1,48 @@
/****************************************************************************
* arch/risc-v/src/k210/hardware/k210_sysctl.h
*
* Derives from software originally provided by Canaan Inc
*
* Copyright 2018 Canaan Inc
*
* 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.
*
****************************************************************************/
#ifndef __ARCH_RISCV_SRC_K210_HARDWARE_K210_SYSCTL_H
#define __ARCH_RISCV_SRC_K210_HARDWARE_K210_SYSCTL_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define K210_SYSCTL_PLL0 (K210_SYSCTL_BASE + 0x08)
#define PLL_CLK_R(n) (n & 0x00000f)
#define PLL_CLK_F(n) ((n & 0x0003f0) >> 4)
#define PLL_CLK_OD(n) ((n & 0x003c00) >> 10)
#define K210_SYSCTL_CLKSEL0 (K210_SYSCTL_BASE + 0x20)
#define CLKSEL0_ACLK_SEL(n) (n & 0x00000001)
#endif /* __ARCH_RISCV_SRC_K210_HARDWARE_K210_SYSCTL_H */

View File

@ -1,35 +1,24 @@
/****************************************************************************
* arch/risc-v/src/k210/k210_clockconfig.c
*
* Copyright (C) 2019 Masayuki Ishikawa. All rights reserved.
* Author: Masayuki Ishikawa <masayuki.ishikawa@gmail.com>
* Derives from software originally provided by Canaan Inc
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* Copyright 2018 Canaan Inc
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
* 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
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* 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.
*
****************************************************************************/
@ -53,6 +42,14 @@
* Pre-processor Definitions
****************************************************************************/
#define OSC_FREQ 26000000
/****************************************************************************
* Private Data
****************************************************************************/
static uint32_t g_cpu_clock = 416000000;
/****************************************************************************
* Public Functions
****************************************************************************/
@ -63,14 +60,54 @@
uint32_t k210_get_cpuclk(void)
{
return 416000000; /* TODO */
return g_cpu_clock;
}
/****************************************************************************
* Name: k210_get_pll0clk
****************************************************************************/
#ifndef CONFIG_K210_WITH_QEMU
uint32_t k210_get_pll0clk(void)
{
uint32_t pll0;
uint32_t nr;
uint32_t nf;
uint32_t od;
pll0 = getreg32(K210_SYSCTL_PLL0);
nr = PLL_CLK_R(pll0) + 1;
nf = PLL_CLK_F(pll0) + 1;
od = PLL_CLK_OD(pll0) + 1;
return OSC_FREQ / nr * nf / od;
}
#endif
/****************************************************************************
* Name: k210_clockconfig
****************************************************************************/
void k210_clockconfig(void)
{
/* TODO */
#ifndef CONFIG_K210_WITH_QEMU
uint32_t clksel0;
/* Obtain clock selector for ACLK */
clksel0 = getreg32(K210_SYSCTL_CLKSEL0);
if (1 == CLKSEL0_ACLK_SEL(clksel0))
{
/* PLL0 selected */
g_cpu_clock = k210_get_pll0clk() / 2;
}
else
{
/* OSC selected */
g_cpu_clock = OSC_FREQ;
}
#endif
}

View File

@ -65,10 +65,11 @@ extern "C"
#endif
/****************************************************************************
* Public Functions
* Public Function Prototypes
****************************************************************************/
EXTERN uint32_t k210_get_cpuclk(void);
EXTERN uint32_t k210_get_pll0clk(void);
EXTERN void k210_clockconfig(void);
#if defined(__cplusplus)

View File

@ -41,6 +41,7 @@
#include "hardware/k210_uart.h"
#include "hardware/k210_clint.h"
#include "hardware/k210_plic.h"
#include "hardware/k210_sysctl.h"
/****************************************************************************
* Pre-processor Definitions