gpmi-lib.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  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 <mach/mxs.h>
  25. #include "gpmi-nand.h"
  26. #include "gpmi-regs.h"
  27. #include "bch-regs.h"
  28. struct timing_threshod timing_default_threshold = {
  29. .max_data_setup_cycles = (BM_GPMI_TIMING0_DATA_SETUP >>
  30. BP_GPMI_TIMING0_DATA_SETUP),
  31. .internal_data_setup_in_ns = 0,
  32. .max_sample_delay_factor = (BM_GPMI_CTRL1_RDN_DELAY >>
  33. BP_GPMI_CTRL1_RDN_DELAY),
  34. .max_dll_clock_period_in_ns = 32,
  35. .max_dll_delay_in_ns = 16,
  36. };
  37. /*
  38. * Clear the bit and poll it cleared. This is usually called with
  39. * a reset address and mask being either SFTRST(bit 31) or CLKGATE
  40. * (bit 30).
  41. */
  42. static int clear_poll_bit(void __iomem *addr, u32 mask)
  43. {
  44. int timeout = 0x400;
  45. /* clear the bit */
  46. __mxs_clrl(mask, addr);
  47. /*
  48. * SFTRST needs 3 GPMI clocks to settle, the reference manual
  49. * recommends to wait 1us.
  50. */
  51. udelay(1);
  52. /* poll the bit becoming clear */
  53. while ((readl(addr) & mask) && --timeout)
  54. /* nothing */;
  55. return !timeout;
  56. }
  57. #define MODULE_CLKGATE (1 << 30)
  58. #define MODULE_SFTRST (1 << 31)
  59. /*
  60. * The current mxs_reset_block() will do two things:
  61. * [1] enable the module.
  62. * [2] reset the module.
  63. *
  64. * In most of the cases, it's ok. But there is a hardware bug in the BCH block.
  65. * If you try to soft reset the BCH block, it becomes unusable until
  66. * the next hard reset. This case occurs in the NAND boot mode. When the board
  67. * boots by NAND, the ROM of the chip will initialize the BCH blocks itself.
  68. * So If the driver tries to reset the BCH again, the BCH will not work anymore.
  69. * You will see a DMA timeout in this case.
  70. *
  71. * To avoid this bug, just add a new parameter `just_enable` for
  72. * the mxs_reset_block(), and rewrite it here.
  73. */
  74. int gpmi_reset_block(void __iomem *reset_addr, bool just_enable)
  75. {
  76. int ret;
  77. int timeout = 0x400;
  78. /* clear and poll SFTRST */
  79. ret = clear_poll_bit(reset_addr, MODULE_SFTRST);
  80. if (unlikely(ret))
  81. goto error;
  82. /* clear CLKGATE */
  83. __mxs_clrl(MODULE_CLKGATE, reset_addr);
  84. if (!just_enable) {
  85. /* set SFTRST to reset the block */
  86. __mxs_setl(MODULE_SFTRST, reset_addr);
  87. udelay(1);
  88. /* poll CLKGATE becoming set */
  89. while ((!(readl(reset_addr) & MODULE_CLKGATE)) && --timeout)
  90. /* nothing */;
  91. if (unlikely(!timeout))
  92. goto error;
  93. }
  94. /* clear and poll SFTRST */
  95. ret = clear_poll_bit(reset_addr, MODULE_SFTRST);
  96. if (unlikely(ret))
  97. goto error;
  98. /* clear and poll CLKGATE */
  99. ret = clear_poll_bit(reset_addr, MODULE_CLKGATE);
  100. if (unlikely(ret))
  101. goto error;
  102. return 0;
  103. error:
  104. pr_err("%s(%p): module reset timeout\n", __func__, reset_addr);
  105. return -ETIMEDOUT;
  106. }
  107. int gpmi_init(struct gpmi_nand_data *this)
  108. {
  109. struct resources *r = &this->resources;
  110. int ret;
  111. ret = clk_prepare_enable(r->clock);
  112. if (ret)
  113. goto err_out;
  114. ret = gpmi_reset_block(r->gpmi_regs, false);
  115. if (ret)
  116. goto err_out;
  117. /* Choose NAND mode. */
  118. writel(BM_GPMI_CTRL1_GPMI_MODE, r->gpmi_regs + HW_GPMI_CTRL1_CLR);
  119. /* Set the IRQ polarity. */
  120. writel(BM_GPMI_CTRL1_ATA_IRQRDY_POLARITY,
  121. r->gpmi_regs + HW_GPMI_CTRL1_SET);
  122. /* Disable Write-Protection. */
  123. writel(BM_GPMI_CTRL1_DEV_RESET, r->gpmi_regs + HW_GPMI_CTRL1_SET);
  124. /* Select BCH ECC. */
  125. writel(BM_GPMI_CTRL1_BCH_MODE, r->gpmi_regs + HW_GPMI_CTRL1_SET);
  126. clk_disable_unprepare(r->clock);
  127. return 0;
  128. err_out:
  129. return ret;
  130. }
  131. /* This function is very useful. It is called only when the bug occur. */
  132. void gpmi_dump_info(struct gpmi_nand_data *this)
  133. {
  134. struct resources *r = &this->resources;
  135. struct bch_geometry *geo = &this->bch_geometry;
  136. u32 reg;
  137. int i;
  138. pr_err("Show GPMI registers :\n");
  139. for (i = 0; i <= HW_GPMI_DEBUG / 0x10 + 1; i++) {
  140. reg = readl(r->gpmi_regs + i * 0x10);
  141. pr_err("offset 0x%.3x : 0x%.8x\n", i * 0x10, reg);
  142. }
  143. /* start to print out the BCH info */
  144. pr_err("BCH Geometry :\n");
  145. pr_err("GF length : %u\n", geo->gf_len);
  146. pr_err("ECC Strength : %u\n", geo->ecc_strength);
  147. pr_err("Page Size in Bytes : %u\n", geo->page_size);
  148. pr_err("Metadata Size in Bytes : %u\n", geo->metadata_size);
  149. pr_err("ECC Chunk Size in Bytes: %u\n", geo->ecc_chunk_size);
  150. pr_err("ECC Chunk Count : %u\n", geo->ecc_chunk_count);
  151. pr_err("Payload Size in Bytes : %u\n", geo->payload_size);
  152. pr_err("Auxiliary Size in Bytes: %u\n", geo->auxiliary_size);
  153. pr_err("Auxiliary Status Offset: %u\n", geo->auxiliary_status_offset);
  154. pr_err("Block Mark Byte Offset : %u\n", geo->block_mark_byte_offset);
  155. pr_err("Block Mark Bit Offset : %u\n", geo->block_mark_bit_offset);
  156. }
  157. /* Configures the geometry for BCH. */
  158. int bch_set_geometry(struct gpmi_nand_data *this)
  159. {
  160. struct resources *r = &this->resources;
  161. struct bch_geometry *bch_geo = &this->bch_geometry;
  162. unsigned int block_count;
  163. unsigned int block_size;
  164. unsigned int metadata_size;
  165. unsigned int ecc_strength;
  166. unsigned int page_size;
  167. int ret;
  168. if (common_nfc_set_geometry(this))
  169. return !0;
  170. block_count = bch_geo->ecc_chunk_count - 1;
  171. block_size = bch_geo->ecc_chunk_size;
  172. metadata_size = bch_geo->metadata_size;
  173. ecc_strength = bch_geo->ecc_strength >> 1;
  174. page_size = bch_geo->page_size;
  175. ret = clk_prepare_enable(r->clock);
  176. if (ret)
  177. goto err_out;
  178. ret = gpmi_reset_block(r->bch_regs, true);
  179. if (ret)
  180. goto err_out;
  181. /* Configure layout 0. */
  182. writel(BF_BCH_FLASH0LAYOUT0_NBLOCKS(block_count)
  183. | BF_BCH_FLASH0LAYOUT0_META_SIZE(metadata_size)
  184. | BF_BCH_FLASH0LAYOUT0_ECC0(ecc_strength)
  185. | BF_BCH_FLASH0LAYOUT0_DATA0_SIZE(block_size),
  186. r->bch_regs + HW_BCH_FLASH0LAYOUT0);
  187. writel(BF_BCH_FLASH0LAYOUT1_PAGE_SIZE(page_size)
  188. | BF_BCH_FLASH0LAYOUT1_ECCN(ecc_strength)
  189. | BF_BCH_FLASH0LAYOUT1_DATAN_SIZE(block_size),
  190. r->bch_regs + HW_BCH_FLASH0LAYOUT1);
  191. /* Set *all* chip selects to use layout 0. */
  192. writel(0, r->bch_regs + HW_BCH_LAYOUTSELECT);
  193. /* Enable interrupts. */
  194. writel(BM_BCH_CTRL_COMPLETE_IRQ_EN,
  195. r->bch_regs + HW_BCH_CTRL_SET);
  196. clk_disable_unprepare(r->clock);
  197. return 0;
  198. err_out:
  199. return ret;
  200. }
  201. /* Converts time in nanoseconds to cycles. */
  202. static unsigned int ns_to_cycles(unsigned int time,
  203. unsigned int period, unsigned int min)
  204. {
  205. unsigned int k;
  206. k = (time + period - 1) / period;
  207. return max(k, min);
  208. }
  209. /* Apply timing to current hardware conditions. */
  210. static int gpmi_nfc_compute_hardware_timing(struct gpmi_nand_data *this,
  211. struct gpmi_nfc_hardware_timing *hw)
  212. {
  213. struct gpmi_nand_platform_data *pdata = this->pdata;
  214. struct timing_threshod *nfc = &timing_default_threshold;
  215. struct nand_chip *nand = &this->nand;
  216. struct nand_timing target = this->timing;
  217. bool improved_timing_is_available;
  218. unsigned long clock_frequency_in_hz;
  219. unsigned int clock_period_in_ns;
  220. bool dll_use_half_periods;
  221. unsigned int dll_delay_shift;
  222. unsigned int max_sample_delay_in_ns;
  223. unsigned int address_setup_in_cycles;
  224. unsigned int data_setup_in_ns;
  225. unsigned int data_setup_in_cycles;
  226. unsigned int data_hold_in_cycles;
  227. int ideal_sample_delay_in_ns;
  228. unsigned int sample_delay_factor;
  229. int tEYE;
  230. unsigned int min_prop_delay_in_ns = pdata->min_prop_delay_in_ns;
  231. unsigned int max_prop_delay_in_ns = pdata->max_prop_delay_in_ns;
  232. /*
  233. * If there are multiple chips, we need to relax the timings to allow
  234. * for signal distortion due to higher capacitance.
  235. */
  236. if (nand->numchips > 2) {
  237. target.data_setup_in_ns += 10;
  238. target.data_hold_in_ns += 10;
  239. target.address_setup_in_ns += 10;
  240. } else if (nand->numchips > 1) {
  241. target.data_setup_in_ns += 5;
  242. target.data_hold_in_ns += 5;
  243. target.address_setup_in_ns += 5;
  244. }
  245. /* Check if improved timing information is available. */
  246. improved_timing_is_available =
  247. (target.tREA_in_ns >= 0) &&
  248. (target.tRLOH_in_ns >= 0) &&
  249. (target.tRHOH_in_ns >= 0) ;
  250. /* Inspect the clock. */
  251. clock_frequency_in_hz = nfc->clock_frequency_in_hz;
  252. clock_period_in_ns = 1000000000 / clock_frequency_in_hz;
  253. /*
  254. * The NFC quantizes setup and hold parameters in terms of clock cycles.
  255. * Here, we quantize the setup and hold timing parameters to the
  256. * next-highest clock period to make sure we apply at least the
  257. * specified times.
  258. *
  259. * For data setup and data hold, the hardware interprets a value of zero
  260. * as the largest possible delay. This is not what's intended by a zero
  261. * in the input parameter, so we impose a minimum of one cycle.
  262. */
  263. data_setup_in_cycles = ns_to_cycles(target.data_setup_in_ns,
  264. clock_period_in_ns, 1);
  265. data_hold_in_cycles = ns_to_cycles(target.data_hold_in_ns,
  266. clock_period_in_ns, 1);
  267. address_setup_in_cycles = ns_to_cycles(target.address_setup_in_ns,
  268. clock_period_in_ns, 0);
  269. /*
  270. * The clock's period affects the sample delay in a number of ways:
  271. *
  272. * (1) The NFC HAL tells us the maximum clock period the sample delay
  273. * DLL can tolerate. If the clock period is greater than half that
  274. * maximum, we must configure the DLL to be driven by half periods.
  275. *
  276. * (2) We need to convert from an ideal sample delay, in ns, to a
  277. * "sample delay factor," which the NFC uses. This factor depends on
  278. * whether we're driving the DLL with full or half periods.
  279. * Paraphrasing the reference manual:
  280. *
  281. * AD = SDF x 0.125 x RP
  282. *
  283. * where:
  284. *
  285. * AD is the applied delay, in ns.
  286. * SDF is the sample delay factor, which is dimensionless.
  287. * RP is the reference period, in ns, which is a full clock period
  288. * if the DLL is being driven by full periods, or half that if
  289. * the DLL is being driven by half periods.
  290. *
  291. * Let's re-arrange this in a way that's more useful to us:
  292. *
  293. * 8
  294. * SDF = AD x ----
  295. * RP
  296. *
  297. * The reference period is either the clock period or half that, so this
  298. * is:
  299. *
  300. * 8 AD x DDF
  301. * SDF = AD x ----- = --------
  302. * f x P P
  303. *
  304. * where:
  305. *
  306. * f is 1 or 1/2, depending on how we're driving the DLL.
  307. * P is the clock period.
  308. * DDF is the DLL Delay Factor, a dimensionless value that
  309. * incorporates all the constants in the conversion.
  310. *
  311. * DDF will be either 8 or 16, both of which are powers of two. We can
  312. * reduce the cost of this conversion by using bit shifts instead of
  313. * multiplication or division. Thus:
  314. *
  315. * AD << DDS
  316. * SDF = ---------
  317. * P
  318. *
  319. * or
  320. *
  321. * AD = (SDF >> DDS) x P
  322. *
  323. * where:
  324. *
  325. * DDS is the DLL Delay Shift, the logarithm to base 2 of the DDF.
  326. */
  327. if (clock_period_in_ns > (nfc->max_dll_clock_period_in_ns >> 1)) {
  328. dll_use_half_periods = true;
  329. dll_delay_shift = 3 + 1;
  330. } else {
  331. dll_use_half_periods = false;
  332. dll_delay_shift = 3;
  333. }
  334. /*
  335. * Compute the maximum sample delay the NFC allows, under current
  336. * conditions. If the clock is running too slowly, no sample delay is
  337. * possible.
  338. */
  339. if (clock_period_in_ns > nfc->max_dll_clock_period_in_ns)
  340. max_sample_delay_in_ns = 0;
  341. else {
  342. /*
  343. * Compute the delay implied by the largest sample delay factor
  344. * the NFC allows.
  345. */
  346. max_sample_delay_in_ns =
  347. (nfc->max_sample_delay_factor * clock_period_in_ns) >>
  348. dll_delay_shift;
  349. /*
  350. * Check if the implied sample delay larger than the NFC
  351. * actually allows.
  352. */
  353. if (max_sample_delay_in_ns > nfc->max_dll_delay_in_ns)
  354. max_sample_delay_in_ns = nfc->max_dll_delay_in_ns;
  355. }
  356. /*
  357. * Check if improved timing information is available. If not, we have to
  358. * use a less-sophisticated algorithm.
  359. */
  360. if (!improved_timing_is_available) {
  361. /*
  362. * Fold the read setup time required by the NFC into the ideal
  363. * sample delay.
  364. */
  365. ideal_sample_delay_in_ns = target.gpmi_sample_delay_in_ns +
  366. nfc->internal_data_setup_in_ns;
  367. /*
  368. * The ideal sample delay may be greater than the maximum
  369. * allowed by the NFC. If so, we can trade off sample delay time
  370. * for more data setup time.
  371. *
  372. * In each iteration of the following loop, we add a cycle to
  373. * the data setup time and subtract a corresponding amount from
  374. * the sample delay until we've satisified the constraints or
  375. * can't do any better.
  376. */
  377. while ((ideal_sample_delay_in_ns > max_sample_delay_in_ns) &&
  378. (data_setup_in_cycles < nfc->max_data_setup_cycles)) {
  379. data_setup_in_cycles++;
  380. ideal_sample_delay_in_ns -= clock_period_in_ns;
  381. if (ideal_sample_delay_in_ns < 0)
  382. ideal_sample_delay_in_ns = 0;
  383. }
  384. /*
  385. * Compute the sample delay factor that corresponds most closely
  386. * to the ideal sample delay. If the result is too large for the
  387. * NFC, use the maximum value.
  388. *
  389. * Notice that we use the ns_to_cycles function to compute the
  390. * sample delay factor. We do this because the form of the
  391. * computation is the same as that for calculating cycles.
  392. */
  393. sample_delay_factor =
  394. ns_to_cycles(
  395. ideal_sample_delay_in_ns << dll_delay_shift,
  396. clock_period_in_ns, 0);
  397. if (sample_delay_factor > nfc->max_sample_delay_factor)
  398. sample_delay_factor = nfc->max_sample_delay_factor;
  399. /* Skip to the part where we return our results. */
  400. goto return_results;
  401. }
  402. /*
  403. * If control arrives here, we have more detailed timing information,
  404. * so we can use a better algorithm.
  405. */
  406. /*
  407. * Fold the read setup time required by the NFC into the maximum
  408. * propagation delay.
  409. */
  410. max_prop_delay_in_ns += nfc->internal_data_setup_in_ns;
  411. /*
  412. * Earlier, we computed the number of clock cycles required to satisfy
  413. * the data setup time. Now, we need to know the actual nanoseconds.
  414. */
  415. data_setup_in_ns = clock_period_in_ns * data_setup_in_cycles;
  416. /*
  417. * Compute tEYE, the width of the data eye when reading from the NAND
  418. * Flash. The eye width is fundamentally determined by the data setup
  419. * time, perturbed by propagation delays and some characteristics of the
  420. * NAND Flash device.
  421. *
  422. * start of the eye = max_prop_delay + tREA
  423. * end of the eye = min_prop_delay + tRHOH + data_setup
  424. */
  425. tEYE = (int)min_prop_delay_in_ns + (int)target.tRHOH_in_ns +
  426. (int)data_setup_in_ns;
  427. tEYE -= (int)max_prop_delay_in_ns + (int)target.tREA_in_ns;
  428. /*
  429. * The eye must be open. If it's not, we can try to open it by
  430. * increasing its main forcer, the data setup time.
  431. *
  432. * In each iteration of the following loop, we increase the data setup
  433. * time by a single clock cycle. We do this until either the eye is
  434. * open or we run into NFC limits.
  435. */
  436. while ((tEYE <= 0) &&
  437. (data_setup_in_cycles < nfc->max_data_setup_cycles)) {
  438. /* Give a cycle to data setup. */
  439. data_setup_in_cycles++;
  440. /* Synchronize the data setup time with the cycles. */
  441. data_setup_in_ns += clock_period_in_ns;
  442. /* Adjust tEYE accordingly. */
  443. tEYE += clock_period_in_ns;
  444. }
  445. /*
  446. * When control arrives here, the eye is open. The ideal time to sample
  447. * the data is in the center of the eye:
  448. *
  449. * end of the eye + start of the eye
  450. * --------------------------------- - data_setup
  451. * 2
  452. *
  453. * After some algebra, this simplifies to the code immediately below.
  454. */
  455. ideal_sample_delay_in_ns =
  456. ((int)max_prop_delay_in_ns +
  457. (int)target.tREA_in_ns +
  458. (int)min_prop_delay_in_ns +
  459. (int)target.tRHOH_in_ns -
  460. (int)data_setup_in_ns) >> 1;
  461. /*
  462. * The following figure illustrates some aspects of a NAND Flash read:
  463. *
  464. *
  465. * __ _____________________________________
  466. * RDN \_________________/
  467. *
  468. * <---- tEYE ----->
  469. * /-----------------\
  470. * Read Data ----------------------------< >---------
  471. * \-----------------/
  472. * ^ ^ ^ ^
  473. * | | | |
  474. * |<--Data Setup -->|<--Delay Time -->| |
  475. * | | | |
  476. * | | |
  477. * | |<-- Quantized Delay Time -->|
  478. * | | |
  479. *
  480. *
  481. * We have some issues we must now address:
  482. *
  483. * (1) The *ideal* sample delay time must not be negative. If it is, we
  484. * jam it to zero.
  485. *
  486. * (2) The *ideal* sample delay time must not be greater than that
  487. * allowed by the NFC. If it is, we can increase the data setup
  488. * time, which will reduce the delay between the end of the data
  489. * setup and the center of the eye. It will also make the eye
  490. * larger, which might help with the next issue...
  491. *
  492. * (3) The *quantized* sample delay time must not fall either before the
  493. * eye opens or after it closes (the latter is the problem
  494. * illustrated in the above figure).
  495. */
  496. /* Jam a negative ideal sample delay to zero. */
  497. if (ideal_sample_delay_in_ns < 0)
  498. ideal_sample_delay_in_ns = 0;
  499. /*
  500. * Extend the data setup as needed to reduce the ideal sample delay
  501. * below the maximum permitted by the NFC.
  502. */
  503. while ((ideal_sample_delay_in_ns > max_sample_delay_in_ns) &&
  504. (data_setup_in_cycles < nfc->max_data_setup_cycles)) {
  505. /* Give a cycle to data setup. */
  506. data_setup_in_cycles++;
  507. /* Synchronize the data setup time with the cycles. */
  508. data_setup_in_ns += clock_period_in_ns;
  509. /* Adjust tEYE accordingly. */
  510. tEYE += clock_period_in_ns;
  511. /*
  512. * Decrease the ideal sample delay by one half cycle, to keep it
  513. * in the middle of the eye.
  514. */
  515. ideal_sample_delay_in_ns -= (clock_period_in_ns >> 1);
  516. /* Jam a negative ideal sample delay to zero. */
  517. if (ideal_sample_delay_in_ns < 0)
  518. ideal_sample_delay_in_ns = 0;
  519. }
  520. /*
  521. * Compute the sample delay factor that corresponds to the ideal sample
  522. * delay. If the result is too large, then use the maximum allowed
  523. * value.
  524. *
  525. * Notice that we use the ns_to_cycles function to compute the sample
  526. * delay factor. We do this because the form of the computation is the
  527. * same as that for calculating cycles.
  528. */
  529. sample_delay_factor =
  530. ns_to_cycles(ideal_sample_delay_in_ns << dll_delay_shift,
  531. clock_period_in_ns, 0);
  532. if (sample_delay_factor > nfc->max_sample_delay_factor)
  533. sample_delay_factor = nfc->max_sample_delay_factor;
  534. /*
  535. * These macros conveniently encapsulate a computation we'll use to
  536. * continuously evaluate whether or not the data sample delay is inside
  537. * the eye.
  538. */
  539. #define IDEAL_DELAY ((int) ideal_sample_delay_in_ns)
  540. #define QUANTIZED_DELAY \
  541. ((int) ((sample_delay_factor * clock_period_in_ns) >> \
  542. dll_delay_shift))
  543. #define DELAY_ERROR (abs(QUANTIZED_DELAY - IDEAL_DELAY))
  544. #define SAMPLE_IS_NOT_WITHIN_THE_EYE (DELAY_ERROR > (tEYE >> 1))
  545. /*
  546. * While the quantized sample time falls outside the eye, reduce the
  547. * sample delay or extend the data setup to move the sampling point back
  548. * toward the eye. Do not allow the number of data setup cycles to
  549. * exceed the maximum allowed by the NFC.
  550. */
  551. while (SAMPLE_IS_NOT_WITHIN_THE_EYE &&
  552. (data_setup_in_cycles < nfc->max_data_setup_cycles)) {
  553. /*
  554. * If control arrives here, the quantized sample delay falls
  555. * outside the eye. Check if it's before the eye opens, or after
  556. * the eye closes.
  557. */
  558. if (QUANTIZED_DELAY > IDEAL_DELAY) {
  559. /*
  560. * If control arrives here, the quantized sample delay
  561. * falls after the eye closes. Decrease the quantized
  562. * delay time and then go back to re-evaluate.
  563. */
  564. if (sample_delay_factor != 0)
  565. sample_delay_factor--;
  566. continue;
  567. }
  568. /*
  569. * If control arrives here, the quantized sample delay falls
  570. * before the eye opens. Shift the sample point by increasing
  571. * data setup time. This will also make the eye larger.
  572. */
  573. /* Give a cycle to data setup. */
  574. data_setup_in_cycles++;
  575. /* Synchronize the data setup time with the cycles. */
  576. data_setup_in_ns += clock_period_in_ns;
  577. /* Adjust tEYE accordingly. */
  578. tEYE += clock_period_in_ns;
  579. /*
  580. * Decrease the ideal sample delay by one half cycle, to keep it
  581. * in the middle of the eye.
  582. */
  583. ideal_sample_delay_in_ns -= (clock_period_in_ns >> 1);
  584. /* ...and one less period for the delay time. */
  585. ideal_sample_delay_in_ns -= clock_period_in_ns;
  586. /* Jam a negative ideal sample delay to zero. */
  587. if (ideal_sample_delay_in_ns < 0)
  588. ideal_sample_delay_in_ns = 0;
  589. /*
  590. * We have a new ideal sample delay, so re-compute the quantized
  591. * delay.
  592. */
  593. sample_delay_factor =
  594. ns_to_cycles(
  595. ideal_sample_delay_in_ns << dll_delay_shift,
  596. clock_period_in_ns, 0);
  597. if (sample_delay_factor > nfc->max_sample_delay_factor)
  598. sample_delay_factor = nfc->max_sample_delay_factor;
  599. }
  600. /* Control arrives here when we're ready to return our results. */
  601. return_results:
  602. hw->data_setup_in_cycles = data_setup_in_cycles;
  603. hw->data_hold_in_cycles = data_hold_in_cycles;
  604. hw->address_setup_in_cycles = address_setup_in_cycles;
  605. hw->use_half_periods = dll_use_half_periods;
  606. hw->sample_delay_factor = sample_delay_factor;
  607. /* Return success. */
  608. return 0;
  609. }
  610. /* Begin the I/O */
  611. void gpmi_begin(struct gpmi_nand_data *this)
  612. {
  613. struct resources *r = &this->resources;
  614. struct timing_threshod *nfc = &timing_default_threshold;
  615. unsigned char *gpmi_regs = r->gpmi_regs;
  616. unsigned int clock_period_in_ns;
  617. uint32_t reg;
  618. unsigned int dll_wait_time_in_us;
  619. struct gpmi_nfc_hardware_timing hw;
  620. int ret;
  621. /* Enable the clock. */
  622. ret = clk_prepare_enable(r->clock);
  623. if (ret) {
  624. pr_err("We failed in enable the clk\n");
  625. goto err_out;
  626. }
  627. /* set ready/busy timeout */
  628. writel(0x500 << BP_GPMI_TIMING1_BUSY_TIMEOUT,
  629. gpmi_regs + HW_GPMI_TIMING1);
  630. /* Get the timing information we need. */
  631. nfc->clock_frequency_in_hz = clk_get_rate(r->clock);
  632. clock_period_in_ns = 1000000000 / nfc->clock_frequency_in_hz;
  633. gpmi_nfc_compute_hardware_timing(this, &hw);
  634. /* Set up all the simple timing parameters. */
  635. reg = BF_GPMI_TIMING0_ADDRESS_SETUP(hw.address_setup_in_cycles) |
  636. BF_GPMI_TIMING0_DATA_HOLD(hw.data_hold_in_cycles) |
  637. BF_GPMI_TIMING0_DATA_SETUP(hw.data_setup_in_cycles) ;
  638. writel(reg, gpmi_regs + HW_GPMI_TIMING0);
  639. /*
  640. * DLL_ENABLE must be set to 0 when setting RDN_DELAY or HALF_PERIOD.
  641. */
  642. writel(BM_GPMI_CTRL1_DLL_ENABLE, gpmi_regs + HW_GPMI_CTRL1_CLR);
  643. /* Clear out the DLL control fields. */
  644. writel(BM_GPMI_CTRL1_RDN_DELAY, gpmi_regs + HW_GPMI_CTRL1_CLR);
  645. writel(BM_GPMI_CTRL1_HALF_PERIOD, gpmi_regs + HW_GPMI_CTRL1_CLR);
  646. /* If no sample delay is called for, return immediately. */
  647. if (!hw.sample_delay_factor)
  648. return;
  649. /* Configure the HALF_PERIOD flag. */
  650. if (hw.use_half_periods)
  651. writel(BM_GPMI_CTRL1_HALF_PERIOD,
  652. gpmi_regs + HW_GPMI_CTRL1_SET);
  653. /* Set the delay factor. */
  654. writel(BF_GPMI_CTRL1_RDN_DELAY(hw.sample_delay_factor),
  655. gpmi_regs + HW_GPMI_CTRL1_SET);
  656. /* Enable the DLL. */
  657. writel(BM_GPMI_CTRL1_DLL_ENABLE, gpmi_regs + HW_GPMI_CTRL1_SET);
  658. /*
  659. * After we enable the GPMI DLL, we have to wait 64 clock cycles before
  660. * we can use the GPMI.
  661. *
  662. * Calculate the amount of time we need to wait, in microseconds.
  663. */
  664. dll_wait_time_in_us = (clock_period_in_ns * 64) / 1000;
  665. if (!dll_wait_time_in_us)
  666. dll_wait_time_in_us = 1;
  667. /* Wait for the DLL to settle. */
  668. udelay(dll_wait_time_in_us);
  669. err_out:
  670. return;
  671. }
  672. void gpmi_end(struct gpmi_nand_data *this)
  673. {
  674. struct resources *r = &this->resources;
  675. clk_disable_unprepare(r->clock);
  676. }
  677. /* Clears a BCH interrupt. */
  678. void gpmi_clear_bch(struct gpmi_nand_data *this)
  679. {
  680. struct resources *r = &this->resources;
  681. writel(BM_BCH_CTRL_COMPLETE_IRQ, r->bch_regs + HW_BCH_CTRL_CLR);
  682. }
  683. /* Returns the Ready/Busy status of the given chip. */
  684. int gpmi_is_ready(struct gpmi_nand_data *this, unsigned chip)
  685. {
  686. struct resources *r = &this->resources;
  687. uint32_t mask = 0;
  688. uint32_t reg = 0;
  689. if (GPMI_IS_MX23(this)) {
  690. mask = MX23_BM_GPMI_DEBUG_READY0 << chip;
  691. reg = readl(r->gpmi_regs + HW_GPMI_DEBUG);
  692. } else if (GPMI_IS_MX28(this)) {
  693. mask = MX28_BF_GPMI_STAT_READY_BUSY(1 << chip);
  694. reg = readl(r->gpmi_regs + HW_GPMI_STAT);
  695. } else
  696. pr_err("unknow arch.\n");
  697. return reg & mask;
  698. }
  699. static inline void set_dma_type(struct gpmi_nand_data *this,
  700. enum dma_ops_type type)
  701. {
  702. this->last_dma_type = this->dma_type;
  703. this->dma_type = type;
  704. }
  705. int gpmi_send_command(struct gpmi_nand_data *this)
  706. {
  707. struct dma_chan *channel = get_dma_chan(this);
  708. struct dma_async_tx_descriptor *desc;
  709. struct scatterlist *sgl;
  710. int chip = this->current_chip;
  711. u32 pio[3];
  712. /* [1] send out the PIO words */
  713. pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__WRITE)
  714. | BM_GPMI_CTRL0_WORD_LENGTH
  715. | BF_GPMI_CTRL0_CS(chip, this)
  716. | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
  717. | BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_CLE)
  718. | BM_GPMI_CTRL0_ADDRESS_INCREMENT
  719. | BF_GPMI_CTRL0_XFER_COUNT(this->command_length);
  720. pio[1] = pio[2] = 0;
  721. desc = channel->device->device_prep_slave_sg(channel,
  722. (struct scatterlist *)pio,
  723. ARRAY_SIZE(pio), DMA_TRANS_NONE, 0);
  724. if (!desc) {
  725. pr_err("step 1 error\n");
  726. return -1;
  727. }
  728. /* [2] send out the COMMAND + ADDRESS string stored in @buffer */
  729. sgl = &this->cmd_sgl;
  730. sg_init_one(sgl, this->cmd_buffer, this->command_length);
  731. dma_map_sg(this->dev, sgl, 1, DMA_TO_DEVICE);
  732. desc = channel->device->device_prep_slave_sg(channel,
  733. sgl, 1, DMA_MEM_TO_DEV, 1);
  734. if (!desc) {
  735. pr_err("step 2 error\n");
  736. return -1;
  737. }
  738. /* [3] submit the DMA */
  739. set_dma_type(this, DMA_FOR_COMMAND);
  740. return start_dma_without_bch_irq(this, desc);
  741. }
  742. int gpmi_send_data(struct gpmi_nand_data *this)
  743. {
  744. struct dma_async_tx_descriptor *desc;
  745. struct dma_chan *channel = get_dma_chan(this);
  746. int chip = this->current_chip;
  747. uint32_t command_mode;
  748. uint32_t address;
  749. u32 pio[2];
  750. /* [1] PIO */
  751. command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WRITE;
  752. address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
  753. pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
  754. | BM_GPMI_CTRL0_WORD_LENGTH
  755. | BF_GPMI_CTRL0_CS(chip, this)
  756. | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
  757. | BF_GPMI_CTRL0_ADDRESS(address)
  758. | BF_GPMI_CTRL0_XFER_COUNT(this->upper_len);
  759. pio[1] = 0;
  760. desc = channel->device->device_prep_slave_sg(channel,
  761. (struct scatterlist *)pio,
  762. ARRAY_SIZE(pio), DMA_TRANS_NONE, 0);
  763. if (!desc) {
  764. pr_err("step 1 error\n");
  765. return -1;
  766. }
  767. /* [2] send DMA request */
  768. prepare_data_dma(this, DMA_TO_DEVICE);
  769. desc = channel->device->device_prep_slave_sg(channel, &this->data_sgl,
  770. 1, DMA_MEM_TO_DEV, 1);
  771. if (!desc) {
  772. pr_err("step 2 error\n");
  773. return -1;
  774. }
  775. /* [3] submit the DMA */
  776. set_dma_type(this, DMA_FOR_WRITE_DATA);
  777. return start_dma_without_bch_irq(this, desc);
  778. }
  779. int gpmi_read_data(struct gpmi_nand_data *this)
  780. {
  781. struct dma_async_tx_descriptor *desc;
  782. struct dma_chan *channel = get_dma_chan(this);
  783. int chip = this->current_chip;
  784. u32 pio[2];
  785. /* [1] : send PIO */
  786. pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__READ)
  787. | BM_GPMI_CTRL0_WORD_LENGTH
  788. | BF_GPMI_CTRL0_CS(chip, this)
  789. | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
  790. | BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_DATA)
  791. | BF_GPMI_CTRL0_XFER_COUNT(this->upper_len);
  792. pio[1] = 0;
  793. desc = channel->device->device_prep_slave_sg(channel,
  794. (struct scatterlist *)pio,
  795. ARRAY_SIZE(pio), DMA_TRANS_NONE, 0);
  796. if (!desc) {
  797. pr_err("step 1 error\n");
  798. return -1;
  799. }
  800. /* [2] : send DMA request */
  801. prepare_data_dma(this, DMA_FROM_DEVICE);
  802. desc = channel->device->device_prep_slave_sg(channel, &this->data_sgl,
  803. 1, DMA_DEV_TO_MEM, 1);
  804. if (!desc) {
  805. pr_err("step 2 error\n");
  806. return -1;
  807. }
  808. /* [3] : submit the DMA */
  809. set_dma_type(this, DMA_FOR_READ_DATA);
  810. return start_dma_without_bch_irq(this, desc);
  811. }
  812. int gpmi_send_page(struct gpmi_nand_data *this,
  813. dma_addr_t payload, dma_addr_t auxiliary)
  814. {
  815. struct bch_geometry *geo = &this->bch_geometry;
  816. uint32_t command_mode;
  817. uint32_t address;
  818. uint32_t ecc_command;
  819. uint32_t buffer_mask;
  820. struct dma_async_tx_descriptor *desc;
  821. struct dma_chan *channel = get_dma_chan(this);
  822. int chip = this->current_chip;
  823. u32 pio[6];
  824. /* A DMA descriptor that does an ECC page read. */
  825. command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WRITE;
  826. address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
  827. ecc_command = BV_GPMI_ECCCTRL_ECC_CMD__BCH_ENCODE;
  828. buffer_mask = BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_PAGE |
  829. BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_AUXONLY;
  830. pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
  831. | BM_GPMI_CTRL0_WORD_LENGTH
  832. | BF_GPMI_CTRL0_CS(chip, this)
  833. | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
  834. | BF_GPMI_CTRL0_ADDRESS(address)
  835. | BF_GPMI_CTRL0_XFER_COUNT(0);
  836. pio[1] = 0;
  837. pio[2] = BM_GPMI_ECCCTRL_ENABLE_ECC
  838. | BF_GPMI_ECCCTRL_ECC_CMD(ecc_command)
  839. | BF_GPMI_ECCCTRL_BUFFER_MASK(buffer_mask);
  840. pio[3] = geo->page_size;
  841. pio[4] = payload;
  842. pio[5] = auxiliary;
  843. desc = channel->device->device_prep_slave_sg(channel,
  844. (struct scatterlist *)pio,
  845. ARRAY_SIZE(pio), DMA_TRANS_NONE, 0);
  846. if (!desc) {
  847. pr_err("step 2 error\n");
  848. return -1;
  849. }
  850. set_dma_type(this, DMA_FOR_WRITE_ECC_PAGE);
  851. return start_dma_with_bch_irq(this, desc);
  852. }
  853. int gpmi_read_page(struct gpmi_nand_data *this,
  854. dma_addr_t payload, dma_addr_t auxiliary)
  855. {
  856. struct bch_geometry *geo = &this->bch_geometry;
  857. uint32_t command_mode;
  858. uint32_t address;
  859. uint32_t ecc_command;
  860. uint32_t buffer_mask;
  861. struct dma_async_tx_descriptor *desc;
  862. struct dma_chan *channel = get_dma_chan(this);
  863. int chip = this->current_chip;
  864. u32 pio[6];
  865. /* [1] Wait for the chip to report ready. */
  866. command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WAIT_FOR_READY;
  867. address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
  868. pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
  869. | BM_GPMI_CTRL0_WORD_LENGTH
  870. | BF_GPMI_CTRL0_CS(chip, this)
  871. | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
  872. | BF_GPMI_CTRL0_ADDRESS(address)
  873. | BF_GPMI_CTRL0_XFER_COUNT(0);
  874. pio[1] = 0;
  875. desc = channel->device->device_prep_slave_sg(channel,
  876. (struct scatterlist *)pio, 2,
  877. DMA_TRANS_NONE, 0);
  878. if (!desc) {
  879. pr_err("step 1 error\n");
  880. return -1;
  881. }
  882. /* [2] Enable the BCH block and read. */
  883. command_mode = BV_GPMI_CTRL0_COMMAND_MODE__READ;
  884. address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
  885. ecc_command = BV_GPMI_ECCCTRL_ECC_CMD__BCH_DECODE;
  886. buffer_mask = BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_PAGE
  887. | BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_AUXONLY;
  888. pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
  889. | BM_GPMI_CTRL0_WORD_LENGTH
  890. | BF_GPMI_CTRL0_CS(chip, this)
  891. | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
  892. | BF_GPMI_CTRL0_ADDRESS(address)
  893. | BF_GPMI_CTRL0_XFER_COUNT(geo->page_size);
  894. pio[1] = 0;
  895. pio[2] = BM_GPMI_ECCCTRL_ENABLE_ECC
  896. | BF_GPMI_ECCCTRL_ECC_CMD(ecc_command)
  897. | BF_GPMI_ECCCTRL_BUFFER_MASK(buffer_mask);
  898. pio[3] = geo->page_size;
  899. pio[4] = payload;
  900. pio[5] = auxiliary;
  901. desc = channel->device->device_prep_slave_sg(channel,
  902. (struct scatterlist *)pio,
  903. ARRAY_SIZE(pio), DMA_TRANS_NONE, 1);
  904. if (!desc) {
  905. pr_err("step 2 error\n");
  906. return -1;
  907. }
  908. /* [3] Disable the BCH block */
  909. command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WAIT_FOR_READY;
  910. address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
  911. pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
  912. | BM_GPMI_CTRL0_WORD_LENGTH
  913. | BF_GPMI_CTRL0_CS(chip, this)
  914. | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
  915. | BF_GPMI_CTRL0_ADDRESS(address)
  916. | BF_GPMI_CTRL0_XFER_COUNT(geo->page_size);
  917. pio[1] = 0;
  918. desc = channel->device->device_prep_slave_sg(channel,
  919. (struct scatterlist *)pio, 2,
  920. DMA_TRANS_NONE, 1);
  921. if (!desc) {
  922. pr_err("step 3 error\n");
  923. return -1;
  924. }
  925. /* [4] submit the DMA */
  926. set_dma_type(this, DMA_FOR_READ_ECC_PAGE);
  927. return start_dma_with_bch_irq(this, desc);
  928. }