au1550nd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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/mtd/mtd.h>
  17. #include <linux/mtd/nand.h>
  18. #include <linux/mtd/partitions.h>
  19. #include <linux/version.h>
  20. #include <asm/io.h>
  21. /* fixme: this is ugly */
  22. #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 0)
  23. #include <asm/mach-au1x00/au1xxx.h>
  24. #else
  25. #include <asm/au1000.h>
  26. #ifdef CONFIG_MIPS_PB1550
  27. #include <asm/pb1550.h>
  28. #endif
  29. #ifdef CONFIG_MIPS_DB1550
  30. #include <asm/db1x00.h>
  31. #endif
  32. #endif
  33. /*
  34. * MTD structure for NAND controller
  35. */
  36. static struct mtd_info *au1550_mtd = NULL;
  37. static void __iomem *p_nand;
  38. static int nand_width = 1; /* default x8*/
  39. /*
  40. * Define partitions for flash device
  41. */
  42. static const struct mtd_partition partition_info[] = {
  43. {
  44. .name = "NAND FS 0",
  45. .offset = 0,
  46. .size = 8*1024*1024
  47. },
  48. {
  49. .name = "NAND FS 1",
  50. .offset = MTDPART_OFS_APPEND,
  51. .size = MTDPART_SIZ_FULL
  52. }
  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_word - write one word to the chip
  124. * @mtd: MTD device structure
  125. * @word: data word to write
  126. *
  127. * write function for 16bit buswith without
  128. * endianess conversion
  129. */
  130. static void au_write_word(struct mtd_info *mtd, u16 word)
  131. {
  132. struct nand_chip *this = mtd->priv;
  133. writew(word, this->IO_ADDR_W);
  134. au_sync();
  135. }
  136. /**
  137. * au_write_buf - write buffer to chip
  138. * @mtd: MTD device structure
  139. * @buf: data buffer
  140. * @len: number of bytes to write
  141. *
  142. * write function for 8bit buswith
  143. */
  144. static void au_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
  145. {
  146. int i;
  147. struct nand_chip *this = mtd->priv;
  148. for (i=0; i<len; i++) {
  149. writeb(buf[i], this->IO_ADDR_W);
  150. au_sync();
  151. }
  152. }
  153. /**
  154. * au_read_buf - read chip data into buffer
  155. * @mtd: MTD device structure
  156. * @buf: buffer to store date
  157. * @len: number of bytes to read
  158. *
  159. * read function for 8bit buswith
  160. */
  161. static void au_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  162. {
  163. int i;
  164. struct nand_chip *this = mtd->priv;
  165. for (i=0; i<len; i++) {
  166. buf[i] = readb(this->IO_ADDR_R);
  167. au_sync();
  168. }
  169. }
  170. /**
  171. * au_verify_buf - Verify chip data against buffer
  172. * @mtd: MTD device structure
  173. * @buf: buffer containing the data to compare
  174. * @len: number of bytes to compare
  175. *
  176. * verify function for 8bit buswith
  177. */
  178. static int au_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
  179. {
  180. int i;
  181. struct nand_chip *this = mtd->priv;
  182. for (i=0; i<len; i++) {
  183. if (buf[i] != readb(this->IO_ADDR_R))
  184. return -EFAULT;
  185. au_sync();
  186. }
  187. return 0;
  188. }
  189. /**
  190. * au_write_buf16 - write buffer to chip
  191. * @mtd: MTD device structure
  192. * @buf: data buffer
  193. * @len: number of bytes to write
  194. *
  195. * write function for 16bit buswith
  196. */
  197. static void au_write_buf16(struct mtd_info *mtd, const u_char *buf, int len)
  198. {
  199. int i;
  200. struct nand_chip *this = mtd->priv;
  201. u16 *p = (u16 *) buf;
  202. len >>= 1;
  203. for (i=0; i<len; i++) {
  204. writew(p[i], this->IO_ADDR_W);
  205. au_sync();
  206. }
  207. }
  208. /**
  209. * au_read_buf16 - read chip data into buffer
  210. * @mtd: MTD device structure
  211. * @buf: buffer to store date
  212. * @len: number of bytes to read
  213. *
  214. * read function for 16bit buswith
  215. */
  216. static void au_read_buf16(struct mtd_info *mtd, u_char *buf, int len)
  217. {
  218. int i;
  219. struct nand_chip *this = mtd->priv;
  220. u16 *p = (u16 *) buf;
  221. len >>= 1;
  222. for (i=0; i<len; i++) {
  223. p[i] = readw(this->IO_ADDR_R);
  224. au_sync();
  225. }
  226. }
  227. /**
  228. * au_verify_buf16 - Verify chip data against buffer
  229. * @mtd: MTD device structure
  230. * @buf: buffer containing the data to compare
  231. * @len: number of bytes to compare
  232. *
  233. * verify function for 16bit buswith
  234. */
  235. static int au_verify_buf16(struct mtd_info *mtd, const u_char *buf, int len)
  236. {
  237. int i;
  238. struct nand_chip *this = mtd->priv;
  239. u16 *p = (u16 *) buf;
  240. len >>= 1;
  241. for (i=0; i<len; i++) {
  242. if (p[i] != readw(this->IO_ADDR_R))
  243. return -EFAULT;
  244. au_sync();
  245. }
  246. return 0;
  247. }
  248. static void au1550_hwcontrol(struct mtd_info *mtd, int cmd)
  249. {
  250. register struct nand_chip *this = mtd->priv;
  251. switch(cmd){
  252. case NAND_CTL_SETCLE: this->IO_ADDR_W = p_nand + MEM_STNAND_CMD; break;
  253. case NAND_CTL_CLRCLE: this->IO_ADDR_W = p_nand + MEM_STNAND_DATA; break;
  254. case NAND_CTL_SETALE: this->IO_ADDR_W = p_nand + MEM_STNAND_ADDR; break;
  255. case NAND_CTL_CLRALE:
  256. this->IO_ADDR_W = p_nand + MEM_STNAND_DATA;
  257. /* FIXME: Nobody knows why this is neccecary,
  258. * but it works only that way */
  259. udelay(1);
  260. break;
  261. case NAND_CTL_SETNCE:
  262. /* assert (force assert) chip enable */
  263. au_writel((1<<(4+NAND_CS)) , MEM_STNDCTL); break;
  264. break;
  265. case NAND_CTL_CLRNCE:
  266. /* deassert chip enable */
  267. au_writel(0, MEM_STNDCTL); break;
  268. break;
  269. }
  270. this->IO_ADDR_R = this->IO_ADDR_W;
  271. /* Drain the writebuffer */
  272. au_sync();
  273. }
  274. int au1550_device_ready(struct mtd_info *mtd)
  275. {
  276. int ret = (au_readl(MEM_STSTAT) & 0x1) ? 1 : 0;
  277. au_sync();
  278. return ret;
  279. }
  280. /*
  281. * Main initialization routine
  282. */
  283. int __init au1xxx_nand_init (void)
  284. {
  285. struct nand_chip *this;
  286. u16 boot_swapboot = 0; /* default value */
  287. int retval;
  288. u32 mem_staddr;
  289. u32 nand_phys;
  290. /* Allocate memory for MTD device structure and private data */
  291. au1550_mtd = kmalloc (sizeof(struct mtd_info) +
  292. sizeof (struct nand_chip), GFP_KERNEL);
  293. if (!au1550_mtd) {
  294. printk ("Unable to allocate NAND MTD dev structure.\n");
  295. return -ENOMEM;
  296. }
  297. /* Get pointer to private data */
  298. this = (struct nand_chip *) (&au1550_mtd[1]);
  299. /* Initialize structures */
  300. memset((char *) au1550_mtd, 0, sizeof(struct mtd_info));
  301. memset((char *) this, 0, sizeof(struct nand_chip));
  302. /* Link the private data with the MTD structure */
  303. au1550_mtd->priv = this;
  304. /* disable interrupts */
  305. au_writel(au_readl(MEM_STNDCTL) & ~(1<<8), MEM_STNDCTL);
  306. /* disable NAND boot */
  307. au_writel(au_readl(MEM_STNDCTL) & ~(1<<0), MEM_STNDCTL);
  308. #ifdef CONFIG_MIPS_PB1550
  309. /* set gpio206 high */
  310. au_writel(au_readl(GPIO2_DIR) & ~(1<<6), GPIO2_DIR);
  311. boot_swapboot = (au_readl(MEM_STSTAT) & (0x7<<1)) |
  312. ((bcsr->status >> 6) & 0x1);
  313. switch (boot_swapboot) {
  314. case 0:
  315. case 2:
  316. case 8:
  317. case 0xC:
  318. case 0xD:
  319. /* x16 NAND Flash */
  320. nand_width = 0;
  321. break;
  322. case 1:
  323. case 9:
  324. case 3:
  325. case 0xE:
  326. case 0xF:
  327. /* x8 NAND Flash */
  328. nand_width = 1;
  329. break;
  330. default:
  331. printk("Pb1550 NAND: bad boot:swap\n");
  332. retval = -EINVAL;
  333. goto outmem;
  334. }
  335. #endif
  336. /* Configure chip-select; normally done by boot code, e.g. YAMON */
  337. #ifdef NAND_STCFG
  338. if (NAND_CS == 0) {
  339. au_writel(NAND_STCFG, MEM_STCFG0);
  340. au_writel(NAND_STTIME, MEM_STTIME0);
  341. au_writel(NAND_STADDR, MEM_STADDR0);
  342. }
  343. if (NAND_CS == 1) {
  344. au_writel(NAND_STCFG, MEM_STCFG1);
  345. au_writel(NAND_STTIME, MEM_STTIME1);
  346. au_writel(NAND_STADDR, MEM_STADDR1);
  347. }
  348. if (NAND_CS == 2) {
  349. au_writel(NAND_STCFG, MEM_STCFG2);
  350. au_writel(NAND_STTIME, MEM_STTIME2);
  351. au_writel(NAND_STADDR, MEM_STADDR2);
  352. }
  353. if (NAND_CS == 3) {
  354. au_writel(NAND_STCFG, MEM_STCFG3);
  355. au_writel(NAND_STTIME, MEM_STTIME3);
  356. au_writel(NAND_STADDR, MEM_STADDR3);
  357. }
  358. #endif
  359. /* Locate NAND chip-select in order to determine NAND phys address */
  360. mem_staddr = 0x00000000;
  361. if (((au_readl(MEM_STCFG0) & 0x7) == 0x5) && (NAND_CS == 0))
  362. mem_staddr = au_readl(MEM_STADDR0);
  363. else if (((au_readl(MEM_STCFG1) & 0x7) == 0x5) && (NAND_CS == 1))
  364. mem_staddr = au_readl(MEM_STADDR1);
  365. else if (((au_readl(MEM_STCFG2) & 0x7) == 0x5) && (NAND_CS == 2))
  366. mem_staddr = au_readl(MEM_STADDR2);
  367. else if (((au_readl(MEM_STCFG3) & 0x7) == 0x5) && (NAND_CS == 3))
  368. mem_staddr = au_readl(MEM_STADDR3);
  369. if (mem_staddr == 0x00000000) {
  370. printk("Au1xxx NAND: ERROR WITH NAND CHIP-SELECT\n");
  371. kfree(au1550_mtd);
  372. return 1;
  373. }
  374. nand_phys = (mem_staddr << 4) & 0xFFFC0000;
  375. p_nand = (void __iomem *)ioremap(nand_phys, 0x1000);
  376. /* make controller and MTD agree */
  377. if (NAND_CS == 0)
  378. nand_width = au_readl(MEM_STCFG0) & (1<<22);
  379. if (NAND_CS == 1)
  380. nand_width = au_readl(MEM_STCFG1) & (1<<22);
  381. if (NAND_CS == 2)
  382. nand_width = au_readl(MEM_STCFG2) & (1<<22);
  383. if (NAND_CS == 3)
  384. nand_width = au_readl(MEM_STCFG3) & (1<<22);
  385. /* Set address of hardware control function */
  386. this->hwcontrol = au1550_hwcontrol;
  387. this->dev_ready = au1550_device_ready;
  388. /* 30 us command delay time */
  389. this->chip_delay = 30;
  390. this->eccmode = NAND_ECC_SOFT;
  391. this->options = NAND_NO_AUTOINCR;
  392. if (!nand_width)
  393. this->options |= NAND_BUSWIDTH_16;
  394. this->read_byte = (!nand_width) ? au_read_byte16 : au_read_byte;
  395. this->write_byte = (!nand_width) ? au_write_byte16 : au_write_byte;
  396. this->write_word = au_write_word;
  397. this->read_word = au_read_word;
  398. this->write_buf = (!nand_width) ? au_write_buf16 : au_write_buf;
  399. this->read_buf = (!nand_width) ? au_read_buf16 : au_read_buf;
  400. this->verify_buf = (!nand_width) ? au_verify_buf16 : au_verify_buf;
  401. /* Scan to find existence of the device */
  402. if (nand_scan (au1550_mtd, 1)) {
  403. retval = -ENXIO;
  404. goto outio;
  405. }
  406. /* Register the partitions */
  407. add_mtd_partitions(au1550_mtd, partition_info, ARRAY_SIZE(partition_info));
  408. return 0;
  409. outio:
  410. iounmap ((void *)p_nand);
  411. outmem:
  412. kfree (au1550_mtd);
  413. return retval;
  414. }
  415. module_init(au1xxx_nand_init);
  416. /*
  417. * Clean up routine
  418. */
  419. #ifdef MODULE
  420. static void __exit au1550_cleanup (void)
  421. {
  422. struct nand_chip *this = (struct nand_chip *) &au1550_mtd[1];
  423. /* Release resources, unregister device */
  424. nand_release (au1550_mtd);
  425. /* Free the MTD device structure */
  426. kfree (au1550_mtd);
  427. /* Unmap */
  428. iounmap ((void *)p_nand);
  429. }
  430. module_exit(au1550_cleanup);
  431. #endif
  432. MODULE_LICENSE("GPL");
  433. MODULE_AUTHOR("Embedded Edge, LLC");
  434. MODULE_DESCRIPTION("Board-specific glue layer for NAND flash on Pb1550 board");