pxa3xx_nand.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440
  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. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/delay.h>
  17. #include <linux/clk.h>
  18. #include <linux/mtd/mtd.h>
  19. #include <linux/mtd/nand.h>
  20. #include <linux/mtd/partitions.h>
  21. #include <linux/io.h>
  22. #include <linux/irq.h>
  23. #include <linux/slab.h>
  24. #include <mach/dma.h>
  25. #include <plat/pxa3xx_nand.h>
  26. #define CHIP_DELAY_TIMEOUT (2 * HZ/10)
  27. /* registers and bit definitions */
  28. #define NDCR (0x00) /* Control register */
  29. #define NDTR0CS0 (0x04) /* Timing Parameter 0 for CS0 */
  30. #define NDTR1CS0 (0x0C) /* Timing Parameter 1 for CS0 */
  31. #define NDSR (0x14) /* Status Register */
  32. #define NDPCR (0x18) /* Page Count Register */
  33. #define NDBDR0 (0x1C) /* Bad Block Register 0 */
  34. #define NDBDR1 (0x20) /* Bad Block Register 1 */
  35. #define NDDB (0x40) /* Data Buffer */
  36. #define NDCB0 (0x48) /* Command Buffer0 */
  37. #define NDCB1 (0x4C) /* Command Buffer1 */
  38. #define NDCB2 (0x50) /* Command Buffer2 */
  39. #define NDCR_SPARE_EN (0x1 << 31)
  40. #define NDCR_ECC_EN (0x1 << 30)
  41. #define NDCR_DMA_EN (0x1 << 29)
  42. #define NDCR_ND_RUN (0x1 << 28)
  43. #define NDCR_DWIDTH_C (0x1 << 27)
  44. #define NDCR_DWIDTH_M (0x1 << 26)
  45. #define NDCR_PAGE_SZ (0x1 << 24)
  46. #define NDCR_NCSX (0x1 << 23)
  47. #define NDCR_ND_MODE (0x3 << 21)
  48. #define NDCR_NAND_MODE (0x0)
  49. #define NDCR_CLR_PG_CNT (0x1 << 20)
  50. #define NDCR_CLR_ECC (0x1 << 19)
  51. #define NDCR_RD_ID_CNT_MASK (0x7 << 16)
  52. #define NDCR_RD_ID_CNT(x) (((x) << 16) & NDCR_RD_ID_CNT_MASK)
  53. #define NDCR_RA_START (0x1 << 15)
  54. #define NDCR_PG_PER_BLK (0x1 << 14)
  55. #define NDCR_ND_ARB_EN (0x1 << 12)
  56. #define NDSR_MASK (0xfff)
  57. #define NDSR_RDY (0x1 << 11)
  58. #define NDSR_CS0_PAGED (0x1 << 10)
  59. #define NDSR_CS1_PAGED (0x1 << 9)
  60. #define NDSR_CS0_CMDD (0x1 << 8)
  61. #define NDSR_CS1_CMDD (0x1 << 7)
  62. #define NDSR_CS0_BBD (0x1 << 6)
  63. #define NDSR_CS1_BBD (0x1 << 5)
  64. #define NDSR_DBERR (0x1 << 4)
  65. #define NDSR_SBERR (0x1 << 3)
  66. #define NDSR_WRDREQ (0x1 << 2)
  67. #define NDSR_RDDREQ (0x1 << 1)
  68. #define NDSR_WRCMDREQ (0x1)
  69. #define NDCB0_AUTO_RS (0x1 << 25)
  70. #define NDCB0_CSEL (0x1 << 24)
  71. #define NDCB0_CMD_TYPE_MASK (0x7 << 21)
  72. #define NDCB0_CMD_TYPE(x) (((x) << 21) & NDCB0_CMD_TYPE_MASK)
  73. #define NDCB0_NC (0x1 << 20)
  74. #define NDCB0_DBC (0x1 << 19)
  75. #define NDCB0_ADDR_CYC_MASK (0x7 << 16)
  76. #define NDCB0_ADDR_CYC(x) (((x) << 16) & NDCB0_ADDR_CYC_MASK)
  77. #define NDCB0_CMD2_MASK (0xff << 8)
  78. #define NDCB0_CMD1_MASK (0xff)
  79. #define NDCB0_ADDR_CYC_SHIFT (16)
  80. /* macros for registers read/write */
  81. #define nand_writel(info, off, val) \
  82. __raw_writel((val), (info)->mmio_base + (off))
  83. #define nand_readl(info, off) \
  84. __raw_readl((info)->mmio_base + (off))
  85. /* error code and state */
  86. enum {
  87. ERR_NONE = 0,
  88. ERR_DMABUSERR = -1,
  89. ERR_SENDCMD = -2,
  90. ERR_DBERR = -3,
  91. ERR_BBERR = -4,
  92. ERR_SBERR = -5,
  93. };
  94. enum {
  95. STATE_READY = 0,
  96. STATE_CMD_HANDLE,
  97. STATE_DMA_READING,
  98. STATE_DMA_WRITING,
  99. STATE_DMA_DONE,
  100. STATE_PIO_READING,
  101. STATE_PIO_WRITING,
  102. };
  103. struct pxa3xx_nand_info {
  104. struct nand_chip nand_chip;
  105. struct platform_device *pdev;
  106. const struct pxa3xx_nand_flash *flash_info;
  107. struct clk *clk;
  108. void __iomem *mmio_base;
  109. unsigned long mmio_phys;
  110. unsigned int buf_start;
  111. unsigned int buf_count;
  112. /* DMA information */
  113. int drcmr_dat;
  114. int drcmr_cmd;
  115. unsigned char *data_buff;
  116. dma_addr_t data_buff_phys;
  117. size_t data_buff_size;
  118. int data_dma_ch;
  119. struct pxa_dma_desc *data_desc;
  120. dma_addr_t data_desc_addr;
  121. uint32_t reg_ndcr;
  122. /* saved column/page_addr during CMD_SEQIN */
  123. int seqin_column;
  124. int seqin_page_addr;
  125. /* relate to the command */
  126. unsigned int state;
  127. int use_ecc; /* use HW ECC ? */
  128. int use_dma; /* use DMA ? */
  129. size_t data_size; /* data size in FIFO */
  130. int retcode;
  131. struct completion cmd_complete;
  132. /* generated NDCBx register values */
  133. uint32_t ndcb0;
  134. uint32_t ndcb1;
  135. uint32_t ndcb2;
  136. /* calculated from pxa3xx_nand_flash data */
  137. size_t oob_size;
  138. size_t read_id_bytes;
  139. unsigned int col_addr_cycles;
  140. unsigned int row_addr_cycles;
  141. };
  142. static int use_dma = 1;
  143. module_param(use_dma, bool, 0444);
  144. MODULE_PARM_DESC(use_dma, "enable DMA for data transfering to/from NAND HW");
  145. /*
  146. * Default NAND flash controller configuration setup by the
  147. * bootloader. This configuration is used only when pdata->keep_config is set
  148. */
  149. static struct pxa3xx_nand_timing default_timing;
  150. static struct pxa3xx_nand_flash default_flash;
  151. static struct pxa3xx_nand_cmdset smallpage_cmdset = {
  152. .read1 = 0x0000,
  153. .read2 = 0x0050,
  154. .program = 0x1080,
  155. .read_status = 0x0070,
  156. .read_id = 0x0090,
  157. .erase = 0xD060,
  158. .reset = 0x00FF,
  159. .lock = 0x002A,
  160. .unlock = 0x2423,
  161. .lock_status = 0x007A,
  162. };
  163. static struct pxa3xx_nand_cmdset largepage_cmdset = {
  164. .read1 = 0x3000,
  165. .read2 = 0x0050,
  166. .program = 0x1080,
  167. .read_status = 0x0070,
  168. .read_id = 0x0090,
  169. .erase = 0xD060,
  170. .reset = 0x00FF,
  171. .lock = 0x002A,
  172. .unlock = 0x2423,
  173. .lock_status = 0x007A,
  174. };
  175. #ifdef CONFIG_MTD_NAND_PXA3xx_BUILTIN
  176. static struct pxa3xx_nand_timing samsung512MbX16_timing = {
  177. .tCH = 10,
  178. .tCS = 0,
  179. .tWH = 20,
  180. .tWP = 40,
  181. .tRH = 30,
  182. .tRP = 40,
  183. .tR = 11123,
  184. .tWHR = 110,
  185. .tAR = 10,
  186. };
  187. static struct pxa3xx_nand_flash samsung512MbX16 = {
  188. .timing = &samsung512MbX16_timing,
  189. .cmdset = &smallpage_cmdset,
  190. .page_per_block = 32,
  191. .page_size = 512,
  192. .flash_width = 16,
  193. .dfc_width = 16,
  194. .num_blocks = 4096,
  195. .chip_id = 0x46ec,
  196. };
  197. static struct pxa3xx_nand_flash samsung2GbX8 = {
  198. .timing = &samsung512MbX16_timing,
  199. .cmdset = &smallpage_cmdset,
  200. .page_per_block = 64,
  201. .page_size = 2048,
  202. .flash_width = 8,
  203. .dfc_width = 8,
  204. .num_blocks = 2048,
  205. .chip_id = 0xdaec,
  206. };
  207. static struct pxa3xx_nand_flash samsung32GbX8 = {
  208. .timing = &samsung512MbX16_timing,
  209. .cmdset = &smallpage_cmdset,
  210. .page_per_block = 128,
  211. .page_size = 4096,
  212. .flash_width = 8,
  213. .dfc_width = 8,
  214. .num_blocks = 8192,
  215. .chip_id = 0xd7ec,
  216. };
  217. static struct pxa3xx_nand_timing micron_timing = {
  218. .tCH = 10,
  219. .tCS = 25,
  220. .tWH = 15,
  221. .tWP = 25,
  222. .tRH = 15,
  223. .tRP = 30,
  224. .tR = 25000,
  225. .tWHR = 60,
  226. .tAR = 10,
  227. };
  228. static struct pxa3xx_nand_flash micron1GbX8 = {
  229. .timing = &micron_timing,
  230. .cmdset = &largepage_cmdset,
  231. .page_per_block = 64,
  232. .page_size = 2048,
  233. .flash_width = 8,
  234. .dfc_width = 8,
  235. .num_blocks = 1024,
  236. .chip_id = 0xa12c,
  237. };
  238. static struct pxa3xx_nand_flash micron1GbX16 = {
  239. .timing = &micron_timing,
  240. .cmdset = &largepage_cmdset,
  241. .page_per_block = 64,
  242. .page_size = 2048,
  243. .flash_width = 16,
  244. .dfc_width = 16,
  245. .num_blocks = 1024,
  246. .chip_id = 0xb12c,
  247. };
  248. static struct pxa3xx_nand_flash micron4GbX8 = {
  249. .timing = &micron_timing,
  250. .cmdset = &largepage_cmdset,
  251. .page_per_block = 64,
  252. .page_size = 2048,
  253. .flash_width = 8,
  254. .dfc_width = 8,
  255. .num_blocks = 4096,
  256. .chip_id = 0xdc2c,
  257. };
  258. static struct pxa3xx_nand_flash micron4GbX16 = {
  259. .timing = &micron_timing,
  260. .cmdset = &largepage_cmdset,
  261. .page_per_block = 64,
  262. .page_size = 2048,
  263. .flash_width = 16,
  264. .dfc_width = 16,
  265. .num_blocks = 4096,
  266. .chip_id = 0xcc2c,
  267. };
  268. static struct pxa3xx_nand_timing stm2GbX16_timing = {
  269. .tCH = 10,
  270. .tCS = 35,
  271. .tWH = 15,
  272. .tWP = 25,
  273. .tRH = 15,
  274. .tRP = 25,
  275. .tR = 25000,
  276. .tWHR = 60,
  277. .tAR = 10,
  278. };
  279. static struct pxa3xx_nand_flash stm2GbX16 = {
  280. .timing = &stm2GbX16_timing,
  281. .cmdset = &largepage_cmdset,
  282. .page_per_block = 64,
  283. .page_size = 2048,
  284. .flash_width = 16,
  285. .dfc_width = 16,
  286. .num_blocks = 2048,
  287. .chip_id = 0xba20,
  288. };
  289. static struct pxa3xx_nand_flash *builtin_flash_types[] = {
  290. &samsung512MbX16,
  291. &samsung2GbX8,
  292. &samsung32GbX8,
  293. &micron1GbX8,
  294. &micron1GbX16,
  295. &micron4GbX8,
  296. &micron4GbX16,
  297. &stm2GbX16,
  298. };
  299. #endif /* CONFIG_MTD_NAND_PXA3xx_BUILTIN */
  300. #define NDTR0_tCH(c) (min((c), 7) << 19)
  301. #define NDTR0_tCS(c) (min((c), 7) << 16)
  302. #define NDTR0_tWH(c) (min((c), 7) << 11)
  303. #define NDTR0_tWP(c) (min((c), 7) << 8)
  304. #define NDTR0_tRH(c) (min((c), 7) << 3)
  305. #define NDTR0_tRP(c) (min((c), 7) << 0)
  306. #define NDTR1_tR(c) (min((c), 65535) << 16)
  307. #define NDTR1_tWHR(c) (min((c), 15) << 4)
  308. #define NDTR1_tAR(c) (min((c), 15) << 0)
  309. #define tCH_NDTR0(r) (((r) >> 19) & 0x7)
  310. #define tCS_NDTR0(r) (((r) >> 16) & 0x7)
  311. #define tWH_NDTR0(r) (((r) >> 11) & 0x7)
  312. #define tWP_NDTR0(r) (((r) >> 8) & 0x7)
  313. #define tRH_NDTR0(r) (((r) >> 3) & 0x7)
  314. #define tRP_NDTR0(r) (((r) >> 0) & 0x7)
  315. #define tR_NDTR1(r) (((r) >> 16) & 0xffff)
  316. #define tWHR_NDTR1(r) (((r) >> 4) & 0xf)
  317. #define tAR_NDTR1(r) (((r) >> 0) & 0xf)
  318. /* convert nano-seconds to nand flash controller clock cycles */
  319. #define ns2cycle(ns, clk) (int)(((ns) * (clk / 1000000) / 1000) - 1)
  320. /* convert nand flash controller clock cycles to nano-seconds */
  321. #define cycle2ns(c, clk) ((((c) + 1) * 1000000 + clk / 500) / (clk / 1000))
  322. static void pxa3xx_nand_set_timing(struct pxa3xx_nand_info *info,
  323. const struct pxa3xx_nand_timing *t)
  324. {
  325. unsigned long nand_clk = clk_get_rate(info->clk);
  326. uint32_t ndtr0, ndtr1;
  327. ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) |
  328. NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) |
  329. NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) |
  330. NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) |
  331. NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) |
  332. NDTR0_tRP(ns2cycle(t->tRP, nand_clk));
  333. ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) |
  334. NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) |
  335. NDTR1_tAR(ns2cycle(t->tAR, nand_clk));
  336. nand_writel(info, NDTR0CS0, ndtr0);
  337. nand_writel(info, NDTR1CS0, ndtr1);
  338. }
  339. #define WAIT_EVENT_TIMEOUT 10
  340. static int wait_for_event(struct pxa3xx_nand_info *info, uint32_t event)
  341. {
  342. int timeout = WAIT_EVENT_TIMEOUT;
  343. uint32_t ndsr;
  344. while (timeout--) {
  345. ndsr = nand_readl(info, NDSR) & NDSR_MASK;
  346. if (ndsr & event) {
  347. nand_writel(info, NDSR, ndsr);
  348. return 0;
  349. }
  350. udelay(10);
  351. }
  352. return -ETIMEDOUT;
  353. }
  354. static int prepare_read_prog_cmd(struct pxa3xx_nand_info *info,
  355. uint16_t cmd, int column, int page_addr)
  356. {
  357. const struct pxa3xx_nand_flash *f = info->flash_info;
  358. const struct pxa3xx_nand_cmdset *cmdset = f->cmdset;
  359. /* calculate data size */
  360. switch (f->page_size) {
  361. case 2048:
  362. info->data_size = (info->use_ecc) ? 2088 : 2112;
  363. break;
  364. case 512:
  365. info->data_size = (info->use_ecc) ? 520 : 528;
  366. break;
  367. default:
  368. return -EINVAL;
  369. }
  370. /* generate values for NDCBx registers */
  371. info->ndcb0 = cmd | ((cmd & 0xff00) ? NDCB0_DBC : 0);
  372. info->ndcb1 = 0;
  373. info->ndcb2 = 0;
  374. info->ndcb0 |= NDCB0_ADDR_CYC(info->row_addr_cycles + info->col_addr_cycles);
  375. if (info->col_addr_cycles == 2) {
  376. /* large block, 2 cycles for column address
  377. * row address starts from 3rd cycle
  378. */
  379. info->ndcb1 |= page_addr << 16;
  380. if (info->row_addr_cycles == 3)
  381. info->ndcb2 = (page_addr >> 16) & 0xff;
  382. } else
  383. /* small block, 1 cycles for column address
  384. * row address starts from 2nd cycle
  385. */
  386. info->ndcb1 = page_addr << 8;
  387. if (cmd == cmdset->program)
  388. info->ndcb0 |= NDCB0_CMD_TYPE(1) | NDCB0_AUTO_RS;
  389. return 0;
  390. }
  391. static int prepare_erase_cmd(struct pxa3xx_nand_info *info,
  392. uint16_t cmd, int page_addr)
  393. {
  394. info->ndcb0 = cmd | ((cmd & 0xff00) ? NDCB0_DBC : 0);
  395. info->ndcb0 |= NDCB0_CMD_TYPE(2) | NDCB0_AUTO_RS | NDCB0_ADDR_CYC(3);
  396. info->ndcb1 = page_addr;
  397. info->ndcb2 = 0;
  398. return 0;
  399. }
  400. static int prepare_other_cmd(struct pxa3xx_nand_info *info, uint16_t cmd)
  401. {
  402. const struct pxa3xx_nand_cmdset *cmdset = info->flash_info->cmdset;
  403. info->ndcb0 = cmd | ((cmd & 0xff00) ? NDCB0_DBC : 0);
  404. info->ndcb1 = 0;
  405. info->ndcb2 = 0;
  406. if (cmd == cmdset->read_id) {
  407. info->ndcb0 |= NDCB0_CMD_TYPE(3);
  408. info->data_size = 8;
  409. } else if (cmd == cmdset->read_status) {
  410. info->ndcb0 |= NDCB0_CMD_TYPE(4);
  411. info->data_size = 8;
  412. } else if (cmd == cmdset->reset || cmd == cmdset->lock ||
  413. cmd == cmdset->unlock) {
  414. info->ndcb0 |= NDCB0_CMD_TYPE(5);
  415. } else
  416. return -EINVAL;
  417. return 0;
  418. }
  419. static void enable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
  420. {
  421. uint32_t ndcr;
  422. ndcr = nand_readl(info, NDCR);
  423. nand_writel(info, NDCR, ndcr & ~int_mask);
  424. }
  425. static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
  426. {
  427. uint32_t ndcr;
  428. ndcr = nand_readl(info, NDCR);
  429. nand_writel(info, NDCR, ndcr | int_mask);
  430. }
  431. /* NOTE: it is a must to set ND_RUN firstly, then write command buffer
  432. * otherwise, it does not work
  433. */
  434. static int write_cmd(struct pxa3xx_nand_info *info)
  435. {
  436. uint32_t ndcr;
  437. /* clear status bits and run */
  438. nand_writel(info, NDSR, NDSR_MASK);
  439. ndcr = info->reg_ndcr;
  440. ndcr |= info->use_ecc ? NDCR_ECC_EN : 0;
  441. ndcr |= info->use_dma ? NDCR_DMA_EN : 0;
  442. ndcr |= NDCR_ND_RUN;
  443. nand_writel(info, NDCR, ndcr);
  444. if (wait_for_event(info, NDSR_WRCMDREQ)) {
  445. printk(KERN_ERR "timed out writing command\n");
  446. return -ETIMEDOUT;
  447. }
  448. nand_writel(info, NDCB0, info->ndcb0);
  449. nand_writel(info, NDCB0, info->ndcb1);
  450. nand_writel(info, NDCB0, info->ndcb2);
  451. return 0;
  452. }
  453. static int handle_data_pio(struct pxa3xx_nand_info *info)
  454. {
  455. int ret, timeout = CHIP_DELAY_TIMEOUT;
  456. switch (info->state) {
  457. case STATE_PIO_WRITING:
  458. __raw_writesl(info->mmio_base + NDDB, info->data_buff,
  459. DIV_ROUND_UP(info->data_size, 4));
  460. enable_int(info, NDSR_CS0_BBD | NDSR_CS0_CMDD);
  461. ret = wait_for_completion_timeout(&info->cmd_complete, timeout);
  462. if (!ret) {
  463. printk(KERN_ERR "program command time out\n");
  464. return -1;
  465. }
  466. break;
  467. case STATE_PIO_READING:
  468. __raw_readsl(info->mmio_base + NDDB, info->data_buff,
  469. DIV_ROUND_UP(info->data_size, 4));
  470. break;
  471. default:
  472. printk(KERN_ERR "%s: invalid state %d\n", __func__,
  473. info->state);
  474. return -EINVAL;
  475. }
  476. info->state = STATE_READY;
  477. return 0;
  478. }
  479. static void start_data_dma(struct pxa3xx_nand_info *info, int dir_out)
  480. {
  481. struct pxa_dma_desc *desc = info->data_desc;
  482. int dma_len = ALIGN(info->data_size, 32);
  483. desc->ddadr = DDADR_STOP;
  484. desc->dcmd = DCMD_ENDIRQEN | DCMD_WIDTH4 | DCMD_BURST32 | dma_len;
  485. if (dir_out) {
  486. desc->dsadr = info->data_buff_phys;
  487. desc->dtadr = info->mmio_phys + NDDB;
  488. desc->dcmd |= DCMD_INCSRCADDR | DCMD_FLOWTRG;
  489. } else {
  490. desc->dtadr = info->data_buff_phys;
  491. desc->dsadr = info->mmio_phys + NDDB;
  492. desc->dcmd |= DCMD_INCTRGADDR | DCMD_FLOWSRC;
  493. }
  494. DRCMR(info->drcmr_dat) = DRCMR_MAPVLD | info->data_dma_ch;
  495. DDADR(info->data_dma_ch) = info->data_desc_addr;
  496. DCSR(info->data_dma_ch) |= DCSR_RUN;
  497. }
  498. static void pxa3xx_nand_data_dma_irq(int channel, void *data)
  499. {
  500. struct pxa3xx_nand_info *info = data;
  501. uint32_t dcsr;
  502. dcsr = DCSR(channel);
  503. DCSR(channel) = dcsr;
  504. if (dcsr & DCSR_BUSERR) {
  505. info->retcode = ERR_DMABUSERR;
  506. complete(&info->cmd_complete);
  507. }
  508. if (info->state == STATE_DMA_WRITING) {
  509. info->state = STATE_DMA_DONE;
  510. enable_int(info, NDSR_CS0_BBD | NDSR_CS0_CMDD);
  511. } else {
  512. info->state = STATE_READY;
  513. complete(&info->cmd_complete);
  514. }
  515. }
  516. static irqreturn_t pxa3xx_nand_irq(int irq, void *devid)
  517. {
  518. struct pxa3xx_nand_info *info = devid;
  519. unsigned int status;
  520. status = nand_readl(info, NDSR);
  521. if (status & (NDSR_RDDREQ | NDSR_DBERR | NDSR_SBERR)) {
  522. if (status & NDSR_DBERR)
  523. info->retcode = ERR_DBERR;
  524. else if (status & NDSR_SBERR)
  525. info->retcode = ERR_SBERR;
  526. disable_int(info, NDSR_RDDREQ | NDSR_DBERR | NDSR_SBERR);
  527. if (info->use_dma) {
  528. info->state = STATE_DMA_READING;
  529. start_data_dma(info, 0);
  530. } else {
  531. info->state = STATE_PIO_READING;
  532. complete(&info->cmd_complete);
  533. }
  534. } else if (status & NDSR_WRDREQ) {
  535. disable_int(info, NDSR_WRDREQ);
  536. if (info->use_dma) {
  537. info->state = STATE_DMA_WRITING;
  538. start_data_dma(info, 1);
  539. } else {
  540. info->state = STATE_PIO_WRITING;
  541. complete(&info->cmd_complete);
  542. }
  543. } else if (status & (NDSR_CS0_BBD | NDSR_CS0_CMDD)) {
  544. if (status & NDSR_CS0_BBD)
  545. info->retcode = ERR_BBERR;
  546. disable_int(info, NDSR_CS0_BBD | NDSR_CS0_CMDD);
  547. info->state = STATE_READY;
  548. complete(&info->cmd_complete);
  549. }
  550. nand_writel(info, NDSR, status);
  551. return IRQ_HANDLED;
  552. }
  553. static int pxa3xx_nand_do_cmd(struct pxa3xx_nand_info *info, uint32_t event)
  554. {
  555. uint32_t ndcr;
  556. int ret, timeout = CHIP_DELAY_TIMEOUT;
  557. if (write_cmd(info)) {
  558. info->retcode = ERR_SENDCMD;
  559. goto fail_stop;
  560. }
  561. info->state = STATE_CMD_HANDLE;
  562. enable_int(info, event);
  563. ret = wait_for_completion_timeout(&info->cmd_complete, timeout);
  564. if (!ret) {
  565. printk(KERN_ERR "command execution timed out\n");
  566. info->retcode = ERR_SENDCMD;
  567. goto fail_stop;
  568. }
  569. if (info->use_dma == 0 && info->data_size > 0)
  570. if (handle_data_pio(info))
  571. goto fail_stop;
  572. return 0;
  573. fail_stop:
  574. ndcr = nand_readl(info, NDCR);
  575. nand_writel(info, NDCR, ndcr & ~NDCR_ND_RUN);
  576. udelay(10);
  577. return -ETIMEDOUT;
  578. }
  579. static int pxa3xx_nand_dev_ready(struct mtd_info *mtd)
  580. {
  581. struct pxa3xx_nand_info *info = mtd->priv;
  582. return (nand_readl(info, NDSR) & NDSR_RDY) ? 1 : 0;
  583. }
  584. static inline int is_buf_blank(uint8_t *buf, size_t len)
  585. {
  586. for (; len > 0; len--)
  587. if (*buf++ != 0xff)
  588. return 0;
  589. return 1;
  590. }
  591. static void pxa3xx_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
  592. int column, int page_addr)
  593. {
  594. struct pxa3xx_nand_info *info = mtd->priv;
  595. const struct pxa3xx_nand_flash *flash_info = info->flash_info;
  596. const struct pxa3xx_nand_cmdset *cmdset = flash_info->cmdset;
  597. int ret;
  598. info->use_dma = (use_dma) ? 1 : 0;
  599. info->use_ecc = 0;
  600. info->data_size = 0;
  601. info->state = STATE_READY;
  602. init_completion(&info->cmd_complete);
  603. switch (command) {
  604. case NAND_CMD_READOOB:
  605. /* disable HW ECC to get all the OOB data */
  606. info->buf_count = mtd->writesize + mtd->oobsize;
  607. info->buf_start = mtd->writesize + column;
  608. memset(info->data_buff, 0xFF, info->buf_count);
  609. if (prepare_read_prog_cmd(info, cmdset->read1, column, page_addr))
  610. break;
  611. pxa3xx_nand_do_cmd(info, NDSR_RDDREQ | NDSR_DBERR | NDSR_SBERR);
  612. /* We only are OOB, so if the data has error, does not matter */
  613. if (info->retcode == ERR_DBERR)
  614. info->retcode = ERR_NONE;
  615. break;
  616. case NAND_CMD_READ0:
  617. info->use_ecc = 1;
  618. info->retcode = ERR_NONE;
  619. info->buf_start = column;
  620. info->buf_count = mtd->writesize + mtd->oobsize;
  621. memset(info->data_buff, 0xFF, info->buf_count);
  622. if (prepare_read_prog_cmd(info, cmdset->read1, column, page_addr))
  623. break;
  624. pxa3xx_nand_do_cmd(info, NDSR_RDDREQ | NDSR_DBERR | NDSR_SBERR);
  625. if (info->retcode == ERR_DBERR) {
  626. /* for blank page (all 0xff), HW will calculate its ECC as
  627. * 0, which is different from the ECC information within
  628. * OOB, ignore such double bit errors
  629. */
  630. if (is_buf_blank(info->data_buff, mtd->writesize))
  631. info->retcode = ERR_NONE;
  632. }
  633. break;
  634. case NAND_CMD_SEQIN:
  635. info->buf_start = column;
  636. info->buf_count = mtd->writesize + mtd->oobsize;
  637. memset(info->data_buff, 0xff, info->buf_count);
  638. /* save column/page_addr for next CMD_PAGEPROG */
  639. info->seqin_column = column;
  640. info->seqin_page_addr = page_addr;
  641. break;
  642. case NAND_CMD_PAGEPROG:
  643. info->use_ecc = (info->seqin_column >= mtd->writesize) ? 0 : 1;
  644. if (prepare_read_prog_cmd(info, cmdset->program,
  645. info->seqin_column, info->seqin_page_addr))
  646. break;
  647. pxa3xx_nand_do_cmd(info, NDSR_WRDREQ);
  648. break;
  649. case NAND_CMD_ERASE1:
  650. if (prepare_erase_cmd(info, cmdset->erase, page_addr))
  651. break;
  652. pxa3xx_nand_do_cmd(info, NDSR_CS0_BBD | NDSR_CS0_CMDD);
  653. break;
  654. case NAND_CMD_ERASE2:
  655. break;
  656. case NAND_CMD_READID:
  657. case NAND_CMD_STATUS:
  658. info->use_dma = 0; /* force PIO read */
  659. info->buf_start = 0;
  660. info->buf_count = (command == NAND_CMD_READID) ?
  661. info->read_id_bytes : 1;
  662. if (prepare_other_cmd(info, (command == NAND_CMD_READID) ?
  663. cmdset->read_id : cmdset->read_status))
  664. break;
  665. pxa3xx_nand_do_cmd(info, NDSR_RDDREQ);
  666. break;
  667. case NAND_CMD_RESET:
  668. if (prepare_other_cmd(info, cmdset->reset))
  669. break;
  670. ret = pxa3xx_nand_do_cmd(info, NDSR_CS0_CMDD);
  671. if (ret == 0) {
  672. int timeout = 2;
  673. uint32_t ndcr;
  674. while (timeout--) {
  675. if (nand_readl(info, NDSR) & NDSR_RDY)
  676. break;
  677. msleep(10);
  678. }
  679. ndcr = nand_readl(info, NDCR);
  680. nand_writel(info, NDCR, ndcr & ~NDCR_ND_RUN);
  681. }
  682. break;
  683. default:
  684. printk(KERN_ERR "non-supported command.\n");
  685. break;
  686. }
  687. if (info->retcode == ERR_DBERR) {
  688. printk(KERN_ERR "double bit error @ page %08x\n", page_addr);
  689. info->retcode = ERR_NONE;
  690. }
  691. }
  692. static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
  693. {
  694. struct pxa3xx_nand_info *info = mtd->priv;
  695. char retval = 0xFF;
  696. if (info->buf_start < info->buf_count)
  697. /* Has just send a new command? */
  698. retval = info->data_buff[info->buf_start++];
  699. return retval;
  700. }
  701. static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
  702. {
  703. struct pxa3xx_nand_info *info = mtd->priv;
  704. u16 retval = 0xFFFF;
  705. if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
  706. retval = *((u16 *)(info->data_buff+info->buf_start));
  707. info->buf_start += 2;
  708. }
  709. return retval;
  710. }
  711. static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  712. {
  713. struct pxa3xx_nand_info *info = mtd->priv;
  714. int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
  715. memcpy(buf, info->data_buff + info->buf_start, real_len);
  716. info->buf_start += real_len;
  717. }
  718. static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
  719. const uint8_t *buf, int len)
  720. {
  721. struct pxa3xx_nand_info *info = mtd->priv;
  722. int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
  723. memcpy(info->data_buff + info->buf_start, buf, real_len);
  724. info->buf_start += real_len;
  725. }
  726. static int pxa3xx_nand_verify_buf(struct mtd_info *mtd,
  727. const uint8_t *buf, int len)
  728. {
  729. return 0;
  730. }
  731. static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
  732. {
  733. return;
  734. }
  735. static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
  736. {
  737. struct pxa3xx_nand_info *info = mtd->priv;
  738. /* pxa3xx_nand_send_command has waited for command complete */
  739. if (this->state == FL_WRITING || this->state == FL_ERASING) {
  740. if (info->retcode == ERR_NONE)
  741. return 0;
  742. else {
  743. /*
  744. * any error make it return 0x01 which will tell
  745. * the caller the erase and write fail
  746. */
  747. return 0x01;
  748. }
  749. }
  750. return 0;
  751. }
  752. static void pxa3xx_nand_ecc_hwctl(struct mtd_info *mtd, int mode)
  753. {
  754. return;
  755. }
  756. static int pxa3xx_nand_ecc_calculate(struct mtd_info *mtd,
  757. const uint8_t *dat, uint8_t *ecc_code)
  758. {
  759. return 0;
  760. }
  761. static int pxa3xx_nand_ecc_correct(struct mtd_info *mtd,
  762. uint8_t *dat, uint8_t *read_ecc, uint8_t *calc_ecc)
  763. {
  764. struct pxa3xx_nand_info *info = mtd->priv;
  765. /*
  766. * Any error include ERR_SEND_CMD, ERR_DBERR, ERR_BUSERR, we
  767. * consider it as a ecc error which will tell the caller the
  768. * read fail We have distinguish all the errors, but the
  769. * nand_read_ecc only check this function return value
  770. *
  771. * Corrected (single-bit) errors must also be noted.
  772. */
  773. if (info->retcode == ERR_SBERR)
  774. return 1;
  775. else if (info->retcode != ERR_NONE)
  776. return -1;
  777. return 0;
  778. }
  779. static int __readid(struct pxa3xx_nand_info *info, uint32_t *id)
  780. {
  781. const struct pxa3xx_nand_flash *f = info->flash_info;
  782. const struct pxa3xx_nand_cmdset *cmdset = f->cmdset;
  783. uint32_t ndcr;
  784. uint8_t id_buff[8];
  785. if (prepare_other_cmd(info, cmdset->read_id)) {
  786. printk(KERN_ERR "failed to prepare command\n");
  787. return -EINVAL;
  788. }
  789. /* Send command */
  790. if (write_cmd(info))
  791. goto fail_timeout;
  792. /* Wait for CMDDM(command done successfully) */
  793. if (wait_for_event(info, NDSR_RDDREQ))
  794. goto fail_timeout;
  795. __raw_readsl(info->mmio_base + NDDB, id_buff, 2);
  796. *id = id_buff[0] | (id_buff[1] << 8);
  797. return 0;
  798. fail_timeout:
  799. ndcr = nand_readl(info, NDCR);
  800. nand_writel(info, NDCR, ndcr & ~NDCR_ND_RUN);
  801. udelay(10);
  802. return -ETIMEDOUT;
  803. }
  804. static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info,
  805. const struct pxa3xx_nand_flash *f)
  806. {
  807. struct platform_device *pdev = info->pdev;
  808. struct pxa3xx_nand_platform_data *pdata = pdev->dev.platform_data;
  809. uint32_t ndcr = 0x00000FFF; /* disable all interrupts */
  810. if (f->page_size != 2048 && f->page_size != 512)
  811. return -EINVAL;
  812. if (f->flash_width != 16 && f->flash_width != 8)
  813. return -EINVAL;
  814. /* calculate flash information */
  815. info->oob_size = (f->page_size == 2048) ? 64 : 16;
  816. info->read_id_bytes = (f->page_size == 2048) ? 4 : 2;
  817. /* calculate addressing information */
  818. info->col_addr_cycles = (f->page_size == 2048) ? 2 : 1;
  819. if (f->num_blocks * f->page_per_block > 65536)
  820. info->row_addr_cycles = 3;
  821. else
  822. info->row_addr_cycles = 2;
  823. ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
  824. ndcr |= (info->col_addr_cycles == 2) ? NDCR_RA_START : 0;
  825. ndcr |= (f->page_per_block == 64) ? NDCR_PG_PER_BLK : 0;
  826. ndcr |= (f->page_size == 2048) ? NDCR_PAGE_SZ : 0;
  827. ndcr |= (f->flash_width == 16) ? NDCR_DWIDTH_M : 0;
  828. ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0;
  829. ndcr |= NDCR_RD_ID_CNT(info->read_id_bytes);
  830. ndcr |= NDCR_SPARE_EN; /* enable spare by default */
  831. info->reg_ndcr = ndcr;
  832. pxa3xx_nand_set_timing(info, f->timing);
  833. info->flash_info = f;
  834. return 0;
  835. }
  836. static void pxa3xx_nand_detect_timing(struct pxa3xx_nand_info *info,
  837. struct pxa3xx_nand_timing *t)
  838. {
  839. unsigned long nand_clk = clk_get_rate(info->clk);
  840. uint32_t ndtr0 = nand_readl(info, NDTR0CS0);
  841. uint32_t ndtr1 = nand_readl(info, NDTR1CS0);
  842. t->tCH = cycle2ns(tCH_NDTR0(ndtr0), nand_clk);
  843. t->tCS = cycle2ns(tCS_NDTR0(ndtr0), nand_clk);
  844. t->tWH = cycle2ns(tWH_NDTR0(ndtr0), nand_clk);
  845. t->tWP = cycle2ns(tWP_NDTR0(ndtr0), nand_clk);
  846. t->tRH = cycle2ns(tRH_NDTR0(ndtr0), nand_clk);
  847. t->tRP = cycle2ns(tRP_NDTR0(ndtr0), nand_clk);
  848. t->tR = cycle2ns(tR_NDTR1(ndtr1), nand_clk);
  849. t->tWHR = cycle2ns(tWHR_NDTR1(ndtr1), nand_clk);
  850. t->tAR = cycle2ns(tAR_NDTR1(ndtr1), nand_clk);
  851. }
  852. static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
  853. {
  854. uint32_t ndcr = nand_readl(info, NDCR);
  855. struct nand_flash_dev *type = NULL;
  856. uint32_t id = -1;
  857. int i;
  858. default_flash.page_per_block = ndcr & NDCR_PG_PER_BLK ? 64 : 32;
  859. default_flash.page_size = ndcr & NDCR_PAGE_SZ ? 2048 : 512;
  860. default_flash.flash_width = ndcr & NDCR_DWIDTH_M ? 16 : 8;
  861. default_flash.dfc_width = ndcr & NDCR_DWIDTH_C ? 16 : 8;
  862. if (default_flash.page_size == 2048)
  863. default_flash.cmdset = &largepage_cmdset;
  864. else
  865. default_flash.cmdset = &smallpage_cmdset;
  866. /* set info fields needed to __readid */
  867. info->flash_info = &default_flash;
  868. info->read_id_bytes = (default_flash.page_size == 2048) ? 4 : 2;
  869. info->reg_ndcr = ndcr;
  870. if (__readid(info, &id))
  871. return -ENODEV;
  872. /* Lookup the flash id */
  873. id = (id >> 8) & 0xff; /* device id is byte 2 */
  874. for (i = 0; nand_flash_ids[i].name != NULL; i++) {
  875. if (id == nand_flash_ids[i].id) {
  876. type = &nand_flash_ids[i];
  877. break;
  878. }
  879. }
  880. if (!type)
  881. return -ENODEV;
  882. /* fill the missing flash information */
  883. i = __ffs(default_flash.page_per_block * default_flash.page_size);
  884. default_flash.num_blocks = type->chipsize << (20 - i);
  885. info->oob_size = (default_flash.page_size == 2048) ? 64 : 16;
  886. /* calculate addressing information */
  887. info->col_addr_cycles = (default_flash.page_size == 2048) ? 2 : 1;
  888. if (default_flash.num_blocks * default_flash.page_per_block > 65536)
  889. info->row_addr_cycles = 3;
  890. else
  891. info->row_addr_cycles = 2;
  892. pxa3xx_nand_detect_timing(info, &default_timing);
  893. default_flash.timing = &default_timing;
  894. return 0;
  895. }
  896. static int pxa3xx_nand_detect_flash(struct pxa3xx_nand_info *info,
  897. const struct pxa3xx_nand_platform_data *pdata)
  898. {
  899. const struct pxa3xx_nand_flash *f;
  900. uint32_t id = -1;
  901. int i;
  902. if (pdata->keep_config)
  903. if (pxa3xx_nand_detect_config(info) == 0)
  904. return 0;
  905. for (i = 0; i<pdata->num_flash; ++i) {
  906. f = pdata->flash + i;
  907. if (pxa3xx_nand_config_flash(info, f))
  908. continue;
  909. if (__readid(info, &id))
  910. continue;
  911. if (id == f->chip_id)
  912. return 0;
  913. }
  914. #ifdef CONFIG_MTD_NAND_PXA3xx_BUILTIN
  915. for (i = 0; i < ARRAY_SIZE(builtin_flash_types); i++) {
  916. f = builtin_flash_types[i];
  917. if (pxa3xx_nand_config_flash(info, f))
  918. continue;
  919. if (__readid(info, &id))
  920. continue;
  921. if (id == f->chip_id)
  922. return 0;
  923. }
  924. #endif
  925. dev_warn(&info->pdev->dev,
  926. "failed to detect configured nand flash; found %04x instead of\n",
  927. id);
  928. return -ENODEV;
  929. }
  930. /* the maximum possible buffer size for large page with OOB data
  931. * is: 2048 + 64 = 2112 bytes, allocate a page here for both the
  932. * data buffer and the DMA descriptor
  933. */
  934. #define MAX_BUFF_SIZE PAGE_SIZE
  935. static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
  936. {
  937. struct platform_device *pdev = info->pdev;
  938. int data_desc_offset = MAX_BUFF_SIZE - sizeof(struct pxa_dma_desc);
  939. if (use_dma == 0) {
  940. info->data_buff = kmalloc(MAX_BUFF_SIZE, GFP_KERNEL);
  941. if (info->data_buff == NULL)
  942. return -ENOMEM;
  943. return 0;
  944. }
  945. info->data_buff = dma_alloc_coherent(&pdev->dev, MAX_BUFF_SIZE,
  946. &info->data_buff_phys, GFP_KERNEL);
  947. if (info->data_buff == NULL) {
  948. dev_err(&pdev->dev, "failed to allocate dma buffer\n");
  949. return -ENOMEM;
  950. }
  951. info->data_buff_size = MAX_BUFF_SIZE;
  952. info->data_desc = (void *)info->data_buff + data_desc_offset;
  953. info->data_desc_addr = info->data_buff_phys + data_desc_offset;
  954. info->data_dma_ch = pxa_request_dma("nand-data", DMA_PRIO_LOW,
  955. pxa3xx_nand_data_dma_irq, info);
  956. if (info->data_dma_ch < 0) {
  957. dev_err(&pdev->dev, "failed to request data dma\n");
  958. dma_free_coherent(&pdev->dev, info->data_buff_size,
  959. info->data_buff, info->data_buff_phys);
  960. return info->data_dma_ch;
  961. }
  962. return 0;
  963. }
  964. static struct nand_ecclayout hw_smallpage_ecclayout = {
  965. .eccbytes = 6,
  966. .eccpos = {8, 9, 10, 11, 12, 13 },
  967. .oobfree = { {2, 6} }
  968. };
  969. static struct nand_ecclayout hw_largepage_ecclayout = {
  970. .eccbytes = 24,
  971. .eccpos = {
  972. 40, 41, 42, 43, 44, 45, 46, 47,
  973. 48, 49, 50, 51, 52, 53, 54, 55,
  974. 56, 57, 58, 59, 60, 61, 62, 63},
  975. .oobfree = { {2, 38} }
  976. };
  977. static void pxa3xx_nand_init_mtd(struct mtd_info *mtd,
  978. struct pxa3xx_nand_info *info)
  979. {
  980. const struct pxa3xx_nand_flash *f = info->flash_info;
  981. struct nand_chip *this = &info->nand_chip;
  982. this->options = (f->flash_width == 16) ? NAND_BUSWIDTH_16: 0;
  983. this->waitfunc = pxa3xx_nand_waitfunc;
  984. this->select_chip = pxa3xx_nand_select_chip;
  985. this->dev_ready = pxa3xx_nand_dev_ready;
  986. this->cmdfunc = pxa3xx_nand_cmdfunc;
  987. this->read_word = pxa3xx_nand_read_word;
  988. this->read_byte = pxa3xx_nand_read_byte;
  989. this->read_buf = pxa3xx_nand_read_buf;
  990. this->write_buf = pxa3xx_nand_write_buf;
  991. this->verify_buf = pxa3xx_nand_verify_buf;
  992. this->ecc.mode = NAND_ECC_HW;
  993. this->ecc.hwctl = pxa3xx_nand_ecc_hwctl;
  994. this->ecc.calculate = pxa3xx_nand_ecc_calculate;
  995. this->ecc.correct = pxa3xx_nand_ecc_correct;
  996. this->ecc.size = f->page_size;
  997. if (f->page_size == 2048)
  998. this->ecc.layout = &hw_largepage_ecclayout;
  999. else
  1000. this->ecc.layout = &hw_smallpage_ecclayout;
  1001. this->chip_delay = 25;
  1002. }
  1003. static int pxa3xx_nand_probe(struct platform_device *pdev)
  1004. {
  1005. struct pxa3xx_nand_platform_data *pdata;
  1006. struct pxa3xx_nand_info *info;
  1007. struct nand_chip *this;
  1008. struct mtd_info *mtd;
  1009. struct resource *r;
  1010. int ret = 0, irq;
  1011. pdata = pdev->dev.platform_data;
  1012. if (!pdata) {
  1013. dev_err(&pdev->dev, "no platform data defined\n");
  1014. return -ENODEV;
  1015. }
  1016. mtd = kzalloc(sizeof(struct mtd_info) + sizeof(struct pxa3xx_nand_info),
  1017. GFP_KERNEL);
  1018. if (!mtd) {
  1019. dev_err(&pdev->dev, "failed to allocate memory\n");
  1020. return -ENOMEM;
  1021. }
  1022. info = (struct pxa3xx_nand_info *)(&mtd[1]);
  1023. info->pdev = pdev;
  1024. this = &info->nand_chip;
  1025. mtd->priv = info;
  1026. mtd->owner = THIS_MODULE;
  1027. info->clk = clk_get(&pdev->dev, NULL);
  1028. if (IS_ERR(info->clk)) {
  1029. dev_err(&pdev->dev, "failed to get nand clock\n");
  1030. ret = PTR_ERR(info->clk);
  1031. goto fail_free_mtd;
  1032. }
  1033. clk_enable(info->clk);
  1034. r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  1035. if (r == NULL) {
  1036. dev_err(&pdev->dev, "no resource defined for data DMA\n");
  1037. ret = -ENXIO;
  1038. goto fail_put_clk;
  1039. }
  1040. info->drcmr_dat = r->start;
  1041. r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
  1042. if (r == NULL) {
  1043. dev_err(&pdev->dev, "no resource defined for command DMA\n");
  1044. ret = -ENXIO;
  1045. goto fail_put_clk;
  1046. }
  1047. info->drcmr_cmd = r->start;
  1048. irq = platform_get_irq(pdev, 0);
  1049. if (irq < 0) {
  1050. dev_err(&pdev->dev, "no IRQ resource defined\n");
  1051. ret = -ENXIO;
  1052. goto fail_put_clk;
  1053. }
  1054. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1055. if (r == NULL) {
  1056. dev_err(&pdev->dev, "no IO memory resource defined\n");
  1057. ret = -ENODEV;
  1058. goto fail_put_clk;
  1059. }
  1060. r = request_mem_region(r->start, resource_size(r), pdev->name);
  1061. if (r == NULL) {
  1062. dev_err(&pdev->dev, "failed to request memory resource\n");
  1063. ret = -EBUSY;
  1064. goto fail_put_clk;
  1065. }
  1066. info->mmio_base = ioremap(r->start, resource_size(r));
  1067. if (info->mmio_base == NULL) {
  1068. dev_err(&pdev->dev, "ioremap() failed\n");
  1069. ret = -ENODEV;
  1070. goto fail_free_res;
  1071. }
  1072. info->mmio_phys = r->start;
  1073. ret = pxa3xx_nand_init_buff(info);
  1074. if (ret)
  1075. goto fail_free_io;
  1076. /* initialize all interrupts to be disabled */
  1077. disable_int(info, NDSR_MASK);
  1078. ret = request_irq(irq, pxa3xx_nand_irq, IRQF_DISABLED,
  1079. pdev->name, info);
  1080. if (ret < 0) {
  1081. dev_err(&pdev->dev, "failed to request IRQ\n");
  1082. goto fail_free_buf;
  1083. }
  1084. ret = pxa3xx_nand_detect_flash(info, pdata);
  1085. if (ret) {
  1086. dev_err(&pdev->dev, "failed to detect flash\n");
  1087. ret = -ENODEV;
  1088. goto fail_free_irq;
  1089. }
  1090. pxa3xx_nand_init_mtd(mtd, info);
  1091. platform_set_drvdata(pdev, mtd);
  1092. if (nand_scan(mtd, 1)) {
  1093. dev_err(&pdev->dev, "failed to scan nand\n");
  1094. ret = -ENXIO;
  1095. goto fail_free_irq;
  1096. }
  1097. if (mtd_has_cmdlinepart()) {
  1098. static const char *probes[] = { "cmdlinepart", NULL };
  1099. struct mtd_partition *parts;
  1100. int nr_parts;
  1101. nr_parts = parse_mtd_partitions(mtd, probes, &parts, 0);
  1102. if (nr_parts)
  1103. return add_mtd_partitions(mtd, parts, nr_parts);
  1104. }
  1105. return add_mtd_partitions(mtd, pdata->parts, pdata->nr_parts);
  1106. fail_free_irq:
  1107. free_irq(irq, info);
  1108. fail_free_buf:
  1109. if (use_dma) {
  1110. pxa_free_dma(info->data_dma_ch);
  1111. dma_free_coherent(&pdev->dev, info->data_buff_size,
  1112. info->data_buff, info->data_buff_phys);
  1113. } else
  1114. kfree(info->data_buff);
  1115. fail_free_io:
  1116. iounmap(info->mmio_base);
  1117. fail_free_res:
  1118. release_mem_region(r->start, resource_size(r));
  1119. fail_put_clk:
  1120. clk_disable(info->clk);
  1121. clk_put(info->clk);
  1122. fail_free_mtd:
  1123. kfree(mtd);
  1124. return ret;
  1125. }
  1126. static int pxa3xx_nand_remove(struct platform_device *pdev)
  1127. {
  1128. struct mtd_info *mtd = platform_get_drvdata(pdev);
  1129. struct pxa3xx_nand_info *info = mtd->priv;
  1130. struct resource *r;
  1131. int irq;
  1132. platform_set_drvdata(pdev, NULL);
  1133. del_mtd_device(mtd);
  1134. del_mtd_partitions(mtd);
  1135. irq = platform_get_irq(pdev, 0);
  1136. if (irq >= 0)
  1137. free_irq(irq, info);
  1138. if (use_dma) {
  1139. pxa_free_dma(info->data_dma_ch);
  1140. dma_free_writecombine(&pdev->dev, info->data_buff_size,
  1141. info->data_buff, info->data_buff_phys);
  1142. } else
  1143. kfree(info->data_buff);
  1144. iounmap(info->mmio_base);
  1145. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1146. release_mem_region(r->start, resource_size(r));
  1147. clk_disable(info->clk);
  1148. clk_put(info->clk);
  1149. kfree(mtd);
  1150. return 0;
  1151. }
  1152. #ifdef CONFIG_PM
  1153. static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state)
  1154. {
  1155. struct mtd_info *mtd = (struct mtd_info *)platform_get_drvdata(pdev);
  1156. struct pxa3xx_nand_info *info = mtd->priv;
  1157. if (info->state != STATE_READY) {
  1158. dev_err(&pdev->dev, "driver busy, state = %d\n", info->state);
  1159. return -EAGAIN;
  1160. }
  1161. return 0;
  1162. }
  1163. static int pxa3xx_nand_resume(struct platform_device *pdev)
  1164. {
  1165. struct mtd_info *mtd = (struct mtd_info *)platform_get_drvdata(pdev);
  1166. struct pxa3xx_nand_info *info = mtd->priv;
  1167. clk_enable(info->clk);
  1168. return pxa3xx_nand_config_flash(info, info->flash_info);
  1169. }
  1170. #else
  1171. #define pxa3xx_nand_suspend NULL
  1172. #define pxa3xx_nand_resume NULL
  1173. #endif
  1174. static struct platform_driver pxa3xx_nand_driver = {
  1175. .driver = {
  1176. .name = "pxa3xx-nand",
  1177. },
  1178. .probe = pxa3xx_nand_probe,
  1179. .remove = pxa3xx_nand_remove,
  1180. .suspend = pxa3xx_nand_suspend,
  1181. .resume = pxa3xx_nand_resume,
  1182. };
  1183. static int __init pxa3xx_nand_init(void)
  1184. {
  1185. return platform_driver_register(&pxa3xx_nand_driver);
  1186. }
  1187. module_init(pxa3xx_nand_init);
  1188. static void __exit pxa3xx_nand_exit(void)
  1189. {
  1190. platform_driver_unregister(&pxa3xx_nand_driver);
  1191. }
  1192. module_exit(pxa3xx_nand_exit);
  1193. MODULE_LICENSE("GPL");
  1194. MODULE_DESCRIPTION("PXA3xx NAND controller driver");