blob: c4c4905e0aa1c64070a4d7a42f3fe6f4fa058c55 [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;
135 u8 cs_chg_udelay;
136 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 Wufad91c82007-12-04 23:45:14 -0800214}
215
Sonic Zhang7c4ef092007-12-04 23:45:16 -0800216#define MAX_SPI_SSEL 7
Bryan Wu5fec5b52007-12-04 23:45:13 -0800217
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700218/* stop controller and re-config current chip*/
Bryan Wu5fec5b52007-12-04 23:45:13 -0800219static int restore_state(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700220{
221 struct chip_data *chip = drv_data->cur_chip;
Bryan Wu5fec5b52007-12-04 23:45:13 -0800222 int ret = 0;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700223
224 /* Clear status and disable clock */
Bryan Wubb90eb02007-12-04 23:45:18 -0800225 write_STAT(drv_data, BIT_STAT_CLR);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700226 bfin_spi_disable(drv_data);
Bryan Wu88b40362007-05-21 18:32:16 +0800227 dev_dbg(&drv_data->pdev->dev, "restoring spi ctl state\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700228
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700229 /* Load the registers */
Bryan Wubb90eb02007-12-04 23:45:18 -0800230 cs_deactive(drv_data, chip);
231 write_BAUD(drv_data, chip->baud);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800232 chip->ctl_reg &= (~BIT_CTL_TIMOD);
233 chip->ctl_reg |= (chip->width << 8);
Bryan Wubb90eb02007-12-04 23:45:18 -0800234 write_CTRL(drv_data, chip->ctl_reg);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800235
236 bfin_spi_enable(drv_data);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800237
Bryan Wu5fec5b52007-12-04 23:45:13 -0800238 if (ret)
239 dev_dbg(&drv_data->pdev->dev,
240 ": request chip select number %d failed\n",
241 chip->chip_select_num);
242
243 return ret;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700244}
245
246/* used to kick off transfer in rx mode */
Bryan Wubb90eb02007-12-04 23:45:18 -0800247static unsigned short dummy_read(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700248{
249 unsigned short tmp;
Bryan Wubb90eb02007-12-04 23:45:18 -0800250 tmp = read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700251 return tmp;
252}
253
254static void null_writer(struct driver_data *drv_data)
255{
256 u8 n_bytes = drv_data->n_bytes;
257
258 while (drv_data->tx < drv_data->tx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800259 write_TDBR(drv_data, 0);
260 while ((read_STAT(drv_data) & BIT_STAT_TXS))
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700261 continue;
262 drv_data->tx += n_bytes;
263 }
264}
265
266static void null_reader(struct driver_data *drv_data)
267{
268 u8 n_bytes = drv_data->n_bytes;
Bryan Wubb90eb02007-12-04 23:45:18 -0800269 dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700270
271 while (drv_data->rx < drv_data->rx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800272 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700273 continue;
Bryan Wubb90eb02007-12-04 23:45:18 -0800274 dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700275 drv_data->rx += n_bytes;
276 }
277}
278
279static void u8_writer(struct driver_data *drv_data)
280{
Bryan Wu131b17d2007-12-04 23:45:12 -0800281 dev_dbg(&drv_data->pdev->dev,
Bryan Wubb90eb02007-12-04 23:45:18 -0800282 "cr8-s is 0x%x\n", read_STAT(drv_data));
Sonic Zhangcc487e72007-12-04 23:45:17 -0800283
Sonic Zhang3f479a62007-12-04 23:45:18 -0800284 /* poll for SPI completion before start */
Bryan Wubb90eb02007-12-04 23:45:18 -0800285 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Sonic Zhang3f479a62007-12-04 23:45:18 -0800286 continue;
287
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700288 while (drv_data->tx < drv_data->tx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800289 write_TDBR(drv_data, (*(u8 *) (drv_data->tx)));
290 while (read_STAT(drv_data) & BIT_STAT_TXS)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700291 continue;
292 ++drv_data->tx;
293 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700294}
295
296static void u8_cs_chg_writer(struct driver_data *drv_data)
297{
298 struct chip_data *chip = drv_data->cur_chip;
299
Sonic Zhang3f479a62007-12-04 23:45:18 -0800300 /* poll for SPI completion before start */
Bryan Wubb90eb02007-12-04 23:45:18 -0800301 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Sonic Zhang3f479a62007-12-04 23:45:18 -0800302 continue;
303
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700304 while (drv_data->tx < drv_data->tx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800305 cs_active(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700306
Bryan Wubb90eb02007-12-04 23:45:18 -0800307 write_TDBR(drv_data, (*(u8 *) (drv_data->tx)));
308 while (read_STAT(drv_data) & BIT_STAT_TXS)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700309 continue;
Bryan Wubb90eb02007-12-04 23:45:18 -0800310 cs_deactive(drv_data, chip);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800311
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700312 if (chip->cs_chg_udelay)
313 udelay(chip->cs_chg_udelay);
314 ++drv_data->tx;
315 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700316}
317
318static void u8_reader(struct driver_data *drv_data)
319{
Bryan Wu131b17d2007-12-04 23:45:12 -0800320 dev_dbg(&drv_data->pdev->dev,
Bryan Wubb90eb02007-12-04 23:45:18 -0800321 "cr-8 is 0x%x\n", read_STAT(drv_data));
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700322
Sonic Zhang3f479a62007-12-04 23:45:18 -0800323 /* poll for SPI completion before start */
Bryan Wubb90eb02007-12-04 23:45:18 -0800324 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Sonic Zhang3f479a62007-12-04 23:45:18 -0800325 continue;
326
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700327 /* clear TDBR buffer before read(else it will be shifted out) */
Bryan Wubb90eb02007-12-04 23:45:18 -0800328 write_TDBR(drv_data, 0xFFFF);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700329
Bryan Wubb90eb02007-12-04 23:45:18 -0800330 dummy_read(drv_data);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800331
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700332 while (drv_data->rx < drv_data->rx_end - 1) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800333 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700334 continue;
Bryan Wubb90eb02007-12-04 23:45:18 -0800335 *(u8 *) (drv_data->rx) = read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700336 ++drv_data->rx;
337 }
338
Bryan Wubb90eb02007-12-04 23:45:18 -0800339 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700340 continue;
Bryan Wubb90eb02007-12-04 23:45:18 -0800341 *(u8 *) (drv_data->rx) = read_SHAW(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700342 ++drv_data->rx;
343}
344
345static void u8_cs_chg_reader(struct driver_data *drv_data)
346{
347 struct chip_data *chip = drv_data->cur_chip;
348
Sonic Zhang3f479a62007-12-04 23:45:18 -0800349 /* poll for SPI completion before start */
Bryan Wubb90eb02007-12-04 23:45:18 -0800350 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Sonic Zhang3f479a62007-12-04 23:45:18 -0800351 continue;
352
Sonic Zhangcc487e72007-12-04 23:45:17 -0800353 /* clear TDBR buffer before read(else it will be shifted out) */
Bryan Wubb90eb02007-12-04 23:45:18 -0800354 write_TDBR(drv_data, 0xFFFF);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700355
Bryan Wubb90eb02007-12-04 23:45:18 -0800356 cs_active(drv_data, chip);
357 dummy_read(drv_data);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800358
359 while (drv_data->rx < drv_data->rx_end - 1) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800360 cs_deactive(drv_data, chip);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800361
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700362 if (chip->cs_chg_udelay)
363 udelay(chip->cs_chg_udelay);
Sonic Zhangcc487e72007-12-04 23:45:17 -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);
415 cs_deactive(drv_data, chip);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800416
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700417 if (chip->cs_chg_udelay)
418 udelay(chip->cs_chg_udelay);
419 ++drv_data->rx;
420 ++drv_data->tx;
421 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700422}
423
424static void u16_writer(struct driver_data *drv_data)
425{
Bryan Wu131b17d2007-12-04 23:45:12 -0800426 dev_dbg(&drv_data->pdev->dev,
Bryan Wubb90eb02007-12-04 23:45:18 -0800427 "cr16 is 0x%x\n", read_STAT(drv_data));
Bryan Wu88b40362007-05-21 18:32:16 +0800428
Sonic Zhang3f479a62007-12-04 23:45:18 -0800429 /* poll for SPI completion before start */
Bryan Wubb90eb02007-12-04 23:45:18 -0800430 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Sonic Zhang3f479a62007-12-04 23:45:18 -0800431 continue;
432
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700433 while (drv_data->tx < drv_data->tx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800434 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
435 while ((read_STAT(drv_data) & BIT_STAT_TXS))
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700436 continue;
437 drv_data->tx += 2;
438 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700439}
440
441static void u16_cs_chg_writer(struct driver_data *drv_data)
442{
443 struct chip_data *chip = drv_data->cur_chip;
444
Sonic Zhang3f479a62007-12-04 23:45:18 -0800445 /* poll for SPI completion before start */
Bryan Wubb90eb02007-12-04 23:45:18 -0800446 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Sonic Zhang3f479a62007-12-04 23:45:18 -0800447 continue;
448
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700449 while (drv_data->tx < drv_data->tx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800450 cs_active(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700451
Bryan Wubb90eb02007-12-04 23:45:18 -0800452 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
453 while ((read_STAT(drv_data) & BIT_STAT_TXS))
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700454 continue;
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 if (chip->cs_chg_udelay)
458 udelay(chip->cs_chg_udelay);
459 drv_data->tx += 2;
460 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700461}
462
463static void u16_reader(struct driver_data *drv_data)
464{
Bryan Wu88b40362007-05-21 18:32:16 +0800465 dev_dbg(&drv_data->pdev->dev,
Bryan Wubb90eb02007-12-04 23:45:18 -0800466 "cr-16 is 0x%x\n", read_STAT(drv_data));
Sonic Zhangcc487e72007-12-04 23:45:17 -0800467
Sonic Zhang3f479a62007-12-04 23:45:18 -0800468 /* poll for SPI completion before start */
Bryan Wubb90eb02007-12-04 23:45:18 -0800469 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Sonic Zhang3f479a62007-12-04 23:45:18 -0800470 continue;
471
Sonic Zhangcc487e72007-12-04 23:45:17 -0800472 /* clear TDBR buffer before read(else it will be shifted out) */
Bryan Wubb90eb02007-12-04 23:45:18 -0800473 write_TDBR(drv_data, 0xFFFF);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800474
Bryan Wubb90eb02007-12-04 23:45:18 -0800475 dummy_read(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700476
477 while (drv_data->rx < (drv_data->rx_end - 2)) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800478 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700479 continue;
Bryan Wubb90eb02007-12-04 23:45:18 -0800480 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700481 drv_data->rx += 2;
482 }
483
Bryan Wubb90eb02007-12-04 23:45:18 -0800484 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700485 continue;
Bryan Wubb90eb02007-12-04 23:45:18 -0800486 *(u16 *) (drv_data->rx) = read_SHAW(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700487 drv_data->rx += 2;
488}
489
490static void u16_cs_chg_reader(struct driver_data *drv_data)
491{
492 struct chip_data *chip = drv_data->cur_chip;
493
Sonic Zhang3f479a62007-12-04 23:45:18 -0800494 /* poll for SPI completion before start */
Bryan Wubb90eb02007-12-04 23:45:18 -0800495 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Sonic Zhang3f479a62007-12-04 23:45:18 -0800496 continue;
497
Sonic Zhangcc487e72007-12-04 23:45:17 -0800498 /* clear TDBR buffer before read(else it will be shifted out) */
Bryan Wubb90eb02007-12-04 23:45:18 -0800499 write_TDBR(drv_data, 0xFFFF);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700500
Bryan Wubb90eb02007-12-04 23:45:18 -0800501 cs_active(drv_data, chip);
502 dummy_read(drv_data);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800503
504 while (drv_data->rx < drv_data->rx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800505 cs_deactive(drv_data, chip);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800506
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700507 if (chip->cs_chg_udelay)
508 udelay(chip->cs_chg_udelay);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800509
Bryan Wubb90eb02007-12-04 23:45:18 -0800510 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Sonic Zhangcc487e72007-12-04 23:45:17 -0800511 continue;
Bryan Wubb90eb02007-12-04 23:45:18 -0800512 cs_active(drv_data, chip);
513 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700514 drv_data->rx += 2;
515 }
Bryan Wubb90eb02007-12-04 23:45:18 -0800516 cs_deactive(drv_data, chip);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800517
Bryan Wubb90eb02007-12-04 23:45:18 -0800518 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Sonic Zhangcc487e72007-12-04 23:45:17 -0800519 continue;
Bryan Wubb90eb02007-12-04 23:45:18 -0800520 *(u16 *) (drv_data->rx) = read_SHAW(drv_data);
Sonic Zhangcc487e72007-12-04 23:45:17 -0800521 drv_data->rx += 2;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700522}
523
524static void u16_duplex(struct driver_data *drv_data)
525{
Sonic Zhang3f479a62007-12-04 23:45:18 -0800526 /* poll for SPI completion before start */
Bryan Wubb90eb02007-12-04 23:45:18 -0800527 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Sonic Zhang3f479a62007-12-04 23:45:18 -0800528 continue;
529
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700530 /* in duplex mode, clk is triggered by writing of TDBR */
531 while (drv_data->tx < drv_data->tx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800532 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
533 while (read_STAT(drv_data) & BIT_STAT_TXS)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700534 continue;
Bryan Wubb90eb02007-12-04 23:45:18 -0800535 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700536 continue;
Bryan Wubb90eb02007-12-04 23:45:18 -0800537 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700538 drv_data->rx += 2;
539 drv_data->tx += 2;
540 }
541}
542
543static void u16_cs_chg_duplex(struct driver_data *drv_data)
544{
545 struct chip_data *chip = drv_data->cur_chip;
546
Sonic Zhang3f479a62007-12-04 23:45:18 -0800547 /* poll for SPI completion before start */
Bryan Wubb90eb02007-12-04 23:45:18 -0800548 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Sonic Zhang3f479a62007-12-04 23:45:18 -0800549 continue;
550
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700551 while (drv_data->tx < drv_data->tx_end) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800552 cs_active(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700553
Bryan Wubb90eb02007-12-04 23:45:18 -0800554 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
555 while (read_STAT(drv_data) & BIT_STAT_TXS)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700556 continue;
Bryan Wubb90eb02007-12-04 23:45:18 -0800557 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700558 continue;
Bryan Wubb90eb02007-12-04 23:45:18 -0800559 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
560 cs_deactive(drv_data, chip);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800561
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700562 if (chip->cs_chg_udelay)
563 udelay(chip->cs_chg_udelay);
564 drv_data->rx += 2;
565 drv_data->tx += 2;
566 }
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700567}
568
569/* test if ther is more transfer to be done */
570static void *next_transfer(struct driver_data *drv_data)
571{
572 struct spi_message *msg = drv_data->cur_msg;
573 struct spi_transfer *trans = drv_data->cur_transfer;
574
575 /* Move to next transfer */
576 if (trans->transfer_list.next != &msg->transfers) {
577 drv_data->cur_transfer =
578 list_entry(trans->transfer_list.next,
579 struct spi_transfer, transfer_list);
580 return RUNNING_STATE;
581 } else
582 return DONE_STATE;
583}
584
585/*
586 * caller already set message->status;
587 * dma and pio irqs are blocked give finished message back
588 */
589static void giveback(struct driver_data *drv_data)
590{
Bryan Wufad91c82007-12-04 23:45:14 -0800591 struct chip_data *chip = drv_data->cur_chip;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700592 struct spi_transfer *last_transfer;
593 unsigned long flags;
594 struct spi_message *msg;
595
596 spin_lock_irqsave(&drv_data->lock, flags);
597 msg = drv_data->cur_msg;
598 drv_data->cur_msg = NULL;
599 drv_data->cur_transfer = NULL;
600 drv_data->cur_chip = NULL;
601 queue_work(drv_data->workqueue, &drv_data->pump_messages);
602 spin_unlock_irqrestore(&drv_data->lock, flags);
603
604 last_transfer = list_entry(msg->transfers.prev,
605 struct spi_transfer, transfer_list);
606
607 msg->state = NULL;
608
609 /* disable chip select signal. And not stop spi in autobuffer mode */
610 if (drv_data->tx_dma != 0xFFFF) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800611 cs_deactive(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700612 bfin_spi_disable(drv_data);
613 }
614
Bryan Wufad91c82007-12-04 23:45:14 -0800615 if (!drv_data->cs_change)
Bryan Wubb90eb02007-12-04 23:45:18 -0800616 cs_deactive(drv_data, chip);
Bryan Wufad91c82007-12-04 23:45:14 -0800617
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700618 if (msg->complete)
619 msg->complete(msg->context);
620}
621
Bryan Wu88b40362007-05-21 18:32:16 +0800622static irqreturn_t dma_irq_handler(int irq, void *dev_id)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700623{
624 struct driver_data *drv_data = (struct driver_data *)dev_id;
Bryan Wufad91c82007-12-04 23:45:14 -0800625 struct chip_data *chip = drv_data->cur_chip;
Bryan Wubb90eb02007-12-04 23:45:18 -0800626 struct spi_message *msg = drv_data->cur_msg;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700627
Bryan Wu88b40362007-05-21 18:32:16 +0800628 dev_dbg(&drv_data->pdev->dev, "in dma_irq_handler\n");
Bryan Wubb90eb02007-12-04 23:45:18 -0800629 clear_dma_irqstat(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700630
Bryan Wud6fe89b2007-06-11 17:34:17 +0800631 /* Wait for DMA to complete */
Bryan Wubb90eb02007-12-04 23:45:18 -0800632 while (get_dma_curr_irqstat(drv_data->dma_channel) & DMA_RUN)
Bryan Wud6fe89b2007-06-11 17:34:17 +0800633 continue;
634
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700635 /*
Bryan Wud6fe89b2007-06-11 17:34:17 +0800636 * wait for the last transaction shifted out. HRM states:
637 * at this point there may still be data in the SPI DMA FIFO waiting
638 * to be transmitted ... software needs to poll TXS in the SPI_STAT
639 * register until it goes low for 2 successive reads
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700640 */
641 if (drv_data->tx != NULL) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800642 while ((read_STAT(drv_data) & TXS) ||
643 (read_STAT(drv_data) & TXS))
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700644 continue;
645 }
646
Bryan Wubb90eb02007-12-04 23:45:18 -0800647 while (!(read_STAT(drv_data) & SPIF))
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700648 continue;
649
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700650 msg->actual_length += drv_data->len_in_bytes;
651
Bryan Wufad91c82007-12-04 23:45:14 -0800652 if (drv_data->cs_change)
Bryan Wubb90eb02007-12-04 23:45:18 -0800653 cs_deactive(drv_data, chip);
Bryan Wufad91c82007-12-04 23:45:14 -0800654
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700655 /* Move to next transfer */
656 msg->state = next_transfer(drv_data);
657
658 /* Schedule transfer tasklet */
659 tasklet_schedule(&drv_data->pump_transfers);
660
661 /* free the irq handler before next transfer */
Bryan Wu88b40362007-05-21 18:32:16 +0800662 dev_dbg(&drv_data->pdev->dev,
663 "disable dma channel irq%d\n",
Bryan Wubb90eb02007-12-04 23:45:18 -0800664 drv_data->dma_channel);
665 dma_disable_irq(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700666
667 return IRQ_HANDLED;
668}
669
670static void pump_transfers(unsigned long data)
671{
672 struct driver_data *drv_data = (struct driver_data *)data;
673 struct spi_message *message = NULL;
674 struct spi_transfer *transfer = NULL;
675 struct spi_transfer *previous = NULL;
676 struct chip_data *chip = NULL;
Bryan Wu88b40362007-05-21 18:32:16 +0800677 u8 width;
678 u16 cr, dma_width, dma_config;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700679 u32 tranf_success = 1;
680
681 /* Get current state information */
682 message = drv_data->cur_msg;
683 transfer = drv_data->cur_transfer;
684 chip = drv_data->cur_chip;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700685 /*
686 * if msg is error or done, report it back using complete() callback
687 */
688
689 /* Handle for abort */
690 if (message->state == ERROR_STATE) {
691 message->status = -EIO;
692 giveback(drv_data);
693 return;
694 }
695
696 /* Handle end of message */
697 if (message->state == DONE_STATE) {
698 message->status = 0;
699 giveback(drv_data);
700 return;
701 }
702
703 /* Delay if requested at end of transfer */
704 if (message->state == RUNNING_STATE) {
705 previous = list_entry(transfer->transfer_list.prev,
706 struct spi_transfer, transfer_list);
707 if (previous->delay_usecs)
708 udelay(previous->delay_usecs);
709 }
710
711 /* Setup the transfer state based on the type of transfer */
712 if (flush(drv_data) == 0) {
713 dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
714 message->status = -EIO;
715 giveback(drv_data);
716 return;
717 }
718
719 if (transfer->tx_buf != NULL) {
720 drv_data->tx = (void *)transfer->tx_buf;
721 drv_data->tx_end = drv_data->tx + transfer->len;
Bryan Wu88b40362007-05-21 18:32:16 +0800722 dev_dbg(&drv_data->pdev->dev, "tx_buf is %p, tx_end is %p\n",
723 transfer->tx_buf, drv_data->tx_end);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700724 } else {
725 drv_data->tx = NULL;
726 }
727
728 if (transfer->rx_buf != NULL) {
729 drv_data->rx = transfer->rx_buf;
730 drv_data->rx_end = drv_data->rx + transfer->len;
Bryan Wu88b40362007-05-21 18:32:16 +0800731 dev_dbg(&drv_data->pdev->dev, "rx_buf is %p, rx_end is %p\n",
732 transfer->rx_buf, drv_data->rx_end);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700733 } else {
734 drv_data->rx = NULL;
735 }
736
737 drv_data->rx_dma = transfer->rx_dma;
738 drv_data->tx_dma = transfer->tx_dma;
739 drv_data->len_in_bytes = transfer->len;
Bryan Wufad91c82007-12-04 23:45:14 -0800740 drv_data->cs_change = transfer->cs_change;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700741
742 width = chip->width;
743 if (width == CFG_SPI_WORDSIZE16) {
744 drv_data->len = (transfer->len) >> 1;
745 } else {
746 drv_data->len = transfer->len;
747 }
748 drv_data->write = drv_data->tx ? chip->write : null_writer;
749 drv_data->read = drv_data->rx ? chip->read : null_reader;
750 drv_data->duplex = chip->duplex ? chip->duplex : null_writer;
Bryan Wu131b17d2007-12-04 23:45:12 -0800751 dev_dbg(&drv_data->pdev->dev, "transfer: ",
752 "drv_data->write is %p, chip->write is %p, null_wr is %p\n",
753 drv_data->write, chip->write, null_writer);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700754
755 /* speed and width has been set on per message */
756 message->state = RUNNING_STATE;
757 dma_config = 0;
758
Bryan Wubb90eb02007-12-04 23:45:18 -0800759 write_STAT(drv_data, BIT_STAT_CLR);
760 cr = (read_CTRL(drv_data) & (~BIT_CTL_TIMOD));
761 cs_active(drv_data, chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700762
Bryan Wu88b40362007-05-21 18:32:16 +0800763 dev_dbg(&drv_data->pdev->dev,
764 "now pumping a transfer: width is %d, len is %d\n",
765 width, transfer->len);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700766
767 /*
768 * Try to map dma buffer and do a dma transfer if
769 * successful use different way to r/w according to
770 * drv_data->cur_chip->enable_dma
771 */
772 if (drv_data->cur_chip->enable_dma && drv_data->len > 6) {
773
Bryan Wubb90eb02007-12-04 23:45:18 -0800774 disable_dma(drv_data->dma_channel);
775 clear_dma_irqstat(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700776
777 /* config dma channel */
Bryan Wu88b40362007-05-21 18:32:16 +0800778 dev_dbg(&drv_data->pdev->dev, "doing dma transfer\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700779 if (width == CFG_SPI_WORDSIZE16) {
Bryan Wubb90eb02007-12-04 23:45:18 -0800780 set_dma_x_count(drv_data->dma_channel, drv_data->len);
781 set_dma_x_modify(drv_data->dma_channel, 2);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700782 dma_width = WDSIZE_16;
783 } else {
Bryan Wubb90eb02007-12-04 23:45:18 -0800784 set_dma_x_count(drv_data->dma_channel, drv_data->len);
785 set_dma_x_modify(drv_data->dma_channel, 1);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700786 dma_width = WDSIZE_8;
787 }
788
Sonic Zhang3f479a62007-12-04 23:45:18 -0800789 /* poll for SPI completion before start */
Bryan Wubb90eb02007-12-04 23:45:18 -0800790 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
Sonic Zhang3f479a62007-12-04 23:45:18 -0800791 continue;
792
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700793 /* dirty hack for autobuffer DMA mode */
794 if (drv_data->tx_dma == 0xFFFF) {
Bryan Wu88b40362007-05-21 18:32:16 +0800795 dev_dbg(&drv_data->pdev->dev,
796 "doing autobuffer DMA out.\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700797
Sonic Zhangcc487e72007-12-04 23:45:17 -0800798 /* set SPI transfer mode */
Bryan Wubb90eb02007-12-04 23:45:18 -0800799 write_CTRL(drv_data, (cr | CFG_SPI_DMAWRITE));
Sonic Zhangcc487e72007-12-04 23:45:17 -0800800
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700801 /* no irq in autobuffer mode */
802 dma_config =
803 (DMAFLOW_AUTO | RESTART | dma_width | DI_EN);
Bryan Wubb90eb02007-12-04 23:45:18 -0800804 set_dma_config(drv_data->dma_channel, dma_config);
805 set_dma_start_addr(drv_data->dma_channel,
Bryan Wua32c6912007-12-04 23:45:15 -0800806 (unsigned long)drv_data->tx);
Bryan Wubb90eb02007-12-04 23:45:18 -0800807 enable_dma(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700808
809 /* just return here, there can only be one transfer in this mode */
810 message->status = 0;
811 giveback(drv_data);
812 return;
813 }
814
815 /* In dma mode, rx or tx must be NULL in one transfer */
816 if (drv_data->rx != NULL) {
817 /* set transfer mode, and enable SPI */
Bryan Wu88b40362007-05-21 18:32:16 +0800818 dev_dbg(&drv_data->pdev->dev, "doing DMA in.\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700819
Sonic Zhangcc487e72007-12-04 23:45:17 -0800820 /* set SPI transfer mode */
Bryan Wubb90eb02007-12-04 23:45:18 -0800821 write_CTRL(drv_data, (cr | CFG_SPI_DMAREAD));
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700822
823 /* clear tx reg soformer data is not shifted out */
Bryan Wubb90eb02007-12-04 23:45:18 -0800824 write_TDBR(drv_data, 0xFFFF);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700825
Bryan Wubb90eb02007-12-04 23:45:18 -0800826 set_dma_x_count(drv_data->dma_channel, drv_data->len);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700827
828 /* start dma */
Bryan Wubb90eb02007-12-04 23:45:18 -0800829 dma_enable_irq(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700830 dma_config = (WNR | RESTART | dma_width | DI_EN);
Bryan Wubb90eb02007-12-04 23:45:18 -0800831 set_dma_config(drv_data->dma_channel, dma_config);
832 set_dma_start_addr(drv_data->dma_channel,
Bryan Wua32c6912007-12-04 23:45:15 -0800833 (unsigned long)drv_data->rx);
Bryan Wubb90eb02007-12-04 23:45:18 -0800834 enable_dma(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700835
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700836 } else if (drv_data->tx != NULL) {
Bryan Wu88b40362007-05-21 18:32:16 +0800837 dev_dbg(&drv_data->pdev->dev, "doing DMA out.\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700838
Sonic Zhangcc487e72007-12-04 23:45:17 -0800839 /* set SPI transfer mode */
Bryan Wubb90eb02007-12-04 23:45:18 -0800840 write_CTRL(drv_data, (cr | CFG_SPI_DMAWRITE));
Sonic Zhangcc487e72007-12-04 23:45:17 -0800841
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700842 /* start dma */
Bryan Wubb90eb02007-12-04 23:45:18 -0800843 dma_enable_irq(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700844 dma_config = (RESTART | dma_width | DI_EN);
Bryan Wubb90eb02007-12-04 23:45:18 -0800845 set_dma_config(drv_data->dma_channel, dma_config);
846 set_dma_start_addr(drv_data->dma_channel,
Bryan Wua32c6912007-12-04 23:45:15 -0800847 (unsigned long)drv_data->tx);
Bryan Wubb90eb02007-12-04 23:45:18 -0800848 enable_dma(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700849 }
850 } else {
851 /* IO mode write then read */
Bryan Wu88b40362007-05-21 18:32:16 +0800852 dev_dbg(&drv_data->pdev->dev, "doing IO transfer\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700853
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700854 if (drv_data->tx != NULL && drv_data->rx != NULL) {
855 /* full duplex mode */
856 BUG_ON((drv_data->tx_end - drv_data->tx) !=
857 (drv_data->rx_end - drv_data->rx));
Bryan Wu88b40362007-05-21 18:32:16 +0800858 dev_dbg(&drv_data->pdev->dev,
859 "IO duplex: cr is 0x%x\n", cr);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700860
Sonic Zhangcc487e72007-12-04 23:45:17 -0800861 /* set SPI transfer mode */
Bryan Wubb90eb02007-12-04 23:45:18 -0800862 write_CTRL(drv_data, (cr | CFG_SPI_WRITE));
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700863
864 drv_data->duplex(drv_data);
865
866 if (drv_data->tx != drv_data->tx_end)
867 tranf_success = 0;
868 } else if (drv_data->tx != NULL) {
869 /* write only half duplex */
Bryan Wu131b17d2007-12-04 23:45:12 -0800870 dev_dbg(&drv_data->pdev->dev,
Bryan Wu88b40362007-05-21 18:32:16 +0800871 "IO write: cr is 0x%x\n", cr);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700872
Sonic Zhangcc487e72007-12-04 23:45:17 -0800873 /* set SPI transfer mode */
Bryan Wubb90eb02007-12-04 23:45:18 -0800874 write_CTRL(drv_data, (cr | CFG_SPI_WRITE));
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700875
876 drv_data->write(drv_data);
877
878 if (drv_data->tx != drv_data->tx_end)
879 tranf_success = 0;
880 } else if (drv_data->rx != NULL) {
881 /* read only half duplex */
Bryan Wu131b17d2007-12-04 23:45:12 -0800882 dev_dbg(&drv_data->pdev->dev,
Bryan Wu88b40362007-05-21 18:32:16 +0800883 "IO read: cr is 0x%x\n", cr);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700884
Sonic Zhangcc487e72007-12-04 23:45:17 -0800885 /* set SPI transfer mode */
Bryan Wubb90eb02007-12-04 23:45:18 -0800886 write_CTRL(drv_data, (cr | CFG_SPI_READ));
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700887
888 drv_data->read(drv_data);
889 if (drv_data->rx != drv_data->rx_end)
890 tranf_success = 0;
891 }
892
893 if (!tranf_success) {
Bryan Wu131b17d2007-12-04 23:45:12 -0800894 dev_dbg(&drv_data->pdev->dev,
Bryan Wu88b40362007-05-21 18:32:16 +0800895 "IO write error!\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700896 message->state = ERROR_STATE;
897 } else {
898 /* Update total byte transfered */
899 message->actual_length += drv_data->len;
900
901 /* Move to next transfer of this msg */
902 message->state = next_transfer(drv_data);
903 }
904
905 /* Schedule next transfer tasklet */
906 tasklet_schedule(&drv_data->pump_transfers);
907
908 }
909}
910
911/* pop a msg from queue and kick off real transfer */
912static void pump_messages(struct work_struct *work)
913{
Bryan Wu131b17d2007-12-04 23:45:12 -0800914 struct driver_data *drv_data;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700915 unsigned long flags;
916
Bryan Wu131b17d2007-12-04 23:45:12 -0800917 drv_data = container_of(work, struct driver_data, pump_messages);
918
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700919 /* Lock queue and check for queue work */
920 spin_lock_irqsave(&drv_data->lock, flags);
921 if (list_empty(&drv_data->queue) || drv_data->run == QUEUE_STOPPED) {
922 /* pumper kicked off but no work to do */
923 drv_data->busy = 0;
924 spin_unlock_irqrestore(&drv_data->lock, flags);
925 return;
926 }
927
928 /* Make sure we are not already running a message */
929 if (drv_data->cur_msg) {
930 spin_unlock_irqrestore(&drv_data->lock, flags);
931 return;
932 }
933
934 /* Extract head of queue */
935 drv_data->cur_msg = list_entry(drv_data->queue.next,
936 struct spi_message, queue);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800937
938 /* Setup the SSP using the per chip configuration */
939 drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
940 if (restore_state(drv_data)) {
941 spin_unlock_irqrestore(&drv_data->lock, flags);
942 return;
943 };
944
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700945 list_del_init(&drv_data->cur_msg->queue);
946
947 /* Initial message state */
948 drv_data->cur_msg->state = START_STATE;
949 drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
950 struct spi_transfer, transfer_list);
951
Bryan Wu5fec5b52007-12-04 23:45:13 -0800952 dev_dbg(&drv_data->pdev->dev, "got a message to pump, "
953 "state is set to: baud %d, flag 0x%x, ctl 0x%x\n",
954 drv_data->cur_chip->baud, drv_data->cur_chip->flag,
955 drv_data->cur_chip->ctl_reg);
Bryan Wu131b17d2007-12-04 23:45:12 -0800956
957 dev_dbg(&drv_data->pdev->dev,
Bryan Wu88b40362007-05-21 18:32:16 +0800958 "the first transfer len is %d\n",
959 drv_data->cur_transfer->len);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700960
961 /* Mark as busy and launch transfers */
962 tasklet_schedule(&drv_data->pump_transfers);
963
964 drv_data->busy = 1;
965 spin_unlock_irqrestore(&drv_data->lock, flags);
966}
967
968/*
969 * got a msg to transfer, queue it in drv_data->queue.
970 * And kick off message pumper
971 */
972static int transfer(struct spi_device *spi, struct spi_message *msg)
973{
974 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
975 unsigned long flags;
976
977 spin_lock_irqsave(&drv_data->lock, flags);
978
979 if (drv_data->run == QUEUE_STOPPED) {
980 spin_unlock_irqrestore(&drv_data->lock, flags);
981 return -ESHUTDOWN;
982 }
983
984 msg->actual_length = 0;
985 msg->status = -EINPROGRESS;
986 msg->state = START_STATE;
987
Bryan Wu88b40362007-05-21 18:32:16 +0800988 dev_dbg(&spi->dev, "adding an msg in transfer() \n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700989 list_add_tail(&msg->queue, &drv_data->queue);
990
991 if (drv_data->run == QUEUE_RUNNING && !drv_data->busy)
992 queue_work(drv_data->workqueue, &drv_data->pump_messages);
993
994 spin_unlock_irqrestore(&drv_data->lock, flags);
995
996 return 0;
997}
998
Sonic Zhang12e17c42007-12-04 23:45:16 -0800999#define MAX_SPI_SSEL 7
1000
1001static u16 ssel[3][MAX_SPI_SSEL] = {
1002 {P_SPI0_SSEL1, P_SPI0_SSEL2, P_SPI0_SSEL3,
1003 P_SPI0_SSEL4, P_SPI0_SSEL5,
1004 P_SPI0_SSEL6, P_SPI0_SSEL7},
1005
1006 {P_SPI1_SSEL1, P_SPI1_SSEL2, P_SPI1_SSEL3,
1007 P_SPI1_SSEL4, P_SPI1_SSEL5,
1008 P_SPI1_SSEL6, P_SPI1_SSEL7},
1009
1010 {P_SPI2_SSEL1, P_SPI2_SSEL2, P_SPI2_SSEL3,
1011 P_SPI2_SSEL4, P_SPI2_SSEL5,
1012 P_SPI2_SSEL6, P_SPI2_SSEL7},
1013};
1014
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001015/* first setup for new devices */
1016static int setup(struct spi_device *spi)
1017{
1018 struct bfin5xx_spi_chip *chip_info = NULL;
1019 struct chip_data *chip;
1020 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
1021 u8 spi_flg;
1022
1023 /* Abort device setup if requested features are not supported */
1024 if (spi->mode & ~(SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST)) {
1025 dev_err(&spi->dev, "requested mode not fully supported\n");
1026 return -EINVAL;
1027 }
1028
1029 /* Zero (the default) here means 8 bits */
1030 if (!spi->bits_per_word)
1031 spi->bits_per_word = 8;
1032
1033 if (spi->bits_per_word != 8 && spi->bits_per_word != 16)
1034 return -EINVAL;
1035
1036 /* Only alloc (or use chip_info) on first setup */
1037 chip = spi_get_ctldata(spi);
1038 if (chip == NULL) {
1039 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
1040 if (!chip)
1041 return -ENOMEM;
1042
1043 chip->enable_dma = 0;
1044 chip_info = spi->controller_data;
1045 }
1046
1047 /* chip_info isn't always needed */
1048 if (chip_info) {
Mike Frysinger2ed35512007-12-04 23:45:14 -08001049 /* Make sure people stop trying to set fields via ctl_reg
1050 * when they should actually be using common SPI framework.
1051 * Currently we let through: WOM EMISO PSSE GM SZ TIMOD.
1052 * Not sure if a user actually needs/uses any of these,
1053 * but let's assume (for now) they do.
1054 */
1055 if (chip_info->ctl_reg & (SPE|MSTR|CPOL|CPHA|LSBF|SIZE)) {
1056 dev_err(&spi->dev, "do not set bits in ctl_reg "
1057 "that the SPI framework manages\n");
1058 return -EINVAL;
1059 }
1060
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001061 chip->enable_dma = chip_info->enable_dma != 0
1062 && drv_data->master_info->enable_dma;
1063 chip->ctl_reg = chip_info->ctl_reg;
1064 chip->bits_per_word = chip_info->bits_per_word;
1065 chip->cs_change_per_word = chip_info->cs_change_per_word;
1066 chip->cs_chg_udelay = chip_info->cs_chg_udelay;
1067 }
1068
1069 /* translate common spi framework into our register */
1070 if (spi->mode & SPI_CPOL)
1071 chip->ctl_reg |= CPOL;
1072 if (spi->mode & SPI_CPHA)
1073 chip->ctl_reg |= CPHA;
1074 if (spi->mode & SPI_LSB_FIRST)
1075 chip->ctl_reg |= LSBF;
1076 /* we dont support running in slave mode (yet?) */
1077 chip->ctl_reg |= MSTR;
1078
1079 /*
1080 * if any one SPI chip is registered and wants DMA, request the
1081 * DMA channel for it
1082 */
Bryan Wubb90eb02007-12-04 23:45:18 -08001083 if (chip->enable_dma && !drv_data->dma_requested) {
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001084 /* register dma irq handler */
Bryan Wubb90eb02007-12-04 23:45:18 -08001085 if (request_dma(drv_data->dma_channel, "BF53x_SPI_DMA") < 0) {
Bryan Wu88b40362007-05-21 18:32:16 +08001086 dev_dbg(&spi->dev,
1087 "Unable to request BlackFin SPI DMA channel\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001088 return -ENODEV;
1089 }
Bryan Wubb90eb02007-12-04 23:45:18 -08001090 if (set_dma_callback(drv_data->dma_channel,
1091 (void *)dma_irq_handler, drv_data) < 0) {
Bryan Wu88b40362007-05-21 18:32:16 +08001092 dev_dbg(&spi->dev, "Unable to set dma callback\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001093 return -EPERM;
1094 }
Bryan Wubb90eb02007-12-04 23:45:18 -08001095 dma_disable_irq(drv_data->dma_channel);
1096 drv_data->dma_requested = 1;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001097 }
1098
1099 /*
1100 * Notice: for blackfin, the speed_hz is the value of register
1101 * SPI_BAUD, not the real baudrate
1102 */
1103 chip->baud = hz_to_spi_baud(spi->max_speed_hz);
1104 spi_flg = ~(1 << (spi->chip_select));
1105 chip->flag = ((u16) spi_flg << 8) | (1 << (spi->chip_select));
1106 chip->chip_select_num = spi->chip_select;
1107
1108 switch (chip->bits_per_word) {
1109 case 8:
1110 chip->n_bytes = 1;
1111 chip->width = CFG_SPI_WORDSIZE8;
1112 chip->read = chip->cs_change_per_word ?
1113 u8_cs_chg_reader : u8_reader;
1114 chip->write = chip->cs_change_per_word ?
1115 u8_cs_chg_writer : u8_writer;
1116 chip->duplex = chip->cs_change_per_word ?
1117 u8_cs_chg_duplex : u8_duplex;
1118 break;
1119
1120 case 16:
1121 chip->n_bytes = 2;
1122 chip->width = CFG_SPI_WORDSIZE16;
1123 chip->read = chip->cs_change_per_word ?
1124 u16_cs_chg_reader : u16_reader;
1125 chip->write = chip->cs_change_per_word ?
1126 u16_cs_chg_writer : u16_writer;
1127 chip->duplex = chip->cs_change_per_word ?
1128 u16_cs_chg_duplex : u16_duplex;
1129 break;
1130
1131 default:
1132 dev_err(&spi->dev, "%d bits_per_word is not supported\n",
1133 chip->bits_per_word);
1134 kfree(chip);
1135 return -ENODEV;
1136 }
1137
Joe Perches898eb712007-10-18 03:06:30 -07001138 dev_dbg(&spi->dev, "setup spi chip %s, width is %d, dma is %d\n",
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001139 spi->modalias, chip->width, chip->enable_dma);
Bryan Wu88b40362007-05-21 18:32:16 +08001140 dev_dbg(&spi->dev, "ctl_reg is 0x%x, flag_reg is 0x%x\n",
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001141 chip->ctl_reg, chip->flag);
1142
1143 spi_set_ctldata(spi, chip);
1144
Sonic Zhang12e17c42007-12-04 23:45:16 -08001145 dev_dbg(&spi->dev, "chip select number is %d\n", chip->chip_select_num);
1146 if ((chip->chip_select_num > 0)
1147 && (chip->chip_select_num <= spi->master->num_chipselect))
1148 peripheral_request(ssel[spi->master->bus_num]
1149 [chip->chip_select_num-1], DRV_NAME);
1150
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001151 return 0;
1152}
1153
1154/*
1155 * callback for spi framework.
1156 * clean driver specific data
1157 */
Bryan Wu88b40362007-05-21 18:32:16 +08001158static void cleanup(struct spi_device *spi)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001159{
Mike Frysinger27bb9e72007-06-11 15:31:30 +08001160 struct chip_data *chip = spi_get_ctldata(spi);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001161
Sonic Zhang12e17c42007-12-04 23:45:16 -08001162 if ((chip->chip_select_num > 0)
1163 && (chip->chip_select_num <= spi->master->num_chipselect))
1164 peripheral_free(ssel[spi->master->bus_num]
1165 [chip->chip_select_num-1]);
1166
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001167 kfree(chip);
1168}
1169
1170static inline int init_queue(struct driver_data *drv_data)
1171{
1172 INIT_LIST_HEAD(&drv_data->queue);
1173 spin_lock_init(&drv_data->lock);
1174
1175 drv_data->run = QUEUE_STOPPED;
1176 drv_data->busy = 0;
1177
1178 /* init transfer tasklet */
1179 tasklet_init(&drv_data->pump_transfers,
1180 pump_transfers, (unsigned long)drv_data);
1181
1182 /* init messages workqueue */
1183 INIT_WORK(&drv_data->pump_messages, pump_messages);
1184 drv_data->workqueue =
Tony Jones49dce682007-10-16 01:27:48 -07001185 create_singlethread_workqueue(drv_data->master->dev.parent->bus_id);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001186 if (drv_data->workqueue == NULL)
1187 return -EBUSY;
1188
1189 return 0;
1190}
1191
1192static inline int start_queue(struct driver_data *drv_data)
1193{
1194 unsigned long flags;
1195
1196 spin_lock_irqsave(&drv_data->lock, flags);
1197
1198 if (drv_data->run == QUEUE_RUNNING || drv_data->busy) {
1199 spin_unlock_irqrestore(&drv_data->lock, flags);
1200 return -EBUSY;
1201 }
1202
1203 drv_data->run = QUEUE_RUNNING;
1204 drv_data->cur_msg = NULL;
1205 drv_data->cur_transfer = NULL;
1206 drv_data->cur_chip = NULL;
1207 spin_unlock_irqrestore(&drv_data->lock, flags);
1208
1209 queue_work(drv_data->workqueue, &drv_data->pump_messages);
1210
1211 return 0;
1212}
1213
1214static inline int stop_queue(struct driver_data *drv_data)
1215{
1216 unsigned long flags;
1217 unsigned limit = 500;
1218 int status = 0;
1219
1220 spin_lock_irqsave(&drv_data->lock, flags);
1221
1222 /*
1223 * This is a bit lame, but is optimized for the common execution path.
1224 * A wait_queue on the drv_data->busy could be used, but then the common
1225 * execution path (pump_messages) would be required to call wake_up or
1226 * friends on every SPI message. Do this instead
1227 */
1228 drv_data->run = QUEUE_STOPPED;
1229 while (!list_empty(&drv_data->queue) && drv_data->busy && limit--) {
1230 spin_unlock_irqrestore(&drv_data->lock, flags);
1231 msleep(10);
1232 spin_lock_irqsave(&drv_data->lock, flags);
1233 }
1234
1235 if (!list_empty(&drv_data->queue) || drv_data->busy)
1236 status = -EBUSY;
1237
1238 spin_unlock_irqrestore(&drv_data->lock, flags);
1239
1240 return status;
1241}
1242
1243static inline int destroy_queue(struct driver_data *drv_data)
1244{
1245 int status;
1246
1247 status = stop_queue(drv_data);
1248 if (status != 0)
1249 return status;
1250
1251 destroy_workqueue(drv_data->workqueue);
1252
1253 return 0;
1254}
1255
Sonic Zhang7c4ef092007-12-04 23:45:16 -08001256static int setup_pin_mux(int action, int bus_num)
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001257{
1258
Sonic Zhang7c4ef092007-12-04 23:45:16 -08001259 u16 pin_req[3][4] = {
1260 {P_SPI0_SCK, P_SPI0_MISO, P_SPI0_MOSI, 0},
1261 {P_SPI1_SCK, P_SPI1_MISO, P_SPI1_MOSI, 0},
1262 {P_SPI2_SCK, P_SPI2_MISO, P_SPI2_MOSI, 0},
1263 };
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001264
1265 if (action) {
Sonic Zhang7c4ef092007-12-04 23:45:16 -08001266 if (peripheral_request_list(pin_req[bus_num], DRV_NAME))
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001267 return -EFAULT;
1268 } else {
Sonic Zhang7c4ef092007-12-04 23:45:16 -08001269 peripheral_free_list(pin_req[bus_num]);
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001270 }
1271
1272 return 0;
1273}
1274
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001275static int __init bfin5xx_spi_probe(struct platform_device *pdev)
1276{
1277 struct device *dev = &pdev->dev;
1278 struct bfin5xx_spi_master *platform_info;
1279 struct spi_master *master;
1280 struct driver_data *drv_data = 0;
Bryan Wua32c6912007-12-04 23:45:15 -08001281 struct resource *res;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001282 int status = 0;
1283
1284 platform_info = dev->platform_data;
1285
1286 /* Allocate master with space for drv_data */
1287 master = spi_alloc_master(dev, sizeof(struct driver_data) + 16);
1288 if (!master) {
1289 dev_err(&pdev->dev, "can not alloc spi_master\n");
1290 return -ENOMEM;
1291 }
Bryan Wu131b17d2007-12-04 23:45:12 -08001292
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001293 drv_data = spi_master_get_devdata(master);
1294 drv_data->master = master;
1295 drv_data->master_info = platform_info;
1296 drv_data->pdev = pdev;
1297
1298 master->bus_num = pdev->id;
1299 master->num_chipselect = platform_info->num_chipselect;
1300 master->cleanup = cleanup;
1301 master->setup = setup;
1302 master->transfer = transfer;
1303
Bryan Wua32c6912007-12-04 23:45:15 -08001304 /* Find and map our resources */
1305 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1306 if (res == NULL) {
1307 dev_err(dev, "Cannot get IORESOURCE_MEM\n");
1308 status = -ENOENT;
1309 goto out_error_get_res;
1310 }
1311
Bryan Wubb90eb02007-12-04 23:45:18 -08001312 drv_data->regs_base = (u32) ioremap(res->start,
1313 (res->end - res->start + 1));
1314 if (!drv_data->regs_base) {
Bryan Wua32c6912007-12-04 23:45:15 -08001315 dev_err(dev, "Cannot map IO\n");
1316 status = -ENXIO;
1317 goto out_error_ioremap;
1318 }
1319
Bryan Wubb90eb02007-12-04 23:45:18 -08001320 drv_data->dma_channel = platform_get_irq(pdev, 0);
1321 if (drv_data->dma_channel < 0) {
Bryan Wua32c6912007-12-04 23:45:15 -08001322 dev_err(dev, "No DMA channel specified\n");
1323 status = -ENOENT;
1324 goto out_error_no_dma_ch;
1325 }
1326
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001327 /* Initial and start queue */
1328 status = init_queue(drv_data);
1329 if (status != 0) {
Bryan Wua32c6912007-12-04 23:45:15 -08001330 dev_err(dev, "problem initializing queue\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001331 goto out_error_queue_alloc;
1332 }
Bryan Wua32c6912007-12-04 23:45:15 -08001333
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001334 status = start_queue(drv_data);
1335 if (status != 0) {
Bryan Wua32c6912007-12-04 23:45:15 -08001336 dev_err(dev, "problem starting queue\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001337 goto out_error_queue_alloc;
1338 }
1339
1340 /* Register with the SPI framework */
1341 platform_set_drvdata(pdev, drv_data);
1342 status = spi_register_master(master);
1343 if (status != 0) {
Bryan Wua32c6912007-12-04 23:45:15 -08001344 dev_err(dev, "problem registering spi master\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001345 goto out_error_queue_alloc;
1346 }
Bryan Wua32c6912007-12-04 23:45:15 -08001347
Sonic Zhang7c4ef092007-12-04 23:45:16 -08001348 if (setup_pin_mux(1, master->bus_num)) {
1349 dev_err(&pdev->dev, ": Requesting Peripherals failed\n");
1350 goto out_error;
1351 }
1352
Bryan Wubb90eb02007-12-04 23:45:18 -08001353 dev_info(dev, "%s, Version %s, regs_base@0x%08x, dma channel@%d\n",
1354 DRV_DESC, DRV_VERSION, drv_data->regs_base,
1355 drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001356 return status;
1357
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001358out_error_queue_alloc:
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001359 destroy_queue(drv_data);
Bryan Wua32c6912007-12-04 23:45:15 -08001360out_error_no_dma_ch:
Bryan Wubb90eb02007-12-04 23:45:18 -08001361 iounmap((void *) drv_data->regs_base);
Bryan Wua32c6912007-12-04 23:45:15 -08001362out_error_ioremap:
1363out_error_get_res:
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001364out_error:
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001365 spi_master_put(master);
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001366
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001367 return status;
1368}
1369
1370/* stop hardware and remove the driver */
1371static int __devexit bfin5xx_spi_remove(struct platform_device *pdev)
1372{
1373 struct driver_data *drv_data = platform_get_drvdata(pdev);
1374 int status = 0;
1375
1376 if (!drv_data)
1377 return 0;
1378
1379 /* Remove the queue */
1380 status = destroy_queue(drv_data);
1381 if (status != 0)
1382 return status;
1383
1384 /* Disable the SSP at the peripheral and SOC level */
1385 bfin_spi_disable(drv_data);
1386
1387 /* Release DMA */
1388 if (drv_data->master_info->enable_dma) {
Bryan Wubb90eb02007-12-04 23:45:18 -08001389 if (dma_channel_active(drv_data->dma_channel))
1390 free_dma(drv_data->dma_channel);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001391 }
1392
1393 /* Disconnect from the SPI framework */
1394 spi_unregister_master(drv_data->master);
1395
Sonic Zhang7c4ef092007-12-04 23:45:16 -08001396 setup_pin_mux(0, drv_data->master->bus_num);
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001397
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001398 /* Prevent double remove */
1399 platform_set_drvdata(pdev, NULL);
1400
1401 return 0;
1402}
1403
1404#ifdef CONFIG_PM
1405static int bfin5xx_spi_suspend(struct platform_device *pdev, pm_message_t state)
1406{
1407 struct driver_data *drv_data = platform_get_drvdata(pdev);
1408 int status = 0;
1409
1410 status = stop_queue(drv_data);
1411 if (status != 0)
1412 return status;
1413
1414 /* stop hardware */
1415 bfin_spi_disable(drv_data);
1416
1417 return 0;
1418}
1419
1420static int bfin5xx_spi_resume(struct platform_device *pdev)
1421{
1422 struct driver_data *drv_data = platform_get_drvdata(pdev);
1423 int status = 0;
1424
1425 /* Enable the SPI interface */
1426 bfin_spi_enable(drv_data);
1427
1428 /* Start the queue running */
1429 status = start_queue(drv_data);
1430 if (status != 0) {
1431 dev_err(&pdev->dev, "problem starting queue (%d)\n", status);
1432 return status;
1433 }
1434
1435 return 0;
1436}
1437#else
1438#define bfin5xx_spi_suspend NULL
1439#define bfin5xx_spi_resume NULL
1440#endif /* CONFIG_PM */
1441
David Brownellfc3ba952007-08-30 23:56:24 -07001442MODULE_ALIAS("bfin-spi-master"); /* for platform bus hotplug */
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001443static struct platform_driver bfin5xx_spi_driver = {
David Brownellfc3ba952007-08-30 23:56:24 -07001444 .driver = {
Bryan Wua32c6912007-12-04 23:45:15 -08001445 .name = DRV_NAME,
Bryan Wu88b40362007-05-21 18:32:16 +08001446 .owner = THIS_MODULE,
1447 },
1448 .suspend = bfin5xx_spi_suspend,
1449 .resume = bfin5xx_spi_resume,
1450 .remove = __devexit_p(bfin5xx_spi_remove),
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001451};
1452
1453static int __init bfin5xx_spi_init(void)
1454{
Bryan Wu88b40362007-05-21 18:32:16 +08001455 return platform_driver_probe(&bfin5xx_spi_driver, bfin5xx_spi_probe);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001456}
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001457module_init(bfin5xx_spi_init);
1458
1459static void __exit bfin5xx_spi_exit(void)
1460{
1461 platform_driver_unregister(&bfin5xx_spi_driver);
1462}
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001463module_exit(bfin5xx_spi_exit);