gpmi-lib.c 33 KB

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