gpmi-lib.c 33 KB

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