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