sfdp.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584
  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. return spi_nor_post_bfpt_fixups(nor, bfpt_header, &bfpt);
  581. }
  582. /**
  583. * spi_nor_smpt_addr_nbytes() - return the number of address bytes used in the
  584. * configuration detection command.
  585. * @nor: pointer to a 'struct spi_nor'
  586. * @settings: configuration detection command descriptor, dword1
  587. */
  588. static u8 spi_nor_smpt_addr_nbytes(const struct spi_nor *nor, const u32 settings)
  589. {
  590. switch (settings & SMPT_CMD_ADDRESS_LEN_MASK) {
  591. case SMPT_CMD_ADDRESS_LEN_0:
  592. return 0;
  593. case SMPT_CMD_ADDRESS_LEN_3:
  594. return 3;
  595. case SMPT_CMD_ADDRESS_LEN_4:
  596. return 4;
  597. case SMPT_CMD_ADDRESS_LEN_USE_CURRENT:
  598. default:
  599. return nor->params->addr_mode_nbytes;
  600. }
  601. }
  602. /**
  603. * spi_nor_smpt_read_dummy() - return the configuration detection command read
  604. * latency, in clock cycles.
  605. * @nor: pointer to a 'struct spi_nor'
  606. * @settings: configuration detection command descriptor, dword1
  607. *
  608. * Return: the number of dummy cycles for an SMPT read
  609. */
  610. static u8 spi_nor_smpt_read_dummy(const struct spi_nor *nor, const u32 settings)
  611. {
  612. u8 read_dummy = SMPT_CMD_READ_DUMMY(settings);
  613. if (read_dummy == SMPT_CMD_READ_DUMMY_IS_VARIABLE)
  614. return nor->read_dummy;
  615. return read_dummy;
  616. }
  617. /**
  618. * spi_nor_get_map_in_use() - get the configuration map in use
  619. * @nor: pointer to a 'struct spi_nor'
  620. * @smpt: pointer to the sector map parameter table
  621. * @smpt_len: sector map parameter table length
  622. *
  623. * Return: pointer to the map in use, ERR_PTR(-errno) otherwise.
  624. */
  625. static const u32 *spi_nor_get_map_in_use(struct spi_nor *nor, const u32 *smpt,
  626. u8 smpt_len)
  627. {
  628. const u32 *ret;
  629. u8 *buf;
  630. u32 addr;
  631. int err;
  632. u8 i;
  633. u8 addr_nbytes, read_opcode, read_dummy;
  634. u8 read_data_mask, map_id;
  635. /* Use a kmalloc'ed bounce buffer to guarantee it is DMA-able. */
  636. buf = kmalloc(sizeof(*buf), GFP_KERNEL);
  637. if (!buf)
  638. return ERR_PTR(-ENOMEM);
  639. addr_nbytes = nor->addr_nbytes;
  640. read_dummy = nor->read_dummy;
  641. read_opcode = nor->read_opcode;
  642. map_id = 0;
  643. /* Determine if there are any optional Detection Command Descriptors */
  644. for (i = 0; i < smpt_len; i += 2) {
  645. if (smpt[i] & SMPT_DESC_TYPE_MAP)
  646. break;
  647. read_data_mask = SMPT_CMD_READ_DATA(smpt[i]);
  648. nor->addr_nbytes = spi_nor_smpt_addr_nbytes(nor, smpt[i]);
  649. nor->read_dummy = spi_nor_smpt_read_dummy(nor, smpt[i]);
  650. nor->read_opcode = SMPT_CMD_OPCODE(smpt[i]);
  651. addr = smpt[i + 1];
  652. err = spi_nor_read_raw(nor, addr, 1, buf);
  653. if (err) {
  654. ret = ERR_PTR(err);
  655. goto out;
  656. }
  657. /*
  658. * Build an index value that is used to select the Sector Map
  659. * Configuration that is currently in use.
  660. */
  661. map_id = map_id << 1 | !!(*buf & read_data_mask);
  662. }
  663. /*
  664. * If command descriptors are provided, they always precede map
  665. * descriptors in the table. There is no need to start the iteration
  666. * over smpt array all over again.
  667. *
  668. * Find the matching configuration map.
  669. */
  670. ret = ERR_PTR(-EINVAL);
  671. while (i < smpt_len) {
  672. if (SMPT_MAP_ID(smpt[i]) == map_id) {
  673. ret = smpt + i;
  674. break;
  675. }
  676. /*
  677. * If there are no more configuration map descriptors and no
  678. * configuration ID matched the configuration identifier, the
  679. * sector address map is unknown.
  680. */
  681. if (smpt[i] & SMPT_DESC_END)
  682. break;
  683. /* increment the table index to the next map */
  684. i += SMPT_MAP_REGION_COUNT(smpt[i]) + 1;
  685. }
  686. /* fall through */
  687. out:
  688. kfree(buf);
  689. nor->addr_nbytes = addr_nbytes;
  690. nor->read_dummy = read_dummy;
  691. nor->read_opcode = read_opcode;
  692. return ret;
  693. }
  694. /**
  695. * spi_nor_region_check_overlay() - set overlay bit when the region is overlaid
  696. * @region: pointer to a structure that describes a SPI NOR erase region
  697. * @erase: pointer to a structure that describes a SPI NOR erase type
  698. * @erase_type: erase type bitmask
  699. */
  700. static void
  701. spi_nor_region_check_overlay(struct spi_nor_erase_region *region,
  702. const struct spi_nor_erase_type *erase,
  703. const u8 erase_type)
  704. {
  705. int i;
  706. for (i = 0; i < SNOR_ERASE_TYPE_MAX; i++) {
  707. if (!(erase[i].size && erase_type & BIT(erase[i].idx)))
  708. continue;
  709. if (region->size & erase[i].size_mask) {
  710. region->overlaid = true;
  711. return;
  712. }
  713. }
  714. }
  715. /**
  716. * spi_nor_init_non_uniform_erase_map() - initialize the non-uniform erase map
  717. * @nor: pointer to a 'struct spi_nor'
  718. * @smpt: pointer to the sector map parameter table
  719. *
  720. * Return: 0 on success, -errno otherwise.
  721. */
  722. static int spi_nor_init_non_uniform_erase_map(struct spi_nor *nor,
  723. const u32 *smpt)
  724. {
  725. struct spi_nor_erase_map *map = &nor->params->erase_map;
  726. struct spi_nor_erase_type *erase = map->erase_type;
  727. struct spi_nor_erase_region *region;
  728. u64 offset;
  729. u32 region_count;
  730. int i, j;
  731. u8 uniform_erase_type, save_uniform_erase_type;
  732. u8 erase_type, regions_erase_type;
  733. region_count = SMPT_MAP_REGION_COUNT(*smpt);
  734. /*
  735. * The regions will be freed when the driver detaches from the
  736. * device.
  737. */
  738. region = devm_kcalloc(nor->dev, region_count, sizeof(*region),
  739. GFP_KERNEL);
  740. if (!region)
  741. return -ENOMEM;
  742. map->regions = region;
  743. map->n_regions = region_count;
  744. uniform_erase_type = 0xff;
  745. regions_erase_type = 0;
  746. offset = 0;
  747. /* Populate regions. */
  748. for (i = 0; i < region_count; i++) {
  749. j = i + 1; /* index for the region dword */
  750. region[i].offset = offset;
  751. region[i].size = SMPT_MAP_REGION_SIZE(smpt[j]);
  752. erase_type = SMPT_MAP_REGION_ERASE_TYPE(smpt[j]);
  753. region[i].erase_mask = erase_type;
  754. spi_nor_region_check_overlay(&region[i], erase, erase_type);
  755. /*
  756. * Save the erase types that are supported in all regions and
  757. * can erase the entire flash memory.
  758. */
  759. uniform_erase_type &= erase_type;
  760. /*
  761. * regions_erase_type mask will indicate all the erase types
  762. * supported in this configuration map.
  763. */
  764. regions_erase_type |= erase_type;
  765. offset = region[i].offset + region[i].size;
  766. }
  767. save_uniform_erase_type = map->uniform_region.erase_mask;
  768. map->uniform_region.erase_mask =
  769. spi_nor_sort_erase_mask(map,
  770. uniform_erase_type);
  771. if (!regions_erase_type) {
  772. /*
  773. * Roll back to the previous uniform_erase_type mask, SMPT is
  774. * broken.
  775. */
  776. map->uniform_region.erase_mask = save_uniform_erase_type;
  777. return -EINVAL;
  778. }
  779. /*
  780. * BFPT advertises all the erase types supported by all the possible
  781. * map configurations. Mask out the erase types that are not supported
  782. * by the current map configuration.
  783. */
  784. for (i = 0; i < SNOR_ERASE_TYPE_MAX; i++)
  785. if (!(regions_erase_type & BIT(erase[i].idx)))
  786. spi_nor_mask_erase_type(&erase[i]);
  787. return 0;
  788. }
  789. /**
  790. * spi_nor_parse_smpt() - parse Sector Map Parameter Table
  791. * @nor: pointer to a 'struct spi_nor'
  792. * @smpt_header: sector map parameter table header
  793. *
  794. * This table is optional, but when available, we parse it to identify the
  795. * location and size of sectors within the main data array of the flash memory
  796. * device and to identify which Erase Types are supported by each sector.
  797. *
  798. * Return: 0 on success, -errno otherwise.
  799. */
  800. static int spi_nor_parse_smpt(struct spi_nor *nor,
  801. const struct sfdp_parameter_header *smpt_header)
  802. {
  803. const u32 *sector_map;
  804. u32 *smpt;
  805. size_t len;
  806. u32 addr;
  807. int ret;
  808. /* Read the Sector Map Parameter Table. */
  809. len = smpt_header->length * sizeof(*smpt);
  810. smpt = kmalloc(len, GFP_KERNEL);
  811. if (!smpt)
  812. return -ENOMEM;
  813. addr = SFDP_PARAM_HEADER_PTP(smpt_header);
  814. ret = spi_nor_read_sfdp(nor, addr, len, smpt);
  815. if (ret)
  816. goto out;
  817. /* Fix endianness of the SMPT DWORDs. */
  818. le32_to_cpu_array(smpt, smpt_header->length);
  819. sector_map = spi_nor_get_map_in_use(nor, smpt, smpt_header->length);
  820. if (IS_ERR(sector_map)) {
  821. ret = PTR_ERR(sector_map);
  822. goto out;
  823. }
  824. ret = spi_nor_init_non_uniform_erase_map(nor, sector_map);
  825. if (ret)
  826. goto out;
  827. spi_nor_regions_sort_erase_types(&nor->params->erase_map);
  828. /* fall through */
  829. out:
  830. kfree(smpt);
  831. return ret;
  832. }
  833. /**
  834. * spi_nor_parse_4bait() - parse the 4-Byte Address Instruction Table
  835. * @nor: pointer to a 'struct spi_nor'.
  836. * @param_header: pointer to the 'struct sfdp_parameter_header' describing
  837. * the 4-Byte Address Instruction Table length and version.
  838. *
  839. * Return: 0 on success, -errno otherwise.
  840. */
  841. static int spi_nor_parse_4bait(struct spi_nor *nor,
  842. const struct sfdp_parameter_header *param_header)
  843. {
  844. static const struct sfdp_4bait reads[] = {
  845. { SNOR_HWCAPS_READ, BIT(0) },
  846. { SNOR_HWCAPS_READ_FAST, BIT(1) },
  847. { SNOR_HWCAPS_READ_1_1_2, BIT(2) },
  848. { SNOR_HWCAPS_READ_1_2_2, BIT(3) },
  849. { SNOR_HWCAPS_READ_1_1_4, BIT(4) },
  850. { SNOR_HWCAPS_READ_1_4_4, BIT(5) },
  851. { SNOR_HWCAPS_READ_1_1_1_DTR, BIT(13) },
  852. { SNOR_HWCAPS_READ_1_2_2_DTR, BIT(14) },
  853. { SNOR_HWCAPS_READ_1_4_4_DTR, BIT(15) },
  854. { SNOR_HWCAPS_READ_1_1_8, BIT(20) },
  855. { SNOR_HWCAPS_READ_1_8_8, BIT(21) },
  856. };
  857. static const struct sfdp_4bait programs[] = {
  858. { SNOR_HWCAPS_PP, BIT(6) },
  859. { SNOR_HWCAPS_PP_1_1_4, BIT(7) },
  860. { SNOR_HWCAPS_PP_1_4_4, BIT(8) },
  861. };
  862. static const struct sfdp_4bait erases[SNOR_ERASE_TYPE_MAX] = {
  863. { 0u /* not used */, BIT(9) },
  864. { 0u /* not used */, BIT(10) },
  865. { 0u /* not used */, BIT(11) },
  866. { 0u /* not used */, BIT(12) },
  867. };
  868. struct spi_nor_flash_parameter *params = nor->params;
  869. struct spi_nor_pp_command *params_pp = params->page_programs;
  870. struct spi_nor_erase_map *map = &params->erase_map;
  871. struct spi_nor_erase_type *erase_type = map->erase_type;
  872. u32 *dwords;
  873. size_t len;
  874. u32 addr, discard_hwcaps, read_hwcaps, pp_hwcaps, erase_mask;
  875. int i, ret;
  876. if (param_header->major != SFDP_JESD216_MAJOR ||
  877. param_header->length < SFDP_4BAIT_DWORD_MAX)
  878. return -EINVAL;
  879. /* Read the 4-byte Address Instruction Table. */
  880. len = sizeof(*dwords) * SFDP_4BAIT_DWORD_MAX;
  881. /* Use a kmalloc'ed bounce buffer to guarantee it is DMA-able. */
  882. dwords = kmalloc(len, GFP_KERNEL);
  883. if (!dwords)
  884. return -ENOMEM;
  885. addr = SFDP_PARAM_HEADER_PTP(param_header);
  886. ret = spi_nor_read_sfdp(nor, addr, len, dwords);
  887. if (ret)
  888. goto out;
  889. /* Fix endianness of the 4BAIT DWORDs. */
  890. le32_to_cpu_array(dwords, SFDP_4BAIT_DWORD_MAX);
  891. /*
  892. * Compute the subset of (Fast) Read commands for which the 4-byte
  893. * version is supported.
  894. */
  895. discard_hwcaps = 0;
  896. read_hwcaps = 0;
  897. for (i = 0; i < ARRAY_SIZE(reads); i++) {
  898. const struct sfdp_4bait *read = &reads[i];
  899. discard_hwcaps |= read->hwcaps;
  900. if ((params->hwcaps.mask & read->hwcaps) &&
  901. (dwords[SFDP_DWORD(1)] & read->supported_bit))
  902. read_hwcaps |= read->hwcaps;
  903. }
  904. /*
  905. * Compute the subset of Page Program commands for which the 4-byte
  906. * version is supported.
  907. */
  908. pp_hwcaps = 0;
  909. for (i = 0; i < ARRAY_SIZE(programs); i++) {
  910. const struct sfdp_4bait *program = &programs[i];
  911. /*
  912. * The 4 Byte Address Instruction (Optional) Table is the only
  913. * SFDP table that indicates support for Page Program Commands.
  914. * Bypass the params->hwcaps.mask and consider 4BAIT the biggest
  915. * authority for specifying Page Program support.
  916. */
  917. discard_hwcaps |= program->hwcaps;
  918. if (dwords[SFDP_DWORD(1)] & program->supported_bit)
  919. pp_hwcaps |= program->hwcaps;
  920. }
  921. /*
  922. * Compute the subset of Sector Erase commands for which the 4-byte
  923. * version is supported.
  924. */
  925. erase_mask = 0;
  926. for (i = 0; i < SNOR_ERASE_TYPE_MAX; i++) {
  927. const struct sfdp_4bait *erase = &erases[i];
  928. if (dwords[SFDP_DWORD(1)] & erase->supported_bit)
  929. erase_mask |= BIT(i);
  930. }
  931. /* Replicate the sort done for the map's erase types in BFPT. */
  932. erase_mask = spi_nor_sort_erase_mask(map, erase_mask);
  933. /*
  934. * We need at least one 4-byte op code per read, program and erase
  935. * operation; the .read(), .write() and .erase() hooks share the
  936. * nor->addr_nbytes value.
  937. */
  938. if (!read_hwcaps || !pp_hwcaps || !erase_mask)
  939. goto out;
  940. /*
  941. * Discard all operations from the 4-byte instruction set which are
  942. * not supported by this memory.
  943. */
  944. params->hwcaps.mask &= ~discard_hwcaps;
  945. params->hwcaps.mask |= (read_hwcaps | pp_hwcaps);
  946. /* Use the 4-byte address instruction set. */
  947. for (i = 0; i < SNOR_CMD_READ_MAX; i++) {
  948. struct spi_nor_read_command *read_cmd = &params->reads[i];
  949. read_cmd->opcode = spi_nor_convert_3to4_read(read_cmd->opcode);
  950. }
  951. /* 4BAIT is the only SFDP table that indicates page program support. */
  952. if (pp_hwcaps & SNOR_HWCAPS_PP) {
  953. spi_nor_set_pp_settings(&params_pp[SNOR_CMD_PP],
  954. SPINOR_OP_PP_4B, SNOR_PROTO_1_1_1);
  955. /*
  956. * Since xSPI Page Program opcode is backward compatible with
  957. * Legacy SPI, use Legacy SPI opcode there as well.
  958. */
  959. spi_nor_set_pp_settings(&params_pp[SNOR_CMD_PP_8_8_8_DTR],
  960. SPINOR_OP_PP_4B, SNOR_PROTO_8_8_8_DTR);
  961. }
  962. if (pp_hwcaps & SNOR_HWCAPS_PP_1_1_4)
  963. spi_nor_set_pp_settings(&params_pp[SNOR_CMD_PP_1_1_4],
  964. SPINOR_OP_PP_1_1_4_4B,
  965. SNOR_PROTO_1_1_4);
  966. if (pp_hwcaps & SNOR_HWCAPS_PP_1_4_4)
  967. spi_nor_set_pp_settings(&params_pp[SNOR_CMD_PP_1_4_4],
  968. SPINOR_OP_PP_1_4_4_4B,
  969. SNOR_PROTO_1_4_4);
  970. for (i = 0; i < SNOR_ERASE_TYPE_MAX; i++) {
  971. if (erase_mask & BIT(i))
  972. erase_type[i].opcode = (dwords[SFDP_DWORD(2)] >>
  973. erase_type[i].idx * 8) & 0xFF;
  974. else
  975. spi_nor_mask_erase_type(&erase_type[i]);
  976. }
  977. /*
  978. * We set SNOR_F_HAS_4BAIT in order to skip spi_nor_set_4byte_opcodes()
  979. * later because we already did the conversion to 4byte opcodes. Also,
  980. * this latest function implements a legacy quirk for the erase size of
  981. * Spansion memory. However this quirk is no longer needed with new
  982. * SFDP compliant memories.
  983. */
  984. params->addr_nbytes = 4;
  985. nor->flags |= SNOR_F_4B_OPCODES | SNOR_F_HAS_4BAIT;
  986. /* fall through */
  987. out:
  988. kfree(dwords);
  989. return ret;
  990. }
  991. #define PROFILE1_DWORD1_RDSR_ADDR_BYTES BIT(29)
  992. #define PROFILE1_DWORD1_RDSR_DUMMY BIT(28)
  993. #define PROFILE1_DWORD1_RD_FAST_CMD GENMASK(15, 8)
  994. #define PROFILE1_DWORD4_DUMMY_200MHZ GENMASK(11, 7)
  995. #define PROFILE1_DWORD5_DUMMY_166MHZ GENMASK(31, 27)
  996. #define PROFILE1_DWORD5_DUMMY_133MHZ GENMASK(21, 17)
  997. #define PROFILE1_DWORD5_DUMMY_100MHZ GENMASK(11, 7)
  998. /**
  999. * spi_nor_parse_profile1() - parse the xSPI Profile 1.0 table
  1000. * @nor: pointer to a 'struct spi_nor'
  1001. * @profile1_header: pointer to the 'struct sfdp_parameter_header' describing
  1002. * the Profile 1.0 Table length and version.
  1003. *
  1004. * Return: 0 on success, -errno otherwise.
  1005. */
  1006. static int spi_nor_parse_profile1(struct spi_nor *nor,
  1007. const struct sfdp_parameter_header *profile1_header)
  1008. {
  1009. u32 *dwords, addr;
  1010. size_t len;
  1011. int ret;
  1012. u8 dummy, opcode;
  1013. len = profile1_header->length * sizeof(*dwords);
  1014. dwords = kmalloc(len, GFP_KERNEL);
  1015. if (!dwords)
  1016. return -ENOMEM;
  1017. addr = SFDP_PARAM_HEADER_PTP(profile1_header);
  1018. ret = spi_nor_read_sfdp(nor, addr, len, dwords);
  1019. if (ret)
  1020. goto out;
  1021. le32_to_cpu_array(dwords, profile1_header->length);
  1022. /* Get 8D-8D-8D fast read opcode and dummy cycles. */
  1023. opcode = FIELD_GET(PROFILE1_DWORD1_RD_FAST_CMD, dwords[SFDP_DWORD(1)]);
  1024. /* Set the Read Status Register dummy cycles and dummy address bytes. */
  1025. if (dwords[SFDP_DWORD(1)] & PROFILE1_DWORD1_RDSR_DUMMY)
  1026. nor->params->rdsr_dummy = 8;
  1027. else
  1028. nor->params->rdsr_dummy = 4;
  1029. if (dwords[SFDP_DWORD(1)] & PROFILE1_DWORD1_RDSR_ADDR_BYTES)
  1030. nor->params->rdsr_addr_nbytes = 4;
  1031. else
  1032. nor->params->rdsr_addr_nbytes = 0;
  1033. /*
  1034. * We don't know what speed the controller is running at. Find the
  1035. * dummy cycles for the fastest frequency the flash can run at to be
  1036. * sure we are never short of dummy cycles. A value of 0 means the
  1037. * frequency is not supported.
  1038. *
  1039. * Default to PROFILE1_DUMMY_DEFAULT if we don't find anything, and let
  1040. * flashes set the correct value if needed in their fixup hooks.
  1041. */
  1042. dummy = FIELD_GET(PROFILE1_DWORD4_DUMMY_200MHZ, dwords[SFDP_DWORD(4)]);
  1043. if (!dummy)
  1044. dummy = FIELD_GET(PROFILE1_DWORD5_DUMMY_166MHZ,
  1045. dwords[SFDP_DWORD(5)]);
  1046. if (!dummy)
  1047. dummy = FIELD_GET(PROFILE1_DWORD5_DUMMY_133MHZ,
  1048. dwords[SFDP_DWORD(5)]);
  1049. if (!dummy)
  1050. dummy = FIELD_GET(PROFILE1_DWORD5_DUMMY_100MHZ,
  1051. dwords[SFDP_DWORD(5)]);
  1052. if (!dummy)
  1053. dev_dbg(nor->dev,
  1054. "Can't find dummy cycles from Profile 1.0 table\n");
  1055. /* Round up to an even value to avoid tripping controllers up. */
  1056. dummy = round_up(dummy, 2);
  1057. /* Update the fast read settings. */
  1058. nor->params->hwcaps.mask |= SNOR_HWCAPS_READ_8_8_8_DTR;
  1059. spi_nor_set_read_settings(&nor->params->reads[SNOR_CMD_READ_8_8_8_DTR],
  1060. 0, dummy, opcode,
  1061. SNOR_PROTO_8_8_8_DTR);
  1062. /*
  1063. * Page Program is "Required Command" in the xSPI Profile 1.0. Update
  1064. * the params->hwcaps.mask here.
  1065. */
  1066. nor->params->hwcaps.mask |= SNOR_HWCAPS_PP_8_8_8_DTR;
  1067. out:
  1068. kfree(dwords);
  1069. return ret;
  1070. }
  1071. #define SCCR_DWORD22_OCTAL_DTR_EN_VOLATILE BIT(31)
  1072. /**
  1073. * spi_nor_parse_sccr() - Parse the Status, Control and Configuration Register
  1074. * Map.
  1075. * @nor: pointer to a 'struct spi_nor'
  1076. * @sccr_header: pointer to the 'struct sfdp_parameter_header' describing
  1077. * the SCCR Map table length and version.
  1078. *
  1079. * Return: 0 on success, -errno otherwise.
  1080. */
  1081. static int spi_nor_parse_sccr(struct spi_nor *nor,
  1082. const struct sfdp_parameter_header *sccr_header)
  1083. {
  1084. struct spi_nor_flash_parameter *params = nor->params;
  1085. u32 *dwords, addr;
  1086. size_t len;
  1087. int ret;
  1088. len = sccr_header->length * sizeof(*dwords);
  1089. dwords = kmalloc(len, GFP_KERNEL);
  1090. if (!dwords)
  1091. return -ENOMEM;
  1092. addr = SFDP_PARAM_HEADER_PTP(sccr_header);
  1093. ret = spi_nor_read_sfdp(nor, addr, len, dwords);
  1094. if (ret)
  1095. goto out;
  1096. le32_to_cpu_array(dwords, sccr_header->length);
  1097. /* Address offset for volatile registers (die 0) */
  1098. if (!params->vreg_offset) {
  1099. params->vreg_offset = devm_kmalloc(nor->dev, sizeof(*dwords),
  1100. GFP_KERNEL);
  1101. if (!params->vreg_offset) {
  1102. ret = -ENOMEM;
  1103. goto out;
  1104. }
  1105. }
  1106. params->vreg_offset[0] = dwords[SFDP_DWORD(1)];
  1107. params->n_dice = 1;
  1108. if (FIELD_GET(SCCR_DWORD22_OCTAL_DTR_EN_VOLATILE,
  1109. dwords[SFDP_DWORD(22)]))
  1110. nor->flags |= SNOR_F_IO_MODE_EN_VOLATILE;
  1111. out:
  1112. kfree(dwords);
  1113. return ret;
  1114. }
  1115. /**
  1116. * spi_nor_parse_sccr_mc() - Parse the Status, Control and Configuration
  1117. * Register Map Offsets for Multi-Chip SPI Memory
  1118. * Devices.
  1119. * @nor: pointer to a 'struct spi_nor'
  1120. * @sccr_mc_header: pointer to the 'struct sfdp_parameter_header' describing
  1121. * the SCCR Map offsets table length and version.
  1122. *
  1123. * Return: 0 on success, -errno otherwise.
  1124. */
  1125. static int spi_nor_parse_sccr_mc(struct spi_nor *nor,
  1126. const struct sfdp_parameter_header *sccr_mc_header)
  1127. {
  1128. struct spi_nor_flash_parameter *params = nor->params;
  1129. u32 *dwords, addr;
  1130. u8 i, n_dice;
  1131. size_t len;
  1132. int ret;
  1133. len = sccr_mc_header->length * sizeof(*dwords);
  1134. dwords = kmalloc(len, GFP_KERNEL);
  1135. if (!dwords)
  1136. return -ENOMEM;
  1137. addr = SFDP_PARAM_HEADER_PTP(sccr_mc_header);
  1138. ret = spi_nor_read_sfdp(nor, addr, len, dwords);
  1139. if (ret)
  1140. goto out;
  1141. le32_to_cpu_array(dwords, sccr_mc_header->length);
  1142. /*
  1143. * Pair of DOWRDs (volatile and non-volatile register offsets) per
  1144. * additional die. Hence, length = 2 * (number of additional dice).
  1145. */
  1146. n_dice = 1 + sccr_mc_header->length / 2;
  1147. /* Address offset for volatile registers of additional dice */
  1148. params->vreg_offset =
  1149. devm_krealloc(nor->dev, params->vreg_offset,
  1150. n_dice * sizeof(*dwords),
  1151. GFP_KERNEL);
  1152. if (!params->vreg_offset) {
  1153. ret = -ENOMEM;
  1154. goto out;
  1155. }
  1156. for (i = 1; i < n_dice; i++)
  1157. params->vreg_offset[i] = dwords[SFDP_DWORD(i) * 2];
  1158. params->n_dice = n_dice;
  1159. out:
  1160. kfree(dwords);
  1161. return ret;
  1162. }
  1163. /**
  1164. * spi_nor_post_sfdp_fixups() - Updates the flash's parameters and settings
  1165. * after SFDP has been parsed. Called only for flashes that define JESD216 SFDP
  1166. * tables.
  1167. * @nor: pointer to a 'struct spi_nor'
  1168. *
  1169. * Used to tweak various flash parameters when information provided by the SFDP
  1170. * tables are wrong.
  1171. */
  1172. static int spi_nor_post_sfdp_fixups(struct spi_nor *nor)
  1173. {
  1174. int ret;
  1175. if (nor->manufacturer && nor->manufacturer->fixups &&
  1176. nor->manufacturer->fixups->post_sfdp) {
  1177. ret = nor->manufacturer->fixups->post_sfdp(nor);
  1178. if (ret)
  1179. return ret;
  1180. }
  1181. if (nor->info->fixups && nor->info->fixups->post_sfdp)
  1182. return nor->info->fixups->post_sfdp(nor);
  1183. return 0;
  1184. }
  1185. /**
  1186. * spi_nor_check_sfdp_signature() - check for a valid SFDP signature
  1187. * @nor: pointer to a 'struct spi_nor'
  1188. *
  1189. * Used to detect if the flash supports the RDSFDP command as well as the
  1190. * presence of a valid SFDP table.
  1191. *
  1192. * Return: 0 on success, -errno otherwise.
  1193. */
  1194. int spi_nor_check_sfdp_signature(struct spi_nor *nor)
  1195. {
  1196. u32 signature;
  1197. int err;
  1198. /* Get the SFDP header. */
  1199. err = spi_nor_read_sfdp_dma_unsafe(nor, 0, sizeof(signature),
  1200. &signature);
  1201. if (err < 0)
  1202. return err;
  1203. /* Check the SFDP signature. */
  1204. if (le32_to_cpu(signature) != SFDP_SIGNATURE)
  1205. return -EINVAL;
  1206. return 0;
  1207. }
  1208. /**
  1209. * spi_nor_parse_sfdp() - parse the Serial Flash Discoverable Parameters.
  1210. * @nor: pointer to a 'struct spi_nor'
  1211. *
  1212. * The Serial Flash Discoverable Parameters are described by the JEDEC JESD216
  1213. * specification. This is a standard which tends to supported by almost all
  1214. * (Q)SPI memory manufacturers. Those hard-coded tables allow us to learn at
  1215. * runtime the main parameters needed to perform basic SPI flash operations such
  1216. * as Fast Read, Page Program or Sector Erase commands.
  1217. *
  1218. * Return: 0 on success, -errno otherwise.
  1219. */
  1220. int spi_nor_parse_sfdp(struct spi_nor *nor)
  1221. {
  1222. const struct sfdp_parameter_header *param_header, *bfpt_header;
  1223. struct sfdp_parameter_header *param_headers = NULL;
  1224. struct sfdp_header header;
  1225. struct device *dev = nor->dev;
  1226. struct sfdp *sfdp;
  1227. size_t sfdp_size;
  1228. size_t psize;
  1229. int i, err;
  1230. /* Get the SFDP header. */
  1231. err = spi_nor_read_sfdp_dma_unsafe(nor, 0, sizeof(header), &header);
  1232. if (err < 0)
  1233. return err;
  1234. /* Check the SFDP header version. */
  1235. if (le32_to_cpu(header.signature) != SFDP_SIGNATURE ||
  1236. header.major != SFDP_JESD216_MAJOR)
  1237. return -EINVAL;
  1238. /*
  1239. * Verify that the first and only mandatory parameter header is a
  1240. * Basic Flash Parameter Table header as specified in JESD216.
  1241. */
  1242. bfpt_header = &header.bfpt_header;
  1243. if (SFDP_PARAM_HEADER_ID(bfpt_header) != SFDP_BFPT_ID ||
  1244. bfpt_header->major != SFDP_JESD216_MAJOR)
  1245. return -EINVAL;
  1246. sfdp_size = SFDP_PARAM_HEADER_PTP(bfpt_header) +
  1247. SFDP_PARAM_HEADER_PARAM_LEN(bfpt_header);
  1248. /*
  1249. * Allocate memory then read all parameter headers with a single
  1250. * Read SFDP command. These parameter headers will actually be parsed
  1251. * twice: a first time to get the latest revision of the basic flash
  1252. * parameter table, then a second time to handle the supported optional
  1253. * tables.
  1254. * Hence we read the parameter headers once for all to reduce the
  1255. * processing time. Also we use kmalloc() instead of devm_kmalloc()
  1256. * because we don't need to keep these parameter headers: the allocated
  1257. * memory is always released with kfree() before exiting this function.
  1258. */
  1259. if (header.nph) {
  1260. psize = header.nph * sizeof(*param_headers);
  1261. param_headers = kmalloc(psize, GFP_KERNEL);
  1262. if (!param_headers)
  1263. return -ENOMEM;
  1264. err = spi_nor_read_sfdp(nor, sizeof(header),
  1265. psize, param_headers);
  1266. if (err < 0) {
  1267. dev_dbg(dev, "failed to read SFDP parameter headers\n");
  1268. goto exit;
  1269. }
  1270. }
  1271. /*
  1272. * Cache the complete SFDP data. It is not (easily) possible to fetch
  1273. * SFDP after probe time and we need it for the sysfs access.
  1274. */
  1275. for (i = 0; i < header.nph; i++) {
  1276. param_header = &param_headers[i];
  1277. sfdp_size = max_t(size_t, sfdp_size,
  1278. SFDP_PARAM_HEADER_PTP(param_header) +
  1279. SFDP_PARAM_HEADER_PARAM_LEN(param_header));
  1280. }
  1281. /*
  1282. * Limit the total size to a reasonable value to avoid allocating too
  1283. * much memory just of because the flash returned some insane values.
  1284. */
  1285. if (sfdp_size > PAGE_SIZE) {
  1286. dev_dbg(dev, "SFDP data (%zu) too big, truncating\n",
  1287. sfdp_size);
  1288. sfdp_size = PAGE_SIZE;
  1289. }
  1290. sfdp = devm_kzalloc(dev, sizeof(*sfdp), GFP_KERNEL);
  1291. if (!sfdp) {
  1292. err = -ENOMEM;
  1293. goto exit;
  1294. }
  1295. /*
  1296. * The SFDP is organized in chunks of DWORDs. Thus, in theory, the
  1297. * sfdp_size should be a multiple of DWORDs. But in case a flash
  1298. * is not spec compliant, make sure that we have enough space to store
  1299. * the complete SFDP data.
  1300. */
  1301. sfdp->num_dwords = DIV_ROUND_UP(sfdp_size, sizeof(*sfdp->dwords));
  1302. sfdp->dwords = devm_kcalloc(dev, sfdp->num_dwords,
  1303. sizeof(*sfdp->dwords), GFP_KERNEL);
  1304. if (!sfdp->dwords) {
  1305. err = -ENOMEM;
  1306. devm_kfree(dev, sfdp);
  1307. goto exit;
  1308. }
  1309. err = spi_nor_read_sfdp(nor, 0, sfdp_size, sfdp->dwords);
  1310. if (err < 0) {
  1311. dev_dbg(dev, "failed to read SFDP data\n");
  1312. devm_kfree(dev, sfdp->dwords);
  1313. devm_kfree(dev, sfdp);
  1314. goto exit;
  1315. }
  1316. nor->sfdp = sfdp;
  1317. /*
  1318. * Check other parameter headers to get the latest revision of
  1319. * the basic flash parameter table.
  1320. */
  1321. for (i = 0; i < header.nph; i++) {
  1322. param_header = &param_headers[i];
  1323. if (SFDP_PARAM_HEADER_ID(param_header) == SFDP_BFPT_ID &&
  1324. param_header->major == SFDP_JESD216_MAJOR &&
  1325. (param_header->minor > bfpt_header->minor ||
  1326. (param_header->minor == bfpt_header->minor &&
  1327. param_header->length > bfpt_header->length)))
  1328. bfpt_header = param_header;
  1329. }
  1330. err = spi_nor_parse_bfpt(nor, bfpt_header);
  1331. if (err)
  1332. goto exit;
  1333. /* Parse optional parameter tables. */
  1334. for (i = 0; i < header.nph; i++) {
  1335. param_header = &param_headers[i];
  1336. switch (SFDP_PARAM_HEADER_ID(param_header)) {
  1337. case SFDP_SECTOR_MAP_ID:
  1338. err = spi_nor_parse_smpt(nor, param_header);
  1339. break;
  1340. case SFDP_4BAIT_ID:
  1341. err = spi_nor_parse_4bait(nor, param_header);
  1342. break;
  1343. case SFDP_PROFILE1_ID:
  1344. err = spi_nor_parse_profile1(nor, param_header);
  1345. break;
  1346. case SFDP_SCCR_MAP_ID:
  1347. err = spi_nor_parse_sccr(nor, param_header);
  1348. break;
  1349. case SFDP_SCCR_MAP_MC_ID:
  1350. err = spi_nor_parse_sccr_mc(nor, param_header);
  1351. break;
  1352. default:
  1353. break;
  1354. }
  1355. if (err) {
  1356. dev_warn(dev, "Failed to parse optional parameter table: %04x\n",
  1357. SFDP_PARAM_HEADER_ID(param_header));
  1358. /*
  1359. * Let's not drop all information we extracted so far
  1360. * if optional table parsers fail. In case of failing,
  1361. * each optional parser is responsible to roll back to
  1362. * the previously known spi_nor data.
  1363. */
  1364. err = 0;
  1365. }
  1366. }
  1367. err = spi_nor_post_sfdp_fixups(nor);
  1368. exit:
  1369. kfree(param_headers);
  1370. return err;
  1371. }