diff --git a/arch/arm/soc/nxp_kinetis/Kconfig b/arch/arm/soc/nxp_kinetis/Kconfig index 2535c64823b..ebb7abd6c18 100644 --- a/arch/arm/soc/nxp_kinetis/Kconfig +++ b/arch/arm/soc/nxp_kinetis/Kconfig @@ -48,6 +48,12 @@ config HAS_MCG help Set if the multipurpose clock generator (MCG) module is present in the SoC. +config HAS_RNGA + bool + default n + help + Set if the random number generator accelerator (RNGA) module is present in the SoC. + if HAS_OSC choice diff --git a/arch/arm/soc/nxp_kinetis/k6x/Kconfig.soc b/arch/arm/soc/nxp_kinetis/k6x/Kconfig.soc index 611bc0bd231..ae711422fd9 100644 --- a/arch/arm/soc/nxp_kinetis/k6x/Kconfig.soc +++ b/arch/arm/soc/nxp_kinetis/k6x/Kconfig.soc @@ -24,6 +24,7 @@ config SOC_MK64F12 select HAS_KSDK select HAS_OSC select HAS_MCG + select HAS_RNGA select CPU_HAS_FPU endchoice diff --git a/drivers/random/Kconfig b/drivers/random/Kconfig index 9921f623b6a..dc6036057ef 100644 --- a/drivers/random/Kconfig +++ b/drivers/random/Kconfig @@ -55,4 +55,14 @@ config TIMER_RANDOM_GENERATOR This options enables number generator based on system timer clock. This number generator is not random and used for testing only. + +config KSDK_RNGA + bool + prompt "KSDK RNGA driver" + depends on RANDOM_GENERATOR && HAS_RNGA + default n + help + This option enables the random number generator accelerator (RNGA) driver based on the + KSDK RNGA driver. + endmenu diff --git a/drivers/random/Makefile b/drivers/random/Makefile index cd9e16a011b..e1383dfd9e6 100644 --- a/drivers/random/Makefile +++ b/drivers/random/Makefile @@ -1,2 +1,3 @@ +obj-$(CONFIG_KSDK_RNGA) += rand32_ksdk_rnga.o obj-$(CONFIG_TIMER_RANDOM_GENERATOR) = rand32_timer.o obj-$(CONFIG_X86_TSC_RANDOM_GENERATOR) += rand32_timestamp.o diff --git a/drivers/random/rand32_ksdk_rnga.c b/drivers/random/rand32_ksdk_rnga.c new file mode 100644 index 00000000000..4a843e5727c --- /dev/null +++ b/drivers/random/rand32_ksdk_rnga.c @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2016 ARM Limited. + * + * SPDX-License-Identifier: Apache-2.0 + * Licensed 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. + */ + +#include +#include +#include + +#include "fsl_rnga.h" + + +void sys_rand32_init(void) +{ + uint32_t seed = sys_cycle_get_32(); + + RNGA_Init(RNG); + + /* The range of seed values acquired by this method is likely + * to be relatively small. The RNGA hardware uses two free + * running oscillators to add entropy to the seed value, we + * take care below to ensure the read rate is lower than the + * rate at which the hardware can add entropy. + */ + RNGA_Seed(RNG, seed); + RNGA_SetMode(RNG, kRNGA_ModeSleep); +} + +uint32_t sys_rand32_get(void) +{ + uint32_t random; + uint32_t output = 0; + int i; + + RNGA_SetMode(RNG, kRNGA_ModeNormal); + /* The Reference manual states that back to back reads from + * the RNGA deliver one or two bits of entropy per 32-bit + * word, therefore we deliberately only use 1 bit per 32 bit + * word read. + */ + for (i = 0; i < 32; i++) { + status_t status; + + status = RNGA_GetRandomData(RNG, &random, sizeof(random)); + __ASSERT(!status, "RNGA_GetRandomData failed"); + if (status) { + SYS_LOG_ERR("RNGA_GetRandomData failed with %d", + status); + } + output <<= 1; + output |= random & 1; + } + RNGA_SetMode(RNG, kRNGA_ModeSleep); + + return output; +} diff --git a/ext/hal/ksdk/drivers/Makefile b/ext/hal/ksdk/drivers/Makefile index 1b547fb1062..2985c2c86c1 100644 --- a/ext/hal/ksdk/drivers/Makefile +++ b/ext/hal/ksdk/drivers/Makefile @@ -15,3 +15,4 @@ # limitations under the License. obj-$(CONFIG_I2C_KSDK) += fsl_i2c.o +obj-$(CONFIG_KSDK_RNGA) += fsl_rnga.o