blob: e9d1cf9a4b63254760b735a9a616769f918dffe4 [file] [log] [blame]
Anup Patel3fda0262019-02-25 08:15:19 +00001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) 2019 Western Digital Corporation or its affiliates.
4 *
5 * Authors:
6 * Anup Patel <anup.patel@wdc.com>
7 */
8
9#include <common.h>
10#include <dm.h>
Simon Glass691d7192020-05-10 11:40:02 -060011#include <init.h>
Anup Patel9f2a0c02019-06-25 06:31:44 +000012#include <linux/delay.h>
13#include <linux/io.h>
14
15#ifdef CONFIG_MISC_INIT_R
16
17#define FU540_OTP_BASE_ADDR 0x10070000
18
19struct fu540_otp_regs {
20 u32 pa; /* Address input */
21 u32 paio; /* Program address input */
22 u32 pas; /* Program redundancy cell selection input */
23 u32 pce; /* OTP Macro enable input */
24 u32 pclk; /* Clock input */
25 u32 pdin; /* Write data input */
26 u32 pdout; /* Read data output */
27 u32 pdstb; /* Deep standby mode enable input (active low) */
28 u32 pprog; /* Program mode enable input */
29 u32 ptc; /* Test column enable input */
30 u32 ptm; /* Test mode enable input */
31 u32 ptm_rep;/* Repair function test mode enable input */
32 u32 ptr; /* Test row enable input */
33 u32 ptrim; /* Repair function enable input */
34 u32 pwe; /* Write enable input (defines program cycle) */
35} __packed;
36
37#define BYTES_PER_FUSE 4
38#define NUM_FUSES 0x1000
39
40static int fu540_otp_read(int offset, void *buf, int size)
41{
42 struct fu540_otp_regs *regs = (void __iomem *)FU540_OTP_BASE_ADDR;
43 unsigned int i;
44 int fuseidx = offset / BYTES_PER_FUSE;
45 int fusecount = size / BYTES_PER_FUSE;
46 u32 fusebuf[fusecount];
47
48 /* check bounds */
49 if (offset < 0 || size < 0)
50 return -EINVAL;
51 if (fuseidx >= NUM_FUSES)
52 return -EINVAL;
53 if ((fuseidx + fusecount) > NUM_FUSES)
54 return -EINVAL;
55
56 /* init OTP */
57 writel(0x01, &regs->pdstb); /* wake up from stand-by */
58 writel(0x01, &regs->ptrim); /* enable repair function */
59 writel(0x01, &regs->pce); /* enable input */
60
61 /* read all requested fuses */
62 for (i = 0; i < fusecount; i++, fuseidx++) {
63 writel(fuseidx, &regs->pa);
64
65 /* cycle clock to read */
66 writel(0x01, &regs->pclk);
67 mdelay(1);
68 writel(0x00, &regs->pclk);
69 mdelay(1);
70
71 /* read the value */
72 fusebuf[i] = readl(&regs->pdout);
73 }
74
75 /* shut down */
76 writel(0, &regs->pce);
77 writel(0, &regs->ptrim);
78 writel(0, &regs->pdstb);
79
80 /* copy out */
81 memcpy(buf, fusebuf, size);
82
83 return 0;
84}
85
86static u32 fu540_read_serialnum(void)
87{
88 int ret;
89 u32 serial[2] = {0};
90
91 for (int i = 0xfe * 4; i > 0; i -= 8) {
92 ret = fu540_otp_read(i, serial, sizeof(serial));
93 if (ret) {
94 printf("%s: error reading from OTP\n", __func__);
95 break;
96 }
97 if (serial[0] == ~serial[1])
98 return serial[0];
99 }
100
101 return 0;
102}
103
104static void fu540_setup_macaddr(u32 serialnum)
105{
106 /* Default MAC address */
107 unsigned char mac[6] = { 0x70, 0xb3, 0xd5, 0x92, 0xf0, 0x00 };
108
109 /*
110 * We derive our board MAC address by ORing last three bytes
111 * of board serial number to above default MAC address.
112 *
113 * This logic of deriving board MAC address is taken from
114 * SiFive FSBL and is kept unchanged.
115 */
116 mac[5] |= (serialnum >> 0) & 0xff;
117 mac[4] |= (serialnum >> 8) & 0xff;
118 mac[3] |= (serialnum >> 16) & 0xff;
119
120 /* Update environment variable */
121 eth_env_set_enetaddr("ethaddr", mac);
122}
123
124int misc_init_r(void)
125{
Sagar Shrikant Kadamcba06352019-08-12 07:57:40 -0700126 u32 serial_num;
127 char buf[9] = {0};
Anup Patel9f2a0c02019-06-25 06:31:44 +0000128
Sagar Shrikant Kadamcba06352019-08-12 07:57:40 -0700129 /* Set ethaddr environment variable from board serial number */
130 if (!env_get("serial#")) {
131 serial_num = fu540_read_serialnum();
132 if (!serial_num) {
133 WARN(true, "Board serial number should not be 0 !!\n");
134 return 0;
135 }
136 snprintf(buf, sizeof(buf), "%08x", serial_num);
137 env_set("serial#", buf);
138 fu540_setup_macaddr(serial_num);
139 }
Anup Patel9f2a0c02019-06-25 06:31:44 +0000140 return 0;
141}
142
143#endif
Anup Patel3fda0262019-02-25 08:15:19 +0000144
145int board_init(void)
146{
147 /* For now nothing to do here. */
148
149 return 0;
150}