spinand.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (c) 2016-2017 Micron Technology, Inc.
  4. *
  5. * Authors:
  6. * Peter Pan <peterpandong@micron.com>
  7. */
  8. #ifndef __LINUX_MTD_SPINAND_H
  9. #define __LINUX_MTD_SPINAND_H
  10. #include <linux/mutex.h>
  11. #include <linux/bitops.h>
  12. #include <linux/device.h>
  13. #include <linux/mtd/mtd.h>
  14. #include <linux/mtd/nand.h>
  15. #include <linux/spi/spi.h>
  16. #include <linux/spi/spi-mem.h>
  17. /**
  18. * Standard SPI NAND flash operations
  19. */
  20. #define SPINAND_RESET_OP \
  21. SPI_MEM_OP(SPI_MEM_OP_CMD(0xff, 1), \
  22. SPI_MEM_OP_NO_ADDR, \
  23. SPI_MEM_OP_NO_DUMMY, \
  24. SPI_MEM_OP_NO_DATA)
  25. #define SPINAND_WR_EN_DIS_OP(enable) \
  26. SPI_MEM_OP(SPI_MEM_OP_CMD((enable) ? 0x06 : 0x04, 1), \
  27. SPI_MEM_OP_NO_ADDR, \
  28. SPI_MEM_OP_NO_DUMMY, \
  29. SPI_MEM_OP_NO_DATA)
  30. #define SPINAND_READID_OP(ndummy, buf, len) \
  31. SPI_MEM_OP(SPI_MEM_OP_CMD(0x9f, 1), \
  32. SPI_MEM_OP_NO_ADDR, \
  33. SPI_MEM_OP_DUMMY(ndummy, 1), \
  34. SPI_MEM_OP_DATA_IN(len, buf, 1))
  35. #define SPINAND_SET_FEATURE_OP(reg, valptr) \
  36. SPI_MEM_OP(SPI_MEM_OP_CMD(0x1f, 1), \
  37. SPI_MEM_OP_ADDR(1, reg, 1), \
  38. SPI_MEM_OP_NO_DUMMY, \
  39. SPI_MEM_OP_DATA_OUT(1, valptr, 1))
  40. #define SPINAND_GET_FEATURE_OP(reg, valptr) \
  41. SPI_MEM_OP(SPI_MEM_OP_CMD(0x0f, 1), \
  42. SPI_MEM_OP_ADDR(1, reg, 1), \
  43. SPI_MEM_OP_NO_DUMMY, \
  44. SPI_MEM_OP_DATA_IN(1, valptr, 1))
  45. #define SPINAND_BLK_ERASE_OP(addr) \
  46. SPI_MEM_OP(SPI_MEM_OP_CMD(0xd8, 1), \
  47. SPI_MEM_OP_ADDR(3, addr, 1), \
  48. SPI_MEM_OP_NO_DUMMY, \
  49. SPI_MEM_OP_NO_DATA)
  50. #define SPINAND_PAGE_READ_OP(addr) \
  51. SPI_MEM_OP(SPI_MEM_OP_CMD(0x13, 1), \
  52. SPI_MEM_OP_ADDR(3, addr, 1), \
  53. SPI_MEM_OP_NO_DUMMY, \
  54. SPI_MEM_OP_NO_DATA)
  55. #define SPINAND_PAGE_READ_FROM_CACHE_OP(fast, addr, ndummy, buf, len) \
  56. SPI_MEM_OP(SPI_MEM_OP_CMD(fast ? 0x0b : 0x03, 1), \
  57. SPI_MEM_OP_ADDR(2, addr, 1), \
  58. SPI_MEM_OP_DUMMY(ndummy, 1), \
  59. SPI_MEM_OP_DATA_IN(len, buf, 1))
  60. #define SPINAND_PAGE_READ_FROM_CACHE_X2_OP(addr, ndummy, buf, len) \
  61. SPI_MEM_OP(SPI_MEM_OP_CMD(0x3b, 1), \
  62. SPI_MEM_OP_ADDR(2, addr, 1), \
  63. SPI_MEM_OP_DUMMY(ndummy, 1), \
  64. SPI_MEM_OP_DATA_IN(len, buf, 2))
  65. #define SPINAND_PAGE_READ_FROM_CACHE_X4_OP(addr, ndummy, buf, len) \
  66. SPI_MEM_OP(SPI_MEM_OP_CMD(0x6b, 1), \
  67. SPI_MEM_OP_ADDR(2, addr, 1), \
  68. SPI_MEM_OP_DUMMY(ndummy, 1), \
  69. SPI_MEM_OP_DATA_IN(len, buf, 4))
  70. #define SPINAND_PAGE_READ_FROM_CACHE_DUALIO_OP(addr, ndummy, buf, len) \
  71. SPI_MEM_OP(SPI_MEM_OP_CMD(0xbb, 1), \
  72. SPI_MEM_OP_ADDR(2, addr, 2), \
  73. SPI_MEM_OP_DUMMY(ndummy, 2), \
  74. SPI_MEM_OP_DATA_IN(len, buf, 2))
  75. #define SPINAND_PAGE_READ_FROM_CACHE_QUADIO_OP(addr, ndummy, buf, len) \
  76. SPI_MEM_OP(SPI_MEM_OP_CMD(0xeb, 1), \
  77. SPI_MEM_OP_ADDR(2, addr, 4), \
  78. SPI_MEM_OP_DUMMY(ndummy, 4), \
  79. SPI_MEM_OP_DATA_IN(len, buf, 4))
  80. #define SPINAND_PROG_EXEC_OP(addr) \
  81. SPI_MEM_OP(SPI_MEM_OP_CMD(0x10, 1), \
  82. SPI_MEM_OP_ADDR(3, addr, 1), \
  83. SPI_MEM_OP_NO_DUMMY, \
  84. SPI_MEM_OP_NO_DATA)
  85. #define SPINAND_PROG_LOAD(reset, addr, buf, len) \
  86. SPI_MEM_OP(SPI_MEM_OP_CMD(reset ? 0x02 : 0x84, 1), \
  87. SPI_MEM_OP_ADDR(2, addr, 1), \
  88. SPI_MEM_OP_NO_DUMMY, \
  89. SPI_MEM_OP_DATA_OUT(len, buf, 1))
  90. #define SPINAND_PROG_LOAD_X4(reset, addr, buf, len) \
  91. SPI_MEM_OP(SPI_MEM_OP_CMD(reset ? 0x32 : 0x34, 1), \
  92. SPI_MEM_OP_ADDR(2, addr, 1), \
  93. SPI_MEM_OP_NO_DUMMY, \
  94. SPI_MEM_OP_DATA_OUT(len, buf, 4))
  95. /**
  96. * Standard SPI NAND flash commands
  97. */
  98. #define SPINAND_CMD_PROG_LOAD_X4 0x32
  99. #define SPINAND_CMD_PROG_LOAD_RDM_DATA_X4 0x34
  100. /* feature register */
  101. #define REG_BLOCK_LOCK 0xa0
  102. #define BL_ALL_UNLOCKED 0x00
  103. /* configuration register */
  104. #define REG_CFG 0xb0
  105. #define CFG_OTP_ENABLE BIT(6)
  106. #define CFG_ECC_ENABLE BIT(4)
  107. #define CFG_QUAD_ENABLE BIT(0)
  108. /* status register */
  109. #define REG_STATUS 0xc0
  110. #define STATUS_BUSY BIT(0)
  111. #define STATUS_ERASE_FAILED BIT(2)
  112. #define STATUS_PROG_FAILED BIT(3)
  113. #define STATUS_ECC_MASK GENMASK(5, 4)
  114. #define STATUS_ECC_NO_BITFLIPS (0 << 4)
  115. #define STATUS_ECC_HAS_BITFLIPS (1 << 4)
  116. #define STATUS_ECC_UNCOR_ERROR (2 << 4)
  117. struct spinand_op;
  118. struct spinand_device;
  119. #define SPINAND_MAX_ID_LEN 4
  120. /**
  121. * struct spinand_id - SPI NAND id structure
  122. * @data: buffer containing the id bytes. Currently 4 bytes large, but can
  123. * be extended if required
  124. * @len: ID length
  125. *
  126. * struct_spinand_id->data contains all bytes returned after a READ_ID command,
  127. * including dummy bytes if the chip does not emit ID bytes right after the
  128. * READ_ID command. The responsibility to extract real ID bytes is left to
  129. * struct_manufacurer_ops->detect().
  130. */
  131. struct spinand_id {
  132. u8 data[SPINAND_MAX_ID_LEN];
  133. int len;
  134. };
  135. /**
  136. * struct manufacurer_ops - SPI NAND manufacturer specific operations
  137. * @detect: detect a SPI NAND device. Every time a SPI NAND device is probed
  138. * the core calls the struct_manufacurer_ops->detect() hook of each
  139. * registered manufacturer until one of them return 1. Note that
  140. * the first thing to check in this hook is that the manufacturer ID
  141. * in struct_spinand_device->id matches the manufacturer whose
  142. * ->detect() hook has been called. Should return 1 if there's a
  143. * match, 0 if the manufacturer ID does not match and a negative
  144. * error code otherwise. When true is returned, the core assumes
  145. * that properties of the NAND chip (spinand->base.memorg and
  146. * spinand->base.eccreq) have been filled
  147. * @init: initialize a SPI NAND device
  148. * @cleanup: cleanup a SPI NAND device
  149. *
  150. * Each SPI NAND manufacturer driver should implement this interface so that
  151. * NAND chips coming from this vendor can be detected and initialized properly.
  152. */
  153. struct spinand_manufacturer_ops {
  154. int (*detect)(struct spinand_device *spinand);
  155. int (*init)(struct spinand_device *spinand);
  156. void (*cleanup)(struct spinand_device *spinand);
  157. };
  158. /**
  159. * struct spinand_manufacturer - SPI NAND manufacturer instance
  160. * @id: manufacturer ID
  161. * @name: manufacturer name
  162. * @ops: manufacturer operations
  163. */
  164. struct spinand_manufacturer {
  165. u8 id;
  166. char *name;
  167. const struct spinand_manufacturer_ops *ops;
  168. };
  169. /**
  170. * struct spinand_op_variants - SPI NAND operation variants
  171. * @ops: the list of variants for a given operation
  172. * @nops: the number of variants
  173. *
  174. * Some operations like read-from-cache/write-to-cache have several variants
  175. * depending on the number of IO lines you use to transfer data or address
  176. * cycles. This structure is a way to describe the different variants supported
  177. * by a chip and let the core pick the best one based on the SPI mem controller
  178. * capabilities.
  179. */
  180. struct spinand_op_variants {
  181. const struct spi_mem_op *ops;
  182. unsigned int nops;
  183. };
  184. #define SPINAND_OP_VARIANTS(name, ...) \
  185. const struct spinand_op_variants name = { \
  186. .ops = (struct spi_mem_op[]) { __VA_ARGS__ }, \
  187. .nops = sizeof((struct spi_mem_op[]){ __VA_ARGS__ }) / \
  188. sizeof(struct spi_mem_op), \
  189. }
  190. /**
  191. * spinand_ecc_info - description of the on-die ECC implemented by a SPI NAND
  192. * chip
  193. * @get_status: get the ECC status. Should return a positive number encoding
  194. * the number of corrected bitflips if correction was possible or
  195. * -EBADMSG if there are uncorrectable errors. I can also return
  196. * other negative error codes if the error is not caused by
  197. * uncorrectable bitflips
  198. * @ooblayout: the OOB layout used by the on-die ECC implementation
  199. */
  200. struct spinand_ecc_info {
  201. int (*get_status)(struct spinand_device *spinand, u8 status);
  202. const struct mtd_ooblayout_ops *ooblayout;
  203. };
  204. #define SPINAND_HAS_QE_BIT BIT(0)
  205. /**
  206. * struct spinand_info - Structure used to describe SPI NAND chips
  207. * @model: model name
  208. * @devid: device ID
  209. * @flags: OR-ing of the SPINAND_XXX flags
  210. * @memorg: memory organization
  211. * @eccreq: ECC requirements
  212. * @eccinfo: on-die ECC info
  213. * @op_variants: operations variants
  214. * @op_variants.read_cache: variants of the read-cache operation
  215. * @op_variants.write_cache: variants of the write-cache operation
  216. * @op_variants.update_cache: variants of the update-cache operation
  217. * @select_target: function used to select a target/die. Required only for
  218. * multi-die chips
  219. *
  220. * Each SPI NAND manufacturer driver should have a spinand_info table
  221. * describing all the chips supported by the driver.
  222. */
  223. struct spinand_info {
  224. const char *model;
  225. u8 devid;
  226. u32 flags;
  227. struct nand_memory_organization memorg;
  228. struct nand_ecc_req eccreq;
  229. struct spinand_ecc_info eccinfo;
  230. struct {
  231. const struct spinand_op_variants *read_cache;
  232. const struct spinand_op_variants *write_cache;
  233. const struct spinand_op_variants *update_cache;
  234. } op_variants;
  235. int (*select_target)(struct spinand_device *spinand,
  236. unsigned int target);
  237. };
  238. #define SPINAND_INFO_OP_VARIANTS(__read, __write, __update) \
  239. { \
  240. .read_cache = __read, \
  241. .write_cache = __write, \
  242. .update_cache = __update, \
  243. }
  244. #define SPINAND_ECCINFO(__ooblayout, __get_status) \
  245. .eccinfo = { \
  246. .ooblayout = __ooblayout, \
  247. .get_status = __get_status, \
  248. }
  249. #define SPINAND_SELECT_TARGET(__func) \
  250. .select_target = __func,
  251. #define SPINAND_INFO(__model, __id, __memorg, __eccreq, __op_variants, \
  252. __flags, ...) \
  253. { \
  254. .model = __model, \
  255. .devid = __id, \
  256. .memorg = __memorg, \
  257. .eccreq = __eccreq, \
  258. .op_variants = __op_variants, \
  259. .flags = __flags, \
  260. __VA_ARGS__ \
  261. }
  262. /**
  263. * struct spinand_device - SPI NAND device instance
  264. * @base: NAND device instance
  265. * @spimem: pointer to the SPI mem object
  266. * @lock: lock used to serialize accesses to the NAND
  267. * @id: NAND ID as returned by READ_ID
  268. * @flags: NAND flags
  269. * @op_templates: various SPI mem op templates
  270. * @op_templates.read_cache: read cache op template
  271. * @op_templates.write_cache: write cache op template
  272. * @op_templates.update_cache: update cache op template
  273. * @select_target: select a specific target/die. Usually called before sending
  274. * a command addressing a page or an eraseblock embedded in
  275. * this die. Only required if your chip exposes several dies
  276. * @cur_target: currently selected target/die
  277. * @eccinfo: on-die ECC information
  278. * @cfg_cache: config register cache. One entry per die
  279. * @databuf: bounce buffer for data
  280. * @oobbuf: bounce buffer for OOB data
  281. * @scratchbuf: buffer used for everything but page accesses. This is needed
  282. * because the spi-mem interface explicitly requests that buffers
  283. * passed in spi_mem_op be DMA-able, so we can't based the bufs on
  284. * the stack
  285. * @manufacturer: SPI NAND manufacturer information
  286. * @priv: manufacturer private data
  287. */
  288. struct spinand_device {
  289. struct nand_device base;
  290. struct spi_mem *spimem;
  291. struct mutex lock;
  292. struct spinand_id id;
  293. u32 flags;
  294. struct {
  295. const struct spi_mem_op *read_cache;
  296. const struct spi_mem_op *write_cache;
  297. const struct spi_mem_op *update_cache;
  298. } op_templates;
  299. int (*select_target)(struct spinand_device *spinand,
  300. unsigned int target);
  301. unsigned int cur_target;
  302. struct spinand_ecc_info eccinfo;
  303. u8 *cfg_cache;
  304. u8 *databuf;
  305. u8 *oobbuf;
  306. u8 *scratchbuf;
  307. const struct spinand_manufacturer *manufacturer;
  308. void *priv;
  309. };
  310. /**
  311. * mtd_to_spinand() - Get the SPI NAND device attached to an MTD instance
  312. * @mtd: MTD instance
  313. *
  314. * Return: the SPI NAND device attached to @mtd.
  315. */
  316. static inline struct spinand_device *mtd_to_spinand(struct mtd_info *mtd)
  317. {
  318. return container_of(mtd_to_nanddev(mtd), struct spinand_device, base);
  319. }
  320. /**
  321. * spinand_to_mtd() - Get the MTD device embedded in a SPI NAND device
  322. * @spinand: SPI NAND device
  323. *
  324. * Return: the MTD device embedded in @spinand.
  325. */
  326. static inline struct mtd_info *spinand_to_mtd(struct spinand_device *spinand)
  327. {
  328. return nanddev_to_mtd(&spinand->base);
  329. }
  330. /**
  331. * nand_to_spinand() - Get the SPI NAND device embedding an NAND object
  332. * @nand: NAND object
  333. *
  334. * Return: the SPI NAND device embedding @nand.
  335. */
  336. static inline struct spinand_device *nand_to_spinand(struct nand_device *nand)
  337. {
  338. return container_of(nand, struct spinand_device, base);
  339. }
  340. /**
  341. * spinand_to_nand() - Get the NAND device embedded in a SPI NAND object
  342. * @spinand: SPI NAND device
  343. *
  344. * Return: the NAND device embedded in @spinand.
  345. */
  346. static inline struct nand_device *
  347. spinand_to_nand(struct spinand_device *spinand)
  348. {
  349. return &spinand->base;
  350. }
  351. /**
  352. * spinand_set_of_node - Attach a DT node to a SPI NAND device
  353. * @spinand: SPI NAND device
  354. * @np: DT node
  355. *
  356. * Attach a DT node to a SPI NAND device.
  357. */
  358. static inline void spinand_set_of_node(struct spinand_device *spinand,
  359. struct device_node *np)
  360. {
  361. nanddev_set_of_node(&spinand->base, np);
  362. }
  363. int spinand_match_and_init(struct spinand_device *dev,
  364. const struct spinand_info *table,
  365. unsigned int table_size, u8 devid);
  366. int spinand_upd_cfg(struct spinand_device *spinand, u8 mask, u8 val);
  367. int spinand_select_target(struct spinand_device *spinand, unsigned int target);
  368. #endif /* __LINUX_MTD_SPINAND_H */