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 | |
| 297 | while (drv_data->tx < drv_data->tx_end) { |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 298 | cs_active(drv_data, chip); |
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 | write_TDBR(drv_data, (*(u8 *) (drv_data->tx))); |
| 301 | while (read_STAT(drv_data) & BIT_STAT_TXS) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 302 | cpu_relax(); |
Bryan Wu | e26aa01 | 2008-02-06 01:38:18 -0800 | [diff] [blame] | 303 | while (!(read_STAT(drv_data) & BIT_STAT_SPIF)) |
| 304 | cpu_relax(); |
Bryan Wu | 62310e5 | 2007-12-04 23:45:20 -0800 | [diff] [blame] | 305 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 306 | cs_deactive(drv_data, chip); |
Bryan Wu | 5fec5b5 | 2007-12-04 23:45:13 -0800 | [diff] [blame] | 307 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 308 | ++drv_data->tx; |
| 309 | } |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | static void u8_reader(struct driver_data *drv_data) |
| 313 | { |
Bryan Wu | 131b17d | 2007-12-04 23:45:12 -0800 | [diff] [blame] | 314 | dev_dbg(&drv_data->pdev->dev, |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 315 | "cr-8 is 0x%x\n", read_STAT(drv_data)); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 316 | |
Sonic Zhang | 3f479a6 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 317 | /* poll for SPI completion before start */ |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 318 | while (!(read_STAT(drv_data) & BIT_STAT_SPIF)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 319 | cpu_relax(); |
Sonic Zhang | 3f479a6 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 320 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 321 | /* clear TDBR buffer before read(else it will be shifted out) */ |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 322 | write_TDBR(drv_data, 0xFFFF); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 323 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 324 | dummy_read(drv_data); |
Sonic Zhang | cc487e7 | 2007-12-04 23:45:17 -0800 | [diff] [blame] | 325 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 326 | while (drv_data->rx < drv_data->rx_end - 1) { |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 327 | while (!(read_STAT(drv_data) & BIT_STAT_RXS)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 328 | cpu_relax(); |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 329 | *(u8 *) (drv_data->rx) = read_RDBR(drv_data); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 330 | ++drv_data->rx; |
| 331 | } |
| 332 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 333 | while (!(read_STAT(drv_data) & BIT_STAT_RXS)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 334 | cpu_relax(); |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 335 | *(u8 *) (drv_data->rx) = read_SHAW(drv_data); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 336 | ++drv_data->rx; |
| 337 | } |
| 338 | |
| 339 | static void u8_cs_chg_reader(struct driver_data *drv_data) |
| 340 | { |
| 341 | struct chip_data *chip = drv_data->cur_chip; |
| 342 | |
Bryan Wu | e26aa01 | 2008-02-06 01:38:18 -0800 | [diff] [blame] | 343 | while (drv_data->rx < drv_data->rx_end) { |
| 344 | cs_active(drv_data, chip); |
| 345 | read_RDBR(drv_data); /* kick off */ |
Bryan Wu | 5fec5b5 | 2007-12-04 23:45:13 -0800 | [diff] [blame] | 346 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 347 | while (!(read_STAT(drv_data) & BIT_STAT_RXS)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 348 | cpu_relax(); |
Bryan Wu | e26aa01 | 2008-02-06 01:38:18 -0800 | [diff] [blame] | 349 | while (!(read_STAT(drv_data) & BIT_STAT_SPIF)) |
| 350 | cpu_relax(); |
| 351 | |
| 352 | *(u8 *) (drv_data->rx) = read_SHAW(drv_data); |
| 353 | cs_deactive(drv_data, chip); |
| 354 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 355 | ++drv_data->rx; |
| 356 | } |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | static void u8_duplex(struct driver_data *drv_data) |
| 360 | { |
| 361 | /* in duplex mode, clk is triggered by writing of TDBR */ |
| 362 | while (drv_data->rx < drv_data->rx_end) { |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 363 | write_TDBR(drv_data, (*(u8 *) (drv_data->tx))); |
Bryan Wu | 4fd432d | 2008-02-06 01:38:19 -0800 | [diff] [blame^] | 364 | while (!(read_STAT(drv_data) & BIT_STAT_SPIF)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 365 | cpu_relax(); |
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_RDBR(drv_data); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 369 | ++drv_data->rx; |
| 370 | ++drv_data->tx; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | static void u8_cs_chg_duplex(struct driver_data *drv_data) |
| 375 | { |
| 376 | struct chip_data *chip = drv_data->cur_chip; |
| 377 | |
| 378 | while (drv_data->rx < drv_data->rx_end) { |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 379 | cs_active(drv_data, chip); |
Bryan Wu | 5fec5b5 | 2007-12-04 23:45:13 -0800 | [diff] [blame] | 380 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 381 | write_TDBR(drv_data, (*(u8 *) (drv_data->tx))); |
Bryan Wu | e26aa01 | 2008-02-06 01:38:18 -0800 | [diff] [blame] | 382 | |
| 383 | while (!(read_STAT(drv_data) & BIT_STAT_SPIF)) |
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 | while (!(read_STAT(drv_data) & BIT_STAT_RXS)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 386 | cpu_relax(); |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 387 | *(u8 *) (drv_data->rx) = read_RDBR(drv_data); |
Bryan Wu | 62310e5 | 2007-12-04 23:45:20 -0800 | [diff] [blame] | 388 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 389 | cs_deactive(drv_data, chip); |
Bryan Wu | 5fec5b5 | 2007-12-04 23:45:13 -0800 | [diff] [blame] | 390 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 391 | ++drv_data->rx; |
| 392 | ++drv_data->tx; |
| 393 | } |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | static void u16_writer(struct driver_data *drv_data) |
| 397 | { |
Bryan Wu | 131b17d | 2007-12-04 23:45:12 -0800 | [diff] [blame] | 398 | dev_dbg(&drv_data->pdev->dev, |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 399 | "cr16 is 0x%x\n", read_STAT(drv_data)); |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 400 | |
Sonic Zhang | 3f479a6 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 401 | /* poll for SPI completion before start */ |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 402 | while (!(read_STAT(drv_data) & BIT_STAT_SPIF)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 403 | cpu_relax(); |
Sonic Zhang | 3f479a6 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 404 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 405 | while (drv_data->tx < drv_data->tx_end) { |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 406 | write_TDBR(drv_data, (*(u16 *) (drv_data->tx))); |
| 407 | while ((read_STAT(drv_data) & BIT_STAT_TXS)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 408 | cpu_relax(); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 409 | drv_data->tx += 2; |
| 410 | } |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | static void u16_cs_chg_writer(struct driver_data *drv_data) |
| 414 | { |
| 415 | struct chip_data *chip = drv_data->cur_chip; |
| 416 | |
Sonic Zhang | 3f479a6 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 417 | /* poll for SPI completion before start */ |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 418 | while (!(read_STAT(drv_data) & BIT_STAT_SPIF)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 419 | cpu_relax(); |
Sonic Zhang | 3f479a6 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 420 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 421 | while (drv_data->tx < drv_data->tx_end) { |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 422 | cs_active(drv_data, chip); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 423 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 424 | write_TDBR(drv_data, (*(u16 *) (drv_data->tx))); |
| 425 | while ((read_STAT(drv_data) & BIT_STAT_TXS)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 426 | cpu_relax(); |
Bryan Wu | 62310e5 | 2007-12-04 23:45:20 -0800 | [diff] [blame] | 427 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 428 | cs_deactive(drv_data, chip); |
Bryan Wu | 5fec5b5 | 2007-12-04 23:45:13 -0800 | [diff] [blame] | 429 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 430 | drv_data->tx += 2; |
| 431 | } |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | static void u16_reader(struct driver_data *drv_data) |
| 435 | { |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 436 | dev_dbg(&drv_data->pdev->dev, |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 437 | "cr-16 is 0x%x\n", read_STAT(drv_data)); |
Sonic Zhang | cc487e7 | 2007-12-04 23:45:17 -0800 | [diff] [blame] | 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); |
Sonic Zhang | cc487e7 | 2007-12-04 23:45:17 -0800 | [diff] [blame] | 445 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 446 | dummy_read(drv_data); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 447 | |
| 448 | while (drv_data->rx < (drv_data->rx_end - 2)) { |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 449 | while (!(read_STAT(drv_data) & BIT_STAT_RXS)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 450 | cpu_relax(); |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 451 | *(u16 *) (drv_data->rx) = read_RDBR(drv_data); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 452 | drv_data->rx += 2; |
| 453 | } |
| 454 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 455 | while (!(read_STAT(drv_data) & BIT_STAT_RXS)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 456 | cpu_relax(); |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 457 | *(u16 *) (drv_data->rx) = read_SHAW(drv_data); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 458 | drv_data->rx += 2; |
| 459 | } |
| 460 | |
| 461 | static void u16_cs_chg_reader(struct driver_data *drv_data) |
| 462 | { |
| 463 | struct chip_data *chip = drv_data->cur_chip; |
| 464 | |
Sonic Zhang | 3f479a6 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 465 | /* poll for SPI completion before start */ |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 466 | while (!(read_STAT(drv_data) & BIT_STAT_SPIF)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 467 | cpu_relax(); |
Sonic Zhang | 3f479a6 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 468 | |
Sonic Zhang | cc487e7 | 2007-12-04 23:45:17 -0800 | [diff] [blame] | 469 | /* clear TDBR buffer before read(else it will be shifted out) */ |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 470 | write_TDBR(drv_data, 0xFFFF); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 471 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 472 | cs_active(drv_data, chip); |
| 473 | dummy_read(drv_data); |
Sonic Zhang | cc487e7 | 2007-12-04 23:45:17 -0800 | [diff] [blame] | 474 | |
Bryan Wu | c3061ab | 2007-12-04 23:45:19 -0800 | [diff] [blame] | 475 | while (drv_data->rx < drv_data->rx_end - 2) { |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 476 | cs_deactive(drv_data, chip); |
Bryan Wu | 5fec5b5 | 2007-12-04 23:45:13 -0800 | [diff] [blame] | 477 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 478 | while (!(read_STAT(drv_data) & BIT_STAT_RXS)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 479 | cpu_relax(); |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 480 | cs_active(drv_data, chip); |
| 481 | *(u16 *) (drv_data->rx) = read_RDBR(drv_data); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 482 | drv_data->rx += 2; |
| 483 | } |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 484 | cs_deactive(drv_data, chip); |
Sonic Zhang | cc487e7 | 2007-12-04 23:45:17 -0800 | [diff] [blame] | 485 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 486 | while (!(read_STAT(drv_data) & BIT_STAT_RXS)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 487 | cpu_relax(); |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 488 | *(u16 *) (drv_data->rx) = read_SHAW(drv_data); |
Sonic Zhang | cc487e7 | 2007-12-04 23:45:17 -0800 | [diff] [blame] | 489 | drv_data->rx += 2; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | static void u16_duplex(struct driver_data *drv_data) |
| 493 | { |
| 494 | /* in duplex mode, clk is triggered by writing of TDBR */ |
| 495 | while (drv_data->tx < drv_data->tx_end) { |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 496 | write_TDBR(drv_data, (*(u16 *) (drv_data->tx))); |
Bryan Wu | 4fd432d | 2008-02-06 01:38:19 -0800 | [diff] [blame^] | 497 | while (!(read_STAT(drv_data) & BIT_STAT_SPIF)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 498 | cpu_relax(); |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 499 | while (!(read_STAT(drv_data) & BIT_STAT_RXS)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 500 | cpu_relax(); |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 501 | *(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 | drv_data->tx += 2; |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | static void u16_cs_chg_duplex(struct driver_data *drv_data) |
| 508 | { |
| 509 | struct chip_data *chip = drv_data->cur_chip; |
| 510 | |
| 511 | while (drv_data->tx < drv_data->tx_end) { |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 512 | cs_active(drv_data, chip); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 513 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 514 | write_TDBR(drv_data, (*(u16 *) (drv_data->tx))); |
Bryan Wu | 4fd432d | 2008-02-06 01:38:19 -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(); |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 517 | while (!(read_STAT(drv_data) & BIT_STAT_RXS)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 518 | cpu_relax(); |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 519 | *(u16 *) (drv_data->rx) = read_RDBR(drv_data); |
Bryan Wu | 62310e5 | 2007-12-04 23:45:20 -0800 | [diff] [blame] | 520 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 521 | cs_deactive(drv_data, chip); |
Bryan Wu | 5fec5b5 | 2007-12-04 23:45:13 -0800 | [diff] [blame] | 522 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 523 | drv_data->rx += 2; |
| 524 | drv_data->tx += 2; |
| 525 | } |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | /* test if ther is more transfer to be done */ |
| 529 | static void *next_transfer(struct driver_data *drv_data) |
| 530 | { |
| 531 | struct spi_message *msg = drv_data->cur_msg; |
| 532 | struct spi_transfer *trans = drv_data->cur_transfer; |
| 533 | |
| 534 | /* Move to next transfer */ |
| 535 | if (trans->transfer_list.next != &msg->transfers) { |
| 536 | drv_data->cur_transfer = |
| 537 | list_entry(trans->transfer_list.next, |
| 538 | struct spi_transfer, transfer_list); |
| 539 | return RUNNING_STATE; |
| 540 | } else |
| 541 | return DONE_STATE; |
| 542 | } |
| 543 | |
| 544 | /* |
| 545 | * caller already set message->status; |
| 546 | * dma and pio irqs are blocked give finished message back |
| 547 | */ |
| 548 | static void giveback(struct driver_data *drv_data) |
| 549 | { |
Bryan Wu | fad91c8 | 2007-12-04 23:45:14 -0800 | [diff] [blame] | 550 | struct chip_data *chip = drv_data->cur_chip; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 551 | struct spi_transfer *last_transfer; |
| 552 | unsigned long flags; |
| 553 | struct spi_message *msg; |
| 554 | |
| 555 | spin_lock_irqsave(&drv_data->lock, flags); |
| 556 | msg = drv_data->cur_msg; |
| 557 | drv_data->cur_msg = NULL; |
| 558 | drv_data->cur_transfer = NULL; |
| 559 | drv_data->cur_chip = NULL; |
| 560 | queue_work(drv_data->workqueue, &drv_data->pump_messages); |
| 561 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 562 | |
| 563 | last_transfer = list_entry(msg->transfers.prev, |
| 564 | struct spi_transfer, transfer_list); |
| 565 | |
| 566 | msg->state = NULL; |
| 567 | |
| 568 | /* disable chip select signal. And not stop spi in autobuffer mode */ |
| 569 | if (drv_data->tx_dma != 0xFFFF) { |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 570 | cs_deactive(drv_data, chip); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 571 | bfin_spi_disable(drv_data); |
| 572 | } |
| 573 | |
Bryan Wu | fad91c8 | 2007-12-04 23:45:14 -0800 | [diff] [blame] | 574 | if (!drv_data->cs_change) |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 575 | cs_deactive(drv_data, chip); |
Bryan Wu | fad91c8 | 2007-12-04 23:45:14 -0800 | [diff] [blame] | 576 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 577 | if (msg->complete) |
| 578 | msg->complete(msg->context); |
| 579 | } |
| 580 | |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 581 | static irqreturn_t dma_irq_handler(int irq, void *dev_id) |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 582 | { |
Jeff Garzik | 15aafa2 | 2008-02-06 01:36:20 -0800 | [diff] [blame] | 583 | struct driver_data *drv_data = dev_id; |
Bryan Wu | fad91c8 | 2007-12-04 23:45:14 -0800 | [diff] [blame] | 584 | struct chip_data *chip = drv_data->cur_chip; |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 585 | struct spi_message *msg = drv_data->cur_msg; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 586 | |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 587 | dev_dbg(&drv_data->pdev->dev, "in dma_irq_handler\n"); |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 588 | clear_dma_irqstat(drv_data->dma_channel); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 589 | |
Bryan Wu | d6fe89b | 2007-06-11 17:34:17 +0800 | [diff] [blame] | 590 | /* Wait for DMA to complete */ |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 591 | while (get_dma_curr_irqstat(drv_data->dma_channel) & DMA_RUN) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 592 | cpu_relax(); |
Bryan Wu | d6fe89b | 2007-06-11 17:34:17 +0800 | [diff] [blame] | 593 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 594 | /* |
Bryan Wu | d6fe89b | 2007-06-11 17:34:17 +0800 | [diff] [blame] | 595 | * wait for the last transaction shifted out. HRM states: |
| 596 | * at this point there may still be data in the SPI DMA FIFO waiting |
| 597 | * to be transmitted ... software needs to poll TXS in the SPI_STAT |
| 598 | * register until it goes low for 2 successive reads |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 599 | */ |
| 600 | if (drv_data->tx != NULL) { |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 601 | while ((read_STAT(drv_data) & TXS) || |
| 602 | (read_STAT(drv_data) & TXS)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 603 | cpu_relax(); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 604 | } |
| 605 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 606 | while (!(read_STAT(drv_data) & SPIF)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 607 | cpu_relax(); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 608 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 609 | msg->actual_length += drv_data->len_in_bytes; |
| 610 | |
Bryan Wu | fad91c8 | 2007-12-04 23:45:14 -0800 | [diff] [blame] | 611 | if (drv_data->cs_change) |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 612 | cs_deactive(drv_data, chip); |
Bryan Wu | fad91c8 | 2007-12-04 23:45:14 -0800 | [diff] [blame] | 613 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 614 | /* Move to next transfer */ |
| 615 | msg->state = next_transfer(drv_data); |
| 616 | |
| 617 | /* Schedule transfer tasklet */ |
| 618 | tasklet_schedule(&drv_data->pump_transfers); |
| 619 | |
| 620 | /* free the irq handler before next transfer */ |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 621 | dev_dbg(&drv_data->pdev->dev, |
| 622 | "disable dma channel irq%d\n", |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 623 | drv_data->dma_channel); |
| 624 | dma_disable_irq(drv_data->dma_channel); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 625 | |
| 626 | return IRQ_HANDLED; |
| 627 | } |
| 628 | |
| 629 | static void pump_transfers(unsigned long data) |
| 630 | { |
| 631 | struct driver_data *drv_data = (struct driver_data *)data; |
| 632 | struct spi_message *message = NULL; |
| 633 | struct spi_transfer *transfer = NULL; |
| 634 | struct spi_transfer *previous = NULL; |
| 635 | struct chip_data *chip = NULL; |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 636 | u8 width; |
| 637 | u16 cr, dma_width, dma_config; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 638 | u32 tranf_success = 1; |
| 639 | |
| 640 | /* Get current state information */ |
| 641 | message = drv_data->cur_msg; |
| 642 | transfer = drv_data->cur_transfer; |
| 643 | chip = drv_data->cur_chip; |
Bryan Wu | 092e1fd | 2007-12-04 23:45:23 -0800 | [diff] [blame] | 644 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 645 | /* |
| 646 | * if msg is error or done, report it back using complete() callback |
| 647 | */ |
| 648 | |
| 649 | /* Handle for abort */ |
| 650 | if (message->state == ERROR_STATE) { |
| 651 | message->status = -EIO; |
| 652 | giveback(drv_data); |
| 653 | return; |
| 654 | } |
| 655 | |
| 656 | /* Handle end of message */ |
| 657 | if (message->state == DONE_STATE) { |
| 658 | message->status = 0; |
| 659 | giveback(drv_data); |
| 660 | return; |
| 661 | } |
| 662 | |
| 663 | /* Delay if requested at end of transfer */ |
| 664 | if (message->state == RUNNING_STATE) { |
| 665 | previous = list_entry(transfer->transfer_list.prev, |
| 666 | struct spi_transfer, transfer_list); |
| 667 | if (previous->delay_usecs) |
| 668 | udelay(previous->delay_usecs); |
| 669 | } |
| 670 | |
| 671 | /* Setup the transfer state based on the type of transfer */ |
| 672 | if (flush(drv_data) == 0) { |
| 673 | dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n"); |
| 674 | message->status = -EIO; |
| 675 | giveback(drv_data); |
| 676 | return; |
| 677 | } |
| 678 | |
| 679 | if (transfer->tx_buf != NULL) { |
| 680 | drv_data->tx = (void *)transfer->tx_buf; |
| 681 | drv_data->tx_end = drv_data->tx + transfer->len; |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 682 | dev_dbg(&drv_data->pdev->dev, "tx_buf is %p, tx_end is %p\n", |
| 683 | transfer->tx_buf, drv_data->tx_end); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 684 | } else { |
| 685 | drv_data->tx = NULL; |
| 686 | } |
| 687 | |
| 688 | if (transfer->rx_buf != NULL) { |
| 689 | drv_data->rx = transfer->rx_buf; |
| 690 | drv_data->rx_end = drv_data->rx + transfer->len; |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 691 | dev_dbg(&drv_data->pdev->dev, "rx_buf is %p, rx_end is %p\n", |
| 692 | transfer->rx_buf, drv_data->rx_end); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 693 | } else { |
| 694 | drv_data->rx = NULL; |
| 695 | } |
| 696 | |
| 697 | drv_data->rx_dma = transfer->rx_dma; |
| 698 | drv_data->tx_dma = transfer->tx_dma; |
| 699 | drv_data->len_in_bytes = transfer->len; |
Bryan Wu | fad91c8 | 2007-12-04 23:45:14 -0800 | [diff] [blame] | 700 | drv_data->cs_change = transfer->cs_change; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 701 | |
Bryan Wu | 092e1fd | 2007-12-04 23:45:23 -0800 | [diff] [blame] | 702 | /* Bits per word setup */ |
| 703 | switch (transfer->bits_per_word) { |
| 704 | case 8: |
| 705 | drv_data->n_bytes = 1; |
| 706 | width = CFG_SPI_WORDSIZE8; |
| 707 | drv_data->read = chip->cs_change_per_word ? |
| 708 | u8_cs_chg_reader : u8_reader; |
| 709 | drv_data->write = chip->cs_change_per_word ? |
| 710 | u8_cs_chg_writer : u8_writer; |
| 711 | drv_data->duplex = chip->cs_change_per_word ? |
| 712 | u8_cs_chg_duplex : u8_duplex; |
| 713 | break; |
| 714 | |
| 715 | case 16: |
| 716 | drv_data->n_bytes = 2; |
| 717 | width = CFG_SPI_WORDSIZE16; |
| 718 | drv_data->read = chip->cs_change_per_word ? |
| 719 | u16_cs_chg_reader : u16_reader; |
| 720 | drv_data->write = chip->cs_change_per_word ? |
| 721 | u16_cs_chg_writer : u16_writer; |
| 722 | drv_data->duplex = chip->cs_change_per_word ? |
| 723 | u16_cs_chg_duplex : u16_duplex; |
| 724 | break; |
| 725 | |
| 726 | default: |
| 727 | /* No change, the same as default setting */ |
| 728 | drv_data->n_bytes = chip->n_bytes; |
| 729 | width = chip->width; |
| 730 | drv_data->write = drv_data->tx ? chip->write : null_writer; |
| 731 | drv_data->read = drv_data->rx ? chip->read : null_reader; |
| 732 | drv_data->duplex = chip->duplex ? chip->duplex : null_writer; |
| 733 | break; |
| 734 | } |
| 735 | cr = (read_CTRL(drv_data) & (~BIT_CTL_TIMOD)); |
| 736 | cr |= (width << 8); |
| 737 | write_CTRL(drv_data, cr); |
| 738 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 739 | if (width == CFG_SPI_WORDSIZE16) { |
| 740 | drv_data->len = (transfer->len) >> 1; |
| 741 | } else { |
| 742 | drv_data->len = transfer->len; |
| 743 | } |
Bryan Wu | 131b17d | 2007-12-04 23:45:12 -0800 | [diff] [blame] | 744 | dev_dbg(&drv_data->pdev->dev, "transfer: ", |
| 745 | "drv_data->write is %p, chip->write is %p, null_wr is %p\n", |
| 746 | drv_data->write, chip->write, null_writer); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 747 | |
| 748 | /* speed and width has been set on per message */ |
| 749 | message->state = RUNNING_STATE; |
| 750 | dma_config = 0; |
| 751 | |
Bryan Wu | 092e1fd | 2007-12-04 23:45:23 -0800 | [diff] [blame] | 752 | /* Speed setup (surely valid because already checked) */ |
| 753 | if (transfer->speed_hz) |
| 754 | write_BAUD(drv_data, hz_to_spi_baud(transfer->speed_hz)); |
| 755 | else |
| 756 | write_BAUD(drv_data, chip->baud); |
| 757 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 758 | write_STAT(drv_data, BIT_STAT_CLR); |
| 759 | cr = (read_CTRL(drv_data) & (~BIT_CTL_TIMOD)); |
| 760 | cs_active(drv_data, chip); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 761 | |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 762 | dev_dbg(&drv_data->pdev->dev, |
| 763 | "now pumping a transfer: width is %d, len is %d\n", |
| 764 | width, transfer->len); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 765 | |
| 766 | /* |
| 767 | * Try to map dma buffer and do a dma transfer if |
| 768 | * successful use different way to r/w according to |
| 769 | * drv_data->cur_chip->enable_dma |
| 770 | */ |
| 771 | if (drv_data->cur_chip->enable_dma && drv_data->len > 6) { |
| 772 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 773 | disable_dma(drv_data->dma_channel); |
| 774 | clear_dma_irqstat(drv_data->dma_channel); |
Sonic Zhang | 07612e5 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 775 | bfin_spi_disable(drv_data); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 776 | |
| 777 | /* config dma channel */ |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 778 | dev_dbg(&drv_data->pdev->dev, "doing dma transfer\n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 779 | if (width == CFG_SPI_WORDSIZE16) { |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 780 | set_dma_x_count(drv_data->dma_channel, drv_data->len); |
| 781 | set_dma_x_modify(drv_data->dma_channel, 2); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 782 | dma_width = WDSIZE_16; |
| 783 | } else { |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 784 | set_dma_x_count(drv_data->dma_channel, drv_data->len); |
| 785 | set_dma_x_modify(drv_data->dma_channel, 1); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 786 | dma_width = WDSIZE_8; |
| 787 | } |
| 788 | |
Sonic Zhang | 3f479a6 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 789 | /* poll for SPI completion before start */ |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 790 | while (!(read_STAT(drv_data) & BIT_STAT_SPIF)) |
Bryan Wu | d8c0500 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 791 | cpu_relax(); |
Sonic Zhang | 3f479a6 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 792 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 793 | /* dirty hack for autobuffer DMA mode */ |
| 794 | if (drv_data->tx_dma == 0xFFFF) { |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 795 | dev_dbg(&drv_data->pdev->dev, |
| 796 | "doing autobuffer DMA out.\n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 797 | |
| 798 | /* no irq in autobuffer mode */ |
| 799 | dma_config = |
| 800 | (DMAFLOW_AUTO | RESTART | dma_width | DI_EN); |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 801 | set_dma_config(drv_data->dma_channel, dma_config); |
| 802 | set_dma_start_addr(drv_data->dma_channel, |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 803 | (unsigned long)drv_data->tx); |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 804 | enable_dma(drv_data->dma_channel); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 805 | |
Sonic Zhang | 07612e5 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 806 | /* start SPI transfer */ |
| 807 | write_CTRL(drv_data, |
| 808 | (cr | CFG_SPI_DMAWRITE | BIT_CTL_ENABLE)); |
| 809 | |
| 810 | /* just return here, there can only be one transfer |
| 811 | * in this mode |
| 812 | */ |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 813 | message->status = 0; |
| 814 | giveback(drv_data); |
| 815 | return; |
| 816 | } |
| 817 | |
| 818 | /* In dma mode, rx or tx must be NULL in one transfer */ |
| 819 | if (drv_data->rx != NULL) { |
| 820 | /* set transfer mode, and enable SPI */ |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 821 | dev_dbg(&drv_data->pdev->dev, "doing DMA in.\n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 822 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 823 | /* clear tx reg soformer data is not shifted out */ |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 824 | write_TDBR(drv_data, 0xFFFF); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 825 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 826 | set_dma_x_count(drv_data->dma_channel, drv_data->len); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 827 | |
| 828 | /* start dma */ |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 829 | dma_enable_irq(drv_data->dma_channel); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 830 | dma_config = (WNR | RESTART | dma_width | DI_EN); |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 831 | set_dma_config(drv_data->dma_channel, dma_config); |
| 832 | set_dma_start_addr(drv_data->dma_channel, |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 833 | (unsigned long)drv_data->rx); |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 834 | enable_dma(drv_data->dma_channel); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 835 | |
Sonic Zhang | 07612e5 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 836 | /* start SPI transfer */ |
| 837 | write_CTRL(drv_data, |
| 838 | (cr | CFG_SPI_DMAREAD | BIT_CTL_ENABLE)); |
| 839 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 840 | } else if (drv_data->tx != NULL) { |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 841 | dev_dbg(&drv_data->pdev->dev, "doing DMA out.\n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 842 | |
| 843 | /* start dma */ |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 844 | dma_enable_irq(drv_data->dma_channel); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 845 | dma_config = (RESTART | dma_width | DI_EN); |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 846 | set_dma_config(drv_data->dma_channel, dma_config); |
| 847 | set_dma_start_addr(drv_data->dma_channel, |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 848 | (unsigned long)drv_data->tx); |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 849 | enable_dma(drv_data->dma_channel); |
Sonic Zhang | 07612e5 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 850 | |
| 851 | /* start SPI transfer */ |
| 852 | write_CTRL(drv_data, |
| 853 | (cr | CFG_SPI_DMAWRITE | BIT_CTL_ENABLE)); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 854 | } |
| 855 | } else { |
| 856 | /* IO mode write then read */ |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 857 | dev_dbg(&drv_data->pdev->dev, "doing IO transfer\n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 858 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 859 | if (drv_data->tx != NULL && drv_data->rx != NULL) { |
| 860 | /* full duplex mode */ |
| 861 | BUG_ON((drv_data->tx_end - drv_data->tx) != |
| 862 | (drv_data->rx_end - drv_data->rx)); |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 863 | dev_dbg(&drv_data->pdev->dev, |
| 864 | "IO duplex: cr is 0x%x\n", cr); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 865 | |
Sonic Zhang | cc487e7 | 2007-12-04 23:45:17 -0800 | [diff] [blame] | 866 | /* set SPI transfer mode */ |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 867 | write_CTRL(drv_data, (cr | CFG_SPI_WRITE)); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 868 | |
| 869 | drv_data->duplex(drv_data); |
| 870 | |
| 871 | if (drv_data->tx != drv_data->tx_end) |
| 872 | tranf_success = 0; |
| 873 | } else if (drv_data->tx != NULL) { |
| 874 | /* write only half duplex */ |
Bryan Wu | 131b17d | 2007-12-04 23:45:12 -0800 | [diff] [blame] | 875 | dev_dbg(&drv_data->pdev->dev, |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 876 | "IO write: cr is 0x%x\n", cr); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 877 | |
Sonic Zhang | cc487e7 | 2007-12-04 23:45:17 -0800 | [diff] [blame] | 878 | /* set SPI transfer mode */ |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 879 | write_CTRL(drv_data, (cr | CFG_SPI_WRITE)); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 880 | |
| 881 | drv_data->write(drv_data); |
| 882 | |
| 883 | if (drv_data->tx != drv_data->tx_end) |
| 884 | tranf_success = 0; |
| 885 | } else if (drv_data->rx != NULL) { |
| 886 | /* read only half duplex */ |
Bryan Wu | 131b17d | 2007-12-04 23:45:12 -0800 | [diff] [blame] | 887 | dev_dbg(&drv_data->pdev->dev, |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 888 | "IO read: cr is 0x%x\n", cr); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 889 | |
Sonic Zhang | cc487e7 | 2007-12-04 23:45:17 -0800 | [diff] [blame] | 890 | /* set SPI transfer mode */ |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 891 | write_CTRL(drv_data, (cr | CFG_SPI_READ)); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 892 | |
| 893 | drv_data->read(drv_data); |
| 894 | if (drv_data->rx != drv_data->rx_end) |
| 895 | tranf_success = 0; |
| 896 | } |
| 897 | |
| 898 | if (!tranf_success) { |
Bryan Wu | 131b17d | 2007-12-04 23:45:12 -0800 | [diff] [blame] | 899 | dev_dbg(&drv_data->pdev->dev, |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 900 | "IO write error!\n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 901 | message->state = ERROR_STATE; |
| 902 | } else { |
| 903 | /* Update total byte transfered */ |
| 904 | message->actual_length += drv_data->len; |
| 905 | |
| 906 | /* Move to next transfer of this msg */ |
| 907 | message->state = next_transfer(drv_data); |
| 908 | } |
| 909 | |
| 910 | /* Schedule next transfer tasklet */ |
| 911 | tasklet_schedule(&drv_data->pump_transfers); |
| 912 | |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | /* pop a msg from queue and kick off real transfer */ |
| 917 | static void pump_messages(struct work_struct *work) |
| 918 | { |
Bryan Wu | 131b17d | 2007-12-04 23:45:12 -0800 | [diff] [blame] | 919 | struct driver_data *drv_data; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 920 | unsigned long flags; |
| 921 | |
Bryan Wu | 131b17d | 2007-12-04 23:45:12 -0800 | [diff] [blame] | 922 | drv_data = container_of(work, struct driver_data, pump_messages); |
| 923 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 924 | /* Lock queue and check for queue work */ |
| 925 | spin_lock_irqsave(&drv_data->lock, flags); |
| 926 | if (list_empty(&drv_data->queue) || drv_data->run == QUEUE_STOPPED) { |
| 927 | /* pumper kicked off but no work to do */ |
| 928 | drv_data->busy = 0; |
| 929 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 930 | return; |
| 931 | } |
| 932 | |
| 933 | /* Make sure we are not already running a message */ |
| 934 | if (drv_data->cur_msg) { |
| 935 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 936 | return; |
| 937 | } |
| 938 | |
| 939 | /* Extract head of queue */ |
| 940 | drv_data->cur_msg = list_entry(drv_data->queue.next, |
| 941 | struct spi_message, queue); |
Bryan Wu | 5fec5b5 | 2007-12-04 23:45:13 -0800 | [diff] [blame] | 942 | |
| 943 | /* Setup the SSP using the per chip configuration */ |
| 944 | drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi); |
Bryan Wu | 8d20d0a | 2008-02-06 01:38:17 -0800 | [diff] [blame] | 945 | restore_state(drv_data); |
Bryan Wu | 5fec5b5 | 2007-12-04 23:45:13 -0800 | [diff] [blame] | 946 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 947 | list_del_init(&drv_data->cur_msg->queue); |
| 948 | |
| 949 | /* Initial message state */ |
| 950 | drv_data->cur_msg->state = START_STATE; |
| 951 | drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next, |
| 952 | struct spi_transfer, transfer_list); |
| 953 | |
Bryan Wu | 5fec5b5 | 2007-12-04 23:45:13 -0800 | [diff] [blame] | 954 | dev_dbg(&drv_data->pdev->dev, "got a message to pump, " |
| 955 | "state is set to: baud %d, flag 0x%x, ctl 0x%x\n", |
| 956 | drv_data->cur_chip->baud, drv_data->cur_chip->flag, |
| 957 | drv_data->cur_chip->ctl_reg); |
Bryan Wu | 131b17d | 2007-12-04 23:45:12 -0800 | [diff] [blame] | 958 | |
| 959 | dev_dbg(&drv_data->pdev->dev, |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 960 | "the first transfer len is %d\n", |
| 961 | drv_data->cur_transfer->len); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 962 | |
| 963 | /* Mark as busy and launch transfers */ |
| 964 | tasklet_schedule(&drv_data->pump_transfers); |
| 965 | |
| 966 | drv_data->busy = 1; |
| 967 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 968 | } |
| 969 | |
| 970 | /* |
| 971 | * got a msg to transfer, queue it in drv_data->queue. |
| 972 | * And kick off message pumper |
| 973 | */ |
| 974 | static int transfer(struct spi_device *spi, struct spi_message *msg) |
| 975 | { |
| 976 | struct driver_data *drv_data = spi_master_get_devdata(spi->master); |
| 977 | unsigned long flags; |
| 978 | |
| 979 | spin_lock_irqsave(&drv_data->lock, flags); |
| 980 | |
| 981 | if (drv_data->run == QUEUE_STOPPED) { |
| 982 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 983 | return -ESHUTDOWN; |
| 984 | } |
| 985 | |
| 986 | msg->actual_length = 0; |
| 987 | msg->status = -EINPROGRESS; |
| 988 | msg->state = START_STATE; |
| 989 | |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 990 | dev_dbg(&spi->dev, "adding an msg in transfer() \n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 991 | list_add_tail(&msg->queue, &drv_data->queue); |
| 992 | |
| 993 | if (drv_data->run == QUEUE_RUNNING && !drv_data->busy) |
| 994 | queue_work(drv_data->workqueue, &drv_data->pump_messages); |
| 995 | |
| 996 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 997 | |
| 998 | return 0; |
| 999 | } |
| 1000 | |
Sonic Zhang | 12e17c4 | 2007-12-04 23:45:16 -0800 | [diff] [blame] | 1001 | #define MAX_SPI_SSEL 7 |
| 1002 | |
| 1003 | static u16 ssel[3][MAX_SPI_SSEL] = { |
| 1004 | {P_SPI0_SSEL1, P_SPI0_SSEL2, P_SPI0_SSEL3, |
| 1005 | P_SPI0_SSEL4, P_SPI0_SSEL5, |
| 1006 | P_SPI0_SSEL6, P_SPI0_SSEL7}, |
| 1007 | |
| 1008 | {P_SPI1_SSEL1, P_SPI1_SSEL2, P_SPI1_SSEL3, |
| 1009 | P_SPI1_SSEL4, P_SPI1_SSEL5, |
| 1010 | P_SPI1_SSEL6, P_SPI1_SSEL7}, |
| 1011 | |
| 1012 | {P_SPI2_SSEL1, P_SPI2_SSEL2, P_SPI2_SSEL3, |
| 1013 | P_SPI2_SSEL4, P_SPI2_SSEL5, |
| 1014 | P_SPI2_SSEL6, P_SPI2_SSEL7}, |
| 1015 | }; |
| 1016 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1017 | /* first setup for new devices */ |
| 1018 | static int setup(struct spi_device *spi) |
| 1019 | { |
| 1020 | struct bfin5xx_spi_chip *chip_info = NULL; |
| 1021 | struct chip_data *chip; |
| 1022 | struct driver_data *drv_data = spi_master_get_devdata(spi->master); |
| 1023 | u8 spi_flg; |
| 1024 | |
| 1025 | /* Abort device setup if requested features are not supported */ |
| 1026 | if (spi->mode & ~(SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST)) { |
| 1027 | dev_err(&spi->dev, "requested mode not fully supported\n"); |
| 1028 | return -EINVAL; |
| 1029 | } |
| 1030 | |
| 1031 | /* Zero (the default) here means 8 bits */ |
| 1032 | if (!spi->bits_per_word) |
| 1033 | spi->bits_per_word = 8; |
| 1034 | |
| 1035 | if (spi->bits_per_word != 8 && spi->bits_per_word != 16) |
| 1036 | return -EINVAL; |
| 1037 | |
| 1038 | /* Only alloc (or use chip_info) on first setup */ |
| 1039 | chip = spi_get_ctldata(spi); |
| 1040 | if (chip == NULL) { |
| 1041 | chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL); |
| 1042 | if (!chip) |
| 1043 | return -ENOMEM; |
| 1044 | |
| 1045 | chip->enable_dma = 0; |
| 1046 | chip_info = spi->controller_data; |
| 1047 | } |
| 1048 | |
| 1049 | /* chip_info isn't always needed */ |
| 1050 | if (chip_info) { |
Mike Frysinger | 2ed3551 | 2007-12-04 23:45:14 -0800 | [diff] [blame] | 1051 | /* Make sure people stop trying to set fields via ctl_reg |
| 1052 | * when they should actually be using common SPI framework. |
| 1053 | * Currently we let through: WOM EMISO PSSE GM SZ TIMOD. |
| 1054 | * Not sure if a user actually needs/uses any of these, |
| 1055 | * but let's assume (for now) they do. |
| 1056 | */ |
| 1057 | if (chip_info->ctl_reg & (SPE|MSTR|CPOL|CPHA|LSBF|SIZE)) { |
| 1058 | dev_err(&spi->dev, "do not set bits in ctl_reg " |
| 1059 | "that the SPI framework manages\n"); |
| 1060 | return -EINVAL; |
| 1061 | } |
| 1062 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1063 | chip->enable_dma = chip_info->enable_dma != 0 |
| 1064 | && drv_data->master_info->enable_dma; |
| 1065 | chip->ctl_reg = chip_info->ctl_reg; |
| 1066 | chip->bits_per_word = chip_info->bits_per_word; |
| 1067 | chip->cs_change_per_word = chip_info->cs_change_per_word; |
| 1068 | chip->cs_chg_udelay = chip_info->cs_chg_udelay; |
| 1069 | } |
| 1070 | |
| 1071 | /* translate common spi framework into our register */ |
| 1072 | if (spi->mode & SPI_CPOL) |
| 1073 | chip->ctl_reg |= CPOL; |
| 1074 | if (spi->mode & SPI_CPHA) |
| 1075 | chip->ctl_reg |= CPHA; |
| 1076 | if (spi->mode & SPI_LSB_FIRST) |
| 1077 | chip->ctl_reg |= LSBF; |
| 1078 | /* we dont support running in slave mode (yet?) */ |
| 1079 | chip->ctl_reg |= MSTR; |
| 1080 | |
| 1081 | /* |
| 1082 | * if any one SPI chip is registered and wants DMA, request the |
| 1083 | * DMA channel for it |
| 1084 | */ |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 1085 | if (chip->enable_dma && !drv_data->dma_requested) { |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1086 | /* register dma irq handler */ |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 1087 | if (request_dma(drv_data->dma_channel, "BF53x_SPI_DMA") < 0) { |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 1088 | dev_dbg(&spi->dev, |
| 1089 | "Unable to request BlackFin SPI DMA channel\n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1090 | return -ENODEV; |
| 1091 | } |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 1092 | if (set_dma_callback(drv_data->dma_channel, |
| 1093 | (void *)dma_irq_handler, drv_data) < 0) { |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 1094 | dev_dbg(&spi->dev, "Unable to set dma callback\n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1095 | return -EPERM; |
| 1096 | } |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 1097 | dma_disable_irq(drv_data->dma_channel); |
| 1098 | drv_data->dma_requested = 1; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | /* |
| 1102 | * Notice: for blackfin, the speed_hz is the value of register |
| 1103 | * SPI_BAUD, not the real baudrate |
| 1104 | */ |
| 1105 | chip->baud = hz_to_spi_baud(spi->max_speed_hz); |
| 1106 | spi_flg = ~(1 << (spi->chip_select)); |
| 1107 | chip->flag = ((u16) spi_flg << 8) | (1 << (spi->chip_select)); |
| 1108 | chip->chip_select_num = spi->chip_select; |
| 1109 | |
| 1110 | switch (chip->bits_per_word) { |
| 1111 | case 8: |
| 1112 | chip->n_bytes = 1; |
| 1113 | chip->width = CFG_SPI_WORDSIZE8; |
| 1114 | chip->read = chip->cs_change_per_word ? |
| 1115 | u8_cs_chg_reader : u8_reader; |
| 1116 | chip->write = chip->cs_change_per_word ? |
| 1117 | u8_cs_chg_writer : u8_writer; |
| 1118 | chip->duplex = chip->cs_change_per_word ? |
| 1119 | u8_cs_chg_duplex : u8_duplex; |
| 1120 | break; |
| 1121 | |
| 1122 | case 16: |
| 1123 | chip->n_bytes = 2; |
| 1124 | chip->width = CFG_SPI_WORDSIZE16; |
| 1125 | chip->read = chip->cs_change_per_word ? |
| 1126 | u16_cs_chg_reader : u16_reader; |
| 1127 | chip->write = chip->cs_change_per_word ? |
| 1128 | u16_cs_chg_writer : u16_writer; |
| 1129 | chip->duplex = chip->cs_change_per_word ? |
| 1130 | u16_cs_chg_duplex : u16_duplex; |
| 1131 | break; |
| 1132 | |
| 1133 | default: |
| 1134 | dev_err(&spi->dev, "%d bits_per_word is not supported\n", |
| 1135 | chip->bits_per_word); |
| 1136 | kfree(chip); |
| 1137 | return -ENODEV; |
| 1138 | } |
| 1139 | |
Joe Perches | 898eb71 | 2007-10-18 03:06:30 -0700 | [diff] [blame] | 1140 | 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] | 1141 | spi->modalias, chip->width, chip->enable_dma); |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 1142 | 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] | 1143 | chip->ctl_reg, chip->flag); |
| 1144 | |
| 1145 | spi_set_ctldata(spi, chip); |
| 1146 | |
Sonic Zhang | 12e17c4 | 2007-12-04 23:45:16 -0800 | [diff] [blame] | 1147 | dev_dbg(&spi->dev, "chip select number is %d\n", chip->chip_select_num); |
| 1148 | if ((chip->chip_select_num > 0) |
| 1149 | && (chip->chip_select_num <= spi->master->num_chipselect)) |
| 1150 | peripheral_request(ssel[spi->master->bus_num] |
Bryan Wu | aab0d83 | 2008-02-06 01:38:17 -0800 | [diff] [blame] | 1151 | [chip->chip_select_num-1], spi->modalias); |
Sonic Zhang | 12e17c4 | 2007-12-04 23:45:16 -0800 | [diff] [blame] | 1152 | |
Sonic Zhang | 07612e5 | 2007-12-04 23:45:21 -0800 | [diff] [blame] | 1153 | cs_deactive(drv_data, chip); |
| 1154 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1155 | return 0; |
| 1156 | } |
| 1157 | |
| 1158 | /* |
| 1159 | * callback for spi framework. |
| 1160 | * clean driver specific data |
| 1161 | */ |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 1162 | static void cleanup(struct spi_device *spi) |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1163 | { |
Mike Frysinger | 27bb9e7 | 2007-06-11 15:31:30 +0800 | [diff] [blame] | 1164 | struct chip_data *chip = spi_get_ctldata(spi); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1165 | |
Sonic Zhang | 12e17c4 | 2007-12-04 23:45:16 -0800 | [diff] [blame] | 1166 | if ((chip->chip_select_num > 0) |
| 1167 | && (chip->chip_select_num <= spi->master->num_chipselect)) |
| 1168 | peripheral_free(ssel[spi->master->bus_num] |
| 1169 | [chip->chip_select_num-1]); |
| 1170 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1171 | kfree(chip); |
| 1172 | } |
| 1173 | |
| 1174 | static inline int init_queue(struct driver_data *drv_data) |
| 1175 | { |
| 1176 | INIT_LIST_HEAD(&drv_data->queue); |
| 1177 | spin_lock_init(&drv_data->lock); |
| 1178 | |
| 1179 | drv_data->run = QUEUE_STOPPED; |
| 1180 | drv_data->busy = 0; |
| 1181 | |
| 1182 | /* init transfer tasklet */ |
| 1183 | tasklet_init(&drv_data->pump_transfers, |
| 1184 | pump_transfers, (unsigned long)drv_data); |
| 1185 | |
| 1186 | /* init messages workqueue */ |
| 1187 | INIT_WORK(&drv_data->pump_messages, pump_messages); |
| 1188 | drv_data->workqueue = |
Tony Jones | 49dce68 | 2007-10-16 01:27:48 -0700 | [diff] [blame] | 1189 | create_singlethread_workqueue(drv_data->master->dev.parent->bus_id); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1190 | if (drv_data->workqueue == NULL) |
| 1191 | return -EBUSY; |
| 1192 | |
| 1193 | return 0; |
| 1194 | } |
| 1195 | |
| 1196 | static inline int start_queue(struct driver_data *drv_data) |
| 1197 | { |
| 1198 | unsigned long flags; |
| 1199 | |
| 1200 | spin_lock_irqsave(&drv_data->lock, flags); |
| 1201 | |
| 1202 | if (drv_data->run == QUEUE_RUNNING || drv_data->busy) { |
| 1203 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 1204 | return -EBUSY; |
| 1205 | } |
| 1206 | |
| 1207 | drv_data->run = QUEUE_RUNNING; |
| 1208 | drv_data->cur_msg = NULL; |
| 1209 | drv_data->cur_transfer = NULL; |
| 1210 | drv_data->cur_chip = NULL; |
| 1211 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 1212 | |
| 1213 | queue_work(drv_data->workqueue, &drv_data->pump_messages); |
| 1214 | |
| 1215 | return 0; |
| 1216 | } |
| 1217 | |
| 1218 | static inline int stop_queue(struct driver_data *drv_data) |
| 1219 | { |
| 1220 | unsigned long flags; |
| 1221 | unsigned limit = 500; |
| 1222 | int status = 0; |
| 1223 | |
| 1224 | spin_lock_irqsave(&drv_data->lock, flags); |
| 1225 | |
| 1226 | /* |
| 1227 | * This is a bit lame, but is optimized for the common execution path. |
| 1228 | * A wait_queue on the drv_data->busy could be used, but then the common |
| 1229 | * execution path (pump_messages) would be required to call wake_up or |
| 1230 | * friends on every SPI message. Do this instead |
| 1231 | */ |
| 1232 | drv_data->run = QUEUE_STOPPED; |
| 1233 | while (!list_empty(&drv_data->queue) && drv_data->busy && limit--) { |
| 1234 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 1235 | msleep(10); |
| 1236 | spin_lock_irqsave(&drv_data->lock, flags); |
| 1237 | } |
| 1238 | |
| 1239 | if (!list_empty(&drv_data->queue) || drv_data->busy) |
| 1240 | status = -EBUSY; |
| 1241 | |
| 1242 | spin_unlock_irqrestore(&drv_data->lock, flags); |
| 1243 | |
| 1244 | return status; |
| 1245 | } |
| 1246 | |
| 1247 | static inline int destroy_queue(struct driver_data *drv_data) |
| 1248 | { |
| 1249 | int status; |
| 1250 | |
| 1251 | status = stop_queue(drv_data); |
| 1252 | if (status != 0) |
| 1253 | return status; |
| 1254 | |
| 1255 | destroy_workqueue(drv_data->workqueue); |
| 1256 | |
| 1257 | return 0; |
| 1258 | } |
| 1259 | |
| 1260 | static int __init bfin5xx_spi_probe(struct platform_device *pdev) |
| 1261 | { |
| 1262 | struct device *dev = &pdev->dev; |
| 1263 | struct bfin5xx_spi_master *platform_info; |
| 1264 | struct spi_master *master; |
| 1265 | struct driver_data *drv_data = 0; |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1266 | struct resource *res; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1267 | int status = 0; |
| 1268 | |
| 1269 | platform_info = dev->platform_data; |
| 1270 | |
| 1271 | /* Allocate master with space for drv_data */ |
| 1272 | master = spi_alloc_master(dev, sizeof(struct driver_data) + 16); |
| 1273 | if (!master) { |
| 1274 | dev_err(&pdev->dev, "can not alloc spi_master\n"); |
| 1275 | return -ENOMEM; |
| 1276 | } |
Bryan Wu | 131b17d | 2007-12-04 23:45:12 -0800 | [diff] [blame] | 1277 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1278 | drv_data = spi_master_get_devdata(master); |
| 1279 | drv_data->master = master; |
| 1280 | drv_data->master_info = platform_info; |
| 1281 | drv_data->pdev = pdev; |
Bryan Wu | 003d922 | 2007-12-04 23:45:22 -0800 | [diff] [blame] | 1282 | drv_data->pin_req = platform_info->pin_req; |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1283 | |
| 1284 | master->bus_num = pdev->id; |
| 1285 | master->num_chipselect = platform_info->num_chipselect; |
| 1286 | master->cleanup = cleanup; |
| 1287 | master->setup = setup; |
| 1288 | master->transfer = transfer; |
| 1289 | |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1290 | /* Find and map our resources */ |
| 1291 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1292 | if (res == NULL) { |
| 1293 | dev_err(dev, "Cannot get IORESOURCE_MEM\n"); |
| 1294 | status = -ENOENT; |
| 1295 | goto out_error_get_res; |
| 1296 | } |
| 1297 | |
Bryan Wu | f452126 | 2007-12-04 23:45:22 -0800 | [diff] [blame] | 1298 | drv_data->regs_base = ioremap(res->start, (res->end - res->start + 1)); |
| 1299 | if (drv_data->regs_base == NULL) { |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1300 | dev_err(dev, "Cannot map IO\n"); |
| 1301 | status = -ENXIO; |
| 1302 | goto out_error_ioremap; |
| 1303 | } |
| 1304 | |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 1305 | drv_data->dma_channel = platform_get_irq(pdev, 0); |
| 1306 | if (drv_data->dma_channel < 0) { |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1307 | dev_err(dev, "No DMA channel specified\n"); |
| 1308 | status = -ENOENT; |
| 1309 | goto out_error_no_dma_ch; |
| 1310 | } |
| 1311 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1312 | /* Initial and start queue */ |
| 1313 | status = init_queue(drv_data); |
| 1314 | if (status != 0) { |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1315 | dev_err(dev, "problem initializing queue\n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1316 | goto out_error_queue_alloc; |
| 1317 | } |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1318 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1319 | status = start_queue(drv_data); |
| 1320 | if (status != 0) { |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1321 | dev_err(dev, "problem starting queue\n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1322 | goto out_error_queue_alloc; |
| 1323 | } |
| 1324 | |
| 1325 | /* Register with the SPI framework */ |
| 1326 | platform_set_drvdata(pdev, drv_data); |
| 1327 | status = spi_register_master(master); |
| 1328 | if (status != 0) { |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1329 | dev_err(dev, "problem registering spi master\n"); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1330 | goto out_error_queue_alloc; |
| 1331 | } |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1332 | |
Bryan Wu | 003d922 | 2007-12-04 23:45:22 -0800 | [diff] [blame] | 1333 | status = peripheral_request_list(drv_data->pin_req, DRV_NAME); |
| 1334 | if (status != 0) { |
Sonic Zhang | 7c4ef09 | 2007-12-04 23:45:16 -0800 | [diff] [blame] | 1335 | dev_err(&pdev->dev, ": Requesting Peripherals failed\n"); |
| 1336 | goto out_error; |
| 1337 | } |
| 1338 | |
Bryan Wu | f452126 | 2007-12-04 23:45:22 -0800 | [diff] [blame] | 1339 | dev_info(dev, "%s, Version %s, regs_base@%p, dma channel@%d\n", |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 1340 | DRV_DESC, DRV_VERSION, drv_data->regs_base, |
| 1341 | drv_data->dma_channel); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1342 | return status; |
| 1343 | |
Michael Hennerich | cc2f81a | 2007-12-04 23:45:13 -0800 | [diff] [blame] | 1344 | out_error_queue_alloc: |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1345 | destroy_queue(drv_data); |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1346 | out_error_no_dma_ch: |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 1347 | iounmap((void *) drv_data->regs_base); |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1348 | out_error_ioremap: |
| 1349 | out_error_get_res: |
Michael Hennerich | cc2f81a | 2007-12-04 23:45:13 -0800 | [diff] [blame] | 1350 | out_error: |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1351 | spi_master_put(master); |
Michael Hennerich | cc2f81a | 2007-12-04 23:45:13 -0800 | [diff] [blame] | 1352 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1353 | return status; |
| 1354 | } |
| 1355 | |
| 1356 | /* stop hardware and remove the driver */ |
| 1357 | static int __devexit bfin5xx_spi_remove(struct platform_device *pdev) |
| 1358 | { |
| 1359 | struct driver_data *drv_data = platform_get_drvdata(pdev); |
| 1360 | int status = 0; |
| 1361 | |
| 1362 | if (!drv_data) |
| 1363 | return 0; |
| 1364 | |
| 1365 | /* Remove the queue */ |
| 1366 | status = destroy_queue(drv_data); |
| 1367 | if (status != 0) |
| 1368 | return status; |
| 1369 | |
| 1370 | /* Disable the SSP at the peripheral and SOC level */ |
| 1371 | bfin_spi_disable(drv_data); |
| 1372 | |
| 1373 | /* Release DMA */ |
| 1374 | if (drv_data->master_info->enable_dma) { |
Bryan Wu | bb90eb0 | 2007-12-04 23:45:18 -0800 | [diff] [blame] | 1375 | if (dma_channel_active(drv_data->dma_channel)) |
| 1376 | free_dma(drv_data->dma_channel); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1377 | } |
| 1378 | |
| 1379 | /* Disconnect from the SPI framework */ |
| 1380 | spi_unregister_master(drv_data->master); |
| 1381 | |
Bryan Wu | 003d922 | 2007-12-04 23:45:22 -0800 | [diff] [blame] | 1382 | peripheral_free_list(drv_data->pin_req); |
Michael Hennerich | cc2f81a | 2007-12-04 23:45:13 -0800 | [diff] [blame] | 1383 | |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1384 | /* Prevent double remove */ |
| 1385 | platform_set_drvdata(pdev, NULL); |
| 1386 | |
| 1387 | return 0; |
| 1388 | } |
| 1389 | |
| 1390 | #ifdef CONFIG_PM |
| 1391 | static int bfin5xx_spi_suspend(struct platform_device *pdev, pm_message_t state) |
| 1392 | { |
| 1393 | struct driver_data *drv_data = platform_get_drvdata(pdev); |
| 1394 | int status = 0; |
| 1395 | |
| 1396 | status = stop_queue(drv_data); |
| 1397 | if (status != 0) |
| 1398 | return status; |
| 1399 | |
| 1400 | /* stop hardware */ |
| 1401 | bfin_spi_disable(drv_data); |
| 1402 | |
| 1403 | return 0; |
| 1404 | } |
| 1405 | |
| 1406 | static int bfin5xx_spi_resume(struct platform_device *pdev) |
| 1407 | { |
| 1408 | struct driver_data *drv_data = platform_get_drvdata(pdev); |
| 1409 | int status = 0; |
| 1410 | |
| 1411 | /* Enable the SPI interface */ |
| 1412 | bfin_spi_enable(drv_data); |
| 1413 | |
| 1414 | /* Start the queue running */ |
| 1415 | status = start_queue(drv_data); |
| 1416 | if (status != 0) { |
| 1417 | dev_err(&pdev->dev, "problem starting queue (%d)\n", status); |
| 1418 | return status; |
| 1419 | } |
| 1420 | |
| 1421 | return 0; |
| 1422 | } |
| 1423 | #else |
| 1424 | #define bfin5xx_spi_suspend NULL |
| 1425 | #define bfin5xx_spi_resume NULL |
| 1426 | #endif /* CONFIG_PM */ |
| 1427 | |
David Brownell | fc3ba95 | 2007-08-30 23:56:24 -0700 | [diff] [blame] | 1428 | MODULE_ALIAS("bfin-spi-master"); /* for platform bus hotplug */ |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1429 | static struct platform_driver bfin5xx_spi_driver = { |
David Brownell | fc3ba95 | 2007-08-30 23:56:24 -0700 | [diff] [blame] | 1430 | .driver = { |
Bryan Wu | a32c691 | 2007-12-04 23:45:15 -0800 | [diff] [blame] | 1431 | .name = DRV_NAME, |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 1432 | .owner = THIS_MODULE, |
| 1433 | }, |
| 1434 | .suspend = bfin5xx_spi_suspend, |
| 1435 | .resume = bfin5xx_spi_resume, |
| 1436 | .remove = __devexit_p(bfin5xx_spi_remove), |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1437 | }; |
| 1438 | |
| 1439 | static int __init bfin5xx_spi_init(void) |
| 1440 | { |
Bryan Wu | 88b4036 | 2007-05-21 18:32:16 +0800 | [diff] [blame] | 1441 | return platform_driver_probe(&bfin5xx_spi_driver, bfin5xx_spi_probe); |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1442 | } |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1443 | module_init(bfin5xx_spi_init); |
| 1444 | |
| 1445 | static void __exit bfin5xx_spi_exit(void) |
| 1446 | { |
| 1447 | platform_driver_unregister(&bfin5xx_spi_driver); |
| 1448 | } |
Wu, Bryan | a5f6abd | 2007-05-06 14:50:34 -0700 | [diff] [blame] | 1449 | module_exit(bfin5xx_spi_exit); |