gpmi-nand.c 45 KB

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