12 #include <linux/module.h>
14 #include <linux/device.h>
19 #include <linux/errno.h>
29 #include <asm/cacheflush.h>
31 #define DRV_NAME "bfin-sport-spi"
32 #define DRV_DESC "SPI bus via the Blackfin SPORT"
128 bfin_sport_hz_to_spi_baud(
u32 speed_hz)
131 int div = (sclk / (2 * speed_hz)) - 1;
136 clk = sclk / (2 * (div + 1));
175 while (drv_data->
tx < drv_data->
tx_end) {
177 bfin_sport_spi_stat_poll_complete(drv_data);
187 while (drv_data->
rx < drv_data->
rx_end) {
189 bfin_sport_spi_stat_poll_complete(drv_data);
197 while (drv_data->
rx < drv_data->
rx_end) {
199 bfin_sport_spi_stat_poll_complete(drv_data);
205 .write = bfin_sport_spi_u8_writer,
206 .read = bfin_sport_spi_u8_reader,
207 .duplex = bfin_sport_spi_u8_duplex,
215 while (drv_data->
tx < drv_data->
tx_end) {
217 bfin_sport_spi_stat_poll_complete(drv_data);
227 while (drv_data->
rx < drv_data->
rx_end) {
229 bfin_sport_spi_stat_poll_complete(drv_data);
237 while (drv_data->
rx < drv_data->
rx_end) {
239 bfin_sport_spi_stat_poll_complete(drv_data);
245 .write = bfin_sport_spi_u16_writer,
246 .read = bfin_sport_spi_u16_reader,
247 .duplex = bfin_sport_spi_u16_duplex,
256 bfin_sport_spi_disable(drv_data);
257 dev_dbg(drv_data->
dev,
"restoring spi ctl state\n");
266 bfin_sport_spi_cs_active(chip);
305 spin_unlock_irqrestore(&drv_data->
lock, flags);
308 bfin_sport_spi_cs_deactive(chip);
315 sport_err_handler(
int irq,
void *
dev_id)
320 dev_dbg(drv_data->
dev,
"%s enter\n", __func__);
327 bfin_sport_spi_disable(drv_data);
328 dev_err(drv_data->
dev,
"status error:%s%s%s%s\n",
329 status &
TOVF ?
" TOVF" :
"",
330 status &
TUVF ?
" TUVF" :
"",
331 status &
ROVF ?
" ROVF" :
"",
332 status &
RUVF ?
" RUVF" :
"");
339 bfin_sport_spi_pump_transfers(
unsigned long data)
346 unsigned int bits_per_word;
347 u32 tranf_success = 1;
357 transfer_speed = bfin_sport_hz_to_spi_baud(transfer->
speed_hz);
359 transfer_speed = chip->
baud;
369 dev_dbg(drv_data->
dev,
"transfer: we've hit an error\n");
371 bfin_sport_spi_giveback(drv_data);
377 dev_dbg(drv_data->
dev,
"transfer: all done!\n");
379 bfin_sport_spi_giveback(drv_data);
385 dev_dbg(drv_data->
dev,
"transfer: still running ...\n");
392 if (transfer->
len == 0) {
394 drv_data->
state = bfin_sport_spi_next_transfer(drv_data);
400 drv_data->
tx = (
void *)transfer->
tx_buf;
402 dev_dbg(drv_data->
dev,
"tx_buf is %p, tx_end is %p\n",
411 dev_dbg(drv_data->
dev,
"rx_buf is %p, rx_end is %p\n",
420 message->
spi->bits_per_word ? : 8;
421 if (bits_per_word % 16 == 0)
422 drv_data->
ops = &bfin_sport_transfer_ops_u16;
424 drv_data->
ops = &bfin_sport_transfer_ops_u8;
432 bfin_sport_spi_cs_active(chip);
435 "now pumping a transfer: width is %d, len is %d\n",
436 bits_per_word, transfer->
len);
439 dev_dbg(drv_data->
dev,
"doing IO transfer\n");
441 bfin_sport_spi_enable(drv_data);
446 drv_data->
ops->duplex(drv_data);
448 if (drv_data->
tx != drv_data->
tx_end)
450 }
else if (drv_data->
tx !=
NULL) {
453 drv_data->
ops->write(drv_data);
455 if (drv_data->
tx != drv_data->
tx_end)
457 }
else if (drv_data->
rx !=
NULL) {
460 drv_data->
ops->read(drv_data);
461 if (drv_data->
rx != drv_data->
rx_end)
464 bfin_sport_spi_disable(drv_data);
466 if (!tranf_success) {
473 drv_data->
state = bfin_sport_spi_next_transfer(drv_data);
475 bfin_sport_spi_cs_deactive(chip);
494 if (list_empty(&drv_data->
queue) || !drv_data->
run) {
497 spin_unlock_irqrestore(&drv_data->
lock, flags);
503 spin_unlock_irqrestore(&drv_data->
lock, flags);
516 list_del_init(&drv_data->
cur_msg->queue);
522 bfin_sport_spi_restore_state(drv_data);
523 dev_dbg(drv_data->
dev,
"got a message to pump, "
524 "state is set to: baud %d, cs_gpio %i, ctl 0x%x\n",
529 "the first transfer len is %d\n",
536 spin_unlock_irqrestore(&drv_data->
lock, flags);
551 if (!drv_data->
run) {
552 spin_unlock_irqrestore(&drv_data->
lock, flags);
560 dev_dbg(&spi->
dev,
"adding an msg in transfer()\n");
563 if (drv_data->
run && !drv_data->
busy)
566 spin_unlock_irqrestore(&drv_data->
lock, flags);
579 chip = spi_get_ctldata(spi);
583 chip = first = kzalloc(
sizeof(*chip),
GFP_KERNEL);
596 dev_err(&spi->
dev,
"don't set ctl_reg/enable_dma fields");
605 dev_err(&spi->
dev,
"%d bits_per_word is not supported\n",
635 dev_dbg(&spi->
dev,
"setup spi chip %s, width is %d\n",
637 dev_dbg(&spi->
dev,
"ctl_reg is 0x%x, GPIO is %i\n",
640 spi_set_ctldata(spi, chip);
642 bfin_sport_spi_cs_deactive(chip);
656 bfin_sport_spi_cleanup(
struct spi_device *spi)
671 INIT_LIST_HEAD(&drv_data->
queue);
674 drv_data->
run =
false;
679 bfin_sport_spi_pump_transfers, (
unsigned long)drv_data);
698 if (drv_data->
run || drv_data->
busy) {
699 spin_unlock_irqrestore(&drv_data->
lock, flags);
703 drv_data->
run =
true;
707 spin_unlock_irqrestore(&drv_data->
lock, flags);
718 unsigned limit = 500;
729 drv_data->
run =
false;
730 while (!list_empty(&drv_data->
queue) && drv_data->
busy && limit--) {
731 spin_unlock_irqrestore(&drv_data->
lock, flags);
736 if (!list_empty(&drv_data->
queue) || drv_data->
busy)
739 spin_unlock_irqrestore(&drv_data->
lock, flags);
749 status = bfin_sport_spi_stop_queue(drv_data);
773 dev_err(dev,
"cannot alloc spi_master\n");
777 drv_data = spi_master_get_devdata(master);
785 master->
cleanup = bfin_sport_spi_cleanup;
786 master->
setup = bfin_sport_spi_setup;
787 master->
transfer = bfin_sport_spi_transfer;
792 dev_err(dev,
"cannot get IORESOURCE_MEM\n");
794 goto out_error_get_res;
799 dev_err(dev,
"cannot map registers\n");
801 goto out_error_ioremap;
806 dev_err(dev,
"cannot get IORESOURCE_IRQ\n");
808 goto out_error_get_ires;
813 status = bfin_sport_spi_init_queue(drv_data);
815 dev_err(dev,
"problem initializing queue\n");
816 goto out_error_queue_alloc;
819 status = bfin_sport_spi_start_queue(drv_data);
821 dev_err(dev,
"problem starting queue\n");
822 goto out_error_queue_alloc;
826 0,
"sport_spi_err", drv_data);
828 dev_err(dev,
"unable to request sport err irq\n");
834 dev_err(dev,
"requesting peripherals failed\n");
835 goto out_error_peripheral;
839 platform_set_drvdata(pdev, drv_data);
842 dev_err(dev,
"problem registering spi master\n");
843 goto out_error_master;
851 out_error_peripheral:
854 out_error_queue_alloc:
855 bfin_sport_spi_destroy_queue(drv_data);
860 spi_master_put(master);
876 status = bfin_sport_spi_destroy_queue(drv_data);
881 bfin_sport_spi_disable(drv_data);
889 platform_set_drvdata(pdev,
NULL);
901 status = bfin_sport_spi_stop_queue(drv_data);
906 bfin_sport_spi_disable(drv_data);
918 bfin_sport_spi_enable(drv_data);
921 status = bfin_sport_spi_start_queue(drv_data);
923 dev_err(drv_data->
dev,
"problem resuming queue\n");
928 # define bfin_sport_spi_suspend NULL
929 # define bfin_sport_spi_resume NULL
937 .probe = bfin_sport_spi_probe,