Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/mtd/nand/au1550nd.c |
| 3 | * |
| 4 | * Copyright (C) 2004 Embedded Edge, LLC |
| 5 | * |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 6 | * $Id: au1550nd.c,v 1.13 2005/11/07 11:14:30 gleixner Exp $ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/module.h> |
Sergei Shtylyov | 35af68b | 2006-05-16 20:52:06 +0400 | [diff] [blame] | 17 | #include <linux/interrupt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/mtd/mtd.h> |
| 19 | #include <linux/mtd/nand.h> |
| 20 | #include <linux/mtd/partitions.h> |
Olaf Hering | 733482e | 2005-11-08 21:34:55 -0800 | [diff] [blame] | 21 | #include <linux/version.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <asm/io.h> |
| 23 | |
| 24 | /* fixme: this is ugly */ |
| 25 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 0) |
Pete Popov | ef6f0d1 | 2005-09-23 02:44:58 +0100 | [diff] [blame] | 26 | #include <asm/mach-au1x00/au1xxx.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #else |
| 28 | #include <asm/au1000.h> |
| 29 | #ifdef CONFIG_MIPS_PB1550 |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 30 | #include <asm/pb1550.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #endif |
| 32 | #ifdef CONFIG_MIPS_DB1550 |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 33 | #include <asm/db1x00.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #endif |
| 35 | #endif |
| 36 | |
| 37 | /* |
| 38 | * MTD structure for NAND controller |
| 39 | */ |
| 40 | static struct mtd_info *au1550_mtd = NULL; |
| 41 | static void __iomem *p_nand; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 42 | static int nand_width = 1; /* default x8 */ |
Thomas Gleixner | cad74f2 | 2006-05-23 23:28:48 +0200 | [diff] [blame^] | 43 | static void (*au1550_write_byte)(struct mtd_info *, u_char); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | /* |
| 46 | * Define partitions for flash device |
| 47 | */ |
Jesper Juhl | 3c6bee1 | 2006-01-09 20:54:01 -0800 | [diff] [blame] | 48 | static const struct mtd_partition partition_info[] = { |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 49 | { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 50 | .name = "NAND FS 0", |
| 51 | .offset = 0, |
| 52 | .size = 8 * 1024 * 1024}, |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 53 | { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 54 | .name = "NAND FS 1", |
| 55 | .offset = MTDPART_OFS_APPEND, |
| 56 | .size = MTDPART_SIZ_FULL} |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * au_read_byte - read one byte from the chip |
| 61 | * @mtd: MTD device structure |
| 62 | * |
| 63 | * read function for 8bit buswith |
| 64 | */ |
| 65 | static u_char au_read_byte(struct mtd_info *mtd) |
| 66 | { |
| 67 | struct nand_chip *this = mtd->priv; |
| 68 | u_char ret = readb(this->IO_ADDR_R); |
| 69 | au_sync(); |
| 70 | return ret; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * au_write_byte - write one byte to the chip |
| 75 | * @mtd: MTD device structure |
| 76 | * @byte: pointer to data byte to write |
| 77 | * |
| 78 | * write function for 8it buswith |
| 79 | */ |
| 80 | static void au_write_byte(struct mtd_info *mtd, u_char byte) |
| 81 | { |
| 82 | struct nand_chip *this = mtd->priv; |
| 83 | writeb(byte, this->IO_ADDR_W); |
| 84 | au_sync(); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * au_read_byte16 - read one byte endianess aware from the chip |
| 89 | * @mtd: MTD device structure |
| 90 | * |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 91 | * read function for 16bit buswith with |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | * endianess conversion |
| 93 | */ |
| 94 | static u_char au_read_byte16(struct mtd_info *mtd) |
| 95 | { |
| 96 | struct nand_chip *this = mtd->priv; |
| 97 | u_char ret = (u_char) cpu_to_le16(readw(this->IO_ADDR_R)); |
| 98 | au_sync(); |
| 99 | return ret; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * au_write_byte16 - write one byte endianess aware to the chip |
| 104 | * @mtd: MTD device structure |
| 105 | * @byte: pointer to data byte to write |
| 106 | * |
| 107 | * write function for 16bit buswith with |
| 108 | * endianess conversion |
| 109 | */ |
| 110 | static void au_write_byte16(struct mtd_info *mtd, u_char byte) |
| 111 | { |
| 112 | struct nand_chip *this = mtd->priv; |
| 113 | writew(le16_to_cpu((u16) byte), this->IO_ADDR_W); |
| 114 | au_sync(); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * au_read_word - read one word from the chip |
| 119 | * @mtd: MTD device structure |
| 120 | * |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 121 | * read function for 16bit buswith without |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | * endianess conversion |
| 123 | */ |
| 124 | static u16 au_read_word(struct mtd_info *mtd) |
| 125 | { |
| 126 | struct nand_chip *this = mtd->priv; |
| 127 | u16 ret = readw(this->IO_ADDR_R); |
| 128 | au_sync(); |
| 129 | return ret; |
| 130 | } |
| 131 | |
| 132 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | * au_write_buf - write buffer to chip |
| 134 | * @mtd: MTD device structure |
| 135 | * @buf: data buffer |
| 136 | * @len: number of bytes to write |
| 137 | * |
| 138 | * write function for 8bit buswith |
| 139 | */ |
| 140 | static void au_write_buf(struct mtd_info *mtd, const u_char *buf, int len) |
| 141 | { |
| 142 | int i; |
| 143 | struct nand_chip *this = mtd->priv; |
| 144 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 145 | for (i = 0; i < len; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | writeb(buf[i], this->IO_ADDR_W); |
| 147 | au_sync(); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /** |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 152 | * au_read_buf - read chip data into buffer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | * @mtd: MTD device structure |
| 154 | * @buf: buffer to store date |
| 155 | * @len: number of bytes to read |
| 156 | * |
| 157 | * read function for 8bit buswith |
| 158 | */ |
| 159 | static void au_read_buf(struct mtd_info *mtd, u_char *buf, int len) |
| 160 | { |
| 161 | int i; |
| 162 | struct nand_chip *this = mtd->priv; |
| 163 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 164 | for (i = 0; i < len; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | buf[i] = readb(this->IO_ADDR_R); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 166 | au_sync(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
| 170 | /** |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 171 | * au_verify_buf - Verify chip data against buffer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | * @mtd: MTD device structure |
| 173 | * @buf: buffer containing the data to compare |
| 174 | * @len: number of bytes to compare |
| 175 | * |
| 176 | * verify function for 8bit buswith |
| 177 | */ |
| 178 | static int au_verify_buf(struct mtd_info *mtd, const u_char *buf, int len) |
| 179 | { |
| 180 | int i; |
| 181 | struct nand_chip *this = mtd->priv; |
| 182 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 183 | for (i = 0; i < len; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | if (buf[i] != readb(this->IO_ADDR_R)) |
| 185 | return -EFAULT; |
| 186 | au_sync(); |
| 187 | } |
| 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * au_write_buf16 - write buffer to chip |
| 194 | * @mtd: MTD device structure |
| 195 | * @buf: data buffer |
| 196 | * @len: number of bytes to write |
| 197 | * |
| 198 | * write function for 16bit buswith |
| 199 | */ |
| 200 | static void au_write_buf16(struct mtd_info *mtd, const u_char *buf, int len) |
| 201 | { |
| 202 | int i; |
| 203 | struct nand_chip *this = mtd->priv; |
| 204 | u16 *p = (u16 *) buf; |
| 205 | len >>= 1; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 206 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 207 | for (i = 0; i < len; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | writew(p[i], this->IO_ADDR_W); |
| 209 | au_sync(); |
| 210 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 211 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /** |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 215 | * au_read_buf16 - read chip data into buffer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | * @mtd: MTD device structure |
| 217 | * @buf: buffer to store date |
| 218 | * @len: number of bytes to read |
| 219 | * |
| 220 | * read function for 16bit buswith |
| 221 | */ |
| 222 | static void au_read_buf16(struct mtd_info *mtd, u_char *buf, int len) |
| 223 | { |
| 224 | int i; |
| 225 | struct nand_chip *this = mtd->priv; |
| 226 | u16 *p = (u16 *) buf; |
| 227 | len >>= 1; |
| 228 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 229 | for (i = 0; i < len; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | p[i] = readw(this->IO_ADDR_R); |
| 231 | au_sync(); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | /** |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 236 | * au_verify_buf16 - Verify chip data against buffer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | * @mtd: MTD device structure |
| 238 | * @buf: buffer containing the data to compare |
| 239 | * @len: number of bytes to compare |
| 240 | * |
| 241 | * verify function for 16bit buswith |
| 242 | */ |
| 243 | static int au_verify_buf16(struct mtd_info *mtd, const u_char *buf, int len) |
| 244 | { |
| 245 | int i; |
| 246 | struct nand_chip *this = mtd->priv; |
| 247 | u16 *p = (u16 *) buf; |
| 248 | len >>= 1; |
| 249 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 250 | for (i = 0; i < len; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | if (p[i] != readw(this->IO_ADDR_R)) |
| 252 | return -EFAULT; |
| 253 | au_sync(); |
| 254 | } |
| 255 | return 0; |
| 256 | } |
| 257 | |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 258 | /* Select the chip by setting nCE to low */ |
| 259 | #define NAND_CTL_SETNCE 1 |
| 260 | /* Deselect the chip by setting nCE to high */ |
| 261 | #define NAND_CTL_CLRNCE 2 |
| 262 | /* Select the command latch by setting CLE to high */ |
| 263 | #define NAND_CTL_SETCLE 3 |
| 264 | /* Deselect the command latch by setting CLE to low */ |
| 265 | #define NAND_CTL_CLRCLE 4 |
| 266 | /* Select the address latch by setting ALE to high */ |
| 267 | #define NAND_CTL_SETALE 5 |
| 268 | /* Deselect the address latch by setting ALE to low */ |
| 269 | #define NAND_CTL_CLRALE 6 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | |
| 271 | static void au1550_hwcontrol(struct mtd_info *mtd, int cmd) |
| 272 | { |
| 273 | register struct nand_chip *this = mtd->priv; |
| 274 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 275 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 277 | case NAND_CTL_SETCLE: |
| 278 | this->IO_ADDR_W = p_nand + MEM_STNAND_CMD; |
| 279 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 281 | case NAND_CTL_CLRCLE: |
| 282 | this->IO_ADDR_W = p_nand + MEM_STNAND_DATA; |
| 283 | break; |
| 284 | |
| 285 | case NAND_CTL_SETALE: |
| 286 | this->IO_ADDR_W = p_nand + MEM_STNAND_ADDR; |
| 287 | break; |
| 288 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 289 | case NAND_CTL_CLRALE: |
| 290 | this->IO_ADDR_W = p_nand + MEM_STNAND_DATA; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 291 | /* FIXME: Nobody knows why this is necessary, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | * but it works only that way */ |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 293 | udelay(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | break; |
| 295 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 296 | case NAND_CTL_SETNCE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | /* assert (force assert) chip enable */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 298 | au_writel((1 << (4 + NAND_CS)), MEM_STNDCTL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | break; |
| 300 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 301 | case NAND_CTL_CLRNCE: |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 302 | /* deassert chip enable */ |
| 303 | au_writel(0, MEM_STNDCTL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | break; |
| 305 | } |
| 306 | |
| 307 | this->IO_ADDR_R = this->IO_ADDR_W; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 308 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | /* Drain the writebuffer */ |
| 310 | au_sync(); |
| 311 | } |
| 312 | |
| 313 | int au1550_device_ready(struct mtd_info *mtd) |
| 314 | { |
| 315 | int ret = (au_readl(MEM_STSTAT) & 0x1) ? 1 : 0; |
| 316 | au_sync(); |
| 317 | return ret; |
| 318 | } |
| 319 | |
Sergei Shtylyov | 35af68b | 2006-05-16 20:52:06 +0400 | [diff] [blame] | 320 | /** |
| 321 | * au1550_select_chip - control -CE line |
| 322 | * Forbid driving -CE manually permitting the NAND controller to do this. |
| 323 | * Keeping -CE asserted during the whole sector reads interferes with the |
| 324 | * NOR flash and PCMCIA drivers as it causes contention on the static bus. |
| 325 | * We only have to hold -CE low for the NAND read commands since the flash |
| 326 | * chip needs it to be asserted during chip not ready time but the NAND |
| 327 | * controller keeps it released. |
| 328 | * |
| 329 | * @mtd: MTD device structure |
| 330 | * @chip: chipnumber to select, -1 for deselect |
| 331 | */ |
| 332 | static void au1550_select_chip(struct mtd_info *mtd, int chip) |
| 333 | { |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * au1550_command - Send command to NAND device |
| 338 | * @mtd: MTD device structure |
| 339 | * @command: the command to be sent |
| 340 | * @column: the column address for this command, -1 if none |
| 341 | * @page_addr: the page address for this command, -1 if none |
| 342 | */ |
| 343 | static void au1550_command(struct mtd_info *mtd, unsigned command, int column, int page_addr) |
| 344 | { |
| 345 | register struct nand_chip *this = mtd->priv; |
| 346 | int ce_override = 0, i; |
| 347 | ulong flags; |
| 348 | |
| 349 | /* Begin command latch cycle */ |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 350 | au1550_hwcontrol(mtd, NAND_CTL_SETCLE); |
Sergei Shtylyov | 35af68b | 2006-05-16 20:52:06 +0400 | [diff] [blame] | 351 | /* |
| 352 | * Write out the command to the device. |
| 353 | */ |
| 354 | if (command == NAND_CMD_SEQIN) { |
| 355 | int readcmd; |
| 356 | |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 357 | if (column >= mtd->writesize) { |
Sergei Shtylyov | 35af68b | 2006-05-16 20:52:06 +0400 | [diff] [blame] | 358 | /* OOB area */ |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 359 | column -= mtd->writesize; |
Sergei Shtylyov | 35af68b | 2006-05-16 20:52:06 +0400 | [diff] [blame] | 360 | readcmd = NAND_CMD_READOOB; |
| 361 | } else if (column < 256) { |
| 362 | /* First 256 bytes --> READ0 */ |
| 363 | readcmd = NAND_CMD_READ0; |
| 364 | } else { |
| 365 | column -= 256; |
| 366 | readcmd = NAND_CMD_READ1; |
| 367 | } |
Thomas Gleixner | cad74f2 | 2006-05-23 23:28:48 +0200 | [diff] [blame^] | 368 | au1550_write_byte(mtd, readcmd); |
Sergei Shtylyov | 35af68b | 2006-05-16 20:52:06 +0400 | [diff] [blame] | 369 | } |
Thomas Gleixner | cad74f2 | 2006-05-23 23:28:48 +0200 | [diff] [blame^] | 370 | au1550_write_byte(mtd, command); |
Sergei Shtylyov | 35af68b | 2006-05-16 20:52:06 +0400 | [diff] [blame] | 371 | |
| 372 | /* Set ALE and clear CLE to start address cycle */ |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 373 | au1550_hwcontrol(mtd, NAND_CTL_CLRCLE); |
Sergei Shtylyov | 35af68b | 2006-05-16 20:52:06 +0400 | [diff] [blame] | 374 | |
| 375 | if (column != -1 || page_addr != -1) { |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 376 | au1550_hwcontrol(mtd, NAND_CTL_SETALE); |
Sergei Shtylyov | 35af68b | 2006-05-16 20:52:06 +0400 | [diff] [blame] | 377 | |
| 378 | /* Serially input address */ |
| 379 | if (column != -1) { |
| 380 | /* Adjust columns for 16 bit buswidth */ |
| 381 | if (this->options & NAND_BUSWIDTH_16) |
| 382 | column >>= 1; |
Thomas Gleixner | cad74f2 | 2006-05-23 23:28:48 +0200 | [diff] [blame^] | 383 | au1550_write_byte(mtd, column); |
Sergei Shtylyov | 35af68b | 2006-05-16 20:52:06 +0400 | [diff] [blame] | 384 | } |
| 385 | if (page_addr != -1) { |
Thomas Gleixner | cad74f2 | 2006-05-23 23:28:48 +0200 | [diff] [blame^] | 386 | au1550_write_byte(mtd, (u8)(page_addr & 0xff)); |
Sergei Shtylyov | 35af68b | 2006-05-16 20:52:06 +0400 | [diff] [blame] | 387 | |
| 388 | if (command == NAND_CMD_READ0 || |
| 389 | command == NAND_CMD_READ1 || |
| 390 | command == NAND_CMD_READOOB) { |
| 391 | /* |
| 392 | * NAND controller will release -CE after |
| 393 | * the last address byte is written, so we'll |
| 394 | * have to forcibly assert it. No interrupts |
| 395 | * are allowed while we do this as we don't |
| 396 | * want the NOR flash or PCMCIA drivers to |
| 397 | * steal our precious bytes of data... |
| 398 | */ |
| 399 | ce_override = 1; |
| 400 | local_irq_save(flags); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 401 | au1550_hwcontrol(mtd, NAND_CTL_SETNCE); |
Sergei Shtylyov | 35af68b | 2006-05-16 20:52:06 +0400 | [diff] [blame] | 402 | } |
| 403 | |
Thomas Gleixner | cad74f2 | 2006-05-23 23:28:48 +0200 | [diff] [blame^] | 404 | au1550_write_byte(mtd, (u8)(page_addr >> 8)); |
Sergei Shtylyov | 35af68b | 2006-05-16 20:52:06 +0400 | [diff] [blame] | 405 | |
| 406 | /* One more address cycle for devices > 32MiB */ |
| 407 | if (this->chipsize > (32 << 20)) |
Thomas Gleixner | cad74f2 | 2006-05-23 23:28:48 +0200 | [diff] [blame^] | 408 | au1550_write_byte(mtd, (u8)((page_addr >> 16) & 0x0f)); |
Sergei Shtylyov | 35af68b | 2006-05-16 20:52:06 +0400 | [diff] [blame] | 409 | } |
| 410 | /* Latch in address */ |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 411 | au1550_hwcontrol(mtd, NAND_CTL_CLRALE); |
Sergei Shtylyov | 35af68b | 2006-05-16 20:52:06 +0400 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | /* |
| 415 | * Program and erase have their own busy handlers. |
| 416 | * Status and sequential in need no delay. |
| 417 | */ |
| 418 | switch (command) { |
| 419 | |
| 420 | case NAND_CMD_PAGEPROG: |
| 421 | case NAND_CMD_ERASE1: |
| 422 | case NAND_CMD_ERASE2: |
| 423 | case NAND_CMD_SEQIN: |
| 424 | case NAND_CMD_STATUS: |
| 425 | return; |
| 426 | |
| 427 | case NAND_CMD_RESET: |
| 428 | break; |
| 429 | |
| 430 | case NAND_CMD_READ0: |
| 431 | case NAND_CMD_READ1: |
| 432 | case NAND_CMD_READOOB: |
| 433 | /* Check if we're really driving -CE low (just in case) */ |
| 434 | if (unlikely(!ce_override)) |
| 435 | break; |
| 436 | |
| 437 | /* Apply a short delay always to ensure that we do wait tWB. */ |
| 438 | ndelay(100); |
| 439 | /* Wait for a chip to become ready... */ |
| 440 | for (i = this->chip_delay; !this->dev_ready(mtd) && i > 0; --i) |
| 441 | udelay(1); |
| 442 | |
| 443 | /* Release -CE and re-enable interrupts. */ |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 444 | au1550_hwcontrol(mtd, NAND_CTL_CLRNCE); |
Sergei Shtylyov | 35af68b | 2006-05-16 20:52:06 +0400 | [diff] [blame] | 445 | local_irq_restore(flags); |
| 446 | return; |
| 447 | } |
| 448 | /* Apply this short delay always to ensure that we do wait tWB. */ |
| 449 | ndelay(100); |
| 450 | |
| 451 | while(!this->dev_ready(mtd)); |
| 452 | } |
| 453 | |
| 454 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | /* |
| 456 | * Main initialization routine |
| 457 | */ |
David Woodhouse | cead4db | 2006-05-16 13:54:50 +0100 | [diff] [blame] | 458 | static int __init au1xxx_nand_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | { |
| 460 | struct nand_chip *this; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 461 | u16 boot_swapboot = 0; /* default value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | int retval; |
Pete Popov | ef6f0d1 | 2005-09-23 02:44:58 +0100 | [diff] [blame] | 463 | u32 mem_staddr; |
| 464 | u32 nand_phys; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | |
| 466 | /* Allocate memory for MTD device structure and private data */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 467 | au1550_mtd = kmalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | if (!au1550_mtd) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 469 | printk("Unable to allocate NAND MTD dev structure.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | return -ENOMEM; |
| 471 | } |
| 472 | |
| 473 | /* Get pointer to private data */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 474 | this = (struct nand_chip *)(&au1550_mtd[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | |
| 476 | /* Initialize structures */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 477 | memset(au1550_mtd, 0, sizeof(struct mtd_info)); |
| 478 | memset(this, 0, sizeof(struct nand_chip)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | |
| 480 | /* Link the private data with the MTD structure */ |
| 481 | au1550_mtd->priv = this; |
David Woodhouse | 552d920 | 2006-05-14 01:20:46 +0100 | [diff] [blame] | 482 | au1550_mtd->owner = THIS_MODULE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 484 | |
Sergei Shtylyov | 155285c | 2006-05-16 20:16:41 +0400 | [diff] [blame] | 485 | /* MEM_STNDCTL: disable ints, disable nand boot */ |
| 486 | au_writel(0, MEM_STNDCTL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | |
| 488 | #ifdef CONFIG_MIPS_PB1550 |
| 489 | /* set gpio206 high */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 490 | au_writel(au_readl(GPIO2_DIR) & ~(1 << 6), GPIO2_DIR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 492 | boot_swapboot = (au_readl(MEM_STSTAT) & (0x7 << 1)) | ((bcsr->status >> 6) & 0x1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | switch (boot_swapboot) { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 494 | case 0: |
| 495 | case 2: |
| 496 | case 8: |
| 497 | case 0xC: |
| 498 | case 0xD: |
| 499 | /* x16 NAND Flash */ |
| 500 | nand_width = 0; |
| 501 | break; |
| 502 | case 1: |
| 503 | case 9: |
| 504 | case 3: |
| 505 | case 0xE: |
| 506 | case 0xF: |
| 507 | /* x8 NAND Flash */ |
| 508 | nand_width = 1; |
| 509 | break; |
| 510 | default: |
| 511 | printk("Pb1550 NAND: bad boot:swap\n"); |
| 512 | retval = -EINVAL; |
| 513 | goto outmem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | } |
| 515 | #endif |
| 516 | |
Pete Popov | ef6f0d1 | 2005-09-23 02:44:58 +0100 | [diff] [blame] | 517 | /* Configure chip-select; normally done by boot code, e.g. YAMON */ |
| 518 | #ifdef NAND_STCFG |
| 519 | if (NAND_CS == 0) { |
| 520 | au_writel(NAND_STCFG, MEM_STCFG0); |
| 521 | au_writel(NAND_STTIME, MEM_STTIME0); |
| 522 | au_writel(NAND_STADDR, MEM_STADDR0); |
| 523 | } |
| 524 | if (NAND_CS == 1) { |
| 525 | au_writel(NAND_STCFG, MEM_STCFG1); |
| 526 | au_writel(NAND_STTIME, MEM_STTIME1); |
| 527 | au_writel(NAND_STADDR, MEM_STADDR1); |
| 528 | } |
| 529 | if (NAND_CS == 2) { |
| 530 | au_writel(NAND_STCFG, MEM_STCFG2); |
| 531 | au_writel(NAND_STTIME, MEM_STTIME2); |
| 532 | au_writel(NAND_STADDR, MEM_STADDR2); |
| 533 | } |
| 534 | if (NAND_CS == 3) { |
| 535 | au_writel(NAND_STCFG, MEM_STCFG3); |
| 536 | au_writel(NAND_STTIME, MEM_STTIME3); |
| 537 | au_writel(NAND_STADDR, MEM_STADDR3); |
| 538 | } |
| 539 | #endif |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 540 | |
Pete Popov | ef6f0d1 | 2005-09-23 02:44:58 +0100 | [diff] [blame] | 541 | /* Locate NAND chip-select in order to determine NAND phys address */ |
| 542 | mem_staddr = 0x00000000; |
| 543 | if (((au_readl(MEM_STCFG0) & 0x7) == 0x5) && (NAND_CS == 0)) |
| 544 | mem_staddr = au_readl(MEM_STADDR0); |
| 545 | else if (((au_readl(MEM_STCFG1) & 0x7) == 0x5) && (NAND_CS == 1)) |
| 546 | mem_staddr = au_readl(MEM_STADDR1); |
| 547 | else if (((au_readl(MEM_STCFG2) & 0x7) == 0x5) && (NAND_CS == 2)) |
| 548 | mem_staddr = au_readl(MEM_STADDR2); |
| 549 | else if (((au_readl(MEM_STCFG3) & 0x7) == 0x5) && (NAND_CS == 3)) |
| 550 | mem_staddr = au_readl(MEM_STADDR3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | |
Pete Popov | ef6f0d1 | 2005-09-23 02:44:58 +0100 | [diff] [blame] | 552 | if (mem_staddr == 0x00000000) { |
| 553 | printk("Au1xxx NAND: ERROR WITH NAND CHIP-SELECT\n"); |
| 554 | kfree(au1550_mtd); |
| 555 | return 1; |
| 556 | } |
| 557 | nand_phys = (mem_staddr << 4) & 0xFFFC0000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | |
Pete Popov | ef6f0d1 | 2005-09-23 02:44:58 +0100 | [diff] [blame] | 559 | p_nand = (void __iomem *)ioremap(nand_phys, 0x1000); |
| 560 | |
| 561 | /* make controller and MTD agree */ |
| 562 | if (NAND_CS == 0) |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 563 | nand_width = au_readl(MEM_STCFG0) & (1 << 22); |
Pete Popov | ef6f0d1 | 2005-09-23 02:44:58 +0100 | [diff] [blame] | 564 | if (NAND_CS == 1) |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 565 | nand_width = au_readl(MEM_STCFG1) & (1 << 22); |
Pete Popov | ef6f0d1 | 2005-09-23 02:44:58 +0100 | [diff] [blame] | 566 | if (NAND_CS == 2) |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 567 | nand_width = au_readl(MEM_STCFG2) & (1 << 22); |
Pete Popov | ef6f0d1 | 2005-09-23 02:44:58 +0100 | [diff] [blame] | 568 | if (NAND_CS == 3) |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 569 | nand_width = au_readl(MEM_STCFG3) & (1 << 22); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | |
| 571 | /* Set address of hardware control function */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | this->dev_ready = au1550_device_ready; |
Sergei Shtylyov | 35af68b | 2006-05-16 20:52:06 +0400 | [diff] [blame] | 573 | this->select_chip = au1550_select_chip; |
| 574 | this->cmdfunc = au1550_command; |
| 575 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | /* 30 us command delay time */ |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 577 | this->chip_delay = 30; |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 578 | this->ecc.mode = NAND_ECC_SOFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | |
| 580 | this->options = NAND_NO_AUTOINCR; |
| 581 | |
| 582 | if (!nand_width) |
| 583 | this->options |= NAND_BUSWIDTH_16; |
| 584 | |
| 585 | this->read_byte = (!nand_width) ? au_read_byte16 : au_read_byte; |
Thomas Gleixner | cad74f2 | 2006-05-23 23:28:48 +0200 | [diff] [blame^] | 586 | au1550_write_byte = (!nand_width) ? au_write_byte16 : au_write_byte; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | this->read_word = au_read_word; |
| 588 | this->write_buf = (!nand_width) ? au_write_buf16 : au_write_buf; |
| 589 | this->read_buf = (!nand_width) ? au_read_buf16 : au_read_buf; |
| 590 | this->verify_buf = (!nand_width) ? au_verify_buf16 : au_verify_buf; |
| 591 | |
| 592 | /* Scan to find existence of the device */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 593 | if (nand_scan(au1550_mtd, 1)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | retval = -ENXIO; |
| 595 | goto outio; |
| 596 | } |
| 597 | |
| 598 | /* Register the partitions */ |
Tobias Klauser | 87d10f3 | 2006-03-31 02:29:45 -0800 | [diff] [blame] | 599 | add_mtd_partitions(au1550_mtd, partition_info, ARRAY_SIZE(partition_info)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | |
| 601 | return 0; |
| 602 | |
| 603 | outio: |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 604 | iounmap((void *)p_nand); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 605 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | outmem: |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 607 | kfree(au1550_mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | return retval; |
| 609 | } |
| 610 | |
Pete Popov | ef6f0d1 | 2005-09-23 02:44:58 +0100 | [diff] [blame] | 611 | module_init(au1xxx_nand_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | |
| 613 | /* |
| 614 | * Clean up routine |
| 615 | */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 616 | static void __exit au1550_cleanup(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | { |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 618 | struct nand_chip *this = (struct nand_chip *)&au1550_mtd[1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | |
| 620 | /* Release resources, unregister device */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 621 | nand_release(au1550_mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | |
| 623 | /* Free the MTD device structure */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 624 | kfree(au1550_mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | |
| 626 | /* Unmap */ |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 627 | iounmap((void *)p_nand); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | } |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 629 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | module_exit(au1550_cleanup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | |
| 632 | MODULE_LICENSE("GPL"); |
| 633 | MODULE_AUTHOR("Embedded Edge, LLC"); |
| 634 | MODULE_DESCRIPTION("Board-specific glue layer for NAND flash on Pb1550 board"); |