gpmi-nand.c 47 KB

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