|
| 1 | +/**************************************************************************** |
| 2 | + * arch/risc-v/src/common/espressif/esp_efuse.c |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 7 | + * contributor license agreements. See the NOTICE file distributed with |
| 8 | + * this work for additional information regarding copyright ownership. The |
| 9 | + * ASF licenses this file to you under the Apache License, Version 2.0 (the |
| 10 | + * "License"); you may not use this file except in compliance with the |
| 11 | + * License. You may obtain a copy of the License at |
| 12 | + * |
| 13 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | + * |
| 15 | + * Unless required by applicable law or agreed to in writing, software |
| 16 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 18 | + * License for the specific language governing permissions and limitations |
| 19 | + * under the License. |
| 20 | + * |
| 21 | + ****************************************************************************/ |
| 22 | + |
| 23 | +/**************************************************************************** |
| 24 | + * Included Files |
| 25 | + ****************************************************************************/ |
| 26 | + |
| 27 | +#include <stdlib.h> |
| 28 | +#include <debug.h> |
| 29 | +#include <debug.h> |
| 30 | +#include <errno.h> |
| 31 | +#include <assert.h> |
| 32 | +#include <string.h> |
| 33 | +#include <sys/param.h> |
| 34 | +#include <nuttx/efuse/efuse.h> |
| 35 | +#include <nuttx/irq.h> |
| 36 | +#include <nuttx/kmalloc.h> |
| 37 | +#include <nuttx/efuse/efuse.h> |
| 38 | + |
| 39 | +#include "chip.h" |
| 40 | + |
| 41 | +#include "riscv_internal.h" |
| 42 | +#include "espressif/esp_efuse.h" |
| 43 | + |
| 44 | +#include "esp_efuse.h" |
| 45 | +#include "esp_clk.h" |
| 46 | +#include "hal/efuse_hal.h" |
| 47 | +#include "esp_efuse_table.h" |
| 48 | +#include "esp_efuse_chip.h" |
| 49 | + |
| 50 | +/**************************************************************************** |
| 51 | + * Private Types |
| 52 | + ****************************************************************************/ |
| 53 | + |
| 54 | +struct esp_efuse_lowerhalf_s |
| 55 | +{ |
| 56 | + const struct efuse_ops_s *ops; /* Lower half operations */ |
| 57 | + void *upper; /* Pointer to efuse_upperhalf_s */ |
| 58 | +}; |
| 59 | + |
| 60 | +/**************************************************************************** |
| 61 | + * Private Functions Prototypes |
| 62 | + ****************************************************************************/ |
| 63 | + |
| 64 | +/* "Lower half" driver methods */ |
| 65 | + |
| 66 | +static int esp_efuse_lowerhalf_read(struct efuse_lowerhalf_s *lower, |
| 67 | + const efuse_desc_t *field[], |
| 68 | + uint8_t *data, size_t bits_len); |
| 69 | +static int esp_efuse_lowerhalf_write(struct efuse_lowerhalf_s *lower, |
| 70 | + const efuse_desc_t *field[], |
| 71 | + const uint8_t *data, |
| 72 | + size_t bits_len); |
| 73 | +static int esp_efuse_lowerhalf_ioctl(struct efuse_lowerhalf_s *lower, |
| 74 | + int cmd, unsigned long arg); |
| 75 | + |
| 76 | +/**************************************************************************** |
| 77 | + * Private Data |
| 78 | + ****************************************************************************/ |
| 79 | + |
| 80 | +/* "Lower half" driver methods */ |
| 81 | + |
| 82 | +static const struct efuse_ops_s g_esp_efuse_ops = |
| 83 | +{ |
| 84 | + .read_field = esp_efuse_lowerhalf_read, |
| 85 | + .write_field = esp_efuse_lowerhalf_write, |
| 86 | + .ioctl = esp_efuse_lowerhalf_ioctl, |
| 87 | +}; |
| 88 | + |
| 89 | +/* EFUSE lower-half */ |
| 90 | + |
| 91 | +static struct esp_efuse_lowerhalf_s g_esp_efuse_lowerhalf = |
| 92 | +{ |
| 93 | + .ops = &g_esp_efuse_ops, |
| 94 | + .upper = NULL, |
| 95 | +}; |
| 96 | + |
| 97 | +/**************************************************************************** |
| 98 | + * Private functions |
| 99 | + ****************************************************************************/ |
| 100 | + |
| 101 | +/**************************************************************************** |
| 102 | + * Name: esp_efuse_lowerhalf_read |
| 103 | + * |
| 104 | + * Description: |
| 105 | + * Read value from EFUSE, writing it into an array. |
| 106 | + * |
| 107 | + * Input Parameters: |
| 108 | + * lower - A pointer the publicly visible representation of |
| 109 | + * the "lower-half" driver state structure |
| 110 | + * field - A pointer to describing the fields of efuse |
| 111 | + * dst - A pointer to array that contains the data for reading |
| 112 | + * bits_len - The number of bits required to read |
| 113 | + * |
| 114 | + * Returned Value: |
| 115 | + * Zero (OK) is returned on success. Otherwise -1 (ERROR). |
| 116 | + * |
| 117 | + ****************************************************************************/ |
| 118 | + |
| 119 | +static int esp_efuse_lowerhalf_read(struct efuse_lowerhalf_s *lower, |
| 120 | + const efuse_desc_t *field[], |
| 121 | + uint8_t *data, size_t bits_len) |
| 122 | +{ |
| 123 | + int ret = OK; |
| 124 | + |
| 125 | + /* Read the requested field */ |
| 126 | + |
| 127 | + ret = esp_efuse_read_field_blob((const esp_efuse_desc_t**)field, |
| 128 | + data, |
| 129 | + bits_len); |
| 130 | + |
| 131 | + return ret; |
| 132 | +} |
| 133 | + |
| 134 | +/**************************************************************************** |
| 135 | + * Name: esp_efuse_lowerhalf_write |
| 136 | + * |
| 137 | + * Description: |
| 138 | + * Write array to EFUSE. |
| 139 | + * |
| 140 | + * Input Parameters: |
| 141 | + * lower - A pointer the publicly visible representation of |
| 142 | + * the "lower-half" driver state structure |
| 143 | + * field - A pointer to describing the fields of efuse |
| 144 | + * data - A pointer to array that contains the data for writing |
| 145 | + * bits_len - The number of bits required to write |
| 146 | + * |
| 147 | + * Returned Value: |
| 148 | + * Zero (OK) is returned on success. Otherwise -1 (ERROR). |
| 149 | + * |
| 150 | + ****************************************************************************/ |
| 151 | + |
| 152 | +static int esp_efuse_lowerhalf_write(struct efuse_lowerhalf_s *lower, |
| 153 | + const efuse_desc_t *field[], |
| 154 | + const uint8_t *data, |
| 155 | + size_t bits_len) |
| 156 | +{ |
| 157 | + irqstate_t flags; |
| 158 | + int ret = OK; |
| 159 | + |
| 160 | + flags = enter_critical_section(); |
| 161 | + |
| 162 | + ret = esp_efuse_write_field_blob((const esp_efuse_desc_t**)field, |
| 163 | + data, |
| 164 | + bits_len); |
| 165 | + |
| 166 | + leave_critical_section(flags); |
| 167 | + |
| 168 | + if (ret != OK) |
| 169 | + { |
| 170 | + return ERROR; |
| 171 | + } |
| 172 | + |
| 173 | + return ret; |
| 174 | +} |
| 175 | + |
| 176 | +/**************************************************************************** |
| 177 | + * Name: esp_efuse_lowerhalf_ioctl |
| 178 | + * |
| 179 | + * Description: |
| 180 | + * Initialize the efuse driver. The efuse is initialized |
| 181 | + * and registered as 'devpath'. |
| 182 | + * |
| 183 | + * Input Parameters: |
| 184 | + * lower - A pointer the publicly visible representation of |
| 185 | + * the "lower-half" driver state structure |
| 186 | + * cmd - The ioctl command value |
| 187 | + * arg - The optional argument that accompanies the 'cmd' |
| 188 | + * |
| 189 | + * Returned Value: |
| 190 | + * Zero (OK) is returned on success. Otherwise -1 (ERROR). |
| 191 | + * |
| 192 | + ****************************************************************************/ |
| 193 | + |
| 194 | +static int esp_efuse_lowerhalf_ioctl(struct efuse_lowerhalf_s *lower, |
| 195 | + int cmd, unsigned long arg) |
| 196 | +{ |
| 197 | + int ret = OK; |
| 198 | + |
| 199 | + switch (cmd) |
| 200 | + { |
| 201 | + /* We don't have proprietary EFUSE ioctls */ |
| 202 | + |
| 203 | + default: |
| 204 | + { |
| 205 | + minfo("Unrecognized cmd: %d\n", cmd); |
| 206 | + ret = -ENOTTY; |
| 207 | + } |
| 208 | + break; |
| 209 | + } |
| 210 | + |
| 211 | + return ret; |
| 212 | +} |
| 213 | + |
| 214 | +/**************************************************************************** |
| 215 | + * Public Functions |
| 216 | + ****************************************************************************/ |
| 217 | + |
| 218 | +/**************************************************************************** |
| 219 | + * Name: esp_efuse_initialize |
| 220 | + * |
| 221 | + * Description: |
| 222 | + * Initialize the efuse driver. The efuse is initialized |
| 223 | + * and registered as 'devpath'. |
| 224 | + * |
| 225 | + * Input Parameters: |
| 226 | + * devpath - The full path to the efuse device. |
| 227 | + * This should be of the form /dev/efuse |
| 228 | + * |
| 229 | + * Returned Value: |
| 230 | + * Zero (OK) is returned on success. Otherwise -1 (ERROR). |
| 231 | + * |
| 232 | + ****************************************************************************/ |
| 233 | + |
| 234 | +int esp_efuse_initialize(const char *devpath) |
| 235 | +{ |
| 236 | + struct esp_efuse_lowerhalf_s *lower = NULL; |
| 237 | + int ret = OK; |
| 238 | + |
| 239 | + DEBUGASSERT(devpath != NULL); |
| 240 | + |
| 241 | + lower = &g_esp_efuse_lowerhalf; |
| 242 | + |
| 243 | + /* Register the efuse upper driver */ |
| 244 | + |
| 245 | + lower->upper = efuse_register(devpath, |
| 246 | + (struct efuse_lowerhalf_s *)lower); |
| 247 | + |
| 248 | + if (lower->upper == NULL) |
| 249 | + { |
| 250 | + /* The actual cause of the failure may have been a failure to allocate |
| 251 | + * perhaps a failure to register the efuse driver (such as if the |
| 252 | + * 'devpath' were not unique). We know here but we return EEXIST to |
| 253 | + * indicate the failure (implying the non-unique devpath). |
| 254 | + */ |
| 255 | + |
| 256 | + ret = -EEXIST; |
| 257 | + } |
| 258 | + |
| 259 | + return ret; |
| 260 | +} |
0 commit comments