pxa3xx_nand.c 35 KB

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