Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 Texas Instruments. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (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, MA 02111-1307 USA |
| 17 | */ |
| 18 | |
| 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/io.h> |
| 21 | #include <linux/gpio.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/delay.h> |
| 24 | #include <linux/platform_device.h> |
| 25 | #include <linux/err.h> |
| 26 | #include <linux/clk.h> |
| 27 | #include <linux/dma-mapping.h> |
| 28 | #include <linux/spi/spi.h> |
| 29 | #include <linux/spi/spi_bitbang.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 30 | #include <linux/slab.h> |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 31 | |
| 32 | #include <mach/spi.h> |
| 33 | #include <mach/edma.h> |
| 34 | |
| 35 | #define SPI_NO_RESOURCE ((resource_size_t)-1) |
| 36 | |
| 37 | #define SPI_MAX_CHIPSELECT 2 |
| 38 | |
| 39 | #define CS_DEFAULT 0xFF |
| 40 | |
| 41 | #define SPI_BUFSIZ (SMP_CACHE_BYTES + 1) |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 42 | |
| 43 | #define SPIFMT_PHASE_MASK BIT(16) |
| 44 | #define SPIFMT_POLARITY_MASK BIT(17) |
| 45 | #define SPIFMT_DISTIMER_MASK BIT(18) |
| 46 | #define SPIFMT_SHIFTDIR_MASK BIT(20) |
| 47 | #define SPIFMT_WAITENA_MASK BIT(21) |
| 48 | #define SPIFMT_PARITYENA_MASK BIT(22) |
| 49 | #define SPIFMT_ODD_PARITY_MASK BIT(23) |
| 50 | #define SPIFMT_WDELAY_MASK 0x3f000000u |
| 51 | #define SPIFMT_WDELAY_SHIFT 24 |
Brian Niebuhr | 7fe0092 | 2010-08-13 13:27:23 +0530 | [diff] [blame] | 52 | #define SPIFMT_PRESCALE_SHIFT 8 |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 53 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 54 | |
| 55 | /* SPIPC0 */ |
| 56 | #define SPIPC0_DIFUN_MASK BIT(11) /* MISO */ |
| 57 | #define SPIPC0_DOFUN_MASK BIT(10) /* MOSI */ |
| 58 | #define SPIPC0_CLKFUN_MASK BIT(9) /* CLK */ |
| 59 | #define SPIPC0_SPIENA_MASK BIT(8) /* nREADY */ |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 60 | |
| 61 | #define SPIINT_MASKALL 0x0101035F |
| 62 | #define SPI_INTLVL_1 0x000001FFu |
| 63 | #define SPI_INTLVL_0 0x00000000u |
| 64 | |
Brian Niebuhr | cfbc5d1 | 2010-08-12 12:27:33 +0530 | [diff] [blame] | 65 | /* SPIDAT1 (upper 16 bit defines) */ |
| 66 | #define SPIDAT1_CSHOLD_MASK BIT(12) |
| 67 | |
| 68 | /* SPIGCR1 */ |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 69 | #define SPIGCR1_CLKMOD_MASK BIT(1) |
| 70 | #define SPIGCR1_MASTER_MASK BIT(0) |
| 71 | #define SPIGCR1_LOOPBACK_MASK BIT(16) |
Sekhar Nori | 8e206f1 | 2010-08-20 16:20:49 +0530 | [diff] [blame] | 72 | #define SPIGCR1_SPIENA_MASK BIT(24) |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 73 | |
| 74 | /* SPIBUF */ |
| 75 | #define SPIBUF_TXFULL_MASK BIT(29) |
| 76 | #define SPIBUF_RXEMPTY_MASK BIT(31) |
| 77 | |
Brian Niebuhr | 7abbf23 | 2010-08-19 15:07:38 +0530 | [diff] [blame] | 78 | /* SPIDELAY */ |
| 79 | #define SPIDELAY_C2TDELAY_SHIFT 24 |
| 80 | #define SPIDELAY_C2TDELAY_MASK (0xFF << SPIDELAY_C2TDELAY_SHIFT) |
| 81 | #define SPIDELAY_T2CDELAY_SHIFT 16 |
| 82 | #define SPIDELAY_T2CDELAY_MASK (0xFF << SPIDELAY_T2CDELAY_SHIFT) |
| 83 | #define SPIDELAY_T2EDELAY_SHIFT 8 |
| 84 | #define SPIDELAY_T2EDELAY_MASK (0xFF << SPIDELAY_T2EDELAY_SHIFT) |
| 85 | #define SPIDELAY_C2EDELAY_SHIFT 0 |
| 86 | #define SPIDELAY_C2EDELAY_MASK 0xFF |
| 87 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 88 | /* Error Masks */ |
| 89 | #define SPIFLG_DLEN_ERR_MASK BIT(0) |
| 90 | #define SPIFLG_TIMEOUT_MASK BIT(1) |
| 91 | #define SPIFLG_PARERR_MASK BIT(2) |
| 92 | #define SPIFLG_DESYNC_MASK BIT(3) |
| 93 | #define SPIFLG_BITERR_MASK BIT(4) |
| 94 | #define SPIFLG_OVRRUN_MASK BIT(6) |
| 95 | #define SPIFLG_RX_INTR_MASK BIT(8) |
| 96 | #define SPIFLG_TX_INTR_MASK BIT(9) |
| 97 | #define SPIFLG_BUF_INIT_ACTIVE_MASK BIT(24) |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 98 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 99 | #define SPIINT_BITERR_INTR BIT(4) |
| 100 | #define SPIINT_OVRRUN_INTR BIT(6) |
| 101 | #define SPIINT_RX_INTR BIT(8) |
| 102 | #define SPIINT_TX_INTR BIT(9) |
| 103 | #define SPIINT_DMA_REQ_EN BIT(16) |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 104 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 105 | /* SPI Controller registers */ |
| 106 | #define SPIGCR0 0x00 |
| 107 | #define SPIGCR1 0x04 |
| 108 | #define SPIINT 0x08 |
| 109 | #define SPILVL 0x0c |
| 110 | #define SPIFLG 0x10 |
| 111 | #define SPIPC0 0x14 |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 112 | #define SPIDAT1 0x3c |
| 113 | #define SPIBUF 0x40 |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 114 | #define SPIDELAY 0x48 |
| 115 | #define SPIDEF 0x4c |
| 116 | #define SPIFMT0 0x50 |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 117 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 118 | /* We have 2 DMA channels per CS, one for RX and one for TX */ |
| 119 | struct davinci_spi_dma { |
| 120 | int dma_tx_channel; |
| 121 | int dma_rx_channel; |
| 122 | int dma_tx_sync_dev; |
| 123 | int dma_rx_sync_dev; |
| 124 | enum dma_event_q eventq; |
| 125 | |
| 126 | struct completion dma_tx_completion; |
| 127 | struct completion dma_rx_completion; |
| 128 | }; |
| 129 | |
| 130 | /* SPI Controller driver's private data. */ |
| 131 | struct davinci_spi { |
| 132 | struct spi_bitbang bitbang; |
| 133 | struct clk *clk; |
| 134 | |
| 135 | u8 version; |
| 136 | resource_size_t pbase; |
| 137 | void __iomem *base; |
| 138 | size_t region_size; |
| 139 | u32 irq; |
| 140 | struct completion done; |
| 141 | |
| 142 | const void *tx; |
| 143 | void *rx; |
| 144 | u8 *tmp_buf; |
| 145 | int count; |
| 146 | struct davinci_spi_dma *dma_channels; |
Brian Niebuhr | 778e261 | 2010-09-03 15:15:06 +0530 | [diff] [blame] | 147 | struct davinci_spi_platform_data *pdata; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 148 | |
| 149 | void (*get_rx)(u32 rx_data, struct davinci_spi *); |
| 150 | u32 (*get_tx)(struct davinci_spi *); |
| 151 | |
Brian Niebuhr | cda987e | 2010-08-19 16:16:28 +0530 | [diff] [blame] | 152 | u8 bytes_per_word[SPI_MAX_CHIPSELECT]; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
Brian Niebuhr | 53a31b0 | 2010-08-16 15:05:51 +0530 | [diff] [blame] | 155 | static struct davinci_spi_config davinci_spi_default_cfg; |
| 156 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 157 | static unsigned use_dma; |
| 158 | |
| 159 | static void davinci_spi_rx_buf_u8(u32 data, struct davinci_spi *davinci_spi) |
| 160 | { |
| 161 | u8 *rx = davinci_spi->rx; |
| 162 | |
| 163 | *rx++ = (u8)data; |
| 164 | davinci_spi->rx = rx; |
| 165 | } |
| 166 | |
| 167 | static void davinci_spi_rx_buf_u16(u32 data, struct davinci_spi *davinci_spi) |
| 168 | { |
| 169 | u16 *rx = davinci_spi->rx; |
| 170 | |
| 171 | *rx++ = (u16)data; |
| 172 | davinci_spi->rx = rx; |
| 173 | } |
| 174 | |
| 175 | static u32 davinci_spi_tx_buf_u8(struct davinci_spi *davinci_spi) |
| 176 | { |
| 177 | u32 data; |
| 178 | const u8 *tx = davinci_spi->tx; |
| 179 | |
| 180 | data = *tx++; |
| 181 | davinci_spi->tx = tx; |
| 182 | return data; |
| 183 | } |
| 184 | |
| 185 | static u32 davinci_spi_tx_buf_u16(struct davinci_spi *davinci_spi) |
| 186 | { |
| 187 | u32 data; |
| 188 | const u16 *tx = davinci_spi->tx; |
| 189 | |
| 190 | data = *tx++; |
| 191 | davinci_spi->tx = tx; |
| 192 | return data; |
| 193 | } |
| 194 | |
| 195 | static inline void set_io_bits(void __iomem *addr, u32 bits) |
| 196 | { |
| 197 | u32 v = ioread32(addr); |
| 198 | |
| 199 | v |= bits; |
| 200 | iowrite32(v, addr); |
| 201 | } |
| 202 | |
| 203 | static inline void clear_io_bits(void __iomem *addr, u32 bits) |
| 204 | { |
| 205 | u32 v = ioread32(addr); |
| 206 | |
| 207 | v &= ~bits; |
| 208 | iowrite32(v, addr); |
| 209 | } |
| 210 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 211 | static void davinci_spi_set_dma_req(const struct spi_device *spi, int enable) |
| 212 | { |
| 213 | struct davinci_spi *davinci_spi = spi_master_get_devdata(spi->master); |
| 214 | |
| 215 | if (enable) |
| 216 | set_io_bits(davinci_spi->base + SPIINT, SPIINT_DMA_REQ_EN); |
| 217 | else |
| 218 | clear_io_bits(davinci_spi->base + SPIINT, SPIINT_DMA_REQ_EN); |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | * Interface to control the chip select signal |
| 223 | */ |
| 224 | static void davinci_spi_chipselect(struct spi_device *spi, int value) |
| 225 | { |
| 226 | struct davinci_spi *davinci_spi; |
| 227 | struct davinci_spi_platform_data *pdata; |
Brian Niebuhr | 7978b8c | 2010-08-13 10:11:03 +0530 | [diff] [blame] | 228 | u8 chip_sel = spi->chip_select; |
Brian Niebuhr | cfbc5d1 | 2010-08-12 12:27:33 +0530 | [diff] [blame] | 229 | u16 spidat1_cfg = CS_DEFAULT; |
Brian Niebuhr | 2385397 | 2010-08-13 10:57:44 +0530 | [diff] [blame] | 230 | bool gpio_chipsel = false; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 231 | |
| 232 | davinci_spi = spi_master_get_devdata(spi->master); |
| 233 | pdata = davinci_spi->pdata; |
| 234 | |
Brian Niebuhr | 2385397 | 2010-08-13 10:57:44 +0530 | [diff] [blame] | 235 | if (pdata->chip_sel && chip_sel < pdata->num_chipselect && |
| 236 | pdata->chip_sel[chip_sel] != SPI_INTERN_CS) |
| 237 | gpio_chipsel = true; |
| 238 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 239 | /* |
| 240 | * Board specific chip select logic decides the polarity and cs |
| 241 | * line for the controller |
| 242 | */ |
Brian Niebuhr | 2385397 | 2010-08-13 10:57:44 +0530 | [diff] [blame] | 243 | if (gpio_chipsel) { |
| 244 | if (value == BITBANG_CS_ACTIVE) |
| 245 | gpio_set_value(pdata->chip_sel[chip_sel], 0); |
| 246 | else |
| 247 | gpio_set_value(pdata->chip_sel[chip_sel], 1); |
| 248 | } else { |
| 249 | if (value == BITBANG_CS_ACTIVE) { |
| 250 | spidat1_cfg |= SPIDAT1_CSHOLD_MASK; |
| 251 | spidat1_cfg &= ~(0x1 << chip_sel); |
| 252 | } |
Brian Niebuhr | 7978b8c | 2010-08-13 10:11:03 +0530 | [diff] [blame] | 253 | |
Brian Niebuhr | 2385397 | 2010-08-13 10:57:44 +0530 | [diff] [blame] | 254 | iowrite16(spidat1_cfg, davinci_spi->base + SPIDAT1 + 2); |
| 255 | } |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | /** |
Brian Niebuhr | 7fe0092 | 2010-08-13 13:27:23 +0530 | [diff] [blame] | 259 | * davinci_spi_get_prescale - Calculates the correct prescale value |
| 260 | * @maxspeed_hz: the maximum rate the SPI clock can run at |
| 261 | * |
| 262 | * This function calculates the prescale value that generates a clock rate |
| 263 | * less than or equal to the specified maximum. |
| 264 | * |
| 265 | * Returns: calculated prescale - 1 for easy programming into SPI registers |
| 266 | * or negative error number if valid prescalar cannot be updated. |
| 267 | */ |
| 268 | static inline int davinci_spi_get_prescale(struct davinci_spi *davinci_spi, |
| 269 | u32 max_speed_hz) |
| 270 | { |
| 271 | int ret; |
| 272 | |
| 273 | ret = DIV_ROUND_UP(clk_get_rate(davinci_spi->clk), max_speed_hz); |
| 274 | |
| 275 | if (ret < 3 || ret > 256) |
| 276 | return -EINVAL; |
| 277 | |
| 278 | return ret - 1; |
| 279 | } |
| 280 | |
| 281 | /** |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 282 | * davinci_spi_setup_transfer - This functions will determine transfer method |
| 283 | * @spi: spi device on which data transfer to be done |
| 284 | * @t: spi transfer in which transfer info is filled |
| 285 | * |
| 286 | * This function determines data transfer method (8/16/32 bit transfer). |
| 287 | * It will also set the SPI Clock Control register according to |
| 288 | * SPI slave device freq. |
| 289 | */ |
| 290 | static int davinci_spi_setup_transfer(struct spi_device *spi, |
| 291 | struct spi_transfer *t) |
| 292 | { |
| 293 | |
| 294 | struct davinci_spi *davinci_spi; |
Brian Niebuhr | 25f3351 | 2010-08-19 12:15:22 +0530 | [diff] [blame] | 295 | struct davinci_spi_config *spicfg; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 296 | u8 bits_per_word = 0; |
Brian Niebuhr | 25f3351 | 2010-08-19 12:15:22 +0530 | [diff] [blame] | 297 | u32 hz = 0, spifmt = 0, prescale = 0; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 298 | |
| 299 | davinci_spi = spi_master_get_devdata(spi->master); |
Brian Niebuhr | 25f3351 | 2010-08-19 12:15:22 +0530 | [diff] [blame] | 300 | spicfg = (struct davinci_spi_config *)spi->controller_data; |
| 301 | if (!spicfg) |
| 302 | spicfg = &davinci_spi_default_cfg; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 303 | |
| 304 | if (t) { |
| 305 | bits_per_word = t->bits_per_word; |
| 306 | hz = t->speed_hz; |
| 307 | } |
| 308 | |
| 309 | /* if bits_per_word is not set then set it default */ |
| 310 | if (!bits_per_word) |
| 311 | bits_per_word = spi->bits_per_word; |
| 312 | |
| 313 | /* |
| 314 | * Assign function pointer to appropriate transfer method |
| 315 | * 8bit, 16bit or 32bit transfer |
| 316 | */ |
| 317 | if (bits_per_word <= 8 && bits_per_word >= 2) { |
| 318 | davinci_spi->get_rx = davinci_spi_rx_buf_u8; |
| 319 | davinci_spi->get_tx = davinci_spi_tx_buf_u8; |
Brian Niebuhr | cda987e | 2010-08-19 16:16:28 +0530 | [diff] [blame] | 320 | davinci_spi->bytes_per_word[spi->chip_select] = 1; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 321 | } else if (bits_per_word <= 16 && bits_per_word >= 2) { |
| 322 | davinci_spi->get_rx = davinci_spi_rx_buf_u16; |
| 323 | davinci_spi->get_tx = davinci_spi_tx_buf_u16; |
Brian Niebuhr | cda987e | 2010-08-19 16:16:28 +0530 | [diff] [blame] | 324 | davinci_spi->bytes_per_word[spi->chip_select] = 2; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 325 | } else |
| 326 | return -EINVAL; |
| 327 | |
| 328 | if (!hz) |
| 329 | hz = spi->max_speed_hz; |
| 330 | |
Brian Niebuhr | 25f3351 | 2010-08-19 12:15:22 +0530 | [diff] [blame] | 331 | /* Set up SPIFMTn register, unique to this chipselect. */ |
| 332 | |
Brian Niebuhr | 7fe0092 | 2010-08-13 13:27:23 +0530 | [diff] [blame] | 333 | prescale = davinci_spi_get_prescale(davinci_spi, hz); |
| 334 | if (prescale < 0) |
| 335 | return prescale; |
| 336 | |
Brian Niebuhr | 25f3351 | 2010-08-19 12:15:22 +0530 | [diff] [blame] | 337 | spifmt = (prescale << SPIFMT_PRESCALE_SHIFT) | (bits_per_word & 0x1f); |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 338 | |
Brian Niebuhr | 25f3351 | 2010-08-19 12:15:22 +0530 | [diff] [blame] | 339 | if (spi->mode & SPI_LSB_FIRST) |
| 340 | spifmt |= SPIFMT_SHIFTDIR_MASK; |
| 341 | |
| 342 | if (spi->mode & SPI_CPOL) |
| 343 | spifmt |= SPIFMT_POLARITY_MASK; |
| 344 | |
| 345 | if (!(spi->mode & SPI_CPHA)) |
| 346 | spifmt |= SPIFMT_PHASE_MASK; |
| 347 | |
| 348 | /* |
| 349 | * Version 1 hardware supports two basic SPI modes: |
| 350 | * - Standard SPI mode uses 4 pins, with chipselect |
| 351 | * - 3 pin SPI is a 4 pin variant without CS (SPI_NO_CS) |
| 352 | * (distinct from SPI_3WIRE, with just one data wire; |
| 353 | * or similar variants without MOSI or without MISO) |
| 354 | * |
| 355 | * Version 2 hardware supports an optional handshaking signal, |
| 356 | * so it can support two more modes: |
| 357 | * - 5 pin SPI variant is standard SPI plus SPI_READY |
| 358 | * - 4 pin with enable is (SPI_READY | SPI_NO_CS) |
| 359 | */ |
| 360 | |
| 361 | if (davinci_spi->version == SPI_VERSION_2) { |
| 362 | |
Brian Niebuhr | 7abbf23 | 2010-08-19 15:07:38 +0530 | [diff] [blame] | 363 | u32 delay = 0; |
| 364 | |
Brian Niebuhr | 25f3351 | 2010-08-19 12:15:22 +0530 | [diff] [blame] | 365 | spifmt |= ((spicfg->wdelay << SPIFMT_WDELAY_SHIFT) |
| 366 | & SPIFMT_WDELAY_MASK); |
| 367 | |
| 368 | if (spicfg->odd_parity) |
| 369 | spifmt |= SPIFMT_ODD_PARITY_MASK; |
| 370 | |
| 371 | if (spicfg->parity_enable) |
| 372 | spifmt |= SPIFMT_PARITYENA_MASK; |
| 373 | |
Brian Niebuhr | 7abbf23 | 2010-08-19 15:07:38 +0530 | [diff] [blame] | 374 | if (spicfg->timer_disable) { |
Brian Niebuhr | 25f3351 | 2010-08-19 12:15:22 +0530 | [diff] [blame] | 375 | spifmt |= SPIFMT_DISTIMER_MASK; |
Brian Niebuhr | 7abbf23 | 2010-08-19 15:07:38 +0530 | [diff] [blame] | 376 | } else { |
| 377 | delay |= (spicfg->c2tdelay << SPIDELAY_C2TDELAY_SHIFT) |
| 378 | & SPIDELAY_C2TDELAY_MASK; |
| 379 | delay |= (spicfg->t2cdelay << SPIDELAY_T2CDELAY_SHIFT) |
| 380 | & SPIDELAY_T2CDELAY_MASK; |
| 381 | } |
Brian Niebuhr | 25f3351 | 2010-08-19 12:15:22 +0530 | [diff] [blame] | 382 | |
Brian Niebuhr | 7abbf23 | 2010-08-19 15:07:38 +0530 | [diff] [blame] | 383 | if (spi->mode & SPI_READY) { |
Brian Niebuhr | 25f3351 | 2010-08-19 12:15:22 +0530 | [diff] [blame] | 384 | spifmt |= SPIFMT_WAITENA_MASK; |
Brian Niebuhr | 7abbf23 | 2010-08-19 15:07:38 +0530 | [diff] [blame] | 385 | delay |= (spicfg->t2edelay << SPIDELAY_T2EDELAY_SHIFT) |
| 386 | & SPIDELAY_T2EDELAY_MASK; |
| 387 | delay |= (spicfg->c2edelay << SPIDELAY_C2EDELAY_SHIFT) |
| 388 | & SPIDELAY_C2EDELAY_MASK; |
| 389 | } |
| 390 | |
| 391 | iowrite32(delay, davinci_spi->base + SPIDELAY); |
Brian Niebuhr | 25f3351 | 2010-08-19 12:15:22 +0530 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | iowrite32(spifmt, davinci_spi->base + SPIFMT0); |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 395 | |
| 396 | return 0; |
| 397 | } |
| 398 | |
| 399 | static void davinci_spi_dma_rx_callback(unsigned lch, u16 ch_status, void *data) |
| 400 | { |
| 401 | struct spi_device *spi = (struct spi_device *)data; |
| 402 | struct davinci_spi *davinci_spi; |
| 403 | struct davinci_spi_dma *davinci_spi_dma; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 404 | |
| 405 | davinci_spi = spi_master_get_devdata(spi->master); |
| 406 | davinci_spi_dma = &(davinci_spi->dma_channels[spi->chip_select]); |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 407 | |
| 408 | if (ch_status == DMA_COMPLETE) |
| 409 | edma_stop(davinci_spi_dma->dma_rx_channel); |
| 410 | else |
| 411 | edma_clean_channel(davinci_spi_dma->dma_rx_channel); |
| 412 | |
| 413 | complete(&davinci_spi_dma->dma_rx_completion); |
| 414 | /* We must disable the DMA RX request */ |
| 415 | davinci_spi_set_dma_req(spi, 0); |
| 416 | } |
| 417 | |
| 418 | static void davinci_spi_dma_tx_callback(unsigned lch, u16 ch_status, void *data) |
| 419 | { |
| 420 | struct spi_device *spi = (struct spi_device *)data; |
| 421 | struct davinci_spi *davinci_spi; |
| 422 | struct davinci_spi_dma *davinci_spi_dma; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 423 | |
| 424 | davinci_spi = spi_master_get_devdata(spi->master); |
| 425 | davinci_spi_dma = &(davinci_spi->dma_channels[spi->chip_select]); |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 426 | |
| 427 | if (ch_status == DMA_COMPLETE) |
| 428 | edma_stop(davinci_spi_dma->dma_tx_channel); |
| 429 | else |
| 430 | edma_clean_channel(davinci_spi_dma->dma_tx_channel); |
| 431 | |
| 432 | complete(&davinci_spi_dma->dma_tx_completion); |
| 433 | /* We must disable the DMA TX request */ |
| 434 | davinci_spi_set_dma_req(spi, 0); |
| 435 | } |
| 436 | |
| 437 | static int davinci_spi_request_dma(struct spi_device *spi) |
| 438 | { |
| 439 | struct davinci_spi *davinci_spi; |
| 440 | struct davinci_spi_dma *davinci_spi_dma; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 441 | struct device *sdev; |
| 442 | int r; |
| 443 | |
| 444 | davinci_spi = spi_master_get_devdata(spi->master); |
| 445 | davinci_spi_dma = &davinci_spi->dma_channels[spi->chip_select]; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 446 | sdev = davinci_spi->bitbang.master->dev.parent; |
| 447 | |
| 448 | r = edma_alloc_channel(davinci_spi_dma->dma_rx_sync_dev, |
| 449 | davinci_spi_dma_rx_callback, spi, |
| 450 | davinci_spi_dma->eventq); |
| 451 | if (r < 0) { |
| 452 | dev_dbg(sdev, "Unable to request DMA channel for SPI RX\n"); |
| 453 | return -EAGAIN; |
| 454 | } |
| 455 | davinci_spi_dma->dma_rx_channel = r; |
| 456 | r = edma_alloc_channel(davinci_spi_dma->dma_tx_sync_dev, |
| 457 | davinci_spi_dma_tx_callback, spi, |
| 458 | davinci_spi_dma->eventq); |
| 459 | if (r < 0) { |
| 460 | edma_free_channel(davinci_spi_dma->dma_rx_channel); |
| 461 | davinci_spi_dma->dma_rx_channel = -1; |
| 462 | dev_dbg(sdev, "Unable to request DMA channel for SPI TX\n"); |
| 463 | return -EAGAIN; |
| 464 | } |
| 465 | davinci_spi_dma->dma_tx_channel = r; |
| 466 | |
| 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * davinci_spi_setup - This functions will set default transfer method |
| 472 | * @spi: spi device on which data transfer to be done |
| 473 | * |
| 474 | * This functions sets the default transfer method. |
| 475 | */ |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 476 | static int davinci_spi_setup(struct spi_device *spi) |
| 477 | { |
| 478 | int retval; |
| 479 | struct davinci_spi *davinci_spi; |
| 480 | struct davinci_spi_dma *davinci_spi_dma; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 481 | |
| 482 | davinci_spi = spi_master_get_devdata(spi->master); |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 483 | |
| 484 | /* if bits per word length is zero then set it default 8 */ |
| 485 | if (!spi->bits_per_word) |
| 486 | spi->bits_per_word = 8; |
| 487 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 488 | if (use_dma && davinci_spi->dma_channels) { |
| 489 | davinci_spi_dma = &davinci_spi->dma_channels[spi->chip_select]; |
| 490 | |
| 491 | if ((davinci_spi_dma->dma_rx_channel == -1) |
| 492 | || (davinci_spi_dma->dma_tx_channel == -1)) { |
| 493 | retval = davinci_spi_request_dma(spi); |
| 494 | if (retval < 0) |
| 495 | return retval; |
| 496 | } |
| 497 | } |
| 498 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 499 | retval = davinci_spi_setup_transfer(spi, NULL); |
| 500 | |
| 501 | return retval; |
| 502 | } |
| 503 | |
| 504 | static void davinci_spi_cleanup(struct spi_device *spi) |
| 505 | { |
| 506 | struct davinci_spi *davinci_spi = spi_master_get_devdata(spi->master); |
| 507 | struct davinci_spi_dma *davinci_spi_dma; |
| 508 | |
| 509 | davinci_spi_dma = &davinci_spi->dma_channels[spi->chip_select]; |
| 510 | |
| 511 | if (use_dma && davinci_spi->dma_channels) { |
| 512 | davinci_spi_dma = &davinci_spi->dma_channels[spi->chip_select]; |
| 513 | |
| 514 | if ((davinci_spi_dma->dma_rx_channel != -1) |
| 515 | && (davinci_spi_dma->dma_tx_channel != -1)) { |
| 516 | edma_free_channel(davinci_spi_dma->dma_tx_channel); |
| 517 | edma_free_channel(davinci_spi_dma->dma_rx_channel); |
| 518 | } |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | static int davinci_spi_bufs_prep(struct spi_device *spi, |
| 523 | struct davinci_spi *davinci_spi) |
| 524 | { |
Brian Niebuhr | 2385397 | 2010-08-13 10:57:44 +0530 | [diff] [blame] | 525 | struct davinci_spi_platform_data *pdata; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 526 | int op_mode = 0; |
| 527 | |
| 528 | /* |
| 529 | * REVISIT unless devices disagree about SPI_LOOP or |
| 530 | * SPI_READY (SPI_NO_CS only allows one device!), this |
| 531 | * should not need to be done before each message... |
| 532 | * optimize for both flags staying cleared. |
| 533 | */ |
| 534 | |
| 535 | op_mode = SPIPC0_DIFUN_MASK |
| 536 | | SPIPC0_DOFUN_MASK |
| 537 | | SPIPC0_CLKFUN_MASK; |
Brian Niebuhr | 2385397 | 2010-08-13 10:57:44 +0530 | [diff] [blame] | 538 | if (!(spi->mode & SPI_NO_CS)) { |
| 539 | pdata = davinci_spi->pdata; |
| 540 | if (!pdata->chip_sel || |
| 541 | pdata->chip_sel[spi->chip_select] == SPI_INTERN_CS) |
| 542 | op_mode |= 1 << spi->chip_select; |
| 543 | } |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 544 | if (spi->mode & SPI_READY) |
| 545 | op_mode |= SPIPC0_SPIENA_MASK; |
| 546 | |
| 547 | iowrite32(op_mode, davinci_spi->base + SPIPC0); |
| 548 | |
| 549 | if (spi->mode & SPI_LOOP) |
| 550 | set_io_bits(davinci_spi->base + SPIGCR1, |
| 551 | SPIGCR1_LOOPBACK_MASK); |
| 552 | else |
| 553 | clear_io_bits(davinci_spi->base + SPIGCR1, |
| 554 | SPIGCR1_LOOPBACK_MASK); |
| 555 | |
| 556 | return 0; |
| 557 | } |
| 558 | |
| 559 | static int davinci_spi_check_error(struct davinci_spi *davinci_spi, |
| 560 | int int_status) |
| 561 | { |
| 562 | struct device *sdev = davinci_spi->bitbang.master->dev.parent; |
| 563 | |
| 564 | if (int_status & SPIFLG_TIMEOUT_MASK) { |
| 565 | dev_dbg(sdev, "SPI Time-out Error\n"); |
| 566 | return -ETIMEDOUT; |
| 567 | } |
| 568 | if (int_status & SPIFLG_DESYNC_MASK) { |
| 569 | dev_dbg(sdev, "SPI Desynchronization Error\n"); |
| 570 | return -EIO; |
| 571 | } |
| 572 | if (int_status & SPIFLG_BITERR_MASK) { |
| 573 | dev_dbg(sdev, "SPI Bit error\n"); |
| 574 | return -EIO; |
| 575 | } |
| 576 | |
| 577 | if (davinci_spi->version == SPI_VERSION_2) { |
| 578 | if (int_status & SPIFLG_DLEN_ERR_MASK) { |
| 579 | dev_dbg(sdev, "SPI Data Length Error\n"); |
| 580 | return -EIO; |
| 581 | } |
| 582 | if (int_status & SPIFLG_PARERR_MASK) { |
| 583 | dev_dbg(sdev, "SPI Parity Error\n"); |
| 584 | return -EIO; |
| 585 | } |
| 586 | if (int_status & SPIFLG_OVRRUN_MASK) { |
| 587 | dev_dbg(sdev, "SPI Data Overrun error\n"); |
| 588 | return -EIO; |
| 589 | } |
| 590 | if (int_status & SPIFLG_TX_INTR_MASK) { |
| 591 | dev_dbg(sdev, "SPI TX intr bit set\n"); |
| 592 | return -EIO; |
| 593 | } |
| 594 | if (int_status & SPIFLG_BUF_INIT_ACTIVE_MASK) { |
| 595 | dev_dbg(sdev, "SPI Buffer Init Active\n"); |
| 596 | return -EBUSY; |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | /** |
| 604 | * davinci_spi_bufs - functions which will handle transfer data |
| 605 | * @spi: spi device on which data transfer to be done |
| 606 | * @t: spi transfer in which transfer info is filled |
| 607 | * |
| 608 | * This function will put data to be transferred into data register |
| 609 | * of SPI controller and then wait until the completion will be marked |
| 610 | * by the IRQ Handler. |
| 611 | */ |
| 612 | static int davinci_spi_bufs_pio(struct spi_device *spi, struct spi_transfer *t) |
| 613 | { |
| 614 | struct davinci_spi *davinci_spi; |
| 615 | int int_status, count, ret; |
Brian Niebuhr | 7978b8c | 2010-08-13 10:11:03 +0530 | [diff] [blame] | 616 | u8 conv; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 617 | u32 tx_data, data1_reg_val; |
| 618 | u32 buf_val, flg_val; |
| 619 | struct davinci_spi_platform_data *pdata; |
| 620 | |
| 621 | davinci_spi = spi_master_get_devdata(spi->master); |
| 622 | pdata = davinci_spi->pdata; |
| 623 | |
| 624 | davinci_spi->tx = t->tx_buf; |
| 625 | davinci_spi->rx = t->rx_buf; |
| 626 | |
| 627 | /* convert len to words based on bits_per_word */ |
Brian Niebuhr | cda987e | 2010-08-19 16:16:28 +0530 | [diff] [blame] | 628 | conv = davinci_spi->bytes_per_word[spi->chip_select]; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 629 | davinci_spi->count = t->len / conv; |
| 630 | |
Brian Niebuhr | 7978b8c | 2010-08-13 10:11:03 +0530 | [diff] [blame] | 631 | data1_reg_val = ioread32(davinci_spi->base + SPIDAT1); |
| 632 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 633 | INIT_COMPLETION(davinci_spi->done); |
| 634 | |
| 635 | ret = davinci_spi_bufs_prep(spi, davinci_spi); |
| 636 | if (ret) |
| 637 | return ret; |
| 638 | |
| 639 | /* Enable SPI */ |
| 640 | set_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_SPIENA_MASK); |
| 641 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 642 | count = davinci_spi->count; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 643 | |
| 644 | /* Determine the command to execute READ or WRITE */ |
| 645 | if (t->tx_buf) { |
| 646 | clear_io_bits(davinci_spi->base + SPIINT, SPIINT_MASKALL); |
| 647 | |
| 648 | while (1) { |
| 649 | tx_data = davinci_spi->get_tx(davinci_spi); |
| 650 | |
| 651 | data1_reg_val &= ~(0xFFFF); |
| 652 | data1_reg_val |= (0xFFFF & tx_data); |
| 653 | |
| 654 | buf_val = ioread32(davinci_spi->base + SPIBUF); |
| 655 | if ((buf_val & SPIBUF_TXFULL_MASK) == 0) { |
| 656 | iowrite32(data1_reg_val, |
| 657 | davinci_spi->base + SPIDAT1); |
| 658 | |
| 659 | count--; |
| 660 | } |
| 661 | while (ioread32(davinci_spi->base + SPIBUF) |
| 662 | & SPIBUF_RXEMPTY_MASK) |
| 663 | cpu_relax(); |
| 664 | |
| 665 | /* getting the returned byte */ |
| 666 | if (t->rx_buf) { |
| 667 | buf_val = ioread32(davinci_spi->base + SPIBUF); |
| 668 | davinci_spi->get_rx(buf_val, davinci_spi); |
| 669 | } |
| 670 | if (count <= 0) |
| 671 | break; |
| 672 | } |
| 673 | } else { |
| 674 | if (pdata->poll_mode) { |
| 675 | while (1) { |
| 676 | /* keeps the serial clock going */ |
| 677 | if ((ioread32(davinci_spi->base + SPIBUF) |
| 678 | & SPIBUF_TXFULL_MASK) == 0) |
| 679 | iowrite32(data1_reg_val, |
| 680 | davinci_spi->base + SPIDAT1); |
| 681 | |
| 682 | while (ioread32(davinci_spi->base + SPIBUF) & |
| 683 | SPIBUF_RXEMPTY_MASK) |
| 684 | cpu_relax(); |
| 685 | |
| 686 | flg_val = ioread32(davinci_spi->base + SPIFLG); |
| 687 | buf_val = ioread32(davinci_spi->base + SPIBUF); |
| 688 | |
| 689 | davinci_spi->get_rx(buf_val, davinci_spi); |
| 690 | |
| 691 | count--; |
| 692 | if (count <= 0) |
| 693 | break; |
| 694 | } |
| 695 | } else { /* Receive in Interrupt mode */ |
| 696 | int i; |
| 697 | |
| 698 | for (i = 0; i < davinci_spi->count; i++) { |
| 699 | set_io_bits(davinci_spi->base + SPIINT, |
| 700 | SPIINT_BITERR_INTR |
| 701 | | SPIINT_OVRRUN_INTR |
| 702 | | SPIINT_RX_INTR); |
| 703 | |
| 704 | iowrite32(data1_reg_val, |
| 705 | davinci_spi->base + SPIDAT1); |
| 706 | |
| 707 | while (ioread32(davinci_spi->base + SPIINT) & |
| 708 | SPIINT_RX_INTR) |
| 709 | cpu_relax(); |
| 710 | } |
| 711 | iowrite32((data1_reg_val & 0x0ffcffff), |
| 712 | davinci_spi->base + SPIDAT1); |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | /* |
| 717 | * Check for bit error, desync error,parity error,timeout error and |
| 718 | * receive overflow errors |
| 719 | */ |
| 720 | int_status = ioread32(davinci_spi->base + SPIFLG); |
| 721 | |
| 722 | ret = davinci_spi_check_error(davinci_spi, int_status); |
| 723 | if (ret != 0) |
| 724 | return ret; |
| 725 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 726 | return t->len; |
| 727 | } |
| 728 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 729 | static int davinci_spi_bufs_dma(struct spi_device *spi, struct spi_transfer *t) |
| 730 | { |
| 731 | struct davinci_spi *davinci_spi; |
| 732 | int int_status = 0; |
| 733 | int count, temp_count; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 734 | u32 data1_reg_val; |
| 735 | struct davinci_spi_dma *davinci_spi_dma; |
Brian Niebuhr | b7ab24a | 2010-08-19 16:42:42 +0530 | [diff] [blame^] | 736 | int data_type, ret; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 737 | unsigned long tx_reg, rx_reg; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 738 | struct device *sdev; |
| 739 | |
| 740 | davinci_spi = spi_master_get_devdata(spi->master); |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 741 | sdev = davinci_spi->bitbang.master->dev.parent; |
| 742 | |
| 743 | davinci_spi_dma = &davinci_spi->dma_channels[spi->chip_select]; |
| 744 | |
| 745 | tx_reg = (unsigned long)davinci_spi->pbase + SPIDAT1; |
| 746 | rx_reg = (unsigned long)davinci_spi->pbase + SPIBUF; |
| 747 | |
| 748 | davinci_spi->tx = t->tx_buf; |
| 749 | davinci_spi->rx = t->rx_buf; |
| 750 | |
| 751 | /* convert len to words based on bits_per_word */ |
Brian Niebuhr | b7ab24a | 2010-08-19 16:42:42 +0530 | [diff] [blame^] | 752 | data_type = davinci_spi->bytes_per_word[spi->chip_select]; |
| 753 | davinci_spi->count = t->len / data_type; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 754 | |
Brian Niebuhr | 7978b8c | 2010-08-13 10:11:03 +0530 | [diff] [blame] | 755 | data1_reg_val = ioread32(davinci_spi->base + SPIDAT1); |
| 756 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 757 | INIT_COMPLETION(davinci_spi->done); |
| 758 | |
| 759 | init_completion(&davinci_spi_dma->dma_rx_completion); |
| 760 | init_completion(&davinci_spi_dma->dma_tx_completion); |
| 761 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 762 | ret = davinci_spi_bufs_prep(spi, davinci_spi); |
| 763 | if (ret) |
| 764 | return ret; |
| 765 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 766 | count = davinci_spi->count; /* the number of elements */ |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 767 | |
| 768 | /* disable all interrupts for dma transfers */ |
| 769 | clear_io_bits(davinci_spi->base + SPIINT, SPIINT_MASKALL); |
| 770 | /* Disable SPI to write configuration bits in SPIDAT */ |
| 771 | clear_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_SPIENA_MASK); |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 772 | /* Enable SPI */ |
| 773 | set_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_SPIENA_MASK); |
| 774 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 775 | if (t->tx_buf) { |
| 776 | t->tx_dma = dma_map_single(&spi->dev, (void *)t->tx_buf, count, |
| 777 | DMA_TO_DEVICE); |
| 778 | if (dma_mapping_error(&spi->dev, t->tx_dma)) { |
| 779 | dev_dbg(sdev, "Unable to DMA map a %d bytes" |
| 780 | " TX buffer\n", count); |
| 781 | return -ENOMEM; |
| 782 | } |
| 783 | temp_count = count; |
| 784 | } else { |
| 785 | /* We need TX clocking for RX transaction */ |
| 786 | t->tx_dma = dma_map_single(&spi->dev, |
| 787 | (void *)davinci_spi->tmp_buf, count + 1, |
| 788 | DMA_TO_DEVICE); |
| 789 | if (dma_mapping_error(&spi->dev, t->tx_dma)) { |
| 790 | dev_dbg(sdev, "Unable to DMA map a %d bytes" |
| 791 | " TX tmp buffer\n", count); |
| 792 | return -ENOMEM; |
| 793 | } |
| 794 | temp_count = count + 1; |
| 795 | } |
| 796 | |
| 797 | edma_set_transfer_params(davinci_spi_dma->dma_tx_channel, |
| 798 | data_type, temp_count, 1, 0, ASYNC); |
| 799 | edma_set_dest(davinci_spi_dma->dma_tx_channel, tx_reg, INCR, W8BIT); |
| 800 | edma_set_src(davinci_spi_dma->dma_tx_channel, t->tx_dma, INCR, W8BIT); |
| 801 | edma_set_src_index(davinci_spi_dma->dma_tx_channel, data_type, 0); |
| 802 | edma_set_dest_index(davinci_spi_dma->dma_tx_channel, 0, 0); |
| 803 | |
| 804 | if (t->rx_buf) { |
| 805 | /* initiate transaction */ |
| 806 | iowrite32(data1_reg_val, davinci_spi->base + SPIDAT1); |
| 807 | |
| 808 | t->rx_dma = dma_map_single(&spi->dev, (void *)t->rx_buf, count, |
| 809 | DMA_FROM_DEVICE); |
| 810 | if (dma_mapping_error(&spi->dev, t->rx_dma)) { |
| 811 | dev_dbg(sdev, "Couldn't DMA map a %d bytes RX buffer\n", |
| 812 | count); |
| 813 | if (t->tx_buf != NULL) |
| 814 | dma_unmap_single(NULL, t->tx_dma, |
| 815 | count, DMA_TO_DEVICE); |
| 816 | return -ENOMEM; |
| 817 | } |
| 818 | edma_set_transfer_params(davinci_spi_dma->dma_rx_channel, |
| 819 | data_type, count, 1, 0, ASYNC); |
| 820 | edma_set_src(davinci_spi_dma->dma_rx_channel, |
| 821 | rx_reg, INCR, W8BIT); |
| 822 | edma_set_dest(davinci_spi_dma->dma_rx_channel, |
| 823 | t->rx_dma, INCR, W8BIT); |
| 824 | edma_set_src_index(davinci_spi_dma->dma_rx_channel, 0, 0); |
| 825 | edma_set_dest_index(davinci_spi_dma->dma_rx_channel, |
| 826 | data_type, 0); |
| 827 | } |
| 828 | |
| 829 | if ((t->tx_buf) || (t->rx_buf)) |
| 830 | edma_start(davinci_spi_dma->dma_tx_channel); |
| 831 | |
| 832 | if (t->rx_buf) |
| 833 | edma_start(davinci_spi_dma->dma_rx_channel); |
| 834 | |
| 835 | if ((t->rx_buf) || (t->tx_buf)) |
| 836 | davinci_spi_set_dma_req(spi, 1); |
| 837 | |
| 838 | if (t->tx_buf) |
| 839 | wait_for_completion_interruptible( |
| 840 | &davinci_spi_dma->dma_tx_completion); |
| 841 | |
| 842 | if (t->rx_buf) |
| 843 | wait_for_completion_interruptible( |
| 844 | &davinci_spi_dma->dma_rx_completion); |
| 845 | |
| 846 | dma_unmap_single(NULL, t->tx_dma, temp_count, DMA_TO_DEVICE); |
| 847 | |
| 848 | if (t->rx_buf) |
| 849 | dma_unmap_single(NULL, t->rx_dma, count, DMA_FROM_DEVICE); |
| 850 | |
| 851 | /* |
| 852 | * Check for bit error, desync error,parity error,timeout error and |
| 853 | * receive overflow errors |
| 854 | */ |
| 855 | int_status = ioread32(davinci_spi->base + SPIFLG); |
| 856 | |
| 857 | ret = davinci_spi_check_error(davinci_spi, int_status); |
| 858 | if (ret != 0) |
| 859 | return ret; |
| 860 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 861 | return t->len; |
| 862 | } |
| 863 | |
| 864 | /** |
| 865 | * davinci_spi_irq - IRQ handler for DaVinci SPI |
| 866 | * @irq: IRQ number for this SPI Master |
| 867 | * @context_data: structure for SPI Master controller davinci_spi |
| 868 | */ |
| 869 | static irqreturn_t davinci_spi_irq(s32 irq, void *context_data) |
| 870 | { |
| 871 | struct davinci_spi *davinci_spi = context_data; |
| 872 | u32 int_status, rx_data = 0; |
| 873 | irqreturn_t ret = IRQ_NONE; |
| 874 | |
| 875 | int_status = ioread32(davinci_spi->base + SPIFLG); |
| 876 | |
| 877 | while ((int_status & SPIFLG_RX_INTR_MASK)) { |
| 878 | if (likely(int_status & SPIFLG_RX_INTR_MASK)) { |
| 879 | ret = IRQ_HANDLED; |
| 880 | |
| 881 | rx_data = ioread32(davinci_spi->base + SPIBUF); |
| 882 | davinci_spi->get_rx(rx_data, davinci_spi); |
| 883 | |
| 884 | /* Disable Receive Interrupt */ |
| 885 | iowrite32(~(SPIINT_RX_INTR | SPIINT_TX_INTR), |
| 886 | davinci_spi->base + SPIINT); |
| 887 | } else |
| 888 | (void)davinci_spi_check_error(davinci_spi, int_status); |
| 889 | |
| 890 | int_status = ioread32(davinci_spi->base + SPIFLG); |
| 891 | } |
| 892 | |
| 893 | return ret; |
| 894 | } |
| 895 | |
| 896 | /** |
| 897 | * davinci_spi_probe - probe function for SPI Master Controller |
| 898 | * @pdev: platform_device structure which contains plateform specific data |
| 899 | */ |
| 900 | static int davinci_spi_probe(struct platform_device *pdev) |
| 901 | { |
| 902 | struct spi_master *master; |
| 903 | struct davinci_spi *davinci_spi; |
| 904 | struct davinci_spi_platform_data *pdata; |
| 905 | struct resource *r, *mem; |
| 906 | resource_size_t dma_rx_chan = SPI_NO_RESOURCE; |
| 907 | resource_size_t dma_tx_chan = SPI_NO_RESOURCE; |
| 908 | resource_size_t dma_eventq = SPI_NO_RESOURCE; |
| 909 | int i = 0, ret = 0; |
| 910 | |
| 911 | pdata = pdev->dev.platform_data; |
| 912 | if (pdata == NULL) { |
| 913 | ret = -ENODEV; |
| 914 | goto err; |
| 915 | } |
| 916 | |
| 917 | master = spi_alloc_master(&pdev->dev, sizeof(struct davinci_spi)); |
| 918 | if (master == NULL) { |
| 919 | ret = -ENOMEM; |
| 920 | goto err; |
| 921 | } |
| 922 | |
| 923 | dev_set_drvdata(&pdev->dev, master); |
| 924 | |
| 925 | davinci_spi = spi_master_get_devdata(master); |
| 926 | if (davinci_spi == NULL) { |
| 927 | ret = -ENOENT; |
| 928 | goto free_master; |
| 929 | } |
| 930 | |
| 931 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 932 | if (r == NULL) { |
| 933 | ret = -ENOENT; |
| 934 | goto free_master; |
| 935 | } |
| 936 | |
| 937 | davinci_spi->pbase = r->start; |
| 938 | davinci_spi->region_size = resource_size(r); |
| 939 | davinci_spi->pdata = pdata; |
| 940 | |
| 941 | mem = request_mem_region(r->start, davinci_spi->region_size, |
| 942 | pdev->name); |
| 943 | if (mem == NULL) { |
| 944 | ret = -EBUSY; |
| 945 | goto free_master; |
| 946 | } |
| 947 | |
Sekhar Nori | 50356dd | 2010-10-08 15:27:26 +0530 | [diff] [blame] | 948 | davinci_spi->base = ioremap(r->start, davinci_spi->region_size); |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 949 | if (davinci_spi->base == NULL) { |
| 950 | ret = -ENOMEM; |
| 951 | goto release_region; |
| 952 | } |
| 953 | |
| 954 | davinci_spi->irq = platform_get_irq(pdev, 0); |
| 955 | if (davinci_spi->irq <= 0) { |
| 956 | ret = -EINVAL; |
| 957 | goto unmap_io; |
| 958 | } |
| 959 | |
| 960 | ret = request_irq(davinci_spi->irq, davinci_spi_irq, IRQF_DISABLED, |
| 961 | dev_name(&pdev->dev), davinci_spi); |
| 962 | if (ret) |
| 963 | goto unmap_io; |
| 964 | |
| 965 | /* Allocate tmp_buf for tx_buf */ |
| 966 | davinci_spi->tmp_buf = kzalloc(SPI_BUFSIZ, GFP_KERNEL); |
| 967 | if (davinci_spi->tmp_buf == NULL) { |
| 968 | ret = -ENOMEM; |
| 969 | goto irq_free; |
| 970 | } |
| 971 | |
| 972 | davinci_spi->bitbang.master = spi_master_get(master); |
| 973 | if (davinci_spi->bitbang.master == NULL) { |
| 974 | ret = -ENODEV; |
| 975 | goto free_tmp_buf; |
| 976 | } |
| 977 | |
| 978 | davinci_spi->clk = clk_get(&pdev->dev, NULL); |
| 979 | if (IS_ERR(davinci_spi->clk)) { |
| 980 | ret = -ENODEV; |
| 981 | goto put_master; |
| 982 | } |
| 983 | clk_enable(davinci_spi->clk); |
| 984 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 985 | master->bus_num = pdev->id; |
| 986 | master->num_chipselect = pdata->num_chipselect; |
| 987 | master->setup = davinci_spi_setup; |
| 988 | master->cleanup = davinci_spi_cleanup; |
| 989 | |
| 990 | davinci_spi->bitbang.chipselect = davinci_spi_chipselect; |
| 991 | davinci_spi->bitbang.setup_transfer = davinci_spi_setup_transfer; |
| 992 | |
| 993 | davinci_spi->version = pdata->version; |
| 994 | use_dma = pdata->use_dma; |
| 995 | |
| 996 | davinci_spi->bitbang.flags = SPI_NO_CS | SPI_LSB_FIRST | SPI_LOOP; |
| 997 | if (davinci_spi->version == SPI_VERSION_2) |
| 998 | davinci_spi->bitbang.flags |= SPI_READY; |
| 999 | |
| 1000 | if (use_dma) { |
Brian Niebuhr | 778e261 | 2010-09-03 15:15:06 +0530 | [diff] [blame] | 1001 | r = platform_get_resource(pdev, IORESOURCE_DMA, 0); |
| 1002 | if (r) |
| 1003 | dma_rx_chan = r->start; |
| 1004 | r = platform_get_resource(pdev, IORESOURCE_DMA, 1); |
| 1005 | if (r) |
| 1006 | dma_tx_chan = r->start; |
| 1007 | r = platform_get_resource(pdev, IORESOURCE_DMA, 2); |
| 1008 | if (r) |
| 1009 | dma_eventq = r->start; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 1010 | } |
| 1011 | |
| 1012 | if (!use_dma || |
| 1013 | dma_rx_chan == SPI_NO_RESOURCE || |
| 1014 | dma_tx_chan == SPI_NO_RESOURCE || |
| 1015 | dma_eventq == SPI_NO_RESOURCE) { |
| 1016 | davinci_spi->bitbang.txrx_bufs = davinci_spi_bufs_pio; |
| 1017 | use_dma = 0; |
| 1018 | } else { |
| 1019 | davinci_spi->bitbang.txrx_bufs = davinci_spi_bufs_dma; |
| 1020 | davinci_spi->dma_channels = kzalloc(master->num_chipselect |
| 1021 | * sizeof(struct davinci_spi_dma), GFP_KERNEL); |
| 1022 | if (davinci_spi->dma_channels == NULL) { |
| 1023 | ret = -ENOMEM; |
| 1024 | goto free_clk; |
| 1025 | } |
| 1026 | |
| 1027 | for (i = 0; i < master->num_chipselect; i++) { |
| 1028 | davinci_spi->dma_channels[i].dma_rx_channel = -1; |
| 1029 | davinci_spi->dma_channels[i].dma_rx_sync_dev = |
| 1030 | dma_rx_chan; |
| 1031 | davinci_spi->dma_channels[i].dma_tx_channel = -1; |
| 1032 | davinci_spi->dma_channels[i].dma_tx_sync_dev = |
| 1033 | dma_tx_chan; |
| 1034 | davinci_spi->dma_channels[i].eventq = dma_eventq; |
| 1035 | } |
| 1036 | dev_info(&pdev->dev, "DaVinci SPI driver in EDMA mode\n" |
| 1037 | "Using RX channel = %d , TX channel = %d and " |
| 1038 | "event queue = %d", dma_rx_chan, dma_tx_chan, |
| 1039 | dma_eventq); |
| 1040 | } |
| 1041 | |
| 1042 | davinci_spi->get_rx = davinci_spi_rx_buf_u8; |
| 1043 | davinci_spi->get_tx = davinci_spi_tx_buf_u8; |
| 1044 | |
| 1045 | init_completion(&davinci_spi->done); |
| 1046 | |
| 1047 | /* Reset In/OUT SPI module */ |
| 1048 | iowrite32(0, davinci_spi->base + SPIGCR0); |
| 1049 | udelay(100); |
| 1050 | iowrite32(1, davinci_spi->base + SPIGCR0); |
| 1051 | |
Brian Niebuhr | 2385397 | 2010-08-13 10:57:44 +0530 | [diff] [blame] | 1052 | /* initialize chip selects */ |
| 1053 | if (pdata->chip_sel) { |
| 1054 | for (i = 0; i < pdata->num_chipselect; i++) { |
| 1055 | if (pdata->chip_sel[i] != SPI_INTERN_CS) |
| 1056 | gpio_direction_output(pdata->chip_sel[i], 1); |
| 1057 | } |
| 1058 | } |
| 1059 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 1060 | /* Clock internal */ |
| 1061 | if (davinci_spi->pdata->clk_internal) |
| 1062 | set_io_bits(davinci_spi->base + SPIGCR1, |
| 1063 | SPIGCR1_CLKMOD_MASK); |
| 1064 | else |
| 1065 | clear_io_bits(davinci_spi->base + SPIGCR1, |
| 1066 | SPIGCR1_CLKMOD_MASK); |
| 1067 | |
Brian Niebuhr | 843a713 | 2010-08-12 12:49:05 +0530 | [diff] [blame] | 1068 | iowrite32(CS_DEFAULT, davinci_spi->base + SPIDEF); |
| 1069 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 1070 | /* master mode default */ |
| 1071 | set_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_MASTER_MASK); |
| 1072 | |
| 1073 | if (davinci_spi->pdata->intr_level) |
| 1074 | iowrite32(SPI_INTLVL_1, davinci_spi->base + SPILVL); |
| 1075 | else |
| 1076 | iowrite32(SPI_INTLVL_0, davinci_spi->base + SPILVL); |
| 1077 | |
| 1078 | ret = spi_bitbang_start(&davinci_spi->bitbang); |
| 1079 | if (ret) |
| 1080 | goto free_clk; |
| 1081 | |
Brian Niebuhr | 3b740b1 | 2010-09-03 14:50:07 +0530 | [diff] [blame] | 1082 | dev_info(&pdev->dev, "Controller at 0x%p\n", davinci_spi->base); |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 1083 | |
| 1084 | if (!pdata->poll_mode) |
| 1085 | dev_info(&pdev->dev, "Operating in interrupt mode" |
| 1086 | " using IRQ %d\n", davinci_spi->irq); |
| 1087 | |
| 1088 | return ret; |
| 1089 | |
| 1090 | free_clk: |
| 1091 | clk_disable(davinci_spi->clk); |
| 1092 | clk_put(davinci_spi->clk); |
| 1093 | put_master: |
| 1094 | spi_master_put(master); |
| 1095 | free_tmp_buf: |
| 1096 | kfree(davinci_spi->tmp_buf); |
| 1097 | irq_free: |
| 1098 | free_irq(davinci_spi->irq, davinci_spi); |
| 1099 | unmap_io: |
| 1100 | iounmap(davinci_spi->base); |
| 1101 | release_region: |
| 1102 | release_mem_region(davinci_spi->pbase, davinci_spi->region_size); |
| 1103 | free_master: |
| 1104 | kfree(master); |
| 1105 | err: |
| 1106 | return ret; |
| 1107 | } |
| 1108 | |
| 1109 | /** |
| 1110 | * davinci_spi_remove - remove function for SPI Master Controller |
| 1111 | * @pdev: platform_device structure which contains plateform specific data |
| 1112 | * |
| 1113 | * This function will do the reverse action of davinci_spi_probe function |
| 1114 | * It will free the IRQ and SPI controller's memory region. |
| 1115 | * It will also call spi_bitbang_stop to destroy the work queue which was |
| 1116 | * created by spi_bitbang_start. |
| 1117 | */ |
| 1118 | static int __exit davinci_spi_remove(struct platform_device *pdev) |
| 1119 | { |
| 1120 | struct davinci_spi *davinci_spi; |
| 1121 | struct spi_master *master; |
| 1122 | |
| 1123 | master = dev_get_drvdata(&pdev->dev); |
| 1124 | davinci_spi = spi_master_get_devdata(master); |
| 1125 | |
| 1126 | spi_bitbang_stop(&davinci_spi->bitbang); |
| 1127 | |
| 1128 | clk_disable(davinci_spi->clk); |
| 1129 | clk_put(davinci_spi->clk); |
| 1130 | spi_master_put(master); |
| 1131 | kfree(davinci_spi->tmp_buf); |
| 1132 | free_irq(davinci_spi->irq, davinci_spi); |
| 1133 | iounmap(davinci_spi->base); |
| 1134 | release_mem_region(davinci_spi->pbase, davinci_spi->region_size); |
| 1135 | |
| 1136 | return 0; |
| 1137 | } |
| 1138 | |
| 1139 | static struct platform_driver davinci_spi_driver = { |
| 1140 | .driver.name = "spi_davinci", |
| 1141 | .remove = __exit_p(davinci_spi_remove), |
| 1142 | }; |
| 1143 | |
| 1144 | static int __init davinci_spi_init(void) |
| 1145 | { |
| 1146 | return platform_driver_probe(&davinci_spi_driver, davinci_spi_probe); |
| 1147 | } |
| 1148 | module_init(davinci_spi_init); |
| 1149 | |
| 1150 | static void __exit davinci_spi_exit(void) |
| 1151 | { |
| 1152 | platform_driver_unregister(&davinci_spi_driver); |
| 1153 | } |
| 1154 | module_exit(davinci_spi_exit); |
| 1155 | |
| 1156 | MODULE_DESCRIPTION("TI DaVinci SPI Master Controller Driver"); |
| 1157 | MODULE_LICENSE("GPL"); |