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