gpmi-nand.c 45 KB

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