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