au1550nd.c 12 KB

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