vf610_nfc.c 25 KB

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