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