gpmi-nand.c 49 KB

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