Maxim Levitsky | 9fc51a3 | 2010-02-22 20:39:39 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2009 - Maxim Levitsky |
| 3 | * Common routines & support for xD format |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
| 8 | */ |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/mtd/nand.h> |
Paul Gortmaker | a0e5cc5 | 2011-07-03 15:17:31 -0400 | [diff] [blame] | 11 | #include <linux/module.h> |
Artem Bityutskiy | 0c4a235 | 2013-03-19 10:47:56 +0200 | [diff] [blame] | 12 | #include <linux/sizes.h> |
Maxim Levitsky | 9fc51a3 | 2010-02-22 20:39:39 +0200 | [diff] [blame] | 13 | #include "sm_common.h" |
| 14 | |
| 15 | static struct nand_ecclayout nand_oob_sm = { |
| 16 | .eccbytes = 6, |
| 17 | .eccpos = {8, 9, 10, 13, 14, 15}, |
| 18 | .oobfree = { |
| 19 | {.offset = 0 , .length = 4}, /* reserved */ |
| 20 | {.offset = 6 , .length = 2}, /* LBA1 */ |
| 21 | {.offset = 11, .length = 2} /* LBA2 */ |
| 22 | } |
| 23 | }; |
| 24 | |
| 25 | /* NOTE: This layout is is not compatabable with SmartMedia, */ |
| 26 | /* because the 256 byte devices have page depenent oob layout */ |
| 27 | /* However it does preserve the bad block markers */ |
| 28 | /* If you use smftl, it will bypass this and work correctly */ |
| 29 | /* If you not, then you break SmartMedia compliance anyway */ |
| 30 | |
| 31 | static struct nand_ecclayout nand_oob_sm_small = { |
| 32 | .eccbytes = 3, |
| 33 | .eccpos = {0, 1, 2}, |
| 34 | .oobfree = { |
| 35 | {.offset = 3 , .length = 2}, /* reserved */ |
| 36 | {.offset = 6 , .length = 2}, /* LBA1 */ |
| 37 | } |
| 38 | }; |
| 39 | |
| 40 | |
| 41 | static int sm_block_markbad(struct mtd_info *mtd, loff_t ofs) |
| 42 | { |
| 43 | struct mtd_oob_ops ops; |
| 44 | struct sm_oob oob; |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 45 | int ret; |
Maxim Levitsky | 9fc51a3 | 2010-02-22 20:39:39 +0200 | [diff] [blame] | 46 | |
| 47 | memset(&oob, -1, SM_OOB_SIZE); |
| 48 | oob.block_status = 0x0F; |
| 49 | |
| 50 | /* As long as this function is called on erase block boundaries |
| 51 | it will work correctly for 256 byte nand */ |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 52 | ops.mode = MTD_OPS_PLACE_OOB; |
Maxim Levitsky | 9fc51a3 | 2010-02-22 20:39:39 +0200 | [diff] [blame] | 53 | ops.ooboffs = 0; |
| 54 | ops.ooblen = mtd->oobsize; |
| 55 | ops.oobbuf = (void *)&oob; |
| 56 | ops.datbuf = NULL; |
| 57 | |
| 58 | |
Artem Bityutskiy | a2cc5ba | 2011-12-23 18:29:55 +0200 | [diff] [blame] | 59 | ret = mtd_write_oob(mtd, ofs, &ops); |
Maxim Levitsky | 9fc51a3 | 2010-02-22 20:39:39 +0200 | [diff] [blame] | 60 | if (ret < 0 || ops.oobretlen != SM_OOB_SIZE) { |
| 61 | printk(KERN_NOTICE |
| 62 | "sm_common: can't mark sector at %i as bad\n", |
| 63 | (int)ofs); |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 64 | return -EIO; |
| 65 | } |
Maxim Levitsky | 9fc51a3 | 2010-02-22 20:39:39 +0200 | [diff] [blame] | 66 | |
Brian Norris | 5a0edb2 | 2013-07-30 17:52:58 -0700 | [diff] [blame] | 67 | return 0; |
Maxim Levitsky | 9fc51a3 | 2010-02-22 20:39:39 +0200 | [diff] [blame] | 68 | } |
| 69 | |
David Woodhouse | 2764fb4 | 2010-02-26 18:45:37 +0000 | [diff] [blame] | 70 | static struct nand_flash_dev nand_smartmedia_flash_ids[] = { |
Artem Bityutskiy | 0c4a235 | 2013-03-19 10:47:56 +0200 | [diff] [blame] | 71 | LEGACY_ID_NAND("SmartMedia 2MiB 3,3V ROM", 0x5d, 2, SZ_8K, NAND_ROM), |
| 72 | LEGACY_ID_NAND("SmartMedia 4MiB 3,3V", 0xe3, 4, SZ_8K, 0), |
| 73 | LEGACY_ID_NAND("SmartMedia 4MiB 3,3/5V", 0xe5, 4, SZ_8K, 0), |
| 74 | LEGACY_ID_NAND("SmartMedia 4MiB 5V", 0x6b, 4, SZ_8K, 0), |
| 75 | LEGACY_ID_NAND("SmartMedia 4MiB 3,3V ROM", 0xd5, 4, SZ_8K, NAND_ROM), |
| 76 | LEGACY_ID_NAND("SmartMedia 8MiB 3,3V", 0xe6, 8, SZ_8K, 0), |
| 77 | LEGACY_ID_NAND("SmartMedia 8MiB 3,3V ROM", 0xd6, 8, SZ_8K, NAND_ROM), |
| 78 | LEGACY_ID_NAND("SmartMedia 16MiB 3,3V", 0x73, 16, SZ_16K, 0), |
| 79 | LEGACY_ID_NAND("SmartMedia 16MiB 3,3V ROM", 0x57, 16, SZ_16K, NAND_ROM), |
| 80 | LEGACY_ID_NAND("SmartMedia 32MiB 3,3V", 0x75, 32, SZ_16K, 0), |
| 81 | LEGACY_ID_NAND("SmartMedia 32MiB 3,3V ROM", 0x58, 32, SZ_16K, NAND_ROM), |
| 82 | LEGACY_ID_NAND("SmartMedia 64MiB 3,3V", 0x76, 64, SZ_16K, 0), |
| 83 | LEGACY_ID_NAND("SmartMedia 64MiB 3,3V ROM", 0xd9, 64, SZ_16K, NAND_ROM), |
| 84 | LEGACY_ID_NAND("SmartMedia 128MiB 3,3V", 0x79, 128, SZ_16K, 0), |
| 85 | LEGACY_ID_NAND("SmartMedia 128MiB 3,3V ROM", 0xda, 128, SZ_16K, NAND_ROM), |
| 86 | LEGACY_ID_NAND("SmartMedia 256MiB 3, 3V", 0x71, 256, SZ_16K, 0), |
| 87 | LEGACY_ID_NAND("SmartMedia 256MiB 3,3V ROM", 0x5b, 256, SZ_16K, NAND_ROM), |
Artem Bityutskiy | 8dbfae1 | 2013-03-04 15:39:18 +0200 | [diff] [blame] | 88 | {NULL} |
David Woodhouse | 2764fb4 | 2010-02-26 18:45:37 +0000 | [diff] [blame] | 89 | }; |
| 90 | |
Maxim Levitsky | c361157 | 2010-04-19 18:20:41 +0300 | [diff] [blame] | 91 | static struct nand_flash_dev nand_xd_flash_ids[] = { |
Artem Bityutskiy | 0c4a235 | 2013-03-19 10:47:56 +0200 | [diff] [blame] | 92 | LEGACY_ID_NAND("xD 16MiB 3,3V", 0x73, 16, SZ_16K, 0), |
| 93 | LEGACY_ID_NAND("xD 32MiB 3,3V", 0x75, 32, SZ_16K, 0), |
| 94 | LEGACY_ID_NAND("xD 64MiB 3,3V", 0x76, 64, SZ_16K, 0), |
| 95 | LEGACY_ID_NAND("xD 128MiB 3,3V", 0x79, 128, SZ_16K, 0), |
| 96 | LEGACY_ID_NAND("xD 256MiB 3,3V", 0x71, 256, SZ_16K, NAND_BROKEN_XD), |
| 97 | LEGACY_ID_NAND("xD 512MiB 3,3V", 0xdc, 512, SZ_16K, NAND_BROKEN_XD), |
| 98 | LEGACY_ID_NAND("xD 1GiB 3,3V", 0xd3, 1024, SZ_16K, NAND_BROKEN_XD), |
| 99 | LEGACY_ID_NAND("xD 2GiB 3,3V", 0xd5, 2048, SZ_16K, NAND_BROKEN_XD), |
Artem Bityutskiy | 8dbfae1 | 2013-03-04 15:39:18 +0200 | [diff] [blame] | 100 | {NULL} |
Maxim Levitsky | c361157 | 2010-04-19 18:20:41 +0300 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | int sm_register_device(struct mtd_info *mtd, int smartmedia) |
Maxim Levitsky | 9fc51a3 | 2010-02-22 20:39:39 +0200 | [diff] [blame] | 104 | { |
Kulikov Vasiliy | ece0ac9 | 2010-06-29 14:15:24 +0400 | [diff] [blame] | 105 | struct nand_chip *chip = mtd->priv; |
Maxim Levitsky | 9fc51a3 | 2010-02-22 20:39:39 +0200 | [diff] [blame] | 106 | int ret; |
| 107 | |
David Woodhouse | 2764fb4 | 2010-02-26 18:45:37 +0000 | [diff] [blame] | 108 | chip->options |= NAND_SKIP_BBTSCAN; |
Maxim Levitsky | 9fc51a3 | 2010-02-22 20:39:39 +0200 | [diff] [blame] | 109 | |
| 110 | /* Scan for card properties */ |
Maxim Levitsky | c361157 | 2010-04-19 18:20:41 +0300 | [diff] [blame] | 111 | ret = nand_scan_ident(mtd, 1, smartmedia ? |
| 112 | nand_smartmedia_flash_ids : nand_xd_flash_ids); |
Maxim Levitsky | 9fc51a3 | 2010-02-22 20:39:39 +0200 | [diff] [blame] | 113 | |
| 114 | if (ret) |
| 115 | return ret; |
| 116 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 117 | /* Bad block marker position */ |
Maxim Levitsky | 9fc51a3 | 2010-02-22 20:39:39 +0200 | [diff] [blame] | 118 | chip->badblockpos = 0x05; |
| 119 | chip->badblockbits = 7; |
| 120 | chip->block_markbad = sm_block_markbad; |
| 121 | |
| 122 | /* ECC layout */ |
| 123 | if (mtd->writesize == SM_SECTOR_SIZE) |
| 124 | chip->ecc.layout = &nand_oob_sm; |
| 125 | else if (mtd->writesize == SM_SMALL_PAGE) |
| 126 | chip->ecc.layout = &nand_oob_sm_small; |
| 127 | else |
| 128 | return -ENODEV; |
| 129 | |
| 130 | ret = nand_scan_tail(mtd); |
| 131 | |
| 132 | if (ret) |
| 133 | return ret; |
| 134 | |
Jamie Iles | ee0e87b | 2011-05-23 10:23:40 +0100 | [diff] [blame] | 135 | return mtd_device_register(mtd, NULL, 0); |
Maxim Levitsky | 9fc51a3 | 2010-02-22 20:39:39 +0200 | [diff] [blame] | 136 | } |
| 137 | EXPORT_SYMBOL_GPL(sm_register_device); |
| 138 | |
| 139 | MODULE_LICENSE("GPL"); |
| 140 | MODULE_AUTHOR("Maxim Levitsky <maximlevitsky@gmail.com>"); |
| 141 | MODULE_DESCRIPTION("Common SmartMedia/xD functions"); |