Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs |
| 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., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/device.h> |
| 22 | #include <linux/ioport.h> |
| 23 | #include <linux/errno.h> |
| 24 | #include <linux/interrupt.h> |
| 25 | #include <linux/platform_device.h> |
| 26 | #include <linux/dma-mapping.h> |
| 27 | #include <linux/spi/spi.h> |
| 28 | #include <linux/workqueue.h> |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 29 | #include <linux/delay.h> |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 30 | #include <linux/clk.h> |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 31 | #include <linux/gpio.h> |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 32 | |
| 33 | #include <asm/io.h> |
| 34 | #include <asm/irq.h> |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 35 | #include <asm/delay.h> |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 36 | |
Russell King | dcea83a | 2008-11-29 11:40:28 +0000 | [diff] [blame] | 37 | #include <mach/dma.h> |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 38 | #include <mach/regs-ssp.h> |
| 39 | #include <mach/ssp.h> |
| 40 | #include <mach/pxa2xx_spi.h> |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 41 | |
| 42 | MODULE_AUTHOR("Stephen Street"); |
Will Newton | 037cdaf | 2007-12-10 15:49:25 -0800 | [diff] [blame] | 43 | MODULE_DESCRIPTION("PXA2xx SSP SPI Controller"); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 44 | MODULE_LICENSE("GPL"); |
Kay Sievers | 7e38c3c | 2008-04-10 21:29:20 -0700 | [diff] [blame] | 45 | MODULE_ALIAS("platform:pxa2xx-spi"); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 46 | |
| 47 | #define MAX_BUSES 3 |
| 48 | |
Vernon Sauder | f1f640a | 2008-10-15 22:02:43 -0700 | [diff] [blame] | 49 | #define RX_THRESH_DFLT 8 |
| 50 | #define TX_THRESH_DFLT 8 |
| 51 | #define TIMOUT_DFLT 1000 |
| 52 | |
Ned Forrester | 7e96445 | 2008-09-13 02:33:18 -0700 | [diff] [blame] | 53 | #define DMA_INT_MASK (DCSR_ENDINTR | DCSR_STARTINTR | DCSR_BUSERR) |
| 54 | #define RESET_DMA_CHANNEL (DCSR_NODESC | DMA_INT_MASK) |
Mike Rapoport | 20b918d | 2008-10-01 10:39:24 -0700 | [diff] [blame] | 55 | #define IS_DMA_ALIGNED(x) ((((u32)(x)) & 0x07) == 0) |
Ned Forrester | 7e96445 | 2008-09-13 02:33:18 -0700 | [diff] [blame] | 56 | #define MAX_DMA_LEN 8191 |
Mike Rapoport | 7ad0ba9 | 2009-04-06 19:00:57 -0700 | [diff] [blame] | 57 | #define DMA_ALIGNMENT 8 |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 58 | |
Ned Forrester | b97c74b | 2008-02-23 15:23:40 -0800 | [diff] [blame] | 59 | /* |
| 60 | * for testing SSCR1 changes that require SSP restart, basically |
| 61 | * everything except the service and interrupt enables, the pxa270 developer |
| 62 | * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this |
| 63 | * list, but the PXA255 dev man says all bits without really meaning the |
| 64 | * service and interrupt enables |
| 65 | */ |
| 66 | #define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \ |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 67 | | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \ |
Ned Forrester | b97c74b | 2008-02-23 15:23:40 -0800 | [diff] [blame] | 68 | | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \ |
| 69 | | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \ |
| 70 | | SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \ |
| 71 | | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM) |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 72 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 73 | #define DEFINE_SSP_REG(reg, off) \ |
David Brownell | cf43369 | 2008-04-28 02:14:17 -0700 | [diff] [blame] | 74 | static inline u32 read_##reg(void const __iomem *p) \ |
| 75 | { return __raw_readl(p + (off)); } \ |
| 76 | \ |
| 77 | static inline void write_##reg(u32 v, void __iomem *p) \ |
| 78 | { __raw_writel(v, p + (off)); } |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 79 | |
| 80 | DEFINE_SSP_REG(SSCR0, 0x00) |
| 81 | DEFINE_SSP_REG(SSCR1, 0x04) |
| 82 | DEFINE_SSP_REG(SSSR, 0x08) |
| 83 | DEFINE_SSP_REG(SSITR, 0x0c) |
| 84 | DEFINE_SSP_REG(SSDR, 0x10) |
| 85 | DEFINE_SSP_REG(SSTO, 0x28) |
| 86 | DEFINE_SSP_REG(SSPSP, 0x2c) |
| 87 | |
| 88 | #define START_STATE ((void*)0) |
| 89 | #define RUNNING_STATE ((void*)1) |
| 90 | #define DONE_STATE ((void*)2) |
| 91 | #define ERROR_STATE ((void*)-1) |
| 92 | |
| 93 | #define QUEUE_RUNNING 0 |
| 94 | #define QUEUE_STOPPED 1 |
| 95 | |
| 96 | struct driver_data { |
| 97 | /* Driver model hookup */ |
| 98 | struct platform_device *pdev; |
| 99 | |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 100 | /* SSP Info */ |
| 101 | struct ssp_device *ssp; |
| 102 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 103 | /* SPI framework hookup */ |
| 104 | enum pxa_ssp_type ssp_type; |
| 105 | struct spi_master *master; |
| 106 | |
| 107 | /* PXA hookup */ |
| 108 | struct pxa2xx_spi_master *master_info; |
| 109 | |
| 110 | /* DMA setup stuff */ |
| 111 | int rx_channel; |
| 112 | int tx_channel; |
| 113 | u32 *null_dma_buf; |
| 114 | |
| 115 | /* SSP register addresses */ |
David Brownell | cf43369 | 2008-04-28 02:14:17 -0700 | [diff] [blame] | 116 | void __iomem *ioaddr; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 117 | u32 ssdr_physical; |
| 118 | |
| 119 | /* SSP masks*/ |
| 120 | u32 dma_cr1; |
| 121 | u32 int_cr1; |
| 122 | u32 clear_sr; |
| 123 | u32 mask_sr; |
| 124 | |
| 125 | /* Driver message queue */ |
| 126 | struct workqueue_struct *workqueue; |
| 127 | struct work_struct pump_messages; |
| 128 | spinlock_t lock; |
| 129 | struct list_head queue; |
| 130 | int busy; |
| 131 | int run; |
| 132 | |
| 133 | /* Message Transfer pump */ |
| 134 | struct tasklet_struct pump_transfers; |
| 135 | |
| 136 | /* Current message transfer state info */ |
| 137 | struct spi_message* cur_msg; |
| 138 | struct spi_transfer* cur_transfer; |
| 139 | struct chip_data *cur_chip; |
| 140 | size_t len; |
| 141 | void *tx; |
| 142 | void *tx_end; |
| 143 | void *rx; |
| 144 | void *rx_end; |
| 145 | int dma_mapped; |
| 146 | dma_addr_t rx_dma; |
| 147 | dma_addr_t tx_dma; |
| 148 | size_t rx_map_len; |
| 149 | size_t tx_map_len; |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 150 | u8 n_bytes; |
| 151 | u32 dma_width; |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 152 | int (*write)(struct driver_data *drv_data); |
| 153 | int (*read)(struct driver_data *drv_data); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 154 | irqreturn_t (*transfer_handler)(struct driver_data *drv_data); |
| 155 | void (*cs_control)(u32 command); |
| 156 | }; |
| 157 | |
| 158 | struct chip_data { |
| 159 | u32 cr0; |
| 160 | u32 cr1; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 161 | u32 psp; |
| 162 | u32 timeout; |
| 163 | u8 n_bytes; |
| 164 | u32 dma_width; |
| 165 | u32 dma_burst_size; |
| 166 | u32 threshold; |
| 167 | u32 dma_threshold; |
| 168 | u8 enable_dma; |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 169 | u8 bits_per_word; |
| 170 | u32 speed_hz; |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 171 | int gpio_cs; |
| 172 | int gpio_cs_inverted; |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 173 | int (*write)(struct driver_data *drv_data); |
| 174 | int (*read)(struct driver_data *drv_data); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 175 | void (*cs_control)(u32 command); |
| 176 | }; |
| 177 | |
David Howells | 6d5aefb | 2006-12-05 19:36:26 +0000 | [diff] [blame] | 178 | static void pump_messages(struct work_struct *work); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 179 | |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 180 | static void cs_assert(struct driver_data *drv_data) |
| 181 | { |
| 182 | struct chip_data *chip = drv_data->cur_chip; |
| 183 | |
| 184 | if (chip->cs_control) { |
| 185 | chip->cs_control(PXA2XX_CS_ASSERT); |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | if (gpio_is_valid(chip->gpio_cs)) |
| 190 | gpio_set_value(chip->gpio_cs, chip->gpio_cs_inverted); |
| 191 | } |
| 192 | |
| 193 | static void cs_deassert(struct driver_data *drv_data) |
| 194 | { |
| 195 | struct chip_data *chip = drv_data->cur_chip; |
| 196 | |
| 197 | if (chip->cs_control) { |
Daniel Ribeiro | 2b2562d | 2009-04-08 22:48:03 -0300 | [diff] [blame] | 198 | chip->cs_control(PXA2XX_CS_DEASSERT); |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 199 | return; |
| 200 | } |
| 201 | |
| 202 | if (gpio_is_valid(chip->gpio_cs)) |
| 203 | gpio_set_value(chip->gpio_cs, !chip->gpio_cs_inverted); |
| 204 | } |
| 205 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 206 | static int flush(struct driver_data *drv_data) |
| 207 | { |
| 208 | unsigned long limit = loops_per_jiffy << 1; |
| 209 | |
David Brownell | cf43369 | 2008-04-28 02:14:17 -0700 | [diff] [blame] | 210 | void __iomem *reg = drv_data->ioaddr; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 211 | |
| 212 | do { |
| 213 | while (read_SSSR(reg) & SSSR_RNE) { |
| 214 | read_SSDR(reg); |
| 215 | } |
Roel Kluin | 306c68a | 2009-04-21 12:24:46 -0700 | [diff] [blame] | 216 | } while ((read_SSSR(reg) & SSSR_BSY) && --limit); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 217 | write_SSSR(SSSR_ROR, reg); |
| 218 | |
| 219 | return limit; |
| 220 | } |
| 221 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 222 | static int null_writer(struct driver_data *drv_data) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 223 | { |
David Brownell | cf43369 | 2008-04-28 02:14:17 -0700 | [diff] [blame] | 224 | void __iomem *reg = drv_data->ioaddr; |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 225 | u8 n_bytes = drv_data->n_bytes; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 226 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 227 | if (((read_SSSR(reg) & 0x00000f00) == 0x00000f00) |
| 228 | || (drv_data->tx == drv_data->tx_end)) |
| 229 | return 0; |
| 230 | |
| 231 | write_SSDR(0, reg); |
| 232 | drv_data->tx += n_bytes; |
| 233 | |
| 234 | return 1; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 235 | } |
| 236 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 237 | static int null_reader(struct driver_data *drv_data) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 238 | { |
David Brownell | cf43369 | 2008-04-28 02:14:17 -0700 | [diff] [blame] | 239 | void __iomem *reg = drv_data->ioaddr; |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 240 | u8 n_bytes = drv_data->n_bytes; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 241 | |
| 242 | while ((read_SSSR(reg) & SSSR_RNE) |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 243 | && (drv_data->rx < drv_data->rx_end)) { |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 244 | read_SSDR(reg); |
| 245 | drv_data->rx += n_bytes; |
| 246 | } |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 247 | |
| 248 | return drv_data->rx == drv_data->rx_end; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 249 | } |
| 250 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 251 | static int u8_writer(struct driver_data *drv_data) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 252 | { |
David Brownell | cf43369 | 2008-04-28 02:14:17 -0700 | [diff] [blame] | 253 | void __iomem *reg = drv_data->ioaddr; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 254 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 255 | if (((read_SSSR(reg) & 0x00000f00) == 0x00000f00) |
| 256 | || (drv_data->tx == drv_data->tx_end)) |
| 257 | return 0; |
| 258 | |
| 259 | write_SSDR(*(u8 *)(drv_data->tx), reg); |
| 260 | ++drv_data->tx; |
| 261 | |
| 262 | return 1; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 263 | } |
| 264 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 265 | static int u8_reader(struct driver_data *drv_data) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 266 | { |
David Brownell | cf43369 | 2008-04-28 02:14:17 -0700 | [diff] [blame] | 267 | void __iomem *reg = drv_data->ioaddr; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 268 | |
| 269 | while ((read_SSSR(reg) & SSSR_RNE) |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 270 | && (drv_data->rx < drv_data->rx_end)) { |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 271 | *(u8 *)(drv_data->rx) = read_SSDR(reg); |
| 272 | ++drv_data->rx; |
| 273 | } |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 274 | |
| 275 | return drv_data->rx == drv_data->rx_end; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 276 | } |
| 277 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 278 | static int u16_writer(struct driver_data *drv_data) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 279 | { |
David Brownell | cf43369 | 2008-04-28 02:14:17 -0700 | [diff] [blame] | 280 | void __iomem *reg = drv_data->ioaddr; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 281 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 282 | if (((read_SSSR(reg) & 0x00000f00) == 0x00000f00) |
| 283 | || (drv_data->tx == drv_data->tx_end)) |
| 284 | return 0; |
| 285 | |
| 286 | write_SSDR(*(u16 *)(drv_data->tx), reg); |
| 287 | drv_data->tx += 2; |
| 288 | |
| 289 | return 1; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 290 | } |
| 291 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 292 | static int u16_reader(struct driver_data *drv_data) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 293 | { |
David Brownell | cf43369 | 2008-04-28 02:14:17 -0700 | [diff] [blame] | 294 | void __iomem *reg = drv_data->ioaddr; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 295 | |
| 296 | while ((read_SSSR(reg) & SSSR_RNE) |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 297 | && (drv_data->rx < drv_data->rx_end)) { |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 298 | *(u16 *)(drv_data->rx) = read_SSDR(reg); |
| 299 | drv_data->rx += 2; |
| 300 | } |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 301 | |
| 302 | return drv_data->rx == drv_data->rx_end; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 303 | } |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 304 | |
| 305 | static int u32_writer(struct driver_data *drv_data) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 306 | { |
David Brownell | cf43369 | 2008-04-28 02:14:17 -0700 | [diff] [blame] | 307 | void __iomem *reg = drv_data->ioaddr; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 308 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 309 | if (((read_SSSR(reg) & 0x00000f00) == 0x00000f00) |
| 310 | || (drv_data->tx == drv_data->tx_end)) |
| 311 | return 0; |
| 312 | |
| 313 | write_SSDR(*(u32 *)(drv_data->tx), reg); |
| 314 | drv_data->tx += 4; |
| 315 | |
| 316 | return 1; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 317 | } |
| 318 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 319 | static int u32_reader(struct driver_data *drv_data) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 320 | { |
David Brownell | cf43369 | 2008-04-28 02:14:17 -0700 | [diff] [blame] | 321 | void __iomem *reg = drv_data->ioaddr; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 322 | |
| 323 | while ((read_SSSR(reg) & SSSR_RNE) |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 324 | && (drv_data->rx < drv_data->rx_end)) { |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 325 | *(u32 *)(drv_data->rx) = read_SSDR(reg); |
| 326 | drv_data->rx += 4; |
| 327 | } |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 328 | |
| 329 | return drv_data->rx == drv_data->rx_end; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | static void *next_transfer(struct driver_data *drv_data) |
| 333 | { |
| 334 | struct spi_message *msg = drv_data->cur_msg; |
| 335 | struct spi_transfer *trans = drv_data->cur_transfer; |
| 336 | |
| 337 | /* Move to next transfer */ |
| 338 | if (trans->transfer_list.next != &msg->transfers) { |
| 339 | drv_data->cur_transfer = |
| 340 | list_entry(trans->transfer_list.next, |
| 341 | struct spi_transfer, |
| 342 | transfer_list); |
| 343 | return RUNNING_STATE; |
| 344 | } else |
| 345 | return DONE_STATE; |
| 346 | } |
| 347 | |
| 348 | static int map_dma_buffers(struct driver_data *drv_data) |
| 349 | { |
| 350 | struct spi_message *msg = drv_data->cur_msg; |
| 351 | struct device *dev = &msg->spi->dev; |
| 352 | |
| 353 | if (!drv_data->cur_chip->enable_dma) |
| 354 | return 0; |
| 355 | |
| 356 | if (msg->is_dma_mapped) |
| 357 | return drv_data->rx_dma && drv_data->tx_dma; |
| 358 | |
| 359 | if (!IS_DMA_ALIGNED(drv_data->rx) || !IS_DMA_ALIGNED(drv_data->tx)) |
| 360 | return 0; |
| 361 | |
| 362 | /* Modify setup if rx buffer is null */ |
| 363 | if (drv_data->rx == NULL) { |
| 364 | *drv_data->null_dma_buf = 0; |
| 365 | drv_data->rx = drv_data->null_dma_buf; |
| 366 | drv_data->rx_map_len = 4; |
| 367 | } else |
| 368 | drv_data->rx_map_len = drv_data->len; |
| 369 | |
| 370 | |
| 371 | /* Modify setup if tx buffer is null */ |
| 372 | if (drv_data->tx == NULL) { |
| 373 | *drv_data->null_dma_buf = 0; |
| 374 | drv_data->tx = drv_data->null_dma_buf; |
| 375 | drv_data->tx_map_len = 4; |
| 376 | } else |
| 377 | drv_data->tx_map_len = drv_data->len; |
| 378 | |
Ned Forrester | 393df74 | 2008-11-19 15:36:21 -0800 | [diff] [blame] | 379 | /* Stream map the tx buffer. Always do DMA_TO_DEVICE first |
| 380 | * so we flush the cache *before* invalidating it, in case |
| 381 | * the tx and rx buffers overlap. |
| 382 | */ |
| 383 | drv_data->tx_dma = dma_map_single(dev, drv_data->tx, |
| 384 | drv_data->tx_map_len, DMA_TO_DEVICE); |
| 385 | if (dma_mapping_error(dev, drv_data->tx_dma)) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 386 | return 0; |
| 387 | |
Ned Forrester | 393df74 | 2008-11-19 15:36:21 -0800 | [diff] [blame] | 388 | /* Stream map the rx buffer */ |
| 389 | drv_data->rx_dma = dma_map_single(dev, drv_data->rx, |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 390 | drv_data->rx_map_len, DMA_FROM_DEVICE); |
Ned Forrester | 393df74 | 2008-11-19 15:36:21 -0800 | [diff] [blame] | 391 | if (dma_mapping_error(dev, drv_data->rx_dma)) { |
| 392 | dma_unmap_single(dev, drv_data->tx_dma, |
| 393 | drv_data->tx_map_len, DMA_TO_DEVICE); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | return 1; |
| 398 | } |
| 399 | |
| 400 | static void unmap_dma_buffers(struct driver_data *drv_data) |
| 401 | { |
| 402 | struct device *dev; |
| 403 | |
| 404 | if (!drv_data->dma_mapped) |
| 405 | return; |
| 406 | |
| 407 | if (!drv_data->cur_msg->is_dma_mapped) { |
| 408 | dev = &drv_data->cur_msg->spi->dev; |
| 409 | dma_unmap_single(dev, drv_data->rx_dma, |
| 410 | drv_data->rx_map_len, DMA_FROM_DEVICE); |
| 411 | dma_unmap_single(dev, drv_data->tx_dma, |
| 412 | drv_data->tx_map_len, DMA_TO_DEVICE); |
| 413 | } |
| 414 | |
| 415 | drv_data->dma_mapped = 0; |
| 416 | } |
| 417 | |
| 418 | /* caller already set message->status; dma and pio irqs are blocked */ |
Stephen Street | 5daa3ba | 2006-05-20 15:00:19 -0700 | [diff] [blame] | 419 | static void giveback(struct driver_data *drv_data) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 420 | { |
| 421 | struct spi_transfer* last_transfer; |
Stephen Street | 5daa3ba | 2006-05-20 15:00:19 -0700 | [diff] [blame] | 422 | unsigned long flags; |
| 423 | struct spi_message *msg; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 424 | |
Stephen Street | 5daa3ba | 2006-05-20 15:00:19 -0700 | [diff] [blame] | 425 | spin_lock_irqsave(&drv_data->lock, flags); |
| 426 | msg = drv_data->cur_msg; |
| 427 | drv_data->cur_msg = NULL; |
| 428 | drv_data->cur_transfer = NULL; |
Stephen Street | 5daa3ba | 2006-05-20 15:00:19 -0700 | [diff] [blame] | 429 | queue_work(drv_data->workqueue, &drv_data->pump_messages); |
| 430 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 431 | |
| 432 | last_transfer = list_entry(msg->transfers.prev, |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 433 | struct spi_transfer, |
| 434 | transfer_list); |
| 435 | |
Ned Forrester | 8423597 | 2008-09-13 02:33:17 -0700 | [diff] [blame] | 436 | /* Delay if requested before any change in chip select */ |
| 437 | if (last_transfer->delay_usecs) |
| 438 | udelay(last_transfer->delay_usecs); |
| 439 | |
| 440 | /* Drop chip select UNLESS cs_change is true or we are returning |
| 441 | * a message with an error, or next message is for another chip |
| 442 | */ |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 443 | if (!last_transfer->cs_change) |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 444 | cs_deassert(drv_data); |
Ned Forrester | 8423597 | 2008-09-13 02:33:17 -0700 | [diff] [blame] | 445 | else { |
| 446 | struct spi_message *next_msg; |
| 447 | |
| 448 | /* Holding of cs was hinted, but we need to make sure |
| 449 | * the next message is for the same chip. Don't waste |
| 450 | * time with the following tests unless this was hinted. |
| 451 | * |
| 452 | * We cannot postpone this until pump_messages, because |
| 453 | * after calling msg->complete (below) the driver that |
| 454 | * sent the current message could be unloaded, which |
| 455 | * could invalidate the cs_control() callback... |
| 456 | */ |
| 457 | |
| 458 | /* get a pointer to the next message, if any */ |
| 459 | spin_lock_irqsave(&drv_data->lock, flags); |
| 460 | if (list_empty(&drv_data->queue)) |
| 461 | next_msg = NULL; |
| 462 | else |
| 463 | next_msg = list_entry(drv_data->queue.next, |
| 464 | struct spi_message, queue); |
| 465 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 466 | |
| 467 | /* see if the next and current messages point |
| 468 | * to the same chip |
| 469 | */ |
| 470 | if (next_msg && next_msg->spi != msg->spi) |
| 471 | next_msg = NULL; |
| 472 | if (!next_msg || msg->state == ERROR_STATE) |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 473 | cs_deassert(drv_data); |
Ned Forrester | 8423597 | 2008-09-13 02:33:17 -0700 | [diff] [blame] | 474 | } |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 475 | |
Stephen Street | 5daa3ba | 2006-05-20 15:00:19 -0700 | [diff] [blame] | 476 | msg->state = NULL; |
| 477 | if (msg->complete) |
| 478 | msg->complete(msg->context); |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 479 | |
| 480 | drv_data->cur_chip = NULL; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 481 | } |
| 482 | |
David Brownell | cf43369 | 2008-04-28 02:14:17 -0700 | [diff] [blame] | 483 | static int wait_ssp_rx_stall(void const __iomem *ioaddr) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 484 | { |
| 485 | unsigned long limit = loops_per_jiffy << 1; |
| 486 | |
Roel Kluin | 306c68a | 2009-04-21 12:24:46 -0700 | [diff] [blame] | 487 | while ((read_SSSR(ioaddr) & SSSR_BSY) && --limit) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 488 | cpu_relax(); |
| 489 | |
| 490 | return limit; |
| 491 | } |
| 492 | |
| 493 | static int wait_dma_channel_stop(int channel) |
| 494 | { |
| 495 | unsigned long limit = loops_per_jiffy << 1; |
| 496 | |
Roel Kluin | 306c68a | 2009-04-21 12:24:46 -0700 | [diff] [blame] | 497 | while (!(DCSR(channel) & DCSR_STOPSTATE) && --limit) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 498 | cpu_relax(); |
| 499 | |
| 500 | return limit; |
| 501 | } |
| 502 | |
David Brownell | cf43369 | 2008-04-28 02:14:17 -0700 | [diff] [blame] | 503 | static void dma_error_stop(struct driver_data *drv_data, const char *msg) |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 504 | { |
David Brownell | cf43369 | 2008-04-28 02:14:17 -0700 | [diff] [blame] | 505 | void __iomem *reg = drv_data->ioaddr; |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 506 | |
| 507 | /* Stop and reset */ |
| 508 | DCSR(drv_data->rx_channel) = RESET_DMA_CHANNEL; |
| 509 | DCSR(drv_data->tx_channel) = RESET_DMA_CHANNEL; |
| 510 | write_SSSR(drv_data->clear_sr, reg); |
| 511 | write_SSCR1(read_SSCR1(reg) & ~drv_data->dma_cr1, reg); |
| 512 | if (drv_data->ssp_type != PXA25x_SSP) |
| 513 | write_SSTO(0, reg); |
| 514 | flush(drv_data); |
| 515 | write_SSCR0(read_SSCR0(reg) & ~SSCR0_SSE, reg); |
| 516 | |
| 517 | unmap_dma_buffers(drv_data); |
| 518 | |
| 519 | dev_err(&drv_data->pdev->dev, "%s\n", msg); |
| 520 | |
| 521 | drv_data->cur_msg->state = ERROR_STATE; |
| 522 | tasklet_schedule(&drv_data->pump_transfers); |
| 523 | } |
| 524 | |
| 525 | static void dma_transfer_complete(struct driver_data *drv_data) |
| 526 | { |
David Brownell | cf43369 | 2008-04-28 02:14:17 -0700 | [diff] [blame] | 527 | void __iomem *reg = drv_data->ioaddr; |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 528 | struct spi_message *msg = drv_data->cur_msg; |
| 529 | |
| 530 | /* Clear and disable interrupts on SSP and DMA channels*/ |
| 531 | write_SSCR1(read_SSCR1(reg) & ~drv_data->dma_cr1, reg); |
| 532 | write_SSSR(drv_data->clear_sr, reg); |
| 533 | DCSR(drv_data->tx_channel) = RESET_DMA_CHANNEL; |
| 534 | DCSR(drv_data->rx_channel) = RESET_DMA_CHANNEL; |
| 535 | |
| 536 | if (wait_dma_channel_stop(drv_data->rx_channel) == 0) |
| 537 | dev_err(&drv_data->pdev->dev, |
| 538 | "dma_handler: dma rx channel stop failed\n"); |
| 539 | |
| 540 | if (wait_ssp_rx_stall(drv_data->ioaddr) == 0) |
| 541 | dev_err(&drv_data->pdev->dev, |
| 542 | "dma_transfer: ssp rx stall failed\n"); |
| 543 | |
| 544 | unmap_dma_buffers(drv_data); |
| 545 | |
| 546 | /* update the buffer pointer for the amount completed in dma */ |
| 547 | drv_data->rx += drv_data->len - |
| 548 | (DCMD(drv_data->rx_channel) & DCMD_LENGTH); |
| 549 | |
| 550 | /* read trailing data from fifo, it does not matter how many |
| 551 | * bytes are in the fifo just read until buffer is full |
| 552 | * or fifo is empty, which ever occurs first */ |
| 553 | drv_data->read(drv_data); |
| 554 | |
| 555 | /* return count of what was actually read */ |
| 556 | msg->actual_length += drv_data->len - |
| 557 | (drv_data->rx_end - drv_data->rx); |
| 558 | |
Ned Forrester | 8423597 | 2008-09-13 02:33:17 -0700 | [diff] [blame] | 559 | /* Transfer delays and chip select release are |
| 560 | * handled in pump_transfers or giveback |
| 561 | */ |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 562 | |
| 563 | /* Move to next transfer */ |
| 564 | msg->state = next_transfer(drv_data); |
| 565 | |
| 566 | /* Schedule transfer tasklet */ |
| 567 | tasklet_schedule(&drv_data->pump_transfers); |
| 568 | } |
| 569 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 570 | static void dma_handler(int channel, void *data) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 571 | { |
| 572 | struct driver_data *drv_data = data; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 573 | u32 irq_status = DCSR(channel) & DMA_INT_MASK; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 574 | |
| 575 | if (irq_status & DCSR_BUSERR) { |
| 576 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 577 | if (channel == drv_data->tx_channel) |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 578 | dma_error_stop(drv_data, |
| 579 | "dma_handler: " |
| 580 | "bad bus address on tx channel"); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 581 | else |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 582 | dma_error_stop(drv_data, |
| 583 | "dma_handler: " |
| 584 | "bad bus address on rx channel"); |
| 585 | return; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | /* PXA255x_SSP has no timeout interrupt, wait for tailing bytes */ |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 589 | if ((channel == drv_data->tx_channel) |
| 590 | && (irq_status & DCSR_ENDINTR) |
| 591 | && (drv_data->ssp_type == PXA25x_SSP)) { |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 592 | |
| 593 | /* Wait for rx to stall */ |
| 594 | if (wait_ssp_rx_stall(drv_data->ioaddr) == 0) |
| 595 | dev_err(&drv_data->pdev->dev, |
| 596 | "dma_handler: ssp rx stall failed\n"); |
| 597 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 598 | /* finish this transfer, start the next */ |
| 599 | dma_transfer_complete(drv_data); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | |
| 603 | static irqreturn_t dma_transfer(struct driver_data *drv_data) |
| 604 | { |
| 605 | u32 irq_status; |
David Brownell | cf43369 | 2008-04-28 02:14:17 -0700 | [diff] [blame] | 606 | void __iomem *reg = drv_data->ioaddr; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 607 | |
| 608 | irq_status = read_SSSR(reg) & drv_data->mask_sr; |
| 609 | if (irq_status & SSSR_ROR) { |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 610 | dma_error_stop(drv_data, "dma_transfer: fifo overrun"); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 611 | return IRQ_HANDLED; |
| 612 | } |
| 613 | |
| 614 | /* Check for false positive timeout */ |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 615 | if ((irq_status & SSSR_TINT) |
| 616 | && (DCSR(drv_data->tx_channel) & DCSR_RUN)) { |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 617 | write_SSSR(SSSR_TINT, reg); |
| 618 | return IRQ_HANDLED; |
| 619 | } |
| 620 | |
| 621 | if (irq_status & SSSR_TINT || drv_data->rx == drv_data->rx_end) { |
| 622 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 623 | /* Clear and disable timeout interrupt, do the rest in |
| 624 | * dma_transfer_complete */ |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 625 | if (drv_data->ssp_type != PXA25x_SSP) |
| 626 | write_SSTO(0, reg); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 627 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 628 | /* finish this transfer, start the next */ |
| 629 | dma_transfer_complete(drv_data); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 630 | |
| 631 | return IRQ_HANDLED; |
| 632 | } |
| 633 | |
| 634 | /* Opps problem detected */ |
| 635 | return IRQ_NONE; |
| 636 | } |
| 637 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 638 | static void int_error_stop(struct driver_data *drv_data, const char* msg) |
| 639 | { |
David Brownell | cf43369 | 2008-04-28 02:14:17 -0700 | [diff] [blame] | 640 | void __iomem *reg = drv_data->ioaddr; |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 641 | |
| 642 | /* Stop and reset SSP */ |
| 643 | write_SSSR(drv_data->clear_sr, reg); |
| 644 | write_SSCR1(read_SSCR1(reg) & ~drv_data->int_cr1, reg); |
| 645 | if (drv_data->ssp_type != PXA25x_SSP) |
| 646 | write_SSTO(0, reg); |
| 647 | flush(drv_data); |
| 648 | write_SSCR0(read_SSCR0(reg) & ~SSCR0_SSE, reg); |
| 649 | |
| 650 | dev_err(&drv_data->pdev->dev, "%s\n", msg); |
| 651 | |
| 652 | drv_data->cur_msg->state = ERROR_STATE; |
| 653 | tasklet_schedule(&drv_data->pump_transfers); |
| 654 | } |
| 655 | |
| 656 | static void int_transfer_complete(struct driver_data *drv_data) |
| 657 | { |
David Brownell | cf43369 | 2008-04-28 02:14:17 -0700 | [diff] [blame] | 658 | void __iomem *reg = drv_data->ioaddr; |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 659 | |
| 660 | /* Stop SSP */ |
| 661 | write_SSSR(drv_data->clear_sr, reg); |
| 662 | write_SSCR1(read_SSCR1(reg) & ~drv_data->int_cr1, reg); |
| 663 | if (drv_data->ssp_type != PXA25x_SSP) |
| 664 | write_SSTO(0, reg); |
| 665 | |
| 666 | /* Update total byte transfered return count actual bytes read */ |
| 667 | drv_data->cur_msg->actual_length += drv_data->len - |
| 668 | (drv_data->rx_end - drv_data->rx); |
| 669 | |
Ned Forrester | 8423597 | 2008-09-13 02:33:17 -0700 | [diff] [blame] | 670 | /* Transfer delays and chip select release are |
| 671 | * handled in pump_transfers or giveback |
| 672 | */ |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 673 | |
| 674 | /* Move to next transfer */ |
| 675 | drv_data->cur_msg->state = next_transfer(drv_data); |
| 676 | |
| 677 | /* Schedule transfer tasklet */ |
| 678 | tasklet_schedule(&drv_data->pump_transfers); |
| 679 | } |
| 680 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 681 | static irqreturn_t interrupt_transfer(struct driver_data *drv_data) |
| 682 | { |
David Brownell | cf43369 | 2008-04-28 02:14:17 -0700 | [diff] [blame] | 683 | void __iomem *reg = drv_data->ioaddr; |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 684 | |
Stephen Street | 5daa3ba | 2006-05-20 15:00:19 -0700 | [diff] [blame] | 685 | u32 irq_mask = (read_SSCR1(reg) & SSCR1_TIE) ? |
| 686 | drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 687 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 688 | u32 irq_status = read_SSSR(reg) & irq_mask; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 689 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 690 | if (irq_status & SSSR_ROR) { |
| 691 | int_error_stop(drv_data, "interrupt_transfer: fifo overrun"); |
| 692 | return IRQ_HANDLED; |
| 693 | } |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 694 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 695 | if (irq_status & SSSR_TINT) { |
| 696 | write_SSSR(SSSR_TINT, reg); |
| 697 | if (drv_data->read(drv_data)) { |
| 698 | int_transfer_complete(drv_data); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 699 | return IRQ_HANDLED; |
| 700 | } |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 701 | } |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 702 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 703 | /* Drain rx fifo, Fill tx fifo and prevent overruns */ |
| 704 | do { |
| 705 | if (drv_data->read(drv_data)) { |
| 706 | int_transfer_complete(drv_data); |
| 707 | return IRQ_HANDLED; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 708 | } |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 709 | } while (drv_data->write(drv_data)); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 710 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 711 | if (drv_data->read(drv_data)) { |
| 712 | int_transfer_complete(drv_data); |
| 713 | return IRQ_HANDLED; |
| 714 | } |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 715 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 716 | if (drv_data->tx == drv_data->tx_end) { |
| 717 | write_SSCR1(read_SSCR1(reg) & ~SSCR1_TIE, reg); |
| 718 | /* PXA25x_SSP has no timeout, read trailing bytes */ |
| 719 | if (drv_data->ssp_type == PXA25x_SSP) { |
| 720 | if (!wait_ssp_rx_stall(reg)) |
| 721 | { |
| 722 | int_error_stop(drv_data, "interrupt_transfer: " |
| 723 | "rx stall failed"); |
| 724 | return IRQ_HANDLED; |
| 725 | } |
| 726 | if (!drv_data->read(drv_data)) |
| 727 | { |
| 728 | int_error_stop(drv_data, |
| 729 | "interrupt_transfer: " |
| 730 | "trailing byte read failed"); |
| 731 | return IRQ_HANDLED; |
| 732 | } |
| 733 | int_transfer_complete(drv_data); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 734 | } |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 735 | } |
| 736 | |
Stephen Street | 5daa3ba | 2006-05-20 15:00:19 -0700 | [diff] [blame] | 737 | /* We did something */ |
| 738 | return IRQ_HANDLED; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 739 | } |
| 740 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 741 | static irqreturn_t ssp_int(int irq, void *dev_id) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 742 | { |
Jeff Garzik | c7bec5a | 2006-10-06 15:00:58 -0400 | [diff] [blame] | 743 | struct driver_data *drv_data = dev_id; |
David Brownell | cf43369 | 2008-04-28 02:14:17 -0700 | [diff] [blame] | 744 | void __iomem *reg = drv_data->ioaddr; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 745 | |
| 746 | if (!drv_data->cur_msg) { |
Stephen Street | 5daa3ba | 2006-05-20 15:00:19 -0700 | [diff] [blame] | 747 | |
| 748 | write_SSCR0(read_SSCR0(reg) & ~SSCR0_SSE, reg); |
| 749 | write_SSCR1(read_SSCR1(reg) & ~drv_data->int_cr1, reg); |
| 750 | if (drv_data->ssp_type != PXA25x_SSP) |
| 751 | write_SSTO(0, reg); |
| 752 | write_SSSR(drv_data->clear_sr, reg); |
| 753 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 754 | dev_err(&drv_data->pdev->dev, "bad message state " |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 755 | "in interrupt handler\n"); |
Stephen Street | 5daa3ba | 2006-05-20 15:00:19 -0700 | [diff] [blame] | 756 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 757 | /* Never fail */ |
| 758 | return IRQ_HANDLED; |
| 759 | } |
| 760 | |
| 761 | return drv_data->transfer_handler(drv_data); |
| 762 | } |
| 763 | |
David Brownell | cf43369 | 2008-04-28 02:14:17 -0700 | [diff] [blame] | 764 | static int set_dma_burst_and_threshold(struct chip_data *chip, |
| 765 | struct spi_device *spi, |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 766 | u8 bits_per_word, u32 *burst_code, |
| 767 | u32 *threshold) |
| 768 | { |
| 769 | struct pxa2xx_spi_chip *chip_info = |
| 770 | (struct pxa2xx_spi_chip *)spi->controller_data; |
| 771 | int bytes_per_word; |
| 772 | int burst_bytes; |
| 773 | int thresh_words; |
| 774 | int req_burst_size; |
| 775 | int retval = 0; |
| 776 | |
| 777 | /* Set the threshold (in registers) to equal the same amount of data |
| 778 | * as represented by burst size (in bytes). The computation below |
| 779 | * is (burst_size rounded up to nearest 8 byte, word or long word) |
| 780 | * divided by (bytes/register); the tx threshold is the inverse of |
| 781 | * the rx, so that there will always be enough data in the rx fifo |
| 782 | * to satisfy a burst, and there will always be enough space in the |
| 783 | * tx fifo to accept a burst (a tx burst will overwrite the fifo if |
| 784 | * there is not enough space), there must always remain enough empty |
| 785 | * space in the rx fifo for any data loaded to the tx fifo. |
| 786 | * Whenever burst_size (in bytes) equals bits/word, the fifo threshold |
| 787 | * will be 8, or half the fifo; |
| 788 | * The threshold can only be set to 2, 4 or 8, but not 16, because |
| 789 | * to burst 16 to the tx fifo, the fifo would have to be empty; |
| 790 | * however, the minimum fifo trigger level is 1, and the tx will |
| 791 | * request service when the fifo is at this level, with only 15 spaces. |
| 792 | */ |
| 793 | |
| 794 | /* find bytes/word */ |
| 795 | if (bits_per_word <= 8) |
| 796 | bytes_per_word = 1; |
| 797 | else if (bits_per_word <= 16) |
| 798 | bytes_per_word = 2; |
| 799 | else |
| 800 | bytes_per_word = 4; |
| 801 | |
| 802 | /* use struct pxa2xx_spi_chip->dma_burst_size if available */ |
| 803 | if (chip_info) |
| 804 | req_burst_size = chip_info->dma_burst_size; |
| 805 | else { |
| 806 | switch (chip->dma_burst_size) { |
| 807 | default: |
| 808 | /* if the default burst size is not set, |
| 809 | * do it now */ |
| 810 | chip->dma_burst_size = DCMD_BURST8; |
| 811 | case DCMD_BURST8: |
| 812 | req_burst_size = 8; |
| 813 | break; |
| 814 | case DCMD_BURST16: |
| 815 | req_burst_size = 16; |
| 816 | break; |
| 817 | case DCMD_BURST32: |
| 818 | req_burst_size = 32; |
| 819 | break; |
| 820 | } |
| 821 | } |
| 822 | if (req_burst_size <= 8) { |
| 823 | *burst_code = DCMD_BURST8; |
| 824 | burst_bytes = 8; |
| 825 | } else if (req_burst_size <= 16) { |
| 826 | if (bytes_per_word == 1) { |
| 827 | /* don't burst more than 1/2 the fifo */ |
| 828 | *burst_code = DCMD_BURST8; |
| 829 | burst_bytes = 8; |
| 830 | retval = 1; |
| 831 | } else { |
| 832 | *burst_code = DCMD_BURST16; |
| 833 | burst_bytes = 16; |
| 834 | } |
| 835 | } else { |
| 836 | if (bytes_per_word == 1) { |
| 837 | /* don't burst more than 1/2 the fifo */ |
| 838 | *burst_code = DCMD_BURST8; |
| 839 | burst_bytes = 8; |
| 840 | retval = 1; |
| 841 | } else if (bytes_per_word == 2) { |
| 842 | /* don't burst more than 1/2 the fifo */ |
| 843 | *burst_code = DCMD_BURST16; |
| 844 | burst_bytes = 16; |
| 845 | retval = 1; |
| 846 | } else { |
| 847 | *burst_code = DCMD_BURST32; |
| 848 | burst_bytes = 32; |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | thresh_words = burst_bytes / bytes_per_word; |
| 853 | |
| 854 | /* thresh_words will be between 2 and 8 */ |
| 855 | *threshold = (SSCR1_RxTresh(thresh_words) & SSCR1_RFT) |
| 856 | | (SSCR1_TxTresh(16-thresh_words) & SSCR1_TFT); |
| 857 | |
| 858 | return retval; |
| 859 | } |
| 860 | |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 861 | static unsigned int ssp_get_clk_div(struct ssp_device *ssp, int rate) |
| 862 | { |
| 863 | unsigned long ssp_clk = clk_get_rate(ssp->clk); |
| 864 | |
| 865 | if (ssp->type == PXA25x_SSP) |
| 866 | return ((ssp_clk / (2 * rate) - 1) & 0xff) << 8; |
| 867 | else |
| 868 | return ((ssp_clk / rate - 1) & 0xfff) << 8; |
| 869 | } |
| 870 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 871 | static void pump_transfers(unsigned long data) |
| 872 | { |
| 873 | struct driver_data *drv_data = (struct driver_data *)data; |
| 874 | struct spi_message *message = NULL; |
| 875 | struct spi_transfer *transfer = NULL; |
| 876 | struct spi_transfer *previous = NULL; |
| 877 | struct chip_data *chip = NULL; |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 878 | struct ssp_device *ssp = drv_data->ssp; |
David Brownell | cf43369 | 2008-04-28 02:14:17 -0700 | [diff] [blame] | 879 | void __iomem *reg = drv_data->ioaddr; |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 880 | u32 clk_div = 0; |
| 881 | u8 bits = 0; |
| 882 | u32 speed = 0; |
| 883 | u32 cr0; |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 884 | u32 cr1; |
| 885 | u32 dma_thresh = drv_data->cur_chip->dma_threshold; |
| 886 | u32 dma_burst = drv_data->cur_chip->dma_burst_size; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 887 | |
| 888 | /* Get current state information */ |
| 889 | message = drv_data->cur_msg; |
| 890 | transfer = drv_data->cur_transfer; |
| 891 | chip = drv_data->cur_chip; |
| 892 | |
| 893 | /* Handle for abort */ |
| 894 | if (message->state == ERROR_STATE) { |
| 895 | message->status = -EIO; |
Stephen Street | 5daa3ba | 2006-05-20 15:00:19 -0700 | [diff] [blame] | 896 | giveback(drv_data); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 897 | return; |
| 898 | } |
| 899 | |
| 900 | /* Handle end of message */ |
| 901 | if (message->state == DONE_STATE) { |
| 902 | message->status = 0; |
Stephen Street | 5daa3ba | 2006-05-20 15:00:19 -0700 | [diff] [blame] | 903 | giveback(drv_data); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 904 | return; |
| 905 | } |
| 906 | |
Ned Forrester | 8423597 | 2008-09-13 02:33:17 -0700 | [diff] [blame] | 907 | /* Delay if requested at end of transfer before CS change */ |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 908 | if (message->state == RUNNING_STATE) { |
| 909 | previous = list_entry(transfer->transfer_list.prev, |
| 910 | struct spi_transfer, |
| 911 | transfer_list); |
| 912 | if (previous->delay_usecs) |
| 913 | udelay(previous->delay_usecs); |
Ned Forrester | 8423597 | 2008-09-13 02:33:17 -0700 | [diff] [blame] | 914 | |
| 915 | /* Drop chip select only if cs_change is requested */ |
| 916 | if (previous->cs_change) |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 917 | cs_deassert(drv_data); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 918 | } |
| 919 | |
Ned Forrester | 7e96445 | 2008-09-13 02:33:18 -0700 | [diff] [blame] | 920 | /* Check for transfers that need multiple DMA segments */ |
| 921 | if (transfer->len > MAX_DMA_LEN && chip->enable_dma) { |
| 922 | |
| 923 | /* reject already-mapped transfers; PIO won't always work */ |
| 924 | if (message->is_dma_mapped |
| 925 | || transfer->rx_dma || transfer->tx_dma) { |
| 926 | dev_err(&drv_data->pdev->dev, |
| 927 | "pump_transfers: mapped transfer length " |
Mike Rapoport | 20b918d | 2008-10-01 10:39:24 -0700 | [diff] [blame] | 928 | "of %u is greater than %d\n", |
Ned Forrester | 7e96445 | 2008-09-13 02:33:18 -0700 | [diff] [blame] | 929 | transfer->len, MAX_DMA_LEN); |
| 930 | message->status = -EINVAL; |
| 931 | giveback(drv_data); |
| 932 | return; |
| 933 | } |
| 934 | |
| 935 | /* warn ... we force this to PIO mode */ |
| 936 | if (printk_ratelimit()) |
| 937 | dev_warn(&message->spi->dev, "pump_transfers: " |
| 938 | "DMA disabled for transfer length %ld " |
| 939 | "greater than %d\n", |
| 940 | (long)drv_data->len, MAX_DMA_LEN); |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 941 | } |
| 942 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 943 | /* Setup the transfer state based on the type of transfer */ |
| 944 | if (flush(drv_data) == 0) { |
| 945 | dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n"); |
| 946 | message->status = -EIO; |
Stephen Street | 5daa3ba | 2006-05-20 15:00:19 -0700 | [diff] [blame] | 947 | giveback(drv_data); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 948 | return; |
| 949 | } |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 950 | drv_data->n_bytes = chip->n_bytes; |
| 951 | drv_data->dma_width = chip->dma_width; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 952 | drv_data->tx = (void *)transfer->tx_buf; |
| 953 | drv_data->tx_end = drv_data->tx + transfer->len; |
| 954 | drv_data->rx = transfer->rx_buf; |
| 955 | drv_data->rx_end = drv_data->rx + transfer->len; |
| 956 | drv_data->rx_dma = transfer->rx_dma; |
| 957 | drv_data->tx_dma = transfer->tx_dma; |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 958 | drv_data->len = transfer->len & DCMD_LENGTH; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 959 | drv_data->write = drv_data->tx ? chip->write : null_writer; |
| 960 | drv_data->read = drv_data->rx ? chip->read : null_reader; |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 961 | |
| 962 | /* Change speed and bit per word on a per transfer */ |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 963 | cr0 = chip->cr0; |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 964 | if (transfer->speed_hz || transfer->bits_per_word) { |
| 965 | |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 966 | bits = chip->bits_per_word; |
| 967 | speed = chip->speed_hz; |
| 968 | |
| 969 | if (transfer->speed_hz) |
| 970 | speed = transfer->speed_hz; |
| 971 | |
| 972 | if (transfer->bits_per_word) |
| 973 | bits = transfer->bits_per_word; |
| 974 | |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 975 | clk_div = ssp_get_clk_div(ssp, speed); |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 976 | |
| 977 | if (bits <= 8) { |
| 978 | drv_data->n_bytes = 1; |
| 979 | drv_data->dma_width = DCMD_WIDTH1; |
| 980 | drv_data->read = drv_data->read != null_reader ? |
| 981 | u8_reader : null_reader; |
| 982 | drv_data->write = drv_data->write != null_writer ? |
| 983 | u8_writer : null_writer; |
| 984 | } else if (bits <= 16) { |
| 985 | drv_data->n_bytes = 2; |
| 986 | drv_data->dma_width = DCMD_WIDTH2; |
| 987 | drv_data->read = drv_data->read != null_reader ? |
| 988 | u16_reader : null_reader; |
| 989 | drv_data->write = drv_data->write != null_writer ? |
| 990 | u16_writer : null_writer; |
| 991 | } else if (bits <= 32) { |
| 992 | drv_data->n_bytes = 4; |
| 993 | drv_data->dma_width = DCMD_WIDTH4; |
| 994 | drv_data->read = drv_data->read != null_reader ? |
| 995 | u32_reader : null_reader; |
| 996 | drv_data->write = drv_data->write != null_writer ? |
| 997 | u32_writer : null_writer; |
| 998 | } |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 999 | /* if bits/word is changed in dma mode, then must check the |
| 1000 | * thresholds and burst also */ |
| 1001 | if (chip->enable_dma) { |
| 1002 | if (set_dma_burst_and_threshold(chip, message->spi, |
| 1003 | bits, &dma_burst, |
| 1004 | &dma_thresh)) |
| 1005 | if (printk_ratelimit()) |
| 1006 | dev_warn(&message->spi->dev, |
Ned Forrester | 7e96445 | 2008-09-13 02:33:18 -0700 | [diff] [blame] | 1007 | "pump_transfers: " |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1008 | "DMA burst size reduced to " |
| 1009 | "match bits_per_word\n"); |
| 1010 | } |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 1011 | |
| 1012 | cr0 = clk_div |
| 1013 | | SSCR0_Motorola |
Stephen Street | 5daa3ba | 2006-05-20 15:00:19 -0700 | [diff] [blame] | 1014 | | SSCR0_DataSize(bits > 16 ? bits - 16 : bits) |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 1015 | | SSCR0_SSE |
| 1016 | | (bits > 16 ? SSCR0_EDSS : 0); |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 1017 | } |
| 1018 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1019 | message->state = RUNNING_STATE; |
| 1020 | |
Ned Forrester | 7e96445 | 2008-09-13 02:33:18 -0700 | [diff] [blame] | 1021 | /* Try to map dma buffer and do a dma transfer if successful, but |
| 1022 | * only if the length is non-zero and less than MAX_DMA_LEN. |
| 1023 | * |
| 1024 | * Zero-length non-descriptor DMA is illegal on PXA2xx; force use |
| 1025 | * of PIO instead. Care is needed above because the transfer may |
| 1026 | * have have been passed with buffers that are already dma mapped. |
| 1027 | * A zero-length transfer in PIO mode will not try to write/read |
| 1028 | * to/from the buffers |
| 1029 | * |
| 1030 | * REVISIT large transfers are exactly where we most want to be |
| 1031 | * using DMA. If this happens much, split those transfers into |
| 1032 | * multiple DMA segments rather than forcing PIO. |
| 1033 | */ |
| 1034 | drv_data->dma_mapped = 0; |
| 1035 | if (drv_data->len > 0 && drv_data->len <= MAX_DMA_LEN) |
| 1036 | drv_data->dma_mapped = map_dma_buffers(drv_data); |
| 1037 | if (drv_data->dma_mapped) { |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1038 | |
| 1039 | /* Ensure we have the correct interrupt handler */ |
| 1040 | drv_data->transfer_handler = dma_transfer; |
| 1041 | |
| 1042 | /* Setup rx DMA Channel */ |
| 1043 | DCSR(drv_data->rx_channel) = RESET_DMA_CHANNEL; |
| 1044 | DSADR(drv_data->rx_channel) = drv_data->ssdr_physical; |
| 1045 | DTADR(drv_data->rx_channel) = drv_data->rx_dma; |
| 1046 | if (drv_data->rx == drv_data->null_dma_buf) |
| 1047 | /* No target address increment */ |
| 1048 | DCMD(drv_data->rx_channel) = DCMD_FLOWSRC |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 1049 | | drv_data->dma_width |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1050 | | dma_burst |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1051 | | drv_data->len; |
| 1052 | else |
| 1053 | DCMD(drv_data->rx_channel) = DCMD_INCTRGADDR |
| 1054 | | DCMD_FLOWSRC |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 1055 | | drv_data->dma_width |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1056 | | dma_burst |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1057 | | drv_data->len; |
| 1058 | |
| 1059 | /* Setup tx DMA Channel */ |
| 1060 | DCSR(drv_data->tx_channel) = RESET_DMA_CHANNEL; |
| 1061 | DSADR(drv_data->tx_channel) = drv_data->tx_dma; |
| 1062 | DTADR(drv_data->tx_channel) = drv_data->ssdr_physical; |
| 1063 | if (drv_data->tx == drv_data->null_dma_buf) |
| 1064 | /* No source address increment */ |
| 1065 | DCMD(drv_data->tx_channel) = DCMD_FLOWTRG |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 1066 | | drv_data->dma_width |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1067 | | dma_burst |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1068 | | drv_data->len; |
| 1069 | else |
| 1070 | DCMD(drv_data->tx_channel) = DCMD_INCSRCADDR |
| 1071 | | DCMD_FLOWTRG |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 1072 | | drv_data->dma_width |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1073 | | dma_burst |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1074 | | drv_data->len; |
| 1075 | |
| 1076 | /* Enable dma end irqs on SSP to detect end of transfer */ |
| 1077 | if (drv_data->ssp_type == PXA25x_SSP) |
| 1078 | DCMD(drv_data->tx_channel) |= DCMD_ENDIRQEN; |
| 1079 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1080 | /* Clear status and start DMA engine */ |
| 1081 | cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1082 | write_SSSR(drv_data->clear_sr, reg); |
| 1083 | DCSR(drv_data->rx_channel) |= DCSR_RUN; |
| 1084 | DCSR(drv_data->tx_channel) |= DCSR_RUN; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1085 | } else { |
| 1086 | /* Ensure we have the correct interrupt handler */ |
| 1087 | drv_data->transfer_handler = interrupt_transfer; |
| 1088 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1089 | /* Clear status */ |
| 1090 | cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1091 | write_SSSR(drv_data->clear_sr, reg); |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | /* see if we need to reload the config registers */ |
| 1095 | if ((read_SSCR0(reg) != cr0) |
| 1096 | || (read_SSCR1(reg) & SSCR1_CHANGE_MASK) != |
| 1097 | (cr1 & SSCR1_CHANGE_MASK)) { |
| 1098 | |
Ned Forrester | b97c74b | 2008-02-23 15:23:40 -0800 | [diff] [blame] | 1099 | /* stop the SSP, and update the other bits */ |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1100 | write_SSCR0(cr0 & ~SSCR0_SSE, reg); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1101 | if (drv_data->ssp_type != PXA25x_SSP) |
| 1102 | write_SSTO(chip->timeout, reg); |
Ned Forrester | b97c74b | 2008-02-23 15:23:40 -0800 | [diff] [blame] | 1103 | /* first set CR1 without interrupt and service enables */ |
| 1104 | write_SSCR1(cr1 & SSCR1_CHANGE_MASK, reg); |
| 1105 | /* restart the SSP */ |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1106 | write_SSCR0(cr0, reg); |
Ned Forrester | b97c74b | 2008-02-23 15:23:40 -0800 | [diff] [blame] | 1107 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1108 | } else { |
| 1109 | if (drv_data->ssp_type != PXA25x_SSP) |
| 1110 | write_SSTO(chip->timeout, reg); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1111 | } |
Ned Forrester | b97c74b | 2008-02-23 15:23:40 -0800 | [diff] [blame] | 1112 | |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 1113 | cs_assert(drv_data); |
Ned Forrester | b97c74b | 2008-02-23 15:23:40 -0800 | [diff] [blame] | 1114 | |
| 1115 | /* after chip select, release the data by enabling service |
| 1116 | * requests and interrupts, without changing any mode bits */ |
| 1117 | write_SSCR1(cr1, reg); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1118 | } |
| 1119 | |
David Howells | 6d5aefb | 2006-12-05 19:36:26 +0000 | [diff] [blame] | 1120 | static void pump_messages(struct work_struct *work) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1121 | { |
David Howells | 6d5aefb | 2006-12-05 19:36:26 +0000 | [diff] [blame] | 1122 | struct driver_data *drv_data = |
| 1123 | container_of(work, struct driver_data, pump_messages); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1124 | unsigned long flags; |
| 1125 | |
| 1126 | /* Lock queue and check for queue work */ |
| 1127 | spin_lock_irqsave(&drv_data->lock, flags); |
| 1128 | if (list_empty(&drv_data->queue) || drv_data->run == QUEUE_STOPPED) { |
| 1129 | drv_data->busy = 0; |
| 1130 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 1131 | return; |
| 1132 | } |
| 1133 | |
| 1134 | /* Make sure we are not already running a message */ |
| 1135 | if (drv_data->cur_msg) { |
| 1136 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 1137 | return; |
| 1138 | } |
| 1139 | |
| 1140 | /* Extract head of queue */ |
| 1141 | drv_data->cur_msg = list_entry(drv_data->queue.next, |
| 1142 | struct spi_message, queue); |
| 1143 | list_del_init(&drv_data->cur_msg->queue); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1144 | |
| 1145 | /* Initial message state*/ |
| 1146 | drv_data->cur_msg->state = START_STATE; |
| 1147 | drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next, |
| 1148 | struct spi_transfer, |
| 1149 | transfer_list); |
| 1150 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1151 | /* prepare to setup the SSP, in pump_transfers, using the per |
| 1152 | * chip configuration */ |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1153 | drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1154 | |
| 1155 | /* Mark as busy and launch transfers */ |
| 1156 | tasklet_schedule(&drv_data->pump_transfers); |
Stephen Street | 5daa3ba | 2006-05-20 15:00:19 -0700 | [diff] [blame] | 1157 | |
| 1158 | drv_data->busy = 1; |
| 1159 | spin_unlock_irqrestore(&drv_data->lock, flags); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | static int transfer(struct spi_device *spi, struct spi_message *msg) |
| 1163 | { |
| 1164 | struct driver_data *drv_data = spi_master_get_devdata(spi->master); |
| 1165 | unsigned long flags; |
| 1166 | |
| 1167 | spin_lock_irqsave(&drv_data->lock, flags); |
| 1168 | |
| 1169 | if (drv_data->run == QUEUE_STOPPED) { |
| 1170 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 1171 | return -ESHUTDOWN; |
| 1172 | } |
| 1173 | |
| 1174 | msg->actual_length = 0; |
| 1175 | msg->status = -EINPROGRESS; |
| 1176 | msg->state = START_STATE; |
| 1177 | |
| 1178 | list_add_tail(&msg->queue, &drv_data->queue); |
| 1179 | |
| 1180 | if (drv_data->run == QUEUE_RUNNING && !drv_data->busy) |
| 1181 | queue_work(drv_data->workqueue, &drv_data->pump_messages); |
| 1182 | |
| 1183 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 1184 | |
| 1185 | return 0; |
| 1186 | } |
| 1187 | |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 1188 | static int setup_cs(struct spi_device *spi, struct chip_data *chip, |
| 1189 | struct pxa2xx_spi_chip *chip_info) |
| 1190 | { |
| 1191 | int err = 0; |
| 1192 | |
| 1193 | if (chip == NULL || chip_info == NULL) |
| 1194 | return 0; |
| 1195 | |
| 1196 | /* NOTE: setup() can be called multiple times, possibly with |
| 1197 | * different chip_info, release previously requested GPIO |
| 1198 | */ |
| 1199 | if (gpio_is_valid(chip->gpio_cs)) |
| 1200 | gpio_free(chip->gpio_cs); |
| 1201 | |
| 1202 | /* If (*cs_control) is provided, ignore GPIO chip select */ |
| 1203 | if (chip_info->cs_control) { |
| 1204 | chip->cs_control = chip_info->cs_control; |
| 1205 | return 0; |
| 1206 | } |
| 1207 | |
| 1208 | if (gpio_is_valid(chip_info->gpio_cs)) { |
| 1209 | err = gpio_request(chip_info->gpio_cs, "SPI_CS"); |
| 1210 | if (err) { |
| 1211 | dev_err(&spi->dev, "failed to request chip select " |
| 1212 | "GPIO%d\n", chip_info->gpio_cs); |
| 1213 | return err; |
| 1214 | } |
| 1215 | |
| 1216 | chip->gpio_cs = chip_info->gpio_cs; |
| 1217 | chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH; |
| 1218 | |
| 1219 | err = gpio_direction_output(chip->gpio_cs, |
| 1220 | !chip->gpio_cs_inverted); |
| 1221 | } |
| 1222 | |
| 1223 | return err; |
| 1224 | } |
| 1225 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1226 | static int setup(struct spi_device *spi) |
| 1227 | { |
| 1228 | struct pxa2xx_spi_chip *chip_info = NULL; |
| 1229 | struct chip_data *chip; |
| 1230 | struct driver_data *drv_data = spi_master_get_devdata(spi->master); |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 1231 | struct ssp_device *ssp = drv_data->ssp; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1232 | unsigned int clk_div; |
Vernon Sauder | f1f640a | 2008-10-15 22:02:43 -0700 | [diff] [blame] | 1233 | uint tx_thres = TX_THRESH_DFLT; |
| 1234 | uint rx_thres = RX_THRESH_DFLT; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1235 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1236 | if (drv_data->ssp_type != PXA25x_SSP |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1237 | && (spi->bits_per_word < 4 || spi->bits_per_word > 32)) { |
| 1238 | dev_err(&spi->dev, "failed setup: ssp_type=%d, bits/wrd=%d " |
| 1239 | "b/w not 4-32 for type non-PXA25x_SSP\n", |
| 1240 | drv_data->ssp_type, spi->bits_per_word); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1241 | return -EINVAL; |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1242 | } |
| 1243 | else if (drv_data->ssp_type == PXA25x_SSP |
| 1244 | && (spi->bits_per_word < 4 |
| 1245 | || spi->bits_per_word > 16)) { |
| 1246 | dev_err(&spi->dev, "failed setup: ssp_type=%d, bits/wrd=%d " |
| 1247 | "b/w not 4-16 for type PXA25x_SSP\n", |
| 1248 | drv_data->ssp_type, spi->bits_per_word); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1249 | return -EINVAL; |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1250 | } |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1251 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1252 | /* Only alloc on first setup */ |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1253 | chip = spi_get_ctldata(spi); |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1254 | if (!chip) { |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1255 | chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL); |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1256 | if (!chip) { |
| 1257 | dev_err(&spi->dev, |
| 1258 | "failed setup: can't allocate chip data\n"); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1259 | return -ENOMEM; |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1260 | } |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1261 | |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 1262 | chip->gpio_cs = -1; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1263 | chip->enable_dma = 0; |
Vernon Sauder | f1f640a | 2008-10-15 22:02:43 -0700 | [diff] [blame] | 1264 | chip->timeout = TIMOUT_DFLT; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1265 | chip->dma_burst_size = drv_data->master_info->enable_dma ? |
| 1266 | DCMD_BURST8 : 0; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1267 | } |
| 1268 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1269 | /* protocol drivers may change the chip settings, so... |
| 1270 | * if chip_info exists, use it */ |
| 1271 | chip_info = spi->controller_data; |
| 1272 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1273 | /* chip_info isn't always needed */ |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1274 | chip->cr1 = 0; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1275 | if (chip_info) { |
Vernon Sauder | f1f640a | 2008-10-15 22:02:43 -0700 | [diff] [blame] | 1276 | if (chip_info->timeout) |
| 1277 | chip->timeout = chip_info->timeout; |
| 1278 | if (chip_info->tx_threshold) |
| 1279 | tx_thres = chip_info->tx_threshold; |
| 1280 | if (chip_info->rx_threshold) |
| 1281 | rx_thres = chip_info->rx_threshold; |
| 1282 | chip->enable_dma = drv_data->master_info->enable_dma; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1283 | chip->dma_threshold = 0; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1284 | if (chip_info->enable_loopback) |
| 1285 | chip->cr1 = SSCR1_LBM; |
| 1286 | } |
| 1287 | |
Vernon Sauder | f1f640a | 2008-10-15 22:02:43 -0700 | [diff] [blame] | 1288 | chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) | |
| 1289 | (SSCR1_TxTresh(tx_thres) & SSCR1_TFT); |
| 1290 | |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1291 | /* set dma burst and threshold outside of chip_info path so that if |
| 1292 | * chip_info goes away after setting chip->enable_dma, the |
| 1293 | * burst and threshold can still respond to changes in bits_per_word */ |
| 1294 | if (chip->enable_dma) { |
| 1295 | /* set up legal burst and threshold for dma */ |
| 1296 | if (set_dma_burst_and_threshold(chip, spi, spi->bits_per_word, |
| 1297 | &chip->dma_burst_size, |
| 1298 | &chip->dma_threshold)) { |
| 1299 | dev_warn(&spi->dev, "in setup: DMA burst size reduced " |
| 1300 | "to match bits_per_word\n"); |
| 1301 | } |
| 1302 | } |
| 1303 | |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 1304 | clk_div = ssp_get_clk_div(ssp, spi->max_speed_hz); |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 1305 | chip->speed_hz = spi->max_speed_hz; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1306 | |
| 1307 | chip->cr0 = clk_div |
| 1308 | | SSCR0_Motorola |
Stephen Street | 5daa3ba | 2006-05-20 15:00:19 -0700 | [diff] [blame] | 1309 | | SSCR0_DataSize(spi->bits_per_word > 16 ? |
| 1310 | spi->bits_per_word - 16 : spi->bits_per_word) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1311 | | SSCR0_SSE |
| 1312 | | (spi->bits_per_word > 16 ? SSCR0_EDSS : 0); |
Justin Clacherty | 7f6ee1a | 2007-01-26 00:56:44 -0800 | [diff] [blame] | 1313 | chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH); |
| 1314 | chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0) |
| 1315 | | (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1316 | |
| 1317 | /* NOTE: PXA25x_SSP _could_ use external clocking ... */ |
| 1318 | if (drv_data->ssp_type != PXA25x_SSP) |
David Brownell | 7d07719 | 2009-06-17 16:26:03 -0700 | [diff] [blame] | 1319 | dev_dbg(&spi->dev, "%ld Hz actual, %s\n", |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 1320 | clk_get_rate(ssp->clk) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1321 | / (1 + ((chip->cr0 & SSCR0_SCR) >> 8)), |
Vernon Sauder | f1f640a | 2008-10-15 22:02:43 -0700 | [diff] [blame] | 1322 | chip->enable_dma ? "DMA" : "PIO"); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1323 | else |
David Brownell | 7d07719 | 2009-06-17 16:26:03 -0700 | [diff] [blame] | 1324 | dev_dbg(&spi->dev, "%ld Hz actual, %s\n", |
Vernon Sauder | f1f640a | 2008-10-15 22:02:43 -0700 | [diff] [blame] | 1325 | clk_get_rate(ssp->clk) / 2 |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1326 | / (1 + ((chip->cr0 & SSCR0_SCR) >> 8)), |
Vernon Sauder | f1f640a | 2008-10-15 22:02:43 -0700 | [diff] [blame] | 1327 | chip->enable_dma ? "DMA" : "PIO"); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1328 | |
| 1329 | if (spi->bits_per_word <= 8) { |
| 1330 | chip->n_bytes = 1; |
| 1331 | chip->dma_width = DCMD_WIDTH1; |
| 1332 | chip->read = u8_reader; |
| 1333 | chip->write = u8_writer; |
| 1334 | } else if (spi->bits_per_word <= 16) { |
| 1335 | chip->n_bytes = 2; |
| 1336 | chip->dma_width = DCMD_WIDTH2; |
| 1337 | chip->read = u16_reader; |
| 1338 | chip->write = u16_writer; |
| 1339 | } else if (spi->bits_per_word <= 32) { |
| 1340 | chip->cr0 |= SSCR0_EDSS; |
| 1341 | chip->n_bytes = 4; |
| 1342 | chip->dma_width = DCMD_WIDTH4; |
| 1343 | chip->read = u32_reader; |
| 1344 | chip->write = u32_writer; |
| 1345 | } else { |
| 1346 | dev_err(&spi->dev, "invalid wordsize\n"); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1347 | return -ENODEV; |
| 1348 | } |
Stephen Street | 9708c12 | 2006-03-28 14:05:23 -0800 | [diff] [blame] | 1349 | chip->bits_per_word = spi->bits_per_word; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1350 | |
| 1351 | spi_set_ctldata(spi, chip); |
| 1352 | |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 1353 | return setup_cs(spi, chip, chip_info); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1354 | } |
| 1355 | |
Hans-Peter Nilsson | 0ffa028 | 2007-02-12 00:52:45 -0800 | [diff] [blame] | 1356 | static void cleanup(struct spi_device *spi) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1357 | { |
Hans-Peter Nilsson | 0ffa028 | 2007-02-12 00:52:45 -0800 | [diff] [blame] | 1358 | struct chip_data *chip = spi_get_ctldata(spi); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1359 | |
Daniel Ribeiro | 7348d82 | 2009-05-12 13:19:36 -0700 | [diff] [blame] | 1360 | if (!chip) |
| 1361 | return; |
| 1362 | |
Eric Miao | a7bb390 | 2009-04-06 19:00:54 -0700 | [diff] [blame] | 1363 | if (gpio_is_valid(chip->gpio_cs)) |
| 1364 | gpio_free(chip->gpio_cs); |
| 1365 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1366 | kfree(chip); |
| 1367 | } |
| 1368 | |
David Brownell | d1e44d9 | 2007-10-16 01:27:46 -0700 | [diff] [blame] | 1369 | static int __init init_queue(struct driver_data *drv_data) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1370 | { |
| 1371 | INIT_LIST_HEAD(&drv_data->queue); |
| 1372 | spin_lock_init(&drv_data->lock); |
| 1373 | |
| 1374 | drv_data->run = QUEUE_STOPPED; |
| 1375 | drv_data->busy = 0; |
| 1376 | |
| 1377 | tasklet_init(&drv_data->pump_transfers, |
| 1378 | pump_transfers, (unsigned long)drv_data); |
| 1379 | |
David Howells | 6d5aefb | 2006-12-05 19:36:26 +0000 | [diff] [blame] | 1380 | INIT_WORK(&drv_data->pump_messages, pump_messages); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1381 | drv_data->workqueue = create_singlethread_workqueue( |
Kay Sievers | 6c7377a | 2009-03-24 16:38:21 -0700 | [diff] [blame] | 1382 | dev_name(drv_data->master->dev.parent)); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1383 | if (drv_data->workqueue == NULL) |
| 1384 | return -EBUSY; |
| 1385 | |
| 1386 | return 0; |
| 1387 | } |
| 1388 | |
| 1389 | static int start_queue(struct driver_data *drv_data) |
| 1390 | { |
| 1391 | unsigned long flags; |
| 1392 | |
| 1393 | spin_lock_irqsave(&drv_data->lock, flags); |
| 1394 | |
| 1395 | if (drv_data->run == QUEUE_RUNNING || drv_data->busy) { |
| 1396 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 1397 | return -EBUSY; |
| 1398 | } |
| 1399 | |
| 1400 | drv_data->run = QUEUE_RUNNING; |
| 1401 | drv_data->cur_msg = NULL; |
| 1402 | drv_data->cur_transfer = NULL; |
| 1403 | drv_data->cur_chip = NULL; |
| 1404 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 1405 | |
| 1406 | queue_work(drv_data->workqueue, &drv_data->pump_messages); |
| 1407 | |
| 1408 | return 0; |
| 1409 | } |
| 1410 | |
| 1411 | static int stop_queue(struct driver_data *drv_data) |
| 1412 | { |
| 1413 | unsigned long flags; |
| 1414 | unsigned limit = 500; |
| 1415 | int status = 0; |
| 1416 | |
| 1417 | spin_lock_irqsave(&drv_data->lock, flags); |
| 1418 | |
| 1419 | /* This is a bit lame, but is optimized for the common execution path. |
| 1420 | * A wait_queue on the drv_data->busy could be used, but then the common |
| 1421 | * execution path (pump_messages) would be required to call wake_up or |
| 1422 | * friends on every SPI message. Do this instead */ |
| 1423 | drv_data->run = QUEUE_STOPPED; |
| 1424 | while (!list_empty(&drv_data->queue) && drv_data->busy && limit--) { |
| 1425 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 1426 | msleep(10); |
| 1427 | spin_lock_irqsave(&drv_data->lock, flags); |
| 1428 | } |
| 1429 | |
| 1430 | if (!list_empty(&drv_data->queue) || drv_data->busy) |
| 1431 | status = -EBUSY; |
| 1432 | |
| 1433 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 1434 | |
| 1435 | return status; |
| 1436 | } |
| 1437 | |
| 1438 | static int destroy_queue(struct driver_data *drv_data) |
| 1439 | { |
| 1440 | int status; |
| 1441 | |
| 1442 | status = stop_queue(drv_data); |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1443 | /* we are unloading the module or failing to load (only two calls |
| 1444 | * to this routine), and neither call can handle a return value. |
| 1445 | * However, destroy_workqueue calls flush_workqueue, and that will |
| 1446 | * block until all work is done. If the reason that stop_queue |
| 1447 | * timed out is that the work will never finish, then it does no |
| 1448 | * good to call destroy_workqueue, so return anyway. */ |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1449 | if (status != 0) |
| 1450 | return status; |
| 1451 | |
| 1452 | destroy_workqueue(drv_data->workqueue); |
| 1453 | |
| 1454 | return 0; |
| 1455 | } |
| 1456 | |
David Brownell | d1e44d9 | 2007-10-16 01:27:46 -0700 | [diff] [blame] | 1457 | static int __init pxa2xx_spi_probe(struct platform_device *pdev) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1458 | { |
| 1459 | struct device *dev = &pdev->dev; |
| 1460 | struct pxa2xx_spi_master *platform_info; |
| 1461 | struct spi_master *master; |
Guennadi Liakhovetski | 65a00a2 | 2008-10-15 22:02:42 -0700 | [diff] [blame] | 1462 | struct driver_data *drv_data; |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 1463 | struct ssp_device *ssp; |
Guennadi Liakhovetski | 65a00a2 | 2008-10-15 22:02:42 -0700 | [diff] [blame] | 1464 | int status; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1465 | |
| 1466 | platform_info = dev->platform_data; |
| 1467 | |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 1468 | ssp = ssp_request(pdev->id, pdev->name); |
| 1469 | if (ssp == NULL) { |
| 1470 | dev_err(&pdev->dev, "failed to request SSP%d\n", pdev->id); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1471 | return -ENODEV; |
| 1472 | } |
| 1473 | |
| 1474 | /* Allocate master with space for drv_data and null dma buffer */ |
| 1475 | master = spi_alloc_master(dev, sizeof(struct driver_data) + 16); |
| 1476 | if (!master) { |
Guennadi Liakhovetski | 65a00a2 | 2008-10-15 22:02:42 -0700 | [diff] [blame] | 1477 | dev_err(&pdev->dev, "cannot alloc spi_master\n"); |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 1478 | ssp_free(ssp); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1479 | return -ENOMEM; |
| 1480 | } |
| 1481 | drv_data = spi_master_get_devdata(master); |
| 1482 | drv_data->master = master; |
| 1483 | drv_data->master_info = platform_info; |
| 1484 | drv_data->pdev = pdev; |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 1485 | drv_data->ssp = ssp; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1486 | |
David Brownell | e7db06b | 2009-06-17 16:26:04 -0700 | [diff] [blame] | 1487 | /* the spi->mode bits understood by this driver: */ |
Daniel Ribeiro | 50e0a7b | 2009-06-17 16:26:06 -0700 | [diff] [blame] | 1488 | master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH; |
David Brownell | e7db06b | 2009-06-17 16:26:04 -0700 | [diff] [blame] | 1489 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1490 | master->bus_num = pdev->id; |
| 1491 | master->num_chipselect = platform_info->num_chipselect; |
Mike Rapoport | 7ad0ba9 | 2009-04-06 19:00:57 -0700 | [diff] [blame] | 1492 | master->dma_alignment = DMA_ALIGNMENT; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1493 | master->cleanup = cleanup; |
| 1494 | master->setup = setup; |
| 1495 | master->transfer = transfer; |
| 1496 | |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 1497 | drv_data->ssp_type = ssp->type; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1498 | drv_data->null_dma_buf = (u32 *)ALIGN((u32)(drv_data + |
| 1499 | sizeof(struct driver_data)), 8); |
| 1500 | |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 1501 | drv_data->ioaddr = ssp->mmio_base; |
| 1502 | drv_data->ssdr_physical = ssp->phys_base + SSDR; |
| 1503 | if (ssp->type == PXA25x_SSP) { |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1504 | drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE; |
| 1505 | drv_data->dma_cr1 = 0; |
| 1506 | drv_data->clear_sr = SSSR_ROR; |
| 1507 | drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR; |
| 1508 | } else { |
| 1509 | drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE; |
| 1510 | drv_data->dma_cr1 = SSCR1_TSRE | SSCR1_RSRE | SSCR1_TINTE; |
| 1511 | drv_data->clear_sr = SSSR_ROR | SSSR_TINT; |
| 1512 | drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS | SSSR_ROR; |
| 1513 | } |
| 1514 | |
Kay Sievers | 6c7377a | 2009-03-24 16:38:21 -0700 | [diff] [blame] | 1515 | status = request_irq(ssp->irq, ssp_int, 0, dev_name(dev), drv_data); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1516 | if (status < 0) { |
Guennadi Liakhovetski | 65a00a2 | 2008-10-15 22:02:42 -0700 | [diff] [blame] | 1517 | dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1518 | goto out_error_master_alloc; |
| 1519 | } |
| 1520 | |
| 1521 | /* Setup DMA if requested */ |
| 1522 | drv_data->tx_channel = -1; |
| 1523 | drv_data->rx_channel = -1; |
| 1524 | if (platform_info->enable_dma) { |
| 1525 | |
| 1526 | /* Get two DMA channels (rx and tx) */ |
| 1527 | drv_data->rx_channel = pxa_request_dma("pxa2xx_spi_ssp_rx", |
| 1528 | DMA_PRIO_HIGH, |
| 1529 | dma_handler, |
| 1530 | drv_data); |
| 1531 | if (drv_data->rx_channel < 0) { |
| 1532 | dev_err(dev, "problem (%d) requesting rx channel\n", |
| 1533 | drv_data->rx_channel); |
| 1534 | status = -ENODEV; |
| 1535 | goto out_error_irq_alloc; |
| 1536 | } |
| 1537 | drv_data->tx_channel = pxa_request_dma("pxa2xx_spi_ssp_tx", |
| 1538 | DMA_PRIO_MEDIUM, |
| 1539 | dma_handler, |
| 1540 | drv_data); |
| 1541 | if (drv_data->tx_channel < 0) { |
| 1542 | dev_err(dev, "problem (%d) requesting tx channel\n", |
| 1543 | drv_data->tx_channel); |
| 1544 | status = -ENODEV; |
| 1545 | goto out_error_dma_alloc; |
| 1546 | } |
| 1547 | |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 1548 | DRCMR(ssp->drcmr_rx) = DRCMR_MAPVLD | drv_data->rx_channel; |
| 1549 | DRCMR(ssp->drcmr_tx) = DRCMR_MAPVLD | drv_data->tx_channel; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1550 | } |
| 1551 | |
| 1552 | /* Enable SOC clock */ |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 1553 | clk_enable(ssp->clk); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1554 | |
| 1555 | /* Load default SSP configuration */ |
| 1556 | write_SSCR0(0, drv_data->ioaddr); |
Vernon Sauder | f1f640a | 2008-10-15 22:02:43 -0700 | [diff] [blame] | 1557 | write_SSCR1(SSCR1_RxTresh(RX_THRESH_DFLT) | |
| 1558 | SSCR1_TxTresh(TX_THRESH_DFLT), |
| 1559 | drv_data->ioaddr); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1560 | write_SSCR0(SSCR0_SerClkDiv(2) |
| 1561 | | SSCR0_Motorola |
| 1562 | | SSCR0_DataSize(8), |
| 1563 | drv_data->ioaddr); |
| 1564 | if (drv_data->ssp_type != PXA25x_SSP) |
| 1565 | write_SSTO(0, drv_data->ioaddr); |
| 1566 | write_SSPSP(0, drv_data->ioaddr); |
| 1567 | |
| 1568 | /* Initial and start queue */ |
| 1569 | status = init_queue(drv_data); |
| 1570 | if (status != 0) { |
| 1571 | dev_err(&pdev->dev, "problem initializing queue\n"); |
| 1572 | goto out_error_clock_enabled; |
| 1573 | } |
| 1574 | status = start_queue(drv_data); |
| 1575 | if (status != 0) { |
| 1576 | dev_err(&pdev->dev, "problem starting queue\n"); |
| 1577 | goto out_error_clock_enabled; |
| 1578 | } |
| 1579 | |
| 1580 | /* Register with the SPI framework */ |
| 1581 | platform_set_drvdata(pdev, drv_data); |
| 1582 | status = spi_register_master(master); |
| 1583 | if (status != 0) { |
| 1584 | dev_err(&pdev->dev, "problem registering spi master\n"); |
| 1585 | goto out_error_queue_alloc; |
| 1586 | } |
| 1587 | |
| 1588 | return status; |
| 1589 | |
| 1590 | out_error_queue_alloc: |
| 1591 | destroy_queue(drv_data); |
| 1592 | |
| 1593 | out_error_clock_enabled: |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 1594 | clk_disable(ssp->clk); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1595 | |
| 1596 | out_error_dma_alloc: |
| 1597 | if (drv_data->tx_channel != -1) |
| 1598 | pxa_free_dma(drv_data->tx_channel); |
| 1599 | if (drv_data->rx_channel != -1) |
| 1600 | pxa_free_dma(drv_data->rx_channel); |
| 1601 | |
| 1602 | out_error_irq_alloc: |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 1603 | free_irq(ssp->irq, drv_data); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1604 | |
| 1605 | out_error_master_alloc: |
| 1606 | spi_master_put(master); |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 1607 | ssp_free(ssp); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1608 | return status; |
| 1609 | } |
| 1610 | |
| 1611 | static int pxa2xx_spi_remove(struct platform_device *pdev) |
| 1612 | { |
| 1613 | struct driver_data *drv_data = platform_get_drvdata(pdev); |
Julia Lawall | 51e911e | 2009-01-06 14:41:45 -0800 | [diff] [blame] | 1614 | struct ssp_device *ssp; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1615 | int status = 0; |
| 1616 | |
| 1617 | if (!drv_data) |
| 1618 | return 0; |
Julia Lawall | 51e911e | 2009-01-06 14:41:45 -0800 | [diff] [blame] | 1619 | ssp = drv_data->ssp; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1620 | |
| 1621 | /* Remove the queue */ |
| 1622 | status = destroy_queue(drv_data); |
| 1623 | if (status != 0) |
Stephen Street | 8d94cc5 | 2006-12-10 02:18:54 -0800 | [diff] [blame] | 1624 | /* the kernel does not check the return status of this |
| 1625 | * this routine (mod->exit, within the kernel). Therefore |
| 1626 | * nothing is gained by returning from here, the module is |
| 1627 | * going away regardless, and we should not leave any more |
| 1628 | * resources allocated than necessary. We cannot free the |
| 1629 | * message memory in drv_data->queue, but we can release the |
| 1630 | * resources below. I think the kernel should honor -EBUSY |
| 1631 | * returns but... */ |
| 1632 | dev_err(&pdev->dev, "pxa2xx_spi_remove: workqueue will not " |
| 1633 | "complete, message memory not freed\n"); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1634 | |
| 1635 | /* Disable the SSP at the peripheral and SOC level */ |
| 1636 | write_SSCR0(0, drv_data->ioaddr); |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 1637 | clk_disable(ssp->clk); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1638 | |
| 1639 | /* Release DMA */ |
| 1640 | if (drv_data->master_info->enable_dma) { |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 1641 | DRCMR(ssp->drcmr_rx) = 0; |
| 1642 | DRCMR(ssp->drcmr_tx) = 0; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1643 | pxa_free_dma(drv_data->tx_channel); |
| 1644 | pxa_free_dma(drv_data->rx_channel); |
| 1645 | } |
| 1646 | |
| 1647 | /* Release IRQ */ |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 1648 | free_irq(ssp->irq, drv_data); |
| 1649 | |
| 1650 | /* Release SSP */ |
| 1651 | ssp_free(ssp); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1652 | |
| 1653 | /* Disconnect from the SPI framework */ |
| 1654 | spi_unregister_master(drv_data->master); |
| 1655 | |
| 1656 | /* Prevent double remove */ |
| 1657 | platform_set_drvdata(pdev, NULL); |
| 1658 | |
| 1659 | return 0; |
| 1660 | } |
| 1661 | |
| 1662 | static void pxa2xx_spi_shutdown(struct platform_device *pdev) |
| 1663 | { |
| 1664 | int status = 0; |
| 1665 | |
| 1666 | if ((status = pxa2xx_spi_remove(pdev)) != 0) |
| 1667 | dev_err(&pdev->dev, "shutdown failed with %d\n", status); |
| 1668 | } |
| 1669 | |
| 1670 | #ifdef CONFIG_PM |
Mike Rapoport | 86d2593 | 2009-07-21 17:50:16 +0300 | [diff] [blame] | 1671 | static int pxa2xx_spi_suspend(struct device *dev) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1672 | { |
Mike Rapoport | 86d2593 | 2009-07-21 17:50:16 +0300 | [diff] [blame] | 1673 | struct driver_data *drv_data = dev_get_drvdata(dev); |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 1674 | struct ssp_device *ssp = drv_data->ssp; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1675 | int status = 0; |
| 1676 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1677 | status = stop_queue(drv_data); |
| 1678 | if (status != 0) |
| 1679 | return status; |
| 1680 | write_SSCR0(0, drv_data->ioaddr); |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 1681 | clk_disable(ssp->clk); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1682 | |
| 1683 | return 0; |
| 1684 | } |
| 1685 | |
Mike Rapoport | 86d2593 | 2009-07-21 17:50:16 +0300 | [diff] [blame] | 1686 | static int pxa2xx_spi_resume(struct device *dev) |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1687 | { |
Mike Rapoport | 86d2593 | 2009-07-21 17:50:16 +0300 | [diff] [blame] | 1688 | struct driver_data *drv_data = dev_get_drvdata(dev); |
eric miao | 2f1a74e | 2007-11-21 18:50:53 +0800 | [diff] [blame] | 1689 | struct ssp_device *ssp = drv_data->ssp; |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1690 | int status = 0; |
| 1691 | |
Daniel Ribeiro | 148da33 | 2009-04-21 12:24:43 -0700 | [diff] [blame] | 1692 | if (drv_data->rx_channel != -1) |
| 1693 | DRCMR(drv_data->ssp->drcmr_rx) = |
| 1694 | DRCMR_MAPVLD | drv_data->rx_channel; |
| 1695 | if (drv_data->tx_channel != -1) |
| 1696 | DRCMR(drv_data->ssp->drcmr_tx) = |
| 1697 | DRCMR_MAPVLD | drv_data->tx_channel; |
| 1698 | |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1699 | /* Enable the SSP clock */ |
Eric BENARD | 0cf942d | 2008-05-12 14:02:01 -0700 | [diff] [blame] | 1700 | clk_enable(ssp->clk); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1701 | |
| 1702 | /* Start the queue running */ |
| 1703 | status = start_queue(drv_data); |
| 1704 | if (status != 0) { |
Mike Rapoport | 86d2593 | 2009-07-21 17:50:16 +0300 | [diff] [blame] | 1705 | dev_err(dev, "problem starting queue (%d)\n", status); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1706 | return status; |
| 1707 | } |
| 1708 | |
| 1709 | return 0; |
| 1710 | } |
Mike Rapoport | 86d2593 | 2009-07-21 17:50:16 +0300 | [diff] [blame] | 1711 | |
| 1712 | static struct dev_pm_ops pxa2xx_spi_pm_ops = { |
| 1713 | .suspend = pxa2xx_spi_suspend, |
| 1714 | .resume = pxa2xx_spi_resume, |
| 1715 | }; |
| 1716 | #endif |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1717 | |
| 1718 | static struct platform_driver driver = { |
| 1719 | .driver = { |
Mike Rapoport | 86d2593 | 2009-07-21 17:50:16 +0300 | [diff] [blame] | 1720 | .name = "pxa2xx-spi", |
| 1721 | .owner = THIS_MODULE, |
| 1722 | #ifdef CONFIG_PM |
| 1723 | .pm = &pxa2xx_spi_pm_ops, |
| 1724 | #endif |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1725 | }, |
David Brownell | d1e44d9 | 2007-10-16 01:27:46 -0700 | [diff] [blame] | 1726 | .remove = pxa2xx_spi_remove, |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1727 | .shutdown = pxa2xx_spi_shutdown, |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1728 | }; |
| 1729 | |
| 1730 | static int __init pxa2xx_spi_init(void) |
| 1731 | { |
David Brownell | d1e44d9 | 2007-10-16 01:27:46 -0700 | [diff] [blame] | 1732 | return platform_driver_probe(&driver, pxa2xx_spi_probe); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1733 | } |
Antonio Ospite | 5b61a74 | 2009-09-22 16:46:10 -0700 | [diff] [blame] | 1734 | subsys_initcall(pxa2xx_spi_init); |
Stephen Street | e0c9905 | 2006-03-07 23:53:24 -0800 | [diff] [blame] | 1735 | |
| 1736 | static void __exit pxa2xx_spi_exit(void) |
| 1737 | { |
| 1738 | platform_driver_unregister(&driver); |
| 1739 | } |
| 1740 | module_exit(pxa2xx_spi_exit); |