gpmi-nand.c 46 KB

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