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> |
Maxim Levitsky | 9fc51a3 | 2010-02-22 20:39:39 +0200 | [diff] [blame] | 12 | #include "sm_common.h" |
| 13 | |
| 14 | static struct nand_ecclayout nand_oob_sm = { |
| 15 | .eccbytes = 6, |
| 16 | .eccpos = {8, 9, 10, 13, 14, 15}, |
| 17 | .oobfree = { |
| 18 | {.offset = 0 , .length = 4}, /* reserved */ |
| 19 | {.offset = 6 , .length = 2}, /* LBA1 */ |
| 20 | {.offset = 11, .length = 2} /* LBA2 */ |
| 21 | } |
| 22 | }; |
| 23 | |
| 24 | /* NOTE: This layout is is not compatabable with SmartMedia, */ |
| 25 | /* because the 256 byte devices have page depenent oob layout */ |
| 26 | /* However it does preserve the bad block markers */ |
| 27 | /* If you use smftl, it will bypass this and work correctly */ |
| 28 | /* If you not, then you break SmartMedia compliance anyway */ |
| 29 | |
| 30 | static struct nand_ecclayout nand_oob_sm_small = { |
| 31 | .eccbytes = 3, |
| 32 | .eccpos = {0, 1, 2}, |
| 33 | .oobfree = { |
| 34 | {.offset = 3 , .length = 2}, /* reserved */ |
| 35 | {.offset = 6 , .length = 2}, /* LBA1 */ |
| 36 | } |
| 37 | }; |
| 38 | |
| 39 | |
| 40 | static int sm_block_markbad(struct mtd_info *mtd, loff_t ofs) |
| 41 | { |
| 42 | struct mtd_oob_ops ops; |
| 43 | struct sm_oob oob; |
| 44 | int ret, error = 0; |
| 45 | |
| 46 | memset(&oob, -1, SM_OOB_SIZE); |
| 47 | oob.block_status = 0x0F; |
| 48 | |
| 49 | /* As long as this function is called on erase block boundaries |
| 50 | it will work correctly for 256 byte nand */ |
Brian Norris | 0612b9d | 2011-08-30 18:45:40 -0700 | [diff] [blame] | 51 | ops.mode = MTD_OPS_PLACE_OOB; |
Maxim Levitsky | 9fc51a3 | 2010-02-22 20:39:39 +0200 | [diff] [blame] | 52 | ops.ooboffs = 0; |
| 53 | ops.ooblen = mtd->oobsize; |
| 54 | ops.oobbuf = (void *)&oob; |
| 55 | ops.datbuf = NULL; |
| 56 | |
| 57 | |
Artem Bityutskiy | a2cc5ba | 2011-12-23 18:29:55 +0200 | [diff] [blame] | 58 | ret = mtd_write_oob(mtd, ofs, &ops); |
Maxim Levitsky | 9fc51a3 | 2010-02-22 20:39:39 +0200 | [diff] [blame] | 59 | if (ret < 0 || ops.oobretlen != SM_OOB_SIZE) { |
| 60 | printk(KERN_NOTICE |
| 61 | "sm_common: can't mark sector at %i as bad\n", |
| 62 | (int)ofs); |
| 63 | error = -EIO; |
| 64 | } else |
| 65 | mtd->ecc_stats.badblocks++; |
| 66 | |
| 67 | return error; |
| 68 | } |
| 69 | |
| 70 | |
David Woodhouse | 2764fb4 | 2010-02-26 18:45:37 +0000 | [diff] [blame] | 71 | static struct nand_flash_dev nand_smartmedia_flash_ids[] = { |
David Woodhouse | 2764fb4 | 2010-02-26 18:45:37 +0000 | [diff] [blame] | 72 | {"SmartMedia 2MiB 3,3V ROM", 0x5d, 512, 2, 0x2000, NAND_ROM}, |
| 73 | {"SmartMedia 4MiB 3,3V", 0xe3, 512, 4, 0x2000, 0}, |
| 74 | {"SmartMedia 4MiB 3,3/5V", 0xe5, 512, 4, 0x2000, 0}, |
| 75 | {"SmartMedia 4MiB 5V", 0x6b, 512, 4, 0x2000, 0}, |
| 76 | {"SmartMedia 4MiB 3,3V ROM", 0xd5, 512, 4, 0x2000, NAND_ROM}, |
| 77 | {"SmartMedia 8MiB 3,3V", 0xe6, 512, 8, 0x2000, 0}, |
| 78 | {"SmartMedia 8MiB 3,3V ROM", 0xd6, 512, 8, 0x2000, NAND_ROM}, |
Maxim Levitsky | c361157 | 2010-04-19 18:20:41 +0300 | [diff] [blame] | 79 | {"SmartMedia 16MiB 3,3V", 0x73, 512, 16, 0x4000, 0}, |
David Woodhouse | 2764fb4 | 2010-02-26 18:45:37 +0000 | [diff] [blame] | 80 | {"SmartMedia 16MiB 3,3V ROM", 0x57, 512, 16, 0x4000, NAND_ROM}, |
Maxim Levitsky | c361157 | 2010-04-19 18:20:41 +0300 | [diff] [blame] | 81 | {"SmartMedia 32MiB 3,3V", 0x75, 512, 32, 0x4000, 0}, |
David Woodhouse | 2764fb4 | 2010-02-26 18:45:37 +0000 | [diff] [blame] | 82 | {"SmartMedia 32MiB 3,3V ROM", 0x58, 512, 32, 0x4000, NAND_ROM}, |
Maxim Levitsky | c361157 | 2010-04-19 18:20:41 +0300 | [diff] [blame] | 83 | {"SmartMedia 64MiB 3,3V", 0x76, 512, 64, 0x4000, 0}, |
David Woodhouse | 2764fb4 | 2010-02-26 18:45:37 +0000 | [diff] [blame] | 84 | {"SmartMedia 64MiB 3,3V ROM", 0xd9, 512, 64, 0x4000, NAND_ROM}, |
Maxim Levitsky | c361157 | 2010-04-19 18:20:41 +0300 | [diff] [blame] | 85 | {"SmartMedia 128MiB 3,3V", 0x79, 512, 128, 0x4000, 0}, |
David Woodhouse | 2764fb4 | 2010-02-26 18:45:37 +0000 | [diff] [blame] | 86 | {"SmartMedia 128MiB 3,3V ROM", 0xda, 512, 128, 0x4000, NAND_ROM}, |
Maxim Levitsky | c361157 | 2010-04-19 18:20:41 +0300 | [diff] [blame] | 87 | {"SmartMedia 256MiB 3,3V", 0x71, 512, 256, 0x4000 }, |
David Woodhouse | 2764fb4 | 2010-02-26 18:45:37 +0000 | [diff] [blame] | 88 | {"SmartMedia 256MiB 3,3V ROM", 0x5b, 512, 256, 0x4000, NAND_ROM}, |
David Woodhouse | 2764fb4 | 2010-02-26 18:45:37 +0000 | [diff] [blame] | 89 | {NULL,} |
| 90 | }; |
| 91 | |
Maxim Levitsky | c361157 | 2010-04-19 18:20:41 +0300 | [diff] [blame] | 92 | static struct nand_flash_dev nand_xd_flash_ids[] = { |
| 93 | |
| 94 | {"xD 16MiB 3,3V", 0x73, 512, 16, 0x4000, 0}, |
| 95 | {"xD 32MiB 3,3V", 0x75, 512, 32, 0x4000, 0}, |
| 96 | {"xD 64MiB 3,3V", 0x76, 512, 64, 0x4000, 0}, |
| 97 | {"xD 128MiB 3,3V", 0x79, 512, 128, 0x4000, 0}, |
Brian Norris | 1826dbc | 2012-05-01 17:12:55 -0700 | [diff] [blame] | 98 | {"xD 256MiB 3,3V", 0x71, 512, 256, 0x4000, NAND_BROKEN_XD}, |
| 99 | {"xD 512MiB 3,3V", 0xdc, 512, 512, 0x4000, NAND_BROKEN_XD}, |
| 100 | {"xD 1GiB 3,3V", 0xd3, 512, 1024, 0x4000, NAND_BROKEN_XD}, |
| 101 | {"xD 2GiB 3,3V", 0xd5, 512, 2048, 0x4000, NAND_BROKEN_XD}, |
Maxim Levitsky | c361157 | 2010-04-19 18:20:41 +0300 | [diff] [blame] | 102 | {NULL,} |
| 103 | }; |
| 104 | |
| 105 | int sm_register_device(struct mtd_info *mtd, int smartmedia) |
Maxim Levitsky | 9fc51a3 | 2010-02-22 20:39:39 +0200 | [diff] [blame] | 106 | { |
Kulikov Vasiliy | ece0ac9 | 2010-06-29 14:15:24 +0400 | [diff] [blame] | 107 | struct nand_chip *chip = mtd->priv; |
Maxim Levitsky | 9fc51a3 | 2010-02-22 20:39:39 +0200 | [diff] [blame] | 108 | int ret; |
| 109 | |
David Woodhouse | 2764fb4 | 2010-02-26 18:45:37 +0000 | [diff] [blame] | 110 | chip->options |= NAND_SKIP_BBTSCAN; |
Maxim Levitsky | 9fc51a3 | 2010-02-22 20:39:39 +0200 | [diff] [blame] | 111 | |
| 112 | /* Scan for card properties */ |
Maxim Levitsky | c361157 | 2010-04-19 18:20:41 +0300 | [diff] [blame] | 113 | ret = nand_scan_ident(mtd, 1, smartmedia ? |
| 114 | nand_smartmedia_flash_ids : nand_xd_flash_ids); |
Maxim Levitsky | 9fc51a3 | 2010-02-22 20:39:39 +0200 | [diff] [blame] | 115 | |
| 116 | if (ret) |
| 117 | return ret; |
| 118 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 119 | /* Bad block marker position */ |
Maxim Levitsky | 9fc51a3 | 2010-02-22 20:39:39 +0200 | [diff] [blame] | 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 | |
Jamie Iles | ee0e87b | 2011-05-23 10:23:40 +0100 | [diff] [blame] | 137 | return mtd_device_register(mtd, NULL, 0); |
Maxim Levitsky | 9fc51a3 | 2010-02-22 20:39:39 +0200 | [diff] [blame] | 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"); |