Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Marek Vasut | ec33de3 | 2011-11-08 23:18:14 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Freescale i.MX28 SPI driver |
| 4 | * |
Lukasz Majewski | d99b018 | 2019-06-19 17:31:07 +0200 | [diff] [blame] | 5 | * Copyright (C) 2019 DENX Software Engineering |
| 6 | * Lukasz Majewski, DENX Software Engineering, lukma@denx.de |
| 7 | * |
Marek Vasut | ec33de3 | 2011-11-08 23:18:14 +0000 | [diff] [blame] | 8 | * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com> |
| 9 | * on behalf of DENX Software Engineering GmbH |
| 10 | * |
Marek Vasut | ec33de3 | 2011-11-08 23:18:14 +0000 | [diff] [blame] | 11 | * NOTE: This driver only supports the SPI-controller chipselects, |
| 12 | * GPIO driven chipselects are not supported. |
| 13 | */ |
| 14 | |
| 15 | #include <common.h> |
| 16 | #include <malloc.h> |
Simon Glass | cf92e05 | 2015-09-02 17:24:58 -0600 | [diff] [blame] | 17 | #include <memalign.h> |
Marek Vasut | ec33de3 | 2011-11-08 23:18:14 +0000 | [diff] [blame] | 18 | #include <spi.h> |
Masahiro Yamada | 1221ce4 | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 19 | #include <linux/errno.h> |
Marek Vasut | ec33de3 | 2011-11-08 23:18:14 +0000 | [diff] [blame] | 20 | #include <asm/io.h> |
| 21 | #include <asm/arch/clock.h> |
| 22 | #include <asm/arch/imx-regs.h> |
| 23 | #include <asm/arch/sys_proto.h> |
Stefano Babic | 552a848 | 2017-06-29 10:16:06 +0200 | [diff] [blame] | 24 | #include <asm/mach-imx/dma.h> |
Marek Vasut | ec33de3 | 2011-11-08 23:18:14 +0000 | [diff] [blame] | 25 | |
| 26 | #define MXS_SPI_MAX_TIMEOUT 1000000 |
| 27 | #define MXS_SPI_PORT_OFFSET 0x2000 |
Fabio Estevam | 148ca64 | 2012-04-23 08:30:50 +0000 | [diff] [blame] | 28 | #define MXS_SSP_CHIPSELECT_MASK 0x00300000 |
| 29 | #define MXS_SSP_CHIPSELECT_SHIFT 20 |
Marek Vasut | ec33de3 | 2011-11-08 23:18:14 +0000 | [diff] [blame] | 30 | |
Marek Vasut | 7c5e6f7 | 2012-07-09 00:48:33 +0000 | [diff] [blame] | 31 | #define MXSSSP_SMALL_TRANSFER 512 |
| 32 | |
Otavio Salvador | 9c47114 | 2012-08-05 09:05:31 +0000 | [diff] [blame] | 33 | static void mxs_spi_start_xfer(struct mxs_ssp_regs *ssp_regs) |
Marek Vasut | ec33de3 | 2011-11-08 23:18:14 +0000 | [diff] [blame] | 34 | { |
| 35 | writel(SSP_CTRL0_LOCK_CS, &ssp_regs->hw_ssp_ctrl0_set); |
| 36 | writel(SSP_CTRL0_IGNORE_CRC, &ssp_regs->hw_ssp_ctrl0_clr); |
| 37 | } |
| 38 | |
Otavio Salvador | 9c47114 | 2012-08-05 09:05:31 +0000 | [diff] [blame] | 39 | static void mxs_spi_end_xfer(struct mxs_ssp_regs *ssp_regs) |
Marek Vasut | ec33de3 | 2011-11-08 23:18:14 +0000 | [diff] [blame] | 40 | { |
| 41 | writel(SSP_CTRL0_LOCK_CS, &ssp_regs->hw_ssp_ctrl0_clr); |
| 42 | writel(SSP_CTRL0_IGNORE_CRC, &ssp_regs->hw_ssp_ctrl0_set); |
| 43 | } |
| 44 | |
Lukasz Majewski | d99b018 | 2019-06-19 17:31:07 +0200 | [diff] [blame] | 45 | #if !CONFIG_IS_ENABLED(DM_SPI) |
| 46 | struct mxs_spi_slave { |
| 47 | struct spi_slave slave; |
| 48 | uint32_t max_khz; |
| 49 | uint32_t mode; |
| 50 | struct mxs_ssp_regs *regs; |
| 51 | }; |
| 52 | |
| 53 | static inline struct mxs_spi_slave *to_mxs_slave(struct spi_slave *slave) |
| 54 | { |
| 55 | return container_of(slave, struct mxs_spi_slave, slave); |
| 56 | } |
| 57 | #else |
| 58 | #include <dm.h> |
| 59 | #include <errno.h> |
Lukasz Majewski | ec0c81f | 2019-09-05 09:54:58 +0200 | [diff] [blame] | 60 | #include <dt-structs.h> |
| 61 | |
| 62 | #ifdef CONFIG_MX28 |
| 63 | #define dtd_fsl_imx_spi dtd_fsl_imx28_spi |
| 64 | #else /* CONFIG_MX23 */ |
| 65 | #define dtd_fsl_imx_spi dtd_fsl_imx23_spi |
| 66 | #endif |
| 67 | |
Lukasz Majewski | d99b018 | 2019-06-19 17:31:07 +0200 | [diff] [blame] | 68 | struct mxs_spi_platdata { |
Lukasz Majewski | ec0c81f | 2019-09-05 09:54:58 +0200 | [diff] [blame] | 69 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 70 | struct dtd_fsl_imx_spi dtplat; |
| 71 | #endif |
Lukasz Majewski | d99b018 | 2019-06-19 17:31:07 +0200 | [diff] [blame] | 72 | s32 frequency; /* Default clock frequency, -1 for none */ |
| 73 | fdt_addr_t base; /* SPI IP block base address */ |
| 74 | int num_cs; /* Number of CSes supported */ |
| 75 | int dma_id; /* ID of the DMA channel */ |
| 76 | int clk_id; /* ID of the SSP clock */ |
| 77 | }; |
| 78 | |
| 79 | struct mxs_spi_priv { |
| 80 | struct mxs_ssp_regs *regs; |
| 81 | unsigned int dma_channel; |
| 82 | unsigned int max_freq; |
| 83 | unsigned int clk_id; |
| 84 | unsigned int mode; |
| 85 | }; |
| 86 | #endif |
| 87 | |
| 88 | #if !CONFIG_IS_ENABLED(DM_SPI) |
Marek Vasut | ccd4d5a | 2012-07-09 00:48:32 +0000 | [diff] [blame] | 89 | static int mxs_spi_xfer_pio(struct mxs_spi_slave *slave, |
| 90 | char *data, int length, int write, unsigned long flags) |
Marek Vasut | ec33de3 | 2011-11-08 23:18:14 +0000 | [diff] [blame] | 91 | { |
Otavio Salvador | 9c47114 | 2012-08-05 09:05:31 +0000 | [diff] [blame] | 92 | struct mxs_ssp_regs *ssp_regs = slave->regs; |
Lukasz Majewski | d99b018 | 2019-06-19 17:31:07 +0200 | [diff] [blame] | 93 | #else |
| 94 | static int mxs_spi_xfer_pio(struct mxs_spi_priv *priv, |
| 95 | char *data, int length, int write, |
| 96 | unsigned long flags) |
| 97 | { |
| 98 | struct mxs_ssp_regs *ssp_regs = priv->regs; |
| 99 | #endif |
Marek Vasut | c7065fa | 2012-07-09 00:48:31 +0000 | [diff] [blame] | 100 | |
Marek Vasut | ec33de3 | 2011-11-08 23:18:14 +0000 | [diff] [blame] | 101 | if (flags & SPI_XFER_BEGIN) |
| 102 | mxs_spi_start_xfer(ssp_regs); |
| 103 | |
Marek Vasut | ccd4d5a | 2012-07-09 00:48:32 +0000 | [diff] [blame] | 104 | while (length--) { |
Marek Vasut | ec33de3 | 2011-11-08 23:18:14 +0000 | [diff] [blame] | 105 | /* We transfer 1 byte */ |
Marek Vasut | c96e78c | 2013-02-23 02:42:59 +0000 | [diff] [blame] | 106 | #if defined(CONFIG_MX23) |
| 107 | writel(SSP_CTRL0_XFER_COUNT_MASK, &ssp_regs->hw_ssp_ctrl0_clr); |
| 108 | writel(1, &ssp_regs->hw_ssp_ctrl0_set); |
| 109 | #elif defined(CONFIG_MX28) |
Marek Vasut | ec33de3 | 2011-11-08 23:18:14 +0000 | [diff] [blame] | 110 | writel(1, &ssp_regs->hw_ssp_xfer_size); |
Marek Vasut | c96e78c | 2013-02-23 02:42:59 +0000 | [diff] [blame] | 111 | #endif |
Marek Vasut | ec33de3 | 2011-11-08 23:18:14 +0000 | [diff] [blame] | 112 | |
Marek Vasut | ccd4d5a | 2012-07-09 00:48:32 +0000 | [diff] [blame] | 113 | if ((flags & SPI_XFER_END) && !length) |
Marek Vasut | ec33de3 | 2011-11-08 23:18:14 +0000 | [diff] [blame] | 114 | mxs_spi_end_xfer(ssp_regs); |
| 115 | |
Marek Vasut | c7065fa | 2012-07-09 00:48:31 +0000 | [diff] [blame] | 116 | if (write) |
Marek Vasut | ec33de3 | 2011-11-08 23:18:14 +0000 | [diff] [blame] | 117 | writel(SSP_CTRL0_READ, &ssp_regs->hw_ssp_ctrl0_clr); |
| 118 | else |
| 119 | writel(SSP_CTRL0_READ, &ssp_regs->hw_ssp_ctrl0_set); |
| 120 | |
| 121 | writel(SSP_CTRL0_RUN, &ssp_regs->hw_ssp_ctrl0_set); |
| 122 | |
Otavio Salvador | fa7a51c | 2012-08-13 09:53:12 +0000 | [diff] [blame] | 123 | if (mxs_wait_mask_set(&ssp_regs->hw_ssp_ctrl0_reg, |
Marek Vasut | ec33de3 | 2011-11-08 23:18:14 +0000 | [diff] [blame] | 124 | SSP_CTRL0_RUN, MXS_SPI_MAX_TIMEOUT)) { |
| 125 | printf("MXS SPI: Timeout waiting for start\n"); |
Fabio Estevam | d9fb6a4 | 2012-03-18 17:23:35 +0000 | [diff] [blame] | 126 | return -ETIMEDOUT; |
Marek Vasut | ec33de3 | 2011-11-08 23:18:14 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Marek Vasut | c7065fa | 2012-07-09 00:48:31 +0000 | [diff] [blame] | 129 | if (write) |
| 130 | writel(*data++, &ssp_regs->hw_ssp_data); |
Marek Vasut | ec33de3 | 2011-11-08 23:18:14 +0000 | [diff] [blame] | 131 | |
| 132 | writel(SSP_CTRL0_DATA_XFER, &ssp_regs->hw_ssp_ctrl0_set); |
| 133 | |
Marek Vasut | c7065fa | 2012-07-09 00:48:31 +0000 | [diff] [blame] | 134 | if (!write) { |
Otavio Salvador | fa7a51c | 2012-08-13 09:53:12 +0000 | [diff] [blame] | 135 | if (mxs_wait_mask_clr(&ssp_regs->hw_ssp_status_reg, |
Marek Vasut | ec33de3 | 2011-11-08 23:18:14 +0000 | [diff] [blame] | 136 | SSP_STATUS_FIFO_EMPTY, MXS_SPI_MAX_TIMEOUT)) { |
| 137 | printf("MXS SPI: Timeout waiting for data\n"); |
Fabio Estevam | d9fb6a4 | 2012-03-18 17:23:35 +0000 | [diff] [blame] | 138 | return -ETIMEDOUT; |
Marek Vasut | ec33de3 | 2011-11-08 23:18:14 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Marek Vasut | c7065fa | 2012-07-09 00:48:31 +0000 | [diff] [blame] | 141 | *data = readl(&ssp_regs->hw_ssp_data); |
| 142 | data++; |
Marek Vasut | ec33de3 | 2011-11-08 23:18:14 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Otavio Salvador | fa7a51c | 2012-08-13 09:53:12 +0000 | [diff] [blame] | 145 | if (mxs_wait_mask_clr(&ssp_regs->hw_ssp_ctrl0_reg, |
Marek Vasut | ec33de3 | 2011-11-08 23:18:14 +0000 | [diff] [blame] | 146 | SSP_CTRL0_RUN, MXS_SPI_MAX_TIMEOUT)) { |
| 147 | printf("MXS SPI: Timeout waiting for finish\n"); |
Fabio Estevam | d9fb6a4 | 2012-03-18 17:23:35 +0000 | [diff] [blame] | 148 | return -ETIMEDOUT; |
Marek Vasut | ec33de3 | 2011-11-08 23:18:14 +0000 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
| 152 | return 0; |
Marek Vasut | ccd4d5a | 2012-07-09 00:48:32 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Lukasz Majewski | d99b018 | 2019-06-19 17:31:07 +0200 | [diff] [blame] | 155 | #if !CONFIG_IS_ENABLED(DM_SPI) |
Marek Vasut | 7c5e6f7 | 2012-07-09 00:48:33 +0000 | [diff] [blame] | 156 | static int mxs_spi_xfer_dma(struct mxs_spi_slave *slave, |
| 157 | char *data, int length, int write, unsigned long flags) |
| 158 | { |
Lukasz Majewski | d99b018 | 2019-06-19 17:31:07 +0200 | [diff] [blame] | 159 | struct mxs_ssp_regs *ssp_regs = slave->regs; |
| 160 | #else |
| 161 | static int mxs_spi_xfer_dma(struct mxs_spi_priv *priv, |
| 162 | char *data, int length, int write, |
| 163 | unsigned long flags) |
| 164 | { struct mxs_ssp_regs *ssp_regs = priv->regs; |
| 165 | #endif |
Marek Vasut | 2c43214 | 2012-08-21 16:17:27 +0000 | [diff] [blame] | 166 | const int xfer_max_sz = 0xff00; |
| 167 | const int desc_count = DIV_ROUND_UP(length, xfer_max_sz) + 1; |
Marek Vasut | 2c43214 | 2012-08-21 16:17:27 +0000 | [diff] [blame] | 168 | struct mxs_dma_desc *dp; |
| 169 | uint32_t ctrl0; |
Marek Vasut | 7c5e6f7 | 2012-07-09 00:48:33 +0000 | [diff] [blame] | 170 | uint32_t cache_data_count; |
Marek Vasut | 88d1555 | 2012-08-31 16:07:59 +0000 | [diff] [blame] | 171 | const uint32_t dstart = (uint32_t)data; |
Marek Vasut | 7c5e6f7 | 2012-07-09 00:48:33 +0000 | [diff] [blame] | 172 | int dmach; |
Marek Vasut | 2c43214 | 2012-08-21 16:17:27 +0000 | [diff] [blame] | 173 | int tl; |
Marek Vasut | e9f7eaf | 2012-08-31 16:08:00 +0000 | [diff] [blame] | 174 | int ret = 0; |
Marek Vasut | 7c5e6f7 | 2012-07-09 00:48:33 +0000 | [diff] [blame] | 175 | |
Marek Vasut | c96e78c | 2013-02-23 02:42:59 +0000 | [diff] [blame] | 176 | #if defined(CONFIG_MX23) |
| 177 | const int mxs_spi_pio_words = 1; |
| 178 | #elif defined(CONFIG_MX28) |
| 179 | const int mxs_spi_pio_words = 4; |
| 180 | #endif |
| 181 | |
Marek Vasut | 2c43214 | 2012-08-21 16:17:27 +0000 | [diff] [blame] | 182 | ALLOC_CACHE_ALIGN_BUFFER(struct mxs_dma_desc, desc, desc_count); |
| 183 | |
| 184 | memset(desc, 0, sizeof(struct mxs_dma_desc) * desc_count); |
| 185 | |
| 186 | ctrl0 = readl(&ssp_regs->hw_ssp_ctrl0); |
| 187 | ctrl0 |= SSP_CTRL0_DATA_XFER; |
Marek Vasut | 7c5e6f7 | 2012-07-09 00:48:33 +0000 | [diff] [blame] | 188 | |
| 189 | if (flags & SPI_XFER_BEGIN) |
| 190 | ctrl0 |= SSP_CTRL0_LOCK_CS; |
Marek Vasut | 7c5e6f7 | 2012-07-09 00:48:33 +0000 | [diff] [blame] | 191 | if (!write) |
| 192 | ctrl0 |= SSP_CTRL0_READ; |
| 193 | |
Marek Vasut | 7c5e6f7 | 2012-07-09 00:48:33 +0000 | [diff] [blame] | 194 | if (length % ARCH_DMA_MINALIGN) |
| 195 | cache_data_count = roundup(length, ARCH_DMA_MINALIGN); |
| 196 | else |
| 197 | cache_data_count = length; |
| 198 | |
Marek Vasut | 88d1555 | 2012-08-31 16:07:59 +0000 | [diff] [blame] | 199 | /* Flush data to DRAM so DMA can pick them up */ |
Marek Vasut | 2c43214 | 2012-08-21 16:17:27 +0000 | [diff] [blame] | 200 | if (write) |
Marek Vasut | 88d1555 | 2012-08-31 16:07:59 +0000 | [diff] [blame] | 201 | flush_dcache_range(dstart, dstart + cache_data_count); |
| 202 | |
| 203 | /* Invalidate the area, so no writeback into the RAM races with DMA */ |
| 204 | invalidate_dcache_range(dstart, dstart + cache_data_count); |
Marek Vasut | 7c5e6f7 | 2012-07-09 00:48:33 +0000 | [diff] [blame] | 205 | |
Lukasz Majewski | d99b018 | 2019-06-19 17:31:07 +0200 | [diff] [blame] | 206 | #if !CONFIG_IS_ENABLED(DM_SPI) |
Marek Vasut | 7c5e6f7 | 2012-07-09 00:48:33 +0000 | [diff] [blame] | 207 | dmach = MXS_DMA_CHANNEL_AHB_APBH_SSP0 + slave->slave.bus; |
Lukasz Majewski | d99b018 | 2019-06-19 17:31:07 +0200 | [diff] [blame] | 208 | #else |
| 209 | dmach = priv->dma_channel; |
| 210 | #endif |
Marek Vasut | 2c43214 | 2012-08-21 16:17:27 +0000 | [diff] [blame] | 211 | |
| 212 | dp = desc; |
| 213 | while (length) { |
| 214 | dp->address = (dma_addr_t)dp; |
| 215 | dp->cmd.address = (dma_addr_t)data; |
| 216 | |
| 217 | /* |
| 218 | * This is correct, even though it does indeed look insane. |
| 219 | * I hereby have to, wholeheartedly, thank Freescale Inc., |
| 220 | * for always inventing insane hardware and keeping me busy |
| 221 | * and employed ;-) |
| 222 | */ |
| 223 | if (write) |
| 224 | dp->cmd.data = MXS_DMA_DESC_COMMAND_DMA_READ; |
| 225 | else |
| 226 | dp->cmd.data = MXS_DMA_DESC_COMMAND_DMA_WRITE; |
| 227 | |
| 228 | /* |
| 229 | * The DMA controller can transfer large chunks (64kB) at |
| 230 | * time by setting the transfer length to 0. Setting tl to |
| 231 | * 0x10000 will overflow below and make .data contain 0. |
| 232 | * Otherwise, 0xff00 is the transfer maximum. |
| 233 | */ |
| 234 | if (length >= 0x10000) |
| 235 | tl = 0x10000; |
| 236 | else |
| 237 | tl = min(length, xfer_max_sz); |
| 238 | |
| 239 | dp->cmd.data |= |
Marek Vasut | e9f7eaf | 2012-08-31 16:08:00 +0000 | [diff] [blame] | 240 | ((tl & 0xffff) << MXS_DMA_DESC_BYTES_OFFSET) | |
Marek Vasut | c96e78c | 2013-02-23 02:42:59 +0000 | [diff] [blame] | 241 | (mxs_spi_pio_words << MXS_DMA_DESC_PIO_WORDS_OFFSET) | |
Marek Vasut | 2c43214 | 2012-08-21 16:17:27 +0000 | [diff] [blame] | 242 | MXS_DMA_DESC_HALT_ON_TERMINATE | |
| 243 | MXS_DMA_DESC_TERMINATE_FLUSH; |
Marek Vasut | 2c43214 | 2012-08-21 16:17:27 +0000 | [diff] [blame] | 244 | |
| 245 | data += tl; |
| 246 | length -= tl; |
| 247 | |
Marek Vasut | e9f7eaf | 2012-08-31 16:08:00 +0000 | [diff] [blame] | 248 | if (!length) { |
| 249 | dp->cmd.data |= MXS_DMA_DESC_IRQ | MXS_DMA_DESC_DEC_SEM; |
| 250 | |
| 251 | if (flags & SPI_XFER_END) { |
| 252 | ctrl0 &= ~SSP_CTRL0_LOCK_CS; |
| 253 | ctrl0 |= SSP_CTRL0_IGNORE_CRC; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /* |
Marek Vasut | c96e78c | 2013-02-23 02:42:59 +0000 | [diff] [blame] | 258 | * Write CTRL0, CMD0, CMD1 and XFER_SIZE registers in |
| 259 | * case of MX28, write only CTRL0 in case of MX23 due |
| 260 | * to the difference in register layout. It is utterly |
Marek Vasut | e9f7eaf | 2012-08-31 16:08:00 +0000 | [diff] [blame] | 261 | * essential that the XFER_SIZE register is written on |
| 262 | * a per-descriptor basis with the same size as is the |
| 263 | * descriptor! |
| 264 | */ |
| 265 | dp->cmd.pio_words[0] = ctrl0; |
Marek Vasut | c96e78c | 2013-02-23 02:42:59 +0000 | [diff] [blame] | 266 | #ifdef CONFIG_MX28 |
Marek Vasut | e9f7eaf | 2012-08-31 16:08:00 +0000 | [diff] [blame] | 267 | dp->cmd.pio_words[1] = 0; |
| 268 | dp->cmd.pio_words[2] = 0; |
| 269 | dp->cmd.pio_words[3] = tl; |
Marek Vasut | c96e78c | 2013-02-23 02:42:59 +0000 | [diff] [blame] | 270 | #endif |
Marek Vasut | e9f7eaf | 2012-08-31 16:08:00 +0000 | [diff] [blame] | 271 | |
Marek Vasut | 2c43214 | 2012-08-21 16:17:27 +0000 | [diff] [blame] | 272 | mxs_dma_desc_append(dmach, dp); |
| 273 | |
| 274 | dp++; |
| 275 | } |
| 276 | |
Marek Vasut | 7c5e6f7 | 2012-07-09 00:48:33 +0000 | [diff] [blame] | 277 | if (mxs_dma_go(dmach)) |
Marek Vasut | e9f7eaf | 2012-08-31 16:08:00 +0000 | [diff] [blame] | 278 | ret = -EINVAL; |
Marek Vasut | 7c5e6f7 | 2012-07-09 00:48:33 +0000 | [diff] [blame] | 279 | |
| 280 | /* The data arrived into DRAM, invalidate cache over them */ |
Marek Vasut | 88d1555 | 2012-08-31 16:07:59 +0000 | [diff] [blame] | 281 | if (!write) |
| 282 | invalidate_dcache_range(dstart, dstart + cache_data_count); |
Marek Vasut | 7c5e6f7 | 2012-07-09 00:48:33 +0000 | [diff] [blame] | 283 | |
Marek Vasut | e9f7eaf | 2012-08-31 16:08:00 +0000 | [diff] [blame] | 284 | return ret; |
Marek Vasut | 7c5e6f7 | 2012-07-09 00:48:33 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Lukasz Majewski | d99b018 | 2019-06-19 17:31:07 +0200 | [diff] [blame] | 287 | #if !CONFIG_IS_ENABLED(DM_SPI) |
Marek Vasut | ccd4d5a | 2012-07-09 00:48:32 +0000 | [diff] [blame] | 288 | int spi_xfer(struct spi_slave *slave, unsigned int bitlen, |
| 289 | const void *dout, void *din, unsigned long flags) |
| 290 | { |
| 291 | struct mxs_spi_slave *mxs_slave = to_mxs_slave(slave); |
Otavio Salvador | 9c47114 | 2012-08-05 09:05:31 +0000 | [diff] [blame] | 292 | struct mxs_ssp_regs *ssp_regs = mxs_slave->regs; |
Lukasz Majewski | d99b018 | 2019-06-19 17:31:07 +0200 | [diff] [blame] | 293 | #else |
| 294 | int mxs_spi_xfer(struct udevice *dev, unsigned int bitlen, |
| 295 | const void *dout, void *din, unsigned long flags) |
| 296 | { |
| 297 | struct udevice *bus = dev_get_parent(dev); |
| 298 | struct mxs_spi_priv *priv = dev_get_priv(bus); |
| 299 | struct mxs_ssp_regs *ssp_regs = priv->regs; |
| 300 | #endif |
Marek Vasut | ccd4d5a | 2012-07-09 00:48:32 +0000 | [diff] [blame] | 301 | int len = bitlen / 8; |
| 302 | char dummy; |
| 303 | int write = 0; |
| 304 | char *data = NULL; |
Marek Vasut | 7c5e6f7 | 2012-07-09 00:48:33 +0000 | [diff] [blame] | 305 | int dma = 1; |
Marek Vasut | 7c5e6f7 | 2012-07-09 00:48:33 +0000 | [diff] [blame] | 306 | |
Marek Vasut | ccd4d5a | 2012-07-09 00:48:32 +0000 | [diff] [blame] | 307 | if (bitlen == 0) { |
| 308 | if (flags & SPI_XFER_END) { |
| 309 | din = (void *)&dummy; |
| 310 | len = 1; |
| 311 | } else |
| 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | /* Half-duplex only */ |
| 316 | if (din && dout) |
| 317 | return -EINVAL; |
| 318 | /* No data */ |
| 319 | if (!din && !dout) |
| 320 | return 0; |
| 321 | |
| 322 | if (dout) { |
| 323 | data = (char *)dout; |
| 324 | write = 1; |
| 325 | } else if (din) { |
| 326 | data = (char *)din; |
| 327 | write = 0; |
| 328 | } |
| 329 | |
Marek Vasut | 7c5e6f7 | 2012-07-09 00:48:33 +0000 | [diff] [blame] | 330 | /* |
| 331 | * Check for alignment, if the buffer is aligned, do DMA transfer, |
| 332 | * PIO otherwise. This is a temporary workaround until proper bounce |
| 333 | * buffer is in place. |
| 334 | */ |
| 335 | if (dma) { |
| 336 | if (((uint32_t)data) & (ARCH_DMA_MINALIGN - 1)) |
| 337 | dma = 0; |
| 338 | if (((uint32_t)len) & (ARCH_DMA_MINALIGN - 1)) |
| 339 | dma = 0; |
| 340 | } |
| 341 | |
| 342 | if (!dma || (len < MXSSSP_SMALL_TRANSFER)) { |
| 343 | writel(SSP_CTRL1_DMA_ENABLE, &ssp_regs->hw_ssp_ctrl1_clr); |
Lukasz Majewski | d99b018 | 2019-06-19 17:31:07 +0200 | [diff] [blame] | 344 | #if !CONFIG_IS_ENABLED(DM_SPI) |
Marek Vasut | 7c5e6f7 | 2012-07-09 00:48:33 +0000 | [diff] [blame] | 345 | return mxs_spi_xfer_pio(mxs_slave, data, len, write, flags); |
Lukasz Majewski | d99b018 | 2019-06-19 17:31:07 +0200 | [diff] [blame] | 346 | #else |
| 347 | return mxs_spi_xfer_pio(priv, data, len, write, flags); |
| 348 | #endif |
Marek Vasut | 7c5e6f7 | 2012-07-09 00:48:33 +0000 | [diff] [blame] | 349 | } else { |
| 350 | writel(SSP_CTRL1_DMA_ENABLE, &ssp_regs->hw_ssp_ctrl1_set); |
Lukasz Majewski | d99b018 | 2019-06-19 17:31:07 +0200 | [diff] [blame] | 351 | #if !CONFIG_IS_ENABLED(DM_SPI) |
Marek Vasut | 7c5e6f7 | 2012-07-09 00:48:33 +0000 | [diff] [blame] | 352 | return mxs_spi_xfer_dma(mxs_slave, data, len, write, flags); |
Lukasz Majewski | d99b018 | 2019-06-19 17:31:07 +0200 | [diff] [blame] | 353 | #else |
| 354 | return mxs_spi_xfer_dma(priv, data, len, write, flags); |
| 355 | #endif |
Marek Vasut | 7c5e6f7 | 2012-07-09 00:48:33 +0000 | [diff] [blame] | 356 | } |
Marek Vasut | ec33de3 | 2011-11-08 23:18:14 +0000 | [diff] [blame] | 357 | } |
Lukasz Majewski | d99b018 | 2019-06-19 17:31:07 +0200 | [diff] [blame] | 358 | |
| 359 | #if !CONFIG_IS_ENABLED(DM_SPI) |
| 360 | int spi_cs_is_valid(unsigned int bus, unsigned int cs) |
| 361 | { |
| 362 | /* MXS SPI: 4 ports and 3 chip selects maximum */ |
| 363 | if (!mxs_ssp_bus_id_valid(bus) || cs > 2) |
| 364 | return 0; |
| 365 | else |
| 366 | return 1; |
| 367 | } |
| 368 | |
| 369 | struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs, |
| 370 | unsigned int max_hz, unsigned int mode) |
| 371 | { |
| 372 | struct mxs_spi_slave *mxs_slave; |
| 373 | |
| 374 | if (!spi_cs_is_valid(bus, cs)) { |
| 375 | printf("mxs_spi: invalid bus %d / chip select %d\n", bus, cs); |
| 376 | return NULL; |
| 377 | } |
| 378 | |
| 379 | mxs_slave = spi_alloc_slave(struct mxs_spi_slave, bus, cs); |
| 380 | if (!mxs_slave) |
| 381 | return NULL; |
| 382 | |
| 383 | if (mxs_dma_init_channel(MXS_DMA_CHANNEL_AHB_APBH_SSP0 + bus)) |
| 384 | goto err_init; |
| 385 | |
| 386 | mxs_slave->max_khz = max_hz / 1000; |
| 387 | mxs_slave->mode = mode; |
| 388 | mxs_slave->regs = mxs_ssp_regs_by_bus(bus); |
| 389 | |
| 390 | return &mxs_slave->slave; |
| 391 | |
| 392 | err_init: |
| 393 | free(mxs_slave); |
| 394 | return NULL; |
| 395 | } |
| 396 | |
| 397 | void spi_free_slave(struct spi_slave *slave) |
| 398 | { |
| 399 | struct mxs_spi_slave *mxs_slave = to_mxs_slave(slave); |
| 400 | |
| 401 | free(mxs_slave); |
| 402 | } |
| 403 | |
| 404 | int spi_claim_bus(struct spi_slave *slave) |
| 405 | { |
| 406 | struct mxs_spi_slave *mxs_slave = to_mxs_slave(slave); |
| 407 | struct mxs_ssp_regs *ssp_regs = mxs_slave->regs; |
| 408 | u32 reg = 0; |
| 409 | |
| 410 | mxs_reset_block(&ssp_regs->hw_ssp_ctrl0_reg); |
| 411 | |
| 412 | writel((slave->cs << MXS_SSP_CHIPSELECT_SHIFT) | |
| 413 | SSP_CTRL0_BUS_WIDTH_ONE_BIT, |
| 414 | &ssp_regs->hw_ssp_ctrl0); |
| 415 | |
| 416 | reg = SSP_CTRL1_SSP_MODE_SPI | SSP_CTRL1_WORD_LENGTH_EIGHT_BITS; |
| 417 | reg |= (mxs_slave->mode & SPI_CPOL) ? SSP_CTRL1_POLARITY : 0; |
| 418 | reg |= (mxs_slave->mode & SPI_CPHA) ? SSP_CTRL1_PHASE : 0; |
| 419 | writel(reg, &ssp_regs->hw_ssp_ctrl1); |
| 420 | |
| 421 | writel(0, &ssp_regs->hw_ssp_cmd0); |
| 422 | |
| 423 | mxs_set_ssp_busclock(slave->bus, mxs_slave->max_khz); |
| 424 | |
| 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | void spi_release_bus(struct spi_slave *slave) |
| 429 | { |
| 430 | } |
| 431 | |
| 432 | #else /* CONFIG_DM_SPI */ |
| 433 | /* Base numbers of i.MX2[38] clk for ssp0 IP block */ |
| 434 | #define MXS_SSP_IMX23_CLKID_SSP0 33 |
| 435 | #define MXS_SSP_IMX28_CLKID_SSP0 46 |
| 436 | |
| 437 | static int mxs_spi_probe(struct udevice *bus) |
| 438 | { |
| 439 | struct mxs_spi_platdata *plat = dev_get_platdata(bus); |
| 440 | struct mxs_spi_priv *priv = dev_get_priv(bus); |
| 441 | int ret; |
| 442 | |
| 443 | debug("%s: probe\n", __func__); |
Lukasz Majewski | ec0c81f | 2019-09-05 09:54:58 +0200 | [diff] [blame] | 444 | |
| 445 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 446 | struct dtd_fsl_imx_spi *dtplat = &plat->dtplat; |
| 447 | struct phandle_1_arg *p1a = &dtplat->clocks[0]; |
| 448 | |
| 449 | priv->regs = (struct mxs_ssp_regs *)dtplat->reg[0]; |
| 450 | priv->dma_channel = dtplat->dmas[1]; |
| 451 | priv->clk_id = p1a->arg[0]; |
| 452 | priv->max_freq = dtplat->spi_max_frequency; |
| 453 | plat->num_cs = dtplat->num_cs; |
| 454 | |
| 455 | debug("OF_PLATDATA: regs: 0x%x max freq: %d clkid: %d\n", |
| 456 | (unsigned int)priv->regs, priv->max_freq, priv->clk_id); |
| 457 | #else |
Lukasz Majewski | d99b018 | 2019-06-19 17:31:07 +0200 | [diff] [blame] | 458 | priv->regs = (struct mxs_ssp_regs *)plat->base; |
| 459 | priv->max_freq = plat->frequency; |
| 460 | |
| 461 | priv->dma_channel = plat->dma_id; |
| 462 | priv->clk_id = plat->clk_id; |
Lukasz Majewski | ec0c81f | 2019-09-05 09:54:58 +0200 | [diff] [blame] | 463 | #endif |
Lukasz Majewski | d99b018 | 2019-06-19 17:31:07 +0200 | [diff] [blame] | 464 | |
Lukasz Majewski | c2050e1 | 2019-09-05 09:54:57 +0200 | [diff] [blame] | 465 | mxs_reset_block(&priv->regs->hw_ssp_ctrl0_reg); |
| 466 | |
Lukasz Majewski | d99b018 | 2019-06-19 17:31:07 +0200 | [diff] [blame] | 467 | ret = mxs_dma_init_channel(priv->dma_channel); |
| 468 | if (ret) { |
| 469 | printf("%s: DMA init channel error %d\n", __func__, ret); |
| 470 | return ret; |
| 471 | } |
| 472 | |
| 473 | return 0; |
| 474 | } |
| 475 | |
| 476 | static int mxs_spi_claim_bus(struct udevice *dev) |
| 477 | { |
| 478 | struct udevice *bus = dev_get_parent(dev); |
| 479 | struct mxs_spi_priv *priv = dev_get_priv(bus); |
| 480 | struct mxs_ssp_regs *ssp_regs = priv->regs; |
| 481 | int cs = spi_chip_select(dev); |
| 482 | |
| 483 | /* |
| 484 | * i.MX28 supports up to 3 CS (SSn0, SSn1, SSn2) |
| 485 | * To set them it uses following tuple (WAIT_FOR_IRQ,WAIT_FOR_CMD), |
| 486 | * where: |
| 487 | * |
| 488 | * WAIT_FOR_IRQ is bit 21 of HW_SSP_CTRL0 |
| 489 | * WAIT_FOR_CMD is bit 20 (#defined as MXS_SSP_CHIPSELECT_SHIFT here) of |
| 490 | * HW_SSP_CTRL0 |
| 491 | * SSn0 b00 |
| 492 | * SSn1 b01 |
| 493 | * SSn2 b10 (which require setting WAIT_FOR_IRQ) |
| 494 | * |
| 495 | * However, for now i.MX28 SPI driver will support up till 2 CSes |
| 496 | * (SSn0, and SSn1). |
| 497 | */ |
| 498 | |
| 499 | /* Ungate SSP clock and set active CS */ |
| 500 | clrsetbits_le32(&ssp_regs->hw_ssp_ctrl0, |
| 501 | BIT(MXS_SSP_CHIPSELECT_SHIFT) | |
| 502 | SSP_CTRL0_CLKGATE, (cs << MXS_SSP_CHIPSELECT_SHIFT)); |
| 503 | |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | static int mxs_spi_release_bus(struct udevice *dev) |
| 508 | { |
| 509 | struct udevice *bus = dev_get_parent(dev); |
| 510 | struct mxs_spi_priv *priv = dev_get_priv(bus); |
| 511 | struct mxs_ssp_regs *ssp_regs = priv->regs; |
| 512 | |
| 513 | /* Gate SSP clock */ |
| 514 | setbits_le32(&ssp_regs->hw_ssp_ctrl0, SSP_CTRL0_CLKGATE); |
| 515 | |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | static int mxs_spi_set_speed(struct udevice *bus, uint speed) |
| 520 | { |
| 521 | struct mxs_spi_priv *priv = dev_get_priv(bus); |
| 522 | #ifdef CONFIG_MX28 |
| 523 | int clkid = priv->clk_id - MXS_SSP_IMX28_CLKID_SSP0; |
| 524 | #else /* CONFIG_MX23 */ |
| 525 | int clkid = priv->clk_id - MXS_SSP_IMX23_CLKID_SSP0; |
| 526 | #endif |
| 527 | if (speed > priv->max_freq) |
| 528 | speed = priv->max_freq; |
| 529 | |
| 530 | debug("%s speed: %u [Hz] clkid: %d\n", __func__, speed, clkid); |
| 531 | mxs_set_ssp_busclock(clkid, speed / 1000); |
| 532 | |
| 533 | return 0; |
| 534 | } |
| 535 | |
| 536 | static int mxs_spi_set_mode(struct udevice *bus, uint mode) |
| 537 | { |
| 538 | struct mxs_spi_priv *priv = dev_get_priv(bus); |
| 539 | struct mxs_ssp_regs *ssp_regs = priv->regs; |
| 540 | u32 reg; |
| 541 | |
| 542 | priv->mode = mode; |
| 543 | debug("%s: mode 0x%x\n", __func__, mode); |
| 544 | |
| 545 | reg = SSP_CTRL1_SSP_MODE_SPI | SSP_CTRL1_WORD_LENGTH_EIGHT_BITS; |
| 546 | reg |= (priv->mode & SPI_CPOL) ? SSP_CTRL1_POLARITY : 0; |
| 547 | reg |= (priv->mode & SPI_CPHA) ? SSP_CTRL1_PHASE : 0; |
| 548 | writel(reg, &ssp_regs->hw_ssp_ctrl1); |
| 549 | |
| 550 | /* Single bit SPI support */ |
| 551 | writel(SSP_CTRL0_BUS_WIDTH_ONE_BIT, &ssp_regs->hw_ssp_ctrl0); |
| 552 | |
| 553 | return 0; |
| 554 | } |
| 555 | |
| 556 | static const struct dm_spi_ops mxs_spi_ops = { |
| 557 | .claim_bus = mxs_spi_claim_bus, |
| 558 | .release_bus = mxs_spi_release_bus, |
| 559 | .xfer = mxs_spi_xfer, |
| 560 | .set_speed = mxs_spi_set_speed, |
| 561 | .set_mode = mxs_spi_set_mode, |
| 562 | /* |
| 563 | * cs_info is not needed, since we require all chip selects to be |
| 564 | * in the device tree explicitly |
| 565 | */ |
| 566 | }; |
| 567 | |
| 568 | #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA) |
| 569 | static int mxs_ofdata_to_platdata(struct udevice *bus) |
| 570 | { |
| 571 | struct mxs_spi_platdata *plat = bus->platdata; |
| 572 | u32 prop[2]; |
| 573 | int ret; |
| 574 | |
| 575 | plat->base = dev_read_addr(bus); |
| 576 | plat->frequency = |
| 577 | dev_read_u32_default(bus, "spi-max-frequency", 40000000); |
| 578 | plat->num_cs = dev_read_u32_default(bus, "num-cs", 2); |
| 579 | |
| 580 | ret = dev_read_u32_array(bus, "dmas", prop, ARRAY_SIZE(prop)); |
| 581 | if (ret) { |
| 582 | printf("%s: Reading 'dmas' property failed!\n", __func__); |
| 583 | return ret; |
| 584 | } |
| 585 | plat->dma_id = prop[1]; |
| 586 | |
| 587 | ret = dev_read_u32_array(bus, "clocks", prop, ARRAY_SIZE(prop)); |
| 588 | if (ret) { |
| 589 | printf("%s: Reading 'clocks' property failed!\n", __func__); |
| 590 | return ret; |
| 591 | } |
| 592 | plat->clk_id = prop[1]; |
| 593 | |
| 594 | debug("%s: base=0x%x, max-frequency=%d num-cs=%d dma_id=%d clk_id=%d\n", |
| 595 | __func__, (uint)plat->base, plat->frequency, plat->num_cs, |
| 596 | plat->dma_id, plat->clk_id); |
| 597 | |
| 598 | return 0; |
| 599 | } |
Lukasz Majewski | d99b018 | 2019-06-19 17:31:07 +0200 | [diff] [blame] | 600 | |
| 601 | static const struct udevice_id mxs_spi_ids[] = { |
| 602 | { .compatible = "fsl,imx23-spi" }, |
| 603 | { .compatible = "fsl,imx28-spi" }, |
| 604 | { } |
| 605 | }; |
Lukasz Majewski | ec0c81f | 2019-09-05 09:54:58 +0200 | [diff] [blame] | 606 | #endif |
Lukasz Majewski | d99b018 | 2019-06-19 17:31:07 +0200 | [diff] [blame] | 607 | |
| 608 | U_BOOT_DRIVER(mxs_spi) = { |
Lukasz Majewski | ec0c81f | 2019-09-05 09:54:58 +0200 | [diff] [blame] | 609 | #ifdef CONFIG_MX28 |
| 610 | .name = "fsl_imx28_spi", |
| 611 | #else /* CONFIG_MX23 */ |
| 612 | .name = "fsl_imx23_spi", |
| 613 | #endif |
Lukasz Majewski | d99b018 | 2019-06-19 17:31:07 +0200 | [diff] [blame] | 614 | .id = UCLASS_SPI, |
| 615 | #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA) |
| 616 | .of_match = mxs_spi_ids, |
| 617 | .ofdata_to_platdata = mxs_ofdata_to_platdata, |
| 618 | #endif |
Lukasz Majewski | 3c50e01 | 2019-09-05 09:54:56 +0200 | [diff] [blame] | 619 | .platdata_auto_alloc_size = sizeof(struct mxs_spi_platdata), |
Lukasz Majewski | d99b018 | 2019-06-19 17:31:07 +0200 | [diff] [blame] | 620 | .ops = &mxs_spi_ops, |
| 621 | .priv_auto_alloc_size = sizeof(struct mxs_spi_priv), |
| 622 | .probe = mxs_spi_probe, |
| 623 | }; |
| 624 | #endif |