blob: 6150a8cfb82a111390b78ab4111140e04fbc9d4f [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Bryan Wu131b17d2007-12-04 23:45:12 -080016#include <linux/io.h>
Wu, Bryana5f6abd2007-05-06 14:50:34 -070017#include <linux/ioport.h>
Bryan Wu131b17d2007-12-04 23:45:12 -080018#include <linux/irq.h>
Wu, Bryana5f6abd2007-05-06 14:50:34 -070019#include <linux/errno.h>
20#include <linux/interrupt.h>
21#include <linux/platform_device.h>
22#include <linux/dma-mapping.h>
23#include <linux/spi/spi.h>
24#include <linux/workqueue.h>
Wu, Bryana5f6abd2007-05-06 14:50:34 -070025
Wu, Bryana5f6abd2007-05-06 14:50:34 -070026#include <asm/dma.h>
Bryan Wu131b17d2007-12-04 23:45:12 -080027#include <asm/portmux.h>
Wu, Bryana5f6abd2007-05-06 14:50:34 -070028#include <asm/bfin5xx_spi.h>
Vitja Makarov8cf58582009-04-06 19:00:31 -070029#include <asm/cacheflush.h>
30
Bryan Wua32c6912007-12-04 23:45:15 -080031#define DRV_NAME "bfin-spi"
32#define DRV_AUTHOR "Bryan Wu, Luke Yang"
Mike Frysinger138f97c2009-04-06 19:00:50 -070033#define DRV_DESC "Blackfin on-chip SPI Controller Driver"
Bryan Wua32c6912007-12-04 23:45:15 -080034#define DRV_VERSION "1.0"
35
36MODULE_AUTHOR(DRV_AUTHOR);
37MODULE_DESCRIPTION(DRV_DESC);
Wu, Bryana5f6abd2007-05-06 14:50:34 -070038MODULE_LICENSE("GPL");
39
Bryan Wubb90eb02007-12-04 23:45:18 -080040#define START_STATE ((void *)0)
41#define RUNNING_STATE ((void *)1)
42#define DONE_STATE ((void *)2)
43#define ERROR_STATE ((void *)-1)
44#define QUEUE_RUNNING 0
45#define QUEUE_STOPPED 1
Wu, Bryana5f6abd2007-05-06 14:50:34 -070046
Wolfgang Muees93b61bd2009-04-06 19:00:53 -070047/* Value to send if no TX value is supplied */
48#define SPI_IDLE_TXVAL 0x0000
49
Wu, Bryana5f6abd2007-05-06 14:50:34 -070050struct driver_data {
51 /* Driver model hookup */
52 struct platform_device *pdev;
53
54 /* SPI framework hookup */
55 struct spi_master *master;
56
Bryan Wubb90eb02007-12-04 23:45:18 -080057 /* Regs base of SPI controller */
Bryan Wuf4521262007-12-04 23:45:22 -080058 void __iomem *regs_base;
Bryan Wubb90eb02007-12-04 23:45:18 -080059
Bryan Wu003d9222007-12-04 23:45:22 -080060 /* Pin request list */
61 u16 *pin_req;
62
Wu, Bryana5f6abd2007-05-06 14:50:34 -070063 /* BFIN hookup */
64 struct bfin5xx_spi_master *master_info;
65
66 /* Driver message queue */
67 struct workqueue_struct *workqueue;
68 struct work_struct pump_messages;
69 spinlock_t lock;
70 struct list_head queue;
71 int busy;
72 int run;
73
74 /* Message Transfer pump */
75 struct tasklet_struct pump_transfers;
76
77 /* Current message transfer state info */
78 struct spi_message *cur_msg;
79 struct spi_transfer *cur_transfer;
80 struct chip_data *cur_chip;
81 size_t len_in_bytes;
82 size_t len;
83 void *tx;
84 void *tx_end;
85 void *rx;
86 void *rx_end;
Bryan Wubb90eb02007-12-04 23:45:18 -080087
88 /* DMA stuffs */
89 int dma_channel;
Wu, Bryana5f6abd2007-05-06 14:50:34 -070090 int dma_mapped;
Bryan Wubb90eb02007-12-04 23:45:18 -080091 int dma_requested;
Wu, Bryana5f6abd2007-05-06 14:50:34 -070092 dma_addr_t rx_dma;
93 dma_addr_t tx_dma;
Bryan Wubb90eb02007-12-04 23:45:18 -080094
Yi Lif6a6d962009-06-03 09:46:22 +000095 int irq_requested;
96 int spi_irq;
97
Wu, Bryana5f6abd2007-05-06 14:50:34 -070098 size_t rx_map_len;
99 size_t tx_map_len;
100 u8 n_bytes;
Bryan Wufad91c82007-12-04 23:45:14 -0800101 int cs_change;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700102 void (*write) (struct driver_data *);
103 void (*read) (struct driver_data *);
104 void (*duplex) (struct driver_data *);
105};
106
107struct chip_data {
108 u16 ctl_reg;
109 u16 baud;
110 u16 flag;
111
112 u8 chip_select_num;
113 u8 n_bytes;
Bryan Wu88b40362007-05-21 18:32:16 +0800114 u8 width; /* 0 or 1 */
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700115 u8 enable_dma;
116 u8 bits_per_word; /* 8 or 16 */
117 u8 cs_change_per_word;
Bryan Wu62310e52007-12-04 23:45:20 -0800118 u16 cs_chg_udelay; /* Some devices require > 255usec delay */
Michael Hennerich42c78b22009-04-06 19:00:51 -0700119 u32 cs_gpio;
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700120 u16 idle_tx_val;
Yi Lif6a6d962009-06-03 09:46:22 +0000121 u8 pio_interrupt; /* use spi data irq */
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700122 void (*write) (struct driver_data *);
123 void (*read) (struct driver_data *);
124 void (*duplex) (struct driver_data *);
125};
126
Bryan Wubb90eb02007-12-04 23:45:18 -0800127#define DEFINE_SPI_REG(reg, off) \
128static inline u16 read_##reg(struct driver_data *drv_data) \
129 { return bfin_read16(drv_data->regs_base + off); } \
130static inline void write_##reg(struct driver_data *drv_data, u16 v) \
131 { bfin_write16(drv_data->regs_base + off, v); }
132
133DEFINE_SPI_REG(CTRL, 0x00)
134DEFINE_SPI_REG(FLAG, 0x04)
135DEFINE_SPI_REG(STAT, 0x08)
136DEFINE_SPI_REG(TDBR, 0x0C)
137DEFINE_SPI_REG(RDBR, 0x10)
138DEFINE_SPI_REG(BAUD, 0x14)
139DEFINE_SPI_REG(SHAW, 0x18)
140
Bryan Wu88b40362007-05-21 18:32:16 +0800141static void bfin_spi_enable(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700142{
143 u16 cr;
144
Bryan Wubb90eb02007-12-04 23:45:18 -0800145 cr = read_CTRL(drv_data);
146 write_CTRL(drv_data, (cr | BIT_CTL_ENABLE));
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700147}
148
Bryan Wu88b40362007-05-21 18:32:16 +0800149static void bfin_spi_disable(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700150{
151 u16 cr;
152
Bryan Wubb90eb02007-12-04 23:45:18 -0800153 cr = read_CTRL(drv_data);
154 write_CTRL(drv_data, (cr & (~BIT_CTL_ENABLE)));
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700155}
156
157/* Caculate the SPI_BAUD register value based on input HZ */
158static u16 hz_to_spi_baud(u32 speed_hz)
159{
160 u_long sclk = get_sclk();
161 u16 spi_baud = (sclk / (2 * speed_hz));
162
163 if ((sclk % (2 * speed_hz)) > 0)
164 spi_baud++;
165
Michael Hennerich7513e002009-04-06 19:00:32 -0700166 if (spi_baud < MIN_SPI_BAUD_VAL)
167 spi_baud = MIN_SPI_BAUD_VAL;
168
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700169 return spi_baud;
170}
171
Mike Frysinger138f97c2009-04-06 19:00:50 -0700172static int bfin_spi_flush(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700173{
174 unsigned long limit = loops_per_jiffy << 1;
175
176 /* wait for stop and clear stat */
Roel Kluinb4bd2ab2009-06-17 16:26:02 -0700177 while (!(read_STAT(drv_data) & BIT_STAT_SPIF) && --limit)
Bryan Wud8c05002007-12-04 23:45:21 -0800178 cpu_relax();
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700179
Bryan Wubb90eb02007-12-04 23:45:18 -0800180 write_STAT(drv_data, BIT_STAT_CLR);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700181
182 return limit;
183}
184
Bryan Wufad91c82007-12-04 23:45:14 -0800185/* Chip select operation functions for cs_change flag */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700186static void bfin_spi_cs_active(struct driver_data *drv_data, struct chip_data *chip)
Bryan Wufad91c82007-12-04 23:45:14 -0800187{
Michael Hennerich42c78b22009-04-06 19:00:51 -0700188 if (likely(chip->chip_select_num)) {
189 u16 flag = read_FLAG(drv_data);
Bryan Wufad91c82007-12-04 23:45:14 -0800190
Barry Song82216102009-06-17 10:10:53 +0000191 flag &= ~chip->flag;
Bryan Wufad91c82007-12-04 23:45:14 -0800192
Michael Hennerich42c78b22009-04-06 19:00:51 -0700193 write_FLAG(drv_data, flag);
194 } else {
195 gpio_set_value(chip->cs_gpio, 0);
196 }
Bryan Wufad91c82007-12-04 23:45:14 -0800197}
198
Mike Frysinger138f97c2009-04-06 19:00:50 -0700199static void bfin_spi_cs_deactive(struct driver_data *drv_data, struct chip_data *chip)
Bryan Wufad91c82007-12-04 23:45:14 -0800200{
Michael Hennerich42c78b22009-04-06 19:00:51 -0700201 if (likely(chip->chip_select_num)) {
202 u16 flag = read_FLAG(drv_data);
Bryan Wufad91c82007-12-04 23:45:14 -0800203
Barry Song82216102009-06-17 10:10:53 +0000204 flag |= chip->flag;
Bryan Wufad91c82007-12-04 23:45:14 -0800205
Michael Hennerich42c78b22009-04-06 19:00:51 -0700206 write_FLAG(drv_data, flag);
207 } else {
208 gpio_set_value(chip->cs_gpio, 1);
209 }
Bryan Wu62310e52007-12-04 23:45:20 -0800210
211 /* Move delay here for consistency */
212 if (chip->cs_chg_udelay)
213 udelay(chip->cs_chg_udelay);
Bryan Wufad91c82007-12-04 23:45:14 -0800214}
215
Barry Song82216102009-06-17 10:10:53 +0000216/* enable or disable the pin muxed by GPIO and SPI CS to work as SPI CS */
217static inline void bfin_spi_cs_enable(struct driver_data *drv_data, struct chip_data *chip)
218{
219 u16 flag = read_FLAG(drv_data);
220
221 flag |= (chip->flag >> 8);
222
223 write_FLAG(drv_data, flag);
224}
225
226static inline void bfin_spi_cs_disable(struct driver_data *drv_data, struct chip_data *chip)
227{
228 u16 flag = read_FLAG(drv_data);
229
230 flag &= ~(chip->flag >> 8);
231
232 write_FLAG(drv_data, flag);
233}
234
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700235/* stop controller and re-config current chip*/
Mike Frysinger138f97c2009-04-06 19:00:50 -0700236static void bfin_spi_restore_state(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700237{
238 struct chip_data *chip = drv_data->cur_chip;
239
240 /* Clear status and disable clock */
Bryan Wubb90eb02007-12-04 23:45:18 -0800241 write_STAT(drv_data, BIT_STAT_CLR);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700242 bfin_spi_disable(drv_data);
Bryan Wu88b40362007-05-21 18:32:16 +0800243 dev_dbg(&drv_data->pdev->dev, "restoring spi ctl state\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700244
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700245 /* Load the registers */
Bryan Wubb90eb02007-12-04 23:45:18 -0800246 write_CTRL(drv_data, chip->ctl_reg);
Bryan Wu092e1fd2007-12-04 23:45:23 -0800247 write_BAUD(drv_data, chip->baud);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800248
249 bfin_spi_enable(drv_data);
Mike Frysinger138f97c2009-04-06 19:00:50 -0700250 bfin_spi_cs_active(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700251}
252
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700253/* used to kick off transfer in rx mode and read unwanted RX data */
254static inline void bfin_spi_dummy_read(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700255{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700256 (void) read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700257}
258
Mike Frysinger138f97c2009-04-06 19:00:50 -0700259static void bfin_spi_null_writer(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 /* clear RXS (we check for RXS inside the loop) */
265 bfin_spi_dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700266
267 while (drv_data->tx < drv_data->tx_end) {
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700268 write_TDBR(drv_data, tx_val);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700269 drv_data->tx += n_bytes;
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700270 /* wait until transfer finished.
271 checking SPIF or TXS may not guarantee transfer completion */
272 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
273 cpu_relax();
274 /* discard RX data and clear RXS */
275 bfin_spi_dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700276 }
277}
278
Mike Frysinger138f97c2009-04-06 19:00:50 -0700279static void bfin_spi_null_reader(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700280{
281 u8 n_bytes = drv_data->n_bytes;
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700282 u16 tx_val = drv_data->cur_chip->idle_tx_val;
283
284 /* discard old RX data and clear RXS */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700285 bfin_spi_dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700286
287 while (drv_data->rx < drv_data->rx_end) {
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700288 write_TDBR(drv_data, tx_val);
289 drv_data->rx += n_bytes;
Bryan Wubb90eb02007-12-04 23:45:18 -0800290 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800291 cpu_relax();
Mike Frysinger138f97c2009-04-06 19:00:50 -0700292 bfin_spi_dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700293 }
294}
295
Mike Frysinger138f97c2009-04-06 19:00:50 -0700296static void bfin_spi_u8_writer(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700297{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700298 /* clear RXS (we check for RXS inside the loop) */
299 bfin_spi_dummy_read(drv_data);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800300
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700301 while (drv_data->tx < drv_data->tx_end) {
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700302 write_TDBR(drv_data, (*(u8 *) (drv_data->tx++)));
303 /* wait until transfer finished.
304 checking SPIF or TXS may not guarantee transfer completion */
305 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800306 cpu_relax();
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700307 /* discard RX data and clear RXS */
308 bfin_spi_dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700309 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700310}
311
Mike Frysinger138f97c2009-04-06 19:00:50 -0700312static void bfin_spi_u8_cs_chg_writer(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700313{
314 struct chip_data *chip = drv_data->cur_chip;
315
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700316 /* clear RXS (we check for RXS inside the loop) */
317 bfin_spi_dummy_read(drv_data);
318
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700319 while (drv_data->tx < drv_data->tx_end) {
Mike Frysinger138f97c2009-04-06 19:00:50 -0700320 bfin_spi_cs_active(drv_data, chip);
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700321 write_TDBR(drv_data, (*(u8 *) (drv_data->tx++)));
322 /* make sure transfer finished before deactiving CS */
323 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800324 cpu_relax();
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700325 bfin_spi_dummy_read(drv_data);
Mike Frysinger138f97c2009-04-06 19:00:50 -0700326 bfin_spi_cs_deactive(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700327 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700328}
329
Mike Frysinger138f97c2009-04-06 19:00:50 -0700330static void bfin_spi_u8_reader(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700331{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700332 u16 tx_val = drv_data->cur_chip->idle_tx_val;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700333
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700334 /* discard old RX data and clear RXS */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700335 bfin_spi_dummy_read(drv_data);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800336
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700337 while (drv_data->rx < drv_data->rx_end) {
338 write_TDBR(drv_data, tx_val);
Bryan Wubb90eb02007-12-04 23:45:18 -0800339 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800340 cpu_relax();
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700341 *(u8 *) (drv_data->rx++) = read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700342 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700343}
344
Mike Frysinger138f97c2009-04-06 19:00:50 -0700345static void bfin_spi_u8_cs_chg_reader(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700346{
347 struct chip_data *chip = drv_data->cur_chip;
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700348 u16 tx_val = chip->idle_tx_val;
349
350 /* discard old RX data and clear RXS */
351 bfin_spi_dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700352
Bryan Wue26aa012008-02-06 01:38:18 -0800353 while (drv_data->rx < drv_data->rx_end) {
Mike Frysinger138f97c2009-04-06 19:00:50 -0700354 bfin_spi_cs_active(drv_data, chip);
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700355 write_TDBR(drv_data, tx_val);
Bryan Wubb90eb02007-12-04 23:45:18 -0800356 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800357 cpu_relax();
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700358 *(u8 *) (drv_data->rx++) = read_RDBR(drv_data);
Mike Frysinger138f97c2009-04-06 19:00:50 -0700359 bfin_spi_cs_deactive(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700360 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700361}
362
Mike Frysinger138f97c2009-04-06 19:00:50 -0700363static void bfin_spi_u8_duplex(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700364{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700365 /* discard old RX data and clear RXS */
366 bfin_spi_dummy_read(drv_data);
367
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700368 while (drv_data->rx < drv_data->rx_end) {
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700369 write_TDBR(drv_data, (*(u8 *) (drv_data->tx++)));
Bryan Wubb90eb02007-12-04 23:45:18 -0800370 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800371 cpu_relax();
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700372 *(u8 *) (drv_data->rx++) = read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700373 }
374}
375
Mike Frysinger138f97c2009-04-06 19:00:50 -0700376static void bfin_spi_u8_cs_chg_duplex(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700377{
378 struct chip_data *chip = drv_data->cur_chip;
379
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700380 /* discard old RX data and clear RXS */
381 bfin_spi_dummy_read(drv_data);
382
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700383 while (drv_data->rx < drv_data->rx_end) {
Mike Frysinger138f97c2009-04-06 19:00:50 -0700384 bfin_spi_cs_active(drv_data, chip);
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700385 write_TDBR(drv_data, (*(u8 *) (drv_data->tx++)));
Bryan Wubb90eb02007-12-04 23:45:18 -0800386 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800387 cpu_relax();
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700388 *(u8 *) (drv_data->rx++) = read_RDBR(drv_data);
Mike Frysinger138f97c2009-04-06 19:00:50 -0700389 bfin_spi_cs_deactive(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700390 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700391}
392
Mike Frysinger138f97c2009-04-06 19:00:50 -0700393static void bfin_spi_u16_writer(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700394{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700395 /* clear RXS (we check for RXS inside the loop) */
396 bfin_spi_dummy_read(drv_data);
Bryan Wu88b40362007-05-21 18:32:16 +0800397
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700398 while (drv_data->tx < drv_data->tx_end) {
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 /* wait until transfer finished.
402 checking SPIF or TXS may not guarantee transfer completion */
403 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
404 cpu_relax();
405 /* discard RX data and clear RXS */
406 bfin_spi_dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700407 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700408}
409
Mike Frysinger138f97c2009-04-06 19:00:50 -0700410static void bfin_spi_u16_cs_chg_writer(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700411{
412 struct chip_data *chip = drv_data->cur_chip;
413
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700414 /* clear RXS (we check for RXS inside the loop) */
415 bfin_spi_dummy_read(drv_data);
416
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700417 while (drv_data->tx < drv_data->tx_end) {
Mike Frysinger138f97c2009-04-06 19:00:50 -0700418 bfin_spi_cs_active(drv_data, chip);
Bryan Wubb90eb02007-12-04 23:45:18 -0800419 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700420 drv_data->tx += 2;
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700421 /* make sure transfer finished before deactiving CS */
422 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
423 cpu_relax();
424 bfin_spi_dummy_read(drv_data);
425 bfin_spi_cs_deactive(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700426 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700427}
428
Mike Frysinger138f97c2009-04-06 19:00:50 -0700429static void bfin_spi_u16_reader(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700430{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700431 u16 tx_val = drv_data->cur_chip->idle_tx_val;
Sonic Zhangcc487e72007-12-04 23:45:17 -0800432
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700433 /* discard old RX data and clear RXS */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700434 bfin_spi_dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700435
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700436 while (drv_data->rx < drv_data->rx_end) {
437 write_TDBR(drv_data, tx_val);
Bryan Wubb90eb02007-12-04 23:45:18 -0800438 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800439 cpu_relax();
Bryan Wubb90eb02007-12-04 23:45:18 -0800440 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700441 drv_data->rx += 2;
442 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700443}
444
Mike Frysinger138f97c2009-04-06 19:00:50 -0700445static void bfin_spi_u16_cs_chg_reader(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700446{
447 struct chip_data *chip = drv_data->cur_chip;
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700448 u16 tx_val = chip->idle_tx_val;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700449
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700450 /* discard old RX data and clear RXS */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700451 bfin_spi_dummy_read(drv_data);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800452
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700453 while (drv_data->rx < drv_data->rx_end) {
454 bfin_spi_cs_active(drv_data, chip);
455 write_TDBR(drv_data, tx_val);
Bryan Wubb90eb02007-12-04 23:45:18 -0800456 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800457 cpu_relax();
Bryan Wubb90eb02007-12-04 23:45:18 -0800458 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700459 drv_data->rx += 2;
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700460 bfin_spi_cs_deactive(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700461 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700462}
463
Mike Frysinger138f97c2009-04-06 19:00:50 -0700464static void bfin_spi_u16_duplex(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700465{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700466 /* discard old RX data and clear RXS */
467 bfin_spi_dummy_read(drv_data);
468
469 while (drv_data->rx < drv_data->rx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800470 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700471 drv_data->tx += 2;
Bryan Wubb90eb02007-12-04 23:45:18 -0800472 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800473 cpu_relax();
Bryan Wubb90eb02007-12-04 23:45:18 -0800474 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700475 drv_data->rx += 2;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700476 }
477}
478
Mike Frysinger138f97c2009-04-06 19:00:50 -0700479static void bfin_spi_u16_cs_chg_duplex(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700480{
481 struct chip_data *chip = drv_data->cur_chip;
482
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700483 /* discard old RX data and clear RXS */
484 bfin_spi_dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700485
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700486 while (drv_data->rx < drv_data->rx_end) {
487 bfin_spi_cs_active(drv_data, chip);
Bryan Wubb90eb02007-12-04 23:45:18 -0800488 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700489 drv_data->tx += 2;
Bryan Wubb90eb02007-12-04 23:45:18 -0800490 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800491 cpu_relax();
Bryan Wubb90eb02007-12-04 23:45:18 -0800492 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700493 drv_data->rx += 2;
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700494 bfin_spi_cs_deactive(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700495 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700496}
497
498/* test if ther is more transfer to be done */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700499static void *bfin_spi_next_transfer(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700500{
501 struct spi_message *msg = drv_data->cur_msg;
502 struct spi_transfer *trans = drv_data->cur_transfer;
503
504 /* Move to next transfer */
505 if (trans->transfer_list.next != &msg->transfers) {
506 drv_data->cur_transfer =
507 list_entry(trans->transfer_list.next,
508 struct spi_transfer, transfer_list);
509 return RUNNING_STATE;
510 } else
511 return DONE_STATE;
512}
513
514/*
515 * caller already set message->status;
516 * dma and pio irqs are blocked give finished message back
517 */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700518static void bfin_spi_giveback(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700519{
Bryan Wufad91c82007-12-04 23:45:14 -0800520 struct chip_data *chip = drv_data->cur_chip;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700521 struct spi_transfer *last_transfer;
522 unsigned long flags;
523 struct spi_message *msg;
524
525 spin_lock_irqsave(&drv_data->lock, flags);
526 msg = drv_data->cur_msg;
527 drv_data->cur_msg = NULL;
528 drv_data->cur_transfer = NULL;
529 drv_data->cur_chip = NULL;
530 queue_work(drv_data->workqueue, &drv_data->pump_messages);
531 spin_unlock_irqrestore(&drv_data->lock, flags);
532
533 last_transfer = list_entry(msg->transfers.prev,
534 struct spi_transfer, transfer_list);
535
536 msg->state = NULL;
537
Bryan Wufad91c82007-12-04 23:45:14 -0800538 if (!drv_data->cs_change)
Mike Frysinger138f97c2009-04-06 19:00:50 -0700539 bfin_spi_cs_deactive(drv_data, chip);
Bryan Wufad91c82007-12-04 23:45:14 -0800540
Yi Lib9b2a762009-04-06 19:00:49 -0700541 /* Not stop spi in autobuffer mode */
542 if (drv_data->tx_dma != 0xFFFF)
543 bfin_spi_disable(drv_data);
544
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700545 if (msg->complete)
546 msg->complete(msg->context);
547}
548
Yi Lif6a6d962009-06-03 09:46:22 +0000549/* spi data irq handler */
550static irqreturn_t bfin_spi_pio_irq_handler(int irq, void *dev_id)
551{
552 struct driver_data *drv_data = dev_id;
553 struct chip_data *chip = drv_data->cur_chip;
554 struct spi_message *msg = drv_data->cur_msg;
555 int n_bytes = drv_data->n_bytes;
556
557 /* wait until transfer finished. */
558 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
559 cpu_relax();
560
561 if ((drv_data->tx && drv_data->tx >= drv_data->tx_end) ||
562 (drv_data->rx && drv_data->rx >= (drv_data->rx_end - n_bytes))) {
563 /* last read */
564 if (drv_data->rx) {
565 dev_dbg(&drv_data->pdev->dev, "last read\n");
566 if (n_bytes == 2)
567 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
568 else if (n_bytes == 1)
569 *(u8 *) (drv_data->rx) = read_RDBR(drv_data);
570 drv_data->rx += n_bytes;
571 }
572
573 msg->actual_length += drv_data->len_in_bytes;
574 if (drv_data->cs_change)
575 bfin_spi_cs_deactive(drv_data, chip);
576 /* Move to next transfer */
577 msg->state = bfin_spi_next_transfer(drv_data);
578
579 disable_irq(drv_data->spi_irq);
580
581 /* Schedule transfer tasklet */
582 tasklet_schedule(&drv_data->pump_transfers);
583 return IRQ_HANDLED;
584 }
585
586 if (drv_data->rx && drv_data->tx) {
587 /* duplex */
588 dev_dbg(&drv_data->pdev->dev, "duplex: write_TDBR\n");
589 if (drv_data->n_bytes == 2) {
590 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
591 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
592 } else if (drv_data->n_bytes == 1) {
593 *(u8 *) (drv_data->rx) = read_RDBR(drv_data);
594 write_TDBR(drv_data, (*(u8 *) (drv_data->tx)));
595 }
596 } else if (drv_data->rx) {
597 /* read */
598 dev_dbg(&drv_data->pdev->dev, "read: write_TDBR\n");
599 if (drv_data->n_bytes == 2)
600 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
601 else if (drv_data->n_bytes == 1)
602 *(u8 *) (drv_data->rx) = read_RDBR(drv_data);
603 write_TDBR(drv_data, chip->idle_tx_val);
604 } else if (drv_data->tx) {
605 /* write */
606 dev_dbg(&drv_data->pdev->dev, "write: write_TDBR\n");
607 bfin_spi_dummy_read(drv_data);
608 if (drv_data->n_bytes == 2)
609 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
610 else if (drv_data->n_bytes == 1)
611 write_TDBR(drv_data, (*(u8 *) (drv_data->tx)));
612 }
613
614 if (drv_data->tx)
615 drv_data->tx += n_bytes;
616 if (drv_data->rx)
617 drv_data->rx += n_bytes;
618
619 return IRQ_HANDLED;
620}
621
Mike Frysinger138f97c2009-04-06 19:00:50 -0700622static irqreturn_t bfin_spi_dma_irq_handler(int irq, void *dev_id)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700623{
Jeff Garzik15aafa22008-02-06 01:36:20 -0800624 struct driver_data *drv_data = dev_id;
Bryan Wufad91c82007-12-04 23:45:14 -0800625 struct chip_data *chip = drv_data->cur_chip;
Bryan Wubb90eb02007-12-04 23:45:18 -0800626 struct spi_message *msg = drv_data->cur_msg;
Mike Frysingeraaaf9392009-04-06 19:00:42 -0700627 unsigned long timeout;
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700628 unsigned short dmastat = get_dma_curr_irqstat(drv_data->dma_channel);
Mike Frysinger04b95d22009-04-06 19:00:35 -0700629 u16 spistat = read_STAT(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700630
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700631 dev_dbg(&drv_data->pdev->dev,
632 "in dma_irq_handler dmastat:0x%x spistat:0x%x\n",
633 dmastat, spistat);
634
Bryan Wubb90eb02007-12-04 23:45:18 -0800635 clear_dma_irqstat(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700636
637 /*
Bryan Wud6fe89b2007-06-11 17:34:17 +0800638 * wait for the last transaction shifted out. HRM states:
639 * at this point there may still be data in the SPI DMA FIFO waiting
640 * to be transmitted ... software needs to poll TXS in the SPI_STAT
641 * register until it goes low for 2 successive reads
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700642 */
643 if (drv_data->tx != NULL) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800644 while ((read_STAT(drv_data) & TXS) ||
645 (read_STAT(drv_data) & TXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800646 cpu_relax();
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700647 }
648
Mike Frysingeraaaf9392009-04-06 19:00:42 -0700649 dev_dbg(&drv_data->pdev->dev,
650 "in dma_irq_handler dmastat:0x%x spistat:0x%x\n",
651 dmastat, read_STAT(drv_data));
652
653 timeout = jiffies + HZ;
Bryan Wubb90eb02007-12-04 23:45:18 -0800654 while (!(read_STAT(drv_data) & SPIF))
Mike Frysingeraaaf9392009-04-06 19:00:42 -0700655 if (!time_before(jiffies, timeout)) {
656 dev_warn(&drv_data->pdev->dev, "timeout waiting for SPIF");
657 break;
658 } else
659 cpu_relax();
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700660
Mike Frysinger40a29452009-04-06 19:00:38 -0700661 if ((dmastat & DMA_ERR) && (spistat & RBSY)) {
Mike Frysinger04b95d22009-04-06 19:00:35 -0700662 msg->state = ERROR_STATE;
663 dev_err(&drv_data->pdev->dev, "dma receive: fifo/buffer overflow\n");
664 } else {
665 msg->actual_length += drv_data->len_in_bytes;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700666
Mike Frysinger04b95d22009-04-06 19:00:35 -0700667 if (drv_data->cs_change)
Mike Frysinger138f97c2009-04-06 19:00:50 -0700668 bfin_spi_cs_deactive(drv_data, chip);
Bryan Wufad91c82007-12-04 23:45:14 -0800669
Mike Frysinger04b95d22009-04-06 19:00:35 -0700670 /* Move to next transfer */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700671 msg->state = bfin_spi_next_transfer(drv_data);
Mike Frysinger04b95d22009-04-06 19:00:35 -0700672 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700673
674 /* Schedule transfer tasklet */
675 tasklet_schedule(&drv_data->pump_transfers);
676
677 /* free the irq handler before next transfer */
Bryan Wu88b40362007-05-21 18:32:16 +0800678 dev_dbg(&drv_data->pdev->dev,
679 "disable dma channel irq%d\n",
Bryan Wubb90eb02007-12-04 23:45:18 -0800680 drv_data->dma_channel);
681 dma_disable_irq(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700682
683 return IRQ_HANDLED;
684}
685
Mike Frysinger138f97c2009-04-06 19:00:50 -0700686static void bfin_spi_pump_transfers(unsigned long data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700687{
688 struct driver_data *drv_data = (struct driver_data *)data;
689 struct spi_message *message = NULL;
690 struct spi_transfer *transfer = NULL;
691 struct spi_transfer *previous = NULL;
692 struct chip_data *chip = NULL;
Bryan Wu88b40362007-05-21 18:32:16 +0800693 u8 width;
694 u16 cr, dma_width, dma_config;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700695 u32 tranf_success = 1;
Vitja Makarov8eeb12e2008-05-01 04:35:03 -0700696 u8 full_duplex = 0;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700697
698 /* Get current state information */
699 message = drv_data->cur_msg;
700 transfer = drv_data->cur_transfer;
701 chip = drv_data->cur_chip;
Bryan Wu092e1fd2007-12-04 23:45:23 -0800702
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700703 /*
704 * if msg is error or done, report it back using complete() callback
705 */
706
707 /* Handle for abort */
708 if (message->state == ERROR_STATE) {
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700709 dev_dbg(&drv_data->pdev->dev, "transfer: we've hit an error\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700710 message->status = -EIO;
Mike Frysinger138f97c2009-04-06 19:00:50 -0700711 bfin_spi_giveback(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700712 return;
713 }
714
715 /* Handle end of message */
716 if (message->state == DONE_STATE) {
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700717 dev_dbg(&drv_data->pdev->dev, "transfer: all done!\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700718 message->status = 0;
Mike Frysinger138f97c2009-04-06 19:00:50 -0700719 bfin_spi_giveback(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700720 return;
721 }
722
723 /* Delay if requested at end of transfer */
724 if (message->state == RUNNING_STATE) {
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700725 dev_dbg(&drv_data->pdev->dev, "transfer: still running ...\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700726 previous = list_entry(transfer->transfer_list.prev,
727 struct spi_transfer, transfer_list);
728 if (previous->delay_usecs)
729 udelay(previous->delay_usecs);
730 }
731
732 /* Setup the transfer state based on the type of transfer */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700733 if (bfin_spi_flush(drv_data) == 0) {
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700734 dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
735 message->status = -EIO;
Mike Frysinger138f97c2009-04-06 19:00:50 -0700736 bfin_spi_giveback(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700737 return;
738 }
739
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700740 if (transfer->len == 0) {
741 /* Move to next transfer of this msg */
742 message->state = bfin_spi_next_transfer(drv_data);
743 /* Schedule next transfer tasklet */
744 tasklet_schedule(&drv_data->pump_transfers);
745 }
746
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700747 if (transfer->tx_buf != NULL) {
748 drv_data->tx = (void *)transfer->tx_buf;
749 drv_data->tx_end = drv_data->tx + transfer->len;
Bryan Wu88b40362007-05-21 18:32:16 +0800750 dev_dbg(&drv_data->pdev->dev, "tx_buf is %p, tx_end is %p\n",
751 transfer->tx_buf, drv_data->tx_end);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700752 } else {
753 drv_data->tx = NULL;
754 }
755
756 if (transfer->rx_buf != NULL) {
Vitja Makarov8eeb12e2008-05-01 04:35:03 -0700757 full_duplex = transfer->tx_buf != NULL;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700758 drv_data->rx = transfer->rx_buf;
759 drv_data->rx_end = drv_data->rx + transfer->len;
Bryan Wu88b40362007-05-21 18:32:16 +0800760 dev_dbg(&drv_data->pdev->dev, "rx_buf is %p, rx_end is %p\n",
761 transfer->rx_buf, drv_data->rx_end);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700762 } else {
763 drv_data->rx = NULL;
764 }
765
766 drv_data->rx_dma = transfer->rx_dma;
767 drv_data->tx_dma = transfer->tx_dma;
768 drv_data->len_in_bytes = transfer->len;
Bryan Wufad91c82007-12-04 23:45:14 -0800769 drv_data->cs_change = transfer->cs_change;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700770
Bryan Wu092e1fd2007-12-04 23:45:23 -0800771 /* Bits per word setup */
772 switch (transfer->bits_per_word) {
773 case 8:
774 drv_data->n_bytes = 1;
775 width = CFG_SPI_WORDSIZE8;
776 drv_data->read = chip->cs_change_per_word ?
Mike Frysinger138f97c2009-04-06 19:00:50 -0700777 bfin_spi_u8_cs_chg_reader : bfin_spi_u8_reader;
Bryan Wu092e1fd2007-12-04 23:45:23 -0800778 drv_data->write = chip->cs_change_per_word ?
Mike Frysinger138f97c2009-04-06 19:00:50 -0700779 bfin_spi_u8_cs_chg_writer : bfin_spi_u8_writer;
Bryan Wu092e1fd2007-12-04 23:45:23 -0800780 drv_data->duplex = chip->cs_change_per_word ?
Mike Frysinger138f97c2009-04-06 19:00:50 -0700781 bfin_spi_u8_cs_chg_duplex : bfin_spi_u8_duplex;
Bryan Wu092e1fd2007-12-04 23:45:23 -0800782 break;
783
784 case 16:
785 drv_data->n_bytes = 2;
786 width = CFG_SPI_WORDSIZE16;
787 drv_data->read = chip->cs_change_per_word ?
Mike Frysinger138f97c2009-04-06 19:00:50 -0700788 bfin_spi_u16_cs_chg_reader : bfin_spi_u16_reader;
Bryan Wu092e1fd2007-12-04 23:45:23 -0800789 drv_data->write = chip->cs_change_per_word ?
Mike Frysinger138f97c2009-04-06 19:00:50 -0700790 bfin_spi_u16_cs_chg_writer : bfin_spi_u16_writer;
Bryan Wu092e1fd2007-12-04 23:45:23 -0800791 drv_data->duplex = chip->cs_change_per_word ?
Mike Frysinger138f97c2009-04-06 19:00:50 -0700792 bfin_spi_u16_cs_chg_duplex : bfin_spi_u16_duplex;
Bryan Wu092e1fd2007-12-04 23:45:23 -0800793 break;
794
795 default:
796 /* No change, the same as default setting */
Yi Lif6a6d962009-06-03 09:46:22 +0000797 transfer->bits_per_word = chip->bits_per_word;
Bryan Wu092e1fd2007-12-04 23:45:23 -0800798 drv_data->n_bytes = chip->n_bytes;
799 width = chip->width;
Mike Frysinger138f97c2009-04-06 19:00:50 -0700800 drv_data->write = drv_data->tx ? chip->write : bfin_spi_null_writer;
801 drv_data->read = drv_data->rx ? chip->read : bfin_spi_null_reader;
802 drv_data->duplex = chip->duplex ? chip->duplex : bfin_spi_null_writer;
Bryan Wu092e1fd2007-12-04 23:45:23 -0800803 break;
804 }
805 cr = (read_CTRL(drv_data) & (~BIT_CTL_TIMOD));
806 cr |= (width << 8);
807 write_CTRL(drv_data, cr);
808
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700809 if (width == CFG_SPI_WORDSIZE16) {
810 drv_data->len = (transfer->len) >> 1;
811 } else {
812 drv_data->len = transfer->len;
813 }
Mike Frysinger4fb98ef2008-04-08 17:41:57 -0700814 dev_dbg(&drv_data->pdev->dev,
815 "transfer: drv_data->write is %p, chip->write is %p, null_wr is %p\n",
Mike Frysinger138f97c2009-04-06 19:00:50 -0700816 drv_data->write, chip->write, bfin_spi_null_writer);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700817
818 /* speed and width has been set on per message */
819 message->state = RUNNING_STATE;
820 dma_config = 0;
821
Bryan Wu092e1fd2007-12-04 23:45:23 -0800822 /* Speed setup (surely valid because already checked) */
823 if (transfer->speed_hz)
824 write_BAUD(drv_data, hz_to_spi_baud(transfer->speed_hz));
825 else
826 write_BAUD(drv_data, chip->baud);
827
Bryan Wubb90eb02007-12-04 23:45:18 -0800828 write_STAT(drv_data, BIT_STAT_CLR);
829 cr = (read_CTRL(drv_data) & (~BIT_CTL_TIMOD));
Yi Lib9b2a762009-04-06 19:00:49 -0700830 if (drv_data->cs_change)
Mike Frysinger138f97c2009-04-06 19:00:50 -0700831 bfin_spi_cs_active(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700832
Bryan Wu88b40362007-05-21 18:32:16 +0800833 dev_dbg(&drv_data->pdev->dev,
834 "now pumping a transfer: width is %d, len is %d\n",
835 width, transfer->len);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700836
837 /*
Vitja Makarov8cf58582009-04-06 19:00:31 -0700838 * Try to map dma buffer and do a dma transfer. If successful use,
839 * different way to r/w according to the enable_dma settings and if
840 * we are not doing a full duplex transfer (since the hardware does
841 * not support full duplex DMA transfers).
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700842 */
Vitja Makarov8eeb12e2008-05-01 04:35:03 -0700843 if (!full_duplex && drv_data->cur_chip->enable_dma
844 && drv_data->len > 6) {
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700845
Mike Frysinger11d6f592009-04-06 19:00:41 -0700846 unsigned long dma_start_addr, flags;
Mike Frysinger7aec3562009-04-06 19:00:36 -0700847
Bryan Wubb90eb02007-12-04 23:45:18 -0800848 disable_dma(drv_data->dma_channel);
849 clear_dma_irqstat(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700850
851 /* config dma channel */
Bryan Wu88b40362007-05-21 18:32:16 +0800852 dev_dbg(&drv_data->pdev->dev, "doing dma transfer\n");
Mike Frysinger7aec3562009-04-06 19:00:36 -0700853 set_dma_x_count(drv_data->dma_channel, drv_data->len);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700854 if (width == CFG_SPI_WORDSIZE16) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800855 set_dma_x_modify(drv_data->dma_channel, 2);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700856 dma_width = WDSIZE_16;
857 } else {
Bryan Wubb90eb02007-12-04 23:45:18 -0800858 set_dma_x_modify(drv_data->dma_channel, 1);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700859 dma_width = WDSIZE_8;
860 }
861
Sonic Zhang3f479a62007-12-04 23:45:18 -0800862 /* poll for SPI completion before start */
Bryan Wubb90eb02007-12-04 23:45:18 -0800863 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Bryan Wud8c05002007-12-04 23:45:21 -0800864 cpu_relax();
Sonic Zhang3f479a62007-12-04 23:45:18 -0800865
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700866 /* dirty hack for autobuffer DMA mode */
867 if (drv_data->tx_dma == 0xFFFF) {
Bryan Wu88b40362007-05-21 18:32:16 +0800868 dev_dbg(&drv_data->pdev->dev,
869 "doing autobuffer DMA out.\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700870
871 /* no irq in autobuffer mode */
872 dma_config =
873 (DMAFLOW_AUTO | RESTART | dma_width | DI_EN);
Bryan Wubb90eb02007-12-04 23:45:18 -0800874 set_dma_config(drv_data->dma_channel, dma_config);
875 set_dma_start_addr(drv_data->dma_channel,
Bryan Wua32c6912007-12-04 23:45:15 -0800876 (unsigned long)drv_data->tx);
Bryan Wubb90eb02007-12-04 23:45:18 -0800877 enable_dma(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700878
Sonic Zhang07612e52007-12-04 23:45:21 -0800879 /* start SPI transfer */
Mike Frysinger11d6f592009-04-06 19:00:41 -0700880 write_CTRL(drv_data, cr | BIT_CTL_TIMOD_DMA_TX);
Sonic Zhang07612e52007-12-04 23:45:21 -0800881
882 /* just return here, there can only be one transfer
883 * in this mode
884 */
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700885 message->status = 0;
Mike Frysinger138f97c2009-04-06 19:00:50 -0700886 bfin_spi_giveback(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700887 return;
888 }
889
890 /* In dma mode, rx or tx must be NULL in one transfer */
Mike Frysinger7aec3562009-04-06 19:00:36 -0700891 dma_config = (RESTART | dma_width | DI_EN);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700892 if (drv_data->rx != NULL) {
893 /* set transfer mode, and enable SPI */
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700894 dev_dbg(&drv_data->pdev->dev, "doing DMA in to %p (size %zx)\n",
895 drv_data->rx, drv_data->len_in_bytes);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700896
Vitja Makarov8cf58582009-04-06 19:00:31 -0700897 /* invalidate caches, if needed */
Jie Zhang67834fa2009-06-10 06:26:26 +0000898 if (bfin_addr_dcacheable((unsigned long) drv_data->rx))
Vitja Makarov8cf58582009-04-06 19:00:31 -0700899 invalidate_dcache_range((unsigned long) drv_data->rx,
900 (unsigned long) (drv_data->rx +
Mike Frysingerace32862009-04-06 19:00:34 -0700901 drv_data->len_in_bytes));
Vitja Makarov8cf58582009-04-06 19:00:31 -0700902
Mike Frysinger7aec3562009-04-06 19:00:36 -0700903 dma_config |= WNR;
904 dma_start_addr = (unsigned long)drv_data->rx;
Mike Frysingerb31e27a2009-04-06 19:00:39 -0700905 cr |= BIT_CTL_TIMOD_DMA_RX | BIT_CTL_SENDOPT;
Sonic Zhang07612e52007-12-04 23:45:21 -0800906
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700907 } else if (drv_data->tx != NULL) {
Bryan Wu88b40362007-05-21 18:32:16 +0800908 dev_dbg(&drv_data->pdev->dev, "doing DMA out.\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700909
Vitja Makarov8cf58582009-04-06 19:00:31 -0700910 /* flush caches, if needed */
Jie Zhang67834fa2009-06-10 06:26:26 +0000911 if (bfin_addr_dcacheable((unsigned long) drv_data->tx))
Vitja Makarov8cf58582009-04-06 19:00:31 -0700912 flush_dcache_range((unsigned long) drv_data->tx,
913 (unsigned long) (drv_data->tx +
Mike Frysingerace32862009-04-06 19:00:34 -0700914 drv_data->len_in_bytes));
Vitja Makarov8cf58582009-04-06 19:00:31 -0700915
Mike Frysinger7aec3562009-04-06 19:00:36 -0700916 dma_start_addr = (unsigned long)drv_data->tx;
Mike Frysingerb31e27a2009-04-06 19:00:39 -0700917 cr |= BIT_CTL_TIMOD_DMA_TX;
Sonic Zhang07612e52007-12-04 23:45:21 -0800918
Mike Frysinger7aec3562009-04-06 19:00:36 -0700919 } else
920 BUG();
921
Mike Frysinger11d6f592009-04-06 19:00:41 -0700922 /* oh man, here there be monsters ... and i dont mean the
923 * fluffy cute ones from pixar, i mean the kind that'll eat
924 * your data, kick your dog, and love it all. do *not* try
925 * and change these lines unless you (1) heavily test DMA
926 * with SPI flashes on a loaded system (e.g. ping floods),
927 * (2) know just how broken the DMA engine interaction with
928 * the SPI peripheral is, and (3) have someone else to blame
929 * when you screw it all up anyways.
930 */
Mike Frysinger7aec3562009-04-06 19:00:36 -0700931 set_dma_start_addr(drv_data->dma_channel, dma_start_addr);
Mike Frysinger11d6f592009-04-06 19:00:41 -0700932 set_dma_config(drv_data->dma_channel, dma_config);
933 local_irq_save(flags);
Mike Frysingera963ea82009-04-06 19:00:43 -0700934 SSYNC();
Mike Frysinger11d6f592009-04-06 19:00:41 -0700935 write_CTRL(drv_data, cr);
Mike Frysingera963ea82009-04-06 19:00:43 -0700936 enable_dma(drv_data->dma_channel);
Mike Frysinger11d6f592009-04-06 19:00:41 -0700937 dma_enable_irq(drv_data->dma_channel);
938 local_irq_restore(flags);
Mike Frysinger7aec3562009-04-06 19:00:36 -0700939
Yi Lif6a6d962009-06-03 09:46:22 +0000940 return;
941 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700942
Yi Lif6a6d962009-06-03 09:46:22 +0000943 if (chip->pio_interrupt) {
944 /* use write mode. spi irq should have been disabled */
945 cr = (read_CTRL(drv_data) & (~BIT_CTL_TIMOD));
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700946 write_CTRL(drv_data, (cr | CFG_SPI_WRITE));
947
Yi Lif6a6d962009-06-03 09:46:22 +0000948 /* discard old RX data and clear RXS */
949 bfin_spi_dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700950
Yi Lif6a6d962009-06-03 09:46:22 +0000951 /* start transfer */
952 if (drv_data->tx == NULL)
953 write_TDBR(drv_data, chip->idle_tx_val);
954 else {
955 if (transfer->bits_per_word == 8)
956 write_TDBR(drv_data, (*(u8 *) (drv_data->tx)));
957 else if (transfer->bits_per_word == 16)
958 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
959 drv_data->tx += drv_data->n_bytes;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700960 }
961
Yi Lif6a6d962009-06-03 09:46:22 +0000962 /* once TDBR is empty, interrupt is triggered */
963 enable_irq(drv_data->spi_irq);
964 return;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700965 }
Yi Lif6a6d962009-06-03 09:46:22 +0000966
967 /* IO mode */
968 dev_dbg(&drv_data->pdev->dev, "doing IO transfer\n");
969
970 /* we always use SPI_WRITE mode. SPI_READ mode
971 seems to have problems with setting up the
972 output value in TDBR prior to the transfer. */
973 write_CTRL(drv_data, (cr | CFG_SPI_WRITE));
974
975 if (full_duplex) {
976 /* full duplex mode */
977 BUG_ON((drv_data->tx_end - drv_data->tx) !=
978 (drv_data->rx_end - drv_data->rx));
979 dev_dbg(&drv_data->pdev->dev,
980 "IO duplex: cr is 0x%x\n", cr);
981
982 drv_data->duplex(drv_data);
983
984 if (drv_data->tx != drv_data->tx_end)
985 tranf_success = 0;
986 } else if (drv_data->tx != NULL) {
987 /* write only half duplex */
988 dev_dbg(&drv_data->pdev->dev,
989 "IO write: cr is 0x%x\n", cr);
990
991 drv_data->write(drv_data);
992
993 if (drv_data->tx != drv_data->tx_end)
994 tranf_success = 0;
995 } else if (drv_data->rx != NULL) {
996 /* read only half duplex */
997 dev_dbg(&drv_data->pdev->dev,
998 "IO read: cr is 0x%x\n", cr);
999
1000 drv_data->read(drv_data);
1001 if (drv_data->rx != drv_data->rx_end)
1002 tranf_success = 0;
1003 }
1004
1005 if (!tranf_success) {
1006 dev_dbg(&drv_data->pdev->dev,
1007 "IO write error!\n");
1008 message->state = ERROR_STATE;
1009 } else {
1010 /* Update total byte transfered */
1011 message->actual_length += drv_data->len_in_bytes;
1012 /* Move to next transfer of this msg */
1013 message->state = bfin_spi_next_transfer(drv_data);
1014 if (drv_data->cs_change)
1015 bfin_spi_cs_deactive(drv_data, chip);
1016 }
1017
1018 /* Schedule next transfer tasklet */
1019 tasklet_schedule(&drv_data->pump_transfers);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001020}
1021
1022/* pop a msg from queue and kick off real transfer */
Mike Frysinger138f97c2009-04-06 19:00:50 -07001023static void bfin_spi_pump_messages(struct work_struct *work)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001024{
Bryan Wu131b17d2007-12-04 23:45:12 -08001025 struct driver_data *drv_data;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001026 unsigned long flags;
1027
Bryan Wu131b17d2007-12-04 23:45:12 -08001028 drv_data = container_of(work, struct driver_data, pump_messages);
1029
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001030 /* Lock queue and check for queue work */
1031 spin_lock_irqsave(&drv_data->lock, flags);
1032 if (list_empty(&drv_data->queue) || drv_data->run == QUEUE_STOPPED) {
1033 /* pumper kicked off but no work to do */
1034 drv_data->busy = 0;
1035 spin_unlock_irqrestore(&drv_data->lock, flags);
1036 return;
1037 }
1038
1039 /* Make sure we are not already running a message */
1040 if (drv_data->cur_msg) {
1041 spin_unlock_irqrestore(&drv_data->lock, flags);
1042 return;
1043 }
1044
1045 /* Extract head of queue */
1046 drv_data->cur_msg = list_entry(drv_data->queue.next,
1047 struct spi_message, queue);
Bryan Wu5fec5b52007-12-04 23:45:13 -08001048
1049 /* Setup the SSP using the per chip configuration */
1050 drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
Mike Frysinger138f97c2009-04-06 19:00:50 -07001051 bfin_spi_restore_state(drv_data);
Bryan Wu5fec5b52007-12-04 23:45:13 -08001052
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001053 list_del_init(&drv_data->cur_msg->queue);
1054
1055 /* Initial message state */
1056 drv_data->cur_msg->state = START_STATE;
1057 drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
1058 struct spi_transfer, transfer_list);
1059
Bryan Wu5fec5b52007-12-04 23:45:13 -08001060 dev_dbg(&drv_data->pdev->dev, "got a message to pump, "
1061 "state is set to: baud %d, flag 0x%x, ctl 0x%x\n",
1062 drv_data->cur_chip->baud, drv_data->cur_chip->flag,
1063 drv_data->cur_chip->ctl_reg);
Bryan Wu131b17d2007-12-04 23:45:12 -08001064
1065 dev_dbg(&drv_data->pdev->dev,
Bryan Wu88b40362007-05-21 18:32:16 +08001066 "the first transfer len is %d\n",
1067 drv_data->cur_transfer->len);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001068
1069 /* Mark as busy and launch transfers */
1070 tasklet_schedule(&drv_data->pump_transfers);
1071
1072 drv_data->busy = 1;
1073 spin_unlock_irqrestore(&drv_data->lock, flags);
1074}
1075
1076/*
1077 * got a msg to transfer, queue it in drv_data->queue.
1078 * And kick off message pumper
1079 */
Mike Frysinger138f97c2009-04-06 19:00:50 -07001080static int bfin_spi_transfer(struct spi_device *spi, struct spi_message *msg)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001081{
1082 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
1083 unsigned long flags;
1084
1085 spin_lock_irqsave(&drv_data->lock, flags);
1086
1087 if (drv_data->run == QUEUE_STOPPED) {
1088 spin_unlock_irqrestore(&drv_data->lock, flags);
1089 return -ESHUTDOWN;
1090 }
1091
1092 msg->actual_length = 0;
1093 msg->status = -EINPROGRESS;
1094 msg->state = START_STATE;
1095
Bryan Wu88b40362007-05-21 18:32:16 +08001096 dev_dbg(&spi->dev, "adding an msg in transfer() \n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001097 list_add_tail(&msg->queue, &drv_data->queue);
1098
1099 if (drv_data->run == QUEUE_RUNNING && !drv_data->busy)
1100 queue_work(drv_data->workqueue, &drv_data->pump_messages);
1101
1102 spin_unlock_irqrestore(&drv_data->lock, flags);
1103
1104 return 0;
1105}
1106
Sonic Zhang12e17c42007-12-04 23:45:16 -08001107#define MAX_SPI_SSEL 7
1108
Mike Frysinger4160bde2009-04-06 19:00:40 -07001109static u16 ssel[][MAX_SPI_SSEL] = {
Sonic Zhang12e17c42007-12-04 23:45:16 -08001110 {P_SPI0_SSEL1, P_SPI0_SSEL2, P_SPI0_SSEL3,
1111 P_SPI0_SSEL4, P_SPI0_SSEL5,
1112 P_SPI0_SSEL6, P_SPI0_SSEL7},
1113
1114 {P_SPI1_SSEL1, P_SPI1_SSEL2, P_SPI1_SSEL3,
1115 P_SPI1_SSEL4, P_SPI1_SSEL5,
1116 P_SPI1_SSEL6, P_SPI1_SSEL7},
1117
1118 {P_SPI2_SSEL1, P_SPI2_SSEL2, P_SPI2_SSEL3,
1119 P_SPI2_SSEL4, P_SPI2_SSEL5,
1120 P_SPI2_SSEL6, P_SPI2_SSEL7},
1121};
1122
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001123/* first setup for new devices */
Mike Frysinger138f97c2009-04-06 19:00:50 -07001124static int bfin_spi_setup(struct spi_device *spi)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001125{
Daniel Mackac01e972009-03-25 00:18:35 +00001126 struct bfin5xx_spi_chip *chip_info;
1127 struct chip_data *chip = NULL;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001128 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
Daniel Mackac01e972009-03-25 00:18:35 +00001129 int ret = -EINVAL;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001130
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001131 if (spi->bits_per_word != 8 && spi->bits_per_word != 16)
Daniel Mackac01e972009-03-25 00:18:35 +00001132 goto error;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001133
1134 /* Only alloc (or use chip_info) on first setup */
Daniel Mackac01e972009-03-25 00:18:35 +00001135 chip_info = NULL;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001136 chip = spi_get_ctldata(spi);
1137 if (chip == NULL) {
Daniel Mackac01e972009-03-25 00:18:35 +00001138 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1139 if (!chip) {
1140 dev_err(&spi->dev, "cannot allocate chip data\n");
1141 ret = -ENOMEM;
1142 goto error;
1143 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001144
1145 chip->enable_dma = 0;
1146 chip_info = spi->controller_data;
1147 }
1148
1149 /* chip_info isn't always needed */
1150 if (chip_info) {
Mike Frysinger2ed35512007-12-04 23:45:14 -08001151 /* Make sure people stop trying to set fields via ctl_reg
1152 * when they should actually be using common SPI framework.
1153 * Currently we let through: WOM EMISO PSSE GM SZ TIMOD.
1154 * Not sure if a user actually needs/uses any of these,
1155 * but let's assume (for now) they do.
1156 */
1157 if (chip_info->ctl_reg & (SPE|MSTR|CPOL|CPHA|LSBF|SIZE)) {
1158 dev_err(&spi->dev, "do not set bits in ctl_reg "
1159 "that the SPI framework manages\n");
Daniel Mackac01e972009-03-25 00:18:35 +00001160 goto error;
Mike Frysinger2ed35512007-12-04 23:45:14 -08001161 }
1162
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001163 chip->enable_dma = chip_info->enable_dma != 0
1164 && drv_data->master_info->enable_dma;
1165 chip->ctl_reg = chip_info->ctl_reg;
1166 chip->bits_per_word = chip_info->bits_per_word;
1167 chip->cs_change_per_word = chip_info->cs_change_per_word;
1168 chip->cs_chg_udelay = chip_info->cs_chg_udelay;
Michael Hennerich42c78b22009-04-06 19:00:51 -07001169 chip->cs_gpio = chip_info->cs_gpio;
Wolfgang Muees93b61bd2009-04-06 19:00:53 -07001170 chip->idle_tx_val = chip_info->idle_tx_val;
Yi Lif6a6d962009-06-03 09:46:22 +00001171 chip->pio_interrupt = chip_info->pio_interrupt;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001172 }
1173
1174 /* translate common spi framework into our register */
1175 if (spi->mode & SPI_CPOL)
1176 chip->ctl_reg |= CPOL;
1177 if (spi->mode & SPI_CPHA)
1178 chip->ctl_reg |= CPHA;
1179 if (spi->mode & SPI_LSB_FIRST)
1180 chip->ctl_reg |= LSBF;
1181 /* we dont support running in slave mode (yet?) */
1182 chip->ctl_reg |= MSTR;
1183
1184 /*
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001185 * Notice: for blackfin, the speed_hz is the value of register
1186 * SPI_BAUD, not the real baudrate
1187 */
1188 chip->baud = hz_to_spi_baud(spi->max_speed_hz);
Barry Song82216102009-06-17 10:10:53 +00001189 chip->flag = (1 << (spi->chip_select)) << 8;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001190 chip->chip_select_num = spi->chip_select;
1191
1192 switch (chip->bits_per_word) {
1193 case 8:
1194 chip->n_bytes = 1;
1195 chip->width = CFG_SPI_WORDSIZE8;
1196 chip->read = chip->cs_change_per_word ?
Mike Frysinger138f97c2009-04-06 19:00:50 -07001197 bfin_spi_u8_cs_chg_reader : bfin_spi_u8_reader;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001198 chip->write = chip->cs_change_per_word ?
Mike Frysinger138f97c2009-04-06 19:00:50 -07001199 bfin_spi_u8_cs_chg_writer : bfin_spi_u8_writer;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001200 chip->duplex = chip->cs_change_per_word ?
Mike Frysinger138f97c2009-04-06 19:00:50 -07001201 bfin_spi_u8_cs_chg_duplex : bfin_spi_u8_duplex;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001202 break;
1203
1204 case 16:
1205 chip->n_bytes = 2;
1206 chip->width = CFG_SPI_WORDSIZE16;
1207 chip->read = chip->cs_change_per_word ?
Mike Frysinger138f97c2009-04-06 19:00:50 -07001208 bfin_spi_u16_cs_chg_reader : bfin_spi_u16_reader;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001209 chip->write = chip->cs_change_per_word ?
Mike Frysinger138f97c2009-04-06 19:00:50 -07001210 bfin_spi_u16_cs_chg_writer : bfin_spi_u16_writer;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001211 chip->duplex = chip->cs_change_per_word ?
Mike Frysinger138f97c2009-04-06 19:00:50 -07001212 bfin_spi_u16_cs_chg_duplex : bfin_spi_u16_duplex;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001213 break;
1214
1215 default:
1216 dev_err(&spi->dev, "%d bits_per_word is not supported\n",
1217 chip->bits_per_word);
Daniel Mackac01e972009-03-25 00:18:35 +00001218 goto error;
1219 }
1220
Yi Lif6a6d962009-06-03 09:46:22 +00001221 if (chip->enable_dma && chip->pio_interrupt) {
1222 dev_err(&spi->dev, "enable_dma is set, "
1223 "do not set pio_interrupt\n");
1224 goto error;
1225 }
Daniel Mackac01e972009-03-25 00:18:35 +00001226 /*
1227 * if any one SPI chip is registered and wants DMA, request the
1228 * DMA channel for it
1229 */
1230 if (chip->enable_dma && !drv_data->dma_requested) {
1231 /* register dma irq handler */
1232 ret = request_dma(drv_data->dma_channel, "BFIN_SPI_DMA");
1233 if (ret) {
1234 dev_err(&spi->dev,
1235 "Unable to request BlackFin SPI DMA channel\n");
1236 goto error;
1237 }
1238 drv_data->dma_requested = 1;
1239
1240 ret = set_dma_callback(drv_data->dma_channel,
1241 bfin_spi_dma_irq_handler, drv_data);
1242 if (ret) {
1243 dev_err(&spi->dev, "Unable to set dma callback\n");
1244 goto error;
1245 }
1246 dma_disable_irq(drv_data->dma_channel);
1247 }
1248
Yi Lif6a6d962009-06-03 09:46:22 +00001249 if (chip->pio_interrupt && !drv_data->irq_requested) {
1250 ret = request_irq(drv_data->spi_irq, bfin_spi_pio_irq_handler,
1251 IRQF_DISABLED, "BFIN_SPI", drv_data);
1252 if (ret) {
1253 dev_err(&spi->dev, "Unable to register spi IRQ\n");
1254 goto error;
1255 }
1256 drv_data->irq_requested = 1;
1257 /* we use write mode, spi irq has to be disabled here */
1258 disable_irq(drv_data->spi_irq);
1259 }
1260
Daniel Mackac01e972009-03-25 00:18:35 +00001261 if (chip->chip_select_num == 0) {
1262 ret = gpio_request(chip->cs_gpio, spi->modalias);
1263 if (ret) {
1264 dev_err(&spi->dev, "gpio_request() error\n");
1265 goto pin_error;
1266 }
1267 gpio_direction_output(chip->cs_gpio, 1);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001268 }
1269
Joe Perches898eb712007-10-18 03:06:30 -07001270 dev_dbg(&spi->dev, "setup spi chip %s, width is %d, dma is %d\n",
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001271 spi->modalias, chip->width, chip->enable_dma);
Bryan Wu88b40362007-05-21 18:32:16 +08001272 dev_dbg(&spi->dev, "ctl_reg is 0x%x, flag_reg is 0x%x\n",
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001273 chip->ctl_reg, chip->flag);
1274
1275 spi_set_ctldata(spi, chip);
1276
Sonic Zhang12e17c42007-12-04 23:45:16 -08001277 dev_dbg(&spi->dev, "chip select number is %d\n", chip->chip_select_num);
Daniel Mackac01e972009-03-25 00:18:35 +00001278 if (chip->chip_select_num > 0 &&
1279 chip->chip_select_num <= spi->master->num_chipselect) {
1280 ret = peripheral_request(ssel[spi->master->bus_num]
1281 [chip->chip_select_num-1], spi->modalias);
1282 if (ret) {
1283 dev_err(&spi->dev, "peripheral_request() error\n");
1284 goto pin_error;
1285 }
1286 }
Sonic Zhang12e17c42007-12-04 23:45:16 -08001287
Barry Song82216102009-06-17 10:10:53 +00001288 bfin_spi_cs_enable(drv_data, chip);
Mike Frysinger138f97c2009-04-06 19:00:50 -07001289 bfin_spi_cs_deactive(drv_data, chip);
Sonic Zhang07612e52007-12-04 23:45:21 -08001290
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001291 return 0;
Daniel Mackac01e972009-03-25 00:18:35 +00001292
1293 pin_error:
1294 if (chip->chip_select_num == 0)
1295 gpio_free(chip->cs_gpio);
1296 else
1297 peripheral_free(ssel[spi->master->bus_num]
1298 [chip->chip_select_num - 1]);
1299 error:
1300 if (chip) {
1301 if (drv_data->dma_requested)
1302 free_dma(drv_data->dma_channel);
1303 drv_data->dma_requested = 0;
1304
1305 kfree(chip);
1306 /* prevent free 'chip' twice */
1307 spi_set_ctldata(spi, NULL);
1308 }
1309
1310 return ret;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001311}
1312
1313/*
1314 * callback for spi framework.
1315 * clean driver specific data
1316 */
Mike Frysinger138f97c2009-04-06 19:00:50 -07001317static void bfin_spi_cleanup(struct spi_device *spi)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001318{
Mike Frysinger27bb9e72007-06-11 15:31:30 +08001319 struct chip_data *chip = spi_get_ctldata(spi);
Barry Song82216102009-06-17 10:10:53 +00001320 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001321
Mike Frysingere7d02e32009-04-06 19:00:51 -07001322 if (!chip)
1323 return;
1324
Sonic Zhang12e17c42007-12-04 23:45:16 -08001325 if ((chip->chip_select_num > 0)
Barry Song82216102009-06-17 10:10:53 +00001326 && (chip->chip_select_num <= spi->master->num_chipselect)) {
Sonic Zhang12e17c42007-12-04 23:45:16 -08001327 peripheral_free(ssel[spi->master->bus_num]
1328 [chip->chip_select_num-1]);
Barry Song82216102009-06-17 10:10:53 +00001329 bfin_spi_cs_disable(drv_data, chip);
1330 }
Sonic Zhang12e17c42007-12-04 23:45:16 -08001331
Michael Hennerich42c78b22009-04-06 19:00:51 -07001332 if (chip->chip_select_num == 0)
1333 gpio_free(chip->cs_gpio);
1334
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001335 kfree(chip);
Daniel Mackac01e972009-03-25 00:18:35 +00001336 /* prevent free 'chip' twice */
1337 spi_set_ctldata(spi, NULL);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001338}
1339
Mike Frysinger138f97c2009-04-06 19:00:50 -07001340static inline int bfin_spi_init_queue(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001341{
1342 INIT_LIST_HEAD(&drv_data->queue);
1343 spin_lock_init(&drv_data->lock);
1344
1345 drv_data->run = QUEUE_STOPPED;
1346 drv_data->busy = 0;
1347
1348 /* init transfer tasklet */
1349 tasklet_init(&drv_data->pump_transfers,
Mike Frysinger138f97c2009-04-06 19:00:50 -07001350 bfin_spi_pump_transfers, (unsigned long)drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001351
1352 /* init messages workqueue */
Mike Frysinger138f97c2009-04-06 19:00:50 -07001353 INIT_WORK(&drv_data->pump_messages, bfin_spi_pump_messages);
Kay Sievers6c7377a2009-03-24 16:38:21 -07001354 drv_data->workqueue = create_singlethread_workqueue(
1355 dev_name(drv_data->master->dev.parent));
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001356 if (drv_data->workqueue == NULL)
1357 return -EBUSY;
1358
1359 return 0;
1360}
1361
Mike Frysinger138f97c2009-04-06 19:00:50 -07001362static inline int bfin_spi_start_queue(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001363{
1364 unsigned long flags;
1365
1366 spin_lock_irqsave(&drv_data->lock, flags);
1367
1368 if (drv_data->run == QUEUE_RUNNING || drv_data->busy) {
1369 spin_unlock_irqrestore(&drv_data->lock, flags);
1370 return -EBUSY;
1371 }
1372
1373 drv_data->run = QUEUE_RUNNING;
1374 drv_data->cur_msg = NULL;
1375 drv_data->cur_transfer = NULL;
1376 drv_data->cur_chip = NULL;
1377 spin_unlock_irqrestore(&drv_data->lock, flags);
1378
1379 queue_work(drv_data->workqueue, &drv_data->pump_messages);
1380
1381 return 0;
1382}
1383
Mike Frysinger138f97c2009-04-06 19:00:50 -07001384static inline int bfin_spi_stop_queue(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001385{
1386 unsigned long flags;
1387 unsigned limit = 500;
1388 int status = 0;
1389
1390 spin_lock_irqsave(&drv_data->lock, flags);
1391
1392 /*
1393 * This is a bit lame, but is optimized for the common execution path.
1394 * A wait_queue on the drv_data->busy could be used, but then the common
1395 * execution path (pump_messages) would be required to call wake_up or
1396 * friends on every SPI message. Do this instead
1397 */
1398 drv_data->run = QUEUE_STOPPED;
1399 while (!list_empty(&drv_data->queue) && drv_data->busy && limit--) {
1400 spin_unlock_irqrestore(&drv_data->lock, flags);
1401 msleep(10);
1402 spin_lock_irqsave(&drv_data->lock, flags);
1403 }
1404
1405 if (!list_empty(&drv_data->queue) || drv_data->busy)
1406 status = -EBUSY;
1407
1408 spin_unlock_irqrestore(&drv_data->lock, flags);
1409
1410 return status;
1411}
1412
Mike Frysinger138f97c2009-04-06 19:00:50 -07001413static inline int bfin_spi_destroy_queue(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001414{
1415 int status;
1416
Mike Frysinger138f97c2009-04-06 19:00:50 -07001417 status = bfin_spi_stop_queue(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001418 if (status != 0)
1419 return status;
1420
1421 destroy_workqueue(drv_data->workqueue);
1422
1423 return 0;
1424}
1425
Mike Frysinger138f97c2009-04-06 19:00:50 -07001426static int __init bfin_spi_probe(struct platform_device *pdev)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001427{
1428 struct device *dev = &pdev->dev;
1429 struct bfin5xx_spi_master *platform_info;
1430 struct spi_master *master;
1431 struct driver_data *drv_data = 0;
Bryan Wua32c6912007-12-04 23:45:15 -08001432 struct resource *res;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001433 int status = 0;
1434
1435 platform_info = dev->platform_data;
1436
1437 /* Allocate master with space for drv_data */
1438 master = spi_alloc_master(dev, sizeof(struct driver_data) + 16);
1439 if (!master) {
1440 dev_err(&pdev->dev, "can not alloc spi_master\n");
1441 return -ENOMEM;
1442 }
Bryan Wu131b17d2007-12-04 23:45:12 -08001443
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001444 drv_data = spi_master_get_devdata(master);
1445 drv_data->master = master;
1446 drv_data->master_info = platform_info;
1447 drv_data->pdev = pdev;
Bryan Wu003d9222007-12-04 23:45:22 -08001448 drv_data->pin_req = platform_info->pin_req;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001449
David Brownelle7db06b2009-06-17 16:26:04 -07001450 /* the spi->mode bits supported by this driver: */
1451 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST;
1452
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001453 master->bus_num = pdev->id;
1454 master->num_chipselect = platform_info->num_chipselect;
Mike Frysinger138f97c2009-04-06 19:00:50 -07001455 master->cleanup = bfin_spi_cleanup;
1456 master->setup = bfin_spi_setup;
1457 master->transfer = bfin_spi_transfer;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001458
Bryan Wua32c6912007-12-04 23:45:15 -08001459 /* Find and map our resources */
1460 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1461 if (res == NULL) {
1462 dev_err(dev, "Cannot get IORESOURCE_MEM\n");
1463 status = -ENOENT;
1464 goto out_error_get_res;
1465 }
1466
hartleys74947b82009-12-14 22:33:43 +00001467 drv_data->regs_base = ioremap(res->start, resource_size(res));
Bryan Wuf4521262007-12-04 23:45:22 -08001468 if (drv_data->regs_base == NULL) {
Bryan Wua32c6912007-12-04 23:45:15 -08001469 dev_err(dev, "Cannot map IO\n");
1470 status = -ENXIO;
1471 goto out_error_ioremap;
1472 }
1473
Yi Lif6a6d962009-06-03 09:46:22 +00001474 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1475 if (res == NULL) {
Bryan Wua32c6912007-12-04 23:45:15 -08001476 dev_err(dev, "No DMA channel specified\n");
1477 status = -ENOENT;
Yi Lif6a6d962009-06-03 09:46:22 +00001478 goto out_error_free_io;
1479 }
1480 drv_data->dma_channel = res->start;
1481
1482 drv_data->spi_irq = platform_get_irq(pdev, 0);
1483 if (drv_data->spi_irq < 0) {
1484 dev_err(dev, "No spi pio irq specified\n");
1485 status = -ENOENT;
1486 goto out_error_free_io;
Bryan Wua32c6912007-12-04 23:45:15 -08001487 }
1488
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001489 /* Initial and start queue */
Mike Frysinger138f97c2009-04-06 19:00:50 -07001490 status = bfin_spi_init_queue(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001491 if (status != 0) {
Bryan Wua32c6912007-12-04 23:45:15 -08001492 dev_err(dev, "problem initializing queue\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001493 goto out_error_queue_alloc;
1494 }
Bryan Wua32c6912007-12-04 23:45:15 -08001495
Mike Frysinger138f97c2009-04-06 19:00:50 -07001496 status = bfin_spi_start_queue(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001497 if (status != 0) {
Bryan Wua32c6912007-12-04 23:45:15 -08001498 dev_err(dev, "problem starting queue\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001499 goto out_error_queue_alloc;
1500 }
1501
Vitja Makarovf9e522c2008-04-08 17:41:57 -07001502 status = peripheral_request_list(drv_data->pin_req, DRV_NAME);
1503 if (status != 0) {
1504 dev_err(&pdev->dev, ": Requesting Peripherals failed\n");
1505 goto out_error_queue_alloc;
1506 }
1507
Wolfgang Mueesbb8beec2009-05-22 01:11:02 +00001508 /* Reset SPI registers. If these registers were used by the boot loader,
1509 * the sky may fall on your head if you enable the dma controller.
1510 */
1511 write_CTRL(drv_data, BIT_CTL_CPHA | BIT_CTL_MASTER);
1512 write_FLAG(drv_data, 0xFF00);
1513
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001514 /* Register with the SPI framework */
1515 platform_set_drvdata(pdev, drv_data);
1516 status = spi_register_master(master);
1517 if (status != 0) {
Bryan Wua32c6912007-12-04 23:45:15 -08001518 dev_err(dev, "problem registering spi master\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001519 goto out_error_queue_alloc;
1520 }
Bryan Wua32c6912007-12-04 23:45:15 -08001521
Bryan Wuf4521262007-12-04 23:45:22 -08001522 dev_info(dev, "%s, Version %s, regs_base@%p, dma channel@%d\n",
Bryan Wubb90eb02007-12-04 23:45:18 -08001523 DRV_DESC, DRV_VERSION, drv_data->regs_base,
1524 drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001525 return status;
1526
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001527out_error_queue_alloc:
Mike Frysinger138f97c2009-04-06 19:00:50 -07001528 bfin_spi_destroy_queue(drv_data);
Yi Lif6a6d962009-06-03 09:46:22 +00001529out_error_free_io:
Bryan Wubb90eb02007-12-04 23:45:18 -08001530 iounmap((void *) drv_data->regs_base);
Bryan Wua32c6912007-12-04 23:45:15 -08001531out_error_ioremap:
1532out_error_get_res:
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001533 spi_master_put(master);
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001534
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001535 return status;
1536}
1537
1538/* stop hardware and remove the driver */
Mike Frysinger138f97c2009-04-06 19:00:50 -07001539static int __devexit bfin_spi_remove(struct platform_device *pdev)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001540{
1541 struct driver_data *drv_data = platform_get_drvdata(pdev);
1542 int status = 0;
1543
1544 if (!drv_data)
1545 return 0;
1546
1547 /* Remove the queue */
Mike Frysinger138f97c2009-04-06 19:00:50 -07001548 status = bfin_spi_destroy_queue(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001549 if (status != 0)
1550 return status;
1551
1552 /* Disable the SSP at the peripheral and SOC level */
1553 bfin_spi_disable(drv_data);
1554
1555 /* Release DMA */
1556 if (drv_data->master_info->enable_dma) {
Bryan Wubb90eb02007-12-04 23:45:18 -08001557 if (dma_channel_active(drv_data->dma_channel))
1558 free_dma(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001559 }
1560
Yi Lif6a6d962009-06-03 09:46:22 +00001561 if (drv_data->irq_requested) {
1562 free_irq(drv_data->spi_irq, drv_data);
1563 drv_data->irq_requested = 0;
1564 }
1565
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001566 /* Disconnect from the SPI framework */
1567 spi_unregister_master(drv_data->master);
1568
Bryan Wu003d9222007-12-04 23:45:22 -08001569 peripheral_free_list(drv_data->pin_req);
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001570
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001571 /* Prevent double remove */
1572 platform_set_drvdata(pdev, NULL);
1573
1574 return 0;
1575}
1576
1577#ifdef CONFIG_PM
Mike Frysinger138f97c2009-04-06 19:00:50 -07001578static int bfin_spi_suspend(struct platform_device *pdev, pm_message_t state)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001579{
1580 struct driver_data *drv_data = platform_get_drvdata(pdev);
1581 int status = 0;
1582
Mike Frysinger138f97c2009-04-06 19:00:50 -07001583 status = bfin_spi_stop_queue(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001584 if (status != 0)
1585 return status;
1586
1587 /* stop hardware */
1588 bfin_spi_disable(drv_data);
1589
1590 return 0;
1591}
1592
Mike Frysinger138f97c2009-04-06 19:00:50 -07001593static int bfin_spi_resume(struct platform_device *pdev)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001594{
1595 struct driver_data *drv_data = platform_get_drvdata(pdev);
1596 int status = 0;
1597
1598 /* Enable the SPI interface */
1599 bfin_spi_enable(drv_data);
1600
1601 /* Start the queue running */
Mike Frysinger138f97c2009-04-06 19:00:50 -07001602 status = bfin_spi_start_queue(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001603 if (status != 0) {
1604 dev_err(&pdev->dev, "problem starting queue (%d)\n", status);
1605 return status;
1606 }
1607
1608 return 0;
1609}
1610#else
Mike Frysinger138f97c2009-04-06 19:00:50 -07001611#define bfin_spi_suspend NULL
1612#define bfin_spi_resume NULL
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001613#endif /* CONFIG_PM */
1614
Kay Sievers7e38c3c2008-04-10 21:29:20 -07001615MODULE_ALIAS("platform:bfin-spi");
Mike Frysinger138f97c2009-04-06 19:00:50 -07001616static struct platform_driver bfin_spi_driver = {
David Brownellfc3ba952007-08-30 23:56:24 -07001617 .driver = {
Bryan Wua32c6912007-12-04 23:45:15 -08001618 .name = DRV_NAME,
Bryan Wu88b40362007-05-21 18:32:16 +08001619 .owner = THIS_MODULE,
1620 },
Mike Frysinger138f97c2009-04-06 19:00:50 -07001621 .suspend = bfin_spi_suspend,
1622 .resume = bfin_spi_resume,
1623 .remove = __devexit_p(bfin_spi_remove),
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001624};
1625
Mike Frysinger138f97c2009-04-06 19:00:50 -07001626static int __init bfin_spi_init(void)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001627{
Mike Frysinger138f97c2009-04-06 19:00:50 -07001628 return platform_driver_probe(&bfin_spi_driver, bfin_spi_probe);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001629}
Mike Frysinger138f97c2009-04-06 19:00:50 -07001630module_init(bfin_spi_init);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001631
Mike Frysinger138f97c2009-04-06 19:00:50 -07001632static void __exit bfin_spi_exit(void)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001633{
Mike Frysinger138f97c2009-04-06 19:00:50 -07001634 platform_driver_unregister(&bfin_spi_driver);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001635}
Mike Frysinger138f97c2009-04-06 19:00:50 -07001636module_exit(bfin_spi_exit);