pxa3xx_nand.c 34 KB

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