blob: 4496ed158e6b3e32b6a608ea6cf67b890657f99c [file] [log] [blame]
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001/*
Bryan Wu131b17d2007-12-04 23:45:12 -08002 * File: drivers/spi/bfin5xx_spi.c
3 * Maintainer:
4 * Bryan Wu <bryan.wu@analog.com>
5 * Original Author:
6 * Luke Yang (Analog Devices Inc.)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07007 *
Bryan Wu131b17d2007-12-04 23:45:12 -08008 * Created: March. 10th 2006
9 * Description: SPI controller driver for Blackfin BF5xx
10 * Bugs: Enter bugs at http://blackfin.uclinux.org/
Wu, Bryana5f6abd2007-05-06 14:50:34 -070011 *
12 * Modified:
13 * March 10, 2006 bfin5xx_spi.c Created. (Luke Yang)
14 * August 7, 2006 added full duplex mode (Axel Weiss & Luke Yang)
Bryan Wu131b17d2007-12-04 23:45:12 -080015 * July 17, 2007 add support for BF54x SPI0 controller (Bryan Wu)
Bryan Wua32c6912007-12-04 23:45:15 -080016 * July 30, 2007 add platfrom_resource interface to support multi-port
17 * SPI controller (Bryan Wu)
Wu, Bryana5f6abd2007-05-06 14:50:34 -070018 *
Bryan Wu131b17d2007-12-04 23:45:12 -080019 * Copyright 2004-2007 Analog Devices Inc.
Wu, Bryana5f6abd2007-05-06 14:50:34 -070020 *
21 * This program is free software ; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation ; either version 2, or (at your option)
24 * any later version.
25 *
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY ; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
30 *
31 * You should have received a copy of the GNU General Public License
32 * along with this program ; see the file COPYING.
33 * If not, write to the Free Software Foundation,
34 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
35 */
36
37#include <linux/init.h>
38#include <linux/module.h>
Bryan Wu131b17d2007-12-04 23:45:12 -080039#include <linux/delay.h>
Wu, Bryana5f6abd2007-05-06 14:50:34 -070040#include <linux/device.h>
Bryan Wu131b17d2007-12-04 23:45:12 -080041#include <linux/io.h>
Wu, Bryana5f6abd2007-05-06 14:50:34 -070042#include <linux/ioport.h>
Bryan Wu131b17d2007-12-04 23:45:12 -080043#include <linux/irq.h>
Wu, Bryana5f6abd2007-05-06 14:50:34 -070044#include <linux/errno.h>
45#include <linux/interrupt.h>
46#include <linux/platform_device.h>
47#include <linux/dma-mapping.h>
48#include <linux/spi/spi.h>
49#include <linux/workqueue.h>
Wu, Bryana5f6abd2007-05-06 14:50:34 -070050
Wu, Bryana5f6abd2007-05-06 14:50:34 -070051#include <asm/dma.h>
Bryan Wu131b17d2007-12-04 23:45:12 -080052#include <asm/portmux.h>
Wu, Bryana5f6abd2007-05-06 14:50:34 -070053#include <asm/bfin5xx_spi.h>
54
Bryan Wua32c6912007-12-04 23:45:15 -080055#define DRV_NAME "bfin-spi"
56#define DRV_AUTHOR "Bryan Wu, Luke Yang"
57#define DRV_DESC "Blackfin BF5xx on-chip SPI Contoller Driver"
58#define DRV_VERSION "1.0"
59
60MODULE_AUTHOR(DRV_AUTHOR);
61MODULE_DESCRIPTION(DRV_DESC);
Wu, Bryana5f6abd2007-05-06 14:50:34 -070062MODULE_LICENSE("GPL");
63
64#define IS_DMA_ALIGNED(x) (((u32)(x)&0x07)==0)
65
Bryan Wua32c6912007-12-04 23:45:15 -080066static u32 spi_dma_ch;
67static u32 spi_regs_base;
68
Wu, Bryana5f6abd2007-05-06 14:50:34 -070069#define DEFINE_SPI_REG(reg, off) \
70static inline u16 read_##reg(void) \
Bryan Wua32c6912007-12-04 23:45:15 -080071 { return bfin_read16(spi_regs_base + off); } \
Wu, Bryana5f6abd2007-05-06 14:50:34 -070072static inline void write_##reg(u16 v) \
Bryan Wua32c6912007-12-04 23:45:15 -080073 {bfin_write16(spi_regs_base + off, v); }
Wu, Bryana5f6abd2007-05-06 14:50:34 -070074
75DEFINE_SPI_REG(CTRL, 0x00)
76DEFINE_SPI_REG(FLAG, 0x04)
77DEFINE_SPI_REG(STAT, 0x08)
78DEFINE_SPI_REG(TDBR, 0x0C)
79DEFINE_SPI_REG(RDBR, 0x10)
80DEFINE_SPI_REG(BAUD, 0x14)
81DEFINE_SPI_REG(SHAW, 0x18)
82#define START_STATE ((void*)0)
83#define RUNNING_STATE ((void*)1)
84#define DONE_STATE ((void*)2)
85#define ERROR_STATE ((void*)-1)
86#define QUEUE_RUNNING 0
87#define QUEUE_STOPPED 1
88int dma_requested;
89
90struct driver_data {
91 /* Driver model hookup */
92 struct platform_device *pdev;
93
94 /* SPI framework hookup */
95 struct spi_master *master;
96
97 /* BFIN hookup */
98 struct bfin5xx_spi_master *master_info;
99
100 /* Driver message queue */
101 struct workqueue_struct *workqueue;
102 struct work_struct pump_messages;
103 spinlock_t lock;
104 struct list_head queue;
105 int busy;
106 int run;
107
108 /* Message Transfer pump */
109 struct tasklet_struct pump_transfers;
110
111 /* Current message transfer state info */
112 struct spi_message *cur_msg;
113 struct spi_transfer *cur_transfer;
114 struct chip_data *cur_chip;
115 size_t len_in_bytes;
116 size_t len;
117 void *tx;
118 void *tx_end;
119 void *rx;
120 void *rx_end;
121 int dma_mapped;
122 dma_addr_t rx_dma;
123 dma_addr_t tx_dma;
124 size_t rx_map_len;
125 size_t tx_map_len;
126 u8 n_bytes;
Bryan Wufad91c82007-12-04 23:45:14 -0800127 int cs_change;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700128 void (*write) (struct driver_data *);
129 void (*read) (struct driver_data *);
130 void (*duplex) (struct driver_data *);
131};
132
133struct chip_data {
134 u16 ctl_reg;
135 u16 baud;
136 u16 flag;
137
138 u8 chip_select_num;
139 u8 n_bytes;
Bryan Wu88b40362007-05-21 18:32:16 +0800140 u8 width; /* 0 or 1 */
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700141 u8 enable_dma;
142 u8 bits_per_word; /* 8 or 16 */
143 u8 cs_change_per_word;
144 u8 cs_chg_udelay;
145 void (*write) (struct driver_data *);
146 void (*read) (struct driver_data *);
147 void (*duplex) (struct driver_data *);
148};
149
Bryan Wu88b40362007-05-21 18:32:16 +0800150static void bfin_spi_enable(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700151{
152 u16 cr;
153
154 cr = read_CTRL();
155 write_CTRL(cr | BIT_CTL_ENABLE);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700156}
157
Bryan Wu88b40362007-05-21 18:32:16 +0800158static void bfin_spi_disable(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700159{
160 u16 cr;
161
162 cr = read_CTRL();
163 write_CTRL(cr & (~BIT_CTL_ENABLE));
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700164}
165
166/* Caculate the SPI_BAUD register value based on input HZ */
167static u16 hz_to_spi_baud(u32 speed_hz)
168{
169 u_long sclk = get_sclk();
170 u16 spi_baud = (sclk / (2 * speed_hz));
171
172 if ((sclk % (2 * speed_hz)) > 0)
173 spi_baud++;
174
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700175 return spi_baud;
176}
177
178static int flush(struct driver_data *drv_data)
179{
180 unsigned long limit = loops_per_jiffy << 1;
181
182 /* wait for stop and clear stat */
183 while (!(read_STAT() & BIT_STAT_SPIF) && limit--)
184 continue;
185
186 write_STAT(BIT_STAT_CLR);
187
188 return limit;
189}
190
Bryan Wufad91c82007-12-04 23:45:14 -0800191/* Chip select operation functions for cs_change flag */
192static void cs_active(struct chip_data *chip)
193{
194 u16 flag = read_FLAG();
195
196 flag |= chip->flag;
197 flag &= ~(chip->flag << 8);
198
199 write_FLAG(flag);
200}
201
202static void cs_deactive(struct chip_data *chip)
203{
204 u16 flag = read_FLAG();
205
206 flag |= (chip->flag << 8);
207
208 write_FLAG(flag);
209}
210
Sonic Zhang7c4ef092007-12-04 23:45:16 -0800211#define MAX_SPI_SSEL 7
Bryan Wu5fec5b52007-12-04 23:45:13 -0800212
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700213/* stop controller and re-config current chip*/
Bryan Wu5fec5b52007-12-04 23:45:13 -0800214static int restore_state(struct driver_data *drv_data)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700215{
216 struct chip_data *chip = drv_data->cur_chip;
Bryan Wu5fec5b52007-12-04 23:45:13 -0800217 int ret = 0;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700218
219 /* Clear status and disable clock */
220 write_STAT(BIT_STAT_CLR);
221 bfin_spi_disable(drv_data);
Bryan Wu88b40362007-05-21 18:32:16 +0800222 dev_dbg(&drv_data->pdev->dev, "restoring spi ctl state\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700223
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700224 /* Load the registers */
225 write_CTRL(chip->ctl_reg);
226 write_BAUD(chip->baud);
Bryan Wufad91c82007-12-04 23:45:14 -0800227 cs_active(chip);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800228
Bryan Wu5fec5b52007-12-04 23:45:13 -0800229 if (ret)
230 dev_dbg(&drv_data->pdev->dev,
231 ": request chip select number %d failed\n",
232 chip->chip_select_num);
233
234 return ret;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700235}
236
237/* used to kick off transfer in rx mode */
238static unsigned short dummy_read(void)
239{
240 unsigned short tmp;
241 tmp = read_RDBR();
242 return tmp;
243}
244
245static void null_writer(struct driver_data *drv_data)
246{
247 u8 n_bytes = drv_data->n_bytes;
248
249 while (drv_data->tx < drv_data->tx_end) {
250 write_TDBR(0);
251 while ((read_STAT() & BIT_STAT_TXS))
252 continue;
253 drv_data->tx += n_bytes;
254 }
255}
256
257static void null_reader(struct driver_data *drv_data)
258{
259 u8 n_bytes = drv_data->n_bytes;
260 dummy_read();
261
262 while (drv_data->rx < drv_data->rx_end) {
263 while (!(read_STAT() & BIT_STAT_RXS))
264 continue;
265 dummy_read();
266 drv_data->rx += n_bytes;
267 }
268}
269
270static void u8_writer(struct driver_data *drv_data)
271{
Bryan Wu131b17d2007-12-04 23:45:12 -0800272 dev_dbg(&drv_data->pdev->dev,
Bryan Wu88b40362007-05-21 18:32:16 +0800273 "cr8-s is 0x%x\n", read_STAT());
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700274 while (drv_data->tx < drv_data->tx_end) {
275 write_TDBR(*(u8 *) (drv_data->tx));
276 while (read_STAT() & BIT_STAT_TXS)
277 continue;
278 ++drv_data->tx;
279 }
280
281 /* poll for SPI completion before returning */
282 while (!(read_STAT() & BIT_STAT_SPIF))
283 continue;
284}
285
286static void u8_cs_chg_writer(struct driver_data *drv_data)
287{
288 struct chip_data *chip = drv_data->cur_chip;
289
290 while (drv_data->tx < drv_data->tx_end) {
Bryan Wufad91c82007-12-04 23:45:14 -0800291 cs_active(chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700292
293 write_TDBR(*(u8 *) (drv_data->tx));
294 while (read_STAT() & BIT_STAT_TXS)
295 continue;
296 while (!(read_STAT() & BIT_STAT_SPIF))
297 continue;
Bryan Wufad91c82007-12-04 23:45:14 -0800298 cs_deactive(chip);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800299
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700300 if (chip->cs_chg_udelay)
301 udelay(chip->cs_chg_udelay);
302 ++drv_data->tx;
303 }
Bryan Wufad91c82007-12-04 23:45:14 -0800304 cs_deactive(chip);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800305
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700306}
307
308static void u8_reader(struct driver_data *drv_data)
309{
Bryan Wu131b17d2007-12-04 23:45:12 -0800310 dev_dbg(&drv_data->pdev->dev,
Bryan Wu88b40362007-05-21 18:32:16 +0800311 "cr-8 is 0x%x\n", read_STAT());
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700312
313 /* clear TDBR buffer before read(else it will be shifted out) */
314 write_TDBR(0xFFFF);
315
316 dummy_read();
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700317 while (drv_data->rx < drv_data->rx_end - 1) {
318 while (!(read_STAT() & BIT_STAT_RXS))
319 continue;
320 *(u8 *) (drv_data->rx) = read_RDBR();
321 ++drv_data->rx;
322 }
323
324 while (!(read_STAT() & BIT_STAT_RXS))
325 continue;
326 *(u8 *) (drv_data->rx) = read_SHAW();
327 ++drv_data->rx;
328}
329
330static void u8_cs_chg_reader(struct driver_data *drv_data)
331{
332 struct chip_data *chip = drv_data->cur_chip;
333
334 while (drv_data->rx < drv_data->rx_end) {
Bryan Wufad91c82007-12-04 23:45:14 -0800335 cs_active(chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700336
337 read_RDBR(); /* kick off */
338 while (!(read_STAT() & BIT_STAT_RXS))
339 continue;
340 while (!(read_STAT() & BIT_STAT_SPIF))
341 continue;
342 *(u8 *) (drv_data->rx) = read_SHAW();
Bryan Wufad91c82007-12-04 23:45:14 -0800343 cs_deactive(chip);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800344
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700345 if (chip->cs_chg_udelay)
346 udelay(chip->cs_chg_udelay);
347 ++drv_data->rx;
348 }
Bryan Wufad91c82007-12-04 23:45:14 -0800349 cs_deactive(chip);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800350
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700351}
352
353static void u8_duplex(struct driver_data *drv_data)
354{
355 /* in duplex mode, clk is triggered by writing of TDBR */
356 while (drv_data->rx < drv_data->rx_end) {
357 write_TDBR(*(u8 *) (drv_data->tx));
358 while (!(read_STAT() & BIT_STAT_SPIF))
359 continue;
360 while (!(read_STAT() & BIT_STAT_RXS))
361 continue;
362 *(u8 *) (drv_data->rx) = read_RDBR();
363 ++drv_data->rx;
364 ++drv_data->tx;
365 }
366}
367
368static void u8_cs_chg_duplex(struct driver_data *drv_data)
369{
370 struct chip_data *chip = drv_data->cur_chip;
371
372 while (drv_data->rx < drv_data->rx_end) {
Bryan Wufad91c82007-12-04 23:45:14 -0800373 cs_active(chip);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800374
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700375
376 write_TDBR(*(u8 *) (drv_data->tx));
377 while (!(read_STAT() & BIT_STAT_SPIF))
378 continue;
379 while (!(read_STAT() & BIT_STAT_RXS))
380 continue;
381 *(u8 *) (drv_data->rx) = read_RDBR();
Bryan Wufad91c82007-12-04 23:45:14 -0800382 cs_deactive(chip);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800383
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700384 if (chip->cs_chg_udelay)
385 udelay(chip->cs_chg_udelay);
386 ++drv_data->rx;
387 ++drv_data->tx;
388 }
Bryan Wufad91c82007-12-04 23:45:14 -0800389 cs_deactive(chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700390}
391
392static void u16_writer(struct driver_data *drv_data)
393{
Bryan Wu131b17d2007-12-04 23:45:12 -0800394 dev_dbg(&drv_data->pdev->dev,
Bryan Wu88b40362007-05-21 18:32:16 +0800395 "cr16 is 0x%x\n", read_STAT());
396
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700397 while (drv_data->tx < drv_data->tx_end) {
398 write_TDBR(*(u16 *) (drv_data->tx));
399 while ((read_STAT() & BIT_STAT_TXS))
400 continue;
401 drv_data->tx += 2;
402 }
403
404 /* poll for SPI completion before returning */
405 while (!(read_STAT() & BIT_STAT_SPIF))
406 continue;
407}
408
409static void u16_cs_chg_writer(struct driver_data *drv_data)
410{
411 struct chip_data *chip = drv_data->cur_chip;
412
413 while (drv_data->tx < drv_data->tx_end) {
Bryan Wufad91c82007-12-04 23:45:14 -0800414 cs_active(chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700415
416 write_TDBR(*(u16 *) (drv_data->tx));
417 while ((read_STAT() & BIT_STAT_TXS))
418 continue;
419 while (!(read_STAT() & BIT_STAT_SPIF))
420 continue;
Bryan Wufad91c82007-12-04 23:45:14 -0800421 cs_deactive(chip);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800422
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700423 if (chip->cs_chg_udelay)
424 udelay(chip->cs_chg_udelay);
425 drv_data->tx += 2;
426 }
Bryan Wufad91c82007-12-04 23:45:14 -0800427 cs_deactive(chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700428}
429
430static void u16_reader(struct driver_data *drv_data)
431{
Bryan Wu88b40362007-05-21 18:32:16 +0800432 dev_dbg(&drv_data->pdev->dev,
433 "cr-16 is 0x%x\n", read_STAT());
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700434 dummy_read();
435
436 while (drv_data->rx < (drv_data->rx_end - 2)) {
437 while (!(read_STAT() & BIT_STAT_RXS))
438 continue;
439 *(u16 *) (drv_data->rx) = read_RDBR();
440 drv_data->rx += 2;
441 }
442
443 while (!(read_STAT() & BIT_STAT_RXS))
444 continue;
445 *(u16 *) (drv_data->rx) = read_SHAW();
446 drv_data->rx += 2;
447}
448
449static void u16_cs_chg_reader(struct driver_data *drv_data)
450{
451 struct chip_data *chip = drv_data->cur_chip;
452
453 while (drv_data->rx < drv_data->rx_end) {
Bryan Wufad91c82007-12-04 23:45:14 -0800454 cs_active(chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700455
456 read_RDBR(); /* kick off */
457 while (!(read_STAT() & BIT_STAT_RXS))
458 continue;
459 while (!(read_STAT() & BIT_STAT_SPIF))
460 continue;
461 *(u16 *) (drv_data->rx) = read_SHAW();
Bryan Wufad91c82007-12-04 23:45:14 -0800462 cs_deactive(chip);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800463
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700464 if (chip->cs_chg_udelay)
465 udelay(chip->cs_chg_udelay);
466 drv_data->rx += 2;
467 }
Bryan Wufad91c82007-12-04 23:45:14 -0800468 cs_deactive(chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700469}
470
471static void u16_duplex(struct driver_data *drv_data)
472{
473 /* in duplex mode, clk is triggered by writing of TDBR */
474 while (drv_data->tx < drv_data->tx_end) {
475 write_TDBR(*(u16 *) (drv_data->tx));
476 while (!(read_STAT() & BIT_STAT_SPIF))
477 continue;
478 while (!(read_STAT() & BIT_STAT_RXS))
479 continue;
480 *(u16 *) (drv_data->rx) = read_RDBR();
481 drv_data->rx += 2;
482 drv_data->tx += 2;
483 }
484}
485
486static void u16_cs_chg_duplex(struct driver_data *drv_data)
487{
488 struct chip_data *chip = drv_data->cur_chip;
489
490 while (drv_data->tx < drv_data->tx_end) {
Bryan Wufad91c82007-12-04 23:45:14 -0800491 cs_active(chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700492
493 write_TDBR(*(u16 *) (drv_data->tx));
494 while (!(read_STAT() & BIT_STAT_SPIF))
495 continue;
496 while (!(read_STAT() & BIT_STAT_RXS))
497 continue;
498 *(u16 *) (drv_data->rx) = read_RDBR();
Bryan Wufad91c82007-12-04 23:45:14 -0800499 cs_deactive(chip);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800500
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700501 if (chip->cs_chg_udelay)
502 udelay(chip->cs_chg_udelay);
503 drv_data->rx += 2;
504 drv_data->tx += 2;
505 }
Bryan Wufad91c82007-12-04 23:45:14 -0800506 cs_deactive(chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700507}
508
509/* test if ther is more transfer to be done */
510static void *next_transfer(struct driver_data *drv_data)
511{
512 struct spi_message *msg = drv_data->cur_msg;
513 struct spi_transfer *trans = drv_data->cur_transfer;
514
515 /* Move to next transfer */
516 if (trans->transfer_list.next != &msg->transfers) {
517 drv_data->cur_transfer =
518 list_entry(trans->transfer_list.next,
519 struct spi_transfer, transfer_list);
520 return RUNNING_STATE;
521 } else
522 return DONE_STATE;
523}
524
525/*
526 * caller already set message->status;
527 * dma and pio irqs are blocked give finished message back
528 */
529static void giveback(struct driver_data *drv_data)
530{
Bryan Wufad91c82007-12-04 23:45:14 -0800531 struct chip_data *chip = drv_data->cur_chip;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700532 struct spi_transfer *last_transfer;
533 unsigned long flags;
534 struct spi_message *msg;
535
536 spin_lock_irqsave(&drv_data->lock, flags);
537 msg = drv_data->cur_msg;
538 drv_data->cur_msg = NULL;
539 drv_data->cur_transfer = NULL;
540 drv_data->cur_chip = NULL;
541 queue_work(drv_data->workqueue, &drv_data->pump_messages);
542 spin_unlock_irqrestore(&drv_data->lock, flags);
543
544 last_transfer = list_entry(msg->transfers.prev,
545 struct spi_transfer, transfer_list);
546
547 msg->state = NULL;
548
549 /* disable chip select signal. And not stop spi in autobuffer mode */
550 if (drv_data->tx_dma != 0xFFFF) {
Bryan Wufad91c82007-12-04 23:45:14 -0800551 cs_deactive(chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700552 bfin_spi_disable(drv_data);
553 }
554
Bryan Wufad91c82007-12-04 23:45:14 -0800555 if (!drv_data->cs_change)
556 cs_deactive(chip);
557
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700558 if (msg->complete)
559 msg->complete(msg->context);
560}
561
Bryan Wu88b40362007-05-21 18:32:16 +0800562static irqreturn_t dma_irq_handler(int irq, void *dev_id)
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700563{
564 struct driver_data *drv_data = (struct driver_data *)dev_id;
565 struct spi_message *msg = drv_data->cur_msg;
Bryan Wufad91c82007-12-04 23:45:14 -0800566 struct chip_data *chip = drv_data->cur_chip;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700567
Bryan Wu88b40362007-05-21 18:32:16 +0800568 dev_dbg(&drv_data->pdev->dev, "in dma_irq_handler\n");
Bryan Wua32c6912007-12-04 23:45:15 -0800569 clear_dma_irqstat(spi_dma_ch);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700570
Bryan Wud6fe89b2007-06-11 17:34:17 +0800571 /* Wait for DMA to complete */
Bryan Wua32c6912007-12-04 23:45:15 -0800572 while (get_dma_curr_irqstat(spi_dma_ch) & DMA_RUN)
Bryan Wud6fe89b2007-06-11 17:34:17 +0800573 continue;
574
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700575 /*
Bryan Wud6fe89b2007-06-11 17:34:17 +0800576 * wait for the last transaction shifted out. HRM states:
577 * at this point there may still be data in the SPI DMA FIFO waiting
578 * to be transmitted ... software needs to poll TXS in the SPI_STAT
579 * register until it goes low for 2 successive reads
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700580 */
581 if (drv_data->tx != NULL) {
Bryan Wua32c6912007-12-04 23:45:15 -0800582 while ((read_STAT() & TXS) ||
583 (read_STAT() & TXS))
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700584 continue;
585 }
586
Bryan Wua32c6912007-12-04 23:45:15 -0800587 while (!(read_STAT() & SPIF))
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700588 continue;
589
590 bfin_spi_disable(drv_data);
591
592 msg->actual_length += drv_data->len_in_bytes;
593
Bryan Wufad91c82007-12-04 23:45:14 -0800594 if (drv_data->cs_change)
595 cs_deactive(chip);
596
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700597 /* Move to next transfer */
598 msg->state = next_transfer(drv_data);
599
600 /* Schedule transfer tasklet */
601 tasklet_schedule(&drv_data->pump_transfers);
602
603 /* free the irq handler before next transfer */
Bryan Wu88b40362007-05-21 18:32:16 +0800604 dev_dbg(&drv_data->pdev->dev,
605 "disable dma channel irq%d\n",
Bryan Wua32c6912007-12-04 23:45:15 -0800606 spi_dma_ch);
607 dma_disable_irq(spi_dma_ch);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700608
609 return IRQ_HANDLED;
610}
611
612static void pump_transfers(unsigned long data)
613{
614 struct driver_data *drv_data = (struct driver_data *)data;
615 struct spi_message *message = NULL;
616 struct spi_transfer *transfer = NULL;
617 struct spi_transfer *previous = NULL;
618 struct chip_data *chip = NULL;
Bryan Wu88b40362007-05-21 18:32:16 +0800619 u8 width;
620 u16 cr, dma_width, dma_config;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700621 u32 tranf_success = 1;
622
623 /* Get current state information */
624 message = drv_data->cur_msg;
625 transfer = drv_data->cur_transfer;
626 chip = drv_data->cur_chip;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700627 /*
628 * if msg is error or done, report it back using complete() callback
629 */
630
631 /* Handle for abort */
632 if (message->state == ERROR_STATE) {
633 message->status = -EIO;
634 giveback(drv_data);
635 return;
636 }
637
638 /* Handle end of message */
639 if (message->state == DONE_STATE) {
640 message->status = 0;
641 giveback(drv_data);
642 return;
643 }
644
645 /* Delay if requested at end of transfer */
646 if (message->state == RUNNING_STATE) {
647 previous = list_entry(transfer->transfer_list.prev,
648 struct spi_transfer, transfer_list);
649 if (previous->delay_usecs)
650 udelay(previous->delay_usecs);
651 }
652
653 /* Setup the transfer state based on the type of transfer */
654 if (flush(drv_data) == 0) {
655 dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
656 message->status = -EIO;
657 giveback(drv_data);
658 return;
659 }
660
661 if (transfer->tx_buf != NULL) {
662 drv_data->tx = (void *)transfer->tx_buf;
663 drv_data->tx_end = drv_data->tx + transfer->len;
Bryan Wu88b40362007-05-21 18:32:16 +0800664 dev_dbg(&drv_data->pdev->dev, "tx_buf is %p, tx_end is %p\n",
665 transfer->tx_buf, drv_data->tx_end);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700666 } else {
667 drv_data->tx = NULL;
668 }
669
670 if (transfer->rx_buf != NULL) {
671 drv_data->rx = transfer->rx_buf;
672 drv_data->rx_end = drv_data->rx + transfer->len;
Bryan Wu88b40362007-05-21 18:32:16 +0800673 dev_dbg(&drv_data->pdev->dev, "rx_buf is %p, rx_end is %p\n",
674 transfer->rx_buf, drv_data->rx_end);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700675 } else {
676 drv_data->rx = NULL;
677 }
678
679 drv_data->rx_dma = transfer->rx_dma;
680 drv_data->tx_dma = transfer->tx_dma;
681 drv_data->len_in_bytes = transfer->len;
Bryan Wufad91c82007-12-04 23:45:14 -0800682 drv_data->cs_change = transfer->cs_change;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700683
684 width = chip->width;
685 if (width == CFG_SPI_WORDSIZE16) {
686 drv_data->len = (transfer->len) >> 1;
687 } else {
688 drv_data->len = transfer->len;
689 }
690 drv_data->write = drv_data->tx ? chip->write : null_writer;
691 drv_data->read = drv_data->rx ? chip->read : null_reader;
692 drv_data->duplex = chip->duplex ? chip->duplex : null_writer;
Bryan Wu131b17d2007-12-04 23:45:12 -0800693 dev_dbg(&drv_data->pdev->dev, "transfer: ",
694 "drv_data->write is %p, chip->write is %p, null_wr is %p\n",
695 drv_data->write, chip->write, null_writer);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700696
697 /* speed and width has been set on per message */
698 message->state = RUNNING_STATE;
699 dma_config = 0;
700
701 /* restore spi status for each spi transfer */
702 if (transfer->speed_hz) {
703 write_BAUD(hz_to_spi_baud(transfer->speed_hz));
704 } else {
705 write_BAUD(chip->baud);
706 }
Bryan Wufad91c82007-12-04 23:45:14 -0800707 cs_active(chip);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700708
Bryan Wu88b40362007-05-21 18:32:16 +0800709 dev_dbg(&drv_data->pdev->dev,
710 "now pumping a transfer: width is %d, len is %d\n",
711 width, transfer->len);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700712
713 /*
714 * Try to map dma buffer and do a dma transfer if
715 * successful use different way to r/w according to
716 * drv_data->cur_chip->enable_dma
717 */
718 if (drv_data->cur_chip->enable_dma && drv_data->len > 6) {
719
720 write_STAT(BIT_STAT_CLR);
Bryan Wua32c6912007-12-04 23:45:15 -0800721 disable_dma(spi_dma_ch);
722 clear_dma_irqstat(spi_dma_ch);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700723 bfin_spi_disable(drv_data);
724
725 /* config dma channel */
Bryan Wu88b40362007-05-21 18:32:16 +0800726 dev_dbg(&drv_data->pdev->dev, "doing dma transfer\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700727 if (width == CFG_SPI_WORDSIZE16) {
Bryan Wua32c6912007-12-04 23:45:15 -0800728 set_dma_x_count(spi_dma_ch, drv_data->len);
729 set_dma_x_modify(spi_dma_ch, 2);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700730 dma_width = WDSIZE_16;
731 } else {
Bryan Wua32c6912007-12-04 23:45:15 -0800732 set_dma_x_count(spi_dma_ch, drv_data->len);
733 set_dma_x_modify(spi_dma_ch, 1);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700734 dma_width = WDSIZE_8;
735 }
736
737 /* set transfer width,direction. And enable spi */
738 cr = (read_CTRL() & (~BIT_CTL_TIMOD));
739
740 /* dirty hack for autobuffer DMA mode */
741 if (drv_data->tx_dma == 0xFFFF) {
Bryan Wu88b40362007-05-21 18:32:16 +0800742 dev_dbg(&drv_data->pdev->dev,
743 "doing autobuffer DMA out.\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700744
745 /* no irq in autobuffer mode */
746 dma_config =
747 (DMAFLOW_AUTO | RESTART | dma_width | DI_EN);
Bryan Wua32c6912007-12-04 23:45:15 -0800748 set_dma_config(spi_dma_ch, dma_config);
749 set_dma_start_addr(spi_dma_ch,
750 (unsigned long)drv_data->tx);
751 enable_dma(spi_dma_ch);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700752 write_CTRL(cr | CFG_SPI_DMAWRITE | (width << 8) |
753 (CFG_SPI_ENABLE << 14));
754
755 /* just return here, there can only be one transfer in this mode */
756 message->status = 0;
757 giveback(drv_data);
758 return;
759 }
760
761 /* In dma mode, rx or tx must be NULL in one transfer */
762 if (drv_data->rx != NULL) {
763 /* set transfer mode, and enable SPI */
Bryan Wu88b40362007-05-21 18:32:16 +0800764 dev_dbg(&drv_data->pdev->dev, "doing DMA in.\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700765
766 /* disable SPI before write to TDBR */
767 write_CTRL(cr & ~BIT_CTL_ENABLE);
768
769 /* clear tx reg soformer data is not shifted out */
770 write_TDBR(0xFF);
771
Bryan Wua32c6912007-12-04 23:45:15 -0800772 set_dma_x_count(spi_dma_ch, drv_data->len);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700773
774 /* start dma */
Bryan Wua32c6912007-12-04 23:45:15 -0800775 dma_enable_irq(spi_dma_ch);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700776 dma_config = (WNR | RESTART | dma_width | DI_EN);
Bryan Wua32c6912007-12-04 23:45:15 -0800777 set_dma_config(spi_dma_ch, dma_config);
778 set_dma_start_addr(spi_dma_ch,
779 (unsigned long)drv_data->rx);
780 enable_dma(spi_dma_ch);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700781
782 cr |=
783 CFG_SPI_DMAREAD | (width << 8) | (CFG_SPI_ENABLE <<
784 14);
785 /* set transfer mode, and enable SPI */
786 write_CTRL(cr);
787 } else if (drv_data->tx != NULL) {
Bryan Wu88b40362007-05-21 18:32:16 +0800788 dev_dbg(&drv_data->pdev->dev, "doing DMA out.\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700789
790 /* start dma */
Bryan Wua32c6912007-12-04 23:45:15 -0800791 dma_enable_irq(spi_dma_ch);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700792 dma_config = (RESTART | dma_width | DI_EN);
Bryan Wua32c6912007-12-04 23:45:15 -0800793 set_dma_config(spi_dma_ch, dma_config);
794 set_dma_start_addr(spi_dma_ch,
795 (unsigned long)drv_data->tx);
796 enable_dma(spi_dma_ch);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700797
798 write_CTRL(cr | CFG_SPI_DMAWRITE | (width << 8) |
799 (CFG_SPI_ENABLE << 14));
800
801 }
802 } else {
803 /* IO mode write then read */
Bryan Wu88b40362007-05-21 18:32:16 +0800804 dev_dbg(&drv_data->pdev->dev, "doing IO transfer\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700805
806 write_STAT(BIT_STAT_CLR);
807
808 if (drv_data->tx != NULL && drv_data->rx != NULL) {
809 /* full duplex mode */
810 BUG_ON((drv_data->tx_end - drv_data->tx) !=
811 (drv_data->rx_end - drv_data->rx));
Bryan Wu131b17d2007-12-04 23:45:12 -0800812 cr = (read_CTRL() & (~BIT_CTL_TIMOD));
Bryan Wu88b40362007-05-21 18:32:16 +0800813 cr |= CFG_SPI_WRITE | (width << 8) |
814 (CFG_SPI_ENABLE << 14);
815 dev_dbg(&drv_data->pdev->dev,
816 "IO duplex: cr is 0x%x\n", cr);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700817
818 write_CTRL(cr);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700819
820 drv_data->duplex(drv_data);
821
822 if (drv_data->tx != drv_data->tx_end)
823 tranf_success = 0;
824 } else if (drv_data->tx != NULL) {
825 /* write only half duplex */
Bryan Wu88b40362007-05-21 18:32:16 +0800826 cr = (read_CTRL() & (~BIT_CTL_TIMOD));
827 cr |= CFG_SPI_WRITE | (width << 8) |
828 (CFG_SPI_ENABLE << 14);
Bryan Wu131b17d2007-12-04 23:45:12 -0800829 dev_dbg(&drv_data->pdev->dev,
Bryan Wu88b40362007-05-21 18:32:16 +0800830 "IO write: cr is 0x%x\n", cr);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700831
832 write_CTRL(cr);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700833
834 drv_data->write(drv_data);
835
836 if (drv_data->tx != drv_data->tx_end)
837 tranf_success = 0;
838 } else if (drv_data->rx != NULL) {
839 /* read only half duplex */
Bryan Wu88b40362007-05-21 18:32:16 +0800840 cr = (read_CTRL() & (~BIT_CTL_TIMOD));
841 cr |= CFG_SPI_READ | (width << 8) |
842 (CFG_SPI_ENABLE << 14);
Bryan Wu131b17d2007-12-04 23:45:12 -0800843 dev_dbg(&drv_data->pdev->dev,
Bryan Wu88b40362007-05-21 18:32:16 +0800844 "IO read: cr is 0x%x\n", cr);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700845
846 write_CTRL(cr);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700847
848 drv_data->read(drv_data);
849 if (drv_data->rx != drv_data->rx_end)
850 tranf_success = 0;
851 }
852
853 if (!tranf_success) {
Bryan Wu131b17d2007-12-04 23:45:12 -0800854 dev_dbg(&drv_data->pdev->dev,
Bryan Wu88b40362007-05-21 18:32:16 +0800855 "IO write error!\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700856 message->state = ERROR_STATE;
857 } else {
858 /* Update total byte transfered */
859 message->actual_length += drv_data->len;
860
Bryan Wufad91c82007-12-04 23:45:14 -0800861 if (drv_data->cs_change)
862 cs_deactive(chip);
863
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700864 /* Move to next transfer of this msg */
865 message->state = next_transfer(drv_data);
866 }
867
868 /* Schedule next transfer tasklet */
869 tasklet_schedule(&drv_data->pump_transfers);
870
871 }
872}
873
874/* pop a msg from queue and kick off real transfer */
875static void pump_messages(struct work_struct *work)
876{
Bryan Wu131b17d2007-12-04 23:45:12 -0800877 struct driver_data *drv_data;
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700878 unsigned long flags;
879
Bryan Wu131b17d2007-12-04 23:45:12 -0800880 drv_data = container_of(work, struct driver_data, pump_messages);
881
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700882 /* Lock queue and check for queue work */
883 spin_lock_irqsave(&drv_data->lock, flags);
884 if (list_empty(&drv_data->queue) || drv_data->run == QUEUE_STOPPED) {
885 /* pumper kicked off but no work to do */
886 drv_data->busy = 0;
887 spin_unlock_irqrestore(&drv_data->lock, flags);
888 return;
889 }
890
891 /* Make sure we are not already running a message */
892 if (drv_data->cur_msg) {
893 spin_unlock_irqrestore(&drv_data->lock, flags);
894 return;
895 }
896
897 /* Extract head of queue */
898 drv_data->cur_msg = list_entry(drv_data->queue.next,
899 struct spi_message, queue);
Bryan Wu5fec5b52007-12-04 23:45:13 -0800900
901 /* Setup the SSP using the per chip configuration */
902 drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
903 if (restore_state(drv_data)) {
904 spin_unlock_irqrestore(&drv_data->lock, flags);
905 return;
906 };
907
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700908 list_del_init(&drv_data->cur_msg->queue);
909
910 /* Initial message state */
911 drv_data->cur_msg->state = START_STATE;
912 drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
913 struct spi_transfer, transfer_list);
914
Bryan Wu5fec5b52007-12-04 23:45:13 -0800915 dev_dbg(&drv_data->pdev->dev, "got a message to pump, "
916 "state is set to: baud %d, flag 0x%x, ctl 0x%x\n",
917 drv_data->cur_chip->baud, drv_data->cur_chip->flag,
918 drv_data->cur_chip->ctl_reg);
Bryan Wu131b17d2007-12-04 23:45:12 -0800919
920 dev_dbg(&drv_data->pdev->dev,
Bryan Wu88b40362007-05-21 18:32:16 +0800921 "the first transfer len is %d\n",
922 drv_data->cur_transfer->len);
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700923
924 /* Mark as busy and launch transfers */
925 tasklet_schedule(&drv_data->pump_transfers);
926
927 drv_data->busy = 1;
928 spin_unlock_irqrestore(&drv_data->lock, flags);
929}
930
931/*
932 * got a msg to transfer, queue it in drv_data->queue.
933 * And kick off message pumper
934 */
935static int transfer(struct spi_device *spi, struct spi_message *msg)
936{
937 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
938 unsigned long flags;
939
940 spin_lock_irqsave(&drv_data->lock, flags);
941
942 if (drv_data->run == QUEUE_STOPPED) {
943 spin_unlock_irqrestore(&drv_data->lock, flags);
944 return -ESHUTDOWN;
945 }
946
947 msg->actual_length = 0;
948 msg->status = -EINPROGRESS;
949 msg->state = START_STATE;
950
Bryan Wu88b40362007-05-21 18:32:16 +0800951 dev_dbg(&spi->dev, "adding an msg in transfer() \n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700952 list_add_tail(&msg->queue, &drv_data->queue);
953
954 if (drv_data->run == QUEUE_RUNNING && !drv_data->busy)
955 queue_work(drv_data->workqueue, &drv_data->pump_messages);
956
957 spin_unlock_irqrestore(&drv_data->lock, flags);
958
959 return 0;
960}
961
Sonic Zhang12e17c42007-12-04 23:45:16 -0800962#define MAX_SPI_SSEL 7
963
964static u16 ssel[3][MAX_SPI_SSEL] = {
965 {P_SPI0_SSEL1, P_SPI0_SSEL2, P_SPI0_SSEL3,
966 P_SPI0_SSEL4, P_SPI0_SSEL5,
967 P_SPI0_SSEL6, P_SPI0_SSEL7},
968
969 {P_SPI1_SSEL1, P_SPI1_SSEL2, P_SPI1_SSEL3,
970 P_SPI1_SSEL4, P_SPI1_SSEL5,
971 P_SPI1_SSEL6, P_SPI1_SSEL7},
972
973 {P_SPI2_SSEL1, P_SPI2_SSEL2, P_SPI2_SSEL3,
974 P_SPI2_SSEL4, P_SPI2_SSEL5,
975 P_SPI2_SSEL6, P_SPI2_SSEL7},
976};
977
Wu, Bryana5f6abd2007-05-06 14:50:34 -0700978/* first setup for new devices */
979static int setup(struct spi_device *spi)
980{
981 struct bfin5xx_spi_chip *chip_info = NULL;
982 struct chip_data *chip;
983 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
984 u8 spi_flg;
985
986 /* Abort device setup if requested features are not supported */
987 if (spi->mode & ~(SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST)) {
988 dev_err(&spi->dev, "requested mode not fully supported\n");
989 return -EINVAL;
990 }
991
992 /* Zero (the default) here means 8 bits */
993 if (!spi->bits_per_word)
994 spi->bits_per_word = 8;
995
996 if (spi->bits_per_word != 8 && spi->bits_per_word != 16)
997 return -EINVAL;
998
999 /* Only alloc (or use chip_info) on first setup */
1000 chip = spi_get_ctldata(spi);
1001 if (chip == NULL) {
1002 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
1003 if (!chip)
1004 return -ENOMEM;
1005
1006 chip->enable_dma = 0;
1007 chip_info = spi->controller_data;
1008 }
1009
1010 /* chip_info isn't always needed */
1011 if (chip_info) {
Mike Frysinger2ed35512007-12-04 23:45:14 -08001012 /* Make sure people stop trying to set fields via ctl_reg
1013 * when they should actually be using common SPI framework.
1014 * Currently we let through: WOM EMISO PSSE GM SZ TIMOD.
1015 * Not sure if a user actually needs/uses any of these,
1016 * but let's assume (for now) they do.
1017 */
1018 if (chip_info->ctl_reg & (SPE|MSTR|CPOL|CPHA|LSBF|SIZE)) {
1019 dev_err(&spi->dev, "do not set bits in ctl_reg "
1020 "that the SPI framework manages\n");
1021 return -EINVAL;
1022 }
1023
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001024 chip->enable_dma = chip_info->enable_dma != 0
1025 && drv_data->master_info->enable_dma;
1026 chip->ctl_reg = chip_info->ctl_reg;
1027 chip->bits_per_word = chip_info->bits_per_word;
1028 chip->cs_change_per_word = chip_info->cs_change_per_word;
1029 chip->cs_chg_udelay = chip_info->cs_chg_udelay;
1030 }
1031
1032 /* translate common spi framework into our register */
1033 if (spi->mode & SPI_CPOL)
1034 chip->ctl_reg |= CPOL;
1035 if (spi->mode & SPI_CPHA)
1036 chip->ctl_reg |= CPHA;
1037 if (spi->mode & SPI_LSB_FIRST)
1038 chip->ctl_reg |= LSBF;
1039 /* we dont support running in slave mode (yet?) */
1040 chip->ctl_reg |= MSTR;
1041
1042 /*
1043 * if any one SPI chip is registered and wants DMA, request the
1044 * DMA channel for it
1045 */
1046 if (chip->enable_dma && !dma_requested) {
1047 /* register dma irq handler */
Bryan Wua32c6912007-12-04 23:45:15 -08001048 if (request_dma(spi_dma_ch, "BF53x_SPI_DMA") < 0) {
Bryan Wu88b40362007-05-21 18:32:16 +08001049 dev_dbg(&spi->dev,
1050 "Unable to request BlackFin SPI DMA channel\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001051 return -ENODEV;
1052 }
Bryan Wua32c6912007-12-04 23:45:15 -08001053 if (set_dma_callback(spi_dma_ch, (void *)dma_irq_handler,
1054 drv_data) < 0) {
Bryan Wu88b40362007-05-21 18:32:16 +08001055 dev_dbg(&spi->dev, "Unable to set dma callback\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001056 return -EPERM;
1057 }
Bryan Wua32c6912007-12-04 23:45:15 -08001058 dma_disable_irq(spi_dma_ch);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001059 dma_requested = 1;
1060 }
1061
1062 /*
1063 * Notice: for blackfin, the speed_hz is the value of register
1064 * SPI_BAUD, not the real baudrate
1065 */
1066 chip->baud = hz_to_spi_baud(spi->max_speed_hz);
1067 spi_flg = ~(1 << (spi->chip_select));
1068 chip->flag = ((u16) spi_flg << 8) | (1 << (spi->chip_select));
1069 chip->chip_select_num = spi->chip_select;
1070
1071 switch (chip->bits_per_word) {
1072 case 8:
1073 chip->n_bytes = 1;
1074 chip->width = CFG_SPI_WORDSIZE8;
1075 chip->read = chip->cs_change_per_word ?
1076 u8_cs_chg_reader : u8_reader;
1077 chip->write = chip->cs_change_per_word ?
1078 u8_cs_chg_writer : u8_writer;
1079 chip->duplex = chip->cs_change_per_word ?
1080 u8_cs_chg_duplex : u8_duplex;
1081 break;
1082
1083 case 16:
1084 chip->n_bytes = 2;
1085 chip->width = CFG_SPI_WORDSIZE16;
1086 chip->read = chip->cs_change_per_word ?
1087 u16_cs_chg_reader : u16_reader;
1088 chip->write = chip->cs_change_per_word ?
1089 u16_cs_chg_writer : u16_writer;
1090 chip->duplex = chip->cs_change_per_word ?
1091 u16_cs_chg_duplex : u16_duplex;
1092 break;
1093
1094 default:
1095 dev_err(&spi->dev, "%d bits_per_word is not supported\n",
1096 chip->bits_per_word);
1097 kfree(chip);
1098 return -ENODEV;
1099 }
1100
Joe Perches898eb712007-10-18 03:06:30 -07001101 dev_dbg(&spi->dev, "setup spi chip %s, width is %d, dma is %d\n",
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001102 spi->modalias, chip->width, chip->enable_dma);
Bryan Wu88b40362007-05-21 18:32:16 +08001103 dev_dbg(&spi->dev, "ctl_reg is 0x%x, flag_reg is 0x%x\n",
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001104 chip->ctl_reg, chip->flag);
1105
1106 spi_set_ctldata(spi, chip);
1107
Sonic Zhang12e17c42007-12-04 23:45:16 -08001108 dev_dbg(&spi->dev, "chip select number is %d\n", chip->chip_select_num);
1109 if ((chip->chip_select_num > 0)
1110 && (chip->chip_select_num <= spi->master->num_chipselect))
1111 peripheral_request(ssel[spi->master->bus_num]
1112 [chip->chip_select_num-1], DRV_NAME);
1113
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001114 return 0;
1115}
1116
1117/*
1118 * callback for spi framework.
1119 * clean driver specific data
1120 */
Bryan Wu88b40362007-05-21 18:32:16 +08001121static void cleanup(struct spi_device *spi)
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001122{
Mike Frysinger27bb9e72007-06-11 15:31:30 +08001123 struct chip_data *chip = spi_get_ctldata(spi);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001124
Sonic Zhang12e17c42007-12-04 23:45:16 -08001125 if ((chip->chip_select_num > 0)
1126 && (chip->chip_select_num <= spi->master->num_chipselect))
1127 peripheral_free(ssel[spi->master->bus_num]
1128 [chip->chip_select_num-1]);
1129
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001130 kfree(chip);
1131}
1132
1133static inline int init_queue(struct driver_data *drv_data)
1134{
1135 INIT_LIST_HEAD(&drv_data->queue);
1136 spin_lock_init(&drv_data->lock);
1137
1138 drv_data->run = QUEUE_STOPPED;
1139 drv_data->busy = 0;
1140
1141 /* init transfer tasklet */
1142 tasklet_init(&drv_data->pump_transfers,
1143 pump_transfers, (unsigned long)drv_data);
1144
1145 /* init messages workqueue */
1146 INIT_WORK(&drv_data->pump_messages, pump_messages);
1147 drv_data->workqueue =
Tony Jones49dce682007-10-16 01:27:48 -07001148 create_singlethread_workqueue(drv_data->master->dev.parent->bus_id);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001149 if (drv_data->workqueue == NULL)
1150 return -EBUSY;
1151
1152 return 0;
1153}
1154
1155static inline int start_queue(struct driver_data *drv_data)
1156{
1157 unsigned long flags;
1158
1159 spin_lock_irqsave(&drv_data->lock, flags);
1160
1161 if (drv_data->run == QUEUE_RUNNING || drv_data->busy) {
1162 spin_unlock_irqrestore(&drv_data->lock, flags);
1163 return -EBUSY;
1164 }
1165
1166 drv_data->run = QUEUE_RUNNING;
1167 drv_data->cur_msg = NULL;
1168 drv_data->cur_transfer = NULL;
1169 drv_data->cur_chip = NULL;
1170 spin_unlock_irqrestore(&drv_data->lock, flags);
1171
1172 queue_work(drv_data->workqueue, &drv_data->pump_messages);
1173
1174 return 0;
1175}
1176
1177static inline int stop_queue(struct driver_data *drv_data)
1178{
1179 unsigned long flags;
1180 unsigned limit = 500;
1181 int status = 0;
1182
1183 spin_lock_irqsave(&drv_data->lock, flags);
1184
1185 /*
1186 * This is a bit lame, but is optimized for the common execution path.
1187 * A wait_queue on the drv_data->busy could be used, but then the common
1188 * execution path (pump_messages) would be required to call wake_up or
1189 * friends on every SPI message. Do this instead
1190 */
1191 drv_data->run = QUEUE_STOPPED;
1192 while (!list_empty(&drv_data->queue) && drv_data->busy && limit--) {
1193 spin_unlock_irqrestore(&drv_data->lock, flags);
1194 msleep(10);
1195 spin_lock_irqsave(&drv_data->lock, flags);
1196 }
1197
1198 if (!list_empty(&drv_data->queue) || drv_data->busy)
1199 status = -EBUSY;
1200
1201 spin_unlock_irqrestore(&drv_data->lock, flags);
1202
1203 return status;
1204}
1205
1206static inline int destroy_queue(struct driver_data *drv_data)
1207{
1208 int status;
1209
1210 status = stop_queue(drv_data);
1211 if (status != 0)
1212 return status;
1213
1214 destroy_workqueue(drv_data->workqueue);
1215
1216 return 0;
1217}
1218
Sonic Zhang7c4ef092007-12-04 23:45:16 -08001219static int setup_pin_mux(int action, int bus_num)
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001220{
1221
Sonic Zhang7c4ef092007-12-04 23:45:16 -08001222 u16 pin_req[3][4] = {
1223 {P_SPI0_SCK, P_SPI0_MISO, P_SPI0_MOSI, 0},
1224 {P_SPI1_SCK, P_SPI1_MISO, P_SPI1_MOSI, 0},
1225 {P_SPI2_SCK, P_SPI2_MISO, P_SPI2_MOSI, 0},
1226 };
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001227
1228 if (action) {
Sonic Zhang7c4ef092007-12-04 23:45:16 -08001229 if (peripheral_request_list(pin_req[bus_num], DRV_NAME))
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001230 return -EFAULT;
1231 } else {
Sonic Zhang7c4ef092007-12-04 23:45:16 -08001232 peripheral_free_list(pin_req[bus_num]);
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001233 }
1234
1235 return 0;
1236}
1237
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001238static int __init bfin5xx_spi_probe(struct platform_device *pdev)
1239{
1240 struct device *dev = &pdev->dev;
1241 struct bfin5xx_spi_master *platform_info;
1242 struct spi_master *master;
1243 struct driver_data *drv_data = 0;
Bryan Wua32c6912007-12-04 23:45:15 -08001244 struct resource *res;
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001245 int status = 0;
1246
1247 platform_info = dev->platform_data;
1248
1249 /* Allocate master with space for drv_data */
1250 master = spi_alloc_master(dev, sizeof(struct driver_data) + 16);
1251 if (!master) {
1252 dev_err(&pdev->dev, "can not alloc spi_master\n");
1253 return -ENOMEM;
1254 }
Bryan Wu131b17d2007-12-04 23:45:12 -08001255
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001256 drv_data = spi_master_get_devdata(master);
1257 drv_data->master = master;
1258 drv_data->master_info = platform_info;
1259 drv_data->pdev = pdev;
1260
1261 master->bus_num = pdev->id;
1262 master->num_chipselect = platform_info->num_chipselect;
1263 master->cleanup = cleanup;
1264 master->setup = setup;
1265 master->transfer = transfer;
1266
Bryan Wua32c6912007-12-04 23:45:15 -08001267 /* Find and map our resources */
1268 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1269 if (res == NULL) {
1270 dev_err(dev, "Cannot get IORESOURCE_MEM\n");
1271 status = -ENOENT;
1272 goto out_error_get_res;
1273 }
1274
1275 spi_regs_base = (u32) ioremap(res->start, (res->end - res->start)+1);
1276 if (!spi_regs_base) {
1277 dev_err(dev, "Cannot map IO\n");
1278 status = -ENXIO;
1279 goto out_error_ioremap;
1280 }
1281
1282 spi_dma_ch = platform_get_irq(pdev, 0);
1283 if (spi_dma_ch < 0) {
1284 dev_err(dev, "No DMA channel specified\n");
1285 status = -ENOENT;
1286 goto out_error_no_dma_ch;
1287 }
1288
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001289 /* Initial and start queue */
1290 status = init_queue(drv_data);
1291 if (status != 0) {
Bryan Wua32c6912007-12-04 23:45:15 -08001292 dev_err(dev, "problem initializing queue\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001293 goto out_error_queue_alloc;
1294 }
Bryan Wua32c6912007-12-04 23:45:15 -08001295
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001296 status = start_queue(drv_data);
1297 if (status != 0) {
Bryan Wua32c6912007-12-04 23:45:15 -08001298 dev_err(dev, "problem starting queue\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001299 goto out_error_queue_alloc;
1300 }
1301
1302 /* Register with the SPI framework */
1303 platform_set_drvdata(pdev, drv_data);
1304 status = spi_register_master(master);
1305 if (status != 0) {
Bryan Wua32c6912007-12-04 23:45:15 -08001306 dev_err(dev, "problem registering spi master\n");
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001307 goto out_error_queue_alloc;
1308 }
Bryan Wua32c6912007-12-04 23:45:15 -08001309
Sonic Zhang7c4ef092007-12-04 23:45:16 -08001310 if (setup_pin_mux(1, master->bus_num)) {
1311 dev_err(&pdev->dev, ": Requesting Peripherals failed\n");
1312 goto out_error;
1313 }
1314
Bryan Wua32c6912007-12-04 23:45:15 -08001315 dev_info(dev, "%s, Version %s, regs_base @ 0x%08x\n",
1316 DRV_DESC, DRV_VERSION, spi_regs_base);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001317 return status;
1318
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001319out_error_queue_alloc:
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001320 destroy_queue(drv_data);
Bryan Wua32c6912007-12-04 23:45:15 -08001321out_error_no_dma_ch:
1322 iounmap((void *) spi_regs_base);
1323out_error_ioremap:
1324out_error_get_res:
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001325out_error:
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001326 spi_master_put(master);
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001327
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001328 return status;
1329}
1330
1331/* stop hardware and remove the driver */
1332static int __devexit bfin5xx_spi_remove(struct platform_device *pdev)
1333{
1334 struct driver_data *drv_data = platform_get_drvdata(pdev);
1335 int status = 0;
1336
1337 if (!drv_data)
1338 return 0;
1339
1340 /* Remove the queue */
1341 status = destroy_queue(drv_data);
1342 if (status != 0)
1343 return status;
1344
1345 /* Disable the SSP at the peripheral and SOC level */
1346 bfin_spi_disable(drv_data);
1347
1348 /* Release DMA */
1349 if (drv_data->master_info->enable_dma) {
Bryan Wua32c6912007-12-04 23:45:15 -08001350 if (dma_channel_active(spi_dma_ch))
1351 free_dma(spi_dma_ch);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001352 }
1353
1354 /* Disconnect from the SPI framework */
1355 spi_unregister_master(drv_data->master);
1356
Sonic Zhang7c4ef092007-12-04 23:45:16 -08001357 setup_pin_mux(0, drv_data->master->bus_num);
Michael Hennerichcc2f81a2007-12-04 23:45:13 -08001358
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001359 /* Prevent double remove */
1360 platform_set_drvdata(pdev, NULL);
1361
1362 return 0;
1363}
1364
1365#ifdef CONFIG_PM
1366static int bfin5xx_spi_suspend(struct platform_device *pdev, pm_message_t state)
1367{
1368 struct driver_data *drv_data = platform_get_drvdata(pdev);
1369 int status = 0;
1370
1371 status = stop_queue(drv_data);
1372 if (status != 0)
1373 return status;
1374
1375 /* stop hardware */
1376 bfin_spi_disable(drv_data);
1377
1378 return 0;
1379}
1380
1381static int bfin5xx_spi_resume(struct platform_device *pdev)
1382{
1383 struct driver_data *drv_data = platform_get_drvdata(pdev);
1384 int status = 0;
1385
1386 /* Enable the SPI interface */
1387 bfin_spi_enable(drv_data);
1388
1389 /* Start the queue running */
1390 status = start_queue(drv_data);
1391 if (status != 0) {
1392 dev_err(&pdev->dev, "problem starting queue (%d)\n", status);
1393 return status;
1394 }
1395
1396 return 0;
1397}
1398#else
1399#define bfin5xx_spi_suspend NULL
1400#define bfin5xx_spi_resume NULL
1401#endif /* CONFIG_PM */
1402
David Brownellfc3ba952007-08-30 23:56:24 -07001403MODULE_ALIAS("bfin-spi-master"); /* for platform bus hotplug */
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001404static struct platform_driver bfin5xx_spi_driver = {
David Brownellfc3ba952007-08-30 23:56:24 -07001405 .driver = {
Bryan Wua32c6912007-12-04 23:45:15 -08001406 .name = DRV_NAME,
Bryan Wu88b40362007-05-21 18:32:16 +08001407 .owner = THIS_MODULE,
1408 },
1409 .suspend = bfin5xx_spi_suspend,
1410 .resume = bfin5xx_spi_resume,
1411 .remove = __devexit_p(bfin5xx_spi_remove),
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001412};
1413
1414static int __init bfin5xx_spi_init(void)
1415{
Bryan Wu88b40362007-05-21 18:32:16 +08001416 return platform_driver_probe(&bfin5xx_spi_driver, bfin5xx_spi_probe);
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001417}
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001418module_init(bfin5xx_spi_init);
1419
1420static void __exit bfin5xx_spi_exit(void)
1421{
1422 platform_driver_unregister(&bfin5xx_spi_driver);
1423}
Wu, Bryana5f6abd2007-05-06 14:50:34 -07001424module_exit(bfin5xx_spi_exit);