151197abfSClaudio Lanconelli /* 251197abfSClaudio Lanconelli * Linux driver for SSFDC Flash Translation Layer (Read only) 3a1452a37SDavid Woodhouse * © 2005 Eptar srl 451197abfSClaudio Lanconelli * Author: Claudio Lanconelli <lanconelli.claudio@eptar.com> 551197abfSClaudio Lanconelli * 651197abfSClaudio Lanconelli * Based on NTFL and MTDBLOCK_RO drivers 751197abfSClaudio Lanconelli * 851197abfSClaudio Lanconelli * This program is free software; you can redistribute it and/or modify 951197abfSClaudio Lanconelli * it under the terms of the GNU General Public License version 2 as 1051197abfSClaudio Lanconelli * published by the Free Software Foundation. 1151197abfSClaudio Lanconelli */ 1251197abfSClaudio Lanconelli 1351197abfSClaudio Lanconelli #include <linux/kernel.h> 1451197abfSClaudio Lanconelli #include <linux/module.h> 1551197abfSClaudio Lanconelli #include <linux/init.h> 1651197abfSClaudio Lanconelli #include <linux/slab.h> 1751197abfSClaudio Lanconelli #include <linux/hdreg.h> 1851197abfSClaudio Lanconelli #include <linux/mtd/mtd.h> 1951197abfSClaudio Lanconelli #include <linux/mtd/nand.h> 2051197abfSClaudio Lanconelli #include <linux/mtd/blktrans.h> 2151197abfSClaudio Lanconelli 2251197abfSClaudio Lanconelli struct ssfdcr_record { 2351197abfSClaudio Lanconelli struct mtd_blktrans_dev mbd; 2451197abfSClaudio Lanconelli int usecount; 2551197abfSClaudio Lanconelli unsigned char heads; 2651197abfSClaudio Lanconelli unsigned char sectors; 2751197abfSClaudio Lanconelli unsigned short cylinders; 2851197abfSClaudio Lanconelli int cis_block; /* block n. containing CIS/IDI */ 2951197abfSClaudio Lanconelli int erase_size; /* phys_block_size */ 3051197abfSClaudio Lanconelli unsigned short *logic_block_map; /* all zones (max 8192 phys blocks on 319a05ededSDavid Woodhouse the 128MiB) */ 3251197abfSClaudio Lanconelli int map_len; /* n. phys_blocks on the card */ 3351197abfSClaudio Lanconelli }; 3451197abfSClaudio Lanconelli 3551197abfSClaudio Lanconelli #define SSFDCR_MAJOR 257 3651197abfSClaudio Lanconelli #define SSFDCR_PARTN_BITS 3 3751197abfSClaudio Lanconelli 3851197abfSClaudio Lanconelli #define SECTOR_SIZE 512 3951197abfSClaudio Lanconelli #define SECTOR_SHIFT 9 4051197abfSClaudio Lanconelli #define OOB_SIZE 16 4151197abfSClaudio Lanconelli 4251197abfSClaudio Lanconelli #define MAX_LOGIC_BLK_PER_ZONE 1000 4351197abfSClaudio Lanconelli #define MAX_PHYS_BLK_PER_ZONE 1024 4451197abfSClaudio Lanconelli 459a05ededSDavid Woodhouse #define KiB(x) ( (x) * 1024L ) 469a05ededSDavid Woodhouse #define MiB(x) ( KiB(x) * 1024L ) 4751197abfSClaudio Lanconelli 4851197abfSClaudio Lanconelli /** CHS Table 499a05ededSDavid Woodhouse 1MiB 2MiB 4MiB 8MiB 16MiB 32MiB 64MiB 128MiB 5051197abfSClaudio Lanconelli NCylinder 125 125 250 250 500 500 500 500 5151197abfSClaudio Lanconelli NHead 4 4 4 4 4 8 8 16 5251197abfSClaudio Lanconelli NSector 4 8 8 16 16 16 32 32 5351197abfSClaudio Lanconelli SumSector 2,000 4,000 8,000 16,000 32,000 64,000 128,000 256,000 5451197abfSClaudio Lanconelli SectorSize 512 512 512 512 512 512 512 512 5551197abfSClaudio Lanconelli **/ 5651197abfSClaudio Lanconelli 5751197abfSClaudio Lanconelli typedef struct { 5851197abfSClaudio Lanconelli unsigned long size; 5951197abfSClaudio Lanconelli unsigned short cyl; 6051197abfSClaudio Lanconelli unsigned char head; 6151197abfSClaudio Lanconelli unsigned char sec; 6251197abfSClaudio Lanconelli } chs_entry_t; 6351197abfSClaudio Lanconelli 6451197abfSClaudio Lanconelli /* Must be ordered by size */ 6551197abfSClaudio Lanconelli static const chs_entry_t chs_table[] = { 669a05ededSDavid Woodhouse { MiB( 1), 125, 4, 4 }, 679a05ededSDavid Woodhouse { MiB( 2), 125, 4, 8 }, 689a05ededSDavid Woodhouse { MiB( 4), 250, 4, 8 }, 699a05ededSDavid Woodhouse { MiB( 8), 250, 4, 16 }, 709a05ededSDavid Woodhouse { MiB( 16), 500, 4, 16 }, 719a05ededSDavid Woodhouse { MiB( 32), 500, 8, 16 }, 729a05ededSDavid Woodhouse { MiB( 64), 500, 8, 32 }, 739a05ededSDavid Woodhouse { MiB(128), 500, 16, 32 }, 7451197abfSClaudio Lanconelli { 0 }, 7551197abfSClaudio Lanconelli }; 7651197abfSClaudio Lanconelli 7751197abfSClaudio Lanconelli static int get_chs(unsigned long size, unsigned short *cyl, unsigned char *head, 7851197abfSClaudio Lanconelli unsigned char *sec) 7951197abfSClaudio Lanconelli { 8051197abfSClaudio Lanconelli int k; 8151197abfSClaudio Lanconelli int found = 0; 8251197abfSClaudio Lanconelli 8351197abfSClaudio Lanconelli k = 0; 8451197abfSClaudio Lanconelli while (chs_table[k].size > 0 && size > chs_table[k].size) 8551197abfSClaudio Lanconelli k++; 8651197abfSClaudio Lanconelli 8751197abfSClaudio Lanconelli if (chs_table[k].size > 0) { 8851197abfSClaudio Lanconelli if (cyl) 8951197abfSClaudio Lanconelli *cyl = chs_table[k].cyl; 9051197abfSClaudio Lanconelli if (head) 9151197abfSClaudio Lanconelli *head = chs_table[k].head; 9251197abfSClaudio Lanconelli if (sec) 9351197abfSClaudio Lanconelli *sec = chs_table[k].sec; 9451197abfSClaudio Lanconelli found = 1; 9551197abfSClaudio Lanconelli } 9651197abfSClaudio Lanconelli 9751197abfSClaudio Lanconelli return found; 9851197abfSClaudio Lanconelli } 9951197abfSClaudio Lanconelli 10051197abfSClaudio Lanconelli /* These bytes are the signature for the CIS/IDI sector */ 10151197abfSClaudio Lanconelli static const uint8_t cis_numbers[] = { 10251197abfSClaudio Lanconelli 0x01, 0x03, 0xD9, 0x01, 0xFF, 0x18, 0x02, 0xDF, 0x01, 0x20 10351197abfSClaudio Lanconelli }; 10451197abfSClaudio Lanconelli 10551197abfSClaudio Lanconelli /* Read and check for a valid CIS sector */ 10651197abfSClaudio Lanconelli static int get_valid_cis_sector(struct mtd_info *mtd) 10751197abfSClaudio Lanconelli { 10851197abfSClaudio Lanconelli int ret, k, cis_sector; 10951197abfSClaudio Lanconelli size_t retlen; 11051197abfSClaudio Lanconelli loff_t offset; 1119a05ededSDavid Woodhouse uint8_t *sect_buf; 1129a05ededSDavid Woodhouse 1139a05ededSDavid Woodhouse cis_sector = -1; 1149a05ededSDavid Woodhouse 1159a05ededSDavid Woodhouse sect_buf = kmalloc(SECTOR_SIZE, GFP_KERNEL); 1169a05ededSDavid Woodhouse if (!sect_buf) 1179a05ededSDavid Woodhouse goto out; 11851197abfSClaudio Lanconelli 11951197abfSClaudio Lanconelli /* 12051197abfSClaudio Lanconelli * Look for CIS/IDI sector on the first GOOD block (give up after 4 bad 12151197abfSClaudio Lanconelli * blocks). If the first good block doesn't contain CIS number the flash 12251197abfSClaudio Lanconelli * is not SSFDC formatted 12351197abfSClaudio Lanconelli */ 12451197abfSClaudio Lanconelli for (k = 0, offset = 0; k < 4; k++, offset += mtd->erasesize) { 12551197abfSClaudio Lanconelli if (!mtd->block_isbad(mtd, offset)) { 12651197abfSClaudio Lanconelli ret = mtd->read(mtd, offset, SECTOR_SIZE, &retlen, 12751197abfSClaudio Lanconelli sect_buf); 12851197abfSClaudio Lanconelli 12951197abfSClaudio Lanconelli /* CIS pattern match on the sector buffer */ 13051197abfSClaudio Lanconelli if (ret < 0 || retlen != SECTOR_SIZE) { 13151197abfSClaudio Lanconelli printk(KERN_WARNING 13251197abfSClaudio Lanconelli "SSFDC_RO:can't read CIS/IDI sector\n"); 13351197abfSClaudio Lanconelli } else if (!memcmp(sect_buf, cis_numbers, 13451197abfSClaudio Lanconelli sizeof(cis_numbers))) { 13551197abfSClaudio Lanconelli /* Found */ 13651197abfSClaudio Lanconelli cis_sector = (int)(offset >> SECTOR_SHIFT); 13751197abfSClaudio Lanconelli } else { 138*289c0522SBrian Norris pr_debug("SSFDC_RO: CIS/IDI sector not found" 13951197abfSClaudio Lanconelli " on %s (mtd%d)\n", mtd->name, 14051197abfSClaudio Lanconelli mtd->index); 14151197abfSClaudio Lanconelli } 14251197abfSClaudio Lanconelli break; 14351197abfSClaudio Lanconelli } 14451197abfSClaudio Lanconelli } 14551197abfSClaudio Lanconelli 1469a05ededSDavid Woodhouse kfree(sect_buf); 1479a05ededSDavid Woodhouse out: 14851197abfSClaudio Lanconelli return cis_sector; 14951197abfSClaudio Lanconelli } 15051197abfSClaudio Lanconelli 15151197abfSClaudio Lanconelli /* Read physical sector (wrapper to MTD_READ) */ 15251197abfSClaudio Lanconelli static int read_physical_sector(struct mtd_info *mtd, uint8_t *sect_buf, 15351197abfSClaudio Lanconelli int sect_no) 15451197abfSClaudio Lanconelli { 15551197abfSClaudio Lanconelli int ret; 15651197abfSClaudio Lanconelli size_t retlen; 15751197abfSClaudio Lanconelli loff_t offset = (loff_t)sect_no << SECTOR_SHIFT; 15851197abfSClaudio Lanconelli 15951197abfSClaudio Lanconelli ret = mtd->read(mtd, offset, SECTOR_SIZE, &retlen, sect_buf); 16051197abfSClaudio Lanconelli if (ret < 0 || retlen != SECTOR_SIZE) 16151197abfSClaudio Lanconelli return -1; 16251197abfSClaudio Lanconelli 16351197abfSClaudio Lanconelli return 0; 16451197abfSClaudio Lanconelli } 16551197abfSClaudio Lanconelli 16651197abfSClaudio Lanconelli /* Read redundancy area (wrapper to MTD_READ_OOB */ 16751197abfSClaudio Lanconelli static int read_raw_oob(struct mtd_info *mtd, loff_t offs, uint8_t *buf) 16851197abfSClaudio Lanconelli { 16951197abfSClaudio Lanconelli struct mtd_oob_ops ops; 17051197abfSClaudio Lanconelli int ret; 17151197abfSClaudio Lanconelli 17251197abfSClaudio Lanconelli ops.mode = MTD_OOB_RAW; 17351197abfSClaudio Lanconelli ops.ooboffs = 0; 1747014568bSVitaly Wool ops.ooblen = OOB_SIZE; 17551197abfSClaudio Lanconelli ops.oobbuf = buf; 17651197abfSClaudio Lanconelli ops.datbuf = NULL; 17751197abfSClaudio Lanconelli 17851197abfSClaudio Lanconelli ret = mtd->read_oob(mtd, offs, &ops); 1797014568bSVitaly Wool if (ret < 0 || ops.oobretlen != OOB_SIZE) 18051197abfSClaudio Lanconelli return -1; 18151197abfSClaudio Lanconelli 18251197abfSClaudio Lanconelli return 0; 18351197abfSClaudio Lanconelli } 18451197abfSClaudio Lanconelli 18551197abfSClaudio Lanconelli /* Parity calculator on a word of n bit size */ 18651197abfSClaudio Lanconelli static int get_parity(int number, int size) 18751197abfSClaudio Lanconelli { 18851197abfSClaudio Lanconelli int k; 18951197abfSClaudio Lanconelli int parity; 19051197abfSClaudio Lanconelli 19151197abfSClaudio Lanconelli parity = 1; 19251197abfSClaudio Lanconelli for (k = 0; k < size; k++) { 19351197abfSClaudio Lanconelli parity += (number >> k); 19451197abfSClaudio Lanconelli parity &= 1; 19551197abfSClaudio Lanconelli } 19651197abfSClaudio Lanconelli return parity; 19751197abfSClaudio Lanconelli } 19851197abfSClaudio Lanconelli 19951197abfSClaudio Lanconelli /* Read and validate the logical block address field stored in the OOB */ 20051197abfSClaudio Lanconelli static int get_logical_address(uint8_t *oob_buf) 20151197abfSClaudio Lanconelli { 20251197abfSClaudio Lanconelli int block_address, parity; 20351197abfSClaudio Lanconelli int offset[2] = {6, 11}; /* offset of the 2 address fields within OOB */ 20451197abfSClaudio Lanconelli int j; 20551197abfSClaudio Lanconelli int ok = 0; 20651197abfSClaudio Lanconelli 20751197abfSClaudio Lanconelli /* 20851197abfSClaudio Lanconelli * Look for the first valid logical address 20951197abfSClaudio Lanconelli * Valid address has fixed pattern on most significant bits and 21051197abfSClaudio Lanconelli * parity check 21151197abfSClaudio Lanconelli */ 21251197abfSClaudio Lanconelli for (j = 0; j < ARRAY_SIZE(offset); j++) { 21351197abfSClaudio Lanconelli block_address = ((int)oob_buf[offset[j]] << 8) | 21451197abfSClaudio Lanconelli oob_buf[offset[j]+1]; 21551197abfSClaudio Lanconelli 21651197abfSClaudio Lanconelli /* Check for the signature bits in the address field (MSBits) */ 21751197abfSClaudio Lanconelli if ((block_address & ~0x7FF) == 0x1000) { 21851197abfSClaudio Lanconelli parity = block_address & 0x01; 21951197abfSClaudio Lanconelli block_address &= 0x7FF; 22051197abfSClaudio Lanconelli block_address >>= 1; 22151197abfSClaudio Lanconelli 22251197abfSClaudio Lanconelli if (get_parity(block_address, 10) != parity) { 223*289c0522SBrian Norris pr_debug("SSFDC_RO: logical address field%d" 22451197abfSClaudio Lanconelli "parity error(0x%04X)\n", j+1, 22551197abfSClaudio Lanconelli block_address); 22651197abfSClaudio Lanconelli } else { 22751197abfSClaudio Lanconelli ok = 1; 22851197abfSClaudio Lanconelli break; 22951197abfSClaudio Lanconelli } 23051197abfSClaudio Lanconelli } 23151197abfSClaudio Lanconelli } 23251197abfSClaudio Lanconelli 23351197abfSClaudio Lanconelli if (!ok) 23451197abfSClaudio Lanconelli block_address = -2; 23551197abfSClaudio Lanconelli 236*289c0522SBrian Norris pr_debug("SSFDC_RO: get_logical_address() %d\n", 23751197abfSClaudio Lanconelli block_address); 23851197abfSClaudio Lanconelli 23951197abfSClaudio Lanconelli return block_address; 24051197abfSClaudio Lanconelli } 24151197abfSClaudio Lanconelli 24251197abfSClaudio Lanconelli /* Build the logic block map */ 24351197abfSClaudio Lanconelli static int build_logical_block_map(struct ssfdcr_record *ssfdc) 24451197abfSClaudio Lanconelli { 24551197abfSClaudio Lanconelli unsigned long offset; 24651197abfSClaudio Lanconelli uint8_t oob_buf[OOB_SIZE]; 24751197abfSClaudio Lanconelli int ret, block_address, phys_block; 24851197abfSClaudio Lanconelli struct mtd_info *mtd = ssfdc->mbd.mtd; 24951197abfSClaudio Lanconelli 250*289c0522SBrian Norris pr_debug("SSFDC_RO: build_block_map() nblks=%d (%luK)\n", 25108d3ad6aSDavid Woodhouse ssfdc->map_len, 25208d3ad6aSDavid Woodhouse (unsigned long)ssfdc->map_len * ssfdc->erase_size / 1024); 25351197abfSClaudio Lanconelli 25451197abfSClaudio Lanconelli /* Scan every physical block, skip CIS block */ 25551197abfSClaudio Lanconelli for (phys_block = ssfdc->cis_block + 1; phys_block < ssfdc->map_len; 25651197abfSClaudio Lanconelli phys_block++) { 25751197abfSClaudio Lanconelli offset = (unsigned long)phys_block * ssfdc->erase_size; 25851197abfSClaudio Lanconelli if (mtd->block_isbad(mtd, offset)) 25951197abfSClaudio Lanconelli continue; /* skip bad blocks */ 26051197abfSClaudio Lanconelli 26151197abfSClaudio Lanconelli ret = read_raw_oob(mtd, offset, oob_buf); 26251197abfSClaudio Lanconelli if (ret < 0) { 263*289c0522SBrian Norris pr_debug("SSFDC_RO: mtd read_oob() failed at %lu\n", 26451197abfSClaudio Lanconelli offset); 26551197abfSClaudio Lanconelli return -1; 26651197abfSClaudio Lanconelli } 26751197abfSClaudio Lanconelli block_address = get_logical_address(oob_buf); 26851197abfSClaudio Lanconelli 26951197abfSClaudio Lanconelli /* Skip invalid addresses */ 27051197abfSClaudio Lanconelli if (block_address >= 0 && 27151197abfSClaudio Lanconelli block_address < MAX_LOGIC_BLK_PER_ZONE) { 27251197abfSClaudio Lanconelli int zone_index; 27351197abfSClaudio Lanconelli 27451197abfSClaudio Lanconelli zone_index = phys_block / MAX_PHYS_BLK_PER_ZONE; 27551197abfSClaudio Lanconelli block_address += zone_index * MAX_LOGIC_BLK_PER_ZONE; 27651197abfSClaudio Lanconelli ssfdc->logic_block_map[block_address] = 27751197abfSClaudio Lanconelli (unsigned short)phys_block; 27851197abfSClaudio Lanconelli 279*289c0522SBrian Norris pr_debug("SSFDC_RO: build_block_map() phys_block=%d," 28051197abfSClaudio Lanconelli "logic_block_addr=%d, zone=%d\n", 28151197abfSClaudio Lanconelli phys_block, block_address, zone_index); 28251197abfSClaudio Lanconelli } 28351197abfSClaudio Lanconelli } 28451197abfSClaudio Lanconelli return 0; 28551197abfSClaudio Lanconelli } 28651197abfSClaudio Lanconelli 28751197abfSClaudio Lanconelli static void ssfdcr_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd) 28851197abfSClaudio Lanconelli { 28951197abfSClaudio Lanconelli struct ssfdcr_record *ssfdc; 29051197abfSClaudio Lanconelli int cis_sector; 29151197abfSClaudio Lanconelli 29251197abfSClaudio Lanconelli /* Check for small page NAND flash */ 29369423d99SAdrian Hunter if (mtd->type != MTD_NANDFLASH || mtd->oobsize != OOB_SIZE || 29469423d99SAdrian Hunter mtd->size > UINT_MAX) 29551197abfSClaudio Lanconelli return; 29651197abfSClaudio Lanconelli 29751197abfSClaudio Lanconelli /* Check for SSDFC format by reading CIS/IDI sector */ 29851197abfSClaudio Lanconelli cis_sector = get_valid_cis_sector(mtd); 29951197abfSClaudio Lanconelli if (cis_sector == -1) 30051197abfSClaudio Lanconelli return; 30151197abfSClaudio Lanconelli 30251197abfSClaudio Lanconelli ssfdc = kzalloc(sizeof(struct ssfdcr_record), GFP_KERNEL); 3030870066dSBrian Norris if (!ssfdc) 30451197abfSClaudio Lanconelli return; 30551197abfSClaudio Lanconelli 30651197abfSClaudio Lanconelli ssfdc->mbd.mtd = mtd; 30751197abfSClaudio Lanconelli ssfdc->mbd.devnum = -1; 30851197abfSClaudio Lanconelli ssfdc->mbd.tr = tr; 30951197abfSClaudio Lanconelli ssfdc->mbd.readonly = 1; 31051197abfSClaudio Lanconelli 31151197abfSClaudio Lanconelli ssfdc->cis_block = cis_sector / (mtd->erasesize >> SECTOR_SHIFT); 31251197abfSClaudio Lanconelli ssfdc->erase_size = mtd->erasesize; 31369423d99SAdrian Hunter ssfdc->map_len = (u32)mtd->size / mtd->erasesize; 31451197abfSClaudio Lanconelli 315*289c0522SBrian Norris pr_debug("SSFDC_RO: cis_block=%d,erase_size=%d,map_len=%d,n_zones=%d\n", 31651197abfSClaudio Lanconelli ssfdc->cis_block, ssfdc->erase_size, ssfdc->map_len, 317c8872b06SJulia Lawall DIV_ROUND_UP(ssfdc->map_len, MAX_PHYS_BLK_PER_ZONE)); 31851197abfSClaudio Lanconelli 31951197abfSClaudio Lanconelli /* Set geometry */ 32051197abfSClaudio Lanconelli ssfdc->heads = 16; 32151197abfSClaudio Lanconelli ssfdc->sectors = 32; 32251197abfSClaudio Lanconelli get_chs(mtd->size, NULL, &ssfdc->heads, &ssfdc->sectors); 32369423d99SAdrian Hunter ssfdc->cylinders = (unsigned short)(((u32)mtd->size >> SECTOR_SHIFT) / 32451197abfSClaudio Lanconelli ((long)ssfdc->sectors * (long)ssfdc->heads)); 32551197abfSClaudio Lanconelli 326*289c0522SBrian Norris pr_debug("SSFDC_RO: using C:%d H:%d S:%d == %ld sects\n", 32751197abfSClaudio Lanconelli ssfdc->cylinders, ssfdc->heads , ssfdc->sectors, 32851197abfSClaudio Lanconelli (long)ssfdc->cylinders * (long)ssfdc->heads * 32951197abfSClaudio Lanconelli (long)ssfdc->sectors); 33051197abfSClaudio Lanconelli 33151197abfSClaudio Lanconelli ssfdc->mbd.size = (long)ssfdc->heads * (long)ssfdc->cylinders * 33251197abfSClaudio Lanconelli (long)ssfdc->sectors; 33351197abfSClaudio Lanconelli 33451197abfSClaudio Lanconelli /* Allocate logical block map */ 33551197abfSClaudio Lanconelli ssfdc->logic_block_map = kmalloc(sizeof(ssfdc->logic_block_map[0]) * 33651197abfSClaudio Lanconelli ssfdc->map_len, GFP_KERNEL); 3370870066dSBrian Norris if (!ssfdc->logic_block_map) 33851197abfSClaudio Lanconelli goto out_err; 33951197abfSClaudio Lanconelli memset(ssfdc->logic_block_map, 0xff, sizeof(ssfdc->logic_block_map[0]) * 34051197abfSClaudio Lanconelli ssfdc->map_len); 34151197abfSClaudio Lanconelli 34251197abfSClaudio Lanconelli /* Build logical block map */ 34351197abfSClaudio Lanconelli if (build_logical_block_map(ssfdc) < 0) 34451197abfSClaudio Lanconelli goto out_err; 34551197abfSClaudio Lanconelli 34651197abfSClaudio Lanconelli /* Register device + partitions */ 34751197abfSClaudio Lanconelli if (add_mtd_blktrans_dev(&ssfdc->mbd)) 34851197abfSClaudio Lanconelli goto out_err; 34951197abfSClaudio Lanconelli 35051197abfSClaudio Lanconelli printk(KERN_INFO "SSFDC_RO: Found ssfdc%c on mtd%d (%s)\n", 35151197abfSClaudio Lanconelli ssfdc->mbd.devnum + 'a', mtd->index, mtd->name); 35251197abfSClaudio Lanconelli return; 35351197abfSClaudio Lanconelli 35451197abfSClaudio Lanconelli out_err: 35551197abfSClaudio Lanconelli kfree(ssfdc->logic_block_map); 35651197abfSClaudio Lanconelli kfree(ssfdc); 35751197abfSClaudio Lanconelli } 35851197abfSClaudio Lanconelli 35951197abfSClaudio Lanconelli static void ssfdcr_remove_dev(struct mtd_blktrans_dev *dev) 36051197abfSClaudio Lanconelli { 36151197abfSClaudio Lanconelli struct ssfdcr_record *ssfdc = (struct ssfdcr_record *)dev; 36251197abfSClaudio Lanconelli 363*289c0522SBrian Norris pr_debug("SSFDC_RO: remove_dev (i=%d)\n", dev->devnum); 36451197abfSClaudio Lanconelli 36551197abfSClaudio Lanconelli del_mtd_blktrans_dev(dev); 36651197abfSClaudio Lanconelli kfree(ssfdc->logic_block_map); 36751197abfSClaudio Lanconelli } 36851197abfSClaudio Lanconelli 36951197abfSClaudio Lanconelli static int ssfdcr_readsect(struct mtd_blktrans_dev *dev, 37051197abfSClaudio Lanconelli unsigned long logic_sect_no, char *buf) 37151197abfSClaudio Lanconelli { 37251197abfSClaudio Lanconelli struct ssfdcr_record *ssfdc = (struct ssfdcr_record *)dev; 37351197abfSClaudio Lanconelli int sectors_per_block, offset, block_address; 37451197abfSClaudio Lanconelli 37551197abfSClaudio Lanconelli sectors_per_block = ssfdc->erase_size >> SECTOR_SHIFT; 37651197abfSClaudio Lanconelli offset = (int)(logic_sect_no % sectors_per_block); 37751197abfSClaudio Lanconelli block_address = (int)(logic_sect_no / sectors_per_block); 37851197abfSClaudio Lanconelli 379*289c0522SBrian Norris pr_debug("SSFDC_RO: ssfdcr_readsect(%lu) sec_per_blk=%d, ofst=%d," 38051197abfSClaudio Lanconelli " block_addr=%d\n", logic_sect_no, sectors_per_block, offset, 38151197abfSClaudio Lanconelli block_address); 38251197abfSClaudio Lanconelli 38351197abfSClaudio Lanconelli if (block_address >= ssfdc->map_len) 38451197abfSClaudio Lanconelli BUG(); 38551197abfSClaudio Lanconelli 38651197abfSClaudio Lanconelli block_address = ssfdc->logic_block_map[block_address]; 38751197abfSClaudio Lanconelli 388*289c0522SBrian Norris pr_debug("SSFDC_RO: ssfdcr_readsect() phys_block_addr=%d\n", 38951197abfSClaudio Lanconelli block_address); 39051197abfSClaudio Lanconelli 39151197abfSClaudio Lanconelli if (block_address < 0xffff) { 39251197abfSClaudio Lanconelli unsigned long sect_no; 39351197abfSClaudio Lanconelli 39451197abfSClaudio Lanconelli sect_no = (unsigned long)block_address * sectors_per_block + 39551197abfSClaudio Lanconelli offset; 39651197abfSClaudio Lanconelli 397*289c0522SBrian Norris pr_debug("SSFDC_RO: ssfdcr_readsect() phys_sect_no=%lu\n", 39851197abfSClaudio Lanconelli sect_no); 39951197abfSClaudio Lanconelli 40051197abfSClaudio Lanconelli if (read_physical_sector(ssfdc->mbd.mtd, buf, sect_no) < 0) 40151197abfSClaudio Lanconelli return -EIO; 40251197abfSClaudio Lanconelli } else { 40351197abfSClaudio Lanconelli memset(buf, 0xff, SECTOR_SIZE); 40451197abfSClaudio Lanconelli } 40551197abfSClaudio Lanconelli 40651197abfSClaudio Lanconelli return 0; 40751197abfSClaudio Lanconelli } 40851197abfSClaudio Lanconelli 40951197abfSClaudio Lanconelli static int ssfdcr_getgeo(struct mtd_blktrans_dev *dev, struct hd_geometry *geo) 41051197abfSClaudio Lanconelli { 41151197abfSClaudio Lanconelli struct ssfdcr_record *ssfdc = (struct ssfdcr_record *)dev; 41251197abfSClaudio Lanconelli 413*289c0522SBrian Norris pr_debug("SSFDC_RO: ssfdcr_getgeo() C=%d, H=%d, S=%d\n", 41451197abfSClaudio Lanconelli ssfdc->cylinders, ssfdc->heads, ssfdc->sectors); 41551197abfSClaudio Lanconelli 41651197abfSClaudio Lanconelli geo->heads = ssfdc->heads; 41751197abfSClaudio Lanconelli geo->sectors = ssfdc->sectors; 41851197abfSClaudio Lanconelli geo->cylinders = ssfdc->cylinders; 41951197abfSClaudio Lanconelli 42051197abfSClaudio Lanconelli return 0; 42151197abfSClaudio Lanconelli } 42251197abfSClaudio Lanconelli 42351197abfSClaudio Lanconelli /**************************************************************************** 42451197abfSClaudio Lanconelli * 42551197abfSClaudio Lanconelli * Module stuff 42651197abfSClaudio Lanconelli * 42751197abfSClaudio Lanconelli ****************************************************************************/ 42851197abfSClaudio Lanconelli 42951197abfSClaudio Lanconelli static struct mtd_blktrans_ops ssfdcr_tr = { 43051197abfSClaudio Lanconelli .name = "ssfdc", 43151197abfSClaudio Lanconelli .major = SSFDCR_MAJOR, 43251197abfSClaudio Lanconelli .part_bits = SSFDCR_PARTN_BITS, 43376ab40e4SDavid Woodhouse .blksize = SECTOR_SIZE, 43451197abfSClaudio Lanconelli .getgeo = ssfdcr_getgeo, 43551197abfSClaudio Lanconelli .readsect = ssfdcr_readsect, 43651197abfSClaudio Lanconelli .add_mtd = ssfdcr_add_mtd, 43751197abfSClaudio Lanconelli .remove_dev = ssfdcr_remove_dev, 43851197abfSClaudio Lanconelli .owner = THIS_MODULE, 43951197abfSClaudio Lanconelli }; 44051197abfSClaudio Lanconelli 44151197abfSClaudio Lanconelli static int __init init_ssfdcr(void) 44251197abfSClaudio Lanconelli { 44351197abfSClaudio Lanconelli printk(KERN_INFO "SSFDC read-only Flash Translation layer\n"); 44451197abfSClaudio Lanconelli 44551197abfSClaudio Lanconelli return register_mtd_blktrans(&ssfdcr_tr); 44651197abfSClaudio Lanconelli } 44751197abfSClaudio Lanconelli 44851197abfSClaudio Lanconelli static void __exit cleanup_ssfdcr(void) 44951197abfSClaudio Lanconelli { 45051197abfSClaudio Lanconelli deregister_mtd_blktrans(&ssfdcr_tr); 45151197abfSClaudio Lanconelli } 45251197abfSClaudio Lanconelli 45351197abfSClaudio Lanconelli module_init(init_ssfdcr); 45451197abfSClaudio Lanconelli module_exit(cleanup_ssfdcr); 45551197abfSClaudio Lanconelli 45651197abfSClaudio Lanconelli MODULE_LICENSE("GPL"); 45751197abfSClaudio Lanconelli MODULE_AUTHOR("Claudio Lanconelli <lanconelli.claudio@eptar.com>"); 45851197abfSClaudio Lanconelli MODULE_DESCRIPTION("Flash Translation Layer for read-only SSFDC SmartMedia card"); 459