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> |
Vitja Makarov | 8cf5858 | 2009-04-06 19:00:31 -0700 | [diff] [blame] | 28 | #include <asm/cacheflush.h> |
| 29 | |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 30 | #define DRV_NAME "bfin-spi" |
| 31 | #define DRV_AUTHOR "Bryan Wu, Luke Yang" |
Will Newton | 6b1a802 | 2007-12-10 15:49:26 -0800 | [diff] [blame] | 32 | #define DRV_DESC "Blackfin BF5xx on-chip SPI Controller Driver" |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 33 | #define DRV_VERSION "1.0" |
| 34 | |
| 35 | MODULE_AUTHOR(DRV_AUTHOR); |
| 36 | MODULE_DESCRIPTION(DRV_DESC); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 37 | MODULE_LICENSE("GPL"); |
| 38 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 39 | #define IS_DMA_ALIGNED(x) (((u32)(x)&0x07) == 0) |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 40 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 41 | #define START_STATE ((void *)0) |
| 42 | #define RUNNING_STATE ((void *)1) |
| 43 | #define DONE_STATE ((void *)2) |
| 44 | #define ERROR_STATE ((void *)-1) |
| 45 | #define QUEUE_RUNNING 0 |
| 46 | #define QUEUE_STOPPED 1 |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 47 | |
| 48 | struct driver_data { |
| 49 | /* Driver model hookup */ |
| 50 | struct platform_device *pdev; |
| 51 | |
| 52 | /* SPI framework hookup */ |
| 53 | struct spi_master *master; |
| 54 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 55 | /* Regs base of SPI controller */ |
Bryan Wu | f452126 | 2007-12-04 23:45:22 -0800 | [diff] [blame] | 56 | void __iomem *regs_base; |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 57 | |
Bryan Wu | 003d922 | 2007-12-04 23:45:22 -0800 | [diff] [blame] | 58 | /* Pin request list */ |
| 59 | u16 *pin_req; |
| 60 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 61 | /* BFIN hookup */ |
| 62 | struct bfin5xx_spi_master *master_info; |
| 63 | |
| 64 | /* Driver message queue */ |
| 65 | struct workqueue_struct *workqueue; |
| 66 | struct work_struct pump_messages; |
| 67 | spinlock_t lock; |
| 68 | struct list_head queue; |
| 69 | int busy; |
| 70 | int run; |
| 71 | |
| 72 | /* Message Transfer pump */ |
| 73 | struct tasklet_struct pump_transfers; |
| 74 | |
| 75 | /* Current message transfer state info */ |
| 76 | struct spi_message *cur_msg; |
| 77 | struct spi_transfer *cur_transfer; |
| 78 | struct chip_data *cur_chip; |
| 79 | size_t len_in_bytes; |
| 80 | size_t len; |
| 81 | void *tx; |
| 82 | void *tx_end; |
| 83 | void *rx; |
| 84 | void *rx_end; |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 85 | |
| 86 | /* DMA stuffs */ |
| 87 | int dma_channel; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 88 | int dma_mapped; |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 89 | int dma_requested; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 90 | dma_addr_t rx_dma; |
| 91 | dma_addr_t tx_dma; |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 92 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 93 | size_t rx_map_len; |
| 94 | size_t tx_map_len; |
| 95 | u8 n_bytes; |
Bryan Wu | fad91c8 | 2007-12-04 23:45:14 -0800 | [diff] [blame] | 96 | int cs_change; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 97 | void (*write) (struct driver_data *); |
| 98 | void (*read) (struct driver_data *); |
| 99 | void (*duplex) (struct driver_data *); |
| 100 | }; |
| 101 | |
| 102 | struct chip_data { |
| 103 | u16 ctl_reg; |
| 104 | u16 baud; |
| 105 | u16 flag; |
| 106 | |
| 107 | u8 chip_select_num; |
| 108 | u8 n_bytes; |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 109 | u8 width; /* 0 or 1 */ |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 110 | u8 enable_dma; |
| 111 | u8 bits_per_word; /* 8 or 16 */ |
| 112 | u8 cs_change_per_word; |
Bryan Wu | 62310e5 | 2007-12-04 23:45:20 -0800 | [diff] [blame] | 113 | u16 cs_chg_udelay; /* Some devices require > 255usec delay */ |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 114 | void (*write) (struct driver_data *); |
| 115 | void (*read) (struct driver_data *); |
| 116 | void (*duplex) (struct driver_data *); |
| 117 | }; |
| 118 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 119 | #define DEFINE_SPI_REG(reg, off) \ |
| 120 | static inline u16 read_##reg(struct driver_data *drv_data) \ |
| 121 | { return bfin_read16(drv_data->regs_base + off); } \ |
| 122 | static inline void write_##reg(struct driver_data *drv_data, u16 v) \ |
| 123 | { bfin_write16(drv_data->regs_base + off, v); } |
| 124 | |
| 125 | DEFINE_SPI_REG(CTRL, 0x00) |
| 126 | DEFINE_SPI_REG(FLAG, 0x04) |
| 127 | DEFINE_SPI_REG(STAT, 0x08) |
| 128 | DEFINE_SPI_REG(TDBR, 0x0C) |
| 129 | DEFINE_SPI_REG(RDBR, 0x10) |
| 130 | DEFINE_SPI_REG(BAUD, 0x14) |
| 131 | DEFINE_SPI_REG(SHAW, 0x18) |
| 132 | |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 133 | static void bfin_spi_enable(struct driver_data *drv_data) |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 134 | { |
| 135 | u16 cr; |
| 136 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 137 | cr = read_CTRL(drv_data); |
| 138 | write_CTRL(drv_data, (cr | BIT_CTL_ENABLE)); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 141 | static void bfin_spi_disable(struct driver_data *drv_data) |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 142 | { |
| 143 | u16 cr; |
| 144 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 145 | cr = read_CTRL(drv_data); |
| 146 | write_CTRL(drv_data, (cr & (~BIT_CTL_ENABLE))); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /* Caculate the SPI_BAUD register value based on input HZ */ |
| 150 | static u16 hz_to_spi_baud(u32 speed_hz) |
| 151 | { |
| 152 | u_long sclk = get_sclk(); |
| 153 | u16 spi_baud = (sclk / (2 * speed_hz)); |
| 154 | |
| 155 | if ((sclk % (2 * speed_hz)) > 0) |
| 156 | spi_baud++; |
| 157 | |
Michael Hennerich | 7513e00 | 2009-04-06 19:00:32 -0700 | [diff] [blame] | 158 | if (spi_baud < MIN_SPI_BAUD_VAL) |
| 159 | spi_baud = MIN_SPI_BAUD_VAL; |
| 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; |
Mike Frysinger | d24bd1d | 2009-04-06 19:00:38 -0700 | [diff] [blame^] | 562 | unsigned short dmastat = get_dma_curr_irqstat(drv_data->dma_channel); |
Mike Frysinger | 04b95d2 | 2009-04-06 19:00:35 -0700 | [diff] [blame] | 563 | u16 spistat = read_STAT(drv_data); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 564 | |
Mike Frysinger | d24bd1d | 2009-04-06 19:00:38 -0700 | [diff] [blame^] | 565 | dev_dbg(&drv_data->pdev->dev, |
| 566 | "in dma_irq_handler dmastat:0x%x spistat:0x%x\n", |
| 567 | dmastat, spistat); |
| 568 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 569 | clear_dma_irqstat(drv_data->dma_channel); |
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 DMA to complete */ |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 572 | while (get_dma_curr_irqstat(drv_data->dma_channel) & DMA_RUN) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 573 | cpu_relax(); |
Bryan Wu | d6fe89b | 2007-06-11 17:34:17 +0800 | [diff] [blame] | 574 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 575 | /* |
Bryan Wu | d6fe89b | 2007-06-11 17:34:17 +0800 | [diff] [blame] | 576 | * wait for the last transaction shifted out. HRM states: |
| 577 | * at this point there may still be data in the SPI DMA FIFO waiting |
| 578 | * to be transmitted ... software needs to poll TXS in the SPI_STAT |
| 579 | * register until it goes low for 2 successive reads |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 580 | */ |
| 581 | if (drv_data->tx != NULL) { |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 582 | while ((read_STAT(drv_data) & TXS) || |
| 583 | (read_STAT(drv_data) & TXS)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 584 | cpu_relax(); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 585 | } |
| 586 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 587 | while (!(read_STAT(drv_data) & SPIF)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 588 | cpu_relax(); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 589 | |
Mike Frysinger | 04b95d2 | 2009-04-06 19:00:35 -0700 | [diff] [blame] | 590 | if (spistat & RBSY) { |
| 591 | msg->state = ERROR_STATE; |
| 592 | dev_err(&drv_data->pdev->dev, "dma receive: fifo/buffer overflow\n"); |
| 593 | } else { |
| 594 | msg->actual_length += drv_data->len_in_bytes; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 595 | |
Mike Frysinger | 04b95d2 | 2009-04-06 19:00:35 -0700 | [diff] [blame] | 596 | if (drv_data->cs_change) |
| 597 | cs_deactive(drv_data, chip); |
Bryan Wu | fad91c8 | 2007-12-04 23:45:14 -0800 | [diff] [blame] | 598 | |
Mike Frysinger | 04b95d2 | 2009-04-06 19:00:35 -0700 | [diff] [blame] | 599 | /* Move to next transfer */ |
| 600 | msg->state = next_transfer(drv_data); |
| 601 | } |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 602 | |
| 603 | /* Schedule transfer tasklet */ |
| 604 | tasklet_schedule(&drv_data->pump_transfers); |
| 605 | |
| 606 | /* free the irq handler before next transfer */ |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 607 | dev_dbg(&drv_data->pdev->dev, |
| 608 | "disable dma channel irq%d\n", |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 609 | drv_data->dma_channel); |
| 610 | dma_disable_irq(drv_data->dma_channel); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 611 | |
| 612 | return IRQ_HANDLED; |
| 613 | } |
| 614 | |
| 615 | static void pump_transfers(unsigned long data) |
| 616 | { |
| 617 | struct driver_data *drv_data = (struct driver_data *)data; |
| 618 | struct spi_message *message = NULL; |
| 619 | struct spi_transfer *transfer = NULL; |
| 620 | struct spi_transfer *previous = NULL; |
| 621 | struct chip_data *chip = NULL; |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 622 | u8 width; |
| 623 | u16 cr, dma_width, dma_config; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 624 | u32 tranf_success = 1; |
Vitja Makarov | 8eeb12e | 2008-05-01 04:35:03 -0700 | [diff] [blame] | 625 | u8 full_duplex = 0; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 626 | |
| 627 | /* Get current state information */ |
| 628 | message = drv_data->cur_msg; |
| 629 | transfer = drv_data->cur_transfer; |
| 630 | chip = drv_data->cur_chip; |
Bryan Wu | 092e1fd | 2007-12-04 23:45:23 -0800 | [diff] [blame] | 631 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 632 | /* |
| 633 | * if msg is error or done, report it back using complete() callback |
| 634 | */ |
| 635 | |
| 636 | /* Handle for abort */ |
| 637 | if (message->state == ERROR_STATE) { |
Mike Frysinger | d24bd1d | 2009-04-06 19:00:38 -0700 | [diff] [blame^] | 638 | dev_dbg(&drv_data->pdev->dev, "transfer: we've hit an error\n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 639 | message->status = -EIO; |
| 640 | giveback(drv_data); |
| 641 | return; |
| 642 | } |
| 643 | |
| 644 | /* Handle end of message */ |
| 645 | if (message->state == DONE_STATE) { |
Mike Frysinger | d24bd1d | 2009-04-06 19:00:38 -0700 | [diff] [blame^] | 646 | dev_dbg(&drv_data->pdev->dev, "transfer: all done!\n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 647 | message->status = 0; |
| 648 | giveback(drv_data); |
| 649 | return; |
| 650 | } |
| 651 | |
| 652 | /* Delay if requested at end of transfer */ |
| 653 | if (message->state == RUNNING_STATE) { |
Mike Frysinger | d24bd1d | 2009-04-06 19:00:38 -0700 | [diff] [blame^] | 654 | dev_dbg(&drv_data->pdev->dev, "transfer: still running ...\n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 655 | previous = list_entry(transfer->transfer_list.prev, |
| 656 | struct spi_transfer, transfer_list); |
| 657 | if (previous->delay_usecs) |
| 658 | udelay(previous->delay_usecs); |
| 659 | } |
| 660 | |
| 661 | /* Setup the transfer state based on the type of transfer */ |
| 662 | if (flush(drv_data) == 0) { |
| 663 | dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n"); |
| 664 | message->status = -EIO; |
| 665 | giveback(drv_data); |
| 666 | return; |
| 667 | } |
| 668 | |
| 669 | if (transfer->tx_buf != NULL) { |
| 670 | drv_data->tx = (void *)transfer->tx_buf; |
| 671 | drv_data->tx_end = drv_data->tx + transfer->len; |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 672 | dev_dbg(&drv_data->pdev->dev, "tx_buf is %p, tx_end is %p\n", |
| 673 | transfer->tx_buf, drv_data->tx_end); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 674 | } else { |
| 675 | drv_data->tx = NULL; |
| 676 | } |
| 677 | |
| 678 | if (transfer->rx_buf != NULL) { |
Vitja Makarov | 8eeb12e | 2008-05-01 04:35:03 -0700 | [diff] [blame] | 679 | full_duplex = transfer->tx_buf != NULL; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 680 | drv_data->rx = transfer->rx_buf; |
| 681 | drv_data->rx_end = drv_data->rx + transfer->len; |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 682 | dev_dbg(&drv_data->pdev->dev, "rx_buf is %p, rx_end is %p\n", |
| 683 | transfer->rx_buf, drv_data->rx_end); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 684 | } else { |
| 685 | drv_data->rx = NULL; |
| 686 | } |
| 687 | |
| 688 | drv_data->rx_dma = transfer->rx_dma; |
| 689 | drv_data->tx_dma = transfer->tx_dma; |
| 690 | drv_data->len_in_bytes = transfer->len; |
Bryan Wu | fad91c8 | 2007-12-04 23:45:14 -0800 | [diff] [blame] | 691 | drv_data->cs_change = transfer->cs_change; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 692 | |
Bryan Wu | 092e1fd | 2007-12-04 23:45:23 -0800 | [diff] [blame] | 693 | /* Bits per word setup */ |
| 694 | switch (transfer->bits_per_word) { |
| 695 | case 8: |
| 696 | drv_data->n_bytes = 1; |
| 697 | width = CFG_SPI_WORDSIZE8; |
| 698 | drv_data->read = chip->cs_change_per_word ? |
| 699 | u8_cs_chg_reader : u8_reader; |
| 700 | drv_data->write = chip->cs_change_per_word ? |
| 701 | u8_cs_chg_writer : u8_writer; |
| 702 | drv_data->duplex = chip->cs_change_per_word ? |
| 703 | u8_cs_chg_duplex : u8_duplex; |
| 704 | break; |
| 705 | |
| 706 | case 16: |
| 707 | drv_data->n_bytes = 2; |
| 708 | width = CFG_SPI_WORDSIZE16; |
| 709 | drv_data->read = chip->cs_change_per_word ? |
| 710 | u16_cs_chg_reader : u16_reader; |
| 711 | drv_data->write = chip->cs_change_per_word ? |
| 712 | u16_cs_chg_writer : u16_writer; |
| 713 | drv_data->duplex = chip->cs_change_per_word ? |
| 714 | u16_cs_chg_duplex : u16_duplex; |
| 715 | break; |
| 716 | |
| 717 | default: |
| 718 | /* No change, the same as default setting */ |
| 719 | drv_data->n_bytes = chip->n_bytes; |
| 720 | width = chip->width; |
| 721 | drv_data->write = drv_data->tx ? chip->write : null_writer; |
| 722 | drv_data->read = drv_data->rx ? chip->read : null_reader; |
| 723 | drv_data->duplex = chip->duplex ? chip->duplex : null_writer; |
| 724 | break; |
| 725 | } |
| 726 | cr = (read_CTRL(drv_data) & (~BIT_CTL_TIMOD)); |
| 727 | cr |= (width << 8); |
| 728 | write_CTRL(drv_data, cr); |
| 729 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 730 | if (width == CFG_SPI_WORDSIZE16) { |
| 731 | drv_data->len = (transfer->len) >> 1; |
| 732 | } else { |
| 733 | drv_data->len = transfer->len; |
| 734 | } |
Mike Frysinger | 4fb98ef | 2008-04-08 17:41:57 -0700 | [diff] [blame] | 735 | dev_dbg(&drv_data->pdev->dev, |
| 736 | "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] | 737 | drv_data->write, chip->write, null_writer); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 738 | |
| 739 | /* speed and width has been set on per message */ |
| 740 | message->state = RUNNING_STATE; |
| 741 | dma_config = 0; |
| 742 | |
Bryan Wu | 092e1fd | 2007-12-04 23:45:23 -0800 | [diff] [blame] | 743 | /* Speed setup (surely valid because already checked) */ |
| 744 | if (transfer->speed_hz) |
| 745 | write_BAUD(drv_data, hz_to_spi_baud(transfer->speed_hz)); |
| 746 | else |
| 747 | write_BAUD(drv_data, chip->baud); |
| 748 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 749 | write_STAT(drv_data, BIT_STAT_CLR); |
| 750 | cr = (read_CTRL(drv_data) & (~BIT_CTL_TIMOD)); |
| 751 | cs_active(drv_data, chip); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 752 | |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 753 | dev_dbg(&drv_data->pdev->dev, |
| 754 | "now pumping a transfer: width is %d, len is %d\n", |
| 755 | width, transfer->len); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 756 | |
| 757 | /* |
Vitja Makarov | 8cf5858 | 2009-04-06 19:00:31 -0700 | [diff] [blame] | 758 | * Try to map dma buffer and do a dma transfer. If successful use, |
| 759 | * different way to r/w according to the enable_dma settings and if |
| 760 | * we are not doing a full duplex transfer (since the hardware does |
| 761 | * not support full duplex DMA transfers). |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 762 | */ |
Vitja Makarov | 8eeb12e | 2008-05-01 04:35:03 -0700 | [diff] [blame] | 763 | if (!full_duplex && drv_data->cur_chip->enable_dma |
| 764 | && drv_data->len > 6) { |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 765 | |
Mike Frysinger | 7aec356 | 2009-04-06 19:00:36 -0700 | [diff] [blame] | 766 | unsigned long dma_start_addr; |
| 767 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 768 | disable_dma(drv_data->dma_channel); |
| 769 | clear_dma_irqstat(drv_data->dma_channel); |
Sonic Zhang | 07612e5 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 770 | bfin_spi_disable(drv_data); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 771 | |
| 772 | /* config dma channel */ |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 773 | dev_dbg(&drv_data->pdev->dev, "doing dma transfer\n"); |
Mike Frysinger | 7aec356 | 2009-04-06 19:00:36 -0700 | [diff] [blame] | 774 | set_dma_x_count(drv_data->dma_channel, drv_data->len); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 775 | if (width == CFG_SPI_WORDSIZE16) { |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 776 | set_dma_x_modify(drv_data->dma_channel, 2); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 777 | dma_width = WDSIZE_16; |
| 778 | } else { |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 779 | set_dma_x_modify(drv_data->dma_channel, 1); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 780 | dma_width = WDSIZE_8; |
| 781 | } |
| 782 | |
Sonic Zhang | 3f479a6 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 783 | /* poll for SPI completion before start */ |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 784 | while (!(read_STAT(drv_data) & BIT_STAT_SPIF)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 785 | cpu_relax(); |
Sonic Zhang | 3f479a6 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 786 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 787 | /* dirty hack for autobuffer DMA mode */ |
| 788 | if (drv_data->tx_dma == 0xFFFF) { |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 789 | dev_dbg(&drv_data->pdev->dev, |
| 790 | "doing autobuffer DMA out.\n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 791 | |
| 792 | /* no irq in autobuffer mode */ |
| 793 | dma_config = |
| 794 | (DMAFLOW_AUTO | RESTART | dma_width | DI_EN); |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 795 | set_dma_config(drv_data->dma_channel, dma_config); |
| 796 | set_dma_start_addr(drv_data->dma_channel, |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 797 | (unsigned long)drv_data->tx); |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 798 | enable_dma(drv_data->dma_channel); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 799 | |
Sonic Zhang | 07612e5 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 800 | /* start SPI transfer */ |
| 801 | write_CTRL(drv_data, |
| 802 | (cr | CFG_SPI_DMAWRITE | BIT_CTL_ENABLE)); |
| 803 | |
| 804 | /* just return here, there can only be one transfer |
| 805 | * in this mode |
| 806 | */ |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 807 | message->status = 0; |
| 808 | giveback(drv_data); |
| 809 | return; |
| 810 | } |
| 811 | |
| 812 | /* In dma mode, rx or tx must be NULL in one transfer */ |
Mike Frysinger | 7aec356 | 2009-04-06 19:00:36 -0700 | [diff] [blame] | 813 | dma_config = (RESTART | dma_width | DI_EN); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 814 | if (drv_data->rx != NULL) { |
| 815 | /* set transfer mode, and enable SPI */ |
Mike Frysinger | d24bd1d | 2009-04-06 19:00:38 -0700 | [diff] [blame^] | 816 | dev_dbg(&drv_data->pdev->dev, "doing DMA in to %p (size %zx)\n", |
| 817 | drv_data->rx, drv_data->len_in_bytes); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 818 | |
Vitja Makarov | 8cf5858 | 2009-04-06 19:00:31 -0700 | [diff] [blame] | 819 | /* invalidate caches, if needed */ |
| 820 | if (bfin_addr_dcachable((unsigned long) drv_data->rx)) |
| 821 | invalidate_dcache_range((unsigned long) drv_data->rx, |
| 822 | (unsigned long) (drv_data->rx + |
Mike Frysinger | ace3286 | 2009-04-06 19:00:34 -0700 | [diff] [blame] | 823 | drv_data->len_in_bytes)); |
Vitja Makarov | 8cf5858 | 2009-04-06 19:00:31 -0700 | [diff] [blame] | 824 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 825 | /* clear tx reg soformer data is not shifted out */ |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 826 | write_TDBR(drv_data, 0xFFFF); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 827 | |
Mike Frysinger | 7aec356 | 2009-04-06 19:00:36 -0700 | [diff] [blame] | 828 | dma_config |= WNR; |
| 829 | dma_start_addr = (unsigned long)drv_data->rx; |
| 830 | cr |= CFG_SPI_DMAREAD; |
Sonic Zhang | 07612e5 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 831 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 832 | } else if (drv_data->tx != NULL) { |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 833 | dev_dbg(&drv_data->pdev->dev, "doing DMA out.\n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 834 | |
Vitja Makarov | 8cf5858 | 2009-04-06 19:00:31 -0700 | [diff] [blame] | 835 | /* flush caches, if needed */ |
| 836 | if (bfin_addr_dcachable((unsigned long) drv_data->tx)) |
| 837 | flush_dcache_range((unsigned long) drv_data->tx, |
| 838 | (unsigned long) (drv_data->tx + |
Mike Frysinger | ace3286 | 2009-04-06 19:00:34 -0700 | [diff] [blame] | 839 | drv_data->len_in_bytes)); |
Vitja Makarov | 8cf5858 | 2009-04-06 19:00:31 -0700 | [diff] [blame] | 840 | |
Mike Frysinger | 7aec356 | 2009-04-06 19:00:36 -0700 | [diff] [blame] | 841 | dma_start_addr = (unsigned long)drv_data->tx; |
| 842 | cr |= CFG_SPI_DMAWRITE; |
Sonic Zhang | 07612e5 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 843 | |
Mike Frysinger | 7aec356 | 2009-04-06 19:00:36 -0700 | [diff] [blame] | 844 | } else |
| 845 | BUG(); |
| 846 | |
| 847 | /* start dma */ |
| 848 | dma_enable_irq(drv_data->dma_channel); |
| 849 | set_dma_config(drv_data->dma_channel, dma_config); |
| 850 | set_dma_start_addr(drv_data->dma_channel, dma_start_addr); |
| 851 | enable_dma(drv_data->dma_channel); |
| 852 | |
| 853 | /* start SPI transfer */ |
| 854 | write_CTRL(drv_data, (cr | BIT_CTL_ENABLE)); |
| 855 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 856 | } else { |
| 857 | /* IO mode write then read */ |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 858 | dev_dbg(&drv_data->pdev->dev, "doing IO transfer\n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 859 | |
Vitja Makarov | 8eeb12e | 2008-05-01 04:35:03 -0700 | [diff] [blame] | 860 | if (full_duplex) { |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 861 | /* full duplex mode */ |
| 862 | BUG_ON((drv_data->tx_end - drv_data->tx) != |
| 863 | (drv_data->rx_end - drv_data->rx)); |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 864 | dev_dbg(&drv_data->pdev->dev, |
| 865 | "IO duplex: cr is 0x%x\n", cr); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 866 | |
Sonic Zhang | cc487e7 | 2007-12-04 23:45:17 -0800 | [diff] [blame] | 867 | /* set SPI transfer mode */ |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 868 | write_CTRL(drv_data, (cr | CFG_SPI_WRITE)); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 869 | |
| 870 | drv_data->duplex(drv_data); |
| 871 | |
| 872 | if (drv_data->tx != drv_data->tx_end) |
| 873 | tranf_success = 0; |
| 874 | } else if (drv_data->tx != NULL) { |
| 875 | /* write only half duplex */ |
Bryan Wu | 131b17d | 2007-12-04 23:45:12 -0800 | [diff] [blame] | 876 | dev_dbg(&drv_data->pdev->dev, |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 877 | "IO write: cr is 0x%x\n", cr); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 878 | |
Sonic Zhang | cc487e7 | 2007-12-04 23:45:17 -0800 | [diff] [blame] | 879 | /* set SPI transfer mode */ |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 880 | write_CTRL(drv_data, (cr | CFG_SPI_WRITE)); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 881 | |
| 882 | drv_data->write(drv_data); |
| 883 | |
| 884 | if (drv_data->tx != drv_data->tx_end) |
| 885 | tranf_success = 0; |
| 886 | } else if (drv_data->rx != NULL) { |
| 887 | /* read only half duplex */ |
Bryan Wu | 131b17d | 2007-12-04 23:45:12 -0800 | [diff] [blame] | 888 | dev_dbg(&drv_data->pdev->dev, |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 889 | "IO read: cr is 0x%x\n", cr); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 890 | |
Sonic Zhang | cc487e7 | 2007-12-04 23:45:17 -0800 | [diff] [blame] | 891 | /* set SPI transfer mode */ |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 892 | write_CTRL(drv_data, (cr | CFG_SPI_READ)); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 893 | |
| 894 | drv_data->read(drv_data); |
| 895 | if (drv_data->rx != drv_data->rx_end) |
| 896 | tranf_success = 0; |
| 897 | } |
| 898 | |
| 899 | if (!tranf_success) { |
Bryan Wu | 131b17d | 2007-12-04 23:45:12 -0800 | [diff] [blame] | 900 | dev_dbg(&drv_data->pdev->dev, |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 901 | "IO write error!\n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 902 | message->state = ERROR_STATE; |
| 903 | } else { |
| 904 | /* Update total byte transfered */ |
Mike Frysinger | ace3286 | 2009-04-06 19:00:34 -0700 | [diff] [blame] | 905 | message->actual_length += drv_data->len_in_bytes; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 906 | |
| 907 | /* Move to next transfer of this msg */ |
| 908 | message->state = next_transfer(drv_data); |
| 909 | } |
| 910 | |
| 911 | /* Schedule next transfer tasklet */ |
| 912 | tasklet_schedule(&drv_data->pump_transfers); |
| 913 | |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | /* pop a msg from queue and kick off real transfer */ |
| 918 | static void pump_messages(struct work_struct *work) |
| 919 | { |
Bryan Wu | 131b17d | 2007-12-04 23:45:12 -0800 | [diff] [blame] | 920 | struct driver_data *drv_data; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 921 | unsigned long flags; |
| 922 | |
Bryan Wu | 131b17d | 2007-12-04 23:45:12 -0800 | [diff] [blame] | 923 | drv_data = container_of(work, struct driver_data, pump_messages); |
| 924 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 925 | /* Lock queue and check for queue work */ |
| 926 | spin_lock_irqsave(&drv_data->lock, flags); |
| 927 | if (list_empty(&drv_data->queue) || drv_data->run == QUEUE_STOPPED) { |
| 928 | /* pumper kicked off but no work to do */ |
| 929 | drv_data->busy = 0; |
| 930 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 931 | return; |
| 932 | } |
| 933 | |
| 934 | /* Make sure we are not already running a message */ |
| 935 | if (drv_data->cur_msg) { |
| 936 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 937 | return; |
| 938 | } |
| 939 | |
| 940 | /* Extract head of queue */ |
| 941 | drv_data->cur_msg = list_entry(drv_data->queue.next, |
| 942 | struct spi_message, queue); |
Bryan Wu | 5fec5b5 | 2007-12-04 23:45:13 -0800 | [diff] [blame] | 943 | |
| 944 | /* Setup the SSP using the per chip configuration */ |
| 945 | drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi); |
Bryan Wu | 8d20d0a | 2008-02-06 01:38:17 -0800 | [diff] [blame] | 946 | restore_state(drv_data); |
Bryan Wu | 5fec5b5 | 2007-12-04 23:45:13 -0800 | [diff] [blame] | 947 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 948 | list_del_init(&drv_data->cur_msg->queue); |
| 949 | |
| 950 | /* Initial message state */ |
| 951 | drv_data->cur_msg->state = START_STATE; |
| 952 | drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next, |
| 953 | struct spi_transfer, transfer_list); |
| 954 | |
Bryan Wu | 5fec5b5 | 2007-12-04 23:45:13 -0800 | [diff] [blame] | 955 | dev_dbg(&drv_data->pdev->dev, "got a message to pump, " |
| 956 | "state is set to: baud %d, flag 0x%x, ctl 0x%x\n", |
| 957 | drv_data->cur_chip->baud, drv_data->cur_chip->flag, |
| 958 | drv_data->cur_chip->ctl_reg); |
Bryan Wu | 131b17d | 2007-12-04 23:45:12 -0800 | [diff] [blame] | 959 | |
| 960 | dev_dbg(&drv_data->pdev->dev, |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 961 | "the first transfer len is %d\n", |
| 962 | drv_data->cur_transfer->len); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 963 | |
| 964 | /* Mark as busy and launch transfers */ |
| 965 | tasklet_schedule(&drv_data->pump_transfers); |
| 966 | |
| 967 | drv_data->busy = 1; |
| 968 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 969 | } |
| 970 | |
| 971 | /* |
| 972 | * got a msg to transfer, queue it in drv_data->queue. |
| 973 | * And kick off message pumper |
| 974 | */ |
| 975 | static int transfer(struct spi_device *spi, struct spi_message *msg) |
| 976 | { |
| 977 | struct driver_data *drv_data = spi_master_get_devdata(spi->master); |
| 978 | unsigned long flags; |
| 979 | |
| 980 | spin_lock_irqsave(&drv_data->lock, flags); |
| 981 | |
| 982 | if (drv_data->run == QUEUE_STOPPED) { |
| 983 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 984 | return -ESHUTDOWN; |
| 985 | } |
| 986 | |
| 987 | msg->actual_length = 0; |
| 988 | msg->status = -EINPROGRESS; |
| 989 | msg->state = START_STATE; |
| 990 | |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 991 | dev_dbg(&spi->dev, "adding an msg in transfer() \n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 992 | list_add_tail(&msg->queue, &drv_data->queue); |
| 993 | |
| 994 | if (drv_data->run == QUEUE_RUNNING && !drv_data->busy) |
| 995 | queue_work(drv_data->workqueue, &drv_data->pump_messages); |
| 996 | |
| 997 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 998 | |
| 999 | return 0; |
| 1000 | } |
| 1001 | |
Sonic Zhang | 12e17c4 | 2007-12-04 23:45:16 -0800 | [diff] [blame] | 1002 | #define MAX_SPI_SSEL 7 |
| 1003 | |
| 1004 | static u16 ssel[3][MAX_SPI_SSEL] = { |
| 1005 | {P_SPI0_SSEL1, P_SPI0_SSEL2, P_SPI0_SSEL3, |
| 1006 | P_SPI0_SSEL4, P_SPI0_SSEL5, |
| 1007 | P_SPI0_SSEL6, P_SPI0_SSEL7}, |
| 1008 | |
| 1009 | {P_SPI1_SSEL1, P_SPI1_SSEL2, P_SPI1_SSEL3, |
| 1010 | P_SPI1_SSEL4, P_SPI1_SSEL5, |
| 1011 | P_SPI1_SSEL6, P_SPI1_SSEL7}, |
| 1012 | |
| 1013 | {P_SPI2_SSEL1, P_SPI2_SSEL2, P_SPI2_SSEL3, |
| 1014 | P_SPI2_SSEL4, P_SPI2_SSEL5, |
| 1015 | P_SPI2_SSEL6, P_SPI2_SSEL7}, |
| 1016 | }; |
| 1017 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1018 | /* first setup for new devices */ |
| 1019 | static int setup(struct spi_device *spi) |
| 1020 | { |
| 1021 | struct bfin5xx_spi_chip *chip_info = NULL; |
| 1022 | struct chip_data *chip; |
| 1023 | struct driver_data *drv_data = spi_master_get_devdata(spi->master); |
| 1024 | u8 spi_flg; |
| 1025 | |
| 1026 | /* Abort device setup if requested features are not supported */ |
| 1027 | if (spi->mode & ~(SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST)) { |
| 1028 | dev_err(&spi->dev, "requested mode not fully supported\n"); |
| 1029 | return -EINVAL; |
| 1030 | } |
| 1031 | |
| 1032 | /* Zero (the default) here means 8 bits */ |
| 1033 | if (!spi->bits_per_word) |
| 1034 | spi->bits_per_word = 8; |
| 1035 | |
| 1036 | if (spi->bits_per_word != 8 && spi->bits_per_word != 16) |
| 1037 | return -EINVAL; |
| 1038 | |
| 1039 | /* Only alloc (or use chip_info) on first setup */ |
| 1040 | chip = spi_get_ctldata(spi); |
| 1041 | if (chip == NULL) { |
| 1042 | chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL); |
| 1043 | if (!chip) |
| 1044 | return -ENOMEM; |
| 1045 | |
| 1046 | chip->enable_dma = 0; |
| 1047 | chip_info = spi->controller_data; |
| 1048 | } |
| 1049 | |
| 1050 | /* chip_info isn't always needed */ |
| 1051 | if (chip_info) { |
Mike Frysinger | 2ed3551 | 2007-12-04 23:45:14 -0800 | [diff] [blame] | 1052 | /* Make sure people stop trying to set fields via ctl_reg |
| 1053 | * when they should actually be using common SPI framework. |
| 1054 | * Currently we let through: WOM EMISO PSSE GM SZ TIMOD. |
| 1055 | * Not sure if a user actually needs/uses any of these, |
| 1056 | * but let's assume (for now) they do. |
| 1057 | */ |
| 1058 | if (chip_info->ctl_reg & (SPE|MSTR|CPOL|CPHA|LSBF|SIZE)) { |
| 1059 | dev_err(&spi->dev, "do not set bits in ctl_reg " |
| 1060 | "that the SPI framework manages\n"); |
| 1061 | return -EINVAL; |
| 1062 | } |
| 1063 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1064 | chip->enable_dma = chip_info->enable_dma != 0 |
| 1065 | && drv_data->master_info->enable_dma; |
| 1066 | chip->ctl_reg = chip_info->ctl_reg; |
| 1067 | chip->bits_per_word = chip_info->bits_per_word; |
| 1068 | chip->cs_change_per_word = chip_info->cs_change_per_word; |
| 1069 | chip->cs_chg_udelay = chip_info->cs_chg_udelay; |
| 1070 | } |
| 1071 | |
| 1072 | /* translate common spi framework into our register */ |
| 1073 | if (spi->mode & SPI_CPOL) |
| 1074 | chip->ctl_reg |= CPOL; |
| 1075 | if (spi->mode & SPI_CPHA) |
| 1076 | chip->ctl_reg |= CPHA; |
| 1077 | if (spi->mode & SPI_LSB_FIRST) |
| 1078 | chip->ctl_reg |= LSBF; |
| 1079 | /* we dont support running in slave mode (yet?) */ |
| 1080 | chip->ctl_reg |= MSTR; |
| 1081 | |
| 1082 | /* |
| 1083 | * if any one SPI chip is registered and wants DMA, request the |
| 1084 | * DMA channel for it |
| 1085 | */ |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 1086 | if (chip->enable_dma && !drv_data->dma_requested) { |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1087 | /* register dma irq handler */ |
Mike Frysinger | 59bfcc6 | 2009-04-06 19:00:37 -0700 | [diff] [blame] | 1088 | if (request_dma(drv_data->dma_channel, "BFIN_SPI_DMA") < 0) { |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 1089 | dev_dbg(&spi->dev, |
| 1090 | "Unable to request BlackFin SPI DMA channel\n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1091 | return -ENODEV; |
| 1092 | } |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 1093 | if (set_dma_callback(drv_data->dma_channel, |
Mike Frysinger | 59bfcc6 | 2009-04-06 19:00:37 -0700 | [diff] [blame] | 1094 | dma_irq_handler, drv_data) < 0) { |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 1095 | dev_dbg(&spi->dev, "Unable to set dma callback\n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1096 | return -EPERM; |
| 1097 | } |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 1098 | dma_disable_irq(drv_data->dma_channel); |
| 1099 | drv_data->dma_requested = 1; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | /* |
| 1103 | * Notice: for blackfin, the speed_hz is the value of register |
| 1104 | * SPI_BAUD, not the real baudrate |
| 1105 | */ |
| 1106 | chip->baud = hz_to_spi_baud(spi->max_speed_hz); |
| 1107 | spi_flg = ~(1 << (spi->chip_select)); |
| 1108 | chip->flag = ((u16) spi_flg << 8) | (1 << (spi->chip_select)); |
| 1109 | chip->chip_select_num = spi->chip_select; |
| 1110 | |
| 1111 | switch (chip->bits_per_word) { |
| 1112 | case 8: |
| 1113 | chip->n_bytes = 1; |
| 1114 | chip->width = CFG_SPI_WORDSIZE8; |
| 1115 | chip->read = chip->cs_change_per_word ? |
| 1116 | u8_cs_chg_reader : u8_reader; |
| 1117 | chip->write = chip->cs_change_per_word ? |
| 1118 | u8_cs_chg_writer : u8_writer; |
| 1119 | chip->duplex = chip->cs_change_per_word ? |
| 1120 | u8_cs_chg_duplex : u8_duplex; |
| 1121 | break; |
| 1122 | |
| 1123 | case 16: |
| 1124 | chip->n_bytes = 2; |
| 1125 | chip->width = CFG_SPI_WORDSIZE16; |
| 1126 | chip->read = chip->cs_change_per_word ? |
| 1127 | u16_cs_chg_reader : u16_reader; |
| 1128 | chip->write = chip->cs_change_per_word ? |
| 1129 | u16_cs_chg_writer : u16_writer; |
| 1130 | chip->duplex = chip->cs_change_per_word ? |
| 1131 | u16_cs_chg_duplex : u16_duplex; |
| 1132 | break; |
| 1133 | |
| 1134 | default: |
| 1135 | dev_err(&spi->dev, "%d bits_per_word is not supported\n", |
| 1136 | chip->bits_per_word); |
| 1137 | kfree(chip); |
| 1138 | return -ENODEV; |
| 1139 | } |
| 1140 | |
Joe Perches | 898eb71 | 2007-10-18 03:06:30 -0700 | [diff] [blame] | 1141 | 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] | 1142 | spi->modalias, chip->width, chip->enable_dma); |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 1143 | 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] | 1144 | chip->ctl_reg, chip->flag); |
| 1145 | |
| 1146 | spi_set_ctldata(spi, chip); |
| 1147 | |
Sonic Zhang | 12e17c4 | 2007-12-04 23:45:16 -0800 | [diff] [blame] | 1148 | dev_dbg(&spi->dev, "chip select number is %d\n", chip->chip_select_num); |
| 1149 | if ((chip->chip_select_num > 0) |
| 1150 | && (chip->chip_select_num <= spi->master->num_chipselect)) |
| 1151 | peripheral_request(ssel[spi->master->bus_num] |
Bryan Wu | aab0d83 | 2008-02-06 01:38:17 -0800 | [diff] [blame] | 1152 | [chip->chip_select_num-1], spi->modalias); |
Sonic Zhang | 12e17c4 | 2007-12-04 23:45:16 -0800 | [diff] [blame] | 1153 | |
Sonic Zhang | 07612e5 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 1154 | cs_deactive(drv_data, chip); |
| 1155 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1156 | return 0; |
| 1157 | } |
| 1158 | |
| 1159 | /* |
| 1160 | * callback for spi framework. |
| 1161 | * clean driver specific data |
| 1162 | */ |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 1163 | static void cleanup(struct spi_device *spi) |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1164 | { |
Mike Frysinger | 27bb9e7 | 2007-06-11 15:31:30 +0800 | [diff] [blame] | 1165 | struct chip_data *chip = spi_get_ctldata(spi); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1166 | |
Sonic Zhang | 12e17c4 | 2007-12-04 23:45:16 -0800 | [diff] [blame] | 1167 | if ((chip->chip_select_num > 0) |
| 1168 | && (chip->chip_select_num <= spi->master->num_chipselect)) |
| 1169 | peripheral_free(ssel[spi->master->bus_num] |
| 1170 | [chip->chip_select_num-1]); |
| 1171 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1172 | kfree(chip); |
| 1173 | } |
| 1174 | |
| 1175 | static inline int init_queue(struct driver_data *drv_data) |
| 1176 | { |
| 1177 | INIT_LIST_HEAD(&drv_data->queue); |
| 1178 | spin_lock_init(&drv_data->lock); |
| 1179 | |
| 1180 | drv_data->run = QUEUE_STOPPED; |
| 1181 | drv_data->busy = 0; |
| 1182 | |
| 1183 | /* init transfer tasklet */ |
| 1184 | tasklet_init(&drv_data->pump_transfers, |
| 1185 | pump_transfers, (unsigned long)drv_data); |
| 1186 | |
| 1187 | /* init messages workqueue */ |
| 1188 | INIT_WORK(&drv_data->pump_messages, pump_messages); |
Kay Sievers | 6c7377a | 2009-03-24 16:38:21 -0700 | [diff] [blame] | 1189 | drv_data->workqueue = create_singlethread_workqueue( |
| 1190 | dev_name(drv_data->master->dev.parent)); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1191 | if (drv_data->workqueue == NULL) |
| 1192 | return -EBUSY; |
| 1193 | |
| 1194 | return 0; |
| 1195 | } |
| 1196 | |
| 1197 | static inline int start_queue(struct driver_data *drv_data) |
| 1198 | { |
| 1199 | unsigned long flags; |
| 1200 | |
| 1201 | spin_lock_irqsave(&drv_data->lock, flags); |
| 1202 | |
| 1203 | if (drv_data->run == QUEUE_RUNNING || drv_data->busy) { |
| 1204 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 1205 | return -EBUSY; |
| 1206 | } |
| 1207 | |
| 1208 | drv_data->run = QUEUE_RUNNING; |
| 1209 | drv_data->cur_msg = NULL; |
| 1210 | drv_data->cur_transfer = NULL; |
| 1211 | drv_data->cur_chip = NULL; |
| 1212 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 1213 | |
| 1214 | queue_work(drv_data->workqueue, &drv_data->pump_messages); |
| 1215 | |
| 1216 | return 0; |
| 1217 | } |
| 1218 | |
| 1219 | static inline int stop_queue(struct driver_data *drv_data) |
| 1220 | { |
| 1221 | unsigned long flags; |
| 1222 | unsigned limit = 500; |
| 1223 | int status = 0; |
| 1224 | |
| 1225 | spin_lock_irqsave(&drv_data->lock, flags); |
| 1226 | |
| 1227 | /* |
| 1228 | * This is a bit lame, but is optimized for the common execution path. |
| 1229 | * A wait_queue on the drv_data->busy could be used, but then the common |
| 1230 | * execution path (pump_messages) would be required to call wake_up or |
| 1231 | * friends on every SPI message. Do this instead |
| 1232 | */ |
| 1233 | drv_data->run = QUEUE_STOPPED; |
| 1234 | while (!list_empty(&drv_data->queue) && drv_data->busy && limit--) { |
| 1235 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 1236 | msleep(10); |
| 1237 | spin_lock_irqsave(&drv_data->lock, flags); |
| 1238 | } |
| 1239 | |
| 1240 | if (!list_empty(&drv_data->queue) || drv_data->busy) |
| 1241 | status = -EBUSY; |
| 1242 | |
| 1243 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 1244 | |
| 1245 | return status; |
| 1246 | } |
| 1247 | |
| 1248 | static inline int destroy_queue(struct driver_data *drv_data) |
| 1249 | { |
| 1250 | int status; |
| 1251 | |
| 1252 | status = stop_queue(drv_data); |
| 1253 | if (status != 0) |
| 1254 | return status; |
| 1255 | |
| 1256 | destroy_workqueue(drv_data->workqueue); |
| 1257 | |
| 1258 | return 0; |
| 1259 | } |
| 1260 | |
| 1261 | static int __init bfin5xx_spi_probe(struct platform_device *pdev) |
| 1262 | { |
| 1263 | struct device *dev = &pdev->dev; |
| 1264 | struct bfin5xx_spi_master *platform_info; |
| 1265 | struct spi_master *master; |
| 1266 | struct driver_data *drv_data = 0; |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1267 | struct resource *res; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1268 | int status = 0; |
| 1269 | |
| 1270 | platform_info = dev->platform_data; |
| 1271 | |
| 1272 | /* Allocate master with space for drv_data */ |
| 1273 | master = spi_alloc_master(dev, sizeof(struct driver_data) + 16); |
| 1274 | if (!master) { |
| 1275 | dev_err(&pdev->dev, "can not alloc spi_master\n"); |
| 1276 | return -ENOMEM; |
| 1277 | } |
Bryan Wu | 131b17d | 2007-12-04 23:45:12 -0800 | [diff] [blame] | 1278 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1279 | drv_data = spi_master_get_devdata(master); |
| 1280 | drv_data->master = master; |
| 1281 | drv_data->master_info = platform_info; |
| 1282 | drv_data->pdev = pdev; |
Bryan Wu | 003d922 | 2007-12-04 23:45:22 -0800 | [diff] [blame] | 1283 | drv_data->pin_req = platform_info->pin_req; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1284 | |
| 1285 | master->bus_num = pdev->id; |
| 1286 | master->num_chipselect = platform_info->num_chipselect; |
| 1287 | master->cleanup = cleanup; |
| 1288 | master->setup = setup; |
| 1289 | master->transfer = transfer; |
| 1290 | |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1291 | /* Find and map our resources */ |
| 1292 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1293 | if (res == NULL) { |
| 1294 | dev_err(dev, "Cannot get IORESOURCE_MEM\n"); |
| 1295 | status = -ENOENT; |
| 1296 | goto out_error_get_res; |
| 1297 | } |
| 1298 | |
Bryan Wu | f452126 | 2007-12-04 23:45:22 -0800 | [diff] [blame] | 1299 | drv_data->regs_base = ioremap(res->start, (res->end - res->start + 1)); |
| 1300 | if (drv_data->regs_base == NULL) { |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1301 | dev_err(dev, "Cannot map IO\n"); |
| 1302 | status = -ENXIO; |
| 1303 | goto out_error_ioremap; |
| 1304 | } |
| 1305 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 1306 | drv_data->dma_channel = platform_get_irq(pdev, 0); |
| 1307 | if (drv_data->dma_channel < 0) { |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1308 | dev_err(dev, "No DMA channel specified\n"); |
| 1309 | status = -ENOENT; |
| 1310 | goto out_error_no_dma_ch; |
| 1311 | } |
| 1312 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1313 | /* Initial and start queue */ |
| 1314 | status = init_queue(drv_data); |
| 1315 | if (status != 0) { |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1316 | dev_err(dev, "problem initializing queue\n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1317 | goto out_error_queue_alloc; |
| 1318 | } |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1319 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1320 | status = start_queue(drv_data); |
| 1321 | if (status != 0) { |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1322 | dev_err(dev, "problem starting queue\n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1323 | goto out_error_queue_alloc; |
| 1324 | } |
| 1325 | |
Vitja Makarov | f9e522c | 2008-04-08 17:41:57 -0700 | [diff] [blame] | 1326 | status = peripheral_request_list(drv_data->pin_req, DRV_NAME); |
| 1327 | if (status != 0) { |
| 1328 | dev_err(&pdev->dev, ": Requesting Peripherals failed\n"); |
| 1329 | goto out_error_queue_alloc; |
| 1330 | } |
| 1331 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1332 | /* Register with the SPI framework */ |
| 1333 | platform_set_drvdata(pdev, drv_data); |
| 1334 | status = spi_register_master(master); |
| 1335 | if (status != 0) { |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1336 | dev_err(dev, "problem registering spi master\n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1337 | goto out_error_queue_alloc; |
| 1338 | } |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1339 | |
Bryan Wu | f452126 | 2007-12-04 23:45:22 -0800 | [diff] [blame] | 1340 | 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] | 1341 | DRV_DESC, DRV_VERSION, drv_data->regs_base, |
| 1342 | drv_data->dma_channel); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1343 | return status; |
| 1344 | |
Michael Hennerich | cc2f81a | 2007-12-04 23:45:13 -0800 | [diff] [blame] | 1345 | out_error_queue_alloc: |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1346 | destroy_queue(drv_data); |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1347 | out_error_no_dma_ch: |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 1348 | iounmap((void *) drv_data->regs_base); |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1349 | out_error_ioremap: |
| 1350 | out_error_get_res: |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1351 | spi_master_put(master); |
Michael Hennerich | cc2f81a | 2007-12-04 23:45:13 -0800 | [diff] [blame] | 1352 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1353 | return status; |
| 1354 | } |
| 1355 | |
| 1356 | /* stop hardware and remove the driver */ |
| 1357 | static int __devexit bfin5xx_spi_remove(struct platform_device *pdev) |
| 1358 | { |
| 1359 | struct driver_data *drv_data = platform_get_drvdata(pdev); |
| 1360 | int status = 0; |
| 1361 | |
| 1362 | if (!drv_data) |
| 1363 | return 0; |
| 1364 | |
| 1365 | /* Remove the queue */ |
| 1366 | status = destroy_queue(drv_data); |
| 1367 | if (status != 0) |
| 1368 | return status; |
| 1369 | |
| 1370 | /* Disable the SSP at the peripheral and SOC level */ |
| 1371 | bfin_spi_disable(drv_data); |
| 1372 | |
| 1373 | /* Release DMA */ |
| 1374 | if (drv_data->master_info->enable_dma) { |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 1375 | if (dma_channel_active(drv_data->dma_channel)) |
| 1376 | free_dma(drv_data->dma_channel); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1377 | } |
| 1378 | |
| 1379 | /* Disconnect from the SPI framework */ |
| 1380 | spi_unregister_master(drv_data->master); |
| 1381 | |
Bryan Wu | 003d922 | 2007-12-04 23:45:22 -0800 | [diff] [blame] | 1382 | peripheral_free_list(drv_data->pin_req); |
Michael Hennerich | cc2f81a | 2007-12-04 23:45:13 -0800 | [diff] [blame] | 1383 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1384 | /* Prevent double remove */ |
| 1385 | platform_set_drvdata(pdev, NULL); |
| 1386 | |
| 1387 | return 0; |
| 1388 | } |
| 1389 | |
| 1390 | #ifdef CONFIG_PM |
| 1391 | static int bfin5xx_spi_suspend(struct platform_device *pdev, pm_message_t state) |
| 1392 | { |
| 1393 | struct driver_data *drv_data = platform_get_drvdata(pdev); |
| 1394 | int status = 0; |
| 1395 | |
| 1396 | status = stop_queue(drv_data); |
| 1397 | if (status != 0) |
| 1398 | return status; |
| 1399 | |
| 1400 | /* stop hardware */ |
| 1401 | bfin_spi_disable(drv_data); |
| 1402 | |
| 1403 | return 0; |
| 1404 | } |
| 1405 | |
| 1406 | static int bfin5xx_spi_resume(struct platform_device *pdev) |
| 1407 | { |
| 1408 | struct driver_data *drv_data = platform_get_drvdata(pdev); |
| 1409 | int status = 0; |
| 1410 | |
| 1411 | /* Enable the SPI interface */ |
| 1412 | bfin_spi_enable(drv_data); |
| 1413 | |
| 1414 | /* Start the queue running */ |
| 1415 | status = start_queue(drv_data); |
| 1416 | if (status != 0) { |
| 1417 | dev_err(&pdev->dev, "problem starting queue (%d)\n", status); |
| 1418 | return status; |
| 1419 | } |
| 1420 | |
| 1421 | return 0; |
| 1422 | } |
| 1423 | #else |
| 1424 | #define bfin5xx_spi_suspend NULL |
| 1425 | #define bfin5xx_spi_resume NULL |
| 1426 | #endif /* CONFIG_PM */ |
| 1427 | |
Kay Sievers | 7e38c3c | 2008-04-10 21:29:20 -0700 | [diff] [blame] | 1428 | MODULE_ALIAS("platform:bfin-spi"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1429 | static struct platform_driver bfin5xx_spi_driver = { |
David Brownell | fc3ba95 | 2007-08-30 23:56:24 -0700 | [diff] [blame] | 1430 | .driver = { |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1431 | .name = DRV_NAME, |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 1432 | .owner = THIS_MODULE, |
| 1433 | }, |
| 1434 | .suspend = bfin5xx_spi_suspend, |
| 1435 | .resume = bfin5xx_spi_resume, |
| 1436 | .remove = __devexit_p(bfin5xx_spi_remove), |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1437 | }; |
| 1438 | |
| 1439 | static int __init bfin5xx_spi_init(void) |
| 1440 | { |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 1441 | return platform_driver_probe(&bfin5xx_spi_driver, bfin5xx_spi_probe); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1442 | } |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1443 | module_init(bfin5xx_spi_init); |
| 1444 | |
| 1445 | static void __exit bfin5xx_spi_exit(void) |
| 1446 | { |
| 1447 | platform_driver_unregister(&bfin5xx_spi_driver); |
| 1448 | } |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1449 | module_exit(bfin5xx_spi_exit); |