vf610_nfc.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2009-2015 Freescale Semiconductor, Inc. and others
  4. *
  5. * Description: MPC5125, VF610, MCF54418 and Kinetis K70 Nand driver.
  6. * Jason ported to M54418TWR and MVFA5 (VF610).
  7. * Authors: Stefan Agner <[email protected]>
  8. * Bill Pringlemeir <[email protected]>
  9. * Shaohui Xie <[email protected]>
  10. * Jason Jin <[email protected]>
  11. *
  12. * Based on original driver mpc5121_nfc.c.
  13. *
  14. * Limitations:
  15. * - Untested on MPC5125 and M54418.
  16. * - DMA and pipelining not used.
  17. * - 2K pages or less.
  18. * - HW ECC: Only 2K page with 64+ OOB.
  19. * - HW ECC: Only 24 and 32-bit error correction implemented.
  20. */
  21. #include <linux/module.h>
  22. #include <linux/bitops.h>
  23. #include <linux/clk.h>
  24. #include <linux/delay.h>
  25. #include <linux/init.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/io.h>
  28. #include <linux/mtd/mtd.h>
  29. #include <linux/mtd/rawnand.h>
  30. #include <linux/mtd/partitions.h>
  31. #include <linux/of_device.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/slab.h>
  34. #include <linux/swab.h>
  35. #define DRV_NAME "vf610_nfc"
  36. /* Register Offsets */
  37. #define NFC_FLASH_CMD1 0x3F00
  38. #define NFC_FLASH_CMD2 0x3F04
  39. #define NFC_COL_ADDR 0x3F08
  40. #define NFC_ROW_ADDR 0x3F0c
  41. #define NFC_ROW_ADDR_INC 0x3F14
  42. #define NFC_FLASH_STATUS1 0x3F18
  43. #define NFC_FLASH_STATUS2 0x3F1c
  44. #define NFC_CACHE_SWAP 0x3F28
  45. #define NFC_SECTOR_SIZE 0x3F2c
  46. #define NFC_FLASH_CONFIG 0x3F30
  47. #define NFC_IRQ_STATUS 0x3F38
  48. /* Addresses for NFC MAIN RAM BUFFER areas */
  49. #define NFC_MAIN_AREA(n) ((n) * 0x1000)
  50. #define PAGE_2K 0x0800
  51. #define OOB_64 0x0040
  52. #define OOB_MAX 0x0100
  53. /* NFC_CMD2[CODE] controller cycle bit masks */
  54. #define COMMAND_CMD_BYTE1 BIT(14)
  55. #define COMMAND_CAR_BYTE1 BIT(13)
  56. #define COMMAND_CAR_BYTE2 BIT(12)
  57. #define COMMAND_RAR_BYTE1 BIT(11)
  58. #define COMMAND_RAR_BYTE2 BIT(10)
  59. #define COMMAND_RAR_BYTE3 BIT(9)
  60. #define COMMAND_NADDR_BYTES(x) GENMASK(13, 13 - (x) + 1)
  61. #define COMMAND_WRITE_DATA BIT(8)
  62. #define COMMAND_CMD_BYTE2 BIT(7)
  63. #define COMMAND_RB_HANDSHAKE BIT(6)
  64. #define COMMAND_READ_DATA BIT(5)
  65. #define COMMAND_CMD_BYTE3 BIT(4)
  66. #define COMMAND_READ_STATUS BIT(3)
  67. #define COMMAND_READ_ID BIT(2)
  68. /* NFC ECC mode define */
  69. #define ECC_BYPASS 0
  70. #define ECC_45_BYTE 6
  71. #define ECC_60_BYTE 7
  72. /*** Register Mask and bit definitions */
  73. /* NFC_FLASH_CMD1 Field */
  74. #define CMD_BYTE2_MASK 0xFF000000
  75. #define CMD_BYTE2_SHIFT 24
  76. /* NFC_FLASH_CM2 Field */
  77. #define CMD_BYTE1_MASK 0xFF000000
  78. #define CMD_BYTE1_SHIFT 24
  79. #define CMD_CODE_MASK 0x00FFFF00
  80. #define CMD_CODE_SHIFT 8
  81. #define BUFNO_MASK 0x00000006
  82. #define BUFNO_SHIFT 1
  83. #define START_BIT BIT(0)
  84. /* NFC_COL_ADDR Field */
  85. #define COL_ADDR_MASK 0x0000FFFF
  86. #define COL_ADDR_SHIFT 0
  87. #define COL_ADDR(pos, val) (((val) & 0xFF) << (8 * (pos)))
  88. /* NFC_ROW_ADDR Field */
  89. #define ROW_ADDR_MASK 0x00FFFFFF
  90. #define ROW_ADDR_SHIFT 0
  91. #define ROW_ADDR(pos, val) (((val) & 0xFF) << (8 * (pos)))
  92. #define ROW_ADDR_CHIP_SEL_RB_MASK 0xF0000000
  93. #define ROW_ADDR_CHIP_SEL_RB_SHIFT 28
  94. #define ROW_ADDR_CHIP_SEL_MASK 0x0F000000
  95. #define ROW_ADDR_CHIP_SEL_SHIFT 24
  96. /* NFC_FLASH_STATUS2 Field */
  97. #define STATUS_BYTE1_MASK 0x000000FF
  98. /* NFC_FLASH_CONFIG Field */
  99. #define CONFIG_ECC_SRAM_ADDR_MASK 0x7FC00000
  100. #define CONFIG_ECC_SRAM_ADDR_SHIFT 22
  101. #define CONFIG_ECC_SRAM_REQ_BIT BIT(21)
  102. #define CONFIG_DMA_REQ_BIT BIT(20)
  103. #define CONFIG_ECC_MODE_MASK 0x000E0000
  104. #define CONFIG_ECC_MODE_SHIFT 17
  105. #define CONFIG_FAST_FLASH_BIT BIT(16)
  106. #define CONFIG_16BIT BIT(7)
  107. #define CONFIG_BOOT_MODE_BIT BIT(6)
  108. #define CONFIG_ADDR_AUTO_INCR_BIT BIT(5)
  109. #define CONFIG_BUFNO_AUTO_INCR_BIT BIT(4)
  110. #define CONFIG_PAGE_CNT_MASK 0xF
  111. #define CONFIG_PAGE_CNT_SHIFT 0
  112. /* NFC_IRQ_STATUS Field */
  113. #define IDLE_IRQ_BIT BIT(29)
  114. #define IDLE_EN_BIT BIT(20)
  115. #define CMD_DONE_CLEAR_BIT BIT(18)
  116. #define IDLE_CLEAR_BIT BIT(17)
  117. /*
  118. * ECC status - seems to consume 8 bytes (double word). The documented
  119. * status byte is located in the lowest byte of the second word (which is
  120. * the 4th or 7th byte depending on endianness).
  121. * Calculate an offset to store the ECC status at the end of the buffer.
  122. */
  123. #define ECC_SRAM_ADDR (PAGE_2K + OOB_MAX - 8)
  124. #define ECC_STATUS 0x4
  125. #define ECC_STATUS_MASK 0x80
  126. #define ECC_STATUS_ERR_COUNT 0x3F
  127. enum vf610_nfc_variant {
  128. NFC_VFC610 = 1,
  129. };
  130. struct vf610_nfc {
  131. struct nand_controller base;
  132. struct nand_chip chip;
  133. struct device *dev;
  134. void __iomem *regs;
  135. struct completion cmd_done;
  136. /* Status and ID are in alternate locations. */
  137. enum vf610_nfc_variant variant;
  138. struct clk *clk;
  139. /*
  140. * Indicate that user data is accessed (full page/oob). This is
  141. * useful to indicate the driver whether to swap byte endianness.
  142. * See comments in vf610_nfc_rd_from_sram/vf610_nfc_wr_to_sram.
  143. */
  144. bool data_access;
  145. u32 ecc_mode;
  146. };
  147. static inline struct vf610_nfc *chip_to_nfc(struct nand_chip *chip)
  148. {
  149. return container_of(chip, struct vf610_nfc, chip);
  150. }
  151. static inline u32 vf610_nfc_read(struct vf610_nfc *nfc, uint reg)
  152. {
  153. return readl(nfc->regs + reg);
  154. }
  155. static inline void vf610_nfc_write(struct vf610_nfc *nfc, uint reg, u32 val)
  156. {
  157. writel(val, nfc->regs + reg);
  158. }
  159. static inline void vf610_nfc_set(struct vf610_nfc *nfc, uint reg, u32 bits)
  160. {
  161. vf610_nfc_write(nfc, reg, vf610_nfc_read(nfc, reg) | bits);
  162. }
  163. static inline void vf610_nfc_clear(struct vf610_nfc *nfc, uint reg, u32 bits)
  164. {
  165. vf610_nfc_write(nfc, reg, vf610_nfc_read(nfc, reg) & ~bits);
  166. }
  167. static inline void vf610_nfc_set_field(struct vf610_nfc *nfc, u32 reg,
  168. u32 mask, u32 shift, u32 val)
  169. {
  170. vf610_nfc_write(nfc, reg,
  171. (vf610_nfc_read(nfc, reg) & (~mask)) | val << shift);
  172. }
  173. static inline bool vf610_nfc_kernel_is_little_endian(void)
  174. {
  175. #ifdef __LITTLE_ENDIAN
  176. return true;
  177. #else
  178. return false;
  179. #endif
  180. }
  181. /**
  182. * Read accessor for internal SRAM buffer
  183. * @dst: destination address in regular memory
  184. * @src: source address in SRAM buffer
  185. * @len: bytes to copy
  186. * @fix_endian: Fix endianness if required
  187. *
  188. * Use this accessor for the internal SRAM buffers. On the ARM
  189. * Freescale Vybrid SoC it's known that the driver can treat
  190. * the SRAM buffer as if it's memory. Other platform might need
  191. * to treat the buffers differently.
  192. *
  193. * The controller stores bytes from the NAND chip internally in big
  194. * endianness. On little endian platforms such as Vybrid this leads
  195. * to reversed byte order.
  196. * For performance reason (and earlier probably due to unawareness)
  197. * the driver avoids correcting endianness where it has control over
  198. * write and read side (e.g. page wise data access).
  199. */
  200. static inline void vf610_nfc_rd_from_sram(void *dst, const void __iomem *src,
  201. size_t len, bool fix_endian)
  202. {
  203. if (vf610_nfc_kernel_is_little_endian() && fix_endian) {
  204. unsigned int i;
  205. for (i = 0; i < len; i += 4) {
  206. u32 val = swab32(__raw_readl(src + i));
  207. memcpy(dst + i, &val, min(sizeof(val), len - i));
  208. }
  209. } else {
  210. memcpy_fromio(dst, src, len);
  211. }
  212. }
  213. /**
  214. * Write accessor for internal SRAM buffer
  215. * @dst: destination address in SRAM buffer
  216. * @src: source address in regular memory
  217. * @len: bytes to copy
  218. * @fix_endian: Fix endianness if required
  219. *
  220. * Use this accessor for the internal SRAM buffers. On the ARM
  221. * Freescale Vybrid SoC it's known that the driver can treat
  222. * the SRAM buffer as if it's memory. Other platform might need
  223. * to treat the buffers differently.
  224. *
  225. * The controller stores bytes from the NAND chip internally in big
  226. * endianness. On little endian platforms such as Vybrid this leads
  227. * to reversed byte order.
  228. * For performance reason (and earlier probably due to unawareness)
  229. * the driver avoids correcting endianness where it has control over
  230. * write and read side (e.g. page wise data access).
  231. */
  232. static inline void vf610_nfc_wr_to_sram(void __iomem *dst, const void *src,
  233. size_t len, bool fix_endian)
  234. {
  235. if (vf610_nfc_kernel_is_little_endian() && fix_endian) {
  236. unsigned int i;
  237. for (i = 0; i < len; i += 4) {
  238. u32 val;
  239. memcpy(&val, src + i, min(sizeof(val), len - i));
  240. __raw_writel(swab32(val), dst + i);
  241. }
  242. } else {
  243. memcpy_toio(dst, src, len);
  244. }
  245. }
  246. /* Clear flags for upcoming command */
  247. static inline void vf610_nfc_clear_status(struct vf610_nfc *nfc)
  248. {
  249. u32 tmp = vf610_nfc_read(nfc, NFC_IRQ_STATUS);
  250. tmp |= CMD_DONE_CLEAR_BIT | IDLE_CLEAR_BIT;
  251. vf610_nfc_write(nfc, NFC_IRQ_STATUS, tmp);
  252. }
  253. static void vf610_nfc_done(struct vf610_nfc *nfc)
  254. {
  255. unsigned long timeout = msecs_to_jiffies(100);
  256. /*
  257. * Barrier is needed after this write. This write need
  258. * to be done before reading the next register the first
  259. * time.
  260. * vf610_nfc_set implicates such a barrier by using writel
  261. * to write to the register.
  262. */
  263. vf610_nfc_set(nfc, NFC_IRQ_STATUS, IDLE_EN_BIT);
  264. vf610_nfc_set(nfc, NFC_FLASH_CMD2, START_BIT);
  265. if (!wait_for_completion_timeout(&nfc->cmd_done, timeout))
  266. dev_warn(nfc->dev, "Timeout while waiting for BUSY.\n");
  267. vf610_nfc_clear_status(nfc);
  268. }
  269. static irqreturn_t vf610_nfc_irq(int irq, void *data)
  270. {
  271. struct vf610_nfc *nfc = data;
  272. vf610_nfc_clear(nfc, NFC_IRQ_STATUS, IDLE_EN_BIT);
  273. complete(&nfc->cmd_done);
  274. return IRQ_HANDLED;
  275. }
  276. static inline void vf610_nfc_ecc_mode(struct vf610_nfc *nfc, int ecc_mode)
  277. {
  278. vf610_nfc_set_field(nfc, NFC_FLASH_CONFIG,
  279. CONFIG_ECC_MODE_MASK,
  280. CONFIG_ECC_MODE_SHIFT, ecc_mode);
  281. }
  282. static inline void vf610_nfc_run(struct vf610_nfc *nfc, u32 col, u32 row,
  283. u32 cmd1, u32 cmd2, u32 trfr_sz)
  284. {
  285. vf610_nfc_set_field(nfc, NFC_COL_ADDR, COL_ADDR_MASK,
  286. COL_ADDR_SHIFT, col);
  287. vf610_nfc_set_field(nfc, NFC_ROW_ADDR, ROW_ADDR_MASK,
  288. ROW_ADDR_SHIFT, row);
  289. vf610_nfc_write(nfc, NFC_SECTOR_SIZE, trfr_sz);
  290. vf610_nfc_write(nfc, NFC_FLASH_CMD1, cmd1);
  291. vf610_nfc_write(nfc, NFC_FLASH_CMD2, cmd2);
  292. dev_dbg(nfc->dev,
  293. "col 0x%04x, row 0x%08x, cmd1 0x%08x, cmd2 0x%08x, len %d\n",
  294. col, row, cmd1, cmd2, trfr_sz);
  295. vf610_nfc_done(nfc);
  296. }
  297. static inline const struct nand_op_instr *
  298. vf610_get_next_instr(const struct nand_subop *subop, int *op_id)
  299. {
  300. if (*op_id + 1 >= subop->ninstrs)
  301. return NULL;
  302. (*op_id)++;
  303. return &subop->instrs[*op_id];
  304. }
  305. static int vf610_nfc_cmd(struct nand_chip *chip,
  306. const struct nand_subop *subop)
  307. {
  308. const struct nand_op_instr *instr;
  309. struct vf610_nfc *nfc = chip_to_nfc(chip);
  310. int op_id = -1, trfr_sz = 0, offset = 0;
  311. u32 col = 0, row = 0, cmd1 = 0, cmd2 = 0, code = 0;
  312. bool force8bit = false;
  313. /*
  314. * Some ops are optional, but the hardware requires the operations
  315. * to be in this exact order.
  316. * The op parser enforces the order and makes sure that there isn't
  317. * a read and write element in a single operation.
  318. */
  319. instr = vf610_get_next_instr(subop, &op_id);
  320. if (!instr)
  321. return -EINVAL;
  322. if (instr && instr->type == NAND_OP_CMD_INSTR) {
  323. cmd2 |= instr->ctx.cmd.opcode << CMD_BYTE1_SHIFT;
  324. code |= COMMAND_CMD_BYTE1;
  325. instr = vf610_get_next_instr(subop, &op_id);
  326. }
  327. if (instr && instr->type == NAND_OP_ADDR_INSTR) {
  328. int naddrs = nand_subop_get_num_addr_cyc(subop, op_id);
  329. int i = nand_subop_get_addr_start_off(subop, op_id);
  330. for (; i < naddrs; i++) {
  331. u8 val = instr->ctx.addr.addrs[i];
  332. if (i < 2)
  333. col |= COL_ADDR(i, val);
  334. else
  335. row |= ROW_ADDR(i - 2, val);
  336. }
  337. code |= COMMAND_NADDR_BYTES(naddrs);
  338. instr = vf610_get_next_instr(subop, &op_id);
  339. }
  340. if (instr && instr->type == NAND_OP_DATA_OUT_INSTR) {
  341. trfr_sz = nand_subop_get_data_len(subop, op_id);
  342. offset = nand_subop_get_data_start_off(subop, op_id);
  343. force8bit = instr->ctx.data.force_8bit;
  344. /*
  345. * Don't fix endianness on page access for historical reasons.
  346. * See comment in vf610_nfc_wr_to_sram
  347. */
  348. vf610_nfc_wr_to_sram(nfc->regs + NFC_MAIN_AREA(0) + offset,
  349. instr->ctx.data.buf.out + offset,
  350. trfr_sz, !nfc->data_access);
  351. code |= COMMAND_WRITE_DATA;
  352. instr = vf610_get_next_instr(subop, &op_id);
  353. }
  354. if (instr && instr->type == NAND_OP_CMD_INSTR) {
  355. cmd1 |= instr->ctx.cmd.opcode << CMD_BYTE2_SHIFT;
  356. code |= COMMAND_CMD_BYTE2;
  357. instr = vf610_get_next_instr(subop, &op_id);
  358. }
  359. if (instr && instr->type == NAND_OP_WAITRDY_INSTR) {
  360. code |= COMMAND_RB_HANDSHAKE;
  361. instr = vf610_get_next_instr(subop, &op_id);
  362. }
  363. if (instr && instr->type == NAND_OP_DATA_IN_INSTR) {
  364. trfr_sz = nand_subop_get_data_len(subop, op_id);
  365. offset = nand_subop_get_data_start_off(subop, op_id);
  366. force8bit = instr->ctx.data.force_8bit;
  367. code |= COMMAND_READ_DATA;
  368. }
  369. if (force8bit && (chip->options & NAND_BUSWIDTH_16))
  370. vf610_nfc_clear(nfc, NFC_FLASH_CONFIG, CONFIG_16BIT);
  371. cmd2 |= code << CMD_CODE_SHIFT;
  372. vf610_nfc_run(nfc, col, row, cmd1, cmd2, trfr_sz);
  373. if (instr && instr->type == NAND_OP_DATA_IN_INSTR) {
  374. /*
  375. * Don't fix endianness on page access for historical reasons.
  376. * See comment in vf610_nfc_rd_from_sram
  377. */
  378. vf610_nfc_rd_from_sram(instr->ctx.data.buf.in + offset,
  379. nfc->regs + NFC_MAIN_AREA(0) + offset,
  380. trfr_sz, !nfc->data_access);
  381. }
  382. if (force8bit && (chip->options & NAND_BUSWIDTH_16))
  383. vf610_nfc_set(nfc, NFC_FLASH_CONFIG, CONFIG_16BIT);
  384. return 0;
  385. }
  386. static const struct nand_op_parser vf610_nfc_op_parser = NAND_OP_PARSER(
  387. NAND_OP_PARSER_PATTERN(vf610_nfc_cmd,
  388. NAND_OP_PARSER_PAT_CMD_ELEM(true),
  389. NAND_OP_PARSER_PAT_ADDR_ELEM(true, 5),
  390. NAND_OP_PARSER_PAT_DATA_OUT_ELEM(true, PAGE_2K + OOB_MAX),
  391. NAND_OP_PARSER_PAT_CMD_ELEM(true),
  392. NAND_OP_PARSER_PAT_WAITRDY_ELEM(true)),
  393. NAND_OP_PARSER_PATTERN(vf610_nfc_cmd,
  394. NAND_OP_PARSER_PAT_CMD_ELEM(true),
  395. NAND_OP_PARSER_PAT_ADDR_ELEM(true, 5),
  396. NAND_OP_PARSER_PAT_CMD_ELEM(true),
  397. NAND_OP_PARSER_PAT_WAITRDY_ELEM(true),
  398. NAND_OP_PARSER_PAT_DATA_IN_ELEM(true, PAGE_2K + OOB_MAX)),
  399. );
  400. /*
  401. * This function supports Vybrid only (MPC5125 would have full RB and four CS)
  402. */
  403. static void vf610_nfc_select_target(struct nand_chip *chip, unsigned int cs)
  404. {
  405. struct vf610_nfc *nfc = chip_to_nfc(chip);
  406. u32 tmp;
  407. /* Vybrid only (MPC5125 would have full RB and four CS) */
  408. if (nfc->variant != NFC_VFC610)
  409. return;
  410. tmp = vf610_nfc_read(nfc, NFC_ROW_ADDR);
  411. tmp &= ~(ROW_ADDR_CHIP_SEL_RB_MASK | ROW_ADDR_CHIP_SEL_MASK);
  412. tmp |= 1 << ROW_ADDR_CHIP_SEL_RB_SHIFT;
  413. tmp |= BIT(cs) << ROW_ADDR_CHIP_SEL_SHIFT;
  414. vf610_nfc_write(nfc, NFC_ROW_ADDR, tmp);
  415. }
  416. static int vf610_nfc_exec_op(struct nand_chip *chip,
  417. const struct nand_operation *op,
  418. bool check_only)
  419. {
  420. if (!check_only)
  421. vf610_nfc_select_target(chip, op->cs);
  422. return nand_op_parser_exec_op(chip, &vf610_nfc_op_parser, op,
  423. check_only);
  424. }
  425. static inline int vf610_nfc_correct_data(struct nand_chip *chip, uint8_t *dat,
  426. uint8_t *oob, int page)
  427. {
  428. struct vf610_nfc *nfc = chip_to_nfc(chip);
  429. struct mtd_info *mtd = nand_to_mtd(chip);
  430. u32 ecc_status_off = NFC_MAIN_AREA(0) + ECC_SRAM_ADDR + ECC_STATUS;
  431. u8 ecc_status;
  432. u8 ecc_count;
  433. int flips_threshold = nfc->chip.ecc.strength / 2;
  434. ecc_status = vf610_nfc_read(nfc, ecc_status_off) & 0xff;
  435. ecc_count = ecc_status & ECC_STATUS_ERR_COUNT;
  436. if (!(ecc_status & ECC_STATUS_MASK))
  437. return ecc_count;
  438. nfc->data_access = true;
  439. nand_read_oob_op(&nfc->chip, page, 0, oob, mtd->oobsize);
  440. nfc->data_access = false;
  441. /*
  442. * On an erased page, bit count (including OOB) should be zero or
  443. * at least less then half of the ECC strength.
  444. */
  445. return nand_check_erased_ecc_chunk(dat, nfc->chip.ecc.size, oob,
  446. mtd->oobsize, NULL, 0,
  447. flips_threshold);
  448. }
  449. static void vf610_nfc_fill_row(struct nand_chip *chip, int page, u32 *code,
  450. u32 *row)
  451. {
  452. *row = ROW_ADDR(0, page & 0xff) | ROW_ADDR(1, page >> 8);
  453. *code |= COMMAND_RAR_BYTE1 | COMMAND_RAR_BYTE2;
  454. if (chip->options & NAND_ROW_ADDR_3) {
  455. *row |= ROW_ADDR(2, page >> 16);
  456. *code |= COMMAND_RAR_BYTE3;
  457. }
  458. }
  459. static int vf610_nfc_read_page(struct nand_chip *chip, uint8_t *buf,
  460. int oob_required, int page)
  461. {
  462. struct vf610_nfc *nfc = chip_to_nfc(chip);
  463. struct mtd_info *mtd = nand_to_mtd(chip);
  464. int trfr_sz = mtd->writesize + mtd->oobsize;
  465. u32 row = 0, cmd1 = 0, cmd2 = 0, code = 0;
  466. int stat;
  467. vf610_nfc_select_target(chip, chip->cur_cs);
  468. cmd2 |= NAND_CMD_READ0 << CMD_BYTE1_SHIFT;
  469. code |= COMMAND_CMD_BYTE1 | COMMAND_CAR_BYTE1 | COMMAND_CAR_BYTE2;
  470. vf610_nfc_fill_row(chip, page, &code, &row);
  471. cmd1 |= NAND_CMD_READSTART << CMD_BYTE2_SHIFT;
  472. code |= COMMAND_CMD_BYTE2 | COMMAND_RB_HANDSHAKE | COMMAND_READ_DATA;
  473. cmd2 |= code << CMD_CODE_SHIFT;
  474. vf610_nfc_ecc_mode(nfc, nfc->ecc_mode);
  475. vf610_nfc_run(nfc, 0, row, cmd1, cmd2, trfr_sz);
  476. vf610_nfc_ecc_mode(nfc, ECC_BYPASS);
  477. /*
  478. * Don't fix endianness on page access for historical reasons.
  479. * See comment in vf610_nfc_rd_from_sram
  480. */
  481. vf610_nfc_rd_from_sram(buf, nfc->regs + NFC_MAIN_AREA(0),
  482. mtd->writesize, false);
  483. if (oob_required)
  484. vf610_nfc_rd_from_sram(chip->oob_poi,
  485. nfc->regs + NFC_MAIN_AREA(0) +
  486. mtd->writesize,
  487. mtd->oobsize, false);
  488. stat = vf610_nfc_correct_data(chip, buf, chip->oob_poi, page);
  489. if (stat < 0) {
  490. mtd->ecc_stats.failed++;
  491. return 0;
  492. } else {
  493. mtd->ecc_stats.corrected += stat;
  494. return stat;
  495. }
  496. }
  497. static int vf610_nfc_write_page(struct nand_chip *chip, const uint8_t *buf,
  498. int oob_required, int page)
  499. {
  500. struct vf610_nfc *nfc = chip_to_nfc(chip);
  501. struct mtd_info *mtd = nand_to_mtd(chip);
  502. int trfr_sz = mtd->writesize + mtd->oobsize;
  503. u32 row = 0, cmd1 = 0, cmd2 = 0, code = 0;
  504. u8 status;
  505. int ret;
  506. vf610_nfc_select_target(chip, chip->cur_cs);
  507. cmd2 |= NAND_CMD_SEQIN << CMD_BYTE1_SHIFT;
  508. code |= COMMAND_CMD_BYTE1 | COMMAND_CAR_BYTE1 | COMMAND_CAR_BYTE2;
  509. vf610_nfc_fill_row(chip, page, &code, &row);
  510. cmd1 |= NAND_CMD_PAGEPROG << CMD_BYTE2_SHIFT;
  511. code |= COMMAND_CMD_BYTE2 | COMMAND_WRITE_DATA;
  512. /*
  513. * Don't fix endianness on page access for historical reasons.
  514. * See comment in vf610_nfc_wr_to_sram
  515. */
  516. vf610_nfc_wr_to_sram(nfc->regs + NFC_MAIN_AREA(0), buf,
  517. mtd->writesize, false);
  518. code |= COMMAND_RB_HANDSHAKE;
  519. cmd2 |= code << CMD_CODE_SHIFT;
  520. vf610_nfc_ecc_mode(nfc, nfc->ecc_mode);
  521. vf610_nfc_run(nfc, 0, row, cmd1, cmd2, trfr_sz);
  522. vf610_nfc_ecc_mode(nfc, ECC_BYPASS);
  523. ret = nand_status_op(chip, &status);
  524. if (ret)
  525. return ret;
  526. if (status & NAND_STATUS_FAIL)
  527. return -EIO;
  528. return 0;
  529. }
  530. static int vf610_nfc_read_page_raw(struct nand_chip *chip, u8 *buf,
  531. int oob_required, int page)
  532. {
  533. struct vf610_nfc *nfc = chip_to_nfc(chip);
  534. int ret;
  535. nfc->data_access = true;
  536. ret = nand_read_page_raw(chip, buf, oob_required, page);
  537. nfc->data_access = false;
  538. return ret;
  539. }
  540. static int vf610_nfc_write_page_raw(struct nand_chip *chip, const u8 *buf,
  541. int oob_required, int page)
  542. {
  543. struct vf610_nfc *nfc = chip_to_nfc(chip);
  544. struct mtd_info *mtd = nand_to_mtd(chip);
  545. int ret;
  546. nfc->data_access = true;
  547. ret = nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize);
  548. if (!ret && oob_required)
  549. ret = nand_write_data_op(chip, chip->oob_poi, mtd->oobsize,
  550. false);
  551. nfc->data_access = false;
  552. if (ret)
  553. return ret;
  554. return nand_prog_page_end_op(chip);
  555. }
  556. static int vf610_nfc_read_oob(struct nand_chip *chip, int page)
  557. {
  558. struct vf610_nfc *nfc = chip_to_nfc(chip);
  559. int ret;
  560. nfc->data_access = true;
  561. ret = nand_read_oob_std(chip, page);
  562. nfc->data_access = false;
  563. return ret;
  564. }
  565. static int vf610_nfc_write_oob(struct nand_chip *chip, int page)
  566. {
  567. struct mtd_info *mtd = nand_to_mtd(chip);
  568. struct vf610_nfc *nfc = chip_to_nfc(chip);
  569. int ret;
  570. nfc->data_access = true;
  571. ret = nand_prog_page_begin_op(chip, page, mtd->writesize,
  572. chip->oob_poi, mtd->oobsize);
  573. nfc->data_access = false;
  574. if (ret)
  575. return ret;
  576. return nand_prog_page_end_op(chip);
  577. }
  578. static const struct of_device_id vf610_nfc_dt_ids[] = {
  579. { .compatible = "fsl,vf610-nfc", .data = (void *)NFC_VFC610 },
  580. { /* sentinel */ }
  581. };
  582. MODULE_DEVICE_TABLE(of, vf610_nfc_dt_ids);
  583. static void vf610_nfc_preinit_controller(struct vf610_nfc *nfc)
  584. {
  585. vf610_nfc_clear(nfc, NFC_FLASH_CONFIG, CONFIG_16BIT);
  586. vf610_nfc_clear(nfc, NFC_FLASH_CONFIG, CONFIG_ADDR_AUTO_INCR_BIT);
  587. vf610_nfc_clear(nfc, NFC_FLASH_CONFIG, CONFIG_BUFNO_AUTO_INCR_BIT);
  588. vf610_nfc_clear(nfc, NFC_FLASH_CONFIG, CONFIG_BOOT_MODE_BIT);
  589. vf610_nfc_clear(nfc, NFC_FLASH_CONFIG, CONFIG_DMA_REQ_BIT);
  590. vf610_nfc_set(nfc, NFC_FLASH_CONFIG, CONFIG_FAST_FLASH_BIT);
  591. vf610_nfc_ecc_mode(nfc, ECC_BYPASS);
  592. /* Disable virtual pages, only one elementary transfer unit */
  593. vf610_nfc_set_field(nfc, NFC_FLASH_CONFIG, CONFIG_PAGE_CNT_MASK,
  594. CONFIG_PAGE_CNT_SHIFT, 1);
  595. }
  596. static void vf610_nfc_init_controller(struct vf610_nfc *nfc)
  597. {
  598. if (nfc->chip.options & NAND_BUSWIDTH_16)
  599. vf610_nfc_set(nfc, NFC_FLASH_CONFIG, CONFIG_16BIT);
  600. else
  601. vf610_nfc_clear(nfc, NFC_FLASH_CONFIG, CONFIG_16BIT);
  602. if (nfc->chip.ecc.engine_type == NAND_ECC_ENGINE_TYPE_ON_HOST) {
  603. /* Set ECC status offset in SRAM */
  604. vf610_nfc_set_field(nfc, NFC_FLASH_CONFIG,
  605. CONFIG_ECC_SRAM_ADDR_MASK,
  606. CONFIG_ECC_SRAM_ADDR_SHIFT,
  607. ECC_SRAM_ADDR >> 3);
  608. /* Enable ECC status in SRAM */
  609. vf610_nfc_set(nfc, NFC_FLASH_CONFIG, CONFIG_ECC_SRAM_REQ_BIT);
  610. }
  611. }
  612. static int vf610_nfc_attach_chip(struct nand_chip *chip)
  613. {
  614. struct mtd_info *mtd = nand_to_mtd(chip);
  615. struct vf610_nfc *nfc = chip_to_nfc(chip);
  616. vf610_nfc_init_controller(nfc);
  617. /* Bad block options. */
  618. if (chip->bbt_options & NAND_BBT_USE_FLASH)
  619. chip->bbt_options |= NAND_BBT_NO_OOB;
  620. /* Single buffer only, max 256 OOB minus ECC status */
  621. if (mtd->writesize + mtd->oobsize > PAGE_2K + OOB_MAX - 8) {
  622. dev_err(nfc->dev, "Unsupported flash page size\n");
  623. return -ENXIO;
  624. }
  625. if (chip->ecc.engine_type != NAND_ECC_ENGINE_TYPE_ON_HOST)
  626. return 0;
  627. if (mtd->writesize != PAGE_2K && mtd->oobsize < 64) {
  628. dev_err(nfc->dev, "Unsupported flash with hwecc\n");
  629. return -ENXIO;
  630. }
  631. if (chip->ecc.size != mtd->writesize) {
  632. dev_err(nfc->dev, "Step size needs to be page size\n");
  633. return -ENXIO;
  634. }
  635. /* Only 64 byte ECC layouts known */
  636. if (mtd->oobsize > 64)
  637. mtd->oobsize = 64;
  638. /* Use default large page ECC layout defined in NAND core */
  639. mtd_set_ooblayout(mtd, nand_get_large_page_ooblayout());
  640. if (chip->ecc.strength == 32) {
  641. nfc->ecc_mode = ECC_60_BYTE;
  642. chip->ecc.bytes = 60;
  643. } else if (chip->ecc.strength == 24) {
  644. nfc->ecc_mode = ECC_45_BYTE;
  645. chip->ecc.bytes = 45;
  646. } else {
  647. dev_err(nfc->dev, "Unsupported ECC strength\n");
  648. return -ENXIO;
  649. }
  650. chip->ecc.read_page = vf610_nfc_read_page;
  651. chip->ecc.write_page = vf610_nfc_write_page;
  652. chip->ecc.read_page_raw = vf610_nfc_read_page_raw;
  653. chip->ecc.write_page_raw = vf610_nfc_write_page_raw;
  654. chip->ecc.read_oob = vf610_nfc_read_oob;
  655. chip->ecc.write_oob = vf610_nfc_write_oob;
  656. chip->ecc.size = PAGE_2K;
  657. return 0;
  658. }
  659. static const struct nand_controller_ops vf610_nfc_controller_ops = {
  660. .attach_chip = vf610_nfc_attach_chip,
  661. .exec_op = vf610_nfc_exec_op,
  662. };
  663. static int vf610_nfc_probe(struct platform_device *pdev)
  664. {
  665. struct vf610_nfc *nfc;
  666. struct mtd_info *mtd;
  667. struct nand_chip *chip;
  668. struct device_node *child;
  669. const struct of_device_id *of_id;
  670. int err;
  671. int irq;
  672. nfc = devm_kzalloc(&pdev->dev, sizeof(*nfc), GFP_KERNEL);
  673. if (!nfc)
  674. return -ENOMEM;
  675. nfc->dev = &pdev->dev;
  676. chip = &nfc->chip;
  677. mtd = nand_to_mtd(chip);
  678. mtd->owner = THIS_MODULE;
  679. mtd->dev.parent = nfc->dev;
  680. mtd->name = DRV_NAME;
  681. irq = platform_get_irq(pdev, 0);
  682. if (irq <= 0)
  683. return -EINVAL;
  684. nfc->regs = devm_platform_ioremap_resource(pdev, 0);
  685. if (IS_ERR(nfc->regs))
  686. return PTR_ERR(nfc->regs);
  687. nfc->clk = devm_clk_get(&pdev->dev, NULL);
  688. if (IS_ERR(nfc->clk))
  689. return PTR_ERR(nfc->clk);
  690. err = clk_prepare_enable(nfc->clk);
  691. if (err) {
  692. dev_err(nfc->dev, "Unable to enable clock!\n");
  693. return err;
  694. }
  695. of_id = of_match_device(vf610_nfc_dt_ids, &pdev->dev);
  696. if (!of_id) {
  697. err = -ENODEV;
  698. goto err_disable_clk;
  699. }
  700. nfc->variant = (enum vf610_nfc_variant)of_id->data;
  701. for_each_available_child_of_node(nfc->dev->of_node, child) {
  702. if (of_device_is_compatible(child, "fsl,vf610-nfc-nandcs")) {
  703. if (nand_get_flash_node(chip)) {
  704. dev_err(nfc->dev,
  705. "Only one NAND chip supported!\n");
  706. err = -EINVAL;
  707. of_node_put(child);
  708. goto err_disable_clk;
  709. }
  710. nand_set_flash_node(chip, child);
  711. }
  712. }
  713. if (!nand_get_flash_node(chip)) {
  714. dev_err(nfc->dev, "NAND chip sub-node missing!\n");
  715. err = -ENODEV;
  716. goto err_disable_clk;
  717. }
  718. chip->options |= NAND_NO_SUBPAGE_WRITE;
  719. init_completion(&nfc->cmd_done);
  720. err = devm_request_irq(nfc->dev, irq, vf610_nfc_irq, 0, DRV_NAME, nfc);
  721. if (err) {
  722. dev_err(nfc->dev, "Error requesting IRQ!\n");
  723. goto err_disable_clk;
  724. }
  725. vf610_nfc_preinit_controller(nfc);
  726. nand_controller_init(&nfc->base);
  727. nfc->base.ops = &vf610_nfc_controller_ops;
  728. chip->controller = &nfc->base;
  729. /* Scan the NAND chip */
  730. err = nand_scan(chip, 1);
  731. if (err)
  732. goto err_disable_clk;
  733. platform_set_drvdata(pdev, nfc);
  734. /* Register device in MTD */
  735. err = mtd_device_register(mtd, NULL, 0);
  736. if (err)
  737. goto err_cleanup_nand;
  738. return 0;
  739. err_cleanup_nand:
  740. nand_cleanup(chip);
  741. err_disable_clk:
  742. clk_disable_unprepare(nfc->clk);
  743. return err;
  744. }
  745. static int vf610_nfc_remove(struct platform_device *pdev)
  746. {
  747. struct vf610_nfc *nfc = platform_get_drvdata(pdev);
  748. struct nand_chip *chip = &nfc->chip;
  749. int ret;
  750. ret = mtd_device_unregister(nand_to_mtd(chip));
  751. WARN_ON(ret);
  752. nand_cleanup(chip);
  753. clk_disable_unprepare(nfc->clk);
  754. return 0;
  755. }
  756. #ifdef CONFIG_PM_SLEEP
  757. static int vf610_nfc_suspend(struct device *dev)
  758. {
  759. struct vf610_nfc *nfc = dev_get_drvdata(dev);
  760. clk_disable_unprepare(nfc->clk);
  761. return 0;
  762. }
  763. static int vf610_nfc_resume(struct device *dev)
  764. {
  765. struct vf610_nfc *nfc = dev_get_drvdata(dev);
  766. int err;
  767. err = clk_prepare_enable(nfc->clk);
  768. if (err)
  769. return err;
  770. vf610_nfc_preinit_controller(nfc);
  771. vf610_nfc_init_controller(nfc);
  772. return 0;
  773. }
  774. #endif
  775. static SIMPLE_DEV_PM_OPS(vf610_nfc_pm_ops, vf610_nfc_suspend, vf610_nfc_resume);
  776. static struct platform_driver vf610_nfc_driver = {
  777. .driver = {
  778. .name = DRV_NAME,
  779. .of_match_table = vf610_nfc_dt_ids,
  780. .pm = &vf610_nfc_pm_ops,
  781. },
  782. .probe = vf610_nfc_probe,
  783. .remove = vf610_nfc_remove,
  784. };
  785. module_platform_driver(vf610_nfc_driver);
  786. MODULE_AUTHOR("Stefan Agner <[email protected]>");
  787. MODULE_DESCRIPTION("Freescale VF610/MPC5125 NFC MTD NAND driver");
  788. MODULE_LICENSE("GPL");