au1550nd.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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 <asm/io.h>
  20. #include <asm/mach-au1x00/au1xxx.h>
  21. #include <asm/mach-db1x00/bcsr.h>
  22. /*
  23. * MTD structure for NAND controller
  24. */
  25. static struct mtd_info *au1550_mtd = NULL;
  26. static void __iomem *p_nand;
  27. static int nand_width = 1; /* default x8 */
  28. static void (*au1550_write_byte)(struct mtd_info *, u_char);
  29. /*
  30. * Define partitions for flash device
  31. */
  32. static const struct mtd_partition partition_info[] = {
  33. {
  34. .name = "NAND FS 0",
  35. .offset = 0,
  36. .size = 8 * 1024 * 1024},
  37. {
  38. .name = "NAND FS 1",
  39. .offset = MTDPART_OFS_APPEND,
  40. .size = MTDPART_SIZ_FULL}
  41. };
  42. /**
  43. * au_read_byte - read one byte from the chip
  44. * @mtd: MTD device structure
  45. *
  46. * read function for 8bit buswidth
  47. */
  48. static u_char au_read_byte(struct mtd_info *mtd)
  49. {
  50. struct nand_chip *this = mtd->priv;
  51. u_char ret = readb(this->IO_ADDR_R);
  52. au_sync();
  53. return ret;
  54. }
  55. /**
  56. * au_write_byte - write one byte to the chip
  57. * @mtd: MTD device structure
  58. * @byte: pointer to data byte to write
  59. *
  60. * write function for 8it buswidth
  61. */
  62. static void au_write_byte(struct mtd_info *mtd, u_char byte)
  63. {
  64. struct nand_chip *this = mtd->priv;
  65. writeb(byte, this->IO_ADDR_W);
  66. au_sync();
  67. }
  68. /**
  69. * au_read_byte16 - read one byte endianness aware from the chip
  70. * @mtd: MTD device structure
  71. *
  72. * read function for 16bit buswidth with endianness conversion
  73. */
  74. static u_char au_read_byte16(struct mtd_info *mtd)
  75. {
  76. struct nand_chip *this = mtd->priv;
  77. u_char ret = (u_char) cpu_to_le16(readw(this->IO_ADDR_R));
  78. au_sync();
  79. return ret;
  80. }
  81. /**
  82. * au_write_byte16 - write one byte endianness aware to the chip
  83. * @mtd: MTD device structure
  84. * @byte: pointer to data byte to write
  85. *
  86. * write function for 16bit buswidth with endianness conversion
  87. */
  88. static void au_write_byte16(struct mtd_info *mtd, u_char byte)
  89. {
  90. struct nand_chip *this = mtd->priv;
  91. writew(le16_to_cpu((u16) byte), this->IO_ADDR_W);
  92. au_sync();
  93. }
  94. /**
  95. * au_read_word - read one word from the chip
  96. * @mtd: MTD device structure
  97. *
  98. * read function for 16bit buswidth without endianness conversion
  99. */
  100. static u16 au_read_word(struct mtd_info *mtd)
  101. {
  102. struct nand_chip *this = mtd->priv;
  103. u16 ret = readw(this->IO_ADDR_R);
  104. au_sync();
  105. return ret;
  106. }
  107. /**
  108. * au_write_buf - write buffer to chip
  109. * @mtd: MTD device structure
  110. * @buf: data buffer
  111. * @len: number of bytes to write
  112. *
  113. * write function for 8bit buswidth
  114. */
  115. static void au_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
  116. {
  117. int i;
  118. struct nand_chip *this = mtd->priv;
  119. for (i = 0; i < len; i++) {
  120. writeb(buf[i], this->IO_ADDR_W);
  121. au_sync();
  122. }
  123. }
  124. /**
  125. * au_read_buf - read chip data into buffer
  126. * @mtd: MTD device structure
  127. * @buf: buffer to store date
  128. * @len: number of bytes to read
  129. *
  130. * read function for 8bit buswidth
  131. */
  132. static void au_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  133. {
  134. int i;
  135. struct nand_chip *this = mtd->priv;
  136. for (i = 0; i < len; i++) {
  137. buf[i] = readb(this->IO_ADDR_R);
  138. au_sync();
  139. }
  140. }
  141. /**
  142. * au_verify_buf - Verify chip data against buffer
  143. * @mtd: MTD device structure
  144. * @buf: buffer containing the data to compare
  145. * @len: number of bytes to compare
  146. *
  147. * verify function for 8bit buswidth
  148. */
  149. static int au_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
  150. {
  151. int i;
  152. struct nand_chip *this = mtd->priv;
  153. for (i = 0; i < len; i++) {
  154. if (buf[i] != readb(this->IO_ADDR_R))
  155. return -EFAULT;
  156. au_sync();
  157. }
  158. return 0;
  159. }
  160. /**
  161. * au_write_buf16 - write buffer to chip
  162. * @mtd: MTD device structure
  163. * @buf: data buffer
  164. * @len: number of bytes to write
  165. *
  166. * write function for 16bit buswidth
  167. */
  168. static void au_write_buf16(struct mtd_info *mtd, const u_char *buf, int len)
  169. {
  170. int i;
  171. struct nand_chip *this = mtd->priv;
  172. u16 *p = (u16 *) buf;
  173. len >>= 1;
  174. for (i = 0; i < len; i++) {
  175. writew(p[i], this->IO_ADDR_W);
  176. au_sync();
  177. }
  178. }
  179. /**
  180. * au_read_buf16 - read chip data into buffer
  181. * @mtd: MTD device structure
  182. * @buf: buffer to store date
  183. * @len: number of bytes to read
  184. *
  185. * read function for 16bit buswidth
  186. */
  187. static void au_read_buf16(struct mtd_info *mtd, u_char *buf, int len)
  188. {
  189. int i;
  190. struct nand_chip *this = mtd->priv;
  191. u16 *p = (u16 *) buf;
  192. len >>= 1;
  193. for (i = 0; i < len; i++) {
  194. p[i] = readw(this->IO_ADDR_R);
  195. au_sync();
  196. }
  197. }
  198. /**
  199. * au_verify_buf16 - Verify chip data against buffer
  200. * @mtd: MTD device structure
  201. * @buf: buffer containing the data to compare
  202. * @len: number of bytes to compare
  203. *
  204. * verify function for 16bit buswidth
  205. */
  206. static int au_verify_buf16(struct mtd_info *mtd, const u_char *buf, int len)
  207. {
  208. int i;
  209. struct nand_chip *this = mtd->priv;
  210. u16 *p = (u16 *) buf;
  211. len >>= 1;
  212. for (i = 0; i < len; i++) {
  213. if (p[i] != readw(this->IO_ADDR_R))
  214. return -EFAULT;
  215. au_sync();
  216. }
  217. return 0;
  218. }
  219. /* Select the chip by setting nCE to low */
  220. #define NAND_CTL_SETNCE 1
  221. /* Deselect the chip by setting nCE to high */
  222. #define NAND_CTL_CLRNCE 2
  223. /* Select the command latch by setting CLE to high */
  224. #define NAND_CTL_SETCLE 3
  225. /* Deselect the command latch by setting CLE to low */
  226. #define NAND_CTL_CLRCLE 4
  227. /* Select the address latch by setting ALE to high */
  228. #define NAND_CTL_SETALE 5
  229. /* Deselect the address latch by setting ALE to low */
  230. #define NAND_CTL_CLRALE 6
  231. static void au1550_hwcontrol(struct mtd_info *mtd, int cmd)
  232. {
  233. register struct nand_chip *this = mtd->priv;
  234. switch (cmd) {
  235. case NAND_CTL_SETCLE:
  236. this->IO_ADDR_W = p_nand + MEM_STNAND_CMD;
  237. break;
  238. case NAND_CTL_CLRCLE:
  239. this->IO_ADDR_W = p_nand + MEM_STNAND_DATA;
  240. break;
  241. case NAND_CTL_SETALE:
  242. this->IO_ADDR_W = p_nand + MEM_STNAND_ADDR;
  243. break;
  244. case NAND_CTL_CLRALE:
  245. this->IO_ADDR_W = p_nand + MEM_STNAND_DATA;
  246. /* FIXME: Nobody knows why this is necessary,
  247. * but it works only that way */
  248. udelay(1);
  249. break;
  250. case NAND_CTL_SETNCE:
  251. /* assert (force assert) chip enable */
  252. au_writel((1 << (4 + NAND_CS)), MEM_STNDCTL);
  253. break;
  254. case NAND_CTL_CLRNCE:
  255. /* deassert chip enable */
  256. au_writel(0, MEM_STNDCTL);
  257. break;
  258. }
  259. this->IO_ADDR_R = this->IO_ADDR_W;
  260. /* Drain the writebuffer */
  261. au_sync();
  262. }
  263. int au1550_device_ready(struct mtd_info *mtd)
  264. {
  265. int ret = (au_readl(MEM_STSTAT) & 0x1) ? 1 : 0;
  266. au_sync();
  267. return ret;
  268. }
  269. /**
  270. * au1550_select_chip - control -CE line
  271. * Forbid driving -CE manually permitting the NAND controller to do this.
  272. * Keeping -CE asserted during the whole sector reads interferes with the
  273. * NOR flash and PCMCIA drivers as it causes contention on the static bus.
  274. * We only have to hold -CE low for the NAND read commands since the flash
  275. * chip needs it to be asserted during chip not ready time but the NAND
  276. * controller keeps it released.
  277. *
  278. * @mtd: MTD device structure
  279. * @chip: chipnumber to select, -1 for deselect
  280. */
  281. static void au1550_select_chip(struct mtd_info *mtd, int chip)
  282. {
  283. }
  284. /**
  285. * au1550_command - Send command to NAND device
  286. * @mtd: MTD device structure
  287. * @command: the command to be sent
  288. * @column: the column address for this command, -1 if none
  289. * @page_addr: the page address for this command, -1 if none
  290. */
  291. static void au1550_command(struct mtd_info *mtd, unsigned command, int column, int page_addr)
  292. {
  293. register struct nand_chip *this = mtd->priv;
  294. int ce_override = 0, i;
  295. ulong flags;
  296. /* Begin command latch cycle */
  297. au1550_hwcontrol(mtd, NAND_CTL_SETCLE);
  298. /*
  299. * Write out the command to the device.
  300. */
  301. if (command == NAND_CMD_SEQIN) {
  302. int readcmd;
  303. if (column >= mtd->writesize) {
  304. /* OOB area */
  305. column -= mtd->writesize;
  306. readcmd = NAND_CMD_READOOB;
  307. } else if (column < 256) {
  308. /* First 256 bytes --> READ0 */
  309. readcmd = NAND_CMD_READ0;
  310. } else {
  311. column -= 256;
  312. readcmd = NAND_CMD_READ1;
  313. }
  314. au1550_write_byte(mtd, readcmd);
  315. }
  316. au1550_write_byte(mtd, command);
  317. /* Set ALE and clear CLE to start address cycle */
  318. au1550_hwcontrol(mtd, NAND_CTL_CLRCLE);
  319. if (column != -1 || page_addr != -1) {
  320. au1550_hwcontrol(mtd, NAND_CTL_SETALE);
  321. /* Serially input address */
  322. if (column != -1) {
  323. /* Adjust columns for 16 bit buswidth */
  324. if (this->options & NAND_BUSWIDTH_16)
  325. column >>= 1;
  326. au1550_write_byte(mtd, column);
  327. }
  328. if (page_addr != -1) {
  329. au1550_write_byte(mtd, (u8)(page_addr & 0xff));
  330. if (command == NAND_CMD_READ0 ||
  331. command == NAND_CMD_READ1 ||
  332. command == NAND_CMD_READOOB) {
  333. /*
  334. * NAND controller will release -CE after
  335. * the last address byte is written, so we'll
  336. * have to forcibly assert it. No interrupts
  337. * are allowed while we do this as we don't
  338. * want the NOR flash or PCMCIA drivers to
  339. * steal our precious bytes of data...
  340. */
  341. ce_override = 1;
  342. local_irq_save(flags);
  343. au1550_hwcontrol(mtd, NAND_CTL_SETNCE);
  344. }
  345. au1550_write_byte(mtd, (u8)(page_addr >> 8));
  346. /* One more address cycle for devices > 32MiB */
  347. if (this->chipsize > (32 << 20))
  348. au1550_write_byte(mtd, (u8)((page_addr >> 16) & 0x0f));
  349. }
  350. /* Latch in address */
  351. au1550_hwcontrol(mtd, NAND_CTL_CLRALE);
  352. }
  353. /*
  354. * Program and erase have their own busy handlers.
  355. * Status and sequential in need no delay.
  356. */
  357. switch (command) {
  358. case NAND_CMD_PAGEPROG:
  359. case NAND_CMD_ERASE1:
  360. case NAND_CMD_ERASE2:
  361. case NAND_CMD_SEQIN:
  362. case NAND_CMD_STATUS:
  363. return;
  364. case NAND_CMD_RESET:
  365. break;
  366. case NAND_CMD_READ0:
  367. case NAND_CMD_READ1:
  368. case NAND_CMD_READOOB:
  369. /* Check if we're really driving -CE low (just in case) */
  370. if (unlikely(!ce_override))
  371. break;
  372. /* Apply a short delay always to ensure that we do wait tWB. */
  373. ndelay(100);
  374. /* Wait for a chip to become ready... */
  375. for (i = this->chip_delay; !this->dev_ready(mtd) && i > 0; --i)
  376. udelay(1);
  377. /* Release -CE and re-enable interrupts. */
  378. au1550_hwcontrol(mtd, NAND_CTL_CLRNCE);
  379. local_irq_restore(flags);
  380. return;
  381. }
  382. /* Apply this short delay always to ensure that we do wait tWB. */
  383. ndelay(100);
  384. while(!this->dev_ready(mtd));
  385. }
  386. /*
  387. * Main initialization routine
  388. */
  389. static int __init au1xxx_nand_init(void)
  390. {
  391. struct nand_chip *this;
  392. u16 boot_swapboot = 0; /* default value */
  393. int retval;
  394. u32 mem_staddr;
  395. u32 nand_phys;
  396. /* Allocate memory for MTD device structure and private data */
  397. au1550_mtd = kzalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip), GFP_KERNEL);
  398. if (!au1550_mtd) {
  399. printk("Unable to allocate NAND MTD dev structure.\n");
  400. return -ENOMEM;
  401. }
  402. /* Get pointer to private data */
  403. this = (struct nand_chip *)(&au1550_mtd[1]);
  404. /* Link the private data with the MTD structure */
  405. au1550_mtd->priv = this;
  406. au1550_mtd->owner = THIS_MODULE;
  407. /* MEM_STNDCTL: disable ints, disable nand boot */
  408. au_writel(0, MEM_STNDCTL);
  409. #ifdef CONFIG_MIPS_PB1550
  410. /* set gpio206 high */
  411. gpio_direction_input(206);
  412. boot_swapboot = (au_readl(MEM_STSTAT) & (0x7 << 1)) | ((bcsr_read(BCSR_STATUS) >> 6) & 0x1);
  413. switch (boot_swapboot) {
  414. case 0:
  415. case 2:
  416. case 8:
  417. case 0xC:
  418. case 0xD:
  419. /* x16 NAND Flash */
  420. nand_width = 0;
  421. break;
  422. case 1:
  423. case 9:
  424. case 3:
  425. case 0xE:
  426. case 0xF:
  427. /* x8 NAND Flash */
  428. nand_width = 1;
  429. break;
  430. default:
  431. printk("Pb1550 NAND: bad boot:swap\n");
  432. retval = -EINVAL;
  433. goto outmem;
  434. }
  435. #endif
  436. /* Configure chip-select; normally done by boot code, e.g. YAMON */
  437. #ifdef NAND_STCFG
  438. if (NAND_CS == 0) {
  439. au_writel(NAND_STCFG, MEM_STCFG0);
  440. au_writel(NAND_STTIME, MEM_STTIME0);
  441. au_writel(NAND_STADDR, MEM_STADDR0);
  442. }
  443. if (NAND_CS == 1) {
  444. au_writel(NAND_STCFG, MEM_STCFG1);
  445. au_writel(NAND_STTIME, MEM_STTIME1);
  446. au_writel(NAND_STADDR, MEM_STADDR1);
  447. }
  448. if (NAND_CS == 2) {
  449. au_writel(NAND_STCFG, MEM_STCFG2);
  450. au_writel(NAND_STTIME, MEM_STTIME2);
  451. au_writel(NAND_STADDR, MEM_STADDR2);
  452. }
  453. if (NAND_CS == 3) {
  454. au_writel(NAND_STCFG, MEM_STCFG3);
  455. au_writel(NAND_STTIME, MEM_STTIME3);
  456. au_writel(NAND_STADDR, MEM_STADDR3);
  457. }
  458. #endif
  459. /* Locate NAND chip-select in order to determine NAND phys address */
  460. mem_staddr = 0x00000000;
  461. if (((au_readl(MEM_STCFG0) & 0x7) == 0x5) && (NAND_CS == 0))
  462. mem_staddr = au_readl(MEM_STADDR0);
  463. else if (((au_readl(MEM_STCFG1) & 0x7) == 0x5) && (NAND_CS == 1))
  464. mem_staddr = au_readl(MEM_STADDR1);
  465. else if (((au_readl(MEM_STCFG2) & 0x7) == 0x5) && (NAND_CS == 2))
  466. mem_staddr = au_readl(MEM_STADDR2);
  467. else if (((au_readl(MEM_STCFG3) & 0x7) == 0x5) && (NAND_CS == 3))
  468. mem_staddr = au_readl(MEM_STADDR3);
  469. if (mem_staddr == 0x00000000) {
  470. printk("Au1xxx NAND: ERROR WITH NAND CHIP-SELECT\n");
  471. kfree(au1550_mtd);
  472. return 1;
  473. }
  474. nand_phys = (mem_staddr << 4) & 0xFFFC0000;
  475. p_nand = ioremap(nand_phys, 0x1000);
  476. /* make controller and MTD agree */
  477. if (NAND_CS == 0)
  478. nand_width = au_readl(MEM_STCFG0) & (1 << 22);
  479. if (NAND_CS == 1)
  480. nand_width = au_readl(MEM_STCFG1) & (1 << 22);
  481. if (NAND_CS == 2)
  482. nand_width = au_readl(MEM_STCFG2) & (1 << 22);
  483. if (NAND_CS == 3)
  484. nand_width = au_readl(MEM_STCFG3) & (1 << 22);
  485. /* Set address of hardware control function */
  486. this->dev_ready = au1550_device_ready;
  487. this->select_chip = au1550_select_chip;
  488. this->cmdfunc = au1550_command;
  489. /* 30 us command delay time */
  490. this->chip_delay = 30;
  491. this->ecc.mode = NAND_ECC_SOFT;
  492. this->options = NAND_NO_AUTOINCR;
  493. if (!nand_width)
  494. this->options |= NAND_BUSWIDTH_16;
  495. this->read_byte = (!nand_width) ? au_read_byte16 : au_read_byte;
  496. au1550_write_byte = (!nand_width) ? au_write_byte16 : au_write_byte;
  497. this->read_word = au_read_word;
  498. this->write_buf = (!nand_width) ? au_write_buf16 : au_write_buf;
  499. this->read_buf = (!nand_width) ? au_read_buf16 : au_read_buf;
  500. this->verify_buf = (!nand_width) ? au_verify_buf16 : au_verify_buf;
  501. /* Scan to find existence of the device */
  502. if (nand_scan(au1550_mtd, 1)) {
  503. retval = -ENXIO;
  504. goto outio;
  505. }
  506. /* Register the partitions */
  507. mtd_device_register(au1550_mtd, partition_info,
  508. ARRAY_SIZE(partition_info));
  509. return 0;
  510. outio:
  511. iounmap(p_nand);
  512. outmem:
  513. kfree(au1550_mtd);
  514. return retval;
  515. }
  516. module_init(au1xxx_nand_init);
  517. /*
  518. * Clean up routine
  519. */
  520. static void __exit au1550_cleanup(void)
  521. {
  522. /* Release resources, unregister device */
  523. nand_release(au1550_mtd);
  524. /* Free the MTD device structure */
  525. kfree(au1550_mtd);
  526. /* Unmap */
  527. iounmap(p_nand);
  528. }
  529. module_exit(au1550_cleanup);
  530. MODULE_LICENSE("GPL");
  531. MODULE_AUTHOR("Embedded Edge, LLC");
  532. MODULE_DESCRIPTION("Board-specific glue layer for NAND flash on Pb1550 board");