18 #include <linux/module.h>
29 #include <mach/platform.h>
30 #include <mach/stmp3xxx.h>
32 #include <mach/regs-ssp.h>
33 #include <mach/regs-apbh.h>
41 #define STMP_SPI_TIMEOUT (2 * HZ)
50 struct stmp3xxx_dma_descriptor
d;
69 #define busy_wait(cond) \
71 unsigned long end_jiffies = jiffies + STMP_SPI_TIMEOUT; \
72 bool succeeded = false; \
79 } while (time_before(jiffies, end_jiffies)); \
87 static int stmp_spi_init_hw(
struct stmp_spi *
ss)
92 err = stmp3xxx_request_pin_group(pins, dev_name(ss->
master_dev));
97 if (IS_ERR(ss->
clk)) {
98 err = PTR_ERR(ss->
clk);
103 stmp3xxx_reset_block(ss->
regs,
false);
104 stmp3xxx_dma_reset_channel(ss->
dma);
109 stmp3xxx_release_pin_group(pins, dev_name(ss->
master_dev));
114 static void stmp_spi_release_hw(
struct stmp_spi *
ss)
118 if (ss->
clk && !IS_ERR(ss->
clk)) {
122 stmp3xxx_release_pin_group(pins, dev_name(ss->
master_dev));
150 dev_err(&spi->
dev,
"Cannot continue with zero clock\n");
154 if (bits_per_word != 8) {
155 dev_err(&spi->
dev,
"%s, unsupported bits_per_word=%d\n",
156 __func__, bits_per_word);
160 dev_dbg(&spi->
dev,
"Requested clk rate = %uHz, max = %uHz/%d = %uHz\n",
165 dev_err(&spi->
dev,
"%s, unsupported clock rate %uHz\n",
173 BF(rate - 1, SSP_TIMING_CLOCK_RATE),
177 BF(4 , SSP_CTRL1_WORD_LENGTH) |
186 static int stmp_spi_setup(
struct spi_device *spi)
192 dev_err(&spi->
dev,
"%s, unsupported bits_per_word=%d\n",
199 static inline u32 stmp_spi_cs(
unsigned cs)
205 static int stmp_spi_txrx_dma(
struct stmp_spi *ss,
int cs,
219 c0 |= stmp_spi_cs(cs);
221 c0 |=
BF(len, SSP_CTRL0_XFER_COUNT);
227 BF(len, APBH_CHn_CMD_XFER_COUNT) |
228 BF(1, APBH_CHn_CMD_CMDWORDS) |
229 BM_APBH_CHn_CMD_WAIT4ENDCMD |
230 BM_APBH_CHn_CMD_IRQONCMPLT |
231 BF(write ? BV_APBH_CHn_CMD_COMMAND__DMA_READ :
232 BV_APBH_CHn_CMD_COMMAND__DMA_WRITE,
233 APBH_CHn_CMD_COMMAND);
234 ss->
d.command->pio_words[0] = c0;
235 ss->
d.command->buf_ptr = spi_buf_dma;
237 stmp3xxx_dma_reset_channel(ss->
dma);
238 stmp3xxx_dma_clear_interrupt(ss->
dma);
239 stmp3xxx_dma_enable_interrupt(ss->
dma);
240 init_completion(&ss->
done);
241 stmp3xxx_dma_go(ss->
dma, &ss->
d, 1);
253 static inline void stmp_spi_enable(
struct stmp_spi *ss)
259 static inline void stmp_spi_disable(
struct stmp_spi *ss)
265 static int stmp_spi_txrx_pio(
struct stmp_spi *ss,
int cs,
266 unsigned char *buf,
int len,
267 bool first,
bool last,
bool write)
275 if (last && len <= 0)
276 stmp_spi_disable(ss);
327 cs = m->
spi->chip_select;
335 stmp_spi_setup_transfer(m->
spi, t);
340 "Message with t->cs_change has been skipped\n");
346 stmp_spi_txrx_pio(ss, cs, (
void *)t->
tx_buf,
347 t->
len, first, last,
true) :
352 print_hex_dump_bytes(
"Tx ",
361 stmp_spi_txrx_pio(ss, cs, t->
rx_buf,
362 t->
len, first, last,
false) :
363 stmp_spi_txrx_dma(ss, cs, t->
rx_buf,
367 print_hex_dump_bytes(
"Rx ",
395 while (!list_empty(&ss->
queue)) {
397 list_del_init(&m->
queue);
398 spin_unlock_irqrestore(&ss->
lock, flags);
400 m->
status = stmp_spi_handle_message(ss, m);
405 spin_unlock_irqrestore(&ss->
lock, flags);
426 spin_unlock_irqrestore(&ss->
lock, flags);
434 stmp3xxx_dma_clear_interrupt(ss->
dma);
461 if (master ==
NULL) {
467 ss = spi_master_get_devdata(master);
468 platform_set_drvdata(dev, master);
486 INIT_LIST_HEAD(&ss->
queue);
494 master->
transfer = stmp_spi_transfer;
495 master->
setup = stmp_spi_setup;
518 err = stmp3xxx_dma_request(ss->
dma, &dev->
dev, dev_name(&dev->
dev));
522 err = stmp3xxx_dma_allocate_command(ss->
dma, &ss->
d);
530 err = stmp_spi_init_hw(ss);
532 dev_dbg(&dev->
dev,
"cannot initialize hardware\n");
533 goto out_free_dma_desc;
542 dev_info(&dev->
dev,
"max possible speed %d = %ld/%d kHz\n",
547 dev_name(&dev->
dev), ss);
549 dev_dbg(&dev->
dev,
"request_irq failed, %d\n", err);
555 dev_name(&dev->
dev), ss);
557 dev_dbg(&dev->
dev,
"request_irq(error) failed, %d\n", err);
563 dev_dbg(&dev->
dev,
"cannot register spi master, %d\n", err);
566 dev_info(&dev->
dev,
"at (mapped) 0x%08X, irq=%d, bus %d, %s mode\n",
568 pio ?
"PIO" :
"DMA");
576 stmp3xxx_dma_free_command(ss->
dma, &ss->
d);
578 stmp3xxx_dma_release(ss->
dma);
580 stmp_spi_release_hw(ss);
586 platform_set_drvdata(dev,
NULL);
587 spi_master_put(master);
597 master = spi_master_get(platform_get_drvdata(dev));
598 ss = spi_master_get_devdata(master);
604 stmp3xxx_dma_free_command(ss->
dma, &ss->
d);
605 stmp3xxx_dma_release(ss->
dma);
606 stmp_spi_release_hw(ss);
609 spi_master_put(master);
619 master = platform_get_drvdata(pdev);
620 ss = spi_master_get_devdata(master);
633 master = platform_get_drvdata(pdev);
634 ss = spi_master_get_devdata(master);
637 stmp3xxx_reset_block(ss->
regs,
false);
644 #define stmp_spi_suspend NULL
645 #define stmp_spi_resume NULL
649 .probe = stmp_spi_probe,
652 .name =
"stmp3xxx_ssp",