gpmi-nand.c 47 KB

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