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> |
| 11 | #include "sm_common.h" |
| 12 | |
| 13 | static struct nand_ecclayout nand_oob_sm = { |
| 14 | .eccbytes = 6, |
| 15 | .eccpos = {8, 9, 10, 13, 14, 15}, |
| 16 | .oobfree = { |
| 17 | {.offset = 0 , .length = 4}, /* reserved */ |
| 18 | {.offset = 6 , .length = 2}, /* LBA1 */ |
| 19 | {.offset = 11, .length = 2} /* LBA2 */ |
| 20 | } |
| 21 | }; |
| 22 | |
| 23 | /* NOTE: This layout is is not compatabable with SmartMedia, */ |
| 24 | /* because the 256 byte devices have page depenent oob layout */ |
| 25 | /* However it does preserve the bad block markers */ |
| 26 | /* If you use smftl, it will bypass this and work correctly */ |
| 27 | /* If you not, then you break SmartMedia compliance anyway */ |
| 28 | |
| 29 | static struct nand_ecclayout nand_oob_sm_small = { |
| 30 | .eccbytes = 3, |
| 31 | .eccpos = {0, 1, 2}, |
| 32 | .oobfree = { |
| 33 | {.offset = 3 , .length = 2}, /* reserved */ |
| 34 | {.offset = 6 , .length = 2}, /* LBA1 */ |
| 35 | } |
| 36 | }; |
| 37 | |
| 38 | |
| 39 | static int sm_block_markbad(struct mtd_info *mtd, loff_t ofs) |
| 40 | { |
| 41 | struct mtd_oob_ops ops; |
| 42 | struct sm_oob oob; |
| 43 | int ret, error = 0; |
| 44 | |
| 45 | memset(&oob, -1, SM_OOB_SIZE); |
| 46 | oob.block_status = 0x0F; |
| 47 | |
| 48 | /* As long as this function is called on erase block boundaries |
| 49 | it will work correctly for 256 byte nand */ |
| 50 | ops.mode = MTD_OOB_PLACE; |
| 51 | ops.ooboffs = 0; |
| 52 | ops.ooblen = mtd->oobsize; |
| 53 | ops.oobbuf = (void *)&oob; |
| 54 | ops.datbuf = NULL; |
| 55 | |
| 56 | |
| 57 | ret = mtd->write_oob(mtd, ofs, &ops); |
| 58 | if (ret < 0 || ops.oobretlen != SM_OOB_SIZE) { |
| 59 | printk(KERN_NOTICE |
| 60 | "sm_common: can't mark sector at %i as bad\n", |
| 61 | (int)ofs); |
| 62 | error = -EIO; |
| 63 | } else |
| 64 | mtd->ecc_stats.badblocks++; |
| 65 | |
| 66 | return error; |
| 67 | } |
| 68 | |
| 69 | |
David Woodhouse | 2764fb4 | 2010-02-26 18:45:37 +0000 | [diff] [blame^] | 70 | static struct nand_flash_dev nand_smartmedia_flash_ids[] = { |
| 71 | |
| 72 | /* SmartMedia */ |
| 73 | {"SmartMedia 1MiB 5V", 0x6e, 256, 1, 0x1000, 0}, |
| 74 | {"SmartMedia 1MiB 3,3V", 0xe8, 256, 1, 0x1000, 0}, |
| 75 | {"SmartMedia 1MiB 3,3V", 0xec, 256, 1, 0x1000, 0}, |
| 76 | {"SmartMedia 2MiB 3,3V", 0xea, 256, 2, 0x1000, 0}, |
| 77 | {"SmartMedia 2MiB 5V", 0x64, 256, 2, 0x1000, 0}, |
| 78 | {"SmartMedia 2MiB 3,3V ROM", 0x5d, 512, 2, 0x2000, NAND_ROM}, |
| 79 | {"SmartMedia 4MiB 3,3V", 0xe3, 512, 4, 0x2000, 0}, |
| 80 | {"SmartMedia 4MiB 3,3/5V", 0xe5, 512, 4, 0x2000, 0}, |
| 81 | {"SmartMedia 4MiB 5V", 0x6b, 512, 4, 0x2000, 0}, |
| 82 | {"SmartMedia 4MiB 3,3V ROM", 0xd5, 512, 4, 0x2000, NAND_ROM}, |
| 83 | {"SmartMedia 8MiB 3,3V", 0xe6, 512, 8, 0x2000, 0}, |
| 84 | {"SmartMedia 8MiB 3,3V ROM", 0xd6, 512, 8, 0x2000, NAND_ROM}, |
| 85 | |
| 86 | #define XD_TYPEM (NAND_NO_AUTOINCR | NAND_BROKEN_XD) |
| 87 | /* xD / SmartMedia */ |
| 88 | {"SmartMedia/xD 16MiB 3,3V", 0x73, 512, 16, 0x4000, 0}, |
| 89 | {"SmartMedia 16MiB 3,3V ROM", 0x57, 512, 16, 0x4000, NAND_ROM}, |
| 90 | {"SmartMedia/xD 32MiB 3,3V", 0x75, 512, 32, 0x4000, 0}, |
| 91 | {"SmartMedia 32MiB 3,3V ROM", 0x58, 512, 32, 0x4000, NAND_ROM}, |
| 92 | {"SmartMedia/xD 64MiB 3,3V", 0x76, 512, 64, 0x4000, 0}, |
| 93 | {"SmartMedia 64MiB 3,3V ROM", 0xd9, 512, 64, 0x4000, NAND_ROM}, |
| 94 | {"SmartMedia/xD 128MiB 3,3V", 0x79, 512, 128, 0x4000, 0}, |
| 95 | {"SmartMedia 128MiB 3,3V ROM", 0xda, 512, 128, 0x4000, NAND_ROM}, |
| 96 | {"SmartMedia/xD 256MiB 3,3V", 0x71, 512, 256, 0x4000, XD_TYPEM}, |
| 97 | {"SmartMedia 256MiB 3,3V ROM", 0x5b, 512, 256, 0x4000, NAND_ROM}, |
| 98 | |
| 99 | /* xD only */ |
| 100 | {"xD 512MiB 3,3V", 0xDC, 512, 512, 0x4000, XD_TYPEM}, |
| 101 | {"xD 1GiB 3,3V", 0xD3, 512, 1024, 0x4000, XD_TYPEM}, |
| 102 | {"xD 2GiB 3,3V", 0xD5, 512, 2048, 0x4000, XD_TYPEM}, |
| 103 | {NULL,} |
| 104 | }; |
| 105 | |
Maxim Levitsky | 9fc51a3 | 2010-02-22 20:39:39 +0200 | [diff] [blame] | 106 | int sm_register_device(struct mtd_info *mtd) |
| 107 | { |
| 108 | struct nand_chip *chip = (struct nand_chip *)mtd->priv; |
| 109 | int ret; |
| 110 | |
David Woodhouse | 2764fb4 | 2010-02-26 18:45:37 +0000 | [diff] [blame^] | 111 | chip->options |= NAND_SKIP_BBTSCAN; |
Maxim Levitsky | 9fc51a3 | 2010-02-22 20:39:39 +0200 | [diff] [blame] | 112 | |
| 113 | /* Scan for card properties */ |
David Woodhouse | 2764fb4 | 2010-02-26 18:45:37 +0000 | [diff] [blame^] | 114 | ret = nand_scan_ident(mtd, 1, nand_smartmedia_flash_ids); |
Maxim Levitsky | 9fc51a3 | 2010-02-22 20:39:39 +0200 | [diff] [blame] | 115 | |
| 116 | if (ret) |
| 117 | return ret; |
| 118 | |
| 119 | /* Bad block marker postion */ |
| 120 | chip->badblockpos = 0x05; |
| 121 | chip->badblockbits = 7; |
| 122 | chip->block_markbad = sm_block_markbad; |
| 123 | |
| 124 | /* ECC layout */ |
| 125 | if (mtd->writesize == SM_SECTOR_SIZE) |
| 126 | chip->ecc.layout = &nand_oob_sm; |
| 127 | else if (mtd->writesize == SM_SMALL_PAGE) |
| 128 | chip->ecc.layout = &nand_oob_sm_small; |
| 129 | else |
| 130 | return -ENODEV; |
| 131 | |
| 132 | ret = nand_scan_tail(mtd); |
| 133 | |
| 134 | if (ret) |
| 135 | return ret; |
| 136 | |
| 137 | return add_mtd_device(mtd); |
| 138 | } |
| 139 | EXPORT_SYMBOL_GPL(sm_register_device); |
| 140 | |
| 141 | MODULE_LICENSE("GPL"); |
| 142 | MODULE_AUTHOR("Maxim Levitsky <maximlevitsky@gmail.com>"); |
| 143 | MODULE_DESCRIPTION("Common SmartMedia/xD functions"); |