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