xway_nand.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * This program is free software; you can redistribute it and/or modify it
  3. * under the terms of the GNU General Public License version 2 as published
  4. * by the Free Software Foundation.
  5. *
  6. * Copyright © 2012 John Crispin <blogic@openwrt.org>
  7. */
  8. #include <linux/mtd/nand.h>
  9. #include <linux/of_gpio.h>
  10. #include <linux/of_platform.h>
  11. #include <lantiq_soc.h>
  12. /* nand registers */
  13. #define EBU_ADDSEL1 0x24
  14. #define EBU_NAND_CON 0xB0
  15. #define EBU_NAND_WAIT 0xB4
  16. #define EBU_NAND_ECC0 0xB8
  17. #define EBU_NAND_ECC_AC 0xBC
  18. /* nand commands */
  19. #define NAND_CMD_ALE (1 << 2)
  20. #define NAND_CMD_CLE (1 << 3)
  21. #define NAND_CMD_CS (1 << 4)
  22. #define NAND_WRITE_CMD_RESET 0xff
  23. #define NAND_WRITE_CMD (NAND_CMD_CS | NAND_CMD_CLE)
  24. #define NAND_WRITE_ADDR (NAND_CMD_CS | NAND_CMD_ALE)
  25. #define NAND_WRITE_DATA (NAND_CMD_CS)
  26. #define NAND_READ_DATA (NAND_CMD_CS)
  27. #define NAND_WAIT_WR_C (1 << 3)
  28. #define NAND_WAIT_RD (0x1)
  29. /* we need to tel the ebu which addr we mapped the nand to */
  30. #define ADDSEL1_MASK(x) (x << 4)
  31. #define ADDSEL1_REGEN 1
  32. /* we need to tell the EBU that we have nand attached and set it up properly */
  33. #define BUSCON1_SETUP (1 << 22)
  34. #define BUSCON1_BCGEN_RES (0x3 << 12)
  35. #define BUSCON1_WAITWRC2 (2 << 8)
  36. #define BUSCON1_WAITRDC2 (2 << 6)
  37. #define BUSCON1_HOLDC1 (1 << 4)
  38. #define BUSCON1_RECOVC1 (1 << 2)
  39. #define BUSCON1_CMULT4 1
  40. #define NAND_CON_CE (1 << 20)
  41. #define NAND_CON_OUT_CS1 (1 << 10)
  42. #define NAND_CON_IN_CS1 (1 << 8)
  43. #define NAND_CON_PRE_P (1 << 7)
  44. #define NAND_CON_WP_P (1 << 6)
  45. #define NAND_CON_SE_P (1 << 5)
  46. #define NAND_CON_CS_P (1 << 4)
  47. #define NAND_CON_CSMUX (1 << 1)
  48. #define NAND_CON_NANDM 1
  49. static void xway_reset_chip(struct nand_chip *chip)
  50. {
  51. unsigned long nandaddr = (unsigned long) chip->IO_ADDR_W;
  52. unsigned long flags;
  53. nandaddr &= ~NAND_WRITE_ADDR;
  54. nandaddr |= NAND_WRITE_CMD;
  55. /* finish with a reset */
  56. spin_lock_irqsave(&ebu_lock, flags);
  57. writeb(NAND_WRITE_CMD_RESET, (void __iomem *) nandaddr);
  58. while ((ltq_ebu_r32(EBU_NAND_WAIT) & NAND_WAIT_WR_C) == 0)
  59. ;
  60. spin_unlock_irqrestore(&ebu_lock, flags);
  61. }
  62. static void xway_select_chip(struct mtd_info *mtd, int chip)
  63. {
  64. switch (chip) {
  65. case -1:
  66. ltq_ebu_w32_mask(NAND_CON_CE, 0, EBU_NAND_CON);
  67. ltq_ebu_w32_mask(NAND_CON_NANDM, 0, EBU_NAND_CON);
  68. break;
  69. case 0:
  70. ltq_ebu_w32_mask(0, NAND_CON_NANDM, EBU_NAND_CON);
  71. ltq_ebu_w32_mask(0, NAND_CON_CE, EBU_NAND_CON);
  72. break;
  73. default:
  74. BUG();
  75. }
  76. }
  77. static void xway_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  78. {
  79. struct nand_chip *this = mtd->priv;
  80. unsigned long nandaddr = (unsigned long) this->IO_ADDR_W;
  81. unsigned long flags;
  82. if (ctrl & NAND_CTRL_CHANGE) {
  83. nandaddr &= ~(NAND_WRITE_CMD | NAND_WRITE_ADDR);
  84. if (ctrl & NAND_CLE)
  85. nandaddr |= NAND_WRITE_CMD;
  86. else
  87. nandaddr |= NAND_WRITE_ADDR;
  88. this->IO_ADDR_W = (void __iomem *) nandaddr;
  89. }
  90. if (cmd != NAND_CMD_NONE) {
  91. spin_lock_irqsave(&ebu_lock, flags);
  92. writeb(cmd, this->IO_ADDR_W);
  93. while ((ltq_ebu_r32(EBU_NAND_WAIT) & NAND_WAIT_WR_C) == 0)
  94. ;
  95. spin_unlock_irqrestore(&ebu_lock, flags);
  96. }
  97. }
  98. static int xway_dev_ready(struct mtd_info *mtd)
  99. {
  100. return ltq_ebu_r32(EBU_NAND_WAIT) & NAND_WAIT_RD;
  101. }
  102. static unsigned char xway_read_byte(struct mtd_info *mtd)
  103. {
  104. struct nand_chip *this = mtd->priv;
  105. unsigned long nandaddr = (unsigned long) this->IO_ADDR_R;
  106. unsigned long flags;
  107. int ret;
  108. spin_lock_irqsave(&ebu_lock, flags);
  109. ret = ltq_r8((void __iomem *)(nandaddr + NAND_READ_DATA));
  110. spin_unlock_irqrestore(&ebu_lock, flags);
  111. return ret;
  112. }
  113. static int xway_nand_probe(struct platform_device *pdev)
  114. {
  115. struct nand_chip *this = platform_get_drvdata(pdev);
  116. unsigned long nandaddr = (unsigned long) this->IO_ADDR_W;
  117. const __be32 *cs = of_get_property(pdev->dev.of_node,
  118. "lantiq,cs", NULL);
  119. u32 cs_flag = 0;
  120. /* load our CS from the DT. Either we find a valid 1 or default to 0 */
  121. if (cs && (*cs == 1))
  122. cs_flag = NAND_CON_IN_CS1 | NAND_CON_OUT_CS1;
  123. /* setup the EBU to run in NAND mode on our base addr */
  124. ltq_ebu_w32(CPHYSADDR(nandaddr)
  125. | ADDSEL1_MASK(3) | ADDSEL1_REGEN, EBU_ADDSEL1);
  126. ltq_ebu_w32(BUSCON1_SETUP | BUSCON1_BCGEN_RES | BUSCON1_WAITWRC2
  127. | BUSCON1_WAITRDC2 | BUSCON1_HOLDC1 | BUSCON1_RECOVC1
  128. | BUSCON1_CMULT4, LTQ_EBU_BUSCON1);
  129. ltq_ebu_w32(NAND_CON_NANDM | NAND_CON_CSMUX | NAND_CON_CS_P
  130. | NAND_CON_SE_P | NAND_CON_WP_P | NAND_CON_PRE_P
  131. | cs_flag, EBU_NAND_CON);
  132. /* finish with a reset */
  133. xway_reset_chip(this);
  134. return 0;
  135. }
  136. static struct platform_nand_data xway_nand_data = {
  137. .chip = {
  138. .nr_chips = 1,
  139. .chip_delay = 30,
  140. },
  141. .ctrl = {
  142. .probe = xway_nand_probe,
  143. .cmd_ctrl = xway_cmd_ctrl,
  144. .dev_ready = xway_dev_ready,
  145. .select_chip = xway_select_chip,
  146. .read_byte = xway_read_byte,
  147. }
  148. };
  149. /*
  150. * Try to find the node inside the DT. If it is available attach out
  151. * platform_nand_data
  152. */
  153. static int __init xway_register_nand(void)
  154. {
  155. struct device_node *node;
  156. struct platform_device *pdev;
  157. node = of_find_compatible_node(NULL, NULL, "lantiq,nand-xway");
  158. if (!node)
  159. return -ENOENT;
  160. pdev = of_find_device_by_node(node);
  161. if (!pdev)
  162. return -EINVAL;
  163. pdev->dev.platform_data = &xway_nand_data;
  164. of_node_put(node);
  165. return 0;
  166. }
  167. subsys_initcall(xway_register_nand);