gpmi-lib.c 33 KB

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