ath25: switch default kernel to 5.15
[openwrt/staging/ldir.git] / target / linux / bcm47xx / patches-5.10 / 108-v5.18-mtd-rawnand-brcmnand-Add-BCMA-shim.patch
1 From: Florian Fainelli <f.fainelli@gmail.com>
2 Subject: [PATCH v3 9/9] mtd: rawnand: brcmnand: Add BCMA shim
3 Date: Fri, 07 Jan 2022 10:46:14 -0800
4 Content-Type: text/plain; charset="utf-8"
5
6 Add a BCMA shim to allow us to register the brcmnand driver using the
7 BCMA bus which provides indirect memory mapped access to SoC registers.
8
9 There are a number of registers that need to be byte swapped because
10 they are natively big endian, coming directly from the NAND chip, and
11 there is no bus interface unlike the iProc or STB platforms that
12 performs the byte swapping for us.
13
14 Signed-off-by: Florian Fainelli <f.fainelli@gmail.com>
15 ---
16 drivers/mtd/nand/raw/Kconfig | 13 +++
17 drivers/mtd/nand/raw/brcmnand/Makefile | 2 +
18 drivers/mtd/nand/raw/brcmnand/bcma_nand.c | 132 ++++++++++++++++++++++
19 drivers/mtd/nand/raw/brcmnand/brcmnand.c | 4 +
20 4 files changed, 151 insertions(+)
21 create mode 100644 drivers/mtd/nand/raw/brcmnand/bcma_nand.c
22
23 --- a/drivers/mtd/nand/raw/Kconfig
24 +++ b/drivers/mtd/nand/raw/Kconfig
25 @@ -236,6 +236,19 @@ config MTD_NAND_BRCMNAND
26 originally designed for Set-Top Box but is used on various BCM7xxx,
27 BCM3xxx, BCM63xxx, iProc/Cygnus and more.
28
29 +if MTD_NAND_BRCMNAND
30 +
31 +config MTD_NAND_BRCMNAND_BCMA
32 + tristate "Broadcom BCMA NAND controller"
33 + depends on BCMA_NFLASH
34 + depends on BCMA
35 + help
36 + Enables the BRCMNAND controller over BCMA on BCM47186/BCM5358 SoCs.
37 + The glue driver will take care of performing the low-level I/O
38 + operations to interface the BRCMNAND controller over the BCMA bus.
39 +
40 +endif # MTD_NAND_BRCMNAND
41 +
42 config MTD_NAND_BCM47XXNFLASH
43 tristate "BCM4706 BCMA NAND controller"
44 depends on BCMA_NFLASH
45 --- a/drivers/mtd/nand/raw/brcmnand/Makefile
46 +++ b/drivers/mtd/nand/raw/brcmnand/Makefile
47 @@ -6,3 +6,5 @@ obj-$(CONFIG_MTD_NAND_BRCMNAND) += bcm6
48 obj-$(CONFIG_MTD_NAND_BRCMNAND) += bcm6368_nand.o
49 obj-$(CONFIG_MTD_NAND_BRCMNAND) += brcmstb_nand.o
50 obj-$(CONFIG_MTD_NAND_BRCMNAND) += brcmnand.o
51 +
52 +obj-$(CONFIG_MTD_NAND_BRCMNAND_BCMA) += bcma_nand.o
53 --- /dev/null
54 +++ b/drivers/mtd/nand/raw/brcmnand/bcma_nand.c
55 @@ -0,0 +1,132 @@
56 +// SPDX-License-Identifier: GPL-2.0-only
57 +/*
58 + * Copyright © 2021 Broadcom
59 + */
60 +#include <linux/bcma/bcma.h>
61 +#include <linux/bcma/bcma_driver_chipcommon.h>
62 +#include <linux/device.h>
63 +#include <linux/module.h>
64 +#include <linux/platform_device.h>
65 +
66 +#include "brcmnand.h"
67 +
68 +struct brcmnand_bcma_soc {
69 + struct brcmnand_soc soc;
70 + struct bcma_drv_cc *cc;
71 +};
72 +
73 +static inline bool brcmnand_bcma_needs_swapping(u32 offset)
74 +{
75 + switch (offset) {
76 + case BCMA_CC_NAND_SPARE_RD0:
77 + case BCMA_CC_NAND_SPARE_RD4:
78 + case BCMA_CC_NAND_SPARE_RD8:
79 + case BCMA_CC_NAND_SPARE_RD12:
80 + case BCMA_CC_NAND_SPARE_WR0:
81 + case BCMA_CC_NAND_SPARE_WR4:
82 + case BCMA_CC_NAND_SPARE_WR8:
83 + case BCMA_CC_NAND_SPARE_WR12:
84 + case BCMA_CC_NAND_DEVID:
85 + case BCMA_CC_NAND_DEVID_X:
86 + case BCMA_CC_NAND_SPARE_RD16:
87 + case BCMA_CC_NAND_SPARE_RD20:
88 + case BCMA_CC_NAND_SPARE_RD24:
89 + case BCMA_CC_NAND_SPARE_RD28:
90 + return true;
91 + }
92 +
93 + return false;
94 +}
95 +
96 +static inline struct brcmnand_bcma_soc *to_bcma_soc(struct brcmnand_soc *soc)
97 +{
98 + return container_of(soc, struct brcmnand_bcma_soc, soc);
99 +}
100 +
101 +static u32 brcmnand_bcma_read_reg(struct brcmnand_soc *soc, u32 offset)
102 +{
103 + struct brcmnand_bcma_soc *sc = to_bcma_soc(soc);
104 + u32 val;
105 +
106 + /* Offset into the NAND block and deal with the flash cache separately */
107 + if (offset == BRCMNAND_NON_MMIO_FC_ADDR)
108 + offset = BCMA_CC_NAND_CACHE_DATA;
109 + else
110 + offset += BCMA_CC_NAND_REVISION;
111 +
112 + val = bcma_cc_read32(sc->cc, offset);
113 +
114 + /* Swap if necessary */
115 + if (brcmnand_bcma_needs_swapping(offset))
116 + val = be32_to_cpu(val);
117 + return val;
118 +}
119 +
120 +static void brcmnand_bcma_write_reg(struct brcmnand_soc *soc, u32 val,
121 + u32 offset)
122 +{
123 + struct brcmnand_bcma_soc *sc = to_bcma_soc(soc);
124 +
125 + /* Offset into the NAND block */
126 + if (offset == BRCMNAND_NON_MMIO_FC_ADDR)
127 + offset = BCMA_CC_NAND_CACHE_DATA;
128 + else
129 + offset += BCMA_CC_NAND_REVISION;
130 +
131 + /* Swap if necessary */
132 + if (brcmnand_bcma_needs_swapping(offset))
133 + val = cpu_to_be32(val);
134 +
135 + bcma_cc_write32(sc->cc, offset, val);
136 +}
137 +
138 +static struct brcmnand_io_ops brcmnand_bcma_io_ops = {
139 + .read_reg = brcmnand_bcma_read_reg,
140 + .write_reg = brcmnand_bcma_write_reg,
141 +};
142 +
143 +static void brcmnand_bcma_prepare_data_bus(struct brcmnand_soc *soc, bool prepare,
144 + bool is_param)
145 +{
146 + struct brcmnand_bcma_soc *sc = to_bcma_soc(soc);
147 +
148 + /* Reset the cache address to ensure we are already accessing the
149 + * beginning of a sub-page.
150 + */
151 + bcma_cc_write32(sc->cc, BCMA_CC_NAND_CACHE_ADDR, 0);
152 +}
153 +
154 +static int brcmnand_bcma_nand_probe(struct platform_device *pdev)
155 +{
156 + struct bcma_nflash *nflash = dev_get_platdata(&pdev->dev);
157 + struct brcmnand_bcma_soc *soc;
158 +
159 + soc = devm_kzalloc(&pdev->dev, sizeof(*soc), GFP_KERNEL);
160 + if (!soc)
161 + return -ENOMEM;
162 +
163 + soc->cc = container_of(nflash, struct bcma_drv_cc, nflash);
164 + soc->soc.prepare_data_bus = brcmnand_bcma_prepare_data_bus;
165 + soc->soc.ops = &brcmnand_bcma_io_ops;
166 +
167 + if (soc->cc->core->bus->chipinfo.id == BCMA_CHIP_ID_BCM4706) {
168 + dev_err(&pdev->dev, "Use bcm47xxnflash for 4706!\n");
169 + return -ENODEV;
170 + }
171 +
172 + return brcmnand_probe(pdev, &soc->soc);
173 +}
174 +
175 +static struct platform_driver brcmnand_bcma_nand_driver = {
176 + .probe = brcmnand_bcma_nand_probe,
177 + .remove = brcmnand_remove,
178 + .driver = {
179 + .name = "bcma_brcmnand",
180 + .pm = &brcmnand_pm_ops,
181 + }
182 +};
183 +module_platform_driver(brcmnand_bcma_nand_driver);
184 +
185 +MODULE_LICENSE("GPL v2");
186 +MODULE_AUTHOR("Broadcom");
187 +MODULE_DESCRIPTION("NAND controller driver glue for BCMA chips");
188 --- a/drivers/mtd/nand/raw/brcmnand/brcmnand.c
189 +++ b/drivers/mtd/nand/raw/brcmnand/brcmnand.c
190 @@ -595,7 +595,11 @@ enum {
191
192 static inline bool brcmnand_non_mmio_ops(struct brcmnand_controller *ctrl)
193 {
194 +#if IS_ENABLED(CONFIG_MTD_NAND_BRCMNAND_BCMA)
195 return static_branch_unlikely(&brcmnand_soc_has_ops_key);
196 +#else
197 + return false;
198 +#endif
199 }
200
201 static inline u32 nand_readreg(struct brcmnand_controller *ctrl, u32 offs)