vf610_nfc.c 26 KB

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