gpmi-lib.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329
  1. /*
  2. * Freescale GPMI NAND Flash Driver
  3. *
  4. * Copyright (C) 2008-2011 Freescale Semiconductor, Inc.
  5. * Copyright (C) 2008 Embedded Alley Solutions, Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. */
  21. #include <linux/mtd/gpmi-nand.h>
  22. #include <linux/delay.h>
  23. #include <linux/clk.h>
  24. #include "gpmi-nand.h"
  25. #include "gpmi-regs.h"
  26. #include "bch-regs.h"
  27. static struct timing_threshod timing_default_threshold = {
  28. .max_data_setup_cycles = (BM_GPMI_TIMING0_DATA_SETUP >>
  29. BP_GPMI_TIMING0_DATA_SETUP),
  30. .internal_data_setup_in_ns = 0,
  31. .max_sample_delay_factor = (BM_GPMI_CTRL1_RDN_DELAY >>
  32. BP_GPMI_CTRL1_RDN_DELAY),
  33. .max_dll_clock_period_in_ns = 32,
  34. .max_dll_delay_in_ns = 16,
  35. };
  36. #define MXS_SET_ADDR 0x4
  37. #define MXS_CLR_ADDR 0x8
  38. /*
  39. * Clear the bit and poll it cleared. This is usually called with
  40. * a reset address and mask being either SFTRST(bit 31) or CLKGATE
  41. * (bit 30).
  42. */
  43. static int clear_poll_bit(void __iomem *addr, u32 mask)
  44. {
  45. int timeout = 0x400;
  46. /* clear the bit */
  47. writel(mask, addr + MXS_CLR_ADDR);
  48. /*
  49. * SFTRST needs 3 GPMI clocks to settle, the reference manual
  50. * recommends to wait 1us.
  51. */
  52. udelay(1);
  53. /* poll the bit becoming clear */
  54. while ((readl(addr) & mask) && --timeout)
  55. /* nothing */;
  56. return !timeout;
  57. }
  58. #define MODULE_CLKGATE (1 << 30)
  59. #define MODULE_SFTRST (1 << 31)
  60. /*
  61. * The current mxs_reset_block() will do two things:
  62. * [1] enable the module.
  63. * [2] reset the module.
  64. *
  65. * In most of the cases, it's ok.
  66. * But in MX23, there is a hardware bug in the BCH block (see erratum #2847).
  67. * If you try to soft reset the BCH block, it becomes unusable until
  68. * the next hard reset. This case occurs in the NAND boot mode. When the board
  69. * boots by NAND, the ROM of the chip will initialize the BCH blocks itself.
  70. * So If the driver tries to reset the BCH again, the BCH will not work anymore.
  71. * You will see a DMA timeout in this case. The bug has been fixed
  72. * in the following chips, such as MX28.
  73. *
  74. * To avoid this bug, just add a new parameter `just_enable` for
  75. * the mxs_reset_block(), and rewrite it here.
  76. */
  77. static int gpmi_reset_block(void __iomem *reset_addr, bool just_enable)
  78. {
  79. int ret;
  80. int timeout = 0x400;
  81. /* clear and poll SFTRST */
  82. ret = clear_poll_bit(reset_addr, MODULE_SFTRST);
  83. if (unlikely(ret))
  84. goto error;
  85. /* clear CLKGATE */
  86. writel(MODULE_CLKGATE, reset_addr + MXS_CLR_ADDR);
  87. if (!just_enable) {
  88. /* set SFTRST to reset the block */
  89. writel(MODULE_SFTRST, reset_addr + MXS_SET_ADDR);
  90. udelay(1);
  91. /* poll CLKGATE becoming set */
  92. while ((!(readl(reset_addr) & MODULE_CLKGATE)) && --timeout)
  93. /* nothing */;
  94. if (unlikely(!timeout))
  95. goto error;
  96. }
  97. /* clear and poll SFTRST */
  98. ret = clear_poll_bit(reset_addr, MODULE_SFTRST);
  99. if (unlikely(ret))
  100. goto error;
  101. /* clear and poll CLKGATE */
  102. ret = clear_poll_bit(reset_addr, MODULE_CLKGATE);
  103. if (unlikely(ret))
  104. goto error;
  105. return 0;
  106. error:
  107. pr_err("%s(%p): module reset timeout\n", __func__, reset_addr);
  108. return -ETIMEDOUT;
  109. }
  110. static int __gpmi_enable_clk(struct gpmi_nand_data *this, bool v)
  111. {
  112. struct clk *clk;
  113. int ret;
  114. int i;
  115. for (i = 0; i < GPMI_CLK_MAX; i++) {
  116. clk = this->resources.clock[i];
  117. if (!clk)
  118. break;
  119. if (v) {
  120. ret = clk_prepare_enable(clk);
  121. if (ret)
  122. goto err_clk;
  123. } else {
  124. clk_disable_unprepare(clk);
  125. }
  126. }
  127. return 0;
  128. err_clk:
  129. for (; i > 0; i--)
  130. clk_disable_unprepare(this->resources.clock[i - 1]);
  131. return ret;
  132. }
  133. #define gpmi_enable_clk(x) __gpmi_enable_clk(x, true)
  134. #define gpmi_disable_clk(x) __gpmi_enable_clk(x, false)
  135. int gpmi_init(struct gpmi_nand_data *this)
  136. {
  137. struct resources *r = &this->resources;
  138. int ret;
  139. ret = gpmi_enable_clk(this);
  140. if (ret)
  141. goto err_out;
  142. ret = gpmi_reset_block(r->gpmi_regs, false);
  143. if (ret)
  144. goto err_out;
  145. /* Choose NAND mode. */
  146. writel(BM_GPMI_CTRL1_GPMI_MODE, r->gpmi_regs + HW_GPMI_CTRL1_CLR);
  147. /* Set the IRQ polarity. */
  148. writel(BM_GPMI_CTRL1_ATA_IRQRDY_POLARITY,
  149. r->gpmi_regs + HW_GPMI_CTRL1_SET);
  150. /* Disable Write-Protection. */
  151. writel(BM_GPMI_CTRL1_DEV_RESET, r->gpmi_regs + HW_GPMI_CTRL1_SET);
  152. /* Select BCH ECC. */
  153. writel(BM_GPMI_CTRL1_BCH_MODE, r->gpmi_regs + HW_GPMI_CTRL1_SET);
  154. gpmi_disable_clk(this);
  155. return 0;
  156. err_out:
  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. pr_err("Show GPMI registers :\n");
  167. for (i = 0; i <= HW_GPMI_DEBUG / 0x10 + 1; i++) {
  168. reg = readl(r->gpmi_regs + i * 0x10);
  169. pr_err("offset 0x%.3x : 0x%.8x\n", i * 0x10, reg);
  170. }
  171. /* start to print out the BCH info */
  172. pr_err("BCH Geometry :\n");
  173. pr_err("GF length : %u\n", geo->gf_len);
  174. pr_err("ECC Strength : %u\n", geo->ecc_strength);
  175. pr_err("Page Size in Bytes : %u\n", geo->page_size);
  176. pr_err("Metadata Size in Bytes : %u\n", geo->metadata_size);
  177. pr_err("ECC Chunk Size in Bytes: %u\n", geo->ecc_chunk_size);
  178. pr_err("ECC Chunk Count : %u\n", geo->ecc_chunk_count);
  179. pr_err("Payload Size in Bytes : %u\n", geo->payload_size);
  180. pr_err("Auxiliary Size in Bytes: %u\n", geo->auxiliary_size);
  181. pr_err("Auxiliary Status Offset: %u\n", geo->auxiliary_status_offset);
  182. pr_err("Block Mark Byte Offset : %u\n", geo->block_mark_byte_offset);
  183. pr_err("Block Mark Bit Offset : %u\n", geo->block_mark_bit_offset);
  184. }
  185. /* Configures the geometry for BCH. */
  186. int bch_set_geometry(struct gpmi_nand_data *this)
  187. {
  188. struct resources *r = &this->resources;
  189. struct bch_geometry *bch_geo = &this->bch_geometry;
  190. unsigned int block_count;
  191. unsigned int block_size;
  192. unsigned int metadata_size;
  193. unsigned int ecc_strength;
  194. unsigned int page_size;
  195. int ret;
  196. if (common_nfc_set_geometry(this))
  197. return !0;
  198. block_count = bch_geo->ecc_chunk_count - 1;
  199. block_size = bch_geo->ecc_chunk_size;
  200. metadata_size = bch_geo->metadata_size;
  201. ecc_strength = bch_geo->ecc_strength >> 1;
  202. page_size = bch_geo->page_size;
  203. ret = gpmi_enable_clk(this);
  204. if (ret)
  205. goto err_out;
  206. /*
  207. * Due to erratum #2847 of the MX23, the BCH cannot be soft reset on this
  208. * chip, otherwise it will lock up. So we skip resetting BCH on the MX23.
  209. * On the other hand, the MX28 needs the reset, because one case has been
  210. * seen where the BCH produced ECC errors constantly after 10000
  211. * consecutive reboots. The latter case has not been seen on the MX23 yet,
  212. * still we don't know if it could happen there as well.
  213. */
  214. ret = gpmi_reset_block(r->bch_regs, GPMI_IS_MX23(this));
  215. if (ret)
  216. goto err_out;
  217. /* Configure layout 0. */
  218. writel(BF_BCH_FLASH0LAYOUT0_NBLOCKS(block_count)
  219. | BF_BCH_FLASH0LAYOUT0_META_SIZE(metadata_size)
  220. | BF_BCH_FLASH0LAYOUT0_ECC0(ecc_strength, this)
  221. | BF_BCH_FLASH0LAYOUT0_DATA0_SIZE(block_size, this),
  222. r->bch_regs + HW_BCH_FLASH0LAYOUT0);
  223. writel(BF_BCH_FLASH0LAYOUT1_PAGE_SIZE(page_size)
  224. | BF_BCH_FLASH0LAYOUT1_ECCN(ecc_strength, this)
  225. | BF_BCH_FLASH0LAYOUT1_DATAN_SIZE(block_size, this),
  226. r->bch_regs + HW_BCH_FLASH0LAYOUT1);
  227. /* Set *all* chip selects to use layout 0. */
  228. writel(0, r->bch_regs + HW_BCH_LAYOUTSELECT);
  229. /* Enable interrupts. */
  230. writel(BM_BCH_CTRL_COMPLETE_IRQ_EN,
  231. r->bch_regs + HW_BCH_CTRL_SET);
  232. gpmi_disable_clk(this);
  233. return 0;
  234. err_out:
  235. return ret;
  236. }
  237. /* Converts time in nanoseconds to cycles. */
  238. static unsigned int ns_to_cycles(unsigned int time,
  239. unsigned int period, unsigned int min)
  240. {
  241. unsigned int k;
  242. k = (time + period - 1) / period;
  243. return max(k, min);
  244. }
  245. #define DEF_MIN_PROP_DELAY 5
  246. #define DEF_MAX_PROP_DELAY 9
  247. /* Apply timing to current hardware conditions. */
  248. static int gpmi_nfc_compute_hardware_timing(struct gpmi_nand_data *this,
  249. struct gpmi_nfc_hardware_timing *hw)
  250. {
  251. struct timing_threshod *nfc = &timing_default_threshold;
  252. struct resources *r = &this->resources;
  253. struct nand_chip *nand = &this->nand;
  254. struct nand_timing target = this->timing;
  255. bool improved_timing_is_available;
  256. unsigned long clock_frequency_in_hz;
  257. unsigned int clock_period_in_ns;
  258. bool dll_use_half_periods;
  259. unsigned int dll_delay_shift;
  260. unsigned int max_sample_delay_in_ns;
  261. unsigned int address_setup_in_cycles;
  262. unsigned int data_setup_in_ns;
  263. unsigned int data_setup_in_cycles;
  264. unsigned int data_hold_in_cycles;
  265. int ideal_sample_delay_in_ns;
  266. unsigned int sample_delay_factor;
  267. int tEYE;
  268. unsigned int min_prop_delay_in_ns = DEF_MIN_PROP_DELAY;
  269. unsigned int max_prop_delay_in_ns = DEF_MAX_PROP_DELAY;
  270. /*
  271. * If there are multiple chips, we need to relax the timings to allow
  272. * for signal distortion due to higher capacitance.
  273. */
  274. if (nand->numchips > 2) {
  275. target.data_setup_in_ns += 10;
  276. target.data_hold_in_ns += 10;
  277. target.address_setup_in_ns += 10;
  278. } else if (nand->numchips > 1) {
  279. target.data_setup_in_ns += 5;
  280. target.data_hold_in_ns += 5;
  281. target.address_setup_in_ns += 5;
  282. }
  283. /* Check if improved timing information is available. */
  284. improved_timing_is_available =
  285. (target.tREA_in_ns >= 0) &&
  286. (target.tRLOH_in_ns >= 0) &&
  287. (target.tRHOH_in_ns >= 0) ;
  288. /* Inspect the clock. */
  289. nfc->clock_frequency_in_hz = clk_get_rate(r->clock[0]);
  290. clock_frequency_in_hz = nfc->clock_frequency_in_hz;
  291. clock_period_in_ns = NSEC_PER_SEC / clock_frequency_in_hz;
  292. /*
  293. * The NFC quantizes setup and hold parameters in terms of clock cycles.
  294. * Here, we quantize the setup and hold timing parameters to the
  295. * next-highest clock period to make sure we apply at least the
  296. * specified times.
  297. *
  298. * For data setup and data hold, the hardware interprets a value of zero
  299. * as the largest possible delay. This is not what's intended by a zero
  300. * in the input parameter, so we impose a minimum of one cycle.
  301. */
  302. data_setup_in_cycles = ns_to_cycles(target.data_setup_in_ns,
  303. clock_period_in_ns, 1);
  304. data_hold_in_cycles = ns_to_cycles(target.data_hold_in_ns,
  305. clock_period_in_ns, 1);
  306. address_setup_in_cycles = ns_to_cycles(target.address_setup_in_ns,
  307. clock_period_in_ns, 0);
  308. /*
  309. * The clock's period affects the sample delay in a number of ways:
  310. *
  311. * (1) The NFC HAL tells us the maximum clock period the sample delay
  312. * DLL can tolerate. If the clock period is greater than half that
  313. * maximum, we must configure the DLL to be driven by half periods.
  314. *
  315. * (2) We need to convert from an ideal sample delay, in ns, to a
  316. * "sample delay factor," which the NFC uses. This factor depends on
  317. * whether we're driving the DLL with full or half periods.
  318. * Paraphrasing the reference manual:
  319. *
  320. * AD = SDF x 0.125 x RP
  321. *
  322. * where:
  323. *
  324. * AD is the applied delay, in ns.
  325. * SDF is the sample delay factor, which is dimensionless.
  326. * RP is the reference period, in ns, which is a full clock period
  327. * if the DLL is being driven by full periods, or half that if
  328. * the DLL is being driven by half periods.
  329. *
  330. * Let's re-arrange this in a way that's more useful to us:
  331. *
  332. * 8
  333. * SDF = AD x ----
  334. * RP
  335. *
  336. * The reference period is either the clock period or half that, so this
  337. * is:
  338. *
  339. * 8 AD x DDF
  340. * SDF = AD x ----- = --------
  341. * f x P P
  342. *
  343. * where:
  344. *
  345. * f is 1 or 1/2, depending on how we're driving the DLL.
  346. * P is the clock period.
  347. * DDF is the DLL Delay Factor, a dimensionless value that
  348. * incorporates all the constants in the conversion.
  349. *
  350. * DDF will be either 8 or 16, both of which are powers of two. We can
  351. * reduce the cost of this conversion by using bit shifts instead of
  352. * multiplication or division. Thus:
  353. *
  354. * AD << DDS
  355. * SDF = ---------
  356. * P
  357. *
  358. * or
  359. *
  360. * AD = (SDF >> DDS) x P
  361. *
  362. * where:
  363. *
  364. * DDS is the DLL Delay Shift, the logarithm to base 2 of the DDF.
  365. */
  366. if (clock_period_in_ns > (nfc->max_dll_clock_period_in_ns >> 1)) {
  367. dll_use_half_periods = true;
  368. dll_delay_shift = 3 + 1;
  369. } else {
  370. dll_use_half_periods = false;
  371. dll_delay_shift = 3;
  372. }
  373. /*
  374. * Compute the maximum sample delay the NFC allows, under current
  375. * conditions. If the clock is running too slowly, no sample delay is
  376. * possible.
  377. */
  378. if (clock_period_in_ns > nfc->max_dll_clock_period_in_ns)
  379. max_sample_delay_in_ns = 0;
  380. else {
  381. /*
  382. * Compute the delay implied by the largest sample delay factor
  383. * the NFC allows.
  384. */
  385. max_sample_delay_in_ns =
  386. (nfc->max_sample_delay_factor * clock_period_in_ns) >>
  387. dll_delay_shift;
  388. /*
  389. * Check if the implied sample delay larger than the NFC
  390. * actually allows.
  391. */
  392. if (max_sample_delay_in_ns > nfc->max_dll_delay_in_ns)
  393. max_sample_delay_in_ns = nfc->max_dll_delay_in_ns;
  394. }
  395. /*
  396. * Check if improved timing information is available. If not, we have to
  397. * use a less-sophisticated algorithm.
  398. */
  399. if (!improved_timing_is_available) {
  400. /*
  401. * Fold the read setup time required by the NFC into the ideal
  402. * sample delay.
  403. */
  404. ideal_sample_delay_in_ns = target.gpmi_sample_delay_in_ns +
  405. nfc->internal_data_setup_in_ns;
  406. /*
  407. * The ideal sample delay may be greater than the maximum
  408. * allowed by the NFC. If so, we can trade off sample delay time
  409. * for more data setup time.
  410. *
  411. * In each iteration of the following loop, we add a cycle to
  412. * the data setup time and subtract a corresponding amount from
  413. * the sample delay until we've satisified the constraints or
  414. * can't do any better.
  415. */
  416. while ((ideal_sample_delay_in_ns > max_sample_delay_in_ns) &&
  417. (data_setup_in_cycles < nfc->max_data_setup_cycles)) {
  418. data_setup_in_cycles++;
  419. ideal_sample_delay_in_ns -= clock_period_in_ns;
  420. if (ideal_sample_delay_in_ns < 0)
  421. ideal_sample_delay_in_ns = 0;
  422. }
  423. /*
  424. * Compute the sample delay factor that corresponds most closely
  425. * to the ideal sample delay. If the result is too large for the
  426. * NFC, use the maximum value.
  427. *
  428. * Notice that we use the ns_to_cycles function to compute the
  429. * sample delay factor. We do this because the form of the
  430. * computation is the same as that for calculating cycles.
  431. */
  432. sample_delay_factor =
  433. ns_to_cycles(
  434. ideal_sample_delay_in_ns << dll_delay_shift,
  435. clock_period_in_ns, 0);
  436. if (sample_delay_factor > nfc->max_sample_delay_factor)
  437. sample_delay_factor = nfc->max_sample_delay_factor;
  438. /* Skip to the part where we return our results. */
  439. goto return_results;
  440. }
  441. /*
  442. * If control arrives here, we have more detailed timing information,
  443. * so we can use a better algorithm.
  444. */
  445. /*
  446. * Fold the read setup time required by the NFC into the maximum
  447. * propagation delay.
  448. */
  449. max_prop_delay_in_ns += nfc->internal_data_setup_in_ns;
  450. /*
  451. * Earlier, we computed the number of clock cycles required to satisfy
  452. * the data setup time. Now, we need to know the actual nanoseconds.
  453. */
  454. data_setup_in_ns = clock_period_in_ns * data_setup_in_cycles;
  455. /*
  456. * Compute tEYE, the width of the data eye when reading from the NAND
  457. * Flash. The eye width is fundamentally determined by the data setup
  458. * time, perturbed by propagation delays and some characteristics of the
  459. * NAND Flash device.
  460. *
  461. * start of the eye = max_prop_delay + tREA
  462. * end of the eye = min_prop_delay + tRHOH + data_setup
  463. */
  464. tEYE = (int)min_prop_delay_in_ns + (int)target.tRHOH_in_ns +
  465. (int)data_setup_in_ns;
  466. tEYE -= (int)max_prop_delay_in_ns + (int)target.tREA_in_ns;
  467. /*
  468. * The eye must be open. If it's not, we can try to open it by
  469. * increasing its main forcer, the data setup time.
  470. *
  471. * In each iteration of the following loop, we increase the data setup
  472. * time by a single clock cycle. We do this until either the eye is
  473. * open or we run into NFC limits.
  474. */
  475. while ((tEYE <= 0) &&
  476. (data_setup_in_cycles < nfc->max_data_setup_cycles)) {
  477. /* Give a cycle to data setup. */
  478. data_setup_in_cycles++;
  479. /* Synchronize the data setup time with the cycles. */
  480. data_setup_in_ns += clock_period_in_ns;
  481. /* Adjust tEYE accordingly. */
  482. tEYE += clock_period_in_ns;
  483. }
  484. /*
  485. * When control arrives here, the eye is open. The ideal time to sample
  486. * the data is in the center of the eye:
  487. *
  488. * end of the eye + start of the eye
  489. * --------------------------------- - data_setup
  490. * 2
  491. *
  492. * After some algebra, this simplifies to the code immediately below.
  493. */
  494. ideal_sample_delay_in_ns =
  495. ((int)max_prop_delay_in_ns +
  496. (int)target.tREA_in_ns +
  497. (int)min_prop_delay_in_ns +
  498. (int)target.tRHOH_in_ns -
  499. (int)data_setup_in_ns) >> 1;
  500. /*
  501. * The following figure illustrates some aspects of a NAND Flash read:
  502. *
  503. *
  504. * __ _____________________________________
  505. * RDN \_________________/
  506. *
  507. * <---- tEYE ----->
  508. * /-----------------\
  509. * Read Data ----------------------------< >---------
  510. * \-----------------/
  511. * ^ ^ ^ ^
  512. * | | | |
  513. * |<--Data Setup -->|<--Delay Time -->| |
  514. * | | | |
  515. * | | |
  516. * | |<-- Quantized Delay Time -->|
  517. * | | |
  518. *
  519. *
  520. * We have some issues we must now address:
  521. *
  522. * (1) The *ideal* sample delay time must not be negative. If it is, we
  523. * jam it to zero.
  524. *
  525. * (2) The *ideal* sample delay time must not be greater than that
  526. * allowed by the NFC. If it is, we can increase the data setup
  527. * time, which will reduce the delay between the end of the data
  528. * setup and the center of the eye. It will also make the eye
  529. * larger, which might help with the next issue...
  530. *
  531. * (3) The *quantized* sample delay time must not fall either before the
  532. * eye opens or after it closes (the latter is the problem
  533. * illustrated in the above figure).
  534. */
  535. /* Jam a negative ideal sample delay to zero. */
  536. if (ideal_sample_delay_in_ns < 0)
  537. ideal_sample_delay_in_ns = 0;
  538. /*
  539. * Extend the data setup as needed to reduce the ideal sample delay
  540. * below the maximum permitted by the NFC.
  541. */
  542. while ((ideal_sample_delay_in_ns > max_sample_delay_in_ns) &&
  543. (data_setup_in_cycles < nfc->max_data_setup_cycles)) {
  544. /* Give a cycle to data setup. */
  545. data_setup_in_cycles++;
  546. /* Synchronize the data setup time with the cycles. */
  547. data_setup_in_ns += clock_period_in_ns;
  548. /* Adjust tEYE accordingly. */
  549. tEYE += clock_period_in_ns;
  550. /*
  551. * Decrease the ideal sample delay by one half cycle, to keep it
  552. * in the middle of the eye.
  553. */
  554. ideal_sample_delay_in_ns -= (clock_period_in_ns >> 1);
  555. /* Jam a negative ideal sample delay to zero. */
  556. if (ideal_sample_delay_in_ns < 0)
  557. ideal_sample_delay_in_ns = 0;
  558. }
  559. /*
  560. * Compute the sample delay factor that corresponds to the ideal sample
  561. * delay. If the result is too large, then use the maximum allowed
  562. * value.
  563. *
  564. * Notice that we use the ns_to_cycles function to compute the sample
  565. * delay factor. We do this because the form of the computation is the
  566. * same as that for calculating cycles.
  567. */
  568. sample_delay_factor =
  569. ns_to_cycles(ideal_sample_delay_in_ns << dll_delay_shift,
  570. clock_period_in_ns, 0);
  571. if (sample_delay_factor > nfc->max_sample_delay_factor)
  572. sample_delay_factor = nfc->max_sample_delay_factor;
  573. /*
  574. * These macros conveniently encapsulate a computation we'll use to
  575. * continuously evaluate whether or not the data sample delay is inside
  576. * the eye.
  577. */
  578. #define IDEAL_DELAY ((int) ideal_sample_delay_in_ns)
  579. #define QUANTIZED_DELAY \
  580. ((int) ((sample_delay_factor * clock_period_in_ns) >> \
  581. dll_delay_shift))
  582. #define DELAY_ERROR (abs(QUANTIZED_DELAY - IDEAL_DELAY))
  583. #define SAMPLE_IS_NOT_WITHIN_THE_EYE (DELAY_ERROR > (tEYE >> 1))
  584. /*
  585. * While the quantized sample time falls outside the eye, reduce the
  586. * sample delay or extend the data setup to move the sampling point back
  587. * toward the eye. Do not allow the number of data setup cycles to
  588. * exceed the maximum allowed by the NFC.
  589. */
  590. while (SAMPLE_IS_NOT_WITHIN_THE_EYE &&
  591. (data_setup_in_cycles < nfc->max_data_setup_cycles)) {
  592. /*
  593. * If control arrives here, the quantized sample delay falls
  594. * outside the eye. Check if it's before the eye opens, or after
  595. * the eye closes.
  596. */
  597. if (QUANTIZED_DELAY > IDEAL_DELAY) {
  598. /*
  599. * If control arrives here, the quantized sample delay
  600. * falls after the eye closes. Decrease the quantized
  601. * delay time and then go back to re-evaluate.
  602. */
  603. if (sample_delay_factor != 0)
  604. sample_delay_factor--;
  605. continue;
  606. }
  607. /*
  608. * If control arrives here, the quantized sample delay falls
  609. * before the eye opens. Shift the sample point by increasing
  610. * data setup time. This will also make the eye larger.
  611. */
  612. /* Give a cycle to data setup. */
  613. data_setup_in_cycles++;
  614. /* Synchronize the data setup time with the cycles. */
  615. data_setup_in_ns += clock_period_in_ns;
  616. /* Adjust tEYE accordingly. */
  617. tEYE += clock_period_in_ns;
  618. /*
  619. * Decrease the ideal sample delay by one half cycle, to keep it
  620. * in the middle of the eye.
  621. */
  622. ideal_sample_delay_in_ns -= (clock_period_in_ns >> 1);
  623. /* ...and one less period for the delay time. */
  624. ideal_sample_delay_in_ns -= clock_period_in_ns;
  625. /* Jam a negative ideal sample delay to zero. */
  626. if (ideal_sample_delay_in_ns < 0)
  627. ideal_sample_delay_in_ns = 0;
  628. /*
  629. * We have a new ideal sample delay, so re-compute the quantized
  630. * delay.
  631. */
  632. sample_delay_factor =
  633. ns_to_cycles(
  634. ideal_sample_delay_in_ns << dll_delay_shift,
  635. clock_period_in_ns, 0);
  636. if (sample_delay_factor > nfc->max_sample_delay_factor)
  637. sample_delay_factor = nfc->max_sample_delay_factor;
  638. }
  639. /* Control arrives here when we're ready to return our results. */
  640. return_results:
  641. hw->data_setup_in_cycles = data_setup_in_cycles;
  642. hw->data_hold_in_cycles = data_hold_in_cycles;
  643. hw->address_setup_in_cycles = address_setup_in_cycles;
  644. hw->use_half_periods = dll_use_half_periods;
  645. hw->sample_delay_factor = sample_delay_factor;
  646. hw->device_busy_timeout = GPMI_DEFAULT_BUSY_TIMEOUT;
  647. hw->wrn_dly_sel = BV_GPMI_CTRL1_WRN_DLY_SEL_4_TO_8NS;
  648. /* Return success. */
  649. return 0;
  650. }
  651. /*
  652. * <1> Firstly, we should know what's the GPMI-clock means.
  653. * The GPMI-clock is the internal clock in the gpmi nand controller.
  654. * If you set 100MHz to gpmi nand controller, the GPMI-clock's period
  655. * is 10ns. Mark the GPMI-clock's period as GPMI-clock-period.
  656. *
  657. * <2> Secondly, we should know what's the frequency on the nand chip pins.
  658. * The frequency on the nand chip pins is derived from the GPMI-clock.
  659. * We can get it from the following equation:
  660. *
  661. * F = G / (DS + DH)
  662. *
  663. * F : the frequency on the nand chip pins.
  664. * G : the GPMI clock, such as 100MHz.
  665. * DS : GPMI_HW_GPMI_TIMING0:DATA_SETUP
  666. * DH : GPMI_HW_GPMI_TIMING0:DATA_HOLD
  667. *
  668. * <3> Thirdly, when the frequency on the nand chip pins is above 33MHz,
  669. * the nand EDO(extended Data Out) timing could be applied.
  670. * The GPMI implements a feedback read strobe to sample the read data.
  671. * The feedback read strobe can be delayed to support the nand EDO timing
  672. * where the read strobe may deasserts before the read data is valid, and
  673. * read data is valid for some time after read strobe.
  674. *
  675. * The following figure illustrates some aspects of a NAND Flash read:
  676. *
  677. * |<---tREA---->|
  678. * | |
  679. * | | |
  680. * |<--tRP-->| |
  681. * | | |
  682. * __ ___|__________________________________
  683. * RDN \________/ |
  684. * |
  685. * /---------\
  686. * Read Data --------------< >---------
  687. * \---------/
  688. * | |
  689. * |<-D->|
  690. * FeedbackRDN ________ ____________
  691. * \___________/
  692. *
  693. * D stands for delay, set in the HW_GPMI_CTRL1:RDN_DELAY.
  694. *
  695. *
  696. * <4> Now, we begin to describe how to compute the right RDN_DELAY.
  697. *
  698. * 4.1) From the aspect of the nand chip pins:
  699. * Delay = (tREA + C - tRP) {1}
  700. *
  701. * tREA : the maximum read access time. From the ONFI nand standards,
  702. * we know that tREA is 16ns in mode 5, tREA is 20ns is mode 4.
  703. * Please check it in : www.onfi.org
  704. * C : a constant for adjust the delay. default is 4.
  705. * tRP : the read pulse width.
  706. * Specified by the HW_GPMI_TIMING0:DATA_SETUP:
  707. * tRP = (GPMI-clock-period) * DATA_SETUP
  708. *
  709. * 4.2) From the aspect of the GPMI nand controller:
  710. * Delay = RDN_DELAY * 0.125 * RP {2}
  711. *
  712. * RP : the DLL reference period.
  713. * if (GPMI-clock-period > DLL_THRETHOLD)
  714. * RP = GPMI-clock-period / 2;
  715. * else
  716. * RP = GPMI-clock-period;
  717. *
  718. * Set the HW_GPMI_CTRL1:HALF_PERIOD if GPMI-clock-period
  719. * is greater DLL_THRETHOLD. In other SOCs, the DLL_THRETHOLD
  720. * is 16ns, but in mx6q, we use 12ns.
  721. *
  722. * 4.3) since {1} equals {2}, we get:
  723. *
  724. * (tREA + 4 - tRP) * 8
  725. * RDN_DELAY = --------------------- {3}
  726. * RP
  727. *
  728. * 4.4) We only support the fastest asynchronous mode of ONFI nand.
  729. * For some ONFI nand, the mode 4 is the fastest mode;
  730. * while for some ONFI nand, the mode 5 is the fastest mode.
  731. * So we only support the mode 4 and mode 5. It is no need to
  732. * support other modes.
  733. */
  734. static void gpmi_compute_edo_timing(struct gpmi_nand_data *this,
  735. struct gpmi_nfc_hardware_timing *hw)
  736. {
  737. struct resources *r = &this->resources;
  738. unsigned long rate = clk_get_rate(r->clock[0]);
  739. int mode = this->timing_mode;
  740. int dll_threshold = 16; /* in ns */
  741. unsigned long delay;
  742. unsigned long clk_period;
  743. int t_rea;
  744. int c = 4;
  745. int t_rp;
  746. int rp;
  747. /*
  748. * [1] for GPMI_HW_GPMI_TIMING0:
  749. * The async mode requires 40MHz for mode 4, 50MHz for mode 5.
  750. * The GPMI can support 100MHz at most. So if we want to
  751. * get the 40MHz or 50MHz, we have to set DS=1, DH=1.
  752. * Set the ADDRESS_SETUP to 0 in mode 4.
  753. */
  754. hw->data_setup_in_cycles = 1;
  755. hw->data_hold_in_cycles = 1;
  756. hw->address_setup_in_cycles = ((mode == 5) ? 1 : 0);
  757. /* [2] for GPMI_HW_GPMI_TIMING1 */
  758. hw->device_busy_timeout = 0x9000;
  759. /* [3] for GPMI_HW_GPMI_CTRL1 */
  760. hw->wrn_dly_sel = BV_GPMI_CTRL1_WRN_DLY_SEL_NO_DELAY;
  761. if (GPMI_IS_MX6Q(this))
  762. dll_threshold = 12;
  763. /*
  764. * Enlarge 10 times for the numerator and denominator in {3}.
  765. * This make us to get more accurate result.
  766. */
  767. clk_period = NSEC_PER_SEC / (rate / 10);
  768. dll_threshold *= 10;
  769. t_rea = ((mode == 5) ? 16 : 20) * 10;
  770. c *= 10;
  771. t_rp = clk_period * 1; /* DATA_SETUP is 1 */
  772. if (clk_period > dll_threshold) {
  773. hw->use_half_periods = 1;
  774. rp = clk_period / 2;
  775. } else {
  776. hw->use_half_periods = 0;
  777. rp = clk_period;
  778. }
  779. /*
  780. * Multiply the numerator with 10, we could do a round off:
  781. * 7.8 round up to 8; 7.4 round down to 7.
  782. */
  783. delay = (((t_rea + c - t_rp) * 8) * 10) / rp;
  784. delay = (delay + 5) / 10;
  785. hw->sample_delay_factor = delay;
  786. }
  787. static int enable_edo_mode(struct gpmi_nand_data *this, int mode)
  788. {
  789. struct resources *r = &this->resources;
  790. struct nand_chip *nand = &this->nand;
  791. struct mtd_info *mtd = &this->mtd;
  792. uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {};
  793. unsigned long rate;
  794. int ret;
  795. nand->select_chip(mtd, 0);
  796. /* [1] send SET FEATURE commond to NAND */
  797. feature[0] = mode;
  798. ret = nand->onfi_set_features(mtd, nand,
  799. ONFI_FEATURE_ADDR_TIMING_MODE, feature);
  800. if (ret)
  801. goto err_out;
  802. /* [2] send GET FEATURE command to double-check the timing mode */
  803. memset(feature, 0, ONFI_SUBFEATURE_PARAM_LEN);
  804. ret = nand->onfi_get_features(mtd, nand,
  805. ONFI_FEATURE_ADDR_TIMING_MODE, feature);
  806. if (ret || feature[0] != mode)
  807. goto err_out;
  808. nand->select_chip(mtd, -1);
  809. /* [3] set the main IO clock, 100MHz for mode 5, 80MHz for mode 4. */
  810. rate = (mode == 5) ? 100000000 : 80000000;
  811. clk_set_rate(r->clock[0], rate);
  812. /* Let the gpmi_begin() re-compute the timing again. */
  813. this->flags &= ~GPMI_TIMING_INIT_OK;
  814. this->flags |= GPMI_ASYNC_EDO_ENABLED;
  815. this->timing_mode = mode;
  816. dev_info(this->dev, "enable the asynchronous EDO mode %d\n", mode);
  817. return 0;
  818. err_out:
  819. nand->select_chip(mtd, -1);
  820. dev_err(this->dev, "mode:%d ,failed in set feature.\n", mode);
  821. return -EINVAL;
  822. }
  823. int gpmi_extra_init(struct gpmi_nand_data *this)
  824. {
  825. struct nand_chip *chip = &this->nand;
  826. /* Enable the asynchronous EDO feature. */
  827. if (GPMI_IS_MX6Q(this) && chip->onfi_version) {
  828. int mode = onfi_get_async_timing_mode(chip);
  829. /* We only support the timing mode 4 and mode 5. */
  830. if (mode & ONFI_TIMING_MODE_5)
  831. mode = 5;
  832. else if (mode & ONFI_TIMING_MODE_4)
  833. mode = 4;
  834. else
  835. return 0;
  836. return enable_edo_mode(this, mode);
  837. }
  838. return 0;
  839. }
  840. /* Begin the I/O */
  841. void gpmi_begin(struct gpmi_nand_data *this)
  842. {
  843. struct resources *r = &this->resources;
  844. void __iomem *gpmi_regs = r->gpmi_regs;
  845. unsigned int clock_period_in_ns;
  846. uint32_t reg;
  847. unsigned int dll_wait_time_in_us;
  848. struct gpmi_nfc_hardware_timing hw;
  849. int ret;
  850. /* Enable the clock. */
  851. ret = gpmi_enable_clk(this);
  852. if (ret) {
  853. pr_err("We failed in enable the clk\n");
  854. goto err_out;
  855. }
  856. /* Only initialize the timing once */
  857. if (this->flags & GPMI_TIMING_INIT_OK)
  858. return;
  859. this->flags |= GPMI_TIMING_INIT_OK;
  860. if (this->flags & GPMI_ASYNC_EDO_ENABLED)
  861. gpmi_compute_edo_timing(this, &hw);
  862. else
  863. gpmi_nfc_compute_hardware_timing(this, &hw);
  864. /* [1] Set HW_GPMI_TIMING0 */
  865. reg = BF_GPMI_TIMING0_ADDRESS_SETUP(hw.address_setup_in_cycles) |
  866. BF_GPMI_TIMING0_DATA_HOLD(hw.data_hold_in_cycles) |
  867. BF_GPMI_TIMING0_DATA_SETUP(hw.data_setup_in_cycles) ;
  868. writel(reg, gpmi_regs + HW_GPMI_TIMING0);
  869. /* [2] Set HW_GPMI_TIMING1 */
  870. writel(BF_GPMI_TIMING1_BUSY_TIMEOUT(hw.device_busy_timeout),
  871. gpmi_regs + HW_GPMI_TIMING1);
  872. /* [3] The following code is to set the HW_GPMI_CTRL1. */
  873. /* Set the WRN_DLY_SEL */
  874. writel(BM_GPMI_CTRL1_WRN_DLY_SEL, gpmi_regs + HW_GPMI_CTRL1_CLR);
  875. writel(BF_GPMI_CTRL1_WRN_DLY_SEL(hw.wrn_dly_sel),
  876. gpmi_regs + HW_GPMI_CTRL1_SET);
  877. /* DLL_ENABLE must be set to 0 when setting RDN_DELAY or HALF_PERIOD. */
  878. writel(BM_GPMI_CTRL1_DLL_ENABLE, gpmi_regs + HW_GPMI_CTRL1_CLR);
  879. /* Clear out the DLL control fields. */
  880. reg = BM_GPMI_CTRL1_RDN_DELAY | BM_GPMI_CTRL1_HALF_PERIOD;
  881. writel(reg, gpmi_regs + HW_GPMI_CTRL1_CLR);
  882. /* If no sample delay is called for, return immediately. */
  883. if (!hw.sample_delay_factor)
  884. return;
  885. /* Set RDN_DELAY or HALF_PERIOD. */
  886. reg = ((hw.use_half_periods) ? BM_GPMI_CTRL1_HALF_PERIOD : 0)
  887. | BF_GPMI_CTRL1_RDN_DELAY(hw.sample_delay_factor);
  888. writel(reg, gpmi_regs + HW_GPMI_CTRL1_SET);
  889. /* At last, we enable the DLL. */
  890. writel(BM_GPMI_CTRL1_DLL_ENABLE, gpmi_regs + HW_GPMI_CTRL1_SET);
  891. /*
  892. * After we enable the GPMI DLL, we have to wait 64 clock cycles before
  893. * we can use the GPMI. Calculate the amount of time we need to wait,
  894. * in microseconds.
  895. */
  896. clock_period_in_ns = NSEC_PER_SEC / clk_get_rate(r->clock[0]);
  897. dll_wait_time_in_us = (clock_period_in_ns * 64) / 1000;
  898. if (!dll_wait_time_in_us)
  899. dll_wait_time_in_us = 1;
  900. /* Wait for the DLL to settle. */
  901. udelay(dll_wait_time_in_us);
  902. err_out:
  903. return;
  904. }
  905. void gpmi_end(struct gpmi_nand_data *this)
  906. {
  907. gpmi_disable_clk(this);
  908. }
  909. /* Clears a BCH interrupt. */
  910. void gpmi_clear_bch(struct gpmi_nand_data *this)
  911. {
  912. struct resources *r = &this->resources;
  913. writel(BM_BCH_CTRL_COMPLETE_IRQ, r->bch_regs + HW_BCH_CTRL_CLR);
  914. }
  915. /* Returns the Ready/Busy status of the given chip. */
  916. int gpmi_is_ready(struct gpmi_nand_data *this, unsigned chip)
  917. {
  918. struct resources *r = &this->resources;
  919. uint32_t mask = 0;
  920. uint32_t reg = 0;
  921. if (GPMI_IS_MX23(this)) {
  922. mask = MX23_BM_GPMI_DEBUG_READY0 << chip;
  923. reg = readl(r->gpmi_regs + HW_GPMI_DEBUG);
  924. } else if (GPMI_IS_MX28(this) || GPMI_IS_MX6Q(this)) {
  925. /* MX28 shares the same R/B register as MX6Q. */
  926. mask = MX28_BF_GPMI_STAT_READY_BUSY(1 << chip);
  927. reg = readl(r->gpmi_regs + HW_GPMI_STAT);
  928. } else
  929. pr_err("unknow arch.\n");
  930. return reg & mask;
  931. }
  932. static inline void set_dma_type(struct gpmi_nand_data *this,
  933. enum dma_ops_type type)
  934. {
  935. this->last_dma_type = this->dma_type;
  936. this->dma_type = type;
  937. }
  938. int gpmi_send_command(struct gpmi_nand_data *this)
  939. {
  940. struct dma_chan *channel = get_dma_chan(this);
  941. struct dma_async_tx_descriptor *desc;
  942. struct scatterlist *sgl;
  943. int chip = this->current_chip;
  944. u32 pio[3];
  945. /* [1] send out the PIO words */
  946. pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__WRITE)
  947. | BM_GPMI_CTRL0_WORD_LENGTH
  948. | BF_GPMI_CTRL0_CS(chip, this)
  949. | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
  950. | BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_CLE)
  951. | BM_GPMI_CTRL0_ADDRESS_INCREMENT
  952. | BF_GPMI_CTRL0_XFER_COUNT(this->command_length);
  953. pio[1] = pio[2] = 0;
  954. desc = dmaengine_prep_slave_sg(channel,
  955. (struct scatterlist *)pio,
  956. ARRAY_SIZE(pio), DMA_TRANS_NONE, 0);
  957. if (!desc) {
  958. pr_err("step 1 error\n");
  959. return -1;
  960. }
  961. /* [2] send out the COMMAND + ADDRESS string stored in @buffer */
  962. sgl = &this->cmd_sgl;
  963. sg_init_one(sgl, this->cmd_buffer, this->command_length);
  964. dma_map_sg(this->dev, sgl, 1, DMA_TO_DEVICE);
  965. desc = dmaengine_prep_slave_sg(channel,
  966. sgl, 1, DMA_MEM_TO_DEV,
  967. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  968. if (!desc) {
  969. pr_err("step 2 error\n");
  970. return -1;
  971. }
  972. /* [3] submit the DMA */
  973. set_dma_type(this, DMA_FOR_COMMAND);
  974. return start_dma_without_bch_irq(this, desc);
  975. }
  976. int gpmi_send_data(struct gpmi_nand_data *this)
  977. {
  978. struct dma_async_tx_descriptor *desc;
  979. struct dma_chan *channel = get_dma_chan(this);
  980. int chip = this->current_chip;
  981. uint32_t command_mode;
  982. uint32_t address;
  983. u32 pio[2];
  984. /* [1] PIO */
  985. command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WRITE;
  986. address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
  987. pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
  988. | BM_GPMI_CTRL0_WORD_LENGTH
  989. | BF_GPMI_CTRL0_CS(chip, this)
  990. | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
  991. | BF_GPMI_CTRL0_ADDRESS(address)
  992. | BF_GPMI_CTRL0_XFER_COUNT(this->upper_len);
  993. pio[1] = 0;
  994. desc = dmaengine_prep_slave_sg(channel, (struct scatterlist *)pio,
  995. ARRAY_SIZE(pio), DMA_TRANS_NONE, 0);
  996. if (!desc) {
  997. pr_err("step 1 error\n");
  998. return -1;
  999. }
  1000. /* [2] send DMA request */
  1001. prepare_data_dma(this, DMA_TO_DEVICE);
  1002. desc = dmaengine_prep_slave_sg(channel, &this->data_sgl,
  1003. 1, DMA_MEM_TO_DEV,
  1004. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  1005. if (!desc) {
  1006. pr_err("step 2 error\n");
  1007. return -1;
  1008. }
  1009. /* [3] submit the DMA */
  1010. set_dma_type(this, DMA_FOR_WRITE_DATA);
  1011. return start_dma_without_bch_irq(this, desc);
  1012. }
  1013. int gpmi_read_data(struct gpmi_nand_data *this)
  1014. {
  1015. struct dma_async_tx_descriptor *desc;
  1016. struct dma_chan *channel = get_dma_chan(this);
  1017. int chip = this->current_chip;
  1018. u32 pio[2];
  1019. /* [1] : send PIO */
  1020. pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__READ)
  1021. | BM_GPMI_CTRL0_WORD_LENGTH
  1022. | BF_GPMI_CTRL0_CS(chip, this)
  1023. | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
  1024. | BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_DATA)
  1025. | BF_GPMI_CTRL0_XFER_COUNT(this->upper_len);
  1026. pio[1] = 0;
  1027. desc = dmaengine_prep_slave_sg(channel,
  1028. (struct scatterlist *)pio,
  1029. ARRAY_SIZE(pio), DMA_TRANS_NONE, 0);
  1030. if (!desc) {
  1031. pr_err("step 1 error\n");
  1032. return -1;
  1033. }
  1034. /* [2] : send DMA request */
  1035. prepare_data_dma(this, DMA_FROM_DEVICE);
  1036. desc = dmaengine_prep_slave_sg(channel, &this->data_sgl,
  1037. 1, DMA_DEV_TO_MEM,
  1038. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  1039. if (!desc) {
  1040. pr_err("step 2 error\n");
  1041. return -1;
  1042. }
  1043. /* [3] : submit the DMA */
  1044. set_dma_type(this, DMA_FOR_READ_DATA);
  1045. return start_dma_without_bch_irq(this, desc);
  1046. }
  1047. int gpmi_send_page(struct gpmi_nand_data *this,
  1048. dma_addr_t payload, dma_addr_t auxiliary)
  1049. {
  1050. struct bch_geometry *geo = &this->bch_geometry;
  1051. uint32_t command_mode;
  1052. uint32_t address;
  1053. uint32_t ecc_command;
  1054. uint32_t buffer_mask;
  1055. struct dma_async_tx_descriptor *desc;
  1056. struct dma_chan *channel = get_dma_chan(this);
  1057. int chip = this->current_chip;
  1058. u32 pio[6];
  1059. /* A DMA descriptor that does an ECC page read. */
  1060. command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WRITE;
  1061. address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
  1062. ecc_command = BV_GPMI_ECCCTRL_ECC_CMD__BCH_ENCODE;
  1063. buffer_mask = BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_PAGE |
  1064. BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_AUXONLY;
  1065. pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
  1066. | BM_GPMI_CTRL0_WORD_LENGTH
  1067. | BF_GPMI_CTRL0_CS(chip, this)
  1068. | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
  1069. | BF_GPMI_CTRL0_ADDRESS(address)
  1070. | BF_GPMI_CTRL0_XFER_COUNT(0);
  1071. pio[1] = 0;
  1072. pio[2] = BM_GPMI_ECCCTRL_ENABLE_ECC
  1073. | BF_GPMI_ECCCTRL_ECC_CMD(ecc_command)
  1074. | BF_GPMI_ECCCTRL_BUFFER_MASK(buffer_mask);
  1075. pio[3] = geo->page_size;
  1076. pio[4] = payload;
  1077. pio[5] = auxiliary;
  1078. desc = dmaengine_prep_slave_sg(channel,
  1079. (struct scatterlist *)pio,
  1080. ARRAY_SIZE(pio), DMA_TRANS_NONE,
  1081. DMA_CTRL_ACK);
  1082. if (!desc) {
  1083. pr_err("step 2 error\n");
  1084. return -1;
  1085. }
  1086. set_dma_type(this, DMA_FOR_WRITE_ECC_PAGE);
  1087. return start_dma_with_bch_irq(this, desc);
  1088. }
  1089. int gpmi_read_page(struct gpmi_nand_data *this,
  1090. dma_addr_t payload, dma_addr_t auxiliary)
  1091. {
  1092. struct bch_geometry *geo = &this->bch_geometry;
  1093. uint32_t command_mode;
  1094. uint32_t address;
  1095. uint32_t ecc_command;
  1096. uint32_t buffer_mask;
  1097. struct dma_async_tx_descriptor *desc;
  1098. struct dma_chan *channel = get_dma_chan(this);
  1099. int chip = this->current_chip;
  1100. u32 pio[6];
  1101. /* [1] Wait for the chip to report ready. */
  1102. command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WAIT_FOR_READY;
  1103. address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
  1104. pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
  1105. | BM_GPMI_CTRL0_WORD_LENGTH
  1106. | BF_GPMI_CTRL0_CS(chip, this)
  1107. | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
  1108. | BF_GPMI_CTRL0_ADDRESS(address)
  1109. | BF_GPMI_CTRL0_XFER_COUNT(0);
  1110. pio[1] = 0;
  1111. desc = dmaengine_prep_slave_sg(channel,
  1112. (struct scatterlist *)pio, 2,
  1113. DMA_TRANS_NONE, 0);
  1114. if (!desc) {
  1115. pr_err("step 1 error\n");
  1116. return -1;
  1117. }
  1118. /* [2] Enable the BCH block and read. */
  1119. command_mode = BV_GPMI_CTRL0_COMMAND_MODE__READ;
  1120. address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
  1121. ecc_command = BV_GPMI_ECCCTRL_ECC_CMD__BCH_DECODE;
  1122. buffer_mask = BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_PAGE
  1123. | BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_AUXONLY;
  1124. pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
  1125. | BM_GPMI_CTRL0_WORD_LENGTH
  1126. | BF_GPMI_CTRL0_CS(chip, this)
  1127. | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
  1128. | BF_GPMI_CTRL0_ADDRESS(address)
  1129. | BF_GPMI_CTRL0_XFER_COUNT(geo->page_size);
  1130. pio[1] = 0;
  1131. pio[2] = BM_GPMI_ECCCTRL_ENABLE_ECC
  1132. | BF_GPMI_ECCCTRL_ECC_CMD(ecc_command)
  1133. | BF_GPMI_ECCCTRL_BUFFER_MASK(buffer_mask);
  1134. pio[3] = geo->page_size;
  1135. pio[4] = payload;
  1136. pio[5] = auxiliary;
  1137. desc = dmaengine_prep_slave_sg(channel,
  1138. (struct scatterlist *)pio,
  1139. ARRAY_SIZE(pio), DMA_TRANS_NONE,
  1140. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  1141. if (!desc) {
  1142. pr_err("step 2 error\n");
  1143. return -1;
  1144. }
  1145. /* [3] Disable the BCH block */
  1146. command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WAIT_FOR_READY;
  1147. address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
  1148. pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
  1149. | BM_GPMI_CTRL0_WORD_LENGTH
  1150. | BF_GPMI_CTRL0_CS(chip, this)
  1151. | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
  1152. | BF_GPMI_CTRL0_ADDRESS(address)
  1153. | BF_GPMI_CTRL0_XFER_COUNT(geo->page_size);
  1154. pio[1] = 0;
  1155. pio[2] = 0; /* clear GPMI_HW_GPMI_ECCCTRL, disable the BCH. */
  1156. desc = dmaengine_prep_slave_sg(channel,
  1157. (struct scatterlist *)pio, 3,
  1158. DMA_TRANS_NONE,
  1159. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  1160. if (!desc) {
  1161. pr_err("step 3 error\n");
  1162. return -1;
  1163. }
  1164. /* [4] submit the DMA */
  1165. set_dma_type(this, DMA_FOR_READ_ECC_PAGE);
  1166. return start_dma_with_bch_irq(this, desc);
  1167. }