au1550nd.c 14 KB

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