hisi504_nand.c 24 KB

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