From nobody Fri Nov 29 14:53:50 2024 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E5A95179658; Wed, 18 Sep 2024 09:03:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726650199; cv=none; b=eU/x0VNjYrBGBVsWB7g9sb7kFWMIahtjUXrlxfx9LjllojCQ8rCYYDu5kxA7DSdfnlL9rUryKzSFSDCqrXebMTtVFBKW0MriR1mLY7cOJKBJKMxKsupZkXm6RJYuF/ImQR8CTwS+VcDoajOuT7aQXGzjGQ2TQl77epABWnyhqmM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726650199; c=relaxed/simple; bh=1z9fnWUuNK0Pc7cPE4EqMowbgf5W39Nzy/4+d/iZNHk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=kxPZp78mUfVXJ0q6fEKNTYAuNZ4vARCBH1vyJLq6eKuJZK2A5G2z+8csvA72vapRlECV0IcyrGHmlAeJSs/GdfzAv/StPYoE6YG1lnTPy1sqlqk8awrOrGgjY5PJQQt97le+24BBx9VuD7XnrFeCh1tHCs+dUeCx5A5zd/cTo3U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=gLKgHtuG; arc=none smtp.client-ip=209.85.214.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="gLKgHtuG" Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-20543fdb7acso38443805ad.1; Wed, 18 Sep 2024 02:03:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1726650197; x=1727254997; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=AjX+GWxHlPwm32RdWG50sup+7/ylH4rWNpxlnfN528U=; b=gLKgHtuGNnmdJL/clxOrg4Eb7frWGhjImg0y4MThjmqcNTfzBf2h+uJcbJ0cbzinD4 clTimxipRUzGskjEdGSGNEj49yp6WIfbK9+bvGmGy7z9qLDE0BOvI0EMsqAbgabqpKMg nByLyYn0uqCidzS7Mej+HCpHGA/5EzvpF862I6/XDFlJZw0vcmXc1dbLjdh9BRNcKo34 HGV4nv2n7+hr1EpYh2sM8ElKqDEQaX8HKsmVY+T3OXDXe1ObPA5NqhNfg8dmuMQFd+CP Qh1RHOsXJkCeSUSSgcYeTNwjoj2ScpLF/4g3naHFO5AGJNSF2tSZ498c6eqJQjMVVZce stEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726650197; x=1727254997; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=AjX+GWxHlPwm32RdWG50sup+7/ylH4rWNpxlnfN528U=; b=CANLmDeTMvnABAgvsCzSDwtiIkEQZIHh2ChnNYue+Y8o6nV13NlkEpaudGr4Q5rzeL +bv9+TuD4sPI6rUPUtrFxeJ3o9ljIaMO4MAbkIL+KsLLjgZ+WObnf9BGqubNLIyOGGZv lFkc1IRYJpsb8ZwGdQHZperXJkYyJMsu8Yj7ZqP+SuBMoDCiKV6rRquD1aXEHsb5fc1B Lszn89vn5ihPJWRJkZj+E4H/bOmZQvwZPpRIffg+0QJ56Qic+K9qgj7REC1WWR0KJwSg FhGzxvTAzMzrF1piegVnoRCB1WW1/JBmPUlOhfAPdqeiYqMp4sOeomNoQE1zng3kYNtb qmcg== X-Forwarded-Encrypted: i=1; AJvYcCUpJYGB2c4vYi25JcfpEGxxr85ja50ShUk4aVWKKIOFS5pifrINcSvE+Y7xOa7YKYGGcJbQCK32ZcZdlh4=@vger.kernel.org, AJvYcCUsjNl6fPDS++rjX1ojA/SaXv+koA7RWZqJQf/NCSBg29714X/af9osUAJjmkBqJVyh8VifILpmtxLJ@vger.kernel.org, AJvYcCVeZ0q3LziR/bOUTgsSnQJxt3ZUMjqckQLCZ8Y0FIbxiDCEiXPyV6PsckT1FQCoFcD15naGnYJc4d+0Wk5f@vger.kernel.org X-Gm-Message-State: AOJu0YzUj3NcQpxZKrw9UYJ7IVdE2Opo/UAYN2xSxW+DlnTTLFA3Sp8W CIw8L+dP28E8yugJOO1U21+xskdWTKfnwRkih6pahvwq1CMXbQLX X-Google-Smtp-Source: AGHT+IEu2ExwD56L0eMxFaxhnTGWr5C0LOkIV1Cbr4483wISjxblVpLqyl0/2mVScIM1AHAa8dS/2Q== X-Received: by 2002:a17:902:e852:b0:203:a14d:ed0 with SMTP id d9443c01a7336-20781b42d61mr326944145ad.11.1726650196885; Wed, 18 Sep 2024 02:03:16 -0700 (PDT) Received: from localhost.localdomain (60-250-196-139.hinet-ip.hinet.net. [60.250.196.139]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-207946010f9sm61021875ad.84.2024.09.18.02.03.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Sep 2024 02:03:16 -0700 (PDT) From: Hui-Ping Chen To: miquel.raynal@bootlin.com, richard@nod.at, vigneshr@ti.com, robh@kernel.org, krzk+dt@kernel.org, conor+dt@kernel.org, sumit.semwal@linaro.org, christian.koenig@amd.com, esben@geanix.com Cc: linux-arm-kernel@lists.infradead.org, linux-mtd@lists.infradead.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, Hui-Ping Chen , Krzysztof Kozlowski Subject: [PATCH v4 1/2] dt-bindings: mtd: nuvoton,ma35d1-nand: add new bindings Date: Wed, 18 Sep 2024 09:03:07 +0000 Message-Id: <20240918090308.292617-2-hpchen0nvt@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240918090308.292617-1-hpchen0nvt@gmail.com> References: <20240918090308.292617-1-hpchen0nvt@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add dt-bindings for the Nuvoton MA35 SoC NAND Controller. Signed-off-by: Hui-Ping Chen Reviewed-by: Krzysztof Kozlowski --- .../bindings/mtd/nuvoton,ma35d1-nand.yaml | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 Documentation/devicetree/bindings/mtd/nuvoton,ma35d1-na= nd.yaml diff --git a/Documentation/devicetree/bindings/mtd/nuvoton,ma35d1-nand.yaml= b/Documentation/devicetree/bindings/mtd/nuvoton,ma35d1-nand.yaml new file mode 100644 index 000000000000..a8a549644c98 --- /dev/null +++ b/Documentation/devicetree/bindings/mtd/nuvoton,ma35d1-nand.yaml @@ -0,0 +1,93 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/mtd/nuvoton,ma35d1-nand.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Nuvoton MA35D1 NAND Flash Interface (NFI) Controller + +maintainers: + - Hui-Ping Chen + +allOf: + - $ref: nand-controller.yaml# + +properties: + compatible: + enum: + - nuvoton,ma35d1-nand-controller + + reg: + maxItems: 1 + + interrupts: + maxItems: 1 + + clocks: + maxItems: 1 + +patternProperties: + "^nand@[a-f0-9]$": + type: object + $ref: raw-nand-chip.yaml + properties: + nand-ecc-step-size: + enum: [512, 1024] + + nand-ecc-strength: + enum: [8, 12, 24] + + required: + - nand-ecc-step-size + - nand-ecc-strength + + unevaluatedProperties: false + +required: + - compatible + - reg + - interrupts + - clocks + +unevaluatedProperties: false + +examples: + - | + #include + #include + + soc { + #address-cells =3D <2>; + #size-cells =3D <2>; + + nand-controller@401A0000 { + compatible =3D "nuvoton,ma35d1-nand-controller"; + reg =3D <0x0 0x401A0000 0x0 0x1000>; + interrupts =3D ; + clocks =3D <&clk NAND_GATE>; + #address-cells =3D <1>; + #size-cells =3D <0>; + + nand@0 { + reg =3D <0>; + + nand-on-flash-bbt; + nand-ecc-step-size =3D <512>; + nand-ecc-strength =3D <8>; + + partitions { + compatible =3D "fixed-partitions"; + #address-cells =3D <1>; + #size-cells =3D <1>; + + uboot@0 { + label =3D "nand-uboot"; + read-only; + reg =3D <0x0 0x300000>; + }; + }; + }; + }; + }; + +... --=20 2.25.1 From nobody Fri Nov 29 14:53:50 2024 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3DFE817C7BB; Wed, 18 Sep 2024 09:03:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726650203; cv=none; b=YlXHB5HnKZB3wHFPjyAtIyru7u5JxVq7rwciCC7YK5hmI2c+OcSABfTwJtyfkAPkqnK00zH5DdBLMAKe5UVKmpbZ457DLRd7IoAowkoD2rm7NoJcIXJBSWr8FA3SXLpxKmJ1QnCw1fET0Xp/OR8OSFr4+0s3U58uDIj4k2N/ynI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726650203; c=relaxed/simple; bh=zAtx6vH91VfNm4spqv2Lo6L9jKCX1m1busbgrYxsQGg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=i6ykvMDEa5p+VvOg0sSOK6rr192uZK2mZvKAvM2oKGpK8/vUSKuE5cGuAkdatLBqM8boQjqclUl4JKr7rNao49miAbrNuEMmla6X3QGtUzP4pVaQtXt3l9H/ISzREiHO0+2B/WEHfks3FH0gjTrNPK6+8hozm9EijLrEVC1O31k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=JN6whrNy; arc=none smtp.client-ip=209.85.214.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="JN6whrNy" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-206f9b872b2so59294725ad.3; Wed, 18 Sep 2024 02:03:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1726650200; x=1727255000; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=hNtbC7DlCoR/OUToXi/QcEm3enZRPjoUdTSvBrwT24M=; b=JN6whrNyiznXZRGEEwWg8MQykRK8HTKAKTrV7ynnyFnf4LpRoehzHUvgjCyRu1NzEv ObFMvAInp8f7Qacsx2tYXV64kO0rLK4jYSePLIGPK/V+qcM8pG5zOB1s70IuIgEoJhYp HymxRKWfx0eJASOKp2VEcGG4kOJHxzC+xCz9EngacF8H8jt4ppavxHz3T1nzD/BVFE/N ZedGQFcf8ZMSssW2z7iwCoSgPqsu8Zl+AoTcR6EJLf1B03thldFFzhsZ4xeYJrfVmsR0 evsskUin3ZXKQXYeKmPUwuLQ7lbyKtXMCcfpjJzFyKg9+71DNSb3zsn4kLVdUDycyBBb be+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726650200; x=1727255000; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hNtbC7DlCoR/OUToXi/QcEm3enZRPjoUdTSvBrwT24M=; b=pMBbqrz1Qvu80rgaOGluztLowimVlr840EHQBg8L8vh1hsuk5UCJt7dphm56dFMb5m FonFDxDY2+lvkY7zvRm1PPhOM+k9YpR+VOTdilwi+9Ashx6VjwFLUp/qxaDlx+5Y5Pi4 AcxVj/toAdx6hD1qkqfFRuyaz69d9YOq0KfeCO97h7dYPC2p35VxC1iBcL4IhAHCjvss 8H7UXnnGphNMy9DG5vvKj5aCfqzc3fTGC2laQg4IHKB6ItD9C3EM3us0sf4QtpAaW+mb N/GHvoDZTud0LbIXYp2nR6aQPdrJNGPkMxv/mScLUIdovA+C/p8OA2N0xTnhR9dqrFXv zFmQ== X-Forwarded-Encrypted: i=1; AJvYcCUzGH5AVcjlc2yzyHC2TOTInsPT0ElV77JUKLnDz0H4Q3/yJ1HODtY8LUf+D65uShex1BW5R8gwZi3IfRs=@vger.kernel.org, AJvYcCVX7dYVj2wCyui5+nAPtZV/LnJAKoIBJl2A3f5kycO5YhEf1uGctBDf6VrAeTAHNlPcHTjpktRu6/IG@vger.kernel.org, AJvYcCXN1iX6Wrbbl2mKKGEdfAs1JItZzyAHsZG+V4GOHPWktj3xftODoEOeYmJCCEezNxCyUKjQoLcDROf0cx89@vger.kernel.org X-Gm-Message-State: AOJu0YyZ3yH7Wr5HRwTq+XupeVpC9S6OsVoBdDJzXqZEtXjENj5mwydU 4TeDnf8ivhc+ih18AHOT1yGkFS1C8JvyjlWrIoyApQyRQGkGzPTL X-Google-Smtp-Source: AGHT+IFaCMyEEjZKFwncs35JRly0dOAsAxqkLNSDFIRH74lsexQMxCdfKZgvqoOhs6XswXqONBYhpw== X-Received: by 2002:a17:902:d491:b0:205:56e8:4a4c with SMTP id d9443c01a7336-2076e3d584bmr309537635ad.26.1726650200100; Wed, 18 Sep 2024 02:03:20 -0700 (PDT) Received: from localhost.localdomain (60-250-196-139.hinet-ip.hinet.net. [60.250.196.139]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-207946010f9sm61021875ad.84.2024.09.18.02.03.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Sep 2024 02:03:19 -0700 (PDT) From: Hui-Ping Chen To: miquel.raynal@bootlin.com, richard@nod.at, vigneshr@ti.com, robh@kernel.org, krzk+dt@kernel.org, conor+dt@kernel.org, sumit.semwal@linaro.org, christian.koenig@amd.com, esben@geanix.com Cc: linux-arm-kernel@lists.infradead.org, linux-mtd@lists.infradead.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, Hui-Ping Chen Subject: [PATCH v4 2/2] mtd: rawnand: nuvoton: add new driver for the Nuvoton MA35 SoC Date: Wed, 18 Sep 2024 09:03:08 +0000 Message-Id: <20240918090308.292617-3-hpchen0nvt@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240918090308.292617-1-hpchen0nvt@gmail.com> References: <20240918090308.292617-1-hpchen0nvt@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Nuvoton MA35 SoCs NAND Flash Interface Controller supports 2kiB, 4kiB and 8kiB page size, and up to 8-bit, 12-bit, and 24-bit hardware ECC calculation circuit to protect data. Signed-off-by: Hui-Ping Chen --- drivers/mtd/nand/raw/Kconfig | 8 + drivers/mtd/nand/raw/Makefile | 1 + drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c | 935 +++++++++++++++++++++ 3 files changed, 944 insertions(+) create mode 100644 drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c diff --git a/drivers/mtd/nand/raw/Kconfig b/drivers/mtd/nand/raw/Kconfig index 614257308516..a95d91e61c42 100644 --- a/drivers/mtd/nand/raw/Kconfig +++ b/drivers/mtd/nand/raw/Kconfig @@ -448,6 +448,14 @@ config MTD_NAND_RENESAS Enables support for the NAND controller found on Renesas R-Car Gen3 and RZ/N1 SoC families. =20 +config MTD_NAND_NUVOTON_MA35 + tristate "Nuvoton MA35 SoC NAND controller" + depends on ARCH_MA35 || COMPILE_TEST + depends on OF + help + Enables support for the NAND controller found on + the Nuvoton MA35 series SoCs. + comment "Misc" =20 config MTD_SM_COMMON diff --git a/drivers/mtd/nand/raw/Makefile b/drivers/mtd/nand/raw/Makefile index 25120a4afada..b8e1b3af6942 100644 --- a/drivers/mtd/nand/raw/Makefile +++ b/drivers/mtd/nand/raw/Makefile @@ -57,6 +57,7 @@ obj-$(CONFIG_MTD_NAND_INTEL_LGM) +=3D intel-nand-controll= er.o obj-$(CONFIG_MTD_NAND_ROCKCHIP) +=3D rockchip-nand-controller.o obj-$(CONFIG_MTD_NAND_PL35X) +=3D pl35x-nand-controller.o obj-$(CONFIG_MTD_NAND_RENESAS) +=3D renesas-nand-controller.o +obj-$(CONFIG_MTD_NAND_NUVOTON_MA35) +=3D nuvoton_ma35d1_nand.o =20 nand-objs :=3D nand_base.o nand_legacy.o nand_bbt.o nand_timings.o nand_id= s.o nand-objs +=3D nand_onfi.o diff --git a/drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c b/drivers/mtd/nand/= raw/nuvoton_ma35d1_nand.c new file mode 100644 index 000000000000..30cbcb999f1a --- /dev/null +++ b/drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c @@ -0,0 +1,935 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2024 Nuvoton Technology Corp. + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +/* NFI Registers */ +#define MA35_NFI_REG_DMACTL 0x400 +#define DMA_EN BIT(0) +#define DMA_RST BIT(1) +#define DMA_BUSY BIT(9) + +#define MA35_NFI_REG_DMASA 0x408 +#define MA35_NFI_REG_GCTL 0x800 +#define NAND_EN BIT(3) + +#define MA35_NFI_REG_NANDCTL 0x8A0 +#define SWRST BIT(0) +#define DMA_R_EN BIT(1) +#define DMA_W_EN BIT(2) +#define ECC_CHK BIT(7) +#define PROT3BEN BIT(8) +#define PSIZE_2K (1 << 16) +#define PSIZE_4K (2 << 16) +#define PSIZE_8K (3 << 16) +#define PSIZE_MASK (3 << 16) +#define BCH_T24 BIT(18) +#define BCH_T8 BIT(20) +#define BCH_T12 BIT(21) +#define BCH_NONE (0x0) +#define BCH_MASK (0x1f << 18) +#define ECC_EN BIT(23) +#define DISABLE_CS0 BIT(25) + +#define MA35_NFI_REG_NANDINTEN 0x8A8 +#define MA35_NFI_REG_NANDINTSTS 0x8AC +#define INT_DMA BIT(0) +#define INT_ECC BIT(2) +#define INT_RB0 BIT(10) +#define INT_RB0_STS BIT(18) + +#define MA35_NFI_REG_NANDCMD 0x8B0 +#define MA35_NFI_REG_NANDADDR 0x8B4 +#define ENDADDR BIT(31) + +#define MA35_NFI_REG_NANDDATA 0x8B8 +#define MA35_NFI_REG_NANDRACTL 0x8BC +#define MA35_NFI_REG_NANDECTL 0x8C0 +#define ENABLE_WP 0x0 +#define DISABLE_WP BIT(0) + +#define MA35_NFI_REG_NANDECCES0 0x8D0 +#define ECC_STATUS_MASK 0x3 +#define ECC_ERR_CNT_MASK 0x1f + +#define MA35_NFI_REG_NANDECCEA0 0x900 +#define MA35_NFI_REG_NANDECCED0 0x960 +#define MA35_NFI_REG_NANDRA0 0xA00 + +#define SKIP_SPARE_BYTES 4 + + +/* Define for the BCH hardware ECC engine */ +/* define the total padding bytes for 512/1024 data segment */ +#define MA35_BCH_PADDING_512 32 +#define MA35_BCH_PADDING_1024 64 +/* define the BCH parity code length for 512 bytes data pattern */ +#define MA35_PARITY_BCH8 15 +#define MA35_PARITY_BCH12 23 +/* define the BCH parity code length for 1024 bytes data pattern */ +#define MA35_PARITY_BCH24 45 + + +struct ma35_nand_info { + struct nand_controller controller; + struct nand_chip chip; + struct device *dev; + void __iomem *regs; + int irq; + struct clk *clk; + struct completion complete; + + u32 bch; + u8 *dma_buf; + u8 *ecc_buf; + spinlock_t dma_lock; + dma_addr_t dma_addr; +}; + +static int ma35_ooblayout_ecc(struct mtd_info *mtd, int section, + struct mtd_oob_region *oobregion) +{ + struct nand_chip *chip =3D mtd_to_nand(mtd); + + if (section) + return -ERANGE; + + oobregion->length =3D chip->ecc.total; + oobregion->offset =3D mtd->oobsize - oobregion->length; + + return 0; +} + +static int ma35_ooblayout_free(struct mtd_info *mtd, int section, + struct mtd_oob_region *oobregion) +{ + struct nand_chip *chip =3D mtd_to_nand(mtd); + + if (section) + return -ERANGE; + + oobregion->length =3D mtd->oobsize - chip->ecc.total - 2; + oobregion->offset =3D 2; + + return 0; +} + +static const struct mtd_ooblayout_ops ma35_ooblayout_ops =3D { + .free =3D ma35_ooblayout_free, + .ecc =3D ma35_ooblayout_ecc, +}; + +static inline void ma35_clear_spare(struct nand_chip *chip, int size) +{ + struct ma35_nand_info *nand =3D nand_get_controller_data(chip); + int i; + + for (i =3D 0; i < size/4; i++) + writel(0xff, nand->regs + MA35_NFI_REG_NANDRA0); +} + +static inline void read_remaining_bytes(struct ma35_nand_info *nand, u32 *= buf, + u32 offset, int size) +{ + u32 value =3D readl(nand->regs + MA35_NFI_REG_NANDRA0 + offset); + u8 *ptr =3D (u8 *)buf; + int i; + + for (i =3D 0; i < size; i++) + ptr[i] =3D (value >> (i * 8)) & 0xff; +} + + +static inline void ma35_read_spare(struct nand_chip *chip, int size, u32 *= buf, u32 offset) +{ + struct ma35_nand_info *nand =3D nand_get_controller_data(chip); + int i, j; + + if ((offset % 4) =3D=3D 0) { + for (i =3D 0, j =3D 0; i < size / 4; i++, j +=3D 4) + *buf++ =3D readl(nand->regs + MA35_NFI_REG_NANDRA0 + offset + j); + + read_remaining_bytes(nand, buf, offset + j, size % 4); + } else { + read_remaining_bytes(nand, buf, offset, 4 - (offset % 4)); + offset +=3D 4; + size -=3D (4 - (offset % 4)); + + for (i =3D 0, j =3D 0; i < size / 4; i++, j +=3D 4) + *buf++ =3D readl(nand->regs + MA35_NFI_REG_NANDRA0 + offset + j); + + read_remaining_bytes(nand, buf, offset + j, size % 4); + } +} + +static inline void ma35_write_spare(struct nand_chip *chip, int size, u32 = *buf) +{ + struct ma35_nand_info *nand =3D nand_get_controller_data(chip); + u32 value; + int i, j; + u8 *ptr; + + for (i =3D 0, j =3D 0; i < size / 4; i++, j +=3D 4) + writel(*buf++, nand->regs + MA35_NFI_REG_NANDRA0 + j); + + ptr =3D (u8 *)buf; + switch (size % 4) { + case 1: + writel(*ptr, nand->regs + MA35_NFI_REG_NANDRA0 + j); + break; + case 2: + value =3D *ptr | (*(ptr+1) << 8); + writel(value, nand->regs + MA35_NFI_REG_NANDRA0 + j); + break; + case 3: + value =3D *ptr | (*(ptr+1) << 8) | (*(ptr+2) << 16); + writel(value, nand->regs + MA35_NFI_REG_NANDRA0 + j); + break; + default: + break; + } +} + +static inline void ma35_nand_target_enable(struct ma35_nand_info *nand) +{ + writel(readl(nand->regs + MA35_NFI_REG_NANDCTL) & (~DISABLE_CS0), + nand->regs+MA35_NFI_REG_NANDCTL); +} + +static inline void ma35_nand_target_disable(struct ma35_nand_info *nand) +{ + writel(readl(nand->regs + MA35_NFI_REG_NANDCTL) | DISABLE_CS0, + nand->regs + MA35_NFI_REG_NANDCTL); +} + +/* + * Initialize hardware ECC + */ +static void ma35_nand_hwecc_init(struct ma35_nand_info *nand) +{ + struct mtd_info *mtd =3D nand_to_mtd(&nand->chip); + u32 reg; + + /* resets the internal state machine and counters */ + reg =3D readl(nand->regs + MA35_NFI_REG_NANDCTL); + reg |=3D SWRST; + writel(reg, nand->regs + MA35_NFI_REG_NANDCTL); + while (readl(nand->regs + MA35_NFI_REG_NANDCTL) & SWRST) + ; + + /* Redundant area size */ + writel(mtd->oobsize, nand->regs + MA35_NFI_REG_NANDRACTL); + + /* Protect redundant 3 bytes */ + reg =3D readl(nand->regs + MA35_NFI_REG_NANDCTL); + reg |=3D (PROT3BEN | ECC_CHK); + writel(reg, nand->regs + MA35_NFI_REG_NANDCTL); + + if (nand->bch =3D=3D BCH_NONE) { + /* Disable H/W ECC, ECC parity check enable bit during read page */ + writel(readl(nand->regs + MA35_NFI_REG_NANDCTL) & (~ECC_EN), + nand->regs + MA35_NFI_REG_NANDCTL); + } else { + /* Set BCH algorithm */ + writel((readl(nand->regs + MA35_NFI_REG_NANDCTL) & (~BCH_MASK)) | + nand->bch, nand->regs + MA35_NFI_REG_NANDCTL); + + /* Enable H/W ECC, ECC parity check enable bit during read page */ + writel(readl(nand->regs + MA35_NFI_REG_NANDCTL) | ECC_EN, + nand->regs + MA35_NFI_REG_NANDCTL); + } + spin_lock_init(&nand->dma_lock); +} + + +/* Correct data by BCH alrogithm */ +static void ma35_nfi_correct(struct ma35_nand_info *nand, u8 index, + u8 err_cnt, u8 *addr) +{ + u32 temp_data[24], temp_addr[24]; + u32 padding_len, parity_len; + u32 value, offset, remain; + u32 err_data[6]; + u8 i, j; + + /* configurations */ + switch (nand->bch) { + case BCH_T24: + parity_len =3D MA35_PARITY_BCH24; + padding_len =3D MA35_BCH_PADDING_1024; + break; + case BCH_T12: + parity_len =3D MA35_PARITY_BCH12; + padding_len =3D MA35_BCH_PADDING_512; + break; + case BCH_T8: + parity_len =3D MA35_PARITY_BCH8; + padding_len =3D MA35_BCH_PADDING_512; + break; + default: + dev_warn(nand->dev, "NAND ERROR: invalid SMCR_BCH_TSEL =3D 0x%08X\n", + (u32)(readl(nand->regs + MA35_NFI_REG_NANDCTL) & BCH_MASK)); + return; + } + + /* got valid BCH_ECC_DATAx and parse them to temp_data[] + * got the valid register number of BCH_ECC_DATAx since + * one register include 4 error bytes + */ + j =3D (err_cnt + 3) / 4; + j =3D (j > 6) ? 6 : j; + for (i =3D 0; i < j; i++) + err_data[i] =3D readl(nand->regs + MA35_NFI_REG_NANDECCED0 + i * 4); + + for (i =3D 0; i < j; i++) { + temp_data[i*4+0] =3D err_data[i] & 0xff; + temp_data[i*4+1] =3D (err_data[i] >> 8) & 0xff; + temp_data[i*4+2] =3D (err_data[i] >> 16) & 0xff; + temp_data[i*4+3] =3D (err_data[i] >> 24) & 0xff; + } + + /* got valid REG_BCH_ECC_ADDRx and parse them to temp_addr[] + * got the valid register number of REG_BCH_ECC_ADDRx since + * one register include 2 error addresses + */ + j =3D (err_cnt + 1) / 2; + j =3D (j > 12) ? 12 : j; + for (i =3D 0; i < j; i++) { + temp_addr[i*2+0] =3D readl(nand->regs + MA35_NFI_REG_NANDECCEA0 + i * 4) + & 0x07ff; + temp_addr[i*2+1] =3D (readl(nand->regs + MA35_NFI_REG_NANDECCEA0 + i * 4= ) >> 16) + & 0x07ff; + } + + /* pointer to begin address of field that with data error */ + addr +=3D index * nand->chip.ecc.steps; + + /* correct each error bytes */ + for (i =3D 0; i < err_cnt; i++) { + u32 corrected_index =3D temp_addr[i]; + + /* for wrong data in field */ + if (corrected_index < nand->chip.ecc.steps) + *(addr + corrected_index) ^=3D temp_data[i]; + + /* for wrong first-3-bytes in redundancy area */ + else if (corrected_index < (nand->chip.ecc.steps + 3)) { + corrected_index -=3D nand->chip.ecc.steps; + temp_addr[i] +=3D (parity_len * index); /* field offset */ + + value =3D readl(nand->regs + MA35_NFI_REG_NANDRA0); + value ^=3D temp_data[i] << (8 * corrected_index); + writel(value, nand->regs + MA35_NFI_REG_NANDRA0); + } + /* for wrong parity code in redundancy area + * BCH_ERR_ADDRx =3D [data in field] + [3 bytes] + [xx] + [parity code] + * |<-- padding bytes -->| + * The BCH_ERR_ADDRx for last parity code always =3D field size + paddin= g size. + * So, the first parity code =3D field size + padding size - parity code= length. + * For example, for BCH T12, the first parity code =3D 512 + 32 - 23 =3D= 521. + * That is, error byte address offset within field is + */ + else { + corrected_index -=3D (nand->chip.ecc.steps + padding_len - parity_len); + + /* final address =3D first parity code of first field + + * offset of fields + + * offset within field + */ + offset =3D (readl(nand->regs+MA35_NFI_REG_NANDRACTL) & 0x1ff) - + (parity_len * nand->chip.ecc.steps) + + (parity_len * index) + corrected_index; + + remain =3D offset % 4; + value =3D readl(nand->regs + MA35_NFI_REG_NANDRA0 + offset - remain); + value ^=3D temp_data[i] << (8 * remain); + writel(value, nand->regs + MA35_NFI_REG_NANDRA0 + offset - remain); + } + } +} + +static int ma35_nfi_ecc_check(struct nand_chip *chip, unsigned long addr) +{ + struct ma35_nand_info *nand =3D nand_get_controller_data(chip); + struct mtd_info *mtd =3D nand_to_mtd(chip); + int status, i, j, nchunks =3D 0; + int report_err =3D 0; + int err_cnt =3D 0; + + nchunks =3D mtd->writesize / chip->ecc.steps; + if (nchunks < 4) + nchunks =3D 1; + else + nchunks /=3D 4; + + for (j =3D 0; j < nchunks; j++) { + status =3D readl(nand->regs + MA35_NFI_REG_NANDECCES0 + j*4); + if (!status) + continue; + + for (i =3D 0; i < 4; i++) { + if (!(status & ECC_STATUS_MASK)) { + /* No error */ + status >>=3D 8; + continue; + + } else if ((status & ECC_STATUS_MASK) =3D=3D 0x01) { + /* Correctable error */ + err_cnt =3D (status >> 2) & ECC_ERR_CNT_MASK; + dev_warn(nand->dev, "nchunks (%d, %d) have %d error!\n", + j, i, err_cnt); + ma35_nfi_correct(nand, j*4+i, err_cnt, (u8 *)addr); + report_err +=3D err_cnt; + + } else { + /* uncorrectable error */ + dev_warn(nand->dev, "uncorrectable error! 0x%4x\n", status); + return -1; + } + status >>=3D 8; + } + } + return report_err; +} + + +/* + * Initialize DMA + */ +static void ma35_nand_dmac_init(struct ma35_nand_info *nand) +{ + /* DMAC reset and enable */ + writel(DMA_RST | DMA_EN, nand->regs + MA35_NFI_REG_DMACTL); + writel(DMA_EN, nand->regs + MA35_NFI_REG_DMACTL); + + /* Clear DMA finished flag */ + writel(INT_DMA | INT_ECC, nand->regs + MA35_NFI_REG_NANDINTSTS); + + init_completion(&nand->complete); +} + + +/* + * configure and start dma transfer + */ +static int ma35_nand_do_write(struct nand_chip *chip, const u8 *addr, u32 = len) +{ + struct ma35_nand_info *nand =3D nand_get_controller_data(chip); + struct mtd_info *mtd =3D nand_to_mtd(chip); + dma_addr_t dma_addr; + int ret =3D 0, i; + u32 val, reg; + + ma35_nand_target_enable(nand); + + if (len !=3D mtd->writesize) { + for (i =3D 0; i < len; i++) + writel(addr[i], nand->regs + MA35_NFI_REG_NANDDATA); + ma35_nand_target_disable(nand); + return ret; + } + + /* Check the DMA status before enabling the DMA */ + ret =3D readl_poll_timeout(nand->regs + MA35_NFI_REG_DMACTL, val, + !(val & DMA_BUSY), 50, HZ/2); + if (ret) + dev_warn(nand->dev, "dma busy\n"); + + /* Reinitial dmac */ + ma35_nand_dmac_init(nand); + + writel(mtd->oobsize, nand->regs + MA35_NFI_REG_NANDRACTL); + + /* setup and start DMA using dma_addr */ + writel(INT_DMA, nand->regs + MA35_NFI_REG_NANDINTEN); + /* To mark this page as dirty. */ + reg =3D readl(nand->regs + MA35_NFI_REG_NANDRA0); + if (reg & 0xffff0000) + writel(reg & 0xffff, nand->regs + MA35_NFI_REG_NANDRA0); + + /* Fill dma_addr */ + dma_addr =3D dma_map_single(nand->dev, (void *)addr, len, DMA_TO_DEVICE); + dma_sync_single_for_device(nand->dev, dma_addr, len, DMA_TO_DEVICE); + ret =3D dma_mapping_error(nand->dev, dma_addr); + if (ret) { + dev_err(nand->dev, "dma mapping error\n"); + return -EINVAL; + } + + writel((unsigned long)dma_addr, nand->regs + MA35_NFI_REG_DMASA); + writel(readl(nand->regs + MA35_NFI_REG_NANDCTL) | DMA_W_EN, + nand->regs + MA35_NFI_REG_NANDCTL); + ret =3D wait_for_completion_timeout(&nand->complete, msecs_to_jiffies(100= 0)); + if (!ret) { + dev_err(nand->dev, "write timeout\n"); + ret =3D -ETIMEDOUT; + } + + dma_unmap_single(nand->dev, dma_addr, len, DMA_TO_DEVICE); + + ma35_nand_target_disable(nand); + + return ret; +} + +static int ma35_nand_do_read(struct nand_chip *chip, const u8 *addr, u32 l= en) +{ + struct ma35_nand_info *nand =3D nand_get_controller_data(chip); + struct mtd_info *mtd =3D nand_to_mtd(chip); + u8 *ptr =3D (u8 *)addr; + dma_addr_t dma_addr; + int ret =3D 0, i; + u32 val; + + ma35_nand_target_enable(nand); + + if (len !=3D mtd->writesize) { + for (i =3D 0; i < len; i++) + *(ptr+i) =3D (u8)readl(nand->regs + MA35_NFI_REG_NANDDATA); + ma35_nand_target_disable(nand); + return ret; + } + + /* Check the DMA status before enabling the DMA */ + ret =3D readl_poll_timeout(nand->regs + MA35_NFI_REG_DMACTL, val, + !(val & DMA_BUSY), 50, HZ/2); + if (ret) + dev_warn(nand->dev, "dma busy\n"); + + /* Reinitial dmac */ + ma35_nand_dmac_init(nand); + + writel(mtd->oobsize, nand->regs + MA35_NFI_REG_NANDRACTL); + + /* setup and start DMA using dma_addr */ + dma_addr =3D dma_map_single(nand->dev, (void *)addr, len, DMA_FROM_DEVICE= ); + ret =3D dma_mapping_error(nand->dev, dma_addr); + if (ret) { + dev_err(nand->dev, "dma mapping error\n"); + return -EINVAL; + } + nand->dma_buf =3D (u8 *)addr; + nand->dma_addr =3D dma_addr; + + writel((unsigned long)dma_addr, nand->regs + MA35_NFI_REG_DMASA); + writel(readl(nand->regs + MA35_NFI_REG_NANDCTL) | DMA_R_EN, + nand->regs + MA35_NFI_REG_NANDCTL); + ret =3D wait_for_completion_timeout(&nand->complete, msecs_to_jiffies(100= 0)); + if (!ret) { + dev_err(nand->dev, "read timeout\n"); + ret =3D -ETIMEDOUT; + } + + dma_sync_single_for_cpu(nand->dev, dma_addr, len, DMA_FROM_DEVICE); + dma_unmap_single(nand->dev, dma_addr, len, DMA_FROM_DEVICE); + + ma35_nand_target_disable(nand); + + return ret; +} + + +static int ma35_nand_write_page_hwecc(struct nand_chip *chip, const u8 *bu= f, + int oob_required, int page) +{ + struct mtd_info *mtd =3D nand_to_mtd(chip); + u8 *ecc_calc =3D chip->ecc.calc_buf; + + ma35_clear_spare(chip, mtd->oobsize); + ma35_write_spare(chip, mtd->oobsize - chip->ecc.total, (u32 *)chip->oob_p= oi); + + nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize); + nand_prog_page_end_op(chip); + + /* Copy parity code in NANDRA to calc */ + ma35_read_spare(chip, chip->ecc.total, (u32 *)ecc_calc, + mtd->oobsize - chip->ecc.total); + + /* Copy parity code in calc to oob_poi */ + memcpy((void *)(chip->oob_poi + (mtd->oobsize - chip->ecc.total)), + (void *)ecc_calc, chip->ecc.total); + + return 0; +} + +static int ma35_nand_read_page_hwecc(struct nand_chip *chip, u8 *buf, + int oob_required, int page) +{ + struct ma35_nand_info *nand =3D nand_get_controller_data(chip); + struct mtd_info *mtd =3D nand_to_mtd(chip); + u32 reg; + + /* read the OOB area */ + nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize); + + /* copy OOB data to NANDRA for page read */ + ma35_write_spare(chip, mtd->oobsize, (u32 *)chip->oob_poi); + + reg =3D readl(nand->regs + MA35_NFI_REG_NANDRA0); + if (reg & 0xffff0000) + memset((void *)buf, 0xff, mtd->writesize); + else { + /* read data from nand */ + nand_read_page_op(chip, page, 0, buf, mtd->writesize); + + /* restore OOB data from SMRA */ + ma35_read_spare(chip, mtd->oobsize, (u32 *)chip->oob_poi, 0); + } + + return 0; +} + + +static int ma35_nand_read_oob_hwecc(struct nand_chip *chip, int page) +{ + struct ma35_nand_info *nand =3D nand_get_controller_data(chip); + struct mtd_info *mtd =3D nand_to_mtd(chip); + u32 reg; + + nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize); + + /* copy OOB data to NANDRA for page read */ + ma35_write_spare(chip, mtd->oobsize, (u32 *)chip->oob_poi); + + reg =3D readl(nand->regs + MA35_NFI_REG_NANDRA0); + if (reg & 0xffff0000) + memset((void *)chip->oob_poi, 0xff, mtd->oobsize); + + return 0; +} + +static irqreturn_t ma35_nand_irq(int irq, void *id) +{ + struct ma35_nand_info *nand =3D (struct ma35_nand_info *)id; + struct mtd_info *mtd =3D nand_to_mtd(&nand->chip); + int stat =3D 0; + u32 isr; + + spin_lock(&nand->dma_lock); + + isr =3D readl(nand->regs + MA35_NFI_REG_NANDINTSTS); + if (isr & INT_ECC) { + dma_sync_single_for_cpu(nand->dev, nand->dma_addr, mtd->writesize, + DMA_FROM_DEVICE); + stat =3D ma35_nfi_ecc_check(&nand->chip, (unsigned long)nand->dma_buf); + if (stat < 0) { + mtd->ecc_stats.failed++; + writel(DMA_RST | DMA_EN, nand->regs + MA35_NFI_REG_DMACTL); + writel(readl(nand->regs + MA35_NFI_REG_NANDCTL) | SWRST, + nand->regs + MA35_NFI_REG_NANDCTL); + } else if (stat > 0) { + mtd->ecc_stats.corrected +=3D stat; /* Add corrected bit count */ + } + writel(INT_ECC, nand->regs + MA35_NFI_REG_NANDINTSTS); + } + if (isr & INT_DMA) { + writel(INT_DMA, nand->regs + MA35_NFI_REG_NANDINTSTS); + complete(&nand->complete); + } + spin_unlock(&nand->dma_lock); + + return IRQ_HANDLED; +} + +static int ma35_nand_attach_chip(struct nand_chip *chip) +{ + struct ma35_nand_info *nand =3D nand_get_controller_data(chip); + struct mtd_info *mtd =3D nand_to_mtd(chip); + unsigned int reg; + + if (chip->options & NAND_BUSWIDTH_16) { + dev_err(nand->dev, "16 bits bus width not supported"); + return -EINVAL; + } + + /* support only ecc hw mode */ + if (chip->ecc.engine_type !=3D NAND_ECC_ENGINE_TYPE_ON_HOST) { + dev_err(nand->dev, "ecc.engine_type not supported\n"); + return -EINVAL; + } + + nand->ecc_buf =3D devm_kzalloc(nand->dev, mtd->writesize + mtd->oobsize, = GFP_KERNEL); + if (!nand->ecc_buf) + return -ENOMEM; + chip->ecc.calc_buf =3D nand->ecc_buf; + + /* Set PSize */ + reg =3D readl(nand->regs + MA35_NFI_REG_NANDCTL) & (~PSIZE_MASK); + if (mtd->writesize =3D=3D 2048) + writel(reg | PSIZE_2K, nand->regs + MA35_NFI_REG_NANDCTL); + else if (mtd->writesize =3D=3D 4096) + writel(reg | PSIZE_4K, nand->regs + MA35_NFI_REG_NANDCTL); + else if (mtd->writesize =3D=3D 8192) + writel(reg | PSIZE_8K, nand->regs + MA35_NFI_REG_NANDCTL); + + chip->ecc.steps =3D mtd->writesize / chip->ecc.size; + if (chip->ecc.strength =3D=3D 0) { + nand->bch =3D BCH_NONE; /* No ECC */ + chip->ecc.total =3D 0; + + } else if (chip->ecc.strength <=3D 8) { + nand->bch =3D BCH_T8; /* T8 */ + chip->ecc.total =3D chip->ecc.steps * MA35_PARITY_BCH8; + + } else if (chip->ecc.strength <=3D 12) { + nand->bch =3D BCH_T12; /* T12 */ + chip->ecc.total =3D chip->ecc.steps * MA35_PARITY_BCH12; + + } else if (chip->ecc.strength <=3D 24) { + nand->bch =3D BCH_T24; /* T24 */ + chip->ecc.total =3D chip->ecc.steps * MA35_PARITY_BCH24; + + } else { + dev_warn(nand->dev, "NAND Controller is not support this flash. (%d, %d)= \n", + mtd->writesize, mtd->oobsize); + } + + chip->ecc.bytes =3D chip->ecc.total / chip->ecc.steps; + mtd_set_ooblayout(mtd, &ma35_ooblayout_ops); + + /* add mtd-id. The string should same as uboot definition */ + mtd->name =3D "nand0"; + + ma35_nand_hwecc_init(nand); + + writel(DISABLE_WP, nand->regs + MA35_NFI_REG_NANDECTL); + + return 0; +} + + + +static int ma35_nfc_exec_instr(struct nand_chip *chip, + const struct nand_op_instr *instr) +{ + struct ma35_nand_info *nand =3D nand_get_controller_data(chip); + unsigned int i; + u32 status; + + switch (instr->type) { + case NAND_OP_CMD_INSTR: + writel(instr->ctx.cmd.opcode, nand->regs + MA35_NFI_REG_NANDCMD); + return 0; + + case NAND_OP_ADDR_INSTR: + for (i =3D 0; i < instr->ctx.addr.naddrs; i++) { + if (i =3D=3D (instr->ctx.addr.naddrs - 1)) + writel(instr->ctx.addr.addrs[i] | ENDADDR, + nand->regs + MA35_NFI_REG_NANDADDR); + else + writel(instr->ctx.addr.addrs[i], + nand->regs + MA35_NFI_REG_NANDADDR); + } + return 0; + + case NAND_OP_DATA_IN_INSTR: + ma35_nand_do_read(chip, instr->ctx.data.buf.in, instr->ctx.data.len); + return 0; + + case NAND_OP_DATA_OUT_INSTR: + ma35_nand_do_write(chip, instr->ctx.data.buf.out, instr->ctx.data.len); + return 0; + + case NAND_OP_WAITRDY_INSTR: + return readl_poll_timeout(nand->regs + MA35_NFI_REG_NANDINTSTS, status, + status & INT_RB0, 20, + instr->ctx.waitrdy.timeout_ms * 1000); + default: + break; + } + + return -EINVAL; +} + + +static int ma35_nfc_exec_op(struct nand_chip *chip, + const struct nand_operation *op, + bool check_only) +{ + struct ma35_nand_info *nand =3D nand_get_controller_data(chip); + u32 i, reg; + int ret =3D 0; + + if (check_only) + return 0; + + ma35_nand_target_enable(nand); + reg =3D readl(nand->regs + MA35_NFI_REG_NANDINTSTS); + reg |=3D INT_RB0; + writel(reg, nand->regs + MA35_NFI_REG_NANDINTSTS); + + for (i =3D 0; i < op->ninstrs; i++) { + ret =3D ma35_nfc_exec_instr(chip, &op->instrs[i]); + if (ret) + break; + } + + return ret; +} + + +static const struct nand_controller_ops ma35_nfc_ops =3D { + .attach_chip =3D ma35_nand_attach_chip, + .exec_op =3D ma35_nfc_exec_op, +}; + +static int ma35_nand_probe(struct platform_device *pdev) +{ + struct ma35_nand_info *nand; + struct nand_chip *chip; + struct mtd_info *mtd; + int ret =3D 0; + + nand =3D devm_kzalloc(&pdev->dev, sizeof(*nand), GFP_KERNEL); + if (!nand) + return -ENOMEM; + + nand_controller_init(&nand->controller); + nand->controller.ops =3D &ma35_nfc_ops; + + nand->regs =3D devm_platform_ioremap_resource(pdev, 0); + if (IS_ERR(nand->regs)) + return PTR_ERR(nand->regs); + + nand->dev =3D &pdev->dev; + chip =3D &nand->chip; + nand_set_controller_data(chip, nand); + nand_set_flash_node(chip, pdev->dev.of_node); + + nand->clk =3D devm_clk_get_enabled(&pdev->dev, "nand_gate"); + if (IS_ERR(nand->clk)) + return dev_err_probe(&pdev->dev, PTR_ERR(nand->clk), + "failed to find nand clock\n"); + + nand->irq =3D platform_get_irq(pdev, 0); + if (nand->irq < 0) + return dev_err_probe(&pdev->dev, nand->irq, + "failed to get platform irq\n"); + + ret =3D devm_request_irq(&pdev->dev, nand->irq, ma35_nand_irq, + IRQF_TRIGGER_HIGH, "ma35d1-nand", nand); + if (ret) { + dev_err(&pdev->dev, "failed to request NAND irq\n"); + clk_disable_unprepare(nand->clk); + return -ENXIO; + } + + nand->chip.controller =3D &nand->controller; + platform_set_drvdata(pdev, nand); + + chip->options |=3D NAND_NO_SUBPAGE_WRITE | NAND_USES_DMA; + + /* set default mode in case dt entry is missing */ + chip->ecc.engine_type =3D NAND_ECC_ENGINE_TYPE_ON_HOST; + + chip->ecc.write_page =3D ma35_nand_write_page_hwecc; + chip->ecc.read_page =3D ma35_nand_read_page_hwecc; + chip->ecc.read_oob =3D ma35_nand_read_oob_hwecc; + + mtd =3D nand_to_mtd(chip); + mtd->priv =3D chip; + mtd->owner =3D THIS_MODULE; + mtd->dev.parent =3D &pdev->dev; + + writel(NAND_EN, nand->regs + MA35_NFI_REG_GCTL); + + ret =3D nand_scan(chip, 1); + if (ret) + return ret; + + ret =3D mtd_device_register(mtd, NULL, 0); + if (ret) { + nand_cleanup(chip); + devm_kfree(&pdev->dev, nand); + return ret; + } + + return ret; +} + +static void ma35_nand_remove(struct platform_device *pdev) +{ + struct ma35_nand_info *nand =3D platform_get_drvdata(pdev); + int ret; + + devm_free_irq(&pdev->dev, nand->irq, nand); + ret =3D mtd_device_unregister(nand_to_mtd(&nand->chip)); + WARN_ON(ret); + nand_cleanup(&nand->chip); + clk_disable_unprepare(nand->clk); +} + +/* PM Support */ +#ifdef CONFIG_PM +static int ma35_nand_suspend(struct platform_device *pdev, pm_message_t pm) +{ + struct ma35_nand_info *nand =3D platform_get_drvdata(pdev); + int ret =3D 0; + u32 val; + + /* wait DMAC to ready */ + ret =3D readl_poll_timeout(nand->regs + MA35_NFI_REG_DMACTL, val, + !(val & DMA_BUSY), 50, HZ/2); + if (ret) + dev_warn(&pdev->dev, "dma busy\n"); + + clk_disable(nand->clk); + + return ret; +} + +static int ma35_nand_resume(struct platform_device *pdev) +{ + struct ma35_nand_info *nand =3D platform_get_drvdata(pdev); + + clk_enable(nand->clk); + ma35_nand_hwecc_init(nand); + ma35_nand_dmac_init(nand); + + return 0; +} + +#else +#define ma35_nand_suspend NULL +#define ma35_nand_resume NULL +#endif + +static const struct of_device_id ma35_nfi_of_match[] =3D { + { .compatible =3D "nuvoton,ma35d1-nand" }, + {}, +}; +MODULE_DEVICE_TABLE(of, ma35_nfi_of_match); + +static struct platform_driver ma35_nand_driver =3D { + .driver =3D { + .name =3D "ma35d1-nand", + .of_match_table =3D ma35_nfi_of_match, + }, + .probe =3D ma35_nand_probe, + .remove =3D ma35_nand_remove, + .suspend =3D ma35_nand_suspend, + .resume =3D ma35_nand_resume, +}; + +module_platform_driver(ma35_nand_driver); + +MODULE_DESCRIPTION("Nuvoton ma35 NAND driver"); +MODULE_AUTHOR("Hui-Ping Chen "); +MODULE_LICENSE("GPL"); --=20 2.25.1