iproc_nand.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright © 2015 Broadcom Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/device.h>
  14. #include <linux/io.h>
  15. #include <linux/ioport.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/of_address.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/slab.h>
  21. #include "brcmnand.h"
  22. struct iproc_nand_soc {
  23. struct brcmnand_soc soc;
  24. void __iomem *idm_base;
  25. void __iomem *ext_base;
  26. spinlock_t idm_lock;
  27. };
  28. #define IPROC_NAND_CTLR_READY_OFFSET 0x10
  29. #define IPROC_NAND_CTLR_READY BIT(0)
  30. #define IPROC_NAND_IO_CTRL_OFFSET 0x00
  31. #define IPROC_NAND_APB_LE_MODE BIT(24)
  32. #define IPROC_NAND_INT_CTRL_READ_ENABLE BIT(6)
  33. static bool iproc_nand_intc_ack(struct brcmnand_soc *soc)
  34. {
  35. struct iproc_nand_soc *priv =
  36. container_of(soc, struct iproc_nand_soc, soc);
  37. void __iomem *mmio = priv->ext_base + IPROC_NAND_CTLR_READY_OFFSET;
  38. u32 val = brcmnand_readl(mmio);
  39. if (val & IPROC_NAND_CTLR_READY) {
  40. brcmnand_writel(IPROC_NAND_CTLR_READY, mmio);
  41. return true;
  42. }
  43. return false;
  44. }
  45. static void iproc_nand_intc_set(struct brcmnand_soc *soc, bool en)
  46. {
  47. struct iproc_nand_soc *priv =
  48. container_of(soc, struct iproc_nand_soc, soc);
  49. void __iomem *mmio = priv->idm_base + IPROC_NAND_IO_CTRL_OFFSET;
  50. u32 val;
  51. unsigned long flags;
  52. spin_lock_irqsave(&priv->idm_lock, flags);
  53. val = brcmnand_readl(mmio);
  54. if (en)
  55. val |= IPROC_NAND_INT_CTRL_READ_ENABLE;
  56. else
  57. val &= ~IPROC_NAND_INT_CTRL_READ_ENABLE;
  58. brcmnand_writel(val, mmio);
  59. spin_unlock_irqrestore(&priv->idm_lock, flags);
  60. }
  61. static void iproc_nand_apb_access(struct brcmnand_soc *soc, bool prepare,
  62. bool is_param)
  63. {
  64. struct iproc_nand_soc *priv =
  65. container_of(soc, struct iproc_nand_soc, soc);
  66. void __iomem *mmio = priv->idm_base + IPROC_NAND_IO_CTRL_OFFSET;
  67. u32 val;
  68. unsigned long flags;
  69. spin_lock_irqsave(&priv->idm_lock, flags);
  70. val = brcmnand_readl(mmio);
  71. /*
  72. * In the case of BE or when dealing with NAND data, alway configure
  73. * the APB bus to LE mode before accessing the FIFO and back to BE mode
  74. * after the access is done
  75. */
  76. if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) || !is_param) {
  77. if (prepare)
  78. val |= IPROC_NAND_APB_LE_MODE;
  79. else
  80. val &= ~IPROC_NAND_APB_LE_MODE;
  81. } else { /* when in LE accessing the parameter page, keep APB in BE */
  82. val &= ~IPROC_NAND_APB_LE_MODE;
  83. }
  84. brcmnand_writel(val, mmio);
  85. spin_unlock_irqrestore(&priv->idm_lock, flags);
  86. }
  87. static int iproc_nand_probe(struct platform_device *pdev)
  88. {
  89. struct device *dev = &pdev->dev;
  90. struct iproc_nand_soc *priv;
  91. struct brcmnand_soc *soc;
  92. struct resource *res;
  93. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  94. if (!priv)
  95. return -ENOMEM;
  96. soc = &priv->soc;
  97. spin_lock_init(&priv->idm_lock);
  98. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "iproc-idm");
  99. priv->idm_base = devm_ioremap_resource(dev, res);
  100. if (IS_ERR(priv->idm_base))
  101. return PTR_ERR(priv->idm_base);
  102. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "iproc-ext");
  103. priv->ext_base = devm_ioremap_resource(dev, res);
  104. if (IS_ERR(priv->ext_base))
  105. return PTR_ERR(priv->ext_base);
  106. soc->ctlrdy_ack = iproc_nand_intc_ack;
  107. soc->ctlrdy_set_enabled = iproc_nand_intc_set;
  108. soc->prepare_data_bus = iproc_nand_apb_access;
  109. return brcmnand_probe(pdev, soc);
  110. }
  111. static const struct of_device_id iproc_nand_of_match[] = {
  112. { .compatible = "brcm,nand-iproc" },
  113. {},
  114. };
  115. MODULE_DEVICE_TABLE(of, iproc_nand_of_match);
  116. static struct platform_driver iproc_nand_driver = {
  117. .probe = iproc_nand_probe,
  118. .remove = brcmnand_remove,
  119. .driver = {
  120. .name = "iproc_nand",
  121. .pm = &brcmnand_pm_ops,
  122. .of_match_table = iproc_nand_of_match,
  123. }
  124. };
  125. module_platform_driver(iproc_nand_driver);
  126. MODULE_LICENSE("GPL v2");
  127. MODULE_AUTHOR("Brian Norris");
  128. MODULE_AUTHOR("Ray Jui");
  129. MODULE_DESCRIPTION("NAND driver for Broadcom IPROC-based SoCs");