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