blob: 2753783ae70fe5aca7a6a8976b711ca7c78faf4a [file] [log] [blame]
Linus Walleij84998f42024-10-11 16:49:54 +02001// SPDX-License-Identifier: GPL-2.0+
2
3#include <asm/io.h>
4#include <memalign.h>
5#include <nand.h>
6#include <linux/bitops.h>
7#include <linux/err.h>
8#include <linux/errno.h>
9#include <linux/io.h>
10#include <linux/ioport.h>
11#include <dm.h>
12#include <linux/printk.h>
13
14#include "brcmnand.h"
15
16struct bcmbca_nand_soc {
17 struct brcmnand_soc soc;
18 void __iomem *base;
19};
20
21#define BCMBCA_NAND_INT 0x00
22#define BCMBCA_NAND_STATUS_SHIFT 0
23#define BCMBCA_NAND_STATUS_MASK (0xfff << BCMBCA_NAND_STATUS_SHIFT)
24
25#define BCMBCA_NAND_INT_EN 0x04
26#define BCMBCA_NAND_ENABLE_SHIFT 0
27#define BCMBCA_NAND_ENABLE_MASK (0xffff << BCMBCA_NAND_ENABLE_SHIFT)
28
29enum {
30 BCMBCA_NP_READ = BIT(0),
31 BCMBCA_BLOCK_ERASE = BIT(1),
32 BCMBCA_COPY_BACK = BIT(2),
33 BCMBCA_PAGE_PGM = BIT(3),
34 BCMBCA_CTRL_READY = BIT(4),
35 BCMBCA_DEV_RBPIN = BIT(5),
36 BCMBCA_ECC_ERR_UNC = BIT(6),
37 BCMBCA_ECC_ERR_CORR = BIT(7),
38};
39
40#if defined(CONFIG_ARM64)
41#define ALIGN_REQ 8
42#else
43#define ALIGN_REQ 4
44#endif
45
46static inline bool bcmbca_nand_is_buf_aligned(void *flash_cache, void *buffer)
47{
48 return IS_ALIGNED((uintptr_t)buffer, ALIGN_REQ) &&
49 IS_ALIGNED((uintptr_t)flash_cache, ALIGN_REQ);
50}
51
52static bool bcmbca_nand_intc_ack(struct brcmnand_soc *soc)
53{
54 struct bcmbca_nand_soc *priv =
55 container_of(soc, struct bcmbca_nand_soc, soc);
56 void __iomem *mmio = priv->base + BCMBCA_NAND_INT;
57 u32 val = brcmnand_readl(mmio);
58
59 if (val & (BCMBCA_CTRL_READY << BCMBCA_NAND_STATUS_SHIFT)) {
60 /* Ack interrupt */
61 val &= ~BCMBCA_NAND_STATUS_MASK;
62 val |= BCMBCA_CTRL_READY << BCMBCA_NAND_STATUS_SHIFT;
63 brcmnand_writel(val, mmio);
64 return true;
65 }
66
67 return false;
68}
69
70static void bcmbca_nand_intc_set(struct brcmnand_soc *soc, bool en)
71{
72 struct bcmbca_nand_soc *priv =
73 container_of(soc, struct bcmbca_nand_soc, soc);
74 void __iomem *mmio = priv->base + BCMBCA_NAND_INT_EN;
75 u32 val = brcmnand_readl(mmio);
76
77 /* Don't ack any interrupts */
78 val &= ~BCMBCA_NAND_STATUS_MASK;
79
80 if (en)
81 val |= BCMBCA_CTRL_READY << BCMBCA_NAND_ENABLE_SHIFT;
82 else
83 val &= ~(BCMBCA_CTRL_READY << BCMBCA_NAND_ENABLE_SHIFT);
84
85 brcmnand_writel(val, mmio);
86}
87
88static void bcmbca_read_data_bus(struct brcmnand_soc *soc,
89 void __iomem *flash_cache, u32 *buffer, int fc_words)
90{
91 /*
92 * memcpy can do unaligned aligned access depending on source
93 * and dest address, which is incompatible with nand cache. Fallback
94 * to the memcpy_fromio in such case
95 */
96 if (bcmbca_nand_is_buf_aligned((void __force *)flash_cache, buffer))
97 memcpy((void *)buffer, (void __force *)flash_cache, fc_words * 4);
98 else
99 memcpy_fromio((void *)buffer, flash_cache, fc_words * 4);
100}
101
102static int bcmbca_nand_probe(struct udevice *dev)
103{
104 struct udevice *pdev = dev;
105 struct bcmbca_nand_soc *priv = dev_get_priv(dev);
106 struct brcmnand_soc *soc;
107 struct resource res;
108
109 soc = &priv->soc;
110
111 dev_read_resource_byname(pdev, "nand-int-base", &res);
112 priv->base = devm_ioremap(dev, res.start, resource_size(&res));
113 if (IS_ERR(priv->base))
114 return PTR_ERR(priv->base);
115
116 soc->ctlrdy_ack = bcmbca_nand_intc_ack;
117 soc->ctlrdy_set_enabled = bcmbca_nand_intc_set;
118 soc->read_data_bus = bcmbca_read_data_bus;
119
120 /* Disable and ack all interrupts */
121 brcmnand_writel(0, priv->base + BCMBCA_NAND_INT_EN);
122 brcmnand_writel(0, priv->base + BCMBCA_NAND_INT);
123
124 return brcmnand_probe(pdev, soc);
125}
126
127static const struct udevice_id bcmbca_nand_dt_ids[] = {
128 {
129 .compatible = "brcm,nand-bcm63138",
130 },
131 { /* sentinel */ }
132};
133
134U_BOOT_DRIVER(bcmbca_nand) = {
135 .name = "bcmbca-nand",
136 .id = UCLASS_MTD,
137 .of_match = bcmbca_nand_dt_ids,
138 .probe = bcmbca_nand_probe,
139 .priv_auto = sizeof(struct bcmbca_nand_soc),
140};
141
142void board_nand_init(void)
143{
144 struct udevice *dev;
145 int ret;
146
147 ret = uclass_get_device_by_driver(UCLASS_MTD,
148 DM_DRIVER_GET(bcmbca_nand), &dev);
149 if (ret && ret != -ENODEV)
150 pr_err("Failed to initialize %s. (error %d)\n", dev->name,
151 ret);
152}