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