au1550nd.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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. #include <asm/mach-au1x00/au1xxx.h>
  23. /*
  24. * MTD structure for NAND controller
  25. */
  26. static struct mtd_info *au1550_mtd = NULL;
  27. static void __iomem *p_nand;
  28. static int nand_width = 1; /* default x8 */
  29. static void (*au1550_write_byte)(struct mtd_info *, u_char);
  30. /*
  31. * Define partitions for flash device
  32. */
  33. static const struct mtd_partition partition_info[] = {
  34. {
  35. .name = "NAND FS 0",
  36. .offset = 0,
  37. .size = 8 * 1024 * 1024},
  38. {
  39. .name = "NAND FS 1",
  40. .offset = MTDPART_OFS_APPEND,
  41. .size = MTDPART_SIZ_FULL}
  42. };
  43. /**
  44. * au_read_byte - read one byte from the chip
  45. * @mtd: MTD device structure
  46. *
  47. * read function for 8bit buswith
  48. */
  49. static u_char au_read_byte(struct mtd_info *mtd)
  50. {
  51. struct nand_chip *this = mtd->priv;
  52. u_char ret = readb(this->IO_ADDR_R);
  53. au_sync();
  54. return ret;
  55. }
  56. /**
  57. * au_write_byte - write one byte to the chip
  58. * @mtd: MTD device structure
  59. * @byte: pointer to data byte to write
  60. *
  61. * write function for 8it buswith
  62. */
  63. static void au_write_byte(struct mtd_info *mtd, u_char byte)
  64. {
  65. struct nand_chip *this = mtd->priv;
  66. writeb(byte, this->IO_ADDR_W);
  67. au_sync();
  68. }
  69. /**
  70. * au_read_byte16 - read one byte endianess aware from the chip
  71. * @mtd: MTD device structure
  72. *
  73. * read function for 16bit buswith with
  74. * endianess conversion
  75. */
  76. static u_char au_read_byte16(struct mtd_info *mtd)
  77. {
  78. struct nand_chip *this = mtd->priv;
  79. u_char ret = (u_char) cpu_to_le16(readw(this->IO_ADDR_R));
  80. au_sync();
  81. return ret;
  82. }
  83. /**
  84. * au_write_byte16 - write one byte endianess aware to the chip
  85. * @mtd: MTD device structure
  86. * @byte: pointer to data byte to write
  87. *
  88. * write function for 16bit buswith with
  89. * endianess conversion
  90. */
  91. static void au_write_byte16(struct mtd_info *mtd, u_char byte)
  92. {
  93. struct nand_chip *this = mtd->priv;
  94. writew(le16_to_cpu((u16) byte), this->IO_ADDR_W);
  95. au_sync();
  96. }
  97. /**
  98. * au_read_word - read one word from the chip
  99. * @mtd: MTD device structure
  100. *
  101. * read function for 16bit buswith without
  102. * endianess 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 buswith
  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 buswith
  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 buswith
  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 buswith
  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 buswith
  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 buswith
  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 = kmalloc(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. /* Initialize structures */
  409. memset(au1550_mtd, 0, sizeof(struct mtd_info));
  410. memset(this, 0, sizeof(struct nand_chip));
  411. /* Link the private data with the MTD structure */
  412. au1550_mtd->priv = this;
  413. au1550_mtd->owner = THIS_MODULE;
  414. /* MEM_STNDCTL: disable ints, disable nand boot */
  415. au_writel(0, MEM_STNDCTL);
  416. #ifdef CONFIG_MIPS_PB1550
  417. /* set gpio206 high */
  418. au_writel(au_readl(GPIO2_DIR) & ~(1 << 6), GPIO2_DIR);
  419. boot_swapboot = (au_readl(MEM_STSTAT) & (0x7 << 1)) | ((bcsr->status >> 6) & 0x1);
  420. switch (boot_swapboot) {
  421. case 0:
  422. case 2:
  423. case 8:
  424. case 0xC:
  425. case 0xD:
  426. /* x16 NAND Flash */
  427. nand_width = 0;
  428. break;
  429. case 1:
  430. case 9:
  431. case 3:
  432. case 0xE:
  433. case 0xF:
  434. /* x8 NAND Flash */
  435. nand_width = 1;
  436. break;
  437. default:
  438. printk("Pb1550 NAND: bad boot:swap\n");
  439. retval = -EINVAL;
  440. goto outmem;
  441. }
  442. #endif
  443. /* Configure chip-select; normally done by boot code, e.g. YAMON */
  444. #ifdef NAND_STCFG
  445. if (NAND_CS == 0) {
  446. au_writel(NAND_STCFG, MEM_STCFG0);
  447. au_writel(NAND_STTIME, MEM_STTIME0);
  448. au_writel(NAND_STADDR, MEM_STADDR0);
  449. }
  450. if (NAND_CS == 1) {
  451. au_writel(NAND_STCFG, MEM_STCFG1);
  452. au_writel(NAND_STTIME, MEM_STTIME1);
  453. au_writel(NAND_STADDR, MEM_STADDR1);
  454. }
  455. if (NAND_CS == 2) {
  456. au_writel(NAND_STCFG, MEM_STCFG2);
  457. au_writel(NAND_STTIME, MEM_STTIME2);
  458. au_writel(NAND_STADDR, MEM_STADDR2);
  459. }
  460. if (NAND_CS == 3) {
  461. au_writel(NAND_STCFG, MEM_STCFG3);
  462. au_writel(NAND_STTIME, MEM_STTIME3);
  463. au_writel(NAND_STADDR, MEM_STADDR3);
  464. }
  465. #endif
  466. /* Locate NAND chip-select in order to determine NAND phys address */
  467. mem_staddr = 0x00000000;
  468. if (((au_readl(MEM_STCFG0) & 0x7) == 0x5) && (NAND_CS == 0))
  469. mem_staddr = au_readl(MEM_STADDR0);
  470. else if (((au_readl(MEM_STCFG1) & 0x7) == 0x5) && (NAND_CS == 1))
  471. mem_staddr = au_readl(MEM_STADDR1);
  472. else if (((au_readl(MEM_STCFG2) & 0x7) == 0x5) && (NAND_CS == 2))
  473. mem_staddr = au_readl(MEM_STADDR2);
  474. else if (((au_readl(MEM_STCFG3) & 0x7) == 0x5) && (NAND_CS == 3))
  475. mem_staddr = au_readl(MEM_STADDR3);
  476. if (mem_staddr == 0x00000000) {
  477. printk("Au1xxx NAND: ERROR WITH NAND CHIP-SELECT\n");
  478. kfree(au1550_mtd);
  479. return 1;
  480. }
  481. nand_phys = (mem_staddr << 4) & 0xFFFC0000;
  482. p_nand = (void __iomem *)ioremap(nand_phys, 0x1000);
  483. /* make controller and MTD agree */
  484. if (NAND_CS == 0)
  485. nand_width = au_readl(MEM_STCFG0) & (1 << 22);
  486. if (NAND_CS == 1)
  487. nand_width = au_readl(MEM_STCFG1) & (1 << 22);
  488. if (NAND_CS == 2)
  489. nand_width = au_readl(MEM_STCFG2) & (1 << 22);
  490. if (NAND_CS == 3)
  491. nand_width = au_readl(MEM_STCFG3) & (1 << 22);
  492. /* Set address of hardware control function */
  493. this->dev_ready = au1550_device_ready;
  494. this->select_chip = au1550_select_chip;
  495. this->cmdfunc = au1550_command;
  496. /* 30 us command delay time */
  497. this->chip_delay = 30;
  498. this->ecc.mode = NAND_ECC_SOFT;
  499. this->options = NAND_NO_AUTOINCR;
  500. if (!nand_width)
  501. this->options |= NAND_BUSWIDTH_16;
  502. this->read_byte = (!nand_width) ? au_read_byte16 : au_read_byte;
  503. au1550_write_byte = (!nand_width) ? au_write_byte16 : au_write_byte;
  504. this->read_word = au_read_word;
  505. this->write_buf = (!nand_width) ? au_write_buf16 : au_write_buf;
  506. this->read_buf = (!nand_width) ? au_read_buf16 : au_read_buf;
  507. this->verify_buf = (!nand_width) ? au_verify_buf16 : au_verify_buf;
  508. /* Scan to find existence of the device */
  509. if (nand_scan(au1550_mtd, 1)) {
  510. retval = -ENXIO;
  511. goto outio;
  512. }
  513. /* Register the partitions */
  514. add_mtd_partitions(au1550_mtd, partition_info, ARRAY_SIZE(partition_info));
  515. return 0;
  516. outio:
  517. iounmap((void *)p_nand);
  518. outmem:
  519. kfree(au1550_mtd);
  520. return retval;
  521. }
  522. module_init(au1xxx_nand_init);
  523. /*
  524. * Clean up routine
  525. */
  526. static void __exit au1550_cleanup(void)
  527. {
  528. struct nand_chip *this = (struct nand_chip *)&au1550_mtd[1];
  529. /* Release resources, unregister device */
  530. nand_release(au1550_mtd);
  531. /* Free the MTD device structure */
  532. kfree(au1550_mtd);
  533. /* Unmap */
  534. iounmap((void *)p_nand);
  535. }
  536. module_exit(au1550_cleanup);
  537. MODULE_LICENSE("GPL");
  538. MODULE_AUTHOR("Embedded Edge, LLC");
  539. MODULE_DESCRIPTION("Board-specific glue layer for NAND flash on Pb1550 board");