vf610_nfc.c 22 KB

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