hisi504_nand.c 24 KB

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