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