Skip to content

Commit abbe26d

Browse files
author
Boris Brezillon
committed
mtd: nand: Add manufacturer specific initialization/detection steps
A lot of NANDs are implementing generic features in a non-generic way, or are providing advanced auto-detection logic where the NAND ID bytes meaning changes with the NAND generation. Providing this vendor specific initialization step will allow us to get rid of full-id entries in the nand_ids table or all the vendor specific cases added over the time in the generic NAND ID decoding logic. Signed-off-by: Boris Brezillon <boris.brezillon@free-electrons.com>
1 parent bcc678c commit abbe26d

2 files changed

Lines changed: 100 additions & 10 deletions

File tree

‎drivers/mtd/nand/nand_base.c‎

Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3819,7 +3819,7 @@ static int nand_get_bits_per_cell(u8 cellinfo)
38193819
* chip. The rest of the parameters must be decoded according to generic or
38203820
* manufacturer-specific "extended ID" decoding patterns.
38213821
*/
3822-
static void nand_decode_ext_id(struct nand_chip *chip)
3822+
void nand_decode_ext_id(struct nand_chip *chip)
38233823
{
38243824
struct mtd_info *mtd = nand_to_mtd(chip);
38253825
int extid, id_len = chip->id.len;
@@ -3944,6 +3944,7 @@ static void nand_decode_ext_id(struct nand_chip *chip)
39443944

39453945
}
39463946
}
3947+
EXPORT_SYMBOL_GPL(nand_decode_ext_id);
39473948

39483949
/*
39493950
* Old devices have chip data hardcoded in the device ID table. nand_decode_id
@@ -4047,6 +4048,53 @@ static bool find_full_id_nand(struct nand_chip *chip,
40474048
return false;
40484049
}
40494050

4051+
/*
4052+
* Manufacturer detection. Only used when the NAND is not ONFI or JEDEC
4053+
* compliant and does not have a full-id or legacy-id entry in the nand_ids
4054+
* table.
4055+
*/
4056+
static void nand_manufacturer_detect(struct nand_chip *chip)
4057+
{
4058+
/*
4059+
* Try manufacturer detection if available and use
4060+
* nand_decode_ext_id() otherwise.
4061+
*/
4062+
if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
4063+
chip->manufacturer.desc->ops->detect)
4064+
chip->manufacturer.desc->ops->detect(chip);
4065+
else
4066+
nand_decode_ext_id(chip);
4067+
}
4068+
4069+
/*
4070+
* Manufacturer initialization. This function is called for all NANDs including
4071+
* ONFI and JEDEC compliant ones.
4072+
* Manufacturer drivers should put all their specific initialization code in
4073+
* their ->init() hook.
4074+
*/
4075+
static int nand_manufacturer_init(struct nand_chip *chip)
4076+
{
4077+
if (!chip->manufacturer.desc || !chip->manufacturer.desc->ops ||
4078+
!chip->manufacturer.desc->ops->init)
4079+
return 0;
4080+
4081+
return chip->manufacturer.desc->ops->init(chip);
4082+
}
4083+
4084+
/*
4085+
* Manufacturer cleanup. This function is called for all NANDs including
4086+
* ONFI and JEDEC compliant ones.
4087+
* Manufacturer drivers should put all their specific cleanup code in their
4088+
* ->cleanup() hook.
4089+
*/
4090+
static void nand_manufacturer_cleanup(struct nand_chip *chip)
4091+
{
4092+
/* Release manufacturer private data */
4093+
if (chip->manufacturer.desc && chip->manufacturer.desc->ops &&
4094+
chip->manufacturer.desc->ops->cleanup)
4095+
chip->manufacturer.desc->ops->cleanup(chip);
4096+
}
4097+
40504098
/*
40514099
* Get the flash and manufacturer id and lookup if the type is supported.
40524100
*/
@@ -4055,7 +4103,7 @@ static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
40554103
const struct nand_manufacturer *manufacturer;
40564104
struct mtd_info *mtd = nand_to_mtd(chip);
40574105
int busw;
4058-
int i;
4106+
int i, ret;
40594107
u8 *id_data = chip->id.data;
40604108
u8 maf_id, dev_id;
40614109

@@ -4096,6 +4144,10 @@ static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
40964144

40974145
chip->id.len = nand_id_len(id_data, 8);
40984146

4147+
/* Try to identify manufacturer */
4148+
manufacturer = nand_get_manufacturer(maf_id);
4149+
chip->manufacturer.desc = manufacturer;
4150+
40994151
if (!type)
41004152
type = nand_flash_ids;
41014153

@@ -4142,12 +4194,11 @@ static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
41424194

41434195
chip->chipsize = (uint64_t)type->chipsize << 20;
41444196

4145-
if (!type->pagesize) {
4146-
/* Decode parameters from extended ID */
4147-
nand_decode_ext_id(chip);
4148-
} else {
4197+
if (!type->pagesize)
4198+
nand_manufacturer_detect(chip);
4199+
else
41494200
nand_decode_id(chip, type);
4150-
}
4201+
41514202
/* Get chip options */
41524203
chip->options |= type->options;
41534204

