1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2019 Western Digital Corporation or its affiliates.
6 * Anup Patel <anup.patel@wdc.com>
13 #include <linux/delay.h>
16 #ifdef CONFIG_MISC_INIT_R
18 #define FU540_OTP_BASE_ADDR 0x10070000
20 struct fu540_otp_regs {
21 u32 pa; /* Address input */
22 u32 paio; /* Program address input */
23 u32 pas; /* Program redundancy cell selection input */
24 u32 pce; /* OTP Macro enable input */
25 u32 pclk; /* Clock input */
26 u32 pdin; /* Write data input */
27 u32 pdout; /* Read data output */
28 u32 pdstb; /* Deep standby mode enable input (active low) */
29 u32 pprog; /* Program mode enable input */
30 u32 ptc; /* Test column enable input */
31 u32 ptm; /* Test mode enable input */
32 u32 ptm_rep;/* Repair function test mode enable input */
33 u32 ptr; /* Test row enable input */
34 u32 ptrim; /* Repair function enable input */
35 u32 pwe; /* Write enable input (defines program cycle) */
38 #define BYTES_PER_FUSE 4
39 #define NUM_FUSES 0x1000
41 static int fu540_otp_read(int offset, void *buf, int size)
43 struct fu540_otp_regs *regs = (void __iomem *)FU540_OTP_BASE_ADDR;
45 int fuseidx = offset / BYTES_PER_FUSE;
46 int fusecount = size / BYTES_PER_FUSE;
47 u32 fusebuf[fusecount];
50 if (offset < 0 || size < 0)
52 if (fuseidx >= NUM_FUSES)
54 if ((fuseidx + fusecount) > NUM_FUSES)
58 writel(0x01, ®s->pdstb); /* wake up from stand-by */
59 writel(0x01, ®s->ptrim); /* enable repair function */
60 writel(0x01, ®s->pce); /* enable input */
62 /* read all requested fuses */
63 for (i = 0; i < fusecount; i++, fuseidx++) {
64 writel(fuseidx, ®s->pa);
66 /* cycle clock to read */
67 writel(0x01, ®s->pclk);
69 writel(0x00, ®s->pclk);
73 fusebuf[i] = readl(®s->pdout);
77 writel(0, ®s->pce);
78 writel(0, ®s->ptrim);
79 writel(0, ®s->pdstb);
82 memcpy(buf, fusebuf, size);
87 static u32 fu540_read_serialnum(void)
92 for (int i = 0xfe * 4; i > 0; i -= 8) {
93 ret = fu540_otp_read(i, serial, sizeof(serial));
95 printf("%s: error reading from OTP\n", __func__);
98 if (serial[0] == ~serial[1])
105 static void fu540_setup_macaddr(u32 serialnum)
107 /* Default MAC address */
108 unsigned char mac[6] = { 0x70, 0xb3, 0xd5, 0x92, 0xf0, 0x00 };
111 * We derive our board MAC address by ORing last three bytes
112 * of board serial number to above default MAC address.
114 * This logic of deriving board MAC address is taken from
115 * SiFive FSBL and is kept unchanged.
117 mac[5] |= (serialnum >> 0) & 0xff;
118 mac[4] |= (serialnum >> 8) & 0xff;
119 mac[3] |= (serialnum >> 16) & 0xff;
121 /* Update environment variable */
122 eth_env_set_enetaddr("ethaddr", mac);
125 int misc_init_r(void)
130 /* Set ethaddr environment variable from board serial number */
131 if (!env_get("serial#")) {
132 serial_num = fu540_read_serialnum();
134 WARN(true, "Board serial number should not be 0 !!\n");
137 snprintf(buf, sizeof(buf), "%08x", serial_num);
138 env_set("serial#", buf);
139 fu540_setup_macaddr(serial_num);
148 /* For now nothing to do here. */