pxa3xx_nand.c 50 KB

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