blob: 249c7a3677c90d2076a27ac6d25c04f220e41829 [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>
Steven Miaoe8304d02014-04-12 09:23:24 +080015#include <linux/gpio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Bryan Wu131b17d2007-12-04 23:45:12 -080017#include <linux/io.h>
Wu, Bryana5f6abd2007-05-06 14:50:34 -070018#include <linux/ioport.h>
Bryan Wu131b17d2007-12-04 23:45:12 -080019#include <linux/irq.h>
Wu, Bryana5f6abd2007-05-06 14:50:34 -070020#include <linux/errno.h>
21#include <linux/interrupt.h>
22#include <linux/platform_device.h>
23#include <linux/dma-mapping.h>
24#include <linux/spi/spi.h>
25#include <linux/workqueue.h>
Wu, Bryana5f6abd2007-05-06 14:50:34 -070026
Wu, Bryana5f6abd2007-05-06 14:50:34 -070027#include <asm/dma.h>
Bryan Wu131b17d2007-12-04 23:45:12 -080028#include <asm/portmux.h>
Wu, Bryana5f6abd2007-05-06 14:50:34 -070029#include <asm/bfin5xx_spi.h>
Vitja Makarov8cf58582009-04-06 19:00:31 -070030#include <asm/cacheflush.h>
31
Bryan Wua32c6912007-12-04 23:45:15 -080032#define DRV_NAME "bfin-spi"
33#define DRV_AUTHOR "Bryan Wu, Luke Yang"
Mike Frysinger138f97c2009-04-06 19:00:50 -070034#define DRV_DESC "Blackfin on-chip SPI Controller Driver"
Bryan Wua32c6912007-12-04 23:45:15 -080035#define DRV_VERSION "1.0"
36
37MODULE_AUTHOR(DRV_AUTHOR);
38MODULE_DESCRIPTION(DRV_DESC);
Wu, Bryana5f6abd2007-05-06 14:50:34 -070039MODULE_LICENSE("GPL");
40
Bryan Wubb90eb02007-12-04 23:45:18 -080041#define START_STATE ((void *)0)
42#define RUNNING_STATE ((void *)1)
43#define DONE_STATE ((void *)2)
44#define ERROR_STATE ((void *)-1)
Wu, Bryana5f6abd2007-05-06 14:50:34 -070045
Mike Frysinger9c0a7882010-10-18 02:45:22 -040046struct bfin_spi_master_data;
Mike Frysinger9c4542c2009-09-24 01:04:04 +000047
Mike Frysinger9c0a7882010-10-18 02:45:22 -040048struct bfin_spi_transfer_ops {
49 void (*write) (struct bfin_spi_master_data *);
50 void (*read) (struct bfin_spi_master_data *);
51 void (*duplex) (struct bfin_spi_master_data *);
Mike Frysinger9c4542c2009-09-24 01:04:04 +000052};
53
Mike Frysinger9c0a7882010-10-18 02:45:22 -040054struct bfin_spi_master_data {
Wu, Bryana5f6abd2007-05-06 14:50:34 -070055 /* Driver model hookup */
56 struct platform_device *pdev;
57
58 /* SPI framework hookup */
59 struct spi_master *master;
60
Bryan Wubb90eb02007-12-04 23:45:18 -080061 /* Regs base of SPI controller */
Mike Frysinger47885ce2011-06-17 04:16:56 -040062 struct bfin_spi_regs __iomem *regs;
Bryan Wubb90eb02007-12-04 23:45:18 -080063
Bryan Wu003d9222007-12-04 23:45:22 -080064 /* Pin request list */
65 u16 *pin_req;
66
Wu, Bryana5f6abd2007-05-06 14:50:34 -070067 /* BFIN hookup */
68 struct bfin5xx_spi_master *master_info;
69
Wu, Bryana5f6abd2007-05-06 14:50:34 -070070 struct work_struct pump_messages;
71 spinlock_t lock;
72 struct list_head queue;
73 int busy;
Mike Frysingerf4f50c32009-09-24 00:41:49 +000074 bool running;
Wu, Bryana5f6abd2007-05-06 14:50:34 -070075
76 /* Message Transfer pump */
77 struct tasklet_struct pump_transfers;
78
79 /* Current message transfer state info */
80 struct spi_message *cur_msg;
81 struct spi_transfer *cur_transfer;
Mike Frysinger9c0a7882010-10-18 02:45:22 -040082 struct bfin_spi_slave_data *cur_chip;
Wu, Bryana5f6abd2007-05-06 14:50:34 -070083 size_t len_in_bytes;
84 size_t len;
85 void *tx;
86 void *tx_end;
87 void *rx;
88 void *rx_end;
Bryan Wubb90eb02007-12-04 23:45:18 -080089
90 /* DMA stuffs */
91 int dma_channel;
Wu, Bryana5f6abd2007-05-06 14:50:34 -070092 int dma_mapped;
Bryan Wubb90eb02007-12-04 23:45:18 -080093 int dma_requested;
Wu, Bryana5f6abd2007-05-06 14:50:34 -070094 dma_addr_t rx_dma;
95 dma_addr_t tx_dma;
Bryan Wubb90eb02007-12-04 23:45:18 -080096
Yi Lif6a6d962009-06-03 09:46:22 +000097 int irq_requested;
98 int spi_irq;
99
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700100 size_t rx_map_len;
101 size_t tx_map_len;
102 u8 n_bytes;
Barry Songb052fd02009-11-18 09:43:21 +0000103 u16 ctrl_reg;
104 u16 flag_reg;
105
Bryan Wufad91c82007-12-04 23:45:14 -0800106 int cs_change;
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400107 const struct bfin_spi_transfer_ops *ops;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700108};
109
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400110struct bfin_spi_slave_data {
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700111 u16 ctl_reg;
112 u16 baud;
113 u16 flag;
114
115 u8 chip_select_num;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700116 u8 enable_dma;
Bryan Wu62310e52007-12-04 23:45:20 -0800117 u16 cs_chg_udelay; /* Some devices require > 255usec delay */
Michael Hennerich42c78b22009-04-06 19:00:51 -0700118 u32 cs_gpio;
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700119 u16 idle_tx_val;
Yi Lif6a6d962009-06-03 09:46:22 +0000120 u8 pio_interrupt; /* use spi data irq */
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400121 const struct bfin_spi_transfer_ops *ops;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700122};
123
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400124static void bfin_spi_enable(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700125{
Mike Frysinger47885ce2011-06-17 04:16:56 -0400126 bfin_write_or(&drv_data->regs->ctl, BIT_CTL_ENABLE);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700127}
128
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400129static void bfin_spi_disable(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700130{
Mike Frysinger47885ce2011-06-17 04:16:56 -0400131 bfin_write_and(&drv_data->regs->ctl, ~BIT_CTL_ENABLE);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700132}
133
134/* Caculate the SPI_BAUD register value based on input HZ */
135static u16 hz_to_spi_baud(u32 speed_hz)
136{
137 u_long sclk = get_sclk();
138 u16 spi_baud = (sclk / (2 * speed_hz));
139
140 if ((sclk % (2 * speed_hz)) > 0)
141 spi_baud++;
142
Michael Hennerich7513e002009-04-06 19:00:32 -0700143 if (spi_baud < MIN_SPI_BAUD_VAL)
144 spi_baud = MIN_SPI_BAUD_VAL;
145
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700146 return spi_baud;
147}
148
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400149static int bfin_spi_flush(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700150{
151 unsigned long limit = loops_per_jiffy << 1;
152
153 /* wait for stop and clear stat */
Mike Frysinger47885ce2011-06-17 04:16:56 -0400154 while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_SPIF) && --limit)
Bryan Wud8c05002007-12-04 23:45:21 -0800155 cpu_relax();
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700156
Mike Frysinger47885ce2011-06-17 04:16:56 -0400157 bfin_write(&drv_data->regs->stat, BIT_STAT_CLR);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700158
159 return limit;
160}
161
Bryan Wufad91c82007-12-04 23:45:14 -0800162/* Chip select operation functions for cs_change flag */
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400163static 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 -0800164{
Mike Frysinger47885ce2011-06-17 04:16:56 -0400165 if (likely(chip->chip_select_num < MAX_CTRL_CS))
166 bfin_write_and(&drv_data->regs->flg, ~chip->flag);
167 else
Michael Hennerich42c78b22009-04-06 19:00:51 -0700168 gpio_set_value(chip->cs_gpio, 0);
Bryan Wufad91c82007-12-04 23:45:14 -0800169}
170
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400171static void bfin_spi_cs_deactive(struct bfin_spi_master_data *drv_data,
172 struct bfin_spi_slave_data *chip)
Bryan Wufad91c82007-12-04 23:45:14 -0800173{
Mike Frysinger47885ce2011-06-17 04:16:56 -0400174 if (likely(chip->chip_select_num < MAX_CTRL_CS))
175 bfin_write_or(&drv_data->regs->flg, chip->flag);
176 else
Michael Hennerich42c78b22009-04-06 19:00:51 -0700177 gpio_set_value(chip->cs_gpio, 1);
Bryan Wu62310e52007-12-04 23:45:20 -0800178
179 /* Move delay here for consistency */
180 if (chip->cs_chg_udelay)
181 udelay(chip->cs_chg_udelay);
Bryan Wufad91c82007-12-04 23:45:14 -0800182}
183
Barry Song82216102009-06-17 10:10:53 +0000184/* enable or disable the pin muxed by GPIO and SPI CS to work as SPI CS */
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400185static inline void bfin_spi_cs_enable(struct bfin_spi_master_data *drv_data,
186 struct bfin_spi_slave_data *chip)
Barry Song82216102009-06-17 10:10:53 +0000187{
Mike Frysinger47885ce2011-06-17 04:16:56 -0400188 if (chip->chip_select_num < MAX_CTRL_CS)
189 bfin_write_or(&drv_data->regs->flg, chip->flag >> 8);
Barry Song82216102009-06-17 10:10:53 +0000190}
191
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400192static inline void bfin_spi_cs_disable(struct bfin_spi_master_data *drv_data,
193 struct bfin_spi_slave_data *chip)
Barry Song82216102009-06-17 10:10:53 +0000194{
Mike Frysinger47885ce2011-06-17 04:16:56 -0400195 if (chip->chip_select_num < MAX_CTRL_CS)
196 bfin_write_and(&drv_data->regs->flg, ~(chip->flag >> 8));
Barry Song82216102009-06-17 10:10:53 +0000197}
198
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700199/* stop controller and re-config current chip*/
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400200static void bfin_spi_restore_state(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700201{
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400202 struct bfin_spi_slave_data *chip = drv_data->cur_chip;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700203
204 /* Clear status and disable clock */
Mike Frysinger47885ce2011-06-17 04:16:56 -0400205 bfin_write(&drv_data->regs->stat, BIT_STAT_CLR);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700206 bfin_spi_disable(drv_data);
Bryan Wu88b40362007-05-21 18:32:16 +0800207 dev_dbg(&drv_data->pdev->dev, "restoring spi ctl state\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700208
Barry Song9677b0de2009-11-30 03:49:41 +0000209 SSYNC();
210
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700211 /* Load the registers */
Mike Frysinger47885ce2011-06-17 04:16:56 -0400212 bfin_write(&drv_data->regs->ctl, chip->ctl_reg);
213 bfin_write(&drv_data->regs->baud, chip->baud);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800214
215 bfin_spi_enable(drv_data);
Mike Frysinger138f97c2009-04-06 19:00:50 -0700216 bfin_spi_cs_active(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700217}
218
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700219/* used to kick off transfer in rx mode and read unwanted RX data */
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400220static inline void bfin_spi_dummy_read(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700221{
Mike Frysinger47885ce2011-06-17 04:16:56 -0400222 (void) bfin_read(&drv_data->regs->rdbr);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700223}
224
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400225static void bfin_spi_u8_writer(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700226{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700227 /* clear RXS (we check for RXS inside the loop) */
228 bfin_spi_dummy_read(drv_data);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800229
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700230 while (drv_data->tx < drv_data->tx_end) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400231 bfin_write(&drv_data->regs->tdbr, (*(u8 *) (drv_data->tx++)));
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700232 /* wait until transfer finished.
233 checking SPIF or TXS may not guarantee transfer completion */
Mike Frysinger47885ce2011-06-17 04:16:56 -0400234 while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800235 cpu_relax();
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700236 /* discard RX data and clear RXS */
237 bfin_spi_dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700238 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700239}
240
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400241static void bfin_spi_u8_reader(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700242{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700243 u16 tx_val = drv_data->cur_chip->idle_tx_val;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700244
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700245 /* discard old RX data and clear RXS */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700246 bfin_spi_dummy_read(drv_data);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800247
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700248 while (drv_data->rx < drv_data->rx_end) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400249 bfin_write(&drv_data->regs->tdbr, tx_val);
250 while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800251 cpu_relax();
Mike Frysinger47885ce2011-06-17 04:16:56 -0400252 *(u8 *) (drv_data->rx++) = bfin_read(&drv_data->regs->rdbr);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700253 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700254}
255
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400256static void bfin_spi_u8_duplex(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700257{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700258 /* discard old RX data and clear RXS */
259 bfin_spi_dummy_read(drv_data);
260
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700261 while (drv_data->rx < drv_data->rx_end) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400262 bfin_write(&drv_data->regs->tdbr, (*(u8 *) (drv_data->tx++)));
263 while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800264 cpu_relax();
Mike Frysinger47885ce2011-06-17 04:16:56 -0400265 *(u8 *) (drv_data->rx++) = bfin_read(&drv_data->regs->rdbr);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700266 }
267}
268
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400269static const struct bfin_spi_transfer_ops bfin_bfin_spi_transfer_ops_u8 = {
Mike Frysinger9c4542c2009-09-24 01:04:04 +0000270 .write = bfin_spi_u8_writer,
271 .read = bfin_spi_u8_reader,
272 .duplex = bfin_spi_u8_duplex,
273};
274
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400275static void bfin_spi_u16_writer(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700276{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700277 /* clear RXS (we check for RXS inside the loop) */
278 bfin_spi_dummy_read(drv_data);
Bryan Wu88b40362007-05-21 18:32:16 +0800279
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700280 while (drv_data->tx < drv_data->tx_end) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400281 bfin_write(&drv_data->regs->tdbr, (*(u16 *) (drv_data->tx)));
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700282 drv_data->tx += 2;
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700283 /* wait until transfer finished.
284 checking SPIF or TXS may not guarantee transfer completion */
Mike Frysinger47885ce2011-06-17 04:16:56 -0400285 while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_RXS))
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700286 cpu_relax();
287 /* discard RX data and clear RXS */
288 bfin_spi_dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700289 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700290}
291
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400292static void bfin_spi_u16_reader(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700293{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700294 u16 tx_val = drv_data->cur_chip->idle_tx_val;
Sonic Zhangcc487e72007-12-04 23:45:17 -0800295
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700296 /* discard old RX data and clear RXS */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700297 bfin_spi_dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700298
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700299 while (drv_data->rx < drv_data->rx_end) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400300 bfin_write(&drv_data->regs->tdbr, tx_val);
301 while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800302 cpu_relax();
Mike Frysinger47885ce2011-06-17 04:16:56 -0400303 *(u16 *) (drv_data->rx) = bfin_read(&drv_data->regs->rdbr);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700304 drv_data->rx += 2;
305 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700306}
307
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400308static void bfin_spi_u16_duplex(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700309{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700310 /* discard old RX data and clear RXS */
311 bfin_spi_dummy_read(drv_data);
312
313 while (drv_data->rx < drv_data->rx_end) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400314 bfin_write(&drv_data->regs->tdbr, (*(u16 *) (drv_data->tx)));
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700315 drv_data->tx += 2;
Mike Frysinger47885ce2011-06-17 04:16:56 -0400316 while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800317 cpu_relax();
Mike Frysinger47885ce2011-06-17 04:16:56 -0400318 *(u16 *) (drv_data->rx) = bfin_read(&drv_data->regs->rdbr);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700319 drv_data->rx += 2;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700320 }
321}
322
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400323static const struct bfin_spi_transfer_ops bfin_bfin_spi_transfer_ops_u16 = {
Mike Frysinger9c4542c2009-09-24 01:04:04 +0000324 .write = bfin_spi_u16_writer,
325 .read = bfin_spi_u16_reader,
326 .duplex = bfin_spi_u16_duplex,
327};
328
Rob Marise3595402010-04-06 04:12:00 +0000329/* test if there is more transfer to be done */
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400330static void *bfin_spi_next_transfer(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700331{
332 struct spi_message *msg = drv_data->cur_msg;
333 struct spi_transfer *trans = drv_data->cur_transfer;
334
335 /* Move to next transfer */
336 if (trans->transfer_list.next != &msg->transfers) {
337 drv_data->cur_transfer =
338 list_entry(trans->transfer_list.next,
339 struct spi_transfer, transfer_list);
340 return RUNNING_STATE;
341 } else
342 return DONE_STATE;
343}
344
345/*
346 * caller already set message->status;
347 * dma and pio irqs are blocked give finished message back
348 */
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400349static void bfin_spi_giveback(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700350{
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400351 struct bfin_spi_slave_data *chip = drv_data->cur_chip;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700352 unsigned long flags;
353 struct spi_message *msg;
354
355 spin_lock_irqsave(&drv_data->lock, flags);
356 msg = drv_data->cur_msg;
357 drv_data->cur_msg = NULL;
358 drv_data->cur_transfer = NULL;
359 drv_data->cur_chip = NULL;
Bhaktipriya Shridhar9b96f072016-07-02 14:12:00 +0530360 schedule_work(&drv_data->pump_messages);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700361 spin_unlock_irqrestore(&drv_data->lock, flags);
362
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700363 msg->state = NULL;
364
Bryan Wufad91c82007-12-04 23:45:14 -0800365 if (!drv_data->cs_change)
Mike Frysinger138f97c2009-04-06 19:00:50 -0700366 bfin_spi_cs_deactive(drv_data, chip);
Bryan Wufad91c82007-12-04 23:45:14 -0800367
Yi Lib9b2a762009-04-06 19:00:49 -0700368 /* Not stop spi in autobuffer mode */
369 if (drv_data->tx_dma != 0xFFFF)
370 bfin_spi_disable(drv_data);
371
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700372 if (msg->complete)
373 msg->complete(msg->context);
374}
375
Yi Lif6a6d962009-06-03 09:46:22 +0000376/* spi data irq handler */
377static irqreturn_t bfin_spi_pio_irq_handler(int irq, void *dev_id)
378{
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400379 struct bfin_spi_master_data *drv_data = dev_id;
380 struct bfin_spi_slave_data *chip = drv_data->cur_chip;
Yi Lif6a6d962009-06-03 09:46:22 +0000381 struct spi_message *msg = drv_data->cur_msg;
382 int n_bytes = drv_data->n_bytes;
Bob Liu4d676fc2011-01-11 11:19:07 -0500383 int loop = 0;
Yi Lif6a6d962009-06-03 09:46:22 +0000384
385 /* wait until transfer finished. */
Mike Frysinger47885ce2011-06-17 04:16:56 -0400386 while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_RXS))
Yi Lif6a6d962009-06-03 09:46:22 +0000387 cpu_relax();
388
389 if ((drv_data->tx && drv_data->tx >= drv_data->tx_end) ||
390 (drv_data->rx && drv_data->rx >= (drv_data->rx_end - n_bytes))) {
391 /* last read */
392 if (drv_data->rx) {
393 dev_dbg(&drv_data->pdev->dev, "last read\n");
Scott Jiang128465c2012-04-23 18:18:12 -0400394 if (!(n_bytes % 2)) {
Bob Liu4d676fc2011-01-11 11:19:07 -0500395 u16 *buf = (u16 *)drv_data->rx;
396 for (loop = 0; loop < n_bytes / 2; loop++)
Mike Frysinger47885ce2011-06-17 04:16:56 -0400397 *buf++ = bfin_read(&drv_data->regs->rdbr);
Bob Liu4d676fc2011-01-11 11:19:07 -0500398 } else {
399 u8 *buf = (u8 *)drv_data->rx;
400 for (loop = 0; loop < n_bytes; loop++)
Mike Frysinger47885ce2011-06-17 04:16:56 -0400401 *buf++ = bfin_read(&drv_data->regs->rdbr);
Bob Liu4d676fc2011-01-11 11:19:07 -0500402 }
Yi Lif6a6d962009-06-03 09:46:22 +0000403 drv_data->rx += n_bytes;
404 }
405
406 msg->actual_length += drv_data->len_in_bytes;
407 if (drv_data->cs_change)
408 bfin_spi_cs_deactive(drv_data, chip);
409 /* Move to next transfer */
410 msg->state = bfin_spi_next_transfer(drv_data);
411
Yi Li7370ed62009-12-07 08:07:01 +0000412 disable_irq_nosync(drv_data->spi_irq);
Yi Lif6a6d962009-06-03 09:46:22 +0000413
414 /* Schedule transfer tasklet */
415 tasklet_schedule(&drv_data->pump_transfers);
416 return IRQ_HANDLED;
417 }
418
419 if (drv_data->rx && drv_data->tx) {
420 /* duplex */
421 dev_dbg(&drv_data->pdev->dev, "duplex: write_TDBR\n");
Scott Jiang128465c2012-04-23 18:18:12 -0400422 if (!(n_bytes % 2)) {
Bob Liu4d676fc2011-01-11 11:19:07 -0500423 u16 *buf = (u16 *)drv_data->rx;
424 u16 *buf2 = (u16 *)drv_data->tx;
425 for (loop = 0; loop < n_bytes / 2; loop++) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400426 *buf++ = bfin_read(&drv_data->regs->rdbr);
427 bfin_write(&drv_data->regs->tdbr, *buf2++);
Bob Liu4d676fc2011-01-11 11:19:07 -0500428 }
429 } else {
430 u8 *buf = (u8 *)drv_data->rx;
431 u8 *buf2 = (u8 *)drv_data->tx;
432 for (loop = 0; loop < n_bytes; loop++) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400433 *buf++ = bfin_read(&drv_data->regs->rdbr);
434 bfin_write(&drv_data->regs->tdbr, *buf2++);
Bob Liu4d676fc2011-01-11 11:19:07 -0500435 }
Yi Lif6a6d962009-06-03 09:46:22 +0000436 }
437 } else if (drv_data->rx) {
438 /* read */
439 dev_dbg(&drv_data->pdev->dev, "read: write_TDBR\n");
Scott Jiang128465c2012-04-23 18:18:12 -0400440 if (!(n_bytes % 2)) {
Bob Liu4d676fc2011-01-11 11:19:07 -0500441 u16 *buf = (u16 *)drv_data->rx;
442 for (loop = 0; loop < n_bytes / 2; loop++) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400443 *buf++ = bfin_read(&drv_data->regs->rdbr);
444 bfin_write(&drv_data->regs->tdbr, chip->idle_tx_val);
Bob Liu4d676fc2011-01-11 11:19:07 -0500445 }
446 } else {
447 u8 *buf = (u8 *)drv_data->rx;
448 for (loop = 0; loop < n_bytes; loop++) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400449 *buf++ = bfin_read(&drv_data->regs->rdbr);
450 bfin_write(&drv_data->regs->tdbr, chip->idle_tx_val);
Bob Liu4d676fc2011-01-11 11:19:07 -0500451 }
452 }
Yi Lif6a6d962009-06-03 09:46:22 +0000453 } else if (drv_data->tx) {
454 /* write */
455 dev_dbg(&drv_data->pdev->dev, "write: write_TDBR\n");
Scott Jiang128465c2012-04-23 18:18:12 -0400456 if (!(n_bytes % 2)) {
Bob Liu4d676fc2011-01-11 11:19:07 -0500457 u16 *buf = (u16 *)drv_data->tx;
458 for (loop = 0; loop < n_bytes / 2; loop++) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400459 bfin_read(&drv_data->regs->rdbr);
460 bfin_write(&drv_data->regs->tdbr, *buf++);
Bob Liu4d676fc2011-01-11 11:19:07 -0500461 }
462 } else {
463 u8 *buf = (u8 *)drv_data->tx;
464 for (loop = 0; loop < n_bytes; loop++) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400465 bfin_read(&drv_data->regs->rdbr);
466 bfin_write(&drv_data->regs->tdbr, *buf++);
Bob Liu4d676fc2011-01-11 11:19:07 -0500467 }
468 }
Yi Lif6a6d962009-06-03 09:46:22 +0000469 }
470
471 if (drv_data->tx)
472 drv_data->tx += n_bytes;
473 if (drv_data->rx)
474 drv_data->rx += n_bytes;
475
476 return IRQ_HANDLED;
477}
478
Mike Frysinger138f97c2009-04-06 19:00:50 -0700479static irqreturn_t bfin_spi_dma_irq_handler(int irq, void *dev_id)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700480{
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400481 struct bfin_spi_master_data *drv_data = dev_id;
482 struct bfin_spi_slave_data *chip = drv_data->cur_chip;
Bryan Wubb90eb02007-12-04 23:45:18 -0800483 struct spi_message *msg = drv_data->cur_msg;
Mike Frysingeraaaf9392009-04-06 19:00:42 -0700484 unsigned long timeout;
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700485 unsigned short dmastat = get_dma_curr_irqstat(drv_data->dma_channel);
Mike Frysinger47885ce2011-06-17 04:16:56 -0400486 u16 spistat = bfin_read(&drv_data->regs->stat);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700487
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700488 dev_dbg(&drv_data->pdev->dev,
489 "in dma_irq_handler dmastat:0x%x spistat:0x%x\n",
490 dmastat, spistat);
491
Michael Hennerich782a8952010-10-22 02:01:48 -0400492 if (drv_data->rx != NULL) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400493 u16 cr = bfin_read(&drv_data->regs->ctl);
Michael Hennerich782a8952010-10-22 02:01:48 -0400494 /* discard old RX data and clear RXS */
495 bfin_spi_dummy_read(drv_data);
Mike Frysinger47885ce2011-06-17 04:16:56 -0400496 bfin_write(&drv_data->regs->ctl, cr & ~BIT_CTL_ENABLE); /* Disable SPI */
497 bfin_write(&drv_data->regs->ctl, cr & ~BIT_CTL_TIMOD); /* Restore State */
498 bfin_write(&drv_data->regs->stat, BIT_STAT_CLR); /* Clear Status */
Michael Hennerich782a8952010-10-22 02:01:48 -0400499 }
500
Bryan Wubb90eb02007-12-04 23:45:18 -0800501 clear_dma_irqstat(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700502
503 /*
Bryan Wud6fe89b2007-06-11 17:34:17 +0800504 * wait for the last transaction shifted out. HRM states:
505 * at this point there may still be data in the SPI DMA FIFO waiting
506 * to be transmitted ... software needs to poll TXS in the SPI_STAT
507 * register until it goes low for 2 successive reads
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700508 */
509 if (drv_data->tx != NULL) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400510 while ((bfin_read(&drv_data->regs->stat) & BIT_STAT_TXS) ||
511 (bfin_read(&drv_data->regs->stat) & BIT_STAT_TXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800512 cpu_relax();
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700513 }
514
Mike Frysingeraaaf9392009-04-06 19:00:42 -0700515 dev_dbg(&drv_data->pdev->dev,
516 "in dma_irq_handler dmastat:0x%x spistat:0x%x\n",
Mike Frysinger47885ce2011-06-17 04:16:56 -0400517 dmastat, bfin_read(&drv_data->regs->stat));
Mike Frysingeraaaf9392009-04-06 19:00:42 -0700518
519 timeout = jiffies + HZ;
Mike Frysinger47885ce2011-06-17 04:16:56 -0400520 while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_SPIF))
Mike Frysingeraaaf9392009-04-06 19:00:42 -0700521 if (!time_before(jiffies, timeout)) {
Jarkko Nikulaa1829d22013-10-11 13:53:59 +0300522 dev_warn(&drv_data->pdev->dev, "timeout waiting for SPIF\n");
Mike Frysingeraaaf9392009-04-06 19:00:42 -0700523 break;
524 } else
525 cpu_relax();
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700526
Mike Frysinger90008a62009-10-15 04:13:29 +0000527 if ((dmastat & DMA_ERR) && (spistat & BIT_STAT_RBSY)) {
Mike Frysinger04b95d22009-04-06 19:00:35 -0700528 msg->state = ERROR_STATE;
529 dev_err(&drv_data->pdev->dev, "dma receive: fifo/buffer overflow\n");
530 } else {
531 msg->actual_length += drv_data->len_in_bytes;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700532
Mike Frysinger04b95d22009-04-06 19:00:35 -0700533 if (drv_data->cs_change)
Mike Frysinger138f97c2009-04-06 19:00:50 -0700534 bfin_spi_cs_deactive(drv_data, chip);
Bryan Wufad91c82007-12-04 23:45:14 -0800535
Mike Frysinger04b95d22009-04-06 19:00:35 -0700536 /* Move to next transfer */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700537 msg->state = bfin_spi_next_transfer(drv_data);
Mike Frysinger04b95d22009-04-06 19:00:35 -0700538 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700539
540 /* Schedule transfer tasklet */
541 tasklet_schedule(&drv_data->pump_transfers);
542
543 /* free the irq handler before next transfer */
Bryan Wu88b40362007-05-21 18:32:16 +0800544 dev_dbg(&drv_data->pdev->dev,
545 "disable dma channel irq%d\n",
Bryan Wubb90eb02007-12-04 23:45:18 -0800546 drv_data->dma_channel);
Barry Songa75bd65b2010-01-22 10:07:30 +0000547 dma_disable_irq_nosync(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700548
549 return IRQ_HANDLED;
550}
551
Mike Frysinger138f97c2009-04-06 19:00:50 -0700552static void bfin_spi_pump_transfers(unsigned long data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700553{
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400554 struct bfin_spi_master_data *drv_data = (struct bfin_spi_master_data *)data;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700555 struct spi_message *message = NULL;
556 struct spi_transfer *transfer = NULL;
557 struct spi_transfer *previous = NULL;
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400558 struct bfin_spi_slave_data *chip = NULL;
Mike Frysinger033f44b2009-12-18 17:38:04 +0000559 unsigned int bits_per_word;
Chen Gang057f6062015-04-02 03:03:33 +0800560 u16 cr, cr_width = 0, dma_width, dma_config;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700561 u32 tranf_success = 1;
Vitja Makarov8eeb12e2008-05-01 04:35:03 -0700562 u8 full_duplex = 0;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700563
564 /* Get current state information */
565 message = drv_data->cur_msg;
566 transfer = drv_data->cur_transfer;
567 chip = drv_data->cur_chip;
Bryan Wu092e1fd2007-12-04 23:45:23 -0800568
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700569 /*
570 * if msg is error or done, report it back using complete() callback
571 */
572
573 /* Handle for abort */
574 if (message->state == ERROR_STATE) {
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700575 dev_dbg(&drv_data->pdev->dev, "transfer: we've hit an error\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700576 message->status = -EIO;
Mike Frysinger138f97c2009-04-06 19:00:50 -0700577 bfin_spi_giveback(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700578 return;
579 }
580
581 /* Handle end of message */
582 if (message->state == DONE_STATE) {
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700583 dev_dbg(&drv_data->pdev->dev, "transfer: all done!\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700584 message->status = 0;
Scott Jiang2431a812012-04-23 18:18:13 -0400585 bfin_spi_flush(drv_data);
Mike Frysinger138f97c2009-04-06 19:00:50 -0700586 bfin_spi_giveback(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700587 return;
588 }
589
590 /* Delay if requested at end of transfer */
591 if (message->state == RUNNING_STATE) {
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700592 dev_dbg(&drv_data->pdev->dev, "transfer: still running ...\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700593 previous = list_entry(transfer->transfer_list.prev,
594 struct spi_transfer, transfer_list);
595 if (previous->delay_usecs)
596 udelay(previous->delay_usecs);
597 }
598
Mike Frysingerab09e042009-09-23 23:32:34 +0000599 /* Flush any existing transfers that may be sitting in the hardware */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700600 if (bfin_spi_flush(drv_data) == 0) {
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700601 dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
602 message->status = -EIO;
Mike Frysinger138f97c2009-04-06 19:00:50 -0700603 bfin_spi_giveback(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700604 return;
605 }
606
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700607 if (transfer->len == 0) {
608 /* Move to next transfer of this msg */
609 message->state = bfin_spi_next_transfer(drv_data);
610 /* Schedule next transfer tasklet */
611 tasklet_schedule(&drv_data->pump_transfers);
Sonic Zhang1974eba2011-01-11 11:19:08 -0500612 return;
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700613 }
614
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700615 if (transfer->tx_buf != NULL) {
616 drv_data->tx = (void *)transfer->tx_buf;
617 drv_data->tx_end = drv_data->tx + transfer->len;
Bryan Wu88b40362007-05-21 18:32:16 +0800618 dev_dbg(&drv_data->pdev->dev, "tx_buf is %p, tx_end is %p\n",
619 transfer->tx_buf, drv_data->tx_end);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700620 } else {
621 drv_data->tx = NULL;
622 }
623
624 if (transfer->rx_buf != NULL) {
Vitja Makarov8eeb12e2008-05-01 04:35:03 -0700625 full_duplex = transfer->tx_buf != NULL;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700626 drv_data->rx = transfer->rx_buf;
627 drv_data->rx_end = drv_data->rx + transfer->len;
Bryan Wu88b40362007-05-21 18:32:16 +0800628 dev_dbg(&drv_data->pdev->dev, "rx_buf is %p, rx_end is %p\n",
629 transfer->rx_buf, drv_data->rx_end);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700630 } else {
631 drv_data->rx = NULL;
632 }
633
634 drv_data->rx_dma = transfer->rx_dma;
635 drv_data->tx_dma = transfer->tx_dma;
636 drv_data->len_in_bytes = transfer->len;
Bryan Wufad91c82007-12-04 23:45:14 -0800637 drv_data->cs_change = transfer->cs_change;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700638
Bryan Wu092e1fd2007-12-04 23:45:23 -0800639 /* Bits per word setup */
Laxman Dewangan766ed702012-12-18 14:25:43 +0530640 bits_per_word = transfer->bits_per_word;
Stephen Warren24778be2013-05-21 20:36:35 -0600641 if (bits_per_word == 16) {
Bob Liu4d676fc2011-01-11 11:19:07 -0500642 drv_data->n_bytes = bits_per_word/8;
Mike Frysinger5e8592d2009-12-18 18:00:10 +0000643 drv_data->len = (transfer->len) >> 1;
644 cr_width = BIT_CTL_WORDSIZE;
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400645 drv_data->ops = &bfin_bfin_spi_transfer_ops_u16;
Stephen Warren24778be2013-05-21 20:36:35 -0600646 } else if (bits_per_word == 8) {
Bob Liu4d676fc2011-01-11 11:19:07 -0500647 drv_data->n_bytes = bits_per_word/8;
648 drv_data->len = transfer->len;
Bob Liu4d676fc2011-01-11 11:19:07 -0500649 drv_data->ops = &bfin_bfin_spi_transfer_ops_u8;
Bryan Wu092e1fd2007-12-04 23:45:23 -0800650 }
Mike Frysinger47885ce2011-06-17 04:16:56 -0400651 cr = bfin_read(&drv_data->regs->ctl) & ~(BIT_CTL_TIMOD | BIT_CTL_WORDSIZE);
Mike Frysinger5e8592d2009-12-18 18:00:10 +0000652 cr |= cr_width;
Mike Frysinger47885ce2011-06-17 04:16:56 -0400653 bfin_write(&drv_data->regs->ctl, cr);
Bryan Wu092e1fd2007-12-04 23:45:23 -0800654
Mike Frysinger4fb98ef2008-04-08 17:41:57 -0700655 dev_dbg(&drv_data->pdev->dev,
Mike Frysinger9c4542c2009-09-24 01:04:04 +0000656 "transfer: drv_data->ops is %p, chip->ops is %p, u8_ops is %p\n",
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400657 drv_data->ops, chip->ops, &bfin_bfin_spi_transfer_ops_u8);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700658
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700659 message->state = RUNNING_STATE;
660 dma_config = 0;
661
Jarkko Nikula95a8fde2015-09-15 16:26:17 +0300662 bfin_write(&drv_data->regs->baud, hz_to_spi_baud(transfer->speed_hz));
Bryan Wu092e1fd2007-12-04 23:45:23 -0800663
Mike Frysinger47885ce2011-06-17 04:16:56 -0400664 bfin_write(&drv_data->regs->stat, BIT_STAT_CLR);
Rob Marise72dcde2010-04-06 04:17:08 +0000665 bfin_spi_cs_active(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700666
Bryan Wu88b40362007-05-21 18:32:16 +0800667 dev_dbg(&drv_data->pdev->dev,
668 "now pumping a transfer: width is %d, len is %d\n",
Mike Frysinger5e8592d2009-12-18 18:00:10 +0000669 cr_width, transfer->len);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700670
671 /*
Vitja Makarov8cf58582009-04-06 19:00:31 -0700672 * Try to map dma buffer and do a dma transfer. If successful use,
673 * different way to r/w according to the enable_dma settings and if
674 * we are not doing a full duplex transfer (since the hardware does
675 * not support full duplex DMA transfers).
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700676 */
Vitja Makarov8eeb12e2008-05-01 04:35:03 -0700677 if (!full_duplex && drv_data->cur_chip->enable_dma
678 && drv_data->len > 6) {
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700679
Mike Frysinger11d6f592009-04-06 19:00:41 -0700680 unsigned long dma_start_addr, flags;
Mike Frysinger7aec3562009-04-06 19:00:36 -0700681
Bryan Wubb90eb02007-12-04 23:45:18 -0800682 disable_dma(drv_data->dma_channel);
683 clear_dma_irqstat(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700684
685 /* config dma channel */
Bryan Wu88b40362007-05-21 18:32:16 +0800686 dev_dbg(&drv_data->pdev->dev, "doing dma transfer\n");
Mike Frysinger7aec3562009-04-06 19:00:36 -0700687 set_dma_x_count(drv_data->dma_channel, drv_data->len);
Mike Frysinger5e8592d2009-12-18 18:00:10 +0000688 if (cr_width == BIT_CTL_WORDSIZE) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800689 set_dma_x_modify(drv_data->dma_channel, 2);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700690 dma_width = WDSIZE_16;
691 } else {
Bryan Wubb90eb02007-12-04 23:45:18 -0800692 set_dma_x_modify(drv_data->dma_channel, 1);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700693 dma_width = WDSIZE_8;
694 }
695
Sonic Zhang3f479a62007-12-04 23:45:18 -0800696 /* poll for SPI completion before start */
Mike Frysinger47885ce2011-06-17 04:16:56 -0400697 while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_SPIF))
Bryan Wud8c05002007-12-04 23:45:21 -0800698 cpu_relax();
Sonic Zhang3f479a62007-12-04 23:45:18 -0800699
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700700 /* dirty hack for autobuffer DMA mode */
701 if (drv_data->tx_dma == 0xFFFF) {
Bryan Wu88b40362007-05-21 18:32:16 +0800702 dev_dbg(&drv_data->pdev->dev,
703 "doing autobuffer DMA out.\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700704
705 /* no irq in autobuffer mode */
706 dma_config =
707 (DMAFLOW_AUTO | RESTART | dma_width | DI_EN);
Bryan Wubb90eb02007-12-04 23:45:18 -0800708 set_dma_config(drv_data->dma_channel, dma_config);
709 set_dma_start_addr(drv_data->dma_channel,
Bryan Wua32c6912007-12-04 23:45:15 -0800710 (unsigned long)drv_data->tx);
Bryan Wubb90eb02007-12-04 23:45:18 -0800711 enable_dma(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700712
Sonic Zhang07612e52007-12-04 23:45:21 -0800713 /* start SPI transfer */
Mike Frysinger47885ce2011-06-17 04:16:56 -0400714 bfin_write(&drv_data->regs->ctl, cr | BIT_CTL_TIMOD_DMA_TX);
Sonic Zhang07612e52007-12-04 23:45:21 -0800715
716 /* just return here, there can only be one transfer
717 * in this mode
718 */
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700719 message->status = 0;
Mike Frysinger138f97c2009-04-06 19:00:50 -0700720 bfin_spi_giveback(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700721 return;
722 }
723
724 /* In dma mode, rx or tx must be NULL in one transfer */
Mike Frysinger7aec3562009-04-06 19:00:36 -0700725 dma_config = (RESTART | dma_width | DI_EN);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700726 if (drv_data->rx != NULL) {
727 /* set transfer mode, and enable SPI */
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700728 dev_dbg(&drv_data->pdev->dev, "doing DMA in to %p (size %zx)\n",
729 drv_data->rx, drv_data->len_in_bytes);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700730
Vitja Makarov8cf58582009-04-06 19:00:31 -0700731 /* invalidate caches, if needed */
Jie Zhang67834fa2009-06-10 06:26:26 +0000732 if (bfin_addr_dcacheable((unsigned long) drv_data->rx))
Vitja Makarov8cf58582009-04-06 19:00:31 -0700733 invalidate_dcache_range((unsigned long) drv_data->rx,
734 (unsigned long) (drv_data->rx +
Mike Frysingerace32862009-04-06 19:00:34 -0700735 drv_data->len_in_bytes));
Vitja Makarov8cf58582009-04-06 19:00:31 -0700736
Mike Frysinger7aec3562009-04-06 19:00:36 -0700737 dma_config |= WNR;
738 dma_start_addr = (unsigned long)drv_data->rx;
Mike Frysingerb31e27a2009-04-06 19:00:39 -0700739 cr |= BIT_CTL_TIMOD_DMA_RX | BIT_CTL_SENDOPT;
Sonic Zhang07612e52007-12-04 23:45:21 -0800740
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700741 } else if (drv_data->tx != NULL) {
Bryan Wu88b40362007-05-21 18:32:16 +0800742 dev_dbg(&drv_data->pdev->dev, "doing DMA out.\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700743
Vitja Makarov8cf58582009-04-06 19:00:31 -0700744 /* flush caches, if needed */
Jie Zhang67834fa2009-06-10 06:26:26 +0000745 if (bfin_addr_dcacheable((unsigned long) drv_data->tx))
Vitja Makarov8cf58582009-04-06 19:00:31 -0700746 flush_dcache_range((unsigned long) drv_data->tx,
747 (unsigned long) (drv_data->tx +
Mike Frysingerace32862009-04-06 19:00:34 -0700748 drv_data->len_in_bytes));
Vitja Makarov8cf58582009-04-06 19:00:31 -0700749
Mike Frysinger7aec3562009-04-06 19:00:36 -0700750 dma_start_addr = (unsigned long)drv_data->tx;
Mike Frysingerb31e27a2009-04-06 19:00:39 -0700751 cr |= BIT_CTL_TIMOD_DMA_TX;
Sonic Zhang07612e52007-12-04 23:45:21 -0800752
Mike Frysinger7aec3562009-04-06 19:00:36 -0700753 } else
754 BUG();
755
Mike Frysinger11d6f592009-04-06 19:00:41 -0700756 /* oh man, here there be monsters ... and i dont mean the
757 * fluffy cute ones from pixar, i mean the kind that'll eat
758 * your data, kick your dog, and love it all. do *not* try
759 * and change these lines unless you (1) heavily test DMA
760 * with SPI flashes on a loaded system (e.g. ping floods),
761 * (2) know just how broken the DMA engine interaction with
762 * the SPI peripheral is, and (3) have someone else to blame
763 * when you screw it all up anyways.
764 */
Mike Frysinger7aec3562009-04-06 19:00:36 -0700765 set_dma_start_addr(drv_data->dma_channel, dma_start_addr);
Mike Frysinger11d6f592009-04-06 19:00:41 -0700766 set_dma_config(drv_data->dma_channel, dma_config);
767 local_irq_save(flags);
Mike Frysingera963ea82009-04-06 19:00:43 -0700768 SSYNC();
Mike Frysinger47885ce2011-06-17 04:16:56 -0400769 bfin_write(&drv_data->regs->ctl, cr);
Mike Frysingera963ea82009-04-06 19:00:43 -0700770 enable_dma(drv_data->dma_channel);
Mike Frysinger11d6f592009-04-06 19:00:41 -0700771 dma_enable_irq(drv_data->dma_channel);
772 local_irq_restore(flags);
Mike Frysinger7aec3562009-04-06 19:00:36 -0700773
Yi Lif6a6d962009-06-03 09:46:22 +0000774 return;
775 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700776
Mike Frysinger5e8592d2009-12-18 18:00:10 +0000777 /*
778 * We always use SPI_WRITE mode (transfer starts with TDBR write).
779 * SPI_READ mode (transfer starts with RDBR read) seems to have
780 * problems with setting up the output value in TDBR prior to the
781 * start of the transfer.
782 */
Mike Frysinger47885ce2011-06-17 04:16:56 -0400783 bfin_write(&drv_data->regs->ctl, cr | BIT_CTL_TXMOD);
Mike Frysinger5e8592d2009-12-18 18:00:10 +0000784
Yi Lif6a6d962009-06-03 09:46:22 +0000785 if (chip->pio_interrupt) {
Mike Frysinger5e8592d2009-12-18 18:00:10 +0000786 /* SPI irq should have been disabled by now */
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700787
Yi Lif6a6d962009-06-03 09:46:22 +0000788 /* discard old RX data and clear RXS */
789 bfin_spi_dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700790
Yi Lif6a6d962009-06-03 09:46:22 +0000791 /* start transfer */
792 if (drv_data->tx == NULL)
Mike Frysinger47885ce2011-06-17 04:16:56 -0400793 bfin_write(&drv_data->regs->tdbr, chip->idle_tx_val);
Yi Lif6a6d962009-06-03 09:46:22 +0000794 else {
Bob Liu4d676fc2011-01-11 11:19:07 -0500795 int loop;
Stephen Warren24778be2013-05-21 20:36:35 -0600796 if (bits_per_word == 16) {
Bob Liu4d676fc2011-01-11 11:19:07 -0500797 u16 *buf = (u16 *)drv_data->tx;
798 for (loop = 0; loop < bits_per_word / 16;
799 loop++) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400800 bfin_write(&drv_data->regs->tdbr, *buf++);
Bob Liu4d676fc2011-01-11 11:19:07 -0500801 }
Stephen Warren24778be2013-05-21 20:36:35 -0600802 } else if (bits_per_word == 8) {
Bob Liu4d676fc2011-01-11 11:19:07 -0500803 u8 *buf = (u8 *)drv_data->tx;
804 for (loop = 0; loop < bits_per_word / 8; loop++)
Mike Frysinger47885ce2011-06-17 04:16:56 -0400805 bfin_write(&drv_data->regs->tdbr, *buf++);
Bob Liu4d676fc2011-01-11 11:19:07 -0500806 }
807
Yi Lif6a6d962009-06-03 09:46:22 +0000808 drv_data->tx += drv_data->n_bytes;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700809 }
810
Yi Lif6a6d962009-06-03 09:46:22 +0000811 /* once TDBR is empty, interrupt is triggered */
812 enable_irq(drv_data->spi_irq);
813 return;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700814 }
Yi Lif6a6d962009-06-03 09:46:22 +0000815
816 /* IO mode */
817 dev_dbg(&drv_data->pdev->dev, "doing IO transfer\n");
818
Yi Lif6a6d962009-06-03 09:46:22 +0000819 if (full_duplex) {
820 /* full duplex mode */
821 BUG_ON((drv_data->tx_end - drv_data->tx) !=
822 (drv_data->rx_end - drv_data->rx));
823 dev_dbg(&drv_data->pdev->dev,
824 "IO duplex: cr is 0x%x\n", cr);
825
Mike Frysinger9c4542c2009-09-24 01:04:04 +0000826 drv_data->ops->duplex(drv_data);
Yi Lif6a6d962009-06-03 09:46:22 +0000827
828 if (drv_data->tx != drv_data->tx_end)
829 tranf_success = 0;
830 } else if (drv_data->tx != NULL) {
831 /* write only half duplex */
832 dev_dbg(&drv_data->pdev->dev,
833 "IO write: cr is 0x%x\n", cr);
834
Mike Frysinger9c4542c2009-09-24 01:04:04 +0000835 drv_data->ops->write(drv_data);
Yi Lif6a6d962009-06-03 09:46:22 +0000836
837 if (drv_data->tx != drv_data->tx_end)
838 tranf_success = 0;
839 } else if (drv_data->rx != NULL) {
840 /* read only half duplex */
841 dev_dbg(&drv_data->pdev->dev,
842 "IO read: cr is 0x%x\n", cr);
843
Mike Frysinger9c4542c2009-09-24 01:04:04 +0000844 drv_data->ops->read(drv_data);
Yi Lif6a6d962009-06-03 09:46:22 +0000845 if (drv_data->rx != drv_data->rx_end)
846 tranf_success = 0;
847 }
848
849 if (!tranf_success) {
850 dev_dbg(&drv_data->pdev->dev,
851 "IO write error!\n");
852 message->state = ERROR_STATE;
853 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300854 /* Update total byte transferred */
Yi Lif6a6d962009-06-03 09:46:22 +0000855 message->actual_length += drv_data->len_in_bytes;
856 /* Move to next transfer of this msg */
857 message->state = bfin_spi_next_transfer(drv_data);
Scott Jiang2431a812012-04-23 18:18:13 -0400858 if (drv_data->cs_change && message->state != DONE_STATE) {
859 bfin_spi_flush(drv_data);
Yi Lif6a6d962009-06-03 09:46:22 +0000860 bfin_spi_cs_deactive(drv_data, chip);
Scott Jiang2431a812012-04-23 18:18:13 -0400861 }
Yi Lif6a6d962009-06-03 09:46:22 +0000862 }
863
864 /* Schedule next transfer tasklet */
865 tasklet_schedule(&drv_data->pump_transfers);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700866}
867
868/* pop a msg from queue and kick off real transfer */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700869static void bfin_spi_pump_messages(struct work_struct *work)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700870{
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400871 struct bfin_spi_master_data *drv_data;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700872 unsigned long flags;
873
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400874 drv_data = container_of(work, struct bfin_spi_master_data, pump_messages);
Bryan Wu131b17d2007-12-04 23:45:12 -0800875
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700876 /* Lock queue and check for queue work */
877 spin_lock_irqsave(&drv_data->lock, flags);
Mike Frysingerf4f50c32009-09-24 00:41:49 +0000878 if (list_empty(&drv_data->queue) || !drv_data->running) {
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700879 /* pumper kicked off but no work to do */
880 drv_data->busy = 0;
881 spin_unlock_irqrestore(&drv_data->lock, flags);
882 return;
883 }
884
885 /* Make sure we are not already running a message */
886 if (drv_data->cur_msg) {
887 spin_unlock_irqrestore(&drv_data->lock, flags);
888 return;
889 }
890
891 /* Extract head of queue */
892 drv_data->cur_msg = list_entry(drv_data->queue.next,
893 struct spi_message, queue);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800894
895 /* Setup the SSP using the per chip configuration */
896 drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
Mike Frysinger138f97c2009-04-06 19:00:50 -0700897 bfin_spi_restore_state(drv_data);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800898
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700899 list_del_init(&drv_data->cur_msg->queue);
900
901 /* Initial message state */
902 drv_data->cur_msg->state = START_STATE;
903 drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
904 struct spi_transfer, transfer_list);
905
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +0300906 dev_dbg(&drv_data->pdev->dev,
907 "got a message to pump, state is set to: baud "
908 "%d, flag 0x%x, ctl 0x%x\n",
Bryan Wu5fec5b52007-12-04 23:45:13 -0800909 drv_data->cur_chip->baud, drv_data->cur_chip->flag,
910 drv_data->cur_chip->ctl_reg);
Bryan Wu131b17d2007-12-04 23:45:12 -0800911
912 dev_dbg(&drv_data->pdev->dev,
Bryan Wu88b40362007-05-21 18:32:16 +0800913 "the first transfer len is %d\n",
914 drv_data->cur_transfer->len);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700915
916 /* Mark as busy and launch transfers */
917 tasklet_schedule(&drv_data->pump_transfers);
918
919 drv_data->busy = 1;
920 spin_unlock_irqrestore(&drv_data->lock, flags);
921}
922
923/*
924 * got a msg to transfer, queue it in drv_data->queue.
925 * And kick off message pumper
926 */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700927static int bfin_spi_transfer(struct spi_device *spi, struct spi_message *msg)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700928{
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400929 struct bfin_spi_master_data *drv_data = spi_master_get_devdata(spi->master);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700930 unsigned long flags;
931
932 spin_lock_irqsave(&drv_data->lock, flags);
933
Mike Frysingerf4f50c32009-09-24 00:41:49 +0000934 if (!drv_data->running) {
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700935 spin_unlock_irqrestore(&drv_data->lock, flags);
936 return -ESHUTDOWN;
937 }
938
939 msg->actual_length = 0;
940 msg->status = -EINPROGRESS;
941 msg->state = START_STATE;
942
Bryan Wu88b40362007-05-21 18:32:16 +0800943 dev_dbg(&spi->dev, "adding an msg in transfer() \n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700944 list_add_tail(&msg->queue, &drv_data->queue);
945
Mike Frysingerf4f50c32009-09-24 00:41:49 +0000946 if (drv_data->running && !drv_data->busy)
Bhaktipriya Shridhar9b96f072016-07-02 14:12:00 +0530947 schedule_work(&drv_data->pump_messages);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700948
949 spin_unlock_irqrestore(&drv_data->lock, flags);
950
951 return 0;
952}
953
Sonic Zhang12e17c42007-12-04 23:45:16 -0800954#define MAX_SPI_SSEL 7
955
Mike Frysingerddc0bf12011-06-17 04:16:57 -0400956static const u16 ssel[][MAX_SPI_SSEL] = {
Sonic Zhang12e17c42007-12-04 23:45:16 -0800957 {P_SPI0_SSEL1, P_SPI0_SSEL2, P_SPI0_SSEL3,
958 P_SPI0_SSEL4, P_SPI0_SSEL5,
959 P_SPI0_SSEL6, P_SPI0_SSEL7},
960
961 {P_SPI1_SSEL1, P_SPI1_SSEL2, P_SPI1_SSEL3,
962 P_SPI1_SSEL4, P_SPI1_SSEL5,
963 P_SPI1_SSEL6, P_SPI1_SSEL7},
964
965 {P_SPI2_SSEL1, P_SPI2_SSEL2, P_SPI2_SSEL3,
966 P_SPI2_SSEL4, P_SPI2_SSEL5,
967 P_SPI2_SSEL6, P_SPI2_SSEL7},
968};
969
Mike Frysingerab09e042009-09-23 23:32:34 +0000970/* setup for devices (may be called multiple times -- not just first setup) */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700971static int bfin_spi_setup(struct spi_device *spi)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700972{
Daniel Mackac01e972009-03-25 00:18:35 +0000973 struct bfin5xx_spi_chip *chip_info;
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400974 struct bfin_spi_slave_data *chip = NULL;
975 struct bfin_spi_master_data *drv_data = spi_master_get_devdata(spi->master);
Mike Frysinger5b47bcd2009-12-18 17:43:31 +0000976 u16 bfin_ctl_reg;
Daniel Mackac01e972009-03-25 00:18:35 +0000977 int ret = -EINVAL;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700978
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700979 /* Only alloc (or use chip_info) on first setup */
Daniel Mackac01e972009-03-25 00:18:35 +0000980 chip_info = NULL;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700981 chip = spi_get_ctldata(spi);
982 if (chip == NULL) {
Daniel Mackac01e972009-03-25 00:18:35 +0000983 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
984 if (!chip) {
985 dev_err(&spi->dev, "cannot allocate chip data\n");
986 ret = -ENOMEM;
987 goto error;
988 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700989
990 chip->enable_dma = 0;
991 chip_info = spi->controller_data;
992 }
993
Mike Frysinger5b47bcd2009-12-18 17:43:31 +0000994 /* Let people set non-standard bits directly */
995 bfin_ctl_reg = BIT_CTL_OPENDRAIN | BIT_CTL_EMISO |
996 BIT_CTL_PSSE | BIT_CTL_GM | BIT_CTL_SZ;
997
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700998 /* chip_info isn't always needed */
999 if (chip_info) {
Mike Frysinger2ed35512007-12-04 23:45:14 -08001000 /* Make sure people stop trying to set fields via ctl_reg
1001 * when they should actually be using common SPI framework.
Mike Frysinger90008a62009-10-15 04:13:29 +00001002 * Currently we let through: WOM EMISO PSSE GM SZ.
Mike Frysinger2ed35512007-12-04 23:45:14 -08001003 * Not sure if a user actually needs/uses any of these,
1004 * but let's assume (for now) they do.
1005 */
Mike Frysinger5b47bcd2009-12-18 17:43:31 +00001006 if (chip_info->ctl_reg & ~bfin_ctl_reg) {
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +03001007 dev_err(&spi->dev,
1008 "do not set bits in ctl_reg that the SPI framework manages\n");
Daniel Mackac01e972009-03-25 00:18:35 +00001009 goto error;
Mike Frysinger2ed35512007-12-04 23:45:14 -08001010 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001011 chip->enable_dma = chip_info->enable_dma != 0
1012 && drv_data->master_info->enable_dma;
1013 chip->ctl_reg = chip_info->ctl_reg;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001014 chip->cs_chg_udelay = chip_info->cs_chg_udelay;
Wolfgang Muees93b61bd2009-04-06 19:00:53 -07001015 chip->idle_tx_val = chip_info->idle_tx_val;
Yi Lif6a6d962009-06-03 09:46:22 +00001016 chip->pio_interrupt = chip_info->pio_interrupt;
Mike Frysinger5b47bcd2009-12-18 17:43:31 +00001017 } else {
1018 /* force a default base state */
1019 chip->ctl_reg &= bfin_ctl_reg;
Mike Frysinger033f44b2009-12-18 17:38:04 +00001020 }
1021
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001022 /* translate common spi framework into our register */
1023 if (spi->mode & SPI_CPOL)
Mike Frysinger90008a62009-10-15 04:13:29 +00001024 chip->ctl_reg |= BIT_CTL_CPOL;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001025 if (spi->mode & SPI_CPHA)
Mike Frysinger90008a62009-10-15 04:13:29 +00001026 chip->ctl_reg |= BIT_CTL_CPHA;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001027 if (spi->mode & SPI_LSB_FIRST)
Mike Frysinger90008a62009-10-15 04:13:29 +00001028 chip->ctl_reg |= BIT_CTL_LSBF;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001029 /* we dont support running in slave mode (yet?) */
Mike Frysinger90008a62009-10-15 04:13:29 +00001030 chip->ctl_reg |= BIT_CTL_MASTER;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001031
1032 /*
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001033 * Notice: for blackfin, the speed_hz is the value of register
1034 * SPI_BAUD, not the real baudrate
1035 */
1036 chip->baud = hz_to_spi_baud(spi->max_speed_hz);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001037 chip->chip_select_num = spi->chip_select;
Barry Song4190f6a2010-04-06 03:36:24 +00001038 if (chip->chip_select_num < MAX_CTRL_CS) {
1039 if (!(spi->mode & SPI_CPHA))
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +03001040 dev_warn(&spi->dev,
1041 "Warning: SPI CPHA not set: Slave Select not under software control!\n"
1042 "See Documentation/blackfin/bfin-spi-notes.txt\n");
Barry Song4190f6a2010-04-06 03:36:24 +00001043
Barry Songd3cc71f2009-11-17 09:45:59 +00001044 chip->flag = (1 << spi->chip_select) << 8;
Barry Song4190f6a2010-04-06 03:36:24 +00001045 } else
Barry Songd3cc71f2009-11-17 09:45:59 +00001046 chip->cs_gpio = chip->chip_select_num - MAX_CTRL_CS;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001047
Yi Lif6a6d962009-06-03 09:46:22 +00001048 if (chip->enable_dma && chip->pio_interrupt) {
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +03001049 dev_err(&spi->dev,
1050 "enable_dma is set, do not set pio_interrupt\n");
Yi Lif6a6d962009-06-03 09:46:22 +00001051 goto error;
1052 }
Daniel Mackac01e972009-03-25 00:18:35 +00001053 /*
1054 * if any one SPI chip is registered and wants DMA, request the
1055 * DMA channel for it
1056 */
1057 if (chip->enable_dma && !drv_data->dma_requested) {
1058 /* register dma irq handler */
1059 ret = request_dma(drv_data->dma_channel, "BFIN_SPI_DMA");
1060 if (ret) {
1061 dev_err(&spi->dev,
1062 "Unable to request BlackFin SPI DMA channel\n");
1063 goto error;
1064 }
1065 drv_data->dma_requested = 1;
1066
1067 ret = set_dma_callback(drv_data->dma_channel,
1068 bfin_spi_dma_irq_handler, drv_data);
1069 if (ret) {
1070 dev_err(&spi->dev, "Unable to set dma callback\n");
1071 goto error;
1072 }
1073 dma_disable_irq(drv_data->dma_channel);
1074 }
1075
Yi Lif6a6d962009-06-03 09:46:22 +00001076 if (chip->pio_interrupt && !drv_data->irq_requested) {
1077 ret = request_irq(drv_data->spi_irq, bfin_spi_pio_irq_handler,
Yong Zhang38ada212011-10-22 17:56:55 +08001078 0, "BFIN_SPI", drv_data);
Yi Lif6a6d962009-06-03 09:46:22 +00001079 if (ret) {
1080 dev_err(&spi->dev, "Unable to register spi IRQ\n");
1081 goto error;
1082 }
1083 drv_data->irq_requested = 1;
1084 /* we use write mode, spi irq has to be disabled here */
1085 disable_irq(drv_data->spi_irq);
1086 }
1087
Barry Songd3cc71f2009-11-17 09:45:59 +00001088 if (chip->chip_select_num >= MAX_CTRL_CS) {
Michael Hennerich73e1ac12010-10-22 02:01:47 -04001089 /* Only request on first setup */
1090 if (spi_get_ctldata(spi) == NULL) {
1091 ret = gpio_request(chip->cs_gpio, spi->modalias);
1092 if (ret) {
1093 dev_err(&spi->dev, "gpio_request() error\n");
1094 goto pin_error;
1095 }
1096 gpio_direction_output(chip->cs_gpio, 1);
Daniel Mackac01e972009-03-25 00:18:35 +00001097 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001098 }
1099
Joe Perches898eb712007-10-18 03:06:30 -07001100 dev_dbg(&spi->dev, "setup spi chip %s, width is %d, dma is %d\n",
Mike Frysinger033f44b2009-12-18 17:38:04 +00001101 spi->modalias, spi->bits_per_word, chip->enable_dma);
Bryan Wu88b40362007-05-21 18:32:16 +08001102 dev_dbg(&spi->dev, "ctl_reg is 0x%x, flag_reg is 0x%x\n",
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001103 chip->ctl_reg, chip->flag);
1104
1105 spi_set_ctldata(spi, chip);
1106
Sonic Zhang12e17c42007-12-04 23:45:16 -08001107 dev_dbg(&spi->dev, "chip select number is %d\n", chip->chip_select_num);
Barry Songd3cc71f2009-11-17 09:45:59 +00001108 if (chip->chip_select_num < MAX_CTRL_CS) {
Daniel Mackac01e972009-03-25 00:18:35 +00001109 ret = peripheral_request(ssel[spi->master->bus_num]
1110 [chip->chip_select_num-1], spi->modalias);
1111 if (ret) {
1112 dev_err(&spi->dev, "peripheral_request() error\n");
1113 goto pin_error;
1114 }
1115 }
Sonic Zhang12e17c42007-12-04 23:45:16 -08001116
Barry Song82216102009-06-17 10:10:53 +00001117 bfin_spi_cs_enable(drv_data, chip);
Mike Frysinger138f97c2009-04-06 19:00:50 -07001118 bfin_spi_cs_deactive(drv_data, chip);
Sonic Zhang07612e52007-12-04 23:45:21 -08001119
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001120 return 0;
Daniel Mackac01e972009-03-25 00:18:35 +00001121
1122 pin_error:
Barry Songd3cc71f2009-11-17 09:45:59 +00001123 if (chip->chip_select_num >= MAX_CTRL_CS)
Daniel Mackac01e972009-03-25 00:18:35 +00001124 gpio_free(chip->cs_gpio);
1125 else
1126 peripheral_free(ssel[spi->master->bus_num]
1127 [chip->chip_select_num - 1]);
1128 error:
1129 if (chip) {
1130 if (drv_data->dma_requested)
1131 free_dma(drv_data->dma_channel);
1132 drv_data->dma_requested = 0;
1133
1134 kfree(chip);
1135 /* prevent free 'chip' twice */
1136 spi_set_ctldata(spi, NULL);
1137 }
1138
1139 return ret;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001140}
1141
1142/*
1143 * callback for spi framework.
1144 * clean driver specific data
1145 */
Mike Frysinger138f97c2009-04-06 19:00:50 -07001146static void bfin_spi_cleanup(struct spi_device *spi)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001147{
Mike Frysinger9c0a7882010-10-18 02:45:22 -04001148 struct bfin_spi_slave_data *chip = spi_get_ctldata(spi);
1149 struct bfin_spi_master_data *drv_data = spi_master_get_devdata(spi->master);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001150
Mike Frysingere7d02e32009-04-06 19:00:51 -07001151 if (!chip)
1152 return;
1153
Barry Songd3cc71f2009-11-17 09:45:59 +00001154 if (chip->chip_select_num < MAX_CTRL_CS) {
Sonic Zhang12e17c42007-12-04 23:45:16 -08001155 peripheral_free(ssel[spi->master->bus_num]
1156 [chip->chip_select_num-1]);
Barry Song82216102009-06-17 10:10:53 +00001157 bfin_spi_cs_disable(drv_data, chip);
Barry Songd3cc71f2009-11-17 09:45:59 +00001158 } else
Michael Hennerich42c78b22009-04-06 19:00:51 -07001159 gpio_free(chip->cs_gpio);
1160
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001161 kfree(chip);
Daniel Mackac01e972009-03-25 00:18:35 +00001162 /* prevent free 'chip' twice */
1163 spi_set_ctldata(spi, NULL);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001164}
1165
Mike Frysingerc52d4e52011-06-17 04:16:58 -04001166static int bfin_spi_init_queue(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001167{
1168 INIT_LIST_HEAD(&drv_data->queue);
1169 spin_lock_init(&drv_data->lock);
1170
Mike Frysingerf4f50c32009-09-24 00:41:49 +00001171 drv_data->running = false;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001172 drv_data->busy = 0;
1173
1174 /* init transfer tasklet */
1175 tasklet_init(&drv_data->pump_transfers,
Mike Frysinger138f97c2009-04-06 19:00:50 -07001176 bfin_spi_pump_transfers, (unsigned long)drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001177
Mike Frysinger138f97c2009-04-06 19:00:50 -07001178 INIT_WORK(&drv_data->pump_messages, bfin_spi_pump_messages);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001179
1180 return 0;
1181}
1182
Mike Frysingerc52d4e52011-06-17 04:16:58 -04001183static int bfin_spi_start_queue(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001184{
1185 unsigned long flags;
1186
1187 spin_lock_irqsave(&drv_data->lock, flags);
1188
Mike Frysingerf4f50c32009-09-24 00:41:49 +00001189 if (drv_data->running || drv_data->busy) {
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001190 spin_unlock_irqrestore(&drv_data->lock, flags);
1191 return -EBUSY;
1192 }
1193
Mike Frysingerf4f50c32009-09-24 00:41:49 +00001194 drv_data->running = true;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001195 drv_data->cur_msg = NULL;
1196 drv_data->cur_transfer = NULL;
1197 drv_data->cur_chip = NULL;
1198 spin_unlock_irqrestore(&drv_data->lock, flags);
1199
Bhaktipriya Shridhar9b96f072016-07-02 14:12:00 +05301200 schedule_work(&drv_data->pump_messages);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001201
1202 return 0;
1203}
1204
Mike Frysingerc52d4e52011-06-17 04:16:58 -04001205static int bfin_spi_stop_queue(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001206{
1207 unsigned long flags;
1208 unsigned limit = 500;
1209 int status = 0;
1210
1211 spin_lock_irqsave(&drv_data->lock, flags);
1212
1213 /*
1214 * This is a bit lame, but is optimized for the common execution path.
1215 * A wait_queue on the drv_data->busy could be used, but then the common
1216 * execution path (pump_messages) would be required to call wake_up or
1217 * friends on every SPI message. Do this instead
1218 */
Mike Frysingerf4f50c32009-09-24 00:41:49 +00001219 drv_data->running = false;
Vasily Khoruzhick850a28e2011-04-06 17:49:15 +03001220 while ((!list_empty(&drv_data->queue) || drv_data->busy) && limit--) {
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001221 spin_unlock_irqrestore(&drv_data->lock, flags);
1222 msleep(10);
1223 spin_lock_irqsave(&drv_data->lock, flags);
1224 }
1225
1226 if (!list_empty(&drv_data->queue) || drv_data->busy)
1227 status = -EBUSY;
1228
1229 spin_unlock_irqrestore(&drv_data->lock, flags);
1230
1231 return status;
1232}
1233
Mike Frysingerc52d4e52011-06-17 04:16:58 -04001234static int bfin_spi_destroy_queue(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001235{
1236 int status;
1237
Mike Frysinger138f97c2009-04-06 19:00:50 -07001238 status = bfin_spi_stop_queue(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001239 if (status != 0)
1240 return status;
1241
Bhaktipriya Shridhar9b96f072016-07-02 14:12:00 +05301242 flush_work(&drv_data->pump_messages);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001243
1244 return 0;
1245}
1246
Grant Likely2deff8d2013-02-05 13:27:35 +00001247static int bfin_spi_probe(struct platform_device *pdev)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001248{
1249 struct device *dev = &pdev->dev;
1250 struct bfin5xx_spi_master *platform_info;
1251 struct spi_master *master;
Mike Frysinger9c0a7882010-10-18 02:45:22 -04001252 struct bfin_spi_master_data *drv_data;
Bryan Wua32c6912007-12-04 23:45:15 -08001253 struct resource *res;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001254 int status = 0;
1255
Jingoo Han8074cf02013-07-30 16:58:59 +09001256 platform_info = dev_get_platdata(dev);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001257
1258 /* Allocate master with space for drv_data */
Mike Frysinger2a045132009-09-24 01:28:54 +00001259 master = spi_alloc_master(dev, sizeof(*drv_data));
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001260 if (!master) {
1261 dev_err(&pdev->dev, "can not alloc spi_master\n");
1262 return -ENOMEM;
1263 }
Bryan Wu131b17d2007-12-04 23:45:12 -08001264
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001265 drv_data = spi_master_get_devdata(master);
1266 drv_data->master = master;
1267 drv_data->master_info = platform_info;
1268 drv_data->pdev = pdev;
Bryan Wu003d9222007-12-04 23:45:22 -08001269 drv_data->pin_req = platform_info->pin_req;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001270
David Brownelle7db06b2009-06-17 16:26:04 -07001271 /* the spi->mode bits supported by this driver: */
1272 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST;
Stephen Warren24778be2013-05-21 20:36:35 -06001273 master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001274 master->bus_num = pdev->id;
1275 master->num_chipselect = platform_info->num_chipselect;
Mike Frysinger138f97c2009-04-06 19:00:50 -07001276 master->cleanup = bfin_spi_cleanup;
1277 master->setup = bfin_spi_setup;
1278 master->transfer = bfin_spi_transfer;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001279
Bryan Wua32c6912007-12-04 23:45:15 -08001280 /* Find and map our resources */
1281 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1282 if (res == NULL) {
1283 dev_err(dev, "Cannot get IORESOURCE_MEM\n");
1284 status = -ENOENT;
1285 goto out_error_get_res;
1286 }
1287
Mike Frysinger47885ce2011-06-17 04:16:56 -04001288 drv_data->regs = ioremap(res->start, resource_size(res));
1289 if (drv_data->regs == NULL) {
Bryan Wua32c6912007-12-04 23:45:15 -08001290 dev_err(dev, "Cannot map IO\n");
1291 status = -ENXIO;
1292 goto out_error_ioremap;
1293 }
1294
Yi Lif6a6d962009-06-03 09:46:22 +00001295 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1296 if (res == NULL) {
Bryan Wua32c6912007-12-04 23:45:15 -08001297 dev_err(dev, "No DMA channel specified\n");
1298 status = -ENOENT;
Yi Lif6a6d962009-06-03 09:46:22 +00001299 goto out_error_free_io;
1300 }
1301 drv_data->dma_channel = res->start;
1302
1303 drv_data->spi_irq = platform_get_irq(pdev, 0);
1304 if (drv_data->spi_irq < 0) {
1305 dev_err(dev, "No spi pio irq specified\n");
1306 status = -ENOENT;
1307 goto out_error_free_io;
Bryan Wua32c6912007-12-04 23:45:15 -08001308 }
1309
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001310 /* Initial and start queue */
Mike Frysinger138f97c2009-04-06 19:00:50 -07001311 status = bfin_spi_init_queue(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001312 if (status != 0) {
Bryan Wua32c6912007-12-04 23:45:15 -08001313 dev_err(dev, "problem initializing queue\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001314 goto out_error_queue_alloc;
1315 }
Bryan Wua32c6912007-12-04 23:45:15 -08001316
Mike Frysinger138f97c2009-04-06 19:00:50 -07001317 status = bfin_spi_start_queue(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001318 if (status != 0) {
Bryan Wua32c6912007-12-04 23:45:15 -08001319 dev_err(dev, "problem starting queue\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001320 goto out_error_queue_alloc;
1321 }
1322
Vitja Makarovf9e522c2008-04-08 17:41:57 -07001323 status = peripheral_request_list(drv_data->pin_req, DRV_NAME);
1324 if (status != 0) {
1325 dev_err(&pdev->dev, ": Requesting Peripherals failed\n");
1326 goto out_error_queue_alloc;
1327 }
1328
Wolfgang Mueesbb8beecd2009-05-22 01:11:02 +00001329 /* Reset SPI registers. If these registers were used by the boot loader,
1330 * the sky may fall on your head if you enable the dma controller.
1331 */
Mike Frysinger47885ce2011-06-17 04:16:56 -04001332 bfin_write(&drv_data->regs->ctl, BIT_CTL_CPHA | BIT_CTL_MASTER);
1333 bfin_write(&drv_data->regs->flg, 0xFF00);
Wolfgang Mueesbb8beecd2009-05-22 01:11:02 +00001334
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001335 /* Register with the SPI framework */
1336 platform_set_drvdata(pdev, drv_data);
1337 status = spi_register_master(master);
1338 if (status != 0) {
Bryan Wua32c6912007-12-04 23:45:15 -08001339 dev_err(dev, "problem registering spi master\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001340 goto out_error_queue_alloc;
1341 }
Bryan Wua32c6912007-12-04 23:45:15 -08001342
Mike Frysinger47885ce2011-06-17 04:16:56 -04001343 dev_info(dev, "%s, Version %s, regs@%p, dma channel@%d\n",
1344 DRV_DESC, DRV_VERSION, drv_data->regs,
Bryan Wubb90eb02007-12-04 23:45:18 -08001345 drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001346 return status;
1347
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001348out_error_queue_alloc:
Mike Frysinger138f97c2009-04-06 19:00:50 -07001349 bfin_spi_destroy_queue(drv_data);
Yi Lif6a6d962009-06-03 09:46:22 +00001350out_error_free_io:
Mike Frysinger47885ce2011-06-17 04:16:56 -04001351 iounmap(drv_data->regs);
Bryan Wua32c6912007-12-04 23:45:15 -08001352out_error_ioremap:
1353out_error_get_res:
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001354 spi_master_put(master);
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001355
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001356 return status;
1357}
1358
1359/* stop hardware and remove the driver */
Grant Likelyfd4a3192012-12-07 16:57:14 +00001360static int bfin_spi_remove(struct platform_device *pdev)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001361{
Mike Frysinger9c0a7882010-10-18 02:45:22 -04001362 struct bfin_spi_master_data *drv_data = platform_get_drvdata(pdev);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001363 int status = 0;
1364
1365 if (!drv_data)
1366 return 0;
1367
1368 /* Remove the queue */
Mike Frysinger138f97c2009-04-06 19:00:50 -07001369 status = bfin_spi_destroy_queue(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001370 if (status != 0)
1371 return status;
1372
1373 /* Disable the SSP at the peripheral and SOC level */
1374 bfin_spi_disable(drv_data);
1375
1376 /* Release DMA */
1377 if (drv_data->master_info->enable_dma) {
Bryan Wubb90eb02007-12-04 23:45:18 -08001378 if (dma_channel_active(drv_data->dma_channel))
1379 free_dma(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001380 }
1381
Yi Lif6a6d962009-06-03 09:46:22 +00001382 if (drv_data->irq_requested) {
1383 free_irq(drv_data->spi_irq, drv_data);
1384 drv_data->irq_requested = 0;
1385 }
1386
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001387 /* Disconnect from the SPI framework */
1388 spi_unregister_master(drv_data->master);
1389
Bryan Wu003d9222007-12-04 23:45:22 -08001390 peripheral_free_list(drv_data->pin_req);
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001391
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001392 return 0;
1393}
1394
Jingoo Hanfbbfd682013-09-09 17:55:32 +09001395#ifdef CONFIG_PM_SLEEP
1396static int bfin_spi_suspend(struct device *dev)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001397{
Jingoo Hanfbbfd682013-09-09 17:55:32 +09001398 struct bfin_spi_master_data *drv_data = dev_get_drvdata(dev);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001399 int status = 0;
1400
Mike Frysinger138f97c2009-04-06 19:00:50 -07001401 status = bfin_spi_stop_queue(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001402 if (status != 0)
1403 return status;
1404
Mike Frysinger47885ce2011-06-17 04:16:56 -04001405 drv_data->ctrl_reg = bfin_read(&drv_data->regs->ctl);
1406 drv_data->flag_reg = bfin_read(&drv_data->regs->flg);
Barry Songb052fd02009-11-18 09:43:21 +00001407
1408 /*
1409 * reset SPI_CTL and SPI_FLG registers
1410 */
Mike Frysinger47885ce2011-06-17 04:16:56 -04001411 bfin_write(&drv_data->regs->ctl, BIT_CTL_CPHA | BIT_CTL_MASTER);
1412 bfin_write(&drv_data->regs->flg, 0xFF00);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001413
1414 return 0;
1415}
1416
Jingoo Hanfbbfd682013-09-09 17:55:32 +09001417static int bfin_spi_resume(struct device *dev)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001418{
Jingoo Hanfbbfd682013-09-09 17:55:32 +09001419 struct bfin_spi_master_data *drv_data = dev_get_drvdata(dev);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001420 int status = 0;
1421
Mike Frysinger47885ce2011-06-17 04:16:56 -04001422 bfin_write(&drv_data->regs->ctl, drv_data->ctrl_reg);
1423 bfin_write(&drv_data->regs->flg, drv_data->flag_reg);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001424
1425 /* Start the queue running */
Mike Frysinger138f97c2009-04-06 19:00:50 -07001426 status = bfin_spi_start_queue(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001427 if (status != 0) {
Jingoo Hanfbbfd682013-09-09 17:55:32 +09001428 dev_err(dev, "problem starting queue (%d)\n", status);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001429 return status;
1430 }
1431
1432 return 0;
1433}
Jingoo Hanfbbfd682013-09-09 17:55:32 +09001434
1435static SIMPLE_DEV_PM_OPS(bfin_spi_pm_ops, bfin_spi_suspend, bfin_spi_resume);
1436
1437#define BFIN_SPI_PM_OPS (&bfin_spi_pm_ops)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001438#else
Jingoo Hanfbbfd682013-09-09 17:55:32 +09001439#define BFIN_SPI_PM_OPS NULL
1440#endif
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001441
Kay Sievers7e38c3c2008-04-10 21:29:20 -07001442MODULE_ALIAS("platform:bfin-spi");
Mike Frysinger138f97c2009-04-06 19:00:50 -07001443static struct platform_driver bfin_spi_driver = {
David Brownellfc3ba952007-08-30 23:56:24 -07001444 .driver = {
Bryan Wua32c6912007-12-04 23:45:15 -08001445 .name = DRV_NAME,
Jingoo Hanfbbfd682013-09-09 17:55:32 +09001446 .pm = BFIN_SPI_PM_OPS,
Bryan Wu88b40362007-05-21 18:32:16 +08001447 },
Wolfram Sangdb9371b2013-10-08 22:35:38 +02001448 .probe = bfin_spi_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +00001449 .remove = bfin_spi_remove,
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001450};
1451
Mike Frysinger138f97c2009-04-06 19:00:50 -07001452static int __init bfin_spi_init(void)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001453{
Wolfram Sangdb9371b2013-10-08 22:35:38 +02001454 return platform_driver_register(&bfin_spi_driver);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001455}
Michael Hennerich6f7c17f2010-07-01 14:34:10 +00001456subsys_initcall(bfin_spi_init);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001457
Mike Frysinger138f97c2009-04-06 19:00:50 -07001458static void __exit bfin_spi_exit(void)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001459{
Mike Frysinger138f97c2009-04-06 19:00:50 -07001460 platform_driver_unregister(&bfin_spi_driver);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001461}
Mike Frysinger138f97c2009-04-06 19:00:50 -07001462module_exit(bfin_spi_exit);