core.h 24 KB

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