xway_nand.c 7.1 KB

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