au1550nd.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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. /* Select the chip by setting nCE to low */
  248. #define NAND_CTL_SETNCE 1
  249. /* Deselect the chip by setting nCE to high */
  250. #define NAND_CTL_CLRNCE 2
  251. /* Select the command latch by setting CLE to high */
  252. #define NAND_CTL_SETCLE 3
  253. /* Deselect the command latch by setting CLE to low */
  254. #define NAND_CTL_CLRCLE 4
  255. /* Select the address latch by setting ALE to high */
  256. #define NAND_CTL_SETALE 5
  257. /* Deselect the address latch by setting ALE to low */
  258. #define NAND_CTL_CLRALE 6
  259. static void au1550_hwcontrol(struct mtd_info *mtd, int cmd)
  260. {
  261. register struct nand_chip *this = mtd->priv;
  262. switch (cmd) {
  263. case NAND_CTL_SETCLE:
  264. this->IO_ADDR_W = p_nand + MEM_STNAND_CMD;
  265. break;
  266. case NAND_CTL_CLRCLE:
  267. this->IO_ADDR_W = p_nand + MEM_STNAND_DATA;
  268. break;
  269. case NAND_CTL_SETALE:
  270. this->IO_ADDR_W = p_nand + MEM_STNAND_ADDR;
  271. break;
  272. case NAND_CTL_CLRALE:
  273. this->IO_ADDR_W = p_nand + MEM_STNAND_DATA;
  274. /* FIXME: Nobody knows why this is necessary,
  275. * but it works only that way */
  276. udelay(1);
  277. break;
  278. case NAND_CTL_SETNCE:
  279. /* assert (force assert) chip enable */
  280. au_writel((1 << (4 + NAND_CS)), MEM_STNDCTL);
  281. break;
  282. case NAND_CTL_CLRNCE:
  283. /* deassert chip enable */
  284. au_writel(0, MEM_STNDCTL);
  285. break;
  286. }
  287. this->IO_ADDR_R = this->IO_ADDR_W;
  288. /* Drain the writebuffer */
  289. au_sync();
  290. }
  291. int au1550_device_ready(struct mtd_info *mtd)
  292. {
  293. int ret = (au_readl(MEM_STSTAT) & 0x1) ? 1 : 0;
  294. au_sync();
  295. return ret;
  296. }
  297. /**
  298. * au1550_select_chip - control -CE line
  299. * Forbid driving -CE manually permitting the NAND controller to do this.
  300. * Keeping -CE asserted during the whole sector reads interferes with the
  301. * NOR flash and PCMCIA drivers as it causes contention on the static bus.
  302. * We only have to hold -CE low for the NAND read commands since the flash
  303. * chip needs it to be asserted during chip not ready time but the NAND
  304. * controller keeps it released.
  305. *
  306. * @mtd: MTD device structure
  307. * @chip: chipnumber to select, -1 for deselect
  308. */
  309. static void au1550_select_chip(struct mtd_info *mtd, int chip)
  310. {
  311. }
  312. /**
  313. * au1550_command - Send command to NAND device
  314. * @mtd: MTD device structure
  315. * @command: the command to be sent
  316. * @column: the column address for this command, -1 if none
  317. * @page_addr: the page address for this command, -1 if none
  318. */
  319. static void au1550_command(struct mtd_info *mtd, unsigned command, int column, int page_addr)
  320. {
  321. register struct nand_chip *this = mtd->priv;
  322. int ce_override = 0, i;
  323. ulong flags;
  324. /* Begin command latch cycle */
  325. au1550_hwcontrol(mtd, NAND_CTL_SETCLE);
  326. /*
  327. * Write out the command to the device.
  328. */
  329. if (command == NAND_CMD_SEQIN) {
  330. int readcmd;
  331. if (column >= mtd->writesize) {
  332. /* OOB area */
  333. column -= mtd->writesize;
  334. readcmd = NAND_CMD_READOOB;
  335. } else if (column < 256) {
  336. /* First 256 bytes --> READ0 */
  337. readcmd = NAND_CMD_READ0;
  338. } else {
  339. column -= 256;
  340. readcmd = NAND_CMD_READ1;
  341. }
  342. this->write_byte(mtd, readcmd);
  343. }
  344. this->write_byte(mtd, command);
  345. /* Set ALE and clear CLE to start address cycle */
  346. au1550_hwcontrol(mtd, NAND_CTL_CLRCLE);
  347. if (column != -1 || page_addr != -1) {
  348. au1550_hwcontrol(mtd, NAND_CTL_SETALE);
  349. /* Serially input address */
  350. if (column != -1) {
  351. /* Adjust columns for 16 bit buswidth */
  352. if (this->options & NAND_BUSWIDTH_16)
  353. column >>= 1;
  354. this->write_byte(mtd, column);
  355. }
  356. if (page_addr != -1) {
  357. this->write_byte(mtd, (u8)(page_addr & 0xff));
  358. if (command == NAND_CMD_READ0 ||
  359. command == NAND_CMD_READ1 ||
  360. command == NAND_CMD_READOOB) {
  361. /*
  362. * NAND controller will release -CE after
  363. * the last address byte is written, so we'll
  364. * have to forcibly assert it. No interrupts
  365. * are allowed while we do this as we don't
  366. * want the NOR flash or PCMCIA drivers to
  367. * steal our precious bytes of data...
  368. */
  369. ce_override = 1;
  370. local_irq_save(flags);
  371. au1550_hwcontrol(mtd, NAND_CTL_SETNCE);
  372. }
  373. this->write_byte(mtd, (u8)(page_addr >> 8));
  374. /* One more address cycle for devices > 32MiB */
  375. if (this->chipsize > (32 << 20))
  376. this->write_byte(mtd, (u8)((page_addr >> 16) & 0x0f));
  377. }
  378. /* Latch in address */
  379. au1550_hwcontrol(mtd, NAND_CTL_CLRALE);
  380. }
  381. /*
  382. * Program and erase have their own busy handlers.
  383. * Status and sequential in need no delay.
  384. */
  385. switch (command) {
  386. case NAND_CMD_PAGEPROG:
  387. case NAND_CMD_ERASE1:
  388. case NAND_CMD_ERASE2:
  389. case NAND_CMD_SEQIN:
  390. case NAND_CMD_STATUS:
  391. return;
  392. case NAND_CMD_RESET:
  393. break;
  394. case NAND_CMD_READ0:
  395. case NAND_CMD_READ1:
  396. case NAND_CMD_READOOB:
  397. /* Check if we're really driving -CE low (just in case) */
  398. if (unlikely(!ce_override))
  399. break;
  400. /* Apply a short delay always to ensure that we do wait tWB. */
  401. ndelay(100);
  402. /* Wait for a chip to become ready... */
  403. for (i = this->chip_delay; !this->dev_ready(mtd) && i > 0; --i)
  404. udelay(1);
  405. /* Release -CE and re-enable interrupts. */
  406. au1550_hwcontrol(mtd, NAND_CTL_CLRNCE);
  407. local_irq_restore(flags);
  408. return;
  409. }
  410. /* Apply this short delay always to ensure that we do wait tWB. */
  411. ndelay(100);
  412. while(!this->dev_ready(mtd));
  413. }
  414. /*
  415. * Main initialization routine
  416. */
  417. static int __init au1xxx_nand_init(void)
  418. {
  419. struct nand_chip *this;
  420. u16 boot_swapboot = 0; /* default value */
  421. int retval;
  422. u32 mem_staddr;
  423. u32 nand_phys;
  424. /* Allocate memory for MTD device structure and private data */
  425. au1550_mtd = kmalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip), GFP_KERNEL);
  426. if (!au1550_mtd) {
  427. printk("Unable to allocate NAND MTD dev structure.\n");
  428. return -ENOMEM;
  429. }
  430. /* Get pointer to private data */
  431. this = (struct nand_chip *)(&au1550_mtd[1]);
  432. /* Initialize structures */
  433. memset(au1550_mtd, 0, sizeof(struct mtd_info));
  434. memset(this, 0, sizeof(struct nand_chip));
  435. /* Link the private data with the MTD structure */
  436. au1550_mtd->priv = this;
  437. au1550_mtd->owner = THIS_MODULE;
  438. /* MEM_STNDCTL: disable ints, disable nand boot */
  439. au_writel(0, MEM_STNDCTL);
  440. #ifdef CONFIG_MIPS_PB1550
  441. /* set gpio206 high */
  442. au_writel(au_readl(GPIO2_DIR) & ~(1 << 6), GPIO2_DIR);
  443. boot_swapboot = (au_readl(MEM_STSTAT) & (0x7 << 1)) | ((bcsr->status >> 6) & 0x1);
  444. switch (boot_swapboot) {
  445. case 0:
  446. case 2:
  447. case 8:
  448. case 0xC:
  449. case 0xD:
  450. /* x16 NAND Flash */
  451. nand_width = 0;
  452. break;
  453. case 1:
  454. case 9:
  455. case 3:
  456. case 0xE:
  457. case 0xF:
  458. /* x8 NAND Flash */
  459. nand_width = 1;
  460. break;
  461. default:
  462. printk("Pb1550 NAND: bad boot:swap\n");
  463. retval = -EINVAL;
  464. goto outmem;
  465. }
  466. #endif
  467. /* Configure chip-select; normally done by boot code, e.g. YAMON */
  468. #ifdef NAND_STCFG
  469. if (NAND_CS == 0) {
  470. au_writel(NAND_STCFG, MEM_STCFG0);
  471. au_writel(NAND_STTIME, MEM_STTIME0);
  472. au_writel(NAND_STADDR, MEM_STADDR0);
  473. }
  474. if (NAND_CS == 1) {
  475. au_writel(NAND_STCFG, MEM_STCFG1);
  476. au_writel(NAND_STTIME, MEM_STTIME1);
  477. au_writel(NAND_STADDR, MEM_STADDR1);
  478. }
  479. if (NAND_CS == 2) {
  480. au_writel(NAND_STCFG, MEM_STCFG2);
  481. au_writel(NAND_STTIME, MEM_STTIME2);
  482. au_writel(NAND_STADDR, MEM_STADDR2);
  483. }
  484. if (NAND_CS == 3) {
  485. au_writel(NAND_STCFG, MEM_STCFG3);
  486. au_writel(NAND_STTIME, MEM_STTIME3);
  487. au_writel(NAND_STADDR, MEM_STADDR3);
  488. }
  489. #endif
  490. /* Locate NAND chip-select in order to determine NAND phys address */
  491. mem_staddr = 0x00000000;
  492. if (((au_readl(MEM_STCFG0) & 0x7) == 0x5) && (NAND_CS == 0))
  493. mem_staddr = au_readl(MEM_STADDR0);
  494. else if (((au_readl(MEM_STCFG1) & 0x7) == 0x5) && (NAND_CS == 1))
  495. mem_staddr = au_readl(MEM_STADDR1);
  496. else if (((au_readl(MEM_STCFG2) & 0x7) == 0x5) && (NAND_CS == 2))
  497. mem_staddr = au_readl(MEM_STADDR2);
  498. else if (((au_readl(MEM_STCFG3) & 0x7) == 0x5) && (NAND_CS == 3))
  499. mem_staddr = au_readl(MEM_STADDR3);
  500. if (mem_staddr == 0x00000000) {
  501. printk("Au1xxx NAND: ERROR WITH NAND CHIP-SELECT\n");
  502. kfree(au1550_mtd);
  503. return 1;
  504. }
  505. nand_phys = (mem_staddr << 4) & 0xFFFC0000;
  506. p_nand = (void __iomem *)ioremap(nand_phys, 0x1000);
  507. /* make controller and MTD agree */
  508. if (NAND_CS == 0)
  509. nand_width = au_readl(MEM_STCFG0) & (1 << 22);
  510. if (NAND_CS == 1)
  511. nand_width = au_readl(MEM_STCFG1) & (1 << 22);
  512. if (NAND_CS == 2)
  513. nand_width = au_readl(MEM_STCFG2) & (1 << 22);
  514. if (NAND_CS == 3)
  515. nand_width = au_readl(MEM_STCFG3) & (1 << 22);
  516. /* Set address of hardware control function */
  517. this->dev_ready = au1550_device_ready;
  518. this->select_chip = au1550_select_chip;
  519. this->cmdfunc = au1550_command;
  520. /* 30 us command delay time */
  521. this->chip_delay = 30;
  522. this->ecc.mode = NAND_ECC_SOFT;
  523. this->options = NAND_NO_AUTOINCR;
  524. if (!nand_width)
  525. this->options |= NAND_BUSWIDTH_16;
  526. this->read_byte = (!nand_width) ? au_read_byte16 : au_read_byte;
  527. this->write_byte = (!nand_width) ? au_write_byte16 : au_write_byte;
  528. this->write_word = au_write_word;
  529. this->read_word = au_read_word;
  530. this->write_buf = (!nand_width) ? au_write_buf16 : au_write_buf;
  531. this->read_buf = (!nand_width) ? au_read_buf16 : au_read_buf;
  532. this->verify_buf = (!nand_width) ? au_verify_buf16 : au_verify_buf;
  533. /* Scan to find existence of the device */
  534. if (nand_scan(au1550_mtd, 1)) {
  535. retval = -ENXIO;
  536. goto outio;
  537. }
  538. /* Register the partitions */
  539. add_mtd_partitions(au1550_mtd, partition_info, ARRAY_SIZE(partition_info));
  540. return 0;
  541. outio:
  542. iounmap((void *)p_nand);
  543. outmem:
  544. kfree(au1550_mtd);
  545. return retval;
  546. }
  547. module_init(au1xxx_nand_init);
  548. /*
  549. * Clean up routine
  550. */
  551. static void __exit au1550_cleanup(void)
  552. {
  553. struct nand_chip *this = (struct nand_chip *)&au1550_mtd[1];
  554. /* Release resources, unregister device */
  555. nand_release(au1550_mtd);
  556. /* Free the MTD device structure */
  557. kfree(au1550_mtd);
  558. /* Unmap */
  559. iounmap((void *)p_nand);
  560. }
  561. module_exit(au1550_cleanup);
  562. MODULE_LICENSE("GPL");
  563. MODULE_AUTHOR("Embedded Edge, LLC");
  564. MODULE_DESCRIPTION("Board-specific glue layer for NAND flash on Pb1550 board");