Path: blob/master/arch/cris/arch-v32/drivers/mach-fs/nandflash.c
15131 views
/*1* arch/cris/arch-v32/drivers/nandflash.c2*3* Copyright (c) 20044*5* Derived from drivers/mtd/nand/spia.c6* Copyright (C) 2000 Steven J. Hill ([email protected])7*8* This program is free software; you can redistribute it and/or modify9* it under the terms of the GNU General Public License version 2 as10* published by the Free Software Foundation.11*12*/1314#include <linux/slab.h>15#include <linux/init.h>16#include <linux/module.h>17#include <linux/mtd/mtd.h>18#include <linux/mtd/nand.h>19#include <linux/mtd/partitions.h>20#include <arch/memmap.h>21#include <hwregs/reg_map.h>22#include <hwregs/reg_rdwr.h>23#include <hwregs/gio_defs.h>24#include <hwregs/bif_core_defs.h>25#include <asm/io.h>2627#define CE_BIT 428#define CLE_BIT 529#define ALE_BIT 630#define BY_BIT 73132struct mtd_info_wrapper {33struct mtd_info info;34struct nand_chip chip;35};3637/* Bitmask for control pins */38#define PIN_BITMASK ((1 << CE_BIT) | (1 << CLE_BIT) | (1 << ALE_BIT))3940/* Bitmask for mtd nand control bits */41#define CTRL_BITMASK (NAND_NCE | NAND_CLE | NAND_ALE)424344static struct mtd_info *crisv32_mtd;45/*46* hardware specific access to control-lines47*/48static void crisv32_hwcontrol(struct mtd_info *mtd, int cmd,49unsigned int ctrl)50{51unsigned long flags;52reg_gio_rw_pa_dout dout;53struct nand_chip *this = mtd->priv;5455local_irq_save(flags);5657/* control bits change */58if (ctrl & NAND_CTRL_CHANGE) {59dout = REG_RD(gio, regi_gio, rw_pa_dout);60dout.data &= ~PIN_BITMASK;6162#if (CE_BIT == 4 && NAND_NCE == 1 && \63CLE_BIT == 5 && NAND_CLE == 2 && \64ALE_BIT == 6 && NAND_ALE == 4)65/* Pins in same order as control bits, but shifted.66* Optimize for this case; works for 2.6.18 */67dout.data |= ((ctrl & CTRL_BITMASK) ^ NAND_NCE) << CE_BIT;68#else69/* the slow way */70if (!(ctrl & NAND_NCE))71dout.data |= (1 << CE_BIT);72if (ctrl & NAND_CLE)73dout.data |= (1 << CLE_BIT);74if (ctrl & NAND_ALE)75dout.data |= (1 << ALE_BIT);76#endif77REG_WR(gio, regi_gio, rw_pa_dout, dout);78}7980/* command to chip */81if (cmd != NAND_CMD_NONE)82writeb(cmd, this->IO_ADDR_W);8384local_irq_restore(flags);85}8687/*88* read device ready pin89*/90static int crisv32_device_ready(struct mtd_info *mtd)91{92reg_gio_r_pa_din din = REG_RD(gio, regi_gio, r_pa_din);93return ((din.data & (1 << BY_BIT)) >> BY_BIT);94}9596/*97* Main initialization routine98*/99struct mtd_info *__init crisv32_nand_flash_probe(void)100{101void __iomem *read_cs;102void __iomem *write_cs;103104reg_bif_core_rw_grp3_cfg bif_cfg = REG_RD(bif_core, regi_bif_core,105rw_grp3_cfg);106reg_gio_rw_pa_oe pa_oe = REG_RD(gio, regi_gio, rw_pa_oe);107struct mtd_info_wrapper *wrapper;108struct nand_chip *this;109int err = 0;110111/* Allocate memory for MTD device structure and private data */112wrapper = kzalloc(sizeof(struct mtd_info_wrapper), GFP_KERNEL);113if (!wrapper) {114printk(KERN_ERR "Unable to allocate CRISv32 NAND MTD "115"device structure.\n");116err = -ENOMEM;117return NULL;118}119120read_cs = ioremap(MEM_CSP0_START | MEM_NON_CACHEABLE, 8192);121write_cs = ioremap(MEM_CSP1_START | MEM_NON_CACHEABLE, 8192);122123if (!read_cs || !write_cs) {124printk(KERN_ERR "CRISv32 NAND ioremap failed\n");125err = -EIO;126goto out_mtd;127}128129/* Get pointer to private data */130this = &wrapper->chip;131crisv32_mtd = &wrapper->info;132133pa_oe.oe |= 1 << CE_BIT;134pa_oe.oe |= 1 << ALE_BIT;135pa_oe.oe |= 1 << CLE_BIT;136pa_oe.oe &= ~(1 << BY_BIT);137REG_WR(gio, regi_gio, rw_pa_oe, pa_oe);138139bif_cfg.gated_csp0 = regk_bif_core_rd;140bif_cfg.gated_csp1 = regk_bif_core_wr;141REG_WR(bif_core, regi_bif_core, rw_grp3_cfg, bif_cfg);142143/* Link the private data with the MTD structure */144crisv32_mtd->priv = this;145146/* Set address of NAND IO lines */147this->IO_ADDR_R = read_cs;148this->IO_ADDR_W = write_cs;149this->cmd_ctrl = crisv32_hwcontrol;150this->dev_ready = crisv32_device_ready;151/* 20 us command delay time */152this->chip_delay = 20;153this->ecc.mode = NAND_ECC_SOFT;154155/* Enable the following for a flash based bad block table */156/* this->options = NAND_USE_FLASH_BBT; */157158/* Scan to find existence of the device */159if (nand_scan(crisv32_mtd, 1)) {160err = -ENXIO;161goto out_ior;162}163164return crisv32_mtd;165166out_ior:167iounmap((void *)read_cs);168iounmap((void *)write_cs);169out_mtd:170kfree(wrapper);171return NULL;172}173174175176