hisi504_nand.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Hisilicon NAND Flash controller driver
  4. *
  5. * Copyright © 2012-2014 HiSilicon Technologies Co., Ltd.
  6. * http://www.hisilicon.com
  7. *
  8. * Author: Zhou Wang <[email protected]>
  9. * The initial developer of the original code is Zhiyong Cai
  10. * <[email protected]>
  11. */
  12. #include <linux/of.h>
  13. #include <linux/mtd/mtd.h>
  14. #include <linux/sizes.h>
  15. #include <linux/clk.h>
  16. #include <linux/slab.h>
  17. #include <linux/module.h>
  18. #include <linux/delay.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/mtd/rawnand.h>
  21. #include <linux/dma-mapping.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/mtd/partitions.h>
  24. #define HINFC504_MAX_CHIP (4)
  25. #define HINFC504_W_LATCH (5)
  26. #define HINFC504_R_LATCH (7)
  27. #define HINFC504_RW_LATCH (3)
  28. #define HINFC504_NFC_TIMEOUT (2 * HZ)
  29. #define HINFC504_NFC_PM_TIMEOUT (1 * HZ)
  30. #define HINFC504_NFC_DMA_TIMEOUT (5 * HZ)
  31. #define HINFC504_CHIP_DELAY (25)
  32. #define HINFC504_REG_BASE_ADDRESS_LEN (0x100)
  33. #define HINFC504_BUFFER_BASE_ADDRESS_LEN (2048 + 128)
  34. #define HINFC504_ADDR_CYCLE_MASK 0x4
  35. #define HINFC504_CON 0x00
  36. #define HINFC504_CON_OP_MODE_NORMAL BIT(0)
  37. #define HINFC504_CON_PAGEISZE_SHIFT (1)
  38. #define HINFC504_CON_PAGESIZE_MASK (0x07)
  39. #define HINFC504_CON_BUS_WIDTH BIT(4)
  40. #define HINFC504_CON_READY_BUSY_SEL BIT(8)
  41. #define HINFC504_CON_ECCTYPE_SHIFT (9)
  42. #define HINFC504_CON_ECCTYPE_MASK (0x07)
  43. #define HINFC504_PWIDTH 0x04
  44. #define SET_HINFC504_PWIDTH(_w_lcnt, _r_lcnt, _rw_hcnt) \
  45. ((_w_lcnt) | (((_r_lcnt) & 0x0F) << 4) | (((_rw_hcnt) & 0x0F) << 8))
  46. #define HINFC504_CMD 0x0C
  47. #define HINFC504_ADDRL 0x10
  48. #define HINFC504_ADDRH 0x14
  49. #define HINFC504_DATA_NUM 0x18
  50. #define HINFC504_OP 0x1C
  51. #define HINFC504_OP_READ_DATA_EN BIT(1)
  52. #define HINFC504_OP_WAIT_READY_EN BIT(2)
  53. #define HINFC504_OP_CMD2_EN BIT(3)
  54. #define HINFC504_OP_WRITE_DATA_EN BIT(4)
  55. #define HINFC504_OP_ADDR_EN BIT(5)
  56. #define HINFC504_OP_CMD1_EN BIT(6)
  57. #define HINFC504_OP_NF_CS_SHIFT (7)
  58. #define HINFC504_OP_NF_CS_MASK (3)
  59. #define HINFC504_OP_ADDR_CYCLE_SHIFT (9)
  60. #define HINFC504_OP_ADDR_CYCLE_MASK (7)
  61. #define HINFC504_STATUS 0x20
  62. #define HINFC504_READY BIT(0)
  63. #define HINFC504_INTEN 0x24
  64. #define HINFC504_INTEN_DMA BIT(9)
  65. #define HINFC504_INTEN_UE BIT(6)
  66. #define HINFC504_INTEN_CE BIT(5)
  67. #define HINFC504_INTS 0x28
  68. #define HINFC504_INTS_DMA BIT(9)
  69. #define HINFC504_INTS_UE BIT(6)
  70. #define HINFC504_INTS_CE BIT(5)
  71. #define HINFC504_INTCLR 0x2C
  72. #define HINFC504_INTCLR_DMA BIT(9)
  73. #define HINFC504_INTCLR_UE BIT(6)
  74. #define HINFC504_INTCLR_CE BIT(5)
  75. #define HINFC504_ECC_STATUS 0x5C
  76. #define HINFC504_ECC_16_BIT_SHIFT 12
  77. #define HINFC504_DMA_CTRL 0x60
  78. #define HINFC504_DMA_CTRL_DMA_START BIT(0)
  79. #define HINFC504_DMA_CTRL_WE BIT(1)
  80. #define HINFC504_DMA_CTRL_DATA_AREA_EN BIT(2)
  81. #define HINFC504_DMA_CTRL_OOB_AREA_EN BIT(3)
  82. #define HINFC504_DMA_CTRL_BURST4_EN BIT(4)
  83. #define HINFC504_DMA_CTRL_BURST8_EN BIT(5)
  84. #define HINFC504_DMA_CTRL_BURST16_EN BIT(6)
  85. #define HINFC504_DMA_CTRL_ADDR_NUM_SHIFT (7)
  86. #define HINFC504_DMA_CTRL_ADDR_NUM_MASK (1)
  87. #define HINFC504_DMA_CTRL_CS_SHIFT (8)
  88. #define HINFC504_DMA_CTRL_CS_MASK (0x03)
  89. #define HINFC504_DMA_ADDR_DATA 0x64
  90. #define HINFC504_DMA_ADDR_OOB 0x68
  91. #define HINFC504_DMA_LEN 0x6C
  92. #define HINFC504_DMA_LEN_OOB_SHIFT (16)
  93. #define HINFC504_DMA_LEN_OOB_MASK (0xFFF)
  94. #define HINFC504_DMA_PARA 0x70
  95. #define HINFC504_DMA_PARA_DATA_RW_EN BIT(0)
  96. #define HINFC504_DMA_PARA_OOB_RW_EN BIT(1)
  97. #define HINFC504_DMA_PARA_DATA_EDC_EN BIT(2)
  98. #define HINFC504_DMA_PARA_OOB_EDC_EN BIT(3)
  99. #define HINFC504_DMA_PARA_DATA_ECC_EN BIT(4)
  100. #define HINFC504_DMA_PARA_OOB_ECC_EN BIT(5)
  101. #define HINFC_VERSION 0x74
  102. #define HINFC504_LOG_READ_ADDR 0x7C
  103. #define HINFC504_LOG_READ_LEN 0x80
  104. #define HINFC504_NANDINFO_LEN 0x10
  105. struct hinfc_host {
  106. struct nand_chip chip;
  107. struct device *dev;
  108. void __iomem *iobase;
  109. void __iomem *mmio;
  110. struct completion cmd_complete;
  111. unsigned int offset;
  112. unsigned int command;
  113. int chipselect;
  114. unsigned int addr_cycle;
  115. u32 addr_value[2];
  116. u32 cache_addr_value[2];
  117. char *buffer;
  118. dma_addr_t dma_buffer;
  119. dma_addr_t dma_oob;
  120. int version;
  121. unsigned int irq_status; /* interrupt status */
  122. };
  123. static inline unsigned int hinfc_read(struct hinfc_host *host, unsigned int reg)
  124. {
  125. return readl(host->iobase + reg);
  126. }
  127. static inline void hinfc_write(struct hinfc_host *host, unsigned int value,
  128. unsigned int reg)
  129. {
  130. writel(value, host->iobase + reg);
  131. }
  132. static void wait_controller_finished(struct hinfc_host *host)
  133. {
  134. unsigned long timeout = jiffies + HINFC504_NFC_TIMEOUT;
  135. int val;
  136. while (time_before(jiffies, timeout)) {
  137. val = hinfc_read(host, HINFC504_STATUS);
  138. if (host->command == NAND_CMD_ERASE2) {
  139. /* nfc is ready */
  140. while (!(val & HINFC504_READY)) {
  141. usleep_range(500, 1000);
  142. val = hinfc_read(host, HINFC504_STATUS);
  143. }
  144. return;
  145. }
  146. if (val & HINFC504_READY)
  147. return;
  148. }
  149. /* wait cmd timeout */
  150. dev_err(host->dev, "Wait NAND controller exec cmd timeout.\n");
  151. }
  152. static void hisi_nfc_dma_transfer(struct hinfc_host *host, int todev)
  153. {
  154. struct nand_chip *chip = &host->chip;
  155. struct mtd_info *mtd = nand_to_mtd(chip);
  156. unsigned long val;
  157. int ret;
  158. hinfc_write(host, host->dma_buffer, HINFC504_DMA_ADDR_DATA);
  159. hinfc_write(host, host->dma_oob, HINFC504_DMA_ADDR_OOB);
  160. if (chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_NONE) {
  161. hinfc_write(host, ((mtd->oobsize & HINFC504_DMA_LEN_OOB_MASK)
  162. << HINFC504_DMA_LEN_OOB_SHIFT), HINFC504_DMA_LEN);
  163. hinfc_write(host, HINFC504_DMA_PARA_DATA_RW_EN
  164. | HINFC504_DMA_PARA_OOB_RW_EN, HINFC504_DMA_PARA);
  165. } else {
  166. if (host->command == NAND_CMD_READOOB)
  167. hinfc_write(host, HINFC504_DMA_PARA_OOB_RW_EN
  168. | HINFC504_DMA_PARA_OOB_EDC_EN
  169. | HINFC504_DMA_PARA_OOB_ECC_EN, HINFC504_DMA_PARA);
  170. else
  171. hinfc_write(host, HINFC504_DMA_PARA_DATA_RW_EN
  172. | HINFC504_DMA_PARA_OOB_RW_EN
  173. | HINFC504_DMA_PARA_DATA_EDC_EN
  174. | HINFC504_DMA_PARA_OOB_EDC_EN
  175. | HINFC504_DMA_PARA_DATA_ECC_EN
  176. | HINFC504_DMA_PARA_OOB_ECC_EN, HINFC504_DMA_PARA);
  177. }
  178. val = (HINFC504_DMA_CTRL_DMA_START | HINFC504_DMA_CTRL_BURST4_EN
  179. | HINFC504_DMA_CTRL_BURST8_EN | HINFC504_DMA_CTRL_BURST16_EN
  180. | HINFC504_DMA_CTRL_DATA_AREA_EN | HINFC504_DMA_CTRL_OOB_AREA_EN
  181. | ((host->addr_cycle == 4 ? 1 : 0)
  182. << HINFC504_DMA_CTRL_ADDR_NUM_SHIFT)
  183. | ((host->chipselect & HINFC504_DMA_CTRL_CS_MASK)
  184. << HINFC504_DMA_CTRL_CS_SHIFT));
  185. if (todev)
  186. val |= HINFC504_DMA_CTRL_WE;
  187. init_completion(&host->cmd_complete);
  188. hinfc_write(host, val, HINFC504_DMA_CTRL);
  189. ret = wait_for_completion_timeout(&host->cmd_complete,
  190. HINFC504_NFC_DMA_TIMEOUT);
  191. if (!ret) {
  192. dev_err(host->dev, "DMA operation(irq) timeout!\n");
  193. /* sanity check */
  194. val = hinfc_read(host, HINFC504_DMA_CTRL);
  195. if (!(val & HINFC504_DMA_CTRL_DMA_START))
  196. dev_err(host->dev, "DMA is already done but without irq ACK!\n");
  197. else
  198. dev_err(host->dev, "DMA is really timeout!\n");
  199. }
  200. }
  201. static int hisi_nfc_send_cmd_pageprog(struct hinfc_host *host)
  202. {
  203. host->addr_value[0] &= 0xffff0000;
  204. hinfc_write(host, host->addr_value[0], HINFC504_ADDRL);
  205. hinfc_write(host, host->addr_value[1], HINFC504_ADDRH);
  206. hinfc_write(host, NAND_CMD_PAGEPROG << 8 | NAND_CMD_SEQIN,
  207. HINFC504_CMD);
  208. hisi_nfc_dma_transfer(host, 1);
  209. return 0;
  210. }
  211. static int hisi_nfc_send_cmd_readstart(struct hinfc_host *host)
  212. {
  213. struct mtd_info *mtd = nand_to_mtd(&host->chip);
  214. if ((host->addr_value[0] == host->cache_addr_value[0]) &&
  215. (host->addr_value[1] == host->cache_addr_value[1]))
  216. return 0;
  217. host->addr_value[0] &= 0xffff0000;
  218. hinfc_write(host, host->addr_value[0], HINFC504_ADDRL);
  219. hinfc_write(host, host->addr_value[1], HINFC504_ADDRH);
  220. hinfc_write(host, NAND_CMD_READSTART << 8 | NAND_CMD_READ0,
  221. HINFC504_CMD);
  222. hinfc_write(host, 0, HINFC504_LOG_READ_ADDR);
  223. hinfc_write(host, mtd->writesize + mtd->oobsize,
  224. HINFC504_LOG_READ_LEN);
  225. hisi_nfc_dma_transfer(host, 0);
  226. host->cache_addr_value[0] = host->addr_value[0];
  227. host->cache_addr_value[1] = host->addr_value[1];
  228. return 0;
  229. }
  230. static int hisi_nfc_send_cmd_erase(struct hinfc_host *host)
  231. {
  232. hinfc_write(host, host->addr_value[0], HINFC504_ADDRL);
  233. hinfc_write(host, (NAND_CMD_ERASE2 << 8) | NAND_CMD_ERASE1,
  234. HINFC504_CMD);
  235. hinfc_write(host, HINFC504_OP_WAIT_READY_EN
  236. | HINFC504_OP_CMD2_EN
  237. | HINFC504_OP_CMD1_EN
  238. | HINFC504_OP_ADDR_EN
  239. | ((host->chipselect & HINFC504_OP_NF_CS_MASK)
  240. << HINFC504_OP_NF_CS_SHIFT)
  241. | ((host->addr_cycle & HINFC504_OP_ADDR_CYCLE_MASK)
  242. << HINFC504_OP_ADDR_CYCLE_SHIFT),
  243. HINFC504_OP);
  244. wait_controller_finished(host);
  245. return 0;
  246. }
  247. static int hisi_nfc_send_cmd_readid(struct hinfc_host *host)
  248. {
  249. hinfc_write(host, HINFC504_NANDINFO_LEN, HINFC504_DATA_NUM);
  250. hinfc_write(host, NAND_CMD_READID, HINFC504_CMD);
  251. hinfc_write(host, 0, HINFC504_ADDRL);
  252. hinfc_write(host, HINFC504_OP_CMD1_EN | HINFC504_OP_ADDR_EN
  253. | HINFC504_OP_READ_DATA_EN
  254. | ((host->chipselect & HINFC504_OP_NF_CS_MASK)
  255. << HINFC504_OP_NF_CS_SHIFT)
  256. | 1 << HINFC504_OP_ADDR_CYCLE_SHIFT, HINFC504_OP);
  257. wait_controller_finished(host);
  258. return 0;
  259. }
  260. static int hisi_nfc_send_cmd_status(struct hinfc_host *host)
  261. {
  262. hinfc_write(host, HINFC504_NANDINFO_LEN, HINFC504_DATA_NUM);
  263. hinfc_write(host, NAND_CMD_STATUS, HINFC504_CMD);
  264. hinfc_write(host, HINFC504_OP_CMD1_EN
  265. | HINFC504_OP_READ_DATA_EN
  266. | ((host->chipselect & HINFC504_OP_NF_CS_MASK)
  267. << HINFC504_OP_NF_CS_SHIFT),
  268. HINFC504_OP);
  269. wait_controller_finished(host);
  270. return 0;
  271. }
  272. static int hisi_nfc_send_cmd_reset(struct hinfc_host *host, int chipselect)
  273. {
  274. hinfc_write(host, NAND_CMD_RESET, HINFC504_CMD);
  275. hinfc_write(host, HINFC504_OP_CMD1_EN
  276. | ((chipselect & HINFC504_OP_NF_CS_MASK)
  277. << HINFC504_OP_NF_CS_SHIFT)
  278. | HINFC504_OP_WAIT_READY_EN,
  279. HINFC504_OP);
  280. wait_controller_finished(host);
  281. return 0;
  282. }
  283. static void hisi_nfc_select_chip(struct nand_chip *chip, int chipselect)
  284. {
  285. struct hinfc_host *host = nand_get_controller_data(chip);
  286. if (chipselect < 0)
  287. return;
  288. host->chipselect = chipselect;
  289. }
  290. static uint8_t hisi_nfc_read_byte(struct nand_chip *chip)
  291. {
  292. struct hinfc_host *host = nand_get_controller_data(chip);
  293. if (host->command == NAND_CMD_STATUS)
  294. return *(uint8_t *)(host->mmio);
  295. host->offset++;
  296. if (host->command == NAND_CMD_READID)
  297. return *(uint8_t *)(host->mmio + host->offset - 1);
  298. return *(uint8_t *)(host->buffer + host->offset - 1);
  299. }
  300. static void
  301. hisi_nfc_write_buf(struct nand_chip *chip, const uint8_t *buf, int len)
  302. {
  303. struct hinfc_host *host = nand_get_controller_data(chip);
  304. memcpy(host->buffer + host->offset, buf, len);
  305. host->offset += len;
  306. }
  307. static void hisi_nfc_read_buf(struct nand_chip *chip, uint8_t *buf, int len)
  308. {
  309. struct hinfc_host *host = nand_get_controller_data(chip);
  310. memcpy(buf, host->buffer + host->offset, len);
  311. host->offset += len;
  312. }
  313. static void set_addr(struct mtd_info *mtd, int column, int page_addr)
  314. {
  315. struct nand_chip *chip = mtd_to_nand(mtd);
  316. struct hinfc_host *host = nand_get_controller_data(chip);
  317. unsigned int command = host->command;
  318. host->addr_cycle = 0;
  319. host->addr_value[0] = 0;
  320. host->addr_value[1] = 0;
  321. /* Serially input address */
  322. if (column != -1) {
  323. /* Adjust columns for 16 bit buswidth */
  324. if (chip->options & NAND_BUSWIDTH_16 &&
  325. !nand_opcode_8bits(command))
  326. column >>= 1;
  327. host->addr_value[0] = column & 0xffff;
  328. host->addr_cycle = 2;
  329. }
  330. if (page_addr != -1) {
  331. host->addr_value[0] |= (page_addr & 0xffff)
  332. << (host->addr_cycle * 8);
  333. host->addr_cycle += 2;
  334. if (chip->options & NAND_ROW_ADDR_3) {
  335. host->addr_cycle += 1;
  336. if (host->command == NAND_CMD_ERASE1)
  337. host->addr_value[0] |= ((page_addr >> 16) & 0xff) << 16;
  338. else
  339. host->addr_value[1] |= ((page_addr >> 16) & 0xff);
  340. }
  341. }
  342. }
  343. static void hisi_nfc_cmdfunc(struct nand_chip *chip, unsigned command,
  344. int column, int page_addr)
  345. {
  346. struct mtd_info *mtd = nand_to_mtd(chip);
  347. struct hinfc_host *host = nand_get_controller_data(chip);
  348. int is_cache_invalid = 1;
  349. unsigned int flag = 0;
  350. host->command = command;
  351. switch (command) {
  352. case NAND_CMD_READ0:
  353. case NAND_CMD_READOOB:
  354. if (command == NAND_CMD_READ0)
  355. host->offset = column;
  356. else
  357. host->offset = column + mtd->writesize;
  358. is_cache_invalid = 0;
  359. set_addr(mtd, column, page_addr);
  360. hisi_nfc_send_cmd_readstart(host);
  361. break;
  362. case NAND_CMD_SEQIN:
  363. host->offset = column;
  364. set_addr(mtd, column, page_addr);
  365. break;
  366. case NAND_CMD_ERASE1:
  367. set_addr(mtd, column, page_addr);
  368. break;
  369. case NAND_CMD_PAGEPROG:
  370. hisi_nfc_send_cmd_pageprog(host);
  371. break;
  372. case NAND_CMD_ERASE2:
  373. hisi_nfc_send_cmd_erase(host);
  374. break;
  375. case NAND_CMD_READID:
  376. host->offset = column;
  377. memset(host->mmio, 0, 0x10);
  378. hisi_nfc_send_cmd_readid(host);
  379. break;
  380. case NAND_CMD_STATUS:
  381. flag = hinfc_read(host, HINFC504_CON);
  382. if (chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_ON_HOST)
  383. hinfc_write(host,
  384. flag & ~(HINFC504_CON_ECCTYPE_MASK <<
  385. HINFC504_CON_ECCTYPE_SHIFT), HINFC504_CON);
  386. host->offset = 0;
  387. memset(host->mmio, 0, 0x10);
  388. hisi_nfc_send_cmd_status(host);
  389. hinfc_write(host, flag, HINFC504_CON);
  390. break;
  391. case NAND_CMD_RESET:
  392. hisi_nfc_send_cmd_reset(host, host->chipselect);
  393. break;
  394. default:
  395. dev_err(host->dev, "Error: unsupported cmd(cmd=%x, col=%x, page=%x)\n",
  396. command, column, page_addr);
  397. }
  398. if (is_cache_invalid) {
  399. host->cache_addr_value[0] = ~0;
  400. host->cache_addr_value[1] = ~0;
  401. }
  402. }
  403. static irqreturn_t hinfc_irq_handle(int irq, void *devid)
  404. {
  405. struct hinfc_host *host = devid;
  406. unsigned int flag;
  407. flag = hinfc_read(host, HINFC504_INTS);
  408. /* store interrupts state */
  409. host->irq_status |= flag;
  410. if (flag & HINFC504_INTS_DMA) {
  411. hinfc_write(host, HINFC504_INTCLR_DMA, HINFC504_INTCLR);
  412. complete(&host->cmd_complete);
  413. } else if (flag & HINFC504_INTS_CE) {
  414. hinfc_write(host, HINFC504_INTCLR_CE, HINFC504_INTCLR);
  415. } else if (flag & HINFC504_INTS_UE) {
  416. hinfc_write(host, HINFC504_INTCLR_UE, HINFC504_INTCLR);
  417. }
  418. return IRQ_HANDLED;
  419. }
  420. static int hisi_nand_read_page_hwecc(struct nand_chip *chip, uint8_t *buf,
  421. int oob_required, int page)
  422. {
  423. struct mtd_info *mtd = nand_to_mtd(chip);
  424. struct hinfc_host *host = nand_get_controller_data(chip);
  425. int max_bitflips = 0, stat = 0, stat_max = 0, status_ecc;
  426. int stat_1, stat_2;
  427. nand_read_page_op(chip, page, 0, buf, mtd->writesize);
  428. chip->legacy.read_buf(chip, chip->oob_poi, mtd->oobsize);
  429. /* errors which can not be corrected by ECC */
  430. if (host->irq_status & HINFC504_INTS_UE) {
  431. mtd->ecc_stats.failed++;
  432. } else if (host->irq_status & HINFC504_INTS_CE) {
  433. /* TODO: need add other ECC modes! */
  434. switch (chip->ecc.strength) {
  435. case 16:
  436. status_ecc = hinfc_read(host, HINFC504_ECC_STATUS) >>
  437. HINFC504_ECC_16_BIT_SHIFT & 0x0fff;
  438. stat_2 = status_ecc & 0x3f;
  439. stat_1 = status_ecc >> 6 & 0x3f;
  440. stat = stat_1 + stat_2;
  441. stat_max = max_t(int, stat_1, stat_2);
  442. }
  443. mtd->ecc_stats.corrected += stat;
  444. max_bitflips = max_t(int, max_bitflips, stat_max);
  445. }
  446. host->irq_status = 0;
  447. return max_bitflips;
  448. }
  449. static int hisi_nand_read_oob(struct nand_chip *chip, int page)
  450. {
  451. struct mtd_info *mtd = nand_to_mtd(chip);
  452. struct hinfc_host *host = nand_get_controller_data(chip);
  453. nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
  454. if (host->irq_status & HINFC504_INTS_UE) {
  455. host->irq_status = 0;
  456. return -EBADMSG;
  457. }
  458. host->irq_status = 0;
  459. return 0;
  460. }
  461. static int hisi_nand_write_page_hwecc(struct nand_chip *chip,
  462. const uint8_t *buf, int oob_required,
  463. int page)
  464. {
  465. struct mtd_info *mtd = nand_to_mtd(chip);
  466. nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize);
  467. if (oob_required)
  468. chip->legacy.write_buf(chip, chip->oob_poi, mtd->oobsize);
  469. return nand_prog_page_end_op(chip);
  470. }
  471. static void hisi_nfc_host_init(struct hinfc_host *host)
  472. {
  473. struct nand_chip *chip = &host->chip;
  474. unsigned int flag = 0;
  475. host->version = hinfc_read(host, HINFC_VERSION);
  476. host->addr_cycle = 0;
  477. host->addr_value[0] = 0;
  478. host->addr_value[1] = 0;
  479. host->cache_addr_value[0] = ~0;
  480. host->cache_addr_value[1] = ~0;
  481. host->chipselect = 0;
  482. /* default page size: 2K, ecc_none. need modify */
  483. flag = HINFC504_CON_OP_MODE_NORMAL | HINFC504_CON_READY_BUSY_SEL
  484. | ((0x001 & HINFC504_CON_PAGESIZE_MASK)
  485. << HINFC504_CON_PAGEISZE_SHIFT)
  486. | ((0x0 & HINFC504_CON_ECCTYPE_MASK)
  487. << HINFC504_CON_ECCTYPE_SHIFT)
  488. | ((chip->options & NAND_BUSWIDTH_16) ?
  489. HINFC504_CON_BUS_WIDTH : 0);
  490. hinfc_write(host, flag, HINFC504_CON);
  491. memset(host->mmio, 0xff, HINFC504_BUFFER_BASE_ADDRESS_LEN);
  492. hinfc_write(host, SET_HINFC504_PWIDTH(HINFC504_W_LATCH,
  493. HINFC504_R_LATCH, HINFC504_RW_LATCH), HINFC504_PWIDTH);
  494. /* enable DMA irq */
  495. hinfc_write(host, HINFC504_INTEN_DMA, HINFC504_INTEN);
  496. }
  497. static int hisi_ooblayout_ecc(struct mtd_info *mtd, int section,
  498. struct mtd_oob_region *oobregion)
  499. {
  500. /* FIXME: add ECC bytes position */
  501. return -ENOTSUPP;
  502. }
  503. static int hisi_ooblayout_free(struct mtd_info *mtd, int section,
  504. struct mtd_oob_region *oobregion)
  505. {
  506. if (section)
  507. return -ERANGE;
  508. oobregion->offset = 2;
  509. oobregion->length = 6;
  510. return 0;
  511. }
  512. static const struct mtd_ooblayout_ops hisi_ooblayout_ops = {
  513. .ecc = hisi_ooblayout_ecc,
  514. .free = hisi_ooblayout_free,
  515. };
  516. static int hisi_nfc_ecc_probe(struct hinfc_host *host)
  517. {
  518. unsigned int flag;
  519. int size, strength, ecc_bits;
  520. struct device *dev = host->dev;
  521. struct nand_chip *chip = &host->chip;
  522. struct mtd_info *mtd = nand_to_mtd(chip);
  523. size = chip->ecc.size;
  524. strength = chip->ecc.strength;
  525. if (size != 1024) {
  526. dev_err(dev, "error ecc size: %d\n", size);
  527. return -EINVAL;
  528. }
  529. if ((size == 1024) && ((strength != 8) && (strength != 16) &&
  530. (strength != 24) && (strength != 40))) {
  531. dev_err(dev, "ecc size and strength do not match\n");
  532. return -EINVAL;
  533. }
  534. chip->ecc.size = size;
  535. chip->ecc.strength = strength;
  536. chip->ecc.read_page = hisi_nand_read_page_hwecc;
  537. chip->ecc.read_oob = hisi_nand_read_oob;
  538. chip->ecc.write_page = hisi_nand_write_page_hwecc;
  539. switch (chip->ecc.strength) {
  540. case 16:
  541. ecc_bits = 6;
  542. if (mtd->writesize == 2048)
  543. mtd_set_ooblayout(mtd, &hisi_ooblayout_ops);
  544. /* TODO: add more page size support */
  545. break;
  546. /* TODO: add more ecc strength support */
  547. default:
  548. dev_err(dev, "not support strength: %d\n", chip->ecc.strength);
  549. return -EINVAL;
  550. }
  551. flag = hinfc_read(host, HINFC504_CON);
  552. /* add ecc type configure */
  553. flag |= ((ecc_bits & HINFC504_CON_ECCTYPE_MASK)
  554. << HINFC504_CON_ECCTYPE_SHIFT);
  555. hinfc_write(host, flag, HINFC504_CON);
  556. /* enable ecc irq */
  557. flag = hinfc_read(host, HINFC504_INTEN) & 0xfff;
  558. hinfc_write(host, flag | HINFC504_INTEN_UE | HINFC504_INTEN_CE,
  559. HINFC504_INTEN);
  560. return 0;
  561. }
  562. static int hisi_nfc_attach_chip(struct nand_chip *chip)
  563. {
  564. struct mtd_info *mtd = nand_to_mtd(chip);
  565. struct hinfc_host *host = nand_get_controller_data(chip);
  566. int flag;
  567. host->buffer = dmam_alloc_coherent(host->dev,
  568. mtd->writesize + mtd->oobsize,
  569. &host->dma_buffer, GFP_KERNEL);
  570. if (!host->buffer)
  571. return -ENOMEM;
  572. host->dma_oob = host->dma_buffer + mtd->writesize;
  573. memset(host->buffer, 0xff, mtd->writesize + mtd->oobsize);
  574. flag = hinfc_read(host, HINFC504_CON);
  575. flag &= ~(HINFC504_CON_PAGESIZE_MASK << HINFC504_CON_PAGEISZE_SHIFT);
  576. switch (mtd->writesize) {
  577. case 2048:
  578. flag |= (0x001 << HINFC504_CON_PAGEISZE_SHIFT);
  579. break;
  580. /*
  581. * TODO: add more pagesize support,
  582. * default pagesize has been set in hisi_nfc_host_init
  583. */
  584. default:
  585. dev_err(host->dev, "NON-2KB page size nand flash\n");
  586. return -EINVAL;
  587. }
  588. hinfc_write(host, flag, HINFC504_CON);
  589. if (chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_ON_HOST)
  590. hisi_nfc_ecc_probe(host);
  591. return 0;
  592. }
  593. static const struct nand_controller_ops hisi_nfc_controller_ops = {
  594. .attach_chip = hisi_nfc_attach_chip,
  595. };
  596. static int hisi_nfc_probe(struct platform_device *pdev)
  597. {
  598. int ret = 0, irq, max_chips = HINFC504_MAX_CHIP;
  599. struct device *dev = &pdev->dev;
  600. struct hinfc_host *host;
  601. struct nand_chip *chip;
  602. struct mtd_info *mtd;
  603. struct device_node *np = dev->of_node;
  604. host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
  605. if (!host)
  606. return -ENOMEM;
  607. host->dev = dev;
  608. platform_set_drvdata(pdev, host);
  609. chip = &host->chip;
  610. mtd = nand_to_mtd(chip);
  611. irq = platform_get_irq(pdev, 0);
  612. if (irq < 0)
  613. return -ENXIO;
  614. host->iobase = devm_platform_ioremap_resource(pdev, 0);
  615. if (IS_ERR(host->iobase))
  616. return PTR_ERR(host->iobase);
  617. host->mmio = devm_platform_ioremap_resource(pdev, 1);
  618. if (IS_ERR(host->mmio))
  619. return PTR_ERR(host->mmio);
  620. mtd->name = "hisi_nand";
  621. mtd->dev.parent = &pdev->dev;
  622. nand_set_controller_data(chip, host);
  623. nand_set_flash_node(chip, np);
  624. chip->legacy.cmdfunc = hisi_nfc_cmdfunc;
  625. chip->legacy.select_chip = hisi_nfc_select_chip;
  626. chip->legacy.read_byte = hisi_nfc_read_byte;
  627. chip->legacy.write_buf = hisi_nfc_write_buf;
  628. chip->legacy.read_buf = hisi_nfc_read_buf;
  629. chip->legacy.chip_delay = HINFC504_CHIP_DELAY;
  630. chip->legacy.set_features = nand_get_set_features_notsupp;
  631. chip->legacy.get_features = nand_get_set_features_notsupp;
  632. hisi_nfc_host_init(host);
  633. ret = devm_request_irq(dev, irq, hinfc_irq_handle, 0x0, "nandc", host);
  634. if (ret) {
  635. dev_err(dev, "failed to request IRQ\n");
  636. return ret;
  637. }
  638. chip->legacy.dummy_controller.ops = &hisi_nfc_controller_ops;
  639. ret = nand_scan(chip, max_chips);
  640. if (ret)
  641. return ret;
  642. ret = mtd_device_register(mtd, NULL, 0);
  643. if (ret) {
  644. dev_err(dev, "Err MTD partition=%d\n", ret);
  645. nand_cleanup(chip);
  646. return ret;
  647. }
  648. return 0;
  649. }
  650. static int hisi_nfc_remove(struct platform_device *pdev)
  651. {
  652. struct hinfc_host *host = platform_get_drvdata(pdev);
  653. struct nand_chip *chip = &host->chip;
  654. int ret;
  655. ret = mtd_device_unregister(nand_to_mtd(chip));
  656. WARN_ON(ret);
  657. nand_cleanup(chip);
  658. return 0;
  659. }
  660. #ifdef CONFIG_PM_SLEEP
  661. static int hisi_nfc_suspend(struct device *dev)
  662. {
  663. struct hinfc_host *host = dev_get_drvdata(dev);
  664. unsigned long timeout = jiffies + HINFC504_NFC_PM_TIMEOUT;
  665. while (time_before(jiffies, timeout)) {
  666. if (((hinfc_read(host, HINFC504_STATUS) & 0x1) == 0x0) &&
  667. (hinfc_read(host, HINFC504_DMA_CTRL) &
  668. HINFC504_DMA_CTRL_DMA_START)) {
  669. cond_resched();
  670. return 0;
  671. }
  672. }
  673. dev_err(host->dev, "nand controller suspend timeout.\n");
  674. return -EAGAIN;
  675. }
  676. static int hisi_nfc_resume(struct device *dev)
  677. {
  678. int cs;
  679. struct hinfc_host *host = dev_get_drvdata(dev);
  680. struct nand_chip *chip = &host->chip;
  681. for (cs = 0; cs < nanddev_ntargets(&chip->base); cs++)
  682. hisi_nfc_send_cmd_reset(host, cs);
  683. hinfc_write(host, SET_HINFC504_PWIDTH(HINFC504_W_LATCH,
  684. HINFC504_R_LATCH, HINFC504_RW_LATCH), HINFC504_PWIDTH);
  685. return 0;
  686. }
  687. #endif
  688. static SIMPLE_DEV_PM_OPS(hisi_nfc_pm_ops, hisi_nfc_suspend, hisi_nfc_resume);
  689. static const struct of_device_id nfc_id_table[] = {
  690. { .compatible = "hisilicon,504-nfc" },
  691. {}
  692. };
  693. MODULE_DEVICE_TABLE(of, nfc_id_table);
  694. static struct platform_driver hisi_nfc_driver = {
  695. .driver = {
  696. .name = "hisi_nand",
  697. .of_match_table = nfc_id_table,
  698. .pm = &hisi_nfc_pm_ops,
  699. },
  700. .probe = hisi_nfc_probe,
  701. .remove = hisi_nfc_remove,
  702. };
  703. module_platform_driver(hisi_nfc_driver);
  704. MODULE_LICENSE("GPL");
  705. MODULE_AUTHOR("Zhou Wang");
  706. MODULE_AUTHOR("Zhiyong Cai");
  707. MODULE_DESCRIPTION("Hisilicon Nand Flash Controller Driver");