From patchwork Tue Jun 25 06:31:44 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anup Patel X-Patchwork-Id: 1121811 X-Patchwork-Delegate: joe.hershberger@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=wdc.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=wdc.com header.i=@wdc.com header.b="o5q+ylUI"; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=sharedspace.onmicrosoft.com header.i=@sharedspace.onmicrosoft.com header.b="jfqC7U1J"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 45XxFh0tFgz9s7h for ; Tue, 25 Jun 2019 16:33:59 +1000 (AEST) Received: by lists.denx.de (Postfix, from userid 105) id B2839C21DA2; Tue, 25 Jun 2019 06:32:57 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id D417AC21CB1; Tue, 25 Jun 2019 06:32:19 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 9E9A9C21DB5; Tue, 25 Jun 2019 06:31:58 +0000 (UTC) Received: from esa1.hgst.iphmx.com (esa1.hgst.iphmx.com [68.232.141.245]) by lists.denx.de (Postfix) with ESMTPS id 0A7ADC21D65 for ; Tue, 25 Jun 2019 06:31:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=wdc.com; i=@wdc.com; q=dns/txt; s=dkim.wdc.com; t=1561444310; x=1592980310; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=L8raPo2WbPdaF2zgKKZn6kQEhFcL1DqnH4O5GIYmx3o=; b=o5q+ylUIuqEcTQ1RtsTc7QeJxSY+0yzyGThM6+AWK3AsEPqxgjitdNae fp9gQwYAEW6CtFY7+TtR8msYuOT1srt4Wd/vg1Xtq2x2tyLuxAi+tA/BU uJTPLO098r8EVbwx8OPJ8F8oqAfdRvIxmBXauVnLyFHfEQkhlczktkKfM D6iJC3wdbfHzbsGb4i+PDTDEDZpAOTfCy+Jeuzr7VDI+NFR4Nx4prJJft 39IN9fxZRSh7Q/BR6lqytkDpq0iX1/jiESBbV9eECgroP2zLnvWldYF1s HC0FQvCIuogRs9iZYSunmlsWCR+xCdn7BC+b37HSHjbd+rtKA5dP0I6xt Q==; X-IronPort-AV: E=Sophos;i="5.63,415,1557158400"; d="scan'208";a="217829289" Received: from mail-sn1nam02lp2052.outbound.protection.outlook.com (HELO NAM02-SN1-obe.outbound.protection.outlook.com) ([104.47.36.52]) by ob1.hgst.iphmx.com with ESMTP; 25 Jun 2019 14:31:48 +0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sharedspace.onmicrosoft.com; s=selector2-sharedspace-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=L8raPo2WbPdaF2zgKKZn6kQEhFcL1DqnH4O5GIYmx3o=; b=jfqC7U1JGDBRv/KOYz5S0HmrSIecx+WTe9Ffb4J1AyLHkCDmdyo8gi5HvgObi5aHpnSt0uO5OwpUXPhFvl19G8b0FjZnAFDWRYODmAqNg7Z7qqgAjzXvM4p1Co2JAk0CDKC/QD0l6m4UHRZCuy9ELEmcRGzK8JaqGZcd4efzqW4= Received: from MN2PR04MB6061.namprd04.prod.outlook.com (20.178.246.15) by MN2PR04MB6959.namprd04.prod.outlook.com (10.186.146.201) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2008.16; Tue, 25 Jun 2019 06:31:45 +0000 Received: from MN2PR04MB6061.namprd04.prod.outlook.com ([fe80::84da:b4e7:4612:48b]) by MN2PR04MB6061.namprd04.prod.outlook.com ([fe80::84da:b4e7:4612:48b%7]) with mapi id 15.20.2008.014; Tue, 25 Jun 2019 06:31:45 +0000 From: Anup Patel To: Rick Chen , Bin Meng , Lukas Auer , Simon Glass Thread-Topic: [PATCH v9 7/9] riscv: sifive: fu540: Setup ethaddr env variable using OTP Thread-Index: AQHVKx+oI7Bp59Tp1UOTl+ddWA4zNA== Date: Tue, 25 Jun 2019 06:31:44 +0000 Message-ID: <20190625063030.6200-8-anup.patel@wdc.com> References: <20190625063030.6200-1-anup.patel@wdc.com> In-Reply-To: <20190625063030.6200-1-anup.patel@wdc.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-clientproxiedby: MA1PR01CA0165.INDPRD01.PROD.OUTLOOK.COM (2603:1096:a00:71::35) To MN2PR04MB6061.namprd04.prod.outlook.com (2603:10b6:208:d8::15) authentication-results: spf=none (sender IP is ) smtp.mailfrom=Anup.Patel@wdc.com; x-ms-exchange-messagesentrepresentingtype: 1 x-mailer: git-send-email 2.17.1 x-originating-ip: [129.253.179.161] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 36c1585e-c7bd-4085-4208-08d6f936cab5 x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: BCL:0; PCL:0; RULEID:(2390118)(7020095)(4652040)(8989299)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(5600148)(711020)(4605104)(1401327)(4618075)(2017052603328)(7193020); SRVR:MN2PR04MB6959; x-ms-traffictypediagnostic: MN2PR04MB6959: wdcipoutbound: EOP-TRUE x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:2150; x-forefront-prvs: 0079056367 x-forefront-antispam-report: SFV:NSPM; SFS:(10019020)(366004)(396003)(39860400002)(376002)(346002)(136003)(199004)(189003)(40224003)(4326008)(14454004)(6436002)(53936002)(7736002)(25786009)(5660300002)(72206003)(6486002)(3846002)(486006)(256004)(14444005)(76176011)(478600001)(54906003)(316002)(110136005)(6512007)(476003)(446003)(11346002)(2616005)(44832011)(36756003)(86362001)(66066001)(81166006)(186003)(6116002)(66946007)(73956011)(81156014)(7416002)(2906002)(66476007)(8676002)(8936002)(66446008)(64756008)(66556008)(26005)(52116002)(6506007)(71200400001)(71190400001)(386003)(305945005)(102836004)(1076003)(50226002)(99286004)(68736007); DIR:OUT; SFP:1102; SCL:1; SRVR:MN2PR04MB6959; H:MN2PR04MB6061.namprd04.prod.outlook.com; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; A:1; MX:1; x-ms-exchange-senderadcheck: 1 x-microsoft-antispam-message-info: zGbEHxCszr0HjNrebgIrx1PEkqYqoUyncqAO0Eyc+9tdhbcmDmAMT4AV1fTv4YFnJzqU75WzNlOsqY6e2aAq3HOnqMg8pphcshBsd5h6UPxi/bKxGe1hutjQutn0fIMXR1z1KbpGShfoqqRAFS8IDi5OVN9PtAXOKtF1zAriD4sNGD8/YFMhbtIbDjYfhoGu9Ph1S9CM3PqjU+HzoFRXQ7CdkLx7aIHzbTu4QOrWBOopC9qus9UQQ1Yvrcyova5EryQLNnps9wveKajzdK5F78fbBUFubW2lejQmEGoTTTNRwX+qujy6COQKZp3/7MhjJkAx15efWmSgNDEfIS5FEJbaQz1H5QUw79jLst+vqKDZIWl/1vs+Mru0QLMvN6RuWn26mf+s5Jxg5chFgNOBG3oGneRdGE/8HfzI6OmkevQ= MIME-Version: 1.0 X-OriginatorOrg: wdc.com X-MS-Exchange-CrossTenant-Network-Message-Id: 36c1585e-c7bd-4085-4208-08d6f936cab5 X-MS-Exchange-CrossTenant-originalarrivaltime: 25 Jun 2019 06:31:44.9767 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: b61c8803-16f3-4c35-9b17-6f65f441df86 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: Anup.Patel@wdc.com X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR04MB6959 Cc: U-Boot Mailing List , Palmer Dabbelt , Joe Hershberger , Alistair Francis Subject: [U-Boot] [PATCH v9 7/9] riscv: sifive: fu540: Setup ethaddr env variable using OTP X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" This patch extends SiFive FU540 board support to setup ethaddr env variable based on board serialnum read from OTP. Signed-off-by: Anup Patel Reviewed-by: Bin Meng --- board/sifive/fu540/fu540.c | 122 +++++++++++++++++++++++++++++++++ configs/sifive_fu540_defconfig | 1 + 2 files changed, 123 insertions(+) diff --git a/board/sifive/fu540/fu540.c b/board/sifive/fu540/fu540.c index 5adc4a3d4a..11daf1a75a 100644 --- a/board/sifive/fu540/fu540.c +++ b/board/sifive/fu540/fu540.c @@ -8,6 +8,128 @@ #include #include +#include +#include + +#ifdef CONFIG_MISC_INIT_R + +#define FU540_OTP_BASE_ADDR 0x10070000 + +struct fu540_otp_regs { + u32 pa; /* Address input */ + u32 paio; /* Program address input */ + u32 pas; /* Program redundancy cell selection input */ + u32 pce; /* OTP Macro enable input */ + u32 pclk; /* Clock input */ + u32 pdin; /* Write data input */ + u32 pdout; /* Read data output */ + u32 pdstb; /* Deep standby mode enable input (active low) */ + u32 pprog; /* Program mode enable input */ + u32 ptc; /* Test column enable input */ + u32 ptm; /* Test mode enable input */ + u32 ptm_rep;/* Repair function test mode enable input */ + u32 ptr; /* Test row enable input */ + u32 ptrim; /* Repair function enable input */ + u32 pwe; /* Write enable input (defines program cycle) */ +} __packed; + +#define BYTES_PER_FUSE 4 +#define NUM_FUSES 0x1000 + +static int fu540_otp_read(int offset, void *buf, int size) +{ + struct fu540_otp_regs *regs = (void __iomem *)FU540_OTP_BASE_ADDR; + unsigned int i; + int fuseidx = offset / BYTES_PER_FUSE; + int fusecount = size / BYTES_PER_FUSE; + u32 fusebuf[fusecount]; + + /* check bounds */ + if (offset < 0 || size < 0) + return -EINVAL; + if (fuseidx >= NUM_FUSES) + return -EINVAL; + if ((fuseidx + fusecount) > NUM_FUSES) + return -EINVAL; + + /* init OTP */ + writel(0x01, ®s->pdstb); /* wake up from stand-by */ + writel(0x01, ®s->ptrim); /* enable repair function */ + writel(0x01, ®s->pce); /* enable input */ + + /* read all requested fuses */ + for (i = 0; i < fusecount; i++, fuseidx++) { + writel(fuseidx, ®s->pa); + + /* cycle clock to read */ + writel(0x01, ®s->pclk); + mdelay(1); + writel(0x00, ®s->pclk); + mdelay(1); + + /* read the value */ + fusebuf[i] = readl(®s->pdout); + } + + /* shut down */ + writel(0, ®s->pce); + writel(0, ®s->ptrim); + writel(0, ®s->pdstb); + + /* copy out */ + memcpy(buf, fusebuf, size); + + return 0; +} + +static u32 fu540_read_serialnum(void) +{ + int ret; + u32 serial[2] = {0}; + + for (int i = 0xfe * 4; i > 0; i -= 8) { + ret = fu540_otp_read(i, serial, sizeof(serial)); + if (ret) { + printf("%s: error reading from OTP\n", __func__); + break; + } + if (serial[0] == ~serial[1]) + return serial[0]; + } + + return 0; +} + +static void fu540_setup_macaddr(u32 serialnum) +{ + /* Default MAC address */ + unsigned char mac[6] = { 0x70, 0xb3, 0xd5, 0x92, 0xf0, 0x00 }; + + /* + * We derive our board MAC address by ORing last three bytes + * of board serial number to above default MAC address. + * + * This logic of deriving board MAC address is taken from + * SiFive FSBL and is kept unchanged. + */ + mac[5] |= (serialnum >> 0) & 0xff; + mac[4] |= (serialnum >> 8) & 0xff; + mac[3] |= (serialnum >> 16) & 0xff; + + /* Update environment variable */ + eth_env_set_enetaddr("ethaddr", mac); +} + +int misc_init_r(void) +{ + /* Set ethaddr environment variable if not set */ + if (!env_get("ethaddr")) + fu540_setup_macaddr(fu540_read_serialnum()); + + return 0; +} + +#endif int board_init(void) { diff --git a/configs/sifive_fu540_defconfig b/configs/sifive_fu540_defconfig index f78412398e..f19203745e 100644 --- a/configs/sifive_fu540_defconfig +++ b/configs/sifive_fu540_defconfig @@ -7,4 +7,5 @@ CONFIG_DISTRO_DEFAULTS=y CONFIG_FIT=y CONFIG_DISPLAY_CPUINFO=y CONFIG_DISPLAY_BOARDINFO=y +CONFIG_MISC_INIT_R=y CONFIG_OF_PRIOR_STAGE=y