gpmi-lib.c 41 KB

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