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