Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1 | /* |
Mike Frysinger | 26fdc1f | 2008-02-06 01:38:21 -0800 | [diff] [blame] | 2 | * Blackfin On-Chip SPI Driver |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 3 | * |
Bryan Wu | 131b17d | 2007-12-04 23:45:12 -0800 | [diff] [blame] | 4 | * Copyright 2004-2007 Analog Devices Inc. |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 5 | * |
Mike Frysinger | 26fdc1f | 2008-02-06 01:38:21 -0800 | [diff] [blame] | 6 | * Enter bugs at http://blackfin.uclinux.org/ |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 7 | * |
Mike Frysinger | 26fdc1f | 2008-02-06 01:38:21 -0800 | [diff] [blame] | 8 | * Licensed under the GPL-2 or later. |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/module.h> |
Bryan Wu | 131b17d | 2007-12-04 23:45:12 -0800 | [diff] [blame] | 13 | #include <linux/delay.h> |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 14 | #include <linux/device.h> |
Bryan Wu | 131b17d | 2007-12-04 23:45:12 -0800 | [diff] [blame] | 15 | #include <linux/io.h> |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 16 | #include <linux/ioport.h> |
Bryan Wu | 131b17d | 2007-12-04 23:45:12 -0800 | [diff] [blame] | 17 | #include <linux/irq.h> |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 18 | #include <linux/errno.h> |
| 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/dma-mapping.h> |
| 22 | #include <linux/spi/spi.h> |
| 23 | #include <linux/workqueue.h> |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 24 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 25 | #include <asm/dma.h> |
Bryan Wu | 131b17d | 2007-12-04 23:45:12 -0800 | [diff] [blame] | 26 | #include <asm/portmux.h> |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 27 | #include <asm/bfin5xx_spi.h> |
| 28 | |
Vitja Makarov | 8cf5858 | 2009-04-06 19:00:31 -0700 | [diff] [blame^] | 29 | /* reserved_mem_dcache_on and cache friends */ |
| 30 | #include <asm/cplbinit.h> |
| 31 | #include <asm/cacheflush.h> |
| 32 | |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 33 | #define DRV_NAME "bfin-spi" |
| 34 | #define DRV_AUTHOR "Bryan Wu, Luke Yang" |
Will Newton | 6b1a802 | 2007-12-10 15:49:26 -0800 | [diff] [blame] | 35 | #define DRV_DESC "Blackfin BF5xx on-chip SPI Controller Driver" |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 36 | #define DRV_VERSION "1.0" |
| 37 | |
| 38 | MODULE_AUTHOR(DRV_AUTHOR); |
| 39 | MODULE_DESCRIPTION(DRV_DESC); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 40 | MODULE_LICENSE("GPL"); |
| 41 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 42 | #define IS_DMA_ALIGNED(x) (((u32)(x)&0x07) == 0) |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 43 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 44 | #define START_STATE ((void *)0) |
| 45 | #define RUNNING_STATE ((void *)1) |
| 46 | #define DONE_STATE ((void *)2) |
| 47 | #define ERROR_STATE ((void *)-1) |
| 48 | #define QUEUE_RUNNING 0 |
| 49 | #define QUEUE_STOPPED 1 |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 50 | |
| 51 | struct driver_data { |
| 52 | /* Driver model hookup */ |
| 53 | struct platform_device *pdev; |
| 54 | |
| 55 | /* SPI framework hookup */ |
| 56 | struct spi_master *master; |
| 57 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 58 | /* Regs base of SPI controller */ |
Bryan Wu | f452126 | 2007-12-04 23:45:22 -0800 | [diff] [blame] | 59 | void __iomem *regs_base; |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 60 | |
Bryan Wu | 003d922 | 2007-12-04 23:45:22 -0800 | [diff] [blame] | 61 | /* Pin request list */ |
| 62 | u16 *pin_req; |
| 63 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 64 | /* BFIN hookup */ |
| 65 | struct bfin5xx_spi_master *master_info; |
| 66 | |
| 67 | /* Driver message queue */ |
| 68 | struct workqueue_struct *workqueue; |
| 69 | struct work_struct pump_messages; |
| 70 | spinlock_t lock; |
| 71 | struct list_head queue; |
| 72 | int busy; |
| 73 | int run; |
| 74 | |
| 75 | /* Message Transfer pump */ |
| 76 | struct tasklet_struct pump_transfers; |
| 77 | |
| 78 | /* Current message transfer state info */ |
| 79 | struct spi_message *cur_msg; |
| 80 | struct spi_transfer *cur_transfer; |
| 81 | struct chip_data *cur_chip; |
| 82 | size_t len_in_bytes; |
| 83 | size_t len; |
| 84 | void *tx; |
| 85 | void *tx_end; |
| 86 | void *rx; |
| 87 | void *rx_end; |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 88 | |
| 89 | /* DMA stuffs */ |
| 90 | int dma_channel; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 91 | int dma_mapped; |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 92 | int dma_requested; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 93 | dma_addr_t rx_dma; |
| 94 | dma_addr_t tx_dma; |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 95 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 96 | size_t rx_map_len; |
| 97 | size_t tx_map_len; |
| 98 | u8 n_bytes; |
Bryan Wu | fad91c8 | 2007-12-04 23:45:14 -0800 | [diff] [blame] | 99 | int cs_change; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 100 | void (*write) (struct driver_data *); |
| 101 | void (*read) (struct driver_data *); |
| 102 | void (*duplex) (struct driver_data *); |
| 103 | }; |
| 104 | |
| 105 | struct chip_data { |
| 106 | u16 ctl_reg; |
| 107 | u16 baud; |
| 108 | u16 flag; |
| 109 | |
| 110 | u8 chip_select_num; |
| 111 | u8 n_bytes; |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 112 | u8 width; /* 0 or 1 */ |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 113 | u8 enable_dma; |
| 114 | u8 bits_per_word; /* 8 or 16 */ |
| 115 | u8 cs_change_per_word; |
Bryan Wu | 62310e5 | 2007-12-04 23:45:20 -0800 | [diff] [blame] | 116 | u16 cs_chg_udelay; /* Some devices require > 255usec delay */ |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 117 | void (*write) (struct driver_data *); |
| 118 | void (*read) (struct driver_data *); |
| 119 | void (*duplex) (struct driver_data *); |
| 120 | }; |
| 121 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 122 | #define DEFINE_SPI_REG(reg, off) \ |
| 123 | static inline u16 read_##reg(struct driver_data *drv_data) \ |
| 124 | { return bfin_read16(drv_data->regs_base + off); } \ |
| 125 | static inline void write_##reg(struct driver_data *drv_data, u16 v) \ |
| 126 | { bfin_write16(drv_data->regs_base + off, v); } |
| 127 | |
| 128 | DEFINE_SPI_REG(CTRL, 0x00) |
| 129 | DEFINE_SPI_REG(FLAG, 0x04) |
| 130 | DEFINE_SPI_REG(STAT, 0x08) |
| 131 | DEFINE_SPI_REG(TDBR, 0x0C) |
| 132 | DEFINE_SPI_REG(RDBR, 0x10) |
| 133 | DEFINE_SPI_REG(BAUD, 0x14) |
| 134 | DEFINE_SPI_REG(SHAW, 0x18) |
| 135 | |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 136 | static void bfin_spi_enable(struct driver_data *drv_data) |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 137 | { |
| 138 | u16 cr; |
| 139 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 140 | cr = read_CTRL(drv_data); |
| 141 | write_CTRL(drv_data, (cr | BIT_CTL_ENABLE)); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 144 | static void bfin_spi_disable(struct driver_data *drv_data) |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 145 | { |
| 146 | u16 cr; |
| 147 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 148 | cr = read_CTRL(drv_data); |
| 149 | write_CTRL(drv_data, (cr & (~BIT_CTL_ENABLE))); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /* Caculate the SPI_BAUD register value based on input HZ */ |
| 153 | static u16 hz_to_spi_baud(u32 speed_hz) |
| 154 | { |
| 155 | u_long sclk = get_sclk(); |
| 156 | u16 spi_baud = (sclk / (2 * speed_hz)); |
| 157 | |
| 158 | if ((sclk % (2 * speed_hz)) > 0) |
| 159 | spi_baud++; |
| 160 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 161 | return spi_baud; |
| 162 | } |
| 163 | |
| 164 | static int flush(struct driver_data *drv_data) |
| 165 | { |
| 166 | unsigned long limit = loops_per_jiffy << 1; |
| 167 | |
| 168 | /* wait for stop and clear stat */ |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 169 | while (!(read_STAT(drv_data) & BIT_STAT_SPIF) && limit--) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 170 | cpu_relax(); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 171 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 172 | write_STAT(drv_data, BIT_STAT_CLR); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 173 | |
| 174 | return limit; |
| 175 | } |
| 176 | |
Bryan Wu | fad91c8 | 2007-12-04 23:45:14 -0800 | [diff] [blame] | 177 | /* Chip select operation functions for cs_change flag */ |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 178 | static void cs_active(struct driver_data *drv_data, struct chip_data *chip) |
Bryan Wu | fad91c8 | 2007-12-04 23:45:14 -0800 | [diff] [blame] | 179 | { |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 180 | u16 flag = read_FLAG(drv_data); |
Bryan Wu | fad91c8 | 2007-12-04 23:45:14 -0800 | [diff] [blame] | 181 | |
| 182 | flag |= chip->flag; |
| 183 | flag &= ~(chip->flag << 8); |
| 184 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 185 | write_FLAG(drv_data, flag); |
Bryan Wu | fad91c8 | 2007-12-04 23:45:14 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 188 | static void cs_deactive(struct driver_data *drv_data, struct chip_data *chip) |
Bryan Wu | fad91c8 | 2007-12-04 23:45:14 -0800 | [diff] [blame] | 189 | { |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 190 | u16 flag = read_FLAG(drv_data); |
Bryan Wu | fad91c8 | 2007-12-04 23:45:14 -0800 | [diff] [blame] | 191 | |
| 192 | flag |= (chip->flag << 8); |
| 193 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 194 | write_FLAG(drv_data, flag); |
Bryan Wu | 62310e5 | 2007-12-04 23:45:20 -0800 | [diff] [blame] | 195 | |
| 196 | /* Move delay here for consistency */ |
| 197 | if (chip->cs_chg_udelay) |
| 198 | udelay(chip->cs_chg_udelay); |
Bryan Wu | fad91c8 | 2007-12-04 23:45:14 -0800 | [diff] [blame] | 199 | } |
| 200 | |
Sonic Zhang | 7c4ef09 | 2007-12-04 23:45:16 -0800 | [diff] [blame] | 201 | #define MAX_SPI_SSEL 7 |
Bryan Wu | 5fec5b5 | 2007-12-04 23:45:13 -0800 | [diff] [blame] | 202 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 203 | /* stop controller and re-config current chip*/ |
Bryan Wu | 8d20d0a | 2008-02-06 01:38:17 -0800 | [diff] [blame] | 204 | static void restore_state(struct driver_data *drv_data) |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 205 | { |
| 206 | struct chip_data *chip = drv_data->cur_chip; |
| 207 | |
| 208 | /* Clear status and disable clock */ |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 209 | write_STAT(drv_data, BIT_STAT_CLR); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 210 | bfin_spi_disable(drv_data); |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 211 | dev_dbg(&drv_data->pdev->dev, "restoring spi ctl state\n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 212 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 213 | /* Load the registers */ |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 214 | write_CTRL(drv_data, chip->ctl_reg); |
Bryan Wu | 092e1fd | 2007-12-04 23:45:23 -0800 | [diff] [blame] | 215 | write_BAUD(drv_data, chip->baud); |
Sonic Zhang | cc487e7 | 2007-12-04 23:45:17 -0800 | [diff] [blame] | 216 | |
| 217 | bfin_spi_enable(drv_data); |
Sonic Zhang | 07612e5 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 218 | cs_active(drv_data, chip); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | /* used to kick off transfer in rx mode */ |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 222 | static unsigned short dummy_read(struct driver_data *drv_data) |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 223 | { |
| 224 | unsigned short tmp; |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 225 | tmp = read_RDBR(drv_data); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 226 | return tmp; |
| 227 | } |
| 228 | |
| 229 | static void null_writer(struct driver_data *drv_data) |
| 230 | { |
| 231 | u8 n_bytes = drv_data->n_bytes; |
| 232 | |
| 233 | while (drv_data->tx < drv_data->tx_end) { |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 234 | write_TDBR(drv_data, 0); |
| 235 | while ((read_STAT(drv_data) & BIT_STAT_TXS)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 236 | cpu_relax(); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 237 | drv_data->tx += n_bytes; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | static void null_reader(struct driver_data *drv_data) |
| 242 | { |
| 243 | u8 n_bytes = drv_data->n_bytes; |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 244 | dummy_read(drv_data); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 245 | |
| 246 | while (drv_data->rx < drv_data->rx_end) { |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 247 | while (!(read_STAT(drv_data) & BIT_STAT_RXS)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 248 | cpu_relax(); |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 249 | dummy_read(drv_data); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 250 | drv_data->rx += n_bytes; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | static void u8_writer(struct driver_data *drv_data) |
| 255 | { |
Bryan Wu | 131b17d | 2007-12-04 23:45:12 -0800 | [diff] [blame] | 256 | dev_dbg(&drv_data->pdev->dev, |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 257 | "cr8-s is 0x%x\n", read_STAT(drv_data)); |
Sonic Zhang | cc487e7 | 2007-12-04 23:45:17 -0800 | [diff] [blame] | 258 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 259 | while (drv_data->tx < drv_data->tx_end) { |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 260 | write_TDBR(drv_data, (*(u8 *) (drv_data->tx))); |
| 261 | while (read_STAT(drv_data) & BIT_STAT_TXS) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 262 | cpu_relax(); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 263 | ++drv_data->tx; |
| 264 | } |
Sonic Zhang | 13f3e64 | 2008-02-06 01:38:20 -0800 | [diff] [blame] | 265 | |
| 266 | /* poll for SPI completion before return */ |
| 267 | while (!(read_STAT(drv_data) & BIT_STAT_SPIF)) |
| 268 | cpu_relax(); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | static void u8_cs_chg_writer(struct driver_data *drv_data) |
| 272 | { |
| 273 | struct chip_data *chip = drv_data->cur_chip; |
| 274 | |
| 275 | while (drv_data->tx < drv_data->tx_end) { |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 276 | cs_active(drv_data, chip); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 277 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 278 | write_TDBR(drv_data, (*(u8 *) (drv_data->tx))); |
| 279 | while (read_STAT(drv_data) & BIT_STAT_TXS) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 280 | cpu_relax(); |
Bryan Wu | e26aa01 | 2008-02-06 01:38:18 -0800 | [diff] [blame] | 281 | while (!(read_STAT(drv_data) & BIT_STAT_SPIF)) |
| 282 | cpu_relax(); |
Bryan Wu | 62310e5 | 2007-12-04 23:45:20 -0800 | [diff] [blame] | 283 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 284 | cs_deactive(drv_data, chip); |
Bryan Wu | 5fec5b5 | 2007-12-04 23:45:13 -0800 | [diff] [blame] | 285 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 286 | ++drv_data->tx; |
| 287 | } |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | static void u8_reader(struct driver_data *drv_data) |
| 291 | { |
Bryan Wu | 131b17d | 2007-12-04 23:45:12 -0800 | [diff] [blame] | 292 | dev_dbg(&drv_data->pdev->dev, |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 293 | "cr-8 is 0x%x\n", read_STAT(drv_data)); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 294 | |
Sonic Zhang | 3f479a6 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 295 | /* poll for SPI completion before start */ |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 296 | while (!(read_STAT(drv_data) & BIT_STAT_SPIF)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 297 | cpu_relax(); |
Sonic Zhang | 3f479a6 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 298 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 299 | /* clear TDBR buffer before read(else it will be shifted out) */ |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 300 | write_TDBR(drv_data, 0xFFFF); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 301 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 302 | dummy_read(drv_data); |
Sonic Zhang | cc487e7 | 2007-12-04 23:45:17 -0800 | [diff] [blame] | 303 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 304 | while (drv_data->rx < drv_data->rx_end - 1) { |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 305 | while (!(read_STAT(drv_data) & BIT_STAT_RXS)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 306 | cpu_relax(); |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 307 | *(u8 *) (drv_data->rx) = read_RDBR(drv_data); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 308 | ++drv_data->rx; |
| 309 | } |
| 310 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 311 | while (!(read_STAT(drv_data) & BIT_STAT_RXS)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 312 | cpu_relax(); |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 313 | *(u8 *) (drv_data->rx) = read_SHAW(drv_data); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 314 | ++drv_data->rx; |
| 315 | } |
| 316 | |
| 317 | static void u8_cs_chg_reader(struct driver_data *drv_data) |
| 318 | { |
| 319 | struct chip_data *chip = drv_data->cur_chip; |
| 320 | |
Bryan Wu | e26aa01 | 2008-02-06 01:38:18 -0800 | [diff] [blame] | 321 | while (drv_data->rx < drv_data->rx_end) { |
| 322 | cs_active(drv_data, chip); |
| 323 | read_RDBR(drv_data); /* kick off */ |
Bryan Wu | 5fec5b5 | 2007-12-04 23:45:13 -0800 | [diff] [blame] | 324 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 325 | while (!(read_STAT(drv_data) & BIT_STAT_RXS)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 326 | cpu_relax(); |
Bryan Wu | e26aa01 | 2008-02-06 01:38:18 -0800 | [diff] [blame] | 327 | while (!(read_STAT(drv_data) & BIT_STAT_SPIF)) |
| 328 | cpu_relax(); |
| 329 | |
| 330 | *(u8 *) (drv_data->rx) = read_SHAW(drv_data); |
| 331 | cs_deactive(drv_data, chip); |
| 332 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 333 | ++drv_data->rx; |
| 334 | } |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | static void u8_duplex(struct driver_data *drv_data) |
| 338 | { |
| 339 | /* in duplex mode, clk is triggered by writing of TDBR */ |
| 340 | while (drv_data->rx < drv_data->rx_end) { |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 341 | write_TDBR(drv_data, (*(u8 *) (drv_data->tx))); |
Bryan Wu | 4fd432d | 2008-02-06 01:38:19 -0800 | [diff] [blame] | 342 | while (!(read_STAT(drv_data) & BIT_STAT_SPIF)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 343 | cpu_relax(); |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 344 | while (!(read_STAT(drv_data) & BIT_STAT_RXS)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 345 | cpu_relax(); |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 346 | *(u8 *) (drv_data->rx) = read_RDBR(drv_data); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 347 | ++drv_data->rx; |
| 348 | ++drv_data->tx; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | static void u8_cs_chg_duplex(struct driver_data *drv_data) |
| 353 | { |
| 354 | struct chip_data *chip = drv_data->cur_chip; |
| 355 | |
| 356 | while (drv_data->rx < drv_data->rx_end) { |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 357 | cs_active(drv_data, chip); |
Bryan Wu | 5fec5b5 | 2007-12-04 23:45:13 -0800 | [diff] [blame] | 358 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 359 | write_TDBR(drv_data, (*(u8 *) (drv_data->tx))); |
Bryan Wu | e26aa01 | 2008-02-06 01:38:18 -0800 | [diff] [blame] | 360 | |
| 361 | while (!(read_STAT(drv_data) & BIT_STAT_SPIF)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 362 | cpu_relax(); |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 363 | while (!(read_STAT(drv_data) & BIT_STAT_RXS)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 364 | cpu_relax(); |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 365 | *(u8 *) (drv_data->rx) = read_RDBR(drv_data); |
Bryan Wu | 62310e5 | 2007-12-04 23:45:20 -0800 | [diff] [blame] | 366 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 367 | cs_deactive(drv_data, chip); |
Bryan Wu | 5fec5b5 | 2007-12-04 23:45:13 -0800 | [diff] [blame] | 368 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 369 | ++drv_data->rx; |
| 370 | ++drv_data->tx; |
| 371 | } |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | static void u16_writer(struct driver_data *drv_data) |
| 375 | { |
Bryan Wu | 131b17d | 2007-12-04 23:45:12 -0800 | [diff] [blame] | 376 | dev_dbg(&drv_data->pdev->dev, |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 377 | "cr16 is 0x%x\n", read_STAT(drv_data)); |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 378 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 379 | while (drv_data->tx < drv_data->tx_end) { |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 380 | write_TDBR(drv_data, (*(u16 *) (drv_data->tx))); |
| 381 | while ((read_STAT(drv_data) & BIT_STAT_TXS)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 382 | cpu_relax(); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 383 | drv_data->tx += 2; |
| 384 | } |
Sonic Zhang | 13f3e64 | 2008-02-06 01:38:20 -0800 | [diff] [blame] | 385 | |
| 386 | /* poll for SPI completion before return */ |
| 387 | while (!(read_STAT(drv_data) & BIT_STAT_SPIF)) |
| 388 | cpu_relax(); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | static void u16_cs_chg_writer(struct driver_data *drv_data) |
| 392 | { |
| 393 | struct chip_data *chip = drv_data->cur_chip; |
| 394 | |
| 395 | while (drv_data->tx < drv_data->tx_end) { |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 396 | cs_active(drv_data, chip); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 397 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 398 | write_TDBR(drv_data, (*(u16 *) (drv_data->tx))); |
| 399 | while ((read_STAT(drv_data) & BIT_STAT_TXS)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 400 | cpu_relax(); |
Sonic Zhang | 13f3e64 | 2008-02-06 01:38:20 -0800 | [diff] [blame] | 401 | while (!(read_STAT(drv_data) & BIT_STAT_SPIF)) |
| 402 | cpu_relax(); |
Bryan Wu | 62310e5 | 2007-12-04 23:45:20 -0800 | [diff] [blame] | 403 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 404 | cs_deactive(drv_data, chip); |
Bryan Wu | 5fec5b5 | 2007-12-04 23:45:13 -0800 | [diff] [blame] | 405 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 406 | drv_data->tx += 2; |
| 407 | } |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | static void u16_reader(struct driver_data *drv_data) |
| 411 | { |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 412 | dev_dbg(&drv_data->pdev->dev, |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 413 | "cr-16 is 0x%x\n", read_STAT(drv_data)); |
Sonic Zhang | cc487e7 | 2007-12-04 23:45:17 -0800 | [diff] [blame] | 414 | |
Sonic Zhang | 3f479a6 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 415 | /* poll for SPI completion before start */ |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 416 | while (!(read_STAT(drv_data) & BIT_STAT_SPIF)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 417 | cpu_relax(); |
Sonic Zhang | 3f479a6 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 418 | |
Sonic Zhang | cc487e7 | 2007-12-04 23:45:17 -0800 | [diff] [blame] | 419 | /* clear TDBR buffer before read(else it will be shifted out) */ |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 420 | write_TDBR(drv_data, 0xFFFF); |
Sonic Zhang | cc487e7 | 2007-12-04 23:45:17 -0800 | [diff] [blame] | 421 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 422 | dummy_read(drv_data); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 423 | |
| 424 | while (drv_data->rx < (drv_data->rx_end - 2)) { |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 425 | while (!(read_STAT(drv_data) & BIT_STAT_RXS)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 426 | cpu_relax(); |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 427 | *(u16 *) (drv_data->rx) = read_RDBR(drv_data); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 428 | drv_data->rx += 2; |
| 429 | } |
| 430 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 431 | while (!(read_STAT(drv_data) & BIT_STAT_RXS)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 432 | cpu_relax(); |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 433 | *(u16 *) (drv_data->rx) = read_SHAW(drv_data); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 434 | drv_data->rx += 2; |
| 435 | } |
| 436 | |
| 437 | static void u16_cs_chg_reader(struct driver_data *drv_data) |
| 438 | { |
| 439 | struct chip_data *chip = drv_data->cur_chip; |
| 440 | |
Sonic Zhang | 3f479a6 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 441 | /* poll for SPI completion before start */ |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 442 | while (!(read_STAT(drv_data) & BIT_STAT_SPIF)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 443 | cpu_relax(); |
Sonic Zhang | 3f479a6 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 444 | |
Sonic Zhang | cc487e7 | 2007-12-04 23:45:17 -0800 | [diff] [blame] | 445 | /* clear TDBR buffer before read(else it will be shifted out) */ |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 446 | write_TDBR(drv_data, 0xFFFF); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 447 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 448 | cs_active(drv_data, chip); |
| 449 | dummy_read(drv_data); |
Sonic Zhang | cc487e7 | 2007-12-04 23:45:17 -0800 | [diff] [blame] | 450 | |
Bryan Wu | c3061ab | 2007-12-04 23:45:19 -0800 | [diff] [blame] | 451 | while (drv_data->rx < drv_data->rx_end - 2) { |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 452 | cs_deactive(drv_data, chip); |
Bryan Wu | 5fec5b5 | 2007-12-04 23:45:13 -0800 | [diff] [blame] | 453 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 454 | while (!(read_STAT(drv_data) & BIT_STAT_RXS)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 455 | cpu_relax(); |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 456 | cs_active(drv_data, chip); |
| 457 | *(u16 *) (drv_data->rx) = read_RDBR(drv_data); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 458 | drv_data->rx += 2; |
| 459 | } |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 460 | cs_deactive(drv_data, chip); |
Sonic Zhang | cc487e7 | 2007-12-04 23:45:17 -0800 | [diff] [blame] | 461 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 462 | while (!(read_STAT(drv_data) & BIT_STAT_RXS)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 463 | cpu_relax(); |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 464 | *(u16 *) (drv_data->rx) = read_SHAW(drv_data); |
Sonic Zhang | cc487e7 | 2007-12-04 23:45:17 -0800 | [diff] [blame] | 465 | drv_data->rx += 2; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | static void u16_duplex(struct driver_data *drv_data) |
| 469 | { |
| 470 | /* in duplex mode, clk is triggered by writing of TDBR */ |
| 471 | while (drv_data->tx < drv_data->tx_end) { |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 472 | write_TDBR(drv_data, (*(u16 *) (drv_data->tx))); |
Bryan Wu | 4fd432d | 2008-02-06 01:38:19 -0800 | [diff] [blame] | 473 | while (!(read_STAT(drv_data) & BIT_STAT_SPIF)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 474 | cpu_relax(); |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 475 | while (!(read_STAT(drv_data) & BIT_STAT_RXS)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 476 | cpu_relax(); |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 477 | *(u16 *) (drv_data->rx) = read_RDBR(drv_data); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 478 | drv_data->rx += 2; |
| 479 | drv_data->tx += 2; |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | static void u16_cs_chg_duplex(struct driver_data *drv_data) |
| 484 | { |
| 485 | struct chip_data *chip = drv_data->cur_chip; |
| 486 | |
| 487 | while (drv_data->tx < drv_data->tx_end) { |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 488 | cs_active(drv_data, chip); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 489 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 490 | write_TDBR(drv_data, (*(u16 *) (drv_data->tx))); |
Bryan Wu | 4fd432d | 2008-02-06 01:38:19 -0800 | [diff] [blame] | 491 | while (!(read_STAT(drv_data) & BIT_STAT_SPIF)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 492 | cpu_relax(); |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 493 | while (!(read_STAT(drv_data) & BIT_STAT_RXS)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 494 | cpu_relax(); |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 495 | *(u16 *) (drv_data->rx) = read_RDBR(drv_data); |
Bryan Wu | 62310e5 | 2007-12-04 23:45:20 -0800 | [diff] [blame] | 496 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 497 | cs_deactive(drv_data, chip); |
Bryan Wu | 5fec5b5 | 2007-12-04 23:45:13 -0800 | [diff] [blame] | 498 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 499 | drv_data->rx += 2; |
| 500 | drv_data->tx += 2; |
| 501 | } |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | /* test if ther is more transfer to be done */ |
| 505 | static void *next_transfer(struct driver_data *drv_data) |
| 506 | { |
| 507 | struct spi_message *msg = drv_data->cur_msg; |
| 508 | struct spi_transfer *trans = drv_data->cur_transfer; |
| 509 | |
| 510 | /* Move to next transfer */ |
| 511 | if (trans->transfer_list.next != &msg->transfers) { |
| 512 | drv_data->cur_transfer = |
| 513 | list_entry(trans->transfer_list.next, |
| 514 | struct spi_transfer, transfer_list); |
| 515 | return RUNNING_STATE; |
| 516 | } else |
| 517 | return DONE_STATE; |
| 518 | } |
| 519 | |
| 520 | /* |
| 521 | * caller already set message->status; |
| 522 | * dma and pio irqs are blocked give finished message back |
| 523 | */ |
| 524 | static void giveback(struct driver_data *drv_data) |
| 525 | { |
Bryan Wu | fad91c8 | 2007-12-04 23:45:14 -0800 | [diff] [blame] | 526 | struct chip_data *chip = drv_data->cur_chip; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 527 | struct spi_transfer *last_transfer; |
| 528 | unsigned long flags; |
| 529 | struct spi_message *msg; |
| 530 | |
| 531 | spin_lock_irqsave(&drv_data->lock, flags); |
| 532 | msg = drv_data->cur_msg; |
| 533 | drv_data->cur_msg = NULL; |
| 534 | drv_data->cur_transfer = NULL; |
| 535 | drv_data->cur_chip = NULL; |
| 536 | queue_work(drv_data->workqueue, &drv_data->pump_messages); |
| 537 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 538 | |
| 539 | last_transfer = list_entry(msg->transfers.prev, |
| 540 | struct spi_transfer, transfer_list); |
| 541 | |
| 542 | msg->state = NULL; |
| 543 | |
| 544 | /* disable chip select signal. And not stop spi in autobuffer mode */ |
| 545 | if (drv_data->tx_dma != 0xFFFF) { |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 546 | cs_deactive(drv_data, chip); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 547 | bfin_spi_disable(drv_data); |
| 548 | } |
| 549 | |
Bryan Wu | fad91c8 | 2007-12-04 23:45:14 -0800 | [diff] [blame] | 550 | if (!drv_data->cs_change) |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 551 | cs_deactive(drv_data, chip); |
Bryan Wu | fad91c8 | 2007-12-04 23:45:14 -0800 | [diff] [blame] | 552 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 553 | if (msg->complete) |
| 554 | msg->complete(msg->context); |
| 555 | } |
| 556 | |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 557 | static irqreturn_t dma_irq_handler(int irq, void *dev_id) |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 558 | { |
Jeff Garzik | 15aafa2 | 2008-02-06 01:36:20 -0800 | [diff] [blame] | 559 | struct driver_data *drv_data = dev_id; |
Bryan Wu | fad91c8 | 2007-12-04 23:45:14 -0800 | [diff] [blame] | 560 | struct chip_data *chip = drv_data->cur_chip; |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 561 | struct spi_message *msg = drv_data->cur_msg; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 562 | |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 563 | dev_dbg(&drv_data->pdev->dev, "in dma_irq_handler\n"); |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 564 | clear_dma_irqstat(drv_data->dma_channel); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 565 | |
Bryan Wu | d6fe89b | 2007-06-11 17:34:17 +0800 | [diff] [blame] | 566 | /* Wait for DMA to complete */ |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 567 | while (get_dma_curr_irqstat(drv_data->dma_channel) & DMA_RUN) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 568 | cpu_relax(); |
Bryan Wu | d6fe89b | 2007-06-11 17:34:17 +0800 | [diff] [blame] | 569 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 570 | /* |
Bryan Wu | d6fe89b | 2007-06-11 17:34:17 +0800 | [diff] [blame] | 571 | * wait for the last transaction shifted out. HRM states: |
| 572 | * at this point there may still be data in the SPI DMA FIFO waiting |
| 573 | * to be transmitted ... software needs to poll TXS in the SPI_STAT |
| 574 | * register until it goes low for 2 successive reads |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 575 | */ |
| 576 | if (drv_data->tx != NULL) { |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 577 | while ((read_STAT(drv_data) & TXS) || |
| 578 | (read_STAT(drv_data) & TXS)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 579 | cpu_relax(); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 580 | } |
| 581 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 582 | while (!(read_STAT(drv_data) & SPIF)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 583 | cpu_relax(); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 584 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 585 | msg->actual_length += drv_data->len_in_bytes; |
| 586 | |
Bryan Wu | fad91c8 | 2007-12-04 23:45:14 -0800 | [diff] [blame] | 587 | if (drv_data->cs_change) |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 588 | cs_deactive(drv_data, chip); |
Bryan Wu | fad91c8 | 2007-12-04 23:45:14 -0800 | [diff] [blame] | 589 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 590 | /* Move to next transfer */ |
| 591 | msg->state = next_transfer(drv_data); |
| 592 | |
| 593 | /* Schedule transfer tasklet */ |
| 594 | tasklet_schedule(&drv_data->pump_transfers); |
| 595 | |
| 596 | /* free the irq handler before next transfer */ |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 597 | dev_dbg(&drv_data->pdev->dev, |
| 598 | "disable dma channel irq%d\n", |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 599 | drv_data->dma_channel); |
| 600 | dma_disable_irq(drv_data->dma_channel); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 601 | |
| 602 | return IRQ_HANDLED; |
| 603 | } |
| 604 | |
| 605 | static void pump_transfers(unsigned long data) |
| 606 | { |
| 607 | struct driver_data *drv_data = (struct driver_data *)data; |
| 608 | struct spi_message *message = NULL; |
| 609 | struct spi_transfer *transfer = NULL; |
| 610 | struct spi_transfer *previous = NULL; |
| 611 | struct chip_data *chip = NULL; |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 612 | u8 width; |
| 613 | u16 cr, dma_width, dma_config; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 614 | u32 tranf_success = 1; |
Vitja Makarov | 8eeb12e | 2008-05-01 04:35:03 -0700 | [diff] [blame] | 615 | u8 full_duplex = 0; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 616 | |
| 617 | /* Get current state information */ |
| 618 | message = drv_data->cur_msg; |
| 619 | transfer = drv_data->cur_transfer; |
| 620 | chip = drv_data->cur_chip; |
Bryan Wu | 092e1fd | 2007-12-04 23:45:23 -0800 | [diff] [blame] | 621 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 622 | /* |
| 623 | * if msg is error or done, report it back using complete() callback |
| 624 | */ |
| 625 | |
| 626 | /* Handle for abort */ |
| 627 | if (message->state == ERROR_STATE) { |
| 628 | message->status = -EIO; |
| 629 | giveback(drv_data); |
| 630 | return; |
| 631 | } |
| 632 | |
| 633 | /* Handle end of message */ |
| 634 | if (message->state == DONE_STATE) { |
| 635 | message->status = 0; |
| 636 | giveback(drv_data); |
| 637 | return; |
| 638 | } |
| 639 | |
| 640 | /* Delay if requested at end of transfer */ |
| 641 | if (message->state == RUNNING_STATE) { |
| 642 | previous = list_entry(transfer->transfer_list.prev, |
| 643 | struct spi_transfer, transfer_list); |
| 644 | if (previous->delay_usecs) |
| 645 | udelay(previous->delay_usecs); |
| 646 | } |
| 647 | |
| 648 | /* Setup the transfer state based on the type of transfer */ |
| 649 | if (flush(drv_data) == 0) { |
| 650 | dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n"); |
| 651 | message->status = -EIO; |
| 652 | giveback(drv_data); |
| 653 | return; |
| 654 | } |
| 655 | |
| 656 | if (transfer->tx_buf != NULL) { |
| 657 | drv_data->tx = (void *)transfer->tx_buf; |
| 658 | drv_data->tx_end = drv_data->tx + transfer->len; |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 659 | dev_dbg(&drv_data->pdev->dev, "tx_buf is %p, tx_end is %p\n", |
| 660 | transfer->tx_buf, drv_data->tx_end); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 661 | } else { |
| 662 | drv_data->tx = NULL; |
| 663 | } |
| 664 | |
| 665 | if (transfer->rx_buf != NULL) { |
Vitja Makarov | 8eeb12e | 2008-05-01 04:35:03 -0700 | [diff] [blame] | 666 | full_duplex = transfer->tx_buf != NULL; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 667 | drv_data->rx = transfer->rx_buf; |
| 668 | drv_data->rx_end = drv_data->rx + transfer->len; |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 669 | dev_dbg(&drv_data->pdev->dev, "rx_buf is %p, rx_end is %p\n", |
| 670 | transfer->rx_buf, drv_data->rx_end); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 671 | } else { |
| 672 | drv_data->rx = NULL; |
| 673 | } |
| 674 | |
| 675 | drv_data->rx_dma = transfer->rx_dma; |
| 676 | drv_data->tx_dma = transfer->tx_dma; |
| 677 | drv_data->len_in_bytes = transfer->len; |
Bryan Wu | fad91c8 | 2007-12-04 23:45:14 -0800 | [diff] [blame] | 678 | drv_data->cs_change = transfer->cs_change; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 679 | |
Bryan Wu | 092e1fd | 2007-12-04 23:45:23 -0800 | [diff] [blame] | 680 | /* Bits per word setup */ |
| 681 | switch (transfer->bits_per_word) { |
| 682 | case 8: |
| 683 | drv_data->n_bytes = 1; |
| 684 | width = CFG_SPI_WORDSIZE8; |
| 685 | drv_data->read = chip->cs_change_per_word ? |
| 686 | u8_cs_chg_reader : u8_reader; |
| 687 | drv_data->write = chip->cs_change_per_word ? |
| 688 | u8_cs_chg_writer : u8_writer; |
| 689 | drv_data->duplex = chip->cs_change_per_word ? |
| 690 | u8_cs_chg_duplex : u8_duplex; |
| 691 | break; |
| 692 | |
| 693 | case 16: |
| 694 | drv_data->n_bytes = 2; |
| 695 | width = CFG_SPI_WORDSIZE16; |
| 696 | drv_data->read = chip->cs_change_per_word ? |
| 697 | u16_cs_chg_reader : u16_reader; |
| 698 | drv_data->write = chip->cs_change_per_word ? |
| 699 | u16_cs_chg_writer : u16_writer; |
| 700 | drv_data->duplex = chip->cs_change_per_word ? |
| 701 | u16_cs_chg_duplex : u16_duplex; |
| 702 | break; |
| 703 | |
| 704 | default: |
| 705 | /* No change, the same as default setting */ |
| 706 | drv_data->n_bytes = chip->n_bytes; |
| 707 | width = chip->width; |
| 708 | drv_data->write = drv_data->tx ? chip->write : null_writer; |
| 709 | drv_data->read = drv_data->rx ? chip->read : null_reader; |
| 710 | drv_data->duplex = chip->duplex ? chip->duplex : null_writer; |
| 711 | break; |
| 712 | } |
| 713 | cr = (read_CTRL(drv_data) & (~BIT_CTL_TIMOD)); |
| 714 | cr |= (width << 8); |
| 715 | write_CTRL(drv_data, cr); |
| 716 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 717 | if (width == CFG_SPI_WORDSIZE16) { |
| 718 | drv_data->len = (transfer->len) >> 1; |
| 719 | } else { |
| 720 | drv_data->len = transfer->len; |
| 721 | } |
Mike Frysinger | 4fb98ef | 2008-04-08 17:41:57 -0700 | [diff] [blame] | 722 | dev_dbg(&drv_data->pdev->dev, |
| 723 | "transfer: drv_data->write is %p, chip->write is %p, null_wr is %p\n", |
Bryan Wu | 131b17d | 2007-12-04 23:45:12 -0800 | [diff] [blame] | 724 | drv_data->write, chip->write, null_writer); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 725 | |
| 726 | /* speed and width has been set on per message */ |
| 727 | message->state = RUNNING_STATE; |
| 728 | dma_config = 0; |
| 729 | |
Bryan Wu | 092e1fd | 2007-12-04 23:45:23 -0800 | [diff] [blame] | 730 | /* Speed setup (surely valid because already checked) */ |
| 731 | if (transfer->speed_hz) |
| 732 | write_BAUD(drv_data, hz_to_spi_baud(transfer->speed_hz)); |
| 733 | else |
| 734 | write_BAUD(drv_data, chip->baud); |
| 735 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 736 | write_STAT(drv_data, BIT_STAT_CLR); |
| 737 | cr = (read_CTRL(drv_data) & (~BIT_CTL_TIMOD)); |
| 738 | cs_active(drv_data, chip); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 739 | |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 740 | dev_dbg(&drv_data->pdev->dev, |
| 741 | "now pumping a transfer: width is %d, len is %d\n", |
| 742 | width, transfer->len); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 743 | |
| 744 | /* |
Vitja Makarov | 8cf5858 | 2009-04-06 19:00:31 -0700 | [diff] [blame^] | 745 | * Try to map dma buffer and do a dma transfer. If successful use, |
| 746 | * different way to r/w according to the enable_dma settings and if |
| 747 | * we are not doing a full duplex transfer (since the hardware does |
| 748 | * not support full duplex DMA transfers). |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 749 | */ |
Vitja Makarov | 8eeb12e | 2008-05-01 04:35:03 -0700 | [diff] [blame] | 750 | if (!full_duplex && drv_data->cur_chip->enable_dma |
| 751 | && drv_data->len > 6) { |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 752 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 753 | disable_dma(drv_data->dma_channel); |
| 754 | clear_dma_irqstat(drv_data->dma_channel); |
Sonic Zhang | 07612e5 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 755 | bfin_spi_disable(drv_data); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 756 | |
| 757 | /* config dma channel */ |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 758 | dev_dbg(&drv_data->pdev->dev, "doing dma transfer\n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 759 | if (width == CFG_SPI_WORDSIZE16) { |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 760 | set_dma_x_count(drv_data->dma_channel, drv_data->len); |
| 761 | set_dma_x_modify(drv_data->dma_channel, 2); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 762 | dma_width = WDSIZE_16; |
| 763 | } else { |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 764 | set_dma_x_count(drv_data->dma_channel, drv_data->len); |
| 765 | set_dma_x_modify(drv_data->dma_channel, 1); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 766 | dma_width = WDSIZE_8; |
| 767 | } |
| 768 | |
Sonic Zhang | 3f479a6 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 769 | /* poll for SPI completion before start */ |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 770 | while (!(read_STAT(drv_data) & BIT_STAT_SPIF)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 771 | cpu_relax(); |
Sonic Zhang | 3f479a6 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 772 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 773 | /* dirty hack for autobuffer DMA mode */ |
| 774 | if (drv_data->tx_dma == 0xFFFF) { |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 775 | dev_dbg(&drv_data->pdev->dev, |
| 776 | "doing autobuffer DMA out.\n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 777 | |
| 778 | /* no irq in autobuffer mode */ |
| 779 | dma_config = |
| 780 | (DMAFLOW_AUTO | RESTART | dma_width | DI_EN); |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 781 | set_dma_config(drv_data->dma_channel, dma_config); |
| 782 | set_dma_start_addr(drv_data->dma_channel, |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 783 | (unsigned long)drv_data->tx); |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 784 | enable_dma(drv_data->dma_channel); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 785 | |
Sonic Zhang | 07612e5 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 786 | /* start SPI transfer */ |
| 787 | write_CTRL(drv_data, |
| 788 | (cr | CFG_SPI_DMAWRITE | BIT_CTL_ENABLE)); |
| 789 | |
| 790 | /* just return here, there can only be one transfer |
| 791 | * in this mode |
| 792 | */ |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 793 | message->status = 0; |
| 794 | giveback(drv_data); |
| 795 | return; |
| 796 | } |
| 797 | |
| 798 | /* In dma mode, rx or tx must be NULL in one transfer */ |
| 799 | if (drv_data->rx != NULL) { |
| 800 | /* set transfer mode, and enable SPI */ |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 801 | dev_dbg(&drv_data->pdev->dev, "doing DMA in.\n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 802 | |
Vitja Makarov | 8cf5858 | 2009-04-06 19:00:31 -0700 | [diff] [blame^] | 803 | /* invalidate caches, if needed */ |
| 804 | if (bfin_addr_dcachable((unsigned long) drv_data->rx)) |
| 805 | invalidate_dcache_range((unsigned long) drv_data->rx, |
| 806 | (unsigned long) (drv_data->rx + |
| 807 | drv_data->len)); |
| 808 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 809 | /* clear tx reg soformer data is not shifted out */ |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 810 | write_TDBR(drv_data, 0xFFFF); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 811 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 812 | set_dma_x_count(drv_data->dma_channel, drv_data->len); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 813 | |
| 814 | /* start dma */ |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 815 | dma_enable_irq(drv_data->dma_channel); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 816 | dma_config = (WNR | RESTART | dma_width | DI_EN); |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 817 | set_dma_config(drv_data->dma_channel, dma_config); |
| 818 | set_dma_start_addr(drv_data->dma_channel, |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 819 | (unsigned long)drv_data->rx); |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 820 | enable_dma(drv_data->dma_channel); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 821 | |
Sonic Zhang | 07612e5 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 822 | /* start SPI transfer */ |
| 823 | write_CTRL(drv_data, |
| 824 | (cr | CFG_SPI_DMAREAD | BIT_CTL_ENABLE)); |
| 825 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 826 | } else if (drv_data->tx != NULL) { |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 827 | dev_dbg(&drv_data->pdev->dev, "doing DMA out.\n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 828 | |
Vitja Makarov | 8cf5858 | 2009-04-06 19:00:31 -0700 | [diff] [blame^] | 829 | /* flush caches, if needed */ |
| 830 | if (bfin_addr_dcachable((unsigned long) drv_data->tx)) |
| 831 | flush_dcache_range((unsigned long) drv_data->tx, |
| 832 | (unsigned long) (drv_data->tx + |
| 833 | drv_data->len)); |
| 834 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 835 | /* start dma */ |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 836 | dma_enable_irq(drv_data->dma_channel); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 837 | dma_config = (RESTART | dma_width | DI_EN); |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 838 | set_dma_config(drv_data->dma_channel, dma_config); |
| 839 | set_dma_start_addr(drv_data->dma_channel, |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 840 | (unsigned long)drv_data->tx); |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 841 | enable_dma(drv_data->dma_channel); |
Sonic Zhang | 07612e5 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 842 | |
| 843 | /* start SPI transfer */ |
| 844 | write_CTRL(drv_data, |
| 845 | (cr | CFG_SPI_DMAWRITE | BIT_CTL_ENABLE)); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 846 | } |
| 847 | } else { |
| 848 | /* IO mode write then read */ |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 849 | dev_dbg(&drv_data->pdev->dev, "doing IO transfer\n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 850 | |
Vitja Makarov | 8eeb12e | 2008-05-01 04:35:03 -0700 | [diff] [blame] | 851 | if (full_duplex) { |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 852 | /* full duplex mode */ |
| 853 | BUG_ON((drv_data->tx_end - drv_data->tx) != |
| 854 | (drv_data->rx_end - drv_data->rx)); |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 855 | dev_dbg(&drv_data->pdev->dev, |
| 856 | "IO duplex: cr is 0x%x\n", cr); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 857 | |
Sonic Zhang | cc487e7 | 2007-12-04 23:45:17 -0800 | [diff] [blame] | 858 | /* set SPI transfer mode */ |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 859 | write_CTRL(drv_data, (cr | CFG_SPI_WRITE)); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 860 | |
| 861 | drv_data->duplex(drv_data); |
| 862 | |
| 863 | if (drv_data->tx != drv_data->tx_end) |
| 864 | tranf_success = 0; |
| 865 | } else if (drv_data->tx != NULL) { |
| 866 | /* write only half duplex */ |
Bryan Wu | 131b17d | 2007-12-04 23:45:12 -0800 | [diff] [blame] | 867 | dev_dbg(&drv_data->pdev->dev, |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 868 | "IO write: cr is 0x%x\n", cr); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 869 | |
Sonic Zhang | cc487e7 | 2007-12-04 23:45:17 -0800 | [diff] [blame] | 870 | /* set SPI transfer mode */ |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 871 | write_CTRL(drv_data, (cr | CFG_SPI_WRITE)); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 872 | |
| 873 | drv_data->write(drv_data); |
| 874 | |
| 875 | if (drv_data->tx != drv_data->tx_end) |
| 876 | tranf_success = 0; |
| 877 | } else if (drv_data->rx != NULL) { |
| 878 | /* read only half duplex */ |
Bryan Wu | 131b17d | 2007-12-04 23:45:12 -0800 | [diff] [blame] | 879 | dev_dbg(&drv_data->pdev->dev, |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 880 | "IO read: cr is 0x%x\n", cr); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 881 | |
Sonic Zhang | cc487e7 | 2007-12-04 23:45:17 -0800 | [diff] [blame] | 882 | /* set SPI transfer mode */ |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 883 | write_CTRL(drv_data, (cr | CFG_SPI_READ)); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 884 | |
| 885 | drv_data->read(drv_data); |
| 886 | if (drv_data->rx != drv_data->rx_end) |
| 887 | tranf_success = 0; |
| 888 | } |
| 889 | |
| 890 | if (!tranf_success) { |
Bryan Wu | 131b17d | 2007-12-04 23:45:12 -0800 | [diff] [blame] | 891 | dev_dbg(&drv_data->pdev->dev, |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 892 | "IO write error!\n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 893 | message->state = ERROR_STATE; |
| 894 | } else { |
| 895 | /* Update total byte transfered */ |
| 896 | message->actual_length += drv_data->len; |
| 897 | |
| 898 | /* Move to next transfer of this msg */ |
| 899 | message->state = next_transfer(drv_data); |
| 900 | } |
| 901 | |
| 902 | /* Schedule next transfer tasklet */ |
| 903 | tasklet_schedule(&drv_data->pump_transfers); |
| 904 | |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | /* pop a msg from queue and kick off real transfer */ |
| 909 | static void pump_messages(struct work_struct *work) |
| 910 | { |
Bryan Wu | 131b17d | 2007-12-04 23:45:12 -0800 | [diff] [blame] | 911 | struct driver_data *drv_data; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 912 | unsigned long flags; |
| 913 | |
Bryan Wu | 131b17d | 2007-12-04 23:45:12 -0800 | [diff] [blame] | 914 | drv_data = container_of(work, struct driver_data, pump_messages); |
| 915 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 916 | /* Lock queue and check for queue work */ |
| 917 | spin_lock_irqsave(&drv_data->lock, flags); |
| 918 | if (list_empty(&drv_data->queue) || drv_data->run == QUEUE_STOPPED) { |
| 919 | /* pumper kicked off but no work to do */ |
| 920 | drv_data->busy = 0; |
| 921 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 922 | return; |
| 923 | } |
| 924 | |
| 925 | /* Make sure we are not already running a message */ |
| 926 | if (drv_data->cur_msg) { |
| 927 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 928 | return; |
| 929 | } |
| 930 | |
| 931 | /* Extract head of queue */ |
| 932 | drv_data->cur_msg = list_entry(drv_data->queue.next, |
| 933 | struct spi_message, queue); |
Bryan Wu | 5fec5b5 | 2007-12-04 23:45:13 -0800 | [diff] [blame] | 934 | |
| 935 | /* Setup the SSP using the per chip configuration */ |
| 936 | drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi); |
Bryan Wu | 8d20d0a | 2008-02-06 01:38:17 -0800 | [diff] [blame] | 937 | restore_state(drv_data); |
Bryan Wu | 5fec5b5 | 2007-12-04 23:45:13 -0800 | [diff] [blame] | 938 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 939 | list_del_init(&drv_data->cur_msg->queue); |
| 940 | |
| 941 | /* Initial message state */ |
| 942 | drv_data->cur_msg->state = START_STATE; |
| 943 | drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next, |
| 944 | struct spi_transfer, transfer_list); |
| 945 | |
Bryan Wu | 5fec5b5 | 2007-12-04 23:45:13 -0800 | [diff] [blame] | 946 | dev_dbg(&drv_data->pdev->dev, "got a message to pump, " |
| 947 | "state is set to: baud %d, flag 0x%x, ctl 0x%x\n", |
| 948 | drv_data->cur_chip->baud, drv_data->cur_chip->flag, |
| 949 | drv_data->cur_chip->ctl_reg); |
Bryan Wu | 131b17d | 2007-12-04 23:45:12 -0800 | [diff] [blame] | 950 | |
| 951 | dev_dbg(&drv_data->pdev->dev, |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 952 | "the first transfer len is %d\n", |
| 953 | drv_data->cur_transfer->len); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 954 | |
| 955 | /* Mark as busy and launch transfers */ |
| 956 | tasklet_schedule(&drv_data->pump_transfers); |
| 957 | |
| 958 | drv_data->busy = 1; |
| 959 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 960 | } |
| 961 | |
| 962 | /* |
| 963 | * got a msg to transfer, queue it in drv_data->queue. |
| 964 | * And kick off message pumper |
| 965 | */ |
| 966 | static int transfer(struct spi_device *spi, struct spi_message *msg) |
| 967 | { |
| 968 | struct driver_data *drv_data = spi_master_get_devdata(spi->master); |
| 969 | unsigned long flags; |
| 970 | |
| 971 | spin_lock_irqsave(&drv_data->lock, flags); |
| 972 | |
| 973 | if (drv_data->run == QUEUE_STOPPED) { |
| 974 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 975 | return -ESHUTDOWN; |
| 976 | } |
| 977 | |
| 978 | msg->actual_length = 0; |
| 979 | msg->status = -EINPROGRESS; |
| 980 | msg->state = START_STATE; |
| 981 | |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 982 | dev_dbg(&spi->dev, "adding an msg in transfer() \n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 983 | list_add_tail(&msg->queue, &drv_data->queue); |
| 984 | |
| 985 | if (drv_data->run == QUEUE_RUNNING && !drv_data->busy) |
| 986 | queue_work(drv_data->workqueue, &drv_data->pump_messages); |
| 987 | |
| 988 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 989 | |
| 990 | return 0; |
| 991 | } |
| 992 | |
Sonic Zhang | 12e17c4 | 2007-12-04 23:45:16 -0800 | [diff] [blame] | 993 | #define MAX_SPI_SSEL 7 |
| 994 | |
| 995 | static u16 ssel[3][MAX_SPI_SSEL] = { |
| 996 | {P_SPI0_SSEL1, P_SPI0_SSEL2, P_SPI0_SSEL3, |
| 997 | P_SPI0_SSEL4, P_SPI0_SSEL5, |
| 998 | P_SPI0_SSEL6, P_SPI0_SSEL7}, |
| 999 | |
| 1000 | {P_SPI1_SSEL1, P_SPI1_SSEL2, P_SPI1_SSEL3, |
| 1001 | P_SPI1_SSEL4, P_SPI1_SSEL5, |
| 1002 | P_SPI1_SSEL6, P_SPI1_SSEL7}, |
| 1003 | |
| 1004 | {P_SPI2_SSEL1, P_SPI2_SSEL2, P_SPI2_SSEL3, |
| 1005 | P_SPI2_SSEL4, P_SPI2_SSEL5, |
| 1006 | P_SPI2_SSEL6, P_SPI2_SSEL7}, |
| 1007 | }; |
| 1008 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1009 | /* first setup for new devices */ |
| 1010 | static int setup(struct spi_device *spi) |
| 1011 | { |
| 1012 | struct bfin5xx_spi_chip *chip_info = NULL; |
| 1013 | struct chip_data *chip; |
| 1014 | struct driver_data *drv_data = spi_master_get_devdata(spi->master); |
| 1015 | u8 spi_flg; |
| 1016 | |
| 1017 | /* Abort device setup if requested features are not supported */ |
| 1018 | if (spi->mode & ~(SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST)) { |
| 1019 | dev_err(&spi->dev, "requested mode not fully supported\n"); |
| 1020 | return -EINVAL; |
| 1021 | } |
| 1022 | |
| 1023 | /* Zero (the default) here means 8 bits */ |
| 1024 | if (!spi->bits_per_word) |
| 1025 | spi->bits_per_word = 8; |
| 1026 | |
| 1027 | if (spi->bits_per_word != 8 && spi->bits_per_word != 16) |
| 1028 | return -EINVAL; |
| 1029 | |
| 1030 | /* Only alloc (or use chip_info) on first setup */ |
| 1031 | chip = spi_get_ctldata(spi); |
| 1032 | if (chip == NULL) { |
| 1033 | chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL); |
| 1034 | if (!chip) |
| 1035 | return -ENOMEM; |
| 1036 | |
| 1037 | chip->enable_dma = 0; |
| 1038 | chip_info = spi->controller_data; |
| 1039 | } |
| 1040 | |
| 1041 | /* chip_info isn't always needed */ |
| 1042 | if (chip_info) { |
Mike Frysinger | 2ed3551 | 2007-12-04 23:45:14 -0800 | [diff] [blame] | 1043 | /* Make sure people stop trying to set fields via ctl_reg |
| 1044 | * when they should actually be using common SPI framework. |
| 1045 | * Currently we let through: WOM EMISO PSSE GM SZ TIMOD. |
| 1046 | * Not sure if a user actually needs/uses any of these, |
| 1047 | * but let's assume (for now) they do. |
| 1048 | */ |
| 1049 | if (chip_info->ctl_reg & (SPE|MSTR|CPOL|CPHA|LSBF|SIZE)) { |
| 1050 | dev_err(&spi->dev, "do not set bits in ctl_reg " |
| 1051 | "that the SPI framework manages\n"); |
| 1052 | return -EINVAL; |
| 1053 | } |
| 1054 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1055 | chip->enable_dma = chip_info->enable_dma != 0 |
| 1056 | && drv_data->master_info->enable_dma; |
| 1057 | chip->ctl_reg = chip_info->ctl_reg; |
| 1058 | chip->bits_per_word = chip_info->bits_per_word; |
| 1059 | chip->cs_change_per_word = chip_info->cs_change_per_word; |
| 1060 | chip->cs_chg_udelay = chip_info->cs_chg_udelay; |
| 1061 | } |
| 1062 | |
| 1063 | /* translate common spi framework into our register */ |
| 1064 | if (spi->mode & SPI_CPOL) |
| 1065 | chip->ctl_reg |= CPOL; |
| 1066 | if (spi->mode & SPI_CPHA) |
| 1067 | chip->ctl_reg |= CPHA; |
| 1068 | if (spi->mode & SPI_LSB_FIRST) |
| 1069 | chip->ctl_reg |= LSBF; |
| 1070 | /* we dont support running in slave mode (yet?) */ |
| 1071 | chip->ctl_reg |= MSTR; |
| 1072 | |
| 1073 | /* |
| 1074 | * if any one SPI chip is registered and wants DMA, request the |
| 1075 | * DMA channel for it |
| 1076 | */ |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 1077 | if (chip->enable_dma && !drv_data->dma_requested) { |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1078 | /* register dma irq handler */ |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 1079 | if (request_dma(drv_data->dma_channel, "BF53x_SPI_DMA") < 0) { |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 1080 | dev_dbg(&spi->dev, |
| 1081 | "Unable to request BlackFin SPI DMA channel\n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1082 | return -ENODEV; |
| 1083 | } |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 1084 | if (set_dma_callback(drv_data->dma_channel, |
| 1085 | (void *)dma_irq_handler, drv_data) < 0) { |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 1086 | dev_dbg(&spi->dev, "Unable to set dma callback\n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1087 | return -EPERM; |
| 1088 | } |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 1089 | dma_disable_irq(drv_data->dma_channel); |
| 1090 | drv_data->dma_requested = 1; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | /* |
| 1094 | * Notice: for blackfin, the speed_hz is the value of register |
| 1095 | * SPI_BAUD, not the real baudrate |
| 1096 | */ |
| 1097 | chip->baud = hz_to_spi_baud(spi->max_speed_hz); |
| 1098 | spi_flg = ~(1 << (spi->chip_select)); |
| 1099 | chip->flag = ((u16) spi_flg << 8) | (1 << (spi->chip_select)); |
| 1100 | chip->chip_select_num = spi->chip_select; |
| 1101 | |
| 1102 | switch (chip->bits_per_word) { |
| 1103 | case 8: |
| 1104 | chip->n_bytes = 1; |
| 1105 | chip->width = CFG_SPI_WORDSIZE8; |
| 1106 | chip->read = chip->cs_change_per_word ? |
| 1107 | u8_cs_chg_reader : u8_reader; |
| 1108 | chip->write = chip->cs_change_per_word ? |
| 1109 | u8_cs_chg_writer : u8_writer; |
| 1110 | chip->duplex = chip->cs_change_per_word ? |
| 1111 | u8_cs_chg_duplex : u8_duplex; |
| 1112 | break; |
| 1113 | |
| 1114 | case 16: |
| 1115 | chip->n_bytes = 2; |
| 1116 | chip->width = CFG_SPI_WORDSIZE16; |
| 1117 | chip->read = chip->cs_change_per_word ? |
| 1118 | u16_cs_chg_reader : u16_reader; |
| 1119 | chip->write = chip->cs_change_per_word ? |
| 1120 | u16_cs_chg_writer : u16_writer; |
| 1121 | chip->duplex = chip->cs_change_per_word ? |
| 1122 | u16_cs_chg_duplex : u16_duplex; |
| 1123 | break; |
| 1124 | |
| 1125 | default: |
| 1126 | dev_err(&spi->dev, "%d bits_per_word is not supported\n", |
| 1127 | chip->bits_per_word); |
| 1128 | kfree(chip); |
| 1129 | return -ENODEV; |
| 1130 | } |
| 1131 | |
Joe Perches | 898eb71 | 2007-10-18 03:06:30 -0700 | [diff] [blame] | 1132 | dev_dbg(&spi->dev, "setup spi chip %s, width is %d, dma is %d\n", |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1133 | spi->modalias, chip->width, chip->enable_dma); |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 1134 | dev_dbg(&spi->dev, "ctl_reg is 0x%x, flag_reg is 0x%x\n", |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1135 | chip->ctl_reg, chip->flag); |
| 1136 | |
| 1137 | spi_set_ctldata(spi, chip); |
| 1138 | |
Sonic Zhang | 12e17c4 | 2007-12-04 23:45:16 -0800 | [diff] [blame] | 1139 | dev_dbg(&spi->dev, "chip select number is %d\n", chip->chip_select_num); |
| 1140 | if ((chip->chip_select_num > 0) |
| 1141 | && (chip->chip_select_num <= spi->master->num_chipselect)) |
| 1142 | peripheral_request(ssel[spi->master->bus_num] |
Bryan Wu | aab0d83 | 2008-02-06 01:38:17 -0800 | [diff] [blame] | 1143 | [chip->chip_select_num-1], spi->modalias); |
Sonic Zhang | 12e17c4 | 2007-12-04 23:45:16 -0800 | [diff] [blame] | 1144 | |
Sonic Zhang | 07612e5 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 1145 | cs_deactive(drv_data, chip); |
| 1146 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1147 | return 0; |
| 1148 | } |
| 1149 | |
| 1150 | /* |
| 1151 | * callback for spi framework. |
| 1152 | * clean driver specific data |
| 1153 | */ |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 1154 | static void cleanup(struct spi_device *spi) |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1155 | { |
Mike Frysinger | 27bb9e7 | 2007-06-11 15:31:30 +0800 | [diff] [blame] | 1156 | struct chip_data *chip = spi_get_ctldata(spi); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1157 | |
Sonic Zhang | 12e17c4 | 2007-12-04 23:45:16 -0800 | [diff] [blame] | 1158 | if ((chip->chip_select_num > 0) |
| 1159 | && (chip->chip_select_num <= spi->master->num_chipselect)) |
| 1160 | peripheral_free(ssel[spi->master->bus_num] |
| 1161 | [chip->chip_select_num-1]); |
| 1162 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1163 | kfree(chip); |
| 1164 | } |
| 1165 | |
| 1166 | static inline int init_queue(struct driver_data *drv_data) |
| 1167 | { |
| 1168 | INIT_LIST_HEAD(&drv_data->queue); |
| 1169 | spin_lock_init(&drv_data->lock); |
| 1170 | |
| 1171 | drv_data->run = QUEUE_STOPPED; |
| 1172 | drv_data->busy = 0; |
| 1173 | |
| 1174 | /* init transfer tasklet */ |
| 1175 | tasklet_init(&drv_data->pump_transfers, |
| 1176 | pump_transfers, (unsigned long)drv_data); |
| 1177 | |
| 1178 | /* init messages workqueue */ |
| 1179 | INIT_WORK(&drv_data->pump_messages, pump_messages); |
Kay Sievers | 6c7377a | 2009-03-24 16:38:21 -0700 | [diff] [blame] | 1180 | drv_data->workqueue = create_singlethread_workqueue( |
| 1181 | dev_name(drv_data->master->dev.parent)); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1182 | if (drv_data->workqueue == NULL) |
| 1183 | return -EBUSY; |
| 1184 | |
| 1185 | return 0; |
| 1186 | } |
| 1187 | |
| 1188 | static inline int start_queue(struct driver_data *drv_data) |
| 1189 | { |
| 1190 | unsigned long flags; |
| 1191 | |
| 1192 | spin_lock_irqsave(&drv_data->lock, flags); |
| 1193 | |
| 1194 | if (drv_data->run == QUEUE_RUNNING || drv_data->busy) { |
| 1195 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 1196 | return -EBUSY; |
| 1197 | } |
| 1198 | |
| 1199 | drv_data->run = QUEUE_RUNNING; |
| 1200 | drv_data->cur_msg = NULL; |
| 1201 | drv_data->cur_transfer = NULL; |
| 1202 | drv_data->cur_chip = NULL; |
| 1203 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 1204 | |
| 1205 | queue_work(drv_data->workqueue, &drv_data->pump_messages); |
| 1206 | |
| 1207 | return 0; |
| 1208 | } |
| 1209 | |
| 1210 | static inline int stop_queue(struct driver_data *drv_data) |
| 1211 | { |
| 1212 | unsigned long flags; |
| 1213 | unsigned limit = 500; |
| 1214 | int status = 0; |
| 1215 | |
| 1216 | spin_lock_irqsave(&drv_data->lock, flags); |
| 1217 | |
| 1218 | /* |
| 1219 | * This is a bit lame, but is optimized for the common execution path. |
| 1220 | * A wait_queue on the drv_data->busy could be used, but then the common |
| 1221 | * execution path (pump_messages) would be required to call wake_up or |
| 1222 | * friends on every SPI message. Do this instead |
| 1223 | */ |
| 1224 | drv_data->run = QUEUE_STOPPED; |
| 1225 | while (!list_empty(&drv_data->queue) && drv_data->busy && limit--) { |
| 1226 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 1227 | msleep(10); |
| 1228 | spin_lock_irqsave(&drv_data->lock, flags); |
| 1229 | } |
| 1230 | |
| 1231 | if (!list_empty(&drv_data->queue) || drv_data->busy) |
| 1232 | status = -EBUSY; |
| 1233 | |
| 1234 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 1235 | |
| 1236 | return status; |
| 1237 | } |
| 1238 | |
| 1239 | static inline int destroy_queue(struct driver_data *drv_data) |
| 1240 | { |
| 1241 | int status; |
| 1242 | |
| 1243 | status = stop_queue(drv_data); |
| 1244 | if (status != 0) |
| 1245 | return status; |
| 1246 | |
| 1247 | destroy_workqueue(drv_data->workqueue); |
| 1248 | |
| 1249 | return 0; |
| 1250 | } |
| 1251 | |
| 1252 | static int __init bfin5xx_spi_probe(struct platform_device *pdev) |
| 1253 | { |
| 1254 | struct device *dev = &pdev->dev; |
| 1255 | struct bfin5xx_spi_master *platform_info; |
| 1256 | struct spi_master *master; |
| 1257 | struct driver_data *drv_data = 0; |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1258 | struct resource *res; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1259 | int status = 0; |
| 1260 | |
| 1261 | platform_info = dev->platform_data; |
| 1262 | |
| 1263 | /* Allocate master with space for drv_data */ |
| 1264 | master = spi_alloc_master(dev, sizeof(struct driver_data) + 16); |
| 1265 | if (!master) { |
| 1266 | dev_err(&pdev->dev, "can not alloc spi_master\n"); |
| 1267 | return -ENOMEM; |
| 1268 | } |
Bryan Wu | 131b17d | 2007-12-04 23:45:12 -0800 | [diff] [blame] | 1269 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1270 | drv_data = spi_master_get_devdata(master); |
| 1271 | drv_data->master = master; |
| 1272 | drv_data->master_info = platform_info; |
| 1273 | drv_data->pdev = pdev; |
Bryan Wu | 003d922 | 2007-12-04 23:45:22 -0800 | [diff] [blame] | 1274 | drv_data->pin_req = platform_info->pin_req; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1275 | |
| 1276 | master->bus_num = pdev->id; |
| 1277 | master->num_chipselect = platform_info->num_chipselect; |
| 1278 | master->cleanup = cleanup; |
| 1279 | master->setup = setup; |
| 1280 | master->transfer = transfer; |
| 1281 | |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1282 | /* Find and map our resources */ |
| 1283 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1284 | if (res == NULL) { |
| 1285 | dev_err(dev, "Cannot get IORESOURCE_MEM\n"); |
| 1286 | status = -ENOENT; |
| 1287 | goto out_error_get_res; |
| 1288 | } |
| 1289 | |
Bryan Wu | f452126 | 2007-12-04 23:45:22 -0800 | [diff] [blame] | 1290 | drv_data->regs_base = ioremap(res->start, (res->end - res->start + 1)); |
| 1291 | if (drv_data->regs_base == NULL) { |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1292 | dev_err(dev, "Cannot map IO\n"); |
| 1293 | status = -ENXIO; |
| 1294 | goto out_error_ioremap; |
| 1295 | } |
| 1296 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 1297 | drv_data->dma_channel = platform_get_irq(pdev, 0); |
| 1298 | if (drv_data->dma_channel < 0) { |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1299 | dev_err(dev, "No DMA channel specified\n"); |
| 1300 | status = -ENOENT; |
| 1301 | goto out_error_no_dma_ch; |
| 1302 | } |
| 1303 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1304 | /* Initial and start queue */ |
| 1305 | status = init_queue(drv_data); |
| 1306 | if (status != 0) { |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1307 | dev_err(dev, "problem initializing queue\n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1308 | goto out_error_queue_alloc; |
| 1309 | } |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1310 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1311 | status = start_queue(drv_data); |
| 1312 | if (status != 0) { |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1313 | dev_err(dev, "problem starting queue\n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1314 | goto out_error_queue_alloc; |
| 1315 | } |
| 1316 | |
Vitja Makarov | f9e522c | 2008-04-08 17:41:57 -0700 | [diff] [blame] | 1317 | status = peripheral_request_list(drv_data->pin_req, DRV_NAME); |
| 1318 | if (status != 0) { |
| 1319 | dev_err(&pdev->dev, ": Requesting Peripherals failed\n"); |
| 1320 | goto out_error_queue_alloc; |
| 1321 | } |
| 1322 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1323 | /* Register with the SPI framework */ |
| 1324 | platform_set_drvdata(pdev, drv_data); |
| 1325 | status = spi_register_master(master); |
| 1326 | if (status != 0) { |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1327 | dev_err(dev, "problem registering spi master\n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1328 | goto out_error_queue_alloc; |
| 1329 | } |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1330 | |
Bryan Wu | f452126 | 2007-12-04 23:45:22 -0800 | [diff] [blame] | 1331 | dev_info(dev, "%s, Version %s, regs_base@%p, dma channel@%d\n", |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 1332 | DRV_DESC, DRV_VERSION, drv_data->regs_base, |
| 1333 | drv_data->dma_channel); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1334 | return status; |
| 1335 | |
Michael Hennerich | cc2f81a | 2007-12-04 23:45:13 -0800 | [diff] [blame] | 1336 | out_error_queue_alloc: |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1337 | destroy_queue(drv_data); |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1338 | out_error_no_dma_ch: |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 1339 | iounmap((void *) drv_data->regs_base); |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1340 | out_error_ioremap: |
| 1341 | out_error_get_res: |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1342 | spi_master_put(master); |
Michael Hennerich | cc2f81a | 2007-12-04 23:45:13 -0800 | [diff] [blame] | 1343 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1344 | return status; |
| 1345 | } |
| 1346 | |
| 1347 | /* stop hardware and remove the driver */ |
| 1348 | static int __devexit bfin5xx_spi_remove(struct platform_device *pdev) |
| 1349 | { |
| 1350 | struct driver_data *drv_data = platform_get_drvdata(pdev); |
| 1351 | int status = 0; |
| 1352 | |
| 1353 | if (!drv_data) |
| 1354 | return 0; |
| 1355 | |
| 1356 | /* Remove the queue */ |
| 1357 | status = destroy_queue(drv_data); |
| 1358 | if (status != 0) |
| 1359 | return status; |
| 1360 | |
| 1361 | /* Disable the SSP at the peripheral and SOC level */ |
| 1362 | bfin_spi_disable(drv_data); |
| 1363 | |
| 1364 | /* Release DMA */ |
| 1365 | if (drv_data->master_info->enable_dma) { |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 1366 | if (dma_channel_active(drv_data->dma_channel)) |
| 1367 | free_dma(drv_data->dma_channel); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1368 | } |
| 1369 | |
| 1370 | /* Disconnect from the SPI framework */ |
| 1371 | spi_unregister_master(drv_data->master); |
| 1372 | |
Bryan Wu | 003d922 | 2007-12-04 23:45:22 -0800 | [diff] [blame] | 1373 | peripheral_free_list(drv_data->pin_req); |
Michael Hennerich | cc2f81a | 2007-12-04 23:45:13 -0800 | [diff] [blame] | 1374 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1375 | /* Prevent double remove */ |
| 1376 | platform_set_drvdata(pdev, NULL); |
| 1377 | |
| 1378 | return 0; |
| 1379 | } |
| 1380 | |
| 1381 | #ifdef CONFIG_PM |
| 1382 | static int bfin5xx_spi_suspend(struct platform_device *pdev, pm_message_t state) |
| 1383 | { |
| 1384 | struct driver_data *drv_data = platform_get_drvdata(pdev); |
| 1385 | int status = 0; |
| 1386 | |
| 1387 | status = stop_queue(drv_data); |
| 1388 | if (status != 0) |
| 1389 | return status; |
| 1390 | |
| 1391 | /* stop hardware */ |
| 1392 | bfin_spi_disable(drv_data); |
| 1393 | |
| 1394 | return 0; |
| 1395 | } |
| 1396 | |
| 1397 | static int bfin5xx_spi_resume(struct platform_device *pdev) |
| 1398 | { |
| 1399 | struct driver_data *drv_data = platform_get_drvdata(pdev); |
| 1400 | int status = 0; |
| 1401 | |
| 1402 | /* Enable the SPI interface */ |
| 1403 | bfin_spi_enable(drv_data); |
| 1404 | |
| 1405 | /* Start the queue running */ |
| 1406 | status = start_queue(drv_data); |
| 1407 | if (status != 0) { |
| 1408 | dev_err(&pdev->dev, "problem starting queue (%d)\n", status); |
| 1409 | return status; |
| 1410 | } |
| 1411 | |
| 1412 | return 0; |
| 1413 | } |
| 1414 | #else |
| 1415 | #define bfin5xx_spi_suspend NULL |
| 1416 | #define bfin5xx_spi_resume NULL |
| 1417 | #endif /* CONFIG_PM */ |
| 1418 | |
Kay Sievers | 7e38c3c | 2008-04-10 21:29:20 -0700 | [diff] [blame] | 1419 | MODULE_ALIAS("platform:bfin-spi"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1420 | static struct platform_driver bfin5xx_spi_driver = { |
David Brownell | fc3ba95 | 2007-08-30 23:56:24 -0700 | [diff] [blame] | 1421 | .driver = { |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1422 | .name = DRV_NAME, |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 1423 | .owner = THIS_MODULE, |
| 1424 | }, |
| 1425 | .suspend = bfin5xx_spi_suspend, |
| 1426 | .resume = bfin5xx_spi_resume, |
| 1427 | .remove = __devexit_p(bfin5xx_spi_remove), |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1428 | }; |
| 1429 | |
| 1430 | static int __init bfin5xx_spi_init(void) |
| 1431 | { |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 1432 | return platform_driver_probe(&bfin5xx_spi_driver, bfin5xx_spi_probe); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1433 | } |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1434 | module_init(bfin5xx_spi_init); |
| 1435 | |
| 1436 | static void __exit bfin5xx_spi_exit(void) |
| 1437 | { |
| 1438 | platform_driver_unregister(&bfin5xx_spi_driver); |
| 1439 | } |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1440 | module_exit(bfin5xx_spi_exit); |