pxa3xx_nand.c 34 KB

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