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