blob: 1e91325bf39c20f0b5fdc368680de06d566fdb6f [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
70 /* Driver message queue */
71 struct workqueue_struct *workqueue;
72 struct work_struct pump_messages;
73 spinlock_t lock;
74 struct list_head queue;
75 int busy;
Mike Frysingerf4f50c32009-09-24 00:41:49 +000076 bool running;
Wu, Bryana5f6abd2007-05-06 14:50:34 -070077
78 /* Message Transfer pump */
79 struct tasklet_struct pump_transfers;
80
81 /* Current message transfer state info */
82 struct spi_message *cur_msg;
83 struct spi_transfer *cur_transfer;
Mike Frysinger9c0a7882010-10-18 02:45:22 -040084 struct bfin_spi_slave_data *cur_chip;
Wu, Bryana5f6abd2007-05-06 14:50:34 -070085 size_t len_in_bytes;
86 size_t len;
87 void *tx;
88 void *tx_end;
89 void *rx;
90 void *rx_end;
Bryan Wubb90eb02007-12-04 23:45:18 -080091
92 /* DMA stuffs */
93 int dma_channel;
Wu, Bryana5f6abd2007-05-06 14:50:34 -070094 int dma_mapped;
Bryan Wubb90eb02007-12-04 23:45:18 -080095 int dma_requested;
Wu, Bryana5f6abd2007-05-06 14:50:34 -070096 dma_addr_t rx_dma;
97 dma_addr_t tx_dma;
Bryan Wubb90eb02007-12-04 23:45:18 -080098
Yi Lif6a6d962009-06-03 09:46:22 +000099 int irq_requested;
100 int spi_irq;
101
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700102 size_t rx_map_len;
103 size_t tx_map_len;
104 u8 n_bytes;
Barry Songb052fd02009-11-18 09:43:21 +0000105 u16 ctrl_reg;
106 u16 flag_reg;
107
Bryan Wufad91c82007-12-04 23:45:14 -0800108 int cs_change;
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400109 const struct bfin_spi_transfer_ops *ops;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700110};
111
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400112struct bfin_spi_slave_data {
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700113 u16 ctl_reg;
114 u16 baud;
115 u16 flag;
116
117 u8 chip_select_num;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700118 u8 enable_dma;
Bryan Wu62310e52007-12-04 23:45:20 -0800119 u16 cs_chg_udelay; /* Some devices require > 255usec delay */
Michael Hennerich42c78b22009-04-06 19:00:51 -0700120 u32 cs_gpio;
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700121 u16 idle_tx_val;
Yi Lif6a6d962009-06-03 09:46:22 +0000122 u8 pio_interrupt; /* use spi data irq */
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400123 const struct bfin_spi_transfer_ops *ops;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700124};
125
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400126static void bfin_spi_enable(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700127{
Mike Frysinger47885ce2011-06-17 04:16:56 -0400128 bfin_write_or(&drv_data->regs->ctl, BIT_CTL_ENABLE);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700129}
130
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400131static void bfin_spi_disable(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700132{
Mike Frysinger47885ce2011-06-17 04:16:56 -0400133 bfin_write_and(&drv_data->regs->ctl, ~BIT_CTL_ENABLE);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700134}
135
136/* Caculate the SPI_BAUD register value based on input HZ */
137static u16 hz_to_spi_baud(u32 speed_hz)
138{
139 u_long sclk = get_sclk();
140 u16 spi_baud = (sclk / (2 * speed_hz));
141
142 if ((sclk % (2 * speed_hz)) > 0)
143 spi_baud++;
144
Michael Hennerich7513e002009-04-06 19:00:32 -0700145 if (spi_baud < MIN_SPI_BAUD_VAL)
146 spi_baud = MIN_SPI_BAUD_VAL;
147
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700148 return spi_baud;
149}
150
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400151static int bfin_spi_flush(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700152{
153 unsigned long limit = loops_per_jiffy << 1;
154
155 /* wait for stop and clear stat */
Mike Frysinger47885ce2011-06-17 04:16:56 -0400156 while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_SPIF) && --limit)
Bryan Wud8c05002007-12-04 23:45:21 -0800157 cpu_relax();
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700158
Mike Frysinger47885ce2011-06-17 04:16:56 -0400159 bfin_write(&drv_data->regs->stat, BIT_STAT_CLR);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700160
161 return limit;
162}
163
Bryan Wufad91c82007-12-04 23:45:14 -0800164/* Chip select operation functions for cs_change flag */
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400165static 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 -0800166{
Mike Frysinger47885ce2011-06-17 04:16:56 -0400167 if (likely(chip->chip_select_num < MAX_CTRL_CS))
168 bfin_write_and(&drv_data->regs->flg, ~chip->flag);
169 else
Michael Hennerich42c78b22009-04-06 19:00:51 -0700170 gpio_set_value(chip->cs_gpio, 0);
Bryan Wufad91c82007-12-04 23:45:14 -0800171}
172
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400173static void bfin_spi_cs_deactive(struct bfin_spi_master_data *drv_data,
174 struct bfin_spi_slave_data *chip)
Bryan Wufad91c82007-12-04 23:45:14 -0800175{
Mike Frysinger47885ce2011-06-17 04:16:56 -0400176 if (likely(chip->chip_select_num < MAX_CTRL_CS))
177 bfin_write_or(&drv_data->regs->flg, chip->flag);
178 else
Michael Hennerich42c78b22009-04-06 19:00:51 -0700179 gpio_set_value(chip->cs_gpio, 1);
Bryan Wu62310e52007-12-04 23:45:20 -0800180
181 /* Move delay here for consistency */
182 if (chip->cs_chg_udelay)
183 udelay(chip->cs_chg_udelay);
Bryan Wufad91c82007-12-04 23:45:14 -0800184}
185
Barry Song82216102009-06-17 10:10:53 +0000186/* enable or disable the pin muxed by GPIO and SPI CS to work as SPI CS */
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400187static inline void bfin_spi_cs_enable(struct bfin_spi_master_data *drv_data,
188 struct bfin_spi_slave_data *chip)
Barry Song82216102009-06-17 10:10:53 +0000189{
Mike Frysinger47885ce2011-06-17 04:16:56 -0400190 if (chip->chip_select_num < MAX_CTRL_CS)
191 bfin_write_or(&drv_data->regs->flg, chip->flag >> 8);
Barry Song82216102009-06-17 10:10:53 +0000192}
193
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400194static inline void bfin_spi_cs_disable(struct bfin_spi_master_data *drv_data,
195 struct bfin_spi_slave_data *chip)
Barry Song82216102009-06-17 10:10:53 +0000196{
Mike Frysinger47885ce2011-06-17 04:16:56 -0400197 if (chip->chip_select_num < MAX_CTRL_CS)
198 bfin_write_and(&drv_data->regs->flg, ~(chip->flag >> 8));
Barry Song82216102009-06-17 10:10:53 +0000199}
200
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700201/* stop controller and re-config current chip*/
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400202static void bfin_spi_restore_state(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700203{
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400204 struct bfin_spi_slave_data *chip = drv_data->cur_chip;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700205
206 /* Clear status and disable clock */
Mike Frysinger47885ce2011-06-17 04:16:56 -0400207 bfin_write(&drv_data->regs->stat, BIT_STAT_CLR);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700208 bfin_spi_disable(drv_data);
Bryan Wu88b40362007-05-21 18:32:16 +0800209 dev_dbg(&drv_data->pdev->dev, "restoring spi ctl state\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700210
Barry Song9677b0de2009-11-30 03:49:41 +0000211 SSYNC();
212
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700213 /* Load the registers */
Mike Frysinger47885ce2011-06-17 04:16:56 -0400214 bfin_write(&drv_data->regs->ctl, chip->ctl_reg);
215 bfin_write(&drv_data->regs->baud, chip->baud);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800216
217 bfin_spi_enable(drv_data);
Mike Frysinger138f97c2009-04-06 19:00:50 -0700218 bfin_spi_cs_active(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700219}
220
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700221/* used to kick off transfer in rx mode and read unwanted RX data */
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400222static inline void bfin_spi_dummy_read(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700223{
Mike Frysinger47885ce2011-06-17 04:16:56 -0400224 (void) bfin_read(&drv_data->regs->rdbr);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700225}
226
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400227static void bfin_spi_u8_writer(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700228{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700229 /* clear RXS (we check for RXS inside the loop) */
230 bfin_spi_dummy_read(drv_data);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800231
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700232 while (drv_data->tx < drv_data->tx_end) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400233 bfin_write(&drv_data->regs->tdbr, (*(u8 *) (drv_data->tx++)));
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700234 /* wait until transfer finished.
235 checking SPIF or TXS may not guarantee transfer completion */
Mike Frysinger47885ce2011-06-17 04:16:56 -0400236 while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800237 cpu_relax();
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700238 /* discard RX data and clear RXS */
239 bfin_spi_dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700240 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700241}
242
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400243static void bfin_spi_u8_reader(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700244{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700245 u16 tx_val = drv_data->cur_chip->idle_tx_val;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700246
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700247 /* discard old RX data and clear RXS */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700248 bfin_spi_dummy_read(drv_data);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800249
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700250 while (drv_data->rx < drv_data->rx_end) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400251 bfin_write(&drv_data->regs->tdbr, tx_val);
252 while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800253 cpu_relax();
Mike Frysinger47885ce2011-06-17 04:16:56 -0400254 *(u8 *) (drv_data->rx++) = bfin_read(&drv_data->regs->rdbr);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700255 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700256}
257
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400258static void bfin_spi_u8_duplex(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700259{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700260 /* discard old RX data and clear RXS */
261 bfin_spi_dummy_read(drv_data);
262
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700263 while (drv_data->rx < drv_data->rx_end) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400264 bfin_write(&drv_data->regs->tdbr, (*(u8 *) (drv_data->tx++)));
265 while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800266 cpu_relax();
Mike Frysinger47885ce2011-06-17 04:16:56 -0400267 *(u8 *) (drv_data->rx++) = bfin_read(&drv_data->regs->rdbr);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700268 }
269}
270
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400271static const struct bfin_spi_transfer_ops bfin_bfin_spi_transfer_ops_u8 = {
Mike Frysinger9c4542c2009-09-24 01:04:04 +0000272 .write = bfin_spi_u8_writer,
273 .read = bfin_spi_u8_reader,
274 .duplex = bfin_spi_u8_duplex,
275};
276
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400277static void bfin_spi_u16_writer(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700278{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700279 /* clear RXS (we check for RXS inside the loop) */
280 bfin_spi_dummy_read(drv_data);
Bryan Wu88b40362007-05-21 18:32:16 +0800281
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700282 while (drv_data->tx < drv_data->tx_end) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400283 bfin_write(&drv_data->regs->tdbr, (*(u16 *) (drv_data->tx)));
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700284 drv_data->tx += 2;
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700285 /* wait until transfer finished.
286 checking SPIF or TXS may not guarantee transfer completion */
Mike Frysinger47885ce2011-06-17 04:16:56 -0400287 while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_RXS))
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700288 cpu_relax();
289 /* discard RX data and clear RXS */
290 bfin_spi_dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700291 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700292}
293
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400294static void bfin_spi_u16_reader(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700295{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700296 u16 tx_val = drv_data->cur_chip->idle_tx_val;
Sonic Zhangcc487e72007-12-04 23:45:17 -0800297
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700298 /* discard old RX data and clear RXS */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700299 bfin_spi_dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700300
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700301 while (drv_data->rx < drv_data->rx_end) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400302 bfin_write(&drv_data->regs->tdbr, tx_val);
303 while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800304 cpu_relax();
Mike Frysinger47885ce2011-06-17 04:16:56 -0400305 *(u16 *) (drv_data->rx) = bfin_read(&drv_data->regs->rdbr);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700306 drv_data->rx += 2;
307 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700308}
309
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400310static void bfin_spi_u16_duplex(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700311{
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700312 /* discard old RX data and clear RXS */
313 bfin_spi_dummy_read(drv_data);
314
315 while (drv_data->rx < drv_data->rx_end) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400316 bfin_write(&drv_data->regs->tdbr, (*(u16 *) (drv_data->tx)));
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700317 drv_data->tx += 2;
Mike Frysinger47885ce2011-06-17 04:16:56 -0400318 while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800319 cpu_relax();
Mike Frysinger47885ce2011-06-17 04:16:56 -0400320 *(u16 *) (drv_data->rx) = bfin_read(&drv_data->regs->rdbr);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700321 drv_data->rx += 2;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700322 }
323}
324
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400325static const struct bfin_spi_transfer_ops bfin_bfin_spi_transfer_ops_u16 = {
Mike Frysinger9c4542c2009-09-24 01:04:04 +0000326 .write = bfin_spi_u16_writer,
327 .read = bfin_spi_u16_reader,
328 .duplex = bfin_spi_u16_duplex,
329};
330
Rob Marise3595402010-04-06 04:12:00 +0000331/* test if there is more transfer to be done */
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400332static void *bfin_spi_next_transfer(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700333{
334 struct spi_message *msg = drv_data->cur_msg;
335 struct spi_transfer *trans = drv_data->cur_transfer;
336
337 /* Move to next transfer */
338 if (trans->transfer_list.next != &msg->transfers) {
339 drv_data->cur_transfer =
340 list_entry(trans->transfer_list.next,
341 struct spi_transfer, transfer_list);
342 return RUNNING_STATE;
343 } else
344 return DONE_STATE;
345}
346
347/*
348 * caller already set message->status;
349 * dma and pio irqs are blocked give finished message back
350 */
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400351static void bfin_spi_giveback(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700352{
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400353 struct bfin_spi_slave_data *chip = drv_data->cur_chip;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700354 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
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700365 msg->state = NULL;
366
Bryan Wufad91c82007-12-04 23:45:14 -0800367 if (!drv_data->cs_change)
Mike Frysinger138f97c2009-04-06 19:00:50 -0700368 bfin_spi_cs_deactive(drv_data, chip);
Bryan Wufad91c82007-12-04 23:45:14 -0800369
Yi Lib9b2a762009-04-06 19:00:49 -0700370 /* Not stop spi in autobuffer mode */
371 if (drv_data->tx_dma != 0xFFFF)
372 bfin_spi_disable(drv_data);
373
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700374 if (msg->complete)
375 msg->complete(msg->context);
376}
377
Yi Lif6a6d962009-06-03 09:46:22 +0000378/* spi data irq handler */
379static irqreturn_t bfin_spi_pio_irq_handler(int irq, void *dev_id)
380{
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400381 struct bfin_spi_master_data *drv_data = dev_id;
382 struct bfin_spi_slave_data *chip = drv_data->cur_chip;
Yi Lif6a6d962009-06-03 09:46:22 +0000383 struct spi_message *msg = drv_data->cur_msg;
384 int n_bytes = drv_data->n_bytes;
Bob Liu4d676fc2011-01-11 11:19:07 -0500385 int loop = 0;
Yi Lif6a6d962009-06-03 09:46:22 +0000386
387 /* wait until transfer finished. */
Mike Frysinger47885ce2011-06-17 04:16:56 -0400388 while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_RXS))
Yi Lif6a6d962009-06-03 09:46:22 +0000389 cpu_relax();
390
391 if ((drv_data->tx && drv_data->tx >= drv_data->tx_end) ||
392 (drv_data->rx && drv_data->rx >= (drv_data->rx_end - n_bytes))) {
393 /* last read */
394 if (drv_data->rx) {
395 dev_dbg(&drv_data->pdev->dev, "last read\n");
Scott Jiang128465c2012-04-23 18:18:12 -0400396 if (!(n_bytes % 2)) {
Bob Liu4d676fc2011-01-11 11:19:07 -0500397 u16 *buf = (u16 *)drv_data->rx;
398 for (loop = 0; loop < n_bytes / 2; loop++)
Mike Frysinger47885ce2011-06-17 04:16:56 -0400399 *buf++ = bfin_read(&drv_data->regs->rdbr);
Bob Liu4d676fc2011-01-11 11:19:07 -0500400 } else {
401 u8 *buf = (u8 *)drv_data->rx;
402 for (loop = 0; loop < n_bytes; loop++)
Mike Frysinger47885ce2011-06-17 04:16:56 -0400403 *buf++ = bfin_read(&drv_data->regs->rdbr);
Bob Liu4d676fc2011-01-11 11:19:07 -0500404 }
Yi Lif6a6d962009-06-03 09:46:22 +0000405 drv_data->rx += n_bytes;
406 }
407
408 msg->actual_length += drv_data->len_in_bytes;
409 if (drv_data->cs_change)
410 bfin_spi_cs_deactive(drv_data, chip);
411 /* Move to next transfer */
412 msg->state = bfin_spi_next_transfer(drv_data);
413
Yi Li7370ed62009-12-07 08:07:01 +0000414 disable_irq_nosync(drv_data->spi_irq);
Yi Lif6a6d962009-06-03 09:46:22 +0000415
416 /* Schedule transfer tasklet */
417 tasklet_schedule(&drv_data->pump_transfers);
418 return IRQ_HANDLED;
419 }
420
421 if (drv_data->rx && drv_data->tx) {
422 /* duplex */
423 dev_dbg(&drv_data->pdev->dev, "duplex: write_TDBR\n");
Scott Jiang128465c2012-04-23 18:18:12 -0400424 if (!(n_bytes % 2)) {
Bob Liu4d676fc2011-01-11 11:19:07 -0500425 u16 *buf = (u16 *)drv_data->rx;
426 u16 *buf2 = (u16 *)drv_data->tx;
427 for (loop = 0; loop < n_bytes / 2; loop++) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400428 *buf++ = bfin_read(&drv_data->regs->rdbr);
429 bfin_write(&drv_data->regs->tdbr, *buf2++);
Bob Liu4d676fc2011-01-11 11:19:07 -0500430 }
431 } else {
432 u8 *buf = (u8 *)drv_data->rx;
433 u8 *buf2 = (u8 *)drv_data->tx;
434 for (loop = 0; loop < n_bytes; loop++) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400435 *buf++ = bfin_read(&drv_data->regs->rdbr);
436 bfin_write(&drv_data->regs->tdbr, *buf2++);
Bob Liu4d676fc2011-01-11 11:19:07 -0500437 }
Yi Lif6a6d962009-06-03 09:46:22 +0000438 }
439 } else if (drv_data->rx) {
440 /* read */
441 dev_dbg(&drv_data->pdev->dev, "read: write_TDBR\n");
Scott Jiang128465c2012-04-23 18:18:12 -0400442 if (!(n_bytes % 2)) {
Bob Liu4d676fc2011-01-11 11:19:07 -0500443 u16 *buf = (u16 *)drv_data->rx;
444 for (loop = 0; loop < n_bytes / 2; loop++) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400445 *buf++ = bfin_read(&drv_data->regs->rdbr);
446 bfin_write(&drv_data->regs->tdbr, chip->idle_tx_val);
Bob Liu4d676fc2011-01-11 11:19:07 -0500447 }
448 } else {
449 u8 *buf = (u8 *)drv_data->rx;
450 for (loop = 0; loop < n_bytes; loop++) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400451 *buf++ = bfin_read(&drv_data->regs->rdbr);
452 bfin_write(&drv_data->regs->tdbr, chip->idle_tx_val);
Bob Liu4d676fc2011-01-11 11:19:07 -0500453 }
454 }
Yi Lif6a6d962009-06-03 09:46:22 +0000455 } else if (drv_data->tx) {
456 /* write */
457 dev_dbg(&drv_data->pdev->dev, "write: write_TDBR\n");
Scott Jiang128465c2012-04-23 18:18:12 -0400458 if (!(n_bytes % 2)) {
Bob Liu4d676fc2011-01-11 11:19:07 -0500459 u16 *buf = (u16 *)drv_data->tx;
460 for (loop = 0; loop < n_bytes / 2; loop++) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400461 bfin_read(&drv_data->regs->rdbr);
462 bfin_write(&drv_data->regs->tdbr, *buf++);
Bob Liu4d676fc2011-01-11 11:19:07 -0500463 }
464 } else {
465 u8 *buf = (u8 *)drv_data->tx;
466 for (loop = 0; loop < n_bytes; loop++) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400467 bfin_read(&drv_data->regs->rdbr);
468 bfin_write(&drv_data->regs->tdbr, *buf++);
Bob Liu4d676fc2011-01-11 11:19:07 -0500469 }
470 }
Yi Lif6a6d962009-06-03 09:46:22 +0000471 }
472
473 if (drv_data->tx)
474 drv_data->tx += n_bytes;
475 if (drv_data->rx)
476 drv_data->rx += n_bytes;
477
478 return IRQ_HANDLED;
479}
480
Mike Frysinger138f97c2009-04-06 19:00:50 -0700481static irqreturn_t bfin_spi_dma_irq_handler(int irq, void *dev_id)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700482{
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400483 struct bfin_spi_master_data *drv_data = dev_id;
484 struct bfin_spi_slave_data *chip = drv_data->cur_chip;
Bryan Wubb90eb02007-12-04 23:45:18 -0800485 struct spi_message *msg = drv_data->cur_msg;
Mike Frysingeraaaf9392009-04-06 19:00:42 -0700486 unsigned long timeout;
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700487 unsigned short dmastat = get_dma_curr_irqstat(drv_data->dma_channel);
Mike Frysinger47885ce2011-06-17 04:16:56 -0400488 u16 spistat = bfin_read(&drv_data->regs->stat);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700489
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700490 dev_dbg(&drv_data->pdev->dev,
491 "in dma_irq_handler dmastat:0x%x spistat:0x%x\n",
492 dmastat, spistat);
493
Michael Hennerich782a8952010-10-22 02:01:48 -0400494 if (drv_data->rx != NULL) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400495 u16 cr = bfin_read(&drv_data->regs->ctl);
Michael Hennerich782a8952010-10-22 02:01:48 -0400496 /* discard old RX data and clear RXS */
497 bfin_spi_dummy_read(drv_data);
Mike Frysinger47885ce2011-06-17 04:16:56 -0400498 bfin_write(&drv_data->regs->ctl, cr & ~BIT_CTL_ENABLE); /* Disable SPI */
499 bfin_write(&drv_data->regs->ctl, cr & ~BIT_CTL_TIMOD); /* Restore State */
500 bfin_write(&drv_data->regs->stat, BIT_STAT_CLR); /* Clear Status */
Michael Hennerich782a8952010-10-22 02:01:48 -0400501 }
502
Bryan Wubb90eb02007-12-04 23:45:18 -0800503 clear_dma_irqstat(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700504
505 /*
Bryan Wud6fe89b2007-06-11 17:34:17 +0800506 * wait for the last transaction shifted out. HRM states:
507 * at this point there may still be data in the SPI DMA FIFO waiting
508 * to be transmitted ... software needs to poll TXS in the SPI_STAT
509 * register until it goes low for 2 successive reads
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700510 */
511 if (drv_data->tx != NULL) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400512 while ((bfin_read(&drv_data->regs->stat) & BIT_STAT_TXS) ||
513 (bfin_read(&drv_data->regs->stat) & BIT_STAT_TXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800514 cpu_relax();
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700515 }
516
Mike Frysingeraaaf9392009-04-06 19:00:42 -0700517 dev_dbg(&drv_data->pdev->dev,
518 "in dma_irq_handler dmastat:0x%x spistat:0x%x\n",
Mike Frysinger47885ce2011-06-17 04:16:56 -0400519 dmastat, bfin_read(&drv_data->regs->stat));
Mike Frysingeraaaf9392009-04-06 19:00:42 -0700520
521 timeout = jiffies + HZ;
Mike Frysinger47885ce2011-06-17 04:16:56 -0400522 while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_SPIF))
Mike Frysingeraaaf9392009-04-06 19:00:42 -0700523 if (!time_before(jiffies, timeout)) {
Jarkko Nikulaa1829d22013-10-11 13:53:59 +0300524 dev_warn(&drv_data->pdev->dev, "timeout waiting for SPIF\n");
Mike Frysingeraaaf9392009-04-06 19:00:42 -0700525 break;
526 } else
527 cpu_relax();
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700528
Mike Frysinger90008a62009-10-15 04:13:29 +0000529 if ((dmastat & DMA_ERR) && (spistat & BIT_STAT_RBSY)) {
Mike Frysinger04b95d22009-04-06 19:00:35 -0700530 msg->state = ERROR_STATE;
531 dev_err(&drv_data->pdev->dev, "dma receive: fifo/buffer overflow\n");
532 } else {
533 msg->actual_length += drv_data->len_in_bytes;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700534
Mike Frysinger04b95d22009-04-06 19:00:35 -0700535 if (drv_data->cs_change)
Mike Frysinger138f97c2009-04-06 19:00:50 -0700536 bfin_spi_cs_deactive(drv_data, chip);
Bryan Wufad91c82007-12-04 23:45:14 -0800537
Mike Frysinger04b95d22009-04-06 19:00:35 -0700538 /* Move to next transfer */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700539 msg->state = bfin_spi_next_transfer(drv_data);
Mike Frysinger04b95d22009-04-06 19:00:35 -0700540 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700541
542 /* Schedule transfer tasklet */
543 tasklet_schedule(&drv_data->pump_transfers);
544
545 /* free the irq handler before next transfer */
Bryan Wu88b40362007-05-21 18:32:16 +0800546 dev_dbg(&drv_data->pdev->dev,
547 "disable dma channel irq%d\n",
Bryan Wubb90eb02007-12-04 23:45:18 -0800548 drv_data->dma_channel);
Barry Songa75bd65b2010-01-22 10:07:30 +0000549 dma_disable_irq_nosync(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700550
551 return IRQ_HANDLED;
552}
553
Mike Frysinger138f97c2009-04-06 19:00:50 -0700554static void bfin_spi_pump_transfers(unsigned long data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700555{
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400556 struct bfin_spi_master_data *drv_data = (struct bfin_spi_master_data *)data;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700557 struct spi_message *message = NULL;
558 struct spi_transfer *transfer = NULL;
559 struct spi_transfer *previous = NULL;
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400560 struct bfin_spi_slave_data *chip = NULL;
Mike Frysinger033f44b2009-12-18 17:38:04 +0000561 unsigned int bits_per_word;
Chen Gang057f6062015-04-02 03:03:33 +0800562 u16 cr, cr_width = 0, dma_width, dma_config;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700563 u32 tranf_success = 1;
Vitja Makarov8eeb12e2008-05-01 04:35:03 -0700564 u8 full_duplex = 0;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700565
566 /* Get current state information */
567 message = drv_data->cur_msg;
568 transfer = drv_data->cur_transfer;
569 chip = drv_data->cur_chip;
Bryan Wu092e1fd2007-12-04 23:45:23 -0800570
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700571 /*
572 * if msg is error or done, report it back using complete() callback
573 */
574
575 /* Handle for abort */
576 if (message->state == ERROR_STATE) {
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700577 dev_dbg(&drv_data->pdev->dev, "transfer: we've hit an error\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700578 message->status = -EIO;
Mike Frysinger138f97c2009-04-06 19:00:50 -0700579 bfin_spi_giveback(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700580 return;
581 }
582
583 /* Handle end of message */
584 if (message->state == DONE_STATE) {
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700585 dev_dbg(&drv_data->pdev->dev, "transfer: all done!\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700586 message->status = 0;
Scott Jiang2431a812012-04-23 18:18:13 -0400587 bfin_spi_flush(drv_data);
Mike Frysinger138f97c2009-04-06 19:00:50 -0700588 bfin_spi_giveback(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700589 return;
590 }
591
592 /* Delay if requested at end of transfer */
593 if (message->state == RUNNING_STATE) {
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700594 dev_dbg(&drv_data->pdev->dev, "transfer: still running ...\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700595 previous = list_entry(transfer->transfer_list.prev,
596 struct spi_transfer, transfer_list);
597 if (previous->delay_usecs)
598 udelay(previous->delay_usecs);
599 }
600
Mike Frysingerab09e042009-09-23 23:32:34 +0000601 /* Flush any existing transfers that may be sitting in the hardware */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700602 if (bfin_spi_flush(drv_data) == 0) {
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700603 dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
604 message->status = -EIO;
Mike Frysinger138f97c2009-04-06 19:00:50 -0700605 bfin_spi_giveback(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700606 return;
607 }
608
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700609 if (transfer->len == 0) {
610 /* Move to next transfer of this msg */
611 message->state = bfin_spi_next_transfer(drv_data);
612 /* Schedule next transfer tasklet */
613 tasklet_schedule(&drv_data->pump_transfers);
Sonic Zhang1974eba2011-01-11 11:19:08 -0500614 return;
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700615 }
616
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700617 if (transfer->tx_buf != NULL) {
618 drv_data->tx = (void *)transfer->tx_buf;
619 drv_data->tx_end = drv_data->tx + transfer->len;
Bryan Wu88b40362007-05-21 18:32:16 +0800620 dev_dbg(&drv_data->pdev->dev, "tx_buf is %p, tx_end is %p\n",
621 transfer->tx_buf, drv_data->tx_end);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700622 } else {
623 drv_data->tx = NULL;
624 }
625
626 if (transfer->rx_buf != NULL) {
Vitja Makarov8eeb12e2008-05-01 04:35:03 -0700627 full_duplex = transfer->tx_buf != NULL;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700628 drv_data->rx = transfer->rx_buf;
629 drv_data->rx_end = drv_data->rx + transfer->len;
Bryan Wu88b40362007-05-21 18:32:16 +0800630 dev_dbg(&drv_data->pdev->dev, "rx_buf is %p, rx_end is %p\n",
631 transfer->rx_buf, drv_data->rx_end);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700632 } else {
633 drv_data->rx = NULL;
634 }
635
636 drv_data->rx_dma = transfer->rx_dma;
637 drv_data->tx_dma = transfer->tx_dma;
638 drv_data->len_in_bytes = transfer->len;
Bryan Wufad91c82007-12-04 23:45:14 -0800639 drv_data->cs_change = transfer->cs_change;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700640
Bryan Wu092e1fd2007-12-04 23:45:23 -0800641 /* Bits per word setup */
Laxman Dewangan766ed702012-12-18 14:25:43 +0530642 bits_per_word = transfer->bits_per_word;
Stephen Warren24778be2013-05-21 20:36:35 -0600643 if (bits_per_word == 16) {
Bob Liu4d676fc2011-01-11 11:19:07 -0500644 drv_data->n_bytes = bits_per_word/8;
Mike Frysinger5e8592d2009-12-18 18:00:10 +0000645 drv_data->len = (transfer->len) >> 1;
646 cr_width = BIT_CTL_WORDSIZE;
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400647 drv_data->ops = &bfin_bfin_spi_transfer_ops_u16;
Stephen Warren24778be2013-05-21 20:36:35 -0600648 } else if (bits_per_word == 8) {
Bob Liu4d676fc2011-01-11 11:19:07 -0500649 drv_data->n_bytes = bits_per_word/8;
650 drv_data->len = transfer->len;
Bob Liu4d676fc2011-01-11 11:19:07 -0500651 drv_data->ops = &bfin_bfin_spi_transfer_ops_u8;
Bryan Wu092e1fd2007-12-04 23:45:23 -0800652 }
Mike Frysinger47885ce2011-06-17 04:16:56 -0400653 cr = bfin_read(&drv_data->regs->ctl) & ~(BIT_CTL_TIMOD | BIT_CTL_WORDSIZE);
Mike Frysinger5e8592d2009-12-18 18:00:10 +0000654 cr |= cr_width;
Mike Frysinger47885ce2011-06-17 04:16:56 -0400655 bfin_write(&drv_data->regs->ctl, cr);
Bryan Wu092e1fd2007-12-04 23:45:23 -0800656
Mike Frysinger4fb98ef2008-04-08 17:41:57 -0700657 dev_dbg(&drv_data->pdev->dev,
Mike Frysinger9c4542c2009-09-24 01:04:04 +0000658 "transfer: drv_data->ops is %p, chip->ops is %p, u8_ops is %p\n",
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400659 drv_data->ops, chip->ops, &bfin_bfin_spi_transfer_ops_u8);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700660
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700661 message->state = RUNNING_STATE;
662 dma_config = 0;
663
Jarkko Nikula95a8fde2015-09-15 16:26:17 +0300664 bfin_write(&drv_data->regs->baud, hz_to_spi_baud(transfer->speed_hz));
Bryan Wu092e1fd2007-12-04 23:45:23 -0800665
Mike Frysinger47885ce2011-06-17 04:16:56 -0400666 bfin_write(&drv_data->regs->stat, BIT_STAT_CLR);
Rob Marise72dcde2010-04-06 04:17:08 +0000667 bfin_spi_cs_active(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700668
Bryan Wu88b40362007-05-21 18:32:16 +0800669 dev_dbg(&drv_data->pdev->dev,
670 "now pumping a transfer: width is %d, len is %d\n",
Mike Frysinger5e8592d2009-12-18 18:00:10 +0000671 cr_width, transfer->len);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700672
673 /*
Vitja Makarov8cf58582009-04-06 19:00:31 -0700674 * Try to map dma buffer and do a dma transfer. If successful use,
675 * different way to r/w according to the enable_dma settings and if
676 * we are not doing a full duplex transfer (since the hardware does
677 * not support full duplex DMA transfers).
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700678 */
Vitja Makarov8eeb12e2008-05-01 04:35:03 -0700679 if (!full_duplex && drv_data->cur_chip->enable_dma
680 && drv_data->len > 6) {
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700681
Mike Frysinger11d6f592009-04-06 19:00:41 -0700682 unsigned long dma_start_addr, flags;
Mike Frysinger7aec3562009-04-06 19:00:36 -0700683
Bryan Wubb90eb02007-12-04 23:45:18 -0800684 disable_dma(drv_data->dma_channel);
685 clear_dma_irqstat(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700686
687 /* config dma channel */
Bryan Wu88b40362007-05-21 18:32:16 +0800688 dev_dbg(&drv_data->pdev->dev, "doing dma transfer\n");
Mike Frysinger7aec3562009-04-06 19:00:36 -0700689 set_dma_x_count(drv_data->dma_channel, drv_data->len);
Mike Frysinger5e8592d2009-12-18 18:00:10 +0000690 if (cr_width == BIT_CTL_WORDSIZE) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800691 set_dma_x_modify(drv_data->dma_channel, 2);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700692 dma_width = WDSIZE_16;
693 } else {
Bryan Wubb90eb02007-12-04 23:45:18 -0800694 set_dma_x_modify(drv_data->dma_channel, 1);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700695 dma_width = WDSIZE_8;
696 }
697
Sonic Zhang3f479a62007-12-04 23:45:18 -0800698 /* poll for SPI completion before start */
Mike Frysinger47885ce2011-06-17 04:16:56 -0400699 while (!(bfin_read(&drv_data->regs->stat) & BIT_STAT_SPIF))
Bryan Wud8c05002007-12-04 23:45:21 -0800700 cpu_relax();
Sonic Zhang3f479a62007-12-04 23:45:18 -0800701
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700702 /* dirty hack for autobuffer DMA mode */
703 if (drv_data->tx_dma == 0xFFFF) {
Bryan Wu88b40362007-05-21 18:32:16 +0800704 dev_dbg(&drv_data->pdev->dev,
705 "doing autobuffer DMA out.\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700706
707 /* no irq in autobuffer mode */
708 dma_config =
709 (DMAFLOW_AUTO | RESTART | dma_width | DI_EN);
Bryan Wubb90eb02007-12-04 23:45:18 -0800710 set_dma_config(drv_data->dma_channel, dma_config);
711 set_dma_start_addr(drv_data->dma_channel,
Bryan Wua32c6912007-12-04 23:45:15 -0800712 (unsigned long)drv_data->tx);
Bryan Wubb90eb02007-12-04 23:45:18 -0800713 enable_dma(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700714
Sonic Zhang07612e52007-12-04 23:45:21 -0800715 /* start SPI transfer */
Mike Frysinger47885ce2011-06-17 04:16:56 -0400716 bfin_write(&drv_data->regs->ctl, cr | BIT_CTL_TIMOD_DMA_TX);
Sonic Zhang07612e52007-12-04 23:45:21 -0800717
718 /* just return here, there can only be one transfer
719 * in this mode
720 */
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700721 message->status = 0;
Mike Frysinger138f97c2009-04-06 19:00:50 -0700722 bfin_spi_giveback(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700723 return;
724 }
725
726 /* In dma mode, rx or tx must be NULL in one transfer */
Mike Frysinger7aec3562009-04-06 19:00:36 -0700727 dma_config = (RESTART | dma_width | DI_EN);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700728 if (drv_data->rx != NULL) {
729 /* set transfer mode, and enable SPI */
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700730 dev_dbg(&drv_data->pdev->dev, "doing DMA in to %p (size %zx)\n",
731 drv_data->rx, drv_data->len_in_bytes);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700732
Vitja Makarov8cf58582009-04-06 19:00:31 -0700733 /* invalidate caches, if needed */
Jie Zhang67834fa2009-06-10 06:26:26 +0000734 if (bfin_addr_dcacheable((unsigned long) drv_data->rx))
Vitja Makarov8cf58582009-04-06 19:00:31 -0700735 invalidate_dcache_range((unsigned long) drv_data->rx,
736 (unsigned long) (drv_data->rx +
Mike Frysingerace32862009-04-06 19:00:34 -0700737 drv_data->len_in_bytes));
Vitja Makarov8cf58582009-04-06 19:00:31 -0700738
Mike Frysinger7aec3562009-04-06 19:00:36 -0700739 dma_config |= WNR;
740 dma_start_addr = (unsigned long)drv_data->rx;
Mike Frysingerb31e27a2009-04-06 19:00:39 -0700741 cr |= BIT_CTL_TIMOD_DMA_RX | BIT_CTL_SENDOPT;
Sonic Zhang07612e52007-12-04 23:45:21 -0800742
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700743 } else if (drv_data->tx != NULL) {
Bryan Wu88b40362007-05-21 18:32:16 +0800744 dev_dbg(&drv_data->pdev->dev, "doing DMA out.\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700745
Vitja Makarov8cf58582009-04-06 19:00:31 -0700746 /* flush caches, if needed */
Jie Zhang67834fa2009-06-10 06:26:26 +0000747 if (bfin_addr_dcacheable((unsigned long) drv_data->tx))
Vitja Makarov8cf58582009-04-06 19:00:31 -0700748 flush_dcache_range((unsigned long) drv_data->tx,
749 (unsigned long) (drv_data->tx +
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_start_addr = (unsigned long)drv_data->tx;
Mike Frysingerb31e27a2009-04-06 19:00:39 -0700753 cr |= BIT_CTL_TIMOD_DMA_TX;
Sonic Zhang07612e52007-12-04 23:45:21 -0800754
Mike Frysinger7aec3562009-04-06 19:00:36 -0700755 } else
756 BUG();
757
Mike Frysinger11d6f592009-04-06 19:00:41 -0700758 /* oh man, here there be monsters ... and i dont mean the
759 * fluffy cute ones from pixar, i mean the kind that'll eat
760 * your data, kick your dog, and love it all. do *not* try
761 * and change these lines unless you (1) heavily test DMA
762 * with SPI flashes on a loaded system (e.g. ping floods),
763 * (2) know just how broken the DMA engine interaction with
764 * the SPI peripheral is, and (3) have someone else to blame
765 * when you screw it all up anyways.
766 */
Mike Frysinger7aec3562009-04-06 19:00:36 -0700767 set_dma_start_addr(drv_data->dma_channel, dma_start_addr);
Mike Frysinger11d6f592009-04-06 19:00:41 -0700768 set_dma_config(drv_data->dma_channel, dma_config);
769 local_irq_save(flags);
Mike Frysingera963ea82009-04-06 19:00:43 -0700770 SSYNC();
Mike Frysinger47885ce2011-06-17 04:16:56 -0400771 bfin_write(&drv_data->regs->ctl, cr);
Mike Frysingera963ea82009-04-06 19:00:43 -0700772 enable_dma(drv_data->dma_channel);
Mike Frysinger11d6f592009-04-06 19:00:41 -0700773 dma_enable_irq(drv_data->dma_channel);
774 local_irq_restore(flags);
Mike Frysinger7aec3562009-04-06 19:00:36 -0700775
Yi Lif6a6d962009-06-03 09:46:22 +0000776 return;
777 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700778
Mike Frysinger5e8592d2009-12-18 18:00:10 +0000779 /*
780 * We always use SPI_WRITE mode (transfer starts with TDBR write).
781 * SPI_READ mode (transfer starts with RDBR read) seems to have
782 * problems with setting up the output value in TDBR prior to the
783 * start of the transfer.
784 */
Mike Frysinger47885ce2011-06-17 04:16:56 -0400785 bfin_write(&drv_data->regs->ctl, cr | BIT_CTL_TXMOD);
Mike Frysinger5e8592d2009-12-18 18:00:10 +0000786
Yi Lif6a6d962009-06-03 09:46:22 +0000787 if (chip->pio_interrupt) {
Mike Frysinger5e8592d2009-12-18 18:00:10 +0000788 /* SPI irq should have been disabled by now */
Wolfgang Muees93b61bd2009-04-06 19:00:53 -0700789
Yi Lif6a6d962009-06-03 09:46:22 +0000790 /* discard old RX data and clear RXS */
791 bfin_spi_dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700792
Yi Lif6a6d962009-06-03 09:46:22 +0000793 /* start transfer */
794 if (drv_data->tx == NULL)
Mike Frysinger47885ce2011-06-17 04:16:56 -0400795 bfin_write(&drv_data->regs->tdbr, chip->idle_tx_val);
Yi Lif6a6d962009-06-03 09:46:22 +0000796 else {
Bob Liu4d676fc2011-01-11 11:19:07 -0500797 int loop;
Stephen Warren24778be2013-05-21 20:36:35 -0600798 if (bits_per_word == 16) {
Bob Liu4d676fc2011-01-11 11:19:07 -0500799 u16 *buf = (u16 *)drv_data->tx;
800 for (loop = 0; loop < bits_per_word / 16;
801 loop++) {
Mike Frysinger47885ce2011-06-17 04:16:56 -0400802 bfin_write(&drv_data->regs->tdbr, *buf++);
Bob Liu4d676fc2011-01-11 11:19:07 -0500803 }
Stephen Warren24778be2013-05-21 20:36:35 -0600804 } else if (bits_per_word == 8) {
Bob Liu4d676fc2011-01-11 11:19:07 -0500805 u8 *buf = (u8 *)drv_data->tx;
806 for (loop = 0; loop < bits_per_word / 8; loop++)
Mike Frysinger47885ce2011-06-17 04:16:56 -0400807 bfin_write(&drv_data->regs->tdbr, *buf++);
Bob Liu4d676fc2011-01-11 11:19:07 -0500808 }
809
Yi Lif6a6d962009-06-03 09:46:22 +0000810 drv_data->tx += drv_data->n_bytes;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700811 }
812
Yi Lif6a6d962009-06-03 09:46:22 +0000813 /* once TDBR is empty, interrupt is triggered */
814 enable_irq(drv_data->spi_irq);
815 return;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700816 }
Yi Lif6a6d962009-06-03 09:46:22 +0000817
818 /* IO mode */
819 dev_dbg(&drv_data->pdev->dev, "doing IO transfer\n");
820
Yi Lif6a6d962009-06-03 09:46:22 +0000821 if (full_duplex) {
822 /* full duplex mode */
823 BUG_ON((drv_data->tx_end - drv_data->tx) !=
824 (drv_data->rx_end - drv_data->rx));
825 dev_dbg(&drv_data->pdev->dev,
826 "IO duplex: cr is 0x%x\n", cr);
827
Mike Frysinger9c4542c2009-09-24 01:04:04 +0000828 drv_data->ops->duplex(drv_data);
Yi Lif6a6d962009-06-03 09:46:22 +0000829
830 if (drv_data->tx != drv_data->tx_end)
831 tranf_success = 0;
832 } else if (drv_data->tx != NULL) {
833 /* write only half duplex */
834 dev_dbg(&drv_data->pdev->dev,
835 "IO write: cr is 0x%x\n", cr);
836
Mike Frysinger9c4542c2009-09-24 01:04:04 +0000837 drv_data->ops->write(drv_data);
Yi Lif6a6d962009-06-03 09:46:22 +0000838
839 if (drv_data->tx != drv_data->tx_end)
840 tranf_success = 0;
841 } else if (drv_data->rx != NULL) {
842 /* read only half duplex */
843 dev_dbg(&drv_data->pdev->dev,
844 "IO read: cr is 0x%x\n", cr);
845
Mike Frysinger9c4542c2009-09-24 01:04:04 +0000846 drv_data->ops->read(drv_data);
Yi Lif6a6d962009-06-03 09:46:22 +0000847 if (drv_data->rx != drv_data->rx_end)
848 tranf_success = 0;
849 }
850
851 if (!tranf_success) {
852 dev_dbg(&drv_data->pdev->dev,
853 "IO write error!\n");
854 message->state = ERROR_STATE;
855 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300856 /* Update total byte transferred */
Yi Lif6a6d962009-06-03 09:46:22 +0000857 message->actual_length += drv_data->len_in_bytes;
858 /* Move to next transfer of this msg */
859 message->state = bfin_spi_next_transfer(drv_data);
Scott Jiang2431a812012-04-23 18:18:13 -0400860 if (drv_data->cs_change && message->state != DONE_STATE) {
861 bfin_spi_flush(drv_data);
Yi Lif6a6d962009-06-03 09:46:22 +0000862 bfin_spi_cs_deactive(drv_data, chip);
Scott Jiang2431a812012-04-23 18:18:13 -0400863 }
Yi Lif6a6d962009-06-03 09:46:22 +0000864 }
865
866 /* Schedule next transfer tasklet */
867 tasklet_schedule(&drv_data->pump_transfers);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700868}
869
870/* pop a msg from queue and kick off real transfer */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700871static void bfin_spi_pump_messages(struct work_struct *work)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700872{
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400873 struct bfin_spi_master_data *drv_data;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700874 unsigned long flags;
875
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400876 drv_data = container_of(work, struct bfin_spi_master_data, pump_messages);
Bryan Wu131b17d2007-12-04 23:45:12 -0800877
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700878 /* Lock queue and check for queue work */
879 spin_lock_irqsave(&drv_data->lock, flags);
Mike Frysingerf4f50c32009-09-24 00:41:49 +0000880 if (list_empty(&drv_data->queue) || !drv_data->running) {
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700881 /* pumper kicked off but no work to do */
882 drv_data->busy = 0;
883 spin_unlock_irqrestore(&drv_data->lock, flags);
884 return;
885 }
886
887 /* Make sure we are not already running a message */
888 if (drv_data->cur_msg) {
889 spin_unlock_irqrestore(&drv_data->lock, flags);
890 return;
891 }
892
893 /* Extract head of queue */
894 drv_data->cur_msg = list_entry(drv_data->queue.next,
895 struct spi_message, queue);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800896
897 /* Setup the SSP using the per chip configuration */
898 drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
Mike Frysinger138f97c2009-04-06 19:00:50 -0700899 bfin_spi_restore_state(drv_data);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800900
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700901 list_del_init(&drv_data->cur_msg->queue);
902
903 /* Initial message state */
904 drv_data->cur_msg->state = START_STATE;
905 drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
906 struct spi_transfer, transfer_list);
907
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +0300908 dev_dbg(&drv_data->pdev->dev,
909 "got a message to pump, state is set to: baud "
910 "%d, flag 0x%x, ctl 0x%x\n",
Bryan Wu5fec5b52007-12-04 23:45:13 -0800911 drv_data->cur_chip->baud, drv_data->cur_chip->flag,
912 drv_data->cur_chip->ctl_reg);
Bryan Wu131b17d2007-12-04 23:45:12 -0800913
914 dev_dbg(&drv_data->pdev->dev,
Bryan Wu88b40362007-05-21 18:32:16 +0800915 "the first transfer len is %d\n",
916 drv_data->cur_transfer->len);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700917
918 /* Mark as busy and launch transfers */
919 tasklet_schedule(&drv_data->pump_transfers);
920
921 drv_data->busy = 1;
922 spin_unlock_irqrestore(&drv_data->lock, flags);
923}
924
925/*
926 * got a msg to transfer, queue it in drv_data->queue.
927 * And kick off message pumper
928 */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700929static int bfin_spi_transfer(struct spi_device *spi, struct spi_message *msg)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700930{
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400931 struct bfin_spi_master_data *drv_data = spi_master_get_devdata(spi->master);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700932 unsigned long flags;
933
934 spin_lock_irqsave(&drv_data->lock, flags);
935
Mike Frysingerf4f50c32009-09-24 00:41:49 +0000936 if (!drv_data->running) {
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700937 spin_unlock_irqrestore(&drv_data->lock, flags);
938 return -ESHUTDOWN;
939 }
940
941 msg->actual_length = 0;
942 msg->status = -EINPROGRESS;
943 msg->state = START_STATE;
944
Bryan Wu88b40362007-05-21 18:32:16 +0800945 dev_dbg(&spi->dev, "adding an msg in transfer() \n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700946 list_add_tail(&msg->queue, &drv_data->queue);
947
Mike Frysingerf4f50c32009-09-24 00:41:49 +0000948 if (drv_data->running && !drv_data->busy)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700949 queue_work(drv_data->workqueue, &drv_data->pump_messages);
950
951 spin_unlock_irqrestore(&drv_data->lock, flags);
952
953 return 0;
954}
955
Sonic Zhang12e17c42007-12-04 23:45:16 -0800956#define MAX_SPI_SSEL 7
957
Mike Frysingerddc0bf12011-06-17 04:16:57 -0400958static const u16 ssel[][MAX_SPI_SSEL] = {
Sonic Zhang12e17c42007-12-04 23:45:16 -0800959 {P_SPI0_SSEL1, P_SPI0_SSEL2, P_SPI0_SSEL3,
960 P_SPI0_SSEL4, P_SPI0_SSEL5,
961 P_SPI0_SSEL6, P_SPI0_SSEL7},
962
963 {P_SPI1_SSEL1, P_SPI1_SSEL2, P_SPI1_SSEL3,
964 P_SPI1_SSEL4, P_SPI1_SSEL5,
965 P_SPI1_SSEL6, P_SPI1_SSEL7},
966
967 {P_SPI2_SSEL1, P_SPI2_SSEL2, P_SPI2_SSEL3,
968 P_SPI2_SSEL4, P_SPI2_SSEL5,
969 P_SPI2_SSEL6, P_SPI2_SSEL7},
970};
971
Mike Frysingerab09e042009-09-23 23:32:34 +0000972/* setup for devices (may be called multiple times -- not just first setup) */
Mike Frysinger138f97c2009-04-06 19:00:50 -0700973static int bfin_spi_setup(struct spi_device *spi)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700974{
Daniel Mackac01e972009-03-25 00:18:35 +0000975 struct bfin5xx_spi_chip *chip_info;
Mike Frysinger9c0a7882010-10-18 02:45:22 -0400976 struct bfin_spi_slave_data *chip = NULL;
977 struct bfin_spi_master_data *drv_data = spi_master_get_devdata(spi->master);
Mike Frysinger5b47bcd2009-12-18 17:43:31 +0000978 u16 bfin_ctl_reg;
Daniel Mackac01e972009-03-25 00:18:35 +0000979 int ret = -EINVAL;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700980
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700981 /* Only alloc (or use chip_info) on first setup */
Daniel Mackac01e972009-03-25 00:18:35 +0000982 chip_info = NULL;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700983 chip = spi_get_ctldata(spi);
984 if (chip == NULL) {
Daniel Mackac01e972009-03-25 00:18:35 +0000985 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
986 if (!chip) {
987 dev_err(&spi->dev, "cannot allocate chip data\n");
988 ret = -ENOMEM;
989 goto error;
990 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700991
992 chip->enable_dma = 0;
993 chip_info = spi->controller_data;
994 }
995
Mike Frysinger5b47bcd2009-12-18 17:43:31 +0000996 /* Let people set non-standard bits directly */
997 bfin_ctl_reg = BIT_CTL_OPENDRAIN | BIT_CTL_EMISO |
998 BIT_CTL_PSSE | BIT_CTL_GM | BIT_CTL_SZ;
999
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001000 /* chip_info isn't always needed */
1001 if (chip_info) {
Mike Frysinger2ed35512007-12-04 23:45:14 -08001002 /* Make sure people stop trying to set fields via ctl_reg
1003 * when they should actually be using common SPI framework.
Mike Frysinger90008a62009-10-15 04:13:29 +00001004 * Currently we let through: WOM EMISO PSSE GM SZ.
Mike Frysinger2ed35512007-12-04 23:45:14 -08001005 * Not sure if a user actually needs/uses any of these,
1006 * but let's assume (for now) they do.
1007 */
Mike Frysinger5b47bcd2009-12-18 17:43:31 +00001008 if (chip_info->ctl_reg & ~bfin_ctl_reg) {
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +03001009 dev_err(&spi->dev,
1010 "do not set bits in ctl_reg that the SPI framework manages\n");
Daniel Mackac01e972009-03-25 00:18:35 +00001011 goto error;
Mike Frysinger2ed35512007-12-04 23:45:14 -08001012 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001013 chip->enable_dma = chip_info->enable_dma != 0
1014 && drv_data->master_info->enable_dma;
1015 chip->ctl_reg = chip_info->ctl_reg;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001016 chip->cs_chg_udelay = chip_info->cs_chg_udelay;
Wolfgang Muees93b61bd2009-04-06 19:00:53 -07001017 chip->idle_tx_val = chip_info->idle_tx_val;
Yi Lif6a6d962009-06-03 09:46:22 +00001018 chip->pio_interrupt = chip_info->pio_interrupt;
Mike Frysinger5b47bcd2009-12-18 17:43:31 +00001019 } else {
1020 /* force a default base state */
1021 chip->ctl_reg &= bfin_ctl_reg;
Mike Frysinger033f44b2009-12-18 17:38:04 +00001022 }
1023
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001024 /* translate common spi framework into our register */
1025 if (spi->mode & SPI_CPOL)
Mike Frysinger90008a62009-10-15 04:13:29 +00001026 chip->ctl_reg |= BIT_CTL_CPOL;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001027 if (spi->mode & SPI_CPHA)
Mike Frysinger90008a62009-10-15 04:13:29 +00001028 chip->ctl_reg |= BIT_CTL_CPHA;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001029 if (spi->mode & SPI_LSB_FIRST)
Mike Frysinger90008a62009-10-15 04:13:29 +00001030 chip->ctl_reg |= BIT_CTL_LSBF;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001031 /* we dont support running in slave mode (yet?) */
Mike Frysinger90008a62009-10-15 04:13:29 +00001032 chip->ctl_reg |= BIT_CTL_MASTER;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001033
1034 /*
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001035 * Notice: for blackfin, the speed_hz is the value of register
1036 * SPI_BAUD, not the real baudrate
1037 */
1038 chip->baud = hz_to_spi_baud(spi->max_speed_hz);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001039 chip->chip_select_num = spi->chip_select;
Barry Song4190f6a2010-04-06 03:36:24 +00001040 if (chip->chip_select_num < MAX_CTRL_CS) {
1041 if (!(spi->mode & SPI_CPHA))
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +03001042 dev_warn(&spi->dev,
1043 "Warning: SPI CPHA not set: Slave Select not under software control!\n"
1044 "See Documentation/blackfin/bfin-spi-notes.txt\n");
Barry Song4190f6a2010-04-06 03:36:24 +00001045
Barry Songd3cc71f2009-11-17 09:45:59 +00001046 chip->flag = (1 << spi->chip_select) << 8;
Barry Song4190f6a2010-04-06 03:36:24 +00001047 } else
Barry Songd3cc71f2009-11-17 09:45:59 +00001048 chip->cs_gpio = chip->chip_select_num - MAX_CTRL_CS;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001049
Yi Lif6a6d962009-06-03 09:46:22 +00001050 if (chip->enable_dma && chip->pio_interrupt) {
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +03001051 dev_err(&spi->dev,
1052 "enable_dma is set, do not set pio_interrupt\n");
Yi Lif6a6d962009-06-03 09:46:22 +00001053 goto error;
1054 }
Daniel Mackac01e972009-03-25 00:18:35 +00001055 /*
1056 * if any one SPI chip is registered and wants DMA, request the
1057 * DMA channel for it
1058 */
1059 if (chip->enable_dma && !drv_data->dma_requested) {
1060 /* register dma irq handler */
1061 ret = request_dma(drv_data->dma_channel, "BFIN_SPI_DMA");
1062 if (ret) {
1063 dev_err(&spi->dev,
1064 "Unable to request BlackFin SPI DMA channel\n");
1065 goto error;
1066 }
1067 drv_data->dma_requested = 1;
1068
1069 ret = set_dma_callback(drv_data->dma_channel,
1070 bfin_spi_dma_irq_handler, drv_data);
1071 if (ret) {
1072 dev_err(&spi->dev, "Unable to set dma callback\n");
1073 goto error;
1074 }
1075 dma_disable_irq(drv_data->dma_channel);
1076 }
1077
Yi Lif6a6d962009-06-03 09:46:22 +00001078 if (chip->pio_interrupt && !drv_data->irq_requested) {
1079 ret = request_irq(drv_data->spi_irq, bfin_spi_pio_irq_handler,
Yong Zhang38ada212011-10-22 17:56:55 +08001080 0, "BFIN_SPI", drv_data);
Yi Lif6a6d962009-06-03 09:46:22 +00001081 if (ret) {
1082 dev_err(&spi->dev, "Unable to register spi IRQ\n");
1083 goto error;
1084 }
1085 drv_data->irq_requested = 1;
1086 /* we use write mode, spi irq has to be disabled here */
1087 disable_irq(drv_data->spi_irq);
1088 }
1089
Barry Songd3cc71f2009-11-17 09:45:59 +00001090 if (chip->chip_select_num >= MAX_CTRL_CS) {
Michael Hennerich73e1ac12010-10-22 02:01:47 -04001091 /* Only request on first setup */
1092 if (spi_get_ctldata(spi) == NULL) {
1093 ret = gpio_request(chip->cs_gpio, spi->modalias);
1094 if (ret) {
1095 dev_err(&spi->dev, "gpio_request() error\n");
1096 goto pin_error;
1097 }
1098 gpio_direction_output(chip->cs_gpio, 1);
Daniel Mackac01e972009-03-25 00:18:35 +00001099 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001100 }
1101
Joe Perches898eb712007-10-18 03:06:30 -07001102 dev_dbg(&spi->dev, "setup spi chip %s, width is %d, dma is %d\n",
Mike Frysinger033f44b2009-12-18 17:38:04 +00001103 spi->modalias, spi->bits_per_word, chip->enable_dma);
Bryan Wu88b40362007-05-21 18:32:16 +08001104 dev_dbg(&spi->dev, "ctl_reg is 0x%x, flag_reg is 0x%x\n",
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001105 chip->ctl_reg, chip->flag);
1106
1107 spi_set_ctldata(spi, chip);
1108
Sonic Zhang12e17c42007-12-04 23:45:16 -08001109 dev_dbg(&spi->dev, "chip select number is %d\n", chip->chip_select_num);
Barry Songd3cc71f2009-11-17 09:45:59 +00001110 if (chip->chip_select_num < MAX_CTRL_CS) {
Daniel Mackac01e972009-03-25 00:18:35 +00001111 ret = peripheral_request(ssel[spi->master->bus_num]
1112 [chip->chip_select_num-1], spi->modalias);
1113 if (ret) {
1114 dev_err(&spi->dev, "peripheral_request() error\n");
1115 goto pin_error;
1116 }
1117 }
Sonic Zhang12e17c42007-12-04 23:45:16 -08001118
Barry Song82216102009-06-17 10:10:53 +00001119 bfin_spi_cs_enable(drv_data, chip);
Mike Frysinger138f97c2009-04-06 19:00:50 -07001120 bfin_spi_cs_deactive(drv_data, chip);
Sonic Zhang07612e52007-12-04 23:45:21 -08001121
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001122 return 0;
Daniel Mackac01e972009-03-25 00:18:35 +00001123
1124 pin_error:
Barry Songd3cc71f2009-11-17 09:45:59 +00001125 if (chip->chip_select_num >= MAX_CTRL_CS)
Daniel Mackac01e972009-03-25 00:18:35 +00001126 gpio_free(chip->cs_gpio);
1127 else
1128 peripheral_free(ssel[spi->master->bus_num]
1129 [chip->chip_select_num - 1]);
1130 error:
1131 if (chip) {
1132 if (drv_data->dma_requested)
1133 free_dma(drv_data->dma_channel);
1134 drv_data->dma_requested = 0;
1135
1136 kfree(chip);
1137 /* prevent free 'chip' twice */
1138 spi_set_ctldata(spi, NULL);
1139 }
1140
1141 return ret;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001142}
1143
1144/*
1145 * callback for spi framework.
1146 * clean driver specific data
1147 */
Mike Frysinger138f97c2009-04-06 19:00:50 -07001148static void bfin_spi_cleanup(struct spi_device *spi)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001149{
Mike Frysinger9c0a7882010-10-18 02:45:22 -04001150 struct bfin_spi_slave_data *chip = spi_get_ctldata(spi);
1151 struct bfin_spi_master_data *drv_data = spi_master_get_devdata(spi->master);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001152
Mike Frysingere7d02e32009-04-06 19:00:51 -07001153 if (!chip)
1154 return;
1155
Barry Songd3cc71f2009-11-17 09:45:59 +00001156 if (chip->chip_select_num < MAX_CTRL_CS) {
Sonic Zhang12e17c42007-12-04 23:45:16 -08001157 peripheral_free(ssel[spi->master->bus_num]
1158 [chip->chip_select_num-1]);
Barry Song82216102009-06-17 10:10:53 +00001159 bfin_spi_cs_disable(drv_data, chip);
Barry Songd3cc71f2009-11-17 09:45:59 +00001160 } else
Michael Hennerich42c78b22009-04-06 19:00:51 -07001161 gpio_free(chip->cs_gpio);
1162
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001163 kfree(chip);
Daniel Mackac01e972009-03-25 00:18:35 +00001164 /* prevent free 'chip' twice */
1165 spi_set_ctldata(spi, NULL);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001166}
1167
Mike Frysingerc52d4e52011-06-17 04:16:58 -04001168static int bfin_spi_init_queue(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001169{
1170 INIT_LIST_HEAD(&drv_data->queue);
1171 spin_lock_init(&drv_data->lock);
1172
Mike Frysingerf4f50c32009-09-24 00:41:49 +00001173 drv_data->running = false;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001174 drv_data->busy = 0;
1175
1176 /* init transfer tasklet */
1177 tasklet_init(&drv_data->pump_transfers,
Mike Frysinger138f97c2009-04-06 19:00:50 -07001178 bfin_spi_pump_transfers, (unsigned long)drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001179
1180 /* init messages workqueue */
Mike Frysinger138f97c2009-04-06 19:00:50 -07001181 INIT_WORK(&drv_data->pump_messages, bfin_spi_pump_messages);
Kay Sievers6c7377a2009-03-24 16:38:21 -07001182 drv_data->workqueue = create_singlethread_workqueue(
1183 dev_name(drv_data->master->dev.parent));
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001184 if (drv_data->workqueue == NULL)
1185 return -EBUSY;
1186
1187 return 0;
1188}
1189
Mike Frysingerc52d4e52011-06-17 04:16:58 -04001190static int bfin_spi_start_queue(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001191{
1192 unsigned long flags;
1193
1194 spin_lock_irqsave(&drv_data->lock, flags);
1195
Mike Frysingerf4f50c32009-09-24 00:41:49 +00001196 if (drv_data->running || drv_data->busy) {
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001197 spin_unlock_irqrestore(&drv_data->lock, flags);
1198 return -EBUSY;
1199 }
1200
Mike Frysingerf4f50c32009-09-24 00:41:49 +00001201 drv_data->running = true;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001202 drv_data->cur_msg = NULL;
1203 drv_data->cur_transfer = NULL;
1204 drv_data->cur_chip = NULL;
1205 spin_unlock_irqrestore(&drv_data->lock, flags);
1206
1207 queue_work(drv_data->workqueue, &drv_data->pump_messages);
1208
1209 return 0;
1210}
1211
Mike Frysingerc52d4e52011-06-17 04:16:58 -04001212static int bfin_spi_stop_queue(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001213{
1214 unsigned long flags;
1215 unsigned limit = 500;
1216 int status = 0;
1217
1218 spin_lock_irqsave(&drv_data->lock, flags);
1219
1220 /*
1221 * This is a bit lame, but is optimized for the common execution path.
1222 * A wait_queue on the drv_data->busy could be used, but then the common
1223 * execution path (pump_messages) would be required to call wake_up or
1224 * friends on every SPI message. Do this instead
1225 */
Mike Frysingerf4f50c32009-09-24 00:41:49 +00001226 drv_data->running = false;
Vasily Khoruzhick850a28e2011-04-06 17:49:15 +03001227 while ((!list_empty(&drv_data->queue) || drv_data->busy) && limit--) {
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001228 spin_unlock_irqrestore(&drv_data->lock, flags);
1229 msleep(10);
1230 spin_lock_irqsave(&drv_data->lock, flags);
1231 }
1232
1233 if (!list_empty(&drv_data->queue) || drv_data->busy)
1234 status = -EBUSY;
1235
1236 spin_unlock_irqrestore(&drv_data->lock, flags);
1237
1238 return status;
1239}
1240
Mike Frysingerc52d4e52011-06-17 04:16:58 -04001241static int bfin_spi_destroy_queue(struct bfin_spi_master_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001242{
1243 int status;
1244
Mike Frysinger138f97c2009-04-06 19:00:50 -07001245 status = bfin_spi_stop_queue(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001246 if (status != 0)
1247 return status;
1248
1249 destroy_workqueue(drv_data->workqueue);
1250
1251 return 0;
1252}
1253
Grant Likely2deff8d2013-02-05 13:27:35 +00001254static int bfin_spi_probe(struct platform_device *pdev)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001255{
1256 struct device *dev = &pdev->dev;
1257 struct bfin5xx_spi_master *platform_info;
1258 struct spi_master *master;
Mike Frysinger9c0a7882010-10-18 02:45:22 -04001259 struct bfin_spi_master_data *drv_data;
Bryan Wua32c6912007-12-04 23:45:15 -08001260 struct resource *res;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001261 int status = 0;
1262
Jingoo Han8074cf02013-07-30 16:58:59 +09001263 platform_info = dev_get_platdata(dev);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001264
1265 /* Allocate master with space for drv_data */
Mike Frysinger2a045132009-09-24 01:28:54 +00001266 master = spi_alloc_master(dev, sizeof(*drv_data));
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001267 if (!master) {
1268 dev_err(&pdev->dev, "can not alloc spi_master\n");
1269 return -ENOMEM;
1270 }
Bryan Wu131b17d2007-12-04 23:45:12 -08001271
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001272 drv_data = spi_master_get_devdata(master);
1273 drv_data->master = master;
1274 drv_data->master_info = platform_info;
1275 drv_data->pdev = pdev;
Bryan Wu003d9222007-12-04 23:45:22 -08001276 drv_data->pin_req = platform_info->pin_req;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001277
David Brownelle7db06b2009-06-17 16:26:04 -07001278 /* the spi->mode bits supported by this driver: */
1279 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST;
Stephen Warren24778be2013-05-21 20:36:35 -06001280 master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001281 master->bus_num = pdev->id;
1282 master->num_chipselect = platform_info->num_chipselect;
Mike Frysinger138f97c2009-04-06 19:00:50 -07001283 master->cleanup = bfin_spi_cleanup;
1284 master->setup = bfin_spi_setup;
1285 master->transfer = bfin_spi_transfer;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001286
Bryan Wua32c6912007-12-04 23:45:15 -08001287 /* Find and map our resources */
1288 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1289 if (res == NULL) {
1290 dev_err(dev, "Cannot get IORESOURCE_MEM\n");
1291 status = -ENOENT;
1292 goto out_error_get_res;
1293 }
1294
Mike Frysinger47885ce2011-06-17 04:16:56 -04001295 drv_data->regs = ioremap(res->start, resource_size(res));
1296 if (drv_data->regs == NULL) {
Bryan Wua32c6912007-12-04 23:45:15 -08001297 dev_err(dev, "Cannot map IO\n");
1298 status = -ENXIO;
1299 goto out_error_ioremap;
1300 }
1301
Yi Lif6a6d962009-06-03 09:46:22 +00001302 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1303 if (res == NULL) {
Bryan Wua32c6912007-12-04 23:45:15 -08001304 dev_err(dev, "No DMA channel specified\n");
1305 status = -ENOENT;
Yi Lif6a6d962009-06-03 09:46:22 +00001306 goto out_error_free_io;
1307 }
1308 drv_data->dma_channel = res->start;
1309
1310 drv_data->spi_irq = platform_get_irq(pdev, 0);
1311 if (drv_data->spi_irq < 0) {
1312 dev_err(dev, "No spi pio irq specified\n");
1313 status = -ENOENT;
1314 goto out_error_free_io;
Bryan Wua32c6912007-12-04 23:45:15 -08001315 }
1316
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001317 /* Initial and start queue */
Mike Frysinger138f97c2009-04-06 19:00:50 -07001318 status = bfin_spi_init_queue(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001319 if (status != 0) {
Bryan Wua32c6912007-12-04 23:45:15 -08001320 dev_err(dev, "problem initializing queue\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001321 goto out_error_queue_alloc;
1322 }
Bryan Wua32c6912007-12-04 23:45:15 -08001323
Mike Frysinger138f97c2009-04-06 19:00:50 -07001324 status = bfin_spi_start_queue(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001325 if (status != 0) {
Bryan Wua32c6912007-12-04 23:45:15 -08001326 dev_err(dev, "problem starting queue\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001327 goto out_error_queue_alloc;
1328 }
1329
Vitja Makarovf9e522c2008-04-08 17:41:57 -07001330 status = peripheral_request_list(drv_data->pin_req, DRV_NAME);
1331 if (status != 0) {
1332 dev_err(&pdev->dev, ": Requesting Peripherals failed\n");
1333 goto out_error_queue_alloc;
1334 }
1335
Wolfgang Mueesbb8beec2009-05-22 01:11:02 +00001336 /* Reset SPI registers. If these registers were used by the boot loader,
1337 * the sky may fall on your head if you enable the dma controller.
1338 */
Mike Frysinger47885ce2011-06-17 04:16:56 -04001339 bfin_write(&drv_data->regs->ctl, BIT_CTL_CPHA | BIT_CTL_MASTER);
1340 bfin_write(&drv_data->regs->flg, 0xFF00);
Wolfgang Mueesbb8beec2009-05-22 01:11:02 +00001341
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001342 /* Register with the SPI framework */
1343 platform_set_drvdata(pdev, drv_data);
1344 status = spi_register_master(master);
1345 if (status != 0) {
Bryan Wua32c6912007-12-04 23:45:15 -08001346 dev_err(dev, "problem registering spi master\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001347 goto out_error_queue_alloc;
1348 }
Bryan Wua32c6912007-12-04 23:45:15 -08001349
Mike Frysinger47885ce2011-06-17 04:16:56 -04001350 dev_info(dev, "%s, Version %s, regs@%p, dma channel@%d\n",
1351 DRV_DESC, DRV_VERSION, drv_data->regs,
Bryan Wubb90eb02007-12-04 23:45:18 -08001352 drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001353 return status;
1354
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001355out_error_queue_alloc:
Mike Frysinger138f97c2009-04-06 19:00:50 -07001356 bfin_spi_destroy_queue(drv_data);
Yi Lif6a6d962009-06-03 09:46:22 +00001357out_error_free_io:
Mike Frysinger47885ce2011-06-17 04:16:56 -04001358 iounmap(drv_data->regs);
Bryan Wua32c6912007-12-04 23:45:15 -08001359out_error_ioremap:
1360out_error_get_res:
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001361 spi_master_put(master);
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001362
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001363 return status;
1364}
1365
1366/* stop hardware and remove the driver */
Grant Likelyfd4a3192012-12-07 16:57:14 +00001367static int bfin_spi_remove(struct platform_device *pdev)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001368{
Mike Frysinger9c0a7882010-10-18 02:45:22 -04001369 struct bfin_spi_master_data *drv_data = platform_get_drvdata(pdev);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001370 int status = 0;
1371
1372 if (!drv_data)
1373 return 0;
1374
1375 /* Remove the queue */
Mike Frysinger138f97c2009-04-06 19:00:50 -07001376 status = bfin_spi_destroy_queue(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001377 if (status != 0)
1378 return status;
1379
1380 /* Disable the SSP at the peripheral and SOC level */
1381 bfin_spi_disable(drv_data);
1382
1383 /* Release DMA */
1384 if (drv_data->master_info->enable_dma) {
Bryan Wubb90eb02007-12-04 23:45:18 -08001385 if (dma_channel_active(drv_data->dma_channel))
1386 free_dma(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001387 }
1388
Yi Lif6a6d962009-06-03 09:46:22 +00001389 if (drv_data->irq_requested) {
1390 free_irq(drv_data->spi_irq, drv_data);
1391 drv_data->irq_requested = 0;
1392 }
1393
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001394 /* Disconnect from the SPI framework */
1395 spi_unregister_master(drv_data->master);
1396
Bryan Wu003d9222007-12-04 23:45:22 -08001397 peripheral_free_list(drv_data->pin_req);
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001398
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001399 return 0;
1400}
1401
Jingoo Hanfbbfd682013-09-09 17:55:32 +09001402#ifdef CONFIG_PM_SLEEP
1403static int bfin_spi_suspend(struct device *dev)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001404{
Jingoo Hanfbbfd682013-09-09 17:55:32 +09001405 struct bfin_spi_master_data *drv_data = dev_get_drvdata(dev);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001406 int status = 0;
1407
Mike Frysinger138f97c2009-04-06 19:00:50 -07001408 status = bfin_spi_stop_queue(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001409 if (status != 0)
1410 return status;
1411
Mike Frysinger47885ce2011-06-17 04:16:56 -04001412 drv_data->ctrl_reg = bfin_read(&drv_data->regs->ctl);
1413 drv_data->flag_reg = bfin_read(&drv_data->regs->flg);
Barry Songb052fd02009-11-18 09:43:21 +00001414
1415 /*
1416 * reset SPI_CTL and SPI_FLG registers
1417 */
Mike Frysinger47885ce2011-06-17 04:16:56 -04001418 bfin_write(&drv_data->regs->ctl, BIT_CTL_CPHA | BIT_CTL_MASTER);
1419 bfin_write(&drv_data->regs->flg, 0xFF00);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001420
1421 return 0;
1422}
1423
Jingoo Hanfbbfd682013-09-09 17:55:32 +09001424static int bfin_spi_resume(struct device *dev)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001425{
Jingoo Hanfbbfd682013-09-09 17:55:32 +09001426 struct bfin_spi_master_data *drv_data = dev_get_drvdata(dev);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001427 int status = 0;
1428
Mike Frysinger47885ce2011-06-17 04:16:56 -04001429 bfin_write(&drv_data->regs->ctl, drv_data->ctrl_reg);
1430 bfin_write(&drv_data->regs->flg, drv_data->flag_reg);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001431
1432 /* Start the queue running */
Mike Frysinger138f97c2009-04-06 19:00:50 -07001433 status = bfin_spi_start_queue(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001434 if (status != 0) {
Jingoo Hanfbbfd682013-09-09 17:55:32 +09001435 dev_err(dev, "problem starting queue (%d)\n", status);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001436 return status;
1437 }
1438
1439 return 0;
1440}
Jingoo Hanfbbfd682013-09-09 17:55:32 +09001441
1442static SIMPLE_DEV_PM_OPS(bfin_spi_pm_ops, bfin_spi_suspend, bfin_spi_resume);
1443
1444#define BFIN_SPI_PM_OPS (&bfin_spi_pm_ops)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001445#else
Jingoo Hanfbbfd682013-09-09 17:55:32 +09001446#define BFIN_SPI_PM_OPS NULL
1447#endif
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001448
Kay Sievers7e38c3c2008-04-10 21:29:20 -07001449MODULE_ALIAS("platform:bfin-spi");
Mike Frysinger138f97c2009-04-06 19:00:50 -07001450static struct platform_driver bfin_spi_driver = {
David Brownellfc3ba952007-08-30 23:56:24 -07001451 .driver = {
Bryan Wua32c6912007-12-04 23:45:15 -08001452 .name = DRV_NAME,
Jingoo Hanfbbfd682013-09-09 17:55:32 +09001453 .pm = BFIN_SPI_PM_OPS,
Bryan Wu88b40362007-05-21 18:32:16 +08001454 },
Wolfram Sangdb9371b2013-10-08 22:35:38 +02001455 .probe = bfin_spi_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +00001456 .remove = bfin_spi_remove,
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001457};
1458
Mike Frysinger138f97c2009-04-06 19:00:50 -07001459static int __init bfin_spi_init(void)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001460{
Wolfram Sangdb9371b2013-10-08 22:35:38 +02001461 return platform_driver_register(&bfin_spi_driver);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001462}
Michael Hennerich6f7c17f2010-07-01 14:34:10 +00001463subsys_initcall(bfin_spi_init);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001464
Mike Frysinger138f97c2009-04-06 19:00:50 -07001465static void __exit bfin_spi_exit(void)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001466{
Mike Frysinger138f97c2009-04-06 19:00:50 -07001467 platform_driver_unregister(&bfin_spi_driver);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001468}
Mike Frysinger138f97c2009-04-06 19:00:50 -07001469module_exit(bfin_spi_exit);