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