xway_nand.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. * Copyright © 2016 Hauke Mehrtens <hauke@hauke-m.de>
  8. */
  9. #include <linux/mtd/nand.h>
  10. #include <linux/of_gpio.h>
  11. #include <linux/of_platform.h>
  12. #include <lantiq_soc.h>
  13. /* nand registers */
  14. #define EBU_ADDSEL1 0x24
  15. #define EBU_NAND_CON 0xB0
  16. #define EBU_NAND_WAIT 0xB4
  17. #define NAND_WAIT_RD BIT(0) /* NAND flash status output */
  18. #define NAND_WAIT_WR_C BIT(3) /* NAND Write/Read complete */
  19. #define EBU_NAND_ECC0 0xB8
  20. #define EBU_NAND_ECC_AC 0xBC
  21. /*
  22. * nand commands
  23. * The pins of the NAND chip are selected based on the address bits of the
  24. * "register" read and write. There are no special registers, but an
  25. * address range and the lower address bits are used to activate the
  26. * correct line. For example when the bit (1 << 2) is set in the address
  27. * the ALE pin will be activated.
  28. */
  29. #define NAND_CMD_ALE BIT(2) /* address latch enable */
  30. #define NAND_CMD_CLE BIT(3) /* command latch enable */
  31. #define NAND_CMD_CS BIT(4) /* chip select */
  32. #define NAND_CMD_SE BIT(5) /* spare area access latch */
  33. #define NAND_CMD_WP BIT(6) /* write protect */
  34. #define NAND_WRITE_CMD (NAND_CMD_CS | NAND_CMD_CLE)
  35. #define NAND_WRITE_ADDR (NAND_CMD_CS | NAND_CMD_ALE)
  36. #define NAND_WRITE_DATA (NAND_CMD_CS)
  37. #define NAND_READ_DATA (NAND_CMD_CS)
  38. /* we need to tel the ebu which addr we mapped the nand to */
  39. #define ADDSEL1_MASK(x) (x << 4)
  40. #define ADDSEL1_REGEN 1
  41. /* we need to tell the EBU that we have nand attached and set it up properly */
  42. #define BUSCON1_SETUP (1 << 22)
  43. #define BUSCON1_BCGEN_RES (0x3 << 12)
  44. #define BUSCON1_WAITWRC2 (2 << 8)
  45. #define BUSCON1_WAITRDC2 (2 << 6)
  46. #define BUSCON1_HOLDC1 (1 << 4)
  47. #define BUSCON1_RECOVC1 (1 << 2)
  48. #define BUSCON1_CMULT4 1
  49. #define NAND_CON_CE (1 << 20)
  50. #define NAND_CON_OUT_CS1 (1 << 10)
  51. #define NAND_CON_IN_CS1 (1 << 8)
  52. #define NAND_CON_PRE_P (1 << 7)
  53. #define NAND_CON_WP_P (1 << 6)
  54. #define NAND_CON_SE_P (1 << 5)
  55. #define NAND_CON_CS_P (1 << 4)
  56. #define NAND_CON_CSMUX (1 << 1)
  57. #define NAND_CON_NANDM 1
  58. struct xway_nand_data {
  59. struct nand_chip chip;
  60. unsigned long csflags;
  61. void __iomem *nandaddr;
  62. };
  63. static u8 xway_readb(struct mtd_info *mtd, int op)
  64. {
  65. struct nand_chip *chip = mtd_to_nand(mtd);
  66. struct xway_nand_data *data = nand_get_controller_data(chip);
  67. return readb(data->nandaddr + op);
  68. }
  69. static void xway_writeb(struct mtd_info *mtd, int op, u8 value)
  70. {
  71. struct nand_chip *chip = mtd_to_nand(mtd);
  72. struct xway_nand_data *data = nand_get_controller_data(chip);
  73. writeb(value, data->nandaddr + op);
  74. }
  75. static void xway_select_chip(struct mtd_info *mtd, int select)
  76. {
  77. struct nand_chip *chip = mtd_to_nand(mtd);
  78. struct xway_nand_data *data = nand_get_controller_data(chip);
  79. switch (select) {
  80. case -1:
  81. ltq_ebu_w32_mask(NAND_CON_CE, 0, EBU_NAND_CON);
  82. ltq_ebu_w32_mask(NAND_CON_NANDM, 0, EBU_NAND_CON);
  83. spin_unlock_irqrestore(&ebu_lock, data->csflags);
  84. break;
  85. case 0:
  86. spin_lock_irqsave(&ebu_lock, data->csflags);
  87. ltq_ebu_w32_mask(0, NAND_CON_NANDM, EBU_NAND_CON);
  88. ltq_ebu_w32_mask(0, NAND_CON_CE, EBU_NAND_CON);
  89. break;
  90. default:
  91. BUG();
  92. }
  93. }
  94. static void xway_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  95. {
  96. if (cmd == NAND_CMD_NONE)
  97. return;
  98. if (ctrl & NAND_CLE)
  99. xway_writeb(mtd, NAND_WRITE_CMD, cmd);
  100. else if (ctrl & NAND_ALE)
  101. xway_writeb(mtd, NAND_WRITE_ADDR, cmd);
  102. while ((ltq_ebu_r32(EBU_NAND_WAIT) & NAND_WAIT_WR_C) == 0)
  103. ;
  104. }
  105. static int xway_dev_ready(struct mtd_info *mtd)
  106. {
  107. return ltq_ebu_r32(EBU_NAND_WAIT) & NAND_WAIT_RD;
  108. }
  109. static unsigned char xway_read_byte(struct mtd_info *mtd)
  110. {
  111. return xway_readb(mtd, NAND_READ_DATA);
  112. }
  113. static void xway_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  114. {
  115. int i;
  116. for (i = 0; i < len; i++)
  117. buf[i] = xway_readb(mtd, NAND_WRITE_DATA);
  118. }
  119. static void xway_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
  120. {
  121. int i;
  122. for (i = 0; i < len; i++)
  123. xway_writeb(mtd, NAND_WRITE_DATA, buf[i]);
  124. }
  125. /*
  126. * Probe for the NAND device.
  127. */
  128. static int xway_nand_probe(struct platform_device *pdev)
  129. {
  130. struct xway_nand_data *data;
  131. struct mtd_info *mtd;
  132. struct resource *res;
  133. int err;
  134. u32 cs;
  135. u32 cs_flag = 0;
  136. /* Allocate memory for the device structure (and zero it) */
  137. data = devm_kzalloc(&pdev->dev, sizeof(struct xway_nand_data),
  138. GFP_KERNEL);
  139. if (!data)
  140. return -ENOMEM;
  141. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  142. data->nandaddr = devm_ioremap_resource(&pdev->dev, res);
  143. if (IS_ERR(data->nandaddr))
  144. return PTR_ERR(data->nandaddr);
  145. nand_set_flash_node(&data->chip, pdev->dev.of_node);
  146. mtd = nand_to_mtd(&data->chip);
  147. mtd->dev.parent = &pdev->dev;
  148. data->chip.cmd_ctrl = xway_cmd_ctrl;
  149. data->chip.dev_ready = xway_dev_ready;
  150. data->chip.select_chip = xway_select_chip;
  151. data->chip.write_buf = xway_write_buf;
  152. data->chip.read_buf = xway_read_buf;
  153. data->chip.read_byte = xway_read_byte;
  154. data->chip.chip_delay = 30;
  155. data->chip.ecc.mode = NAND_ECC_SOFT;
  156. data->chip.ecc.algo = NAND_ECC_HAMMING;
  157. platform_set_drvdata(pdev, data);
  158. nand_set_controller_data(&data->chip, data);
  159. /* load our CS from the DT. Either we find a valid 1 or default to 0 */
  160. err = of_property_read_u32(pdev->dev.of_node, "lantiq,cs", &cs);
  161. if (!err && cs == 1)
  162. cs_flag = NAND_CON_IN_CS1 | NAND_CON_OUT_CS1;
  163. /* setup the EBU to run in NAND mode on our base addr */
  164. ltq_ebu_w32(CPHYSADDR(data->nandaddr)
  165. | ADDSEL1_MASK(3) | ADDSEL1_REGEN, EBU_ADDSEL1);
  166. ltq_ebu_w32(BUSCON1_SETUP | BUSCON1_BCGEN_RES | BUSCON1_WAITWRC2
  167. | BUSCON1_WAITRDC2 | BUSCON1_HOLDC1 | BUSCON1_RECOVC1
  168. | BUSCON1_CMULT4, LTQ_EBU_BUSCON1);
  169. ltq_ebu_w32(NAND_CON_NANDM | NAND_CON_CSMUX | NAND_CON_CS_P
  170. | NAND_CON_SE_P | NAND_CON_WP_P | NAND_CON_PRE_P
  171. | cs_flag, EBU_NAND_CON);
  172. /* Scan to find existence of the device */
  173. err = nand_scan(mtd, 1);
  174. if (err)
  175. return err;
  176. err = mtd_device_register(mtd, NULL, 0);
  177. if (err)
  178. nand_release(mtd);
  179. return err;
  180. }
  181. /*
  182. * Remove a NAND device.
  183. */
  184. static int xway_nand_remove(struct platform_device *pdev)
  185. {
  186. struct xway_nand_data *data = platform_get_drvdata(pdev);
  187. nand_release(nand_to_mtd(&data->chip));
  188. return 0;
  189. }
  190. static const struct of_device_id xway_nand_match[] = {
  191. { .compatible = "lantiq,nand-xway" },
  192. {},
  193. };
  194. static struct platform_driver xway_nand_driver = {
  195. .probe = xway_nand_probe,
  196. .remove = xway_nand_remove,
  197. .driver = {
  198. .name = "lantiq,nand-xway",
  199. .of_match_table = xway_nand_match,
  200. },
  201. };
  202. builtin_platform_driver(xway_nand_driver);