pxa3xx_nand.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888
  1. /*
  2. * drivers/mtd/nand/pxa3xx_nand.c
  3. *
  4. * Copyright © 2005 Intel Corporation
  5. * Copyright © 2006 Marvell International Ltd.
  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 version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * See Documentation/mtd/nand/pxa3xx-nand.txt for more details.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/dma-mapping.h>
  18. #include <linux/delay.h>
  19. #include <linux/clk.h>
  20. #include <linux/mtd/mtd.h>
  21. #include <linux/mtd/nand.h>
  22. #include <linux/mtd/partitions.h>
  23. #include <linux/io.h>
  24. #include <linux/irq.h>
  25. #include <linux/slab.h>
  26. #include <linux/of.h>
  27. #include <linux/of_device.h>
  28. #include <linux/of_mtd.h>
  29. #if defined(CONFIG_ARCH_PXA) || defined(CONFIG_ARCH_MMP)
  30. #define ARCH_HAS_DMA
  31. #endif
  32. #ifdef ARCH_HAS_DMA
  33. #include <mach/dma.h>
  34. #endif
  35. #include <linux/platform_data/mtd-nand-pxa3xx.h>
  36. #define CHIP_DELAY_TIMEOUT (2 * HZ/10)
  37. #define NAND_STOP_DELAY (2 * HZ/50)
  38. #define PAGE_CHUNK_SIZE (2048)
  39. /*
  40. * Define a buffer size for the initial command that detects the flash device:
  41. * STATUS, READID and PARAM. The largest of these is the PARAM command,
  42. * needing 256 bytes.
  43. */
  44. #define INIT_BUFFER_SIZE 256
  45. /* registers and bit definitions */
  46. #define NDCR (0x00) /* Control register */
  47. #define NDTR0CS0 (0x04) /* Timing Parameter 0 for CS0 */
  48. #define NDTR1CS0 (0x0C) /* Timing Parameter 1 for CS0 */
  49. #define NDSR (0x14) /* Status Register */
  50. #define NDPCR (0x18) /* Page Count Register */
  51. #define NDBDR0 (0x1C) /* Bad Block Register 0 */
  52. #define NDBDR1 (0x20) /* Bad Block Register 1 */
  53. #define NDECCCTRL (0x28) /* ECC control */
  54. #define NDDB (0x40) /* Data Buffer */
  55. #define NDCB0 (0x48) /* Command Buffer0 */
  56. #define NDCB1 (0x4C) /* Command Buffer1 */
  57. #define NDCB2 (0x50) /* Command Buffer2 */
  58. #define NDCR_SPARE_EN (0x1 << 31)
  59. #define NDCR_ECC_EN (0x1 << 30)
  60. #define NDCR_DMA_EN (0x1 << 29)
  61. #define NDCR_ND_RUN (0x1 << 28)
  62. #define NDCR_DWIDTH_C (0x1 << 27)
  63. #define NDCR_DWIDTH_M (0x1 << 26)
  64. #define NDCR_PAGE_SZ (0x1 << 24)
  65. #define NDCR_NCSX (0x1 << 23)
  66. #define NDCR_ND_MODE (0x3 << 21)
  67. #define NDCR_NAND_MODE (0x0)
  68. #define NDCR_CLR_PG_CNT (0x1 << 20)
  69. #define NDCR_STOP_ON_UNCOR (0x1 << 19)
  70. #define NDCR_RD_ID_CNT_MASK (0x7 << 16)
  71. #define NDCR_RD_ID_CNT(x) (((x) << 16) & NDCR_RD_ID_CNT_MASK)
  72. #define NDCR_RA_START (0x1 << 15)
  73. #define NDCR_PG_PER_BLK (0x1 << 14)
  74. #define NDCR_ND_ARB_EN (0x1 << 12)
  75. #define NDCR_INT_MASK (0xFFF)
  76. #define NDSR_MASK (0xfff)
  77. #define NDSR_ERR_CNT_OFF (16)
  78. #define NDSR_ERR_CNT_MASK (0x1f)
  79. #define NDSR_ERR_CNT(sr) ((sr >> NDSR_ERR_CNT_OFF) & NDSR_ERR_CNT_MASK)
  80. #define NDSR_RDY (0x1 << 12)
  81. #define NDSR_FLASH_RDY (0x1 << 11)
  82. #define NDSR_CS0_PAGED (0x1 << 10)
  83. #define NDSR_CS1_PAGED (0x1 << 9)
  84. #define NDSR_CS0_CMDD (0x1 << 8)
  85. #define NDSR_CS1_CMDD (0x1 << 7)
  86. #define NDSR_CS0_BBD (0x1 << 6)
  87. #define NDSR_CS1_BBD (0x1 << 5)
  88. #define NDSR_UNCORERR (0x1 << 4)
  89. #define NDSR_CORERR (0x1 << 3)
  90. #define NDSR_WRDREQ (0x1 << 2)
  91. #define NDSR_RDDREQ (0x1 << 1)
  92. #define NDSR_WRCMDREQ (0x1)
  93. #define NDCB0_LEN_OVRD (0x1 << 28)
  94. #define NDCB0_ST_ROW_EN (0x1 << 26)
  95. #define NDCB0_AUTO_RS (0x1 << 25)
  96. #define NDCB0_CSEL (0x1 << 24)
  97. #define NDCB0_EXT_CMD_TYPE_MASK (0x7 << 29)
  98. #define NDCB0_EXT_CMD_TYPE(x) (((x) << 29) & NDCB0_EXT_CMD_TYPE_MASK)
  99. #define NDCB0_CMD_TYPE_MASK (0x7 << 21)
  100. #define NDCB0_CMD_TYPE(x) (((x) << 21) & NDCB0_CMD_TYPE_MASK)
  101. #define NDCB0_NC (0x1 << 20)
  102. #define NDCB0_DBC (0x1 << 19)
  103. #define NDCB0_ADDR_CYC_MASK (0x7 << 16)
  104. #define NDCB0_ADDR_CYC(x) (((x) << 16) & NDCB0_ADDR_CYC_MASK)
  105. #define NDCB0_CMD2_MASK (0xff << 8)
  106. #define NDCB0_CMD1_MASK (0xff)
  107. #define NDCB0_ADDR_CYC_SHIFT (16)
  108. #define EXT_CMD_TYPE_DISPATCH 6 /* Command dispatch */
  109. #define EXT_CMD_TYPE_NAKED_RW 5 /* Naked read or Naked write */
  110. #define EXT_CMD_TYPE_READ 4 /* Read */
  111. #define EXT_CMD_TYPE_DISP_WR 4 /* Command dispatch with write */
  112. #define EXT_CMD_TYPE_FINAL 3 /* Final command */
  113. #define EXT_CMD_TYPE_LAST_RW 1 /* Last naked read/write */
  114. #define EXT_CMD_TYPE_MONO 0 /* Monolithic read/write */
  115. /* macros for registers read/write */
  116. #define nand_writel(info, off, val) \
  117. writel_relaxed((val), (info)->mmio_base + (off))
  118. #define nand_readl(info, off) \
  119. readl_relaxed((info)->mmio_base + (off))
  120. /* error code and state */
  121. enum {
  122. ERR_NONE = 0,
  123. ERR_DMABUSERR = -1,
  124. ERR_SENDCMD = -2,
  125. ERR_UNCORERR = -3,
  126. ERR_BBERR = -4,
  127. ERR_CORERR = -5,
  128. };
  129. enum {
  130. STATE_IDLE = 0,
  131. STATE_PREPARED,
  132. STATE_CMD_HANDLE,
  133. STATE_DMA_READING,
  134. STATE_DMA_WRITING,
  135. STATE_DMA_DONE,
  136. STATE_PIO_READING,
  137. STATE_PIO_WRITING,
  138. STATE_CMD_DONE,
  139. STATE_READY,
  140. };
  141. enum pxa3xx_nand_variant {
  142. PXA3XX_NAND_VARIANT_PXA,
  143. PXA3XX_NAND_VARIANT_ARMADA370,
  144. };
  145. struct pxa3xx_nand_host {
  146. struct nand_chip chip;
  147. struct mtd_info *mtd;
  148. void *info_data;
  149. /* page size of attached chip */
  150. int use_ecc;
  151. int cs;
  152. /* calculated from pxa3xx_nand_flash data */
  153. unsigned int col_addr_cycles;
  154. unsigned int row_addr_cycles;
  155. size_t read_id_bytes;
  156. };
  157. struct pxa3xx_nand_info {
  158. struct nand_hw_control controller;
  159. struct platform_device *pdev;
  160. struct clk *clk;
  161. void __iomem *mmio_base;
  162. unsigned long mmio_phys;
  163. struct completion cmd_complete, dev_ready;
  164. unsigned int buf_start;
  165. unsigned int buf_count;
  166. unsigned int buf_size;
  167. unsigned int data_buff_pos;
  168. unsigned int oob_buff_pos;
  169. /* DMA information */
  170. int drcmr_dat;
  171. int drcmr_cmd;
  172. unsigned char *data_buff;
  173. unsigned char *oob_buff;
  174. dma_addr_t data_buff_phys;
  175. int data_dma_ch;
  176. struct pxa_dma_desc *data_desc;
  177. dma_addr_t data_desc_addr;
  178. struct pxa3xx_nand_host *host[NUM_CHIP_SELECT];
  179. unsigned int state;
  180. /*
  181. * This driver supports NFCv1 (as found in PXA SoC)
  182. * and NFCv2 (as found in Armada 370/XP SoC).
  183. */
  184. enum pxa3xx_nand_variant variant;
  185. int cs;
  186. int use_ecc; /* use HW ECC ? */
  187. int ecc_bch; /* using BCH ECC? */
  188. int use_dma; /* use DMA ? */
  189. int use_spare; /* use spare ? */
  190. int need_wait;
  191. unsigned int data_size; /* data to be read from FIFO */
  192. unsigned int chunk_size; /* split commands chunk size */
  193. unsigned int oob_size;
  194. unsigned int spare_size;
  195. unsigned int ecc_size;
  196. unsigned int ecc_err_cnt;
  197. unsigned int max_bitflips;
  198. int retcode;
  199. /* cached register value */
  200. uint32_t reg_ndcr;
  201. uint32_t ndtr0cs0;
  202. uint32_t ndtr1cs0;
  203. /* generated NDCBx register values */
  204. uint32_t ndcb0;
  205. uint32_t ndcb1;
  206. uint32_t ndcb2;
  207. uint32_t ndcb3;
  208. };
  209. static bool use_dma = 1;
  210. module_param(use_dma, bool, 0444);
  211. MODULE_PARM_DESC(use_dma, "enable DMA for data transferring to/from NAND HW");
  212. static struct pxa3xx_nand_timing timing[] = {
  213. { 40, 80, 60, 100, 80, 100, 90000, 400, 40, },
  214. { 10, 0, 20, 40, 30, 40, 11123, 110, 10, },
  215. { 10, 25, 15, 25, 15, 30, 25000, 60, 10, },
  216. { 10, 35, 15, 25, 15, 25, 25000, 60, 10, },
  217. };
  218. static struct pxa3xx_nand_flash builtin_flash_types[] = {
  219. { "DEFAULT FLASH", 0, 0, 2048, 8, 8, 0, &timing[0] },
  220. { "64MiB 16-bit", 0x46ec, 32, 512, 16, 16, 4096, &timing[1] },
  221. { "256MiB 8-bit", 0xdaec, 64, 2048, 8, 8, 2048, &timing[1] },
  222. { "4GiB 8-bit", 0xd7ec, 128, 4096, 8, 8, 8192, &timing[1] },
  223. { "128MiB 8-bit", 0xa12c, 64, 2048, 8, 8, 1024, &timing[2] },
  224. { "128MiB 16-bit", 0xb12c, 64, 2048, 16, 16, 1024, &timing[2] },
  225. { "512MiB 8-bit", 0xdc2c, 64, 2048, 8, 8, 4096, &timing[2] },
  226. { "512MiB 16-bit", 0xcc2c, 64, 2048, 16, 16, 4096, &timing[2] },
  227. { "256MiB 16-bit", 0xba20, 64, 2048, 16, 16, 2048, &timing[3] },
  228. };
  229. static u8 bbt_pattern[] = {'M', 'V', 'B', 'b', 't', '0' };
  230. static u8 bbt_mirror_pattern[] = {'1', 't', 'b', 'B', 'V', 'M' };
  231. static struct nand_bbt_descr bbt_main_descr = {
  232. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  233. | NAND_BBT_2BIT | NAND_BBT_VERSION,
  234. .offs = 8,
  235. .len = 6,
  236. .veroffs = 14,
  237. .maxblocks = 8, /* Last 8 blocks in each chip */
  238. .pattern = bbt_pattern
  239. };
  240. static struct nand_bbt_descr bbt_mirror_descr = {
  241. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  242. | NAND_BBT_2BIT | NAND_BBT_VERSION,
  243. .offs = 8,
  244. .len = 6,
  245. .veroffs = 14,
  246. .maxblocks = 8, /* Last 8 blocks in each chip */
  247. .pattern = bbt_mirror_pattern
  248. };
  249. static struct nand_ecclayout ecc_layout_2KB_bch4bit = {
  250. .eccbytes = 32,
  251. .eccpos = {
  252. 32, 33, 34, 35, 36, 37, 38, 39,
  253. 40, 41, 42, 43, 44, 45, 46, 47,
  254. 48, 49, 50, 51, 52, 53, 54, 55,
  255. 56, 57, 58, 59, 60, 61, 62, 63},
  256. .oobfree = { {2, 30} }
  257. };
  258. static struct nand_ecclayout ecc_layout_4KB_bch4bit = {
  259. .eccbytes = 64,
  260. .eccpos = {
  261. 32, 33, 34, 35, 36, 37, 38, 39,
  262. 40, 41, 42, 43, 44, 45, 46, 47,
  263. 48, 49, 50, 51, 52, 53, 54, 55,
  264. 56, 57, 58, 59, 60, 61, 62, 63,
  265. 96, 97, 98, 99, 100, 101, 102, 103,
  266. 104, 105, 106, 107, 108, 109, 110, 111,
  267. 112, 113, 114, 115, 116, 117, 118, 119,
  268. 120, 121, 122, 123, 124, 125, 126, 127},
  269. /* Bootrom looks in bytes 0 & 5 for bad blocks */
  270. .oobfree = { {6, 26}, { 64, 32} }
  271. };
  272. static struct nand_ecclayout ecc_layout_4KB_bch8bit = {
  273. .eccbytes = 128,
  274. .eccpos = {
  275. 32, 33, 34, 35, 36, 37, 38, 39,
  276. 40, 41, 42, 43, 44, 45, 46, 47,
  277. 48, 49, 50, 51, 52, 53, 54, 55,
  278. 56, 57, 58, 59, 60, 61, 62, 63},
  279. .oobfree = { }
  280. };
  281. /* Define a default flash type setting serve as flash detecting only */
  282. #define DEFAULT_FLASH_TYPE (&builtin_flash_types[0])
  283. #define NDTR0_tCH(c) (min((c), 7) << 19)
  284. #define NDTR0_tCS(c) (min((c), 7) << 16)
  285. #define NDTR0_tWH(c) (min((c), 7) << 11)
  286. #define NDTR0_tWP(c) (min((c), 7) << 8)
  287. #define NDTR0_tRH(c) (min((c), 7) << 3)
  288. #define NDTR0_tRP(c) (min((c), 7) << 0)
  289. #define NDTR1_tR(c) (min((c), 65535) << 16)
  290. #define NDTR1_tWHR(c) (min((c), 15) << 4)
  291. #define NDTR1_tAR(c) (min((c), 15) << 0)
  292. /* convert nano-seconds to nand flash controller clock cycles */
  293. #define ns2cycle(ns, clk) (int)((ns) * (clk / 1000000) / 1000)
  294. static const struct of_device_id pxa3xx_nand_dt_ids[] = {
  295. {
  296. .compatible = "marvell,pxa3xx-nand",
  297. .data = (void *)PXA3XX_NAND_VARIANT_PXA,
  298. },
  299. {
  300. .compatible = "marvell,armada370-nand",
  301. .data = (void *)PXA3XX_NAND_VARIANT_ARMADA370,
  302. },
  303. {}
  304. };
  305. MODULE_DEVICE_TABLE(of, pxa3xx_nand_dt_ids);
  306. static enum pxa3xx_nand_variant
  307. pxa3xx_nand_get_variant(struct platform_device *pdev)
  308. {
  309. const struct of_device_id *of_id =
  310. of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
  311. if (!of_id)
  312. return PXA3XX_NAND_VARIANT_PXA;
  313. return (enum pxa3xx_nand_variant)of_id->data;
  314. }
  315. static void pxa3xx_nand_set_timing(struct pxa3xx_nand_host *host,
  316. const struct pxa3xx_nand_timing *t)
  317. {
  318. struct pxa3xx_nand_info *info = host->info_data;
  319. unsigned long nand_clk = clk_get_rate(info->clk);
  320. uint32_t ndtr0, ndtr1;
  321. ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) |
  322. NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) |
  323. NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) |
  324. NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) |
  325. NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) |
  326. NDTR0_tRP(ns2cycle(t->tRP, nand_clk));
  327. ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) |
  328. NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) |
  329. NDTR1_tAR(ns2cycle(t->tAR, nand_clk));
  330. info->ndtr0cs0 = ndtr0;
  331. info->ndtr1cs0 = ndtr1;
  332. nand_writel(info, NDTR0CS0, ndtr0);
  333. nand_writel(info, NDTR1CS0, ndtr1);
  334. }
  335. /*
  336. * Set the data and OOB size, depending on the selected
  337. * spare and ECC configuration.
  338. * Only applicable to READ0, READOOB and PAGEPROG commands.
  339. */
  340. static void pxa3xx_set_datasize(struct pxa3xx_nand_info *info,
  341. struct mtd_info *mtd)
  342. {
  343. int oob_enable = info->reg_ndcr & NDCR_SPARE_EN;
  344. info->data_size = mtd->writesize;
  345. if (!oob_enable)
  346. return;
  347. info->oob_size = info->spare_size;
  348. if (!info->use_ecc)
  349. info->oob_size += info->ecc_size;
  350. }
  351. /**
  352. * NOTE: it is a must to set ND_RUN firstly, then write
  353. * command buffer, otherwise, it does not work.
  354. * We enable all the interrupt at the same time, and
  355. * let pxa3xx_nand_irq to handle all logic.
  356. */
  357. static void pxa3xx_nand_start(struct pxa3xx_nand_info *info)
  358. {
  359. uint32_t ndcr;
  360. ndcr = info->reg_ndcr;
  361. if (info->use_ecc) {
  362. ndcr |= NDCR_ECC_EN;
  363. if (info->ecc_bch)
  364. nand_writel(info, NDECCCTRL, 0x1);
  365. } else {
  366. ndcr &= ~NDCR_ECC_EN;
  367. if (info->ecc_bch)
  368. nand_writel(info, NDECCCTRL, 0x0);
  369. }
  370. if (info->use_dma)
  371. ndcr |= NDCR_DMA_EN;
  372. else
  373. ndcr &= ~NDCR_DMA_EN;
  374. if (info->use_spare)
  375. ndcr |= NDCR_SPARE_EN;
  376. else
  377. ndcr &= ~NDCR_SPARE_EN;
  378. ndcr |= NDCR_ND_RUN;
  379. /* clear status bits and run */
  380. nand_writel(info, NDCR, 0);
  381. nand_writel(info, NDSR, NDSR_MASK);
  382. nand_writel(info, NDCR, ndcr);
  383. }
  384. static void pxa3xx_nand_stop(struct pxa3xx_nand_info *info)
  385. {
  386. uint32_t ndcr;
  387. int timeout = NAND_STOP_DELAY;
  388. /* wait RUN bit in NDCR become 0 */
  389. ndcr = nand_readl(info, NDCR);
  390. while ((ndcr & NDCR_ND_RUN) && (timeout-- > 0)) {
  391. ndcr = nand_readl(info, NDCR);
  392. udelay(1);
  393. }
  394. if (timeout <= 0) {
  395. ndcr &= ~NDCR_ND_RUN;
  396. nand_writel(info, NDCR, ndcr);
  397. }
  398. /* clear status bits */
  399. nand_writel(info, NDSR, NDSR_MASK);
  400. }
  401. static void __maybe_unused
  402. enable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
  403. {
  404. uint32_t ndcr;
  405. ndcr = nand_readl(info, NDCR);
  406. nand_writel(info, NDCR, ndcr & ~int_mask);
  407. }
  408. static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
  409. {
  410. uint32_t ndcr;
  411. ndcr = nand_readl(info, NDCR);
  412. nand_writel(info, NDCR, ndcr | int_mask);
  413. }
  414. static void handle_data_pio(struct pxa3xx_nand_info *info)
  415. {
  416. unsigned int do_bytes = min(info->data_size, info->chunk_size);
  417. switch (info->state) {
  418. case STATE_PIO_WRITING:
  419. __raw_writesl(info->mmio_base + NDDB,
  420. info->data_buff + info->data_buff_pos,
  421. DIV_ROUND_UP(do_bytes, 4));
  422. if (info->oob_size > 0)
  423. __raw_writesl(info->mmio_base + NDDB,
  424. info->oob_buff + info->oob_buff_pos,
  425. DIV_ROUND_UP(info->oob_size, 4));
  426. break;
  427. case STATE_PIO_READING:
  428. __raw_readsl(info->mmio_base + NDDB,
  429. info->data_buff + info->data_buff_pos,
  430. DIV_ROUND_UP(do_bytes, 4));
  431. if (info->oob_size > 0)
  432. __raw_readsl(info->mmio_base + NDDB,
  433. info->oob_buff + info->oob_buff_pos,
  434. DIV_ROUND_UP(info->oob_size, 4));
  435. break;
  436. default:
  437. dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
  438. info->state);
  439. BUG();
  440. }
  441. /* Update buffer pointers for multi-page read/write */
  442. info->data_buff_pos += do_bytes;
  443. info->oob_buff_pos += info->oob_size;
  444. info->data_size -= do_bytes;
  445. }
  446. #ifdef ARCH_HAS_DMA
  447. static void start_data_dma(struct pxa3xx_nand_info *info)
  448. {
  449. struct pxa_dma_desc *desc = info->data_desc;
  450. int dma_len = ALIGN(info->data_size + info->oob_size, 32);
  451. desc->ddadr = DDADR_STOP;
  452. desc->dcmd = DCMD_ENDIRQEN | DCMD_WIDTH4 | DCMD_BURST32 | dma_len;
  453. switch (info->state) {
  454. case STATE_DMA_WRITING:
  455. desc->dsadr = info->data_buff_phys;
  456. desc->dtadr = info->mmio_phys + NDDB;
  457. desc->dcmd |= DCMD_INCSRCADDR | DCMD_FLOWTRG;
  458. break;
  459. case STATE_DMA_READING:
  460. desc->dtadr = info->data_buff_phys;
  461. desc->dsadr = info->mmio_phys + NDDB;
  462. desc->dcmd |= DCMD_INCTRGADDR | DCMD_FLOWSRC;
  463. break;
  464. default:
  465. dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
  466. info->state);
  467. BUG();
  468. }
  469. DRCMR(info->drcmr_dat) = DRCMR_MAPVLD | info->data_dma_ch;
  470. DDADR(info->data_dma_ch) = info->data_desc_addr;
  471. DCSR(info->data_dma_ch) |= DCSR_RUN;
  472. }
  473. static void pxa3xx_nand_data_dma_irq(int channel, void *data)
  474. {
  475. struct pxa3xx_nand_info *info = data;
  476. uint32_t dcsr;
  477. dcsr = DCSR(channel);
  478. DCSR(channel) = dcsr;
  479. if (dcsr & DCSR_BUSERR) {
  480. info->retcode = ERR_DMABUSERR;
  481. }
  482. info->state = STATE_DMA_DONE;
  483. enable_int(info, NDCR_INT_MASK);
  484. nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
  485. }
  486. #else
  487. static void start_data_dma(struct pxa3xx_nand_info *info)
  488. {}
  489. #endif
  490. static irqreturn_t pxa3xx_nand_irq(int irq, void *devid)
  491. {
  492. struct pxa3xx_nand_info *info = devid;
  493. unsigned int status, is_completed = 0, is_ready = 0;
  494. unsigned int ready, cmd_done;
  495. if (info->cs == 0) {
  496. ready = NDSR_FLASH_RDY;
  497. cmd_done = NDSR_CS0_CMDD;
  498. } else {
  499. ready = NDSR_RDY;
  500. cmd_done = NDSR_CS1_CMDD;
  501. }
  502. status = nand_readl(info, NDSR);
  503. if (status & NDSR_UNCORERR)
  504. info->retcode = ERR_UNCORERR;
  505. if (status & NDSR_CORERR) {
  506. info->retcode = ERR_CORERR;
  507. if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370 &&
  508. info->ecc_bch)
  509. info->ecc_err_cnt = NDSR_ERR_CNT(status);
  510. else
  511. info->ecc_err_cnt = 1;
  512. /*
  513. * Each chunk composing a page is corrected independently,
  514. * and we need to store maximum number of corrected bitflips
  515. * to return it to the MTD layer in ecc.read_page().
  516. */
  517. info->max_bitflips = max_t(unsigned int,
  518. info->max_bitflips,
  519. info->ecc_err_cnt);
  520. }
  521. if (status & (NDSR_RDDREQ | NDSR_WRDREQ)) {
  522. /* whether use dma to transfer data */
  523. if (info->use_dma) {
  524. disable_int(info, NDCR_INT_MASK);
  525. info->state = (status & NDSR_RDDREQ) ?
  526. STATE_DMA_READING : STATE_DMA_WRITING;
  527. start_data_dma(info);
  528. goto NORMAL_IRQ_EXIT;
  529. } else {
  530. info->state = (status & NDSR_RDDREQ) ?
  531. STATE_PIO_READING : STATE_PIO_WRITING;
  532. handle_data_pio(info);
  533. }
  534. }
  535. if (status & cmd_done) {
  536. info->state = STATE_CMD_DONE;
  537. is_completed = 1;
  538. }
  539. if (status & ready) {
  540. info->state = STATE_READY;
  541. is_ready = 1;
  542. }
  543. if (status & NDSR_WRCMDREQ) {
  544. nand_writel(info, NDSR, NDSR_WRCMDREQ);
  545. status &= ~NDSR_WRCMDREQ;
  546. info->state = STATE_CMD_HANDLE;
  547. /*
  548. * Command buffer registers NDCB{0-2} (and optionally NDCB3)
  549. * must be loaded by writing directly either 12 or 16
  550. * bytes directly to NDCB0, four bytes at a time.
  551. *
  552. * Direct write access to NDCB1, NDCB2 and NDCB3 is ignored
  553. * but each NDCBx register can be read.
  554. */
  555. nand_writel(info, NDCB0, info->ndcb0);
  556. nand_writel(info, NDCB0, info->ndcb1);
  557. nand_writel(info, NDCB0, info->ndcb2);
  558. /* NDCB3 register is available in NFCv2 (Armada 370/XP SoC) */
  559. if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
  560. nand_writel(info, NDCB0, info->ndcb3);
  561. }
  562. /* clear NDSR to let the controller exit the IRQ */
  563. nand_writel(info, NDSR, status);
  564. if (is_completed)
  565. complete(&info->cmd_complete);
  566. if (is_ready)
  567. complete(&info->dev_ready);
  568. NORMAL_IRQ_EXIT:
  569. return IRQ_HANDLED;
  570. }
  571. static inline int is_buf_blank(uint8_t *buf, size_t len)
  572. {
  573. for (; len > 0; len--)
  574. if (*buf++ != 0xff)
  575. return 0;
  576. return 1;
  577. }
  578. static void set_command_address(struct pxa3xx_nand_info *info,
  579. unsigned int page_size, uint16_t column, int page_addr)
  580. {
  581. /* small page addr setting */
  582. if (page_size < PAGE_CHUNK_SIZE) {
  583. info->ndcb1 = ((page_addr & 0xFFFFFF) << 8)
  584. | (column & 0xFF);
  585. info->ndcb2 = 0;
  586. } else {
  587. info->ndcb1 = ((page_addr & 0xFFFF) << 16)
  588. | (column & 0xFFFF);
  589. if (page_addr & 0xFF0000)
  590. info->ndcb2 = (page_addr & 0xFF0000) >> 16;
  591. else
  592. info->ndcb2 = 0;
  593. }
  594. }
  595. static void prepare_start_command(struct pxa3xx_nand_info *info, int command)
  596. {
  597. struct pxa3xx_nand_host *host = info->host[info->cs];
  598. struct mtd_info *mtd = host->mtd;
  599. /* reset data and oob column point to handle data */
  600. info->buf_start = 0;
  601. info->buf_count = 0;
  602. info->oob_size = 0;
  603. info->data_buff_pos = 0;
  604. info->oob_buff_pos = 0;
  605. info->use_ecc = 0;
  606. info->use_spare = 1;
  607. info->retcode = ERR_NONE;
  608. info->ecc_err_cnt = 0;
  609. info->ndcb3 = 0;
  610. info->need_wait = 0;
  611. switch (command) {
  612. case NAND_CMD_READ0:
  613. case NAND_CMD_PAGEPROG:
  614. info->use_ecc = 1;
  615. case NAND_CMD_READOOB:
  616. pxa3xx_set_datasize(info, mtd);
  617. break;
  618. case NAND_CMD_PARAM:
  619. info->use_spare = 0;
  620. break;
  621. default:
  622. info->ndcb1 = 0;
  623. info->ndcb2 = 0;
  624. break;
  625. }
  626. /*
  627. * If we are about to issue a read command, or about to set
  628. * the write address, then clean the data buffer.
  629. */
  630. if (command == NAND_CMD_READ0 ||
  631. command == NAND_CMD_READOOB ||
  632. command == NAND_CMD_SEQIN) {
  633. info->buf_count = mtd->writesize + mtd->oobsize;
  634. memset(info->data_buff, 0xFF, info->buf_count);
  635. }
  636. }
  637. static int prepare_set_command(struct pxa3xx_nand_info *info, int command,
  638. int ext_cmd_type, uint16_t column, int page_addr)
  639. {
  640. int addr_cycle, exec_cmd;
  641. struct pxa3xx_nand_host *host;
  642. struct mtd_info *mtd;
  643. host = info->host[info->cs];
  644. mtd = host->mtd;
  645. addr_cycle = 0;
  646. exec_cmd = 1;
  647. if (info->cs != 0)
  648. info->ndcb0 = NDCB0_CSEL;
  649. else
  650. info->ndcb0 = 0;
  651. if (command == NAND_CMD_SEQIN)
  652. exec_cmd = 0;
  653. addr_cycle = NDCB0_ADDR_CYC(host->row_addr_cycles
  654. + host->col_addr_cycles);
  655. switch (command) {
  656. case NAND_CMD_READOOB:
  657. case NAND_CMD_READ0:
  658. info->buf_start = column;
  659. info->ndcb0 |= NDCB0_CMD_TYPE(0)
  660. | addr_cycle
  661. | NAND_CMD_READ0;
  662. if (command == NAND_CMD_READOOB)
  663. info->buf_start += mtd->writesize;
  664. /*
  665. * Multiple page read needs an 'extended command type' field,
  666. * which is either naked-read or last-read according to the
  667. * state.
  668. */
  669. if (mtd->writesize == PAGE_CHUNK_SIZE) {
  670. info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8);
  671. } else if (mtd->writesize > PAGE_CHUNK_SIZE) {
  672. info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8)
  673. | NDCB0_LEN_OVRD
  674. | NDCB0_EXT_CMD_TYPE(ext_cmd_type);
  675. info->ndcb3 = info->chunk_size +
  676. info->oob_size;
  677. }
  678. set_command_address(info, mtd->writesize, column, page_addr);
  679. break;
  680. case NAND_CMD_SEQIN:
  681. info->buf_start = column;
  682. set_command_address(info, mtd->writesize, 0, page_addr);
  683. /*
  684. * Multiple page programming needs to execute the initial
  685. * SEQIN command that sets the page address.
  686. */
  687. if (mtd->writesize > PAGE_CHUNK_SIZE) {
  688. info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
  689. | NDCB0_EXT_CMD_TYPE(ext_cmd_type)
  690. | addr_cycle
  691. | command;
  692. /* No data transfer in this case */
  693. info->data_size = 0;
  694. exec_cmd = 1;
  695. }
  696. break;
  697. case NAND_CMD_PAGEPROG:
  698. if (is_buf_blank(info->data_buff,
  699. (mtd->writesize + mtd->oobsize))) {
  700. exec_cmd = 0;
  701. break;
  702. }
  703. /* Second command setting for large pages */
  704. if (mtd->writesize > PAGE_CHUNK_SIZE) {
  705. /*
  706. * Multiple page write uses the 'extended command'
  707. * field. This can be used to issue a command dispatch
  708. * or a naked-write depending on the current stage.
  709. */
  710. info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
  711. | NDCB0_LEN_OVRD
  712. | NDCB0_EXT_CMD_TYPE(ext_cmd_type);
  713. info->ndcb3 = info->chunk_size +
  714. info->oob_size;
  715. /*
  716. * This is the command dispatch that completes a chunked
  717. * page program operation.
  718. */
  719. if (info->data_size == 0) {
  720. info->ndcb0 = NDCB0_CMD_TYPE(0x1)
  721. | NDCB0_EXT_CMD_TYPE(ext_cmd_type)
  722. | command;
  723. info->ndcb1 = 0;
  724. info->ndcb2 = 0;
  725. info->ndcb3 = 0;
  726. }
  727. } else {
  728. info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
  729. | NDCB0_AUTO_RS
  730. | NDCB0_ST_ROW_EN
  731. | NDCB0_DBC
  732. | (NAND_CMD_PAGEPROG << 8)
  733. | NAND_CMD_SEQIN
  734. | addr_cycle;
  735. }
  736. break;
  737. case NAND_CMD_PARAM:
  738. info->buf_count = 256;
  739. info->ndcb0 |= NDCB0_CMD_TYPE(0)
  740. | NDCB0_ADDR_CYC(1)
  741. | NDCB0_LEN_OVRD
  742. | command;
  743. info->ndcb1 = (column & 0xFF);
  744. info->ndcb3 = 256;
  745. info->data_size = 256;
  746. break;
  747. case NAND_CMD_READID:
  748. info->buf_count = host->read_id_bytes;
  749. info->ndcb0 |= NDCB0_CMD_TYPE(3)
  750. | NDCB0_ADDR_CYC(1)
  751. | command;
  752. info->ndcb1 = (column & 0xFF);
  753. info->data_size = 8;
  754. break;
  755. case NAND_CMD_STATUS:
  756. info->buf_count = 1;
  757. info->ndcb0 |= NDCB0_CMD_TYPE(4)
  758. | NDCB0_ADDR_CYC(1)
  759. | command;
  760. info->data_size = 8;
  761. break;
  762. case NAND_CMD_ERASE1:
  763. info->ndcb0 |= NDCB0_CMD_TYPE(2)
  764. | NDCB0_AUTO_RS
  765. | NDCB0_ADDR_CYC(3)
  766. | NDCB0_DBC
  767. | (NAND_CMD_ERASE2 << 8)
  768. | NAND_CMD_ERASE1;
  769. info->ndcb1 = page_addr;
  770. info->ndcb2 = 0;
  771. break;
  772. case NAND_CMD_RESET:
  773. info->ndcb0 |= NDCB0_CMD_TYPE(5)
  774. | command;
  775. break;
  776. case NAND_CMD_ERASE2:
  777. exec_cmd = 0;
  778. break;
  779. default:
  780. exec_cmd = 0;
  781. dev_err(&info->pdev->dev, "non-supported command %x\n",
  782. command);
  783. break;
  784. }
  785. return exec_cmd;
  786. }
  787. static void nand_cmdfunc(struct mtd_info *mtd, unsigned command,
  788. int column, int page_addr)
  789. {
  790. struct pxa3xx_nand_host *host = mtd->priv;
  791. struct pxa3xx_nand_info *info = host->info_data;
  792. int ret, exec_cmd;
  793. /*
  794. * if this is a x16 device ,then convert the input
  795. * "byte" address into a "word" address appropriate
  796. * for indexing a word-oriented device
  797. */
  798. if (info->reg_ndcr & NDCR_DWIDTH_M)
  799. column /= 2;
  800. /*
  801. * There may be different NAND chip hooked to
  802. * different chip select, so check whether
  803. * chip select has been changed, if yes, reset the timing
  804. */
  805. if (info->cs != host->cs) {
  806. info->cs = host->cs;
  807. nand_writel(info, NDTR0CS0, info->ndtr0cs0);
  808. nand_writel(info, NDTR1CS0, info->ndtr1cs0);
  809. }
  810. prepare_start_command(info, command);
  811. info->state = STATE_PREPARED;
  812. exec_cmd = prepare_set_command(info, command, 0, column, page_addr);
  813. if (exec_cmd) {
  814. init_completion(&info->cmd_complete);
  815. init_completion(&info->dev_ready);
  816. info->need_wait = 1;
  817. pxa3xx_nand_start(info);
  818. ret = wait_for_completion_timeout(&info->cmd_complete,
  819. CHIP_DELAY_TIMEOUT);
  820. if (!ret) {
  821. dev_err(&info->pdev->dev, "Wait time out!!!\n");
  822. /* Stop State Machine for next command cycle */
  823. pxa3xx_nand_stop(info);
  824. }
  825. }
  826. info->state = STATE_IDLE;
  827. }
  828. static void nand_cmdfunc_extended(struct mtd_info *mtd,
  829. const unsigned command,
  830. int column, int page_addr)
  831. {
  832. struct pxa3xx_nand_host *host = mtd->priv;
  833. struct pxa3xx_nand_info *info = host->info_data;
  834. int ret, exec_cmd, ext_cmd_type;
  835. /*
  836. * if this is a x16 device then convert the input
  837. * "byte" address into a "word" address appropriate
  838. * for indexing a word-oriented device
  839. */
  840. if (info->reg_ndcr & NDCR_DWIDTH_M)
  841. column /= 2;
  842. /*
  843. * There may be different NAND chip hooked to
  844. * different chip select, so check whether
  845. * chip select has been changed, if yes, reset the timing
  846. */
  847. if (info->cs != host->cs) {
  848. info->cs = host->cs;
  849. nand_writel(info, NDTR0CS0, info->ndtr0cs0);
  850. nand_writel(info, NDTR1CS0, info->ndtr1cs0);
  851. }
  852. /* Select the extended command for the first command */
  853. switch (command) {
  854. case NAND_CMD_READ0:
  855. case NAND_CMD_READOOB:
  856. ext_cmd_type = EXT_CMD_TYPE_MONO;
  857. break;
  858. case NAND_CMD_SEQIN:
  859. ext_cmd_type = EXT_CMD_TYPE_DISPATCH;
  860. break;
  861. case NAND_CMD_PAGEPROG:
  862. ext_cmd_type = EXT_CMD_TYPE_NAKED_RW;
  863. break;
  864. default:
  865. ext_cmd_type = 0;
  866. break;
  867. }
  868. prepare_start_command(info, command);
  869. /*
  870. * Prepare the "is ready" completion before starting a command
  871. * transaction sequence. If the command is not executed the
  872. * completion will be completed, see below.
  873. *
  874. * We can do that inside the loop because the command variable
  875. * is invariant and thus so is the exec_cmd.
  876. */
  877. info->need_wait = 1;
  878. init_completion(&info->dev_ready);
  879. do {
  880. info->state = STATE_PREPARED;
  881. exec_cmd = prepare_set_command(info, command, ext_cmd_type,
  882. column, page_addr);
  883. if (!exec_cmd) {
  884. info->need_wait = 0;
  885. complete(&info->dev_ready);
  886. break;
  887. }
  888. init_completion(&info->cmd_complete);
  889. pxa3xx_nand_start(info);
  890. ret = wait_for_completion_timeout(&info->cmd_complete,
  891. CHIP_DELAY_TIMEOUT);
  892. if (!ret) {
  893. dev_err(&info->pdev->dev, "Wait time out!!!\n");
  894. /* Stop State Machine for next command cycle */
  895. pxa3xx_nand_stop(info);
  896. break;
  897. }
  898. /* Check if the sequence is complete */
  899. if (info->data_size == 0 && command != NAND_CMD_PAGEPROG)
  900. break;
  901. /*
  902. * After a splitted program command sequence has issued
  903. * the command dispatch, the command sequence is complete.
  904. */
  905. if (info->data_size == 0 &&
  906. command == NAND_CMD_PAGEPROG &&
  907. ext_cmd_type == EXT_CMD_TYPE_DISPATCH)
  908. break;
  909. if (command == NAND_CMD_READ0 || command == NAND_CMD_READOOB) {
  910. /* Last read: issue a 'last naked read' */
  911. if (info->data_size == info->chunk_size)
  912. ext_cmd_type = EXT_CMD_TYPE_LAST_RW;
  913. else
  914. ext_cmd_type = EXT_CMD_TYPE_NAKED_RW;
  915. /*
  916. * If a splitted program command has no more data to transfer,
  917. * the command dispatch must be issued to complete.
  918. */
  919. } else if (command == NAND_CMD_PAGEPROG &&
  920. info->data_size == 0) {
  921. ext_cmd_type = EXT_CMD_TYPE_DISPATCH;
  922. }
  923. } while (1);
  924. info->state = STATE_IDLE;
  925. }
  926. static int pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd,
  927. struct nand_chip *chip, const uint8_t *buf, int oob_required)
  928. {
  929. chip->write_buf(mtd, buf, mtd->writesize);
  930. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  931. return 0;
  932. }
  933. static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd,
  934. struct nand_chip *chip, uint8_t *buf, int oob_required,
  935. int page)
  936. {
  937. struct pxa3xx_nand_host *host = mtd->priv;
  938. struct pxa3xx_nand_info *info = host->info_data;
  939. chip->read_buf(mtd, buf, mtd->writesize);
  940. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  941. if (info->retcode == ERR_CORERR && info->use_ecc) {
  942. mtd->ecc_stats.corrected += info->ecc_err_cnt;
  943. } else if (info->retcode == ERR_UNCORERR) {
  944. /*
  945. * for blank page (all 0xff), HW will calculate its ECC as
  946. * 0, which is different from the ECC information within
  947. * OOB, ignore such uncorrectable errors
  948. */
  949. if (is_buf_blank(buf, mtd->writesize))
  950. info->retcode = ERR_NONE;
  951. else
  952. mtd->ecc_stats.failed++;
  953. }
  954. return info->max_bitflips;
  955. }
  956. static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
  957. {
  958. struct pxa3xx_nand_host *host = mtd->priv;
  959. struct pxa3xx_nand_info *info = host->info_data;
  960. char retval = 0xFF;
  961. if (info->buf_start < info->buf_count)
  962. /* Has just send a new command? */
  963. retval = info->data_buff[info->buf_start++];
  964. return retval;
  965. }
  966. static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
  967. {
  968. struct pxa3xx_nand_host *host = mtd->priv;
  969. struct pxa3xx_nand_info *info = host->info_data;
  970. u16 retval = 0xFFFF;
  971. if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
  972. retval = *((u16 *)(info->data_buff+info->buf_start));
  973. info->buf_start += 2;
  974. }
  975. return retval;
  976. }
  977. static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  978. {
  979. struct pxa3xx_nand_host *host = mtd->priv;
  980. struct pxa3xx_nand_info *info = host->info_data;
  981. int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
  982. memcpy(buf, info->data_buff + info->buf_start, real_len);
  983. info->buf_start += real_len;
  984. }
  985. static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
  986. const uint8_t *buf, int len)
  987. {
  988. struct pxa3xx_nand_host *host = mtd->priv;
  989. struct pxa3xx_nand_info *info = host->info_data;
  990. int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
  991. memcpy(info->data_buff + info->buf_start, buf, real_len);
  992. info->buf_start += real_len;
  993. }
  994. static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
  995. {
  996. return;
  997. }
  998. static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
  999. {
  1000. struct pxa3xx_nand_host *host = mtd->priv;
  1001. struct pxa3xx_nand_info *info = host->info_data;
  1002. int ret;
  1003. if (info->need_wait) {
  1004. ret = wait_for_completion_timeout(&info->dev_ready,
  1005. CHIP_DELAY_TIMEOUT);
  1006. info->need_wait = 0;
  1007. if (!ret) {
  1008. dev_err(&info->pdev->dev, "Ready time out!!!\n");
  1009. return NAND_STATUS_FAIL;
  1010. }
  1011. }
  1012. /* pxa3xx_nand_send_command has waited for command complete */
  1013. if (this->state == FL_WRITING || this->state == FL_ERASING) {
  1014. if (info->retcode == ERR_NONE)
  1015. return 0;
  1016. else
  1017. return NAND_STATUS_FAIL;
  1018. }
  1019. return NAND_STATUS_READY;
  1020. }
  1021. static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info,
  1022. const struct pxa3xx_nand_flash *f)
  1023. {
  1024. struct platform_device *pdev = info->pdev;
  1025. struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
  1026. struct pxa3xx_nand_host *host = info->host[info->cs];
  1027. uint32_t ndcr = 0x0; /* enable all interrupts */
  1028. if (f->page_size != 2048 && f->page_size != 512) {
  1029. dev_err(&pdev->dev, "Current only support 2048 and 512 size\n");
  1030. return -EINVAL;
  1031. }
  1032. if (f->flash_width != 16 && f->flash_width != 8) {
  1033. dev_err(&pdev->dev, "Only support 8bit and 16 bit!\n");
  1034. return -EINVAL;
  1035. }
  1036. /* calculate flash information */
  1037. host->read_id_bytes = (f->page_size == 2048) ? 4 : 2;
  1038. /* calculate addressing information */
  1039. host->col_addr_cycles = (f->page_size == 2048) ? 2 : 1;
  1040. if (f->num_blocks * f->page_per_block > 65536)
  1041. host->row_addr_cycles = 3;
  1042. else
  1043. host->row_addr_cycles = 2;
  1044. ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
  1045. ndcr |= (host->col_addr_cycles == 2) ? NDCR_RA_START : 0;
  1046. ndcr |= (f->page_per_block == 64) ? NDCR_PG_PER_BLK : 0;
  1047. ndcr |= (f->page_size == 2048) ? NDCR_PAGE_SZ : 0;
  1048. ndcr |= (f->flash_width == 16) ? NDCR_DWIDTH_M : 0;
  1049. ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0;
  1050. ndcr |= NDCR_RD_ID_CNT(host->read_id_bytes);
  1051. ndcr |= NDCR_SPARE_EN; /* enable spare by default */
  1052. info->reg_ndcr = ndcr;
  1053. pxa3xx_nand_set_timing(host, f->timing);
  1054. return 0;
  1055. }
  1056. static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
  1057. {
  1058. /*
  1059. * We set 0 by hard coding here, for we don't support keep_config
  1060. * when there is more than one chip attached to the controller
  1061. */
  1062. struct pxa3xx_nand_host *host = info->host[0];
  1063. uint32_t ndcr = nand_readl(info, NDCR);
  1064. if (ndcr & NDCR_PAGE_SZ) {
  1065. /* Controller's FIFO size */
  1066. info->chunk_size = 2048;
  1067. host->read_id_bytes = 4;
  1068. } else {
  1069. info->chunk_size = 512;
  1070. host->read_id_bytes = 2;
  1071. }
  1072. /* Set an initial chunk size */
  1073. info->reg_ndcr = ndcr & ~NDCR_INT_MASK;
  1074. info->ndtr0cs0 = nand_readl(info, NDTR0CS0);
  1075. info->ndtr1cs0 = nand_readl(info, NDTR1CS0);
  1076. return 0;
  1077. }
  1078. #ifdef ARCH_HAS_DMA
  1079. static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
  1080. {
  1081. struct platform_device *pdev = info->pdev;
  1082. int data_desc_offset = info->buf_size - sizeof(struct pxa_dma_desc);
  1083. if (use_dma == 0) {
  1084. info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
  1085. if (info->data_buff == NULL)
  1086. return -ENOMEM;
  1087. return 0;
  1088. }
  1089. info->data_buff = dma_alloc_coherent(&pdev->dev, info->buf_size,
  1090. &info->data_buff_phys, GFP_KERNEL);
  1091. if (info->data_buff == NULL) {
  1092. dev_err(&pdev->dev, "failed to allocate dma buffer\n");
  1093. return -ENOMEM;
  1094. }
  1095. info->data_desc = (void *)info->data_buff + data_desc_offset;
  1096. info->data_desc_addr = info->data_buff_phys + data_desc_offset;
  1097. info->data_dma_ch = pxa_request_dma("nand-data", DMA_PRIO_LOW,
  1098. pxa3xx_nand_data_dma_irq, info);
  1099. if (info->data_dma_ch < 0) {
  1100. dev_err(&pdev->dev, "failed to request data dma\n");
  1101. dma_free_coherent(&pdev->dev, info->buf_size,
  1102. info->data_buff, info->data_buff_phys);
  1103. return info->data_dma_ch;
  1104. }
  1105. /*
  1106. * Now that DMA buffers are allocated we turn on
  1107. * DMA proper for I/O operations.
  1108. */
  1109. info->use_dma = 1;
  1110. return 0;
  1111. }
  1112. static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
  1113. {
  1114. struct platform_device *pdev = info->pdev;
  1115. if (info->use_dma) {
  1116. pxa_free_dma(info->data_dma_ch);
  1117. dma_free_coherent(&pdev->dev, info->buf_size,
  1118. info->data_buff, info->data_buff_phys);
  1119. } else {
  1120. kfree(info->data_buff);
  1121. }
  1122. }
  1123. #else
  1124. static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
  1125. {
  1126. info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
  1127. if (info->data_buff == NULL)
  1128. return -ENOMEM;
  1129. return 0;
  1130. }
  1131. static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
  1132. {
  1133. kfree(info->data_buff);
  1134. }
  1135. #endif
  1136. static int pxa3xx_nand_sensing(struct pxa3xx_nand_info *info)
  1137. {
  1138. struct mtd_info *mtd;
  1139. struct nand_chip *chip;
  1140. int ret;
  1141. mtd = info->host[info->cs]->mtd;
  1142. chip = mtd->priv;
  1143. /* use the common timing to make a try */
  1144. ret = pxa3xx_nand_config_flash(info, &builtin_flash_types[0]);
  1145. if (ret)
  1146. return ret;
  1147. chip->cmdfunc(mtd, NAND_CMD_RESET, 0, 0);
  1148. ret = chip->waitfunc(mtd, chip);
  1149. if (ret & NAND_STATUS_FAIL)
  1150. return -ENODEV;
  1151. return 0;
  1152. }
  1153. static int pxa_ecc_init(struct pxa3xx_nand_info *info,
  1154. struct nand_ecc_ctrl *ecc,
  1155. int strength, int ecc_stepsize, int page_size)
  1156. {
  1157. if (strength == 1 && ecc_stepsize == 512 && page_size == 2048) {
  1158. info->chunk_size = 2048;
  1159. info->spare_size = 40;
  1160. info->ecc_size = 24;
  1161. ecc->mode = NAND_ECC_HW;
  1162. ecc->size = 512;
  1163. ecc->strength = 1;
  1164. } else if (strength == 1 && ecc_stepsize == 512 && page_size == 512) {
  1165. info->chunk_size = 512;
  1166. info->spare_size = 8;
  1167. info->ecc_size = 8;
  1168. ecc->mode = NAND_ECC_HW;
  1169. ecc->size = 512;
  1170. ecc->strength = 1;
  1171. /*
  1172. * Required ECC: 4-bit correction per 512 bytes
  1173. * Select: 16-bit correction per 2048 bytes
  1174. */
  1175. } else if (strength == 4 && ecc_stepsize == 512 && page_size == 2048) {
  1176. info->ecc_bch = 1;
  1177. info->chunk_size = 2048;
  1178. info->spare_size = 32;
  1179. info->ecc_size = 32;
  1180. ecc->mode = NAND_ECC_HW;
  1181. ecc->size = info->chunk_size;
  1182. ecc->layout = &ecc_layout_2KB_bch4bit;
  1183. ecc->strength = 16;
  1184. } else if (strength == 4 && ecc_stepsize == 512 && page_size == 4096) {
  1185. info->ecc_bch = 1;
  1186. info->chunk_size = 2048;
  1187. info->spare_size = 32;
  1188. info->ecc_size = 32;
  1189. ecc->mode = NAND_ECC_HW;
  1190. ecc->size = info->chunk_size;
  1191. ecc->layout = &ecc_layout_4KB_bch4bit;
  1192. ecc->strength = 16;
  1193. /*
  1194. * Required ECC: 8-bit correction per 512 bytes
  1195. * Select: 16-bit correction per 1024 bytes
  1196. */
  1197. } else if (strength == 8 && ecc_stepsize == 512 && page_size == 4096) {
  1198. info->ecc_bch = 1;
  1199. info->chunk_size = 1024;
  1200. info->spare_size = 0;
  1201. info->ecc_size = 32;
  1202. ecc->mode = NAND_ECC_HW;
  1203. ecc->size = info->chunk_size;
  1204. ecc->layout = &ecc_layout_4KB_bch8bit;
  1205. ecc->strength = 16;
  1206. } else {
  1207. dev_err(&info->pdev->dev,
  1208. "ECC strength %d at page size %d is not supported\n",
  1209. strength, page_size);
  1210. return -ENODEV;
  1211. }
  1212. dev_info(&info->pdev->dev, "ECC strength %d, ECC step size %d\n",
  1213. ecc->strength, ecc->size);
  1214. return 0;
  1215. }
  1216. static int pxa3xx_nand_scan(struct mtd_info *mtd)
  1217. {
  1218. struct pxa3xx_nand_host *host = mtd->priv;
  1219. struct pxa3xx_nand_info *info = host->info_data;
  1220. struct platform_device *pdev = info->pdev;
  1221. struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
  1222. struct nand_flash_dev pxa3xx_flash_ids[2], *def = NULL;
  1223. const struct pxa3xx_nand_flash *f = NULL;
  1224. struct nand_chip *chip = mtd->priv;
  1225. uint32_t id = -1;
  1226. uint64_t chipsize;
  1227. int i, ret, num;
  1228. uint16_t ecc_strength, ecc_step;
  1229. if (pdata->keep_config && !pxa3xx_nand_detect_config(info))
  1230. goto KEEP_CONFIG;
  1231. ret = pxa3xx_nand_sensing(info);
  1232. if (ret) {
  1233. dev_info(&info->pdev->dev, "There is no chip on cs %d!\n",
  1234. info->cs);
  1235. return ret;
  1236. }
  1237. chip->cmdfunc(mtd, NAND_CMD_READID, 0, 0);
  1238. id = *((uint16_t *)(info->data_buff));
  1239. if (id != 0)
  1240. dev_info(&info->pdev->dev, "Detect a flash id %x\n", id);
  1241. else {
  1242. dev_warn(&info->pdev->dev,
  1243. "Read out ID 0, potential timing set wrong!!\n");
  1244. return -EINVAL;
  1245. }
  1246. num = ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1;
  1247. for (i = 0; i < num; i++) {
  1248. if (i < pdata->num_flash)
  1249. f = pdata->flash + i;
  1250. else
  1251. f = &builtin_flash_types[i - pdata->num_flash + 1];
  1252. /* find the chip in default list */
  1253. if (f->chip_id == id)
  1254. break;
  1255. }
  1256. if (i >= (ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1)) {
  1257. dev_err(&info->pdev->dev, "ERROR!! flash not defined!!!\n");
  1258. return -EINVAL;
  1259. }
  1260. ret = pxa3xx_nand_config_flash(info, f);
  1261. if (ret) {
  1262. dev_err(&info->pdev->dev, "ERROR! Configure failed\n");
  1263. return ret;
  1264. }
  1265. pxa3xx_flash_ids[0].name = f->name;
  1266. pxa3xx_flash_ids[0].dev_id = (f->chip_id >> 8) & 0xffff;
  1267. pxa3xx_flash_ids[0].pagesize = f->page_size;
  1268. chipsize = (uint64_t)f->num_blocks * f->page_per_block * f->page_size;
  1269. pxa3xx_flash_ids[0].chipsize = chipsize >> 20;
  1270. pxa3xx_flash_ids[0].erasesize = f->page_size * f->page_per_block;
  1271. if (f->flash_width == 16)
  1272. pxa3xx_flash_ids[0].options = NAND_BUSWIDTH_16;
  1273. pxa3xx_flash_ids[1].name = NULL;
  1274. def = pxa3xx_flash_ids;
  1275. KEEP_CONFIG:
  1276. if (info->reg_ndcr & NDCR_DWIDTH_M)
  1277. chip->options |= NAND_BUSWIDTH_16;
  1278. /* Device detection must be done with ECC disabled */
  1279. if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
  1280. nand_writel(info, NDECCCTRL, 0x0);
  1281. if (nand_scan_ident(mtd, 1, def))
  1282. return -ENODEV;
  1283. if (pdata->flash_bbt) {
  1284. /*
  1285. * We'll use a bad block table stored in-flash and don't
  1286. * allow writing the bad block marker to the flash.
  1287. */
  1288. chip->bbt_options |= NAND_BBT_USE_FLASH |
  1289. NAND_BBT_NO_OOB_BBM;
  1290. chip->bbt_td = &bbt_main_descr;
  1291. chip->bbt_md = &bbt_mirror_descr;
  1292. }
  1293. /*
  1294. * If the page size is bigger than the FIFO size, let's check
  1295. * we are given the right variant and then switch to the extended
  1296. * (aka splitted) command handling,
  1297. */
  1298. if (mtd->writesize > PAGE_CHUNK_SIZE) {
  1299. if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370) {
  1300. chip->cmdfunc = nand_cmdfunc_extended;
  1301. } else {
  1302. dev_err(&info->pdev->dev,
  1303. "unsupported page size on this variant\n");
  1304. return -ENODEV;
  1305. }
  1306. }
  1307. if (pdata->ecc_strength && pdata->ecc_step_size) {
  1308. ecc_strength = pdata->ecc_strength;
  1309. ecc_step = pdata->ecc_step_size;
  1310. } else {
  1311. ecc_strength = chip->ecc_strength_ds;
  1312. ecc_step = chip->ecc_step_ds;
  1313. }
  1314. /* Set default ECC strength requirements on non-ONFI devices */
  1315. if (ecc_strength < 1 && ecc_step < 1) {
  1316. ecc_strength = 1;
  1317. ecc_step = 512;
  1318. }
  1319. ret = pxa_ecc_init(info, &chip->ecc, ecc_strength,
  1320. ecc_step, mtd->writesize);
  1321. if (ret)
  1322. return ret;
  1323. /* calculate addressing information */
  1324. if (mtd->writesize >= 2048)
  1325. host->col_addr_cycles = 2;
  1326. else
  1327. host->col_addr_cycles = 1;
  1328. /* release the initial buffer */
  1329. kfree(info->data_buff);
  1330. /* allocate the real data + oob buffer */
  1331. info->buf_size = mtd->writesize + mtd->oobsize;
  1332. ret = pxa3xx_nand_init_buff(info);
  1333. if (ret)
  1334. return ret;
  1335. info->oob_buff = info->data_buff + mtd->writesize;
  1336. if ((mtd->size >> chip->page_shift) > 65536)
  1337. host->row_addr_cycles = 3;
  1338. else
  1339. host->row_addr_cycles = 2;
  1340. return nand_scan_tail(mtd);
  1341. }
  1342. static int alloc_nand_resource(struct platform_device *pdev)
  1343. {
  1344. struct pxa3xx_nand_platform_data *pdata;
  1345. struct pxa3xx_nand_info *info;
  1346. struct pxa3xx_nand_host *host;
  1347. struct nand_chip *chip = NULL;
  1348. struct mtd_info *mtd;
  1349. struct resource *r;
  1350. int ret, irq, cs;
  1351. pdata = dev_get_platdata(&pdev->dev);
  1352. info = devm_kzalloc(&pdev->dev, sizeof(*info) + (sizeof(*mtd) +
  1353. sizeof(*host)) * pdata->num_cs, GFP_KERNEL);
  1354. if (!info)
  1355. return -ENOMEM;
  1356. info->pdev = pdev;
  1357. info->variant = pxa3xx_nand_get_variant(pdev);
  1358. for (cs = 0; cs < pdata->num_cs; cs++) {
  1359. mtd = (struct mtd_info *)((unsigned int)&info[1] +
  1360. (sizeof(*mtd) + sizeof(*host)) * cs);
  1361. chip = (struct nand_chip *)(&mtd[1]);
  1362. host = (struct pxa3xx_nand_host *)chip;
  1363. info->host[cs] = host;
  1364. host->mtd = mtd;
  1365. host->cs = cs;
  1366. host->info_data = info;
  1367. mtd->priv = host;
  1368. mtd->owner = THIS_MODULE;
  1369. chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
  1370. chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
  1371. chip->controller = &info->controller;
  1372. chip->waitfunc = pxa3xx_nand_waitfunc;
  1373. chip->select_chip = pxa3xx_nand_select_chip;
  1374. chip->read_word = pxa3xx_nand_read_word;
  1375. chip->read_byte = pxa3xx_nand_read_byte;
  1376. chip->read_buf = pxa3xx_nand_read_buf;
  1377. chip->write_buf = pxa3xx_nand_write_buf;
  1378. chip->options |= NAND_NO_SUBPAGE_WRITE;
  1379. chip->cmdfunc = nand_cmdfunc;
  1380. }
  1381. spin_lock_init(&chip->controller->lock);
  1382. init_waitqueue_head(&chip->controller->wq);
  1383. info->clk = devm_clk_get(&pdev->dev, NULL);
  1384. if (IS_ERR(info->clk)) {
  1385. dev_err(&pdev->dev, "failed to get nand clock\n");
  1386. return PTR_ERR(info->clk);
  1387. }
  1388. ret = clk_prepare_enable(info->clk);
  1389. if (ret < 0)
  1390. return ret;
  1391. if (use_dma) {
  1392. /*
  1393. * This is a dirty hack to make this driver work from
  1394. * devicetree bindings. It can be removed once we have
  1395. * a prober DMA controller framework for DT.
  1396. */
  1397. if (pdev->dev.of_node &&
  1398. of_machine_is_compatible("marvell,pxa3xx")) {
  1399. info->drcmr_dat = 97;
  1400. info->drcmr_cmd = 99;
  1401. } else {
  1402. r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  1403. if (r == NULL) {
  1404. dev_err(&pdev->dev,
  1405. "no resource defined for data DMA\n");
  1406. ret = -ENXIO;
  1407. goto fail_disable_clk;
  1408. }
  1409. info->drcmr_dat = r->start;
  1410. r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
  1411. if (r == NULL) {
  1412. dev_err(&pdev->dev,
  1413. "no resource defined for cmd DMA\n");
  1414. ret = -ENXIO;
  1415. goto fail_disable_clk;
  1416. }
  1417. info->drcmr_cmd = r->start;
  1418. }
  1419. }
  1420. irq = platform_get_irq(pdev, 0);
  1421. if (irq < 0) {
  1422. dev_err(&pdev->dev, "no IRQ resource defined\n");
  1423. ret = -ENXIO;
  1424. goto fail_disable_clk;
  1425. }
  1426. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1427. info->mmio_base = devm_ioremap_resource(&pdev->dev, r);
  1428. if (IS_ERR(info->mmio_base)) {
  1429. ret = PTR_ERR(info->mmio_base);
  1430. goto fail_disable_clk;
  1431. }
  1432. info->mmio_phys = r->start;
  1433. /* Allocate a buffer to allow flash detection */
  1434. info->buf_size = INIT_BUFFER_SIZE;
  1435. info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
  1436. if (info->data_buff == NULL) {
  1437. ret = -ENOMEM;
  1438. goto fail_disable_clk;
  1439. }
  1440. /* initialize all interrupts to be disabled */
  1441. disable_int(info, NDSR_MASK);
  1442. ret = request_irq(irq, pxa3xx_nand_irq, 0, pdev->name, info);
  1443. if (ret < 0) {
  1444. dev_err(&pdev->dev, "failed to request IRQ\n");
  1445. goto fail_free_buf;
  1446. }
  1447. platform_set_drvdata(pdev, info);
  1448. return 0;
  1449. fail_free_buf:
  1450. free_irq(irq, info);
  1451. kfree(info->data_buff);
  1452. fail_disable_clk:
  1453. clk_disable_unprepare(info->clk);
  1454. return ret;
  1455. }
  1456. static int pxa3xx_nand_remove(struct platform_device *pdev)
  1457. {
  1458. struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
  1459. struct pxa3xx_nand_platform_data *pdata;
  1460. int irq, cs;
  1461. if (!info)
  1462. return 0;
  1463. pdata = dev_get_platdata(&pdev->dev);
  1464. irq = platform_get_irq(pdev, 0);
  1465. if (irq >= 0)
  1466. free_irq(irq, info);
  1467. pxa3xx_nand_free_buff(info);
  1468. clk_disable_unprepare(info->clk);
  1469. for (cs = 0; cs < pdata->num_cs; cs++)
  1470. nand_release(info->host[cs]->mtd);
  1471. return 0;
  1472. }
  1473. static int pxa3xx_nand_probe_dt(struct platform_device *pdev)
  1474. {
  1475. struct pxa3xx_nand_platform_data *pdata;
  1476. struct device_node *np = pdev->dev.of_node;
  1477. const struct of_device_id *of_id =
  1478. of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
  1479. if (!of_id)
  1480. return 0;
  1481. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  1482. if (!pdata)
  1483. return -ENOMEM;
  1484. if (of_get_property(np, "marvell,nand-enable-arbiter", NULL))
  1485. pdata->enable_arbiter = 1;
  1486. if (of_get_property(np, "marvell,nand-keep-config", NULL))
  1487. pdata->keep_config = 1;
  1488. of_property_read_u32(np, "num-cs", &pdata->num_cs);
  1489. pdata->flash_bbt = of_get_nand_on_flash_bbt(np);
  1490. pdata->ecc_strength = of_get_nand_ecc_strength(np);
  1491. if (pdata->ecc_strength < 0)
  1492. pdata->ecc_strength = 0;
  1493. pdata->ecc_step_size = of_get_nand_ecc_step_size(np);
  1494. if (pdata->ecc_step_size < 0)
  1495. pdata->ecc_step_size = 0;
  1496. pdev->dev.platform_data = pdata;
  1497. return 0;
  1498. }
  1499. static int pxa3xx_nand_probe(struct platform_device *pdev)
  1500. {
  1501. struct pxa3xx_nand_platform_data *pdata;
  1502. struct mtd_part_parser_data ppdata = {};
  1503. struct pxa3xx_nand_info *info;
  1504. int ret, cs, probe_success;
  1505. #ifndef ARCH_HAS_DMA
  1506. if (use_dma) {
  1507. use_dma = 0;
  1508. dev_warn(&pdev->dev,
  1509. "This platform can't do DMA on this device\n");
  1510. }
  1511. #endif
  1512. ret = pxa3xx_nand_probe_dt(pdev);
  1513. if (ret)
  1514. return ret;
  1515. pdata = dev_get_platdata(&pdev->dev);
  1516. if (!pdata) {
  1517. dev_err(&pdev->dev, "no platform data defined\n");
  1518. return -ENODEV;
  1519. }
  1520. ret = alloc_nand_resource(pdev);
  1521. if (ret) {
  1522. dev_err(&pdev->dev, "alloc nand resource failed\n");
  1523. return ret;
  1524. }
  1525. info = platform_get_drvdata(pdev);
  1526. probe_success = 0;
  1527. for (cs = 0; cs < pdata->num_cs; cs++) {
  1528. struct mtd_info *mtd = info->host[cs]->mtd;
  1529. /*
  1530. * The mtd name matches the one used in 'mtdparts' kernel
  1531. * parameter. This name cannot be changed or otherwise
  1532. * user's mtd partitions configuration would get broken.
  1533. */
  1534. mtd->name = "pxa3xx_nand-0";
  1535. info->cs = cs;
  1536. ret = pxa3xx_nand_scan(mtd);
  1537. if (ret) {
  1538. dev_warn(&pdev->dev, "failed to scan nand at cs %d\n",
  1539. cs);
  1540. continue;
  1541. }
  1542. ppdata.of_node = pdev->dev.of_node;
  1543. ret = mtd_device_parse_register(mtd, NULL,
  1544. &ppdata, pdata->parts[cs],
  1545. pdata->nr_parts[cs]);
  1546. if (!ret)
  1547. probe_success = 1;
  1548. }
  1549. if (!probe_success) {
  1550. pxa3xx_nand_remove(pdev);
  1551. return -ENODEV;
  1552. }
  1553. return 0;
  1554. }
  1555. #ifdef CONFIG_PM
  1556. static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state)
  1557. {
  1558. struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
  1559. struct pxa3xx_nand_platform_data *pdata;
  1560. struct mtd_info *mtd;
  1561. int cs;
  1562. pdata = dev_get_platdata(&pdev->dev);
  1563. if (info->state) {
  1564. dev_err(&pdev->dev, "driver busy, state = %d\n", info->state);
  1565. return -EAGAIN;
  1566. }
  1567. for (cs = 0; cs < pdata->num_cs; cs++) {
  1568. mtd = info->host[cs]->mtd;
  1569. mtd_suspend(mtd);
  1570. }
  1571. return 0;
  1572. }
  1573. static int pxa3xx_nand_resume(struct platform_device *pdev)
  1574. {
  1575. struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
  1576. struct pxa3xx_nand_platform_data *pdata;
  1577. struct mtd_info *mtd;
  1578. int cs;
  1579. pdata = dev_get_platdata(&pdev->dev);
  1580. /* We don't want to handle interrupt without calling mtd routine */
  1581. disable_int(info, NDCR_INT_MASK);
  1582. /*
  1583. * Directly set the chip select to a invalid value,
  1584. * then the driver would reset the timing according
  1585. * to current chip select at the beginning of cmdfunc
  1586. */
  1587. info->cs = 0xff;
  1588. /*
  1589. * As the spec says, the NDSR would be updated to 0x1800 when
  1590. * doing the nand_clk disable/enable.
  1591. * To prevent it damaging state machine of the driver, clear
  1592. * all status before resume
  1593. */
  1594. nand_writel(info, NDSR, NDSR_MASK);
  1595. for (cs = 0; cs < pdata->num_cs; cs++) {
  1596. mtd = info->host[cs]->mtd;
  1597. mtd_resume(mtd);
  1598. }
  1599. return 0;
  1600. }
  1601. #else
  1602. #define pxa3xx_nand_suspend NULL
  1603. #define pxa3xx_nand_resume NULL
  1604. #endif
  1605. static struct platform_driver pxa3xx_nand_driver = {
  1606. .driver = {
  1607. .name = "pxa3xx-nand",
  1608. .of_match_table = pxa3xx_nand_dt_ids,
  1609. },
  1610. .probe = pxa3xx_nand_probe,
  1611. .remove = pxa3xx_nand_remove,
  1612. .suspend = pxa3xx_nand_suspend,
  1613. .resume = pxa3xx_nand_resume,
  1614. };
  1615. module_platform_driver(pxa3xx_nand_driver);
  1616. MODULE_LICENSE("GPL");
  1617. MODULE_DESCRIPTION("PXA3xx NAND controller driver");