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