Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008, Guennadi Liakhovetski <lg@denx.de> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License as |
| 6 | * published by the Free Software Foundation; either version 2 of |
| 7 | * the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 17 | * MA 02111-1307 USA |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <common.h> |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 22 | #include <malloc.h> |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 23 | #include <spi.h> |
Guennadi Liakhovetski | fc7a93c | 2009-02-13 09:26:40 +0100 | [diff] [blame] | 24 | #include <asm/errno.h> |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 25 | #include <asm/io.h> |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 26 | #include <mxc_gpio.h> |
Stefano Babic | 8627111 | 2011-03-14 15:43:56 +0100 | [diff] [blame] | 27 | #include <asm/arch/imx-regs.h> |
| 28 | #include <asm/arch/clock.h> |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 29 | |
| 30 | #ifdef CONFIG_MX27 |
| 31 | /* i.MX27 has a completely wrong register layout and register definitions in the |
| 32 | * datasheet, the correct one is in the Freescale's Linux driver */ |
| 33 | |
Helmut Raiger | 61a58a1 | 2011-06-15 01:45:45 +0000 | [diff] [blame] | 34 | #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] | 35 | "See linux mxc_spi driver from Freescale for details." |
| 36 | |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 37 | #elif defined(CONFIG_MX31) |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 38 | |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 39 | #define MXC_CSPICTRL_EN (1 << 0) |
| 40 | #define MXC_CSPICTRL_MODE (1 << 1) |
| 41 | #define MXC_CSPICTRL_XCH (1 << 2) |
| 42 | #define MXC_CSPICTRL_SMC (1 << 3) |
| 43 | #define MXC_CSPICTRL_POL (1 << 4) |
| 44 | #define MXC_CSPICTRL_PHA (1 << 5) |
| 45 | #define MXC_CSPICTRL_SSCTL (1 << 6) |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 46 | #define MXC_CSPICTRL_SSPOL (1 << 7) |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 47 | #define MXC_CSPICTRL_CHIPSELECT(x) (((x) & 0x3) << 24) |
| 48 | #define MXC_CSPICTRL_BITCOUNT(x) (((x) & 0x1f) << 8) |
| 49 | #define MXC_CSPICTRL_DATARATE(x) (((x) & 0x7) << 16) |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 50 | #define MXC_CSPICTRL_TC (1 << 8) |
| 51 | #define MXC_CSPICTRL_RXOVF (1 << 6) |
| 52 | #define MXC_CSPICTRL_MAXBITS 0x1f |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 53 | |
| 54 | #define MXC_CSPIPERIOD_32KHZ (1 << 15) |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 55 | #define MAX_SPI_BYTES 4 |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 56 | |
| 57 | static unsigned long spi_bases[] = { |
| 58 | 0x43fa4000, |
| 59 | 0x50010000, |
| 60 | 0x53f84000, |
| 61 | }; |
| 62 | |
Stefano Babic | c9d59c7 | 2011-01-19 22:46:30 +0000 | [diff] [blame] | 63 | #define mxc_get_clock(x) mx31_get_ipg_clk() |
| 64 | |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 65 | #elif defined(CONFIG_MX51) |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 66 | |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 67 | #define MXC_CSPICTRL_EN (1 << 0) |
| 68 | #define MXC_CSPICTRL_MODE (1 << 1) |
| 69 | #define MXC_CSPICTRL_XCH (1 << 2) |
| 70 | #define MXC_CSPICTRL_CHIPSELECT(x) (((x) & 0x3) << 12) |
| 71 | #define MXC_CSPICTRL_BITCOUNT(x) (((x) & 0xfff) << 20) |
| 72 | #define MXC_CSPICTRL_PREDIV(x) (((x) & 0xF) << 12) |
| 73 | #define MXC_CSPICTRL_POSTDIV(x) (((x) & 0xF) << 8) |
| 74 | #define MXC_CSPICTRL_SELCHAN(x) (((x) & 0x3) << 18) |
| 75 | #define MXC_CSPICTRL_MAXBITS 0xfff |
| 76 | #define MXC_CSPICTRL_TC (1 << 7) |
| 77 | #define MXC_CSPICTRL_RXOVF (1 << 6) |
| 78 | |
| 79 | #define MXC_CSPIPERIOD_32KHZ (1 << 15) |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 80 | #define MAX_SPI_BYTES 32 |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 81 | |
| 82 | /* Bit position inside CTRL register to be associated with SS */ |
| 83 | #define MXC_CSPICTRL_CHAN 18 |
| 84 | |
| 85 | /* Bit position inside CON register to be associated with SS */ |
| 86 | #define MXC_CSPICON_POL 4 |
| 87 | #define MXC_CSPICON_PHA 0 |
| 88 | #define MXC_CSPICON_SSPOL 12 |
| 89 | |
| 90 | static unsigned long spi_bases[] = { |
| 91 | CSPI1_BASE_ADDR, |
| 92 | CSPI2_BASE_ADDR, |
| 93 | CSPI3_BASE_ADDR, |
| 94 | }; |
Stefano Babic | c9d59c7 | 2011-01-19 22:46:30 +0000 | [diff] [blame] | 95 | |
| 96 | #elif defined(CONFIG_MX35) |
| 97 | |
Stefano Babic | c9d59c7 | 2011-01-19 22:46:30 +0000 | [diff] [blame] | 98 | #define MXC_CSPICTRL_EN (1 << 0) |
| 99 | #define MXC_CSPICTRL_MODE (1 << 1) |
| 100 | #define MXC_CSPICTRL_XCH (1 << 2) |
| 101 | #define MXC_CSPICTRL_SMC (1 << 3) |
| 102 | #define MXC_CSPICTRL_POL (1 << 4) |
| 103 | #define MXC_CSPICTRL_PHA (1 << 5) |
| 104 | #define MXC_CSPICTRL_SSCTL (1 << 6) |
| 105 | #define MXC_CSPICTRL_SSPOL (1 << 7) |
| 106 | #define MXC_CSPICTRL_CHIPSELECT(x) (((x) & 0x3) << 12) |
| 107 | #define MXC_CSPICTRL_BITCOUNT(x) (((x) & 0xfff) << 20) |
| 108 | #define MXC_CSPICTRL_DATARATE(x) (((x) & 0x7) << 16) |
| 109 | #define MXC_CSPICTRL_TC (1 << 7) |
| 110 | #define MXC_CSPICTRL_RXOVF (1 << 6) |
| 111 | #define MXC_CSPICTRL_MAXBITS 0xfff |
| 112 | |
| 113 | #define MXC_CSPIPERIOD_32KHZ (1 << 15) |
| 114 | #define MAX_SPI_BYTES 4 |
| 115 | |
| 116 | static unsigned long spi_bases[] = { |
| 117 | 0x43fa4000, |
| 118 | 0x50010000, |
| 119 | }; |
| 120 | |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 121 | #else |
| 122 | #error "Unsupported architecture" |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 123 | #endif |
| 124 | |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 125 | #define OUT MXC_GPIO_DIRECTION_OUT |
| 126 | |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 127 | #define reg_read readl |
| 128 | #define reg_write(a, v) writel(v, a) |
| 129 | |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 130 | struct mxc_spi_slave { |
| 131 | struct spi_slave slave; |
| 132 | unsigned long base; |
| 133 | u32 ctrl_reg; |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 134 | #if defined(CONFIG_MX51) |
| 135 | u32 cfg_reg; |
| 136 | #endif |
Guennadi Liakhovetski | fc7a93c | 2009-02-13 09:26:40 +0100 | [diff] [blame] | 137 | int gpio; |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 138 | int ss_pol; |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 139 | }; |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 140 | |
| 141 | static inline struct mxc_spi_slave *to_mxc_spi_slave(struct spi_slave *slave) |
| 142 | { |
| 143 | return container_of(slave, struct mxc_spi_slave, slave); |
| 144 | } |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 145 | |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 146 | void spi_cs_activate(struct spi_slave *slave) |
| 147 | { |
| 148 | struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave); |
| 149 | if (mxcs->gpio > 0) |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 150 | mxc_gpio_set(mxcs->gpio, mxcs->ss_pol); |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | void spi_cs_deactivate(struct spi_slave *slave) |
| 154 | { |
| 155 | struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave); |
| 156 | if (mxcs->gpio > 0) |
| 157 | mxc_gpio_set(mxcs->gpio, |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 158 | !(mxcs->ss_pol)); |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 159 | } |
| 160 | |
Anatolij Gustschin | afaa9f6 | 2011-01-19 22:46:32 +0000 | [diff] [blame] | 161 | u32 get_cspi_div(u32 div) |
| 162 | { |
| 163 | int i; |
| 164 | |
| 165 | for (i = 0; i < 8; i++) { |
| 166 | if (div <= (4 << i)) |
| 167 | return i; |
| 168 | } |
| 169 | return i; |
| 170 | } |
| 171 | |
Stefano Babic | c9d59c7 | 2011-01-19 22:46:30 +0000 | [diff] [blame] | 172 | #if defined(CONFIG_MX31) || defined(CONFIG_MX35) |
| 173 | static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs, |
| 174 | unsigned int max_hz, unsigned int mode) |
| 175 | { |
| 176 | unsigned int ctrl_reg; |
Anatolij Gustschin | afaa9f6 | 2011-01-19 22:46:32 +0000 | [diff] [blame] | 177 | u32 clk_src; |
| 178 | u32 div; |
| 179 | |
| 180 | clk_src = mxc_get_clock(MXC_CSPI_CLK); |
| 181 | |
| 182 | div = clk_src / max_hz; |
| 183 | div = get_cspi_div(div); |
| 184 | |
| 185 | debug("clk %d Hz, div %d, real clk %d Hz\n", |
| 186 | max_hz, div, clk_src / (4 << div)); |
Stefano Babic | c9d59c7 | 2011-01-19 22:46:30 +0000 | [diff] [blame] | 187 | |
| 188 | ctrl_reg = MXC_CSPICTRL_CHIPSELECT(cs) | |
| 189 | MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS) | |
Anatolij Gustschin | afaa9f6 | 2011-01-19 22:46:32 +0000 | [diff] [blame] | 190 | MXC_CSPICTRL_DATARATE(div) | |
Stefano Babic | c9d59c7 | 2011-01-19 22:46:30 +0000 | [diff] [blame] | 191 | MXC_CSPICTRL_EN | |
| 192 | #ifdef CONFIG_MX35 |
| 193 | MXC_CSPICTRL_SSCTL | |
| 194 | #endif |
| 195 | MXC_CSPICTRL_MODE; |
| 196 | |
| 197 | if (mode & SPI_CPHA) |
| 198 | ctrl_reg |= MXC_CSPICTRL_PHA; |
| 199 | if (mode & SPI_CPOL) |
| 200 | ctrl_reg |= MXC_CSPICTRL_POL; |
| 201 | if (mode & SPI_CS_HIGH) |
| 202 | ctrl_reg |= MXC_CSPICTRL_SSPOL; |
| 203 | mxcs->ctrl_reg = ctrl_reg; |
| 204 | |
| 205 | return 0; |
| 206 | } |
| 207 | #endif |
| 208 | |
| 209 | #if defined(CONFIG_MX51) |
| 210 | 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] | 211 | unsigned int max_hz, unsigned int mode) |
| 212 | { |
| 213 | u32 clk_src = mxc_get_clock(MXC_CSPI_CLK); |
| 214 | s32 pre_div = 0, post_div = 0, i, reg_ctrl, reg_config; |
| 215 | u32 ss_pol = 0, sclkpol = 0, sclkpha = 0; |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 216 | struct cspi_regs *regs = (struct cspi_regs *)mxcs->base; |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 217 | |
| 218 | if (max_hz == 0) { |
| 219 | printf("Error: desired clock is 0\n"); |
| 220 | return -1; |
| 221 | } |
| 222 | |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 223 | reg_ctrl = reg_read(®s->ctrl); |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 224 | |
| 225 | /* Reset spi */ |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 226 | reg_write(®s->ctrl, 0); |
| 227 | reg_write(®s->ctrl, (reg_ctrl | 0x1)); |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 228 | |
| 229 | /* |
| 230 | * The following computation is taken directly from Freescale's code. |
| 231 | */ |
| 232 | if (clk_src > max_hz) { |
| 233 | pre_div = clk_src / max_hz; |
| 234 | if (pre_div > 16) { |
| 235 | post_div = pre_div / 16; |
| 236 | pre_div = 15; |
| 237 | } |
| 238 | if (post_div != 0) { |
| 239 | for (i = 0; i < 16; i++) { |
| 240 | if ((1 << i) >= post_div) |
| 241 | break; |
| 242 | } |
| 243 | if (i == 16) { |
| 244 | printf("Error: no divider for the freq: %d\n", |
| 245 | max_hz); |
| 246 | return -1; |
| 247 | } |
| 248 | post_div = i; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | debug("pre_div = %d, post_div=%d\n", pre_div, post_div); |
| 253 | reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_SELCHAN(3)) | |
| 254 | MXC_CSPICTRL_SELCHAN(cs); |
| 255 | reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_PREDIV(0x0F)) | |
| 256 | MXC_CSPICTRL_PREDIV(pre_div); |
| 257 | reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_POSTDIV(0x0F)) | |
| 258 | MXC_CSPICTRL_POSTDIV(post_div); |
| 259 | |
| 260 | /* always set to master mode */ |
| 261 | reg_ctrl |= 1 << (cs + 4); |
| 262 | |
| 263 | /* We need to disable SPI before changing registers */ |
| 264 | reg_ctrl &= ~MXC_CSPICTRL_EN; |
| 265 | |
| 266 | if (mode & SPI_CS_HIGH) |
| 267 | ss_pol = 1; |
| 268 | |
Stefano Babic | 9f481e9 | 2010-08-23 20:41:19 +0200 | [diff] [blame] | 269 | if (mode & SPI_CPOL) |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 270 | sclkpol = 1; |
| 271 | |
| 272 | if (mode & SPI_CPHA) |
| 273 | sclkpha = 1; |
| 274 | |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 275 | reg_config = reg_read(®s->cfg); |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 276 | |
| 277 | /* |
| 278 | * Configuration register setup |
Stefano Babic | c9d59c7 | 2011-01-19 22:46:30 +0000 | [diff] [blame] | 279 | * The MX51 supports different setup for each SS |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 280 | */ |
| 281 | reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_SSPOL))) | |
| 282 | (ss_pol << (cs + MXC_CSPICON_SSPOL)); |
| 283 | reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_POL))) | |
| 284 | (sclkpol << (cs + MXC_CSPICON_POL)); |
| 285 | reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_PHA))) | |
| 286 | (sclkpha << (cs + MXC_CSPICON_PHA)); |
| 287 | |
| 288 | debug("reg_ctrl = 0x%x\n", reg_ctrl); |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 289 | reg_write(®s->ctrl, reg_ctrl); |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 290 | debug("reg_config = 0x%x\n", reg_config); |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 291 | reg_write(®s->cfg, reg_config); |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 292 | |
| 293 | /* save config register and control register */ |
| 294 | mxcs->ctrl_reg = reg_ctrl; |
| 295 | mxcs->cfg_reg = reg_config; |
| 296 | |
| 297 | /* clear interrupt reg */ |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 298 | reg_write(®s->intr, 0); |
| 299 | reg_write(®s->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF); |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 300 | |
| 301 | return 0; |
| 302 | } |
| 303 | #endif |
| 304 | |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 305 | int spi_xchg_single(struct spi_slave *slave, unsigned int bitlen, |
| 306 | const u8 *dout, u8 *din, unsigned long flags) |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 307 | { |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 308 | struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave); |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 309 | int nbytes = (bitlen + 7) / 8; |
| 310 | u32 data, cnt, i; |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 311 | struct cspi_regs *regs = (struct cspi_regs *)mxcs->base; |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 312 | |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 313 | debug("%s: bitlen %d dout 0x%x din 0x%x\n", |
| 314 | __func__, bitlen, (u32)dout, (u32)din); |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 315 | |
| 316 | mxcs->ctrl_reg = (mxcs->ctrl_reg & |
| 317 | ~MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS)) | |
Guennadi Liakhovetski | f9b6a15 | 2009-02-07 00:09:12 +0100 | [diff] [blame] | 318 | MXC_CSPICTRL_BITCOUNT(bitlen - 1); |
| 319 | |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 320 | reg_write(®s->ctrl, mxcs->ctrl_reg | MXC_CSPICTRL_EN); |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 321 | #ifdef CONFIG_MX51 |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 322 | reg_write(®s->cfg, mxcs->cfg_reg); |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 323 | #endif |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 324 | |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 325 | /* Clear interrupt register */ |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 326 | reg_write(®s->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF); |
Guennadi Liakhovetski | fc7a93c | 2009-02-13 09:26:40 +0100 | [diff] [blame] | 327 | |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 328 | /* |
| 329 | * The SPI controller works only with words, |
| 330 | * check if less than a word is sent. |
| 331 | * Access to the FIFO is only 32 bit |
| 332 | */ |
| 333 | if (bitlen % 32) { |
| 334 | data = 0; |
| 335 | cnt = (bitlen % 32) / 8; |
| 336 | if (dout) { |
| 337 | for (i = 0; i < cnt; i++) { |
| 338 | data = (data << 8) | (*dout++ & 0xFF); |
| 339 | } |
| 340 | } |
| 341 | debug("Sending SPI 0x%x\n", data); |
| 342 | |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 343 | reg_write(®s->txdata, data); |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 344 | nbytes -= cnt; |
| 345 | } |
| 346 | |
| 347 | data = 0; |
| 348 | |
| 349 | while (nbytes > 0) { |
| 350 | data = 0; |
| 351 | if (dout) { |
| 352 | /* Buffer is not 32-bit aligned */ |
| 353 | if ((unsigned long)dout & 0x03) { |
| 354 | data = 0; |
Anatolij Gustschin | dff0109 | 2011-01-20 07:53:06 +0000 | [diff] [blame] | 355 | for (i = 0; i < 4; i++) |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 356 | data = (data << 8) | (*dout++ & 0xFF); |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 357 | } else { |
| 358 | data = *(u32 *)dout; |
| 359 | data = cpu_to_be32(data); |
| 360 | } |
| 361 | dout += 4; |
| 362 | } |
| 363 | debug("Sending SPI 0x%x\n", data); |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 364 | reg_write(®s->txdata, data); |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 365 | nbytes -= 4; |
| 366 | } |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 367 | |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 368 | /* FIFO is written, now starts the transfer setting the XCH bit */ |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 369 | reg_write(®s->ctrl, mxcs->ctrl_reg | |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 370 | MXC_CSPICTRL_EN | MXC_CSPICTRL_XCH); |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 371 | |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 372 | /* Wait until the TC (Transfer completed) bit is set */ |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 373 | while ((reg_read(®s->stat) & MXC_CSPICTRL_TC) == 0) |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 374 | ; |
| 375 | |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 376 | /* Transfer completed, clear any pending request */ |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 377 | reg_write(®s->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF); |
Guennadi Liakhovetski | fc7a93c | 2009-02-13 09:26:40 +0100 | [diff] [blame] | 378 | |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 379 | nbytes = (bitlen + 7) / 8; |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 380 | |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 381 | cnt = nbytes % 32; |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 382 | |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 383 | if (bitlen % 32) { |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 384 | data = reg_read(®s->rxdata); |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 385 | cnt = (bitlen % 32) / 8; |
Anatolij Gustschin | dff0109 | 2011-01-20 07:53:06 +0000 | [diff] [blame] | 386 | data = cpu_to_be32(data) >> ((sizeof(data) - cnt) * 8); |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 387 | debug("SPI Rx unaligned: 0x%x\n", data); |
| 388 | if (din) { |
Anatolij Gustschin | dff0109 | 2011-01-20 07:53:06 +0000 | [diff] [blame] | 389 | memcpy(din, &data, cnt); |
| 390 | din += cnt; |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 391 | } |
| 392 | nbytes -= cnt; |
| 393 | } |
| 394 | |
| 395 | while (nbytes > 0) { |
| 396 | u32 tmp; |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 397 | tmp = reg_read(®s->rxdata); |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 398 | data = cpu_to_be32(tmp); |
| 399 | debug("SPI Rx: 0x%x 0x%x\n", tmp, data); |
| 400 | cnt = min(nbytes, sizeof(data)); |
| 401 | if (din) { |
| 402 | memcpy(din, &data, cnt); |
| 403 | din += cnt; |
| 404 | } |
| 405 | nbytes -= cnt; |
| 406 | } |
| 407 | |
| 408 | return 0; |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 409 | |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 410 | } |
| 411 | |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 412 | int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout, |
| 413 | void *din, unsigned long flags) |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 414 | { |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 415 | int n_bytes = (bitlen + 7) / 8; |
| 416 | int n_bits; |
| 417 | int ret; |
| 418 | u32 blk_size; |
| 419 | u8 *p_outbuf = (u8 *)dout; |
| 420 | u8 *p_inbuf = (u8 *)din; |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 421 | |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 422 | if (!slave) |
| 423 | return -1; |
| 424 | |
| 425 | if (flags & SPI_XFER_BEGIN) |
| 426 | spi_cs_activate(slave); |
| 427 | |
| 428 | while (n_bytes > 0) { |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 429 | if (n_bytes < MAX_SPI_BYTES) |
| 430 | blk_size = n_bytes; |
| 431 | else |
| 432 | blk_size = MAX_SPI_BYTES; |
| 433 | |
| 434 | n_bits = blk_size * 8; |
| 435 | |
| 436 | ret = spi_xchg_single(slave, n_bits, p_outbuf, p_inbuf, 0); |
| 437 | |
| 438 | if (ret) |
| 439 | return ret; |
| 440 | if (dout) |
| 441 | p_outbuf += blk_size; |
| 442 | if (din) |
| 443 | p_inbuf += blk_size; |
| 444 | n_bytes -= blk_size; |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 445 | } |
| 446 | |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 447 | if (flags & SPI_XFER_END) { |
| 448 | spi_cs_deactivate(slave); |
Guennadi Liakhovetski | f9b6a15 | 2009-02-07 00:09:12 +0100 | [diff] [blame] | 449 | } |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 450 | |
| 451 | return 0; |
| 452 | } |
| 453 | |
| 454 | void spi_init(void) |
| 455 | { |
| 456 | } |
| 457 | |
Guennadi Liakhovetski | fc7a93c | 2009-02-13 09:26:40 +0100 | [diff] [blame] | 458 | static int decode_cs(struct mxc_spi_slave *mxcs, unsigned int cs) |
| 459 | { |
| 460 | int ret; |
| 461 | |
| 462 | /* |
| 463 | * Some SPI devices require active chip-select over multiple |
| 464 | * transactions, we achieve this using a GPIO. Still, the SPI |
| 465 | * controller has to be configured to use one of its own chipselects. |
| 466 | * To use this feature you have to call spi_setup_slave() with |
| 467 | * cs = internal_cs | (gpio << 8), and you have to use some unused |
| 468 | * on this SPI controller cs between 0 and 3. |
| 469 | */ |
| 470 | if (cs > 3) { |
| 471 | mxcs->gpio = cs >> 8; |
| 472 | cs &= 3; |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 473 | ret = mxc_gpio_direction(mxcs->gpio, OUT); |
Guennadi Liakhovetski | fc7a93c | 2009-02-13 09:26:40 +0100 | [diff] [blame] | 474 | if (ret) { |
| 475 | printf("mxc_spi: cannot setup gpio %d\n", mxcs->gpio); |
| 476 | return -EINVAL; |
| 477 | } |
| 478 | } else { |
| 479 | mxcs->gpio = -1; |
| 480 | } |
| 481 | |
| 482 | return cs; |
| 483 | } |
| 484 | |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 485 | struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs, |
| 486 | unsigned int max_hz, unsigned int mode) |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 487 | { |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 488 | struct mxc_spi_slave *mxcs; |
Guennadi Liakhovetski | fc7a93c | 2009-02-13 09:26:40 +0100 | [diff] [blame] | 489 | int ret; |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 490 | |
Guennadi Liakhovetski | fc7a93c | 2009-02-13 09:26:40 +0100 | [diff] [blame] | 491 | if (bus >= ARRAY_SIZE(spi_bases)) |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 492 | return NULL; |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 493 | |
Guennadi Liakhovetski | fc7a93c | 2009-02-13 09:26:40 +0100 | [diff] [blame] | 494 | mxcs = malloc(sizeof(struct mxc_spi_slave)); |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 495 | if (!mxcs) { |
| 496 | puts("mxc_spi: SPI Slave not allocated !\n"); |
Guennadi Liakhovetski | fc7a93c | 2009-02-13 09:26:40 +0100 | [diff] [blame] | 497 | return NULL; |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 498 | } |
Guennadi Liakhovetski | fc7a93c | 2009-02-13 09:26:40 +0100 | [diff] [blame] | 499 | |
| 500 | ret = decode_cs(mxcs, cs); |
| 501 | if (ret < 0) { |
| 502 | free(mxcs); |
| 503 | return NULL; |
| 504 | } |
| 505 | |
| 506 | cs = ret; |
| 507 | |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 508 | mxcs->slave.bus = bus; |
| 509 | mxcs->slave.cs = cs; |
| 510 | mxcs->base = spi_bases[bus]; |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 511 | mxcs->ss_pol = (mode & SPI_CS_HIGH) ? 1 : 0; |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 512 | |
Stefano Babic | c9d59c7 | 2011-01-19 22:46:30 +0000 | [diff] [blame] | 513 | ret = spi_cfg_mxc(mxcs, cs, max_hz, mode); |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 514 | if (ret) { |
| 515 | printf("mxc_spi: cannot setup SPI controller\n"); |
| 516 | free(mxcs); |
| 517 | return NULL; |
| 518 | } |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 519 | return &mxcs->slave; |
| 520 | } |
| 521 | |
| 522 | void spi_free_slave(struct spi_slave *slave) |
| 523 | { |
Guennadi Liakhovetski | f9b6a15 | 2009-02-07 00:09:12 +0100 | [diff] [blame] | 524 | struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave); |
| 525 | |
| 526 | free(mxcs); |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | int spi_claim_bus(struct spi_slave *slave) |
| 530 | { |
| 531 | struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave); |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 532 | struct cspi_regs *regs = (struct cspi_regs *)mxcs->base; |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 533 | |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 534 | reg_write(®s->rxdata, 1); |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 535 | udelay(1); |
Stefano Babic | ac87c17 | 2011-01-19 22:46:33 +0000 | [diff] [blame] | 536 | reg_write(®s->ctrl, mxcs->ctrl_reg); |
| 537 | reg_write(®s->period, MXC_CSPIPERIOD_32KHZ); |
| 538 | reg_write(®s->intr, 0); |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 539 | |
| 540 | return 0; |
| 541 | } |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 542 | |
| 543 | void spi_release_bus(struct spi_slave *slave) |
| 544 | { |
| 545 | /* TODO: Shut the controller down */ |
| 546 | } |