| 1 |
|
| 2 |
|
| 3 |
#include <delay.h> |
| 4 |
#include <device/mmio.h> |
| 5 |
#include <console/console.h> |
| 6 |
#include <console/uart.h> |
| 7 |
#include <soc/addressmap.h> |
| 8 |
#include <soc/otp.h> |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
struct sifive_otp_registers { |
| 18 |
u32 pa; |
| 19 |
u32 paio; |
| 20 |
u32 pas; |
| 21 |
u32 pce; |
| 22 |
u32 pclk; |
| 23 |
u32 pdin; |
| 24 |
u32 pdout; |
| 25 |
u32 pdstb; |
| 26 |
u32 pprog; |
| 27 |
u32 ptc; |
| 28 |
u32 ptm; |
| 29 |
u32 ptm_rep; |
| 30 |
u32 ptr; |
| 31 |
u32 ptrim; |
| 32 |
u32 pwe; |
| 33 |
} __packed; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
u32 otp_read_word(u16 idx) |
| 42 |
{ |
| 43 |
u32 w; |
| 44 |
|
| 45 |
if (idx >= 0x1000) |
| 46 |
die("otp: idx out of bounds"); |
| 47 |
|
| 48 |
struct sifive_otp_registers *regs = (void *)(FU740_OTP); |
| 49 |
|
| 50 |
|
| 51 |
write32(®s->pdstb, 0x01); |
| 52 |
|
| 53 |
|
| 54 |
write32(®s->ptrim, 0x01); |
| 55 |
|
| 56 |
|
| 57 |
write32(®s->pce, 0x01); |
| 58 |
|
| 59 |
|
| 60 |
write32(®s->pa, idx); |
| 61 |
|
| 62 |
|
| 63 |
write32(®s->pclk, 0x01); |
| 64 |
mdelay(1); |
| 65 |
|
| 66 |
write32(®s->pclk, 0x00); |
| 67 |
mdelay(1); |
| 68 |
|
| 69 |
w = read32(®s->pdout); |
| 70 |
|
| 71 |
|
| 72 |
write32(®s->pce, 0x00); |
| 73 |
write32(®s->ptrim, 0x00); |
| 74 |
write32(®s->pdstb, 0x00); |
| 75 |
|
| 76 |
return w; |
| 77 |
} |
| 78 |
|
| 79 |
u32 otp_read_serial(void) |
| 80 |
{ |
| 81 |
u32 serial = 0; |
| 82 |
u32 serial_n = 0; |
| 83 |
for (int i = 0xfe; i > 0; i -= 2) { |
| 84 |
serial = otp_read_word(i); |
| 85 |
serial_n = otp_read_word(i+1); |
| 86 |
if (serial == ~serial_n) |
| 87 |
break; |
| 88 |
} |
| 89 |
return serial; |
| 90 |
} |