- 
                Notifications
    You must be signed in to change notification settings 
- Fork 8.2k
drivers: entropy: sf32lb: add trng driver support #98467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            ck-telecom
  wants to merge
  4
  commits into
  zephyrproject-rtos:main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
ck-telecom:sf32lb_trng
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            4 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      d50db29
              
                dts: bindings: rng: add sifli,sf32lb-trng
              
              
                ck-telecom 6d2ccc3
              
                dts: arm: sifli: sf32lb52x: add trng
              
              
                ck-telecom b9c066b
              
                drivers: entropy: sf32lb: add trng driver support
              
              
                ck-telecom 607d84d
              
                boards: sifli: sf32lb52_devkit_lcd: enable trng
              
              
                ck-telecom File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
          Some comments aren't visible on the classic Files Changed page.
        
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # Copyright (c) 2025, Qingsong Gou <gouqs@hotmail.com> | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|  | ||
| config ENTROPY_SF32LB | ||
| bool "SF32LB Entropy driver" | ||
| default y | ||
| depends on DT_HAS_SIFLI_SF32LB_TRNG_ENABLED | ||
| select ENTROPY_HAS_DRIVER | ||
| help | ||
| Enable driver for SF32LB True Random Number Generator (TRNG). | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| /* | ||
| * Copyright (c) 2025 Qingsong Gou <gouqs@hotmail.com> | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| #define DT_DRV_COMPAT sifli_sf32lb_trng | ||
|  | ||
| #include <zephyr/arch/cpu.h> | ||
| #include <zephyr/device.h> | ||
| #include <zephyr/drivers/clock_control/sf32lb.h> | ||
| #include <zephyr/drivers/entropy.h> | ||
| #include <zephyr/logging/log.h> | ||
|  | ||
| #include <register.h> | ||
|  | ||
| LOG_MODULE_REGISTER(entropy_sf32lb, CONFIG_ENTROPY_LOG_LEVEL); | ||
|  | ||
| #define TRNG_CTRL offsetof(TRNG_TypeDef, CTRL) | ||
| #define TRNG_STAT offsetof(TRNG_TypeDef, STAT) | ||
| #define TRNG_RAND offsetof(TRNG_TypeDef, RAND_NUM0) | ||
|  | ||
| #define TRNG_RAND_NUM_MAX (8U) | ||
|  | ||
| #define TRNG_RAND_MASK (TRNG_RAND_NUM_MAX - 1U) | ||
|  | ||
| struct entropy_sf32lb_config { | ||
| uintptr_t base; | ||
| struct sf32lb_clock_dt_spec clock; | ||
| }; | ||
|  | ||
| static int entropy_sf32lb_get_entropy(const struct device *dev, uint8_t *buffer, uint16_t length) | ||
| { | ||
| const struct entropy_sf32lb_config *config = dev->config; | ||
| int ret = 0; | ||
|  | ||
| sys_set_bit(config->base + TRNG_CTRL, TRNG_CTRL_GEN_SEED_START_Pos); | ||
| while (!sys_test_bit(config->base + TRNG_STAT, TRNG_STAT_SEED_VALID_Pos)) { | ||
| } | ||
|  | ||
| /* Generate random data */ | ||
| sys_set_bit(config->base + TRNG_CTRL, TRNG_CTRL_GEN_RAND_NUM_START_Pos); | ||
| while (!sys_test_bit(config->base + TRNG_STAT, TRNG_STAT_RAND_NUM_VALID_Pos)) { | ||
| } | ||
|  | ||
| for (uint16_t i = 0U; i < length; i += 4) { | ||
| uint8_t pos = (i & TRNG_RAND_MASK); | ||
| /* Copy random data to buffer */ | ||
| if (i + 4 <= length) { | ||
| memcpy(buffer + i, (void *)(config->base + TRNG_RAND + pos), 4); | ||
| } else { | ||
| memcpy(buffer + i, (void *)(config->base + TRNG_RAND + pos), length - i); | ||
| } | ||
| } | ||
|  | ||
| return ret; | ||
| } | ||
|  | ||
| static DEVICE_API(entropy, entropy_sf32lb_api) = { | ||
| .get_entropy = entropy_sf32lb_get_entropy, | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. consider adding support get_entropy_isr (non-blocking version) | ||
| }; | ||
|  | ||
| static int entropy_sf32lb_init(const struct device *dev) | ||
| { | ||
| const struct entropy_sf32lb_config *config = dev->config; | ||
| int ret; | ||
|  | ||
| if (!sf32lb_clock_is_ready_dt(&config->clock)) { | ||
| return -ENODEV; | ||
| } | ||
|  | ||
| ret = sf32lb_clock_control_on_dt(&config->clock); | ||
| if (ret < 0) { | ||
| return ret; | ||
| } | ||
|  | ||
| return ret; | ||
| } | ||
|  | ||
| #define ENTROPY_SF32LB_DEFINE(n) \ | ||
| static const struct entropy_sf32lb_config entropy_sf32lb_config_##n = { \ | ||
| .base = DT_INST_REG_ADDR(n), \ | ||
| .clock = SF32LB_CLOCK_DT_INST_SPEC_GET(n), \ | ||
| }; \ | ||
| \ | ||
| DEVICE_DT_INST_DEFINE(n, entropy_sf32lb_init, NULL, NULL, \ | ||
| &entropy_sf32lb_config_##n, PRE_KERNEL_1, \ | ||
| CONFIG_ENTROPY_INIT_PRIORITY, &entropy_sf32lb_api); | ||
|  | ||
| DT_INST_FOREACH_STATUS_OKAY(ENTROPY_SF32LB_DEFINE) | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # Copyright (c) 2025, Qingsong Gou <gouqs@hotmail.com> | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|  | ||
| description: Sifli SF32LB TRNG (True Random Number Generator). | ||
|  | ||
| compatible: "sifli,sf32lb-trng" | ||
|  | ||
| include: base.yaml | ||
|  | ||
| properties: | ||
| reg: | ||
| required: true | ||
|  | ||
| interrupts: | ||
| required: true | ||
|  | ||
| clocks: | ||
| required: true | 
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does TRNG feed registers with new random data when read? If not, returned data won't be random but repeat on rollover.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
generate seed before generate random