au1550nd.c 14 KB

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