gpmi-nand.c 46 KB

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