blob: e1d9eae1fae16db3878450058c16274599ec379f [file] [log] [blame]
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001/*
Mike Frysinger26fdc1f2008-02-06 01:38:21 -08002 * Blackfin On-Chip SPI Driver
Wu, Bryana5f6abd2007-05-06 14:50:34 -07003 *
Bryan Wu131b17d2007-12-04 23:45:12 -08004 * Copyright 2004-2007 Analog Devices Inc.
Wu, Bryana5f6abd2007-05-06 14:50:34 -07005 *
Mike Frysinger26fdc1f2008-02-06 01:38:21 -08006 * Enter bugs at http://blackfin.uclinux.org/
Wu, Bryana5f6abd2007-05-06 14:50:34 -07007 *
Mike Frysinger26fdc1f2008-02-06 01:38:21 -08008 * Licensed under the GPL-2 or later.
Wu, Bryana5f6abd2007-05-06 14:50:34 -07009 */
10
11#include <linux/init.h>
12#include <linux/module.h>
Bryan Wu131b17d2007-12-04 23:45:12 -080013#include <linux/delay.h>
Wu, Bryana5f6abd2007-05-06 14:50:34 -070014#include <linux/device.h>
Bryan Wu131b17d2007-12-04 23:45:12 -080015#include <linux/io.h>
Wu, Bryana5f6abd2007-05-06 14:50:34 -070016#include <linux/ioport.h>
Bryan Wu131b17d2007-12-04 23:45:12 -080017#include <linux/irq.h>
Wu, Bryana5f6abd2007-05-06 14:50:34 -070018#include <linux/errno.h>
19#include <linux/interrupt.h>
20#include <linux/platform_device.h>
21#include <linux/dma-mapping.h>
22#include <linux/spi/spi.h>
23#include <linux/workqueue.h>
Wu, Bryana5f6abd2007-05-06 14:50:34 -070024
Wu, Bryana5f6abd2007-05-06 14:50:34 -070025#include <asm/dma.h>
Bryan Wu131b17d2007-12-04 23:45:12 -080026#include <asm/portmux.h>
Wu, Bryana5f6abd2007-05-06 14:50:34 -070027#include <asm/bfin5xx_spi.h>
Vitja Makarov8cf58582009-04-06 19:00:31 -070028#include <asm/cacheflush.h>
29
Bryan Wua32c6912007-12-04 23:45:15 -080030#define DRV_NAME "bfin-spi"
31#define DRV_AUTHOR "Bryan Wu, Luke Yang"
Mike Frysinger138f97c2009-04-06 19:00:50 -070032#define DRV_DESC "Blackfin on-chip SPI Controller Driver"
Bryan Wua32c6912007-12-04 23:45:15 -080033#define DRV_VERSION "1.0"
34
35MODULE_AUTHOR(DRV_AUTHOR);
36MODULE_DESCRIPTION(DRV_DESC);
Wu, Bryana5f6abd2007-05-06 14:50:34 -070037MODULE_LICENSE("GPL");
38
Bryan Wubb90eb02007-12-04 23:45:18 -080039#define IS_DMA_ALIGNED(x) (((u32)(x)&0x07) == 0)
Wu, Bryana5f6abd2007-05-06 14:50:34 -070040
Bryan Wubb90eb02007-12-04 23:45:18 -080041#define START_STATE ((void *)0)
42#define RUNNING_STATE ((void *)1)
43#define DONE_STATE ((void *)2)
44#define ERROR_STATE ((void *)-1)
45#define QUEUE_RUNNING 0
46#define QUEUE_STOPPED 1
Wu, Bryana5f6abd2007-05-06 14:50:34 -070047
Wolfgang Muees93b61bd2009-04-06 19:00:53 -070048/* Value to send if no TX value is supplied */
49#define SPI_IDLE_TXVAL 0x0000
50
Wu, Bryana5f6abd2007-05-06 14:50:34 -070051struct driver_data {
52 /* Driver model hookup */
53 struct platform_device *pdev;
54
55 /* SPI framework hookup */
56 struct spi_master *master;
57
Bryan Wubb90eb02007-12-04 23:45:18 -080058 /* Regs base of SPI controller */
Bryan Wuf4521262007-12-04 23:45:22 -080059 void __iomem *regs_base;
Bryan Wubb90eb02007-12-04 23:45:18 -080060
Bryan Wu003d9222007-12-04 23:45:22 -080061 /* Pin request list */
62 u16 *pin_req;
63
Wu, Bryana5f6abd2007-05-06 14:50:34 -070064 /* BFIN hookup */
65 struct bfin5xx_spi_master *master_info;
66
67 /* Driver message queue */
68 struct workqueue_struct *workqueue;
69 struct work_struct pump_messages;
70 spinlock_t lock;
71 struct list_head queue;
72 int busy;
73 int run;
74
75 /* Message Transfer pump */
76 struct tasklet_struct pump_transfers;
77
78 /* Current message transfer state info */
79 struct spi_message *cur_msg;
80 struct spi_transfer *cur_transfer;
81 struct chip_data *cur_chip;
82 size_t len_in_bytes;
83 size_t len;
84 void *tx;
85 void *tx_end;
86 void *rx;
87 void *rx_end;
Bryan Wubb90eb02007-12-04 23:45:18 -080088
89 /* DMA stuffs */
90 int dma_channel;
Wu, Bryana5f6abd2007-05-06 14:50:34 -070091 int dma_mapped;
Bryan Wubb90eb02007-12-04 23:45:18 -080092 int dma_requested;
Wu, Bryana5f6abd2007-05-06 14:50:34 -070093 dma_addr_t rx_dma;
94 dma_addr_t tx_dma;
Bryan Wubb90eb02007-12-04 23:45:18 -080095
Wu, Bryana5f6abd2007-05-06 14:50:34 -070096 size_t rx_map_len;
97 size_t tx_map_len;
98 u8 n_bytes;
Bryan Wufad91c82007-12-04 23:45:14 -080099 int cs_change;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700100 void (*write) (struct driver_data *);
101 void (*read) (struct driver_data *);
102 void (*duplex) (struct driver_data *);
103};
104
105struct chip_data {
106 u16 ctl_reg;
107 u16 baud;
108 u16 flag;
109
110 u8 chip_select_num;
111 u8 n_bytes;
Bryan Wu88b40362007-05-21 18:32:16 +0800112 u8 width; /* 0 or 1 */
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700113 u8 enable_dma;
114 u8 bits_per_word; /* 8 or 16 */
115 u8 cs_change_per_word;
Bryan Wu62310e52007-12-04 23:45:20 -0800116 u16 cs_chg_udelay; /* Some devices require > 255usec delay */
Michael Hennerich42c78b22009-04-06 19:00:51 -0700117 u32 cs_gpio;
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700118 u16 idle_tx_val;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700119 void (*write) (struct driver_data *);
120 void (*read) (struct driver_data *);
121 void (*duplex) (struct driver_data *);
122};
123
Bryan Wubb90eb02007-12-04 23:45:18 -0800124#define DEFINE_SPI_REG(reg, off) \
125static inline u16 read_##reg(struct driver_data *drv_data) \
126 { return bfin_read16(drv_data->regs_base + off); } \
127static inline void write_##reg(struct driver_data *drv_data, u16 v) \
128 { bfin_write16(drv_data->regs_base + off, v); }
129
130DEFINE_SPI_REG(CTRL, 0x00)
131DEFINE_SPI_REG(FLAG, 0x04)
132DEFINE_SPI_REG(STAT, 0x08)
133DEFINE_SPI_REG(TDBR, 0x0C)
134DEFINE_SPI_REG(RDBR, 0x10)
135DEFINE_SPI_REG(BAUD, 0x14)
136DEFINE_SPI_REG(SHAW, 0x18)
137
Bryan Wu88b40362007-05-21 18:32:16 +0800138static void bfin_spi_enable(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700139{
140 u16 cr;
141
Bryan Wubb90eb02007-12-04 23:45:18 -0800142 cr = read_CTRL(drv_data);
143 write_CTRL(drv_data, (cr | BIT_CTL_ENABLE));
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700144}
145
Bryan Wu88b40362007-05-21 18:32:16 +0800146static void bfin_spi_disable(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700147{
148 u16 cr;
149
Bryan Wubb90eb02007-12-04 23:45:18 -0800150 cr = read_CTRL(drv_data);
151 write_CTRL(drv_data, (cr & (~BIT_CTL_ENABLE)));
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700152}
153
154/* Caculate the SPI_BAUD register value based on input HZ */
155static u16 hz_to_spi_baud(u32 speed_hz)
156{
157 u_long sclk = get_sclk();
158 u16 spi_baud = (sclk / (2 * speed_hz));
159
160 if ((sclk % (2 * speed_hz)) > 0)
161 spi_baud++;
162
Michael Hennerich7513e002009-04-06 19:00:32 -0700163 if (spi_baud < MIN_SPI_BAUD_VAL)
164 spi_baud = MIN_SPI_BAUD_VAL;
165
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700166 return spi_baud;
167}
168
Mike Frysinger138f97c2009-04-06 19:00:50 -0700169static int bfin_spi_flush(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700170{
171 unsigned long limit = loops_per_jiffy << 1;
172
173 /* wait for stop and clear stat */
Bryan Wubb90eb02007-12-04 23:45:18 -0800174 while (!(read_STAT(drv_data) & BIT_STAT_SPIF) && limit--)
Bryan Wud8c05002007-12-04 23:45:21 -0800175 cpu_relax();
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700176
Bryan Wubb90eb02007-12-04 23:45:18 -0800177 write_STAT(drv_data, BIT_STAT_CLR);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700178
179 return limit;
180}
181
Bryan Wufad91c82007-12-04 23:45:14 -0800182/* Chip select operation functions for cs_change flag */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700183static void bfin_spi_cs_active(struct driver_data *drv_data, struct chip_data *chip)
Bryan Wufad91c82007-12-04 23:45:14 -0800184{
Michael Hennerich42c78b22009-04-06 19:00:51 -0700185 if (likely(chip->chip_select_num)) {
186 u16 flag = read_FLAG(drv_data);
Bryan Wufad91c82007-12-04 23:45:14 -0800187
Michael Hennerich42c78b22009-04-06 19:00:51 -0700188 flag |= chip->flag;
189 flag &= ~(chip->flag << 8);
Bryan Wufad91c82007-12-04 23:45:14 -0800190
Michael Hennerich42c78b22009-04-06 19:00:51 -0700191 write_FLAG(drv_data, flag);
192 } else {
193 gpio_set_value(chip->cs_gpio, 0);
194 }
Bryan Wufad91c82007-12-04 23:45:14 -0800195}
196
Mike Frysinger138f97c2009-04-06 19:00:50 -0700197static void bfin_spi_cs_deactive(struct driver_data *drv_data, struct chip_data *chip)
Bryan Wufad91c82007-12-04 23:45:14 -0800198{
Michael Hennerich42c78b22009-04-06 19:00:51 -0700199 if (likely(chip->chip_select_num)) {
200 u16 flag = read_FLAG(drv_data);
Bryan Wufad91c82007-12-04 23:45:14 -0800201
Michael Hennerich42c78b22009-04-06 19:00:51 -0700202 flag &= ~chip->flag;
203 flag |= (chip->flag << 8);
Bryan Wufad91c82007-12-04 23:45:14 -0800204
Michael Hennerich42c78b22009-04-06 19:00:51 -0700205 write_FLAG(drv_data, flag);
206 } else {
207 gpio_set_value(chip->cs_gpio, 1);
208 }
Bryan Wu62310e52007-12-04 23:45:20 -0800209
210 /* Move delay here for consistency */
211 if (chip->cs_chg_udelay)
212 udelay(chip->cs_chg_udelay);
Bryan Wufad91c82007-12-04 23:45:14 -0800213}
214
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700215/* stop controller and re-config current chip*/
Mike Frysinger138f97c2009-04-06 19:00:50 -0700216static void bfin_spi_restore_state(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700217{
218 struct chip_data *chip = drv_data->cur_chip;
219
220 /* Clear status and disable clock */
Bryan Wubb90eb02007-12-04 23:45:18 -0800221 write_STAT(drv_data, BIT_STAT_CLR);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700222 bfin_spi_disable(drv_data);
Bryan Wu88b40362007-05-21 18:32:16 +0800223 dev_dbg(&drv_data->pdev->dev, "restoring spi ctl state\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700224
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700225 /* Load the registers */
Bryan Wubb90eb02007-12-04 23:45:18 -0800226 write_CTRL(drv_data, chip->ctl_reg);
Bryan Wu092e1fd2007-12-04 23:45:23 -0800227 write_BAUD(drv_data, chip->baud);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800228
229 bfin_spi_enable(drv_data);
Mike Frysinger138f97c2009-04-06 19:00:50 -0700230 bfin_spi_cs_active(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700231}
232
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700233/* used to kick off transfer in rx mode and read unwanted RX data */
234static inline void bfin_spi_dummy_read(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700235{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700236 (void) read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700237}
238
Mike Frysinger138f97c2009-04-06 19:00:50 -0700239static void bfin_spi_null_writer(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700240{
241 u8 n_bytes = drv_data->n_bytes;
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700242 u16 tx_val = drv_data->cur_chip->idle_tx_val;
243
244 /* clear RXS (we check for RXS inside the loop) */
245 bfin_spi_dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700246
247 while (drv_data->tx < drv_data->tx_end) {
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700248 write_TDBR(drv_data, tx_val);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700249 drv_data->tx += n_bytes;
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700250 /* wait until transfer finished.
251 checking SPIF or TXS may not guarantee transfer completion */
252 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
253 cpu_relax();
254 /* discard RX data and clear RXS */
255 bfin_spi_dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700256 }
257}
258
Mike Frysinger138f97c2009-04-06 19:00:50 -0700259static void bfin_spi_null_reader(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700260{
261 u8 n_bytes = drv_data->n_bytes;
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700262 u16 tx_val = drv_data->cur_chip->idle_tx_val;
263
264 /* discard old RX data and clear RXS */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700265 bfin_spi_dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700266
267 while (drv_data->rx < drv_data->rx_end) {
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700268 write_TDBR(drv_data, tx_val);
269 drv_data->rx += n_bytes;
Bryan Wubb90eb02007-12-04 23:45:18 -0800270 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800271 cpu_relax();
Mike Frysinger138f97c2009-04-06 19:00:50 -0700272 bfin_spi_dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700273 }
274}
275
Mike Frysinger138f97c2009-04-06 19:00:50 -0700276static void bfin_spi_u8_writer(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700277{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700278 /* clear RXS (we check for RXS inside the loop) */
279 bfin_spi_dummy_read(drv_data);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800280
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700281 while (drv_data->tx < drv_data->tx_end) {
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700282 write_TDBR(drv_data, (*(u8 *) (drv_data->tx++)));
283 /* wait until transfer finished.
284 checking SPIF or TXS may not guarantee transfer completion */
285 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800286 cpu_relax();
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700287 /* discard RX data and clear RXS */
288 bfin_spi_dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700289 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700290}
291
Mike Frysinger138f97c2009-04-06 19:00:50 -0700292static void bfin_spi_u8_cs_chg_writer(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700293{
294 struct chip_data *chip = drv_data->cur_chip;
295
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700296 /* clear RXS (we check for RXS inside the loop) */
297 bfin_spi_dummy_read(drv_data);
298
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700299 while (drv_data->tx < drv_data->tx_end) {
Mike Frysinger138f97c2009-04-06 19:00:50 -0700300 bfin_spi_cs_active(drv_data, chip);
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700301 write_TDBR(drv_data, (*(u8 *) (drv_data->tx++)));
302 /* make sure transfer finished before deactiving CS */
303 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800304 cpu_relax();
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700305 bfin_spi_dummy_read(drv_data);
Mike Frysinger138f97c2009-04-06 19:00:50 -0700306 bfin_spi_cs_deactive(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700307 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700308}
309
Mike Frysinger138f97c2009-04-06 19:00:50 -0700310static void bfin_spi_u8_reader(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700311{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700312 u16 tx_val = drv_data->cur_chip->idle_tx_val;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700313
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700314 /* discard old RX data and clear RXS */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700315 bfin_spi_dummy_read(drv_data);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800316
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700317 while (drv_data->rx < drv_data->rx_end) {
318 write_TDBR(drv_data, tx_val);
Bryan Wubb90eb02007-12-04 23:45:18 -0800319 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800320 cpu_relax();
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700321 *(u8 *) (drv_data->rx++) = read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700322 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700323}
324
Mike Frysinger138f97c2009-04-06 19:00:50 -0700325static void bfin_spi_u8_cs_chg_reader(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700326{
327 struct chip_data *chip = drv_data->cur_chip;
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700328 u16 tx_val = chip->idle_tx_val;
329
330 /* discard old RX data and clear RXS */
331 bfin_spi_dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700332
Bryan Wue26aa012008-02-06 01:38:18 -0800333 while (drv_data->rx < drv_data->rx_end) {
Mike Frysinger138f97c2009-04-06 19:00:50 -0700334 bfin_spi_cs_active(drv_data, chip);
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700335 write_TDBR(drv_data, tx_val);
Bryan Wubb90eb02007-12-04 23:45:18 -0800336 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800337 cpu_relax();
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700338 *(u8 *) (drv_data->rx++) = read_RDBR(drv_data);
Mike Frysinger138f97c2009-04-06 19:00:50 -0700339 bfin_spi_cs_deactive(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700340 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700341}
342
Mike Frysinger138f97c2009-04-06 19:00:50 -0700343static void bfin_spi_u8_duplex(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700344{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700345 /* discard old RX data and clear RXS */
346 bfin_spi_dummy_read(drv_data);
347
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700348 while (drv_data->rx < drv_data->rx_end) {
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700349 write_TDBR(drv_data, (*(u8 *) (drv_data->tx++)));
Bryan Wubb90eb02007-12-04 23:45:18 -0800350 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800351 cpu_relax();
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700352 *(u8 *) (drv_data->rx++) = read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700353 }
354}
355
Mike Frysinger138f97c2009-04-06 19:00:50 -0700356static void bfin_spi_u8_cs_chg_duplex(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700357{
358 struct chip_data *chip = drv_data->cur_chip;
359
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700360 /* discard old RX data and clear RXS */
361 bfin_spi_dummy_read(drv_data);
362
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700363 while (drv_data->rx < drv_data->rx_end) {
Mike Frysinger138f97c2009-04-06 19:00:50 -0700364 bfin_spi_cs_active(drv_data, chip);
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700365 write_TDBR(drv_data, (*(u8 *) (drv_data->tx++)));
Bryan Wubb90eb02007-12-04 23:45:18 -0800366 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800367 cpu_relax();
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700368 *(u8 *) (drv_data->rx++) = read_RDBR(drv_data);
Mike Frysinger138f97c2009-04-06 19:00:50 -0700369 bfin_spi_cs_deactive(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700370 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700371}
372
Mike Frysinger138f97c2009-04-06 19:00:50 -0700373static void bfin_spi_u16_writer(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700374{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700375 /* clear RXS (we check for RXS inside the loop) */
376 bfin_spi_dummy_read(drv_data);
Bryan Wu88b40362007-05-21 18:32:16 +0800377
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700378 while (drv_data->tx < drv_data->tx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800379 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700380 drv_data->tx += 2;
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700381 /* wait until transfer finished.
382 checking SPIF or TXS may not guarantee transfer completion */
383 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
384 cpu_relax();
385 /* discard RX data and clear RXS */
386 bfin_spi_dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700387 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700388}
389
Mike Frysinger138f97c2009-04-06 19:00:50 -0700390static void bfin_spi_u16_cs_chg_writer(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700391{
392 struct chip_data *chip = drv_data->cur_chip;
393
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700394 /* clear RXS (we check for RXS inside the loop) */
395 bfin_spi_dummy_read(drv_data);
396
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700397 while (drv_data->tx < drv_data->tx_end) {
Mike Frysinger138f97c2009-04-06 19:00:50 -0700398 bfin_spi_cs_active(drv_data, chip);
Bryan Wubb90eb02007-12-04 23:45:18 -0800399 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700400 drv_data->tx += 2;
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700401 /* make sure transfer finished before deactiving CS */
402 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
403 cpu_relax();
404 bfin_spi_dummy_read(drv_data);
405 bfin_spi_cs_deactive(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700406 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700407}
408
Mike Frysinger138f97c2009-04-06 19:00:50 -0700409static void bfin_spi_u16_reader(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700410{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700411 u16 tx_val = drv_data->cur_chip->idle_tx_val;
Sonic Zhangcc487e72007-12-04 23:45:17 -0800412
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700413 /* discard old RX data and clear RXS */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700414 bfin_spi_dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700415
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700416 while (drv_data->rx < drv_data->rx_end) {
417 write_TDBR(drv_data, tx_val);
Bryan Wubb90eb02007-12-04 23:45:18 -0800418 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800419 cpu_relax();
Bryan Wubb90eb02007-12-04 23:45:18 -0800420 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700421 drv_data->rx += 2;
422 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700423}
424
Mike Frysinger138f97c2009-04-06 19:00:50 -0700425static void bfin_spi_u16_cs_chg_reader(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700426{
427 struct chip_data *chip = drv_data->cur_chip;
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700428 u16 tx_val = chip->idle_tx_val;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700429
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700430 /* discard old RX data and clear RXS */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700431 bfin_spi_dummy_read(drv_data);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800432
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700433 while (drv_data->rx < drv_data->rx_end) {
434 bfin_spi_cs_active(drv_data, chip);
435 write_TDBR(drv_data, tx_val);
Bryan Wubb90eb02007-12-04 23:45:18 -0800436 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800437 cpu_relax();
Bryan Wubb90eb02007-12-04 23:45:18 -0800438 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700439 drv_data->rx += 2;
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700440 bfin_spi_cs_deactive(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700441 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700442}
443
Mike Frysinger138f97c2009-04-06 19:00:50 -0700444static void bfin_spi_u16_duplex(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700445{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700446 /* discard old RX data and clear RXS */
447 bfin_spi_dummy_read(drv_data);
448
449 while (drv_data->rx < drv_data->rx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800450 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700451 drv_data->tx += 2;
Bryan Wubb90eb02007-12-04 23:45:18 -0800452 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800453 cpu_relax();
Bryan Wubb90eb02007-12-04 23:45:18 -0800454 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700455 drv_data->rx += 2;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700456 }
457}
458
Mike Frysinger138f97c2009-04-06 19:00:50 -0700459static void bfin_spi_u16_cs_chg_duplex(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700460{
461 struct chip_data *chip = drv_data->cur_chip;
462
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700463 /* discard old RX data and clear RXS */
464 bfin_spi_dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700465
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700466 while (drv_data->rx < drv_data->rx_end) {
467 bfin_spi_cs_active(drv_data, chip);
Bryan Wubb90eb02007-12-04 23:45:18 -0800468 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700469 drv_data->tx += 2;
Bryan Wubb90eb02007-12-04 23:45:18 -0800470 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800471 cpu_relax();
Bryan Wubb90eb02007-12-04 23:45:18 -0800472 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700473 drv_data->rx += 2;
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700474 bfin_spi_cs_deactive(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700475 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700476}
477
478/* test if ther is more transfer to be done */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700479static void *bfin_spi_next_transfer(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700480{
481 struct spi_message *msg = drv_data->cur_msg;
482 struct spi_transfer *trans = drv_data->cur_transfer;
483
484 /* Move to next transfer */
485 if (trans->transfer_list.next != &msg->transfers) {
486 drv_data->cur_transfer =
487 list_entry(trans->transfer_list.next,
488 struct spi_transfer, transfer_list);
489 return RUNNING_STATE;
490 } else
491 return DONE_STATE;
492}
493
494/*
495 * caller already set message->status;
496 * dma and pio irqs are blocked give finished message back
497 */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700498static void bfin_spi_giveback(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700499{
Bryan Wufad91c82007-12-04 23:45:14 -0800500 struct chip_data *chip = drv_data->cur_chip;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700501 struct spi_transfer *last_transfer;
502 unsigned long flags;
503 struct spi_message *msg;
504
505 spin_lock_irqsave(&drv_data->lock, flags);
506 msg = drv_data->cur_msg;
507 drv_data->cur_msg = NULL;
508 drv_data->cur_transfer = NULL;
509 drv_data->cur_chip = NULL;
510 queue_work(drv_data->workqueue, &drv_data->pump_messages);
511 spin_unlock_irqrestore(&drv_data->lock, flags);
512
513 last_transfer = list_entry(msg->transfers.prev,
514 struct spi_transfer, transfer_list);
515
516 msg->state = NULL;
517
Bryan Wufad91c82007-12-04 23:45:14 -0800518 if (!drv_data->cs_change)
Mike Frysinger138f97c2009-04-06 19:00:50 -0700519 bfin_spi_cs_deactive(drv_data, chip);
Bryan Wufad91c82007-12-04 23:45:14 -0800520
Yi Lib9b2a762009-04-06 19:00:49 -0700521 /* Not stop spi in autobuffer mode */
522 if (drv_data->tx_dma != 0xFFFF)
523 bfin_spi_disable(drv_data);
524
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700525 if (msg->complete)
526 msg->complete(msg->context);
527}
528
Mike Frysinger138f97c2009-04-06 19:00:50 -0700529static irqreturn_t bfin_spi_dma_irq_handler(int irq, void *dev_id)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700530{
Jeff Garzik15aafa22008-02-06 01:36:20 -0800531 struct driver_data *drv_data = dev_id;
Bryan Wufad91c82007-12-04 23:45:14 -0800532 struct chip_data *chip = drv_data->cur_chip;
Bryan Wubb90eb02007-12-04 23:45:18 -0800533 struct spi_message *msg = drv_data->cur_msg;
Mike Frysingeraaaf9392009-04-06 19:00:42 -0700534 unsigned long timeout;
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700535 unsigned short dmastat = get_dma_curr_irqstat(drv_data->dma_channel);
Mike Frysinger04b95d22009-04-06 19:00:35 -0700536 u16 spistat = read_STAT(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700537
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700538 dev_dbg(&drv_data->pdev->dev,
539 "in dma_irq_handler dmastat:0x%x spistat:0x%x\n",
540 dmastat, spistat);
541
Bryan Wubb90eb02007-12-04 23:45:18 -0800542 clear_dma_irqstat(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700543
Bryan Wud6fe89b2007-06-11 17:34:17 +0800544 /* Wait for DMA to complete */
Bryan Wubb90eb02007-12-04 23:45:18 -0800545 while (get_dma_curr_irqstat(drv_data->dma_channel) & DMA_RUN)
Bryan Wud8c05002007-12-04 23:45:21 -0800546 cpu_relax();
Bryan Wud6fe89b2007-06-11 17:34:17 +0800547
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700548 /*
Bryan Wud6fe89b2007-06-11 17:34:17 +0800549 * wait for the last transaction shifted out. HRM states:
550 * at this point there may still be data in the SPI DMA FIFO waiting
551 * to be transmitted ... software needs to poll TXS in the SPI_STAT
552 * register until it goes low for 2 successive reads
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700553 */
554 if (drv_data->tx != NULL) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800555 while ((read_STAT(drv_data) & TXS) ||
556 (read_STAT(drv_data) & TXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800557 cpu_relax();
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700558 }
559
Mike Frysingeraaaf9392009-04-06 19:00:42 -0700560 dev_dbg(&drv_data->pdev->dev,
561 "in dma_irq_handler dmastat:0x%x spistat:0x%x\n",
562 dmastat, read_STAT(drv_data));
563
564 timeout = jiffies + HZ;
Bryan Wubb90eb02007-12-04 23:45:18 -0800565 while (!(read_STAT(drv_data) & SPIF))
Mike Frysingeraaaf9392009-04-06 19:00:42 -0700566 if (!time_before(jiffies, timeout)) {
567 dev_warn(&drv_data->pdev->dev, "timeout waiting for SPIF");
568 break;
569 } else
570 cpu_relax();
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700571
Mike Frysinger40a29452009-04-06 19:00:38 -0700572 if ((dmastat & DMA_ERR) && (spistat & RBSY)) {
Mike Frysinger04b95d22009-04-06 19:00:35 -0700573 msg->state = ERROR_STATE;
574 dev_err(&drv_data->pdev->dev, "dma receive: fifo/buffer overflow\n");
575 } else {
576 msg->actual_length += drv_data->len_in_bytes;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700577
Mike Frysinger04b95d22009-04-06 19:00:35 -0700578 if (drv_data->cs_change)
Mike Frysinger138f97c2009-04-06 19:00:50 -0700579 bfin_spi_cs_deactive(drv_data, chip);
Bryan Wufad91c82007-12-04 23:45:14 -0800580
Mike Frysinger04b95d22009-04-06 19:00:35 -0700581 /* Move to next transfer */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700582 msg->state = bfin_spi_next_transfer(drv_data);
Mike Frysinger04b95d22009-04-06 19:00:35 -0700583 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700584
585 /* Schedule transfer tasklet */
586 tasklet_schedule(&drv_data->pump_transfers);
587
588 /* free the irq handler before next transfer */
Bryan Wu88b40362007-05-21 18:32:16 +0800589 dev_dbg(&drv_data->pdev->dev,
590 "disable dma channel irq%d\n",
Bryan Wubb90eb02007-12-04 23:45:18 -0800591 drv_data->dma_channel);
592 dma_disable_irq(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700593
594 return IRQ_HANDLED;
595}
596
Mike Frysinger138f97c2009-04-06 19:00:50 -0700597static void bfin_spi_pump_transfers(unsigned long data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700598{
599 struct driver_data *drv_data = (struct driver_data *)data;
600 struct spi_message *message = NULL;
601 struct spi_transfer *transfer = NULL;
602 struct spi_transfer *previous = NULL;
603 struct chip_data *chip = NULL;
Bryan Wu88b40362007-05-21 18:32:16 +0800604 u8 width;
605 u16 cr, dma_width, dma_config;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700606 u32 tranf_success = 1;
Vitja Makarov8eeb12e2008-05-01 04:35:03 -0700607 u8 full_duplex = 0;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700608
609 /* Get current state information */
610 message = drv_data->cur_msg;
611 transfer = drv_data->cur_transfer;
612 chip = drv_data->cur_chip;
Bryan Wu092e1fd2007-12-04 23:45:23 -0800613
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700614 /*
615 * if msg is error or done, report it back using complete() callback
616 */
617
618 /* Handle for abort */
619 if (message->state == ERROR_STATE) {
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700620 dev_dbg(&drv_data->pdev->dev, "transfer: we've hit an error\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700621 message->status = -EIO;
Mike Frysinger138f97c2009-04-06 19:00:50 -0700622 bfin_spi_giveback(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700623 return;
624 }
625
626 /* Handle end of message */
627 if (message->state == DONE_STATE) {
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700628 dev_dbg(&drv_data->pdev->dev, "transfer: all done!\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700629 message->status = 0;
Mike Frysinger138f97c2009-04-06 19:00:50 -0700630 bfin_spi_giveback(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700631 return;
632 }
633
634 /* Delay if requested at end of transfer */
635 if (message->state == RUNNING_STATE) {
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700636 dev_dbg(&drv_data->pdev->dev, "transfer: still running ...\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700637 previous = list_entry(transfer->transfer_list.prev,
638 struct spi_transfer, transfer_list);
639 if (previous->delay_usecs)
640 udelay(previous->delay_usecs);
641 }
642
643 /* Setup the transfer state based on the type of transfer */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700644 if (bfin_spi_flush(drv_data) == 0) {
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700645 dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
646 message->status = -EIO;
Mike Frysinger138f97c2009-04-06 19:00:50 -0700647 bfin_spi_giveback(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700648 return;
649 }
650
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700651 if (transfer->len == 0) {
652 /* Move to next transfer of this msg */
653 message->state = bfin_spi_next_transfer(drv_data);
654 /* Schedule next transfer tasklet */
655 tasklet_schedule(&drv_data->pump_transfers);
656 }
657
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700658 if (transfer->tx_buf != NULL) {
659 drv_data->tx = (void *)transfer->tx_buf;
660 drv_data->tx_end = drv_data->tx + transfer->len;
Bryan Wu88b40362007-05-21 18:32:16 +0800661 dev_dbg(&drv_data->pdev->dev, "tx_buf is %p, tx_end is %p\n",
662 transfer->tx_buf, drv_data->tx_end);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700663 } else {
664 drv_data->tx = NULL;
665 }
666
667 if (transfer->rx_buf != NULL) {
Vitja Makarov8eeb12e2008-05-01 04:35:03 -0700668 full_duplex = transfer->tx_buf != NULL;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700669 drv_data->rx = transfer->rx_buf;
670 drv_data->rx_end = drv_data->rx + transfer->len;
Bryan Wu88b40362007-05-21 18:32:16 +0800671 dev_dbg(&drv_data->pdev->dev, "rx_buf is %p, rx_end is %p\n",
672 transfer->rx_buf, drv_data->rx_end);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700673 } else {
674 drv_data->rx = NULL;
675 }
676
677 drv_data->rx_dma = transfer->rx_dma;
678 drv_data->tx_dma = transfer->tx_dma;
679 drv_data->len_in_bytes = transfer->len;
Bryan Wufad91c82007-12-04 23:45:14 -0800680 drv_data->cs_change = transfer->cs_change;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700681
Bryan Wu092e1fd2007-12-04 23:45:23 -0800682 /* Bits per word setup */
683 switch (transfer->bits_per_word) {
684 case 8:
685 drv_data->n_bytes = 1;
686 width = CFG_SPI_WORDSIZE8;
687 drv_data->read = chip->cs_change_per_word ?
Mike Frysinger138f97c2009-04-06 19:00:50 -0700688 bfin_spi_u8_cs_chg_reader : bfin_spi_u8_reader;
Bryan Wu092e1fd2007-12-04 23:45:23 -0800689 drv_data->write = chip->cs_change_per_word ?
Mike Frysinger138f97c2009-04-06 19:00:50 -0700690 bfin_spi_u8_cs_chg_writer : bfin_spi_u8_writer;
Bryan Wu092e1fd2007-12-04 23:45:23 -0800691 drv_data->duplex = chip->cs_change_per_word ?
Mike Frysinger138f97c2009-04-06 19:00:50 -0700692 bfin_spi_u8_cs_chg_duplex : bfin_spi_u8_duplex;
Bryan Wu092e1fd2007-12-04 23:45:23 -0800693 break;
694
695 case 16:
696 drv_data->n_bytes = 2;
697 width = CFG_SPI_WORDSIZE16;
698 drv_data->read = chip->cs_change_per_word ?
Mike Frysinger138f97c2009-04-06 19:00:50 -0700699 bfin_spi_u16_cs_chg_reader : bfin_spi_u16_reader;
Bryan Wu092e1fd2007-12-04 23:45:23 -0800700 drv_data->write = chip->cs_change_per_word ?
Mike Frysinger138f97c2009-04-06 19:00:50 -0700701 bfin_spi_u16_cs_chg_writer : bfin_spi_u16_writer;
Bryan Wu092e1fd2007-12-04 23:45:23 -0800702 drv_data->duplex = chip->cs_change_per_word ?
Mike Frysinger138f97c2009-04-06 19:00:50 -0700703 bfin_spi_u16_cs_chg_duplex : bfin_spi_u16_duplex;
Bryan Wu092e1fd2007-12-04 23:45:23 -0800704 break;
705
706 default:
707 /* No change, the same as default setting */
708 drv_data->n_bytes = chip->n_bytes;
709 width = chip->width;
Mike Frysinger138f97c2009-04-06 19:00:50 -0700710 drv_data->write = drv_data->tx ? chip->write : bfin_spi_null_writer;
711 drv_data->read = drv_data->rx ? chip->read : bfin_spi_null_reader;
712 drv_data->duplex = chip->duplex ? chip->duplex : bfin_spi_null_writer;
Bryan Wu092e1fd2007-12-04 23:45:23 -0800713 break;
714 }
715 cr = (read_CTRL(drv_data) & (~BIT_CTL_TIMOD));
716 cr |= (width << 8);
717 write_CTRL(drv_data, cr);
718
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700719 if (width == CFG_SPI_WORDSIZE16) {
720 drv_data->len = (transfer->len) >> 1;
721 } else {
722 drv_data->len = transfer->len;
723 }
Mike Frysinger4fb98ef2008-04-08 17:41:57 -0700724 dev_dbg(&drv_data->pdev->dev,
725 "transfer: drv_data->write is %p, chip->write is %p, null_wr is %p\n",
Mike Frysinger138f97c2009-04-06 19:00:50 -0700726 drv_data->write, chip->write, bfin_spi_null_writer);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700727
728 /* speed and width has been set on per message */
729 message->state = RUNNING_STATE;
730 dma_config = 0;
731
Bryan Wu092e1fd2007-12-04 23:45:23 -0800732 /* Speed setup (surely valid because already checked) */
733 if (transfer->speed_hz)
734 write_BAUD(drv_data, hz_to_spi_baud(transfer->speed_hz));
735 else
736 write_BAUD(drv_data, chip->baud);
737
Bryan Wubb90eb02007-12-04 23:45:18 -0800738 write_STAT(drv_data, BIT_STAT_CLR);
739 cr = (read_CTRL(drv_data) & (~BIT_CTL_TIMOD));
Yi Lib9b2a762009-04-06 19:00:49 -0700740 if (drv_data->cs_change)
Mike Frysinger138f97c2009-04-06 19:00:50 -0700741 bfin_spi_cs_active(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700742
Bryan Wu88b40362007-05-21 18:32:16 +0800743 dev_dbg(&drv_data->pdev->dev,
744 "now pumping a transfer: width is %d, len is %d\n",
745 width, transfer->len);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700746
747 /*
Vitja Makarov8cf58582009-04-06 19:00:31 -0700748 * Try to map dma buffer and do a dma transfer. If successful use,
749 * different way to r/w according to the enable_dma settings and if
750 * we are not doing a full duplex transfer (since the hardware does
751 * not support full duplex DMA transfers).
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700752 */
Vitja Makarov8eeb12e2008-05-01 04:35:03 -0700753 if (!full_duplex && drv_data->cur_chip->enable_dma
754 && drv_data->len > 6) {
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700755
Mike Frysinger11d6f592009-04-06 19:00:41 -0700756 unsigned long dma_start_addr, flags;
Mike Frysinger7aec3562009-04-06 19:00:36 -0700757
Bryan Wubb90eb02007-12-04 23:45:18 -0800758 disable_dma(drv_data->dma_channel);
759 clear_dma_irqstat(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700760
761 /* config dma channel */
Bryan Wu88b40362007-05-21 18:32:16 +0800762 dev_dbg(&drv_data->pdev->dev, "doing dma transfer\n");
Mike Frysinger7aec3562009-04-06 19:00:36 -0700763 set_dma_x_count(drv_data->dma_channel, drv_data->len);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700764 if (width == CFG_SPI_WORDSIZE16) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800765 set_dma_x_modify(drv_data->dma_channel, 2);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700766 dma_width = WDSIZE_16;
767 } else {
Bryan Wubb90eb02007-12-04 23:45:18 -0800768 set_dma_x_modify(drv_data->dma_channel, 1);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700769 dma_width = WDSIZE_8;
770 }
771
Sonic Zhang3f479a62007-12-04 23:45:18 -0800772 /* poll for SPI completion before start */
Bryan Wubb90eb02007-12-04 23:45:18 -0800773 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Bryan Wud8c05002007-12-04 23:45:21 -0800774 cpu_relax();
Sonic Zhang3f479a62007-12-04 23:45:18 -0800775
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700776 /* dirty hack for autobuffer DMA mode */
777 if (drv_data->tx_dma == 0xFFFF) {
Bryan Wu88b40362007-05-21 18:32:16 +0800778 dev_dbg(&drv_data->pdev->dev,
779 "doing autobuffer DMA out.\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700780
781 /* no irq in autobuffer mode */
782 dma_config =
783 (DMAFLOW_AUTO | RESTART | dma_width | DI_EN);
Bryan Wubb90eb02007-12-04 23:45:18 -0800784 set_dma_config(drv_data->dma_channel, dma_config);
785 set_dma_start_addr(drv_data->dma_channel,
Bryan Wua32c6912007-12-04 23:45:15 -0800786 (unsigned long)drv_data->tx);
Bryan Wubb90eb02007-12-04 23:45:18 -0800787 enable_dma(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700788
Sonic Zhang07612e52007-12-04 23:45:21 -0800789 /* start SPI transfer */
Mike Frysinger11d6f592009-04-06 19:00:41 -0700790 write_CTRL(drv_data, cr | BIT_CTL_TIMOD_DMA_TX);
Sonic Zhang07612e52007-12-04 23:45:21 -0800791
792 /* just return here, there can only be one transfer
793 * in this mode
794 */
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700795 message->status = 0;
Mike Frysinger138f97c2009-04-06 19:00:50 -0700796 bfin_spi_giveback(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700797 return;
798 }
799
800 /* In dma mode, rx or tx must be NULL in one transfer */
Mike Frysinger7aec3562009-04-06 19:00:36 -0700801 dma_config = (RESTART | dma_width | DI_EN);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700802 if (drv_data->rx != NULL) {
803 /* set transfer mode, and enable SPI */
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700804 dev_dbg(&drv_data->pdev->dev, "doing DMA in to %p (size %zx)\n",
805 drv_data->rx, drv_data->len_in_bytes);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700806
Vitja Makarov8cf58582009-04-06 19:00:31 -0700807 /* invalidate caches, if needed */
808 if (bfin_addr_dcachable((unsigned long) drv_data->rx))
809 invalidate_dcache_range((unsigned long) drv_data->rx,
810 (unsigned long) (drv_data->rx +
Mike Frysingerace32862009-04-06 19:00:34 -0700811 drv_data->len_in_bytes));
Vitja Makarov8cf58582009-04-06 19:00:31 -0700812
Mike Frysinger7aec3562009-04-06 19:00:36 -0700813 dma_config |= WNR;
814 dma_start_addr = (unsigned long)drv_data->rx;
Mike Frysingerb31e27a2009-04-06 19:00:39 -0700815 cr |= BIT_CTL_TIMOD_DMA_RX | BIT_CTL_SENDOPT;
Sonic Zhang07612e52007-12-04 23:45:21 -0800816
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700817 } else if (drv_data->tx != NULL) {
Bryan Wu88b40362007-05-21 18:32:16 +0800818 dev_dbg(&drv_data->pdev->dev, "doing DMA out.\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700819
Vitja Makarov8cf58582009-04-06 19:00:31 -0700820 /* flush caches, if needed */
821 if (bfin_addr_dcachable((unsigned long) drv_data->tx))
822 flush_dcache_range((unsigned long) drv_data->tx,
823 (unsigned long) (drv_data->tx +
Mike Frysingerace32862009-04-06 19:00:34 -0700824 drv_data->len_in_bytes));
Vitja Makarov8cf58582009-04-06 19:00:31 -0700825
Mike Frysinger7aec3562009-04-06 19:00:36 -0700826 dma_start_addr = (unsigned long)drv_data->tx;
Mike Frysingerb31e27a2009-04-06 19:00:39 -0700827 cr |= BIT_CTL_TIMOD_DMA_TX;
Sonic Zhang07612e52007-12-04 23:45:21 -0800828
Mike Frysinger7aec3562009-04-06 19:00:36 -0700829 } else
830 BUG();
831
Mike Frysinger11d6f592009-04-06 19:00:41 -0700832 /* oh man, here there be monsters ... and i dont mean the
833 * fluffy cute ones from pixar, i mean the kind that'll eat
834 * your data, kick your dog, and love it all. do *not* try
835 * and change these lines unless you (1) heavily test DMA
836 * with SPI flashes on a loaded system (e.g. ping floods),
837 * (2) know just how broken the DMA engine interaction with
838 * the SPI peripheral is, and (3) have someone else to blame
839 * when you screw it all up anyways.
840 */
Mike Frysinger7aec3562009-04-06 19:00:36 -0700841 set_dma_start_addr(drv_data->dma_channel, dma_start_addr);
Mike Frysinger11d6f592009-04-06 19:00:41 -0700842 set_dma_config(drv_data->dma_channel, dma_config);
843 local_irq_save(flags);
Mike Frysingera963ea82009-04-06 19:00:43 -0700844 SSYNC();
Mike Frysinger11d6f592009-04-06 19:00:41 -0700845 write_CTRL(drv_data, cr);
Mike Frysingera963ea82009-04-06 19:00:43 -0700846 enable_dma(drv_data->dma_channel);
Mike Frysinger11d6f592009-04-06 19:00:41 -0700847 dma_enable_irq(drv_data->dma_channel);
848 local_irq_restore(flags);
Mike Frysinger7aec3562009-04-06 19:00:36 -0700849
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700850 } else {
851 /* IO mode write then read */
Bryan Wu88b40362007-05-21 18:32:16 +0800852 dev_dbg(&drv_data->pdev->dev, "doing IO transfer\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700853
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700854 /* we always use SPI_WRITE mode. SPI_READ mode
855 seems to have problems with setting up the
856 output value in TDBR prior to the transfer. */
857 write_CTRL(drv_data, (cr | CFG_SPI_WRITE));
858
Vitja Makarov8eeb12e2008-05-01 04:35:03 -0700859 if (full_duplex) {
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700860 /* full duplex mode */
861 BUG_ON((drv_data->tx_end - drv_data->tx) !=
862 (drv_data->rx_end - drv_data->rx));
Bryan Wu88b40362007-05-21 18:32:16 +0800863 dev_dbg(&drv_data->pdev->dev,
864 "IO duplex: cr is 0x%x\n", cr);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700865
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700866 drv_data->duplex(drv_data);
867
868 if (drv_data->tx != drv_data->tx_end)
869 tranf_success = 0;
870 } else if (drv_data->tx != NULL) {
871 /* write only half duplex */
Bryan Wu131b17d2007-12-04 23:45:12 -0800872 dev_dbg(&drv_data->pdev->dev,
Bryan Wu88b40362007-05-21 18:32:16 +0800873 "IO write: cr is 0x%x\n", cr);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700874
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700875 drv_data->write(drv_data);
876
877 if (drv_data->tx != drv_data->tx_end)
878 tranf_success = 0;
879 } else if (drv_data->rx != NULL) {
880 /* read only half duplex */
Bryan Wu131b17d2007-12-04 23:45:12 -0800881 dev_dbg(&drv_data->pdev->dev,
Bryan Wu88b40362007-05-21 18:32:16 +0800882 "IO read: cr is 0x%x\n", cr);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700883
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700884 drv_data->read(drv_data);
885 if (drv_data->rx != drv_data->rx_end)
886 tranf_success = 0;
887 }
888
889 if (!tranf_success) {
Bryan Wu131b17d2007-12-04 23:45:12 -0800890 dev_dbg(&drv_data->pdev->dev,
Bryan Wu88b40362007-05-21 18:32:16 +0800891 "IO write error!\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700892 message->state = ERROR_STATE;
893 } else {
894 /* Update total byte transfered */
Mike Frysingerace32862009-04-06 19:00:34 -0700895 message->actual_length += drv_data->len_in_bytes;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700896 /* Move to next transfer of this msg */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700897 message->state = bfin_spi_next_transfer(drv_data);
Yi Lib9b2a762009-04-06 19:00:49 -0700898 if (drv_data->cs_change)
Mike Frysinger138f97c2009-04-06 19:00:50 -0700899 bfin_spi_cs_deactive(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700900 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700901 /* Schedule next transfer tasklet */
902 tasklet_schedule(&drv_data->pump_transfers);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700903 }
904}
905
906/* pop a msg from queue and kick off real transfer */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700907static void bfin_spi_pump_messages(struct work_struct *work)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700908{
Bryan Wu131b17d2007-12-04 23:45:12 -0800909 struct driver_data *drv_data;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700910 unsigned long flags;
911
Bryan Wu131b17d2007-12-04 23:45:12 -0800912 drv_data = container_of(work, struct driver_data, pump_messages);
913
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700914 /* Lock queue and check for queue work */
915 spin_lock_irqsave(&drv_data->lock, flags);
916 if (list_empty(&drv_data->queue) || drv_data->run == QUEUE_STOPPED) {
917 /* pumper kicked off but no work to do */
918 drv_data->busy = 0;
919 spin_unlock_irqrestore(&drv_data->lock, flags);
920 return;
921 }
922
923 /* Make sure we are not already running a message */
924 if (drv_data->cur_msg) {
925 spin_unlock_irqrestore(&drv_data->lock, flags);
926 return;
927 }
928
929 /* Extract head of queue */
930 drv_data->cur_msg = list_entry(drv_data->queue.next,
931 struct spi_message, queue);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800932
933 /* Setup the SSP using the per chip configuration */
934 drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
Mike Frysinger138f97c2009-04-06 19:00:50 -0700935 bfin_spi_restore_state(drv_data);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800936
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700937 list_del_init(&drv_data->cur_msg->queue);
938
939 /* Initial message state */
940 drv_data->cur_msg->state = START_STATE;
941 drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
942 struct spi_transfer, transfer_list);
943
Bryan Wu5fec5b52007-12-04 23:45:13 -0800944 dev_dbg(&drv_data->pdev->dev, "got a message to pump, "
945 "state is set to: baud %d, flag 0x%x, ctl 0x%x\n",
946 drv_data->cur_chip->baud, drv_data->cur_chip->flag,
947 drv_data->cur_chip->ctl_reg);
Bryan Wu131b17d2007-12-04 23:45:12 -0800948
949 dev_dbg(&drv_data->pdev->dev,
Bryan Wu88b40362007-05-21 18:32:16 +0800950 "the first transfer len is %d\n",
951 drv_data->cur_transfer->len);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700952
953 /* Mark as busy and launch transfers */
954 tasklet_schedule(&drv_data->pump_transfers);
955
956 drv_data->busy = 1;
957 spin_unlock_irqrestore(&drv_data->lock, flags);
958}
959
960/*
961 * got a msg to transfer, queue it in drv_data->queue.
962 * And kick off message pumper
963 */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700964static int bfin_spi_transfer(struct spi_device *spi, struct spi_message *msg)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700965{
966 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
967 unsigned long flags;
968
969 spin_lock_irqsave(&drv_data->lock, flags);
970
971 if (drv_data->run == QUEUE_STOPPED) {
972 spin_unlock_irqrestore(&drv_data->lock, flags);
973 return -ESHUTDOWN;
974 }
975
976 msg->actual_length = 0;
977 msg->status = -EINPROGRESS;
978 msg->state = START_STATE;
979
Bryan Wu88b40362007-05-21 18:32:16 +0800980 dev_dbg(&spi->dev, "adding an msg in transfer() \n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700981 list_add_tail(&msg->queue, &drv_data->queue);
982
983 if (drv_data->run == QUEUE_RUNNING && !drv_data->busy)
984 queue_work(drv_data->workqueue, &drv_data->pump_messages);
985
986 spin_unlock_irqrestore(&drv_data->lock, flags);
987
988 return 0;
989}
990
Sonic Zhang12e17c42007-12-04 23:45:16 -0800991#define MAX_SPI_SSEL 7
992
Mike Frysinger4160bde2009-04-06 19:00:40 -0700993static u16 ssel[][MAX_SPI_SSEL] = {
Sonic Zhang12e17c42007-12-04 23:45:16 -0800994 {P_SPI0_SSEL1, P_SPI0_SSEL2, P_SPI0_SSEL3,
995 P_SPI0_SSEL4, P_SPI0_SSEL5,
996 P_SPI0_SSEL6, P_SPI0_SSEL7},
997
998 {P_SPI1_SSEL1, P_SPI1_SSEL2, P_SPI1_SSEL3,
999 P_SPI1_SSEL4, P_SPI1_SSEL5,
1000 P_SPI1_SSEL6, P_SPI1_SSEL7},
1001
1002 {P_SPI2_SSEL1, P_SPI2_SSEL2, P_SPI2_SSEL3,
1003 P_SPI2_SSEL4, P_SPI2_SSEL5,
1004 P_SPI2_SSEL6, P_SPI2_SSEL7},
1005};
1006
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001007/* first setup for new devices */
Mike Frysinger138f97c2009-04-06 19:00:50 -07001008static int bfin_spi_setup(struct spi_device *spi)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001009{
1010 struct bfin5xx_spi_chip *chip_info = NULL;
1011 struct chip_data *chip;
1012 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
Michael Hennerich42c78b22009-04-06 19:00:51 -07001013 int ret;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001014
1015 /* Abort device setup if requested features are not supported */
1016 if (spi->mode & ~(SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST)) {
1017 dev_err(&spi->dev, "requested mode not fully supported\n");
1018 return -EINVAL;
1019 }
1020
1021 /* Zero (the default) here means 8 bits */
1022 if (!spi->bits_per_word)
1023 spi->bits_per_word = 8;
1024
1025 if (spi->bits_per_word != 8 && spi->bits_per_word != 16)
1026 return -EINVAL;
1027
1028 /* Only alloc (or use chip_info) on first setup */
1029 chip = spi_get_ctldata(spi);
1030 if (chip == NULL) {
1031 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
1032 if (!chip)
1033 return -ENOMEM;
1034
1035 chip->enable_dma = 0;
1036 chip_info = spi->controller_data;
1037 }
1038
1039 /* chip_info isn't always needed */
1040 if (chip_info) {
Mike Frysinger2ed35512007-12-04 23:45:14 -08001041 /* Make sure people stop trying to set fields via ctl_reg
1042 * when they should actually be using common SPI framework.
1043 * Currently we let through: WOM EMISO PSSE GM SZ TIMOD.
1044 * Not sure if a user actually needs/uses any of these,
1045 * but let's assume (for now) they do.
1046 */
1047 if (chip_info->ctl_reg & (SPE|MSTR|CPOL|CPHA|LSBF|SIZE)) {
1048 dev_err(&spi->dev, "do not set bits in ctl_reg "
1049 "that the SPI framework manages\n");
1050 return -EINVAL;
1051 }
1052
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001053 chip->enable_dma = chip_info->enable_dma != 0
1054 && drv_data->master_info->enable_dma;
1055 chip->ctl_reg = chip_info->ctl_reg;
1056 chip->bits_per_word = chip_info->bits_per_word;
1057 chip->cs_change_per_word = chip_info->cs_change_per_word;
1058 chip->cs_chg_udelay = chip_info->cs_chg_udelay;
Michael Hennerich42c78b22009-04-06 19:00:51 -07001059 chip->cs_gpio = chip_info->cs_gpio;
Wolfgang Muees93b61bd2009-04-06 19:00:53 -07001060 chip->idle_tx_val = chip_info->idle_tx_val;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001061 }
1062
1063 /* translate common spi framework into our register */
1064 if (spi->mode & SPI_CPOL)
1065 chip->ctl_reg |= CPOL;
1066 if (spi->mode & SPI_CPHA)
1067 chip->ctl_reg |= CPHA;
1068 if (spi->mode & SPI_LSB_FIRST)
1069 chip->ctl_reg |= LSBF;
1070 /* we dont support running in slave mode (yet?) */
1071 chip->ctl_reg |= MSTR;
1072
1073 /*
1074 * if any one SPI chip is registered and wants DMA, request the
1075 * DMA channel for it
1076 */
Bryan Wubb90eb02007-12-04 23:45:18 -08001077 if (chip->enable_dma && !drv_data->dma_requested) {
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001078 /* register dma irq handler */
Mike Frysinger59bfcc62009-04-06 19:00:37 -07001079 if (request_dma(drv_data->dma_channel, "BFIN_SPI_DMA") < 0) {
Bryan Wu88b40362007-05-21 18:32:16 +08001080 dev_dbg(&spi->dev,
1081 "Unable to request BlackFin SPI DMA channel\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001082 return -ENODEV;
1083 }
Bryan Wubb90eb02007-12-04 23:45:18 -08001084 if (set_dma_callback(drv_data->dma_channel,
Mike Frysinger138f97c2009-04-06 19:00:50 -07001085 bfin_spi_dma_irq_handler, drv_data) < 0) {
Bryan Wu88b40362007-05-21 18:32:16 +08001086 dev_dbg(&spi->dev, "Unable to set dma callback\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001087 return -EPERM;
1088 }
Bryan Wubb90eb02007-12-04 23:45:18 -08001089 dma_disable_irq(drv_data->dma_channel);
1090 drv_data->dma_requested = 1;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001091 }
1092
1093 /*
1094 * Notice: for blackfin, the speed_hz is the value of register
1095 * SPI_BAUD, not the real baudrate
1096 */
1097 chip->baud = hz_to_spi_baud(spi->max_speed_hz);
Yi Li2cf36832009-04-06 19:00:44 -07001098 chip->flag = 1 << (spi->chip_select);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001099 chip->chip_select_num = spi->chip_select;
1100
Michael Hennerich42c78b22009-04-06 19:00:51 -07001101 if (chip->chip_select_num == 0) {
1102 ret = gpio_request(chip->cs_gpio, spi->modalias);
1103 if (ret) {
1104 if (drv_data->dma_requested)
1105 free_dma(drv_data->dma_channel);
1106 return ret;
1107 }
1108 gpio_direction_output(chip->cs_gpio, 1);
1109 }
1110
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001111 switch (chip->bits_per_word) {
1112 case 8:
1113 chip->n_bytes = 1;
1114 chip->width = CFG_SPI_WORDSIZE8;
1115 chip->read = chip->cs_change_per_word ?
Mike Frysinger138f97c2009-04-06 19:00:50 -07001116 bfin_spi_u8_cs_chg_reader : bfin_spi_u8_reader;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001117 chip->write = chip->cs_change_per_word ?
Mike Frysinger138f97c2009-04-06 19:00:50 -07001118 bfin_spi_u8_cs_chg_writer : bfin_spi_u8_writer;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001119 chip->duplex = chip->cs_change_per_word ?
Mike Frysinger138f97c2009-04-06 19:00:50 -07001120 bfin_spi_u8_cs_chg_duplex : bfin_spi_u8_duplex;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001121 break;
1122
1123 case 16:
1124 chip->n_bytes = 2;
1125 chip->width = CFG_SPI_WORDSIZE16;
1126 chip->read = chip->cs_change_per_word ?
Mike Frysinger138f97c2009-04-06 19:00:50 -07001127 bfin_spi_u16_cs_chg_reader : bfin_spi_u16_reader;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001128 chip->write = chip->cs_change_per_word ?
Mike Frysinger138f97c2009-04-06 19:00:50 -07001129 bfin_spi_u16_cs_chg_writer : bfin_spi_u16_writer;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001130 chip->duplex = chip->cs_change_per_word ?
Mike Frysinger138f97c2009-04-06 19:00:50 -07001131 bfin_spi_u16_cs_chg_duplex : bfin_spi_u16_duplex;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001132 break;
1133
1134 default:
1135 dev_err(&spi->dev, "%d bits_per_word is not supported\n",
1136 chip->bits_per_word);
Mike Frysinger138f97c2009-04-06 19:00:50 -07001137 if (chip_info)
1138 kfree(chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001139 return -ENODEV;
1140 }
1141
Joe Perches898eb712007-10-18 03:06:30 -07001142 dev_dbg(&spi->dev, "setup spi chip %s, width is %d, dma is %d\n",
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001143 spi->modalias, chip->width, chip->enable_dma);
Bryan Wu88b40362007-05-21 18:32:16 +08001144 dev_dbg(&spi->dev, "ctl_reg is 0x%x, flag_reg is 0x%x\n",
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001145 chip->ctl_reg, chip->flag);
1146
1147 spi_set_ctldata(spi, chip);
1148
Sonic Zhang12e17c42007-12-04 23:45:16 -08001149 dev_dbg(&spi->dev, "chip select number is %d\n", chip->chip_select_num);
1150 if ((chip->chip_select_num > 0)
1151 && (chip->chip_select_num <= spi->master->num_chipselect))
1152 peripheral_request(ssel[spi->master->bus_num]
Bryan Wuaab0d832008-02-06 01:38:17 -08001153 [chip->chip_select_num-1], spi->modalias);
Sonic Zhang12e17c42007-12-04 23:45:16 -08001154
Mike Frysinger138f97c2009-04-06 19:00:50 -07001155 bfin_spi_cs_deactive(drv_data, chip);
Sonic Zhang07612e52007-12-04 23:45:21 -08001156
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001157 return 0;
1158}
1159
1160/*
1161 * callback for spi framework.
1162 * clean driver specific data
1163 */
Mike Frysinger138f97c2009-04-06 19:00:50 -07001164static void bfin_spi_cleanup(struct spi_device *spi)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001165{
Mike Frysinger27bb9e72007-06-11 15:31:30 +08001166 struct chip_data *chip = spi_get_ctldata(spi);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001167
Mike Frysingere7d02e32009-04-06 19:00:51 -07001168 if (!chip)
1169 return;
1170
Sonic Zhang12e17c42007-12-04 23:45:16 -08001171 if ((chip->chip_select_num > 0)
1172 && (chip->chip_select_num <= spi->master->num_chipselect))
1173 peripheral_free(ssel[spi->master->bus_num]
1174 [chip->chip_select_num-1]);
1175
Michael Hennerich42c78b22009-04-06 19:00:51 -07001176 if (chip->chip_select_num == 0)
1177 gpio_free(chip->cs_gpio);
1178
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001179 kfree(chip);
1180}
1181
Mike Frysinger138f97c2009-04-06 19:00:50 -07001182static inline int bfin_spi_init_queue(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001183{
1184 INIT_LIST_HEAD(&drv_data->queue);
1185 spin_lock_init(&drv_data->lock);
1186
1187 drv_data->run = QUEUE_STOPPED;
1188 drv_data->busy = 0;
1189
1190 /* init transfer tasklet */
1191 tasklet_init(&drv_data->pump_transfers,
Mike Frysinger138f97c2009-04-06 19:00:50 -07001192 bfin_spi_pump_transfers, (unsigned long)drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001193
1194 /* init messages workqueue */
Mike Frysinger138f97c2009-04-06 19:00:50 -07001195 INIT_WORK(&drv_data->pump_messages, bfin_spi_pump_messages);
Kay Sievers6c7377a2009-03-24 16:38:21 -07001196 drv_data->workqueue = create_singlethread_workqueue(
1197 dev_name(drv_data->master->dev.parent));
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001198 if (drv_data->workqueue == NULL)
1199 return -EBUSY;
1200
1201 return 0;
1202}
1203
Mike Frysinger138f97c2009-04-06 19:00:50 -07001204static inline int bfin_spi_start_queue(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001205{
1206 unsigned long flags;
1207
1208 spin_lock_irqsave(&drv_data->lock, flags);
1209
1210 if (drv_data->run == QUEUE_RUNNING || drv_data->busy) {
1211 spin_unlock_irqrestore(&drv_data->lock, flags);
1212 return -EBUSY;
1213 }
1214
1215 drv_data->run = QUEUE_RUNNING;
1216 drv_data->cur_msg = NULL;
1217 drv_data->cur_transfer = NULL;
1218 drv_data->cur_chip = NULL;
1219 spin_unlock_irqrestore(&drv_data->lock, flags);
1220
1221 queue_work(drv_data->workqueue, &drv_data->pump_messages);
1222
1223 return 0;
1224}
1225
Mike Frysinger138f97c2009-04-06 19:00:50 -07001226static inline int bfin_spi_stop_queue(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001227{
1228 unsigned long flags;
1229 unsigned limit = 500;
1230 int status = 0;
1231
1232 spin_lock_irqsave(&drv_data->lock, flags);
1233
1234 /*
1235 * This is a bit lame, but is optimized for the common execution path.
1236 * A wait_queue on the drv_data->busy could be used, but then the common
1237 * execution path (pump_messages) would be required to call wake_up or
1238 * friends on every SPI message. Do this instead
1239 */
1240 drv_data->run = QUEUE_STOPPED;
1241 while (!list_empty(&drv_data->queue) && drv_data->busy && limit--) {
1242 spin_unlock_irqrestore(&drv_data->lock, flags);
1243 msleep(10);
1244 spin_lock_irqsave(&drv_data->lock, flags);
1245 }
1246
1247 if (!list_empty(&drv_data->queue) || drv_data->busy)
1248 status = -EBUSY;
1249
1250 spin_unlock_irqrestore(&drv_data->lock, flags);
1251
1252 return status;
1253}
1254
Mike Frysinger138f97c2009-04-06 19:00:50 -07001255static inline int bfin_spi_destroy_queue(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001256{
1257 int status;
1258
Mike Frysinger138f97c2009-04-06 19:00:50 -07001259 status = bfin_spi_stop_queue(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001260 if (status != 0)
1261 return status;
1262
1263 destroy_workqueue(drv_data->workqueue);
1264
1265 return 0;
1266}
1267
Mike Frysinger138f97c2009-04-06 19:00:50 -07001268static int __init bfin_spi_probe(struct platform_device *pdev)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001269{
1270 struct device *dev = &pdev->dev;
1271 struct bfin5xx_spi_master *platform_info;
1272 struct spi_master *master;
1273 struct driver_data *drv_data = 0;
Bryan Wua32c6912007-12-04 23:45:15 -08001274 struct resource *res;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001275 int status = 0;
1276
1277 platform_info = dev->platform_data;
1278
1279 /* Allocate master with space for drv_data */
1280 master = spi_alloc_master(dev, sizeof(struct driver_data) + 16);
1281 if (!master) {
1282 dev_err(&pdev->dev, "can not alloc spi_master\n");
1283 return -ENOMEM;
1284 }
Bryan Wu131b17d2007-12-04 23:45:12 -08001285
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001286 drv_data = spi_master_get_devdata(master);
1287 drv_data->master = master;
1288 drv_data->master_info = platform_info;
1289 drv_data->pdev = pdev;
Bryan Wu003d9222007-12-04 23:45:22 -08001290 drv_data->pin_req = platform_info->pin_req;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001291
1292 master->bus_num = pdev->id;
1293 master->num_chipselect = platform_info->num_chipselect;
Mike Frysinger138f97c2009-04-06 19:00:50 -07001294 master->cleanup = bfin_spi_cleanup;
1295 master->setup = bfin_spi_setup;
1296 master->transfer = bfin_spi_transfer;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001297
Bryan Wua32c6912007-12-04 23:45:15 -08001298 /* Find and map our resources */
1299 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1300 if (res == NULL) {
1301 dev_err(dev, "Cannot get IORESOURCE_MEM\n");
1302 status = -ENOENT;
1303 goto out_error_get_res;
1304 }
1305
Bryan Wuf4521262007-12-04 23:45:22 -08001306 drv_data->regs_base = ioremap(res->start, (res->end - res->start + 1));
1307 if (drv_data->regs_base == NULL) {
Bryan Wua32c6912007-12-04 23:45:15 -08001308 dev_err(dev, "Cannot map IO\n");
1309 status = -ENXIO;
1310 goto out_error_ioremap;
1311 }
1312
Bryan Wubb90eb02007-12-04 23:45:18 -08001313 drv_data->dma_channel = platform_get_irq(pdev, 0);
1314 if (drv_data->dma_channel < 0) {
Bryan Wua32c6912007-12-04 23:45:15 -08001315 dev_err(dev, "No DMA channel specified\n");
1316 status = -ENOENT;
1317 goto out_error_no_dma_ch;
1318 }
1319
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001320 /* Initial and start queue */
Mike Frysinger138f97c2009-04-06 19:00:50 -07001321 status = bfin_spi_init_queue(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001322 if (status != 0) {
Bryan Wua32c6912007-12-04 23:45:15 -08001323 dev_err(dev, "problem initializing queue\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001324 goto out_error_queue_alloc;
1325 }
Bryan Wua32c6912007-12-04 23:45:15 -08001326
Mike Frysinger138f97c2009-04-06 19:00:50 -07001327 status = bfin_spi_start_queue(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001328 if (status != 0) {
Bryan Wua32c6912007-12-04 23:45:15 -08001329 dev_err(dev, "problem starting queue\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001330 goto out_error_queue_alloc;
1331 }
1332
Vitja Makarovf9e522c2008-04-08 17:41:57 -07001333 status = peripheral_request_list(drv_data->pin_req, DRV_NAME);
1334 if (status != 0) {
1335 dev_err(&pdev->dev, ": Requesting Peripherals failed\n");
1336 goto out_error_queue_alloc;
1337 }
1338
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001339 /* Register with the SPI framework */
1340 platform_set_drvdata(pdev, drv_data);
1341 status = spi_register_master(master);
1342 if (status != 0) {
Bryan Wua32c6912007-12-04 23:45:15 -08001343 dev_err(dev, "problem registering spi master\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001344 goto out_error_queue_alloc;
1345 }
Bryan Wua32c6912007-12-04 23:45:15 -08001346
Bryan Wuf4521262007-12-04 23:45:22 -08001347 dev_info(dev, "%s, Version %s, regs_base@%p, dma channel@%d\n",
Bryan Wubb90eb02007-12-04 23:45:18 -08001348 DRV_DESC, DRV_VERSION, drv_data->regs_base,
1349 drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001350 return status;
1351
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001352out_error_queue_alloc:
Mike Frysinger138f97c2009-04-06 19:00:50 -07001353 bfin_spi_destroy_queue(drv_data);
Bryan Wua32c6912007-12-04 23:45:15 -08001354out_error_no_dma_ch:
Bryan Wubb90eb02007-12-04 23:45:18 -08001355 iounmap((void *) drv_data->regs_base);
Bryan Wua32c6912007-12-04 23:45:15 -08001356out_error_ioremap:
1357out_error_get_res:
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001358 spi_master_put(master);
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001359
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001360 return status;
1361}
1362
1363/* stop hardware and remove the driver */
Mike Frysinger138f97c2009-04-06 19:00:50 -07001364static int __devexit bfin_spi_remove(struct platform_device *pdev)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001365{
1366 struct driver_data *drv_data = platform_get_drvdata(pdev);
1367 int status = 0;
1368
1369 if (!drv_data)
1370 return 0;
1371
1372 /* Remove the queue */
Mike Frysinger138f97c2009-04-06 19:00:50 -07001373 status = bfin_spi_destroy_queue(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001374 if (status != 0)
1375 return status;
1376
1377 /* Disable the SSP at the peripheral and SOC level */
1378 bfin_spi_disable(drv_data);
1379
1380 /* Release DMA */
1381 if (drv_data->master_info->enable_dma) {
Bryan Wubb90eb02007-12-04 23:45:18 -08001382 if (dma_channel_active(drv_data->dma_channel))
1383 free_dma(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001384 }
1385
1386 /* Disconnect from the SPI framework */
1387 spi_unregister_master(drv_data->master);
1388
Bryan Wu003d9222007-12-04 23:45:22 -08001389 peripheral_free_list(drv_data->pin_req);
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001390
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001391 /* Prevent double remove */
1392 platform_set_drvdata(pdev, NULL);
1393
1394 return 0;
1395}
1396
1397#ifdef CONFIG_PM
Mike Frysinger138f97c2009-04-06 19:00:50 -07001398static int bfin_spi_suspend(struct platform_device *pdev, pm_message_t state)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001399{
1400 struct driver_data *drv_data = platform_get_drvdata(pdev);
1401 int status = 0;
1402
Mike Frysinger138f97c2009-04-06 19:00:50 -07001403 status = bfin_spi_stop_queue(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001404 if (status != 0)
1405 return status;
1406
1407 /* stop hardware */
1408 bfin_spi_disable(drv_data);
1409
1410 return 0;
1411}
1412
Mike Frysinger138f97c2009-04-06 19:00:50 -07001413static int bfin_spi_resume(struct platform_device *pdev)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001414{
1415 struct driver_data *drv_data = platform_get_drvdata(pdev);
1416 int status = 0;
1417
1418 /* Enable the SPI interface */
1419 bfin_spi_enable(drv_data);
1420
1421 /* Start the queue running */
Mike Frysinger138f97c2009-04-06 19:00:50 -07001422 status = bfin_spi_start_queue(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001423 if (status != 0) {
1424 dev_err(&pdev->dev, "problem starting queue (%d)\n", status);
1425 return status;
1426 }
1427
1428 return 0;
1429}
1430#else
Mike Frysinger138f97c2009-04-06 19:00:50 -07001431#define bfin_spi_suspend NULL
1432#define bfin_spi_resume NULL
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001433#endif /* CONFIG_PM */
1434
Kay Sievers7e38c3c2008-04-10 21:29:20 -07001435MODULE_ALIAS("platform:bfin-spi");
Mike Frysinger138f97c2009-04-06 19:00:50 -07001436static struct platform_driver bfin_spi_driver = {
David Brownellfc3ba952007-08-30 23:56:24 -07001437 .driver = {
Bryan Wua32c6912007-12-04 23:45:15 -08001438 .name = DRV_NAME,
Bryan Wu88b40362007-05-21 18:32:16 +08001439 .owner = THIS_MODULE,
1440 },
Mike Frysinger138f97c2009-04-06 19:00:50 -07001441 .suspend = bfin_spi_suspend,
1442 .resume = bfin_spi_resume,
1443 .remove = __devexit_p(bfin_spi_remove),
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001444};
1445
Mike Frysinger138f97c2009-04-06 19:00:50 -07001446static int __init bfin_spi_init(void)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001447{
Mike Frysinger138f97c2009-04-06 19:00:50 -07001448 return platform_driver_probe(&bfin_spi_driver, bfin_spi_probe);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001449}
Mike Frysinger138f97c2009-04-06 19:00:50 -07001450module_init(bfin_spi_init);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001451
Mike Frysinger138f97c2009-04-06 19:00:50 -07001452static void __exit bfin_spi_exit(void)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001453{
Mike Frysinger138f97c2009-04-06 19:00:50 -07001454 platform_driver_unregister(&bfin_spi_driver);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001455}
Mike Frysinger138f97c2009-04-06 19:00:50 -07001456module_exit(bfin_spi_exit);