blob: 550aaf70cb2d24614b6bb7fc26885e3e7d2fd7af [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)
Wu, Bryana5f6abd2007-05-06 14:50:34 -070044
Mike Frysingerb9f139a2009-09-24 01:27:47 +000045struct master_data;
Mike Frysinger9c4542c2009-09-24 01:04:04 +000046
47struct transfer_ops {
Mike Frysingerb9f139a2009-09-24 01:27:47 +000048 void (*write) (struct master_data *);
49 void (*read) (struct master_data *);
50 void (*duplex) (struct master_data *);
Mike Frysinger9c4542c2009-09-24 01:04:04 +000051};
52
Mike Frysingerb9f139a2009-09-24 01:27:47 +000053struct 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 Frysingerb9f139a2009-09-24 01:27:47 +000083 struct 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 Frysinger9c4542c2009-09-24 01:04:04 +0000108 const struct transfer_ops *ops;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700109};
110
Mike Frysingerb9f139a2009-09-24 01:27:47 +0000111struct 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 Frysinger9c4542c2009-09-24 01:04:04 +0000122 const struct 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 Frysingerb9f139a2009-09-24 01:27:47 +0000126static inline u16 read_##reg(struct master_data *drv_data) \
Bryan Wubb90eb02007-12-04 23:45:18 -0800127 { return bfin_read16(drv_data->regs_base + off); } \
Mike Frysingerb9f139a2009-09-24 01:27:47 +0000128static inline void write_##reg(struct 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 Frysingerb9f139a2009-09-24 01:27:47 +0000139static void bfin_spi_enable(struct 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 Frysingerb9f139a2009-09-24 01:27:47 +0000147static void bfin_spi_disable(struct 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 Frysingerb9f139a2009-09-24 01:27:47 +0000170static int bfin_spi_flush(struct 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 Frysingerb9f139a2009-09-24 01:27:47 +0000184static void bfin_spi_cs_active(struct master_data *drv_data, struct 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 Frysingerb9f139a2009-09-24 01:27:47 +0000197static void bfin_spi_cs_deactive(struct master_data *drv_data, struct slave_data *chip)
Bryan Wufad91c82007-12-04 23:45:14 -0800198{
Barry Songd3cc71f2009-11-17 09:45:59 +0000199 if (likely(chip->chip_select_num < MAX_CTRL_CS)) {
Michael Hennerich42c78b22009-04-06 19:00:51 -0700200 u16 flag = read_FLAG(drv_data);
Bryan Wufad91c82007-12-04 23:45:14 -0800201
Barry Song82216102009-06-17 10:10:53 +0000202 flag |= chip->flag;
Bryan Wufad91c82007-12-04 23:45:14 -0800203
Michael Hennerich42c78b22009-04-06 19:00:51 -0700204 write_FLAG(drv_data, flag);
205 } else {
206 gpio_set_value(chip->cs_gpio, 1);
207 }
Bryan Wu62310e52007-12-04 23:45:20 -0800208
209 /* Move delay here for consistency */
210 if (chip->cs_chg_udelay)
211 udelay(chip->cs_chg_udelay);
Bryan Wufad91c82007-12-04 23:45:14 -0800212}
213
Barry Song82216102009-06-17 10:10:53 +0000214/* enable or disable the pin muxed by GPIO and SPI CS to work as SPI CS */
Mike Frysingerb9f139a2009-09-24 01:27:47 +0000215static inline void bfin_spi_cs_enable(struct master_data *drv_data, struct slave_data *chip)
Barry Song82216102009-06-17 10:10:53 +0000216{
Barry Songd3cc71f2009-11-17 09:45:59 +0000217 if (chip->chip_select_num < MAX_CTRL_CS) {
218 u16 flag = read_FLAG(drv_data);
Barry Song82216102009-06-17 10:10:53 +0000219
Barry Songd3cc71f2009-11-17 09:45:59 +0000220 flag |= (chip->flag >> 8);
Barry Song82216102009-06-17 10:10:53 +0000221
Barry Songd3cc71f2009-11-17 09:45:59 +0000222 write_FLAG(drv_data, flag);
223 }
Barry Song82216102009-06-17 10:10:53 +0000224}
225
Mike Frysingerb9f139a2009-09-24 01:27:47 +0000226static inline void bfin_spi_cs_disable(struct master_data *drv_data, struct slave_data *chip)
Barry Song82216102009-06-17 10:10:53 +0000227{
Barry Songd3cc71f2009-11-17 09:45:59 +0000228 if (chip->chip_select_num < MAX_CTRL_CS) {
229 u16 flag = read_FLAG(drv_data);
Barry Song82216102009-06-17 10:10:53 +0000230
Barry Songd3cc71f2009-11-17 09:45:59 +0000231 flag &= ~(chip->flag >> 8);
Barry Song82216102009-06-17 10:10:53 +0000232
Barry Songd3cc71f2009-11-17 09:45:59 +0000233 write_FLAG(drv_data, flag);
234 }
Barry Song82216102009-06-17 10:10:53 +0000235}
236
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700237/* stop controller and re-config current chip*/
Mike Frysingerb9f139a2009-09-24 01:27:47 +0000238static void bfin_spi_restore_state(struct master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700239{
Mike Frysingerb9f139a2009-09-24 01:27:47 +0000240 struct slave_data *chip = drv_data->cur_chip;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700241
242 /* Clear status and disable clock */
Bryan Wubb90eb02007-12-04 23:45:18 -0800243 write_STAT(drv_data, BIT_STAT_CLR);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700244 bfin_spi_disable(drv_data);
Bryan Wu88b40362007-05-21 18:32:16 +0800245 dev_dbg(&drv_data->pdev->dev, "restoring spi ctl state\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700246
Barry Song9677b0de2009-11-30 03:49:41 +0000247 SSYNC();
248
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700249 /* Load the registers */
Bryan Wubb90eb02007-12-04 23:45:18 -0800250 write_CTRL(drv_data, chip->ctl_reg);
Bryan Wu092e1fd2007-12-04 23:45:23 -0800251 write_BAUD(drv_data, chip->baud);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800252
253 bfin_spi_enable(drv_data);
Mike Frysinger138f97c2009-04-06 19:00:50 -0700254 bfin_spi_cs_active(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700255}
256
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700257/* used to kick off transfer in rx mode and read unwanted RX data */
Mike Frysingerb9f139a2009-09-24 01:27:47 +0000258static inline void bfin_spi_dummy_read(struct master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700259{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700260 (void) read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700261}
262
Mike Frysingerb9f139a2009-09-24 01:27:47 +0000263static void bfin_spi_u8_writer(struct master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700264{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700265 /* clear RXS (we check for RXS inside the loop) */
266 bfin_spi_dummy_read(drv_data);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800267
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700268 while (drv_data->tx < drv_data->tx_end) {
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700269 write_TDBR(drv_data, (*(u8 *) (drv_data->tx++)));
270 /* wait until transfer finished.
271 checking SPIF or TXS may not guarantee transfer completion */
272 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800273 cpu_relax();
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700274 /* discard RX data and clear RXS */
275 bfin_spi_dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700276 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700277}
278
Mike Frysingerb9f139a2009-09-24 01:27:47 +0000279static void bfin_spi_u8_reader(struct master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700280{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700281 u16 tx_val = drv_data->cur_chip->idle_tx_val;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700282
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700283 /* discard old RX data and clear RXS */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700284 bfin_spi_dummy_read(drv_data);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800285
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700286 while (drv_data->rx < drv_data->rx_end) {
287 write_TDBR(drv_data, tx_val);
Bryan Wubb90eb02007-12-04 23:45:18 -0800288 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800289 cpu_relax();
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700290 *(u8 *) (drv_data->rx++) = read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700291 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700292}
293
Mike Frysingerb9f139a2009-09-24 01:27:47 +0000294static void bfin_spi_u8_duplex(struct master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700295{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700296 /* discard old RX data and clear RXS */
297 bfin_spi_dummy_read(drv_data);
298
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700299 while (drv_data->rx < drv_data->rx_end) {
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700300 write_TDBR(drv_data, (*(u8 *) (drv_data->tx++)));
Bryan Wubb90eb02007-12-04 23:45:18 -0800301 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800302 cpu_relax();
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700303 *(u8 *) (drv_data->rx++) = read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700304 }
305}
306
Mike Frysinger9c4542c2009-09-24 01:04:04 +0000307static const struct transfer_ops bfin_transfer_ops_u8 = {
308 .write = bfin_spi_u8_writer,
309 .read = bfin_spi_u8_reader,
310 .duplex = bfin_spi_u8_duplex,
311};
312
Mike Frysingerb9f139a2009-09-24 01:27:47 +0000313static void bfin_spi_u16_writer(struct master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700314{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700315 /* clear RXS (we check for RXS inside the loop) */
316 bfin_spi_dummy_read(drv_data);
Bryan Wu88b40362007-05-21 18:32:16 +0800317
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700318 while (drv_data->tx < drv_data->tx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800319 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700320 drv_data->tx += 2;
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700321 /* wait until transfer finished.
322 checking SPIF or TXS may not guarantee transfer completion */
323 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
324 cpu_relax();
325 /* discard RX data and clear RXS */
326 bfin_spi_dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700327 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700328}
329
Mike Frysingerb9f139a2009-09-24 01:27:47 +0000330static void bfin_spi_u16_reader(struct master_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;
Sonic Zhangcc487e72007-12-04 23:45:17 -0800333
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);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700336
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();
Bryan Wubb90eb02007-12-04 23:45:18 -0800341 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700342 drv_data->rx += 2;
343 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700344}
345
Mike Frysingerb9f139a2009-09-24 01:27:47 +0000346static void bfin_spi_u16_duplex(struct master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700347{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700348 /* discard old RX data and clear RXS */
349 bfin_spi_dummy_read(drv_data);
350
351 while (drv_data->rx < drv_data->rx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800352 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700353 drv_data->tx += 2;
Bryan Wubb90eb02007-12-04 23:45:18 -0800354 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800355 cpu_relax();
Bryan Wubb90eb02007-12-04 23:45:18 -0800356 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700357 drv_data->rx += 2;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700358 }
359}
360
Mike Frysinger9c4542c2009-09-24 01:04:04 +0000361static const struct transfer_ops bfin_transfer_ops_u16 = {
362 .write = bfin_spi_u16_writer,
363 .read = bfin_spi_u16_reader,
364 .duplex = bfin_spi_u16_duplex,
365};
366
Rob Marise3595402010-04-06 04:12:00 +0000367/* test if there is more transfer to be done */
Mike Frysingerb9f139a2009-09-24 01:27:47 +0000368static void *bfin_spi_next_transfer(struct master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700369{
370 struct spi_message *msg = drv_data->cur_msg;
371 struct spi_transfer *trans = drv_data->cur_transfer;
372
373 /* Move to next transfer */
374 if (trans->transfer_list.next != &msg->transfers) {
375 drv_data->cur_transfer =
376 list_entry(trans->transfer_list.next,
377 struct spi_transfer, transfer_list);
378 return RUNNING_STATE;
379 } else
380 return DONE_STATE;
381}
382
383/*
384 * caller already set message->status;
385 * dma and pio irqs are blocked give finished message back
386 */
Mike Frysingerb9f139a2009-09-24 01:27:47 +0000387static void bfin_spi_giveback(struct master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700388{
Mike Frysingerb9f139a2009-09-24 01:27:47 +0000389 struct slave_data *chip = drv_data->cur_chip;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700390 struct spi_transfer *last_transfer;
391 unsigned long flags;
392 struct spi_message *msg;
393
394 spin_lock_irqsave(&drv_data->lock, flags);
395 msg = drv_data->cur_msg;
396 drv_data->cur_msg = NULL;
397 drv_data->cur_transfer = NULL;
398 drv_data->cur_chip = NULL;
399 queue_work(drv_data->workqueue, &drv_data->pump_messages);
400 spin_unlock_irqrestore(&drv_data->lock, flags);
401
402 last_transfer = list_entry(msg->transfers.prev,
403 struct spi_transfer, transfer_list);
404
405 msg->state = NULL;
406
Bryan Wufad91c82007-12-04 23:45:14 -0800407 if (!drv_data->cs_change)
Mike Frysinger138f97c2009-04-06 19:00:50 -0700408 bfin_spi_cs_deactive(drv_data, chip);
Bryan Wufad91c82007-12-04 23:45:14 -0800409
Yi Lib9b2a762009-04-06 19:00:49 -0700410 /* Not stop spi in autobuffer mode */
411 if (drv_data->tx_dma != 0xFFFF)
412 bfin_spi_disable(drv_data);
413
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700414 if (msg->complete)
415 msg->complete(msg->context);
416}
417
Yi Lif6a6d962009-06-03 09:46:22 +0000418/* spi data irq handler */
419static irqreturn_t bfin_spi_pio_irq_handler(int irq, void *dev_id)
420{
Mike Frysingerb9f139a2009-09-24 01:27:47 +0000421 struct master_data *drv_data = dev_id;
422 struct slave_data *chip = drv_data->cur_chip;
Yi Lif6a6d962009-06-03 09:46:22 +0000423 struct spi_message *msg = drv_data->cur_msg;
424 int n_bytes = drv_data->n_bytes;
425
426 /* wait until transfer finished. */
427 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
428 cpu_relax();
429
430 if ((drv_data->tx && drv_data->tx >= drv_data->tx_end) ||
431 (drv_data->rx && drv_data->rx >= (drv_data->rx_end - n_bytes))) {
432 /* last read */
433 if (drv_data->rx) {
434 dev_dbg(&drv_data->pdev->dev, "last read\n");
435 if (n_bytes == 2)
436 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
437 else if (n_bytes == 1)
438 *(u8 *) (drv_data->rx) = read_RDBR(drv_data);
439 drv_data->rx += n_bytes;
440 }
441
442 msg->actual_length += drv_data->len_in_bytes;
443 if (drv_data->cs_change)
444 bfin_spi_cs_deactive(drv_data, chip);
445 /* Move to next transfer */
446 msg->state = bfin_spi_next_transfer(drv_data);
447
Yi Li7370ed62009-12-07 08:07:01 +0000448 disable_irq_nosync(drv_data->spi_irq);
Yi Lif6a6d962009-06-03 09:46:22 +0000449
450 /* Schedule transfer tasklet */
451 tasklet_schedule(&drv_data->pump_transfers);
452 return IRQ_HANDLED;
453 }
454
455 if (drv_data->rx && drv_data->tx) {
456 /* duplex */
457 dev_dbg(&drv_data->pdev->dev, "duplex: write_TDBR\n");
458 if (drv_data->n_bytes == 2) {
459 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
460 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
461 } else if (drv_data->n_bytes == 1) {
462 *(u8 *) (drv_data->rx) = read_RDBR(drv_data);
463 write_TDBR(drv_data, (*(u8 *) (drv_data->tx)));
464 }
465 } else if (drv_data->rx) {
466 /* read */
467 dev_dbg(&drv_data->pdev->dev, "read: write_TDBR\n");
468 if (drv_data->n_bytes == 2)
469 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
470 else if (drv_data->n_bytes == 1)
471 *(u8 *) (drv_data->rx) = read_RDBR(drv_data);
472 write_TDBR(drv_data, chip->idle_tx_val);
473 } else if (drv_data->tx) {
474 /* write */
475 dev_dbg(&drv_data->pdev->dev, "write: write_TDBR\n");
476 bfin_spi_dummy_read(drv_data);
477 if (drv_data->n_bytes == 2)
478 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
479 else if (drv_data->n_bytes == 1)
480 write_TDBR(drv_data, (*(u8 *) (drv_data->tx)));
481 }
482
483 if (drv_data->tx)
484 drv_data->tx += n_bytes;
485 if (drv_data->rx)
486 drv_data->rx += n_bytes;
487
488 return IRQ_HANDLED;
489}
490
Mike Frysinger138f97c2009-04-06 19:00:50 -0700491static irqreturn_t bfin_spi_dma_irq_handler(int irq, void *dev_id)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700492{
Mike Frysingerb9f139a2009-09-24 01:27:47 +0000493 struct master_data *drv_data = dev_id;
494 struct slave_data *chip = drv_data->cur_chip;
Bryan Wubb90eb02007-12-04 23:45:18 -0800495 struct spi_message *msg = drv_data->cur_msg;
Mike Frysingeraaaf9392009-04-06 19:00:42 -0700496 unsigned long timeout;
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700497 unsigned short dmastat = get_dma_curr_irqstat(drv_data->dma_channel);
Mike Frysinger04b95d22009-04-06 19:00:35 -0700498 u16 spistat = read_STAT(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700499
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700500 dev_dbg(&drv_data->pdev->dev,
501 "in dma_irq_handler dmastat:0x%x spistat:0x%x\n",
502 dmastat, spistat);
503
Bryan Wubb90eb02007-12-04 23:45:18 -0800504 clear_dma_irqstat(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700505
506 /*
Bryan Wud6fe89b2007-06-11 17:34:17 +0800507 * wait for the last transaction shifted out. HRM states:
508 * at this point there may still be data in the SPI DMA FIFO waiting
509 * to be transmitted ... software needs to poll TXS in the SPI_STAT
510 * register until it goes low for 2 successive reads
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700511 */
512 if (drv_data->tx != NULL) {
Mike Frysinger90008a62009-10-15 04:13:29 +0000513 while ((read_STAT(drv_data) & BIT_STAT_TXS) ||
514 (read_STAT(drv_data) & BIT_STAT_TXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800515 cpu_relax();
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700516 }
517
Mike Frysingeraaaf9392009-04-06 19:00:42 -0700518 dev_dbg(&drv_data->pdev->dev,
519 "in dma_irq_handler dmastat:0x%x spistat:0x%x\n",
520 dmastat, read_STAT(drv_data));
521
522 timeout = jiffies + HZ;
Mike Frysinger90008a62009-10-15 04:13:29 +0000523 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Mike Frysingeraaaf9392009-04-06 19:00:42 -0700524 if (!time_before(jiffies, timeout)) {
525 dev_warn(&drv_data->pdev->dev, "timeout waiting for SPIF");
526 break;
527 } else
528 cpu_relax();
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700529
Mike Frysinger90008a62009-10-15 04:13:29 +0000530 if ((dmastat & DMA_ERR) && (spistat & BIT_STAT_RBSY)) {
Mike Frysinger04b95d22009-04-06 19:00:35 -0700531 msg->state = ERROR_STATE;
532 dev_err(&drv_data->pdev->dev, "dma receive: fifo/buffer overflow\n");
533 } else {
534 msg->actual_length += drv_data->len_in_bytes;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700535
Mike Frysinger04b95d22009-04-06 19:00:35 -0700536 if (drv_data->cs_change)
Mike Frysinger138f97c2009-04-06 19:00:50 -0700537 bfin_spi_cs_deactive(drv_data, chip);
Bryan Wufad91c82007-12-04 23:45:14 -0800538
Mike Frysinger04b95d22009-04-06 19:00:35 -0700539 /* Move to next transfer */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700540 msg->state = bfin_spi_next_transfer(drv_data);
Mike Frysinger04b95d22009-04-06 19:00:35 -0700541 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700542
543 /* Schedule transfer tasklet */
544 tasklet_schedule(&drv_data->pump_transfers);
545
546 /* free the irq handler before next transfer */
Bryan Wu88b40362007-05-21 18:32:16 +0800547 dev_dbg(&drv_data->pdev->dev,
548 "disable dma channel irq%d\n",
Bryan Wubb90eb02007-12-04 23:45:18 -0800549 drv_data->dma_channel);
Barry Songa75bd652010-01-22 10:07:30 +0000550 dma_disable_irq_nosync(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700551
552 return IRQ_HANDLED;
553}
554
Mike Frysinger138f97c2009-04-06 19:00:50 -0700555static void bfin_spi_pump_transfers(unsigned long data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700556{
Mike Frysingerb9f139a2009-09-24 01:27:47 +0000557 struct master_data *drv_data = (struct master_data *)data;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700558 struct spi_message *message = NULL;
559 struct spi_transfer *transfer = NULL;
560 struct spi_transfer *previous = NULL;
Mike Frysingerb9f139a2009-09-24 01:27:47 +0000561 struct slave_data *chip = NULL;
Mike Frysinger033f44b2009-12-18 17:38:04 +0000562 unsigned int bits_per_word;
Mike Frysinger5e8592d2009-12-18 18:00:10 +0000563 u16 cr, cr_width, dma_width, dma_config;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700564 u32 tranf_success = 1;
Vitja Makarov8eeb12e2008-05-01 04:35:03 -0700565 u8 full_duplex = 0;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700566
567 /* Get current state information */
568 message = drv_data->cur_msg;
569 transfer = drv_data->cur_transfer;
570 chip = drv_data->cur_chip;
Bryan Wu092e1fd2007-12-04 23:45:23 -0800571
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700572 /*
573 * if msg is error or done, report it back using complete() callback
574 */
575
576 /* Handle for abort */
577 if (message->state == ERROR_STATE) {
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700578 dev_dbg(&drv_data->pdev->dev, "transfer: we've hit an error\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700579 message->status = -EIO;
Mike Frysinger138f97c2009-04-06 19:00:50 -0700580 bfin_spi_giveback(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700581 return;
582 }
583
584 /* Handle end of message */
585 if (message->state == DONE_STATE) {
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700586 dev_dbg(&drv_data->pdev->dev, "transfer: all done!\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700587 message->status = 0;
Mike Frysinger138f97c2009-04-06 19:00:50 -0700588 bfin_spi_giveback(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700589 return;
590 }
591
592 /* Delay if requested at end of transfer */
593 if (message->state == RUNNING_STATE) {
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700594 dev_dbg(&drv_data->pdev->dev, "transfer: still running ...\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700595 previous = list_entry(transfer->transfer_list.prev,
596 struct spi_transfer, transfer_list);
597 if (previous->delay_usecs)
598 udelay(previous->delay_usecs);
599 }
600
Mike Frysingerab09e042009-09-23 23:32:34 +0000601 /* Flush any existing transfers that may be sitting in the hardware */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700602 if (bfin_spi_flush(drv_data) == 0) {
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700603 dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
604 message->status = -EIO;
Mike Frysinger138f97c2009-04-06 19:00:50 -0700605 bfin_spi_giveback(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700606 return;
607 }
608
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700609 if (transfer->len == 0) {
610 /* Move to next transfer of this msg */
611 message->state = bfin_spi_next_transfer(drv_data);
612 /* Schedule next transfer tasklet */
613 tasklet_schedule(&drv_data->pump_transfers);
614 }
615
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700616 if (transfer->tx_buf != NULL) {
617 drv_data->tx = (void *)transfer->tx_buf;
618 drv_data->tx_end = drv_data->tx + transfer->len;
Bryan Wu88b40362007-05-21 18:32:16 +0800619 dev_dbg(&drv_data->pdev->dev, "tx_buf is %p, tx_end is %p\n",
620 transfer->tx_buf, drv_data->tx_end);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700621 } else {
622 drv_data->tx = NULL;
623 }
624
625 if (transfer->rx_buf != NULL) {
Vitja Makarov8eeb12e2008-05-01 04:35:03 -0700626 full_duplex = transfer->tx_buf != NULL;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700627 drv_data->rx = transfer->rx_buf;
628 drv_data->rx_end = drv_data->rx + transfer->len;
Bryan Wu88b40362007-05-21 18:32:16 +0800629 dev_dbg(&drv_data->pdev->dev, "rx_buf is %p, rx_end is %p\n",
630 transfer->rx_buf, drv_data->rx_end);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700631 } else {
632 drv_data->rx = NULL;
633 }
634
635 drv_data->rx_dma = transfer->rx_dma;
636 drv_data->tx_dma = transfer->tx_dma;
637 drv_data->len_in_bytes = transfer->len;
Bryan Wufad91c82007-12-04 23:45:14 -0800638 drv_data->cs_change = transfer->cs_change;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700639
Bryan Wu092e1fd2007-12-04 23:45:23 -0800640 /* Bits per word setup */
Mike Frysinger033f44b2009-12-18 17:38:04 +0000641 bits_per_word = transfer->bits_per_word ? : message->spi->bits_per_word;
642 if (bits_per_word == 8) {
Bryan Wu092e1fd2007-12-04 23:45:23 -0800643 drv_data->n_bytes = 1;
Mike Frysinger5e8592d2009-12-18 18:00:10 +0000644 drv_data->len = transfer->len;
645 cr_width = 0;
Mike Frysinger9c4542c2009-09-24 01:04:04 +0000646 drv_data->ops = &bfin_transfer_ops_u8;
Mike Frysinger033f44b2009-12-18 17:38:04 +0000647 } else {
Bryan Wu092e1fd2007-12-04 23:45:23 -0800648 drv_data->n_bytes = 2;
Mike Frysinger5e8592d2009-12-18 18:00:10 +0000649 drv_data->len = (transfer->len) >> 1;
650 cr_width = BIT_CTL_WORDSIZE;
Mike Frysinger9c4542c2009-09-24 01:04:04 +0000651 drv_data->ops = &bfin_transfer_ops_u16;
Bryan Wu092e1fd2007-12-04 23:45:23 -0800652 }
Mike Frysinger5e8592d2009-12-18 18:00:10 +0000653 cr = read_CTRL(drv_data) & ~(BIT_CTL_TIMOD | BIT_CTL_WORDSIZE);
654 cr |= cr_width;
Bryan Wu092e1fd2007-12-04 23:45:23 -0800655 write_CTRL(drv_data, cr);
656
Mike Frysinger4fb98ef2008-04-08 17:41:57 -0700657 dev_dbg(&drv_data->pdev->dev,
Mike Frysinger9c4542c2009-09-24 01:04:04 +0000658 "transfer: drv_data->ops is %p, chip->ops is %p, u8_ops is %p\n",
659 drv_data->ops, chip->ops, &bfin_transfer_ops_u8);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700660
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700661 message->state = RUNNING_STATE;
662 dma_config = 0;
663
Bryan Wu092e1fd2007-12-04 23:45:23 -0800664 /* Speed setup (surely valid because already checked) */
665 if (transfer->speed_hz)
666 write_BAUD(drv_data, hz_to_spi_baud(transfer->speed_hz));
667 else
668 write_BAUD(drv_data, chip->baud);
669
Bryan Wubb90eb02007-12-04 23:45:18 -0800670 write_STAT(drv_data, BIT_STAT_CLR);
Yi Lib9b2a762009-04-06 19:00:49 -0700671 if (drv_data->cs_change)
Mike Frysinger138f97c2009-04-06 19:00:50 -0700672 bfin_spi_cs_active(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700673
Bryan Wu88b40362007-05-21 18:32:16 +0800674 dev_dbg(&drv_data->pdev->dev,
675 "now pumping a transfer: width is %d, len is %d\n",
Mike Frysinger5e8592d2009-12-18 18:00:10 +0000676 cr_width, transfer->len);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700677
678 /*
Vitja Makarov8cf58582009-04-06 19:00:31 -0700679 * Try to map dma buffer and do a dma transfer. If successful use,
680 * different way to r/w according to the enable_dma settings and if
681 * we are not doing a full duplex transfer (since the hardware does
682 * not support full duplex DMA transfers).
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700683 */
Vitja Makarov8eeb12e2008-05-01 04:35:03 -0700684 if (!full_duplex && drv_data->cur_chip->enable_dma
685 && drv_data->len > 6) {
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700686
Mike Frysinger11d6f592009-04-06 19:00:41 -0700687 unsigned long dma_start_addr, flags;
Mike Frysinger7aec3562009-04-06 19:00:36 -0700688
Bryan Wubb90eb02007-12-04 23:45:18 -0800689 disable_dma(drv_data->dma_channel);
690 clear_dma_irqstat(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700691
692 /* config dma channel */
Bryan Wu88b40362007-05-21 18:32:16 +0800693 dev_dbg(&drv_data->pdev->dev, "doing dma transfer\n");
Mike Frysinger7aec3562009-04-06 19:00:36 -0700694 set_dma_x_count(drv_data->dma_channel, drv_data->len);
Mike Frysinger5e8592d2009-12-18 18:00:10 +0000695 if (cr_width == BIT_CTL_WORDSIZE) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800696 set_dma_x_modify(drv_data->dma_channel, 2);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700697 dma_width = WDSIZE_16;
698 } else {
Bryan Wubb90eb02007-12-04 23:45:18 -0800699 set_dma_x_modify(drv_data->dma_channel, 1);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700700 dma_width = WDSIZE_8;
701 }
702
Sonic Zhang3f479a62007-12-04 23:45:18 -0800703 /* poll for SPI completion before start */
Bryan Wubb90eb02007-12-04 23:45:18 -0800704 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Bryan Wud8c05002007-12-04 23:45:21 -0800705 cpu_relax();
Sonic Zhang3f479a62007-12-04 23:45:18 -0800706
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700707 /* dirty hack for autobuffer DMA mode */
708 if (drv_data->tx_dma == 0xFFFF) {
Bryan Wu88b40362007-05-21 18:32:16 +0800709 dev_dbg(&drv_data->pdev->dev,
710 "doing autobuffer DMA out.\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700711
712 /* no irq in autobuffer mode */
713 dma_config =
714 (DMAFLOW_AUTO | RESTART | dma_width | DI_EN);
Bryan Wubb90eb02007-12-04 23:45:18 -0800715 set_dma_config(drv_data->dma_channel, dma_config);
716 set_dma_start_addr(drv_data->dma_channel,
Bryan Wua32c6912007-12-04 23:45:15 -0800717 (unsigned long)drv_data->tx);
Bryan Wubb90eb02007-12-04 23:45:18 -0800718 enable_dma(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700719
Sonic Zhang07612e52007-12-04 23:45:21 -0800720 /* start SPI transfer */
Mike Frysinger11d6f592009-04-06 19:00:41 -0700721 write_CTRL(drv_data, cr | BIT_CTL_TIMOD_DMA_TX);
Sonic Zhang07612e52007-12-04 23:45:21 -0800722
723 /* just return here, there can only be one transfer
724 * in this mode
725 */
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700726 message->status = 0;
Mike Frysinger138f97c2009-04-06 19:00:50 -0700727 bfin_spi_giveback(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700728 return;
729 }
730
731 /* In dma mode, rx or tx must be NULL in one transfer */
Mike Frysinger7aec3562009-04-06 19:00:36 -0700732 dma_config = (RESTART | dma_width | DI_EN);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700733 if (drv_data->rx != NULL) {
734 /* set transfer mode, and enable SPI */
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700735 dev_dbg(&drv_data->pdev->dev, "doing DMA in to %p (size %zx)\n",
736 drv_data->rx, drv_data->len_in_bytes);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700737
Vitja Makarov8cf58582009-04-06 19:00:31 -0700738 /* invalidate caches, if needed */
Jie Zhang67834fa2009-06-10 06:26:26 +0000739 if (bfin_addr_dcacheable((unsigned long) drv_data->rx))
Vitja Makarov8cf58582009-04-06 19:00:31 -0700740 invalidate_dcache_range((unsigned long) drv_data->rx,
741 (unsigned long) (drv_data->rx +
Mike Frysingerace32862009-04-06 19:00:34 -0700742 drv_data->len_in_bytes));
Vitja Makarov8cf58582009-04-06 19:00:31 -0700743
Mike Frysinger7aec3562009-04-06 19:00:36 -0700744 dma_config |= WNR;
745 dma_start_addr = (unsigned long)drv_data->rx;
Mike Frysingerb31e27a2009-04-06 19:00:39 -0700746 cr |= BIT_CTL_TIMOD_DMA_RX | BIT_CTL_SENDOPT;
Sonic Zhang07612e52007-12-04 23:45:21 -0800747
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700748 } else if (drv_data->tx != NULL) {
Bryan Wu88b40362007-05-21 18:32:16 +0800749 dev_dbg(&drv_data->pdev->dev, "doing DMA out.\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700750
Vitja Makarov8cf58582009-04-06 19:00:31 -0700751 /* flush caches, if needed */
Jie Zhang67834fa2009-06-10 06:26:26 +0000752 if (bfin_addr_dcacheable((unsigned long) drv_data->tx))
Vitja Makarov8cf58582009-04-06 19:00:31 -0700753 flush_dcache_range((unsigned long) drv_data->tx,
754 (unsigned long) (drv_data->tx +
Mike Frysingerace32862009-04-06 19:00:34 -0700755 drv_data->len_in_bytes));
Vitja Makarov8cf58582009-04-06 19:00:31 -0700756
Mike Frysinger7aec3562009-04-06 19:00:36 -0700757 dma_start_addr = (unsigned long)drv_data->tx;
Mike Frysingerb31e27a2009-04-06 19:00:39 -0700758 cr |= BIT_CTL_TIMOD_DMA_TX;
Sonic Zhang07612e52007-12-04 23:45:21 -0800759
Mike Frysinger7aec3562009-04-06 19:00:36 -0700760 } else
761 BUG();
762
Mike Frysinger11d6f592009-04-06 19:00:41 -0700763 /* oh man, here there be monsters ... and i dont mean the
764 * fluffy cute ones from pixar, i mean the kind that'll eat
765 * your data, kick your dog, and love it all. do *not* try
766 * and change these lines unless you (1) heavily test DMA
767 * with SPI flashes on a loaded system (e.g. ping floods),
768 * (2) know just how broken the DMA engine interaction with
769 * the SPI peripheral is, and (3) have someone else to blame
770 * when you screw it all up anyways.
771 */
Mike Frysinger7aec3562009-04-06 19:00:36 -0700772 set_dma_start_addr(drv_data->dma_channel, dma_start_addr);
Mike Frysinger11d6f592009-04-06 19:00:41 -0700773 set_dma_config(drv_data->dma_channel, dma_config);
774 local_irq_save(flags);
Mike Frysingera963ea82009-04-06 19:00:43 -0700775 SSYNC();
Mike Frysinger11d6f592009-04-06 19:00:41 -0700776 write_CTRL(drv_data, cr);
Mike Frysingera963ea82009-04-06 19:00:43 -0700777 enable_dma(drv_data->dma_channel);
Mike Frysinger11d6f592009-04-06 19:00:41 -0700778 dma_enable_irq(drv_data->dma_channel);
779 local_irq_restore(flags);
Mike Frysinger7aec3562009-04-06 19:00:36 -0700780
Yi Lif6a6d962009-06-03 09:46:22 +0000781 return;
782 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700783
Mike Frysinger5e8592d2009-12-18 18:00:10 +0000784 /*
785 * We always use SPI_WRITE mode (transfer starts with TDBR write).
786 * SPI_READ mode (transfer starts with RDBR read) seems to have
787 * problems with setting up the output value in TDBR prior to the
788 * start of the transfer.
789 */
790 write_CTRL(drv_data, cr | BIT_CTL_TXMOD);
791
Yi Lif6a6d962009-06-03 09:46:22 +0000792 if (chip->pio_interrupt) {
Mike Frysinger5e8592d2009-12-18 18:00:10 +0000793 /* SPI irq should have been disabled by now */
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700794
Yi Lif6a6d962009-06-03 09:46:22 +0000795 /* discard old RX data and clear RXS */
796 bfin_spi_dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700797
Yi Lif6a6d962009-06-03 09:46:22 +0000798 /* start transfer */
799 if (drv_data->tx == NULL)
800 write_TDBR(drv_data, chip->idle_tx_val);
801 else {
Mike Frysinger033f44b2009-12-18 17:38:04 +0000802 if (bits_per_word == 8)
Yi Lif6a6d962009-06-03 09:46:22 +0000803 write_TDBR(drv_data, (*(u8 *) (drv_data->tx)));
Mike Frysinger033f44b2009-12-18 17:38:04 +0000804 else
Yi Lif6a6d962009-06-03 09:46:22 +0000805 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
806 drv_data->tx += drv_data->n_bytes;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700807 }
808
Yi Lif6a6d962009-06-03 09:46:22 +0000809 /* once TDBR is empty, interrupt is triggered */
810 enable_irq(drv_data->spi_irq);
811 return;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700812 }
Yi Lif6a6d962009-06-03 09:46:22 +0000813
814 /* IO mode */
815 dev_dbg(&drv_data->pdev->dev, "doing IO transfer\n");
816
Yi Lif6a6d962009-06-03 09:46:22 +0000817 if (full_duplex) {
818 /* full duplex mode */
819 BUG_ON((drv_data->tx_end - drv_data->tx) !=
820 (drv_data->rx_end - drv_data->rx));
821 dev_dbg(&drv_data->pdev->dev,
822 "IO duplex: cr is 0x%x\n", cr);
823
Mike Frysinger9c4542c2009-09-24 01:04:04 +0000824 drv_data->ops->duplex(drv_data);
Yi Lif6a6d962009-06-03 09:46:22 +0000825
826 if (drv_data->tx != drv_data->tx_end)
827 tranf_success = 0;
828 } else if (drv_data->tx != NULL) {
829 /* write only half duplex */
830 dev_dbg(&drv_data->pdev->dev,
831 "IO write: cr is 0x%x\n", cr);
832
Mike Frysinger9c4542c2009-09-24 01:04:04 +0000833 drv_data->ops->write(drv_data);
Yi Lif6a6d962009-06-03 09:46:22 +0000834
835 if (drv_data->tx != drv_data->tx_end)
836 tranf_success = 0;
837 } else if (drv_data->rx != NULL) {
838 /* read only half duplex */
839 dev_dbg(&drv_data->pdev->dev,
840 "IO read: cr is 0x%x\n", cr);
841
Mike Frysinger9c4542c2009-09-24 01:04:04 +0000842 drv_data->ops->read(drv_data);
Yi Lif6a6d962009-06-03 09:46:22 +0000843 if (drv_data->rx != drv_data->rx_end)
844 tranf_success = 0;
845 }
846
847 if (!tranf_success) {
848 dev_dbg(&drv_data->pdev->dev,
849 "IO write error!\n");
850 message->state = ERROR_STATE;
851 } else {
852 /* Update total byte transfered */
853 message->actual_length += drv_data->len_in_bytes;
854 /* Move to next transfer of this msg */
855 message->state = bfin_spi_next_transfer(drv_data);
856 if (drv_data->cs_change)
857 bfin_spi_cs_deactive(drv_data, chip);
858 }
859
860 /* Schedule next transfer tasklet */
861 tasklet_schedule(&drv_data->pump_transfers);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700862}
863
864/* pop a msg from queue and kick off real transfer */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700865static void bfin_spi_pump_messages(struct work_struct *work)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700866{
Mike Frysingerb9f139a2009-09-24 01:27:47 +0000867 struct master_data *drv_data;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700868 unsigned long flags;
869
Mike Frysingerb9f139a2009-09-24 01:27:47 +0000870 drv_data = container_of(work, struct master_data, pump_messages);
Bryan Wu131b17d2007-12-04 23:45:12 -0800871
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700872 /* Lock queue and check for queue work */
873 spin_lock_irqsave(&drv_data->lock, flags);
Mike Frysingerf4f50c32009-09-24 00:41:49 +0000874 if (list_empty(&drv_data->queue) || !drv_data->running) {
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700875 /* pumper kicked off but no work to do */
876 drv_data->busy = 0;
877 spin_unlock_irqrestore(&drv_data->lock, flags);
878 return;
879 }
880
881 /* Make sure we are not already running a message */
882 if (drv_data->cur_msg) {
883 spin_unlock_irqrestore(&drv_data->lock, flags);
884 return;
885 }
886
887 /* Extract head of queue */
888 drv_data->cur_msg = list_entry(drv_data->queue.next,
889 struct spi_message, queue);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800890
891 /* Setup the SSP using the per chip configuration */
892 drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
Mike Frysinger138f97c2009-04-06 19:00:50 -0700893 bfin_spi_restore_state(drv_data);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800894
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700895 list_del_init(&drv_data->cur_msg->queue);
896
897 /* Initial message state */
898 drv_data->cur_msg->state = START_STATE;
899 drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
900 struct spi_transfer, transfer_list);
901
Bryan Wu5fec5b52007-12-04 23:45:13 -0800902 dev_dbg(&drv_data->pdev->dev, "got a message to pump, "
903 "state is set to: baud %d, flag 0x%x, ctl 0x%x\n",
904 drv_data->cur_chip->baud, drv_data->cur_chip->flag,
905 drv_data->cur_chip->ctl_reg);
Bryan Wu131b17d2007-12-04 23:45:12 -0800906
907 dev_dbg(&drv_data->pdev->dev,
Bryan Wu88b40362007-05-21 18:32:16 +0800908 "the first transfer len is %d\n",
909 drv_data->cur_transfer->len);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700910
911 /* Mark as busy and launch transfers */
912 tasklet_schedule(&drv_data->pump_transfers);
913
914 drv_data->busy = 1;
915 spin_unlock_irqrestore(&drv_data->lock, flags);
916}
917
918/*
919 * got a msg to transfer, queue it in drv_data->queue.
920 * And kick off message pumper
921 */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700922static int bfin_spi_transfer(struct spi_device *spi, struct spi_message *msg)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700923{
Mike Frysingerb9f139a2009-09-24 01:27:47 +0000924 struct master_data *drv_data = spi_master_get_devdata(spi->master);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700925 unsigned long flags;
926
927 spin_lock_irqsave(&drv_data->lock, flags);
928
Mike Frysingerf4f50c32009-09-24 00:41:49 +0000929 if (!drv_data->running) {
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700930 spin_unlock_irqrestore(&drv_data->lock, flags);
931 return -ESHUTDOWN;
932 }
933
934 msg->actual_length = 0;
935 msg->status = -EINPROGRESS;
936 msg->state = START_STATE;
937
Bryan Wu88b40362007-05-21 18:32:16 +0800938 dev_dbg(&spi->dev, "adding an msg in transfer() \n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700939 list_add_tail(&msg->queue, &drv_data->queue);
940
Mike Frysingerf4f50c32009-09-24 00:41:49 +0000941 if (drv_data->running && !drv_data->busy)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700942 queue_work(drv_data->workqueue, &drv_data->pump_messages);
943
944 spin_unlock_irqrestore(&drv_data->lock, flags);
945
946 return 0;
947}
948
Sonic Zhang12e17c42007-12-04 23:45:16 -0800949#define MAX_SPI_SSEL 7
950
Mike Frysinger4160bde2009-04-06 19:00:40 -0700951static u16 ssel[][MAX_SPI_SSEL] = {
Sonic Zhang12e17c42007-12-04 23:45:16 -0800952 {P_SPI0_SSEL1, P_SPI0_SSEL2, P_SPI0_SSEL3,
953 P_SPI0_SSEL4, P_SPI0_SSEL5,
954 P_SPI0_SSEL6, P_SPI0_SSEL7},
955
956 {P_SPI1_SSEL1, P_SPI1_SSEL2, P_SPI1_SSEL3,
957 P_SPI1_SSEL4, P_SPI1_SSEL5,
958 P_SPI1_SSEL6, P_SPI1_SSEL7},
959
960 {P_SPI2_SSEL1, P_SPI2_SSEL2, P_SPI2_SSEL3,
961 P_SPI2_SSEL4, P_SPI2_SSEL5,
962 P_SPI2_SSEL6, P_SPI2_SSEL7},
963};
964
Mike Frysingerab09e042009-09-23 23:32:34 +0000965/* setup for devices (may be called multiple times -- not just first setup) */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700966static int bfin_spi_setup(struct spi_device *spi)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700967{
Daniel Mackac01e972009-03-25 00:18:35 +0000968 struct bfin5xx_spi_chip *chip_info;
Mike Frysingerb9f139a2009-09-24 01:27:47 +0000969 struct slave_data *chip = NULL;
970 struct master_data *drv_data = spi_master_get_devdata(spi->master);
Mike Frysinger5b47bcd2009-12-18 17:43:31 +0000971 u16 bfin_ctl_reg;
Daniel Mackac01e972009-03-25 00:18:35 +0000972 int ret = -EINVAL;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700973
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700974 /* Only alloc (or use chip_info) on first setup */
Daniel Mackac01e972009-03-25 00:18:35 +0000975 chip_info = NULL;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700976 chip = spi_get_ctldata(spi);
977 if (chip == NULL) {
Daniel Mackac01e972009-03-25 00:18:35 +0000978 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
979 if (!chip) {
980 dev_err(&spi->dev, "cannot allocate chip data\n");
981 ret = -ENOMEM;
982 goto error;
983 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700984
985 chip->enable_dma = 0;
986 chip_info = spi->controller_data;
987 }
988
Mike Frysinger5b47bcd2009-12-18 17:43:31 +0000989 /* Let people set non-standard bits directly */
990 bfin_ctl_reg = BIT_CTL_OPENDRAIN | BIT_CTL_EMISO |
991 BIT_CTL_PSSE | BIT_CTL_GM | BIT_CTL_SZ;
992
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700993 /* chip_info isn't always needed */
994 if (chip_info) {
Mike Frysinger2ed35512007-12-04 23:45:14 -0800995 /* Make sure people stop trying to set fields via ctl_reg
996 * when they should actually be using common SPI framework.
Mike Frysinger90008a62009-10-15 04:13:29 +0000997 * Currently we let through: WOM EMISO PSSE GM SZ.
Mike Frysinger2ed35512007-12-04 23:45:14 -0800998 * Not sure if a user actually needs/uses any of these,
999 * but let's assume (for now) they do.
1000 */
Mike Frysinger5b47bcd2009-12-18 17:43:31 +00001001 if (chip_info->ctl_reg & ~bfin_ctl_reg) {
Mike Frysinger2ed35512007-12-04 23:45:14 -08001002 dev_err(&spi->dev, "do not set bits in ctl_reg "
1003 "that the SPI framework manages\n");
Daniel Mackac01e972009-03-25 00:18:35 +00001004 goto error;
Mike Frysinger2ed35512007-12-04 23:45:14 -08001005 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001006 chip->enable_dma = chip_info->enable_dma != 0
1007 && drv_data->master_info->enable_dma;
1008 chip->ctl_reg = chip_info->ctl_reg;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001009 chip->cs_chg_udelay = chip_info->cs_chg_udelay;
Wolfgang Muees93b61bd2009-04-06 19:00:53 -07001010 chip->idle_tx_val = chip_info->idle_tx_val;
Yi Lif6a6d962009-06-03 09:46:22 +00001011 chip->pio_interrupt = chip_info->pio_interrupt;
Mike Frysinger033f44b2009-12-18 17:38:04 +00001012 spi->bits_per_word = chip_info->bits_per_word;
Mike Frysinger5b47bcd2009-12-18 17:43:31 +00001013 } else {
1014 /* force a default base state */
1015 chip->ctl_reg &= bfin_ctl_reg;
Mike Frysinger033f44b2009-12-18 17:38:04 +00001016 }
1017
1018 if (spi->bits_per_word != 8 && spi->bits_per_word != 16) {
1019 dev_err(&spi->dev, "%d bits_per_word is not supported\n",
1020 spi->bits_per_word);
1021 goto error;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001022 }
1023
1024 /* translate common spi framework into our register */
Mike Frysinger7715aad2010-02-25 10:00:55 +00001025 if (spi->mode & ~(SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST)) {
1026 dev_err(&spi->dev, "unsupported spi modes detected\n");
1027 goto error;
1028 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001029 if (spi->mode & SPI_CPOL)
Mike Frysinger90008a62009-10-15 04:13:29 +00001030 chip->ctl_reg |= BIT_CTL_CPOL;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001031 if (spi->mode & SPI_CPHA)
Mike Frysinger90008a62009-10-15 04:13:29 +00001032 chip->ctl_reg |= BIT_CTL_CPHA;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001033 if (spi->mode & SPI_LSB_FIRST)
Mike Frysinger90008a62009-10-15 04:13:29 +00001034 chip->ctl_reg |= BIT_CTL_LSBF;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001035 /* we dont support running in slave mode (yet?) */
Mike Frysinger90008a62009-10-15 04:13:29 +00001036 chip->ctl_reg |= BIT_CTL_MASTER;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001037
1038 /*
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001039 * Notice: for blackfin, the speed_hz is the value of register
1040 * SPI_BAUD, not the real baudrate
1041 */
1042 chip->baud = hz_to_spi_baud(spi->max_speed_hz);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001043 chip->chip_select_num = spi->chip_select;
Barry Songd3cc71f2009-11-17 09:45:59 +00001044 if (chip->chip_select_num < MAX_CTRL_CS)
1045 chip->flag = (1 << spi->chip_select) << 8;
1046 else
1047 chip->cs_gpio = chip->chip_select_num - MAX_CTRL_CS;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001048
Yi Lif6a6d962009-06-03 09:46:22 +00001049 if (chip->enable_dma && chip->pio_interrupt) {
1050 dev_err(&spi->dev, "enable_dma is set, "
1051 "do not set pio_interrupt\n");
1052 goto error;
1053 }
Daniel Mackac01e972009-03-25 00:18:35 +00001054 /*
1055 * if any one SPI chip is registered and wants DMA, request the
1056 * DMA channel for it
1057 */
1058 if (chip->enable_dma && !drv_data->dma_requested) {
1059 /* register dma irq handler */
1060 ret = request_dma(drv_data->dma_channel, "BFIN_SPI_DMA");
1061 if (ret) {
1062 dev_err(&spi->dev,
1063 "Unable to request BlackFin SPI DMA channel\n");
1064 goto error;
1065 }
1066 drv_data->dma_requested = 1;
1067
1068 ret = set_dma_callback(drv_data->dma_channel,
1069 bfin_spi_dma_irq_handler, drv_data);
1070 if (ret) {
1071 dev_err(&spi->dev, "Unable to set dma callback\n");
1072 goto error;
1073 }
1074 dma_disable_irq(drv_data->dma_channel);
1075 }
1076
Yi Lif6a6d962009-06-03 09:46:22 +00001077 if (chip->pio_interrupt && !drv_data->irq_requested) {
1078 ret = request_irq(drv_data->spi_irq, bfin_spi_pio_irq_handler,
1079 IRQF_DISABLED, "BFIN_SPI", drv_data);
1080 if (ret) {
1081 dev_err(&spi->dev, "Unable to register spi IRQ\n");
1082 goto error;
1083 }
1084 drv_data->irq_requested = 1;
1085 /* we use write mode, spi irq has to be disabled here */
1086 disable_irq(drv_data->spi_irq);
1087 }
1088
Barry Songd3cc71f2009-11-17 09:45:59 +00001089 if (chip->chip_select_num >= MAX_CTRL_CS) {
Daniel Mackac01e972009-03-25 00:18:35 +00001090 ret = gpio_request(chip->cs_gpio, spi->modalias);
1091 if (ret) {
1092 dev_err(&spi->dev, "gpio_request() error\n");
1093 goto pin_error;
1094 }
1095 gpio_direction_output(chip->cs_gpio, 1);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001096 }
1097
Joe Perches898eb712007-10-18 03:06:30 -07001098 dev_dbg(&spi->dev, "setup spi chip %s, width is %d, dma is %d\n",
Mike Frysinger033f44b2009-12-18 17:38:04 +00001099 spi->modalias, spi->bits_per_word, chip->enable_dma);
Bryan Wu88b40362007-05-21 18:32:16 +08001100 dev_dbg(&spi->dev, "ctl_reg is 0x%x, flag_reg is 0x%x\n",
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001101 chip->ctl_reg, chip->flag);
1102
1103 spi_set_ctldata(spi, chip);
1104
Sonic Zhang12e17c42007-12-04 23:45:16 -08001105 dev_dbg(&spi->dev, "chip select number is %d\n", chip->chip_select_num);
Barry Songd3cc71f2009-11-17 09:45:59 +00001106 if (chip->chip_select_num < MAX_CTRL_CS) {
Daniel Mackac01e972009-03-25 00:18:35 +00001107 ret = peripheral_request(ssel[spi->master->bus_num]
1108 [chip->chip_select_num-1], spi->modalias);
1109 if (ret) {
1110 dev_err(&spi->dev, "peripheral_request() error\n");
1111 goto pin_error;
1112 }
1113 }
Sonic Zhang12e17c42007-12-04 23:45:16 -08001114
Barry Song82216102009-06-17 10:10:53 +00001115 bfin_spi_cs_enable(drv_data, chip);
Mike Frysinger138f97c2009-04-06 19:00:50 -07001116 bfin_spi_cs_deactive(drv_data, chip);
Sonic Zhang07612e52007-12-04 23:45:21 -08001117
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001118 return 0;
Daniel Mackac01e972009-03-25 00:18:35 +00001119
1120 pin_error:
Barry Songd3cc71f2009-11-17 09:45:59 +00001121 if (chip->chip_select_num >= MAX_CTRL_CS)
Daniel Mackac01e972009-03-25 00:18:35 +00001122 gpio_free(chip->cs_gpio);
1123 else
1124 peripheral_free(ssel[spi->master->bus_num]
1125 [chip->chip_select_num - 1]);
1126 error:
1127 if (chip) {
1128 if (drv_data->dma_requested)
1129 free_dma(drv_data->dma_channel);
1130 drv_data->dma_requested = 0;
1131
1132 kfree(chip);
1133 /* prevent free 'chip' twice */
1134 spi_set_ctldata(spi, NULL);
1135 }
1136
1137 return ret;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001138}
1139
1140/*
1141 * callback for spi framework.
1142 * clean driver specific data
1143 */
Mike Frysinger138f97c2009-04-06 19:00:50 -07001144static void bfin_spi_cleanup(struct spi_device *spi)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001145{
Mike Frysingerb9f139a2009-09-24 01:27:47 +00001146 struct slave_data *chip = spi_get_ctldata(spi);
1147 struct master_data *drv_data = spi_master_get_devdata(spi->master);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001148
Mike Frysingere7d02e32009-04-06 19:00:51 -07001149 if (!chip)
1150 return;
1151
Barry Songd3cc71f2009-11-17 09:45:59 +00001152 if (chip->chip_select_num < MAX_CTRL_CS) {
Sonic Zhang12e17c42007-12-04 23:45:16 -08001153 peripheral_free(ssel[spi->master->bus_num]
1154 [chip->chip_select_num-1]);
Barry Song82216102009-06-17 10:10:53 +00001155 bfin_spi_cs_disable(drv_data, chip);
Barry Songd3cc71f2009-11-17 09:45:59 +00001156 } else
Michael Hennerich42c78b22009-04-06 19:00:51 -07001157 gpio_free(chip->cs_gpio);
1158
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001159 kfree(chip);
Daniel Mackac01e972009-03-25 00:18:35 +00001160 /* prevent free 'chip' twice */
1161 spi_set_ctldata(spi, NULL);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001162}
1163
Mike Frysingerb9f139a2009-09-24 01:27:47 +00001164static inline int bfin_spi_init_queue(struct master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001165{
1166 INIT_LIST_HEAD(&drv_data->queue);
1167 spin_lock_init(&drv_data->lock);
1168
Mike Frysingerf4f50c32009-09-24 00:41:49 +00001169 drv_data->running = false;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001170 drv_data->busy = 0;
1171
1172 /* init transfer tasklet */
1173 tasklet_init(&drv_data->pump_transfers,
Mike Frysinger138f97c2009-04-06 19:00:50 -07001174 bfin_spi_pump_transfers, (unsigned long)drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001175
1176 /* init messages workqueue */
Mike Frysinger138f97c2009-04-06 19:00:50 -07001177 INIT_WORK(&drv_data->pump_messages, bfin_spi_pump_messages);
Kay Sievers6c7377a2009-03-24 16:38:21 -07001178 drv_data->workqueue = create_singlethread_workqueue(
1179 dev_name(drv_data->master->dev.parent));
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001180 if (drv_data->workqueue == NULL)
1181 return -EBUSY;
1182
1183 return 0;
1184}
1185
Mike Frysingerb9f139a2009-09-24 01:27:47 +00001186static inline int bfin_spi_start_queue(struct master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001187{
1188 unsigned long flags;
1189
1190 spin_lock_irqsave(&drv_data->lock, flags);
1191
Mike Frysingerf4f50c32009-09-24 00:41:49 +00001192 if (drv_data->running || drv_data->busy) {
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001193 spin_unlock_irqrestore(&drv_data->lock, flags);
1194 return -EBUSY;
1195 }
1196
Mike Frysingerf4f50c32009-09-24 00:41:49 +00001197 drv_data->running = true;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001198 drv_data->cur_msg = NULL;
1199 drv_data->cur_transfer = NULL;
1200 drv_data->cur_chip = NULL;
1201 spin_unlock_irqrestore(&drv_data->lock, flags);
1202
1203 queue_work(drv_data->workqueue, &drv_data->pump_messages);
1204
1205 return 0;
1206}
1207
Mike Frysingerb9f139a2009-09-24 01:27:47 +00001208static inline int bfin_spi_stop_queue(struct master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001209{
1210 unsigned long flags;
1211 unsigned limit = 500;
1212 int status = 0;
1213
1214 spin_lock_irqsave(&drv_data->lock, flags);
1215
1216 /*
1217 * This is a bit lame, but is optimized for the common execution path.
1218 * A wait_queue on the drv_data->busy could be used, but then the common
1219 * execution path (pump_messages) would be required to call wake_up or
1220 * friends on every SPI message. Do this instead
1221 */
Mike Frysingerf4f50c32009-09-24 00:41:49 +00001222 drv_data->running = false;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001223 while (!list_empty(&drv_data->queue) && drv_data->busy && limit--) {
1224 spin_unlock_irqrestore(&drv_data->lock, flags);
1225 msleep(10);
1226 spin_lock_irqsave(&drv_data->lock, flags);
1227 }
1228
1229 if (!list_empty(&drv_data->queue) || drv_data->busy)
1230 status = -EBUSY;
1231
1232 spin_unlock_irqrestore(&drv_data->lock, flags);
1233
1234 return status;
1235}
1236
Mike Frysingerb9f139a2009-09-24 01:27:47 +00001237static inline int bfin_spi_destroy_queue(struct master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001238{
1239 int status;
1240
Mike Frysinger138f97c2009-04-06 19:00:50 -07001241 status = bfin_spi_stop_queue(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001242 if (status != 0)
1243 return status;
1244
1245 destroy_workqueue(drv_data->workqueue);
1246
1247 return 0;
1248}
1249
Mike Frysinger138f97c2009-04-06 19:00:50 -07001250static int __init bfin_spi_probe(struct platform_device *pdev)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001251{
1252 struct device *dev = &pdev->dev;
1253 struct bfin5xx_spi_master *platform_info;
1254 struct spi_master *master;
Mike Frysinger2a045132009-09-24 01:28:54 +00001255 struct master_data *drv_data;
Bryan Wua32c6912007-12-04 23:45:15 -08001256 struct resource *res;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001257 int status = 0;
1258
1259 platform_info = dev->platform_data;
1260
1261 /* Allocate master with space for drv_data */
Mike Frysinger2a045132009-09-24 01:28:54 +00001262 master = spi_alloc_master(dev, sizeof(*drv_data));
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001263 if (!master) {
1264 dev_err(&pdev->dev, "can not alloc spi_master\n");
1265 return -ENOMEM;
1266 }
Bryan Wu131b17d2007-12-04 23:45:12 -08001267
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001268 drv_data = spi_master_get_devdata(master);
1269 drv_data->master = master;
1270 drv_data->master_info = platform_info;
1271 drv_data->pdev = pdev;
Bryan Wu003d9222007-12-04 23:45:22 -08001272 drv_data->pin_req = platform_info->pin_req;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001273
David Brownelle7db06b2009-06-17 16:26:04 -07001274 /* the spi->mode bits supported by this driver: */
1275 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST;
1276
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001277 master->bus_num = pdev->id;
1278 master->num_chipselect = platform_info->num_chipselect;
Mike Frysinger138f97c2009-04-06 19:00:50 -07001279 master->cleanup = bfin_spi_cleanup;
1280 master->setup = bfin_spi_setup;
1281 master->transfer = bfin_spi_transfer;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001282
Bryan Wua32c6912007-12-04 23:45:15 -08001283 /* Find and map our resources */
1284 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1285 if (res == NULL) {
1286 dev_err(dev, "Cannot get IORESOURCE_MEM\n");
1287 status = -ENOENT;
1288 goto out_error_get_res;
1289 }
1290
hartleys74947b82009-12-14 22:33:43 +00001291 drv_data->regs_base = ioremap(res->start, resource_size(res));
Bryan Wuf4521262007-12-04 23:45:22 -08001292 if (drv_data->regs_base == NULL) {
Bryan Wua32c6912007-12-04 23:45:15 -08001293 dev_err(dev, "Cannot map IO\n");
1294 status = -ENXIO;
1295 goto out_error_ioremap;
1296 }
1297
Yi Lif6a6d962009-06-03 09:46:22 +00001298 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1299 if (res == NULL) {
Bryan Wua32c6912007-12-04 23:45:15 -08001300 dev_err(dev, "No DMA channel specified\n");
1301 status = -ENOENT;
Yi Lif6a6d962009-06-03 09:46:22 +00001302 goto out_error_free_io;
1303 }
1304 drv_data->dma_channel = res->start;
1305
1306 drv_data->spi_irq = platform_get_irq(pdev, 0);
1307 if (drv_data->spi_irq < 0) {
1308 dev_err(dev, "No spi pio irq specified\n");
1309 status = -ENOENT;
1310 goto out_error_free_io;
Bryan Wua32c6912007-12-04 23:45:15 -08001311 }
1312
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001313 /* Initial and start queue */
Mike Frysinger138f97c2009-04-06 19:00:50 -07001314 status = bfin_spi_init_queue(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001315 if (status != 0) {
Bryan Wua32c6912007-12-04 23:45:15 -08001316 dev_err(dev, "problem initializing queue\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001317 goto out_error_queue_alloc;
1318 }
Bryan Wua32c6912007-12-04 23:45:15 -08001319
Mike Frysinger138f97c2009-04-06 19:00:50 -07001320 status = bfin_spi_start_queue(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001321 if (status != 0) {
Bryan Wua32c6912007-12-04 23:45:15 -08001322 dev_err(dev, "problem starting queue\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001323 goto out_error_queue_alloc;
1324 }
1325
Vitja Makarovf9e522c2008-04-08 17:41:57 -07001326 status = peripheral_request_list(drv_data->pin_req, DRV_NAME);
1327 if (status != 0) {
1328 dev_err(&pdev->dev, ": Requesting Peripherals failed\n");
1329 goto out_error_queue_alloc;
1330 }
1331
Wolfgang Mueesbb8beec2009-05-22 01:11:02 +00001332 /* Reset SPI registers. If these registers were used by the boot loader,
1333 * the sky may fall on your head if you enable the dma controller.
1334 */
1335 write_CTRL(drv_data, BIT_CTL_CPHA | BIT_CTL_MASTER);
1336 write_FLAG(drv_data, 0xFF00);
1337
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001338 /* Register with the SPI framework */
1339 platform_set_drvdata(pdev, drv_data);
1340 status = spi_register_master(master);
1341 if (status != 0) {
Bryan Wua32c6912007-12-04 23:45:15 -08001342 dev_err(dev, "problem registering spi master\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001343 goto out_error_queue_alloc;
1344 }
Bryan Wua32c6912007-12-04 23:45:15 -08001345
Bryan Wuf4521262007-12-04 23:45:22 -08001346 dev_info(dev, "%s, Version %s, regs_base@%p, dma channel@%d\n",
Bryan Wubb90eb02007-12-04 23:45:18 -08001347 DRV_DESC, DRV_VERSION, drv_data->regs_base,
1348 drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001349 return status;
1350
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001351out_error_queue_alloc:
Mike Frysinger138f97c2009-04-06 19:00:50 -07001352 bfin_spi_destroy_queue(drv_data);
Yi Lif6a6d962009-06-03 09:46:22 +00001353out_error_free_io:
Bryan Wubb90eb02007-12-04 23:45:18 -08001354 iounmap((void *) drv_data->regs_base);
Bryan Wua32c6912007-12-04 23:45:15 -08001355out_error_ioremap:
1356out_error_get_res:
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001357 spi_master_put(master);
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001358
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001359 return status;
1360}
1361
1362/* stop hardware and remove the driver */
Mike Frysinger138f97c2009-04-06 19:00:50 -07001363static int __devexit bfin_spi_remove(struct platform_device *pdev)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001364{
Mike Frysingerb9f139a2009-09-24 01:27:47 +00001365 struct master_data *drv_data = platform_get_drvdata(pdev);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001366 int status = 0;
1367
1368 if (!drv_data)
1369 return 0;
1370
1371 /* Remove the queue */
Mike Frysinger138f97c2009-04-06 19:00:50 -07001372 status = bfin_spi_destroy_queue(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001373 if (status != 0)
1374 return status;
1375
1376 /* Disable the SSP at the peripheral and SOC level */
1377 bfin_spi_disable(drv_data);
1378
1379 /* Release DMA */
1380 if (drv_data->master_info->enable_dma) {
Bryan Wubb90eb02007-12-04 23:45:18 -08001381 if (dma_channel_active(drv_data->dma_channel))
1382 free_dma(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001383 }
1384
Yi Lif6a6d962009-06-03 09:46:22 +00001385 if (drv_data->irq_requested) {
1386 free_irq(drv_data->spi_irq, drv_data);
1387 drv_data->irq_requested = 0;
1388 }
1389
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001390 /* Disconnect from the SPI framework */
1391 spi_unregister_master(drv_data->master);
1392
Bryan Wu003d9222007-12-04 23:45:22 -08001393 peripheral_free_list(drv_data->pin_req);
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001394
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001395 /* Prevent double remove */
1396 platform_set_drvdata(pdev, NULL);
1397
1398 return 0;
1399}
1400
1401#ifdef CONFIG_PM
Mike Frysinger138f97c2009-04-06 19:00:50 -07001402static int bfin_spi_suspend(struct platform_device *pdev, pm_message_t state)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001403{
Mike Frysingerb9f139a2009-09-24 01:27:47 +00001404 struct master_data *drv_data = platform_get_drvdata(pdev);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001405 int status = 0;
1406
Mike Frysinger138f97c2009-04-06 19:00:50 -07001407 status = bfin_spi_stop_queue(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001408 if (status != 0)
1409 return status;
1410
Barry Songb052fd02009-11-18 09:43:21 +00001411 drv_data->ctrl_reg = read_CTRL(drv_data);
1412 drv_data->flag_reg = read_FLAG(drv_data);
1413
1414 /*
1415 * reset SPI_CTL and SPI_FLG registers
1416 */
1417 write_CTRL(drv_data, BIT_CTL_CPHA | BIT_CTL_MASTER);
1418 write_FLAG(drv_data, 0xFF00);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001419
1420 return 0;
1421}
1422
Mike Frysinger138f97c2009-04-06 19:00:50 -07001423static int bfin_spi_resume(struct platform_device *pdev)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001424{
Mike Frysingerb9f139a2009-09-24 01:27:47 +00001425 struct master_data *drv_data = platform_get_drvdata(pdev);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001426 int status = 0;
1427
Barry Songb052fd02009-11-18 09:43:21 +00001428 write_CTRL(drv_data, drv_data->ctrl_reg);
1429 write_FLAG(drv_data, drv_data->flag_reg);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001430
1431 /* Start the queue running */
Mike Frysinger138f97c2009-04-06 19:00:50 -07001432 status = bfin_spi_start_queue(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001433 if (status != 0) {
1434 dev_err(&pdev->dev, "problem starting queue (%d)\n", status);
1435 return status;
1436 }
1437
1438 return 0;
1439}
1440#else
Mike Frysinger138f97c2009-04-06 19:00:50 -07001441#define bfin_spi_suspend NULL
1442#define bfin_spi_resume NULL
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001443#endif /* CONFIG_PM */
1444
Kay Sievers7e38c3c2008-04-10 21:29:20 -07001445MODULE_ALIAS("platform:bfin-spi");
Mike Frysinger138f97c2009-04-06 19:00:50 -07001446static struct platform_driver bfin_spi_driver = {
David Brownellfc3ba952007-08-30 23:56:24 -07001447 .driver = {
Bryan Wua32c6912007-12-04 23:45:15 -08001448 .name = DRV_NAME,
Bryan Wu88b40362007-05-21 18:32:16 +08001449 .owner = THIS_MODULE,
1450 },
Mike Frysinger138f97c2009-04-06 19:00:50 -07001451 .suspend = bfin_spi_suspend,
1452 .resume = bfin_spi_resume,
1453 .remove = __devexit_p(bfin_spi_remove),
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001454};
1455
Mike Frysinger138f97c2009-04-06 19:00:50 -07001456static int __init bfin_spi_init(void)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001457{
Mike Frysinger138f97c2009-04-06 19:00:50 -07001458 return platform_driver_probe(&bfin_spi_driver, bfin_spi_probe);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001459}
Mike Frysinger138f97c2009-04-06 19:00:50 -07001460module_init(bfin_spi_init);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001461
Mike Frysinger138f97c2009-04-06 19:00:50 -07001462static void __exit bfin_spi_exit(void)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001463{
Mike Frysinger138f97c2009-04-06 19:00:50 -07001464 platform_driver_unregister(&bfin_spi_driver);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001465}
Mike Frysinger138f97c2009-04-06 19:00:50 -07001466module_exit(bfin_spi_exit);