Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Renesas RCar Gen3 RPC QSPI driver |
| 4 | * |
| 5 | * Copyright (C) 2018 Marek Vasut <marek.vasut@gmail.com> |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 8 | #include <asm/global_data.h> |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 9 | #include <asm/io.h> |
| 10 | #include <clk.h> |
| 11 | #include <dm.h> |
| 12 | #include <dm/of_access.h> |
| 13 | #include <dt-structs.h> |
| 14 | #include <errno.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 15 | #include <linux/bitops.h> |
Simon Glass | c06c1be | 2020-05-10 11:40:08 -0600 | [diff] [blame] | 16 | #include <linux/bug.h> |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 17 | #include <linux/errno.h> |
| 18 | #include <spi.h> |
Cong Dang | ce33563 | 2022-08-25 06:06:54 +0700 | [diff] [blame] | 19 | #include <spi-mem.h> |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 20 | #include <wait_bit.h> |
| 21 | |
| 22 | #define RPC_CMNCR 0x0000 /* R/W */ |
| 23 | #define RPC_CMNCR_MD BIT(31) |
| 24 | #define RPC_CMNCR_SFDE BIT(24) |
| 25 | #define RPC_CMNCR_MOIIO3(val) (((val) & 0x3) << 22) |
| 26 | #define RPC_CMNCR_MOIIO2(val) (((val) & 0x3) << 20) |
| 27 | #define RPC_CMNCR_MOIIO1(val) (((val) & 0x3) << 18) |
| 28 | #define RPC_CMNCR_MOIIO0(val) (((val) & 0x3) << 16) |
| 29 | #define RPC_CMNCR_MOIIO_HIZ (RPC_CMNCR_MOIIO0(3) | RPC_CMNCR_MOIIO1(3) | \ |
| 30 | RPC_CMNCR_MOIIO2(3) | RPC_CMNCR_MOIIO3(3)) |
| 31 | #define RPC_CMNCR_IO3FV(val) (((val) & 0x3) << 14) |
| 32 | #define RPC_CMNCR_IO2FV(val) (((val) & 0x3) << 12) |
| 33 | #define RPC_CMNCR_IO0FV(val) (((val) & 0x3) << 8) |
| 34 | #define RPC_CMNCR_IOFV_HIZ (RPC_CMNCR_IO0FV(3) | RPC_CMNCR_IO2FV(3) | \ |
| 35 | RPC_CMNCR_IO3FV(3)) |
| 36 | #define RPC_CMNCR_CPHAT BIT(6) |
| 37 | #define RPC_CMNCR_CPHAR BIT(5) |
| 38 | #define RPC_CMNCR_SSLP BIT(4) |
| 39 | #define RPC_CMNCR_CPOL BIT(3) |
| 40 | #define RPC_CMNCR_BSZ(val) (((val) & 0x3) << 0) |
| 41 | |
| 42 | #define RPC_SSLDR 0x0004 /* R/W */ |
| 43 | #define RPC_SSLDR_SPNDL(d) (((d) & 0x7) << 16) |
| 44 | #define RPC_SSLDR_SLNDL(d) (((d) & 0x7) << 8) |
| 45 | #define RPC_SSLDR_SCKDL(d) (((d) & 0x7) << 0) |
| 46 | |
| 47 | #define RPC_DRCR 0x000C /* R/W */ |
| 48 | #define RPC_DRCR_SSLN BIT(24) |
| 49 | #define RPC_DRCR_RBURST(v) (((v) & 0x1F) << 16) |
| 50 | #define RPC_DRCR_RCF BIT(9) |
| 51 | #define RPC_DRCR_RBE BIT(8) |
| 52 | #define RPC_DRCR_SSLE BIT(0) |
| 53 | |
| 54 | #define RPC_DRCMR 0x0010 /* R/W */ |
| 55 | #define RPC_DRCMR_CMD(c) (((c) & 0xFF) << 16) |
| 56 | #define RPC_DRCMR_OCMD(c) (((c) & 0xFF) << 0) |
| 57 | |
| 58 | #define RPC_DREAR 0x0014 /* R/W */ |
| 59 | #define RPC_DREAR_EAV(v) (((v) & 0xFF) << 16) |
| 60 | #define RPC_DREAR_EAC(v) (((v) & 0x7) << 0) |
| 61 | |
| 62 | #define RPC_DROPR 0x0018 /* R/W */ |
| 63 | #define RPC_DROPR_OPD3(o) (((o) & 0xFF) << 24) |
| 64 | #define RPC_DROPR_OPD2(o) (((o) & 0xFF) << 16) |
| 65 | #define RPC_DROPR_OPD1(o) (((o) & 0xFF) << 8) |
| 66 | #define RPC_DROPR_OPD0(o) (((o) & 0xFF) << 0) |
| 67 | |
| 68 | #define RPC_DRENR 0x001C /* R/W */ |
| 69 | #define RPC_DRENR_CDB(o) (u32)((((o) & 0x3) << 30)) |
| 70 | #define RPC_DRENR_OCDB(o) (((o) & 0x3) << 28) |
| 71 | #define RPC_DRENR_ADB(o) (((o) & 0x3) << 24) |
| 72 | #define RPC_DRENR_OPDB(o) (((o) & 0x3) << 20) |
| 73 | #define RPC_DRENR_SPIDB(o) (((o) & 0x3) << 16) |
| 74 | #define RPC_DRENR_DME BIT(15) |
| 75 | #define RPC_DRENR_CDE BIT(14) |
| 76 | #define RPC_DRENR_OCDE BIT(12) |
| 77 | #define RPC_DRENR_ADE(v) (((v) & 0xF) << 8) |
| 78 | #define RPC_DRENR_OPDE(v) (((v) & 0xF) << 4) |
| 79 | |
| 80 | #define RPC_SMCR 0x0020 /* R/W */ |
| 81 | #define RPC_SMCR_SSLKP BIT(8) |
| 82 | #define RPC_SMCR_SPIRE BIT(2) |
| 83 | #define RPC_SMCR_SPIWE BIT(1) |
| 84 | #define RPC_SMCR_SPIE BIT(0) |
| 85 | |
| 86 | #define RPC_SMCMR 0x0024 /* R/W */ |
| 87 | #define RPC_SMCMR_CMD(c) (((c) & 0xFF) << 16) |
| 88 | #define RPC_SMCMR_OCMD(c) (((c) & 0xFF) << 0) |
| 89 | |
| 90 | #define RPC_SMADR 0x0028 /* R/W */ |
| 91 | #define RPC_SMOPR 0x002C /* R/W */ |
| 92 | #define RPC_SMOPR_OPD0(o) (((o) & 0xFF) << 0) |
| 93 | #define RPC_SMOPR_OPD1(o) (((o) & 0xFF) << 8) |
| 94 | #define RPC_SMOPR_OPD2(o) (((o) & 0xFF) << 16) |
| 95 | #define RPC_SMOPR_OPD3(o) (((o) & 0xFF) << 24) |
| 96 | |
| 97 | #define RPC_SMENR 0x0030 /* R/W */ |
| 98 | #define RPC_SMENR_CDB(o) (((o) & 0x3) << 30) |
| 99 | #define RPC_SMENR_OCDB(o) (((o) & 0x3) << 28) |
| 100 | #define RPC_SMENR_ADB(o) (((o) & 0x3) << 24) |
| 101 | #define RPC_SMENR_OPDB(o) (((o) & 0x3) << 20) |
| 102 | #define RPC_SMENR_SPIDB(o) (((o) & 0x3) << 16) |
| 103 | #define RPC_SMENR_DME BIT(15) |
| 104 | #define RPC_SMENR_CDE BIT(14) |
| 105 | #define RPC_SMENR_OCDE BIT(12) |
| 106 | #define RPC_SMENR_ADE(v) (((v) & 0xF) << 8) |
| 107 | #define RPC_SMENR_OPDE(v) (((v) & 0xF) << 4) |
| 108 | #define RPC_SMENR_SPIDE(v) (((v) & 0xF) << 0) |
| 109 | |
| 110 | #define RPC_SMRDR0 0x0038 /* R */ |
| 111 | #define RPC_SMRDR1 0x003C /* R */ |
| 112 | #define RPC_SMWDR0 0x0040 /* R/W */ |
| 113 | #define RPC_SMWDR1 0x0044 /* R/W */ |
| 114 | #define RPC_CMNSR 0x0048 /* R */ |
| 115 | #define RPC_CMNSR_SSLF BIT(1) |
| 116 | #define RPC_CMNSR_TEND BIT(0) |
| 117 | |
| 118 | #define RPC_DRDMCR 0x0058 /* R/W */ |
| 119 | #define RPC_DRDMCR_DMCYC(v) (((v) & 0xF) << 0) |
| 120 | |
| 121 | #define RPC_DRDRENR 0x005C /* R/W */ |
| 122 | #define RPC_DRDRENR_HYPE (0x5 << 12) |
| 123 | #define RPC_DRDRENR_ADDRE BIT(8) |
| 124 | #define RPC_DRDRENR_OPDRE BIT(4) |
| 125 | #define RPC_DRDRENR_DRDRE BIT(0) |
| 126 | |
| 127 | #define RPC_SMDMCR 0x0060 /* R/W */ |
| 128 | #define RPC_SMDMCR_DMCYC(v) (((v) & 0xF) << 0) |
| 129 | |
| 130 | #define RPC_SMDRENR 0x0064 /* R/W */ |
| 131 | #define RPC_SMDRENR_HYPE (0x5 << 12) |
| 132 | #define RPC_SMDRENR_ADDRE BIT(8) |
| 133 | #define RPC_SMDRENR_OPDRE BIT(4) |
| 134 | #define RPC_SMDRENR_SPIDRE BIT(0) |
| 135 | |
| 136 | #define RPC_PHYCNT 0x007C /* R/W */ |
| 137 | #define RPC_PHYCNT_CAL BIT(31) |
| 138 | #define PRC_PHYCNT_OCTA_AA BIT(22) |
| 139 | #define PRC_PHYCNT_OCTA_SA BIT(23) |
| 140 | #define PRC_PHYCNT_EXDS BIT(21) |
| 141 | #define RPC_PHYCNT_OCT BIT(20) |
| 142 | #define RPC_PHYCNT_STRTIM(v) (((v) & 0x7) << 15) |
Hai Pham | 581d253 | 2021-08-05 14:38:26 +0700 | [diff] [blame] | 143 | #define RPC_PHYCNT_STRTIM2(v) ((((v) & 0x7) << 15) | (((v) & 0x8) << 24)) |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 144 | #define RPC_PHYCNT_WBUF2 BIT(4) |
| 145 | #define RPC_PHYCNT_WBUF BIT(2) |
| 146 | #define RPC_PHYCNT_MEM(v) (((v) & 0x3) << 0) |
| 147 | |
Marek Vasut | 9e27479 | 2024-08-31 22:31:47 +0200 | [diff] [blame] | 148 | #define RPCIF_PHYOFFSET1 0x0080 /* R/W */ |
| 149 | #define RPCIF_PHYOFFSET1_DDRTMG(v) (((v) & 0x3) << 28) |
| 150 | |
| 151 | #define RPCIF_PHYOFFSET2 0x0084 /* R/W */ |
| 152 | #define RPCIF_PHYOFFSET2_OCTTMG(v) (((v) & 0x7) << 8) |
| 153 | |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 154 | #define RPC_PHYINT 0x0088 /* R/W */ |
| 155 | #define RPC_PHYINT_RSTEN BIT(18) |
| 156 | #define RPC_PHYINT_WPEN BIT(17) |
| 157 | #define RPC_PHYINT_INTEN BIT(16) |
| 158 | #define RPC_PHYINT_RST BIT(2) |
| 159 | #define RPC_PHYINT_WP BIT(1) |
| 160 | #define RPC_PHYINT_INT BIT(0) |
| 161 | |
| 162 | #define RPC_WBUF 0x8000 /* R/W size=4/8/16/32/64Bytes */ |
| 163 | #define RPC_WBUF_SIZE 0x100 |
| 164 | |
| 165 | DECLARE_GLOBAL_DATA_PTR; |
| 166 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 167 | struct rpc_spi_plat { |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 168 | fdt_addr_t regs; |
| 169 | fdt_addr_t extr; |
| 170 | s32 freq; /* Default clock freq, -1 for none */ |
| 171 | }; |
| 172 | |
| 173 | struct rpc_spi_priv { |
| 174 | fdt_addr_t regs; |
| 175 | fdt_addr_t extr; |
| 176 | struct clk clk; |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 177 | }; |
| 178 | |
| 179 | static int rpc_spi_wait_sslf(struct udevice *dev) |
| 180 | { |
| 181 | struct rpc_spi_priv *priv = dev_get_priv(dev->parent); |
| 182 | |
| 183 | return wait_for_bit_le32((void *)priv->regs + RPC_CMNSR, RPC_CMNSR_SSLF, |
| 184 | false, 1000, false); |
| 185 | } |
| 186 | |
| 187 | static int rpc_spi_wait_tend(struct udevice *dev) |
| 188 | { |
| 189 | struct rpc_spi_priv *priv = dev_get_priv(dev->parent); |
| 190 | |
| 191 | return wait_for_bit_le32((void *)priv->regs + RPC_CMNSR, RPC_CMNSR_TEND, |
| 192 | true, 1000, false); |
| 193 | } |
| 194 | |
| 195 | static void rpc_spi_flush_read_cache(struct udevice *dev) |
| 196 | { |
| 197 | struct udevice *bus = dev->parent; |
| 198 | struct rpc_spi_priv *priv = dev_get_priv(bus); |
| 199 | |
| 200 | /* Flush read cache */ |
| 201 | writel(RPC_DRCR_SSLN | RPC_DRCR_RBURST(0x1f) | |
| 202 | RPC_DRCR_RCF | RPC_DRCR_RBE | RPC_DRCR_SSLE, |
| 203 | priv->regs + RPC_DRCR); |
| 204 | readl(priv->regs + RPC_DRCR); |
| 205 | |
| 206 | } |
| 207 | |
Hai Pham | 286ed07 | 2021-08-16 09:26:36 +0700 | [diff] [blame] | 208 | static u32 rpc_spi_get_strobe_delay(void) |
| 209 | { |
| 210 | #ifndef CONFIG_RZA1 |
Marek Vasut | 30fe98e | 2024-02-27 17:05:45 +0100 | [diff] [blame] | 211 | u32 cpu_type = renesas_get_cpu_type(); |
Hai Pham | 286ed07 | 2021-08-16 09:26:36 +0700 | [diff] [blame] | 212 | |
| 213 | /* |
| 214 | * NOTE: RPC_PHYCNT_STRTIM value: |
| 215 | * 0: On H3 ES1.x (not supported in mainline U-Boot) |
| 216 | * 6: On M3 ES1.x |
| 217 | * 7: On other R-Car Gen3 |
Hai Pham | 581d253 | 2021-08-05 14:38:26 +0700 | [diff] [blame] | 218 | * 15: On R-Car Gen4 |
Hai Pham | 286ed07 | 2021-08-16 09:26:36 +0700 | [diff] [blame] | 219 | */ |
Marek Vasut | f972661 | 2024-02-27 17:05:47 +0100 | [diff] [blame] | 220 | if (cpu_type == RENESAS_CPU_TYPE_R8A7796 && renesas_get_cpu_rev_integer() == 1) |
Hai Pham | 286ed07 | 2021-08-16 09:26:36 +0700 | [diff] [blame] | 221 | return RPC_PHYCNT_STRTIM(6); |
Marek Vasut | f972661 | 2024-02-27 17:05:47 +0100 | [diff] [blame] | 222 | else if (cpu_type == RENESAS_CPU_TYPE_R8A779F0 || |
| 223 | cpu_type == RENESAS_CPU_TYPE_R8A779G0 || |
| 224 | cpu_type == RENESAS_CPU_TYPE_R8A779H0) |
Hai Pham | 581d253 | 2021-08-05 14:38:26 +0700 | [diff] [blame] | 225 | return RPC_PHYCNT_STRTIM2(15); |
Hai Pham | 286ed07 | 2021-08-16 09:26:36 +0700 | [diff] [blame] | 226 | else |
| 227 | #endif |
| 228 | return RPC_PHYCNT_STRTIM(7); |
| 229 | } |
| 230 | |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 231 | static int rpc_spi_claim_bus(struct udevice *dev, bool manual) |
| 232 | { |
| 233 | struct udevice *bus = dev->parent; |
| 234 | struct rpc_spi_priv *priv = dev_get_priv(bus); |
| 235 | |
Marek Vasut | 9e27479 | 2024-08-31 22:31:47 +0200 | [diff] [blame] | 236 | setbits_le32(priv->regs + RPCIF_PHYOFFSET1, |
| 237 | RPCIF_PHYOFFSET1_DDRTMG(3)); |
| 238 | clrsetbits_le32(priv->regs + RPCIF_PHYOFFSET2, |
| 239 | RPCIF_PHYOFFSET2_OCTTMG(7), |
| 240 | RPCIF_PHYOFFSET2_OCTTMG(4)); |
| 241 | |
Hai Pham | 286ed07 | 2021-08-16 09:26:36 +0700 | [diff] [blame] | 242 | /* NOTE: The 0x260 are undocumented bits, but they must be set. */ |
| 243 | writel(RPC_PHYCNT_CAL | rpc_spi_get_strobe_delay() | 0x260, |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 244 | priv->regs + RPC_PHYCNT); |
| 245 | writel((manual ? RPC_CMNCR_MD : 0) | RPC_CMNCR_SFDE | |
| 246 | RPC_CMNCR_MOIIO_HIZ | RPC_CMNCR_IOFV_HIZ | RPC_CMNCR_BSZ(0), |
| 247 | priv->regs + RPC_CMNCR); |
| 248 | |
| 249 | writel(RPC_SSLDR_SPNDL(7) | RPC_SSLDR_SLNDL(7) | |
| 250 | RPC_SSLDR_SCKDL(7), priv->regs + RPC_SSLDR); |
| 251 | |
| 252 | rpc_spi_flush_read_cache(dev); |
| 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | static int rpc_spi_release_bus(struct udevice *dev) |
| 258 | { |
| 259 | struct udevice *bus = dev->parent; |
| 260 | struct rpc_spi_priv *priv = dev_get_priv(bus); |
| 261 | |
| 262 | /* NOTE: The 0x260 are undocumented bits, but they must be set. */ |
Hai Pham | 286ed07 | 2021-08-16 09:26:36 +0700 | [diff] [blame] | 263 | writel(rpc_spi_get_strobe_delay() | 0x260, priv->regs + RPC_PHYCNT); |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 264 | |
| 265 | rpc_spi_flush_read_cache(dev); |
| 266 | |
| 267 | return 0; |
| 268 | } |
| 269 | |
Cong Dang | ce33563 | 2022-08-25 06:06:54 +0700 | [diff] [blame] | 270 | static int rpc_spi_mem_exec_op(struct spi_slave *spi, |
| 271 | const struct spi_mem_op *op) |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 272 | { |
Cong Dang | ce33563 | 2022-08-25 06:06:54 +0700 | [diff] [blame] | 273 | struct udevice *bus = spi->dev->parent; |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 274 | struct rpc_spi_priv *priv = dev_get_priv(bus); |
Cong Dang | ce33563 | 2022-08-25 06:06:54 +0700 | [diff] [blame] | 275 | const void *dout = op->data.buf.out ? op->data.buf.out : NULL; |
| 276 | void *din = op->data.buf.in ? op->data.buf.in : NULL; |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 277 | int ret = 0; |
Cong Dang | ce33563 | 2022-08-25 06:06:54 +0700 | [diff] [blame] | 278 | u32 offset = 0; |
| 279 | u32 smenr, smcr; |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 280 | |
Cong Dang | ce33563 | 2022-08-25 06:06:54 +0700 | [diff] [blame] | 281 | smenr = 0; |
| 282 | offset = op->addr.val; |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 283 | |
Cong Dang | ce33563 | 2022-08-25 06:06:54 +0700 | [diff] [blame] | 284 | switch (op->data.dir) { |
| 285 | case SPI_MEM_DATA_IN: |
| 286 | rpc_spi_claim_bus(spi->dev, false); |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 287 | |
Cong Dang | ce33563 | 2022-08-25 06:06:54 +0700 | [diff] [blame] | 288 | writel(0, priv->regs + RPC_DRCMR); |
| 289 | writel(RPC_DRCMR_CMD(op->cmd.opcode), priv->regs + RPC_DRCMR); |
| 290 | smenr |= RPC_DRENR_CDE; |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 291 | |
Cong Dang | ce33563 | 2022-08-25 06:06:54 +0700 | [diff] [blame] | 292 | if (op->addr.nbytes == 4) { |
| 293 | writel(RPC_DREAR_EAV(offset >> 25) | RPC_DREAR_EAC(1), |
| 294 | priv->regs + RPC_DREAR); |
| 295 | smenr |= RPC_DRENR_ADE(0xF); |
| 296 | } else if (op->addr.nbytes == 3) { |
Marek Vasut | be61015 | 2024-08-31 22:31:44 +0200 | [diff] [blame] | 297 | writel(0, priv->regs + RPC_DREAR); |
Cong Dang | ce33563 | 2022-08-25 06:06:54 +0700 | [diff] [blame] | 298 | smenr |= RPC_DRENR_ADE(0x7); |
| 299 | } else { |
Marek Vasut | be61015 | 2024-08-31 22:31:44 +0200 | [diff] [blame] | 300 | writel(0, priv->regs + RPC_DREAR); |
Cong Dang | ce33563 | 2022-08-25 06:06:54 +0700 | [diff] [blame] | 301 | smenr |= RPC_DRENR_ADE(0); |
| 302 | } |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 303 | |
Marek Vasut | 4d799b6 | 2024-08-31 22:31:45 +0200 | [diff] [blame] | 304 | if (op->dummy.nbytes) |
Cong Dang | ce33563 | 2022-08-25 06:06:54 +0700 | [diff] [blame] | 305 | smenr |= RPC_DRENR_DME; |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 306 | |
Marek Vasut | 4d799b6 | 2024-08-31 22:31:45 +0200 | [diff] [blame] | 307 | writel(8 * op->dummy.nbytes - 1, priv->regs + RPC_DRDMCR); |
Cong Dang | ce33563 | 2022-08-25 06:06:54 +0700 | [diff] [blame] | 308 | writel(0, priv->regs + RPC_DROPR); |
Marek Vasut | fec46b9 | 2024-08-31 22:31:46 +0200 | [diff] [blame] | 309 | writel(0, priv->regs + RPC_DRDRENR); |
Cong Dang | ce33563 | 2022-08-25 06:06:54 +0700 | [diff] [blame] | 310 | writel(smenr, priv->regs + RPC_DRENR); |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 311 | |
Cong Dang | ce33563 | 2022-08-25 06:06:54 +0700 | [diff] [blame] | 312 | memcpy_fromio(din, (void *)(priv->extr + offset), op->data.nbytes); |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 313 | |
Cong Dang | ce33563 | 2022-08-25 06:06:54 +0700 | [diff] [blame] | 314 | rpc_spi_release_bus(spi->dev); |
| 315 | break; |
| 316 | case SPI_MEM_DATA_OUT: |
| 317 | case SPI_MEM_NO_DATA: |
| 318 | rpc_spi_claim_bus(spi->dev, true); |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 319 | |
Cong Dang | ce33563 | 2022-08-25 06:06:54 +0700 | [diff] [blame] | 320 | writel(0, priv->regs + RPC_SMCR); |
| 321 | writel(0, priv->regs + RPC_SMCMR); |
| 322 | writel(RPC_SMCMR_CMD(op->cmd.opcode), priv->regs + RPC_SMCMR); |
| 323 | smenr |= RPC_SMENR_CDE; |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 324 | |
Cong Dang | ce33563 | 2022-08-25 06:06:54 +0700 | [diff] [blame] | 325 | writel(0, priv->regs + RPC_SMADR); |
| 326 | if (op->addr.nbytes == 4) |
| 327 | smenr |= RPC_SMENR_ADE(0xF); |
| 328 | else if (op->addr.nbytes == 3) |
| 329 | smenr |= RPC_SMENR_ADE(0x7); |
| 330 | else |
| 331 | smenr |= RPC_SMENR_ADE(0); |
| 332 | writel(offset, priv->regs + RPC_SMADR); |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 333 | |
Cong Dang | ce33563 | 2022-08-25 06:06:54 +0700 | [diff] [blame] | 334 | writel(0, priv->regs + RPC_SMDMCR); |
| 335 | if (op->dummy.nbytes) { |
| 336 | writel(8 * op->dummy.nbytes - 1, priv->regs + RPC_SMDMCR); |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 337 | smenr |= RPC_SMENR_DME; |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | writel(0, priv->regs + RPC_SMOPR); |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 341 | writel(0, priv->regs + RPC_SMDRENR); |
| 342 | |
Cong Dang | ce33563 | 2022-08-25 06:06:54 +0700 | [diff] [blame] | 343 | if (dout && op->data.nbytes) { |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 344 | u32 *datout = (u32 *)dout; |
Cong Dang | ce33563 | 2022-08-25 06:06:54 +0700 | [diff] [blame] | 345 | u32 wloop = DIV_ROUND_UP(op->data.nbytes, 4); |
| 346 | |
| 347 | smenr |= RPC_SMENR_SPIDE(0xF); |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 348 | |
| 349 | while (wloop--) { |
| 350 | smcr = RPC_SMCR_SPIWE | RPC_SMCR_SPIE; |
| 351 | if (wloop >= 1) |
| 352 | smcr |= RPC_SMCR_SSLKP; |
| 353 | writel(smenr, priv->regs + RPC_SMENR); |
| 354 | writel(*datout, priv->regs + RPC_SMWDR0); |
| 355 | writel(smcr, priv->regs + RPC_SMCR); |
Cong Dang | ce33563 | 2022-08-25 06:06:54 +0700 | [diff] [blame] | 356 | ret = rpc_spi_wait_tend(spi->dev); |
| 357 | if (ret) { |
| 358 | rpc_spi_release_bus(spi->dev); |
| 359 | return ret; |
| 360 | } |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 361 | datout++; |
Cong Dang | ce33563 | 2022-08-25 06:06:54 +0700 | [diff] [blame] | 362 | smenr &= (~RPC_SMENR_CDE & ~RPC_SMENR_ADE(0xF)); |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 363 | } |
| 364 | |
Cong Dang | ce33563 | 2022-08-25 06:06:54 +0700 | [diff] [blame] | 365 | ret = rpc_spi_wait_sslf(spi->dev); |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 366 | } else { |
| 367 | writel(smenr, priv->regs + RPC_SMENR); |
| 368 | writel(RPC_SMCR_SPIE, priv->regs + RPC_SMCR); |
Cong Dang | ce33563 | 2022-08-25 06:06:54 +0700 | [diff] [blame] | 369 | ret = rpc_spi_wait_tend(spi->dev); |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 370 | } |
| 371 | |
Cong Dang | ce33563 | 2022-08-25 06:06:54 +0700 | [diff] [blame] | 372 | rpc_spi_release_bus(spi->dev); |
| 373 | break; |
| 374 | default: |
| 375 | break; |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 376 | } |
| 377 | |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 378 | return ret; |
| 379 | } |
| 380 | |
| 381 | static int rpc_spi_set_speed(struct udevice *bus, uint speed) |
| 382 | { |
| 383 | /* This is a SPI NOR controller, do nothing. */ |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | static int rpc_spi_set_mode(struct udevice *bus, uint mode) |
| 388 | { |
| 389 | /* This is a SPI NOR controller, do nothing. */ |
| 390 | return 0; |
| 391 | } |
| 392 | |
Cong Dang | ce33563 | 2022-08-25 06:06:54 +0700 | [diff] [blame] | 393 | static const struct spi_controller_mem_ops rpc_spi_mem_ops = { |
| 394 | .exec_op = rpc_spi_mem_exec_op |
| 395 | }; |
| 396 | |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 397 | static int rpc_spi_bind(struct udevice *parent) |
| 398 | { |
| 399 | const void *fdt = gd->fdt_blob; |
| 400 | ofnode node; |
| 401 | int ret, off; |
| 402 | |
| 403 | /* |
| 404 | * Check if there are any SPI NOR child nodes, if so, bind as |
| 405 | * this controller will be operated in SPI mode. |
| 406 | */ |
| 407 | dev_for_each_subnode(node, parent) { |
| 408 | off = ofnode_to_offset(node); |
| 409 | |
| 410 | ret = fdt_node_check_compatible(fdt, off, "spi-flash"); |
| 411 | if (!ret) |
| 412 | return 0; |
| 413 | |
| 414 | ret = fdt_node_check_compatible(fdt, off, "jedec,spi-nor"); |
| 415 | if (!ret) |
| 416 | return 0; |
| 417 | } |
| 418 | |
| 419 | return -ENODEV; |
| 420 | } |
| 421 | |
| 422 | static int rpc_spi_probe(struct udevice *dev) |
| 423 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 424 | struct rpc_spi_plat *plat = dev_get_plat(dev); |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 425 | struct rpc_spi_priv *priv = dev_get_priv(dev); |
| 426 | |
| 427 | priv->regs = plat->regs; |
| 428 | priv->extr = plat->extr; |
Marek Vasut | f9db3b3 | 2019-05-04 18:52:33 +0200 | [diff] [blame] | 429 | #if CONFIG_IS_ENABLED(CLK) |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 430 | clk_enable(&priv->clk); |
Marek Vasut | f9db3b3 | 2019-05-04 18:52:33 +0200 | [diff] [blame] | 431 | #endif |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 432 | return 0; |
| 433 | } |
| 434 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 435 | static int rpc_spi_of_to_plat(struct udevice *bus) |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 436 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 437 | struct rpc_spi_plat *plat = dev_get_plat(bus); |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 438 | |
| 439 | plat->regs = dev_read_addr_index(bus, 0); |
| 440 | plat->extr = dev_read_addr_index(bus, 1); |
| 441 | |
Marek Vasut | f9db3b3 | 2019-05-04 18:52:33 +0200 | [diff] [blame] | 442 | #if CONFIG_IS_ENABLED(CLK) |
| 443 | struct rpc_spi_priv *priv = dev_get_priv(bus); |
| 444 | int ret; |
| 445 | |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 446 | ret = clk_get_by_index(bus, 0, &priv->clk); |
| 447 | if (ret < 0) { |
| 448 | printf("%s: Could not get clock for %s: %d\n", |
| 449 | __func__, bus->name, ret); |
| 450 | return ret; |
| 451 | } |
Marek Vasut | f9db3b3 | 2019-05-04 18:52:33 +0200 | [diff] [blame] | 452 | #endif |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 453 | |
| 454 | plat->freq = dev_read_u32_default(bus, "spi-max-freq", 50000000); |
| 455 | |
| 456 | return 0; |
| 457 | } |
| 458 | |
| 459 | static const struct dm_spi_ops rpc_spi_ops = { |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 460 | .set_speed = rpc_spi_set_speed, |
| 461 | .set_mode = rpc_spi_set_mode, |
Cong Dang | ce33563 | 2022-08-25 06:06:54 +0700 | [diff] [blame] | 462 | .mem_ops = &rpc_spi_mem_ops |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 463 | }; |
| 464 | |
| 465 | static const struct udevice_id rpc_spi_ids[] = { |
Geert Uytterhoeven | 431ded6 | 2022-03-29 14:19:09 +0200 | [diff] [blame] | 466 | { .compatible = "renesas,r7s72100-rpc-if" }, |
| 467 | { .compatible = "renesas,rcar-gen3-rpc-if" }, |
Marek Vasut | db7f76d | 2024-08-31 22:31:48 +0200 | [diff] [blame] | 468 | { .compatible = "renesas,rcar-gen4-rpc-if" }, |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 469 | { } |
| 470 | }; |
| 471 | |
| 472 | U_BOOT_DRIVER(rpc_spi) = { |
| 473 | .name = "rpc_spi", |
| 474 | .id = UCLASS_SPI, |
| 475 | .of_match = rpc_spi_ids, |
| 476 | .ops = &rpc_spi_ops, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 477 | .of_to_plat = rpc_spi_of_to_plat, |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 478 | .plat_auto = sizeof(struct rpc_spi_plat), |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 479 | .priv_auto = sizeof(struct rpc_spi_priv), |
Marek Vasut | 6ca967b | 2017-11-29 06:29:46 +0100 | [diff] [blame] | 480 | .bind = rpc_spi_bind, |
| 481 | .probe = rpc_spi_probe, |
| 482 | }; |