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> |
Guennadi Liakhovetski | 38254f4 | 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 | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 35 | #elif defined(CONFIG_MX31) |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 36 | |
Guennadi Liakhovetski | fc7a93c | 2009-02-13 09:26:40 +0100 | [diff] [blame] | 37 | #include <asm/arch/mx31.h> |
| 38 | |
Guennadi Liakhovetski | 38254f4 | 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 | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 56 | #define MXC_CSPICTRL_SSPOL (1 << 7) |
Guennadi Liakhovetski | 38254f4 | 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 | d205ddc | 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 | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 63 | |
| 64 | #define MXC_CSPIPERIOD_32KHZ (1 << 15) |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 65 | #define MAX_SPI_BYTES 4 |
Guennadi Liakhovetski | 38254f4 | 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 | c9d59c7 | 2011-01-19 22:46:30 +0000 | [diff] [blame] | 73 | #define mxc_get_clock(x) mx31_get_ipg_clk() |
| 74 | |
Stefano Babic | d205ddc | 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 | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 101 | #define MAX_SPI_BYTES 32 |
Stefano Babic | d205ddc | 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 | c9d59c7 | 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 | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 155 | #else |
| 156 | #error "Unsupported architecture" |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 157 | #endif |
| 158 | |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 159 | #define OUT MXC_GPIO_DIRECTION_OUT |
| 160 | |
Haavard Skinnemoen | d255bb0 | 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 | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 165 | #if defined(CONFIG_MX51) |
| 166 | u32 cfg_reg; |
| 167 | #endif |
Guennadi Liakhovetski | fc7a93c | 2009-02-13 09:26:40 +0100 | [diff] [blame] | 168 | int gpio; |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 169 | int ss_pol; |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 170 | }; |
Haavard Skinnemoen | d255bb0 | 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 | 38254f4 | 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 | } |
| 186 | |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 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 | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 191 | mxc_gpio_set(mxcs->gpio, mxcs->ss_pol); |
Stefano Babic | d205ddc | 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 | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 199 | !(mxcs->ss_pol)); |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 200 | } |
| 201 | |
Anatolij Gustschin | afaa9f6 | 2011-01-19 22:46:32 +0000 | [diff] [blame^] | 202 | u32 get_cspi_div(u32 div) |
| 203 | { |
| 204 | int i; |
| 205 | |
| 206 | for (i = 0; i < 8; i++) { |
| 207 | if (div <= (4 << i)) |
| 208 | return i; |
| 209 | } |
| 210 | return i; |
| 211 | } |
| 212 | |
Stefano Babic | c9d59c7 | 2011-01-19 22:46:30 +0000 | [diff] [blame] | 213 | #if defined(CONFIG_MX31) || defined(CONFIG_MX35) |
| 214 | static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs, |
| 215 | unsigned int max_hz, unsigned int mode) |
| 216 | { |
| 217 | unsigned int ctrl_reg; |
Anatolij Gustschin | afaa9f6 | 2011-01-19 22:46:32 +0000 | [diff] [blame^] | 218 | u32 clk_src; |
| 219 | u32 div; |
| 220 | |
| 221 | clk_src = mxc_get_clock(MXC_CSPI_CLK); |
| 222 | |
| 223 | div = clk_src / max_hz; |
| 224 | div = get_cspi_div(div); |
| 225 | |
| 226 | debug("clk %d Hz, div %d, real clk %d Hz\n", |
| 227 | max_hz, div, clk_src / (4 << div)); |
Stefano Babic | c9d59c7 | 2011-01-19 22:46:30 +0000 | [diff] [blame] | 228 | |
| 229 | ctrl_reg = MXC_CSPICTRL_CHIPSELECT(cs) | |
| 230 | MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS) | |
Anatolij Gustschin | afaa9f6 | 2011-01-19 22:46:32 +0000 | [diff] [blame^] | 231 | MXC_CSPICTRL_DATARATE(div) | |
Stefano Babic | c9d59c7 | 2011-01-19 22:46:30 +0000 | [diff] [blame] | 232 | MXC_CSPICTRL_EN | |
| 233 | #ifdef CONFIG_MX35 |
| 234 | MXC_CSPICTRL_SSCTL | |
| 235 | #endif |
| 236 | MXC_CSPICTRL_MODE; |
| 237 | |
| 238 | if (mode & SPI_CPHA) |
| 239 | ctrl_reg |= MXC_CSPICTRL_PHA; |
| 240 | if (mode & SPI_CPOL) |
| 241 | ctrl_reg |= MXC_CSPICTRL_POL; |
| 242 | if (mode & SPI_CS_HIGH) |
| 243 | ctrl_reg |= MXC_CSPICTRL_SSPOL; |
| 244 | mxcs->ctrl_reg = ctrl_reg; |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | #endif |
| 249 | |
| 250 | #if defined(CONFIG_MX51) |
| 251 | 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] | 252 | unsigned int max_hz, unsigned int mode) |
| 253 | { |
| 254 | u32 clk_src = mxc_get_clock(MXC_CSPI_CLK); |
| 255 | s32 pre_div = 0, post_div = 0, i, reg_ctrl, reg_config; |
| 256 | u32 ss_pol = 0, sclkpol = 0, sclkpha = 0; |
| 257 | |
| 258 | if (max_hz == 0) { |
| 259 | printf("Error: desired clock is 0\n"); |
| 260 | return -1; |
| 261 | } |
| 262 | |
| 263 | reg_ctrl = reg_read(mxcs->base + MXC_CSPICTRL); |
| 264 | |
| 265 | /* Reset spi */ |
| 266 | reg_write(mxcs->base + MXC_CSPICTRL, 0); |
| 267 | reg_write(mxcs->base + MXC_CSPICTRL, (reg_ctrl | 0x1)); |
| 268 | |
| 269 | /* |
| 270 | * The following computation is taken directly from Freescale's code. |
| 271 | */ |
| 272 | if (clk_src > max_hz) { |
| 273 | pre_div = clk_src / max_hz; |
| 274 | if (pre_div > 16) { |
| 275 | post_div = pre_div / 16; |
| 276 | pre_div = 15; |
| 277 | } |
| 278 | if (post_div != 0) { |
| 279 | for (i = 0; i < 16; i++) { |
| 280 | if ((1 << i) >= post_div) |
| 281 | break; |
| 282 | } |
| 283 | if (i == 16) { |
| 284 | printf("Error: no divider for the freq: %d\n", |
| 285 | max_hz); |
| 286 | return -1; |
| 287 | } |
| 288 | post_div = i; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | debug("pre_div = %d, post_div=%d\n", pre_div, post_div); |
| 293 | reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_SELCHAN(3)) | |
| 294 | MXC_CSPICTRL_SELCHAN(cs); |
| 295 | reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_PREDIV(0x0F)) | |
| 296 | MXC_CSPICTRL_PREDIV(pre_div); |
| 297 | reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_POSTDIV(0x0F)) | |
| 298 | MXC_CSPICTRL_POSTDIV(post_div); |
| 299 | |
| 300 | /* always set to master mode */ |
| 301 | reg_ctrl |= 1 << (cs + 4); |
| 302 | |
| 303 | /* We need to disable SPI before changing registers */ |
| 304 | reg_ctrl &= ~MXC_CSPICTRL_EN; |
| 305 | |
| 306 | if (mode & SPI_CS_HIGH) |
| 307 | ss_pol = 1; |
| 308 | |
Stefano Babic | 9f481e9 | 2010-08-23 20:41:19 +0200 | [diff] [blame] | 309 | if (mode & SPI_CPOL) |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 310 | sclkpol = 1; |
| 311 | |
| 312 | if (mode & SPI_CPHA) |
| 313 | sclkpha = 1; |
| 314 | |
| 315 | reg_config = reg_read(mxcs->base + MXC_CSPICON); |
| 316 | |
| 317 | /* |
| 318 | * Configuration register setup |
Stefano Babic | c9d59c7 | 2011-01-19 22:46:30 +0000 | [diff] [blame] | 319 | * The MX51 supports different setup for each SS |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 320 | */ |
| 321 | reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_SSPOL))) | |
| 322 | (ss_pol << (cs + MXC_CSPICON_SSPOL)); |
| 323 | reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_POL))) | |
| 324 | (sclkpol << (cs + MXC_CSPICON_POL)); |
| 325 | reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_PHA))) | |
| 326 | (sclkpha << (cs + MXC_CSPICON_PHA)); |
| 327 | |
| 328 | debug("reg_ctrl = 0x%x\n", reg_ctrl); |
| 329 | reg_write(mxcs->base + MXC_CSPICTRL, reg_ctrl); |
| 330 | debug("reg_config = 0x%x\n", reg_config); |
| 331 | reg_write(mxcs->base + MXC_CSPICON, reg_config); |
| 332 | |
| 333 | /* save config register and control register */ |
| 334 | mxcs->ctrl_reg = reg_ctrl; |
| 335 | mxcs->cfg_reg = reg_config; |
| 336 | |
| 337 | /* clear interrupt reg */ |
| 338 | reg_write(mxcs->base + MXC_CSPIINT, 0); |
| 339 | reg_write(mxcs->base + MXC_CSPISTAT, |
| 340 | MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF); |
| 341 | |
| 342 | return 0; |
| 343 | } |
| 344 | #endif |
| 345 | |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 346 | int spi_xchg_single(struct spi_slave *slave, unsigned int bitlen, |
| 347 | const u8 *dout, u8 *din, unsigned long flags) |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 348 | { |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 349 | struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave); |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 350 | int nbytes = (bitlen + 7) / 8; |
| 351 | u32 data, cnt, i; |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 352 | |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 353 | debug("%s: bitlen %d dout 0x%x din 0x%x\n", |
| 354 | __func__, bitlen, (u32)dout, (u32)din); |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 355 | |
| 356 | mxcs->ctrl_reg = (mxcs->ctrl_reg & |
| 357 | ~MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS)) | |
Guennadi Liakhovetski | f9b6a15 | 2009-02-07 00:09:12 +0100 | [diff] [blame] | 358 | MXC_CSPICTRL_BITCOUNT(bitlen - 1); |
| 359 | |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 360 | reg_write(mxcs->base + MXC_CSPICTRL, mxcs->ctrl_reg | MXC_CSPICTRL_EN); |
| 361 | #ifdef CONFIG_MX51 |
| 362 | reg_write(mxcs->base + MXC_CSPICON, mxcs->cfg_reg); |
| 363 | #endif |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 364 | |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 365 | /* Clear interrupt register */ |
| 366 | reg_write(mxcs->base + MXC_CSPISTAT, |
| 367 | MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF); |
Guennadi Liakhovetski | fc7a93c | 2009-02-13 09:26:40 +0100 | [diff] [blame] | 368 | |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 369 | /* |
| 370 | * The SPI controller works only with words, |
| 371 | * check if less than a word is sent. |
| 372 | * Access to the FIFO is only 32 bit |
| 373 | */ |
| 374 | if (bitlen % 32) { |
| 375 | data = 0; |
| 376 | cnt = (bitlen % 32) / 8; |
| 377 | if (dout) { |
| 378 | for (i = 0; i < cnt; i++) { |
| 379 | data = (data << 8) | (*dout++ & 0xFF); |
| 380 | } |
| 381 | } |
| 382 | debug("Sending SPI 0x%x\n", data); |
| 383 | |
| 384 | reg_write(mxcs->base + MXC_CSPITXDATA, data); |
| 385 | nbytes -= cnt; |
| 386 | } |
| 387 | |
| 388 | data = 0; |
| 389 | |
| 390 | while (nbytes > 0) { |
| 391 | data = 0; |
| 392 | if (dout) { |
| 393 | /* Buffer is not 32-bit aligned */ |
| 394 | if ((unsigned long)dout & 0x03) { |
| 395 | data = 0; |
Anatolij Gustschin | dff0109 | 2011-01-20 07:53:06 +0000 | [diff] [blame] | 396 | for (i = 0; i < 4; i++) |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 397 | data = (data << 8) | (*dout++ & 0xFF); |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 398 | } else { |
| 399 | data = *(u32 *)dout; |
| 400 | data = cpu_to_be32(data); |
| 401 | } |
| 402 | dout += 4; |
| 403 | } |
| 404 | debug("Sending SPI 0x%x\n", data); |
| 405 | reg_write(mxcs->base + MXC_CSPITXDATA, data); |
| 406 | nbytes -= 4; |
| 407 | } |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 408 | |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 409 | /* FIFO is written, now starts the transfer setting the XCH bit */ |
| 410 | reg_write(mxcs->base + MXC_CSPICTRL, mxcs->ctrl_reg | |
| 411 | MXC_CSPICTRL_EN | MXC_CSPICTRL_XCH); |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 412 | |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 413 | /* Wait until the TC (Transfer completed) bit is set */ |
| 414 | while ((reg_read(mxcs->base + MXC_CSPISTAT) & MXC_CSPICTRL_TC) == 0) |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 415 | ; |
| 416 | |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 417 | /* Transfer completed, clear any pending request */ |
| 418 | reg_write(mxcs->base + MXC_CSPISTAT, |
| 419 | MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF); |
Guennadi Liakhovetski | fc7a93c | 2009-02-13 09:26:40 +0100 | [diff] [blame] | 420 | |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 421 | nbytes = (bitlen + 7) / 8; |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 422 | |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 423 | cnt = nbytes % 32; |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 424 | |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 425 | if (bitlen % 32) { |
| 426 | data = reg_read(mxcs->base + MXC_CSPIRXDATA); |
| 427 | cnt = (bitlen % 32) / 8; |
Anatolij Gustschin | dff0109 | 2011-01-20 07:53:06 +0000 | [diff] [blame] | 428 | data = cpu_to_be32(data) >> ((sizeof(data) - cnt) * 8); |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 429 | debug("SPI Rx unaligned: 0x%x\n", data); |
| 430 | if (din) { |
Anatolij Gustschin | dff0109 | 2011-01-20 07:53:06 +0000 | [diff] [blame] | 431 | memcpy(din, &data, cnt); |
| 432 | din += cnt; |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 433 | } |
| 434 | nbytes -= cnt; |
| 435 | } |
| 436 | |
| 437 | while (nbytes > 0) { |
| 438 | u32 tmp; |
| 439 | tmp = reg_read(mxcs->base + MXC_CSPIRXDATA); |
| 440 | data = cpu_to_be32(tmp); |
| 441 | debug("SPI Rx: 0x%x 0x%x\n", tmp, data); |
| 442 | cnt = min(nbytes, sizeof(data)); |
| 443 | if (din) { |
| 444 | memcpy(din, &data, cnt); |
| 445 | din += cnt; |
| 446 | } |
| 447 | nbytes -= cnt; |
| 448 | } |
| 449 | |
| 450 | return 0; |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 451 | |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 452 | } |
| 453 | |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 454 | int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout, |
| 455 | void *din, unsigned long flags) |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 456 | { |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 457 | int n_bytes = (bitlen + 7) / 8; |
| 458 | int n_bits; |
| 459 | int ret; |
| 460 | u32 blk_size; |
| 461 | u8 *p_outbuf = (u8 *)dout; |
| 462 | u8 *p_inbuf = (u8 *)din; |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 463 | |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 464 | if (!slave) |
| 465 | return -1; |
| 466 | |
| 467 | if (flags & SPI_XFER_BEGIN) |
| 468 | spi_cs_activate(slave); |
| 469 | |
| 470 | while (n_bytes > 0) { |
| 471 | |
| 472 | if (n_bytes < MAX_SPI_BYTES) |
| 473 | blk_size = n_bytes; |
| 474 | else |
| 475 | blk_size = MAX_SPI_BYTES; |
| 476 | |
| 477 | n_bits = blk_size * 8; |
| 478 | |
| 479 | ret = spi_xchg_single(slave, n_bits, p_outbuf, p_inbuf, 0); |
| 480 | |
| 481 | if (ret) |
| 482 | return ret; |
| 483 | if (dout) |
| 484 | p_outbuf += blk_size; |
| 485 | if (din) |
| 486 | p_inbuf += blk_size; |
| 487 | n_bytes -= blk_size; |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 488 | } |
| 489 | |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 490 | if (flags & SPI_XFER_END) { |
| 491 | spi_cs_deactivate(slave); |
Guennadi Liakhovetski | f9b6a15 | 2009-02-07 00:09:12 +0100 | [diff] [blame] | 492 | } |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 493 | |
| 494 | return 0; |
| 495 | } |
| 496 | |
| 497 | void spi_init(void) |
| 498 | { |
| 499 | } |
| 500 | |
Guennadi Liakhovetski | fc7a93c | 2009-02-13 09:26:40 +0100 | [diff] [blame] | 501 | static int decode_cs(struct mxc_spi_slave *mxcs, unsigned int cs) |
| 502 | { |
| 503 | int ret; |
| 504 | |
| 505 | /* |
| 506 | * Some SPI devices require active chip-select over multiple |
| 507 | * transactions, we achieve this using a GPIO. Still, the SPI |
| 508 | * controller has to be configured to use one of its own chipselects. |
| 509 | * To use this feature you have to call spi_setup_slave() with |
| 510 | * cs = internal_cs | (gpio << 8), and you have to use some unused |
| 511 | * on this SPI controller cs between 0 and 3. |
| 512 | */ |
| 513 | if (cs > 3) { |
| 514 | mxcs->gpio = cs >> 8; |
| 515 | cs &= 3; |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 516 | ret = mxc_gpio_direction(mxcs->gpio, OUT); |
Guennadi Liakhovetski | fc7a93c | 2009-02-13 09:26:40 +0100 | [diff] [blame] | 517 | if (ret) { |
| 518 | printf("mxc_spi: cannot setup gpio %d\n", mxcs->gpio); |
| 519 | return -EINVAL; |
| 520 | } |
| 521 | } else { |
| 522 | mxcs->gpio = -1; |
| 523 | } |
| 524 | |
| 525 | return cs; |
| 526 | } |
| 527 | |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 528 | struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs, |
| 529 | unsigned int max_hz, unsigned int mode) |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 530 | { |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 531 | struct mxc_spi_slave *mxcs; |
Guennadi Liakhovetski | fc7a93c | 2009-02-13 09:26:40 +0100 | [diff] [blame] | 532 | int ret; |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 533 | |
Guennadi Liakhovetski | fc7a93c | 2009-02-13 09:26:40 +0100 | [diff] [blame] | 534 | if (bus >= ARRAY_SIZE(spi_bases)) |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 535 | return NULL; |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 536 | |
Guennadi Liakhovetski | fc7a93c | 2009-02-13 09:26:40 +0100 | [diff] [blame] | 537 | mxcs = malloc(sizeof(struct mxc_spi_slave)); |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 538 | if (!mxcs) { |
| 539 | puts("mxc_spi: SPI Slave not allocated !\n"); |
Guennadi Liakhovetski | fc7a93c | 2009-02-13 09:26:40 +0100 | [diff] [blame] | 540 | return NULL; |
Stefano Babic | 2f721d1 | 2010-08-20 12:05:03 +0200 | [diff] [blame] | 541 | } |
Guennadi Liakhovetski | fc7a93c | 2009-02-13 09:26:40 +0100 | [diff] [blame] | 542 | |
| 543 | ret = decode_cs(mxcs, cs); |
| 544 | if (ret < 0) { |
| 545 | free(mxcs); |
| 546 | return NULL; |
| 547 | } |
| 548 | |
| 549 | cs = ret; |
| 550 | |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 551 | mxcs->slave.bus = bus; |
| 552 | mxcs->slave.cs = cs; |
| 553 | mxcs->base = spi_bases[bus]; |
Stefano Babic | c4ea142 | 2010-07-06 17:05:06 +0200 | [diff] [blame] | 554 | mxcs->ss_pol = (mode & SPI_CS_HIGH) ? 1 : 0; |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 555 | |
Stefano Babic | c9d59c7 | 2011-01-19 22:46:30 +0000 | [diff] [blame] | 556 | ret = spi_cfg_mxc(mxcs, cs, max_hz, mode); |
Stefano Babic | d205ddc | 2010-04-04 22:43:38 +0200 | [diff] [blame] | 557 | if (ret) { |
| 558 | printf("mxc_spi: cannot setup SPI controller\n"); |
| 559 | free(mxcs); |
| 560 | return NULL; |
| 561 | } |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 562 | return &mxcs->slave; |
| 563 | } |
| 564 | |
| 565 | void spi_free_slave(struct spi_slave *slave) |
| 566 | { |
Guennadi Liakhovetski | f9b6a15 | 2009-02-07 00:09:12 +0100 | [diff] [blame] | 567 | struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave); |
| 568 | |
| 569 | free(mxcs); |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | int spi_claim_bus(struct spi_slave *slave) |
| 573 | { |
| 574 | struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave); |
| 575 | |
| 576 | reg_write(mxcs->base + MXC_CSPIRESET, 1); |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 577 | udelay(1); |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 578 | reg_write(mxcs->base + MXC_CSPICTRL, mxcs->ctrl_reg); |
| 579 | reg_write(mxcs->base + MXC_CSPIPERIOD, |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 580 | MXC_CSPIPERIOD_32KHZ); |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 581 | reg_write(mxcs->base + MXC_CSPIINT, 0); |
Guennadi Liakhovetski | 38254f4 | 2008-04-15 14:14:25 +0200 | [diff] [blame] | 582 | |
| 583 | return 0; |
| 584 | } |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 585 | |
| 586 | void spi_release_bus(struct spi_slave *slave) |
| 587 | { |
| 588 | /* TODO: Shut the controller down */ |
| 589 | } |