au1550nd.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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/gpio.h>
  13. #include <linux/init.h>
  14. #include <linux/module.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/mtd/mtd.h>
  17. #include <linux/mtd/nand.h>
  18. #include <linux/mtd/partitions.h>
  19. #include <asm/io.h>
  20. #ifdef CONFIG_MIPS_PB1550
  21. #include <asm/mach-pb1x00/pb1550.h>
  22. #elif defined(CONFIG_MIPS_DB1550)
  23. #include <asm/mach-db1x00/db1x00.h>
  24. #endif
  25. #include <asm/mach-db1x00/bcsr.h>
  26. /*
  27. * MTD structure for NAND controller
  28. */
  29. static struct mtd_info *au1550_mtd = NULL;
  30. static void __iomem *p_nand;
  31. static int nand_width = 1; /* default x8 */
  32. static void (*au1550_write_byte)(struct mtd_info *, u_char);
  33. /*
  34. * Define partitions for flash device
  35. */
  36. static const struct mtd_partition partition_info[] = {
  37. {
  38. .name = "NAND FS 0",
  39. .offset = 0,
  40. .size = 8 * 1024 * 1024},
  41. {
  42. .name = "NAND FS 1",
  43. .offset = MTDPART_OFS_APPEND,
  44. .size = MTDPART_SIZ_FULL}
  45. };
  46. /**
  47. * au_read_byte - read one byte from the chip
  48. * @mtd: MTD device structure
  49. *
  50. * read function for 8bit buswith
  51. */
  52. static u_char au_read_byte(struct mtd_info *mtd)
  53. {
  54. struct nand_chip *this = mtd->priv;
  55. u_char ret = readb(this->IO_ADDR_R);
  56. au_sync();
  57. return ret;
  58. }
  59. /**
  60. * au_write_byte - write one byte to the chip
  61. * @mtd: MTD device structure
  62. * @byte: pointer to data byte to write
  63. *
  64. * write function for 8it buswith
  65. */
  66. static void au_write_byte(struct mtd_info *mtd, u_char byte)
  67. {
  68. struct nand_chip *this = mtd->priv;
  69. writeb(byte, this->IO_ADDR_W);
  70. au_sync();
  71. }
  72. /**
  73. * au_read_byte16 - read one byte endianess aware from the chip
  74. * @mtd: MTD device structure
  75. *
  76. * read function for 16bit buswith with
  77. * endianess conversion
  78. */
  79. static u_char au_read_byte16(struct mtd_info *mtd)
  80. {
  81. struct nand_chip *this = mtd->priv;
  82. u_char ret = (u_char) cpu_to_le16(readw(this->IO_ADDR_R));
  83. au_sync();
  84. return ret;
  85. }
  86. /**
  87. * au_write_byte16 - write one byte endianess aware to the chip
  88. * @mtd: MTD device structure
  89. * @byte: pointer to data byte to write
  90. *
  91. * write function for 16bit buswith with
  92. * endianess conversion
  93. */
  94. static void au_write_byte16(struct mtd_info *mtd, u_char byte)
  95. {
  96. struct nand_chip *this = mtd->priv;
  97. writew(le16_to_cpu((u16) byte), this->IO_ADDR_W);
  98. au_sync();
  99. }
  100. /**
  101. * au_read_word - read one word from the chip
  102. * @mtd: MTD device structure
  103. *
  104. * read function for 16bit buswith without
  105. * endianess conversion
  106. */
  107. static u16 au_read_word(struct mtd_info *mtd)
  108. {
  109. struct nand_chip *this = mtd->priv;
  110. u16 ret = readw(this->IO_ADDR_R);
  111. au_sync();
  112. return ret;
  113. }
  114. /**
  115. * au_write_buf - write buffer to chip
  116. * @mtd: MTD device structure
  117. * @buf: data buffer
  118. * @len: number of bytes to write
  119. *
  120. * write function for 8bit buswith
  121. */
  122. static void au_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
  123. {
  124. int i;
  125. struct nand_chip *this = mtd->priv;
  126. for (i = 0; i < len; i++) {
  127. writeb(buf[i], this->IO_ADDR_W);
  128. au_sync();
  129. }
  130. }
  131. /**
  132. * au_read_buf - read chip data into buffer
  133. * @mtd: MTD device structure
  134. * @buf: buffer to store date
  135. * @len: number of bytes to read
  136. *
  137. * read function for 8bit buswith
  138. */
  139. static void au_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  140. {
  141. int i;
  142. struct nand_chip *this = mtd->priv;
  143. for (i = 0; i < len; i++) {
  144. buf[i] = readb(this->IO_ADDR_R);
  145. au_sync();
  146. }
  147. }
  148. /**
  149. * au_verify_buf - Verify chip data against buffer
  150. * @mtd: MTD device structure
  151. * @buf: buffer containing the data to compare
  152. * @len: number of bytes to compare
  153. *
  154. * verify function for 8bit buswith
  155. */
  156. static int au_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
  157. {
  158. int i;
  159. struct nand_chip *this = mtd->priv;
  160. for (i = 0; i < len; i++) {
  161. if (buf[i] != readb(this->IO_ADDR_R))
  162. return -EFAULT;
  163. au_sync();
  164. }
  165. return 0;
  166. }
  167. /**
  168. * au_write_buf16 - write buffer to chip
  169. * @mtd: MTD device structure
  170. * @buf: data buffer
  171. * @len: number of bytes to write
  172. *
  173. * write function for 16bit buswith
  174. */
  175. static void au_write_buf16(struct mtd_info *mtd, const u_char *buf, int len)
  176. {
  177. int i;
  178. struct nand_chip *this = mtd->priv;
  179. u16 *p = (u16 *) buf;
  180. len >>= 1;
  181. for (i = 0; i < len; i++) {
  182. writew(p[i], this->IO_ADDR_W);
  183. au_sync();
  184. }
  185. }
  186. /**
  187. * au_read_buf16 - read chip data into buffer
  188. * @mtd: MTD device structure
  189. * @buf: buffer to store date
  190. * @len: number of bytes to read
  191. *
  192. * read function for 16bit buswith
  193. */
  194. static void au_read_buf16(struct mtd_info *mtd, u_char *buf, int len)
  195. {
  196. int i;
  197. struct nand_chip *this = mtd->priv;
  198. u16 *p = (u16 *) buf;
  199. len >>= 1;
  200. for (i = 0; i < len; i++) {
  201. p[i] = readw(this->IO_ADDR_R);
  202. au_sync();
  203. }
  204. }
  205. /**
  206. * au_verify_buf16 - Verify chip data against buffer
  207. * @mtd: MTD device structure
  208. * @buf: buffer containing the data to compare
  209. * @len: number of bytes to compare
  210. *
  211. * verify function for 16bit buswith
  212. */
  213. static int au_verify_buf16(struct mtd_info *mtd, const u_char *buf, int len)
  214. {
  215. int i;
  216. struct nand_chip *this = mtd->priv;
  217. u16 *p = (u16 *) buf;
  218. len >>= 1;
  219. for (i = 0; i < len; i++) {
  220. if (p[i] != readw(this->IO_ADDR_R))
  221. return -EFAULT;
  222. au_sync();
  223. }
  224. return 0;
  225. }
  226. /* Select the chip by setting nCE to low */
  227. #define NAND_CTL_SETNCE 1
  228. /* Deselect the chip by setting nCE to high */
  229. #define NAND_CTL_CLRNCE 2
  230. /* Select the command latch by setting CLE to high */
  231. #define NAND_CTL_SETCLE 3
  232. /* Deselect the command latch by setting CLE to low */
  233. #define NAND_CTL_CLRCLE 4
  234. /* Select the address latch by setting ALE to high */
  235. #define NAND_CTL_SETALE 5
  236. /* Deselect the address latch by setting ALE to low */
  237. #define NAND_CTL_CLRALE 6
  238. static void au1550_hwcontrol(struct mtd_info *mtd, int cmd)
  239. {
  240. register struct nand_chip *this = mtd->priv;
  241. switch (cmd) {
  242. case NAND_CTL_SETCLE:
  243. this->IO_ADDR_W = p_nand + MEM_STNAND_CMD;
  244. break;
  245. case NAND_CTL_CLRCLE:
  246. this->IO_ADDR_W = p_nand + MEM_STNAND_DATA;
  247. break;
  248. case NAND_CTL_SETALE:
  249. this->IO_ADDR_W = p_nand + MEM_STNAND_ADDR;
  250. break;
  251. case NAND_CTL_CLRALE:
  252. this->IO_ADDR_W = p_nand + MEM_STNAND_DATA;
  253. /* FIXME: Nobody knows why this is necessary,
  254. * but it works only that way */
  255. udelay(1);
  256. break;
  257. case NAND_CTL_SETNCE:
  258. /* assert (force assert) chip enable */
  259. au_writel((1 << (4 + NAND_CS)), MEM_STNDCTL);
  260. break;
  261. case NAND_CTL_CLRNCE:
  262. /* deassert chip enable */
  263. au_writel(0, MEM_STNDCTL);
  264. break;
  265. }
  266. this->IO_ADDR_R = this->IO_ADDR_W;
  267. /* Drain the writebuffer */
  268. au_sync();
  269. }
  270. int au1550_device_ready(struct mtd_info *mtd)
  271. {
  272. int ret = (au_readl(MEM_STSTAT) & 0x1) ? 1 : 0;
  273. au_sync();
  274. return ret;
  275. }
  276. /**
  277. * au1550_select_chip - control -CE line
  278. * Forbid driving -CE manually permitting the NAND controller to do this.
  279. * Keeping -CE asserted during the whole sector reads interferes with the
  280. * NOR flash and PCMCIA drivers as it causes contention on the static bus.
  281. * We only have to hold -CE low for the NAND read commands since the flash
  282. * chip needs it to be asserted during chip not ready time but the NAND
  283. * controller keeps it released.
  284. *
  285. * @mtd: MTD device structure
  286. * @chip: chipnumber to select, -1 for deselect
  287. */
  288. static void au1550_select_chip(struct mtd_info *mtd, int chip)
  289. {
  290. }
  291. /**
  292. * au1550_command - Send command to NAND device
  293. * @mtd: MTD device structure
  294. * @command: the command to be sent
  295. * @column: the column address for this command, -1 if none
  296. * @page_addr: the page address for this command, -1 if none
  297. */
  298. static void au1550_command(struct mtd_info *mtd, unsigned command, int column, int page_addr)
  299. {
  300. register struct nand_chip *this = mtd->priv;
  301. int ce_override = 0, i;
  302. ulong flags;
  303. /* Begin command latch cycle */
  304. au1550_hwcontrol(mtd, NAND_CTL_SETCLE);
  305. /*
  306. * Write out the command to the device.
  307. */
  308. if (command == NAND_CMD_SEQIN) {
  309. int readcmd;
  310. if (column >= mtd->writesize) {
  311. /* OOB area */
  312. column -= mtd->writesize;
  313. readcmd = NAND_CMD_READOOB;
  314. } else if (column < 256) {
  315. /* First 256 bytes --> READ0 */
  316. readcmd = NAND_CMD_READ0;
  317. } else {
  318. column -= 256;
  319. readcmd = NAND_CMD_READ1;
  320. }
  321. au1550_write_byte(mtd, readcmd);
  322. }
  323. au1550_write_byte(mtd, command);
  324. /* Set ALE and clear CLE to start address cycle */
  325. au1550_hwcontrol(mtd, NAND_CTL_CLRCLE);
  326. if (column != -1 || page_addr != -1) {
  327. au1550_hwcontrol(mtd, NAND_CTL_SETALE);
  328. /* Serially input address */
  329. if (column != -1) {
  330. /* Adjust columns for 16 bit buswidth */
  331. if (this->options & NAND_BUSWIDTH_16)
  332. column >>= 1;
  333. au1550_write_byte(mtd, column);
  334. }
  335. if (page_addr != -1) {
  336. au1550_write_byte(mtd, (u8)(page_addr & 0xff));
  337. if (command == NAND_CMD_READ0 ||
  338. command == NAND_CMD_READ1 ||
  339. command == NAND_CMD_READOOB) {
  340. /*
  341. * NAND controller will release -CE after
  342. * the last address byte is written, so we'll
  343. * have to forcibly assert it. No interrupts
  344. * are allowed while we do this as we don't
  345. * want the NOR flash or PCMCIA drivers to
  346. * steal our precious bytes of data...
  347. */
  348. ce_override = 1;
  349. local_irq_save(flags);
  350. au1550_hwcontrol(mtd, NAND_CTL_SETNCE);
  351. }
  352. au1550_write_byte(mtd, (u8)(page_addr >> 8));
  353. /* One more address cycle for devices > 32MiB */
  354. if (this->chipsize > (32 << 20))
  355. au1550_write_byte(mtd, (u8)((page_addr >> 16) & 0x0f));
  356. }
  357. /* Latch in address */
  358. au1550_hwcontrol(mtd, NAND_CTL_CLRALE);
  359. }
  360. /*
  361. * Program and erase have their own busy handlers.
  362. * Status and sequential in need no delay.
  363. */
  364. switch (command) {
  365. case NAND_CMD_PAGEPROG:
  366. case NAND_CMD_ERASE1:
  367. case NAND_CMD_ERASE2:
  368. case NAND_CMD_SEQIN:
  369. case NAND_CMD_STATUS:
  370. return;
  371. case NAND_CMD_RESET:
  372. break;
  373. case NAND_CMD_READ0:
  374. case NAND_CMD_READ1:
  375. case NAND_CMD_READOOB:
  376. /* Check if we're really driving -CE low (just in case) */
  377. if (unlikely(!ce_override))
  378. break;
  379. /* Apply a short delay always to ensure that we do wait tWB. */
  380. ndelay(100);
  381. /* Wait for a chip to become ready... */
  382. for (i = this->chip_delay; !this->dev_ready(mtd) && i > 0; --i)
  383. udelay(1);
  384. /* Release -CE and re-enable interrupts. */
  385. au1550_hwcontrol(mtd, NAND_CTL_CLRNCE);
  386. local_irq_restore(flags);
  387. return;
  388. }
  389. /* Apply this short delay always to ensure that we do wait tWB. */
  390. ndelay(100);
  391. while(!this->dev_ready(mtd));
  392. }
  393. /*
  394. * Main initialization routine
  395. */
  396. static int __init au1xxx_nand_init(void)
  397. {
  398. struct nand_chip *this;
  399. u16 boot_swapboot = 0; /* default value */
  400. int retval;
  401. u32 mem_staddr;
  402. u32 nand_phys;
  403. /* Allocate memory for MTD device structure and private data */
  404. au1550_mtd = kzalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip), GFP_KERNEL);
  405. if (!au1550_mtd) {
  406. printk("Unable to allocate NAND MTD dev structure.\n");
  407. return -ENOMEM;
  408. }
  409. /* Get pointer to private data */
  410. this = (struct nand_chip *)(&au1550_mtd[1]);
  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. gpio_direction_input(206);
  419. boot_swapboot = (au_readl(MEM_STSTAT) & (0x7 << 1)) | ((bcsr_read(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 = 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. mtd_device_register(au1550_mtd, partition_info,
  515. ARRAY_SIZE(partition_info));
  516. return 0;
  517. outio:
  518. iounmap(p_nand);
  519. outmem:
  520. kfree(au1550_mtd);
  521. return retval;
  522. }
  523. module_init(au1xxx_nand_init);
  524. /*
  525. * Clean up routine
  526. */
  527. static void __exit au1550_cleanup(void)
  528. {
  529. /* Release resources, unregister device */
  530. nand_release(au1550_mtd);
  531. /* Free the MTD device structure */
  532. kfree(au1550_mtd);
  533. /* Unmap */
  534. iounmap(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");