24 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/device.h>
37 #include <linux/slab.h>
48 #define OMAP2_MCSPI_MAX_FREQ 48000000
49 #define SPI_AUTOSUSPEND_TIMEOUT 2000
51 #define OMAP2_MCSPI_REVISION 0x00
52 #define OMAP2_MCSPI_SYSSTATUS 0x14
53 #define OMAP2_MCSPI_IRQSTATUS 0x18
54 #define OMAP2_MCSPI_IRQENABLE 0x1c
55 #define OMAP2_MCSPI_WAKEUPENABLE 0x20
56 #define OMAP2_MCSPI_SYST 0x24
57 #define OMAP2_MCSPI_MODULCTRL 0x28
60 #define OMAP2_MCSPI_CHCONF0 0x2c
61 #define OMAP2_MCSPI_CHSTAT0 0x30
62 #define OMAP2_MCSPI_CHCTRL0 0x34
63 #define OMAP2_MCSPI_TX0 0x38
64 #define OMAP2_MCSPI_RX0 0x3c
68 #define OMAP2_MCSPI_MODULCTRL_SINGLE BIT(0)
69 #define OMAP2_MCSPI_MODULCTRL_MS BIT(2)
70 #define OMAP2_MCSPI_MODULCTRL_STEST BIT(3)
72 #define OMAP2_MCSPI_CHCONF_PHA BIT(0)
73 #define OMAP2_MCSPI_CHCONF_POL BIT(1)
74 #define OMAP2_MCSPI_CHCONF_CLKD_MASK (0x0f << 2)
75 #define OMAP2_MCSPI_CHCONF_EPOL BIT(6)
76 #define OMAP2_MCSPI_CHCONF_WL_MASK (0x1f << 7)
77 #define OMAP2_MCSPI_CHCONF_TRM_RX_ONLY BIT(12)
78 #define OMAP2_MCSPI_CHCONF_TRM_TX_ONLY BIT(13)
79 #define OMAP2_MCSPI_CHCONF_TRM_MASK (0x03 << 12)
80 #define OMAP2_MCSPI_CHCONF_DMAW BIT(14)
81 #define OMAP2_MCSPI_CHCONF_DMAR BIT(15)
82 #define OMAP2_MCSPI_CHCONF_DPE0 BIT(16)
83 #define OMAP2_MCSPI_CHCONF_DPE1 BIT(17)
84 #define OMAP2_MCSPI_CHCONF_IS BIT(18)
85 #define OMAP2_MCSPI_CHCONF_TURBO BIT(19)
86 #define OMAP2_MCSPI_CHCONF_FORCE BIT(20)
88 #define OMAP2_MCSPI_CHSTAT_RXS BIT(0)
89 #define OMAP2_MCSPI_CHSTAT_TXS BIT(1)
90 #define OMAP2_MCSPI_CHSTAT_EOT BIT(2)
92 #define OMAP2_MCSPI_CHCTRL_EN BIT(0)
94 #define OMAP2_MCSPI_WAKEUPENABLE_WKEN BIT(0)
111 #define DMA_MIN_BYTES 160
144 static inline void mcspi_write_reg(
struct spi_master *master,
147 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
154 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
159 static inline void mcspi_write_cs_reg(
const struct spi_device *
spi,
190 static void omap2_mcspi_set_dma_req(
const struct spi_device *
spi,
195 l = mcspi_cached_chconf0(spi);
207 mcspi_write_chconf0(spi, l);
210 static void omap2_mcspi_set_enable(
const struct spi_device *spi,
int enable)
220 static void omap2_mcspi_force_cs(
struct spi_device *spi,
int cs_active)
224 l = mcspi_cached_chconf0(spi);
230 mcspi_write_chconf0(spi, l);
233 static void omap2_mcspi_set_master_mode(
struct spi_master *master)
235 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
251 static void omap2_mcspi_restore_ctx(
struct omap2_mcspi *mcspi)
267 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
269 pm_runtime_get_sync(mcspi->
dev);
275 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
277 pm_runtime_mark_last_busy(mcspi->
dev);
278 pm_runtime_put_autosuspend(mcspi->
dev);
282 static int mcspi_wait_for_reg_bit(
void __iomem *
reg,
unsigned long bit)
295 static void omap2_mcspi_rx_callback(
void *
data)
304 omap2_mcspi_set_dma_req(spi, 1, 0);
307 static void omap2_mcspi_tx_callback(
void *
data)
316 omap2_mcspi_set_dma_req(spi, 0, 0);
319 static void omap2_mcspi_tx_dma(
struct spi_device *spi,
331 mcspi = spi_master_get_devdata(spi->
master);
343 dmaengine_slave_config(mcspi_dma->
dma_tx, &cfg);
349 tx = dmaengine_prep_slave_sg(mcspi_dma->
dma_tx, &
sg, 1,
352 tx->
callback = omap2_mcspi_tx_callback;
354 dmaengine_submit(tx);
359 dma_async_issue_pending(mcspi_dma->
dma_tx);
360 omap2_mcspi_set_dma_req(spi, 0, 1);
368 if (mcspi_wait_for_reg_bit(chstat_reg,
371 else if (mcspi_wait_for_reg_bit(chstat_reg,
387 int word_len, element_count;
389 mcspi = spi_master_get_devdata(spi->
master);
393 l = mcspi_cached_chconf0(spi);
396 element_count =
count;
397 else if (word_len <= 16)
398 element_count = count >> 1;
400 element_count = count >> 2;
407 dmaengine_slave_config(mcspi_dma->
dma_rx, &cfg);
416 tx = dmaengine_prep_slave_sg(mcspi_dma->
dma_rx, &
sg, 1,
420 tx->
callback = omap2_mcspi_rx_callback;
422 dmaengine_submit(tx);
428 dma_async_issue_pending(mcspi_dma->
dma_rx);
429 omap2_mcspi_set_dma_req(spi, 1, 1);
434 omap2_mcspi_set_enable(spi, 0);
436 elements = element_count - 1;
438 if (l & OMAP2_MCSPI_CHCONF_TURBO) {
448 else if (word_len <= 16)
453 dev_err(&spi->
dev,
"DMA RX penultimate word empty");
454 count -= (word_len <= 8) ? 2 :
455 (word_len <= 16) ? 4 :
457 omap2_mcspi_set_enable(spi, 1);
462 & OMAP2_MCSPI_CHSTAT_RXS)) {
468 else if (word_len <= 16)
474 count -= (word_len <= 8) ? 1 :
475 (word_len <= 16) ? 2 :
478 omap2_mcspi_set_enable(spi, 1);
496 mcspi = spi_master_get_devdata(spi->
master);
498 l = mcspi_cached_chconf0(spi);
512 memset(&cfg, 0,
sizeof(cfg));
526 omap2_mcspi_tx_dma(spi, xfer, cfg);
529 return omap2_mcspi_rx_dma(spi, xfer, cfg, es);
547 mcspi = spi_master_get_devdata(spi->
master);
552 l = mcspi_cached_chconf0(spi);
560 if (c < (word_len>>3))
573 if (mcspi_wait_for_reg_bit(chstat_reg,
583 if (mcspi_wait_for_reg_bit(chstat_reg,
584 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
589 if (c == 1 && tx ==
NULL &&
590 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
591 omap2_mcspi_set_enable(spi, 0);
594 word_len, *(rx - 1));
595 if (mcspi_wait_for_reg_bit(chstat_reg,
596 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
602 }
else if (c == 0 && tx ==
NULL) {
603 omap2_mcspi_set_enable(spi, 0);
608 word_len, *(rx - 1));
611 }
else if (word_len <= 16) {
620 if (mcspi_wait_for_reg_bit(chstat_reg,
630 if (mcspi_wait_for_reg_bit(chstat_reg,
631 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
636 if (c == 2 && tx ==
NULL &&
637 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
638 omap2_mcspi_set_enable(spi, 0);
641 word_len, *(rx - 1));
642 if (mcspi_wait_for_reg_bit(chstat_reg,
643 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
649 }
else if (c == 0 && tx ==
NULL) {
650 omap2_mcspi_set_enable(spi, 0);
655 word_len, *(rx - 1));
658 }
else if (word_len <= 32) {
667 if (mcspi_wait_for_reg_bit(chstat_reg,
677 if (mcspi_wait_for_reg_bit(chstat_reg,
678 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
683 if (c == 4 && tx ==
NULL &&
684 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
685 omap2_mcspi_set_enable(spi, 0);
688 word_len, *(rx - 1));
689 if (mcspi_wait_for_reg_bit(chstat_reg,
690 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
696 }
else if (c == 0 && tx ==
NULL) {
697 omap2_mcspi_set_enable(spi, 0);
702 word_len, *(rx - 1));
709 if (mcspi_wait_for_reg_bit(chstat_reg,
712 }
else if (mcspi_wait_for_reg_bit(chstat_reg,
720 omap2_mcspi_set_enable(spi, 0);
723 omap2_mcspi_set_enable(spi, 1);
727 static u32 omap2_mcspi_calc_divisor(
u32 speed_hz)
731 for (div = 0; div < 15; div++)
739 static int omap2_mcspi_setup_transfer(
struct spi_device *spi,
749 mcspi = spi_master_get_devdata(spi->
master);
750 spi_cntrl = mcspi->
master;
761 div = omap2_mcspi_calc_divisor(speed_hz);
763 l = mcspi_cached_chconf0(spi);
773 l |= (word_len - 1) << 7;
795 mcspi_write_chconf0(spi, l);
797 dev_dbg(&spi->
dev,
"setup: speed %d, sample %s edge, clk %s\n",
805 static int omap2_mcspi_request_dma(
struct spi_device *spi)
813 mcspi = spi_master_get_devdata(master);
824 dev_err(&spi->
dev,
"no RX DMA engine channel for McSPI\n");
831 dev_err(&spi->
dev,
"no TX DMA engine channel for McSPI\n");
840 static int omap2_mcspi_setup(
struct spi_device *spi)
849 dev_dbg(&spi->
dev,
"setup: unsupported %d bit words\n",
869 ret = omap2_mcspi_request_dma(spi);
874 ret = pm_runtime_get_sync(mcspi->
dev);
878 ret = omap2_mcspi_setup_transfer(spi,
NULL);
879 pm_runtime_mark_last_busy(mcspi->
dev);
880 pm_runtime_put_autosuspend(mcspi->
dev);
885 static void omap2_mcspi_cleanup(
struct spi_device *spi)
891 mcspi = spi_master_get_devdata(spi->
master);
930 int par_override = 0;
938 omap2_mcspi_set_enable(spi, 1);
946 status = omap2_mcspi_setup_transfer(spi, t);
954 omap2_mcspi_force_cs(spi, 1);
958 chconf = mcspi_cached_chconf0(spi);
960 chconf &= ~OMAP2_MCSPI_CHCONF_TURBO;
973 mcspi_write_chconf0(spi, chconf);
984 count = omap2_mcspi_txrx_dma(spi, t);
986 count = omap2_mcspi_txrx_pio(spi, t);
989 if (count != t->
len) {
1000 omap2_mcspi_force_cs(spi, 0);
1007 status = omap2_mcspi_setup_transfer(spi,
NULL);
1011 omap2_mcspi_force_cs(spi, 0);
1013 omap2_mcspi_set_enable(spi, 0);
1019 static int omap2_mcspi_transfer_one_message(
struct spi_master *master,
1025 mcspi = spi_master_get_devdata(master);
1035 unsigned len = t->
len;
1038 || (len && !(rx_buf || tx_buf))
1042 dev_dbg(mcspi->
dev,
"transfer: %d Hz, %d %s%s, %d bpw\n",
1051 dev_dbg(mcspi->
dev,
"speed_hz %d below minimum %d Hz\n",
1060 if (tx_buf !=
NULL) {
1064 dev_dbg(mcspi->
dev,
"dma %cX %d bytes error\n",
1069 if (rx_buf !=
NULL) {
1073 dev_dbg(mcspi->
dev,
"dma %cX %d bytes error\n",
1083 omap2_mcspi_work(mcspi, m);
1094 ret = pm_runtime_get_sync(mcspi->
dev);
1102 omap2_mcspi_set_master_mode(master);
1103 pm_runtime_mark_last_busy(mcspi->
dev);
1104 pm_runtime_put_autosuspend(mcspi->
dev);
1108 static int omap_mcspi_runtime_resume(
struct device *
dev)
1114 mcspi = spi_master_get_devdata(master);
1115 omap2_mcspi_restore_ctx(mcspi);
1128 static const struct of_device_id omap_mcspi_of_match[] = {
1130 .compatible =
"ti,omap2-mcspi",
1131 .data = &omap2_pdata,
1134 .compatible =
"ti,omap4-mcspi",
1135 .data = &omap4_pdata,
1148 u32 regs_offset = 0;
1149 static int bus_num = 1;
1155 if (master ==
NULL) {
1156 dev_dbg(&pdev->
dev,
"master allocation failed\n");
1163 master->
setup = omap2_mcspi_setup;
1167 master->
cleanup = omap2_mcspi_cleanup;
1173 pdata = match->
data;
1175 of_property_read_u32(node,
"ti,spi-num-cs", &num_cs);
1179 pdata = pdev->
dev.platform_data;
1188 mcspi = spi_master_get_devdata(master);
1197 r->
start += regs_offset;
1198 r->
end += regs_offset;
1203 dev_dbg(&pdev->
dev,
"can't ioremap MCSPI\n");
1210 INIT_LIST_HEAD(&mcspi->
ctx.cs);
1220 char dma_ch_name[14];
1227 dev_dbg(&pdev->
dev,
"cannot get DMA RX channel\n");
1237 dev_dbg(&pdev->
dev,
"cannot get DMA TX channel\n");
1248 pinctrl = devm_pinctrl_get_select_default(&pdev->
dev);
1249 if (IS_ERR(pinctrl))
1251 "pins are not configured from the driver\n");
1253 pm_runtime_use_autosuspend(&pdev->
dev);
1257 if (status || omap2_mcspi_master_setup(mcspi) < 0)
1267 pm_runtime_disable(&pdev->
dev);
1271 spi_master_put(master);
1282 mcspi = spi_master_get_devdata(master);
1285 pm_runtime_put_sync(mcspi->
dev);
1286 pm_runtime_disable(&pdev->
dev);
1289 kfree(dma_channels);
1297 #ifdef CONFIG_SUSPEND
1306 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
1310 pm_runtime_get_sync(mcspi->
dev);
1323 pm_runtime_mark_last_busy(mcspi->
dev);
1324 pm_runtime_put_autosuspend(mcspi->
dev);
1328 #define omap2_mcspi_resume NULL
1331 static const struct dev_pm_ops omap2_mcspi_pm_ops = {
1333 .runtime_resume = omap_mcspi_runtime_resume,
1338 .name =
"omap2_mcspi",
1340 .pm = &omap2_mcspi_pm_ops,
1341 .of_match_table = omap_mcspi_of_match,
1343 .probe = omap2_mcspi_probe,