sfdp.c 41 KB

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