blob: a28462486df8c6f53eabd609a01e18cc8d361314 [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 *
Mike Frysinger9c0a7882010-10-18 02:45:22 -04004 * Copyright 2004-2010 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)
Wu, Bryana5f6abd2007-05-06 14:50:34 -070044
Mike Frysinger9c0a7882010-10-18 02:45:22 -040045struct bfin_spi_master_data;
Mike Frysinger9c4542c2009-09-24 01:04:04 +000046
Mike Frysinger9c0a7882010-10-18 02:45:22 -040047struct bfin_spi_transfer_ops {
48 void (*write) (struct bfin_spi_master_data *);
49 void (*read) (struct bfin_spi_master_data *);
50 void (*duplex) (struct bfin_spi_master_data *);
Mike Frysinger9c4542c2009-09-24 01:04:04 +000051};
52
Mike Frysinger9c0a7882010-10-18 02:45:22 -040053struct bfin_spi_master_data {
Wu, Bryana5f6abd2007-05-06 14:50:34 -070054 /* Driver model hookup */
55 struct platform_device *pdev;
56
57 /* SPI framework hookup */
58 struct spi_master *master;
59
Bryan Wubb90eb02007-12-04 23:45:18 -080060 /* Regs base of SPI controller */
Bryan Wuf4521262007-12-04 23:45:22 -080061 void __iomem *regs_base;
Bryan Wubb90eb02007-12-04 23:45:18 -080062
Bryan Wu003d9222007-12-04 23:45:22 -080063 /* Pin request list */
64 u16 *pin_req;
65
Wu, Bryana5f6abd2007-05-06 14:50:34 -070066 /* BFIN hookup */
67 struct bfin5xx_spi_master *master_info;
68
69 /* Driver message queue */
70 struct workqueue_struct *workqueue;
71 struct work_struct pump_messages;
72 spinlock_t lock;
73 struct list_head queue;
74 int busy;
Mike Frysingerf4f50c32009-09-24 00:41:49 +000075 bool running;
Wu, Bryana5f6abd2007-05-06 14:50:34 -070076
77 /* Message Transfer pump */
78 struct tasklet_struct pump_transfers;
79
80 /* Current message transfer state info */
81 struct spi_message *cur_msg;
82 struct spi_transfer *cur_transfer;
Mike Frysinger9c0a7882010-10-18 02:45:22 -040083 struct bfin_spi_slave_data *cur_chip;
Wu, Bryana5f6abd2007-05-06 14:50:34 -070084 size_t len_in_bytes;
85 size_t len;
86 void *tx;
87 void *tx_end;
88 void *rx;
89 void *rx_end;
Bryan Wubb90eb02007-12-04 23:45:18 -080090
91 /* DMA stuffs */
92 int dma_channel;
Wu, Bryana5f6abd2007-05-06 14:50:34 -070093 int dma_mapped;
Bryan Wubb90eb02007-12-04 23:45:18 -080094 int dma_requested;
Wu, Bryana5f6abd2007-05-06 14:50:34 -070095 dma_addr_t rx_dma;
96 dma_addr_t tx_dma;
Bryan Wubb90eb02007-12-04 23:45:18 -080097
Yi Lif6a6d962009-06-03 09:46:22 +000098 int irq_requested;
99 int spi_irq;
100
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700101 size_t rx_map_len;
102 size_t tx_map_len;
103 u8 n_bytes;
Barry Songb052fd02009-11-18 09:43:21 +0000104 u16 ctrl_reg;
105 u16 flag_reg;
106
Bryan Wufad91c82007-12-04 23:45:14 -0800107 int cs_change;
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400108 const struct bfin_spi_transfer_ops *ops;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700109};
110
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400111struct bfin_spi_slave_data {
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700112 u16 ctl_reg;
113 u16 baud;
114 u16 flag;
115
116 u8 chip_select_num;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700117 u8 enable_dma;
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 */
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400122 const struct bfin_spi_transfer_ops *ops;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700123};
124
Bryan Wubb90eb02007-12-04 23:45:18 -0800125#define DEFINE_SPI_REG(reg, off) \
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400126static inline u16 read_##reg(struct bfin_spi_master_data *drv_data) \
Bryan Wubb90eb02007-12-04 23:45:18 -0800127 { return bfin_read16(drv_data->regs_base + off); } \
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400128static inline void write_##reg(struct bfin_spi_master_data *drv_data, u16 v) \
Bryan Wubb90eb02007-12-04 23:45:18 -0800129 { bfin_write16(drv_data->regs_base + off, v); }
130
131DEFINE_SPI_REG(CTRL, 0x00)
132DEFINE_SPI_REG(FLAG, 0x04)
133DEFINE_SPI_REG(STAT, 0x08)
134DEFINE_SPI_REG(TDBR, 0x0C)
135DEFINE_SPI_REG(RDBR, 0x10)
136DEFINE_SPI_REG(BAUD, 0x14)
137DEFINE_SPI_REG(SHAW, 0x18)
138
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400139static void bfin_spi_enable(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700140{
141 u16 cr;
142
Bryan Wubb90eb02007-12-04 23:45:18 -0800143 cr = read_CTRL(drv_data);
144 write_CTRL(drv_data, (cr | BIT_CTL_ENABLE));
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700145}
146
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400147static void bfin_spi_disable(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700148{
149 u16 cr;
150
Bryan Wubb90eb02007-12-04 23:45:18 -0800151 cr = read_CTRL(drv_data);
152 write_CTRL(drv_data, (cr & (~BIT_CTL_ENABLE)));
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700153}
154
155/* Caculate the SPI_BAUD register value based on input HZ */
156static u16 hz_to_spi_baud(u32 speed_hz)
157{
158 u_long sclk = get_sclk();
159 u16 spi_baud = (sclk / (2 * speed_hz));
160
161 if ((sclk % (2 * speed_hz)) > 0)
162 spi_baud++;
163
Michael Hennerich7513e002009-04-06 19:00:32 -0700164 if (spi_baud < MIN_SPI_BAUD_VAL)
165 spi_baud = MIN_SPI_BAUD_VAL;
166
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700167 return spi_baud;
168}
169
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400170static int bfin_spi_flush(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700171{
172 unsigned long limit = loops_per_jiffy << 1;
173
174 /* wait for stop and clear stat */
Roel Kluinb4bd2ab2009-06-17 16:26:02 -0700175 while (!(read_STAT(drv_data) & BIT_STAT_SPIF) && --limit)
Bryan Wud8c05002007-12-04 23:45:21 -0800176 cpu_relax();
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700177
Bryan Wubb90eb02007-12-04 23:45:18 -0800178 write_STAT(drv_data, BIT_STAT_CLR);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700179
180 return limit;
181}
182
Bryan Wufad91c82007-12-04 23:45:14 -0800183/* Chip select operation functions for cs_change flag */
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400184static void bfin_spi_cs_active(struct bfin_spi_master_data *drv_data, struct bfin_spi_slave_data *chip)
Bryan Wufad91c82007-12-04 23:45:14 -0800185{
Barry Songd3cc71f2009-11-17 09:45:59 +0000186 if (likely(chip->chip_select_num < MAX_CTRL_CS)) {
Michael Hennerich42c78b22009-04-06 19:00:51 -0700187 u16 flag = read_FLAG(drv_data);
Bryan Wufad91c82007-12-04 23:45:14 -0800188
Barry Song82216102009-06-17 10:10:53 +0000189 flag &= ~chip->flag;
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 Frysinger9c0a7882010-10-18 02:45:22 -0400197static void bfin_spi_cs_deactive(struct bfin_spi_master_data *drv_data,
198 struct bfin_spi_slave_data *chip)
Bryan Wufad91c82007-12-04 23:45:14 -0800199{
Barry Songd3cc71f2009-11-17 09:45:59 +0000200 if (likely(chip->chip_select_num < MAX_CTRL_CS)) {
Michael Hennerich42c78b22009-04-06 19:00:51 -0700201 u16 flag = read_FLAG(drv_data);
Bryan Wufad91c82007-12-04 23:45:14 -0800202
Barry Song82216102009-06-17 10:10:53 +0000203 flag |= chip->flag;
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
Barry Song82216102009-06-17 10:10:53 +0000215/* enable or disable the pin muxed by GPIO and SPI CS to work as SPI CS */
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400216static inline void bfin_spi_cs_enable(struct bfin_spi_master_data *drv_data,
217 struct bfin_spi_slave_data *chip)
Barry Song82216102009-06-17 10:10:53 +0000218{
Barry Songd3cc71f2009-11-17 09:45:59 +0000219 if (chip->chip_select_num < MAX_CTRL_CS) {
220 u16 flag = read_FLAG(drv_data);
Barry Song82216102009-06-17 10:10:53 +0000221
Barry Songd3cc71f2009-11-17 09:45:59 +0000222 flag |= (chip->flag >> 8);
Barry Song82216102009-06-17 10:10:53 +0000223
Barry Songd3cc71f2009-11-17 09:45:59 +0000224 write_FLAG(drv_data, flag);
225 }
Barry Song82216102009-06-17 10:10:53 +0000226}
227
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400228static inline void bfin_spi_cs_disable(struct bfin_spi_master_data *drv_data,
229 struct bfin_spi_slave_data *chip)
Barry Song82216102009-06-17 10:10:53 +0000230{
Barry Songd3cc71f2009-11-17 09:45:59 +0000231 if (chip->chip_select_num < MAX_CTRL_CS) {
232 u16 flag = read_FLAG(drv_data);
Barry Song82216102009-06-17 10:10:53 +0000233
Barry Songd3cc71f2009-11-17 09:45:59 +0000234 flag &= ~(chip->flag >> 8);
Barry Song82216102009-06-17 10:10:53 +0000235
Barry Songd3cc71f2009-11-17 09:45:59 +0000236 write_FLAG(drv_data, flag);
237 }
Barry Song82216102009-06-17 10:10:53 +0000238}
239
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700240/* stop controller and re-config current chip*/
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400241static void bfin_spi_restore_state(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700242{
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400243 struct bfin_spi_slave_data *chip = drv_data->cur_chip;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700244
245 /* Clear status and disable clock */
Bryan Wubb90eb02007-12-04 23:45:18 -0800246 write_STAT(drv_data, BIT_STAT_CLR);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700247 bfin_spi_disable(drv_data);
Bryan Wu88b40362007-05-21 18:32:16 +0800248 dev_dbg(&drv_data->pdev->dev, "restoring spi ctl state\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700249
Barry Song9677b0de2009-11-30 03:49:41 +0000250 SSYNC();
251
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700252 /* Load the registers */
Bryan Wubb90eb02007-12-04 23:45:18 -0800253 write_CTRL(drv_data, chip->ctl_reg);
Bryan Wu092e1fd2007-12-04 23:45:23 -0800254 write_BAUD(drv_data, chip->baud);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800255
256 bfin_spi_enable(drv_data);
Mike Frysinger138f97c2009-04-06 19:00:50 -0700257 bfin_spi_cs_active(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700258}
259
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700260/* used to kick off transfer in rx mode and read unwanted RX data */
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400261static inline void bfin_spi_dummy_read(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700262{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700263 (void) read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700264}
265
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400266static void bfin_spi_u8_writer(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700267{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700268 /* clear RXS (we check for RXS inside the loop) */
269 bfin_spi_dummy_read(drv_data);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800270
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700271 while (drv_data->tx < drv_data->tx_end) {
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700272 write_TDBR(drv_data, (*(u8 *) (drv_data->tx++)));
273 /* wait until transfer finished.
274 checking SPIF or TXS may not guarantee transfer completion */
275 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800276 cpu_relax();
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700277 /* discard RX data and clear RXS */
278 bfin_spi_dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700279 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700280}
281
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400282static void bfin_spi_u8_reader(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700283{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700284 u16 tx_val = drv_data->cur_chip->idle_tx_val;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700285
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700286 /* discard old RX data and clear RXS */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700287 bfin_spi_dummy_read(drv_data);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800288
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700289 while (drv_data->rx < drv_data->rx_end) {
290 write_TDBR(drv_data, tx_val);
Bryan Wubb90eb02007-12-04 23:45:18 -0800291 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800292 cpu_relax();
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700293 *(u8 *) (drv_data->rx++) = read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700294 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700295}
296
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400297static void bfin_spi_u8_duplex(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700298{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700299 /* discard old RX data and clear RXS */
300 bfin_spi_dummy_read(drv_data);
301
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700302 while (drv_data->rx < drv_data->rx_end) {
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700303 write_TDBR(drv_data, (*(u8 *) (drv_data->tx++)));
Bryan Wubb90eb02007-12-04 23:45:18 -0800304 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800305 cpu_relax();
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700306 *(u8 *) (drv_data->rx++) = read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700307 }
308}
309
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400310static const struct bfin_spi_transfer_ops bfin_bfin_spi_transfer_ops_u8 = {
Mike Frysinger9c4542c2009-09-24 01:04:04 +0000311 .write = bfin_spi_u8_writer,
312 .read = bfin_spi_u8_reader,
313 .duplex = bfin_spi_u8_duplex,
314};
315
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400316static void bfin_spi_u16_writer(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700317{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700318 /* clear RXS (we check for RXS inside the loop) */
319 bfin_spi_dummy_read(drv_data);
Bryan Wu88b40362007-05-21 18:32:16 +0800320
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700321 while (drv_data->tx < drv_data->tx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800322 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700323 drv_data->tx += 2;
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700324 /* wait until transfer finished.
325 checking SPIF or TXS may not guarantee transfer completion */
326 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
327 cpu_relax();
328 /* discard RX data and clear RXS */
329 bfin_spi_dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700330 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700331}
332
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400333static void bfin_spi_u16_reader(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700334{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700335 u16 tx_val = drv_data->cur_chip->idle_tx_val;
Sonic Zhangcc487e72007-12-04 23:45:17 -0800336
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700337 /* discard old RX data and clear RXS */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700338 bfin_spi_dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700339
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700340 while (drv_data->rx < drv_data->rx_end) {
341 write_TDBR(drv_data, tx_val);
Bryan Wubb90eb02007-12-04 23:45:18 -0800342 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800343 cpu_relax();
Bryan Wubb90eb02007-12-04 23:45:18 -0800344 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700345 drv_data->rx += 2;
346 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700347}
348
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400349static void bfin_spi_u16_duplex(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700350{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700351 /* discard old RX data and clear RXS */
352 bfin_spi_dummy_read(drv_data);
353
354 while (drv_data->rx < drv_data->rx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800355 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700356 drv_data->tx += 2;
Bryan Wubb90eb02007-12-04 23:45:18 -0800357 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800358 cpu_relax();
Bryan Wubb90eb02007-12-04 23:45:18 -0800359 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700360 drv_data->rx += 2;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700361 }
362}
363
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400364static const struct bfin_spi_transfer_ops bfin_bfin_spi_transfer_ops_u16 = {
Mike Frysinger9c4542c2009-09-24 01:04:04 +0000365 .write = bfin_spi_u16_writer,
366 .read = bfin_spi_u16_reader,
367 .duplex = bfin_spi_u16_duplex,
368};
369
Rob Marise3595402010-04-06 04:12:00 +0000370/* test if there is more transfer to be done */
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400371static void *bfin_spi_next_transfer(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700372{
373 struct spi_message *msg = drv_data->cur_msg;
374 struct spi_transfer *trans = drv_data->cur_transfer;
375
376 /* Move to next transfer */
377 if (trans->transfer_list.next != &msg->transfers) {
378 drv_data->cur_transfer =
379 list_entry(trans->transfer_list.next,
380 struct spi_transfer, transfer_list);
381 return RUNNING_STATE;
382 } else
383 return DONE_STATE;
384}
385
386/*
387 * caller already set message->status;
388 * dma and pio irqs are blocked give finished message back
389 */
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400390static void bfin_spi_giveback(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700391{
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400392 struct bfin_spi_slave_data *chip = drv_data->cur_chip;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700393 struct spi_transfer *last_transfer;
394 unsigned long flags;
395 struct spi_message *msg;
396
397 spin_lock_irqsave(&drv_data->lock, flags);
398 msg = drv_data->cur_msg;
399 drv_data->cur_msg = NULL;
400 drv_data->cur_transfer = NULL;
401 drv_data->cur_chip = NULL;
402 queue_work(drv_data->workqueue, &drv_data->pump_messages);
403 spin_unlock_irqrestore(&drv_data->lock, flags);
404
405 last_transfer = list_entry(msg->transfers.prev,
406 struct spi_transfer, transfer_list);
407
408 msg->state = NULL;
409
Bryan Wufad91c82007-12-04 23:45:14 -0800410 if (!drv_data->cs_change)
Mike Frysinger138f97c2009-04-06 19:00:50 -0700411 bfin_spi_cs_deactive(drv_data, chip);
Bryan Wufad91c82007-12-04 23:45:14 -0800412
Yi Lib9b2a762009-04-06 19:00:49 -0700413 /* Not stop spi in autobuffer mode */
414 if (drv_data->tx_dma != 0xFFFF)
415 bfin_spi_disable(drv_data);
416
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700417 if (msg->complete)
418 msg->complete(msg->context);
419}
420
Yi Lif6a6d962009-06-03 09:46:22 +0000421/* spi data irq handler */
422static irqreturn_t bfin_spi_pio_irq_handler(int irq, void *dev_id)
423{
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400424 struct bfin_spi_master_data *drv_data = dev_id;
425 struct bfin_spi_slave_data *chip = drv_data->cur_chip;
Yi Lif6a6d962009-06-03 09:46:22 +0000426 struct spi_message *msg = drv_data->cur_msg;
427 int n_bytes = drv_data->n_bytes;
Bob Liu4d676fc2011-01-11 11:19:07 -0500428 int loop = 0;
Yi Lif6a6d962009-06-03 09:46:22 +0000429
430 /* wait until transfer finished. */
431 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
432 cpu_relax();
433
434 if ((drv_data->tx && drv_data->tx >= drv_data->tx_end) ||
435 (drv_data->rx && drv_data->rx >= (drv_data->rx_end - n_bytes))) {
436 /* last read */
437 if (drv_data->rx) {
438 dev_dbg(&drv_data->pdev->dev, "last read\n");
Bob Liu4d676fc2011-01-11 11:19:07 -0500439 if (n_bytes % 2) {
440 u16 *buf = (u16 *)drv_data->rx;
441 for (loop = 0; loop < n_bytes / 2; loop++)
442 *buf++ = read_RDBR(drv_data);
443 } else {
444 u8 *buf = (u8 *)drv_data->rx;
445 for (loop = 0; loop < n_bytes; loop++)
446 *buf++ = read_RDBR(drv_data);
447 }
Yi Lif6a6d962009-06-03 09:46:22 +0000448 drv_data->rx += n_bytes;
449 }
450
451 msg->actual_length += drv_data->len_in_bytes;
452 if (drv_data->cs_change)
453 bfin_spi_cs_deactive(drv_data, chip);
454 /* Move to next transfer */
455 msg->state = bfin_spi_next_transfer(drv_data);
456
Yi Li7370ed62009-12-07 08:07:01 +0000457 disable_irq_nosync(drv_data->spi_irq);
Yi Lif6a6d962009-06-03 09:46:22 +0000458
459 /* Schedule transfer tasklet */
460 tasklet_schedule(&drv_data->pump_transfers);
461 return IRQ_HANDLED;
462 }
463
464 if (drv_data->rx && drv_data->tx) {
465 /* duplex */
466 dev_dbg(&drv_data->pdev->dev, "duplex: write_TDBR\n");
Bob Liu4d676fc2011-01-11 11:19:07 -0500467 if (n_bytes % 2) {
468 u16 *buf = (u16 *)drv_data->rx;
469 u16 *buf2 = (u16 *)drv_data->tx;
470 for (loop = 0; loop < n_bytes / 2; loop++) {
471 *buf++ = read_RDBR(drv_data);
472 write_TDBR(drv_data, *buf2++);
473 }
474 } else {
475 u8 *buf = (u8 *)drv_data->rx;
476 u8 *buf2 = (u8 *)drv_data->tx;
477 for (loop = 0; loop < n_bytes; loop++) {
478 *buf++ = read_RDBR(drv_data);
479 write_TDBR(drv_data, *buf2++);
480 }
Yi Lif6a6d962009-06-03 09:46:22 +0000481 }
482 } else if (drv_data->rx) {
483 /* read */
484 dev_dbg(&drv_data->pdev->dev, "read: write_TDBR\n");
Bob Liu4d676fc2011-01-11 11:19:07 -0500485 if (n_bytes % 2) {
486 u16 *buf = (u16 *)drv_data->rx;
487 for (loop = 0; loop < n_bytes / 2; loop++) {
488 *buf++ = read_RDBR(drv_data);
489 write_TDBR(drv_data, chip->idle_tx_val);
490 }
491 } else {
492 u8 *buf = (u8 *)drv_data->rx;
493 for (loop = 0; loop < n_bytes; loop++) {
494 *buf++ = read_RDBR(drv_data);
495 write_TDBR(drv_data, chip->idle_tx_val);
496 }
497 }
Yi Lif6a6d962009-06-03 09:46:22 +0000498 } else if (drv_data->tx) {
499 /* write */
500 dev_dbg(&drv_data->pdev->dev, "write: write_TDBR\n");
Bob Liu4d676fc2011-01-11 11:19:07 -0500501 if (n_bytes % 2) {
502 u16 *buf = (u16 *)drv_data->tx;
503 for (loop = 0; loop < n_bytes / 2; loop++) {
504 read_RDBR(drv_data);
505 write_TDBR(drv_data, *buf++);
506 }
507 } else {
508 u8 *buf = (u8 *)drv_data->tx;
509 for (loop = 0; loop < n_bytes; loop++) {
510 read_RDBR(drv_data);
511 write_TDBR(drv_data, *buf++);
512 }
513 }
Yi Lif6a6d962009-06-03 09:46:22 +0000514 }
515
516 if (drv_data->tx)
517 drv_data->tx += n_bytes;
518 if (drv_data->rx)
519 drv_data->rx += n_bytes;
520
521 return IRQ_HANDLED;
522}
523
Mike Frysinger138f97c2009-04-06 19:00:50 -0700524static irqreturn_t bfin_spi_dma_irq_handler(int irq, void *dev_id)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700525{
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400526 struct bfin_spi_master_data *drv_data = dev_id;
527 struct bfin_spi_slave_data *chip = drv_data->cur_chip;
Bryan Wubb90eb02007-12-04 23:45:18 -0800528 struct spi_message *msg = drv_data->cur_msg;
Mike Frysingeraaaf9392009-04-06 19:00:42 -0700529 unsigned long timeout;
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700530 unsigned short dmastat = get_dma_curr_irqstat(drv_data->dma_channel);
Mike Frysinger04b95d22009-04-06 19:00:35 -0700531 u16 spistat = read_STAT(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700532
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700533 dev_dbg(&drv_data->pdev->dev,
534 "in dma_irq_handler dmastat:0x%x spistat:0x%x\n",
535 dmastat, spistat);
536
Michael Hennerich782a8952010-10-22 02:01:48 -0400537 if (drv_data->rx != NULL) {
538 u16 cr = read_CTRL(drv_data);
539 /* discard old RX data and clear RXS */
540 bfin_spi_dummy_read(drv_data);
541 write_CTRL(drv_data, cr & ~BIT_CTL_ENABLE); /* Disable SPI */
542 write_CTRL(drv_data, cr & ~BIT_CTL_TIMOD); /* Restore State */
543 write_STAT(drv_data, BIT_STAT_CLR); /* Clear Status */
544 }
545
Bryan Wubb90eb02007-12-04 23:45:18 -0800546 clear_dma_irqstat(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700547
548 /*
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) {
Mike Frysinger90008a62009-10-15 04:13:29 +0000555 while ((read_STAT(drv_data) & BIT_STAT_TXS) ||
556 (read_STAT(drv_data) & BIT_STAT_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;
Mike Frysinger90008a62009-10-15 04:13:29 +0000565 while (!(read_STAT(drv_data) & BIT_STAT_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 Frysinger90008a62009-10-15 04:13:29 +0000572 if ((dmastat & DMA_ERR) && (spistat & BIT_STAT_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);
Barry Songa75bd652010-01-22 10:07:30 +0000592 dma_disable_irq_nosync(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{
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400599 struct bfin_spi_master_data *drv_data = (struct bfin_spi_master_data *)data;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700600 struct spi_message *message = NULL;
601 struct spi_transfer *transfer = NULL;
602 struct spi_transfer *previous = NULL;
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400603 struct bfin_spi_slave_data *chip = NULL;
Mike Frysinger033f44b2009-12-18 17:38:04 +0000604 unsigned int bits_per_word;
Mike Frysinger5e8592d2009-12-18 18:00:10 +0000605 u16 cr, cr_width, 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
Mike Frysingerab09e042009-09-23 23:32:34 +0000643 /* Flush any existing transfers that may be sitting in the hardware */
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);
Sonic Zhang1974eba2011-01-11 11:19:08 -0500656 return;
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700657 }
658
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700659 if (transfer->tx_buf != NULL) {
660 drv_data->tx = (void *)transfer->tx_buf;
661 drv_data->tx_end = drv_data->tx + transfer->len;
Bryan Wu88b40362007-05-21 18:32:16 +0800662 dev_dbg(&drv_data->pdev->dev, "tx_buf is %p, tx_end is %p\n",
663 transfer->tx_buf, drv_data->tx_end);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700664 } else {
665 drv_data->tx = NULL;
666 }
667
668 if (transfer->rx_buf != NULL) {
Vitja Makarov8eeb12e2008-05-01 04:35:03 -0700669 full_duplex = transfer->tx_buf != NULL;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700670 drv_data->rx = transfer->rx_buf;
671 drv_data->rx_end = drv_data->rx + transfer->len;
Bryan Wu88b40362007-05-21 18:32:16 +0800672 dev_dbg(&drv_data->pdev->dev, "rx_buf is %p, rx_end is %p\n",
673 transfer->rx_buf, drv_data->rx_end);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700674 } else {
675 drv_data->rx = NULL;
676 }
677
678 drv_data->rx_dma = transfer->rx_dma;
679 drv_data->tx_dma = transfer->tx_dma;
680 drv_data->len_in_bytes = transfer->len;
Bryan Wufad91c82007-12-04 23:45:14 -0800681 drv_data->cs_change = transfer->cs_change;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700682
Bryan Wu092e1fd2007-12-04 23:45:23 -0800683 /* Bits per word setup */
Mike Frysinger033f44b2009-12-18 17:38:04 +0000684 bits_per_word = transfer->bits_per_word ? : message->spi->bits_per_word;
Bob Liu4d676fc2011-01-11 11:19:07 -0500685 if ((bits_per_word > 0) && (bits_per_word % 16 == 0)) {
686 drv_data->n_bytes = bits_per_word/8;
Mike Frysinger5e8592d2009-12-18 18:00:10 +0000687 drv_data->len = (transfer->len) >> 1;
688 cr_width = BIT_CTL_WORDSIZE;
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400689 drv_data->ops = &bfin_bfin_spi_transfer_ops_u16;
Bob Liu4d676fc2011-01-11 11:19:07 -0500690 } else if ((bits_per_word > 0) && (bits_per_word % 8 == 0)) {
691 drv_data->n_bytes = bits_per_word/8;
692 drv_data->len = transfer->len;
693 cr_width = 0;
694 drv_data->ops = &bfin_bfin_spi_transfer_ops_u8;
Bob Liu2e768652010-09-17 03:46:22 +0000695 } else {
696 dev_err(&drv_data->pdev->dev, "transfer: unsupported bits_per_word\n");
697 message->status = -EINVAL;
698 bfin_spi_giveback(drv_data);
699 return;
Bryan Wu092e1fd2007-12-04 23:45:23 -0800700 }
Mike Frysinger5e8592d2009-12-18 18:00:10 +0000701 cr = read_CTRL(drv_data) & ~(BIT_CTL_TIMOD | BIT_CTL_WORDSIZE);
702 cr |= cr_width;
Bryan Wu092e1fd2007-12-04 23:45:23 -0800703 write_CTRL(drv_data, cr);
704
Mike Frysinger4fb98ef2008-04-08 17:41:57 -0700705 dev_dbg(&drv_data->pdev->dev,
Mike Frysinger9c4542c2009-09-24 01:04:04 +0000706 "transfer: drv_data->ops is %p, chip->ops is %p, u8_ops is %p\n",
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400707 drv_data->ops, chip->ops, &bfin_bfin_spi_transfer_ops_u8);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700708
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700709 message->state = RUNNING_STATE;
710 dma_config = 0;
711
Bryan Wu092e1fd2007-12-04 23:45:23 -0800712 /* Speed setup (surely valid because already checked) */
713 if (transfer->speed_hz)
714 write_BAUD(drv_data, hz_to_spi_baud(transfer->speed_hz));
715 else
716 write_BAUD(drv_data, chip->baud);
717
Bryan Wubb90eb02007-12-04 23:45:18 -0800718 write_STAT(drv_data, BIT_STAT_CLR);
Rob Marise72dcde2010-04-06 04:17:08 +0000719 bfin_spi_cs_active(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700720
Bryan Wu88b40362007-05-21 18:32:16 +0800721 dev_dbg(&drv_data->pdev->dev,
722 "now pumping a transfer: width is %d, len is %d\n",
Mike Frysinger5e8592d2009-12-18 18:00:10 +0000723 cr_width, transfer->len);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700724
725 /*
Vitja Makarov8cf58582009-04-06 19:00:31 -0700726 * Try to map dma buffer and do a dma transfer. If successful use,
727 * different way to r/w according to the enable_dma settings and if
728 * we are not doing a full duplex transfer (since the hardware does
729 * not support full duplex DMA transfers).
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700730 */
Vitja Makarov8eeb12e2008-05-01 04:35:03 -0700731 if (!full_duplex && drv_data->cur_chip->enable_dma
732 && drv_data->len > 6) {
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700733
Mike Frysinger11d6f592009-04-06 19:00:41 -0700734 unsigned long dma_start_addr, flags;
Mike Frysinger7aec3562009-04-06 19:00:36 -0700735
Bryan Wubb90eb02007-12-04 23:45:18 -0800736 disable_dma(drv_data->dma_channel);
737 clear_dma_irqstat(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700738
739 /* config dma channel */
Bryan Wu88b40362007-05-21 18:32:16 +0800740 dev_dbg(&drv_data->pdev->dev, "doing dma transfer\n");
Mike Frysinger7aec3562009-04-06 19:00:36 -0700741 set_dma_x_count(drv_data->dma_channel, drv_data->len);
Mike Frysinger5e8592d2009-12-18 18:00:10 +0000742 if (cr_width == BIT_CTL_WORDSIZE) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800743 set_dma_x_modify(drv_data->dma_channel, 2);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700744 dma_width = WDSIZE_16;
745 } else {
Bryan Wubb90eb02007-12-04 23:45:18 -0800746 set_dma_x_modify(drv_data->dma_channel, 1);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700747 dma_width = WDSIZE_8;
748 }
749
Sonic Zhang3f479a62007-12-04 23:45:18 -0800750 /* poll for SPI completion before start */
Bryan Wubb90eb02007-12-04 23:45:18 -0800751 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Bryan Wud8c05002007-12-04 23:45:21 -0800752 cpu_relax();
Sonic Zhang3f479a62007-12-04 23:45:18 -0800753
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700754 /* dirty hack for autobuffer DMA mode */
755 if (drv_data->tx_dma == 0xFFFF) {
Bryan Wu88b40362007-05-21 18:32:16 +0800756 dev_dbg(&drv_data->pdev->dev,
757 "doing autobuffer DMA out.\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700758
759 /* no irq in autobuffer mode */
760 dma_config =
761 (DMAFLOW_AUTO | RESTART | dma_width | DI_EN);
Bryan Wubb90eb02007-12-04 23:45:18 -0800762 set_dma_config(drv_data->dma_channel, dma_config);
763 set_dma_start_addr(drv_data->dma_channel,
Bryan Wua32c6912007-12-04 23:45:15 -0800764 (unsigned long)drv_data->tx);
Bryan Wubb90eb02007-12-04 23:45:18 -0800765 enable_dma(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700766
Sonic Zhang07612e52007-12-04 23:45:21 -0800767 /* start SPI transfer */
Mike Frysinger11d6f592009-04-06 19:00:41 -0700768 write_CTRL(drv_data, cr | BIT_CTL_TIMOD_DMA_TX);
Sonic Zhang07612e52007-12-04 23:45:21 -0800769
770 /* just return here, there can only be one transfer
771 * in this mode
772 */
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700773 message->status = 0;
Mike Frysinger138f97c2009-04-06 19:00:50 -0700774 bfin_spi_giveback(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700775 return;
776 }
777
778 /* In dma mode, rx or tx must be NULL in one transfer */
Mike Frysinger7aec3562009-04-06 19:00:36 -0700779 dma_config = (RESTART | dma_width | DI_EN);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700780 if (drv_data->rx != NULL) {
781 /* set transfer mode, and enable SPI */
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700782 dev_dbg(&drv_data->pdev->dev, "doing DMA in to %p (size %zx)\n",
783 drv_data->rx, drv_data->len_in_bytes);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700784
Vitja Makarov8cf58582009-04-06 19:00:31 -0700785 /* invalidate caches, if needed */
Jie Zhang67834fa2009-06-10 06:26:26 +0000786 if (bfin_addr_dcacheable((unsigned long) drv_data->rx))
Vitja Makarov8cf58582009-04-06 19:00:31 -0700787 invalidate_dcache_range((unsigned long) drv_data->rx,
788 (unsigned long) (drv_data->rx +
Mike Frysingerace32862009-04-06 19:00:34 -0700789 drv_data->len_in_bytes));
Vitja Makarov8cf58582009-04-06 19:00:31 -0700790
Mike Frysinger7aec3562009-04-06 19:00:36 -0700791 dma_config |= WNR;
792 dma_start_addr = (unsigned long)drv_data->rx;
Mike Frysingerb31e27a2009-04-06 19:00:39 -0700793 cr |= BIT_CTL_TIMOD_DMA_RX | BIT_CTL_SENDOPT;
Sonic Zhang07612e52007-12-04 23:45:21 -0800794
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700795 } else if (drv_data->tx != NULL) {
Bryan Wu88b40362007-05-21 18:32:16 +0800796 dev_dbg(&drv_data->pdev->dev, "doing DMA out.\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700797
Vitja Makarov8cf58582009-04-06 19:00:31 -0700798 /* flush caches, if needed */
Jie Zhang67834fa2009-06-10 06:26:26 +0000799 if (bfin_addr_dcacheable((unsigned long) drv_data->tx))
Vitja Makarov8cf58582009-04-06 19:00:31 -0700800 flush_dcache_range((unsigned long) drv_data->tx,
801 (unsigned long) (drv_data->tx +
Mike Frysingerace32862009-04-06 19:00:34 -0700802 drv_data->len_in_bytes));
Vitja Makarov8cf58582009-04-06 19:00:31 -0700803
Mike Frysinger7aec3562009-04-06 19:00:36 -0700804 dma_start_addr = (unsigned long)drv_data->tx;
Mike Frysingerb31e27a2009-04-06 19:00:39 -0700805 cr |= BIT_CTL_TIMOD_DMA_TX;
Sonic Zhang07612e52007-12-04 23:45:21 -0800806
Mike Frysinger7aec3562009-04-06 19:00:36 -0700807 } else
808 BUG();
809
Mike Frysinger11d6f592009-04-06 19:00:41 -0700810 /* oh man, here there be monsters ... and i dont mean the
811 * fluffy cute ones from pixar, i mean the kind that'll eat
812 * your data, kick your dog, and love it all. do *not* try
813 * and change these lines unless you (1) heavily test DMA
814 * with SPI flashes on a loaded system (e.g. ping floods),
815 * (2) know just how broken the DMA engine interaction with
816 * the SPI peripheral is, and (3) have someone else to blame
817 * when you screw it all up anyways.
818 */
Mike Frysinger7aec3562009-04-06 19:00:36 -0700819 set_dma_start_addr(drv_data->dma_channel, dma_start_addr);
Mike Frysinger11d6f592009-04-06 19:00:41 -0700820 set_dma_config(drv_data->dma_channel, dma_config);
821 local_irq_save(flags);
Mike Frysingera963ea82009-04-06 19:00:43 -0700822 SSYNC();
Mike Frysinger11d6f592009-04-06 19:00:41 -0700823 write_CTRL(drv_data, cr);
Mike Frysingera963ea82009-04-06 19:00:43 -0700824 enable_dma(drv_data->dma_channel);
Mike Frysinger11d6f592009-04-06 19:00:41 -0700825 dma_enable_irq(drv_data->dma_channel);
826 local_irq_restore(flags);
Mike Frysinger7aec3562009-04-06 19:00:36 -0700827
Yi Lif6a6d962009-06-03 09:46:22 +0000828 return;
829 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700830
Mike Frysinger5e8592d2009-12-18 18:00:10 +0000831 /*
832 * We always use SPI_WRITE mode (transfer starts with TDBR write).
833 * SPI_READ mode (transfer starts with RDBR read) seems to have
834 * problems with setting up the output value in TDBR prior to the
835 * start of the transfer.
836 */
837 write_CTRL(drv_data, cr | BIT_CTL_TXMOD);
838
Yi Lif6a6d962009-06-03 09:46:22 +0000839 if (chip->pio_interrupt) {
Mike Frysinger5e8592d2009-12-18 18:00:10 +0000840 /* SPI irq should have been disabled by now */
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700841
Yi Lif6a6d962009-06-03 09:46:22 +0000842 /* discard old RX data and clear RXS */
843 bfin_spi_dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700844
Yi Lif6a6d962009-06-03 09:46:22 +0000845 /* start transfer */
846 if (drv_data->tx == NULL)
847 write_TDBR(drv_data, chip->idle_tx_val);
848 else {
Bob Liu4d676fc2011-01-11 11:19:07 -0500849 int loop;
850 if (bits_per_word % 16 == 0) {
851 u16 *buf = (u16 *)drv_data->tx;
852 for (loop = 0; loop < bits_per_word / 16;
853 loop++) {
854 write_TDBR(drv_data, *buf++);
855 }
856 } else if (bits_per_word % 8 == 0) {
857 u8 *buf = (u8 *)drv_data->tx;
858 for (loop = 0; loop < bits_per_word / 8; loop++)
859 write_TDBR(drv_data, *buf++);
860 }
861
Yi Lif6a6d962009-06-03 09:46:22 +0000862 drv_data->tx += drv_data->n_bytes;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700863 }
864
Yi Lif6a6d962009-06-03 09:46:22 +0000865 /* once TDBR is empty, interrupt is triggered */
866 enable_irq(drv_data->spi_irq);
867 return;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700868 }
Yi Lif6a6d962009-06-03 09:46:22 +0000869
870 /* IO mode */
871 dev_dbg(&drv_data->pdev->dev, "doing IO transfer\n");
872
Yi Lif6a6d962009-06-03 09:46:22 +0000873 if (full_duplex) {
874 /* full duplex mode */
875 BUG_ON((drv_data->tx_end - drv_data->tx) !=
876 (drv_data->rx_end - drv_data->rx));
877 dev_dbg(&drv_data->pdev->dev,
878 "IO duplex: cr is 0x%x\n", cr);
879
Mike Frysinger9c4542c2009-09-24 01:04:04 +0000880 drv_data->ops->duplex(drv_data);
Yi Lif6a6d962009-06-03 09:46:22 +0000881
882 if (drv_data->tx != drv_data->tx_end)
883 tranf_success = 0;
884 } else if (drv_data->tx != NULL) {
885 /* write only half duplex */
886 dev_dbg(&drv_data->pdev->dev,
887 "IO write: cr is 0x%x\n", cr);
888
Mike Frysinger9c4542c2009-09-24 01:04:04 +0000889 drv_data->ops->write(drv_data);
Yi Lif6a6d962009-06-03 09:46:22 +0000890
891 if (drv_data->tx != drv_data->tx_end)
892 tranf_success = 0;
893 } else if (drv_data->rx != NULL) {
894 /* read only half duplex */
895 dev_dbg(&drv_data->pdev->dev,
896 "IO read: cr is 0x%x\n", cr);
897
Mike Frysinger9c4542c2009-09-24 01:04:04 +0000898 drv_data->ops->read(drv_data);
Yi Lif6a6d962009-06-03 09:46:22 +0000899 if (drv_data->rx != drv_data->rx_end)
900 tranf_success = 0;
901 }
902
903 if (!tranf_success) {
904 dev_dbg(&drv_data->pdev->dev,
905 "IO write error!\n");
906 message->state = ERROR_STATE;
907 } else {
908 /* Update total byte transfered */
909 message->actual_length += drv_data->len_in_bytes;
910 /* Move to next transfer of this msg */
911 message->state = bfin_spi_next_transfer(drv_data);
912 if (drv_data->cs_change)
913 bfin_spi_cs_deactive(drv_data, chip);
914 }
915
916 /* Schedule next transfer tasklet */
917 tasklet_schedule(&drv_data->pump_transfers);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700918}
919
920/* pop a msg from queue and kick off real transfer */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700921static void bfin_spi_pump_messages(struct work_struct *work)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700922{
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400923 struct bfin_spi_master_data *drv_data;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700924 unsigned long flags;
925
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400926 drv_data = container_of(work, struct bfin_spi_master_data, pump_messages);
Bryan Wu131b17d2007-12-04 23:45:12 -0800927
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700928 /* Lock queue and check for queue work */
929 spin_lock_irqsave(&drv_data->lock, flags);
Mike Frysingerf4f50c32009-09-24 00:41:49 +0000930 if (list_empty(&drv_data->queue) || !drv_data->running) {
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700931 /* pumper kicked off but no work to do */
932 drv_data->busy = 0;
933 spin_unlock_irqrestore(&drv_data->lock, flags);
934 return;
935 }
936
937 /* Make sure we are not already running a message */
938 if (drv_data->cur_msg) {
939 spin_unlock_irqrestore(&drv_data->lock, flags);
940 return;
941 }
942
943 /* Extract head of queue */
944 drv_data->cur_msg = list_entry(drv_data->queue.next,
945 struct spi_message, queue);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800946
947 /* Setup the SSP using the per chip configuration */
948 drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
Mike Frysinger138f97c2009-04-06 19:00:50 -0700949 bfin_spi_restore_state(drv_data);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800950
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700951 list_del_init(&drv_data->cur_msg->queue);
952
953 /* Initial message state */
954 drv_data->cur_msg->state = START_STATE;
955 drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
956 struct spi_transfer, transfer_list);
957
Bryan Wu5fec5b52007-12-04 23:45:13 -0800958 dev_dbg(&drv_data->pdev->dev, "got a message to pump, "
959 "state is set to: baud %d, flag 0x%x, ctl 0x%x\n",
960 drv_data->cur_chip->baud, drv_data->cur_chip->flag,
961 drv_data->cur_chip->ctl_reg);
Bryan Wu131b17d2007-12-04 23:45:12 -0800962
963 dev_dbg(&drv_data->pdev->dev,
Bryan Wu88b40362007-05-21 18:32:16 +0800964 "the first transfer len is %d\n",
965 drv_data->cur_transfer->len);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700966
967 /* Mark as busy and launch transfers */
968 tasklet_schedule(&drv_data->pump_transfers);
969
970 drv_data->busy = 1;
971 spin_unlock_irqrestore(&drv_data->lock, flags);
972}
973
974/*
975 * got a msg to transfer, queue it in drv_data->queue.
976 * And kick off message pumper
977 */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700978static int bfin_spi_transfer(struct spi_device *spi, struct spi_message *msg)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700979{
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400980 struct bfin_spi_master_data *drv_data = spi_master_get_devdata(spi->master);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700981 unsigned long flags;
982
983 spin_lock_irqsave(&drv_data->lock, flags);
984
Mike Frysingerf4f50c32009-09-24 00:41:49 +0000985 if (!drv_data->running) {
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700986 spin_unlock_irqrestore(&drv_data->lock, flags);
987 return -ESHUTDOWN;
988 }
989
990 msg->actual_length = 0;
991 msg->status = -EINPROGRESS;
992 msg->state = START_STATE;
993
Bryan Wu88b40362007-05-21 18:32:16 +0800994 dev_dbg(&spi->dev, "adding an msg in transfer() \n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700995 list_add_tail(&msg->queue, &drv_data->queue);
996
Mike Frysingerf4f50c32009-09-24 00:41:49 +0000997 if (drv_data->running && !drv_data->busy)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700998 queue_work(drv_data->workqueue, &drv_data->pump_messages);
999
1000 spin_unlock_irqrestore(&drv_data->lock, flags);
1001
1002 return 0;
1003}
1004
Sonic Zhang12e17c42007-12-04 23:45:16 -08001005#define MAX_SPI_SSEL 7
1006
Mike Frysinger4160bde2009-04-06 19:00:40 -07001007static u16 ssel[][MAX_SPI_SSEL] = {
Sonic Zhang12e17c42007-12-04 23:45:16 -08001008 {P_SPI0_SSEL1, P_SPI0_SSEL2, P_SPI0_SSEL3,
1009 P_SPI0_SSEL4, P_SPI0_SSEL5,
1010 P_SPI0_SSEL6, P_SPI0_SSEL7},
1011
1012 {P_SPI1_SSEL1, P_SPI1_SSEL2, P_SPI1_SSEL3,
1013 P_SPI1_SSEL4, P_SPI1_SSEL5,
1014 P_SPI1_SSEL6, P_SPI1_SSEL7},
1015
1016 {P_SPI2_SSEL1, P_SPI2_SSEL2, P_SPI2_SSEL3,
1017 P_SPI2_SSEL4, P_SPI2_SSEL5,
1018 P_SPI2_SSEL6, P_SPI2_SSEL7},
1019};
1020
Mike Frysingerab09e042009-09-23 23:32:34 +00001021/* setup for devices (may be called multiple times -- not just first setup) */
Mike Frysinger138f97c2009-04-06 19:00:50 -07001022static int bfin_spi_setup(struct spi_device *spi)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001023{
Daniel Mackac01e972009-03-25 00:18:35 +00001024 struct bfin5xx_spi_chip *chip_info;
Mike Frysinger9c0a7882010-10-18 02:45:22 -04001025 struct bfin_spi_slave_data *chip = NULL;
1026 struct bfin_spi_master_data *drv_data = spi_master_get_devdata(spi->master);
Mike Frysinger5b47bcd2009-12-18 17:43:31 +00001027 u16 bfin_ctl_reg;
Daniel Mackac01e972009-03-25 00:18:35 +00001028 int ret = -EINVAL;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001029
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001030 /* Only alloc (or use chip_info) on first setup */
Daniel Mackac01e972009-03-25 00:18:35 +00001031 chip_info = NULL;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001032 chip = spi_get_ctldata(spi);
1033 if (chip == NULL) {
Daniel Mackac01e972009-03-25 00:18:35 +00001034 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1035 if (!chip) {
1036 dev_err(&spi->dev, "cannot allocate chip data\n");
1037 ret = -ENOMEM;
1038 goto error;
1039 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001040
1041 chip->enable_dma = 0;
1042 chip_info = spi->controller_data;
1043 }
1044
Mike Frysinger5b47bcd2009-12-18 17:43:31 +00001045 /* Let people set non-standard bits directly */
1046 bfin_ctl_reg = BIT_CTL_OPENDRAIN | BIT_CTL_EMISO |
1047 BIT_CTL_PSSE | BIT_CTL_GM | BIT_CTL_SZ;
1048
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001049 /* chip_info isn't always needed */
1050 if (chip_info) {
Mike Frysinger2ed35512007-12-04 23:45:14 -08001051 /* Make sure people stop trying to set fields via ctl_reg
1052 * when they should actually be using common SPI framework.
Mike Frysinger90008a62009-10-15 04:13:29 +00001053 * Currently we let through: WOM EMISO PSSE GM SZ.
Mike Frysinger2ed35512007-12-04 23:45:14 -08001054 * Not sure if a user actually needs/uses any of these,
1055 * but let's assume (for now) they do.
1056 */
Mike Frysinger5b47bcd2009-12-18 17:43:31 +00001057 if (chip_info->ctl_reg & ~bfin_ctl_reg) {
Mike Frysinger2ed35512007-12-04 23:45:14 -08001058 dev_err(&spi->dev, "do not set bits in ctl_reg "
1059 "that the SPI framework manages\n");
Daniel Mackac01e972009-03-25 00:18:35 +00001060 goto error;
Mike Frysinger2ed35512007-12-04 23:45:14 -08001061 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001062 chip->enable_dma = chip_info->enable_dma != 0
1063 && drv_data->master_info->enable_dma;
1064 chip->ctl_reg = chip_info->ctl_reg;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001065 chip->cs_chg_udelay = chip_info->cs_chg_udelay;
Wolfgang Muees93b61bd2009-04-06 19:00:53 -07001066 chip->idle_tx_val = chip_info->idle_tx_val;
Yi Lif6a6d962009-06-03 09:46:22 +00001067 chip->pio_interrupt = chip_info->pio_interrupt;
Mike Frysinger033f44b2009-12-18 17:38:04 +00001068 spi->bits_per_word = chip_info->bits_per_word;
Mike Frysinger5b47bcd2009-12-18 17:43:31 +00001069 } else {
1070 /* force a default base state */
1071 chip->ctl_reg &= bfin_ctl_reg;
Mike Frysinger033f44b2009-12-18 17:38:04 +00001072 }
1073
Bob Liu4d676fc2011-01-11 11:19:07 -05001074 if (spi->bits_per_word % 8) {
Mike Frysinger033f44b2009-12-18 17:38:04 +00001075 dev_err(&spi->dev, "%d bits_per_word is not supported\n",
1076 spi->bits_per_word);
1077 goto error;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001078 }
1079
1080 /* translate common spi framework into our register */
Mike Frysinger7715aad2010-02-25 10:00:55 +00001081 if (spi->mode & ~(SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST)) {
1082 dev_err(&spi->dev, "unsupported spi modes detected\n");
1083 goto error;
1084 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001085 if (spi->mode & SPI_CPOL)
Mike Frysinger90008a62009-10-15 04:13:29 +00001086 chip->ctl_reg |= BIT_CTL_CPOL;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001087 if (spi->mode & SPI_CPHA)
Mike Frysinger90008a62009-10-15 04:13:29 +00001088 chip->ctl_reg |= BIT_CTL_CPHA;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001089 if (spi->mode & SPI_LSB_FIRST)
Mike Frysinger90008a62009-10-15 04:13:29 +00001090 chip->ctl_reg |= BIT_CTL_LSBF;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001091 /* we dont support running in slave mode (yet?) */
Mike Frysinger90008a62009-10-15 04:13:29 +00001092 chip->ctl_reg |= BIT_CTL_MASTER;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001093
1094 /*
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001095 * Notice: for blackfin, the speed_hz is the value of register
1096 * SPI_BAUD, not the real baudrate
1097 */
1098 chip->baud = hz_to_spi_baud(spi->max_speed_hz);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001099 chip->chip_select_num = spi->chip_select;
Barry Song4190f6a2010-04-06 03:36:24 +00001100 if (chip->chip_select_num < MAX_CTRL_CS) {
1101 if (!(spi->mode & SPI_CPHA))
1102 dev_warn(&spi->dev, "Warning: SPI CPHA not set:"
1103 " Slave Select not under software control!\n"
1104 " See Documentation/blackfin/bfin-spi-notes.txt");
1105
Barry Songd3cc71f2009-11-17 09:45:59 +00001106 chip->flag = (1 << spi->chip_select) << 8;
Barry Song4190f6a2010-04-06 03:36:24 +00001107 } else
Barry Songd3cc71f2009-11-17 09:45:59 +00001108 chip->cs_gpio = chip->chip_select_num - MAX_CTRL_CS;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001109
Yi Lif6a6d962009-06-03 09:46:22 +00001110 if (chip->enable_dma && chip->pio_interrupt) {
1111 dev_err(&spi->dev, "enable_dma is set, "
1112 "do not set pio_interrupt\n");
1113 goto error;
1114 }
Daniel Mackac01e972009-03-25 00:18:35 +00001115 /*
1116 * if any one SPI chip is registered and wants DMA, request the
1117 * DMA channel for it
1118 */
1119 if (chip->enable_dma && !drv_data->dma_requested) {
1120 /* register dma irq handler */
1121 ret = request_dma(drv_data->dma_channel, "BFIN_SPI_DMA");
1122 if (ret) {
1123 dev_err(&spi->dev,
1124 "Unable to request BlackFin SPI DMA channel\n");
1125 goto error;
1126 }
1127 drv_data->dma_requested = 1;
1128
1129 ret = set_dma_callback(drv_data->dma_channel,
1130 bfin_spi_dma_irq_handler, drv_data);
1131 if (ret) {
1132 dev_err(&spi->dev, "Unable to set dma callback\n");
1133 goto error;
1134 }
1135 dma_disable_irq(drv_data->dma_channel);
1136 }
1137
Yi Lif6a6d962009-06-03 09:46:22 +00001138 if (chip->pio_interrupt && !drv_data->irq_requested) {
1139 ret = request_irq(drv_data->spi_irq, bfin_spi_pio_irq_handler,
1140 IRQF_DISABLED, "BFIN_SPI", drv_data);
1141 if (ret) {
1142 dev_err(&spi->dev, "Unable to register spi IRQ\n");
1143 goto error;
1144 }
1145 drv_data->irq_requested = 1;
1146 /* we use write mode, spi irq has to be disabled here */
1147 disable_irq(drv_data->spi_irq);
1148 }
1149
Barry Songd3cc71f2009-11-17 09:45:59 +00001150 if (chip->chip_select_num >= MAX_CTRL_CS) {
Michael Hennerich73e1ac12010-10-22 02:01:47 -04001151 /* Only request on first setup */
1152 if (spi_get_ctldata(spi) == NULL) {
1153 ret = gpio_request(chip->cs_gpio, spi->modalias);
1154 if (ret) {
1155 dev_err(&spi->dev, "gpio_request() error\n");
1156 goto pin_error;
1157 }
1158 gpio_direction_output(chip->cs_gpio, 1);
Daniel Mackac01e972009-03-25 00:18:35 +00001159 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001160 }
1161
Joe Perches898eb712007-10-18 03:06:30 -07001162 dev_dbg(&spi->dev, "setup spi chip %s, width is %d, dma is %d\n",
Mike Frysinger033f44b2009-12-18 17:38:04 +00001163 spi->modalias, spi->bits_per_word, chip->enable_dma);
Bryan Wu88b40362007-05-21 18:32:16 +08001164 dev_dbg(&spi->dev, "ctl_reg is 0x%x, flag_reg is 0x%x\n",
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001165 chip->ctl_reg, chip->flag);
1166
1167 spi_set_ctldata(spi, chip);
1168
Sonic Zhang12e17c42007-12-04 23:45:16 -08001169 dev_dbg(&spi->dev, "chip select number is %d\n", chip->chip_select_num);
Barry Songd3cc71f2009-11-17 09:45:59 +00001170 if (chip->chip_select_num < MAX_CTRL_CS) {
Daniel Mackac01e972009-03-25 00:18:35 +00001171 ret = peripheral_request(ssel[spi->master->bus_num]
1172 [chip->chip_select_num-1], spi->modalias);
1173 if (ret) {
1174 dev_err(&spi->dev, "peripheral_request() error\n");
1175 goto pin_error;
1176 }
1177 }
Sonic Zhang12e17c42007-12-04 23:45:16 -08001178
Barry Song82216102009-06-17 10:10:53 +00001179 bfin_spi_cs_enable(drv_data, chip);
Mike Frysinger138f97c2009-04-06 19:00:50 -07001180 bfin_spi_cs_deactive(drv_data, chip);
Sonic Zhang07612e52007-12-04 23:45:21 -08001181
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001182 return 0;
Daniel Mackac01e972009-03-25 00:18:35 +00001183
1184 pin_error:
Barry Songd3cc71f2009-11-17 09:45:59 +00001185 if (chip->chip_select_num >= MAX_CTRL_CS)
Daniel Mackac01e972009-03-25 00:18:35 +00001186 gpio_free(chip->cs_gpio);
1187 else
1188 peripheral_free(ssel[spi->master->bus_num]
1189 [chip->chip_select_num - 1]);
1190 error:
1191 if (chip) {
1192 if (drv_data->dma_requested)
1193 free_dma(drv_data->dma_channel);
1194 drv_data->dma_requested = 0;
1195
1196 kfree(chip);
1197 /* prevent free 'chip' twice */
1198 spi_set_ctldata(spi, NULL);
1199 }
1200
1201 return ret;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001202}
1203
1204/*
1205 * callback for spi framework.
1206 * clean driver specific data
1207 */
Mike Frysinger138f97c2009-04-06 19:00:50 -07001208static void bfin_spi_cleanup(struct spi_device *spi)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001209{
Mike Frysinger9c0a7882010-10-18 02:45:22 -04001210 struct bfin_spi_slave_data *chip = spi_get_ctldata(spi);
1211 struct bfin_spi_master_data *drv_data = spi_master_get_devdata(spi->master);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001212
Mike Frysingere7d02e32009-04-06 19:00:51 -07001213 if (!chip)
1214 return;
1215
Barry Songd3cc71f2009-11-17 09:45:59 +00001216 if (chip->chip_select_num < MAX_CTRL_CS) {
Sonic Zhang12e17c42007-12-04 23:45:16 -08001217 peripheral_free(ssel[spi->master->bus_num]
1218 [chip->chip_select_num-1]);
Barry Song82216102009-06-17 10:10:53 +00001219 bfin_spi_cs_disable(drv_data, chip);
Barry Songd3cc71f2009-11-17 09:45:59 +00001220 } else
Michael Hennerich42c78b22009-04-06 19:00:51 -07001221 gpio_free(chip->cs_gpio);
1222
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001223 kfree(chip);
Daniel Mackac01e972009-03-25 00:18:35 +00001224 /* prevent free 'chip' twice */
1225 spi_set_ctldata(spi, NULL);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001226}
1227
Mike Frysinger9c0a7882010-10-18 02:45:22 -04001228static inline int bfin_spi_init_queue(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001229{
1230 INIT_LIST_HEAD(&drv_data->queue);
1231 spin_lock_init(&drv_data->lock);
1232
Mike Frysingerf4f50c32009-09-24 00:41:49 +00001233 drv_data->running = false;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001234 drv_data->busy = 0;
1235
1236 /* init transfer tasklet */
1237 tasklet_init(&drv_data->pump_transfers,
Mike Frysinger138f97c2009-04-06 19:00:50 -07001238 bfin_spi_pump_transfers, (unsigned long)drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001239
1240 /* init messages workqueue */
Mike Frysinger138f97c2009-04-06 19:00:50 -07001241 INIT_WORK(&drv_data->pump_messages, bfin_spi_pump_messages);
Kay Sievers6c7377a2009-03-24 16:38:21 -07001242 drv_data->workqueue = create_singlethread_workqueue(
1243 dev_name(drv_data->master->dev.parent));
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001244 if (drv_data->workqueue == NULL)
1245 return -EBUSY;
1246
1247 return 0;
1248}
1249
Mike Frysinger9c0a7882010-10-18 02:45:22 -04001250static inline int bfin_spi_start_queue(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001251{
1252 unsigned long flags;
1253
1254 spin_lock_irqsave(&drv_data->lock, flags);
1255
Mike Frysingerf4f50c32009-09-24 00:41:49 +00001256 if (drv_data->running || drv_data->busy) {
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001257 spin_unlock_irqrestore(&drv_data->lock, flags);
1258 return -EBUSY;
1259 }
1260
Mike Frysingerf4f50c32009-09-24 00:41:49 +00001261 drv_data->running = true;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001262 drv_data->cur_msg = NULL;
1263 drv_data->cur_transfer = NULL;
1264 drv_data->cur_chip = NULL;
1265 spin_unlock_irqrestore(&drv_data->lock, flags);
1266
1267 queue_work(drv_data->workqueue, &drv_data->pump_messages);
1268
1269 return 0;
1270}
1271
Mike Frysinger9c0a7882010-10-18 02:45:22 -04001272static inline int bfin_spi_stop_queue(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001273{
1274 unsigned long flags;
1275 unsigned limit = 500;
1276 int status = 0;
1277
1278 spin_lock_irqsave(&drv_data->lock, flags);
1279
1280 /*
1281 * This is a bit lame, but is optimized for the common execution path.
1282 * A wait_queue on the drv_data->busy could be used, but then the common
1283 * execution path (pump_messages) would be required to call wake_up or
1284 * friends on every SPI message. Do this instead
1285 */
Mike Frysingerf4f50c32009-09-24 00:41:49 +00001286 drv_data->running = false;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001287 while (!list_empty(&drv_data->queue) && drv_data->busy && limit--) {
1288 spin_unlock_irqrestore(&drv_data->lock, flags);
1289 msleep(10);
1290 spin_lock_irqsave(&drv_data->lock, flags);
1291 }
1292
1293 if (!list_empty(&drv_data->queue) || drv_data->busy)
1294 status = -EBUSY;
1295
1296 spin_unlock_irqrestore(&drv_data->lock, flags);
1297
1298 return status;
1299}
1300
Mike Frysinger9c0a7882010-10-18 02:45:22 -04001301static inline int bfin_spi_destroy_queue(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001302{
1303 int status;
1304
Mike Frysinger138f97c2009-04-06 19:00:50 -07001305 status = bfin_spi_stop_queue(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001306 if (status != 0)
1307 return status;
1308
1309 destroy_workqueue(drv_data->workqueue);
1310
1311 return 0;
1312}
1313
Mike Frysinger138f97c2009-04-06 19:00:50 -07001314static int __init bfin_spi_probe(struct platform_device *pdev)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001315{
1316 struct device *dev = &pdev->dev;
1317 struct bfin5xx_spi_master *platform_info;
1318 struct spi_master *master;
Mike Frysinger9c0a7882010-10-18 02:45:22 -04001319 struct bfin_spi_master_data *drv_data;
Bryan Wua32c6912007-12-04 23:45:15 -08001320 struct resource *res;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001321 int status = 0;
1322
1323 platform_info = dev->platform_data;
1324
1325 /* Allocate master with space for drv_data */
Mike Frysinger2a045132009-09-24 01:28:54 +00001326 master = spi_alloc_master(dev, sizeof(*drv_data));
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001327 if (!master) {
1328 dev_err(&pdev->dev, "can not alloc spi_master\n");
1329 return -ENOMEM;
1330 }
Bryan Wu131b17d2007-12-04 23:45:12 -08001331
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001332 drv_data = spi_master_get_devdata(master);
1333 drv_data->master = master;
1334 drv_data->master_info = platform_info;
1335 drv_data->pdev = pdev;
Bryan Wu003d9222007-12-04 23:45:22 -08001336 drv_data->pin_req = platform_info->pin_req;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001337
David Brownelle7db06b2009-06-17 16:26:04 -07001338 /* the spi->mode bits supported by this driver: */
1339 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST;
1340
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001341 master->bus_num = pdev->id;
1342 master->num_chipselect = platform_info->num_chipselect;
Mike Frysinger138f97c2009-04-06 19:00:50 -07001343 master->cleanup = bfin_spi_cleanup;
1344 master->setup = bfin_spi_setup;
1345 master->transfer = bfin_spi_transfer;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001346
Bryan Wua32c6912007-12-04 23:45:15 -08001347 /* Find and map our resources */
1348 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1349 if (res == NULL) {
1350 dev_err(dev, "Cannot get IORESOURCE_MEM\n");
1351 status = -ENOENT;
1352 goto out_error_get_res;
1353 }
1354
hartleys74947b82009-12-14 22:33:43 +00001355 drv_data->regs_base = ioremap(res->start, resource_size(res));
Bryan Wuf4521262007-12-04 23:45:22 -08001356 if (drv_data->regs_base == NULL) {
Bryan Wua32c6912007-12-04 23:45:15 -08001357 dev_err(dev, "Cannot map IO\n");
1358 status = -ENXIO;
1359 goto out_error_ioremap;
1360 }
1361
Yi Lif6a6d962009-06-03 09:46:22 +00001362 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1363 if (res == NULL) {
Bryan Wua32c6912007-12-04 23:45:15 -08001364 dev_err(dev, "No DMA channel specified\n");
1365 status = -ENOENT;
Yi Lif6a6d962009-06-03 09:46:22 +00001366 goto out_error_free_io;
1367 }
1368 drv_data->dma_channel = res->start;
1369
1370 drv_data->spi_irq = platform_get_irq(pdev, 0);
1371 if (drv_data->spi_irq < 0) {
1372 dev_err(dev, "No spi pio irq specified\n");
1373 status = -ENOENT;
1374 goto out_error_free_io;
Bryan Wua32c6912007-12-04 23:45:15 -08001375 }
1376
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001377 /* Initial and start queue */
Mike Frysinger138f97c2009-04-06 19:00:50 -07001378 status = bfin_spi_init_queue(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001379 if (status != 0) {
Bryan Wua32c6912007-12-04 23:45:15 -08001380 dev_err(dev, "problem initializing queue\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001381 goto out_error_queue_alloc;
1382 }
Bryan Wua32c6912007-12-04 23:45:15 -08001383
Mike Frysinger138f97c2009-04-06 19:00:50 -07001384 status = bfin_spi_start_queue(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001385 if (status != 0) {
Bryan Wua32c6912007-12-04 23:45:15 -08001386 dev_err(dev, "problem starting queue\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001387 goto out_error_queue_alloc;
1388 }
1389
Vitja Makarovf9e522c2008-04-08 17:41:57 -07001390 status = peripheral_request_list(drv_data->pin_req, DRV_NAME);
1391 if (status != 0) {
1392 dev_err(&pdev->dev, ": Requesting Peripherals failed\n");
1393 goto out_error_queue_alloc;
1394 }
1395
Wolfgang Mueesbb8beec2009-05-22 01:11:02 +00001396 /* Reset SPI registers. If these registers were used by the boot loader,
1397 * the sky may fall on your head if you enable the dma controller.
1398 */
1399 write_CTRL(drv_data, BIT_CTL_CPHA | BIT_CTL_MASTER);
1400 write_FLAG(drv_data, 0xFF00);
1401
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001402 /* Register with the SPI framework */
1403 platform_set_drvdata(pdev, drv_data);
1404 status = spi_register_master(master);
1405 if (status != 0) {
Bryan Wua32c6912007-12-04 23:45:15 -08001406 dev_err(dev, "problem registering spi master\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001407 goto out_error_queue_alloc;
1408 }
Bryan Wua32c6912007-12-04 23:45:15 -08001409
Bryan Wuf4521262007-12-04 23:45:22 -08001410 dev_info(dev, "%s, Version %s, regs_base@%p, dma channel@%d\n",
Bryan Wubb90eb02007-12-04 23:45:18 -08001411 DRV_DESC, DRV_VERSION, drv_data->regs_base,
1412 drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001413 return status;
1414
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001415out_error_queue_alloc:
Mike Frysinger138f97c2009-04-06 19:00:50 -07001416 bfin_spi_destroy_queue(drv_data);
Yi Lif6a6d962009-06-03 09:46:22 +00001417out_error_free_io:
Bryan Wubb90eb02007-12-04 23:45:18 -08001418 iounmap((void *) drv_data->regs_base);
Bryan Wua32c6912007-12-04 23:45:15 -08001419out_error_ioremap:
1420out_error_get_res:
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001421 spi_master_put(master);
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001422
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001423 return status;
1424}
1425
1426/* stop hardware and remove the driver */
Mike Frysinger138f97c2009-04-06 19:00:50 -07001427static int __devexit bfin_spi_remove(struct platform_device *pdev)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001428{
Mike Frysinger9c0a7882010-10-18 02:45:22 -04001429 struct bfin_spi_master_data *drv_data = platform_get_drvdata(pdev);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001430 int status = 0;
1431
1432 if (!drv_data)
1433 return 0;
1434
1435 /* Remove the queue */
Mike Frysinger138f97c2009-04-06 19:00:50 -07001436 status = bfin_spi_destroy_queue(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001437 if (status != 0)
1438 return status;
1439
1440 /* Disable the SSP at the peripheral and SOC level */
1441 bfin_spi_disable(drv_data);
1442
1443 /* Release DMA */
1444 if (drv_data->master_info->enable_dma) {
Bryan Wubb90eb02007-12-04 23:45:18 -08001445 if (dma_channel_active(drv_data->dma_channel))
1446 free_dma(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001447 }
1448
Yi Lif6a6d962009-06-03 09:46:22 +00001449 if (drv_data->irq_requested) {
1450 free_irq(drv_data->spi_irq, drv_data);
1451 drv_data->irq_requested = 0;
1452 }
1453
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001454 /* Disconnect from the SPI framework */
1455 spi_unregister_master(drv_data->master);
1456
Bryan Wu003d9222007-12-04 23:45:22 -08001457 peripheral_free_list(drv_data->pin_req);
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001458
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001459 /* Prevent double remove */
1460 platform_set_drvdata(pdev, NULL);
1461
1462 return 0;
1463}
1464
1465#ifdef CONFIG_PM
Mike Frysinger138f97c2009-04-06 19:00:50 -07001466static int bfin_spi_suspend(struct platform_device *pdev, pm_message_t state)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001467{
Mike Frysinger9c0a7882010-10-18 02:45:22 -04001468 struct bfin_spi_master_data *drv_data = platform_get_drvdata(pdev);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001469 int status = 0;
1470
Mike Frysinger138f97c2009-04-06 19:00:50 -07001471 status = bfin_spi_stop_queue(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001472 if (status != 0)
1473 return status;
1474
Barry Songb052fd02009-11-18 09:43:21 +00001475 drv_data->ctrl_reg = read_CTRL(drv_data);
1476 drv_data->flag_reg = read_FLAG(drv_data);
1477
1478 /*
1479 * reset SPI_CTL and SPI_FLG registers
1480 */
1481 write_CTRL(drv_data, BIT_CTL_CPHA | BIT_CTL_MASTER);
1482 write_FLAG(drv_data, 0xFF00);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001483
1484 return 0;
1485}
1486
Mike Frysinger138f97c2009-04-06 19:00:50 -07001487static int bfin_spi_resume(struct platform_device *pdev)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001488{
Mike Frysinger9c0a7882010-10-18 02:45:22 -04001489 struct bfin_spi_master_data *drv_data = platform_get_drvdata(pdev);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001490 int status = 0;
1491
Barry Songb052fd02009-11-18 09:43:21 +00001492 write_CTRL(drv_data, drv_data->ctrl_reg);
1493 write_FLAG(drv_data, drv_data->flag_reg);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001494
1495 /* Start the queue running */
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) {
1498 dev_err(&pdev->dev, "problem starting queue (%d)\n", status);
1499 return status;
1500 }
1501
1502 return 0;
1503}
1504#else
Mike Frysinger138f97c2009-04-06 19:00:50 -07001505#define bfin_spi_suspend NULL
1506#define bfin_spi_resume NULL
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001507#endif /* CONFIG_PM */
1508
Kay Sievers7e38c3c2008-04-10 21:29:20 -07001509MODULE_ALIAS("platform:bfin-spi");
Mike Frysinger138f97c2009-04-06 19:00:50 -07001510static struct platform_driver bfin_spi_driver = {
David Brownellfc3ba952007-08-30 23:56:24 -07001511 .driver = {
Bryan Wua32c6912007-12-04 23:45:15 -08001512 .name = DRV_NAME,
Bryan Wu88b40362007-05-21 18:32:16 +08001513 .owner = THIS_MODULE,
1514 },
Mike Frysinger138f97c2009-04-06 19:00:50 -07001515 .suspend = bfin_spi_suspend,
1516 .resume = bfin_spi_resume,
1517 .remove = __devexit_p(bfin_spi_remove),
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001518};
1519
Mike Frysinger138f97c2009-04-06 19:00:50 -07001520static int __init bfin_spi_init(void)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001521{
Mike Frysinger138f97c2009-04-06 19:00:50 -07001522 return platform_driver_probe(&bfin_spi_driver, bfin_spi_probe);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001523}
Michael Hennerich6f7c17f2010-07-01 14:34:10 +00001524subsys_initcall(bfin_spi_init);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001525
Mike Frysinger138f97c2009-04-06 19:00:50 -07001526static void __exit bfin_spi_exit(void)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001527{
Mike Frysinger138f97c2009-04-06 19:00:50 -07001528 platform_driver_unregister(&bfin_spi_driver);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001529}
Mike Frysinger138f97c2009-04-06 19:00:50 -07001530module_exit(bfin_spi_exit);