gpmi-nand.c 46 KB

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