blob: c7cfd95fa8ec3368006d967c2e2452423e93439f [file] [log] [blame]
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001/*
Bryan Wu131b17d2007-12-04 23:45:12 -08002 * File: drivers/spi/bfin5xx_spi.c
3 * Maintainer:
4 * Bryan Wu <bryan.wu@analog.com>
5 * Original Author:
6 * Luke Yang (Analog Devices Inc.)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07007 *
Bryan Wu131b17d2007-12-04 23:45:12 -08008 * Created: March. 10th 2006
9 * Description: SPI controller driver for Blackfin BF5xx
10 * Bugs: Enter bugs at http://blackfin.uclinux.org/
Wu, Bryana5f6abd2007-05-06 14:50:34 -070011 *
12 * Modified:
13 * March 10, 2006 bfin5xx_spi.c Created. (Luke Yang)
14 * August 7, 2006 added full duplex mode (Axel Weiss & Luke Yang)
Bryan Wu131b17d2007-12-04 23:45:12 -080015 * July 17, 2007 add support for BF54x SPI0 controller (Bryan Wu)
Bryan Wua32c6912007-12-04 23:45:15 -080016 * July 30, 2007 add platfrom_resource interface to support multi-port
17 * SPI controller (Bryan Wu)
Wu, Bryana5f6abd2007-05-06 14:50:34 -070018 *
Bryan Wu131b17d2007-12-04 23:45:12 -080019 * Copyright 2004-2007 Analog Devices Inc.
Wu, Bryana5f6abd2007-05-06 14:50:34 -070020 *
21 * This program is free software ; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation ; either version 2, or (at your option)
24 * any later version.
25 *
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY ; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
30 *
31 * You should have received a copy of the GNU General Public License
32 * along with this program ; see the file COPYING.
33 * If not, write to the Free Software Foundation,
34 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
35 */
36
37#include <linux/init.h>
38#include <linux/module.h>
Bryan Wu131b17d2007-12-04 23:45:12 -080039#include <linux/delay.h>
Wu, Bryana5f6abd2007-05-06 14:50:34 -070040#include <linux/device.h>
Bryan Wu131b17d2007-12-04 23:45:12 -080041#include <linux/io.h>
Wu, Bryana5f6abd2007-05-06 14:50:34 -070042#include <linux/ioport.h>
Bryan Wu131b17d2007-12-04 23:45:12 -080043#include <linux/irq.h>
Wu, Bryana5f6abd2007-05-06 14:50:34 -070044#include <linux/errno.h>
45#include <linux/interrupt.h>
46#include <linux/platform_device.h>
47#include <linux/dma-mapping.h>
48#include <linux/spi/spi.h>
49#include <linux/workqueue.h>
Wu, Bryana5f6abd2007-05-06 14:50:34 -070050
Wu, Bryana5f6abd2007-05-06 14:50:34 -070051#include <asm/dma.h>
Bryan Wu131b17d2007-12-04 23:45:12 -080052#include <asm/portmux.h>
Wu, Bryana5f6abd2007-05-06 14:50:34 -070053#include <asm/bfin5xx_spi.h>
54
Bryan Wua32c6912007-12-04 23:45:15 -080055#define DRV_NAME "bfin-spi"
56#define DRV_AUTHOR "Bryan Wu, Luke Yang"
57#define DRV_DESC "Blackfin BF5xx on-chip SPI Contoller Driver"
58#define DRV_VERSION "1.0"
59
60MODULE_AUTHOR(DRV_AUTHOR);
61MODULE_DESCRIPTION(DRV_DESC);
Wu, Bryana5f6abd2007-05-06 14:50:34 -070062MODULE_LICENSE("GPL");
63
64#define IS_DMA_ALIGNED(x) (((u32)(x)&0x07)==0)
65
Bryan Wua32c6912007-12-04 23:45:15 -080066static u32 spi_dma_ch;
67static u32 spi_regs_base;
68
Wu, Bryana5f6abd2007-05-06 14:50:34 -070069#define DEFINE_SPI_REG(reg, off) \
70static inline u16 read_##reg(void) \
Bryan Wua32c6912007-12-04 23:45:15 -080071 { return bfin_read16(spi_regs_base + off); } \
Wu, Bryana5f6abd2007-05-06 14:50:34 -070072static inline void write_##reg(u16 v) \
Bryan Wua32c6912007-12-04 23:45:15 -080073 {bfin_write16(spi_regs_base + off, v); }
Wu, Bryana5f6abd2007-05-06 14:50:34 -070074
75DEFINE_SPI_REG(CTRL, 0x00)
76DEFINE_SPI_REG(FLAG, 0x04)
77DEFINE_SPI_REG(STAT, 0x08)
78DEFINE_SPI_REG(TDBR, 0x0C)
79DEFINE_SPI_REG(RDBR, 0x10)
80DEFINE_SPI_REG(BAUD, 0x14)
81DEFINE_SPI_REG(SHAW, 0x18)
82#define START_STATE ((void*)0)
83#define RUNNING_STATE ((void*)1)
84#define DONE_STATE ((void*)2)
85#define ERROR_STATE ((void*)-1)
86#define QUEUE_RUNNING 0
87#define QUEUE_STOPPED 1
88int dma_requested;
89
90struct driver_data {
91 /* Driver model hookup */
92 struct platform_device *pdev;
93
94 /* SPI framework hookup */
95 struct spi_master *master;
96
97 /* BFIN hookup */
98 struct bfin5xx_spi_master *master_info;
99
100 /* Driver message queue */
101 struct workqueue_struct *workqueue;
102 struct work_struct pump_messages;
103 spinlock_t lock;
104 struct list_head queue;
105 int busy;
106 int run;
107
108 /* Message Transfer pump */
109 struct tasklet_struct pump_transfers;
110
111 /* Current message transfer state info */
112 struct spi_message *cur_msg;
113 struct spi_transfer *cur_transfer;
114 struct chip_data *cur_chip;
115 size_t len_in_bytes;
116 size_t len;
117 void *tx;
118 void *tx_end;
119 void *rx;
120 void *rx_end;
121 int dma_mapped;
122 dma_addr_t rx_dma;
123 dma_addr_t tx_dma;
124 size_t rx_map_len;
125 size_t tx_map_len;
126 u8 n_bytes;
Bryan Wufad91c82007-12-04 23:45:14 -0800127 int cs_change;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700128 void (*write) (struct driver_data *);
129 void (*read) (struct driver_data *);
130 void (*duplex) (struct driver_data *);
131};
132
133struct chip_data {
134 u16 ctl_reg;
135 u16 baud;
136 u16 flag;
137
138 u8 chip_select_num;
139 u8 n_bytes;
Bryan Wu88b40362007-05-21 18:32:16 +0800140 u8 width; /* 0 or 1 */
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700141 u8 enable_dma;
142 u8 bits_per_word; /* 8 or 16 */
143 u8 cs_change_per_word;
144 u8 cs_chg_udelay;
145 void (*write) (struct driver_data *);
146 void (*read) (struct driver_data *);
147 void (*duplex) (struct driver_data *);
148};
149
Bryan Wu88b40362007-05-21 18:32:16 +0800150static void bfin_spi_enable(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700151{
152 u16 cr;
153
154 cr = read_CTRL();
155 write_CTRL(cr | BIT_CTL_ENABLE);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700156}
157
Bryan Wu88b40362007-05-21 18:32:16 +0800158static void bfin_spi_disable(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700159{
160 u16 cr;
161
162 cr = read_CTRL();
163 write_CTRL(cr & (~BIT_CTL_ENABLE));
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700164}
165
166/* Caculate the SPI_BAUD register value based on input HZ */
167static u16 hz_to_spi_baud(u32 speed_hz)
168{
169 u_long sclk = get_sclk();
170 u16 spi_baud = (sclk / (2 * speed_hz));
171
172 if ((sclk % (2 * speed_hz)) > 0)
173 spi_baud++;
174
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700175 return spi_baud;
176}
177
178static int flush(struct driver_data *drv_data)
179{
180 unsigned long limit = loops_per_jiffy << 1;
181
182 /* wait for stop and clear stat */
183 while (!(read_STAT() & BIT_STAT_SPIF) && limit--)
184 continue;
185
186 write_STAT(BIT_STAT_CLR);
187
188 return limit;
189}
190
Bryan Wufad91c82007-12-04 23:45:14 -0800191/* Chip select operation functions for cs_change flag */
192static void cs_active(struct chip_data *chip)
193{
194 u16 flag = read_FLAG();
195
196 flag |= chip->flag;
197 flag &= ~(chip->flag << 8);
198
199 write_FLAG(flag);
200}
201
202static void cs_deactive(struct chip_data *chip)
203{
204 u16 flag = read_FLAG();
205
206 flag |= (chip->flag << 8);
207
208 write_FLAG(flag);
209}
210
Sonic Zhang7c4ef092007-12-04 23:45:16 -0800211#define MAX_SPI_SSEL 7
Bryan Wu5fec5b52007-12-04 23:45:13 -0800212
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700213/* stop controller and re-config current chip*/
Bryan Wu5fec5b52007-12-04 23:45:13 -0800214static int restore_state(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700215{
216 struct chip_data *chip = drv_data->cur_chip;
Bryan Wu5fec5b52007-12-04 23:45:13 -0800217 int ret = 0;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700218
219 /* Clear status and disable clock */
220 write_STAT(BIT_STAT_CLR);
221 bfin_spi_disable(drv_data);
Bryan Wu88b40362007-05-21 18:32:16 +0800222 dev_dbg(&drv_data->pdev->dev, "restoring spi ctl state\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700223
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700224 /* Load the registers */
Sonic Zhangcc487e72007-12-04 23:45:17 -0800225 cs_deactive(chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700226 write_BAUD(chip->baud);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800227 chip->ctl_reg &= (~BIT_CTL_TIMOD);
228 chip->ctl_reg |= (chip->width << 8);
229 write_CTRL(chip->ctl_reg);
230
231 bfin_spi_enable(drv_data);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800232
Bryan Wu5fec5b52007-12-04 23:45:13 -0800233 if (ret)
234 dev_dbg(&drv_data->pdev->dev,
235 ": request chip select number %d failed\n",
236 chip->chip_select_num);
237
238 return ret;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700239}
240
241/* used to kick off transfer in rx mode */
242static unsigned short dummy_read(void)
243{
244 unsigned short tmp;
245 tmp = read_RDBR();
246 return tmp;
247}
248
249static void null_writer(struct driver_data *drv_data)
250{
251 u8 n_bytes = drv_data->n_bytes;
252
253 while (drv_data->tx < drv_data->tx_end) {
254 write_TDBR(0);
255 while ((read_STAT() & BIT_STAT_TXS))
256 continue;
257 drv_data->tx += n_bytes;
258 }
259}
260
261static void null_reader(struct driver_data *drv_data)
262{
263 u8 n_bytes = drv_data->n_bytes;
264 dummy_read();
265
266 while (drv_data->rx < drv_data->rx_end) {
267 while (!(read_STAT() & BIT_STAT_RXS))
268 continue;
269 dummy_read();
270 drv_data->rx += n_bytes;
271 }
272}
273
274static void u8_writer(struct driver_data *drv_data)
275{
Bryan Wu131b17d2007-12-04 23:45:12 -0800276 dev_dbg(&drv_data->pdev->dev,
Bryan Wu88b40362007-05-21 18:32:16 +0800277 "cr8-s is 0x%x\n", read_STAT());
Sonic Zhangcc487e72007-12-04 23:45:17 -0800278
Sonic Zhang3f479a62007-12-04 23:45:18 -0800279 /* poll for SPI completion before start */
280 while (!(read_STAT() & BIT_STAT_SPIF))
281 continue;
282
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700283 while (drv_data->tx < drv_data->tx_end) {
284 write_TDBR(*(u8 *) (drv_data->tx));
285 while (read_STAT() & BIT_STAT_TXS)
286 continue;
287 ++drv_data->tx;
288 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700289}
290
291static void u8_cs_chg_writer(struct driver_data *drv_data)
292{
293 struct chip_data *chip = drv_data->cur_chip;
294
Sonic Zhang3f479a62007-12-04 23:45:18 -0800295 /* poll for SPI completion before start */
296 while (!(read_STAT() & BIT_STAT_SPIF))
297 continue;
298
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700299 while (drv_data->tx < drv_data->tx_end) {
Bryan Wufad91c82007-12-04 23:45:14 -0800300 cs_active(chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700301
302 write_TDBR(*(u8 *) (drv_data->tx));
303 while (read_STAT() & BIT_STAT_TXS)
304 continue;
Bryan Wufad91c82007-12-04 23:45:14 -0800305 cs_deactive(chip);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800306
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700307 if (chip->cs_chg_udelay)
308 udelay(chip->cs_chg_udelay);
309 ++drv_data->tx;
310 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700311}
312
313static void u8_reader(struct driver_data *drv_data)
314{
Bryan Wu131b17d2007-12-04 23:45:12 -0800315 dev_dbg(&drv_data->pdev->dev,
Bryan Wu88b40362007-05-21 18:32:16 +0800316 "cr-8 is 0x%x\n", read_STAT());
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700317
Sonic Zhang3f479a62007-12-04 23:45:18 -0800318 /* poll for SPI completion before start */
319 while (!(read_STAT() & BIT_STAT_SPIF))
320 continue;
321
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700322 /* clear TDBR buffer before read(else it will be shifted out) */
323 write_TDBR(0xFFFF);
324
325 dummy_read();
Sonic Zhangcc487e72007-12-04 23:45:17 -0800326
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700327 while (drv_data->rx < drv_data->rx_end - 1) {
328 while (!(read_STAT() & BIT_STAT_RXS))
329 continue;
330 *(u8 *) (drv_data->rx) = read_RDBR();
331 ++drv_data->rx;
332 }
333
334 while (!(read_STAT() & BIT_STAT_RXS))
335 continue;
336 *(u8 *) (drv_data->rx) = read_SHAW();
337 ++drv_data->rx;
338}
339
340static void u8_cs_chg_reader(struct driver_data *drv_data)
341{
342 struct chip_data *chip = drv_data->cur_chip;
343
Sonic Zhang3f479a62007-12-04 23:45:18 -0800344 /* poll for SPI completion before start */
345 while (!(read_STAT() & BIT_STAT_SPIF))
346 continue;
347
Sonic Zhangcc487e72007-12-04 23:45:17 -0800348 /* clear TDBR buffer before read(else it will be shifted out) */
349 write_TDBR(0xFFFF);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700350
Sonic Zhangcc487e72007-12-04 23:45:17 -0800351 cs_active(chip);
352 dummy_read();
353
354 while (drv_data->rx < drv_data->rx_end - 1) {
Bryan Wufad91c82007-12-04 23:45:14 -0800355 cs_deactive(chip);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800356
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700357 if (chip->cs_chg_udelay)
358 udelay(chip->cs_chg_udelay);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800359
360 while (!(read_STAT() & BIT_STAT_RXS))
361 continue;
362 cs_active(chip);
363 *(u8 *) (drv_data->rx) = read_RDBR();
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700364 ++drv_data->rx;
365 }
Bryan Wufad91c82007-12-04 23:45:14 -0800366 cs_deactive(chip);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800367
Sonic Zhangcc487e72007-12-04 23:45:17 -0800368 while (!(read_STAT() & BIT_STAT_RXS))
369 continue;
370 *(u8 *) (drv_data->rx) = read_SHAW();
371 ++drv_data->rx;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700372}
373
374static void u8_duplex(struct driver_data *drv_data)
375{
Sonic Zhang3f479a62007-12-04 23:45:18 -0800376 /* poll for SPI completion before start */
377 while (!(read_STAT() & BIT_STAT_SPIF))
378 continue;
379
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700380 /* in duplex mode, clk is triggered by writing of TDBR */
381 while (drv_data->rx < drv_data->rx_end) {
382 write_TDBR(*(u8 *) (drv_data->tx));
Sonic Zhangcc487e72007-12-04 23:45:17 -0800383 while (read_STAT() & BIT_STAT_TXS)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700384 continue;
385 while (!(read_STAT() & BIT_STAT_RXS))
386 continue;
387 *(u8 *) (drv_data->rx) = read_RDBR();
388 ++drv_data->rx;
389 ++drv_data->tx;
390 }
391}
392
393static void u8_cs_chg_duplex(struct driver_data *drv_data)
394{
395 struct chip_data *chip = drv_data->cur_chip;
396
Sonic Zhang3f479a62007-12-04 23:45:18 -0800397 /* poll for SPI completion before start */
398 while (!(read_STAT() & BIT_STAT_SPIF))
399 continue;
400
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700401 while (drv_data->rx < drv_data->rx_end) {
Bryan Wufad91c82007-12-04 23:45:14 -0800402 cs_active(chip);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800403
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700404 write_TDBR(*(u8 *) (drv_data->tx));
Sonic Zhangcc487e72007-12-04 23:45:17 -0800405 while (read_STAT() & BIT_STAT_TXS)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700406 continue;
407 while (!(read_STAT() & BIT_STAT_RXS))
408 continue;
409 *(u8 *) (drv_data->rx) = read_RDBR();
Bryan Wufad91c82007-12-04 23:45:14 -0800410 cs_deactive(chip);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800411
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700412 if (chip->cs_chg_udelay)
413 udelay(chip->cs_chg_udelay);
414 ++drv_data->rx;
415 ++drv_data->tx;
416 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700417}
418
419static void u16_writer(struct driver_data *drv_data)
420{
Bryan Wu131b17d2007-12-04 23:45:12 -0800421 dev_dbg(&drv_data->pdev->dev,
Bryan Wu88b40362007-05-21 18:32:16 +0800422 "cr16 is 0x%x\n", read_STAT());
423
Sonic Zhang3f479a62007-12-04 23:45:18 -0800424 /* poll for SPI completion before start */
425 while (!(read_STAT() & BIT_STAT_SPIF))
426 continue;
427
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700428 while (drv_data->tx < drv_data->tx_end) {
429 write_TDBR(*(u16 *) (drv_data->tx));
430 while ((read_STAT() & BIT_STAT_TXS))
431 continue;
432 drv_data->tx += 2;
433 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700434}
435
436static void u16_cs_chg_writer(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 */
441 while (!(read_STAT() & BIT_STAT_SPIF))
442 continue;
443
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700444 while (drv_data->tx < drv_data->tx_end) {
Bryan Wufad91c82007-12-04 23:45:14 -0800445 cs_active(chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700446
447 write_TDBR(*(u16 *) (drv_data->tx));
448 while ((read_STAT() & BIT_STAT_TXS))
449 continue;
Bryan Wufad91c82007-12-04 23:45:14 -0800450 cs_deactive(chip);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800451
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700452 if (chip->cs_chg_udelay)
453 udelay(chip->cs_chg_udelay);
454 drv_data->tx += 2;
455 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700456}
457
458static void u16_reader(struct driver_data *drv_data)
459{
Bryan Wu88b40362007-05-21 18:32:16 +0800460 dev_dbg(&drv_data->pdev->dev,
461 "cr-16 is 0x%x\n", read_STAT());
Sonic Zhangcc487e72007-12-04 23:45:17 -0800462
Sonic Zhang3f479a62007-12-04 23:45:18 -0800463 /* poll for SPI completion before start */
464 while (!(read_STAT() & BIT_STAT_SPIF))
465 continue;
466
Sonic Zhangcc487e72007-12-04 23:45:17 -0800467 /* clear TDBR buffer before read(else it will be shifted out) */
468 write_TDBR(0xFFFF);
469
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700470 dummy_read();
471
472 while (drv_data->rx < (drv_data->rx_end - 2)) {
473 while (!(read_STAT() & BIT_STAT_RXS))
474 continue;
475 *(u16 *) (drv_data->rx) = read_RDBR();
476 drv_data->rx += 2;
477 }
478
479 while (!(read_STAT() & BIT_STAT_RXS))
480 continue;
481 *(u16 *) (drv_data->rx) = read_SHAW();
482 drv_data->rx += 2;
483}
484
485static void u16_cs_chg_reader(struct driver_data *drv_data)
486{
487 struct chip_data *chip = drv_data->cur_chip;
488
Sonic Zhang3f479a62007-12-04 23:45:18 -0800489 /* poll for SPI completion before start */
490 while (!(read_STAT() & BIT_STAT_SPIF))
491 continue;
492
Sonic Zhangcc487e72007-12-04 23:45:17 -0800493 /* clear TDBR buffer before read(else it will be shifted out) */
494 write_TDBR(0xFFFF);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700495
Sonic Zhangcc487e72007-12-04 23:45:17 -0800496 cs_active(chip);
497 dummy_read();
498
499 while (drv_data->rx < drv_data->rx_end) {
Bryan Wufad91c82007-12-04 23:45:14 -0800500 cs_deactive(chip);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800501
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700502 if (chip->cs_chg_udelay)
503 udelay(chip->cs_chg_udelay);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800504
505 while (!(read_STAT() & BIT_STAT_RXS))
506 continue;
507 cs_active(chip);
508 *(u16 *) (drv_data->rx) = read_RDBR();
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700509 drv_data->rx += 2;
510 }
Bryan Wufad91c82007-12-04 23:45:14 -0800511 cs_deactive(chip);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800512
513 while (!(read_STAT() & BIT_STAT_RXS))
514 continue;
515 *(u16 *) (drv_data->rx) = read_SHAW();
516 drv_data->rx += 2;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700517}
518
519static void u16_duplex(struct driver_data *drv_data)
520{
Sonic Zhang3f479a62007-12-04 23:45:18 -0800521 /* poll for SPI completion before start */
522 while (!(read_STAT() & BIT_STAT_SPIF))
523 continue;
524
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700525 /* in duplex mode, clk is triggered by writing of TDBR */
526 while (drv_data->tx < drv_data->tx_end) {
527 write_TDBR(*(u16 *) (drv_data->tx));
Sonic Zhangcc487e72007-12-04 23:45:17 -0800528 while (read_STAT() & BIT_STAT_TXS)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700529 continue;
530 while (!(read_STAT() & BIT_STAT_RXS))
531 continue;
532 *(u16 *) (drv_data->rx) = read_RDBR();
533 drv_data->rx += 2;
534 drv_data->tx += 2;
535 }
536}
537
538static void u16_cs_chg_duplex(struct driver_data *drv_data)
539{
540 struct chip_data *chip = drv_data->cur_chip;
541
Sonic Zhang3f479a62007-12-04 23:45:18 -0800542 /* poll for SPI completion before start */
543 while (!(read_STAT() & BIT_STAT_SPIF))
544 continue;
545
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700546 while (drv_data->tx < drv_data->tx_end) {
Bryan Wufad91c82007-12-04 23:45:14 -0800547 cs_active(chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700548
549 write_TDBR(*(u16 *) (drv_data->tx));
Sonic Zhangcc487e72007-12-04 23:45:17 -0800550 while (read_STAT() & BIT_STAT_TXS)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700551 continue;
552 while (!(read_STAT() & BIT_STAT_RXS))
553 continue;
554 *(u16 *) (drv_data->rx) = read_RDBR();
Bryan Wufad91c82007-12-04 23:45:14 -0800555 cs_deactive(chip);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800556
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700557 if (chip->cs_chg_udelay)
558 udelay(chip->cs_chg_udelay);
559 drv_data->rx += 2;
560 drv_data->tx += 2;
561 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700562}
563
564/* test if ther is more transfer to be done */
565static void *next_transfer(struct driver_data *drv_data)
566{
567 struct spi_message *msg = drv_data->cur_msg;
568 struct spi_transfer *trans = drv_data->cur_transfer;
569
570 /* Move to next transfer */
571 if (trans->transfer_list.next != &msg->transfers) {
572 drv_data->cur_transfer =
573 list_entry(trans->transfer_list.next,
574 struct spi_transfer, transfer_list);
575 return RUNNING_STATE;
576 } else
577 return DONE_STATE;
578}
579
580/*
581 * caller already set message->status;
582 * dma and pio irqs are blocked give finished message back
583 */
584static void giveback(struct driver_data *drv_data)
585{
Bryan Wufad91c82007-12-04 23:45:14 -0800586 struct chip_data *chip = drv_data->cur_chip;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700587 struct spi_transfer *last_transfer;
588 unsigned long flags;
589 struct spi_message *msg;
590
591 spin_lock_irqsave(&drv_data->lock, flags);
592 msg = drv_data->cur_msg;
593 drv_data->cur_msg = NULL;
594 drv_data->cur_transfer = NULL;
595 drv_data->cur_chip = NULL;
596 queue_work(drv_data->workqueue, &drv_data->pump_messages);
597 spin_unlock_irqrestore(&drv_data->lock, flags);
598
599 last_transfer = list_entry(msg->transfers.prev,
600 struct spi_transfer, transfer_list);
601
602 msg->state = NULL;
603
604 /* disable chip select signal. And not stop spi in autobuffer mode */
605 if (drv_data->tx_dma != 0xFFFF) {
Bryan Wufad91c82007-12-04 23:45:14 -0800606 cs_deactive(chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700607 bfin_spi_disable(drv_data);
608 }
609
Bryan Wufad91c82007-12-04 23:45:14 -0800610 if (!drv_data->cs_change)
611 cs_deactive(chip);
612
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700613 if (msg->complete)
614 msg->complete(msg->context);
615}
616
Bryan Wu88b40362007-05-21 18:32:16 +0800617static irqreturn_t dma_irq_handler(int irq, void *dev_id)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700618{
619 struct driver_data *drv_data = (struct driver_data *)dev_id;
620 struct spi_message *msg = drv_data->cur_msg;
Bryan Wufad91c82007-12-04 23:45:14 -0800621 struct chip_data *chip = drv_data->cur_chip;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700622
Bryan Wu88b40362007-05-21 18:32:16 +0800623 dev_dbg(&drv_data->pdev->dev, "in dma_irq_handler\n");
Bryan Wua32c6912007-12-04 23:45:15 -0800624 clear_dma_irqstat(spi_dma_ch);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700625
Bryan Wud6fe89b2007-06-11 17:34:17 +0800626 /* Wait for DMA to complete */
Bryan Wua32c6912007-12-04 23:45:15 -0800627 while (get_dma_curr_irqstat(spi_dma_ch) & DMA_RUN)
Bryan Wud6fe89b2007-06-11 17:34:17 +0800628 continue;
629
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700630 /*
Bryan Wud6fe89b2007-06-11 17:34:17 +0800631 * wait for the last transaction shifted out. HRM states:
632 * at this point there may still be data in the SPI DMA FIFO waiting
633 * to be transmitted ... software needs to poll TXS in the SPI_STAT
634 * register until it goes low for 2 successive reads
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700635 */
636 if (drv_data->tx != NULL) {
Bryan Wua32c6912007-12-04 23:45:15 -0800637 while ((read_STAT() & TXS) ||
638 (read_STAT() & TXS))
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700639 continue;
640 }
641
Bryan Wua32c6912007-12-04 23:45:15 -0800642 while (!(read_STAT() & SPIF))
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700643 continue;
644
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700645 msg->actual_length += drv_data->len_in_bytes;
646
Bryan Wufad91c82007-12-04 23:45:14 -0800647 if (drv_data->cs_change)
648 cs_deactive(chip);
649
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700650 /* Move to next transfer */
651 msg->state = next_transfer(drv_data);
652
653 /* Schedule transfer tasklet */
654 tasklet_schedule(&drv_data->pump_transfers);
655
656 /* free the irq handler before next transfer */
Bryan Wu88b40362007-05-21 18:32:16 +0800657 dev_dbg(&drv_data->pdev->dev,
658 "disable dma channel irq%d\n",
Bryan Wua32c6912007-12-04 23:45:15 -0800659 spi_dma_ch);
660 dma_disable_irq(spi_dma_ch);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700661
662 return IRQ_HANDLED;
663}
664
665static void pump_transfers(unsigned long data)
666{
667 struct driver_data *drv_data = (struct driver_data *)data;
668 struct spi_message *message = NULL;
669 struct spi_transfer *transfer = NULL;
670 struct spi_transfer *previous = NULL;
671 struct chip_data *chip = NULL;
Bryan Wu88b40362007-05-21 18:32:16 +0800672 u8 width;
673 u16 cr, dma_width, dma_config;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700674 u32 tranf_success = 1;
675
676 /* Get current state information */
677 message = drv_data->cur_msg;
678 transfer = drv_data->cur_transfer;
679 chip = drv_data->cur_chip;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700680 /*
681 * if msg is error or done, report it back using complete() callback
682 */
683
684 /* Handle for abort */
685 if (message->state == ERROR_STATE) {
686 message->status = -EIO;
687 giveback(drv_data);
688 return;
689 }
690
691 /* Handle end of message */
692 if (message->state == DONE_STATE) {
693 message->status = 0;
694 giveback(drv_data);
695 return;
696 }
697
698 /* Delay if requested at end of transfer */
699 if (message->state == RUNNING_STATE) {
700 previous = list_entry(transfer->transfer_list.prev,
701 struct spi_transfer, transfer_list);
702 if (previous->delay_usecs)
703 udelay(previous->delay_usecs);
704 }
705
706 /* Setup the transfer state based on the type of transfer */
707 if (flush(drv_data) == 0) {
708 dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
709 message->status = -EIO;
710 giveback(drv_data);
711 return;
712 }
713
714 if (transfer->tx_buf != NULL) {
715 drv_data->tx = (void *)transfer->tx_buf;
716 drv_data->tx_end = drv_data->tx + transfer->len;
Bryan Wu88b40362007-05-21 18:32:16 +0800717 dev_dbg(&drv_data->pdev->dev, "tx_buf is %p, tx_end is %p\n",
718 transfer->tx_buf, drv_data->tx_end);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700719 } else {
720 drv_data->tx = NULL;
721 }
722
723 if (transfer->rx_buf != NULL) {
724 drv_data->rx = transfer->rx_buf;
725 drv_data->rx_end = drv_data->rx + transfer->len;
Bryan Wu88b40362007-05-21 18:32:16 +0800726 dev_dbg(&drv_data->pdev->dev, "rx_buf is %p, rx_end is %p\n",
727 transfer->rx_buf, drv_data->rx_end);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700728 } else {
729 drv_data->rx = NULL;
730 }
731
732 drv_data->rx_dma = transfer->rx_dma;
733 drv_data->tx_dma = transfer->tx_dma;
734 drv_data->len_in_bytes = transfer->len;
Bryan Wufad91c82007-12-04 23:45:14 -0800735 drv_data->cs_change = transfer->cs_change;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700736
737 width = chip->width;
738 if (width == CFG_SPI_WORDSIZE16) {
739 drv_data->len = (transfer->len) >> 1;
740 } else {
741 drv_data->len = transfer->len;
742 }
743 drv_data->write = drv_data->tx ? chip->write : null_writer;
744 drv_data->read = drv_data->rx ? chip->read : null_reader;
745 drv_data->duplex = chip->duplex ? chip->duplex : null_writer;
Bryan Wu131b17d2007-12-04 23:45:12 -0800746 dev_dbg(&drv_data->pdev->dev, "transfer: ",
747 "drv_data->write is %p, chip->write is %p, null_wr is %p\n",
748 drv_data->write, chip->write, null_writer);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700749
750 /* speed and width has been set on per message */
751 message->state = RUNNING_STATE;
752 dma_config = 0;
753
Sonic Zhangcc487e72007-12-04 23:45:17 -0800754 write_STAT(BIT_STAT_CLR);
755 cr = (read_CTRL() & (~BIT_CTL_TIMOD));
Bryan Wufad91c82007-12-04 23:45:14 -0800756 cs_active(chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700757
Bryan Wu88b40362007-05-21 18:32:16 +0800758 dev_dbg(&drv_data->pdev->dev,
759 "now pumping a transfer: width is %d, len is %d\n",
760 width, transfer->len);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700761
762 /*
763 * Try to map dma buffer and do a dma transfer if
764 * successful use different way to r/w according to
765 * drv_data->cur_chip->enable_dma
766 */
767 if (drv_data->cur_chip->enable_dma && drv_data->len > 6) {
768
Bryan Wua32c6912007-12-04 23:45:15 -0800769 disable_dma(spi_dma_ch);
770 clear_dma_irqstat(spi_dma_ch);
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");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700774 if (width == CFG_SPI_WORDSIZE16) {
Bryan Wua32c6912007-12-04 23:45:15 -0800775 set_dma_x_count(spi_dma_ch, drv_data->len);
776 set_dma_x_modify(spi_dma_ch, 2);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700777 dma_width = WDSIZE_16;
778 } else {
Bryan Wua32c6912007-12-04 23:45:15 -0800779 set_dma_x_count(spi_dma_ch, drv_data->len);
780 set_dma_x_modify(spi_dma_ch, 1);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700781 dma_width = WDSIZE_8;
782 }
783
Sonic Zhang3f479a62007-12-04 23:45:18 -0800784 /* poll for SPI completion before start */
785 while (!(read_STAT() & BIT_STAT_SPIF))
786 continue;
787
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700788 /* dirty hack for autobuffer DMA mode */
789 if (drv_data->tx_dma == 0xFFFF) {
Bryan Wu88b40362007-05-21 18:32:16 +0800790 dev_dbg(&drv_data->pdev->dev,
791 "doing autobuffer DMA out.\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700792
Sonic Zhangcc487e72007-12-04 23:45:17 -0800793 /* set SPI transfer mode */
794 write_CTRL(cr | CFG_SPI_DMAWRITE);
795
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700796 /* no irq in autobuffer mode */
797 dma_config =
798 (DMAFLOW_AUTO | RESTART | dma_width | DI_EN);
Bryan Wua32c6912007-12-04 23:45:15 -0800799 set_dma_config(spi_dma_ch, dma_config);
800 set_dma_start_addr(spi_dma_ch,
801 (unsigned long)drv_data->tx);
802 enable_dma(spi_dma_ch);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700803
804 /* just return here, there can only be one transfer in this mode */
805 message->status = 0;
806 giveback(drv_data);
807 return;
808 }
809
810 /* In dma mode, rx or tx must be NULL in one transfer */
811 if (drv_data->rx != NULL) {
812 /* set transfer mode, and enable SPI */
Bryan Wu88b40362007-05-21 18:32:16 +0800813 dev_dbg(&drv_data->pdev->dev, "doing DMA in.\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700814
Sonic Zhangcc487e72007-12-04 23:45:17 -0800815 /* set SPI transfer mode */
816 write_CTRL(cr | CFG_SPI_DMAREAD);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700817
818 /* clear tx reg soformer data is not shifted out */
Sonic Zhangcc487e72007-12-04 23:45:17 -0800819 write_TDBR(0xFFFF);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700820
Bryan Wua32c6912007-12-04 23:45:15 -0800821 set_dma_x_count(spi_dma_ch, drv_data->len);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700822
823 /* start dma */
Bryan Wua32c6912007-12-04 23:45:15 -0800824 dma_enable_irq(spi_dma_ch);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700825 dma_config = (WNR | RESTART | dma_width | DI_EN);
Bryan Wua32c6912007-12-04 23:45:15 -0800826 set_dma_config(spi_dma_ch, dma_config);
827 set_dma_start_addr(spi_dma_ch,
828 (unsigned long)drv_data->rx);
829 enable_dma(spi_dma_ch);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700830
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700831 } else if (drv_data->tx != NULL) {
Bryan Wu88b40362007-05-21 18:32:16 +0800832 dev_dbg(&drv_data->pdev->dev, "doing DMA out.\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700833
Sonic Zhangcc487e72007-12-04 23:45:17 -0800834 /* set SPI transfer mode */
835 write_CTRL(cr | CFG_SPI_DMAWRITE);
836
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700837 /* start dma */
Bryan Wua32c6912007-12-04 23:45:15 -0800838 dma_enable_irq(spi_dma_ch);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700839 dma_config = (RESTART | dma_width | DI_EN);
Bryan Wua32c6912007-12-04 23:45:15 -0800840 set_dma_config(spi_dma_ch, dma_config);
841 set_dma_start_addr(spi_dma_ch,
842 (unsigned long)drv_data->tx);
843 enable_dma(spi_dma_ch);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700844 }
845 } else {
846 /* IO mode write then read */
Bryan Wu88b40362007-05-21 18:32:16 +0800847 dev_dbg(&drv_data->pdev->dev, "doing IO transfer\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700848
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700849 if (drv_data->tx != NULL && drv_data->rx != NULL) {
850 /* full duplex mode */
851 BUG_ON((drv_data->tx_end - drv_data->tx) !=
852 (drv_data->rx_end - drv_data->rx));
Bryan Wu88b40362007-05-21 18:32:16 +0800853 dev_dbg(&drv_data->pdev->dev,
854 "IO duplex: cr is 0x%x\n", cr);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700855
Sonic Zhangcc487e72007-12-04 23:45:17 -0800856 /* set SPI transfer mode */
857 write_CTRL(cr | CFG_SPI_WRITE);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700858
859 drv_data->duplex(drv_data);
860
861 if (drv_data->tx != drv_data->tx_end)
862 tranf_success = 0;
863 } else if (drv_data->tx != NULL) {
864 /* write only half duplex */
Bryan Wu131b17d2007-12-04 23:45:12 -0800865 dev_dbg(&drv_data->pdev->dev,
Bryan Wu88b40362007-05-21 18:32:16 +0800866 "IO write: cr is 0x%x\n", cr);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700867
Sonic Zhangcc487e72007-12-04 23:45:17 -0800868 /* set SPI transfer mode */
869 write_CTRL(cr | CFG_SPI_WRITE);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700870
871 drv_data->write(drv_data);
872
873 if (drv_data->tx != drv_data->tx_end)
874 tranf_success = 0;
875 } else if (drv_data->rx != NULL) {
876 /* read only half duplex */
Bryan Wu131b17d2007-12-04 23:45:12 -0800877 dev_dbg(&drv_data->pdev->dev,
Bryan Wu88b40362007-05-21 18:32:16 +0800878 "IO read: cr is 0x%x\n", cr);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700879
Sonic Zhangcc487e72007-12-04 23:45:17 -0800880 /* set SPI transfer mode */
881 write_CTRL(cr | CFG_SPI_READ);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700882
883 drv_data->read(drv_data);
884 if (drv_data->rx != drv_data->rx_end)
885 tranf_success = 0;
886 }
887
888 if (!tranf_success) {
Bryan Wu131b17d2007-12-04 23:45:12 -0800889 dev_dbg(&drv_data->pdev->dev,
Bryan Wu88b40362007-05-21 18:32:16 +0800890 "IO write error!\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700891 message->state = ERROR_STATE;
892 } else {
893 /* Update total byte transfered */
894 message->actual_length += drv_data->len;
895
896 /* Move to next transfer of this msg */
897 message->state = next_transfer(drv_data);
898 }
899
900 /* Schedule next transfer tasklet */
901 tasklet_schedule(&drv_data->pump_transfers);
902
903 }
904}
905
906/* pop a msg from queue and kick off real transfer */
907static void pump_messages(struct work_struct *work)
908{
Bryan Wu131b17d2007-12-04 23:45:12 -0800909 struct driver_data *drv_data;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700910 unsigned long flags;
911
Bryan Wu131b17d2007-12-04 23:45:12 -0800912 drv_data = container_of(work, struct driver_data, pump_messages);
913
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700914 /* Lock queue and check for queue work */
915 spin_lock_irqsave(&drv_data->lock, flags);
916 if (list_empty(&drv_data->queue) || drv_data->run == QUEUE_STOPPED) {
917 /* pumper kicked off but no work to do */
918 drv_data->busy = 0;
919 spin_unlock_irqrestore(&drv_data->lock, flags);
920 return;
921 }
922
923 /* Make sure we are not already running a message */
924 if (drv_data->cur_msg) {
925 spin_unlock_irqrestore(&drv_data->lock, flags);
926 return;
927 }
928
929 /* Extract head of queue */
930 drv_data->cur_msg = list_entry(drv_data->queue.next,
931 struct spi_message, queue);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800932
933 /* Setup the SSP using the per chip configuration */
934 drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
935 if (restore_state(drv_data)) {
936 spin_unlock_irqrestore(&drv_data->lock, flags);
937 return;
938 };
939
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700940 list_del_init(&drv_data->cur_msg->queue);
941
942 /* Initial message state */
943 drv_data->cur_msg->state = START_STATE;
944 drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
945 struct spi_transfer, transfer_list);
946
Bryan Wu5fec5b52007-12-04 23:45:13 -0800947 dev_dbg(&drv_data->pdev->dev, "got a message to pump, "
948 "state is set to: baud %d, flag 0x%x, ctl 0x%x\n",
949 drv_data->cur_chip->baud, drv_data->cur_chip->flag,
950 drv_data->cur_chip->ctl_reg);
Bryan Wu131b17d2007-12-04 23:45:12 -0800951
952 dev_dbg(&drv_data->pdev->dev,
Bryan Wu88b40362007-05-21 18:32:16 +0800953 "the first transfer len is %d\n",
954 drv_data->cur_transfer->len);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700955
956 /* Mark as busy and launch transfers */
957 tasklet_schedule(&drv_data->pump_transfers);
958
959 drv_data->busy = 1;
960 spin_unlock_irqrestore(&drv_data->lock, flags);
961}
962
963/*
964 * got a msg to transfer, queue it in drv_data->queue.
965 * And kick off message pumper
966 */
967static int transfer(struct spi_device *spi, struct spi_message *msg)
968{
969 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
970 unsigned long flags;
971
972 spin_lock_irqsave(&drv_data->lock, flags);
973
974 if (drv_data->run == QUEUE_STOPPED) {
975 spin_unlock_irqrestore(&drv_data->lock, flags);
976 return -ESHUTDOWN;
977 }
978
979 msg->actual_length = 0;
980 msg->status = -EINPROGRESS;
981 msg->state = START_STATE;
982
Bryan Wu88b40362007-05-21 18:32:16 +0800983 dev_dbg(&spi->dev, "adding an msg in transfer() \n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700984 list_add_tail(&msg->queue, &drv_data->queue);
985
986 if (drv_data->run == QUEUE_RUNNING && !drv_data->busy)
987 queue_work(drv_data->workqueue, &drv_data->pump_messages);
988
989 spin_unlock_irqrestore(&drv_data->lock, flags);
990
991 return 0;
992}
993
Sonic Zhang12e17c42007-12-04 23:45:16 -0800994#define MAX_SPI_SSEL 7
995
996static u16 ssel[3][MAX_SPI_SSEL] = {
997 {P_SPI0_SSEL1, P_SPI0_SSEL2, P_SPI0_SSEL3,
998 P_SPI0_SSEL4, P_SPI0_SSEL5,
999 P_SPI0_SSEL6, P_SPI0_SSEL7},
1000
1001 {P_SPI1_SSEL1, P_SPI1_SSEL2, P_SPI1_SSEL3,
1002 P_SPI1_SSEL4, P_SPI1_SSEL5,
1003 P_SPI1_SSEL6, P_SPI1_SSEL7},
1004
1005 {P_SPI2_SSEL1, P_SPI2_SSEL2, P_SPI2_SSEL3,
1006 P_SPI2_SSEL4, P_SPI2_SSEL5,
1007 P_SPI2_SSEL6, P_SPI2_SSEL7},
1008};
1009
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001010/* first setup for new devices */
1011static int setup(struct spi_device *spi)
1012{
1013 struct bfin5xx_spi_chip *chip_info = NULL;
1014 struct chip_data *chip;
1015 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
1016 u8 spi_flg;
1017
1018 /* Abort device setup if requested features are not supported */
1019 if (spi->mode & ~(SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST)) {
1020 dev_err(&spi->dev, "requested mode not fully supported\n");
1021 return -EINVAL;
1022 }
1023
1024 /* Zero (the default) here means 8 bits */
1025 if (!spi->bits_per_word)
1026 spi->bits_per_word = 8;
1027
1028 if (spi->bits_per_word != 8 && spi->bits_per_word != 16)
1029 return -EINVAL;
1030
1031 /* Only alloc (or use chip_info) on first setup */
1032 chip = spi_get_ctldata(spi);
1033 if (chip == NULL) {
1034 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
1035 if (!chip)
1036 return -ENOMEM;
1037
1038 chip->enable_dma = 0;
1039 chip_info = spi->controller_data;
1040 }
1041
1042 /* chip_info isn't always needed */
1043 if (chip_info) {
Mike Frysinger2ed35512007-12-04 23:45:14 -08001044 /* Make sure people stop trying to set fields via ctl_reg
1045 * when they should actually be using common SPI framework.
1046 * Currently we let through: WOM EMISO PSSE GM SZ TIMOD.
1047 * Not sure if a user actually needs/uses any of these,
1048 * but let's assume (for now) they do.
1049 */
1050 if (chip_info->ctl_reg & (SPE|MSTR|CPOL|CPHA|LSBF|SIZE)) {
1051 dev_err(&spi->dev, "do not set bits in ctl_reg "
1052 "that the SPI framework manages\n");
1053 return -EINVAL;
1054 }
1055
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001056 chip->enable_dma = chip_info->enable_dma != 0
1057 && drv_data->master_info->enable_dma;
1058 chip->ctl_reg = chip_info->ctl_reg;
1059 chip->bits_per_word = chip_info->bits_per_word;
1060 chip->cs_change_per_word = chip_info->cs_change_per_word;
1061 chip->cs_chg_udelay = chip_info->cs_chg_udelay;
1062 }
1063
1064 /* translate common spi framework into our register */
1065 if (spi->mode & SPI_CPOL)
1066 chip->ctl_reg |= CPOL;
1067 if (spi->mode & SPI_CPHA)
1068 chip->ctl_reg |= CPHA;
1069 if (spi->mode & SPI_LSB_FIRST)
1070 chip->ctl_reg |= LSBF;
1071 /* we dont support running in slave mode (yet?) */
1072 chip->ctl_reg |= MSTR;
1073
1074 /*
1075 * if any one SPI chip is registered and wants DMA, request the
1076 * DMA channel for it
1077 */
1078 if (chip->enable_dma && !dma_requested) {
1079 /* register dma irq handler */
Bryan Wua32c6912007-12-04 23:45:15 -08001080 if (request_dma(spi_dma_ch, "BF53x_SPI_DMA") < 0) {
Bryan Wu88b40362007-05-21 18:32:16 +08001081 dev_dbg(&spi->dev,
1082 "Unable to request BlackFin SPI DMA channel\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001083 return -ENODEV;
1084 }
Bryan Wua32c6912007-12-04 23:45:15 -08001085 if (set_dma_callback(spi_dma_ch, (void *)dma_irq_handler,
1086 drv_data) < 0) {
Bryan Wu88b40362007-05-21 18:32:16 +08001087 dev_dbg(&spi->dev, "Unable to set dma callback\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001088 return -EPERM;
1089 }
Bryan Wua32c6912007-12-04 23:45:15 -08001090 dma_disable_irq(spi_dma_ch);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001091 dma_requested = 1;
1092 }
1093
1094 /*
1095 * Notice: for blackfin, the speed_hz is the value of register
1096 * SPI_BAUD, not the real baudrate
1097 */
1098 chip->baud = hz_to_spi_baud(spi->max_speed_hz);
1099 spi_flg = ~(1 << (spi->chip_select));
1100 chip->flag = ((u16) spi_flg << 8) | (1 << (spi->chip_select));
1101 chip->chip_select_num = spi->chip_select;
1102
1103 switch (chip->bits_per_word) {
1104 case 8:
1105 chip->n_bytes = 1;
1106 chip->width = CFG_SPI_WORDSIZE8;
1107 chip->read = chip->cs_change_per_word ?
1108 u8_cs_chg_reader : u8_reader;
1109 chip->write = chip->cs_change_per_word ?
1110 u8_cs_chg_writer : u8_writer;
1111 chip->duplex = chip->cs_change_per_word ?
1112 u8_cs_chg_duplex : u8_duplex;
1113 break;
1114
1115 case 16:
1116 chip->n_bytes = 2;
1117 chip->width = CFG_SPI_WORDSIZE16;
1118 chip->read = chip->cs_change_per_word ?
1119 u16_cs_chg_reader : u16_reader;
1120 chip->write = chip->cs_change_per_word ?
1121 u16_cs_chg_writer : u16_writer;
1122 chip->duplex = chip->cs_change_per_word ?
1123 u16_cs_chg_duplex : u16_duplex;
1124 break;
1125
1126 default:
1127 dev_err(&spi->dev, "%d bits_per_word is not supported\n",
1128 chip->bits_per_word);
1129 kfree(chip);
1130 return -ENODEV;
1131 }
1132
Joe Perches898eb712007-10-18 03:06:30 -07001133 dev_dbg(&spi->dev, "setup spi chip %s, width is %d, dma is %d\n",
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001134 spi->modalias, chip->width, chip->enable_dma);
Bryan Wu88b40362007-05-21 18:32:16 +08001135 dev_dbg(&spi->dev, "ctl_reg is 0x%x, flag_reg is 0x%x\n",
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001136 chip->ctl_reg, chip->flag);
1137
1138 spi_set_ctldata(spi, chip);
1139
Sonic Zhang12e17c42007-12-04 23:45:16 -08001140 dev_dbg(&spi->dev, "chip select number is %d\n", chip->chip_select_num);
1141 if ((chip->chip_select_num > 0)
1142 && (chip->chip_select_num <= spi->master->num_chipselect))
1143 peripheral_request(ssel[spi->master->bus_num]
1144 [chip->chip_select_num-1], DRV_NAME);
1145
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001146 return 0;
1147}
1148
1149/*
1150 * callback for spi framework.
1151 * clean driver specific data
1152 */
Bryan Wu88b40362007-05-21 18:32:16 +08001153static void cleanup(struct spi_device *spi)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001154{
Mike Frysinger27bb9e72007-06-11 15:31:30 +08001155 struct chip_data *chip = spi_get_ctldata(spi);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001156
Sonic Zhang12e17c42007-12-04 23:45:16 -08001157 if ((chip->chip_select_num > 0)
1158 && (chip->chip_select_num <= spi->master->num_chipselect))
1159 peripheral_free(ssel[spi->master->bus_num]
1160 [chip->chip_select_num-1]);
1161
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001162 kfree(chip);
1163}
1164
1165static inline int init_queue(struct driver_data *drv_data)
1166{
1167 INIT_LIST_HEAD(&drv_data->queue);
1168 spin_lock_init(&drv_data->lock);
1169
1170 drv_data->run = QUEUE_STOPPED;
1171 drv_data->busy = 0;
1172
1173 /* init transfer tasklet */
1174 tasklet_init(&drv_data->pump_transfers,
1175 pump_transfers, (unsigned long)drv_data);
1176
1177 /* init messages workqueue */
1178 INIT_WORK(&drv_data->pump_messages, pump_messages);
1179 drv_data->workqueue =
Tony Jones49dce682007-10-16 01:27:48 -07001180 create_singlethread_workqueue(drv_data->master->dev.parent->bus_id);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001181 if (drv_data->workqueue == NULL)
1182 return -EBUSY;
1183
1184 return 0;
1185}
1186
1187static inline int start_queue(struct driver_data *drv_data)
1188{
1189 unsigned long flags;
1190
1191 spin_lock_irqsave(&drv_data->lock, flags);
1192
1193 if (drv_data->run == QUEUE_RUNNING || drv_data->busy) {
1194 spin_unlock_irqrestore(&drv_data->lock, flags);
1195 return -EBUSY;
1196 }
1197
1198 drv_data->run = QUEUE_RUNNING;
1199 drv_data->cur_msg = NULL;
1200 drv_data->cur_transfer = NULL;
1201 drv_data->cur_chip = NULL;
1202 spin_unlock_irqrestore(&drv_data->lock, flags);
1203
1204 queue_work(drv_data->workqueue, &drv_data->pump_messages);
1205
1206 return 0;
1207}
1208
1209static inline int stop_queue(struct driver_data *drv_data)
1210{
1211 unsigned long flags;
1212 unsigned limit = 500;
1213 int status = 0;
1214
1215 spin_lock_irqsave(&drv_data->lock, flags);
1216
1217 /*
1218 * This is a bit lame, but is optimized for the common execution path.
1219 * A wait_queue on the drv_data->busy could be used, but then the common
1220 * execution path (pump_messages) would be required to call wake_up or
1221 * friends on every SPI message. Do this instead
1222 */
1223 drv_data->run = QUEUE_STOPPED;
1224 while (!list_empty(&drv_data->queue) && drv_data->busy && limit--) {
1225 spin_unlock_irqrestore(&drv_data->lock, flags);
1226 msleep(10);
1227 spin_lock_irqsave(&drv_data->lock, flags);
1228 }
1229
1230 if (!list_empty(&drv_data->queue) || drv_data->busy)
1231 status = -EBUSY;
1232
1233 spin_unlock_irqrestore(&drv_data->lock, flags);
1234
1235 return status;
1236}
1237
1238static inline int destroy_queue(struct driver_data *drv_data)
1239{
1240 int status;
1241
1242 status = stop_queue(drv_data);
1243 if (status != 0)
1244 return status;
1245
1246 destroy_workqueue(drv_data->workqueue);
1247
1248 return 0;
1249}
1250
Sonic Zhang7c4ef092007-12-04 23:45:16 -08001251static int setup_pin_mux(int action, int bus_num)
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001252{
1253
Sonic Zhang7c4ef092007-12-04 23:45:16 -08001254 u16 pin_req[3][4] = {
1255 {P_SPI0_SCK, P_SPI0_MISO, P_SPI0_MOSI, 0},
1256 {P_SPI1_SCK, P_SPI1_MISO, P_SPI1_MOSI, 0},
1257 {P_SPI2_SCK, P_SPI2_MISO, P_SPI2_MOSI, 0},
1258 };
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001259
1260 if (action) {
Sonic Zhang7c4ef092007-12-04 23:45:16 -08001261 if (peripheral_request_list(pin_req[bus_num], DRV_NAME))
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001262 return -EFAULT;
1263 } else {
Sonic Zhang7c4ef092007-12-04 23:45:16 -08001264 peripheral_free_list(pin_req[bus_num]);
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001265 }
1266
1267 return 0;
1268}
1269
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001270static int __init bfin5xx_spi_probe(struct platform_device *pdev)
1271{
1272 struct device *dev = &pdev->dev;
1273 struct bfin5xx_spi_master *platform_info;
1274 struct spi_master *master;
1275 struct driver_data *drv_data = 0;
Bryan Wua32c6912007-12-04 23:45:15 -08001276 struct resource *res;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001277 int status = 0;
1278
1279 platform_info = dev->platform_data;
1280
1281 /* Allocate master with space for drv_data */
1282 master = spi_alloc_master(dev, sizeof(struct driver_data) + 16);
1283 if (!master) {
1284 dev_err(&pdev->dev, "can not alloc spi_master\n");
1285 return -ENOMEM;
1286 }
Bryan Wu131b17d2007-12-04 23:45:12 -08001287
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001288 drv_data = spi_master_get_devdata(master);
1289 drv_data->master = master;
1290 drv_data->master_info = platform_info;
1291 drv_data->pdev = pdev;
1292
1293 master->bus_num = pdev->id;
1294 master->num_chipselect = platform_info->num_chipselect;
1295 master->cleanup = cleanup;
1296 master->setup = setup;
1297 master->transfer = transfer;
1298
Bryan Wua32c6912007-12-04 23:45:15 -08001299 /* Find and map our resources */
1300 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1301 if (res == NULL) {
1302 dev_err(dev, "Cannot get IORESOURCE_MEM\n");
1303 status = -ENOENT;
1304 goto out_error_get_res;
1305 }
1306
1307 spi_regs_base = (u32) ioremap(res->start, (res->end - res->start)+1);
1308 if (!spi_regs_base) {
1309 dev_err(dev, "Cannot map IO\n");
1310 status = -ENXIO;
1311 goto out_error_ioremap;
1312 }
1313
1314 spi_dma_ch = platform_get_irq(pdev, 0);
1315 if (spi_dma_ch < 0) {
1316 dev_err(dev, "No DMA channel specified\n");
1317 status = -ENOENT;
1318 goto out_error_no_dma_ch;
1319 }
1320
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001321 /* Initial and start queue */
1322 status = init_queue(drv_data);
1323 if (status != 0) {
Bryan Wua32c6912007-12-04 23:45:15 -08001324 dev_err(dev, "problem initializing queue\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001325 goto out_error_queue_alloc;
1326 }
Bryan Wua32c6912007-12-04 23:45:15 -08001327
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001328 status = start_queue(drv_data);
1329 if (status != 0) {
Bryan Wua32c6912007-12-04 23:45:15 -08001330 dev_err(dev, "problem starting queue\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001331 goto out_error_queue_alloc;
1332 }
1333
1334 /* Register with the SPI framework */
1335 platform_set_drvdata(pdev, drv_data);
1336 status = spi_register_master(master);
1337 if (status != 0) {
Bryan Wua32c6912007-12-04 23:45:15 -08001338 dev_err(dev, "problem registering spi master\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001339 goto out_error_queue_alloc;
1340 }
Bryan Wua32c6912007-12-04 23:45:15 -08001341
Sonic Zhang7c4ef092007-12-04 23:45:16 -08001342 if (setup_pin_mux(1, master->bus_num)) {
1343 dev_err(&pdev->dev, ": Requesting Peripherals failed\n");
1344 goto out_error;
1345 }
1346
Bryan Wua32c6912007-12-04 23:45:15 -08001347 dev_info(dev, "%s, Version %s, regs_base @ 0x%08x\n",
1348 DRV_DESC, DRV_VERSION, spi_regs_base);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001349 return status;
1350
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001351out_error_queue_alloc:
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001352 destroy_queue(drv_data);
Bryan Wua32c6912007-12-04 23:45:15 -08001353out_error_no_dma_ch:
1354 iounmap((void *) spi_regs_base);
1355out_error_ioremap:
1356out_error_get_res:
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001357out_error:
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001358 spi_master_put(master);
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001359
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001360 return status;
1361}
1362
1363/* stop hardware and remove the driver */
1364static int __devexit bfin5xx_spi_remove(struct platform_device *pdev)
1365{
1366 struct driver_data *drv_data = platform_get_drvdata(pdev);
1367 int status = 0;
1368
1369 if (!drv_data)
1370 return 0;
1371
1372 /* Remove the queue */
1373 status = destroy_queue(drv_data);
1374 if (status != 0)
1375 return status;
1376
1377 /* Disable the SSP at the peripheral and SOC level */
1378 bfin_spi_disable(drv_data);
1379
1380 /* Release DMA */
1381 if (drv_data->master_info->enable_dma) {
Bryan Wua32c6912007-12-04 23:45:15 -08001382 if (dma_channel_active(spi_dma_ch))
1383 free_dma(spi_dma_ch);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001384 }
1385
1386 /* Disconnect from the SPI framework */
1387 spi_unregister_master(drv_data->master);
1388
Sonic Zhang7c4ef092007-12-04 23:45:16 -08001389 setup_pin_mux(0, drv_data->master->bus_num);
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001390
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001391 /* Prevent double remove */
1392 platform_set_drvdata(pdev, NULL);
1393
1394 return 0;
1395}
1396
1397#ifdef CONFIG_PM
1398static int bfin5xx_spi_suspend(struct platform_device *pdev, pm_message_t state)
1399{
1400 struct driver_data *drv_data = platform_get_drvdata(pdev);
1401 int status = 0;
1402
1403 status = stop_queue(drv_data);
1404 if (status != 0)
1405 return status;
1406
1407 /* stop hardware */
1408 bfin_spi_disable(drv_data);
1409
1410 return 0;
1411}
1412
1413static int bfin5xx_spi_resume(struct platform_device *pdev)
1414{
1415 struct driver_data *drv_data = platform_get_drvdata(pdev);
1416 int status = 0;
1417
1418 /* Enable the SPI interface */
1419 bfin_spi_enable(drv_data);
1420
1421 /* Start the queue running */
1422 status = start_queue(drv_data);
1423 if (status != 0) {
1424 dev_err(&pdev->dev, "problem starting queue (%d)\n", status);
1425 return status;
1426 }
1427
1428 return 0;
1429}
1430#else
1431#define bfin5xx_spi_suspend NULL
1432#define bfin5xx_spi_resume NULL
1433#endif /* CONFIG_PM */
1434
David Brownellfc3ba952007-08-30 23:56:24 -07001435MODULE_ALIAS("bfin-spi-master"); /* for platform bus hotplug */
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001436static struct platform_driver bfin5xx_spi_driver = {
David Brownellfc3ba952007-08-30 23:56:24 -07001437 .driver = {
Bryan Wua32c6912007-12-04 23:45:15 -08001438 .name = DRV_NAME,
Bryan Wu88b40362007-05-21 18:32:16 +08001439 .owner = THIS_MODULE,
1440 },
1441 .suspend = bfin5xx_spi_suspend,
1442 .resume = bfin5xx_spi_resume,
1443 .remove = __devexit_p(bfin5xx_spi_remove),
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001444};
1445
1446static int __init bfin5xx_spi_init(void)
1447{
Bryan Wu88b40362007-05-21 18:32:16 +08001448 return platform_driver_probe(&bfin5xx_spi_driver, bfin5xx_spi_probe);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001449}
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001450module_init(bfin5xx_spi_init);
1451
1452static void __exit bfin5xx_spi_exit(void)
1453{
1454 platform_driver_unregister(&bfin5xx_spi_driver);
1455}
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001456module_exit(bfin5xx_spi_exit);