Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 1 | /* |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 2 | * Copyright © 2003 Rick Bronson |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 3 | * |
| 4 | * Derived from drivers/mtd/nand/autcpu12.c |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 5 | * Copyright © 2001 Thomas Gleixner (gleixner@autronix.de) |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 6 | * |
| 7 | * Derived from drivers/mtd/spia.c |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 8 | * Copyright © 2000 Steven J. Hill (sjhill@cotw.com) |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 9 | * |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 10 | * |
| 11 | * Add Hardware ECC support for AT91SAM9260 / AT91SAM9263 |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 12 | * Richard Genoud (richard.genoud@gmail.com), Adeneo Copyright © 2007 |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 13 | * |
| 14 | * Derived from Das U-Boot source code |
| 15 | * (u-boot-1.1.5/board/atmel/at91sam9263ek/nand.c) |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 16 | * © Copyright 2006 ATMEL Rousset, Lacressonniere Nicolas |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 17 | * |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 18 | * Add Programmable Multibit ECC support for various AT91 SoC |
| 19 | * © Copyright 2012 ATMEL, Hong Xu |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 20 | * |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 21 | * Add Nand Flash Controller support for SAMA5 SoC |
| 22 | * © Copyright 2013 ATMEL, Josh Wu (josh.wu@atmel.com) |
| 23 | * |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 24 | * This program is free software; you can redistribute it and/or modify |
| 25 | * it under the terms of the GNU General Public License version 2 as |
| 26 | * published by the Free Software Foundation. |
| 27 | * |
| 28 | */ |
| 29 | |
Boris BREZILLON | 2d405ec | 2014-09-13 01:23:59 +0200 | [diff] [blame] | 30 | #include <linux/clk.h> |
Alexey Dobriyan | b7f080c | 2011-06-16 11:01:34 +0000 | [diff] [blame] | 31 | #include <linux/dma-mapping.h> |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 32 | #include <linux/slab.h> |
| 33 | #include <linux/module.h> |
Simon Polette | f4fa697 | 2009-05-27 18:19:39 +0300 | [diff] [blame] | 34 | #include <linux/moduleparam.h> |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 35 | #include <linux/platform_device.h> |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 36 | #include <linux/of.h> |
| 37 | #include <linux/of_device.h> |
| 38 | #include <linux/of_gpio.h> |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 39 | #include <linux/mtd/mtd.h> |
| 40 | #include <linux/mtd/nand.h> |
| 41 | #include <linux/mtd/partitions.h> |
| 42 | |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 43 | #include <linux/delay.h> |
Hans-Christian Egtvedt | 5c39c4c | 2011-04-13 15:55:17 +0200 | [diff] [blame] | 44 | #include <linux/dmaengine.h> |
David Woodhouse | 90574d0 | 2008-06-07 08:49:00 +0100 | [diff] [blame] | 45 | #include <linux/gpio.h> |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 46 | #include <linux/interrupt.h> |
David Woodhouse | 90574d0 | 2008-06-07 08:49:00 +0100 | [diff] [blame] | 47 | #include <linux/io.h> |
Jean-Christophe PLAGNIOL-VILLARD | bf4289c | 2011-12-29 14:43:24 +0800 | [diff] [blame] | 48 | #include <linux/platform_data/atmel.h> |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 49 | |
Hong Xu | cbc6c5e | 2011-01-18 14:36:05 +0800 | [diff] [blame] | 50 | static int use_dma = 1; |
| 51 | module_param(use_dma, int, 0); |
| 52 | |
Simon Polette | f4fa697 | 2009-05-27 18:19:39 +0300 | [diff] [blame] | 53 | static int on_flash_bbt = 0; |
| 54 | module_param(on_flash_bbt, int, 0); |
| 55 | |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 56 | /* Register access macros */ |
| 57 | #define ecc_readl(add, reg) \ |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 58 | __raw_readl(add + ATMEL_ECC_##reg) |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 59 | #define ecc_writel(add, reg, value) \ |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 60 | __raw_writel((value), add + ATMEL_ECC_##reg) |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 61 | |
Håvard Skinnemoen | d4f4c0a | 2008-06-06 18:04:52 +0200 | [diff] [blame] | 62 | #include "atmel_nand_ecc.h" /* Hardware ECC registers */ |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 63 | #include "atmel_nand_nfc.h" /* Nand Flash Controller definition */ |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 64 | |
Wu, Josh | 5158577 | 2015-01-19 16:33:06 +0800 | [diff] [blame] | 65 | struct atmel_nand_caps { |
| 66 | bool pmecc_correct_erase_page; |
Romain Izard | 5575075 | 2016-02-10 10:56:25 +0100 | [diff] [blame] | 67 | uint8_t pmecc_max_correction; |
Wu, Josh | 5158577 | 2015-01-19 16:33:06 +0800 | [diff] [blame] | 68 | }; |
| 69 | |
Romain Izard | 5ddc7bd | 2016-02-10 10:56:23 +0100 | [diff] [blame] | 70 | struct atmel_nand_nfc_caps { |
| 71 | uint32_t rb_mask; |
| 72 | }; |
| 73 | |
Boris Brezillon | cc00383 | 2016-02-03 19:59:11 +0100 | [diff] [blame^] | 74 | /* |
| 75 | * oob layout for large page size |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 76 | * bad block info is on bytes 0 and 1 |
| 77 | * the bytes have to be consecutives to avoid |
| 78 | * several NAND_CMD_RNDOUT during read |
Boris Brezillon | cc00383 | 2016-02-03 19:59:11 +0100 | [diff] [blame^] | 79 | * |
| 80 | * oob layout for small page size |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 81 | * bad block info is on bytes 4 and 5 |
| 82 | * the bytes have to be consecutives to avoid |
| 83 | * several NAND_CMD_RNDOUT during read |
| 84 | */ |
Boris Brezillon | cc00383 | 2016-02-03 19:59:11 +0100 | [diff] [blame^] | 85 | static int atmel_ooblayout_ecc_sp(struct mtd_info *mtd, int section, |
| 86 | struct mtd_oob_region *oobregion) |
| 87 | { |
| 88 | if (section) |
| 89 | return -ERANGE; |
| 90 | |
| 91 | oobregion->length = 4; |
| 92 | oobregion->offset = 0; |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | static int atmel_ooblayout_free_sp(struct mtd_info *mtd, int section, |
| 98 | struct mtd_oob_region *oobregion) |
| 99 | { |
| 100 | if (section) |
| 101 | return -ERANGE; |
| 102 | |
| 103 | oobregion->offset = 6; |
| 104 | oobregion->length = mtd->oobsize - oobregion->offset; |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | static const struct mtd_ooblayout_ops atmel_ooblayout_sp_ops = { |
| 110 | .ecc = atmel_ooblayout_ecc_sp, |
| 111 | .free = atmel_ooblayout_free_sp, |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 112 | }; |
| 113 | |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 114 | struct atmel_nfc { |
| 115 | void __iomem *base_cmd_regs; |
| 116 | void __iomem *hsmc_regs; |
Wu, Josh | 068b44b | 2014-11-07 15:26:09 +0800 | [diff] [blame] | 117 | void *sram_bank0; |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 118 | dma_addr_t sram_bank0_phys; |
Josh Wu | 1ae9c09 | 2013-08-05 19:14:36 +0800 | [diff] [blame] | 119 | bool use_nfc_sram; |
Josh Wu | 6054d4d | 2013-08-05 19:14:37 +0800 | [diff] [blame] | 120 | bool write_by_sram; |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 121 | |
Boris BREZILLON | 2d405ec | 2014-09-13 01:23:59 +0200 | [diff] [blame] | 122 | struct clk *clk; |
| 123 | |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 124 | bool is_initialized; |
Josh Wu | e4e0693 | 2014-06-10 17:50:11 +0800 | [diff] [blame] | 125 | struct completion comp_ready; |
| 126 | struct completion comp_cmd_done; |
| 127 | struct completion comp_xfer_done; |
Josh Wu | 1ae9c09 | 2013-08-05 19:14:36 +0800 | [diff] [blame] | 128 | |
| 129 | /* Point to the sram bank which include readed data via NFC */ |
Wu, Josh | 068b44b | 2014-11-07 15:26:09 +0800 | [diff] [blame] | 130 | void *data_in_sram; |
Josh Wu | 6054d4d | 2013-08-05 19:14:37 +0800 | [diff] [blame] | 131 | bool will_write_sram; |
Romain Izard | 5ddc7bd | 2016-02-10 10:56:23 +0100 | [diff] [blame] | 132 | const struct atmel_nand_nfc_caps *caps; |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 133 | }; |
| 134 | static struct atmel_nfc nand_nfc; |
| 135 | |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 136 | struct atmel_nand_host { |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 137 | struct nand_chip nand_chip; |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 138 | void __iomem *io_base; |
Hong Xu | cbc6c5e | 2011-01-18 14:36:05 +0800 | [diff] [blame] | 139 | dma_addr_t io_phys; |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 140 | struct atmel_nand_data board; |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 141 | struct device *dev; |
| 142 | void __iomem *ecc; |
Hong Xu | cbc6c5e | 2011-01-18 14:36:05 +0800 | [diff] [blame] | 143 | |
| 144 | struct completion comp; |
| 145 | struct dma_chan *dma_chan; |
Josh Wu | a41b51a | 2012-06-29 17:47:54 +0800 | [diff] [blame] | 146 | |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 147 | struct atmel_nfc *nfc; |
| 148 | |
LABBE Corentin | 72eaec2 | 2015-11-20 08:45:16 +0100 | [diff] [blame] | 149 | const struct atmel_nand_caps *caps; |
Josh Wu | a41b51a | 2012-06-29 17:47:54 +0800 | [diff] [blame] | 150 | bool has_pmecc; |
| 151 | u8 pmecc_corr_cap; |
| 152 | u16 pmecc_sector_size; |
Josh Wu | abb1cd0 | 2014-10-11 18:01:50 +0800 | [diff] [blame] | 153 | bool has_no_lookup_table; |
Josh Wu | a41b51a | 2012-06-29 17:47:54 +0800 | [diff] [blame] | 154 | u32 pmecc_lookup_table_offset; |
Josh Wu | e66b431 | 2013-01-23 20:47:11 +0800 | [diff] [blame] | 155 | u32 pmecc_lookup_table_offset_512; |
| 156 | u32 pmecc_lookup_table_offset_1024; |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 157 | |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 158 | int pmecc_degree; /* Degree of remainders */ |
| 159 | int pmecc_cw_len; /* Length of codeword */ |
| 160 | |
| 161 | void __iomem *pmerrloc_base; |
Romain Izard | 5575075 | 2016-02-10 10:56:25 +0100 | [diff] [blame] | 162 | void __iomem *pmerrloc_el_base; |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 163 | void __iomem *pmecc_rom_base; |
| 164 | |
| 165 | /* lookup table for alpha_to and index_of */ |
| 166 | void __iomem *pmecc_alpha_to; |
| 167 | void __iomem *pmecc_index_of; |
| 168 | |
| 169 | /* data for pmecc computation */ |
| 170 | int16_t *pmecc_partial_syn; |
| 171 | int16_t *pmecc_si; |
| 172 | int16_t *pmecc_smu; /* Sigma table */ |
| 173 | int16_t *pmecc_lmu; /* polynomal order */ |
| 174 | int *pmecc_mu; |
| 175 | int *pmecc_dmu; |
| 176 | int *pmecc_delta; |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 177 | }; |
| 178 | |
| 179 | /* |
Atsushi Nemoto | 8136508 | 2008-04-27 01:51:12 +0900 | [diff] [blame] | 180 | * Enable NAND. |
| 181 | */ |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 182 | static void atmel_nand_enable(struct atmel_nand_host *host) |
Atsushi Nemoto | 8136508 | 2008-04-27 01:51:12 +0900 | [diff] [blame] | 183 | { |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 184 | if (gpio_is_valid(host->board.enable_pin)) |
| 185 | gpio_set_value(host->board.enable_pin, 0); |
Atsushi Nemoto | 8136508 | 2008-04-27 01:51:12 +0900 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /* |
| 189 | * Disable NAND. |
| 190 | */ |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 191 | static void atmel_nand_disable(struct atmel_nand_host *host) |
Atsushi Nemoto | 8136508 | 2008-04-27 01:51:12 +0900 | [diff] [blame] | 192 | { |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 193 | if (gpio_is_valid(host->board.enable_pin)) |
| 194 | gpio_set_value(host->board.enable_pin, 1); |
Atsushi Nemoto | 8136508 | 2008-04-27 01:51:12 +0900 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /* |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 198 | * Hardware specific access to control-lines |
| 199 | */ |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 200 | static void atmel_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl) |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 201 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 202 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 203 | struct atmel_nand_host *host = nand_get_controller_data(nand_chip); |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 204 | |
Atsushi Nemoto | 8136508 | 2008-04-27 01:51:12 +0900 | [diff] [blame] | 205 | if (ctrl & NAND_CTRL_CHANGE) { |
Atsushi Nemoto | 2314488 | 2008-04-24 23:51:29 +0900 | [diff] [blame] | 206 | if (ctrl & NAND_NCE) |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 207 | atmel_nand_enable(host); |
Atsushi Nemoto | 2314488 | 2008-04-24 23:51:29 +0900 | [diff] [blame] | 208 | else |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 209 | atmel_nand_disable(host); |
Atsushi Nemoto | 2314488 | 2008-04-24 23:51:29 +0900 | [diff] [blame] | 210 | } |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 211 | if (cmd == NAND_CMD_NONE) |
| 212 | return; |
| 213 | |
| 214 | if (ctrl & NAND_CLE) |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 215 | writeb(cmd, host->io_base + (1 << host->board.cle)); |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 216 | else |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 217 | writeb(cmd, host->io_base + (1 << host->board.ale)); |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | /* |
| 221 | * Read the Device Ready pin. |
| 222 | */ |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 223 | static int atmel_nand_device_ready(struct mtd_info *mtd) |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 224 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 225 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 226 | struct atmel_nand_host *host = nand_get_controller_data(nand_chip); |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 227 | |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 228 | return gpio_get_value(host->board.rdy_pin) ^ |
| 229 | !!host->board.rdy_pin_active_low; |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 230 | } |
| 231 | |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 232 | /* Set up for hardware ready pin and enable pin. */ |
| 233 | static int atmel_nand_set_enable_ready_pins(struct mtd_info *mtd) |
| 234 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 235 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 236 | struct atmel_nand_host *host = nand_get_controller_data(chip); |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 237 | int res = 0; |
| 238 | |
| 239 | if (gpio_is_valid(host->board.rdy_pin)) { |
| 240 | res = devm_gpio_request(host->dev, |
| 241 | host->board.rdy_pin, "nand_rdy"); |
| 242 | if (res < 0) { |
| 243 | dev_err(host->dev, |
| 244 | "can't request rdy gpio %d\n", |
| 245 | host->board.rdy_pin); |
| 246 | return res; |
| 247 | } |
| 248 | |
| 249 | res = gpio_direction_input(host->board.rdy_pin); |
| 250 | if (res < 0) { |
| 251 | dev_err(host->dev, |
| 252 | "can't request input direction rdy gpio %d\n", |
| 253 | host->board.rdy_pin); |
| 254 | return res; |
| 255 | } |
| 256 | |
| 257 | chip->dev_ready = atmel_nand_device_ready; |
| 258 | } |
| 259 | |
| 260 | if (gpio_is_valid(host->board.enable_pin)) { |
| 261 | res = devm_gpio_request(host->dev, |
| 262 | host->board.enable_pin, "nand_enable"); |
| 263 | if (res < 0) { |
| 264 | dev_err(host->dev, |
| 265 | "can't request enable gpio %d\n", |
| 266 | host->board.enable_pin); |
| 267 | return res; |
| 268 | } |
| 269 | |
| 270 | res = gpio_direction_output(host->board.enable_pin, 1); |
| 271 | if (res < 0) { |
| 272 | dev_err(host->dev, |
| 273 | "can't request output direction enable gpio %d\n", |
| 274 | host->board.enable_pin); |
| 275 | return res; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | return res; |
| 280 | } |
| 281 | |
Artem Bityutskiy | 5008231 | 2012-02-02 13:54:25 +0200 | [diff] [blame] | 282 | /* |
| 283 | * Minimal-overhead PIO for data access. |
| 284 | */ |
| 285 | static void atmel_read_buf8(struct mtd_info *mtd, u8 *buf, int len) |
| 286 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 287 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 288 | struct atmel_nand_host *host = nand_get_controller_data(nand_chip); |
Artem Bityutskiy | 5008231 | 2012-02-02 13:54:25 +0200 | [diff] [blame] | 289 | |
Josh Wu | 1ae9c09 | 2013-08-05 19:14:36 +0800 | [diff] [blame] | 290 | if (host->nfc && host->nfc->use_nfc_sram && host->nfc->data_in_sram) { |
Wu, Josh | 068b44b | 2014-11-07 15:26:09 +0800 | [diff] [blame] | 291 | memcpy(buf, host->nfc->data_in_sram, len); |
Josh Wu | 1ae9c09 | 2013-08-05 19:14:36 +0800 | [diff] [blame] | 292 | host->nfc->data_in_sram += len; |
| 293 | } else { |
| 294 | __raw_readsb(nand_chip->IO_ADDR_R, buf, len); |
| 295 | } |
Artem Bityutskiy | 5008231 | 2012-02-02 13:54:25 +0200 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | static void atmel_read_buf16(struct mtd_info *mtd, u8 *buf, int len) |
| 299 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 300 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 301 | struct atmel_nand_host *host = nand_get_controller_data(nand_chip); |
Artem Bityutskiy | 5008231 | 2012-02-02 13:54:25 +0200 | [diff] [blame] | 302 | |
Josh Wu | 1ae9c09 | 2013-08-05 19:14:36 +0800 | [diff] [blame] | 303 | if (host->nfc && host->nfc->use_nfc_sram && host->nfc->data_in_sram) { |
Wu, Josh | 068b44b | 2014-11-07 15:26:09 +0800 | [diff] [blame] | 304 | memcpy(buf, host->nfc->data_in_sram, len); |
Josh Wu | 1ae9c09 | 2013-08-05 19:14:36 +0800 | [diff] [blame] | 305 | host->nfc->data_in_sram += len; |
| 306 | } else { |
| 307 | __raw_readsw(nand_chip->IO_ADDR_R, buf, len / 2); |
| 308 | } |
Artem Bityutskiy | 5008231 | 2012-02-02 13:54:25 +0200 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | static void atmel_write_buf8(struct mtd_info *mtd, const u8 *buf, int len) |
| 312 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 313 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
Artem Bityutskiy | 5008231 | 2012-02-02 13:54:25 +0200 | [diff] [blame] | 314 | |
| 315 | __raw_writesb(nand_chip->IO_ADDR_W, buf, len); |
| 316 | } |
| 317 | |
| 318 | static void atmel_write_buf16(struct mtd_info *mtd, const u8 *buf, int len) |
| 319 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 320 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
Artem Bityutskiy | 5008231 | 2012-02-02 13:54:25 +0200 | [diff] [blame] | 321 | |
| 322 | __raw_writesw(nand_chip->IO_ADDR_W, buf, len / 2); |
| 323 | } |
| 324 | |
Hong Xu | cbc6c5e | 2011-01-18 14:36:05 +0800 | [diff] [blame] | 325 | static void dma_complete_func(void *completion) |
| 326 | { |
| 327 | complete(completion); |
| 328 | } |
| 329 | |
Josh Wu | 1ae9c09 | 2013-08-05 19:14:36 +0800 | [diff] [blame] | 330 | static int nfc_set_sram_bank(struct atmel_nand_host *host, unsigned int bank) |
| 331 | { |
| 332 | /* NFC only has two banks. Must be 0 or 1 */ |
| 333 | if (bank > 1) |
| 334 | return -EINVAL; |
| 335 | |
| 336 | if (bank) { |
Boris BREZILLON | ac01efe | 2015-12-10 08:59:50 +0100 | [diff] [blame] | 337 | struct mtd_info *mtd = nand_to_mtd(&host->nand_chip); |
| 338 | |
Josh Wu | 1ae9c09 | 2013-08-05 19:14:36 +0800 | [diff] [blame] | 339 | /* Only for a 2k-page or lower flash, NFC can handle 2 banks */ |
Boris BREZILLON | ac01efe | 2015-12-10 08:59:50 +0100 | [diff] [blame] | 340 | if (mtd->writesize > 2048) |
Josh Wu | 1ae9c09 | 2013-08-05 19:14:36 +0800 | [diff] [blame] | 341 | return -EINVAL; |
| 342 | nfc_writel(host->nfc->hsmc_regs, BANK, ATMEL_HSMC_NFC_BANK1); |
| 343 | } else { |
| 344 | nfc_writel(host->nfc->hsmc_regs, BANK, ATMEL_HSMC_NFC_BANK0); |
| 345 | } |
| 346 | |
| 347 | return 0; |
| 348 | } |
| 349 | |
| 350 | static uint nfc_get_sram_off(struct atmel_nand_host *host) |
| 351 | { |
| 352 | if (nfc_readl(host->nfc->hsmc_regs, BANK) & ATMEL_HSMC_NFC_BANK1) |
| 353 | return NFC_SRAM_BANK1_OFFSET; |
| 354 | else |
| 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | static dma_addr_t nfc_sram_phys(struct atmel_nand_host *host) |
| 359 | { |
| 360 | if (nfc_readl(host->nfc->hsmc_regs, BANK) & ATMEL_HSMC_NFC_BANK1) |
| 361 | return host->nfc->sram_bank0_phys + NFC_SRAM_BANK1_OFFSET; |
| 362 | else |
| 363 | return host->nfc->sram_bank0_phys; |
| 364 | } |
| 365 | |
Hong Xu | cbc6c5e | 2011-01-18 14:36:05 +0800 | [diff] [blame] | 366 | static int atmel_nand_dma_op(struct mtd_info *mtd, void *buf, int len, |
| 367 | int is_read) |
| 368 | { |
| 369 | struct dma_device *dma_dev; |
| 370 | enum dma_ctrl_flags flags; |
| 371 | dma_addr_t dma_src_addr, dma_dst_addr, phys_addr; |
| 372 | struct dma_async_tx_descriptor *tx = NULL; |
| 373 | dma_cookie_t cookie; |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 374 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 375 | struct atmel_nand_host *host = nand_get_controller_data(chip); |
Hong Xu | cbc6c5e | 2011-01-18 14:36:05 +0800 | [diff] [blame] | 376 | void *p = buf; |
| 377 | int err = -EIO; |
| 378 | enum dma_data_direction dir = is_read ? DMA_FROM_DEVICE : DMA_TO_DEVICE; |
Josh Wu | 1ae9c09 | 2013-08-05 19:14:36 +0800 | [diff] [blame] | 379 | struct atmel_nfc *nfc = host->nfc; |
Hong Xu | cbc6c5e | 2011-01-18 14:36:05 +0800 | [diff] [blame] | 380 | |
Hong Xu | 80b4f81 | 2011-03-31 18:33:15 +0800 | [diff] [blame] | 381 | if (buf >= high_memory) |
| 382 | goto err_buf; |
Hong Xu | cbc6c5e | 2011-01-18 14:36:05 +0800 | [diff] [blame] | 383 | |
| 384 | dma_dev = host->dma_chan->device; |
| 385 | |
Bartlomiej Zolnierkiewicz | 0776ae7 | 2013-10-18 19:35:33 +0200 | [diff] [blame] | 386 | flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT; |
Hong Xu | cbc6c5e | 2011-01-18 14:36:05 +0800 | [diff] [blame] | 387 | |
| 388 | phys_addr = dma_map_single(dma_dev->dev, p, len, dir); |
| 389 | if (dma_mapping_error(dma_dev->dev, phys_addr)) { |
| 390 | dev_err(host->dev, "Failed to dma_map_single\n"); |
| 391 | goto err_buf; |
| 392 | } |
| 393 | |
| 394 | if (is_read) { |
Josh Wu | 1ae9c09 | 2013-08-05 19:14:36 +0800 | [diff] [blame] | 395 | if (nfc && nfc->data_in_sram) |
| 396 | dma_src_addr = nfc_sram_phys(host) + (nfc->data_in_sram |
| 397 | - (nfc->sram_bank0 + nfc_get_sram_off(host))); |
| 398 | else |
| 399 | dma_src_addr = host->io_phys; |
| 400 | |
Hong Xu | cbc6c5e | 2011-01-18 14:36:05 +0800 | [diff] [blame] | 401 | dma_dst_addr = phys_addr; |
| 402 | } else { |
| 403 | dma_src_addr = phys_addr; |
Josh Wu | 6054d4d | 2013-08-05 19:14:37 +0800 | [diff] [blame] | 404 | |
| 405 | if (nfc && nfc->write_by_sram) |
| 406 | dma_dst_addr = nfc_sram_phys(host); |
| 407 | else |
| 408 | dma_dst_addr = host->io_phys; |
Hong Xu | cbc6c5e | 2011-01-18 14:36:05 +0800 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | tx = dma_dev->device_prep_dma_memcpy(host->dma_chan, dma_dst_addr, |
| 412 | dma_src_addr, len, flags); |
| 413 | if (!tx) { |
| 414 | dev_err(host->dev, "Failed to prepare DMA memcpy\n"); |
| 415 | goto err_dma; |
| 416 | } |
| 417 | |
| 418 | init_completion(&host->comp); |
| 419 | tx->callback = dma_complete_func; |
| 420 | tx->callback_param = &host->comp; |
| 421 | |
| 422 | cookie = tx->tx_submit(tx); |
| 423 | if (dma_submit_error(cookie)) { |
| 424 | dev_err(host->dev, "Failed to do DMA tx_submit\n"); |
| 425 | goto err_dma; |
| 426 | } |
| 427 | |
| 428 | dma_async_issue_pending(host->dma_chan); |
| 429 | wait_for_completion(&host->comp); |
| 430 | |
Josh Wu | 1ae9c09 | 2013-08-05 19:14:36 +0800 | [diff] [blame] | 431 | if (is_read && nfc && nfc->data_in_sram) |
| 432 | /* After read data from SRAM, need to increase the position */ |
| 433 | nfc->data_in_sram += len; |
| 434 | |
Hong Xu | cbc6c5e | 2011-01-18 14:36:05 +0800 | [diff] [blame] | 435 | err = 0; |
| 436 | |
| 437 | err_dma: |
| 438 | dma_unmap_single(dma_dev->dev, phys_addr, len, dir); |
| 439 | err_buf: |
| 440 | if (err != 0) |
Nicolas Ferre | 74414a94 | 2014-02-12 12:26:54 +0100 | [diff] [blame] | 441 | dev_dbg(host->dev, "Fall back to CPU I/O\n"); |
Hong Xu | cbc6c5e | 2011-01-18 14:36:05 +0800 | [diff] [blame] | 442 | return err; |
| 443 | } |
| 444 | |
| 445 | static void atmel_read_buf(struct mtd_info *mtd, u8 *buf, int len) |
| 446 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 447 | struct nand_chip *chip = mtd_to_nand(mtd); |
Hong Xu | cbc6c5e | 2011-01-18 14:36:05 +0800 | [diff] [blame] | 448 | |
Nicolas Ferre | 9d51567 | 2011-04-01 16:40:44 +0200 | [diff] [blame] | 449 | if (use_dma && len > mtd->oobsize) |
| 450 | /* only use DMA for bigger than oob size: better performances */ |
Hong Xu | cbc6c5e | 2011-01-18 14:36:05 +0800 | [diff] [blame] | 451 | if (atmel_nand_dma_op(mtd, buf, len, 1) == 0) |
| 452 | return; |
| 453 | |
Boris Brezillon | 4f3cab9 | 2016-04-01 14:54:22 +0200 | [diff] [blame] | 454 | if (chip->options & NAND_BUSWIDTH_16) |
Artem Bityutskiy | 5008231 | 2012-02-02 13:54:25 +0200 | [diff] [blame] | 455 | atmel_read_buf16(mtd, buf, len); |
| 456 | else |
| 457 | atmel_read_buf8(mtd, buf, len); |
Hong Xu | cbc6c5e | 2011-01-18 14:36:05 +0800 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | static void atmel_write_buf(struct mtd_info *mtd, const u8 *buf, int len) |
| 461 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 462 | struct nand_chip *chip = mtd_to_nand(mtd); |
Hong Xu | cbc6c5e | 2011-01-18 14:36:05 +0800 | [diff] [blame] | 463 | |
Nicolas Ferre | 9d51567 | 2011-04-01 16:40:44 +0200 | [diff] [blame] | 464 | if (use_dma && len > mtd->oobsize) |
| 465 | /* only use DMA for bigger than oob size: better performances */ |
Hong Xu | cbc6c5e | 2011-01-18 14:36:05 +0800 | [diff] [blame] | 466 | if (atmel_nand_dma_op(mtd, (void *)buf, len, 0) == 0) |
| 467 | return; |
| 468 | |
Boris Brezillon | 4f3cab9 | 2016-04-01 14:54:22 +0200 | [diff] [blame] | 469 | if (chip->options & NAND_BUSWIDTH_16) |
Artem Bityutskiy | 5008231 | 2012-02-02 13:54:25 +0200 | [diff] [blame] | 470 | atmel_write_buf16(mtd, buf, len); |
| 471 | else |
| 472 | atmel_write_buf8(mtd, buf, len); |
Hong Xu | cbc6c5e | 2011-01-18 14:36:05 +0800 | [diff] [blame] | 473 | } |
| 474 | |
David Brownell | 23a346c | 2008-07-03 23:40:16 -0700 | [diff] [blame] | 475 | /* |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 476 | * Return number of ecc bytes per sector according to sector size and |
| 477 | * correction capability |
| 478 | * |
| 479 | * Following table shows what at91 PMECC supported: |
| 480 | * Correction Capability Sector_512_bytes Sector_1024_bytes |
| 481 | * ===================== ================ ================= |
| 482 | * 2-bits 4-bytes 4-bytes |
| 483 | * 4-bits 7-bytes 7-bytes |
| 484 | * 8-bits 13-bytes 14-bytes |
| 485 | * 12-bits 20-bytes 21-bytes |
| 486 | * 24-bits 39-bytes 42-bytes |
Romain Izard | 9424846 | 2016-02-10 10:56:26 +0100 | [diff] [blame] | 487 | * 32-bits 52-bytes 56-bytes |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 488 | */ |
Bill Pemberton | 06f2551 | 2012-11-19 13:23:07 -0500 | [diff] [blame] | 489 | static int pmecc_get_ecc_bytes(int cap, int sector_size) |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 490 | { |
| 491 | int m = 12 + sector_size / 512; |
| 492 | return (m * cap + 7) / 8; |
| 493 | } |
| 494 | |
Bill Pemberton | 06f2551 | 2012-11-19 13:23:07 -0500 | [diff] [blame] | 495 | static void __iomem *pmecc_get_alpha_to(struct atmel_nand_host *host) |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 496 | { |
| 497 | int table_size; |
| 498 | |
| 499 | table_size = host->pmecc_sector_size == 512 ? |
| 500 | PMECC_LOOKUP_TABLE_SIZE_512 : PMECC_LOOKUP_TABLE_SIZE_1024; |
| 501 | |
| 502 | return host->pmecc_rom_base + host->pmecc_lookup_table_offset + |
| 503 | table_size * sizeof(int16_t); |
| 504 | } |
| 505 | |
Bill Pemberton | 06f2551 | 2012-11-19 13:23:07 -0500 | [diff] [blame] | 506 | static int pmecc_data_alloc(struct atmel_nand_host *host) |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 507 | { |
| 508 | const int cap = host->pmecc_corr_cap; |
Jean-Christophe PLAGNIOL-VILLARD | 0d63748 | 2013-08-05 19:14:33 +0800 | [diff] [blame] | 509 | int size; |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 510 | |
Jean-Christophe PLAGNIOL-VILLARD | 0d63748 | 2013-08-05 19:14:33 +0800 | [diff] [blame] | 511 | size = (2 * cap + 1) * sizeof(int16_t); |
| 512 | host->pmecc_partial_syn = devm_kzalloc(host->dev, size, GFP_KERNEL); |
| 513 | host->pmecc_si = devm_kzalloc(host->dev, size, GFP_KERNEL); |
| 514 | host->pmecc_lmu = devm_kzalloc(host->dev, |
| 515 | (cap + 1) * sizeof(int16_t), GFP_KERNEL); |
| 516 | host->pmecc_smu = devm_kzalloc(host->dev, |
| 517 | (cap + 2) * size, GFP_KERNEL); |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 518 | |
Jean-Christophe PLAGNIOL-VILLARD | 0d63748 | 2013-08-05 19:14:33 +0800 | [diff] [blame] | 519 | size = (cap + 1) * sizeof(int); |
| 520 | host->pmecc_mu = devm_kzalloc(host->dev, size, GFP_KERNEL); |
| 521 | host->pmecc_dmu = devm_kzalloc(host->dev, size, GFP_KERNEL); |
| 522 | host->pmecc_delta = devm_kzalloc(host->dev, size, GFP_KERNEL); |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 523 | |
Jean-Christophe PLAGNIOL-VILLARD | 0d63748 | 2013-08-05 19:14:33 +0800 | [diff] [blame] | 524 | if (!host->pmecc_partial_syn || |
| 525 | !host->pmecc_si || |
| 526 | !host->pmecc_lmu || |
| 527 | !host->pmecc_smu || |
| 528 | !host->pmecc_mu || |
| 529 | !host->pmecc_dmu || |
| 530 | !host->pmecc_delta) |
| 531 | return -ENOMEM; |
| 532 | |
| 533 | return 0; |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | static void pmecc_gen_syndrome(struct mtd_info *mtd, int sector) |
| 537 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 538 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 539 | struct atmel_nand_host *host = nand_get_controller_data(nand_chip); |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 540 | int i; |
| 541 | uint32_t value; |
| 542 | |
| 543 | /* Fill odd syndromes */ |
| 544 | for (i = 0; i < host->pmecc_corr_cap; i++) { |
| 545 | value = pmecc_readl_rem_relaxed(host->ecc, sector, i / 2); |
| 546 | if (i & 1) |
| 547 | value >>= 16; |
| 548 | value &= 0xffff; |
| 549 | host->pmecc_partial_syn[(2 * i) + 1] = (int16_t)value; |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | static void pmecc_substitute(struct mtd_info *mtd) |
| 554 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 555 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 556 | struct atmel_nand_host *host = nand_get_controller_data(nand_chip); |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 557 | int16_t __iomem *alpha_to = host->pmecc_alpha_to; |
| 558 | int16_t __iomem *index_of = host->pmecc_index_of; |
| 559 | int16_t *partial_syn = host->pmecc_partial_syn; |
| 560 | const int cap = host->pmecc_corr_cap; |
| 561 | int16_t *si; |
| 562 | int i, j; |
| 563 | |
| 564 | /* si[] is a table that holds the current syndrome value, |
| 565 | * an element of that table belongs to the field |
| 566 | */ |
| 567 | si = host->pmecc_si; |
| 568 | |
| 569 | memset(&si[1], 0, sizeof(int16_t) * (2 * cap - 1)); |
| 570 | |
| 571 | /* Computation 2t syndromes based on S(x) */ |
| 572 | /* Odd syndromes */ |
| 573 | for (i = 1; i < 2 * cap; i += 2) { |
| 574 | for (j = 0; j < host->pmecc_degree; j++) { |
| 575 | if (partial_syn[i] & ((unsigned short)0x1 << j)) |
| 576 | si[i] = readw_relaxed(alpha_to + i * j) ^ si[i]; |
| 577 | } |
| 578 | } |
| 579 | /* Even syndrome = (Odd syndrome) ** 2 */ |
| 580 | for (i = 2, j = 1; j <= cap; i = ++j << 1) { |
| 581 | if (si[j] == 0) { |
| 582 | si[i] = 0; |
| 583 | } else { |
| 584 | int16_t tmp; |
| 585 | |
| 586 | tmp = readw_relaxed(index_of + si[j]); |
| 587 | tmp = (tmp * 2) % host->pmecc_cw_len; |
| 588 | si[i] = readw_relaxed(alpha_to + tmp); |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | return; |
| 593 | } |
| 594 | |
| 595 | static void pmecc_get_sigma(struct mtd_info *mtd) |
| 596 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 597 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 598 | struct atmel_nand_host *host = nand_get_controller_data(nand_chip); |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 599 | |
| 600 | int16_t *lmu = host->pmecc_lmu; |
| 601 | int16_t *si = host->pmecc_si; |
| 602 | int *mu = host->pmecc_mu; |
| 603 | int *dmu = host->pmecc_dmu; /* Discrepancy */ |
| 604 | int *delta = host->pmecc_delta; /* Delta order */ |
| 605 | int cw_len = host->pmecc_cw_len; |
| 606 | const int16_t cap = host->pmecc_corr_cap; |
| 607 | const int num = 2 * cap + 1; |
| 608 | int16_t __iomem *index_of = host->pmecc_index_of; |
| 609 | int16_t __iomem *alpha_to = host->pmecc_alpha_to; |
| 610 | int i, j, k; |
| 611 | uint32_t dmu_0_count, tmp; |
| 612 | int16_t *smu = host->pmecc_smu; |
| 613 | |
| 614 | /* index of largest delta */ |
| 615 | int ro; |
| 616 | int largest; |
| 617 | int diff; |
| 618 | |
| 619 | dmu_0_count = 0; |
| 620 | |
| 621 | /* First Row */ |
| 622 | |
| 623 | /* Mu */ |
| 624 | mu[0] = -1; |
| 625 | |
| 626 | memset(smu, 0, sizeof(int16_t) * num); |
| 627 | smu[0] = 1; |
| 628 | |
| 629 | /* discrepancy set to 1 */ |
| 630 | dmu[0] = 1; |
| 631 | /* polynom order set to 0 */ |
| 632 | lmu[0] = 0; |
| 633 | delta[0] = (mu[0] * 2 - lmu[0]) >> 1; |
| 634 | |
| 635 | /* Second Row */ |
| 636 | |
| 637 | /* Mu */ |
| 638 | mu[1] = 0; |
| 639 | /* Sigma(x) set to 1 */ |
| 640 | memset(&smu[num], 0, sizeof(int16_t) * num); |
| 641 | smu[num] = 1; |
| 642 | |
| 643 | /* discrepancy set to S1 */ |
| 644 | dmu[1] = si[1]; |
| 645 | |
| 646 | /* polynom order set to 0 */ |
| 647 | lmu[1] = 0; |
| 648 | |
| 649 | delta[1] = (mu[1] * 2 - lmu[1]) >> 1; |
| 650 | |
| 651 | /* Init the Sigma(x) last row */ |
| 652 | memset(&smu[(cap + 1) * num], 0, sizeof(int16_t) * num); |
| 653 | |
| 654 | for (i = 1; i <= cap; i++) { |
| 655 | mu[i + 1] = i << 1; |
| 656 | /* Begin Computing Sigma (Mu+1) and L(mu) */ |
| 657 | /* check if discrepancy is set to 0 */ |
| 658 | if (dmu[i] == 0) { |
| 659 | dmu_0_count++; |
| 660 | |
| 661 | tmp = ((cap - (lmu[i] >> 1) - 1) / 2); |
| 662 | if ((cap - (lmu[i] >> 1) - 1) & 0x1) |
| 663 | tmp += 2; |
| 664 | else |
| 665 | tmp += 1; |
| 666 | |
| 667 | if (dmu_0_count == tmp) { |
| 668 | for (j = 0; j <= (lmu[i] >> 1) + 1; j++) |
| 669 | smu[(cap + 1) * num + j] = |
| 670 | smu[i * num + j]; |
| 671 | |
| 672 | lmu[cap + 1] = lmu[i]; |
| 673 | return; |
| 674 | } |
| 675 | |
| 676 | /* copy polynom */ |
| 677 | for (j = 0; j <= lmu[i] >> 1; j++) |
| 678 | smu[(i + 1) * num + j] = smu[i * num + j]; |
| 679 | |
| 680 | /* copy previous polynom order to the next */ |
| 681 | lmu[i + 1] = lmu[i]; |
| 682 | } else { |
| 683 | ro = 0; |
| 684 | largest = -1; |
| 685 | /* find largest delta with dmu != 0 */ |
| 686 | for (j = 0; j < i; j++) { |
| 687 | if ((dmu[j]) && (delta[j] > largest)) { |
| 688 | largest = delta[j]; |
| 689 | ro = j; |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | /* compute difference */ |
| 694 | diff = (mu[i] - mu[ro]); |
| 695 | |
| 696 | /* Compute degree of the new smu polynomial */ |
| 697 | if ((lmu[i] >> 1) > ((lmu[ro] >> 1) + diff)) |
| 698 | lmu[i + 1] = lmu[i]; |
| 699 | else |
| 700 | lmu[i + 1] = ((lmu[ro] >> 1) + diff) * 2; |
| 701 | |
| 702 | /* Init smu[i+1] with 0 */ |
| 703 | for (k = 0; k < num; k++) |
| 704 | smu[(i + 1) * num + k] = 0; |
| 705 | |
| 706 | /* Compute smu[i+1] */ |
| 707 | for (k = 0; k <= lmu[ro] >> 1; k++) { |
| 708 | int16_t a, b, c; |
| 709 | |
| 710 | if (!(smu[ro * num + k] && dmu[i])) |
| 711 | continue; |
| 712 | a = readw_relaxed(index_of + dmu[i]); |
| 713 | b = readw_relaxed(index_of + dmu[ro]); |
| 714 | c = readw_relaxed(index_of + smu[ro * num + k]); |
| 715 | tmp = a + (cw_len - b) + c; |
| 716 | a = readw_relaxed(alpha_to + tmp % cw_len); |
| 717 | smu[(i + 1) * num + (k + diff)] = a; |
| 718 | } |
| 719 | |
| 720 | for (k = 0; k <= lmu[i] >> 1; k++) |
| 721 | smu[(i + 1) * num + k] ^= smu[i * num + k]; |
| 722 | } |
| 723 | |
| 724 | /* End Computing Sigma (Mu+1) and L(mu) */ |
| 725 | /* In either case compute delta */ |
| 726 | delta[i + 1] = (mu[i + 1] * 2 - lmu[i + 1]) >> 1; |
| 727 | |
| 728 | /* Do not compute discrepancy for the last iteration */ |
| 729 | if (i >= cap) |
| 730 | continue; |
| 731 | |
| 732 | for (k = 0; k <= (lmu[i + 1] >> 1); k++) { |
| 733 | tmp = 2 * (i - 1); |
| 734 | if (k == 0) { |
| 735 | dmu[i + 1] = si[tmp + 3]; |
| 736 | } else if (smu[(i + 1) * num + k] && si[tmp + 3 - k]) { |
| 737 | int16_t a, b, c; |
| 738 | a = readw_relaxed(index_of + |
| 739 | smu[(i + 1) * num + k]); |
| 740 | b = si[2 * (i - 1) + 3 - k]; |
| 741 | c = readw_relaxed(index_of + b); |
| 742 | tmp = a + c; |
| 743 | tmp %= cw_len; |
| 744 | dmu[i + 1] = readw_relaxed(alpha_to + tmp) ^ |
| 745 | dmu[i + 1]; |
| 746 | } |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | return; |
| 751 | } |
| 752 | |
| 753 | static int pmecc_err_location(struct mtd_info *mtd) |
| 754 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 755 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 756 | struct atmel_nand_host *host = nand_get_controller_data(nand_chip); |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 757 | unsigned long end_time; |
| 758 | const int cap = host->pmecc_corr_cap; |
| 759 | const int num = 2 * cap + 1; |
| 760 | int sector_size = host->pmecc_sector_size; |
| 761 | int err_nbr = 0; /* number of error */ |
| 762 | int roots_nbr; /* number of roots */ |
| 763 | int i; |
| 764 | uint32_t val; |
| 765 | int16_t *smu = host->pmecc_smu; |
| 766 | |
| 767 | pmerrloc_writel(host->pmerrloc_base, ELDIS, PMERRLOC_DISABLE); |
| 768 | |
| 769 | for (i = 0; i <= host->pmecc_lmu[cap + 1] >> 1; i++) { |
| 770 | pmerrloc_writel_sigma_relaxed(host->pmerrloc_base, i, |
| 771 | smu[(cap + 1) * num + i]); |
| 772 | err_nbr++; |
| 773 | } |
| 774 | |
| 775 | val = (err_nbr - 1) << 16; |
| 776 | if (sector_size == 1024) |
| 777 | val |= 1; |
| 778 | |
| 779 | pmerrloc_writel(host->pmerrloc_base, ELCFG, val); |
| 780 | pmerrloc_writel(host->pmerrloc_base, ELEN, |
| 781 | sector_size * 8 + host->pmecc_degree * cap); |
| 782 | |
| 783 | end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS); |
| 784 | while (!(pmerrloc_readl_relaxed(host->pmerrloc_base, ELISR) |
| 785 | & PMERRLOC_CALC_DONE)) { |
| 786 | if (unlikely(time_after(jiffies, end_time))) { |
| 787 | dev_err(host->dev, "PMECC: Timeout to calculate error location.\n"); |
| 788 | return -1; |
| 789 | } |
| 790 | cpu_relax(); |
| 791 | } |
| 792 | |
| 793 | roots_nbr = (pmerrloc_readl_relaxed(host->pmerrloc_base, ELISR) |
| 794 | & PMERRLOC_ERR_NUM_MASK) >> 8; |
| 795 | /* Number of roots == degree of smu hence <= cap */ |
| 796 | if (roots_nbr == host->pmecc_lmu[cap + 1] >> 1) |
| 797 | return err_nbr - 1; |
| 798 | |
| 799 | /* Number of roots does not match the degree of smu |
| 800 | * unable to correct error */ |
| 801 | return -1; |
| 802 | } |
| 803 | |
| 804 | static void pmecc_correct_data(struct mtd_info *mtd, uint8_t *buf, uint8_t *ecc, |
| 805 | int sector_num, int extra_bytes, int err_nbr) |
| 806 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 807 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 808 | struct atmel_nand_host *host = nand_get_controller_data(nand_chip); |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 809 | int i = 0; |
| 810 | int byte_pos, bit_pos, sector_size, pos; |
| 811 | uint32_t tmp; |
| 812 | uint8_t err_byte; |
| 813 | |
| 814 | sector_size = host->pmecc_sector_size; |
| 815 | |
| 816 | while (err_nbr) { |
Romain Izard | 5575075 | 2016-02-10 10:56:25 +0100 | [diff] [blame] | 817 | tmp = pmerrloc_readl_el_relaxed(host->pmerrloc_el_base, i) - 1; |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 818 | byte_pos = tmp / 8; |
| 819 | bit_pos = tmp % 8; |
| 820 | |
| 821 | if (byte_pos >= (sector_size + extra_bytes)) |
| 822 | BUG(); /* should never happen */ |
| 823 | |
| 824 | if (byte_pos < sector_size) { |
| 825 | err_byte = *(buf + byte_pos); |
| 826 | *(buf + byte_pos) ^= (1 << bit_pos); |
| 827 | |
| 828 | pos = sector_num * host->pmecc_sector_size + byte_pos; |
Romain Izard | 12197bf | 2016-01-13 17:34:13 +0100 | [diff] [blame] | 829 | dev_dbg(host->dev, "Bit flip in data area, byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n", |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 830 | pos, bit_pos, err_byte, *(buf + byte_pos)); |
| 831 | } else { |
Boris Brezillon | 78d28e8 | 2016-02-03 20:11:14 +0100 | [diff] [blame] | 832 | struct mtd_oob_region oobregion; |
| 833 | |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 834 | /* Bit flip in OOB area */ |
Wu, Josh | 022a478 | 2014-08-08 17:12:35 +0800 | [diff] [blame] | 835 | tmp = sector_num * nand_chip->ecc.bytes |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 836 | + (byte_pos - sector_size); |
| 837 | err_byte = ecc[tmp]; |
| 838 | ecc[tmp] ^= (1 << bit_pos); |
| 839 | |
Boris Brezillon | 78d28e8 | 2016-02-03 20:11:14 +0100 | [diff] [blame] | 840 | mtd_ooblayout_ecc(mtd, 0, &oobregion); |
| 841 | pos = tmp + oobregion.offset; |
Romain Izard | 12197bf | 2016-01-13 17:34:13 +0100 | [diff] [blame] | 842 | dev_dbg(host->dev, "Bit flip in OOB, oob_byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n", |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 843 | pos, bit_pos, err_byte, ecc[tmp]); |
| 844 | } |
| 845 | |
| 846 | i++; |
| 847 | err_nbr--; |
| 848 | } |
| 849 | |
| 850 | return; |
| 851 | } |
| 852 | |
| 853 | static int pmecc_correction(struct mtd_info *mtd, u32 pmecc_stat, uint8_t *buf, |
| 854 | u8 *ecc) |
| 855 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 856 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 857 | struct atmel_nand_host *host = nand_get_controller_data(nand_chip); |
Bo Shen | b385766 | 2014-06-12 15:58:45 +0800 | [diff] [blame] | 858 | int i, err_nbr; |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 859 | uint8_t *buf_pos; |
Wu, Josh | 267d46e | 2015-01-14 11:50:46 +0800 | [diff] [blame] | 860 | int max_bitflips = 0; |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 861 | |
Wu, Josh | c9447ff | 2014-08-08 17:12:34 +0800 | [diff] [blame] | 862 | for (i = 0; i < nand_chip->ecc.steps; i++) { |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 863 | err_nbr = 0; |
| 864 | if (pmecc_stat & 0x1) { |
| 865 | buf_pos = buf + i * host->pmecc_sector_size; |
| 866 | |
| 867 | pmecc_gen_syndrome(mtd, i); |
| 868 | pmecc_substitute(mtd); |
| 869 | pmecc_get_sigma(mtd); |
| 870 | |
| 871 | err_nbr = pmecc_err_location(mtd); |
Boris Brezillon | ff6ee10 | 2016-03-01 14:11:52 +0100 | [diff] [blame] | 872 | if (err_nbr >= 0) { |
| 873 | pmecc_correct_data(mtd, buf_pos, ecc, i, |
| 874 | nand_chip->ecc.bytes, |
| 875 | err_nbr); |
| 876 | } else if (!host->caps->pmecc_correct_erase_page) { |
| 877 | u8 *ecc_pos = ecc + (i * nand_chip->ecc.bytes); |
| 878 | |
| 879 | /* Try to detect erased pages */ |
| 880 | err_nbr = nand_check_erased_ecc_chunk(buf_pos, |
| 881 | host->pmecc_sector_size, |
| 882 | ecc_pos, |
| 883 | nand_chip->ecc.bytes, |
| 884 | NULL, 0, |
| 885 | nand_chip->ecc.strength); |
| 886 | } |
| 887 | |
| 888 | if (err_nbr < 0) { |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 889 | dev_err(host->dev, "PMECC: Too many errors\n"); |
| 890 | mtd->ecc_stats.failed++; |
| 891 | return -EIO; |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 892 | } |
Boris Brezillon | ff6ee10 | 2016-03-01 14:11:52 +0100 | [diff] [blame] | 893 | |
| 894 | mtd->ecc_stats.corrected += err_nbr; |
| 895 | max_bitflips = max_t(int, max_bitflips, err_nbr); |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 896 | } |
| 897 | pmecc_stat >>= 1; |
| 898 | } |
| 899 | |
Wu, Josh | 267d46e | 2015-01-14 11:50:46 +0800 | [diff] [blame] | 900 | return max_bitflips; |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 901 | } |
| 902 | |
Josh Wu | 5ee3d9d | 2013-08-05 19:14:34 +0800 | [diff] [blame] | 903 | static void pmecc_enable(struct atmel_nand_host *host, int ecc_op) |
| 904 | { |
| 905 | u32 val; |
| 906 | |
Josh Wu | 5ee3d9d | 2013-08-05 19:14:34 +0800 | [diff] [blame] | 907 | if (ecc_op != NAND_ECC_READ && ecc_op != NAND_ECC_WRITE) { |
| 908 | dev_err(host->dev, "atmel_nand: wrong pmecc operation type!"); |
| 909 | return; |
| 910 | } |
| 911 | |
Josh Wu | 1fad0e8 | 2013-08-07 17:58:11 +0800 | [diff] [blame] | 912 | pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST); |
| 913 | pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE); |
| 914 | val = pmecc_readl_relaxed(host->ecc, CFG); |
| 915 | |
Josh Wu | 5ee3d9d | 2013-08-05 19:14:34 +0800 | [diff] [blame] | 916 | if (ecc_op == NAND_ECC_READ) |
| 917 | pmecc_writel(host->ecc, CFG, (val & ~PMECC_CFG_WRITE_OP) |
| 918 | | PMECC_CFG_AUTO_ENABLE); |
| 919 | else |
| 920 | pmecc_writel(host->ecc, CFG, (val | PMECC_CFG_WRITE_OP) |
| 921 | & ~PMECC_CFG_AUTO_ENABLE); |
| 922 | |
| 923 | pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE); |
| 924 | pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DATA); |
| 925 | } |
| 926 | |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 927 | static int atmel_nand_pmecc_read_page(struct mtd_info *mtd, |
| 928 | struct nand_chip *chip, uint8_t *buf, int oob_required, int page) |
| 929 | { |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 930 | struct atmel_nand_host *host = nand_get_controller_data(chip); |
Bo Shen | b385766 | 2014-06-12 15:58:45 +0800 | [diff] [blame] | 931 | int eccsize = chip->ecc.size * chip->ecc.steps; |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 932 | uint8_t *oob = chip->oob_poi; |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 933 | uint32_t stat; |
| 934 | unsigned long end_time; |
Josh Wu | c0c70d9 | 2012-11-27 18:50:31 +0800 | [diff] [blame] | 935 | int bitflips = 0; |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 936 | |
Josh Wu | 1ae9c09 | 2013-08-05 19:14:36 +0800 | [diff] [blame] | 937 | if (!host->nfc || !host->nfc->use_nfc_sram) |
| 938 | pmecc_enable(host, NAND_ECC_READ); |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 939 | |
| 940 | chip->read_buf(mtd, buf, eccsize); |
| 941 | chip->read_buf(mtd, oob, mtd->oobsize); |
| 942 | |
| 943 | end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS); |
| 944 | while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) { |
| 945 | if (unlikely(time_after(jiffies, end_time))) { |
| 946 | dev_err(host->dev, "PMECC: Timeout to get error status.\n"); |
| 947 | return -EIO; |
| 948 | } |
| 949 | cpu_relax(); |
| 950 | } |
| 951 | |
| 952 | stat = pmecc_readl_relaxed(host->ecc, ISR); |
Josh Wu | c0c70d9 | 2012-11-27 18:50:31 +0800 | [diff] [blame] | 953 | if (stat != 0) { |
Boris Brezillon | 78d28e8 | 2016-02-03 20:11:14 +0100 | [diff] [blame] | 954 | struct mtd_oob_region oobregion; |
| 955 | |
| 956 | mtd_ooblayout_ecc(mtd, 0, &oobregion); |
| 957 | bitflips = pmecc_correction(mtd, stat, buf, |
| 958 | &oob[oobregion.offset]); |
Josh Wu | c0c70d9 | 2012-11-27 18:50:31 +0800 | [diff] [blame] | 959 | if (bitflips < 0) |
| 960 | /* uncorrectable errors */ |
| 961 | return 0; |
| 962 | } |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 963 | |
Josh Wu | c0c70d9 | 2012-11-27 18:50:31 +0800 | [diff] [blame] | 964 | return bitflips; |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | static int atmel_nand_pmecc_write_page(struct mtd_info *mtd, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 968 | struct nand_chip *chip, const uint8_t *buf, int oob_required, |
| 969 | int page) |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 970 | { |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 971 | struct atmel_nand_host *host = nand_get_controller_data(chip); |
Boris Brezillon | 78d28e8 | 2016-02-03 20:11:14 +0100 | [diff] [blame] | 972 | struct mtd_oob_region oobregion = { }; |
| 973 | int i, j, section = 0; |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 974 | unsigned long end_time; |
| 975 | |
Josh Wu | 6054d4d | 2013-08-05 19:14:37 +0800 | [diff] [blame] | 976 | if (!host->nfc || !host->nfc->write_by_sram) { |
| 977 | pmecc_enable(host, NAND_ECC_WRITE); |
| 978 | chip->write_buf(mtd, (u8 *)buf, mtd->writesize); |
| 979 | } |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 980 | |
| 981 | end_time = jiffies + msecs_to_jiffies(PMECC_MAX_TIMEOUT_MS); |
| 982 | while ((pmecc_readl_relaxed(host->ecc, SR) & PMECC_SR_BUSY)) { |
| 983 | if (unlikely(time_after(jiffies, end_time))) { |
| 984 | dev_err(host->dev, "PMECC: Timeout to get ECC value.\n"); |
| 985 | return -EIO; |
| 986 | } |
| 987 | cpu_relax(); |
| 988 | } |
| 989 | |
Wu, Josh | c9447ff | 2014-08-08 17:12:34 +0800 | [diff] [blame] | 990 | for (i = 0; i < chip->ecc.steps; i++) { |
Wu, Josh | 022a478 | 2014-08-08 17:12:35 +0800 | [diff] [blame] | 991 | for (j = 0; j < chip->ecc.bytes; j++) { |
Boris Brezillon | 78d28e8 | 2016-02-03 20:11:14 +0100 | [diff] [blame] | 992 | if (!oobregion.length) |
| 993 | mtd_ooblayout_ecc(mtd, section, &oobregion); |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 994 | |
Boris Brezillon | 78d28e8 | 2016-02-03 20:11:14 +0100 | [diff] [blame] | 995 | chip->oob_poi[oobregion.offset] = |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 996 | pmecc_readb_ecc_relaxed(host->ecc, i, j); |
Boris Brezillon | 78d28e8 | 2016-02-03 20:11:14 +0100 | [diff] [blame] | 997 | oobregion.length--; |
| 998 | oobregion.offset++; |
| 999 | section++; |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 1000 | } |
| 1001 | } |
| 1002 | chip->write_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 1003 | |
| 1004 | return 0; |
| 1005 | } |
| 1006 | |
| 1007 | static void atmel_pmecc_core_init(struct mtd_info *mtd) |
| 1008 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 1009 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 1010 | struct atmel_nand_host *host = nand_get_controller_data(nand_chip); |
Boris Brezillon | cc00383 | 2016-02-03 19:59:11 +0100 | [diff] [blame^] | 1011 | int eccbytes = mtd_ooblayout_count_eccbytes(mtd); |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 1012 | uint32_t val = 0; |
Boris Brezillon | 78d28e8 | 2016-02-03 20:11:14 +0100 | [diff] [blame] | 1013 | struct mtd_oob_region oobregion; |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 1014 | |
| 1015 | pmecc_writel(host->ecc, CTRL, PMECC_CTRL_RST); |
| 1016 | pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE); |
| 1017 | |
| 1018 | switch (host->pmecc_corr_cap) { |
| 1019 | case 2: |
| 1020 | val = PMECC_CFG_BCH_ERR2; |
| 1021 | break; |
| 1022 | case 4: |
| 1023 | val = PMECC_CFG_BCH_ERR4; |
| 1024 | break; |
| 1025 | case 8: |
| 1026 | val = PMECC_CFG_BCH_ERR8; |
| 1027 | break; |
| 1028 | case 12: |
| 1029 | val = PMECC_CFG_BCH_ERR12; |
| 1030 | break; |
| 1031 | case 24: |
| 1032 | val = PMECC_CFG_BCH_ERR24; |
| 1033 | break; |
Romain Izard | 9424846 | 2016-02-10 10:56:26 +0100 | [diff] [blame] | 1034 | case 32: |
| 1035 | val = PMECC_CFG_BCH_ERR32; |
| 1036 | break; |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | if (host->pmecc_sector_size == 512) |
| 1040 | val |= PMECC_CFG_SECTOR512; |
| 1041 | else if (host->pmecc_sector_size == 1024) |
| 1042 | val |= PMECC_CFG_SECTOR1024; |
| 1043 | |
Wu, Josh | c9447ff | 2014-08-08 17:12:34 +0800 | [diff] [blame] | 1044 | switch (nand_chip->ecc.steps) { |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 1045 | case 1: |
| 1046 | val |= PMECC_CFG_PAGE_1SECTOR; |
| 1047 | break; |
| 1048 | case 2: |
| 1049 | val |= PMECC_CFG_PAGE_2SECTORS; |
| 1050 | break; |
| 1051 | case 4: |
| 1052 | val |= PMECC_CFG_PAGE_4SECTORS; |
| 1053 | break; |
| 1054 | case 8: |
| 1055 | val |= PMECC_CFG_PAGE_8SECTORS; |
| 1056 | break; |
| 1057 | } |
| 1058 | |
| 1059 | val |= (PMECC_CFG_READ_OP | PMECC_CFG_SPARE_DISABLE |
| 1060 | | PMECC_CFG_AUTO_DISABLE); |
| 1061 | pmecc_writel(host->ecc, CFG, val); |
| 1062 | |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 1063 | pmecc_writel(host->ecc, SAREA, mtd->oobsize - 1); |
Boris Brezillon | 78d28e8 | 2016-02-03 20:11:14 +0100 | [diff] [blame] | 1064 | mtd_ooblayout_ecc(mtd, 0, &oobregion); |
| 1065 | pmecc_writel(host->ecc, SADDR, oobregion.offset); |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 1066 | pmecc_writel(host->ecc, EADDR, |
Boris Brezillon | cc00383 | 2016-02-03 19:59:11 +0100 | [diff] [blame^] | 1067 | oobregion.offset + eccbytes - 1); |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 1068 | /* See datasheet about PMECC Clock Control Register */ |
| 1069 | pmecc_writel(host->ecc, CLK, 2); |
| 1070 | pmecc_writel(host->ecc, IDR, 0xff); |
| 1071 | pmecc_writel(host->ecc, CTRL, PMECC_CTRL_ENABLE); |
| 1072 | } |
| 1073 | |
Josh Wu | 84cfbbb | 2013-01-23 20:47:12 +0800 | [diff] [blame] | 1074 | /* |
Josh Wu | 2a3d933 | 2013-09-18 13:58:48 +0800 | [diff] [blame] | 1075 | * Get minimum ecc requirements from NAND. |
Josh Wu | 84cfbbb | 2013-01-23 20:47:12 +0800 | [diff] [blame] | 1076 | * If pmecc-cap, pmecc-sector-size in DTS are not specified, this function |
Josh Wu | 2a3d933 | 2013-09-18 13:58:48 +0800 | [diff] [blame] | 1077 | * will set them according to minimum ecc requirement. Otherwise, use the |
Josh Wu | 84cfbbb | 2013-01-23 20:47:12 +0800 | [diff] [blame] | 1078 | * value in DTS file. |
| 1079 | * return 0 if success. otherwise return error code. |
| 1080 | */ |
| 1081 | static int pmecc_choose_ecc(struct atmel_nand_host *host, |
| 1082 | int *cap, int *sector_size) |
| 1083 | { |
Josh Wu | 2a3d933 | 2013-09-18 13:58:48 +0800 | [diff] [blame] | 1084 | /* Get minimum ECC requirements */ |
| 1085 | if (host->nand_chip.ecc_strength_ds) { |
| 1086 | *cap = host->nand_chip.ecc_strength_ds; |
| 1087 | *sector_size = host->nand_chip.ecc_step_ds; |
| 1088 | dev_info(host->dev, "minimum ECC: %d bits in %d bytes\n", |
Josh Wu | 84cfbbb | 2013-01-23 20:47:12 +0800 | [diff] [blame] | 1089 | *cap, *sector_size); |
Josh Wu | 84cfbbb | 2013-01-23 20:47:12 +0800 | [diff] [blame] | 1090 | } else { |
Josh Wu | 84cfbbb | 2013-01-23 20:47:12 +0800 | [diff] [blame] | 1091 | *cap = 2; |
| 1092 | *sector_size = 512; |
Josh Wu | 2a3d933 | 2013-09-18 13:58:48 +0800 | [diff] [blame] | 1093 | dev_info(host->dev, "can't detect min. ECC, assume 2 bits in 512 bytes\n"); |
Josh Wu | 84cfbbb | 2013-01-23 20:47:12 +0800 | [diff] [blame] | 1094 | } |
| 1095 | |
Josh Wu | 2a3d933 | 2013-09-18 13:58:48 +0800 | [diff] [blame] | 1096 | /* If device tree doesn't specify, use NAND's minimum ECC parameters */ |
Josh Wu | 84cfbbb | 2013-01-23 20:47:12 +0800 | [diff] [blame] | 1097 | if (host->pmecc_corr_cap == 0) { |
Romain Izard | 9424846 | 2016-02-10 10:56:26 +0100 | [diff] [blame] | 1098 | if (*cap > host->caps->pmecc_max_correction) |
| 1099 | return -EINVAL; |
| 1100 | |
Josh Wu | 84cfbbb | 2013-01-23 20:47:12 +0800 | [diff] [blame] | 1101 | /* use the most fitable ecc bits (the near bigger one ) */ |
| 1102 | if (*cap <= 2) |
| 1103 | host->pmecc_corr_cap = 2; |
| 1104 | else if (*cap <= 4) |
| 1105 | host->pmecc_corr_cap = 4; |
Josh Wu | edc9cba | 2013-07-03 17:56:19 +0800 | [diff] [blame] | 1106 | else if (*cap <= 8) |
Josh Wu | 84cfbbb | 2013-01-23 20:47:12 +0800 | [diff] [blame] | 1107 | host->pmecc_corr_cap = 8; |
Josh Wu | edc9cba | 2013-07-03 17:56:19 +0800 | [diff] [blame] | 1108 | else if (*cap <= 12) |
Josh Wu | 84cfbbb | 2013-01-23 20:47:12 +0800 | [diff] [blame] | 1109 | host->pmecc_corr_cap = 12; |
Josh Wu | edc9cba | 2013-07-03 17:56:19 +0800 | [diff] [blame] | 1110 | else if (*cap <= 24) |
Josh Wu | 84cfbbb | 2013-01-23 20:47:12 +0800 | [diff] [blame] | 1111 | host->pmecc_corr_cap = 24; |
Romain Izard | 9424846 | 2016-02-10 10:56:26 +0100 | [diff] [blame] | 1112 | else if (*cap <= 32) |
| 1113 | host->pmecc_corr_cap = 32; |
Josh Wu | 84cfbbb | 2013-01-23 20:47:12 +0800 | [diff] [blame] | 1114 | else |
| 1115 | return -EINVAL; |
| 1116 | } |
| 1117 | if (host->pmecc_sector_size == 0) { |
| 1118 | /* use the most fitable sector size (the near smaller one ) */ |
| 1119 | if (*sector_size >= 1024) |
| 1120 | host->pmecc_sector_size = 1024; |
| 1121 | else if (*sector_size >= 512) |
| 1122 | host->pmecc_sector_size = 512; |
| 1123 | else |
| 1124 | return -EINVAL; |
| 1125 | } |
| 1126 | return 0; |
| 1127 | } |
| 1128 | |
Josh Wu | abb1cd0 | 2014-10-11 18:01:50 +0800 | [diff] [blame] | 1129 | static inline int deg(unsigned int poly) |
| 1130 | { |
| 1131 | /* polynomial degree is the most-significant bit index */ |
| 1132 | return fls(poly) - 1; |
| 1133 | } |
| 1134 | |
| 1135 | static int build_gf_tables(int mm, unsigned int poly, |
| 1136 | int16_t *index_of, int16_t *alpha_to) |
| 1137 | { |
| 1138 | unsigned int i, x = 1; |
| 1139 | const unsigned int k = 1 << deg(poly); |
| 1140 | unsigned int nn = (1 << mm) - 1; |
| 1141 | |
| 1142 | /* primitive polynomial must be of degree m */ |
| 1143 | if (k != (1u << mm)) |
| 1144 | return -EINVAL; |
| 1145 | |
| 1146 | for (i = 0; i < nn; i++) { |
| 1147 | alpha_to[i] = x; |
| 1148 | index_of[x] = i; |
| 1149 | if (i && (x == 1)) |
| 1150 | /* polynomial is not primitive (a^i=1 with 0<i<2^m-1) */ |
| 1151 | return -EINVAL; |
| 1152 | x <<= 1; |
| 1153 | if (x & k) |
| 1154 | x ^= poly; |
| 1155 | } |
| 1156 | alpha_to[nn] = 1; |
| 1157 | index_of[0] = 0; |
| 1158 | |
| 1159 | return 0; |
| 1160 | } |
| 1161 | |
| 1162 | static uint16_t *create_lookup_table(struct device *dev, int sector_size) |
| 1163 | { |
| 1164 | int degree = (sector_size == 512) ? |
| 1165 | PMECC_GF_DIMENSION_13 : |
| 1166 | PMECC_GF_DIMENSION_14; |
| 1167 | unsigned int poly = (sector_size == 512) ? |
| 1168 | PMECC_GF_13_PRIMITIVE_POLY : |
| 1169 | PMECC_GF_14_PRIMITIVE_POLY; |
| 1170 | int table_size = (sector_size == 512) ? |
| 1171 | PMECC_LOOKUP_TABLE_SIZE_512 : |
| 1172 | PMECC_LOOKUP_TABLE_SIZE_1024; |
| 1173 | |
| 1174 | int16_t *addr = devm_kzalloc(dev, 2 * table_size * sizeof(uint16_t), |
| 1175 | GFP_KERNEL); |
| 1176 | if (addr && build_gf_tables(degree, poly, addr, addr + table_size)) |
| 1177 | return NULL; |
| 1178 | |
| 1179 | return addr; |
| 1180 | } |
| 1181 | |
Johan Hovold | 2c2b928 | 2013-09-23 16:27:28 +0200 | [diff] [blame] | 1182 | static int atmel_pmecc_nand_init_params(struct platform_device *pdev, |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 1183 | struct atmel_nand_host *host) |
| 1184 | { |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 1185 | struct nand_chip *nand_chip = &host->nand_chip; |
Boris BREZILLON | ac01efe | 2015-12-10 08:59:50 +0100 | [diff] [blame] | 1186 | struct mtd_info *mtd = nand_to_mtd(nand_chip); |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 1187 | struct resource *regs, *regs_pmerr, *regs_rom; |
Josh Wu | abb1cd0 | 2014-10-11 18:01:50 +0800 | [diff] [blame] | 1188 | uint16_t *galois_table; |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 1189 | int cap, sector_size, err_no; |
| 1190 | |
Josh Wu | 84cfbbb | 2013-01-23 20:47:12 +0800 | [diff] [blame] | 1191 | err_no = pmecc_choose_ecc(host, &cap, §or_size); |
| 1192 | if (err_no) { |
| 1193 | dev_err(host->dev, "The NAND flash's ECC requirement are not support!"); |
| 1194 | return err_no; |
| 1195 | } |
| 1196 | |
Richard Genoud | f666d64 | 2013-07-30 17:17:29 +0200 | [diff] [blame] | 1197 | if (cap > host->pmecc_corr_cap || |
Josh Wu | 84cfbbb | 2013-01-23 20:47:12 +0800 | [diff] [blame] | 1198 | sector_size != host->pmecc_sector_size) |
| 1199 | dev_info(host->dev, "WARNING: Be Caution! Using different PMECC parameters from Nand ONFI ECC reqirement.\n"); |
Josh Wu | e66b431 | 2013-01-23 20:47:11 +0800 | [diff] [blame] | 1200 | |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 1201 | cap = host->pmecc_corr_cap; |
| 1202 | sector_size = host->pmecc_sector_size; |
Josh Wu | e66b431 | 2013-01-23 20:47:11 +0800 | [diff] [blame] | 1203 | host->pmecc_lookup_table_offset = (sector_size == 512) ? |
| 1204 | host->pmecc_lookup_table_offset_512 : |
| 1205 | host->pmecc_lookup_table_offset_1024; |
| 1206 | |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 1207 | dev_info(host->dev, "Initialize PMECC params, cap: %d, sector: %d\n", |
| 1208 | cap, sector_size); |
| 1209 | |
| 1210 | regs = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 1211 | if (!regs) { |
| 1212 | dev_warn(host->dev, |
| 1213 | "Can't get I/O resource regs for PMECC controller, rolling back on software ECC\n"); |
| 1214 | nand_chip->ecc.mode = NAND_ECC_SOFT; |
| 1215 | return 0; |
| 1216 | } |
| 1217 | |
Jean-Christophe PLAGNIOL-VILLARD | 0d63748 | 2013-08-05 19:14:33 +0800 | [diff] [blame] | 1218 | host->ecc = devm_ioremap_resource(&pdev->dev, regs); |
| 1219 | if (IS_ERR(host->ecc)) { |
Jean-Christophe PLAGNIOL-VILLARD | 0d63748 | 2013-08-05 19:14:33 +0800 | [diff] [blame] | 1220 | err_no = PTR_ERR(host->ecc); |
| 1221 | goto err; |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | regs_pmerr = platform_get_resource(pdev, IORESOURCE_MEM, 2); |
Jean-Christophe PLAGNIOL-VILLARD | 0d63748 | 2013-08-05 19:14:33 +0800 | [diff] [blame] | 1225 | host->pmerrloc_base = devm_ioremap_resource(&pdev->dev, regs_pmerr); |
| 1226 | if (IS_ERR(host->pmerrloc_base)) { |
Jean-Christophe PLAGNIOL-VILLARD | 0d63748 | 2013-08-05 19:14:33 +0800 | [diff] [blame] | 1227 | err_no = PTR_ERR(host->pmerrloc_base); |
| 1228 | goto err; |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 1229 | } |
Romain Izard | 5575075 | 2016-02-10 10:56:25 +0100 | [diff] [blame] | 1230 | host->pmerrloc_el_base = host->pmerrloc_base + ATMEL_PMERRLOC_SIGMAx + |
| 1231 | (host->caps->pmecc_max_correction + 1) * 4; |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 1232 | |
Wu, Josh | 41c7540 | 2015-04-02 14:13:47 +0800 | [diff] [blame] | 1233 | if (!host->has_no_lookup_table) { |
| 1234 | regs_rom = platform_get_resource(pdev, IORESOURCE_MEM, 3); |
| 1235 | host->pmecc_rom_base = devm_ioremap_resource(&pdev->dev, |
| 1236 | regs_rom); |
| 1237 | if (IS_ERR(host->pmecc_rom_base)) { |
Josh Wu | abb1cd0 | 2014-10-11 18:01:50 +0800 | [diff] [blame] | 1238 | dev_err(host->dev, "Can not get I/O resource for ROM, will build a lookup table in runtime!\n"); |
Wu, Josh | 41c7540 | 2015-04-02 14:13:47 +0800 | [diff] [blame] | 1239 | host->has_no_lookup_table = true; |
| 1240 | } |
Josh Wu | abb1cd0 | 2014-10-11 18:01:50 +0800 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | if (host->has_no_lookup_table) { |
| 1244 | /* Build the look-up table in runtime */ |
| 1245 | galois_table = create_lookup_table(host->dev, sector_size); |
| 1246 | if (!galois_table) { |
| 1247 | dev_err(host->dev, "Failed to build a lookup table in runtime!\n"); |
| 1248 | err_no = -EINVAL; |
| 1249 | goto err; |
| 1250 | } |
| 1251 | |
| 1252 | host->pmecc_rom_base = (void __iomem *)galois_table; |
| 1253 | host->pmecc_lookup_table_offset = 0; |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 1254 | } |
| 1255 | |
Bo Shen | b385766 | 2014-06-12 15:58:45 +0800 | [diff] [blame] | 1256 | nand_chip->ecc.size = sector_size; |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 1257 | |
| 1258 | /* set ECC page size and oob layout */ |
| 1259 | switch (mtd->writesize) { |
Wu, Josh | a355710 | 2014-07-22 17:24:18 +0800 | [diff] [blame] | 1260 | case 512: |
| 1261 | case 1024: |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 1262 | case 2048: |
Wu, Josh | a355710 | 2014-07-22 17:24:18 +0800 | [diff] [blame] | 1263 | case 4096: |
| 1264 | case 8192: |
| 1265 | if (sector_size > mtd->writesize) { |
| 1266 | dev_err(host->dev, "pmecc sector size is bigger than the page size!\n"); |
| 1267 | err_no = -EINVAL; |
| 1268 | goto err; |
| 1269 | } |
| 1270 | |
Josh Wu | 2fa831f | 2013-08-19 18:05:44 +0800 | [diff] [blame] | 1271 | host->pmecc_degree = (sector_size == 512) ? |
| 1272 | PMECC_GF_DIMENSION_13 : PMECC_GF_DIMENSION_14; |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 1273 | host->pmecc_cw_len = (1 << host->pmecc_degree) - 1; |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 1274 | host->pmecc_alpha_to = pmecc_get_alpha_to(host); |
| 1275 | host->pmecc_index_of = host->pmecc_rom_base + |
| 1276 | host->pmecc_lookup_table_offset; |
| 1277 | |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 1278 | nand_chip->ecc.strength = cap; |
Wu, Josh | 022a478 | 2014-08-08 17:12:35 +0800 | [diff] [blame] | 1279 | nand_chip->ecc.bytes = pmecc_get_ecc_bytes(cap, sector_size); |
Wu, Josh | c9447ff | 2014-08-08 17:12:34 +0800 | [diff] [blame] | 1280 | nand_chip->ecc.steps = mtd->writesize / sector_size; |
| 1281 | nand_chip->ecc.total = nand_chip->ecc.bytes * |
| 1282 | nand_chip->ecc.steps; |
Josh Wu | 477478a | 2015-04-02 14:12:33 +0800 | [diff] [blame] | 1283 | if (nand_chip->ecc.total > |
| 1284 | mtd->oobsize - PMECC_OOB_RESERVED_BYTES) { |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 1285 | dev_err(host->dev, "No room for ECC bytes\n"); |
| 1286 | err_no = -EINVAL; |
Jean-Christophe PLAGNIOL-VILLARD | 0d63748 | 2013-08-05 19:14:33 +0800 | [diff] [blame] | 1287 | goto err; |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 1288 | } |
Bo Shen | b385766 | 2014-06-12 15:58:45 +0800 | [diff] [blame] | 1289 | |
Boris Brezillon | cc00383 | 2016-02-03 19:59:11 +0100 | [diff] [blame^] | 1290 | mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops); |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 1291 | break; |
Wu, Josh | a355710 | 2014-07-22 17:24:18 +0800 | [diff] [blame] | 1292 | default: |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 1293 | dev_warn(host->dev, |
| 1294 | "Unsupported page size for PMECC, use Software ECC\n"); |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 1295 | /* page size not handled by HW ECC */ |
| 1296 | /* switching back to soft ECC */ |
| 1297 | nand_chip->ecc.mode = NAND_ECC_SOFT; |
| 1298 | return 0; |
| 1299 | } |
| 1300 | |
| 1301 | /* Allocate data for PMECC computation */ |
| 1302 | err_no = pmecc_data_alloc(host); |
| 1303 | if (err_no) { |
| 1304 | dev_err(host->dev, |
| 1305 | "Cannot allocate memory for PMECC computation!\n"); |
Jean-Christophe PLAGNIOL-VILLARD | 0d63748 | 2013-08-05 19:14:33 +0800 | [diff] [blame] | 1306 | goto err; |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 1307 | } |
| 1308 | |
Herve Codina | 90445ff | 2014-03-03 12:15:29 +0100 | [diff] [blame] | 1309 | nand_chip->options |= NAND_NO_SUBPAGE_WRITE; |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 1310 | nand_chip->ecc.read_page = atmel_nand_pmecc_read_page; |
| 1311 | nand_chip->ecc.write_page = atmel_nand_pmecc_write_page; |
| 1312 | |
| 1313 | atmel_pmecc_core_init(mtd); |
| 1314 | |
| 1315 | return 0; |
| 1316 | |
Jean-Christophe PLAGNIOL-VILLARD | 0d63748 | 2013-08-05 19:14:33 +0800 | [diff] [blame] | 1317 | err: |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 1318 | return err_no; |
| 1319 | } |
| 1320 | |
| 1321 | /* |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1322 | * Calculate HW ECC |
| 1323 | * |
| 1324 | * function called after a write |
| 1325 | * |
| 1326 | * mtd: MTD block structure |
| 1327 | * dat: raw data (unused) |
| 1328 | * ecc_code: buffer for ECC |
| 1329 | */ |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 1330 | static int atmel_nand_calculate(struct mtd_info *mtd, |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1331 | const u_char *dat, unsigned char *ecc_code) |
| 1332 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 1333 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 1334 | struct atmel_nand_host *host = nand_get_controller_data(nand_chip); |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1335 | unsigned int ecc_value; |
| 1336 | |
| 1337 | /* get the first 2 ECC bytes */ |
Richard Genoud | d43fa14 | 2008-04-25 09:32:26 +0200 | [diff] [blame] | 1338 | ecc_value = ecc_readl(host->ecc, PR); |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1339 | |
Richard Genoud | 3fc2389 | 2008-10-12 08:42:28 +0200 | [diff] [blame] | 1340 | ecc_code[0] = ecc_value & 0xFF; |
| 1341 | ecc_code[1] = (ecc_value >> 8) & 0xFF; |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1342 | |
| 1343 | /* get the last 2 ECC bytes */ |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 1344 | ecc_value = ecc_readl(host->ecc, NPR) & ATMEL_ECC_NPARITY; |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1345 | |
Richard Genoud | 3fc2389 | 2008-10-12 08:42:28 +0200 | [diff] [blame] | 1346 | ecc_code[2] = ecc_value & 0xFF; |
| 1347 | ecc_code[3] = (ecc_value >> 8) & 0xFF; |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1348 | |
| 1349 | return 0; |
| 1350 | } |
| 1351 | |
| 1352 | /* |
| 1353 | * HW ECC read page function |
| 1354 | * |
| 1355 | * mtd: mtd info structure |
| 1356 | * chip: nand chip info structure |
| 1357 | * buf: buffer to store read data |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1358 | * oob_required: caller expects OOB data read to chip->oob_poi |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1359 | */ |
Brian Norris | 1fbb938 | 2012-05-02 10:14:55 -0700 | [diff] [blame] | 1360 | static int atmel_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip, |
| 1361 | uint8_t *buf, int oob_required, int page) |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1362 | { |
| 1363 | int eccsize = chip->ecc.size; |
| 1364 | int eccbytes = chip->ecc.bytes; |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1365 | uint8_t *p = buf; |
| 1366 | uint8_t *oob = chip->oob_poi; |
| 1367 | uint8_t *ecc_pos; |
| 1368 | int stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1369 | unsigned int max_bitflips = 0; |
Boris Brezillon | 78d28e8 | 2016-02-03 20:11:14 +0100 | [diff] [blame] | 1370 | struct mtd_oob_region oobregion = {}; |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1371 | |
Haavard Skinnemoen | d6248fd | 2008-07-03 23:40:18 -0700 | [diff] [blame] | 1372 | /* |
| 1373 | * Errata: ALE is incorrectly wired up to the ECC controller |
| 1374 | * on the AP7000, so it will include the address cycles in the |
| 1375 | * ECC calculation. |
| 1376 | * |
| 1377 | * Workaround: Reset the parity registers before reading the |
| 1378 | * actual data. |
| 1379 | */ |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 1380 | struct atmel_nand_host *host = nand_get_controller_data(chip); |
Josh Wu | 71b94e2 | 2013-05-09 15:34:54 +0800 | [diff] [blame] | 1381 | if (host->board.need_reset_workaround) |
Haavard Skinnemoen | d6248fd | 2008-07-03 23:40:18 -0700 | [diff] [blame] | 1382 | ecc_writel(host->ecc, CR, ATMEL_ECC_RST); |
Haavard Skinnemoen | d6248fd | 2008-07-03 23:40:18 -0700 | [diff] [blame] | 1383 | |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1384 | /* read the page */ |
| 1385 | chip->read_buf(mtd, p, eccsize); |
| 1386 | |
| 1387 | /* move to ECC position if needed */ |
Boris Brezillon | 78d28e8 | 2016-02-03 20:11:14 +0100 | [diff] [blame] | 1388 | mtd_ooblayout_ecc(mtd, 0, &oobregion); |
| 1389 | if (oobregion.offset != 0) { |
| 1390 | /* |
| 1391 | * This only works on large pages because the ECC controller |
| 1392 | * waits for NAND_CMD_RNDOUTSTART after the NAND_CMD_RNDOUT. |
| 1393 | * Anyway, for small pages, the first ECC byte is at offset |
| 1394 | * 0 in the OOB area. |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1395 | */ |
| 1396 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, |
Boris Brezillon | 78d28e8 | 2016-02-03 20:11:14 +0100 | [diff] [blame] | 1397 | mtd->writesize + oobregion.offset, -1); |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | /* the ECC controller needs to read the ECC just after the data */ |
Boris Brezillon | 78d28e8 | 2016-02-03 20:11:14 +0100 | [diff] [blame] | 1401 | ecc_pos = oob + oobregion.offset; |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1402 | chip->read_buf(mtd, ecc_pos, eccbytes); |
| 1403 | |
| 1404 | /* check if there's an error */ |
| 1405 | stat = chip->ecc.correct(mtd, p, oob, NULL); |
| 1406 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1407 | if (stat < 0) { |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1408 | mtd->ecc_stats.failed++; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1409 | } else { |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1410 | mtd->ecc_stats.corrected += stat; |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1411 | max_bitflips = max_t(unsigned int, max_bitflips, stat); |
| 1412 | } |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1413 | |
| 1414 | /* get back to oob start (end of page) */ |
| 1415 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1); |
| 1416 | |
| 1417 | /* read the oob */ |
| 1418 | chip->read_buf(mtd, oob, mtd->oobsize); |
| 1419 | |
Mike Dunn | 3f91e94 | 2012-04-25 12:06:09 -0700 | [diff] [blame] | 1420 | return max_bitflips; |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1421 | } |
| 1422 | |
| 1423 | /* |
| 1424 | * HW ECC Correction |
| 1425 | * |
| 1426 | * function called after a read |
| 1427 | * |
| 1428 | * mtd: MTD block structure |
| 1429 | * dat: raw data read from the chip |
| 1430 | * read_ecc: ECC from the chip (unused) |
| 1431 | * isnull: unused |
| 1432 | * |
| 1433 | * Detect and correct a 1 bit error for a page |
| 1434 | */ |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 1435 | static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat, |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1436 | u_char *read_ecc, u_char *isnull) |
| 1437 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 1438 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 1439 | struct atmel_nand_host *host = nand_get_controller_data(nand_chip); |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1440 | unsigned int ecc_status; |
| 1441 | unsigned int ecc_word, ecc_bit; |
| 1442 | |
| 1443 | /* get the status from the Status Register */ |
| 1444 | ecc_status = ecc_readl(host->ecc, SR); |
| 1445 | |
| 1446 | /* if there's no error */ |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 1447 | if (likely(!(ecc_status & ATMEL_ECC_RECERR))) |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1448 | return 0; |
| 1449 | |
| 1450 | /* get error bit offset (4 bits) */ |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 1451 | ecc_bit = ecc_readl(host->ecc, PR) & ATMEL_ECC_BITADDR; |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1452 | /* get word address (12 bits) */ |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 1453 | ecc_word = ecc_readl(host->ecc, PR) & ATMEL_ECC_WORDADDR; |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1454 | ecc_word >>= 4; |
| 1455 | |
| 1456 | /* if there are multiple errors */ |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 1457 | if (ecc_status & ATMEL_ECC_MULERR) { |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1458 | /* check if it is a freshly erased block |
| 1459 | * (filled with 0xff) */ |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 1460 | if ((ecc_bit == ATMEL_ECC_BITADDR) |
| 1461 | && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) { |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1462 | /* the block has just been erased, return OK */ |
| 1463 | return 0; |
| 1464 | } |
| 1465 | /* it doesn't seems to be a freshly |
| 1466 | * erased block. |
| 1467 | * We can't correct so many errors */ |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 1468 | dev_dbg(host->dev, "atmel_nand : multiple errors detected." |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1469 | " Unable to correct.\n"); |
Boris BREZILLON | 6e94119 | 2015-12-30 20:32:03 +0100 | [diff] [blame] | 1470 | return -EBADMSG; |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1471 | } |
| 1472 | |
| 1473 | /* if there's a single bit error : we can correct it */ |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 1474 | if (ecc_status & ATMEL_ECC_ECCERR) { |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1475 | /* there's nothing much to do here. |
| 1476 | * the bit error is on the ECC itself. |
| 1477 | */ |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 1478 | dev_dbg(host->dev, "atmel_nand : one bit error on ECC code." |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1479 | " Nothing to correct\n"); |
| 1480 | return 0; |
| 1481 | } |
| 1482 | |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 1483 | dev_dbg(host->dev, "atmel_nand : one bit error on data." |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1484 | " (word offset in the page :" |
| 1485 | " 0x%x bit offset : 0x%x)\n", |
| 1486 | ecc_word, ecc_bit); |
| 1487 | /* correct the error */ |
| 1488 | if (nand_chip->options & NAND_BUSWIDTH_16) { |
| 1489 | /* 16 bits words */ |
| 1490 | ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit); |
| 1491 | } else { |
| 1492 | /* 8 bits words */ |
| 1493 | dat[ecc_word] ^= (1 << ecc_bit); |
| 1494 | } |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 1495 | dev_dbg(host->dev, "atmel_nand : error corrected\n"); |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1496 | return 1; |
| 1497 | } |
| 1498 | |
| 1499 | /* |
Haavard Skinnemoen | d6248fd | 2008-07-03 23:40:18 -0700 | [diff] [blame] | 1500 | * Enable HW ECC : unused on most chips |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1501 | */ |
Haavard Skinnemoen | d6248fd | 2008-07-03 23:40:18 -0700 | [diff] [blame] | 1502 | static void atmel_nand_hwctl(struct mtd_info *mtd, int mode) |
| 1503 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 1504 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 1505 | struct atmel_nand_host *host = nand_get_controller_data(nand_chip); |
Josh Wu | 71b94e2 | 2013-05-09 15:34:54 +0800 | [diff] [blame] | 1506 | |
| 1507 | if (host->board.need_reset_workaround) |
Haavard Skinnemoen | d6248fd | 2008-07-03 23:40:18 -0700 | [diff] [blame] | 1508 | ecc_writel(host->ecc, CR, ATMEL_ECC_RST); |
Haavard Skinnemoen | d6248fd | 2008-07-03 23:40:18 -0700 | [diff] [blame] | 1509 | } |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 1510 | |
Boris Brezillon | 4f3cab9 | 2016-04-01 14:54:22 +0200 | [diff] [blame] | 1511 | static int atmel_of_init_ecc(struct atmel_nand_host *host, |
| 1512 | struct device_node *np) |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 1513 | { |
Josh Wu | a41b51a | 2012-06-29 17:47:54 +0800 | [diff] [blame] | 1514 | u32 offset[2]; |
Boris Brezillon | 4f3cab9 | 2016-04-01 14:54:22 +0200 | [diff] [blame] | 1515 | u32 val; |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 1516 | |
Josh Wu | a41b51a | 2012-06-29 17:47:54 +0800 | [diff] [blame] | 1517 | host->has_pmecc = of_property_read_bool(np, "atmel,has-pmecc"); |
| 1518 | |
Boris Brezillon | 4f3cab9 | 2016-04-01 14:54:22 +0200 | [diff] [blame] | 1519 | /* Not using PMECC */ |
| 1520 | if (!(host->nand_chip.ecc.mode == NAND_ECC_HW) || !host->has_pmecc) |
| 1521 | return 0; |
Josh Wu | a41b51a | 2012-06-29 17:47:54 +0800 | [diff] [blame] | 1522 | |
| 1523 | /* use PMECC, get correction capability, sector size and lookup |
| 1524 | * table offset. |
Josh Wu | e66b431 | 2013-01-23 20:47:11 +0800 | [diff] [blame] | 1525 | * If correction bits and sector size are not specified, then find |
| 1526 | * them from NAND ONFI parameters. |
Josh Wu | a41b51a | 2012-06-29 17:47:54 +0800 | [diff] [blame] | 1527 | */ |
Josh Wu | e66b431 | 2013-01-23 20:47:11 +0800 | [diff] [blame] | 1528 | if (of_property_read_u32(np, "atmel,pmecc-cap", &val) == 0) { |
Romain Izard | 9424846 | 2016-02-10 10:56:26 +0100 | [diff] [blame] | 1529 | if (val > host->caps->pmecc_max_correction) { |
| 1530 | dev_err(host->dev, |
| 1531 | "Required ECC strength too high: %u max %u\n", |
| 1532 | val, host->caps->pmecc_max_correction); |
| 1533 | return -EINVAL; |
| 1534 | } |
| 1535 | if ((val != 2) && (val != 4) && (val != 8) && |
| 1536 | (val != 12) && (val != 24) && (val != 32)) { |
Josh Wu | e66b431 | 2013-01-23 20:47:11 +0800 | [diff] [blame] | 1537 | dev_err(host->dev, |
Romain Izard | e88b7f7 | 2016-01-15 11:34:56 +0100 | [diff] [blame] | 1538 | "Required ECC strength not supported: %u\n", |
Josh Wu | e66b431 | 2013-01-23 20:47:11 +0800 | [diff] [blame] | 1539 | val); |
| 1540 | return -EINVAL; |
| 1541 | } |
| 1542 | host->pmecc_corr_cap = (u8)val; |
Josh Wu | a41b51a | 2012-06-29 17:47:54 +0800 | [diff] [blame] | 1543 | } |
Josh Wu | a41b51a | 2012-06-29 17:47:54 +0800 | [diff] [blame] | 1544 | |
Josh Wu | e66b431 | 2013-01-23 20:47:11 +0800 | [diff] [blame] | 1545 | if (of_property_read_u32(np, "atmel,pmecc-sector-size", &val) == 0) { |
| 1546 | if ((val != 512) && (val != 1024)) { |
| 1547 | dev_err(host->dev, |
Romain Izard | e88b7f7 | 2016-01-15 11:34:56 +0100 | [diff] [blame] | 1548 | "Required ECC sector size not supported: %u\n", |
Josh Wu | e66b431 | 2013-01-23 20:47:11 +0800 | [diff] [blame] | 1549 | val); |
| 1550 | return -EINVAL; |
| 1551 | } |
| 1552 | host->pmecc_sector_size = (u16)val; |
Josh Wu | a41b51a | 2012-06-29 17:47:54 +0800 | [diff] [blame] | 1553 | } |
Josh Wu | a41b51a | 2012-06-29 17:47:54 +0800 | [diff] [blame] | 1554 | |
| 1555 | if (of_property_read_u32_array(np, "atmel,pmecc-lookup-table-offset", |
| 1556 | offset, 2) != 0) { |
Josh Wu | abb1cd0 | 2014-10-11 18:01:50 +0800 | [diff] [blame] | 1557 | dev_err(host->dev, "Cannot get PMECC lookup table offset, will build a lookup table in runtime.\n"); |
| 1558 | host->has_no_lookup_table = true; |
| 1559 | /* Will build a lookup table and initialize the offset later */ |
| 1560 | return 0; |
Josh Wu | a41b51a | 2012-06-29 17:47:54 +0800 | [diff] [blame] | 1561 | } |
Boris Brezillon | 4f3cab9 | 2016-04-01 14:54:22 +0200 | [diff] [blame] | 1562 | |
Josh Wu | c0cf787 | 2013-01-23 20:47:08 +0800 | [diff] [blame] | 1563 | if (!offset[0] && !offset[1]) { |
Josh Wu | a41b51a | 2012-06-29 17:47:54 +0800 | [diff] [blame] | 1564 | dev_err(host->dev, "Invalid PMECC lookup table offset\n"); |
| 1565 | return -EINVAL; |
| 1566 | } |
Boris Brezillon | 4f3cab9 | 2016-04-01 14:54:22 +0200 | [diff] [blame] | 1567 | |
Josh Wu | e66b431 | 2013-01-23 20:47:11 +0800 | [diff] [blame] | 1568 | host->pmecc_lookup_table_offset_512 = offset[0]; |
| 1569 | host->pmecc_lookup_table_offset_1024 = offset[1]; |
Josh Wu | a41b51a | 2012-06-29 17:47:54 +0800 | [diff] [blame] | 1570 | |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 1571 | return 0; |
| 1572 | } |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 1573 | |
Boris Brezillon | 4f3cab9 | 2016-04-01 14:54:22 +0200 | [diff] [blame] | 1574 | static int atmel_of_init_port(struct atmel_nand_host *host, |
| 1575 | struct device_node *np) |
| 1576 | { |
| 1577 | u32 val; |
| 1578 | struct atmel_nand_data *board = &host->board; |
| 1579 | enum of_gpio_flags flags = 0; |
| 1580 | |
| 1581 | host->caps = (struct atmel_nand_caps *) |
| 1582 | of_device_get_match_data(host->dev); |
| 1583 | |
| 1584 | if (of_property_read_u32(np, "atmel,nand-addr-offset", &val) == 0) { |
| 1585 | if (val >= 32) { |
| 1586 | dev_err(host->dev, "invalid addr-offset %u\n", val); |
| 1587 | return -EINVAL; |
| 1588 | } |
| 1589 | board->ale = val; |
| 1590 | } |
| 1591 | |
| 1592 | if (of_property_read_u32(np, "atmel,nand-cmd-offset", &val) == 0) { |
| 1593 | if (val >= 32) { |
| 1594 | dev_err(host->dev, "invalid cmd-offset %u\n", val); |
| 1595 | return -EINVAL; |
| 1596 | } |
| 1597 | board->cle = val; |
| 1598 | } |
| 1599 | |
| 1600 | board->has_dma = of_property_read_bool(np, "atmel,nand-has-dma"); |
| 1601 | |
| 1602 | board->rdy_pin = of_get_gpio_flags(np, 0, &flags); |
| 1603 | board->rdy_pin_active_low = (flags == OF_GPIO_ACTIVE_LOW); |
| 1604 | |
| 1605 | board->enable_pin = of_get_gpio(np, 1); |
| 1606 | board->det_pin = of_get_gpio(np, 2); |
| 1607 | |
| 1608 | /* load the nfc driver if there is */ |
| 1609 | of_platform_populate(np, NULL, NULL, host->dev); |
| 1610 | |
| 1611 | /* |
| 1612 | * Initialize ECC mode to NAND_ECC_SOFT so that we have a correct value |
| 1613 | * even if the nand-ecc-mode property is not defined. |
| 1614 | */ |
| 1615 | host->nand_chip.ecc.mode = NAND_ECC_SOFT; |
| 1616 | |
| 1617 | return 0; |
| 1618 | } |
| 1619 | |
Johan Hovold | 2c2b928 | 2013-09-23 16:27:28 +0200 | [diff] [blame] | 1620 | static int atmel_hw_nand_init_params(struct platform_device *pdev, |
Josh Wu | 3dfe41a | 2012-06-25 18:07:43 +0800 | [diff] [blame] | 1621 | struct atmel_nand_host *host) |
| 1622 | { |
Josh Wu | 3dfe41a | 2012-06-25 18:07:43 +0800 | [diff] [blame] | 1623 | struct nand_chip *nand_chip = &host->nand_chip; |
Boris BREZILLON | ac01efe | 2015-12-10 08:59:50 +0100 | [diff] [blame] | 1624 | struct mtd_info *mtd = nand_to_mtd(nand_chip); |
Josh Wu | 3dfe41a | 2012-06-25 18:07:43 +0800 | [diff] [blame] | 1625 | struct resource *regs; |
| 1626 | |
| 1627 | regs = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 1628 | if (!regs) { |
| 1629 | dev_err(host->dev, |
| 1630 | "Can't get I/O resource regs, use software ECC\n"); |
| 1631 | nand_chip->ecc.mode = NAND_ECC_SOFT; |
| 1632 | return 0; |
| 1633 | } |
| 1634 | |
Jean-Christophe PLAGNIOL-VILLARD | 0d63748 | 2013-08-05 19:14:33 +0800 | [diff] [blame] | 1635 | host->ecc = devm_ioremap_resource(&pdev->dev, regs); |
Wei Yongjun | 8fb7b93 | 2014-07-28 21:19:55 +0800 | [diff] [blame] | 1636 | if (IS_ERR(host->ecc)) |
Jean-Christophe PLAGNIOL-VILLARD | 0d63748 | 2013-08-05 19:14:33 +0800 | [diff] [blame] | 1637 | return PTR_ERR(host->ecc); |
Josh Wu | 3dfe41a | 2012-06-25 18:07:43 +0800 | [diff] [blame] | 1638 | |
| 1639 | /* ECC is calculated for the whole page (1 step) */ |
| 1640 | nand_chip->ecc.size = mtd->writesize; |
| 1641 | |
| 1642 | /* set ECC page size and oob layout */ |
| 1643 | switch (mtd->writesize) { |
| 1644 | case 512: |
Boris Brezillon | cc00383 | 2016-02-03 19:59:11 +0100 | [diff] [blame^] | 1645 | mtd_set_ooblayout(mtd, &atmel_ooblayout_sp_ops); |
Josh Wu | 3dfe41a | 2012-06-25 18:07:43 +0800 | [diff] [blame] | 1646 | ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_528); |
| 1647 | break; |
| 1648 | case 1024: |
Boris Brezillon | cc00383 | 2016-02-03 19:59:11 +0100 | [diff] [blame^] | 1649 | mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops); |
Josh Wu | 3dfe41a | 2012-06-25 18:07:43 +0800 | [diff] [blame] | 1650 | ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_1056); |
| 1651 | break; |
| 1652 | case 2048: |
Boris Brezillon | cc00383 | 2016-02-03 19:59:11 +0100 | [diff] [blame^] | 1653 | mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops); |
Josh Wu | 3dfe41a | 2012-06-25 18:07:43 +0800 | [diff] [blame] | 1654 | ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_2112); |
| 1655 | break; |
| 1656 | case 4096: |
Boris Brezillon | cc00383 | 2016-02-03 19:59:11 +0100 | [diff] [blame^] | 1657 | mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops); |
Josh Wu | 3dfe41a | 2012-06-25 18:07:43 +0800 | [diff] [blame] | 1658 | ecc_writel(host->ecc, MR, ATMEL_ECC_PAGESIZE_4224); |
| 1659 | break; |
| 1660 | default: |
| 1661 | /* page size not handled by HW ECC */ |
| 1662 | /* switching back to soft ECC */ |
| 1663 | nand_chip->ecc.mode = NAND_ECC_SOFT; |
| 1664 | return 0; |
| 1665 | } |
| 1666 | |
| 1667 | /* set up for HW ECC */ |
| 1668 | nand_chip->ecc.calculate = atmel_nand_calculate; |
| 1669 | nand_chip->ecc.correct = atmel_nand_correct; |
| 1670 | nand_chip->ecc.hwctl = atmel_nand_hwctl; |
| 1671 | nand_chip->ecc.read_page = atmel_nand_read_page; |
| 1672 | nand_chip->ecc.bytes = 4; |
| 1673 | nand_chip->ecc.strength = 1; |
| 1674 | |
| 1675 | return 0; |
| 1676 | } |
| 1677 | |
Wu, Josh | 50e04e2 | 2014-06-10 17:50:09 +0800 | [diff] [blame] | 1678 | static inline u32 nfc_read_status(struct atmel_nand_host *host) |
| 1679 | { |
| 1680 | u32 err_flags = NFC_SR_DTOE | NFC_SR_UNDEF | NFC_SR_AWB | NFC_SR_ASE; |
| 1681 | u32 nfc_status = nfc_readl(host->nfc->hsmc_regs, SR); |
| 1682 | |
| 1683 | if (unlikely(nfc_status & err_flags)) { |
| 1684 | if (nfc_status & NFC_SR_DTOE) |
| 1685 | dev_err(host->dev, "NFC: Waiting Nand R/B Timeout Error\n"); |
| 1686 | else if (nfc_status & NFC_SR_UNDEF) |
| 1687 | dev_err(host->dev, "NFC: Access Undefined Area Error\n"); |
| 1688 | else if (nfc_status & NFC_SR_AWB) |
| 1689 | dev_err(host->dev, "NFC: Access memory While NFC is busy\n"); |
| 1690 | else if (nfc_status & NFC_SR_ASE) |
| 1691 | dev_err(host->dev, "NFC: Access memory Size Error\n"); |
| 1692 | } |
| 1693 | |
| 1694 | return nfc_status; |
| 1695 | } |
| 1696 | |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 1697 | /* SMC interrupt service routine */ |
| 1698 | static irqreturn_t hsmc_interrupt(int irq, void *dev_id) |
| 1699 | { |
| 1700 | struct atmel_nand_host *host = dev_id; |
| 1701 | u32 status, mask, pending; |
Josh Wu | e4e0693 | 2014-06-10 17:50:11 +0800 | [diff] [blame] | 1702 | irqreturn_t ret = IRQ_NONE; |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 1703 | |
Wu, Josh | 50e04e2 | 2014-06-10 17:50:09 +0800 | [diff] [blame] | 1704 | status = nfc_read_status(host); |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 1705 | mask = nfc_readl(host->nfc->hsmc_regs, IMR); |
| 1706 | pending = status & mask; |
| 1707 | |
| 1708 | if (pending & NFC_SR_XFR_DONE) { |
Josh Wu | e4e0693 | 2014-06-10 17:50:11 +0800 | [diff] [blame] | 1709 | complete(&host->nfc->comp_xfer_done); |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 1710 | nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_XFR_DONE); |
Josh Wu | e4e0693 | 2014-06-10 17:50:11 +0800 | [diff] [blame] | 1711 | ret = IRQ_HANDLED; |
| 1712 | } |
Romain Izard | 5ddc7bd | 2016-02-10 10:56:23 +0100 | [diff] [blame] | 1713 | if (pending & host->nfc->caps->rb_mask) { |
Josh Wu | e4e0693 | 2014-06-10 17:50:11 +0800 | [diff] [blame] | 1714 | complete(&host->nfc->comp_ready); |
Romain Izard | 5ddc7bd | 2016-02-10 10:56:23 +0100 | [diff] [blame] | 1715 | nfc_writel(host->nfc->hsmc_regs, IDR, host->nfc->caps->rb_mask); |
Josh Wu | e4e0693 | 2014-06-10 17:50:11 +0800 | [diff] [blame] | 1716 | ret = IRQ_HANDLED; |
| 1717 | } |
| 1718 | if (pending & NFC_SR_CMD_DONE) { |
| 1719 | complete(&host->nfc->comp_cmd_done); |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 1720 | nfc_writel(host->nfc->hsmc_regs, IDR, NFC_SR_CMD_DONE); |
Josh Wu | e4e0693 | 2014-06-10 17:50:11 +0800 | [diff] [blame] | 1721 | ret = IRQ_HANDLED; |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 1722 | } |
| 1723 | |
| 1724 | return ret; |
| 1725 | } |
| 1726 | |
| 1727 | /* NFC(Nand Flash Controller) related functions */ |
Josh Wu | e4e0693 | 2014-06-10 17:50:11 +0800 | [diff] [blame] | 1728 | static void nfc_prepare_interrupt(struct atmel_nand_host *host, u32 flag) |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 1729 | { |
Josh Wu | e4e0693 | 2014-06-10 17:50:11 +0800 | [diff] [blame] | 1730 | if (flag & NFC_SR_XFR_DONE) |
| 1731 | init_completion(&host->nfc->comp_xfer_done); |
| 1732 | |
Romain Izard | 5ddc7bd | 2016-02-10 10:56:23 +0100 | [diff] [blame] | 1733 | if (flag & host->nfc->caps->rb_mask) |
Josh Wu | e4e0693 | 2014-06-10 17:50:11 +0800 | [diff] [blame] | 1734 | init_completion(&host->nfc->comp_ready); |
| 1735 | |
| 1736 | if (flag & NFC_SR_CMD_DONE) |
| 1737 | init_completion(&host->nfc->comp_cmd_done); |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 1738 | |
| 1739 | /* Enable interrupt that need to wait for */ |
| 1740 | nfc_writel(host->nfc->hsmc_regs, IER, flag); |
Josh Wu | e4e0693 | 2014-06-10 17:50:11 +0800 | [diff] [blame] | 1741 | } |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 1742 | |
Josh Wu | e4e0693 | 2014-06-10 17:50:11 +0800 | [diff] [blame] | 1743 | static int nfc_wait_interrupt(struct atmel_nand_host *host, u32 flag) |
| 1744 | { |
| 1745 | int i, index = 0; |
| 1746 | struct completion *comp[3]; /* Support 3 interrupt completion */ |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 1747 | |
Josh Wu | e4e0693 | 2014-06-10 17:50:11 +0800 | [diff] [blame] | 1748 | if (flag & NFC_SR_XFR_DONE) |
| 1749 | comp[index++] = &host->nfc->comp_xfer_done; |
| 1750 | |
Romain Izard | 5ddc7bd | 2016-02-10 10:56:23 +0100 | [diff] [blame] | 1751 | if (flag & host->nfc->caps->rb_mask) |
Josh Wu | e4e0693 | 2014-06-10 17:50:11 +0800 | [diff] [blame] | 1752 | comp[index++] = &host->nfc->comp_ready; |
| 1753 | |
| 1754 | if (flag & NFC_SR_CMD_DONE) |
| 1755 | comp[index++] = &host->nfc->comp_cmd_done; |
| 1756 | |
| 1757 | if (index == 0) { |
Colin Ian King | 393d23c | 2015-02-28 20:27:56 +0000 | [diff] [blame] | 1758 | dev_err(host->dev, "Unknown interrupt flag: 0x%08x\n", flag); |
Josh Wu | e4e0693 | 2014-06-10 17:50:11 +0800 | [diff] [blame] | 1759 | return -EINVAL; |
| 1760 | } |
| 1761 | |
| 1762 | for (i = 0; i < index; i++) { |
| 1763 | if (wait_for_completion_timeout(comp[i], |
| 1764 | msecs_to_jiffies(NFC_TIME_OUT_MS))) |
| 1765 | continue; /* wait for next completion */ |
| 1766 | else |
| 1767 | goto err_timeout; |
| 1768 | } |
| 1769 | |
| 1770 | return 0; |
| 1771 | |
| 1772 | err_timeout: |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 1773 | dev_err(host->dev, "Time out to wait for interrupt: 0x%08x\n", flag); |
Josh Wu | e4e0693 | 2014-06-10 17:50:11 +0800 | [diff] [blame] | 1774 | /* Disable the interrupt as it is not handled by interrupt handler */ |
| 1775 | nfc_writel(host->nfc->hsmc_regs, IDR, flag); |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 1776 | return -ETIMEDOUT; |
| 1777 | } |
| 1778 | |
| 1779 | static int nfc_send_command(struct atmel_nand_host *host, |
| 1780 | unsigned int cmd, unsigned int addr, unsigned char cycle0) |
| 1781 | { |
| 1782 | unsigned long timeout; |
Josh Wu | e4e0693 | 2014-06-10 17:50:11 +0800 | [diff] [blame] | 1783 | u32 flag = NFC_SR_CMD_DONE; |
| 1784 | flag |= cmd & NFCADDR_CMD_DATAEN ? NFC_SR_XFR_DONE : 0; |
| 1785 | |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 1786 | dev_dbg(host->dev, |
| 1787 | "nfc_cmd: 0x%08x, addr1234: 0x%08x, cycle0: 0x%02x\n", |
| 1788 | cmd, addr, cycle0); |
| 1789 | |
| 1790 | timeout = jiffies + msecs_to_jiffies(NFC_TIME_OUT_MS); |
Boris Brezillon | 111573cc | 2015-01-22 18:21:30 +0800 | [diff] [blame] | 1791 | while (nfc_readl(host->nfc->hsmc_regs, SR) & NFC_SR_BUSY) { |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 1792 | if (time_after(jiffies, timeout)) { |
| 1793 | dev_err(host->dev, |
Boris Brezillon | 111573cc | 2015-01-22 18:21:30 +0800 | [diff] [blame] | 1794 | "Time out to wait for NFC ready!\n"); |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 1795 | return -ETIMEDOUT; |
| 1796 | } |
| 1797 | } |
Josh Wu | e4e0693 | 2014-06-10 17:50:11 +0800 | [diff] [blame] | 1798 | |
| 1799 | nfc_prepare_interrupt(host, flag); |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 1800 | nfc_writel(host->nfc->hsmc_regs, CYCLE0, cycle0); |
| 1801 | nfc_cmd_addr1234_writel(cmd, addr, host->nfc->base_cmd_regs); |
Josh Wu | e4e0693 | 2014-06-10 17:50:11 +0800 | [diff] [blame] | 1802 | return nfc_wait_interrupt(host, flag); |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 1803 | } |
| 1804 | |
| 1805 | static int nfc_device_ready(struct mtd_info *mtd) |
| 1806 | { |
Wu, Josh | 72a78e3 | 2014-06-10 17:50:10 +0800 | [diff] [blame] | 1807 | u32 status, mask; |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 1808 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 1809 | struct atmel_nand_host *host = nand_get_controller_data(nand_chip); |
Wu, Josh | 72a78e3 | 2014-06-10 17:50:10 +0800 | [diff] [blame] | 1810 | |
| 1811 | status = nfc_read_status(host); |
| 1812 | mask = nfc_readl(host->nfc->hsmc_regs, IMR); |
| 1813 | |
| 1814 | /* The mask should be 0. If not we may lost interrupts */ |
| 1815 | if (unlikely(mask & status)) |
| 1816 | dev_err(host->dev, "Lost the interrupt flags: 0x%08x\n", |
| 1817 | mask & status); |
| 1818 | |
Romain Izard | 5ddc7bd | 2016-02-10 10:56:23 +0100 | [diff] [blame] | 1819 | return status & host->nfc->caps->rb_mask; |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 1820 | } |
| 1821 | |
| 1822 | static void nfc_select_chip(struct mtd_info *mtd, int chip) |
| 1823 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 1824 | struct nand_chip *nand_chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 1825 | struct atmel_nand_host *host = nand_get_controller_data(nand_chip); |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 1826 | |
| 1827 | if (chip == -1) |
| 1828 | nfc_writel(host->nfc->hsmc_regs, CTRL, NFC_CTRL_DISABLE); |
| 1829 | else |
| 1830 | nfc_writel(host->nfc->hsmc_regs, CTRL, NFC_CTRL_ENABLE); |
| 1831 | } |
| 1832 | |
Brian Norris | 3dad234 | 2014-01-29 14:08:12 -0800 | [diff] [blame] | 1833 | static int nfc_make_addr(struct mtd_info *mtd, int command, int column, |
| 1834 | int page_addr, unsigned int *addr1234, unsigned int *cycle0) |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 1835 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 1836 | struct nand_chip *chip = mtd_to_nand(mtd); |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 1837 | |
| 1838 | int acycle = 0; |
| 1839 | unsigned char addr_bytes[8]; |
| 1840 | int index = 0, bit_shift; |
| 1841 | |
| 1842 | BUG_ON(addr1234 == NULL || cycle0 == NULL); |
| 1843 | |
| 1844 | *cycle0 = 0; |
| 1845 | *addr1234 = 0; |
| 1846 | |
| 1847 | if (column != -1) { |
Brian Norris | 3dad234 | 2014-01-29 14:08:12 -0800 | [diff] [blame] | 1848 | if (chip->options & NAND_BUSWIDTH_16 && |
| 1849 | !nand_opcode_8bits(command)) |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 1850 | column >>= 1; |
| 1851 | addr_bytes[acycle++] = column & 0xff; |
| 1852 | if (mtd->writesize > 512) |
| 1853 | addr_bytes[acycle++] = (column >> 8) & 0xff; |
| 1854 | } |
| 1855 | |
| 1856 | if (page_addr != -1) { |
| 1857 | addr_bytes[acycle++] = page_addr & 0xff; |
| 1858 | addr_bytes[acycle++] = (page_addr >> 8) & 0xff; |
| 1859 | if (chip->chipsize > (128 << 20)) |
| 1860 | addr_bytes[acycle++] = (page_addr >> 16) & 0xff; |
| 1861 | } |
| 1862 | |
| 1863 | if (acycle > 4) |
| 1864 | *cycle0 = addr_bytes[index++]; |
| 1865 | |
| 1866 | for (bit_shift = 0; index < acycle; bit_shift += 8) |
| 1867 | *addr1234 += addr_bytes[index++] << bit_shift; |
| 1868 | |
| 1869 | /* return acycle in cmd register */ |
| 1870 | return acycle << NFCADDR_CMD_ACYCLE_BIT_POS; |
| 1871 | } |
| 1872 | |
| 1873 | static void nfc_nand_command(struct mtd_info *mtd, unsigned int command, |
| 1874 | int column, int page_addr) |
| 1875 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 1876 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 1877 | struct atmel_nand_host *host = nand_get_controller_data(chip); |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 1878 | unsigned long timeout; |
| 1879 | unsigned int nfc_addr_cmd = 0; |
| 1880 | |
| 1881 | unsigned int cmd1 = command << NFCADDR_CMD_CMD1_BIT_POS; |
| 1882 | |
| 1883 | /* Set default settings: no cmd2, no addr cycle. read from nand */ |
| 1884 | unsigned int cmd2 = 0; |
| 1885 | unsigned int vcmd2 = 0; |
| 1886 | int acycle = NFCADDR_CMD_ACYCLE_NONE; |
| 1887 | int csid = NFCADDR_CMD_CSID_3; |
| 1888 | int dataen = NFCADDR_CMD_DATADIS; |
| 1889 | int nfcwr = NFCADDR_CMD_NFCRD; |
| 1890 | unsigned int addr1234 = 0; |
| 1891 | unsigned int cycle0 = 0; |
| 1892 | bool do_addr = true; |
Josh Wu | 1ae9c09 | 2013-08-05 19:14:36 +0800 | [diff] [blame] | 1893 | host->nfc->data_in_sram = NULL; |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 1894 | |
| 1895 | dev_dbg(host->dev, "%s: cmd = 0x%02x, col = 0x%08x, page = 0x%08x\n", |
| 1896 | __func__, command, column, page_addr); |
| 1897 | |
| 1898 | switch (command) { |
| 1899 | case NAND_CMD_RESET: |
| 1900 | nfc_addr_cmd = cmd1 | acycle | csid | dataen | nfcwr; |
| 1901 | nfc_send_command(host, nfc_addr_cmd, addr1234, cycle0); |
| 1902 | udelay(chip->chip_delay); |
| 1903 | |
| 1904 | nfc_nand_command(mtd, NAND_CMD_STATUS, -1, -1); |
| 1905 | timeout = jiffies + msecs_to_jiffies(NFC_TIME_OUT_MS); |
| 1906 | while (!(chip->read_byte(mtd) & NAND_STATUS_READY)) { |
| 1907 | if (time_after(jiffies, timeout)) { |
| 1908 | dev_err(host->dev, |
| 1909 | "Time out to wait status ready!\n"); |
| 1910 | break; |
| 1911 | } |
| 1912 | } |
| 1913 | return; |
| 1914 | case NAND_CMD_STATUS: |
| 1915 | do_addr = false; |
| 1916 | break; |
| 1917 | case NAND_CMD_PARAM: |
| 1918 | case NAND_CMD_READID: |
| 1919 | do_addr = false; |
| 1920 | acycle = NFCADDR_CMD_ACYCLE_1; |
| 1921 | if (column != -1) |
| 1922 | addr1234 = column; |
| 1923 | break; |
| 1924 | case NAND_CMD_RNDOUT: |
| 1925 | cmd2 = NAND_CMD_RNDOUTSTART << NFCADDR_CMD_CMD2_BIT_POS; |
| 1926 | vcmd2 = NFCADDR_CMD_VCMD2; |
| 1927 | break; |
| 1928 | case NAND_CMD_READ0: |
| 1929 | case NAND_CMD_READOOB: |
| 1930 | if (command == NAND_CMD_READOOB) { |
| 1931 | column += mtd->writesize; |
| 1932 | command = NAND_CMD_READ0; /* only READ0 is valid */ |
| 1933 | cmd1 = command << NFCADDR_CMD_CMD1_BIT_POS; |
| 1934 | } |
Josh Wu | 1ae9c09 | 2013-08-05 19:14:36 +0800 | [diff] [blame] | 1935 | if (host->nfc->use_nfc_sram) { |
| 1936 | /* Enable Data transfer to sram */ |
| 1937 | dataen = NFCADDR_CMD_DATAEN; |
| 1938 | |
| 1939 | /* Need enable PMECC now, since NFC will transfer |
| 1940 | * data in bus after sending nfc read command. |
| 1941 | */ |
| 1942 | if (chip->ecc.mode == NAND_ECC_HW && host->has_pmecc) |
| 1943 | pmecc_enable(host, NAND_ECC_READ); |
| 1944 | } |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 1945 | |
| 1946 | cmd2 = NAND_CMD_READSTART << NFCADDR_CMD_CMD2_BIT_POS; |
| 1947 | vcmd2 = NFCADDR_CMD_VCMD2; |
| 1948 | break; |
| 1949 | /* For prgramming command, the cmd need set to write enable */ |
| 1950 | case NAND_CMD_PAGEPROG: |
| 1951 | case NAND_CMD_SEQIN: |
| 1952 | case NAND_CMD_RNDIN: |
| 1953 | nfcwr = NFCADDR_CMD_NFCWR; |
Josh Wu | 6054d4d | 2013-08-05 19:14:37 +0800 | [diff] [blame] | 1954 | if (host->nfc->will_write_sram && command == NAND_CMD_SEQIN) |
| 1955 | dataen = NFCADDR_CMD_DATAEN; |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 1956 | break; |
| 1957 | default: |
| 1958 | break; |
| 1959 | } |
| 1960 | |
| 1961 | if (do_addr) |
Brian Norris | 3dad234 | 2014-01-29 14:08:12 -0800 | [diff] [blame] | 1962 | acycle = nfc_make_addr(mtd, command, column, page_addr, |
| 1963 | &addr1234, &cycle0); |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 1964 | |
| 1965 | nfc_addr_cmd = cmd1 | cmd2 | vcmd2 | acycle | csid | dataen | nfcwr; |
| 1966 | nfc_send_command(host, nfc_addr_cmd, addr1234, cycle0); |
| 1967 | |
| 1968 | /* |
| 1969 | * Program and erase have their own busy handlers status, sequential |
| 1970 | * in, and deplete1 need no delay. |
| 1971 | */ |
| 1972 | switch (command) { |
| 1973 | case NAND_CMD_CACHEDPROG: |
| 1974 | case NAND_CMD_PAGEPROG: |
| 1975 | case NAND_CMD_ERASE1: |
| 1976 | case NAND_CMD_ERASE2: |
| 1977 | case NAND_CMD_RNDIN: |
| 1978 | case NAND_CMD_STATUS: |
| 1979 | case NAND_CMD_RNDOUT: |
| 1980 | case NAND_CMD_SEQIN: |
| 1981 | case NAND_CMD_READID: |
| 1982 | return; |
| 1983 | |
| 1984 | case NAND_CMD_READ0: |
Josh Wu | 1ae9c09 | 2013-08-05 19:14:36 +0800 | [diff] [blame] | 1985 | if (dataen == NFCADDR_CMD_DATAEN) { |
| 1986 | host->nfc->data_in_sram = host->nfc->sram_bank0 + |
| 1987 | nfc_get_sram_off(host); |
| 1988 | return; |
| 1989 | } |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 1990 | /* fall through */ |
| 1991 | default: |
Romain Izard | 5ddc7bd | 2016-02-10 10:56:23 +0100 | [diff] [blame] | 1992 | nfc_prepare_interrupt(host, host->nfc->caps->rb_mask); |
| 1993 | nfc_wait_interrupt(host, host->nfc->caps->rb_mask); |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 1994 | } |
| 1995 | } |
| 1996 | |
Josh Wu | 6054d4d | 2013-08-05 19:14:37 +0800 | [diff] [blame] | 1997 | static int nfc_sram_write_page(struct mtd_info *mtd, struct nand_chip *chip, |
| 1998 | uint32_t offset, int data_len, const uint8_t *buf, |
| 1999 | int oob_required, int page, int cached, int raw) |
| 2000 | { |
| 2001 | int cfg, len; |
| 2002 | int status = 0; |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 2003 | struct atmel_nand_host *host = nand_get_controller_data(chip); |
Wu, Josh | 068b44b | 2014-11-07 15:26:09 +0800 | [diff] [blame] | 2004 | void *sram = host->nfc->sram_bank0 + nfc_get_sram_off(host); |
Josh Wu | 6054d4d | 2013-08-05 19:14:37 +0800 | [diff] [blame] | 2005 | |
| 2006 | /* Subpage write is not supported */ |
| 2007 | if (offset || (data_len < mtd->writesize)) |
| 2008 | return -EINVAL; |
| 2009 | |
Josh Wu | 6054d4d | 2013-08-05 19:14:37 +0800 | [diff] [blame] | 2010 | len = mtd->writesize; |
Josh Wu | 6054d4d | 2013-08-05 19:14:37 +0800 | [diff] [blame] | 2011 | /* Copy page data to sram that will write to nand via NFC */ |
| 2012 | if (use_dma) { |
| 2013 | if (atmel_nand_dma_op(mtd, (void *)buf, len, 0) != 0) |
| 2014 | /* Fall back to use cpu copy */ |
Wu, Josh | 068b44b | 2014-11-07 15:26:09 +0800 | [diff] [blame] | 2015 | memcpy(sram, buf, len); |
Josh Wu | 6054d4d | 2013-08-05 19:14:37 +0800 | [diff] [blame] | 2016 | } else { |
Wu, Josh | 068b44b | 2014-11-07 15:26:09 +0800 | [diff] [blame] | 2017 | memcpy(sram, buf, len); |
Josh Wu | 6054d4d | 2013-08-05 19:14:37 +0800 | [diff] [blame] | 2018 | } |
| 2019 | |
Wu, Josh | ff0a215 | 2014-08-05 18:38:52 +0800 | [diff] [blame] | 2020 | cfg = nfc_readl(host->nfc->hsmc_regs, CFG); |
| 2021 | if (unlikely(raw) && oob_required) { |
Wu, Josh | 068b44b | 2014-11-07 15:26:09 +0800 | [diff] [blame] | 2022 | memcpy(sram + len, chip->oob_poi, mtd->oobsize); |
Wu, Josh | ff0a215 | 2014-08-05 18:38:52 +0800 | [diff] [blame] | 2023 | len += mtd->oobsize; |
| 2024 | nfc_writel(host->nfc->hsmc_regs, CFG, cfg | NFC_CFG_WSPARE); |
| 2025 | } else { |
| 2026 | nfc_writel(host->nfc->hsmc_regs, CFG, cfg & ~NFC_CFG_WSPARE); |
| 2027 | } |
| 2028 | |
Josh Wu | 6054d4d | 2013-08-05 19:14:37 +0800 | [diff] [blame] | 2029 | if (chip->ecc.mode == NAND_ECC_HW && host->has_pmecc) |
| 2030 | /* |
| 2031 | * When use NFC sram, need set up PMECC before send |
| 2032 | * NAND_CMD_SEQIN command. Since when the nand command |
| 2033 | * is sent, nfc will do transfer from sram and nand. |
| 2034 | */ |
| 2035 | pmecc_enable(host, NAND_ECC_WRITE); |
| 2036 | |
| 2037 | host->nfc->will_write_sram = true; |
| 2038 | chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page); |
| 2039 | host->nfc->will_write_sram = false; |
| 2040 | |
| 2041 | if (likely(!raw)) |
| 2042 | /* Need to write ecc into oob */ |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 2043 | status = chip->ecc.write_page(mtd, chip, buf, oob_required, |
| 2044 | page); |
Josh Wu | 6054d4d | 2013-08-05 19:14:37 +0800 | [diff] [blame] | 2045 | |
| 2046 | if (status < 0) |
| 2047 | return status; |
| 2048 | |
| 2049 | chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1); |
| 2050 | status = chip->waitfunc(mtd, chip); |
| 2051 | |
| 2052 | if ((status & NAND_STATUS_FAIL) && (chip->errstat)) |
| 2053 | status = chip->errstat(mtd, chip, FL_WRITING, status, page); |
| 2054 | |
| 2055 | if (status & NAND_STATUS_FAIL) |
| 2056 | return -EIO; |
| 2057 | |
| 2058 | return 0; |
| 2059 | } |
| 2060 | |
Josh Wu | 1ae9c09 | 2013-08-05 19:14:36 +0800 | [diff] [blame] | 2061 | static int nfc_sram_init(struct mtd_info *mtd) |
| 2062 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 2063 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 2064 | struct atmel_nand_host *host = nand_get_controller_data(chip); |
Josh Wu | 1ae9c09 | 2013-08-05 19:14:36 +0800 | [diff] [blame] | 2065 | int res = 0; |
| 2066 | |
| 2067 | /* Initialize the NFC CFG register */ |
| 2068 | unsigned int cfg_nfc = 0; |
| 2069 | |
| 2070 | /* set page size and oob layout */ |
| 2071 | switch (mtd->writesize) { |
| 2072 | case 512: |
| 2073 | cfg_nfc = NFC_CFG_PAGESIZE_512; |
| 2074 | break; |
| 2075 | case 1024: |
| 2076 | cfg_nfc = NFC_CFG_PAGESIZE_1024; |
| 2077 | break; |
| 2078 | case 2048: |
| 2079 | cfg_nfc = NFC_CFG_PAGESIZE_2048; |
| 2080 | break; |
| 2081 | case 4096: |
| 2082 | cfg_nfc = NFC_CFG_PAGESIZE_4096; |
| 2083 | break; |
| 2084 | case 8192: |
| 2085 | cfg_nfc = NFC_CFG_PAGESIZE_8192; |
| 2086 | break; |
| 2087 | default: |
| 2088 | dev_err(host->dev, "Unsupported page size for NFC.\n"); |
| 2089 | res = -ENXIO; |
| 2090 | return res; |
| 2091 | } |
| 2092 | |
| 2093 | /* oob bytes size = (NFCSPARESIZE + 1) * 4 |
| 2094 | * Max support spare size is 512 bytes. */ |
| 2095 | cfg_nfc |= (((mtd->oobsize / 4) - 1) << NFC_CFG_NFC_SPARESIZE_BIT_POS |
| 2096 | & NFC_CFG_NFC_SPARESIZE); |
| 2097 | /* default set a max timeout */ |
| 2098 | cfg_nfc |= NFC_CFG_RSPARE | |
| 2099 | NFC_CFG_NFC_DTOCYC | NFC_CFG_NFC_DTOMUL; |
| 2100 | |
| 2101 | nfc_writel(host->nfc->hsmc_regs, CFG, cfg_nfc); |
| 2102 | |
Josh Wu | 6054d4d | 2013-08-05 19:14:37 +0800 | [diff] [blame] | 2103 | host->nfc->will_write_sram = false; |
Josh Wu | 1ae9c09 | 2013-08-05 19:14:36 +0800 | [diff] [blame] | 2104 | nfc_set_sram_bank(host, 0); |
| 2105 | |
Josh Wu | 6054d4d | 2013-08-05 19:14:37 +0800 | [diff] [blame] | 2106 | /* Use Write page with NFC SRAM only for PMECC or ECC NONE. */ |
| 2107 | if (host->nfc->write_by_sram) { |
| 2108 | if ((chip->ecc.mode == NAND_ECC_HW && host->has_pmecc) || |
| 2109 | chip->ecc.mode == NAND_ECC_NONE) |
| 2110 | chip->write_page = nfc_sram_write_page; |
| 2111 | else |
| 2112 | host->nfc->write_by_sram = false; |
| 2113 | } |
Josh Wu | 1ae9c09 | 2013-08-05 19:14:36 +0800 | [diff] [blame] | 2114 | |
Josh Wu | 6054d4d | 2013-08-05 19:14:37 +0800 | [diff] [blame] | 2115 | dev_info(host->dev, "Using NFC Sram read %s\n", |
| 2116 | host->nfc->write_by_sram ? "and write" : ""); |
Josh Wu | 1ae9c09 | 2013-08-05 19:14:36 +0800 | [diff] [blame] | 2117 | return 0; |
| 2118 | } |
| 2119 | |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 2120 | static struct platform_driver atmel_nand_nfc_driver; |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 2121 | /* |
| 2122 | * Probe for the NAND device. |
| 2123 | */ |
Johan Hovold | 2c2b928 | 2013-09-23 16:27:28 +0200 | [diff] [blame] | 2124 | static int atmel_nand_probe(struct platform_device *pdev) |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 2125 | { |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 2126 | struct atmel_nand_host *host; |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 2127 | struct mtd_info *mtd; |
| 2128 | struct nand_chip *nand_chip; |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 2129 | struct resource *mem; |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 2130 | int res, irq; |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 2131 | |
| 2132 | /* Allocate memory for the device structure (and zero it) */ |
Jean-Christophe PLAGNIOL-VILLARD | 0d63748 | 2013-08-05 19:14:33 +0800 | [diff] [blame] | 2133 | host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL); |
Jingoo Han | 9e3677a | 2013-12-26 12:00:16 +0900 | [diff] [blame] | 2134 | if (!host) |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 2135 | return -ENOMEM; |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 2136 | |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 2137 | res = platform_driver_register(&atmel_nand_nfc_driver); |
| 2138 | if (res) |
| 2139 | dev_err(&pdev->dev, "atmel_nand: can't register NFC driver\n"); |
| 2140 | |
Jean-Christophe PLAGNIOL-VILLARD | 0d63748 | 2013-08-05 19:14:33 +0800 | [diff] [blame] | 2141 | mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 2142 | host->io_base = devm_ioremap_resource(&pdev->dev, mem); |
| 2143 | if (IS_ERR(host->io_base)) { |
Jean-Christophe PLAGNIOL-VILLARD | 0d63748 | 2013-08-05 19:14:33 +0800 | [diff] [blame] | 2144 | res = PTR_ERR(host->io_base); |
Håvard Skinnemoen | cc0c72e | 2008-06-06 18:04:54 +0200 | [diff] [blame] | 2145 | goto err_nand_ioremap; |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 2146 | } |
Jean-Christophe PLAGNIOL-VILLARD | 0d63748 | 2013-08-05 19:14:33 +0800 | [diff] [blame] | 2147 | host->io_phys = (dma_addr_t)mem->start; |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 2148 | |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 2149 | nand_chip = &host->nand_chip; |
Boris BREZILLON | ac01efe | 2015-12-10 08:59:50 +0100 | [diff] [blame] | 2150 | mtd = nand_to_mtd(nand_chip); |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 2151 | host->dev = &pdev->dev; |
Josh Wu | e9d8da8 | 2013-09-18 11:31:19 +0800 | [diff] [blame] | 2152 | if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) { |
Brian Norris | a61ae81 | 2015-10-30 20:33:25 -0700 | [diff] [blame] | 2153 | nand_set_flash_node(nand_chip, pdev->dev.of_node); |
Josh Wu | e9d8da8 | 2013-09-18 11:31:19 +0800 | [diff] [blame] | 2154 | /* Only when CONFIG_OF is enabled of_node can be parsed */ |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 2155 | res = atmel_of_init_port(host, pdev->dev.of_node); |
| 2156 | if (res) |
Jean-Christophe PLAGNIOL-VILLARD | 0d63748 | 2013-08-05 19:14:33 +0800 | [diff] [blame] | 2157 | goto err_nand_ioremap; |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 2158 | } else { |
Jingoo Han | 453810b | 2013-07-30 17:18:33 +0900 | [diff] [blame] | 2159 | memcpy(&host->board, dev_get_platdata(&pdev->dev), |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 2160 | sizeof(struct atmel_nand_data)); |
Boris Brezillon | 4f3cab9 | 2016-04-01 14:54:22 +0200 | [diff] [blame] | 2161 | nand_chip->ecc.mode = host->board.ecc_mode; |
| 2162 | |
| 2163 | /* 16-bit bus width */ |
| 2164 | if (host->board.bus_width_16) |
| 2165 | nand_chip->options |= NAND_BUSWIDTH_16; |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 2166 | } |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 2167 | |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 2168 | /* link the private data structures */ |
| 2169 | nand_set_controller_data(nand_chip, host); |
Frans Klaver | 03c287d | 2015-06-10 22:38:36 +0200 | [diff] [blame] | 2170 | mtd->dev.parent = &pdev->dev; |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 2171 | |
| 2172 | /* Set address of NAND IO lines */ |
| 2173 | nand_chip->IO_ADDR_R = host->io_base; |
| 2174 | nand_chip->IO_ADDR_W = host->io_base; |
Ivan Kuten | a4265f8 | 2007-05-24 14:35:58 +0300 | [diff] [blame] | 2175 | |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 2176 | if (nand_nfc.is_initialized) { |
| 2177 | /* NFC driver is probed and initialized */ |
| 2178 | host->nfc = &nand_nfc; |
| 2179 | |
| 2180 | nand_chip->select_chip = nfc_select_chip; |
| 2181 | nand_chip->dev_ready = nfc_device_ready; |
| 2182 | nand_chip->cmdfunc = nfc_nand_command; |
| 2183 | |
| 2184 | /* Initialize the interrupt for NFC */ |
| 2185 | irq = platform_get_irq(pdev, 0); |
| 2186 | if (irq < 0) { |
| 2187 | dev_err(host->dev, "Cannot get HSMC irq!\n"); |
Wei Yongjun | ff52c67 | 2013-08-23 10:50:36 +0800 | [diff] [blame] | 2188 | res = irq; |
Jean-Christophe PLAGNIOL-VILLARD | 0d63748 | 2013-08-05 19:14:33 +0800 | [diff] [blame] | 2189 | goto err_nand_ioremap; |
Jean-Christophe PLAGNIOL-VILLARD | 28446ac | 2012-07-12 10:31:08 +0200 | [diff] [blame] | 2190 | } |
| 2191 | |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 2192 | res = devm_request_irq(&pdev->dev, irq, hsmc_interrupt, |
| 2193 | 0, "hsmc", host); |
| 2194 | if (res) { |
| 2195 | dev_err(&pdev->dev, "Unable to request HSMC irq %d\n", |
| 2196 | irq); |
Jean-Christophe PLAGNIOL-VILLARD | 0d63748 | 2013-08-05 19:14:33 +0800 | [diff] [blame] | 2197 | goto err_nand_ioremap; |
Jean-Christophe PLAGNIOL-VILLARD | 28446ac | 2012-07-12 10:31:08 +0200 | [diff] [blame] | 2198 | } |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 2199 | } else { |
| 2200 | res = atmel_nand_set_enable_ready_pins(mtd); |
| 2201 | if (res) |
Jean-Christophe PLAGNIOL-VILLARD | 0d63748 | 2013-08-05 19:14:33 +0800 | [diff] [blame] | 2202 | goto err_nand_ioremap; |
Jean-Christophe PLAGNIOL-VILLARD | 28446ac | 2012-07-12 10:31:08 +0200 | [diff] [blame] | 2203 | |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 2204 | nand_chip->cmd_ctrl = atmel_nand_cmd_ctrl; |
Jean-Christophe PLAGNIOL-VILLARD | 28446ac | 2012-07-12 10:31:08 +0200 | [diff] [blame] | 2205 | } |
Ivan Kuten | a4265f8 | 2007-05-24 14:35:58 +0300 | [diff] [blame] | 2206 | |
Raphaël Poggi | 796fe36 | 2014-07-29 15:27:27 +0200 | [diff] [blame] | 2207 | nand_chip->chip_delay = 40; /* 40us command delay time */ |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 2208 | |
Hong Xu | cbc6c5e | 2011-01-18 14:36:05 +0800 | [diff] [blame] | 2209 | |
| 2210 | nand_chip->read_buf = atmel_read_buf; |
| 2211 | nand_chip->write_buf = atmel_write_buf; |
Andrew Victor | dd11b8c | 2006-12-08 13:49:42 +0200 | [diff] [blame] | 2212 | |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 2213 | platform_set_drvdata(pdev, host); |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 2214 | atmel_nand_enable(host); |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 2215 | |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 2216 | if (gpio_is_valid(host->board.det_pin)) { |
Jean-Christophe PLAGNIOL-VILLARD | 0d63748 | 2013-08-05 19:14:33 +0800 | [diff] [blame] | 2217 | res = devm_gpio_request(&pdev->dev, |
| 2218 | host->board.det_pin, "nand_det"); |
Jean-Christophe PLAGNIOL-VILLARD | 28446ac | 2012-07-12 10:31:08 +0200 | [diff] [blame] | 2219 | if (res < 0) { |
| 2220 | dev_err(&pdev->dev, |
| 2221 | "can't request det gpio %d\n", |
| 2222 | host->board.det_pin); |
| 2223 | goto err_no_card; |
| 2224 | } |
| 2225 | |
| 2226 | res = gpio_direction_input(host->board.det_pin); |
| 2227 | if (res < 0) { |
| 2228 | dev_err(&pdev->dev, |
| 2229 | "can't request input direction det gpio %d\n", |
| 2230 | host->board.det_pin); |
| 2231 | goto err_no_card; |
| 2232 | } |
| 2233 | |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 2234 | if (gpio_get_value(host->board.det_pin)) { |
Jingoo Han | 1295f97 | 2013-12-26 12:30:58 +0900 | [diff] [blame] | 2235 | dev_info(&pdev->dev, "No SmartMedia card inserted.\n"); |
Roel Kluin | 895fb49 | 2009-11-11 21:47:06 +0100 | [diff] [blame] | 2236 | res = -ENXIO; |
Håvard Skinnemoen | cc0c72e | 2008-06-06 18:04:54 +0200 | [diff] [blame] | 2237 | goto err_no_card; |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 2238 | } |
| 2239 | } |
| 2240 | |
Josh Wu | 1b71926 | 2013-05-09 15:34:55 +0800 | [diff] [blame] | 2241 | if (!host->board.has_dma) |
Hong Xu | cb457a4 | 2011-03-30 16:26:41 +0800 | [diff] [blame] | 2242 | use_dma = 0; |
| 2243 | |
| 2244 | if (use_dma) { |
Hong Xu | cbc6c5e | 2011-01-18 14:36:05 +0800 | [diff] [blame] | 2245 | dma_cap_mask_t mask; |
| 2246 | |
| 2247 | dma_cap_zero(mask); |
| 2248 | dma_cap_set(DMA_MEMCPY, mask); |
Nicolas Ferre | 201ab53 | 2011-06-29 18:41:16 +0200 | [diff] [blame] | 2249 | host->dma_chan = dma_request_channel(mask, NULL, NULL); |
Hong Xu | cbc6c5e | 2011-01-18 14:36:05 +0800 | [diff] [blame] | 2250 | if (!host->dma_chan) { |
| 2251 | dev_err(host->dev, "Failed to request DMA channel\n"); |
| 2252 | use_dma = 0; |
| 2253 | } |
| 2254 | } |
| 2255 | if (use_dma) |
Nicolas Ferre | 042bc9c | 2011-03-30 16:26:40 +0800 | [diff] [blame] | 2256 | dev_info(host->dev, "Using %s for DMA transfers.\n", |
| 2257 | dma_chan_name(host->dma_chan)); |
Hong Xu | cbc6c5e | 2011-01-18 14:36:05 +0800 | [diff] [blame] | 2258 | else |
| 2259 | dev_info(host->dev, "No DMA support for NAND access.\n"); |
| 2260 | |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 2261 | /* first scan to find the device and get the page size */ |
David Woodhouse | 5e81e88 | 2010-02-26 18:32:56 +0000 | [diff] [blame] | 2262 | if (nand_scan_ident(mtd, 1, NULL)) { |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 2263 | res = -ENXIO; |
Håvard Skinnemoen | cc0c72e | 2008-06-06 18:04:54 +0200 | [diff] [blame] | 2264 | goto err_scan_ident; |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 2265 | } |
| 2266 | |
Boris Brezillon | 4f3cab9 | 2016-04-01 14:54:22 +0200 | [diff] [blame] | 2267 | if (host->board.on_flash_bbt || on_flash_bbt) |
| 2268 | nand_chip->bbt_options |= NAND_BBT_USE_FLASH; |
| 2269 | |
| 2270 | if (nand_chip->bbt_options & NAND_BBT_USE_FLASH) |
| 2271 | dev_info(&pdev->dev, "Use On Flash BBT\n"); |
| 2272 | |
| 2273 | if (IS_ENABLED(CONFIG_OF) && pdev->dev.of_node) { |
| 2274 | res = atmel_of_init_ecc(host, pdev->dev.of_node); |
| 2275 | if (res) |
| 2276 | goto err_hw_ecc; |
| 2277 | } |
| 2278 | |
Richard Genoud | 3fc2389 | 2008-10-12 08:42:28 +0200 | [diff] [blame] | 2279 | if (nand_chip->ecc.mode == NAND_ECC_HW) { |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 2280 | if (host->has_pmecc) |
| 2281 | res = atmel_pmecc_nand_init_params(pdev, host); |
| 2282 | else |
| 2283 | res = atmel_hw_nand_init_params(pdev, host); |
| 2284 | |
Josh Wu | 3dfe41a | 2012-06-25 18:07:43 +0800 | [diff] [blame] | 2285 | if (res != 0) |
| 2286 | goto err_hw_ecc; |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 2287 | } |
| 2288 | |
Josh Wu | 1ae9c09 | 2013-08-05 19:14:36 +0800 | [diff] [blame] | 2289 | /* initialize the nfc configuration register */ |
| 2290 | if (host->nfc && host->nfc->use_nfc_sram) { |
| 2291 | res = nfc_sram_init(mtd); |
| 2292 | if (res) { |
| 2293 | host->nfc->use_nfc_sram = false; |
| 2294 | dev_err(host->dev, "Disable use nfc sram for data transfer.\n"); |
| 2295 | } |
| 2296 | } |
| 2297 | |
Richard Genoud | 77f5492 | 2008-04-23 19:51:14 +0200 | [diff] [blame] | 2298 | /* second phase scan */ |
| 2299 | if (nand_scan_tail(mtd)) { |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 2300 | res = -ENXIO; |
Håvard Skinnemoen | cc0c72e | 2008-06-06 18:04:54 +0200 | [diff] [blame] | 2301 | goto err_scan_tail; |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 2302 | } |
| 2303 | |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 2304 | mtd->name = "atmel_nand"; |
Brian Norris | a61ae81 | 2015-10-30 20:33:25 -0700 | [diff] [blame] | 2305 | res = mtd_device_register(mtd, host->board.parts, |
| 2306 | host->board.num_parts); |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 2307 | if (!res) |
| 2308 | return res; |
| 2309 | |
Håvard Skinnemoen | cc0c72e | 2008-06-06 18:04:54 +0200 | [diff] [blame] | 2310 | err_scan_tail: |
Jean-Christophe PLAGNIOL-VILLARD | 0d63748 | 2013-08-05 19:14:33 +0800 | [diff] [blame] | 2311 | if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW) |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 2312 | pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE); |
Josh Wu | 3dfe41a | 2012-06-25 18:07:43 +0800 | [diff] [blame] | 2313 | err_hw_ecc: |
Håvard Skinnemoen | cc0c72e | 2008-06-06 18:04:54 +0200 | [diff] [blame] | 2314 | err_scan_ident: |
| 2315 | err_no_card: |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 2316 | atmel_nand_disable(host); |
Hong Xu | cbc6c5e | 2011-01-18 14:36:05 +0800 | [diff] [blame] | 2317 | if (host->dma_chan) |
| 2318 | dma_release_channel(host->dma_chan); |
Håvard Skinnemoen | cc0c72e | 2008-06-06 18:04:54 +0200 | [diff] [blame] | 2319 | err_nand_ioremap: |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 2320 | return res; |
| 2321 | } |
| 2322 | |
| 2323 | /* |
| 2324 | * Remove a NAND device. |
| 2325 | */ |
Johan Hovold | 2c2b928 | 2013-09-23 16:27:28 +0200 | [diff] [blame] | 2326 | static int atmel_nand_remove(struct platform_device *pdev) |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 2327 | { |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 2328 | struct atmel_nand_host *host = platform_get_drvdata(pdev); |
Boris BREZILLON | ac01efe | 2015-12-10 08:59:50 +0100 | [diff] [blame] | 2329 | struct mtd_info *mtd = nand_to_mtd(&host->nand_chip); |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 2330 | |
| 2331 | nand_release(mtd); |
| 2332 | |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 2333 | atmel_nand_disable(host); |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 2334 | |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 2335 | if (host->has_pmecc && host->nand_chip.ecc.mode == NAND_ECC_HW) { |
| 2336 | pmecc_writel(host->ecc, CTRL, PMECC_CTRL_DISABLE); |
| 2337 | pmerrloc_writel(host->pmerrloc_base, ELDIS, |
| 2338 | PMERRLOC_DISABLE); |
Josh Wu | 1c7b874 | 2012-06-29 17:47:55 +0800 | [diff] [blame] | 2339 | } |
| 2340 | |
Hong Xu | cbc6c5e | 2011-01-18 14:36:05 +0800 | [diff] [blame] | 2341 | if (host->dma_chan) |
| 2342 | dma_release_channel(host->dma_chan); |
| 2343 | |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 2344 | platform_driver_unregister(&atmel_nand_nfc_driver); |
| 2345 | |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 2346 | return 0; |
| 2347 | } |
| 2348 | |
Romain Izard | 5575075 | 2016-02-10 10:56:25 +0100 | [diff] [blame] | 2349 | /* |
| 2350 | * AT91RM9200 does not have PMECC or PMECC Errloc peripherals for |
| 2351 | * BCH ECC. Combined with the "atmel,has-pmecc", it is used to describe |
| 2352 | * devices from the SAM9 family that have those. |
| 2353 | */ |
LABBE Corentin | 72eaec2 | 2015-11-20 08:45:16 +0100 | [diff] [blame] | 2354 | static const struct atmel_nand_caps at91rm9200_caps = { |
Wu, Josh | 5158577 | 2015-01-19 16:33:06 +0800 | [diff] [blame] | 2355 | .pmecc_correct_erase_page = false, |
Romain Izard | 5575075 | 2016-02-10 10:56:25 +0100 | [diff] [blame] | 2356 | .pmecc_max_correction = 24, |
Wu, Josh | 5158577 | 2015-01-19 16:33:06 +0800 | [diff] [blame] | 2357 | }; |
| 2358 | |
LABBE Corentin | 72eaec2 | 2015-11-20 08:45:16 +0100 | [diff] [blame] | 2359 | static const struct atmel_nand_caps sama5d4_caps = { |
Wu, Josh | 5158577 | 2015-01-19 16:33:06 +0800 | [diff] [blame] | 2360 | .pmecc_correct_erase_page = true, |
Romain Izard | 5575075 | 2016-02-10 10:56:25 +0100 | [diff] [blame] | 2361 | .pmecc_max_correction = 24, |
| 2362 | }; |
| 2363 | |
| 2364 | /* |
| 2365 | * The PMECC Errloc controller starting in SAMA5D2 is not compatible, |
| 2366 | * as the increased correction strength requires more registers. |
| 2367 | */ |
| 2368 | static const struct atmel_nand_caps sama5d2_caps = { |
| 2369 | .pmecc_correct_erase_page = true, |
| 2370 | .pmecc_max_correction = 32, |
Wu, Josh | 5158577 | 2015-01-19 16:33:06 +0800 | [diff] [blame] | 2371 | }; |
| 2372 | |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 2373 | static const struct of_device_id atmel_nand_dt_ids[] = { |
Wu, Josh | 5158577 | 2015-01-19 16:33:06 +0800 | [diff] [blame] | 2374 | { .compatible = "atmel,at91rm9200-nand", .data = &at91rm9200_caps }, |
| 2375 | { .compatible = "atmel,sama5d4-nand", .data = &sama5d4_caps }, |
Romain Izard | 5575075 | 2016-02-10 10:56:25 +0100 | [diff] [blame] | 2376 | { .compatible = "atmel,sama5d2-nand", .data = &sama5d2_caps }, |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 2377 | { /* sentinel */ } |
| 2378 | }; |
| 2379 | |
| 2380 | MODULE_DEVICE_TABLE(of, atmel_nand_dt_ids); |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 2381 | |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 2382 | static int atmel_nand_nfc_probe(struct platform_device *pdev) |
| 2383 | { |
| 2384 | struct atmel_nfc *nfc = &nand_nfc; |
| 2385 | struct resource *nfc_cmd_regs, *nfc_hsmc_regs, *nfc_sram; |
Boris BREZILLON | 2d405ec | 2014-09-13 01:23:59 +0200 | [diff] [blame] | 2386 | int ret; |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 2387 | |
| 2388 | nfc_cmd_regs = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 2389 | nfc->base_cmd_regs = devm_ioremap_resource(&pdev->dev, nfc_cmd_regs); |
| 2390 | if (IS_ERR(nfc->base_cmd_regs)) |
| 2391 | return PTR_ERR(nfc->base_cmd_regs); |
| 2392 | |
| 2393 | nfc_hsmc_regs = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 2394 | nfc->hsmc_regs = devm_ioremap_resource(&pdev->dev, nfc_hsmc_regs); |
| 2395 | if (IS_ERR(nfc->hsmc_regs)) |
| 2396 | return PTR_ERR(nfc->hsmc_regs); |
| 2397 | |
| 2398 | nfc_sram = platform_get_resource(pdev, IORESOURCE_MEM, 2); |
| 2399 | if (nfc_sram) { |
Wu, Josh | 068b44b | 2014-11-07 15:26:09 +0800 | [diff] [blame] | 2400 | nfc->sram_bank0 = (void * __force) |
| 2401 | devm_ioremap_resource(&pdev->dev, nfc_sram); |
Josh Wu | 1ae9c09 | 2013-08-05 19:14:36 +0800 | [diff] [blame] | 2402 | if (IS_ERR(nfc->sram_bank0)) { |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 2403 | dev_warn(&pdev->dev, "Fail to ioremap the NFC sram with error: %ld. So disable NFC sram.\n", |
| 2404 | PTR_ERR(nfc->sram_bank0)); |
Josh Wu | 1ae9c09 | 2013-08-05 19:14:36 +0800 | [diff] [blame] | 2405 | } else { |
| 2406 | nfc->use_nfc_sram = true; |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 2407 | nfc->sram_bank0_phys = (dma_addr_t)nfc_sram->start; |
Josh Wu | 6054d4d | 2013-08-05 19:14:37 +0800 | [diff] [blame] | 2408 | |
| 2409 | if (pdev->dev.of_node) |
| 2410 | nfc->write_by_sram = of_property_read_bool( |
| 2411 | pdev->dev.of_node, |
| 2412 | "atmel,write-by-sram"); |
Josh Wu | 1ae9c09 | 2013-08-05 19:14:36 +0800 | [diff] [blame] | 2413 | } |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 2414 | } |
| 2415 | |
Romain Izard | 5ddc7bd | 2016-02-10 10:56:23 +0100 | [diff] [blame] | 2416 | nfc->caps = (const struct atmel_nand_nfc_caps *) |
| 2417 | of_device_get_match_data(&pdev->dev); |
| 2418 | if (!nfc->caps) |
| 2419 | return -ENODEV; |
| 2420 | |
Wu, Josh | 50e04e2 | 2014-06-10 17:50:09 +0800 | [diff] [blame] | 2421 | nfc_writel(nfc->hsmc_regs, IDR, 0xffffffff); |
| 2422 | nfc_readl(nfc->hsmc_regs, SR); /* clear the NFC_SR */ |
| 2423 | |
Boris BREZILLON | 2d405ec | 2014-09-13 01:23:59 +0200 | [diff] [blame] | 2424 | nfc->clk = devm_clk_get(&pdev->dev, NULL); |
| 2425 | if (!IS_ERR(nfc->clk)) { |
| 2426 | ret = clk_prepare_enable(nfc->clk); |
| 2427 | if (ret) |
| 2428 | return ret; |
| 2429 | } else { |
| 2430 | dev_warn(&pdev->dev, "NFC clock missing, update your Device Tree"); |
| 2431 | } |
| 2432 | |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 2433 | nfc->is_initialized = true; |
| 2434 | dev_info(&pdev->dev, "NFC is probed.\n"); |
Boris BREZILLON | 2d405ec | 2014-09-13 01:23:59 +0200 | [diff] [blame] | 2435 | |
| 2436 | return 0; |
| 2437 | } |
| 2438 | |
| 2439 | static int atmel_nand_nfc_remove(struct platform_device *pdev) |
| 2440 | { |
| 2441 | struct atmel_nfc *nfc = &nand_nfc; |
| 2442 | |
| 2443 | if (!IS_ERR(nfc->clk)) |
| 2444 | clk_disable_unprepare(nfc->clk); |
| 2445 | |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 2446 | return 0; |
| 2447 | } |
| 2448 | |
Romain Izard | 5ddc7bd | 2016-02-10 10:56:23 +0100 | [diff] [blame] | 2449 | static const struct atmel_nand_nfc_caps sama5d3_nfc_caps = { |
| 2450 | .rb_mask = NFC_SR_RB_EDGE0, |
| 2451 | }; |
| 2452 | |
| 2453 | static const struct atmel_nand_nfc_caps sama5d4_nfc_caps = { |
| 2454 | .rb_mask = NFC_SR_RB_EDGE3, |
| 2455 | }; |
| 2456 | |
Josh Wu | 81f29b4 | 2013-09-18 11:31:20 +0800 | [diff] [blame] | 2457 | static const struct of_device_id atmel_nand_nfc_match[] = { |
Romain Izard | 5ddc7bd | 2016-02-10 10:56:23 +0100 | [diff] [blame] | 2458 | { .compatible = "atmel,sama5d3-nfc", .data = &sama5d3_nfc_caps }, |
| 2459 | { .compatible = "atmel,sama5d4-nfc", .data = &sama5d4_nfc_caps }, |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 2460 | { /* sentinel */ } |
| 2461 | }; |
Josh Wu | 81f29b4 | 2013-09-18 11:31:20 +0800 | [diff] [blame] | 2462 | MODULE_DEVICE_TABLE(of, atmel_nand_nfc_match); |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 2463 | |
| 2464 | static struct platform_driver atmel_nand_nfc_driver = { |
| 2465 | .driver = { |
| 2466 | .name = "atmel_nand_nfc", |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 2467 | .of_match_table = of_match_ptr(atmel_nand_nfc_match), |
| 2468 | }, |
| 2469 | .probe = atmel_nand_nfc_probe, |
Boris BREZILLON | 2d405ec | 2014-09-13 01:23:59 +0200 | [diff] [blame] | 2470 | .remove = atmel_nand_nfc_remove, |
Josh Wu | 7dc37de | 2013-08-05 19:14:35 +0800 | [diff] [blame] | 2471 | }; |
| 2472 | |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 2473 | static struct platform_driver atmel_nand_driver = { |
Johan Hovold | 2c2b928 | 2013-09-23 16:27:28 +0200 | [diff] [blame] | 2474 | .probe = atmel_nand_probe, |
| 2475 | .remove = atmel_nand_remove, |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 2476 | .driver = { |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 2477 | .name = "atmel_nand", |
Jean-Christophe PLAGNIOL-VILLARD | d6a0166 | 2012-01-26 02:11:06 +0800 | [diff] [blame] | 2478 | .of_match_table = of_match_ptr(atmel_nand_dt_ids), |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 2479 | }, |
| 2480 | }; |
| 2481 | |
Johan Hovold | 2c2b928 | 2013-09-23 16:27:28 +0200 | [diff] [blame] | 2482 | module_platform_driver(atmel_nand_driver); |
Andrew Victor | 42cb140 | 2006-10-19 18:24:35 +0200 | [diff] [blame] | 2483 | |
| 2484 | MODULE_LICENSE("GPL"); |
| 2485 | MODULE_AUTHOR("Rick Bronson"); |
Håvard Skinnemoen | d4f4c0a | 2008-06-06 18:04:52 +0200 | [diff] [blame] | 2486 | MODULE_DESCRIPTION("NAND/SmartMedia driver for AT91 / AVR32"); |
Håvard Skinnemoen | 3c3796c | 2008-06-06 18:04:53 +0200 | [diff] [blame] | 2487 | MODULE_ALIAS("platform:atmel_nand"); |