au1550nd.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /*
  2. * Copyright (C) 2004 Embedded Edge, LLC
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. */
  9. #include <linux/slab.h>
  10. #include <linux/module.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/mtd/mtd.h>
  13. #include <linux/mtd/rawnand.h>
  14. #include <linux/mtd/partitions.h>
  15. #include <linux/platform_device.h>
  16. #include <asm/io.h>
  17. #include <asm/mach-au1x00/au1000.h>
  18. #include <asm/mach-au1x00/au1550nd.h>
  19. struct au1550nd_ctx {
  20. struct nand_chip chip;
  21. int cs;
  22. void __iomem *base;
  23. void (*write_byte)(struct nand_chip *, u_char);
  24. };
  25. /**
  26. * au_read_byte - read one byte from the chip
  27. * @this: NAND chip object
  28. *
  29. * read function for 8bit buswidth
  30. */
  31. static u_char au_read_byte(struct nand_chip *this)
  32. {
  33. u_char ret = readb(this->legacy.IO_ADDR_R);
  34. wmb(); /* drain writebuffer */
  35. return ret;
  36. }
  37. /**
  38. * au_write_byte - write one byte to the chip
  39. * @this: NAND chip object
  40. * @byte: pointer to data byte to write
  41. *
  42. * write function for 8it buswidth
  43. */
  44. static void au_write_byte(struct nand_chip *this, u_char byte)
  45. {
  46. writeb(byte, this->legacy.IO_ADDR_W);
  47. wmb(); /* drain writebuffer */
  48. }
  49. /**
  50. * au_read_byte16 - read one byte endianness aware from the chip
  51. * @this: NAND chip object
  52. *
  53. * read function for 16bit buswidth with endianness conversion
  54. */
  55. static u_char au_read_byte16(struct nand_chip *this)
  56. {
  57. u_char ret = (u_char) cpu_to_le16(readw(this->legacy.IO_ADDR_R));
  58. wmb(); /* drain writebuffer */
  59. return ret;
  60. }
  61. /**
  62. * au_write_byte16 - write one byte endianness aware to the chip
  63. * @this: NAND chip object
  64. * @byte: pointer to data byte to write
  65. *
  66. * write function for 16bit buswidth with endianness conversion
  67. */
  68. static void au_write_byte16(struct nand_chip *this, u_char byte)
  69. {
  70. writew(le16_to_cpu((u16) byte), this->legacy.IO_ADDR_W);
  71. wmb(); /* drain writebuffer */
  72. }
  73. /**
  74. * au_write_buf - write buffer to chip
  75. * @this: NAND chip object
  76. * @buf: data buffer
  77. * @len: number of bytes to write
  78. *
  79. * write function for 8bit buswidth
  80. */
  81. static void au_write_buf(struct nand_chip *this, const u_char *buf, int len)
  82. {
  83. int i;
  84. for (i = 0; i < len; i++) {
  85. writeb(buf[i], this->legacy.IO_ADDR_W);
  86. wmb(); /* drain writebuffer */
  87. }
  88. }
  89. /**
  90. * au_read_buf - read chip data into buffer
  91. * @this: NAND chip object
  92. * @buf: buffer to store date
  93. * @len: number of bytes to read
  94. *
  95. * read function for 8bit buswidth
  96. */
  97. static void au_read_buf(struct nand_chip *this, u_char *buf, int len)
  98. {
  99. int i;
  100. for (i = 0; i < len; i++) {
  101. buf[i] = readb(this->legacy.IO_ADDR_R);
  102. wmb(); /* drain writebuffer */
  103. }
  104. }
  105. /**
  106. * au_write_buf16 - write buffer to chip
  107. * @this: NAND chip object
  108. * @buf: data buffer
  109. * @len: number of bytes to write
  110. *
  111. * write function for 16bit buswidth
  112. */
  113. static void au_write_buf16(struct nand_chip *this, const u_char *buf, int len)
  114. {
  115. int i;
  116. u16 *p = (u16 *) buf;
  117. len >>= 1;
  118. for (i = 0; i < len; i++) {
  119. writew(p[i], this->legacy.IO_ADDR_W);
  120. wmb(); /* drain writebuffer */
  121. }
  122. }
  123. /**
  124. * au_read_buf16 - read chip data into buffer
  125. * @mtd: MTD device structure
  126. * @buf: buffer to store date
  127. * @len: number of bytes to read
  128. *
  129. * read function for 16bit buswidth
  130. */
  131. static void au_read_buf16(struct mtd_info *mtd, u_char *buf, int len)
  132. {
  133. int i;
  134. struct nand_chip *this = mtd_to_nand(mtd);
  135. u16 *p = (u16 *) buf;
  136. len >>= 1;
  137. for (i = 0; i < len; i++) {
  138. p[i] = readw(this->legacy.IO_ADDR_R);
  139. wmb(); /* drain writebuffer */
  140. }
  141. }
  142. /* Select the chip by setting nCE to low */
  143. #define NAND_CTL_SETNCE 1
  144. /* Deselect the chip by setting nCE to high */
  145. #define NAND_CTL_CLRNCE 2
  146. /* Select the command latch by setting CLE to high */
  147. #define NAND_CTL_SETCLE 3
  148. /* Deselect the command latch by setting CLE to low */
  149. #define NAND_CTL_CLRCLE 4
  150. /* Select the address latch by setting ALE to high */
  151. #define NAND_CTL_SETALE 5
  152. /* Deselect the address latch by setting ALE to low */
  153. #define NAND_CTL_CLRALE 6
  154. static void au1550_hwcontrol(struct mtd_info *mtd, int cmd)
  155. {
  156. struct nand_chip *this = mtd_to_nand(mtd);
  157. struct au1550nd_ctx *ctx = container_of(this, struct au1550nd_ctx,
  158. chip);
  159. switch (cmd) {
  160. case NAND_CTL_SETCLE:
  161. this->legacy.IO_ADDR_W = ctx->base + MEM_STNAND_CMD;
  162. break;
  163. case NAND_CTL_CLRCLE:
  164. this->legacy.IO_ADDR_W = ctx->base + MEM_STNAND_DATA;
  165. break;
  166. case NAND_CTL_SETALE:
  167. this->legacy.IO_ADDR_W = ctx->base + MEM_STNAND_ADDR;
  168. break;
  169. case NAND_CTL_CLRALE:
  170. this->legacy.IO_ADDR_W = ctx->base + MEM_STNAND_DATA;
  171. /* FIXME: Nobody knows why this is necessary,
  172. * but it works only that way */
  173. udelay(1);
  174. break;
  175. case NAND_CTL_SETNCE:
  176. /* assert (force assert) chip enable */
  177. alchemy_wrsmem((1 << (4 + ctx->cs)), AU1000_MEM_STNDCTL);
  178. break;
  179. case NAND_CTL_CLRNCE:
  180. /* deassert chip enable */
  181. alchemy_wrsmem(0, AU1000_MEM_STNDCTL);
  182. break;
  183. }
  184. this->legacy.IO_ADDR_R = this->legacy.IO_ADDR_W;
  185. wmb(); /* Drain the writebuffer */
  186. }
  187. int au1550_device_ready(struct nand_chip *this)
  188. {
  189. return (alchemy_rdsmem(AU1000_MEM_STSTAT) & 0x1) ? 1 : 0;
  190. }
  191. /**
  192. * au1550_select_chip - control -CE line
  193. * Forbid driving -CE manually permitting the NAND controller to do this.
  194. * Keeping -CE asserted during the whole sector reads interferes with the
  195. * NOR flash and PCMCIA drivers as it causes contention on the static bus.
  196. * We only have to hold -CE low for the NAND read commands since the flash
  197. * chip needs it to be asserted during chip not ready time but the NAND
  198. * controller keeps it released.
  199. *
  200. * @this: NAND chip object
  201. * @chip: chipnumber to select, -1 for deselect
  202. */
  203. static void au1550_select_chip(struct nand_chip *this, int chip)
  204. {
  205. }
  206. /**
  207. * au1550_command - Send command to NAND device
  208. * @this: NAND chip object
  209. * @command: the command to be sent
  210. * @column: the column address for this command, -1 if none
  211. * @page_addr: the page address for this command, -1 if none
  212. */
  213. static void au1550_command(struct nand_chip *this, unsigned command,
  214. int column, int page_addr)
  215. {
  216. struct mtd_info *mtd = nand_to_mtd(this);
  217. struct au1550nd_ctx *ctx = container_of(this, struct au1550nd_ctx,
  218. chip);
  219. int ce_override = 0, i;
  220. unsigned long flags = 0;
  221. /* Begin command latch cycle */
  222. au1550_hwcontrol(mtd, NAND_CTL_SETCLE);
  223. /*
  224. * Write out the command to the device.
  225. */
  226. if (command == NAND_CMD_SEQIN) {
  227. int readcmd;
  228. if (column >= mtd->writesize) {
  229. /* OOB area */
  230. column -= mtd->writesize;
  231. readcmd = NAND_CMD_READOOB;
  232. } else if (column < 256) {
  233. /* First 256 bytes --> READ0 */
  234. readcmd = NAND_CMD_READ0;
  235. } else {
  236. column -= 256;
  237. readcmd = NAND_CMD_READ1;
  238. }
  239. ctx->write_byte(this, readcmd);
  240. }
  241. ctx->write_byte(this, command);
  242. /* Set ALE and clear CLE to start address cycle */
  243. au1550_hwcontrol(mtd, NAND_CTL_CLRCLE);
  244. if (column != -1 || page_addr != -1) {
  245. au1550_hwcontrol(mtd, NAND_CTL_SETALE);
  246. /* Serially input address */
  247. if (column != -1) {
  248. /* Adjust columns for 16 bit buswidth */
  249. if (this->options & NAND_BUSWIDTH_16 &&
  250. !nand_opcode_8bits(command))
  251. column >>= 1;
  252. ctx->write_byte(this, column);
  253. }
  254. if (page_addr != -1) {
  255. ctx->write_byte(this, (u8)(page_addr & 0xff));
  256. if (command == NAND_CMD_READ0 ||
  257. command == NAND_CMD_READ1 ||
  258. command == NAND_CMD_READOOB) {
  259. /*
  260. * NAND controller will release -CE after
  261. * the last address byte is written, so we'll
  262. * have to forcibly assert it. No interrupts
  263. * are allowed while we do this as we don't
  264. * want the NOR flash or PCMCIA drivers to
  265. * steal our precious bytes of data...
  266. */
  267. ce_override = 1;
  268. local_irq_save(flags);
  269. au1550_hwcontrol(mtd, NAND_CTL_SETNCE);
  270. }
  271. ctx->write_byte(this, (u8)(page_addr >> 8));
  272. if (this->options & NAND_ROW_ADDR_3)
  273. ctx->write_byte(this,
  274. ((page_addr >> 16) & 0x0f));
  275. }
  276. /* Latch in address */
  277. au1550_hwcontrol(mtd, NAND_CTL_CLRALE);
  278. }
  279. /*
  280. * Program and erase have their own busy handlers.
  281. * Status and sequential in need no delay.
  282. */
  283. switch (command) {
  284. case NAND_CMD_PAGEPROG:
  285. case NAND_CMD_ERASE1:
  286. case NAND_CMD_ERASE2:
  287. case NAND_CMD_SEQIN:
  288. case NAND_CMD_STATUS:
  289. return;
  290. case NAND_CMD_RESET:
  291. break;
  292. case NAND_CMD_READ0:
  293. case NAND_CMD_READ1:
  294. case NAND_CMD_READOOB:
  295. /* Check if we're really driving -CE low (just in case) */
  296. if (unlikely(!ce_override))
  297. break;
  298. /* Apply a short delay always to ensure that we do wait tWB. */
  299. ndelay(100);
  300. /* Wait for a chip to become ready... */
  301. for (i = this->legacy.chip_delay;
  302. !this->legacy.dev_ready(this) && i > 0; --i)
  303. udelay(1);
  304. /* Release -CE and re-enable interrupts. */
  305. au1550_hwcontrol(mtd, NAND_CTL_CLRNCE);
  306. local_irq_restore(flags);
  307. return;
  308. }
  309. /* Apply this short delay always to ensure that we do wait tWB. */
  310. ndelay(100);
  311. while(!this->legacy.dev_ready(this));
  312. }
  313. static int find_nand_cs(unsigned long nand_base)
  314. {
  315. void __iomem *base =
  316. (void __iomem *)KSEG1ADDR(AU1000_STATIC_MEM_PHYS_ADDR);
  317. unsigned long addr, staddr, start, mask, end;
  318. int i;
  319. for (i = 0; i < 4; i++) {
  320. addr = 0x1000 + (i * 0x10); /* CSx */
  321. staddr = __raw_readl(base + addr + 0x08); /* STADDRx */
  322. /* figure out the decoded range of this CS */
  323. start = (staddr << 4) & 0xfffc0000;
  324. mask = (staddr << 18) & 0xfffc0000;
  325. end = (start | (start - 1)) & ~(start ^ mask);
  326. if ((nand_base >= start) && (nand_base < end))
  327. return i;
  328. }
  329. return -ENODEV;
  330. }
  331. static int au1550nd_probe(struct platform_device *pdev)
  332. {
  333. struct au1550nd_platdata *pd;
  334. struct au1550nd_ctx *ctx;
  335. struct nand_chip *this;
  336. struct mtd_info *mtd;
  337. struct resource *r;
  338. int ret, cs;
  339. pd = dev_get_platdata(&pdev->dev);
  340. if (!pd) {
  341. dev_err(&pdev->dev, "missing platform data\n");
  342. return -ENODEV;
  343. }
  344. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  345. if (!ctx)
  346. return -ENOMEM;
  347. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  348. if (!r) {
  349. dev_err(&pdev->dev, "no NAND memory resource\n");
  350. ret = -ENODEV;
  351. goto out1;
  352. }
  353. if (request_mem_region(r->start, resource_size(r), "au1550-nand")) {
  354. dev_err(&pdev->dev, "cannot claim NAND memory area\n");
  355. ret = -ENOMEM;
  356. goto out1;
  357. }
  358. ctx->base = ioremap_nocache(r->start, 0x1000);
  359. if (!ctx->base) {
  360. dev_err(&pdev->dev, "cannot remap NAND memory area\n");
  361. ret = -ENODEV;
  362. goto out2;
  363. }
  364. this = &ctx->chip;
  365. mtd = nand_to_mtd(this);
  366. mtd->dev.parent = &pdev->dev;
  367. /* figure out which CS# r->start belongs to */
  368. cs = find_nand_cs(r->start);
  369. if (cs < 0) {
  370. dev_err(&pdev->dev, "cannot detect NAND chipselect\n");
  371. ret = -ENODEV;
  372. goto out3;
  373. }
  374. ctx->cs = cs;
  375. this->legacy.dev_ready = au1550_device_ready;
  376. this->select_chip = au1550_select_chip;
  377. this->legacy.cmdfunc = au1550_command;
  378. /* 30 us command delay time */
  379. this->legacy.chip_delay = 30;
  380. this->ecc.mode = NAND_ECC_SOFT;
  381. this->ecc.algo = NAND_ECC_HAMMING;
  382. if (pd->devwidth)
  383. this->options |= NAND_BUSWIDTH_16;
  384. this->legacy.read_byte = (pd->devwidth) ? au_read_byte16 : au_read_byte;
  385. ctx->write_byte = (pd->devwidth) ? au_write_byte16 : au_write_byte;
  386. this->legacy.write_buf = (pd->devwidth) ? au_write_buf16 : au_write_buf;
  387. this->legacy.read_buf = (pd->devwidth) ? au_read_buf16 : au_read_buf;
  388. ret = nand_scan(this, 1);
  389. if (ret) {
  390. dev_err(&pdev->dev, "NAND scan failed with %d\n", ret);
  391. goto out3;
  392. }
  393. mtd_device_register(mtd, pd->parts, pd->num_parts);
  394. platform_set_drvdata(pdev, ctx);
  395. return 0;
  396. out3:
  397. iounmap(ctx->base);
  398. out2:
  399. release_mem_region(r->start, resource_size(r));
  400. out1:
  401. kfree(ctx);
  402. return ret;
  403. }
  404. static int au1550nd_remove(struct platform_device *pdev)
  405. {
  406. struct au1550nd_ctx *ctx = platform_get_drvdata(pdev);
  407. struct resource *r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  408. nand_release(&ctx->chip);
  409. iounmap(ctx->base);
  410. release_mem_region(r->start, 0x1000);
  411. kfree(ctx);
  412. return 0;
  413. }
  414. static struct platform_driver au1550nd_driver = {
  415. .driver = {
  416. .name = "au1550-nand",
  417. },
  418. .probe = au1550nd_probe,
  419. .remove = au1550nd_remove,
  420. };
  421. module_platform_driver(au1550nd_driver);
  422. MODULE_LICENSE("GPL");
  423. MODULE_AUTHOR("Embedded Edge, LLC");
  424. MODULE_DESCRIPTION("Board-specific glue layer for NAND flash on Pb1550 board");