pxa3xx_nand.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374
  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/module.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/dma-mapping.h>
  15. #include <linux/delay.h>
  16. #include <linux/clk.h>
  17. #include <linux/mtd/mtd.h>
  18. #include <linux/mtd/nand.h>
  19. #include <linux/mtd/partitions.h>
  20. #include <linux/io.h>
  21. #include <linux/irq.h>
  22. #include <mach/dma.h>
  23. #include <mach/pxa3xx_nand.h>
  24. #define CHIP_DELAY_TIMEOUT (2 * HZ/10)
  25. /* registers and bit definitions */
  26. #define NDCR (0x00) /* Control register */
  27. #define NDTR0CS0 (0x04) /* Timing Parameter 0 for CS0 */
  28. #define NDTR1CS0 (0x0C) /* Timing Parameter 1 for CS0 */
  29. #define NDSR (0x14) /* Status Register */
  30. #define NDPCR (0x18) /* Page Count Register */
  31. #define NDBDR0 (0x1C) /* Bad Block Register 0 */
  32. #define NDBDR1 (0x20) /* Bad Block Register 1 */
  33. #define NDDB (0x40) /* Data Buffer */
  34. #define NDCB0 (0x48) /* Command Buffer0 */
  35. #define NDCB1 (0x4C) /* Command Buffer1 */
  36. #define NDCB2 (0x50) /* Command Buffer2 */
  37. #define NDCR_SPARE_EN (0x1 << 31)
  38. #define NDCR_ECC_EN (0x1 << 30)
  39. #define NDCR_DMA_EN (0x1 << 29)
  40. #define NDCR_ND_RUN (0x1 << 28)
  41. #define NDCR_DWIDTH_C (0x1 << 27)
  42. #define NDCR_DWIDTH_M (0x1 << 26)
  43. #define NDCR_PAGE_SZ (0x1 << 24)
  44. #define NDCR_NCSX (0x1 << 23)
  45. #define NDCR_ND_MODE (0x3 << 21)
  46. #define NDCR_NAND_MODE (0x0)
  47. #define NDCR_CLR_PG_CNT (0x1 << 20)
  48. #define NDCR_CLR_ECC (0x1 << 19)
  49. #define NDCR_RD_ID_CNT_MASK (0x7 << 16)
  50. #define NDCR_RD_ID_CNT(x) (((x) << 16) & NDCR_RD_ID_CNT_MASK)
  51. #define NDCR_RA_START (0x1 << 15)
  52. #define NDCR_PG_PER_BLK (0x1 << 14)
  53. #define NDCR_ND_ARB_EN (0x1 << 12)
  54. #define NDSR_MASK (0xfff)
  55. #define NDSR_RDY (0x1 << 11)
  56. #define NDSR_CS0_PAGED (0x1 << 10)
  57. #define NDSR_CS1_PAGED (0x1 << 9)
  58. #define NDSR_CS0_CMDD (0x1 << 8)
  59. #define NDSR_CS1_CMDD (0x1 << 7)
  60. #define NDSR_CS0_BBD (0x1 << 6)
  61. #define NDSR_CS1_BBD (0x1 << 5)
  62. #define NDSR_DBERR (0x1 << 4)
  63. #define NDSR_SBERR (0x1 << 3)
  64. #define NDSR_WRDREQ (0x1 << 2)
  65. #define NDSR_RDDREQ (0x1 << 1)
  66. #define NDSR_WRCMDREQ (0x1)
  67. #define NDCB0_AUTO_RS (0x1 << 25)
  68. #define NDCB0_CSEL (0x1 << 24)
  69. #define NDCB0_CMD_TYPE_MASK (0x7 << 21)
  70. #define NDCB0_CMD_TYPE(x) (((x) << 21) & NDCB0_CMD_TYPE_MASK)
  71. #define NDCB0_NC (0x1 << 20)
  72. #define NDCB0_DBC (0x1 << 19)
  73. #define NDCB0_ADDR_CYC_MASK (0x7 << 16)
  74. #define NDCB0_ADDR_CYC(x) (((x) << 16) & NDCB0_ADDR_CYC_MASK)
  75. #define NDCB0_CMD2_MASK (0xff << 8)
  76. #define NDCB0_CMD1_MASK (0xff)
  77. #define NDCB0_ADDR_CYC_SHIFT (16)
  78. /* dma-able I/O address for the NAND data and commands */
  79. #define NDCB0_DMA_ADDR (0x43100048)
  80. #define NDDB_DMA_ADDR (0x43100040)
  81. /* macros for registers read/write */
  82. #define nand_writel(info, off, val) \
  83. __raw_writel((val), (info)->mmio_base + (off))
  84. #define nand_readl(info, off) \
  85. __raw_readl((info)->mmio_base + (off))
  86. /* error code and state */
  87. enum {
  88. ERR_NONE = 0,
  89. ERR_DMABUSERR = -1,
  90. ERR_SENDCMD = -2,
  91. ERR_DBERR = -3,
  92. ERR_BBERR = -4,
  93. ERR_SBERR = -5,
  94. };
  95. enum {
  96. STATE_READY = 0,
  97. STATE_CMD_HANDLE,
  98. STATE_DMA_READING,
  99. STATE_DMA_WRITING,
  100. STATE_DMA_DONE,
  101. STATE_PIO_READING,
  102. STATE_PIO_WRITING,
  103. };
  104. struct pxa3xx_nand_info {
  105. struct nand_chip nand_chip;
  106. struct platform_device *pdev;
  107. const struct pxa3xx_nand_flash *flash_info;
  108. struct clk *clk;
  109. void __iomem *mmio_base;
  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_timing micron_timing = {
  198. .tCH = 10,
  199. .tCS = 25,
  200. .tWH = 15,
  201. .tWP = 25,
  202. .tRH = 15,
  203. .tRP = 25,
  204. .tR = 25000,
  205. .tWHR = 60,
  206. .tAR = 10,
  207. };
  208. static struct pxa3xx_nand_flash micron1GbX8 = {
  209. .timing = &micron_timing,
  210. .cmdset = &largepage_cmdset,
  211. .page_per_block = 64,
  212. .page_size = 2048,
  213. .flash_width = 8,
  214. .dfc_width = 8,
  215. .num_blocks = 1024,
  216. .chip_id = 0xa12c,
  217. };
  218. static struct pxa3xx_nand_flash micron1GbX16 = {
  219. .timing = &micron_timing,
  220. .cmdset = &largepage_cmdset,
  221. .page_per_block = 64,
  222. .page_size = 2048,
  223. .flash_width = 16,
  224. .dfc_width = 16,
  225. .num_blocks = 1024,
  226. .chip_id = 0xb12c,
  227. };
  228. static struct pxa3xx_nand_timing stm2GbX16_timing = {
  229. .tCH = 10,
  230. .tCS = 35,
  231. .tWH = 15,
  232. .tWP = 25,
  233. .tRH = 15,
  234. .tRP = 25,
  235. .tR = 25000,
  236. .tWHR = 60,
  237. .tAR = 10,
  238. };
  239. static struct pxa3xx_nand_flash stm2GbX16 = {
  240. .timing = &stm2GbX16_timing,
  241. .cmdset = &largepage_cmdset,
  242. .page_per_block = 64,
  243. .page_size = 2048,
  244. .flash_width = 16,
  245. .dfc_width = 16,
  246. .num_blocks = 2048,
  247. .chip_id = 0xba20,
  248. };
  249. static struct pxa3xx_nand_flash *builtin_flash_types[] = {
  250. &samsung512MbX16,
  251. &micron1GbX8,
  252. &micron1GbX16,
  253. &stm2GbX16,
  254. };
  255. #endif /* CONFIG_MTD_NAND_PXA3xx_BUILTIN */
  256. #define NDTR0_tCH(c) (min((c), 7) << 19)
  257. #define NDTR0_tCS(c) (min((c), 7) << 16)
  258. #define NDTR0_tWH(c) (min((c), 7) << 11)
  259. #define NDTR0_tWP(c) (min((c), 7) << 8)
  260. #define NDTR0_tRH(c) (min((c), 7) << 3)
  261. #define NDTR0_tRP(c) (min((c), 7) << 0)
  262. #define NDTR1_tR(c) (min((c), 65535) << 16)
  263. #define NDTR1_tWHR(c) (min((c), 15) << 4)
  264. #define NDTR1_tAR(c) (min((c), 15) << 0)
  265. #define tCH_NDTR0(r) (((r) >> 19) & 0x7)
  266. #define tCS_NDTR0(r) (((r) >> 16) & 0x7)
  267. #define tWH_NDTR0(r) (((r) >> 11) & 0x7)
  268. #define tWP_NDTR0(r) (((r) >> 8) & 0x7)
  269. #define tRH_NDTR0(r) (((r) >> 3) & 0x7)
  270. #define tRP_NDTR0(r) (((r) >> 0) & 0x7)
  271. #define tR_NDTR1(r) (((r) >> 16) & 0xffff)
  272. #define tWHR_NDTR1(r) (((r) >> 4) & 0xf)
  273. #define tAR_NDTR1(r) (((r) >> 0) & 0xf)
  274. /* convert nano-seconds to nand flash controller clock cycles */
  275. #define ns2cycle(ns, clk) (int)(((ns) * (clk / 1000000) / 1000) - 1)
  276. /* convert nand flash controller clock cycles to nano-seconds */
  277. #define cycle2ns(c, clk) ((((c) + 1) * 1000000 + clk / 500) / (clk / 1000))
  278. static void pxa3xx_nand_set_timing(struct pxa3xx_nand_info *info,
  279. const struct pxa3xx_nand_timing *t)
  280. {
  281. unsigned long nand_clk = clk_get_rate(info->clk);
  282. uint32_t ndtr0, ndtr1;
  283. ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) |
  284. NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) |
  285. NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) |
  286. NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) |
  287. NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) |
  288. NDTR0_tRP(ns2cycle(t->tRP, nand_clk));
  289. ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) |
  290. NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) |
  291. NDTR1_tAR(ns2cycle(t->tAR, nand_clk));
  292. nand_writel(info, NDTR0CS0, ndtr0);
  293. nand_writel(info, NDTR1CS0, ndtr1);
  294. }
  295. #define WAIT_EVENT_TIMEOUT 10
  296. static int wait_for_event(struct pxa3xx_nand_info *info, uint32_t event)
  297. {
  298. int timeout = WAIT_EVENT_TIMEOUT;
  299. uint32_t ndsr;
  300. while (timeout--) {
  301. ndsr = nand_readl(info, NDSR) & NDSR_MASK;
  302. if (ndsr & event) {
  303. nand_writel(info, NDSR, ndsr);
  304. return 0;
  305. }
  306. udelay(10);
  307. }
  308. return -ETIMEDOUT;
  309. }
  310. static int prepare_read_prog_cmd(struct pxa3xx_nand_info *info,
  311. uint16_t cmd, int column, int page_addr)
  312. {
  313. const struct pxa3xx_nand_flash *f = info->flash_info;
  314. const struct pxa3xx_nand_cmdset *cmdset = f->cmdset;
  315. /* calculate data size */
  316. switch (f->page_size) {
  317. case 2048:
  318. info->data_size = (info->use_ecc) ? 2088 : 2112;
  319. break;
  320. case 512:
  321. info->data_size = (info->use_ecc) ? 520 : 528;
  322. break;
  323. default:
  324. return -EINVAL;
  325. }
  326. /* generate values for NDCBx registers */
  327. info->ndcb0 = cmd | ((cmd & 0xff00) ? NDCB0_DBC : 0);
  328. info->ndcb1 = 0;
  329. info->ndcb2 = 0;
  330. info->ndcb0 |= NDCB0_ADDR_CYC(info->row_addr_cycles + info->col_addr_cycles);
  331. if (info->col_addr_cycles == 2) {
  332. /* large block, 2 cycles for column address
  333. * row address starts from 3rd cycle
  334. */
  335. info->ndcb1 |= page_addr << 16;
  336. if (info->row_addr_cycles == 3)
  337. info->ndcb2 = (page_addr >> 16) & 0xff;
  338. } else
  339. /* small block, 1 cycles for column address
  340. * row address starts from 2nd cycle
  341. */
  342. info->ndcb1 = page_addr << 8;
  343. if (cmd == cmdset->program)
  344. info->ndcb0 |= NDCB0_CMD_TYPE(1) | NDCB0_AUTO_RS;
  345. return 0;
  346. }
  347. static int prepare_erase_cmd(struct pxa3xx_nand_info *info,
  348. uint16_t cmd, int page_addr)
  349. {
  350. info->ndcb0 = cmd | ((cmd & 0xff00) ? NDCB0_DBC : 0);
  351. info->ndcb0 |= NDCB0_CMD_TYPE(2) | NDCB0_AUTO_RS | NDCB0_ADDR_CYC(3);
  352. info->ndcb1 = page_addr;
  353. info->ndcb2 = 0;
  354. return 0;
  355. }
  356. static int prepare_other_cmd(struct pxa3xx_nand_info *info, uint16_t cmd)
  357. {
  358. const struct pxa3xx_nand_cmdset *cmdset = info->flash_info->cmdset;
  359. info->ndcb0 = cmd | ((cmd & 0xff00) ? NDCB0_DBC : 0);
  360. info->ndcb1 = 0;
  361. info->ndcb2 = 0;
  362. if (cmd == cmdset->read_id) {
  363. info->ndcb0 |= NDCB0_CMD_TYPE(3);
  364. info->data_size = 8;
  365. } else if (cmd == cmdset->read_status) {
  366. info->ndcb0 |= NDCB0_CMD_TYPE(4);
  367. info->data_size = 8;
  368. } else if (cmd == cmdset->reset || cmd == cmdset->lock ||
  369. cmd == cmdset->unlock) {
  370. info->ndcb0 |= NDCB0_CMD_TYPE(5);
  371. } else
  372. return -EINVAL;
  373. return 0;
  374. }
  375. static void enable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
  376. {
  377. uint32_t ndcr;
  378. ndcr = nand_readl(info, NDCR);
  379. nand_writel(info, NDCR, ndcr & ~int_mask);
  380. }
  381. static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
  382. {
  383. uint32_t ndcr;
  384. ndcr = nand_readl(info, NDCR);
  385. nand_writel(info, NDCR, ndcr | int_mask);
  386. }
  387. /* NOTE: it is a must to set ND_RUN firstly, then write command buffer
  388. * otherwise, it does not work
  389. */
  390. static int write_cmd(struct pxa3xx_nand_info *info)
  391. {
  392. uint32_t ndcr;
  393. /* clear status bits and run */
  394. nand_writel(info, NDSR, NDSR_MASK);
  395. ndcr = info->reg_ndcr;
  396. ndcr |= info->use_ecc ? NDCR_ECC_EN : 0;
  397. ndcr |= info->use_dma ? NDCR_DMA_EN : 0;
  398. ndcr |= NDCR_ND_RUN;
  399. nand_writel(info, NDCR, ndcr);
  400. if (wait_for_event(info, NDSR_WRCMDREQ)) {
  401. printk(KERN_ERR "timed out writing command\n");
  402. return -ETIMEDOUT;
  403. }
  404. nand_writel(info, NDCB0, info->ndcb0);
  405. nand_writel(info, NDCB0, info->ndcb1);
  406. nand_writel(info, NDCB0, info->ndcb2);
  407. return 0;
  408. }
  409. static int handle_data_pio(struct pxa3xx_nand_info *info)
  410. {
  411. int ret, timeout = CHIP_DELAY_TIMEOUT;
  412. switch (info->state) {
  413. case STATE_PIO_WRITING:
  414. __raw_writesl(info->mmio_base + NDDB, info->data_buff,
  415. info->data_size << 2);
  416. enable_int(info, NDSR_CS0_BBD | NDSR_CS0_CMDD);
  417. ret = wait_for_completion_timeout(&info->cmd_complete, timeout);
  418. if (!ret) {
  419. printk(KERN_ERR "program command time out\n");
  420. return -1;
  421. }
  422. break;
  423. case STATE_PIO_READING:
  424. __raw_readsl(info->mmio_base + NDDB, info->data_buff,
  425. info->data_size << 2);
  426. break;
  427. default:
  428. printk(KERN_ERR "%s: invalid state %d\n", __func__,
  429. info->state);
  430. return -EINVAL;
  431. }
  432. info->state = STATE_READY;
  433. return 0;
  434. }
  435. static void start_data_dma(struct pxa3xx_nand_info *info, int dir_out)
  436. {
  437. struct pxa_dma_desc *desc = info->data_desc;
  438. int dma_len = ALIGN(info->data_size, 32);
  439. desc->ddadr = DDADR_STOP;
  440. desc->dcmd = DCMD_ENDIRQEN | DCMD_WIDTH4 | DCMD_BURST32 | dma_len;
  441. if (dir_out) {
  442. desc->dsadr = info->data_buff_phys;
  443. desc->dtadr = NDDB_DMA_ADDR;
  444. desc->dcmd |= DCMD_INCSRCADDR | DCMD_FLOWTRG;
  445. } else {
  446. desc->dtadr = info->data_buff_phys;
  447. desc->dsadr = NDDB_DMA_ADDR;
  448. desc->dcmd |= DCMD_INCTRGADDR | DCMD_FLOWSRC;
  449. }
  450. DRCMR(info->drcmr_dat) = DRCMR_MAPVLD | info->data_dma_ch;
  451. DDADR(info->data_dma_ch) = info->data_desc_addr;
  452. DCSR(info->data_dma_ch) |= DCSR_RUN;
  453. }
  454. static void pxa3xx_nand_data_dma_irq(int channel, void *data)
  455. {
  456. struct pxa3xx_nand_info *info = data;
  457. uint32_t dcsr;
  458. dcsr = DCSR(channel);
  459. DCSR(channel) = dcsr;
  460. if (dcsr & DCSR_BUSERR) {
  461. info->retcode = ERR_DMABUSERR;
  462. complete(&info->cmd_complete);
  463. }
  464. if (info->state == STATE_DMA_WRITING) {
  465. info->state = STATE_DMA_DONE;
  466. enable_int(info, NDSR_CS0_BBD | NDSR_CS0_CMDD);
  467. } else {
  468. info->state = STATE_READY;
  469. complete(&info->cmd_complete);
  470. }
  471. }
  472. static irqreturn_t pxa3xx_nand_irq(int irq, void *devid)
  473. {
  474. struct pxa3xx_nand_info *info = devid;
  475. unsigned int status;
  476. status = nand_readl(info, NDSR);
  477. if (status & (NDSR_RDDREQ | NDSR_DBERR | NDSR_SBERR)) {
  478. if (status & NDSR_DBERR)
  479. info->retcode = ERR_DBERR;
  480. else if (status & NDSR_SBERR)
  481. info->retcode = ERR_SBERR;
  482. disable_int(info, NDSR_RDDREQ | NDSR_DBERR | NDSR_SBERR);
  483. if (info->use_dma) {
  484. info->state = STATE_DMA_READING;
  485. start_data_dma(info, 0);
  486. } else {
  487. info->state = STATE_PIO_READING;
  488. complete(&info->cmd_complete);
  489. }
  490. } else if (status & NDSR_WRDREQ) {
  491. disable_int(info, NDSR_WRDREQ);
  492. if (info->use_dma) {
  493. info->state = STATE_DMA_WRITING;
  494. start_data_dma(info, 1);
  495. } else {
  496. info->state = STATE_PIO_WRITING;
  497. complete(&info->cmd_complete);
  498. }
  499. } else if (status & (NDSR_CS0_BBD | NDSR_CS0_CMDD)) {
  500. if (status & NDSR_CS0_BBD)
  501. info->retcode = ERR_BBERR;
  502. disable_int(info, NDSR_CS0_BBD | NDSR_CS0_CMDD);
  503. info->state = STATE_READY;
  504. complete(&info->cmd_complete);
  505. }
  506. nand_writel(info, NDSR, status);
  507. return IRQ_HANDLED;
  508. }
  509. static int pxa3xx_nand_do_cmd(struct pxa3xx_nand_info *info, uint32_t event)
  510. {
  511. uint32_t ndcr;
  512. int ret, timeout = CHIP_DELAY_TIMEOUT;
  513. if (write_cmd(info)) {
  514. info->retcode = ERR_SENDCMD;
  515. goto fail_stop;
  516. }
  517. info->state = STATE_CMD_HANDLE;
  518. enable_int(info, event);
  519. ret = wait_for_completion_timeout(&info->cmd_complete, timeout);
  520. if (!ret) {
  521. printk(KERN_ERR "command execution timed out\n");
  522. info->retcode = ERR_SENDCMD;
  523. goto fail_stop;
  524. }
  525. if (info->use_dma == 0 && info->data_size > 0)
  526. if (handle_data_pio(info))
  527. goto fail_stop;
  528. return 0;
  529. fail_stop:
  530. ndcr = nand_readl(info, NDCR);
  531. nand_writel(info, NDCR, ndcr & ~NDCR_ND_RUN);
  532. udelay(10);
  533. return -ETIMEDOUT;
  534. }
  535. static int pxa3xx_nand_dev_ready(struct mtd_info *mtd)
  536. {
  537. struct pxa3xx_nand_info *info = mtd->priv;
  538. return (nand_readl(info, NDSR) & NDSR_RDY) ? 1 : 0;
  539. }
  540. static inline int is_buf_blank(uint8_t *buf, size_t len)
  541. {
  542. for (; len > 0; len--)
  543. if (*buf++ != 0xff)
  544. return 0;
  545. return 1;
  546. }
  547. static void pxa3xx_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
  548. int column, int page_addr)
  549. {
  550. struct pxa3xx_nand_info *info = mtd->priv;
  551. const struct pxa3xx_nand_flash *flash_info = info->flash_info;
  552. const struct pxa3xx_nand_cmdset *cmdset = flash_info->cmdset;
  553. int ret;
  554. info->use_dma = (use_dma) ? 1 : 0;
  555. info->use_ecc = 0;
  556. info->data_size = 0;
  557. info->state = STATE_READY;
  558. init_completion(&info->cmd_complete);
  559. switch (command) {
  560. case NAND_CMD_READOOB:
  561. /* disable HW ECC to get all the OOB data */
  562. info->buf_count = mtd->writesize + mtd->oobsize;
  563. info->buf_start = mtd->writesize + column;
  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. ret = pxa3xx_nand_init_buff(info);
  1028. if (ret)
  1029. goto fail_free_io;
  1030. ret = request_irq(IRQ_NAND, pxa3xx_nand_irq, IRQF_DISABLED,
  1031. pdev->name, info);
  1032. if (ret < 0) {
  1033. dev_err(&pdev->dev, "failed to request IRQ\n");
  1034. goto fail_free_buf;
  1035. }
  1036. ret = pxa3xx_nand_detect_flash(info, pdata);
  1037. if (ret) {
  1038. dev_err(&pdev->dev, "failed to detect flash\n");
  1039. ret = -ENODEV;
  1040. goto fail_free_irq;
  1041. }
  1042. pxa3xx_nand_init_mtd(mtd, info);
  1043. platform_set_drvdata(pdev, mtd);
  1044. if (nand_scan(mtd, 1)) {
  1045. dev_err(&pdev->dev, "failed to scan nand\n");
  1046. ret = -ENXIO;
  1047. goto fail_free_irq;
  1048. }
  1049. return add_mtd_partitions(mtd, pdata->parts, pdata->nr_parts);
  1050. fail_free_irq:
  1051. free_irq(IRQ_NAND, info);
  1052. fail_free_buf:
  1053. if (use_dma) {
  1054. pxa_free_dma(info->data_dma_ch);
  1055. dma_free_coherent(&pdev->dev, info->data_buff_size,
  1056. info->data_buff, info->data_buff_phys);
  1057. } else
  1058. kfree(info->data_buff);
  1059. fail_free_io:
  1060. iounmap(info->mmio_base);
  1061. fail_free_res:
  1062. release_mem_region(r->start, resource_size(r));
  1063. fail_put_clk:
  1064. clk_disable(info->clk);
  1065. clk_put(info->clk);
  1066. fail_free_mtd:
  1067. kfree(mtd);
  1068. return ret;
  1069. }
  1070. static int pxa3xx_nand_remove(struct platform_device *pdev)
  1071. {
  1072. struct mtd_info *mtd = platform_get_drvdata(pdev);
  1073. struct pxa3xx_nand_info *info = mtd->priv;
  1074. struct resource *r;
  1075. platform_set_drvdata(pdev, NULL);
  1076. del_mtd_device(mtd);
  1077. del_mtd_partitions(mtd);
  1078. free_irq(IRQ_NAND, info);
  1079. if (use_dma) {
  1080. pxa_free_dma(info->data_dma_ch);
  1081. dma_free_writecombine(&pdev->dev, info->data_buff_size,
  1082. info->data_buff, info->data_buff_phys);
  1083. } else
  1084. kfree(info->data_buff);
  1085. iounmap(info->mmio_base);
  1086. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1087. release_mem_region(r->start, resource_size(r));
  1088. clk_disable(info->clk);
  1089. clk_put(info->clk);
  1090. kfree(mtd);
  1091. return 0;
  1092. }
  1093. #ifdef CONFIG_PM
  1094. static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state)
  1095. {
  1096. struct mtd_info *mtd = (struct mtd_info *)platform_get_drvdata(pdev);
  1097. struct pxa3xx_nand_info *info = mtd->priv;
  1098. if (info->state != STATE_READY) {
  1099. dev_err(&pdev->dev, "driver busy, state = %d\n", info->state);
  1100. return -EAGAIN;
  1101. }
  1102. return 0;
  1103. }
  1104. static int pxa3xx_nand_resume(struct platform_device *pdev)
  1105. {
  1106. struct mtd_info *mtd = (struct mtd_info *)platform_get_drvdata(pdev);
  1107. struct pxa3xx_nand_info *info = mtd->priv;
  1108. clk_enable(info->clk);
  1109. return pxa3xx_nand_config_flash(info, info->flash_info);
  1110. }
  1111. #else
  1112. #define pxa3xx_nand_suspend NULL
  1113. #define pxa3xx_nand_resume NULL
  1114. #endif
  1115. static struct platform_driver pxa3xx_nand_driver = {
  1116. .driver = {
  1117. .name = "pxa3xx-nand",
  1118. },
  1119. .probe = pxa3xx_nand_probe,
  1120. .remove = pxa3xx_nand_remove,
  1121. .suspend = pxa3xx_nand_suspend,
  1122. .resume = pxa3xx_nand_resume,
  1123. };
  1124. static int __init pxa3xx_nand_init(void)
  1125. {
  1126. return platform_driver_register(&pxa3xx_nand_driver);
  1127. }
  1128. module_init(pxa3xx_nand_init);
  1129. static void __exit pxa3xx_nand_exit(void)
  1130. {
  1131. platform_driver_unregister(&pxa3xx_nand_driver);
  1132. }
  1133. module_exit(pxa3xx_nand_exit);
  1134. MODULE_LICENSE("GPL");
  1135. MODULE_DESCRIPTION("PXA3xx NAND controller driver");