Fabio Estevam | b04a3fe | 2018-07-07 14:25:20 -0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Freescale GPMI NAND Flash Driver |
| 4 | * |
Huang Shijie | 026918e | 2015-12-02 16:47:40 -0600 | [diff] [blame] | 5 | * Copyright (C) 2010-2015 Freescale Semiconductor, Inc. |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 6 | * Copyright (C) 2008 Embedded Alley Solutions, Inc. |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 7 | */ |
| 8 | #include <linux/clk.h> |
| 9 | #include <linux/slab.h> |
Ingo Molnar | 68db0cf | 2017-02-08 18:51:37 +0100 | [diff] [blame] | 10 | #include <linux/sched/task_stack.h> |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 11 | #include <linux/interrupt.h> |
Wolfram Sang | df16c86 | 2011-11-23 15:57:06 +0100 | [diff] [blame] | 12 | #include <linux/module.h> |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 13 | #include <linux/mtd/partitions.h> |
Huang Shijie | e10db1f | 2012-05-04 21:42:05 -0400 | [diff] [blame] | 14 | #include <linux/of.h> |
| 15 | #include <linux/of_device.h> |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 16 | #include "gpmi-nand.h" |
Huang Shijie | b8e2931 | 2014-01-03 11:01:42 +0800 | [diff] [blame] | 17 | #include "bch-regs.h" |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 18 | |
Huang Shijie | 5de0b52 | 2012-10-13 13:03:29 -0400 | [diff] [blame] | 19 | /* Resource names for the GPMI NAND driver. */ |
| 20 | #define GPMI_NAND_GPMI_REGS_ADDR_RES_NAME "gpmi-nand" |
| 21 | #define GPMI_NAND_BCH_REGS_ADDR_RES_NAME "bch" |
| 22 | #define GPMI_NAND_BCH_INTERRUPT_RES_NAME "bch" |
Huang Shijie | 5de0b52 | 2012-10-13 13:03:29 -0400 | [diff] [blame] | 23 | |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 24 | /* add our owner bbt descriptor */ |
| 25 | static uint8_t scan_ff_pattern[] = { 0xff }; |
| 26 | static struct nand_bbt_descr gpmi_bbt_descr = { |
| 27 | .options = 0, |
| 28 | .offs = 0, |
| 29 | .len = 1, |
| 30 | .pattern = scan_ff_pattern |
| 31 | }; |
| 32 | |
Huang Shijie | 7a2b89a | 2013-09-25 14:58:15 +0800 | [diff] [blame] | 33 | /* |
| 34 | * We may change the layout if we can get the ECC info from the datasheet, |
| 35 | * else we will use all the (page + OOB). |
| 36 | */ |
Boris Brezillon | 3f158e4 | 2016-02-03 20:01:54 +0100 | [diff] [blame] | 37 | static int gpmi_ooblayout_ecc(struct mtd_info *mtd, int section, |
| 38 | struct mtd_oob_region *oobregion) |
| 39 | { |
| 40 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 41 | struct gpmi_nand_data *this = nand_get_controller_data(chip); |
| 42 | struct bch_geometry *geo = &this->bch_geometry; |
| 43 | |
| 44 | if (section) |
| 45 | return -ERANGE; |
| 46 | |
| 47 | oobregion->offset = 0; |
| 48 | oobregion->length = geo->page_size - mtd->writesize; |
| 49 | |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | static int gpmi_ooblayout_free(struct mtd_info *mtd, int section, |
| 54 | struct mtd_oob_region *oobregion) |
| 55 | { |
| 56 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 57 | struct gpmi_nand_data *this = nand_get_controller_data(chip); |
| 58 | struct bch_geometry *geo = &this->bch_geometry; |
| 59 | |
| 60 | if (section) |
| 61 | return -ERANGE; |
| 62 | |
| 63 | /* The available oob size we have. */ |
| 64 | if (geo->page_size < mtd->writesize + mtd->oobsize) { |
| 65 | oobregion->offset = geo->page_size - mtd->writesize; |
| 66 | oobregion->length = mtd->oobsize - oobregion->offset; |
| 67 | } |
| 68 | |
| 69 | return 0; |
| 70 | } |
| 71 | |
Stefan Agner | 6b7ee72 | 2017-04-21 18:23:34 -0700 | [diff] [blame] | 72 | static const char * const gpmi_clks_for_mx2x[] = { |
| 73 | "gpmi_io", |
| 74 | }; |
| 75 | |
Boris Brezillon | 3f158e4 | 2016-02-03 20:01:54 +0100 | [diff] [blame] | 76 | static const struct mtd_ooblayout_ops gpmi_ooblayout_ops = { |
| 77 | .ecc = gpmi_ooblayout_ecc, |
| 78 | .free = gpmi_ooblayout_free, |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 79 | }; |
| 80 | |
Huang Shijie | 6189ccc | 2014-03-21 18:19:39 +0800 | [diff] [blame] | 81 | static const struct gpmi_devdata gpmi_devdata_imx23 = { |
| 82 | .type = IS_MX23, |
| 83 | .bch_max_ecc_strength = 20, |
Miquel Raynal | b120612 | 2018-03-02 15:38:40 +0100 | [diff] [blame] | 84 | .max_chain_delay = 16000, |
Stefan Agner | 6b7ee72 | 2017-04-21 18:23:34 -0700 | [diff] [blame] | 85 | .clks = gpmi_clks_for_mx2x, |
| 86 | .clks_count = ARRAY_SIZE(gpmi_clks_for_mx2x), |
Huang Shijie | 6189ccc | 2014-03-21 18:19:39 +0800 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | static const struct gpmi_devdata gpmi_devdata_imx28 = { |
| 90 | .type = IS_MX28, |
| 91 | .bch_max_ecc_strength = 20, |
Miquel Raynal | b120612 | 2018-03-02 15:38:40 +0100 | [diff] [blame] | 92 | .max_chain_delay = 16000, |
Stefan Agner | 6b7ee72 | 2017-04-21 18:23:34 -0700 | [diff] [blame] | 93 | .clks = gpmi_clks_for_mx2x, |
| 94 | .clks_count = ARRAY_SIZE(gpmi_clks_for_mx2x), |
| 95 | }; |
| 96 | |
| 97 | static const char * const gpmi_clks_for_mx6[] = { |
| 98 | "gpmi_io", "gpmi_apb", "gpmi_bch", "gpmi_bch_apb", "per1_bch", |
Huang Shijie | 6189ccc | 2014-03-21 18:19:39 +0800 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | static const struct gpmi_devdata gpmi_devdata_imx6q = { |
| 102 | .type = IS_MX6Q, |
| 103 | .bch_max_ecc_strength = 40, |
Miquel Raynal | b120612 | 2018-03-02 15:38:40 +0100 | [diff] [blame] | 104 | .max_chain_delay = 12000, |
Stefan Agner | 6b7ee72 | 2017-04-21 18:23:34 -0700 | [diff] [blame] | 105 | .clks = gpmi_clks_for_mx6, |
| 106 | .clks_count = ARRAY_SIZE(gpmi_clks_for_mx6), |
Huang Shijie | 6189ccc | 2014-03-21 18:19:39 +0800 | [diff] [blame] | 107 | }; |
| 108 | |
Huang Shijie | 91f5498 | 2014-03-27 10:43:22 +0800 | [diff] [blame] | 109 | static const struct gpmi_devdata gpmi_devdata_imx6sx = { |
| 110 | .type = IS_MX6SX, |
| 111 | .bch_max_ecc_strength = 62, |
Miquel Raynal | b120612 | 2018-03-02 15:38:40 +0100 | [diff] [blame] | 112 | .max_chain_delay = 12000, |
Stefan Agner | 6b7ee72 | 2017-04-21 18:23:34 -0700 | [diff] [blame] | 113 | .clks = gpmi_clks_for_mx6, |
| 114 | .clks_count = ARRAY_SIZE(gpmi_clks_for_mx6), |
Huang Shijie | 91f5498 | 2014-03-27 10:43:22 +0800 | [diff] [blame] | 115 | }; |
| 116 | |
Stefan Agner | b4af694 | 2017-04-21 18:23:35 -0700 | [diff] [blame] | 117 | static const char * const gpmi_clks_for_mx7d[] = { |
| 118 | "gpmi_io", "gpmi_bch_apb", |
| 119 | }; |
| 120 | |
| 121 | static const struct gpmi_devdata gpmi_devdata_imx7d = { |
| 122 | .type = IS_MX7D, |
| 123 | .bch_max_ecc_strength = 62, |
Miquel Raynal | b120612 | 2018-03-02 15:38:40 +0100 | [diff] [blame] | 124 | .max_chain_delay = 12000, |
Stefan Agner | b4af694 | 2017-04-21 18:23:35 -0700 | [diff] [blame] | 125 | .clks = gpmi_clks_for_mx7d, |
| 126 | .clks_count = ARRAY_SIZE(gpmi_clks_for_mx7d), |
| 127 | }; |
| 128 | |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 129 | static irqreturn_t bch_irq(int irq, void *cookie) |
| 130 | { |
| 131 | struct gpmi_nand_data *this = cookie; |
| 132 | |
| 133 | gpmi_clear_bch(this); |
| 134 | complete(&this->bch_done); |
| 135 | return IRQ_HANDLED; |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | * Calculate the ECC strength by hand: |
| 140 | * E : The ECC strength. |
| 141 | * G : the length of Galois Field. |
| 142 | * N : The chunk count of per page. |
| 143 | * O : the oobsize of the NAND chip. |
| 144 | * M : the metasize of per page. |
| 145 | * |
| 146 | * The formula is : |
| 147 | * E * G * N |
| 148 | * ------------ <= (O - M) |
| 149 | * 8 |
| 150 | * |
| 151 | * So, we get E by: |
| 152 | * (O - M) * 8 |
| 153 | * E <= ------------- |
| 154 | * G * N |
| 155 | */ |
| 156 | static inline int get_ecc_strength(struct gpmi_nand_data *this) |
| 157 | { |
| 158 | struct bch_geometry *geo = &this->bch_geometry; |
Boris BREZILLON | 2a690b2 | 2015-12-10 09:00:07 +0100 | [diff] [blame] | 159 | struct mtd_info *mtd = nand_to_mtd(&this->nand); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 160 | int ecc_strength; |
| 161 | |
| 162 | ecc_strength = ((mtd->oobsize - geo->metadata_size) * 8) |
| 163 | / (geo->gf_len * geo->ecc_chunk_count); |
| 164 | |
| 165 | /* We need the minor even number. */ |
| 166 | return round_down(ecc_strength, 2); |
| 167 | } |
| 168 | |
Huang Shijie | 92d0e09 | 2013-01-29 09:23:38 +0800 | [diff] [blame] | 169 | static inline bool gpmi_check_ecc(struct gpmi_nand_data *this) |
| 170 | { |
| 171 | struct bch_geometry *geo = &this->bch_geometry; |
| 172 | |
| 173 | /* Do the sanity check. */ |
| 174 | if (GPMI_IS_MX23(this) || GPMI_IS_MX28(this)) { |
| 175 | /* The mx23/mx28 only support the GF13. */ |
| 176 | if (geo->gf_len == 14) |
| 177 | return false; |
Huang Shijie | 92d0e09 | 2013-01-29 09:23:38 +0800 | [diff] [blame] | 178 | } |
Huang Shijie | 6189ccc | 2014-03-21 18:19:39 +0800 | [diff] [blame] | 179 | return geo->ecc_strength <= this->devdata->bch_max_ecc_strength; |
Huang Shijie | 92d0e09 | 2013-01-29 09:23:38 +0800 | [diff] [blame] | 180 | } |
| 181 | |
Huang Shijie | 2febcdf | 2013-05-17 11:17:34 +0800 | [diff] [blame] | 182 | /* |
| 183 | * If we can get the ECC information from the nand chip, we do not |
| 184 | * need to calculate them ourselves. |
| 185 | * |
| 186 | * We may have available oob space in this case. |
| 187 | */ |
Stefan Agner | 6bf6ec5 | 2018-03-04 21:06:01 +0100 | [diff] [blame] | 188 | static int set_geometry_by_ecc_info(struct gpmi_nand_data *this, |
| 189 | unsigned int ecc_strength, |
| 190 | unsigned int ecc_step) |
Huang Shijie | 2febcdf | 2013-05-17 11:17:34 +0800 | [diff] [blame] | 191 | { |
| 192 | struct bch_geometry *geo = &this->bch_geometry; |
Boris BREZILLON | 2a690b2 | 2015-12-10 09:00:07 +0100 | [diff] [blame] | 193 | struct nand_chip *chip = &this->nand; |
| 194 | struct mtd_info *mtd = nand_to_mtd(chip); |
Huang Shijie | 2febcdf | 2013-05-17 11:17:34 +0800 | [diff] [blame] | 195 | unsigned int block_mark_bit_offset; |
| 196 | |
Stefan Agner | 6bf6ec5 | 2018-03-04 21:06:01 +0100 | [diff] [blame] | 197 | switch (ecc_step) { |
Huang Shijie | 2febcdf | 2013-05-17 11:17:34 +0800 | [diff] [blame] | 198 | case SZ_512: |
| 199 | geo->gf_len = 13; |
| 200 | break; |
| 201 | case SZ_1K: |
| 202 | geo->gf_len = 14; |
| 203 | break; |
| 204 | default: |
| 205 | dev_err(this->dev, |
| 206 | "unsupported nand chip. ecc bits : %d, ecc size : %d\n", |
| 207 | chip->ecc_strength_ds, chip->ecc_step_ds); |
Han Xu | b8b0e46 | 2015-12-02 16:47:43 -0600 | [diff] [blame] | 208 | return -EINVAL; |
Huang Shijie | 2febcdf | 2013-05-17 11:17:34 +0800 | [diff] [blame] | 209 | } |
Stefan Agner | 6bf6ec5 | 2018-03-04 21:06:01 +0100 | [diff] [blame] | 210 | geo->ecc_chunk_size = ecc_step; |
| 211 | geo->ecc_strength = round_up(ecc_strength, 2); |
Huang Shijie | 2febcdf | 2013-05-17 11:17:34 +0800 | [diff] [blame] | 212 | if (!gpmi_check_ecc(this)) |
Han Xu | b8b0e46 | 2015-12-02 16:47:43 -0600 | [diff] [blame] | 213 | return -EINVAL; |
Huang Shijie | 2febcdf | 2013-05-17 11:17:34 +0800 | [diff] [blame] | 214 | |
| 215 | /* Keep the C >= O */ |
| 216 | if (geo->ecc_chunk_size < mtd->oobsize) { |
| 217 | dev_err(this->dev, |
| 218 | "unsupported nand chip. ecc size: %d, oob size : %d\n", |
Stefan Agner | 6bf6ec5 | 2018-03-04 21:06:01 +0100 | [diff] [blame] | 219 | ecc_step, mtd->oobsize); |
Han Xu | b8b0e46 | 2015-12-02 16:47:43 -0600 | [diff] [blame] | 220 | return -EINVAL; |
Huang Shijie | 2febcdf | 2013-05-17 11:17:34 +0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /* The default value, see comment in the legacy_set_geometry(). */ |
| 224 | geo->metadata_size = 10; |
| 225 | |
| 226 | geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunk_size; |
| 227 | |
| 228 | /* |
| 229 | * Now, the NAND chip with 2K page(data chunk is 512byte) shows below: |
| 230 | * |
| 231 | * | P | |
| 232 | * |<----------------------------------------------------->| |
| 233 | * | | |
| 234 | * | (Block Mark) | |
| 235 | * | P' | | | | |
| 236 | * |<-------------------------------------------->| D | | O' | |
| 237 | * | |<---->| |<--->| |
| 238 | * V V V V V |
| 239 | * +---+----------+-+----------+-+----------+-+----------+-+-----+ |
| 240 | * | M | data |E| data |E| data |E| data |E| | |
| 241 | * +---+----------+-+----------+-+----------+-+----------+-+-----+ |
| 242 | * ^ ^ |
| 243 | * | O | |
| 244 | * |<------------>| |
| 245 | * | | |
| 246 | * |
| 247 | * P : the page size for BCH module. |
| 248 | * E : The ECC strength. |
| 249 | * G : the length of Galois Field. |
| 250 | * N : The chunk count of per page. |
| 251 | * M : the metasize of per page. |
| 252 | * C : the ecc chunk size, aka the "data" above. |
| 253 | * P': the nand chip's page size. |
| 254 | * O : the nand chip's oob size. |
| 255 | * O': the free oob. |
| 256 | * |
| 257 | * The formula for P is : |
| 258 | * |
| 259 | * E * G * N |
| 260 | * P = ------------ + P' + M |
| 261 | * 8 |
| 262 | * |
| 263 | * The position of block mark moves forward in the ECC-based view |
| 264 | * of page, and the delta is: |
| 265 | * |
| 266 | * E * G * (N - 1) |
| 267 | * D = (---------------- + M) |
| 268 | * 8 |
| 269 | * |
| 270 | * Please see the comment in legacy_set_geometry(). |
| 271 | * With the condition C >= O , we still can get same result. |
| 272 | * So the bit position of the physical block mark within the ECC-based |
| 273 | * view of the page is : |
| 274 | * (P' - D) * 8 |
| 275 | */ |
| 276 | geo->page_size = mtd->writesize + geo->metadata_size + |
| 277 | (geo->gf_len * geo->ecc_strength * geo->ecc_chunk_count) / 8; |
| 278 | |
Huang Shijie | 2febcdf | 2013-05-17 11:17:34 +0800 | [diff] [blame] | 279 | geo->payload_size = mtd->writesize; |
| 280 | |
| 281 | geo->auxiliary_status_offset = ALIGN(geo->metadata_size, 4); |
| 282 | geo->auxiliary_size = ALIGN(geo->metadata_size, 4) |
| 283 | + ALIGN(geo->ecc_chunk_count, 4); |
| 284 | |
| 285 | if (!this->swap_block_mark) |
Han Xu | b8b0e46 | 2015-12-02 16:47:43 -0600 | [diff] [blame] | 286 | return 0; |
Huang Shijie | 2febcdf | 2013-05-17 11:17:34 +0800 | [diff] [blame] | 287 | |
| 288 | /* For bit swap. */ |
| 289 | block_mark_bit_offset = mtd->writesize * 8 - |
| 290 | (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1) |
| 291 | + geo->metadata_size * 8); |
| 292 | |
| 293 | geo->block_mark_byte_offset = block_mark_bit_offset / 8; |
| 294 | geo->block_mark_bit_offset = block_mark_bit_offset % 8; |
Han Xu | b8b0e46 | 2015-12-02 16:47:43 -0600 | [diff] [blame] | 295 | return 0; |
Huang Shijie | 2febcdf | 2013-05-17 11:17:34 +0800 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | static int legacy_set_geometry(struct gpmi_nand_data *this) |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 299 | { |
| 300 | struct bch_geometry *geo = &this->bch_geometry; |
Boris BREZILLON | 2a690b2 | 2015-12-10 09:00:07 +0100 | [diff] [blame] | 301 | struct mtd_info *mtd = nand_to_mtd(&this->nand); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 302 | unsigned int metadata_size; |
| 303 | unsigned int status_size; |
| 304 | unsigned int block_mark_bit_offset; |
| 305 | |
| 306 | /* |
| 307 | * The size of the metadata can be changed, though we set it to 10 |
| 308 | * bytes now. But it can't be too large, because we have to save |
| 309 | * enough space for BCH. |
| 310 | */ |
| 311 | geo->metadata_size = 10; |
| 312 | |
| 313 | /* The default for the length of Galois Field. */ |
| 314 | geo->gf_len = 13; |
| 315 | |
Huang Shijie | 9ff16f0 | 2013-01-25 14:04:07 +0800 | [diff] [blame] | 316 | /* The default for chunk size. */ |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 317 | geo->ecc_chunk_size = 512; |
Huang Shijie | 9ff16f0 | 2013-01-25 14:04:07 +0800 | [diff] [blame] | 318 | while (geo->ecc_chunk_size < mtd->oobsize) { |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 319 | geo->ecc_chunk_size *= 2; /* keep C >= O */ |
Huang Shijie | 9ff16f0 | 2013-01-25 14:04:07 +0800 | [diff] [blame] | 320 | geo->gf_len = 14; |
| 321 | } |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 322 | |
| 323 | geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunk_size; |
| 324 | |
| 325 | /* We use the same ECC strength for all chunks. */ |
| 326 | geo->ecc_strength = get_ecc_strength(this); |
Huang Shijie | 92d0e09 | 2013-01-29 09:23:38 +0800 | [diff] [blame] | 327 | if (!gpmi_check_ecc(this)) { |
| 328 | dev_err(this->dev, |
Han Xu | b8b0e46 | 2015-12-02 16:47:43 -0600 | [diff] [blame] | 329 | "ecc strength: %d cannot be supported by the controller (%d)\n" |
| 330 | "try to use minimum ecc strength that NAND chip required\n", |
Lothar Waßmann | d8c0372 | 2014-06-12 15:20:42 +0200 | [diff] [blame] | 331 | geo->ecc_strength, |
Huang Shijie | 6189ccc | 2014-03-21 18:19:39 +0800 | [diff] [blame] | 332 | this->devdata->bch_max_ecc_strength); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 333 | return -EINVAL; |
| 334 | } |
| 335 | |
Han Xu | 1848a05 | 2016-04-12 17:06:33 -0500 | [diff] [blame] | 336 | geo->page_size = mtd->writesize + geo->metadata_size + |
| 337 | (geo->gf_len * geo->ecc_strength * geo->ecc_chunk_count) / 8; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 338 | geo->payload_size = mtd->writesize; |
| 339 | |
| 340 | /* |
| 341 | * The auxiliary buffer contains the metadata and the ECC status. The |
| 342 | * metadata is padded to the nearest 32-bit boundary. The ECC status |
| 343 | * contains one byte for every ECC chunk, and is also padded to the |
| 344 | * nearest 32-bit boundary. |
| 345 | */ |
| 346 | metadata_size = ALIGN(geo->metadata_size, 4); |
| 347 | status_size = ALIGN(geo->ecc_chunk_count, 4); |
| 348 | |
| 349 | geo->auxiliary_size = metadata_size + status_size; |
| 350 | geo->auxiliary_status_offset = metadata_size; |
| 351 | |
| 352 | if (!this->swap_block_mark) |
| 353 | return 0; |
| 354 | |
| 355 | /* |
| 356 | * We need to compute the byte and bit offsets of |
| 357 | * the physical block mark within the ECC-based view of the page. |
| 358 | * |
| 359 | * NAND chip with 2K page shows below: |
| 360 | * (Block Mark) |
| 361 | * | | |
| 362 | * | D | |
| 363 | * |<---->| |
| 364 | * V V |
| 365 | * +---+----------+-+----------+-+----------+-+----------+-+ |
| 366 | * | M | data |E| data |E| data |E| data |E| |
| 367 | * +---+----------+-+----------+-+----------+-+----------+-+ |
| 368 | * |
| 369 | * The position of block mark moves forward in the ECC-based view |
| 370 | * of page, and the delta is: |
| 371 | * |
| 372 | * E * G * (N - 1) |
| 373 | * D = (---------------- + M) |
| 374 | * 8 |
| 375 | * |
| 376 | * With the formula to compute the ECC strength, and the condition |
| 377 | * : C >= O (C is the ecc chunk size) |
| 378 | * |
| 379 | * It's easy to deduce to the following result: |
| 380 | * |
| 381 | * E * G (O - M) C - M C - M |
| 382 | * ----------- <= ------- <= -------- < --------- |
| 383 | * 8 N N (N - 1) |
| 384 | * |
| 385 | * So, we get: |
| 386 | * |
| 387 | * E * G * (N - 1) |
| 388 | * D = (---------------- + M) < C |
| 389 | * 8 |
| 390 | * |
| 391 | * The above inequality means the position of block mark |
| 392 | * within the ECC-based view of the page is still in the data chunk, |
| 393 | * and it's NOT in the ECC bits of the chunk. |
| 394 | * |
| 395 | * Use the following to compute the bit position of the |
| 396 | * physical block mark within the ECC-based view of the page: |
| 397 | * (page_size - D) * 8 |
| 398 | * |
| 399 | * --Huang Shijie |
| 400 | */ |
| 401 | block_mark_bit_offset = mtd->writesize * 8 - |
| 402 | (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1) |
| 403 | + geo->metadata_size * 8); |
| 404 | |
| 405 | geo->block_mark_byte_offset = block_mark_bit_offset / 8; |
| 406 | geo->block_mark_bit_offset = block_mark_bit_offset % 8; |
| 407 | return 0; |
| 408 | } |
| 409 | |
Huang Shijie | 2febcdf | 2013-05-17 11:17:34 +0800 | [diff] [blame] | 410 | int common_nfc_set_geometry(struct gpmi_nand_data *this) |
| 411 | { |
Stefan Agner | 6bf6ec5 | 2018-03-04 21:06:01 +0100 | [diff] [blame] | 412 | struct nand_chip *chip = &this->nand; |
| 413 | |
| 414 | if (chip->ecc.strength > 0 && chip->ecc.size > 0) |
| 415 | return set_geometry_by_ecc_info(this, chip->ecc.strength, |
| 416 | chip->ecc.size); |
| 417 | |
Han Xu | b8b0e46 | 2015-12-02 16:47:43 -0600 | [diff] [blame] | 418 | if ((of_property_read_bool(this->dev->of_node, "fsl,use-minimum-ecc")) |
Stefan Agner | 6bf6ec5 | 2018-03-04 21:06:01 +0100 | [diff] [blame] | 419 | || legacy_set_geometry(this)) { |
| 420 | if (!(chip->ecc_strength_ds > 0 && chip->ecc_step_ds > 0)) |
| 421 | return -EINVAL; |
| 422 | |
| 423 | return set_geometry_by_ecc_info(this, chip->ecc_strength_ds, |
| 424 | chip->ecc_step_ds); |
| 425 | } |
Han Xu | b8b0e46 | 2015-12-02 16:47:43 -0600 | [diff] [blame] | 426 | |
| 427 | return 0; |
Huang Shijie | 2febcdf | 2013-05-17 11:17:34 +0800 | [diff] [blame] | 428 | } |
| 429 | |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 430 | struct dma_chan *get_dma_chan(struct gpmi_nand_data *this) |
| 431 | { |
Huang Shijie | a7c12d0 | 2013-08-27 17:29:05 +0800 | [diff] [blame] | 432 | /* We use the DMA channel 0 to access all the nand chips. */ |
| 433 | return this->dma_chans[0]; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | /* Can we use the upper's buffer directly for DMA? */ |
Sascha Hauer | 111bfed | 2018-04-26 17:41:25 +0200 | [diff] [blame] | 437 | bool prepare_data_dma(struct gpmi_nand_data *this, const void *buf, int len, |
Sascha Hauer | ba3900e | 2018-04-26 17:41:23 +0200 | [diff] [blame] | 438 | enum dma_data_direction dr) |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 439 | { |
| 440 | struct scatterlist *sgl = &this->data_sgl; |
| 441 | int ret; |
| 442 | |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 443 | /* first try to map the upper buffer directly */ |
Sascha Hauer | ba3900e | 2018-04-26 17:41:23 +0200 | [diff] [blame] | 444 | if (virt_addr_valid(buf) && !object_is_on_stack(buf)) { |
| 445 | sg_init_one(sgl, buf, len); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 446 | ret = dma_map_sg(this->dev, sgl, 1, dr); |
| 447 | if (ret == 0) |
Huang Shijie | 0ff76a9 | 2013-12-18 23:41:00 +0800 | [diff] [blame] | 448 | goto map_fail; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 449 | |
Sascha Hauer | 111bfed | 2018-04-26 17:41:25 +0200 | [diff] [blame] | 450 | return true; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 451 | } |
Huang Shijie | 0ff76a9 | 2013-12-18 23:41:00 +0800 | [diff] [blame] | 452 | |
| 453 | map_fail: |
| 454 | /* We have to use our own DMA buffer. */ |
Sascha Hauer | ba3900e | 2018-04-26 17:41:23 +0200 | [diff] [blame] | 455 | sg_init_one(sgl, this->data_buffer_dma, len); |
Huang Shijie | 0ff76a9 | 2013-12-18 23:41:00 +0800 | [diff] [blame] | 456 | |
| 457 | if (dr == DMA_TO_DEVICE) |
Sascha Hauer | ba3900e | 2018-04-26 17:41:23 +0200 | [diff] [blame] | 458 | memcpy(this->data_buffer_dma, buf, len); |
Huang Shijie | 0ff76a9 | 2013-12-18 23:41:00 +0800 | [diff] [blame] | 459 | |
| 460 | dma_map_sg(this->dev, sgl, 1, dr); |
| 461 | |
Sascha Hauer | 111bfed | 2018-04-26 17:41:25 +0200 | [diff] [blame] | 462 | return false; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | /* This will be called after the DMA operation is finished. */ |
| 466 | static void dma_irq_callback(void *param) |
| 467 | { |
| 468 | struct gpmi_nand_data *this = param; |
| 469 | struct completion *dma_c = &this->dma_done; |
| 470 | |
Huang Shijie | 7b3d2fb | 2013-11-11 12:13:45 +0800 | [diff] [blame] | 471 | complete(dma_c); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | int start_dma_without_bch_irq(struct gpmi_nand_data *this, |
| 475 | struct dma_async_tx_descriptor *desc) |
| 476 | { |
| 477 | struct completion *dma_c = &this->dma_done; |
Nicholas Mc Guire | 706d5b2 | 2015-02-08 11:37:33 -0500 | [diff] [blame] | 478 | unsigned long timeout; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 479 | |
| 480 | init_completion(dma_c); |
| 481 | |
| 482 | desc->callback = dma_irq_callback; |
| 483 | desc->callback_param = this; |
| 484 | dmaengine_submit(desc); |
Shawn Guo | d04525e | 2012-04-11 13:29:31 +0800 | [diff] [blame] | 485 | dma_async_issue_pending(get_dma_chan(this)); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 486 | |
| 487 | /* Wait for the interrupt from the DMA block. */ |
Nicholas Mc Guire | 706d5b2 | 2015-02-08 11:37:33 -0500 | [diff] [blame] | 488 | timeout = wait_for_completion_timeout(dma_c, msecs_to_jiffies(1000)); |
| 489 | if (!timeout) { |
Sascha Hauer | c3ee3f3 | 2018-04-26 17:41:22 +0200 | [diff] [blame] | 490 | dev_err(this->dev, "DMA timeout, last DMA\n"); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 491 | gpmi_dump_info(this); |
| 492 | return -ETIMEDOUT; |
| 493 | } |
| 494 | return 0; |
| 495 | } |
| 496 | |
| 497 | /* |
| 498 | * This function is used in BCH reading or BCH writing pages. |
| 499 | * It will wait for the BCH interrupt as long as ONE second. |
| 500 | * Actually, we must wait for two interrupts : |
| 501 | * [1] firstly the DMA interrupt and |
| 502 | * [2] secondly the BCH interrupt. |
| 503 | */ |
| 504 | int start_dma_with_bch_irq(struct gpmi_nand_data *this, |
| 505 | struct dma_async_tx_descriptor *desc) |
| 506 | { |
| 507 | struct completion *bch_c = &this->bch_done; |
Nicholas Mc Guire | 706d5b2 | 2015-02-08 11:37:33 -0500 | [diff] [blame] | 508 | unsigned long timeout; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 509 | |
| 510 | /* Prepare to receive an interrupt from the BCH block. */ |
| 511 | init_completion(bch_c); |
| 512 | |
| 513 | /* start the DMA */ |
| 514 | start_dma_without_bch_irq(this, desc); |
| 515 | |
| 516 | /* Wait for the interrupt from the BCH block. */ |
Nicholas Mc Guire | 706d5b2 | 2015-02-08 11:37:33 -0500 | [diff] [blame] | 517 | timeout = wait_for_completion_timeout(bch_c, msecs_to_jiffies(1000)); |
| 518 | if (!timeout) { |
Sascha Hauer | c3ee3f3 | 2018-04-26 17:41:22 +0200 | [diff] [blame] | 519 | dev_err(this->dev, "BCH timeout\n"); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 520 | gpmi_dump_info(this); |
| 521 | return -ETIMEDOUT; |
| 522 | } |
| 523 | return 0; |
| 524 | } |
| 525 | |
Greg Kroah-Hartman | d892994 | 2012-12-21 13:19:05 -0800 | [diff] [blame] | 526 | static int acquire_register_block(struct gpmi_nand_data *this, |
| 527 | const char *res_name) |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 528 | { |
| 529 | struct platform_device *pdev = this->pdev; |
| 530 | struct resources *res = &this->resources; |
| 531 | struct resource *r; |
Huang Shijie | 513d57e | 2012-07-17 14:14:02 +0800 | [diff] [blame] | 532 | void __iomem *p; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 533 | |
| 534 | r = platform_get_resource_byname(pdev, IORESOURCE_MEM, res_name); |
Huang Shijie | 87a9d69 | 2013-11-14 14:25:48 +0800 | [diff] [blame] | 535 | p = devm_ioremap_resource(&pdev->dev, r); |
| 536 | if (IS_ERR(p)) |
| 537 | return PTR_ERR(p); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 538 | |
| 539 | if (!strcmp(res_name, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME)) |
| 540 | res->gpmi_regs = p; |
| 541 | else if (!strcmp(res_name, GPMI_NAND_BCH_REGS_ADDR_RES_NAME)) |
| 542 | res->bch_regs = p; |
| 543 | else |
Huang Shijie | da40c16 | 2013-11-20 10:09:43 +0800 | [diff] [blame] | 544 | dev_err(this->dev, "unknown resource name : %s\n", res_name); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 545 | |
| 546 | return 0; |
| 547 | } |
| 548 | |
Greg Kroah-Hartman | d892994 | 2012-12-21 13:19:05 -0800 | [diff] [blame] | 549 | static int acquire_bch_irq(struct gpmi_nand_data *this, irq_handler_t irq_h) |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 550 | { |
| 551 | struct platform_device *pdev = this->pdev; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 552 | const char *res_name = GPMI_NAND_BCH_INTERRUPT_RES_NAME; |
| 553 | struct resource *r; |
| 554 | int err; |
| 555 | |
| 556 | r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, res_name); |
| 557 | if (!r) { |
Huang Shijie | da40c16 | 2013-11-20 10:09:43 +0800 | [diff] [blame] | 558 | dev_err(this->dev, "Can't get resource for %s\n", res_name); |
Lothar Waßmann | 52a073b | 2013-08-07 08:15:38 +0200 | [diff] [blame] | 559 | return -ENODEV; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 560 | } |
| 561 | |
Huang Shijie | 3cb2c1e | 2013-11-14 14:25:49 +0800 | [diff] [blame] | 562 | err = devm_request_irq(this->dev, r->start, irq_h, 0, res_name, this); |
| 563 | if (err) |
| 564 | dev_err(this->dev, "error requesting BCH IRQ\n"); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 565 | |
Huang Shijie | 3cb2c1e | 2013-11-14 14:25:49 +0800 | [diff] [blame] | 566 | return err; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 567 | } |
| 568 | |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 569 | static void release_dma_channels(struct gpmi_nand_data *this) |
| 570 | { |
| 571 | unsigned int i; |
| 572 | for (i = 0; i < DMA_CHANS; i++) |
| 573 | if (this->dma_chans[i]) { |
| 574 | dma_release_channel(this->dma_chans[i]); |
| 575 | this->dma_chans[i] = NULL; |
| 576 | } |
| 577 | } |
| 578 | |
Bill Pemberton | 06f2551 | 2012-11-19 13:23:07 -0500 | [diff] [blame] | 579 | static int acquire_dma_channels(struct gpmi_nand_data *this) |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 580 | { |
| 581 | struct platform_device *pdev = this->pdev; |
Huang Shijie | e10db1f | 2012-05-04 21:42:05 -0400 | [diff] [blame] | 582 | struct dma_chan *dma_chan; |
Huang Shijie | e10db1f | 2012-05-04 21:42:05 -0400 | [diff] [blame] | 583 | |
| 584 | /* request dma channel */ |
Shawn Guo | 5fac0e1 | 2013-02-26 11:44:28 +0800 | [diff] [blame] | 585 | dma_chan = dma_request_slave_channel(&pdev->dev, "rx-tx"); |
Huang Shijie | e10db1f | 2012-05-04 21:42:05 -0400 | [diff] [blame] | 586 | if (!dma_chan) { |
Huang Shijie | da40c16 | 2013-11-20 10:09:43 +0800 | [diff] [blame] | 587 | dev_err(this->dev, "Failed to request DMA channel.\n"); |
Huang Shijie | e10db1f | 2012-05-04 21:42:05 -0400 | [diff] [blame] | 588 | goto acquire_err; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 589 | } |
| 590 | |
Huang Shijie | e10db1f | 2012-05-04 21:42:05 -0400 | [diff] [blame] | 591 | this->dma_chans[0] = dma_chan; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 592 | return 0; |
| 593 | |
| 594 | acquire_err: |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 595 | release_dma_channels(this); |
| 596 | return -EINVAL; |
| 597 | } |
| 598 | |
Bill Pemberton | 06f2551 | 2012-11-19 13:23:07 -0500 | [diff] [blame] | 599 | static int gpmi_get_clks(struct gpmi_nand_data *this) |
Huang Shijie | ff50617 | 2012-07-02 21:39:32 -0400 | [diff] [blame] | 600 | { |
| 601 | struct resources *r = &this->resources; |
Huang Shijie | ff50617 | 2012-07-02 21:39:32 -0400 | [diff] [blame] | 602 | struct clk *clk; |
Michał Mirosław | d1cb556 | 2013-05-04 15:19:35 +0200 | [diff] [blame] | 603 | int err, i; |
Huang Shijie | ff50617 | 2012-07-02 21:39:32 -0400 | [diff] [blame] | 604 | |
Stefan Agner | 6b7ee72 | 2017-04-21 18:23:34 -0700 | [diff] [blame] | 605 | for (i = 0; i < this->devdata->clks_count; i++) { |
| 606 | clk = devm_clk_get(this->dev, this->devdata->clks[i]); |
Michał Mirosław | d1cb556 | 2013-05-04 15:19:35 +0200 | [diff] [blame] | 607 | if (IS_ERR(clk)) { |
| 608 | err = PTR_ERR(clk); |
Huang Shijie | ff50617 | 2012-07-02 21:39:32 -0400 | [diff] [blame] | 609 | goto err_clock; |
Michał Mirosław | d1cb556 | 2013-05-04 15:19:35 +0200 | [diff] [blame] | 610 | } |
Huang Shijie | ff50617 | 2012-07-02 21:39:32 -0400 | [diff] [blame] | 611 | |
| 612 | r->clock[i] = clk; |
| 613 | } |
| 614 | |
Huang Shijie | 91f5498 | 2014-03-27 10:43:22 +0800 | [diff] [blame] | 615 | if (GPMI_IS_MX6(this)) |
Huang Shijie | ff50617 | 2012-07-02 21:39:32 -0400 | [diff] [blame] | 616 | /* |
Huang Shijie | 91f5498 | 2014-03-27 10:43:22 +0800 | [diff] [blame] | 617 | * Set the default value for the gpmi clock. |
Huang Shijie | ff50617 | 2012-07-02 21:39:32 -0400 | [diff] [blame] | 618 | * |
Huang Shijie | e1ca95e | 2012-09-13 14:57:58 +0800 | [diff] [blame] | 619 | * If you want to use the ONFI nand which is in the |
| 620 | * Synchronous Mode, you should change the clock as you need. |
Huang Shijie | ff50617 | 2012-07-02 21:39:32 -0400 | [diff] [blame] | 621 | */ |
| 622 | clk_set_rate(r->clock[0], 22000000); |
Huang Shijie | e1ca95e | 2012-09-13 14:57:58 +0800 | [diff] [blame] | 623 | |
Huang Shijie | ff50617 | 2012-07-02 21:39:32 -0400 | [diff] [blame] | 624 | return 0; |
| 625 | |
| 626 | err_clock: |
| 627 | dev_dbg(this->dev, "failed in finding the clocks.\n"); |
Michał Mirosław | d1cb556 | 2013-05-04 15:19:35 +0200 | [diff] [blame] | 628 | return err; |
Huang Shijie | ff50617 | 2012-07-02 21:39:32 -0400 | [diff] [blame] | 629 | } |
| 630 | |
Bill Pemberton | 06f2551 | 2012-11-19 13:23:07 -0500 | [diff] [blame] | 631 | static int acquire_resources(struct gpmi_nand_data *this) |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 632 | { |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 633 | int ret; |
| 634 | |
| 635 | ret = acquire_register_block(this, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME); |
| 636 | if (ret) |
| 637 | goto exit_regs; |
| 638 | |
| 639 | ret = acquire_register_block(this, GPMI_NAND_BCH_REGS_ADDR_RES_NAME); |
| 640 | if (ret) |
| 641 | goto exit_regs; |
| 642 | |
| 643 | ret = acquire_bch_irq(this, bch_irq); |
| 644 | if (ret) |
| 645 | goto exit_regs; |
| 646 | |
| 647 | ret = acquire_dma_channels(this); |
| 648 | if (ret) |
Huang Shijie | 3cb2c1e | 2013-11-14 14:25:49 +0800 | [diff] [blame] | 649 | goto exit_regs; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 650 | |
Huang Shijie | ff50617 | 2012-07-02 21:39:32 -0400 | [diff] [blame] | 651 | ret = gpmi_get_clks(this); |
| 652 | if (ret) |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 653 | goto exit_clock; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 654 | return 0; |
| 655 | |
| 656 | exit_clock: |
| 657 | release_dma_channels(this); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 658 | exit_regs: |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 659 | return ret; |
| 660 | } |
| 661 | |
| 662 | static void release_resources(struct gpmi_nand_data *this) |
| 663 | { |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 664 | release_dma_channels(this); |
| 665 | } |
| 666 | |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 667 | static int send_page_prepare(struct gpmi_nand_data *this, |
| 668 | const void *source, unsigned length, |
| 669 | void *alt_virt, dma_addr_t alt_phys, unsigned alt_size, |
| 670 | const void **use_virt, dma_addr_t *use_phys) |
| 671 | { |
| 672 | struct device *dev = this->dev; |
| 673 | |
| 674 | if (virt_addr_valid(source)) { |
| 675 | dma_addr_t source_phys; |
| 676 | |
| 677 | source_phys = dma_map_single(dev, (void *)source, length, |
| 678 | DMA_TO_DEVICE); |
| 679 | if (dma_mapping_error(dev, source_phys)) { |
| 680 | if (alt_size < length) { |
Huang Shijie | da40c16 | 2013-11-20 10:09:43 +0800 | [diff] [blame] | 681 | dev_err(dev, "Alternate buffer is too small\n"); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 682 | return -ENOMEM; |
| 683 | } |
| 684 | goto map_failed; |
| 685 | } |
| 686 | *use_virt = source; |
| 687 | *use_phys = source_phys; |
| 688 | return 0; |
| 689 | } |
| 690 | map_failed: |
| 691 | /* |
| 692 | * Copy the content of the source buffer into the alternate |
| 693 | * buffer and set up the return values accordingly. |
| 694 | */ |
| 695 | memcpy(alt_virt, source, length); |
| 696 | |
| 697 | *use_virt = alt_virt; |
| 698 | *use_phys = alt_phys; |
| 699 | return 0; |
| 700 | } |
| 701 | |
| 702 | static void send_page_end(struct gpmi_nand_data *this, |
| 703 | const void *source, unsigned length, |
| 704 | void *alt_virt, dma_addr_t alt_phys, unsigned alt_size, |
| 705 | const void *used_virt, dma_addr_t used_phys) |
| 706 | { |
| 707 | struct device *dev = this->dev; |
| 708 | if (used_virt == source) |
| 709 | dma_unmap_single(dev, used_phys, length, DMA_TO_DEVICE); |
| 710 | } |
| 711 | |
| 712 | static void gpmi_free_dma_buffer(struct gpmi_nand_data *this) |
| 713 | { |
| 714 | struct device *dev = this->dev; |
| 715 | |
| 716 | if (this->page_buffer_virt && virt_addr_valid(this->page_buffer_virt)) |
| 717 | dma_free_coherent(dev, this->page_buffer_size, |
| 718 | this->page_buffer_virt, |
| 719 | this->page_buffer_phys); |
| 720 | kfree(this->cmd_buffer); |
| 721 | kfree(this->data_buffer_dma); |
Boris BREZILLON | da3bc42c | 2014-11-30 19:10:29 +0100 | [diff] [blame] | 722 | kfree(this->raw_buffer); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 723 | |
| 724 | this->cmd_buffer = NULL; |
| 725 | this->data_buffer_dma = NULL; |
Han Xu | 2cd395d | 2016-04-04 15:41:29 -0500 | [diff] [blame] | 726 | this->raw_buffer = NULL; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 727 | this->page_buffer_virt = NULL; |
| 728 | this->page_buffer_size = 0; |
| 729 | } |
| 730 | |
| 731 | /* Allocate the DMA buffers */ |
| 732 | static int gpmi_alloc_dma_buffer(struct gpmi_nand_data *this) |
| 733 | { |
| 734 | struct bch_geometry *geo = &this->bch_geometry; |
| 735 | struct device *dev = this->dev; |
Boris BREZILLON | 2a690b2 | 2015-12-10 09:00:07 +0100 | [diff] [blame] | 736 | struct mtd_info *mtd = nand_to_mtd(&this->nand); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 737 | |
| 738 | /* [1] Allocate a command buffer. PAGE_SIZE is enough. */ |
Huang Shijie | 513d57e | 2012-07-17 14:14:02 +0800 | [diff] [blame] | 739 | this->cmd_buffer = kzalloc(PAGE_SIZE, GFP_DMA | GFP_KERNEL); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 740 | if (this->cmd_buffer == NULL) |
| 741 | goto error_alloc; |
| 742 | |
Huang Shijie | 06f216c | 2013-12-18 23:40:59 +0800 | [diff] [blame] | 743 | /* |
| 744 | * [2] Allocate a read/write data buffer. |
| 745 | * The gpmi_alloc_dma_buffer can be called twice. |
| 746 | * We allocate a PAGE_SIZE length buffer if gpmi_alloc_dma_buffer |
Miquel Raynal | 5f8374d | 2018-07-20 17:15:00 +0200 | [diff] [blame] | 747 | * is called before the NAND identification; and we allocate a |
| 748 | * buffer of the real NAND page size when the gpmi_alloc_dma_buffer |
| 749 | * is called after. |
Huang Shijie | 06f216c | 2013-12-18 23:40:59 +0800 | [diff] [blame] | 750 | */ |
| 751 | this->data_buffer_dma = kzalloc(mtd->writesize ?: PAGE_SIZE, |
| 752 | GFP_DMA | GFP_KERNEL); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 753 | if (this->data_buffer_dma == NULL) |
| 754 | goto error_alloc; |
| 755 | |
| 756 | /* |
| 757 | * [3] Allocate the page buffer. |
| 758 | * |
| 759 | * Both the payload buffer and the auxiliary buffer must appear on |
| 760 | * 32-bit boundaries. We presume the size of the payload buffer is a |
| 761 | * power of two and is much larger than four, which guarantees the |
| 762 | * auxiliary buffer will appear on a 32-bit boundary. |
| 763 | */ |
| 764 | this->page_buffer_size = geo->payload_size + geo->auxiliary_size; |
| 765 | this->page_buffer_virt = dma_alloc_coherent(dev, this->page_buffer_size, |
| 766 | &this->page_buffer_phys, GFP_DMA); |
| 767 | if (!this->page_buffer_virt) |
| 768 | goto error_alloc; |
| 769 | |
Boris BREZILLON | da3bc42c | 2014-11-30 19:10:29 +0100 | [diff] [blame] | 770 | this->raw_buffer = kzalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL); |
| 771 | if (!this->raw_buffer) |
| 772 | goto error_alloc; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 773 | |
| 774 | /* Slice up the page buffer. */ |
| 775 | this->payload_virt = this->page_buffer_virt; |
| 776 | this->payload_phys = this->page_buffer_phys; |
| 777 | this->auxiliary_virt = this->payload_virt + geo->payload_size; |
| 778 | this->auxiliary_phys = this->payload_phys + geo->payload_size; |
| 779 | return 0; |
| 780 | |
| 781 | error_alloc: |
| 782 | gpmi_free_dma_buffer(this); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 783 | return -ENOMEM; |
| 784 | } |
| 785 | |
| 786 | static void gpmi_cmd_ctrl(struct mtd_info *mtd, int data, unsigned int ctrl) |
| 787 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 788 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 789 | struct gpmi_nand_data *this = nand_get_controller_data(chip); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 790 | int ret; |
| 791 | |
| 792 | /* |
| 793 | * Every operation begins with a command byte and a series of zero or |
| 794 | * more address bytes. These are distinguished by either the Address |
| 795 | * Latch Enable (ALE) or Command Latch Enable (CLE) signals being |
| 796 | * asserted. When MTD is ready to execute the command, it will deassert |
| 797 | * both latch enables. |
| 798 | * |
| 799 | * Rather than run a separate DMA operation for every single byte, we |
| 800 | * queue them up and run a single DMA operation for the entire series |
| 801 | * of command and data bytes. NAND_CMD_NONE means the END of the queue. |
| 802 | */ |
| 803 | if ((ctrl & (NAND_ALE | NAND_CLE))) { |
| 804 | if (data != NAND_CMD_NONE) |
| 805 | this->cmd_buffer[this->command_length++] = data; |
| 806 | return; |
| 807 | } |
| 808 | |
| 809 | if (!this->command_length) |
| 810 | return; |
| 811 | |
| 812 | ret = gpmi_send_command(this); |
| 813 | if (ret) |
Huang Shijie | da40c16 | 2013-11-20 10:09:43 +0800 | [diff] [blame] | 814 | dev_err(this->dev, "Chip: %u, Error %d\n", |
| 815 | this->current_chip, ret); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 816 | |
| 817 | this->command_length = 0; |
| 818 | } |
| 819 | |
| 820 | static int gpmi_dev_ready(struct mtd_info *mtd) |
| 821 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 822 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 823 | struct gpmi_nand_data *this = nand_get_controller_data(chip); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 824 | |
| 825 | return gpmi_is_ready(this, this->current_chip); |
| 826 | } |
| 827 | |
| 828 | static void gpmi_select_chip(struct mtd_info *mtd, int chipnr) |
| 829 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 830 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 831 | struct gpmi_nand_data *this = nand_get_controller_data(chip); |
Miquel Raynal | 76e1a00 | 2018-03-02 15:38:39 +0100 | [diff] [blame] | 832 | int ret; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 833 | |
Miquel Raynal | 76e1a00 | 2018-03-02 15:38:39 +0100 | [diff] [blame] | 834 | /* |
| 835 | * For power consumption matters, disable/enable the clock each time a |
| 836 | * die is selected/unselected. |
| 837 | */ |
| 838 | if (this->current_chip < 0 && chipnr >= 0) { |
| 839 | ret = gpmi_enable_clk(this); |
| 840 | if (ret) |
| 841 | dev_err(this->dev, "Failed to enable the clock\n"); |
| 842 | } else if (this->current_chip >= 0 && chipnr < 0) { |
| 843 | ret = gpmi_disable_clk(this); |
| 844 | if (ret) |
| 845 | dev_err(this->dev, "Failed to disable the clock\n"); |
| 846 | } |
| 847 | |
| 848 | /* |
| 849 | * This driver currently supports only one NAND chip. Plus, dies share |
| 850 | * the same configuration. So once timings have been applied on the |
| 851 | * controller side, they will not change anymore. When the time will |
| 852 | * come, the check on must_apply_timings will have to be dropped. |
| 853 | */ |
| 854 | if (chipnr >= 0 && this->hw.must_apply_timings) { |
| 855 | this->hw.must_apply_timings = false; |
| 856 | gpmi_nfc_apply_timings(this); |
| 857 | } |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 858 | |
| 859 | this->current_chip = chipnr; |
| 860 | } |
| 861 | |
| 862 | static void gpmi_read_buf(struct mtd_info *mtd, uint8_t *buf, int len) |
| 863 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 864 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 865 | struct gpmi_nand_data *this = nand_get_controller_data(chip); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 866 | |
Huang Shijie | c232596 | 2013-11-20 10:09:44 +0800 | [diff] [blame] | 867 | dev_dbg(this->dev, "len is %d\n", len); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 868 | |
Sascha Hauer | ba3900e | 2018-04-26 17:41:23 +0200 | [diff] [blame] | 869 | gpmi_read_data(this, buf, len); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | static void gpmi_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len) |
| 873 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 874 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 875 | struct gpmi_nand_data *this = nand_get_controller_data(chip); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 876 | |
Huang Shijie | c232596 | 2013-11-20 10:09:44 +0800 | [diff] [blame] | 877 | dev_dbg(this->dev, "len is %d\n", len); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 878 | |
Sascha Hauer | ba3900e | 2018-04-26 17:41:23 +0200 | [diff] [blame] | 879 | gpmi_send_data(this, buf, len); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | static uint8_t gpmi_read_byte(struct mtd_info *mtd) |
| 883 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 884 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 885 | struct gpmi_nand_data *this = nand_get_controller_data(chip); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 886 | uint8_t *buf = this->data_buffer_dma; |
| 887 | |
| 888 | gpmi_read_buf(mtd, buf, 1); |
| 889 | return buf[0]; |
| 890 | } |
| 891 | |
| 892 | /* |
| 893 | * Handles block mark swapping. |
| 894 | * It can be called in swapping the block mark, or swapping it back, |
| 895 | * because the the operations are the same. |
| 896 | */ |
| 897 | static void block_mark_swapping(struct gpmi_nand_data *this, |
| 898 | void *payload, void *auxiliary) |
| 899 | { |
| 900 | struct bch_geometry *nfc_geo = &this->bch_geometry; |
| 901 | unsigned char *p; |
| 902 | unsigned char *a; |
| 903 | unsigned int bit; |
| 904 | unsigned char mask; |
| 905 | unsigned char from_data; |
| 906 | unsigned char from_oob; |
| 907 | |
| 908 | if (!this->swap_block_mark) |
| 909 | return; |
| 910 | |
| 911 | /* |
| 912 | * If control arrives here, we're swapping. Make some convenience |
| 913 | * variables. |
| 914 | */ |
| 915 | bit = nfc_geo->block_mark_bit_offset; |
| 916 | p = payload + nfc_geo->block_mark_byte_offset; |
| 917 | a = auxiliary; |
| 918 | |
| 919 | /* |
| 920 | * Get the byte from the data area that overlays the block mark. Since |
| 921 | * the ECC engine applies its own view to the bits in the page, the |
| 922 | * physical block mark won't (in general) appear on a byte boundary in |
| 923 | * the data. |
| 924 | */ |
| 925 | from_data = (p[0] >> bit) | (p[1] << (8 - bit)); |
| 926 | |
| 927 | /* Get the byte from the OOB. */ |
| 928 | from_oob = a[0]; |
| 929 | |
| 930 | /* Swap them. */ |
| 931 | a[0] = from_data; |
| 932 | |
| 933 | mask = (0x1 << bit) - 1; |
| 934 | p[0] = (p[0] & mask) | (from_oob << bit); |
| 935 | |
| 936 | mask = ~0 << bit; |
| 937 | p[1] = (p[1] & mask) | (from_oob >> (8 - bit)); |
| 938 | } |
| 939 | |
Boris Brezillon | 4c7e95b | 2018-01-23 11:13:17 +0100 | [diff] [blame] | 940 | static int gpmi_ecc_read_page_data(struct nand_chip *chip, |
| 941 | uint8_t *buf, int oob_required, |
| 942 | int page) |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 943 | { |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 944 | struct gpmi_nand_data *this = nand_get_controller_data(chip); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 945 | struct bch_geometry *nfc_geo = &this->bch_geometry; |
Boris Brezillon | 4c7e95b | 2018-01-23 11:13:17 +0100 | [diff] [blame] | 946 | struct mtd_info *mtd = nand_to_mtd(chip); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 947 | dma_addr_t payload_phys; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 948 | unsigned int i; |
| 949 | unsigned char *status; |
Zach Sadecki | b23b746 | 2012-12-13 20:36:29 -0600 | [diff] [blame] | 950 | unsigned int max_bitflips = 0; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 951 | int ret; |
Sascha Hauer | 111bfed | 2018-04-26 17:41:25 +0200 | [diff] [blame] | 952 | bool direct = false; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 953 | |
Huang Shijie | c232596 | 2013-11-20 10:09:44 +0800 | [diff] [blame] | 954 | dev_dbg(this->dev, "page number is : %d\n", page); |
Sascha Hauer | 118c3f9 | 2018-04-26 17:41:24 +0200 | [diff] [blame] | 955 | |
Sascha Hauer | 118c3f9 | 2018-04-26 17:41:24 +0200 | [diff] [blame] | 956 | payload_phys = this->payload_phys; |
Sascha Hauer | 118c3f9 | 2018-04-26 17:41:24 +0200 | [diff] [blame] | 957 | |
| 958 | if (virt_addr_valid(buf)) { |
| 959 | dma_addr_t dest_phys; |
| 960 | |
| 961 | dest_phys = dma_map_single(this->dev, buf, nfc_geo->payload_size, |
| 962 | DMA_FROM_DEVICE); |
| 963 | if (!dma_mapping_error(this->dev, dest_phys)) { |
Sascha Hauer | 118c3f9 | 2018-04-26 17:41:24 +0200 | [diff] [blame] | 964 | payload_phys = dest_phys; |
Sascha Hauer | 111bfed | 2018-04-26 17:41:25 +0200 | [diff] [blame] | 965 | direct = true; |
Sascha Hauer | 118c3f9 | 2018-04-26 17:41:24 +0200 | [diff] [blame] | 966 | } |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 967 | } |
Sascha Hauer | 118c3f9 | 2018-04-26 17:41:24 +0200 | [diff] [blame] | 968 | |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 969 | /* go! */ |
Sascha Hauer | f6b74db | 2018-04-26 17:41:27 +0200 | [diff] [blame] | 970 | ret = gpmi_read_page(this, payload_phys, this->auxiliary_phys); |
Sascha Hauer | 118c3f9 | 2018-04-26 17:41:24 +0200 | [diff] [blame] | 971 | |
Sascha Hauer | 111bfed | 2018-04-26 17:41:25 +0200 | [diff] [blame] | 972 | if (direct) |
Sascha Hauer | 118c3f9 | 2018-04-26 17:41:24 +0200 | [diff] [blame] | 973 | dma_unmap_single(this->dev, payload_phys, nfc_geo->payload_size, |
| 974 | DMA_FROM_DEVICE); |
| 975 | |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 976 | if (ret) { |
Huang Shijie | da40c16 | 2013-11-20 10:09:43 +0800 | [diff] [blame] | 977 | dev_err(this->dev, "Error in ECC-based read: %d\n", ret); |
Zach Sadecki | b23b746 | 2012-12-13 20:36:29 -0600 | [diff] [blame] | 978 | return ret; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 979 | } |
| 980 | |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 981 | /* Loop over status bytes, accumulating ECC status. */ |
Sascha Hauer | f6b74db | 2018-04-26 17:41:27 +0200 | [diff] [blame] | 982 | status = this->auxiliary_virt + nfc_geo->auxiliary_status_offset; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 983 | |
Sascha Hauer | 111bfed | 2018-04-26 17:41:25 +0200 | [diff] [blame] | 984 | if (!direct) |
Sascha Hauer | 118c3f9 | 2018-04-26 17:41:24 +0200 | [diff] [blame] | 985 | memcpy(buf, this->payload_virt, nfc_geo->payload_size); |
Markus Pargmann | bd2e778 | 2016-04-25 14:35:12 +0200 | [diff] [blame] | 986 | |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 987 | for (i = 0; i < nfc_geo->ecc_chunk_count; i++, status++) { |
| 988 | if ((*status == STATUS_GOOD) || (*status == STATUS_ERASED)) |
| 989 | continue; |
| 990 | |
| 991 | if (*status == STATUS_UNCORRECTABLE) { |
Markus Pargmann | bd2e778 | 2016-04-25 14:35:12 +0200 | [diff] [blame] | 992 | int eccbits = nfc_geo->ecc_strength * nfc_geo->gf_len; |
| 993 | u8 *eccbuf = this->raw_buffer; |
| 994 | int offset, bitoffset; |
| 995 | int eccbytes; |
| 996 | int flips; |
| 997 | |
| 998 | /* Read ECC bytes into our internal raw_buffer */ |
| 999 | offset = nfc_geo->metadata_size * 8; |
| 1000 | offset += ((8 * nfc_geo->ecc_chunk_size) + eccbits) * (i + 1); |
| 1001 | offset -= eccbits; |
| 1002 | bitoffset = offset % 8; |
| 1003 | eccbytes = DIV_ROUND_UP(offset + eccbits, 8); |
| 1004 | offset /= 8; |
| 1005 | eccbytes -= offset; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1006 | nand_change_read_column_op(chip, offset, eccbuf, |
| 1007 | eccbytes, false); |
Markus Pargmann | bd2e778 | 2016-04-25 14:35:12 +0200 | [diff] [blame] | 1008 | |
| 1009 | /* |
| 1010 | * ECC data are not byte aligned and we may have |
| 1011 | * in-band data in the first and last byte of |
| 1012 | * eccbuf. Set non-eccbits to one so that |
| 1013 | * nand_check_erased_ecc_chunk() does not count them |
| 1014 | * as bitflips. |
| 1015 | */ |
| 1016 | if (bitoffset) |
| 1017 | eccbuf[0] |= GENMASK(bitoffset - 1, 0); |
| 1018 | |
| 1019 | bitoffset = (bitoffset + eccbits) % 8; |
| 1020 | if (bitoffset) |
| 1021 | eccbuf[eccbytes - 1] |= GENMASK(7, bitoffset); |
| 1022 | |
| 1023 | /* |
| 1024 | * The ECC hardware has an uncorrectable ECC status |
| 1025 | * code in case we have bitflips in an erased page. As |
| 1026 | * nothing was written into this subpage the ECC is |
| 1027 | * obviously wrong and we can not trust it. We assume |
| 1028 | * at this point that we are reading an erased page and |
| 1029 | * try to correct the bitflips in buffer up to |
| 1030 | * ecc_strength bitflips. If this is a page with random |
| 1031 | * data, we exceed this number of bitflips and have a |
| 1032 | * ECC failure. Otherwise we use the corrected buffer. |
| 1033 | */ |
| 1034 | if (i == 0) { |
| 1035 | /* The first block includes metadata */ |
| 1036 | flips = nand_check_erased_ecc_chunk( |
| 1037 | buf + i * nfc_geo->ecc_chunk_size, |
| 1038 | nfc_geo->ecc_chunk_size, |
| 1039 | eccbuf, eccbytes, |
Sascha Hauer | f6b74db | 2018-04-26 17:41:27 +0200 | [diff] [blame] | 1040 | this->auxiliary_virt, |
Markus Pargmann | bd2e778 | 2016-04-25 14:35:12 +0200 | [diff] [blame] | 1041 | nfc_geo->metadata_size, |
| 1042 | nfc_geo->ecc_strength); |
| 1043 | } else { |
| 1044 | flips = nand_check_erased_ecc_chunk( |
| 1045 | buf + i * nfc_geo->ecc_chunk_size, |
| 1046 | nfc_geo->ecc_chunk_size, |
| 1047 | eccbuf, eccbytes, |
| 1048 | NULL, 0, |
| 1049 | nfc_geo->ecc_strength); |
| 1050 | } |
| 1051 | |
| 1052 | if (flips > 0) { |
| 1053 | max_bitflips = max_t(unsigned int, max_bitflips, |
| 1054 | flips); |
| 1055 | mtd->ecc_stats.corrected += flips; |
| 1056 | continue; |
| 1057 | } |
| 1058 | |
Zach Sadecki | b23b746 | 2012-12-13 20:36:29 -0600 | [diff] [blame] | 1059 | mtd->ecc_stats.failed++; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1060 | continue; |
| 1061 | } |
Markus Pargmann | bd2e778 | 2016-04-25 14:35:12 +0200 | [diff] [blame] | 1062 | |
Zach Sadecki | b23b746 | 2012-12-13 20:36:29 -0600 | [diff] [blame] | 1063 | mtd->ecc_stats.corrected += *status; |
| 1064 | max_bitflips = max_t(unsigned int, max_bitflips, *status); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1065 | } |
| 1066 | |
Sascha Hauer | fdf2e82 | 2017-12-05 11:51:40 +0100 | [diff] [blame] | 1067 | /* handle the block mark swapping */ |
Sascha Hauer | f6b74db | 2018-04-26 17:41:27 +0200 | [diff] [blame] | 1068 | block_mark_swapping(this, buf, this->auxiliary_virt); |
Sascha Hauer | fdf2e82 | 2017-12-05 11:51:40 +0100 | [diff] [blame] | 1069 | |
Brian Norris | 7725cc8 | 2012-05-02 10:15:02 -0700 | [diff] [blame] | 1070 | if (oob_required) { |
| 1071 | /* |
| 1072 | * It's time to deliver the OOB bytes. See gpmi_ecc_read_oob() |
| 1073 | * for details about our policy for delivering the OOB. |
| 1074 | * |
| 1075 | * We fill the caller's buffer with set bits, and then copy the |
| 1076 | * block mark to th caller's buffer. Note that, if block mark |
| 1077 | * swapping was necessary, it has already been done, so we can |
| 1078 | * rely on the first byte of the auxiliary buffer to contain |
| 1079 | * the block mark. |
| 1080 | */ |
| 1081 | memset(chip->oob_poi, ~0, mtd->oobsize); |
Sascha Hauer | f6b74db | 2018-04-26 17:41:27 +0200 | [diff] [blame] | 1082 | chip->oob_poi[0] = ((uint8_t *)this->auxiliary_virt)[0]; |
Brian Norris | 7725cc8 | 2012-05-02 10:15:02 -0700 | [diff] [blame] | 1083 | } |
Sascha Hauer | 6023813 | 2012-06-26 17:26:16 +0200 | [diff] [blame] | 1084 | |
Zach Sadecki | b23b746 | 2012-12-13 20:36:29 -0600 | [diff] [blame] | 1085 | return max_bitflips; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1086 | } |
| 1087 | |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 1088 | static int gpmi_ecc_read_page(struct nand_chip *chip, uint8_t *buf, |
| 1089 | int oob_required, int page) |
Boris Brezillon | 4c7e95b | 2018-01-23 11:13:17 +0100 | [diff] [blame] | 1090 | { |
| 1091 | nand_read_page_op(chip, page, 0, NULL, 0); |
| 1092 | |
| 1093 | return gpmi_ecc_read_page_data(chip, buf, oob_required, page); |
| 1094 | } |
| 1095 | |
Huang Shijie | b8e2931 | 2014-01-03 11:01:42 +0800 | [diff] [blame] | 1096 | /* Fake a virtual small page for the subpage read */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 1097 | static int gpmi_ecc_read_subpage(struct nand_chip *chip, uint32_t offs, |
| 1098 | uint32_t len, uint8_t *buf, int page) |
Huang Shijie | b8e2931 | 2014-01-03 11:01:42 +0800 | [diff] [blame] | 1099 | { |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 1100 | struct gpmi_nand_data *this = nand_get_controller_data(chip); |
Huang Shijie | b8e2931 | 2014-01-03 11:01:42 +0800 | [diff] [blame] | 1101 | void __iomem *bch_regs = this->resources.bch_regs; |
| 1102 | struct bch_geometry old_geo = this->bch_geometry; |
| 1103 | struct bch_geometry *geo = &this->bch_geometry; |
| 1104 | int size = chip->ecc.size; /* ECC chunk size */ |
| 1105 | int meta, n, page_size; |
| 1106 | u32 r1_old, r2_old, r1_new, r2_new; |
| 1107 | unsigned int max_bitflips; |
| 1108 | int first, last, marker_pos; |
| 1109 | int ecc_parity_size; |
| 1110 | int col = 0; |
Lothar Waßmann | 2a500af | 2014-03-28 11:35:06 +0100 | [diff] [blame] | 1111 | int old_swap_block_mark = this->swap_block_mark; |
Huang Shijie | b8e2931 | 2014-01-03 11:01:42 +0800 | [diff] [blame] | 1112 | |
| 1113 | /* The size of ECC parity */ |
| 1114 | ecc_parity_size = geo->gf_len * geo->ecc_strength / 8; |
| 1115 | |
| 1116 | /* Align it with the chunk size */ |
| 1117 | first = offs / size; |
| 1118 | last = (offs + len - 1) / size; |
| 1119 | |
Lothar Waßmann | 2a500af | 2014-03-28 11:35:06 +0100 | [diff] [blame] | 1120 | if (this->swap_block_mark) { |
| 1121 | /* |
| 1122 | * Find the chunk which contains the Block Marker. |
| 1123 | * If this chunk is in the range of [first, last], |
| 1124 | * we have to read out the whole page. |
| 1125 | * Why? since we had swapped the data at the position of Block |
| 1126 | * Marker to the metadata which is bound with the chunk 0. |
| 1127 | */ |
| 1128 | marker_pos = geo->block_mark_byte_offset / size; |
| 1129 | if (last >= marker_pos && first <= marker_pos) { |
| 1130 | dev_dbg(this->dev, |
| 1131 | "page:%d, first:%d, last:%d, marker at:%d\n", |
Huang Shijie | b8e2931 | 2014-01-03 11:01:42 +0800 | [diff] [blame] | 1132 | page, first, last, marker_pos); |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 1133 | return gpmi_ecc_read_page(chip, buf, 0, page); |
Lothar Waßmann | 2a500af | 2014-03-28 11:35:06 +0100 | [diff] [blame] | 1134 | } |
Huang Shijie | b8e2931 | 2014-01-03 11:01:42 +0800 | [diff] [blame] | 1135 | } |
| 1136 | |
| 1137 | meta = geo->metadata_size; |
| 1138 | if (first) { |
| 1139 | col = meta + (size + ecc_parity_size) * first; |
Huang Shijie | b8e2931 | 2014-01-03 11:01:42 +0800 | [diff] [blame] | 1140 | meta = 0; |
| 1141 | buf = buf + first * size; |
| 1142 | } |
| 1143 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 1144 | nand_read_page_op(chip, page, col, NULL, 0); |
| 1145 | |
Huang Shijie | b8e2931 | 2014-01-03 11:01:42 +0800 | [diff] [blame] | 1146 | /* Save the old environment */ |
| 1147 | r1_old = r1_new = readl(bch_regs + HW_BCH_FLASH0LAYOUT0); |
| 1148 | r2_old = r2_new = readl(bch_regs + HW_BCH_FLASH0LAYOUT1); |
| 1149 | |
| 1150 | /* change the BCH registers and bch_geometry{} */ |
| 1151 | n = last - first + 1; |
| 1152 | page_size = meta + (size + ecc_parity_size) * n; |
| 1153 | |
| 1154 | r1_new &= ~(BM_BCH_FLASH0LAYOUT0_NBLOCKS | |
| 1155 | BM_BCH_FLASH0LAYOUT0_META_SIZE); |
| 1156 | r1_new |= BF_BCH_FLASH0LAYOUT0_NBLOCKS(n - 1) |
| 1157 | | BF_BCH_FLASH0LAYOUT0_META_SIZE(meta); |
| 1158 | writel(r1_new, bch_regs + HW_BCH_FLASH0LAYOUT0); |
| 1159 | |
| 1160 | r2_new &= ~BM_BCH_FLASH0LAYOUT1_PAGE_SIZE; |
| 1161 | r2_new |= BF_BCH_FLASH0LAYOUT1_PAGE_SIZE(page_size); |
| 1162 | writel(r2_new, bch_regs + HW_BCH_FLASH0LAYOUT1); |
| 1163 | |
| 1164 | geo->ecc_chunk_count = n; |
| 1165 | geo->payload_size = n * size; |
| 1166 | geo->page_size = page_size; |
| 1167 | geo->auxiliary_status_offset = ALIGN(meta, 4); |
| 1168 | |
| 1169 | dev_dbg(this->dev, "page:%d(%d:%d)%d, chunk:(%d:%d), BCH PG size:%d\n", |
| 1170 | page, offs, len, col, first, n, page_size); |
| 1171 | |
| 1172 | /* Read the subpage now */ |
| 1173 | this->swap_block_mark = false; |
Boris Brezillon | 4c7e95b | 2018-01-23 11:13:17 +0100 | [diff] [blame] | 1174 | max_bitflips = gpmi_ecc_read_page_data(chip, buf, 0, page); |
Huang Shijie | b8e2931 | 2014-01-03 11:01:42 +0800 | [diff] [blame] | 1175 | |
| 1176 | /* Restore */ |
| 1177 | writel(r1_old, bch_regs + HW_BCH_FLASH0LAYOUT0); |
| 1178 | writel(r2_old, bch_regs + HW_BCH_FLASH0LAYOUT1); |
| 1179 | this->bch_geometry = old_geo; |
Lothar Waßmann | 2a500af | 2014-03-28 11:35:06 +0100 | [diff] [blame] | 1180 | this->swap_block_mark = old_swap_block_mark; |
Huang Shijie | b8e2931 | 2014-01-03 11:01:42 +0800 | [diff] [blame] | 1181 | |
| 1182 | return max_bitflips; |
| 1183 | } |
| 1184 | |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame^] | 1185 | static int gpmi_ecc_write_page(struct nand_chip *chip, const uint8_t *buf, |
| 1186 | int oob_required, int page) |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1187 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame^] | 1188 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 1189 | struct gpmi_nand_data *this = nand_get_controller_data(chip); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1190 | struct bch_geometry *nfc_geo = &this->bch_geometry; |
| 1191 | const void *payload_virt; |
| 1192 | dma_addr_t payload_phys; |
| 1193 | const void *auxiliary_virt; |
| 1194 | dma_addr_t auxiliary_phys; |
| 1195 | int ret; |
| 1196 | |
Huang Shijie | c232596 | 2013-11-20 10:09:44 +0800 | [diff] [blame] | 1197 | dev_dbg(this->dev, "ecc write page.\n"); |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 1198 | |
| 1199 | nand_prog_page_begin_op(chip, page, 0, NULL, 0); |
| 1200 | |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1201 | if (this->swap_block_mark) { |
| 1202 | /* |
| 1203 | * If control arrives here, we're doing block mark swapping. |
| 1204 | * Since we can't modify the caller's buffers, we must copy them |
| 1205 | * into our own. |
| 1206 | */ |
| 1207 | memcpy(this->payload_virt, buf, mtd->writesize); |
| 1208 | payload_virt = this->payload_virt; |
| 1209 | payload_phys = this->payload_phys; |
| 1210 | |
| 1211 | memcpy(this->auxiliary_virt, chip->oob_poi, |
| 1212 | nfc_geo->auxiliary_size); |
| 1213 | auxiliary_virt = this->auxiliary_virt; |
| 1214 | auxiliary_phys = this->auxiliary_phys; |
| 1215 | |
| 1216 | /* Handle block mark swapping. */ |
| 1217 | block_mark_swapping(this, |
Lothar Waßmann | 6a76096 | 2014-06-12 15:20:41 +0200 | [diff] [blame] | 1218 | (void *)payload_virt, (void *)auxiliary_virt); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1219 | } else { |
| 1220 | /* |
| 1221 | * If control arrives here, we're not doing block mark swapping, |
| 1222 | * so we can to try and use the caller's buffers. |
| 1223 | */ |
| 1224 | ret = send_page_prepare(this, |
| 1225 | buf, mtd->writesize, |
| 1226 | this->payload_virt, this->payload_phys, |
| 1227 | nfc_geo->payload_size, |
| 1228 | &payload_virt, &payload_phys); |
| 1229 | if (ret) { |
Huang Shijie | da40c16 | 2013-11-20 10:09:43 +0800 | [diff] [blame] | 1230 | dev_err(this->dev, "Inadequate payload DMA buffer\n"); |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 1231 | return 0; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | ret = send_page_prepare(this, |
| 1235 | chip->oob_poi, mtd->oobsize, |
| 1236 | this->auxiliary_virt, this->auxiliary_phys, |
| 1237 | nfc_geo->auxiliary_size, |
| 1238 | &auxiliary_virt, &auxiliary_phys); |
| 1239 | if (ret) { |
Huang Shijie | da40c16 | 2013-11-20 10:09:43 +0800 | [diff] [blame] | 1240 | dev_err(this->dev, "Inadequate auxiliary DMA buffer\n"); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1241 | goto exit_auxiliary; |
| 1242 | } |
| 1243 | } |
| 1244 | |
| 1245 | /* Ask the NFC. */ |
| 1246 | ret = gpmi_send_page(this, payload_phys, auxiliary_phys); |
| 1247 | if (ret) |
Huang Shijie | da40c16 | 2013-11-20 10:09:43 +0800 | [diff] [blame] | 1248 | dev_err(this->dev, "Error in ECC-based write: %d\n", ret); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1249 | |
| 1250 | if (!this->swap_block_mark) { |
| 1251 | send_page_end(this, chip->oob_poi, mtd->oobsize, |
| 1252 | this->auxiliary_virt, this->auxiliary_phys, |
| 1253 | nfc_geo->auxiliary_size, |
| 1254 | auxiliary_virt, auxiliary_phys); |
| 1255 | exit_auxiliary: |
| 1256 | send_page_end(this, buf, mtd->writesize, |
| 1257 | this->payload_virt, this->payload_phys, |
| 1258 | nfc_geo->payload_size, |
| 1259 | payload_virt, payload_phys); |
| 1260 | } |
Josh Wu | fdbad98d | 2012-06-25 18:07:45 +0800 | [diff] [blame] | 1261 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 1262 | if (ret) |
| 1263 | return ret; |
| 1264 | |
| 1265 | return nand_prog_page_end_op(chip); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1266 | } |
| 1267 | |
| 1268 | /* |
| 1269 | * There are several places in this driver where we have to handle the OOB and |
| 1270 | * block marks. This is the function where things are the most complicated, so |
| 1271 | * this is where we try to explain it all. All the other places refer back to |
| 1272 | * here. |
| 1273 | * |
| 1274 | * These are the rules, in order of decreasing importance: |
| 1275 | * |
| 1276 | * 1) Nothing the caller does can be allowed to imperil the block mark. |
| 1277 | * |
| 1278 | * 2) In read operations, the first byte of the OOB we return must reflect the |
| 1279 | * true state of the block mark, no matter where that block mark appears in |
| 1280 | * the physical page. |
| 1281 | * |
| 1282 | * 3) ECC-based read operations return an OOB full of set bits (since we never |
| 1283 | * allow ECC-based writes to the OOB, it doesn't matter what ECC-based reads |
| 1284 | * return). |
| 1285 | * |
| 1286 | * 4) "Raw" read operations return a direct view of the physical bytes in the |
| 1287 | * page, using the conventional definition of which bytes are data and which |
| 1288 | * are OOB. This gives the caller a way to see the actual, physical bytes |
| 1289 | * in the page, without the distortions applied by our ECC engine. |
| 1290 | * |
| 1291 | * |
| 1292 | * What we do for this specific read operation depends on two questions: |
| 1293 | * |
| 1294 | * 1) Are we doing a "raw" read, or an ECC-based read? |
| 1295 | * |
| 1296 | * 2) Are we using block mark swapping or transcription? |
| 1297 | * |
| 1298 | * There are four cases, illustrated by the following Karnaugh map: |
| 1299 | * |
| 1300 | * | Raw | ECC-based | |
| 1301 | * -------------+-------------------------+-------------------------+ |
| 1302 | * | Read the conventional | | |
| 1303 | * | OOB at the end of the | | |
| 1304 | * Swapping | page and return it. It | | |
| 1305 | * | contains exactly what | | |
| 1306 | * | we want. | Read the block mark and | |
| 1307 | * -------------+-------------------------+ return it in a buffer | |
| 1308 | * | Read the conventional | full of set bits. | |
| 1309 | * | OOB at the end of the | | |
| 1310 | * | page and also the block | | |
| 1311 | * Transcribing | mark in the metadata. | | |
| 1312 | * | Copy the block mark | | |
| 1313 | * | into the first byte of | | |
| 1314 | * | the OOB. | | |
| 1315 | * -------------+-------------------------+-------------------------+ |
| 1316 | * |
| 1317 | * Note that we break rule #4 in the Transcribing/Raw case because we're not |
| 1318 | * giving an accurate view of the actual, physical bytes in the page (we're |
| 1319 | * overwriting the block mark). That's OK because it's more important to follow |
| 1320 | * rule #2. |
| 1321 | * |
| 1322 | * It turns out that knowing whether we want an "ECC-based" or "raw" read is not |
| 1323 | * easy. When reading a page, for example, the NAND Flash MTD code calls our |
| 1324 | * ecc.read_page or ecc.read_page_raw function. Thus, the fact that MTD wants an |
| 1325 | * ECC-based or raw view of the page is implicit in which function it calls |
| 1326 | * (there is a similar pair of ECC-based/raw functions for writing). |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1327 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 1328 | static int gpmi_ecc_read_oob(struct nand_chip *chip, int page) |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1329 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 1330 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 1331 | struct gpmi_nand_data *this = nand_get_controller_data(chip); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1332 | |
Huang Shijie | c232596 | 2013-11-20 10:09:44 +0800 | [diff] [blame] | 1333 | dev_dbg(this->dev, "page number is %d\n", page); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1334 | /* clear the OOB buffer */ |
| 1335 | memset(chip->oob_poi, ~0, mtd->oobsize); |
| 1336 | |
| 1337 | /* Read out the conventional OOB. */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1338 | nand_read_page_op(chip, page, mtd->writesize, NULL, 0); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1339 | chip->read_buf(mtd, chip->oob_poi, mtd->oobsize); |
| 1340 | |
| 1341 | /* |
| 1342 | * Now, we want to make sure the block mark is correct. In the |
Lothar Waßmann | 2a500af | 2014-03-28 11:35:06 +0100 | [diff] [blame] | 1343 | * non-transcribing case (!GPMI_IS_MX23()), we already have it. |
| 1344 | * Otherwise, we need to explicitly read it. |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1345 | */ |
Lothar Waßmann | 2a500af | 2014-03-28 11:35:06 +0100 | [diff] [blame] | 1346 | if (GPMI_IS_MX23(this)) { |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1347 | /* Read the block mark into the first byte of the OOB buffer. */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1348 | nand_read_page_op(chip, page, 0, NULL, 0); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1349 | chip->oob_poi[0] = chip->read_byte(mtd); |
| 1350 | } |
| 1351 | |
Shmulik Ladkani | 5c2ffb1 | 2012-05-09 13:06:35 +0300 | [diff] [blame] | 1352 | return 0; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1353 | } |
| 1354 | |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame^] | 1355 | static int gpmi_ecc_write_oob(struct nand_chip *chip, int page) |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1356 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame^] | 1357 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris Brezillon | 191a829 | 2016-02-03 20:11:44 +0100 | [diff] [blame] | 1358 | struct mtd_oob_region of = { }; |
Huang Shijie | 7a2b89a | 2013-09-25 14:58:15 +0800 | [diff] [blame] | 1359 | |
| 1360 | /* Do we have available oob area? */ |
Boris Brezillon | 191a829 | 2016-02-03 20:11:44 +0100 | [diff] [blame] | 1361 | mtd_ooblayout_free(mtd, 0, &of); |
| 1362 | if (!of.length) |
Huang Shijie | 7a2b89a | 2013-09-25 14:58:15 +0800 | [diff] [blame] | 1363 | return -EPERM; |
| 1364 | |
| 1365 | if (!nand_is_slc(chip)) |
| 1366 | return -EPERM; |
| 1367 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1368 | return nand_prog_page_op(chip, page, mtd->writesize + of.offset, |
| 1369 | chip->oob_poi + of.offset, of.length); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1370 | } |
| 1371 | |
Boris BREZILLON | da3bc42c | 2014-11-30 19:10:29 +0100 | [diff] [blame] | 1372 | /* |
| 1373 | * This function reads a NAND page without involving the ECC engine (no HW |
| 1374 | * ECC correction). |
| 1375 | * The tricky part in the GPMI/BCH controller is that it stores ECC bits |
| 1376 | * inline (interleaved with payload DATA), and do not align data chunk on |
| 1377 | * byte boundaries. |
| 1378 | * We thus need to take care moving the payload data and ECC bits stored in the |
| 1379 | * page into the provided buffers, which is why we're using gpmi_copy_bits. |
| 1380 | * |
| 1381 | * See set_geometry_by_ecc_info inline comments to have a full description |
| 1382 | * of the layout used by the GPMI controller. |
| 1383 | */ |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 1384 | static int gpmi_ecc_read_page_raw(struct nand_chip *chip, uint8_t *buf, |
Boris BREZILLON | da3bc42c | 2014-11-30 19:10:29 +0100 | [diff] [blame] | 1385 | int oob_required, int page) |
| 1386 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 1387 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 1388 | struct gpmi_nand_data *this = nand_get_controller_data(chip); |
Boris BREZILLON | da3bc42c | 2014-11-30 19:10:29 +0100 | [diff] [blame] | 1389 | struct bch_geometry *nfc_geo = &this->bch_geometry; |
| 1390 | int eccsize = nfc_geo->ecc_chunk_size; |
| 1391 | int eccbits = nfc_geo->ecc_strength * nfc_geo->gf_len; |
| 1392 | u8 *tmp_buf = this->raw_buffer; |
| 1393 | size_t src_bit_off; |
| 1394 | size_t oob_bit_off; |
| 1395 | size_t oob_byte_off; |
| 1396 | uint8_t *oob = chip->oob_poi; |
| 1397 | int step; |
| 1398 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 1399 | nand_read_page_op(chip, page, 0, tmp_buf, |
| 1400 | mtd->writesize + mtd->oobsize); |
Boris BREZILLON | da3bc42c | 2014-11-30 19:10:29 +0100 | [diff] [blame] | 1401 | |
| 1402 | /* |
| 1403 | * If required, swap the bad block marker and the data stored in the |
| 1404 | * metadata section, so that we don't wrongly consider a block as bad. |
| 1405 | * |
| 1406 | * See the layout description for a detailed explanation on why this |
| 1407 | * is needed. |
| 1408 | */ |
Gustavo A. R. Silva | b13a973 | 2017-11-03 15:31:47 -0500 | [diff] [blame] | 1409 | if (this->swap_block_mark) |
| 1410 | swap(tmp_buf[0], tmp_buf[mtd->writesize]); |
Boris BREZILLON | da3bc42c | 2014-11-30 19:10:29 +0100 | [diff] [blame] | 1411 | |
| 1412 | /* |
| 1413 | * Copy the metadata section into the oob buffer (this section is |
| 1414 | * guaranteed to be aligned on a byte boundary). |
| 1415 | */ |
| 1416 | if (oob_required) |
| 1417 | memcpy(oob, tmp_buf, nfc_geo->metadata_size); |
| 1418 | |
| 1419 | oob_bit_off = nfc_geo->metadata_size * 8; |
| 1420 | src_bit_off = oob_bit_off; |
| 1421 | |
| 1422 | /* Extract interleaved payload data and ECC bits */ |
| 1423 | for (step = 0; step < nfc_geo->ecc_chunk_count; step++) { |
| 1424 | if (buf) |
| 1425 | gpmi_copy_bits(buf, step * eccsize * 8, |
| 1426 | tmp_buf, src_bit_off, |
| 1427 | eccsize * 8); |
| 1428 | src_bit_off += eccsize * 8; |
| 1429 | |
| 1430 | /* Align last ECC block to align a byte boundary */ |
| 1431 | if (step == nfc_geo->ecc_chunk_count - 1 && |
| 1432 | (oob_bit_off + eccbits) % 8) |
| 1433 | eccbits += 8 - ((oob_bit_off + eccbits) % 8); |
| 1434 | |
| 1435 | if (oob_required) |
| 1436 | gpmi_copy_bits(oob, oob_bit_off, |
| 1437 | tmp_buf, src_bit_off, |
| 1438 | eccbits); |
| 1439 | |
| 1440 | src_bit_off += eccbits; |
| 1441 | oob_bit_off += eccbits; |
| 1442 | } |
| 1443 | |
| 1444 | if (oob_required) { |
| 1445 | oob_byte_off = oob_bit_off / 8; |
| 1446 | |
| 1447 | if (oob_byte_off < mtd->oobsize) |
| 1448 | memcpy(oob + oob_byte_off, |
| 1449 | tmp_buf + mtd->writesize + oob_byte_off, |
| 1450 | mtd->oobsize - oob_byte_off); |
| 1451 | } |
| 1452 | |
| 1453 | return 0; |
| 1454 | } |
| 1455 | |
| 1456 | /* |
| 1457 | * This function writes a NAND page without involving the ECC engine (no HW |
| 1458 | * ECC generation). |
| 1459 | * The tricky part in the GPMI/BCH controller is that it stores ECC bits |
| 1460 | * inline (interleaved with payload DATA), and do not align data chunk on |
| 1461 | * byte boundaries. |
| 1462 | * We thus need to take care moving the OOB area at the right place in the |
| 1463 | * final page, which is why we're using gpmi_copy_bits. |
| 1464 | * |
| 1465 | * See set_geometry_by_ecc_info inline comments to have a full description |
| 1466 | * of the layout used by the GPMI controller. |
| 1467 | */ |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame^] | 1468 | static int gpmi_ecc_write_page_raw(struct nand_chip *chip, const uint8_t *buf, |
Boris BREZILLON | 45aaeff | 2015-10-13 11:22:18 +0200 | [diff] [blame] | 1469 | int oob_required, int page) |
Boris BREZILLON | da3bc42c | 2014-11-30 19:10:29 +0100 | [diff] [blame] | 1470 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame^] | 1471 | struct mtd_info *mtd = nand_to_mtd(chip); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 1472 | struct gpmi_nand_data *this = nand_get_controller_data(chip); |
Boris BREZILLON | da3bc42c | 2014-11-30 19:10:29 +0100 | [diff] [blame] | 1473 | struct bch_geometry *nfc_geo = &this->bch_geometry; |
| 1474 | int eccsize = nfc_geo->ecc_chunk_size; |
| 1475 | int eccbits = nfc_geo->ecc_strength * nfc_geo->gf_len; |
| 1476 | u8 *tmp_buf = this->raw_buffer; |
| 1477 | uint8_t *oob = chip->oob_poi; |
| 1478 | size_t dst_bit_off; |
| 1479 | size_t oob_bit_off; |
| 1480 | size_t oob_byte_off; |
| 1481 | int step; |
| 1482 | |
| 1483 | /* |
| 1484 | * Initialize all bits to 1 in case we don't have a buffer for the |
| 1485 | * payload or oob data in order to leave unspecified bits of data |
| 1486 | * to their initial state. |
| 1487 | */ |
| 1488 | if (!buf || !oob_required) |
| 1489 | memset(tmp_buf, 0xff, mtd->writesize + mtd->oobsize); |
| 1490 | |
| 1491 | /* |
| 1492 | * First copy the metadata section (stored in oob buffer) at the |
| 1493 | * beginning of the page, as imposed by the GPMI layout. |
| 1494 | */ |
| 1495 | memcpy(tmp_buf, oob, nfc_geo->metadata_size); |
| 1496 | oob_bit_off = nfc_geo->metadata_size * 8; |
| 1497 | dst_bit_off = oob_bit_off; |
| 1498 | |
| 1499 | /* Interleave payload data and ECC bits */ |
| 1500 | for (step = 0; step < nfc_geo->ecc_chunk_count; step++) { |
| 1501 | if (buf) |
| 1502 | gpmi_copy_bits(tmp_buf, dst_bit_off, |
| 1503 | buf, step * eccsize * 8, eccsize * 8); |
| 1504 | dst_bit_off += eccsize * 8; |
| 1505 | |
| 1506 | /* Align last ECC block to align a byte boundary */ |
| 1507 | if (step == nfc_geo->ecc_chunk_count - 1 && |
| 1508 | (oob_bit_off + eccbits) % 8) |
| 1509 | eccbits += 8 - ((oob_bit_off + eccbits) % 8); |
| 1510 | |
| 1511 | if (oob_required) |
| 1512 | gpmi_copy_bits(tmp_buf, dst_bit_off, |
| 1513 | oob, oob_bit_off, eccbits); |
| 1514 | |
| 1515 | dst_bit_off += eccbits; |
| 1516 | oob_bit_off += eccbits; |
| 1517 | } |
| 1518 | |
| 1519 | oob_byte_off = oob_bit_off / 8; |
| 1520 | |
| 1521 | if (oob_required && oob_byte_off < mtd->oobsize) |
| 1522 | memcpy(tmp_buf + mtd->writesize + oob_byte_off, |
| 1523 | oob + oob_byte_off, mtd->oobsize - oob_byte_off); |
| 1524 | |
| 1525 | /* |
| 1526 | * If required, swap the bad block marker and the first byte of the |
| 1527 | * metadata section, so that we don't modify the bad block marker. |
| 1528 | * |
| 1529 | * See the layout description for a detailed explanation on why this |
| 1530 | * is needed. |
| 1531 | */ |
Gustavo A. R. Silva | b13a973 | 2017-11-03 15:31:47 -0500 | [diff] [blame] | 1532 | if (this->swap_block_mark) |
| 1533 | swap(tmp_buf[0], tmp_buf[mtd->writesize]); |
Boris BREZILLON | da3bc42c | 2014-11-30 19:10:29 +0100 | [diff] [blame] | 1534 | |
Boris Brezillon | 25f815f | 2017-11-30 18:01:30 +0100 | [diff] [blame] | 1535 | return nand_prog_page_op(chip, page, 0, tmp_buf, |
| 1536 | mtd->writesize + mtd->oobsize); |
Boris BREZILLON | da3bc42c | 2014-11-30 19:10:29 +0100 | [diff] [blame] | 1537 | } |
| 1538 | |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 1539 | static int gpmi_ecc_read_oob_raw(struct nand_chip *chip, int page) |
Boris BREZILLON | 7ca94e0 | 2014-11-30 19:10:30 +0100 | [diff] [blame] | 1540 | { |
Boris Brezillon | b976168 | 2018-09-06 14:05:20 +0200 | [diff] [blame] | 1541 | return gpmi_ecc_read_page_raw(chip, NULL, 1, page); |
Boris BREZILLON | 7ca94e0 | 2014-11-30 19:10:30 +0100 | [diff] [blame] | 1542 | } |
| 1543 | |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame^] | 1544 | static int gpmi_ecc_write_oob_raw(struct nand_chip *chip, int page) |
Boris BREZILLON | 7ca94e0 | 2014-11-30 19:10:30 +0100 | [diff] [blame] | 1545 | { |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame^] | 1546 | return gpmi_ecc_write_page_raw(chip, NULL, 1, page); |
Boris BREZILLON | 7ca94e0 | 2014-11-30 19:10:30 +0100 | [diff] [blame] | 1547 | } |
| 1548 | |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1549 | static int gpmi_block_markbad(struct mtd_info *mtd, loff_t ofs) |
| 1550 | { |
Boris BREZILLON | 4bd4ebc | 2015-12-01 12:03:04 +0100 | [diff] [blame] | 1551 | struct nand_chip *chip = mtd_to_nand(mtd); |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 1552 | struct gpmi_nand_data *this = nand_get_controller_data(chip); |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 1553 | int ret = 0; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1554 | uint8_t *block_mark; |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1555 | int column, page, chipnr; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1556 | |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 1557 | chipnr = (int)(ofs >> chip->chip_shift); |
| 1558 | chip->select_chip(mtd, chipnr); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1559 | |
Lothar Waßmann | 2a500af | 2014-03-28 11:35:06 +0100 | [diff] [blame] | 1560 | column = !GPMI_IS_MX23(this) ? mtd->writesize : 0; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1561 | |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 1562 | /* Write the block mark. */ |
| 1563 | block_mark = this->data_buffer_dma; |
| 1564 | block_mark[0] = 0; /* bad block marker */ |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1565 | |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 1566 | /* Shift to get page */ |
| 1567 | page = (int)(ofs >> chip->page_shift); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1568 | |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1569 | ret = nand_prog_page_op(chip, page, column, block_mark, 1); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1570 | |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 1571 | chip->select_chip(mtd, -1); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1572 | |
| 1573 | return ret; |
| 1574 | } |
| 1575 | |
Wolfram Sang | a78da28 | 2012-03-21 19:29:17 +0100 | [diff] [blame] | 1576 | static int nand_boot_set_geometry(struct gpmi_nand_data *this) |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1577 | { |
| 1578 | struct boot_rom_geometry *geometry = &this->rom_geometry; |
| 1579 | |
| 1580 | /* |
| 1581 | * Set the boot block stride size. |
| 1582 | * |
| 1583 | * In principle, we should be reading this from the OTP bits, since |
| 1584 | * that's where the ROM is going to get it. In fact, we don't have any |
| 1585 | * way to read the OTP bits, so we go with the default and hope for the |
| 1586 | * best. |
| 1587 | */ |
| 1588 | geometry->stride_size_in_pages = 64; |
| 1589 | |
| 1590 | /* |
| 1591 | * Set the search area stride exponent. |
| 1592 | * |
| 1593 | * In principle, we should be reading this from the OTP bits, since |
| 1594 | * that's where the ROM is going to get it. In fact, we don't have any |
| 1595 | * way to read the OTP bits, so we go with the default and hope for the |
| 1596 | * best. |
| 1597 | */ |
| 1598 | geometry->search_area_stride_exponent = 2; |
| 1599 | return 0; |
| 1600 | } |
| 1601 | |
| 1602 | static const char *fingerprint = "STMP"; |
Wolfram Sang | a78da28 | 2012-03-21 19:29:17 +0100 | [diff] [blame] | 1603 | static int mx23_check_transcription_stamp(struct gpmi_nand_data *this) |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1604 | { |
| 1605 | struct boot_rom_geometry *rom_geo = &this->rom_geometry; |
| 1606 | struct device *dev = this->dev; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1607 | struct nand_chip *chip = &this->nand; |
Boris BREZILLON | 2a690b2 | 2015-12-10 09:00:07 +0100 | [diff] [blame] | 1608 | struct mtd_info *mtd = nand_to_mtd(chip); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1609 | unsigned int search_area_size_in_strides; |
| 1610 | unsigned int stride; |
| 1611 | unsigned int page; |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 1612 | uint8_t *buffer = chip->data_buf; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1613 | int saved_chip_number; |
| 1614 | int found_an_ncb_fingerprint = false; |
| 1615 | |
| 1616 | /* Compute the number of strides in a search area. */ |
| 1617 | search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent; |
| 1618 | |
| 1619 | saved_chip_number = this->current_chip; |
| 1620 | chip->select_chip(mtd, 0); |
| 1621 | |
| 1622 | /* |
| 1623 | * Loop through the first search area, looking for the NCB fingerprint. |
| 1624 | */ |
| 1625 | dev_dbg(dev, "Scanning for an NCB fingerprint...\n"); |
| 1626 | |
| 1627 | for (stride = 0; stride < search_area_size_in_strides; stride++) { |
Huang Shijie | 513d57e | 2012-07-17 14:14:02 +0800 | [diff] [blame] | 1628 | /* Compute the page addresses. */ |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1629 | page = stride * rom_geo->stride_size_in_pages; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1630 | |
| 1631 | dev_dbg(dev, "Looking for a fingerprint in page 0x%x\n", page); |
| 1632 | |
| 1633 | /* |
| 1634 | * Read the NCB fingerprint. The fingerprint is four bytes long |
| 1635 | * and starts in the 12th byte of the page. |
| 1636 | */ |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1637 | nand_read_page_op(chip, page, 12, NULL, 0); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1638 | chip->read_buf(mtd, buffer, strlen(fingerprint)); |
| 1639 | |
| 1640 | /* Look for the fingerprint. */ |
| 1641 | if (!memcmp(buffer, fingerprint, strlen(fingerprint))) { |
| 1642 | found_an_ncb_fingerprint = true; |
| 1643 | break; |
| 1644 | } |
| 1645 | |
| 1646 | } |
| 1647 | |
| 1648 | chip->select_chip(mtd, saved_chip_number); |
| 1649 | |
| 1650 | if (found_an_ncb_fingerprint) |
| 1651 | dev_dbg(dev, "\tFound a fingerprint\n"); |
| 1652 | else |
| 1653 | dev_dbg(dev, "\tNo fingerprint found\n"); |
| 1654 | return found_an_ncb_fingerprint; |
| 1655 | } |
| 1656 | |
| 1657 | /* Writes a transcription stamp. */ |
Wolfram Sang | a78da28 | 2012-03-21 19:29:17 +0100 | [diff] [blame] | 1658 | static int mx23_write_transcription_stamp(struct gpmi_nand_data *this) |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1659 | { |
| 1660 | struct device *dev = this->dev; |
| 1661 | struct boot_rom_geometry *rom_geo = &this->rom_geometry; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1662 | struct nand_chip *chip = &this->nand; |
Boris BREZILLON | 2a690b2 | 2015-12-10 09:00:07 +0100 | [diff] [blame] | 1663 | struct mtd_info *mtd = nand_to_mtd(chip); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1664 | unsigned int block_size_in_pages; |
| 1665 | unsigned int search_area_size_in_strides; |
| 1666 | unsigned int search_area_size_in_pages; |
| 1667 | unsigned int search_area_size_in_blocks; |
| 1668 | unsigned int block; |
| 1669 | unsigned int stride; |
| 1670 | unsigned int page; |
Masahiro Yamada | c0313b9 | 2017-12-05 17:47:16 +0900 | [diff] [blame] | 1671 | uint8_t *buffer = chip->data_buf; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1672 | int saved_chip_number; |
| 1673 | int status; |
| 1674 | |
| 1675 | /* Compute the search area geometry. */ |
| 1676 | block_size_in_pages = mtd->erasesize / mtd->writesize; |
| 1677 | search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent; |
| 1678 | search_area_size_in_pages = search_area_size_in_strides * |
| 1679 | rom_geo->stride_size_in_pages; |
| 1680 | search_area_size_in_blocks = |
| 1681 | (search_area_size_in_pages + (block_size_in_pages - 1)) / |
| 1682 | block_size_in_pages; |
| 1683 | |
| 1684 | dev_dbg(dev, "Search Area Geometry :\n"); |
| 1685 | dev_dbg(dev, "\tin Blocks : %u\n", search_area_size_in_blocks); |
| 1686 | dev_dbg(dev, "\tin Strides: %u\n", search_area_size_in_strides); |
| 1687 | dev_dbg(dev, "\tin Pages : %u\n", search_area_size_in_pages); |
| 1688 | |
| 1689 | /* Select chip 0. */ |
| 1690 | saved_chip_number = this->current_chip; |
| 1691 | chip->select_chip(mtd, 0); |
| 1692 | |
| 1693 | /* Loop over blocks in the first search area, erasing them. */ |
| 1694 | dev_dbg(dev, "Erasing the search area...\n"); |
| 1695 | |
| 1696 | for (block = 0; block < search_area_size_in_blocks; block++) { |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1697 | /* Erase this block. */ |
| 1698 | dev_dbg(dev, "\tErasing block 0x%x\n", block); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1699 | status = nand_erase_op(chip, block); |
| 1700 | if (status) |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1701 | dev_err(dev, "[%s] Erase failed.\n", __func__); |
| 1702 | } |
| 1703 | |
| 1704 | /* Write the NCB fingerprint into the page buffer. */ |
| 1705 | memset(buffer, ~0, mtd->writesize); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1706 | memcpy(buffer + 12, fingerprint, strlen(fingerprint)); |
| 1707 | |
| 1708 | /* Loop through the first search area, writing NCB fingerprints. */ |
| 1709 | dev_dbg(dev, "Writing NCB fingerprints...\n"); |
| 1710 | for (stride = 0; stride < search_area_size_in_strides; stride++) { |
Huang Shijie | 513d57e | 2012-07-17 14:14:02 +0800 | [diff] [blame] | 1711 | /* Compute the page addresses. */ |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1712 | page = stride * rom_geo->stride_size_in_pages; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1713 | |
| 1714 | /* Write the first page of the current stride. */ |
| 1715 | dev_dbg(dev, "Writing an NCB fingerprint in page 0x%x\n", page); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1716 | |
Boris Brezillon | 767eb6f | 2018-09-06 14:05:21 +0200 | [diff] [blame^] | 1717 | status = chip->ecc.write_page_raw(chip, buffer, 0, page); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1718 | if (status) |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1719 | dev_err(dev, "[%s] Write failed.\n", __func__); |
| 1720 | } |
| 1721 | |
| 1722 | /* Deselect chip 0. */ |
| 1723 | chip->select_chip(mtd, saved_chip_number); |
| 1724 | return 0; |
| 1725 | } |
| 1726 | |
Wolfram Sang | a78da28 | 2012-03-21 19:29:17 +0100 | [diff] [blame] | 1727 | static int mx23_boot_init(struct gpmi_nand_data *this) |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1728 | { |
| 1729 | struct device *dev = this->dev; |
| 1730 | struct nand_chip *chip = &this->nand; |
Boris BREZILLON | 2a690b2 | 2015-12-10 09:00:07 +0100 | [diff] [blame] | 1731 | struct mtd_info *mtd = nand_to_mtd(chip); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1732 | unsigned int block_count; |
| 1733 | unsigned int block; |
| 1734 | int chipnr; |
| 1735 | int page; |
| 1736 | loff_t byte; |
| 1737 | uint8_t block_mark; |
| 1738 | int ret = 0; |
| 1739 | |
| 1740 | /* |
| 1741 | * If control arrives here, we can't use block mark swapping, which |
| 1742 | * means we're forced to use transcription. First, scan for the |
| 1743 | * transcription stamp. If we find it, then we don't have to do |
| 1744 | * anything -- the block marks are already transcribed. |
| 1745 | */ |
| 1746 | if (mx23_check_transcription_stamp(this)) |
| 1747 | return 0; |
| 1748 | |
| 1749 | /* |
| 1750 | * If control arrives here, we couldn't find a transcription stamp, so |
| 1751 | * so we presume the block marks are in the conventional location. |
| 1752 | */ |
| 1753 | dev_dbg(dev, "Transcribing bad block marks...\n"); |
| 1754 | |
| 1755 | /* Compute the number of blocks in the entire medium. */ |
| 1756 | block_count = chip->chipsize >> chip->phys_erase_shift; |
| 1757 | |
| 1758 | /* |
| 1759 | * Loop over all the blocks in the medium, transcribing block marks as |
| 1760 | * we go. |
| 1761 | */ |
| 1762 | for (block = 0; block < block_count; block++) { |
| 1763 | /* |
| 1764 | * Compute the chip, page and byte addresses for this block's |
| 1765 | * conventional mark. |
| 1766 | */ |
| 1767 | chipnr = block >> (chip->chip_shift - chip->phys_erase_shift); |
| 1768 | page = block << (chip->phys_erase_shift - chip->page_shift); |
| 1769 | byte = block << chip->phys_erase_shift; |
| 1770 | |
| 1771 | /* Send the command to read the conventional block mark. */ |
| 1772 | chip->select_chip(mtd, chipnr); |
Boris Brezillon | 97d90da | 2017-11-30 18:01:29 +0100 | [diff] [blame] | 1773 | nand_read_page_op(chip, page, mtd->writesize, NULL, 0); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1774 | block_mark = chip->read_byte(mtd); |
| 1775 | chip->select_chip(mtd, -1); |
| 1776 | |
| 1777 | /* |
| 1778 | * Check if the block is marked bad. If so, we need to mark it |
| 1779 | * again, but this time the result will be a mark in the |
| 1780 | * location where we transcribe block marks. |
| 1781 | */ |
| 1782 | if (block_mark != 0xff) { |
| 1783 | dev_dbg(dev, "Transcribing mark in block %u\n", block); |
| 1784 | ret = chip->block_markbad(mtd, byte); |
| 1785 | if (ret) |
Lothar Waßmann | d8c0372 | 2014-06-12 15:20:42 +0200 | [diff] [blame] | 1786 | dev_err(dev, |
| 1787 | "Failed to mark block bad with ret %d\n", |
| 1788 | ret); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1789 | } |
| 1790 | } |
| 1791 | |
| 1792 | /* Write the stamp that indicates we've transcribed the block marks. */ |
| 1793 | mx23_write_transcription_stamp(this); |
| 1794 | return 0; |
| 1795 | } |
| 1796 | |
Wolfram Sang | a78da28 | 2012-03-21 19:29:17 +0100 | [diff] [blame] | 1797 | static int nand_boot_init(struct gpmi_nand_data *this) |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1798 | { |
| 1799 | nand_boot_set_geometry(this); |
| 1800 | |
| 1801 | /* This is ROM arch-specific initilization before the BBT scanning. */ |
| 1802 | if (GPMI_IS_MX23(this)) |
| 1803 | return mx23_boot_init(this); |
| 1804 | return 0; |
| 1805 | } |
| 1806 | |
Wolfram Sang | a78da28 | 2012-03-21 19:29:17 +0100 | [diff] [blame] | 1807 | static int gpmi_set_geometry(struct gpmi_nand_data *this) |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1808 | { |
| 1809 | int ret; |
| 1810 | |
| 1811 | /* Free the temporary DMA memory for reading ID. */ |
| 1812 | gpmi_free_dma_buffer(this); |
| 1813 | |
| 1814 | /* Set up the NFC geometry which is used by BCH. */ |
| 1815 | ret = bch_set_geometry(this); |
| 1816 | if (ret) { |
Huang Shijie | da40c16 | 2013-11-20 10:09:43 +0800 | [diff] [blame] | 1817 | dev_err(this->dev, "Error setting BCH geometry : %d\n", ret); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1818 | return ret; |
| 1819 | } |
| 1820 | |
| 1821 | /* Alloc the new DMA buffers according to the pagesize and oobsize */ |
| 1822 | return gpmi_alloc_dma_buffer(this); |
| 1823 | } |
| 1824 | |
Huang Shijie | f720e7c | 2013-08-16 10:10:08 +0800 | [diff] [blame] | 1825 | static int gpmi_init_last(struct gpmi_nand_data *this) |
| 1826 | { |
Boris BREZILLON | 2a690b2 | 2015-12-10 09:00:07 +0100 | [diff] [blame] | 1827 | struct nand_chip *chip = &this->nand; |
Boris Brezillon | 3f158e4 | 2016-02-03 20:01:54 +0100 | [diff] [blame] | 1828 | struct mtd_info *mtd = nand_to_mtd(chip); |
Huang Shijie | f720e7c | 2013-08-16 10:10:08 +0800 | [diff] [blame] | 1829 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
| 1830 | struct bch_geometry *bch_geo = &this->bch_geometry; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1831 | int ret; |
| 1832 | |
Huang Shijie | d7364a27 | 2013-11-14 14:25:45 +0800 | [diff] [blame] | 1833 | /* Set up the medium geometry */ |
| 1834 | ret = gpmi_set_geometry(this); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1835 | if (ret) |
| 1836 | return ret; |
| 1837 | |
Huang Shijie | f720e7c | 2013-08-16 10:10:08 +0800 | [diff] [blame] | 1838 | /* Init the nand_ecc_ctrl{} */ |
| 1839 | ecc->read_page = gpmi_ecc_read_page; |
| 1840 | ecc->write_page = gpmi_ecc_write_page; |
| 1841 | ecc->read_oob = gpmi_ecc_read_oob; |
| 1842 | ecc->write_oob = gpmi_ecc_write_oob; |
Boris BREZILLON | da3bc42c | 2014-11-30 19:10:29 +0100 | [diff] [blame] | 1843 | ecc->read_page_raw = gpmi_ecc_read_page_raw; |
| 1844 | ecc->write_page_raw = gpmi_ecc_write_page_raw; |
Boris BREZILLON | 7ca94e0 | 2014-11-30 19:10:30 +0100 | [diff] [blame] | 1845 | ecc->read_oob_raw = gpmi_ecc_read_oob_raw; |
| 1846 | ecc->write_oob_raw = gpmi_ecc_write_oob_raw; |
Huang Shijie | f720e7c | 2013-08-16 10:10:08 +0800 | [diff] [blame] | 1847 | ecc->mode = NAND_ECC_HW; |
| 1848 | ecc->size = bch_geo->ecc_chunk_size; |
| 1849 | ecc->strength = bch_geo->ecc_strength; |
Boris Brezillon | 3f158e4 | 2016-02-03 20:01:54 +0100 | [diff] [blame] | 1850 | mtd_set_ooblayout(mtd, &gpmi_ooblayout_ops); |
Huang Shijie | f720e7c | 2013-08-16 10:10:08 +0800 | [diff] [blame] | 1851 | |
Huang Shijie | 995fbbf | 2012-09-13 14:57:59 +0800 | [diff] [blame] | 1852 | /* |
Huang Shijie | b8e2931 | 2014-01-03 11:01:42 +0800 | [diff] [blame] | 1853 | * We only enable the subpage read when: |
| 1854 | * (1) the chip is imx6, and |
| 1855 | * (2) the size of the ECC parity is byte aligned. |
| 1856 | */ |
Huang Shijie | 91f5498 | 2014-03-27 10:43:22 +0800 | [diff] [blame] | 1857 | if (GPMI_IS_MX6(this) && |
Huang Shijie | b8e2931 | 2014-01-03 11:01:42 +0800 | [diff] [blame] | 1858 | ((bch_geo->gf_len * bch_geo->ecc_strength) % 8) == 0) { |
| 1859 | ecc->read_subpage = gpmi_ecc_read_subpage; |
| 1860 | chip->options |= NAND_SUBPAGE_READ; |
| 1861 | } |
| 1862 | |
Huang Shijie | f720e7c | 2013-08-16 10:10:08 +0800 | [diff] [blame] | 1863 | return 0; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1864 | } |
| 1865 | |
Miquel Raynal | 5f8374d | 2018-07-20 17:15:00 +0200 | [diff] [blame] | 1866 | static int gpmi_nand_attach_chip(struct nand_chip *chip) |
| 1867 | { |
| 1868 | struct gpmi_nand_data *this = nand_get_controller_data(chip); |
| 1869 | int ret; |
| 1870 | |
| 1871 | if (chip->bbt_options & NAND_BBT_USE_FLASH) { |
| 1872 | chip->bbt_options |= NAND_BBT_NO_OOB; |
| 1873 | |
| 1874 | if (of_property_read_bool(this->dev->of_node, |
| 1875 | "fsl,no-blockmark-swap")) |
| 1876 | this->swap_block_mark = false; |
| 1877 | } |
| 1878 | dev_dbg(this->dev, "Blockmark swapping %sabled\n", |
| 1879 | this->swap_block_mark ? "en" : "dis"); |
| 1880 | |
| 1881 | ret = gpmi_init_last(this); |
| 1882 | if (ret) |
| 1883 | return ret; |
| 1884 | |
| 1885 | chip->options |= NAND_SKIP_BBTSCAN; |
| 1886 | |
| 1887 | return 0; |
| 1888 | } |
| 1889 | |
| 1890 | static const struct nand_controller_ops gpmi_nand_controller_ops = { |
| 1891 | .attach_chip = gpmi_nand_attach_chip, |
| 1892 | }; |
| 1893 | |
Huang Shijie | ccce417 | 2013-11-14 14:25:47 +0800 | [diff] [blame] | 1894 | static int gpmi_nand_init(struct gpmi_nand_data *this) |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1895 | { |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1896 | struct nand_chip *chip = &this->nand; |
Boris BREZILLON | 2a690b2 | 2015-12-10 09:00:07 +0100 | [diff] [blame] | 1897 | struct mtd_info *mtd = nand_to_mtd(chip); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1898 | int ret; |
| 1899 | |
| 1900 | /* init current chip */ |
| 1901 | this->current_chip = -1; |
| 1902 | |
| 1903 | /* init the MTD data structures */ |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1904 | mtd->name = "gpmi-nand"; |
Frans Klaver | 4dc67b1 | 2015-06-10 22:38:49 +0200 | [diff] [blame] | 1905 | mtd->dev.parent = this->dev; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1906 | |
| 1907 | /* init the nand_chip{}, we don't support a 16-bit NAND Flash bus. */ |
Boris BREZILLON | d699ed2 | 2015-12-10 09:00:41 +0100 | [diff] [blame] | 1908 | nand_set_controller_data(chip, this); |
Brian Norris | a61ae81 | 2015-10-30 20:33:25 -0700 | [diff] [blame] | 1909 | nand_set_flash_node(chip, this->pdev->dev.of_node); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1910 | chip->select_chip = gpmi_select_chip; |
Miquel Raynal | 76e1a00 | 2018-03-02 15:38:39 +0100 | [diff] [blame] | 1911 | chip->setup_data_interface = gpmi_setup_data_interface; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1912 | chip->cmd_ctrl = gpmi_cmd_ctrl; |
| 1913 | chip->dev_ready = gpmi_dev_ready; |
| 1914 | chip->read_byte = gpmi_read_byte; |
| 1915 | chip->read_buf = gpmi_read_buf; |
| 1916 | chip->write_buf = gpmi_write_buf; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1917 | chip->badblock_pattern = &gpmi_bbt_descr; |
| 1918 | chip->block_markbad = gpmi_block_markbad; |
| 1919 | chip->options |= NAND_NO_SUBPAGE_WRITE; |
Lothar Waßmann | 2a500af | 2014-03-28 11:35:06 +0100 | [diff] [blame] | 1920 | |
| 1921 | /* Set up swap_block_mark, must be set before the gpmi_set_geometry() */ |
| 1922 | this->swap_block_mark = !GPMI_IS_MX23(this); |
| 1923 | |
Huang Shijie | f720e7c | 2013-08-16 10:10:08 +0800 | [diff] [blame] | 1924 | /* |
| 1925 | * Allocate a temporary DMA buffer for reading ID in the |
| 1926 | * nand_scan_ident(). |
| 1927 | */ |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1928 | this->bch_geometry.payload_size = 1024; |
| 1929 | this->bch_geometry.auxiliary_size = 128; |
| 1930 | ret = gpmi_alloc_dma_buffer(this); |
| 1931 | if (ret) |
| 1932 | goto err_out; |
| 1933 | |
Miquel Raynal | 5f8374d | 2018-07-20 17:15:00 +0200 | [diff] [blame] | 1934 | chip->dummy_controller.ops = &gpmi_nand_controller_ops; |
Boris Brezillon | 00ad378 | 2018-09-06 14:05:14 +0200 | [diff] [blame] | 1935 | ret = nand_scan(chip, GPMI_IS_MX6(this) ? 2 : 1); |
Huang Shijie | f720e7c | 2013-08-16 10:10:08 +0800 | [diff] [blame] | 1936 | if (ret) |
| 1937 | goto err_out; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1938 | |
Huang Shijie | 885d71e | 2013-11-12 12:23:08 +0800 | [diff] [blame] | 1939 | ret = nand_boot_init(this); |
| 1940 | if (ret) |
Boris Brezillon | 4d02423 | 2017-04-10 10:35:17 +0200 | [diff] [blame] | 1941 | goto err_nand_cleanup; |
Boris Brezillon | e80eba7 | 2018-07-05 12:27:31 +0200 | [diff] [blame] | 1942 | ret = nand_create_bbt(chip); |
Fabio Estevam | 899b834 | 2015-02-09 19:22:33 -0200 | [diff] [blame] | 1943 | if (ret) |
Boris Brezillon | 4d02423 | 2017-04-10 10:35:17 +0200 | [diff] [blame] | 1944 | goto err_nand_cleanup; |
Huang Shijie | 885d71e | 2013-11-12 12:23:08 +0800 | [diff] [blame] | 1945 | |
Brian Norris | a61ae81 | 2015-10-30 20:33:25 -0700 | [diff] [blame] | 1946 | ret = mtd_device_register(mtd, NULL, 0); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1947 | if (ret) |
Boris Brezillon | 4d02423 | 2017-04-10 10:35:17 +0200 | [diff] [blame] | 1948 | goto err_nand_cleanup; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1949 | return 0; |
| 1950 | |
Boris Brezillon | 4d02423 | 2017-04-10 10:35:17 +0200 | [diff] [blame] | 1951 | err_nand_cleanup: |
| 1952 | nand_cleanup(chip); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1953 | err_out: |
Boris Brezillon | 4d02423 | 2017-04-10 10:35:17 +0200 | [diff] [blame] | 1954 | gpmi_free_dma_buffer(this); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1955 | return ret; |
| 1956 | } |
| 1957 | |
Huang Shijie | e10db1f | 2012-05-04 21:42:05 -0400 | [diff] [blame] | 1958 | static const struct of_device_id gpmi_nand_id_table[] = { |
| 1959 | { |
| 1960 | .compatible = "fsl,imx23-gpmi-nand", |
Lothar Waßmann | 6a76096 | 2014-06-12 15:20:41 +0200 | [diff] [blame] | 1961 | .data = &gpmi_devdata_imx23, |
Huang Shijie | e10db1f | 2012-05-04 21:42:05 -0400 | [diff] [blame] | 1962 | }, { |
| 1963 | .compatible = "fsl,imx28-gpmi-nand", |
Lothar Waßmann | 6a76096 | 2014-06-12 15:20:41 +0200 | [diff] [blame] | 1964 | .data = &gpmi_devdata_imx28, |
Huang Shijie | 9013bb4 | 2012-05-04 21:42:06 -0400 | [diff] [blame] | 1965 | }, { |
| 1966 | .compatible = "fsl,imx6q-gpmi-nand", |
Lothar Waßmann | 6a76096 | 2014-06-12 15:20:41 +0200 | [diff] [blame] | 1967 | .data = &gpmi_devdata_imx6q, |
Huang Shijie | 91f5498 | 2014-03-27 10:43:22 +0800 | [diff] [blame] | 1968 | }, { |
| 1969 | .compatible = "fsl,imx6sx-gpmi-nand", |
Lothar Waßmann | 6a76096 | 2014-06-12 15:20:41 +0200 | [diff] [blame] | 1970 | .data = &gpmi_devdata_imx6sx, |
Stefan Agner | b4af694 | 2017-04-21 18:23:35 -0700 | [diff] [blame] | 1971 | }, { |
| 1972 | .compatible = "fsl,imx7d-gpmi-nand", |
| 1973 | .data = &gpmi_devdata_imx7d, |
Huang Shijie | e10db1f | 2012-05-04 21:42:05 -0400 | [diff] [blame] | 1974 | }, {} |
| 1975 | }; |
| 1976 | MODULE_DEVICE_TABLE(of, gpmi_nand_id_table); |
| 1977 | |
Bill Pemberton | 06f2551 | 2012-11-19 13:23:07 -0500 | [diff] [blame] | 1978 | static int gpmi_nand_probe(struct platform_device *pdev) |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1979 | { |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1980 | struct gpmi_nand_data *this; |
Huang Shijie | e10db1f | 2012-05-04 21:42:05 -0400 | [diff] [blame] | 1981 | const struct of_device_id *of_id; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1982 | int ret; |
| 1983 | |
Huang Shijie | 6189ccc | 2014-03-21 18:19:39 +0800 | [diff] [blame] | 1984 | this = devm_kzalloc(&pdev->dev, sizeof(*this), GFP_KERNEL); |
| 1985 | if (!this) |
| 1986 | return -ENOMEM; |
| 1987 | |
Huang Shijie | e10db1f | 2012-05-04 21:42:05 -0400 | [diff] [blame] | 1988 | of_id = of_match_device(gpmi_nand_id_table, &pdev->dev); |
| 1989 | if (of_id) { |
Huang Shijie | 6189ccc | 2014-03-21 18:19:39 +0800 | [diff] [blame] | 1990 | this->devdata = of_id->data; |
Huang Shijie | e10db1f | 2012-05-04 21:42:05 -0400 | [diff] [blame] | 1991 | } else { |
Huang Shijie | da40c16 | 2013-11-20 10:09:43 +0800 | [diff] [blame] | 1992 | dev_err(&pdev->dev, "Failed to find the right device id.\n"); |
Lothar Waßmann | 52a073b | 2013-08-07 08:15:38 +0200 | [diff] [blame] | 1993 | return -ENODEV; |
Huang Shijie | e10db1f | 2012-05-04 21:42:05 -0400 | [diff] [blame] | 1994 | } |
| 1995 | |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1996 | platform_set_drvdata(pdev, this); |
| 1997 | this->pdev = pdev; |
| 1998 | this->dev = &pdev->dev; |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 1999 | |
| 2000 | ret = acquire_resources(this); |
| 2001 | if (ret) |
| 2002 | goto exit_acquire_resources; |
| 2003 | |
Miquel Raynal | b120612 | 2018-03-02 15:38:40 +0100 | [diff] [blame] | 2004 | ret = gpmi_init(this); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 2005 | if (ret) |
| 2006 | goto exit_nfc_init; |
| 2007 | |
Huang Shijie | ccce417 | 2013-11-14 14:25:47 +0800 | [diff] [blame] | 2008 | ret = gpmi_nand_init(this); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 2009 | if (ret) |
| 2010 | goto exit_nfc_init; |
| 2011 | |
Fabio Estevam | 490e280 | 2012-09-05 11:35:24 -0300 | [diff] [blame] | 2012 | dev_info(this->dev, "driver registered.\n"); |
| 2013 | |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 2014 | return 0; |
| 2015 | |
| 2016 | exit_nfc_init: |
| 2017 | release_resources(this); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 2018 | exit_acquire_resources: |
Fabio Estevam | 490e280 | 2012-09-05 11:35:24 -0300 | [diff] [blame] | 2019 | |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 2020 | return ret; |
| 2021 | } |
| 2022 | |
Bill Pemberton | 810b7e0 | 2012-11-19 13:26:04 -0500 | [diff] [blame] | 2023 | static int gpmi_nand_remove(struct platform_device *pdev) |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 2024 | { |
| 2025 | struct gpmi_nand_data *this = platform_get_drvdata(pdev); |
| 2026 | |
Boris Brezillon | 59ac276 | 2018-09-06 14:05:15 +0200 | [diff] [blame] | 2027 | nand_release(&this->nand); |
Boris Brezillon | ebb528d9 | 2017-04-10 10:35:18 +0200 | [diff] [blame] | 2028 | gpmi_free_dma_buffer(this); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 2029 | release_resources(this); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 2030 | return 0; |
| 2031 | } |
| 2032 | |
Huang Shijie | 026918e | 2015-12-02 16:47:40 -0600 | [diff] [blame] | 2033 | #ifdef CONFIG_PM_SLEEP |
| 2034 | static int gpmi_pm_suspend(struct device *dev) |
| 2035 | { |
| 2036 | struct gpmi_nand_data *this = dev_get_drvdata(dev); |
| 2037 | |
| 2038 | release_dma_channels(this); |
| 2039 | return 0; |
| 2040 | } |
| 2041 | |
| 2042 | static int gpmi_pm_resume(struct device *dev) |
| 2043 | { |
| 2044 | struct gpmi_nand_data *this = dev_get_drvdata(dev); |
| 2045 | int ret; |
| 2046 | |
| 2047 | ret = acquire_dma_channels(this); |
| 2048 | if (ret < 0) |
| 2049 | return ret; |
| 2050 | |
| 2051 | /* re-init the GPMI registers */ |
Huang Shijie | 026918e | 2015-12-02 16:47:40 -0600 | [diff] [blame] | 2052 | ret = gpmi_init(this); |
| 2053 | if (ret) { |
| 2054 | dev_err(this->dev, "Error setting GPMI : %d\n", ret); |
| 2055 | return ret; |
| 2056 | } |
| 2057 | |
| 2058 | /* re-init the BCH registers */ |
| 2059 | ret = bch_set_geometry(this); |
| 2060 | if (ret) { |
| 2061 | dev_err(this->dev, "Error setting BCH : %d\n", ret); |
| 2062 | return ret; |
| 2063 | } |
| 2064 | |
Huang Shijie | 026918e | 2015-12-02 16:47:40 -0600 | [diff] [blame] | 2065 | return 0; |
| 2066 | } |
| 2067 | #endif /* CONFIG_PM_SLEEP */ |
| 2068 | |
| 2069 | static const struct dev_pm_ops gpmi_pm_ops = { |
| 2070 | SET_SYSTEM_SLEEP_PM_OPS(gpmi_pm_suspend, gpmi_pm_resume) |
| 2071 | }; |
| 2072 | |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 2073 | static struct platform_driver gpmi_nand_driver = { |
| 2074 | .driver = { |
| 2075 | .name = "gpmi-nand", |
Huang Shijie | 026918e | 2015-12-02 16:47:40 -0600 | [diff] [blame] | 2076 | .pm = &gpmi_pm_ops, |
Huang Shijie | e10db1f | 2012-05-04 21:42:05 -0400 | [diff] [blame] | 2077 | .of_match_table = gpmi_nand_id_table, |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 2078 | }, |
| 2079 | .probe = gpmi_nand_probe, |
Bill Pemberton | 5153b88 | 2012-11-19 13:21:24 -0500 | [diff] [blame] | 2080 | .remove = gpmi_nand_remove, |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 2081 | }; |
Fabio Estevam | 490e280 | 2012-09-05 11:35:24 -0300 | [diff] [blame] | 2082 | module_platform_driver(gpmi_nand_driver); |
Huang Shijie | 10a2bca | 2011-09-08 10:47:09 +0800 | [diff] [blame] | 2083 | |
| 2084 | MODULE_AUTHOR("Freescale Semiconductor, Inc."); |
| 2085 | MODULE_DESCRIPTION("i.MX GPMI NAND Flash Controller Driver"); |
| 2086 | MODULE_LICENSE("GPL"); |