1*68fbcc34STong Ho /*
2*68fbcc34STong Ho * Xilinx eFuse/bbram CRC calculator
3*68fbcc34STong Ho *
4*68fbcc34STong Ho * Copyright (c) 2021 Xilinx Inc.
5*68fbcc34STong Ho *
6*68fbcc34STong Ho * Permission is hereby granted, free of charge, to any person obtaining a copy
7*68fbcc34STong Ho * of this software and associated documentation files (the "Software"), to deal
8*68fbcc34STong Ho * in the Software without restriction, including without limitation the rights
9*68fbcc34STong Ho * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10*68fbcc34STong Ho * copies of the Software, and to permit persons to whom the Software is
11*68fbcc34STong Ho * furnished to do so, subject to the following conditions:
12*68fbcc34STong Ho *
13*68fbcc34STong Ho * The above copyright notice and this permission notice shall be included in
14*68fbcc34STong Ho * all copies or substantial portions of the Software.
15*68fbcc34STong Ho *
16*68fbcc34STong Ho * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*68fbcc34STong Ho * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*68fbcc34STong Ho * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19*68fbcc34STong Ho * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*68fbcc34STong Ho * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*68fbcc34STong Ho * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22*68fbcc34STong Ho * THE SOFTWARE.
23*68fbcc34STong Ho */
24*68fbcc34STong Ho #include "qemu/osdep.h"
25*68fbcc34STong Ho #include "hw/nvram/xlnx-efuse.h"
26*68fbcc34STong Ho
xlnx_efuse_u37_crc(uint32_t prev_crc,uint32_t data,uint32_t addr)27*68fbcc34STong Ho static uint32_t xlnx_efuse_u37_crc(uint32_t prev_crc, uint32_t data,
28*68fbcc34STong Ho uint32_t addr)
29*68fbcc34STong Ho {
30*68fbcc34STong Ho /* A table for 7-bit slicing */
31*68fbcc34STong Ho static const uint32_t crc_tab[128] = {
32*68fbcc34STong Ho 0x00000000, 0xe13b70f7, 0xc79a971f, 0x26a1e7e8,
33*68fbcc34STong Ho 0x8ad958cf, 0x6be22838, 0x4d43cfd0, 0xac78bf27,
34*68fbcc34STong Ho 0x105ec76f, 0xf165b798, 0xd7c45070, 0x36ff2087,
35*68fbcc34STong Ho 0x9a879fa0, 0x7bbcef57, 0x5d1d08bf, 0xbc267848,
36*68fbcc34STong Ho 0x20bd8ede, 0xc186fe29, 0xe72719c1, 0x061c6936,
37*68fbcc34STong Ho 0xaa64d611, 0x4b5fa6e6, 0x6dfe410e, 0x8cc531f9,
38*68fbcc34STong Ho 0x30e349b1, 0xd1d83946, 0xf779deae, 0x1642ae59,
39*68fbcc34STong Ho 0xba3a117e, 0x5b016189, 0x7da08661, 0x9c9bf696,
40*68fbcc34STong Ho 0x417b1dbc, 0xa0406d4b, 0x86e18aa3, 0x67dafa54,
41*68fbcc34STong Ho 0xcba24573, 0x2a993584, 0x0c38d26c, 0xed03a29b,
42*68fbcc34STong Ho 0x5125dad3, 0xb01eaa24, 0x96bf4dcc, 0x77843d3b,
43*68fbcc34STong Ho 0xdbfc821c, 0x3ac7f2eb, 0x1c661503, 0xfd5d65f4,
44*68fbcc34STong Ho 0x61c69362, 0x80fde395, 0xa65c047d, 0x4767748a,
45*68fbcc34STong Ho 0xeb1fcbad, 0x0a24bb5a, 0x2c855cb2, 0xcdbe2c45,
46*68fbcc34STong Ho 0x7198540d, 0x90a324fa, 0xb602c312, 0x5739b3e5,
47*68fbcc34STong Ho 0xfb410cc2, 0x1a7a7c35, 0x3cdb9bdd, 0xdde0eb2a,
48*68fbcc34STong Ho 0x82f63b78, 0x63cd4b8f, 0x456cac67, 0xa457dc90,
49*68fbcc34STong Ho 0x082f63b7, 0xe9141340, 0xcfb5f4a8, 0x2e8e845f,
50*68fbcc34STong Ho 0x92a8fc17, 0x73938ce0, 0x55326b08, 0xb4091bff,
51*68fbcc34STong Ho 0x1871a4d8, 0xf94ad42f, 0xdfeb33c7, 0x3ed04330,
52*68fbcc34STong Ho 0xa24bb5a6, 0x4370c551, 0x65d122b9, 0x84ea524e,
53*68fbcc34STong Ho 0x2892ed69, 0xc9a99d9e, 0xef087a76, 0x0e330a81,
54*68fbcc34STong Ho 0xb21572c9, 0x532e023e, 0x758fe5d6, 0x94b49521,
55*68fbcc34STong Ho 0x38cc2a06, 0xd9f75af1, 0xff56bd19, 0x1e6dcdee,
56*68fbcc34STong Ho 0xc38d26c4, 0x22b65633, 0x0417b1db, 0xe52cc12c,
57*68fbcc34STong Ho 0x49547e0b, 0xa86f0efc, 0x8ecee914, 0x6ff599e3,
58*68fbcc34STong Ho 0xd3d3e1ab, 0x32e8915c, 0x144976b4, 0xf5720643,
59*68fbcc34STong Ho 0x590ab964, 0xb831c993, 0x9e902e7b, 0x7fab5e8c,
60*68fbcc34STong Ho 0xe330a81a, 0x020bd8ed, 0x24aa3f05, 0xc5914ff2,
61*68fbcc34STong Ho 0x69e9f0d5, 0x88d28022, 0xae7367ca, 0x4f48173d,
62*68fbcc34STong Ho 0xf36e6f75, 0x12551f82, 0x34f4f86a, 0xd5cf889d,
63*68fbcc34STong Ho 0x79b737ba, 0x988c474d, 0xbe2da0a5, 0x5f16d052
64*68fbcc34STong Ho };
65*68fbcc34STong Ho
66*68fbcc34STong Ho /*
67*68fbcc34STong Ho * eFuse calculation is shown here:
68*68fbcc34STong Ho * https://github.com/Xilinx/embeddedsw/blob/release-2019.2/lib/sw_services/xilskey/src/xilskey_utils.c#L1496
69*68fbcc34STong Ho *
70*68fbcc34STong Ho * Each u32 word is appended a 5-bit value, for a total of 37 bits; see:
71*68fbcc34STong Ho * https://github.com/Xilinx/embeddedsw/blob/release-2019.2/lib/sw_services/xilskey/src/xilskey_utils.c#L1356
72*68fbcc34STong Ho */
73*68fbcc34STong Ho uint32_t crc = prev_crc;
74*68fbcc34STong Ho const unsigned rshf = 7;
75*68fbcc34STong Ho const uint32_t im = (1 << rshf) - 1;
76*68fbcc34STong Ho const uint32_t rm = (1 << (32 - rshf)) - 1;
77*68fbcc34STong Ho const uint32_t i2 = (1 << 2) - 1;
78*68fbcc34STong Ho const uint32_t r2 = (1 << 30) - 1;
79*68fbcc34STong Ho
80*68fbcc34STong Ho unsigned j;
81*68fbcc34STong Ho uint32_t i, r;
82*68fbcc34STong Ho uint64_t w;
83*68fbcc34STong Ho
84*68fbcc34STong Ho w = (uint64_t)(addr) << 32;
85*68fbcc34STong Ho w |= data;
86*68fbcc34STong Ho
87*68fbcc34STong Ho /* Feed 35 bits, in 5 rounds, each a slice of 7 bits */
88*68fbcc34STong Ho for (j = 0; j < 5; j++) {
89*68fbcc34STong Ho r = rm & (crc >> rshf);
90*68fbcc34STong Ho i = im & (crc ^ w);
91*68fbcc34STong Ho crc = crc_tab[i] ^ r;
92*68fbcc34STong Ho
93*68fbcc34STong Ho w >>= rshf;
94*68fbcc34STong Ho }
95*68fbcc34STong Ho
96*68fbcc34STong Ho /* Feed the remaining 2 bits */
97*68fbcc34STong Ho r = r2 & (crc >> 2);
98*68fbcc34STong Ho i = i2 & (crc ^ w);
99*68fbcc34STong Ho crc = crc_tab[i << (rshf - 2)] ^ r;
100*68fbcc34STong Ho
101*68fbcc34STong Ho return crc;
102*68fbcc34STong Ho }
103*68fbcc34STong Ho
xlnx_efuse_calc_crc(const uint32_t * data,unsigned u32_cnt,unsigned zpads)104*68fbcc34STong Ho uint32_t xlnx_efuse_calc_crc(const uint32_t *data, unsigned u32_cnt,
105*68fbcc34STong Ho unsigned zpads)
106*68fbcc34STong Ho {
107*68fbcc34STong Ho uint32_t crc = 0;
108*68fbcc34STong Ho unsigned index;
109*68fbcc34STong Ho
110*68fbcc34STong Ho for (index = zpads; index; index--) {
111*68fbcc34STong Ho crc = xlnx_efuse_u37_crc(crc, 0, (index + u32_cnt));
112*68fbcc34STong Ho }
113*68fbcc34STong Ho
114*68fbcc34STong Ho for (index = u32_cnt; index; index--) {
115*68fbcc34STong Ho crc = xlnx_efuse_u37_crc(crc, data[index - 1], index);
116*68fbcc34STong Ho }
117*68fbcc34STong Ho
118*68fbcc34STong Ho return crc;
119*68fbcc34STong Ho }
120