Weijie Gao | b34a236 | 2022-09-09 19:59:45 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright (C) 2022 MediaTek Inc. All Rights Reserved. |
| 4 | * |
| 5 | * Author: SkyLake.Huang <skylake.huang@mediatek.com> |
| 6 | */ |
| 7 | |
| 8 | #include <clk.h> |
| 9 | #include <cpu_func.h> |
| 10 | #include <div64.h> |
| 11 | #include <dm.h> |
| 12 | #include <spi.h> |
| 13 | #include <spi-mem.h> |
| 14 | #include <stdbool.h> |
| 15 | #include <watchdog.h> |
| 16 | #include <dm/device.h> |
| 17 | #include <dm/device_compat.h> |
| 18 | #include <dm/devres.h> |
| 19 | #include <dm/pinctrl.h> |
| 20 | #include <linux/bitops.h> |
| 21 | #include <linux/completion.h> |
| 22 | #include <linux/dma-mapping.h> |
| 23 | #include <linux/io.h> |
| 24 | #include <linux/iopoll.h> |
Tom Rini | 15713fc | 2022-10-28 20:27:08 -0400 | [diff] [blame] | 25 | #include <linux/sizes.h> |
Weijie Gao | b34a236 | 2022-09-09 19:59:45 +0800 | [diff] [blame] | 26 | |
| 27 | #define SPI_CFG0_REG 0x0000 |
| 28 | #define SPI_CFG1_REG 0x0004 |
| 29 | #define SPI_TX_SRC_REG 0x0008 |
| 30 | #define SPI_RX_DST_REG 0x000c |
| 31 | #define SPI_TX_DATA_REG 0x0010 |
| 32 | #define SPI_RX_DATA_REG 0x0014 |
| 33 | #define SPI_CMD_REG 0x0018 |
| 34 | #define SPI_IRQ_REG 0x001c |
| 35 | #define SPI_STATUS_REG 0x0020 |
| 36 | #define SPI_PAD_SEL_REG 0x0024 |
| 37 | #define SPI_CFG2_REG 0x0028 |
| 38 | #define SPI_TX_SRC_REG_64 0x002c |
| 39 | #define SPI_RX_DST_REG_64 0x0030 |
| 40 | #define SPI_CFG3_IPM_REG 0x0040 |
| 41 | |
| 42 | #define SPI_CFG0_SCK_HIGH_OFFSET 0 |
| 43 | #define SPI_CFG0_SCK_LOW_OFFSET 8 |
| 44 | #define SPI_CFG0_CS_HOLD_OFFSET 16 |
| 45 | #define SPI_CFG0_CS_SETUP_OFFSET 24 |
| 46 | #define SPI_ADJUST_CFG0_CS_HOLD_OFFSET 0 |
| 47 | #define SPI_ADJUST_CFG0_CS_SETUP_OFFSET 16 |
| 48 | |
| 49 | #define SPI_CFG1_CS_IDLE_OFFSET 0 |
| 50 | #define SPI_CFG1_PACKET_LOOP_OFFSET 8 |
| 51 | #define SPI_CFG1_PACKET_LENGTH_OFFSET 16 |
| 52 | #define SPI_CFG1_GET_TICKDLY_OFFSET 29 |
| 53 | |
| 54 | #define SPI_CFG1_GET_TICKDLY_MASK GENMASK(31, 29) |
| 55 | #define SPI_CFG1_CS_IDLE_MASK 0xff |
| 56 | #define SPI_CFG1_PACKET_LOOP_MASK 0xff00 |
| 57 | #define SPI_CFG1_PACKET_LENGTH_MASK 0x3ff0000 |
| 58 | #define SPI_CFG1_IPM_PACKET_LENGTH_MASK GENMASK(31, 16) |
| 59 | #define SPI_CFG2_SCK_HIGH_OFFSET 0 |
| 60 | #define SPI_CFG2_SCK_LOW_OFFSET 16 |
| 61 | #define SPI_CFG2_SCK_HIGH_MASK GENMASK(15, 0) |
| 62 | #define SPI_CFG2_SCK_LOW_MASK GENMASK(31, 16) |
| 63 | |
| 64 | #define SPI_CMD_ACT BIT(0) |
| 65 | #define SPI_CMD_RESUME BIT(1) |
| 66 | #define SPI_CMD_RST BIT(2) |
| 67 | #define SPI_CMD_PAUSE_EN BIT(4) |
| 68 | #define SPI_CMD_DEASSERT BIT(5) |
| 69 | #define SPI_CMD_SAMPLE_SEL BIT(6) |
| 70 | #define SPI_CMD_CS_POL BIT(7) |
| 71 | #define SPI_CMD_CPHA BIT(8) |
| 72 | #define SPI_CMD_CPOL BIT(9) |
| 73 | #define SPI_CMD_RX_DMA BIT(10) |
| 74 | #define SPI_CMD_TX_DMA BIT(11) |
| 75 | #define SPI_CMD_TXMSBF BIT(12) |
| 76 | #define SPI_CMD_RXMSBF BIT(13) |
| 77 | #define SPI_CMD_RX_ENDIAN BIT(14) |
| 78 | #define SPI_CMD_TX_ENDIAN BIT(15) |
| 79 | #define SPI_CMD_FINISH_IE BIT(16) |
| 80 | #define SPI_CMD_PAUSE_IE BIT(17) |
| 81 | #define SPI_CMD_IPM_NONIDLE_MODE BIT(19) |
| 82 | #define SPI_CMD_IPM_SPIM_LOOP BIT(21) |
| 83 | #define SPI_CMD_IPM_GET_TICKDLY_OFFSET 22 |
| 84 | |
| 85 | #define SPI_CMD_IPM_GET_TICKDLY_MASK GENMASK(24, 22) |
| 86 | |
| 87 | #define PIN_MODE_CFG(x) ((x) / 2) |
| 88 | |
| 89 | #define SPI_CFG3_IPM_PIN_MODE_OFFSET 0 |
| 90 | #define SPI_CFG3_IPM_HALF_DUPLEX_DIR BIT(2) |
| 91 | #define SPI_CFG3_IPM_HALF_DUPLEX_EN BIT(3) |
| 92 | #define SPI_CFG3_IPM_XMODE_EN BIT(4) |
| 93 | #define SPI_CFG3_IPM_NODATA_FLAG BIT(5) |
| 94 | #define SPI_CFG3_IPM_CMD_BYTELEN_OFFSET 8 |
| 95 | #define SPI_CFG3_IPM_ADDR_BYTELEN_OFFSET 12 |
| 96 | #define SPI_CFG3_IPM_DUMMY_BYTELEN_OFFSET 16 |
| 97 | |
| 98 | #define SPI_CFG3_IPM_CMD_PIN_MODE_MASK GENMASK(1, 0) |
| 99 | #define SPI_CFG3_IPM_CMD_BYTELEN_MASK GENMASK(11, 8) |
| 100 | #define SPI_CFG3_IPM_ADDR_BYTELEN_MASK GENMASK(15, 12) |
| 101 | #define SPI_CFG3_IPM_DUMMY_BYTELEN_MASK GENMASK(19, 16) |
| 102 | |
| 103 | #define MT8173_SPI_MAX_PAD_SEL 3 |
| 104 | |
| 105 | #define MTK_SPI_PAUSE_INT_STATUS 0x2 |
| 106 | |
| 107 | #define MTK_SPI_IDLE 0 |
| 108 | #define MTK_SPI_PAUSED 1 |
| 109 | |
| 110 | #define MTK_SPI_MAX_FIFO_SIZE 32U |
| 111 | #define MTK_SPI_PACKET_SIZE 1024 |
| 112 | #define MTK_SPI_IPM_PACKET_SIZE SZ_64K |
| 113 | #define MTK_SPI_IPM_PACKET_LOOP SZ_256 |
| 114 | |
| 115 | #define MTK_SPI_32BITS_MASK 0xffffffff |
| 116 | |
| 117 | #define DMA_ADDR_EXT_BITS 36 |
| 118 | #define DMA_ADDR_DEF_BITS 32 |
| 119 | |
| 120 | #define CLK_TO_US(freq, clkcnt) DIV_ROUND_UP((clkcnt), (freq) / 1000000) |
| 121 | |
| 122 | /* struct mtk_spim_capability |
| 123 | * @enhance_timing: Some IC design adjust cfg register to enhance time accuracy |
| 124 | * @dma_ext: Some IC support DMA addr extension |
| 125 | * @ipm_design: The IPM IP design improves some features, and supports dual/quad mode |
| 126 | * @support_quad: Whether quad mode is supported |
| 127 | */ |
| 128 | struct mtk_spim_capability { |
| 129 | bool enhance_timing; |
| 130 | bool dma_ext; |
| 131 | bool ipm_design; |
| 132 | bool support_quad; |
| 133 | }; |
| 134 | |
| 135 | /* struct mtk_spim_priv |
| 136 | * @base: Base address of the spi controller |
| 137 | * @state: Controller state |
| 138 | * @sel_clk: Pad clock |
| 139 | * @spi_clk: Core clock |
Weijie Gao | 793e623 | 2023-07-19 17:15:54 +0800 | [diff] [blame^] | 140 | * @pll_clk_rate: Controller's PLL source clock rate, which is different |
| 141 | * from SPI bus clock rate |
Weijie Gao | b34a236 | 2022-09-09 19:59:45 +0800 | [diff] [blame] | 142 | * @xfer_len: Current length of data for transfer |
| 143 | * @hw_cap: Controller capabilities |
| 144 | * @tick_dly: Used to postpone SPI sampling time |
| 145 | * @sample_sel: Sample edge of MISO |
| 146 | * @dev: udevice of this spi controller |
| 147 | * @tx_dma: Tx DMA address |
| 148 | * @rx_dma: Rx DMA address |
| 149 | */ |
| 150 | struct mtk_spim_priv { |
| 151 | void __iomem *base; |
| 152 | u32 state; |
| 153 | struct clk sel_clk, spi_clk; |
Weijie Gao | 793e623 | 2023-07-19 17:15:54 +0800 | [diff] [blame^] | 154 | u32 pll_clk_rate; |
Weijie Gao | b34a236 | 2022-09-09 19:59:45 +0800 | [diff] [blame] | 155 | u32 xfer_len; |
| 156 | struct mtk_spim_capability hw_cap; |
| 157 | u32 tick_dly; |
| 158 | u32 sample_sel; |
| 159 | |
| 160 | struct device *dev; |
| 161 | dma_addr_t tx_dma; |
| 162 | dma_addr_t rx_dma; |
| 163 | }; |
| 164 | |
| 165 | static void mtk_spim_reset(struct mtk_spim_priv *priv) |
| 166 | { |
| 167 | /* set the software reset bit in SPI_CMD_REG. */ |
| 168 | setbits_le32(priv->base + SPI_CMD_REG, SPI_CMD_RST); |
| 169 | clrbits_le32(priv->base + SPI_CMD_REG, SPI_CMD_RST); |
| 170 | } |
| 171 | |
| 172 | static int mtk_spim_hw_init(struct spi_slave *slave) |
| 173 | { |
| 174 | struct udevice *bus = dev_get_parent(slave->dev); |
| 175 | struct mtk_spim_priv *priv = dev_get_priv(bus); |
| 176 | u16 cpha, cpol; |
| 177 | u32 reg_val; |
| 178 | |
| 179 | cpha = slave->mode & SPI_CPHA ? 1 : 0; |
| 180 | cpol = slave->mode & SPI_CPOL ? 1 : 0; |
| 181 | |
| 182 | if (priv->hw_cap.enhance_timing) { |
| 183 | if (priv->hw_cap.ipm_design) { |
| 184 | /* CFG3 reg only used for spi-mem, |
| 185 | * here write to default value |
| 186 | */ |
| 187 | writel(0x0, priv->base + SPI_CFG3_IPM_REG); |
| 188 | clrsetbits_le32(priv->base + SPI_CMD_REG, |
| 189 | SPI_CMD_IPM_GET_TICKDLY_MASK, |
| 190 | priv->tick_dly << |
| 191 | SPI_CMD_IPM_GET_TICKDLY_OFFSET); |
| 192 | } else { |
| 193 | clrsetbits_le32(priv->base + SPI_CFG1_REG, |
| 194 | SPI_CFG1_GET_TICKDLY_MASK, |
| 195 | priv->tick_dly << |
| 196 | SPI_CFG1_GET_TICKDLY_OFFSET); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | reg_val = readl(priv->base + SPI_CMD_REG); |
| 201 | if (priv->hw_cap.ipm_design) { |
| 202 | /* SPI transfer without idle time until packet length done */ |
| 203 | reg_val |= SPI_CMD_IPM_NONIDLE_MODE; |
| 204 | if (slave->mode & SPI_LOOP) |
| 205 | reg_val |= SPI_CMD_IPM_SPIM_LOOP; |
| 206 | else |
| 207 | reg_val &= ~SPI_CMD_IPM_SPIM_LOOP; |
| 208 | } |
| 209 | |
| 210 | if (cpha) |
| 211 | reg_val |= SPI_CMD_CPHA; |
| 212 | else |
| 213 | reg_val &= ~SPI_CMD_CPHA; |
| 214 | if (cpol) |
| 215 | reg_val |= SPI_CMD_CPOL; |
| 216 | else |
| 217 | reg_val &= ~SPI_CMD_CPOL; |
| 218 | |
| 219 | /* set the mlsbx and mlsbtx */ |
| 220 | if (slave->mode & SPI_LSB_FIRST) { |
| 221 | reg_val &= ~SPI_CMD_TXMSBF; |
| 222 | reg_val &= ~SPI_CMD_RXMSBF; |
| 223 | } else { |
| 224 | reg_val |= SPI_CMD_TXMSBF; |
| 225 | reg_val |= SPI_CMD_RXMSBF; |
| 226 | } |
| 227 | |
| 228 | /* do not reverse tx/rx endian */ |
| 229 | reg_val &= ~SPI_CMD_TX_ENDIAN; |
| 230 | reg_val &= ~SPI_CMD_RX_ENDIAN; |
| 231 | |
| 232 | if (priv->hw_cap.enhance_timing) { |
| 233 | /* set CS polarity */ |
| 234 | if (slave->mode & SPI_CS_HIGH) |
| 235 | reg_val |= SPI_CMD_CS_POL; |
| 236 | else |
| 237 | reg_val &= ~SPI_CMD_CS_POL; |
| 238 | |
| 239 | if (priv->sample_sel) |
| 240 | reg_val |= SPI_CMD_SAMPLE_SEL; |
| 241 | else |
| 242 | reg_val &= ~SPI_CMD_SAMPLE_SEL; |
| 243 | } |
| 244 | |
| 245 | /* disable dma mode */ |
| 246 | reg_val &= ~(SPI_CMD_TX_DMA | SPI_CMD_RX_DMA); |
| 247 | |
| 248 | /* disable deassert mode */ |
| 249 | reg_val &= ~SPI_CMD_DEASSERT; |
| 250 | |
| 251 | writel(reg_val, priv->base + SPI_CMD_REG); |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | static void mtk_spim_prepare_transfer(struct mtk_spim_priv *priv, |
| 257 | u32 speed_hz) |
| 258 | { |
Weijie Gao | 793e623 | 2023-07-19 17:15:54 +0800 | [diff] [blame^] | 259 | u32 div, sck_time, cs_time, reg_val; |
Weijie Gao | b34a236 | 2022-09-09 19:59:45 +0800 | [diff] [blame] | 260 | |
Weijie Gao | 793e623 | 2023-07-19 17:15:54 +0800 | [diff] [blame^] | 261 | if (speed_hz <= priv->pll_clk_rate / 4) |
| 262 | div = DIV_ROUND_UP(priv->pll_clk_rate, speed_hz); |
Weijie Gao | b34a236 | 2022-09-09 19:59:45 +0800 | [diff] [blame] | 263 | else |
| 264 | div = 4; |
| 265 | |
| 266 | sck_time = (div + 1) / 2; |
| 267 | cs_time = sck_time * 2; |
| 268 | |
| 269 | if (priv->hw_cap.enhance_timing) { |
| 270 | reg_val = ((sck_time - 1) & 0xffff) |
| 271 | << SPI_CFG2_SCK_HIGH_OFFSET; |
| 272 | reg_val |= ((sck_time - 1) & 0xffff) |
| 273 | << SPI_CFG2_SCK_LOW_OFFSET; |
| 274 | writel(reg_val, priv->base + SPI_CFG2_REG); |
| 275 | |
| 276 | reg_val = ((cs_time - 1) & 0xffff) |
| 277 | << SPI_ADJUST_CFG0_CS_HOLD_OFFSET; |
| 278 | reg_val |= ((cs_time - 1) & 0xffff) |
| 279 | << SPI_ADJUST_CFG0_CS_SETUP_OFFSET; |
| 280 | writel(reg_val, priv->base + SPI_CFG0_REG); |
| 281 | } else { |
| 282 | reg_val = ((sck_time - 1) & 0xff) |
| 283 | << SPI_CFG0_SCK_HIGH_OFFSET; |
| 284 | reg_val |= ((sck_time - 1) & 0xff) << SPI_CFG0_SCK_LOW_OFFSET; |
| 285 | reg_val |= ((cs_time - 1) & 0xff) << SPI_CFG0_CS_HOLD_OFFSET; |
| 286 | reg_val |= ((cs_time - 1) & 0xff) << SPI_CFG0_CS_SETUP_OFFSET; |
| 287 | writel(reg_val, priv->base + SPI_CFG0_REG); |
| 288 | } |
| 289 | |
| 290 | reg_val = readl(priv->base + SPI_CFG1_REG); |
| 291 | reg_val &= ~SPI_CFG1_CS_IDLE_MASK; |
| 292 | reg_val |= ((cs_time - 1) & 0xff) << SPI_CFG1_CS_IDLE_OFFSET; |
| 293 | writel(reg_val, priv->base + SPI_CFG1_REG); |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * mtk_spim_setup_packet() - setup packet format. |
| 298 | * @priv: controller priv |
| 299 | * |
| 300 | * This controller sents/receives data in packets. The packet size is |
| 301 | * configurable. |
| 302 | * |
| 303 | * This function calculates the maximum packet size available for current |
| 304 | * data, and calculates the number of packets required to sent/receive data |
| 305 | * as much as possible. |
| 306 | */ |
| 307 | static void mtk_spim_setup_packet(struct mtk_spim_priv *priv) |
| 308 | { |
| 309 | u32 packet_size, packet_loop, reg_val; |
| 310 | |
| 311 | /* Calculate maximum packet size */ |
| 312 | if (priv->hw_cap.ipm_design) |
| 313 | packet_size = min_t(u32, |
| 314 | priv->xfer_len, |
| 315 | MTK_SPI_IPM_PACKET_SIZE); |
| 316 | else |
| 317 | packet_size = min_t(u32, |
| 318 | priv->xfer_len, |
| 319 | MTK_SPI_PACKET_SIZE); |
| 320 | |
| 321 | /* Calculates number of packets to sent/receive */ |
| 322 | packet_loop = priv->xfer_len / packet_size; |
| 323 | |
| 324 | reg_val = readl(priv->base + SPI_CFG1_REG); |
| 325 | if (priv->hw_cap.ipm_design) |
| 326 | reg_val &= ~SPI_CFG1_IPM_PACKET_LENGTH_MASK; |
| 327 | else |
| 328 | reg_val &= ~SPI_CFG1_PACKET_LENGTH_MASK; |
| 329 | |
| 330 | reg_val |= (packet_size - 1) << SPI_CFG1_PACKET_LENGTH_OFFSET; |
| 331 | |
| 332 | reg_val &= ~SPI_CFG1_PACKET_LOOP_MASK; |
| 333 | |
| 334 | reg_val |= (packet_loop - 1) << SPI_CFG1_PACKET_LOOP_OFFSET; |
| 335 | |
| 336 | writel(reg_val, priv->base + SPI_CFG1_REG); |
| 337 | } |
| 338 | |
| 339 | static void mtk_spim_enable_transfer(struct mtk_spim_priv *priv) |
| 340 | { |
| 341 | u32 cmd; |
| 342 | |
| 343 | cmd = readl(priv->base + SPI_CMD_REG); |
| 344 | if (priv->state == MTK_SPI_IDLE) |
| 345 | cmd |= SPI_CMD_ACT; |
| 346 | else |
| 347 | cmd |= SPI_CMD_RESUME; |
| 348 | writel(cmd, priv->base + SPI_CMD_REG); |
| 349 | } |
| 350 | |
| 351 | static bool mtk_spim_supports_op(struct spi_slave *slave, |
| 352 | const struct spi_mem_op *op) |
| 353 | { |
| 354 | struct udevice *bus = dev_get_parent(slave->dev); |
| 355 | struct mtk_spim_priv *priv = dev_get_priv(bus); |
| 356 | |
| 357 | if (op->cmd.buswidth == 0 || op->cmd.buswidth > 4 || |
| 358 | op->addr.buswidth > 4 || op->dummy.buswidth > 4 || |
| 359 | op->data.buswidth > 4) |
| 360 | return false; |
| 361 | |
| 362 | if (!priv->hw_cap.support_quad && (op->cmd.buswidth > 2 || |
| 363 | op->addr.buswidth > 2 || op->dummy.buswidth > 2 || |
| 364 | op->data.buswidth > 2)) |
| 365 | return false; |
| 366 | |
| 367 | if (op->addr.nbytes && op->dummy.nbytes && |
| 368 | op->addr.buswidth != op->dummy.buswidth) |
| 369 | return false; |
| 370 | |
| 371 | if (op->addr.nbytes + op->dummy.nbytes > 16) |
| 372 | return false; |
| 373 | |
| 374 | if (op->data.nbytes > MTK_SPI_IPM_PACKET_SIZE) { |
| 375 | if (op->data.nbytes / MTK_SPI_IPM_PACKET_SIZE > |
| 376 | MTK_SPI_IPM_PACKET_LOOP || |
| 377 | op->data.nbytes % MTK_SPI_IPM_PACKET_SIZE != 0) |
| 378 | return false; |
| 379 | } |
| 380 | |
| 381 | return true; |
| 382 | } |
| 383 | |
| 384 | static void mtk_spim_setup_dma_xfer(struct mtk_spim_priv *priv, |
| 385 | const struct spi_mem_op *op) |
| 386 | { |
| 387 | writel((u32)(priv->tx_dma & MTK_SPI_32BITS_MASK), |
| 388 | priv->base + SPI_TX_SRC_REG); |
| 389 | |
| 390 | if (priv->hw_cap.dma_ext) |
| 391 | writel((u32)(priv->tx_dma >> 32), |
| 392 | priv->base + SPI_TX_SRC_REG_64); |
| 393 | |
| 394 | if (op->data.dir == SPI_MEM_DATA_IN) { |
| 395 | writel((u32)(priv->rx_dma & MTK_SPI_32BITS_MASK), |
| 396 | priv->base + SPI_RX_DST_REG); |
| 397 | |
| 398 | if (priv->hw_cap.dma_ext) |
| 399 | writel((u32)(priv->rx_dma >> 32), |
| 400 | priv->base + SPI_RX_DST_REG_64); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | static int mtk_spim_transfer_wait(struct spi_slave *slave, |
| 405 | const struct spi_mem_op *op) |
| 406 | { |
| 407 | struct udevice *bus = dev_get_parent(slave->dev); |
| 408 | struct mtk_spim_priv *priv = dev_get_priv(bus); |
Weijie Gao | 793e623 | 2023-07-19 17:15:54 +0800 | [diff] [blame^] | 409 | u32 sck_l, sck_h, clk_count, reg; |
Weijie Gao | b34a236 | 2022-09-09 19:59:45 +0800 | [diff] [blame] | 410 | ulong us = 1; |
| 411 | int ret = 0; |
| 412 | |
| 413 | if (op->data.dir == SPI_MEM_NO_DATA) |
| 414 | clk_count = 32; |
| 415 | else |
| 416 | clk_count = op->data.nbytes; |
| 417 | |
Weijie Gao | b34a236 | 2022-09-09 19:59:45 +0800 | [diff] [blame] | 418 | sck_l = readl(priv->base + SPI_CFG2_REG) >> SPI_CFG2_SCK_LOW_OFFSET; |
| 419 | sck_h = readl(priv->base + SPI_CFG2_REG) & SPI_CFG2_SCK_HIGH_MASK; |
Weijie Gao | 793e623 | 2023-07-19 17:15:54 +0800 | [diff] [blame^] | 420 | do_div(priv->pll_clk_rate, sck_l + sck_h + 2); |
Weijie Gao | b34a236 | 2022-09-09 19:59:45 +0800 | [diff] [blame] | 421 | |
Weijie Gao | 793e623 | 2023-07-19 17:15:54 +0800 | [diff] [blame^] | 422 | us = CLK_TO_US(priv->pll_clk_rate, clk_count * 8); |
Weijie Gao | b34a236 | 2022-09-09 19:59:45 +0800 | [diff] [blame] | 423 | us += 1000 * 1000; /* 1s tolerance */ |
| 424 | |
| 425 | if (us > UINT_MAX) |
| 426 | us = UINT_MAX; |
| 427 | |
| 428 | ret = readl_poll_timeout(priv->base + SPI_STATUS_REG, reg, |
| 429 | reg & 0x1, us); |
| 430 | if (ret < 0) { |
| 431 | dev_err(priv->dev, "transfer timeout, val: 0x%lx\n", us); |
| 432 | return -ETIMEDOUT; |
| 433 | } |
| 434 | |
| 435 | return 0; |
| 436 | } |
| 437 | |
| 438 | static int mtk_spim_exec_op(struct spi_slave *slave, |
| 439 | const struct spi_mem_op *op) |
| 440 | { |
| 441 | struct udevice *bus = dev_get_parent(slave->dev); |
| 442 | struct mtk_spim_priv *priv = dev_get_priv(bus); |
| 443 | u32 reg_val, nio = 1, tx_size; |
| 444 | char *tx_tmp_buf; |
| 445 | char *rx_tmp_buf; |
| 446 | int i, ret = 0; |
| 447 | |
| 448 | mtk_spim_reset(priv); |
| 449 | mtk_spim_hw_init(slave); |
| 450 | mtk_spim_prepare_transfer(priv, slave->max_hz); |
| 451 | |
| 452 | reg_val = readl(priv->base + SPI_CFG3_IPM_REG); |
| 453 | /* opcode byte len */ |
| 454 | reg_val &= ~SPI_CFG3_IPM_CMD_BYTELEN_MASK; |
| 455 | reg_val |= 1 << SPI_CFG3_IPM_CMD_BYTELEN_OFFSET; |
| 456 | |
| 457 | /* addr & dummy byte len */ |
| 458 | if (op->addr.nbytes || op->dummy.nbytes) |
| 459 | reg_val |= (op->addr.nbytes + op->dummy.nbytes) << |
| 460 | SPI_CFG3_IPM_ADDR_BYTELEN_OFFSET; |
| 461 | |
| 462 | /* data byte len */ |
| 463 | if (!op->data.nbytes) { |
| 464 | reg_val |= SPI_CFG3_IPM_NODATA_FLAG; |
| 465 | writel(0, priv->base + SPI_CFG1_REG); |
| 466 | } else { |
| 467 | reg_val &= ~SPI_CFG3_IPM_NODATA_FLAG; |
| 468 | priv->xfer_len = op->data.nbytes; |
| 469 | mtk_spim_setup_packet(priv); |
| 470 | } |
| 471 | |
| 472 | if (op->addr.nbytes || op->dummy.nbytes) { |
| 473 | if (op->addr.buswidth == 1 || op->dummy.buswidth == 1) |
| 474 | reg_val |= SPI_CFG3_IPM_XMODE_EN; |
| 475 | else |
| 476 | reg_val &= ~SPI_CFG3_IPM_XMODE_EN; |
| 477 | } |
| 478 | |
| 479 | if (op->addr.buswidth == 2 || |
| 480 | op->dummy.buswidth == 2 || |
| 481 | op->data.buswidth == 2) |
| 482 | nio = 2; |
| 483 | else if (op->addr.buswidth == 4 || |
| 484 | op->dummy.buswidth == 4 || |
| 485 | op->data.buswidth == 4) |
| 486 | nio = 4; |
| 487 | |
| 488 | reg_val &= ~SPI_CFG3_IPM_CMD_PIN_MODE_MASK; |
| 489 | reg_val |= PIN_MODE_CFG(nio) << SPI_CFG3_IPM_PIN_MODE_OFFSET; |
| 490 | |
| 491 | reg_val |= SPI_CFG3_IPM_HALF_DUPLEX_EN; |
| 492 | if (op->data.dir == SPI_MEM_DATA_IN) |
| 493 | reg_val |= SPI_CFG3_IPM_HALF_DUPLEX_DIR; |
| 494 | else |
| 495 | reg_val &= ~SPI_CFG3_IPM_HALF_DUPLEX_DIR; |
| 496 | writel(reg_val, priv->base + SPI_CFG3_IPM_REG); |
| 497 | |
| 498 | tx_size = 1 + op->addr.nbytes + op->dummy.nbytes; |
| 499 | if (op->data.dir == SPI_MEM_DATA_OUT) |
| 500 | tx_size += op->data.nbytes; |
| 501 | |
| 502 | tx_size = max(tx_size, (u32)32); |
| 503 | |
| 504 | /* Fill up tx data */ |
| 505 | tx_tmp_buf = kzalloc(tx_size, GFP_KERNEL); |
| 506 | if (!tx_tmp_buf) { |
| 507 | ret = -ENOMEM; |
| 508 | goto exit; |
| 509 | } |
| 510 | |
| 511 | tx_tmp_buf[0] = op->cmd.opcode; |
| 512 | |
| 513 | if (op->addr.nbytes) { |
| 514 | for (i = 0; i < op->addr.nbytes; i++) |
| 515 | tx_tmp_buf[i + 1] = op->addr.val >> |
| 516 | (8 * (op->addr.nbytes - i - 1)); |
| 517 | } |
| 518 | |
| 519 | if (op->dummy.nbytes) |
| 520 | memset(tx_tmp_buf + op->addr.nbytes + 1, 0xff, |
| 521 | op->dummy.nbytes); |
| 522 | |
| 523 | if (op->data.nbytes && op->data.dir == SPI_MEM_DATA_OUT) |
| 524 | memcpy(tx_tmp_buf + op->dummy.nbytes + op->addr.nbytes + 1, |
| 525 | op->data.buf.out, op->data.nbytes); |
| 526 | /* Finish filling up tx data */ |
| 527 | |
| 528 | priv->tx_dma = dma_map_single(tx_tmp_buf, tx_size, DMA_TO_DEVICE); |
| 529 | if (dma_mapping_error(priv->dev, priv->tx_dma)) { |
| 530 | ret = -ENOMEM; |
| 531 | goto tx_free; |
| 532 | } |
| 533 | |
| 534 | if (op->data.dir == SPI_MEM_DATA_IN) { |
| 535 | if (!IS_ALIGNED((size_t)op->data.buf.in, 4)) { |
| 536 | rx_tmp_buf = kzalloc(op->data.nbytes, GFP_KERNEL); |
| 537 | if (!rx_tmp_buf) { |
| 538 | ret = -ENOMEM; |
| 539 | goto tx_unmap; |
| 540 | } |
| 541 | } else { |
| 542 | rx_tmp_buf = op->data.buf.in; |
| 543 | } |
| 544 | |
| 545 | priv->rx_dma = dma_map_single(rx_tmp_buf, op->data.nbytes, |
| 546 | DMA_FROM_DEVICE); |
| 547 | if (dma_mapping_error(priv->dev, priv->rx_dma)) { |
| 548 | ret = -ENOMEM; |
| 549 | goto rx_free; |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | reg_val = readl(priv->base + SPI_CMD_REG); |
| 554 | reg_val |= SPI_CMD_TX_DMA; |
| 555 | if (op->data.dir == SPI_MEM_DATA_IN) |
| 556 | reg_val |= SPI_CMD_RX_DMA; |
| 557 | |
| 558 | writel(reg_val, priv->base + SPI_CMD_REG); |
| 559 | |
| 560 | mtk_spim_setup_dma_xfer(priv, op); |
| 561 | |
| 562 | mtk_spim_enable_transfer(priv); |
| 563 | |
| 564 | /* Wait for the interrupt. */ |
| 565 | ret = mtk_spim_transfer_wait(slave, op); |
| 566 | if (ret) |
| 567 | goto rx_unmap; |
| 568 | |
| 569 | if (op->data.dir == SPI_MEM_DATA_IN && |
| 570 | !IS_ALIGNED((size_t)op->data.buf.in, 4)) |
| 571 | memcpy(op->data.buf.in, rx_tmp_buf, op->data.nbytes); |
| 572 | |
| 573 | rx_unmap: |
| 574 | /* spi disable dma */ |
| 575 | reg_val = readl(priv->base + SPI_CMD_REG); |
| 576 | reg_val &= ~SPI_CMD_TX_DMA; |
| 577 | if (op->data.dir == SPI_MEM_DATA_IN) |
| 578 | reg_val &= ~SPI_CMD_RX_DMA; |
| 579 | writel(reg_val, priv->base + SPI_CMD_REG); |
| 580 | |
| 581 | writel(0, priv->base + SPI_TX_SRC_REG); |
| 582 | writel(0, priv->base + SPI_RX_DST_REG); |
| 583 | |
| 584 | if (op->data.dir == SPI_MEM_DATA_IN) |
| 585 | dma_unmap_single(priv->rx_dma, |
| 586 | op->data.nbytes, DMA_FROM_DEVICE); |
| 587 | rx_free: |
| 588 | if (op->data.dir == SPI_MEM_DATA_IN && |
| 589 | !IS_ALIGNED((size_t)op->data.buf.in, 4)) |
| 590 | kfree(rx_tmp_buf); |
| 591 | tx_unmap: |
| 592 | dma_unmap_single(priv->tx_dma, |
| 593 | tx_size, DMA_TO_DEVICE); |
| 594 | tx_free: |
| 595 | kfree(tx_tmp_buf); |
| 596 | exit: |
| 597 | return ret; |
| 598 | } |
| 599 | |
| 600 | static int mtk_spim_adjust_op_size(struct spi_slave *slave, |
| 601 | struct spi_mem_op *op) |
| 602 | { |
| 603 | int opcode_len; |
| 604 | |
| 605 | if (!op->data.nbytes) |
| 606 | return 0; |
| 607 | |
| 608 | if (op->data.dir != SPI_MEM_NO_DATA) { |
| 609 | opcode_len = 1 + op->addr.nbytes + op->dummy.nbytes; |
| 610 | if (opcode_len + op->data.nbytes > MTK_SPI_IPM_PACKET_SIZE) { |
| 611 | op->data.nbytes = MTK_SPI_IPM_PACKET_SIZE - opcode_len; |
| 612 | /* force data buffer dma-aligned. */ |
| 613 | op->data.nbytes -= op->data.nbytes % 4; |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | static int mtk_spim_get_attr(struct mtk_spim_priv *priv, struct udevice *dev) |
| 621 | { |
| 622 | int ret; |
| 623 | |
| 624 | priv->hw_cap.enhance_timing = dev_read_bool(dev, "enhance_timing"); |
| 625 | priv->hw_cap.dma_ext = dev_read_bool(dev, "dma_ext"); |
| 626 | priv->hw_cap.ipm_design = dev_read_bool(dev, "ipm_design"); |
| 627 | priv->hw_cap.support_quad = dev_read_bool(dev, "support_quad"); |
| 628 | |
| 629 | ret = dev_read_u32(dev, "tick_dly", &priv->tick_dly); |
| 630 | if (ret < 0) |
| 631 | dev_err(priv->dev, "tick dly not set.\n"); |
| 632 | |
| 633 | ret = dev_read_u32(dev, "sample_sel", &priv->sample_sel); |
| 634 | if (ret < 0) |
| 635 | dev_err(priv->dev, "sample sel not set.\n"); |
| 636 | |
| 637 | return ret; |
| 638 | } |
| 639 | |
| 640 | static int mtk_spim_probe(struct udevice *dev) |
| 641 | { |
| 642 | struct mtk_spim_priv *priv = dev_get_priv(dev); |
| 643 | int ret; |
| 644 | |
Johan Jonker | 0d01046 | 2023-03-13 01:32:44 +0100 | [diff] [blame] | 645 | priv->base = devfdt_get_addr_ptr(dev); |
Weijie Gao | b34a236 | 2022-09-09 19:59:45 +0800 | [diff] [blame] | 646 | if (!priv->base) |
| 647 | return -EINVAL; |
| 648 | |
| 649 | mtk_spim_get_attr(priv, dev); |
| 650 | |
| 651 | ret = clk_get_by_name(dev, "sel-clk", &priv->sel_clk); |
| 652 | if (ret < 0) { |
| 653 | dev_err(dev, "failed to get sel-clk\n"); |
| 654 | return ret; |
| 655 | } |
| 656 | |
| 657 | ret = clk_get_by_name(dev, "spi-clk", &priv->spi_clk); |
| 658 | if (ret < 0) { |
| 659 | dev_err(dev, "failed to get spi-clk\n"); |
| 660 | return ret; |
| 661 | } |
| 662 | |
| 663 | clk_enable(&priv->sel_clk); |
| 664 | clk_enable(&priv->spi_clk); |
| 665 | |
Weijie Gao | 793e623 | 2023-07-19 17:15:54 +0800 | [diff] [blame^] | 666 | priv->pll_clk_rate = clk_get_rate(&priv->spi_clk); |
| 667 | if (priv->pll_clk_rate == 0) |
| 668 | return -EINVAL; |
| 669 | |
Weijie Gao | b34a236 | 2022-09-09 19:59:45 +0800 | [diff] [blame] | 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | static int mtk_spim_set_speed(struct udevice *dev, uint speed) |
| 674 | { |
| 675 | return 0; |
| 676 | } |
| 677 | |
| 678 | static int mtk_spim_set_mode(struct udevice *dev, uint mode) |
| 679 | { |
| 680 | return 0; |
| 681 | } |
| 682 | |
| 683 | static const struct spi_controller_mem_ops mtk_spim_mem_ops = { |
| 684 | .adjust_op_size = mtk_spim_adjust_op_size, |
| 685 | .supports_op = mtk_spim_supports_op, |
| 686 | .exec_op = mtk_spim_exec_op |
| 687 | }; |
| 688 | |
| 689 | static const struct dm_spi_ops mtk_spim_ops = { |
| 690 | .mem_ops = &mtk_spim_mem_ops, |
| 691 | .set_speed = mtk_spim_set_speed, |
| 692 | .set_mode = mtk_spim_set_mode, |
| 693 | }; |
| 694 | |
| 695 | static const struct udevice_id mtk_spim_ids[] = { |
| 696 | { .compatible = "mediatek,ipm-spi" }, |
| 697 | {} |
| 698 | }; |
| 699 | |
| 700 | U_BOOT_DRIVER(mtk_spim) = { |
| 701 | .name = "mtk_spim", |
| 702 | .id = UCLASS_SPI, |
| 703 | .of_match = mtk_spim_ids, |
| 704 | .ops = &mtk_spim_ops, |
| 705 | .priv_auto = sizeof(struct mtk_spim_priv), |
| 706 | .probe = mtk_spim_probe, |
| 707 | }; |