gpmi-nand.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695
  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("map 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("dma_request_channel failed.\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("Alternate buffer is too small\n");
  535. return -ENOMEM;
  536. }
  537. goto map_failed;
  538. }
  539. *use_virt = destination;
  540. *use_phys = dest_phys;
  541. this->direct_dma_map_ok = true;
  542. return 0;
  543. }
  544. map_failed:
  545. *use_virt = alt_virt;
  546. *use_phys = alt_phys;
  547. this->direct_dma_map_ok = false;
  548. return 0;
  549. }
  550. static inline void read_page_end(struct gpmi_nand_data *this,
  551. void *destination, unsigned length,
  552. void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
  553. void *used_virt, dma_addr_t used_phys)
  554. {
  555. if (this->direct_dma_map_ok)
  556. dma_unmap_single(this->dev, used_phys, length, DMA_FROM_DEVICE);
  557. }
  558. static inline void read_page_swap_end(struct gpmi_nand_data *this,
  559. void *destination, unsigned length,
  560. void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
  561. void *used_virt, dma_addr_t used_phys)
  562. {
  563. if (!this->direct_dma_map_ok)
  564. memcpy(destination, alt_virt, length);
  565. }
  566. static int send_page_prepare(struct gpmi_nand_data *this,
  567. const void *source, unsigned length,
  568. void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
  569. const void **use_virt, dma_addr_t *use_phys)
  570. {
  571. struct device *dev = this->dev;
  572. if (virt_addr_valid(source)) {
  573. dma_addr_t source_phys;
  574. source_phys = dma_map_single(dev, (void *)source, length,
  575. DMA_TO_DEVICE);
  576. if (dma_mapping_error(dev, source_phys)) {
  577. if (alt_size < length) {
  578. pr_err("Alternate buffer is too small\n");
  579. return -ENOMEM;
  580. }
  581. goto map_failed;
  582. }
  583. *use_virt = source;
  584. *use_phys = source_phys;
  585. return 0;
  586. }
  587. map_failed:
  588. /*
  589. * Copy the content of the source buffer into the alternate
  590. * buffer and set up the return values accordingly.
  591. */
  592. memcpy(alt_virt, source, length);
  593. *use_virt = alt_virt;
  594. *use_phys = alt_phys;
  595. return 0;
  596. }
  597. static void send_page_end(struct gpmi_nand_data *this,
  598. const void *source, unsigned length,
  599. void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
  600. const void *used_virt, dma_addr_t used_phys)
  601. {
  602. struct device *dev = this->dev;
  603. if (used_virt == source)
  604. dma_unmap_single(dev, used_phys, length, DMA_TO_DEVICE);
  605. }
  606. static void gpmi_free_dma_buffer(struct gpmi_nand_data *this)
  607. {
  608. struct device *dev = this->dev;
  609. if (this->page_buffer_virt && virt_addr_valid(this->page_buffer_virt))
  610. dma_free_coherent(dev, this->page_buffer_size,
  611. this->page_buffer_virt,
  612. this->page_buffer_phys);
  613. kfree(this->cmd_buffer);
  614. kfree(this->data_buffer_dma);
  615. this->cmd_buffer = NULL;
  616. this->data_buffer_dma = NULL;
  617. this->page_buffer_virt = NULL;
  618. this->page_buffer_size = 0;
  619. }
  620. /* Allocate the DMA buffers */
  621. static int gpmi_alloc_dma_buffer(struct gpmi_nand_data *this)
  622. {
  623. struct bch_geometry *geo = &this->bch_geometry;
  624. struct device *dev = this->dev;
  625. /* [1] Allocate a command buffer. PAGE_SIZE is enough. */
  626. this->cmd_buffer = kzalloc(PAGE_SIZE, GFP_DMA | GFP_KERNEL);
  627. if (this->cmd_buffer == NULL)
  628. goto error_alloc;
  629. /* [2] Allocate a read/write data buffer. PAGE_SIZE is enough. */
  630. this->data_buffer_dma = kzalloc(PAGE_SIZE, GFP_DMA | GFP_KERNEL);
  631. if (this->data_buffer_dma == NULL)
  632. goto error_alloc;
  633. /*
  634. * [3] Allocate the page buffer.
  635. *
  636. * Both the payload buffer and the auxiliary buffer must appear on
  637. * 32-bit boundaries. We presume the size of the payload buffer is a
  638. * power of two and is much larger than four, which guarantees the
  639. * auxiliary buffer will appear on a 32-bit boundary.
  640. */
  641. this->page_buffer_size = geo->payload_size + geo->auxiliary_size;
  642. this->page_buffer_virt = dma_alloc_coherent(dev, this->page_buffer_size,
  643. &this->page_buffer_phys, GFP_DMA);
  644. if (!this->page_buffer_virt)
  645. goto error_alloc;
  646. /* Slice up the page buffer. */
  647. this->payload_virt = this->page_buffer_virt;
  648. this->payload_phys = this->page_buffer_phys;
  649. this->auxiliary_virt = this->payload_virt + geo->payload_size;
  650. this->auxiliary_phys = this->payload_phys + geo->payload_size;
  651. return 0;
  652. error_alloc:
  653. gpmi_free_dma_buffer(this);
  654. pr_err("allocate DMA buffer ret!!\n");
  655. return -ENOMEM;
  656. }
  657. static void gpmi_cmd_ctrl(struct mtd_info *mtd, int data, unsigned int ctrl)
  658. {
  659. struct nand_chip *chip = mtd->priv;
  660. struct gpmi_nand_data *this = chip->priv;
  661. int ret;
  662. /*
  663. * Every operation begins with a command byte and a series of zero or
  664. * more address bytes. These are distinguished by either the Address
  665. * Latch Enable (ALE) or Command Latch Enable (CLE) signals being
  666. * asserted. When MTD is ready to execute the command, it will deassert
  667. * both latch enables.
  668. *
  669. * Rather than run a separate DMA operation for every single byte, we
  670. * queue them up and run a single DMA operation for the entire series
  671. * of command and data bytes. NAND_CMD_NONE means the END of the queue.
  672. */
  673. if ((ctrl & (NAND_ALE | NAND_CLE))) {
  674. if (data != NAND_CMD_NONE)
  675. this->cmd_buffer[this->command_length++] = data;
  676. return;
  677. }
  678. if (!this->command_length)
  679. return;
  680. ret = gpmi_send_command(this);
  681. if (ret)
  682. pr_err("Chip: %u, Error %d\n", this->current_chip, ret);
  683. this->command_length = 0;
  684. }
  685. static int gpmi_dev_ready(struct mtd_info *mtd)
  686. {
  687. struct nand_chip *chip = mtd->priv;
  688. struct gpmi_nand_data *this = chip->priv;
  689. return gpmi_is_ready(this, this->current_chip);
  690. }
  691. static void gpmi_select_chip(struct mtd_info *mtd, int chipnr)
  692. {
  693. struct nand_chip *chip = mtd->priv;
  694. struct gpmi_nand_data *this = chip->priv;
  695. if ((this->current_chip < 0) && (chipnr >= 0))
  696. gpmi_begin(this);
  697. else if ((this->current_chip >= 0) && (chipnr < 0))
  698. gpmi_end(this);
  699. this->current_chip = chipnr;
  700. }
  701. static void gpmi_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  702. {
  703. struct nand_chip *chip = mtd->priv;
  704. struct gpmi_nand_data *this = chip->priv;
  705. pr_debug("len is %d\n", len);
  706. this->upper_buf = buf;
  707. this->upper_len = len;
  708. gpmi_read_data(this);
  709. }
  710. static void gpmi_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  711. {
  712. struct nand_chip *chip = mtd->priv;
  713. struct gpmi_nand_data *this = chip->priv;
  714. pr_debug("len is %d\n", len);
  715. this->upper_buf = (uint8_t *)buf;
  716. this->upper_len = len;
  717. gpmi_send_data(this);
  718. }
  719. static uint8_t gpmi_read_byte(struct mtd_info *mtd)
  720. {
  721. struct nand_chip *chip = mtd->priv;
  722. struct gpmi_nand_data *this = chip->priv;
  723. uint8_t *buf = this->data_buffer_dma;
  724. gpmi_read_buf(mtd, buf, 1);
  725. return buf[0];
  726. }
  727. /*
  728. * Handles block mark swapping.
  729. * It can be called in swapping the block mark, or swapping it back,
  730. * because the the operations are the same.
  731. */
  732. static void block_mark_swapping(struct gpmi_nand_data *this,
  733. void *payload, void *auxiliary)
  734. {
  735. struct bch_geometry *nfc_geo = &this->bch_geometry;
  736. unsigned char *p;
  737. unsigned char *a;
  738. unsigned int bit;
  739. unsigned char mask;
  740. unsigned char from_data;
  741. unsigned char from_oob;
  742. if (!this->swap_block_mark)
  743. return;
  744. /*
  745. * If control arrives here, we're swapping. Make some convenience
  746. * variables.
  747. */
  748. bit = nfc_geo->block_mark_bit_offset;
  749. p = payload + nfc_geo->block_mark_byte_offset;
  750. a = auxiliary;
  751. /*
  752. * Get the byte from the data area that overlays the block mark. Since
  753. * the ECC engine applies its own view to the bits in the page, the
  754. * physical block mark won't (in general) appear on a byte boundary in
  755. * the data.
  756. */
  757. from_data = (p[0] >> bit) | (p[1] << (8 - bit));
  758. /* Get the byte from the OOB. */
  759. from_oob = a[0];
  760. /* Swap them. */
  761. a[0] = from_data;
  762. mask = (0x1 << bit) - 1;
  763. p[0] = (p[0] & mask) | (from_oob << bit);
  764. mask = ~0 << bit;
  765. p[1] = (p[1] & mask) | (from_oob >> (8 - bit));
  766. }
  767. static int gpmi_ecc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
  768. uint8_t *buf, int oob_required, int page)
  769. {
  770. struct gpmi_nand_data *this = chip->priv;
  771. struct bch_geometry *nfc_geo = &this->bch_geometry;
  772. void *payload_virt;
  773. dma_addr_t payload_phys;
  774. void *auxiliary_virt;
  775. dma_addr_t auxiliary_phys;
  776. unsigned int i;
  777. unsigned char *status;
  778. unsigned int failed;
  779. unsigned int corrected;
  780. int ret;
  781. pr_debug("page number is : %d\n", page);
  782. ret = read_page_prepare(this, buf, mtd->writesize,
  783. this->payload_virt, this->payload_phys,
  784. nfc_geo->payload_size,
  785. &payload_virt, &payload_phys);
  786. if (ret) {
  787. pr_err("Inadequate DMA buffer\n");
  788. ret = -ENOMEM;
  789. return ret;
  790. }
  791. auxiliary_virt = this->auxiliary_virt;
  792. auxiliary_phys = this->auxiliary_phys;
  793. /* go! */
  794. ret = gpmi_read_page(this, payload_phys, auxiliary_phys);
  795. read_page_end(this, buf, mtd->writesize,
  796. this->payload_virt, this->payload_phys,
  797. nfc_geo->payload_size,
  798. payload_virt, payload_phys);
  799. if (ret) {
  800. pr_err("Error in ECC-based read: %d\n", ret);
  801. goto exit_nfc;
  802. }
  803. /* handle the block mark swapping */
  804. block_mark_swapping(this, payload_virt, auxiliary_virt);
  805. /* Loop over status bytes, accumulating ECC status. */
  806. failed = 0;
  807. corrected = 0;
  808. status = auxiliary_virt + nfc_geo->auxiliary_status_offset;
  809. for (i = 0; i < nfc_geo->ecc_chunk_count; i++, status++) {
  810. if ((*status == STATUS_GOOD) || (*status == STATUS_ERASED))
  811. continue;
  812. if (*status == STATUS_UNCORRECTABLE) {
  813. failed++;
  814. continue;
  815. }
  816. corrected += *status;
  817. }
  818. /*
  819. * Propagate ECC status to the owning MTD only when failed or
  820. * corrected times nearly reaches our ECC correction threshold.
  821. */
  822. if (failed || corrected >= (nfc_geo->ecc_strength - 1)) {
  823. mtd->ecc_stats.failed += failed;
  824. mtd->ecc_stats.corrected += corrected;
  825. }
  826. if (oob_required) {
  827. /*
  828. * It's time to deliver the OOB bytes. See gpmi_ecc_read_oob()
  829. * for details about our policy for delivering the OOB.
  830. *
  831. * We fill the caller's buffer with set bits, and then copy the
  832. * block mark to th caller's buffer. Note that, if block mark
  833. * swapping was necessary, it has already been done, so we can
  834. * rely on the first byte of the auxiliary buffer to contain
  835. * the block mark.
  836. */
  837. memset(chip->oob_poi, ~0, mtd->oobsize);
  838. chip->oob_poi[0] = ((uint8_t *) auxiliary_virt)[0];
  839. }
  840. read_page_swap_end(this, buf, mtd->writesize,
  841. this->payload_virt, this->payload_phys,
  842. nfc_geo->payload_size,
  843. payload_virt, payload_phys);
  844. exit_nfc:
  845. return ret;
  846. }
  847. static int gpmi_ecc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  848. const uint8_t *buf, int oob_required)
  849. {
  850. struct gpmi_nand_data *this = chip->priv;
  851. struct bch_geometry *nfc_geo = &this->bch_geometry;
  852. const void *payload_virt;
  853. dma_addr_t payload_phys;
  854. const void *auxiliary_virt;
  855. dma_addr_t auxiliary_phys;
  856. int ret;
  857. pr_debug("ecc write page.\n");
  858. if (this->swap_block_mark) {
  859. /*
  860. * If control arrives here, we're doing block mark swapping.
  861. * Since we can't modify the caller's buffers, we must copy them
  862. * into our own.
  863. */
  864. memcpy(this->payload_virt, buf, mtd->writesize);
  865. payload_virt = this->payload_virt;
  866. payload_phys = this->payload_phys;
  867. memcpy(this->auxiliary_virt, chip->oob_poi,
  868. nfc_geo->auxiliary_size);
  869. auxiliary_virt = this->auxiliary_virt;
  870. auxiliary_phys = this->auxiliary_phys;
  871. /* Handle block mark swapping. */
  872. block_mark_swapping(this,
  873. (void *) payload_virt, (void *) auxiliary_virt);
  874. } else {
  875. /*
  876. * If control arrives here, we're not doing block mark swapping,
  877. * so we can to try and use the caller's buffers.
  878. */
  879. ret = send_page_prepare(this,
  880. buf, mtd->writesize,
  881. this->payload_virt, this->payload_phys,
  882. nfc_geo->payload_size,
  883. &payload_virt, &payload_phys);
  884. if (ret) {
  885. pr_err("Inadequate payload DMA buffer\n");
  886. return 0;
  887. }
  888. ret = send_page_prepare(this,
  889. chip->oob_poi, mtd->oobsize,
  890. this->auxiliary_virt, this->auxiliary_phys,
  891. nfc_geo->auxiliary_size,
  892. &auxiliary_virt, &auxiliary_phys);
  893. if (ret) {
  894. pr_err("Inadequate auxiliary DMA buffer\n");
  895. goto exit_auxiliary;
  896. }
  897. }
  898. /* Ask the NFC. */
  899. ret = gpmi_send_page(this, payload_phys, auxiliary_phys);
  900. if (ret)
  901. pr_err("Error in ECC-based write: %d\n", ret);
  902. if (!this->swap_block_mark) {
  903. send_page_end(this, chip->oob_poi, mtd->oobsize,
  904. this->auxiliary_virt, this->auxiliary_phys,
  905. nfc_geo->auxiliary_size,
  906. auxiliary_virt, auxiliary_phys);
  907. exit_auxiliary:
  908. send_page_end(this, buf, mtd->writesize,
  909. this->payload_virt, this->payload_phys,
  910. nfc_geo->payload_size,
  911. payload_virt, payload_phys);
  912. }
  913. return 0;
  914. }
  915. /*
  916. * There are several places in this driver where we have to handle the OOB and
  917. * block marks. This is the function where things are the most complicated, so
  918. * this is where we try to explain it all. All the other places refer back to
  919. * here.
  920. *
  921. * These are the rules, in order of decreasing importance:
  922. *
  923. * 1) Nothing the caller does can be allowed to imperil the block mark.
  924. *
  925. * 2) In read operations, the first byte of the OOB we return must reflect the
  926. * true state of the block mark, no matter where that block mark appears in
  927. * the physical page.
  928. *
  929. * 3) ECC-based read operations return an OOB full of set bits (since we never
  930. * allow ECC-based writes to the OOB, it doesn't matter what ECC-based reads
  931. * return).
  932. *
  933. * 4) "Raw" read operations return a direct view of the physical bytes in the
  934. * page, using the conventional definition of which bytes are data and which
  935. * are OOB. This gives the caller a way to see the actual, physical bytes
  936. * in the page, without the distortions applied by our ECC engine.
  937. *
  938. *
  939. * What we do for this specific read operation depends on two questions:
  940. *
  941. * 1) Are we doing a "raw" read, or an ECC-based read?
  942. *
  943. * 2) Are we using block mark swapping or transcription?
  944. *
  945. * There are four cases, illustrated by the following Karnaugh map:
  946. *
  947. * | Raw | ECC-based |
  948. * -------------+-------------------------+-------------------------+
  949. * | Read the conventional | |
  950. * | OOB at the end of the | |
  951. * Swapping | page and return it. It | |
  952. * | contains exactly what | |
  953. * | we want. | Read the block mark and |
  954. * -------------+-------------------------+ return it in a buffer |
  955. * | Read the conventional | full of set bits. |
  956. * | OOB at the end of the | |
  957. * | page and also the block | |
  958. * Transcribing | mark in the metadata. | |
  959. * | Copy the block mark | |
  960. * | into the first byte of | |
  961. * | the OOB. | |
  962. * -------------+-------------------------+-------------------------+
  963. *
  964. * Note that we break rule #4 in the Transcribing/Raw case because we're not
  965. * giving an accurate view of the actual, physical bytes in the page (we're
  966. * overwriting the block mark). That's OK because it's more important to follow
  967. * rule #2.
  968. *
  969. * It turns out that knowing whether we want an "ECC-based" or "raw" read is not
  970. * easy. When reading a page, for example, the NAND Flash MTD code calls our
  971. * ecc.read_page or ecc.read_page_raw function. Thus, the fact that MTD wants an
  972. * ECC-based or raw view of the page is implicit in which function it calls
  973. * (there is a similar pair of ECC-based/raw functions for writing).
  974. *
  975. * FIXME: The following paragraph is incorrect, now that there exist
  976. * ecc.read_oob_raw and ecc.write_oob_raw functions.
  977. *
  978. * Since MTD assumes the OOB is not covered by ECC, there is no pair of
  979. * ECC-based/raw functions for reading or or writing the OOB. The fact that the
  980. * caller wants an ECC-based or raw view of the page is not propagated down to
  981. * this driver.
  982. */
  983. static int gpmi_ecc_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
  984. int page)
  985. {
  986. struct gpmi_nand_data *this = chip->priv;
  987. pr_debug("page number is %d\n", page);
  988. /* clear the OOB buffer */
  989. memset(chip->oob_poi, ~0, mtd->oobsize);
  990. /* Read out the conventional OOB. */
  991. chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
  992. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  993. /*
  994. * Now, we want to make sure the block mark is correct. In the
  995. * Swapping/Raw case, we already have it. Otherwise, we need to
  996. * explicitly read it.
  997. */
  998. if (!this->swap_block_mark) {
  999. /* Read the block mark into the first byte of the OOB buffer. */
  1000. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  1001. chip->oob_poi[0] = chip->read_byte(mtd);
  1002. }
  1003. return 0;
  1004. }
  1005. static int
  1006. gpmi_ecc_write_oob(struct mtd_info *mtd, struct nand_chip *chip, int page)
  1007. {
  1008. /*
  1009. * The BCH will use all the (page + oob).
  1010. * Our gpmi_hw_ecclayout can only prohibit the JFFS2 to write the oob.
  1011. * But it can not stop some ioctls such MEMWRITEOOB which uses
  1012. * MTD_OPS_PLACE_OOB. So We have to implement this function to prohibit
  1013. * these ioctls too.
  1014. */
  1015. return -EPERM;
  1016. }
  1017. static int gpmi_block_markbad(struct mtd_info *mtd, loff_t ofs)
  1018. {
  1019. struct nand_chip *chip = mtd->priv;
  1020. struct gpmi_nand_data *this = chip->priv;
  1021. int block, ret = 0;
  1022. uint8_t *block_mark;
  1023. int column, page, status, chipnr;
  1024. /* Get block number */
  1025. block = (int)(ofs >> chip->bbt_erase_shift);
  1026. if (chip->bbt)
  1027. chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
  1028. /* Do we have a flash based bad block table ? */
  1029. if (chip->bbt_options & NAND_BBT_USE_FLASH)
  1030. ret = nand_update_bbt(mtd, ofs);
  1031. else {
  1032. chipnr = (int)(ofs >> chip->chip_shift);
  1033. chip->select_chip(mtd, chipnr);
  1034. column = this->swap_block_mark ? mtd->writesize : 0;
  1035. /* Write the block mark. */
  1036. block_mark = this->data_buffer_dma;
  1037. block_mark[0] = 0; /* bad block marker */
  1038. /* Shift to get page */
  1039. page = (int)(ofs >> chip->page_shift);
  1040. chip->cmdfunc(mtd, NAND_CMD_SEQIN, column, page);
  1041. chip->write_buf(mtd, block_mark, 1);
  1042. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1043. status = chip->waitfunc(mtd, chip);
  1044. if (status & NAND_STATUS_FAIL)
  1045. ret = -EIO;
  1046. chip->select_chip(mtd, -1);
  1047. }
  1048. if (!ret)
  1049. mtd->ecc_stats.badblocks++;
  1050. return ret;
  1051. }
  1052. static int nand_boot_set_geometry(struct gpmi_nand_data *this)
  1053. {
  1054. struct boot_rom_geometry *geometry = &this->rom_geometry;
  1055. /*
  1056. * Set the boot block stride size.
  1057. *
  1058. * In principle, we should be reading this from the OTP bits, since
  1059. * that's where the ROM is going to get it. In fact, we don't have any
  1060. * way to read the OTP bits, so we go with the default and hope for the
  1061. * best.
  1062. */
  1063. geometry->stride_size_in_pages = 64;
  1064. /*
  1065. * Set the search area stride exponent.
  1066. *
  1067. * In principle, we should be reading this from the OTP bits, since
  1068. * that's where the ROM is going to get it. In fact, we don't have any
  1069. * way to read the OTP bits, so we go with the default and hope for the
  1070. * best.
  1071. */
  1072. geometry->search_area_stride_exponent = 2;
  1073. return 0;
  1074. }
  1075. static const char *fingerprint = "STMP";
  1076. static int mx23_check_transcription_stamp(struct gpmi_nand_data *this)
  1077. {
  1078. struct boot_rom_geometry *rom_geo = &this->rom_geometry;
  1079. struct device *dev = this->dev;
  1080. struct mtd_info *mtd = &this->mtd;
  1081. struct nand_chip *chip = &this->nand;
  1082. unsigned int search_area_size_in_strides;
  1083. unsigned int stride;
  1084. unsigned int page;
  1085. uint8_t *buffer = chip->buffers->databuf;
  1086. int saved_chip_number;
  1087. int found_an_ncb_fingerprint = false;
  1088. /* Compute the number of strides in a search area. */
  1089. search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
  1090. saved_chip_number = this->current_chip;
  1091. chip->select_chip(mtd, 0);
  1092. /*
  1093. * Loop through the first search area, looking for the NCB fingerprint.
  1094. */
  1095. dev_dbg(dev, "Scanning for an NCB fingerprint...\n");
  1096. for (stride = 0; stride < search_area_size_in_strides; stride++) {
  1097. /* Compute the page addresses. */
  1098. page = stride * rom_geo->stride_size_in_pages;
  1099. dev_dbg(dev, "Looking for a fingerprint in page 0x%x\n", page);
  1100. /*
  1101. * Read the NCB fingerprint. The fingerprint is four bytes long
  1102. * and starts in the 12th byte of the page.
  1103. */
  1104. chip->cmdfunc(mtd, NAND_CMD_READ0, 12, page);
  1105. chip->read_buf(mtd, buffer, strlen(fingerprint));
  1106. /* Look for the fingerprint. */
  1107. if (!memcmp(buffer, fingerprint, strlen(fingerprint))) {
  1108. found_an_ncb_fingerprint = true;
  1109. break;
  1110. }
  1111. }
  1112. chip->select_chip(mtd, saved_chip_number);
  1113. if (found_an_ncb_fingerprint)
  1114. dev_dbg(dev, "\tFound a fingerprint\n");
  1115. else
  1116. dev_dbg(dev, "\tNo fingerprint found\n");
  1117. return found_an_ncb_fingerprint;
  1118. }
  1119. /* Writes a transcription stamp. */
  1120. static int mx23_write_transcription_stamp(struct gpmi_nand_data *this)
  1121. {
  1122. struct device *dev = this->dev;
  1123. struct boot_rom_geometry *rom_geo = &this->rom_geometry;
  1124. struct mtd_info *mtd = &this->mtd;
  1125. struct nand_chip *chip = &this->nand;
  1126. unsigned int block_size_in_pages;
  1127. unsigned int search_area_size_in_strides;
  1128. unsigned int search_area_size_in_pages;
  1129. unsigned int search_area_size_in_blocks;
  1130. unsigned int block;
  1131. unsigned int stride;
  1132. unsigned int page;
  1133. uint8_t *buffer = chip->buffers->databuf;
  1134. int saved_chip_number;
  1135. int status;
  1136. /* Compute the search area geometry. */
  1137. block_size_in_pages = mtd->erasesize / mtd->writesize;
  1138. search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
  1139. search_area_size_in_pages = search_area_size_in_strides *
  1140. rom_geo->stride_size_in_pages;
  1141. search_area_size_in_blocks =
  1142. (search_area_size_in_pages + (block_size_in_pages - 1)) /
  1143. block_size_in_pages;
  1144. dev_dbg(dev, "Search Area Geometry :\n");
  1145. dev_dbg(dev, "\tin Blocks : %u\n", search_area_size_in_blocks);
  1146. dev_dbg(dev, "\tin Strides: %u\n", search_area_size_in_strides);
  1147. dev_dbg(dev, "\tin Pages : %u\n", search_area_size_in_pages);
  1148. /* Select chip 0. */
  1149. saved_chip_number = this->current_chip;
  1150. chip->select_chip(mtd, 0);
  1151. /* Loop over blocks in the first search area, erasing them. */
  1152. dev_dbg(dev, "Erasing the search area...\n");
  1153. for (block = 0; block < search_area_size_in_blocks; block++) {
  1154. /* Compute the page address. */
  1155. page = block * block_size_in_pages;
  1156. /* Erase this block. */
  1157. dev_dbg(dev, "\tErasing block 0x%x\n", block);
  1158. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
  1159. chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
  1160. /* Wait for the erase to finish. */
  1161. status = chip->waitfunc(mtd, chip);
  1162. if (status & NAND_STATUS_FAIL)
  1163. dev_err(dev, "[%s] Erase failed.\n", __func__);
  1164. }
  1165. /* Write the NCB fingerprint into the page buffer. */
  1166. memset(buffer, ~0, mtd->writesize);
  1167. memset(chip->oob_poi, ~0, mtd->oobsize);
  1168. memcpy(buffer + 12, fingerprint, strlen(fingerprint));
  1169. /* Loop through the first search area, writing NCB fingerprints. */
  1170. dev_dbg(dev, "Writing NCB fingerprints...\n");
  1171. for (stride = 0; stride < search_area_size_in_strides; stride++) {
  1172. /* Compute the page addresses. */
  1173. page = stride * rom_geo->stride_size_in_pages;
  1174. /* Write the first page of the current stride. */
  1175. dev_dbg(dev, "Writing an NCB fingerprint in page 0x%x\n", page);
  1176. chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
  1177. chip->ecc.write_page_raw(mtd, chip, buffer, 0);
  1178. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1179. /* Wait for the write to finish. */
  1180. status = chip->waitfunc(mtd, chip);
  1181. if (status & NAND_STATUS_FAIL)
  1182. dev_err(dev, "[%s] Write failed.\n", __func__);
  1183. }
  1184. /* Deselect chip 0. */
  1185. chip->select_chip(mtd, saved_chip_number);
  1186. return 0;
  1187. }
  1188. static int mx23_boot_init(struct gpmi_nand_data *this)
  1189. {
  1190. struct device *dev = this->dev;
  1191. struct nand_chip *chip = &this->nand;
  1192. struct mtd_info *mtd = &this->mtd;
  1193. unsigned int block_count;
  1194. unsigned int block;
  1195. int chipnr;
  1196. int page;
  1197. loff_t byte;
  1198. uint8_t block_mark;
  1199. int ret = 0;
  1200. /*
  1201. * If control arrives here, we can't use block mark swapping, which
  1202. * means we're forced to use transcription. First, scan for the
  1203. * transcription stamp. If we find it, then we don't have to do
  1204. * anything -- the block marks are already transcribed.
  1205. */
  1206. if (mx23_check_transcription_stamp(this))
  1207. return 0;
  1208. /*
  1209. * If control arrives here, we couldn't find a transcription stamp, so
  1210. * so we presume the block marks are in the conventional location.
  1211. */
  1212. dev_dbg(dev, "Transcribing bad block marks...\n");
  1213. /* Compute the number of blocks in the entire medium. */
  1214. block_count = chip->chipsize >> chip->phys_erase_shift;
  1215. /*
  1216. * Loop over all the blocks in the medium, transcribing block marks as
  1217. * we go.
  1218. */
  1219. for (block = 0; block < block_count; block++) {
  1220. /*
  1221. * Compute the chip, page and byte addresses for this block's
  1222. * conventional mark.
  1223. */
  1224. chipnr = block >> (chip->chip_shift - chip->phys_erase_shift);
  1225. page = block << (chip->phys_erase_shift - chip->page_shift);
  1226. byte = block << chip->phys_erase_shift;
  1227. /* Send the command to read the conventional block mark. */
  1228. chip->select_chip(mtd, chipnr);
  1229. chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
  1230. block_mark = chip->read_byte(mtd);
  1231. chip->select_chip(mtd, -1);
  1232. /*
  1233. * Check if the block is marked bad. If so, we need to mark it
  1234. * again, but this time the result will be a mark in the
  1235. * location where we transcribe block marks.
  1236. */
  1237. if (block_mark != 0xff) {
  1238. dev_dbg(dev, "Transcribing mark in block %u\n", block);
  1239. ret = chip->block_markbad(mtd, byte);
  1240. if (ret)
  1241. dev_err(dev, "Failed to mark block bad with "
  1242. "ret %d\n", ret);
  1243. }
  1244. }
  1245. /* Write the stamp that indicates we've transcribed the block marks. */
  1246. mx23_write_transcription_stamp(this);
  1247. return 0;
  1248. }
  1249. static int nand_boot_init(struct gpmi_nand_data *this)
  1250. {
  1251. nand_boot_set_geometry(this);
  1252. /* This is ROM arch-specific initilization before the BBT scanning. */
  1253. if (GPMI_IS_MX23(this))
  1254. return mx23_boot_init(this);
  1255. return 0;
  1256. }
  1257. static int gpmi_set_geometry(struct gpmi_nand_data *this)
  1258. {
  1259. int ret;
  1260. /* Free the temporary DMA memory for reading ID. */
  1261. gpmi_free_dma_buffer(this);
  1262. /* Set up the NFC geometry which is used by BCH. */
  1263. ret = bch_set_geometry(this);
  1264. if (ret) {
  1265. pr_err("set geometry ret : %d\n", ret);
  1266. return ret;
  1267. }
  1268. /* Alloc the new DMA buffers according to the pagesize and oobsize */
  1269. return gpmi_alloc_dma_buffer(this);
  1270. }
  1271. static int gpmi_pre_bbt_scan(struct gpmi_nand_data *this)
  1272. {
  1273. int ret;
  1274. /* Set up swap_block_mark, must be set before the gpmi_set_geometry() */
  1275. if (GPMI_IS_MX23(this))
  1276. this->swap_block_mark = false;
  1277. else
  1278. this->swap_block_mark = true;
  1279. /* Set up the medium geometry */
  1280. ret = gpmi_set_geometry(this);
  1281. if (ret)
  1282. return ret;
  1283. /* Adjust the ECC strength according to the chip. */
  1284. this->nand.ecc.strength = this->bch_geometry.ecc_strength;
  1285. this->mtd.ecc_strength = this->bch_geometry.ecc_strength;
  1286. this->mtd.bitflip_threshold = this->bch_geometry.ecc_strength;
  1287. /* NAND boot init, depends on the gpmi_set_geometry(). */
  1288. return nand_boot_init(this);
  1289. }
  1290. static int gpmi_scan_bbt(struct mtd_info *mtd)
  1291. {
  1292. struct nand_chip *chip = mtd->priv;
  1293. struct gpmi_nand_data *this = chip->priv;
  1294. int ret;
  1295. /* Prepare for the BBT scan. */
  1296. ret = gpmi_pre_bbt_scan(this);
  1297. if (ret)
  1298. return ret;
  1299. /*
  1300. * Can we enable the extra features? such as EDO or Sync mode.
  1301. *
  1302. * We do not check the return value now. That's means if we fail in
  1303. * enable the extra features, we still can run in the normal way.
  1304. */
  1305. gpmi_extra_init(this);
  1306. /* use the default BBT implementation */
  1307. return nand_default_bbt(mtd);
  1308. }
  1309. static void gpmi_nfc_exit(struct gpmi_nand_data *this)
  1310. {
  1311. nand_release(&this->mtd);
  1312. gpmi_free_dma_buffer(this);
  1313. }
  1314. static int __devinit gpmi_nfc_init(struct gpmi_nand_data *this)
  1315. {
  1316. struct mtd_info *mtd = &this->mtd;
  1317. struct nand_chip *chip = &this->nand;
  1318. struct mtd_part_parser_data ppdata = {};
  1319. int ret;
  1320. /* init current chip */
  1321. this->current_chip = -1;
  1322. /* init the MTD data structures */
  1323. mtd->priv = chip;
  1324. mtd->name = "gpmi-nand";
  1325. mtd->owner = THIS_MODULE;
  1326. /* init the nand_chip{}, we don't support a 16-bit NAND Flash bus. */
  1327. chip->priv = this;
  1328. chip->select_chip = gpmi_select_chip;
  1329. chip->cmd_ctrl = gpmi_cmd_ctrl;
  1330. chip->dev_ready = gpmi_dev_ready;
  1331. chip->read_byte = gpmi_read_byte;
  1332. chip->read_buf = gpmi_read_buf;
  1333. chip->write_buf = gpmi_write_buf;
  1334. chip->ecc.read_page = gpmi_ecc_read_page;
  1335. chip->ecc.write_page = gpmi_ecc_write_page;
  1336. chip->ecc.read_oob = gpmi_ecc_read_oob;
  1337. chip->ecc.write_oob = gpmi_ecc_write_oob;
  1338. chip->scan_bbt = gpmi_scan_bbt;
  1339. chip->badblock_pattern = &gpmi_bbt_descr;
  1340. chip->block_markbad = gpmi_block_markbad;
  1341. chip->options |= NAND_NO_SUBPAGE_WRITE;
  1342. chip->ecc.mode = NAND_ECC_HW;
  1343. chip->ecc.size = 1;
  1344. chip->ecc.strength = 8;
  1345. chip->ecc.layout = &gpmi_hw_ecclayout;
  1346. if (of_get_nand_on_flash_bbt(this->dev->of_node))
  1347. chip->bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
  1348. /* Allocate a temporary DMA buffer for reading ID in the nand_scan() */
  1349. this->bch_geometry.payload_size = 1024;
  1350. this->bch_geometry.auxiliary_size = 128;
  1351. ret = gpmi_alloc_dma_buffer(this);
  1352. if (ret)
  1353. goto err_out;
  1354. ret = nand_scan(mtd, 1);
  1355. if (ret) {
  1356. pr_err("Chip scan failed\n");
  1357. goto err_out;
  1358. }
  1359. ppdata.of_node = this->pdev->dev.of_node;
  1360. ret = mtd_device_parse_register(mtd, NULL, &ppdata, NULL, 0);
  1361. if (ret)
  1362. goto err_out;
  1363. return 0;
  1364. err_out:
  1365. gpmi_nfc_exit(this);
  1366. return ret;
  1367. }
  1368. static const struct platform_device_id gpmi_ids[] = {
  1369. { .name = "imx23-gpmi-nand", .driver_data = IS_MX23, },
  1370. { .name = "imx28-gpmi-nand", .driver_data = IS_MX28, },
  1371. { .name = "imx6q-gpmi-nand", .driver_data = IS_MX6Q, },
  1372. {},
  1373. };
  1374. static const struct of_device_id gpmi_nand_id_table[] = {
  1375. {
  1376. .compatible = "fsl,imx23-gpmi-nand",
  1377. .data = (void *)&gpmi_ids[IS_MX23]
  1378. }, {
  1379. .compatible = "fsl,imx28-gpmi-nand",
  1380. .data = (void *)&gpmi_ids[IS_MX28]
  1381. }, {
  1382. .compatible = "fsl,imx6q-gpmi-nand",
  1383. .data = (void *)&gpmi_ids[IS_MX6Q]
  1384. }, {}
  1385. };
  1386. MODULE_DEVICE_TABLE(of, gpmi_nand_id_table);
  1387. static int __devinit gpmi_nand_probe(struct platform_device *pdev)
  1388. {
  1389. struct gpmi_nand_data *this;
  1390. const struct of_device_id *of_id;
  1391. int ret;
  1392. of_id = of_match_device(gpmi_nand_id_table, &pdev->dev);
  1393. if (of_id) {
  1394. pdev->id_entry = of_id->data;
  1395. } else {
  1396. pr_err("Failed to find the right device id.\n");
  1397. return -ENOMEM;
  1398. }
  1399. this = kzalloc(sizeof(*this), GFP_KERNEL);
  1400. if (!this) {
  1401. pr_err("Failed to allocate per-device memory\n");
  1402. return -ENOMEM;
  1403. }
  1404. platform_set_drvdata(pdev, this);
  1405. this->pdev = pdev;
  1406. this->dev = &pdev->dev;
  1407. ret = acquire_resources(this);
  1408. if (ret)
  1409. goto exit_acquire_resources;
  1410. ret = init_hardware(this);
  1411. if (ret)
  1412. goto exit_nfc_init;
  1413. ret = gpmi_nfc_init(this);
  1414. if (ret)
  1415. goto exit_nfc_init;
  1416. dev_info(this->dev, "driver registered.\n");
  1417. return 0;
  1418. exit_nfc_init:
  1419. release_resources(this);
  1420. exit_acquire_resources:
  1421. platform_set_drvdata(pdev, NULL);
  1422. kfree(this);
  1423. dev_err(this->dev, "driver registration failed: %d\n", ret);
  1424. return ret;
  1425. }
  1426. static int __devexit gpmi_nand_remove(struct platform_device *pdev)
  1427. {
  1428. struct gpmi_nand_data *this = platform_get_drvdata(pdev);
  1429. gpmi_nfc_exit(this);
  1430. release_resources(this);
  1431. platform_set_drvdata(pdev, NULL);
  1432. kfree(this);
  1433. return 0;
  1434. }
  1435. static struct platform_driver gpmi_nand_driver = {
  1436. .driver = {
  1437. .name = "gpmi-nand",
  1438. .of_match_table = gpmi_nand_id_table,
  1439. },
  1440. .probe = gpmi_nand_probe,
  1441. .remove = __devexit_p(gpmi_nand_remove),
  1442. .id_table = gpmi_ids,
  1443. };
  1444. module_platform_driver(gpmi_nand_driver);
  1445. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  1446. MODULE_DESCRIPTION("i.MX GPMI NAND Flash Controller Driver");
  1447. MODULE_LICENSE("GPL");