xref: /qemu/include/hw/nvram/xlnx-efuse.h (revision 65cb7129f4160c7e07a0da107f888ec73ae96776)
168fbcc34STong Ho /*
268fbcc34STong Ho  * QEMU model of the Xilinx eFuse core
368fbcc34STong Ho  *
468fbcc34STong Ho  * Copyright (c) 2015 Xilinx Inc.
568fbcc34STong Ho  *
668fbcc34STong Ho  * Written by Edgar E. Iglesias <edgari@xilinx.com>
768fbcc34STong Ho  *
868fbcc34STong Ho  * Permission is hereby granted, free of charge, to any person obtaining a copy
968fbcc34STong Ho  * of this software and associated documentation files (the "Software"), to deal
1068fbcc34STong Ho  * in the Software without restriction, including without limitation the rights
1168fbcc34STong Ho  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1268fbcc34STong Ho  * copies of the Software, and to permit persons to whom the Software is
1368fbcc34STong Ho  * furnished to do so, subject to the following conditions:
1468fbcc34STong Ho  *
1568fbcc34STong Ho  * The above copyright notice and this permission notice shall be included in
1668fbcc34STong Ho  * all copies or substantial portions of the Software.
1768fbcc34STong Ho  *
1868fbcc34STong Ho  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1968fbcc34STong Ho  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2068fbcc34STong Ho  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
2168fbcc34STong Ho  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2268fbcc34STong Ho  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2368fbcc34STong Ho  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2468fbcc34STong Ho  * THE SOFTWARE.
2568fbcc34STong Ho  */
2668fbcc34STong Ho 
2768fbcc34STong Ho #ifndef XLNX_EFUSE_H
2868fbcc34STong Ho #define XLNX_EFUSE_H
2968fbcc34STong Ho 
30*32cad1ffSPhilippe Mathieu-Daudé #include "system/block-backend.h"
3168fbcc34STong Ho #include "hw/qdev-core.h"
3268fbcc34STong Ho 
33c455e011SMarkus Armbruster #define TYPE_XLNX_EFUSE "xlnx-efuse"
3468fbcc34STong Ho OBJECT_DECLARE_SIMPLE_TYPE(XlnxEFuse, XLNX_EFUSE);
3568fbcc34STong Ho 
3668fbcc34STong Ho struct XlnxEFuse {
3768fbcc34STong Ho     DeviceState parent_obj;
3868fbcc34STong Ho     BlockBackend *blk;
3968fbcc34STong Ho     bool blk_ro;
4068fbcc34STong Ho     uint32_t *fuse32;
4168fbcc34STong Ho 
4268fbcc34STong Ho     DeviceState *dev;
4368fbcc34STong Ho 
4468fbcc34STong Ho     bool init_tbits;
4568fbcc34STong Ho 
4668fbcc34STong Ho     uint8_t efuse_nr;
4768fbcc34STong Ho     uint32_t efuse_size;
4868fbcc34STong Ho 
4968fbcc34STong Ho     uint32_t *ro_bits;
5068fbcc34STong Ho     uint32_t ro_bits_cnt;
5168fbcc34STong Ho };
5268fbcc34STong Ho 
5368fbcc34STong Ho /**
5468fbcc34STong Ho  * xlnx_efuse_calc_crc:
5568fbcc34STong Ho  * @data: an array of 32-bit words for which the CRC should be computed
5668fbcc34STong Ho  * @u32_cnt: the array size in number of 32-bit words
5768fbcc34STong Ho  * @zpads: the number of 32-bit zeros prepended to @data before computation
5868fbcc34STong Ho  *
5968fbcc34STong Ho  * This function is used to compute the CRC for an array of 32-bit words,
6068fbcc34STong Ho  * using a Xilinx-specific data padding.
6168fbcc34STong Ho  *
6268fbcc34STong Ho  * Returns: the computed 32-bit CRC
6368fbcc34STong Ho  */
6468fbcc34STong Ho uint32_t xlnx_efuse_calc_crc(const uint32_t *data, unsigned u32_cnt,
6568fbcc34STong Ho                              unsigned zpads);
6668fbcc34STong Ho 
6768fbcc34STong Ho /**
6868fbcc34STong Ho  * xlnx_efuse_get_bit:
6968fbcc34STong Ho  * @s: the efuse object
7068fbcc34STong Ho  * @bit: the efuse bit-address to read the data
7168fbcc34STong Ho  *
7268fbcc34STong Ho  * Returns: the bit, 0 or 1, at @bit of object @s
7368fbcc34STong Ho  */
7468fbcc34STong Ho bool xlnx_efuse_get_bit(XlnxEFuse *s, unsigned int bit);
7568fbcc34STong Ho 
7668fbcc34STong Ho /**
7768fbcc34STong Ho  * xlnx_efuse_set_bit:
7868fbcc34STong Ho  * @s: the efuse object
7968fbcc34STong Ho  * @bit: the efuse bit-address to be written a value of 1
8068fbcc34STong Ho  *
8168fbcc34STong Ho  * Returns: true on success, false on failure
8268fbcc34STong Ho  */
8368fbcc34STong Ho bool xlnx_efuse_set_bit(XlnxEFuse *s, unsigned int bit);
8468fbcc34STong Ho 
8568fbcc34STong Ho /**
8668fbcc34STong Ho  * xlnx_efuse_k256_check:
8768fbcc34STong Ho  * @s: the efuse object
8868fbcc34STong Ho  * @crc: the 32-bit CRC to be compared with
8968fbcc34STong Ho  * @start: the efuse bit-address (which must be multiple of 32) of the
9068fbcc34STong Ho  *         start of a 256-bit array
9168fbcc34STong Ho  *
9268fbcc34STong Ho  * This function computes the CRC of a 256-bit array starting at @start
9368fbcc34STong Ho  * then compares to the given @crc
9468fbcc34STong Ho  *
9568fbcc34STong Ho  * Returns: true of @crc == computed, false otherwise
9668fbcc34STong Ho  */
9768fbcc34STong Ho bool xlnx_efuse_k256_check(XlnxEFuse *s, uint32_t crc, unsigned start);
9868fbcc34STong Ho 
9968fbcc34STong Ho /**
10068fbcc34STong Ho  * xlnx_efuse_tbits_check:
10168fbcc34STong Ho  * @s: the efuse object
10268fbcc34STong Ho  *
10368fbcc34STong Ho  * This function inspects a number of efuse bits at specific addresses
10468fbcc34STong Ho  * to see if they match a validation pattern. Each pattern is a group
10568fbcc34STong Ho  * of 4 bits, and there are 3 groups.
10668fbcc34STong Ho  *
10768fbcc34STong Ho  * Returns: a 3-bit mask, where a bit of '1' means the corresponding
10868fbcc34STong Ho  * group has a valid pattern.
10968fbcc34STong Ho  */
11068fbcc34STong Ho uint32_t xlnx_efuse_tbits_check(XlnxEFuse *s);
11168fbcc34STong Ho 
11268fbcc34STong Ho /**
11368fbcc34STong Ho  * xlnx_efuse_get_row:
11468fbcc34STong Ho  * @s: the efuse object
11568fbcc34STong Ho  * @bit: the efuse bit address for which a 32-bit value is read
11668fbcc34STong Ho  *
11768fbcc34STong Ho  * Returns: the entire 32 bits of the efuse, starting at a bit
11868fbcc34STong Ho  * address that is multiple of 32 and contains the bit at @bit
11968fbcc34STong Ho  */
xlnx_efuse_get_row(XlnxEFuse * s,unsigned int bit)12068fbcc34STong Ho static inline uint32_t xlnx_efuse_get_row(XlnxEFuse *s, unsigned int bit)
12168fbcc34STong Ho {
12268fbcc34STong Ho     if (!(s->fuse32)) {
12368fbcc34STong Ho         return 0;
12468fbcc34STong Ho     } else {
12568fbcc34STong Ho         unsigned int row_idx = bit / 32;
12668fbcc34STong Ho 
12768fbcc34STong Ho         assert(row_idx < (s->efuse_size * s->efuse_nr / 32));
12868fbcc34STong Ho         return s->fuse32[row_idx];
12968fbcc34STong Ho     }
13068fbcc34STong Ho }
13168fbcc34STong Ho 
13268fbcc34STong Ho #endif
133