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