gpmi-lib.c 45 KB

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