pxa3xx_nand.c 53 KB

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