From bcd7ccc0b5392279a159de056779830b57092eb2 Mon Sep 17 00:00:00 2001 From: Huang Qi Date: Sat, 6 Jun 2020 10:42:33 +0800 Subject: [PATCH] arch/risc-v/src/k210: Add basic gpiohs support Signed-off-by: Huang Qi --- arch/risc-v/src/k210/Make.defs | 4 +- .../risc-v/src/k210/hardware/k210_memorymap.h | 3 +- arch/risc-v/src/k210/k210_fpioa.c | 45 +++++++++ arch/risc-v/src/k210/k210_fpioa.h | 92 +++++++++++++++++++ arch/risc-v/src/k210/k210_gpiohs.c | 83 +++++++++++++++++ arch/risc-v/src/k210/k210_gpiohs.h | 83 +++++++++++++++++ 6 files changed, 307 insertions(+), 3 deletions(-) create mode 100644 arch/risc-v/src/k210/k210_fpioa.c create mode 100644 arch/risc-v/src/k210/k210_fpioa.h create mode 100644 arch/risc-v/src/k210/k210_gpiohs.c create mode 100644 arch/risc-v/src/k210/k210_gpiohs.h diff --git a/arch/risc-v/src/k210/Make.defs b/arch/risc-v/src/k210/Make.defs index f6b8f72864..c8ad47e5fe 100644 --- a/arch/risc-v/src/k210/Make.defs +++ b/arch/risc-v/src/k210/Make.defs @@ -59,8 +59,8 @@ endif # Specify our C code within this directory to be included CHIP_CSRCS = k210_allocateheap.c k210_clockconfig.c CHIP_CSRCS += k210_idle.c k210_irq.c k210_irq_dispatch.c -CHIP_CSRCS += k210_lowputc.c k210_serial.c -CHIP_CSRCS += k210_start.c k210_timerisr.c +CHIP_CSRCS += k210_lowputc.c k210_serial.c k210_fpioa.c +CHIP_CSRCS += k210_start.c k210_timerisr.c k210_gpiohs.c ifeq ($(CONFIG_SMP), y) CHIP_CSRCS += k210_cpuidlestack.c k210_cpuindex.c diff --git a/arch/risc-v/src/k210/hardware/k210_memorymap.h b/arch/risc-v/src/k210/hardware/k210_memorymap.h index ac09406f23..60b6a2a781 100644 --- a/arch/risc-v/src/k210/hardware/k210_memorymap.h +++ b/arch/risc-v/src/k210/hardware/k210_memorymap.h @@ -39,7 +39,8 @@ #else #define K210_UART0_BASE 0x38000000 #endif -#define K210_GPIO_BASE 0x38001000 +#define K210_GPIOHS_BASE 0x38001000 +#define K210_FPIOA_BASE 0x502B0000 #define K210_SYSCTL_BASE 0x50440000 diff --git a/arch/risc-v/src/k210/k210_fpioa.c b/arch/risc-v/src/k210/k210_fpioa.c new file mode 100644 index 0000000000..bb71863da8 --- /dev/null +++ b/arch/risc-v/src/k210/k210_fpioa.c @@ -0,0 +1,45 @@ +/**************************************************************************** + * arch/risc-v/src/k210/k210_fpioa.c + * + * 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 + +#include +#include + +#include "riscv_internal.h" +#include "riscv_arch.h" + +#include "k210_memorymap.h" +#include "k210_fpioa.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +void k210_fpioa_config(uint32_t io, uint32_t ioflags) +{ + uint32_t *fpioa = (uint32_t *)K210_FPIOA_BASE; + DEBUGASSERT(io < K210_IO_NUMBER); + putreg32(ioflags, &fpioa[io]); +} diff --git a/arch/risc-v/src/k210/k210_fpioa.h b/arch/risc-v/src/k210/k210_fpioa.h new file mode 100644 index 0000000000..5b13884a26 --- /dev/null +++ b/arch/risc-v/src/k210/k210_fpioa.h @@ -0,0 +1,92 @@ +/**************************************************************************** + * arch/risc-v/src/k210/k210_fpioa.h + * + * 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_K210_FPIOA_H +#define __ARCH_RISCV_SRC_K210_K210_FPIOA_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define K210_IO_NUMBER 48 + +#define K210_IO_FUNC_UARTHS_RX 18 /* UART High speed Receiver */ +#define K210_IO_FUNC_UARTHS_TX 19 /* UART High speed Transmitter */ +#define K210_IO_FUNC_GPIOHS0 24 /* GPIO High speed 0 */ +#define K210_IO_FUNC_GPIOHS1 25 /* GPIO High speed 1 */ +#define K210_IO_FUNC_GPIOHS2 26 /* GPIO High speed 2 */ +#define K210_IO_FUNC_GPIOHS3 27 /* GPIO High speed 3 */ +#define K210_IO_FUNC_GPIOHS4 28 /* GPIO High speed 4 */ +#define K210_IO_FUNC_GPIOHS5 29 /* GPIO High speed 5 */ +#define K210_IO_FUNC_GPIOHS6 30 /* GPIO High speed 6 */ +#define K210_IO_FUNC_GPIOHS7 31 /* GPIO High speed 7 */ +#define K210_IO_FUNC_GPIOHS8 32 /* GPIO High speed 8 */ +#define K210_IO_FUNC_GPIOHS9 33 /* GPIO High speed 9 */ +#define K210_IO_FUNC_GPIOHS10 34 /* GPIO High speed 10 */ +#define K210_IO_FUNC_GPIOHS11 35 /* GPIO High speed 11 */ +#define K210_IO_FUNC_GPIOHS12 36 /* GPIO High speed 12 */ +#define K210_IO_FUNC_GPIOHS13 37 /* GPIO High speed 13 */ +#define K210_IO_FUNC_GPIOHS14 38 /* GPIO High speed 14 */ +#define K210_IO_FUNC_GPIOHS15 39 /* GPIO High speed 15 */ +#define K210_IO_FUNC_GPIOHS16 40 /* GPIO High speed 16 */ +#define K210_IO_FUNC_GPIOHS17 41 /* GPIO High speed 17 */ +#define K210_IO_FUNC_GPIOHS18 42 /* GPIO High speed 18 */ +#define K210_IO_FUNC_GPIOHS19 43 /* GPIO High speed 19 */ +#define K210_IO_FUNC_GPIOHS20 44 /* GPIO High speed 20 */ +#define K210_IO_FUNC_GPIOHS21 45 /* GPIO High speed 21 */ +#define K210_IO_FUNC_GPIOHS22 46 /* GPIO High speed 22 */ +#define K210_IO_FUNC_GPIOHS23 47 /* GPIO High speed 23 */ +#define K210_IO_FUNC_GPIOHS24 48 /* GPIO High speed 24 */ +#define K210_IO_FUNC_GPIOHS25 49 /* GPIO High speed 25 */ +#define K210_IO_FUNC_GPIOHS26 50 /* GPIO High speed 26 */ +#define K210_IO_FUNC_GPIOHS27 51 /* GPIO High speed 27 */ +#define K210_IO_FUNC_GPIOHS28 52 /* GPIO High speed 28 */ +#define K210_IO_FUNC_GPIOHS29 53 /* GPIO High speed 29 */ +#define K210_IO_FUNC_GPIOHS30 54 /* GPIO High speed 30 */ +#define K210_IO_FUNC_GPIOHS31 55 /* GPIO High speed 31 */ + +#define K210_IO_DS(x) (x << 8) /* Driving Selector */ + +#define K210_IO_OUTPUT_ENABLE (1 << 12) +#define K210_IO_OUTPUT_INVERT (1 << 13) +#define K210_IO_INPUT_ENABLE (1 << 20) +#define K210_IO_INPUT_INVERT (1 << 21) +#define K210_IO_PULL_DOWN (1 << 16) +#define K210_IO_PULL_UP (3 << 16) +#define K210_IO_PULL_UP_STRONG (7 << 16) +#define K210_IO_SL (1 << 19) +#define K210_IO_ST (1 << 23) + +#define K210_FLAG_GPIOHS (K210_IO_DS(0xf) | K210_IO_OUTPUT_ENABLE | \ + K210_IO_INPUT_ENABLE | K210_IO_ST) + +/**************************************************************************** + * Public Functions Prototypes + ****************************************************************************/ + +void k210_fpioa_config(uint32_t io, uint32_t ioflag); + +#endif /* __ARCH_RISCV_SRC_K210_K210_FPIOA_H */ diff --git a/arch/risc-v/src/k210/k210_gpiohs.c b/arch/risc-v/src/k210/k210_gpiohs.c new file mode 100644 index 0000000000..6ec32e2b12 --- /dev/null +++ b/arch/risc-v/src/k210/k210_gpiohs.c @@ -0,0 +1,83 @@ +/**************************************************************************** + * arch/risc-v/src/k210/k210_gpiohs.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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include "riscv_arch.h" + +#include "k210_memorymap.h" +#include "k210_gpiohs.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define GPIOHS_INPUT_VAL_OFFSET 0x00 +#define GPIOHS_INPUT_EN_OFFSET 0x04 +#define GPIOHS_OUTPUT_EN_OFFSET 0x08 +#define GPIOHS_OUTPUT_VAL_OFFSET 0x0c +#define GPIOHS_PULLUP_EN_OFFSET 0x10 +#define GPIOHS_DRIVE_OFFSET 0x14 + +#define GPIOHS_INPUT (K210_GPIOHS_BASE + GPIOHS_INPUT_VAL_OFFSET) +#define GPIOHS_INPUT_EN (K210_GPIOHS_BASE + GPIOHS_INPUT_EN_OFFSET) +#define GPIOHS_OUTPUT (K210_GPIOHS_BASE + GPIOHS_OUTPUT_VAL_OFFSET) +#define GPIOHS_OUTPUT_EN (K210_GPIOHS_BASE + GPIOHS_OUTPUT_EN_OFFSET) + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +void k210_gpiohs_set_direction(uint32_t io, bool dir) +{ + uint32_t outbit = dir << io; + uint32_t inbit = (!dir) << io; + modifyreg32(GPIOHS_OUTPUT_EN, inbit, outbit); + modifyreg32(GPIOHS_INPUT_EN, outbit, inbit); +} + +void k210_gpiohs_set_value(uint32_t io, bool val) +{ + uint32_t setbit = val << io; + uint32_t clrbit = (!val) << io; + modifyreg32(GPIOHS_OUTPUT, clrbit, setbit); +} + +bool k210_gpiohs_get_value(uint32_t io) +{ + uint32_t reg = getreg32(GPIOHS_INPUT); + + if (reg & (1 << io)) + { + return true; + } + else + { + return false; + } +} diff --git a/arch/risc-v/src/k210/k210_gpiohs.h b/arch/risc-v/src/k210/k210_gpiohs.h new file mode 100644 index 0000000000..1c00fc1e35 --- /dev/null +++ b/arch/risc-v/src/k210/k210_gpiohs.h @@ -0,0 +1,83 @@ +/**************************************************************************** + * arch/risc-v/src/k210/k210_gpiohs.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_K210_GPIOHS_H +#define __ARCH_RISCV_SRC_K210_K210_GPIOHS_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include + +/**************************************************************************** + * Public Functions Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Name: k210_gpiohs_set_direction + * + * Description: + * Set gpiohs direction + * + * Input Parameters: + * io - IO number + * dir - true for output, false for input + * + ****************************************************************************/ + +void k210_gpiohs_set_direction(uint32_t io, bool dir); + +/**************************************************************************** + * Name: k210_gpiohs_set_value + * + * Description: + * Set gpiohs direction + * + * Input Parameters: + * io - IO number + * dir - true for high level, false for low level + * + ****************************************************************************/ + +void k210_gpiohs_set_value(uint32_t io, bool val); + +/**************************************************************************** + * Name: k210_gpiohs_get_value + * + * Description: + * Get gpiohs level + * + * Input Parameters: + * io - IO number + * + * Returned Value: + * true for high level, false for low level + * + ****************************************************************************/ + +bool k210_gpiohs_get_value(uint32_t io); + +#endif /* __ARCH_RISCV_SRC_K210_K210_GPIOHS_H */