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