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