gpmi-nand.c 47 KB

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