au1550nd.c 13 KB

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