gpmi-nand.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708
  1. /*
  2. * Freescale GPMI NAND Flash Driver
  3. *
  4. * Copyright (C) 2010-2011 Freescale Semiconductor, Inc.
  5. * Copyright (C) 2008 Embedded Alley Solutions, Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. */
  21. #include <linux/clk.h>
  22. #include <linux/slab.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/module.h>
  25. #include <linux/mtd/gpmi-nand.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. /* add our owner bbt descriptor */
  33. static uint8_t scan_ff_pattern[] = { 0xff };
  34. static struct nand_bbt_descr gpmi_bbt_descr = {
  35. .options = 0,
  36. .offs = 0,
  37. .len = 1,
  38. .pattern = scan_ff_pattern
  39. };
  40. /* We will use all the (page + OOB). */
  41. static struct nand_ecclayout gpmi_hw_ecclayout = {
  42. .eccbytes = 0,
  43. .eccpos = { 0, },
  44. .oobfree = { {.offset = 0, .length = 0} }
  45. };
  46. static irqreturn_t bch_irq(int irq, void *cookie)
  47. {
  48. struct gpmi_nand_data *this = cookie;
  49. gpmi_clear_bch(this);
  50. complete(&this->bch_done);
  51. return IRQ_HANDLED;
  52. }
  53. /*
  54. * Calculate the ECC strength by hand:
  55. * E : The ECC strength.
  56. * G : the length of Galois Field.
  57. * N : The chunk count of per page.
  58. * O : the oobsize of the NAND chip.
  59. * M : the metasize of per page.
  60. *
  61. * The formula is :
  62. * E * G * N
  63. * ------------ <= (O - M)
  64. * 8
  65. *
  66. * So, we get E by:
  67. * (O - M) * 8
  68. * E <= -------------
  69. * G * N
  70. */
  71. static inline int get_ecc_strength(struct gpmi_nand_data *this)
  72. {
  73. struct bch_geometry *geo = &this->bch_geometry;
  74. struct mtd_info *mtd = &this->mtd;
  75. int ecc_strength;
  76. ecc_strength = ((mtd->oobsize - geo->metadata_size) * 8)
  77. / (geo->gf_len * geo->ecc_chunk_count);
  78. /* We need the minor even number. */
  79. return round_down(ecc_strength, 2);
  80. }
  81. int common_nfc_set_geometry(struct gpmi_nand_data *this)
  82. {
  83. struct bch_geometry *geo = &this->bch_geometry;
  84. struct mtd_info *mtd = &this->mtd;
  85. unsigned int metadata_size;
  86. unsigned int status_size;
  87. unsigned int block_mark_bit_offset;
  88. /*
  89. * The size of the metadata can be changed, though we set it to 10
  90. * bytes now. But it can't be too large, because we have to save
  91. * enough space for BCH.
  92. */
  93. geo->metadata_size = 10;
  94. /* The default for the length of Galois Field. */
  95. geo->gf_len = 13;
  96. /* The default for chunk size. There is no oobsize greater then 512. */
  97. geo->ecc_chunk_size = 512;
  98. while (geo->ecc_chunk_size < mtd->oobsize)
  99. geo->ecc_chunk_size *= 2; /* keep C >= O */
  100. geo->ecc_chunk_count = mtd->writesize / geo->ecc_chunk_size;
  101. /* We use the same ECC strength for all chunks. */
  102. geo->ecc_strength = get_ecc_strength(this);
  103. if (!geo->ecc_strength) {
  104. pr_err("We get a wrong ECC strength.\n");
  105. return -EINVAL;
  106. }
  107. geo->page_size = mtd->writesize + mtd->oobsize;
  108. geo->payload_size = mtd->writesize;
  109. /*
  110. * The auxiliary buffer contains the metadata and the ECC status. The
  111. * metadata is padded to the nearest 32-bit boundary. The ECC status
  112. * contains one byte for every ECC chunk, and is also padded to the
  113. * nearest 32-bit boundary.
  114. */
  115. metadata_size = ALIGN(geo->metadata_size, 4);
  116. status_size = ALIGN(geo->ecc_chunk_count, 4);
  117. geo->auxiliary_size = metadata_size + status_size;
  118. geo->auxiliary_status_offset = metadata_size;
  119. if (!this->swap_block_mark)
  120. return 0;
  121. /*
  122. * We need to compute the byte and bit offsets of
  123. * the physical block mark within the ECC-based view of the page.
  124. *
  125. * NAND chip with 2K page shows below:
  126. * (Block Mark)
  127. * | |
  128. * | D |
  129. * |<---->|
  130. * V V
  131. * +---+----------+-+----------+-+----------+-+----------+-+
  132. * | M | data |E| data |E| data |E| data |E|
  133. * +---+----------+-+----------+-+----------+-+----------+-+
  134. *
  135. * The position of block mark moves forward in the ECC-based view
  136. * of page, and the delta is:
  137. *
  138. * E * G * (N - 1)
  139. * D = (---------------- + M)
  140. * 8
  141. *
  142. * With the formula to compute the ECC strength, and the condition
  143. * : C >= O (C is the ecc chunk size)
  144. *
  145. * It's easy to deduce to the following result:
  146. *
  147. * E * G (O - M) C - M C - M
  148. * ----------- <= ------- <= -------- < ---------
  149. * 8 N N (N - 1)
  150. *
  151. * So, we get:
  152. *
  153. * E * G * (N - 1)
  154. * D = (---------------- + M) < C
  155. * 8
  156. *
  157. * The above inequality means the position of block mark
  158. * within the ECC-based view of the page is still in the data chunk,
  159. * and it's NOT in the ECC bits of the chunk.
  160. *
  161. * Use the following to compute the bit position of the
  162. * physical block mark within the ECC-based view of the page:
  163. * (page_size - D) * 8
  164. *
  165. * --Huang Shijie
  166. */
  167. block_mark_bit_offset = mtd->writesize * 8 -
  168. (geo->ecc_strength * geo->gf_len * (geo->ecc_chunk_count - 1)
  169. + geo->metadata_size * 8);
  170. geo->block_mark_byte_offset = block_mark_bit_offset / 8;
  171. geo->block_mark_bit_offset = block_mark_bit_offset % 8;
  172. return 0;
  173. }
  174. struct dma_chan *get_dma_chan(struct gpmi_nand_data *this)
  175. {
  176. int chipnr = this->current_chip;
  177. return this->dma_chans[chipnr];
  178. }
  179. /* Can we use the upper's buffer directly for DMA? */
  180. void prepare_data_dma(struct gpmi_nand_data *this, enum dma_data_direction dr)
  181. {
  182. struct scatterlist *sgl = &this->data_sgl;
  183. int ret;
  184. this->direct_dma_map_ok = true;
  185. /* first try to map the upper buffer directly */
  186. sg_init_one(sgl, this->upper_buf, this->upper_len);
  187. ret = dma_map_sg(this->dev, sgl, 1, dr);
  188. if (ret == 0) {
  189. /* We have to use our own DMA buffer. */
  190. sg_init_one(sgl, this->data_buffer_dma, PAGE_SIZE);
  191. if (dr == DMA_TO_DEVICE)
  192. memcpy(this->data_buffer_dma, this->upper_buf,
  193. this->upper_len);
  194. ret = dma_map_sg(this->dev, sgl, 1, dr);
  195. if (ret == 0)
  196. pr_err("map failed.\n");
  197. this->direct_dma_map_ok = false;
  198. }
  199. }
  200. /* This will be called after the DMA operation is finished. */
  201. static void dma_irq_callback(void *param)
  202. {
  203. struct gpmi_nand_data *this = param;
  204. struct completion *dma_c = &this->dma_done;
  205. complete(dma_c);
  206. switch (this->dma_type) {
  207. case DMA_FOR_COMMAND:
  208. dma_unmap_sg(this->dev, &this->cmd_sgl, 1, DMA_TO_DEVICE);
  209. break;
  210. case DMA_FOR_READ_DATA:
  211. dma_unmap_sg(this->dev, &this->data_sgl, 1, DMA_FROM_DEVICE);
  212. if (this->direct_dma_map_ok == false)
  213. memcpy(this->upper_buf, this->data_buffer_dma,
  214. this->upper_len);
  215. break;
  216. case DMA_FOR_WRITE_DATA:
  217. dma_unmap_sg(this->dev, &this->data_sgl, 1, DMA_TO_DEVICE);
  218. break;
  219. case DMA_FOR_READ_ECC_PAGE:
  220. case DMA_FOR_WRITE_ECC_PAGE:
  221. /* We have to wait the BCH interrupt to finish. */
  222. break;
  223. default:
  224. pr_err("in wrong DMA operation.\n");
  225. }
  226. }
  227. int start_dma_without_bch_irq(struct gpmi_nand_data *this,
  228. struct dma_async_tx_descriptor *desc)
  229. {
  230. struct completion *dma_c = &this->dma_done;
  231. int err;
  232. init_completion(dma_c);
  233. desc->callback = dma_irq_callback;
  234. desc->callback_param = this;
  235. dmaengine_submit(desc);
  236. dma_async_issue_pending(get_dma_chan(this));
  237. /* Wait for the interrupt from the DMA block. */
  238. err = wait_for_completion_timeout(dma_c, msecs_to_jiffies(1000));
  239. if (!err) {
  240. pr_err("DMA timeout, last DMA :%d\n", this->last_dma_type);
  241. gpmi_dump_info(this);
  242. return -ETIMEDOUT;
  243. }
  244. return 0;
  245. }
  246. /*
  247. * This function is used in BCH reading or BCH writing pages.
  248. * It will wait for the BCH interrupt as long as ONE second.
  249. * Actually, we must wait for two interrupts :
  250. * [1] firstly the DMA interrupt and
  251. * [2] secondly the BCH interrupt.
  252. */
  253. int start_dma_with_bch_irq(struct gpmi_nand_data *this,
  254. struct dma_async_tx_descriptor *desc)
  255. {
  256. struct completion *bch_c = &this->bch_done;
  257. int err;
  258. /* Prepare to receive an interrupt from the BCH block. */
  259. init_completion(bch_c);
  260. /* start the DMA */
  261. start_dma_without_bch_irq(this, desc);
  262. /* Wait for the interrupt from the BCH block. */
  263. err = wait_for_completion_timeout(bch_c, msecs_to_jiffies(1000));
  264. if (!err) {
  265. pr_err("BCH timeout, last DMA :%d\n", this->last_dma_type);
  266. gpmi_dump_info(this);
  267. return -ETIMEDOUT;
  268. }
  269. return 0;
  270. }
  271. static int __devinit
  272. acquire_register_block(struct gpmi_nand_data *this, const char *res_name)
  273. {
  274. struct platform_device *pdev = this->pdev;
  275. struct resources *res = &this->resources;
  276. struct resource *r;
  277. void *p;
  278. r = platform_get_resource_byname(pdev, IORESOURCE_MEM, res_name);
  279. if (!r) {
  280. pr_err("Can't get resource for %s\n", res_name);
  281. return -ENXIO;
  282. }
  283. p = ioremap(r->start, resource_size(r));
  284. if (!p) {
  285. pr_err("Can't remap %s\n", res_name);
  286. return -ENOMEM;
  287. }
  288. if (!strcmp(res_name, GPMI_NAND_GPMI_REGS_ADDR_RES_NAME))
  289. res->gpmi_regs = p;
  290. else if (!strcmp(res_name, GPMI_NAND_BCH_REGS_ADDR_RES_NAME))
  291. res->bch_regs = p;
  292. else
  293. pr_err("unknown resource name : %s\n", res_name);
  294. return 0;
  295. }
  296. static void release_register_block(struct gpmi_nand_data *this)
  297. {
  298. struct resources *res = &this->resources;
  299. if (res->gpmi_regs)
  300. iounmap(res->gpmi_regs);
  301. if (res->bch_regs)
  302. iounmap(res->bch_regs);
  303. res->gpmi_regs = NULL;
  304. res->bch_regs = NULL;
  305. }
  306. static int __devinit
  307. acquire_bch_irq(struct gpmi_nand_data *this, irq_handler_t irq_h)
  308. {
  309. struct platform_device *pdev = this->pdev;
  310. struct resources *res = &this->resources;
  311. const char *res_name = GPMI_NAND_BCH_INTERRUPT_RES_NAME;
  312. struct resource *r;
  313. int err;
  314. r = platform_get_resource_byname(pdev, IORESOURCE_IRQ, res_name);
  315. if (!r) {
  316. pr_err("Can't get resource for %s\n", res_name);
  317. return -ENXIO;
  318. }
  319. err = request_irq(r->start, irq_h, 0, res_name, this);
  320. if (err) {
  321. pr_err("Can't own %s\n", res_name);
  322. return err;
  323. }
  324. res->bch_low_interrupt = r->start;
  325. res->bch_high_interrupt = r->end;
  326. return 0;
  327. }
  328. static void release_bch_irq(struct gpmi_nand_data *this)
  329. {
  330. struct resources *res = &this->resources;
  331. int i = res->bch_low_interrupt;
  332. for (; i <= res->bch_high_interrupt; i++)
  333. free_irq(i, this);
  334. }
  335. static bool gpmi_dma_filter(struct dma_chan *chan, void *param)
  336. {
  337. struct gpmi_nand_data *this = param;
  338. int dma_channel = (int)this->private;
  339. if (!mxs_dma_is_apbh(chan))
  340. return false;
  341. /*
  342. * only catch the GPMI dma channels :
  343. * for mx23 : MX23_DMA_GPMI0 ~ MX23_DMA_GPMI3
  344. * (These four channels share the same IRQ!)
  345. *
  346. * for mx28 : MX28_DMA_GPMI0 ~ MX28_DMA_GPMI7
  347. * (These eight channels share the same IRQ!)
  348. */
  349. if (dma_channel == chan->chan_id) {
  350. chan->private = &this->dma_data;
  351. return true;
  352. }
  353. return false;
  354. }
  355. static void release_dma_channels(struct gpmi_nand_data *this)
  356. {
  357. unsigned int i;
  358. for (i = 0; i < DMA_CHANS; i++)
  359. if (this->dma_chans[i]) {
  360. dma_release_channel(this->dma_chans[i]);
  361. this->dma_chans[i] = NULL;
  362. }
  363. }
  364. static int __devinit acquire_dma_channels(struct gpmi_nand_data *this)
  365. {
  366. struct platform_device *pdev = this->pdev;
  367. struct resource *r_dma;
  368. struct device_node *dn;
  369. int dma_channel;
  370. unsigned int ret;
  371. struct dma_chan *dma_chan;
  372. dma_cap_mask_t mask;
  373. /* dma channel, we only use the first one. */
  374. dn = pdev->dev.of_node;
  375. ret = of_property_read_u32(dn, "fsl,gpmi-dma-channel", &dma_channel);
  376. if (ret) {
  377. pr_err("unable to get DMA channel from dt.\n");
  378. goto acquire_err;
  379. }
  380. this->private = (void *)dma_channel;
  381. /* gpmi dma interrupt */
  382. r_dma = platform_get_resource_byname(pdev, IORESOURCE_IRQ,
  383. GPMI_NAND_DMA_INTERRUPT_RES_NAME);
  384. if (!r_dma) {
  385. pr_err("Can't get resource for DMA\n");
  386. goto acquire_err;
  387. }
  388. this->dma_data.chan_irq = r_dma->start;
  389. /* request dma channel */
  390. dma_cap_zero(mask);
  391. dma_cap_set(DMA_SLAVE, mask);
  392. dma_chan = dma_request_channel(mask, gpmi_dma_filter, this);
  393. if (!dma_chan) {
  394. pr_err("dma_request_channel failed.\n");
  395. goto acquire_err;
  396. }
  397. this->dma_chans[0] = dma_chan;
  398. return 0;
  399. acquire_err:
  400. release_dma_channels(this);
  401. return -EINVAL;
  402. }
  403. static void gpmi_put_clks(struct gpmi_nand_data *this)
  404. {
  405. struct resources *r = &this->resources;
  406. struct clk *clk;
  407. int i;
  408. for (i = 0; i < GPMI_CLK_MAX; i++) {
  409. clk = r->clock[i];
  410. if (clk) {
  411. clk_put(clk);
  412. r->clock[i] = NULL;
  413. }
  414. }
  415. }
  416. static char *extra_clks_for_mx6q[GPMI_CLK_MAX] = {
  417. "gpmi_apb", "gpmi_bch", "gpmi_bch_apb", "per1_bch",
  418. };
  419. static int __devinit gpmi_get_clks(struct gpmi_nand_data *this)
  420. {
  421. struct resources *r = &this->resources;
  422. char **extra_clks = NULL;
  423. struct clk *clk;
  424. int i;
  425. /* The main clock is stored in the first. */
  426. r->clock[0] = clk_get(this->dev, "gpmi_io");
  427. if (IS_ERR(r->clock[0]))
  428. goto err_clock;
  429. /* Get extra clocks */
  430. if (GPMI_IS_MX6Q(this))
  431. extra_clks = extra_clks_for_mx6q;
  432. if (!extra_clks)
  433. return 0;
  434. for (i = 1; i < GPMI_CLK_MAX; i++) {
  435. if (extra_clks[i - 1] == NULL)
  436. break;
  437. clk = clk_get(this->dev, extra_clks[i - 1]);
  438. if (IS_ERR(clk))
  439. goto err_clock;
  440. r->clock[i] = clk;
  441. }
  442. if (GPMI_IS_MX6Q(this)) {
  443. /*
  444. * Set the default values for the clocks in mx6q:
  445. * The main clock(enfc) : 22MHz
  446. * The others : 44.5MHz
  447. *
  448. * These are just the default values. If you want to use
  449. * the ONFI nand which is in the Synchronous Mode, you should
  450. * change the clocks's frequencies as you need.
  451. */
  452. clk_set_rate(r->clock[0], 22000000);
  453. for (i = 1; i < GPMI_CLK_MAX && r->clock[i]; i++)
  454. clk_set_rate(r->clock[i], 44500000);
  455. }
  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 __devinit 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 __devinit 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("Alternate buffer is too small\n");
  540. return -ENOMEM;
  541. }
  542. goto map_failed;
  543. }
  544. *use_virt = destination;
  545. *use_phys = dest_phys;
  546. this->direct_dma_map_ok = true;
  547. return 0;
  548. }
  549. map_failed:
  550. *use_virt = alt_virt;
  551. *use_phys = alt_phys;
  552. this->direct_dma_map_ok = false;
  553. return 0;
  554. }
  555. static inline void read_page_end(struct gpmi_nand_data *this,
  556. void *destination, unsigned length,
  557. void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
  558. void *used_virt, dma_addr_t used_phys)
  559. {
  560. if (this->direct_dma_map_ok)
  561. dma_unmap_single(this->dev, used_phys, length, DMA_FROM_DEVICE);
  562. }
  563. static inline void read_page_swap_end(struct gpmi_nand_data *this,
  564. void *destination, unsigned length,
  565. void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
  566. void *used_virt, dma_addr_t used_phys)
  567. {
  568. if (!this->direct_dma_map_ok)
  569. memcpy(destination, alt_virt, length);
  570. }
  571. static int send_page_prepare(struct gpmi_nand_data *this,
  572. const void *source, unsigned length,
  573. void *alt_virt, dma_addr_t alt_phys, unsigned alt_size,
  574. const void **use_virt, dma_addr_t *use_phys)
  575. {
  576. struct device *dev = this->dev;
  577. if (virt_addr_valid(source)) {
  578. dma_addr_t source_phys;
  579. source_phys = dma_map_single(dev, (void *)source, length,
  580. DMA_TO_DEVICE);
  581. if (dma_mapping_error(dev, source_phys)) {
  582. if (alt_size < length) {
  583. pr_err("Alternate buffer is too small\n");
  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);
  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);
  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("allocate DMA buffer ret!!\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. loff_t byte;
  1091. uint8_t *buffer = chip->buffers->databuf;
  1092. int saved_chip_number;
  1093. int found_an_ncb_fingerprint = false;
  1094. /* Compute the number of strides in a search area. */
  1095. search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
  1096. saved_chip_number = this->current_chip;
  1097. chip->select_chip(mtd, 0);
  1098. /*
  1099. * Loop through the first search area, looking for the NCB fingerprint.
  1100. */
  1101. dev_dbg(dev, "Scanning for an NCB fingerprint...\n");
  1102. for (stride = 0; stride < search_area_size_in_strides; stride++) {
  1103. /* Compute the page and byte addresses. */
  1104. page = stride * rom_geo->stride_size_in_pages;
  1105. byte = page * mtd->writesize;
  1106. dev_dbg(dev, "Looking for a fingerprint in page 0x%x\n", page);
  1107. /*
  1108. * Read the NCB fingerprint. The fingerprint is four bytes long
  1109. * and starts in the 12th byte of the page.
  1110. */
  1111. chip->cmdfunc(mtd, NAND_CMD_READ0, 12, page);
  1112. chip->read_buf(mtd, buffer, strlen(fingerprint));
  1113. /* Look for the fingerprint. */
  1114. if (!memcmp(buffer, fingerprint, strlen(fingerprint))) {
  1115. found_an_ncb_fingerprint = true;
  1116. break;
  1117. }
  1118. }
  1119. chip->select_chip(mtd, saved_chip_number);
  1120. if (found_an_ncb_fingerprint)
  1121. dev_dbg(dev, "\tFound a fingerprint\n");
  1122. else
  1123. dev_dbg(dev, "\tNo fingerprint found\n");
  1124. return found_an_ncb_fingerprint;
  1125. }
  1126. /* Writes a transcription stamp. */
  1127. static int mx23_write_transcription_stamp(struct gpmi_nand_data *this)
  1128. {
  1129. struct device *dev = this->dev;
  1130. struct boot_rom_geometry *rom_geo = &this->rom_geometry;
  1131. struct mtd_info *mtd = &this->mtd;
  1132. struct nand_chip *chip = &this->nand;
  1133. unsigned int block_size_in_pages;
  1134. unsigned int search_area_size_in_strides;
  1135. unsigned int search_area_size_in_pages;
  1136. unsigned int search_area_size_in_blocks;
  1137. unsigned int block;
  1138. unsigned int stride;
  1139. unsigned int page;
  1140. loff_t byte;
  1141. uint8_t *buffer = chip->buffers->databuf;
  1142. int saved_chip_number;
  1143. int status;
  1144. /* Compute the search area geometry. */
  1145. block_size_in_pages = mtd->erasesize / mtd->writesize;
  1146. search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;
  1147. search_area_size_in_pages = search_area_size_in_strides *
  1148. rom_geo->stride_size_in_pages;
  1149. search_area_size_in_blocks =
  1150. (search_area_size_in_pages + (block_size_in_pages - 1)) /
  1151. block_size_in_pages;
  1152. dev_dbg(dev, "Search Area Geometry :\n");
  1153. dev_dbg(dev, "\tin Blocks : %u\n", search_area_size_in_blocks);
  1154. dev_dbg(dev, "\tin Strides: %u\n", search_area_size_in_strides);
  1155. dev_dbg(dev, "\tin Pages : %u\n", search_area_size_in_pages);
  1156. /* Select chip 0. */
  1157. saved_chip_number = this->current_chip;
  1158. chip->select_chip(mtd, 0);
  1159. /* Loop over blocks in the first search area, erasing them. */
  1160. dev_dbg(dev, "Erasing the search area...\n");
  1161. for (block = 0; block < search_area_size_in_blocks; block++) {
  1162. /* Compute the page address. */
  1163. page = block * block_size_in_pages;
  1164. /* Erase this block. */
  1165. dev_dbg(dev, "\tErasing block 0x%x\n", block);
  1166. chip->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
  1167. chip->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);
  1168. /* Wait for the erase to finish. */
  1169. status = chip->waitfunc(mtd, chip);
  1170. if (status & NAND_STATUS_FAIL)
  1171. dev_err(dev, "[%s] Erase failed.\n", __func__);
  1172. }
  1173. /* Write the NCB fingerprint into the page buffer. */
  1174. memset(buffer, ~0, mtd->writesize);
  1175. memset(chip->oob_poi, ~0, mtd->oobsize);
  1176. memcpy(buffer + 12, fingerprint, strlen(fingerprint));
  1177. /* Loop through the first search area, writing NCB fingerprints. */
  1178. dev_dbg(dev, "Writing NCB fingerprints...\n");
  1179. for (stride = 0; stride < search_area_size_in_strides; stride++) {
  1180. /* Compute the page and byte addresses. */
  1181. page = stride * rom_geo->stride_size_in_pages;
  1182. byte = page * mtd->writesize;
  1183. /* Write the first page of the current stride. */
  1184. dev_dbg(dev, "Writing an NCB fingerprint in page 0x%x\n", page);
  1185. chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
  1186. chip->ecc.write_page_raw(mtd, chip, buffer, 0);
  1187. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  1188. /* Wait for the write to finish. */
  1189. status = chip->waitfunc(mtd, chip);
  1190. if (status & NAND_STATUS_FAIL)
  1191. dev_err(dev, "[%s] Write failed.\n", __func__);
  1192. }
  1193. /* Deselect chip 0. */
  1194. chip->select_chip(mtd, saved_chip_number);
  1195. return 0;
  1196. }
  1197. static int mx23_boot_init(struct gpmi_nand_data *this)
  1198. {
  1199. struct device *dev = this->dev;
  1200. struct nand_chip *chip = &this->nand;
  1201. struct mtd_info *mtd = &this->mtd;
  1202. unsigned int block_count;
  1203. unsigned int block;
  1204. int chipnr;
  1205. int page;
  1206. loff_t byte;
  1207. uint8_t block_mark;
  1208. int ret = 0;
  1209. /*
  1210. * If control arrives here, we can't use block mark swapping, which
  1211. * means we're forced to use transcription. First, scan for the
  1212. * transcription stamp. If we find it, then we don't have to do
  1213. * anything -- the block marks are already transcribed.
  1214. */
  1215. if (mx23_check_transcription_stamp(this))
  1216. return 0;
  1217. /*
  1218. * If control arrives here, we couldn't find a transcription stamp, so
  1219. * so we presume the block marks are in the conventional location.
  1220. */
  1221. dev_dbg(dev, "Transcribing bad block marks...\n");
  1222. /* Compute the number of blocks in the entire medium. */
  1223. block_count = chip->chipsize >> chip->phys_erase_shift;
  1224. /*
  1225. * Loop over all the blocks in the medium, transcribing block marks as
  1226. * we go.
  1227. */
  1228. for (block = 0; block < block_count; block++) {
  1229. /*
  1230. * Compute the chip, page and byte addresses for this block's
  1231. * conventional mark.
  1232. */
  1233. chipnr = block >> (chip->chip_shift - chip->phys_erase_shift);
  1234. page = block << (chip->phys_erase_shift - chip->page_shift);
  1235. byte = block << chip->phys_erase_shift;
  1236. /* Send the command to read the conventional block mark. */
  1237. chip->select_chip(mtd, chipnr);
  1238. chip->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
  1239. block_mark = chip->read_byte(mtd);
  1240. chip->select_chip(mtd, -1);
  1241. /*
  1242. * Check if the block is marked bad. If so, we need to mark it
  1243. * again, but this time the result will be a mark in the
  1244. * location where we transcribe block marks.
  1245. */
  1246. if (block_mark != 0xff) {
  1247. dev_dbg(dev, "Transcribing mark in block %u\n", block);
  1248. ret = chip->block_markbad(mtd, byte);
  1249. if (ret)
  1250. dev_err(dev, "Failed to mark block bad with "
  1251. "ret %d\n", ret);
  1252. }
  1253. }
  1254. /* Write the stamp that indicates we've transcribed the block marks. */
  1255. mx23_write_transcription_stamp(this);
  1256. return 0;
  1257. }
  1258. static int nand_boot_init(struct gpmi_nand_data *this)
  1259. {
  1260. nand_boot_set_geometry(this);
  1261. /* This is ROM arch-specific initilization before the BBT scanning. */
  1262. if (GPMI_IS_MX23(this))
  1263. return mx23_boot_init(this);
  1264. return 0;
  1265. }
  1266. static int gpmi_set_geometry(struct gpmi_nand_data *this)
  1267. {
  1268. int ret;
  1269. /* Free the temporary DMA memory for reading ID. */
  1270. gpmi_free_dma_buffer(this);
  1271. /* Set up the NFC geometry which is used by BCH. */
  1272. ret = bch_set_geometry(this);
  1273. if (ret) {
  1274. pr_err("set geometry ret : %d\n", ret);
  1275. return ret;
  1276. }
  1277. /* Alloc the new DMA buffers according to the pagesize and oobsize */
  1278. return gpmi_alloc_dma_buffer(this);
  1279. }
  1280. static int gpmi_pre_bbt_scan(struct gpmi_nand_data *this)
  1281. {
  1282. int ret;
  1283. /* Set up swap_block_mark, must be set before the gpmi_set_geometry() */
  1284. if (GPMI_IS_MX23(this))
  1285. this->swap_block_mark = false;
  1286. else
  1287. this->swap_block_mark = true;
  1288. /* Set up the medium geometry */
  1289. ret = gpmi_set_geometry(this);
  1290. if (ret)
  1291. return ret;
  1292. /* Adjust the ECC strength according to the chip. */
  1293. this->nand.ecc.strength = this->bch_geometry.ecc_strength;
  1294. this->mtd.ecc_strength = this->bch_geometry.ecc_strength;
  1295. this->mtd.bitflip_threshold = this->bch_geometry.ecc_strength;
  1296. /* NAND boot init, depends on the gpmi_set_geometry(). */
  1297. return nand_boot_init(this);
  1298. }
  1299. static int gpmi_scan_bbt(struct mtd_info *mtd)
  1300. {
  1301. struct nand_chip *chip = mtd->priv;
  1302. struct gpmi_nand_data *this = chip->priv;
  1303. int ret;
  1304. /* Prepare for the BBT scan. */
  1305. ret = gpmi_pre_bbt_scan(this);
  1306. if (ret)
  1307. return ret;
  1308. /* use the default BBT implementation */
  1309. return nand_default_bbt(mtd);
  1310. }
  1311. void gpmi_nfc_exit(struct gpmi_nand_data *this)
  1312. {
  1313. nand_release(&this->mtd);
  1314. gpmi_free_dma_buffer(this);
  1315. }
  1316. static int __devinit gpmi_nfc_init(struct gpmi_nand_data *this)
  1317. {
  1318. struct mtd_info *mtd = &this->mtd;
  1319. struct nand_chip *chip = &this->nand;
  1320. struct mtd_part_parser_data ppdata = {};
  1321. int ret;
  1322. /* init current chip */
  1323. this->current_chip = -1;
  1324. /* init the MTD data structures */
  1325. mtd->priv = chip;
  1326. mtd->name = "gpmi-nand";
  1327. mtd->owner = THIS_MODULE;
  1328. /* init the nand_chip{}, we don't support a 16-bit NAND Flash bus. */
  1329. chip->priv = this;
  1330. chip->select_chip = gpmi_select_chip;
  1331. chip->cmd_ctrl = gpmi_cmd_ctrl;
  1332. chip->dev_ready = gpmi_dev_ready;
  1333. chip->read_byte = gpmi_read_byte;
  1334. chip->read_buf = gpmi_read_buf;
  1335. chip->write_buf = gpmi_write_buf;
  1336. chip->ecc.read_page = gpmi_ecc_read_page;
  1337. chip->ecc.write_page = gpmi_ecc_write_page;
  1338. chip->ecc.read_oob = gpmi_ecc_read_oob;
  1339. chip->ecc.write_oob = gpmi_ecc_write_oob;
  1340. chip->scan_bbt = gpmi_scan_bbt;
  1341. chip->badblock_pattern = &gpmi_bbt_descr;
  1342. chip->block_markbad = gpmi_block_markbad;
  1343. chip->options |= NAND_NO_SUBPAGE_WRITE;
  1344. chip->ecc.mode = NAND_ECC_HW;
  1345. chip->ecc.size = 1;
  1346. chip->ecc.strength = 8;
  1347. chip->ecc.layout = &gpmi_hw_ecclayout;
  1348. if (of_get_nand_on_flash_bbt(this->dev->of_node))
  1349. chip->bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
  1350. /* Allocate a temporary DMA buffer for reading ID in the nand_scan() */
  1351. this->bch_geometry.payload_size = 1024;
  1352. this->bch_geometry.auxiliary_size = 128;
  1353. ret = gpmi_alloc_dma_buffer(this);
  1354. if (ret)
  1355. goto err_out;
  1356. ret = nand_scan(mtd, 1);
  1357. if (ret) {
  1358. pr_err("Chip scan failed\n");
  1359. goto err_out;
  1360. }
  1361. ppdata.of_node = this->pdev->dev.of_node;
  1362. ret = mtd_device_parse_register(mtd, NULL, &ppdata, NULL, 0);
  1363. if (ret)
  1364. goto err_out;
  1365. return 0;
  1366. err_out:
  1367. gpmi_nfc_exit(this);
  1368. return ret;
  1369. }
  1370. static const struct platform_device_id gpmi_ids[] = {
  1371. { .name = "imx23-gpmi-nand", .driver_data = IS_MX23, },
  1372. { .name = "imx28-gpmi-nand", .driver_data = IS_MX28, },
  1373. { .name = "imx6q-gpmi-nand", .driver_data = IS_MX6Q, },
  1374. {},
  1375. };
  1376. static const struct of_device_id gpmi_nand_id_table[] = {
  1377. {
  1378. .compatible = "fsl,imx23-gpmi-nand",
  1379. .data = (void *)&gpmi_ids[IS_MX23]
  1380. }, {
  1381. .compatible = "fsl,imx28-gpmi-nand",
  1382. .data = (void *)&gpmi_ids[IS_MX28]
  1383. }, {
  1384. .compatible = "fsl,imx6q-gpmi-nand",
  1385. .data = (void *)&gpmi_ids[IS_MX6Q]
  1386. }, {}
  1387. };
  1388. MODULE_DEVICE_TABLE(of, gpmi_nand_id_table);
  1389. static int __devinit gpmi_nand_probe(struct platform_device *pdev)
  1390. {
  1391. struct gpmi_nand_data *this;
  1392. const struct of_device_id *of_id;
  1393. int ret;
  1394. of_id = of_match_device(gpmi_nand_id_table, &pdev->dev);
  1395. if (of_id) {
  1396. pdev->id_entry = of_id->data;
  1397. } else {
  1398. pr_err("Failed to find the right device id.\n");
  1399. return -ENOMEM;
  1400. }
  1401. this = kzalloc(sizeof(*this), GFP_KERNEL);
  1402. if (!this) {
  1403. pr_err("Failed to allocate per-device memory\n");
  1404. return -ENOMEM;
  1405. }
  1406. platform_set_drvdata(pdev, this);
  1407. this->pdev = pdev;
  1408. this->dev = &pdev->dev;
  1409. ret = acquire_resources(this);
  1410. if (ret)
  1411. goto exit_acquire_resources;
  1412. ret = init_hardware(this);
  1413. if (ret)
  1414. goto exit_nfc_init;
  1415. ret = gpmi_nfc_init(this);
  1416. if (ret)
  1417. goto exit_nfc_init;
  1418. return 0;
  1419. exit_nfc_init:
  1420. release_resources(this);
  1421. exit_acquire_resources:
  1422. platform_set_drvdata(pdev, NULL);
  1423. kfree(this);
  1424. return ret;
  1425. }
  1426. static int __exit gpmi_nand_remove(struct platform_device *pdev)
  1427. {
  1428. struct gpmi_nand_data *this = platform_get_drvdata(pdev);
  1429. gpmi_nfc_exit(this);
  1430. release_resources(this);
  1431. platform_set_drvdata(pdev, NULL);
  1432. kfree(this);
  1433. return 0;
  1434. }
  1435. static struct platform_driver gpmi_nand_driver = {
  1436. .driver = {
  1437. .name = "gpmi-nand",
  1438. .of_match_table = gpmi_nand_id_table,
  1439. },
  1440. .probe = gpmi_nand_probe,
  1441. .remove = __exit_p(gpmi_nand_remove),
  1442. .id_table = gpmi_ids,
  1443. };
  1444. static int __init gpmi_nand_init(void)
  1445. {
  1446. int err;
  1447. err = platform_driver_register(&gpmi_nand_driver);
  1448. if (err == 0)
  1449. printk(KERN_INFO "GPMI NAND driver registered. (IMX)\n");
  1450. else
  1451. pr_err("i.MX GPMI NAND driver registration failed\n");
  1452. return err;
  1453. }
  1454. static void __exit gpmi_nand_exit(void)
  1455. {
  1456. platform_driver_unregister(&gpmi_nand_driver);
  1457. }
  1458. module_init(gpmi_nand_init);
  1459. module_exit(gpmi_nand_exit);
  1460. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  1461. MODULE_DESCRIPTION("i.MX GPMI NAND Flash Controller Driver");
  1462. MODULE_LICENSE("GPL");