From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.1 required=3.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 95B34C43441 for ; Fri, 9 Nov 2018 05:01:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id BAEAE20840 for ; Fri, 9 Nov 2018 05:01:28 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=xilinx.onmicrosoft.com header.i=@xilinx.onmicrosoft.com header.b="iq7sY3sC" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org BAEAE20840 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=xilinx.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727851AbeKIOkR (ORCPT ); Fri, 9 Nov 2018 09:40:17 -0500 Received: from mail-eopbgr820059.outbound.protection.outlook.com ([40.107.82.59]:22858 "EHLO NAM01-SN1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727474AbeKIOkQ (ORCPT ); Fri, 9 Nov 2018 09:40:16 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=xilinx.onmicrosoft.com; s=selector1-xilinx-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Zqxg9qcdIJbis74c7OmygL8tC9HbCiuQQCA94fkvjoY=; b=iq7sY3sCbRZP+QkVj1btsG2wAUZaSiNxqLHaKCQrE4DhPbW1AyKnOnBRdxPyp/y76W7l/chUtoORkWLvspgiLRXEDHkI4F5/tZB85Lr4LDmrubuFNB2xbAuptlJFqj+rC0Qwt9LhRoFFJyu0gIx/o6+GqhrxDd5BG1d6fharxzg= Received: from MWHPR0201CA0097.namprd02.prod.outlook.com (2603:10b6:301:75::38) by SN4PR0201MB3519.namprd02.prod.outlook.com (2603:10b6:803:44::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1294.30; Fri, 9 Nov 2018 05:01:17 +0000 Received: from CY1NAM02FT020.eop-nam02.prod.protection.outlook.com (2a01:111:f400:7e45::209) by MWHPR0201CA0097.outlook.office365.com (2603:10b6:301:75::38) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.1294.22 via Frontend Transport; Fri, 9 Nov 2018 05:01:17 +0000 Authentication-Results: spf=pass (sender IP is 149.199.60.100) smtp.mailfrom=xilinx.com; kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=bestguesspass action=none header.from=xilinx.com; Received-SPF: Pass (protection.outlook.com: domain of xilinx.com designates 149.199.60.100 as permitted sender) receiver=protection.outlook.com; client-ip=149.199.60.100; helo=xsj-pvapsmtpgw02; Received: from xsj-pvapsmtpgw02 (149.199.60.100) by CY1NAM02FT020.mail.protection.outlook.com (10.152.75.191) with Microsoft SMTP Server (version=TLS1_0, cipher=TLS_RSA_WITH_AES_256_CBC_SHA) id 15.20.1339.11 via Frontend Transport; Fri, 9 Nov 2018 05:01:12 +0000 Received: from unknown-38-66.xilinx.com ([149.199.38.66]:42480 helo=xsj-pvapsmtp01) by xsj-pvapsmtpgw02 with esmtp (Exim 4.63) (envelope-from ) id 1gKyut-0004AA-Vr; Thu, 08 Nov 2018 21:01:12 -0800 Received: from [127.0.0.1] (helo=localhost) by xsj-pvapsmtp01 with smtp (Exim 4.63) (envelope-from ) id 1gKyuo-0001W6-PT; Thu, 08 Nov 2018 21:01:06 -0800 Received: from xsj-pvapsmtp01 (maildrop.xilinx.com [149.199.38.66]) by xsj-smtp-dlp2.xlnx.xilinx.com (8.13.8/8.13.1) with ESMTP id wA950wrM025684; Thu, 8 Nov 2018 21:00:58 -0800 Received: from [172.23.37.108] (helo=xhdnagasure40.xilinx.com) by xsj-pvapsmtp01 with esmtp (Exim 4.63) (envelope-from ) id 1gKyuf-0001Cd-Mk; Thu, 08 Nov 2018 21:00:58 -0800 From: Naga Sureshkumar Relli To: , , , , , , CC: , , , , Naga Sureshkumar Relli Subject: [LINUX PATCH v12 3/3] mtd: rawnand: arasan: Add support for Arasan NAND Flash Controller Date: Fri, 9 Nov 2018 10:30:41 +0530 Message-ID: <1541739641-17789-4-git-send-email-naga.sureshkumar.relli@xilinx.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1541739641-17789-1-git-send-email-naga.sureshkumar.relli@xilinx.com> References: <1541739641-17789-1-git-send-email-naga.sureshkumar.relli@xilinx.com> X-RCIS-Action: ALLOW X-TM-AS-Product-Ver: IMSS-7.1.0.1224-8.2.0.1013-23620.005 X-TM-AS-User-Approved-Sender: Yes;Yes X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-HT: Tenant X-Forefront-Antispam-Report: CIP:149.199.60.100;IPV:NLI;CTRY:US;EFV:NLI;SFV:NSPM;SFS:(10009020)(396003)(39860400002)(376002)(136003)(346002)(2980300002)(438002)(189003)(199004)(478600001)(54906003)(446003)(110136005)(4744004)(9786002)(106002)(47776003)(7416002)(2616005)(16586007)(2201001)(11346002)(476003)(5660300001)(426003)(126002)(305945005)(336012)(39060400002)(316002)(8676002)(5024004)(107886003)(77096007)(217873002)(14444005)(36386004)(50466002)(81156014)(575784001)(76176011)(106466001)(63266004)(53946003)(48376002)(26005)(81166006)(486006)(186003)(50226002)(6636002)(356004)(6666004)(36756003)(2906002)(4326008)(51416003)(7696005)(8936002)(107986001)(5001870100001)(2101003)(579004);DIR:OUT;SFP:1101;SCL:1;SRVR:SN4PR0201MB3519;H:xsj-pvapsmtpgw02;FPR:;SPF:Pass;LANG:en;PTR:unknown-60-100.xilinx.com,xapps1.xilinx.com;MX:1;A:1; X-Microsoft-Exchange-Diagnostics: 1;CY1NAM02FT020;1:jWRr/Xp71VCUi5G4wyl+X3Agev7UNjX1u/yW4eTVCllnG0svyxLW9NAHCXX/6wRc0lvpaEyaJHT2Yo57yWjpuzkkrj30RsHao2+6vzclmPmjUYyuY9x7LbkY1to4aMHh MIME-Version: 1.0 Content-Type: text/plain X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 0c1223f8-6459-4017-e9ff-08d64600613f X-Microsoft-Antispam: BCL:0;PCL:0;RULEID:(7020095)(4652040)(8989299)(5600074)(711020)(4608076)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(2017052603328)(7153060);SRVR:SN4PR0201MB3519; X-Microsoft-Exchange-Diagnostics: 1;SN4PR0201MB3519;3:+UCBHDPxmf/FKYENEYc0CJ3xKlSpLCntHBkMBHwsYO+ozmfPr0UWIuUNRbADQ1TFDUQ5nItC/83hxfLJ/kRzofxcaGErvuddVCZDIN1tR7JYN7TZ/Ow4AST+WF/M27r9C4yTm6ZbYGRLb1bzHI9c8w2ePkDMCvDWeDnAKvbx3EwlyXPqq9mgrwoZ40QUs7JL3uP6eRCy0ATDzI49Bv2XwJzzX7g61U4Sfl6w2g1ZYgrKqqatur/XMSO3CR5H45EbPiX9Pr16sTpa7DV0XSiZVWxcE9U0g+wlhkcNprEykmfCJU1iw3Gm/1fRBgIj36d7SjwebdIEXzF/8q9HdB0GKlMnNmxsmFuyb7o87qRDRL0=;25:yk8Krx1XL60ZjprTTqgVdU5NENN8nSrN0ywKvSFFZoZ2TCgMkrIlIk1xlXyF3ywBEE+MZ6EeSULvMGuHNOeaFzgGTFs1A8fd8UHKF8+4g/Z2aFEeSfdefLN47roYlJOHRLFFcdrgDmenesE/FTQMbr8oS4CCQt72LmFGhRZHOS13GGmuFcwaH2bgaPgr7uxIRcxpAVU1FO9oL9pfO22Vw55J9moi4kWqauYLLmlSthMF4W2l64+nDrHo5n3MhQRlUiy5SrlC0EzY/c+G+X+3U5Sv0XQUUJSzpHEjv9+vzui09579sdZKRGC5U0G/t9YcZuJAcNPemO3VH5v4/aSN4A== X-MS-TrafficTypeDiagnostic: SN4PR0201MB3519: X-Microsoft-Exchange-Diagnostics: 1;SN4PR0201MB3519;31:WtykeUUfElvAnzrOy6J4OOSHbG+QftwT2nOUK2SrxWphIg3qlxrIzE+d5hVEc/N6FlJhNCc+QgEHh/K5XgfHYiqjcuw2Ix4zVVVZk2MmppzSFbbU00963UM+Gz0fIOKuQXeeQmcvCUkBGccQfXU3o0BNoFWiiX8N6jO1WgpjGh5cievyvbock+tF+jn7L0xo9zE5YxF66mDpwJdcjDyDmdPMNxDyu8OOTc3vOSTLo14=;20:60lBh6HJC9AksNDRwHTSgPZyUeyu6GzSBXgPhWQ0DfVpVgmOLsCpcy1iCdUJQjXTX7/5Nza1PlGjKZu9zJiS8tlmHjOJN5lkkKsgppT896i6bSmI57qtvnzPNIERRLI7nR+4L7iHb0F4GuJFPNKwqAh/Cve29iUgjl58bwcGYPHACNjHSGfsL+dPWR4dzMLpNlHjCjZrbGoqf82dQUkDkxZ2cXX1e20iy3FxGMWYAaL/uIj3+YFNSwMGBLaRsDAicso0ctycDeEMFbOGgEyi2spshdeur9A+/UxDr+FWjUB0/S5Egaswgv6ZzXXMl173Jkqs7mAxY053Hjr2SH4NCkuv1C5+X/pttmP+yUWaHHss9gp7dm3fZ0ACJ0t5LCLwY6NITxGxIvWnMPknQN4GI0qgNnxmqAX2MLUFF2TIVwEs5Igc/UrFlffmEZbjQJ9inf0RfBTmZ+1wlpmEHyDW5XZeyml4JxF1o4V4B8rL6eZtzt0SKavnkDJW/QcXhGk8 X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(192813158149592)(788757137089)(211171220733660); X-MS-Exchange-SenderADCheck: 1 X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(8211001083)(6040522)(2401047)(8121501046)(5005006)(93006095)(93004095)(10201501046)(3002001)(3231382)(944501410)(52105095)(6055026)(148016)(149066)(150057)(6041310)(20161123562045)(20161123558120)(20161123560045)(20161123564045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(201708071742011)(7699051)(76991095);SRVR:SN4PR0201MB3519;BCL:0;PCL:0;RULEID:;SRVR:SN4PR0201MB3519; X-Microsoft-Exchange-Diagnostics: 1;SN4PR0201MB3519;4:1ERYlvgJfVJXMjs8s+VgEF+ls0CUl7DdKnf6ex9ixB7XyNpbEr5Q4es7XqeiI7MK5y2twIiEdsxLkFQ14W0JFrBwVwEpYuQKx/UwL6wYV+0il4l2iLiHBsV/91ukDFDIyi4jppIHDKRD9LRpD56GCwOadkPXiPJCbADajBRDm0OtkUNEHoKWICLz3GihkP68Ht1I8NAAnRACNTCgUP3xY/z5NAJiOnptAsVP5c0Peo7ASjfe3ikmMkHLxhYI7ExkL8uDdw/JxBMterm3OQCODS1k1Ke0fXLsgmq+4k5NHLLvCrVuVvGuFGmKk2sueELDQfKaWd7VDymUC6CeEixri1B1uZ88PdI282oS/Ioe09eJ0TesI8dBC8xFgPR3Hyzu X-Forefront-PRVS: 08512C5403 X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;SN4PR0201MB3519;23:yvA6noBri69pKOgAxnpv8GhoQ/E9FMc+1xRydXb?= =?us-ascii?Q?c8dS7ZUVp/WjJwAIdTxtdkIaJXePA2jqLNVgnuHF29RPC6akqUL6bJKdASJu?= =?us-ascii?Q?fr/ir4dMg5TwiqbbkTzys13kdjSROOLOXjyiRcNth+QObn6eLgIviPObe5+Y?= =?us-ascii?Q?/r/DKJ7JSId6F2mfwEZP3zZr9FUE9WCQq+jKCjAkdz0oGU78+Zq0augYECXg?= =?us-ascii?Q?J5aaYjwebzDJDyZO2fYEQXHa/hBOTHhrcXHURVWEdpgGTwVJdOb+Rtv8Jlv8?= =?us-ascii?Q?Yf4RZ6izUV6bGt5HgC5Cbh+BWJn7MqWuw2XWiVwX3yHrjjFyhdgy02fdX+Df?= =?us-ascii?Q?NT2f3sXo2k7+NA4HQiiS1LhovZYpU1pHFXBIEkzCGMVrESdvyKqMo0V8Q2SQ?= =?us-ascii?Q?94qmsQDxyqj+5DHrQhL6v1moy15fXw3x18yECRemX5cgvufgMxv1NIbwckrS?= =?us-ascii?Q?aq3eonB/ikseH2n7mRD2zjSfy/+MVbpszvSn6YBjGqgJqfWVnXaoeNPy2Fol?= =?us-ascii?Q?N4dlwYsVRVtNU+mBm+g2K/1TV7ALlwHxlvEMoI/aHcvh/JamVSg1y7CeEUsA?= =?us-ascii?Q?xl9sezQu0KgWr+nLBHVZNQrro1UmJuYtTGYk6UzKOdziWHQp1p0okUnqBuiG?= =?us-ascii?Q?G6NILr8+Pe1UaDVfePZ8hvPK+kx4Q75HREFbyN/dJ1FjkzxBmSV0GTudfzaC?= =?us-ascii?Q?M7l24Z0YwMkufbj90qljN01xXxlKKpsvqkbw5s1yfFBrPsOMC0yohdBgpGUd?= =?us-ascii?Q?Xy+2lQDIPj9h94y82m5/b40RN+Xx4bljVR53hzLnDa/EvAeEuIKe79biAm6M?= =?us-ascii?Q?HwvKWwcUOxr8b5I5H7K+hjZPbOc0skf8nUppjz154ReMnKVxUTnrYlwxTseU?= =?us-ascii?Q?XDknGbCfEZDJZSM4uygA7sP3p9D7qoWY6TC0Ld/a3qnVK5yWYQFayHSWqy8I?= =?us-ascii?Q?cvUqee2zCS8b//FBdUusu4w7sn7l7la/PPGA8JY8iBPUmH0ukNUVa6oymlOw?= =?us-ascii?Q?IRnr3cRBNYBTQG6ldfH1f/w04HSwR2PIKWz0eSu0sMaihlORmtECwMBEutDF?= =?us-ascii?Q?Lekgj8hnX5DTUWWzq0tyBGiyIoxbKWOuiioDM/+ZqoDWK8KTp43dhgdqRv1O?= =?us-ascii?Q?bYOeEh6nnDNMV9WBu789hl8dGsuD90AZPdiZ7zYmVfgJvEmkxb+xaY0anmd8?= =?us-ascii?Q?kYvfuKB6xqeuw6er2xJEaiGXcn635ouBjWopsnuGOSOHTm7CoPl9bPySoy6K?= =?us-ascii?Q?ZPeVX4E4778RRvfrtV3xhtMhPVa3T7rPFtIrWATqqaFL6C8f5I0vTFTqDHHu?= =?us-ascii?Q?8gJfiS01sdhKJq1xRN480d6ga05HSVL9bP9yEjdHihpF3FG2e7t8gcy/bGy7?= =?us-ascii?Q?TGEiTYqbimKWos723JYbg0isiZrg=3D?= X-Microsoft-Antispam-Message-Info: TBuJZDyhHHKN7kd/DZuNzL27p+RL6sH+zq6Ld5KhrTyqcXS3XKipqW6GSg4fR3aRwJRgrV13fB3ERQSptolFnlkwGd8a5bH8WWThfxE1L8nFEt0XrV53gH6CWO8azrvxiUZa8bXLrf0JfLpIxZTIbr34SJGfM/QMznreyMCuPJeR/hCrgPHbCxoBtFeBMbFIU/OXLd//iM7ddsvchqlr2FvveuBYVATVmcCkJ4wMy3gxUhcTx0+V+rMBp25MvLvYmPUUiusNoW+ladP2gPZL1CZ2ZcTaC/cGraWyuq0LF8QaXg07Sr2cBsj/DRkaPNNtukKbKmgV/KT0q3FXrprZ0pU8yjmwoVNZFvX6S4/ARqQ= X-Microsoft-Exchange-Diagnostics: 1;SN4PR0201MB3519;6:gTuf9wywpEUVeR6PvAKq954lA+lhaHz+PlAR1V4eBVJ+cSL8hadiyrqEIug+zPlzPr8fD4zC73+p6a+9eQzDPynZ3AsYvmr3eLCxCGYQB7wVAWH5zdGMbu9L1OBtnx414XKa3xY2iGJC1qa/T3z88fmTfVB47WD/Iw5R81M2kFlCu9G1o6S06G78ytJjvPo5C65FLI/xY0hPzPw5Ek5vhmOLqscFZBvF/D/P9epkHY5KDEG7QHH7QfElP1f0Ewbj3Uxsd0D3B4OH6OsDMVUeFWX3VBH6v9h8IegTgK3L376iMK3ohq1CZWxIK0JqG5gTsv2/bdm299+7SVFJqBnC/spREny8TGnIgITe8+zC80IqgBmZovE4Lrt1gcD8VIDXuzOflYrLEy7rKJau+d5hduyFhli7t2cYNd5cyU8EDC041zbhUEXJWnfqRjvz+pUfaKykVizHALpcD771Da3D9Q==;5:KXe8IqwSOLLGpQmKTYNWD305Cb34vHWM2gUQgq/XEtKrPOWUQu8z2f00nyY0ypdHfhAxwuFMp9hdhfkZlTkFyLC7S0V+2xL7exPavSnSYc5tyc+xvln9gcCrNikY0tTe6TjyxBOgVrFy/F6wN7ZBeqiv3YtELqOiwT3CvKHsXp8=;7:afFijac/8LjaY9acUl06l/j2Q8+rAVtL7+xt092AhzuyoTdOigEopBv5Xuk7js5NAUwJX6E0Gw/VosqokYJ2SjbEkVUDeLHiYfMG5G/t/uJJRNdhNx2hUwndcisRj4guVFT00/N6cSxQmukCYJCOPQ== SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: xilinx.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Nov 2018 05:01:12.7057 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 0c1223f8-6459-4017-e9ff-08d64600613f X-MS-Exchange-CrossTenant-Id: 657af505-d5df-48d0-8300-c31994686c5c X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=657af505-d5df-48d0-8300-c31994686c5c;Ip=[149.199.60.100];Helo=[xsj-pvapsmtpgw02] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN4PR0201MB3519 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add the basic driver for Arasan NAND Flash Controller used in Zynq UltraScale+ MPSoC. It supports HW ECC and upto 24bit correction Signed-off-by: Naga Sureshkumar Relli --- Changes in v12: - Rebased on top of 4.20 - As suggested by Boris, instead of checking the command using nfc_op.cmds[], use PROG_PGRD or PROG_PGPROG as appropriate in reads and writes. - Also use address cycles information provided by core instead of guessing it. Changes in v11: Fixed the below commits given by Boris - implemented separate hooks for each pattern - Changed EVNT_TIMEOUT_MSEC to EVENT_TIMEOUT_MSEC - Grouped register offsets with theri fields, previously there are defines at randome positions - changes cmnds to cmds and s32 to u32 - Removed unnecessary fields from struct anfc_op - Renamed bch and bchmode to strength and ecc_strength respectively - Passed nand_chip object direclty to all functions - Replace is_vmalloc_addr() with virt_addr_valid() - Use default routines for read/write_oob() - Added core support to get sdr timing mode value Changes in v10: - Implemented ->exec_op() interface. - Converted the driver to nand_scan(). Changes in v9: - Added the SPDX tags Changes in v8: - Implemented setup_data_interface hook - fixed checkpatch --strict warnings - Added anfc_config_ecc in read_page_hwecc - Fixed returning status value by reading flash status in read_byte() instead of reading previous value. Changes in v7: - Implemented Marek suggestions and comments - Corrected the acronyms those should be in caps - Modified kconfig/Make file to keep arasan entry in sorted order - Added is_vmlloc_addr check - Used ioread/write32_rep variants to avoid compilation error for intel platforms - separated PIO and DMA mode read/write functions - Minor cleanup Chnages in v6: - Addressed most of the Brian and Boris comments - Separated the nandchip from the nand controller - Removed the ecc lookup table from driver - Now use framework nand waitfunction and readoob - Fixed the compiler warning - Adapted the new frameowrk changes related to ecc and ooblayout - Disabled the clocks after the nand_reelase - Now using only one completion object - Boris suggessions like adapting cmd_ctrl and rework on read/write byte are not implemented and i will patch them later - Also check_erased_ecc_chunk for erase and check for is_vmalloc_addr will implement later once the basic driver is mainlined. Changes in v5: - Renamed the driver filei as arasan_nand.c - Fixed all comments relaqted coding style - Fixed comments related to propagating the errors - Modified the anfc_write_page_hwecc as per the write_page prototype Changes in v4: - Added support for onfi timing mode configuration - Added clock supppport - Added support for multiple chipselects Changes in v3: - Removed unused variables - Avoided busy loop and used jifies based implementation - Fixed compiler warnings "right shift count >= width of type" - Removed unneeded codei and improved error reporting - Added onfi version check to ensure reading the valid address cycles Changes in v2: - Added missing of.h to avoid kbuild system report erro --- drivers/mtd/nand/raw/Kconfig | 7 + drivers/mtd/nand/raw/Makefile | 1 + drivers/mtd/nand/raw/arasan_nand.c | 1238 ++++++++++++++++++++++++++++++++++++ 3 files changed, 1246 insertions(+) create mode 100644 drivers/mtd/nand/raw/arasan_nand.c diff --git a/drivers/mtd/nand/raw/Kconfig b/drivers/mtd/nand/raw/Kconfig index c7efc31..3f7ae73 100644 --- a/drivers/mtd/nand/raw/Kconfig +++ b/drivers/mtd/nand/raw/Kconfig @@ -541,4 +541,11 @@ config MTD_NAND_TEGRA is supported. Extra OOB bytes when using HW ECC are currently not supported. +config MTD_NAND_ARASAN + tristate "Support for Arasan Nand Flash controller" + depends on HAS_IOMEM && HAS_DMA + help + Enables the driver for the Arasan Nand Flash controller on + Zynq Ultrascale+ MPSoC. + endif # MTD_NAND diff --git a/drivers/mtd/nand/raw/Makefile b/drivers/mtd/nand/raw/Makefile index 57159b3..042d53d 100644 --- a/drivers/mtd/nand/raw/Makefile +++ b/drivers/mtd/nand/raw/Makefile @@ -56,6 +56,7 @@ obj-$(CONFIG_MTD_NAND_BRCMNAND) += brcmnand/ obj-$(CONFIG_MTD_NAND_QCOM) += qcom_nandc.o obj-$(CONFIG_MTD_NAND_MTK) += mtk_ecc.o mtk_nand.o obj-$(CONFIG_MTD_NAND_TEGRA) += tegra_nand.o +obj-$(CONFIG_MTD_NAND_ARASAN) += arasan_nand.o nand-objs := nand_base.o nand_legacy.o nand_bbt.o nand_timings.o nand_ids.o nand-objs += nand_onfi.o diff --git a/drivers/mtd/nand/raw/arasan_nand.c b/drivers/mtd/nand/raw/arasan_nand.c new file mode 100644 index 0000000..b8f39c3 --- /dev/null +++ b/drivers/mtd/nand/raw/arasan_nand.c @@ -0,0 +1,1238 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Arasan NAND Flash Controller Driver + * + * Copyright (C) 2014 - 2017 Xilinx, Inc. + * Author: Punnaiah Choudary Kalluri + * Author: Naga Sureshkumar Relli + * + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define EVENT_TIMEOUT_MSEC 1000 + +#define PKT_OFST 0x00 +#define PKT_CNT_SHIFT 12 + +#define MEM_ADDR1_OFST 0x04 +#define MEM_ADDR2_OFST 0x08 +#define PG_ADDR_SHIFT 16 +#define BCH_MODE_SHIFT 25 +#define MEM_ADDR_MASK GENMASK(7, 0) +#define BCH_MODE_MASK GENMASK(27, 25) +#define CS_MASK GENMASK(31, 30) +#define CS_SHIFT 30 + +#define CMD_OFST 0x0C +#define ECC_ENABLE BIT(31) +#define DMA_EN_MASK GENMASK(27, 26) +#define DMA_ENABLE 0x2 +#define DMA_EN_SHIFT 26 +#define REG_PAGE_SIZE_SHIFT 23 + +#define PROG_OFST 0x10 +#define PROG_PGRD BIT(0) +#define PROG_ERASE BIT(2) +#define PROG_STATUS BIT(3) +#define PROG_PGPROG BIT(4) +#define PROG_RDID BIT(6) +#define PROG_RDPARAM BIT(7) +#define PROG_RST BIT(8) +#define PROG_GET_FEATURE BIT(9) +#define PROG_SET_FEATURE BIT(10) + +#define INTR_STS_EN_OFST 0x14 +#define INTR_SIG_EN_OFST 0x18 +#define XFER_COMPLETE BIT(2) +#define READ_READY BIT(1) +#define WRITE_READY BIT(0) +#define MBIT_ERROR BIT(3) +#define EVENT_MASK (XFER_COMPLETE | READ_READY | WRITE_READY | MBIT_ERROR) + +#define INTR_STS_OFST 0x1C +#define READY_STS_OFST 0x20 +#define DMA_ADDR1_OFST 0x24 +#define FLASH_STS_OFST 0x28 +#define DATA_PORT_OFST 0x30 +#define ECC_OFST 0x34 +#define BCH_EN_SHIFT 27 +#define ECC_SIZE_SHIFT 16 + +#define ECC_ERR_CNT_OFST 0x38 +#define PAGE_ERR_CNT_MASK GENMASK(16, 8) +#define PKT_ERR_CNT_MASK GENMASK(7, 0) + +#define ECC_SPR_CMD_OFST 0x3C +#define CMD2_SHIFT 8 +#define ADDR_CYCLES_SHIFT 28 + +#define ECC_ERR_CNT_1BIT_OFST 0x40 +#define ECC_ERR_CNT_2BIT_OFST 0x44 +#define DMA_ADDR0_OFST 0x50 +#define DATA_INTERFACE_OFST 0x6C +#define ANFC_MAX_CHUNK_SIZE 0x4000 +#define ANFC_MAX_ADDR_CYCLES 7 + +#define REG_PAGE_SIZE_512 0 +#define REG_PAGE_SIZE_1K 5 +#define REG_PAGE_SIZE_2K 1 +#define REG_PAGE_SIZE_4K 2 +#define REG_PAGE_SIZE_8K 3 +#define REG_PAGE_SIZE_16K 4 + +#define TEMP_BUF_SIZE 1024 +#define SDR_MODE_PACKET_SIZE 4 + +#define SDR_MODE_DEFLT_FREQ 80000000 +#define COL_ROW_ADDR(pos, val) (((val) & 0xFF) << (8 * (pos))) + +/* + * Arasan NAND controller can't detect errors beyond 24-bit in BCH + * For an erased page we observed that multibit error count as 16 + * with 24-bit ECC. so if the count is equal to or greater than 16 + * then we can say that its an uncorrectable ECC error. + */ +#define MULTI_BIT_ERR_CNT 16 + +struct anfc_op { + u32 cmds[4]; + u32 len; + u32 col; + u32 row; + unsigned int data_instr_idx; + const struct nand_op_instr *data_instr; + u32 naddrcycles; +}; + +/** + * struct anfc_nand_chip - Defines the nand chip related information + * @node: Used to store NAND chips into a list. + * @chip: NAND chip information structure. + * @strength: Bch or Hamming mode enable/disable. + * @ecc_strength: Ecc strength 4.8/12/16. + * @eccval: Ecc config value. + * @spare_caddr_cycles: Column address cycle information for spare area. + * @pktsize: Packet size for read / write operation. + * @csnum: chipselect number to be used. + * @spktsize: Packet size in ddr mode for status operation. + * @inftimeval: Data interface and timing mode information + */ +struct anfc_nand_chip { + struct list_head node; + struct nand_chip chip; + bool strength; + u32 ecc_strength; + u32 eccval; + u16 spare_caddr_cycles; + u32 pktsize; + int csnum; + u32 spktsize; + u32 inftimeval; +}; + +/** + * struct anfc_nand_controller - Defines the Arasan NAND flash controller + * driver instance + * @controller: base controller structure. + * @chips: list of all nand chips attached to the ctrler. + * @dev: Pointer to the device structure. + * @base: Virtual address of the NAND flash device. + * @clk_sys: Pointer to the system clock. + * @clk_flash: Pointer to the flash clock. + * @dma: Dma enable/disable. + * @buf: Buffer used for read/write byte operations. + * @irq: irq number + * @bufshift: Variable used for indexing buffer operation + * @csnum: Chip select number currently inuse. + * @event: Completion event for nand status events. + * @status: Status of the flash device. + * @prog: Used to initiate controller operations. + */ +struct anfc_nand_controller { + struct nand_controller controller; + struct list_head chips; + struct device *dev; + void __iomem *base; + struct clk *clk_sys; + struct clk *clk_flash; + int irq; + int csnum; + struct completion event; + int status; + u8 buf[TEMP_BUF_SIZE]; + u32 eccval; +}; + +static int anfc_ooblayout_ecc(struct mtd_info *mtd, int section, + struct mtd_oob_region *oobregion) +{ + struct nand_chip *nand = mtd_to_nand(mtd); + + if (section >= nand->ecc.steps) + return -ERANGE; + + if (section) + return -ERANGE; + + oobregion->length = nand->ecc.total; + oobregion->offset = mtd->oobsize - oobregion->length; + + return 0; +} + +static int anfc_ooblayout_free(struct mtd_info *mtd, int section, + struct mtd_oob_region *oobregion) +{ + struct nand_chip *nand = mtd_to_nand(mtd); + + if (section >= nand->ecc.steps) + return -ERANGE; + + if (section) + return -ERANGE; + + oobregion->offset = 2; + oobregion->length = mtd->oobsize - nand->ecc.total - 2; + + return 0; +} + +static const struct mtd_ooblayout_ops anfc_ooblayout_ops = { + .ecc = anfc_ooblayout_ecc, + .free = anfc_ooblayout_free, +}; + +static inline struct anfc_nand_chip *to_anfc_nand(struct nand_chip *nand) +{ + return container_of(nand, struct anfc_nand_chip, chip); +} + +static inline struct anfc_nand_controller *to_anfc(struct nand_controller *ctrl) +{ + return container_of(ctrl, struct anfc_nand_controller, controller); +} + +static u8 anfc_page(u32 pagesize) +{ + switch (pagesize) { + case 512: + return REG_PAGE_SIZE_512; + case 1024: + return REG_PAGE_SIZE_1K; + case 2048: + return REG_PAGE_SIZE_2K; + case 4096: + return REG_PAGE_SIZE_4K; + case 8192: + return REG_PAGE_SIZE_8K; + case 16384: + return REG_PAGE_SIZE_16K; + default: + break; + } + + return 0; +} + +static inline void anfc_enable_intrs(struct anfc_nand_controller *nfc, u32 val) +{ + writel(val, nfc->base + INTR_STS_EN_OFST); + writel(val, nfc->base + INTR_SIG_EN_OFST); +} + +static inline void anfc_config_ecc(struct anfc_nand_controller *nfc, bool on) +{ + if (on) + nfc->eccval = 1; + else + nfc->eccval = 0; +} + +static inline void anfc_config_dma(struct anfc_nand_controller *nfc, int on) +{ + u32 val; + + val = readl(nfc->base + CMD_OFST); + val &= ~DMA_EN_MASK; + if (on) + val |= DMA_ENABLE << DMA_EN_SHIFT; + writel(val, nfc->base + CMD_OFST); +} + +static inline int anfc_wait_for_event(struct anfc_nand_controller *nfc) +{ + return wait_for_completion_timeout(&nfc->event, + msecs_to_jiffies(EVENT_TIMEOUT_MSEC)); +} + +static inline void anfc_setpktszcnt(struct anfc_nand_controller *nfc, + u32 pktsize, u32 pktcount) +{ + writel(pktsize | (pktcount << PKT_CNT_SHIFT), nfc->base + PKT_OFST); +} + +static inline void anfc_set_eccsparecmd(struct anfc_nand_controller *nfc, + struct anfc_nand_chip *achip, u8 cmd1, + u8 cmd2) +{ + writel(cmd1 | (cmd2 << CMD2_SHIFT) | + (achip->spare_caddr_cycles << ADDR_CYCLES_SHIFT), + nfc->base + ECC_SPR_CMD_OFST); +} + +static void anfc_setpagecoladdr(struct anfc_nand_controller *nfc, u32 page, + u16 col) +{ + u32 val; + + writel(col | (page << PG_ADDR_SHIFT), nfc->base + MEM_ADDR1_OFST); + + val = readl(nfc->base + MEM_ADDR2_OFST); + val = (val & ~MEM_ADDR_MASK) | + ((page >> PG_ADDR_SHIFT) & MEM_ADDR_MASK); + writel(val, nfc->base + MEM_ADDR2_OFST); +} + +static void anfc_prepare_cmd(struct nand_chip *chip, u8 cmd1, + u8 cmd2, u8 dmamode, + u32 pagesize, u8 addrcycles) +{ + u32 regval; + struct anfc_nand_controller *nfc = to_anfc(chip->controller); + + regval = cmd1 | (cmd2 << CMD2_SHIFT); + if (dmamode) + regval |= DMA_ENABLE << DMA_EN_SHIFT; + regval |= addrcycles << ADDR_CYCLES_SHIFT; + regval |= anfc_page(pagesize) << REG_PAGE_SIZE_SHIFT; + if (chip->ecc.mode == NAND_ECC_HW) + regval |= (nfc->eccval << 31); + writel(regval, nfc->base + CMD_OFST); +} + +static void anfc_rw_dma_op(struct mtd_info *mtd, u8 *buf, int len, + bool do_read, u32 prog, int pktcount, int pktsize) +{ + dma_addr_t paddr; + struct nand_chip *chip = mtd_to_nand(mtd); + struct anfc_nand_controller *nfc = to_anfc(chip->controller); + struct anfc_nand_chip *achip = to_anfc_nand(chip); + u32 eccintr = 0, dir; + + if (pktsize == 0) + pktsize = len; + + anfc_setpktszcnt(nfc, pktsize, pktcount); + + if (!achip->strength) + eccintr = MBIT_ERROR; + + if (do_read) + dir = DMA_FROM_DEVICE; + else + dir = DMA_TO_DEVICE; + paddr = dma_map_single(nfc->dev, buf, len, dir); + if (dma_mapping_error(nfc->dev, paddr)) { + dev_err(nfc->dev, "Read buffer mapping error"); + return; + } + writel(paddr, nfc->base + DMA_ADDR0_OFST); + writel((paddr >> 32), nfc->base + DMA_ADDR1_OFST); + anfc_enable_intrs(nfc, (XFER_COMPLETE | eccintr)); + writel(prog, nfc->base + PROG_OFST); + anfc_wait_for_event(nfc); + dma_unmap_single(nfc->dev, paddr, len, dir); +} + +static void anfc_rw_pio_op(struct mtd_info *mtd, u8 *buf, int len, + bool do_read, int prog, int pktcount, int pktsize) +{ + struct nand_chip *chip = mtd_to_nand(mtd); + struct anfc_nand_controller *nfc = to_anfc(chip->controller); + struct anfc_nand_chip *achip = to_anfc_nand(chip); + u32 *bufptr = (u32 *)buf; + u32 cnt = 0, intr = 0; + + anfc_config_dma(nfc, 0); + + if (pktsize == 0) + pktsize = len; + + anfc_setpktszcnt(nfc, pktsize, pktcount); + + if (!achip->strength) + intr = MBIT_ERROR; + + if (do_read) + intr |= READ_READY; + else + intr |= WRITE_READY; + anfc_enable_intrs(nfc, intr); + writel(prog, nfc->base + PROG_OFST); + while (cnt < pktcount) { + anfc_wait_for_event(nfc); + cnt++; + if (cnt == pktcount) + anfc_enable_intrs(nfc, XFER_COMPLETE); + if (do_read) + ioread32_rep(nfc->base + DATA_PORT_OFST, bufptr, + pktsize / 4); + else + iowrite32_rep(nfc->base + DATA_PORT_OFST, bufptr, + pktsize / 4); + bufptr += (pktsize / 4); + if (cnt < pktcount) + anfc_enable_intrs(nfc, intr); + } + anfc_wait_for_event(nfc); +} + +static void anfc_read_data_op(struct nand_chip *chip, u8 *buf, int len, + int pktcount, int pktsize) +{ + struct mtd_info *mtd = nand_to_mtd(chip); + + if (virt_addr_valid(buf)) + anfc_rw_dma_op(mtd, buf, len, 1, PROG_PGRD, pktcount, pktsize); + else + anfc_rw_pio_op(mtd, buf, len, 1, PROG_PGRD, pktcount, pktsize); +} + +static void anfc_write_data_op(struct nand_chip *chip, const u8 *buf, + int len, int pktcount, int pktsize) +{ + struct mtd_info *mtd = nand_to_mtd(chip); + + if (virt_addr_valid(buf)) + anfc_rw_dma_op(mtd, (char *)buf, len, 0, PROG_PGPROG, pktcount, + pktsize); + else + anfc_rw_pio_op(mtd, (char *)buf, len, 0, PROG_PGPROG, pktcount, + pktsize); +} + +static int anfc_read_page_hwecc(struct nand_chip *chip, u8 *buf, + int oob_required, int page) +{ + struct anfc_nand_controller *nfc = to_anfc(chip->controller); + struct anfc_nand_chip *achip = to_anfc_nand(chip); + struct mtd_info *mtd = nand_to_mtd(chip); + u8 *ecc_code = chip->ecc.code_buf; + u8 *p; + int eccsize = chip->ecc.size; + int eccbytes = chip->ecc.bytes; + int stat = 0, i; + u32 ret; + unsigned int max_bitflips = 0; + u32 eccsteps = chip->ecc.steps; + u32 one_bit_err = 0, multi_bit_err = 0; + + anfc_set_eccsparecmd(nfc, achip, NAND_CMD_RNDOUT, NAND_CMD_RNDOUTSTART); + anfc_config_ecc(nfc, true); + + ret = nand_read_page_op(chip, page, 0, buf, mtd->writesize); + if (ret) + return ret; + + anfc_config_ecc(nfc, false); + if (achip->strength) { + /* + * In BCH mode Arasan NAND controller can correct ECC upto + * 24-bit Beyond that, it can't even detect errors. + */ + multi_bit_err = readl(nfc->base + ECC_ERR_CNT_OFST); + multi_bit_err = ((multi_bit_err & PAGE_ERR_CNT_MASK) >> 8); + } else { + /* + * In Hamming mode Arasan NAND controller can correct ECC upto + * 1-bit and can detect upto 2-bit errors. + */ + one_bit_err = readl(nfc->base + ECC_ERR_CNT_1BIT_OFST); + multi_bit_err = readl(nfc->base + ECC_ERR_CNT_2BIT_OFST); + /* Clear ecc error count register 1Bit, 2Bit */ + writel(0x0, nfc->base + ECC_ERR_CNT_1BIT_OFST); + writel(0x0, nfc->base + ECC_ERR_CNT_2BIT_OFST); + } + + if (oob_required) + chip->ecc.read_oob(chip, page); + + if (multi_bit_err >= MULTI_BIT_ERR_CNT) { + if (!oob_required) + chip->ecc.read_oob(chip, page); + + mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0, + chip->ecc.total); + p = buf; + for (i = 0; eccsteps; eccsteps--, i += eccbytes, + p += eccsize) { + stat = nand_check_erased_ecc_chunk(p, + chip->ecc.size, + &ecc_code[i], + eccbytes, + NULL, 0, + chip->ecc.strength); + if (stat < 0) { + mtd->ecc_stats.failed++; + } else { + mtd->ecc_stats.corrected += stat; + max_bitflips = max_t(unsigned int, max_bitflips, + stat); + } + } + } + + return max_bitflips; +} + +static int anfc_write_page_hwecc(struct nand_chip *chip, const u8 *buf, + int oob_required, int page) +{ + int ret; + struct anfc_nand_controller *nfc = to_anfc(chip->controller); + struct anfc_nand_chip *achip = to_anfc_nand(chip); + struct mtd_info *mtd = nand_to_mtd(chip); + u8 *ecc_calc = chip->ecc.calc_buf; + + anfc_config_ecc(nfc, true); + anfc_set_eccsparecmd(nfc, achip, NAND_CMD_RNDIN, 0); + ret = nand_prog_page_op(chip, page, 0, buf, mtd->writesize); + if (ret) + return ret; + + anfc_config_ecc(nfc, false); + if (oob_required) { + nand_read_oob_op(chip, page, 0, ecc_calc, mtd->oobsize); + ret = mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, + 0, chip->ecc.total); + chip->ecc.write_oob(chip, page); + } + + return 0; +} + +static int anfc_ecc_init(struct mtd_info *mtd, + struct nand_ecc_ctrl *ecc, int ecc_mode) +{ + u32 ecc_addr; + unsigned int ecc_strength, steps; + struct nand_chip *chip = mtd_to_nand(mtd); + struct anfc_nand_chip *achip = to_anfc_nand(chip); + + ecc->mode = NAND_ECC_HW; + ecc->read_page = anfc_read_page_hwecc; + ecc->write_page = anfc_write_page_hwecc; + + mtd_set_ooblayout(mtd, &anfc_ooblayout_ops); + steps = mtd->writesize / chip->ecc_step_ds; + + switch (chip->ecc_strength_ds) { + case 12: + ecc_strength = 0x1; + break; + case 8: + ecc_strength = 0x2; + break; + case 4: + ecc_strength = 0x3; + break; + case 24: + ecc_strength = 0x4; + break; + default: + ecc_strength = 0x0; + } + if (!ecc_strength) + ecc->total = 3 * steps; + else + ecc->total = + DIV_ROUND_UP(fls(8 * chip->ecc_step_ds) * + chip->ecc_strength_ds * steps, 8); + ecc->strength = chip->ecc_strength_ds; + ecc->size = chip->ecc_step_ds; + ecc->bytes = ecc->total / steps; + ecc->steps = steps; + achip->ecc_strength = ecc_strength; + achip->strength = achip->ecc_strength; + ecc_addr = mtd->writesize + (mtd->oobsize - ecc->total); + achip->eccval = ecc_addr | (ecc->total << ECC_SIZE_SHIFT) | + (achip->strength << BCH_EN_SHIFT); + + if (chip->ecc_step_ds >= 1024) + achip->pktsize = 1024; + else + achip->pktsize = 512; + + return 0; +} + +/* NAND framework ->exec_op() hooks and related helpers */ +static void anfc_parse_instructions(struct nand_chip *chip, + const struct nand_subop *subop, + struct anfc_op *nfc_op) +{ + const struct nand_op_instr *instr = NULL; + unsigned int op_id; + int i = 0; + + memset(nfc_op, 0, sizeof(struct anfc_op)); + for (op_id = 0; op_id < subop->ninstrs; op_id++) { + instr = &subop->instrs[op_id]; + switch (instr->type) { + case NAND_OP_CMD_INSTR: + if (op_id) + nfc_op->cmds[1] = instr->ctx.cmd.opcode; + else + nfc_op->cmds[0] = instr->ctx.cmd.opcode; + break; + + case NAND_OP_ADDR_INSTR: + i = nand_subop_get_addr_start_off(subop, op_id); + nfc_op->naddrcycles = nand_subop_get_num_addr_cyc(subop, + op_id + ); + for (; i < nfc_op->naddrcycles; i++) { + u8 val = instr->ctx.addr.addrs[i]; + + if (i < 2) + nfc_op->col |= COL_ROW_ADDR(i, + val); + else + nfc_op->row |= COL_ROW_ADDR(i - + 2, val); + } + break; + case NAND_OP_DATA_IN_INSTR: + nfc_op->data_instr = instr; + nfc_op->data_instr_idx = op_id; + break; + case NAND_OP_DATA_OUT_INSTR: + nfc_op->data_instr = instr; + nfc_op->data_instr_idx = op_id; + break; + case NAND_OP_WAITRDY_INSTR: + break; + } + } +} + +static int anfc_reset_cmd_type_exec(struct nand_chip *chip, + const struct nand_subop *subop) +{ + struct anfc_op nfc_op = {}; + struct anfc_nand_controller *nfc = to_anfc(chip->controller); + + anfc_parse_instructions(chip, subop, &nfc_op); + anfc_prepare_cmd(chip, nfc_op.cmds[0], 0, 0, 0, 0); + anfc_enable_intrs(nfc, XFER_COMPLETE); + writel(PROG_RST, nfc->base + PROG_OFST); + anfc_wait_for_event(nfc); + + return 0; +} + +static int anfc_read_id_type_exec(struct nand_chip *chip, + const struct nand_subop *subop) +{ + const struct nand_op_instr *instr; + struct anfc_op nfc_op = {}; + unsigned int op_id, len; + struct anfc_nand_controller *nfc = to_anfc(chip->controller); + struct mtd_info *mtd = nand_to_mtd(chip); + + anfc_parse_instructions(chip, subop, &nfc_op); + instr = nfc_op.data_instr; + op_id = nfc_op.data_instr_idx; + len = nand_subop_get_data_len(subop, op_id); + anfc_prepare_cmd(chip, nfc_op.cmds[0], 0, 0, 0, 1); + anfc_setpagecoladdr(nfc, nfc_op.row, nfc_op.col); + anfc_rw_pio_op(mtd, nfc->buf, roundup(len, 4), 1, PROG_RDID, 1, 0); + memcpy(instr->ctx.data.buf.in, nfc->buf, len); + + return 0; +} + +static int anfc_read_status_exec(struct nand_chip *chip, + const struct nand_subop *subop) +{ + const struct nand_op_instr *instr; + struct anfc_op nfc_op = {}; + unsigned int op_id, len; + struct anfc_nand_chip *achip = to_anfc_nand(chip); + struct anfc_nand_controller *nfc = to_anfc(chip->controller); + + anfc_parse_instructions(chip, subop, &nfc_op); + instr = nfc_op.data_instr; + op_id = nfc_op.data_instr_idx; + + anfc_prepare_cmd(chip, nfc_op.cmds[0], 0, 0, 0, 0); + anfc_setpktszcnt(nfc, achip->spktsize / 4, 1); + anfc_setpagecoladdr(nfc, nfc_op.row, nfc_op.col); + + anfc_enable_intrs(nfc, XFER_COMPLETE); + writel(PROG_STATUS, nfc->base + PROG_OFST); + anfc_wait_for_event(nfc); + + if (!nfc_op.data_instr) + return 0; + + len = nand_subop_get_data_len(subop, op_id); + + /* + * The Arasan NAND controller will update the status value + * returned by the flash device in FLASH_STS register. + */ + nfc->status = readl(nfc->base + FLASH_STS_OFST); + memcpy(instr->ctx.data.buf.in, &nfc->status, len); + + return 0; +} + +static int anfc_erase_type_exec(struct nand_chip *chip, + const struct nand_subop *subop) +{ + const struct nand_op_instr *instr; + struct anfc_nand_controller *nfc = to_anfc(chip->controller); + struct anfc_op nfc_op = {}; + u32 op_id; + + anfc_parse_instructions(chip, subop, &nfc_op); + instr = nfc_op.data_instr; + op_id = nfc_op.data_instr_idx; + + anfc_prepare_cmd(chip, nfc_op.cmds[0], nfc_op.cmds[1], 0, + 0, nfc_op.naddrcycles); + anfc_setpagecoladdr(nfc, nfc_op.row, nfc_op.col); + anfc_enable_intrs(nfc, XFER_COMPLETE); + writel(PROG_ERASE, nfc->base + PROG_OFST); + anfc_wait_for_event(nfc); + + return 0; +} + +static int anfc_read_param_get_feature_sp_read_type_exec(struct nand_chip *chip, + const struct nand_subop + *subop) +{ + const struct nand_op_instr *instr; + struct anfc_nand_controller *nfc = to_anfc(chip->controller); + unsigned int op_id, len; + struct anfc_op nfc_op = {}; + struct mtd_info *mtd = nand_to_mtd(chip); + + anfc_parse_instructions(chip, subop, &nfc_op); + instr = nfc_op.data_instr; + op_id = nfc_op.data_instr_idx; + + anfc_prepare_cmd(chip, nfc_op.cmds[0], 0, 1, mtd->writesize, + nfc_op.naddrcycles); + anfc_setpagecoladdr(nfc, nfc_op.row, nfc_op.col); + + if (!nfc_op.data_instr) + return 0; + + len = nand_subop_get_data_len(subop, op_id); + anfc_read_data_op(chip, nfc->buf, roundup(len, 4), + 1, 0); + memcpy(instr->ctx.data.buf.in, nfc->buf, len); + + return 0; +} + +static int anfc_random_datain_type_exec(struct nand_chip *chip, + const struct nand_subop *subop) +{ + const struct nand_op_instr *instr; + struct anfc_nand_controller *nfc = to_anfc(chip->controller); + unsigned int op_id, len; + struct anfc_op nfc_op = {}; + struct mtd_info *mtd = nand_to_mtd(chip); + + anfc_parse_instructions(chip, subop, &nfc_op); + instr = nfc_op.data_instr; + op_id = nfc_op.data_instr_idx; + + len = nand_subop_get_data_len(subop, op_id); + anfc_rw_pio_op(mtd, nfc->buf, roundup(len, 4), 1, PROG_PGRD, 1, 0); + memcpy(instr->ctx.data.buf.in, nfc->buf, len); + + return 0; +} + +static int anfc_setfeature_type_exec(struct nand_chip *chip, + const struct nand_subop *subop) +{ + const struct nand_op_instr *instr; + struct anfc_nand_controller *nfc = to_anfc(chip->controller); + unsigned int op_id, len; + struct anfc_op nfc_op = {}; + struct mtd_info *mtd = nand_to_mtd(chip); + + anfc_parse_instructions(chip, subop, &nfc_op); + instr = nfc_op.data_instr; + op_id = nfc_op.data_instr_idx; + + anfc_prepare_cmd(chip, nfc_op.cmds[0], 0, 0, 0, nfc_op.naddrcycles); + anfc_setpagecoladdr(nfc, nfc_op.row, nfc_op.col); + + if (!nfc_op.data_instr) + return 0; + + len = nand_subop_get_data_len(subop, op_id); + memcpy(nfc->buf, (char *)instr->ctx.data.buf.out, len); + anfc_rw_pio_op(mtd, nfc->buf, roundup(len, 4), 0, PROG_SET_FEATURE, 1, + 0); + + return 0; +} + +static int anfc_change_read_column_type_exec(struct nand_chip *chip, + const struct nand_subop *subop) +{ + const struct nand_op_instr *instr; + struct anfc_nand_controller *nfc = to_anfc(chip->controller); + unsigned int op_id, len; + struct anfc_op nfc_op = {}; + struct mtd_info *mtd = nand_to_mtd(chip); + + anfc_parse_instructions(chip, subop, &nfc_op); + instr = nfc_op.data_instr; + op_id = nfc_op.data_instr_idx; + + anfc_prepare_cmd(chip, nfc_op.cmds[0], nfc_op.cmds[1], 1, + mtd->writesize, nfc_op.naddrcycles); + anfc_setpagecoladdr(nfc, nfc_op.row, nfc_op.col); + + if (!nfc_op.data_instr) + return 0; + + len = nand_subop_get_data_len(subop, op_id); + anfc_rw_pio_op(mtd, nfc->buf, roundup(len, 4), 1, PROG_PGRD, 1, 0); + memcpy(instr->ctx.data.buf.in, nfc->buf, len); + + return 0; +} + +static int anfc_page_read_type_exec(struct nand_chip *chip, + const struct nand_subop *subop) +{ + const struct nand_op_instr *instr; + struct anfc_nand_chip *achip = to_anfc_nand(chip); + struct anfc_nand_controller *nfc = to_anfc(chip->controller); + unsigned int op_id, len; + struct anfc_op nfc_op = {}; + struct mtd_info *mtd = nand_to_mtd(chip); + + anfc_parse_instructions(chip, subop, &nfc_op); + instr = nfc_op.data_instr; + op_id = nfc_op.data_instr_idx; + anfc_prepare_cmd(chip, nfc_op.cmds[0], nfc_op.cmds[1], 1, + mtd->writesize, nfc_op.naddrcycles); + anfc_setpagecoladdr(nfc, nfc_op.row, nfc_op.col); + + if (!nfc_op.data_instr) + return 0; + + len = nand_subop_get_data_len(subop, op_id); + anfc_read_data_op(chip, instr->ctx.data.buf.in, mtd->writesize, + DIV_ROUND_UP(mtd->writesize, achip->pktsize), + achip->pktsize); + + return 0; +} + +static int anfc_zero_len_page_write_type_exec(struct nand_chip *chip, + const struct nand_subop *subop) +{ + const struct nand_op_instr *instr; + struct anfc_nand_controller *nfc = to_anfc(chip->controller); + unsigned int op_id; + struct anfc_op nfc_op = {}; + struct mtd_info *mtd = nand_to_mtd(chip); + + anfc_parse_instructions(chip, subop, &nfc_op); + instr = nfc_op.data_instr; + op_id = nfc_op.data_instr_idx; + + anfc_prepare_cmd(chip, nfc_op.cmds[0], nfc_op.cmds[1], 1, + mtd->writesize, nfc_op.naddrcycles); + anfc_setpagecoladdr(nfc, nfc_op.row, nfc_op.col); + + return 0; +} + +static int anfc_page_write_type_exec(struct nand_chip *chip, + const struct nand_subop *subop) +{ + const struct nand_op_instr *instr; + struct anfc_nand_chip *achip = to_anfc_nand(chip); + struct anfc_nand_controller *nfc = to_anfc(chip->controller); + unsigned int op_id, len; + struct anfc_op nfc_op = {}; + struct mtd_info *mtd = nand_to_mtd(chip); + + anfc_parse_instructions(chip, subop, &nfc_op); + instr = nfc_op.data_instr; + op_id = nfc_op.data_instr_idx; + anfc_prepare_cmd(chip, nfc_op.cmds[0], nfc_op.cmds[1], 1, + mtd->writesize, nfc_op.naddrcycles); + anfc_setpagecoladdr(nfc, nfc_op.row, nfc_op.col); + if (!nfc_op.data_instr) + return 0; + + len = nand_subop_get_data_len(subop, op_id); + anfc_write_data_op(chip, (char *)instr->ctx.data.buf.out, + mtd->writesize, + DIV_ROUND_UP(mtd->writesize, achip->pktsize), + achip->pktsize); + + return 0; +} + +static const struct nand_op_parser anfc_op_parser = NAND_OP_PARSER( + /* Use a separate function for each pattern */ + NAND_OP_PARSER_PATTERN( + anfc_random_datain_type_exec, + NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, ANFC_MAX_CHUNK_SIZE)), + NAND_OP_PARSER_PATTERN( + anfc_change_read_column_type_exec, + NAND_OP_PARSER_PAT_CMD_ELEM(false), + NAND_OP_PARSER_PAT_ADDR_ELEM(false, ANFC_MAX_ADDR_CYCLES), + NAND_OP_PARSER_PAT_CMD_ELEM(false), + NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, ANFC_MAX_CHUNK_SIZE)), + NAND_OP_PARSER_PATTERN( + anfc_page_read_type_exec, + NAND_OP_PARSER_PAT_CMD_ELEM(false), + NAND_OP_PARSER_PAT_ADDR_ELEM(false, ANFC_MAX_ADDR_CYCLES), + NAND_OP_PARSER_PAT_CMD_ELEM(false), + NAND_OP_PARSER_PAT_WAITRDY_ELEM(false), + NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, ANFC_MAX_CHUNK_SIZE)), + NAND_OP_PARSER_PATTERN( + anfc_page_write_type_exec, + NAND_OP_PARSER_PAT_CMD_ELEM(false), + NAND_OP_PARSER_PAT_ADDR_ELEM(false, ANFC_MAX_ADDR_CYCLES), + NAND_OP_PARSER_PAT_DATA_OUT_ELEM(false, ANFC_MAX_CHUNK_SIZE), + NAND_OP_PARSER_PAT_CMD_ELEM(false), + NAND_OP_PARSER_PAT_WAITRDY_ELEM(true)), + NAND_OP_PARSER_PATTERN( + anfc_read_id_type_exec, + NAND_OP_PARSER_PAT_CMD_ELEM(false), + NAND_OP_PARSER_PAT_ADDR_ELEM(false, ANFC_MAX_ADDR_CYCLES), + NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, ANFC_MAX_CHUNK_SIZE)), + NAND_OP_PARSER_PATTERN( + anfc_erase_type_exec, + NAND_OP_PARSER_PAT_CMD_ELEM(false), + NAND_OP_PARSER_PAT_ADDR_ELEM(false, ANFC_MAX_ADDR_CYCLES), + NAND_OP_PARSER_PAT_CMD_ELEM(false), + NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)), + NAND_OP_PARSER_PATTERN( + anfc_read_status_exec, + NAND_OP_PARSER_PAT_CMD_ELEM(false), + NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, 1)), + NAND_OP_PARSER_PATTERN( + anfc_reset_cmd_type_exec, + NAND_OP_PARSER_PAT_CMD_ELEM(false), + NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)), + NAND_OP_PARSER_PATTERN( + anfc_setfeature_type_exec, + NAND_OP_PARSER_PAT_CMD_ELEM(false), + NAND_OP_PARSER_PAT_ADDR_ELEM(false, ANFC_MAX_ADDR_CYCLES), + NAND_OP_PARSER_PAT_DATA_OUT_ELEM(false, ANFC_MAX_CHUNK_SIZE), + NAND_OP_PARSER_PAT_WAITRDY_ELEM(true)), + NAND_OP_PARSER_PATTERN( + anfc_read_param_get_feature_sp_read_type_exec, + NAND_OP_PARSER_PAT_CMD_ELEM(false), + NAND_OP_PARSER_PAT_ADDR_ELEM(false, ANFC_MAX_ADDR_CYCLES), + NAND_OP_PARSER_PAT_WAITRDY_ELEM(false), + NAND_OP_PARSER_PAT_DATA_IN_ELEM(true, ANFC_MAX_CHUNK_SIZE)), + NAND_OP_PARSER_PATTERN( + anfc_zero_len_page_write_type_exec, + NAND_OP_PARSER_PAT_CMD_ELEM(false), + NAND_OP_PARSER_PAT_ADDR_ELEM(false, ANFC_MAX_ADDR_CYCLES)), + ); + +static int anfc_exec_op(struct nand_chip *chip, + const struct nand_operation *op, + bool check_only) +{ + return nand_op_parser_exec_op(chip, &anfc_op_parser, + op, check_only); +} + +static void anfc_select_chip(struct nand_chip *chip, int num) +{ + u32 val; + struct anfc_nand_chip *achip = to_anfc_nand(chip); + struct anfc_nand_controller *nfc = to_anfc(chip->controller); + + if (num < 0) + return; + + val = readl(nfc->base + MEM_ADDR2_OFST); + val &= (val & ~(CS_MASK | BCH_MODE_MASK)); + val |= (achip->csnum << CS_SHIFT) | + (achip->ecc_strength << BCH_MODE_SHIFT); + writel(val, nfc->base + MEM_ADDR2_OFST); + nfc->csnum = achip->csnum; + writel(achip->eccval, nfc->base + ECC_OFST); + writel(achip->inftimeval, nfc->base + DATA_INTERFACE_OFST); +} + +static irqreturn_t anfc_irq_handler(int irq, void *ptr) +{ + struct anfc_nand_controller *nfc = ptr; + u32 status; + + status = readl(nfc->base + INTR_STS_OFST); + if (status & EVENT_MASK) { + complete(&nfc->event); + writel(status & EVENT_MASK, nfc->base + INTR_STS_OFST); + writel(0, nfc->base + INTR_STS_EN_OFST); + writel(0, nfc->base + INTR_SIG_EN_OFST); + return IRQ_HANDLED; + } + + return IRQ_NONE; +} + +static int anfc_setup_data_interface(struct nand_chip *chip, int csline, + const struct nand_data_interface *conf) +{ + struct anfc_nand_controller *nfc = to_anfc(chip->controller); + struct anfc_nand_chip *achip = to_anfc_nand(chip); + int err; + const struct nand_sdr_timings *sdr; + u32 inftimeval; + bool change_sdr_clk = false; + + if (csline == NAND_DATA_IFACE_CHECK_ONLY) + return 0; + + /* + * If the controller is already in the same mode as flash device + * then no need to change the timing mode again. + */ + sdr = nand_get_sdr_timings(conf); + if (IS_ERR(sdr)) + return PTR_ERR(sdr); + + if (sdr->mode < 0) + return -ENOTSUPP; + + inftimeval = sdr->mode & 7; + if (sdr->mode >= 2 && sdr->mode <= 5) + change_sdr_clk = true; + /* + * SDR timing modes 2-5 will not work for the arasan nand when + * freq > 90 MHz, so reduce the freq in SDR modes 2-5 to < 90Mhz + */ + if (change_sdr_clk) { + clk_disable_unprepare(nfc->clk_sys); + err = clk_set_rate(nfc->clk_sys, SDR_MODE_DEFLT_FREQ); + if (err) { + dev_err(nfc->dev, "Can't set the clock rate\n"); + return err; + } + err = clk_prepare_enable(nfc->clk_sys); + if (err) { + dev_err(nfc->dev, "Unable to enable sys clock.\n"); + clk_disable_unprepare(nfc->clk_sys); + return err; + } + } + achip->inftimeval = inftimeval; + + return 0; +} + +static int anfc_nand_attach_chip(struct nand_chip *chip) +{ + struct mtd_info *mtd = nand_to_mtd(chip); + struct anfc_nand_chip *achip = to_anfc_nand(chip); + u32 ret; + + if (mtd->writesize <= SZ_512) + achip->spare_caddr_cycles = 1; + else + achip->spare_caddr_cycles = 2; + + chip->ecc.calc_buf = kmalloc(mtd->oobsize, GFP_KERNEL); + chip->ecc.code_buf = kmalloc(mtd->oobsize, GFP_KERNEL); + ret = anfc_ecc_init(mtd, &chip->ecc, chip->ecc.mode); + if (ret) + return ret; + + return 0; +} + +static const struct nand_controller_ops anfc_nand_controller_ops = { + .attach_chip = anfc_nand_attach_chip, +}; + +static int anfc_nand_chip_init(struct anfc_nand_controller *nfc, + struct anfc_nand_chip *anand_chip, + struct device_node *np) +{ + struct nand_chip *chip = &anand_chip->chip; + struct mtd_info *mtd = nand_to_mtd(chip); + int ret; + + ret = of_property_read_u32(np, "reg", &anand_chip->csnum); + if (ret) { + dev_err(nfc->dev, "can't get chip-select\n"); + return -ENXIO; + } + mtd->name = devm_kasprintf(nfc->dev, GFP_KERNEL, "arasan_nand.%d", + anand_chip->csnum); + mtd->dev.parent = nfc->dev; + chip->controller = &nfc->controller; + chip->options = NAND_BUSWIDTH_AUTO | NAND_NO_SUBPAGE_WRITE; + chip->bbt_options = NAND_BBT_USE_FLASH; + chip->select_chip = anfc_select_chip; + chip->setup_data_interface = anfc_setup_data_interface; + chip->exec_op = anfc_exec_op; + nand_set_flash_node(chip, np); + + anand_chip->spktsize = SDR_MODE_PACKET_SIZE; + + ret = nand_scan(chip, 1); + if (ret) { + dev_err(nfc->dev, "nand_scan_tail for NAND failed\n"); + return ret; + } + + return mtd_device_register(mtd, NULL, 0); +} + +static int anfc_probe(struct platform_device *pdev) +{ + struct anfc_nand_controller *nfc; + struct anfc_nand_chip *anand_chip; + struct device_node *np = pdev->dev.of_node, *child; + struct resource *res; + int err; + + nfc = devm_kzalloc(&pdev->dev, sizeof(*nfc), GFP_KERNEL); + if (!nfc) + return -ENOMEM; + + nand_controller_init(&nfc->controller); + INIT_LIST_HEAD(&nfc->chips); + init_completion(&nfc->event); + nfc->dev = &pdev->dev; + platform_set_drvdata(pdev, nfc); + nfc->csnum = -1; + nfc->controller.ops = &anfc_nand_controller_ops; + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); + nfc->base = devm_ioremap_resource(&pdev->dev, res); + if (IS_ERR(nfc->base)) + return PTR_ERR(nfc->base); + nfc->irq = platform_get_irq(pdev, 0); + if (nfc->irq < 0) { + dev_err(&pdev->dev, "platform_get_irq failed\n"); + return -ENXIO; + } + dma_set_mask(&pdev->dev, DMA_BIT_MASK(64)); + err = devm_request_irq(&pdev->dev, nfc->irq, anfc_irq_handler, + 0, "arasannfc", nfc); + if (err) + return err; + nfc->clk_sys = devm_clk_get(&pdev->dev, "clk_sys"); + if (IS_ERR(nfc->clk_sys)) { + dev_err(&pdev->dev, "sys clock not found.\n"); + return PTR_ERR(nfc->clk_sys); + } + + nfc->clk_flash = devm_clk_get(&pdev->dev, "clk_flash"); + if (IS_ERR(nfc->clk_flash)) { + dev_err(&pdev->dev, "flash clock not found.\n"); + return PTR_ERR(nfc->clk_flash); + } + + err = clk_prepare_enable(nfc->clk_sys); + if (err) { + dev_err(&pdev->dev, "Unable to enable sys clock.\n"); + return err; + } + + err = clk_prepare_enable(nfc->clk_flash); + if (err) { + dev_err(&pdev->dev, "Unable to enable flash clock.\n"); + goto clk_dis_sys; + } + + for_each_available_child_of_node(np, child) { + anand_chip = devm_kzalloc(&pdev->dev, sizeof(*anand_chip), + GFP_KERNEL); + if (!anand_chip) { + of_node_put(child); + err = -ENOMEM; + goto nandchip_clean_up; + } + err = anfc_nand_chip_init(nfc, anand_chip, child); + if (err) { + devm_kfree(&pdev->dev, anand_chip); + continue; + } + + list_add_tail(&anand_chip->node, &nfc->chips); + } + return 0; + +nandchip_clean_up: + list_for_each_entry(anand_chip, &nfc->chips, node) + nand_release(&anand_chip->chip); + clk_disable_unprepare(nfc->clk_flash); +clk_dis_sys: + clk_disable_unprepare(nfc->clk_sys); + + return err; +} + +static int anfc_remove(struct platform_device *pdev) +{ + struct anfc_nand_controller *nfc = platform_get_drvdata(pdev); + struct anfc_nand_chip *anand_chip; + + list_for_each_entry(anand_chip, &nfc->chips, node) + nand_release(&anand_chip->chip); + + clk_disable_unprepare(nfc->clk_sys); + clk_disable_unprepare(nfc->clk_flash); + + return 0; +} + +static const struct of_device_id anfc_ids[] = { + { .compatible = "arasan,nfc-v3p10" }, + { .compatible = "xlnx,zynqmp-nand" }, + { } +}; +MODULE_DEVICE_TABLE(of, anfc_ids); + +static struct platform_driver anfc_driver = { + .driver = { + .name = "arasan-nand-controller", + .of_match_table = anfc_ids, + }, + .probe = anfc_probe, + .remove = anfc_remove, +}; +module_platform_driver(anfc_driver); + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Xilinx, Inc"); +MODULE_DESCRIPTION("Arasan NAND Flash Controller Driver"); -- 2.7.4