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