21 #include <linux/pci.h>
22 #include <linux/wait.h>
25 #include <linux/sched.h>
27 #include <linux/module.h>
28 #include <linux/device.h>
36 #define PCH_SPBRR 0x04
38 #define PCH_SPDWR 0x0C
39 #define PCH_SPDRR 0x10
40 #define PCH_SSNXCR 0x18
42 #define PCH_ADDRESS_SIZE 0x20
44 #define PCH_SPSR_TFD 0x000007C0
45 #define PCH_SPSR_RFD 0x0000F800
47 #define PCH_READABLE(x) (((x) & PCH_SPSR_RFD)>>11)
48 #define PCH_WRITABLE(x) (((x) & PCH_SPSR_TFD)>>6)
50 #define PCH_RX_THOLD 7
51 #define PCH_RX_THOLD_MAX 15
53 #define PCH_TX_THOLD 2
55 #define PCH_MAX_BAUDRATE 5000000
56 #define PCH_MAX_FIFO_DEPTH 16
58 #define STATUS_RUNNING 1
59 #define STATUS_EXITING 2
60 #define PCH_SLEEP_TIME 10
63 #define SSN_HIGH 0x03U
64 #define SSN_NO_CONTROL 0x00U
65 #define PCH_MAX_CS 0xFF
66 #define PCI_DEVICE_ID_GE_SPI 0x8816
68 #define SPCR_SPE_BIT (1 << 0)
69 #define SPCR_MSTR_BIT (1 << 1)
70 #define SPCR_LSBF_BIT (1 << 4)
71 #define SPCR_CPHA_BIT (1 << 5)
72 #define SPCR_CPOL_BIT (1 << 6)
73 #define SPCR_TFIE_BIT (1 << 8)
74 #define SPCR_RFIE_BIT (1 << 9)
75 #define SPCR_FIE_BIT (1 << 10)
76 #define SPCR_ORIE_BIT (1 << 11)
77 #define SPCR_MDFIE_BIT (1 << 12)
78 #define SPCR_FICLR_BIT (1 << 24)
79 #define SPSR_TFI_BIT (1 << 0)
80 #define SPSR_RFI_BIT (1 << 1)
81 #define SPSR_FI_BIT (1 << 2)
82 #define SPSR_ORF_BIT (1 << 3)
83 #define SPBRR_SIZE_BIT (1 << 10)
85 #define PCH_ALL (SPCR_TFIE_BIT|SPCR_RFIE_BIT|SPCR_FIE_BIT|\
86 SPCR_ORIE_BIT|SPCR_MDFIE_BIT)
88 #define SPCR_RFIC_FIELD 20
89 #define SPCR_TFIC_FIELD 16
91 #define MASK_SPBRR_SPBR_BITS ((1 << 10) - 1)
92 #define MASK_RFIC_SPCR_BITS (0xf << SPCR_RFIC_FIELD)
93 #define MASK_TFIC_SPCR_BITS (0xf << SPCR_TFIC_FIELD)
95 #define PCH_CLOCK_HZ 50000000
96 #define PCH_MAX_SPBR 1023
99 #define PCI_VENDOR_ID_ROHM 0x10DB
100 #define PCI_DEVICE_ID_ML7213_SPI 0x802c
101 #define PCI_DEVICE_ID_ML7223_SPI 0x800F
102 #define PCI_DEVICE_ID_ML7831_SPI 0x8816
111 #define PCH_SPI_MAX_DEV 2
113 #define PCH_BUF_SIZE 4096
114 #define PCH_DMA_TRANS_SIZE 12
245 static inline u32 pch_spi_readreg(
struct spi_master *master,
int idx)
247 struct pch_spi_data *data = spi_master_get_devdata(master);
251 static inline void pch_spi_setclr_reg(
struct spi_master *master,
int idx,
254 u32 tmp = pch_spi_readreg(master, idx);
255 tmp = (tmp & ~clr) |
set;
256 pch_spi_writereg(master, idx, tmp);
259 static void pch_spi_set_master_mode(
struct spi_master *master)
268 static void pch_spi_clear_fifo(
struct spi_master *master)
274 static void pch_spi_handler_sub(
struct pch_spi_data *data,
u32 reg_spsr_val,
300 for (read_cnt = 0; (read_cnt < n_read); read_cnt++) {
301 pkt_rx_buffer[rx_index++] =
ioread32(spdrr);
302 if (tx_index < bpw_len)
303 iowrite32(pkt_tx_buff[tx_index++], spdwr);
324 if ((tx_index == bpw_len) && (rx_index == tx_index)) {
336 "%s : Transfer is not completed",
359 "%s returning due to suspend\n", __func__);
369 dev_err(&board_dat->
pdev->dev,
"%s Over run error\n", __func__);
385 pch_spi_handler_sub(data, reg_spsr_val, io_remap_addr);
389 dev_dbg(&board_dat->
pdev->dev,
"%s EXIT return value=%d\n",
400 static void pch_spi_set_baud_rate(
struct spi_master *master,
u32 speed_hz)
416 static void pch_spi_set_bits_per_word(
struct spi_master *master,
419 if (bits_per_word == 8)
433 dev_dbg(&spi->
dev,
"%s SPBRR content =%x setting baud rate=%d\n",
451 pch_spi_clear_fifo(spi->
master);
458 static void pch_spi_reset(
struct spi_master *master)
461 pch_spi_writereg(master,
PCH_SRST, 0x1);
464 pch_spi_writereg(master,
PCH_SRST, 0x0);
467 static int pch_spi_setup(
struct spi_device *pspi)
472 dev_dbg(&pspi->
dev,
"%s 8 bits per word\n", __func__);
476 dev_err(&pspi->
dev,
"%s Invalid bits per word\n", __func__);
486 dev_dbg(&pspi->
dev,
"%s MODE = %x\n", __func__,
502 dev_err(&pspi->
dev,
"%s list empty\n", __func__);
508 dev_err(&pspi->
dev,
"%s pch_spi_transfer maxspeed=%d\n",
514 dev_dbg(&pspi->
dev,
"%s Transfer List not empty. "
515 "Transfer Speed is set.\n", __func__);
522 "%s Tx and Rx buffer NULL\n", __func__);
524 goto err_return_spinlock;
527 if (!transfer->
len) {
528 dev_err(&pspi->
dev,
"%s Transfer length invalid\n",
531 goto err_return_spinlock;
534 dev_dbg(&pspi->
dev,
"%s Tx/Rx buffer valid. Transfer length"
535 " valid\n", __func__);
547 "%s Invalid bits per word\n", __func__);
548 goto err_return_spinlock;
552 spin_unlock_irqrestore(&data->
lock, flags);
556 dev_err(&pspi->
dev,
"%s status = STATUS_EXITING.\n", __func__);
563 dev_err(&pspi->
dev,
"%s suspend; returning EINVAL\n", __func__);
576 spin_unlock_irqrestore(&data->
lock, flags);
578 dev_dbg(&pspi->
dev,
"%s - Invoked list_add_tail\n", __func__);
582 dev_dbg(&pspi->
dev,
"%s - Invoked queue work\n", __func__);
587 dev_dbg(&pspi->
dev,
"%s RETURN=%d\n", __func__, retval);
590 dev_dbg(&pspi->
dev,
"%s RETURN=%d\n", __func__, retval);
591 spin_unlock_irqrestore(&data->
lock, flags);
595 static inline void pch_spi_select_chip(
struct pch_spi_data *data,
600 dev_dbg(&pspi->
dev,
"%s : different slave\n", __func__);
609 dev_dbg(&pspi->
dev,
"%s :Invoking pch_spi_setup_transfer\n", __func__);
610 pch_spi_setup_transfer(pspi);
613 static void pch_spi_set_tx(
struct pch_spi_data *data,
int *bpw)
624 dev_dbg(&data->
master->dev,
"%s:setting baud rate\n", __func__);
631 dev_dbg(&data->
master->dev,
"%s:set bits per word\n", __func__);
632 pch_spi_set_bits_per_word(data->
master,
658 dev_err(&data->
master->dev,
"%s :kzalloc failed\n", __func__);
666 list_del_init(&pmsg->
queue);
675 for (j = 0; j < data->
bpw_len; j++)
679 for (j = 0; j < data->
bpw_len; j++)
689 dev_dbg(&data->
master->dev,
"\n%s:Pulling down SSN low - writing "
690 "0x2 to SSNXCR\n", __func__);
693 for (j = 0; j < n_writes; j++)
704 static void pch_spi_nomore_transfer(
struct pch_spi_data *data)
714 "%s:Invoking callback of SPI core\n", __func__);
722 "%s:data->bcurrent_msg_processing = false\n", __func__);
729 if ((list_empty(&data->
queue) == 0) &&
736 dev_dbg(&data->
master->dev,
"%s:Invoke queue_work\n", __func__);
738 }
else if (data->
board_dat->suspend_sts ||
741 "%s suspend/remove initiated, flushing queue\n",
750 list_del_init(&pmsg->
queue);
776 "%s:waiting for transfer to get over\n", __func__);
781 pch_spi_writereg(data->
master, PCH_SPSR,
782 pch_spi_readreg(data->
master, PCH_SPSR));
786 pch_spi_clear_fifo(data->
master);
789 static void pch_spi_copy_rx_data(
struct pch_spi_data *data,
int bpw)
801 for (j = 0; j < data->
bpw_len; j++)
805 for (j = 0; j < data->
bpw_len; j++)
810 static void pch_spi_copy_rx_data_for_dma(
struct pch_spi_data *data,
int bpw)
815 const u8 *rx_dma_buf;
816 const u16 *rx_dma_sbuf;
824 rx_dma_buf = data->
dma.rx_buf_virt;
825 for (j = 0; j < data->
bpw_len; j++)
826 *rx_buf++ = *rx_dma_buf++ & 0xFF;
830 rx_dma_sbuf = data->
dma.rx_buf_virt;
831 for (j = 0; j < data->
bpw_len; j++)
832 *rx_sbuf++ = *rx_dma_sbuf++;
837 static int pch_spi_start_transfer(
struct pch_spi_data *data)
850 spin_unlock_irqrestore(&data->
lock, flags);
855 "%s:waiting for transfer to get over\n", __func__);
861 "%s wait-event timeout\n", __func__);
882 pch_spi_writereg(data->
master, PCH_SPSR,
883 pch_spi_readreg(data->
master, PCH_SPSR));
885 pch_spi_clear_fifo(data->
master);
887 spin_unlock_irqrestore(&data->
lock, flags);
892 static void pch_dma_rx_complete(
void *
arg)
914 static void pch_spi_request_dma(
struct pch_spi_data *data,
int bpw)
945 "ERROR: dma_request_channel FAILS(Tx)\n");
960 "ERROR: dma_request_channel FAILS(Rx)\n");
969 static void pch_spi_release_dma(
struct pch_spi_data *data)
985 static void pch_spi_handle_dma(
struct pch_spi_data *data,
int *bpw)
1006 dev_dbg(&data->
master->dev,
"%s:setting baud rate\n", __func__);
1009 spin_unlock_irqrestore(&data->
lock, flags);
1016 dev_dbg(&data->
master->dev,
"%s:set bits per word\n", __func__);
1018 pch_spi_set_bits_per_word(data->
master,
1020 spin_unlock_irqrestore(&data->
lock, flags);
1037 for (i = 0; i < data->
bpw_len; i++)
1038 *tx_dma_buf++ = *tx_buf++;
1042 for (i = 0; i < data->
bpw_len; i++)
1043 *tx_dma_sbuf++ = *tx_sbuf++;
1063 __func__, num, size, rem);
1072 spin_unlock_irqrestore(&data->
lock, flags);
1079 for (i = 0; i < num; i++, sg++) {
1080 if (i == (num - 2)) {
1086 }
else if (i == (num - 1)) {
1087 sg->
offset = size * (i - 1) + rem;
1102 desc_rx = dmaengine_prep_slave_sg(dma->
chan_rx, sg,
1106 dev_err(&data->
master->dev,
"%s:device_prep_slave_sg Failed\n",
1111 desc_rx->
callback = pch_dma_rx_complete;
1119 if (data->
bpw_len % PCH_DMA_TRANS_SIZE > 4) {
1120 num = data->
bpw_len / PCH_DMA_TRANS_SIZE + 1;
1124 rem = data->
bpw_len % PCH_DMA_TRANS_SIZE +
1125 PCH_DMA_TRANS_SIZE -
head;
1139 for (i = 0; i < num; i++, sg++) {
1145 }
else if (i == (num - 1)) {
1161 desc_tx = dmaengine_prep_slave_sg(dma->
chan_tx,
1165 dev_err(&data->
master->dev,
"%s:device_prep_slave_sg Failed\n",
1175 dev_dbg(&data->
master->dev,
"\n%s:Pulling down SSN low - writing "
1176 "0x2 to SSNXCR\n", __func__);
1182 spin_unlock_irqrestore(&data->
lock, flags);
1188 static void pch_spi_process_messages(
struct work_struct *pwork)
1195 dev_dbg(&data->
master->dev,
"%s data initialized\n", __func__);
1197 spin_lock(&data->
lock);
1201 "flushing queue\n", __func__);
1206 spin_unlock(&data->
lock);
1208 spin_lock(&data->
lock);
1212 list_del_init(&pmsg->
queue);
1215 spin_unlock(&data->
lock);
1221 "%s Set data->bcurrent_msg_processing= true\n", __func__);
1231 pch_spi_select_chip(data, data->
current_msg->spi);
1233 spin_unlock(&data->
lock);
1236 pch_spi_request_dma(data,
1244 spin_lock(&data->
lock);
1250 ":Getting 1st transfer message\n", __func__);
1256 ":Getting next transfer message\n", __func__);
1258 spin_unlock(&data->
lock);
1266 char *save_rx_buf = data->
cur_trans->rx_buf;
1267 for (i = 0; i <
cnt; i ++) {
1268 pch_spi_handle_dma(data, &bpw);
1269 if (!pch_spi_start_transfer(data)) {
1279 pch_spi_copy_rx_data_for_dma(data, bpw);
1283 pch_spi_set_tx(data, &bpw);
1284 pch_spi_set_ir(data);
1285 pch_spi_copy_rx_data(data, bpw);
1296 "%s:data->current_msg->actual_length=%d\n",
1302 "delay in usec=%d\n", __func__,
1307 spin_lock(&data->
lock);
1310 if ((data->
cur_trans->transfer_list.next) ==
1312 pch_spi_nomore_transfer(data);
1315 spin_unlock(&data->
lock);
1322 pch_spi_release_dma(data);
1328 dev_dbg(&board_dat->
pdev->dev,
"%s ENTRY\n", __func__);
1335 "%s destroy_workqueue invoked successfully\n",
1345 dev_dbg(&board_dat->
pdev->dev,
"%s ENTRY\n", __func__);
1351 "%s create_singlet hread_workqueue failed\n", __func__);
1357 pch_spi_reset(data->
master);
1359 "%s pch_spi_reset invoked successfully\n", __func__);
1361 dev_dbg(&board_dat->
pdev->dev,
"%s data->irq_reg_sts=true\n", __func__);
1366 "%s FAIL:invoking pch_spi_free_resources\n", __func__);
1367 pch_spi_free_resources(board_dat, data);
1370 dev_dbg(&board_dat->
pdev->dev,
"%s Return=%d\n", __func__, retval);
1411 dev_dbg(&plat_dev->
dev,
"%s:debug\n", __func__);
1416 dev_err(&plat_dev->
dev,
"spi_alloc_master[%d] failed.\n",
1421 data = spi_master_get_devdata(master);
1424 platform_set_drvdata(plat_dev, data);
1432 dev_err(&plat_dev->
dev,
"%s pci_iomap failed\n", __func__);
1437 dev_dbg(&plat_dev->
dev,
"[ch%d] remap_addr=%p\n",
1442 master->
setup = pch_spi_setup;
1443 master->
transfer = pch_spi_transfer;
1450 data->
ch = plat_dev->
id;
1453 INIT_LIST_HEAD(&data->
queue);
1458 ret = pch_spi_get_resources(board_dat, data);
1460 dev_err(&plat_dev->
dev,
"%s fail(retval=%d)\n", __func__, ret);
1461 goto err_spi_get_resources;
1468 "%s request_irq failed\n", __func__);
1469 goto err_request_irq;
1473 pch_spi_set_master_mode(master);
1478 "%s spi_register_master FAILED\n", __func__);
1479 goto err_spi_register_master;
1483 dev_info(&plat_dev->
dev,
"Use DMA for data transfers\n");
1484 pch_alloc_dma_buf(board_dat, data);
1489 err_spi_register_master:
1492 pch_spi_free_resources(board_dat, data);
1493 err_spi_get_resources:
1496 spi_master_put(master);
1504 struct pch_spi_data *data = platform_get_drvdata(plat_dev);
1506 unsigned long flags;
1508 dev_dbg(&plat_dev->
dev,
"%s:[ch%d] irq=%d\n",
1509 __func__, plat_dev->
id, board_dat->
pdev->irq);
1512 pch_free_dma_buf(board_dat, data);
1519 while ((list_empty(&data->
queue) == 0) && --count) {
1520 dev_dbg(&board_dat->
pdev->dev,
"%s :queue not empty\n",
1522 spin_unlock_irqrestore(&data->
lock, flags);
1526 spin_unlock_irqrestore(&data->
lock, flags);
1528 pch_spi_free_resources(board_dat, data);
1548 struct pch_spi_data *data = platform_get_drvdata(pd_dev);
1550 dev_dbg(&pd_dev->
dev,
"%s ENTRY\n", __func__);
1554 "%s pci_get_drvdata returned NULL\n", __func__);
1561 while ((--count) > 0) {
1571 pch_spi_reset(data->
master);
1576 "%s free_irq invoked successfully.\n", __func__);
1585 struct pch_spi_data *data = platform_get_drvdata(pd_dev);
1590 "%s pci_get_drvdata returned NULL\n", __func__);
1600 "%s request_irq failed\n", __func__);
1605 pch_spi_reset(data->
master);
1606 pch_spi_set_master_mode(data->
master);
1612 #define pch_spi_pd_suspend NULL
1613 #define pch_spi_pd_resume NULL
1621 .probe = pch_spi_pd_probe,
1638 dev_err(&pdev->
dev,
"%s Can't allocate pd_dev_sav\n", __func__);
1644 dev_err(&pdev->
dev,
"%s Can't allocate board_dat\n", __func__);
1651 dev_err(&pdev->
dev,
"%s request_region failed\n", __func__);
1655 board_dat->
pdev = pdev;
1656 board_dat->
num =
id->driver_data;
1657 pd_dev_save->
num =
id->driver_data;
1662 dev_err(&pdev->
dev,
"%s pci_enable_device failed\n", __func__);
1666 for (i = 0; i < board_dat->
num; i++) {
1669 dev_err(&pdev->
dev,
"platform_device_alloc failed\n");
1670 goto err_platform_device;
1673 pd_dev->
dev.parent = &pdev->
dev;
1676 sizeof(*board_dat));
1679 "platform_device_add_data failed\n");
1681 goto err_platform_device;
1686 dev_err(&pdev->
dev,
"platform_device_add failed\n");
1688 goto err_platform_device;
1692 pci_set_drvdata(pdev, pd_dev_save);
1696 err_platform_device:
1713 dev_dbg(&pdev->
dev,
"%s ENTRY:pdev=%p\n", __func__, pdev);
1715 for (i = 0; i < pd_dev_save->
num; i++)
1732 pd_dev_save->
board_dat->suspend_sts =
true;
1741 dev_err(&pdev->
dev,
"%s pci_save_state failed\n", __func__);
1759 "%s pci_enable_device failed\n", __func__);
1764 pd_dev_save->
board_dat->suspend_sts =
false;
1770 #define pch_spi_suspend NULL
1771 #define pch_spi_resume NULL
1775 static struct pci_driver pch_spi_pcidev_driver = {
1777 .id_table = pch_spi_pcidev_id,
1778 .probe = pch_spi_probe,
1784 static int __init pch_spi_init(
void)
1791 ret = pci_register_driver(&pch_spi_pcidev_driver);
1799 static void __exit pch_spi_exit(
void)
1808 "to use DMA for data transfers pass 1 else 0; default 1");