gpmi-nand.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793
  1. /*
  2. * Freescale GPMI NAND Flash Driver
  3. *
  4. * Copyright (C) 2010-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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <linux/clk.h>
  23. #include <linux/slab.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/module.h>
  26. #include <linux/mtd/partitions.h>
  27. #include <linux/of.h>
  28. #include <linux/of_device.h>
  29. #include <linux/of_mtd.h>
  30. #include "gpmi-nand.h"
  31. /* Resource names for the GPMI NAND driver. */
  32. #define GPMI_NAND_GPMI_REGS_ADDR_RES_NAME "gpmi-nand"
  33. #define GPMI_NAND_BCH_REGS_ADDR_RES_NAME "bch"
  34. #define GPMI_NAND_BCH_INTERRUPT_RES_NAME "bch"
  35. /* add our owner bbt descriptor */
  36. static uint8_t scan_ff_pattern[] = { 0xff };
  37. static struct nand_bbt_descr gpmi_bbt_descr = {
  38. .options = 0,
  39. .offs = 0,
  40. .len = 1,
  41. .pattern = scan_ff_pattern
  42. };
  43. /*
  44. * We may change the layout if we can get the ECC info from the datasheet,
  45. * else we will use all the (page + OOB).
  46. */
  47. static struct nand_ecclayout gpmi_hw_ecclayout = {
  48. .eccbytes = 0,
  49. .eccpos = { 0, },
  50. .oobfree = { {.offset = 0, .length = 0} }
  51. };
  52. static irqreturn_t bch_irq(int irq, void *cookie)
  53. {
  54. struct gpmi_nand_data *this = cookie;
  55. gpmi_clear_bch(this);
  56. complete(&this->bch_done);
  57. return IRQ_HANDLED;
  58. }
  59. /*
  60. * Calculate the ECC strength by hand:
  61. * E : The ECC strength.
  62. * G : the length of Galois Field.
  63. * N : The chunk count of per page.
  64. * O : the oobsize of the NAND chip.
  65. * M : the metasize of per page.
  66. *
  67. * The formula is :
  68. * E * G * N
  69. * ------------ <= (O - M)
  70. * 8
  71. *
  72. * So, we get E by:
  73. * (O - M) * 8
  74. * E <= -------------
  75. * G * N
  76. */
  77. static inline int get_ecc_strength(struct gpmi_nand_data *this)
  78. {
  79. struct bch_geometry *geo = &this->bch_geometry;
  80. struct mtd_info *mtd = &this->mtd;
  81. int ecc_strength;
  82. ecc_strength = ((mtd->oobsize - geo->metadata_size) * 8)
  83. / (geo->gf_len * geo->ecc_chunk_count);
  84. /* We need the minor even number. */
  85. return round_down(ecc_strength, 2);
  86. }
  87. static inline bool gpmi_check_ecc(struct gpmi_nand_data *this)
  88. {
  89. struct bch_geometry *geo = &this->bch_geometry;
  90. /* Do the sanity check. */
  91. if (GPMI_IS_MX23(this) || GPMI_IS_MX28(this)) {
  92. /* The mx23/mx28 only support the GF13. */
  93. if (geo->gf_len == 14)
  94. return false;
  95. if (geo->ecc_strength > MXS_ECC_STRENGTH_MAX)
  96. return false;
  97. } else if (GPMI_IS_MX6Q(this)) {
  98. if (geo->ecc_strength > MX6_ECC_STRENGTH_MAX)
  99. return false;
  100. }
  101. return true;
  102. }
  103. /*
  104. * If we can get the ECC information from the nand chip, we do not
  105. * need to calculate them ourselves.
  106. *
  107. * We may have available oob space in this case.
  108. */
  109. static bool set_geometry_by_ecc_info(struct gpmi_nand_data *this)
  110. {
  111. struct bch_geometry *geo = &this->bch_geometry;
  112. struct mtd_info *mtd = &this->mtd;
  113. struct nand_chip *chip = mtd->priv;
  114. struct nand_oobfree *of = gpmi_hw_ecclayout.oobfree;
  115. unsigned int block_mark_bit_offset;
  116. if (!(chip->ecc_strength_ds > 0 && chip->ecc_step_ds > 0))
  117. return false;
  118. switch (chip->ecc_step_ds) {
  119. case SZ_512:
  120. geo->gf_len = 13;
  121. break;
  122. case SZ_1K:
  123. geo->gf_len = 14;
  124. break;
  125. default:
  126. dev_err(this->dev,
  127. "unsupported nand chip. ecc bits : %d, ecc size : %d\n",
  128. chip->ecc_strength_ds, chip->ecc_step_ds);
  129. return false;
  130. }
  131. geo->ecc_chunk_size = chip->ecc_step_ds;
  132. geo->ecc_strength = round_up(chip->ecc_strength_ds, 2);
  133. if (!gpmi_check_ecc(this))
  134. return false;
  135. /* Keep the C >= O */
  136. if (geo->ecc_chunk_size < mtd->oobsize) {
  137. dev_err(this->dev,
  138. "unsupported nand chip. ecc size: %d, oob size : %d\n",
  139. chip->ecc_step_ds, mtd->oobsize);
  140. return false;
  141. }
  142. /* The default value, see comment in the legacy_set_geometry(). */
  143. geo->metadata_size = 10;
  144. geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunk_size;
  145. /*
  146. * Now, the NAND chip with 2K page(data chunk is 512byte) shows below:
  147. *
  148. * | P |
  149. * |<----------------------------------------------------->|
  150. * | |
  151. * | (Block Mark) |
  152. * | P' | | | |
  153. * |<-------------------------------------------->| D | | O' |
  154. * | |<---->| |<--->|
  155. * V V V V V
  156. * +---+----------+-+----------+-+----------+-+----------+-+-----+
  157. * | M | data |E| data |E| data |E| data |E| |
  158. * +---+----------+-+----------+-+----------+-+----------+-+-----+
  159. * ^ ^
  160. * | O |
  161. * |<------------>|
  162. * | |
  163. *
  164. * P : the page size for BCH module.
  165. * E : The ECC strength.
  166. * G : the length of Galois Field.
  167. * N : The chunk count of per page.
  168. * M : the metasize of per page.
  169. * C : the ecc chunk size, aka the "data" above.
  170. * P': the nand chip's page size.
  171. * O : the nand chip's oob size.
  172. * O': the free oob.
  173. *
  174. * The formula for P is :
  175. *
  176. * E * G * N
  177. * P = ------------ + P' + M
  178. * 8
  179. *
  180. * The position of block mark moves forward in the ECC-based view
  181. * of page, and the delta is:
  182. *
  183. * E * G * (N - 1)
  184. * D = (---------------- + M)
  185. * 8
  186. *
  187. * Please see the comment in legacy_set_geometry().
  188. * With the condition C >= O , we still can get same result.
  189. * So the bit position of the physical block mark within the ECC-based
  190. * view of the page is :
  191. * (P' - D) * 8
  192. */
  193. geo->page_size = mtd->writesize + geo->metadata_size +
  194. (geo->gf_len * geo->ecc_strength * geo->ecc_chunk_count) / 8;
  195. /* The available oob size we have. */
  196. if (geo->page_size < mtd->writesize + mtd->oobsize) {
  197. of->offset = geo->page_size - mtd->writesize;
  198. of->length = mtd->oobsize - of->offset;
  199. }
  200. geo->payload_size = mtd->writesize;
  201. geo->auxiliary_status_offset = ALIGN(geo->metadata_size, 4);
  202. geo->auxiliary_size = ALIGN(geo->metadata_size, 4)
  203. + ALIGN(geo->ecc_chunk_count, 4);
  204. if (!this->swap_block_mark)
  205. return true;
  206. /* For bit swap. */
  207. block_mark_bit_offset = mtd->writesize * 8 -
  208. (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
  209. + geo->metadata_size * 8);
  210. geo->block_mark_byte_offset = block_mark_bit_offset / 8;
  211. geo->block_mark_bit_offset = block_mark_bit_offset % 8;
  212. return true;
  213. }
  214. static int legacy_set_geometry(struct gpmi_nand_data *this)
  215. {
  216. struct bch_geometry *geo = &this->bch_geometry;
  217. struct mtd_info *mtd = &this->mtd;
  218. unsigned int metadata_size;
  219. unsigned int status_size;
  220. unsigned int block_mark_bit_offset;
  221. /*
  222. * The size of the metadata can be changed, though we set it to 10
  223. * bytes now. But it can't be too large, because we have to save
  224. * enough space for BCH.
  225. */
  226. geo->metadata_size = 10;
  227. /* The default for the length of Galois Field. */
  228. geo->gf_len = 13;
  229. /* The default for chunk size. */
  230. geo->ecc_chunk_size = 512;
  231. while (geo->ecc_chunk_size < mtd->oobsize) {
  232. geo->ecc_chunk_size *= 2; /* keep C >= O */
  233. geo->gf_len = 14;
  234. }
  235. geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunk_size;
  236. /* We use the same ECC strength for all chunks. */
  237. geo->ecc_strength = get_ecc_strength(this);
  238. if (!gpmi_check_ecc(this)) {
  239. dev_err(this->dev,
  240. "We can not support this nand chip."
  241. " Its required ecc strength(%d) is beyond our"
  242. " capability(%d).\n", geo->ecc_strength,
  243. (GPMI_IS_MX6Q(this) ? MX6_ECC_STRENGTH_MAX
  244. : MXS_ECC_STRENGTH_MAX));
  245. return -EINVAL;
  246. }
  247. geo->page_size = mtd->writesize + mtd->oobsize;
  248. geo->payload_size = mtd->writesize;
  249. /*
  250. * The auxiliary buffer contains the metadata and the ECC status. The
  251. * metadata is padded to the nearest 32-bit boundary. The ECC status
  252. * contains one byte for every ECC chunk, and is also padded to the
  253. * nearest 32-bit boundary.
  254. */
  255. metadata_size = ALIGN(geo->metadata_size, 4);
  256. status_size = ALIGN(geo->ecc_chunk_count, 4);
  257. geo->auxiliary_size = metadata_size + status_size;
  258. geo->auxiliary_status_offset = metadata_size;
  259. if (!this->swap_block_mark)
  260. return 0;
  261. /*
  262. * We need to compute the byte and bit offsets of
  263. * the physical block mark within the ECC-based view of the page.
  264. *
  265. * NAND chip with 2K page shows below:
  266. * (Block Mark)
  267. * | |
  268. * | D |
  269. * |<---->|
  270. * V V
  271. * +---+----------+-+----------+-+----------+-+----------+-+
  272. * | M | data |E| data |E| data |E| data |E|
  273. * +---+----------+-+----------+-+----------+-+----------+-+
  274. *
  275. * The position of block mark moves forward in the ECC-based view
  276. * of page, and the delta is:
  277. *
  278. * E * G * (N - 1)
  279. * D = (---------------- + M)
  280. * 8
  281. *
  282. * With the formula to compute the ECC strength, and the condition
  283. * : C >= O (C is the ecc chunk size)
  284. *
  285. * It's easy to deduce to the following result:
  286. *
  287. * E * G (O - M) C - M C - M
  288. * ----------- <= ------- <= -------- < ---------
  289. * 8 N N (N - 1)
  290. *
  291. * So, we get:
  292. *
  293. * E * G * (N - 1)
  294. * D = (---------------- + M) < C
  295. * 8
  296. *
  297. * The above inequality means the position of block mark
  298. * within the ECC-based view of the page is still in the data chunk,
  299. * and it's NOT in the ECC bits of the chunk.
  300. *
  301. * Use the following to compute the bit position of the
  302. * physical block mark within the ECC-based view of the page:
  303. * (page_size - D) * 8
  304. *
  305. * --Huang Shijie
  306. */
  307. block_mark_bit_offset = mtd->writesize * 8 -
  308. (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
  309. + geo->metadata_size * 8);
  310. geo->block_mark_byte_offset = block_mark_bit_offset / 8;
  311. geo->block_mark_bit_offset = block_mark_bit_offset % 8;
  312. return 0;
  313. }
  314. int common_nfc_set_geometry(struct gpmi_nand_data *this)
  315. {
  316. return legacy_set_geometry(this);
  317. }
  318. struct dma_chan *get_dma_chan(struct gpmi_nand_data *this)
  319. {
  320. /* We use the DMA channel 0 to access all the nand chips. */
  321. return this->dma_chans[0];
  322. }
  323. /* Can we use the upper's buffer directly for DMA? */
  324. void prepare_data_dma(struct gpmi_nand_data *this, enum dma_data_direction dr)
  325. {
  326. struct scatterlist *sgl = &this->data_sgl;
  327. int ret;
  328. this->direct_dma_map_ok = true;
  329. /* first try to map the upper buffer directly */
  330. sg_init_one(sgl, this->upper_buf, this->upper_len);
  331. ret = dma_map_sg(this->dev, sgl, 1, dr);
  332. if (ret == 0) {
  333. /* We have to use our own DMA buffer. */
  334. sg_init_one(sgl, this->data_buffer_dma, PAGE_SIZE);
  335. if (dr == DMA_TO_DEVICE)
  336. memcpy(this->data_buffer_dma, this->upper_buf,
  337. this->upper_len);
  338. ret = dma_map_sg(this->dev, sgl, 1, dr);
  339. if (ret == 0)
  340. pr_err("DMA mapping failed.\n");
  341. this->direct_dma_map_ok = false;
  342. }
  343. }
  344. /* This will be called after the DMA operation is finished. */
  345. static void dma_irq_callback(void *param)
  346. {
  347. struct gpmi_nand_data *this = param;
  348. struct completion *dma_c = &this->dma_done;
  349. complete(dma_c);
  350. switch (this->dma_type) {
  351. case DMA_FOR_COMMAND:
  352. dma_unmap_sg(this->dev, &this->cmd_sgl, 1, DMA_TO_DEVICE);
  353. break;
  354. case DMA_FOR_READ_DATA:
  355. dma_unmap_sg(this->dev, &this->data_sgl, 1, DMA_FROM_DEVICE);
  356. if (this->direct_dma_map_ok == false)
  357. memcpy(this->upper_buf, this->data_buffer_dma,
  358. this->upper_len);
  359. break;
  360. case DMA_FOR_WRITE_DATA:
  361. dma_unmap_sg(this->dev, &this->data_sgl, 1, DMA_TO_DEVICE);
  362. break;
  363. case DMA_FOR_READ_ECC_PAGE:
  364. case DMA_FOR_WRITE_ECC_PAGE:
  365. /* We have to wait the BCH interrupt to finish. */
  366. break;
  367. default:
  368. pr_err("in wrong DMA operation.\n");
  369. }
  370. }
  371. int start_dma_without_bch_irq(struct gpmi_nand_data *this,
  372. struct dma_async_tx_descriptor *desc)
  373. {
  374. struct completion *dma_c = &this->dma_done;
  375. int err;
  376. init_completion(dma_c);
  377. desc->callback = dma_irq_callback;
  378. desc->callback_param = this;
  379. dmaengine_submit(desc);
  380. dma_async_issue_pending(get_dma_chan(this));
  381. /* Wait for the interrupt from the DMA block. */
  382. err = wait_for_completion_timeout(dma_c, msecs_to_jiffies(1000));
  383. if (!err) {
  384. pr_err("DMA timeout, last DMA :%d\n", this->last_dma_type);
  385. gpmi_dump_info(this);
  386. return -ETIMEDOUT;
  387. }
  388. return 0;
  389. }
  390. /*
  391. * This function is used in BCH reading or BCH writing pages.
  392. * It will wait for the BCH interrupt as long as ONE second.
  393. * Actually, we must wait for two interrupts :
  394. * [1] firstly the DMA interrupt and
  395. * [2] secondly the BCH interrupt.
  396. */
  397. int start_dma_with_bch_irq(struct gpmi_nand_data *this,
  398. struct dma_async_tx_descriptor *desc)
  399. {
  400. struct completion *bch_c = &this->bch_done;
  401. int err;
  402. /* Prepare to receive an interrupt from the BCH block. */
  403. init_completion(bch_c);
  404. /* start the DMA */
  405. start_dma_without_bch_irq(this, desc);
  406. /* Wait for the interrupt from the BCH block. */
  407. err = wait_for_completion_timeout(bch_c, msecs_to_jiffies(1000));
  408. if (!err) {
  409. pr_err("BCH timeout, last DMA :%d\n", this->last_dma_type);
  410. gpmi_dump_info(this);
  411. return -ETIMEDOUT;
  412. }
  413. return 0;
  414. }
  415. static int acquire_register_block(struct gpmi_nand_data *this,
  416. const char *res_name)
  417. {
  418. struct platform_device *pdev = this->pdev;
  419. struct resources *res = &this->resources;
  420. struct resource *r;
  421. void __iomem *p;
  422. r = platform_get_resource_byname(pdev, IORESOURCE_MEM, res_name);
  423. if (!r) {
  424. pr_err("Can't get resource for %s\n", res_name);
  425. return -ENODEV;
  426. }
  427. p = ioremap(r->start, resource_size(r));
  428. if (!p) {
  429. pr_err("Can't remap %s\n", res_name);
  430. return -ENOMEM;
  431. }
  432. if (!strcmp(res_name, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME))
  433. res->gpmi_regs = p;
  434. else if (!strcmp(res_name, GPMI_NAND_BCH_REGS_ADDR_RES_NAME))
  435. res->bch_regs = p;
  436. else
  437. pr_err("unknown resource name : %s\n", res_name);
  438. return 0;
  439. }
  440. static void release_register_block(struct gpmi_nand_data *this)
  441. {
  442. struct resources *res = &this->resources;
  443. if (res->gpmi_regs)
  444. iounmap(res->gpmi_regs);
  445. if (res->bch_regs)
  446. iounmap(res->bch_regs);
  447. res->gpmi_regs = NULL;
  448. res->bch_regs = NULL;
  449. }
  450. static int acquire_bch_irq(struct gpmi_nand_data *this, irq_handler_t irq_h)
  451. {
  452. struct platform_device *pdev = this->pdev;
  453. struct resources *res = &this->resources;
  454. const char *res_name = GPMI_NAND_BCH_INTERRUPT_RES_NAME;
  455. struct resource *r;
  456. int err;
  457. r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, res_name);
  458. if (!r) {
  459. pr_err("Can't get resource for %s\n", res_name);
  460. return -ENODEV;
  461. }
  462. err = request_irq(r->start, irq_h, 0, res_name, this);
  463. if (err) {
  464. pr_err("Can't own %s\n", res_name);
  465. return err;
  466. }
  467. res->bch_low_interrupt = r->start;
  468. res->bch_high_interrupt = r->end;
  469. return 0;
  470. }
  471. static void release_bch_irq(struct gpmi_nand_data *this)
  472. {
  473. struct resources *res = &this->resources;
  474. int i = res->bch_low_interrupt;
  475. for (; i <= res->bch_high_interrupt; i++)
  476. free_irq(i, this);
  477. }
  478. static void release_dma_channels(struct gpmi_nand_data *this)
  479. {
  480. unsigned int i;
  481. for (i = 0; i < DMA_CHANS; i++)
  482. if (this->dma_chans[i]) {
  483. dma_release_channel(this->dma_chans[i]);
  484. this->dma_chans[i] = NULL;
  485. }
  486. }
  487. static int acquire_dma_channels(struct gpmi_nand_data *this)
  488. {
  489. struct platform_device *pdev = this->pdev;
  490. struct dma_chan *dma_chan;
  491. /* request dma channel */
  492. dma_chan = dma_request_slave_channel(&pdev->dev, "rx-tx");
  493. if (!dma_chan) {
  494. pr_err("Failed to request DMA channel.\n");
  495. goto acquire_err;
  496. }
  497. this->dma_chans[0] = dma_chan;
  498. return 0;
  499. acquire_err:
  500. release_dma_channels(this);
  501. return -EINVAL;
  502. }
  503. static void gpmi_put_clks(struct gpmi_nand_data *this)
  504. {
  505. struct resources *r = &this->resources;
  506. struct clk *clk;
  507. int i;
  508. for (i = 0; i < GPMI_CLK_MAX; i++) {
  509. clk = r->clock[i];
  510. if (clk) {
  511. clk_put(clk);
  512. r->clock[i] = NULL;
  513. }
  514. }
  515. }
  516. static char *extra_clks_for_mx6q[GPMI_CLK_MAX] = {
  517. "gpmi_apb", "gpmi_bch", "gpmi_bch_apb", "per1_bch",
  518. };
  519. static int gpmi_get_clks(struct gpmi_nand_data *this)
  520. {
  521. struct resources *r = &this->resources;
  522. char **extra_clks = NULL;
  523. struct clk *clk;
  524. int err, i;
  525. /* The main clock is stored in the first. */
  526. r->clock[0] = clk_get(this->dev, "gpmi_io");
  527. if (IS_ERR(r->clock[0])) {
  528. err = PTR_ERR(r->clock[0]);
  529. goto err_clock;
  530. }
  531. /* Get extra clocks */
  532. if (GPMI_IS_MX6Q(this))
  533. extra_clks = extra_clks_for_mx6q;
  534. if (!extra_clks)
  535. return 0;
  536. for (i = 1; i < GPMI_CLK_MAX; i++) {
  537. if (extra_clks[i - 1] == NULL)
  538. break;
  539. clk = clk_get(this->dev, extra_clks[i - 1]);
  540. if (IS_ERR(clk)) {
  541. err = PTR_ERR(clk);
  542. goto err_clock;
  543. }
  544. r->clock[i] = clk;
  545. }
  546. if (GPMI_IS_MX6Q(this))
  547. /*
  548. * Set the default value for the gpmi clock in mx6q:
  549. *
  550. * If you want to use the ONFI nand which is in the
  551. * Synchronous Mode, you should change the clock as you need.
  552. */
  553. clk_set_rate(r->clock[0], 22000000);
  554. return 0;
  555. err_clock:
  556. dev_dbg(this->dev, "failed in finding the clocks.\n");
  557. gpmi_put_clks(this);
  558. return err;
  559. }
  560. static int acquire_resources(struct gpmi_nand_data *this)
  561. {
  562. int ret;
  563. ret = acquire_register_block(this, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME);
  564. if (ret)
  565. goto exit_regs;
  566. ret = acquire_register_block(this, GPMI_NAND_BCH_REGS_ADDR_RES_NAME);
  567. if (ret)
  568. goto exit_regs;
  569. ret = acquire_bch_irq(this, bch_irq);
  570. if (ret)
  571. goto exit_regs;
  572. ret = acquire_dma_channels(this);
  573. if (ret)
  574. goto exit_dma_channels;
  575. ret = gpmi_get_clks(this);
  576. if (ret)
  577. goto exit_clock;
  578. return 0;
  579. exit_clock:
  580. release_dma_channels(this);
  581. exit_dma_channels:
  582. release_bch_irq(this);
  583. exit_regs:
  584. release_register_block(this);
  585. return ret;
  586. }
  587. static void release_resources(struct gpmi_nand_data *this)
  588. {
  589. gpmi_put_clks(this);
  590. release_register_block(this);
  591. release_bch_irq(this);
  592. release_dma_channels(this);
  593. }
  594. static int init_hardware(struct gpmi_nand_data *this)
  595. {
  596. int ret;
  597. /*
  598. * This structure contains the "safe" GPMI timing that should succeed
  599. * with any NAND Flash device
  600. * (although, with less-than-optimal performance).
  601. */
  602. struct nand_timing safe_timing = {
  603. .data_setup_in_ns = 80,
  604. .data_hold_in_ns = 60,
  605. .address_setup_in_ns = 25,
  606. .gpmi_sample_delay_in_ns = 6,
  607. .tREA_in_ns = -1,
  608. .tRLOH_in_ns = -1,
  609. .tRHOH_in_ns = -1,
  610. };
  611. /* Initialize the hardwares. */
  612. ret = gpmi_init(this);
  613. if (ret)
  614. return ret;
  615. this->timing = safe_timing;
  616. return 0;
  617. }
  618. static int read_page_prepare(struct gpmi_nand_data *this,
  619. void *destination, unsigned length,
  620. void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
  621. void **use_virt, dma_addr_t *use_phys)
  622. {
  623. struct device *dev = this->dev;
  624. if (virt_addr_valid(destination)) {
  625. dma_addr_t dest_phys;
  626. dest_phys = dma_map_single(dev, destination,
  627. length, DMA_FROM_DEVICE);
  628. if (dma_mapping_error(dev, dest_phys)) {
  629. if (alt_size < length) {
  630. pr_err("%s, Alternate buffer is too small\n",
  631. __func__);
  632. return -ENOMEM;
  633. }
  634. goto map_failed;
  635. }
  636. *use_virt = destination;
  637. *use_phys = dest_phys;
  638. this->direct_dma_map_ok = true;
  639. return 0;
  640. }
  641. map_failed:
  642. *use_virt = alt_virt;
  643. *use_phys = alt_phys;
  644. this->direct_dma_map_ok = false;
  645. return 0;
  646. }
  647. static inline void read_page_end(struct gpmi_nand_data *this,
  648. void *destination, unsigned length,
  649. void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
  650. void *used_virt, dma_addr_t used_phys)
  651. {
  652. if (this->direct_dma_map_ok)
  653. dma_unmap_single(this->dev, used_phys, length, DMA_FROM_DEVICE);
  654. }
  655. static inline void read_page_swap_end(struct gpmi_nand_data *this,
  656. void *destination, unsigned length,
  657. void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
  658. void *used_virt, dma_addr_t used_phys)
  659. {
  660. if (!this->direct_dma_map_ok)
  661. memcpy(destination, alt_virt, length);
  662. }
  663. static int send_page_prepare(struct gpmi_nand_data *this,
  664. const void *source, unsigned length,
  665. void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
  666. const void **use_virt, dma_addr_t *use_phys)
  667. {
  668. struct device *dev = this->dev;
  669. if (virt_addr_valid(source)) {
  670. dma_addr_t source_phys;
  671. source_phys = dma_map_single(dev, (void *)source, length,
  672. DMA_TO_DEVICE);
  673. if (dma_mapping_error(dev, source_phys)) {
  674. if (alt_size < length) {
  675. pr_err("%s, Alternate buffer is too small\n",
  676. __func__);
  677. return -ENOMEM;
  678. }
  679. goto map_failed;
  680. }
  681. *use_virt = source;
  682. *use_phys = source_phys;
  683. return 0;
  684. }
  685. map_failed:
  686. /*
  687. * Copy the content of the source buffer into the alternate
  688. * buffer and set up the return values accordingly.
  689. */
  690. memcpy(alt_virt, source, length);
  691. *use_virt = alt_virt;
  692. *use_phys = alt_phys;
  693. return 0;
  694. }
  695. static void send_page_end(struct gpmi_nand_data *this,
  696. const void *source, unsigned length,
  697. void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
  698. const void *used_virt, dma_addr_t used_phys)
  699. {
  700. struct device *dev = this->dev;
  701. if (used_virt == source)
  702. dma_unmap_single(dev, used_phys, length, DMA_TO_DEVICE);
  703. }
  704. static void gpmi_free_dma_buffer(struct gpmi_nand_data *this)
  705. {
  706. struct device *dev = this->dev;
  707. if (this->page_buffer_virt && virt_addr_valid(this->page_buffer_virt))
  708. dma_free_coherent(dev, this->page_buffer_size,
  709. this->page_buffer_virt,
  710. this->page_buffer_phys);
  711. kfree(this->cmd_buffer);
  712. kfree(this->data_buffer_dma);
  713. this->cmd_buffer = NULL;
  714. this->data_buffer_dma = NULL;
  715. this->page_buffer_virt = NULL;
  716. this->page_buffer_size = 0;
  717. }
  718. /* Allocate the DMA buffers */
  719. static int gpmi_alloc_dma_buffer(struct gpmi_nand_data *this)
  720. {
  721. struct bch_geometry *geo = &this->bch_geometry;
  722. struct device *dev = this->dev;
  723. /* [1] Allocate a command buffer. PAGE_SIZE is enough. */
  724. this->cmd_buffer = kzalloc(PAGE_SIZE, GFP_DMA | GFP_KERNEL);
  725. if (this->cmd_buffer == NULL)
  726. goto error_alloc;
  727. /* [2] Allocate a read/write data buffer. PAGE_SIZE is enough. */
  728. this->data_buffer_dma = kzalloc(PAGE_SIZE, GFP_DMA | GFP_KERNEL);
  729. if (this->data_buffer_dma == NULL)
  730. goto error_alloc;
  731. /*
  732. * [3] Allocate the page buffer.
  733. *
  734. * Both the payload buffer and the auxiliary buffer must appear on
  735. * 32-bit boundaries. We presume the size of the payload buffer is a
  736. * power of two and is much larger than four, which guarantees the
  737. * auxiliary buffer will appear on a 32-bit boundary.
  738. */
  739. this->page_buffer_size = geo->payload_size + geo->auxiliary_size;
  740. this->page_buffer_virt = dma_alloc_coherent(dev, this->page_buffer_size,
  741. &this->page_buffer_phys, GFP_DMA);
  742. if (!this->page_buffer_virt)
  743. goto error_alloc;
  744. /* Slice up the page buffer. */
  745. this->payload_virt = this->page_buffer_virt;
  746. this->payload_phys = this->page_buffer_phys;
  747. this->auxiliary_virt = this->payload_virt + geo->payload_size;
  748. this->auxiliary_phys = this->payload_phys + geo->payload_size;
  749. return 0;
  750. error_alloc:
  751. gpmi_free_dma_buffer(this);
  752. pr_err("Error allocating DMA buffers!\n");
  753. return -ENOMEM;
  754. }
  755. static void gpmi_cmd_ctrl(struct mtd_info *mtd, int data, unsigned int ctrl)
  756. {
  757. struct nand_chip *chip = mtd->priv;
  758. struct gpmi_nand_data *this = chip->priv;
  759. int ret;
  760. /*
  761. * Every operation begins with a command byte and a series of zero or
  762. * more address bytes. These are distinguished by either the Address
  763. * Latch Enable (ALE) or Command Latch Enable (CLE) signals being
  764. * asserted. When MTD is ready to execute the command, it will deassert
  765. * both latch enables.
  766. *
  767. * Rather than run a separate DMA operation for every single byte, we
  768. * queue them up and run a single DMA operation for the entire series
  769. * of command and data bytes. NAND_CMD_NONE means the END of the queue.
  770. */
  771. if ((ctrl & (NAND_ALE | NAND_CLE))) {
  772. if (data != NAND_CMD_NONE)
  773. this->cmd_buffer[this->command_length++] = data;
  774. return;
  775. }
  776. if (!this->command_length)
  777. return;
  778. ret = gpmi_send_command(this);
  779. if (ret)
  780. pr_err("Chip: %u, Error %d\n", this->current_chip, ret);
  781. this->command_length = 0;
  782. }
  783. static int gpmi_dev_ready(struct mtd_info *mtd)
  784. {
  785. struct nand_chip *chip = mtd->priv;
  786. struct gpmi_nand_data *this = chip->priv;
  787. return gpmi_is_ready(this, this->current_chip);
  788. }
  789. static void gpmi_select_chip(struct mtd_info *mtd, int chipnr)
  790. {
  791. struct nand_chip *chip = mtd->priv;
  792. struct gpmi_nand_data *this = chip->priv;
  793. if ((this->current_chip < 0) && (chipnr >= 0))
  794. gpmi_begin(this);
  795. else if ((this->current_chip >= 0) && (chipnr < 0))
  796. gpmi_end(this);
  797. this->current_chip = chipnr;
  798. }
  799. static void gpmi_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  800. {
  801. struct nand_chip *chip = mtd->priv;
  802. struct gpmi_nand_data *this = chip->priv;
  803. pr_debug("len is %d\n", len);
  804. this->upper_buf = buf;
  805. this->upper_len = len;
  806. gpmi_read_data(this);
  807. }
  808. static void gpmi_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  809. {
  810. struct nand_chip *chip = mtd->priv;
  811. struct gpmi_nand_data *this = chip->priv;
  812. pr_debug("len is %d\n", len);
  813. this->upper_buf = (uint8_t *)buf;
  814. this->upper_len = len;
  815. gpmi_send_data(this);
  816. }
  817. static uint8_t gpmi_read_byte(struct mtd_info *mtd)
  818. {
  819. struct nand_chip *chip = mtd->priv;
  820. struct gpmi_nand_data *this = chip->priv;
  821. uint8_t *buf = this->data_buffer_dma;
  822. gpmi_read_buf(mtd, buf, 1);
  823. return buf[0];
  824. }
  825. /*
  826. * Handles block mark swapping.
  827. * It can be called in swapping the block mark, or swapping it back,
  828. * because the the operations are the same.
  829. */
  830. static void block_mark_swapping(struct gpmi_nand_data *this,
  831. void *payload, void *auxiliary)
  832. {
  833. struct bch_geometry *nfc_geo = &this->bch_geometry;
  834. unsigned char *p;
  835. unsigned char *a;
  836. unsigned int bit;
  837. unsigned char mask;
  838. unsigned char from_data;
  839. unsigned char from_oob;
  840. if (!this->swap_block_mark)
  841. return;
  842. /*
  843. * If control arrives here, we're swapping. Make some convenience
  844. * variables.
  845. */
  846. bit = nfc_geo->block_mark_bit_offset;
  847. p = payload + nfc_geo->block_mark_byte_offset;
  848. a = auxiliary;
  849. /*
  850. * Get the byte from the data area that overlays the block mark. Since
  851. * the ECC engine applies its own view to the bits in the page, the
  852. * physical block mark won't (in general) appear on a byte boundary in
  853. * the data.
  854. */
  855. from_data = (p[0] >> bit) | (p[1] << (8 - bit));
  856. /* Get the byte from the OOB. */
  857. from_oob = a[0];
  858. /* Swap them. */
  859. a[0] = from_data;
  860. mask = (0x1 << bit) - 1;
  861. p[0] = (p[0] & mask) | (from_oob << bit);
  862. mask = ~0 << bit;
  863. p[1] = (p[1] & mask) | (from_oob >> (8 - bit));
  864. }
  865. static int gpmi_ecc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
  866. uint8_t *buf, int oob_required, int page)
  867. {
  868. struct gpmi_nand_data *this = chip->priv;
  869. struct bch_geometry *nfc_geo = &this->bch_geometry;
  870. void *payload_virt;
  871. dma_addr_t payload_phys;
  872. void *auxiliary_virt;
  873. dma_addr_t auxiliary_phys;
  874. unsigned int i;
  875. unsigned char *status;
  876. unsigned int max_bitflips = 0;
  877. int ret;
  878. pr_debug("page number is : %d\n", page);
  879. ret = read_page_prepare(this, buf, mtd->writesize,
  880. this->payload_virt, this->payload_phys,
  881. nfc_geo->payload_size,
  882. &payload_virt, &payload_phys);
  883. if (ret) {
  884. pr_err("Inadequate DMA buffer\n");
  885. ret = -ENOMEM;
  886. return ret;
  887. }
  888. auxiliary_virt = this->auxiliary_virt;
  889. auxiliary_phys = this->auxiliary_phys;
  890. /* go! */
  891. ret = gpmi_read_page(this, payload_phys, auxiliary_phys);
  892. read_page_end(this, buf, mtd->writesize,
  893. this->payload_virt, this->payload_phys,
  894. nfc_geo->payload_size,
  895. payload_virt, payload_phys);
  896. if (ret) {
  897. pr_err("Error in ECC-based read: %d\n", ret);
  898. return ret;
  899. }
  900. /* handle the block mark swapping */
  901. block_mark_swapping(this, payload_virt, auxiliary_virt);
  902. /* Loop over status bytes, accumulating ECC status. */
  903. status = auxiliary_virt + nfc_geo->auxiliary_status_offset;
  904. for (i = 0; i < nfc_geo->ecc_chunk_count; i++, status++) {
  905. if ((*status == STATUS_GOOD) || (*status == STATUS_ERASED))
  906. continue;
  907. if (*status == STATUS_UNCORRECTABLE) {
  908. mtd->ecc_stats.failed++;
  909. continue;
  910. }
  911. mtd->ecc_stats.corrected += *status;
  912. max_bitflips = max_t(unsigned int, max_bitflips, *status);
  913. }
  914. if (oob_required) {
  915. /*
  916. * It's time to deliver the OOB bytes. See gpmi_ecc_read_oob()
  917. * for details about our policy for delivering the OOB.
  918. *
  919. * We fill the caller's buffer with set bits, and then copy the
  920. * block mark to th caller's buffer. Note that, if block mark
  921. * swapping was necessary, it has already been done, so we can
  922. * rely on the first byte of the auxiliary buffer to contain
  923. * the block mark.
  924. */
  925. memset(chip->oob_poi, ~0, mtd->oobsize);
  926. chip->oob_poi[0] = ((uint8_t *) auxiliary_virt)[0];
  927. }
  928. read_page_swap_end(this, buf, mtd->writesize,
  929. this->payload_virt, this->payload_phys,
  930. nfc_geo->payload_size,
  931. payload_virt, payload_phys);
  932. return max_bitflips;
  933. }
  934. static int gpmi_ecc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  935. const uint8_t *buf, int oob_required)
  936. {
  937. struct gpmi_nand_data *this = chip->priv;
  938. struct bch_geometry *nfc_geo = &this->bch_geometry;
  939. const void *payload_virt;
  940. dma_addr_t payload_phys;
  941. const void *auxiliary_virt;
  942. dma_addr_t auxiliary_phys;
  943. int ret;
  944. pr_debug("ecc write page.\n");
  945. if (this->swap_block_mark) {
  946. /*
  947. * If control arrives here, we're doing block mark swapping.
  948. * Since we can't modify the caller's buffers, we must copy them
  949. * into our own.
  950. */
  951. memcpy(this->payload_virt, buf, mtd->writesize);
  952. payload_virt = this->payload_virt;
  953. payload_phys = this->payload_phys;
  954. memcpy(this->auxiliary_virt, chip->oob_poi,
  955. nfc_geo->auxiliary_size);
  956. auxiliary_virt = this->auxiliary_virt;
  957. auxiliary_phys = this->auxiliary_phys;
  958. /* Handle block mark swapping. */
  959. block_mark_swapping(this,
  960. (void *) payload_virt, (void *) auxiliary_virt);
  961. } else {
  962. /*
  963. * If control arrives here, we're not doing block mark swapping,
  964. * so we can to try and use the caller's buffers.
  965. */
  966. ret = send_page_prepare(this,
  967. buf, mtd->writesize,
  968. this->payload_virt, this->payload_phys,
  969. nfc_geo->payload_size,
  970. &payload_virt, &payload_phys);
  971. if (ret) {
  972. pr_err("Inadequate payload DMA buffer\n");
  973. return 0;
  974. }
  975. ret = send_page_prepare(this,
  976. chip->oob_poi, mtd->oobsize,
  977. this->auxiliary_virt, this->auxiliary_phys,
  978. nfc_geo->auxiliary_size,
  979. &auxiliary_virt, &auxiliary_phys);
  980. if (ret) {
  981. pr_err("Inadequate auxiliary DMA buffer\n");
  982. goto exit_auxiliary;
  983. }
  984. }
  985. /* Ask the NFC. */
  986. ret = gpmi_send_page(this, payload_phys, auxiliary_phys);
  987. if (ret)
  988. pr_err("Error in ECC-based write: %d\n", ret);
  989. if (!this->swap_block_mark) {
  990. send_page_end(this, chip->oob_poi, mtd->oobsize,
  991. this->auxiliary_virt, this->auxiliary_phys,
  992. nfc_geo->auxiliary_size,
  993. auxiliary_virt, auxiliary_phys);
  994. exit_auxiliary:
  995. send_page_end(this, buf, mtd->writesize,
  996. this->payload_virt, this->payload_phys,
  997. nfc_geo->payload_size,
  998. payload_virt, payload_phys);
  999. }
  1000. return 0;
  1001. }
  1002. /*
  1003. * There are several places in this driver where we have to handle the OOB and
  1004. * block marks. This is the function where things are the most complicated, so
  1005. * this is where we try to explain it all. All the other places refer back to
  1006. * here.
  1007. *
  1008. * These are the rules, in order of decreasing importance:
  1009. *
  1010. * 1) Nothing the caller does can be allowed to imperil the block mark.
  1011. *
  1012. * 2) In read operations, the first byte of the OOB we return must reflect the
  1013. * true state of the block mark, no matter where that block mark appears in
  1014. * the physical page.
  1015. *
  1016. * 3) ECC-based read operations return an OOB full of set bits (since we never
  1017. * allow ECC-based writes to the OOB, it doesn't matter what ECC-based reads
  1018. * return).
  1019. *
  1020. * 4) "Raw" read operations return a direct view of the physical bytes in the
  1021. * page, using the conventional definition of which bytes are data and which
  1022. * are OOB. This gives the caller a way to see the actual, physical bytes
  1023. * in the page, without the distortions applied by our ECC engine.
  1024. *
  1025. *
  1026. * What we do for this specific read operation depends on two questions:
  1027. *
  1028. * 1) Are we doing a "raw" read, or an ECC-based read?
  1029. *
  1030. * 2) Are we using block mark swapping or transcription?
  1031. *
  1032. * There are four cases, illustrated by the following Karnaugh map:
  1033. *
  1034. * | Raw | ECC-based |
  1035. * -------------+-------------------------+-------------------------+
  1036. * | Read the conventional | |
  1037. * | OOB at the end of the | |
  1038. * Swapping | page and return it. It | |
  1039. * | contains exactly what | |
  1040. * | we want. | Read the block mark and |
  1041. * -------------+-------------------------+ return it in a buffer |
  1042. * | Read the conventional | full of set bits. |
  1043. * | OOB at the end of the | |
  1044. * | page and also the block | |
  1045. * Transcribing | mark in the metadata. | |
  1046. * | Copy the block mark | |
  1047. * | into the first byte of | |
  1048. * | the OOB. | |
  1049. * -------------+-------------------------+-------------------------+
  1050. *
  1051. * Note that we break rule #4 in the Transcribing/Raw case because we're not
  1052. * giving an accurate view of the actual, physical bytes in the page (we're
  1053. * overwriting the block mark). That's OK because it's more important to follow
  1054. * rule #2.
  1055. *
  1056. * It turns out that knowing whether we want an "ECC-based" or "raw" read is not
  1057. * easy. When reading a page, for example, the NAND Flash MTD code calls our
  1058. * ecc.read_page or ecc.read_page_raw function. Thus, the fact that MTD wants an
  1059. * ECC-based or raw view of the page is implicit in which function it calls
  1060. * (there is a similar pair of ECC-based/raw functions for writing).
  1061. *
  1062. * FIXME: The following paragraph is incorrect, now that there exist
  1063. * ecc.read_oob_raw and ecc.write_oob_raw functions.
  1064. *
  1065. * Since MTD assumes the OOB is not covered by ECC, there is no pair of
  1066. * ECC-based/raw functions for reading or or writing the OOB. The fact that the
  1067. * caller wants an ECC-based or raw view of the page is not propagated down to
  1068. * this driver.
  1069. */
  1070. static int gpmi_ecc_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
  1071. int page)
  1072. {
  1073. struct gpmi_nand_data *this = chip->priv;
  1074. pr_debug("page number is %d\n", page);
  1075. /* clear the OOB buffer */
  1076. memset(chip->oob_poi, ~0, mtd->oobsize);
  1077. /* Read out the conventional OOB. */
  1078. chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
  1079. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1080. /*
  1081. * Now, we want to make sure the block mark is correct. In the
  1082. * Swapping/Raw case, we already have it. Otherwise, we need to
  1083. * explicitly read it.
  1084. */
  1085. if (!this->swap_block_mark) {
  1086. /* Read the block mark into the first byte of the OOB buffer. */
  1087. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  1088. chip->oob_poi[0] = chip->read_byte(mtd);
  1089. }
  1090. return 0;
  1091. }
  1092. static int
  1093. gpmi_ecc_write_oob(struct mtd_info *mtd, struct nand_chip *chip, int page)
  1094. {
  1095. struct nand_oobfree *of = mtd->ecclayout->oobfree;
  1096. int status = 0;
  1097. /* Do we have available oob area? */
  1098. if (!of->length)
  1099. return -EPERM;
  1100. if (!nand_is_slc(chip))
  1101. return -EPERM;
  1102. chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize + of->offset, page);
  1103. chip->write_buf(mtd, chip->oob_poi + of->offset, of->length);
  1104. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1105. status = chip->waitfunc(mtd, chip);
  1106. return status & NAND_STATUS_FAIL ? -EIO : 0;
  1107. }
  1108. static int gpmi_block_markbad(struct mtd_info *mtd, loff_t ofs)
  1109. {
  1110. struct nand_chip *chip = mtd->priv;
  1111. struct gpmi_nand_data *this = chip->priv;
  1112. int ret = 0;
  1113. uint8_t *block_mark;
  1114. int column, page, status, chipnr;
  1115. chipnr = (int)(ofs >> chip->chip_shift);
  1116. chip->select_chip(mtd, chipnr);
  1117. column = this->swap_block_mark ? mtd->writesize : 0;
  1118. /* Write the block mark. */
  1119. block_mark = this->data_buffer_dma;
  1120. block_mark[0] = 0; /* bad block marker */
  1121. /* Shift to get page */
  1122. page = (int)(ofs >> chip->page_shift);
  1123. chip->cmdfunc(mtd, NAND_CMD_SEQIN, column, page);
  1124. chip->write_buf(mtd, block_mark, 1);
  1125. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1126. status = chip->waitfunc(mtd, chip);
  1127. if (status & NAND_STATUS_FAIL)
  1128. ret = -EIO;
  1129. chip->select_chip(mtd, -1);
  1130. return ret;
  1131. }
  1132. static int nand_boot_set_geometry(struct gpmi_nand_data *this)
  1133. {
  1134. struct boot_rom_geometry *geometry = &this->rom_geometry;
  1135. /*
  1136. * Set the boot block stride size.
  1137. *
  1138. * In principle, we should be reading this from the OTP bits, since
  1139. * that's where the ROM is going to get it. In fact, we don't have any
  1140. * way to read the OTP bits, so we go with the default and hope for the
  1141. * best.
  1142. */
  1143. geometry->stride_size_in_pages = 64;
  1144. /*
  1145. * Set the search area stride exponent.
  1146. *
  1147. * In principle, we should be reading this from the OTP bits, since
  1148. * that's where the ROM is going to get it. In fact, we don't have any
  1149. * way to read the OTP bits, so we go with the default and hope for the
  1150. * best.
  1151. */
  1152. geometry->search_area_stride_exponent = 2;
  1153. return 0;
  1154. }
  1155. static const char *fingerprint = "STMP";
  1156. static int mx23_check_transcription_stamp(struct gpmi_nand_data *this)
  1157. {
  1158. struct boot_rom_geometry *rom_geo = &this->rom_geometry;
  1159. struct device *dev = this->dev;
  1160. struct mtd_info *mtd = &this->mtd;
  1161. struct nand_chip *chip = &this->nand;
  1162. unsigned int search_area_size_in_strides;
  1163. unsigned int stride;
  1164. unsigned int page;
  1165. uint8_t *buffer = chip->buffers->databuf;
  1166. int saved_chip_number;
  1167. int found_an_ncb_fingerprint = false;
  1168. /* Compute the number of strides in a search area. */
  1169. search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
  1170. saved_chip_number = this->current_chip;
  1171. chip->select_chip(mtd, 0);
  1172. /*
  1173. * Loop through the first search area, looking for the NCB fingerprint.
  1174. */
  1175. dev_dbg(dev, "Scanning for an NCB fingerprint...\n");
  1176. for (stride = 0; stride < search_area_size_in_strides; stride++) {
  1177. /* Compute the page addresses. */
  1178. page = stride * rom_geo->stride_size_in_pages;
  1179. dev_dbg(dev, "Looking for a fingerprint in page 0x%x\n", page);
  1180. /*
  1181. * Read the NCB fingerprint. The fingerprint is four bytes long
  1182. * and starts in the 12th byte of the page.
  1183. */
  1184. chip->cmdfunc(mtd, NAND_CMD_READ0, 12, page);
  1185. chip->read_buf(mtd, buffer, strlen(fingerprint));
  1186. /* Look for the fingerprint. */
  1187. if (!memcmp(buffer, fingerprint, strlen(fingerprint))) {
  1188. found_an_ncb_fingerprint = true;
  1189. break;
  1190. }
  1191. }
  1192. chip->select_chip(mtd, saved_chip_number);
  1193. if (found_an_ncb_fingerprint)
  1194. dev_dbg(dev, "\tFound a fingerprint\n");
  1195. else
  1196. dev_dbg(dev, "\tNo fingerprint found\n");
  1197. return found_an_ncb_fingerprint;
  1198. }
  1199. /* Writes a transcription stamp. */
  1200. static int mx23_write_transcription_stamp(struct gpmi_nand_data *this)
  1201. {
  1202. struct device *dev = this->dev;
  1203. struct boot_rom_geometry *rom_geo = &this->rom_geometry;
  1204. struct mtd_info *mtd = &this->mtd;
  1205. struct nand_chip *chip = &this->nand;
  1206. unsigned int block_size_in_pages;
  1207. unsigned int search_area_size_in_strides;
  1208. unsigned int search_area_size_in_pages;
  1209. unsigned int search_area_size_in_blocks;
  1210. unsigned int block;
  1211. unsigned int stride;
  1212. unsigned int page;
  1213. uint8_t *buffer = chip->buffers->databuf;
  1214. int saved_chip_number;
  1215. int status;
  1216. /* Compute the search area geometry. */
  1217. block_size_in_pages = mtd->erasesize / mtd->writesize;
  1218. search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
  1219. search_area_size_in_pages = search_area_size_in_strides *
  1220. rom_geo->stride_size_in_pages;
  1221. search_area_size_in_blocks =
  1222. (search_area_size_in_pages + (block_size_in_pages - 1)) /
  1223. block_size_in_pages;
  1224. dev_dbg(dev, "Search Area Geometry :\n");
  1225. dev_dbg(dev, "\tin Blocks : %u\n", search_area_size_in_blocks);
  1226. dev_dbg(dev, "\tin Strides: %u\n", search_area_size_in_strides);
  1227. dev_dbg(dev, "\tin Pages : %u\n", search_area_size_in_pages);
  1228. /* Select chip 0. */
  1229. saved_chip_number = this->current_chip;
  1230. chip->select_chip(mtd, 0);
  1231. /* Loop over blocks in the first search area, erasing them. */
  1232. dev_dbg(dev, "Erasing the search area...\n");
  1233. for (block = 0; block < search_area_size_in_blocks; block++) {
  1234. /* Compute the page address. */
  1235. page = block * block_size_in_pages;
  1236. /* Erase this block. */
  1237. dev_dbg(dev, "\tErasing block 0x%x\n", block);
  1238. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
  1239. chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
  1240. /* Wait for the erase to finish. */
  1241. status = chip->waitfunc(mtd, chip);
  1242. if (status & NAND_STATUS_FAIL)
  1243. dev_err(dev, "[%s] Erase failed.\n", __func__);
  1244. }
  1245. /* Write the NCB fingerprint into the page buffer. */
  1246. memset(buffer, ~0, mtd->writesize);
  1247. memset(chip->oob_poi, ~0, mtd->oobsize);
  1248. memcpy(buffer + 12, fingerprint, strlen(fingerprint));
  1249. /* Loop through the first search area, writing NCB fingerprints. */
  1250. dev_dbg(dev, "Writing NCB fingerprints...\n");
  1251. for (stride = 0; stride < search_area_size_in_strides; stride++) {
  1252. /* Compute the page addresses. */
  1253. page = stride * rom_geo->stride_size_in_pages;
  1254. /* Write the first page of the current stride. */
  1255. dev_dbg(dev, "Writing an NCB fingerprint in page 0x%x\n", page);
  1256. chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
  1257. chip->ecc.write_page_raw(mtd, chip, buffer, 0);
  1258. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1259. /* Wait for the write to finish. */
  1260. status = chip->waitfunc(mtd, chip);
  1261. if (status & NAND_STATUS_FAIL)
  1262. dev_err(dev, "[%s] Write failed.\n", __func__);
  1263. }
  1264. /* Deselect chip 0. */
  1265. chip->select_chip(mtd, saved_chip_number);
  1266. return 0;
  1267. }
  1268. static int mx23_boot_init(struct gpmi_nand_data *this)
  1269. {
  1270. struct device *dev = this->dev;
  1271. struct nand_chip *chip = &this->nand;
  1272. struct mtd_info *mtd = &this->mtd;
  1273. unsigned int block_count;
  1274. unsigned int block;
  1275. int chipnr;
  1276. int page;
  1277. loff_t byte;
  1278. uint8_t block_mark;
  1279. int ret = 0;
  1280. /*
  1281. * If control arrives here, we can't use block mark swapping, which
  1282. * means we're forced to use transcription. First, scan for the
  1283. * transcription stamp. If we find it, then we don't have to do
  1284. * anything -- the block marks are already transcribed.
  1285. */
  1286. if (mx23_check_transcription_stamp(this))
  1287. return 0;
  1288. /*
  1289. * If control arrives here, we couldn't find a transcription stamp, so
  1290. * so we presume the block marks are in the conventional location.
  1291. */
  1292. dev_dbg(dev, "Transcribing bad block marks...\n");
  1293. /* Compute the number of blocks in the entire medium. */
  1294. block_count = chip->chipsize >> chip->phys_erase_shift;
  1295. /*
  1296. * Loop over all the blocks in the medium, transcribing block marks as
  1297. * we go.
  1298. */
  1299. for (block = 0; block < block_count; block++) {
  1300. /*
  1301. * Compute the chip, page and byte addresses for this block's
  1302. * conventional mark.
  1303. */
  1304. chipnr = block >> (chip->chip_shift - chip->phys_erase_shift);
  1305. page = block << (chip->phys_erase_shift - chip->page_shift);
  1306. byte = block << chip->phys_erase_shift;
  1307. /* Send the command to read the conventional block mark. */
  1308. chip->select_chip(mtd, chipnr);
  1309. chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
  1310. block_mark = chip->read_byte(mtd);
  1311. chip->select_chip(mtd, -1);
  1312. /*
  1313. * Check if the block is marked bad. If so, we need to mark it
  1314. * again, but this time the result will be a mark in the
  1315. * location where we transcribe block marks.
  1316. */
  1317. if (block_mark != 0xff) {
  1318. dev_dbg(dev, "Transcribing mark in block %u\n", block);
  1319. ret = chip->block_markbad(mtd, byte);
  1320. if (ret)
  1321. dev_err(dev, "Failed to mark block bad with "
  1322. "ret %d\n", ret);
  1323. }
  1324. }
  1325. /* Write the stamp that indicates we've transcribed the block marks. */
  1326. mx23_write_transcription_stamp(this);
  1327. return 0;
  1328. }
  1329. static int nand_boot_init(struct gpmi_nand_data *this)
  1330. {
  1331. nand_boot_set_geometry(this);
  1332. /* This is ROM arch-specific initilization before the BBT scanning. */
  1333. if (GPMI_IS_MX23(this))
  1334. return mx23_boot_init(this);
  1335. return 0;
  1336. }
  1337. static int gpmi_set_geometry(struct gpmi_nand_data *this)
  1338. {
  1339. int ret;
  1340. /* Free the temporary DMA memory for reading ID. */
  1341. gpmi_free_dma_buffer(this);
  1342. /* Set up the NFC geometry which is used by BCH. */
  1343. ret = bch_set_geometry(this);
  1344. if (ret) {
  1345. pr_err("Error setting BCH geometry : %d\n", ret);
  1346. return ret;
  1347. }
  1348. /* Alloc the new DMA buffers according to the pagesize and oobsize */
  1349. return gpmi_alloc_dma_buffer(this);
  1350. }
  1351. static int gpmi_pre_bbt_scan(struct gpmi_nand_data *this)
  1352. {
  1353. int ret;
  1354. /* Set up swap_block_mark, must be set before the gpmi_set_geometry() */
  1355. if (GPMI_IS_MX23(this))
  1356. this->swap_block_mark = false;
  1357. else
  1358. this->swap_block_mark = true;
  1359. /* Set up the medium geometry */
  1360. ret = gpmi_set_geometry(this);
  1361. if (ret)
  1362. return ret;
  1363. /* NAND boot init, depends on the gpmi_set_geometry(). */
  1364. return nand_boot_init(this);
  1365. }
  1366. static void gpmi_nfc_exit(struct gpmi_nand_data *this)
  1367. {
  1368. nand_release(&this->mtd);
  1369. gpmi_free_dma_buffer(this);
  1370. }
  1371. static int gpmi_init_last(struct gpmi_nand_data *this)
  1372. {
  1373. struct mtd_info *mtd = &this->mtd;
  1374. struct nand_chip *chip = mtd->priv;
  1375. struct nand_ecc_ctrl *ecc = &chip->ecc;
  1376. struct bch_geometry *bch_geo = &this->bch_geometry;
  1377. int ret;
  1378. /* Prepare for the BBT scan. */
  1379. ret = gpmi_pre_bbt_scan(this);
  1380. if (ret)
  1381. return ret;
  1382. /* Init the nand_ecc_ctrl{} */
  1383. ecc->read_page = gpmi_ecc_read_page;
  1384. ecc->write_page = gpmi_ecc_write_page;
  1385. ecc->read_oob = gpmi_ecc_read_oob;
  1386. ecc->write_oob = gpmi_ecc_write_oob;
  1387. ecc->mode = NAND_ECC_HW;
  1388. ecc->size = bch_geo->ecc_chunk_size;
  1389. ecc->strength = bch_geo->ecc_strength;
  1390. ecc->layout = &gpmi_hw_ecclayout;
  1391. /*
  1392. * Can we enable the extra features? such as EDO or Sync mode.
  1393. *
  1394. * We do not check the return value now. That's means if we fail in
  1395. * enable the extra features, we still can run in the normal way.
  1396. */
  1397. gpmi_extra_init(this);
  1398. return 0;
  1399. }
  1400. static int gpmi_nfc_init(struct gpmi_nand_data *this)
  1401. {
  1402. struct mtd_info *mtd = &this->mtd;
  1403. struct nand_chip *chip = &this->nand;
  1404. struct mtd_part_parser_data ppdata = {};
  1405. int ret;
  1406. /* init current chip */
  1407. this->current_chip = -1;
  1408. /* init the MTD data structures */
  1409. mtd->priv = chip;
  1410. mtd->name = "gpmi-nand";
  1411. mtd->owner = THIS_MODULE;
  1412. /* init the nand_chip{}, we don't support a 16-bit NAND Flash bus. */
  1413. chip->priv = this;
  1414. chip->select_chip = gpmi_select_chip;
  1415. chip->cmd_ctrl = gpmi_cmd_ctrl;
  1416. chip->dev_ready = gpmi_dev_ready;
  1417. chip->read_byte = gpmi_read_byte;
  1418. chip->read_buf = gpmi_read_buf;
  1419. chip->write_buf = gpmi_write_buf;
  1420. chip->badblock_pattern = &gpmi_bbt_descr;
  1421. chip->block_markbad = gpmi_block_markbad;
  1422. chip->options |= NAND_NO_SUBPAGE_WRITE;
  1423. if (of_get_nand_on_flash_bbt(this->dev->of_node))
  1424. chip->bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
  1425. /*
  1426. * Allocate a temporary DMA buffer for reading ID in the
  1427. * nand_scan_ident().
  1428. */
  1429. this->bch_geometry.payload_size = 1024;
  1430. this->bch_geometry.auxiliary_size = 128;
  1431. ret = gpmi_alloc_dma_buffer(this);
  1432. if (ret)
  1433. goto err_out;
  1434. ret = nand_scan_ident(mtd, 2, NULL);
  1435. if (ret)
  1436. goto err_out;
  1437. ret = gpmi_init_last(this);
  1438. if (ret)
  1439. goto err_out;
  1440. ret = nand_scan_tail(mtd);
  1441. if (ret)
  1442. goto err_out;
  1443. ppdata.of_node = this->pdev->dev.of_node;
  1444. ret = mtd_device_parse_register(mtd, NULL, &ppdata, NULL, 0);
  1445. if (ret)
  1446. goto err_out;
  1447. return 0;
  1448. err_out:
  1449. gpmi_nfc_exit(this);
  1450. return ret;
  1451. }
  1452. static const struct platform_device_id gpmi_ids[] = {
  1453. { .name = "imx23-gpmi-nand", .driver_data = IS_MX23, },
  1454. { .name = "imx28-gpmi-nand", .driver_data = IS_MX28, },
  1455. { .name = "imx6q-gpmi-nand", .driver_data = IS_MX6Q, },
  1456. {}
  1457. };
  1458. static const struct of_device_id gpmi_nand_id_table[] = {
  1459. {
  1460. .compatible = "fsl,imx23-gpmi-nand",
  1461. .data = (void *)&gpmi_ids[IS_MX23],
  1462. }, {
  1463. .compatible = "fsl,imx28-gpmi-nand",
  1464. .data = (void *)&gpmi_ids[IS_MX28],
  1465. }, {
  1466. .compatible = "fsl,imx6q-gpmi-nand",
  1467. .data = (void *)&gpmi_ids[IS_MX6Q],
  1468. }, {}
  1469. };
  1470. MODULE_DEVICE_TABLE(of, gpmi_nand_id_table);
  1471. static int gpmi_nand_probe(struct platform_device *pdev)
  1472. {
  1473. struct gpmi_nand_data *this;
  1474. const struct of_device_id *of_id;
  1475. int ret;
  1476. of_id = of_match_device(gpmi_nand_id_table, &pdev->dev);
  1477. if (of_id) {
  1478. pdev->id_entry = of_id->data;
  1479. } else {
  1480. pr_err("Failed to find the right device id.\n");
  1481. return -ENODEV;
  1482. }
  1483. this = kzalloc(sizeof(*this), GFP_KERNEL);
  1484. if (!this) {
  1485. pr_err("Failed to allocate per-device memory\n");
  1486. return -ENOMEM;
  1487. }
  1488. platform_set_drvdata(pdev, this);
  1489. this->pdev = pdev;
  1490. this->dev = &pdev->dev;
  1491. ret = acquire_resources(this);
  1492. if (ret)
  1493. goto exit_acquire_resources;
  1494. ret = init_hardware(this);
  1495. if (ret)
  1496. goto exit_nfc_init;
  1497. ret = gpmi_nfc_init(this);
  1498. if (ret)
  1499. goto exit_nfc_init;
  1500. dev_info(this->dev, "driver registered.\n");
  1501. return 0;
  1502. exit_nfc_init:
  1503. release_resources(this);
  1504. exit_acquire_resources:
  1505. dev_err(this->dev, "driver registration failed: %d\n", ret);
  1506. kfree(this);
  1507. return ret;
  1508. }
  1509. static int gpmi_nand_remove(struct platform_device *pdev)
  1510. {
  1511. struct gpmi_nand_data *this = platform_get_drvdata(pdev);
  1512. gpmi_nfc_exit(this);
  1513. release_resources(this);
  1514. kfree(this);
  1515. return 0;
  1516. }
  1517. static struct platform_driver gpmi_nand_driver = {
  1518. .driver = {
  1519. .name = "gpmi-nand",
  1520. .of_match_table = gpmi_nand_id_table,
  1521. },
  1522. .probe = gpmi_nand_probe,
  1523. .remove = gpmi_nand_remove,
  1524. .id_table = gpmi_ids,
  1525. };
  1526. module_platform_driver(gpmi_nand_driver);
  1527. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  1528. MODULE_DESCRIPTION("i.MX GPMI NAND Flash Controller Driver");
  1529. MODULE_LICENSE("GPL");