gpmi-nand.c 46 KB

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