au1550nd.c 14 KB

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