@@ -4159,9 +4210,6 @@ static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
41594210
chip->options &= ~NAND_SAMSUNG_LP_OPTIONS;
41604211
ident_done:
41614212

4162-
/* Try to identify manufacturer */
4163-
manufacturer = nand_get_manufacturer(maf_id);
4164-
41654213
if (chip->options & NAND_BUSWIDTH_AUTO) {
41664214
WARN_ON(busw & NAND_BUSWIDTH_16);
41674215
nand_set_defaults(chip);
@@ -4202,6 +4250,10 @@ static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
42024250
if (mtd->writesize > 512 && chip->cmdfunc == nand_command)
42034251
chip->cmdfunc = nand_command_lp;
42044252

4253+
ret = nand_manufacturer_init(chip);
4254+
if (ret)
4255+
return ret;
4256+
42054257
pr_info("device found, Manufacturer ID: 0x%02x, Chip ID: 0x%02x\n",
42064258
maf_id, dev_id);
42074259

@@ -4947,6 +4999,9 @@ void nand_cleanup(struct nand_chip *chip)
49474999
if (chip->badblock_pattern && chip->badblock_pattern->options
49485000
& NAND_BBT_DYNAMICSTRUCT)
49495001
kfree(chip->badblock_pattern);
5002+
5003+
/* Free manufacturer priv data. */
5004+
nand_manufacturer_cleanup(chip);
49505005
}
49515006
EXPORT_SYMBOL_GPL(nand_cleanup);
49525007

‎include/linux/mtd/nand.h‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,20 @@ nand_get_sdr_timings(const struct nand_data_interface *conf)
731731
return &conf->timings.sdr;
732732
}
733733

734+
/**
735+
* struct nand_manufacturer_ops - NAND Manufacturer operations
736+
* @detect: detect the NAND memory organization and capabilities
737+
* @init: initialize all vendor specific fields (like the ->read_retry()
738+
* implementation) if any.
739+
* @cleanup: the ->init() function may have allocated resources, ->cleanup()
740+
* is here to let vendor specific code release those resources.
741+
*/
742+
struct nand_manufacturer_ops {
743+
void (*detect)(struct nand_chip *chip);
744+
int (*init)(struct nand_chip *chip);
745+
void (*cleanup)(struct nand_chip *chip);
746+
};
747+
734748
/**
735749
* struct nand_chip - NAND Private Flash Chip Data
736750
* @mtd: MTD device registered to the MTD framework
@@ -835,6 +849,7 @@ nand_get_sdr_timings(const struct nand_data_interface *conf)
835849
* additional error status checks (determine if errors are
836850
* correctable).
837851
* @write_page: [REPLACEABLE] High-level page write function
852+
* @manufacturer: [INTERN] Contains manufacturer information
838853
*/
839854

840855
struct nand_chip {
@@ -923,6 +938,11 @@ struct nand_chip {
923938
struct nand_bbt_descr *badblock_pattern;
924939

925940
void *priv;
941+
942+
struct {
943+
const struct nand_manufacturer *desc;
944+
void *priv;
945+
} manufacturer;
926946
};
927947

928948
extern const struct mtd_ooblayout_ops nand_ooblayout_sp_ops;
@@ -959,6 +979,17 @@ static inline void nand_set_controller_data(struct nand_chip *chip, void *priv)
959979
chip->priv = priv;
960980
}
961981

982+
static inline void nand_set_manufacturer_data(struct nand_chip *chip,
983+
void *priv)
984+
{
985+
chip->manufacturer.priv = priv;
986+
}
987+
988+
static inline void *nand_get_manufacturer_data(struct nand_chip *chip)
989+
{
990+
return chip->manufacturer.priv;
991+
}
992+
962993
/*
963994
* NAND Flash Manufacturer ID Codes
964995
*/
@@ -1065,10 +1096,12 @@ struct nand_flash_dev {
10651096
* struct nand_manufacturer - NAND Flash Manufacturer structure
10661097
* @name: Manufacturer name
10671098
* @id: manufacturer ID code of device.
1099+
* @ops: manufacturer operations
10681100
*/
10691101
struct nand_manufacturer {
10701102
int id;
10711103
char *name;
1104+
const struct nand_manufacturer_ops *ops;
10721105
};
10731106

10741107
const struct nand_manufacturer *nand_get_manufacturer(u8 id);
@@ -1246,4 +1279,6 @@ int nand_reset(struct nand_chip *chip, int chipnr);
12461279
/* Free resources held by the NAND device */
12471280
void nand_cleanup(struct nand_chip *chip);
12481281

1282+
/* Default extended ID decoding function */
1283+
void nand_decode_ext_id(struct nand_chip *chip);
12491284
#endif /* __LINUX_MTD_NAND_H */

0 commit comments

Comments
 (0)