| 1 | |
| 2 | |
| 3 | * Linux driver for SSFDC Flash Translation Layer (Read only)
|
| 4 | * © 2005 Eptar srl
|
| 5 | * Author: Claudio Lanconelli <[email protected]>
|
| 6 | *
|
| 7 | * Based on NTFL and MTDBLOCK_RO drivers
|
| 8 |
|
| 9 |
|
| 10 | #include <linux/kernel.h>
|
| 11 | #include <linux/module.h>
|
| 12 | #include <linux/init.h>
|
| 13 | #include <linux/slab.h>
|
| 14 | #include <linux/hdreg.h>
|
| 15 | #include <linux/mtd/mtd.h>
|
| 16 | #include <linux/mtd/rawnand.h>
|
| 17 | #include <linux/mtd/blktrans.h>
|
| 18 |
|
| 19 | struct ssfdcr_record {
|
| 20 | struct mtd_blktrans_dev mbd;
|
| 21 | unsigned char heads;
|
| 22 | unsigned char sectors;
|
| 23 | unsigned short cylinders;
|
| 24 | int cis_block;
|
| 25 | int erase_size;
|
| 26 | unsigned short *logic_block_map; |
| 27 | the 128MiB)
|
| 28 | int map_len;
|
| 29 | };
|
| 30 |
|
| 31 | #define SSFDCR_MAJOR 257
|
| 32 | #define SSFDCR_PARTN_BITS 3
|
| 33 |
|
| 34 | #define SECTOR_SIZE 512
|
| 35 | #define SECTOR_SHIFT 9
|
| 36 | #define OOB_SIZE 16
|
| 37 |
|
| 38 | #define MAX_LOGIC_BLK_PER_ZONE 1000
|
| 39 | #define MAX_PHYS_BLK_PER_ZONE 1024
|
| 40 |
|
| 41 | #define KiB(x) ( (x) * 1024L )
|
| 42 | #define MiB(x) ( KiB(x) * 1024L )
|
| 43 |
|
| 44 | |
| 45 | 1MiB 2MiB 4MiB 8MiB 16MiB 32MiB 64MiB 128MiB
|
| 46 | NCylinder 125 125 250 250 500 500 500 500
|
| 47 | NHead 4 4 4 4 4 8 8 16
|
| 48 | NSector 4 8 8 16 16 16 32 32
|
| 49 | SumSector 2,000 4,000 8,000 16,000 32,000 64,000 128,000 256,000
|
| 50 | SectorSize 512 512 512 512 512 512 512 512
|
| 51 | *
|
| 52 |
|
| 53 | typedef struct {
|
| 54 | unsigned long size;
|
| 55 | unsigned short cyl;
|
| 56 | unsigned char head;
|
| 57 | unsigned char sec;
|
| 58 | } chs_entry_t;
|
| 59 |
|
| 60 |
|
| 61 | static const chs_entry_t chs_table[] = {
|
| 62 | { MiB( 1), 125, 4, 4 },
|
| 63 | { MiB( 2), 125, 4, 8 },
|
| 64 | { MiB( 4), 250, 4, 8 },
|
| 65 | { MiB( 8), 250, 4, 16 },
|
| 66 | { MiB( 16), 500, 4, 16 },
|
| 67 | { MiB( 32), 500, 8, 16 },
|
| 68 | { MiB( 64), 500, 8, 32 },
|
| 69 | { MiB(128), 500, 16, 32 },
|
| 70 | { 0 },
|
| 71 | };
|
| 72 |
|
| 73 | static int get_chs(unsigned long size, unsigned short *cyl, unsigned char *head,
|
| 74 | unsigned char *sec)
|
| 75 | {
|
| 76 | int k;
|
| 77 | int found = 0;
|
| 78 |
|
| 79 | k = 0;
|
| 80 | while (chs_table[k].size > 0 && size > chs_table[k].size)
|
| 81 | k++;
|
| 82 |
|
| 83 | if (chs_table[k].size > 0) {
|
| 84 | if (cyl)
|
| 85 | *cyl = chs_table[k].cyl;
|
| 86 | if (head)
|
| 87 | *head = chs_table[k].head;
|
| 88 | if (sec)
|
| 89 | *sec = chs_table[k].sec;
|
| 90 | found = 1;
|
| 91 | }
|
| 92 |
|
| 93 | return found;
|
| 94 | }
|
| 95 |
|
| 96 |
|
| 97 | static const uint8_t cis_numbers[] = {
|
| 98 | 0x01, 0x03, 0xD9, 0x01, 0xFF, 0x18, 0x02, 0xDF, 0x01, 0x20
|
| 99 | };
|
| 100 |
|
| 101 |
|
| 102 | static int get_valid_cis_sector(struct mtd_info *mtd)
|
| 103 | {
|
| 104 | int ret, k, cis_sector;
|
| 105 | size_t retlen;
|
| 106 | loff_t offset;
|
| 107 | uint8_t *sect_buf;
|
| 108 |
|
| 109 | cis_sector = -1;
|
| 110 |
|
| 111 | sect_buf = kmalloc(SECTOR_SIZE, GFP_KERNEL);
|
| 112 | if (!sect_buf)
|
| 113 | goto out;
|
| 114 |
|
| 115 | |
| 116 | * Look for CIS/IDI sector on the first GOOD block (give up after 4 bad
|
| 117 | * blocks). If the first good block doesn't contain CIS number the flash
|
| 118 | * is not SSFDC formatted
|
| 119 |
|
| 120 | for (k = 0, offset = 0; k < 4; k++, offset += mtd->erasesize) {
|
| 121 | if (mtd_block_isbad(mtd, offset)) {
|
| 122 | ret = mtd_read(mtd, offset, SECTOR_SIZE, &retlen,
|
| 123 | sect_buf);
|
| 124 |
|
| 125 |
|
| 126 | if (ret < 0 || retlen != SECTOR_SIZE) {
|
| 127 | printk(KERN_WARNING
|
| 128 | "SSFDC_RO:can't read CIS/IDI sector\n");
|
| 129 | } else if (!memcmp(sect_buf, cis_numbers,
|
| 130 | sizeof(cis_numbers))) {
|
| 131 |
|
| 132 | cis_sector = (int)(offset >> SECTOR_SHIFT);
|
| 133 | } else {
|
| 134 | pr_debug("SSFDC_RO: CIS/IDI sector not found"
|
| 135 | " on %s (mtd%d)\n", mtd->name,
|
| 136 | mtd->index);
|
| 137 | }
|
| 138 | break;
|
| 139 | }
|
| 140 | }
|
| 141 |
|
| 142 | kfree(sect_buf);
|
| 143 | out:
|
| 144 | return cis_sector;
|
| 145 | }
|
| 146 |
|
| 147 |
|
| 148 | static int read_physical_sector(struct mtd_info *mtd, uint8_t *sect_buf,
|
| 149 | int sect_no)
|
| 150 | {
|
| 151 | int ret;
|
| 152 | size_t retlen;
|
| 153 | loff_t offset = (loff_t)sect_no << SECTOR_SHIFT;
|
| 154 |
|
| 155 | ret = mtd_read(mtd, offset, SECTOR_SIZE, &retlen, sect_buf);
|
| 156 | if (ret < 0 || retlen != SECTOR_SIZE)
|
| 157 | return -1;
|
| 158 |
|
| 159 | return 0;
|
| 160 | }
|
| 161 |
|
| 162 |
|
| 163 | static int read_raw_oob(struct mtd_info *mtd, loff_t offs, uint8_t *buf)
|
| 164 | {
|
| 165 | struct mtd_oob_ops ops = { };
|
| 166 | int ret;
|
| 167 |
|
| 168 | ops.mode = MTD_OPS_RAW;
|
| 169 | ops.ooboffs = 0;
|
| 170 | ops.ooblen = OOB_SIZE;
|
| 171 | ops.oobbuf = buf;
|
| 172 | ops.datbuf = NULL;
|
| 173 |
|
| 174 | ret = mtd_read_oob(mtd, offs, &ops);
|
| 175 | if (ret < 0 || ops.oobretlen != OOB_SIZE)
|
| 176 | return -1;
|
| 177 |
|
| 178 | return 0;
|
| 179 | }
|
| 180 |
|
| 181 |
|
| 182 | static int get_parity(int number, int size)
|
| 183 | {
|
| 184 | int k;
|
| 185 | int parity;
|
| 186 |
|
| 187 | parity = 1;
|
| 188 | for (k = 0; k < size; k++) {
|
| 189 | parity += (number >> k);
|
| 190 | parity &= 1;
|
| 191 | }
|
| 192 | return parity;
|
| 193 | }
|
| 194 |
|
| 195 |
|
| 196 | static int get_logical_address(uint8_t *oob_buf)
|
| 197 | {
|
| 198 | int block_address, parity;
|
| 199 | int offset[2] = {6, 11};
|
| 200 | int j;
|
| 201 | int ok = 0;
|
| 202 |
|
| 203 | |
| 204 | * Look for the first valid logical address
|
| 205 | * Valid address has fixed pattern on most significant bits and
|
| 206 | * parity check
|
| 207 |
|
| 208 | for (j = 0; j < ARRAY_SIZE(offset); j++) {
|
| 209 | block_address = ((int)oob_buf[offset[j]] << 8) |
|
| 210 | oob_buf[offset[j]+1];
|
| 211 |
|
| 212 |
|
| 213 | if ((block_address & ~0x7FF) == 0x1000) {
|
| 214 | parity = block_address & 0x01;
|
| 215 | block_address &= 0x7FF;
|
| 216 | block_address >>= 1;
|
| 217 |
|
| 218 | if (get_parity(block_address, 10) != parity) {
|
| 219 | pr_debug("SSFDC_RO: logical address field%d"
|
| 220 | "parity error(0x%04X)\n", j+1,
|
| 221 | block_address);
|
| 222 | } else {
|
| 223 | ok = 1;
|
| 224 | break;
|
| 225 | }
|
| 226 | }
|
| 227 | }
|
| 228 |
|
| 229 | if (!ok)
|
| 230 | block_address = -2;
|
| 231 |
|
| 232 | pr_debug("SSFDC_RO: get_logical_address() %d\n",
|
| 233 | block_address);
|
| 234 |
|
| 235 | return block_address;
|
| 236 | }
|
| 237 |
|
| 238 |
|
| 239 | static int build_logical_block_map(struct ssfdcr_record *ssfdc)
|
| 240 | {
|
| 241 | unsigned long offset;
|
| 242 | uint8_t oob_buf[OOB_SIZE];
|
| 243 | int ret, block_address, phys_block;
|
| 244 | struct mtd_info *mtd = ssfdc->mbd.mtd;
|
| 245 |
|
| 246 | pr_debug("SSFDC_RO: build_block_map() nblks=%d (%luK)\n",
|
| 247 | ssfdc->map_len,
|
| 248 | (unsigned long)ssfdc->map_len * ssfdc->erase_size / 1024);
|
| 249 |
|
| 250 |
|
| 251 | for (phys_block = ssfdc->cis_block + 1; phys_block < ssfdc->map_len;
|
| 252 | phys_block++) {
|
| 253 | offset = (unsigned long)phys_block * ssfdc->erase_size;
|
| 254 | if (mtd_block_isbad(mtd, offset))
|
| 255 | continue;
|
| 256 |
|
| 257 | ret = read_raw_oob(mtd, offset, oob_buf);
|
| 258 | if (ret < 0) {
|
| 259 | pr_debug("SSFDC_RO: mtd read_oob() failed at %lu\n",
|
| 260 | offset);
|
| 261 | return -1;
|
| 262 | }
|
| 263 | block_address = get_logical_address(oob_buf);
|
| 264 |
|
| 265 |
|
| 266 | if (block_address >= 0 &&
|
| 267 | block_address < MAX_LOGIC_BLK_PER_ZONE) {
|
| 268 | int zone_index;
|
| 269 |
|
| 270 | zone_index = phys_block / MAX_PHYS_BLK_PER_ZONE;
|
| 271 | block_address += zone_index * MAX_LOGIC_BLK_PER_ZONE;
|
| 272 | ssfdc->logic_block_map[block_address] =
|
| 273 | (unsigned short)phys_block;
|
| 274 |
|
| 275 | pr_debug("SSFDC_RO: build_block_map() phys_block=%d,"
|
| 276 | "logic_block_addr=%d, zone=%d\n",
|
| 277 | phys_block, block_address, zone_index);
|
| 278 | }
|
| 279 | }
|
| 280 | return 0;
|
| 281 | }
|
| 282 |
|
| 283 | static void ssfdcr_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
|
| 284 | {
|
| 285 | struct ssfdcr_record *ssfdc;
|
| 286 | int cis_sector;
|
| 287 |
|
| 288 |
|
| 289 | if (!mtd_type_is_nand(mtd) || mtd->oobsize != OOB_SIZE ||
|
| 290 | mtd->size > UINT_MAX)
|
| 291 | return;
|
| 292 |
|
| 293 |
|
| 294 | cis_sector = get_valid_cis_sector(mtd);
|
| 295 | if (cis_sector == -1)
|
| 296 | return;
|
| 297 |
|
| 298 | ssfdc = kzalloc_obj(*ssfdc);
|
| 299 | if (!ssfdc)
|
| 300 | return;
|
| 301 |
|
| 302 | ssfdc->mbd.mtd = mtd;
|
| 303 | ssfdc->mbd.devnum = -1;
|
| 304 | ssfdc->mbd.tr = tr;
|
| 305 | ssfdc->mbd.readonly = 1;
|
| 306 |
|
| 307 | ssfdc->cis_block = cis_sector / (mtd->erasesize >> SECTOR_SHIFT);
|
| 308 | ssfdc->erase_size = mtd->erasesize;
|
| 309 | ssfdc->map_len = (u32)mtd->size / mtd->erasesize;
|
| 310 |
|
| 311 | pr_debug("SSFDC_RO: cis_block=%d,erase_size=%d,map_len=%d,n_zones=%d\n",
|
| 312 | ssfdc->cis_block, ssfdc->erase_size, ssfdc->map_len,
|
| 313 | DIV_ROUND_UP(ssfdc->map_len, MAX_PHYS_BLK_PER_ZONE));
|
| 314 |
|
| 315 |
|
| 316 | ssfdc->heads = 16;
|
| 317 | ssfdc->sectors = 32;
|
| 318 | get_chs(mtd->size, NULL, &ssfdc->heads, &ssfdc->sectors);
|
| 319 | ssfdc->cylinders = (unsigned short)(((u32)mtd->size >> SECTOR_SHIFT) /
|
| 320 | ((long)ssfdc->sectors * (long)ssfdc->heads));
|
| 321 |
|
| 322 | pr_debug("SSFDC_RO: using C:%d H:%d S:%d == %ld sects\n",
|
| 323 | ssfdc->cylinders, ssfdc->heads , ssfdc->sectors,
|
| 324 | (long)ssfdc->cylinders * (long)ssfdc->heads *
|
| 325 | (long)ssfdc->sectors);
|
| 326 |
|
| 327 | ssfdc->mbd.size = (long)ssfdc->heads * (long)ssfdc->cylinders *
|
| 328 | (long)ssfdc->sectors;
|
| 329 |
|
| 330 |
|
| 331 | ssfdc->logic_block_map =
|
| 332 | kmalloc_array(ssfdc->map_len,
|
| 333 | sizeof(ssfdc->logic_block_map[0]), GFP_KERNEL);
|
| 334 | if (!ssfdc->logic_block_map)
|
| 335 | goto out_free_ssfdc;
|
| 336 | memset(ssfdc->logic_block_map, 0xff, sizeof(ssfdc->logic_block_map[0]) *
|
| 337 | ssfdc->map_len);
|
| 338 |
|
| 339 |
|
| 340 | if (build_logical_block_map(ssfdc) < 0)
|
| 341 | goto out_err;
|
| 342 |
|
| 343 |
|
| 344 | if (add_mtd_blktrans_dev(&ssfdc->mbd))
|
| 345 | goto out_err;
|
| 346 |
|
| 347 | printk(KERN_INFO "SSFDC_RO: Found ssfdc%c on mtd%d (%s)\n",
|
| 348 | ssfdc->mbd.devnum + 'a', mtd->index, mtd->name);
|
| 349 | return;
|
| 350 |
|
| 351 | out_err:
|
| 352 | kfree(ssfdc->logic_block_map);
|
| 353 | out_free_ssfdc:
|
| 354 | kfree(ssfdc);
|
| 355 | }
|
| 356 |
|
| 357 | static void ssfdcr_remove_dev(struct mtd_blktrans_dev *dev)
|
| 358 | {
|
| 359 | struct ssfdcr_record *ssfdc = (struct ssfdcr_record *)dev;
|
| 360 |
|
| 361 | pr_debug("SSFDC_RO: remove_dev (i=%d)\n", dev->devnum);
|
| 362 |
|
| 363 | del_mtd_blktrans_dev(dev);
|
| 364 | kfree(ssfdc->logic_block_map);
|
| 365 | }
|
| 366 |
|
| 367 | static int ssfdcr_readsect(struct mtd_blktrans_dev *dev,
|
| 368 | unsigned long logic_sect_no, char *buf)
|
| 369 | {
|
| 370 | struct ssfdcr_record *ssfdc = (struct ssfdcr_record *)dev;
|
| 371 | int sectors_per_block, offset, block_address;
|
| 372 |
|
| 373 | sectors_per_block = ssfdc->erase_size >> SECTOR_SHIFT;
|
| 374 | offset = (int)(logic_sect_no % sectors_per_block);
|
| 375 | block_address = (int)(logic_sect_no / sectors_per_block);
|
| 376 |
|
| 377 | pr_debug("SSFDC_RO: ssfdcr_readsect(%lu) sec_per_blk=%d, ofst=%d,"
|
| 378 | " block_addr=%d\n", logic_sect_no, sectors_per_block, offset,
|
| 379 | block_address);
|
| 380 |
|
| 381 | BUG_ON(block_address >= ssfdc->map_len);
|
| 382 |
|
| 383 | block_address = ssfdc->logic_block_map[block_address];
|
| 384 |
|
| 385 | pr_debug("SSFDC_RO: ssfdcr_readsect() phys_block_addr=%d\n",
|
| 386 | block_address);
|
| 387 |
|
| 388 | if (block_address < 0xffff) {
|
| 389 | unsigned long sect_no;
|
| 390 |
|
| 391 | sect_no = (unsigned long)block_address * sectors_per_block +
|
| 392 | offset;
|
| 393 |
|
| 394 | pr_debug("SSFDC_RO: ssfdcr_readsect() phys_sect_no=%lu\n",
|
| 395 | sect_no);
|
| 396 |
|
| 397 | if (read_physical_sector(ssfdc->mbd.mtd, buf, sect_no) < 0)
|
| 398 | return -EIO;
|
| 399 | } else {
|
| 400 | memset(buf, 0xff, SECTOR_SIZE);
|
| 401 | }
|
| 402 |
|
| 403 | return 0;
|
| 404 | }
|
| 405 |
|
| 406 | static int ssfdcr_getgeo(struct mtd_blktrans_dev *dev, struct hd_geometry *geo)
|
| 407 | {
|
| 408 | struct ssfdcr_record *ssfdc = (struct ssfdcr_record *)dev;
|
| 409 |
|
| 410 | pr_debug("SSFDC_RO: ssfdcr_getgeo() C=%d, H=%d, S=%d\n",
|
| 411 | ssfdc->cylinders, ssfdc->heads, ssfdc->sectors);
|
| 412 |
|
| 413 | geo->heads = ssfdc->heads;
|
| 414 | geo->sectors = ssfdc->sectors;
|
| 415 | geo->cylinders = ssfdc->cylinders;
|
| 416 |
|
| 417 | return 0;
|
| 418 | }
|
| 419 |
|
| 420 | |
| 421 | *
|
| 422 | * Module stuff
|
| 423 | *
|
| 424 | ***************************************************************************
|
| 425 |
|
| 426 | static struct mtd_blktrans_ops ssfdcr_tr = {
|
| 427 | .name = "ssfdc",
|
| 428 | .major = SSFDCR_MAJOR,
|
| 429 | .part_bits = SSFDCR_PARTN_BITS,
|
| 430 | .blksize = SECTOR_SIZE,
|
| 431 | .getgeo = ssfdcr_getgeo,
|
| 432 | .readsect = ssfdcr_readsect,
|
| 433 | .add_mtd = ssfdcr_add_mtd,
|
| 434 | .remove_dev = ssfdcr_remove_dev,
|
| 435 | .owner = THIS_MODULE,
|
| 436 | };
|
| 437 |
|
| 438 | static int __init init_ssfdcr(void)
|
| 439 | {
|
| 440 | printk(KERN_INFO "SSFDC read-only Flash Translation layer\n");
|
| 441 |
|
| 442 | return register_mtd_blktrans(&ssfdcr_tr);
|
| 443 | }
|
| 444 |
|
| 445 | static void __exit cleanup_ssfdcr(void)
|
| 446 | {
|
| 447 | deregister_mtd_blktrans(&ssfdcr_tr);
|
| 448 | }
|
| 449 |
|
| 450 | module_init(init_ssfdcr);
|
| 451 | module_exit(cleanup_ssfdcr);
|
| 452 |
|
| 453 | MODULE_LICENSE("GPL");
|
| 454 | MODULE_AUTHOR("Claudio Lanconelli <[email protected]>");
|
| 455 | MODULE_DESCRIPTION("Flash Translation Layer for read-only SSFDC SmartMedia card");
|