From patchwork Mon Jun 24 03:26:38 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anup Patel X-Patchwork-Id: 1121034 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="iYZ4hfeV"; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=sharedspace.onmicrosoft.com header.i=@sharedspace.onmicrosoft.com header.b="t6V1ENbg"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 45XFC133Zdz9s4Y for ; Mon, 24 Jun 2019 13:29:17 +1000 (AEST) Received: by lists.denx.de (Postfix, from userid 105) id 1DD7BC21ED6; Mon, 24 Jun 2019 03:28:21 +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 EAB5BC21F04; Mon, 24 Jun 2019 03:28:10 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 62CC7C21F05; Mon, 24 Jun 2019 03:26:44 +0000 (UTC) Received: from esa1.hgst.iphmx.com (esa1.hgst.iphmx.com [68.232.141.245]) by lists.denx.de (Postfix) with ESMTPS id A7708C21F2B for ; Mon, 24 Jun 2019 03:26:40 +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=1561346800; x=1592882800; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=QTPnBp1Ox2cjjfEWRiao6TyfIP3gNllxIQt4nGomETQ=; b=iYZ4hfeVpr3J6ewcDLbpbNbMor68SqPWksYOn/Oz1LLjgMtvVuc4SVSU gkw9W3/JCome5aXN2bN3RISfhBXaZGkIG9J4oUk1kI2GDABbDJinrlNWb 4T9E7pHqgroah69n93wOfNE7vvkSAWlEIw1yTSrPcTIF0Lzlmsxb4tpCR jgWy2pmyLbkpVNCTTbc4I11pxGXV3oPKfAtaSKR6BdflHMJBYBnR8EizD 5kcrtJ447D1XgdsD+Kz+To+GE4pJIvU9mCTuoKwMeWzY2uHfOpFyHAtIA sqEknDHIiD+jKmU3VuDE2YkN8JH299OEuFJ+OG6JYRqXbWFEJe3CrJ2wo g==; X-IronPort-AV: E=Sophos;i="5.63,410,1557158400"; d="scan'208";a="217713029" Received: from mail-by2nam01lp2052.outbound.protection.outlook.com (HELO NAM01-BY2-obe.outbound.protection.outlook.com) ([104.47.34.52]) by ob1.hgst.iphmx.com with ESMTP; 24 Jun 2019 11:26:39 +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=t6V1ENbgsPjaJjDBqcFSLjyGqB0UfqGz9UqY+dPt5tZx7mr3WUakWDy5Z27lmzzJQFDHMa6Hp/DPNEU3LwzLkf9vcJ4889dYDZ7ext5JA3TvN+Zmryz3XgzcxNQEwlro+PVItY45Mt9PqA/Ww/Dpn78V190NCCXn7IrUyk4avlg= Received: from MN2PR04MB6061.namprd04.prod.outlook.com (20.178.246.15) by MN2PR04MB5551.namprd04.prod.outlook.com (20.178.248.225) 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 03:26:38 +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 03:26:38 +0000 From: Anup Patel To: Rick Chen , Bin Meng , Lukas Auer , Simon Glass Thread-Topic: [PATCH v6 7/8] riscv: sifive: fu540: Setup ethaddr env variable using OTP Thread-Index: AQHVKjyiIdc2209+X0aaGgRdoPsrAw== Date: Mon, 24 Jun 2019 03:26:38 +0000 Message-ID: <20190624032431.20854-8-anup.patel@wdc.com> References: <20190624032431.20854-1-anup.patel@wdc.com> In-Reply-To: <20190624032431.20854-1-anup.patel@wdc.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-clientproxiedby: MA1PR0101CA0057.INDPRD01.PROD.OUTLOOK.COM (2603:1096:a00:20::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: 2d868e33-60a2-4da1-ee6e-08d6f853c499 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:MN2PR04MB5551; x-ms-traffictypediagnostic: MN2PR04MB5551: 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)(396003)(366004)(346002)(376002)(39860400002)(40224003)(189003)(199004)(256004)(11346002)(316002)(478600001)(72206003)(1076003)(8676002)(54906003)(5660300002)(110136005)(14454004)(50226002)(52116002)(99286004)(7736002)(6436002)(81156014)(81166006)(53936002)(66946007)(66556008)(2906002)(8936002)(66476007)(305945005)(86362001)(73956011)(6512007)(64756008)(66446008)(71190400001)(14444005)(446003)(2616005)(3846002)(25786009)(4326008)(36756003)(71200400001)(476003)(44832011)(68736007)(6486002)(7416002)(102836004)(6506007)(386003)(26005)(186003)(76176011)(66066001)(6116002)(486006); DIR:OUT; SFP:1102; SCL:1; SRVR:MN2PR04MB5551; 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: +pgbdTTEIV0OoVoTg7aG09F/5lbgUlylUxkSWIjq07MxJl5PjW6imIl9dBdDNZF3Cz4ldcJsv5jnKAQ94raH7gqQ93PKD5pVYsOOLrlREzzHv/PJlqAYlX6T8ohcsvkAtQmRn+Oj/zegVHSA8nbXy0SbMthfa3FnbydciVMk/Y2wuDicuM+ijmeFsxxpGsI/hMYtfNwCc7BX9QVB964dE55EO7TqbslnJRqQM6UUT6t09rLHn6TNR51EWUqIm9kAPDLFM/cXPzSjBnfXjYrL9baiDFxIAWvbv+v16C7vJDne7B22PlIj2XeJ2qRQW4rEHy1b2O0zWWnxCFbJEVSedW1uh+S7UiRj8VGFYzX94YOaFZoQ3o3CK7mI+Y+aF423fkb26P5cxG+rVAlN0RUWOK+4qEJV2los8g2kSfCQkeQ= MIME-Version: 1.0 X-OriginatorOrg: wdc.com X-MS-Exchange-CrossTenant-Network-Message-Id: 2d868e33-60a2-4da1-ee6e-08d6f853c499 X-MS-Exchange-CrossTenant-originalarrivaltime: 24 Jun 2019 03:26:38.6398 (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: MN2PR04MB5551 Cc: U-Boot Mailing List , Palmer Dabbelt , Joe Hershberger , Alistair Francis Subject: [U-Boot] [PATCH v6 7/8] 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 --- 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