blob: b8d25f2b7038a2d3c5400641b34285556fb1077e [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 */
Mike Frysinger47885ce2011-06-17 04:16:56 -040061 struct bfin_spi_regs __iomem *regs;
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
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400125static void bfin_spi_enable(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700126{
Mike Frysinger47885ce2011-06-17 04:16:56 -0400127 bfin_write_or(&drv_data->regs->ctl, BIT_CTL_ENABLE);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700128}
129
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400130static void bfin_spi_disable(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700131{
Mike Frysinger47885ce2011-06-17 04:16:56 -0400132 bfin_write_and(&drv_data->regs->ctl, ~BIT_CTL_ENABLE);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700133}
134
135/* Caculate the SPI_BAUD register value based on input HZ */
136static u16 hz_to_spi_baud(u32 speed_hz)
137{
138 u_long sclk = get_sclk();
139 u16 spi_baud = (sclk / (2 * speed_hz));
140
141 if ((sclk % (2 * speed_hz)) > 0)
142 spi_baud++;
143
Michael Hennerich7513e002009-04-06 19:00:32 -0700144 if (spi_baud < MIN_SPI_BAUD_VAL)
145 spi_baud = MIN_SPI_BAUD_VAL;
146
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700147 return spi_baud;
148}
149
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400150static int bfin_spi_flush(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700151{
152 unsigned long limit = loops_per_jiffy << 1;
153
154 /* wait for stop and clear stat */
Mike Frysinger47885ce2011-06-17 04:16:56 -0400155 while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_SPIF) && --limit)
Bryan Wud8c05002007-12-04 23:45:21 -0800156 cpu_relax();
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700157
Mike Frysinger47885ce2011-06-17 04:16:56 -0400158 bfin_write(&drv_data->regs->stat, BIT_STAT_CLR);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700159
160 return limit;
161}
162
Bryan Wufad91c82007-12-04 23:45:14 -0800163/* Chip select operation functions for cs_change flag */
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400164static 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 -0800165{
Mike Frysinger47885ce2011-06-17 04:16:56 -0400166 if (likely(chip->chip_select_num < MAX_CTRL_CS))
167 bfin_write_and(&drv_data->regs->flg, ~chip->flag);
168 else
Michael Hennerich42c78b22009-04-06 19:00:51 -0700169 gpio_set_value(chip->cs_gpio, 0);
Bryan Wufad91c82007-12-04 23:45:14 -0800170}
171
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400172static void bfin_spi_cs_deactive(struct bfin_spi_master_data *drv_data,
173 struct bfin_spi_slave_data *chip)
Bryan Wufad91c82007-12-04 23:45:14 -0800174{
Mike Frysinger47885ce2011-06-17 04:16:56 -0400175 if (likely(chip->chip_select_num < MAX_CTRL_CS))
176 bfin_write_or(&drv_data->regs->flg, chip->flag);
177 else
Michael Hennerich42c78b22009-04-06 19:00:51 -0700178 gpio_set_value(chip->cs_gpio, 1);
Bryan Wu62310e52007-12-04 23:45:20 -0800179
180 /* Move delay here for consistency */
181 if (chip->cs_chg_udelay)
182 udelay(chip->cs_chg_udelay);
Bryan Wufad91c82007-12-04 23:45:14 -0800183}
184
Barry Song82216102009-06-17 10:10:53 +0000185/* enable or disable the pin muxed by GPIO and SPI CS to work as SPI CS */
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400186static inline void bfin_spi_cs_enable(struct bfin_spi_master_data *drv_data,
187 struct bfin_spi_slave_data *chip)
Barry Song82216102009-06-17 10:10:53 +0000188{
Mike Frysinger47885ce2011-06-17 04:16:56 -0400189 if (chip->chip_select_num < MAX_CTRL_CS)
190 bfin_write_or(&drv_data->regs->flg, chip->flag >> 8);
Barry Song82216102009-06-17 10:10:53 +0000191}
192
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400193static inline void bfin_spi_cs_disable(struct bfin_spi_master_data *drv_data,
194 struct bfin_spi_slave_data *chip)
Barry Song82216102009-06-17 10:10:53 +0000195{
Mike Frysinger47885ce2011-06-17 04:16:56 -0400196 if (chip->chip_select_num < MAX_CTRL_CS)
197 bfin_write_and(&drv_data->regs->flg, ~(chip->flag >> 8));
Barry Song82216102009-06-17 10:10:53 +0000198}
199
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700200/* stop controller and re-config current chip*/
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400201static void bfin_spi_restore_state(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700202{
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400203 struct bfin_spi_slave_data *chip = drv_data->cur_chip;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700204
205 /* Clear status and disable clock */
Mike Frysinger47885ce2011-06-17 04:16:56 -0400206 bfin_write(&drv_data->regs->stat, BIT_STAT_CLR);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700207 bfin_spi_disable(drv_data);
Bryan Wu88b40362007-05-21 18:32:16 +0800208 dev_dbg(&drv_data->pdev->dev, "restoring spi ctl state\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700209
Barry Song9677b0de2009-11-30 03:49:41 +0000210 SSYNC();
211
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700212 /* Load the registers */
Mike Frysinger47885ce2011-06-17 04:16:56 -0400213 bfin_write(&drv_data->regs->ctl, chip->ctl_reg);
214 bfin_write(&drv_data->regs->baud, chip->baud);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800215
216 bfin_spi_enable(drv_data);
Mike Frysinger138f97c2009-04-06 19:00:50 -0700217 bfin_spi_cs_active(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700218}
219
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700220/* used to kick off transfer in rx mode and read unwanted RX data */
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400221static inline void bfin_spi_dummy_read(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700222{
Mike Frysinger47885ce2011-06-17 04:16:56 -0400223 (void) bfin_read(&drv_data->regs->rdbr);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700224}
225
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400226static void bfin_spi_u8_writer(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700227{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700228 /* clear RXS (we check for RXS inside the loop) */
229 bfin_spi_dummy_read(drv_data);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800230
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700231 while (drv_data->tx < drv_data->tx_end) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400232 bfin_write(&drv_data->regs->tdbr, (*(u8 *) (drv_data->tx++)));
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700233 /* wait until transfer finished.
234 checking SPIF or TXS may not guarantee transfer completion */
Mike Frysinger47885ce2011-06-17 04:16:56 -0400235 while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800236 cpu_relax();
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700237 /* discard RX data and clear RXS */
238 bfin_spi_dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700239 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700240}
241
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400242static void bfin_spi_u8_reader(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700243{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700244 u16 tx_val = drv_data->cur_chip->idle_tx_val;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700245
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700246 /* discard old RX data and clear RXS */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700247 bfin_spi_dummy_read(drv_data);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800248
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700249 while (drv_data->rx < drv_data->rx_end) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400250 bfin_write(&drv_data->regs->tdbr, tx_val);
251 while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800252 cpu_relax();
Mike Frysinger47885ce2011-06-17 04:16:56 -0400253 *(u8 *) (drv_data->rx++) = bfin_read(&drv_data->regs->rdbr);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700254 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700255}
256
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400257static void bfin_spi_u8_duplex(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700258{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700259 /* discard old RX data and clear RXS */
260 bfin_spi_dummy_read(drv_data);
261
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700262 while (drv_data->rx < drv_data->rx_end) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400263 bfin_write(&drv_data->regs->tdbr, (*(u8 *) (drv_data->tx++)));
264 while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800265 cpu_relax();
Mike Frysinger47885ce2011-06-17 04:16:56 -0400266 *(u8 *) (drv_data->rx++) = bfin_read(&drv_data->regs->rdbr);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700267 }
268}
269
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400270static const struct bfin_spi_transfer_ops bfin_bfin_spi_transfer_ops_u8 = {
Mike Frysinger9c4542c2009-09-24 01:04:04 +0000271 .write = bfin_spi_u8_writer,
272 .read = bfin_spi_u8_reader,
273 .duplex = bfin_spi_u8_duplex,
274};
275
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400276static void bfin_spi_u16_writer(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700277{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700278 /* clear RXS (we check for RXS inside the loop) */
279 bfin_spi_dummy_read(drv_data);
Bryan Wu88b40362007-05-21 18:32:16 +0800280
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700281 while (drv_data->tx < drv_data->tx_end) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400282 bfin_write(&drv_data->regs->tdbr, (*(u16 *) (drv_data->tx)));
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700283 drv_data->tx += 2;
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700284 /* wait until transfer finished.
285 checking SPIF or TXS may not guarantee transfer completion */
Mike Frysinger47885ce2011-06-17 04:16:56 -0400286 while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_RXS))
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700287 cpu_relax();
288 /* discard RX data and clear RXS */
289 bfin_spi_dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700290 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700291}
292
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400293static void bfin_spi_u16_reader(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700294{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700295 u16 tx_val = drv_data->cur_chip->idle_tx_val;
Sonic Zhangcc487e72007-12-04 23:45:17 -0800296
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700297 /* discard old RX data and clear RXS */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700298 bfin_spi_dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700299
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700300 while (drv_data->rx < drv_data->rx_end) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400301 bfin_write(&drv_data->regs->tdbr, tx_val);
302 while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800303 cpu_relax();
Mike Frysinger47885ce2011-06-17 04:16:56 -0400304 *(u16 *) (drv_data->rx) = bfin_read(&drv_data->regs->rdbr);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700305 drv_data->rx += 2;
306 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700307}
308
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400309static void bfin_spi_u16_duplex(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700310{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700311 /* discard old RX data and clear RXS */
312 bfin_spi_dummy_read(drv_data);
313
314 while (drv_data->rx < drv_data->rx_end) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400315 bfin_write(&drv_data->regs->tdbr, (*(u16 *) (drv_data->tx)));
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700316 drv_data->tx += 2;
Mike Frysinger47885ce2011-06-17 04:16:56 -0400317 while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800318 cpu_relax();
Mike Frysinger47885ce2011-06-17 04:16:56 -0400319 *(u16 *) (drv_data->rx) = bfin_read(&drv_data->regs->rdbr);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700320 drv_data->rx += 2;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700321 }
322}
323
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400324static const struct bfin_spi_transfer_ops bfin_bfin_spi_transfer_ops_u16 = {
Mike Frysinger9c4542c2009-09-24 01:04:04 +0000325 .write = bfin_spi_u16_writer,
326 .read = bfin_spi_u16_reader,
327 .duplex = bfin_spi_u16_duplex,
328};
329
Rob Marise3595402010-04-06 04:12:00 +0000330/* test if there is more transfer to be done */
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400331static void *bfin_spi_next_transfer(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700332{
333 struct spi_message *msg = drv_data->cur_msg;
334 struct spi_transfer *trans = drv_data->cur_transfer;
335
336 /* Move to next transfer */
337 if (trans->transfer_list.next != &msg->transfers) {
338 drv_data->cur_transfer =
339 list_entry(trans->transfer_list.next,
340 struct spi_transfer, transfer_list);
341 return RUNNING_STATE;
342 } else
343 return DONE_STATE;
344}
345
346/*
347 * caller already set message->status;
348 * dma and pio irqs are blocked give finished message back
349 */
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400350static void bfin_spi_giveback(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700351{
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400352 struct bfin_spi_slave_data *chip = drv_data->cur_chip;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700353 struct spi_transfer *last_transfer;
354 unsigned long flags;
355 struct spi_message *msg;
356
357 spin_lock_irqsave(&drv_data->lock, flags);
358 msg = drv_data->cur_msg;
359 drv_data->cur_msg = NULL;
360 drv_data->cur_transfer = NULL;
361 drv_data->cur_chip = NULL;
362 queue_work(drv_data->workqueue, &drv_data->pump_messages);
363 spin_unlock_irqrestore(&drv_data->lock, flags);
364
365 last_transfer = list_entry(msg->transfers.prev,
366 struct spi_transfer, transfer_list);
367
368 msg->state = NULL;
369
Bryan Wufad91c82007-12-04 23:45:14 -0800370 if (!drv_data->cs_change)
Mike Frysinger138f97c2009-04-06 19:00:50 -0700371 bfin_spi_cs_deactive(drv_data, chip);
Bryan Wufad91c82007-12-04 23:45:14 -0800372
Yi Lib9b2a762009-04-06 19:00:49 -0700373 /* Not stop spi in autobuffer mode */
374 if (drv_data->tx_dma != 0xFFFF)
375 bfin_spi_disable(drv_data);
376
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700377 if (msg->complete)
378 msg->complete(msg->context);
379}
380
Yi Lif6a6d962009-06-03 09:46:22 +0000381/* spi data irq handler */
382static irqreturn_t bfin_spi_pio_irq_handler(int irq, void *dev_id)
383{
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400384 struct bfin_spi_master_data *drv_data = dev_id;
385 struct bfin_spi_slave_data *chip = drv_data->cur_chip;
Yi Lif6a6d962009-06-03 09:46:22 +0000386 struct spi_message *msg = drv_data->cur_msg;
387 int n_bytes = drv_data->n_bytes;
Bob Liu4d676fc2011-01-11 11:19:07 -0500388 int loop = 0;
Yi Lif6a6d962009-06-03 09:46:22 +0000389
390 /* wait until transfer finished. */
Mike Frysinger47885ce2011-06-17 04:16:56 -0400391 while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_RXS))
Yi Lif6a6d962009-06-03 09:46:22 +0000392 cpu_relax();
393
394 if ((drv_data->tx && drv_data->tx >= drv_data->tx_end) ||
395 (drv_data->rx && drv_data->rx >= (drv_data->rx_end - n_bytes))) {
396 /* last read */
397 if (drv_data->rx) {
398 dev_dbg(&drv_data->pdev->dev, "last read\n");
Bob Liu4d676fc2011-01-11 11:19:07 -0500399 if (n_bytes % 2) {
400 u16 *buf = (u16 *)drv_data->rx;
401 for (loop = 0; loop < n_bytes / 2; loop++)
Mike Frysinger47885ce2011-06-17 04:16:56 -0400402 *buf++ = bfin_read(&drv_data->regs->rdbr);
Bob Liu4d676fc2011-01-11 11:19:07 -0500403 } else {
404 u8 *buf = (u8 *)drv_data->rx;
405 for (loop = 0; loop < n_bytes; loop++)
Mike Frysinger47885ce2011-06-17 04:16:56 -0400406 *buf++ = bfin_read(&drv_data->regs->rdbr);
Bob Liu4d676fc2011-01-11 11:19:07 -0500407 }
Yi Lif6a6d962009-06-03 09:46:22 +0000408 drv_data->rx += n_bytes;
409 }
410
411 msg->actual_length += drv_data->len_in_bytes;
412 if (drv_data->cs_change)
413 bfin_spi_cs_deactive(drv_data, chip);
414 /* Move to next transfer */
415 msg->state = bfin_spi_next_transfer(drv_data);
416
Yi Li7370ed62009-12-07 08:07:01 +0000417 disable_irq_nosync(drv_data->spi_irq);
Yi Lif6a6d962009-06-03 09:46:22 +0000418
419 /* Schedule transfer tasklet */
420 tasklet_schedule(&drv_data->pump_transfers);
421 return IRQ_HANDLED;
422 }
423
424 if (drv_data->rx && drv_data->tx) {
425 /* duplex */
426 dev_dbg(&drv_data->pdev->dev, "duplex: write_TDBR\n");
Bob Liu4d676fc2011-01-11 11:19:07 -0500427 if (n_bytes % 2) {
428 u16 *buf = (u16 *)drv_data->rx;
429 u16 *buf2 = (u16 *)drv_data->tx;
430 for (loop = 0; loop < n_bytes / 2; loop++) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400431 *buf++ = bfin_read(&drv_data->regs->rdbr);
432 bfin_write(&drv_data->regs->tdbr, *buf2++);
Bob Liu4d676fc2011-01-11 11:19:07 -0500433 }
434 } else {
435 u8 *buf = (u8 *)drv_data->rx;
436 u8 *buf2 = (u8 *)drv_data->tx;
437 for (loop = 0; loop < n_bytes; loop++) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400438 *buf++ = bfin_read(&drv_data->regs->rdbr);
439 bfin_write(&drv_data->regs->tdbr, *buf2++);
Bob Liu4d676fc2011-01-11 11:19:07 -0500440 }
Yi Lif6a6d962009-06-03 09:46:22 +0000441 }
442 } else if (drv_data->rx) {
443 /* read */
444 dev_dbg(&drv_data->pdev->dev, "read: write_TDBR\n");
Bob Liu4d676fc2011-01-11 11:19:07 -0500445 if (n_bytes % 2) {
446 u16 *buf = (u16 *)drv_data->rx;
447 for (loop = 0; loop < n_bytes / 2; loop++) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400448 *buf++ = bfin_read(&drv_data->regs->rdbr);
449 bfin_write(&drv_data->regs->tdbr, chip->idle_tx_val);
Bob Liu4d676fc2011-01-11 11:19:07 -0500450 }
451 } else {
452 u8 *buf = (u8 *)drv_data->rx;
453 for (loop = 0; loop < n_bytes; loop++) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400454 *buf++ = bfin_read(&drv_data->regs->rdbr);
455 bfin_write(&drv_data->regs->tdbr, chip->idle_tx_val);
Bob Liu4d676fc2011-01-11 11:19:07 -0500456 }
457 }
Yi Lif6a6d962009-06-03 09:46:22 +0000458 } else if (drv_data->tx) {
459 /* write */
460 dev_dbg(&drv_data->pdev->dev, "write: write_TDBR\n");
Bob Liu4d676fc2011-01-11 11:19:07 -0500461 if (n_bytes % 2) {
462 u16 *buf = (u16 *)drv_data->tx;
463 for (loop = 0; loop < n_bytes / 2; loop++) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400464 bfin_read(&drv_data->regs->rdbr);
465 bfin_write(&drv_data->regs->tdbr, *buf++);
Bob Liu4d676fc2011-01-11 11:19:07 -0500466 }
467 } else {
468 u8 *buf = (u8 *)drv_data->tx;
469 for (loop = 0; loop < n_bytes; loop++) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400470 bfin_read(&drv_data->regs->rdbr);
471 bfin_write(&drv_data->regs->tdbr, *buf++);
Bob Liu4d676fc2011-01-11 11:19:07 -0500472 }
473 }
Yi Lif6a6d962009-06-03 09:46:22 +0000474 }
475
476 if (drv_data->tx)
477 drv_data->tx += n_bytes;
478 if (drv_data->rx)
479 drv_data->rx += n_bytes;
480
481 return IRQ_HANDLED;
482}
483
Mike Frysinger138f97c2009-04-06 19:00:50 -0700484static irqreturn_t bfin_spi_dma_irq_handler(int irq, void *dev_id)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700485{
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400486 struct bfin_spi_master_data *drv_data = dev_id;
487 struct bfin_spi_slave_data *chip = drv_data->cur_chip;
Bryan Wubb90eb02007-12-04 23:45:18 -0800488 struct spi_message *msg = drv_data->cur_msg;
Mike Frysingeraaaf9392009-04-06 19:00:42 -0700489 unsigned long timeout;
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700490 unsigned short dmastat = get_dma_curr_irqstat(drv_data->dma_channel);
Mike Frysinger47885ce2011-06-17 04:16:56 -0400491 u16 spistat = bfin_read(&drv_data->regs->stat);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700492
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700493 dev_dbg(&drv_data->pdev->dev,
494 "in dma_irq_handler dmastat:0x%x spistat:0x%x\n",
495 dmastat, spistat);
496
Michael Hennerich782a8952010-10-22 02:01:48 -0400497 if (drv_data->rx != NULL) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400498 u16 cr = bfin_read(&drv_data->regs->ctl);
Michael Hennerich782a8952010-10-22 02:01:48 -0400499 /* discard old RX data and clear RXS */
500 bfin_spi_dummy_read(drv_data);
Mike Frysinger47885ce2011-06-17 04:16:56 -0400501 bfin_write(&drv_data->regs->ctl, cr & ~BIT_CTL_ENABLE); /* Disable SPI */
502 bfin_write(&drv_data->regs->ctl, cr & ~BIT_CTL_TIMOD); /* Restore State */
503 bfin_write(&drv_data->regs->stat, BIT_STAT_CLR); /* Clear Status */
Michael Hennerich782a8952010-10-22 02:01:48 -0400504 }
505
Bryan Wubb90eb02007-12-04 23:45:18 -0800506 clear_dma_irqstat(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700507
508 /*
Bryan Wud6fe89b2007-06-11 17:34:17 +0800509 * wait for the last transaction shifted out. HRM states:
510 * at this point there may still be data in the SPI DMA FIFO waiting
511 * to be transmitted ... software needs to poll TXS in the SPI_STAT
512 * register until it goes low for 2 successive reads
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700513 */
514 if (drv_data->tx != NULL) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400515 while ((bfin_read(&drv_data->regs->stat) & BIT_STAT_TXS) ||
516 (bfin_read(&drv_data->regs->stat) & BIT_STAT_TXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800517 cpu_relax();
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700518 }
519
Mike Frysingeraaaf9392009-04-06 19:00:42 -0700520 dev_dbg(&drv_data->pdev->dev,
521 "in dma_irq_handler dmastat:0x%x spistat:0x%x\n",
Mike Frysinger47885ce2011-06-17 04:16:56 -0400522 dmastat, bfin_read(&drv_data->regs->stat));
Mike Frysingeraaaf9392009-04-06 19:00:42 -0700523
524 timeout = jiffies + HZ;
Mike Frysinger47885ce2011-06-17 04:16:56 -0400525 while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_SPIF))
Mike Frysingeraaaf9392009-04-06 19:00:42 -0700526 if (!time_before(jiffies, timeout)) {
527 dev_warn(&drv_data->pdev->dev, "timeout waiting for SPIF");
528 break;
529 } else
530 cpu_relax();
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700531
Mike Frysinger90008a62009-10-15 04:13:29 +0000532 if ((dmastat & DMA_ERR) && (spistat & BIT_STAT_RBSY)) {
Mike Frysinger04b95d22009-04-06 19:00:35 -0700533 msg->state = ERROR_STATE;
534 dev_err(&drv_data->pdev->dev, "dma receive: fifo/buffer overflow\n");
535 } else {
536 msg->actual_length += drv_data->len_in_bytes;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700537
Mike Frysinger04b95d22009-04-06 19:00:35 -0700538 if (drv_data->cs_change)
Mike Frysinger138f97c2009-04-06 19:00:50 -0700539 bfin_spi_cs_deactive(drv_data, chip);
Bryan Wufad91c82007-12-04 23:45:14 -0800540
Mike Frysinger04b95d22009-04-06 19:00:35 -0700541 /* Move to next transfer */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700542 msg->state = bfin_spi_next_transfer(drv_data);
Mike Frysinger04b95d22009-04-06 19:00:35 -0700543 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700544
545 /* Schedule transfer tasklet */
546 tasklet_schedule(&drv_data->pump_transfers);
547
548 /* free the irq handler before next transfer */
Bryan Wu88b40362007-05-21 18:32:16 +0800549 dev_dbg(&drv_data->pdev->dev,
550 "disable dma channel irq%d\n",
Bryan Wubb90eb02007-12-04 23:45:18 -0800551 drv_data->dma_channel);
Barry Songa75bd652010-01-22 10:07:30 +0000552 dma_disable_irq_nosync(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700553
554 return IRQ_HANDLED;
555}
556
Mike Frysinger138f97c2009-04-06 19:00:50 -0700557static void bfin_spi_pump_transfers(unsigned long data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700558{
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400559 struct bfin_spi_master_data *drv_data = (struct bfin_spi_master_data *)data;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700560 struct spi_message *message = NULL;
561 struct spi_transfer *transfer = NULL;
562 struct spi_transfer *previous = NULL;
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400563 struct bfin_spi_slave_data *chip = NULL;
Mike Frysinger033f44b2009-12-18 17:38:04 +0000564 unsigned int bits_per_word;
Mike Frysinger5e8592d2009-12-18 18:00:10 +0000565 u16 cr, cr_width, dma_width, dma_config;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700566 u32 tranf_success = 1;
Vitja Makarov8eeb12e2008-05-01 04:35:03 -0700567 u8 full_duplex = 0;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700568
569 /* Get current state information */
570 message = drv_data->cur_msg;
571 transfer = drv_data->cur_transfer;
572 chip = drv_data->cur_chip;
Bryan Wu092e1fd2007-12-04 23:45:23 -0800573
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700574 /*
575 * if msg is error or done, report it back using complete() callback
576 */
577
578 /* Handle for abort */
579 if (message->state == ERROR_STATE) {
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700580 dev_dbg(&drv_data->pdev->dev, "transfer: we've hit an error\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700581 message->status = -EIO;
Mike Frysinger138f97c2009-04-06 19:00:50 -0700582 bfin_spi_giveback(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700583 return;
584 }
585
586 /* Handle end of message */
587 if (message->state == DONE_STATE) {
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700588 dev_dbg(&drv_data->pdev->dev, "transfer: all done!\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700589 message->status = 0;
Mike Frysinger138f97c2009-04-06 19:00:50 -0700590 bfin_spi_giveback(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700591 return;
592 }
593
594 /* Delay if requested at end of transfer */
595 if (message->state == RUNNING_STATE) {
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700596 dev_dbg(&drv_data->pdev->dev, "transfer: still running ...\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700597 previous = list_entry(transfer->transfer_list.prev,
598 struct spi_transfer, transfer_list);
599 if (previous->delay_usecs)
600 udelay(previous->delay_usecs);
601 }
602
Mike Frysingerab09e042009-09-23 23:32:34 +0000603 /* Flush any existing transfers that may be sitting in the hardware */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700604 if (bfin_spi_flush(drv_data) == 0) {
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700605 dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
606 message->status = -EIO;
Mike Frysinger138f97c2009-04-06 19:00:50 -0700607 bfin_spi_giveback(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700608 return;
609 }
610
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700611 if (transfer->len == 0) {
612 /* Move to next transfer of this msg */
613 message->state = bfin_spi_next_transfer(drv_data);
614 /* Schedule next transfer tasklet */
615 tasklet_schedule(&drv_data->pump_transfers);
Sonic Zhang1974eba2011-01-11 11:19:08 -0500616 return;
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700617 }
618
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700619 if (transfer->tx_buf != NULL) {
620 drv_data->tx = (void *)transfer->tx_buf;
621 drv_data->tx_end = drv_data->tx + transfer->len;
Bryan Wu88b40362007-05-21 18:32:16 +0800622 dev_dbg(&drv_data->pdev->dev, "tx_buf is %p, tx_end is %p\n",
623 transfer->tx_buf, drv_data->tx_end);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700624 } else {
625 drv_data->tx = NULL;
626 }
627
628 if (transfer->rx_buf != NULL) {
Vitja Makarov8eeb12e2008-05-01 04:35:03 -0700629 full_duplex = transfer->tx_buf != NULL;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700630 drv_data->rx = transfer->rx_buf;
631 drv_data->rx_end = drv_data->rx + transfer->len;
Bryan Wu88b40362007-05-21 18:32:16 +0800632 dev_dbg(&drv_data->pdev->dev, "rx_buf is %p, rx_end is %p\n",
633 transfer->rx_buf, drv_data->rx_end);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700634 } else {
635 drv_data->rx = NULL;
636 }
637
638 drv_data->rx_dma = transfer->rx_dma;
639 drv_data->tx_dma = transfer->tx_dma;
640 drv_data->len_in_bytes = transfer->len;
Bryan Wufad91c82007-12-04 23:45:14 -0800641 drv_data->cs_change = transfer->cs_change;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700642
Bryan Wu092e1fd2007-12-04 23:45:23 -0800643 /* Bits per word setup */
Mike Frysingere479c602011-06-17 04:35:37 -0400644 bits_per_word = transfer->bits_per_word ? :
645 message->spi->bits_per_word ? : 8;
646 if (bits_per_word % 16 == 0) {
Bob Liu4d676fc2011-01-11 11:19:07 -0500647 drv_data->n_bytes = bits_per_word/8;
Mike Frysinger5e8592d2009-12-18 18:00:10 +0000648 drv_data->len = (transfer->len) >> 1;
649 cr_width = BIT_CTL_WORDSIZE;
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400650 drv_data->ops = &bfin_bfin_spi_transfer_ops_u16;
Mike Frysingere479c602011-06-17 04:35:37 -0400651 } else if (bits_per_word % 8 == 0) {
Bob Liu4d676fc2011-01-11 11:19:07 -0500652 drv_data->n_bytes = bits_per_word/8;
653 drv_data->len = transfer->len;
654 cr_width = 0;
655 drv_data->ops = &bfin_bfin_spi_transfer_ops_u8;
Bob Liu2e768652010-09-17 03:46:22 +0000656 } else {
657 dev_err(&drv_data->pdev->dev, "transfer: unsupported bits_per_word\n");
658 message->status = -EINVAL;
659 bfin_spi_giveback(drv_data);
660 return;
Bryan Wu092e1fd2007-12-04 23:45:23 -0800661 }
Mike Frysinger47885ce2011-06-17 04:16:56 -0400662 cr = bfin_read(&drv_data->regs->ctl) & ~(BIT_CTL_TIMOD | BIT_CTL_WORDSIZE);
Mike Frysinger5e8592d2009-12-18 18:00:10 +0000663 cr |= cr_width;
Mike Frysinger47885ce2011-06-17 04:16:56 -0400664 bfin_write(&drv_data->regs->ctl, cr);
Bryan Wu092e1fd2007-12-04 23:45:23 -0800665
Mike Frysinger4fb98ef2008-04-08 17:41:57 -0700666 dev_dbg(&drv_data->pdev->dev,
Mike Frysinger9c4542c2009-09-24 01:04:04 +0000667 "transfer: drv_data->ops is %p, chip->ops is %p, u8_ops is %p\n",
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400668 drv_data->ops, chip->ops, &bfin_bfin_spi_transfer_ops_u8);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700669
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700670 message->state = RUNNING_STATE;
671 dma_config = 0;
672
Bryan Wu092e1fd2007-12-04 23:45:23 -0800673 /* Speed setup (surely valid because already checked) */
674 if (transfer->speed_hz)
Mike Frysinger47885ce2011-06-17 04:16:56 -0400675 bfin_write(&drv_data->regs->baud, hz_to_spi_baud(transfer->speed_hz));
Bryan Wu092e1fd2007-12-04 23:45:23 -0800676 else
Mike Frysinger47885ce2011-06-17 04:16:56 -0400677 bfin_write(&drv_data->regs->baud, chip->baud);
Bryan Wu092e1fd2007-12-04 23:45:23 -0800678
Mike Frysinger47885ce2011-06-17 04:16:56 -0400679 bfin_write(&drv_data->regs->stat, BIT_STAT_CLR);
Rob Marise72dcde2010-04-06 04:17:08 +0000680 bfin_spi_cs_active(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700681
Bryan Wu88b40362007-05-21 18:32:16 +0800682 dev_dbg(&drv_data->pdev->dev,
683 "now pumping a transfer: width is %d, len is %d\n",
Mike Frysinger5e8592d2009-12-18 18:00:10 +0000684 cr_width, transfer->len);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700685
686 /*
Vitja Makarov8cf58582009-04-06 19:00:31 -0700687 * Try to map dma buffer and do a dma transfer. If successful use,
688 * different way to r/w according to the enable_dma settings and if
689 * we are not doing a full duplex transfer (since the hardware does
690 * not support full duplex DMA transfers).
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700691 */
Vitja Makarov8eeb12e2008-05-01 04:35:03 -0700692 if (!full_duplex && drv_data->cur_chip->enable_dma
693 && drv_data->len > 6) {
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700694
Mike Frysinger11d6f592009-04-06 19:00:41 -0700695 unsigned long dma_start_addr, flags;
Mike Frysinger7aec3562009-04-06 19:00:36 -0700696
Bryan Wubb90eb02007-12-04 23:45:18 -0800697 disable_dma(drv_data->dma_channel);
698 clear_dma_irqstat(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700699
700 /* config dma channel */
Bryan Wu88b40362007-05-21 18:32:16 +0800701 dev_dbg(&drv_data->pdev->dev, "doing dma transfer\n");
Mike Frysinger7aec3562009-04-06 19:00:36 -0700702 set_dma_x_count(drv_data->dma_channel, drv_data->len);
Mike Frysinger5e8592d2009-12-18 18:00:10 +0000703 if (cr_width == BIT_CTL_WORDSIZE) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800704 set_dma_x_modify(drv_data->dma_channel, 2);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700705 dma_width = WDSIZE_16;
706 } else {
Bryan Wubb90eb02007-12-04 23:45:18 -0800707 set_dma_x_modify(drv_data->dma_channel, 1);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700708 dma_width = WDSIZE_8;
709 }
710
Sonic Zhang3f479a62007-12-04 23:45:18 -0800711 /* poll for SPI completion before start */
Mike Frysinger47885ce2011-06-17 04:16:56 -0400712 while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_SPIF))
Bryan Wud8c05002007-12-04 23:45:21 -0800713 cpu_relax();
Sonic Zhang3f479a62007-12-04 23:45:18 -0800714
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700715 /* dirty hack for autobuffer DMA mode */
716 if (drv_data->tx_dma == 0xFFFF) {
Bryan Wu88b40362007-05-21 18:32:16 +0800717 dev_dbg(&drv_data->pdev->dev,
718 "doing autobuffer DMA out.\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700719
720 /* no irq in autobuffer mode */
721 dma_config =
722 (DMAFLOW_AUTO | RESTART | dma_width | DI_EN);
Bryan Wubb90eb02007-12-04 23:45:18 -0800723 set_dma_config(drv_data->dma_channel, dma_config);
724 set_dma_start_addr(drv_data->dma_channel,
Bryan Wua32c6912007-12-04 23:45:15 -0800725 (unsigned long)drv_data->tx);
Bryan Wubb90eb02007-12-04 23:45:18 -0800726 enable_dma(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700727
Sonic Zhang07612e52007-12-04 23:45:21 -0800728 /* start SPI transfer */
Mike Frysinger47885ce2011-06-17 04:16:56 -0400729 bfin_write(&drv_data->regs->ctl, cr | BIT_CTL_TIMOD_DMA_TX);
Sonic Zhang07612e52007-12-04 23:45:21 -0800730
731 /* just return here, there can only be one transfer
732 * in this mode
733 */
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700734 message->status = 0;
Mike Frysinger138f97c2009-04-06 19:00:50 -0700735 bfin_spi_giveback(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700736 return;
737 }
738
739 /* In dma mode, rx or tx must be NULL in one transfer */
Mike Frysinger7aec3562009-04-06 19:00:36 -0700740 dma_config = (RESTART | dma_width | DI_EN);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700741 if (drv_data->rx != NULL) {
742 /* set transfer mode, and enable SPI */
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700743 dev_dbg(&drv_data->pdev->dev, "doing DMA in to %p (size %zx)\n",
744 drv_data->rx, drv_data->len_in_bytes);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700745
Vitja Makarov8cf58582009-04-06 19:00:31 -0700746 /* invalidate caches, if needed */
Jie Zhang67834fa2009-06-10 06:26:26 +0000747 if (bfin_addr_dcacheable((unsigned long) drv_data->rx))
Vitja Makarov8cf58582009-04-06 19:00:31 -0700748 invalidate_dcache_range((unsigned long) drv_data->rx,
749 (unsigned long) (drv_data->rx +
Mike Frysingerace32862009-04-06 19:00:34 -0700750 drv_data->len_in_bytes));
Vitja Makarov8cf58582009-04-06 19:00:31 -0700751
Mike Frysinger7aec3562009-04-06 19:00:36 -0700752 dma_config |= WNR;
753 dma_start_addr = (unsigned long)drv_data->rx;
Mike Frysingerb31e27a2009-04-06 19:00:39 -0700754 cr |= BIT_CTL_TIMOD_DMA_RX | BIT_CTL_SENDOPT;
Sonic Zhang07612e52007-12-04 23:45:21 -0800755
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700756 } else if (drv_data->tx != NULL) {
Bryan Wu88b40362007-05-21 18:32:16 +0800757 dev_dbg(&drv_data->pdev->dev, "doing DMA out.\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700758
Vitja Makarov8cf58582009-04-06 19:00:31 -0700759 /* flush caches, if needed */
Jie Zhang67834fa2009-06-10 06:26:26 +0000760 if (bfin_addr_dcacheable((unsigned long) drv_data->tx))
Vitja Makarov8cf58582009-04-06 19:00:31 -0700761 flush_dcache_range((unsigned long) drv_data->tx,
762 (unsigned long) (drv_data->tx +
Mike Frysingerace32862009-04-06 19:00:34 -0700763 drv_data->len_in_bytes));
Vitja Makarov8cf58582009-04-06 19:00:31 -0700764
Mike Frysinger7aec3562009-04-06 19:00:36 -0700765 dma_start_addr = (unsigned long)drv_data->tx;
Mike Frysingerb31e27a2009-04-06 19:00:39 -0700766 cr |= BIT_CTL_TIMOD_DMA_TX;
Sonic Zhang07612e52007-12-04 23:45:21 -0800767
Mike Frysinger7aec3562009-04-06 19:00:36 -0700768 } else
769 BUG();
770
Mike Frysinger11d6f592009-04-06 19:00:41 -0700771 /* oh man, here there be monsters ... and i dont mean the
772 * fluffy cute ones from pixar, i mean the kind that'll eat
773 * your data, kick your dog, and love it all. do *not* try
774 * and change these lines unless you (1) heavily test DMA
775 * with SPI flashes on a loaded system (e.g. ping floods),
776 * (2) know just how broken the DMA engine interaction with
777 * the SPI peripheral is, and (3) have someone else to blame
778 * when you screw it all up anyways.
779 */
Mike Frysinger7aec3562009-04-06 19:00:36 -0700780 set_dma_start_addr(drv_data->dma_channel, dma_start_addr);
Mike Frysinger11d6f592009-04-06 19:00:41 -0700781 set_dma_config(drv_data->dma_channel, dma_config);
782 local_irq_save(flags);
Mike Frysingera963ea82009-04-06 19:00:43 -0700783 SSYNC();
Mike Frysinger47885ce2011-06-17 04:16:56 -0400784 bfin_write(&drv_data->regs->ctl, cr);
Mike Frysingera963ea82009-04-06 19:00:43 -0700785 enable_dma(drv_data->dma_channel);
Mike Frysinger11d6f592009-04-06 19:00:41 -0700786 dma_enable_irq(drv_data->dma_channel);
787 local_irq_restore(flags);
Mike Frysinger7aec3562009-04-06 19:00:36 -0700788
Yi Lif6a6d962009-06-03 09:46:22 +0000789 return;
790 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700791
Mike Frysinger5e8592d2009-12-18 18:00:10 +0000792 /*
793 * We always use SPI_WRITE mode (transfer starts with TDBR write).
794 * SPI_READ mode (transfer starts with RDBR read) seems to have
795 * problems with setting up the output value in TDBR prior to the
796 * start of the transfer.
797 */
Mike Frysinger47885ce2011-06-17 04:16:56 -0400798 bfin_write(&drv_data->regs->ctl, cr | BIT_CTL_TXMOD);
Mike Frysinger5e8592d2009-12-18 18:00:10 +0000799
Yi Lif6a6d962009-06-03 09:46:22 +0000800 if (chip->pio_interrupt) {
Mike Frysinger5e8592d2009-12-18 18:00:10 +0000801 /* SPI irq should have been disabled by now */
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700802
Yi Lif6a6d962009-06-03 09:46:22 +0000803 /* discard old RX data and clear RXS */
804 bfin_spi_dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700805
Yi Lif6a6d962009-06-03 09:46:22 +0000806 /* start transfer */
807 if (drv_data->tx == NULL)
Mike Frysinger47885ce2011-06-17 04:16:56 -0400808 bfin_write(&drv_data->regs->tdbr, chip->idle_tx_val);
Yi Lif6a6d962009-06-03 09:46:22 +0000809 else {
Bob Liu4d676fc2011-01-11 11:19:07 -0500810 int loop;
811 if (bits_per_word % 16 == 0) {
812 u16 *buf = (u16 *)drv_data->tx;
813 for (loop = 0; loop < bits_per_word / 16;
814 loop++) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400815 bfin_write(&drv_data->regs->tdbr, *buf++);
Bob Liu4d676fc2011-01-11 11:19:07 -0500816 }
817 } else if (bits_per_word % 8 == 0) {
818 u8 *buf = (u8 *)drv_data->tx;
819 for (loop = 0; loop < bits_per_word / 8; loop++)
Mike Frysinger47885ce2011-06-17 04:16:56 -0400820 bfin_write(&drv_data->regs->tdbr, *buf++);
Bob Liu4d676fc2011-01-11 11:19:07 -0500821 }
822
Yi Lif6a6d962009-06-03 09:46:22 +0000823 drv_data->tx += drv_data->n_bytes;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700824 }
825
Yi Lif6a6d962009-06-03 09:46:22 +0000826 /* once TDBR is empty, interrupt is triggered */
827 enable_irq(drv_data->spi_irq);
828 return;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700829 }
Yi Lif6a6d962009-06-03 09:46:22 +0000830
831 /* IO mode */
832 dev_dbg(&drv_data->pdev->dev, "doing IO transfer\n");
833
Yi Lif6a6d962009-06-03 09:46:22 +0000834 if (full_duplex) {
835 /* full duplex mode */
836 BUG_ON((drv_data->tx_end - drv_data->tx) !=
837 (drv_data->rx_end - drv_data->rx));
838 dev_dbg(&drv_data->pdev->dev,
839 "IO duplex: cr is 0x%x\n", cr);
840
Mike Frysinger9c4542c2009-09-24 01:04:04 +0000841 drv_data->ops->duplex(drv_data);
Yi Lif6a6d962009-06-03 09:46:22 +0000842
843 if (drv_data->tx != drv_data->tx_end)
844 tranf_success = 0;
845 } else if (drv_data->tx != NULL) {
846 /* write only half duplex */
847 dev_dbg(&drv_data->pdev->dev,
848 "IO write: cr is 0x%x\n", cr);
849
Mike Frysinger9c4542c2009-09-24 01:04:04 +0000850 drv_data->ops->write(drv_data);
Yi Lif6a6d962009-06-03 09:46:22 +0000851
852 if (drv_data->tx != drv_data->tx_end)
853 tranf_success = 0;
854 } else if (drv_data->rx != NULL) {
855 /* read only half duplex */
856 dev_dbg(&drv_data->pdev->dev,
857 "IO read: cr is 0x%x\n", cr);
858
Mike Frysinger9c4542c2009-09-24 01:04:04 +0000859 drv_data->ops->read(drv_data);
Yi Lif6a6d962009-06-03 09:46:22 +0000860 if (drv_data->rx != drv_data->rx_end)
861 tranf_success = 0;
862 }
863
864 if (!tranf_success) {
865 dev_dbg(&drv_data->pdev->dev,
866 "IO write error!\n");
867 message->state = ERROR_STATE;
868 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300869 /* Update total byte transferred */
Yi Lif6a6d962009-06-03 09:46:22 +0000870 message->actual_length += drv_data->len_in_bytes;
871 /* Move to next transfer of this msg */
872 message->state = bfin_spi_next_transfer(drv_data);
873 if (drv_data->cs_change)
874 bfin_spi_cs_deactive(drv_data, chip);
875 }
876
877 /* Schedule next transfer tasklet */
878 tasklet_schedule(&drv_data->pump_transfers);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700879}
880
881/* pop a msg from queue and kick off real transfer */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700882static void bfin_spi_pump_messages(struct work_struct *work)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700883{
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400884 struct bfin_spi_master_data *drv_data;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700885 unsigned long flags;
886
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400887 drv_data = container_of(work, struct bfin_spi_master_data, pump_messages);
Bryan Wu131b17d2007-12-04 23:45:12 -0800888
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700889 /* Lock queue and check for queue work */
890 spin_lock_irqsave(&drv_data->lock, flags);
Mike Frysingerf4f50c32009-09-24 00:41:49 +0000891 if (list_empty(&drv_data->queue) || !drv_data->running) {
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700892 /* pumper kicked off but no work to do */
893 drv_data->busy = 0;
894 spin_unlock_irqrestore(&drv_data->lock, flags);
895 return;
896 }
897
898 /* Make sure we are not already running a message */
899 if (drv_data->cur_msg) {
900 spin_unlock_irqrestore(&drv_data->lock, flags);
901 return;
902 }
903
904 /* Extract head of queue */
905 drv_data->cur_msg = list_entry(drv_data->queue.next,
906 struct spi_message, queue);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800907
908 /* Setup the SSP using the per chip configuration */
909 drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
Mike Frysinger138f97c2009-04-06 19:00:50 -0700910 bfin_spi_restore_state(drv_data);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800911
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700912 list_del_init(&drv_data->cur_msg->queue);
913
914 /* Initial message state */
915 drv_data->cur_msg->state = START_STATE;
916 drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
917 struct spi_transfer, transfer_list);
918
Bryan Wu5fec5b52007-12-04 23:45:13 -0800919 dev_dbg(&drv_data->pdev->dev, "got a message to pump, "
920 "state is set to: baud %d, flag 0x%x, ctl 0x%x\n",
921 drv_data->cur_chip->baud, drv_data->cur_chip->flag,
922 drv_data->cur_chip->ctl_reg);
Bryan Wu131b17d2007-12-04 23:45:12 -0800923
924 dev_dbg(&drv_data->pdev->dev,
Bryan Wu88b40362007-05-21 18:32:16 +0800925 "the first transfer len is %d\n",
926 drv_data->cur_transfer->len);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700927
928 /* Mark as busy and launch transfers */
929 tasklet_schedule(&drv_data->pump_transfers);
930
931 drv_data->busy = 1;
932 spin_unlock_irqrestore(&drv_data->lock, flags);
933}
934
935/*
936 * got a msg to transfer, queue it in drv_data->queue.
937 * And kick off message pumper
938 */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700939static int bfin_spi_transfer(struct spi_device *spi, struct spi_message *msg)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700940{
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400941 struct bfin_spi_master_data *drv_data = spi_master_get_devdata(spi->master);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700942 unsigned long flags;
943
944 spin_lock_irqsave(&drv_data->lock, flags);
945
Mike Frysingerf4f50c32009-09-24 00:41:49 +0000946 if (!drv_data->running) {
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700947 spin_unlock_irqrestore(&drv_data->lock, flags);
948 return -ESHUTDOWN;
949 }
950
951 msg->actual_length = 0;
952 msg->status = -EINPROGRESS;
953 msg->state = START_STATE;
954
Bryan Wu88b40362007-05-21 18:32:16 +0800955 dev_dbg(&spi->dev, "adding an msg in transfer() \n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700956 list_add_tail(&msg->queue, &drv_data->queue);
957
Mike Frysingerf4f50c32009-09-24 00:41:49 +0000958 if (drv_data->running && !drv_data->busy)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700959 queue_work(drv_data->workqueue, &drv_data->pump_messages);
960
961 spin_unlock_irqrestore(&drv_data->lock, flags);
962
963 return 0;
964}
965
Sonic Zhang12e17c42007-12-04 23:45:16 -0800966#define MAX_SPI_SSEL 7
967
Mike Frysingerddc0bf12011-06-17 04:16:57 -0400968static const u16 ssel[][MAX_SPI_SSEL] = {
Sonic Zhang12e17c42007-12-04 23:45:16 -0800969 {P_SPI0_SSEL1, P_SPI0_SSEL2, P_SPI0_SSEL3,
970 P_SPI0_SSEL4, P_SPI0_SSEL5,
971 P_SPI0_SSEL6, P_SPI0_SSEL7},
972
973 {P_SPI1_SSEL1, P_SPI1_SSEL2, P_SPI1_SSEL3,
974 P_SPI1_SSEL4, P_SPI1_SSEL5,
975 P_SPI1_SSEL6, P_SPI1_SSEL7},
976
977 {P_SPI2_SSEL1, P_SPI2_SSEL2, P_SPI2_SSEL3,
978 P_SPI2_SSEL4, P_SPI2_SSEL5,
979 P_SPI2_SSEL6, P_SPI2_SSEL7},
980};
981
Mike Frysingerab09e042009-09-23 23:32:34 +0000982/* setup for devices (may be called multiple times -- not just first setup) */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700983static int bfin_spi_setup(struct spi_device *spi)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700984{
Daniel Mackac01e972009-03-25 00:18:35 +0000985 struct bfin5xx_spi_chip *chip_info;
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400986 struct bfin_spi_slave_data *chip = NULL;
987 struct bfin_spi_master_data *drv_data = spi_master_get_devdata(spi->master);
Mike Frysinger5b47bcd2009-12-18 17:43:31 +0000988 u16 bfin_ctl_reg;
Daniel Mackac01e972009-03-25 00:18:35 +0000989 int ret = -EINVAL;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700990
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700991 /* Only alloc (or use chip_info) on first setup */
Daniel Mackac01e972009-03-25 00:18:35 +0000992 chip_info = NULL;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700993 chip = spi_get_ctldata(spi);
994 if (chip == NULL) {
Daniel Mackac01e972009-03-25 00:18:35 +0000995 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
996 if (!chip) {
997 dev_err(&spi->dev, "cannot allocate chip data\n");
998 ret = -ENOMEM;
999 goto error;
1000 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001001
1002 chip->enable_dma = 0;
1003 chip_info = spi->controller_data;
1004 }
1005
Mike Frysinger5b47bcd2009-12-18 17:43:31 +00001006 /* Let people set non-standard bits directly */
1007 bfin_ctl_reg = BIT_CTL_OPENDRAIN | BIT_CTL_EMISO |
1008 BIT_CTL_PSSE | BIT_CTL_GM | BIT_CTL_SZ;
1009
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001010 /* chip_info isn't always needed */
1011 if (chip_info) {
Mike Frysinger2ed35512007-12-04 23:45:14 -08001012 /* Make sure people stop trying to set fields via ctl_reg
1013 * when they should actually be using common SPI framework.
Mike Frysinger90008a62009-10-15 04:13:29 +00001014 * Currently we let through: WOM EMISO PSSE GM SZ.
Mike Frysinger2ed35512007-12-04 23:45:14 -08001015 * Not sure if a user actually needs/uses any of these,
1016 * but let's assume (for now) they do.
1017 */
Mike Frysinger5b47bcd2009-12-18 17:43:31 +00001018 if (chip_info->ctl_reg & ~bfin_ctl_reg) {
Mike Frysinger2ed35512007-12-04 23:45:14 -08001019 dev_err(&spi->dev, "do not set bits in ctl_reg "
1020 "that the SPI framework manages\n");
Daniel Mackac01e972009-03-25 00:18:35 +00001021 goto error;
Mike Frysinger2ed35512007-12-04 23:45:14 -08001022 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001023 chip->enable_dma = chip_info->enable_dma != 0
1024 && drv_data->master_info->enable_dma;
1025 chip->ctl_reg = chip_info->ctl_reg;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001026 chip->cs_chg_udelay = chip_info->cs_chg_udelay;
Wolfgang Muees93b61bd2009-04-06 19:00:53 -07001027 chip->idle_tx_val = chip_info->idle_tx_val;
Yi Lif6a6d962009-06-03 09:46:22 +00001028 chip->pio_interrupt = chip_info->pio_interrupt;
Mike Frysinger033f44b2009-12-18 17:38:04 +00001029 spi->bits_per_word = chip_info->bits_per_word;
Mike Frysinger5b47bcd2009-12-18 17:43:31 +00001030 } else {
1031 /* force a default base state */
1032 chip->ctl_reg &= bfin_ctl_reg;
Mike Frysinger033f44b2009-12-18 17:38:04 +00001033 }
1034
Bob Liu4d676fc2011-01-11 11:19:07 -05001035 if (spi->bits_per_word % 8) {
Mike Frysinger033f44b2009-12-18 17:38:04 +00001036 dev_err(&spi->dev, "%d bits_per_word is not supported\n",
1037 spi->bits_per_word);
1038 goto error;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001039 }
1040
1041 /* translate common spi framework into our register */
Mike Frysinger7715aad2010-02-25 10:00:55 +00001042 if (spi->mode & ~(SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST)) {
1043 dev_err(&spi->dev, "unsupported spi modes detected\n");
1044 goto error;
1045 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001046 if (spi->mode & SPI_CPOL)
Mike Frysinger90008a62009-10-15 04:13:29 +00001047 chip->ctl_reg |= BIT_CTL_CPOL;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001048 if (spi->mode & SPI_CPHA)
Mike Frysinger90008a62009-10-15 04:13:29 +00001049 chip->ctl_reg |= BIT_CTL_CPHA;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001050 if (spi->mode & SPI_LSB_FIRST)
Mike Frysinger90008a62009-10-15 04:13:29 +00001051 chip->ctl_reg |= BIT_CTL_LSBF;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001052 /* we dont support running in slave mode (yet?) */
Mike Frysinger90008a62009-10-15 04:13:29 +00001053 chip->ctl_reg |= BIT_CTL_MASTER;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001054
1055 /*
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001056 * Notice: for blackfin, the speed_hz is the value of register
1057 * SPI_BAUD, not the real baudrate
1058 */
1059 chip->baud = hz_to_spi_baud(spi->max_speed_hz);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001060 chip->chip_select_num = spi->chip_select;
Barry Song4190f6a2010-04-06 03:36:24 +00001061 if (chip->chip_select_num < MAX_CTRL_CS) {
1062 if (!(spi->mode & SPI_CPHA))
1063 dev_warn(&spi->dev, "Warning: SPI CPHA not set:"
1064 " Slave Select not under software control!\n"
1065 " See Documentation/blackfin/bfin-spi-notes.txt");
1066
Barry Songd3cc71f2009-11-17 09:45:59 +00001067 chip->flag = (1 << spi->chip_select) << 8;
Barry Song4190f6a2010-04-06 03:36:24 +00001068 } else
Barry Songd3cc71f2009-11-17 09:45:59 +00001069 chip->cs_gpio = chip->chip_select_num - MAX_CTRL_CS;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001070
Yi Lif6a6d962009-06-03 09:46:22 +00001071 if (chip->enable_dma && chip->pio_interrupt) {
1072 dev_err(&spi->dev, "enable_dma is set, "
1073 "do not set pio_interrupt\n");
1074 goto error;
1075 }
Daniel Mackac01e972009-03-25 00:18:35 +00001076 /*
1077 * if any one SPI chip is registered and wants DMA, request the
1078 * DMA channel for it
1079 */
1080 if (chip->enable_dma && !drv_data->dma_requested) {
1081 /* register dma irq handler */
1082 ret = request_dma(drv_data->dma_channel, "BFIN_SPI_DMA");
1083 if (ret) {
1084 dev_err(&spi->dev,
1085 "Unable to request BlackFin SPI DMA channel\n");
1086 goto error;
1087 }
1088 drv_data->dma_requested = 1;
1089
1090 ret = set_dma_callback(drv_data->dma_channel,
1091 bfin_spi_dma_irq_handler, drv_data);
1092 if (ret) {
1093 dev_err(&spi->dev, "Unable to set dma callback\n");
1094 goto error;
1095 }
1096 dma_disable_irq(drv_data->dma_channel);
1097 }
1098
Yi Lif6a6d962009-06-03 09:46:22 +00001099 if (chip->pio_interrupt && !drv_data->irq_requested) {
1100 ret = request_irq(drv_data->spi_irq, bfin_spi_pio_irq_handler,
1101 IRQF_DISABLED, "BFIN_SPI", drv_data);
1102 if (ret) {
1103 dev_err(&spi->dev, "Unable to register spi IRQ\n");
1104 goto error;
1105 }
1106 drv_data->irq_requested = 1;
1107 /* we use write mode, spi irq has to be disabled here */
1108 disable_irq(drv_data->spi_irq);
1109 }
1110
Barry Songd3cc71f2009-11-17 09:45:59 +00001111 if (chip->chip_select_num >= MAX_CTRL_CS) {
Michael Hennerich73e1ac12010-10-22 02:01:47 -04001112 /* Only request on first setup */
1113 if (spi_get_ctldata(spi) == NULL) {
1114 ret = gpio_request(chip->cs_gpio, spi->modalias);
1115 if (ret) {
1116 dev_err(&spi->dev, "gpio_request() error\n");
1117 goto pin_error;
1118 }
1119 gpio_direction_output(chip->cs_gpio, 1);
Daniel Mackac01e972009-03-25 00:18:35 +00001120 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001121 }
1122
Joe Perches898eb712007-10-18 03:06:30 -07001123 dev_dbg(&spi->dev, "setup spi chip %s, width is %d, dma is %d\n",
Mike Frysinger033f44b2009-12-18 17:38:04 +00001124 spi->modalias, spi->bits_per_word, chip->enable_dma);
Bryan Wu88b40362007-05-21 18:32:16 +08001125 dev_dbg(&spi->dev, "ctl_reg is 0x%x, flag_reg is 0x%x\n",
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001126 chip->ctl_reg, chip->flag);
1127
1128 spi_set_ctldata(spi, chip);
1129
Sonic Zhang12e17c42007-12-04 23:45:16 -08001130 dev_dbg(&spi->dev, "chip select number is %d\n", chip->chip_select_num);
Barry Songd3cc71f2009-11-17 09:45:59 +00001131 if (chip->chip_select_num < MAX_CTRL_CS) {
Daniel Mackac01e972009-03-25 00:18:35 +00001132 ret = peripheral_request(ssel[spi->master->bus_num]
1133 [chip->chip_select_num-1], spi->modalias);
1134 if (ret) {
1135 dev_err(&spi->dev, "peripheral_request() error\n");
1136 goto pin_error;
1137 }
1138 }
Sonic Zhang12e17c42007-12-04 23:45:16 -08001139
Barry Song82216102009-06-17 10:10:53 +00001140 bfin_spi_cs_enable(drv_data, chip);
Mike Frysinger138f97c2009-04-06 19:00:50 -07001141 bfin_spi_cs_deactive(drv_data, chip);
Sonic Zhang07612e52007-12-04 23:45:21 -08001142
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001143 return 0;
Daniel Mackac01e972009-03-25 00:18:35 +00001144
1145 pin_error:
Barry Songd3cc71f2009-11-17 09:45:59 +00001146 if (chip->chip_select_num >= MAX_CTRL_CS)
Daniel Mackac01e972009-03-25 00:18:35 +00001147 gpio_free(chip->cs_gpio);
1148 else
1149 peripheral_free(ssel[spi->master->bus_num]
1150 [chip->chip_select_num - 1]);
1151 error:
1152 if (chip) {
1153 if (drv_data->dma_requested)
1154 free_dma(drv_data->dma_channel);
1155 drv_data->dma_requested = 0;
1156
1157 kfree(chip);
1158 /* prevent free 'chip' twice */
1159 spi_set_ctldata(spi, NULL);
1160 }
1161
1162 return ret;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001163}
1164
1165/*
1166 * callback for spi framework.
1167 * clean driver specific data
1168 */
Mike Frysinger138f97c2009-04-06 19:00:50 -07001169static void bfin_spi_cleanup(struct spi_device *spi)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001170{
Mike Frysinger9c0a7882010-10-18 02:45:22 -04001171 struct bfin_spi_slave_data *chip = spi_get_ctldata(spi);
1172 struct bfin_spi_master_data *drv_data = spi_master_get_devdata(spi->master);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001173
Mike Frysingere7d02e32009-04-06 19:00:51 -07001174 if (!chip)
1175 return;
1176
Barry Songd3cc71f2009-11-17 09:45:59 +00001177 if (chip->chip_select_num < MAX_CTRL_CS) {
Sonic Zhang12e17c42007-12-04 23:45:16 -08001178 peripheral_free(ssel[spi->master->bus_num]
1179 [chip->chip_select_num-1]);
Barry Song82216102009-06-17 10:10:53 +00001180 bfin_spi_cs_disable(drv_data, chip);
Barry Songd3cc71f2009-11-17 09:45:59 +00001181 } else
Michael Hennerich42c78b22009-04-06 19:00:51 -07001182 gpio_free(chip->cs_gpio);
1183
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001184 kfree(chip);
Daniel Mackac01e972009-03-25 00:18:35 +00001185 /* prevent free 'chip' twice */
1186 spi_set_ctldata(spi, NULL);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001187}
1188
Mike Frysingerc52d4e52011-06-17 04:16:58 -04001189static int bfin_spi_init_queue(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001190{
1191 INIT_LIST_HEAD(&drv_data->queue);
1192 spin_lock_init(&drv_data->lock);
1193
Mike Frysingerf4f50c32009-09-24 00:41:49 +00001194 drv_data->running = false;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001195 drv_data->busy = 0;
1196
1197 /* init transfer tasklet */
1198 tasklet_init(&drv_data->pump_transfers,
Mike Frysinger138f97c2009-04-06 19:00:50 -07001199 bfin_spi_pump_transfers, (unsigned long)drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001200
1201 /* init messages workqueue */
Mike Frysinger138f97c2009-04-06 19:00:50 -07001202 INIT_WORK(&drv_data->pump_messages, bfin_spi_pump_messages);
Kay Sievers6c7377a2009-03-24 16:38:21 -07001203 drv_data->workqueue = create_singlethread_workqueue(
1204 dev_name(drv_data->master->dev.parent));
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001205 if (drv_data->workqueue == NULL)
1206 return -EBUSY;
1207
1208 return 0;
1209}
1210
Mike Frysingerc52d4e52011-06-17 04:16:58 -04001211static int bfin_spi_start_queue(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001212{
1213 unsigned long flags;
1214
1215 spin_lock_irqsave(&drv_data->lock, flags);
1216
Mike Frysingerf4f50c32009-09-24 00:41:49 +00001217 if (drv_data->running || drv_data->busy) {
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001218 spin_unlock_irqrestore(&drv_data->lock, flags);
1219 return -EBUSY;
1220 }
1221
Mike Frysingerf4f50c32009-09-24 00:41:49 +00001222 drv_data->running = true;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001223 drv_data->cur_msg = NULL;
1224 drv_data->cur_transfer = NULL;
1225 drv_data->cur_chip = NULL;
1226 spin_unlock_irqrestore(&drv_data->lock, flags);
1227
1228 queue_work(drv_data->workqueue, &drv_data->pump_messages);
1229
1230 return 0;
1231}
1232
Mike Frysingerc52d4e52011-06-17 04:16:58 -04001233static int bfin_spi_stop_queue(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001234{
1235 unsigned long flags;
1236 unsigned limit = 500;
1237 int status = 0;
1238
1239 spin_lock_irqsave(&drv_data->lock, flags);
1240
1241 /*
1242 * This is a bit lame, but is optimized for the common execution path.
1243 * A wait_queue on the drv_data->busy could be used, but then the common
1244 * execution path (pump_messages) would be required to call wake_up or
1245 * friends on every SPI message. Do this instead
1246 */
Mike Frysingerf4f50c32009-09-24 00:41:49 +00001247 drv_data->running = false;
Vasily Khoruzhick850a28e2011-04-06 17:49:15 +03001248 while ((!list_empty(&drv_data->queue) || drv_data->busy) && limit--) {
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001249 spin_unlock_irqrestore(&drv_data->lock, flags);
1250 msleep(10);
1251 spin_lock_irqsave(&drv_data->lock, flags);
1252 }
1253
1254 if (!list_empty(&drv_data->queue) || drv_data->busy)
1255 status = -EBUSY;
1256
1257 spin_unlock_irqrestore(&drv_data->lock, flags);
1258
1259 return status;
1260}
1261
Mike Frysingerc52d4e52011-06-17 04:16:58 -04001262static int bfin_spi_destroy_queue(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001263{
1264 int status;
1265
Mike Frysinger138f97c2009-04-06 19:00:50 -07001266 status = bfin_spi_stop_queue(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001267 if (status != 0)
1268 return status;
1269
1270 destroy_workqueue(drv_data->workqueue);
1271
1272 return 0;
1273}
1274
Mike Frysinger138f97c2009-04-06 19:00:50 -07001275static int __init bfin_spi_probe(struct platform_device *pdev)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001276{
1277 struct device *dev = &pdev->dev;
1278 struct bfin5xx_spi_master *platform_info;
1279 struct spi_master *master;
Mike Frysinger9c0a7882010-10-18 02:45:22 -04001280 struct bfin_spi_master_data *drv_data;
Bryan Wua32c6912007-12-04 23:45:15 -08001281 struct resource *res;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001282 int status = 0;
1283
1284 platform_info = dev->platform_data;
1285
1286 /* Allocate master with space for drv_data */
Mike Frysinger2a045132009-09-24 01:28:54 +00001287 master = spi_alloc_master(dev, sizeof(*drv_data));
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001288 if (!master) {
1289 dev_err(&pdev->dev, "can not alloc spi_master\n");
1290 return -ENOMEM;
1291 }
Bryan Wu131b17d2007-12-04 23:45:12 -08001292
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001293 drv_data = spi_master_get_devdata(master);
1294 drv_data->master = master;
1295 drv_data->master_info = platform_info;
1296 drv_data->pdev = pdev;
Bryan Wu003d9222007-12-04 23:45:22 -08001297 drv_data->pin_req = platform_info->pin_req;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001298
David Brownelle7db06b2009-06-17 16:26:04 -07001299 /* the spi->mode bits supported by this driver: */
1300 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST;
1301
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001302 master->bus_num = pdev->id;
1303 master->num_chipselect = platform_info->num_chipselect;
Mike Frysinger138f97c2009-04-06 19:00:50 -07001304 master->cleanup = bfin_spi_cleanup;
1305 master->setup = bfin_spi_setup;
1306 master->transfer = bfin_spi_transfer;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001307
Bryan Wua32c6912007-12-04 23:45:15 -08001308 /* Find and map our resources */
1309 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1310 if (res == NULL) {
1311 dev_err(dev, "Cannot get IORESOURCE_MEM\n");
1312 status = -ENOENT;
1313 goto out_error_get_res;
1314 }
1315
Mike Frysinger47885ce2011-06-17 04:16:56 -04001316 drv_data->regs = ioremap(res->start, resource_size(res));
1317 if (drv_data->regs == NULL) {
Bryan Wua32c6912007-12-04 23:45:15 -08001318 dev_err(dev, "Cannot map IO\n");
1319 status = -ENXIO;
1320 goto out_error_ioremap;
1321 }
1322
Yi Lif6a6d962009-06-03 09:46:22 +00001323 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1324 if (res == NULL) {
Bryan Wua32c6912007-12-04 23:45:15 -08001325 dev_err(dev, "No DMA channel specified\n");
1326 status = -ENOENT;
Yi Lif6a6d962009-06-03 09:46:22 +00001327 goto out_error_free_io;
1328 }
1329 drv_data->dma_channel = res->start;
1330
1331 drv_data->spi_irq = platform_get_irq(pdev, 0);
1332 if (drv_data->spi_irq < 0) {
1333 dev_err(dev, "No spi pio irq specified\n");
1334 status = -ENOENT;
1335 goto out_error_free_io;
Bryan Wua32c6912007-12-04 23:45:15 -08001336 }
1337
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001338 /* Initial and start queue */
Mike Frysinger138f97c2009-04-06 19:00:50 -07001339 status = bfin_spi_init_queue(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001340 if (status != 0) {
Bryan Wua32c6912007-12-04 23:45:15 -08001341 dev_err(dev, "problem initializing queue\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001342 goto out_error_queue_alloc;
1343 }
Bryan Wua32c6912007-12-04 23:45:15 -08001344
Mike Frysinger138f97c2009-04-06 19:00:50 -07001345 status = bfin_spi_start_queue(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001346 if (status != 0) {
Bryan Wua32c6912007-12-04 23:45:15 -08001347 dev_err(dev, "problem starting queue\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001348 goto out_error_queue_alloc;
1349 }
1350
Vitja Makarovf9e522c2008-04-08 17:41:57 -07001351 status = peripheral_request_list(drv_data->pin_req, DRV_NAME);
1352 if (status != 0) {
1353 dev_err(&pdev->dev, ": Requesting Peripherals failed\n");
1354 goto out_error_queue_alloc;
1355 }
1356
Wolfgang Mueesbb8beec2009-05-22 01:11:02 +00001357 /* Reset SPI registers. If these registers were used by the boot loader,
1358 * the sky may fall on your head if you enable the dma controller.
1359 */
Mike Frysinger47885ce2011-06-17 04:16:56 -04001360 bfin_write(&drv_data->regs->ctl, BIT_CTL_CPHA | BIT_CTL_MASTER);
1361 bfin_write(&drv_data->regs->flg, 0xFF00);
Wolfgang Mueesbb8beec2009-05-22 01:11:02 +00001362
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001363 /* Register with the SPI framework */
1364 platform_set_drvdata(pdev, drv_data);
1365 status = spi_register_master(master);
1366 if (status != 0) {
Bryan Wua32c6912007-12-04 23:45:15 -08001367 dev_err(dev, "problem registering spi master\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001368 goto out_error_queue_alloc;
1369 }
Bryan Wua32c6912007-12-04 23:45:15 -08001370
Mike Frysinger47885ce2011-06-17 04:16:56 -04001371 dev_info(dev, "%s, Version %s, regs@%p, dma channel@%d\n",
1372 DRV_DESC, DRV_VERSION, drv_data->regs,
Bryan Wubb90eb02007-12-04 23:45:18 -08001373 drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001374 return status;
1375
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001376out_error_queue_alloc:
Mike Frysinger138f97c2009-04-06 19:00:50 -07001377 bfin_spi_destroy_queue(drv_data);
Yi Lif6a6d962009-06-03 09:46:22 +00001378out_error_free_io:
Mike Frysinger47885ce2011-06-17 04:16:56 -04001379 iounmap(drv_data->regs);
Bryan Wua32c6912007-12-04 23:45:15 -08001380out_error_ioremap:
1381out_error_get_res:
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001382 spi_master_put(master);
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001383
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001384 return status;
1385}
1386
1387/* stop hardware and remove the driver */
Mike Frysinger138f97c2009-04-06 19:00:50 -07001388static int __devexit bfin_spi_remove(struct platform_device *pdev)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001389{
Mike Frysinger9c0a7882010-10-18 02:45:22 -04001390 struct bfin_spi_master_data *drv_data = platform_get_drvdata(pdev);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001391 int status = 0;
1392
1393 if (!drv_data)
1394 return 0;
1395
1396 /* Remove the queue */
Mike Frysinger138f97c2009-04-06 19:00:50 -07001397 status = bfin_spi_destroy_queue(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001398 if (status != 0)
1399 return status;
1400
1401 /* Disable the SSP at the peripheral and SOC level */
1402 bfin_spi_disable(drv_data);
1403
1404 /* Release DMA */
1405 if (drv_data->master_info->enable_dma) {
Bryan Wubb90eb02007-12-04 23:45:18 -08001406 if (dma_channel_active(drv_data->dma_channel))
1407 free_dma(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001408 }
1409
Yi Lif6a6d962009-06-03 09:46:22 +00001410 if (drv_data->irq_requested) {
1411 free_irq(drv_data->spi_irq, drv_data);
1412 drv_data->irq_requested = 0;
1413 }
1414
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001415 /* Disconnect from the SPI framework */
1416 spi_unregister_master(drv_data->master);
1417
Bryan Wu003d9222007-12-04 23:45:22 -08001418 peripheral_free_list(drv_data->pin_req);
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001419
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001420 /* Prevent double remove */
1421 platform_set_drvdata(pdev, NULL);
1422
1423 return 0;
1424}
1425
1426#ifdef CONFIG_PM
Mike Frysinger138f97c2009-04-06 19:00:50 -07001427static int bfin_spi_suspend(struct platform_device *pdev, pm_message_t state)
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
Mike Frysinger138f97c2009-04-06 19:00:50 -07001432 status = bfin_spi_stop_queue(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001433 if (status != 0)
1434 return status;
1435
Mike Frysinger47885ce2011-06-17 04:16:56 -04001436 drv_data->ctrl_reg = bfin_read(&drv_data->regs->ctl);
1437 drv_data->flag_reg = bfin_read(&drv_data->regs->flg);
Barry Songb052fd02009-11-18 09:43:21 +00001438
1439 /*
1440 * reset SPI_CTL and SPI_FLG registers
1441 */
Mike Frysinger47885ce2011-06-17 04:16:56 -04001442 bfin_write(&drv_data->regs->ctl, BIT_CTL_CPHA | BIT_CTL_MASTER);
1443 bfin_write(&drv_data->regs->flg, 0xFF00);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001444
1445 return 0;
1446}
1447
Mike Frysinger138f97c2009-04-06 19:00:50 -07001448static int bfin_spi_resume(struct platform_device *pdev)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001449{
Mike Frysinger9c0a7882010-10-18 02:45:22 -04001450 struct bfin_spi_master_data *drv_data = platform_get_drvdata(pdev);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001451 int status = 0;
1452
Mike Frysinger47885ce2011-06-17 04:16:56 -04001453 bfin_write(&drv_data->regs->ctl, drv_data->ctrl_reg);
1454 bfin_write(&drv_data->regs->flg, drv_data->flag_reg);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001455
1456 /* Start the queue running */
Mike Frysinger138f97c2009-04-06 19:00:50 -07001457 status = bfin_spi_start_queue(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001458 if (status != 0) {
1459 dev_err(&pdev->dev, "problem starting queue (%d)\n", status);
1460 return status;
1461 }
1462
1463 return 0;
1464}
1465#else
Mike Frysinger138f97c2009-04-06 19:00:50 -07001466#define bfin_spi_suspend NULL
1467#define bfin_spi_resume NULL
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001468#endif /* CONFIG_PM */
1469
Kay Sievers7e38c3c2008-04-10 21:29:20 -07001470MODULE_ALIAS("platform:bfin-spi");
Mike Frysinger138f97c2009-04-06 19:00:50 -07001471static struct platform_driver bfin_spi_driver = {
David Brownellfc3ba952007-08-30 23:56:24 -07001472 .driver = {
Bryan Wua32c6912007-12-04 23:45:15 -08001473 .name = DRV_NAME,
Bryan Wu88b40362007-05-21 18:32:16 +08001474 .owner = THIS_MODULE,
1475 },
Mike Frysinger138f97c2009-04-06 19:00:50 -07001476 .suspend = bfin_spi_suspend,
1477 .resume = bfin_spi_resume,
1478 .remove = __devexit_p(bfin_spi_remove),
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001479};
1480
Mike Frysinger138f97c2009-04-06 19:00:50 -07001481static int __init bfin_spi_init(void)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001482{
Mike Frysinger138f97c2009-04-06 19:00:50 -07001483 return platform_driver_probe(&bfin_spi_driver, bfin_spi_probe);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001484}
Michael Hennerich6f7c17f2010-07-01 14:34:10 +00001485subsys_initcall(bfin_spi_init);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001486
Mike Frysinger138f97c2009-04-06 19:00:50 -07001487static void __exit bfin_spi_exit(void)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001488{
Mike Frysinger138f97c2009-04-06 19:00:50 -07001489 platform_driver_unregister(&bfin_spi_driver);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001490}
Mike Frysinger138f97c2009-04-06 19:00:50 -07001491module_exit(bfin_spi_exit);