au1550nd.c 15 KB

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