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 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | */ |
| 11 | |
| 12 | #include <linux/slab.h> |
Manuel Lauss | b7f720d | 2011-05-08 10:42:20 +0200 | [diff] [blame] | 13 | #include <linux/gpio.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/init.h> |
| 15 | #include <linux/module.h> |
Sergei Shtylyov | 35af68b | 2006-05-16 20:52:06 +0400 | [diff] [blame] | 16 | #include <linux/interrupt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/mtd/mtd.h> |
| 18 | #include <linux/mtd/nand.h> |
| 19 | #include <linux/mtd/partitions.h> |
Manuel Lauss | b67a1a0 | 2011-12-08 10:42:10 +0000 | [diff] [blame] | 20 | #include <linux/platform_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <asm/io.h> |
Manuel Lauss | b67a1a0 | 2011-12-08 10:42:10 +0000 | [diff] [blame] | 22 | #include <asm/mach-au1x00/au1000.h> |
| 23 | #include <asm/mach-au1x00/au1550nd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Manuel Lauss | b67a1a0 | 2011-12-08 10:42:10 +0000 | [diff] [blame] | 26 | struct au1550nd_ctx { |
| 27 | struct mtd_info info; |
| 28 | struct nand_chip chip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
Manuel Lauss | b67a1a0 | 2011-12-08 10:42:10 +0000 | [diff] [blame] | 30 | int cs; |
| 31 | void __iomem *base; |
| 32 | void (*write_byte)(struct mtd_info *, u_char); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * au_read_byte - read one byte from the chip |
| 37 | * @mtd: MTD device structure |
| 38 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 39 | * read function for 8bit buswidth |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | */ |
| 41 | static u_char au_read_byte(struct mtd_info *mtd) |
| 42 | { |
| 43 | struct nand_chip *this = mtd->priv; |
| 44 | u_char ret = readb(this->IO_ADDR_R); |
| 45 | au_sync(); |
| 46 | return ret; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * au_write_byte - write one byte to the chip |
| 51 | * @mtd: MTD device structure |
| 52 | * @byte: pointer to data byte to write |
| 53 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 54 | * write function for 8it buswidth |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | */ |
| 56 | static void au_write_byte(struct mtd_info *mtd, u_char byte) |
| 57 | { |
| 58 | struct nand_chip *this = mtd->priv; |
| 59 | writeb(byte, this->IO_ADDR_W); |
| 60 | au_sync(); |
| 61 | } |
| 62 | |
| 63 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 64 | * au_read_byte16 - read one byte endianness aware from the chip |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | * @mtd: MTD device structure |
| 66 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 67 | * read function for 16bit buswidth with endianness conversion |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | */ |
| 69 | static u_char au_read_byte16(struct mtd_info *mtd) |
| 70 | { |
| 71 | struct nand_chip *this = mtd->priv; |
| 72 | u_char ret = (u_char) cpu_to_le16(readw(this->IO_ADDR_R)); |
| 73 | au_sync(); |
| 74 | return ret; |
| 75 | } |
| 76 | |
| 77 | /** |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 78 | * au_write_byte16 - write one byte endianness aware to the chip |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | * @mtd: MTD device structure |
| 80 | * @byte: pointer to data byte to write |
| 81 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 82 | * write function for 16bit buswidth with endianness conversion |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | */ |
| 84 | static void au_write_byte16(struct mtd_info *mtd, u_char byte) |
| 85 | { |
| 86 | struct nand_chip *this = mtd->priv; |
| 87 | writew(le16_to_cpu((u16) byte), this->IO_ADDR_W); |
| 88 | au_sync(); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * au_read_word - read one word from the chip |
| 93 | * @mtd: MTD device structure |
| 94 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 95 | * read function for 16bit buswidth without endianness conversion |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | */ |
| 97 | static u16 au_read_word(struct mtd_info *mtd) |
| 98 | { |
| 99 | struct nand_chip *this = mtd->priv; |
| 100 | u16 ret = readw(this->IO_ADDR_R); |
| 101 | au_sync(); |
| 102 | return ret; |
| 103 | } |
| 104 | |
| 105 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | * au_write_buf - write buffer to chip |
| 107 | * @mtd: MTD device structure |
| 108 | * @buf: data buffer |
| 109 | * @len: number of bytes to write |
| 110 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 111 | * write function for 8bit buswidth |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | */ |
| 113 | static void au_write_buf(struct mtd_info *mtd, const u_char *buf, int len) |
| 114 | { |
| 115 | int i; |
| 116 | struct nand_chip *this = mtd->priv; |
| 117 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 118 | for (i = 0; i < len; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | writeb(buf[i], this->IO_ADDR_W); |
| 120 | au_sync(); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /** |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 125 | * au_read_buf - read chip data into buffer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | * @mtd: MTD device structure |
| 127 | * @buf: buffer to store date |
| 128 | * @len: number of bytes to read |
| 129 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 130 | * read function for 8bit buswidth |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | */ |
| 132 | static void au_read_buf(struct mtd_info *mtd, u_char *buf, int len) |
| 133 | { |
| 134 | int i; |
| 135 | struct nand_chip *this = mtd->priv; |
| 136 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 137 | for (i = 0; i < len; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | buf[i] = readb(this->IO_ADDR_R); |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 139 | au_sync(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
| 143 | /** |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 144 | * au_verify_buf - Verify chip data against buffer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | * @mtd: MTD device structure |
| 146 | * @buf: buffer containing the data to compare |
| 147 | * @len: number of bytes to compare |
| 148 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 149 | * verify function for 8bit buswidth |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | */ |
| 151 | static int au_verify_buf(struct mtd_info *mtd, const u_char *buf, int len) |
| 152 | { |
| 153 | int i; |
| 154 | struct nand_chip *this = mtd->priv; |
| 155 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 156 | for (i = 0; i < len; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | if (buf[i] != readb(this->IO_ADDR_R)) |
| 158 | return -EFAULT; |
| 159 | au_sync(); |
| 160 | } |
| 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * au_write_buf16 - write buffer to chip |
| 167 | * @mtd: MTD device structure |
| 168 | * @buf: data buffer |
| 169 | * @len: number of bytes to write |
| 170 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 171 | * write function for 16bit buswidth |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | */ |
| 173 | static void au_write_buf16(struct mtd_info *mtd, const u_char *buf, int len) |
| 174 | { |
| 175 | int i; |
| 176 | struct nand_chip *this = mtd->priv; |
| 177 | u16 *p = (u16 *) buf; |
| 178 | len >>= 1; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 179 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 180 | for (i = 0; i < len; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | writew(p[i], this->IO_ADDR_W); |
| 182 | au_sync(); |
| 183 | } |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | /** |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 188 | * au_read_buf16 - read chip data into buffer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | * @mtd: MTD device structure |
| 190 | * @buf: buffer to store date |
| 191 | * @len: number of bytes to read |
| 192 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 193 | * read function for 16bit buswidth |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | */ |
| 195 | static void au_read_buf16(struct mtd_info *mtd, u_char *buf, int len) |
| 196 | { |
| 197 | int i; |
| 198 | struct nand_chip *this = mtd->priv; |
| 199 | u16 *p = (u16 *) buf; |
| 200 | len >>= 1; |
| 201 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 202 | for (i = 0; i < len; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | p[i] = readw(this->IO_ADDR_R); |
| 204 | au_sync(); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /** |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 209 | * au_verify_buf16 - Verify chip data against buffer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | * @mtd: MTD device structure |
| 211 | * @buf: buffer containing the data to compare |
| 212 | * @len: number of bytes to compare |
| 213 | * |
Brian Norris | 7854d3f | 2011-06-23 14:12:08 -0700 | [diff] [blame] | 214 | * verify function for 16bit buswidth |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | */ |
| 216 | static int au_verify_buf16(struct mtd_info *mtd, const u_char *buf, int len) |
| 217 | { |
| 218 | int i; |
| 219 | struct nand_chip *this = mtd->priv; |
| 220 | u16 *p = (u16 *) buf; |
| 221 | len >>= 1; |
| 222 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 223 | for (i = 0; i < len; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | if (p[i] != readw(this->IO_ADDR_R)) |
| 225 | return -EFAULT; |
| 226 | au_sync(); |
| 227 | } |
| 228 | return 0; |
| 229 | } |
| 230 | |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 231 | /* Select the chip by setting nCE to low */ |
| 232 | #define NAND_CTL_SETNCE 1 |
| 233 | /* Deselect the chip by setting nCE to high */ |
| 234 | #define NAND_CTL_CLRNCE 2 |
| 235 | /* Select the command latch by setting CLE to high */ |
| 236 | #define NAND_CTL_SETCLE 3 |
| 237 | /* Deselect the command latch by setting CLE to low */ |
| 238 | #define NAND_CTL_CLRCLE 4 |
| 239 | /* Select the address latch by setting ALE to high */ |
| 240 | #define NAND_CTL_SETALE 5 |
| 241 | /* Deselect the address latch by setting ALE to low */ |
| 242 | #define NAND_CTL_CLRALE 6 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
| 244 | static void au1550_hwcontrol(struct mtd_info *mtd, int cmd) |
| 245 | { |
Manuel Lauss | b67a1a0 | 2011-12-08 10:42:10 +0000 | [diff] [blame] | 246 | struct au1550nd_ctx *ctx = container_of(mtd, struct au1550nd_ctx, info); |
| 247 | struct nand_chip *this = mtd->priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 249 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 251 | case NAND_CTL_SETCLE: |
Manuel Lauss | b67a1a0 | 2011-12-08 10:42:10 +0000 | [diff] [blame] | 252 | this->IO_ADDR_W = ctx->base + MEM_STNAND_CMD; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 253 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 255 | case NAND_CTL_CLRCLE: |
Manuel Lauss | b67a1a0 | 2011-12-08 10:42:10 +0000 | [diff] [blame] | 256 | this->IO_ADDR_W = ctx->base + MEM_STNAND_DATA; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 257 | break; |
| 258 | |
| 259 | case NAND_CTL_SETALE: |
Manuel Lauss | b67a1a0 | 2011-12-08 10:42:10 +0000 | [diff] [blame] | 260 | this->IO_ADDR_W = ctx->base + MEM_STNAND_ADDR; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 261 | break; |
| 262 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 263 | case NAND_CTL_CLRALE: |
Manuel Lauss | b67a1a0 | 2011-12-08 10:42:10 +0000 | [diff] [blame] | 264 | this->IO_ADDR_W = ctx->base + MEM_STNAND_DATA; |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 265 | /* FIXME: Nobody knows why this is necessary, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | * but it works only that way */ |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 267 | udelay(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | break; |
| 269 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 270 | case NAND_CTL_SETNCE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | /* assert (force assert) chip enable */ |
Manuel Lauss | b67a1a0 | 2011-12-08 10:42:10 +0000 | [diff] [blame] | 272 | au_writel((1 << (4 + ctx->cs)), MEM_STNDCTL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | break; |
| 274 | |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 275 | case NAND_CTL_CLRNCE: |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 276 | /* deassert chip enable */ |
| 277 | au_writel(0, MEM_STNDCTL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | break; |
| 279 | } |
| 280 | |
| 281 | this->IO_ADDR_R = this->IO_ADDR_W; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | /* Drain the writebuffer */ |
| 284 | au_sync(); |
| 285 | } |
| 286 | |
| 287 | int au1550_device_ready(struct mtd_info *mtd) |
| 288 | { |
| 289 | int ret = (au_readl(MEM_STSTAT) & 0x1) ? 1 : 0; |
| 290 | au_sync(); |
| 291 | return ret; |
| 292 | } |
| 293 | |
Sergei Shtylyov | 35af68b | 2006-05-16 20:52:06 +0400 | [diff] [blame] | 294 | /** |
| 295 | * au1550_select_chip - control -CE line |
| 296 | * Forbid driving -CE manually permitting the NAND controller to do this. |
| 297 | * Keeping -CE asserted during the whole sector reads interferes with the |
| 298 | * NOR flash and PCMCIA drivers as it causes contention on the static bus. |
| 299 | * We only have to hold -CE low for the NAND read commands since the flash |
| 300 | * chip needs it to be asserted during chip not ready time but the NAND |
| 301 | * controller keeps it released. |
| 302 | * |
| 303 | * @mtd: MTD device structure |
| 304 | * @chip: chipnumber to select, -1 for deselect |
| 305 | */ |
| 306 | static void au1550_select_chip(struct mtd_info *mtd, int chip) |
| 307 | { |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * au1550_command - Send command to NAND device |
| 312 | * @mtd: MTD device structure |
| 313 | * @command: the command to be sent |
| 314 | * @column: the column address for this command, -1 if none |
| 315 | * @page_addr: the page address for this command, -1 if none |
| 316 | */ |
| 317 | static void au1550_command(struct mtd_info *mtd, unsigned command, int column, int page_addr) |
| 318 | { |
Manuel Lauss | b67a1a0 | 2011-12-08 10:42:10 +0000 | [diff] [blame] | 319 | struct au1550nd_ctx *ctx = container_of(mtd, struct au1550nd_ctx, info); |
| 320 | struct nand_chip *this = mtd->priv; |
Sergei Shtylyov | 35af68b | 2006-05-16 20:52:06 +0400 | [diff] [blame] | 321 | int ce_override = 0, i; |
Manuel Lauss | b67a1a0 | 2011-12-08 10:42:10 +0000 | [diff] [blame] | 322 | unsigned long flags = 0; |
Sergei Shtylyov | 35af68b | 2006-05-16 20:52:06 +0400 | [diff] [blame] | 323 | |
| 324 | /* Begin command latch cycle */ |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 325 | au1550_hwcontrol(mtd, NAND_CTL_SETCLE); |
Sergei Shtylyov | 35af68b | 2006-05-16 20:52:06 +0400 | [diff] [blame] | 326 | /* |
| 327 | * Write out the command to the device. |
| 328 | */ |
| 329 | if (command == NAND_CMD_SEQIN) { |
| 330 | int readcmd; |
| 331 | |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 332 | if (column >= mtd->writesize) { |
Sergei Shtylyov | 35af68b | 2006-05-16 20:52:06 +0400 | [diff] [blame] | 333 | /* OOB area */ |
Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 334 | column -= mtd->writesize; |
Sergei Shtylyov | 35af68b | 2006-05-16 20:52:06 +0400 | [diff] [blame] | 335 | readcmd = NAND_CMD_READOOB; |
| 336 | } else if (column < 256) { |
| 337 | /* First 256 bytes --> READ0 */ |
| 338 | readcmd = NAND_CMD_READ0; |
| 339 | } else { |
| 340 | column -= 256; |
| 341 | readcmd = NAND_CMD_READ1; |
| 342 | } |
Manuel Lauss | b67a1a0 | 2011-12-08 10:42:10 +0000 | [diff] [blame] | 343 | ctx->write_byte(mtd, readcmd); |
Sergei Shtylyov | 35af68b | 2006-05-16 20:52:06 +0400 | [diff] [blame] | 344 | } |
Manuel Lauss | b67a1a0 | 2011-12-08 10:42:10 +0000 | [diff] [blame] | 345 | ctx->write_byte(mtd, command); |
Sergei Shtylyov | 35af68b | 2006-05-16 20:52:06 +0400 | [diff] [blame] | 346 | |
| 347 | /* Set ALE and clear CLE to start address cycle */ |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 348 | au1550_hwcontrol(mtd, NAND_CTL_CLRCLE); |
Sergei Shtylyov | 35af68b | 2006-05-16 20:52:06 +0400 | [diff] [blame] | 349 | |
| 350 | if (column != -1 || page_addr != -1) { |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 351 | au1550_hwcontrol(mtd, NAND_CTL_SETALE); |
Sergei Shtylyov | 35af68b | 2006-05-16 20:52:06 +0400 | [diff] [blame] | 352 | |
| 353 | /* Serially input address */ |
| 354 | if (column != -1) { |
| 355 | /* Adjust columns for 16 bit buswidth */ |
| 356 | if (this->options & NAND_BUSWIDTH_16) |
| 357 | column >>= 1; |
Manuel Lauss | b67a1a0 | 2011-12-08 10:42:10 +0000 | [diff] [blame] | 358 | ctx->write_byte(mtd, column); |
Sergei Shtylyov | 35af68b | 2006-05-16 20:52:06 +0400 | [diff] [blame] | 359 | } |
| 360 | if (page_addr != -1) { |
Manuel Lauss | b67a1a0 | 2011-12-08 10:42:10 +0000 | [diff] [blame] | 361 | ctx->write_byte(mtd, (u8)(page_addr & 0xff)); |
Sergei Shtylyov | 35af68b | 2006-05-16 20:52:06 +0400 | [diff] [blame] | 362 | |
| 363 | if (command == NAND_CMD_READ0 || |
| 364 | command == NAND_CMD_READ1 || |
| 365 | command == NAND_CMD_READOOB) { |
| 366 | /* |
| 367 | * NAND controller will release -CE after |
| 368 | * the last address byte is written, so we'll |
| 369 | * have to forcibly assert it. No interrupts |
| 370 | * are allowed while we do this as we don't |
| 371 | * want the NOR flash or PCMCIA drivers to |
| 372 | * steal our precious bytes of data... |
| 373 | */ |
| 374 | ce_override = 1; |
| 375 | local_irq_save(flags); |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 376 | au1550_hwcontrol(mtd, NAND_CTL_SETNCE); |
Sergei Shtylyov | 35af68b | 2006-05-16 20:52:06 +0400 | [diff] [blame] | 377 | } |
| 378 | |
Manuel Lauss | b67a1a0 | 2011-12-08 10:42:10 +0000 | [diff] [blame] | 379 | ctx->write_byte(mtd, (u8)(page_addr >> 8)); |
Sergei Shtylyov | 35af68b | 2006-05-16 20:52:06 +0400 | [diff] [blame] | 380 | |
| 381 | /* One more address cycle for devices > 32MiB */ |
| 382 | if (this->chipsize > (32 << 20)) |
Manuel Lauss | b67a1a0 | 2011-12-08 10:42:10 +0000 | [diff] [blame] | 383 | ctx->write_byte(mtd, |
| 384 | ((page_addr >> 16) & 0x0f)); |
Sergei Shtylyov | 35af68b | 2006-05-16 20:52:06 +0400 | [diff] [blame] | 385 | } |
| 386 | /* Latch in address */ |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 387 | au1550_hwcontrol(mtd, NAND_CTL_CLRALE); |
Sergei Shtylyov | 35af68b | 2006-05-16 20:52:06 +0400 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | /* |
| 391 | * Program and erase have their own busy handlers. |
| 392 | * Status and sequential in need no delay. |
| 393 | */ |
| 394 | switch (command) { |
| 395 | |
| 396 | case NAND_CMD_PAGEPROG: |
| 397 | case NAND_CMD_ERASE1: |
| 398 | case NAND_CMD_ERASE2: |
| 399 | case NAND_CMD_SEQIN: |
| 400 | case NAND_CMD_STATUS: |
| 401 | return; |
| 402 | |
| 403 | case NAND_CMD_RESET: |
| 404 | break; |
| 405 | |
| 406 | case NAND_CMD_READ0: |
| 407 | case NAND_CMD_READ1: |
| 408 | case NAND_CMD_READOOB: |
| 409 | /* Check if we're really driving -CE low (just in case) */ |
| 410 | if (unlikely(!ce_override)) |
| 411 | break; |
| 412 | |
| 413 | /* Apply a short delay always to ensure that we do wait tWB. */ |
| 414 | ndelay(100); |
| 415 | /* Wait for a chip to become ready... */ |
| 416 | for (i = this->chip_delay; !this->dev_ready(mtd) && i > 0; --i) |
| 417 | udelay(1); |
| 418 | |
| 419 | /* Release -CE and re-enable interrupts. */ |
Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 420 | au1550_hwcontrol(mtd, NAND_CTL_CLRNCE); |
Sergei Shtylyov | 35af68b | 2006-05-16 20:52:06 +0400 | [diff] [blame] | 421 | local_irq_restore(flags); |
| 422 | return; |
| 423 | } |
| 424 | /* Apply this short delay always to ensure that we do wait tWB. */ |
| 425 | ndelay(100); |
| 426 | |
| 427 | while(!this->dev_ready(mtd)); |
| 428 | } |
| 429 | |
Manuel Lauss | b67a1a0 | 2011-12-08 10:42:10 +0000 | [diff] [blame] | 430 | static int __devinit find_nand_cs(unsigned long nand_base) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | { |
Manuel Lauss | b67a1a0 | 2011-12-08 10:42:10 +0000 | [diff] [blame] | 432 | void __iomem *base = |
| 433 | (void __iomem *)KSEG1ADDR(AU1000_STATIC_MEM_PHYS_ADDR); |
| 434 | unsigned long addr, staddr, start, mask, end; |
| 435 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | |
Manuel Lauss | b67a1a0 | 2011-12-08 10:42:10 +0000 | [diff] [blame] | 437 | for (i = 0; i < 4; i++) { |
| 438 | addr = 0x1000 + (i * 0x10); /* CSx */ |
| 439 | staddr = __raw_readl(base + addr + 0x08); /* STADDRx */ |
| 440 | /* figure out the decoded range of this CS */ |
| 441 | start = (staddr << 4) & 0xfffc0000; |
| 442 | mask = (staddr << 18) & 0xfffc0000; |
| 443 | end = (start | (start - 1)) & ~(start ^ mask); |
| 444 | if ((nand_base >= start) && (nand_base < end)) |
| 445 | return i; |
| 446 | } |
| 447 | |
| 448 | return -ENODEV; |
| 449 | } |
| 450 | |
| 451 | static int __devinit au1550nd_probe(struct platform_device *pdev) |
| 452 | { |
| 453 | struct au1550nd_platdata *pd; |
| 454 | struct au1550nd_ctx *ctx; |
| 455 | struct nand_chip *this; |
| 456 | struct resource *r; |
| 457 | int ret, cs; |
| 458 | |
| 459 | pd = pdev->dev.platform_data; |
| 460 | if (!pd) { |
| 461 | dev_err(&pdev->dev, "missing platform data\n"); |
| 462 | return -ENODEV; |
| 463 | } |
| 464 | |
| 465 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 466 | if (!ctx) { |
| 467 | dev_err(&pdev->dev, "no memory for NAND context\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | return -ENOMEM; |
| 469 | } |
| 470 | |
Manuel Lauss | b67a1a0 | 2011-12-08 10:42:10 +0000 | [diff] [blame] | 471 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 472 | if (!r) { |
| 473 | dev_err(&pdev->dev, "no NAND memory resource\n"); |
| 474 | ret = -ENODEV; |
| 475 | goto out1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | } |
Manuel Lauss | b67a1a0 | 2011-12-08 10:42:10 +0000 | [diff] [blame] | 477 | if (request_mem_region(r->start, resource_size(r), "au1550-nand")) { |
| 478 | dev_err(&pdev->dev, "cannot claim NAND memory area\n"); |
| 479 | ret = -ENOMEM; |
| 480 | goto out1; |
Pete Popov | ef6f0d1 | 2005-09-23 02:44:58 +0100 | [diff] [blame] | 481 | } |
Manuel Lauss | b67a1a0 | 2011-12-08 10:42:10 +0000 | [diff] [blame] | 482 | |
| 483 | ctx->base = ioremap_nocache(r->start, 0x1000); |
| 484 | if (!ctx->base) { |
| 485 | dev_err(&pdev->dev, "cannot remap NAND memory area\n"); |
| 486 | ret = -ENODEV; |
| 487 | goto out2; |
Pete Popov | ef6f0d1 | 2005-09-23 02:44:58 +0100 | [diff] [blame] | 488 | } |
Manuel Lauss | b67a1a0 | 2011-12-08 10:42:10 +0000 | [diff] [blame] | 489 | |
| 490 | this = &ctx->chip; |
| 491 | ctx->info.priv = this; |
| 492 | ctx->info.owner = THIS_MODULE; |
| 493 | |
| 494 | /* figure out which CS# r->start belongs to */ |
| 495 | cs = find_nand_cs(r->start); |
| 496 | if (cs < 0) { |
| 497 | dev_err(&pdev->dev, "cannot detect NAND chipselect\n"); |
| 498 | ret = -ENODEV; |
| 499 | goto out3; |
Pete Popov | ef6f0d1 | 2005-09-23 02:44:58 +0100 | [diff] [blame] | 500 | } |
Manuel Lauss | b67a1a0 | 2011-12-08 10:42:10 +0000 | [diff] [blame] | 501 | ctx->cs = cs; |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 502 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | this->dev_ready = au1550_device_ready; |
Sergei Shtylyov | 35af68b | 2006-05-16 20:52:06 +0400 | [diff] [blame] | 504 | this->select_chip = au1550_select_chip; |
| 505 | this->cmdfunc = au1550_command; |
| 506 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | /* 30 us command delay time */ |
Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 508 | this->chip_delay = 30; |
Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 509 | this->ecc.mode = NAND_ECC_SOFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | |
| 511 | this->options = NAND_NO_AUTOINCR; |
| 512 | |
Manuel Lauss | b67a1a0 | 2011-12-08 10:42:10 +0000 | [diff] [blame] | 513 | if (pd->devwidth) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | this->options |= NAND_BUSWIDTH_16; |
| 515 | |
Manuel Lauss | b67a1a0 | 2011-12-08 10:42:10 +0000 | [diff] [blame] | 516 | this->read_byte = (pd->devwidth) ? au_read_byte16 : au_read_byte; |
| 517 | ctx->write_byte = (pd->devwidth) ? au_write_byte16 : au_write_byte; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | this->read_word = au_read_word; |
Manuel Lauss | b67a1a0 | 2011-12-08 10:42:10 +0000 | [diff] [blame] | 519 | this->write_buf = (pd->devwidth) ? au_write_buf16 : au_write_buf; |
| 520 | this->read_buf = (pd->devwidth) ? au_read_buf16 : au_read_buf; |
| 521 | this->verify_buf = (pd->devwidth) ? au_verify_buf16 : au_verify_buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | |
Manuel Lauss | b67a1a0 | 2011-12-08 10:42:10 +0000 | [diff] [blame] | 523 | ret = nand_scan(&ctx->info, 1); |
| 524 | if (ret) { |
| 525 | dev_err(&pdev->dev, "NAND scan failed with %d\n", ret); |
| 526 | goto out3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Manuel Lauss | b67a1a0 | 2011-12-08 10:42:10 +0000 | [diff] [blame] | 529 | mtd_device_register(&ctx->info, pd->parts, pd->num_parts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | |
| 531 | return 0; |
| 532 | |
Manuel Lauss | b67a1a0 | 2011-12-08 10:42:10 +0000 | [diff] [blame] | 533 | out3: |
| 534 | iounmap(ctx->base); |
| 535 | out2: |
| 536 | release_mem_region(r->start, resource_size(r)); |
| 537 | out1: |
| 538 | kfree(ctx); |
| 539 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | } |
| 541 | |
Manuel Lauss | b67a1a0 | 2011-12-08 10:42:10 +0000 | [diff] [blame] | 542 | static int __devexit au1550nd_remove(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | { |
Manuel Lauss | b67a1a0 | 2011-12-08 10:42:10 +0000 | [diff] [blame] | 544 | struct au1550nd_ctx *ctx = platform_get_drvdata(pdev); |
| 545 | struct resource *r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | |
Manuel Lauss | b67a1a0 | 2011-12-08 10:42:10 +0000 | [diff] [blame] | 547 | nand_release(&ctx->info); |
| 548 | iounmap(ctx->base); |
| 549 | release_mem_region(r->start, 0x1000); |
| 550 | kfree(ctx); |
| 551 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | } |
David Woodhouse | e0c7d76 | 2006-05-13 18:07:53 +0100 | [diff] [blame] | 553 | |
Manuel Lauss | b67a1a0 | 2011-12-08 10:42:10 +0000 | [diff] [blame] | 554 | static struct platform_driver au1550nd_driver = { |
| 555 | .driver = { |
| 556 | .name = "au1550-nand", |
| 557 | .owner = THIS_MODULE, |
| 558 | }, |
| 559 | .probe = au1550nd_probe, |
| 560 | .remove = __devexit_p(au1550nd_remove), |
| 561 | }; |
| 562 | |
| 563 | module_platform_driver(au1550nd_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | |
| 565 | MODULE_LICENSE("GPL"); |
| 566 | MODULE_AUTHOR("Embedded Edge, LLC"); |
| 567 | MODULE_DESCRIPTION("Board-specific glue layer for NAND flash on Pb1550 board"); |