blob: 4dc7e67493220866e1c70bc8414298518d321f5b [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
Bryan Wubb90eb02007-12-04 23:45:18 -080064#define IS_DMA_ALIGNED(x) (((u32)(x)&0x07) == 0)
Wu, Bryana5f6abd2007-05-06 14:50:34 -070065
Bryan Wubb90eb02007-12-04 23:45:18 -080066#define START_STATE ((void *)0)
67#define RUNNING_STATE ((void *)1)
68#define DONE_STATE ((void *)2)
69#define ERROR_STATE ((void *)-1)
70#define QUEUE_RUNNING 0
71#define QUEUE_STOPPED 1
Wu, Bryana5f6abd2007-05-06 14:50:34 -070072
73struct driver_data {
74 /* Driver model hookup */
75 struct platform_device *pdev;
76
77 /* SPI framework hookup */
78 struct spi_master *master;
79
Bryan Wubb90eb02007-12-04 23:45:18 -080080 /* Regs base of SPI controller */
81 u32 regs_base;
82
Wu, Bryana5f6abd2007-05-06 14:50:34 -070083 /* BFIN hookup */
84 struct bfin5xx_spi_master *master_info;
85
86 /* Driver message queue */
87 struct workqueue_struct *workqueue;
88 struct work_struct pump_messages;
89 spinlock_t lock;
90 struct list_head queue;
91 int busy;
92 int run;
93
94 /* Message Transfer pump */
95 struct tasklet_struct pump_transfers;
96
97 /* Current message transfer state info */
98 struct spi_message *cur_msg;
99 struct spi_transfer *cur_transfer;
100 struct chip_data *cur_chip;
101 size_t len_in_bytes;
102 size_t len;
103 void *tx;
104 void *tx_end;
105 void *rx;
106 void *rx_end;
Bryan Wubb90eb02007-12-04 23:45:18 -0800107
108 /* DMA stuffs */
109 int dma_channel;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700110 int dma_mapped;
Bryan Wubb90eb02007-12-04 23:45:18 -0800111 int dma_requested;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700112 dma_addr_t rx_dma;
113 dma_addr_t tx_dma;
Bryan Wubb90eb02007-12-04 23:45:18 -0800114
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700115 size_t rx_map_len;
116 size_t tx_map_len;
117 u8 n_bytes;
Bryan Wufad91c82007-12-04 23:45:14 -0800118 int cs_change;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700119 void (*write) (struct driver_data *);
120 void (*read) (struct driver_data *);
121 void (*duplex) (struct driver_data *);
122};
123
124struct chip_data {
125 u16 ctl_reg;
126 u16 baud;
127 u16 flag;
128
129 u8 chip_select_num;
130 u8 n_bytes;
Bryan Wu88b40362007-05-21 18:32:16 +0800131 u8 width; /* 0 or 1 */
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700132 u8 enable_dma;
133 u8 bits_per_word; /* 8 or 16 */
134 u8 cs_change_per_word;
Bryan Wu62310e52007-12-04 23:45:20 -0800135 u16 cs_chg_udelay; /* Some devices require > 255usec delay */
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700136 void (*write) (struct driver_data *);
137 void (*read) (struct driver_data *);
138 void (*duplex) (struct driver_data *);
139};
140
Bryan Wubb90eb02007-12-04 23:45:18 -0800141#define DEFINE_SPI_REG(reg, off) \
142static inline u16 read_##reg(struct driver_data *drv_data) \
143 { return bfin_read16(drv_data->regs_base + off); } \
144static inline void write_##reg(struct driver_data *drv_data, u16 v) \
145 { bfin_write16(drv_data->regs_base + off, v); }
146
147DEFINE_SPI_REG(CTRL, 0x00)
148DEFINE_SPI_REG(FLAG, 0x04)
149DEFINE_SPI_REG(STAT, 0x08)
150DEFINE_SPI_REG(TDBR, 0x0C)
151DEFINE_SPI_REG(RDBR, 0x10)
152DEFINE_SPI_REG(BAUD, 0x14)
153DEFINE_SPI_REG(SHAW, 0x18)
154
Bryan Wu88b40362007-05-21 18:32:16 +0800155static void bfin_spi_enable(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700156{
157 u16 cr;
158
Bryan Wubb90eb02007-12-04 23:45:18 -0800159 cr = read_CTRL(drv_data);
160 write_CTRL(drv_data, (cr | BIT_CTL_ENABLE));
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700161}
162
Bryan Wu88b40362007-05-21 18:32:16 +0800163static void bfin_spi_disable(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700164{
165 u16 cr;
166
Bryan Wubb90eb02007-12-04 23:45:18 -0800167 cr = read_CTRL(drv_data);
168 write_CTRL(drv_data, (cr & (~BIT_CTL_ENABLE)));
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700169}
170
171/* Caculate the SPI_BAUD register value based on input HZ */
172static u16 hz_to_spi_baud(u32 speed_hz)
173{
174 u_long sclk = get_sclk();
175 u16 spi_baud = (sclk / (2 * speed_hz));
176
177 if ((sclk % (2 * speed_hz)) > 0)
178 spi_baud++;
179
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700180 return spi_baud;
181}
182
183static int flush(struct driver_data *drv_data)
184{
185 unsigned long limit = loops_per_jiffy << 1;
186
187 /* wait for stop and clear stat */
Bryan Wubb90eb02007-12-04 23:45:18 -0800188 while (!(read_STAT(drv_data) & BIT_STAT_SPIF) && limit--)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700189 continue;
190
Bryan Wubb90eb02007-12-04 23:45:18 -0800191 write_STAT(drv_data, BIT_STAT_CLR);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700192
193 return limit;
194}
195
Bryan Wufad91c82007-12-04 23:45:14 -0800196/* Chip select operation functions for cs_change flag */
Bryan Wubb90eb02007-12-04 23:45:18 -0800197static void cs_active(struct driver_data *drv_data, struct chip_data *chip)
Bryan Wufad91c82007-12-04 23:45:14 -0800198{
Bryan Wubb90eb02007-12-04 23:45:18 -0800199 u16 flag = read_FLAG(drv_data);
Bryan Wufad91c82007-12-04 23:45:14 -0800200
201 flag |= chip->flag;
202 flag &= ~(chip->flag << 8);
203
Bryan Wubb90eb02007-12-04 23:45:18 -0800204 write_FLAG(drv_data, flag);
Bryan Wufad91c82007-12-04 23:45:14 -0800205}
206
Bryan Wubb90eb02007-12-04 23:45:18 -0800207static void cs_deactive(struct driver_data *drv_data, struct chip_data *chip)
Bryan Wufad91c82007-12-04 23:45:14 -0800208{
Bryan Wubb90eb02007-12-04 23:45:18 -0800209 u16 flag = read_FLAG(drv_data);
Bryan Wufad91c82007-12-04 23:45:14 -0800210
211 flag |= (chip->flag << 8);
212
Bryan Wubb90eb02007-12-04 23:45:18 -0800213 write_FLAG(drv_data, flag);
Bryan Wu62310e52007-12-04 23:45:20 -0800214
215 /* Move delay here for consistency */
216 if (chip->cs_chg_udelay)
217 udelay(chip->cs_chg_udelay);
Bryan Wufad91c82007-12-04 23:45:14 -0800218}
219
Sonic Zhang7c4ef092007-12-04 23:45:16 -0800220#define MAX_SPI_SSEL 7
Bryan Wu5fec5b52007-12-04 23:45:13 -0800221
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700222/* stop controller and re-config current chip*/
Bryan Wu5fec5b52007-12-04 23:45:13 -0800223static int restore_state(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700224{
225 struct chip_data *chip = drv_data->cur_chip;
Bryan Wu5fec5b52007-12-04 23:45:13 -0800226 int ret = 0;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700227
228 /* Clear status and disable clock */
Bryan Wubb90eb02007-12-04 23:45:18 -0800229 write_STAT(drv_data, BIT_STAT_CLR);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700230 bfin_spi_disable(drv_data);
Bryan Wu88b40362007-05-21 18:32:16 +0800231 dev_dbg(&drv_data->pdev->dev, "restoring spi ctl state\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700232
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700233 /* Load the registers */
Bryan Wubb90eb02007-12-04 23:45:18 -0800234 cs_deactive(drv_data, chip);
235 write_BAUD(drv_data, chip->baud);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800236 chip->ctl_reg &= (~BIT_CTL_TIMOD);
237 chip->ctl_reg |= (chip->width << 8);
Bryan Wubb90eb02007-12-04 23:45:18 -0800238 write_CTRL(drv_data, chip->ctl_reg);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800239
240 bfin_spi_enable(drv_data);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800241
Bryan Wu5fec5b52007-12-04 23:45:13 -0800242 if (ret)
243 dev_dbg(&drv_data->pdev->dev,
244 ": request chip select number %d failed\n",
245 chip->chip_select_num);
246
247 return ret;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700248}
249
250/* used to kick off transfer in rx mode */
Bryan Wubb90eb02007-12-04 23:45:18 -0800251static unsigned short dummy_read(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700252{
253 unsigned short tmp;
Bryan Wubb90eb02007-12-04 23:45:18 -0800254 tmp = read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700255 return tmp;
256}
257
258static void null_writer(struct driver_data *drv_data)
259{
260 u8 n_bytes = drv_data->n_bytes;
261
262 while (drv_data->tx < drv_data->tx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800263 write_TDBR(drv_data, 0);
264 while ((read_STAT(drv_data) & BIT_STAT_TXS))
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700265 continue;
266 drv_data->tx += n_bytes;
267 }
268}
269
270static void null_reader(struct driver_data *drv_data)
271{
272 u8 n_bytes = drv_data->n_bytes;
Bryan Wubb90eb02007-12-04 23:45:18 -0800273 dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700274
275 while (drv_data->rx < drv_data->rx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800276 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700277 continue;
Bryan Wubb90eb02007-12-04 23:45:18 -0800278 dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700279 drv_data->rx += n_bytes;
280 }
281}
282
283static void u8_writer(struct driver_data *drv_data)
284{
Bryan Wu131b17d2007-12-04 23:45:12 -0800285 dev_dbg(&drv_data->pdev->dev,
Bryan Wubb90eb02007-12-04 23:45:18 -0800286 "cr8-s is 0x%x\n", read_STAT(drv_data));
Sonic Zhangcc487e72007-12-04 23:45:17 -0800287
Sonic Zhang3f479a62007-12-04 23:45:18 -0800288 /* poll for SPI completion before start */
Bryan Wubb90eb02007-12-04 23:45:18 -0800289 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Sonic Zhang3f479a62007-12-04 23:45:18 -0800290 continue;
291
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700292 while (drv_data->tx < drv_data->tx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800293 write_TDBR(drv_data, (*(u8 *) (drv_data->tx)));
294 while (read_STAT(drv_data) & BIT_STAT_TXS)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700295 continue;
296 ++drv_data->tx;
297 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700298}
299
300static void u8_cs_chg_writer(struct driver_data *drv_data)
301{
302 struct chip_data *chip = drv_data->cur_chip;
303
Sonic Zhang3f479a62007-12-04 23:45:18 -0800304 /* poll for SPI completion before start */
Bryan Wubb90eb02007-12-04 23:45:18 -0800305 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Sonic Zhang3f479a62007-12-04 23:45:18 -0800306 continue;
307
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700308 while (drv_data->tx < drv_data->tx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800309 cs_active(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700310
Bryan Wubb90eb02007-12-04 23:45:18 -0800311 write_TDBR(drv_data, (*(u8 *) (drv_data->tx)));
312 while (read_STAT(drv_data) & BIT_STAT_TXS)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700313 continue;
Bryan Wu62310e52007-12-04 23:45:20 -0800314
Bryan Wubb90eb02007-12-04 23:45:18 -0800315 cs_deactive(drv_data, chip);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800316
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700317 ++drv_data->tx;
318 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700319}
320
321static void u8_reader(struct driver_data *drv_data)
322{
Bryan Wu131b17d2007-12-04 23:45:12 -0800323 dev_dbg(&drv_data->pdev->dev,
Bryan Wubb90eb02007-12-04 23:45:18 -0800324 "cr-8 is 0x%x\n", read_STAT(drv_data));
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700325
Sonic Zhang3f479a62007-12-04 23:45:18 -0800326 /* poll for SPI completion before start */
Bryan Wubb90eb02007-12-04 23:45:18 -0800327 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Sonic Zhang3f479a62007-12-04 23:45:18 -0800328 continue;
329
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700330 /* clear TDBR buffer before read(else it will be shifted out) */
Bryan Wubb90eb02007-12-04 23:45:18 -0800331 write_TDBR(drv_data, 0xFFFF);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700332
Bryan Wubb90eb02007-12-04 23:45:18 -0800333 dummy_read(drv_data);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800334
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700335 while (drv_data->rx < drv_data->rx_end - 1) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800336 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700337 continue;
Bryan Wubb90eb02007-12-04 23:45:18 -0800338 *(u8 *) (drv_data->rx) = read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700339 ++drv_data->rx;
340 }
341
Bryan Wubb90eb02007-12-04 23:45:18 -0800342 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700343 continue;
Bryan Wubb90eb02007-12-04 23:45:18 -0800344 *(u8 *) (drv_data->rx) = read_SHAW(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700345 ++drv_data->rx;
346}
347
348static void u8_cs_chg_reader(struct driver_data *drv_data)
349{
350 struct chip_data *chip = drv_data->cur_chip;
351
Sonic Zhang3f479a62007-12-04 23:45:18 -0800352 /* poll for SPI completion before start */
Bryan Wubb90eb02007-12-04 23:45:18 -0800353 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Sonic Zhang3f479a62007-12-04 23:45:18 -0800354 continue;
355
Sonic Zhangcc487e72007-12-04 23:45:17 -0800356 /* clear TDBR buffer before read(else it will be shifted out) */
Bryan Wubb90eb02007-12-04 23:45:18 -0800357 write_TDBR(drv_data, 0xFFFF);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700358
Bryan Wubb90eb02007-12-04 23:45:18 -0800359 cs_active(drv_data, chip);
360 dummy_read(drv_data);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800361
362 while (drv_data->rx < drv_data->rx_end - 1) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800363 cs_deactive(drv_data, chip);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800364
Bryan Wubb90eb02007-12-04 23:45:18 -0800365 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Sonic Zhangcc487e72007-12-04 23:45:17 -0800366 continue;
Bryan Wubb90eb02007-12-04 23:45:18 -0800367 cs_active(drv_data, chip);
368 *(u8 *) (drv_data->rx) = read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700369 ++drv_data->rx;
370 }
Bryan Wubb90eb02007-12-04 23:45:18 -0800371 cs_deactive(drv_data, chip);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800372
Bryan Wubb90eb02007-12-04 23:45:18 -0800373 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Sonic Zhangcc487e72007-12-04 23:45:17 -0800374 continue;
Bryan Wubb90eb02007-12-04 23:45:18 -0800375 *(u8 *) (drv_data->rx) = read_SHAW(drv_data);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800376 ++drv_data->rx;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700377}
378
379static void u8_duplex(struct driver_data *drv_data)
380{
Sonic Zhang3f479a62007-12-04 23:45:18 -0800381 /* poll for SPI completion before start */
Bryan Wubb90eb02007-12-04 23:45:18 -0800382 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Sonic Zhang3f479a62007-12-04 23:45:18 -0800383 continue;
384
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700385 /* in duplex mode, clk is triggered by writing of TDBR */
386 while (drv_data->rx < drv_data->rx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800387 write_TDBR(drv_data, (*(u8 *) (drv_data->tx)));
388 while (read_STAT(drv_data) & BIT_STAT_TXS)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700389 continue;
Bryan Wubb90eb02007-12-04 23:45:18 -0800390 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700391 continue;
Bryan Wubb90eb02007-12-04 23:45:18 -0800392 *(u8 *) (drv_data->rx) = read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700393 ++drv_data->rx;
394 ++drv_data->tx;
395 }
396}
397
398static void u8_cs_chg_duplex(struct driver_data *drv_data)
399{
400 struct chip_data *chip = drv_data->cur_chip;
401
Sonic Zhang3f479a62007-12-04 23:45:18 -0800402 /* poll for SPI completion before start */
Bryan Wubb90eb02007-12-04 23:45:18 -0800403 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Sonic Zhang3f479a62007-12-04 23:45:18 -0800404 continue;
405
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700406 while (drv_data->rx < drv_data->rx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800407 cs_active(drv_data, chip);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800408
Bryan Wubb90eb02007-12-04 23:45:18 -0800409 write_TDBR(drv_data, (*(u8 *) (drv_data->tx)));
410 while (read_STAT(drv_data) & BIT_STAT_TXS)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700411 continue;
Bryan Wubb90eb02007-12-04 23:45:18 -0800412 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700413 continue;
Bryan Wubb90eb02007-12-04 23:45:18 -0800414 *(u8 *) (drv_data->rx) = read_RDBR(drv_data);
Bryan Wu62310e52007-12-04 23:45:20 -0800415
Bryan Wubb90eb02007-12-04 23:45:18 -0800416 cs_deactive(drv_data, chip);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800417
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700418 ++drv_data->rx;
419 ++drv_data->tx;
420 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700421}
422
423static void u16_writer(struct driver_data *drv_data)
424{
Bryan Wu131b17d2007-12-04 23:45:12 -0800425 dev_dbg(&drv_data->pdev->dev,
Bryan Wubb90eb02007-12-04 23:45:18 -0800426 "cr16 is 0x%x\n", read_STAT(drv_data));
Bryan Wu88b40362007-05-21 18:32:16 +0800427
Sonic Zhang3f479a62007-12-04 23:45:18 -0800428 /* poll for SPI completion before start */
Bryan Wubb90eb02007-12-04 23:45:18 -0800429 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Sonic Zhang3f479a62007-12-04 23:45:18 -0800430 continue;
431
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700432 while (drv_data->tx < drv_data->tx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800433 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
434 while ((read_STAT(drv_data) & BIT_STAT_TXS))
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700435 continue;
436 drv_data->tx += 2;
437 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700438}
439
440static void u16_cs_chg_writer(struct driver_data *drv_data)
441{
442 struct chip_data *chip = drv_data->cur_chip;
443
Sonic Zhang3f479a62007-12-04 23:45:18 -0800444 /* poll for SPI completion before start */
Bryan Wubb90eb02007-12-04 23:45:18 -0800445 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Sonic Zhang3f479a62007-12-04 23:45:18 -0800446 continue;
447
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700448 while (drv_data->tx < drv_data->tx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800449 cs_active(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700450
Bryan Wubb90eb02007-12-04 23:45:18 -0800451 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
452 while ((read_STAT(drv_data) & BIT_STAT_TXS))
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700453 continue;
Bryan Wu62310e52007-12-04 23:45:20 -0800454
Bryan Wubb90eb02007-12-04 23:45:18 -0800455 cs_deactive(drv_data, chip);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800456
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700457 drv_data->tx += 2;
458 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700459}
460
461static void u16_reader(struct driver_data *drv_data)
462{
Bryan Wu88b40362007-05-21 18:32:16 +0800463 dev_dbg(&drv_data->pdev->dev,
Bryan Wubb90eb02007-12-04 23:45:18 -0800464 "cr-16 is 0x%x\n", read_STAT(drv_data));
Sonic Zhangcc487e72007-12-04 23:45:17 -0800465
Sonic Zhang3f479a62007-12-04 23:45:18 -0800466 /* poll for SPI completion before start */
Bryan Wubb90eb02007-12-04 23:45:18 -0800467 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Sonic Zhang3f479a62007-12-04 23:45:18 -0800468 continue;
469
Sonic Zhangcc487e72007-12-04 23:45:17 -0800470 /* clear TDBR buffer before read(else it will be shifted out) */
Bryan Wubb90eb02007-12-04 23:45:18 -0800471 write_TDBR(drv_data, 0xFFFF);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800472
Bryan Wubb90eb02007-12-04 23:45:18 -0800473 dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700474
475 while (drv_data->rx < (drv_data->rx_end - 2)) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800476 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700477 continue;
Bryan Wubb90eb02007-12-04 23:45:18 -0800478 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700479 drv_data->rx += 2;
480 }
481
Bryan Wubb90eb02007-12-04 23:45:18 -0800482 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700483 continue;
Bryan Wubb90eb02007-12-04 23:45:18 -0800484 *(u16 *) (drv_data->rx) = read_SHAW(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700485 drv_data->rx += 2;
486}
487
488static void u16_cs_chg_reader(struct driver_data *drv_data)
489{
490 struct chip_data *chip = drv_data->cur_chip;
491
Sonic Zhang3f479a62007-12-04 23:45:18 -0800492 /* poll for SPI completion before start */
Bryan Wubb90eb02007-12-04 23:45:18 -0800493 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Sonic Zhang3f479a62007-12-04 23:45:18 -0800494 continue;
495
Sonic Zhangcc487e72007-12-04 23:45:17 -0800496 /* clear TDBR buffer before read(else it will be shifted out) */
Bryan Wubb90eb02007-12-04 23:45:18 -0800497 write_TDBR(drv_data, 0xFFFF);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700498
Bryan Wubb90eb02007-12-04 23:45:18 -0800499 cs_active(drv_data, chip);
500 dummy_read(drv_data);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800501
Bryan Wuc3061ab2007-12-04 23:45:19 -0800502 while (drv_data->rx < drv_data->rx_end - 2) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800503 cs_deactive(drv_data, chip);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800504
Bryan Wubb90eb02007-12-04 23:45:18 -0800505 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Sonic Zhangcc487e72007-12-04 23:45:17 -0800506 continue;
Bryan Wubb90eb02007-12-04 23:45:18 -0800507 cs_active(drv_data, chip);
508 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700509 drv_data->rx += 2;
510 }
Bryan Wubb90eb02007-12-04 23:45:18 -0800511 cs_deactive(drv_data, chip);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800512
Bryan Wubb90eb02007-12-04 23:45:18 -0800513 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Sonic Zhangcc487e72007-12-04 23:45:17 -0800514 continue;
Bryan Wubb90eb02007-12-04 23:45:18 -0800515 *(u16 *) (drv_data->rx) = read_SHAW(drv_data);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800516 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 */
Bryan Wubb90eb02007-12-04 23:45:18 -0800522 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Sonic Zhang3f479a62007-12-04 23:45:18 -0800523 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) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800527 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
528 while (read_STAT(drv_data) & BIT_STAT_TXS)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700529 continue;
Bryan Wubb90eb02007-12-04 23:45:18 -0800530 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700531 continue;
Bryan Wubb90eb02007-12-04 23:45:18 -0800532 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700533 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 */
Bryan Wubb90eb02007-12-04 23:45:18 -0800543 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Sonic Zhang3f479a62007-12-04 23:45:18 -0800544 continue;
545
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700546 while (drv_data->tx < drv_data->tx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800547 cs_active(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700548
Bryan Wubb90eb02007-12-04 23:45:18 -0800549 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
550 while (read_STAT(drv_data) & BIT_STAT_TXS)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700551 continue;
Bryan Wubb90eb02007-12-04 23:45:18 -0800552 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700553 continue;
Bryan Wubb90eb02007-12-04 23:45:18 -0800554 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
Bryan Wu62310e52007-12-04 23:45:20 -0800555
Bryan Wubb90eb02007-12-04 23:45:18 -0800556 cs_deactive(drv_data, chip);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800557
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700558 drv_data->rx += 2;
559 drv_data->tx += 2;
560 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700561}
562
563/* test if ther is more transfer to be done */
564static void *next_transfer(struct driver_data *drv_data)
565{
566 struct spi_message *msg = drv_data->cur_msg;
567 struct spi_transfer *trans = drv_data->cur_transfer;
568
569 /* Move to next transfer */
570 if (trans->transfer_list.next != &msg->transfers) {
571 drv_data->cur_transfer =
572 list_entry(trans->transfer_list.next,
573 struct spi_transfer, transfer_list);
574 return RUNNING_STATE;
575 } else
576 return DONE_STATE;
577}
578
579/*
580 * caller already set message->status;
581 * dma and pio irqs are blocked give finished message back
582 */
583static void giveback(struct driver_data *drv_data)
584{
Bryan Wufad91c82007-12-04 23:45:14 -0800585 struct chip_data *chip = drv_data->cur_chip;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700586 struct spi_transfer *last_transfer;
587 unsigned long flags;
588 struct spi_message *msg;
589
590 spin_lock_irqsave(&drv_data->lock, flags);
591 msg = drv_data->cur_msg;
592 drv_data->cur_msg = NULL;
593 drv_data->cur_transfer = NULL;
594 drv_data->cur_chip = NULL;
595 queue_work(drv_data->workqueue, &drv_data->pump_messages);
596 spin_unlock_irqrestore(&drv_data->lock, flags);
597
598 last_transfer = list_entry(msg->transfers.prev,
599 struct spi_transfer, transfer_list);
600
601 msg->state = NULL;
602
603 /* disable chip select signal. And not stop spi in autobuffer mode */
604 if (drv_data->tx_dma != 0xFFFF) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800605 cs_deactive(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700606 bfin_spi_disable(drv_data);
607 }
608
Bryan Wufad91c82007-12-04 23:45:14 -0800609 if (!drv_data->cs_change)
Bryan Wubb90eb02007-12-04 23:45:18 -0800610 cs_deactive(drv_data, chip);
Bryan Wufad91c82007-12-04 23:45:14 -0800611
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700612 if (msg->complete)
613 msg->complete(msg->context);
614}
615
Bryan Wu88b40362007-05-21 18:32:16 +0800616static irqreturn_t dma_irq_handler(int irq, void *dev_id)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700617{
618 struct driver_data *drv_data = (struct driver_data *)dev_id;
Bryan Wufad91c82007-12-04 23:45:14 -0800619 struct chip_data *chip = drv_data->cur_chip;
Bryan Wubb90eb02007-12-04 23:45:18 -0800620 struct spi_message *msg = drv_data->cur_msg;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700621
Bryan Wu88b40362007-05-21 18:32:16 +0800622 dev_dbg(&drv_data->pdev->dev, "in dma_irq_handler\n");
Bryan Wubb90eb02007-12-04 23:45:18 -0800623 clear_dma_irqstat(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700624
Bryan Wud6fe89b2007-06-11 17:34:17 +0800625 /* Wait for DMA to complete */
Bryan Wubb90eb02007-12-04 23:45:18 -0800626 while (get_dma_curr_irqstat(drv_data->dma_channel) & DMA_RUN)
Bryan Wud6fe89b2007-06-11 17:34:17 +0800627 continue;
628
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700629 /*
Bryan Wud6fe89b2007-06-11 17:34:17 +0800630 * wait for the last transaction shifted out. HRM states:
631 * at this point there may still be data in the SPI DMA FIFO waiting
632 * to be transmitted ... software needs to poll TXS in the SPI_STAT
633 * register until it goes low for 2 successive reads
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700634 */
635 if (drv_data->tx != NULL) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800636 while ((read_STAT(drv_data) & TXS) ||
637 (read_STAT(drv_data) & TXS))
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700638 continue;
639 }
640
Bryan Wubb90eb02007-12-04 23:45:18 -0800641 while (!(read_STAT(drv_data) & SPIF))
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700642 continue;
643
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700644 msg->actual_length += drv_data->len_in_bytes;
645
Bryan Wufad91c82007-12-04 23:45:14 -0800646 if (drv_data->cs_change)
Bryan Wubb90eb02007-12-04 23:45:18 -0800647 cs_deactive(drv_data, chip);
Bryan Wufad91c82007-12-04 23:45:14 -0800648
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700649 /* Move to next transfer */
650 msg->state = next_transfer(drv_data);
651
652 /* Schedule transfer tasklet */
653 tasklet_schedule(&drv_data->pump_transfers);
654
655 /* free the irq handler before next transfer */
Bryan Wu88b40362007-05-21 18:32:16 +0800656 dev_dbg(&drv_data->pdev->dev,
657 "disable dma channel irq%d\n",
Bryan Wubb90eb02007-12-04 23:45:18 -0800658 drv_data->dma_channel);
659 dma_disable_irq(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700660
661 return IRQ_HANDLED;
662}
663
664static void pump_transfers(unsigned long data)
665{
666 struct driver_data *drv_data = (struct driver_data *)data;
667 struct spi_message *message = NULL;
668 struct spi_transfer *transfer = NULL;
669 struct spi_transfer *previous = NULL;
670 struct chip_data *chip = NULL;
Bryan Wu88b40362007-05-21 18:32:16 +0800671 u8 width;
672 u16 cr, dma_width, dma_config;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700673 u32 tranf_success = 1;
674
675 /* Get current state information */
676 message = drv_data->cur_msg;
677 transfer = drv_data->cur_transfer;
678 chip = drv_data->cur_chip;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700679 /*
680 * if msg is error or done, report it back using complete() callback
681 */
682
683 /* Handle for abort */
684 if (message->state == ERROR_STATE) {
685 message->status = -EIO;
686 giveback(drv_data);
687 return;
688 }
689
690 /* Handle end of message */
691 if (message->state == DONE_STATE) {
692 message->status = 0;
693 giveback(drv_data);
694 return;
695 }
696
697 /* Delay if requested at end of transfer */
698 if (message->state == RUNNING_STATE) {
699 previous = list_entry(transfer->transfer_list.prev,
700 struct spi_transfer, transfer_list);
701 if (previous->delay_usecs)
702 udelay(previous->delay_usecs);
703 }
704
705 /* Setup the transfer state based on the type of transfer */
706 if (flush(drv_data) == 0) {
707 dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
708 message->status = -EIO;
709 giveback(drv_data);
710 return;
711 }
712
713 if (transfer->tx_buf != NULL) {
714 drv_data->tx = (void *)transfer->tx_buf;
715 drv_data->tx_end = drv_data->tx + transfer->len;
Bryan Wu88b40362007-05-21 18:32:16 +0800716 dev_dbg(&drv_data->pdev->dev, "tx_buf is %p, tx_end is %p\n",
717 transfer->tx_buf, drv_data->tx_end);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700718 } else {
719 drv_data->tx = NULL;
720 }
721
722 if (transfer->rx_buf != NULL) {
723 drv_data->rx = transfer->rx_buf;
724 drv_data->rx_end = drv_data->rx + transfer->len;
Bryan Wu88b40362007-05-21 18:32:16 +0800725 dev_dbg(&drv_data->pdev->dev, "rx_buf is %p, rx_end is %p\n",
726 transfer->rx_buf, drv_data->rx_end);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700727 } else {
728 drv_data->rx = NULL;
729 }
730
731 drv_data->rx_dma = transfer->rx_dma;
732 drv_data->tx_dma = transfer->tx_dma;
733 drv_data->len_in_bytes = transfer->len;
Bryan Wufad91c82007-12-04 23:45:14 -0800734 drv_data->cs_change = transfer->cs_change;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700735
736 width = chip->width;
737 if (width == CFG_SPI_WORDSIZE16) {
738 drv_data->len = (transfer->len) >> 1;
739 } else {
740 drv_data->len = transfer->len;
741 }
742 drv_data->write = drv_data->tx ? chip->write : null_writer;
743 drv_data->read = drv_data->rx ? chip->read : null_reader;
744 drv_data->duplex = chip->duplex ? chip->duplex : null_writer;
Bryan Wu131b17d2007-12-04 23:45:12 -0800745 dev_dbg(&drv_data->pdev->dev, "transfer: ",
746 "drv_data->write is %p, chip->write is %p, null_wr is %p\n",
747 drv_data->write, chip->write, null_writer);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700748
749 /* speed and width has been set on per message */
750 message->state = RUNNING_STATE;
751 dma_config = 0;
752
Bryan Wubb90eb02007-12-04 23:45:18 -0800753 write_STAT(drv_data, BIT_STAT_CLR);
754 cr = (read_CTRL(drv_data) & (~BIT_CTL_TIMOD));
755 cs_active(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700756
Bryan Wu88b40362007-05-21 18:32:16 +0800757 dev_dbg(&drv_data->pdev->dev,
758 "now pumping a transfer: width is %d, len is %d\n",
759 width, transfer->len);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700760
761 /*
762 * Try to map dma buffer and do a dma transfer if
763 * successful use different way to r/w according to
764 * drv_data->cur_chip->enable_dma
765 */
766 if (drv_data->cur_chip->enable_dma && drv_data->len > 6) {
767
Bryan Wubb90eb02007-12-04 23:45:18 -0800768 disable_dma(drv_data->dma_channel);
769 clear_dma_irqstat(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700770
771 /* config dma channel */
Bryan Wu88b40362007-05-21 18:32:16 +0800772 dev_dbg(&drv_data->pdev->dev, "doing dma transfer\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700773 if (width == CFG_SPI_WORDSIZE16) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800774 set_dma_x_count(drv_data->dma_channel, drv_data->len);
775 set_dma_x_modify(drv_data->dma_channel, 2);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700776 dma_width = WDSIZE_16;
777 } else {
Bryan Wubb90eb02007-12-04 23:45:18 -0800778 set_dma_x_count(drv_data->dma_channel, drv_data->len);
779 set_dma_x_modify(drv_data->dma_channel, 1);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700780 dma_width = WDSIZE_8;
781 }
782
Sonic Zhang3f479a62007-12-04 23:45:18 -0800783 /* poll for SPI completion before start */
Bryan Wubb90eb02007-12-04 23:45:18 -0800784 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Sonic Zhang3f479a62007-12-04 23:45:18 -0800785 continue;
786
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700787 /* dirty hack for autobuffer DMA mode */
788 if (drv_data->tx_dma == 0xFFFF) {
Bryan Wu88b40362007-05-21 18:32:16 +0800789 dev_dbg(&drv_data->pdev->dev,
790 "doing autobuffer DMA out.\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700791
Sonic Zhangcc487e72007-12-04 23:45:17 -0800792 /* set SPI transfer mode */
Bryan Wubb90eb02007-12-04 23:45:18 -0800793 write_CTRL(drv_data, (cr | CFG_SPI_DMAWRITE));
Sonic Zhangcc487e72007-12-04 23:45:17 -0800794
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700795 /* no irq in autobuffer mode */
796 dma_config =
797 (DMAFLOW_AUTO | RESTART | dma_width | DI_EN);
Bryan Wubb90eb02007-12-04 23:45:18 -0800798 set_dma_config(drv_data->dma_channel, dma_config);
799 set_dma_start_addr(drv_data->dma_channel,
Bryan Wua32c6912007-12-04 23:45:15 -0800800 (unsigned long)drv_data->tx);
Bryan Wubb90eb02007-12-04 23:45:18 -0800801 enable_dma(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700802
803 /* just return here, there can only be one transfer in this mode */
804 message->status = 0;
805 giveback(drv_data);
806 return;
807 }
808
809 /* In dma mode, rx or tx must be NULL in one transfer */
810 if (drv_data->rx != NULL) {
811 /* set transfer mode, and enable SPI */
Bryan Wu88b40362007-05-21 18:32:16 +0800812 dev_dbg(&drv_data->pdev->dev, "doing DMA in.\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700813
Sonic Zhangcc487e72007-12-04 23:45:17 -0800814 /* set SPI transfer mode */
Bryan Wubb90eb02007-12-04 23:45:18 -0800815 write_CTRL(drv_data, (cr | CFG_SPI_DMAREAD));
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700816
817 /* clear tx reg soformer data is not shifted out */
Bryan Wubb90eb02007-12-04 23:45:18 -0800818 write_TDBR(drv_data, 0xFFFF);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700819
Bryan Wubb90eb02007-12-04 23:45:18 -0800820 set_dma_x_count(drv_data->dma_channel, drv_data->len);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700821
822 /* start dma */
Bryan Wubb90eb02007-12-04 23:45:18 -0800823 dma_enable_irq(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700824 dma_config = (WNR | RESTART | dma_width | DI_EN);
Bryan Wubb90eb02007-12-04 23:45:18 -0800825 set_dma_config(drv_data->dma_channel, dma_config);
826 set_dma_start_addr(drv_data->dma_channel,
Bryan Wua32c6912007-12-04 23:45:15 -0800827 (unsigned long)drv_data->rx);
Bryan Wubb90eb02007-12-04 23:45:18 -0800828 enable_dma(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700829
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700830 } else if (drv_data->tx != NULL) {
Bryan Wu88b40362007-05-21 18:32:16 +0800831 dev_dbg(&drv_data->pdev->dev, "doing DMA out.\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700832
Sonic Zhangcc487e72007-12-04 23:45:17 -0800833 /* set SPI transfer mode */
Bryan Wubb90eb02007-12-04 23:45:18 -0800834 write_CTRL(drv_data, (cr | CFG_SPI_DMAWRITE));
Sonic Zhangcc487e72007-12-04 23:45:17 -0800835
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700836 /* start dma */
Bryan Wubb90eb02007-12-04 23:45:18 -0800837 dma_enable_irq(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700838 dma_config = (RESTART | dma_width | DI_EN);
Bryan Wubb90eb02007-12-04 23:45:18 -0800839 set_dma_config(drv_data->dma_channel, dma_config);
840 set_dma_start_addr(drv_data->dma_channel,
Bryan Wua32c6912007-12-04 23:45:15 -0800841 (unsigned long)drv_data->tx);
Bryan Wubb90eb02007-12-04 23:45:18 -0800842 enable_dma(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700843 }
844 } else {
845 /* IO mode write then read */
Bryan Wu88b40362007-05-21 18:32:16 +0800846 dev_dbg(&drv_data->pdev->dev, "doing IO transfer\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700847
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700848 if (drv_data->tx != NULL && drv_data->rx != NULL) {
849 /* full duplex mode */
850 BUG_ON((drv_data->tx_end - drv_data->tx) !=
851 (drv_data->rx_end - drv_data->rx));
Bryan Wu88b40362007-05-21 18:32:16 +0800852 dev_dbg(&drv_data->pdev->dev,
853 "IO duplex: cr is 0x%x\n", cr);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700854
Sonic Zhangcc487e72007-12-04 23:45:17 -0800855 /* set SPI transfer mode */
Bryan Wubb90eb02007-12-04 23:45:18 -0800856 write_CTRL(drv_data, (cr | CFG_SPI_WRITE));
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700857
858 drv_data->duplex(drv_data);
859
860 if (drv_data->tx != drv_data->tx_end)
861 tranf_success = 0;
862 } else if (drv_data->tx != NULL) {
863 /* write only half duplex */
Bryan Wu131b17d2007-12-04 23:45:12 -0800864 dev_dbg(&drv_data->pdev->dev,
Bryan Wu88b40362007-05-21 18:32:16 +0800865 "IO write: cr is 0x%x\n", cr);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700866
Sonic Zhangcc487e72007-12-04 23:45:17 -0800867 /* set SPI transfer mode */
Bryan Wubb90eb02007-12-04 23:45:18 -0800868 write_CTRL(drv_data, (cr | CFG_SPI_WRITE));
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700869
870 drv_data->write(drv_data);
871
872 if (drv_data->tx != drv_data->tx_end)
873 tranf_success = 0;
874 } else if (drv_data->rx != NULL) {
875 /* read only half duplex */
Bryan Wu131b17d2007-12-04 23:45:12 -0800876 dev_dbg(&drv_data->pdev->dev,
Bryan Wu88b40362007-05-21 18:32:16 +0800877 "IO read: cr is 0x%x\n", cr);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700878
Sonic Zhangcc487e72007-12-04 23:45:17 -0800879 /* set SPI transfer mode */
Bryan Wubb90eb02007-12-04 23:45:18 -0800880 write_CTRL(drv_data, (cr | CFG_SPI_READ));
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700881
882 drv_data->read(drv_data);
883 if (drv_data->rx != drv_data->rx_end)
884 tranf_success = 0;
885 }
886
887 if (!tranf_success) {
Bryan Wu131b17d2007-12-04 23:45:12 -0800888 dev_dbg(&drv_data->pdev->dev,
Bryan Wu88b40362007-05-21 18:32:16 +0800889 "IO write error!\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700890 message->state = ERROR_STATE;
891 } else {
892 /* Update total byte transfered */
893 message->actual_length += drv_data->len;
894
895 /* Move to next transfer of this msg */
896 message->state = next_transfer(drv_data);
897 }
898
899 /* Schedule next transfer tasklet */
900 tasklet_schedule(&drv_data->pump_transfers);
901
902 }
903}
904
905/* pop a msg from queue and kick off real transfer */
906static void pump_messages(struct work_struct *work)
907{
Bryan Wu131b17d2007-12-04 23:45:12 -0800908 struct driver_data *drv_data;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700909 unsigned long flags;
910
Bryan Wu131b17d2007-12-04 23:45:12 -0800911 drv_data = container_of(work, struct driver_data, pump_messages);
912
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700913 /* Lock queue and check for queue work */
914 spin_lock_irqsave(&drv_data->lock, flags);
915 if (list_empty(&drv_data->queue) || drv_data->run == QUEUE_STOPPED) {
916 /* pumper kicked off but no work to do */
917 drv_data->busy = 0;
918 spin_unlock_irqrestore(&drv_data->lock, flags);
919 return;
920 }
921
922 /* Make sure we are not already running a message */
923 if (drv_data->cur_msg) {
924 spin_unlock_irqrestore(&drv_data->lock, flags);
925 return;
926 }
927
928 /* Extract head of queue */
929 drv_data->cur_msg = list_entry(drv_data->queue.next,
930 struct spi_message, queue);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800931
932 /* Setup the SSP using the per chip configuration */
933 drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
934 if (restore_state(drv_data)) {
935 spin_unlock_irqrestore(&drv_data->lock, flags);
936 return;
937 };
938
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700939 list_del_init(&drv_data->cur_msg->queue);
940
941 /* Initial message state */
942 drv_data->cur_msg->state = START_STATE;
943 drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
944 struct spi_transfer, transfer_list);
945
Bryan Wu5fec5b52007-12-04 23:45:13 -0800946 dev_dbg(&drv_data->pdev->dev, "got a message to pump, "
947 "state is set to: baud %d, flag 0x%x, ctl 0x%x\n",
948 drv_data->cur_chip->baud, drv_data->cur_chip->flag,
949 drv_data->cur_chip->ctl_reg);
Bryan Wu131b17d2007-12-04 23:45:12 -0800950
951 dev_dbg(&drv_data->pdev->dev,
Bryan Wu88b40362007-05-21 18:32:16 +0800952 "the first transfer len is %d\n",
953 drv_data->cur_transfer->len);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700954
955 /* Mark as busy and launch transfers */
956 tasklet_schedule(&drv_data->pump_transfers);
957
958 drv_data->busy = 1;
959 spin_unlock_irqrestore(&drv_data->lock, flags);
960}
961
962/*
963 * got a msg to transfer, queue it in drv_data->queue.
964 * And kick off message pumper
965 */
966static int transfer(struct spi_device *spi, struct spi_message *msg)
967{
968 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
969 unsigned long flags;
970
971 spin_lock_irqsave(&drv_data->lock, flags);
972
973 if (drv_data->run == QUEUE_STOPPED) {
974 spin_unlock_irqrestore(&drv_data->lock, flags);
975 return -ESHUTDOWN;
976 }
977
978 msg->actual_length = 0;
979 msg->status = -EINPROGRESS;
980 msg->state = START_STATE;
981
Bryan Wu88b40362007-05-21 18:32:16 +0800982 dev_dbg(&spi->dev, "adding an msg in transfer() \n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700983 list_add_tail(&msg->queue, &drv_data->queue);
984
985 if (drv_data->run == QUEUE_RUNNING && !drv_data->busy)
986 queue_work(drv_data->workqueue, &drv_data->pump_messages);
987
988 spin_unlock_irqrestore(&drv_data->lock, flags);
989
990 return 0;
991}
992
Sonic Zhang12e17c42007-12-04 23:45:16 -0800993#define MAX_SPI_SSEL 7
994
995static u16 ssel[3][MAX_SPI_SSEL] = {
996 {P_SPI0_SSEL1, P_SPI0_SSEL2, P_SPI0_SSEL3,
997 P_SPI0_SSEL4, P_SPI0_SSEL5,
998 P_SPI0_SSEL6, P_SPI0_SSEL7},
999
1000 {P_SPI1_SSEL1, P_SPI1_SSEL2, P_SPI1_SSEL3,
1001 P_SPI1_SSEL4, P_SPI1_SSEL5,
1002 P_SPI1_SSEL6, P_SPI1_SSEL7},
1003
1004 {P_SPI2_SSEL1, P_SPI2_SSEL2, P_SPI2_SSEL3,
1005 P_SPI2_SSEL4, P_SPI2_SSEL5,
1006 P_SPI2_SSEL6, P_SPI2_SSEL7},
1007};
1008
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001009/* first setup for new devices */
1010static int setup(struct spi_device *spi)
1011{
1012 struct bfin5xx_spi_chip *chip_info = NULL;
1013 struct chip_data *chip;
1014 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
1015 u8 spi_flg;
1016
1017 /* Abort device setup if requested features are not supported */
1018 if (spi->mode & ~(SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST)) {
1019 dev_err(&spi->dev, "requested mode not fully supported\n");
1020 return -EINVAL;
1021 }
1022
1023 /* Zero (the default) here means 8 bits */
1024 if (!spi->bits_per_word)
1025 spi->bits_per_word = 8;
1026
1027 if (spi->bits_per_word != 8 && spi->bits_per_word != 16)
1028 return -EINVAL;
1029
1030 /* Only alloc (or use chip_info) on first setup */
1031 chip = spi_get_ctldata(spi);
1032 if (chip == NULL) {
1033 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
1034 if (!chip)
1035 return -ENOMEM;
1036
1037 chip->enable_dma = 0;
1038 chip_info = spi->controller_data;
1039 }
1040
1041 /* chip_info isn't always needed */
1042 if (chip_info) {
Mike Frysinger2ed35512007-12-04 23:45:14 -08001043 /* Make sure people stop trying to set fields via ctl_reg
1044 * when they should actually be using common SPI framework.
1045 * Currently we let through: WOM EMISO PSSE GM SZ TIMOD.
1046 * Not sure if a user actually needs/uses any of these,
1047 * but let's assume (for now) they do.
1048 */
1049 if (chip_info->ctl_reg & (SPE|MSTR|CPOL|CPHA|LSBF|SIZE)) {
1050 dev_err(&spi->dev, "do not set bits in ctl_reg "
1051 "that the SPI framework manages\n");
1052 return -EINVAL;
1053 }
1054
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001055 chip->enable_dma = chip_info->enable_dma != 0
1056 && drv_data->master_info->enable_dma;
1057 chip->ctl_reg = chip_info->ctl_reg;
1058 chip->bits_per_word = chip_info->bits_per_word;
1059 chip->cs_change_per_word = chip_info->cs_change_per_word;
1060 chip->cs_chg_udelay = chip_info->cs_chg_udelay;
1061 }
1062
1063 /* translate common spi framework into our register */
1064 if (spi->mode & SPI_CPOL)
1065 chip->ctl_reg |= CPOL;
1066 if (spi->mode & SPI_CPHA)
1067 chip->ctl_reg |= CPHA;
1068 if (spi->mode & SPI_LSB_FIRST)
1069 chip->ctl_reg |= LSBF;
1070 /* we dont support running in slave mode (yet?) */
1071 chip->ctl_reg |= MSTR;
1072
1073 /*
1074 * if any one SPI chip is registered and wants DMA, request the
1075 * DMA channel for it
1076 */
Bryan Wubb90eb02007-12-04 23:45:18 -08001077 if (chip->enable_dma && !drv_data->dma_requested) {
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001078 /* register dma irq handler */
Bryan Wubb90eb02007-12-04 23:45:18 -08001079 if (request_dma(drv_data->dma_channel, "BF53x_SPI_DMA") < 0) {
Bryan Wu88b40362007-05-21 18:32:16 +08001080 dev_dbg(&spi->dev,
1081 "Unable to request BlackFin SPI DMA channel\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001082 return -ENODEV;
1083 }
Bryan Wubb90eb02007-12-04 23:45:18 -08001084 if (set_dma_callback(drv_data->dma_channel,
1085 (void *)dma_irq_handler, drv_data) < 0) {
Bryan Wu88b40362007-05-21 18:32:16 +08001086 dev_dbg(&spi->dev, "Unable to set dma callback\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001087 return -EPERM;
1088 }
Bryan Wubb90eb02007-12-04 23:45:18 -08001089 dma_disable_irq(drv_data->dma_channel);
1090 drv_data->dma_requested = 1;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001091 }
1092
1093 /*
1094 * Notice: for blackfin, the speed_hz is the value of register
1095 * SPI_BAUD, not the real baudrate
1096 */
1097 chip->baud = hz_to_spi_baud(spi->max_speed_hz);
1098 spi_flg = ~(1 << (spi->chip_select));
1099 chip->flag = ((u16) spi_flg << 8) | (1 << (spi->chip_select));
1100 chip->chip_select_num = spi->chip_select;
1101
1102 switch (chip->bits_per_word) {
1103 case 8:
1104 chip->n_bytes = 1;
1105 chip->width = CFG_SPI_WORDSIZE8;
1106 chip->read = chip->cs_change_per_word ?
1107 u8_cs_chg_reader : u8_reader;
1108 chip->write = chip->cs_change_per_word ?
1109 u8_cs_chg_writer : u8_writer;
1110 chip->duplex = chip->cs_change_per_word ?
1111 u8_cs_chg_duplex : u8_duplex;
1112 break;
1113
1114 case 16:
1115 chip->n_bytes = 2;
1116 chip->width = CFG_SPI_WORDSIZE16;
1117 chip->read = chip->cs_change_per_word ?
1118 u16_cs_chg_reader : u16_reader;
1119 chip->write = chip->cs_change_per_word ?
1120 u16_cs_chg_writer : u16_writer;
1121 chip->duplex = chip->cs_change_per_word ?
1122 u16_cs_chg_duplex : u16_duplex;
1123 break;
1124
1125 default:
1126 dev_err(&spi->dev, "%d bits_per_word is not supported\n",
1127 chip->bits_per_word);
1128 kfree(chip);
1129 return -ENODEV;
1130 }
1131
Joe Perches898eb712007-10-18 03:06:30 -07001132 dev_dbg(&spi->dev, "setup spi chip %s, width is %d, dma is %d\n",
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001133 spi->modalias, chip->width, chip->enable_dma);
Bryan Wu88b40362007-05-21 18:32:16 +08001134 dev_dbg(&spi->dev, "ctl_reg is 0x%x, flag_reg is 0x%x\n",
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001135 chip->ctl_reg, chip->flag);
1136
1137 spi_set_ctldata(spi, chip);
1138
Sonic Zhang12e17c42007-12-04 23:45:16 -08001139 dev_dbg(&spi->dev, "chip select number is %d\n", chip->chip_select_num);
1140 if ((chip->chip_select_num > 0)
1141 && (chip->chip_select_num <= spi->master->num_chipselect))
1142 peripheral_request(ssel[spi->master->bus_num]
1143 [chip->chip_select_num-1], DRV_NAME);
1144
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001145 return 0;
1146}
1147
1148/*
1149 * callback for spi framework.
1150 * clean driver specific data
1151 */
Bryan Wu88b40362007-05-21 18:32:16 +08001152static void cleanup(struct spi_device *spi)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001153{
Mike Frysinger27bb9e72007-06-11 15:31:30 +08001154 struct chip_data *chip = spi_get_ctldata(spi);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001155
Sonic Zhang12e17c42007-12-04 23:45:16 -08001156 if ((chip->chip_select_num > 0)
1157 && (chip->chip_select_num <= spi->master->num_chipselect))
1158 peripheral_free(ssel[spi->master->bus_num]
1159 [chip->chip_select_num-1]);
1160
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001161 kfree(chip);
1162}
1163
1164static inline int init_queue(struct driver_data *drv_data)
1165{
1166 INIT_LIST_HEAD(&drv_data->queue);
1167 spin_lock_init(&drv_data->lock);
1168
1169 drv_data->run = QUEUE_STOPPED;
1170 drv_data->busy = 0;
1171
1172 /* init transfer tasklet */
1173 tasklet_init(&drv_data->pump_transfers,
1174 pump_transfers, (unsigned long)drv_data);
1175
1176 /* init messages workqueue */
1177 INIT_WORK(&drv_data->pump_messages, pump_messages);
1178 drv_data->workqueue =
Tony Jones49dce682007-10-16 01:27:48 -07001179 create_singlethread_workqueue(drv_data->master->dev.parent->bus_id);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001180 if (drv_data->workqueue == NULL)
1181 return -EBUSY;
1182
1183 return 0;
1184}
1185
1186static inline int start_queue(struct driver_data *drv_data)
1187{
1188 unsigned long flags;
1189
1190 spin_lock_irqsave(&drv_data->lock, flags);
1191
1192 if (drv_data->run == QUEUE_RUNNING || drv_data->busy) {
1193 spin_unlock_irqrestore(&drv_data->lock, flags);
1194 return -EBUSY;
1195 }
1196
1197 drv_data->run = QUEUE_RUNNING;
1198 drv_data->cur_msg = NULL;
1199 drv_data->cur_transfer = NULL;
1200 drv_data->cur_chip = NULL;
1201 spin_unlock_irqrestore(&drv_data->lock, flags);
1202
1203 queue_work(drv_data->workqueue, &drv_data->pump_messages);
1204
1205 return 0;
1206}
1207
1208static inline int stop_queue(struct driver_data *drv_data)
1209{
1210 unsigned long flags;
1211 unsigned limit = 500;
1212 int status = 0;
1213
1214 spin_lock_irqsave(&drv_data->lock, flags);
1215
1216 /*
1217 * This is a bit lame, but is optimized for the common execution path.
1218 * A wait_queue on the drv_data->busy could be used, but then the common
1219 * execution path (pump_messages) would be required to call wake_up or
1220 * friends on every SPI message. Do this instead
1221 */
1222 drv_data->run = QUEUE_STOPPED;
1223 while (!list_empty(&drv_data->queue) && drv_data->busy && limit--) {
1224 spin_unlock_irqrestore(&drv_data->lock, flags);
1225 msleep(10);
1226 spin_lock_irqsave(&drv_data->lock, flags);
1227 }
1228
1229 if (!list_empty(&drv_data->queue) || drv_data->busy)
1230 status = -EBUSY;
1231
1232 spin_unlock_irqrestore(&drv_data->lock, flags);
1233
1234 return status;
1235}
1236
1237static inline int destroy_queue(struct driver_data *drv_data)
1238{
1239 int status;
1240
1241 status = stop_queue(drv_data);
1242 if (status != 0)
1243 return status;
1244
1245 destroy_workqueue(drv_data->workqueue);
1246
1247 return 0;
1248}
1249
Sonic Zhang7c4ef092007-12-04 23:45:16 -08001250static int setup_pin_mux(int action, int bus_num)
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001251{
1252
Sonic Zhang7c4ef092007-12-04 23:45:16 -08001253 u16 pin_req[3][4] = {
1254 {P_SPI0_SCK, P_SPI0_MISO, P_SPI0_MOSI, 0},
1255 {P_SPI1_SCK, P_SPI1_MISO, P_SPI1_MOSI, 0},
1256 {P_SPI2_SCK, P_SPI2_MISO, P_SPI2_MOSI, 0},
1257 };
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001258
1259 if (action) {
Sonic Zhang7c4ef092007-12-04 23:45:16 -08001260 if (peripheral_request_list(pin_req[bus_num], DRV_NAME))
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001261 return -EFAULT;
1262 } else {
Sonic Zhang7c4ef092007-12-04 23:45:16 -08001263 peripheral_free_list(pin_req[bus_num]);
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001264 }
1265
1266 return 0;
1267}
1268
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001269static int __init bfin5xx_spi_probe(struct platform_device *pdev)
1270{
1271 struct device *dev = &pdev->dev;
1272 struct bfin5xx_spi_master *platform_info;
1273 struct spi_master *master;
1274 struct driver_data *drv_data = 0;
Bryan Wua32c6912007-12-04 23:45:15 -08001275 struct resource *res;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001276 int status = 0;
1277
1278 platform_info = dev->platform_data;
1279
1280 /* Allocate master with space for drv_data */
1281 master = spi_alloc_master(dev, sizeof(struct driver_data) + 16);
1282 if (!master) {
1283 dev_err(&pdev->dev, "can not alloc spi_master\n");
1284 return -ENOMEM;
1285 }
Bryan Wu131b17d2007-12-04 23:45:12 -08001286
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001287 drv_data = spi_master_get_devdata(master);
1288 drv_data->master = master;
1289 drv_data->master_info = platform_info;
1290 drv_data->pdev = pdev;
1291
1292 master->bus_num = pdev->id;
1293 master->num_chipselect = platform_info->num_chipselect;
1294 master->cleanup = cleanup;
1295 master->setup = setup;
1296 master->transfer = transfer;
1297
Bryan Wua32c6912007-12-04 23:45:15 -08001298 /* Find and map our resources */
1299 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1300 if (res == NULL) {
1301 dev_err(dev, "Cannot get IORESOURCE_MEM\n");
1302 status = -ENOENT;
1303 goto out_error_get_res;
1304 }
1305
Bryan Wubb90eb02007-12-04 23:45:18 -08001306 drv_data->regs_base = (u32) ioremap(res->start,
1307 (res->end - res->start + 1));
1308 if (!drv_data->regs_base) {
Bryan Wua32c6912007-12-04 23:45:15 -08001309 dev_err(dev, "Cannot map IO\n");
1310 status = -ENXIO;
1311 goto out_error_ioremap;
1312 }
1313
Bryan Wubb90eb02007-12-04 23:45:18 -08001314 drv_data->dma_channel = platform_get_irq(pdev, 0);
1315 if (drv_data->dma_channel < 0) {
Bryan Wua32c6912007-12-04 23:45:15 -08001316 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 Wubb90eb02007-12-04 23:45:18 -08001347 dev_info(dev, "%s, Version %s, regs_base@0x%08x, dma channel@%d\n",
1348 DRV_DESC, DRV_VERSION, drv_data->regs_base,
1349 drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001350 return status;
1351
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001352out_error_queue_alloc:
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001353 destroy_queue(drv_data);
Bryan Wua32c6912007-12-04 23:45:15 -08001354out_error_no_dma_ch:
Bryan Wubb90eb02007-12-04 23:45:18 -08001355 iounmap((void *) drv_data->regs_base);
Bryan Wua32c6912007-12-04 23:45:15 -08001356out_error_ioremap:
1357out_error_get_res:
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001358out_error:
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001359 spi_master_put(master);
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001360
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001361 return status;
1362}
1363
1364/* stop hardware and remove the driver */
1365static int __devexit bfin5xx_spi_remove(struct platform_device *pdev)
1366{
1367 struct driver_data *drv_data = platform_get_drvdata(pdev);
1368 int status = 0;
1369
1370 if (!drv_data)
1371 return 0;
1372
1373 /* Remove the queue */
1374 status = destroy_queue(drv_data);
1375 if (status != 0)
1376 return status;
1377
1378 /* Disable the SSP at the peripheral and SOC level */
1379 bfin_spi_disable(drv_data);
1380
1381 /* Release DMA */
1382 if (drv_data->master_info->enable_dma) {
Bryan Wubb90eb02007-12-04 23:45:18 -08001383 if (dma_channel_active(drv_data->dma_channel))
1384 free_dma(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001385 }
1386
1387 /* Disconnect from the SPI framework */
1388 spi_unregister_master(drv_data->master);
1389
Sonic Zhang7c4ef092007-12-04 23:45:16 -08001390 setup_pin_mux(0, drv_data->master->bus_num);
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001391
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001392 /* Prevent double remove */
1393 platform_set_drvdata(pdev, NULL);
1394
1395 return 0;
1396}
1397
1398#ifdef CONFIG_PM
1399static int bfin5xx_spi_suspend(struct platform_device *pdev, pm_message_t state)
1400{
1401 struct driver_data *drv_data = platform_get_drvdata(pdev);
1402 int status = 0;
1403
1404 status = stop_queue(drv_data);
1405 if (status != 0)
1406 return status;
1407
1408 /* stop hardware */
1409 bfin_spi_disable(drv_data);
1410
1411 return 0;
1412}
1413
1414static int bfin5xx_spi_resume(struct platform_device *pdev)
1415{
1416 struct driver_data *drv_data = platform_get_drvdata(pdev);
1417 int status = 0;
1418
1419 /* Enable the SPI interface */
1420 bfin_spi_enable(drv_data);
1421
1422 /* Start the queue running */
1423 status = start_queue(drv_data);
1424 if (status != 0) {
1425 dev_err(&pdev->dev, "problem starting queue (%d)\n", status);
1426 return status;
1427 }
1428
1429 return 0;
1430}
1431#else
1432#define bfin5xx_spi_suspend NULL
1433#define bfin5xx_spi_resume NULL
1434#endif /* CONFIG_PM */
1435
David Brownellfc3ba952007-08-30 23:56:24 -07001436MODULE_ALIAS("bfin-spi-master"); /* for platform bus hotplug */
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001437static struct platform_driver bfin5xx_spi_driver = {
David Brownellfc3ba952007-08-30 23:56:24 -07001438 .driver = {
Bryan Wua32c6912007-12-04 23:45:15 -08001439 .name = DRV_NAME,
Bryan Wu88b40362007-05-21 18:32:16 +08001440 .owner = THIS_MODULE,
1441 },
1442 .suspend = bfin5xx_spi_suspend,
1443 .resume = bfin5xx_spi_resume,
1444 .remove = __devexit_p(bfin5xx_spi_remove),
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001445};
1446
1447static int __init bfin5xx_spi_init(void)
1448{
Bryan Wu88b40362007-05-21 18:32:16 +08001449 return platform_driver_probe(&bfin5xx_spi_driver, bfin5xx_spi_probe);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001450}
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001451module_init(bfin5xx_spi_init);
1452
1453static void __exit bfin5xx_spi_exit(void)
1454{
1455 platform_driver_unregister(&bfin5xx_spi_driver);
1456}
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001457module_exit(bfin5xx_spi_exit);