intel-nand-controller.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /* Copyright (c) 2020 Intel Corporation. */
  3. #include <linux/clk.h>
  4. #include <linux/completion.h>
  5. #include <linux/dmaengine.h>
  6. #include <linux/dma-direction.h>
  7. #include <linux/dma-mapping.h>
  8. #include <linux/err.h>
  9. #include <linux/init.h>
  10. #include <linux/iopoll.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/mtd/mtd.h>
  14. #include <linux/mtd/rawnand.h>
  15. #include <linux/mtd/nand.h>
  16. #include <linux/of.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/sched.h>
  19. #include <linux/slab.h>
  20. #include <linux/types.h>
  21. #include <linux/units.h>
  22. #include <asm/unaligned.h>
  23. #define EBU_CLC 0x000
  24. #define EBU_CLC_RST 0x00000000u
  25. #define EBU_ADDR_SEL(n) (0x020 + (n) * 4)
  26. /* 5 bits 26:22 included for comparison in the ADDR_SELx */
  27. #define EBU_ADDR_MASK(x) ((x) << 4)
  28. #define EBU_ADDR_SEL_REGEN 0x1
  29. #define EBU_BUSCON(n) (0x060 + (n) * 4)
  30. #define EBU_BUSCON_CMULT_V4 0x1
  31. #define EBU_BUSCON_RECOVC(n) ((n) << 2)
  32. #define EBU_BUSCON_HOLDC(n) ((n) << 4)
  33. #define EBU_BUSCON_WAITRDC(n) ((n) << 6)
  34. #define EBU_BUSCON_WAITWRC(n) ((n) << 8)
  35. #define EBU_BUSCON_BCGEN_CS 0x0
  36. #define EBU_BUSCON_SETUP_EN BIT(22)
  37. #define EBU_BUSCON_ALEC 0xC000
  38. #define EBU_CON 0x0B0
  39. #define EBU_CON_NANDM_EN BIT(0)
  40. #define EBU_CON_NANDM_DIS 0x0
  41. #define EBU_CON_CSMUX_E_EN BIT(1)
  42. #define EBU_CON_ALE_P_LOW BIT(2)
  43. #define EBU_CON_CLE_P_LOW BIT(3)
  44. #define EBU_CON_CS_P_LOW BIT(4)
  45. #define EBU_CON_SE_P_LOW BIT(5)
  46. #define EBU_CON_WP_P_LOW BIT(6)
  47. #define EBU_CON_PRE_P_LOW BIT(7)
  48. #define EBU_CON_IN_CS_S(n) ((n) << 8)
  49. #define EBU_CON_OUT_CS_S(n) ((n) << 10)
  50. #define EBU_CON_LAT_EN_CS_P ((0x3D) << 18)
  51. #define EBU_WAIT 0x0B4
  52. #define EBU_WAIT_RDBY BIT(0)
  53. #define EBU_WAIT_WR_C BIT(3)
  54. #define HSNAND_CTL1 0x110
  55. #define HSNAND_CTL1_ADDR_SHIFT 24
  56. #define HSNAND_CTL2 0x114
  57. #define HSNAND_CTL2_ADDR_SHIFT 8
  58. #define HSNAND_CTL2_CYC_N_V5 (0x2 << 16)
  59. #define HSNAND_INT_MSK_CTL 0x124
  60. #define HSNAND_INT_MSK_CTL_WR_C BIT(4)
  61. #define HSNAND_INT_STA 0x128
  62. #define HSNAND_INT_STA_WR_C BIT(4)
  63. #define HSNAND_CTL 0x130
  64. #define HSNAND_CTL_ENABLE_ECC BIT(0)
  65. #define HSNAND_CTL_GO BIT(2)
  66. #define HSNAND_CTL_CE_SEL_CS(n) BIT(3 + (n))
  67. #define HSNAND_CTL_RW_READ 0x0
  68. #define HSNAND_CTL_RW_WRITE BIT(10)
  69. #define HSNAND_CTL_ECC_OFF_V8TH BIT(11)
  70. #define HSNAND_CTL_CKFF_EN 0x0
  71. #define HSNAND_CTL_MSG_EN BIT(17)
  72. #define HSNAND_PARA0 0x13c
  73. #define HSNAND_PARA0_PAGE_V8192 0x3
  74. #define HSNAND_PARA0_PIB_V256 (0x3 << 4)
  75. #define HSNAND_PARA0_BYP_EN_NP 0x0
  76. #define HSNAND_PARA0_BYP_DEC_NP 0x0
  77. #define HSNAND_PARA0_TYPE_ONFI BIT(18)
  78. #define HSNAND_PARA0_ADEP_EN BIT(21)
  79. #define HSNAND_CMSG_0 0x150
  80. #define HSNAND_CMSG_1 0x154
  81. #define HSNAND_ALE_OFFS BIT(2)
  82. #define HSNAND_CLE_OFFS BIT(3)
  83. #define HSNAND_CS_OFFS BIT(4)
  84. #define HSNAND_ECC_OFFSET 0x008
  85. #define MAX_CS 2
  86. #define USEC_PER_SEC 1000000L
  87. struct ebu_nand_cs {
  88. void __iomem *chipaddr;
  89. u32 addr_sel;
  90. };
  91. struct ebu_nand_controller {
  92. struct nand_controller controller;
  93. struct nand_chip chip;
  94. struct device *dev;
  95. void __iomem *ebu;
  96. void __iomem *hsnand;
  97. struct dma_chan *dma_tx;
  98. struct dma_chan *dma_rx;
  99. struct completion dma_access_complete;
  100. struct clk *clk;
  101. u32 nd_para0;
  102. u8 cs_num;
  103. struct ebu_nand_cs cs[MAX_CS];
  104. };
  105. static inline struct ebu_nand_controller *nand_to_ebu(struct nand_chip *chip)
  106. {
  107. return container_of(chip, struct ebu_nand_controller, chip);
  108. }
  109. static int ebu_nand_waitrdy(struct nand_chip *chip, int timeout_ms)
  110. {
  111. struct ebu_nand_controller *ctrl = nand_to_ebu(chip);
  112. u32 status;
  113. return readl_poll_timeout(ctrl->ebu + EBU_WAIT, status,
  114. (status & EBU_WAIT_RDBY) ||
  115. (status & EBU_WAIT_WR_C), 20, timeout_ms);
  116. }
  117. static u8 ebu_nand_readb(struct nand_chip *chip)
  118. {
  119. struct ebu_nand_controller *ebu_host = nand_get_controller_data(chip);
  120. u8 cs_num = ebu_host->cs_num;
  121. u8 val;
  122. val = readb(ebu_host->cs[cs_num].chipaddr + HSNAND_CS_OFFS);
  123. ebu_nand_waitrdy(chip, 1000);
  124. return val;
  125. }
  126. static void ebu_nand_writeb(struct nand_chip *chip, u32 offset, u8 value)
  127. {
  128. struct ebu_nand_controller *ebu_host = nand_get_controller_data(chip);
  129. u8 cs_num = ebu_host->cs_num;
  130. writeb(value, ebu_host->cs[cs_num].chipaddr + offset);
  131. ebu_nand_waitrdy(chip, 1000);
  132. }
  133. static void ebu_read_buf(struct nand_chip *chip, u_char *buf, unsigned int len)
  134. {
  135. int i;
  136. for (i = 0; i < len; i++)
  137. buf[i] = ebu_nand_readb(chip);
  138. }
  139. static void ebu_write_buf(struct nand_chip *chip, const u_char *buf, int len)
  140. {
  141. int i;
  142. for (i = 0; i < len; i++)
  143. ebu_nand_writeb(chip, HSNAND_CS_OFFS, buf[i]);
  144. }
  145. static void ebu_nand_disable(struct nand_chip *chip)
  146. {
  147. struct ebu_nand_controller *ebu_host = nand_get_controller_data(chip);
  148. writel(0, ebu_host->ebu + EBU_CON);
  149. }
  150. static void ebu_select_chip(struct nand_chip *chip)
  151. {
  152. struct ebu_nand_controller *ebu_host = nand_get_controller_data(chip);
  153. void __iomem *nand_con = ebu_host->ebu + EBU_CON;
  154. u32 cs = ebu_host->cs_num;
  155. writel(EBU_CON_NANDM_EN | EBU_CON_CSMUX_E_EN | EBU_CON_CS_P_LOW |
  156. EBU_CON_SE_P_LOW | EBU_CON_WP_P_LOW | EBU_CON_PRE_P_LOW |
  157. EBU_CON_IN_CS_S(cs) | EBU_CON_OUT_CS_S(cs) |
  158. EBU_CON_LAT_EN_CS_P, nand_con);
  159. }
  160. static int ebu_nand_set_timings(struct nand_chip *chip, int csline,
  161. const struct nand_interface_config *conf)
  162. {
  163. struct ebu_nand_controller *ctrl = nand_to_ebu(chip);
  164. unsigned int rate = clk_get_rate(ctrl->clk) / HZ_PER_MHZ;
  165. unsigned int period = DIV_ROUND_UP(USEC_PER_SEC, rate);
  166. const struct nand_sdr_timings *timings;
  167. u32 trecov, thold, twrwait, trdwait;
  168. u32 reg = 0;
  169. timings = nand_get_sdr_timings(conf);
  170. if (IS_ERR(timings))
  171. return PTR_ERR(timings);
  172. if (csline == NAND_DATA_IFACE_CHECK_ONLY)
  173. return 0;
  174. trecov = DIV_ROUND_UP(max(timings->tREA_max, timings->tREH_min),
  175. period);
  176. reg |= EBU_BUSCON_RECOVC(trecov);
  177. thold = DIV_ROUND_UP(max(timings->tDH_min, timings->tDS_min), period);
  178. reg |= EBU_BUSCON_HOLDC(thold);
  179. trdwait = DIV_ROUND_UP(max(timings->tRC_min, timings->tREH_min),
  180. period);
  181. reg |= EBU_BUSCON_WAITRDC(trdwait);
  182. twrwait = DIV_ROUND_UP(max(timings->tWC_min, timings->tWH_min), period);
  183. reg |= EBU_BUSCON_WAITWRC(twrwait);
  184. reg |= EBU_BUSCON_CMULT_V4 | EBU_BUSCON_BCGEN_CS | EBU_BUSCON_ALEC |
  185. EBU_BUSCON_SETUP_EN;
  186. writel(reg, ctrl->ebu + EBU_BUSCON(ctrl->cs_num));
  187. return 0;
  188. }
  189. static int ebu_nand_ooblayout_ecc(struct mtd_info *mtd, int section,
  190. struct mtd_oob_region *oobregion)
  191. {
  192. struct nand_chip *chip = mtd_to_nand(mtd);
  193. if (section)
  194. return -ERANGE;
  195. oobregion->offset = HSNAND_ECC_OFFSET;
  196. oobregion->length = chip->ecc.total;
  197. return 0;
  198. }
  199. static int ebu_nand_ooblayout_free(struct mtd_info *mtd, int section,
  200. struct mtd_oob_region *oobregion)
  201. {
  202. struct nand_chip *chip = mtd_to_nand(mtd);
  203. if (section)
  204. return -ERANGE;
  205. oobregion->offset = chip->ecc.total + HSNAND_ECC_OFFSET;
  206. oobregion->length = mtd->oobsize - oobregion->offset;
  207. return 0;
  208. }
  209. static const struct mtd_ooblayout_ops ebu_nand_ooblayout_ops = {
  210. .ecc = ebu_nand_ooblayout_ecc,
  211. .free = ebu_nand_ooblayout_free,
  212. };
  213. static void ebu_dma_rx_callback(void *cookie)
  214. {
  215. struct ebu_nand_controller *ebu_host = cookie;
  216. dmaengine_terminate_async(ebu_host->dma_rx);
  217. complete(&ebu_host->dma_access_complete);
  218. }
  219. static void ebu_dma_tx_callback(void *cookie)
  220. {
  221. struct ebu_nand_controller *ebu_host = cookie;
  222. dmaengine_terminate_async(ebu_host->dma_tx);
  223. complete(&ebu_host->dma_access_complete);
  224. }
  225. static int ebu_dma_start(struct ebu_nand_controller *ebu_host, u32 dir,
  226. const u8 *buf, u32 len)
  227. {
  228. struct dma_async_tx_descriptor *tx;
  229. struct completion *dma_completion;
  230. dma_async_tx_callback callback;
  231. struct dma_chan *chan;
  232. dma_cookie_t cookie;
  233. unsigned long flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
  234. dma_addr_t buf_dma;
  235. int ret;
  236. u32 timeout;
  237. if (dir == DMA_DEV_TO_MEM) {
  238. chan = ebu_host->dma_rx;
  239. dma_completion = &ebu_host->dma_access_complete;
  240. callback = ebu_dma_rx_callback;
  241. } else {
  242. chan = ebu_host->dma_tx;
  243. dma_completion = &ebu_host->dma_access_complete;
  244. callback = ebu_dma_tx_callback;
  245. }
  246. buf_dma = dma_map_single(chan->device->dev, (void *)buf, len, dir);
  247. if (dma_mapping_error(chan->device->dev, buf_dma)) {
  248. dev_err(ebu_host->dev, "Failed to map DMA buffer\n");
  249. ret = -EIO;
  250. goto err_unmap;
  251. }
  252. tx = dmaengine_prep_slave_single(chan, buf_dma, len, dir, flags);
  253. if (!tx) {
  254. ret = -ENXIO;
  255. goto err_unmap;
  256. }
  257. tx->callback = callback;
  258. tx->callback_param = ebu_host;
  259. cookie = tx->tx_submit(tx);
  260. ret = dma_submit_error(cookie);
  261. if (ret) {
  262. dev_err(ebu_host->dev, "dma_submit_error %d\n", cookie);
  263. ret = -EIO;
  264. goto err_unmap;
  265. }
  266. init_completion(dma_completion);
  267. dma_async_issue_pending(chan);
  268. /* Wait DMA to finish the data transfer.*/
  269. timeout = wait_for_completion_timeout(dma_completion, msecs_to_jiffies(1000));
  270. if (!timeout) {
  271. dev_err(ebu_host->dev, "I/O Error in DMA RX (status %d)\n",
  272. dmaengine_tx_status(chan, cookie, NULL));
  273. dmaengine_terminate_sync(chan);
  274. ret = -ETIMEDOUT;
  275. goto err_unmap;
  276. }
  277. return 0;
  278. err_unmap:
  279. dma_unmap_single(ebu_host->dev, buf_dma, len, dir);
  280. return ret;
  281. }
  282. static void ebu_nand_trigger(struct ebu_nand_controller *ebu_host,
  283. int page, u32 cmd)
  284. {
  285. unsigned int val;
  286. val = cmd | (page & 0xFF) << HSNAND_CTL1_ADDR_SHIFT;
  287. writel(val, ebu_host->hsnand + HSNAND_CTL1);
  288. val = (page & 0xFFFF00) >> 8 | HSNAND_CTL2_CYC_N_V5;
  289. writel(val, ebu_host->hsnand + HSNAND_CTL2);
  290. writel(ebu_host->nd_para0, ebu_host->hsnand + HSNAND_PARA0);
  291. /* clear first, will update later */
  292. writel(0xFFFFFFFF, ebu_host->hsnand + HSNAND_CMSG_0);
  293. writel(0xFFFFFFFF, ebu_host->hsnand + HSNAND_CMSG_1);
  294. writel(HSNAND_INT_MSK_CTL_WR_C,
  295. ebu_host->hsnand + HSNAND_INT_MSK_CTL);
  296. if (!cmd)
  297. val = HSNAND_CTL_RW_READ;
  298. else
  299. val = HSNAND_CTL_RW_WRITE;
  300. writel(HSNAND_CTL_MSG_EN | HSNAND_CTL_CKFF_EN |
  301. HSNAND_CTL_ECC_OFF_V8TH | HSNAND_CTL_CE_SEL_CS(ebu_host->cs_num) |
  302. HSNAND_CTL_ENABLE_ECC | HSNAND_CTL_GO | val,
  303. ebu_host->hsnand + HSNAND_CTL);
  304. }
  305. static int ebu_nand_read_page_hwecc(struct nand_chip *chip, u8 *buf,
  306. int oob_required, int page)
  307. {
  308. struct mtd_info *mtd = nand_to_mtd(chip);
  309. struct ebu_nand_controller *ebu_host = nand_get_controller_data(chip);
  310. int ret, reg_data;
  311. ebu_nand_trigger(ebu_host, page, NAND_CMD_READ0);
  312. ret = ebu_dma_start(ebu_host, DMA_DEV_TO_MEM, buf, mtd->writesize);
  313. if (ret)
  314. return ret;
  315. if (oob_required)
  316. chip->ecc.read_oob(chip, page);
  317. reg_data = readl(ebu_host->hsnand + HSNAND_CTL);
  318. reg_data &= ~HSNAND_CTL_GO;
  319. writel(reg_data, ebu_host->hsnand + HSNAND_CTL);
  320. return 0;
  321. }
  322. static int ebu_nand_write_page_hwecc(struct nand_chip *chip, const u8 *buf,
  323. int oob_required, int page)
  324. {
  325. struct mtd_info *mtd = nand_to_mtd(chip);
  326. struct ebu_nand_controller *ebu_host = nand_get_controller_data(chip);
  327. void __iomem *int_sta = ebu_host->hsnand + HSNAND_INT_STA;
  328. int reg_data, ret, val;
  329. u32 reg;
  330. ebu_nand_trigger(ebu_host, page, NAND_CMD_SEQIN);
  331. ret = ebu_dma_start(ebu_host, DMA_MEM_TO_DEV, buf, mtd->writesize);
  332. if (ret)
  333. return ret;
  334. if (oob_required) {
  335. reg = get_unaligned_le32(chip->oob_poi);
  336. writel(reg, ebu_host->hsnand + HSNAND_CMSG_0);
  337. reg = get_unaligned_le32(chip->oob_poi + 4);
  338. writel(reg, ebu_host->hsnand + HSNAND_CMSG_1);
  339. }
  340. ret = readl_poll_timeout_atomic(int_sta, val, !(val & HSNAND_INT_STA_WR_C),
  341. 10, 1000);
  342. if (ret)
  343. return ret;
  344. reg_data = readl(ebu_host->hsnand + HSNAND_CTL);
  345. reg_data &= ~HSNAND_CTL_GO;
  346. writel(reg_data, ebu_host->hsnand + HSNAND_CTL);
  347. return 0;
  348. }
  349. static const u8 ecc_strength[] = { 1, 1, 4, 8, 24, 32, 40, 60, };
  350. static int ebu_nand_attach_chip(struct nand_chip *chip)
  351. {
  352. struct mtd_info *mtd = nand_to_mtd(chip);
  353. struct ebu_nand_controller *ebu_host = nand_get_controller_data(chip);
  354. u32 ecc_steps, ecc_bytes, ecc_total, pagesize, pg_per_blk;
  355. u32 ecc_strength_ds = chip->ecc.strength;
  356. u32 ecc_size = chip->ecc.size;
  357. u32 writesize = mtd->writesize;
  358. u32 blocksize = mtd->erasesize;
  359. int bch_algo, start, val;
  360. /* Default to an ECC size of 512 */
  361. if (!chip->ecc.size)
  362. chip->ecc.size = 512;
  363. switch (ecc_size) {
  364. case 512:
  365. start = 1;
  366. if (!ecc_strength_ds)
  367. ecc_strength_ds = 4;
  368. break;
  369. case 1024:
  370. start = 4;
  371. if (!ecc_strength_ds)
  372. ecc_strength_ds = 32;
  373. break;
  374. default:
  375. return -EINVAL;
  376. }
  377. /* BCH ECC algorithm Settings for number of bits per 512B/1024B */
  378. bch_algo = round_up(start + 1, 4);
  379. for (val = start; val < bch_algo; val++) {
  380. if (ecc_strength_ds == ecc_strength[val])
  381. break;
  382. }
  383. if (val == bch_algo)
  384. return -EINVAL;
  385. if (ecc_strength_ds == 8)
  386. ecc_bytes = 14;
  387. else
  388. ecc_bytes = DIV_ROUND_UP(ecc_strength_ds * fls(8 * ecc_size), 8);
  389. ecc_steps = writesize / ecc_size;
  390. ecc_total = ecc_steps * ecc_bytes;
  391. if ((ecc_total + 8) > mtd->oobsize)
  392. return -ERANGE;
  393. chip->ecc.total = ecc_total;
  394. pagesize = fls(writesize >> 11);
  395. if (pagesize > HSNAND_PARA0_PAGE_V8192)
  396. return -ERANGE;
  397. pg_per_blk = fls((blocksize / writesize) >> 6) / 8;
  398. if (pg_per_blk > HSNAND_PARA0_PIB_V256)
  399. return -ERANGE;
  400. ebu_host->nd_para0 = pagesize | pg_per_blk | HSNAND_PARA0_BYP_EN_NP |
  401. HSNAND_PARA0_BYP_DEC_NP | HSNAND_PARA0_ADEP_EN |
  402. HSNAND_PARA0_TYPE_ONFI | (val << 29);
  403. mtd_set_ooblayout(mtd, &ebu_nand_ooblayout_ops);
  404. chip->ecc.read_page = ebu_nand_read_page_hwecc;
  405. chip->ecc.write_page = ebu_nand_write_page_hwecc;
  406. return 0;
  407. }
  408. static int ebu_nand_exec_op(struct nand_chip *chip,
  409. const struct nand_operation *op, bool check_only)
  410. {
  411. const struct nand_op_instr *instr = NULL;
  412. unsigned int op_id;
  413. int i, timeout_ms, ret = 0;
  414. if (check_only)
  415. return 0;
  416. ebu_select_chip(chip);
  417. for (op_id = 0; op_id < op->ninstrs; op_id++) {
  418. instr = &op->instrs[op_id];
  419. switch (instr->type) {
  420. case NAND_OP_CMD_INSTR:
  421. ebu_nand_writeb(chip, HSNAND_CLE_OFFS | HSNAND_CS_OFFS,
  422. instr->ctx.cmd.opcode);
  423. break;
  424. case NAND_OP_ADDR_INSTR:
  425. for (i = 0; i < instr->ctx.addr.naddrs; i++)
  426. ebu_nand_writeb(chip,
  427. HSNAND_ALE_OFFS | HSNAND_CS_OFFS,
  428. instr->ctx.addr.addrs[i]);
  429. break;
  430. case NAND_OP_DATA_IN_INSTR:
  431. ebu_read_buf(chip, instr->ctx.data.buf.in,
  432. instr->ctx.data.len);
  433. break;
  434. case NAND_OP_DATA_OUT_INSTR:
  435. ebu_write_buf(chip, instr->ctx.data.buf.out,
  436. instr->ctx.data.len);
  437. break;
  438. case NAND_OP_WAITRDY_INSTR:
  439. timeout_ms = instr->ctx.waitrdy.timeout_ms * 1000;
  440. ret = ebu_nand_waitrdy(chip, timeout_ms);
  441. break;
  442. }
  443. }
  444. return ret;
  445. }
  446. static const struct nand_controller_ops ebu_nand_controller_ops = {
  447. .attach_chip = ebu_nand_attach_chip,
  448. .setup_interface = ebu_nand_set_timings,
  449. .exec_op = ebu_nand_exec_op,
  450. };
  451. static void ebu_dma_cleanup(struct ebu_nand_controller *ebu_host)
  452. {
  453. if (ebu_host->dma_rx)
  454. dma_release_channel(ebu_host->dma_rx);
  455. if (ebu_host->dma_tx)
  456. dma_release_channel(ebu_host->dma_tx);
  457. }
  458. static int ebu_nand_probe(struct platform_device *pdev)
  459. {
  460. struct device *dev = &pdev->dev;
  461. struct ebu_nand_controller *ebu_host;
  462. struct device_node *chip_np;
  463. struct nand_chip *nand;
  464. struct mtd_info *mtd;
  465. struct resource *res;
  466. char *resname;
  467. int ret;
  468. u32 cs;
  469. ebu_host = devm_kzalloc(dev, sizeof(*ebu_host), GFP_KERNEL);
  470. if (!ebu_host)
  471. return -ENOMEM;
  472. ebu_host->dev = dev;
  473. nand_controller_init(&ebu_host->controller);
  474. ebu_host->ebu = devm_platform_ioremap_resource_byname(pdev, "ebunand");
  475. if (IS_ERR(ebu_host->ebu))
  476. return PTR_ERR(ebu_host->ebu);
  477. ebu_host->hsnand = devm_platform_ioremap_resource_byname(pdev, "hsnand");
  478. if (IS_ERR(ebu_host->hsnand))
  479. return PTR_ERR(ebu_host->hsnand);
  480. chip_np = of_get_next_child(dev->of_node, NULL);
  481. if (!chip_np)
  482. return dev_err_probe(dev, -EINVAL,
  483. "Could not find child node for the NAND chip\n");
  484. ret = of_property_read_u32(chip_np, "reg", &cs);
  485. if (ret) {
  486. dev_err(dev, "failed to get chip select: %d\n", ret);
  487. goto err_of_node_put;
  488. }
  489. if (cs >= MAX_CS) {
  490. dev_err(dev, "got invalid chip select: %d\n", cs);
  491. ret = -EINVAL;
  492. goto err_of_node_put;
  493. }
  494. ebu_host->cs_num = cs;
  495. resname = devm_kasprintf(dev, GFP_KERNEL, "nand_cs%d", cs);
  496. if (!resname) {
  497. ret = -ENOMEM;
  498. goto err_of_node_put;
  499. }
  500. ebu_host->cs[cs].chipaddr = devm_platform_ioremap_resource_byname(pdev,
  501. resname);
  502. if (IS_ERR(ebu_host->cs[cs].chipaddr)) {
  503. ret = PTR_ERR(ebu_host->cs[cs].chipaddr);
  504. goto err_of_node_put;
  505. }
  506. ebu_host->clk = devm_clk_get(dev, NULL);
  507. if (IS_ERR(ebu_host->clk)) {
  508. ret = dev_err_probe(dev, PTR_ERR(ebu_host->clk),
  509. "failed to get clock\n");
  510. goto err_of_node_put;
  511. }
  512. ret = clk_prepare_enable(ebu_host->clk);
  513. if (ret) {
  514. dev_err(dev, "failed to enable clock: %d\n", ret);
  515. goto err_of_node_put;
  516. }
  517. ebu_host->dma_tx = dma_request_chan(dev, "tx");
  518. if (IS_ERR(ebu_host->dma_tx)) {
  519. ret = dev_err_probe(dev, PTR_ERR(ebu_host->dma_tx),
  520. "failed to request DMA tx chan!.\n");
  521. goto err_disable_unprepare_clk;
  522. }
  523. ebu_host->dma_rx = dma_request_chan(dev, "rx");
  524. if (IS_ERR(ebu_host->dma_rx)) {
  525. ret = dev_err_probe(dev, PTR_ERR(ebu_host->dma_rx),
  526. "failed to request DMA rx chan!.\n");
  527. ebu_host->dma_rx = NULL;
  528. goto err_cleanup_dma;
  529. }
  530. resname = devm_kasprintf(dev, GFP_KERNEL, "addr_sel%d", cs);
  531. if (!resname) {
  532. ret = -ENOMEM;
  533. goto err_cleanup_dma;
  534. }
  535. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, resname);
  536. if (!res) {
  537. ret = -EINVAL;
  538. goto err_cleanup_dma;
  539. }
  540. ebu_host->cs[cs].addr_sel = res->start;
  541. writel(ebu_host->cs[cs].addr_sel | EBU_ADDR_MASK(5) | EBU_ADDR_SEL_REGEN,
  542. ebu_host->ebu + EBU_ADDR_SEL(cs));
  543. nand_set_flash_node(&ebu_host->chip, chip_np);
  544. mtd = nand_to_mtd(&ebu_host->chip);
  545. if (!mtd->name) {
  546. dev_err(ebu_host->dev, "NAND label property is mandatory\n");
  547. ret = -EINVAL;
  548. goto err_cleanup_dma;
  549. }
  550. mtd->dev.parent = dev;
  551. ebu_host->dev = dev;
  552. platform_set_drvdata(pdev, ebu_host);
  553. nand_set_controller_data(&ebu_host->chip, ebu_host);
  554. nand = &ebu_host->chip;
  555. nand->controller = &ebu_host->controller;
  556. nand->controller->ops = &ebu_nand_controller_ops;
  557. /* Scan to find existence of the device */
  558. ret = nand_scan(&ebu_host->chip, 1);
  559. if (ret)
  560. goto err_cleanup_dma;
  561. ret = mtd_device_register(mtd, NULL, 0);
  562. if (ret)
  563. goto err_clean_nand;
  564. return 0;
  565. err_clean_nand:
  566. nand_cleanup(&ebu_host->chip);
  567. err_cleanup_dma:
  568. ebu_dma_cleanup(ebu_host);
  569. err_disable_unprepare_clk:
  570. clk_disable_unprepare(ebu_host->clk);
  571. err_of_node_put:
  572. of_node_put(chip_np);
  573. return ret;
  574. }
  575. static int ebu_nand_remove(struct platform_device *pdev)
  576. {
  577. struct ebu_nand_controller *ebu_host = platform_get_drvdata(pdev);
  578. int ret;
  579. ret = mtd_device_unregister(nand_to_mtd(&ebu_host->chip));
  580. WARN_ON(ret);
  581. nand_cleanup(&ebu_host->chip);
  582. ebu_nand_disable(&ebu_host->chip);
  583. ebu_dma_cleanup(ebu_host);
  584. clk_disable_unprepare(ebu_host->clk);
  585. return 0;
  586. }
  587. static const struct of_device_id ebu_nand_match[] = {
  588. { .compatible = "intel,lgm-ebunand" },
  589. {}
  590. };
  591. MODULE_DEVICE_TABLE(of, ebu_nand_match);
  592. static struct platform_driver ebu_nand_driver = {
  593. .probe = ebu_nand_probe,
  594. .remove = ebu_nand_remove,
  595. .driver = {
  596. .name = "intel-nand-controller",
  597. .of_match_table = ebu_nand_match,
  598. },
  599. };
  600. module_platform_driver(ebu_nand_driver);
  601. MODULE_LICENSE("GPL v2");
  602. MODULE_AUTHOR("Vadivel Murugan R <[email protected]>");
  603. MODULE_DESCRIPTION("Intel's LGM External Bus NAND Controller driver");