pxa3xx_nand.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069
  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_READOOB:
  814. case NAND_CMD_PAGEPROG:
  815. info->use_ecc = 1;
  816. break;
  817. case NAND_CMD_PARAM:
  818. info->use_spare = 0;
  819. break;
  820. default:
  821. info->ndcb1 = 0;
  822. info->ndcb2 = 0;
  823. break;
  824. }
  825. /*
  826. * If we are about to issue a read command, or about to set
  827. * the write address, then clean the data buffer.
  828. */
  829. if (command == NAND_CMD_READ0 ||
  830. command == NAND_CMD_READOOB ||
  831. command == NAND_CMD_SEQIN) {
  832. info->buf_count = mtd->writesize + mtd->oobsize;
  833. memset(info->data_buff, 0xFF, info->buf_count);
  834. }
  835. }
  836. static int prepare_set_command(struct pxa3xx_nand_info *info, int command,
  837. int ext_cmd_type, uint16_t column, int page_addr)
  838. {
  839. int addr_cycle, exec_cmd;
  840. struct pxa3xx_nand_host *host;
  841. struct mtd_info *mtd;
  842. host = info->host[info->cs];
  843. mtd = nand_to_mtd(&host->chip);
  844. addr_cycle = 0;
  845. exec_cmd = 1;
  846. if (info->cs != 0)
  847. info->ndcb0 = NDCB0_CSEL;
  848. else
  849. info->ndcb0 = 0;
  850. if (command == NAND_CMD_SEQIN)
  851. exec_cmd = 0;
  852. addr_cycle = NDCB0_ADDR_CYC(host->row_addr_cycles
  853. + host->col_addr_cycles);
  854. switch (command) {
  855. case NAND_CMD_READOOB:
  856. case NAND_CMD_READ0:
  857. info->buf_start = column;
  858. info->ndcb0 |= NDCB0_CMD_TYPE(0)
  859. | addr_cycle
  860. | NAND_CMD_READ0;
  861. if (command == NAND_CMD_READOOB)
  862. info->buf_start += mtd->writesize;
  863. if (info->cur_chunk < info->nfullchunks) {
  864. info->step_chunk_size = info->chunk_size;
  865. info->step_spare_size = info->spare_size;
  866. } else {
  867. info->step_chunk_size = info->last_chunk_size;
  868. info->step_spare_size = info->last_spare_size;
  869. }
  870. /*
  871. * Multiple page read needs an 'extended command type' field,
  872. * which is either naked-read or last-read according to the
  873. * state.
  874. */
  875. if (mtd->writesize == PAGE_CHUNK_SIZE) {
  876. info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8);
  877. } else if (mtd->writesize > PAGE_CHUNK_SIZE) {
  878. info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8)
  879. | NDCB0_LEN_OVRD
  880. | NDCB0_EXT_CMD_TYPE(ext_cmd_type);
  881. info->ndcb3 = info->step_chunk_size +
  882. info->step_spare_size;
  883. }
  884. set_command_address(info, mtd->writesize, column, page_addr);
  885. break;
  886. case NAND_CMD_SEQIN:
  887. info->buf_start = column;
  888. set_command_address(info, mtd->writesize, 0, page_addr);
  889. /*
  890. * Multiple page programming needs to execute the initial
  891. * SEQIN command that sets the page address.
  892. */
  893. if (mtd->writesize > PAGE_CHUNK_SIZE) {
  894. info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
  895. | NDCB0_EXT_CMD_TYPE(ext_cmd_type)
  896. | addr_cycle
  897. | command;
  898. exec_cmd = 1;
  899. }
  900. break;
  901. case NAND_CMD_PAGEPROG:
  902. if (is_buf_blank(info->data_buff,
  903. (mtd->writesize + mtd->oobsize))) {
  904. exec_cmd = 0;
  905. break;
  906. }
  907. if (info->cur_chunk < info->nfullchunks) {
  908. info->step_chunk_size = info->chunk_size;
  909. info->step_spare_size = info->spare_size;
  910. } else {
  911. info->step_chunk_size = info->last_chunk_size;
  912. info->step_spare_size = info->last_spare_size;
  913. }
  914. /* Second command setting for large pages */
  915. if (mtd->writesize > PAGE_CHUNK_SIZE) {
  916. /*
  917. * Multiple page write uses the 'extended command'
  918. * field. This can be used to issue a command dispatch
  919. * or a naked-write depending on the current stage.
  920. */
  921. info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
  922. | NDCB0_LEN_OVRD
  923. | NDCB0_EXT_CMD_TYPE(ext_cmd_type);
  924. info->ndcb3 = info->step_chunk_size +
  925. info->step_spare_size;
  926. /*
  927. * This is the command dispatch that completes a chunked
  928. * page program operation.
  929. */
  930. if (info->cur_chunk == info->ntotalchunks) {
  931. info->ndcb0 = NDCB0_CMD_TYPE(0x1)
  932. | NDCB0_EXT_CMD_TYPE(ext_cmd_type)
  933. | command;
  934. info->ndcb1 = 0;
  935. info->ndcb2 = 0;
  936. info->ndcb3 = 0;
  937. }
  938. } else {
  939. info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
  940. | NDCB0_AUTO_RS
  941. | NDCB0_ST_ROW_EN
  942. | NDCB0_DBC
  943. | (NAND_CMD_PAGEPROG << 8)
  944. | NAND_CMD_SEQIN
  945. | addr_cycle;
  946. }
  947. break;
  948. case NAND_CMD_PARAM:
  949. info->buf_count = INIT_BUFFER_SIZE;
  950. info->ndcb0 |= NDCB0_CMD_TYPE(0)
  951. | NDCB0_ADDR_CYC(1)
  952. | NDCB0_LEN_OVRD
  953. | command;
  954. info->ndcb1 = (column & 0xFF);
  955. info->ndcb3 = INIT_BUFFER_SIZE;
  956. info->step_chunk_size = INIT_BUFFER_SIZE;
  957. break;
  958. case NAND_CMD_READID:
  959. info->buf_count = READ_ID_BYTES;
  960. info->ndcb0 |= NDCB0_CMD_TYPE(3)
  961. | NDCB0_ADDR_CYC(1)
  962. | command;
  963. info->ndcb1 = (column & 0xFF);
  964. info->step_chunk_size = 8;
  965. break;
  966. case NAND_CMD_STATUS:
  967. info->buf_count = 1;
  968. info->ndcb0 |= NDCB0_CMD_TYPE(4)
  969. | NDCB0_ADDR_CYC(1)
  970. | command;
  971. info->step_chunk_size = 8;
  972. break;
  973. case NAND_CMD_ERASE1:
  974. info->ndcb0 |= NDCB0_CMD_TYPE(2)
  975. | NDCB0_AUTO_RS
  976. | NDCB0_ADDR_CYC(3)
  977. | NDCB0_DBC
  978. | (NAND_CMD_ERASE2 << 8)
  979. | NAND_CMD_ERASE1;
  980. info->ndcb1 = page_addr;
  981. info->ndcb2 = 0;
  982. break;
  983. case NAND_CMD_RESET:
  984. info->ndcb0 |= NDCB0_CMD_TYPE(5)
  985. | command;
  986. break;
  987. case NAND_CMD_ERASE2:
  988. exec_cmd = 0;
  989. break;
  990. default:
  991. exec_cmd = 0;
  992. dev_err(&info->pdev->dev, "non-supported command %x\n",
  993. command);
  994. break;
  995. }
  996. return exec_cmd;
  997. }
  998. static void nand_cmdfunc(struct mtd_info *mtd, unsigned command,
  999. int column, int page_addr)
  1000. {
  1001. struct nand_chip *chip = mtd_to_nand(mtd);
  1002. struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
  1003. struct pxa3xx_nand_info *info = host->info_data;
  1004. int exec_cmd;
  1005. /*
  1006. * if this is a x16 device ,then convert the input
  1007. * "byte" address into a "word" address appropriate
  1008. * for indexing a word-oriented device
  1009. */
  1010. if (info->reg_ndcr & NDCR_DWIDTH_M)
  1011. column /= 2;
  1012. /*
  1013. * There may be different NAND chip hooked to
  1014. * different chip select, so check whether
  1015. * chip select has been changed, if yes, reset the timing
  1016. */
  1017. if (info->cs != host->cs) {
  1018. info->cs = host->cs;
  1019. nand_writel(info, NDTR0CS0, info->ndtr0cs0);
  1020. nand_writel(info, NDTR1CS0, info->ndtr1cs0);
  1021. }
  1022. prepare_start_command(info, command);
  1023. info->state = STATE_PREPARED;
  1024. exec_cmd = prepare_set_command(info, command, 0, column, page_addr);
  1025. if (exec_cmd) {
  1026. init_completion(&info->cmd_complete);
  1027. init_completion(&info->dev_ready);
  1028. info->need_wait = 1;
  1029. pxa3xx_nand_start(info);
  1030. if (!wait_for_completion_timeout(&info->cmd_complete,
  1031. CHIP_DELAY_TIMEOUT)) {
  1032. dev_err(&info->pdev->dev, "Wait time out!!!\n");
  1033. /* Stop State Machine for next command cycle */
  1034. pxa3xx_nand_stop(info);
  1035. }
  1036. }
  1037. info->state = STATE_IDLE;
  1038. }
  1039. static void nand_cmdfunc_extended(struct mtd_info *mtd,
  1040. const unsigned command,
  1041. int column, int page_addr)
  1042. {
  1043. struct nand_chip *chip = mtd_to_nand(mtd);
  1044. struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
  1045. struct pxa3xx_nand_info *info = host->info_data;
  1046. int exec_cmd, ext_cmd_type;
  1047. /*
  1048. * if this is a x16 device then convert the input
  1049. * "byte" address into a "word" address appropriate
  1050. * for indexing a word-oriented device
  1051. */
  1052. if (info->reg_ndcr & NDCR_DWIDTH_M)
  1053. column /= 2;
  1054. /*
  1055. * There may be different NAND chip hooked to
  1056. * different chip select, so check whether
  1057. * chip select has been changed, if yes, reset the timing
  1058. */
  1059. if (info->cs != host->cs) {
  1060. info->cs = host->cs;
  1061. nand_writel(info, NDTR0CS0, info->ndtr0cs0);
  1062. nand_writel(info, NDTR1CS0, info->ndtr1cs0);
  1063. }
  1064. /* Select the extended command for the first command */
  1065. switch (command) {
  1066. case NAND_CMD_READ0:
  1067. case NAND_CMD_READOOB:
  1068. ext_cmd_type = EXT_CMD_TYPE_MONO;
  1069. break;
  1070. case NAND_CMD_SEQIN:
  1071. ext_cmd_type = EXT_CMD_TYPE_DISPATCH;
  1072. break;
  1073. case NAND_CMD_PAGEPROG:
  1074. ext_cmd_type = EXT_CMD_TYPE_NAKED_RW;
  1075. break;
  1076. default:
  1077. ext_cmd_type = 0;
  1078. break;
  1079. }
  1080. prepare_start_command(info, command);
  1081. /*
  1082. * Prepare the "is ready" completion before starting a command
  1083. * transaction sequence. If the command is not executed the
  1084. * completion will be completed, see below.
  1085. *
  1086. * We can do that inside the loop because the command variable
  1087. * is invariant and thus so is the exec_cmd.
  1088. */
  1089. info->need_wait = 1;
  1090. init_completion(&info->dev_ready);
  1091. do {
  1092. info->state = STATE_PREPARED;
  1093. exec_cmd = prepare_set_command(info, command, ext_cmd_type,
  1094. column, page_addr);
  1095. if (!exec_cmd) {
  1096. info->need_wait = 0;
  1097. complete(&info->dev_ready);
  1098. break;
  1099. }
  1100. init_completion(&info->cmd_complete);
  1101. pxa3xx_nand_start(info);
  1102. if (!wait_for_completion_timeout(&info->cmd_complete,
  1103. CHIP_DELAY_TIMEOUT)) {
  1104. dev_err(&info->pdev->dev, "Wait time out!!!\n");
  1105. /* Stop State Machine for next command cycle */
  1106. pxa3xx_nand_stop(info);
  1107. break;
  1108. }
  1109. /* Only a few commands need several steps */
  1110. if (command != NAND_CMD_PAGEPROG &&
  1111. command != NAND_CMD_READ0 &&
  1112. command != NAND_CMD_READOOB)
  1113. break;
  1114. info->cur_chunk++;
  1115. /* Check if the sequence is complete */
  1116. if (info->cur_chunk == info->ntotalchunks && command != NAND_CMD_PAGEPROG)
  1117. break;
  1118. /*
  1119. * After a splitted program command sequence has issued
  1120. * the command dispatch, the command sequence is complete.
  1121. */
  1122. if (info->cur_chunk == (info->ntotalchunks + 1) &&
  1123. command == NAND_CMD_PAGEPROG &&
  1124. ext_cmd_type == EXT_CMD_TYPE_DISPATCH)
  1125. break;
  1126. if (command == NAND_CMD_READ0 || command == NAND_CMD_READOOB) {
  1127. /* Last read: issue a 'last naked read' */
  1128. if (info->cur_chunk == info->ntotalchunks - 1)
  1129. ext_cmd_type = EXT_CMD_TYPE_LAST_RW;
  1130. else
  1131. ext_cmd_type = EXT_CMD_TYPE_NAKED_RW;
  1132. /*
  1133. * If a splitted program command has no more data to transfer,
  1134. * the command dispatch must be issued to complete.
  1135. */
  1136. } else if (command == NAND_CMD_PAGEPROG &&
  1137. info->cur_chunk == info->ntotalchunks) {
  1138. ext_cmd_type = EXT_CMD_TYPE_DISPATCH;
  1139. }
  1140. } while (1);
  1141. info->state = STATE_IDLE;
  1142. }
  1143. static int pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd,
  1144. struct nand_chip *chip, const uint8_t *buf, int oob_required,
  1145. int page)
  1146. {
  1147. chip->write_buf(mtd, buf, mtd->writesize);
  1148. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  1149. return 0;
  1150. }
  1151. static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd,
  1152. struct nand_chip *chip, uint8_t *buf, int oob_required,
  1153. int page)
  1154. {
  1155. struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
  1156. struct pxa3xx_nand_info *info = host->info_data;
  1157. chip->read_buf(mtd, buf, mtd->writesize);
  1158. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  1159. if (info->retcode == ERR_CORERR && info->use_ecc) {
  1160. mtd->ecc_stats.corrected += info->ecc_err_cnt;
  1161. } else if (info->retcode == ERR_UNCORERR) {
  1162. /*
  1163. * for blank page (all 0xff), HW will calculate its ECC as
  1164. * 0, which is different from the ECC information within
  1165. * OOB, ignore such uncorrectable errors
  1166. */
  1167. if (is_buf_blank(buf, mtd->writesize))
  1168. info->retcode = ERR_NONE;
  1169. else
  1170. mtd->ecc_stats.failed++;
  1171. }
  1172. return info->max_bitflips;
  1173. }
  1174. static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
  1175. {
  1176. struct nand_chip *chip = mtd_to_nand(mtd);
  1177. struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
  1178. struct pxa3xx_nand_info *info = host->info_data;
  1179. char retval = 0xFF;
  1180. if (info->buf_start < info->buf_count)
  1181. /* Has just send a new command? */
  1182. retval = info->data_buff[info->buf_start++];
  1183. return retval;
  1184. }
  1185. static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
  1186. {
  1187. struct nand_chip *chip = mtd_to_nand(mtd);
  1188. struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
  1189. struct pxa3xx_nand_info *info = host->info_data;
  1190. u16 retval = 0xFFFF;
  1191. if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
  1192. retval = *((u16 *)(info->data_buff+info->buf_start));
  1193. info->buf_start += 2;
  1194. }
  1195. return retval;
  1196. }
  1197. static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  1198. {
  1199. struct nand_chip *chip = mtd_to_nand(mtd);
  1200. struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
  1201. struct pxa3xx_nand_info *info = host->info_data;
  1202. int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
  1203. memcpy(buf, info->data_buff + info->buf_start, real_len);
  1204. info->buf_start += real_len;
  1205. }
  1206. static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
  1207. const uint8_t *buf, int len)
  1208. {
  1209. struct nand_chip *chip = mtd_to_nand(mtd);
  1210. struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
  1211. struct pxa3xx_nand_info *info = host->info_data;
  1212. int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
  1213. memcpy(info->data_buff + info->buf_start, buf, real_len);
  1214. info->buf_start += real_len;
  1215. }
  1216. static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
  1217. {
  1218. return;
  1219. }
  1220. static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
  1221. {
  1222. struct nand_chip *chip = mtd_to_nand(mtd);
  1223. struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
  1224. struct pxa3xx_nand_info *info = host->info_data;
  1225. if (info->need_wait) {
  1226. info->need_wait = 0;
  1227. if (!wait_for_completion_timeout(&info->dev_ready,
  1228. CHIP_DELAY_TIMEOUT)) {
  1229. dev_err(&info->pdev->dev, "Ready time out!!!\n");
  1230. return NAND_STATUS_FAIL;
  1231. }
  1232. }
  1233. /* pxa3xx_nand_send_command has waited for command complete */
  1234. if (this->state == FL_WRITING || this->state == FL_ERASING) {
  1235. if (info->retcode == ERR_NONE)
  1236. return 0;
  1237. else
  1238. return NAND_STATUS_FAIL;
  1239. }
  1240. return NAND_STATUS_READY;
  1241. }
  1242. static int pxa3xx_nand_config_ident(struct pxa3xx_nand_info *info)
  1243. {
  1244. struct pxa3xx_nand_host *host = info->host[info->cs];
  1245. struct platform_device *pdev = info->pdev;
  1246. struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
  1247. const struct nand_sdr_timings *timings;
  1248. /* Configure default flash values */
  1249. info->chunk_size = PAGE_CHUNK_SIZE;
  1250. info->reg_ndcr = 0x0; /* enable all interrupts */
  1251. info->reg_ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
  1252. info->reg_ndcr |= NDCR_RD_ID_CNT(READ_ID_BYTES);
  1253. info->reg_ndcr |= NDCR_SPARE_EN;
  1254. /* use the common timing to make a try */
  1255. timings = onfi_async_timing_mode_to_sdr_timings(0);
  1256. if (IS_ERR(timings))
  1257. return PTR_ERR(timings);
  1258. pxa3xx_nand_set_sdr_timing(host, timings);
  1259. return 0;
  1260. }
  1261. static void pxa3xx_nand_config_tail(struct pxa3xx_nand_info *info)
  1262. {
  1263. struct pxa3xx_nand_host *host = info->host[info->cs];
  1264. struct nand_chip *chip = &host->chip;
  1265. struct mtd_info *mtd = nand_to_mtd(chip);
  1266. info->reg_ndcr |= (host->col_addr_cycles == 2) ? NDCR_RA_START : 0;
  1267. info->reg_ndcr |= (chip->page_shift == 6) ? NDCR_PG_PER_BLK : 0;
  1268. info->reg_ndcr |= (mtd->writesize == 2048) ? NDCR_PAGE_SZ : 0;
  1269. }
  1270. static void pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
  1271. {
  1272. struct platform_device *pdev = info->pdev;
  1273. struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
  1274. uint32_t ndcr = nand_readl(info, NDCR);
  1275. /* Set an initial chunk size */
  1276. info->chunk_size = ndcr & NDCR_PAGE_SZ ? 2048 : 512;
  1277. info->reg_ndcr = ndcr &
  1278. ~(NDCR_INT_MASK | NDCR_ND_ARB_EN | NFCV1_NDCR_ARB_CNTL);
  1279. info->reg_ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
  1280. info->ndtr0cs0 = nand_readl(info, NDTR0CS0);
  1281. info->ndtr1cs0 = nand_readl(info, NDTR1CS0);
  1282. }
  1283. static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
  1284. {
  1285. struct platform_device *pdev = info->pdev;
  1286. struct dma_slave_config config;
  1287. dma_cap_mask_t mask;
  1288. struct pxad_param param;
  1289. int ret;
  1290. info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
  1291. if (info->data_buff == NULL)
  1292. return -ENOMEM;
  1293. if (use_dma == 0)
  1294. return 0;
  1295. ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  1296. if (ret)
  1297. return ret;
  1298. sg_init_one(&info->sg, info->data_buff, info->buf_size);
  1299. dma_cap_zero(mask);
  1300. dma_cap_set(DMA_SLAVE, mask);
  1301. param.prio = PXAD_PRIO_LOWEST;
  1302. param.drcmr = info->drcmr_dat;
  1303. info->dma_chan = dma_request_slave_channel_compat(mask, pxad_filter_fn,
  1304. &param, &pdev->dev,
  1305. "data");
  1306. if (!info->dma_chan) {
  1307. dev_err(&pdev->dev, "unable to request data dma channel\n");
  1308. return -ENODEV;
  1309. }
  1310. memset(&config, 0, sizeof(config));
  1311. config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1312. config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  1313. config.src_addr = info->mmio_phys + NDDB;
  1314. config.dst_addr = info->mmio_phys + NDDB;
  1315. config.src_maxburst = 32;
  1316. config.dst_maxburst = 32;
  1317. ret = dmaengine_slave_config(info->dma_chan, &config);
  1318. if (ret < 0) {
  1319. dev_err(&info->pdev->dev,
  1320. "dma channel configuration failed: %d\n",
  1321. ret);
  1322. return ret;
  1323. }
  1324. /*
  1325. * Now that DMA buffers are allocated we turn on
  1326. * DMA proper for I/O operations.
  1327. */
  1328. info->use_dma = 1;
  1329. return 0;
  1330. }
  1331. static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
  1332. {
  1333. if (info->use_dma) {
  1334. dmaengine_terminate_all(info->dma_chan);
  1335. dma_release_channel(info->dma_chan);
  1336. }
  1337. kfree(info->data_buff);
  1338. }
  1339. static int pxa_ecc_init(struct pxa3xx_nand_info *info,
  1340. struct mtd_info *mtd,
  1341. int strength, int ecc_stepsize, int page_size)
  1342. {
  1343. struct nand_chip *chip = mtd_to_nand(mtd);
  1344. struct nand_ecc_ctrl *ecc = &chip->ecc;
  1345. if (strength == 1 && ecc_stepsize == 512 && page_size == 2048) {
  1346. info->nfullchunks = 1;
  1347. info->ntotalchunks = 1;
  1348. info->chunk_size = 2048;
  1349. info->spare_size = 40;
  1350. info->ecc_size = 24;
  1351. ecc->mode = NAND_ECC_HW;
  1352. ecc->size = 512;
  1353. ecc->strength = 1;
  1354. } else if (strength == 1 && ecc_stepsize == 512 && page_size == 512) {
  1355. info->nfullchunks = 1;
  1356. info->ntotalchunks = 1;
  1357. info->chunk_size = 512;
  1358. info->spare_size = 8;
  1359. info->ecc_size = 8;
  1360. ecc->mode = NAND_ECC_HW;
  1361. ecc->size = 512;
  1362. ecc->strength = 1;
  1363. /*
  1364. * Required ECC: 4-bit correction per 512 bytes
  1365. * Select: 16-bit correction per 2048 bytes
  1366. */
  1367. } else if (strength == 4 && ecc_stepsize == 512 && page_size == 2048) {
  1368. info->ecc_bch = 1;
  1369. info->nfullchunks = 1;
  1370. info->ntotalchunks = 1;
  1371. info->chunk_size = 2048;
  1372. info->spare_size = 32;
  1373. info->ecc_size = 32;
  1374. ecc->mode = NAND_ECC_HW;
  1375. ecc->size = info->chunk_size;
  1376. mtd_set_ooblayout(mtd, &pxa3xx_ooblayout_ops);
  1377. ecc->strength = 16;
  1378. } else if (strength == 4 && ecc_stepsize == 512 && page_size == 4096) {
  1379. info->ecc_bch = 1;
  1380. info->nfullchunks = 2;
  1381. info->ntotalchunks = 2;
  1382. info->chunk_size = 2048;
  1383. info->spare_size = 32;
  1384. info->ecc_size = 32;
  1385. ecc->mode = NAND_ECC_HW;
  1386. ecc->size = info->chunk_size;
  1387. mtd_set_ooblayout(mtd, &pxa3xx_ooblayout_ops);
  1388. ecc->strength = 16;
  1389. /*
  1390. * Required ECC: 8-bit correction per 512 bytes
  1391. * Select: 16-bit correction per 1024 bytes
  1392. */
  1393. } else if (strength == 8 && ecc_stepsize == 512 && page_size == 4096) {
  1394. info->ecc_bch = 1;
  1395. info->nfullchunks = 4;
  1396. info->ntotalchunks = 5;
  1397. info->chunk_size = 1024;
  1398. info->spare_size = 0;
  1399. info->last_chunk_size = 0;
  1400. info->last_spare_size = 64;
  1401. info->ecc_size = 32;
  1402. ecc->mode = NAND_ECC_HW;
  1403. ecc->size = info->chunk_size;
  1404. mtd_set_ooblayout(mtd, &pxa3xx_ooblayout_ops);
  1405. ecc->strength = 16;
  1406. } else {
  1407. dev_err(&info->pdev->dev,
  1408. "ECC strength %d at page size %d is not supported\n",
  1409. strength, page_size);
  1410. return -ENODEV;
  1411. }
  1412. dev_info(&info->pdev->dev, "ECC strength %d, ECC step size %d\n",
  1413. ecc->strength, ecc->size);
  1414. return 0;
  1415. }
  1416. static int pxa3xx_nand_scan(struct mtd_info *mtd)
  1417. {
  1418. struct nand_chip *chip = mtd_to_nand(mtd);
  1419. struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
  1420. struct pxa3xx_nand_info *info = host->info_data;
  1421. struct platform_device *pdev = info->pdev;
  1422. struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
  1423. int ret;
  1424. uint16_t ecc_strength, ecc_step;
  1425. if (pdata->keep_config) {
  1426. pxa3xx_nand_detect_config(info);
  1427. } else {
  1428. ret = pxa3xx_nand_config_ident(info);
  1429. if (ret)
  1430. return ret;
  1431. }
  1432. if (info->reg_ndcr & NDCR_DWIDTH_M)
  1433. chip->options |= NAND_BUSWIDTH_16;
  1434. /* Device detection must be done with ECC disabled */
  1435. if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
  1436. nand_writel(info, NDECCCTRL, 0x0);
  1437. if (pdata->flash_bbt)
  1438. chip->bbt_options |= NAND_BBT_USE_FLASH;
  1439. chip->ecc.strength = pdata->ecc_strength;
  1440. chip->ecc.size = pdata->ecc_step_size;
  1441. if (nand_scan_ident(mtd, 1, NULL))
  1442. return -ENODEV;
  1443. if (!pdata->keep_config) {
  1444. ret = pxa3xx_nand_init(host);
  1445. if (ret) {
  1446. dev_err(&info->pdev->dev, "Failed to init nand: %d\n",
  1447. ret);
  1448. return ret;
  1449. }
  1450. }
  1451. if (chip->bbt_options & NAND_BBT_USE_FLASH) {
  1452. /*
  1453. * We'll use a bad block table stored in-flash and don't
  1454. * allow writing the bad block marker to the flash.
  1455. */
  1456. chip->bbt_options |= NAND_BBT_NO_OOB_BBM;
  1457. chip->bbt_td = &bbt_main_descr;
  1458. chip->bbt_md = &bbt_mirror_descr;
  1459. }
  1460. /*
  1461. * If the page size is bigger than the FIFO size, let's check
  1462. * we are given the right variant and then switch to the extended
  1463. * (aka splitted) command handling,
  1464. */
  1465. if (mtd->writesize > PAGE_CHUNK_SIZE) {
  1466. if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370) {
  1467. chip->cmdfunc = nand_cmdfunc_extended;
  1468. } else {
  1469. dev_err(&info->pdev->dev,
  1470. "unsupported page size on this variant\n");
  1471. return -ENODEV;
  1472. }
  1473. }
  1474. ecc_strength = chip->ecc.strength;
  1475. ecc_step = chip->ecc.size;
  1476. if (!ecc_strength || !ecc_step) {
  1477. ecc_strength = chip->ecc_strength_ds;
  1478. ecc_step = chip->ecc_step_ds;
  1479. }
  1480. /* Set default ECC strength requirements on non-ONFI devices */
  1481. if (ecc_strength < 1 && ecc_step < 1) {
  1482. ecc_strength = 1;
  1483. ecc_step = 512;
  1484. }
  1485. ret = pxa_ecc_init(info, mtd, ecc_strength,
  1486. ecc_step, mtd->writesize);
  1487. if (ret)
  1488. return ret;
  1489. /* calculate addressing information */
  1490. if (mtd->writesize >= 2048)
  1491. host->col_addr_cycles = 2;
  1492. else
  1493. host->col_addr_cycles = 1;
  1494. /* release the initial buffer */
  1495. kfree(info->data_buff);
  1496. /* allocate the real data + oob buffer */
  1497. info->buf_size = mtd->writesize + mtd->oobsize;
  1498. ret = pxa3xx_nand_init_buff(info);
  1499. if (ret)
  1500. return ret;
  1501. info->oob_buff = info->data_buff + mtd->writesize;
  1502. if ((mtd->size >> chip->page_shift) > 65536)
  1503. host->row_addr_cycles = 3;
  1504. else
  1505. host->row_addr_cycles = 2;
  1506. if (!pdata->keep_config)
  1507. pxa3xx_nand_config_tail(info);
  1508. return nand_scan_tail(mtd);
  1509. }
  1510. static int alloc_nand_resource(struct platform_device *pdev)
  1511. {
  1512. struct device_node *np = pdev->dev.of_node;
  1513. struct pxa3xx_nand_platform_data *pdata;
  1514. struct pxa3xx_nand_info *info;
  1515. struct pxa3xx_nand_host *host;
  1516. struct nand_chip *chip = NULL;
  1517. struct mtd_info *mtd;
  1518. struct resource *r;
  1519. int ret, irq, cs;
  1520. pdata = dev_get_platdata(&pdev->dev);
  1521. if (pdata->num_cs <= 0)
  1522. return -ENODEV;
  1523. info = devm_kzalloc(&pdev->dev,
  1524. sizeof(*info) + sizeof(*host) * pdata->num_cs,
  1525. GFP_KERNEL);
  1526. if (!info)
  1527. return -ENOMEM;
  1528. info->pdev = pdev;
  1529. info->variant = pxa3xx_nand_get_variant(pdev);
  1530. for (cs = 0; cs < pdata->num_cs; cs++) {
  1531. host = (void *)&info[1] + sizeof(*host) * cs;
  1532. chip = &host->chip;
  1533. nand_set_controller_data(chip, host);
  1534. mtd = nand_to_mtd(chip);
  1535. info->host[cs] = host;
  1536. host->cs = cs;
  1537. host->info_data = info;
  1538. mtd->dev.parent = &pdev->dev;
  1539. /* FIXME: all chips use the same device tree partitions */
  1540. nand_set_flash_node(chip, np);
  1541. nand_set_controller_data(chip, host);
  1542. chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
  1543. chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
  1544. chip->controller = &info->controller;
  1545. chip->waitfunc = pxa3xx_nand_waitfunc;
  1546. chip->select_chip = pxa3xx_nand_select_chip;
  1547. chip->read_word = pxa3xx_nand_read_word;
  1548. chip->read_byte = pxa3xx_nand_read_byte;
  1549. chip->read_buf = pxa3xx_nand_read_buf;
  1550. chip->write_buf = pxa3xx_nand_write_buf;
  1551. chip->options |= NAND_NO_SUBPAGE_WRITE;
  1552. chip->cmdfunc = nand_cmdfunc;
  1553. }
  1554. nand_hw_control_init(chip->controller);
  1555. info->clk = devm_clk_get(&pdev->dev, NULL);
  1556. if (IS_ERR(info->clk)) {
  1557. dev_err(&pdev->dev, "failed to get nand clock\n");
  1558. return PTR_ERR(info->clk);
  1559. }
  1560. ret = clk_prepare_enable(info->clk);
  1561. if (ret < 0)
  1562. return ret;
  1563. if (!np && use_dma) {
  1564. r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  1565. if (r == NULL) {
  1566. dev_err(&pdev->dev,
  1567. "no resource defined for data DMA\n");
  1568. ret = -ENXIO;
  1569. goto fail_disable_clk;
  1570. }
  1571. info->drcmr_dat = r->start;
  1572. }
  1573. irq = platform_get_irq(pdev, 0);
  1574. if (irq < 0) {
  1575. dev_err(&pdev->dev, "no IRQ resource defined\n");
  1576. ret = -ENXIO;
  1577. goto fail_disable_clk;
  1578. }
  1579. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1580. info->mmio_base = devm_ioremap_resource(&pdev->dev, r);
  1581. if (IS_ERR(info->mmio_base)) {
  1582. ret = PTR_ERR(info->mmio_base);
  1583. goto fail_disable_clk;
  1584. }
  1585. info->mmio_phys = r->start;
  1586. /* Allocate a buffer to allow flash detection */
  1587. info->buf_size = INIT_BUFFER_SIZE;
  1588. info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
  1589. if (info->data_buff == NULL) {
  1590. ret = -ENOMEM;
  1591. goto fail_disable_clk;
  1592. }
  1593. /* initialize all interrupts to be disabled */
  1594. disable_int(info, NDSR_MASK);
  1595. ret = request_threaded_irq(irq, pxa3xx_nand_irq,
  1596. pxa3xx_nand_irq_thread, IRQF_ONESHOT,
  1597. pdev->name, info);
  1598. if (ret < 0) {
  1599. dev_err(&pdev->dev, "failed to request IRQ\n");
  1600. goto fail_free_buf;
  1601. }
  1602. platform_set_drvdata(pdev, info);
  1603. return 0;
  1604. fail_free_buf:
  1605. free_irq(irq, info);
  1606. kfree(info->data_buff);
  1607. fail_disable_clk:
  1608. clk_disable_unprepare(info->clk);
  1609. return ret;
  1610. }
  1611. static int pxa3xx_nand_remove(struct platform_device *pdev)
  1612. {
  1613. struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
  1614. struct pxa3xx_nand_platform_data *pdata;
  1615. int irq, cs;
  1616. if (!info)
  1617. return 0;
  1618. pdata = dev_get_platdata(&pdev->dev);
  1619. irq = platform_get_irq(pdev, 0);
  1620. if (irq >= 0)
  1621. free_irq(irq, info);
  1622. pxa3xx_nand_free_buff(info);
  1623. /*
  1624. * In the pxa3xx case, the DFI bus is shared between the SMC and NFC.
  1625. * In order to prevent a lockup of the system bus, the DFI bus
  1626. * arbitration is granted to SMC upon driver removal. This is done by
  1627. * setting the x_ARB_CNTL bit, which also prevents the NAND to have
  1628. * access to the bus anymore.
  1629. */
  1630. nand_writel(info, NDCR,
  1631. (nand_readl(info, NDCR) & ~NDCR_ND_ARB_EN) |
  1632. NFCV1_NDCR_ARB_CNTL);
  1633. clk_disable_unprepare(info->clk);
  1634. for (cs = 0; cs < pdata->num_cs; cs++)
  1635. nand_release(nand_to_mtd(&info->host[cs]->chip));
  1636. return 0;
  1637. }
  1638. static int pxa3xx_nand_probe_dt(struct platform_device *pdev)
  1639. {
  1640. struct pxa3xx_nand_platform_data *pdata;
  1641. struct device_node *np = pdev->dev.of_node;
  1642. const struct of_device_id *of_id =
  1643. of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
  1644. if (!of_id)
  1645. return 0;
  1646. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  1647. if (!pdata)
  1648. return -ENOMEM;
  1649. if (of_get_property(np, "marvell,nand-enable-arbiter", NULL))
  1650. pdata->enable_arbiter = 1;
  1651. if (of_get_property(np, "marvell,nand-keep-config", NULL))
  1652. pdata->keep_config = 1;
  1653. of_property_read_u32(np, "num-cs", &pdata->num_cs);
  1654. pdev->dev.platform_data = pdata;
  1655. return 0;
  1656. }
  1657. static int pxa3xx_nand_probe(struct platform_device *pdev)
  1658. {
  1659. struct pxa3xx_nand_platform_data *pdata;
  1660. struct pxa3xx_nand_info *info;
  1661. int ret, cs, probe_success, dma_available;
  1662. dma_available = IS_ENABLED(CONFIG_ARM) &&
  1663. (IS_ENABLED(CONFIG_ARCH_PXA) || IS_ENABLED(CONFIG_ARCH_MMP));
  1664. if (use_dma && !dma_available) {
  1665. use_dma = 0;
  1666. dev_warn(&pdev->dev,
  1667. "This platform can't do DMA on this device\n");
  1668. }
  1669. ret = pxa3xx_nand_probe_dt(pdev);
  1670. if (ret)
  1671. return ret;
  1672. pdata = dev_get_platdata(&pdev->dev);
  1673. if (!pdata) {
  1674. dev_err(&pdev->dev, "no platform data defined\n");
  1675. return -ENODEV;
  1676. }
  1677. ret = alloc_nand_resource(pdev);
  1678. if (ret) {
  1679. dev_err(&pdev->dev, "alloc nand resource failed\n");
  1680. return ret;
  1681. }
  1682. info = platform_get_drvdata(pdev);
  1683. probe_success = 0;
  1684. for (cs = 0; cs < pdata->num_cs; cs++) {
  1685. struct mtd_info *mtd = nand_to_mtd(&info->host[cs]->chip);
  1686. /*
  1687. * The mtd name matches the one used in 'mtdparts' kernel
  1688. * parameter. This name cannot be changed or otherwise
  1689. * user's mtd partitions configuration would get broken.
  1690. */
  1691. mtd->name = "pxa3xx_nand-0";
  1692. info->cs = cs;
  1693. ret = pxa3xx_nand_scan(mtd);
  1694. if (ret) {
  1695. dev_warn(&pdev->dev, "failed to scan nand at cs %d\n",
  1696. cs);
  1697. continue;
  1698. }
  1699. ret = mtd_device_register(mtd, pdata->parts[cs],
  1700. pdata->nr_parts[cs]);
  1701. if (!ret)
  1702. probe_success = 1;
  1703. }
  1704. if (!probe_success) {
  1705. pxa3xx_nand_remove(pdev);
  1706. return -ENODEV;
  1707. }
  1708. return 0;
  1709. }
  1710. #ifdef CONFIG_PM
  1711. static int pxa3xx_nand_suspend(struct device *dev)
  1712. {
  1713. struct pxa3xx_nand_info *info = dev_get_drvdata(dev);
  1714. if (info->state) {
  1715. dev_err(dev, "driver busy, state = %d\n", info->state);
  1716. return -EAGAIN;
  1717. }
  1718. clk_disable(info->clk);
  1719. return 0;
  1720. }
  1721. static int pxa3xx_nand_resume(struct device *dev)
  1722. {
  1723. struct pxa3xx_nand_info *info = dev_get_drvdata(dev);
  1724. int ret;
  1725. ret = clk_enable(info->clk);
  1726. if (ret < 0)
  1727. return ret;
  1728. /* We don't want to handle interrupt without calling mtd routine */
  1729. disable_int(info, NDCR_INT_MASK);
  1730. /*
  1731. * Directly set the chip select to a invalid value,
  1732. * then the driver would reset the timing according
  1733. * to current chip select at the beginning of cmdfunc
  1734. */
  1735. info->cs = 0xff;
  1736. /*
  1737. * As the spec says, the NDSR would be updated to 0x1800 when
  1738. * doing the nand_clk disable/enable.
  1739. * To prevent it damaging state machine of the driver, clear
  1740. * all status before resume
  1741. */
  1742. nand_writel(info, NDSR, NDSR_MASK);
  1743. return 0;
  1744. }
  1745. #else
  1746. #define pxa3xx_nand_suspend NULL
  1747. #define pxa3xx_nand_resume NULL
  1748. #endif
  1749. static const struct dev_pm_ops pxa3xx_nand_pm_ops = {
  1750. .suspend = pxa3xx_nand_suspend,
  1751. .resume = pxa3xx_nand_resume,
  1752. };
  1753. static struct platform_driver pxa3xx_nand_driver = {
  1754. .driver = {
  1755. .name = "pxa3xx-nand",
  1756. .of_match_table = pxa3xx_nand_dt_ids,
  1757. .pm = &pxa3xx_nand_pm_ops,
  1758. },
  1759. .probe = pxa3xx_nand_probe,
  1760. .remove = pxa3xx_nand_remove,
  1761. };
  1762. module_platform_driver(pxa3xx_nand_driver);
  1763. MODULE_LICENSE("GPL");
  1764. MODULE_DESCRIPTION("PXA3xx NAND controller driver");