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