au1550nd.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. wmb(); /* drain writebuffer */
  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. wmb(); /* drain writebuffer */
  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. wmb(); /* drain writebuffer */
  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. wmb(); /* drain writebuffer */
  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. wmb(); /* drain writebuffer */
  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. wmb(); /* drain writebuffer */
  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. wmb(); /* drain writebuffer */
  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. wmb(); /* drain writebuffer */
  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. wmb(); /* drain writebuffer */
  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. alchemy_wrsmem((1 << (4 + ctx->cs)), AU1000_MEM_STNDCTL);
  201. break;
  202. case NAND_CTL_CLRNCE:
  203. /* deassert chip enable */
  204. alchemy_wrsmem(0, AU1000_MEM_STNDCTL);
  205. break;
  206. }
  207. this->IO_ADDR_R = this->IO_ADDR_W;
  208. wmb(); /* Drain the writebuffer */
  209. }
  210. int au1550_device_ready(struct mtd_info *mtd)
  211. {
  212. return (alchemy_rdsmem(AU1000_MEM_STSTAT) & 0x1) ? 1 : 0;
  213. }
  214. /**
  215. * au1550_select_chip - control -CE line
  216. * Forbid driving -CE manually permitting the NAND controller to do this.
  217. * Keeping -CE asserted during the whole sector reads interferes with the
  218. * NOR flash and PCMCIA drivers as it causes contention on the static bus.
  219. * We only have to hold -CE low for the NAND read commands since the flash
  220. * chip needs it to be asserted during chip not ready time but the NAND
  221. * controller keeps it released.
  222. *
  223. * @mtd: MTD device structure
  224. * @chip: chipnumber to select, -1 for deselect
  225. */
  226. static void au1550_select_chip(struct mtd_info *mtd, int chip)
  227. {
  228. }
  229. /**
  230. * au1550_command - Send command to NAND device
  231. * @mtd: MTD device structure
  232. * @command: the command to be sent
  233. * @column: the column address for this command, -1 if none
  234. * @page_addr: the page address for this command, -1 if none
  235. */
  236. static void au1550_command(struct mtd_info *mtd, unsigned command, int column, int page_addr)
  237. {
  238. struct au1550nd_ctx *ctx = container_of(mtd, struct au1550nd_ctx, info);
  239. struct nand_chip *this = mtd->priv;
  240. int ce_override = 0, i;
  241. unsigned long flags = 0;
  242. /* Begin command latch cycle */
  243. au1550_hwcontrol(mtd, NAND_CTL_SETCLE);
  244. /*
  245. * Write out the command to the device.
  246. */
  247. if (command == NAND_CMD_SEQIN) {
  248. int readcmd;
  249. if (column >= mtd->writesize) {
  250. /* OOB area */
  251. column -= mtd->writesize;
  252. readcmd = NAND_CMD_READOOB;
  253. } else if (column < 256) {
  254. /* First 256 bytes --> READ0 */
  255. readcmd = NAND_CMD_READ0;
  256. } else {
  257. column -= 256;
  258. readcmd = NAND_CMD_READ1;
  259. }
  260. ctx->write_byte(mtd, readcmd);
  261. }
  262. ctx->write_byte(mtd, command);
  263. /* Set ALE and clear CLE to start address cycle */
  264. au1550_hwcontrol(mtd, NAND_CTL_CLRCLE);
  265. if (column != -1 || page_addr != -1) {
  266. au1550_hwcontrol(mtd, NAND_CTL_SETALE);
  267. /* Serially input address */
  268. if (column != -1) {
  269. /* Adjust columns for 16 bit buswidth */
  270. if (this->options & NAND_BUSWIDTH_16 &&
  271. !nand_opcode_8bits(command))
  272. column >>= 1;
  273. ctx->write_byte(mtd, column);
  274. }
  275. if (page_addr != -1) {
  276. ctx->write_byte(mtd, (u8)(page_addr & 0xff));
  277. if (command == NAND_CMD_READ0 ||
  278. command == NAND_CMD_READ1 ||
  279. command == NAND_CMD_READOOB) {
  280. /*
  281. * NAND controller will release -CE after
  282. * the last address byte is written, so we'll
  283. * have to forcibly assert it. No interrupts
  284. * are allowed while we do this as we don't
  285. * want the NOR flash or PCMCIA drivers to
  286. * steal our precious bytes of data...
  287. */
  288. ce_override = 1;
  289. local_irq_save(flags);
  290. au1550_hwcontrol(mtd, NAND_CTL_SETNCE);
  291. }
  292. ctx->write_byte(mtd, (u8)(page_addr >> 8));
  293. /* One more address cycle for devices > 32MiB */
  294. if (this->chipsize > (32 << 20))
  295. ctx->write_byte(mtd,
  296. ((page_addr >> 16) & 0x0f));
  297. }
  298. /* Latch in address */
  299. au1550_hwcontrol(mtd, NAND_CTL_CLRALE);
  300. }
  301. /*
  302. * Program and erase have their own busy handlers.
  303. * Status and sequential in need no delay.
  304. */
  305. switch (command) {
  306. case NAND_CMD_PAGEPROG:
  307. case NAND_CMD_ERASE1:
  308. case NAND_CMD_ERASE2:
  309. case NAND_CMD_SEQIN:
  310. case NAND_CMD_STATUS:
  311. return;
  312. case NAND_CMD_RESET:
  313. break;
  314. case NAND_CMD_READ0:
  315. case NAND_CMD_READ1:
  316. case NAND_CMD_READOOB:
  317. /* Check if we're really driving -CE low (just in case) */
  318. if (unlikely(!ce_override))
  319. break;
  320. /* Apply a short delay always to ensure that we do wait tWB. */
  321. ndelay(100);
  322. /* Wait for a chip to become ready... */
  323. for (i = this->chip_delay; !this->dev_ready(mtd) && i > 0; --i)
  324. udelay(1);
  325. /* Release -CE and re-enable interrupts. */
  326. au1550_hwcontrol(mtd, NAND_CTL_CLRNCE);
  327. local_irq_restore(flags);
  328. return;
  329. }
  330. /* Apply this short delay always to ensure that we do wait tWB. */
  331. ndelay(100);
  332. while(!this->dev_ready(mtd));
  333. }
  334. static int find_nand_cs(unsigned long nand_base)
  335. {
  336. void __iomem *base =
  337. (void __iomem *)KSEG1ADDR(AU1000_STATIC_MEM_PHYS_ADDR);
  338. unsigned long addr, staddr, start, mask, end;
  339. int i;
  340. for (i = 0; i < 4; i++) {
  341. addr = 0x1000 + (i * 0x10); /* CSx */
  342. staddr = __raw_readl(base + addr + 0x08); /* STADDRx */
  343. /* figure out the decoded range of this CS */
  344. start = (staddr << 4) & 0xfffc0000;
  345. mask = (staddr << 18) & 0xfffc0000;
  346. end = (start | (start - 1)) & ~(start ^ mask);
  347. if ((nand_base >= start) && (nand_base < end))
  348. return i;
  349. }
  350. return -ENODEV;
  351. }
  352. static int au1550nd_probe(struct platform_device *pdev)
  353. {
  354. struct au1550nd_platdata *pd;
  355. struct au1550nd_ctx *ctx;
  356. struct nand_chip *this;
  357. struct resource *r;
  358. int ret, cs;
  359. pd = dev_get_platdata(&pdev->dev);
  360. if (!pd) {
  361. dev_err(&pdev->dev, "missing platform data\n");
  362. return -ENODEV;
  363. }
  364. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  365. if (!ctx)
  366. return -ENOMEM;
  367. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  368. if (!r) {
  369. dev_err(&pdev->dev, "no NAND memory resource\n");
  370. ret = -ENODEV;
  371. goto out1;
  372. }
  373. if (request_mem_region(r->start, resource_size(r), "au1550-nand")) {
  374. dev_err(&pdev->dev, "cannot claim NAND memory area\n");
  375. ret = -ENOMEM;
  376. goto out1;
  377. }
  378. ctx->base = ioremap_nocache(r->start, 0x1000);
  379. if (!ctx->base) {
  380. dev_err(&pdev->dev, "cannot remap NAND memory area\n");
  381. ret = -ENODEV;
  382. goto out2;
  383. }
  384. this = &ctx->chip;
  385. ctx->info.priv = this;
  386. ctx->info.dev.parent = &pdev->dev;
  387. /* figure out which CS# r->start belongs to */
  388. cs = find_nand_cs(r->start);
  389. if (cs < 0) {
  390. dev_err(&pdev->dev, "cannot detect NAND chipselect\n");
  391. ret = -ENODEV;
  392. goto out3;
  393. }
  394. ctx->cs = cs;
  395. this->dev_ready = au1550_device_ready;
  396. this->select_chip = au1550_select_chip;
  397. this->cmdfunc = au1550_command;
  398. /* 30 us command delay time */
  399. this->chip_delay = 30;
  400. this->ecc.mode = NAND_ECC_SOFT;
  401. if (pd->devwidth)
  402. this->options |= NAND_BUSWIDTH_16;
  403. this->read_byte = (pd->devwidth) ? au_read_byte16 : au_read_byte;
  404. ctx->write_byte = (pd->devwidth) ? au_write_byte16 : au_write_byte;
  405. this->read_word = au_read_word;
  406. this->write_buf = (pd->devwidth) ? au_write_buf16 : au_write_buf;
  407. this->read_buf = (pd->devwidth) ? au_read_buf16 : au_read_buf;
  408. ret = nand_scan(&ctx->info, 1);
  409. if (ret) {
  410. dev_err(&pdev->dev, "NAND scan failed with %d\n", ret);
  411. goto out3;
  412. }
  413. mtd_device_register(&ctx->info, pd->parts, pd->num_parts);
  414. platform_set_drvdata(pdev, ctx);
  415. return 0;
  416. out3:
  417. iounmap(ctx->base);
  418. out2:
  419. release_mem_region(r->start, resource_size(r));
  420. out1:
  421. kfree(ctx);
  422. return ret;
  423. }
  424. static int au1550nd_remove(struct platform_device *pdev)
  425. {
  426. struct au1550nd_ctx *ctx = platform_get_drvdata(pdev);
  427. struct resource *r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  428. nand_release(&ctx->info);
  429. iounmap(ctx->base);
  430. release_mem_region(r->start, 0x1000);
  431. kfree(ctx);
  432. return 0;
  433. }
  434. static struct platform_driver au1550nd_driver = {
  435. .driver = {
  436. .name = "au1550-nand",
  437. },
  438. .probe = au1550nd_probe,
  439. .remove = au1550nd_remove,
  440. };
  441. module_platform_driver(au1550nd_driver);
  442. MODULE_LICENSE("GPL");
  443. MODULE_AUTHOR("Embedded Edge, LLC");
  444. MODULE_DESCRIPTION("Board-specific glue layer for NAND flash on Pb1550 board");