blob: 122292557bf35890dfcc868974ef769975ed9f86 [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
Yi Li2cf36832009-04-06 19:00:44 -0700192 flag &= ~chip->flag;
Bryan Wufad91c82007-12-04 23:45:14 -0800193 flag |= (chip->flag << 8);
194
Bryan Wubb90eb02007-12-04 23:45:18 -0800195 write_FLAG(drv_data, flag);
Bryan Wu62310e52007-12-04 23:45:20 -0800196
197 /* Move delay here for consistency */
198 if (chip->cs_chg_udelay)
199 udelay(chip->cs_chg_udelay);
Bryan Wufad91c82007-12-04 23:45:14 -0800200}
201
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700202/* stop controller and re-config current chip*/
Bryan Wu8d20d0a2008-02-06 01:38:17 -0800203static void restore_state(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700204{
205 struct chip_data *chip = drv_data->cur_chip;
206
207 /* Clear status and disable clock */
Bryan Wubb90eb02007-12-04 23:45:18 -0800208 write_STAT(drv_data, BIT_STAT_CLR);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700209 bfin_spi_disable(drv_data);
Bryan Wu88b40362007-05-21 18:32:16 +0800210 dev_dbg(&drv_data->pdev->dev, "restoring spi ctl state\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700211
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700212 /* Load the registers */
Bryan Wubb90eb02007-12-04 23:45:18 -0800213 write_CTRL(drv_data, chip->ctl_reg);
Bryan Wu092e1fd2007-12-04 23:45:23 -0800214 write_BAUD(drv_data, chip->baud);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800215
216 bfin_spi_enable(drv_data);
Sonic Zhang07612e52007-12-04 23:45:21 -0800217 cs_active(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700218}
219
220/* used to kick off transfer in rx mode */
Bryan Wubb90eb02007-12-04 23:45:18 -0800221static unsigned short dummy_read(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700222{
223 unsigned short tmp;
Bryan Wubb90eb02007-12-04 23:45:18 -0800224 tmp = read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700225 return tmp;
226}
227
228static void null_writer(struct driver_data *drv_data)
229{
230 u8 n_bytes = drv_data->n_bytes;
231
232 while (drv_data->tx < drv_data->tx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800233 write_TDBR(drv_data, 0);
234 while ((read_STAT(drv_data) & BIT_STAT_TXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800235 cpu_relax();
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700236 drv_data->tx += n_bytes;
237 }
238}
239
240static void null_reader(struct driver_data *drv_data)
241{
242 u8 n_bytes = drv_data->n_bytes;
Bryan Wubb90eb02007-12-04 23:45:18 -0800243 dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700244
245 while (drv_data->rx < drv_data->rx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800246 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800247 cpu_relax();
Bryan Wubb90eb02007-12-04 23:45:18 -0800248 dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700249 drv_data->rx += n_bytes;
250 }
251}
252
253static void u8_writer(struct driver_data *drv_data)
254{
Bryan Wu131b17d2007-12-04 23:45:12 -0800255 dev_dbg(&drv_data->pdev->dev,
Bryan Wubb90eb02007-12-04 23:45:18 -0800256 "cr8-s is 0x%x\n", read_STAT(drv_data));
Sonic Zhangcc487e72007-12-04 23:45:17 -0800257
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700258 while (drv_data->tx < drv_data->tx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800259 write_TDBR(drv_data, (*(u8 *) (drv_data->tx)));
260 while (read_STAT(drv_data) & BIT_STAT_TXS)
Bryan Wud8c05002007-12-04 23:45:21 -0800261 cpu_relax();
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700262 ++drv_data->tx;
263 }
Sonic Zhang13f3e6422008-02-06 01:38:20 -0800264
265 /* poll for SPI completion before return */
266 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
267 cpu_relax();
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700268}
269
270static void u8_cs_chg_writer(struct driver_data *drv_data)
271{
272 struct chip_data *chip = drv_data->cur_chip;
273
274 while (drv_data->tx < drv_data->tx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800275 cs_active(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700276
Bryan Wubb90eb02007-12-04 23:45:18 -0800277 write_TDBR(drv_data, (*(u8 *) (drv_data->tx)));
278 while (read_STAT(drv_data) & BIT_STAT_TXS)
Bryan Wud8c05002007-12-04 23:45:21 -0800279 cpu_relax();
Bryan Wue26aa012008-02-06 01:38:18 -0800280 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
281 cpu_relax();
Bryan Wu62310e52007-12-04 23:45:20 -0800282
Bryan Wubb90eb02007-12-04 23:45:18 -0800283 cs_deactive(drv_data, chip);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800284
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700285 ++drv_data->tx;
286 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700287}
288
289static void u8_reader(struct driver_data *drv_data)
290{
Bryan Wu131b17d2007-12-04 23:45:12 -0800291 dev_dbg(&drv_data->pdev->dev,
Bryan Wubb90eb02007-12-04 23:45:18 -0800292 "cr-8 is 0x%x\n", read_STAT(drv_data));
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700293
Sonic Zhang3f479a62007-12-04 23:45:18 -0800294 /* poll for SPI completion before start */
Bryan Wubb90eb02007-12-04 23:45:18 -0800295 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Bryan Wud8c05002007-12-04 23:45:21 -0800296 cpu_relax();
Sonic Zhang3f479a62007-12-04 23:45:18 -0800297
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700298 /* clear TDBR buffer before read(else it will be shifted out) */
Bryan Wubb90eb02007-12-04 23:45:18 -0800299 write_TDBR(drv_data, 0xFFFF);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700300
Bryan Wubb90eb02007-12-04 23:45:18 -0800301 dummy_read(drv_data);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800302
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700303 while (drv_data->rx < drv_data->rx_end - 1) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800304 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800305 cpu_relax();
Bryan Wubb90eb02007-12-04 23:45:18 -0800306 *(u8 *) (drv_data->rx) = read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700307 ++drv_data->rx;
308 }
309
Bryan Wubb90eb02007-12-04 23:45:18 -0800310 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800311 cpu_relax();
Bryan Wubb90eb02007-12-04 23:45:18 -0800312 *(u8 *) (drv_data->rx) = read_SHAW(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700313 ++drv_data->rx;
314}
315
316static void u8_cs_chg_reader(struct driver_data *drv_data)
317{
318 struct chip_data *chip = drv_data->cur_chip;
319
Bryan Wue26aa012008-02-06 01:38:18 -0800320 while (drv_data->rx < drv_data->rx_end) {
321 cs_active(drv_data, chip);
322 read_RDBR(drv_data); /* kick off */
Bryan Wu5fec5b52007-12-04 23:45:13 -0800323
Bryan Wubb90eb02007-12-04 23:45:18 -0800324 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800325 cpu_relax();
Bryan Wue26aa012008-02-06 01:38:18 -0800326 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
327 cpu_relax();
328
329 *(u8 *) (drv_data->rx) = read_SHAW(drv_data);
330 cs_deactive(drv_data, chip);
331
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700332 ++drv_data->rx;
333 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700334}
335
336static void u8_duplex(struct driver_data *drv_data)
337{
338 /* in duplex mode, clk is triggered by writing of TDBR */
339 while (drv_data->rx < drv_data->rx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800340 write_TDBR(drv_data, (*(u8 *) (drv_data->tx)));
Bryan Wu4fd432d2008-02-06 01:38:19 -0800341 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Bryan Wud8c05002007-12-04 23:45:21 -0800342 cpu_relax();
Bryan Wubb90eb02007-12-04 23:45:18 -0800343 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800344 cpu_relax();
Bryan Wubb90eb02007-12-04 23:45:18 -0800345 *(u8 *) (drv_data->rx) = read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700346 ++drv_data->rx;
347 ++drv_data->tx;
348 }
349}
350
351static void u8_cs_chg_duplex(struct driver_data *drv_data)
352{
353 struct chip_data *chip = drv_data->cur_chip;
354
355 while (drv_data->rx < drv_data->rx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800356 cs_active(drv_data, chip);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800357
Bryan Wubb90eb02007-12-04 23:45:18 -0800358 write_TDBR(drv_data, (*(u8 *) (drv_data->tx)));
Bryan Wue26aa012008-02-06 01:38:18 -0800359
360 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Bryan Wud8c05002007-12-04 23:45:21 -0800361 cpu_relax();
Bryan Wubb90eb02007-12-04 23:45:18 -0800362 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800363 cpu_relax();
Bryan Wubb90eb02007-12-04 23:45:18 -0800364 *(u8 *) (drv_data->rx) = read_RDBR(drv_data);
Bryan Wu62310e52007-12-04 23:45:20 -0800365
Bryan Wubb90eb02007-12-04 23:45:18 -0800366 cs_deactive(drv_data, chip);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800367
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700368 ++drv_data->rx;
369 ++drv_data->tx;
370 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700371}
372
373static void u16_writer(struct driver_data *drv_data)
374{
Bryan Wu131b17d2007-12-04 23:45:12 -0800375 dev_dbg(&drv_data->pdev->dev,
Bryan Wubb90eb02007-12-04 23:45:18 -0800376 "cr16 is 0x%x\n", read_STAT(drv_data));
Bryan Wu88b40362007-05-21 18:32:16 +0800377
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700378 while (drv_data->tx < drv_data->tx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800379 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
380 while ((read_STAT(drv_data) & BIT_STAT_TXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800381 cpu_relax();
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700382 drv_data->tx += 2;
383 }
Sonic Zhang13f3e6422008-02-06 01:38:20 -0800384
385 /* poll for SPI completion before return */
386 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
387 cpu_relax();
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700388}
389
390static void u16_cs_chg_writer(struct driver_data *drv_data)
391{
392 struct chip_data *chip = drv_data->cur_chip;
393
394 while (drv_data->tx < drv_data->tx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800395 cs_active(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700396
Bryan Wubb90eb02007-12-04 23:45:18 -0800397 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
398 while ((read_STAT(drv_data) & BIT_STAT_TXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800399 cpu_relax();
Sonic Zhang13f3e6422008-02-06 01:38:20 -0800400 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
401 cpu_relax();
Bryan Wu62310e52007-12-04 23:45:20 -0800402
Bryan Wubb90eb02007-12-04 23:45:18 -0800403 cs_deactive(drv_data, chip);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800404
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700405 drv_data->tx += 2;
406 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700407}
408
409static void u16_reader(struct driver_data *drv_data)
410{
Bryan Wu88b40362007-05-21 18:32:16 +0800411 dev_dbg(&drv_data->pdev->dev,
Bryan Wubb90eb02007-12-04 23:45:18 -0800412 "cr-16 is 0x%x\n", read_STAT(drv_data));
Sonic Zhangcc487e72007-12-04 23:45:17 -0800413
Sonic Zhang3f479a62007-12-04 23:45:18 -0800414 /* poll for SPI completion before start */
Bryan Wubb90eb02007-12-04 23:45:18 -0800415 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Bryan Wud8c05002007-12-04 23:45:21 -0800416 cpu_relax();
Sonic Zhang3f479a62007-12-04 23:45:18 -0800417
Sonic Zhangcc487e72007-12-04 23:45:17 -0800418 /* clear TDBR buffer before read(else it will be shifted out) */
Bryan Wubb90eb02007-12-04 23:45:18 -0800419 write_TDBR(drv_data, 0xFFFF);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800420
Bryan Wubb90eb02007-12-04 23:45:18 -0800421 dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700422
423 while (drv_data->rx < (drv_data->rx_end - 2)) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800424 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800425 cpu_relax();
Bryan Wubb90eb02007-12-04 23:45:18 -0800426 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700427 drv_data->rx += 2;
428 }
429
Bryan Wubb90eb02007-12-04 23:45:18 -0800430 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800431 cpu_relax();
Bryan Wubb90eb02007-12-04 23:45:18 -0800432 *(u16 *) (drv_data->rx) = read_SHAW(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700433 drv_data->rx += 2;
434}
435
436static void u16_cs_chg_reader(struct driver_data *drv_data)
437{
438 struct chip_data *chip = drv_data->cur_chip;
439
Sonic Zhang3f479a62007-12-04 23:45:18 -0800440 /* poll for SPI completion before start */
Bryan Wubb90eb02007-12-04 23:45:18 -0800441 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Bryan Wud8c05002007-12-04 23:45:21 -0800442 cpu_relax();
Sonic Zhang3f479a62007-12-04 23:45:18 -0800443
Sonic Zhangcc487e72007-12-04 23:45:17 -0800444 /* clear TDBR buffer before read(else it will be shifted out) */
Bryan Wubb90eb02007-12-04 23:45:18 -0800445 write_TDBR(drv_data, 0xFFFF);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700446
Bryan Wubb90eb02007-12-04 23:45:18 -0800447 cs_active(drv_data, chip);
448 dummy_read(drv_data);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800449
Bryan Wuc3061ab2007-12-04 23:45:19 -0800450 while (drv_data->rx < drv_data->rx_end - 2) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800451 cs_deactive(drv_data, chip);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800452
Bryan Wubb90eb02007-12-04 23:45:18 -0800453 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800454 cpu_relax();
Bryan Wubb90eb02007-12-04 23:45:18 -0800455 cs_active(drv_data, chip);
456 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700457 drv_data->rx += 2;
458 }
Bryan Wubb90eb02007-12-04 23:45:18 -0800459 cs_deactive(drv_data, chip);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800460
Bryan Wubb90eb02007-12-04 23:45:18 -0800461 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800462 cpu_relax();
Bryan Wubb90eb02007-12-04 23:45:18 -0800463 *(u16 *) (drv_data->rx) = read_SHAW(drv_data);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800464 drv_data->rx += 2;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700465}
466
467static void u16_duplex(struct driver_data *drv_data)
468{
469 /* in duplex mode, clk is triggered by writing of TDBR */
470 while (drv_data->tx < drv_data->tx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800471 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
Bryan Wu4fd432d2008-02-06 01:38:19 -0800472 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Bryan Wud8c05002007-12-04 23:45:21 -0800473 cpu_relax();
Bryan Wubb90eb02007-12-04 23:45:18 -0800474 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800475 cpu_relax();
Bryan Wubb90eb02007-12-04 23:45:18 -0800476 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700477 drv_data->rx += 2;
478 drv_data->tx += 2;
479 }
480}
481
482static void u16_cs_chg_duplex(struct driver_data *drv_data)
483{
484 struct chip_data *chip = drv_data->cur_chip;
485
486 while (drv_data->tx < drv_data->tx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800487 cs_active(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700488
Bryan Wubb90eb02007-12-04 23:45:18 -0800489 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
Bryan Wu4fd432d2008-02-06 01:38:19 -0800490 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Bryan Wud8c05002007-12-04 23:45:21 -0800491 cpu_relax();
Bryan Wubb90eb02007-12-04 23:45:18 -0800492 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Bryan Wud8c05002007-12-04 23:45:21 -0800493 cpu_relax();
Bryan Wubb90eb02007-12-04 23:45:18 -0800494 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
Bryan Wu62310e52007-12-04 23:45:20 -0800495
Bryan Wubb90eb02007-12-04 23:45:18 -0800496 cs_deactive(drv_data, chip);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800497
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700498 drv_data->rx += 2;
499 drv_data->tx += 2;
500 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700501}
502
503/* test if ther is more transfer to be done */
504static void *next_transfer(struct driver_data *drv_data)
505{
506 struct spi_message *msg = drv_data->cur_msg;
507 struct spi_transfer *trans = drv_data->cur_transfer;
508
509 /* Move to next transfer */
510 if (trans->transfer_list.next != &msg->transfers) {
511 drv_data->cur_transfer =
512 list_entry(trans->transfer_list.next,
513 struct spi_transfer, transfer_list);
514 return RUNNING_STATE;
515 } else
516 return DONE_STATE;
517}
518
519/*
520 * caller already set message->status;
521 * dma and pio irqs are blocked give finished message back
522 */
523static void giveback(struct driver_data *drv_data)
524{
Bryan Wufad91c82007-12-04 23:45:14 -0800525 struct chip_data *chip = drv_data->cur_chip;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700526 struct spi_transfer *last_transfer;
527 unsigned long flags;
528 struct spi_message *msg;
529
530 spin_lock_irqsave(&drv_data->lock, flags);
531 msg = drv_data->cur_msg;
532 drv_data->cur_msg = NULL;
533 drv_data->cur_transfer = NULL;
534 drv_data->cur_chip = NULL;
535 queue_work(drv_data->workqueue, &drv_data->pump_messages);
536 spin_unlock_irqrestore(&drv_data->lock, flags);
537
538 last_transfer = list_entry(msg->transfers.prev,
539 struct spi_transfer, transfer_list);
540
541 msg->state = NULL;
542
543 /* disable chip select signal. And not stop spi in autobuffer mode */
544 if (drv_data->tx_dma != 0xFFFF) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800545 cs_deactive(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700546 bfin_spi_disable(drv_data);
547 }
548
Bryan Wufad91c82007-12-04 23:45:14 -0800549 if (!drv_data->cs_change)
Bryan Wubb90eb02007-12-04 23:45:18 -0800550 cs_deactive(drv_data, chip);
Bryan Wufad91c82007-12-04 23:45:14 -0800551
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700552 if (msg->complete)
553 msg->complete(msg->context);
554}
555
Bryan Wu88b40362007-05-21 18:32:16 +0800556static irqreturn_t dma_irq_handler(int irq, void *dev_id)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700557{
Jeff Garzik15aafa22008-02-06 01:36:20 -0800558 struct driver_data *drv_data = dev_id;
Bryan Wufad91c82007-12-04 23:45:14 -0800559 struct chip_data *chip = drv_data->cur_chip;
Bryan Wubb90eb02007-12-04 23:45:18 -0800560 struct spi_message *msg = drv_data->cur_msg;
Mike Frysingeraaaf9392009-04-06 19:00:42 -0700561 unsigned long timeout;
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
Mike Frysingeraaaf9392009-04-06 19:00:42 -0700587 dev_dbg(&drv_data->pdev->dev,
588 "in dma_irq_handler dmastat:0x%x spistat:0x%x\n",
589 dmastat, read_STAT(drv_data));
590
591 timeout = jiffies + HZ;
Bryan Wubb90eb02007-12-04 23:45:18 -0800592 while (!(read_STAT(drv_data) & SPIF))
Mike Frysingeraaaf9392009-04-06 19:00:42 -0700593 if (!time_before(jiffies, timeout)) {
594 dev_warn(&drv_data->pdev->dev, "timeout waiting for SPIF");
595 break;
596 } else
597 cpu_relax();
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700598
Mike Frysinger40a29452009-04-06 19:00:38 -0700599 if ((dmastat & DMA_ERR) && (spistat & RBSY)) {
Mike Frysinger04b95d22009-04-06 19:00:35 -0700600 msg->state = ERROR_STATE;
601 dev_err(&drv_data->pdev->dev, "dma receive: fifo/buffer overflow\n");
602 } else {
603 msg->actual_length += drv_data->len_in_bytes;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700604
Mike Frysinger04b95d22009-04-06 19:00:35 -0700605 if (drv_data->cs_change)
606 cs_deactive(drv_data, chip);
Bryan Wufad91c82007-12-04 23:45:14 -0800607
Mike Frysinger04b95d22009-04-06 19:00:35 -0700608 /* Move to next transfer */
609 msg->state = next_transfer(drv_data);
610 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700611
612 /* Schedule transfer tasklet */
613 tasklet_schedule(&drv_data->pump_transfers);
614
615 /* free the irq handler before next transfer */
Bryan Wu88b40362007-05-21 18:32:16 +0800616 dev_dbg(&drv_data->pdev->dev,
617 "disable dma channel irq%d\n",
Bryan Wubb90eb02007-12-04 23:45:18 -0800618 drv_data->dma_channel);
619 dma_disable_irq(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700620
621 return IRQ_HANDLED;
622}
623
624static void pump_transfers(unsigned long data)
625{
626 struct driver_data *drv_data = (struct driver_data *)data;
627 struct spi_message *message = NULL;
628 struct spi_transfer *transfer = NULL;
629 struct spi_transfer *previous = NULL;
630 struct chip_data *chip = NULL;
Bryan Wu88b40362007-05-21 18:32:16 +0800631 u8 width;
632 u16 cr, dma_width, dma_config;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700633 u32 tranf_success = 1;
Vitja Makarov8eeb12e2008-05-01 04:35:03 -0700634 u8 full_duplex = 0;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700635
636 /* Get current state information */
637 message = drv_data->cur_msg;
638 transfer = drv_data->cur_transfer;
639 chip = drv_data->cur_chip;
Bryan Wu092e1fd2007-12-04 23:45:23 -0800640
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700641 /*
642 * if msg is error or done, report it back using complete() callback
643 */
644
645 /* Handle for abort */
646 if (message->state == ERROR_STATE) {
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700647 dev_dbg(&drv_data->pdev->dev, "transfer: we've hit an error\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700648 message->status = -EIO;
649 giveback(drv_data);
650 return;
651 }
652
653 /* Handle end of message */
654 if (message->state == DONE_STATE) {
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700655 dev_dbg(&drv_data->pdev->dev, "transfer: all done!\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700656 message->status = 0;
657 giveback(drv_data);
658 return;
659 }
660
661 /* Delay if requested at end of transfer */
662 if (message->state == RUNNING_STATE) {
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700663 dev_dbg(&drv_data->pdev->dev, "transfer: still running ...\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700664 previous = list_entry(transfer->transfer_list.prev,
665 struct spi_transfer, transfer_list);
666 if (previous->delay_usecs)
667 udelay(previous->delay_usecs);
668 }
669
670 /* Setup the transfer state based on the type of transfer */
671 if (flush(drv_data) == 0) {
672 dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
673 message->status = -EIO;
674 giveback(drv_data);
675 return;
676 }
677
678 if (transfer->tx_buf != NULL) {
679 drv_data->tx = (void *)transfer->tx_buf;
680 drv_data->tx_end = drv_data->tx + transfer->len;
Bryan Wu88b40362007-05-21 18:32:16 +0800681 dev_dbg(&drv_data->pdev->dev, "tx_buf is %p, tx_end is %p\n",
682 transfer->tx_buf, drv_data->tx_end);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700683 } else {
684 drv_data->tx = NULL;
685 }
686
687 if (transfer->rx_buf != NULL) {
Vitja Makarov8eeb12e2008-05-01 04:35:03 -0700688 full_duplex = transfer->tx_buf != NULL;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700689 drv_data->rx = transfer->rx_buf;
690 drv_data->rx_end = drv_data->rx + transfer->len;
Bryan Wu88b40362007-05-21 18:32:16 +0800691 dev_dbg(&drv_data->pdev->dev, "rx_buf is %p, rx_end is %p\n",
692 transfer->rx_buf, drv_data->rx_end);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700693 } else {
694 drv_data->rx = NULL;
695 }
696
697 drv_data->rx_dma = transfer->rx_dma;
698 drv_data->tx_dma = transfer->tx_dma;
699 drv_data->len_in_bytes = transfer->len;
Bryan Wufad91c82007-12-04 23:45:14 -0800700 drv_data->cs_change = transfer->cs_change;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700701
Bryan Wu092e1fd2007-12-04 23:45:23 -0800702 /* Bits per word setup */
703 switch (transfer->bits_per_word) {
704 case 8:
705 drv_data->n_bytes = 1;
706 width = CFG_SPI_WORDSIZE8;
707 drv_data->read = chip->cs_change_per_word ?
708 u8_cs_chg_reader : u8_reader;
709 drv_data->write = chip->cs_change_per_word ?
710 u8_cs_chg_writer : u8_writer;
711 drv_data->duplex = chip->cs_change_per_word ?
712 u8_cs_chg_duplex : u8_duplex;
713 break;
714
715 case 16:
716 drv_data->n_bytes = 2;
717 width = CFG_SPI_WORDSIZE16;
718 drv_data->read = chip->cs_change_per_word ?
719 u16_cs_chg_reader : u16_reader;
720 drv_data->write = chip->cs_change_per_word ?
721 u16_cs_chg_writer : u16_writer;
722 drv_data->duplex = chip->cs_change_per_word ?
723 u16_cs_chg_duplex : u16_duplex;
724 break;
725
726 default:
727 /* No change, the same as default setting */
728 drv_data->n_bytes = chip->n_bytes;
729 width = chip->width;
730 drv_data->write = drv_data->tx ? chip->write : null_writer;
731 drv_data->read = drv_data->rx ? chip->read : null_reader;
732 drv_data->duplex = chip->duplex ? chip->duplex : null_writer;
733 break;
734 }
735 cr = (read_CTRL(drv_data) & (~BIT_CTL_TIMOD));
736 cr |= (width << 8);
737 write_CTRL(drv_data, cr);
738
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700739 if (width == CFG_SPI_WORDSIZE16) {
740 drv_data->len = (transfer->len) >> 1;
741 } else {
742 drv_data->len = transfer->len;
743 }
Mike Frysinger4fb98ef2008-04-08 17:41:57 -0700744 dev_dbg(&drv_data->pdev->dev,
745 "transfer: drv_data->write is %p, chip->write is %p, null_wr is %p\n",
Bryan Wu131b17d2007-12-04 23:45:12 -0800746 drv_data->write, chip->write, null_writer);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700747
748 /* speed and width has been set on per message */
749 message->state = RUNNING_STATE;
750 dma_config = 0;
751
Bryan Wu092e1fd2007-12-04 23:45:23 -0800752 /* Speed setup (surely valid because already checked) */
753 if (transfer->speed_hz)
754 write_BAUD(drv_data, hz_to_spi_baud(transfer->speed_hz));
755 else
756 write_BAUD(drv_data, chip->baud);
757
Bryan Wubb90eb02007-12-04 23:45:18 -0800758 write_STAT(drv_data, BIT_STAT_CLR);
759 cr = (read_CTRL(drv_data) & (~BIT_CTL_TIMOD));
760 cs_active(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700761
Bryan Wu88b40362007-05-21 18:32:16 +0800762 dev_dbg(&drv_data->pdev->dev,
763 "now pumping a transfer: width is %d, len is %d\n",
764 width, transfer->len);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700765
766 /*
Vitja Makarov8cf58582009-04-06 19:00:31 -0700767 * Try to map dma buffer and do a dma transfer. If successful use,
768 * different way to r/w according to the enable_dma settings and if
769 * we are not doing a full duplex transfer (since the hardware does
770 * not support full duplex DMA transfers).
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700771 */
Vitja Makarov8eeb12e2008-05-01 04:35:03 -0700772 if (!full_duplex && drv_data->cur_chip->enable_dma
773 && drv_data->len > 6) {
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700774
Mike Frysinger11d6f592009-04-06 19:00:41 -0700775 unsigned long dma_start_addr, flags;
Mike Frysinger7aec3562009-04-06 19:00:36 -0700776
Bryan Wubb90eb02007-12-04 23:45:18 -0800777 disable_dma(drv_data->dma_channel);
778 clear_dma_irqstat(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700779
780 /* config dma channel */
Bryan Wu88b40362007-05-21 18:32:16 +0800781 dev_dbg(&drv_data->pdev->dev, "doing dma transfer\n");
Mike Frysinger7aec3562009-04-06 19:00:36 -0700782 set_dma_x_count(drv_data->dma_channel, drv_data->len);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700783 if (width == CFG_SPI_WORDSIZE16) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800784 set_dma_x_modify(drv_data->dma_channel, 2);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700785 dma_width = WDSIZE_16;
786 } else {
Bryan Wubb90eb02007-12-04 23:45:18 -0800787 set_dma_x_modify(drv_data->dma_channel, 1);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700788 dma_width = WDSIZE_8;
789 }
790
Sonic Zhang3f479a62007-12-04 23:45:18 -0800791 /* poll for SPI completion before start */
Bryan Wubb90eb02007-12-04 23:45:18 -0800792 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Bryan Wud8c05002007-12-04 23:45:21 -0800793 cpu_relax();
Sonic Zhang3f479a62007-12-04 23:45:18 -0800794
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700795 /* dirty hack for autobuffer DMA mode */
796 if (drv_data->tx_dma == 0xFFFF) {
Bryan Wu88b40362007-05-21 18:32:16 +0800797 dev_dbg(&drv_data->pdev->dev,
798 "doing autobuffer DMA out.\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700799
800 /* no irq in autobuffer mode */
801 dma_config =
802 (DMAFLOW_AUTO | RESTART | dma_width | DI_EN);
Bryan Wubb90eb02007-12-04 23:45:18 -0800803 set_dma_config(drv_data->dma_channel, dma_config);
804 set_dma_start_addr(drv_data->dma_channel,
Bryan Wua32c6912007-12-04 23:45:15 -0800805 (unsigned long)drv_data->tx);
Bryan Wubb90eb02007-12-04 23:45:18 -0800806 enable_dma(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700807
Sonic Zhang07612e52007-12-04 23:45:21 -0800808 /* start SPI transfer */
Mike Frysinger11d6f592009-04-06 19:00:41 -0700809 write_CTRL(drv_data, cr | BIT_CTL_TIMOD_DMA_TX);
Sonic Zhang07612e52007-12-04 23:45:21 -0800810
811 /* just return here, there can only be one transfer
812 * in this mode
813 */
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700814 message->status = 0;
815 giveback(drv_data);
816 return;
817 }
818
819 /* In dma mode, rx or tx must be NULL in one transfer */
Mike Frysinger7aec3562009-04-06 19:00:36 -0700820 dma_config = (RESTART | dma_width | DI_EN);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700821 if (drv_data->rx != NULL) {
822 /* set transfer mode, and enable SPI */
Mike Frysingerd24bd1d2009-04-06 19:00:38 -0700823 dev_dbg(&drv_data->pdev->dev, "doing DMA in to %p (size %zx)\n",
824 drv_data->rx, drv_data->len_in_bytes);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700825
Vitja Makarov8cf58582009-04-06 19:00:31 -0700826 /* invalidate caches, if needed */
827 if (bfin_addr_dcachable((unsigned long) drv_data->rx))
828 invalidate_dcache_range((unsigned long) drv_data->rx,
829 (unsigned long) (drv_data->rx +
Mike Frysingerace32862009-04-06 19:00:34 -0700830 drv_data->len_in_bytes));
Vitja Makarov8cf58582009-04-06 19:00:31 -0700831
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700832 /* clear tx reg soformer data is not shifted out */
Bryan Wubb90eb02007-12-04 23:45:18 -0800833 write_TDBR(drv_data, 0xFFFF);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700834
Mike Frysinger7aec3562009-04-06 19:00:36 -0700835 dma_config |= WNR;
836 dma_start_addr = (unsigned long)drv_data->rx;
Mike Frysingerb31e27a2009-04-06 19:00:39 -0700837 cr |= BIT_CTL_TIMOD_DMA_RX | BIT_CTL_SENDOPT;
Sonic Zhang07612e52007-12-04 23:45:21 -0800838
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700839 } else if (drv_data->tx != NULL) {
Bryan Wu88b40362007-05-21 18:32:16 +0800840 dev_dbg(&drv_data->pdev->dev, "doing DMA out.\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700841
Vitja Makarov8cf58582009-04-06 19:00:31 -0700842 /* flush caches, if needed */
843 if (bfin_addr_dcachable((unsigned long) drv_data->tx))
844 flush_dcache_range((unsigned long) drv_data->tx,
845 (unsigned long) (drv_data->tx +
Mike Frysingerace32862009-04-06 19:00:34 -0700846 drv_data->len_in_bytes));
Vitja Makarov8cf58582009-04-06 19:00:31 -0700847
Mike Frysinger7aec3562009-04-06 19:00:36 -0700848 dma_start_addr = (unsigned long)drv_data->tx;
Mike Frysingerb31e27a2009-04-06 19:00:39 -0700849 cr |= BIT_CTL_TIMOD_DMA_TX;
Sonic Zhang07612e52007-12-04 23:45:21 -0800850
Mike Frysinger7aec3562009-04-06 19:00:36 -0700851 } else
852 BUG();
853
Mike Frysinger11d6f592009-04-06 19:00:41 -0700854 /* oh man, here there be monsters ... and i dont mean the
855 * fluffy cute ones from pixar, i mean the kind that'll eat
856 * your data, kick your dog, and love it all. do *not* try
857 * and change these lines unless you (1) heavily test DMA
858 * with SPI flashes on a loaded system (e.g. ping floods),
859 * (2) know just how broken the DMA engine interaction with
860 * the SPI peripheral is, and (3) have someone else to blame
861 * when you screw it all up anyways.
862 */
Mike Frysinger7aec3562009-04-06 19:00:36 -0700863 set_dma_start_addr(drv_data->dma_channel, dma_start_addr);
Mike Frysinger11d6f592009-04-06 19:00:41 -0700864 set_dma_config(drv_data->dma_channel, dma_config);
865 local_irq_save(flags);
Mike Frysingera963ea82009-04-06 19:00:43 -0700866 SSYNC();
Mike Frysinger11d6f592009-04-06 19:00:41 -0700867 write_CTRL(drv_data, cr);
Mike Frysingera963ea82009-04-06 19:00:43 -0700868 enable_dma(drv_data->dma_channel);
Mike Frysinger11d6f592009-04-06 19:00:41 -0700869 dma_enable_irq(drv_data->dma_channel);
870 local_irq_restore(flags);
Mike Frysinger7aec3562009-04-06 19:00:36 -0700871
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700872 } else {
873 /* IO mode write then read */
Bryan Wu88b40362007-05-21 18:32:16 +0800874 dev_dbg(&drv_data->pdev->dev, "doing IO transfer\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700875
Vitja Makarov8eeb12e2008-05-01 04:35:03 -0700876 if (full_duplex) {
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700877 /* full duplex mode */
878 BUG_ON((drv_data->tx_end - drv_data->tx) !=
879 (drv_data->rx_end - drv_data->rx));
Bryan Wu88b40362007-05-21 18:32:16 +0800880 dev_dbg(&drv_data->pdev->dev,
881 "IO duplex: cr is 0x%x\n", cr);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700882
Sonic Zhangcc487e72007-12-04 23:45:17 -0800883 /* set SPI transfer mode */
Bryan Wubb90eb02007-12-04 23:45:18 -0800884 write_CTRL(drv_data, (cr | CFG_SPI_WRITE));
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700885
886 drv_data->duplex(drv_data);
887
888 if (drv_data->tx != drv_data->tx_end)
889 tranf_success = 0;
890 } else if (drv_data->tx != NULL) {
891 /* write only half duplex */
Bryan Wu131b17d2007-12-04 23:45:12 -0800892 dev_dbg(&drv_data->pdev->dev,
Bryan Wu88b40362007-05-21 18:32:16 +0800893 "IO write: cr is 0x%x\n", cr);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700894
Sonic Zhangcc487e72007-12-04 23:45:17 -0800895 /* set SPI transfer mode */
Bryan Wubb90eb02007-12-04 23:45:18 -0800896 write_CTRL(drv_data, (cr | CFG_SPI_WRITE));
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700897
898 drv_data->write(drv_data);
899
900 if (drv_data->tx != drv_data->tx_end)
901 tranf_success = 0;
902 } else if (drv_data->rx != NULL) {
903 /* read only half duplex */
Bryan Wu131b17d2007-12-04 23:45:12 -0800904 dev_dbg(&drv_data->pdev->dev,
Bryan Wu88b40362007-05-21 18:32:16 +0800905 "IO read: cr is 0x%x\n", cr);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700906
Sonic Zhangcc487e72007-12-04 23:45:17 -0800907 /* set SPI transfer mode */
Bryan Wubb90eb02007-12-04 23:45:18 -0800908 write_CTRL(drv_data, (cr | CFG_SPI_READ));
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700909
910 drv_data->read(drv_data);
911 if (drv_data->rx != drv_data->rx_end)
912 tranf_success = 0;
913 }
914
915 if (!tranf_success) {
Bryan Wu131b17d2007-12-04 23:45:12 -0800916 dev_dbg(&drv_data->pdev->dev,
Bryan Wu88b40362007-05-21 18:32:16 +0800917 "IO write error!\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700918 message->state = ERROR_STATE;
919 } else {
920 /* Update total byte transfered */
Mike Frysingerace32862009-04-06 19:00:34 -0700921 message->actual_length += drv_data->len_in_bytes;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700922
923 /* Move to next transfer of this msg */
924 message->state = next_transfer(drv_data);
925 }
926
927 /* Schedule next transfer tasklet */
928 tasklet_schedule(&drv_data->pump_transfers);
929
930 }
931}
932
933/* pop a msg from queue and kick off real transfer */
934static void pump_messages(struct work_struct *work)
935{
Bryan Wu131b17d2007-12-04 23:45:12 -0800936 struct driver_data *drv_data;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700937 unsigned long flags;
938
Bryan Wu131b17d2007-12-04 23:45:12 -0800939 drv_data = container_of(work, struct driver_data, pump_messages);
940
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700941 /* Lock queue and check for queue work */
942 spin_lock_irqsave(&drv_data->lock, flags);
943 if (list_empty(&drv_data->queue) || drv_data->run == QUEUE_STOPPED) {
944 /* pumper kicked off but no work to do */
945 drv_data->busy = 0;
946 spin_unlock_irqrestore(&drv_data->lock, flags);
947 return;
948 }
949
950 /* Make sure we are not already running a message */
951 if (drv_data->cur_msg) {
952 spin_unlock_irqrestore(&drv_data->lock, flags);
953 return;
954 }
955
956 /* Extract head of queue */
957 drv_data->cur_msg = list_entry(drv_data->queue.next,
958 struct spi_message, queue);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800959
960 /* Setup the SSP using the per chip configuration */
961 drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
Bryan Wu8d20d0a2008-02-06 01:38:17 -0800962 restore_state(drv_data);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800963
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700964 list_del_init(&drv_data->cur_msg->queue);
965
966 /* Initial message state */
967 drv_data->cur_msg->state = START_STATE;
968 drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
969 struct spi_transfer, transfer_list);
970
Bryan Wu5fec5b52007-12-04 23:45:13 -0800971 dev_dbg(&drv_data->pdev->dev, "got a message to pump, "
972 "state is set to: baud %d, flag 0x%x, ctl 0x%x\n",
973 drv_data->cur_chip->baud, drv_data->cur_chip->flag,
974 drv_data->cur_chip->ctl_reg);
Bryan Wu131b17d2007-12-04 23:45:12 -0800975
976 dev_dbg(&drv_data->pdev->dev,
Bryan Wu88b40362007-05-21 18:32:16 +0800977 "the first transfer len is %d\n",
978 drv_data->cur_transfer->len);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700979
980 /* Mark as busy and launch transfers */
981 tasklet_schedule(&drv_data->pump_transfers);
982
983 drv_data->busy = 1;
984 spin_unlock_irqrestore(&drv_data->lock, flags);
985}
986
987/*
988 * got a msg to transfer, queue it in drv_data->queue.
989 * And kick off message pumper
990 */
991static int transfer(struct spi_device *spi, struct spi_message *msg)
992{
993 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
994 unsigned long flags;
995
996 spin_lock_irqsave(&drv_data->lock, flags);
997
998 if (drv_data->run == QUEUE_STOPPED) {
999 spin_unlock_irqrestore(&drv_data->lock, flags);
1000 return -ESHUTDOWN;
1001 }
1002
1003 msg->actual_length = 0;
1004 msg->status = -EINPROGRESS;
1005 msg->state = START_STATE;
1006
Bryan Wu88b40362007-05-21 18:32:16 +08001007 dev_dbg(&spi->dev, "adding an msg in transfer() \n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001008 list_add_tail(&msg->queue, &drv_data->queue);
1009
1010 if (drv_data->run == QUEUE_RUNNING && !drv_data->busy)
1011 queue_work(drv_data->workqueue, &drv_data->pump_messages);
1012
1013 spin_unlock_irqrestore(&drv_data->lock, flags);
1014
1015 return 0;
1016}
1017
Sonic Zhang12e17c42007-12-04 23:45:16 -08001018#define MAX_SPI_SSEL 7
1019
Mike Frysinger4160bde2009-04-06 19:00:40 -07001020static u16 ssel[][MAX_SPI_SSEL] = {
Sonic Zhang12e17c42007-12-04 23:45:16 -08001021 {P_SPI0_SSEL1, P_SPI0_SSEL2, P_SPI0_SSEL3,
1022 P_SPI0_SSEL4, P_SPI0_SSEL5,
1023 P_SPI0_SSEL6, P_SPI0_SSEL7},
1024
1025 {P_SPI1_SSEL1, P_SPI1_SSEL2, P_SPI1_SSEL3,
1026 P_SPI1_SSEL4, P_SPI1_SSEL5,
1027 P_SPI1_SSEL6, P_SPI1_SSEL7},
1028
1029 {P_SPI2_SSEL1, P_SPI2_SSEL2, P_SPI2_SSEL3,
1030 P_SPI2_SSEL4, P_SPI2_SSEL5,
1031 P_SPI2_SSEL6, P_SPI2_SSEL7},
1032};
1033
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001034/* first setup for new devices */
1035static int setup(struct spi_device *spi)
1036{
1037 struct bfin5xx_spi_chip *chip_info = NULL;
1038 struct chip_data *chip;
1039 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001040
1041 /* Abort device setup if requested features are not supported */
1042 if (spi->mode & ~(SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST)) {
1043 dev_err(&spi->dev, "requested mode not fully supported\n");
1044 return -EINVAL;
1045 }
1046
1047 /* Zero (the default) here means 8 bits */
1048 if (!spi->bits_per_word)
1049 spi->bits_per_word = 8;
1050
1051 if (spi->bits_per_word != 8 && spi->bits_per_word != 16)
1052 return -EINVAL;
1053
1054 /* Only alloc (or use chip_info) on first setup */
1055 chip = spi_get_ctldata(spi);
1056 if (chip == NULL) {
1057 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
1058 if (!chip)
1059 return -ENOMEM;
1060
1061 chip->enable_dma = 0;
1062 chip_info = spi->controller_data;
1063 }
1064
1065 /* chip_info isn't always needed */
1066 if (chip_info) {
Mike Frysinger2ed35512007-12-04 23:45:14 -08001067 /* Make sure people stop trying to set fields via ctl_reg
1068 * when they should actually be using common SPI framework.
1069 * Currently we let through: WOM EMISO PSSE GM SZ TIMOD.
1070 * Not sure if a user actually needs/uses any of these,
1071 * but let's assume (for now) they do.
1072 */
1073 if (chip_info->ctl_reg & (SPE|MSTR|CPOL|CPHA|LSBF|SIZE)) {
1074 dev_err(&spi->dev, "do not set bits in ctl_reg "
1075 "that the SPI framework manages\n");
1076 return -EINVAL;
1077 }
1078
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001079 chip->enable_dma = chip_info->enable_dma != 0
1080 && drv_data->master_info->enable_dma;
1081 chip->ctl_reg = chip_info->ctl_reg;
1082 chip->bits_per_word = chip_info->bits_per_word;
1083 chip->cs_change_per_word = chip_info->cs_change_per_word;
1084 chip->cs_chg_udelay = chip_info->cs_chg_udelay;
1085 }
1086
1087 /* translate common spi framework into our register */
1088 if (spi->mode & SPI_CPOL)
1089 chip->ctl_reg |= CPOL;
1090 if (spi->mode & SPI_CPHA)
1091 chip->ctl_reg |= CPHA;
1092 if (spi->mode & SPI_LSB_FIRST)
1093 chip->ctl_reg |= LSBF;
1094 /* we dont support running in slave mode (yet?) */
1095 chip->ctl_reg |= MSTR;
1096
1097 /*
1098 * if any one SPI chip is registered and wants DMA, request the
1099 * DMA channel for it
1100 */
Bryan Wubb90eb02007-12-04 23:45:18 -08001101 if (chip->enable_dma && !drv_data->dma_requested) {
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001102 /* register dma irq handler */
Mike Frysinger59bfcc62009-04-06 19:00:37 -07001103 if (request_dma(drv_data->dma_channel, "BFIN_SPI_DMA") < 0) {
Bryan Wu88b40362007-05-21 18:32:16 +08001104 dev_dbg(&spi->dev,
1105 "Unable to request BlackFin SPI DMA channel\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001106 return -ENODEV;
1107 }
Bryan Wubb90eb02007-12-04 23:45:18 -08001108 if (set_dma_callback(drv_data->dma_channel,
Mike Frysinger59bfcc62009-04-06 19:00:37 -07001109 dma_irq_handler, drv_data) < 0) {
Bryan Wu88b40362007-05-21 18:32:16 +08001110 dev_dbg(&spi->dev, "Unable to set dma callback\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001111 return -EPERM;
1112 }
Bryan Wubb90eb02007-12-04 23:45:18 -08001113 dma_disable_irq(drv_data->dma_channel);
1114 drv_data->dma_requested = 1;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001115 }
1116
1117 /*
1118 * Notice: for blackfin, the speed_hz is the value of register
1119 * SPI_BAUD, not the real baudrate
1120 */
1121 chip->baud = hz_to_spi_baud(spi->max_speed_hz);
Yi Li2cf36832009-04-06 19:00:44 -07001122 chip->flag = 1 << (spi->chip_select);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001123 chip->chip_select_num = spi->chip_select;
1124
1125 switch (chip->bits_per_word) {
1126 case 8:
1127 chip->n_bytes = 1;
1128 chip->width = CFG_SPI_WORDSIZE8;
1129 chip->read = chip->cs_change_per_word ?
1130 u8_cs_chg_reader : u8_reader;
1131 chip->write = chip->cs_change_per_word ?
1132 u8_cs_chg_writer : u8_writer;
1133 chip->duplex = chip->cs_change_per_word ?
1134 u8_cs_chg_duplex : u8_duplex;
1135 break;
1136
1137 case 16:
1138 chip->n_bytes = 2;
1139 chip->width = CFG_SPI_WORDSIZE16;
1140 chip->read = chip->cs_change_per_word ?
1141 u16_cs_chg_reader : u16_reader;
1142 chip->write = chip->cs_change_per_word ?
1143 u16_cs_chg_writer : u16_writer;
1144 chip->duplex = chip->cs_change_per_word ?
1145 u16_cs_chg_duplex : u16_duplex;
1146 break;
1147
1148 default:
1149 dev_err(&spi->dev, "%d bits_per_word is not supported\n",
1150 chip->bits_per_word);
1151 kfree(chip);
1152 return -ENODEV;
1153 }
1154
Joe Perches898eb712007-10-18 03:06:30 -07001155 dev_dbg(&spi->dev, "setup spi chip %s, width is %d, dma is %d\n",
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001156 spi->modalias, chip->width, chip->enable_dma);
Bryan Wu88b40362007-05-21 18:32:16 +08001157 dev_dbg(&spi->dev, "ctl_reg is 0x%x, flag_reg is 0x%x\n",
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001158 chip->ctl_reg, chip->flag);
1159
1160 spi_set_ctldata(spi, chip);
1161
Sonic Zhang12e17c42007-12-04 23:45:16 -08001162 dev_dbg(&spi->dev, "chip select number is %d\n", chip->chip_select_num);
1163 if ((chip->chip_select_num > 0)
1164 && (chip->chip_select_num <= spi->master->num_chipselect))
1165 peripheral_request(ssel[spi->master->bus_num]
Bryan Wuaab0d832008-02-06 01:38:17 -08001166 [chip->chip_select_num-1], spi->modalias);
Sonic Zhang12e17c42007-12-04 23:45:16 -08001167
Sonic Zhang07612e52007-12-04 23:45:21 -08001168 cs_deactive(drv_data, chip);
1169
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001170 return 0;
1171}
1172
1173/*
1174 * callback for spi framework.
1175 * clean driver specific data
1176 */
Bryan Wu88b40362007-05-21 18:32:16 +08001177static void cleanup(struct spi_device *spi)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001178{
Mike Frysinger27bb9e72007-06-11 15:31:30 +08001179 struct chip_data *chip = spi_get_ctldata(spi);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001180
Sonic Zhang12e17c42007-12-04 23:45:16 -08001181 if ((chip->chip_select_num > 0)
1182 && (chip->chip_select_num <= spi->master->num_chipselect))
1183 peripheral_free(ssel[spi->master->bus_num]
1184 [chip->chip_select_num-1]);
1185
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001186 kfree(chip);
1187}
1188
1189static inline int init_queue(struct driver_data *drv_data)
1190{
1191 INIT_LIST_HEAD(&drv_data->queue);
1192 spin_lock_init(&drv_data->lock);
1193
1194 drv_data->run = QUEUE_STOPPED;
1195 drv_data->busy = 0;
1196
1197 /* init transfer tasklet */
1198 tasklet_init(&drv_data->pump_transfers,
1199 pump_transfers, (unsigned long)drv_data);
1200
1201 /* init messages workqueue */
1202 INIT_WORK(&drv_data->pump_messages, pump_messages);
Kay Sievers6c7377a2009-03-24 16:38:21 -07001203 drv_data->workqueue = create_singlethread_workqueue(
1204 dev_name(drv_data->master->dev.parent));
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001205 if (drv_data->workqueue == NULL)
1206 return -EBUSY;
1207
1208 return 0;
1209}
1210
1211static inline int start_queue(struct driver_data *drv_data)
1212{
1213 unsigned long flags;
1214
1215 spin_lock_irqsave(&drv_data->lock, flags);
1216
1217 if (drv_data->run == QUEUE_RUNNING || drv_data->busy) {
1218 spin_unlock_irqrestore(&drv_data->lock, flags);
1219 return -EBUSY;
1220 }
1221
1222 drv_data->run = QUEUE_RUNNING;
1223 drv_data->cur_msg = NULL;
1224 drv_data->cur_transfer = NULL;
1225 drv_data->cur_chip = NULL;
1226 spin_unlock_irqrestore(&drv_data->lock, flags);
1227
1228 queue_work(drv_data->workqueue, &drv_data->pump_messages);
1229
1230 return 0;
1231}
1232
1233static inline int stop_queue(struct driver_data *drv_data)
1234{
1235 unsigned long flags;
1236 unsigned limit = 500;
1237 int status = 0;
1238
1239 spin_lock_irqsave(&drv_data->lock, flags);
1240
1241 /*
1242 * This is a bit lame, but is optimized for the common execution path.
1243 * A wait_queue on the drv_data->busy could be used, but then the common
1244 * execution path (pump_messages) would be required to call wake_up or
1245 * friends on every SPI message. Do this instead
1246 */
1247 drv_data->run = QUEUE_STOPPED;
1248 while (!list_empty(&drv_data->queue) && drv_data->busy && limit--) {
1249 spin_unlock_irqrestore(&drv_data->lock, flags);
1250 msleep(10);
1251 spin_lock_irqsave(&drv_data->lock, flags);
1252 }
1253
1254 if (!list_empty(&drv_data->queue) || drv_data->busy)
1255 status = -EBUSY;
1256
1257 spin_unlock_irqrestore(&drv_data->lock, flags);
1258
1259 return status;
1260}
1261
1262static inline int destroy_queue(struct driver_data *drv_data)
1263{
1264 int status;
1265
1266 status = stop_queue(drv_data);
1267 if (status != 0)
1268 return status;
1269
1270 destroy_workqueue(drv_data->workqueue);
1271
1272 return 0;
1273}
1274
1275static int __init bfin5xx_spi_probe(struct platform_device *pdev)
1276{
1277 struct device *dev = &pdev->dev;
1278 struct bfin5xx_spi_master *platform_info;
1279 struct spi_master *master;
1280 struct driver_data *drv_data = 0;
Bryan Wua32c6912007-12-04 23:45:15 -08001281 struct resource *res;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001282 int status = 0;
1283
1284 platform_info = dev->platform_data;
1285
1286 /* Allocate master with space for drv_data */
1287 master = spi_alloc_master(dev, sizeof(struct driver_data) + 16);
1288 if (!master) {
1289 dev_err(&pdev->dev, "can not alloc spi_master\n");
1290 return -ENOMEM;
1291 }
Bryan Wu131b17d2007-12-04 23:45:12 -08001292
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001293 drv_data = spi_master_get_devdata(master);
1294 drv_data->master = master;
1295 drv_data->master_info = platform_info;
1296 drv_data->pdev = pdev;
Bryan Wu003d9222007-12-04 23:45:22 -08001297 drv_data->pin_req = platform_info->pin_req;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001298
1299 master->bus_num = pdev->id;
1300 master->num_chipselect = platform_info->num_chipselect;
1301 master->cleanup = cleanup;
1302 master->setup = setup;
1303 master->transfer = transfer;
1304
Bryan Wua32c6912007-12-04 23:45:15 -08001305 /* Find and map our resources */
1306 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1307 if (res == NULL) {
1308 dev_err(dev, "Cannot get IORESOURCE_MEM\n");
1309 status = -ENOENT;
1310 goto out_error_get_res;
1311 }
1312
Bryan Wuf4521262007-12-04 23:45:22 -08001313 drv_data->regs_base = ioremap(res->start, (res->end - res->start + 1));
1314 if (drv_data->regs_base == NULL) {
Bryan Wua32c6912007-12-04 23:45:15 -08001315 dev_err(dev, "Cannot map IO\n");
1316 status = -ENXIO;
1317 goto out_error_ioremap;
1318 }
1319
Bryan Wubb90eb02007-12-04 23:45:18 -08001320 drv_data->dma_channel = platform_get_irq(pdev, 0);
1321 if (drv_data->dma_channel < 0) {
Bryan Wua32c6912007-12-04 23:45:15 -08001322 dev_err(dev, "No DMA channel specified\n");
1323 status = -ENOENT;
1324 goto out_error_no_dma_ch;
1325 }
1326
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001327 /* Initial and start queue */
1328 status = init_queue(drv_data);
1329 if (status != 0) {
Bryan Wua32c6912007-12-04 23:45:15 -08001330 dev_err(dev, "problem initializing queue\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001331 goto out_error_queue_alloc;
1332 }
Bryan Wua32c6912007-12-04 23:45:15 -08001333
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001334 status = start_queue(drv_data);
1335 if (status != 0) {
Bryan Wua32c6912007-12-04 23:45:15 -08001336 dev_err(dev, "problem starting queue\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001337 goto out_error_queue_alloc;
1338 }
1339
Vitja Makarovf9e522c2008-04-08 17:41:57 -07001340 status = peripheral_request_list(drv_data->pin_req, DRV_NAME);
1341 if (status != 0) {
1342 dev_err(&pdev->dev, ": Requesting Peripherals failed\n");
1343 goto out_error_queue_alloc;
1344 }
1345
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001346 /* Register with the SPI framework */
1347 platform_set_drvdata(pdev, drv_data);
1348 status = spi_register_master(master);
1349 if (status != 0) {
Bryan Wua32c6912007-12-04 23:45:15 -08001350 dev_err(dev, "problem registering spi master\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001351 goto out_error_queue_alloc;
1352 }
Bryan Wua32c6912007-12-04 23:45:15 -08001353
Bryan Wuf4521262007-12-04 23:45:22 -08001354 dev_info(dev, "%s, Version %s, regs_base@%p, dma channel@%d\n",
Bryan Wubb90eb02007-12-04 23:45:18 -08001355 DRV_DESC, DRV_VERSION, drv_data->regs_base,
1356 drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001357 return status;
1358
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001359out_error_queue_alloc:
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001360 destroy_queue(drv_data);
Bryan Wua32c6912007-12-04 23:45:15 -08001361out_error_no_dma_ch:
Bryan Wubb90eb02007-12-04 23:45:18 -08001362 iounmap((void *) drv_data->regs_base);
Bryan Wua32c6912007-12-04 23:45:15 -08001363out_error_ioremap:
1364out_error_get_res:
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001365 spi_master_put(master);
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001366
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001367 return status;
1368}
1369
1370/* stop hardware and remove the driver */
1371static int __devexit bfin5xx_spi_remove(struct platform_device *pdev)
1372{
1373 struct driver_data *drv_data = platform_get_drvdata(pdev);
1374 int status = 0;
1375
1376 if (!drv_data)
1377 return 0;
1378
1379 /* Remove the queue */
1380 status = destroy_queue(drv_data);
1381 if (status != 0)
1382 return status;
1383
1384 /* Disable the SSP at the peripheral and SOC level */
1385 bfin_spi_disable(drv_data);
1386
1387 /* Release DMA */
1388 if (drv_data->master_info->enable_dma) {
Bryan Wubb90eb02007-12-04 23:45:18 -08001389 if (dma_channel_active(drv_data->dma_channel))
1390 free_dma(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001391 }
1392
1393 /* Disconnect from the SPI framework */
1394 spi_unregister_master(drv_data->master);
1395
Bryan Wu003d9222007-12-04 23:45:22 -08001396 peripheral_free_list(drv_data->pin_req);
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001397
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001398 /* Prevent double remove */
1399 platform_set_drvdata(pdev, NULL);
1400
1401 return 0;
1402}
1403
1404#ifdef CONFIG_PM
1405static int bfin5xx_spi_suspend(struct platform_device *pdev, pm_message_t state)
1406{
1407 struct driver_data *drv_data = platform_get_drvdata(pdev);
1408 int status = 0;
1409
1410 status = stop_queue(drv_data);
1411 if (status != 0)
1412 return status;
1413
1414 /* stop hardware */
1415 bfin_spi_disable(drv_data);
1416
1417 return 0;
1418}
1419
1420static int bfin5xx_spi_resume(struct platform_device *pdev)
1421{
1422 struct driver_data *drv_data = platform_get_drvdata(pdev);
1423 int status = 0;
1424
1425 /* Enable the SPI interface */
1426 bfin_spi_enable(drv_data);
1427
1428 /* Start the queue running */
1429 status = start_queue(drv_data);
1430 if (status != 0) {
1431 dev_err(&pdev->dev, "problem starting queue (%d)\n", status);
1432 return status;
1433 }
1434
1435 return 0;
1436}
1437#else
1438#define bfin5xx_spi_suspend NULL
1439#define bfin5xx_spi_resume NULL
1440#endif /* CONFIG_PM */
1441
Kay Sievers7e38c3c2008-04-10 21:29:20 -07001442MODULE_ALIAS("platform:bfin-spi");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001443static struct platform_driver bfin5xx_spi_driver = {
David Brownellfc3ba952007-08-30 23:56:24 -07001444 .driver = {
Bryan Wua32c6912007-12-04 23:45:15 -08001445 .name = DRV_NAME,
Bryan Wu88b40362007-05-21 18:32:16 +08001446 .owner = THIS_MODULE,
1447 },
1448 .suspend = bfin5xx_spi_suspend,
1449 .resume = bfin5xx_spi_resume,
1450 .remove = __devexit_p(bfin5xx_spi_remove),
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001451};
1452
1453static int __init bfin5xx_spi_init(void)
1454{
Bryan Wu88b40362007-05-21 18:32:16 +08001455 return platform_driver_probe(&bfin5xx_spi_driver, bfin5xx_spi_probe);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001456}
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001457module_init(bfin5xx_spi_init);
1458
1459static void __exit bfin5xx_spi_exit(void)
1460{
1461 platform_driver_unregister(&bfin5xx_spi_driver);
1462}
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001463module_exit(bfin5xx_spi_exit);