vf610_nfc.c 22 KB

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