hisi504_nand.c 23 KB

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