gpmi-lib.c 44 KB

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