blob: 5acec9ff01754ad494b76d01aa973c27a8f55ef5 [file] [log] [blame]
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001/*
Mike Frysinger26fdc1f2008-02-06 01:38:21 -08002 * Blackfin On-Chip SPI Driver
Wu, Bryana5f6abd2007-05-06 14:50:34 -07003 *
Bryan Wu131b17d2007-12-04 23:45:12 -08004 * Copyright 2004-2007 Analog Devices Inc.
Wu, Bryana5f6abd2007-05-06 14:50:34 -07005 *
Mike Frysinger26fdc1f2008-02-06 01:38:21 -08006 * Enter bugs at http://blackfin.uclinux.org/
Wu, Bryana5f6abd2007-05-06 14:50:34 -07007 *
Mike Frysinger26fdc1f2008-02-06 01:38:21 -08008 * Licensed under the GPL-2 or later.
Wu, Bryana5f6abd2007-05-06 14:50:34 -07009 */
10
11#include <linux/init.h>
12#include <linux/module.h>
Bryan Wu131b17d2007-12-04 23:45:12 -080013#include <linux/delay.h>
Wu, Bryana5f6abd2007-05-06 14:50:34 -070014#include <linux/device.h>
Bryan Wu131b17d2007-12-04 23:45:12 -080015#include <linux/io.h>
Wu, Bryana5f6abd2007-05-06 14:50:34 -070016#include <linux/ioport.h>
Bryan Wu131b17d2007-12-04 23:45:12 -080017#include <linux/irq.h>
Wu, Bryana5f6abd2007-05-06 14:50:34 -070018#include <linux/errno.h>
19#include <linux/interrupt.h>
20#include <linux/platform_device.h>
21#include <linux/dma-mapping.h>
22#include <linux/spi/spi.h>
23#include <linux/workqueue.h>
Wu, Bryana5f6abd2007-05-06 14:50:34 -070024
Wu, Bryana5f6abd2007-05-06 14:50:34 -070025#include <asm/dma.h>
Bryan Wu131b17d2007-12-04 23:45:12 -080026#include <asm/portmux.h>
Wu, Bryana5f6abd2007-05-06 14:50:34 -070027#include <asm/bfin5xx_spi.h>
Vitja Makarov8cf58582009-04-06 19:00:31 -070028#include <asm/cacheflush.h>
29
Bryan Wua32c6912007-12-04 23:45:15 -080030#define DRV_NAME "bfin-spi"
31#define DRV_AUTHOR "Bryan Wu, Luke Yang"
Will Newton6b1a8022007-12-10 15:49:26 -080032#define DRV_DESC "Blackfin BF5xx on-chip SPI Controller Driver"
Bryan Wua32c6912007-12-04 23:45:15 -080033#define DRV_VERSION "1.0"
34
35MODULE_AUTHOR(DRV_AUTHOR);
36MODULE_DESCRIPTION(DRV_DESC);
Wu, Bryana5f6abd2007-05-06 14:50:34 -070037MODULE_LICENSE("GPL");
38
Bryan Wubb90eb02007-12-04 23:45:18 -080039#define IS_DMA_ALIGNED(x) (((u32)(x)&0x07) == 0)
Wu, Bryana5f6abd2007-05-06 14:50:34 -070040
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)
45#define QUEUE_RUNNING 0
46#define QUEUE_STOPPED 1
Wu, Bryana5f6abd2007-05-06 14:50:34 -070047
48struct driver_data {
49 /* Driver model hookup */
50 struct platform_device *pdev;
51
52 /* SPI framework hookup */
53 struct spi_master *master;
54
Bryan Wubb90eb02007-12-04 23:45:18 -080055 /* Regs base of SPI controller */
Bryan Wuf4521262007-12-04 23:45:22 -080056 void __iomem *regs_base;
Bryan Wubb90eb02007-12-04 23:45:18 -080057
Bryan Wu003d9222007-12-04 23:45:22 -080058 /* Pin request list */
59 u16 *pin_req;
60
Wu, Bryana5f6abd2007-05-06 14:50:34 -070061 /* BFIN hookup */
62 struct bfin5xx_spi_master *master_info;
63
64 /* Driver message queue */
65 struct workqueue_struct *workqueue;
66 struct work_struct pump_messages;
67 spinlock_t lock;
68 struct list_head queue;
69 int busy;
70 int run;
71
72 /* Message Transfer pump */
73 struct tasklet_struct pump_transfers;
74
75 /* Current message transfer state info */
76 struct spi_message *cur_msg;
77 struct spi_transfer *cur_transfer;
78 struct chip_data *cur_chip;
79 size_t len_in_bytes;
80 size_t len;
81 void *tx;
82 void *tx_end;
83 void *rx;
84 void *rx_end;
Bryan Wubb90eb02007-12-04 23:45:18 -080085
86 /* DMA stuffs */
87 int dma_channel;
Wu, Bryana5f6abd2007-05-06 14:50:34 -070088 int dma_mapped;
Bryan Wubb90eb02007-12-04 23:45:18 -080089 int dma_requested;
Wu, Bryana5f6abd2007-05-06 14:50:34 -070090 dma_addr_t rx_dma;
91 dma_addr_t tx_dma;
Bryan Wubb90eb02007-12-04 23:45:18 -080092
Wu, Bryana5f6abd2007-05-06 14:50:34 -070093 size_t rx_map_len;
94 size_t tx_map_len;
95 u8 n_bytes;
Bryan Wufad91c82007-12-04 23:45:14 -080096 int cs_change;
Wu, Bryana5f6abd2007-05-06 14:50:34 -070097 void (*write) (struct driver_data *);
98 void (*read) (struct driver_data *);
99 void (*duplex) (struct driver_data *);
100};
101
102struct chip_data {
103 u16 ctl_reg;
104 u16 baud;
105 u16 flag;
106
107 u8 chip_select_num;
108 u8 n_bytes;
Bryan Wu88b40362007-05-21 18:32:16 +0800109 u8 width; /* 0 or 1 */
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700110 u8 enable_dma;
111 u8 bits_per_word; /* 8 or 16 */
112 u8 cs_change_per_word;
Bryan Wu62310e52007-12-04 23:45:20 -0800113 u16 cs_chg_udelay; /* Some devices require > 255usec delay */
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700114 void (*write) (struct driver_data *);
115 void (*read) (struct driver_data *);
116 void (*duplex) (struct driver_data *);
117};
118
Bryan Wubb90eb02007-12-04 23:45:18 -0800119#define DEFINE_SPI_REG(reg, off) \
120static inline u16 read_##reg(struct driver_data *drv_data) \
121 { return bfin_read16(drv_data->regs_base + off); } \
122static inline void write_##reg(struct driver_data *drv_data, u16 v) \
123 { bfin_write16(drv_data->regs_base + off, v); }
124
125DEFINE_SPI_REG(CTRL, 0x00)
126DEFINE_SPI_REG(FLAG, 0x04)
127DEFINE_SPI_REG(STAT, 0x08)
128DEFINE_SPI_REG(TDBR, 0x0C)
129DEFINE_SPI_REG(RDBR, 0x10)
130DEFINE_SPI_REG(BAUD, 0x14)
131DEFINE_SPI_REG(SHAW, 0x18)
132
Bryan Wu88b40362007-05-21 18:32:16 +0800133static void bfin_spi_enable(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700134{
135 u16 cr;
136
Bryan Wubb90eb02007-12-04 23:45:18 -0800137 cr = read_CTRL(drv_data);
138 write_CTRL(drv_data, (cr | BIT_CTL_ENABLE));
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700139}
140
Bryan Wu88b40362007-05-21 18:32:16 +0800141static void bfin_spi_disable(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700142{
143 u16 cr;
144
Bryan Wubb90eb02007-12-04 23:45:18 -0800145 cr = read_CTRL(drv_data);
146 write_CTRL(drv_data, (cr & (~BIT_CTL_ENABLE)));
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700147}
148
149/* Caculate the SPI_BAUD register value based on input HZ */
150static u16 hz_to_spi_baud(u32 speed_hz)
151{
152 u_long sclk = get_sclk();
153 u16 spi_baud = (sclk / (2 * speed_hz));
154
155 if ((sclk % (2 * speed_hz)) > 0)
156 spi_baud++;
157
Michael Hennerich7513e002009-04-06 19:00:32 -0700158 if (spi_baud < MIN_SPI_BAUD_VAL)
159 spi_baud = MIN_SPI_BAUD_VAL;
160
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700161 return spi_baud;
162}
163
164static int flush(struct driver_data *drv_data)
165{
166 unsigned long limit = loops_per_jiffy << 1;
167
168 /* wait for stop and clear stat */
Bryan Wubb90eb02007-12-04 23:45:18 -0800169 while (!(read_STAT(drv_data) & BIT_STAT_SPIF) && limit--)
Bryan Wud8c05002007-12-04 23:45:21 -0800170 cpu_relax();
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700171
Bryan Wubb90eb02007-12-04 23:45:18 -0800172 write_STAT(drv_data, BIT_STAT_CLR);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700173
174 return limit;
175}
176
Bryan Wufad91c82007-12-04 23:45:14 -0800177/* Chip select operation functions for cs_change flag */
Bryan Wubb90eb02007-12-04 23:45:18 -0800178static void cs_active(struct driver_data *drv_data, struct chip_data *chip)
Bryan Wufad91c82007-12-04 23:45:14 -0800179{
Bryan Wubb90eb02007-12-04 23:45:18 -0800180 u16 flag = read_FLAG(drv_data);
Bryan Wufad91c82007-12-04 23:45:14 -0800181
182 flag |= chip->flag;
183 flag &= ~(chip->flag << 8);
184
Bryan Wubb90eb02007-12-04 23:45:18 -0800185 write_FLAG(drv_data, flag);
Bryan Wufad91c82007-12-04 23:45:14 -0800186}
187
Bryan Wubb90eb02007-12-04 23:45:18 -0800188static void cs_deactive(struct driver_data *drv_data, struct chip_data *chip)
Bryan Wufad91c82007-12-04 23:45:14 -0800189{
Bryan Wubb90eb02007-12-04 23:45:18 -0800190 u16 flag = read_FLAG(drv_data);
Bryan Wufad91c82007-12-04 23:45:14 -0800191
192 flag |= (chip->flag << 8);
193
Bryan Wubb90eb02007-12-04 23:45:18 -0800194 write_FLAG(drv_data, flag);
Bryan Wu62310e52007-12-04 23:45:20 -0800195
196 /* Move delay here for consistency */
197 if (chip->cs_chg_udelay)
198 udelay(chip->cs_chg_udelay);
Bryan Wufad91c82007-12-04 23:45:14 -0800199}
200
Sonic Zhang7c4ef092007-12-04 23:45:16 -0800201#define MAX_SPI_SSEL 7
Bryan Wu5fec5b52007-12-04 23:45:13 -0800202
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700203/* stop controller and re-config current chip*/
Bryan Wu8d20d0a2008-02-06 01:38:17 -0800204static void restore_state(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700205{
206 struct chip_data *chip = drv_data->cur_chip;
207
208 /* Clear status and disable clock */
Bryan Wubb90eb02007-12-04 23:45:18 -0800209 write_STAT(drv_data, BIT_STAT_CLR);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700210 bfin_spi_disable(drv_data);
Bryan Wu88b40362007-05-21 18:32:16 +0800211 dev_dbg(&drv_data->pdev->dev, "restoring spi ctl state\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700212
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700213 /* Load the registers */
Bryan Wubb90eb02007-12-04 23:45:18 -0800214 write_CTRL(drv_data, chip->ctl_reg);
Bryan Wu092e1fd2007-12-04 23:45:23 -0800215 write_BAUD(drv_data, chip->baud);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800216
217 bfin_spi_enable(drv_data);
Sonic Zhang07612e52007-12-04 23:45:21 -0800218 cs_active(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700219}
220
221/* used to kick off transfer in rx mode */
Bryan Wubb90eb02007-12-04 23:45:18 -0800222static unsigned short dummy_read(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700223{
224 unsigned short tmp;
Bryan Wubb90eb02007-12-04 23:45:18 -0800225 tmp = read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700226 return tmp;
227}
228
229static void null_writer(struct driver_data *drv_data)
230{
231 u8 n_bytes = drv_data->n_bytes;
232
233 while (drv_data->tx < drv_data->tx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800234 write_TDBR(drv_data, 0);
235 while ((read_STAT(drv_data) & BIT_STAT_TXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800236 cpu_relax();
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700237 drv_data->tx += n_bytes;
238 }
239}
240
241static void null_reader(struct driver_data *drv_data)
242{
243 u8 n_bytes = drv_data->n_bytes;
Bryan Wubb90eb02007-12-04 23:45:18 -0800244 dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700245
246 while (drv_data->rx < drv_data->rx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800247 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800248 cpu_relax();
Bryan Wubb90eb02007-12-04 23:45:18 -0800249 dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700250 drv_data->rx += n_bytes;
251 }
252}
253
254static void u8_writer(struct driver_data *drv_data)
255{
Bryan Wu131b17d2007-12-04 23:45:12 -0800256 dev_dbg(&drv_data->pdev->dev,
Bryan Wubb90eb02007-12-04 23:45:18 -0800257 "cr8-s is 0x%x\n", read_STAT(drv_data));
Sonic Zhangcc487e72007-12-04 23:45:17 -0800258
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700259 while (drv_data->tx < drv_data->tx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800260 write_TDBR(drv_data, (*(u8 *) (drv_data->tx)));
261 while (read_STAT(drv_data) & BIT_STAT_TXS)
Bryan Wud8c05002007-12-04 23:45:21 -0800262 cpu_relax();
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700263 ++drv_data->tx;
264 }
Sonic Zhang13f3e642008-02-06 01:38:20 -0800265
266 /* poll for SPI completion before return */
267 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
268 cpu_relax();
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700269}
270
271static void u8_cs_chg_writer(struct driver_data *drv_data)
272{
273 struct chip_data *chip = drv_data->cur_chip;
274
275 while (drv_data->tx < drv_data->tx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800276 cs_active(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700277
Bryan Wubb90eb02007-12-04 23:45:18 -0800278 write_TDBR(drv_data, (*(u8 *) (drv_data->tx)));
279 while (read_STAT(drv_data) & BIT_STAT_TXS)
Bryan Wud8c05002007-12-04 23:45:21 -0800280 cpu_relax();
Bryan Wue26aa012008-02-06 01:38:18 -0800281 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
282 cpu_relax();
Bryan Wu62310e52007-12-04 23:45:20 -0800283
Bryan Wubb90eb02007-12-04 23:45:18 -0800284 cs_deactive(drv_data, chip);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800285
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700286 ++drv_data->tx;
287 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700288}
289
290static void u8_reader(struct driver_data *drv_data)
291{
Bryan Wu131b17d2007-12-04 23:45:12 -0800292 dev_dbg(&drv_data->pdev->dev,
Bryan Wubb90eb02007-12-04 23:45:18 -0800293 "cr-8 is 0x%x\n", read_STAT(drv_data));
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700294
Sonic Zhang3f479a62007-12-04 23:45:18 -0800295 /* poll for SPI completion before start */
Bryan Wubb90eb02007-12-04 23:45:18 -0800296 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Bryan Wud8c05002007-12-04 23:45:21 -0800297 cpu_relax();
Sonic Zhang3f479a62007-12-04 23:45:18 -0800298
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700299 /* clear TDBR buffer before read(else it will be shifted out) */
Bryan Wubb90eb02007-12-04 23:45:18 -0800300 write_TDBR(drv_data, 0xFFFF);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700301
Bryan Wubb90eb02007-12-04 23:45:18 -0800302 dummy_read(drv_data);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800303
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700304 while (drv_data->rx < drv_data->rx_end - 1) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800305 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800306 cpu_relax();
Bryan Wubb90eb02007-12-04 23:45:18 -0800307 *(u8 *) (drv_data->rx) = read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700308 ++drv_data->rx;
309 }
310
Bryan Wubb90eb02007-12-04 23:45:18 -0800311 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800312 cpu_relax();
Bryan Wubb90eb02007-12-04 23:45:18 -0800313 *(u8 *) (drv_data->rx) = read_SHAW(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700314 ++drv_data->rx;
315}
316
317static void u8_cs_chg_reader(struct driver_data *drv_data)
318{
319 struct chip_data *chip = drv_data->cur_chip;
320
Bryan Wue26aa012008-02-06 01:38:18 -0800321 while (drv_data->rx < drv_data->rx_end) {
322 cs_active(drv_data, chip);
323 read_RDBR(drv_data); /* kick off */
Bryan Wu5fec5b52007-12-04 23:45:13 -0800324
Bryan Wubb90eb02007-12-04 23:45:18 -0800325 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800326 cpu_relax();
Bryan Wue26aa012008-02-06 01:38:18 -0800327 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
328 cpu_relax();
329
330 *(u8 *) (drv_data->rx) = read_SHAW(drv_data);
331 cs_deactive(drv_data, chip);
332
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700333 ++drv_data->rx;
334 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700335}
336
337static void u8_duplex(struct driver_data *drv_data)
338{
339 /* in duplex mode, clk is triggered by writing of TDBR */
340 while (drv_data->rx < drv_data->rx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800341 write_TDBR(drv_data, (*(u8 *) (drv_data->tx)));
Bryan Wu4fd432d2008-02-06 01:38:19 -0800342 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Bryan Wud8c05002007-12-04 23:45:21 -0800343 cpu_relax();
Bryan Wubb90eb02007-12-04 23:45:18 -0800344 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800345 cpu_relax();
Bryan Wubb90eb02007-12-04 23:45:18 -0800346 *(u8 *) (drv_data->rx) = read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700347 ++drv_data->rx;
348 ++drv_data->tx;
349 }
350}
351
352static void u8_cs_chg_duplex(struct driver_data *drv_data)
353{
354 struct chip_data *chip = drv_data->cur_chip;
355
356 while (drv_data->rx < drv_data->rx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800357 cs_active(drv_data, chip);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800358
Bryan Wubb90eb02007-12-04 23:45:18 -0800359 write_TDBR(drv_data, (*(u8 *) (drv_data->tx)));
Bryan Wue26aa012008-02-06 01:38:18 -0800360
361 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Bryan Wud8c05002007-12-04 23:45:21 -0800362 cpu_relax();
Bryan Wubb90eb02007-12-04 23:45:18 -0800363 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800364 cpu_relax();
Bryan Wubb90eb02007-12-04 23:45:18 -0800365 *(u8 *) (drv_data->rx) = read_RDBR(drv_data);
Bryan Wu62310e52007-12-04 23:45:20 -0800366
Bryan Wubb90eb02007-12-04 23:45:18 -0800367 cs_deactive(drv_data, chip);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800368
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700369 ++drv_data->rx;
370 ++drv_data->tx;
371 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700372}
373
374static void u16_writer(struct driver_data *drv_data)
375{
Bryan Wu131b17d2007-12-04 23:45:12 -0800376 dev_dbg(&drv_data->pdev->dev,
Bryan Wubb90eb02007-12-04 23:45:18 -0800377 "cr16 is 0x%x\n", read_STAT(drv_data));
Bryan Wu88b40362007-05-21 18:32:16 +0800378
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700379 while (drv_data->tx < drv_data->tx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800380 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
381 while ((read_STAT(drv_data) & BIT_STAT_TXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800382 cpu_relax();
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700383 drv_data->tx += 2;
384 }
Sonic Zhang13f3e642008-02-06 01:38:20 -0800385
386 /* poll for SPI completion before return */
387 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
388 cpu_relax();
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700389}
390
391static void u16_cs_chg_writer(struct driver_data *drv_data)
392{
393 struct chip_data *chip = drv_data->cur_chip;
394
395 while (drv_data->tx < drv_data->tx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800396 cs_active(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700397
Bryan Wubb90eb02007-12-04 23:45:18 -0800398 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
399 while ((read_STAT(drv_data) & BIT_STAT_TXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800400 cpu_relax();
Sonic Zhang13f3e642008-02-06 01:38:20 -0800401 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
402 cpu_relax();
Bryan Wu62310e52007-12-04 23:45:20 -0800403
Bryan Wubb90eb02007-12-04 23:45:18 -0800404 cs_deactive(drv_data, chip);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800405
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700406 drv_data->tx += 2;
407 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700408}
409
410static void u16_reader(struct driver_data *drv_data)
411{
Bryan Wu88b40362007-05-21 18:32:16 +0800412 dev_dbg(&drv_data->pdev->dev,
Bryan Wubb90eb02007-12-04 23:45:18 -0800413 "cr-16 is 0x%x\n", read_STAT(drv_data));
Sonic Zhangcc487e72007-12-04 23:45:17 -0800414
Sonic Zhang3f479a62007-12-04 23:45:18 -0800415 /* poll for SPI completion before start */
Bryan Wubb90eb02007-12-04 23:45:18 -0800416 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Bryan Wud8c05002007-12-04 23:45:21 -0800417 cpu_relax();
Sonic Zhang3f479a62007-12-04 23:45:18 -0800418
Sonic Zhangcc487e72007-12-04 23:45:17 -0800419 /* clear TDBR buffer before read(else it will be shifted out) */
Bryan Wubb90eb02007-12-04 23:45:18 -0800420 write_TDBR(drv_data, 0xFFFF);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800421
Bryan Wubb90eb02007-12-04 23:45:18 -0800422 dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700423
424 while (drv_data->rx < (drv_data->rx_end - 2)) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800425 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800426 cpu_relax();
Bryan Wubb90eb02007-12-04 23:45:18 -0800427 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700428 drv_data->rx += 2;
429 }
430
Bryan Wubb90eb02007-12-04 23:45:18 -0800431 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800432 cpu_relax();
Bryan Wubb90eb02007-12-04 23:45:18 -0800433 *(u16 *) (drv_data->rx) = read_SHAW(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700434 drv_data->rx += 2;
435}
436
437static void u16_cs_chg_reader(struct driver_data *drv_data)
438{
439 struct chip_data *chip = drv_data->cur_chip;
440
Sonic Zhang3f479a62007-12-04 23:45:18 -0800441 /* poll for SPI completion before start */
Bryan Wubb90eb02007-12-04 23:45:18 -0800442 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Bryan Wud8c05002007-12-04 23:45:21 -0800443 cpu_relax();
Sonic Zhang3f479a62007-12-04 23:45:18 -0800444
Sonic Zhangcc487e72007-12-04 23:45:17 -0800445 /* clear TDBR buffer before read(else it will be shifted out) */
Bryan Wubb90eb02007-12-04 23:45:18 -0800446 write_TDBR(drv_data, 0xFFFF);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700447
Bryan Wubb90eb02007-12-04 23:45:18 -0800448 cs_active(drv_data, chip);
449 dummy_read(drv_data);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800450
Bryan Wuc3061ab2007-12-04 23:45:19 -0800451 while (drv_data->rx < drv_data->rx_end - 2) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800452 cs_deactive(drv_data, chip);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800453
Bryan Wubb90eb02007-12-04 23:45:18 -0800454 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800455 cpu_relax();
Bryan Wubb90eb02007-12-04 23:45:18 -0800456 cs_active(drv_data, chip);
457 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700458 drv_data->rx += 2;
459 }
Bryan Wubb90eb02007-12-04 23:45:18 -0800460 cs_deactive(drv_data, chip);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800461
Bryan Wubb90eb02007-12-04 23:45:18 -0800462 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800463 cpu_relax();
Bryan Wubb90eb02007-12-04 23:45:18 -0800464 *(u16 *) (drv_data->rx) = read_SHAW(drv_data);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800465 drv_data->rx += 2;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700466}
467
468static void u16_duplex(struct driver_data *drv_data)
469{
470 /* in duplex mode, clk is triggered by writing of TDBR */
471 while (drv_data->tx < drv_data->tx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800472 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
Bryan Wu4fd432d2008-02-06 01:38:19 -0800473 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Bryan Wud8c05002007-12-04 23:45:21 -0800474 cpu_relax();
Bryan Wubb90eb02007-12-04 23:45:18 -0800475 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800476 cpu_relax();
Bryan Wubb90eb02007-12-04 23:45:18 -0800477 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700478 drv_data->rx += 2;
479 drv_data->tx += 2;
480 }
481}
482
483static void u16_cs_chg_duplex(struct driver_data *drv_data)
484{
485 struct chip_data *chip = drv_data->cur_chip;
486
487 while (drv_data->tx < drv_data->tx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800488 cs_active(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700489
Bryan Wubb90eb02007-12-04 23:45:18 -0800490 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
Bryan Wu4fd432d2008-02-06 01:38:19 -0800491 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Bryan Wud8c05002007-12-04 23:45:21 -0800492 cpu_relax();
Bryan Wubb90eb02007-12-04 23:45:18 -0800493 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800494 cpu_relax();
Bryan Wubb90eb02007-12-04 23:45:18 -0800495 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
Bryan Wu62310e52007-12-04 23:45:20 -0800496
Bryan Wubb90eb02007-12-04 23:45:18 -0800497 cs_deactive(drv_data, chip);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800498
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700499 drv_data->rx += 2;
500 drv_data->tx += 2;
501 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700502}
503
504/* test if ther is more transfer to be done */
505static void *next_transfer(struct driver_data *drv_data)
506{
507 struct spi_message *msg = drv_data->cur_msg;
508 struct spi_transfer *trans = drv_data->cur_transfer;
509
510 /* Move to next transfer */
511 if (trans->transfer_list.next != &msg->transfers) {
512 drv_data->cur_transfer =
513 list_entry(trans->transfer_list.next,
514 struct spi_transfer, transfer_list);
515 return RUNNING_STATE;
516 } else
517 return DONE_STATE;
518}
519
520/*
521 * caller already set message->status;
522 * dma and pio irqs are blocked give finished message back
523 */
524static void giveback(struct driver_data *drv_data)
525{
Bryan Wufad91c82007-12-04 23:45:14 -0800526 struct chip_data *chip = drv_data->cur_chip;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700527 struct spi_transfer *last_transfer;
528 unsigned long flags;
529 struct spi_message *msg;
530
531 spin_lock_irqsave(&drv_data->lock, flags);
532 msg = drv_data->cur_msg;
533 drv_data->cur_msg = NULL;
534 drv_data->cur_transfer = NULL;
535 drv_data->cur_chip = NULL;
536 queue_work(drv_data->workqueue, &drv_data->pump_messages);
537 spin_unlock_irqrestore(&drv_data->lock, flags);
538
539 last_transfer = list_entry(msg->transfers.prev,
540 struct spi_transfer, transfer_list);
541
542 msg->state = NULL;
543
544 /* disable chip select signal. And not stop spi in autobuffer mode */
545 if (drv_data->tx_dma != 0xFFFF) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800546 cs_deactive(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700547 bfin_spi_disable(drv_data);
548 }
549
Bryan Wufad91c82007-12-04 23:45:14 -0800550 if (!drv_data->cs_change)
Bryan Wubb90eb02007-12-04 23:45:18 -0800551 cs_deactive(drv_data, chip);
Bryan Wufad91c82007-12-04 23:45:14 -0800552
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700553 if (msg->complete)
554 msg->complete(msg->context);
555}
556
Bryan Wu88b40362007-05-21 18:32:16 +0800557static irqreturn_t dma_irq_handler(int irq, void *dev_id)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700558{
Jeff Garzik15aafa22008-02-06 01:36:20 -0800559 struct driver_data *drv_data = dev_id;
Bryan Wufad91c82007-12-04 23:45:14 -0800560 struct chip_data *chip = drv_data->cur_chip;
Bryan Wubb90eb02007-12-04 23:45:18 -0800561 struct spi_message *msg = drv_data->cur_msg;
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700562 unsigned short dmastat = get_dma_curr_irqstat(drv_data->dma_channel);
Mike Frysinger04b95d22009-04-06 19:00:35 -0700563 u16 spistat = read_STAT(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700564
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700565 dev_dbg(&drv_data->pdev->dev,
566 "in dma_irq_handler dmastat:0x%x spistat:0x%x\n",
567 dmastat, spistat);
568
Bryan Wubb90eb02007-12-04 23:45:18 -0800569 clear_dma_irqstat(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700570
Bryan Wud6fe89b2007-06-11 17:34:17 +0800571 /* Wait for DMA to complete */
Bryan Wubb90eb02007-12-04 23:45:18 -0800572 while (get_dma_curr_irqstat(drv_data->dma_channel) & DMA_RUN)
Bryan Wud8c05002007-12-04 23:45:21 -0800573 cpu_relax();
Bryan Wud6fe89b2007-06-11 17:34:17 +0800574
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700575 /*
Bryan Wud6fe89b2007-06-11 17:34:17 +0800576 * wait for the last transaction shifted out. HRM states:
577 * at this point there may still be data in the SPI DMA FIFO waiting
578 * to be transmitted ... software needs to poll TXS in the SPI_STAT
579 * register until it goes low for 2 successive reads
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700580 */
581 if (drv_data->tx != NULL) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800582 while ((read_STAT(drv_data) & TXS) ||
583 (read_STAT(drv_data) & TXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800584 cpu_relax();
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700585 }
586
Bryan Wubb90eb02007-12-04 23:45:18 -0800587 while (!(read_STAT(drv_data) & SPIF))
Bryan Wud8c05002007-12-04 23:45:21 -0800588 cpu_relax();
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700589
Mike Frysinger04b95d22009-04-06 19:00:35 -0700590 if (spistat & RBSY) {
591 msg->state = ERROR_STATE;
592 dev_err(&drv_data->pdev->dev, "dma receive: fifo/buffer overflow\n");
593 } else {
594 msg->actual_length += drv_data->len_in_bytes;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700595
Mike Frysinger04b95d22009-04-06 19:00:35 -0700596 if (drv_data->cs_change)
597 cs_deactive(drv_data, chip);
Bryan Wufad91c82007-12-04 23:45:14 -0800598
Mike Frysinger04b95d22009-04-06 19:00:35 -0700599 /* Move to next transfer */
600 msg->state = next_transfer(drv_data);
601 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700602
603 /* Schedule transfer tasklet */
604 tasklet_schedule(&drv_data->pump_transfers);
605
606 /* free the irq handler before next transfer */
Bryan Wu88b40362007-05-21 18:32:16 +0800607 dev_dbg(&drv_data->pdev->dev,
608 "disable dma channel irq%d\n",
Bryan Wubb90eb02007-12-04 23:45:18 -0800609 drv_data->dma_channel);
610 dma_disable_irq(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700611
612 return IRQ_HANDLED;
613}
614
615static void pump_transfers(unsigned long data)
616{
617 struct driver_data *drv_data = (struct driver_data *)data;
618 struct spi_message *message = NULL;
619 struct spi_transfer *transfer = NULL;
620 struct spi_transfer *previous = NULL;
621 struct chip_data *chip = NULL;
Bryan Wu88b40362007-05-21 18:32:16 +0800622 u8 width;
623 u16 cr, dma_width, dma_config;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700624 u32 tranf_success = 1;
Vitja Makarov8eeb12e2008-05-01 04:35:03 -0700625 u8 full_duplex = 0;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700626
627 /* Get current state information */
628 message = drv_data->cur_msg;
629 transfer = drv_data->cur_transfer;
630 chip = drv_data->cur_chip;
Bryan Wu092e1fd2007-12-04 23:45:23 -0800631
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700632 /*
633 * if msg is error or done, report it back using complete() callback
634 */
635
636 /* Handle for abort */
637 if (message->state == ERROR_STATE) {
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700638 dev_dbg(&drv_data->pdev->dev, "transfer: we've hit an error\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700639 message->status = -EIO;
640 giveback(drv_data);
641 return;
642 }
643
644 /* Handle end of message */
645 if (message->state == DONE_STATE) {
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700646 dev_dbg(&drv_data->pdev->dev, "transfer: all done!\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700647 message->status = 0;
648 giveback(drv_data);
649 return;
650 }
651
652 /* Delay if requested at end of transfer */
653 if (message->state == RUNNING_STATE) {
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700654 dev_dbg(&drv_data->pdev->dev, "transfer: still running ...\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700655 previous = list_entry(transfer->transfer_list.prev,
656 struct spi_transfer, transfer_list);
657 if (previous->delay_usecs)
658 udelay(previous->delay_usecs);
659 }
660
661 /* Setup the transfer state based on the type of transfer */
662 if (flush(drv_data) == 0) {
663 dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
664 message->status = -EIO;
665 giveback(drv_data);
666 return;
667 }
668
669 if (transfer->tx_buf != NULL) {
670 drv_data->tx = (void *)transfer->tx_buf;
671 drv_data->tx_end = drv_data->tx + transfer->len;
Bryan Wu88b40362007-05-21 18:32:16 +0800672 dev_dbg(&drv_data->pdev->dev, "tx_buf is %p, tx_end is %p\n",
673 transfer->tx_buf, drv_data->tx_end);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700674 } else {
675 drv_data->tx = NULL;
676 }
677
678 if (transfer->rx_buf != NULL) {
Vitja Makarov8eeb12e2008-05-01 04:35:03 -0700679 full_duplex = transfer->tx_buf != NULL;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700680 drv_data->rx = transfer->rx_buf;
681 drv_data->rx_end = drv_data->rx + transfer->len;
Bryan Wu88b40362007-05-21 18:32:16 +0800682 dev_dbg(&drv_data->pdev->dev, "rx_buf is %p, rx_end is %p\n",
683 transfer->rx_buf, drv_data->rx_end);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700684 } else {
685 drv_data->rx = NULL;
686 }
687
688 drv_data->rx_dma = transfer->rx_dma;
689 drv_data->tx_dma = transfer->tx_dma;
690 drv_data->len_in_bytes = transfer->len;
Bryan Wufad91c82007-12-04 23:45:14 -0800691 drv_data->cs_change = transfer->cs_change;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700692
Bryan Wu092e1fd2007-12-04 23:45:23 -0800693 /* Bits per word setup */
694 switch (transfer->bits_per_word) {
695 case 8:
696 drv_data->n_bytes = 1;
697 width = CFG_SPI_WORDSIZE8;
698 drv_data->read = chip->cs_change_per_word ?
699 u8_cs_chg_reader : u8_reader;
700 drv_data->write = chip->cs_change_per_word ?
701 u8_cs_chg_writer : u8_writer;
702 drv_data->duplex = chip->cs_change_per_word ?
703 u8_cs_chg_duplex : u8_duplex;
704 break;
705
706 case 16:
707 drv_data->n_bytes = 2;
708 width = CFG_SPI_WORDSIZE16;
709 drv_data->read = chip->cs_change_per_word ?
710 u16_cs_chg_reader : u16_reader;
711 drv_data->write = chip->cs_change_per_word ?
712 u16_cs_chg_writer : u16_writer;
713 drv_data->duplex = chip->cs_change_per_word ?
714 u16_cs_chg_duplex : u16_duplex;
715 break;
716
717 default:
718 /* No change, the same as default setting */
719 drv_data->n_bytes = chip->n_bytes;
720 width = chip->width;
721 drv_data->write = drv_data->tx ? chip->write : null_writer;
722 drv_data->read = drv_data->rx ? chip->read : null_reader;
723 drv_data->duplex = chip->duplex ? chip->duplex : null_writer;
724 break;
725 }
726 cr = (read_CTRL(drv_data) & (~BIT_CTL_TIMOD));
727 cr |= (width << 8);
728 write_CTRL(drv_data, cr);
729
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700730 if (width == CFG_SPI_WORDSIZE16) {
731 drv_data->len = (transfer->len) >> 1;
732 } else {
733 drv_data->len = transfer->len;
734 }
Mike Frysinger4fb98ef2008-04-08 17:41:57 -0700735 dev_dbg(&drv_data->pdev->dev,
736 "transfer: drv_data->write is %p, chip->write is %p, null_wr is %p\n",
Bryan Wu131b17d2007-12-04 23:45:12 -0800737 drv_data->write, chip->write, null_writer);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700738
739 /* speed and width has been set on per message */
740 message->state = RUNNING_STATE;
741 dma_config = 0;
742
Bryan Wu092e1fd2007-12-04 23:45:23 -0800743 /* Speed setup (surely valid because already checked) */
744 if (transfer->speed_hz)
745 write_BAUD(drv_data, hz_to_spi_baud(transfer->speed_hz));
746 else
747 write_BAUD(drv_data, chip->baud);
748
Bryan Wubb90eb02007-12-04 23:45:18 -0800749 write_STAT(drv_data, BIT_STAT_CLR);
750 cr = (read_CTRL(drv_data) & (~BIT_CTL_TIMOD));
751 cs_active(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700752
Bryan Wu88b40362007-05-21 18:32:16 +0800753 dev_dbg(&drv_data->pdev->dev,
754 "now pumping a transfer: width is %d, len is %d\n",
755 width, transfer->len);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700756
757 /*
Vitja Makarov8cf58582009-04-06 19:00:31 -0700758 * Try to map dma buffer and do a dma transfer. If successful use,
759 * different way to r/w according to the enable_dma settings and if
760 * we are not doing a full duplex transfer (since the hardware does
761 * not support full duplex DMA transfers).
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700762 */
Vitja Makarov8eeb12e2008-05-01 04:35:03 -0700763 if (!full_duplex && drv_data->cur_chip->enable_dma
764 && drv_data->len > 6) {
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700765
Mike Frysinger7aec3562009-04-06 19:00:36 -0700766 unsigned long dma_start_addr;
767
Bryan Wubb90eb02007-12-04 23:45:18 -0800768 disable_dma(drv_data->dma_channel);
769 clear_dma_irqstat(drv_data->dma_channel);
Sonic Zhang07612e52007-12-04 23:45:21 -0800770 bfin_spi_disable(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700771
772 /* config dma channel */
Bryan Wu88b40362007-05-21 18:32:16 +0800773 dev_dbg(&drv_data->pdev->dev, "doing dma transfer\n");
Mike Frysinger7aec3562009-04-06 19:00:36 -0700774 set_dma_x_count(drv_data->dma_channel, drv_data->len);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700775 if (width == CFG_SPI_WORDSIZE16) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800776 set_dma_x_modify(drv_data->dma_channel, 2);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700777 dma_width = WDSIZE_16;
778 } else {
Bryan Wubb90eb02007-12-04 23:45:18 -0800779 set_dma_x_modify(drv_data->dma_channel, 1);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700780 dma_width = WDSIZE_8;
781 }
782
Sonic Zhang3f479a62007-12-04 23:45:18 -0800783 /* poll for SPI completion before start */
Bryan Wubb90eb02007-12-04 23:45:18 -0800784 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Bryan Wud8c05002007-12-04 23:45:21 -0800785 cpu_relax();
Sonic Zhang3f479a62007-12-04 23:45:18 -0800786
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700787 /* dirty hack for autobuffer DMA mode */
788 if (drv_data->tx_dma == 0xFFFF) {
Bryan Wu88b40362007-05-21 18:32:16 +0800789 dev_dbg(&drv_data->pdev->dev,
790 "doing autobuffer DMA out.\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700791
792 /* no irq in autobuffer mode */
793 dma_config =
794 (DMAFLOW_AUTO | RESTART | dma_width | DI_EN);
Bryan Wubb90eb02007-12-04 23:45:18 -0800795 set_dma_config(drv_data->dma_channel, dma_config);
796 set_dma_start_addr(drv_data->dma_channel,
Bryan Wua32c6912007-12-04 23:45:15 -0800797 (unsigned long)drv_data->tx);
Bryan Wubb90eb02007-12-04 23:45:18 -0800798 enable_dma(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700799
Sonic Zhang07612e52007-12-04 23:45:21 -0800800 /* start SPI transfer */
801 write_CTRL(drv_data,
802 (cr | CFG_SPI_DMAWRITE | BIT_CTL_ENABLE));
803
804 /* just return here, there can only be one transfer
805 * in this mode
806 */
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700807 message->status = 0;
808 giveback(drv_data);
809 return;
810 }
811
812 /* In dma mode, rx or tx must be NULL in one transfer */
Mike Frysinger7aec3562009-04-06 19:00:36 -0700813 dma_config = (RESTART | dma_width | DI_EN);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700814 if (drv_data->rx != NULL) {
815 /* set transfer mode, and enable SPI */
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700816 dev_dbg(&drv_data->pdev->dev, "doing DMA in to %p (size %zx)\n",
817 drv_data->rx, drv_data->len_in_bytes);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700818
Vitja Makarov8cf58582009-04-06 19:00:31 -0700819 /* invalidate caches, if needed */
820 if (bfin_addr_dcachable((unsigned long) drv_data->rx))
821 invalidate_dcache_range((unsigned long) drv_data->rx,
822 (unsigned long) (drv_data->rx +
Mike Frysingerace32862009-04-06 19:00:34 -0700823 drv_data->len_in_bytes));
Vitja Makarov8cf58582009-04-06 19:00:31 -0700824
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700825 /* clear tx reg soformer data is not shifted out */
Bryan Wubb90eb02007-12-04 23:45:18 -0800826 write_TDBR(drv_data, 0xFFFF);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700827
Mike Frysinger7aec3562009-04-06 19:00:36 -0700828 dma_config |= WNR;
829 dma_start_addr = (unsigned long)drv_data->rx;
830 cr |= CFG_SPI_DMAREAD;
Sonic Zhang07612e52007-12-04 23:45:21 -0800831
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700832 } else if (drv_data->tx != NULL) {
Bryan Wu88b40362007-05-21 18:32:16 +0800833 dev_dbg(&drv_data->pdev->dev, "doing DMA out.\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700834
Vitja Makarov8cf58582009-04-06 19:00:31 -0700835 /* flush caches, if needed */
836 if (bfin_addr_dcachable((unsigned long) drv_data->tx))
837 flush_dcache_range((unsigned long) drv_data->tx,
838 (unsigned long) (drv_data->tx +
Mike Frysingerace32862009-04-06 19:00:34 -0700839 drv_data->len_in_bytes));
Vitja Makarov8cf58582009-04-06 19:00:31 -0700840
Mike Frysinger7aec3562009-04-06 19:00:36 -0700841 dma_start_addr = (unsigned long)drv_data->tx;
842 cr |= CFG_SPI_DMAWRITE;
Sonic Zhang07612e52007-12-04 23:45:21 -0800843
Mike Frysinger7aec3562009-04-06 19:00:36 -0700844 } else
845 BUG();
846
847 /* start dma */
848 dma_enable_irq(drv_data->dma_channel);
849 set_dma_config(drv_data->dma_channel, dma_config);
850 set_dma_start_addr(drv_data->dma_channel, dma_start_addr);
851 enable_dma(drv_data->dma_channel);
852
853 /* start SPI transfer */
854 write_CTRL(drv_data, (cr | BIT_CTL_ENABLE));
855
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700856 } else {
857 /* IO mode write then read */
Bryan Wu88b40362007-05-21 18:32:16 +0800858 dev_dbg(&drv_data->pdev->dev, "doing IO transfer\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700859
Vitja Makarov8eeb12e2008-05-01 04:35:03 -0700860 if (full_duplex) {
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700861 /* full duplex mode */
862 BUG_ON((drv_data->tx_end - drv_data->tx) !=
863 (drv_data->rx_end - drv_data->rx));
Bryan Wu88b40362007-05-21 18:32:16 +0800864 dev_dbg(&drv_data->pdev->dev,
865 "IO duplex: cr is 0x%x\n", cr);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700866
Sonic Zhangcc487e72007-12-04 23:45:17 -0800867 /* set SPI transfer mode */
Bryan Wubb90eb02007-12-04 23:45:18 -0800868 write_CTRL(drv_data, (cr | CFG_SPI_WRITE));
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700869
870 drv_data->duplex(drv_data);
871
872 if (drv_data->tx != drv_data->tx_end)
873 tranf_success = 0;
874 } else if (drv_data->tx != NULL) {
875 /* write only half duplex */
Bryan Wu131b17d2007-12-04 23:45:12 -0800876 dev_dbg(&drv_data->pdev->dev,
Bryan Wu88b40362007-05-21 18:32:16 +0800877 "IO write: cr is 0x%x\n", cr);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700878
Sonic Zhangcc487e72007-12-04 23:45:17 -0800879 /* set SPI transfer mode */
Bryan Wubb90eb02007-12-04 23:45:18 -0800880 write_CTRL(drv_data, (cr | CFG_SPI_WRITE));
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700881
882 drv_data->write(drv_data);
883
884 if (drv_data->tx != drv_data->tx_end)
885 tranf_success = 0;
886 } else if (drv_data->rx != NULL) {
887 /* read only half duplex */
Bryan Wu131b17d2007-12-04 23:45:12 -0800888 dev_dbg(&drv_data->pdev->dev,
Bryan Wu88b40362007-05-21 18:32:16 +0800889 "IO read: cr is 0x%x\n", cr);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700890
Sonic Zhangcc487e72007-12-04 23:45:17 -0800891 /* set SPI transfer mode */
Bryan Wubb90eb02007-12-04 23:45:18 -0800892 write_CTRL(drv_data, (cr | CFG_SPI_READ));
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700893
894 drv_data->read(drv_data);
895 if (drv_data->rx != drv_data->rx_end)
896 tranf_success = 0;
897 }
898
899 if (!tranf_success) {
Bryan Wu131b17d2007-12-04 23:45:12 -0800900 dev_dbg(&drv_data->pdev->dev,
Bryan Wu88b40362007-05-21 18:32:16 +0800901 "IO write error!\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700902 message->state = ERROR_STATE;
903 } else {
904 /* Update total byte transfered */
Mike Frysingerace32862009-04-06 19:00:34 -0700905 message->actual_length += drv_data->len_in_bytes;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700906
907 /* Move to next transfer of this msg */
908 message->state = next_transfer(drv_data);
909 }
910
911 /* Schedule next transfer tasklet */
912 tasklet_schedule(&drv_data->pump_transfers);
913
914 }
915}
916
917/* pop a msg from queue and kick off real transfer */
918static void pump_messages(struct work_struct *work)
919{
Bryan Wu131b17d2007-12-04 23:45:12 -0800920 struct driver_data *drv_data;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700921 unsigned long flags;
922
Bryan Wu131b17d2007-12-04 23:45:12 -0800923 drv_data = container_of(work, struct driver_data, pump_messages);
924
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700925 /* Lock queue and check for queue work */
926 spin_lock_irqsave(&drv_data->lock, flags);
927 if (list_empty(&drv_data->queue) || drv_data->run == QUEUE_STOPPED) {
928 /* pumper kicked off but no work to do */
929 drv_data->busy = 0;
930 spin_unlock_irqrestore(&drv_data->lock, flags);
931 return;
932 }
933
934 /* Make sure we are not already running a message */
935 if (drv_data->cur_msg) {
936 spin_unlock_irqrestore(&drv_data->lock, flags);
937 return;
938 }
939
940 /* Extract head of queue */
941 drv_data->cur_msg = list_entry(drv_data->queue.next,
942 struct spi_message, queue);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800943
944 /* Setup the SSP using the per chip configuration */
945 drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
Bryan Wu8d20d0a2008-02-06 01:38:17 -0800946 restore_state(drv_data);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800947
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700948 list_del_init(&drv_data->cur_msg->queue);
949
950 /* Initial message state */
951 drv_data->cur_msg->state = START_STATE;
952 drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
953 struct spi_transfer, transfer_list);
954
Bryan Wu5fec5b52007-12-04 23:45:13 -0800955 dev_dbg(&drv_data->pdev->dev, "got a message to pump, "
956 "state is set to: baud %d, flag 0x%x, ctl 0x%x\n",
957 drv_data->cur_chip->baud, drv_data->cur_chip->flag,
958 drv_data->cur_chip->ctl_reg);
Bryan Wu131b17d2007-12-04 23:45:12 -0800959
960 dev_dbg(&drv_data->pdev->dev,
Bryan Wu88b40362007-05-21 18:32:16 +0800961 "the first transfer len is %d\n",
962 drv_data->cur_transfer->len);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700963
964 /* Mark as busy and launch transfers */
965 tasklet_schedule(&drv_data->pump_transfers);
966
967 drv_data->busy = 1;
968 spin_unlock_irqrestore(&drv_data->lock, flags);
969}
970
971/*
972 * got a msg to transfer, queue it in drv_data->queue.
973 * And kick off message pumper
974 */
975static int transfer(struct spi_device *spi, struct spi_message *msg)
976{
977 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
978 unsigned long flags;
979
980 spin_lock_irqsave(&drv_data->lock, flags);
981
982 if (drv_data->run == QUEUE_STOPPED) {
983 spin_unlock_irqrestore(&drv_data->lock, flags);
984 return -ESHUTDOWN;
985 }
986
987 msg->actual_length = 0;
988 msg->status = -EINPROGRESS;
989 msg->state = START_STATE;
990
Bryan Wu88b40362007-05-21 18:32:16 +0800991 dev_dbg(&spi->dev, "adding an msg in transfer() \n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700992 list_add_tail(&msg->queue, &drv_data->queue);
993
994 if (drv_data->run == QUEUE_RUNNING && !drv_data->busy)
995 queue_work(drv_data->workqueue, &drv_data->pump_messages);
996
997 spin_unlock_irqrestore(&drv_data->lock, flags);
998
999 return 0;
1000}
1001
Sonic Zhang12e17c42007-12-04 23:45:16 -08001002#define MAX_SPI_SSEL 7
1003
1004static u16 ssel[3][MAX_SPI_SSEL] = {
1005 {P_SPI0_SSEL1, P_SPI0_SSEL2, P_SPI0_SSEL3,
1006 P_SPI0_SSEL4, P_SPI0_SSEL5,
1007 P_SPI0_SSEL6, P_SPI0_SSEL7},
1008
1009 {P_SPI1_SSEL1, P_SPI1_SSEL2, P_SPI1_SSEL3,
1010 P_SPI1_SSEL4, P_SPI1_SSEL5,
1011 P_SPI1_SSEL6, P_SPI1_SSEL7},
1012
1013 {P_SPI2_SSEL1, P_SPI2_SSEL2, P_SPI2_SSEL3,
1014 P_SPI2_SSEL4, P_SPI2_SSEL5,
1015 P_SPI2_SSEL6, P_SPI2_SSEL7},
1016};
1017
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001018/* first setup for new devices */
1019static int setup(struct spi_device *spi)
1020{
1021 struct bfin5xx_spi_chip *chip_info = NULL;
1022 struct chip_data *chip;
1023 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
1024 u8 spi_flg;
1025
1026 /* Abort device setup if requested features are not supported */
1027 if (spi->mode & ~(SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST)) {
1028 dev_err(&spi->dev, "requested mode not fully supported\n");
1029 return -EINVAL;
1030 }
1031
1032 /* Zero (the default) here means 8 bits */
1033 if (!spi->bits_per_word)
1034 spi->bits_per_word = 8;
1035
1036 if (spi->bits_per_word != 8 && spi->bits_per_word != 16)
1037 return -EINVAL;
1038
1039 /* Only alloc (or use chip_info) on first setup */
1040 chip = spi_get_ctldata(spi);
1041 if (chip == NULL) {
1042 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
1043 if (!chip)
1044 return -ENOMEM;
1045
1046 chip->enable_dma = 0;
1047 chip_info = spi->controller_data;
1048 }
1049
1050 /* chip_info isn't always needed */
1051 if (chip_info) {
Mike Frysinger2ed35512007-12-04 23:45:14 -08001052 /* Make sure people stop trying to set fields via ctl_reg
1053 * when they should actually be using common SPI framework.
1054 * Currently we let through: WOM EMISO PSSE GM SZ TIMOD.
1055 * Not sure if a user actually needs/uses any of these,
1056 * but let's assume (for now) they do.
1057 */
1058 if (chip_info->ctl_reg & (SPE|MSTR|CPOL|CPHA|LSBF|SIZE)) {
1059 dev_err(&spi->dev, "do not set bits in ctl_reg "
1060 "that the SPI framework manages\n");
1061 return -EINVAL;
1062 }
1063
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001064 chip->enable_dma = chip_info->enable_dma != 0
1065 && drv_data->master_info->enable_dma;
1066 chip->ctl_reg = chip_info->ctl_reg;
1067 chip->bits_per_word = chip_info->bits_per_word;
1068 chip->cs_change_per_word = chip_info->cs_change_per_word;
1069 chip->cs_chg_udelay = chip_info->cs_chg_udelay;
1070 }
1071
1072 /* translate common spi framework into our register */
1073 if (spi->mode & SPI_CPOL)
1074 chip->ctl_reg |= CPOL;
1075 if (spi->mode & SPI_CPHA)
1076 chip->ctl_reg |= CPHA;
1077 if (spi->mode & SPI_LSB_FIRST)
1078 chip->ctl_reg |= LSBF;
1079 /* we dont support running in slave mode (yet?) */
1080 chip->ctl_reg |= MSTR;
1081
1082 /*
1083 * if any one SPI chip is registered and wants DMA, request the
1084 * DMA channel for it
1085 */
Bryan Wubb90eb02007-12-04 23:45:18 -08001086 if (chip->enable_dma && !drv_data->dma_requested) {
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001087 /* register dma irq handler */
Mike Frysinger59bfcc62009-04-06 19:00:37 -07001088 if (request_dma(drv_data->dma_channel, "BFIN_SPI_DMA") < 0) {
Bryan Wu88b40362007-05-21 18:32:16 +08001089 dev_dbg(&spi->dev,
1090 "Unable to request BlackFin SPI DMA channel\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001091 return -ENODEV;
1092 }
Bryan Wubb90eb02007-12-04 23:45:18 -08001093 if (set_dma_callback(drv_data->dma_channel,
Mike Frysinger59bfcc62009-04-06 19:00:37 -07001094 dma_irq_handler, drv_data) < 0) {
Bryan Wu88b40362007-05-21 18:32:16 +08001095 dev_dbg(&spi->dev, "Unable to set dma callback\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001096 return -EPERM;
1097 }
Bryan Wubb90eb02007-12-04 23:45:18 -08001098 dma_disable_irq(drv_data->dma_channel);
1099 drv_data->dma_requested = 1;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001100 }
1101
1102 /*
1103 * Notice: for blackfin, the speed_hz is the value of register
1104 * SPI_BAUD, not the real baudrate
1105 */
1106 chip->baud = hz_to_spi_baud(spi->max_speed_hz);
1107 spi_flg = ~(1 << (spi->chip_select));
1108 chip->flag = ((u16) spi_flg << 8) | (1 << (spi->chip_select));
1109 chip->chip_select_num = spi->chip_select;
1110
1111 switch (chip->bits_per_word) {
1112 case 8:
1113 chip->n_bytes = 1;
1114 chip->width = CFG_SPI_WORDSIZE8;
1115 chip->read = chip->cs_change_per_word ?
1116 u8_cs_chg_reader : u8_reader;
1117 chip->write = chip->cs_change_per_word ?
1118 u8_cs_chg_writer : u8_writer;
1119 chip->duplex = chip->cs_change_per_word ?
1120 u8_cs_chg_duplex : u8_duplex;
1121 break;
1122
1123 case 16:
1124 chip->n_bytes = 2;
1125 chip->width = CFG_SPI_WORDSIZE16;
1126 chip->read = chip->cs_change_per_word ?
1127 u16_cs_chg_reader : u16_reader;
1128 chip->write = chip->cs_change_per_word ?
1129 u16_cs_chg_writer : u16_writer;
1130 chip->duplex = chip->cs_change_per_word ?
1131 u16_cs_chg_duplex : u16_duplex;
1132 break;
1133
1134 default:
1135 dev_err(&spi->dev, "%d bits_per_word is not supported\n",
1136 chip->bits_per_word);
1137 kfree(chip);
1138 return -ENODEV;
1139 }
1140
Joe Perches898eb712007-10-18 03:06:30 -07001141 dev_dbg(&spi->dev, "setup spi chip %s, width is %d, dma is %d\n",
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001142 spi->modalias, chip->width, chip->enable_dma);
Bryan Wu88b40362007-05-21 18:32:16 +08001143 dev_dbg(&spi->dev, "ctl_reg is 0x%x, flag_reg is 0x%x\n",
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001144 chip->ctl_reg, chip->flag);
1145
1146 spi_set_ctldata(spi, chip);
1147
Sonic Zhang12e17c42007-12-04 23:45:16 -08001148 dev_dbg(&spi->dev, "chip select number is %d\n", chip->chip_select_num);
1149 if ((chip->chip_select_num > 0)
1150 && (chip->chip_select_num <= spi->master->num_chipselect))
1151 peripheral_request(ssel[spi->master->bus_num]
Bryan Wuaab0d832008-02-06 01:38:17 -08001152 [chip->chip_select_num-1], spi->modalias);
Sonic Zhang12e17c42007-12-04 23:45:16 -08001153
Sonic Zhang07612e52007-12-04 23:45:21 -08001154 cs_deactive(drv_data, chip);
1155
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001156 return 0;
1157}
1158
1159/*
1160 * callback for spi framework.
1161 * clean driver specific data
1162 */
Bryan Wu88b40362007-05-21 18:32:16 +08001163static void cleanup(struct spi_device *spi)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001164{
Mike Frysinger27bb9e72007-06-11 15:31:30 +08001165 struct chip_data *chip = spi_get_ctldata(spi);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001166
Sonic Zhang12e17c42007-12-04 23:45:16 -08001167 if ((chip->chip_select_num > 0)
1168 && (chip->chip_select_num <= spi->master->num_chipselect))
1169 peripheral_free(ssel[spi->master->bus_num]
1170 [chip->chip_select_num-1]);
1171
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001172 kfree(chip);
1173}
1174
1175static inline int init_queue(struct driver_data *drv_data)
1176{
1177 INIT_LIST_HEAD(&drv_data->queue);
1178 spin_lock_init(&drv_data->lock);
1179
1180 drv_data->run = QUEUE_STOPPED;
1181 drv_data->busy = 0;
1182
1183 /* init transfer tasklet */
1184 tasklet_init(&drv_data->pump_transfers,
1185 pump_transfers, (unsigned long)drv_data);
1186
1187 /* init messages workqueue */
1188 INIT_WORK(&drv_data->pump_messages, pump_messages);
Kay Sievers6c7377a2009-03-24 16:38:21 -07001189 drv_data->workqueue = create_singlethread_workqueue(
1190 dev_name(drv_data->master->dev.parent));
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001191 if (drv_data->workqueue == NULL)
1192 return -EBUSY;
1193
1194 return 0;
1195}
1196
1197static inline int start_queue(struct driver_data *drv_data)
1198{
1199 unsigned long flags;
1200
1201 spin_lock_irqsave(&drv_data->lock, flags);
1202
1203 if (drv_data->run == QUEUE_RUNNING || drv_data->busy) {
1204 spin_unlock_irqrestore(&drv_data->lock, flags);
1205 return -EBUSY;
1206 }
1207
1208 drv_data->run = QUEUE_RUNNING;
1209 drv_data->cur_msg = NULL;
1210 drv_data->cur_transfer = NULL;
1211 drv_data->cur_chip = NULL;
1212 spin_unlock_irqrestore(&drv_data->lock, flags);
1213
1214 queue_work(drv_data->workqueue, &drv_data->pump_messages);
1215
1216 return 0;
1217}
1218
1219static inline int stop_queue(struct driver_data *drv_data)
1220{
1221 unsigned long flags;
1222 unsigned limit = 500;
1223 int status = 0;
1224
1225 spin_lock_irqsave(&drv_data->lock, flags);
1226
1227 /*
1228 * This is a bit lame, but is optimized for the common execution path.
1229 * A wait_queue on the drv_data->busy could be used, but then the common
1230 * execution path (pump_messages) would be required to call wake_up or
1231 * friends on every SPI message. Do this instead
1232 */
1233 drv_data->run = QUEUE_STOPPED;
1234 while (!list_empty(&drv_data->queue) && drv_data->busy && limit--) {
1235 spin_unlock_irqrestore(&drv_data->lock, flags);
1236 msleep(10);
1237 spin_lock_irqsave(&drv_data->lock, flags);
1238 }
1239
1240 if (!list_empty(&drv_data->queue) || drv_data->busy)
1241 status = -EBUSY;
1242
1243 spin_unlock_irqrestore(&drv_data->lock, flags);
1244
1245 return status;
1246}
1247
1248static inline int destroy_queue(struct driver_data *drv_data)
1249{
1250 int status;
1251
1252 status = stop_queue(drv_data);
1253 if (status != 0)
1254 return status;
1255
1256 destroy_workqueue(drv_data->workqueue);
1257
1258 return 0;
1259}
1260
1261static int __init bfin5xx_spi_probe(struct platform_device *pdev)
1262{
1263 struct device *dev = &pdev->dev;
1264 struct bfin5xx_spi_master *platform_info;
1265 struct spi_master *master;
1266 struct driver_data *drv_data = 0;
Bryan Wua32c6912007-12-04 23:45:15 -08001267 struct resource *res;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001268 int status = 0;
1269
1270 platform_info = dev->platform_data;
1271
1272 /* Allocate master with space for drv_data */
1273 master = spi_alloc_master(dev, sizeof(struct driver_data) + 16);
1274 if (!master) {
1275 dev_err(&pdev->dev, "can not alloc spi_master\n");
1276 return -ENOMEM;
1277 }
Bryan Wu131b17d2007-12-04 23:45:12 -08001278
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001279 drv_data = spi_master_get_devdata(master);
1280 drv_data->master = master;
1281 drv_data->master_info = platform_info;
1282 drv_data->pdev = pdev;
Bryan Wu003d9222007-12-04 23:45:22 -08001283 drv_data->pin_req = platform_info->pin_req;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001284
1285 master->bus_num = pdev->id;
1286 master->num_chipselect = platform_info->num_chipselect;
1287 master->cleanup = cleanup;
1288 master->setup = setup;
1289 master->transfer = transfer;
1290
Bryan Wua32c6912007-12-04 23:45:15 -08001291 /* Find and map our resources */
1292 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1293 if (res == NULL) {
1294 dev_err(dev, "Cannot get IORESOURCE_MEM\n");
1295 status = -ENOENT;
1296 goto out_error_get_res;
1297 }
1298
Bryan Wuf4521262007-12-04 23:45:22 -08001299 drv_data->regs_base = ioremap(res->start, (res->end - res->start + 1));
1300 if (drv_data->regs_base == NULL) {
Bryan Wua32c6912007-12-04 23:45:15 -08001301 dev_err(dev, "Cannot map IO\n");
1302 status = -ENXIO;
1303 goto out_error_ioremap;
1304 }
1305
Bryan Wubb90eb02007-12-04 23:45:18 -08001306 drv_data->dma_channel = platform_get_irq(pdev, 0);
1307 if (drv_data->dma_channel < 0) {
Bryan Wua32c6912007-12-04 23:45:15 -08001308 dev_err(dev, "No DMA channel specified\n");
1309 status = -ENOENT;
1310 goto out_error_no_dma_ch;
1311 }
1312
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001313 /* Initial and start queue */
1314 status = init_queue(drv_data);
1315 if (status != 0) {
Bryan Wua32c6912007-12-04 23:45:15 -08001316 dev_err(dev, "problem initializing queue\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001317 goto out_error_queue_alloc;
1318 }
Bryan Wua32c6912007-12-04 23:45:15 -08001319
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001320 status = start_queue(drv_data);
1321 if (status != 0) {
Bryan Wua32c6912007-12-04 23:45:15 -08001322 dev_err(dev, "problem starting queue\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001323 goto out_error_queue_alloc;
1324 }
1325
Vitja Makarovf9e522c2008-04-08 17:41:57 -07001326 status = peripheral_request_list(drv_data->pin_req, DRV_NAME);
1327 if (status != 0) {
1328 dev_err(&pdev->dev, ": Requesting Peripherals failed\n");
1329 goto out_error_queue_alloc;
1330 }
1331
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001332 /* Register with the SPI framework */
1333 platform_set_drvdata(pdev, drv_data);
1334 status = spi_register_master(master);
1335 if (status != 0) {
Bryan Wua32c6912007-12-04 23:45:15 -08001336 dev_err(dev, "problem registering spi master\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001337 goto out_error_queue_alloc;
1338 }
Bryan Wua32c6912007-12-04 23:45:15 -08001339
Bryan Wuf4521262007-12-04 23:45:22 -08001340 dev_info(dev, "%s, Version %s, regs_base@%p, dma channel@%d\n",
Bryan Wubb90eb02007-12-04 23:45:18 -08001341 DRV_DESC, DRV_VERSION, drv_data->regs_base,
1342 drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001343 return status;
1344
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001345out_error_queue_alloc:
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001346 destroy_queue(drv_data);
Bryan Wua32c6912007-12-04 23:45:15 -08001347out_error_no_dma_ch:
Bryan Wubb90eb02007-12-04 23:45:18 -08001348 iounmap((void *) drv_data->regs_base);
Bryan Wua32c6912007-12-04 23:45:15 -08001349out_error_ioremap:
1350out_error_get_res:
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001351 spi_master_put(master);
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001352
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001353 return status;
1354}
1355
1356/* stop hardware and remove the driver */
1357static int __devexit bfin5xx_spi_remove(struct platform_device *pdev)
1358{
1359 struct driver_data *drv_data = platform_get_drvdata(pdev);
1360 int status = 0;
1361
1362 if (!drv_data)
1363 return 0;
1364
1365 /* Remove the queue */
1366 status = destroy_queue(drv_data);
1367 if (status != 0)
1368 return status;
1369
1370 /* Disable the SSP at the peripheral and SOC level */
1371 bfin_spi_disable(drv_data);
1372
1373 /* Release DMA */
1374 if (drv_data->master_info->enable_dma) {
Bryan Wubb90eb02007-12-04 23:45:18 -08001375 if (dma_channel_active(drv_data->dma_channel))
1376 free_dma(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001377 }
1378
1379 /* Disconnect from the SPI framework */
1380 spi_unregister_master(drv_data->master);
1381
Bryan Wu003d9222007-12-04 23:45:22 -08001382 peripheral_free_list(drv_data->pin_req);
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001383
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001384 /* Prevent double remove */
1385 platform_set_drvdata(pdev, NULL);
1386
1387 return 0;
1388}
1389
1390#ifdef CONFIG_PM
1391static int bfin5xx_spi_suspend(struct platform_device *pdev, pm_message_t state)
1392{
1393 struct driver_data *drv_data = platform_get_drvdata(pdev);
1394 int status = 0;
1395
1396 status = stop_queue(drv_data);
1397 if (status != 0)
1398 return status;
1399
1400 /* stop hardware */
1401 bfin_spi_disable(drv_data);
1402
1403 return 0;
1404}
1405
1406static int bfin5xx_spi_resume(struct platform_device *pdev)
1407{
1408 struct driver_data *drv_data = platform_get_drvdata(pdev);
1409 int status = 0;
1410
1411 /* Enable the SPI interface */
1412 bfin_spi_enable(drv_data);
1413
1414 /* Start the queue running */
1415 status = start_queue(drv_data);
1416 if (status != 0) {
1417 dev_err(&pdev->dev, "problem starting queue (%d)\n", status);
1418 return status;
1419 }
1420
1421 return 0;
1422}
1423#else
1424#define bfin5xx_spi_suspend NULL
1425#define bfin5xx_spi_resume NULL
1426#endif /* CONFIG_PM */
1427
Kay Sievers7e38c3c2008-04-10 21:29:20 -07001428MODULE_ALIAS("platform:bfin-spi");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001429static struct platform_driver bfin5xx_spi_driver = {
David Brownellfc3ba952007-08-30 23:56:24 -07001430 .driver = {
Bryan Wua32c6912007-12-04 23:45:15 -08001431 .name = DRV_NAME,
Bryan Wu88b40362007-05-21 18:32:16 +08001432 .owner = THIS_MODULE,
1433 },
1434 .suspend = bfin5xx_spi_suspend,
1435 .resume = bfin5xx_spi_resume,
1436 .remove = __devexit_p(bfin5xx_spi_remove),
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001437};
1438
1439static int __init bfin5xx_spi_init(void)
1440{
Bryan Wu88b40362007-05-21 18:32:16 +08001441 return platform_driver_probe(&bfin5xx_spi_driver, bfin5xx_spi_probe);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001442}
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001443module_init(bfin5xx_spi_init);
1444
1445static void __exit bfin5xx_spi_exit(void)
1446{
1447 platform_driver_unregister(&bfin5xx_spi_driver);
1448}
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001449module_exit(bfin5xx_spi_exit);