gpmi-nand.c 52 KB

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