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