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/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_write_buf16 - write buffer to chip
  131. * @mtd: MTD device structure
  132. * @buf: data buffer
  133. * @len: number of bytes to write
  134. *
  135. * write function for 16bit buswidth
  136. */
  137. static void au_write_buf16(struct mtd_info *mtd, const u_char *buf, int len)
  138. {
  139. int i;
  140. struct nand_chip *this = mtd->priv;
  141. u16 *p = (u16 *) buf;
  142. len >>= 1;
  143. for (i = 0; i < len; i++) {
  144. writew(p[i], this->IO_ADDR_W);
  145. au_sync();
  146. }
  147. }
  148. /**
  149. * au_read_buf16 - read chip data into buffer
  150. * @mtd: MTD device structure
  151. * @buf: buffer to store date
  152. * @len: number of bytes to read
  153. *
  154. * read function for 16bit buswidth
  155. */
  156. static void au_read_buf16(struct mtd_info *mtd, 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. p[i] = readw(this->IO_ADDR_R);
  164. au_sync();
  165. }
  166. }
  167. /* Select the chip by setting nCE to low */
  168. #define NAND_CTL_SETNCE 1
  169. /* Deselect the chip by setting nCE to high */
  170. #define NAND_CTL_CLRNCE 2
  171. /* Select the command latch by setting CLE to high */
  172. #define NAND_CTL_SETCLE 3
  173. /* Deselect the command latch by setting CLE to low */
  174. #define NAND_CTL_CLRCLE 4
  175. /* Select the address latch by setting ALE to high */
  176. #define NAND_CTL_SETALE 5
  177. /* Deselect the address latch by setting ALE to low */
  178. #define NAND_CTL_CLRALE 6
  179. static void au1550_hwcontrol(struct mtd_info *mtd, int cmd)
  180. {
  181. struct au1550nd_ctx *ctx = container_of(mtd, struct au1550nd_ctx, info);
  182. struct nand_chip *this = mtd->priv;
  183. switch (cmd) {
  184. case NAND_CTL_SETCLE:
  185. this->IO_ADDR_W = ctx->base + MEM_STNAND_CMD;
  186. break;
  187. case NAND_CTL_CLRCLE:
  188. this->IO_ADDR_W = ctx->base + MEM_STNAND_DATA;
  189. break;
  190. case NAND_CTL_SETALE:
  191. this->IO_ADDR_W = ctx->base + MEM_STNAND_ADDR;
  192. break;
  193. case NAND_CTL_CLRALE:
  194. this->IO_ADDR_W = ctx->base + MEM_STNAND_DATA;
  195. /* FIXME: Nobody knows why this is necessary,
  196. * but it works only that way */
  197. udelay(1);
  198. break;
  199. case NAND_CTL_SETNCE:
  200. /* assert (force assert) chip enable */
  201. au_writel((1 << (4 + ctx->cs)), MEM_STNDCTL);
  202. break;
  203. case NAND_CTL_CLRNCE:
  204. /* deassert chip enable */
  205. au_writel(0, MEM_STNDCTL);
  206. break;
  207. }
  208. this->IO_ADDR_R = this->IO_ADDR_W;
  209. /* Drain the writebuffer */
  210. au_sync();
  211. }
  212. int au1550_device_ready(struct mtd_info *mtd)
  213. {
  214. int ret = (au_readl(MEM_STSTAT) & 0x1) ? 1 : 0;
  215. au_sync();
  216. return ret;
  217. }
  218. /**
  219. * au1550_select_chip - control -CE line
  220. * Forbid driving -CE manually permitting the NAND controller to do this.
  221. * Keeping -CE asserted during the whole sector reads interferes with the
  222. * NOR flash and PCMCIA drivers as it causes contention on the static bus.
  223. * We only have to hold -CE low for the NAND read commands since the flash
  224. * chip needs it to be asserted during chip not ready time but the NAND
  225. * controller keeps it released.
  226. *
  227. * @mtd: MTD device structure
  228. * @chip: chipnumber to select, -1 for deselect
  229. */
  230. static void au1550_select_chip(struct mtd_info *mtd, int chip)
  231. {
  232. }
  233. /**
  234. * au1550_command - Send command to NAND device
  235. * @mtd: MTD device structure
  236. * @command: the command to be sent
  237. * @column: the column address for this command, -1 if none
  238. * @page_addr: the page address for this command, -1 if none
  239. */
  240. static void au1550_command(struct mtd_info *mtd, unsigned command, int column, int page_addr)
  241. {
  242. struct au1550nd_ctx *ctx = container_of(mtd, struct au1550nd_ctx, info);
  243. struct nand_chip *this = mtd->priv;
  244. int ce_override = 0, i;
  245. unsigned long flags = 0;
  246. /* Begin command latch cycle */
  247. au1550_hwcontrol(mtd, NAND_CTL_SETCLE);
  248. /*
  249. * Write out the command to the device.
  250. */
  251. if (command == NAND_CMD_SEQIN) {
  252. int readcmd;
  253. if (column >= mtd->writesize) {
  254. /* OOB area */
  255. column -= mtd->writesize;
  256. readcmd = NAND_CMD_READOOB;
  257. } else if (column < 256) {
  258. /* First 256 bytes --> READ0 */
  259. readcmd = NAND_CMD_READ0;
  260. } else {
  261. column -= 256;
  262. readcmd = NAND_CMD_READ1;
  263. }
  264. ctx->write_byte(mtd, readcmd);
  265. }
  266. ctx->write_byte(mtd, command);
  267. /* Set ALE and clear CLE to start address cycle */
  268. au1550_hwcontrol(mtd, NAND_CTL_CLRCLE);
  269. if (column != -1 || page_addr != -1) {
  270. au1550_hwcontrol(mtd, NAND_CTL_SETALE);
  271. /* Serially input address */
  272. if (column != -1) {
  273. /* Adjust columns for 16 bit buswidth */
  274. if (this->options & NAND_BUSWIDTH_16)
  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 = pdev->dev.platform_data;
  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. dev_err(&pdev->dev, "no memory for NAND context\n");
  370. return -ENOMEM;
  371. }
  372. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  373. if (!r) {
  374. dev_err(&pdev->dev, "no NAND memory resource\n");
  375. ret = -ENODEV;
  376. goto out1;
  377. }
  378. if (request_mem_region(r->start, resource_size(r), "au1550-nand")) {
  379. dev_err(&pdev->dev, "cannot claim NAND memory area\n");
  380. ret = -ENOMEM;
  381. goto out1;
  382. }
  383. ctx->base = ioremap_nocache(r->start, 0x1000);
  384. if (!ctx->base) {
  385. dev_err(&pdev->dev, "cannot remap NAND memory area\n");
  386. ret = -ENODEV;
  387. goto out2;
  388. }
  389. this = &ctx->chip;
  390. ctx->info.priv = this;
  391. ctx->info.owner = THIS_MODULE;
  392. /* figure out which CS# r->start belongs to */
  393. cs = find_nand_cs(r->start);
  394. if (cs < 0) {
  395. dev_err(&pdev->dev, "cannot detect NAND chipselect\n");
  396. ret = -ENODEV;
  397. goto out3;
  398. }
  399. ctx->cs = cs;
  400. this->dev_ready = au1550_device_ready;
  401. this->select_chip = au1550_select_chip;
  402. this->cmdfunc = au1550_command;
  403. /* 30 us command delay time */
  404. this->chip_delay = 30;
  405. this->ecc.mode = NAND_ECC_SOFT;
  406. if (pd->devwidth)
  407. this->options |= NAND_BUSWIDTH_16;
  408. this->read_byte = (pd->devwidth) ? au_read_byte16 : au_read_byte;
  409. ctx->write_byte = (pd->devwidth) ? au_write_byte16 : au_write_byte;
  410. this->read_word = au_read_word;
  411. this->write_buf = (pd->devwidth) ? au_write_buf16 : au_write_buf;
  412. this->read_buf = (pd->devwidth) ? au_read_buf16 : au_read_buf;
  413. ret = nand_scan(&ctx->info, 1);
  414. if (ret) {
  415. dev_err(&pdev->dev, "NAND scan failed with %d\n", ret);
  416. goto out3;
  417. }
  418. mtd_device_register(&ctx->info, pd->parts, pd->num_parts);
  419. return 0;
  420. out3:
  421. iounmap(ctx->base);
  422. out2:
  423. release_mem_region(r->start, resource_size(r));
  424. out1:
  425. kfree(ctx);
  426. return ret;
  427. }
  428. static int au1550nd_remove(struct platform_device *pdev)
  429. {
  430. struct au1550nd_ctx *ctx = platform_get_drvdata(pdev);
  431. struct resource *r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  432. nand_release(&ctx->info);
  433. iounmap(ctx->base);
  434. release_mem_region(r->start, 0x1000);
  435. kfree(ctx);
  436. return 0;
  437. }
  438. static struct platform_driver au1550nd_driver = {
  439. .driver = {
  440. .name = "au1550-nand",
  441. .owner = THIS_MODULE,
  442. },
  443. .probe = au1550nd_probe,
  444. .remove = au1550nd_remove,
  445. };
  446. module_platform_driver(au1550nd_driver);
  447. MODULE_LICENSE("GPL");
  448. MODULE_AUTHOR("Embedded Edge, LLC");
  449. MODULE_DESCRIPTION("Board-specific glue layer for NAND flash on Pb1550 board");