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