aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/spi
diff options
context:
space:
mode:
authorGrant Likely <grant.likely@secretlab.ca>2010-10-18 11:25:09 -0400
committerGrant Likely <grant.likely@secretlab.ca>2010-10-18 11:25:09 -0400
commitdb181a8ee158fd0ccea2e2670c4f2d36af2814a0 (patch)
treed03adc3926b4aca7ee172d825b90fe965b4f01b9 /drivers/spi
parentee2007d299ad4020115b193858817e6c57e95db5 (diff)
parent9c0a788b4315b83f6138ffa15c56ccf541106e58 (diff)
Merge branch 'for-spi' of git://git.kernel.org/pub/scm/linux/kernel/git/vapier/blackfin into spi/next
Diffstat (limited to 'drivers/spi')
-rw-r--r--drivers/spi/spi_bfin5xx.c844
1 files changed, 437 insertions, 407 deletions
diff --git a/drivers/spi/spi_bfin5xx.c b/drivers/spi/spi_bfin5xx.c
index 10a6dc3d37ac..ab483a0ec6d0 100644
--- a/drivers/spi/spi_bfin5xx.c
+++ b/drivers/spi/spi_bfin5xx.c
@@ -1,7 +1,7 @@
1/* 1/*
2 * Blackfin On-Chip SPI Driver 2 * Blackfin On-Chip SPI Driver
3 * 3 *
4 * Copyright 2004-2007 Analog Devices Inc. 4 * Copyright 2004-2010 Analog Devices Inc.
5 * 5 *
6 * Enter bugs at http://blackfin.uclinux.org/ 6 * Enter bugs at http://blackfin.uclinux.org/
7 * 7 *
@@ -41,13 +41,16 @@ MODULE_LICENSE("GPL");
41#define RUNNING_STATE ((void *)1) 41#define RUNNING_STATE ((void *)1)
42#define DONE_STATE ((void *)2) 42#define DONE_STATE ((void *)2)
43#define ERROR_STATE ((void *)-1) 43#define ERROR_STATE ((void *)-1)
44#define QUEUE_RUNNING 0
45#define QUEUE_STOPPED 1
46 44
47/* Value to send if no TX value is supplied */ 45struct bfin_spi_master_data;
48#define SPI_IDLE_TXVAL 0x0000
49 46
50struct driver_data { 47struct bfin_spi_transfer_ops {
48 void (*write) (struct bfin_spi_master_data *);
49 void (*read) (struct bfin_spi_master_data *);
50 void (*duplex) (struct bfin_spi_master_data *);
51};
52
53struct bfin_spi_master_data {
51 /* Driver model hookup */ 54 /* Driver model hookup */
52 struct platform_device *pdev; 55 struct platform_device *pdev;
53 56
@@ -69,7 +72,7 @@ struct driver_data {
69 spinlock_t lock; 72 spinlock_t lock;
70 struct list_head queue; 73 struct list_head queue;
71 int busy; 74 int busy;
72 int run; 75 bool running;
73 76
74 /* Message Transfer pump */ 77 /* Message Transfer pump */
75 struct tasklet_struct pump_transfers; 78 struct tasklet_struct pump_transfers;
@@ -77,7 +80,7 @@ struct driver_data {
77 /* Current message transfer state info */ 80 /* Current message transfer state info */
78 struct spi_message *cur_msg; 81 struct spi_message *cur_msg;
79 struct spi_transfer *cur_transfer; 82 struct spi_transfer *cur_transfer;
80 struct chip_data *cur_chip; 83 struct bfin_spi_slave_data *cur_chip;
81 size_t len_in_bytes; 84 size_t len_in_bytes;
82 size_t len; 85 size_t len;
83 void *tx; 86 void *tx;
@@ -92,38 +95,37 @@ struct driver_data {
92 dma_addr_t rx_dma; 95 dma_addr_t rx_dma;
93 dma_addr_t tx_dma; 96 dma_addr_t tx_dma;
94 97
98 int irq_requested;
99 int spi_irq;
100
95 size_t rx_map_len; 101 size_t rx_map_len;
96 size_t tx_map_len; 102 size_t tx_map_len;
97 u8 n_bytes; 103 u8 n_bytes;
104 u16 ctrl_reg;
105 u16 flag_reg;
106
98 int cs_change; 107 int cs_change;
99 void (*write) (struct driver_data *); 108 const struct bfin_spi_transfer_ops *ops;
100 void (*read) (struct driver_data *);
101 void (*duplex) (struct driver_data *);
102}; 109};
103 110
104struct chip_data { 111struct bfin_spi_slave_data {
105 u16 ctl_reg; 112 u16 ctl_reg;
106 u16 baud; 113 u16 baud;
107 u16 flag; 114 u16 flag;
108 115
109 u8 chip_select_num; 116 u8 chip_select_num;
110 u8 n_bytes;
111 u8 width; /* 0 or 1 */
112 u8 enable_dma; 117 u8 enable_dma;
113 u8 bits_per_word; /* 8 or 16 */
114 u8 cs_change_per_word;
115 u16 cs_chg_udelay; /* Some devices require > 255usec delay */ 118 u16 cs_chg_udelay; /* Some devices require > 255usec delay */
116 u32 cs_gpio; 119 u32 cs_gpio;
117 u16 idle_tx_val; 120 u16 idle_tx_val;
118 void (*write) (struct driver_data *); 121 u8 pio_interrupt; /* use spi data irq */
119 void (*read) (struct driver_data *); 122 const struct bfin_spi_transfer_ops *ops;
120 void (*duplex) (struct driver_data *);
121}; 123};
122 124
123#define DEFINE_SPI_REG(reg, off) \ 125#define DEFINE_SPI_REG(reg, off) \
124static inline u16 read_##reg(struct driver_data *drv_data) \ 126static inline u16 read_##reg(struct bfin_spi_master_data *drv_data) \
125 { return bfin_read16(drv_data->regs_base + off); } \ 127 { return bfin_read16(drv_data->regs_base + off); } \
126static inline void write_##reg(struct driver_data *drv_data, u16 v) \ 128static inline void write_##reg(struct bfin_spi_master_data *drv_data, u16 v) \
127 { bfin_write16(drv_data->regs_base + off, v); } 129 { bfin_write16(drv_data->regs_base + off, v); }
128 130
129DEFINE_SPI_REG(CTRL, 0x00) 131DEFINE_SPI_REG(CTRL, 0x00)
@@ -134,7 +136,7 @@ DEFINE_SPI_REG(RDBR, 0x10)
134DEFINE_SPI_REG(BAUD, 0x14) 136DEFINE_SPI_REG(BAUD, 0x14)
135DEFINE_SPI_REG(SHAW, 0x18) 137DEFINE_SPI_REG(SHAW, 0x18)
136 138
137static void bfin_spi_enable(struct driver_data *drv_data) 139static void bfin_spi_enable(struct bfin_spi_master_data *drv_data)
138{ 140{
139 u16 cr; 141 u16 cr;
140 142
@@ -142,7 +144,7 @@ static void bfin_spi_enable(struct driver_data *drv_data)
142 write_CTRL(drv_data, (cr | BIT_CTL_ENABLE)); 144 write_CTRL(drv_data, (cr | BIT_CTL_ENABLE));
143} 145}
144 146
145static void bfin_spi_disable(struct driver_data *drv_data) 147static void bfin_spi_disable(struct bfin_spi_master_data *drv_data)
146{ 148{
147 u16 cr; 149 u16 cr;
148 150
@@ -165,7 +167,7 @@ static u16 hz_to_spi_baud(u32 speed_hz)
165 return spi_baud; 167 return spi_baud;
166} 168}
167 169
168static int bfin_spi_flush(struct driver_data *drv_data) 170static int bfin_spi_flush(struct bfin_spi_master_data *drv_data)
169{ 171{
170 unsigned long limit = loops_per_jiffy << 1; 172 unsigned long limit = loops_per_jiffy << 1;
171 173
@@ -179,13 +181,12 @@ static int bfin_spi_flush(struct driver_data *drv_data)
179} 181}
180 182
181/* Chip select operation functions for cs_change flag */ 183/* Chip select operation functions for cs_change flag */
182static void bfin_spi_cs_active(struct driver_data *drv_data, struct chip_data *chip) 184static void bfin_spi_cs_active(struct bfin_spi_master_data *drv_data, struct bfin_spi_slave_data *chip)
183{ 185{
184 if (likely(chip->chip_select_num)) { 186 if (likely(chip->chip_select_num < MAX_CTRL_CS)) {
185 u16 flag = read_FLAG(drv_data); 187 u16 flag = read_FLAG(drv_data);
186 188
187 flag |= chip->flag; 189 flag &= ~chip->flag;
188 flag &= ~(chip->flag << 8);
189 190
190 write_FLAG(drv_data, flag); 191 write_FLAG(drv_data, flag);
191 } else { 192 } else {
@@ -193,13 +194,13 @@ static void bfin_spi_cs_active(struct driver_data *drv_data, struct chip_data *c
193 } 194 }
194} 195}
195 196
196static void bfin_spi_cs_deactive(struct driver_data *drv_data, struct chip_data *chip) 197static void bfin_spi_cs_deactive(struct bfin_spi_master_data *drv_data,
198 struct bfin_spi_slave_data *chip)
197{ 199{
198 if (likely(chip->chip_select_num)) { 200 if (likely(chip->chip_select_num < MAX_CTRL_CS)) {
199 u16 flag = read_FLAG(drv_data); 201 u16 flag = read_FLAG(drv_data);
200 202
201 flag &= ~chip->flag; 203 flag |= chip->flag;
202 flag |= (chip->flag << 8);
203 204
204 write_FLAG(drv_data, flag); 205 write_FLAG(drv_data, flag);
205 } else { 206 } else {
@@ -211,16 +212,43 @@ static void bfin_spi_cs_deactive(struct driver_data *drv_data, struct chip_data
211 udelay(chip->cs_chg_udelay); 212 udelay(chip->cs_chg_udelay);
212} 213}
213 214
215/* enable or disable the pin muxed by GPIO and SPI CS to work as SPI CS */
216static inline void bfin_spi_cs_enable(struct bfin_spi_master_data *drv_data,
217 struct bfin_spi_slave_data *chip)
218{
219 if (chip->chip_select_num < MAX_CTRL_CS) {
220 u16 flag = read_FLAG(drv_data);
221
222 flag |= (chip->flag >> 8);
223
224 write_FLAG(drv_data, flag);
225 }
226}
227
228static inline void bfin_spi_cs_disable(struct bfin_spi_master_data *drv_data,
229 struct bfin_spi_slave_data *chip)
230{
231 if (chip->chip_select_num < MAX_CTRL_CS) {
232 u16 flag = read_FLAG(drv_data);
233
234 flag &= ~(chip->flag >> 8);
235
236 write_FLAG(drv_data, flag);
237 }
238}
239
214/* stop controller and re-config current chip*/ 240/* stop controller and re-config current chip*/
215static void bfin_spi_restore_state(struct driver_data *drv_data) 241static void bfin_spi_restore_state(struct bfin_spi_master_data *drv_data)
216{ 242{
217 struct chip_data *chip = drv_data->cur_chip; 243 struct bfin_spi_slave_data *chip = drv_data->cur_chip;
218 244
219 /* Clear status and disable clock */ 245 /* Clear status and disable clock */
220 write_STAT(drv_data, BIT_STAT_CLR); 246 write_STAT(drv_data, BIT_STAT_CLR);
221 bfin_spi_disable(drv_data); 247 bfin_spi_disable(drv_data);
222 dev_dbg(&drv_data->pdev->dev, "restoring spi ctl state\n"); 248 dev_dbg(&drv_data->pdev->dev, "restoring spi ctl state\n");
223 249
250 SSYNC();
251
224 /* Load the registers */ 252 /* Load the registers */
225 write_CTRL(drv_data, chip->ctl_reg); 253 write_CTRL(drv_data, chip->ctl_reg);
226 write_BAUD(drv_data, chip->baud); 254 write_BAUD(drv_data, chip->baud);
@@ -230,49 +258,12 @@ static void bfin_spi_restore_state(struct driver_data *drv_data)
230} 258}
231 259
232/* used to kick off transfer in rx mode and read unwanted RX data */ 260/* used to kick off transfer in rx mode and read unwanted RX data */
233static inline void bfin_spi_dummy_read(struct driver_data *drv_data) 261static inline void bfin_spi_dummy_read(struct bfin_spi_master_data *drv_data)
234{ 262{
235 (void) read_RDBR(drv_data); 263 (void) read_RDBR(drv_data);
236} 264}
237 265
238static void bfin_spi_null_writer(struct driver_data *drv_data) 266static void bfin_spi_u8_writer(struct bfin_spi_master_data *drv_data)
239{
240 u8 n_bytes = drv_data->n_bytes;
241 u16 tx_val = drv_data->cur_chip->idle_tx_val;
242
243 /* clear RXS (we check for RXS inside the loop) */
244 bfin_spi_dummy_read(drv_data);
245
246 while (drv_data->tx < drv_data->tx_end) {
247 write_TDBR(drv_data, tx_val);
248 drv_data->tx += n_bytes;
249 /* wait until transfer finished.
250 checking SPIF or TXS may not guarantee transfer completion */
251 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
252 cpu_relax();
253 /* discard RX data and clear RXS */
254 bfin_spi_dummy_read(drv_data);
255 }
256}
257
258static void bfin_spi_null_reader(struct driver_data *drv_data)
259{
260 u8 n_bytes = drv_data->n_bytes;
261 u16 tx_val = drv_data->cur_chip->idle_tx_val;
262
263 /* discard old RX data and clear RXS */
264 bfin_spi_dummy_read(drv_data);
265
266 while (drv_data->rx < drv_data->rx_end) {
267 write_TDBR(drv_data, tx_val);
268 drv_data->rx += n_bytes;
269 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
270 cpu_relax();
271 bfin_spi_dummy_read(drv_data);
272 }
273}
274
275static void bfin_spi_u8_writer(struct driver_data *drv_data)
276{ 267{
277 /* clear RXS (we check for RXS inside the loop) */ 268 /* clear RXS (we check for RXS inside the loop) */
278 bfin_spi_dummy_read(drv_data); 269 bfin_spi_dummy_read(drv_data);
@@ -288,25 +279,7 @@ static void bfin_spi_u8_writer(struct driver_data *drv_data)
288 } 279 }
289} 280}
290 281
291static void bfin_spi_u8_cs_chg_writer(struct driver_data *drv_data) 282static void bfin_spi_u8_reader(struct bfin_spi_master_data *drv_data)
292{
293 struct chip_data *chip = drv_data->cur_chip;
294
295 /* clear RXS (we check for RXS inside the loop) */
296 bfin_spi_dummy_read(drv_data);
297
298 while (drv_data->tx < drv_data->tx_end) {
299 bfin_spi_cs_active(drv_data, chip);
300 write_TDBR(drv_data, (*(u8 *) (drv_data->tx++)));
301 /* make sure transfer finished before deactiving CS */
302 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
303 cpu_relax();
304 bfin_spi_dummy_read(drv_data);
305 bfin_spi_cs_deactive(drv_data, chip);
306 }
307}
308
309static void bfin_spi_u8_reader(struct driver_data *drv_data)
310{ 283{
311 u16 tx_val = drv_data->cur_chip->idle_tx_val; 284 u16 tx_val = drv_data->cur_chip->idle_tx_val;
312 285
@@ -321,25 +294,7 @@ static void bfin_spi_u8_reader(struct driver_data *drv_data)
321 } 294 }
322} 295}
323 296
324static void bfin_spi_u8_cs_chg_reader(struct driver_data *drv_data) 297static void bfin_spi_u8_duplex(struct bfin_spi_master_data *drv_data)
325{
326 struct chip_data *chip = drv_data->cur_chip;
327 u16 tx_val = chip->idle_tx_val;
328
329 /* discard old RX data and clear RXS */
330 bfin_spi_dummy_read(drv_data);
331
332 while (drv_data->rx < drv_data->rx_end) {
333 bfin_spi_cs_active(drv_data, chip);
334 write_TDBR(drv_data, tx_val);
335 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
336 cpu_relax();
337 *(u8 *) (drv_data->rx++) = read_RDBR(drv_data);
338 bfin_spi_cs_deactive(drv_data, chip);
339 }
340}
341
342static void bfin_spi_u8_duplex(struct driver_data *drv_data)
343{ 298{
344 /* discard old RX data and clear RXS */ 299 /* discard old RX data and clear RXS */
345 bfin_spi_dummy_read(drv_data); 300 bfin_spi_dummy_read(drv_data);
@@ -352,24 +307,13 @@ static void bfin_spi_u8_duplex(struct driver_data *drv_data)
352 } 307 }
353} 308}
354 309
355static void bfin_spi_u8_cs_chg_duplex(struct driver_data *drv_data) 310static const struct bfin_spi_transfer_ops bfin_bfin_spi_transfer_ops_u8 = {
356{ 311 .write = bfin_spi_u8_writer,
357 struct chip_data *chip = drv_data->cur_chip; 312 .read = bfin_spi_u8_reader,
358 313 .duplex = bfin_spi_u8_duplex,
359 /* discard old RX data and clear RXS */ 314};
360 bfin_spi_dummy_read(drv_data);
361
362 while (drv_data->rx < drv_data->rx_end) {
363 bfin_spi_cs_active(drv_data, chip);
364 write_TDBR(drv_data, (*(u8 *) (drv_data->tx++)));
365 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
366 cpu_relax();
367 *(u8 *) (drv_data->rx++) = read_RDBR(drv_data);
368 bfin_spi_cs_deactive(drv_data, chip);
369 }
370}
371 315
372static void bfin_spi_u16_writer(struct driver_data *drv_data) 316static void bfin_spi_u16_writer(struct bfin_spi_master_data *drv_data)
373{ 317{
374 /* clear RXS (we check for RXS inside the loop) */ 318 /* clear RXS (we check for RXS inside the loop) */
375 bfin_spi_dummy_read(drv_data); 319 bfin_spi_dummy_read(drv_data);
@@ -386,26 +330,7 @@ static void bfin_spi_u16_writer(struct driver_data *drv_data)
386 } 330 }
387} 331}
388 332
389static void bfin_spi_u16_cs_chg_writer(struct driver_data *drv_data) 333static void bfin_spi_u16_reader(struct bfin_spi_master_data *drv_data)
390{
391 struct chip_data *chip = drv_data->cur_chip;
392
393 /* clear RXS (we check for RXS inside the loop) */
394 bfin_spi_dummy_read(drv_data);
395
396 while (drv_data->tx < drv_data->tx_end) {
397 bfin_spi_cs_active(drv_data, chip);
398 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
399 drv_data->tx += 2;
400 /* make sure transfer finished before deactiving CS */
401 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
402 cpu_relax();
403 bfin_spi_dummy_read(drv_data);
404 bfin_spi_cs_deactive(drv_data, chip);
405 }
406}
407
408static void bfin_spi_u16_reader(struct driver_data *drv_data)
409{ 334{
410 u16 tx_val = drv_data->cur_chip->idle_tx_val; 335 u16 tx_val = drv_data->cur_chip->idle_tx_val;
411 336
@@ -421,26 +346,7 @@ static void bfin_spi_u16_reader(struct driver_data *drv_data)
421 } 346 }
422} 347}
423 348
424static void bfin_spi_u16_cs_chg_reader(struct driver_data *drv_data) 349static void bfin_spi_u16_duplex(struct bfin_spi_master_data *drv_data)
425{
426 struct chip_data *chip = drv_data->cur_chip;
427 u16 tx_val = chip->idle_tx_val;
428
429 /* discard old RX data and clear RXS */
430 bfin_spi_dummy_read(drv_data);
431
432 while (drv_data->rx < drv_data->rx_end) {
433 bfin_spi_cs_active(drv_data, chip);
434 write_TDBR(drv_data, tx_val);
435 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
436 cpu_relax();
437 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
438 drv_data->rx += 2;
439 bfin_spi_cs_deactive(drv_data, chip);
440 }
441}
442
443static void bfin_spi_u16_duplex(struct driver_data *drv_data)
444{ 350{
445 /* discard old RX data and clear RXS */ 351 /* discard old RX data and clear RXS */
446 bfin_spi_dummy_read(drv_data); 352 bfin_spi_dummy_read(drv_data);
@@ -455,27 +361,14 @@ static void bfin_spi_u16_duplex(struct driver_data *drv_data)
455 } 361 }
456} 362}
457 363
458static void bfin_spi_u16_cs_chg_duplex(struct driver_data *drv_data) 364static const struct bfin_spi_transfer_ops bfin_bfin_spi_transfer_ops_u16 = {
459{ 365 .write = bfin_spi_u16_writer,
460 struct chip_data *chip = drv_data->cur_chip; 366 .read = bfin_spi_u16_reader,
461 367 .duplex = bfin_spi_u16_duplex,
462 /* discard old RX data and clear RXS */ 368};
463 bfin_spi_dummy_read(drv_data);
464
465 while (drv_data->rx < drv_data->rx_end) {
466 bfin_spi_cs_active(drv_data, chip);
467 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
468 drv_data->tx += 2;
469 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
470 cpu_relax();
471 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
472 drv_data->rx += 2;
473 bfin_spi_cs_deactive(drv_data, chip);
474 }
475}
476 369
477/* test if ther is more transfer to be done */ 370/* test if there is more transfer to be done */
478static void *bfin_spi_next_transfer(struct driver_data *drv_data) 371static void *bfin_spi_next_transfer(struct bfin_spi_master_data *drv_data)
479{ 372{
480 struct spi_message *msg = drv_data->cur_msg; 373 struct spi_message *msg = drv_data->cur_msg;
481 struct spi_transfer *trans = drv_data->cur_transfer; 374 struct spi_transfer *trans = drv_data->cur_transfer;
@@ -494,9 +387,9 @@ static void *bfin_spi_next_transfer(struct driver_data *drv_data)
494 * caller already set message->status; 387 * caller already set message->status;
495 * dma and pio irqs are blocked give finished message back 388 * dma and pio irqs are blocked give finished message back
496 */ 389 */
497static void bfin_spi_giveback(struct driver_data *drv_data) 390static void bfin_spi_giveback(struct bfin_spi_master_data *drv_data)
498{ 391{
499 struct chip_data *chip = drv_data->cur_chip; 392 struct bfin_spi_slave_data *chip = drv_data->cur_chip;
500 struct spi_transfer *last_transfer; 393 struct spi_transfer *last_transfer;
501 unsigned long flags; 394 unsigned long flags;
502 struct spi_message *msg; 395 struct spi_message *msg;
@@ -525,10 +418,83 @@ static void bfin_spi_giveback(struct driver_data *drv_data)
525 msg->complete(msg->context); 418 msg->complete(msg->context);
526} 419}
527 420
421/* spi data irq handler */
422static irqreturn_t bfin_spi_pio_irq_handler(int irq, void *dev_id)
423{
424 struct bfin_spi_master_data *drv_data = dev_id;
425 struct bfin_spi_slave_data *chip = drv_data->cur_chip;
426 struct spi_message *msg = drv_data->cur_msg;
427 int n_bytes = drv_data->n_bytes;
428
429 /* wait until transfer finished. */
430 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
431 cpu_relax();
432
433 if ((drv_data->tx && drv_data->tx >= drv_data->tx_end) ||
434 (drv_data->rx && drv_data->rx >= (drv_data->rx_end - n_bytes))) {
435 /* last read */
436 if (drv_data->rx) {
437 dev_dbg(&drv_data->pdev->dev, "last read\n");
438 if (n_bytes == 2)
439 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
440 else if (n_bytes == 1)
441 *(u8 *) (drv_data->rx) = read_RDBR(drv_data);
442 drv_data->rx += n_bytes;
443 }
444
445 msg->actual_length += drv_data->len_in_bytes;
446 if (drv_data->cs_change)
447 bfin_spi_cs_deactive(drv_data, chip);
448 /* Move to next transfer */
449 msg->state = bfin_spi_next_transfer(drv_data);
450
451 disable_irq_nosync(drv_data->spi_irq);
452
453 /* Schedule transfer tasklet */
454 tasklet_schedule(&drv_data->pump_transfers);
455 return IRQ_HANDLED;
456 }
457
458 if (drv_data->rx && drv_data->tx) {
459 /* duplex */
460 dev_dbg(&drv_data->pdev->dev, "duplex: write_TDBR\n");
461 if (drv_data->n_bytes == 2) {
462 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
463 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
464 } else if (drv_data->n_bytes == 1) {
465 *(u8 *) (drv_data->rx) = read_RDBR(drv_data);
466 write_TDBR(drv_data, (*(u8 *) (drv_data->tx)));
467 }
468 } else if (drv_data->rx) {
469 /* read */
470 dev_dbg(&drv_data->pdev->dev, "read: write_TDBR\n");
471 if (drv_data->n_bytes == 2)
472 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
473 else if (drv_data->n_bytes == 1)
474 *(u8 *) (drv_data->rx) = read_RDBR(drv_data);
475 write_TDBR(drv_data, chip->idle_tx_val);
476 } else if (drv_data->tx) {
477 /* write */
478 dev_dbg(&drv_data->pdev->dev, "write: write_TDBR\n");
479 bfin_spi_dummy_read(drv_data);
480 if (drv_data->n_bytes == 2)
481 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
482 else if (drv_data->n_bytes == 1)
483 write_TDBR(drv_data, (*(u8 *) (drv_data->tx)));
484 }
485
486 if (drv_data->tx)
487 drv_data->tx += n_bytes;
488 if (drv_data->rx)
489 drv_data->rx += n_bytes;
490
491 return IRQ_HANDLED;
492}
493
528static irqreturn_t bfin_spi_dma_irq_handler(int irq, void *dev_id) 494static irqreturn_t bfin_spi_dma_irq_handler(int irq, void *dev_id)
529{ 495{
530 struct driver_data *drv_data = dev_id; 496 struct bfin_spi_master_data *drv_data = dev_id;
531 struct chip_data *chip = drv_data->cur_chip; 497 struct bfin_spi_slave_data *chip = drv_data->cur_chip;
532 struct spi_message *msg = drv_data->cur_msg; 498 struct spi_message *msg = drv_data->cur_msg;
533 unsigned long timeout; 499 unsigned long timeout;
534 unsigned short dmastat = get_dma_curr_irqstat(drv_data->dma_channel); 500 unsigned short dmastat = get_dma_curr_irqstat(drv_data->dma_channel);
@@ -540,10 +506,6 @@ static irqreturn_t bfin_spi_dma_irq_handler(int irq, void *dev_id)
540 506
541 clear_dma_irqstat(drv_data->dma_channel); 507 clear_dma_irqstat(drv_data->dma_channel);
542 508
543 /* Wait for DMA to complete */
544 while (get_dma_curr_irqstat(drv_data->dma_channel) & DMA_RUN)
545 cpu_relax();
546
547 /* 509 /*
548 * wait for the last transaction shifted out. HRM states: 510 * wait for the last transaction shifted out. HRM states:
549 * at this point there may still be data in the SPI DMA FIFO waiting 511 * at this point there may still be data in the SPI DMA FIFO waiting
@@ -551,8 +513,8 @@ static irqreturn_t bfin_spi_dma_irq_handler(int irq, void *dev_id)
551 * register until it goes low for 2 successive reads 513 * register until it goes low for 2 successive reads
552 */ 514 */
553 if (drv_data->tx != NULL) { 515 if (drv_data->tx != NULL) {
554 while ((read_STAT(drv_data) & TXS) || 516 while ((read_STAT(drv_data) & BIT_STAT_TXS) ||
555 (read_STAT(drv_data) & TXS)) 517 (read_STAT(drv_data) & BIT_STAT_TXS))
556 cpu_relax(); 518 cpu_relax();
557 } 519 }
558 520
@@ -561,14 +523,14 @@ static irqreturn_t bfin_spi_dma_irq_handler(int irq, void *dev_id)
561 dmastat, read_STAT(drv_data)); 523 dmastat, read_STAT(drv_data));
562 524
563 timeout = jiffies + HZ; 525 timeout = jiffies + HZ;
564 while (!(read_STAT(drv_data) & SPIF)) 526 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
565 if (!time_before(jiffies, timeout)) { 527 if (!time_before(jiffies, timeout)) {
566 dev_warn(&drv_data->pdev->dev, "timeout waiting for SPIF"); 528 dev_warn(&drv_data->pdev->dev, "timeout waiting for SPIF");
567 break; 529 break;
568 } else 530 } else
569 cpu_relax(); 531 cpu_relax();
570 532
571 if ((dmastat & DMA_ERR) && (spistat & RBSY)) { 533 if ((dmastat & DMA_ERR) && (spistat & BIT_STAT_RBSY)) {
572 msg->state = ERROR_STATE; 534 msg->state = ERROR_STATE;
573 dev_err(&drv_data->pdev->dev, "dma receive: fifo/buffer overflow\n"); 535 dev_err(&drv_data->pdev->dev, "dma receive: fifo/buffer overflow\n");
574 } else { 536 } else {
@@ -588,20 +550,20 @@ static irqreturn_t bfin_spi_dma_irq_handler(int irq, void *dev_id)
588 dev_dbg(&drv_data->pdev->dev, 550 dev_dbg(&drv_data->pdev->dev,
589 "disable dma channel irq%d\n", 551 "disable dma channel irq%d\n",
590 drv_data->dma_channel); 552 drv_data->dma_channel);
591 dma_disable_irq(drv_data->dma_channel); 553 dma_disable_irq_nosync(drv_data->dma_channel);
592 554
593 return IRQ_HANDLED; 555 return IRQ_HANDLED;
594} 556}
595 557
596static void bfin_spi_pump_transfers(unsigned long data) 558static void bfin_spi_pump_transfers(unsigned long data)
597{ 559{
598 struct driver_data *drv_data = (struct driver_data *)data; 560 struct bfin_spi_master_data *drv_data = (struct bfin_spi_master_data *)data;
599 struct spi_message *message = NULL; 561 struct spi_message *message = NULL;
600 struct spi_transfer *transfer = NULL; 562 struct spi_transfer *transfer = NULL;
601 struct spi_transfer *previous = NULL; 563 struct spi_transfer *previous = NULL;
602 struct chip_data *chip = NULL; 564 struct bfin_spi_slave_data *chip = NULL;
603 u8 width; 565 unsigned int bits_per_word;
604 u16 cr, dma_width, dma_config; 566 u16 cr, cr_width, dma_width, dma_config;
605 u32 tranf_success = 1; 567 u32 tranf_success = 1;
606 u8 full_duplex = 0; 568 u8 full_duplex = 0;
607 569
@@ -639,7 +601,7 @@ static void bfin_spi_pump_transfers(unsigned long data)
639 udelay(previous->delay_usecs); 601 udelay(previous->delay_usecs);
640 } 602 }
641 603
642 /* Setup the transfer state based on the type of transfer */ 604 /* Flush any existing transfers that may be sitting in the hardware */
643 if (bfin_spi_flush(drv_data) == 0) { 605 if (bfin_spi_flush(drv_data) == 0) {
644 dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n"); 606 dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
645 message->status = -EIO; 607 message->status = -EIO;
@@ -679,52 +641,31 @@ static void bfin_spi_pump_transfers(unsigned long data)
679 drv_data->cs_change = transfer->cs_change; 641 drv_data->cs_change = transfer->cs_change;
680 642
681 /* Bits per word setup */ 643 /* Bits per word setup */
682 switch (transfer->bits_per_word) { 644 bits_per_word = transfer->bits_per_word ? : message->spi->bits_per_word;
683 case 8: 645 if (bits_per_word == 8) {
684 drv_data->n_bytes = 1; 646 drv_data->n_bytes = 1;
685 width = CFG_SPI_WORDSIZE8; 647 drv_data->len = transfer->len;
686 drv_data->read = chip->cs_change_per_word ? 648 cr_width = 0;
687 bfin_spi_u8_cs_chg_reader : bfin_spi_u8_reader; 649 drv_data->ops = &bfin_bfin_spi_transfer_ops_u8;
688 drv_data->write = chip->cs_change_per_word ? 650 } else if (bits_per_word == 16) {
689 bfin_spi_u8_cs_chg_writer : bfin_spi_u8_writer;
690 drv_data->duplex = chip->cs_change_per_word ?
691 bfin_spi_u8_cs_chg_duplex : bfin_spi_u8_duplex;
692 break;
693
694 case 16:
695 drv_data->n_bytes = 2; 651 drv_data->n_bytes = 2;
696 width = CFG_SPI_WORDSIZE16;
697 drv_data->read = chip->cs_change_per_word ?
698 bfin_spi_u16_cs_chg_reader : bfin_spi_u16_reader;
699 drv_data->write = chip->cs_change_per_word ?
700 bfin_spi_u16_cs_chg_writer : bfin_spi_u16_writer;
701 drv_data->duplex = chip->cs_change_per_word ?
702 bfin_spi_u16_cs_chg_duplex : bfin_spi_u16_duplex;
703 break;
704
705 default:
706 /* No change, the same as default setting */
707 drv_data->n_bytes = chip->n_bytes;
708 width = chip->width;
709 drv_data->write = drv_data->tx ? chip->write : bfin_spi_null_writer;
710 drv_data->read = drv_data->rx ? chip->read : bfin_spi_null_reader;
711 drv_data->duplex = chip->duplex ? chip->duplex : bfin_spi_null_writer;
712 break;
713 }
714 cr = (read_CTRL(drv_data) & (~BIT_CTL_TIMOD));
715 cr |= (width << 8);
716 write_CTRL(drv_data, cr);
717
718 if (width == CFG_SPI_WORDSIZE16) {
719 drv_data->len = (transfer->len) >> 1; 652 drv_data->len = (transfer->len) >> 1;
653 cr_width = BIT_CTL_WORDSIZE;
654 drv_data->ops = &bfin_bfin_spi_transfer_ops_u16;
720 } else { 655 } else {
721 drv_data->len = transfer->len; 656 dev_err(&drv_data->pdev->dev, "transfer: unsupported bits_per_word\n");
657 message->status = -EINVAL;
658 bfin_spi_giveback(drv_data);
659 return;
722 } 660 }
661 cr = read_CTRL(drv_data) & ~(BIT_CTL_TIMOD | BIT_CTL_WORDSIZE);
662 cr |= cr_width;
663 write_CTRL(drv_data, cr);
664
723 dev_dbg(&drv_data->pdev->dev, 665 dev_dbg(&drv_data->pdev->dev,
724 "transfer: drv_data->write is %p, chip->write is %p, null_wr is %p\n", 666 "transfer: drv_data->ops is %p, chip->ops is %p, u8_ops is %p\n",
725 drv_data->write, chip->write, bfin_spi_null_writer); 667 drv_data->ops, chip->ops, &bfin_bfin_spi_transfer_ops_u8);
726 668
727 /* speed and width has been set on per message */
728 message->state = RUNNING_STATE; 669 message->state = RUNNING_STATE;
729 dma_config = 0; 670 dma_config = 0;
730 671
@@ -735,13 +676,11 @@ static void bfin_spi_pump_transfers(unsigned long data)
735 write_BAUD(drv_data, chip->baud); 676 write_BAUD(drv_data, chip->baud);
736 677
737 write_STAT(drv_data, BIT_STAT_CLR); 678 write_STAT(drv_data, BIT_STAT_CLR);
738 cr = (read_CTRL(drv_data) & (~BIT_CTL_TIMOD)); 679 bfin_spi_cs_active(drv_data, chip);
739 if (drv_data->cs_change)
740 bfin_spi_cs_active(drv_data, chip);
741 680
742 dev_dbg(&drv_data->pdev->dev, 681 dev_dbg(&drv_data->pdev->dev,
743 "now pumping a transfer: width is %d, len is %d\n", 682 "now pumping a transfer: width is %d, len is %d\n",
744 width, transfer->len); 683 cr_width, transfer->len);
745 684
746 /* 685 /*
747 * Try to map dma buffer and do a dma transfer. If successful use, 686 * Try to map dma buffer and do a dma transfer. If successful use,
@@ -760,7 +699,7 @@ static void bfin_spi_pump_transfers(unsigned long data)
760 /* config dma channel */ 699 /* config dma channel */
761 dev_dbg(&drv_data->pdev->dev, "doing dma transfer\n"); 700 dev_dbg(&drv_data->pdev->dev, "doing dma transfer\n");
762 set_dma_x_count(drv_data->dma_channel, drv_data->len); 701 set_dma_x_count(drv_data->dma_channel, drv_data->len);
763 if (width == CFG_SPI_WORDSIZE16) { 702 if (cr_width == BIT_CTL_WORDSIZE) {
764 set_dma_x_modify(drv_data->dma_channel, 2); 703 set_dma_x_modify(drv_data->dma_channel, 2);
765 dma_width = WDSIZE_16; 704 dma_width = WDSIZE_16;
766 } else { 705 } else {
@@ -846,73 +785,100 @@ static void bfin_spi_pump_transfers(unsigned long data)
846 dma_enable_irq(drv_data->dma_channel); 785 dma_enable_irq(drv_data->dma_channel);
847 local_irq_restore(flags); 786 local_irq_restore(flags);
848 787
849 } else { 788 return;
850 /* IO mode write then read */ 789 }
851 dev_dbg(&drv_data->pdev->dev, "doing IO transfer\n");
852
853 /* we always use SPI_WRITE mode. SPI_READ mode
854 seems to have problems with setting up the
855 output value in TDBR prior to the transfer. */
856 write_CTRL(drv_data, (cr | CFG_SPI_WRITE));
857
858 if (full_duplex) {
859 /* full duplex mode */
860 BUG_ON((drv_data->tx_end - drv_data->tx) !=
861 (drv_data->rx_end - drv_data->rx));
862 dev_dbg(&drv_data->pdev->dev,
863 "IO duplex: cr is 0x%x\n", cr);
864
865 drv_data->duplex(drv_data);
866 790
867 if (drv_data->tx != drv_data->tx_end) 791 /*
868 tranf_success = 0; 792 * We always use SPI_WRITE mode (transfer starts with TDBR write).
869 } else if (drv_data->tx != NULL) { 793 * SPI_READ mode (transfer starts with RDBR read) seems to have
870 /* write only half duplex */ 794 * problems with setting up the output value in TDBR prior to the
871 dev_dbg(&drv_data->pdev->dev, 795 * start of the transfer.
872 "IO write: cr is 0x%x\n", cr); 796 */
797 write_CTRL(drv_data, cr | BIT_CTL_TXMOD);
873 798
874 drv_data->write(drv_data); 799 if (chip->pio_interrupt) {
800 /* SPI irq should have been disabled by now */
875 801
876 if (drv_data->tx != drv_data->tx_end) 802 /* discard old RX data and clear RXS */
877 tranf_success = 0; 803 bfin_spi_dummy_read(drv_data);
878 } else if (drv_data->rx != NULL) {
879 /* read only half duplex */
880 dev_dbg(&drv_data->pdev->dev,
881 "IO read: cr is 0x%x\n", cr);
882 804
883 drv_data->read(drv_data); 805 /* start transfer */
884 if (drv_data->rx != drv_data->rx_end) 806 if (drv_data->tx == NULL)
885 tranf_success = 0; 807 write_TDBR(drv_data, chip->idle_tx_val);
808 else {
809 if (bits_per_word == 8)
810 write_TDBR(drv_data, (*(u8 *) (drv_data->tx)));
811 else
812 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
813 drv_data->tx += drv_data->n_bytes;
886 } 814 }
887 815
888 if (!tranf_success) { 816 /* once TDBR is empty, interrupt is triggered */
889 dev_dbg(&drv_data->pdev->dev, 817 enable_irq(drv_data->spi_irq);
890 "IO write error!\n"); 818 return;
891 message->state = ERROR_STATE; 819 }
892 } else { 820
893 /* Update total byte transfered */ 821 /* IO mode */
894 message->actual_length += drv_data->len_in_bytes; 822 dev_dbg(&drv_data->pdev->dev, "doing IO transfer\n");
895 /* Move to next transfer of this msg */ 823
896 message->state = bfin_spi_next_transfer(drv_data); 824 if (full_duplex) {
897 if (drv_data->cs_change) 825 /* full duplex mode */
898 bfin_spi_cs_deactive(drv_data, chip); 826 BUG_ON((drv_data->tx_end - drv_data->tx) !=
899 } 827 (drv_data->rx_end - drv_data->rx));
900 /* Schedule next transfer tasklet */ 828 dev_dbg(&drv_data->pdev->dev,
901 tasklet_schedule(&drv_data->pump_transfers); 829 "IO duplex: cr is 0x%x\n", cr);
830
831 drv_data->ops->duplex(drv_data);
832
833 if (drv_data->tx != drv_data->tx_end)
834 tranf_success = 0;
835 } else if (drv_data->tx != NULL) {
836 /* write only half duplex */
837 dev_dbg(&drv_data->pdev->dev,
838 "IO write: cr is 0x%x\n", cr);
839
840 drv_data->ops->write(drv_data);
841
842 if (drv_data->tx != drv_data->tx_end)
843 tranf_success = 0;
844 } else if (drv_data->rx != NULL) {
845 /* read only half duplex */
846 dev_dbg(&drv_data->pdev->dev,
847 "IO read: cr is 0x%x\n", cr);
848
849 drv_data->ops->read(drv_data);
850 if (drv_data->rx != drv_data->rx_end)
851 tranf_success = 0;
852 }
853
854 if (!tranf_success) {
855 dev_dbg(&drv_data->pdev->dev,
856 "IO write error!\n");
857 message->state = ERROR_STATE;
858 } else {
859 /* Update total byte transfered */
860 message->actual_length += drv_data->len_in_bytes;
861 /* Move to next transfer of this msg */
862 message->state = bfin_spi_next_transfer(drv_data);
863 if (drv_data->cs_change)
864 bfin_spi_cs_deactive(drv_data, chip);
902 } 865 }
866
867 /* Schedule next transfer tasklet */
868 tasklet_schedule(&drv_data->pump_transfers);
903} 869}
904 870
905/* pop a msg from queue and kick off real transfer */ 871/* pop a msg from queue and kick off real transfer */
906static void bfin_spi_pump_messages(struct work_struct *work) 872static void bfin_spi_pump_messages(struct work_struct *work)
907{ 873{
908 struct driver_data *drv_data; 874 struct bfin_spi_master_data *drv_data;
909 unsigned long flags; 875 unsigned long flags;
910 876
911 drv_data = container_of(work, struct driver_data, pump_messages); 877 drv_data = container_of(work, struct bfin_spi_master_data, pump_messages);
912 878
913 /* Lock queue and check for queue work */ 879 /* Lock queue and check for queue work */
914 spin_lock_irqsave(&drv_data->lock, flags); 880 spin_lock_irqsave(&drv_data->lock, flags);
915 if (list_empty(&drv_data->queue) || drv_data->run == QUEUE_STOPPED) { 881 if (list_empty(&drv_data->queue) || !drv_data->running) {
916 /* pumper kicked off but no work to do */ 882 /* pumper kicked off but no work to do */
917 drv_data->busy = 0; 883 drv_data->busy = 0;
918 spin_unlock_irqrestore(&drv_data->lock, flags); 884 spin_unlock_irqrestore(&drv_data->lock, flags);
@@ -962,12 +928,12 @@ static void bfin_spi_pump_messages(struct work_struct *work)
962 */ 928 */
963static int bfin_spi_transfer(struct spi_device *spi, struct spi_message *msg) 929static int bfin_spi_transfer(struct spi_device *spi, struct spi_message *msg)
964{ 930{
965 struct driver_data *drv_data = spi_master_get_devdata(spi->master); 931 struct bfin_spi_master_data *drv_data = spi_master_get_devdata(spi->master);
966 unsigned long flags; 932 unsigned long flags;
967 933
968 spin_lock_irqsave(&drv_data->lock, flags); 934 spin_lock_irqsave(&drv_data->lock, flags);
969 935
970 if (drv_data->run == QUEUE_STOPPED) { 936 if (!drv_data->running) {
971 spin_unlock_irqrestore(&drv_data->lock, flags); 937 spin_unlock_irqrestore(&drv_data->lock, flags);
972 return -ESHUTDOWN; 938 return -ESHUTDOWN;
973 } 939 }
@@ -979,7 +945,7 @@ static int bfin_spi_transfer(struct spi_device *spi, struct spi_message *msg)
979 dev_dbg(&spi->dev, "adding an msg in transfer() \n"); 945 dev_dbg(&spi->dev, "adding an msg in transfer() \n");
980 list_add_tail(&msg->queue, &drv_data->queue); 946 list_add_tail(&msg->queue, &drv_data->queue);
981 947
982 if (drv_data->run == QUEUE_RUNNING && !drv_data->busy) 948 if (drv_data->running && !drv_data->busy)
983 queue_work(drv_data->workqueue, &drv_data->pump_messages); 949 queue_work(drv_data->workqueue, &drv_data->pump_messages);
984 950
985 spin_unlock_irqrestore(&drv_data->lock, flags); 951 spin_unlock_irqrestore(&drv_data->lock, flags);
@@ -1003,147 +969,184 @@ static u16 ssel[][MAX_SPI_SSEL] = {
1003 P_SPI2_SSEL6, P_SPI2_SSEL7}, 969 P_SPI2_SSEL6, P_SPI2_SSEL7},
1004}; 970};
1005 971
1006/* first setup for new devices */ 972/* setup for devices (may be called multiple times -- not just first setup) */
1007static int bfin_spi_setup(struct spi_device *spi) 973static int bfin_spi_setup(struct spi_device *spi)
1008{ 974{
1009 struct bfin5xx_spi_chip *chip_info = NULL; 975 struct bfin5xx_spi_chip *chip_info;
1010 struct chip_data *chip; 976 struct bfin_spi_slave_data *chip = NULL;
1011 struct driver_data *drv_data = spi_master_get_devdata(spi->master); 977 struct bfin_spi_master_data *drv_data = spi_master_get_devdata(spi->master);
1012 int ret; 978 u16 bfin_ctl_reg;
1013 979 int ret = -EINVAL;
1014 if (spi->bits_per_word != 8 && spi->bits_per_word != 16)
1015 return -EINVAL;
1016 980
1017 /* Only alloc (or use chip_info) on first setup */ 981 /* Only alloc (or use chip_info) on first setup */
982 chip_info = NULL;
1018 chip = spi_get_ctldata(spi); 983 chip = spi_get_ctldata(spi);
1019 if (chip == NULL) { 984 if (chip == NULL) {
1020 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL); 985 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1021 if (!chip) 986 if (!chip) {
1022 return -ENOMEM; 987 dev_err(&spi->dev, "cannot allocate chip data\n");
988 ret = -ENOMEM;
989 goto error;
990 }
1023 991
1024 chip->enable_dma = 0; 992 chip->enable_dma = 0;
1025 chip_info = spi->controller_data; 993 chip_info = spi->controller_data;
1026 } 994 }
1027 995
996 /* Let people set non-standard bits directly */
997 bfin_ctl_reg = BIT_CTL_OPENDRAIN | BIT_CTL_EMISO |
998 BIT_CTL_PSSE | BIT_CTL_GM | BIT_CTL_SZ;
999
1028 /* chip_info isn't always needed */ 1000 /* chip_info isn't always needed */
1029 if (chip_info) { 1001 if (chip_info) {
1030 /* Make sure people stop trying to set fields via ctl_reg 1002 /* Make sure people stop trying to set fields via ctl_reg
1031 * when they should actually be using common SPI framework. 1003 * when they should actually be using common SPI framework.
1032 * Currently we let through: WOM EMISO PSSE GM SZ TIMOD. 1004 * Currently we let through: WOM EMISO PSSE GM SZ.
1033 * Not sure if a user actually needs/uses any of these, 1005 * Not sure if a user actually needs/uses any of these,
1034 * but let's assume (for now) they do. 1006 * but let's assume (for now) they do.
1035 */ 1007 */
1036 if (chip_info->ctl_reg & (SPE|MSTR|CPOL|CPHA|LSBF|SIZE)) { 1008 if (chip_info->ctl_reg & ~bfin_ctl_reg) {
1037 dev_err(&spi->dev, "do not set bits in ctl_reg " 1009 dev_err(&spi->dev, "do not set bits in ctl_reg "
1038 "that the SPI framework manages\n"); 1010 "that the SPI framework manages\n");
1039 return -EINVAL; 1011 goto error;
1040 } 1012 }
1041
1042 chip->enable_dma = chip_info->enable_dma != 0 1013 chip->enable_dma = chip_info->enable_dma != 0
1043 && drv_data->master_info->enable_dma; 1014 && drv_data->master_info->enable_dma;
1044 chip->ctl_reg = chip_info->ctl_reg; 1015 chip->ctl_reg = chip_info->ctl_reg;
1045 chip->bits_per_word = chip_info->bits_per_word;
1046 chip->cs_change_per_word = chip_info->cs_change_per_word;
1047 chip->cs_chg_udelay = chip_info->cs_chg_udelay; 1016 chip->cs_chg_udelay = chip_info->cs_chg_udelay;
1048 chip->cs_gpio = chip_info->cs_gpio;
1049 chip->idle_tx_val = chip_info->idle_tx_val; 1017 chip->idle_tx_val = chip_info->idle_tx_val;
1018 chip->pio_interrupt = chip_info->pio_interrupt;
1019 spi->bits_per_word = chip_info->bits_per_word;
1020 } else {
1021 /* force a default base state */
1022 chip->ctl_reg &= bfin_ctl_reg;
1023 }
1024
1025 if (spi->bits_per_word != 8 && spi->bits_per_word != 16) {
1026 dev_err(&spi->dev, "%d bits_per_word is not supported\n",
1027 spi->bits_per_word);
1028 goto error;
1050 } 1029 }
1051 1030
1052 /* translate common spi framework into our register */ 1031 /* translate common spi framework into our register */
1032 if (spi->mode & ~(SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST)) {
1033 dev_err(&spi->dev, "unsupported spi modes detected\n");
1034 goto error;
1035 }
1053 if (spi->mode & SPI_CPOL) 1036 if (spi->mode & SPI_CPOL)
1054 chip->ctl_reg |= CPOL; 1037 chip->ctl_reg |= BIT_CTL_CPOL;
1055 if (spi->mode & SPI_CPHA) 1038 if (spi->mode & SPI_CPHA)
1056 chip->ctl_reg |= CPHA; 1039 chip->ctl_reg |= BIT_CTL_CPHA;
1057 if (spi->mode & SPI_LSB_FIRST) 1040 if (spi->mode & SPI_LSB_FIRST)
1058 chip->ctl_reg |= LSBF; 1041 chip->ctl_reg |= BIT_CTL_LSBF;
1059 /* we dont support running in slave mode (yet?) */ 1042 /* we dont support running in slave mode (yet?) */
1060 chip->ctl_reg |= MSTR; 1043 chip->ctl_reg |= BIT_CTL_MASTER;
1061 1044
1062 /* 1045 /*
1046 * Notice: for blackfin, the speed_hz is the value of register
1047 * SPI_BAUD, not the real baudrate
1048 */
1049 chip->baud = hz_to_spi_baud(spi->max_speed_hz);
1050 chip->chip_select_num = spi->chip_select;
1051 if (chip->chip_select_num < MAX_CTRL_CS) {
1052 if (!(spi->mode & SPI_CPHA))
1053 dev_warn(&spi->dev, "Warning: SPI CPHA not set:"
1054 " Slave Select not under software control!\n"
1055 " See Documentation/blackfin/bfin-spi-notes.txt");
1056
1057 chip->flag = (1 << spi->chip_select) << 8;
1058 } else
1059 chip->cs_gpio = chip->chip_select_num - MAX_CTRL_CS;
1060
1061 if (chip->enable_dma && chip->pio_interrupt) {
1062 dev_err(&spi->dev, "enable_dma is set, "
1063 "do not set pio_interrupt\n");
1064 goto error;
1065 }
1066 /*
1063 * if any one SPI chip is registered and wants DMA, request the 1067 * if any one SPI chip is registered and wants DMA, request the
1064 * DMA channel for it 1068 * DMA channel for it
1065 */ 1069 */
1066 if (chip->enable_dma && !drv_data->dma_requested) { 1070 if (chip->enable_dma && !drv_data->dma_requested) {
1067 /* register dma irq handler */ 1071 /* register dma irq handler */
1068 if (request_dma(drv_data->dma_channel, "BFIN_SPI_DMA") < 0) { 1072 ret = request_dma(drv_data->dma_channel, "BFIN_SPI_DMA");
1069 dev_dbg(&spi->dev, 1073 if (ret) {
1074 dev_err(&spi->dev,
1070 "Unable to request BlackFin SPI DMA channel\n"); 1075 "Unable to request BlackFin SPI DMA channel\n");
1071 return -ENODEV; 1076 goto error;
1072 } 1077 }
1073 if (set_dma_callback(drv_data->dma_channel, 1078 drv_data->dma_requested = 1;
1074 bfin_spi_dma_irq_handler, drv_data) < 0) { 1079
1075 dev_dbg(&spi->dev, "Unable to set dma callback\n"); 1080 ret = set_dma_callback(drv_data->dma_channel,
1076 return -EPERM; 1081 bfin_spi_dma_irq_handler, drv_data);
1082 if (ret) {
1083 dev_err(&spi->dev, "Unable to set dma callback\n");
1084 goto error;
1077 } 1085 }
1078 dma_disable_irq(drv_data->dma_channel); 1086 dma_disable_irq(drv_data->dma_channel);
1079 drv_data->dma_requested = 1;
1080 } 1087 }
1081 1088
1082 /* 1089 if (chip->pio_interrupt && !drv_data->irq_requested) {
1083 * Notice: for blackfin, the speed_hz is the value of register 1090 ret = request_irq(drv_data->spi_irq, bfin_spi_pio_irq_handler,
1084 * SPI_BAUD, not the real baudrate 1091 IRQF_DISABLED, "BFIN_SPI", drv_data);
1085 */ 1092 if (ret) {
1086 chip->baud = hz_to_spi_baud(spi->max_speed_hz); 1093 dev_err(&spi->dev, "Unable to register spi IRQ\n");
1087 chip->flag = 1 << (spi->chip_select); 1094 goto error;
1088 chip->chip_select_num = spi->chip_select; 1095 }
1096 drv_data->irq_requested = 1;
1097 /* we use write mode, spi irq has to be disabled here */
1098 disable_irq(drv_data->spi_irq);
1099 }
1089 1100
1090 if (chip->chip_select_num == 0) { 1101 if (chip->chip_select_num >= MAX_CTRL_CS) {
1091 ret = gpio_request(chip->cs_gpio, spi->modalias); 1102 ret = gpio_request(chip->cs_gpio, spi->modalias);
1092 if (ret) { 1103 if (ret) {
1093 if (drv_data->dma_requested) 1104 dev_err(&spi->dev, "gpio_request() error\n");
1094 free_dma(drv_data->dma_channel); 1105 goto pin_error;
1095 return ret;
1096 } 1106 }
1097 gpio_direction_output(chip->cs_gpio, 1); 1107 gpio_direction_output(chip->cs_gpio, 1);
1098 } 1108 }
1099 1109
1100 switch (chip->bits_per_word) {
1101 case 8:
1102 chip->n_bytes = 1;
1103 chip->width = CFG_SPI_WORDSIZE8;
1104 chip->read = chip->cs_change_per_word ?
1105 bfin_spi_u8_cs_chg_reader : bfin_spi_u8_reader;
1106 chip->write = chip->cs_change_per_word ?
1107 bfin_spi_u8_cs_chg_writer : bfin_spi_u8_writer;
1108 chip->duplex = chip->cs_change_per_word ?
1109 bfin_spi_u8_cs_chg_duplex : bfin_spi_u8_duplex;
1110 break;
1111
1112 case 16:
1113 chip->n_bytes = 2;
1114 chip->width = CFG_SPI_WORDSIZE16;
1115 chip->read = chip->cs_change_per_word ?
1116 bfin_spi_u16_cs_chg_reader : bfin_spi_u16_reader;
1117 chip->write = chip->cs_change_per_word ?
1118 bfin_spi_u16_cs_chg_writer : bfin_spi_u16_writer;
1119 chip->duplex = chip->cs_change_per_word ?
1120 bfin_spi_u16_cs_chg_duplex : bfin_spi_u16_duplex;
1121 break;
1122
1123 default:
1124 dev_err(&spi->dev, "%d bits_per_word is not supported\n",
1125 chip->bits_per_word);
1126 if (chip_info)
1127 kfree(chip);
1128 return -ENODEV;
1129 }
1130
1131 dev_dbg(&spi->dev, "setup spi chip %s, width is %d, dma is %d\n", 1110 dev_dbg(&spi->dev, "setup spi chip %s, width is %d, dma is %d\n",
1132 spi->modalias, chip->width, chip->enable_dma); 1111 spi->modalias, spi->bits_per_word, chip->enable_dma);
1133 dev_dbg(&spi->dev, "ctl_reg is 0x%x, flag_reg is 0x%x\n", 1112 dev_dbg(&spi->dev, "ctl_reg is 0x%x, flag_reg is 0x%x\n",
1134 chip->ctl_reg, chip->flag); 1113 chip->ctl_reg, chip->flag);
1135 1114
1136 spi_set_ctldata(spi, chip); 1115 spi_set_ctldata(spi, chip);
1137 1116
1138 dev_dbg(&spi->dev, "chip select number is %d\n", chip->chip_select_num); 1117 dev_dbg(&spi->dev, "chip select number is %d\n", chip->chip_select_num);
1139 if ((chip->chip_select_num > 0) 1118 if (chip->chip_select_num < MAX_CTRL_CS) {
1140 && (chip->chip_select_num <= spi->master->num_chipselect)) 1119 ret = peripheral_request(ssel[spi->master->bus_num]
1141 peripheral_request(ssel[spi->master->bus_num] 1120 [chip->chip_select_num-1], spi->modalias);
1142 [chip->chip_select_num-1], spi->modalias); 1121 if (ret) {
1122 dev_err(&spi->dev, "peripheral_request() error\n");
1123 goto pin_error;
1124 }
1125 }
1143 1126
1127 bfin_spi_cs_enable(drv_data, chip);
1144 bfin_spi_cs_deactive(drv_data, chip); 1128 bfin_spi_cs_deactive(drv_data, chip);
1145 1129
1146 return 0; 1130 return 0;
1131
1132 pin_error:
1133 if (chip->chip_select_num >= MAX_CTRL_CS)
1134 gpio_free(chip->cs_gpio);
1135 else
1136 peripheral_free(ssel[spi->master->bus_num]
1137 [chip->chip_select_num - 1]);
1138 error:
1139 if (chip) {
1140 if (drv_data->dma_requested)
1141 free_dma(drv_data->dma_channel);
1142 drv_data->dma_requested = 0;
1143
1144 kfree(chip);
1145 /* prevent free 'chip' twice */
1146 spi_set_ctldata(spi, NULL);
1147 }
1148
1149 return ret;
1147} 1150}
1148 1151
1149/* 1152/*
@@ -1152,28 +1155,30 @@ static int bfin_spi_setup(struct spi_device *spi)
1152 */ 1155 */
1153static void bfin_spi_cleanup(struct spi_device *spi) 1156static void bfin_spi_cleanup(struct spi_device *spi)
1154{ 1157{
1155 struct chip_data *chip = spi_get_ctldata(spi); 1158 struct bfin_spi_slave_data *chip = spi_get_ctldata(spi);
1159 struct bfin_spi_master_data *drv_data = spi_master_get_devdata(spi->master);
1156 1160
1157 if (!chip) 1161 if (!chip)
1158 return; 1162 return;
1159 1163
1160 if ((chip->chip_select_num > 0) 1164 if (chip->chip_select_num < MAX_CTRL_CS) {
1161 && (chip->chip_select_num <= spi->master->num_chipselect))
1162 peripheral_free(ssel[spi->master->bus_num] 1165 peripheral_free(ssel[spi->master->bus_num]
1163 [chip->chip_select_num-1]); 1166 [chip->chip_select_num-1]);
1164 1167 bfin_spi_cs_disable(drv_data, chip);
1165 if (chip->chip_select_num == 0) 1168 } else
1166 gpio_free(chip->cs_gpio); 1169 gpio_free(chip->cs_gpio);
1167 1170
1168 kfree(chip); 1171 kfree(chip);
1172 /* prevent free 'chip' twice */
1173 spi_set_ctldata(spi, NULL);
1169} 1174}
1170 1175
1171static inline int bfin_spi_init_queue(struct driver_data *drv_data) 1176static inline int bfin_spi_init_queue(struct bfin_spi_master_data *drv_data)
1172{ 1177{
1173 INIT_LIST_HEAD(&drv_data->queue); 1178 INIT_LIST_HEAD(&drv_data->queue);
1174 spin_lock_init(&drv_data->lock); 1179 spin_lock_init(&drv_data->lock);
1175 1180
1176 drv_data->run = QUEUE_STOPPED; 1181 drv_data->running = false;
1177 drv_data->busy = 0; 1182 drv_data->busy = 0;
1178 1183
1179 /* init transfer tasklet */ 1184 /* init transfer tasklet */
@@ -1190,18 +1195,18 @@ static inline int bfin_spi_init_queue(struct driver_data *drv_data)
1190 return 0; 1195 return 0;
1191} 1196}
1192 1197
1193static inline int bfin_spi_start_queue(struct driver_data *drv_data) 1198static inline int bfin_spi_start_queue(struct bfin_spi_master_data *drv_data)
1194{ 1199{
1195 unsigned long flags; 1200 unsigned long flags;
1196 1201
1197 spin_lock_irqsave(&drv_data->lock, flags); 1202 spin_lock_irqsave(&drv_data->lock, flags);
1198 1203
1199 if (drv_data->run == QUEUE_RUNNING || drv_data->busy) { 1204 if (drv_data->running || drv_data->busy) {
1200 spin_unlock_irqrestore(&drv_data->lock, flags); 1205 spin_unlock_irqrestore(&drv_data->lock, flags);
1201 return -EBUSY; 1206 return -EBUSY;
1202 } 1207 }
1203 1208
1204 drv_data->run = QUEUE_RUNNING; 1209 drv_data->running = true;
1205 drv_data->cur_msg = NULL; 1210 drv_data->cur_msg = NULL;
1206 drv_data->cur_transfer = NULL; 1211 drv_data->cur_transfer = NULL;
1207 drv_data->cur_chip = NULL; 1212 drv_data->cur_chip = NULL;
@@ -1212,7 +1217,7 @@ static inline int bfin_spi_start_queue(struct driver_data *drv_data)
1212 return 0; 1217 return 0;
1213} 1218}
1214 1219
1215static inline int bfin_spi_stop_queue(struct driver_data *drv_data) 1220static inline int bfin_spi_stop_queue(struct bfin_spi_master_data *drv_data)
1216{ 1221{
1217 unsigned long flags; 1222 unsigned long flags;
1218 unsigned limit = 500; 1223 unsigned limit = 500;
@@ -1226,7 +1231,7 @@ static inline int bfin_spi_stop_queue(struct driver_data *drv_data)
1226 * execution path (pump_messages) would be required to call wake_up or 1231 * execution path (pump_messages) would be required to call wake_up or
1227 * friends on every SPI message. Do this instead 1232 * friends on every SPI message. Do this instead
1228 */ 1233 */
1229 drv_data->run = QUEUE_STOPPED; 1234 drv_data->running = false;
1230 while (!list_empty(&drv_data->queue) && drv_data->busy && limit--) { 1235 while (!list_empty(&drv_data->queue) && drv_data->busy && limit--) {
1231 spin_unlock_irqrestore(&drv_data->lock, flags); 1236 spin_unlock_irqrestore(&drv_data->lock, flags);
1232 msleep(10); 1237 msleep(10);
@@ -1241,7 +1246,7 @@ static inline int bfin_spi_stop_queue(struct driver_data *drv_data)
1241 return status; 1246 return status;
1242} 1247}
1243 1248
1244static inline int bfin_spi_destroy_queue(struct driver_data *drv_data) 1249static inline int bfin_spi_destroy_queue(struct bfin_spi_master_data *drv_data)
1245{ 1250{
1246 int status; 1251 int status;
1247 1252
@@ -1259,14 +1264,14 @@ static int __init bfin_spi_probe(struct platform_device *pdev)
1259 struct device *dev = &pdev->dev; 1264 struct device *dev = &pdev->dev;
1260 struct bfin5xx_spi_master *platform_info; 1265 struct bfin5xx_spi_master *platform_info;
1261 struct spi_master *master; 1266 struct spi_master *master;
1262 struct driver_data *drv_data = 0; 1267 struct bfin_spi_master_data *drv_data;
1263 struct resource *res; 1268 struct resource *res;
1264 int status = 0; 1269 int status = 0;
1265 1270
1266 platform_info = dev->platform_data; 1271 platform_info = dev->platform_data;
1267 1272
1268 /* Allocate master with space for drv_data */ 1273 /* Allocate master with space for drv_data */
1269 master = spi_alloc_master(dev, sizeof(struct driver_data) + 16); 1274 master = spi_alloc_master(dev, sizeof(*drv_data));
1270 if (!master) { 1275 if (!master) {
1271 dev_err(&pdev->dev, "can not alloc spi_master\n"); 1276 dev_err(&pdev->dev, "can not alloc spi_master\n");
1272 return -ENOMEM; 1277 return -ENOMEM;
@@ -1302,11 +1307,19 @@ static int __init bfin_spi_probe(struct platform_device *pdev)
1302 goto out_error_ioremap; 1307 goto out_error_ioremap;
1303 } 1308 }
1304 1309
1305 drv_data->dma_channel = platform_get_irq(pdev, 0); 1310 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1306 if (drv_data->dma_channel < 0) { 1311 if (res == NULL) {
1307 dev_err(dev, "No DMA channel specified\n"); 1312 dev_err(dev, "No DMA channel specified\n");
1308 status = -ENOENT; 1313 status = -ENOENT;
1309 goto out_error_no_dma_ch; 1314 goto out_error_free_io;
1315 }
1316 drv_data->dma_channel = res->start;
1317
1318 drv_data->spi_irq = platform_get_irq(pdev, 0);
1319 if (drv_data->spi_irq < 0) {
1320 dev_err(dev, "No spi pio irq specified\n");
1321 status = -ENOENT;
1322 goto out_error_free_io;
1310 } 1323 }
1311 1324
1312 /* Initial and start queue */ 1325 /* Initial and start queue */
@@ -1328,6 +1341,12 @@ static int __init bfin_spi_probe(struct platform_device *pdev)
1328 goto out_error_queue_alloc; 1341 goto out_error_queue_alloc;
1329 } 1342 }
1330 1343
1344 /* Reset SPI registers. If these registers were used by the boot loader,
1345 * the sky may fall on your head if you enable the dma controller.
1346 */
1347 write_CTRL(drv_data, BIT_CTL_CPHA | BIT_CTL_MASTER);
1348 write_FLAG(drv_data, 0xFF00);
1349
1331 /* Register with the SPI framework */ 1350 /* Register with the SPI framework */
1332 platform_set_drvdata(pdev, drv_data); 1351 platform_set_drvdata(pdev, drv_data);
1333 status = spi_register_master(master); 1352 status = spi_register_master(master);
@@ -1343,7 +1362,7 @@ static int __init bfin_spi_probe(struct platform_device *pdev)
1343 1362
1344out_error_queue_alloc: 1363out_error_queue_alloc:
1345 bfin_spi_destroy_queue(drv_data); 1364 bfin_spi_destroy_queue(drv_data);
1346out_error_no_dma_ch: 1365out_error_free_io:
1347 iounmap((void *) drv_data->regs_base); 1366 iounmap((void *) drv_data->regs_base);
1348out_error_ioremap: 1367out_error_ioremap:
1349out_error_get_res: 1368out_error_get_res:
@@ -1355,7 +1374,7 @@ out_error_get_res:
1355/* stop hardware and remove the driver */ 1374/* stop hardware and remove the driver */
1356static int __devexit bfin_spi_remove(struct platform_device *pdev) 1375static int __devexit bfin_spi_remove(struct platform_device *pdev)
1357{ 1376{
1358 struct driver_data *drv_data = platform_get_drvdata(pdev); 1377 struct bfin_spi_master_data *drv_data = platform_get_drvdata(pdev);
1359 int status = 0; 1378 int status = 0;
1360 1379
1361 if (!drv_data) 1380 if (!drv_data)
@@ -1375,6 +1394,11 @@ static int __devexit bfin_spi_remove(struct platform_device *pdev)
1375 free_dma(drv_data->dma_channel); 1394 free_dma(drv_data->dma_channel);
1376 } 1395 }
1377 1396
1397 if (drv_data->irq_requested) {
1398 free_irq(drv_data->spi_irq, drv_data);
1399 drv_data->irq_requested = 0;
1400 }
1401
1378 /* Disconnect from the SPI framework */ 1402 /* Disconnect from the SPI framework */
1379 spi_unregister_master(drv_data->master); 1403 spi_unregister_master(drv_data->master);
1380 1404
@@ -1389,26 +1413,32 @@ static int __devexit bfin_spi_remove(struct platform_device *pdev)
1389#ifdef CONFIG_PM 1413#ifdef CONFIG_PM
1390static int bfin_spi_suspend(struct platform_device *pdev, pm_message_t state) 1414static int bfin_spi_suspend(struct platform_device *pdev, pm_message_t state)
1391{ 1415{
1392 struct driver_data *drv_data = platform_get_drvdata(pdev); 1416 struct bfin_spi_master_data *drv_data = platform_get_drvdata(pdev);
1393 int status = 0; 1417 int status = 0;
1394 1418
1395 status = bfin_spi_stop_queue(drv_data); 1419 status = bfin_spi_stop_queue(drv_data);
1396 if (status != 0) 1420 if (status != 0)
1397 return status; 1421 return status;
1398 1422
1399 /* stop hardware */ 1423 drv_data->ctrl_reg = read_CTRL(drv_data);
1400 bfin_spi_disable(drv_data); 1424 drv_data->flag_reg = read_FLAG(drv_data);
1425
1426 /*
1427 * reset SPI_CTL and SPI_FLG registers
1428 */
1429 write_CTRL(drv_data, BIT_CTL_CPHA | BIT_CTL_MASTER);
1430 write_FLAG(drv_data, 0xFF00);
1401 1431
1402 return 0; 1432 return 0;
1403} 1433}
1404 1434
1405static int bfin_spi_resume(struct platform_device *pdev) 1435static int bfin_spi_resume(struct platform_device *pdev)
1406{ 1436{
1407 struct driver_data *drv_data = platform_get_drvdata(pdev); 1437 struct bfin_spi_master_data *drv_data = platform_get_drvdata(pdev);
1408 int status = 0; 1438 int status = 0;
1409 1439
1410 /* Enable the SPI interface */ 1440 write_CTRL(drv_data, drv_data->ctrl_reg);
1411 bfin_spi_enable(drv_data); 1441 write_FLAG(drv_data, drv_data->flag_reg);
1412 1442
1413 /* Start the queue running */ 1443 /* Start the queue running */
1414 status = bfin_spi_start_queue(drv_data); 1444 status = bfin_spi_start_queue(drv_data);
@@ -1439,7 +1469,7 @@ static int __init bfin_spi_init(void)
1439{ 1469{
1440 return platform_driver_probe(&bfin_spi_driver, bfin_spi_probe); 1470 return platform_driver_probe(&bfin_spi_driver, bfin_spi_probe);
1441} 1471}
1442module_init(bfin_spi_init); 1472subsys_initcall(bfin_spi_init);
1443 1473
1444static void __exit bfin_spi_exit(void) 1474static void __exit bfin_spi_exit(void)
1445{ 1475{