hisi504_nand.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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/rawnand.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. if (chip->options & NAND_ROW_ADDR_3) {
  355. host->addr_cycle += 1;
  356. if (host->command == NAND_CMD_ERASE1)
  357. host->addr_value[0] |= ((page_addr >> 16) & 0xff) << 16;
  358. else
  359. host->addr_value[1] |= ((page_addr >> 16) & 0xff);
  360. }
  361. }
  362. }
  363. static void hisi_nfc_cmdfunc(struct mtd_info *mtd, unsigned command, int column,
  364. int page_addr)
  365. {
  366. struct nand_chip *chip = mtd_to_nand(mtd);
  367. struct hinfc_host *host = nand_get_controller_data(chip);
  368. int is_cache_invalid = 1;
  369. unsigned int flag = 0;
  370. host->command = command;
  371. switch (command) {
  372. case NAND_CMD_READ0:
  373. case NAND_CMD_READOOB:
  374. if (command == NAND_CMD_READ0)
  375. host->offset = column;
  376. else
  377. host->offset = column + mtd->writesize;
  378. is_cache_invalid = 0;
  379. set_addr(mtd, column, page_addr);
  380. hisi_nfc_send_cmd_readstart(host);
  381. break;
  382. case NAND_CMD_SEQIN:
  383. host->offset = column;
  384. set_addr(mtd, column, page_addr);
  385. break;
  386. case NAND_CMD_ERASE1:
  387. set_addr(mtd, column, page_addr);
  388. break;
  389. case NAND_CMD_PAGEPROG:
  390. hisi_nfc_send_cmd_pageprog(host);
  391. break;
  392. case NAND_CMD_ERASE2:
  393. hisi_nfc_send_cmd_erase(host);
  394. break;
  395. case NAND_CMD_READID:
  396. host->offset = column;
  397. memset(host->mmio, 0, 0x10);
  398. hisi_nfc_send_cmd_readid(host);
  399. break;
  400. case NAND_CMD_STATUS:
  401. flag = hinfc_read(host, HINFC504_CON);
  402. if (chip->ecc.mode == NAND_ECC_HW)
  403. hinfc_write(host,
  404. flag & ~(HINFC504_CON_ECCTYPE_MASK <<
  405. HINFC504_CON_ECCTYPE_SHIFT), HINFC504_CON);
  406. host->offset = 0;
  407. memset(host->mmio, 0, 0x10);
  408. hisi_nfc_send_cmd_status(host);
  409. hinfc_write(host, flag, HINFC504_CON);
  410. break;
  411. case NAND_CMD_RESET:
  412. hisi_nfc_send_cmd_reset(host, host->chipselect);
  413. break;
  414. default:
  415. dev_err(host->dev, "Error: unsupported cmd(cmd=%x, col=%x, page=%x)\n",
  416. command, column, page_addr);
  417. }
  418. if (is_cache_invalid) {
  419. host->cache_addr_value[0] = ~0;
  420. host->cache_addr_value[1] = ~0;
  421. }
  422. }
  423. static irqreturn_t hinfc_irq_handle(int irq, void *devid)
  424. {
  425. struct hinfc_host *host = devid;
  426. unsigned int flag;
  427. flag = hinfc_read(host, HINFC504_INTS);
  428. /* store interrupts state */
  429. host->irq_status |= flag;
  430. if (flag & HINFC504_INTS_DMA) {
  431. hinfc_write(host, HINFC504_INTCLR_DMA, HINFC504_INTCLR);
  432. complete(&host->cmd_complete);
  433. } else if (flag & HINFC504_INTS_CE) {
  434. hinfc_write(host, HINFC504_INTCLR_CE, HINFC504_INTCLR);
  435. } else if (flag & HINFC504_INTS_UE) {
  436. hinfc_write(host, HINFC504_INTCLR_UE, HINFC504_INTCLR);
  437. }
  438. return IRQ_HANDLED;
  439. }
  440. static int hisi_nand_read_page_hwecc(struct mtd_info *mtd,
  441. struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
  442. {
  443. struct hinfc_host *host = nand_get_controller_data(chip);
  444. int max_bitflips = 0, stat = 0, stat_max = 0, status_ecc;
  445. int stat_1, stat_2;
  446. nand_read_page_op(chip, page, 0, buf, mtd->writesize);
  447. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  448. /* errors which can not be corrected by ECC */
  449. if (host->irq_status & HINFC504_INTS_UE) {
  450. mtd->ecc_stats.failed++;
  451. } else if (host->irq_status & HINFC504_INTS_CE) {
  452. /* TODO: need add other ECC modes! */
  453. switch (chip->ecc.strength) {
  454. case 16:
  455. status_ecc = hinfc_read(host, HINFC504_ECC_STATUS) >>
  456. HINFC504_ECC_16_BIT_SHIFT & 0x0fff;
  457. stat_2 = status_ecc & 0x3f;
  458. stat_1 = status_ecc >> 6 & 0x3f;
  459. stat = stat_1 + stat_2;
  460. stat_max = max_t(int, stat_1, stat_2);
  461. }
  462. mtd->ecc_stats.corrected += stat;
  463. max_bitflips = max_t(int, max_bitflips, stat_max);
  464. }
  465. host->irq_status = 0;
  466. return max_bitflips;
  467. }
  468. static int hisi_nand_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
  469. int page)
  470. {
  471. struct hinfc_host *host = nand_get_controller_data(chip);
  472. nand_read_oob_op(chip, page, 0, chip->oob_poi, mtd->oobsize);
  473. if (host->irq_status & HINFC504_INTS_UE) {
  474. host->irq_status = 0;
  475. return -EBADMSG;
  476. }
  477. host->irq_status = 0;
  478. return 0;
  479. }
  480. static int hisi_nand_write_page_hwecc(struct mtd_info *mtd,
  481. struct nand_chip *chip, const uint8_t *buf, int oob_required,
  482. int page)
  483. {
  484. nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize);
  485. if (oob_required)
  486. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  487. return nand_prog_page_end_op(chip);
  488. }
  489. static void hisi_nfc_host_init(struct hinfc_host *host)
  490. {
  491. struct nand_chip *chip = &host->chip;
  492. unsigned int flag = 0;
  493. host->version = hinfc_read(host, HINFC_VERSION);
  494. host->addr_cycle = 0;
  495. host->addr_value[0] = 0;
  496. host->addr_value[1] = 0;
  497. host->cache_addr_value[0] = ~0;
  498. host->cache_addr_value[1] = ~0;
  499. host->chipselect = 0;
  500. /* default page size: 2K, ecc_none. need modify */
  501. flag = HINFC504_CON_OP_MODE_NORMAL | HINFC504_CON_READY_BUSY_SEL
  502. | ((0x001 & HINFC504_CON_PAGESIZE_MASK)
  503. << HINFC504_CON_PAGEISZE_SHIFT)
  504. | ((0x0 & HINFC504_CON_ECCTYPE_MASK)
  505. << HINFC504_CON_ECCTYPE_SHIFT)
  506. | ((chip->options & NAND_BUSWIDTH_16) ?
  507. HINFC504_CON_BUS_WIDTH : 0);
  508. hinfc_write(host, flag, HINFC504_CON);
  509. memset(host->mmio, 0xff, HINFC504_BUFFER_BASE_ADDRESS_LEN);
  510. hinfc_write(host, SET_HINFC504_PWIDTH(HINFC504_W_LATCH,
  511. HINFC504_R_LATCH, HINFC504_RW_LATCH), HINFC504_PWIDTH);
  512. /* enable DMA irq */
  513. hinfc_write(host, HINFC504_INTEN_DMA, HINFC504_INTEN);
  514. }
  515. static int hisi_ooblayout_ecc(struct mtd_info *mtd, int section,
  516. struct mtd_oob_region *oobregion)
  517. {
  518. /* FIXME: add ECC bytes position */
  519. return -ENOTSUPP;
  520. }
  521. static int hisi_ooblayout_free(struct mtd_info *mtd, int section,
  522. struct mtd_oob_region *oobregion)
  523. {
  524. if (section)
  525. return -ERANGE;
  526. oobregion->offset = 2;
  527. oobregion->length = 6;
  528. return 0;
  529. }
  530. static const struct mtd_ooblayout_ops hisi_ooblayout_ops = {
  531. .ecc = hisi_ooblayout_ecc,
  532. .free = hisi_ooblayout_free,
  533. };
  534. static int hisi_nfc_ecc_probe(struct hinfc_host *host)
  535. {
  536. unsigned int flag;
  537. int size, strength, ecc_bits;
  538. struct device *dev = host->dev;
  539. struct nand_chip *chip = &host->chip;
  540. struct mtd_info *mtd = nand_to_mtd(chip);
  541. size = chip->ecc.size;
  542. strength = chip->ecc.strength;
  543. if (size != 1024) {
  544. dev_err(dev, "error ecc size: %d\n", size);
  545. return -EINVAL;
  546. }
  547. if ((size == 1024) && ((strength != 8) && (strength != 16) &&
  548. (strength != 24) && (strength != 40))) {
  549. dev_err(dev, "ecc size and strength do not match\n");
  550. return -EINVAL;
  551. }
  552. chip->ecc.size = size;
  553. chip->ecc.strength = strength;
  554. chip->ecc.read_page = hisi_nand_read_page_hwecc;
  555. chip->ecc.read_oob = hisi_nand_read_oob;
  556. chip->ecc.write_page = hisi_nand_write_page_hwecc;
  557. switch (chip->ecc.strength) {
  558. case 16:
  559. ecc_bits = 6;
  560. if (mtd->writesize == 2048)
  561. mtd_set_ooblayout(mtd, &hisi_ooblayout_ops);
  562. /* TODO: add more page size support */
  563. break;
  564. /* TODO: add more ecc strength support */
  565. default:
  566. dev_err(dev, "not support strength: %d\n", chip->ecc.strength);
  567. return -EINVAL;
  568. }
  569. flag = hinfc_read(host, HINFC504_CON);
  570. /* add ecc type configure */
  571. flag |= ((ecc_bits & HINFC504_CON_ECCTYPE_MASK)
  572. << HINFC504_CON_ECCTYPE_SHIFT);
  573. hinfc_write(host, flag, HINFC504_CON);
  574. /* enable ecc irq */
  575. flag = hinfc_read(host, HINFC504_INTEN) & 0xfff;
  576. hinfc_write(host, flag | HINFC504_INTEN_UE | HINFC504_INTEN_CE,
  577. HINFC504_INTEN);
  578. return 0;
  579. }
  580. static int hisi_nfc_probe(struct platform_device *pdev)
  581. {
  582. int ret = 0, irq, flag, max_chips = HINFC504_MAX_CHIP;
  583. struct device *dev = &pdev->dev;
  584. struct hinfc_host *host;
  585. struct nand_chip *chip;
  586. struct mtd_info *mtd;
  587. struct resource *res;
  588. struct device_node *np = dev->of_node;
  589. host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
  590. if (!host)
  591. return -ENOMEM;
  592. host->dev = dev;
  593. platform_set_drvdata(pdev, host);
  594. chip = &host->chip;
  595. mtd = nand_to_mtd(chip);
  596. irq = platform_get_irq(pdev, 0);
  597. if (irq < 0) {
  598. dev_err(dev, "no IRQ resource defined\n");
  599. ret = -ENXIO;
  600. goto err_res;
  601. }
  602. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  603. host->iobase = devm_ioremap_resource(dev, res);
  604. if (IS_ERR(host->iobase)) {
  605. ret = PTR_ERR(host->iobase);
  606. goto err_res;
  607. }
  608. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  609. host->mmio = devm_ioremap_resource(dev, res);
  610. if (IS_ERR(host->mmio)) {
  611. ret = PTR_ERR(host->mmio);
  612. dev_err(dev, "devm_ioremap_resource[1] fail\n");
  613. goto err_res;
  614. }
  615. mtd->name = "hisi_nand";
  616. mtd->dev.parent = &pdev->dev;
  617. nand_set_controller_data(chip, host);
  618. nand_set_flash_node(chip, np);
  619. chip->cmdfunc = hisi_nfc_cmdfunc;
  620. chip->select_chip = hisi_nfc_select_chip;
  621. chip->read_byte = hisi_nfc_read_byte;
  622. chip->read_word = hisi_nfc_read_word;
  623. chip->write_buf = hisi_nfc_write_buf;
  624. chip->read_buf = hisi_nfc_read_buf;
  625. chip->chip_delay = HINFC504_CHIP_DELAY;
  626. chip->set_features = nand_get_set_features_notsupp;
  627. chip->get_features = nand_get_set_features_notsupp;
  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. goto err_res;
  637. host->buffer = dmam_alloc_coherent(dev, mtd->writesize + mtd->oobsize,
  638. &host->dma_buffer, GFP_KERNEL);
  639. if (!host->buffer) {
  640. ret = -ENOMEM;
  641. goto err_res;
  642. }
  643. host->dma_oob = host->dma_buffer + mtd->writesize;
  644. memset(host->buffer, 0xff, mtd->writesize + mtd->oobsize);
  645. flag = hinfc_read(host, HINFC504_CON);
  646. flag &= ~(HINFC504_CON_PAGESIZE_MASK << HINFC504_CON_PAGEISZE_SHIFT);
  647. switch (mtd->writesize) {
  648. case 2048:
  649. flag |= (0x001 << HINFC504_CON_PAGEISZE_SHIFT); break;
  650. /*
  651. * TODO: add more pagesize support,
  652. * default pagesize has been set in hisi_nfc_host_init
  653. */
  654. default:
  655. dev_err(dev, "NON-2KB page size nand flash\n");
  656. ret = -EINVAL;
  657. goto err_res;
  658. }
  659. hinfc_write(host, flag, HINFC504_CON);
  660. if (chip->ecc.mode == NAND_ECC_HW)
  661. hisi_nfc_ecc_probe(host);
  662. ret = nand_scan_tail(mtd);
  663. if (ret) {
  664. dev_err(dev, "nand_scan_tail failed: %d\n", ret);
  665. goto err_res;
  666. }
  667. ret = mtd_device_register(mtd, NULL, 0);
  668. if (ret) {
  669. dev_err(dev, "Err MTD partition=%d\n", ret);
  670. goto err_mtd;
  671. }
  672. return 0;
  673. err_mtd:
  674. nand_release(mtd);
  675. err_res:
  676. return ret;
  677. }
  678. static int hisi_nfc_remove(struct platform_device *pdev)
  679. {
  680. struct hinfc_host *host = platform_get_drvdata(pdev);
  681. struct mtd_info *mtd = nand_to_mtd(&host->chip);
  682. nand_release(mtd);
  683. return 0;
  684. }
  685. #ifdef CONFIG_PM_SLEEP
  686. static int hisi_nfc_suspend(struct device *dev)
  687. {
  688. struct hinfc_host *host = dev_get_drvdata(dev);
  689. unsigned long timeout = jiffies + HINFC504_NFC_PM_TIMEOUT;
  690. while (time_before(jiffies, timeout)) {
  691. if (((hinfc_read(host, HINFC504_STATUS) & 0x1) == 0x0) &&
  692. (hinfc_read(host, HINFC504_DMA_CTRL) &
  693. HINFC504_DMA_CTRL_DMA_START)) {
  694. cond_resched();
  695. return 0;
  696. }
  697. }
  698. dev_err(host->dev, "nand controller suspend timeout.\n");
  699. return -EAGAIN;
  700. }
  701. static int hisi_nfc_resume(struct device *dev)
  702. {
  703. int cs;
  704. struct hinfc_host *host = dev_get_drvdata(dev);
  705. struct nand_chip *chip = &host->chip;
  706. for (cs = 0; cs < chip->numchips; cs++)
  707. hisi_nfc_send_cmd_reset(host, cs);
  708. hinfc_write(host, SET_HINFC504_PWIDTH(HINFC504_W_LATCH,
  709. HINFC504_R_LATCH, HINFC504_RW_LATCH), HINFC504_PWIDTH);
  710. return 0;
  711. }
  712. #endif
  713. static SIMPLE_DEV_PM_OPS(hisi_nfc_pm_ops, hisi_nfc_suspend, hisi_nfc_resume);
  714. static const struct of_device_id nfc_id_table[] = {
  715. { .compatible = "hisilicon,504-nfc" },
  716. {}
  717. };
  718. MODULE_DEVICE_TABLE(of, nfc_id_table);
  719. static struct platform_driver hisi_nfc_driver = {
  720. .driver = {
  721. .name = "hisi_nand",
  722. .of_match_table = nfc_id_table,
  723. .pm = &hisi_nfc_pm_ops,
  724. },
  725. .probe = hisi_nfc_probe,
  726. .remove = hisi_nfc_remove,
  727. };
  728. module_platform_driver(hisi_nfc_driver);
  729. MODULE_LICENSE("GPL");
  730. MODULE_AUTHOR("Zhou Wang");
  731. MODULE_AUTHOR("Zhiyong Cai");
  732. MODULE_DESCRIPTION("Hisilicon Nand Flash Controller Driver");