sfdp.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2005, Intec Automation Inc.
  4. * Copyright (C) 2014, Freescale Semiconductor, Inc.
  5. */
  6. #include <linux/bitfield.h>
  7. #include <linux/mtd/spi-nor.h>
  8. #include <linux/slab.h>
  9. #include <linux/sort.h>
  10. #include "core.h"
  11. #define SFDP_PARAM_HEADER_ID(p) (((p)->id_msb << 8) | (p)->id_lsb)
  12. #define SFDP_PARAM_HEADER_PTP(p) \
  13. (((p)->parameter_table_pointer[2] << 16) | \
  14. ((p)->parameter_table_pointer[1] << 8) | \
  15. ((p)->parameter_table_pointer[0] << 0))
  16. #define SFDP_PARAM_HEADER_PARAM_LEN(p) ((p)->length * 4)
  17. #define SFDP_BFPT_ID 0xff00 /* Basic Flash Parameter Table */
  18. #define SFDP_SECTOR_MAP_ID 0xff81 /* Sector Map Table */
  19. #define SFDP_4BAIT_ID 0xff84 /* 4-byte Address Instruction Table */
  20. #define SFDP_PROFILE1_ID 0xff05 /* xSPI Profile 1.0 table. */
  21. #define SFDP_SCCR_MAP_ID 0xff87 /*
  22. * Status, Control and Configuration
  23. * Register Map.
  24. */
  25. #define SFDP_SCCR_MAP_MC_ID 0xff88 /*
  26. * Status, Control and Configuration
  27. * Register Map Offsets for Multi-Chip
  28. * SPI Memory Devices.
  29. */
  30. #define SFDP_SIGNATURE 0x50444653U
  31. struct sfdp_header {
  32. u32 signature; /* Ox50444653U <=> "SFDP" */
  33. u8 minor;
  34. u8 major;
  35. u8 nph; /* 0-base number of parameter headers */
  36. u8 unused;
  37. /* Basic Flash Parameter Table. */
  38. struct sfdp_parameter_header bfpt_header;
  39. };
  40. /* Fast Read settings. */
  41. struct sfdp_bfpt_read {
  42. /* The Fast Read x-y-z hardware capability in params->hwcaps.mask. */
  43. u32 hwcaps;
  44. /*
  45. * The <supported_bit> bit in <supported_dword> BFPT DWORD tells us
  46. * whether the Fast Read x-y-z command is supported.
  47. */
  48. u32 supported_dword;
  49. u32 supported_bit;
  50. /*
  51. * The half-word at offset <setting_shift> in <setting_dword> BFPT DWORD
  52. * encodes the op code, the number of mode clocks and the number of wait
  53. * states to be used by Fast Read x-y-z command.
  54. */
  55. u32 settings_dword;
  56. u32 settings_shift;
  57. /* The SPI protocol for this Fast Read x-y-z command. */
  58. enum spi_nor_protocol proto;
  59. };
  60. struct sfdp_bfpt_erase {
  61. /*
  62. * The half-word at offset <shift> in DWORD <dword> encodes the
  63. * op code and erase sector size to be used by Sector Erase commands.
  64. */
  65. u32 dword;
  66. u32 shift;
  67. };
  68. #define SMPT_CMD_ADDRESS_LEN_MASK GENMASK(23, 22)
  69. #define SMPT_CMD_ADDRESS_LEN_0 (0x0UL << 22)
  70. #define SMPT_CMD_ADDRESS_LEN_3 (0x1UL << 22)
  71. #define SMPT_CMD_ADDRESS_LEN_4 (0x2UL << 22)
  72. #define SMPT_CMD_ADDRESS_LEN_USE_CURRENT (0x3UL << 22)
  73. #define SMPT_CMD_READ_DUMMY_MASK GENMASK(19, 16)
  74. #define SMPT_CMD_READ_DUMMY_SHIFT 16
  75. #define SMPT_CMD_READ_DUMMY(_cmd) \
  76. (((_cmd) & SMPT_CMD_READ_DUMMY_MASK) >> SMPT_CMD_READ_DUMMY_SHIFT)
  77. #define SMPT_CMD_READ_DUMMY_IS_VARIABLE 0xfUL
  78. #define SMPT_CMD_READ_DATA_MASK GENMASK(31, 24)
  79. #define SMPT_CMD_READ_DATA_SHIFT 24
  80. #define SMPT_CMD_READ_DATA(_cmd) \
  81. (((_cmd) & SMPT_CMD_READ_DATA_MASK) >> SMPT_CMD_READ_DATA_SHIFT)
  82. #define SMPT_CMD_OPCODE_MASK GENMASK(15, 8)
  83. #define SMPT_CMD_OPCODE_SHIFT 8
  84. #define SMPT_CMD_OPCODE(_cmd) \
  85. (((_cmd) & SMPT_CMD_OPCODE_MASK) >> SMPT_CMD_OPCODE_SHIFT)
  86. #define SMPT_MAP_REGION_COUNT_MASK GENMASK(23, 16)
  87. #define SMPT_MAP_REGION_COUNT_SHIFT 16
  88. #define SMPT_MAP_REGION_COUNT(_header) \
  89. ((((_header) & SMPT_MAP_REGION_COUNT_MASK) >> \
  90. SMPT_MAP_REGION_COUNT_SHIFT) + 1)
  91. #define SMPT_MAP_ID_MASK GENMASK(15, 8)
  92. #define SMPT_MAP_ID_SHIFT 8
  93. #define SMPT_MAP_ID(_header) \
  94. (((_header) & SMPT_MAP_ID_MASK) >> SMPT_MAP_ID_SHIFT)
  95. #define SMPT_MAP_REGION_SIZE_MASK GENMASK(31, 8)
  96. #define SMPT_MAP_REGION_SIZE_SHIFT 8
  97. #define SMPT_MAP_REGION_SIZE(_region) \
  98. (((((_region) & SMPT_MAP_REGION_SIZE_MASK) >> \
  99. SMPT_MAP_REGION_SIZE_SHIFT) + 1) * 256)
  100. #define SMPT_MAP_REGION_ERASE_TYPE_MASK GENMASK(3, 0)
  101. #define SMPT_MAP_REGION_ERASE_TYPE(_region) \
  102. ((_region) & SMPT_MAP_REGION_ERASE_TYPE_MASK)
  103. #define SMPT_DESC_TYPE_MAP BIT(1)
  104. #define SMPT_DESC_END BIT(0)
  105. #define SFDP_4BAIT_DWORD_MAX 2
  106. struct sfdp_4bait {
  107. /* The hardware capability. */
  108. u32 hwcaps;
  109. /*
  110. * The <supported_bit> bit in DWORD1 of the 4BAIT tells us whether
  111. * the associated 4-byte address op code is supported.
  112. */
  113. u32 supported_bit;
  114. };
  115. /**
  116. * spi_nor_read_raw() - raw read of serial flash memory. read_opcode,
  117. * addr_nbytes and read_dummy members of the struct spi_nor
  118. * should be previously set.
  119. * @nor: pointer to a 'struct spi_nor'
  120. * @addr: offset in the serial flash memory
  121. * @len: number of bytes to read
  122. * @buf: buffer where the data is copied into (dma-safe memory)
  123. *
  124. * Return: 0 on success, -errno otherwise.
  125. */
  126. static int spi_nor_read_raw(struct spi_nor *nor, u32 addr, size_t len, u8 *buf)
  127. {
  128. ssize_t ret;
  129. while (len) {
  130. ret = spi_nor_read_data(nor, addr, len, buf);
  131. if (ret < 0)
  132. return ret;
  133. if (!ret || ret > len)
  134. return -EIO;
  135. buf += ret;
  136. addr += ret;
  137. len -= ret;
  138. }
  139. return 0;
  140. }
  141. /**
  142. * spi_nor_read_sfdp() - read Serial Flash Discoverable Parameters.
  143. * @nor: pointer to a 'struct spi_nor'
  144. * @addr: offset in the SFDP area to start reading data from
  145. * @len: number of bytes to read
  146. * @buf: buffer where the SFDP data are copied into (dma-safe memory)
  147. *
  148. * Whatever the actual numbers of bytes for address and dummy cycles are
  149. * for (Fast) Read commands, the Read SFDP (5Ah) instruction is always
  150. * followed by a 3-byte address and 8 dummy clock cycles.
  151. *
  152. * Return: 0 on success, -errno otherwise.
  153. */
  154. static int spi_nor_read_sfdp(struct spi_nor *nor, u32 addr,
  155. size_t len, void *buf)
  156. {
  157. u8 addr_nbytes, read_opcode, read_dummy;
  158. int ret;
  159. read_opcode = nor->read_opcode;
  160. addr_nbytes = nor->addr_nbytes;
  161. read_dummy = nor->read_dummy;
  162. nor->read_opcode = SPINOR_OP_RDSFDP;
  163. nor->addr_nbytes = 3;
  164. nor->read_dummy = 8;
  165. ret = spi_nor_read_raw(nor, addr, len, buf);
  166. nor->read_opcode = read_opcode;
  167. nor->addr_nbytes = addr_nbytes;
  168. nor->read_dummy = read_dummy;
  169. return ret;
  170. }
  171. /**
  172. * spi_nor_read_sfdp_dma_unsafe() - read Serial Flash Discoverable Parameters.
  173. * @nor: pointer to a 'struct spi_nor'
  174. * @addr: offset in the SFDP area to start reading data from
  175. * @len: number of bytes to read
  176. * @buf: buffer where the SFDP data are copied into
  177. *
  178. * Wrap spi_nor_read_sfdp() using a kmalloc'ed bounce buffer as @buf is now not
  179. * guaranteed to be dma-safe.
  180. *
  181. * Return: -ENOMEM if kmalloc() fails, the return code of spi_nor_read_sfdp()
  182. * otherwise.
  183. */
  184. static int spi_nor_read_sfdp_dma_unsafe(struct spi_nor *nor, u32 addr,
  185. size_t len, void *buf)
  186. {
  187. void *dma_safe_buf;
  188. int ret;
  189. dma_safe_buf = kmalloc(len, GFP_KERNEL);
  190. if (!dma_safe_buf)
  191. return -ENOMEM;
  192. ret = spi_nor_read_sfdp(nor, addr, len, dma_safe_buf);
  193. memcpy(buf, dma_safe_buf, len);
  194. kfree(dma_safe_buf);
  195. return ret;
  196. }
  197. static void
  198. spi_nor_set_read_settings_from_bfpt(struct spi_nor_read_command *read,
  199. u16 half,
  200. enum spi_nor_protocol proto)
  201. {
  202. read->num_mode_clocks = (half >> 5) & 0x07;
  203. read->num_wait_states = (half >> 0) & 0x1f;
  204. read->opcode = (half >> 8) & 0xff;
  205. read->proto = proto;
  206. }
  207. static const struct sfdp_bfpt_read sfdp_bfpt_reads[] = {
  208. /* Fast Read 1-1-2 */
  209. {
  210. SNOR_HWCAPS_READ_1_1_2,
  211. SFDP_DWORD(1), BIT(16), /* Supported bit */
  212. SFDP_DWORD(4), 0, /* Settings */
  213. SNOR_PROTO_1_1_2,
  214. },
  215. /* Fast Read 1-2-2 */
  216. {
  217. SNOR_HWCAPS_READ_1_2_2,
  218. SFDP_DWORD(1), BIT(20), /* Supported bit */
  219. SFDP_DWORD(4), 16, /* Settings */
  220. SNOR_PROTO_1_2_2,
  221. },
  222. /* Fast Read 2-2-2 */
  223. {
  224. SNOR_HWCAPS_READ_2_2_2,
  225. SFDP_DWORD(5), BIT(0), /* Supported bit */
  226. SFDP_DWORD(6), 16, /* Settings */
  227. SNOR_PROTO_2_2_2,
  228. },
  229. /* Fast Read 1-1-4 */
  230. {
  231. SNOR_HWCAPS_READ_1_1_4,
  232. SFDP_DWORD(1), BIT(22), /* Supported bit */
  233. SFDP_DWORD(3), 16, /* Settings */
  234. SNOR_PROTO_1_1_4,
  235. },
  236. /* Fast Read 1-4-4 */
  237. {
  238. SNOR_HWCAPS_READ_1_4_4,
  239. SFDP_DWORD(1), BIT(21), /* Supported bit */
  240. SFDP_DWORD(3), 0, /* Settings */
  241. SNOR_PROTO_1_4_4,
  242. },
  243. /* Fast Read 4-4-4 */
  244. {
  245. SNOR_HWCAPS_READ_4_4_4,
  246. SFDP_DWORD(5), BIT(4), /* Supported bit */
  247. SFDP_DWORD(7), 16, /* Settings */
  248. SNOR_PROTO_4_4_4,
  249. },
  250. };
  251. static const struct sfdp_bfpt_erase sfdp_bfpt_erases[] = {
  252. /* Erase Type 1 in DWORD8 bits[15:0] */
  253. {SFDP_DWORD(8), 0},
  254. /* Erase Type 2 in DWORD8 bits[31:16] */
  255. {SFDP_DWORD(8), 16},
  256. /* Erase Type 3 in DWORD9 bits[15:0] */
  257. {SFDP_DWORD(9), 0},
  258. /* Erase Type 4 in DWORD9 bits[31:16] */
  259. {SFDP_DWORD(9), 16},
  260. };
  261. /**
  262. * spi_nor_set_erase_settings_from_bfpt() - set erase type settings from BFPT
  263. * @erase: pointer to a structure that describes a SPI NOR erase type
  264. * @size: the size of the sector/block erased by the erase type
  265. * @opcode: the SPI command op code to erase the sector/block
  266. * @i: erase type index as sorted in the Basic Flash Parameter Table
  267. *
  268. * The supported Erase Types will be sorted at init in ascending order, with
  269. * the smallest Erase Type size being the first member in the erase_type array
  270. * of the spi_nor_erase_map structure. Save the Erase Type index as sorted in
  271. * the Basic Flash Parameter Table since it will be used later on to
  272. * synchronize with the supported Erase Types defined in SFDP optional tables.
  273. */
  274. static void
  275. spi_nor_set_erase_settings_from_bfpt(struct spi_nor_erase_type *erase,
  276. u32 size, u8 opcode, u8 i)
  277. {
  278. erase->idx = i;
  279. spi_nor_set_erase_type(erase, size, opcode);
  280. }
  281. /**
  282. * spi_nor_map_cmp_erase_type() - compare the map's erase types by size
  283. * @l: member in the left half of the map's erase_type array
  284. * @r: member in the right half of the map's erase_type array
  285. *
  286. * Comparison function used in the sort() call to sort in ascending order the
  287. * map's erase types, the smallest erase type size being the first member in the
  288. * sorted erase_type array.
  289. *
  290. * Return: the result of @l->size - @r->size
  291. */
  292. static int spi_nor_map_cmp_erase_type(const void *l, const void *r)
  293. {
  294. const struct spi_nor_erase_type *left = l, *right = r;
  295. return left->size - right->size;
  296. }
  297. /**
  298. * spi_nor_sort_erase_mask() - sort erase mask
  299. * @map: the erase map of the SPI NOR
  300. * @erase_mask: the erase type mask to be sorted
  301. *
  302. * Replicate the sort done for the map's erase types in BFPT: sort the erase
  303. * mask in ascending order with the smallest erase type size starting from
  304. * BIT(0) in the sorted erase mask.
  305. *
  306. * Return: sorted erase mask.
  307. */
  308. static u8 spi_nor_sort_erase_mask(struct spi_nor_erase_map *map, u8 erase_mask)
  309. {
  310. struct spi_nor_erase_type *erase_type = map->erase_type;
  311. int i;
  312. u8 sorted_erase_mask = 0;
  313. if (!erase_mask)
  314. return 0;
  315. /* Replicate the sort done for the map's erase types. */
  316. for (i = 0; i < SNOR_ERASE_TYPE_MAX; i++)
  317. if (erase_type[i].size && erase_mask & BIT(erase_type[i].idx))
  318. sorted_erase_mask |= BIT(i);
  319. return sorted_erase_mask;
  320. }
  321. /**
  322. * spi_nor_regions_sort_erase_types() - sort erase types in each region
  323. * @map: the erase map of the SPI NOR
  324. *
  325. * Function assumes that the erase types defined in the erase map are already
  326. * sorted in ascending order, with the smallest erase type size being the first
  327. * member in the erase_type array. It replicates the sort done for the map's
  328. * erase types. Each region's erase bitmask will indicate which erase types are
  329. * supported from the sorted erase types defined in the erase map.
  330. * Sort the all region's erase type at init in order to speed up the process of
  331. * finding the best erase command at runtime.
  332. */
  333. static void spi_nor_regions_sort_erase_types(struct spi_nor_erase_map *map)
  334. {
  335. struct spi_nor_erase_region *region = map->regions;
  336. u8 sorted_erase_mask;
  337. unsigned int i;
  338. for (i = 0; i < map->n_regions; i++) {
  339. sorted_erase_mask =
  340. spi_nor_sort_erase_mask(map, region[i].erase_mask);
  341. /* Overwrite erase mask. */
  342. region[i].erase_mask = sorted_erase_mask;
  343. }
  344. }
  345. /**
  346. * spi_nor_parse_bfpt() - read and parse the Basic Flash Parameter Table.
  347. * @nor: pointer to a 'struct spi_nor'
  348. * @bfpt_header: pointer to the 'struct sfdp_parameter_header' describing
  349. * the Basic Flash Parameter Table length and version
  350. *
  351. * The Basic Flash Parameter Table is the main and only mandatory table as
  352. * defined by the SFDP (JESD216) specification.
  353. * It provides us with the total size (memory density) of the data array and
  354. * the number of address bytes for Fast Read, Page Program and Sector Erase
  355. * commands.
  356. * For Fast READ commands, it also gives the number of mode clock cycles and
  357. * wait states (regrouped in the number of dummy clock cycles) for each
  358. * supported instruction op code.
  359. * For Page Program, the page size is now available since JESD216 rev A, however
  360. * the supported instruction op codes are still not provided.
  361. * For Sector Erase commands, this table stores the supported instruction op
  362. * codes and the associated sector sizes.
  363. * Finally, the Quad Enable Requirements (QER) are also available since JESD216
  364. * rev A. The QER bits encode the manufacturer dependent procedure to be
  365. * executed to set the Quad Enable (QE) bit in some internal register of the
  366. * Quad SPI memory. Indeed the QE bit, when it exists, must be set before
  367. * sending any Quad SPI command to the memory. Actually, setting the QE bit
  368. * tells the memory to reassign its WP# and HOLD#/RESET# pins to functions IO2
  369. * and IO3 hence enabling 4 (Quad) I/O lines.
  370. *
  371. * Return: 0 on success, -errno otherwise.
  372. */
  373. static int spi_nor_parse_bfpt(struct spi_nor *nor,
  374. const struct sfdp_parameter_header *bfpt_header)
  375. {
  376. struct spi_nor_flash_parameter *params = nor->params;
  377. struct spi_nor_erase_map *map = &params->erase_map;
  378. struct spi_nor_erase_type *erase_type = map->erase_type;
  379. struct sfdp_bfpt bfpt;
  380. size_t len;
  381. int i, cmd, err;
  382. u32 addr, val;
  383. u32 dword;
  384. u16 half;
  385. u8 erase_mask;
  386. u8 wait_states, mode_clocks, opcode;
  387. /* JESD216 Basic Flash Parameter Table length is at least 9 DWORDs. */
  388. if (bfpt_header->length < BFPT_DWORD_MAX_JESD216)
  389. return -EINVAL;
  390. /* Read the Basic Flash Parameter Table. */
  391. len = min_t(size_t, sizeof(bfpt),
  392. bfpt_header->length * sizeof(u32));
  393. addr = SFDP_PARAM_HEADER_PTP(bfpt_header);
  394. memset(&bfpt, 0, sizeof(bfpt));
  395. err = spi_nor_read_sfdp_dma_unsafe(nor, addr, len, &bfpt);
  396. if (err < 0)
  397. return err;
  398. /* Fix endianness of the BFPT DWORDs. */
  399. le32_to_cpu_array(bfpt.dwords, BFPT_DWORD_MAX);
  400. /* Number of address bytes. */
  401. switch (bfpt.dwords[SFDP_DWORD(1)] & BFPT_DWORD1_ADDRESS_BYTES_MASK) {
  402. case BFPT_DWORD1_ADDRESS_BYTES_3_ONLY:
  403. case BFPT_DWORD1_ADDRESS_BYTES_3_OR_4:
  404. params->addr_nbytes = 3;
  405. params->addr_mode_nbytes = 3;
  406. break;
  407. case BFPT_DWORD1_ADDRESS_BYTES_4_ONLY:
  408. params->addr_nbytes = 4;
  409. params->addr_mode_nbytes = 4;
  410. break;
  411. default:
  412. break;
  413. }
  414. /* Flash Memory Density (in bits). */
  415. val = bfpt.dwords[SFDP_DWORD(2)];
  416. if (val & BIT(31)) {
  417. val &= ~BIT(31);
  418. /*
  419. * Prevent overflows on params->size. Anyway, a NOR of 2^64
  420. * bits is unlikely to exist so this error probably means
  421. * the BFPT we are reading is corrupted/wrong.
  422. */
  423. if (val > 63)
  424. return -EINVAL;
  425. params->size = 1ULL << val;
  426. } else {
  427. params->size = val + 1;
  428. }
  429. params->size >>= 3; /* Convert to bytes. */
  430. /* Fast Read settings. */
  431. for (i = 0; i < ARRAY_SIZE(sfdp_bfpt_reads); i++) {
  432. const struct sfdp_bfpt_read *rd = &sfdp_bfpt_reads[i];
  433. struct spi_nor_read_command *read;
  434. if (!(bfpt.dwords[rd->supported_dword] & rd->supported_bit)) {
  435. params->hwcaps.mask &= ~rd->hwcaps;
  436. continue;
  437. }
  438. params->hwcaps.mask |= rd->hwcaps;
  439. cmd = spi_nor_hwcaps_read2cmd(rd->hwcaps);
  440. read = &params->reads[cmd];
  441. half = bfpt.dwords[rd->settings_dword] >> rd->settings_shift;
  442. spi_nor_set_read_settings_from_bfpt(read, half, rd->proto);
  443. }
  444. /*
  445. * Sector Erase settings. Reinitialize the uniform erase map using the
  446. * Erase Types defined in the bfpt table.
  447. */
  448. erase_mask = 0;
  449. memset(&params->erase_map, 0, sizeof(params->erase_map));
  450. for (i = 0; i < ARRAY_SIZE(sfdp_bfpt_erases); i++) {
  451. const struct sfdp_bfpt_erase *er = &sfdp_bfpt_erases[i];
  452. u32 erasesize;
  453. u8 opcode;
  454. half = bfpt.dwords[er->dword] >> er->shift;
  455. erasesize = half & 0xff;
  456. /* erasesize == 0 means this Erase Type is not supported. */
  457. if (!erasesize)
  458. continue;
  459. erasesize = 1U << erasesize;
  460. opcode = (half >> 8) & 0xff;
  461. erase_mask |= BIT(i);
  462. spi_nor_set_erase_settings_from_bfpt(&erase_type[i], erasesize,
  463. opcode, i);
  464. }
  465. spi_nor_init_uniform_erase_map(map, erase_mask, params->size);
  466. /*
  467. * Sort all the map's Erase Types in ascending order with the smallest
  468. * erase size being the first member in the erase_type array.
  469. */
  470. sort(erase_type, SNOR_ERASE_TYPE_MAX, sizeof(erase_type[0]),
  471. spi_nor_map_cmp_erase_type, NULL);
  472. /*
  473. * Sort the erase types in the uniform region in order to update the
  474. * uniform_erase_type bitmask. The bitmask will be used later on when
  475. * selecting the uniform erase.
  476. */
  477. spi_nor_regions_sort_erase_types(map);
  478. /* Stop here if not JESD216 rev A or later. */
  479. if (bfpt_header->length == BFPT_DWORD_MAX_JESD216)
  480. return spi_nor_post_bfpt_fixups(nor, bfpt_header, &bfpt);
  481. /* Page size: this field specifies 'N' so the page size = 2^N bytes. */
  482. val = bfpt.dwords[SFDP_DWORD(11)];
  483. val &= BFPT_DWORD11_PAGE_SIZE_MASK;
  484. val >>= BFPT_DWORD11_PAGE_SIZE_SHIFT;
  485. params->page_size = 1U << val;
  486. /* Quad Enable Requirements. */
  487. switch (bfpt.dwords[SFDP_DWORD(15)] & BFPT_DWORD15_QER_MASK) {
  488. case BFPT_DWORD15_QER_NONE:
  489. params->quad_enable = NULL;
  490. break;
  491. case BFPT_DWORD15_QER_SR2_BIT1_BUGGY:
  492. /*
  493. * Writing only one byte to the Status Register has the
  494. * side-effect of clearing Status Register 2.
  495. */
  496. case BFPT_DWORD15_QER_SR2_BIT1_NO_RD:
  497. /*
  498. * Read Configuration Register (35h) instruction is not
  499. * supported.
  500. */
  501. nor->flags |= SNOR_F_HAS_16BIT_SR | SNOR_F_NO_READ_CR;
  502. params->quad_enable = spi_nor_sr2_bit1_quad_enable;
  503. break;
  504. case BFPT_DWORD15_QER_SR1_BIT6:
  505. nor->flags &= ~SNOR_F_HAS_16BIT_SR;
  506. params->quad_enable = spi_nor_sr1_bit6_quad_enable;
  507. break;
  508. case BFPT_DWORD15_QER_SR2_BIT7:
  509. nor->flags &= ~SNOR_F_HAS_16BIT_SR;
  510. params->quad_enable = spi_nor_sr2_bit7_quad_enable;
  511. break;
  512. case BFPT_DWORD15_QER_SR2_BIT1:
  513. /*
  514. * JESD216 rev B or later does not specify if writing only one
  515. * byte to the Status Register clears or not the Status
  516. * Register 2, so let's be cautious and keep the default
  517. * assumption of a 16-bit Write Status (01h) command.
  518. */
  519. nor->flags |= SNOR_F_HAS_16BIT_SR;
  520. params->quad_enable = spi_nor_sr2_bit1_quad_enable;
  521. break;
  522. default:
  523. dev_dbg(nor->dev, "BFPT QER reserved value used\n");
  524. break;
  525. }
  526. dword = bfpt.dwords[SFDP_DWORD(16)] & BFPT_DWORD16_4B_ADDR_MODE_MASK;
  527. if (SFDP_MASK_CHECK(dword, BFPT_DWORD16_4B_ADDR_MODE_BRWR))
  528. params->set_4byte_addr_mode = spi_nor_set_4byte_addr_mode_brwr;
  529. else if (SFDP_MASK_CHECK(dword, BFPT_DWORD16_4B_ADDR_MODE_WREN_EN4B_EX4B))
  530. params->set_4byte_addr_mode = spi_nor_set_4byte_addr_mode_wren_en4b_ex4b;
  531. else if (SFDP_MASK_CHECK(dword, BFPT_DWORD16_4B_ADDR_MODE_EN4B_EX4B))
  532. params->set_4byte_addr_mode = spi_nor_set_4byte_addr_mode_en4b_ex4b;
  533. else
  534. dev_dbg(nor->dev, "BFPT: 4-Byte Address Mode method is not recognized or not implemented\n");
  535. /* Soft Reset support. */
  536. if (bfpt.dwords[SFDP_DWORD(16)] & BFPT_DWORD16_SWRST_EN_RST)
  537. nor->flags |= SNOR_F_SOFT_RESET;
  538. /* Stop here if not JESD216 rev C or later. */
  539. if (bfpt_header->length == BFPT_DWORD_MAX_JESD216B)
  540. return spi_nor_post_bfpt_fixups(nor, bfpt_header, &bfpt);
  541. /* Parse 1-1-8 read instruction */
  542. opcode = FIELD_GET(BFPT_DWORD17_RD_1_1_8_CMD, bfpt.dwords[SFDP_DWORD(17)]);
  543. if (opcode) {
  544. mode_clocks = FIELD_GET(BFPT_DWORD17_RD_1_1_8_MODE_CLOCKS,
  545. bfpt.dwords[SFDP_DWORD(17)]);
  546. wait_states = FIELD_GET(BFPT_DWORD17_RD_1_1_8_WAIT_STATES,
  547. bfpt.dwords[SFDP_DWORD(17)]);
  548. params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_8;
  549. spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_1_1_8],
  550. mode_clocks, wait_states, opcode,
  551. SNOR_PROTO_1_1_8);
  552. }
  553. /* Parse 1-8-8 read instruction */
  554. opcode = FIELD_GET(BFPT_DWORD17_RD_1_8_8_CMD, bfpt.dwords[SFDP_DWORD(17)]);
  555. if (opcode) {
  556. mode_clocks = FIELD_GET(BFPT_DWORD17_RD_1_8_8_MODE_CLOCKS,
  557. bfpt.dwords[SFDP_DWORD(17)]);
  558. wait_states = FIELD_GET(BFPT_DWORD17_RD_1_8_8_WAIT_STATES,
  559. bfpt.dwords[SFDP_DWORD(17)]);
  560. params->hwcaps.mask |= SNOR_HWCAPS_READ_1_8_8;
  561. spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_1_8_8],
  562. mode_clocks, wait_states, opcode,
  563. SNOR_PROTO_1_8_8);
  564. }
  565. /* 8D-8D-8D command extension. */
  566. switch (bfpt.dwords[SFDP_DWORD(18)] & BFPT_DWORD18_CMD_EXT_MASK) {
  567. case BFPT_DWORD18_CMD_EXT_REP:
  568. nor->cmd_ext_type = SPI_NOR_EXT_REPEAT;
  569. break;
  570. case BFPT_DWORD18_CMD_EXT_INV:
  571. nor->cmd_ext_type = SPI_NOR_EXT_INVERT;
  572. break;
  573. case BFPT_DWORD18_CMD_EXT_RES:
  574. dev_dbg(nor->dev, "Reserved command extension used\n");
  575. break;
  576. case BFPT_DWORD18_CMD_EXT_16B:
  577. dev_dbg(nor->dev, "16-bit opcodes not supported\n");
  578. return -EOPNOTSUPP;
  579. }
  580. /* Byte order in 8D-8D-8D mode */
  581. if (bfpt.dwords[SFDP_DWORD(18)] & BFPT_DWORD18_BYTE_ORDER_SWAPPED)
  582. nor->flags |= SNOR_F_SWAP16;
  583. return spi_nor_post_bfpt_fixups(nor, bfpt_header, &bfpt);
  584. }
  585. /**
  586. * spi_nor_smpt_addr_nbytes() - return the number of address bytes used in the
  587. * configuration detection command.
  588. * @nor: pointer to a 'struct spi_nor'
  589. * @settings: configuration detection command descriptor, dword1
  590. */
  591. static u8 spi_nor_smpt_addr_nbytes(const struct spi_nor *nor, const u32 settings)
  592. {
  593. switch (settings & SMPT_CMD_ADDRESS_LEN_MASK) {
  594. case SMPT_CMD_ADDRESS_LEN_0:
  595. return 0;
  596. case SMPT_CMD_ADDRESS_LEN_3:
  597. return 3;
  598. case SMPT_CMD_ADDRESS_LEN_4:
  599. return 4;
  600. case SMPT_CMD_ADDRESS_LEN_USE_CURRENT:
  601. default:
  602. return nor->params->addr_mode_nbytes;
  603. }
  604. }
  605. static void spi_nor_smpt_read_dummy_fixups(const struct spi_nor *nor,
  606. u8 *read_dummy)
  607. {
  608. if (nor->manufacturer && nor->manufacturer->fixups &&
  609. nor->manufacturer->fixups->smpt_read_dummy)
  610. nor->manufacturer->fixups->smpt_read_dummy(nor, read_dummy);
  611. if (nor->info->fixups && nor->info->fixups->smpt_read_dummy)
  612. nor->info->fixups->smpt_read_dummy(nor, read_dummy);
  613. }
  614. /**
  615. * spi_nor_smpt_read_dummy() - return the configuration detection command read
  616. * latency, in clock cycles.
  617. * @nor: pointer to a 'struct spi_nor'
  618. * @settings: configuration detection command descriptor, dword1
  619. *
  620. * Return: the number of dummy cycles for an SMPT read
  621. */
  622. static u8 spi_nor_smpt_read_dummy(const struct spi_nor *nor, const u32 settings)
  623. {
  624. u8 read_dummy = SMPT_CMD_READ_DUMMY(settings);
  625. if (read_dummy == SMPT_CMD_READ_DUMMY_IS_VARIABLE) {
  626. read_dummy = nor->read_dummy;
  627. spi_nor_smpt_read_dummy_fixups(nor, &read_dummy);
  628. }
  629. return read_dummy;
  630. }
  631. static void spi_nor_smpt_map_id_fixups(const struct spi_nor *nor, u8 *map_id)
  632. {
  633. if (nor->manufacturer && nor->manufacturer->fixups &&
  634. nor->manufacturer->fixups->smpt_map_id)
  635. nor->manufacturer->fixups->smpt_map_id(nor, map_id);
  636. if (nor->info->fixups && nor->info->fixups->smpt_map_id)
  637. nor->info->fixups->smpt_map_id(nor, map_id);
  638. }
  639. /**
  640. * spi_nor_get_map_in_use() - get the configuration map in use
  641. * @nor: pointer to a 'struct spi_nor'
  642. * @smpt: pointer to the sector map parameter table
  643. * @smpt_len: sector map parameter table length
  644. *
  645. * Return: pointer to the map in use, ERR_PTR(-errno) otherwise.
  646. */
  647. static const u32 *spi_nor_get_map_in_use(struct spi_nor *nor, const u32 *smpt,
  648. u8 smpt_len)
  649. {
  650. const u32 *ret;
  651. u8 *buf;
  652. u32 addr;
  653. int err;
  654. u8 i;
  655. u8 addr_nbytes, read_opcode, read_dummy;
  656. u8 read_data_mask, map_id;
  657. /* Use a kmalloc'ed bounce buffer to guarantee it is DMA-able. */
  658. buf = kmalloc_obj(*buf);
  659. if (!buf)
  660. return ERR_PTR(-ENOMEM);
  661. addr_nbytes = nor->addr_nbytes;
  662. read_dummy = nor->read_dummy;
  663. read_opcode = nor->read_opcode;
  664. map_id = 0;
  665. /* Determine if there are any optional Detection Command Descriptors */
  666. for (i = 0; i < smpt_len; i += 2) {
  667. if (smpt[i] & SMPT_DESC_TYPE_MAP)
  668. break;
  669. read_data_mask = SMPT_CMD_READ_DATA(smpt[i]);
  670. nor->addr_nbytes = spi_nor_smpt_addr_nbytes(nor, smpt[i]);
  671. nor->read_dummy = spi_nor_smpt_read_dummy(nor, smpt[i]);
  672. nor->read_opcode = SMPT_CMD_OPCODE(smpt[i]);
  673. addr = smpt[i + 1];
  674. err = spi_nor_read_raw(nor, addr, 1, buf);
  675. if (err) {
  676. ret = ERR_PTR(err);
  677. goto out;
  678. }
  679. /*
  680. * Build an index value that is used to select the Sector Map
  681. * Configuration that is currently in use.
  682. */
  683. map_id = map_id << 1 | !!(*buf & read_data_mask);
  684. }
  685. spi_nor_smpt_map_id_fixups(nor, &map_id);
  686. /*
  687. * If command descriptors are provided, they always precede map
  688. * descriptors in the table. There is no need to start the iteration
  689. * over smpt array all over again.
  690. *
  691. * Find the matching configuration map.
  692. */
  693. ret = ERR_PTR(-EINVAL);
  694. while (i < smpt_len) {
  695. if (SMPT_MAP_ID(smpt[i]) == map_id) {
  696. ret = smpt + i;
  697. break;
  698. }
  699. /*
  700. * If there are no more configuration map descriptors and no
  701. * configuration ID matched the configuration identifier, the
  702. * sector address map is unknown.
  703. */
  704. if (smpt[i] & SMPT_DESC_END)
  705. break;
  706. /* increment the table index to the next map */
  707. i += SMPT_MAP_REGION_COUNT(smpt[i]) + 1;
  708. }
  709. /* fall through */
  710. out:
  711. kfree(buf);
  712. nor->addr_nbytes = addr_nbytes;
  713. nor->read_dummy = read_dummy;
  714. nor->read_opcode = read_opcode;
  715. return ret;
  716. }
  717. /**
  718. * spi_nor_region_check_overlay() - set overlay bit when the region is overlaid
  719. * @region: pointer to a structure that describes a SPI NOR erase region
  720. * @erase: pointer to a structure that describes a SPI NOR erase type
  721. * @erase_type: erase type bitmask
  722. */
  723. static void
  724. spi_nor_region_check_overlay(struct spi_nor_erase_region *region,
  725. const struct spi_nor_erase_type *erase,
  726. const u8 erase_type)
  727. {
  728. int i;
  729. for (i = 0; i < SNOR_ERASE_TYPE_MAX; i++) {
  730. if (!(erase[i].size && erase_type & BIT(erase[i].idx)))
  731. continue;
  732. if (region->size & erase[i].size_mask) {
  733. region->overlaid = true;
  734. return;
  735. }
  736. }
  737. }
  738. /**
  739. * spi_nor_init_non_uniform_erase_map() - initialize the non-uniform erase map
  740. * @nor: pointer to a 'struct spi_nor'
  741. * @smpt: pointer to the sector map parameter table
  742. *
  743. * Return: 0 on success, -errno otherwise.
  744. */
  745. static int spi_nor_init_non_uniform_erase_map(struct spi_nor *nor,
  746. const u32 *smpt)
  747. {
  748. struct spi_nor_erase_map *map = &nor->params->erase_map;
  749. struct spi_nor_erase_type *erase = map->erase_type;
  750. struct spi_nor_erase_region *region;
  751. u64 offset;
  752. u32 region_count;
  753. int i, j;
  754. u8 uniform_erase_type, save_uniform_erase_type;
  755. u8 erase_type, regions_erase_type;
  756. region_count = SMPT_MAP_REGION_COUNT(*smpt);
  757. /*
  758. * The regions will be freed when the driver detaches from the
  759. * device.
  760. */
  761. region = devm_kcalloc(nor->dev, region_count, sizeof(*region),
  762. GFP_KERNEL);
  763. if (!region)
  764. return -ENOMEM;
  765. map->regions = region;
  766. map->n_regions = region_count;
  767. uniform_erase_type = 0xff;
  768. regions_erase_type = 0;
  769. offset = 0;
  770. /* Populate regions. */
  771. for (i = 0; i < region_count; i++) {
  772. j = i + 1; /* index for the region dword */
  773. region[i].offset = offset;
  774. region[i].size = SMPT_MAP_REGION_SIZE(smpt[j]);
  775. erase_type = SMPT_MAP_REGION_ERASE_TYPE(smpt[j]);
  776. region[i].erase_mask = erase_type;
  777. spi_nor_region_check_overlay(&region[i], erase, erase_type);
  778. /*
  779. * Save the erase types that are supported in all regions and
  780. * can erase the entire flash memory.
  781. */
  782. uniform_erase_type &= erase_type;
  783. /*
  784. * regions_erase_type mask will indicate all the erase types
  785. * supported in this configuration map.
  786. */
  787. regions_erase_type |= erase_type;
  788. offset = region[i].offset + region[i].size;
  789. }
  790. save_uniform_erase_type = map->uniform_region.erase_mask;
  791. map->uniform_region.erase_mask =
  792. spi_nor_sort_erase_mask(map,
  793. uniform_erase_type);
  794. if (!regions_erase_type) {
  795. /*
  796. * Roll back to the previous uniform_erase_type mask, SMPT is
  797. * broken.
  798. */
  799. map->uniform_region.erase_mask = save_uniform_erase_type;
  800. return -EINVAL;
  801. }
  802. /*
  803. * BFPT advertises all the erase types supported by all the possible
  804. * map configurations. Mask out the erase types that are not supported
  805. * by the current map configuration.
  806. */
  807. for (i = 0; i < SNOR_ERASE_TYPE_MAX; i++)
  808. if (!(regions_erase_type & BIT(erase[i].idx)))
  809. spi_nor_mask_erase_type(&erase[i]);
  810. return 0;
  811. }
  812. /**
  813. * spi_nor_parse_smpt() - parse Sector Map Parameter Table
  814. * @nor: pointer to a 'struct spi_nor'
  815. * @smpt_header: sector map parameter table header
  816. *
  817. * This table is optional, but when available, we parse it to identify the
  818. * location and size of sectors within the main data array of the flash memory
  819. * device and to identify which Erase Types are supported by each sector.
  820. *
  821. * Return: 0 on success, -errno otherwise.
  822. */
  823. static int spi_nor_parse_smpt(struct spi_nor *nor,
  824. const struct sfdp_parameter_header *smpt_header)
  825. {
  826. const u32 *sector_map;
  827. u32 *smpt;
  828. size_t len;
  829. u32 addr;
  830. int ret;
  831. /* Read the Sector Map Parameter Table. */
  832. len = smpt_header->length * sizeof(*smpt);
  833. smpt = kmalloc(len, GFP_KERNEL);
  834. if (!smpt)
  835. return -ENOMEM;
  836. addr = SFDP_PARAM_HEADER_PTP(smpt_header);
  837. ret = spi_nor_read_sfdp(nor, addr, len, smpt);
  838. if (ret)
  839. goto out;
  840. /* Fix endianness of the SMPT DWORDs. */
  841. le32_to_cpu_array(smpt, smpt_header->length);
  842. sector_map = spi_nor_get_map_in_use(nor, smpt, smpt_header->length);
  843. if (IS_ERR(sector_map)) {
  844. ret = PTR_ERR(sector_map);
  845. goto out;
  846. }
  847. ret = spi_nor_init_non_uniform_erase_map(nor, sector_map);
  848. if (ret)
  849. goto out;
  850. spi_nor_regions_sort_erase_types(&nor->params->erase_map);
  851. /* fall through */
  852. out:
  853. kfree(smpt);
  854. return ret;
  855. }
  856. /**
  857. * spi_nor_parse_4bait() - parse the 4-Byte Address Instruction Table
  858. * @nor: pointer to a 'struct spi_nor'.
  859. * @param_header: pointer to the 'struct sfdp_parameter_header' describing
  860. * the 4-Byte Address Instruction Table length and version.
  861. *
  862. * Return: 0 on success, -errno otherwise.
  863. */
  864. static int spi_nor_parse_4bait(struct spi_nor *nor,
  865. const struct sfdp_parameter_header *param_header)
  866. {
  867. static const struct sfdp_4bait reads[] = {
  868. { SNOR_HWCAPS_READ, BIT(0) },
  869. { SNOR_HWCAPS_READ_FAST, BIT(1) },
  870. { SNOR_HWCAPS_READ_1_1_2, BIT(2) },
  871. { SNOR_HWCAPS_READ_1_2_2, BIT(3) },
  872. { SNOR_HWCAPS_READ_1_1_4, BIT(4) },
  873. { SNOR_HWCAPS_READ_1_4_4, BIT(5) },
  874. { SNOR_HWCAPS_READ_1_1_1_DTR, BIT(13) },
  875. { SNOR_HWCAPS_READ_1_2_2_DTR, BIT(14) },
  876. { SNOR_HWCAPS_READ_1_4_4_DTR, BIT(15) },
  877. { SNOR_HWCAPS_READ_1_1_8, BIT(20) },
  878. { SNOR_HWCAPS_READ_1_8_8, BIT(21) },
  879. };
  880. static const struct sfdp_4bait programs[] = {
  881. { SNOR_HWCAPS_PP, BIT(6) },
  882. { SNOR_HWCAPS_PP_1_1_4, BIT(7) },
  883. { SNOR_HWCAPS_PP_1_4_4, BIT(8) },
  884. };
  885. static const struct sfdp_4bait erases[SNOR_ERASE_TYPE_MAX] = {
  886. { 0u /* not used */, BIT(9) },
  887. { 0u /* not used */, BIT(10) },
  888. { 0u /* not used */, BIT(11) },
  889. { 0u /* not used */, BIT(12) },
  890. };
  891. struct spi_nor_flash_parameter *params = nor->params;
  892. struct spi_nor_pp_command *params_pp = params->page_programs;
  893. struct spi_nor_erase_map *map = &params->erase_map;
  894. struct spi_nor_erase_type *erase_type = map->erase_type;
  895. u32 *dwords;
  896. size_t len;
  897. u32 addr, discard_hwcaps, read_hwcaps, pp_hwcaps, erase_mask;
  898. int i, ret;
  899. if (param_header->major != SFDP_JESD216_MAJOR ||
  900. param_header->length < SFDP_4BAIT_DWORD_MAX)
  901. return -EINVAL;
  902. /* Read the 4-byte Address Instruction Table. */
  903. len = sizeof(*dwords) * SFDP_4BAIT_DWORD_MAX;
  904. /* Use a kmalloc'ed bounce buffer to guarantee it is DMA-able. */
  905. dwords = kmalloc(len, GFP_KERNEL);
  906. if (!dwords)
  907. return -ENOMEM;
  908. addr = SFDP_PARAM_HEADER_PTP(param_header);
  909. ret = spi_nor_read_sfdp(nor, addr, len, dwords);
  910. if (ret)
  911. goto out;
  912. /* Fix endianness of the 4BAIT DWORDs. */
  913. le32_to_cpu_array(dwords, SFDP_4BAIT_DWORD_MAX);
  914. /*
  915. * Compute the subset of (Fast) Read commands for which the 4-byte
  916. * version is supported.
  917. */
  918. discard_hwcaps = 0;
  919. read_hwcaps = 0;
  920. for (i = 0; i < ARRAY_SIZE(reads); i++) {
  921. const struct sfdp_4bait *read = &reads[i];
  922. discard_hwcaps |= read->hwcaps;
  923. if ((params->hwcaps.mask & read->hwcaps) &&
  924. (dwords[SFDP_DWORD(1)] & read->supported_bit))
  925. read_hwcaps |= read->hwcaps;
  926. }
  927. /*
  928. * Compute the subset of Page Program commands for which the 4-byte
  929. * version is supported.
  930. */
  931. pp_hwcaps = 0;
  932. for (i = 0; i < ARRAY_SIZE(programs); i++) {
  933. const struct sfdp_4bait *program = &programs[i];
  934. /*
  935. * The 4 Byte Address Instruction (Optional) Table is the only
  936. * SFDP table that indicates support for Page Program Commands.
  937. * Bypass the params->hwcaps.mask and consider 4BAIT the biggest
  938. * authority for specifying Page Program support.
  939. */
  940. discard_hwcaps |= program->hwcaps;
  941. if (dwords[SFDP_DWORD(1)] & program->supported_bit)
  942. pp_hwcaps |= program->hwcaps;
  943. }
  944. /*
  945. * Compute the subset of Sector Erase commands for which the 4-byte
  946. * version is supported.
  947. */
  948. erase_mask = 0;
  949. for (i = 0; i < SNOR_ERASE_TYPE_MAX; i++) {
  950. const struct sfdp_4bait *erase = &erases[i];
  951. if (dwords[SFDP_DWORD(1)] & erase->supported_bit)
  952. erase_mask |= BIT(i);
  953. }
  954. /* Replicate the sort done for the map's erase types in BFPT. */
  955. erase_mask = spi_nor_sort_erase_mask(map, erase_mask);
  956. /*
  957. * We need at least one 4-byte op code per read, program and erase
  958. * operation; the .read(), .write() and .erase() hooks share the
  959. * nor->addr_nbytes value.
  960. */
  961. if (!read_hwcaps || !pp_hwcaps || !erase_mask)
  962. goto out;
  963. /*
  964. * Discard all operations from the 4-byte instruction set which are
  965. * not supported by this memory.
  966. */
  967. params->hwcaps.mask &= ~discard_hwcaps;
  968. params->hwcaps.mask |= (read_hwcaps | pp_hwcaps);
  969. /* Use the 4-byte address instruction set. */
  970. for (i = 0; i < SNOR_CMD_READ_MAX; i++) {
  971. struct spi_nor_read_command *read_cmd = &params->reads[i];
  972. read_cmd->opcode = spi_nor_convert_3to4_read(read_cmd->opcode);
  973. }
  974. /* 4BAIT is the only SFDP table that indicates page program support. */
  975. if (pp_hwcaps & SNOR_HWCAPS_PP) {
  976. spi_nor_set_pp_settings(&params_pp[SNOR_CMD_PP],
  977. SPINOR_OP_PP_4B, SNOR_PROTO_1_1_1);
  978. /*
  979. * Since xSPI Page Program opcode is backward compatible with
  980. * Legacy SPI, use Legacy SPI opcode there as well.
  981. */
  982. spi_nor_set_pp_settings(&params_pp[SNOR_CMD_PP_8_8_8_DTR],
  983. SPINOR_OP_PP_4B, SNOR_PROTO_8_8_8_DTR);
  984. }
  985. if (pp_hwcaps & SNOR_HWCAPS_PP_1_1_4)
  986. spi_nor_set_pp_settings(&params_pp[SNOR_CMD_PP_1_1_4],
  987. SPINOR_OP_PP_1_1_4_4B,
  988. SNOR_PROTO_1_1_4);
  989. if (pp_hwcaps & SNOR_HWCAPS_PP_1_4_4)
  990. spi_nor_set_pp_settings(&params_pp[SNOR_CMD_PP_1_4_4],
  991. SPINOR_OP_PP_1_4_4_4B,
  992. SNOR_PROTO_1_4_4);
  993. for (i = 0; i < SNOR_ERASE_TYPE_MAX; i++) {
  994. if (erase_mask & BIT(i))
  995. erase_type[i].opcode = (dwords[SFDP_DWORD(2)] >>
  996. erase_type[i].idx * 8) & 0xFF;
  997. else
  998. spi_nor_mask_erase_type(&erase_type[i]);
  999. }
  1000. /*
  1001. * We set SNOR_F_HAS_4BAIT in order to skip spi_nor_set_4byte_opcodes()
  1002. * later because we already did the conversion to 4byte opcodes. Also,
  1003. * this latest function implements a legacy quirk for the erase size of
  1004. * Spansion memory. However this quirk is no longer needed with new
  1005. * SFDP compliant memories.
  1006. */
  1007. params->addr_nbytes = 4;
  1008. nor->flags |= SNOR_F_4B_OPCODES | SNOR_F_HAS_4BAIT;
  1009. /* fall through */
  1010. out:
  1011. kfree(dwords);
  1012. return ret;
  1013. }
  1014. #define PROFILE1_DWORD1_RDSR_ADDR_BYTES BIT(29)
  1015. #define PROFILE1_DWORD1_RDSR_DUMMY BIT(28)
  1016. #define PROFILE1_DWORD1_RD_FAST_CMD GENMASK(15, 8)
  1017. #define PROFILE1_DWORD4_DUMMY_200MHZ GENMASK(11, 7)
  1018. #define PROFILE1_DWORD5_DUMMY_166MHZ GENMASK(31, 27)
  1019. #define PROFILE1_DWORD5_DUMMY_133MHZ GENMASK(21, 17)
  1020. #define PROFILE1_DWORD5_DUMMY_100MHZ GENMASK(11, 7)
  1021. /**
  1022. * spi_nor_parse_profile1() - parse the xSPI Profile 1.0 table
  1023. * @nor: pointer to a 'struct spi_nor'
  1024. * @profile1_header: pointer to the 'struct sfdp_parameter_header' describing
  1025. * the Profile 1.0 Table length and version.
  1026. *
  1027. * Return: 0 on success, -errno otherwise.
  1028. */
  1029. static int spi_nor_parse_profile1(struct spi_nor *nor,
  1030. const struct sfdp_parameter_header *profile1_header)
  1031. {
  1032. u32 *dwords, addr;
  1033. size_t len;
  1034. int ret;
  1035. u8 dummy, opcode;
  1036. len = profile1_header->length * sizeof(*dwords);
  1037. dwords = kmalloc(len, GFP_KERNEL);
  1038. if (!dwords)
  1039. return -ENOMEM;
  1040. addr = SFDP_PARAM_HEADER_PTP(profile1_header);
  1041. ret = spi_nor_read_sfdp(nor, addr, len, dwords);
  1042. if (ret)
  1043. goto out;
  1044. le32_to_cpu_array(dwords, profile1_header->length);
  1045. /* Get 8D-8D-8D fast read opcode and dummy cycles. */
  1046. opcode = FIELD_GET(PROFILE1_DWORD1_RD_FAST_CMD, dwords[SFDP_DWORD(1)]);
  1047. /* Set the Read Status Register dummy cycles and dummy address bytes. */
  1048. if (dwords[SFDP_DWORD(1)] & PROFILE1_DWORD1_RDSR_DUMMY)
  1049. nor->params->rdsr_dummy = 8;
  1050. else
  1051. nor->params->rdsr_dummy = 4;
  1052. if (dwords[SFDP_DWORD(1)] & PROFILE1_DWORD1_RDSR_ADDR_BYTES)
  1053. nor->params->rdsr_addr_nbytes = 4;
  1054. else
  1055. nor->params->rdsr_addr_nbytes = 0;
  1056. /*
  1057. * We don't know what speed the controller is running at. Find the
  1058. * dummy cycles for the fastest frequency the flash can run at to be
  1059. * sure we are never short of dummy cycles. A value of 0 means the
  1060. * frequency is not supported.
  1061. *
  1062. * Default to PROFILE1_DUMMY_DEFAULT if we don't find anything, and let
  1063. * flashes set the correct value if needed in their fixup hooks.
  1064. */
  1065. dummy = FIELD_GET(PROFILE1_DWORD4_DUMMY_200MHZ, dwords[SFDP_DWORD(4)]);
  1066. if (!dummy)
  1067. dummy = FIELD_GET(PROFILE1_DWORD5_DUMMY_166MHZ,
  1068. dwords[SFDP_DWORD(5)]);
  1069. if (!dummy)
  1070. dummy = FIELD_GET(PROFILE1_DWORD5_DUMMY_133MHZ,
  1071. dwords[SFDP_DWORD(5)]);
  1072. if (!dummy)
  1073. dummy = FIELD_GET(PROFILE1_DWORD5_DUMMY_100MHZ,
  1074. dwords[SFDP_DWORD(5)]);
  1075. if (!dummy)
  1076. dev_dbg(nor->dev,
  1077. "Can't find dummy cycles from Profile 1.0 table\n");
  1078. /* Round up to an even value to avoid tripping controllers up. */
  1079. dummy = round_up(dummy, 2);
  1080. /* Update the fast read settings. */
  1081. nor->params->hwcaps.mask |= SNOR_HWCAPS_READ_8_8_8_DTR;
  1082. spi_nor_set_read_settings(&nor->params->reads[SNOR_CMD_READ_8_8_8_DTR],
  1083. 0, dummy, opcode,
  1084. SNOR_PROTO_8_8_8_DTR);
  1085. /*
  1086. * Page Program is "Required Command" in the xSPI Profile 1.0. Update
  1087. * the params->hwcaps.mask here.
  1088. */
  1089. nor->params->hwcaps.mask |= SNOR_HWCAPS_PP_8_8_8_DTR;
  1090. out:
  1091. kfree(dwords);
  1092. return ret;
  1093. }
  1094. #define SCCR_DWORD22_OCTAL_DTR_EN_VOLATILE BIT(31)
  1095. /**
  1096. * spi_nor_parse_sccr() - Parse the Status, Control and Configuration Register
  1097. * Map.
  1098. * @nor: pointer to a 'struct spi_nor'
  1099. * @sccr_header: pointer to the 'struct sfdp_parameter_header' describing
  1100. * the SCCR Map table length and version.
  1101. *
  1102. * Return: 0 on success, -errno otherwise.
  1103. */
  1104. static int spi_nor_parse_sccr(struct spi_nor *nor,
  1105. const struct sfdp_parameter_header *sccr_header)
  1106. {
  1107. struct spi_nor_flash_parameter *params = nor->params;
  1108. u32 *dwords, addr;
  1109. size_t len;
  1110. int ret;
  1111. len = sccr_header->length * sizeof(*dwords);
  1112. dwords = kmalloc(len, GFP_KERNEL);
  1113. if (!dwords)
  1114. return -ENOMEM;
  1115. addr = SFDP_PARAM_HEADER_PTP(sccr_header);
  1116. ret = spi_nor_read_sfdp(nor, addr, len, dwords);
  1117. if (ret)
  1118. goto out;
  1119. le32_to_cpu_array(dwords, sccr_header->length);
  1120. /* Address offset for volatile registers (die 0) */
  1121. if (!params->vreg_offset) {
  1122. params->vreg_offset = devm_kmalloc(nor->dev, sizeof(*dwords),
  1123. GFP_KERNEL);
  1124. if (!params->vreg_offset) {
  1125. ret = -ENOMEM;
  1126. goto out;
  1127. }
  1128. }
  1129. params->vreg_offset[0] = dwords[SFDP_DWORD(1)];
  1130. params->n_dice = 1;
  1131. if (FIELD_GET(SCCR_DWORD22_OCTAL_DTR_EN_VOLATILE,
  1132. dwords[SFDP_DWORD(22)]))
  1133. nor->flags |= SNOR_F_IO_MODE_EN_VOLATILE;
  1134. out:
  1135. kfree(dwords);
  1136. return ret;
  1137. }
  1138. /**
  1139. * spi_nor_parse_sccr_mc() - Parse the Status, Control and Configuration
  1140. * Register Map Offsets for Multi-Chip SPI Memory
  1141. * Devices.
  1142. * @nor: pointer to a 'struct spi_nor'
  1143. * @sccr_mc_header: pointer to the 'struct sfdp_parameter_header' describing
  1144. * the SCCR Map offsets table length and version.
  1145. *
  1146. * Return: 0 on success, -errno otherwise.
  1147. */
  1148. static int spi_nor_parse_sccr_mc(struct spi_nor *nor,
  1149. const struct sfdp_parameter_header *sccr_mc_header)
  1150. {
  1151. struct spi_nor_flash_parameter *params = nor->params;
  1152. u32 *dwords, addr;
  1153. u8 i, n_dice;
  1154. size_t len;
  1155. int ret;
  1156. len = sccr_mc_header->length * sizeof(*dwords);
  1157. dwords = kmalloc(len, GFP_KERNEL);
  1158. if (!dwords)
  1159. return -ENOMEM;
  1160. addr = SFDP_PARAM_HEADER_PTP(sccr_mc_header);
  1161. ret = spi_nor_read_sfdp(nor, addr, len, dwords);
  1162. if (ret)
  1163. goto out;
  1164. le32_to_cpu_array(dwords, sccr_mc_header->length);
  1165. /*
  1166. * Pair of DOWRDs (volatile and non-volatile register offsets) per
  1167. * additional die. Hence, length = 2 * (number of additional dice).
  1168. */
  1169. n_dice = 1 + sccr_mc_header->length / 2;
  1170. /* Address offset for volatile registers of additional dice */
  1171. params->vreg_offset =
  1172. devm_krealloc(nor->dev, params->vreg_offset,
  1173. n_dice * sizeof(*dwords),
  1174. GFP_KERNEL);
  1175. if (!params->vreg_offset) {
  1176. ret = -ENOMEM;
  1177. goto out;
  1178. }
  1179. for (i = 1; i < n_dice; i++)
  1180. params->vreg_offset[i] = dwords[SFDP_DWORD(i) * 2];
  1181. params->n_dice = n_dice;
  1182. out:
  1183. kfree(dwords);
  1184. return ret;
  1185. }
  1186. /**
  1187. * spi_nor_post_sfdp_fixups() - Updates the flash's parameters and settings
  1188. * after SFDP has been parsed. Called only for flashes that define JESD216 SFDP
  1189. * tables.
  1190. * @nor: pointer to a 'struct spi_nor'
  1191. *
  1192. * Used to tweak various flash parameters when information provided by the SFDP
  1193. * tables are wrong.
  1194. */
  1195. static int spi_nor_post_sfdp_fixups(struct spi_nor *nor)
  1196. {
  1197. int ret;
  1198. if (nor->manufacturer && nor->manufacturer->fixups &&
  1199. nor->manufacturer->fixups->post_sfdp) {
  1200. ret = nor->manufacturer->fixups->post_sfdp(nor);
  1201. if (ret)
  1202. return ret;
  1203. }
  1204. if (nor->info->fixups && nor->info->fixups->post_sfdp)
  1205. return nor->info->fixups->post_sfdp(nor);
  1206. return 0;
  1207. }
  1208. /**
  1209. * spi_nor_check_sfdp_signature() - check for a valid SFDP signature
  1210. * @nor: pointer to a 'struct spi_nor'
  1211. *
  1212. * Used to detect if the flash supports the RDSFDP command as well as the
  1213. * presence of a valid SFDP table.
  1214. *
  1215. * Return: 0 on success, -errno otherwise.
  1216. */
  1217. int spi_nor_check_sfdp_signature(struct spi_nor *nor)
  1218. {
  1219. u32 signature;
  1220. int err;
  1221. /* Get the SFDP header. */
  1222. err = spi_nor_read_sfdp_dma_unsafe(nor, 0, sizeof(signature),
  1223. &signature);
  1224. if (err < 0)
  1225. return err;
  1226. /* Check the SFDP signature. */
  1227. if (le32_to_cpu(signature) != SFDP_SIGNATURE)
  1228. return -EINVAL;
  1229. return 0;
  1230. }
  1231. /**
  1232. * spi_nor_parse_sfdp() - parse the Serial Flash Discoverable Parameters.
  1233. * @nor: pointer to a 'struct spi_nor'
  1234. *
  1235. * The Serial Flash Discoverable Parameters are described by the JEDEC JESD216
  1236. * specification. This is a standard which tends to supported by almost all
  1237. * (Q)SPI memory manufacturers. Those hard-coded tables allow us to learn at
  1238. * runtime the main parameters needed to perform basic SPI flash operations such
  1239. * as Fast Read, Page Program or Sector Erase commands.
  1240. *
  1241. * Return: 0 on success, -errno otherwise.
  1242. */
  1243. int spi_nor_parse_sfdp(struct spi_nor *nor)
  1244. {
  1245. const struct sfdp_parameter_header *param_header, *bfpt_header;
  1246. struct sfdp_parameter_header *param_headers = NULL;
  1247. struct sfdp_header header;
  1248. struct device *dev = nor->dev;
  1249. struct sfdp *sfdp;
  1250. size_t sfdp_size;
  1251. size_t psize;
  1252. int i, err;
  1253. /* Get the SFDP header. */
  1254. err = spi_nor_read_sfdp_dma_unsafe(nor, 0, sizeof(header), &header);
  1255. if (err < 0)
  1256. return err;
  1257. /* Check the SFDP header version. */
  1258. if (le32_to_cpu(header.signature) != SFDP_SIGNATURE ||
  1259. header.major != SFDP_JESD216_MAJOR)
  1260. return -EINVAL;
  1261. /*
  1262. * Verify that the first and only mandatory parameter header is a
  1263. * Basic Flash Parameter Table header as specified in JESD216.
  1264. */
  1265. bfpt_header = &header.bfpt_header;
  1266. if (SFDP_PARAM_HEADER_ID(bfpt_header) != SFDP_BFPT_ID ||
  1267. bfpt_header->major != SFDP_JESD216_MAJOR)
  1268. return -EINVAL;
  1269. sfdp_size = SFDP_PARAM_HEADER_PTP(bfpt_header) +
  1270. SFDP_PARAM_HEADER_PARAM_LEN(bfpt_header);
  1271. /*
  1272. * Allocate memory then read all parameter headers with a single
  1273. * Read SFDP command. These parameter headers will actually be parsed
  1274. * twice: a first time to get the latest revision of the basic flash
  1275. * parameter table, then a second time to handle the supported optional
  1276. * tables.
  1277. * Hence we read the parameter headers once for all to reduce the
  1278. * processing time. Also we use kmalloc() instead of devm_kmalloc()
  1279. * because we don't need to keep these parameter headers: the allocated
  1280. * memory is always released with kfree() before exiting this function.
  1281. */
  1282. if (header.nph) {
  1283. psize = header.nph * sizeof(*param_headers);
  1284. param_headers = kmalloc(psize, GFP_KERNEL);
  1285. if (!param_headers)
  1286. return -ENOMEM;
  1287. err = spi_nor_read_sfdp(nor, sizeof(header),
  1288. psize, param_headers);
  1289. if (err < 0) {
  1290. dev_dbg(dev, "failed to read SFDP parameter headers\n");
  1291. goto exit;
  1292. }
  1293. }
  1294. /*
  1295. * Cache the complete SFDP data. It is not (easily) possible to fetch
  1296. * SFDP after probe time and we need it for the sysfs access.
  1297. */
  1298. for (i = 0; i < header.nph; i++) {
  1299. param_header = &param_headers[i];
  1300. sfdp_size = max_t(size_t, sfdp_size,
  1301. SFDP_PARAM_HEADER_PTP(param_header) +
  1302. SFDP_PARAM_HEADER_PARAM_LEN(param_header));
  1303. }
  1304. /*
  1305. * Limit the total size to a reasonable value to avoid allocating too
  1306. * much memory just of because the flash returned some insane values.
  1307. */
  1308. if (sfdp_size > PAGE_SIZE) {
  1309. dev_dbg(dev, "SFDP data (%zu) too big, truncating\n",
  1310. sfdp_size);
  1311. sfdp_size = PAGE_SIZE;
  1312. }
  1313. sfdp = devm_kzalloc(dev, sizeof(*sfdp), GFP_KERNEL);
  1314. if (!sfdp) {
  1315. err = -ENOMEM;
  1316. goto exit;
  1317. }
  1318. /*
  1319. * The SFDP is organized in chunks of DWORDs. Thus, in theory, the
  1320. * sfdp_size should be a multiple of DWORDs. But in case a flash
  1321. * is not spec compliant, make sure that we have enough space to store
  1322. * the complete SFDP data.
  1323. */
  1324. sfdp->num_dwords = DIV_ROUND_UP(sfdp_size, sizeof(*sfdp->dwords));
  1325. sfdp->dwords = devm_kcalloc(dev, sfdp->num_dwords,
  1326. sizeof(*sfdp->dwords), GFP_KERNEL);
  1327. if (!sfdp->dwords) {
  1328. err = -ENOMEM;
  1329. devm_kfree(dev, sfdp);
  1330. goto exit;
  1331. }
  1332. err = spi_nor_read_sfdp(nor, 0, sfdp_size, sfdp->dwords);
  1333. if (err < 0) {
  1334. dev_dbg(dev, "failed to read SFDP data\n");
  1335. devm_kfree(dev, sfdp->dwords);
  1336. devm_kfree(dev, sfdp);
  1337. goto exit;
  1338. }
  1339. nor->sfdp = sfdp;
  1340. /*
  1341. * Check other parameter headers to get the latest revision of
  1342. * the basic flash parameter table.
  1343. */
  1344. for (i = 0; i < header.nph; i++) {
  1345. param_header = &param_headers[i];
  1346. if (SFDP_PARAM_HEADER_ID(param_header) == SFDP_BFPT_ID &&
  1347. param_header->major == SFDP_JESD216_MAJOR &&
  1348. (param_header->minor > bfpt_header->minor ||
  1349. (param_header->minor == bfpt_header->minor &&
  1350. param_header->length > bfpt_header->length)))
  1351. bfpt_header = param_header;
  1352. }
  1353. err = spi_nor_parse_bfpt(nor, bfpt_header);
  1354. if (err)
  1355. goto exit;
  1356. /* Parse optional parameter tables. */
  1357. for (i = 0; i < header.nph; i++) {
  1358. param_header = &param_headers[i];
  1359. switch (SFDP_PARAM_HEADER_ID(param_header)) {
  1360. case SFDP_SECTOR_MAP_ID:
  1361. err = spi_nor_parse_smpt(nor, param_header);
  1362. break;
  1363. case SFDP_4BAIT_ID:
  1364. err = spi_nor_parse_4bait(nor, param_header);
  1365. break;
  1366. case SFDP_PROFILE1_ID:
  1367. err = spi_nor_parse_profile1(nor, param_header);
  1368. break;
  1369. case SFDP_SCCR_MAP_ID:
  1370. err = spi_nor_parse_sccr(nor, param_header);
  1371. break;
  1372. case SFDP_SCCR_MAP_MC_ID:
  1373. err = spi_nor_parse_sccr_mc(nor, param_header);
  1374. break;
  1375. default:
  1376. break;
  1377. }
  1378. if (err) {
  1379. dev_warn(dev, "Failed to parse optional parameter table: %04x\n",
  1380. SFDP_PARAM_HEADER_ID(param_header));
  1381. /*
  1382. * Let's not drop all information we extracted so far
  1383. * if optional table parsers fail. In case of failing,
  1384. * each optional parser is responsible to roll back to
  1385. * the previously known spi_nor data.
  1386. */
  1387. err = 0;
  1388. }
  1389. }
  1390. err = spi_nor_post_sfdp_fixups(nor);
  1391. exit:
  1392. kfree(param_headers);
  1393. return err;
  1394. }