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