Boris Brezillon | b95db8d | 2018-08-16 17:30:09 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | /* |
| 3 | * Copyright 2017 - Free Electrons |
| 4 | * |
| 5 | * Authors: |
| 6 | * Boris Brezillon <boris.brezillon@free-electrons.com> |
| 7 | * Peter Pan <peterpandong@micron.com> |
| 8 | */ |
| 9 | |
| 10 | #ifndef __LINUX_MTD_NAND_H |
| 11 | #define __LINUX_MTD_NAND_H |
| 12 | |
| 13 | #include <linux/mtd/mtd.h> |
| 14 | |
| 15 | /** |
| 16 | * struct nand_memory_organization - Memory organization structure |
| 17 | * @bits_per_cell: number of bits per NAND cell |
| 18 | * @pagesize: page size |
| 19 | * @oobsize: OOB area size |
| 20 | * @pages_per_eraseblock: number of pages per eraseblock |
| 21 | * @eraseblocks_per_lun: number of eraseblocks per LUN (Logical Unit Number) |
| 22 | * @planes_per_lun: number of planes per LUN |
| 23 | * @luns_per_target: number of LUN per target (target is a synonym for die) |
| 24 | * @ntargets: total number of targets exposed by the NAND device |
| 25 | */ |
| 26 | struct nand_memory_organization { |
| 27 | unsigned int bits_per_cell; |
| 28 | unsigned int pagesize; |
| 29 | unsigned int oobsize; |
| 30 | unsigned int pages_per_eraseblock; |
| 31 | unsigned int eraseblocks_per_lun; |
| 32 | unsigned int planes_per_lun; |
| 33 | unsigned int luns_per_target; |
| 34 | unsigned int ntargets; |
| 35 | }; |
| 36 | |
| 37 | #define NAND_MEMORG(bpc, ps, os, ppe, epl, ppl, lpt, nt) \ |
| 38 | { \ |
| 39 | .bits_per_cell = (bpc), \ |
| 40 | .pagesize = (ps), \ |
| 41 | .oobsize = (os), \ |
| 42 | .pages_per_eraseblock = (ppe), \ |
| 43 | .eraseblocks_per_lun = (epl), \ |
| 44 | .planes_per_lun = (ppl), \ |
| 45 | .luns_per_target = (lpt), \ |
| 46 | .ntargets = (nt), \ |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * struct nand_row_converter - Information needed to convert an absolute offset |
| 51 | * into a row address |
| 52 | * @lun_addr_shift: position of the LUN identifier in the row address |
| 53 | * @eraseblock_addr_shift: position of the eraseblock identifier in the row |
| 54 | * address |
| 55 | */ |
| 56 | struct nand_row_converter { |
| 57 | unsigned int lun_addr_shift; |
| 58 | unsigned int eraseblock_addr_shift; |
| 59 | }; |
| 60 | |
| 61 | /** |
| 62 | * struct nand_pos - NAND position object |
| 63 | * @target: the NAND target/die |
| 64 | * @lun: the LUN identifier |
| 65 | * @plane: the plane within the LUN |
| 66 | * @eraseblock: the eraseblock within the LUN |
| 67 | * @page: the page within the LUN |
| 68 | * |
| 69 | * These information are usually used by specific sub-layers to select the |
| 70 | * appropriate target/die and generate a row address to pass to the device. |
| 71 | */ |
| 72 | struct nand_pos { |
| 73 | unsigned int target; |
| 74 | unsigned int lun; |
| 75 | unsigned int plane; |
| 76 | unsigned int eraseblock; |
| 77 | unsigned int page; |
| 78 | }; |
| 79 | |
| 80 | /** |
| 81 | * struct nand_page_io_req - NAND I/O request object |
| 82 | * @pos: the position this I/O request is targeting |
| 83 | * @dataoffs: the offset within the page |
| 84 | * @datalen: number of data bytes to read from/write to this page |
| 85 | * @databuf: buffer to store data in or get data from |
| 86 | * @ooboffs: the OOB offset within the page |
| 87 | * @ooblen: the number of OOB bytes to read from/write to this page |
| 88 | * @oobbuf: buffer to store OOB data in or get OOB data from |
Boris Brezillon | f867872 | 2018-08-16 17:30:10 +0200 | [diff] [blame] | 89 | * @mode: one of the %MTD_OPS_XXX mode |
Boris Brezillon | b95db8d | 2018-08-16 17:30:09 +0200 | [diff] [blame] | 90 | * |
| 91 | * This object is used to pass per-page I/O requests to NAND sub-layers. This |
| 92 | * way all useful information are already formatted in a useful way and |
| 93 | * specific NAND layers can focus on translating these information into |
| 94 | * specific commands/operations. |
| 95 | */ |
| 96 | struct nand_page_io_req { |
| 97 | struct nand_pos pos; |
| 98 | unsigned int dataoffs; |
| 99 | unsigned int datalen; |
| 100 | union { |
| 101 | const void *out; |
| 102 | void *in; |
| 103 | } databuf; |
| 104 | unsigned int ooboffs; |
| 105 | unsigned int ooblen; |
| 106 | union { |
| 107 | const void *out; |
| 108 | void *in; |
| 109 | } oobbuf; |
Boris Brezillon | f867872 | 2018-08-16 17:30:10 +0200 | [diff] [blame] | 110 | int mode; |
Boris Brezillon | b95db8d | 2018-08-16 17:30:09 +0200 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | /** |
| 114 | * struct nand_ecc_req - NAND ECC requirements |
| 115 | * @strength: ECC strength |
| 116 | * @step_size: ECC step/block size |
| 117 | */ |
| 118 | struct nand_ecc_req { |
| 119 | unsigned int strength; |
| 120 | unsigned int step_size; |
| 121 | }; |
| 122 | |
| 123 | #define NAND_ECCREQ(str, stp) { .strength = (str), .step_size = (stp) } |
| 124 | |
| 125 | /** |
| 126 | * struct nand_bbt - bad block table object |
| 127 | * @cache: in memory BBT cache |
| 128 | */ |
| 129 | struct nand_bbt { |
| 130 | unsigned long *cache; |
| 131 | }; |
| 132 | |
| 133 | struct nand_device; |
| 134 | |
| 135 | /** |
| 136 | * struct nand_ops - NAND operations |
| 137 | * @erase: erase a specific block. No need to check if the block is bad before |
| 138 | * erasing, this has been taken care of by the generic NAND layer |
| 139 | * @markbad: mark a specific block bad. No need to check if the block is |
| 140 | * already marked bad, this has been taken care of by the generic |
| 141 | * NAND layer. This method should just write the BBM (Bad Block |
| 142 | * Marker) so that future call to struct_nand_ops->isbad() return |
| 143 | * true |
| 144 | * @isbad: check whether a block is bad or not. This method should just read |
| 145 | * the BBM and return whether the block is bad or not based on what it |
| 146 | * reads |
| 147 | * |
| 148 | * These are all low level operations that should be implemented by specialized |
| 149 | * NAND layers (SPI NAND, raw NAND, ...). |
| 150 | */ |
| 151 | struct nand_ops { |
| 152 | int (*erase)(struct nand_device *nand, const struct nand_pos *pos); |
| 153 | int (*markbad)(struct nand_device *nand, const struct nand_pos *pos); |
| 154 | bool (*isbad)(struct nand_device *nand, const struct nand_pos *pos); |
| 155 | }; |
| 156 | |
| 157 | /** |
| 158 | * struct nand_device - NAND device |
| 159 | * @mtd: MTD instance attached to the NAND device |
| 160 | * @memorg: memory layout |
| 161 | * @eccreq: ECC requirements |
| 162 | * @rowconv: position to row address converter |
| 163 | * @bbt: bad block table info |
| 164 | * @ops: NAND operations attached to the NAND device |
| 165 | * |
| 166 | * Generic NAND object. Specialized NAND layers (raw NAND, SPI NAND, OneNAND) |
| 167 | * should declare their own NAND object embedding a nand_device struct (that's |
| 168 | * how inheritance is done). |
| 169 | * struct_nand_device->memorg and struct_nand_device->eccreq should be filled |
| 170 | * at device detection time to reflect the NAND device |
| 171 | * capabilities/requirements. Once this is done nanddev_init() can be called. |
| 172 | * It will take care of converting NAND information into MTD ones, which means |
| 173 | * the specialized NAND layers should never manually tweak |
| 174 | * struct_nand_device->mtd except for the ->_read/write() hooks. |
| 175 | */ |
| 176 | struct nand_device { |
| 177 | struct mtd_info *mtd; |
| 178 | struct nand_memory_organization memorg; |
| 179 | struct nand_ecc_req eccreq; |
| 180 | struct nand_row_converter rowconv; |
| 181 | struct nand_bbt bbt; |
| 182 | const struct nand_ops *ops; |
| 183 | }; |
| 184 | |
| 185 | /** |
| 186 | * struct nand_io_iter - NAND I/O iterator |
| 187 | * @req: current I/O request |
| 188 | * @oobbytes_per_page: maximum number of OOB bytes per page |
| 189 | * @dataleft: remaining number of data bytes to read/write |
| 190 | * @oobleft: remaining number of OOB bytes to read/write |
| 191 | * |
| 192 | * Can be used by specialized NAND layers to iterate over all pages covered |
| 193 | * by an MTD I/O request, which should greatly simplifies the boiler-plate |
| 194 | * code needed to read/write data from/to a NAND device. |
| 195 | */ |
| 196 | struct nand_io_iter { |
| 197 | struct nand_page_io_req req; |
| 198 | unsigned int oobbytes_per_page; |
| 199 | unsigned int dataleft; |
| 200 | unsigned int oobleft; |
| 201 | }; |
| 202 | |
| 203 | /** |
| 204 | * mtd_to_nanddev() - Get the NAND device attached to the MTD instance |
| 205 | * @mtd: MTD instance |
| 206 | * |
| 207 | * Return: the NAND device embedding @mtd. |
| 208 | */ |
| 209 | static inline struct nand_device *mtd_to_nanddev(struct mtd_info *mtd) |
| 210 | { |
| 211 | return mtd->priv; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * nanddev_to_mtd() - Get the MTD device attached to a NAND device |
| 216 | * @nand: NAND device |
| 217 | * |
| 218 | * Return: the MTD device embedded in @nand. |
| 219 | */ |
| 220 | static inline struct mtd_info *nanddev_to_mtd(struct nand_device *nand) |
| 221 | { |
| 222 | return nand->mtd; |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | * nanddev_bits_per_cell() - Get the number of bits per cell |
| 227 | * @nand: NAND device |
| 228 | * |
| 229 | * Return: the number of bits per cell. |
| 230 | */ |
| 231 | static inline unsigned int nanddev_bits_per_cell(const struct nand_device *nand) |
| 232 | { |
| 233 | return nand->memorg.bits_per_cell; |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * nanddev_page_size() - Get NAND page size |
| 238 | * @nand: NAND device |
| 239 | * |
| 240 | * Return: the page size. |
| 241 | */ |
| 242 | static inline size_t nanddev_page_size(const struct nand_device *nand) |
| 243 | { |
| 244 | return nand->memorg.pagesize; |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * nanddev_per_page_oobsize() - Get NAND OOB size |
| 249 | * @nand: NAND device |
| 250 | * |
| 251 | * Return: the OOB size. |
| 252 | */ |
| 253 | static inline unsigned int |
| 254 | nanddev_per_page_oobsize(const struct nand_device *nand) |
| 255 | { |
| 256 | return nand->memorg.oobsize; |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * nanddev_pages_per_eraseblock() - Get the number of pages per eraseblock |
| 261 | * @nand: NAND device |
| 262 | * |
| 263 | * Return: the number of pages per eraseblock. |
| 264 | */ |
| 265 | static inline unsigned int |
| 266 | nanddev_pages_per_eraseblock(const struct nand_device *nand) |
| 267 | { |
| 268 | return nand->memorg.pages_per_eraseblock; |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * nanddev_per_page_oobsize() - Get NAND erase block size |
| 273 | * @nand: NAND device |
| 274 | * |
| 275 | * Return: the eraseblock size. |
| 276 | */ |
| 277 | static inline size_t nanddev_eraseblock_size(const struct nand_device *nand) |
| 278 | { |
| 279 | return nand->memorg.pagesize * nand->memorg.pages_per_eraseblock; |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * nanddev_eraseblocks_per_lun() - Get the number of eraseblocks per LUN |
| 284 | * @nand: NAND device |
| 285 | * |
| 286 | * Return: the number of eraseblocks per LUN. |
| 287 | */ |
| 288 | static inline unsigned int |
| 289 | nanddev_eraseblocks_per_lun(const struct nand_device *nand) |
| 290 | { |
| 291 | return nand->memorg.eraseblocks_per_lun; |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * nanddev_target_size() - Get the total size provided by a single target/die |
| 296 | * @nand: NAND device |
| 297 | * |
| 298 | * Return: the total size exposed by a single target/die in bytes. |
| 299 | */ |
| 300 | static inline u64 nanddev_target_size(const struct nand_device *nand) |
| 301 | { |
| 302 | return (u64)nand->memorg.luns_per_target * |
| 303 | nand->memorg.eraseblocks_per_lun * |
| 304 | nand->memorg.pages_per_eraseblock * |
| 305 | nand->memorg.pagesize; |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * nanddev_ntarget() - Get the total of targets |
| 310 | * @nand: NAND device |
| 311 | * |
| 312 | * Return: the number of targets/dies exposed by @nand. |
| 313 | */ |
| 314 | static inline unsigned int nanddev_ntargets(const struct nand_device *nand) |
| 315 | { |
| 316 | return nand->memorg.ntargets; |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * nanddev_neraseblocks() - Get the total number of erasablocks |
| 321 | * @nand: NAND device |
| 322 | * |
| 323 | * Return: the total number of eraseblocks exposed by @nand. |
| 324 | */ |
| 325 | static inline unsigned int nanddev_neraseblocks(const struct nand_device *nand) |
| 326 | { |
| 327 | return (u64)nand->memorg.luns_per_target * |
| 328 | nand->memorg.eraseblocks_per_lun * |
| 329 | nand->memorg.pages_per_eraseblock; |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * nanddev_size() - Get NAND size |
| 334 | * @nand: NAND device |
| 335 | * |
| 336 | * Return: the total size (in bytes) exposed by @nand. |
| 337 | */ |
| 338 | static inline u64 nanddev_size(const struct nand_device *nand) |
| 339 | { |
| 340 | return nanddev_target_size(nand) * nanddev_ntargets(nand); |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * nanddev_get_memorg() - Extract memory organization info from a NAND device |
| 345 | * @nand: NAND device |
| 346 | * |
| 347 | * This can be used by the upper layer to fill the memorg info before calling |
| 348 | * nanddev_init(). |
| 349 | * |
| 350 | * Return: the memorg object embedded in the NAND device. |
| 351 | */ |
| 352 | static inline struct nand_memory_organization * |
| 353 | nanddev_get_memorg(struct nand_device *nand) |
| 354 | { |
| 355 | return &nand->memorg; |
| 356 | } |
| 357 | |
| 358 | int nanddev_init(struct nand_device *nand, const struct nand_ops *ops, |
| 359 | struct module *owner); |
| 360 | void nanddev_cleanup(struct nand_device *nand); |
| 361 | |
| 362 | /** |
| 363 | * nanddev_register() - Register a NAND device |
| 364 | * @nand: NAND device |
| 365 | * |
| 366 | * Register a NAND device. |
| 367 | * This function is just a wrapper around mtd_device_register() |
| 368 | * registering the MTD device embedded in @nand. |
| 369 | * |
| 370 | * Return: 0 in case of success, a negative error code otherwise. |
| 371 | */ |
| 372 | static inline int nanddev_register(struct nand_device *nand) |
| 373 | { |
| 374 | return mtd_device_register(nand->mtd, NULL, 0); |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * nanddev_unregister() - Unregister a NAND device |
| 379 | * @nand: NAND device |
| 380 | * |
| 381 | * Unregister a NAND device. |
| 382 | * This function is just a wrapper around mtd_device_unregister() |
| 383 | * unregistering the MTD device embedded in @nand. |
| 384 | * |
| 385 | * Return: 0 in case of success, a negative error code otherwise. |
| 386 | */ |
| 387 | static inline int nanddev_unregister(struct nand_device *nand) |
| 388 | { |
| 389 | return mtd_device_unregister(nand->mtd); |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * nanddev_set_of_node() - Attach a DT node to a NAND device |
| 394 | * @nand: NAND device |
| 395 | * @np: DT node |
| 396 | * |
| 397 | * Attach a DT node to a NAND device. |
| 398 | */ |
| 399 | static inline void nanddev_set_of_node(struct nand_device *nand, |
| 400 | const struct device_node *np) |
| 401 | { |
| 402 | mtd_set_of_node(nand->mtd, np); |
| 403 | } |
| 404 | |
| 405 | /** |
| 406 | * nanddev_get_of_node() - Retrieve the DT node attached to a NAND device |
| 407 | * @nand: NAND device |
| 408 | * |
| 409 | * Return: the DT node attached to @nand. |
| 410 | */ |
| 411 | static inline const struct device_node *nanddev_get_of_node(struct nand_device *nand) |
| 412 | { |
| 413 | return mtd_get_of_node(nand->mtd); |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * nanddev_offs_to_pos() - Convert an absolute NAND offset into a NAND position |
| 418 | * @nand: NAND device |
| 419 | * @offs: absolute NAND offset (usually passed by the MTD layer) |
| 420 | * @pos: a NAND position object to fill in |
| 421 | * |
| 422 | * Converts @offs into a nand_pos representation. |
| 423 | * |
| 424 | * Return: the offset within the NAND page pointed by @pos. |
| 425 | */ |
| 426 | static inline unsigned int nanddev_offs_to_pos(struct nand_device *nand, |
| 427 | loff_t offs, |
| 428 | struct nand_pos *pos) |
| 429 | { |
| 430 | unsigned int pageoffs; |
| 431 | u64 tmp = offs; |
| 432 | |
| 433 | pageoffs = do_div(tmp, nand->memorg.pagesize); |
| 434 | pos->page = do_div(tmp, nand->memorg.pages_per_eraseblock); |
| 435 | pos->eraseblock = do_div(tmp, nand->memorg.eraseblocks_per_lun); |
| 436 | pos->plane = pos->eraseblock % nand->memorg.planes_per_lun; |
| 437 | pos->lun = do_div(tmp, nand->memorg.luns_per_target); |
| 438 | pos->target = tmp; |
| 439 | |
| 440 | return pageoffs; |
| 441 | } |
| 442 | |
| 443 | /** |
| 444 | * nanddev_pos_cmp() - Compare two NAND positions |
| 445 | * @a: First NAND position |
| 446 | * @b: Second NAND position |
| 447 | * |
| 448 | * Compares two NAND positions. |
| 449 | * |
| 450 | * Return: -1 if @a < @b, 0 if @a == @b and 1 if @a > @b. |
| 451 | */ |
| 452 | static inline int nanddev_pos_cmp(const struct nand_pos *a, |
| 453 | const struct nand_pos *b) |
| 454 | { |
| 455 | if (a->target != b->target) |
| 456 | return a->target < b->target ? -1 : 1; |
| 457 | |
| 458 | if (a->lun != b->lun) |
| 459 | return a->lun < b->lun ? -1 : 1; |
| 460 | |
| 461 | if (a->eraseblock != b->eraseblock) |
| 462 | return a->eraseblock < b->eraseblock ? -1 : 1; |
| 463 | |
| 464 | if (a->page != b->page) |
| 465 | return a->page < b->page ? -1 : 1; |
| 466 | |
| 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * nanddev_pos_to_offs() - Convert a NAND position into an absolute offset |
| 472 | * @nand: NAND device |
| 473 | * @pos: the NAND position to convert |
| 474 | * |
| 475 | * Converts @pos NAND position into an absolute offset. |
| 476 | * |
| 477 | * Return: the absolute offset. Note that @pos points to the beginning of a |
| 478 | * page, if one wants to point to a specific offset within this page |
| 479 | * the returned offset has to be adjusted manually. |
| 480 | */ |
| 481 | static inline loff_t nanddev_pos_to_offs(struct nand_device *nand, |
| 482 | const struct nand_pos *pos) |
| 483 | { |
| 484 | unsigned int npages; |
| 485 | |
| 486 | npages = pos->page + |
| 487 | ((pos->eraseblock + |
| 488 | (pos->lun + |
| 489 | (pos->target * nand->memorg.luns_per_target)) * |
| 490 | nand->memorg.eraseblocks_per_lun) * |
| 491 | nand->memorg.pages_per_eraseblock); |
| 492 | |
| 493 | return (loff_t)npages * nand->memorg.pagesize; |
| 494 | } |
| 495 | |
| 496 | /** |
| 497 | * nanddev_pos_to_row() - Extract a row address from a NAND position |
| 498 | * @nand: NAND device |
| 499 | * @pos: the position to convert |
| 500 | * |
| 501 | * Converts a NAND position into a row address that can then be passed to the |
| 502 | * device. |
| 503 | * |
| 504 | * Return: the row address extracted from @pos. |
| 505 | */ |
| 506 | static inline unsigned int nanddev_pos_to_row(struct nand_device *nand, |
| 507 | const struct nand_pos *pos) |
| 508 | { |
| 509 | return (pos->lun << nand->rowconv.lun_addr_shift) | |
| 510 | (pos->eraseblock << nand->rowconv.eraseblock_addr_shift) | |
| 511 | pos->page; |
| 512 | } |
| 513 | |
| 514 | /** |
| 515 | * nanddev_pos_next_target() - Move a position to the next target/die |
| 516 | * @nand: NAND device |
| 517 | * @pos: the position to update |
| 518 | * |
| 519 | * Updates @pos to point to the start of the next target/die. Useful when you |
| 520 | * want to iterate over all targets/dies of a NAND device. |
| 521 | */ |
| 522 | static inline void nanddev_pos_next_target(struct nand_device *nand, |
| 523 | struct nand_pos *pos) |
| 524 | { |
| 525 | pos->page = 0; |
| 526 | pos->plane = 0; |
| 527 | pos->eraseblock = 0; |
| 528 | pos->lun = 0; |
| 529 | pos->target++; |
| 530 | } |
| 531 | |
| 532 | /** |
| 533 | * nanddev_pos_next_lun() - Move a position to the next LUN |
| 534 | * @nand: NAND device |
| 535 | * @pos: the position to update |
| 536 | * |
| 537 | * Updates @pos to point to the start of the next LUN. Useful when you want to |
| 538 | * iterate over all LUNs of a NAND device. |
| 539 | */ |
| 540 | static inline void nanddev_pos_next_lun(struct nand_device *nand, |
| 541 | struct nand_pos *pos) |
| 542 | { |
| 543 | if (pos->lun >= nand->memorg.luns_per_target - 1) |
| 544 | return nanddev_pos_next_target(nand, pos); |
| 545 | |
| 546 | pos->lun++; |
| 547 | pos->page = 0; |
| 548 | pos->plane = 0; |
| 549 | pos->eraseblock = 0; |
| 550 | } |
| 551 | |
| 552 | /** |
| 553 | * nanddev_pos_next_eraseblock() - Move a position to the next eraseblock |
| 554 | * @nand: NAND device |
| 555 | * @pos: the position to update |
| 556 | * |
| 557 | * Updates @pos to point to the start of the next eraseblock. Useful when you |
| 558 | * want to iterate over all eraseblocks of a NAND device. |
| 559 | */ |
| 560 | static inline void nanddev_pos_next_eraseblock(struct nand_device *nand, |
| 561 | struct nand_pos *pos) |
| 562 | { |
| 563 | if (pos->eraseblock >= nand->memorg.eraseblocks_per_lun - 1) |
| 564 | return nanddev_pos_next_lun(nand, pos); |
| 565 | |
| 566 | pos->eraseblock++; |
| 567 | pos->page = 0; |
| 568 | pos->plane = pos->eraseblock % nand->memorg.planes_per_lun; |
| 569 | } |
| 570 | |
| 571 | /** |
| 572 | * nanddev_pos_next_eraseblock() - Move a position to the next page |
| 573 | * @nand: NAND device |
| 574 | * @pos: the position to update |
| 575 | * |
| 576 | * Updates @pos to point to the start of the next page. Useful when you want to |
| 577 | * iterate over all pages of a NAND device. |
| 578 | */ |
| 579 | static inline void nanddev_pos_next_page(struct nand_device *nand, |
| 580 | struct nand_pos *pos) |
| 581 | { |
| 582 | if (pos->page >= nand->memorg.pages_per_eraseblock - 1) |
| 583 | return nanddev_pos_next_eraseblock(nand, pos); |
| 584 | |
| 585 | pos->page++; |
| 586 | } |
| 587 | |
| 588 | /** |
| 589 | * nand_io_iter_init - Initialize a NAND I/O iterator |
| 590 | * @nand: NAND device |
| 591 | * @offs: absolute offset |
| 592 | * @req: MTD request |
| 593 | * @iter: NAND I/O iterator |
| 594 | * |
| 595 | * Initializes a NAND iterator based on the information passed by the MTD |
| 596 | * layer. |
| 597 | */ |
| 598 | static inline void nanddev_io_iter_init(struct nand_device *nand, |
| 599 | loff_t offs, struct mtd_oob_ops *req, |
| 600 | struct nand_io_iter *iter) |
| 601 | { |
| 602 | struct mtd_info *mtd = nanddev_to_mtd(nand); |
| 603 | |
Boris Brezillon | f867872 | 2018-08-16 17:30:10 +0200 | [diff] [blame] | 604 | iter->req.mode = req->mode; |
Boris Brezillon | b95db8d | 2018-08-16 17:30:09 +0200 | [diff] [blame] | 605 | iter->req.dataoffs = nanddev_offs_to_pos(nand, offs, &iter->req.pos); |
| 606 | iter->req.ooboffs = req->ooboffs; |
| 607 | iter->oobbytes_per_page = mtd_oobavail(mtd, req); |
| 608 | iter->dataleft = req->len; |
| 609 | iter->oobleft = req->ooblen; |
| 610 | iter->req.databuf.in = req->datbuf; |
| 611 | iter->req.datalen = min_t(unsigned int, |
| 612 | nand->memorg.pagesize - iter->req.dataoffs, |
| 613 | iter->dataleft); |
| 614 | iter->req.oobbuf.in = req->oobbuf; |
| 615 | iter->req.ooblen = min_t(unsigned int, |
| 616 | iter->oobbytes_per_page - iter->req.ooboffs, |
| 617 | iter->oobleft); |
| 618 | } |
| 619 | |
| 620 | /** |
| 621 | * nand_io_iter_next_page - Move to the next page |
| 622 | * @nand: NAND device |
| 623 | * @iter: NAND I/O iterator |
| 624 | * |
| 625 | * Updates the @iter to point to the next page. |
| 626 | */ |
| 627 | static inline void nanddev_io_iter_next_page(struct nand_device *nand, |
| 628 | struct nand_io_iter *iter) |
| 629 | { |
| 630 | nanddev_pos_next_page(nand, &iter->req.pos); |
| 631 | iter->dataleft -= iter->req.datalen; |
| 632 | iter->req.databuf.in += iter->req.datalen; |
| 633 | iter->oobleft -= iter->req.ooblen; |
| 634 | iter->req.oobbuf.in += iter->req.ooblen; |
| 635 | iter->req.dataoffs = 0; |
| 636 | iter->req.ooboffs = 0; |
| 637 | iter->req.datalen = min_t(unsigned int, nand->memorg.pagesize, |
| 638 | iter->dataleft); |
| 639 | iter->req.ooblen = min_t(unsigned int, iter->oobbytes_per_page, |
| 640 | iter->oobleft); |
| 641 | } |
| 642 | |
| 643 | /** |
| 644 | * nand_io_iter_end - Should end iteration or not |
| 645 | * @nand: NAND device |
| 646 | * @iter: NAND I/O iterator |
| 647 | * |
| 648 | * Check whether @iter has reached the end of the NAND portion it was asked to |
| 649 | * iterate on or not. |
| 650 | * |
| 651 | * Return: true if @iter has reached the end of the iteration request, false |
| 652 | * otherwise. |
| 653 | */ |
| 654 | static inline bool nanddev_io_iter_end(struct nand_device *nand, |
| 655 | const struct nand_io_iter *iter) |
| 656 | { |
| 657 | if (iter->dataleft || iter->oobleft) |
| 658 | return false; |
| 659 | |
| 660 | return true; |
| 661 | } |
| 662 | |
| 663 | /** |
| 664 | * nand_io_for_each_page - Iterate over all NAND pages contained in an MTD I/O |
| 665 | * request |
| 666 | * @nand: NAND device |
| 667 | * @start: start address to read/write from |
| 668 | * @req: MTD I/O request |
| 669 | * @iter: NAND I/O iterator |
| 670 | * |
| 671 | * Should be used for iterate over pages that are contained in an MTD request. |
| 672 | */ |
| 673 | #define nanddev_io_for_each_page(nand, start, req, iter) \ |
| 674 | for (nanddev_io_iter_init(nand, start, req, iter); \ |
| 675 | !nanddev_io_iter_end(nand, iter); \ |
| 676 | nanddev_io_iter_next_page(nand, iter)) |
| 677 | |
| 678 | bool nanddev_isbad(struct nand_device *nand, const struct nand_pos *pos); |
| 679 | bool nanddev_isreserved(struct nand_device *nand, const struct nand_pos *pos); |
| 680 | int nanddev_erase(struct nand_device *nand, const struct nand_pos *pos); |
| 681 | int nanddev_markbad(struct nand_device *nand, const struct nand_pos *pos); |
| 682 | |
| 683 | /* BBT related functions */ |
| 684 | enum nand_bbt_block_status { |
| 685 | NAND_BBT_BLOCK_STATUS_UNKNOWN, |
| 686 | NAND_BBT_BLOCK_GOOD, |
| 687 | NAND_BBT_BLOCK_WORN, |
| 688 | NAND_BBT_BLOCK_RESERVED, |
| 689 | NAND_BBT_BLOCK_FACTORY_BAD, |
| 690 | NAND_BBT_BLOCK_NUM_STATUS, |
| 691 | }; |
| 692 | |
| 693 | int nanddev_bbt_init(struct nand_device *nand); |
| 694 | void nanddev_bbt_cleanup(struct nand_device *nand); |
| 695 | int nanddev_bbt_update(struct nand_device *nand); |
| 696 | int nanddev_bbt_get_block_status(const struct nand_device *nand, |
| 697 | unsigned int entry); |
| 698 | int nanddev_bbt_set_block_status(struct nand_device *nand, unsigned int entry, |
| 699 | enum nand_bbt_block_status status); |
| 700 | int nanddev_bbt_markbad(struct nand_device *nand, unsigned int block); |
| 701 | |
| 702 | /** |
| 703 | * nanddev_bbt_pos_to_entry() - Convert a NAND position into a BBT entry |
| 704 | * @nand: NAND device |
| 705 | * @pos: the NAND position we want to get BBT entry for |
| 706 | * |
| 707 | * Return the BBT entry used to store information about the eraseblock pointed |
| 708 | * by @pos. |
| 709 | * |
| 710 | * Return: the BBT entry storing information about eraseblock pointed by @pos. |
| 711 | */ |
| 712 | static inline unsigned int nanddev_bbt_pos_to_entry(struct nand_device *nand, |
| 713 | const struct nand_pos *pos) |
| 714 | { |
| 715 | return pos->eraseblock + |
| 716 | ((pos->lun + (pos->target * nand->memorg.luns_per_target)) * |
| 717 | nand->memorg.eraseblocks_per_lun); |
| 718 | } |
| 719 | |
| 720 | /** |
| 721 | * nanddev_bbt_is_initialized() - Check if the BBT has been initialized |
| 722 | * @nand: NAND device |
| 723 | * |
| 724 | * Return: true if the BBT has been initialized, false otherwise. |
| 725 | */ |
| 726 | static inline bool nanddev_bbt_is_initialized(struct nand_device *nand) |
| 727 | { |
| 728 | return !!nand->bbt.cache; |
| 729 | } |
| 730 | |
| 731 | /* MTD -> NAND helper functions. */ |
| 732 | int nanddev_mtd_erase(struct mtd_info *mtd, struct erase_info *einfo); |
| 733 | |
| 734 | #endif /* __LINUX_MTD_NAND_H */ |