From mboxrd@z Thu Jan 1 00:00:00 1970 From: Zhou Wang Subject: Re: [PATCH v6 1/2] mtd: hisilicon: add a new NAND controller driver for hisilicon hip04 Soc Date: Wed, 14 Jan 2015 20:34:39 +0800 Message-ID: <54B6625F.8060607@hisilicon.com> References: <1421047734-30818-1-git-send-email-wangzhou1@hisilicon.com> <1421047734-30818-2-git-send-email-wangzhou1@hisilicon.com> <20150113035844.GJ9759@ld-irv-0074> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: QUOTED-PRINTABLE Return-path: In-Reply-To: <20150113035844.GJ9759@ld-irv-0074> Sender: devicetree-owner-u79uwXL29TY76Z2rM5mHXA@public.gmane.org To: Brian Norris Cc: David Woodhouse , haojian.zhuang-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org, xuwei5-C8/M+/jPZTeaMJb+Lgu22Q@public.gmane.org, devicetree-u79uwXL29TY76Z2rM5mHXA@public.gmane.org, linux-mtd-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r@public.gmane.org, linux-arm-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r@public.gmane.org, caizhiyong-hv44wF8Li93QT0dZR+AlfA@public.gmane.org, yubingxu-C8/M+/jPZTeaMJb+Lgu22Q@public.gmane.org, liguozhu-C8/M+/jPZTeaMJb+Lgu22Q@public.gmane.org List-Id: devicetree@vger.kernel.org On 2015/1/13 11:58, Brian Norris wrote: > On Mon, Jan 12, 2015 at 03:28:53PM +0800, Zhou Wang wrote: >> Signed-off-by: Zhou Wang >=20 > Mostly good. A few small comments. >=20 >> --- >> drivers/mtd/nand/Kconfig | 5 + >> drivers/mtd/nand/Makefile | 1 + >> drivers/mtd/nand/hisi504_nand.c | 907 +++++++++++++++++++++++++++++= +++++++++++ >> 3 files changed, 913 insertions(+) >> create mode 100644 drivers/mtd/nand/hisi504_nand.c >> >> diff --git a/drivers/mtd/nand/Kconfig b/drivers/mtd/nand/Kconfig >> index 7d0150d..e1220fc 100644 >> --- a/drivers/mtd/nand/Kconfig >> +++ b/drivers/mtd/nand/Kconfig >> @@ -524,4 +524,9 @@ config MTD_NAND_SUNXI >> help >> Enables support for NAND Flash chips on Allwinner SoCs. >> =20 >> +config MTD_NAND_HISI504 >> + tristate "Support for NAND controller on Hisilicon SoC Hip04" >> + help >> + Enables support for NAND controller on Hisilicon SoC Hip04. >> + >> endif # MTD_NAND >> diff --git a/drivers/mtd/nand/Makefile b/drivers/mtd/nand/Makefile >> index bd38f21..582bbd05 100644 >> --- a/drivers/mtd/nand/Makefile >> +++ b/drivers/mtd/nand/Makefile >> @@ -51,5 +51,6 @@ obj-$(CONFIG_MTD_NAND_GPMI_NAND) +=3D gpmi-nand/ >> obj-$(CONFIG_MTD_NAND_XWAY) +=3D xway_nand.o >> obj-$(CONFIG_MTD_NAND_BCM47XXNFLASH) +=3D bcm47xxnflash/ >> obj-$(CONFIG_MTD_NAND_SUNXI) +=3D sunxi_nand.o >> +obj-$(CONFIG_MTD_NAND_HISI504) +=3D hisi504_nand.o >> =20 >> nand-objs :=3D nand_base.o nand_bbt.o nand_timings.o >> diff --git a/drivers/mtd/nand/hisi504_nand.c b/drivers/mtd/nand/hisi= 504_nand.c >> new file mode 100644 >> index 0000000..2000f21 >> --- /dev/null >> +++ b/drivers/mtd/nand/hisi504_nand.c >> @@ -0,0 +1,907 @@ >> +/* >> + * Hisilicon NAND Flash controller driver >> + * >> + * Copyright =A9 2012-2014 HiSilicon Technologies Co., Ltd. >> + * http://www.hisilicon.com >> + * >> + * Author: Zhou Wang >> + * The initial developer of the original code is Zhiyong Cai >> + * >> + * >> + * This program is free software; you can redistribute it and/or mo= dify >> + * it under the terms of the GNU General Public License as publishe= d by >> + * the Free Software Foundation; either version 2 of the License, o= r >> + * (at your option) any later version. >> + * >> + * This program is distributed in the hope that it will be useful, >> + * but WITHOUT ANY WARRANTY; without even the implied warranty of >> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the >> + * GNU General Public License for more details. >> + */ >> +#include >> +#include >> +#include >> +#include >> +#include >> +#include >> +#include >> +#include >> +#include >> +#include >> +#include >> +#include >> +#include >> + >> +#define HINFC504_MAX_CHIP (4) >> +#define HINFC504_W_LATCH (5) >> +#define HINFC504_R_LATCH (7) >> +#define HINFC504_RW_LATCH (3) >> + >> +#define HINFC504_NFC_TIMEOUT (2 * HZ) >> +#define HINFC504_NFC_PM_TIMEOUT (1 * HZ) >> +#define HINFC504_NFC_DMA_TIMEOUT (5 * HZ) >> +#define HINFC504_CHIP_DELAY (25) >> + >> +#define HINFC504_REG_BASE_ADDRESS_LEN (0x100) >> +#define HINFC504_BUFFER_BASE_ADDRESS_LEN (2048 + 128) >> + >> +#define HINFC504_ADDR_CYCLE_MASK 0x4 >> + >> +#define HINFC504_CON 0x00 >> +#define HINFC504_CON_OP_MODE_NORMAL BIT(0) >> +#define HINFC504_CON_PAGEISZE_SHIFT (1) >> +#define HINFC504_CON_PAGESIZE_MASK (0x07) >> +#define HINFC504_CON_BUS_WIDTH BIT(4) >> +#define HINFC504_CON_READY_BUSY_SEL BIT(8) >> +#define HINFC504_CON_ECCTYPE_SHIFT (9) >> +#define HINFC504_CON_ECCTYPE_MASK (0x07) >> + >> +#define HINFC504_PWIDTH 0x04 >> +#define SET_HINFC504_PWIDTH(_w_lcnt, _r_lcnt, _rw_hcnt) \ >> + ((_w_lcnt) | (((_r_lcnt) & 0x0F) << 4) | (((_rw_hcnt) & 0x0F) << 8= )) >> + >> +#define HINFC504_CMD 0x0C >> +#define HINFC504_ADDRL 0x10 >> +#define HINFC504_ADDRH 0x14 >> +#define HINFC504_DATA_NUM 0x18 >> + >> +#define HINFC504_OP 0x1C >> +#define HINFC504_OP_READ_DATA_EN BIT(1) >> +#define HINFC504_OP_WAIT_READY_EN BIT(2) >> +#define HINFC504_OP_CMD2_EN BIT(3) >> +#define HINFC504_OP_WRITE_DATA_EN BIT(4) >> +#define HINFC504_OP_ADDR_EN BIT(5) >> +#define HINFC504_OP_CMD1_EN BIT(6) >> +#define HINFC504_OP_NF_CS_SHIFT (7) >> +#define HINFC504_OP_NF_CS_MASK (3) >> +#define HINFC504_OP_ADDR_CYCLE_SHIFT (9) >> +#define HINFC504_OP_ADDR_CYCLE_MASK (7) >> + >> +#define HINFC504_STATUS 0x20 >> +#define HINFC504_READY BIT(0) >> + >> +#define HINFC504_INTEN 0x24 >> +#define HINFC504_INTEN_DMA BIT(9) >> +#define HINFC504_INTEN_UE BIT(6) >> +#define HINFC504_INTEN_CE BIT(5) >> + >> +#define HINFC504_INTS 0x28 >> +#define HINFC504_INTS_DMA BIT(9) >> +#define HINFC504_INTS_UE BIT(6) >> +#define HINFC504_INTS_CE BIT(5) >> + >> +#define HINFC504_INTCLR 0x2C >> +#define HINFC504_INTCLR_DMA BIT(9) >> +#define HINFC504_INTCLR_UE BIT(6) >> +#define HINFC504_INTCLR_CE BIT(5) >> + >> +#define HINFC504_ECC_STATUS 0x5C >> +#define HINFC504_ECC_1_BIT_SHIFT 16 >> +#define HINFC504_ECC_16_BIT_SHIFT 12 >> + >> +#define HINFC504_DMA_CTRL 0x60 >> +#define HINFC504_DMA_CTRL_DMA_START BIT(0) >> +#define HINFC504_DMA_CTRL_WE BIT(1) >> +#define HINFC504_DMA_CTRL_DATA_AREA_EN BIT(2) >> +#define HINFC504_DMA_CTRL_OOB_AREA_EN BIT(3) >> +#define HINFC504_DMA_CTRL_BURST4_EN BIT(4) >> +#define HINFC504_DMA_CTRL_BURST8_EN BIT(5) >> +#define HINFC504_DMA_CTRL_BURST16_EN BIT(6) >> +#define HINFC504_DMA_CTRL_ADDR_NUM_SHIFT (7) >> +#define HINFC504_DMA_CTRL_ADDR_NUM_MASK (1) >> +#define HINFC504_DMA_CTRL_CS_SHIFT (8) >> +#define HINFC504_DMA_CTRL_CS_MASK (0x03) >> + >> +#define HINFC504_DMA_ADDR_DATA 0x64 >> +#define HINFC504_DMA_ADDR_OOB 0x68 >> + >> +#define HINFC504_DMA_LEN 0x6C >> +#define HINFC504_DMA_LEN_OOB_SHIFT (16) >> +#define HINFC504_DMA_LEN_OOB_MASK (0xFFF) >> + >> +#define HINFC504_DMA_PARA 0x70 >> +#define HINFC504_DMA_PARA_DATA_RW_EN BIT(0) >> +#define HINFC504_DMA_PARA_OOB_RW_EN BIT(1) >> +#define HINFC504_DMA_PARA_DATA_EDC_EN BIT(2) >> +#define HINFC504_DMA_PARA_OOB_EDC_EN BIT(3) >> +#define HINFC504_DMA_PARA_DATA_ECC_EN BIT(4) >> +#define HINFC504_DMA_PARA_OOB_ECC_EN BIT(5) >> + >> +#define HINFC_VERSION 0x74 >> +#define HINFC504_LOG_READ_ADDR 0x7C >> +#define HINFC504_LOG_READ_LEN 0x80 >> + >> +#define HINFC504_NANDINFO_LEN 0x10 >> + >> +struct hinfc_host { >> + struct nand_chip chip; >> + struct mtd_info mtd; >> + struct device *dev; >> + void __iomem *iobase; >> + struct completion cmd_complete; >> + unsigned int offset; >> + unsigned int command; >> + int chipselect; >> + unsigned int addr_cycle; >> + u32 addr_value[2]; >> + u32 cache_addr_value[2]; >> + char *buffer; >> + dma_addr_t dma_buffer; >> + dma_addr_t dma_oob; >> + int version; >> + unsigned int irq_status; /* interrupt status */ >> +}; >> + >> +static inline unsigned int hinfc_read(struct hinfc_host *host, unsi= gned int reg) >> +{ >> + return readl(host->iobase + reg); >> +} >> + >> +static inline void hinfc_write(struct hinfc_host *host, unsigned in= t value, >> + unsigned int reg) >> +{ >> + writel(value, host->iobase + reg); >> +} >> + >> +static void wait_controller_finished(struct hinfc_host *host) >> +{ >> + unsigned long timeout =3D jiffies + HINFC504_NFC_TIMEOUT; >> + int val; >> + >> + while (time_before(jiffies, timeout)) { >> + val =3D hinfc_read(host, HINFC504_STATUS); >> + if (host->command =3D=3D NAND_CMD_ERASE2) { >> + /* nfc is ready */ >> + while (!(val & HINFC504_READY)) { >> + usleep_range(500, 1000); >> + val =3D hinfc_read(host, HINFC504_STATUS); >> + } >> + return; >> + } >> + >> + if (val & HINFC504_READY) >> + return; >> + } >> + >> + /* wait cmd timeout */ >> + dev_err(host->dev, "Wait NAND controller exec cmd timeout.\n"); >> +} >> + >> +static void hisi_nfc_dma_transfer(struct hinfc_host *host, int tode= v) >> +{ >> + struct mtd_info *mtd =3D &host->mtd; >> + struct nand_chip *chip =3D mtd->priv; >> + unsigned long val; >> + int ret; >> + >> + hinfc_write(host, host->dma_buffer, HINFC504_DMA_ADDR_DATA); >> + hinfc_write(host, host->dma_oob, HINFC504_DMA_ADDR_OOB); >> + >> + if (chip->ecc.mode =3D=3D NAND_ECC_NONE) { >> + hinfc_write(host, ((mtd->oobsize & HINFC504_DMA_LEN_OOB_MASK) >> + << HINFC504_DMA_LEN_OOB_SHIFT), HINFC504_DMA_LEN); >> + >> + hinfc_write(host, HINFC504_DMA_PARA_DATA_RW_EN >> + | HINFC504_DMA_PARA_OOB_RW_EN, HINFC504_DMA_PARA); >> + } else { >> + if (host->command =3D=3D NAND_CMD_READOOB) >> + hinfc_write(host, HINFC504_DMA_PARA_OOB_RW_EN >> + | HINFC504_DMA_PARA_OOB_EDC_EN >> + | HINFC504_DMA_PARA_OOB_ECC_EN, HINFC504_DMA_PARA); >> + else >> + hinfc_write(host, HINFC504_DMA_PARA_DATA_RW_EN >> + | HINFC504_DMA_PARA_OOB_RW_EN >> + | HINFC504_DMA_PARA_DATA_EDC_EN >> + | HINFC504_DMA_PARA_OOB_EDC_EN >> + | HINFC504_DMA_PARA_DATA_ECC_EN >> + | HINFC504_DMA_PARA_OOB_ECC_EN, HINFC504_DMA_PARA); >> + >> + } >> + >> + val =3D (HINFC504_DMA_CTRL_DMA_START | HINFC504_DMA_CTRL_BURST4_EN >> + | HINFC504_DMA_CTRL_BURST8_EN | HINFC504_DMA_CTRL_BURST16_EN >> + | HINFC504_DMA_CTRL_DATA_AREA_EN | HINFC504_DMA_CTRL_OOB_AREA_EN >> + | ((host->addr_cycle =3D=3D 4 ? 1 : 0) >> + << HINFC504_DMA_CTRL_ADDR_NUM_SHIFT) >> + | ((host->chipselect & HINFC504_DMA_CTRL_CS_MASK) >> + << HINFC504_DMA_CTRL_CS_SHIFT)); >> + >> + if (todev) >> + val |=3D HINFC504_DMA_CTRL_WE; >> + >> + init_completion(&host->cmd_complete); >> + >> + hinfc_write(host, val, HINFC504_DMA_CTRL); >> + ret =3D wait_for_completion_timeout(&host->cmd_complete, >> + HINFC504_NFC_DMA_TIMEOUT); >> + >> + if (!ret) { >> + dev_err(host->dev, "DMA operation(irq) timeout!\n"); >> + /* sanity check */ >> + val =3D hinfc_read(host, HINFC504_DMA_CTRL); >> + if (!(val & HINFC504_DMA_CTRL_DMA_START)) >> + dev_err(host->dev, "DMA is already done but without irq ACK!\n")= ; >> + else >> + dev_err(host->dev, "DMA is really timeout!\n"); >> + } >> +} >> + >> +static int hisi_nfc_send_cmd_pageprog(struct hinfc_host *host) >> +{ >> + host->addr_value[0] &=3D 0xffff0000; >> + >> + hinfc_write(host, host->addr_value[0], HINFC504_ADDRL); >> + hinfc_write(host, host->addr_value[1], HINFC504_ADDRH); >> + hinfc_write(host, NAND_CMD_PAGEPROG << 8 | NAND_CMD_SEQIN, >> + HINFC504_CMD); >> + >> + hisi_nfc_dma_transfer(host, 1); >> + >> + return 0; >> +} >> + >> +static int hisi_nfc_send_cmd_readstart(struct hinfc_host *host) >> +{ >> + struct mtd_info *mtd =3D &host->mtd; >> + >> + if ((host->addr_value[0] =3D=3D host->cache_addr_value[0]) && >> + (host->addr_value[1] =3D=3D host->cache_addr_value[1])) >> + return 0; >> + >> + host->addr_value[0] &=3D 0xffff0000; >> + >> + hinfc_write(host, host->addr_value[0], HINFC504_ADDRL); >> + hinfc_write(host, host->addr_value[1], HINFC504_ADDRH); >> + hinfc_write(host, NAND_CMD_READSTART << 8 | NAND_CMD_READ0, >> + HINFC504_CMD); >> + >> + hinfc_write(host, 0, HINFC504_LOG_READ_ADDR); >> + hinfc_write(host, mtd->writesize + mtd->oobsize, >> + HINFC504_LOG_READ_LEN); >> + >> + hisi_nfc_dma_transfer(host, 0); >> + >> + host->cache_addr_value[0] =3D host->addr_value[0]; >> + host->cache_addr_value[1] =3D host->addr_value[1]; >> + >> + return 0; >> +} >> + >> +static int hisi_nfc_send_cmd_erase(struct hinfc_host *host) >> +{ >> + hinfc_write(host, host->addr_value[0], HINFC504_ADDRL); >> + hinfc_write(host, (NAND_CMD_ERASE2 << 8) | NAND_CMD_ERASE1, >> + HINFC504_CMD); >> + >> + hinfc_write(host, HINFC504_OP_WAIT_READY_EN >> + | HINFC504_OP_CMD2_EN >> + | HINFC504_OP_CMD1_EN >> + | HINFC504_OP_ADDR_EN >> + | ((host->chipselect & HINFC504_OP_NF_CS_MASK) >> + << HINFC504_OP_NF_CS_SHIFT) >> + | ((host->addr_cycle & HINFC504_OP_ADDR_CYCLE_MASK) >> + << HINFC504_OP_ADDR_CYCLE_SHIFT), >> + HINFC504_OP); >> + >> + wait_controller_finished(host); >> + >> + return 0; >> +} >> + >> +static int hisi_nfc_send_cmd_readid(struct hinfc_host *host) >> +{ >> + hinfc_write(host, HINFC504_NANDINFO_LEN, HINFC504_DATA_NUM); >> + hinfc_write(host, NAND_CMD_READID, HINFC504_CMD); >> + hinfc_write(host, 0, HINFC504_ADDRL); >> + >> + hinfc_write(host, HINFC504_OP_CMD1_EN | HINFC504_OP_ADDR_EN >> + | HINFC504_OP_READ_DATA_EN >> + | ((host->chipselect & HINFC504_OP_NF_CS_MASK) >> + << HINFC504_OP_NF_CS_SHIFT) >> + | 1 << HINFC504_OP_ADDR_CYCLE_SHIFT, HINFC504_OP); >> + >> + wait_controller_finished(host); >> + >> + return 0; >> +} >> + >> +static int hisi_nfc_send_cmd_status(struct hinfc_host *host) >> +{ >> + hinfc_write(host, HINFC504_NANDINFO_LEN, HINFC504_DATA_NUM); >> + hinfc_write(host, NAND_CMD_STATUS, HINFC504_CMD); >> + hinfc_write(host, HINFC504_OP_CMD1_EN >> + | HINFC504_OP_READ_DATA_EN >> + | ((host->chipselect & HINFC504_OP_NF_CS_MASK) >> + << HINFC504_OP_NF_CS_SHIFT), >> + HINFC504_OP); >> + >> + wait_controller_finished(host); >> + >> + return 0; >> +} >> + >> +static int hisi_nfc_send_cmd_reset(struct hinfc_host *host, int chi= pselect) >> +{ >> + hinfc_write(host, NAND_CMD_RESET, HINFC504_CMD); >> + >> + hinfc_write(host, HINFC504_OP_CMD1_EN >> + | ((chipselect & HINFC504_OP_NF_CS_MASK) >> + << HINFC504_OP_NF_CS_SHIFT) >> + | HINFC504_OP_WAIT_READY_EN, >> + HINFC504_OP); >> + >> + wait_controller_finished(host); >> + >> + return 0; >> +} >> + >> +static void hisi_nfc_select_chip(struct mtd_info *mtd, int chipsele= ct) >> +{ >> + struct nand_chip *chip =3D mtd->priv; >> + struct hinfc_host *host =3D chip->priv; >> + >> + if (chipselect < 0) >> + return; >> + >> + host->chipselect =3D chipselect; >> +} >> + >> +static uint8_t hisi_nfc_read_byte(struct mtd_info *mtd) >> +{ >> + struct nand_chip *chip =3D mtd->priv; >> + struct hinfc_host *host =3D chip->priv; >> + >> + if (host->command =3D=3D NAND_CMD_STATUS) >> + return readb(chip->IO_ADDR_R); >> + >> + host->offset++; >> + >> + if (host->command =3D=3D NAND_CMD_READID) >> + return readb(chip->IO_ADDR_R + host->offset - 1); >> + >> + return readb(host->buffer + host->offset - 1); >=20 > You're reading from memory, not IO. You don't need readb(). Yes, I will get the value directly. Thanks! >=20 >> +} >> + >> +static u16 hisi_nfc_read_word(struct mtd_info *mtd) >> +{ >> + struct nand_chip *chip =3D mtd->priv; >> + struct hinfc_host *host =3D chip->priv; >> + >> + host->offset +=3D 2; >> + return readw(host->buffer + host->offset - 2); >=20 > Same here, for readw(). Thanks! >=20 >> +} >> + >> +static void >> +hisi_nfc_write_buf(struct mtd_info *mtd, const uint8_t *buf, int le= n) >> +{ >> + struct nand_chip *chip =3D mtd->priv; >> + struct hinfc_host *host =3D chip->priv; >> + >> + memcpy(host->buffer + host->offset, buf, len); >> + host->offset +=3D len; >> +} >> + >> +static void hisi_nfc_read_buf(struct mtd_info *mtd, uint8_t *buf, i= nt len) >> +{ >> + struct nand_chip *chip =3D mtd->priv; >> + struct hinfc_host *host =3D chip->priv; >> + >> + memcpy(buf, host->buffer + host->offset, len); >> + host->offset +=3D len; >> +} >> + >> +static void set_addr(struct mtd_info *mtd, int column, int page_add= r) >> +{ >> + struct nand_chip *chip =3D mtd->priv; >> + struct hinfc_host *host =3D chip->priv; >> + unsigned int command =3D host->command; >> + >> + host->addr_cycle =3D 0; >> + host->addr_value[0] =3D 0; >> + host->addr_value[1] =3D 0; >> + >> + /* Serially input address */ >> + if (column !=3D -1) { >> + /* Adjust columns for 16 bit buswidth */ >> + if (chip->options & NAND_BUSWIDTH_16 && >> + !nand_opcode_8bits(command)) >> + column >>=3D 1; >> + >> + host->addr_value[0] =3D column & 0xffff; >> + host->addr_cycle =3D 2; >> + } >> + if (page_addr !=3D -1) { >> + host->addr_value[0] |=3D (page_addr & 0xffff) >> + << (host->addr_cycle * 8); >> + host->addr_cycle +=3D 2; >> + /* One more address cycle for devices > 128MiB */ >> + if (chip->chipsize > (128 << 20)) { >> + host->addr_cycle +=3D 1; >> + if (host->command =3D=3D NAND_CMD_ERASE1) >> + host->addr_value[0] |=3D ((page_addr >> 16) & 0xff) << 16; >> + else >> + host->addr_value[1] |=3D ((page_addr >> 16) & 0xff); >> + } >> + } >> +} >> + >> +static void hisi_nfc_cmdfunc(struct mtd_info *mtd, unsigned command= , int column, >> + int page_addr) >> +{ >> + struct nand_chip *chip =3D mtd->priv; >> + struct hinfc_host *host =3D chip->priv; >> + int is_cache_invalid =3D 1; >> + unsigned int flag =3D 0; >> + >> + host->command =3D command; >> + >> + switch (command) { >> + case NAND_CMD_READ0: >> + case NAND_CMD_READOOB: >> + if (command =3D=3D NAND_CMD_READ0) >> + host->offset =3D column; >> + else >> + host->offset =3D column + mtd->writesize; >> + >> + is_cache_invalid =3D 0; >> + set_addr(mtd, column, page_addr); >> + hisi_nfc_send_cmd_readstart(host); >> + break; >> + >> + case NAND_CMD_SEQIN: >> + host->offset =3D column; >> + set_addr(mtd, column, page_addr); >> + break; >> + >> + case NAND_CMD_ERASE1: >> + set_addr(mtd, column, page_addr); >> + break; >> + >> + case NAND_CMD_PAGEPROG: >> + hisi_nfc_send_cmd_pageprog(host); >> + break; >> + >> + case NAND_CMD_ERASE2: >> + hisi_nfc_send_cmd_erase(host); >> + break; >> + >> + case NAND_CMD_READID: >> + host->offset =3D column; >> + memset(chip->IO_ADDR_R, 0, 0x10); >> + hisi_nfc_send_cmd_readid(host); >> + break; >> + >> + case NAND_CMD_STATUS: >> + flag =3D hinfc_read(host, HINFC504_CON); >> + if (chip->ecc.mode =3D=3D NAND_ECC_HW) >> + hinfc_write(host, >> + flag && ~(HINFC504_CON_ECCTYPE_MASK << >> + HINFC504_CON_ECCTYPE_SHIFT), HINFC504_CON); >> + >> + host->offset =3D 0; >> + memset(chip->IO_ADDR_R, 0, 0x10); >> + hisi_nfc_send_cmd_status(host); >> + hinfc_write(host, flag, HINFC504_CON); >> + break; >> + >> + case NAND_CMD_RESET: >> + hisi_nfc_send_cmd_reset(host, host->chipselect); >> + break; >> + >> + default: >> + dev_err(host->dev, "Error: unsupported cmd(cmd=3D%x, col=3D%x, pa= ge=3D%x)\n", >> + command, column, page_addr); >> + } >> + >> + if (is_cache_invalid) { >> + host->cache_addr_value[0] =3D ~0; >> + host->cache_addr_value[1] =3D ~0; >> + } >> +} >> + >> +static irqreturn_t hinfc_irq_handle(int irq, void *devid) >> +{ >> + struct hinfc_host *host =3D devid; >> + unsigned int flag; >> + >> + flag =3D hinfc_read(host, HINFC504_INTS); >> + /* store interrupts state */ >> + host->irq_status |=3D flag; >> + >> + if (flag & HINFC504_INTS_DMA) { >> + hinfc_write(host, HINFC504_INTCLR_DMA, HINFC504_INTCLR); >> + complete(&host->cmd_complete); >> + } else if (flag & HINFC504_INTS_CE) { >> + hinfc_write(host, HINFC504_INTCLR_CE, HINFC504_INTCLR); >> + } else if (flag & HINFC504_INTS_UE) { >> + hinfc_write(host, HINFC504_INTCLR_UE, HINFC504_INTCLR); >> + } >> + >> + return IRQ_HANDLED; >> +} >> + >> +static int hisi_nand_read_page_hwecc(struct mtd_info *mtd, >> + struct nand_chip *chip, uint8_t *buf, int oob_required, int page) >> +{ >> + struct hinfc_host *host =3D chip->priv; >> + int max_bitflips =3D 0, stat =3D 0, stat_max, status_ecc; >> + int stat_1, stat_2; >> + >> + chip->read_buf(mtd, buf, mtd->writesize); >> + chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); >> + >> + /* errors which can not be corrected by ECC */ >> + if (host->irq_status & HINFC504_INTS_UE) { >> + mtd->ecc_stats.failed++; >> + } else if (host->irq_status & HINFC504_INTS_CE) { >> + /* TODO: need add other ECC modes! */ >> + switch (chip->ecc.strength) { >> + case 1: >> + stat =3D hweight8(hinfc_read(host, HINFC504_ECC_STATUS)>> >> + HINFC504_ECC_1_BIT_SHIFT); >> + stat_max =3D 1; >> + break; >> + case 16: >> + status_ecc =3D hinfc_read(host, HINFC504_ECC_STATUS) >> >> + HINFC504_ECC_16_BIT_SHIFT & 0x0fff; >> + stat_2 =3D status_ecc & 0x3f; >> + stat_1 =3D status_ecc >> 6 & 0x3f; >> + stat =3D stat_1 + stat_2; >> + stat_max =3D max_t(int, stat_1, stat_2); >> + } >> + mtd->ecc_stats.corrected +=3D stat; >> + max_bitflips =3D max_t(int, max_bitflips, stat_max); >> + } >> + host->irq_status =3D 0; >> + >> + return max_bitflips; >> +} >> + >> +static int hisi_nand_read_oob(struct mtd_info *mtd, struct nand_chi= p *chip, >> + int page) >> +{ >> + struct hinfc_host *host =3D chip->priv; >> + >> + chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page); >> + chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); >> + >> + if (host->irq_status & HINFC504_INTS_UE) { >> + host->irq_status =3D 0; >> + return -EBADMSG; >> + } >> + >> + host->irq_status =3D 0; >> + return 0; >> +} >> + >> +static int hisi_nand_write_page_hwecc(struct mtd_info *mtd, >> + struct nand_chip *chip, const uint8_t *buf, int oob_required) >> +{ >> + chip->write_buf(mtd, buf, mtd->writesize); >> + if (oob_required) >> + chip->write_buf(mtd, chip->oob_poi, mtd->oobsize); >> + >> + return 0; >> +} >> + >> +static void hisi_nfc_host_init(struct hinfc_host *host) >> +{ >> + struct nand_chip *chip =3D &host->chip; >> + unsigned int flag =3D 0; >> + >> + host->version =3D hinfc_read(host, HINFC_VERSION); >> + host->addr_cycle =3D 0; >> + host->addr_value[0] =3D 0; >> + host->addr_value[1] =3D 0; >> + host->cache_addr_value[0] =3D ~0; >> + host->cache_addr_value[1] =3D ~0; >> + host->chipselect =3D 0; >> + >> + /* default page size: 2K, ecc_none. need modify */ >> + flag =3D HINFC504_CON_OP_MODE_NORMAL | HINFC504_CON_READY_BUSY_SEL >> + | ((0x001 & HINFC504_CON_PAGESIZE_MASK) >> + << HINFC504_CON_PAGEISZE_SHIFT) >> + | ((0x0 & HINFC504_CON_ECCTYPE_MASK) >> + << HINFC504_CON_ECCTYPE_SHIFT) >> + | ((chip->options & NAND_BUSWIDTH_16) ? >> + HINFC504_CON_BUS_WIDTH : 0); >> + hinfc_write(host, flag, HINFC504_CON); >> + >> + memset(chip->IO_ADDR_R, 0xff, HINFC504_BUFFER_BASE_ADDRESS_LEN); >> + >> + hinfc_write(host, SET_HINFC504_PWIDTH(HINFC504_W_LATCH, >> + HINFC504_R_LATCH, HINFC504_RW_LATCH), HINFC504_PWIDTH); >> + >> + /* enable DMA irq */ >> + hinfc_write(host, HINFC504_INTEN_DMA, HINFC504_INTEN); >> +} >> + >> +static struct nand_ecclayout nand_ecc_2K_1bit =3D { >> + .oobfree =3D { {24, 40} } >> +}; >> + >> +static struct nand_ecclayout nand_ecc_2K_16bits =3D { >> + .oobavail =3D 6, >> + .oobfree =3D { {2, 6} }, >> +}; >> + >> +static int hisi_nfc_ecc_probe(struct hinfc_host *host) >> +{ >> + unsigned int flag; >> + int size, strength, ecc_bits; >> + struct device *dev =3D host->dev; >> + struct nand_chip *chip =3D &host->chip; >> + struct mtd_info *mtd =3D &host->mtd; >> + struct device_node *np =3D host->dev->of_node; >> + >> + size =3D of_get_nand_ecc_step_size(np); >> + strength =3D of_get_nand_ecc_strength(np); >> + if ((size !=3D 512) && (size !=3D 1024)) { >> + dev_err(dev, "error ecc size: %d\n", size); >> + return -EINVAL; >> + } >> + >> + if ((size =3D=3D 512) && (strength !=3D 1)) { >> + dev_err(dev, "ecc size and strength do not match\n"); >> + return -EINVAL; >> + } >> + if ((size =3D=3D 1024) && ((strength !=3D 8) && (strength !=3D 16)= && >> + (strength !=3D 24) && (strength !=3D 40))) { >> + dev_err(dev, "ecc size and strength do not match\n"); >> + return -EINVAL; >> + } >> + >> + chip->ecc.size =3D size; >> + chip->ecc.strength =3D strength; >> + >> + chip->ecc.read_page =3D hisi_nand_read_page_hwecc; >> + chip->ecc.read_oob =3D hisi_nand_read_oob; >> + chip->ecc.write_page =3D hisi_nand_write_page_hwecc; >> + >> + switch (chip->ecc.strength) { >> + case 1: >> + ecc_bits =3D 1; >> + if (mtd->writesize =3D=3D 2048) >> + chip->ecc.layout =3D &nand_ecc_2K_1bit; >> + >> + /* TODO: add more page size support */ >> + break; >> + case 16: >> + ecc_bits =3D 6; >> + if (mtd->writesize =3D=3D 2048) >> + chip->ecc.layout =3D &nand_ecc_2K_16bits; >> + >> + /* TODO: add more page size support */ >> + break; >> + >> + /* TODO: add more ecc strength support */ >> + default: >> + dev_err(dev, "not support strength: %d\n", chip->ecc.strength); >> + return -EINVAL; >> + } >> + >> + flag =3D hinfc_read(host, HINFC504_CON); >> + /* add ecc type configure */ >> + flag |=3D ((ecc_bits & HINFC504_CON_ECCTYPE_MASK) >> + << HINFC504_CON_ECCTYPE_SHIFT); >> + hinfc_write(host, flag, HINFC504_CON); >> + >> + /* enable ecc irq */ >> + flag =3D hinfc_read(host, HINFC504_INTEN) & 0xfff; >> + hinfc_write(host, flag | HINFC504_INTEN_UE | HINFC504_INTEN_CE, >> + HINFC504_INTEN); >> + >> + return 0; >> +} >> + >> +static int hisi_nfc_probe(struct platform_device *pdev) >> +{ >> + int ret =3D 0, irq, buswidth, flag, max_chips =3D HINFC504_MAX_CHI= P; >> + struct device *dev =3D &pdev->dev; >> + struct hinfc_host *host; >> + struct nand_chip *chip; >> + struct mtd_info *mtd; >> + struct resource *res; >> + struct device_node *np =3D dev->of_node; >> + struct mtd_part_parser_data ppdata; >> + >> + host =3D devm_kzalloc(dev, sizeof(*host), GFP_KERNEL); >> + if (!host) >> + return -ENOMEM; >> + host->dev =3D dev; >> + >> + platform_set_drvdata(pdev, host); >> + chip =3D &host->chip; >> + mtd =3D &host->mtd; >> + >> + irq =3D platform_get_irq(pdev, 0); >> + if (irq < 0) { >> + dev_err(dev, "no IRQ resource defined\n"); >> + ret =3D -ENXIO; >> + goto err_res; >> + } >> + >> + res =3D platform_get_resource(pdev, IORESOURCE_MEM, 0); >> + host->iobase =3D devm_ioremap_resource(dev, res); >> + if (IS_ERR(host->iobase)) { >> + ret =3D PTR_ERR(host->iobase); >> + dev_err(dev, "devm_ioremap_resource[0] fail\n"); >=20 > I don't think yhou really need this error print. devm_ioremap_resourc= e() > should be descriptive enough, I think. Will delete the error print here, thanks! >=20 >> + goto err_res; >> + } >> + >> + res =3D platform_get_resource(pdev, IORESOURCE_MEM, 1); >> + chip->IO_ADDR_R =3D chip->IO_ADDR_W =3D devm_ioremap_resource(dev,= res); >=20 > Hmm, do you really have to reuse IO_ADDR_{R,W} here? Those are only > targeted for NAND systems which have a direct MMIO mapping to the NAN= D > I/O pins. See nand_base's {read,write}_buf() and read_{byte,word}() > implementations. But you override those. There is a hardware buffer in this NAND controller, and the buffer can = be accessed as MMIO. IO_ADDR_R/W just indicates the base address of this b= uffer. Maybe I need to use a void __iomem pointer stored in my host struct to = use this buffer instead of IO_ADDR_R/W as you said below here? >=20 > It's best if it's obvious if nand_base is somehow inadvertently using > these pointers. So leaving them NULL is helpful. >=20 > As an alternative, you can just stash another private void __iomem > pointer in you your host struct. >=20 >> + if (IS_ERR(chip->IO_ADDR_R)) { >> + ret =3D PTR_ERR(chip->IO_ADDR_R); >> + dev_err(dev, "devm_ioremap_resource[1] fail\n"); >> + goto err_res; >> + } >> + >> + mtd->priv =3D chip; >> + mtd->owner =3D THIS_MODULE; >> + mtd->name =3D "hisi_nand"; >> + mtd->dev.parent =3D &pdev->dev; >> + >> + chip->priv =3D host; >> + chip->cmdfunc =3D hisi_nfc_cmdfunc; >> + chip->select_chip =3D hisi_nfc_select_chip; >> + chip->read_byte =3D hisi_nfc_read_byte; >> + chip->read_word =3D hisi_nfc_read_word; >> + chip->write_buf =3D hisi_nfc_write_buf; >> + chip->read_buf =3D hisi_nfc_read_buf; >> + chip->chip_delay =3D HINFC504_CHIP_DELAY; >> + >> + chip->ecc.mode =3D of_get_nand_ecc_mode(np); >> + >> + buswidth =3D of_get_nand_bus_width(np); >> + if (buswidth =3D=3D 16) >> + chip->options |=3D NAND_BUSWIDTH_16; >> + >> + hisi_nfc_host_init(host); >> + >> + ret =3D devm_request_irq(dev, irq, hinfc_irq_handle, IRQF_DISABLED= , >> + "nandc", host); >> + if (ret) { >> + dev_err(dev, "failed to request IRQ\n"); >> + goto err_res; >> + } >> + >> + ret =3D nand_scan_ident(mtd, max_chips, NULL); >> + if (ret) { >> + ret =3D -ENODEV; >> + goto err_res; >> + } >> + >> + host->buffer =3D dmam_alloc_coherent(dev, mtd->writesize + mtd->oo= bsize, >> + &host->dma_buffer, GFP_KERNEL); >=20 > You need to check this for allocation failures. > Will add relative code to check this, thanks! >> + host->dma_oob =3D host->dma_buffer + mtd->writesize; >> + memset(host->buffer, 0xff, mtd->writesize + mtd->oobsize); >> + >> + flag =3D hinfc_read(host, HINFC504_CON); >> + flag &=3D ~(HINFC504_CON_PAGESIZE_MASK << HINFC504_CON_PAGEISZE_SH= IFT); >> + switch (mtd->writesize) { >> + case 2048: >> + flag |=3D (0x001 << HINFC504_CON_PAGEISZE_SHIFT); break; >> + /* >> + * TODO: add more pagesize support, >> + * default pagesize has been set in hisi_nfc_host_init >> + */ >> + default: >> + dev_err(dev, "NON-2KB page size nand flash\n"); >> + ret =3D -EINVAL; >> + goto err_res; >> + } >> + hinfc_write(host, flag, HINFC504_CON); >> + >> + if (chip->ecc.mode =3D=3D NAND_ECC_HW) >> + hisi_nfc_ecc_probe(host); >> + >> + ret =3D nand_scan_tail(mtd); >> + if (ret) { >> + dev_err(dev, "nand_scan_tail failed: %d\n", ret); >> + goto err_res; >> + } >> + >> + ppdata.of_node =3D np; >> + ret =3D mtd_device_parse_register(mtd, NULL, &ppdata, NULL, 0); >> + if (ret) { >> + dev_err(dev, "Err MTD partition=3D%d\n", ret); >> + goto err_mtd; >> + } >> + >> + return 0; >> + >> +err_mtd: >> + nand_release(mtd); >> +err_res: >> + return ret; >> +} >> + >> +static int hisi_nfc_remove(struct platform_device *pdev) >> +{ >> + struct hinfc_host *host =3D platform_get_drvdata(pdev); >> + struct mtd_info *mtd =3D &host->mtd; >> + >> + nand_release(mtd); >> + >> + return 0; >> +} >> + >> +#ifdef CONFIG_PM_SLEEP >> +static int hisi_nfc_suspend(struct device *dev) >> +{ >> + struct hinfc_host *host =3D dev_get_drvdata(dev); >> + unsigned long timeout =3D jiffies + HINFC504_NFC_PM_TIMEOUT; >> + >> + while (time_before(jiffies, timeout)) { >> + if (((hinfc_read(host, HINFC504_STATUS) & 0x1) =3D=3D 0x0) && >> + (hinfc_read(host, HINFC504_DMA_CTRL) & >> + HINFC504_DMA_CTRL_DMA_START)) { >> + _cond_resched(); >=20 > Why not just cond_resched()? > It is better to just use cond_resched(), will modify this. Thanks! >> + return 0; >> + } >> + } >> + >> + dev_err(host->dev, "nand controller suspend timeout.\n"); >> + >> + return -EAGAIN; >> +} >> + >> +static int hisi_nfc_resume(struct device *dev) >> +{ >> + int cs; >> + struct hinfc_host *host =3D dev_get_drvdata(dev); >> + struct nand_chip *chip =3D &host->chip; >> + >> + for (cs =3D 0; cs < chip->numchips; cs++) >> + hisi_nfc_send_cmd_reset(host, cs); >> + hinfc_write(host, SET_HINFC504_PWIDTH(HINFC504_W_LATCH, >> + HINFC504_R_LATCH, HINFC504_RW_LATCH), HINFC504_PWIDTH); >> + >> + return 0; >> +} >> +#endif >> +static SIMPLE_DEV_PM_OPS(hisi_nfc_pm_ops, hisi_nfc_suspend, hisi_nf= c_resume); >> + >> +static const struct of_device_id nfc_id_table[] =3D { >> + { .compatible =3D "hisilicon,504-nfc" }, >> + {} >> +}; >> +MODULE_DEVICE_TABLE(of, nfc_id_table); >> + >> +static struct platform_driver hisi_nfc_driver =3D { >> + .driver =3D { >> + .name =3D "hisi_nand", >> + .of_match_table =3D of_match_ptr(nfc_id_table), >> + .pm =3D &hisi_nfc_pm_ops, >> + }, >> + .probe =3D hisi_nfc_probe, >> + .remove =3D hisi_nfc_remove, >> +}; >> + >> +module_platform_driver(hisi_nfc_driver); >> + >> +MODULE_LICENSE("GPL"); >> +MODULE_AUTHOR("Zhiyong Cai"); >> +MODULE_AUTHOR("Zhou Wang"); >> +MODULE_DESCRIPTION("Hisilicon Nand Flash Controller Driver"); >=20 > Brian Thanks again for your comments. Zhou Wang >=20 > . >=20 -- To unsubscribe from this list: send the line "unsubscribe devicetree" i= n the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org More majordomo info at http://vger.kernel.org/majordomo-info.html