vf610_nfc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  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/nand.h>
  34. #include <linux/mtd/partitions.h>
  35. #include <linux/of_device.h>
  36. #include <linux/pinctrl/consumer.h>
  37. #include <linux/platform_device.h>
  38. #include <linux/slab.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. /*
  58. * NFC_CMD2[CODE] values. See section:
  59. * - 31.4.7 Flash Command Code Description, Vybrid manual
  60. * - 23.8.6 Flash Command Sequencer, MPC5125 manual
  61. *
  62. * Briefly these are bitmasks of controller cycles.
  63. */
  64. #define READ_PAGE_CMD_CODE 0x7EE0
  65. #define READ_ONFI_PARAM_CMD_CODE 0x4860
  66. #define PROGRAM_PAGE_CMD_CODE 0x7FC0
  67. #define ERASE_CMD_CODE 0x4EC0
  68. #define READ_ID_CMD_CODE 0x4804
  69. #define RESET_CMD_CODE 0x4040
  70. #define STATUS_READ_CMD_CODE 0x4068
  71. /* NFC ECC mode define */
  72. #define ECC_BYPASS 0
  73. #define ECC_45_BYTE 6
  74. #define ECC_60_BYTE 7
  75. /*** Register Mask and bit definitions */
  76. /* NFC_FLASH_CMD1 Field */
  77. #define CMD_BYTE2_MASK 0xFF000000
  78. #define CMD_BYTE2_SHIFT 24
  79. /* NFC_FLASH_CM2 Field */
  80. #define CMD_BYTE1_MASK 0xFF000000
  81. #define CMD_BYTE1_SHIFT 24
  82. #define CMD_CODE_MASK 0x00FFFF00
  83. #define CMD_CODE_SHIFT 8
  84. #define BUFNO_MASK 0x00000006
  85. #define BUFNO_SHIFT 1
  86. #define START_BIT BIT(0)
  87. /* NFC_COL_ADDR Field */
  88. #define COL_ADDR_MASK 0x0000FFFF
  89. #define COL_ADDR_SHIFT 0
  90. /* NFC_ROW_ADDR Field */
  91. #define ROW_ADDR_MASK 0x00FFFFFF
  92. #define ROW_ADDR_SHIFT 0
  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_alt_buf {
  129. ALT_BUF_DATA = 0,
  130. ALT_BUF_ID = 1,
  131. ALT_BUF_STAT = 2,
  132. ALT_BUF_ONFI = 3,
  133. };
  134. enum vf610_nfc_variant {
  135. NFC_VFC610 = 1,
  136. };
  137. struct vf610_nfc {
  138. struct nand_chip chip;
  139. struct device *dev;
  140. void __iomem *regs;
  141. struct completion cmd_done;
  142. uint buf_offset;
  143. int write_sz;
  144. /* Status and ID are in alternate locations. */
  145. enum vf610_nfc_alt_buf alt_buf;
  146. enum vf610_nfc_variant variant;
  147. struct clk *clk;
  148. bool use_hw_ecc;
  149. u32 ecc_mode;
  150. };
  151. static inline struct vf610_nfc *mtd_to_nfc(struct mtd_info *mtd)
  152. {
  153. return container_of(mtd_to_nand(mtd), struct vf610_nfc, chip);
  154. }
  155. static inline u32 vf610_nfc_read(struct vf610_nfc *nfc, uint reg)
  156. {
  157. return readl(nfc->regs + reg);
  158. }
  159. static inline void vf610_nfc_write(struct vf610_nfc *nfc, uint reg, u32 val)
  160. {
  161. writel(val, nfc->regs + reg);
  162. }
  163. static inline void vf610_nfc_set(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_clear(struct vf610_nfc *nfc, uint reg, u32 bits)
  168. {
  169. vf610_nfc_write(nfc, reg, vf610_nfc_read(nfc, reg) & ~bits);
  170. }
  171. static inline void vf610_nfc_set_field(struct vf610_nfc *nfc, u32 reg,
  172. u32 mask, u32 shift, u32 val)
  173. {
  174. vf610_nfc_write(nfc, reg,
  175. (vf610_nfc_read(nfc, reg) & (~mask)) | val << shift);
  176. }
  177. static inline void vf610_nfc_memcpy(void *dst, const void __iomem *src,
  178. size_t n)
  179. {
  180. /*
  181. * Use this accessor for the internal SRAM buffers. On the ARM
  182. * Freescale Vybrid SoC it's known that the driver can treat
  183. * the SRAM buffer as if it's memory. Other platform might need
  184. * to treat the buffers differently.
  185. *
  186. * For the time being, use memcpy
  187. */
  188. memcpy(dst, src, n);
  189. }
  190. /* Clear flags for upcoming command */
  191. static inline void vf610_nfc_clear_status(struct vf610_nfc *nfc)
  192. {
  193. u32 tmp = vf610_nfc_read(nfc, NFC_IRQ_STATUS);
  194. tmp |= CMD_DONE_CLEAR_BIT | IDLE_CLEAR_BIT;
  195. vf610_nfc_write(nfc, NFC_IRQ_STATUS, tmp);
  196. }
  197. static void vf610_nfc_done(struct vf610_nfc *nfc)
  198. {
  199. unsigned long timeout = msecs_to_jiffies(100);
  200. /*
  201. * Barrier is needed after this write. This write need
  202. * to be done before reading the next register the first
  203. * time.
  204. * vf610_nfc_set implicates such a barrier by using writel
  205. * to write to the register.
  206. */
  207. vf610_nfc_set(nfc, NFC_IRQ_STATUS, IDLE_EN_BIT);
  208. vf610_nfc_set(nfc, NFC_FLASH_CMD2, START_BIT);
  209. if (!wait_for_completion_timeout(&nfc->cmd_done, timeout))
  210. dev_warn(nfc->dev, "Timeout while waiting for BUSY.\n");
  211. vf610_nfc_clear_status(nfc);
  212. }
  213. static u8 vf610_nfc_get_id(struct vf610_nfc *nfc, int col)
  214. {
  215. u32 flash_id;
  216. if (col < 4) {
  217. flash_id = vf610_nfc_read(nfc, NFC_FLASH_STATUS1);
  218. flash_id >>= (3 - col) * 8;
  219. } else {
  220. flash_id = vf610_nfc_read(nfc, NFC_FLASH_STATUS2);
  221. flash_id >>= 24;
  222. }
  223. return flash_id & 0xff;
  224. }
  225. static u8 vf610_nfc_get_status(struct vf610_nfc *nfc)
  226. {
  227. return vf610_nfc_read(nfc, NFC_FLASH_STATUS2) & STATUS_BYTE1_MASK;
  228. }
  229. static void vf610_nfc_send_command(struct vf610_nfc *nfc, u32 cmd_byte1,
  230. u32 cmd_code)
  231. {
  232. u32 tmp;
  233. vf610_nfc_clear_status(nfc);
  234. tmp = vf610_nfc_read(nfc, NFC_FLASH_CMD2);
  235. tmp &= ~(CMD_BYTE1_MASK | CMD_CODE_MASK | BUFNO_MASK);
  236. tmp |= cmd_byte1 << CMD_BYTE1_SHIFT;
  237. tmp |= cmd_code << CMD_CODE_SHIFT;
  238. vf610_nfc_write(nfc, NFC_FLASH_CMD2, tmp);
  239. }
  240. static void vf610_nfc_send_commands(struct vf610_nfc *nfc, u32 cmd_byte1,
  241. u32 cmd_byte2, u32 cmd_code)
  242. {
  243. u32 tmp;
  244. vf610_nfc_send_command(nfc, cmd_byte1, cmd_code);
  245. tmp = vf610_nfc_read(nfc, NFC_FLASH_CMD1);
  246. tmp &= ~CMD_BYTE2_MASK;
  247. tmp |= cmd_byte2 << CMD_BYTE2_SHIFT;
  248. vf610_nfc_write(nfc, NFC_FLASH_CMD1, tmp);
  249. }
  250. static irqreturn_t vf610_nfc_irq(int irq, void *data)
  251. {
  252. struct mtd_info *mtd = data;
  253. struct vf610_nfc *nfc = mtd_to_nfc(mtd);
  254. vf610_nfc_clear(nfc, NFC_IRQ_STATUS, IDLE_EN_BIT);
  255. complete(&nfc->cmd_done);
  256. return IRQ_HANDLED;
  257. }
  258. static void vf610_nfc_addr_cycle(struct vf610_nfc *nfc, int column, int page)
  259. {
  260. if (column != -1) {
  261. if (nfc->chip.options & NAND_BUSWIDTH_16)
  262. column = column / 2;
  263. vf610_nfc_set_field(nfc, NFC_COL_ADDR, COL_ADDR_MASK,
  264. COL_ADDR_SHIFT, column);
  265. }
  266. if (page != -1)
  267. vf610_nfc_set_field(nfc, NFC_ROW_ADDR, ROW_ADDR_MASK,
  268. ROW_ADDR_SHIFT, page);
  269. }
  270. static inline void vf610_nfc_ecc_mode(struct vf610_nfc *nfc, int ecc_mode)
  271. {
  272. vf610_nfc_set_field(nfc, NFC_FLASH_CONFIG,
  273. CONFIG_ECC_MODE_MASK,
  274. CONFIG_ECC_MODE_SHIFT, ecc_mode);
  275. }
  276. static inline void vf610_nfc_transfer_size(struct vf610_nfc *nfc, int size)
  277. {
  278. vf610_nfc_write(nfc, NFC_SECTOR_SIZE, size);
  279. }
  280. static void vf610_nfc_command(struct mtd_info *mtd, unsigned command,
  281. int column, int page)
  282. {
  283. struct vf610_nfc *nfc = mtd_to_nfc(mtd);
  284. int trfr_sz = nfc->chip.options & NAND_BUSWIDTH_16 ? 1 : 0;
  285. nfc->buf_offset = max(column, 0);
  286. nfc->alt_buf = ALT_BUF_DATA;
  287. switch (command) {
  288. case NAND_CMD_SEQIN:
  289. /* Use valid column/page from preread... */
  290. vf610_nfc_addr_cycle(nfc, column, page);
  291. nfc->buf_offset = 0;
  292. /*
  293. * SEQIN => data => PAGEPROG sequence is done by the controller
  294. * hence we do not need to issue the command here...
  295. */
  296. return;
  297. case NAND_CMD_PAGEPROG:
  298. trfr_sz += nfc->write_sz;
  299. vf610_nfc_transfer_size(nfc, trfr_sz);
  300. vf610_nfc_send_commands(nfc, NAND_CMD_SEQIN,
  301. command, PROGRAM_PAGE_CMD_CODE);
  302. if (nfc->use_hw_ecc)
  303. vf610_nfc_ecc_mode(nfc, nfc->ecc_mode);
  304. else
  305. vf610_nfc_ecc_mode(nfc, ECC_BYPASS);
  306. break;
  307. case NAND_CMD_RESET:
  308. vf610_nfc_transfer_size(nfc, 0);
  309. vf610_nfc_send_command(nfc, command, RESET_CMD_CODE);
  310. break;
  311. case NAND_CMD_READOOB:
  312. trfr_sz += mtd->oobsize;
  313. column = mtd->writesize;
  314. vf610_nfc_transfer_size(nfc, trfr_sz);
  315. vf610_nfc_send_commands(nfc, NAND_CMD_READ0,
  316. NAND_CMD_READSTART, READ_PAGE_CMD_CODE);
  317. vf610_nfc_addr_cycle(nfc, column, page);
  318. vf610_nfc_ecc_mode(nfc, ECC_BYPASS);
  319. break;
  320. case NAND_CMD_READ0:
  321. trfr_sz += mtd->writesize + mtd->oobsize;
  322. vf610_nfc_transfer_size(nfc, trfr_sz);
  323. vf610_nfc_send_commands(nfc, NAND_CMD_READ0,
  324. NAND_CMD_READSTART, READ_PAGE_CMD_CODE);
  325. vf610_nfc_addr_cycle(nfc, column, page);
  326. vf610_nfc_ecc_mode(nfc, nfc->ecc_mode);
  327. break;
  328. case NAND_CMD_PARAM:
  329. nfc->alt_buf = ALT_BUF_ONFI;
  330. trfr_sz = 3 * sizeof(struct nand_onfi_params);
  331. vf610_nfc_transfer_size(nfc, trfr_sz);
  332. vf610_nfc_send_command(nfc, command, READ_ONFI_PARAM_CMD_CODE);
  333. vf610_nfc_addr_cycle(nfc, -1, column);
  334. vf610_nfc_ecc_mode(nfc, ECC_BYPASS);
  335. break;
  336. case NAND_CMD_ERASE1:
  337. vf610_nfc_transfer_size(nfc, 0);
  338. vf610_nfc_send_commands(nfc, command,
  339. NAND_CMD_ERASE2, ERASE_CMD_CODE);
  340. vf610_nfc_addr_cycle(nfc, column, page);
  341. break;
  342. case NAND_CMD_READID:
  343. nfc->alt_buf = ALT_BUF_ID;
  344. nfc->buf_offset = 0;
  345. vf610_nfc_transfer_size(nfc, 0);
  346. vf610_nfc_send_command(nfc, command, READ_ID_CMD_CODE);
  347. vf610_nfc_addr_cycle(nfc, -1, column);
  348. break;
  349. case NAND_CMD_STATUS:
  350. nfc->alt_buf = ALT_BUF_STAT;
  351. vf610_nfc_transfer_size(nfc, 0);
  352. vf610_nfc_send_command(nfc, command, STATUS_READ_CMD_CODE);
  353. break;
  354. default:
  355. return;
  356. }
  357. vf610_nfc_done(nfc);
  358. nfc->use_hw_ecc = false;
  359. nfc->write_sz = 0;
  360. }
  361. static void vf610_nfc_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  362. {
  363. struct vf610_nfc *nfc = mtd_to_nfc(mtd);
  364. uint c = nfc->buf_offset;
  365. /* Alternate buffers are only supported through read_byte */
  366. WARN_ON(nfc->alt_buf);
  367. vf610_nfc_memcpy(buf, nfc->regs + NFC_MAIN_AREA(0) + c, len);
  368. nfc->buf_offset += len;
  369. }
  370. static void vf610_nfc_write_buf(struct mtd_info *mtd, const uint8_t *buf,
  371. int len)
  372. {
  373. struct vf610_nfc *nfc = mtd_to_nfc(mtd);
  374. uint c = nfc->buf_offset;
  375. uint l;
  376. l = min_t(uint, len, mtd->writesize + mtd->oobsize - c);
  377. vf610_nfc_memcpy(nfc->regs + NFC_MAIN_AREA(0) + c, buf, l);
  378. nfc->write_sz += l;
  379. nfc->buf_offset += l;
  380. }
  381. static uint8_t vf610_nfc_read_byte(struct mtd_info *mtd)
  382. {
  383. struct vf610_nfc *nfc = mtd_to_nfc(mtd);
  384. u8 tmp;
  385. uint c = nfc->buf_offset;
  386. switch (nfc->alt_buf) {
  387. case ALT_BUF_ID:
  388. tmp = vf610_nfc_get_id(nfc, c);
  389. break;
  390. case ALT_BUF_STAT:
  391. tmp = vf610_nfc_get_status(nfc);
  392. break;
  393. #ifdef __LITTLE_ENDIAN
  394. case ALT_BUF_ONFI:
  395. /* Reverse byte since the controller uses big endianness */
  396. c = nfc->buf_offset ^ 0x3;
  397. /* fall-through */
  398. #endif
  399. default:
  400. tmp = *((u8 *)(nfc->regs + NFC_MAIN_AREA(0) + c));
  401. break;
  402. }
  403. nfc->buf_offset++;
  404. return tmp;
  405. }
  406. static u16 vf610_nfc_read_word(struct mtd_info *mtd)
  407. {
  408. u16 tmp;
  409. vf610_nfc_read_buf(mtd, (u_char *)&tmp, sizeof(tmp));
  410. return tmp;
  411. }
  412. /* If not provided, upper layers apply a fixed delay. */
  413. static int vf610_nfc_dev_ready(struct mtd_info *mtd)
  414. {
  415. /* NFC handles R/B internally; always ready. */
  416. return 1;
  417. }
  418. /*
  419. * This function supports Vybrid only (MPC5125 would have full RB and four CS)
  420. */
  421. static void vf610_nfc_select_chip(struct mtd_info *mtd, int chip)
  422. {
  423. struct vf610_nfc *nfc = mtd_to_nfc(mtd);
  424. u32 tmp = vf610_nfc_read(nfc, NFC_ROW_ADDR);
  425. /* Vybrid only (MPC5125 would have full RB and four CS) */
  426. if (nfc->variant != NFC_VFC610)
  427. return;
  428. tmp &= ~(ROW_ADDR_CHIP_SEL_RB_MASK | ROW_ADDR_CHIP_SEL_MASK);
  429. if (chip >= 0) {
  430. tmp |= 1 << ROW_ADDR_CHIP_SEL_RB_SHIFT;
  431. tmp |= BIT(chip) << ROW_ADDR_CHIP_SEL_SHIFT;
  432. }
  433. vf610_nfc_write(nfc, NFC_ROW_ADDR, tmp);
  434. }
  435. /* Count the number of 0's in buff up to max_bits */
  436. static inline int count_written_bits(uint8_t *buff, int size, int max_bits)
  437. {
  438. uint32_t *buff32 = (uint32_t *)buff;
  439. int k, written_bits = 0;
  440. for (k = 0; k < (size / 4); k++) {
  441. written_bits += hweight32(~buff32[k]);
  442. if (unlikely(written_bits > max_bits))
  443. break;
  444. }
  445. return written_bits;
  446. }
  447. static inline int vf610_nfc_correct_data(struct mtd_info *mtd, uint8_t *dat,
  448. uint8_t *oob, int page)
  449. {
  450. struct vf610_nfc *nfc = mtd_to_nfc(mtd);
  451. u32 ecc_status_off = NFC_MAIN_AREA(0) + ECC_SRAM_ADDR + ECC_STATUS;
  452. u8 ecc_status;
  453. u8 ecc_count;
  454. int flips_threshold = nfc->chip.ecc.strength / 2;
  455. ecc_status = vf610_nfc_read(nfc, ecc_status_off) & 0xff;
  456. ecc_count = ecc_status & ECC_STATUS_ERR_COUNT;
  457. if (!(ecc_status & ECC_STATUS_MASK))
  458. return ecc_count;
  459. /* Read OOB without ECC unit enabled */
  460. vf610_nfc_command(mtd, NAND_CMD_READOOB, 0, page);
  461. vf610_nfc_read_buf(mtd, oob, mtd->oobsize);
  462. /*
  463. * On an erased page, bit count (including OOB) should be zero or
  464. * at least less then half of the ECC strength.
  465. */
  466. return nand_check_erased_ecc_chunk(dat, nfc->chip.ecc.size, oob,
  467. mtd->oobsize, NULL, 0,
  468. flips_threshold);
  469. }
  470. static int vf610_nfc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
  471. uint8_t *buf, int oob_required, int page)
  472. {
  473. int eccsize = chip->ecc.size;
  474. int stat;
  475. vf610_nfc_read_buf(mtd, buf, eccsize);
  476. if (oob_required)
  477. vf610_nfc_read_buf(mtd, chip->oob_poi, mtd->oobsize);
  478. stat = vf610_nfc_correct_data(mtd, buf, chip->oob_poi, page);
  479. if (stat < 0) {
  480. mtd->ecc_stats.failed++;
  481. return 0;
  482. } else {
  483. mtd->ecc_stats.corrected += stat;
  484. return stat;
  485. }
  486. }
  487. static int vf610_nfc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  488. const uint8_t *buf, int oob_required, int page)
  489. {
  490. struct vf610_nfc *nfc = mtd_to_nfc(mtd);
  491. vf610_nfc_write_buf(mtd, buf, mtd->writesize);
  492. if (oob_required)
  493. vf610_nfc_write_buf(mtd, chip->oob_poi, mtd->oobsize);
  494. /* Always write whole page including OOB due to HW ECC */
  495. nfc->use_hw_ecc = true;
  496. nfc->write_sz = mtd->writesize + mtd->oobsize;
  497. return 0;
  498. }
  499. static const struct of_device_id vf610_nfc_dt_ids[] = {
  500. { .compatible = "fsl,vf610-nfc", .data = (void *)NFC_VFC610 },
  501. { /* sentinel */ }
  502. };
  503. MODULE_DEVICE_TABLE(of, vf610_nfc_dt_ids);
  504. static void vf610_nfc_preinit_controller(struct vf610_nfc *nfc)
  505. {
  506. vf610_nfc_clear(nfc, NFC_FLASH_CONFIG, CONFIG_16BIT);
  507. vf610_nfc_clear(nfc, NFC_FLASH_CONFIG, CONFIG_ADDR_AUTO_INCR_BIT);
  508. vf610_nfc_clear(nfc, NFC_FLASH_CONFIG, CONFIG_BUFNO_AUTO_INCR_BIT);
  509. vf610_nfc_clear(nfc, NFC_FLASH_CONFIG, CONFIG_BOOT_MODE_BIT);
  510. vf610_nfc_clear(nfc, NFC_FLASH_CONFIG, CONFIG_DMA_REQ_BIT);
  511. vf610_nfc_set(nfc, NFC_FLASH_CONFIG, CONFIG_FAST_FLASH_BIT);
  512. /* Disable virtual pages, only one elementary transfer unit */
  513. vf610_nfc_set_field(nfc, NFC_FLASH_CONFIG, CONFIG_PAGE_CNT_MASK,
  514. CONFIG_PAGE_CNT_SHIFT, 1);
  515. }
  516. static void vf610_nfc_init_controller(struct vf610_nfc *nfc)
  517. {
  518. if (nfc->chip.options & NAND_BUSWIDTH_16)
  519. vf610_nfc_set(nfc, NFC_FLASH_CONFIG, CONFIG_16BIT);
  520. else
  521. vf610_nfc_clear(nfc, NFC_FLASH_CONFIG, CONFIG_16BIT);
  522. if (nfc->chip.ecc.mode == NAND_ECC_HW) {
  523. /* Set ECC status offset in SRAM */
  524. vf610_nfc_set_field(nfc, NFC_FLASH_CONFIG,
  525. CONFIG_ECC_SRAM_ADDR_MASK,
  526. CONFIG_ECC_SRAM_ADDR_SHIFT,
  527. ECC_SRAM_ADDR >> 3);
  528. /* Enable ECC status in SRAM */
  529. vf610_nfc_set(nfc, NFC_FLASH_CONFIG, CONFIG_ECC_SRAM_REQ_BIT);
  530. }
  531. }
  532. static int vf610_nfc_probe(struct platform_device *pdev)
  533. {
  534. struct vf610_nfc *nfc;
  535. struct resource *res;
  536. struct mtd_info *mtd;
  537. struct nand_chip *chip;
  538. struct device_node *child;
  539. const struct of_device_id *of_id;
  540. int err;
  541. int irq;
  542. nfc = devm_kzalloc(&pdev->dev, sizeof(*nfc), GFP_KERNEL);
  543. if (!nfc)
  544. return -ENOMEM;
  545. nfc->dev = &pdev->dev;
  546. chip = &nfc->chip;
  547. mtd = nand_to_mtd(chip);
  548. mtd->owner = THIS_MODULE;
  549. mtd->dev.parent = nfc->dev;
  550. mtd->name = DRV_NAME;
  551. irq = platform_get_irq(pdev, 0);
  552. if (irq <= 0)
  553. return -EINVAL;
  554. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  555. nfc->regs = devm_ioremap_resource(nfc->dev, res);
  556. if (IS_ERR(nfc->regs))
  557. return PTR_ERR(nfc->regs);
  558. nfc->clk = devm_clk_get(&pdev->dev, NULL);
  559. if (IS_ERR(nfc->clk))
  560. return PTR_ERR(nfc->clk);
  561. err = clk_prepare_enable(nfc->clk);
  562. if (err) {
  563. dev_err(nfc->dev, "Unable to enable clock!\n");
  564. return err;
  565. }
  566. of_id = of_match_device(vf610_nfc_dt_ids, &pdev->dev);
  567. nfc->variant = (enum vf610_nfc_variant)of_id->data;
  568. for_each_available_child_of_node(nfc->dev->of_node, child) {
  569. if (of_device_is_compatible(child, "fsl,vf610-nfc-nandcs")) {
  570. if (nand_get_flash_node(chip)) {
  571. dev_err(nfc->dev,
  572. "Only one NAND chip supported!\n");
  573. err = -EINVAL;
  574. goto error;
  575. }
  576. nand_set_flash_node(chip, child);
  577. }
  578. }
  579. if (!nand_get_flash_node(chip)) {
  580. dev_err(nfc->dev, "NAND chip sub-node missing!\n");
  581. err = -ENODEV;
  582. goto err_clk;
  583. }
  584. chip->dev_ready = vf610_nfc_dev_ready;
  585. chip->cmdfunc = vf610_nfc_command;
  586. chip->read_byte = vf610_nfc_read_byte;
  587. chip->read_word = vf610_nfc_read_word;
  588. chip->read_buf = vf610_nfc_read_buf;
  589. chip->write_buf = vf610_nfc_write_buf;
  590. chip->select_chip = vf610_nfc_select_chip;
  591. chip->options |= NAND_NO_SUBPAGE_WRITE;
  592. init_completion(&nfc->cmd_done);
  593. err = devm_request_irq(nfc->dev, irq, vf610_nfc_irq, 0, DRV_NAME, mtd);
  594. if (err) {
  595. dev_err(nfc->dev, "Error requesting IRQ!\n");
  596. goto error;
  597. }
  598. vf610_nfc_preinit_controller(nfc);
  599. /* first scan to find the device and get the page size */
  600. if (nand_scan_ident(mtd, 1, NULL)) {
  601. err = -ENXIO;
  602. goto error;
  603. }
  604. vf610_nfc_init_controller(nfc);
  605. /* Bad block options. */
  606. if (chip->bbt_options & NAND_BBT_USE_FLASH)
  607. chip->bbt_options |= NAND_BBT_NO_OOB;
  608. /* Single buffer only, max 256 OOB minus ECC status */
  609. if (mtd->writesize + mtd->oobsize > PAGE_2K + OOB_MAX - 8) {
  610. dev_err(nfc->dev, "Unsupported flash page size\n");
  611. err = -ENXIO;
  612. goto error;
  613. }
  614. if (chip->ecc.mode == NAND_ECC_HW) {
  615. if (mtd->writesize != PAGE_2K && mtd->oobsize < 64) {
  616. dev_err(nfc->dev, "Unsupported flash with hwecc\n");
  617. err = -ENXIO;
  618. goto error;
  619. }
  620. if (chip->ecc.size != mtd->writesize) {
  621. dev_err(nfc->dev, "Step size needs to be page size\n");
  622. err = -ENXIO;
  623. goto error;
  624. }
  625. /* Only 64 byte ECC layouts known */
  626. if (mtd->oobsize > 64)
  627. mtd->oobsize = 64;
  628. /* Use default large page ECC layout defined in NAND core */
  629. mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
  630. if (chip->ecc.strength == 32) {
  631. nfc->ecc_mode = ECC_60_BYTE;
  632. chip->ecc.bytes = 60;
  633. } else if (chip->ecc.strength == 24) {
  634. nfc->ecc_mode = ECC_45_BYTE;
  635. chip->ecc.bytes = 45;
  636. } else {
  637. dev_err(nfc->dev, "Unsupported ECC strength\n");
  638. err = -ENXIO;
  639. goto error;
  640. }
  641. chip->ecc.read_page = vf610_nfc_read_page;
  642. chip->ecc.write_page = vf610_nfc_write_page;
  643. chip->ecc.size = PAGE_2K;
  644. }
  645. /* second phase scan */
  646. if (nand_scan_tail(mtd)) {
  647. err = -ENXIO;
  648. goto error;
  649. }
  650. platform_set_drvdata(pdev, mtd);
  651. /* Register device in MTD */
  652. return mtd_device_register(mtd, NULL, 0);
  653. error:
  654. of_node_put(nand_get_flash_node(chip));
  655. err_clk:
  656. clk_disable_unprepare(nfc->clk);
  657. return err;
  658. }
  659. static int vf610_nfc_remove(struct platform_device *pdev)
  660. {
  661. struct mtd_info *mtd = platform_get_drvdata(pdev);
  662. struct vf610_nfc *nfc = mtd_to_nfc(mtd);
  663. nand_release(mtd);
  664. clk_disable_unprepare(nfc->clk);
  665. return 0;
  666. }
  667. #ifdef CONFIG_PM_SLEEP
  668. static int vf610_nfc_suspend(struct device *dev)
  669. {
  670. struct mtd_info *mtd = dev_get_drvdata(dev);
  671. struct vf610_nfc *nfc = mtd_to_nfc(mtd);
  672. clk_disable_unprepare(nfc->clk);
  673. return 0;
  674. }
  675. static int vf610_nfc_resume(struct device *dev)
  676. {
  677. struct mtd_info *mtd = dev_get_drvdata(dev);
  678. struct vf610_nfc *nfc = mtd_to_nfc(mtd);
  679. pinctrl_pm_select_default_state(dev);
  680. clk_prepare_enable(nfc->clk);
  681. vf610_nfc_preinit_controller(nfc);
  682. vf610_nfc_init_controller(nfc);
  683. return 0;
  684. }
  685. #endif
  686. static SIMPLE_DEV_PM_OPS(vf610_nfc_pm_ops, vf610_nfc_suspend, vf610_nfc_resume);
  687. static struct platform_driver vf610_nfc_driver = {
  688. .driver = {
  689. .name = DRV_NAME,
  690. .of_match_table = vf610_nfc_dt_ids,
  691. .pm = &vf610_nfc_pm_ops,
  692. },
  693. .probe = vf610_nfc_probe,
  694. .remove = vf610_nfc_remove,
  695. };
  696. module_platform_driver(vf610_nfc_driver);
  697. MODULE_AUTHOR("Stefan Agner <stefan.agner@toradex.com>");
  698. MODULE_DESCRIPTION("Freescale VF610/MPC5125 NFC MTD NAND driver");
  699. MODULE_LICENSE("GPL");