au1550nd.c 14 KB

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