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