gpmi-lib.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Freescale GPMI NAND Flash Driver
  4. *
  5. * Copyright (C) 2008-2011 Freescale Semiconductor, Inc.
  6. * Copyright (C) 2008 Embedded Alley Solutions, Inc.
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/clk.h>
  10. #include <linux/slab.h>
  11. #include "gpmi-nand.h"
  12. #include "gpmi-regs.h"
  13. #include "bch-regs.h"
  14. /* Converts time to clock cycles */
  15. #define TO_CYCLES(duration, period) DIV_ROUND_UP_ULL(duration, period)
  16. #define MXS_SET_ADDR 0x4
  17. #define MXS_CLR_ADDR 0x8
  18. /*
  19. * Clear the bit and poll it cleared. This is usually called with
  20. * a reset address and mask being either SFTRST(bit 31) or CLKGATE
  21. * (bit 30).
  22. */
  23. static int clear_poll_bit(void __iomem *addr, u32 mask)
  24. {
  25. int timeout = 0x400;
  26. /* clear the bit */
  27. writel(mask, addr + MXS_CLR_ADDR);
  28. /*
  29. * SFTRST needs 3 GPMI clocks to settle, the reference manual
  30. * recommends to wait 1us.
  31. */
  32. udelay(1);
  33. /* poll the bit becoming clear */
  34. while ((readl(addr) & mask) && --timeout)
  35. /* nothing */;
  36. return !timeout;
  37. }
  38. #define MODULE_CLKGATE (1 << 30)
  39. #define MODULE_SFTRST (1 << 31)
  40. /*
  41. * The current mxs_reset_block() will do two things:
  42. * [1] enable the module.
  43. * [2] reset the module.
  44. *
  45. * In most of the cases, it's ok.
  46. * But in MX23, there is a hardware bug in the BCH block (see erratum #2847).
  47. * If you try to soft reset the BCH block, it becomes unusable until
  48. * the next hard reset. This case occurs in the NAND boot mode. When the board
  49. * boots by NAND, the ROM of the chip will initialize the BCH blocks itself.
  50. * So If the driver tries to reset the BCH again, the BCH will not work anymore.
  51. * You will see a DMA timeout in this case. The bug has been fixed
  52. * in the following chips, such as MX28.
  53. *
  54. * To avoid this bug, just add a new parameter `just_enable` for
  55. * the mxs_reset_block(), and rewrite it here.
  56. */
  57. static int gpmi_reset_block(void __iomem *reset_addr, bool just_enable)
  58. {
  59. int ret;
  60. int timeout = 0x400;
  61. /* clear and poll SFTRST */
  62. ret = clear_poll_bit(reset_addr, MODULE_SFTRST);
  63. if (unlikely(ret))
  64. goto error;
  65. /* clear CLKGATE */
  66. writel(MODULE_CLKGATE, reset_addr + MXS_CLR_ADDR);
  67. if (!just_enable) {
  68. /* set SFTRST to reset the block */
  69. writel(MODULE_SFTRST, reset_addr + MXS_SET_ADDR);
  70. udelay(1);
  71. /* poll CLKGATE becoming set */
  72. while ((!(readl(reset_addr) & MODULE_CLKGATE)) && --timeout)
  73. /* nothing */;
  74. if (unlikely(!timeout))
  75. goto error;
  76. }
  77. /* clear and poll SFTRST */
  78. ret = clear_poll_bit(reset_addr, MODULE_SFTRST);
  79. if (unlikely(ret))
  80. goto error;
  81. /* clear and poll CLKGATE */
  82. ret = clear_poll_bit(reset_addr, MODULE_CLKGATE);
  83. if (unlikely(ret))
  84. goto error;
  85. return 0;
  86. error:
  87. pr_err("%s(%p): module reset timeout\n", __func__, reset_addr);
  88. return -ETIMEDOUT;
  89. }
  90. static int __gpmi_enable_clk(struct gpmi_nand_data *this, bool v)
  91. {
  92. struct clk *clk;
  93. int ret;
  94. int i;
  95. for (i = 0; i < GPMI_CLK_MAX; i++) {
  96. clk = this->resources.clock[i];
  97. if (!clk)
  98. break;
  99. if (v) {
  100. ret = clk_prepare_enable(clk);
  101. if (ret)
  102. goto err_clk;
  103. } else {
  104. clk_disable_unprepare(clk);
  105. }
  106. }
  107. return 0;
  108. err_clk:
  109. for (; i > 0; i--)
  110. clk_disable_unprepare(this->resources.clock[i - 1]);
  111. return ret;
  112. }
  113. int gpmi_enable_clk(struct gpmi_nand_data *this)
  114. {
  115. return __gpmi_enable_clk(this, true);
  116. }
  117. int gpmi_disable_clk(struct gpmi_nand_data *this)
  118. {
  119. return __gpmi_enable_clk(this, false);
  120. }
  121. int gpmi_init(struct gpmi_nand_data *this)
  122. {
  123. struct resources *r = &this->resources;
  124. int ret;
  125. ret = gpmi_enable_clk(this);
  126. if (ret)
  127. return ret;
  128. ret = gpmi_reset_block(r->gpmi_regs, false);
  129. if (ret)
  130. goto err_out;
  131. /*
  132. * Reset BCH here, too. We got failures otherwise :(
  133. * See later BCH reset for explanation of MX23 and MX28 handling
  134. */
  135. ret = gpmi_reset_block(r->bch_regs,
  136. GPMI_IS_MX23(this) || GPMI_IS_MX28(this));
  137. if (ret)
  138. goto err_out;
  139. /* Choose NAND mode. */
  140. writel(BM_GPMI_CTRL1_GPMI_MODE, r->gpmi_regs + HW_GPMI_CTRL1_CLR);
  141. /* Set the IRQ polarity. */
  142. writel(BM_GPMI_CTRL1_ATA_IRQRDY_POLARITY,
  143. r->gpmi_regs + HW_GPMI_CTRL1_SET);
  144. /* Disable Write-Protection. */
  145. writel(BM_GPMI_CTRL1_DEV_RESET, r->gpmi_regs + HW_GPMI_CTRL1_SET);
  146. /* Select BCH ECC. */
  147. writel(BM_GPMI_CTRL1_BCH_MODE, r->gpmi_regs + HW_GPMI_CTRL1_SET);
  148. /*
  149. * Decouple the chip select from dma channel. We use dma0 for all
  150. * the chips.
  151. */
  152. writel(BM_GPMI_CTRL1_DECOUPLE_CS, r->gpmi_regs + HW_GPMI_CTRL1_SET);
  153. gpmi_disable_clk(this);
  154. return 0;
  155. err_out:
  156. gpmi_disable_clk(this);
  157. return ret;
  158. }
  159. /* This function is very useful. It is called only when the bug occur. */
  160. void gpmi_dump_info(struct gpmi_nand_data *this)
  161. {
  162. struct resources *r = &this->resources;
  163. struct bch_geometry *geo = &this->bch_geometry;
  164. u32 reg;
  165. int i;
  166. dev_err(this->dev, "Show GPMI registers :\n");
  167. for (i = 0; i <= HW_GPMI_DEBUG / 0x10 + 1; i++) {
  168. reg = readl(r->gpmi_regs + i * 0x10);
  169. dev_err(this->dev, "offset 0x%.3x : 0x%.8x\n", i * 0x10, reg);
  170. }
  171. /* start to print out the BCH info */
  172. dev_err(this->dev, "Show BCH registers :\n");
  173. for (i = 0; i <= HW_BCH_VERSION / 0x10 + 1; i++) {
  174. reg = readl(r->bch_regs + i * 0x10);
  175. dev_err(this->dev, "offset 0x%.3x : 0x%.8x\n", i * 0x10, reg);
  176. }
  177. dev_err(this->dev, "BCH Geometry :\n"
  178. "GF length : %u\n"
  179. "ECC Strength : %u\n"
  180. "Page Size in Bytes : %u\n"
  181. "Metadata Size in Bytes : %u\n"
  182. "ECC Chunk Size in Bytes: %u\n"
  183. "ECC Chunk Count : %u\n"
  184. "Payload Size in Bytes : %u\n"
  185. "Auxiliary Size in Bytes: %u\n"
  186. "Auxiliary Status Offset: %u\n"
  187. "Block Mark Byte Offset : %u\n"
  188. "Block Mark Bit Offset : %u\n",
  189. geo->gf_len,
  190. geo->ecc_strength,
  191. geo->page_size,
  192. geo->metadata_size,
  193. geo->ecc_chunk_size,
  194. geo->ecc_chunk_count,
  195. geo->payload_size,
  196. geo->auxiliary_size,
  197. geo->auxiliary_status_offset,
  198. geo->block_mark_byte_offset,
  199. geo->block_mark_bit_offset);
  200. }
  201. /* Configures the geometry for BCH. */
  202. int bch_set_geometry(struct gpmi_nand_data *this)
  203. {
  204. struct resources *r = &this->resources;
  205. struct bch_geometry *bch_geo = &this->bch_geometry;
  206. unsigned int block_count;
  207. unsigned int block_size;
  208. unsigned int metadata_size;
  209. unsigned int ecc_strength;
  210. unsigned int page_size;
  211. unsigned int gf_len;
  212. int ret;
  213. ret = common_nfc_set_geometry(this);
  214. if (ret)
  215. return ret;
  216. block_count = bch_geo->ecc_chunk_count - 1;
  217. block_size = bch_geo->ecc_chunk_size;
  218. metadata_size = bch_geo->metadata_size;
  219. ecc_strength = bch_geo->ecc_strength >> 1;
  220. page_size = bch_geo->page_size;
  221. gf_len = bch_geo->gf_len;
  222. ret = gpmi_enable_clk(this);
  223. if (ret)
  224. return ret;
  225. /*
  226. * Due to erratum #2847 of the MX23, the BCH cannot be soft reset on this
  227. * chip, otherwise it will lock up. So we skip resetting BCH on the MX23.
  228. * and MX28.
  229. */
  230. ret = gpmi_reset_block(r->bch_regs,
  231. GPMI_IS_MX23(this) || GPMI_IS_MX28(this));
  232. if (ret)
  233. goto err_out;
  234. /* Configure layout 0. */
  235. writel(BF_BCH_FLASH0LAYOUT0_NBLOCKS(block_count)
  236. | BF_BCH_FLASH0LAYOUT0_META_SIZE(metadata_size)
  237. | BF_BCH_FLASH0LAYOUT0_ECC0(ecc_strength, this)
  238. | BF_BCH_FLASH0LAYOUT0_GF(gf_len, this)
  239. | BF_BCH_FLASH0LAYOUT0_DATA0_SIZE(block_size, this),
  240. r->bch_regs + HW_BCH_FLASH0LAYOUT0);
  241. writel(BF_BCH_FLASH0LAYOUT1_PAGE_SIZE(page_size)
  242. | BF_BCH_FLASH0LAYOUT1_ECCN(ecc_strength, this)
  243. | BF_BCH_FLASH0LAYOUT1_GF(gf_len, this)
  244. | BF_BCH_FLASH0LAYOUT1_DATAN_SIZE(block_size, this),
  245. r->bch_regs + HW_BCH_FLASH0LAYOUT1);
  246. /* Set *all* chip selects to use layout 0. */
  247. writel(0, r->bch_regs + HW_BCH_LAYOUTSELECT);
  248. /* Enable interrupts. */
  249. writel(BM_BCH_CTRL_COMPLETE_IRQ_EN,
  250. r->bch_regs + HW_BCH_CTRL_SET);
  251. gpmi_disable_clk(this);
  252. return 0;
  253. err_out:
  254. gpmi_disable_clk(this);
  255. return ret;
  256. }
  257. /*
  258. * <1> Firstly, we should know what's the GPMI-clock means.
  259. * The GPMI-clock is the internal clock in the gpmi nand controller.
  260. * If you set 100MHz to gpmi nand controller, the GPMI-clock's period
  261. * is 10ns. Mark the GPMI-clock's period as GPMI-clock-period.
  262. *
  263. * <2> Secondly, we should know what's the frequency on the nand chip pins.
  264. * The frequency on the nand chip pins is derived from the GPMI-clock.
  265. * We can get it from the following equation:
  266. *
  267. * F = G / (DS + DH)
  268. *
  269. * F : the frequency on the nand chip pins.
  270. * G : the GPMI clock, such as 100MHz.
  271. * DS : GPMI_HW_GPMI_TIMING0:DATA_SETUP
  272. * DH : GPMI_HW_GPMI_TIMING0:DATA_HOLD
  273. *
  274. * <3> Thirdly, when the frequency on the nand chip pins is above 33MHz,
  275. * the nand EDO(extended Data Out) timing could be applied.
  276. * The GPMI implements a feedback read strobe to sample the read data.
  277. * The feedback read strobe can be delayed to support the nand EDO timing
  278. * where the read strobe may deasserts before the read data is valid, and
  279. * read data is valid for some time after read strobe.
  280. *
  281. * The following figure illustrates some aspects of a NAND Flash read:
  282. *
  283. * |<---tREA---->|
  284. * | |
  285. * | | |
  286. * |<--tRP-->| |
  287. * | | |
  288. * __ ___|__________________________________
  289. * RDN \________/ |
  290. * |
  291. * /---------\
  292. * Read Data --------------< >---------
  293. * \---------/
  294. * | |
  295. * |<-D->|
  296. * FeedbackRDN ________ ____________
  297. * \___________/
  298. *
  299. * D stands for delay, set in the HW_GPMI_CTRL1:RDN_DELAY.
  300. *
  301. *
  302. * <4> Now, we begin to describe how to compute the right RDN_DELAY.
  303. *
  304. * 4.1) From the aspect of the nand chip pins:
  305. * Delay = (tREA + C - tRP) {1}
  306. *
  307. * tREA : the maximum read access time.
  308. * C : a constant to adjust the delay. default is 4000ps.
  309. * tRP : the read pulse width, which is exactly:
  310. * tRP = (GPMI-clock-period) * DATA_SETUP
  311. *
  312. * 4.2) From the aspect of the GPMI nand controller:
  313. * Delay = RDN_DELAY * 0.125 * RP {2}
  314. *
  315. * RP : the DLL reference period.
  316. * if (GPMI-clock-period > DLL_THRETHOLD)
  317. * RP = GPMI-clock-period / 2;
  318. * else
  319. * RP = GPMI-clock-period;
  320. *
  321. * Set the HW_GPMI_CTRL1:HALF_PERIOD if GPMI-clock-period
  322. * is greater DLL_THRETHOLD. In other SOCs, the DLL_THRETHOLD
  323. * is 16000ps, but in mx6q, we use 12000ps.
  324. *
  325. * 4.3) since {1} equals {2}, we get:
  326. *
  327. * (tREA + 4000 - tRP) * 8
  328. * RDN_DELAY = ----------------------- {3}
  329. * RP
  330. */
  331. static void gpmi_nfc_compute_timings(struct gpmi_nand_data *this,
  332. const struct nand_sdr_timings *sdr)
  333. {
  334. struct gpmi_nfc_hardware_timing *hw = &this->hw;
  335. unsigned int dll_threshold_ps = this->devdata->max_chain_delay;
  336. unsigned int period_ps, reference_period_ps;
  337. unsigned int data_setup_cycles, data_hold_cycles, addr_setup_cycles;
  338. unsigned int tRP_ps;
  339. bool use_half_period;
  340. int sample_delay_ps, sample_delay_factor;
  341. u16 busy_timeout_cycles;
  342. u8 wrn_dly_sel;
  343. if (sdr->tRC_min >= 30000) {
  344. /* ONFI non-EDO modes [0-3] */
  345. hw->clk_rate = 22000000;
  346. wrn_dly_sel = BV_GPMI_CTRL1_WRN_DLY_SEL_4_TO_8NS;
  347. } else if (sdr->tRC_min >= 25000) {
  348. /* ONFI EDO mode 4 */
  349. hw->clk_rate = 80000000;
  350. wrn_dly_sel = BV_GPMI_CTRL1_WRN_DLY_SEL_NO_DELAY;
  351. } else {
  352. /* ONFI EDO mode 5 */
  353. hw->clk_rate = 100000000;
  354. wrn_dly_sel = BV_GPMI_CTRL1_WRN_DLY_SEL_NO_DELAY;
  355. }
  356. /* SDR core timings are given in picoseconds */
  357. period_ps = div_u64((u64)NSEC_PER_SEC * 1000, hw->clk_rate);
  358. addr_setup_cycles = TO_CYCLES(sdr->tALS_min, period_ps);
  359. data_setup_cycles = TO_CYCLES(sdr->tDS_min, period_ps);
  360. data_hold_cycles = TO_CYCLES(sdr->tDH_min, period_ps);
  361. busy_timeout_cycles = TO_CYCLES(sdr->tWB_max + sdr->tR_max, period_ps);
  362. hw->timing0 = BF_GPMI_TIMING0_ADDRESS_SETUP(addr_setup_cycles) |
  363. BF_GPMI_TIMING0_DATA_HOLD(data_hold_cycles) |
  364. BF_GPMI_TIMING0_DATA_SETUP(data_setup_cycles);
  365. hw->timing1 = BF_GPMI_TIMING1_BUSY_TIMEOUT(busy_timeout_cycles * 4096);
  366. /*
  367. * Derive NFC ideal delay from {3}:
  368. *
  369. * (tREA + 4000 - tRP) * 8
  370. * RDN_DELAY = -----------------------
  371. * RP
  372. */
  373. if (period_ps > dll_threshold_ps) {
  374. use_half_period = true;
  375. reference_period_ps = period_ps / 2;
  376. } else {
  377. use_half_period = false;
  378. reference_period_ps = period_ps;
  379. }
  380. tRP_ps = data_setup_cycles * period_ps;
  381. sample_delay_ps = (sdr->tREA_max + 4000 - tRP_ps) * 8;
  382. if (sample_delay_ps > 0)
  383. sample_delay_factor = sample_delay_ps / reference_period_ps;
  384. else
  385. sample_delay_factor = 0;
  386. hw->ctrl1n = BF_GPMI_CTRL1_WRN_DLY_SEL(wrn_dly_sel);
  387. if (sample_delay_factor)
  388. hw->ctrl1n |= BF_GPMI_CTRL1_RDN_DELAY(sample_delay_factor) |
  389. BM_GPMI_CTRL1_DLL_ENABLE |
  390. (use_half_period ? BM_GPMI_CTRL1_HALF_PERIOD : 0);
  391. }
  392. void gpmi_nfc_apply_timings(struct gpmi_nand_data *this)
  393. {
  394. struct gpmi_nfc_hardware_timing *hw = &this->hw;
  395. struct resources *r = &this->resources;
  396. void __iomem *gpmi_regs = r->gpmi_regs;
  397. unsigned int dll_wait_time_us;
  398. clk_set_rate(r->clock[0], hw->clk_rate);
  399. writel(hw->timing0, gpmi_regs + HW_GPMI_TIMING0);
  400. writel(hw->timing1, gpmi_regs + HW_GPMI_TIMING1);
  401. /*
  402. * Clear several CTRL1 fields, DLL must be disabled when setting
  403. * RDN_DELAY or HALF_PERIOD.
  404. */
  405. writel(BM_GPMI_CTRL1_CLEAR_MASK, gpmi_regs + HW_GPMI_CTRL1_CLR);
  406. writel(hw->ctrl1n, gpmi_regs + HW_GPMI_CTRL1_SET);
  407. /* Wait 64 clock cycles before using the GPMI after enabling the DLL */
  408. dll_wait_time_us = USEC_PER_SEC / hw->clk_rate * 64;
  409. if (!dll_wait_time_us)
  410. dll_wait_time_us = 1;
  411. /* Wait for the DLL to settle. */
  412. udelay(dll_wait_time_us);
  413. }
  414. int gpmi_setup_data_interface(struct mtd_info *mtd, int chipnr,
  415. const struct nand_data_interface *conf)
  416. {
  417. struct nand_chip *chip = mtd_to_nand(mtd);
  418. struct gpmi_nand_data *this = nand_get_controller_data(chip);
  419. const struct nand_sdr_timings *sdr;
  420. /* Retrieve required NAND timings */
  421. sdr = nand_get_sdr_timings(conf);
  422. if (IS_ERR(sdr))
  423. return PTR_ERR(sdr);
  424. /* Only MX6 GPMI controller can reach EDO timings */
  425. if (sdr->tRC_min <= 25000 && !GPMI_IS_MX6(this))
  426. return -ENOTSUPP;
  427. /* Stop here if this call was just a check */
  428. if (chipnr < 0)
  429. return 0;
  430. /* Do the actual derivation of the controller timings */
  431. gpmi_nfc_compute_timings(this, sdr);
  432. this->hw.must_apply_timings = true;
  433. return 0;
  434. }
  435. /* Clears a BCH interrupt. */
  436. void gpmi_clear_bch(struct gpmi_nand_data *this)
  437. {
  438. struct resources *r = &this->resources;
  439. writel(BM_BCH_CTRL_COMPLETE_IRQ, r->bch_regs + HW_BCH_CTRL_CLR);
  440. }
  441. /* Returns the Ready/Busy status of the given chip. */
  442. int gpmi_is_ready(struct gpmi_nand_data *this, unsigned chip)
  443. {
  444. struct resources *r = &this->resources;
  445. uint32_t mask = 0;
  446. uint32_t reg = 0;
  447. if (GPMI_IS_MX23(this)) {
  448. mask = MX23_BM_GPMI_DEBUG_READY0 << chip;
  449. reg = readl(r->gpmi_regs + HW_GPMI_DEBUG);
  450. } else if (GPMI_IS_MX28(this) || GPMI_IS_MX6(this)) {
  451. /*
  452. * In the imx6, all the ready/busy pins are bound
  453. * together. So we only need to check chip 0.
  454. */
  455. if (GPMI_IS_MX6(this))
  456. chip = 0;
  457. /* MX28 shares the same R/B register as MX6Q. */
  458. mask = MX28_BF_GPMI_STAT_READY_BUSY(1 << chip);
  459. reg = readl(r->gpmi_regs + HW_GPMI_STAT);
  460. } else
  461. dev_err(this->dev, "unknown arch.\n");
  462. return reg & mask;
  463. }
  464. int gpmi_send_command(struct gpmi_nand_data *this)
  465. {
  466. struct dma_chan *channel = get_dma_chan(this);
  467. struct dma_async_tx_descriptor *desc;
  468. struct scatterlist *sgl;
  469. int chip = this->current_chip;
  470. int ret;
  471. u32 pio[3];
  472. /* [1] send out the PIO words */
  473. pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__WRITE)
  474. | BM_GPMI_CTRL0_WORD_LENGTH
  475. | BF_GPMI_CTRL0_CS(chip, this)
  476. | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
  477. | BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_CLE)
  478. | BM_GPMI_CTRL0_ADDRESS_INCREMENT
  479. | BF_GPMI_CTRL0_XFER_COUNT(this->command_length);
  480. pio[1] = pio[2] = 0;
  481. desc = dmaengine_prep_slave_sg(channel,
  482. (struct scatterlist *)pio,
  483. ARRAY_SIZE(pio), DMA_TRANS_NONE, 0);
  484. if (!desc)
  485. return -EINVAL;
  486. /* [2] send out the COMMAND + ADDRESS string stored in @buffer */
  487. sgl = &this->cmd_sgl;
  488. sg_init_one(sgl, this->cmd_buffer, this->command_length);
  489. dma_map_sg(this->dev, sgl, 1, DMA_TO_DEVICE);
  490. desc = dmaengine_prep_slave_sg(channel,
  491. sgl, 1, DMA_MEM_TO_DEV,
  492. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  493. if (!desc)
  494. return -EINVAL;
  495. /* [3] submit the DMA */
  496. ret = start_dma_without_bch_irq(this, desc);
  497. dma_unmap_sg(this->dev, sgl, 1, DMA_TO_DEVICE);
  498. return ret;
  499. }
  500. int gpmi_send_data(struct gpmi_nand_data *this, const void *buf, int len)
  501. {
  502. struct dma_async_tx_descriptor *desc;
  503. struct dma_chan *channel = get_dma_chan(this);
  504. int chip = this->current_chip;
  505. int ret;
  506. uint32_t command_mode;
  507. uint32_t address;
  508. u32 pio[2];
  509. /* [1] PIO */
  510. command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WRITE;
  511. address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
  512. pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
  513. | BM_GPMI_CTRL0_WORD_LENGTH
  514. | BF_GPMI_CTRL0_CS(chip, this)
  515. | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
  516. | BF_GPMI_CTRL0_ADDRESS(address)
  517. | BF_GPMI_CTRL0_XFER_COUNT(len);
  518. pio[1] = 0;
  519. desc = dmaengine_prep_slave_sg(channel, (struct scatterlist *)pio,
  520. ARRAY_SIZE(pio), DMA_TRANS_NONE, 0);
  521. if (!desc)
  522. return -EINVAL;
  523. /* [2] send DMA request */
  524. prepare_data_dma(this, buf, len, DMA_TO_DEVICE);
  525. desc = dmaengine_prep_slave_sg(channel, &this->data_sgl,
  526. 1, DMA_MEM_TO_DEV,
  527. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  528. if (!desc)
  529. return -EINVAL;
  530. /* [3] submit the DMA */
  531. ret = start_dma_without_bch_irq(this, desc);
  532. dma_unmap_sg(this->dev, &this->data_sgl, 1, DMA_TO_DEVICE);
  533. return ret;
  534. }
  535. int gpmi_read_data(struct gpmi_nand_data *this, void *buf, int len)
  536. {
  537. struct dma_async_tx_descriptor *desc;
  538. struct dma_chan *channel = get_dma_chan(this);
  539. int chip = this->current_chip;
  540. int ret;
  541. u32 pio[2];
  542. bool direct;
  543. /* [1] : send PIO */
  544. pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__READ)
  545. | BM_GPMI_CTRL0_WORD_LENGTH
  546. | BF_GPMI_CTRL0_CS(chip, this)
  547. | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
  548. | BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_DATA)
  549. | BF_GPMI_CTRL0_XFER_COUNT(len);
  550. pio[1] = 0;
  551. desc = dmaengine_prep_slave_sg(channel,
  552. (struct scatterlist *)pio,
  553. ARRAY_SIZE(pio), DMA_TRANS_NONE, 0);
  554. if (!desc)
  555. return -EINVAL;
  556. /* [2] : send DMA request */
  557. direct = prepare_data_dma(this, buf, len, DMA_FROM_DEVICE);
  558. desc = dmaengine_prep_slave_sg(channel, &this->data_sgl,
  559. 1, DMA_DEV_TO_MEM,
  560. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  561. if (!desc)
  562. return -EINVAL;
  563. /* [3] : submit the DMA */
  564. ret = start_dma_without_bch_irq(this, desc);
  565. dma_unmap_sg(this->dev, &this->data_sgl, 1, DMA_FROM_DEVICE);
  566. if (!direct)
  567. memcpy(buf, this->data_buffer_dma, len);
  568. return ret;
  569. }
  570. int gpmi_send_page(struct gpmi_nand_data *this,
  571. dma_addr_t payload, dma_addr_t auxiliary)
  572. {
  573. struct bch_geometry *geo = &this->bch_geometry;
  574. uint32_t command_mode;
  575. uint32_t address;
  576. uint32_t ecc_command;
  577. uint32_t buffer_mask;
  578. struct dma_async_tx_descriptor *desc;
  579. struct dma_chan *channel = get_dma_chan(this);
  580. int chip = this->current_chip;
  581. u32 pio[6];
  582. /* A DMA descriptor that does an ECC page read. */
  583. command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WRITE;
  584. address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
  585. ecc_command = BV_GPMI_ECCCTRL_ECC_CMD__BCH_ENCODE;
  586. buffer_mask = BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_PAGE |
  587. BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_AUXONLY;
  588. pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
  589. | BM_GPMI_CTRL0_WORD_LENGTH
  590. | BF_GPMI_CTRL0_CS(chip, this)
  591. | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
  592. | BF_GPMI_CTRL0_ADDRESS(address)
  593. | BF_GPMI_CTRL0_XFER_COUNT(0);
  594. pio[1] = 0;
  595. pio[2] = BM_GPMI_ECCCTRL_ENABLE_ECC
  596. | BF_GPMI_ECCCTRL_ECC_CMD(ecc_command)
  597. | BF_GPMI_ECCCTRL_BUFFER_MASK(buffer_mask);
  598. pio[3] = geo->page_size;
  599. pio[4] = payload;
  600. pio[5] = auxiliary;
  601. desc = dmaengine_prep_slave_sg(channel,
  602. (struct scatterlist *)pio,
  603. ARRAY_SIZE(pio), DMA_TRANS_NONE,
  604. DMA_CTRL_ACK);
  605. if (!desc)
  606. return -EINVAL;
  607. return start_dma_with_bch_irq(this, desc);
  608. }
  609. int gpmi_read_page(struct gpmi_nand_data *this,
  610. dma_addr_t payload, dma_addr_t auxiliary)
  611. {
  612. struct bch_geometry *geo = &this->bch_geometry;
  613. uint32_t command_mode;
  614. uint32_t address;
  615. uint32_t ecc_command;
  616. uint32_t buffer_mask;
  617. struct dma_async_tx_descriptor *desc;
  618. struct dma_chan *channel = get_dma_chan(this);
  619. int chip = this->current_chip;
  620. u32 pio[6];
  621. /* [1] Wait for the chip to report ready. */
  622. command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WAIT_FOR_READY;
  623. address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
  624. pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
  625. | BM_GPMI_CTRL0_WORD_LENGTH
  626. | BF_GPMI_CTRL0_CS(chip, this)
  627. | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
  628. | BF_GPMI_CTRL0_ADDRESS(address)
  629. | BF_GPMI_CTRL0_XFER_COUNT(0);
  630. pio[1] = 0;
  631. desc = dmaengine_prep_slave_sg(channel,
  632. (struct scatterlist *)pio, 2,
  633. DMA_TRANS_NONE, 0);
  634. if (!desc)
  635. return -EINVAL;
  636. /* [2] Enable the BCH block and read. */
  637. command_mode = BV_GPMI_CTRL0_COMMAND_MODE__READ;
  638. address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
  639. ecc_command = BV_GPMI_ECCCTRL_ECC_CMD__BCH_DECODE;
  640. buffer_mask = BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_PAGE
  641. | BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_AUXONLY;
  642. pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
  643. | BM_GPMI_CTRL0_WORD_LENGTH
  644. | BF_GPMI_CTRL0_CS(chip, this)
  645. | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
  646. | BF_GPMI_CTRL0_ADDRESS(address)
  647. | BF_GPMI_CTRL0_XFER_COUNT(geo->page_size);
  648. pio[1] = 0;
  649. pio[2] = BM_GPMI_ECCCTRL_ENABLE_ECC
  650. | BF_GPMI_ECCCTRL_ECC_CMD(ecc_command)
  651. | BF_GPMI_ECCCTRL_BUFFER_MASK(buffer_mask);
  652. pio[3] = geo->page_size;
  653. pio[4] = payload;
  654. pio[5] = auxiliary;
  655. desc = dmaengine_prep_slave_sg(channel,
  656. (struct scatterlist *)pio,
  657. ARRAY_SIZE(pio), DMA_TRANS_NONE,
  658. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  659. if (!desc)
  660. return -EINVAL;
  661. /* [3] Disable the BCH block */
  662. command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WAIT_FOR_READY;
  663. address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
  664. pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
  665. | BM_GPMI_CTRL0_WORD_LENGTH
  666. | BF_GPMI_CTRL0_CS(chip, this)
  667. | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
  668. | BF_GPMI_CTRL0_ADDRESS(address)
  669. | BF_GPMI_CTRL0_XFER_COUNT(geo->page_size);
  670. pio[1] = 0;
  671. pio[2] = 0; /* clear GPMI_HW_GPMI_ECCCTRL, disable the BCH. */
  672. desc = dmaengine_prep_slave_sg(channel,
  673. (struct scatterlist *)pio, 3,
  674. DMA_TRANS_NONE,
  675. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  676. if (!desc)
  677. return -EINVAL;
  678. /* [4] submit the DMA */
  679. return start_dma_with_bch_irq(this, desc);
  680. }
  681. /**
  682. * gpmi_copy_bits - copy bits from one memory region to another
  683. * @dst: destination buffer
  684. * @dst_bit_off: bit offset we're starting to write at
  685. * @src: source buffer
  686. * @src_bit_off: bit offset we're starting to read from
  687. * @nbits: number of bits to copy
  688. *
  689. * This functions copies bits from one memory region to another, and is used by
  690. * the GPMI driver to copy ECC sections which are not guaranteed to be byte
  691. * aligned.
  692. *
  693. * src and dst should not overlap.
  694. *
  695. */
  696. void gpmi_copy_bits(u8 *dst, size_t dst_bit_off,
  697. const u8 *src, size_t src_bit_off,
  698. size_t nbits)
  699. {
  700. size_t i;
  701. size_t nbytes;
  702. u32 src_buffer = 0;
  703. size_t bits_in_src_buffer = 0;
  704. if (!nbits)
  705. return;
  706. /*
  707. * Move src and dst pointers to the closest byte pointer and store bit
  708. * offsets within a byte.
  709. */
  710. src += src_bit_off / 8;
  711. src_bit_off %= 8;
  712. dst += dst_bit_off / 8;
  713. dst_bit_off %= 8;
  714. /*
  715. * Initialize the src_buffer value with bits available in the first
  716. * byte of data so that we end up with a byte aligned src pointer.
  717. */
  718. if (src_bit_off) {
  719. src_buffer = src[0] >> src_bit_off;
  720. if (nbits >= (8 - src_bit_off)) {
  721. bits_in_src_buffer += 8 - src_bit_off;
  722. } else {
  723. src_buffer &= GENMASK(nbits - 1, 0);
  724. bits_in_src_buffer += nbits;
  725. }
  726. nbits -= bits_in_src_buffer;
  727. src++;
  728. }
  729. /* Calculate the number of bytes that can be copied from src to dst. */
  730. nbytes = nbits / 8;
  731. /* Try to align dst to a byte boundary. */
  732. if (dst_bit_off) {
  733. if (bits_in_src_buffer < (8 - dst_bit_off) && nbytes) {
  734. src_buffer |= src[0] << bits_in_src_buffer;
  735. bits_in_src_buffer += 8;
  736. src++;
  737. nbytes--;
  738. }
  739. if (bits_in_src_buffer >= (8 - dst_bit_off)) {
  740. dst[0] &= GENMASK(dst_bit_off - 1, 0);
  741. dst[0] |= src_buffer << dst_bit_off;
  742. src_buffer >>= (8 - dst_bit_off);
  743. bits_in_src_buffer -= (8 - dst_bit_off);
  744. dst_bit_off = 0;
  745. dst++;
  746. if (bits_in_src_buffer > 7) {
  747. bits_in_src_buffer -= 8;
  748. dst[0] = src_buffer;
  749. dst++;
  750. src_buffer >>= 8;
  751. }
  752. }
  753. }
  754. if (!bits_in_src_buffer && !dst_bit_off) {
  755. /*
  756. * Both src and dst pointers are byte aligned, thus we can
  757. * just use the optimized memcpy function.
  758. */
  759. if (nbytes)
  760. memcpy(dst, src, nbytes);
  761. } else {
  762. /*
  763. * src buffer is not byte aligned, hence we have to copy each
  764. * src byte to the src_buffer variable before extracting a byte
  765. * to store in dst.
  766. */
  767. for (i = 0; i < nbytes; i++) {
  768. src_buffer |= src[i] << bits_in_src_buffer;
  769. dst[i] = src_buffer;
  770. src_buffer >>= 8;
  771. }
  772. }
  773. /* Update dst and src pointers */
  774. dst += nbytes;
  775. src += nbytes;
  776. /*
  777. * nbits is the number of remaining bits. It should not exceed 8 as
  778. * we've already copied as much bytes as possible.
  779. */
  780. nbits %= 8;
  781. /*
  782. * If there's no more bits to copy to the destination and src buffer
  783. * was already byte aligned, then we're done.
  784. */
  785. if (!nbits && !bits_in_src_buffer)
  786. return;
  787. /* Copy the remaining bits to src_buffer */
  788. if (nbits)
  789. src_buffer |= (*src & GENMASK(nbits - 1, 0)) <<
  790. bits_in_src_buffer;
  791. bits_in_src_buffer += nbits;
  792. /*
  793. * In case there were not enough bits to get a byte aligned dst buffer
  794. * prepare the src_buffer variable to match the dst organization (shift
  795. * src_buffer by dst_bit_off and retrieve the least significant bits
  796. * from dst).
  797. */
  798. if (dst_bit_off)
  799. src_buffer = (src_buffer << dst_bit_off) |
  800. (*dst & GENMASK(dst_bit_off - 1, 0));
  801. bits_in_src_buffer += dst_bit_off;
  802. /*
  803. * Keep most significant bits from dst if we end up with an unaligned
  804. * number of bits.
  805. */
  806. nbytes = bits_in_src_buffer / 8;
  807. if (bits_in_src_buffer % 8) {
  808. src_buffer |= (dst[nbytes] &
  809. GENMASK(7, bits_in_src_buffer % 8)) <<
  810. (nbytes * 8);
  811. nbytes++;
  812. }
  813. /* Copy the remaining bytes to dst */
  814. for (i = 0; i < nbytes; i++) {
  815. dst[i] = src_buffer;
  816. src_buffer >>= 8;
  817. }
  818. }