au1550nd.c 15 KB

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