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