pxa3xx_nand.c 54 KB

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