gpmi-lib.c 41 KB

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