<p>Philipp Hug has uploaded this change for <strong>review</strong>.</p><p><a href="https://review.coreboot.org/27435">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">soc/sifive: driver for otp memory<br><br>Provides minimal functionality to read the SOC s/n from the NeoFuse OTP memory.<br><br>Change-Id: I14b010ad9958931e0a98a76f76090fd7c66f19a0<br>Signed-off-by: Philipp Hug <philipp@hug.cx><br>---<br>M src/soc/sifive/fu540/Makefile.inc<br>A src/soc/sifive/fu540/include/soc/otp.h<br>A src/soc/sifive/fu540/otp.c<br>3 files changed, 118 insertions(+), 0 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://review.coreboot.org:29418/coreboot refs/changes/35/27435/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/src/soc/sifive/fu540/Makefile.inc b/src/soc/sifive/fu540/Makefile.inc</span><br><span>index 8a2f3a6..6a0355e 100644</span><br><span>--- a/src/soc/sifive/fu540/Makefile.inc</span><br><span>+++ b/src/soc/sifive/fu540/Makefile.inc</span><br><span>@@ -19,10 +19,12 @@</span><br><span> </span><br><span> romstage-y += uart.c</span><br><span> romstage-y += media.c</span><br><span style="color: hsl(120, 100%, 40%);">+romstage-y += otp.c</span><br><span> </span><br><span> ramstage-y += uart.c</span><br><span> ramstage-y += media.c</span><br><span> ramstage-y += cbmem.c</span><br><span style="color: hsl(120, 100%, 40%);">+ramstage-y += otp.c</span><br><span> </span><br><span> CPPFLAGS_common += -Isrc/soc/sifive/fu540/include</span><br><span> </span><br><span>diff --git a/src/soc/sifive/fu540/include/soc/otp.h b/src/soc/sifive/fu540/include/soc/otp.h</span><br><span>new file mode 100644</span><br><span>index 0000000..027d57f</span><br><span>--- /dev/null</span><br><span>+++ b/src/soc/sifive/fu540/include/soc/otp.h</span><br><span>@@ -0,0 +1,22 @@</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+ * This file is part of the coreboot project.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Copyright 2018 Philipp Hug <philipp@hug.cx></span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This program is free software; you can redistribute it and/or modify</span><br><span style="color: hsl(120, 100%, 40%);">+ * it under the terms of the GNU General Public License as published by</span><br><span style="color: hsl(120, 100%, 40%);">+ * the Free Software Foundation; version 2 of the License.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This program is distributed in the hope that it will be useful,</span><br><span style="color: hsl(120, 100%, 40%);">+ * but WITHOUT ANY WARRANTY; without even the implied warranty of</span><br><span style="color: hsl(120, 100%, 40%);">+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span><br><span style="color: hsl(120, 100%, 40%);">+ * GNU General Public License for more details.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#ifndef __SOC_SIFIVE_HIFIVE_U_OTP_H__</span><br><span style="color: hsl(120, 100%, 40%);">+#define __SOC_SIFIVE_HIFIVE_U_OTP_H__</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+u32 otp_read_word(u8 idx);</span><br><span style="color: hsl(120, 100%, 40%);">+u32 otp_read_serial(void);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#endif /* __SOC_SIFIVE_HIFIFE_U_OTP_H__ */</span><br><span>diff --git a/src/soc/sifive/fu540/otp.c b/src/soc/sifive/fu540/otp.c</span><br><span>new file mode 100644</span><br><span>index 0000000..2326ce2</span><br><span>--- /dev/null</span><br><span>+++ b/src/soc/sifive/fu540/otp.c</span><br><span>@@ -0,0 +1,94 @@</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+ * This file is part of the coreboot project.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Copyright (C) 2018 Philipp Hug <philipp@hug.cx></span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This program is free software; you can redistribute it and/or modify</span><br><span style="color: hsl(120, 100%, 40%);">+ * it under the terms of the GNU General Public License as published by</span><br><span style="color: hsl(120, 100%, 40%);">+ * the Free Software Foundation; version 2 of the License.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This program is distributed in the hope that it will be useful,</span><br><span style="color: hsl(120, 100%, 40%);">+ * but WITHOUT ANY WARRANTY; without even the implied warranty of</span><br><span style="color: hsl(120, 100%, 40%);">+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span><br><span style="color: hsl(120, 100%, 40%);">+ * GNU General Public License for more details.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#include <stddef.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <delay.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <arch/barrier.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <arch/io.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <console/uart.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <soc/addressmap.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <soc/otp.h></span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+ * This is a driver for SiFive's one-time programmable memory,</span><br><span style="color: hsl(120, 100%, 40%);">+ * documented in the FU540 manual:</span><br><span style="color: hsl(120, 100%, 40%);">+ * https://www.sifive.com/documentation/chips/freedom-u540-c000-manual/</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+typedef struct sifive_otp_registers {</span><br><span style="color: hsl(120, 100%, 40%);">+        u32 pa;     /* Address input */</span><br><span style="color: hsl(120, 100%, 40%);">+        u32 paio;   /* Program address input */</span><br><span style="color: hsl(120, 100%, 40%);">+        u32 pas;    /* Program redundancy cell selection input */</span><br><span style="color: hsl(120, 100%, 40%);">+        u32 pce;    /* OTP Macro enable input */</span><br><span style="color: hsl(120, 100%, 40%);">+        u32 pclk;   /* Clock input */</span><br><span style="color: hsl(120, 100%, 40%);">+        u32 pdin;   /* Write data input */</span><br><span style="color: hsl(120, 100%, 40%);">+        u32 pdout;  /* Read data output */</span><br><span style="color: hsl(120, 100%, 40%);">+        u32 pdstb;  /* Deep standby mode enable input (active low) */</span><br><span style="color: hsl(120, 100%, 40%);">+        u32 pprog;  /* Program mode enable input */</span><br><span style="color: hsl(120, 100%, 40%);">+        u32 ptc;    /* Test column enable input */</span><br><span style="color: hsl(120, 100%, 40%);">+        u32 ptm;    /* Test mode enable input */</span><br><span style="color: hsl(120, 100%, 40%);">+        u32 ptm_rep;/* Repair function test mode enable input */</span><br><span style="color: hsl(120, 100%, 40%);">+        u32 ptr;    /* Test row enable inpu */</span><br><span style="color: hsl(120, 100%, 40%);">+        u32 ptrim;  /* Repair function enable input */</span><br><span style="color: hsl(120, 100%, 40%);">+        u32 pwe;    /* Write enable input (defines program cycle) */</span><br><span style="color: hsl(120, 100%, 40%);">+} __packed;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+u32 otp_read_word(u8 idx)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+        u32 w;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      struct sifive_otp_registers *regs = (void *)(FU540_OTP);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    // wake-up from stand-by</span><br><span style="color: hsl(120, 100%, 40%);">+      write32(&regs->pdstb, 0x01);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ // enable repair function</span><br><span style="color: hsl(120, 100%, 40%);">+     write32(&regs->ptrim, 0x01);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ // enable input</span><br><span style="color: hsl(120, 100%, 40%);">+       write32(&regs->pce, 0x01);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   // adress to read</span><br><span style="color: hsl(120, 100%, 40%);">+     write32(&regs->pa, idx);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     // cycle clock to read</span><br><span style="color: hsl(120, 100%, 40%);">+        write32(&regs->pclk, 0x01);</span><br><span style="color: hsl(120, 100%, 40%);">+    mdelay(1);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  write32(&regs->pclk, 0x00);</span><br><span style="color: hsl(120, 100%, 40%);">+    mdelay(1);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  w = read32(&regs->pdout);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    // shutdown</span><br><span style="color: hsl(120, 100%, 40%);">+   write32(&regs->pce, 0x00);</span><br><span style="color: hsl(120, 100%, 40%);">+     write32(&regs->ptrim, 0x00);</span><br><span style="color: hsl(120, 100%, 40%);">+   write32(&regs->pdstb, 0x00);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return w;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+u32 otp_read_serial(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+  u32 serial = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+       u32 serial_n = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+     for (int i = 0xfe; i > 0; i -= 2) {</span><br><span style="color: hsl(120, 100%, 40%);">+                serial = otp_read_word(i);</span><br><span style="color: hsl(120, 100%, 40%);">+            serial_n = otp_read_word(i+1);</span><br><span style="color: hsl(120, 100%, 40%);">+                if (serial == ~serial_n)</span><br><span style="color: hsl(120, 100%, 40%);">+                      break;</span><br><span style="color: hsl(120, 100%, 40%);">+        }</span><br><span style="color: hsl(120, 100%, 40%);">+     return serial;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span></span><br></pre><p>To view, visit <a href="https://review.coreboot.org/27435">change 27435</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://review.coreboot.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://review.coreboot.org/27435"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: coreboot </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>
<div style="display:none"> Gerrit-Change-Id: I14b010ad9958931e0a98a76f76090fd7c66f19a0 </div>
<div style="display:none"> Gerrit-Change-Number: 27435 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Philipp Hug <philipp@hug.cx> </div>