gpmi-lib.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  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/delay.h>
  22. #include <linux/clk.h>
  23. #include "gpmi-nand.h"
  24. #include "gpmi-regs.h"
  25. #include "bch-regs.h"
  26. static struct timing_threshod timing_default_threshold = {
  27. .max_data_setup_cycles = (BM_GPMI_TIMING0_DATA_SETUP >>
  28. BP_GPMI_TIMING0_DATA_SETUP),
  29. .internal_data_setup_in_ns = 0,
  30. .max_sample_delay_factor = (BM_GPMI_CTRL1_RDN_DELAY >>
  31. BP_GPMI_CTRL1_RDN_DELAY),
  32. .max_dll_clock_period_in_ns = 32,
  33. .max_dll_delay_in_ns = 16,
  34. };
  35. #define MXS_SET_ADDR 0x4
  36. #define MXS_CLR_ADDR 0x8
  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. writel(mask, addr + MXS_CLR_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. writel(MODULE_CLKGATE, reset_addr + MXS_CLR_ADDR);
  86. if (!just_enable) {
  87. /* set SFTRST to reset the block */
  88. writel(MODULE_SFTRST, reset_addr + MXS_SET_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. static int __gpmi_enable_clk(struct gpmi_nand_data *this, bool v)
  110. {
  111. struct clk *clk;
  112. int ret;
  113. int i;
  114. for (i = 0; i < GPMI_CLK_MAX; i++) {
  115. clk = this->resources.clock[i];
  116. if (!clk)
  117. break;
  118. if (v) {
  119. ret = clk_prepare_enable(clk);
  120. if (ret)
  121. goto err_clk;
  122. } else {
  123. clk_disable_unprepare(clk);
  124. }
  125. }
  126. return 0;
  127. err_clk:
  128. for (; i > 0; i--)
  129. clk_disable_unprepare(this->resources.clock[i - 1]);
  130. return ret;
  131. }
  132. #define gpmi_enable_clk(x) __gpmi_enable_clk(x, true)
  133. #define gpmi_disable_clk(x) __gpmi_enable_clk(x, false)
  134. int gpmi_init(struct gpmi_nand_data *this)
  135. {
  136. struct resources *r = &this->resources;
  137. int ret;
  138. ret = gpmi_enable_clk(this);
  139. if (ret)
  140. goto err_out;
  141. ret = gpmi_reset_block(r->gpmi_regs, false);
  142. if (ret)
  143. goto err_out;
  144. /*
  145. * Reset BCH here, too. We got failures otherwise :(
  146. * See later BCH reset for explanation of MX23 handling
  147. */
  148. ret = gpmi_reset_block(r->bch_regs, GPMI_IS_MX23(this));
  149. if (ret)
  150. goto err_out;
  151. /* Choose NAND mode. */
  152. writel(BM_GPMI_CTRL1_GPMI_MODE, r->gpmi_regs + HW_GPMI_CTRL1_CLR);
  153. /* Set the IRQ polarity. */
  154. writel(BM_GPMI_CTRL1_ATA_IRQRDY_POLARITY,
  155. r->gpmi_regs + HW_GPMI_CTRL1_SET);
  156. /* Disable Write-Protection. */
  157. writel(BM_GPMI_CTRL1_DEV_RESET, r->gpmi_regs + HW_GPMI_CTRL1_SET);
  158. /* Select BCH ECC. */
  159. writel(BM_GPMI_CTRL1_BCH_MODE, r->gpmi_regs + HW_GPMI_CTRL1_SET);
  160. gpmi_disable_clk(this);
  161. return 0;
  162. err_out:
  163. return ret;
  164. }
  165. /* This function is very useful. It is called only when the bug occur. */
  166. void gpmi_dump_info(struct gpmi_nand_data *this)
  167. {
  168. struct resources *r = &this->resources;
  169. struct bch_geometry *geo = &this->bch_geometry;
  170. u32 reg;
  171. int i;
  172. pr_err("Show GPMI registers :\n");
  173. for (i = 0; i <= HW_GPMI_DEBUG / 0x10 + 1; i++) {
  174. reg = readl(r->gpmi_regs + i * 0x10);
  175. pr_err("offset 0x%.3x : 0x%.8x\n", i * 0x10, reg);
  176. }
  177. /* start to print out the BCH info */
  178. pr_err("Show BCH registers :\n");
  179. for (i = 0; i <= HW_BCH_VERSION / 0x10 + 1; i++) {
  180. reg = readl(r->bch_regs + i * 0x10);
  181. pr_err("offset 0x%.3x : 0x%.8x\n", i * 0x10, reg);
  182. }
  183. pr_err("BCH Geometry :\n");
  184. pr_err("GF length : %u\n", geo->gf_len);
  185. pr_err("ECC Strength : %u\n", geo->ecc_strength);
  186. pr_err("Page Size in Bytes : %u\n", geo->page_size);
  187. pr_err("Metadata Size in Bytes : %u\n", geo->metadata_size);
  188. pr_err("ECC Chunk Size in Bytes: %u\n", geo->ecc_chunk_size);
  189. pr_err("ECC Chunk Count : %u\n", geo->ecc_chunk_count);
  190. pr_err("Payload Size in Bytes : %u\n", geo->payload_size);
  191. pr_err("Auxiliary Size in Bytes: %u\n", geo->auxiliary_size);
  192. pr_err("Auxiliary Status Offset: %u\n", geo->auxiliary_status_offset);
  193. pr_err("Block Mark Byte Offset : %u\n", geo->block_mark_byte_offset);
  194. pr_err("Block Mark Bit Offset : %u\n", geo->block_mark_bit_offset);
  195. }
  196. /* Configures the geometry for BCH. */
  197. int bch_set_geometry(struct gpmi_nand_data *this)
  198. {
  199. struct resources *r = &this->resources;
  200. struct bch_geometry *bch_geo = &this->bch_geometry;
  201. unsigned int block_count;
  202. unsigned int block_size;
  203. unsigned int metadata_size;
  204. unsigned int ecc_strength;
  205. unsigned int page_size;
  206. unsigned int gf_len;
  207. int ret;
  208. if (common_nfc_set_geometry(this))
  209. return !0;
  210. block_count = bch_geo->ecc_chunk_count - 1;
  211. block_size = bch_geo->ecc_chunk_size;
  212. metadata_size = bch_geo->metadata_size;
  213. ecc_strength = bch_geo->ecc_strength >> 1;
  214. page_size = bch_geo->page_size;
  215. gf_len = bch_geo->gf_len;
  216. ret = gpmi_enable_clk(this);
  217. if (ret)
  218. goto err_out;
  219. /*
  220. * Due to erratum #2847 of the MX23, the BCH cannot be soft reset on this
  221. * chip, otherwise it will lock up. So we skip resetting BCH on the MX23.
  222. * On the other hand, the MX28 needs the reset, because one case has been
  223. * seen where the BCH produced ECC errors constantly after 10000
  224. * consecutive reboots. The latter case has not been seen on the MX23 yet,
  225. * still we don't know if it could happen there as well.
  226. */
  227. ret = gpmi_reset_block(r->bch_regs, GPMI_IS_MX23(this));
  228. if (ret)
  229. goto err_out;
  230. /* Configure layout 0. */
  231. writel(BF_BCH_FLASH0LAYOUT0_NBLOCKS(block_count)
  232. | BF_BCH_FLASH0LAYOUT0_META_SIZE(metadata_size)
  233. | BF_BCH_FLASH0LAYOUT0_ECC0(ecc_strength, this)
  234. | BF_BCH_FLASH0LAYOUT0_GF(gf_len, this)
  235. | BF_BCH_FLASH0LAYOUT0_DATA0_SIZE(block_size, this),
  236. r->bch_regs + HW_BCH_FLASH0LAYOUT0);
  237. writel(BF_BCH_FLASH0LAYOUT1_PAGE_SIZE(page_size)
  238. | BF_BCH_FLASH0LAYOUT1_ECCN(ecc_strength, this)
  239. | BF_BCH_FLASH0LAYOUT1_GF(gf_len, this)
  240. | BF_BCH_FLASH0LAYOUT1_DATAN_SIZE(block_size, this),
  241. r->bch_regs + HW_BCH_FLASH0LAYOUT1);
  242. /* Set *all* chip selects to use layout 0. */
  243. writel(0, r->bch_regs + HW_BCH_LAYOUTSELECT);
  244. /* Enable interrupts. */
  245. writel(BM_BCH_CTRL_COMPLETE_IRQ_EN,
  246. r->bch_regs + HW_BCH_CTRL_SET);
  247. gpmi_disable_clk(this);
  248. return 0;
  249. err_out:
  250. return ret;
  251. }
  252. /* Converts time in nanoseconds to cycles. */
  253. static unsigned int ns_to_cycles(unsigned int time,
  254. unsigned int period, unsigned int min)
  255. {
  256. unsigned int k;
  257. k = (time + period - 1) / period;
  258. return max(k, min);
  259. }
  260. #define DEF_MIN_PROP_DELAY 5
  261. #define DEF_MAX_PROP_DELAY 9
  262. /* Apply timing to current hardware conditions. */
  263. static int gpmi_nfc_compute_hardware_timing(struct gpmi_nand_data *this,
  264. struct gpmi_nfc_hardware_timing *hw)
  265. {
  266. struct timing_threshod *nfc = &timing_default_threshold;
  267. struct resources *r = &this->resources;
  268. struct nand_chip *nand = &this->nand;
  269. struct nand_timing target = this->timing;
  270. bool improved_timing_is_available;
  271. unsigned long clock_frequency_in_hz;
  272. unsigned int clock_period_in_ns;
  273. bool dll_use_half_periods;
  274. unsigned int dll_delay_shift;
  275. unsigned int max_sample_delay_in_ns;
  276. unsigned int address_setup_in_cycles;
  277. unsigned int data_setup_in_ns;
  278. unsigned int data_setup_in_cycles;
  279. unsigned int data_hold_in_cycles;
  280. int ideal_sample_delay_in_ns;
  281. unsigned int sample_delay_factor;
  282. int tEYE;
  283. unsigned int min_prop_delay_in_ns = DEF_MIN_PROP_DELAY;
  284. unsigned int max_prop_delay_in_ns = DEF_MAX_PROP_DELAY;
  285. /*
  286. * If there are multiple chips, we need to relax the timings to allow
  287. * for signal distortion due to higher capacitance.
  288. */
  289. if (nand->numchips > 2) {
  290. target.data_setup_in_ns += 10;
  291. target.data_hold_in_ns += 10;
  292. target.address_setup_in_ns += 10;
  293. } else if (nand->numchips > 1) {
  294. target.data_setup_in_ns += 5;
  295. target.data_hold_in_ns += 5;
  296. target.address_setup_in_ns += 5;
  297. }
  298. /* Check if improved timing information is available. */
  299. improved_timing_is_available =
  300. (target.tREA_in_ns >= 0) &&
  301. (target.tRLOH_in_ns >= 0) &&
  302. (target.tRHOH_in_ns >= 0) ;
  303. /* Inspect the clock. */
  304. nfc->clock_frequency_in_hz = clk_get_rate(r->clock[0]);
  305. clock_frequency_in_hz = nfc->clock_frequency_in_hz;
  306. clock_period_in_ns = NSEC_PER_SEC / clock_frequency_in_hz;
  307. /*
  308. * The NFC quantizes setup and hold parameters in terms of clock cycles.
  309. * Here, we quantize the setup and hold timing parameters to the
  310. * next-highest clock period to make sure we apply at least the
  311. * specified times.
  312. *
  313. * For data setup and data hold, the hardware interprets a value of zero
  314. * as the largest possible delay. This is not what's intended by a zero
  315. * in the input parameter, so we impose a minimum of one cycle.
  316. */
  317. data_setup_in_cycles = ns_to_cycles(target.data_setup_in_ns,
  318. clock_period_in_ns, 1);
  319. data_hold_in_cycles = ns_to_cycles(target.data_hold_in_ns,
  320. clock_period_in_ns, 1);
  321. address_setup_in_cycles = ns_to_cycles(target.address_setup_in_ns,
  322. clock_period_in_ns, 0);
  323. /*
  324. * The clock's period affects the sample delay in a number of ways:
  325. *
  326. * (1) The NFC HAL tells us the maximum clock period the sample delay
  327. * DLL can tolerate. If the clock period is greater than half that
  328. * maximum, we must configure the DLL to be driven by half periods.
  329. *
  330. * (2) We need to convert from an ideal sample delay, in ns, to a
  331. * "sample delay factor," which the NFC uses. This factor depends on
  332. * whether we're driving the DLL with full or half periods.
  333. * Paraphrasing the reference manual:
  334. *
  335. * AD = SDF x 0.125 x RP
  336. *
  337. * where:
  338. *
  339. * AD is the applied delay, in ns.
  340. * SDF is the sample delay factor, which is dimensionless.
  341. * RP is the reference period, in ns, which is a full clock period
  342. * if the DLL is being driven by full periods, or half that if
  343. * the DLL is being driven by half periods.
  344. *
  345. * Let's re-arrange this in a way that's more useful to us:
  346. *
  347. * 8
  348. * SDF = AD x ----
  349. * RP
  350. *
  351. * The reference period is either the clock period or half that, so this
  352. * is:
  353. *
  354. * 8 AD x DDF
  355. * SDF = AD x ----- = --------
  356. * f x P P
  357. *
  358. * where:
  359. *
  360. * f is 1 or 1/2, depending on how we're driving the DLL.
  361. * P is the clock period.
  362. * DDF is the DLL Delay Factor, a dimensionless value that
  363. * incorporates all the constants in the conversion.
  364. *
  365. * DDF will be either 8 or 16, both of which are powers of two. We can
  366. * reduce the cost of this conversion by using bit shifts instead of
  367. * multiplication or division. Thus:
  368. *
  369. * AD << DDS
  370. * SDF = ---------
  371. * P
  372. *
  373. * or
  374. *
  375. * AD = (SDF >> DDS) x P
  376. *
  377. * where:
  378. *
  379. * DDS is the DLL Delay Shift, the logarithm to base 2 of the DDF.
  380. */
  381. if (clock_period_in_ns > (nfc->max_dll_clock_period_in_ns >> 1)) {
  382. dll_use_half_periods = true;
  383. dll_delay_shift = 3 + 1;
  384. } else {
  385. dll_use_half_periods = false;
  386. dll_delay_shift = 3;
  387. }
  388. /*
  389. * Compute the maximum sample delay the NFC allows, under current
  390. * conditions. If the clock is running too slowly, no sample delay is
  391. * possible.
  392. */
  393. if (clock_period_in_ns > nfc->max_dll_clock_period_in_ns)
  394. max_sample_delay_in_ns = 0;
  395. else {
  396. /*
  397. * Compute the delay implied by the largest sample delay factor
  398. * the NFC allows.
  399. */
  400. max_sample_delay_in_ns =
  401. (nfc->max_sample_delay_factor * clock_period_in_ns) >>
  402. dll_delay_shift;
  403. /*
  404. * Check if the implied sample delay larger than the NFC
  405. * actually allows.
  406. */
  407. if (max_sample_delay_in_ns > nfc->max_dll_delay_in_ns)
  408. max_sample_delay_in_ns = nfc->max_dll_delay_in_ns;
  409. }
  410. /*
  411. * Check if improved timing information is available. If not, we have to
  412. * use a less-sophisticated algorithm.
  413. */
  414. if (!improved_timing_is_available) {
  415. /*
  416. * Fold the read setup time required by the NFC into the ideal
  417. * sample delay.
  418. */
  419. ideal_sample_delay_in_ns = target.gpmi_sample_delay_in_ns +
  420. nfc->internal_data_setup_in_ns;
  421. /*
  422. * The ideal sample delay may be greater than the maximum
  423. * allowed by the NFC. If so, we can trade off sample delay time
  424. * for more data setup time.
  425. *
  426. * In each iteration of the following loop, we add a cycle to
  427. * the data setup time and subtract a corresponding amount from
  428. * the sample delay until we've satisified the constraints or
  429. * can't do any better.
  430. */
  431. while ((ideal_sample_delay_in_ns > max_sample_delay_in_ns) &&
  432. (data_setup_in_cycles < nfc->max_data_setup_cycles)) {
  433. data_setup_in_cycles++;
  434. ideal_sample_delay_in_ns -= clock_period_in_ns;
  435. if (ideal_sample_delay_in_ns < 0)
  436. ideal_sample_delay_in_ns = 0;
  437. }
  438. /*
  439. * Compute the sample delay factor that corresponds most closely
  440. * to the ideal sample delay. If the result is too large for the
  441. * NFC, use the maximum value.
  442. *
  443. * Notice that we use the ns_to_cycles function to compute the
  444. * sample delay factor. We do this because the form of the
  445. * computation is the same as that for calculating cycles.
  446. */
  447. sample_delay_factor =
  448. ns_to_cycles(
  449. ideal_sample_delay_in_ns << dll_delay_shift,
  450. clock_period_in_ns, 0);
  451. if (sample_delay_factor > nfc->max_sample_delay_factor)
  452. sample_delay_factor = nfc->max_sample_delay_factor;
  453. /* Skip to the part where we return our results. */
  454. goto return_results;
  455. }
  456. /*
  457. * If control arrives here, we have more detailed timing information,
  458. * so we can use a better algorithm.
  459. */
  460. /*
  461. * Fold the read setup time required by the NFC into the maximum
  462. * propagation delay.
  463. */
  464. max_prop_delay_in_ns += nfc->internal_data_setup_in_ns;
  465. /*
  466. * Earlier, we computed the number of clock cycles required to satisfy
  467. * the data setup time. Now, we need to know the actual nanoseconds.
  468. */
  469. data_setup_in_ns = clock_period_in_ns * data_setup_in_cycles;
  470. /*
  471. * Compute tEYE, the width of the data eye when reading from the NAND
  472. * Flash. The eye width is fundamentally determined by the data setup
  473. * time, perturbed by propagation delays and some characteristics of the
  474. * NAND Flash device.
  475. *
  476. * start of the eye = max_prop_delay + tREA
  477. * end of the eye = min_prop_delay + tRHOH + data_setup
  478. */
  479. tEYE = (int)min_prop_delay_in_ns + (int)target.tRHOH_in_ns +
  480. (int)data_setup_in_ns;
  481. tEYE -= (int)max_prop_delay_in_ns + (int)target.tREA_in_ns;
  482. /*
  483. * The eye must be open. If it's not, we can try to open it by
  484. * increasing its main forcer, the data setup time.
  485. *
  486. * In each iteration of the following loop, we increase the data setup
  487. * time by a single clock cycle. We do this until either the eye is
  488. * open or we run into NFC limits.
  489. */
  490. while ((tEYE <= 0) &&
  491. (data_setup_in_cycles < nfc->max_data_setup_cycles)) {
  492. /* Give a cycle to data setup. */
  493. data_setup_in_cycles++;
  494. /* Synchronize the data setup time with the cycles. */
  495. data_setup_in_ns += clock_period_in_ns;
  496. /* Adjust tEYE accordingly. */
  497. tEYE += clock_period_in_ns;
  498. }
  499. /*
  500. * When control arrives here, the eye is open. The ideal time to sample
  501. * the data is in the center of the eye:
  502. *
  503. * end of the eye + start of the eye
  504. * --------------------------------- - data_setup
  505. * 2
  506. *
  507. * After some algebra, this simplifies to the code immediately below.
  508. */
  509. ideal_sample_delay_in_ns =
  510. ((int)max_prop_delay_in_ns +
  511. (int)target.tREA_in_ns +
  512. (int)min_prop_delay_in_ns +
  513. (int)target.tRHOH_in_ns -
  514. (int)data_setup_in_ns) >> 1;
  515. /*
  516. * The following figure illustrates some aspects of a NAND Flash read:
  517. *
  518. *
  519. * __ _____________________________________
  520. * RDN \_________________/
  521. *
  522. * <---- tEYE ----->
  523. * /-----------------\
  524. * Read Data ----------------------------< >---------
  525. * \-----------------/
  526. * ^ ^ ^ ^
  527. * | | | |
  528. * |<--Data Setup -->|<--Delay Time -->| |
  529. * | | | |
  530. * | | |
  531. * | |<-- Quantized Delay Time -->|
  532. * | | |
  533. *
  534. *
  535. * We have some issues we must now address:
  536. *
  537. * (1) The *ideal* sample delay time must not be negative. If it is, we
  538. * jam it to zero.
  539. *
  540. * (2) The *ideal* sample delay time must not be greater than that
  541. * allowed by the NFC. If it is, we can increase the data setup
  542. * time, which will reduce the delay between the end of the data
  543. * setup and the center of the eye. It will also make the eye
  544. * larger, which might help with the next issue...
  545. *
  546. * (3) The *quantized* sample delay time must not fall either before the
  547. * eye opens or after it closes (the latter is the problem
  548. * illustrated in the above figure).
  549. */
  550. /* Jam a negative ideal sample delay to zero. */
  551. if (ideal_sample_delay_in_ns < 0)
  552. ideal_sample_delay_in_ns = 0;
  553. /*
  554. * Extend the data setup as needed to reduce the ideal sample delay
  555. * below the maximum permitted by the NFC.
  556. */
  557. while ((ideal_sample_delay_in_ns > max_sample_delay_in_ns) &&
  558. (data_setup_in_cycles < nfc->max_data_setup_cycles)) {
  559. /* Give a cycle to data setup. */
  560. data_setup_in_cycles++;
  561. /* Synchronize the data setup time with the cycles. */
  562. data_setup_in_ns += clock_period_in_ns;
  563. /* Adjust tEYE accordingly. */
  564. tEYE += clock_period_in_ns;
  565. /*
  566. * Decrease the ideal sample delay by one half cycle, to keep it
  567. * in the middle of the eye.
  568. */
  569. ideal_sample_delay_in_ns -= (clock_period_in_ns >> 1);
  570. /* Jam a negative ideal sample delay to zero. */
  571. if (ideal_sample_delay_in_ns < 0)
  572. ideal_sample_delay_in_ns = 0;
  573. }
  574. /*
  575. * Compute the sample delay factor that corresponds to the ideal sample
  576. * delay. If the result is too large, then use the maximum allowed
  577. * value.
  578. *
  579. * Notice that we use the ns_to_cycles function to compute the sample
  580. * delay factor. We do this because the form of the computation is the
  581. * same as that for calculating cycles.
  582. */
  583. sample_delay_factor =
  584. ns_to_cycles(ideal_sample_delay_in_ns << dll_delay_shift,
  585. clock_period_in_ns, 0);
  586. if (sample_delay_factor > nfc->max_sample_delay_factor)
  587. sample_delay_factor = nfc->max_sample_delay_factor;
  588. /*
  589. * These macros conveniently encapsulate a computation we'll use to
  590. * continuously evaluate whether or not the data sample delay is inside
  591. * the eye.
  592. */
  593. #define IDEAL_DELAY ((int) ideal_sample_delay_in_ns)
  594. #define QUANTIZED_DELAY \
  595. ((int) ((sample_delay_factor * clock_period_in_ns) >> \
  596. dll_delay_shift))
  597. #define DELAY_ERROR (abs(QUANTIZED_DELAY - IDEAL_DELAY))
  598. #define SAMPLE_IS_NOT_WITHIN_THE_EYE (DELAY_ERROR > (tEYE >> 1))
  599. /*
  600. * While the quantized sample time falls outside the eye, reduce the
  601. * sample delay or extend the data setup to move the sampling point back
  602. * toward the eye. Do not allow the number of data setup cycles to
  603. * exceed the maximum allowed by the NFC.
  604. */
  605. while (SAMPLE_IS_NOT_WITHIN_THE_EYE &&
  606. (data_setup_in_cycles < nfc->max_data_setup_cycles)) {
  607. /*
  608. * If control arrives here, the quantized sample delay falls
  609. * outside the eye. Check if it's before the eye opens, or after
  610. * the eye closes.
  611. */
  612. if (QUANTIZED_DELAY > IDEAL_DELAY) {
  613. /*
  614. * If control arrives here, the quantized sample delay
  615. * falls after the eye closes. Decrease the quantized
  616. * delay time and then go back to re-evaluate.
  617. */
  618. if (sample_delay_factor != 0)
  619. sample_delay_factor--;
  620. continue;
  621. }
  622. /*
  623. * If control arrives here, the quantized sample delay falls
  624. * before the eye opens. Shift the sample point by increasing
  625. * data setup time. This will also make the eye larger.
  626. */
  627. /* Give a cycle to data setup. */
  628. data_setup_in_cycles++;
  629. /* Synchronize the data setup time with the cycles. */
  630. data_setup_in_ns += clock_period_in_ns;
  631. /* Adjust tEYE accordingly. */
  632. tEYE += clock_period_in_ns;
  633. /*
  634. * Decrease the ideal sample delay by one half cycle, to keep it
  635. * in the middle of the eye.
  636. */
  637. ideal_sample_delay_in_ns -= (clock_period_in_ns >> 1);
  638. /* ...and one less period for the delay time. */
  639. ideal_sample_delay_in_ns -= clock_period_in_ns;
  640. /* Jam a negative ideal sample delay to zero. */
  641. if (ideal_sample_delay_in_ns < 0)
  642. ideal_sample_delay_in_ns = 0;
  643. /*
  644. * We have a new ideal sample delay, so re-compute the quantized
  645. * delay.
  646. */
  647. sample_delay_factor =
  648. ns_to_cycles(
  649. ideal_sample_delay_in_ns << dll_delay_shift,
  650. clock_period_in_ns, 0);
  651. if (sample_delay_factor > nfc->max_sample_delay_factor)
  652. sample_delay_factor = nfc->max_sample_delay_factor;
  653. }
  654. /* Control arrives here when we're ready to return our results. */
  655. return_results:
  656. hw->data_setup_in_cycles = data_setup_in_cycles;
  657. hw->data_hold_in_cycles = data_hold_in_cycles;
  658. hw->address_setup_in_cycles = address_setup_in_cycles;
  659. hw->use_half_periods = dll_use_half_periods;
  660. hw->sample_delay_factor = sample_delay_factor;
  661. hw->device_busy_timeout = GPMI_DEFAULT_BUSY_TIMEOUT;
  662. hw->wrn_dly_sel = BV_GPMI_CTRL1_WRN_DLY_SEL_4_TO_8NS;
  663. /* Return success. */
  664. return 0;
  665. }
  666. /*
  667. * <1> Firstly, we should know what's the GPMI-clock means.
  668. * The GPMI-clock is the internal clock in the gpmi nand controller.
  669. * If you set 100MHz to gpmi nand controller, the GPMI-clock's period
  670. * is 10ns. Mark the GPMI-clock's period as GPMI-clock-period.
  671. *
  672. * <2> Secondly, we should know what's the frequency on the nand chip pins.
  673. * The frequency on the nand chip pins is derived from the GPMI-clock.
  674. * We can get it from the following equation:
  675. *
  676. * F = G / (DS + DH)
  677. *
  678. * F : the frequency on the nand chip pins.
  679. * G : the GPMI clock, such as 100MHz.
  680. * DS : GPMI_HW_GPMI_TIMING0:DATA_SETUP
  681. * DH : GPMI_HW_GPMI_TIMING0:DATA_HOLD
  682. *
  683. * <3> Thirdly, when the frequency on the nand chip pins is above 33MHz,
  684. * the nand EDO(extended Data Out) timing could be applied.
  685. * The GPMI implements a feedback read strobe to sample the read data.
  686. * The feedback read strobe can be delayed to support the nand EDO timing
  687. * where the read strobe may deasserts before the read data is valid, and
  688. * read data is valid for some time after read strobe.
  689. *
  690. * The following figure illustrates some aspects of a NAND Flash read:
  691. *
  692. * |<---tREA---->|
  693. * | |
  694. * | | |
  695. * |<--tRP-->| |
  696. * | | |
  697. * __ ___|__________________________________
  698. * RDN \________/ |
  699. * |
  700. * /---------\
  701. * Read Data --------------< >---------
  702. * \---------/
  703. * | |
  704. * |<-D->|
  705. * FeedbackRDN ________ ____________
  706. * \___________/
  707. *
  708. * D stands for delay, set in the HW_GPMI_CTRL1:RDN_DELAY.
  709. *
  710. *
  711. * <4> Now, we begin to describe how to compute the right RDN_DELAY.
  712. *
  713. * 4.1) From the aspect of the nand chip pins:
  714. * Delay = (tREA + C - tRP) {1}
  715. *
  716. * tREA : the maximum read access time. From the ONFI nand standards,
  717. * we know that tREA is 16ns in mode 5, tREA is 20ns is mode 4.
  718. * Please check it in : www.onfi.org
  719. * C : a constant for adjust the delay. default is 4.
  720. * tRP : the read pulse width.
  721. * Specified by the HW_GPMI_TIMING0:DATA_SETUP:
  722. * tRP = (GPMI-clock-period) * DATA_SETUP
  723. *
  724. * 4.2) From the aspect of the GPMI nand controller:
  725. * Delay = RDN_DELAY * 0.125 * RP {2}
  726. *
  727. * RP : the DLL reference period.
  728. * if (GPMI-clock-period > DLL_THRETHOLD)
  729. * RP = GPMI-clock-period / 2;
  730. * else
  731. * RP = GPMI-clock-period;
  732. *
  733. * Set the HW_GPMI_CTRL1:HALF_PERIOD if GPMI-clock-period
  734. * is greater DLL_THRETHOLD. In other SOCs, the DLL_THRETHOLD
  735. * is 16ns, but in mx6q, we use 12ns.
  736. *
  737. * 4.3) since {1} equals {2}, we get:
  738. *
  739. * (tREA + 4 - tRP) * 8
  740. * RDN_DELAY = --------------------- {3}
  741. * RP
  742. *
  743. * 4.4) We only support the fastest asynchronous mode of ONFI nand.
  744. * For some ONFI nand, the mode 4 is the fastest mode;
  745. * while for some ONFI nand, the mode 5 is the fastest mode.
  746. * So we only support the mode 4 and mode 5. It is no need to
  747. * support other modes.
  748. */
  749. static void gpmi_compute_edo_timing(struct gpmi_nand_data *this,
  750. struct gpmi_nfc_hardware_timing *hw)
  751. {
  752. struct resources *r = &this->resources;
  753. unsigned long rate = clk_get_rate(r->clock[0]);
  754. int mode = this->timing_mode;
  755. int dll_threshold = 16; /* in ns */
  756. unsigned long delay;
  757. unsigned long clk_period;
  758. int t_rea;
  759. int c = 4;
  760. int t_rp;
  761. int rp;
  762. /*
  763. * [1] for GPMI_HW_GPMI_TIMING0:
  764. * The async mode requires 40MHz for mode 4, 50MHz for mode 5.
  765. * The GPMI can support 100MHz at most. So if we want to
  766. * get the 40MHz or 50MHz, we have to set DS=1, DH=1.
  767. * Set the ADDRESS_SETUP to 0 in mode 4.
  768. */
  769. hw->data_setup_in_cycles = 1;
  770. hw->data_hold_in_cycles = 1;
  771. hw->address_setup_in_cycles = ((mode == 5) ? 1 : 0);
  772. /* [2] for GPMI_HW_GPMI_TIMING1 */
  773. hw->device_busy_timeout = 0x9000;
  774. /* [3] for GPMI_HW_GPMI_CTRL1 */
  775. hw->wrn_dly_sel = BV_GPMI_CTRL1_WRN_DLY_SEL_NO_DELAY;
  776. if (GPMI_IS_MX6Q(this))
  777. dll_threshold = 12;
  778. /*
  779. * Enlarge 10 times for the numerator and denominator in {3}.
  780. * This make us to get more accurate result.
  781. */
  782. clk_period = NSEC_PER_SEC / (rate / 10);
  783. dll_threshold *= 10;
  784. t_rea = ((mode == 5) ? 16 : 20) * 10;
  785. c *= 10;
  786. t_rp = clk_period * 1; /* DATA_SETUP is 1 */
  787. if (clk_period > dll_threshold) {
  788. hw->use_half_periods = 1;
  789. rp = clk_period / 2;
  790. } else {
  791. hw->use_half_periods = 0;
  792. rp = clk_period;
  793. }
  794. /*
  795. * Multiply the numerator with 10, we could do a round off:
  796. * 7.8 round up to 8; 7.4 round down to 7.
  797. */
  798. delay = (((t_rea + c - t_rp) * 8) * 10) / rp;
  799. delay = (delay + 5) / 10;
  800. hw->sample_delay_factor = delay;
  801. }
  802. static int enable_edo_mode(struct gpmi_nand_data *this, int mode)
  803. {
  804. struct resources *r = &this->resources;
  805. struct nand_chip *nand = &this->nand;
  806. struct mtd_info *mtd = &this->mtd;
  807. uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {};
  808. unsigned long rate;
  809. int ret;
  810. nand->select_chip(mtd, 0);
  811. /* [1] send SET FEATURE commond to NAND */
  812. feature[0] = mode;
  813. ret = nand->onfi_set_features(mtd, nand,
  814. ONFI_FEATURE_ADDR_TIMING_MODE, feature);
  815. if (ret)
  816. goto err_out;
  817. /* [2] send GET FEATURE command to double-check the timing mode */
  818. memset(feature, 0, ONFI_SUBFEATURE_PARAM_LEN);
  819. ret = nand->onfi_get_features(mtd, nand,
  820. ONFI_FEATURE_ADDR_TIMING_MODE, feature);
  821. if (ret || feature[0] != mode)
  822. goto err_out;
  823. nand->select_chip(mtd, -1);
  824. /* [3] set the main IO clock, 100MHz for mode 5, 80MHz for mode 4. */
  825. rate = (mode == 5) ? 100000000 : 80000000;
  826. clk_set_rate(r->clock[0], rate);
  827. /* Let the gpmi_begin() re-compute the timing again. */
  828. this->flags &= ~GPMI_TIMING_INIT_OK;
  829. this->flags |= GPMI_ASYNC_EDO_ENABLED;
  830. this->timing_mode = mode;
  831. dev_info(this->dev, "enable the asynchronous EDO mode %d\n", mode);
  832. return 0;
  833. err_out:
  834. nand->select_chip(mtd, -1);
  835. dev_err(this->dev, "mode:%d ,failed in set feature.\n", mode);
  836. return -EINVAL;
  837. }
  838. int gpmi_extra_init(struct gpmi_nand_data *this)
  839. {
  840. struct nand_chip *chip = &this->nand;
  841. /* Enable the asynchronous EDO feature. */
  842. if (GPMI_IS_MX6Q(this) && chip->onfi_version) {
  843. int mode = onfi_get_async_timing_mode(chip);
  844. /* We only support the timing mode 4 and mode 5. */
  845. if (mode & ONFI_TIMING_MODE_5)
  846. mode = 5;
  847. else if (mode & ONFI_TIMING_MODE_4)
  848. mode = 4;
  849. else
  850. return 0;
  851. return enable_edo_mode(this, mode);
  852. }
  853. return 0;
  854. }
  855. /* Begin the I/O */
  856. void gpmi_begin(struct gpmi_nand_data *this)
  857. {
  858. struct resources *r = &this->resources;
  859. void __iomem *gpmi_regs = r->gpmi_regs;
  860. unsigned int clock_period_in_ns;
  861. uint32_t reg;
  862. unsigned int dll_wait_time_in_us;
  863. struct gpmi_nfc_hardware_timing hw;
  864. int ret;
  865. /* Enable the clock. */
  866. ret = gpmi_enable_clk(this);
  867. if (ret) {
  868. pr_err("We failed in enable the clk\n");
  869. goto err_out;
  870. }
  871. /* Only initialize the timing once */
  872. if (this->flags & GPMI_TIMING_INIT_OK)
  873. return;
  874. this->flags |= GPMI_TIMING_INIT_OK;
  875. if (this->flags & GPMI_ASYNC_EDO_ENABLED)
  876. gpmi_compute_edo_timing(this, &hw);
  877. else
  878. gpmi_nfc_compute_hardware_timing(this, &hw);
  879. /* [1] Set HW_GPMI_TIMING0 */
  880. reg = BF_GPMI_TIMING0_ADDRESS_SETUP(hw.address_setup_in_cycles) |
  881. BF_GPMI_TIMING0_DATA_HOLD(hw.data_hold_in_cycles) |
  882. BF_GPMI_TIMING0_DATA_SETUP(hw.data_setup_in_cycles) ;
  883. writel(reg, gpmi_regs + HW_GPMI_TIMING0);
  884. /* [2] Set HW_GPMI_TIMING1 */
  885. writel(BF_GPMI_TIMING1_BUSY_TIMEOUT(hw.device_busy_timeout),
  886. gpmi_regs + HW_GPMI_TIMING1);
  887. /* [3] The following code is to set the HW_GPMI_CTRL1. */
  888. /* Set the WRN_DLY_SEL */
  889. writel(BM_GPMI_CTRL1_WRN_DLY_SEL, gpmi_regs + HW_GPMI_CTRL1_CLR);
  890. writel(BF_GPMI_CTRL1_WRN_DLY_SEL(hw.wrn_dly_sel),
  891. gpmi_regs + HW_GPMI_CTRL1_SET);
  892. /* DLL_ENABLE must be set to 0 when setting RDN_DELAY or HALF_PERIOD. */
  893. writel(BM_GPMI_CTRL1_DLL_ENABLE, gpmi_regs + HW_GPMI_CTRL1_CLR);
  894. /* Clear out the DLL control fields. */
  895. reg = BM_GPMI_CTRL1_RDN_DELAY | BM_GPMI_CTRL1_HALF_PERIOD;
  896. writel(reg, gpmi_regs + HW_GPMI_CTRL1_CLR);
  897. /* If no sample delay is called for, return immediately. */
  898. if (!hw.sample_delay_factor)
  899. return;
  900. /* Set RDN_DELAY or HALF_PERIOD. */
  901. reg = ((hw.use_half_periods) ? BM_GPMI_CTRL1_HALF_PERIOD : 0)
  902. | BF_GPMI_CTRL1_RDN_DELAY(hw.sample_delay_factor);
  903. writel(reg, gpmi_regs + HW_GPMI_CTRL1_SET);
  904. /* At last, we enable the DLL. */
  905. writel(BM_GPMI_CTRL1_DLL_ENABLE, gpmi_regs + HW_GPMI_CTRL1_SET);
  906. /*
  907. * After we enable the GPMI DLL, we have to wait 64 clock cycles before
  908. * we can use the GPMI. Calculate the amount of time we need to wait,
  909. * in microseconds.
  910. */
  911. clock_period_in_ns = NSEC_PER_SEC / clk_get_rate(r->clock[0]);
  912. dll_wait_time_in_us = (clock_period_in_ns * 64) / 1000;
  913. if (!dll_wait_time_in_us)
  914. dll_wait_time_in_us = 1;
  915. /* Wait for the DLL to settle. */
  916. udelay(dll_wait_time_in_us);
  917. err_out:
  918. return;
  919. }
  920. void gpmi_end(struct gpmi_nand_data *this)
  921. {
  922. gpmi_disable_clk(this);
  923. }
  924. /* Clears a BCH interrupt. */
  925. void gpmi_clear_bch(struct gpmi_nand_data *this)
  926. {
  927. struct resources *r = &this->resources;
  928. writel(BM_BCH_CTRL_COMPLETE_IRQ, r->bch_regs + HW_BCH_CTRL_CLR);
  929. }
  930. /* Returns the Ready/Busy status of the given chip. */
  931. int gpmi_is_ready(struct gpmi_nand_data *this, unsigned chip)
  932. {
  933. struct resources *r = &this->resources;
  934. uint32_t mask = 0;
  935. uint32_t reg = 0;
  936. if (GPMI_IS_MX23(this)) {
  937. mask = MX23_BM_GPMI_DEBUG_READY0 << chip;
  938. reg = readl(r->gpmi_regs + HW_GPMI_DEBUG);
  939. } else if (GPMI_IS_MX28(this) || GPMI_IS_MX6Q(this)) {
  940. /* MX28 shares the same R/B register as MX6Q. */
  941. mask = MX28_BF_GPMI_STAT_READY_BUSY(1 << chip);
  942. reg = readl(r->gpmi_regs + HW_GPMI_STAT);
  943. } else
  944. pr_err("unknow arch.\n");
  945. return reg & mask;
  946. }
  947. static inline void set_dma_type(struct gpmi_nand_data *this,
  948. enum dma_ops_type type)
  949. {
  950. this->last_dma_type = this->dma_type;
  951. this->dma_type = type;
  952. }
  953. int gpmi_send_command(struct gpmi_nand_data *this)
  954. {
  955. struct dma_chan *channel = get_dma_chan(this);
  956. struct dma_async_tx_descriptor *desc;
  957. struct scatterlist *sgl;
  958. int chip = this->current_chip;
  959. u32 pio[3];
  960. /* [1] send out the PIO words */
  961. pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__WRITE)
  962. | BM_GPMI_CTRL0_WORD_LENGTH
  963. | BF_GPMI_CTRL0_CS(chip, this)
  964. | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
  965. | BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_CLE)
  966. | BM_GPMI_CTRL0_ADDRESS_INCREMENT
  967. | BF_GPMI_CTRL0_XFER_COUNT(this->command_length);
  968. pio[1] = pio[2] = 0;
  969. desc = dmaengine_prep_slave_sg(channel,
  970. (struct scatterlist *)pio,
  971. ARRAY_SIZE(pio), DMA_TRANS_NONE, 0);
  972. if (!desc) {
  973. pr_err("step 1 error\n");
  974. return -1;
  975. }
  976. /* [2] send out the COMMAND + ADDRESS string stored in @buffer */
  977. sgl = &this->cmd_sgl;
  978. sg_init_one(sgl, this->cmd_buffer, this->command_length);
  979. dma_map_sg(this->dev, sgl, 1, DMA_TO_DEVICE);
  980. desc = dmaengine_prep_slave_sg(channel,
  981. sgl, 1, DMA_MEM_TO_DEV,
  982. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  983. if (!desc) {
  984. pr_err("step 2 error\n");
  985. return -1;
  986. }
  987. /* [3] submit the DMA */
  988. set_dma_type(this, DMA_FOR_COMMAND);
  989. return start_dma_without_bch_irq(this, desc);
  990. }
  991. int gpmi_send_data(struct gpmi_nand_data *this)
  992. {
  993. struct dma_async_tx_descriptor *desc;
  994. struct dma_chan *channel = get_dma_chan(this);
  995. int chip = this->current_chip;
  996. uint32_t command_mode;
  997. uint32_t address;
  998. u32 pio[2];
  999. /* [1] PIO */
  1000. command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WRITE;
  1001. address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
  1002. pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
  1003. | BM_GPMI_CTRL0_WORD_LENGTH
  1004. | BF_GPMI_CTRL0_CS(chip, this)
  1005. | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
  1006. | BF_GPMI_CTRL0_ADDRESS(address)
  1007. | BF_GPMI_CTRL0_XFER_COUNT(this->upper_len);
  1008. pio[1] = 0;
  1009. desc = dmaengine_prep_slave_sg(channel, (struct scatterlist *)pio,
  1010. ARRAY_SIZE(pio), DMA_TRANS_NONE, 0);
  1011. if (!desc) {
  1012. pr_err("step 1 error\n");
  1013. return -1;
  1014. }
  1015. /* [2] send DMA request */
  1016. prepare_data_dma(this, DMA_TO_DEVICE);
  1017. desc = dmaengine_prep_slave_sg(channel, &this->data_sgl,
  1018. 1, DMA_MEM_TO_DEV,
  1019. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  1020. if (!desc) {
  1021. pr_err("step 2 error\n");
  1022. return -1;
  1023. }
  1024. /* [3] submit the DMA */
  1025. set_dma_type(this, DMA_FOR_WRITE_DATA);
  1026. return start_dma_without_bch_irq(this, desc);
  1027. }
  1028. int gpmi_read_data(struct gpmi_nand_data *this)
  1029. {
  1030. struct dma_async_tx_descriptor *desc;
  1031. struct dma_chan *channel = get_dma_chan(this);
  1032. int chip = this->current_chip;
  1033. u32 pio[2];
  1034. /* [1] : send PIO */
  1035. pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__READ)
  1036. | BM_GPMI_CTRL0_WORD_LENGTH
  1037. | BF_GPMI_CTRL0_CS(chip, this)
  1038. | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
  1039. | BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_DATA)
  1040. | BF_GPMI_CTRL0_XFER_COUNT(this->upper_len);
  1041. pio[1] = 0;
  1042. desc = dmaengine_prep_slave_sg(channel,
  1043. (struct scatterlist *)pio,
  1044. ARRAY_SIZE(pio), DMA_TRANS_NONE, 0);
  1045. if (!desc) {
  1046. pr_err("step 1 error\n");
  1047. return -1;
  1048. }
  1049. /* [2] : send DMA request */
  1050. prepare_data_dma(this, DMA_FROM_DEVICE);
  1051. desc = dmaengine_prep_slave_sg(channel, &this->data_sgl,
  1052. 1, DMA_DEV_TO_MEM,
  1053. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  1054. if (!desc) {
  1055. pr_err("step 2 error\n");
  1056. return -1;
  1057. }
  1058. /* [3] : submit the DMA */
  1059. set_dma_type(this, DMA_FOR_READ_DATA);
  1060. return start_dma_without_bch_irq(this, desc);
  1061. }
  1062. int gpmi_send_page(struct gpmi_nand_data *this,
  1063. dma_addr_t payload, dma_addr_t auxiliary)
  1064. {
  1065. struct bch_geometry *geo = &this->bch_geometry;
  1066. uint32_t command_mode;
  1067. uint32_t address;
  1068. uint32_t ecc_command;
  1069. uint32_t buffer_mask;
  1070. struct dma_async_tx_descriptor *desc;
  1071. struct dma_chan *channel = get_dma_chan(this);
  1072. int chip = this->current_chip;
  1073. u32 pio[6];
  1074. /* A DMA descriptor that does an ECC page read. */
  1075. command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WRITE;
  1076. address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
  1077. ecc_command = BV_GPMI_ECCCTRL_ECC_CMD__BCH_ENCODE;
  1078. buffer_mask = BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_PAGE |
  1079. BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_AUXONLY;
  1080. pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
  1081. | BM_GPMI_CTRL0_WORD_LENGTH
  1082. | BF_GPMI_CTRL0_CS(chip, this)
  1083. | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
  1084. | BF_GPMI_CTRL0_ADDRESS(address)
  1085. | BF_GPMI_CTRL0_XFER_COUNT(0);
  1086. pio[1] = 0;
  1087. pio[2] = BM_GPMI_ECCCTRL_ENABLE_ECC
  1088. | BF_GPMI_ECCCTRL_ECC_CMD(ecc_command)
  1089. | BF_GPMI_ECCCTRL_BUFFER_MASK(buffer_mask);
  1090. pio[3] = geo->page_size;
  1091. pio[4] = payload;
  1092. pio[5] = auxiliary;
  1093. desc = dmaengine_prep_slave_sg(channel,
  1094. (struct scatterlist *)pio,
  1095. ARRAY_SIZE(pio), DMA_TRANS_NONE,
  1096. DMA_CTRL_ACK);
  1097. if (!desc) {
  1098. pr_err("step 2 error\n");
  1099. return -1;
  1100. }
  1101. set_dma_type(this, DMA_FOR_WRITE_ECC_PAGE);
  1102. return start_dma_with_bch_irq(this, desc);
  1103. }
  1104. int gpmi_read_page(struct gpmi_nand_data *this,
  1105. dma_addr_t payload, dma_addr_t auxiliary)
  1106. {
  1107. struct bch_geometry *geo = &this->bch_geometry;
  1108. uint32_t command_mode;
  1109. uint32_t address;
  1110. uint32_t ecc_command;
  1111. uint32_t buffer_mask;
  1112. struct dma_async_tx_descriptor *desc;
  1113. struct dma_chan *channel = get_dma_chan(this);
  1114. int chip = this->current_chip;
  1115. u32 pio[6];
  1116. /* [1] Wait for the chip to report ready. */
  1117. command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WAIT_FOR_READY;
  1118. address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
  1119. pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
  1120. | BM_GPMI_CTRL0_WORD_LENGTH
  1121. | BF_GPMI_CTRL0_CS(chip, this)
  1122. | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
  1123. | BF_GPMI_CTRL0_ADDRESS(address)
  1124. | BF_GPMI_CTRL0_XFER_COUNT(0);
  1125. pio[1] = 0;
  1126. desc = dmaengine_prep_slave_sg(channel,
  1127. (struct scatterlist *)pio, 2,
  1128. DMA_TRANS_NONE, 0);
  1129. if (!desc) {
  1130. pr_err("step 1 error\n");
  1131. return -1;
  1132. }
  1133. /* [2] Enable the BCH block and read. */
  1134. command_mode = BV_GPMI_CTRL0_COMMAND_MODE__READ;
  1135. address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
  1136. ecc_command = BV_GPMI_ECCCTRL_ECC_CMD__BCH_DECODE;
  1137. buffer_mask = BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_PAGE
  1138. | BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_AUXONLY;
  1139. pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
  1140. | BM_GPMI_CTRL0_WORD_LENGTH
  1141. | BF_GPMI_CTRL0_CS(chip, this)
  1142. | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
  1143. | BF_GPMI_CTRL0_ADDRESS(address)
  1144. | BF_GPMI_CTRL0_XFER_COUNT(geo->page_size);
  1145. pio[1] = 0;
  1146. pio[2] = BM_GPMI_ECCCTRL_ENABLE_ECC
  1147. | BF_GPMI_ECCCTRL_ECC_CMD(ecc_command)
  1148. | BF_GPMI_ECCCTRL_BUFFER_MASK(buffer_mask);
  1149. pio[3] = geo->page_size;
  1150. pio[4] = payload;
  1151. pio[5] = auxiliary;
  1152. desc = dmaengine_prep_slave_sg(channel,
  1153. (struct scatterlist *)pio,
  1154. ARRAY_SIZE(pio), DMA_TRANS_NONE,
  1155. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  1156. if (!desc) {
  1157. pr_err("step 2 error\n");
  1158. return -1;
  1159. }
  1160. /* [3] Disable the BCH block */
  1161. command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WAIT_FOR_READY;
  1162. address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
  1163. pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
  1164. | BM_GPMI_CTRL0_WORD_LENGTH
  1165. | BF_GPMI_CTRL0_CS(chip, this)
  1166. | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
  1167. | BF_GPMI_CTRL0_ADDRESS(address)
  1168. | BF_GPMI_CTRL0_XFER_COUNT(geo->page_size);
  1169. pio[1] = 0;
  1170. pio[2] = 0; /* clear GPMI_HW_GPMI_ECCCTRL, disable the BCH. */
  1171. desc = dmaengine_prep_slave_sg(channel,
  1172. (struct scatterlist *)pio, 3,
  1173. DMA_TRANS_NONE,
  1174. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  1175. if (!desc) {
  1176. pr_err("step 3 error\n");
  1177. return -1;
  1178. }
  1179. /* [4] submit the DMA */
  1180. set_dma_type(this, DMA_FOR_READ_ECC_PAGE);
  1181. return start_dma_with_bch_irq(this, desc);
  1182. }