hisi504_nand.c 24 KB

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