From patchwork Mon Jun 24 04:03:13 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anup Patel X-Patchwork-Id: 1121054 X-Patchwork-Delegate: uboot@andestech.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="k6iAoX6V"; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=sharedspace.onmicrosoft.com header.i=@sharedspace.onmicrosoft.com header.b="tf7NOLjy"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 45XG5q551Zz9s4Y for ; Mon, 24 Jun 2019 14:09:51 +1000 (AEST) Received: by lists.denx.de (Postfix, from userid 105) id 61E39C21F32; Mon, 24 Jun 2019 04:08:27 +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 4DECDC21F67; Mon, 24 Jun 2019 04:03:48 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 94A09C21F67; Mon, 24 Jun 2019 04:03:38 +0000 (UTC) Received: from esa1.hgst.iphmx.com (esa1.hgst.iphmx.com [68.232.141.245]) by lists.denx.de (Postfix) with ESMTPS id EB799C21F58 for ; Mon, 24 Jun 2019 04:03:16 +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=1561348997; x=1592884997; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=QTPnBp1Ox2cjjfEWRiao6TyfIP3gNllxIQt4nGomETQ=; b=k6iAoX6VHbVxvTDsEkS/nFPuzgMyzW94VLl9PemnlSufGir3Vn/8HYF9 XmawehsQljarifV1RT7Odmh/+zaGWTETwL5FWH7M9rtiXGK+SdUGtYYvw iLQnoAf7koTZFYs/TS4dV0qsCILEk+i02t2YWwnJTK1irbu6cQ2lbE/Xs M8CunbVdzeaR7td3RnTL3Q+VYMa8JN+NqfR5+ifGjG1wr4xAGxZ6dGYLA uRtx8pGkZmAHpPbU/FTpunBujXP7p4TeknijkTi2Mzf0mdXOhZg1c2ZOo mrHeyeng3uOGzbm6JfHR6hFExCxj7CRIkw0j5bP4A7ZpXCZGI/en0Bayz g==; X-IronPort-AV: E=Sophos;i="5.63,411,1557158400"; d="scan'208";a="217715303" Received: from mail-co1nam03lp2058.outbound.protection.outlook.com (HELO NAM03-CO1-obe.outbound.protection.outlook.com) ([104.47.40.58]) by ob1.hgst.iphmx.com with ESMTP; 24 Jun 2019 12:03:15 +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=QTPnBp1Ox2cjjfEWRiao6TyfIP3gNllxIQt4nGomETQ=; b=tf7NOLjyaiXhjGiG46y7Jfq53qSX28dpMlBJttX70nugmJrAurJ2Xu8++YtePHugBDlH3Rqh+hd0dHFYbLisZs6HETL9d97yUt78PUtg2Bfz810fGiJu1LGpfvhcpUhu2cvrGebYBAGDP3/AQpU4crJldzTlJIUIDvZQ1U3Bhlo= Received: from MN2PR04MB6061.namprd04.prod.outlook.com (20.178.246.15) by MN2PR04MB5725.namprd04.prod.outlook.com (20.179.22.141) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2008.16; Mon, 24 Jun 2019 04:03:14 +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; Mon, 24 Jun 2019 04:03:14 +0000 From: Anup Patel To: Rick Chen , Bin Meng , Lukas Auer , Simon Glass Thread-Topic: [PATCH v7 7/9] riscv: sifive: fu540: Setup ethaddr env variable using OTP Thread-Index: AQHVKkG+wCBPVsFpX0+ISTC2VaPmWA== Date: Mon, 24 Jun 2019 04:03:13 +0000 Message-ID: <20190624040212.3726-8-anup.patel@wdc.com> References: <20190624040212.3726-1-anup.patel@wdc.com> In-Reply-To: <20190624040212.3726-1-anup.patel@wdc.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-clientproxiedby: MAXPR01CA0101.INDPRD01.PROD.OUTLOOK.COM (2603:1096:a00:5d::19) 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: 9c37abd3-c1af-4453-8211-08d6f858e12c 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:MN2PR04MB5725; x-ms-traffictypediagnostic: MN2PR04MB5725: wdcipoutbound: EOP-TRUE x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:2150; x-forefront-prvs: 007814487B x-forefront-antispam-report: SFV:NSPM; SFS:(10019020)(136003)(366004)(396003)(376002)(346002)(39860400002)(40224003)(189003)(199004)(36756003)(14454004)(72206003)(14444005)(256004)(316002)(7416002)(3846002)(6116002)(86362001)(110136005)(6486002)(6436002)(99286004)(2906002)(478600001)(8676002)(81156014)(50226002)(8936002)(81166006)(25786009)(305945005)(186003)(4326008)(71190400001)(71200400001)(54906003)(66066001)(446003)(66446008)(7736002)(66476007)(66556008)(64756008)(44832011)(2616005)(476003)(11346002)(5660300002)(66946007)(73956011)(486006)(1076003)(53936002)(68736007)(6512007)(52116002)(386003)(6506007)(102836004)(26005)(76176011); DIR:OUT; SFP:1102; SCL:1; SRVR:MN2PR04MB5725; H:MN2PR04MB6061.namprd04.prod.outlook.com; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; MX:1; A:1; x-ms-exchange-senderadcheck: 1 x-microsoft-antispam-message-info: 0RFL6ELCvP2inW8TMucFzXuXoYFifQDiEL4UDlJzZt7uXCM0rxh6Ro7nXnnw4tEJDMfU+GfbXDHdGWmc2d+Ltu8kNCxeKzm3gJ9KDK/CxPD+gm6DfpUrl62JEDRXcT5Kyx6g5C+NWwCuVUcjcssHBP6e3sA5Yww2YBFzaspXEhQn8nipoYeASj2rjsgItLziblcn1Z42rvE1NM0YNfUbS07dkCk1IuaKVVWYHvll9sZFy+DYrg+1hd91RjFgKECMcRG86Dmvp/A0mYvxt7uxOtoL2goPoX6frt1mPCCmtO79SgYdsStMyqGVawmVLYUbftCCgMkzjRPoPnEsJpOWQyRVKPQWoJRQVlzAKjdKkYE3FzI5hjbmCUWWDB3YDFE+VqF2TN5PmnvH3znSMi8QeWo7z7Eq39U+xpket7xanyA= MIME-Version: 1.0 X-OriginatorOrg: wdc.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9c37abd3-c1af-4453-8211-08d6f858e12c X-MS-Exchange-CrossTenant-originalarrivaltime: 24 Jun 2019 04:03:13.9973 (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: MN2PR04MB5725 Cc: U-Boot Mailing List , Palmer Dabbelt , Joe Hershberger , Alistair Francis Subject: [U-Boot] [PATCH v7 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