core.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2005, Intec Automation Inc.
  4. * Copyright (C) 2014, Freescale Semiconductor, Inc.
  5. */
  6. #ifndef __LINUX_MTD_SPI_NOR_INTERNAL_H
  7. #define __LINUX_MTD_SPI_NOR_INTERNAL_H
  8. #include "sfdp.h"
  9. #define SPI_NOR_MAX_ID_LEN 6
  10. /*
  11. * 256 bytes is a sane default for most older flashes. Newer flashes will
  12. * have the page size defined within their SFDP tables.
  13. */
  14. #define SPI_NOR_DEFAULT_PAGE_SIZE 256
  15. #define SPI_NOR_DEFAULT_N_BANKS 1
  16. #define SPI_NOR_DEFAULT_SECTOR_SIZE SZ_64K
  17. /* Standard SPI NOR flash operations. */
  18. #define SPI_NOR_READID_OP(naddr, ndummy, buf, len) \
  19. SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDID, 0), \
  20. SPI_MEM_OP_ADDR(naddr, 0, 0), \
  21. SPI_MEM_OP_DUMMY(ndummy, 0), \
  22. SPI_MEM_OP_DATA_IN(len, buf, 0))
  23. #define SPI_NOR_WREN_OP \
  24. SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WREN, 0), \
  25. SPI_MEM_OP_NO_ADDR, \
  26. SPI_MEM_OP_NO_DUMMY, \
  27. SPI_MEM_OP_NO_DATA)
  28. #define SPI_NOR_WRDI_OP \
  29. SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WRDI, 0), \
  30. SPI_MEM_OP_NO_ADDR, \
  31. SPI_MEM_OP_NO_DUMMY, \
  32. SPI_MEM_OP_NO_DATA)
  33. #define SPI_NOR_RDSR_OP(buf) \
  34. SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDSR, 0), \
  35. SPI_MEM_OP_NO_ADDR, \
  36. SPI_MEM_OP_NO_DUMMY, \
  37. SPI_MEM_OP_DATA_IN(1, buf, 0))
  38. #define SPI_NOR_WRSR_OP(buf, len) \
  39. SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WRSR, 0), \
  40. SPI_MEM_OP_NO_ADDR, \
  41. SPI_MEM_OP_NO_DUMMY, \
  42. SPI_MEM_OP_DATA_OUT(len, buf, 0))
  43. #define SPI_NOR_RDSR2_OP(buf) \
  44. SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDSR2, 0), \
  45. SPI_MEM_OP_NO_ADDR, \
  46. SPI_MEM_OP_NO_DUMMY, \
  47. SPI_MEM_OP_DATA_OUT(1, buf, 0))
  48. #define SPI_NOR_WRSR2_OP(buf) \
  49. SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_WRSR2, 0), \
  50. SPI_MEM_OP_NO_ADDR, \
  51. SPI_MEM_OP_NO_DUMMY, \
  52. SPI_MEM_OP_DATA_OUT(1, buf, 0))
  53. #define SPI_NOR_RDCR_OP(buf) \
  54. SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDCR, 0), \
  55. SPI_MEM_OP_NO_ADDR, \
  56. SPI_MEM_OP_NO_DUMMY, \
  57. SPI_MEM_OP_DATA_IN(1, buf, 0))
  58. #define SPI_NOR_EN4B_EX4B_OP(enable) \
  59. SPI_MEM_OP(SPI_MEM_OP_CMD(enable ? SPINOR_OP_EN4B : SPINOR_OP_EX4B, 0), \
  60. SPI_MEM_OP_NO_ADDR, \
  61. SPI_MEM_OP_NO_DUMMY, \
  62. SPI_MEM_OP_NO_DATA)
  63. #define SPI_NOR_BRWR_OP(buf) \
  64. SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_BRWR, 0), \
  65. SPI_MEM_OP_NO_ADDR, \
  66. SPI_MEM_OP_NO_DUMMY, \
  67. SPI_MEM_OP_DATA_OUT(1, buf, 0))
  68. #define SPI_NOR_GBULK_OP \
  69. SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_GBULK, 0), \
  70. SPI_MEM_OP_NO_ADDR, \
  71. SPI_MEM_OP_NO_DUMMY, \
  72. SPI_MEM_OP_NO_DATA)
  73. #define SPI_NOR_DIE_ERASE_OP(opcode, addr_nbytes, addr, dice) \
  74. SPI_MEM_OP(SPI_MEM_OP_CMD(opcode, 0), \
  75. SPI_MEM_OP_ADDR(dice ? addr_nbytes : 0, addr, 0), \
  76. SPI_MEM_OP_NO_DUMMY, \
  77. SPI_MEM_OP_NO_DATA)
  78. #define SPI_NOR_SECTOR_ERASE_OP(opcode, addr_nbytes, addr) \
  79. SPI_MEM_OP(SPI_MEM_OP_CMD(opcode, 0), \
  80. SPI_MEM_OP_ADDR(addr_nbytes, addr, 0), \
  81. SPI_MEM_OP_NO_DUMMY, \
  82. SPI_MEM_OP_NO_DATA)
  83. #define SPI_NOR_READ_OP(opcode) \
  84. SPI_MEM_OP(SPI_MEM_OP_CMD(opcode, 0), \
  85. SPI_MEM_OP_ADDR(3, 0, 0), \
  86. SPI_MEM_OP_DUMMY(1, 0), \
  87. SPI_MEM_OP_DATA_IN(2, NULL, 0))
  88. #define SPI_NOR_PP_OP(opcode) \
  89. SPI_MEM_OP(SPI_MEM_OP_CMD(opcode, 0), \
  90. SPI_MEM_OP_ADDR(3, 0, 0), \
  91. SPI_MEM_OP_NO_DUMMY, \
  92. SPI_MEM_OP_DATA_OUT(2, NULL, 0))
  93. #define SPINOR_SRSTEN_OP \
  94. SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_SRSTEN, 0), \
  95. SPI_MEM_OP_NO_DUMMY, \
  96. SPI_MEM_OP_NO_ADDR, \
  97. SPI_MEM_OP_NO_DATA)
  98. #define SPINOR_SRST_OP \
  99. SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_SRST, 0), \
  100. SPI_MEM_OP_NO_DUMMY, \
  101. SPI_MEM_OP_NO_ADDR, \
  102. SPI_MEM_OP_NO_DATA)
  103. /* Keep these in sync with the list in debugfs.c */
  104. enum spi_nor_option_flags {
  105. SNOR_F_HAS_SR_TB = BIT(0),
  106. SNOR_F_NO_OP_CHIP_ERASE = BIT(1),
  107. SNOR_F_BROKEN_RESET = BIT(2),
  108. SNOR_F_4B_OPCODES = BIT(3),
  109. SNOR_F_HAS_4BAIT = BIT(4),
  110. SNOR_F_HAS_LOCK = BIT(5),
  111. SNOR_F_HAS_16BIT_SR = BIT(6),
  112. SNOR_F_NO_READ_CR = BIT(7),
  113. SNOR_F_HAS_SR_TB_BIT6 = BIT(8),
  114. SNOR_F_HAS_4BIT_BP = BIT(9),
  115. SNOR_F_HAS_SR_BP3_BIT6 = BIT(10),
  116. SNOR_F_IO_MODE_EN_VOLATILE = BIT(11),
  117. SNOR_F_SOFT_RESET = BIT(12),
  118. SNOR_F_SWP_IS_VOLATILE = BIT(13),
  119. SNOR_F_RWW = BIT(14),
  120. SNOR_F_ECC = BIT(15),
  121. SNOR_F_NO_WP = BIT(16),
  122. SNOR_F_SWAP16 = BIT(17),
  123. };
  124. struct spi_nor_read_command {
  125. u8 num_mode_clocks;
  126. u8 num_wait_states;
  127. u8 opcode;
  128. enum spi_nor_protocol proto;
  129. };
  130. struct spi_nor_pp_command {
  131. u8 opcode;
  132. enum spi_nor_protocol proto;
  133. };
  134. enum spi_nor_read_command_index {
  135. SNOR_CMD_READ,
  136. SNOR_CMD_READ_FAST,
  137. SNOR_CMD_READ_1_1_1_DTR,
  138. /* Dual SPI */
  139. SNOR_CMD_READ_1_1_2,
  140. SNOR_CMD_READ_1_2_2,
  141. SNOR_CMD_READ_2_2_2,
  142. SNOR_CMD_READ_1_2_2_DTR,
  143. /* Quad SPI */
  144. SNOR_CMD_READ_1_1_4,
  145. SNOR_CMD_READ_1_4_4,
  146. SNOR_CMD_READ_4_4_4,
  147. SNOR_CMD_READ_1_4_4_DTR,
  148. /* Octal SPI */
  149. SNOR_CMD_READ_1_1_8,
  150. SNOR_CMD_READ_1_8_8,
  151. SNOR_CMD_READ_8_8_8,
  152. SNOR_CMD_READ_1_8_8_DTR,
  153. SNOR_CMD_READ_8_8_8_DTR,
  154. SNOR_CMD_READ_MAX
  155. };
  156. enum spi_nor_pp_command_index {
  157. SNOR_CMD_PP,
  158. /* Quad SPI */
  159. SNOR_CMD_PP_1_1_4,
  160. SNOR_CMD_PP_1_4_4,
  161. SNOR_CMD_PP_4_4_4,
  162. /* Octal SPI */
  163. SNOR_CMD_PP_1_1_8,
  164. SNOR_CMD_PP_1_8_8,
  165. SNOR_CMD_PP_8_8_8,
  166. SNOR_CMD_PP_8_8_8_DTR,
  167. SNOR_CMD_PP_MAX
  168. };
  169. /**
  170. * struct spi_nor_erase_type - Structure to describe a SPI NOR erase type
  171. * @size: the size of the sector/block erased by the erase type.
  172. * JEDEC JESD216B imposes erase sizes to be a power of 2.
  173. * @size_shift: @size is a power of 2, the shift is stored in
  174. * @size_shift.
  175. * @size_mask: the size mask based on @size_shift.
  176. * @opcode: the SPI command op code to erase the sector/block.
  177. * @idx: Erase Type index as sorted in the Basic Flash Parameter
  178. * Table. It will be used to synchronize the supported
  179. * Erase Types with the ones identified in the SFDP
  180. * optional tables.
  181. */
  182. struct spi_nor_erase_type {
  183. u32 size;
  184. u32 size_shift;
  185. u32 size_mask;
  186. u8 opcode;
  187. u8 idx;
  188. };
  189. /**
  190. * struct spi_nor_erase_command - Used for non-uniform erases
  191. * The structure is used to describe a list of erase commands to be executed
  192. * once we validate that the erase can be performed. The elements in the list
  193. * are run-length encoded.
  194. * @list: for inclusion into the list of erase commands.
  195. * @count: how many times the same erase command should be
  196. * consecutively used.
  197. * @size: the size of the sector/block erased by the command.
  198. * @opcode: the SPI command op code to erase the sector/block.
  199. */
  200. struct spi_nor_erase_command {
  201. struct list_head list;
  202. u32 count;
  203. u32 size;
  204. u8 opcode;
  205. };
  206. /**
  207. * struct spi_nor_erase_region - Structure to describe a SPI NOR erase region
  208. * @offset: the offset in the data array of erase region start.
  209. * @size: the size of the region in bytes.
  210. * @erase_mask: bitmask to indicate all the supported erase commands
  211. * inside this region. The erase types are sorted in
  212. * ascending order with the smallest Erase Type size being
  213. * at BIT(0).
  214. * @overlaid: determine if this region is overlaid.
  215. */
  216. struct spi_nor_erase_region {
  217. u64 offset;
  218. u64 size;
  219. u8 erase_mask;
  220. bool overlaid;
  221. };
  222. #define SNOR_ERASE_TYPE_MAX 4
  223. /**
  224. * struct spi_nor_erase_map - Structure to describe the SPI NOR erase map
  225. * @regions: array of erase regions. The regions are consecutive in
  226. * address space. Walking through the regions is done
  227. * incrementally.
  228. * @uniform_region: a pre-allocated erase region for SPI NOR with a uniform
  229. * sector size (legacy implementation).
  230. * @erase_type: an array of erase types shared by all the regions.
  231. * The erase types are sorted in ascending order, with the
  232. * smallest Erase Type size being the first member in the
  233. * erase_type array.
  234. * @n_regions: number of erase regions.
  235. */
  236. struct spi_nor_erase_map {
  237. struct spi_nor_erase_region *regions;
  238. struct spi_nor_erase_region uniform_region;
  239. struct spi_nor_erase_type erase_type[SNOR_ERASE_TYPE_MAX];
  240. unsigned int n_regions;
  241. };
  242. /**
  243. * struct spi_nor_locking_ops - SPI NOR locking methods
  244. * @lock: lock a region of the SPI NOR.
  245. * @unlock: unlock a region of the SPI NOR.
  246. * @is_locked: check if a region of the SPI NOR is completely locked
  247. */
  248. struct spi_nor_locking_ops {
  249. int (*lock)(struct spi_nor *nor, loff_t ofs, u64 len);
  250. int (*unlock)(struct spi_nor *nor, loff_t ofs, u64 len);
  251. int (*is_locked)(struct spi_nor *nor, loff_t ofs, u64 len);
  252. };
  253. /**
  254. * struct spi_nor_otp_organization - Structure to describe the SPI NOR OTP regions
  255. * @len: size of one OTP region in bytes.
  256. * @base: start address of the OTP area.
  257. * @offset: offset between consecutive OTP regions if there are more
  258. * than one.
  259. * @n_regions: number of individual OTP regions.
  260. */
  261. struct spi_nor_otp_organization {
  262. size_t len;
  263. loff_t base;
  264. loff_t offset;
  265. unsigned int n_regions;
  266. };
  267. /**
  268. * struct spi_nor_otp_ops - SPI NOR OTP methods
  269. * @read: read from the SPI NOR OTP area.
  270. * @write: write to the SPI NOR OTP area.
  271. * @lock: lock an OTP region.
  272. * @erase: erase an OTP region.
  273. * @is_locked: check if an OTP region of the SPI NOR is locked.
  274. */
  275. struct spi_nor_otp_ops {
  276. int (*read)(struct spi_nor *nor, loff_t addr, size_t len, u8 *buf);
  277. int (*write)(struct spi_nor *nor, loff_t addr, size_t len,
  278. const u8 *buf);
  279. int (*lock)(struct spi_nor *nor, unsigned int region);
  280. int (*erase)(struct spi_nor *nor, loff_t addr);
  281. int (*is_locked)(struct spi_nor *nor, unsigned int region);
  282. };
  283. /**
  284. * struct spi_nor_otp - SPI NOR OTP grouping structure
  285. * @org: OTP region organization
  286. * @ops: OTP access ops
  287. */
  288. struct spi_nor_otp {
  289. const struct spi_nor_otp_organization *org;
  290. const struct spi_nor_otp_ops *ops;
  291. };
  292. /**
  293. * struct spi_nor_flash_parameter - SPI NOR flash parameters and settings.
  294. * Includes legacy flash parameters and settings that can be overwritten
  295. * by the spi_nor_fixups hooks, or dynamically when parsing the JESD216
  296. * Serial Flash Discoverable Parameters (SFDP) tables.
  297. *
  298. * @bank_size: the flash memory bank density in bytes.
  299. * @size: the total flash memory density in bytes.
  300. * @writesize Minimal writable flash unit size. Defaults to 1. Set to
  301. * ECC unit size for ECC-ed flashes.
  302. * @page_size: the page size of the SPI NOR flash memory.
  303. * @addr_nbytes: number of address bytes to send.
  304. * @addr_mode_nbytes: number of address bytes of current address mode. Useful
  305. * when the flash operates with 4B opcodes but needs the
  306. * internal address mode for opcodes that don't have a 4B
  307. * opcode correspondent.
  308. * @rdsr_dummy: dummy cycles needed for Read Status Register command
  309. * in octal DTR mode.
  310. * @rdsr_addr_nbytes: dummy address bytes needed for Read Status Register
  311. * command in octal DTR mode.
  312. * @n_banks: number of banks.
  313. * @n_dice: number of dice in the flash memory.
  314. * @die_erase_opcode: die erase opcode. Defaults to SPINOR_OP_CHIP_ERASE.
  315. * @vreg_offset: volatile register offset for each die.
  316. * @hwcaps: describes the read and page program hardware
  317. * capabilities.
  318. * @reads: read capabilities ordered by priority: the higher index
  319. * in the array, the higher priority.
  320. * @page_programs: page program capabilities ordered by priority: the
  321. * higher index in the array, the higher priority.
  322. * @erase_map: the erase map parsed from the SFDP Sector Map Parameter
  323. * Table.
  324. * @otp: SPI NOR OTP info.
  325. * @set_octal_dtr: enables or disables SPI NOR octal DTR mode.
  326. * @quad_enable: enables SPI NOR quad mode.
  327. * @set_4byte_addr_mode: puts the SPI NOR in 4 byte addressing mode.
  328. * @ready: (optional) flashes might use a different mechanism
  329. * than reading the status register to indicate they
  330. * are ready for a new command
  331. * @locking_ops: SPI NOR locking methods.
  332. * @priv: flash's private data.
  333. */
  334. struct spi_nor_flash_parameter {
  335. u64 bank_size;
  336. u64 size;
  337. u32 writesize;
  338. u32 page_size;
  339. u8 addr_nbytes;
  340. u8 addr_mode_nbytes;
  341. u8 rdsr_dummy;
  342. u8 rdsr_addr_nbytes;
  343. u8 n_banks;
  344. u8 n_dice;
  345. u8 die_erase_opcode;
  346. u32 *vreg_offset;
  347. struct spi_nor_hwcaps hwcaps;
  348. struct spi_nor_read_command reads[SNOR_CMD_READ_MAX];
  349. struct spi_nor_pp_command page_programs[SNOR_CMD_PP_MAX];
  350. struct spi_nor_erase_map erase_map;
  351. struct spi_nor_otp otp;
  352. int (*set_octal_dtr)(struct spi_nor *nor, bool enable);
  353. int (*quad_enable)(struct spi_nor *nor);
  354. int (*set_4byte_addr_mode)(struct spi_nor *nor, bool enable);
  355. int (*ready)(struct spi_nor *nor);
  356. const struct spi_nor_locking_ops *locking_ops;
  357. void *priv;
  358. };
  359. /**
  360. * struct spi_nor_fixups - SPI NOR fixup hooks
  361. * @default_init: called after default flash parameters init. Used to tweak
  362. * flash parameters when information provided by the flash_info
  363. * table is incomplete or wrong.
  364. * @post_bfpt: called after the BFPT table has been parsed
  365. * @smpt_read_dummy: called during SMPT table is being parsed. Used to fix the
  366. * number of dummy cycles in read register ops.
  367. * @smpt_map_id: called after map ID in SMPT table has been determined for the
  368. * case the map ID is wrong and needs to be fixed.
  369. * @post_sfdp: called after SFDP has been parsed (is also called for SPI NORs
  370. * that do not support RDSFDP). Typically used to tweak various
  371. * parameters that could not be extracted by other means (i.e.
  372. * when information provided by the SFDP/flash_info tables are
  373. * incomplete or wrong).
  374. * @late_init: used to initialize flash parameters that are not declared in the
  375. * JESD216 SFDP standard, or where SFDP tables not defined at all.
  376. * Will replace the default_init() hook.
  377. *
  378. * Those hooks can be used to tweak the SPI NOR configuration when the SFDP
  379. * table is broken or not available.
  380. */
  381. struct spi_nor_fixups {
  382. void (*default_init)(struct spi_nor *nor);
  383. int (*post_bfpt)(struct spi_nor *nor,
  384. const struct sfdp_parameter_header *bfpt_header,
  385. const struct sfdp_bfpt *bfpt);
  386. void (*smpt_read_dummy)(const struct spi_nor *nor, u8 *read_dummy);
  387. void (*smpt_map_id)(const struct spi_nor *nor, u8 *map_id);
  388. int (*post_sfdp)(struct spi_nor *nor);
  389. int (*late_init)(struct spi_nor *nor);
  390. };
  391. /**
  392. * struct spi_nor_id - SPI NOR flash ID.
  393. *
  394. * @bytes: the bytes returned by the flash when issuing command 9F. Typically,
  395. * the first byte is the manufacturer ID code (see JEP106) and the next
  396. * two bytes are a flash part specific ID.
  397. * @len: the number of bytes of ID.
  398. */
  399. struct spi_nor_id {
  400. const u8 *bytes;
  401. u8 len;
  402. };
  403. /**
  404. * struct flash_info - SPI NOR flash_info entry.
  405. * @id: pointer to struct spi_nor_id or NULL, which means "no ID" (mostly
  406. * older chips).
  407. * @name: (obsolete) the name of the flash. Do not set it for new additions.
  408. * @size: the size of the flash in bytes. The flash size is one
  409. * property parsed by the SFDP. We use it as an indicator
  410. * whether we need SFDP parsing for a particular flash.
  411. * I.e. non-legacy flash entries in flash_info will have
  412. * a size of zero iff SFDP should be used.
  413. * @sector_size: (optional) the size listed here is what works with
  414. * SPINOR_OP_SE, which isn't necessarily called a "sector" by
  415. * the vendor. Defaults to 64k.
  416. * @n_banks: (optional) the number of banks. Defaults to 1.
  417. * @page_size: (optional) the flash's page size. Defaults to 256.
  418. * @addr_nbytes: number of address bytes to send.
  419. *
  420. * @flags: flags that indicate support that is not defined by the
  421. * JESD216 standard in its SFDP tables. Flag meanings:
  422. * SPI_NOR_HAS_LOCK: flash supports lock/unlock via SR
  423. * SPI_NOR_HAS_TB: flash SR has Top/Bottom (TB) protect bit. Must be
  424. * used with SPI_NOR_HAS_LOCK.
  425. * SPI_NOR_TB_SR_BIT6: Top/Bottom (TB) is bit 6 of status register.
  426. * Must be used with SPI_NOR_HAS_TB.
  427. * SPI_NOR_4BIT_BP: flash SR has 4 bit fields (BP0-3) for block
  428. * protection.
  429. * SPI_NOR_BP3_SR_BIT6: BP3 is bit 6 of status register. Must be used with
  430. * SPI_NOR_4BIT_BP.
  431. * SPI_NOR_SWP_IS_VOLATILE: flash has volatile software write protection bits.
  432. * Usually these will power-up in a write-protected
  433. * state.
  434. * SPI_NOR_NO_ERASE: no erase command needed.
  435. * SPI_NOR_QUAD_PP: flash supports Quad Input Page Program.
  436. * SPI_NOR_RWW: flash supports reads while write.
  437. *
  438. * @no_sfdp_flags: flags that indicate support that can be discovered via SFDP.
  439. * Used when SFDP tables are not defined in the flash. These
  440. * flags are used together with the SPI_NOR_SKIP_SFDP flag.
  441. * SPI_NOR_SKIP_SFDP: skip parsing of SFDP tables.
  442. * SECT_4K: SPINOR_OP_BE_4K works uniformly.
  443. * SPI_NOR_DUAL_READ: flash supports Dual Read.
  444. * SPI_NOR_QUAD_READ: flash supports Quad Read.
  445. * SPI_NOR_OCTAL_READ: flash supports Octal Read.
  446. * SPI_NOR_OCTAL_DTR_READ: flash supports octal DTR Read.
  447. * SPI_NOR_OCTAL_DTR_PP: flash supports Octal DTR Page Program.
  448. *
  449. * @fixup_flags: flags that indicate support that can be discovered via SFDP
  450. * ideally, but can not be discovered for this particular flash
  451. * because the SFDP table that indicates this support is not
  452. * defined by the flash. In case the table for this support is
  453. * defined but has wrong values, one should instead use a
  454. * post_sfdp() hook to set the SNOR_F equivalent flag.
  455. *
  456. * SPI_NOR_4B_OPCODES: use dedicated 4byte address op codes to support
  457. * memory size above 128Mib.
  458. * SPI_NOR_IO_MODE_EN_VOLATILE: flash enables the best available I/O mode
  459. * via a volatile bit.
  460. * @mfr_flags: manufacturer private flags. Used in the manufacturer fixup
  461. * hooks to differentiate support between flashes of the same
  462. * manufacturer.
  463. * @otp_org: flash's OTP organization.
  464. * @fixups: part specific fixup hooks.
  465. */
  466. struct flash_info {
  467. char *name;
  468. const struct spi_nor_id *id;
  469. size_t size;
  470. unsigned sector_size;
  471. u16 page_size;
  472. u8 n_banks;
  473. u8 addr_nbytes;
  474. u16 flags;
  475. #define SPI_NOR_HAS_LOCK BIT(0)
  476. #define SPI_NOR_HAS_TB BIT(1)
  477. #define SPI_NOR_TB_SR_BIT6 BIT(2)
  478. #define SPI_NOR_4BIT_BP BIT(3)
  479. #define SPI_NOR_BP3_SR_BIT6 BIT(4)
  480. #define SPI_NOR_SWP_IS_VOLATILE BIT(5)
  481. #define SPI_NOR_NO_ERASE BIT(6)
  482. #define SPI_NOR_QUAD_PP BIT(8)
  483. #define SPI_NOR_RWW BIT(9)
  484. u8 no_sfdp_flags;
  485. #define SPI_NOR_SKIP_SFDP BIT(0)
  486. #define SECT_4K BIT(1)
  487. #define SPI_NOR_DUAL_READ BIT(3)
  488. #define SPI_NOR_QUAD_READ BIT(4)
  489. #define SPI_NOR_OCTAL_READ BIT(5)
  490. #define SPI_NOR_OCTAL_DTR_READ BIT(6)
  491. #define SPI_NOR_OCTAL_DTR_PP BIT(7)
  492. u8 fixup_flags;
  493. #define SPI_NOR_4B_OPCODES BIT(0)
  494. #define SPI_NOR_IO_MODE_EN_VOLATILE BIT(1)
  495. u8 mfr_flags;
  496. const struct spi_nor_otp_organization *otp;
  497. const struct spi_nor_fixups *fixups;
  498. };
  499. #define SNOR_ID(...) \
  500. (&(const struct spi_nor_id){ \
  501. .bytes = (const u8[]){ __VA_ARGS__ }, \
  502. .len = sizeof((u8[]){ __VA_ARGS__ }), \
  503. })
  504. #define SNOR_OTP(_len, _n_regions, _base, _offset) \
  505. (&(const struct spi_nor_otp_organization){ \
  506. .len = (_len), \
  507. .base = (_base), \
  508. .offset = (_offset), \
  509. .n_regions = (_n_regions), \
  510. })
  511. /**
  512. * struct spi_nor_manufacturer - SPI NOR manufacturer object
  513. * @name: manufacturer name
  514. * @parts: array of parts supported by this manufacturer
  515. * @nparts: number of entries in the parts array
  516. * @fixups: hooks called at various points in time during spi_nor_scan()
  517. */
  518. struct spi_nor_manufacturer {
  519. const char *name;
  520. const struct flash_info *parts;
  521. unsigned int nparts;
  522. const struct spi_nor_fixups *fixups;
  523. };
  524. /**
  525. * struct sfdp - SFDP data
  526. * @num_dwords: number of entries in the dwords array
  527. * @dwords: array of double words of the SFDP data
  528. */
  529. struct sfdp {
  530. size_t num_dwords;
  531. u32 *dwords;
  532. };
  533. /* Manufacturer drivers. */
  534. extern const struct spi_nor_manufacturer spi_nor_atmel;
  535. extern const struct spi_nor_manufacturer spi_nor_eon;
  536. extern const struct spi_nor_manufacturer spi_nor_esmt;
  537. extern const struct spi_nor_manufacturer spi_nor_everspin;
  538. extern const struct spi_nor_manufacturer spi_nor_gigadevice;
  539. extern const struct spi_nor_manufacturer spi_nor_intel;
  540. extern const struct spi_nor_manufacturer spi_nor_issi;
  541. extern const struct spi_nor_manufacturer spi_nor_macronix;
  542. extern const struct spi_nor_manufacturer spi_nor_micron;
  543. extern const struct spi_nor_manufacturer spi_nor_st;
  544. extern const struct spi_nor_manufacturer spi_nor_spansion;
  545. extern const struct spi_nor_manufacturer spi_nor_sst;
  546. extern const struct spi_nor_manufacturer spi_nor_winbond;
  547. extern const struct spi_nor_manufacturer spi_nor_xmc;
  548. extern const struct attribute_group *spi_nor_sysfs_groups[];
  549. void spi_nor_spimem_setup_op(const struct spi_nor *nor,
  550. struct spi_mem_op *op,
  551. const enum spi_nor_protocol proto);
  552. int spi_nor_write_enable(struct spi_nor *nor);
  553. int spi_nor_write_disable(struct spi_nor *nor);
  554. int spi_nor_set_4byte_addr_mode_en4b_ex4b(struct spi_nor *nor, bool enable);
  555. int spi_nor_set_4byte_addr_mode_wren_en4b_ex4b(struct spi_nor *nor,
  556. bool enable);
  557. int spi_nor_set_4byte_addr_mode_brwr(struct spi_nor *nor, bool enable);
  558. int spi_nor_set_4byte_addr_mode(struct spi_nor *nor, bool enable);
  559. int spi_nor_wait_till_ready(struct spi_nor *nor);
  560. int spi_nor_global_block_unlock(struct spi_nor *nor);
  561. int spi_nor_prep_and_lock(struct spi_nor *nor);
  562. void spi_nor_unlock_and_unprep(struct spi_nor *nor);
  563. int spi_nor_sr1_bit6_quad_enable(struct spi_nor *nor);
  564. int spi_nor_sr2_bit1_quad_enable(struct spi_nor *nor);
  565. int spi_nor_sr2_bit7_quad_enable(struct spi_nor *nor);
  566. int spi_nor_read_id(struct spi_nor *nor, u8 naddr, u8 ndummy, u8 *id,
  567. enum spi_nor_protocol reg_proto);
  568. int spi_nor_read_sr(struct spi_nor *nor, u8 *sr);
  569. int spi_nor_sr_ready(struct spi_nor *nor);
  570. int spi_nor_read_cr(struct spi_nor *nor, u8 *cr);
  571. int spi_nor_write_sr(struct spi_nor *nor, const u8 *sr, size_t len);
  572. int spi_nor_write_sr_and_check(struct spi_nor *nor, u8 sr1);
  573. int spi_nor_write_16bit_cr_and_check(struct spi_nor *nor, u8 cr);
  574. ssize_t spi_nor_read_data(struct spi_nor *nor, loff_t from, size_t len,
  575. u8 *buf);
  576. ssize_t spi_nor_write_data(struct spi_nor *nor, loff_t to, size_t len,
  577. const u8 *buf);
  578. int spi_nor_read_any_reg(struct spi_nor *nor, struct spi_mem_op *op,
  579. enum spi_nor_protocol proto);
  580. int spi_nor_write_any_volatile_reg(struct spi_nor *nor, struct spi_mem_op *op,
  581. enum spi_nor_protocol proto);
  582. int spi_nor_erase_sector(struct spi_nor *nor, u32 addr);
  583. int spi_nor_otp_read_secr(struct spi_nor *nor, loff_t addr, size_t len, u8 *buf);
  584. int spi_nor_otp_write_secr(struct spi_nor *nor, loff_t addr, size_t len,
  585. const u8 *buf);
  586. int spi_nor_otp_erase_secr(struct spi_nor *nor, loff_t addr);
  587. int spi_nor_otp_lock_sr2(struct spi_nor *nor, unsigned int region);
  588. int spi_nor_otp_is_locked_sr2(struct spi_nor *nor, unsigned int region);
  589. int spi_nor_hwcaps_read2cmd(u32 hwcaps);
  590. int spi_nor_hwcaps_pp2cmd(u32 hwcaps);
  591. u8 spi_nor_convert_3to4_read(u8 opcode);
  592. void spi_nor_set_read_settings(struct spi_nor_read_command *read,
  593. u8 num_mode_clocks,
  594. u8 num_wait_states,
  595. u8 opcode,
  596. enum spi_nor_protocol proto);
  597. void spi_nor_set_pp_settings(struct spi_nor_pp_command *pp, u8 opcode,
  598. enum spi_nor_protocol proto);
  599. void spi_nor_set_erase_type(struct spi_nor_erase_type *erase, u32 size,
  600. u8 opcode);
  601. void spi_nor_mask_erase_type(struct spi_nor_erase_type *erase);
  602. void spi_nor_init_uniform_erase_map(struct spi_nor_erase_map *map,
  603. u8 erase_mask, u64 flash_size);
  604. int spi_nor_post_bfpt_fixups(struct spi_nor *nor,
  605. const struct sfdp_parameter_header *bfpt_header,
  606. const struct sfdp_bfpt *bfpt);
  607. void spi_nor_init_default_locking_ops(struct spi_nor *nor);
  608. void spi_nor_try_unlock_all(struct spi_nor *nor);
  609. void spi_nor_set_mtd_locking_ops(struct spi_nor *nor);
  610. void spi_nor_set_mtd_otp_ops(struct spi_nor *nor);
  611. int spi_nor_controller_ops_read_reg(struct spi_nor *nor, u8 opcode,
  612. u8 *buf, size_t len);
  613. int spi_nor_controller_ops_write_reg(struct spi_nor *nor, u8 opcode,
  614. const u8 *buf, size_t len);
  615. int spi_nor_check_sfdp_signature(struct spi_nor *nor);
  616. int spi_nor_parse_sfdp(struct spi_nor *nor);
  617. static inline struct spi_nor *mtd_to_spi_nor(struct mtd_info *mtd)
  618. {
  619. return container_of(mtd, struct spi_nor, mtd);
  620. }
  621. /**
  622. * spi_nor_needs_sfdp() - returns true if SFDP parsing is used for this flash.
  623. *
  624. * Return: true if SFDP parsing is needed
  625. */
  626. static inline bool spi_nor_needs_sfdp(const struct spi_nor *nor)
  627. {
  628. /*
  629. * The flash size is one property parsed by the SFDP. We use it as an
  630. * indicator whether we need SFDP parsing for a particular flash. I.e.
  631. * non-legacy flash entries in flash_info will have a size of zero iff
  632. * SFDP should be used.
  633. */
  634. return !nor->info->size;
  635. }
  636. #ifdef CONFIG_DEBUG_FS
  637. void spi_nor_debugfs_register(struct spi_nor *nor);
  638. void spi_nor_debugfs_shutdown(void);
  639. #else
  640. static inline void spi_nor_debugfs_register(struct spi_nor *nor) {}
  641. static inline void spi_nor_debugfs_shutdown(void) {}
  642. #endif
  643. #endif /* __LINUX_MTD_SPI_NOR_INTERNAL_H */