Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004-2007 Freescale Semiconductor, Inc. All Rights Reserved. |
| 3 | * Copyright 2008 Sascha Hauer, kernel@pengutronix.de |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; either version 2 |
| 8 | * of the License, or (at your option) any later version. |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 17 | * MA 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | #include <linux/delay.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/mtd/mtd.h> |
| 25 | #include <linux/mtd/nand.h> |
| 26 | #include <linux/mtd/partitions.h> |
| 27 | #include <linux/interrupt.h> |
| 28 | #include <linux/device.h> |
| 29 | #include <linux/platform_device.h> |
| 30 | #include <linux/clk.h> |
| 31 | #include <linux/err.h> |
| 32 | #include <linux/io.h> |
| 33 | |
| 34 | #include <asm/mach/flash.h> |
| 35 | #include <mach/mxc_nand.h> |
| 36 | |
| 37 | #define DRIVER_NAME "mxc_nand" |
| 38 | |
| 39 | /* Addresses for NFC registers */ |
| 40 | #define NFC_BUF_SIZE 0xE00 |
| 41 | #define NFC_BUF_ADDR 0xE04 |
| 42 | #define NFC_FLASH_ADDR 0xE06 |
| 43 | #define NFC_FLASH_CMD 0xE08 |
| 44 | #define NFC_CONFIG 0xE0A |
| 45 | #define NFC_ECC_STATUS_RESULT 0xE0C |
| 46 | #define NFC_RSLTMAIN_AREA 0xE0E |
| 47 | #define NFC_RSLTSPARE_AREA 0xE10 |
| 48 | #define NFC_WRPROT 0xE12 |
| 49 | #define NFC_UNLOCKSTART_BLKADDR 0xE14 |
| 50 | #define NFC_UNLOCKEND_BLKADDR 0xE16 |
| 51 | #define NFC_NF_WRPRST 0xE18 |
| 52 | #define NFC_CONFIG1 0xE1A |
| 53 | #define NFC_CONFIG2 0xE1C |
| 54 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 55 | /* Set INT to 0, FCMD to 1, rest to 0 in NFC_CONFIG2 Register |
| 56 | * for Command operation */ |
| 57 | #define NFC_CMD 0x1 |
| 58 | |
| 59 | /* Set INT to 0, FADD to 1, rest to 0 in NFC_CONFIG2 Register |
| 60 | * for Address operation */ |
| 61 | #define NFC_ADDR 0x2 |
| 62 | |
| 63 | /* Set INT to 0, FDI to 1, rest to 0 in NFC_CONFIG2 Register |
| 64 | * for Input operation */ |
| 65 | #define NFC_INPUT 0x4 |
| 66 | |
| 67 | /* Set INT to 0, FDO to 001, rest to 0 in NFC_CONFIG2 Register |
| 68 | * for Data Output operation */ |
| 69 | #define NFC_OUTPUT 0x8 |
| 70 | |
| 71 | /* Set INT to 0, FD0 to 010, rest to 0 in NFC_CONFIG2 Register |
| 72 | * for Read ID operation */ |
| 73 | #define NFC_ID 0x10 |
| 74 | |
| 75 | /* Set INT to 0, FDO to 100, rest to 0 in NFC_CONFIG2 Register |
| 76 | * for Read Status operation */ |
| 77 | #define NFC_STATUS 0x20 |
| 78 | |
| 79 | /* Set INT to 1, rest to 0 in NFC_CONFIG2 Register for Read |
| 80 | * Status operation */ |
| 81 | #define NFC_INT 0x8000 |
| 82 | |
| 83 | #define NFC_SP_EN (1 << 2) |
| 84 | #define NFC_ECC_EN (1 << 3) |
| 85 | #define NFC_INT_MSK (1 << 4) |
| 86 | #define NFC_BIG (1 << 5) |
| 87 | #define NFC_RST (1 << 6) |
| 88 | #define NFC_CE (1 << 7) |
| 89 | #define NFC_ONE_CYCLE (1 << 8) |
| 90 | |
| 91 | struct mxc_nand_host { |
| 92 | struct mtd_info mtd; |
| 93 | struct nand_chip nand; |
| 94 | struct mtd_partition *parts; |
| 95 | struct device *dev; |
| 96 | |
Sascha Hauer | c6de7e1 | 2009-10-05 11:14:35 +0200 | [diff] [blame] | 97 | void *spare0; |
| 98 | void *main_area0; |
| 99 | void *main_area1; |
| 100 | |
| 101 | void __iomem *base; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 102 | void __iomem *regs; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 103 | int status_request; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 104 | struct clk *clk; |
| 105 | int clk_act; |
| 106 | int irq; |
| 107 | |
| 108 | wait_queue_head_t irq_waitq; |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 109 | |
| 110 | uint8_t *data_buf; |
| 111 | unsigned int buf_start; |
| 112 | int spare_len; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | /* Define delays in microsec for NAND device operations */ |
| 116 | #define TROP_US_DELAY 2000 |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 117 | |
| 118 | /* OOB placement block for use with hardware ecc generation */ |
Sascha Hauer | 8c1fd89 | 2009-10-21 10:22:01 +0200 | [diff] [blame] | 119 | static struct nand_ecclayout nand_hw_eccoob_smallpage = { |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 120 | .eccbytes = 5, |
| 121 | .eccpos = {6, 7, 8, 9, 10}, |
Sascha Hauer | 8c1fd89 | 2009-10-21 10:22:01 +0200 | [diff] [blame] | 122 | .oobfree = {{0, 5}, {12, 4}, } |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 123 | }; |
| 124 | |
Sascha Hauer | 8c1fd89 | 2009-10-21 10:22:01 +0200 | [diff] [blame] | 125 | static struct nand_ecclayout nand_hw_eccoob_largepage = { |
Vladimir Barinov | bd3fd62 | 2009-05-25 13:06:17 +0400 | [diff] [blame] | 126 | .eccbytes = 20, |
| 127 | .eccpos = {6, 7, 8, 9, 10, 22, 23, 24, 25, 26, |
| 128 | 38, 39, 40, 41, 42, 54, 55, 56, 57, 58}, |
| 129 | .oobfree = {{2, 4}, {11, 10}, {27, 10}, {43, 10}, {59, 5}, } |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | #ifdef CONFIG_MTD_PARTITIONS |
| 133 | static const char *part_probes[] = { "RedBoot", "cmdlinepart", NULL }; |
| 134 | #endif |
| 135 | |
| 136 | static irqreturn_t mxc_nfc_irq(int irq, void *dev_id) |
| 137 | { |
| 138 | struct mxc_nand_host *host = dev_id; |
| 139 | |
| 140 | uint16_t tmp; |
| 141 | |
| 142 | tmp = readw(host->regs + NFC_CONFIG1); |
| 143 | tmp |= NFC_INT_MSK; /* Disable interrupt */ |
| 144 | writew(tmp, host->regs + NFC_CONFIG1); |
| 145 | |
| 146 | wake_up(&host->irq_waitq); |
| 147 | |
| 148 | return IRQ_HANDLED; |
| 149 | } |
| 150 | |
| 151 | /* This function polls the NANDFC to wait for the basic operation to |
| 152 | * complete by checking the INT bit of config2 register. |
| 153 | */ |
| 154 | static void wait_op_done(struct mxc_nand_host *host, int max_retries, |
Sascha Hauer | 6246549 | 2009-06-04 15:57:20 +0200 | [diff] [blame] | 155 | int useirq) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 156 | { |
| 157 | uint32_t tmp; |
| 158 | |
| 159 | if (useirq) { |
| 160 | if ((readw(host->regs + NFC_CONFIG2) & NFC_INT) == 0) { |
| 161 | |
| 162 | tmp = readw(host->regs + NFC_CONFIG1); |
| 163 | tmp &= ~NFC_INT_MSK; /* Enable interrupt */ |
| 164 | writew(tmp, host->regs + NFC_CONFIG1); |
| 165 | |
| 166 | wait_event(host->irq_waitq, |
| 167 | readw(host->regs + NFC_CONFIG2) & NFC_INT); |
| 168 | |
| 169 | tmp = readw(host->regs + NFC_CONFIG2); |
| 170 | tmp &= ~NFC_INT; |
| 171 | writew(tmp, host->regs + NFC_CONFIG2); |
| 172 | } |
| 173 | } else { |
| 174 | while (max_retries-- > 0) { |
| 175 | if (readw(host->regs + NFC_CONFIG2) & NFC_INT) { |
| 176 | tmp = readw(host->regs + NFC_CONFIG2); |
| 177 | tmp &= ~NFC_INT; |
| 178 | writew(tmp, host->regs + NFC_CONFIG2); |
| 179 | break; |
| 180 | } |
| 181 | udelay(1); |
| 182 | } |
Roel Kluin | 43950a6 | 2009-06-04 16:24:59 +0200 | [diff] [blame] | 183 | if (max_retries < 0) |
Sascha Hauer | 6246549 | 2009-06-04 15:57:20 +0200 | [diff] [blame] | 184 | DEBUG(MTD_DEBUG_LEVEL0, "%s: INT not set\n", |
| 185 | __func__); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
| 189 | /* This function issues the specified command to the NAND device and |
| 190 | * waits for completion. */ |
| 191 | static void send_cmd(struct mxc_nand_host *host, uint16_t cmd, int useirq) |
| 192 | { |
| 193 | DEBUG(MTD_DEBUG_LEVEL3, "send_cmd(host, 0x%x, %d)\n", cmd, useirq); |
| 194 | |
| 195 | writew(cmd, host->regs + NFC_FLASH_CMD); |
| 196 | writew(NFC_CMD, host->regs + NFC_CONFIG2); |
| 197 | |
| 198 | /* Wait for operation to complete */ |
Sascha Hauer | 6246549 | 2009-06-04 15:57:20 +0200 | [diff] [blame] | 199 | wait_op_done(host, TROP_US_DELAY, useirq); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /* This function sends an address (or partial address) to the |
| 203 | * NAND device. The address is used to select the source/destination for |
| 204 | * a NAND command. */ |
| 205 | static void send_addr(struct mxc_nand_host *host, uint16_t addr, int islast) |
| 206 | { |
| 207 | DEBUG(MTD_DEBUG_LEVEL3, "send_addr(host, 0x%x %d)\n", addr, islast); |
| 208 | |
| 209 | writew(addr, host->regs + NFC_FLASH_ADDR); |
| 210 | writew(NFC_ADDR, host->regs + NFC_CONFIG2); |
| 211 | |
| 212 | /* Wait for operation to complete */ |
Sascha Hauer | 6246549 | 2009-06-04 15:57:20 +0200 | [diff] [blame] | 213 | wait_op_done(host, TROP_US_DELAY, islast); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 214 | } |
| 215 | |
Sascha Hauer | 2d69c7f | 2009-10-05 11:24:02 +0200 | [diff] [blame^] | 216 | static void send_page(struct mtd_info *mtd, unsigned int ops) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 217 | { |
Sascha Hauer | 2d69c7f | 2009-10-05 11:24:02 +0200 | [diff] [blame^] | 218 | struct nand_chip *nand_chip = mtd->priv; |
| 219 | struct mxc_nand_host *host = nand_chip->priv; |
Sascha Hauer | c5d23f1 | 2009-06-04 17:25:53 +0200 | [diff] [blame] | 220 | int bufs, i; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 221 | |
Sascha Hauer | 2d69c7f | 2009-10-05 11:24:02 +0200 | [diff] [blame^] | 222 | if (mtd->writesize > 512) |
Sascha Hauer | c5d23f1 | 2009-06-04 17:25:53 +0200 | [diff] [blame] | 223 | bufs = 4; |
| 224 | else |
| 225 | bufs = 1; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 226 | |
Sascha Hauer | c5d23f1 | 2009-06-04 17:25:53 +0200 | [diff] [blame] | 227 | for (i = 0; i < bufs; i++) { |
| 228 | |
| 229 | /* NANDFC buffer 0 is used for page read/write */ |
| 230 | writew(i, host->regs + NFC_BUF_ADDR); |
| 231 | |
| 232 | writew(ops, host->regs + NFC_CONFIG2); |
| 233 | |
| 234 | /* Wait for operation to complete */ |
| 235 | wait_op_done(host, TROP_US_DELAY, true); |
| 236 | } |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | /* Request the NANDFC to perform a read of the NAND device ID. */ |
| 240 | static void send_read_id(struct mxc_nand_host *host) |
| 241 | { |
| 242 | struct nand_chip *this = &host->nand; |
| 243 | uint16_t tmp; |
| 244 | |
| 245 | /* NANDFC buffer 0 is used for device ID output */ |
| 246 | writew(0x0, host->regs + NFC_BUF_ADDR); |
| 247 | |
| 248 | /* Read ID into main buffer */ |
| 249 | tmp = readw(host->regs + NFC_CONFIG1); |
| 250 | tmp &= ~NFC_SP_EN; |
| 251 | writew(tmp, host->regs + NFC_CONFIG1); |
| 252 | |
| 253 | writew(NFC_ID, host->regs + NFC_CONFIG2); |
| 254 | |
| 255 | /* Wait for operation to complete */ |
Sascha Hauer | 6246549 | 2009-06-04 15:57:20 +0200 | [diff] [blame] | 256 | wait_op_done(host, TROP_US_DELAY, true); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 257 | |
| 258 | if (this->options & NAND_BUSWIDTH_16) { |
Sascha Hauer | c6de7e1 | 2009-10-05 11:14:35 +0200 | [diff] [blame] | 259 | void __iomem *main_buf = host->main_area0; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 260 | /* compress the ID info */ |
| 261 | writeb(readb(main_buf + 2), main_buf + 1); |
| 262 | writeb(readb(main_buf + 4), main_buf + 2); |
| 263 | writeb(readb(main_buf + 6), main_buf + 3); |
| 264 | writeb(readb(main_buf + 8), main_buf + 4); |
| 265 | writeb(readb(main_buf + 10), main_buf + 5); |
| 266 | } |
Sascha Hauer | c6de7e1 | 2009-10-05 11:14:35 +0200 | [diff] [blame] | 267 | memcpy(host->data_buf, host->main_area0, 16); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | /* This function requests the NANDFC to perform a read of the |
| 271 | * NAND device status and returns the current status. */ |
| 272 | static uint16_t get_dev_status(struct mxc_nand_host *host) |
| 273 | { |
Sascha Hauer | c6de7e1 | 2009-10-05 11:14:35 +0200 | [diff] [blame] | 274 | void __iomem *main_buf = host->main_area1; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 275 | uint32_t store; |
| 276 | uint16_t ret, tmp; |
| 277 | /* Issue status request to NAND device */ |
| 278 | |
| 279 | /* store the main area1 first word, later do recovery */ |
| 280 | store = readl(main_buf); |
| 281 | /* NANDFC buffer 1 is used for device status to prevent |
| 282 | * corruption of read/write buffer on status requests. */ |
| 283 | writew(1, host->regs + NFC_BUF_ADDR); |
| 284 | |
| 285 | /* Read status into main buffer */ |
| 286 | tmp = readw(host->regs + NFC_CONFIG1); |
| 287 | tmp &= ~NFC_SP_EN; |
| 288 | writew(tmp, host->regs + NFC_CONFIG1); |
| 289 | |
| 290 | writew(NFC_STATUS, host->regs + NFC_CONFIG2); |
| 291 | |
| 292 | /* Wait for operation to complete */ |
Sascha Hauer | 6246549 | 2009-06-04 15:57:20 +0200 | [diff] [blame] | 293 | wait_op_done(host, TROP_US_DELAY, true); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 294 | |
| 295 | /* Status is placed in first word of main buffer */ |
| 296 | /* get status, then recovery area 1 data */ |
| 297 | ret = readw(main_buf); |
| 298 | writel(store, main_buf); |
| 299 | |
| 300 | return ret; |
| 301 | } |
| 302 | |
| 303 | /* This functions is used by upper layer to checks if device is ready */ |
| 304 | static int mxc_nand_dev_ready(struct mtd_info *mtd) |
| 305 | { |
| 306 | /* |
| 307 | * NFC handles R/B internally. Therefore, this function |
| 308 | * always returns status as ready. |
| 309 | */ |
| 310 | return 1; |
| 311 | } |
| 312 | |
| 313 | static void mxc_nand_enable_hwecc(struct mtd_info *mtd, int mode) |
| 314 | { |
| 315 | /* |
| 316 | * If HW ECC is enabled, we turn it on during init. There is |
| 317 | * no need to enable again here. |
| 318 | */ |
| 319 | } |
| 320 | |
| 321 | static int mxc_nand_correct_data(struct mtd_info *mtd, u_char *dat, |
| 322 | u_char *read_ecc, u_char *calc_ecc) |
| 323 | { |
| 324 | struct nand_chip *nand_chip = mtd->priv; |
| 325 | struct mxc_nand_host *host = nand_chip->priv; |
| 326 | |
| 327 | /* |
| 328 | * 1-Bit errors are automatically corrected in HW. No need for |
| 329 | * additional correction. 2-Bit errors cannot be corrected by |
| 330 | * HW ECC, so we need to return failure |
| 331 | */ |
| 332 | uint16_t ecc_status = readw(host->regs + NFC_ECC_STATUS_RESULT); |
| 333 | |
| 334 | if (((ecc_status & 0x3) == 2) || ((ecc_status >> 2) == 2)) { |
| 335 | DEBUG(MTD_DEBUG_LEVEL0, |
| 336 | "MXC_NAND: HWECC uncorrectable 2-bit ECC error\n"); |
| 337 | return -1; |
| 338 | } |
| 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | static int mxc_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, |
| 344 | u_char *ecc_code) |
| 345 | { |
| 346 | return 0; |
| 347 | } |
| 348 | |
| 349 | static u_char mxc_nand_read_byte(struct mtd_info *mtd) |
| 350 | { |
| 351 | struct nand_chip *nand_chip = mtd->priv; |
| 352 | struct mxc_nand_host *host = nand_chip->priv; |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 353 | uint8_t ret; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 354 | |
| 355 | /* Check for status request */ |
| 356 | if (host->status_request) |
| 357 | return get_dev_status(host) & 0xFF; |
| 358 | |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 359 | ret = *(uint8_t *)(host->data_buf + host->buf_start); |
| 360 | host->buf_start++; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 361 | |
| 362 | return ret; |
| 363 | } |
| 364 | |
| 365 | static uint16_t mxc_nand_read_word(struct mtd_info *mtd) |
| 366 | { |
| 367 | struct nand_chip *nand_chip = mtd->priv; |
| 368 | struct mxc_nand_host *host = nand_chip->priv; |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 369 | uint16_t ret; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 370 | |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 371 | ret = *(uint16_t *)(host->data_buf + host->buf_start); |
| 372 | host->buf_start += 2; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 373 | |
| 374 | return ret; |
| 375 | } |
| 376 | |
| 377 | /* Write data of length len to buffer buf. The data to be |
| 378 | * written on NAND Flash is first copied to RAMbuffer. After the Data Input |
| 379 | * Operation by the NFC, the data is written to NAND Flash */ |
| 380 | static void mxc_nand_write_buf(struct mtd_info *mtd, |
| 381 | const u_char *buf, int len) |
| 382 | { |
| 383 | struct nand_chip *nand_chip = mtd->priv; |
| 384 | struct mxc_nand_host *host = nand_chip->priv; |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 385 | u16 col = host->buf_start; |
| 386 | int n = mtd->oobsize + mtd->writesize - col; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 387 | |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 388 | n = min(n, len); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 389 | |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 390 | memcpy(host->data_buf + col, buf, n); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 391 | |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 392 | host->buf_start += n; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | /* Read the data buffer from the NAND Flash. To read the data from NAND |
| 396 | * Flash first the data output cycle is initiated by the NFC, which copies |
| 397 | * the data to RAMbuffer. This data of length len is then copied to buffer buf. |
| 398 | */ |
| 399 | static void mxc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len) |
| 400 | { |
| 401 | struct nand_chip *nand_chip = mtd->priv; |
| 402 | struct mxc_nand_host *host = nand_chip->priv; |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 403 | u16 col = host->buf_start; |
| 404 | int n = mtd->oobsize + mtd->writesize - col; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 405 | |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 406 | n = min(n, len); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 407 | |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 408 | memcpy(buf, host->data_buf + col, len); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 409 | |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 410 | host->buf_start += len; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | /* Used by the upper layer to verify the data in NAND Flash |
| 414 | * with the data in the buf. */ |
| 415 | static int mxc_nand_verify_buf(struct mtd_info *mtd, |
| 416 | const u_char *buf, int len) |
| 417 | { |
| 418 | return -EFAULT; |
| 419 | } |
| 420 | |
| 421 | /* This function is used by upper layer for select and |
| 422 | * deselect of the NAND chip */ |
| 423 | static void mxc_nand_select_chip(struct mtd_info *mtd, int chip) |
| 424 | { |
| 425 | struct nand_chip *nand_chip = mtd->priv; |
| 426 | struct mxc_nand_host *host = nand_chip->priv; |
| 427 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 428 | switch (chip) { |
| 429 | case -1: |
| 430 | /* Disable the NFC clock */ |
| 431 | if (host->clk_act) { |
| 432 | clk_disable(host->clk); |
| 433 | host->clk_act = 0; |
| 434 | } |
| 435 | break; |
| 436 | case 0: |
| 437 | /* Enable the NFC clock */ |
| 438 | if (!host->clk_act) { |
| 439 | clk_enable(host->clk); |
| 440 | host->clk_act = 1; |
| 441 | } |
| 442 | break; |
| 443 | |
| 444 | default: |
| 445 | break; |
| 446 | } |
| 447 | } |
| 448 | |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 449 | /* |
| 450 | * Function to transfer data to/from spare area. |
| 451 | */ |
| 452 | static void copy_spare(struct mtd_info *mtd, bool bfrom) |
| 453 | { |
| 454 | struct nand_chip *this = mtd->priv; |
| 455 | struct mxc_nand_host *host = this->priv; |
| 456 | u16 i, j; |
| 457 | u16 n = mtd->writesize >> 9; |
| 458 | u8 *d = host->data_buf + mtd->writesize; |
Sascha Hauer | c6de7e1 | 2009-10-05 11:14:35 +0200 | [diff] [blame] | 459 | u8 *s = host->spare0; |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 460 | u16 t = host->spare_len; |
| 461 | |
| 462 | j = (mtd->oobsize / n >> 1) << 1; |
| 463 | |
| 464 | if (bfrom) { |
| 465 | for (i = 0; i < n - 1; i++) |
| 466 | memcpy(d + i * j, s + i * t, j); |
| 467 | |
| 468 | /* the last section */ |
| 469 | memcpy(d + i * j, s + i * t, mtd->oobsize - i * j); |
| 470 | } else { |
| 471 | for (i = 0; i < n - 1; i++) |
| 472 | memcpy(&s[i * t], &d[i * j], j); |
| 473 | |
| 474 | /* the last section */ |
| 475 | memcpy(&s[i * t], &d[i * j], mtd->oobsize - i * j); |
| 476 | } |
| 477 | } |
| 478 | |
Sascha Hauer | a3e65b6 | 2009-06-02 11:47:59 +0200 | [diff] [blame] | 479 | static void mxc_do_addr_cycle(struct mtd_info *mtd, int column, int page_addr) |
| 480 | { |
| 481 | struct nand_chip *nand_chip = mtd->priv; |
| 482 | struct mxc_nand_host *host = nand_chip->priv; |
| 483 | |
| 484 | /* Write out column address, if necessary */ |
| 485 | if (column != -1) { |
| 486 | /* |
| 487 | * MXC NANDFC can only perform full page+spare or |
| 488 | * spare-only read/write. When the upper layers |
| 489 | * layers perform a read/write buf operation, |
| 490 | * we will used the saved column adress to index into |
| 491 | * the full page. |
| 492 | */ |
| 493 | send_addr(host, 0, page_addr == -1); |
Sascha Hauer | 2d69c7f | 2009-10-05 11:24:02 +0200 | [diff] [blame^] | 494 | if (mtd->writesize > 512) |
Sascha Hauer | a3e65b6 | 2009-06-02 11:47:59 +0200 | [diff] [blame] | 495 | /* another col addr cycle for 2k page */ |
| 496 | send_addr(host, 0, false); |
| 497 | } |
| 498 | |
| 499 | /* Write out page address, if necessary */ |
| 500 | if (page_addr != -1) { |
| 501 | /* paddr_0 - p_addr_7 */ |
| 502 | send_addr(host, (page_addr & 0xff), false); |
| 503 | |
Sascha Hauer | 2d69c7f | 2009-10-05 11:24:02 +0200 | [diff] [blame^] | 504 | if (mtd->writesize > 512) { |
Sascha Hauer | a3e65b6 | 2009-06-02 11:47:59 +0200 | [diff] [blame] | 505 | if (mtd->size >= 0x10000000) { |
| 506 | /* paddr_8 - paddr_15 */ |
| 507 | send_addr(host, (page_addr >> 8) & 0xff, false); |
| 508 | send_addr(host, (page_addr >> 16) & 0xff, true); |
| 509 | } else |
| 510 | /* paddr_8 - paddr_15 */ |
| 511 | send_addr(host, (page_addr >> 8) & 0xff, true); |
| 512 | } else { |
| 513 | /* One more address cycle for higher density devices */ |
| 514 | if (mtd->size >= 0x4000000) { |
| 515 | /* paddr_8 - paddr_15 */ |
| 516 | send_addr(host, (page_addr >> 8) & 0xff, false); |
| 517 | send_addr(host, (page_addr >> 16) & 0xff, true); |
| 518 | } else |
| 519 | /* paddr_8 - paddr_15 */ |
| 520 | send_addr(host, (page_addr >> 8) & 0xff, true); |
| 521 | } |
| 522 | } |
| 523 | } |
| 524 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 525 | /* Used by the upper layer to write command to NAND Flash for |
| 526 | * different operations to be carried out on NAND Flash */ |
| 527 | static void mxc_nand_command(struct mtd_info *mtd, unsigned command, |
| 528 | int column, int page_addr) |
| 529 | { |
| 530 | struct nand_chip *nand_chip = mtd->priv; |
| 531 | struct mxc_nand_host *host = nand_chip->priv; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 532 | |
| 533 | DEBUG(MTD_DEBUG_LEVEL3, |
| 534 | "mxc_nand_command (cmd = 0x%x, col = 0x%x, page = 0x%x)\n", |
| 535 | command, column, page_addr); |
| 536 | |
| 537 | /* Reset command state information */ |
| 538 | host->status_request = false; |
| 539 | |
| 540 | /* Command pre-processing step */ |
| 541 | switch (command) { |
| 542 | |
| 543 | case NAND_CMD_STATUS: |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 544 | host->buf_start = 0; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 545 | host->status_request = true; |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 546 | |
| 547 | send_cmd(host, command, true); |
| 548 | mxc_do_addr_cycle(mtd, column, page_addr); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 549 | break; |
| 550 | |
| 551 | case NAND_CMD_READ0: |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 552 | case NAND_CMD_READOOB: |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 553 | if (command == NAND_CMD_READ0) |
| 554 | host->buf_start = column; |
| 555 | else |
| 556 | host->buf_start = column + mtd->writesize; |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 557 | |
Sascha Hauer | 2d69c7f | 2009-10-05 11:24:02 +0200 | [diff] [blame^] | 558 | if (mtd->writesize > 512) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 559 | command = NAND_CMD_READ0; /* only READ0 is valid */ |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 560 | |
| 561 | send_cmd(host, command, false); |
| 562 | mxc_do_addr_cycle(mtd, column, page_addr); |
| 563 | |
Sascha Hauer | 2d69c7f | 2009-10-05 11:24:02 +0200 | [diff] [blame^] | 564 | if (mtd->writesize > 512) |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 565 | send_cmd(host, NAND_CMD_READSTART, true); |
Sascha Hauer | c5d23f1 | 2009-06-04 17:25:53 +0200 | [diff] [blame] | 566 | |
Sascha Hauer | 2d69c7f | 2009-10-05 11:24:02 +0200 | [diff] [blame^] | 567 | send_page(mtd, NFC_OUTPUT); |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 568 | |
Sascha Hauer | c6de7e1 | 2009-10-05 11:14:35 +0200 | [diff] [blame] | 569 | memcpy(host->data_buf, host->main_area0, mtd->writesize); |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 570 | copy_spare(mtd, true); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 571 | break; |
| 572 | |
| 573 | case NAND_CMD_SEQIN: |
| 574 | if (column >= mtd->writesize) { |
| 575 | /* |
| 576 | * FIXME: before send SEQIN command for write OOB, |
| 577 | * We must read one page out. |
| 578 | * For K9F1GXX has no READ1 command to set current HW |
| 579 | * pointer to spare area, we must write the whole page |
| 580 | * including OOB together. |
| 581 | */ |
Sascha Hauer | 2d69c7f | 2009-10-05 11:24:02 +0200 | [diff] [blame^] | 582 | if (mtd->writesize > 512) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 583 | /* call ourself to read a page */ |
| 584 | mxc_nand_command(mtd, NAND_CMD_READ0, 0, |
| 585 | page_addr); |
| 586 | |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 587 | host->buf_start = column; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 588 | |
| 589 | /* Set program pointer to spare region */ |
Sascha Hauer | 2d69c7f | 2009-10-05 11:24:02 +0200 | [diff] [blame^] | 590 | if (mtd->writesize == 512) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 591 | send_cmd(host, NAND_CMD_READOOB, false); |
| 592 | } else { |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 593 | host->buf_start = column; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 594 | |
| 595 | /* Set program pointer to page start */ |
Sascha Hauer | 2d69c7f | 2009-10-05 11:24:02 +0200 | [diff] [blame^] | 596 | if (mtd->writesize == 512) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 597 | send_cmd(host, NAND_CMD_READ0, false); |
| 598 | } |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 599 | |
| 600 | send_cmd(host, command, false); |
| 601 | mxc_do_addr_cycle(mtd, column, page_addr); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 602 | break; |
| 603 | |
| 604 | case NAND_CMD_PAGEPROG: |
Sascha Hauer | c6de7e1 | 2009-10-05 11:14:35 +0200 | [diff] [blame] | 605 | memcpy(host->main_area0, host->data_buf, mtd->writesize); |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 606 | copy_spare(mtd, false); |
Sascha Hauer | 2d69c7f | 2009-10-05 11:24:02 +0200 | [diff] [blame^] | 607 | send_page(mtd, NFC_INPUT); |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 608 | send_cmd(host, command, true); |
| 609 | mxc_do_addr_cycle(mtd, column, page_addr); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 610 | break; |
| 611 | |
| 612 | case NAND_CMD_READID: |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 613 | send_cmd(host, command, true); |
| 614 | mxc_do_addr_cycle(mtd, column, page_addr); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 615 | send_read_id(host); |
| 616 | break; |
| 617 | |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 618 | case NAND_CMD_ERASE1: |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 619 | case NAND_CMD_ERASE2: |
Sascha Hauer | 89121a6 | 2009-06-04 17:18:01 +0200 | [diff] [blame] | 620 | send_cmd(host, command, false); |
| 621 | mxc_do_addr_cycle(mtd, column, page_addr); |
| 622 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 623 | break; |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | static int __init mxcnd_probe(struct platform_device *pdev) |
| 628 | { |
| 629 | struct nand_chip *this; |
| 630 | struct mtd_info *mtd; |
| 631 | struct mxc_nand_platform_data *pdata = pdev->dev.platform_data; |
| 632 | struct mxc_nand_host *host; |
| 633 | struct resource *res; |
| 634 | uint16_t tmp; |
| 635 | int err = 0, nr_parts = 0; |
| 636 | |
| 637 | /* Allocate memory for MTD device structure and private data */ |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 638 | host = kzalloc(sizeof(struct mxc_nand_host) + NAND_MAX_PAGESIZE + |
| 639 | NAND_MAX_OOBSIZE, GFP_KERNEL); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 640 | if (!host) |
| 641 | return -ENOMEM; |
| 642 | |
Sascha Hauer | f8f9608 | 2009-06-04 17:12:26 +0200 | [diff] [blame] | 643 | host->data_buf = (uint8_t *)(host + 1); |
| 644 | host->spare_len = 16; |
| 645 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 646 | host->dev = &pdev->dev; |
| 647 | /* structures must be linked */ |
| 648 | this = &host->nand; |
| 649 | mtd = &host->mtd; |
| 650 | mtd->priv = this; |
| 651 | mtd->owner = THIS_MODULE; |
David Brownell | 87f39f0 | 2009-03-26 00:42:50 -0700 | [diff] [blame] | 652 | mtd->dev.parent = &pdev->dev; |
Vladimir Barinov | 8541c11 | 2009-04-23 15:47:22 +0400 | [diff] [blame] | 653 | mtd->name = "mxc_nand"; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 654 | |
| 655 | /* 50 us command delay time */ |
| 656 | this->chip_delay = 5; |
| 657 | |
| 658 | this->priv = host; |
| 659 | this->dev_ready = mxc_nand_dev_ready; |
| 660 | this->cmdfunc = mxc_nand_command; |
| 661 | this->select_chip = mxc_nand_select_chip; |
| 662 | this->read_byte = mxc_nand_read_byte; |
| 663 | this->read_word = mxc_nand_read_word; |
| 664 | this->write_buf = mxc_nand_write_buf; |
| 665 | this->read_buf = mxc_nand_read_buf; |
| 666 | this->verify_buf = mxc_nand_verify_buf; |
| 667 | |
Sascha Hauer | e65fb00 | 2009-02-16 14:29:10 +0100 | [diff] [blame] | 668 | host->clk = clk_get(&pdev->dev, "nfc"); |
Vladimir Barinov | 8541c11 | 2009-04-23 15:47:22 +0400 | [diff] [blame] | 669 | if (IS_ERR(host->clk)) { |
| 670 | err = PTR_ERR(host->clk); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 671 | goto eclk; |
Vladimir Barinov | 8541c11 | 2009-04-23 15:47:22 +0400 | [diff] [blame] | 672 | } |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 673 | |
| 674 | clk_enable(host->clk); |
| 675 | host->clk_act = 1; |
| 676 | |
| 677 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 678 | if (!res) { |
| 679 | err = -ENODEV; |
| 680 | goto eres; |
| 681 | } |
| 682 | |
Sascha Hauer | c6de7e1 | 2009-10-05 11:14:35 +0200 | [diff] [blame] | 683 | host->base = ioremap(res->start, resource_size(res)); |
| 684 | if (!host->base) { |
Vladimir Barinov | 8541c11 | 2009-04-23 15:47:22 +0400 | [diff] [blame] | 685 | err = -ENOMEM; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 686 | goto eres; |
| 687 | } |
| 688 | |
Sascha Hauer | c6de7e1 | 2009-10-05 11:14:35 +0200 | [diff] [blame] | 689 | host->regs = host->base; |
| 690 | host->main_area0 = host->base; |
| 691 | host->main_area1 = host->base + 0x200; |
| 692 | host->spare0 = host->base + 0x800; |
| 693 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 694 | tmp = readw(host->regs + NFC_CONFIG1); |
| 695 | tmp |= NFC_INT_MSK; |
| 696 | writew(tmp, host->regs + NFC_CONFIG1); |
| 697 | |
| 698 | init_waitqueue_head(&host->irq_waitq); |
| 699 | |
| 700 | host->irq = platform_get_irq(pdev, 0); |
| 701 | |
| 702 | err = request_irq(host->irq, mxc_nfc_irq, 0, "mxc_nd", host); |
| 703 | if (err) |
| 704 | goto eirq; |
| 705 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 706 | /* Reset NAND */ |
| 707 | this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1); |
| 708 | |
| 709 | /* preset operation */ |
| 710 | /* Unlock the internal RAM Buffer */ |
| 711 | writew(0x2, host->regs + NFC_CONFIG); |
| 712 | |
| 713 | /* Blocks to be unlocked */ |
| 714 | writew(0x0, host->regs + NFC_UNLOCKSTART_BLKADDR); |
| 715 | writew(0x4000, host->regs + NFC_UNLOCKEND_BLKADDR); |
| 716 | |
| 717 | /* Unlock Block Command for given address range */ |
| 718 | writew(0x4, host->regs + NFC_WRPROT); |
| 719 | |
Sascha Hauer | 13e1add | 2009-10-21 10:39:05 +0200 | [diff] [blame] | 720 | this->ecc.size = 512; |
| 721 | this->ecc.bytes = 3; |
| 722 | this->ecc.layout = &nand_hw_eccoob_smallpage; |
| 723 | |
| 724 | if (pdata->hw_ecc) { |
| 725 | this->ecc.calculate = mxc_nand_calculate_ecc; |
| 726 | this->ecc.hwctl = mxc_nand_enable_hwecc; |
| 727 | this->ecc.correct = mxc_nand_correct_data; |
| 728 | this->ecc.mode = NAND_ECC_HW; |
| 729 | tmp = readw(host->regs + NFC_CONFIG1); |
| 730 | tmp |= NFC_ECC_EN; |
| 731 | writew(tmp, host->regs + NFC_CONFIG1); |
| 732 | } else { |
| 733 | this->ecc.mode = NAND_ECC_SOFT; |
| 734 | tmp = readw(host->regs + NFC_CONFIG1); |
| 735 | tmp &= ~NFC_ECC_EN; |
| 736 | writew(tmp, host->regs + NFC_CONFIG1); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 737 | } |
| 738 | |
Sascha Hauer | 13e1add | 2009-10-21 10:39:05 +0200 | [diff] [blame] | 739 | /* NAND bus width determines access funtions used by upper layer */ |
| 740 | if (pdata->width == 2) |
| 741 | this->options |= NAND_BUSWIDTH_16; |
| 742 | |
Vladimir Barinov | bd3fd62 | 2009-05-25 13:06:17 +0400 | [diff] [blame] | 743 | /* first scan to find the device and get the page size */ |
| 744 | if (nand_scan_ident(mtd, 1)) { |
| 745 | err = -ENXIO; |
| 746 | goto escan; |
| 747 | } |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 748 | |
Sascha Hauer | 2d69c7f | 2009-10-05 11:24:02 +0200 | [diff] [blame^] | 749 | if (mtd->writesize == 2048) |
Sascha Hauer | 13e1add | 2009-10-21 10:39:05 +0200 | [diff] [blame] | 750 | this->ecc.layout = &nand_hw_eccoob_largepage; |
Vladimir Barinov | bd3fd62 | 2009-05-25 13:06:17 +0400 | [diff] [blame] | 751 | |
| 752 | /* second phase scan */ |
| 753 | if (nand_scan_tail(mtd)) { |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 754 | err = -ENXIO; |
| 755 | goto escan; |
| 756 | } |
| 757 | |
| 758 | /* Register the partitions */ |
| 759 | #ifdef CONFIG_MTD_PARTITIONS |
| 760 | nr_parts = |
| 761 | parse_mtd_partitions(mtd, part_probes, &host->parts, 0); |
| 762 | if (nr_parts > 0) |
| 763 | add_mtd_partitions(mtd, host->parts, nr_parts); |
| 764 | else |
| 765 | #endif |
| 766 | { |
| 767 | pr_info("Registering %s as whole device\n", mtd->name); |
| 768 | add_mtd_device(mtd); |
| 769 | } |
| 770 | |
| 771 | platform_set_drvdata(pdev, host); |
| 772 | |
| 773 | return 0; |
| 774 | |
| 775 | escan: |
Magnus Lilja | b258fd8 | 2009-05-08 21:57:47 +0200 | [diff] [blame] | 776 | free_irq(host->irq, host); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 777 | eirq: |
Sascha Hauer | c6de7e1 | 2009-10-05 11:14:35 +0200 | [diff] [blame] | 778 | iounmap(host->base); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 779 | eres: |
| 780 | clk_put(host->clk); |
| 781 | eclk: |
| 782 | kfree(host); |
| 783 | |
| 784 | return err; |
| 785 | } |
| 786 | |
Uwe Kleine-König | 82613b0 | 2009-10-01 10:28:21 +0200 | [diff] [blame] | 787 | static int __exit mxcnd_remove(struct platform_device *pdev) |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 788 | { |
| 789 | struct mxc_nand_host *host = platform_get_drvdata(pdev); |
| 790 | |
| 791 | clk_put(host->clk); |
| 792 | |
| 793 | platform_set_drvdata(pdev, NULL); |
| 794 | |
| 795 | nand_release(&host->mtd); |
Magnus Lilja | b258fd8 | 2009-05-08 21:57:47 +0200 | [diff] [blame] | 796 | free_irq(host->irq, host); |
Sascha Hauer | c6de7e1 | 2009-10-05 11:14:35 +0200 | [diff] [blame] | 797 | iounmap(host->base); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 798 | kfree(host); |
| 799 | |
| 800 | return 0; |
| 801 | } |
| 802 | |
| 803 | #ifdef CONFIG_PM |
| 804 | static int mxcnd_suspend(struct platform_device *pdev, pm_message_t state) |
| 805 | { |
Vladimir Barinov | 8541c11 | 2009-04-23 15:47:22 +0400 | [diff] [blame] | 806 | struct mtd_info *mtd = platform_get_drvdata(pdev); |
| 807 | struct nand_chip *nand_chip = mtd->priv; |
| 808 | struct mxc_nand_host *host = nand_chip->priv; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 809 | int ret = 0; |
| 810 | |
| 811 | DEBUG(MTD_DEBUG_LEVEL0, "MXC_ND : NAND suspend\n"); |
Vladimir Barinov | 8541c11 | 2009-04-23 15:47:22 +0400 | [diff] [blame] | 812 | if (mtd) { |
| 813 | ret = mtd->suspend(mtd); |
| 814 | /* Disable the NFC clock */ |
| 815 | clk_disable(host->clk); |
| 816 | } |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 817 | |
| 818 | return ret; |
| 819 | } |
| 820 | |
| 821 | static int mxcnd_resume(struct platform_device *pdev) |
| 822 | { |
Vladimir Barinov | 8541c11 | 2009-04-23 15:47:22 +0400 | [diff] [blame] | 823 | struct mtd_info *mtd = platform_get_drvdata(pdev); |
| 824 | struct nand_chip *nand_chip = mtd->priv; |
| 825 | struct mxc_nand_host *host = nand_chip->priv; |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 826 | int ret = 0; |
| 827 | |
| 828 | DEBUG(MTD_DEBUG_LEVEL0, "MXC_ND : NAND resume\n"); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 829 | |
Vladimir Barinov | 8541c11 | 2009-04-23 15:47:22 +0400 | [diff] [blame] | 830 | if (mtd) { |
| 831 | /* Enable the NFC clock */ |
| 832 | clk_enable(host->clk); |
| 833 | mtd->resume(mtd); |
| 834 | } |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 835 | |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 836 | return ret; |
| 837 | } |
| 838 | |
| 839 | #else |
| 840 | # define mxcnd_suspend NULL |
| 841 | # define mxcnd_resume NULL |
| 842 | #endif /* CONFIG_PM */ |
| 843 | |
| 844 | static struct platform_driver mxcnd_driver = { |
| 845 | .driver = { |
| 846 | .name = DRIVER_NAME, |
| 847 | }, |
| 848 | .remove = __exit_p(mxcnd_remove), |
| 849 | .suspend = mxcnd_suspend, |
| 850 | .resume = mxcnd_resume, |
| 851 | }; |
| 852 | |
| 853 | static int __init mxc_nd_init(void) |
| 854 | { |
Vladimir Barinov | 8541c11 | 2009-04-23 15:47:22 +0400 | [diff] [blame] | 855 | return platform_driver_probe(&mxcnd_driver, mxcnd_probe); |
Sascha Hauer | 34f6e15 | 2008-09-02 17:16:59 +0200 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | static void __exit mxc_nd_cleanup(void) |
| 859 | { |
| 860 | /* Unregister the device structure */ |
| 861 | platform_driver_unregister(&mxcnd_driver); |
| 862 | } |
| 863 | |
| 864 | module_init(mxc_nd_init); |
| 865 | module_exit(mxc_nd_cleanup); |
| 866 | |
| 867 | MODULE_AUTHOR("Freescale Semiconductor, Inc."); |
| 868 | MODULE_DESCRIPTION("MXC NAND MTD driver"); |
| 869 | MODULE_LICENSE("GPL"); |