vf610_nfc.c 22 KB

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