gpmi-nand.c 46 KB

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