blob: 5898ac71175dd96b7dd4678b9732d851e5a70630 [file] [log] [blame]
Jesper Nilsson6107c612007-11-29 17:05:58 +01001/*
2 * arch/cris/arch-v32/drivers/nandflash.c
3 *
4 * Copyright (c) 2004
5 *
6 * Derived from drivers/mtd/nand/spia.c
7 * Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 */
14
15#include <linux/slab.h>
16#include <linux/init.h>
17#include <linux/module.h>
18#include <linux/mtd/mtd.h>
19#include <linux/mtd/nand.h>
20#include <linux/mtd/partitions.h>
21#include <asm/arch/memmap.h>
22#include <hwregs/reg_map.h>
23#include <hwregs/reg_rdwr.h>
24#include <hwregs/gio_defs.h>
25#include <hwregs/bif_core_defs.h>
26#include <asm/io.h>
27
28#define CE_BIT 4
29#define CLE_BIT 5
30#define ALE_BIT 6
31#define BY_BIT 7
32
33/* Bitmask for control pins */
34#define PIN_BITMASK ((1 << CE_BIT) | (1 << CLE_BIT) | (1 << ALE_BIT))
35
36/* Bitmask for mtd nand control bits */
37#define CTRL_BITMASK (NAND_NCE | NAND_CLE | NAND_ALE)
38
39
40static struct mtd_info *crisv32_mtd;
41/*
42 * hardware specific access to control-lines
43 */
44static void crisv32_hwcontrol(struct mtd_info *mtd, int cmd,
45 unsigned int ctrl)
46{
47 unsigned long flags;
48 reg_gio_rw_pa_dout dout;
49 struct nand_chip *this = mtd->priv;
50
51 local_irq_save(flags);
52
53 /* control bits change */
54 if (ctrl & NAND_CTRL_CHANGE) {
55 dout = REG_RD(gio, regi_gio, rw_pa_dout);
56 dout.data &= ~PIN_BITMASK;
57
58#if (CE_BIT == 4 && NAND_NCE == 1 && \
59 CLE_BIT == 5 && NAND_CLE == 2 && \
60 ALE_BIT == 6 && NAND_ALE == 4)
61 /* Pins in same order as control bits, but shifted.
62 * Optimize for this case; works for 2.6.18 */
63 dout.data |= ((ctrl & CTRL_BITMASK) ^ NAND_NCE) << CE_BIT;
64#else
65 /* the slow way */
66 if (!(ctrl & NAND_NCE))
67 dout.data |= (1 << CE_BIT);
68 if (ctrl & NAND_CLE)
69 dout.data |= (1 << CLE_BIT);
70 if (ctrl & NAND_ALE)
71 dout.data |= (1 << ALE_BIT);
72#endif
73 REG_WR(gio, regi_gio, rw_pa_dout, dout);
74 }
75
76 /* command to chip */
77 if (cmd != NAND_CMD_NONE)
78 writeb(cmd, this->IO_ADDR_W);
79
80 local_irq_restore(flags);
81}
82
83/*
84* read device ready pin
85*/
86int crisv32_device_ready(struct mtd_info *mtd)
87{
88 reg_gio_r_pa_din din = REG_RD(gio, regi_gio, r_pa_din);
89 return ((din.data & (1 << BY_BIT)) >> BY_BIT);
90}
91
92/*
93 * Main initialization routine
94 */
95struct mtd_info *__init crisv32_nand_flash_probe(void)
96{
97 void __iomem *read_cs;
98 void __iomem *write_cs;
99
100 reg_bif_core_rw_grp3_cfg bif_cfg = REG_RD(bif_core, regi_bif_core,
101 rw_grp3_cfg);
102 reg_gio_rw_pa_oe pa_oe = REG_RD(gio, regi_gio, rw_pa_oe);
103 struct nand_chip *this;
104 int err = 0;
105
106 /* Allocate memory for MTD device structure and private data */
107 crisv32_mtd = kmalloc(sizeof(struct mtd_info) +
108 sizeof(struct nand_chip), GFP_KERNEL);
109 if (!crisv32_mtd) {
110 printk(KERN_ERR "Unable to allocate CRISv32 NAND MTD "
111 "device structure.\n");
112 err = -ENOMEM;
113 return NULL;
114 }
115
116 read_cs = ioremap(MEM_CSP0_START | MEM_NON_CACHEABLE, 8192);
117 write_cs = ioremap(MEM_CSP1_START | MEM_NON_CACHEABLE, 8192);
118
119 if (!read_cs || !write_cs) {
120 printk(KERN_ERR "CRISv32 NAND ioremap failed\n");
121 err = -EIO;
122 goto out_mtd;
123 }
124
125 /* Get pointer to private data */
126 this = (struct nand_chip *) (&crisv32_mtd[1]);
127
128 pa_oe.oe |= 1 << CE_BIT;
129 pa_oe.oe |= 1 << ALE_BIT;
130 pa_oe.oe |= 1 << CLE_BIT;
131 pa_oe.oe &= ~(1 << BY_BIT);
132 REG_WR(gio, regi_gio, rw_pa_oe, pa_oe);
133
134 bif_cfg.gated_csp0 = regk_bif_core_rd;
135 bif_cfg.gated_csp1 = regk_bif_core_wr;
136 REG_WR(bif_core, regi_bif_core, rw_grp3_cfg, bif_cfg);
137
138 /* Initialize structures */
139 memset((char *) crisv32_mtd, 0, sizeof(struct mtd_info));
140 memset((char *) this, 0, sizeof(struct nand_chip));
141
142 /* Link the private data with the MTD structure */
143 crisv32_mtd->priv = this;
144
145 /* Set address of NAND IO lines */
146 this->IO_ADDR_R = read_cs;
147 this->IO_ADDR_W = write_cs;
148 this->cmd_ctrl = crisv32_hwcontrol;
149 this->dev_ready = crisv32_device_ready;
150 /* 20 us command delay time */
151 this->chip_delay = 20;
152 this->ecc.mode = NAND_ECC_SOFT;
153
154 /* Enable the following for a flash based bad block table */
155 /* this->options = NAND_USE_FLASH_BBT; */
156
157 /* Scan to find existance of the device */
158 if (nand_scan(crisv32_mtd, 1)) {
159 err = -ENXIO;
160 goto out_ior;
161 }
162
163 return crisv32_mtd;
164
165out_ior:
166 iounmap((void *)read_cs);
167 iounmap((void *)write_cs);
168out_mtd:
169 kfree(crisv32_mtd);
170 return NULL;
171}
172