gpmi-nand.c 45 KB

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