Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008, Guennadi Liakhovetski <lg@denx.de> |
| 3 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 4 | * SPDX-License-Identifier: GPL-2.0+ |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 8 | #include <malloc.h> |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 9 | #include <spi.h> |
Guennadi Liakhovetski | fc7a93c | 2009-02-13 09:26:40 +0100 | [diff] [blame] | 10 | #include <asm/errno.h> |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 11 | #include <asm/io.h> |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 12 | #include <asm/gpio.h> |
Stefano Babic | 8627111 | 2011-03-14 15:43:56 +0100 | [diff] [blame] | 13 | #include <asm/arch/imx-regs.h> |
| 14 | #include <asm/arch/clock.h> |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 15 | |
| 16 | #ifdef CONFIG_MX27 |
| 17 | /* i.MX27 has a completely wrong register layout and register definitions in the |
| 18 | * datasheet, the correct one is in the Freescale's Linux driver */ |
| 19 | |
Helmut Raiger | 61a58a1 | 2011-06-15 01:45:45 +0000 | [diff] [blame] | 20 | #error "i.MX27 CSPI not supported due to drastic differences in register definitions" \ |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 21 | "See linux mxc_spi driver from Freescale for details." |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 22 | #endif |
| 23 | |
Eric Nelson | 08c61a5 | 2012-01-31 07:52:03 +0000 | [diff] [blame] | 24 | static unsigned long spi_bases[] = { |
| 25 | MXC_SPI_BASE_ADDRESSES |
| 26 | }; |
| 27 | |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 28 | #define OUT MXC_GPIO_DIRECTION_OUT |
| 29 | |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 30 | #define reg_read readl |
| 31 | #define reg_write(a, v) writel(v, a) |
| 32 | |
Heiko Schocher | f659b57 | 2014-07-14 10:22:11 +0200 | [diff] [blame] | 33 | #if !defined(CONFIG_SYS_SPI_MXC_WAIT) |
| 34 | #define CONFIG_SYS_SPI_MXC_WAIT (CONFIG_SYS_HZ/100) /* 10 ms */ |
| 35 | #endif |
| 36 | |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 37 | struct mxc_spi_slave { |
| 38 | struct spi_slave slave; |
| 39 | unsigned long base; |
| 40 | u32 ctrl_reg; |
Eric Nelson | 08c61a5 | 2012-01-31 07:52:03 +0000 | [diff] [blame] | 41 | #if defined(MXC_ECSPI) |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 42 | u32 cfg_reg; |
| 43 | #endif |
Guennadi Liakhovetski | fc7a93c | 2009-02-13 09:26:40 +0100 | [diff] [blame] | 44 | int gpio; |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 45 | int ss_pol; |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 46 | }; |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 47 | |
| 48 | static inline struct mxc_spi_slave *to_mxc_spi_slave(struct spi_slave *slave) |
| 49 | { |
| 50 | return container_of(slave, struct mxc_spi_slave, slave); |
| 51 | } |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 52 | |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 53 | void spi_cs_activate(struct spi_slave *slave) |
| 54 | { |
| 55 | struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave); |
| 56 | if (mxcs->gpio > 0) |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 57 | gpio_set_value(mxcs->gpio, mxcs->ss_pol); |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void spi_cs_deactivate(struct spi_slave *slave) |
| 61 | { |
| 62 | struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave); |
| 63 | if (mxcs->gpio > 0) |
Stefano Babic | d8e0ca8 | 2011-08-21 10:45:44 +0200 | [diff] [blame] | 64 | gpio_set_value(mxcs->gpio, |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 65 | !(mxcs->ss_pol)); |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 66 | } |
| 67 | |
Anatolij Gustschin | afaa9f6 | 2011-01-19 22:46:32 +0000 | [diff] [blame] | 68 | u32 get_cspi_div(u32 div) |
| 69 | { |
| 70 | int i; |
| 71 | |
| 72 | for (i = 0; i < 8; i++) { |
| 73 | if (div <= (4 << i)) |
| 74 | return i; |
| 75 | } |
| 76 | return i; |
| 77 | } |
| 78 | |
Eric Nelson | 08c61a5 | 2012-01-31 07:52:03 +0000 | [diff] [blame] | 79 | #ifdef MXC_CSPI |
Stefano Babic | c9d59c7 | 2011-01-19 22:46:30 +0000 | [diff] [blame] | 80 | static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs, |
| 81 | unsigned int max_hz, unsigned int mode) |
| 82 | { |
| 83 | unsigned int ctrl_reg; |
Anatolij Gustschin | afaa9f6 | 2011-01-19 22:46:32 +0000 | [diff] [blame] | 84 | u32 clk_src; |
| 85 | u32 div; |
| 86 | |
| 87 | clk_src = mxc_get_clock(MXC_CSPI_CLK); |
| 88 | |
Benoît Thébaudeau | cd20040 | 2012-08-10 08:51:50 +0000 | [diff] [blame] | 89 | div = DIV_ROUND_UP(clk_src, max_hz); |
Anatolij Gustschin | afaa9f6 | 2011-01-19 22:46:32 +0000 | [diff] [blame] | 90 | div = get_cspi_div(div); |
| 91 | |
| 92 | debug("clk %d Hz, div %d, real clk %d Hz\n", |
| 93 | max_hz, div, clk_src / (4 << div)); |
Stefano Babic | c9d59c7 | 2011-01-19 22:46:30 +0000 | [diff] [blame] | 94 | |
| 95 | ctrl_reg = MXC_CSPICTRL_CHIPSELECT(cs) | |
| 96 | MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS) | |
Anatolij Gustschin | afaa9f6 | 2011-01-19 22:46:32 +0000 | [diff] [blame] | 97 | MXC_CSPICTRL_DATARATE(div) | |
Stefano Babic | c9d59c7 | 2011-01-19 22:46:30 +0000 | [diff] [blame] | 98 | MXC_CSPICTRL_EN | |
| 99 | #ifdef CONFIG_MX35 |
| 100 | MXC_CSPICTRL_SSCTL | |
| 101 | #endif |
| 102 | MXC_CSPICTRL_MODE; |
| 103 | |
| 104 | if (mode & SPI_CPHA) |
| 105 | ctrl_reg |= MXC_CSPICTRL_PHA; |
| 106 | if (mode & SPI_CPOL) |
| 107 | ctrl_reg |= MXC_CSPICTRL_POL; |
| 108 | if (mode & SPI_CS_HIGH) |
| 109 | ctrl_reg |= MXC_CSPICTRL_SSPOL; |
| 110 | mxcs->ctrl_reg = ctrl_reg; |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | #endif |
| 115 | |
Eric Nelson | 08c61a5 | 2012-01-31 07:52:03 +0000 | [diff] [blame] | 116 | #ifdef MXC_ECSPI |
Stefano Babic | c9d59c7 | 2011-01-19 22:46:30 +0000 | [diff] [blame] | 117 | static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs, |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 118 | unsigned int max_hz, unsigned int mode) |
| 119 | { |
| 120 | u32 clk_src = mxc_get_clock(MXC_CSPI_CLK); |
Dirk Behme | 9a30903 | 2013-05-11 07:25:54 +0200 | [diff] [blame] | 121 | s32 reg_ctrl, reg_config; |
Markus Niebel | 5d584cc | 2014-02-17 17:33:17 +0100 | [diff] [blame] | 122 | u32 ss_pol = 0, sclkpol = 0, sclkpha = 0, sclkctl = 0; |
| 123 | u32 pre_div = 0, post_div = 0; |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 124 | struct cspi_regs *regs = (struct cspi_regs *)mxcs->base; |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 125 | |
| 126 | if (max_hz == 0) { |
| 127 | printf("Error: desired clock is 0\n"); |
| 128 | return -1; |
| 129 | } |
| 130 | |
Fabio Estevam | 0f1411b | 2013-04-09 13:06:25 +0000 | [diff] [blame] | 131 | /* |
| 132 | * Reset SPI and set all CSs to master mode, if toggling |
| 133 | * between slave and master mode we might see a glitch |
| 134 | * on the clock line |
| 135 | */ |
| 136 | reg_ctrl = MXC_CSPICTRL_MODE_MASK; |
| 137 | reg_write(®s->ctrl, reg_ctrl); |
| 138 | reg_ctrl |= MXC_CSPICTRL_EN; |
| 139 | reg_write(®s->ctrl, reg_ctrl); |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 140 | |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 141 | if (clk_src > max_hz) { |
Dirk Behme | 9a30903 | 2013-05-11 07:25:54 +0200 | [diff] [blame] | 142 | pre_div = (clk_src - 1) / max_hz; |
| 143 | /* fls(1) = 1, fls(0x80000000) = 32, fls(16) = 5 */ |
| 144 | post_div = fls(pre_div); |
| 145 | if (post_div > 4) { |
| 146 | post_div -= 4; |
| 147 | if (post_div >= 16) { |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 148 | printf("Error: no divider for the freq: %d\n", |
| 149 | max_hz); |
| 150 | return -1; |
| 151 | } |
Dirk Behme | 9a30903 | 2013-05-11 07:25:54 +0200 | [diff] [blame] | 152 | pre_div >>= post_div; |
| 153 | } else { |
| 154 | post_div = 0; |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
| 158 | debug("pre_div = %d, post_div=%d\n", pre_div, post_div); |
| 159 | reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_SELCHAN(3)) | |
| 160 | MXC_CSPICTRL_SELCHAN(cs); |
| 161 | reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_PREDIV(0x0F)) | |
| 162 | MXC_CSPICTRL_PREDIV(pre_div); |
| 163 | reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_POSTDIV(0x0F)) | |
| 164 | MXC_CSPICTRL_POSTDIV(post_div); |
| 165 | |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 166 | /* We need to disable SPI before changing registers */ |
| 167 | reg_ctrl &= ~MXC_CSPICTRL_EN; |
| 168 | |
| 169 | if (mode & SPI_CS_HIGH) |
| 170 | ss_pol = 1; |
| 171 | |
Markus Niebel | 5d584cc | 2014-02-17 17:33:17 +0100 | [diff] [blame] | 172 | if (mode & SPI_CPOL) { |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 173 | sclkpol = 1; |
Markus Niebel | 5d584cc | 2014-02-17 17:33:17 +0100 | [diff] [blame] | 174 | sclkctl = 1; |
| 175 | } |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 176 | |
| 177 | if (mode & SPI_CPHA) |
| 178 | sclkpha = 1; |
| 179 | |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 180 | reg_config = reg_read(®s->cfg); |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 181 | |
| 182 | /* |
| 183 | * Configuration register setup |
Stefano Babic | c9d59c7 | 2011-01-19 22:46:30 +0000 | [diff] [blame] | 184 | * The MX51 supports different setup for each SS |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 185 | */ |
| 186 | reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_SSPOL))) | |
| 187 | (ss_pol << (cs + MXC_CSPICON_SSPOL)); |
| 188 | reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_POL))) | |
| 189 | (sclkpol << (cs + MXC_CSPICON_POL)); |
Markus Niebel | 5d584cc | 2014-02-17 17:33:17 +0100 | [diff] [blame] | 190 | reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_CTL))) | |
| 191 | (sclkctl << (cs + MXC_CSPICON_CTL)); |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 192 | reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_PHA))) | |
| 193 | (sclkpha << (cs + MXC_CSPICON_PHA)); |
| 194 | |
| 195 | debug("reg_ctrl = 0x%x\n", reg_ctrl); |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 196 | reg_write(®s->ctrl, reg_ctrl); |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 197 | debug("reg_config = 0x%x\n", reg_config); |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 198 | reg_write(®s->cfg, reg_config); |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 199 | |
| 200 | /* save config register and control register */ |
| 201 | mxcs->ctrl_reg = reg_ctrl; |
| 202 | mxcs->cfg_reg = reg_config; |
| 203 | |
| 204 | /* clear interrupt reg */ |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 205 | reg_write(®s->intr, 0); |
| 206 | reg_write(®s->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF); |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 207 | |
| 208 | return 0; |
| 209 | } |
| 210 | #endif |
| 211 | |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 212 | int spi_xchg_single(struct spi_slave *slave, unsigned int bitlen, |
| 213 | const u8 *dout, u8 *din, unsigned long flags) |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 214 | { |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 215 | struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave); |
Axel Lin | 9675fed | 2013-06-14 21:13:32 +0800 | [diff] [blame] | 216 | int nbytes = DIV_ROUND_UP(bitlen, 8); |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 217 | u32 data, cnt, i; |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 218 | struct cspi_regs *regs = (struct cspi_regs *)mxcs->base; |
Heiko Schocher | f659b57 | 2014-07-14 10:22:11 +0200 | [diff] [blame] | 219 | u32 ts; |
| 220 | int status; |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 221 | |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 222 | debug("%s: bitlen %d dout 0x%x din 0x%x\n", |
| 223 | __func__, bitlen, (u32)dout, (u32)din); |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 224 | |
| 225 | mxcs->ctrl_reg = (mxcs->ctrl_reg & |
| 226 | ~MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS)) | |
Guennadi Liakhovetski | f9b6a15 | 2009-02-07 00:09:12 +0100 | [diff] [blame] | 227 | MXC_CSPICTRL_BITCOUNT(bitlen - 1); |
| 228 | |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 229 | reg_write(®s->ctrl, mxcs->ctrl_reg | MXC_CSPICTRL_EN); |
Eric Nelson | 08c61a5 | 2012-01-31 07:52:03 +0000 | [diff] [blame] | 230 | #ifdef MXC_ECSPI |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 231 | reg_write(®s->cfg, mxcs->cfg_reg); |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 232 | #endif |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 233 | |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 234 | /* Clear interrupt register */ |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 235 | reg_write(®s->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF); |
Guennadi Liakhovetski | fc7a93c | 2009-02-13 09:26:40 +0100 | [diff] [blame] | 236 | |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 237 | /* |
| 238 | * The SPI controller works only with words, |
| 239 | * check if less than a word is sent. |
| 240 | * Access to the FIFO is only 32 bit |
| 241 | */ |
| 242 | if (bitlen % 32) { |
| 243 | data = 0; |
| 244 | cnt = (bitlen % 32) / 8; |
| 245 | if (dout) { |
| 246 | for (i = 0; i < cnt; i++) { |
| 247 | data = (data << 8) | (*dout++ & 0xFF); |
| 248 | } |
| 249 | } |
| 250 | debug("Sending SPI 0x%x\n", data); |
| 251 | |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 252 | reg_write(®s->txdata, data); |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 253 | nbytes -= cnt; |
| 254 | } |
| 255 | |
| 256 | data = 0; |
| 257 | |
| 258 | while (nbytes > 0) { |
| 259 | data = 0; |
| 260 | if (dout) { |
| 261 | /* Buffer is not 32-bit aligned */ |
| 262 | if ((unsigned long)dout & 0x03) { |
| 263 | data = 0; |
Anatolij Gustschin | dff0109 | 2011-01-20 07:53:06 +0000 | [diff] [blame] | 264 | for (i = 0; i < 4; i++) |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 265 | data = (data << 8) | (*dout++ & 0xFF); |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 266 | } else { |
| 267 | data = *(u32 *)dout; |
| 268 | data = cpu_to_be32(data); |
Timo Herbrecher | 6d5ce1b | 2013-10-16 00:05:09 +0530 | [diff] [blame] | 269 | dout += 4; |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 270 | } |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 271 | } |
| 272 | debug("Sending SPI 0x%x\n", data); |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 273 | reg_write(®s->txdata, data); |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 274 | nbytes -= 4; |
| 275 | } |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 276 | |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 277 | /* FIFO is written, now starts the transfer setting the XCH bit */ |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 278 | reg_write(®s->ctrl, mxcs->ctrl_reg | |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 279 | MXC_CSPICTRL_EN | MXC_CSPICTRL_XCH); |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 280 | |
Heiko Schocher | f659b57 | 2014-07-14 10:22:11 +0200 | [diff] [blame] | 281 | ts = get_timer(0); |
| 282 | status = reg_read(®s->stat); |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 283 | /* Wait until the TC (Transfer completed) bit is set */ |
Heiko Schocher | f659b57 | 2014-07-14 10:22:11 +0200 | [diff] [blame] | 284 | while ((status & MXC_CSPICTRL_TC) == 0) { |
| 285 | if (get_timer(ts) > CONFIG_SYS_SPI_MXC_WAIT) { |
| 286 | printf("spi_xchg_single: Timeout!\n"); |
| 287 | return -1; |
| 288 | } |
| 289 | status = reg_read(®s->stat); |
| 290 | } |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 291 | |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 292 | /* Transfer completed, clear any pending request */ |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 293 | reg_write(®s->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF); |
Guennadi Liakhovetski | fc7a93c | 2009-02-13 09:26:40 +0100 | [diff] [blame] | 294 | |
Axel Lin | 9675fed | 2013-06-14 21:13:32 +0800 | [diff] [blame] | 295 | nbytes = DIV_ROUND_UP(bitlen, 8); |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 296 | |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 297 | cnt = nbytes % 32; |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 298 | |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 299 | if (bitlen % 32) { |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 300 | data = reg_read(®s->rxdata); |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 301 | cnt = (bitlen % 32) / 8; |
Anatolij Gustschin | dff0109 | 2011-01-20 07:53:06 +0000 | [diff] [blame] | 302 | data = cpu_to_be32(data) >> ((sizeof(data) - cnt) * 8); |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 303 | debug("SPI Rx unaligned: 0x%x\n", data); |
| 304 | if (din) { |
Anatolij Gustschin | dff0109 | 2011-01-20 07:53:06 +0000 | [diff] [blame] | 305 | memcpy(din, &data, cnt); |
| 306 | din += cnt; |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 307 | } |
| 308 | nbytes -= cnt; |
| 309 | } |
| 310 | |
| 311 | while (nbytes > 0) { |
| 312 | u32 tmp; |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 313 | tmp = reg_read(®s->rxdata); |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 314 | data = cpu_to_be32(tmp); |
| 315 | debug("SPI Rx: 0x%x 0x%x\n", tmp, data); |
| 316 | cnt = min(nbytes, sizeof(data)); |
| 317 | if (din) { |
| 318 | memcpy(din, &data, cnt); |
| 319 | din += cnt; |
| 320 | } |
| 321 | nbytes -= cnt; |
| 322 | } |
| 323 | |
| 324 | return 0; |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 325 | |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 326 | } |
| 327 | |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 328 | int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout, |
| 329 | void *din, unsigned long flags) |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 330 | { |
Axel Lin | 9675fed | 2013-06-14 21:13:32 +0800 | [diff] [blame] | 331 | int n_bytes = DIV_ROUND_UP(bitlen, 8); |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 332 | int n_bits; |
| 333 | int ret; |
| 334 | u32 blk_size; |
| 335 | u8 *p_outbuf = (u8 *)dout; |
| 336 | u8 *p_inbuf = (u8 *)din; |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 337 | |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 338 | if (!slave) |
| 339 | return -1; |
| 340 | |
| 341 | if (flags & SPI_XFER_BEGIN) |
| 342 | spi_cs_activate(slave); |
| 343 | |
| 344 | while (n_bytes > 0) { |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 345 | if (n_bytes < MAX_SPI_BYTES) |
| 346 | blk_size = n_bytes; |
| 347 | else |
| 348 | blk_size = MAX_SPI_BYTES; |
| 349 | |
| 350 | n_bits = blk_size * 8; |
| 351 | |
| 352 | ret = spi_xchg_single(slave, n_bits, p_outbuf, p_inbuf, 0); |
| 353 | |
| 354 | if (ret) |
| 355 | return ret; |
| 356 | if (dout) |
| 357 | p_outbuf += blk_size; |
| 358 | if (din) |
| 359 | p_inbuf += blk_size; |
| 360 | n_bytes -= blk_size; |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 361 | } |
| 362 | |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 363 | if (flags & SPI_XFER_END) { |
| 364 | spi_cs_deactivate(slave); |
Guennadi Liakhovetski | f9b6a15 | 2009-02-07 00:09:12 +0100 | [diff] [blame] | 365 | } |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 366 | |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | void spi_init(void) |
| 371 | { |
| 372 | } |
| 373 | |
Guennadi Liakhovetski | fc7a93c | 2009-02-13 09:26:40 +0100 | [diff] [blame] | 374 | static int decode_cs(struct mxc_spi_slave *mxcs, unsigned int cs) |
| 375 | { |
| 376 | int ret; |
| 377 | |
| 378 | /* |
| 379 | * Some SPI devices require active chip-select over multiple |
| 380 | * transactions, we achieve this using a GPIO. Still, the SPI |
| 381 | * controller has to be configured to use one of its own chipselects. |
| 382 | * To use this feature you have to call spi_setup_slave() with |
| 383 | * cs = internal_cs | (gpio << 8), and you have to use some unused |
| 384 | * on this SPI controller cs between 0 and 3. |
| 385 | */ |
| 386 | if (cs > 3) { |
| 387 | mxcs->gpio = cs >> 8; |
| 388 | cs &= 3; |
Fabio Estevam | de5bf02 | 2012-11-15 11:23:23 +0000 | [diff] [blame] | 389 | ret = gpio_direction_output(mxcs->gpio, !(mxcs->ss_pol)); |
Guennadi Liakhovetski | fc7a93c | 2009-02-13 09:26:40 +0100 | [diff] [blame] | 390 | if (ret) { |
| 391 | printf("mxc_spi: cannot setup gpio %d\n", mxcs->gpio); |
| 392 | return -EINVAL; |
| 393 | } |
| 394 | } else { |
| 395 | mxcs->gpio = -1; |
| 396 | } |
| 397 | |
| 398 | return cs; |
| 399 | } |
| 400 | |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 401 | struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs, |
| 402 | unsigned int max_hz, unsigned int mode) |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 403 | { |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 404 | struct mxc_spi_slave *mxcs; |
Guennadi Liakhovetski | fc7a93c | 2009-02-13 09:26:40 +0100 | [diff] [blame] | 405 | int ret; |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 406 | |
Guennadi Liakhovetski | fc7a93c | 2009-02-13 09:26:40 +0100 | [diff] [blame] | 407 | if (bus >= ARRAY_SIZE(spi_bases)) |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 408 | return NULL; |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 409 | |
Simon Glass | d3504fe | 2013-03-18 19:23:40 +0000 | [diff] [blame] | 410 | mxcs = spi_alloc_slave(struct mxc_spi_slave, bus, cs); |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 411 | if (!mxcs) { |
| 412 | puts("mxc_spi: SPI Slave not allocated !\n"); |
Guennadi Liakhovetski | fc7a93c | 2009-02-13 09:26:40 +0100 | [diff] [blame] | 413 | return NULL; |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 414 | } |
Guennadi Liakhovetski | fc7a93c | 2009-02-13 09:26:40 +0100 | [diff] [blame] | 415 | |
Fabio Estevam | de5bf02 | 2012-11-15 11:23:23 +0000 | [diff] [blame] | 416 | mxcs->ss_pol = (mode & SPI_CS_HIGH) ? 1 : 0; |
| 417 | |
Guennadi Liakhovetski | fc7a93c | 2009-02-13 09:26:40 +0100 | [diff] [blame] | 418 | ret = decode_cs(mxcs, cs); |
| 419 | if (ret < 0) { |
| 420 | free(mxcs); |
| 421 | return NULL; |
| 422 | } |
| 423 | |
| 424 | cs = ret; |
| 425 | |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 426 | mxcs->base = spi_bases[bus]; |
| 427 | |
Stefano Babic | c9d59c7 | 2011-01-19 22:46:30 +0000 | [diff] [blame] | 428 | ret = spi_cfg_mxc(mxcs, cs, max_hz, mode); |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 429 | if (ret) { |
| 430 | printf("mxc_spi: cannot setup SPI controller\n"); |
| 431 | free(mxcs); |
| 432 | return NULL; |
| 433 | } |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 434 | return &mxcs->slave; |
| 435 | } |
| 436 | |
| 437 | void spi_free_slave(struct spi_slave *slave) |
| 438 | { |
Guennadi Liakhovetski | f9b6a15 | 2009-02-07 00:09:12 +0100 | [diff] [blame] | 439 | struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave); |
| 440 | |
| 441 | free(mxcs); |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | int spi_claim_bus(struct spi_slave *slave) |
| 445 | { |
| 446 | struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave); |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 447 | struct cspi_regs *regs = (struct cspi_regs *)mxcs->base; |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 448 | |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 449 | reg_write(®s->rxdata, 1); |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 450 | udelay(1); |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 451 | reg_write(®s->ctrl, mxcs->ctrl_reg); |
| 452 | reg_write(®s->period, MXC_CSPIPERIOD_32KHZ); |
| 453 | reg_write(®s->intr, 0); |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 454 | |
| 455 | return 0; |
| 456 | } |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 457 | |
| 458 | void spi_release_bus(struct spi_slave *slave) |
| 459 | { |
| 460 | /* TODO: Shut the controller down */ |
| 461 | } |