pxa3xx_nand.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367
  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. };
  94. enum {
  95. STATE_READY = 0,
  96. STATE_CMD_HANDLE,
  97. STATE_DMA_READING,
  98. STATE_DMA_WRITING,
  99. STATE_DMA_DONE,
  100. STATE_PIO_READING,
  101. STATE_PIO_WRITING,
  102. };
  103. struct pxa3xx_nand_info {
  104. struct nand_chip nand_chip;
  105. struct platform_device *pdev;
  106. const struct pxa3xx_nand_flash *flash_info;
  107. struct clk *clk;
  108. void __iomem *mmio_base;
  109. unsigned 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 = 25,
  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. info->data_size << 2);
  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. info->data_size << 2);
  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 = NDDB_DMA_ADDR;
  443. desc->dcmd |= DCMD_INCSRCADDR | DCMD_FLOWTRG;
  444. } else {
  445. desc->dtadr = info->data_buff_phys;
  446. desc->dsadr = NDDB_DMA_ADDR;
  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)) {
  477. if (status & NDSR_DBERR)
  478. info->retcode = ERR_DBERR;
  479. disable_int(info, NDSR_RDDREQ | NDSR_DBERR);
  480. if (info->use_dma) {
  481. info->state = STATE_DMA_READING;
  482. start_data_dma(info, 0);
  483. } else {
  484. info->state = STATE_PIO_READING;
  485. complete(&info->cmd_complete);
  486. }
  487. } else if (status & NDSR_WRDREQ) {
  488. disable_int(info, NDSR_WRDREQ);
  489. if (info->use_dma) {
  490. info->state = STATE_DMA_WRITING;
  491. start_data_dma(info, 1);
  492. } else {
  493. info->state = STATE_PIO_WRITING;
  494. complete(&info->cmd_complete);
  495. }
  496. } else if (status & (NDSR_CS0_BBD | NDSR_CS0_CMDD)) {
  497. if (status & NDSR_CS0_BBD)
  498. info->retcode = ERR_BBERR;
  499. disable_int(info, NDSR_CS0_BBD | NDSR_CS0_CMDD);
  500. info->state = STATE_READY;
  501. complete(&info->cmd_complete);
  502. }
  503. nand_writel(info, NDSR, status);
  504. return IRQ_HANDLED;
  505. }
  506. static int pxa3xx_nand_do_cmd(struct pxa3xx_nand_info *info, uint32_t event)
  507. {
  508. uint32_t ndcr;
  509. int ret, timeout = CHIP_DELAY_TIMEOUT;
  510. if (write_cmd(info)) {
  511. info->retcode = ERR_SENDCMD;
  512. goto fail_stop;
  513. }
  514. info->state = STATE_CMD_HANDLE;
  515. enable_int(info, event);
  516. ret = wait_for_completion_timeout(&info->cmd_complete, timeout);
  517. if (!ret) {
  518. printk(KERN_ERR "command execution timed out\n");
  519. info->retcode = ERR_SENDCMD;
  520. goto fail_stop;
  521. }
  522. if (info->use_dma == 0 && info->data_size > 0)
  523. if (handle_data_pio(info))
  524. goto fail_stop;
  525. return 0;
  526. fail_stop:
  527. ndcr = nand_readl(info, NDCR);
  528. nand_writel(info, NDCR, ndcr & ~NDCR_ND_RUN);
  529. udelay(10);
  530. return -ETIMEDOUT;
  531. }
  532. static int pxa3xx_nand_dev_ready(struct mtd_info *mtd)
  533. {
  534. struct pxa3xx_nand_info *info = mtd->priv;
  535. return (nand_readl(info, NDSR) & NDSR_RDY) ? 1 : 0;
  536. }
  537. static inline int is_buf_blank(uint8_t *buf, size_t len)
  538. {
  539. for (; len > 0; len--)
  540. if (*buf++ != 0xff)
  541. return 0;
  542. return 1;
  543. }
  544. static void pxa3xx_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
  545. int column, int page_addr)
  546. {
  547. struct pxa3xx_nand_info *info = mtd->priv;
  548. const struct pxa3xx_nand_flash *flash_info = info->flash_info;
  549. const struct pxa3xx_nand_cmdset *cmdset = flash_info->cmdset;
  550. int ret;
  551. info->use_dma = (use_dma) ? 1 : 0;
  552. info->use_ecc = 0;
  553. info->data_size = 0;
  554. info->state = STATE_READY;
  555. init_completion(&info->cmd_complete);
  556. switch (command) {
  557. case NAND_CMD_READOOB:
  558. /* disable HW ECC to get all the OOB data */
  559. info->buf_count = mtd->writesize + mtd->oobsize;
  560. info->buf_start = mtd->writesize + column;
  561. if (prepare_read_prog_cmd(info, cmdset->read1, column, page_addr))
  562. break;
  563. pxa3xx_nand_do_cmd(info, NDSR_RDDREQ | NDSR_DBERR);
  564. /* We only are OOB, so if the data has error, does not matter */
  565. if (info->retcode == ERR_DBERR)
  566. info->retcode = ERR_NONE;
  567. break;
  568. case NAND_CMD_READ0:
  569. info->use_ecc = 1;
  570. info->retcode = ERR_NONE;
  571. info->buf_start = column;
  572. info->buf_count = mtd->writesize + mtd->oobsize;
  573. memset(info->data_buff, 0xFF, info->buf_count);
  574. if (prepare_read_prog_cmd(info, cmdset->read1, column, page_addr))
  575. break;
  576. pxa3xx_nand_do_cmd(info, NDSR_RDDREQ | NDSR_DBERR);
  577. if (info->retcode == ERR_DBERR) {
  578. /* for blank page (all 0xff), HW will calculate its ECC as
  579. * 0, which is different from the ECC information within
  580. * OOB, ignore such double bit errors
  581. */
  582. if (is_buf_blank(info->data_buff, mtd->writesize))
  583. info->retcode = ERR_NONE;
  584. }
  585. break;
  586. case NAND_CMD_SEQIN:
  587. info->buf_start = column;
  588. info->buf_count = mtd->writesize + mtd->oobsize;
  589. memset(info->data_buff, 0xff, info->buf_count);
  590. /* save column/page_addr for next CMD_PAGEPROG */
  591. info->seqin_column = column;
  592. info->seqin_page_addr = page_addr;
  593. break;
  594. case NAND_CMD_PAGEPROG:
  595. info->use_ecc = (info->seqin_column >= mtd->writesize) ? 0 : 1;
  596. if (prepare_read_prog_cmd(info, cmdset->program,
  597. info->seqin_column, info->seqin_page_addr))
  598. break;
  599. pxa3xx_nand_do_cmd(info, NDSR_WRDREQ);
  600. break;
  601. case NAND_CMD_ERASE1:
  602. if (prepare_erase_cmd(info, cmdset->erase, page_addr))
  603. break;
  604. pxa3xx_nand_do_cmd(info, NDSR_CS0_BBD | NDSR_CS0_CMDD);
  605. break;
  606. case NAND_CMD_ERASE2:
  607. break;
  608. case NAND_CMD_READID:
  609. case NAND_CMD_STATUS:
  610. info->use_dma = 0; /* force PIO read */
  611. info->buf_start = 0;
  612. info->buf_count = (command == NAND_CMD_READID) ?
  613. info->read_id_bytes : 1;
  614. if (prepare_other_cmd(info, (command == NAND_CMD_READID) ?
  615. cmdset->read_id : cmdset->read_status))
  616. break;
  617. pxa3xx_nand_do_cmd(info, NDSR_RDDREQ);
  618. break;
  619. case NAND_CMD_RESET:
  620. if (prepare_other_cmd(info, cmdset->reset))
  621. break;
  622. ret = pxa3xx_nand_do_cmd(info, NDSR_CS0_CMDD);
  623. if (ret == 0) {
  624. int timeout = 2;
  625. uint32_t ndcr;
  626. while (timeout--) {
  627. if (nand_readl(info, NDSR) & NDSR_RDY)
  628. break;
  629. msleep(10);
  630. }
  631. ndcr = nand_readl(info, NDCR);
  632. nand_writel(info, NDCR, ndcr & ~NDCR_ND_RUN);
  633. }
  634. break;
  635. default:
  636. printk(KERN_ERR "non-supported command.\n");
  637. break;
  638. }
  639. if (info->retcode == ERR_DBERR) {
  640. printk(KERN_ERR "double bit error @ page %08x\n", page_addr);
  641. info->retcode = ERR_NONE;
  642. }
  643. }
  644. static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
  645. {
  646. struct pxa3xx_nand_info *info = mtd->priv;
  647. char retval = 0xFF;
  648. if (info->buf_start < info->buf_count)
  649. /* Has just send a new command? */
  650. retval = info->data_buff[info->buf_start++];
  651. return retval;
  652. }
  653. static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
  654. {
  655. struct pxa3xx_nand_info *info = mtd->priv;
  656. u16 retval = 0xFFFF;
  657. if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
  658. retval = *((u16 *)(info->data_buff+info->buf_start));
  659. info->buf_start += 2;
  660. }
  661. return retval;
  662. }
  663. static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  664. {
  665. struct pxa3xx_nand_info *info = mtd->priv;
  666. int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
  667. memcpy(buf, info->data_buff + info->buf_start, real_len);
  668. info->buf_start += real_len;
  669. }
  670. static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
  671. const uint8_t *buf, int len)
  672. {
  673. struct pxa3xx_nand_info *info = mtd->priv;
  674. int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
  675. memcpy(info->data_buff + info->buf_start, buf, real_len);
  676. info->buf_start += real_len;
  677. }
  678. static int pxa3xx_nand_verify_buf(struct mtd_info *mtd,
  679. const uint8_t *buf, int len)
  680. {
  681. return 0;
  682. }
  683. static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
  684. {
  685. return;
  686. }
  687. static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
  688. {
  689. struct pxa3xx_nand_info *info = mtd->priv;
  690. /* pxa3xx_nand_send_command has waited for command complete */
  691. if (this->state == FL_WRITING || this->state == FL_ERASING) {
  692. if (info->retcode == ERR_NONE)
  693. return 0;
  694. else {
  695. /*
  696. * any error make it return 0x01 which will tell
  697. * the caller the erase and write fail
  698. */
  699. return 0x01;
  700. }
  701. }
  702. return 0;
  703. }
  704. static void pxa3xx_nand_ecc_hwctl(struct mtd_info *mtd, int mode)
  705. {
  706. return;
  707. }
  708. static int pxa3xx_nand_ecc_calculate(struct mtd_info *mtd,
  709. const uint8_t *dat, uint8_t *ecc_code)
  710. {
  711. return 0;
  712. }
  713. static int pxa3xx_nand_ecc_correct(struct mtd_info *mtd,
  714. uint8_t *dat, uint8_t *read_ecc, uint8_t *calc_ecc)
  715. {
  716. struct pxa3xx_nand_info *info = mtd->priv;
  717. /*
  718. * Any error include ERR_SEND_CMD, ERR_DBERR, ERR_BUSERR, we
  719. * consider it as a ecc error which will tell the caller the
  720. * read fail We have distinguish all the errors, but the
  721. * nand_read_ecc only check this function return value
  722. */
  723. if (info->retcode != ERR_NONE)
  724. return -1;
  725. return 0;
  726. }
  727. static int __readid(struct pxa3xx_nand_info *info, uint32_t *id)
  728. {
  729. const struct pxa3xx_nand_flash *f = info->flash_info;
  730. const struct pxa3xx_nand_cmdset *cmdset = f->cmdset;
  731. uint32_t ndcr;
  732. uint8_t id_buff[8];
  733. if (prepare_other_cmd(info, cmdset->read_id)) {
  734. printk(KERN_ERR "failed to prepare command\n");
  735. return -EINVAL;
  736. }
  737. /* Send command */
  738. if (write_cmd(info))
  739. goto fail_timeout;
  740. /* Wait for CMDDM(command done successfully) */
  741. if (wait_for_event(info, NDSR_RDDREQ))
  742. goto fail_timeout;
  743. __raw_readsl(info->mmio_base + NDDB, id_buff, 2);
  744. *id = id_buff[0] | (id_buff[1] << 8);
  745. return 0;
  746. fail_timeout:
  747. ndcr = nand_readl(info, NDCR);
  748. nand_writel(info, NDCR, ndcr & ~NDCR_ND_RUN);
  749. udelay(10);
  750. return -ETIMEDOUT;
  751. }
  752. static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info,
  753. const struct pxa3xx_nand_flash *f)
  754. {
  755. struct platform_device *pdev = info->pdev;
  756. struct pxa3xx_nand_platform_data *pdata = pdev->dev.platform_data;
  757. uint32_t ndcr = 0x00000FFF; /* disable all interrupts */
  758. if (f->page_size != 2048 && f->page_size != 512)
  759. return -EINVAL;
  760. if (f->flash_width != 16 && f->flash_width != 8)
  761. return -EINVAL;
  762. /* calculate flash information */
  763. info->oob_size = (f->page_size == 2048) ? 64 : 16;
  764. info->read_id_bytes = (f->page_size == 2048) ? 4 : 2;
  765. /* calculate addressing information */
  766. info->col_addr_cycles = (f->page_size == 2048) ? 2 : 1;
  767. if (f->num_blocks * f->page_per_block > 65536)
  768. info->row_addr_cycles = 3;
  769. else
  770. info->row_addr_cycles = 2;
  771. ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
  772. ndcr |= (info->col_addr_cycles == 2) ? NDCR_RA_START : 0;
  773. ndcr |= (f->page_per_block == 64) ? NDCR_PG_PER_BLK : 0;
  774. ndcr |= (f->page_size == 2048) ? NDCR_PAGE_SZ : 0;
  775. ndcr |= (f->flash_width == 16) ? NDCR_DWIDTH_M : 0;
  776. ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0;
  777. ndcr |= NDCR_RD_ID_CNT(info->read_id_bytes);
  778. ndcr |= NDCR_SPARE_EN; /* enable spare by default */
  779. info->reg_ndcr = ndcr;
  780. pxa3xx_nand_set_timing(info, f->timing);
  781. info->flash_info = f;
  782. return 0;
  783. }
  784. static void pxa3xx_nand_detect_timing(struct pxa3xx_nand_info *info,
  785. struct pxa3xx_nand_timing *t)
  786. {
  787. unsigned long nand_clk = clk_get_rate(info->clk);
  788. uint32_t ndtr0 = nand_readl(info, NDTR0CS0);
  789. uint32_t ndtr1 = nand_readl(info, NDTR1CS0);
  790. t->tCH = cycle2ns(tCH_NDTR0(ndtr0), nand_clk);
  791. t->tCS = cycle2ns(tCS_NDTR0(ndtr0), nand_clk);
  792. t->tWH = cycle2ns(tWH_NDTR0(ndtr0), nand_clk);
  793. t->tWP = cycle2ns(tWP_NDTR0(ndtr0), nand_clk);
  794. t->tRH = cycle2ns(tRH_NDTR0(ndtr0), nand_clk);
  795. t->tRP = cycle2ns(tRP_NDTR0(ndtr0), nand_clk);
  796. t->tR = cycle2ns(tR_NDTR1(ndtr1), nand_clk);
  797. t->tWHR = cycle2ns(tWHR_NDTR1(ndtr1), nand_clk);
  798. t->tAR = cycle2ns(tAR_NDTR1(ndtr1), nand_clk);
  799. }
  800. static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
  801. {
  802. uint32_t ndcr = nand_readl(info, NDCR);
  803. struct nand_flash_dev *type = NULL;
  804. uint32_t id = -1;
  805. int i;
  806. default_flash.page_per_block = ndcr & NDCR_PG_PER_BLK ? 64 : 32;
  807. default_flash.page_size = ndcr & NDCR_PAGE_SZ ? 2048 : 512;
  808. default_flash.flash_width = ndcr & NDCR_DWIDTH_M ? 16 : 8;
  809. default_flash.dfc_width = ndcr & NDCR_DWIDTH_C ? 16 : 8;
  810. if (default_flash.page_size == 2048)
  811. default_flash.cmdset = &largepage_cmdset;
  812. else
  813. default_flash.cmdset = &smallpage_cmdset;
  814. /* set info fields needed to __readid */
  815. info->flash_info = &default_flash;
  816. info->read_id_bytes = (default_flash.page_size == 2048) ? 4 : 2;
  817. info->reg_ndcr = ndcr;
  818. if (__readid(info, &id))
  819. return -ENODEV;
  820. /* Lookup the flash id */
  821. id = (id >> 8) & 0xff; /* device id is byte 2 */
  822. for (i = 0; nand_flash_ids[i].name != NULL; i++) {
  823. if (id == nand_flash_ids[i].id) {
  824. type = &nand_flash_ids[i];
  825. break;
  826. }
  827. }
  828. if (!type)
  829. return -ENODEV;
  830. /* fill the missing flash information */
  831. i = __ffs(default_flash.page_per_block * default_flash.page_size);
  832. default_flash.num_blocks = type->chipsize << (20 - i);
  833. info->oob_size = (default_flash.page_size == 2048) ? 64 : 16;
  834. /* calculate addressing information */
  835. info->col_addr_cycles = (default_flash.page_size == 2048) ? 2 : 1;
  836. if (default_flash.num_blocks * default_flash.page_per_block > 65536)
  837. info->row_addr_cycles = 3;
  838. else
  839. info->row_addr_cycles = 2;
  840. pxa3xx_nand_detect_timing(info, &default_timing);
  841. default_flash.timing = &default_timing;
  842. return 0;
  843. }
  844. static int pxa3xx_nand_detect_flash(struct pxa3xx_nand_info *info,
  845. const struct pxa3xx_nand_platform_data *pdata)
  846. {
  847. const struct pxa3xx_nand_flash *f;
  848. uint32_t id = -1;
  849. int i;
  850. if (pdata->keep_config)
  851. if (pxa3xx_nand_detect_config(info) == 0)
  852. return 0;
  853. for (i = 0; i<pdata->num_flash; ++i) {
  854. f = pdata->flash + i;
  855. if (pxa3xx_nand_config_flash(info, f))
  856. continue;
  857. if (__readid(info, &id))
  858. continue;
  859. if (id == f->chip_id)
  860. return 0;
  861. }
  862. #ifdef CONFIG_MTD_NAND_PXA3xx_BUILTIN
  863. for (i = 0; i < ARRAY_SIZE(builtin_flash_types); i++) {
  864. f = builtin_flash_types[i];
  865. if (pxa3xx_nand_config_flash(info, f))
  866. continue;
  867. if (__readid(info, &id))
  868. continue;
  869. if (id == f->chip_id)
  870. return 0;
  871. }
  872. #endif
  873. dev_warn(&info->pdev->dev,
  874. "failed to detect configured nand flash; found %04x instead of\n",
  875. id);
  876. return -ENODEV;
  877. }
  878. /* the maximum possible buffer size for large page with OOB data
  879. * is: 2048 + 64 = 2112 bytes, allocate a page here for both the
  880. * data buffer and the DMA descriptor
  881. */
  882. #define MAX_BUFF_SIZE PAGE_SIZE
  883. static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
  884. {
  885. struct platform_device *pdev = info->pdev;
  886. int data_desc_offset = MAX_BUFF_SIZE - sizeof(struct pxa_dma_desc);
  887. if (use_dma == 0) {
  888. info->data_buff = kmalloc(MAX_BUFF_SIZE, GFP_KERNEL);
  889. if (info->data_buff == NULL)
  890. return -ENOMEM;
  891. return 0;
  892. }
  893. info->data_buff = dma_alloc_coherent(&pdev->dev, MAX_BUFF_SIZE,
  894. &info->data_buff_phys, GFP_KERNEL);
  895. if (info->data_buff == NULL) {
  896. dev_err(&pdev->dev, "failed to allocate dma buffer\n");
  897. return -ENOMEM;
  898. }
  899. info->data_buff_size = MAX_BUFF_SIZE;
  900. info->data_desc = (void *)info->data_buff + data_desc_offset;
  901. info->data_desc_addr = info->data_buff_phys + data_desc_offset;
  902. info->data_dma_ch = pxa_request_dma("nand-data", DMA_PRIO_LOW,
  903. pxa3xx_nand_data_dma_irq, info);
  904. if (info->data_dma_ch < 0) {
  905. dev_err(&pdev->dev, "failed to request data dma\n");
  906. dma_free_coherent(&pdev->dev, info->data_buff_size,
  907. info->data_buff, info->data_buff_phys);
  908. return info->data_dma_ch;
  909. }
  910. return 0;
  911. }
  912. static struct nand_ecclayout hw_smallpage_ecclayout = {
  913. .eccbytes = 6,
  914. .eccpos = {8, 9, 10, 11, 12, 13 },
  915. .oobfree = { {2, 6} }
  916. };
  917. static struct nand_ecclayout hw_largepage_ecclayout = {
  918. .eccbytes = 24,
  919. .eccpos = {
  920. 40, 41, 42, 43, 44, 45, 46, 47,
  921. 48, 49, 50, 51, 52, 53, 54, 55,
  922. 56, 57, 58, 59, 60, 61, 62, 63},
  923. .oobfree = { {2, 38} }
  924. };
  925. static void pxa3xx_nand_init_mtd(struct mtd_info *mtd,
  926. struct pxa3xx_nand_info *info)
  927. {
  928. const struct pxa3xx_nand_flash *f = info->flash_info;
  929. struct nand_chip *this = &info->nand_chip;
  930. this->options = (f->flash_width == 16) ? NAND_BUSWIDTH_16: 0;
  931. this->waitfunc = pxa3xx_nand_waitfunc;
  932. this->select_chip = pxa3xx_nand_select_chip;
  933. this->dev_ready = pxa3xx_nand_dev_ready;
  934. this->cmdfunc = pxa3xx_nand_cmdfunc;
  935. this->read_word = pxa3xx_nand_read_word;
  936. this->read_byte = pxa3xx_nand_read_byte;
  937. this->read_buf = pxa3xx_nand_read_buf;
  938. this->write_buf = pxa3xx_nand_write_buf;
  939. this->verify_buf = pxa3xx_nand_verify_buf;
  940. this->ecc.mode = NAND_ECC_HW;
  941. this->ecc.hwctl = pxa3xx_nand_ecc_hwctl;
  942. this->ecc.calculate = pxa3xx_nand_ecc_calculate;
  943. this->ecc.correct = pxa3xx_nand_ecc_correct;
  944. this->ecc.size = f->page_size;
  945. if (f->page_size == 2048)
  946. this->ecc.layout = &hw_largepage_ecclayout;
  947. else
  948. this->ecc.layout = &hw_smallpage_ecclayout;
  949. this->chip_delay = 25;
  950. }
  951. static int pxa3xx_nand_probe(struct platform_device *pdev)
  952. {
  953. struct pxa3xx_nand_platform_data *pdata;
  954. struct pxa3xx_nand_info *info;
  955. struct nand_chip *this;
  956. struct mtd_info *mtd;
  957. struct resource *r;
  958. int ret = 0, irq;
  959. pdata = pdev->dev.platform_data;
  960. if (!pdata) {
  961. dev_err(&pdev->dev, "no platform data defined\n");
  962. return -ENODEV;
  963. }
  964. mtd = kzalloc(sizeof(struct mtd_info) + sizeof(struct pxa3xx_nand_info),
  965. GFP_KERNEL);
  966. if (!mtd) {
  967. dev_err(&pdev->dev, "failed to allocate memory\n");
  968. return -ENOMEM;
  969. }
  970. info = (struct pxa3xx_nand_info *)(&mtd[1]);
  971. info->pdev = pdev;
  972. this = &info->nand_chip;
  973. mtd->priv = info;
  974. mtd->owner = THIS_MODULE;
  975. info->clk = clk_get(&pdev->dev, NULL);
  976. if (IS_ERR(info->clk)) {
  977. dev_err(&pdev->dev, "failed to get nand clock\n");
  978. ret = PTR_ERR(info->clk);
  979. goto fail_free_mtd;
  980. }
  981. clk_enable(info->clk);
  982. r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  983. if (r == NULL) {
  984. dev_err(&pdev->dev, "no resource defined for data DMA\n");
  985. ret = -ENXIO;
  986. goto fail_put_clk;
  987. }
  988. info->drcmr_dat = r->start;
  989. r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
  990. if (r == NULL) {
  991. dev_err(&pdev->dev, "no resource defined for command DMA\n");
  992. ret = -ENXIO;
  993. goto fail_put_clk;
  994. }
  995. info->drcmr_cmd = r->start;
  996. irq = platform_get_irq(pdev, 0);
  997. if (irq < 0) {
  998. dev_err(&pdev->dev, "no IRQ resource defined\n");
  999. ret = -ENXIO;
  1000. goto fail_put_clk;
  1001. }
  1002. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1003. if (r == NULL) {
  1004. dev_err(&pdev->dev, "no IO memory resource defined\n");
  1005. ret = -ENODEV;
  1006. goto fail_put_clk;
  1007. }
  1008. r = request_mem_region(r->start, resource_size(r), pdev->name);
  1009. if (r == NULL) {
  1010. dev_err(&pdev->dev, "failed to request memory resource\n");
  1011. ret = -EBUSY;
  1012. goto fail_put_clk;
  1013. }
  1014. info->mmio_base = ioremap(r->start, resource_size(r));
  1015. if (info->mmio_base == NULL) {
  1016. dev_err(&pdev->dev, "ioremap() failed\n");
  1017. ret = -ENODEV;
  1018. goto fail_free_res;
  1019. }
  1020. ret = pxa3xx_nand_init_buff(info);
  1021. if (ret)
  1022. goto fail_free_io;
  1023. ret = request_irq(IRQ_NAND, pxa3xx_nand_irq, IRQF_DISABLED,
  1024. pdev->name, info);
  1025. if (ret < 0) {
  1026. dev_err(&pdev->dev, "failed to request IRQ\n");
  1027. goto fail_free_buf;
  1028. }
  1029. ret = pxa3xx_nand_detect_flash(info, pdata);
  1030. if (ret) {
  1031. dev_err(&pdev->dev, "failed to detect flash\n");
  1032. ret = -ENODEV;
  1033. goto fail_free_irq;
  1034. }
  1035. pxa3xx_nand_init_mtd(mtd, info);
  1036. platform_set_drvdata(pdev, mtd);
  1037. if (nand_scan(mtd, 1)) {
  1038. dev_err(&pdev->dev, "failed to scan nand\n");
  1039. ret = -ENXIO;
  1040. goto fail_free_irq;
  1041. }
  1042. return add_mtd_partitions(mtd, pdata->parts, pdata->nr_parts);
  1043. fail_free_irq:
  1044. free_irq(IRQ_NAND, info);
  1045. fail_free_buf:
  1046. if (use_dma) {
  1047. pxa_free_dma(info->data_dma_ch);
  1048. dma_free_coherent(&pdev->dev, info->data_buff_size,
  1049. info->data_buff, info->data_buff_phys);
  1050. } else
  1051. kfree(info->data_buff);
  1052. fail_free_io:
  1053. iounmap(info->mmio_base);
  1054. fail_free_res:
  1055. release_mem_region(r->start, resource_size(r));
  1056. fail_put_clk:
  1057. clk_disable(info->clk);
  1058. clk_put(info->clk);
  1059. fail_free_mtd:
  1060. kfree(mtd);
  1061. return ret;
  1062. }
  1063. static int pxa3xx_nand_remove(struct platform_device *pdev)
  1064. {
  1065. struct mtd_info *mtd = platform_get_drvdata(pdev);
  1066. struct pxa3xx_nand_info *info = mtd->priv;
  1067. struct resource *r;
  1068. platform_set_drvdata(pdev, NULL);
  1069. del_mtd_device(mtd);
  1070. del_mtd_partitions(mtd);
  1071. free_irq(IRQ_NAND, info);
  1072. if (use_dma) {
  1073. pxa_free_dma(info->data_dma_ch);
  1074. dma_free_writecombine(&pdev->dev, info->data_buff_size,
  1075. info->data_buff, info->data_buff_phys);
  1076. } else
  1077. kfree(info->data_buff);
  1078. iounmap(info->mmio_base);
  1079. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1080. release_mem_region(r->start, resource_size(r));
  1081. clk_disable(info->clk);
  1082. clk_put(info->clk);
  1083. kfree(mtd);
  1084. return 0;
  1085. }
  1086. #ifdef CONFIG_PM
  1087. static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state)
  1088. {
  1089. struct mtd_info *mtd = (struct mtd_info *)platform_get_drvdata(pdev);
  1090. struct pxa3xx_nand_info *info = mtd->priv;
  1091. if (info->state != STATE_READY) {
  1092. dev_err(&pdev->dev, "driver busy, state = %d\n", info->state);
  1093. return -EAGAIN;
  1094. }
  1095. return 0;
  1096. }
  1097. static int pxa3xx_nand_resume(struct platform_device *pdev)
  1098. {
  1099. struct mtd_info *mtd = (struct mtd_info *)platform_get_drvdata(pdev);
  1100. struct pxa3xx_nand_info *info = mtd->priv;
  1101. clk_enable(info->clk);
  1102. return pxa3xx_nand_config_flash(info, info->flash_info);
  1103. }
  1104. #else
  1105. #define pxa3xx_nand_suspend NULL
  1106. #define pxa3xx_nand_resume NULL
  1107. #endif
  1108. static struct platform_driver pxa3xx_nand_driver = {
  1109. .driver = {
  1110. .name = "pxa3xx-nand",
  1111. },
  1112. .probe = pxa3xx_nand_probe,
  1113. .remove = pxa3xx_nand_remove,
  1114. .suspend = pxa3xx_nand_suspend,
  1115. .resume = pxa3xx_nand_resume,
  1116. };
  1117. static int __init pxa3xx_nand_init(void)
  1118. {
  1119. return platform_driver_register(&pxa3xx_nand_driver);
  1120. }
  1121. module_init(pxa3xx_nand_init);
  1122. static void __exit pxa3xx_nand_exit(void)
  1123. {
  1124. platform_driver_unregister(&pxa3xx_nand_driver);
  1125. }
  1126. module_exit(pxa3xx_nand_exit);
  1127. MODULE_LICENSE("GPL");
  1128. MODULE_DESCRIPTION("PXA3xx NAND controller driver");