au1550nd.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*
  2. * drivers/mtd/nand/au1550nd.c
  3. *
  4. * Copyright (C) 2004 Embedded Edge, LLC
  5. *
  6. * $Id: au1550nd.c,v 1.11 2004/11/04 12:53:10 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/mtd/mtd.h>
  17. #include <linux/mtd/nand.h>
  18. #include <linux/mtd/partitions.h>
  19. #include <asm/io.h>
  20. /* fixme: this is ugly */
  21. #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 0)
  22. #include <asm/mach-au1x00/au1000.h>
  23. #ifdef CONFIG_MIPS_PB1550
  24. #include <asm/mach-pb1x00/pb1550.h>
  25. #endif
  26. #ifdef CONFIG_MIPS_DB1550
  27. #include <asm/mach-db1x00/db1x00.h>
  28. #endif
  29. #else
  30. #include <asm/au1000.h>
  31. #ifdef CONFIG_MIPS_PB1550
  32. #include <asm/pb1550.h>
  33. #endif
  34. #ifdef CONFIG_MIPS_DB1550
  35. #include <asm/db1x00.h>
  36. #endif
  37. #endif
  38. /*
  39. * MTD structure for NAND controller
  40. */
  41. static struct mtd_info *au1550_mtd = NULL;
  42. static void __iomem *p_nand;
  43. static int nand_width = 1; /* default x8*/
  44. #define NAND_CS 1
  45. /*
  46. * Define partitions for flash device
  47. */
  48. const static struct mtd_partition partition_info[] = {
  49. #ifdef CONFIG_MIPS_PB1550
  50. #define NUM_PARTITIONS 2
  51. {
  52. .name = "Pb1550 NAND FS 0",
  53. .offset = 0,
  54. .size = 8*1024*1024
  55. },
  56. {
  57. .name = "Pb1550 NAND FS 1",
  58. .offset = MTDPART_OFS_APPEND,
  59. .size = MTDPART_SIZ_FULL
  60. }
  61. #endif
  62. #ifdef CONFIG_MIPS_DB1550
  63. #define NUM_PARTITIONS 2
  64. {
  65. .name = "Db1550 NAND FS 0",
  66. .offset = 0,
  67. .size = 8*1024*1024
  68. },
  69. {
  70. .name = "Db1550 NAND FS 1",
  71. .offset = MTDPART_OFS_APPEND,
  72. .size = MTDPART_SIZ_FULL
  73. }
  74. #endif
  75. };
  76. /**
  77. * au_read_byte - read one byte from the chip
  78. * @mtd: MTD device structure
  79. *
  80. * read function for 8bit buswith
  81. */
  82. static u_char au_read_byte(struct mtd_info *mtd)
  83. {
  84. struct nand_chip *this = mtd->priv;
  85. u_char ret = readb(this->IO_ADDR_R);
  86. au_sync();
  87. return ret;
  88. }
  89. /**
  90. * au_write_byte - write one byte to the chip
  91. * @mtd: MTD device structure
  92. * @byte: pointer to data byte to write
  93. *
  94. * write function for 8it buswith
  95. */
  96. static void au_write_byte(struct mtd_info *mtd, u_char byte)
  97. {
  98. struct nand_chip *this = mtd->priv;
  99. writeb(byte, this->IO_ADDR_W);
  100. au_sync();
  101. }
  102. /**
  103. * au_read_byte16 - read one byte endianess aware from the chip
  104. * @mtd: MTD device structure
  105. *
  106. * read function for 16bit buswith with
  107. * endianess conversion
  108. */
  109. static u_char au_read_byte16(struct mtd_info *mtd)
  110. {
  111. struct nand_chip *this = mtd->priv;
  112. u_char ret = (u_char) cpu_to_le16(readw(this->IO_ADDR_R));
  113. au_sync();
  114. return ret;
  115. }
  116. /**
  117. * au_write_byte16 - write one byte endianess aware to the chip
  118. * @mtd: MTD device structure
  119. * @byte: pointer to data byte to write
  120. *
  121. * write function for 16bit buswith with
  122. * endianess conversion
  123. */
  124. static void au_write_byte16(struct mtd_info *mtd, u_char byte)
  125. {
  126. struct nand_chip *this = mtd->priv;
  127. writew(le16_to_cpu((u16) byte), this->IO_ADDR_W);
  128. au_sync();
  129. }
  130. /**
  131. * au_read_word - read one word from the chip
  132. * @mtd: MTD device structure
  133. *
  134. * read function for 16bit buswith without
  135. * endianess conversion
  136. */
  137. static u16 au_read_word(struct mtd_info *mtd)
  138. {
  139. struct nand_chip *this = mtd->priv;
  140. u16 ret = readw(this->IO_ADDR_R);
  141. au_sync();
  142. return ret;
  143. }
  144. /**
  145. * au_write_word - write one word to the chip
  146. * @mtd: MTD device structure
  147. * @word: data word to write
  148. *
  149. * write function for 16bit buswith without
  150. * endianess conversion
  151. */
  152. static void au_write_word(struct mtd_info *mtd, u16 word)
  153. {
  154. struct nand_chip *this = mtd->priv;
  155. writew(word, this->IO_ADDR_W);
  156. au_sync();
  157. }
  158. /**
  159. * au_write_buf - write buffer to chip
  160. * @mtd: MTD device structure
  161. * @buf: data buffer
  162. * @len: number of bytes to write
  163. *
  164. * write function for 8bit buswith
  165. */
  166. static void au_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
  167. {
  168. int i;
  169. struct nand_chip *this = mtd->priv;
  170. for (i=0; i<len; i++) {
  171. writeb(buf[i], this->IO_ADDR_W);
  172. au_sync();
  173. }
  174. }
  175. /**
  176. * au_read_buf - read chip data into buffer
  177. * @mtd: MTD device structure
  178. * @buf: buffer to store date
  179. * @len: number of bytes to read
  180. *
  181. * read function for 8bit buswith
  182. */
  183. static void au_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  184. {
  185. int i;
  186. struct nand_chip *this = mtd->priv;
  187. for (i=0; i<len; i++) {
  188. buf[i] = readb(this->IO_ADDR_R);
  189. au_sync();
  190. }
  191. }
  192. /**
  193. * au_verify_buf - Verify chip data against buffer
  194. * @mtd: MTD device structure
  195. * @buf: buffer containing the data to compare
  196. * @len: number of bytes to compare
  197. *
  198. * verify function for 8bit buswith
  199. */
  200. static int au_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
  201. {
  202. int i;
  203. struct nand_chip *this = mtd->priv;
  204. for (i=0; i<len; i++) {
  205. if (buf[i] != readb(this->IO_ADDR_R))
  206. return -EFAULT;
  207. au_sync();
  208. }
  209. return 0;
  210. }
  211. /**
  212. * au_write_buf16 - write buffer to chip
  213. * @mtd: MTD device structure
  214. * @buf: data buffer
  215. * @len: number of bytes to write
  216. *
  217. * write function for 16bit buswith
  218. */
  219. static void au_write_buf16(struct mtd_info *mtd, const u_char *buf, int len)
  220. {
  221. int i;
  222. struct nand_chip *this = mtd->priv;
  223. u16 *p = (u16 *) buf;
  224. len >>= 1;
  225. for (i=0; i<len; i++) {
  226. writew(p[i], this->IO_ADDR_W);
  227. au_sync();
  228. }
  229. }
  230. /**
  231. * au_read_buf16 - read chip data into buffer
  232. * @mtd: MTD device structure
  233. * @buf: buffer to store date
  234. * @len: number of bytes to read
  235. *
  236. * read function for 16bit buswith
  237. */
  238. static void au_read_buf16(struct mtd_info *mtd, u_char *buf, int len)
  239. {
  240. int i;
  241. struct nand_chip *this = mtd->priv;
  242. u16 *p = (u16 *) buf;
  243. len >>= 1;
  244. for (i=0; i<len; i++) {
  245. p[i] = readw(this->IO_ADDR_R);
  246. au_sync();
  247. }
  248. }
  249. /**
  250. * au_verify_buf16 - Verify chip data against buffer
  251. * @mtd: MTD device structure
  252. * @buf: buffer containing the data to compare
  253. * @len: number of bytes to compare
  254. *
  255. * verify function for 16bit buswith
  256. */
  257. static int au_verify_buf16(struct mtd_info *mtd, const u_char *buf, int len)
  258. {
  259. int i;
  260. struct nand_chip *this = mtd->priv;
  261. u16 *p = (u16 *) buf;
  262. len >>= 1;
  263. for (i=0; i<len; i++) {
  264. if (p[i] != readw(this->IO_ADDR_R))
  265. return -EFAULT;
  266. au_sync();
  267. }
  268. return 0;
  269. }
  270. static void au1550_hwcontrol(struct mtd_info *mtd, int cmd)
  271. {
  272. register struct nand_chip *this = mtd->priv;
  273. switch(cmd){
  274. case NAND_CTL_SETCLE: this->IO_ADDR_W = p_nand + MEM_STNAND_CMD; break;
  275. case NAND_CTL_CLRCLE: this->IO_ADDR_W = p_nand + MEM_STNAND_DATA; break;
  276. case NAND_CTL_SETALE: this->IO_ADDR_W = p_nand + MEM_STNAND_ADDR; break;
  277. case NAND_CTL_CLRALE:
  278. this->IO_ADDR_W = p_nand + MEM_STNAND_DATA;
  279. /* FIXME: Nobody knows why this is neccecary,
  280. * but it works only that way */
  281. udelay(1);
  282. break;
  283. case NAND_CTL_SETNCE:
  284. /* assert (force assert) chip enable */
  285. au_writel((1<<(4+NAND_CS)) , MEM_STNDCTL); break;
  286. break;
  287. case NAND_CTL_CLRNCE:
  288. /* deassert chip enable */
  289. au_writel(0, MEM_STNDCTL); break;
  290. break;
  291. }
  292. this->IO_ADDR_R = this->IO_ADDR_W;
  293. /* Drain the writebuffer */
  294. au_sync();
  295. }
  296. int au1550_device_ready(struct mtd_info *mtd)
  297. {
  298. int ret = (au_readl(MEM_STSTAT) & 0x1) ? 1 : 0;
  299. au_sync();
  300. return ret;
  301. }
  302. /*
  303. * Main initialization routine
  304. */
  305. int __init au1550_init (void)
  306. {
  307. struct nand_chip *this;
  308. u16 boot_swapboot = 0; /* default value */
  309. int retval;
  310. /* Allocate memory for MTD device structure and private data */
  311. au1550_mtd = kmalloc (sizeof(struct mtd_info) +
  312. sizeof (struct nand_chip), GFP_KERNEL);
  313. if (!au1550_mtd) {
  314. printk ("Unable to allocate NAND MTD dev structure.\n");
  315. return -ENOMEM;
  316. }
  317. /* Get pointer to private data */
  318. this = (struct nand_chip *) (&au1550_mtd[1]);
  319. /* Initialize structures */
  320. memset((char *) au1550_mtd, 0, sizeof(struct mtd_info));
  321. memset((char *) this, 0, sizeof(struct nand_chip));
  322. /* Link the private data with the MTD structure */
  323. au1550_mtd->priv = this;
  324. /* MEM_STNDCTL: disable ints, disable nand boot */
  325. au_writel(0, MEM_STNDCTL);
  326. #ifdef CONFIG_MIPS_PB1550
  327. /* set gpio206 high */
  328. au_writel(au_readl(GPIO2_DIR) & ~(1<<6), GPIO2_DIR);
  329. boot_swapboot = (au_readl(MEM_STSTAT) & (0x7<<1)) |
  330. ((bcsr->status >> 6) & 0x1);
  331. switch (boot_swapboot) {
  332. case 0:
  333. case 2:
  334. case 8:
  335. case 0xC:
  336. case 0xD:
  337. /* x16 NAND Flash */
  338. nand_width = 0;
  339. break;
  340. case 1:
  341. case 9:
  342. case 3:
  343. case 0xE:
  344. case 0xF:
  345. /* x8 NAND Flash */
  346. nand_width = 1;
  347. break;
  348. default:
  349. printk("Pb1550 NAND: bad boot:swap\n");
  350. retval = -EINVAL;
  351. goto outmem;
  352. }
  353. #endif
  354. /* Configure RCE1 - should be done by YAMON */
  355. au_writel(0x5 | (nand_width << 22), 0xB4001010); /* MEM_STCFG1 */
  356. au_writel(NAND_TIMING, 0xB4001014); /* MEM_STTIME1 */
  357. au_sync();
  358. /* setup and enable chip select, MEM_STADDR1 */
  359. /* we really need to decode offsets only up till 0x20 */
  360. au_writel((1<<28) | (NAND_PHYS_ADDR>>4) |
  361. (((NAND_PHYS_ADDR + 0x1000)-1) & (0x3fff<<18)>>18),
  362. MEM_STADDR1);
  363. au_sync();
  364. p_nand = ioremap(NAND_PHYS_ADDR, 0x1000);
  365. /* Set address of hardware control function */
  366. this->hwcontrol = au1550_hwcontrol;
  367. this->dev_ready = au1550_device_ready;
  368. /* 30 us command delay time */
  369. this->chip_delay = 30;
  370. this->eccmode = NAND_ECC_SOFT;
  371. this->options = NAND_NO_AUTOINCR;
  372. if (!nand_width)
  373. this->options |= NAND_BUSWIDTH_16;
  374. this->read_byte = (!nand_width) ? au_read_byte16 : au_read_byte;
  375. this->write_byte = (!nand_width) ? au_write_byte16 : au_write_byte;
  376. this->write_word = au_write_word;
  377. this->read_word = au_read_word;
  378. this->write_buf = (!nand_width) ? au_write_buf16 : au_write_buf;
  379. this->read_buf = (!nand_width) ? au_read_buf16 : au_read_buf;
  380. this->verify_buf = (!nand_width) ? au_verify_buf16 : au_verify_buf;
  381. /* Scan to find existence of the device */
  382. if (nand_scan (au1550_mtd, 1)) {
  383. retval = -ENXIO;
  384. goto outio;
  385. }
  386. /* Register the partitions */
  387. add_mtd_partitions(au1550_mtd, partition_info, NUM_PARTITIONS);
  388. return 0;
  389. outio:
  390. iounmap ((void *)p_nand);
  391. outmem:
  392. kfree (au1550_mtd);
  393. return retval;
  394. }
  395. module_init(au1550_init);
  396. /*
  397. * Clean up routine
  398. */
  399. #ifdef MODULE
  400. static void __exit au1550_cleanup (void)
  401. {
  402. struct nand_chip *this = (struct nand_chip *) &au1550_mtd[1];
  403. /* Release resources, unregister device */
  404. nand_release (au1550_mtd);
  405. /* Free the MTD device structure */
  406. kfree (au1550_mtd);
  407. /* Unmap */
  408. iounmap ((void *)p_nand);
  409. }
  410. module_exit(au1550_cleanup);
  411. #endif
  412. MODULE_LICENSE("GPL");
  413. MODULE_AUTHOR("Embedded Edge, LLC");
  414. MODULE_DESCRIPTION("Board-specific glue layer for NAND flash on Pb1550 board");