pxa3xx_nand.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924
  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 drain_fifo(struct pxa3xx_nand_info *info, void *data, int len)
  415. {
  416. if (info->ecc_bch) {
  417. int timeout;
  418. /*
  419. * According to the datasheet, when reading from NDDB
  420. * with BCH enabled, after each 32 bytes reads, we
  421. * have to make sure that the NDSR.RDDREQ bit is set.
  422. *
  423. * Drain the FIFO 8 32 bits reads at a time, and skip
  424. * the polling on the last read.
  425. */
  426. while (len > 8) {
  427. __raw_readsl(info->mmio_base + NDDB, data, 8);
  428. for (timeout = 0;
  429. !(nand_readl(info, NDSR) & NDSR_RDDREQ);
  430. timeout++) {
  431. if (timeout >= 5) {
  432. dev_err(&info->pdev->dev,
  433. "Timeout on RDDREQ while draining the FIFO\n");
  434. return;
  435. }
  436. mdelay(1);
  437. }
  438. data += 32;
  439. len -= 8;
  440. }
  441. }
  442. __raw_readsl(info->mmio_base + NDDB, data, len);
  443. }
  444. static void handle_data_pio(struct pxa3xx_nand_info *info)
  445. {
  446. unsigned int do_bytes = min(info->data_size, info->chunk_size);
  447. switch (info->state) {
  448. case STATE_PIO_WRITING:
  449. __raw_writesl(info->mmio_base + NDDB,
  450. info->data_buff + info->data_buff_pos,
  451. DIV_ROUND_UP(do_bytes, 4));
  452. if (info->oob_size > 0)
  453. __raw_writesl(info->mmio_base + NDDB,
  454. info->oob_buff + info->oob_buff_pos,
  455. DIV_ROUND_UP(info->oob_size, 4));
  456. break;
  457. case STATE_PIO_READING:
  458. drain_fifo(info,
  459. info->data_buff + info->data_buff_pos,
  460. DIV_ROUND_UP(do_bytes, 4));
  461. if (info->oob_size > 0)
  462. drain_fifo(info,
  463. info->oob_buff + info->oob_buff_pos,
  464. DIV_ROUND_UP(info->oob_size, 4));
  465. break;
  466. default:
  467. dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
  468. info->state);
  469. BUG();
  470. }
  471. /* Update buffer pointers for multi-page read/write */
  472. info->data_buff_pos += do_bytes;
  473. info->oob_buff_pos += info->oob_size;
  474. info->data_size -= do_bytes;
  475. }
  476. #ifdef ARCH_HAS_DMA
  477. static void start_data_dma(struct pxa3xx_nand_info *info)
  478. {
  479. struct pxa_dma_desc *desc = info->data_desc;
  480. int dma_len = ALIGN(info->data_size + info->oob_size, 32);
  481. desc->ddadr = DDADR_STOP;
  482. desc->dcmd = DCMD_ENDIRQEN | DCMD_WIDTH4 | DCMD_BURST32 | dma_len;
  483. switch (info->state) {
  484. case STATE_DMA_WRITING:
  485. desc->dsadr = info->data_buff_phys;
  486. desc->dtadr = info->mmio_phys + NDDB;
  487. desc->dcmd |= DCMD_INCSRCADDR | DCMD_FLOWTRG;
  488. break;
  489. case STATE_DMA_READING:
  490. desc->dtadr = info->data_buff_phys;
  491. desc->dsadr = info->mmio_phys + NDDB;
  492. desc->dcmd |= DCMD_INCTRGADDR | DCMD_FLOWSRC;
  493. break;
  494. default:
  495. dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
  496. info->state);
  497. BUG();
  498. }
  499. DRCMR(info->drcmr_dat) = DRCMR_MAPVLD | info->data_dma_ch;
  500. DDADR(info->data_dma_ch) = info->data_desc_addr;
  501. DCSR(info->data_dma_ch) |= DCSR_RUN;
  502. }
  503. static void pxa3xx_nand_data_dma_irq(int channel, void *data)
  504. {
  505. struct pxa3xx_nand_info *info = data;
  506. uint32_t dcsr;
  507. dcsr = DCSR(channel);
  508. DCSR(channel) = dcsr;
  509. if (dcsr & DCSR_BUSERR) {
  510. info->retcode = ERR_DMABUSERR;
  511. }
  512. info->state = STATE_DMA_DONE;
  513. enable_int(info, NDCR_INT_MASK);
  514. nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
  515. }
  516. #else
  517. static void start_data_dma(struct pxa3xx_nand_info *info)
  518. {}
  519. #endif
  520. static irqreturn_t pxa3xx_nand_irq(int irq, void *devid)
  521. {
  522. struct pxa3xx_nand_info *info = devid;
  523. unsigned int status, is_completed = 0, is_ready = 0;
  524. unsigned int ready, cmd_done;
  525. if (info->cs == 0) {
  526. ready = NDSR_FLASH_RDY;
  527. cmd_done = NDSR_CS0_CMDD;
  528. } else {
  529. ready = NDSR_RDY;
  530. cmd_done = NDSR_CS1_CMDD;
  531. }
  532. status = nand_readl(info, NDSR);
  533. if (status & NDSR_UNCORERR)
  534. info->retcode = ERR_UNCORERR;
  535. if (status & NDSR_CORERR) {
  536. info->retcode = ERR_CORERR;
  537. if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370 &&
  538. info->ecc_bch)
  539. info->ecc_err_cnt = NDSR_ERR_CNT(status);
  540. else
  541. info->ecc_err_cnt = 1;
  542. /*
  543. * Each chunk composing a page is corrected independently,
  544. * and we need to store maximum number of corrected bitflips
  545. * to return it to the MTD layer in ecc.read_page().
  546. */
  547. info->max_bitflips = max_t(unsigned int,
  548. info->max_bitflips,
  549. info->ecc_err_cnt);
  550. }
  551. if (status & (NDSR_RDDREQ | NDSR_WRDREQ)) {
  552. /* whether use dma to transfer data */
  553. if (info->use_dma) {
  554. disable_int(info, NDCR_INT_MASK);
  555. info->state = (status & NDSR_RDDREQ) ?
  556. STATE_DMA_READING : STATE_DMA_WRITING;
  557. start_data_dma(info);
  558. goto NORMAL_IRQ_EXIT;
  559. } else {
  560. info->state = (status & NDSR_RDDREQ) ?
  561. STATE_PIO_READING : STATE_PIO_WRITING;
  562. handle_data_pio(info);
  563. }
  564. }
  565. if (status & cmd_done) {
  566. info->state = STATE_CMD_DONE;
  567. is_completed = 1;
  568. }
  569. if (status & ready) {
  570. info->state = STATE_READY;
  571. is_ready = 1;
  572. }
  573. if (status & NDSR_WRCMDREQ) {
  574. nand_writel(info, NDSR, NDSR_WRCMDREQ);
  575. status &= ~NDSR_WRCMDREQ;
  576. info->state = STATE_CMD_HANDLE;
  577. /*
  578. * Command buffer registers NDCB{0-2} (and optionally NDCB3)
  579. * must be loaded by writing directly either 12 or 16
  580. * bytes directly to NDCB0, four bytes at a time.
  581. *
  582. * Direct write access to NDCB1, NDCB2 and NDCB3 is ignored
  583. * but each NDCBx register can be read.
  584. */
  585. nand_writel(info, NDCB0, info->ndcb0);
  586. nand_writel(info, NDCB0, info->ndcb1);
  587. nand_writel(info, NDCB0, info->ndcb2);
  588. /* NDCB3 register is available in NFCv2 (Armada 370/XP SoC) */
  589. if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
  590. nand_writel(info, NDCB0, info->ndcb3);
  591. }
  592. /* clear NDSR to let the controller exit the IRQ */
  593. nand_writel(info, NDSR, status);
  594. if (is_completed)
  595. complete(&info->cmd_complete);
  596. if (is_ready)
  597. complete(&info->dev_ready);
  598. NORMAL_IRQ_EXIT:
  599. return IRQ_HANDLED;
  600. }
  601. static inline int is_buf_blank(uint8_t *buf, size_t len)
  602. {
  603. for (; len > 0; len--)
  604. if (*buf++ != 0xff)
  605. return 0;
  606. return 1;
  607. }
  608. static void set_command_address(struct pxa3xx_nand_info *info,
  609. unsigned int page_size, uint16_t column, int page_addr)
  610. {
  611. /* small page addr setting */
  612. if (page_size < PAGE_CHUNK_SIZE) {
  613. info->ndcb1 = ((page_addr & 0xFFFFFF) << 8)
  614. | (column & 0xFF);
  615. info->ndcb2 = 0;
  616. } else {
  617. info->ndcb1 = ((page_addr & 0xFFFF) << 16)
  618. | (column & 0xFFFF);
  619. if (page_addr & 0xFF0000)
  620. info->ndcb2 = (page_addr & 0xFF0000) >> 16;
  621. else
  622. info->ndcb2 = 0;
  623. }
  624. }
  625. static void prepare_start_command(struct pxa3xx_nand_info *info, int command)
  626. {
  627. struct pxa3xx_nand_host *host = info->host[info->cs];
  628. struct mtd_info *mtd = host->mtd;
  629. /* reset data and oob column point to handle data */
  630. info->buf_start = 0;
  631. info->buf_count = 0;
  632. info->oob_size = 0;
  633. info->data_buff_pos = 0;
  634. info->oob_buff_pos = 0;
  635. info->use_ecc = 0;
  636. info->use_spare = 1;
  637. info->retcode = ERR_NONE;
  638. info->ecc_err_cnt = 0;
  639. info->ndcb3 = 0;
  640. info->need_wait = 0;
  641. switch (command) {
  642. case NAND_CMD_READ0:
  643. case NAND_CMD_PAGEPROG:
  644. info->use_ecc = 1;
  645. case NAND_CMD_READOOB:
  646. pxa3xx_set_datasize(info, mtd);
  647. break;
  648. case NAND_CMD_PARAM:
  649. info->use_spare = 0;
  650. break;
  651. default:
  652. info->ndcb1 = 0;
  653. info->ndcb2 = 0;
  654. break;
  655. }
  656. /*
  657. * If we are about to issue a read command, or about to set
  658. * the write address, then clean the data buffer.
  659. */
  660. if (command == NAND_CMD_READ0 ||
  661. command == NAND_CMD_READOOB ||
  662. command == NAND_CMD_SEQIN) {
  663. info->buf_count = mtd->writesize + mtd->oobsize;
  664. memset(info->data_buff, 0xFF, info->buf_count);
  665. }
  666. }
  667. static int prepare_set_command(struct pxa3xx_nand_info *info, int command,
  668. int ext_cmd_type, uint16_t column, int page_addr)
  669. {
  670. int addr_cycle, exec_cmd;
  671. struct pxa3xx_nand_host *host;
  672. struct mtd_info *mtd;
  673. host = info->host[info->cs];
  674. mtd = host->mtd;
  675. addr_cycle = 0;
  676. exec_cmd = 1;
  677. if (info->cs != 0)
  678. info->ndcb0 = NDCB0_CSEL;
  679. else
  680. info->ndcb0 = 0;
  681. if (command == NAND_CMD_SEQIN)
  682. exec_cmd = 0;
  683. addr_cycle = NDCB0_ADDR_CYC(host->row_addr_cycles
  684. + host->col_addr_cycles);
  685. switch (command) {
  686. case NAND_CMD_READOOB:
  687. case NAND_CMD_READ0:
  688. info->buf_start = column;
  689. info->ndcb0 |= NDCB0_CMD_TYPE(0)
  690. | addr_cycle
  691. | NAND_CMD_READ0;
  692. if (command == NAND_CMD_READOOB)
  693. info->buf_start += mtd->writesize;
  694. /*
  695. * Multiple page read needs an 'extended command type' field,
  696. * which is either naked-read or last-read according to the
  697. * state.
  698. */
  699. if (mtd->writesize == PAGE_CHUNK_SIZE) {
  700. info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8);
  701. } else if (mtd->writesize > PAGE_CHUNK_SIZE) {
  702. info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8)
  703. | NDCB0_LEN_OVRD
  704. | NDCB0_EXT_CMD_TYPE(ext_cmd_type);
  705. info->ndcb3 = info->chunk_size +
  706. info->oob_size;
  707. }
  708. set_command_address(info, mtd->writesize, column, page_addr);
  709. break;
  710. case NAND_CMD_SEQIN:
  711. info->buf_start = column;
  712. set_command_address(info, mtd->writesize, 0, page_addr);
  713. /*
  714. * Multiple page programming needs to execute the initial
  715. * SEQIN command that sets the page address.
  716. */
  717. if (mtd->writesize > PAGE_CHUNK_SIZE) {
  718. info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
  719. | NDCB0_EXT_CMD_TYPE(ext_cmd_type)
  720. | addr_cycle
  721. | command;
  722. /* No data transfer in this case */
  723. info->data_size = 0;
  724. exec_cmd = 1;
  725. }
  726. break;
  727. case NAND_CMD_PAGEPROG:
  728. if (is_buf_blank(info->data_buff,
  729. (mtd->writesize + mtd->oobsize))) {
  730. exec_cmd = 0;
  731. break;
  732. }
  733. /* Second command setting for large pages */
  734. if (mtd->writesize > PAGE_CHUNK_SIZE) {
  735. /*
  736. * Multiple page write uses the 'extended command'
  737. * field. This can be used to issue a command dispatch
  738. * or a naked-write depending on the current stage.
  739. */
  740. info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
  741. | NDCB0_LEN_OVRD
  742. | NDCB0_EXT_CMD_TYPE(ext_cmd_type);
  743. info->ndcb3 = info->chunk_size +
  744. info->oob_size;
  745. /*
  746. * This is the command dispatch that completes a chunked
  747. * page program operation.
  748. */
  749. if (info->data_size == 0) {
  750. info->ndcb0 = NDCB0_CMD_TYPE(0x1)
  751. | NDCB0_EXT_CMD_TYPE(ext_cmd_type)
  752. | command;
  753. info->ndcb1 = 0;
  754. info->ndcb2 = 0;
  755. info->ndcb3 = 0;
  756. }
  757. } else {
  758. info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
  759. | NDCB0_AUTO_RS
  760. | NDCB0_ST_ROW_EN
  761. | NDCB0_DBC
  762. | (NAND_CMD_PAGEPROG << 8)
  763. | NAND_CMD_SEQIN
  764. | addr_cycle;
  765. }
  766. break;
  767. case NAND_CMD_PARAM:
  768. info->buf_count = 256;
  769. info->ndcb0 |= NDCB0_CMD_TYPE(0)
  770. | NDCB0_ADDR_CYC(1)
  771. | NDCB0_LEN_OVRD
  772. | command;
  773. info->ndcb1 = (column & 0xFF);
  774. info->ndcb3 = 256;
  775. info->data_size = 256;
  776. break;
  777. case NAND_CMD_READID:
  778. info->buf_count = host->read_id_bytes;
  779. info->ndcb0 |= NDCB0_CMD_TYPE(3)
  780. | NDCB0_ADDR_CYC(1)
  781. | command;
  782. info->ndcb1 = (column & 0xFF);
  783. info->data_size = 8;
  784. break;
  785. case NAND_CMD_STATUS:
  786. info->buf_count = 1;
  787. info->ndcb0 |= NDCB0_CMD_TYPE(4)
  788. | NDCB0_ADDR_CYC(1)
  789. | command;
  790. info->data_size = 8;
  791. break;
  792. case NAND_CMD_ERASE1:
  793. info->ndcb0 |= NDCB0_CMD_TYPE(2)
  794. | NDCB0_AUTO_RS
  795. | NDCB0_ADDR_CYC(3)
  796. | NDCB0_DBC
  797. | (NAND_CMD_ERASE2 << 8)
  798. | NAND_CMD_ERASE1;
  799. info->ndcb1 = page_addr;
  800. info->ndcb2 = 0;
  801. break;
  802. case NAND_CMD_RESET:
  803. info->ndcb0 |= NDCB0_CMD_TYPE(5)
  804. | command;
  805. break;
  806. case NAND_CMD_ERASE2:
  807. exec_cmd = 0;
  808. break;
  809. default:
  810. exec_cmd = 0;
  811. dev_err(&info->pdev->dev, "non-supported command %x\n",
  812. command);
  813. break;
  814. }
  815. return exec_cmd;
  816. }
  817. static void nand_cmdfunc(struct mtd_info *mtd, unsigned command,
  818. int column, int page_addr)
  819. {
  820. struct pxa3xx_nand_host *host = mtd->priv;
  821. struct pxa3xx_nand_info *info = host->info_data;
  822. int ret, exec_cmd;
  823. /*
  824. * if this is a x16 device ,then convert the input
  825. * "byte" address into a "word" address appropriate
  826. * for indexing a word-oriented device
  827. */
  828. if (info->reg_ndcr & NDCR_DWIDTH_M)
  829. column /= 2;
  830. /*
  831. * There may be different NAND chip hooked to
  832. * different chip select, so check whether
  833. * chip select has been changed, if yes, reset the timing
  834. */
  835. if (info->cs != host->cs) {
  836. info->cs = host->cs;
  837. nand_writel(info, NDTR0CS0, info->ndtr0cs0);
  838. nand_writel(info, NDTR1CS0, info->ndtr1cs0);
  839. }
  840. prepare_start_command(info, command);
  841. info->state = STATE_PREPARED;
  842. exec_cmd = prepare_set_command(info, command, 0, column, page_addr);
  843. if (exec_cmd) {
  844. init_completion(&info->cmd_complete);
  845. init_completion(&info->dev_ready);
  846. info->need_wait = 1;
  847. pxa3xx_nand_start(info);
  848. ret = wait_for_completion_timeout(&info->cmd_complete,
  849. CHIP_DELAY_TIMEOUT);
  850. if (!ret) {
  851. dev_err(&info->pdev->dev, "Wait time out!!!\n");
  852. /* Stop State Machine for next command cycle */
  853. pxa3xx_nand_stop(info);
  854. }
  855. }
  856. info->state = STATE_IDLE;
  857. }
  858. static void nand_cmdfunc_extended(struct mtd_info *mtd,
  859. const unsigned command,
  860. int column, int page_addr)
  861. {
  862. struct pxa3xx_nand_host *host = mtd->priv;
  863. struct pxa3xx_nand_info *info = host->info_data;
  864. int ret, exec_cmd, ext_cmd_type;
  865. /*
  866. * if this is a x16 device then convert the input
  867. * "byte" address into a "word" address appropriate
  868. * for indexing a word-oriented device
  869. */
  870. if (info->reg_ndcr & NDCR_DWIDTH_M)
  871. column /= 2;
  872. /*
  873. * There may be different NAND chip hooked to
  874. * different chip select, so check whether
  875. * chip select has been changed, if yes, reset the timing
  876. */
  877. if (info->cs != host->cs) {
  878. info->cs = host->cs;
  879. nand_writel(info, NDTR0CS0, info->ndtr0cs0);
  880. nand_writel(info, NDTR1CS0, info->ndtr1cs0);
  881. }
  882. /* Select the extended command for the first command */
  883. switch (command) {
  884. case NAND_CMD_READ0:
  885. case NAND_CMD_READOOB:
  886. ext_cmd_type = EXT_CMD_TYPE_MONO;
  887. break;
  888. case NAND_CMD_SEQIN:
  889. ext_cmd_type = EXT_CMD_TYPE_DISPATCH;
  890. break;
  891. case NAND_CMD_PAGEPROG:
  892. ext_cmd_type = EXT_CMD_TYPE_NAKED_RW;
  893. break;
  894. default:
  895. ext_cmd_type = 0;
  896. break;
  897. }
  898. prepare_start_command(info, command);
  899. /*
  900. * Prepare the "is ready" completion before starting a command
  901. * transaction sequence. If the command is not executed the
  902. * completion will be completed, see below.
  903. *
  904. * We can do that inside the loop because the command variable
  905. * is invariant and thus so is the exec_cmd.
  906. */
  907. info->need_wait = 1;
  908. init_completion(&info->dev_ready);
  909. do {
  910. info->state = STATE_PREPARED;
  911. exec_cmd = prepare_set_command(info, command, ext_cmd_type,
  912. column, page_addr);
  913. if (!exec_cmd) {
  914. info->need_wait = 0;
  915. complete(&info->dev_ready);
  916. break;
  917. }
  918. init_completion(&info->cmd_complete);
  919. pxa3xx_nand_start(info);
  920. ret = wait_for_completion_timeout(&info->cmd_complete,
  921. CHIP_DELAY_TIMEOUT);
  922. if (!ret) {
  923. dev_err(&info->pdev->dev, "Wait time out!!!\n");
  924. /* Stop State Machine for next command cycle */
  925. pxa3xx_nand_stop(info);
  926. break;
  927. }
  928. /* Check if the sequence is complete */
  929. if (info->data_size == 0 && command != NAND_CMD_PAGEPROG)
  930. break;
  931. /*
  932. * After a splitted program command sequence has issued
  933. * the command dispatch, the command sequence is complete.
  934. */
  935. if (info->data_size == 0 &&
  936. command == NAND_CMD_PAGEPROG &&
  937. ext_cmd_type == EXT_CMD_TYPE_DISPATCH)
  938. break;
  939. if (command == NAND_CMD_READ0 || command == NAND_CMD_READOOB) {
  940. /* Last read: issue a 'last naked read' */
  941. if (info->data_size == info->chunk_size)
  942. ext_cmd_type = EXT_CMD_TYPE_LAST_RW;
  943. else
  944. ext_cmd_type = EXT_CMD_TYPE_NAKED_RW;
  945. /*
  946. * If a splitted program command has no more data to transfer,
  947. * the command dispatch must be issued to complete.
  948. */
  949. } else if (command == NAND_CMD_PAGEPROG &&
  950. info->data_size == 0) {
  951. ext_cmd_type = EXT_CMD_TYPE_DISPATCH;
  952. }
  953. } while (1);
  954. info->state = STATE_IDLE;
  955. }
  956. static int pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd,
  957. struct nand_chip *chip, const uint8_t *buf, int oob_required)
  958. {
  959. chip->write_buf(mtd, buf, mtd->writesize);
  960. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  961. return 0;
  962. }
  963. static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd,
  964. struct nand_chip *chip, uint8_t *buf, int oob_required,
  965. int page)
  966. {
  967. struct pxa3xx_nand_host *host = mtd->priv;
  968. struct pxa3xx_nand_info *info = host->info_data;
  969. chip->read_buf(mtd, buf, mtd->writesize);
  970. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  971. if (info->retcode == ERR_CORERR && info->use_ecc) {
  972. mtd->ecc_stats.corrected += info->ecc_err_cnt;
  973. } else if (info->retcode == ERR_UNCORERR) {
  974. /*
  975. * for blank page (all 0xff), HW will calculate its ECC as
  976. * 0, which is different from the ECC information within
  977. * OOB, ignore such uncorrectable errors
  978. */
  979. if (is_buf_blank(buf, mtd->writesize))
  980. info->retcode = ERR_NONE;
  981. else
  982. mtd->ecc_stats.failed++;
  983. }
  984. return info->max_bitflips;
  985. }
  986. static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
  987. {
  988. struct pxa3xx_nand_host *host = mtd->priv;
  989. struct pxa3xx_nand_info *info = host->info_data;
  990. char retval = 0xFF;
  991. if (info->buf_start < info->buf_count)
  992. /* Has just send a new command? */
  993. retval = info->data_buff[info->buf_start++];
  994. return retval;
  995. }
  996. static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
  997. {
  998. struct pxa3xx_nand_host *host = mtd->priv;
  999. struct pxa3xx_nand_info *info = host->info_data;
  1000. u16 retval = 0xFFFF;
  1001. if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
  1002. retval = *((u16 *)(info->data_buff+info->buf_start));
  1003. info->buf_start += 2;
  1004. }
  1005. return retval;
  1006. }
  1007. static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  1008. {
  1009. struct pxa3xx_nand_host *host = mtd->priv;
  1010. struct pxa3xx_nand_info *info = host->info_data;
  1011. int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
  1012. memcpy(buf, info->data_buff + info->buf_start, real_len);
  1013. info->buf_start += real_len;
  1014. }
  1015. static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
  1016. const uint8_t *buf, int len)
  1017. {
  1018. struct pxa3xx_nand_host *host = mtd->priv;
  1019. struct pxa3xx_nand_info *info = host->info_data;
  1020. int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
  1021. memcpy(info->data_buff + info->buf_start, buf, real_len);
  1022. info->buf_start += real_len;
  1023. }
  1024. static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
  1025. {
  1026. return;
  1027. }
  1028. static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
  1029. {
  1030. struct pxa3xx_nand_host *host = mtd->priv;
  1031. struct pxa3xx_nand_info *info = host->info_data;
  1032. int ret;
  1033. if (info->need_wait) {
  1034. ret = wait_for_completion_timeout(&info->dev_ready,
  1035. CHIP_DELAY_TIMEOUT);
  1036. info->need_wait = 0;
  1037. if (!ret) {
  1038. dev_err(&info->pdev->dev, "Ready time out!!!\n");
  1039. return NAND_STATUS_FAIL;
  1040. }
  1041. }
  1042. /* pxa3xx_nand_send_command has waited for command complete */
  1043. if (this->state == FL_WRITING || this->state == FL_ERASING) {
  1044. if (info->retcode == ERR_NONE)
  1045. return 0;
  1046. else
  1047. return NAND_STATUS_FAIL;
  1048. }
  1049. return NAND_STATUS_READY;
  1050. }
  1051. static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info,
  1052. const struct pxa3xx_nand_flash *f)
  1053. {
  1054. struct platform_device *pdev = info->pdev;
  1055. struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
  1056. struct pxa3xx_nand_host *host = info->host[info->cs];
  1057. uint32_t ndcr = 0x0; /* enable all interrupts */
  1058. if (f->page_size != 2048 && f->page_size != 512) {
  1059. dev_err(&pdev->dev, "Current only support 2048 and 512 size\n");
  1060. return -EINVAL;
  1061. }
  1062. if (f->flash_width != 16 && f->flash_width != 8) {
  1063. dev_err(&pdev->dev, "Only support 8bit and 16 bit!\n");
  1064. return -EINVAL;
  1065. }
  1066. /* calculate flash information */
  1067. host->read_id_bytes = (f->page_size == 2048) ? 4 : 2;
  1068. /* calculate addressing information */
  1069. host->col_addr_cycles = (f->page_size == 2048) ? 2 : 1;
  1070. if (f->num_blocks * f->page_per_block > 65536)
  1071. host->row_addr_cycles = 3;
  1072. else
  1073. host->row_addr_cycles = 2;
  1074. ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
  1075. ndcr |= (host->col_addr_cycles == 2) ? NDCR_RA_START : 0;
  1076. ndcr |= (f->page_per_block == 64) ? NDCR_PG_PER_BLK : 0;
  1077. ndcr |= (f->page_size == 2048) ? NDCR_PAGE_SZ : 0;
  1078. ndcr |= (f->flash_width == 16) ? NDCR_DWIDTH_M : 0;
  1079. ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0;
  1080. ndcr |= NDCR_RD_ID_CNT(host->read_id_bytes);
  1081. ndcr |= NDCR_SPARE_EN; /* enable spare by default */
  1082. info->reg_ndcr = ndcr;
  1083. pxa3xx_nand_set_timing(host, f->timing);
  1084. return 0;
  1085. }
  1086. static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
  1087. {
  1088. /*
  1089. * We set 0 by hard coding here, for we don't support keep_config
  1090. * when there is more than one chip attached to the controller
  1091. */
  1092. struct pxa3xx_nand_host *host = info->host[0];
  1093. uint32_t ndcr = nand_readl(info, NDCR);
  1094. if (ndcr & NDCR_PAGE_SZ) {
  1095. /* Controller's FIFO size */
  1096. info->chunk_size = 2048;
  1097. host->read_id_bytes = 4;
  1098. } else {
  1099. info->chunk_size = 512;
  1100. host->read_id_bytes = 2;
  1101. }
  1102. /* Set an initial chunk size */
  1103. info->reg_ndcr = ndcr & ~NDCR_INT_MASK;
  1104. info->ndtr0cs0 = nand_readl(info, NDTR0CS0);
  1105. info->ndtr1cs0 = nand_readl(info, NDTR1CS0);
  1106. return 0;
  1107. }
  1108. #ifdef ARCH_HAS_DMA
  1109. static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
  1110. {
  1111. struct platform_device *pdev = info->pdev;
  1112. int data_desc_offset = info->buf_size - sizeof(struct pxa_dma_desc);
  1113. if (use_dma == 0) {
  1114. info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
  1115. if (info->data_buff == NULL)
  1116. return -ENOMEM;
  1117. return 0;
  1118. }
  1119. info->data_buff = dma_alloc_coherent(&pdev->dev, info->buf_size,
  1120. &info->data_buff_phys, GFP_KERNEL);
  1121. if (info->data_buff == NULL) {
  1122. dev_err(&pdev->dev, "failed to allocate dma buffer\n");
  1123. return -ENOMEM;
  1124. }
  1125. info->data_desc = (void *)info->data_buff + data_desc_offset;
  1126. info->data_desc_addr = info->data_buff_phys + data_desc_offset;
  1127. info->data_dma_ch = pxa_request_dma("nand-data", DMA_PRIO_LOW,
  1128. pxa3xx_nand_data_dma_irq, info);
  1129. if (info->data_dma_ch < 0) {
  1130. dev_err(&pdev->dev, "failed to request data dma\n");
  1131. dma_free_coherent(&pdev->dev, info->buf_size,
  1132. info->data_buff, info->data_buff_phys);
  1133. return info->data_dma_ch;
  1134. }
  1135. /*
  1136. * Now that DMA buffers are allocated we turn on
  1137. * DMA proper for I/O operations.
  1138. */
  1139. info->use_dma = 1;
  1140. return 0;
  1141. }
  1142. static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
  1143. {
  1144. struct platform_device *pdev = info->pdev;
  1145. if (info->use_dma) {
  1146. pxa_free_dma(info->data_dma_ch);
  1147. dma_free_coherent(&pdev->dev, info->buf_size,
  1148. info->data_buff, info->data_buff_phys);
  1149. } else {
  1150. kfree(info->data_buff);
  1151. }
  1152. }
  1153. #else
  1154. static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
  1155. {
  1156. info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
  1157. if (info->data_buff == NULL)
  1158. return -ENOMEM;
  1159. return 0;
  1160. }
  1161. static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
  1162. {
  1163. kfree(info->data_buff);
  1164. }
  1165. #endif
  1166. static int pxa3xx_nand_sensing(struct pxa3xx_nand_info *info)
  1167. {
  1168. struct mtd_info *mtd;
  1169. struct nand_chip *chip;
  1170. int ret;
  1171. mtd = info->host[info->cs]->mtd;
  1172. chip = mtd->priv;
  1173. /* use the common timing to make a try */
  1174. ret = pxa3xx_nand_config_flash(info, &builtin_flash_types[0]);
  1175. if (ret)
  1176. return ret;
  1177. chip->cmdfunc(mtd, NAND_CMD_RESET, 0, 0);
  1178. ret = chip->waitfunc(mtd, chip);
  1179. if (ret & NAND_STATUS_FAIL)
  1180. return -ENODEV;
  1181. return 0;
  1182. }
  1183. static int pxa_ecc_init(struct pxa3xx_nand_info *info,
  1184. struct nand_ecc_ctrl *ecc,
  1185. int strength, int ecc_stepsize, int page_size)
  1186. {
  1187. if (strength == 1 && ecc_stepsize == 512 && page_size == 2048) {
  1188. info->chunk_size = 2048;
  1189. info->spare_size = 40;
  1190. info->ecc_size = 24;
  1191. ecc->mode = NAND_ECC_HW;
  1192. ecc->size = 512;
  1193. ecc->strength = 1;
  1194. } else if (strength == 1 && ecc_stepsize == 512 && page_size == 512) {
  1195. info->chunk_size = 512;
  1196. info->spare_size = 8;
  1197. info->ecc_size = 8;
  1198. ecc->mode = NAND_ECC_HW;
  1199. ecc->size = 512;
  1200. ecc->strength = 1;
  1201. /*
  1202. * Required ECC: 4-bit correction per 512 bytes
  1203. * Select: 16-bit correction per 2048 bytes
  1204. */
  1205. } else if (strength == 4 && ecc_stepsize == 512 && page_size == 2048) {
  1206. info->ecc_bch = 1;
  1207. info->chunk_size = 2048;
  1208. info->spare_size = 32;
  1209. info->ecc_size = 32;
  1210. ecc->mode = NAND_ECC_HW;
  1211. ecc->size = info->chunk_size;
  1212. ecc->layout = &ecc_layout_2KB_bch4bit;
  1213. ecc->strength = 16;
  1214. } else if (strength == 4 && ecc_stepsize == 512 && page_size == 4096) {
  1215. info->ecc_bch = 1;
  1216. info->chunk_size = 2048;
  1217. info->spare_size = 32;
  1218. info->ecc_size = 32;
  1219. ecc->mode = NAND_ECC_HW;
  1220. ecc->size = info->chunk_size;
  1221. ecc->layout = &ecc_layout_4KB_bch4bit;
  1222. ecc->strength = 16;
  1223. /*
  1224. * Required ECC: 8-bit correction per 512 bytes
  1225. * Select: 16-bit correction per 1024 bytes
  1226. */
  1227. } else if (strength == 8 && ecc_stepsize == 512 && page_size == 4096) {
  1228. info->ecc_bch = 1;
  1229. info->chunk_size = 1024;
  1230. info->spare_size = 0;
  1231. info->ecc_size = 32;
  1232. ecc->mode = NAND_ECC_HW;
  1233. ecc->size = info->chunk_size;
  1234. ecc->layout = &ecc_layout_4KB_bch8bit;
  1235. ecc->strength = 16;
  1236. } else {
  1237. dev_err(&info->pdev->dev,
  1238. "ECC strength %d at page size %d is not supported\n",
  1239. strength, page_size);
  1240. return -ENODEV;
  1241. }
  1242. dev_info(&info->pdev->dev, "ECC strength %d, ECC step size %d\n",
  1243. ecc->strength, ecc->size);
  1244. return 0;
  1245. }
  1246. static int pxa3xx_nand_scan(struct mtd_info *mtd)
  1247. {
  1248. struct pxa3xx_nand_host *host = mtd->priv;
  1249. struct pxa3xx_nand_info *info = host->info_data;
  1250. struct platform_device *pdev = info->pdev;
  1251. struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
  1252. struct nand_flash_dev pxa3xx_flash_ids[2], *def = NULL;
  1253. const struct pxa3xx_nand_flash *f = NULL;
  1254. struct nand_chip *chip = mtd->priv;
  1255. uint32_t id = -1;
  1256. uint64_t chipsize;
  1257. int i, ret, num;
  1258. uint16_t ecc_strength, ecc_step;
  1259. if (pdata->keep_config && !pxa3xx_nand_detect_config(info))
  1260. goto KEEP_CONFIG;
  1261. ret = pxa3xx_nand_sensing(info);
  1262. if (ret) {
  1263. dev_info(&info->pdev->dev, "There is no chip on cs %d!\n",
  1264. info->cs);
  1265. return ret;
  1266. }
  1267. chip->cmdfunc(mtd, NAND_CMD_READID, 0, 0);
  1268. id = *((uint16_t *)(info->data_buff));
  1269. if (id != 0)
  1270. dev_info(&info->pdev->dev, "Detect a flash id %x\n", id);
  1271. else {
  1272. dev_warn(&info->pdev->dev,
  1273. "Read out ID 0, potential timing set wrong!!\n");
  1274. return -EINVAL;
  1275. }
  1276. num = ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1;
  1277. for (i = 0; i < num; i++) {
  1278. if (i < pdata->num_flash)
  1279. f = pdata->flash + i;
  1280. else
  1281. f = &builtin_flash_types[i - pdata->num_flash + 1];
  1282. /* find the chip in default list */
  1283. if (f->chip_id == id)
  1284. break;
  1285. }
  1286. if (i >= (ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1)) {
  1287. dev_err(&info->pdev->dev, "ERROR!! flash not defined!!!\n");
  1288. return -EINVAL;
  1289. }
  1290. ret = pxa3xx_nand_config_flash(info, f);
  1291. if (ret) {
  1292. dev_err(&info->pdev->dev, "ERROR! Configure failed\n");
  1293. return ret;
  1294. }
  1295. pxa3xx_flash_ids[0].name = f->name;
  1296. pxa3xx_flash_ids[0].dev_id = (f->chip_id >> 8) & 0xffff;
  1297. pxa3xx_flash_ids[0].pagesize = f->page_size;
  1298. chipsize = (uint64_t)f->num_blocks * f->page_per_block * f->page_size;
  1299. pxa3xx_flash_ids[0].chipsize = chipsize >> 20;
  1300. pxa3xx_flash_ids[0].erasesize = f->page_size * f->page_per_block;
  1301. if (f->flash_width == 16)
  1302. pxa3xx_flash_ids[0].options = NAND_BUSWIDTH_16;
  1303. pxa3xx_flash_ids[1].name = NULL;
  1304. def = pxa3xx_flash_ids;
  1305. KEEP_CONFIG:
  1306. if (info->reg_ndcr & NDCR_DWIDTH_M)
  1307. chip->options |= NAND_BUSWIDTH_16;
  1308. /* Device detection must be done with ECC disabled */
  1309. if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
  1310. nand_writel(info, NDECCCTRL, 0x0);
  1311. if (nand_scan_ident(mtd, 1, def))
  1312. return -ENODEV;
  1313. if (pdata->flash_bbt) {
  1314. /*
  1315. * We'll use a bad block table stored in-flash and don't
  1316. * allow writing the bad block marker to the flash.
  1317. */
  1318. chip->bbt_options |= NAND_BBT_USE_FLASH |
  1319. NAND_BBT_NO_OOB_BBM;
  1320. chip->bbt_td = &bbt_main_descr;
  1321. chip->bbt_md = &bbt_mirror_descr;
  1322. }
  1323. /*
  1324. * If the page size is bigger than the FIFO size, let's check
  1325. * we are given the right variant and then switch to the extended
  1326. * (aka splitted) command handling,
  1327. */
  1328. if (mtd->writesize > PAGE_CHUNK_SIZE) {
  1329. if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370) {
  1330. chip->cmdfunc = nand_cmdfunc_extended;
  1331. } else {
  1332. dev_err(&info->pdev->dev,
  1333. "unsupported page size on this variant\n");
  1334. return -ENODEV;
  1335. }
  1336. }
  1337. if (pdata->ecc_strength && pdata->ecc_step_size) {
  1338. ecc_strength = pdata->ecc_strength;
  1339. ecc_step = pdata->ecc_step_size;
  1340. } else {
  1341. ecc_strength = chip->ecc_strength_ds;
  1342. ecc_step = chip->ecc_step_ds;
  1343. }
  1344. /* Set default ECC strength requirements on non-ONFI devices */
  1345. if (ecc_strength < 1 && ecc_step < 1) {
  1346. ecc_strength = 1;
  1347. ecc_step = 512;
  1348. }
  1349. ret = pxa_ecc_init(info, &chip->ecc, ecc_strength,
  1350. ecc_step, mtd->writesize);
  1351. if (ret)
  1352. return ret;
  1353. /* calculate addressing information */
  1354. if (mtd->writesize >= 2048)
  1355. host->col_addr_cycles = 2;
  1356. else
  1357. host->col_addr_cycles = 1;
  1358. /* release the initial buffer */
  1359. kfree(info->data_buff);
  1360. /* allocate the real data + oob buffer */
  1361. info->buf_size = mtd->writesize + mtd->oobsize;
  1362. ret = pxa3xx_nand_init_buff(info);
  1363. if (ret)
  1364. return ret;
  1365. info->oob_buff = info->data_buff + mtd->writesize;
  1366. if ((mtd->size >> chip->page_shift) > 65536)
  1367. host->row_addr_cycles = 3;
  1368. else
  1369. host->row_addr_cycles = 2;
  1370. return nand_scan_tail(mtd);
  1371. }
  1372. static int alloc_nand_resource(struct platform_device *pdev)
  1373. {
  1374. struct pxa3xx_nand_platform_data *pdata;
  1375. struct pxa3xx_nand_info *info;
  1376. struct pxa3xx_nand_host *host;
  1377. struct nand_chip *chip = NULL;
  1378. struct mtd_info *mtd;
  1379. struct resource *r;
  1380. int ret, irq, cs;
  1381. pdata = dev_get_platdata(&pdev->dev);
  1382. info = devm_kzalloc(&pdev->dev, sizeof(*info) + (sizeof(*mtd) +
  1383. sizeof(*host)) * pdata->num_cs, GFP_KERNEL);
  1384. if (!info)
  1385. return -ENOMEM;
  1386. info->pdev = pdev;
  1387. info->variant = pxa3xx_nand_get_variant(pdev);
  1388. for (cs = 0; cs < pdata->num_cs; cs++) {
  1389. mtd = (struct mtd_info *)((unsigned int)&info[1] +
  1390. (sizeof(*mtd) + sizeof(*host)) * cs);
  1391. chip = (struct nand_chip *)(&mtd[1]);
  1392. host = (struct pxa3xx_nand_host *)chip;
  1393. info->host[cs] = host;
  1394. host->mtd = mtd;
  1395. host->cs = cs;
  1396. host->info_data = info;
  1397. mtd->priv = host;
  1398. mtd->owner = THIS_MODULE;
  1399. chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
  1400. chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
  1401. chip->controller = &info->controller;
  1402. chip->waitfunc = pxa3xx_nand_waitfunc;
  1403. chip->select_chip = pxa3xx_nand_select_chip;
  1404. chip->read_word = pxa3xx_nand_read_word;
  1405. chip->read_byte = pxa3xx_nand_read_byte;
  1406. chip->read_buf = pxa3xx_nand_read_buf;
  1407. chip->write_buf = pxa3xx_nand_write_buf;
  1408. chip->options |= NAND_NO_SUBPAGE_WRITE;
  1409. chip->cmdfunc = nand_cmdfunc;
  1410. }
  1411. spin_lock_init(&chip->controller->lock);
  1412. init_waitqueue_head(&chip->controller->wq);
  1413. info->clk = devm_clk_get(&pdev->dev, NULL);
  1414. if (IS_ERR(info->clk)) {
  1415. dev_err(&pdev->dev, "failed to get nand clock\n");
  1416. return PTR_ERR(info->clk);
  1417. }
  1418. ret = clk_prepare_enable(info->clk);
  1419. if (ret < 0)
  1420. return ret;
  1421. if (use_dma) {
  1422. /*
  1423. * This is a dirty hack to make this driver work from
  1424. * devicetree bindings. It can be removed once we have
  1425. * a prober DMA controller framework for DT.
  1426. */
  1427. if (pdev->dev.of_node &&
  1428. of_machine_is_compatible("marvell,pxa3xx")) {
  1429. info->drcmr_dat = 97;
  1430. info->drcmr_cmd = 99;
  1431. } else {
  1432. r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  1433. if (r == NULL) {
  1434. dev_err(&pdev->dev,
  1435. "no resource defined for data DMA\n");
  1436. ret = -ENXIO;
  1437. goto fail_disable_clk;
  1438. }
  1439. info->drcmr_dat = r->start;
  1440. r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
  1441. if (r == NULL) {
  1442. dev_err(&pdev->dev,
  1443. "no resource defined for cmd DMA\n");
  1444. ret = -ENXIO;
  1445. goto fail_disable_clk;
  1446. }
  1447. info->drcmr_cmd = r->start;
  1448. }
  1449. }
  1450. irq = platform_get_irq(pdev, 0);
  1451. if (irq < 0) {
  1452. dev_err(&pdev->dev, "no IRQ resource defined\n");
  1453. ret = -ENXIO;
  1454. goto fail_disable_clk;
  1455. }
  1456. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1457. info->mmio_base = devm_ioremap_resource(&pdev->dev, r);
  1458. if (IS_ERR(info->mmio_base)) {
  1459. ret = PTR_ERR(info->mmio_base);
  1460. goto fail_disable_clk;
  1461. }
  1462. info->mmio_phys = r->start;
  1463. /* Allocate a buffer to allow flash detection */
  1464. info->buf_size = INIT_BUFFER_SIZE;
  1465. info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
  1466. if (info->data_buff == NULL) {
  1467. ret = -ENOMEM;
  1468. goto fail_disable_clk;
  1469. }
  1470. /* initialize all interrupts to be disabled */
  1471. disable_int(info, NDSR_MASK);
  1472. ret = request_irq(irq, pxa3xx_nand_irq, 0, pdev->name, info);
  1473. if (ret < 0) {
  1474. dev_err(&pdev->dev, "failed to request IRQ\n");
  1475. goto fail_free_buf;
  1476. }
  1477. platform_set_drvdata(pdev, info);
  1478. return 0;
  1479. fail_free_buf:
  1480. free_irq(irq, info);
  1481. kfree(info->data_buff);
  1482. fail_disable_clk:
  1483. clk_disable_unprepare(info->clk);
  1484. return ret;
  1485. }
  1486. static int pxa3xx_nand_remove(struct platform_device *pdev)
  1487. {
  1488. struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
  1489. struct pxa3xx_nand_platform_data *pdata;
  1490. int irq, cs;
  1491. if (!info)
  1492. return 0;
  1493. pdata = dev_get_platdata(&pdev->dev);
  1494. irq = platform_get_irq(pdev, 0);
  1495. if (irq >= 0)
  1496. free_irq(irq, info);
  1497. pxa3xx_nand_free_buff(info);
  1498. clk_disable_unprepare(info->clk);
  1499. for (cs = 0; cs < pdata->num_cs; cs++)
  1500. nand_release(info->host[cs]->mtd);
  1501. return 0;
  1502. }
  1503. static int pxa3xx_nand_probe_dt(struct platform_device *pdev)
  1504. {
  1505. struct pxa3xx_nand_platform_data *pdata;
  1506. struct device_node *np = pdev->dev.of_node;
  1507. const struct of_device_id *of_id =
  1508. of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
  1509. if (!of_id)
  1510. return 0;
  1511. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  1512. if (!pdata)
  1513. return -ENOMEM;
  1514. if (of_get_property(np, "marvell,nand-enable-arbiter", NULL))
  1515. pdata->enable_arbiter = 1;
  1516. if (of_get_property(np, "marvell,nand-keep-config", NULL))
  1517. pdata->keep_config = 1;
  1518. of_property_read_u32(np, "num-cs", &pdata->num_cs);
  1519. pdata->flash_bbt = of_get_nand_on_flash_bbt(np);
  1520. pdata->ecc_strength = of_get_nand_ecc_strength(np);
  1521. if (pdata->ecc_strength < 0)
  1522. pdata->ecc_strength = 0;
  1523. pdata->ecc_step_size = of_get_nand_ecc_step_size(np);
  1524. if (pdata->ecc_step_size < 0)
  1525. pdata->ecc_step_size = 0;
  1526. pdev->dev.platform_data = pdata;
  1527. return 0;
  1528. }
  1529. static int pxa3xx_nand_probe(struct platform_device *pdev)
  1530. {
  1531. struct pxa3xx_nand_platform_data *pdata;
  1532. struct mtd_part_parser_data ppdata = {};
  1533. struct pxa3xx_nand_info *info;
  1534. int ret, cs, probe_success;
  1535. #ifndef ARCH_HAS_DMA
  1536. if (use_dma) {
  1537. use_dma = 0;
  1538. dev_warn(&pdev->dev,
  1539. "This platform can't do DMA on this device\n");
  1540. }
  1541. #endif
  1542. ret = pxa3xx_nand_probe_dt(pdev);
  1543. if (ret)
  1544. return ret;
  1545. pdata = dev_get_platdata(&pdev->dev);
  1546. if (!pdata) {
  1547. dev_err(&pdev->dev, "no platform data defined\n");
  1548. return -ENODEV;
  1549. }
  1550. ret = alloc_nand_resource(pdev);
  1551. if (ret) {
  1552. dev_err(&pdev->dev, "alloc nand resource failed\n");
  1553. return ret;
  1554. }
  1555. info = platform_get_drvdata(pdev);
  1556. probe_success = 0;
  1557. for (cs = 0; cs < pdata->num_cs; cs++) {
  1558. struct mtd_info *mtd = info->host[cs]->mtd;
  1559. /*
  1560. * The mtd name matches the one used in 'mtdparts' kernel
  1561. * parameter. This name cannot be changed or otherwise
  1562. * user's mtd partitions configuration would get broken.
  1563. */
  1564. mtd->name = "pxa3xx_nand-0";
  1565. info->cs = cs;
  1566. ret = pxa3xx_nand_scan(mtd);
  1567. if (ret) {
  1568. dev_warn(&pdev->dev, "failed to scan nand at cs %d\n",
  1569. cs);
  1570. continue;
  1571. }
  1572. ppdata.of_node = pdev->dev.of_node;
  1573. ret = mtd_device_parse_register(mtd, NULL,
  1574. &ppdata, pdata->parts[cs],
  1575. pdata->nr_parts[cs]);
  1576. if (!ret)
  1577. probe_success = 1;
  1578. }
  1579. if (!probe_success) {
  1580. pxa3xx_nand_remove(pdev);
  1581. return -ENODEV;
  1582. }
  1583. return 0;
  1584. }
  1585. #ifdef CONFIG_PM
  1586. static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state)
  1587. {
  1588. struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
  1589. struct pxa3xx_nand_platform_data *pdata;
  1590. struct mtd_info *mtd;
  1591. int cs;
  1592. pdata = dev_get_platdata(&pdev->dev);
  1593. if (info->state) {
  1594. dev_err(&pdev->dev, "driver busy, state = %d\n", info->state);
  1595. return -EAGAIN;
  1596. }
  1597. for (cs = 0; cs < pdata->num_cs; cs++) {
  1598. mtd = info->host[cs]->mtd;
  1599. mtd_suspend(mtd);
  1600. }
  1601. return 0;
  1602. }
  1603. static int pxa3xx_nand_resume(struct platform_device *pdev)
  1604. {
  1605. struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
  1606. struct pxa3xx_nand_platform_data *pdata;
  1607. struct mtd_info *mtd;
  1608. int cs;
  1609. pdata = dev_get_platdata(&pdev->dev);
  1610. /* We don't want to handle interrupt without calling mtd routine */
  1611. disable_int(info, NDCR_INT_MASK);
  1612. /*
  1613. * Directly set the chip select to a invalid value,
  1614. * then the driver would reset the timing according
  1615. * to current chip select at the beginning of cmdfunc
  1616. */
  1617. info->cs = 0xff;
  1618. /*
  1619. * As the spec says, the NDSR would be updated to 0x1800 when
  1620. * doing the nand_clk disable/enable.
  1621. * To prevent it damaging state machine of the driver, clear
  1622. * all status before resume
  1623. */
  1624. nand_writel(info, NDSR, NDSR_MASK);
  1625. for (cs = 0; cs < pdata->num_cs; cs++) {
  1626. mtd = info->host[cs]->mtd;
  1627. mtd_resume(mtd);
  1628. }
  1629. return 0;
  1630. }
  1631. #else
  1632. #define pxa3xx_nand_suspend NULL
  1633. #define pxa3xx_nand_resume NULL
  1634. #endif
  1635. static struct platform_driver pxa3xx_nand_driver = {
  1636. .driver = {
  1637. .name = "pxa3xx-nand",
  1638. .of_match_table = pxa3xx_nand_dt_ids,
  1639. },
  1640. .probe = pxa3xx_nand_probe,
  1641. .remove = pxa3xx_nand_remove,
  1642. .suspend = pxa3xx_nand_suspend,
  1643. .resume = pxa3xx_nand_resume,
  1644. };
  1645. module_platform_driver(pxa3xx_nand_driver);
  1646. MODULE_LICENSE("GPL");
  1647. MODULE_DESCRIPTION("PXA3xx NAND controller driver");