gpmi-lib.c 33 KB

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