19fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky/* 29fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky * Copyright © 2009 - Maxim Levitsky 39fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky * Common routines & support for SmartMedia/xD format 49fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky * 59fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky * This program is free software; you can redistribute it and/or modify 69fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky * it under the terms of the GNU General Public License version 2 as 79fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky * published by the Free Software Foundation. 89fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky */ 99fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky#include <linux/bitops.h> 109fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky#include <linux/mtd/mtd.h> 119fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky 129fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky/* Full oob structure as written on the flash */ 139fc51a37a8da84618df7584cad67c078317f6720Maxim Levitskystruct sm_oob { 149fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky uint32_t reserved; 159fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky uint8_t data_status; 169fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky uint8_t block_status; 179fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky uint8_t lba_copy1[2]; 189fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky uint8_t ecc2[3]; 199fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky uint8_t lba_copy2[2]; 209fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky uint8_t ecc1[3]; 2131f754628cbb12c983600f22d9f0fed50dfe2134Brian Norris} __packed; 229fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky 239fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky 249fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky/* one sector is always 512 bytes, but it can consist of two nand pages */ 259fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky#define SM_SECTOR_SIZE 512 269fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky 279fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky/* oob area is also 16 bytes, but might be from two pages */ 289fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky#define SM_OOB_SIZE 16 299fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky 309fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky/* This is maximum zone size, and all devices that have more that one zone 319fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky have this size */ 329fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky#define SM_MAX_ZONE_SIZE 1024 339fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky 349fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky/* support for small page nand */ 359fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky#define SM_SMALL_PAGE 256 369fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky#define SM_SMALL_OOB_SIZE 8 379fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky 389fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky 39c3611570ddf601609f8803574ea83889ff969aa0Maxim Levitskyextern int sm_register_device(struct mtd_info *mtd, int smartmedia); 409fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky 419fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky 421f6ca0d6213278f8608c7e342e423ec0c0198040Stephen Rothwellstatic inline int sm_sector_valid(struct sm_oob *oob) 439fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky{ 449fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky return hweight16(oob->data_status) >= 5; 459fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky} 469fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky 471f6ca0d6213278f8608c7e342e423ec0c0198040Stephen Rothwellstatic inline int sm_block_valid(struct sm_oob *oob) 489fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky{ 499fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky return hweight16(oob->block_status) >= 7; 509fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky} 519fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky 521f6ca0d6213278f8608c7e342e423ec0c0198040Stephen Rothwellstatic inline int sm_block_erased(struct sm_oob *oob) 539fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky{ 549fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky static const uint32_t erased_pattern[4] = { 559fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF }; 569fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky 579fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky /* First test for erased block */ 589fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky if (!memcmp(oob, erased_pattern, sizeof(*oob))) 599fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky return 1; 609fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky return 0; 619fc51a37a8da84618df7584cad67c078317f6720Maxim Levitsky} 62