21 #include <linux/module.h>
37 #define MAX_SPI_PORTS 3
41 #define S3C64XX_SPI_CH_CFG 0x00
42 #define S3C64XX_SPI_CLK_CFG 0x04
43 #define S3C64XX_SPI_MODE_CFG 0x08
44 #define S3C64XX_SPI_SLAVE_SEL 0x0C
45 #define S3C64XX_SPI_INT_EN 0x10
46 #define S3C64XX_SPI_STATUS 0x14
47 #define S3C64XX_SPI_TX_DATA 0x18
48 #define S3C64XX_SPI_RX_DATA 0x1C
49 #define S3C64XX_SPI_PACKET_CNT 0x20
50 #define S3C64XX_SPI_PENDING_CLR 0x24
51 #define S3C64XX_SPI_SWAP_CFG 0x28
52 #define S3C64XX_SPI_FB_CLK 0x2C
54 #define S3C64XX_SPI_CH_HS_EN (1<<6)
55 #define S3C64XX_SPI_CH_SW_RST (1<<5)
56 #define S3C64XX_SPI_CH_SLAVE (1<<4)
57 #define S3C64XX_SPI_CPOL_L (1<<3)
58 #define S3C64XX_SPI_CPHA_B (1<<2)
59 #define S3C64XX_SPI_CH_RXCH_ON (1<<1)
60 #define S3C64XX_SPI_CH_TXCH_ON (1<<0)
62 #define S3C64XX_SPI_CLKSEL_SRCMSK (3<<9)
63 #define S3C64XX_SPI_CLKSEL_SRCSHFT 9
64 #define S3C64XX_SPI_ENCLK_ENABLE (1<<8)
65 #define S3C64XX_SPI_PSR_MASK 0xff
67 #define S3C64XX_SPI_MODE_CH_TSZ_BYTE (0<<29)
68 #define S3C64XX_SPI_MODE_CH_TSZ_HALFWORD (1<<29)
69 #define S3C64XX_SPI_MODE_CH_TSZ_WORD (2<<29)
70 #define S3C64XX_SPI_MODE_CH_TSZ_MASK (3<<29)
71 #define S3C64XX_SPI_MODE_BUS_TSZ_BYTE (0<<17)
72 #define S3C64XX_SPI_MODE_BUS_TSZ_HALFWORD (1<<17)
73 #define S3C64XX_SPI_MODE_BUS_TSZ_WORD (2<<17)
74 #define S3C64XX_SPI_MODE_BUS_TSZ_MASK (3<<17)
75 #define S3C64XX_SPI_MODE_RXDMA_ON (1<<2)
76 #define S3C64XX_SPI_MODE_TXDMA_ON (1<<1)
77 #define S3C64XX_SPI_MODE_4BURST (1<<0)
79 #define S3C64XX_SPI_SLAVE_AUTO (1<<1)
80 #define S3C64XX_SPI_SLAVE_SIG_INACT (1<<0)
82 #define S3C64XX_SPI_INT_TRAILING_EN (1<<6)
83 #define S3C64XX_SPI_INT_RX_OVERRUN_EN (1<<5)
84 #define S3C64XX_SPI_INT_RX_UNDERRUN_EN (1<<4)
85 #define S3C64XX_SPI_INT_TX_OVERRUN_EN (1<<3)
86 #define S3C64XX_SPI_INT_TX_UNDERRUN_EN (1<<2)
87 #define S3C64XX_SPI_INT_RX_FIFORDY_EN (1<<1)
88 #define S3C64XX_SPI_INT_TX_FIFORDY_EN (1<<0)
90 #define S3C64XX_SPI_ST_RX_OVERRUN_ERR (1<<5)
91 #define S3C64XX_SPI_ST_RX_UNDERRUN_ERR (1<<4)
92 #define S3C64XX_SPI_ST_TX_OVERRUN_ERR (1<<3)
93 #define S3C64XX_SPI_ST_TX_UNDERRUN_ERR (1<<2)
94 #define S3C64XX_SPI_ST_RX_FIFORDY (1<<1)
95 #define S3C64XX_SPI_ST_TX_FIFORDY (1<<0)
97 #define S3C64XX_SPI_PACKET_CNT_EN (1<<16)
99 #define S3C64XX_SPI_PND_TX_UNDERRUN_CLR (1<<4)
100 #define S3C64XX_SPI_PND_TX_OVERRUN_CLR (1<<3)
101 #define S3C64XX_SPI_PND_RX_UNDERRUN_CLR (1<<2)
102 #define S3C64XX_SPI_PND_RX_OVERRUN_CLR (1<<1)
103 #define S3C64XX_SPI_PND_TRAILING_CLR (1<<0)
105 #define S3C64XX_SPI_SWAP_RX_HALF_WORD (1<<7)
106 #define S3C64XX_SPI_SWAP_RX_BYTE (1<<6)
107 #define S3C64XX_SPI_SWAP_RX_BIT (1<<5)
108 #define S3C64XX_SPI_SWAP_RX_EN (1<<4)
109 #define S3C64XX_SPI_SWAP_TX_HALF_WORD (1<<3)
110 #define S3C64XX_SPI_SWAP_TX_BYTE (1<<2)
111 #define S3C64XX_SPI_SWAP_TX_BIT (1<<1)
112 #define S3C64XX_SPI_SWAP_TX_EN (1<<0)
114 #define S3C64XX_SPI_FBCLK_MSK (3<<0)
116 #define FIFO_LVL_MASK(i) ((i)->port_conf->fifo_lvl_mask[i->port_id])
117 #define S3C64XX_SPI_ST_TX_DONE(v, i) (((v) & \
118 (1 << (i)->port_conf->tx_st_done)) ? 1 : 0)
119 #define TX_FIFO_LVL(v, i) (((v) >> 6) & FIFO_LVL_MASK(i))
120 #define RX_FIFO_LVL(v, i) (((v) >> (i)->port_conf->rx_lvl_offset) & \
123 #define S3C64XX_SPI_MAX_TRAILCNT 0x3ff
124 #define S3C64XX_SPI_TRAILCNT_OFF 19
126 #define S3C64XX_SPI_TRAILCNT S3C64XX_SPI_MAX_TRAILCNT
128 #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
130 #define RXBUSY (1<<2)
131 #define TXBUSY (1<<3)
206 .name =
"samsung-spi-dma",
229 dev_warn(&sdd->
pdev->dev,
"Timed out flushing TX FIFO\n");
242 dev_warn(&sdd->
pdev->dev,
"Timed out flushing RX FIFO\n");
257 static void s3c64xx_spi_dmacb(
void *
data)
282 spin_unlock_irqrestore(&sdd->
lock, flags);
310 info.fp = s3c64xx_spi_dmacb;
316 sdd->
ops->trigger(dma->
ch);
326 req.client = &s3c64xx_spi_dma_client;
328 req.dt_dmach_prop = sdd->
rx_dma.dma_prop;
330 req.dt_dmach_prop = sdd->
tx_dma.dma_prop;
415 cs = sdd->
tgl_spi->controller_data;
583 #define XFER_DMAADDR_INVALID DMA_BIT_MASK(32)
611 dev_err(dev,
"dma_map_single Tx failed\n");
621 dev_err(dev,
"dma_map_single Rx failed\n");
660 static int s3c64xx_spi_transfer_one_message(
struct spi_master *master,
667 int status = 0, cs_toggle = 0;
678 s3c64xx_spi_config(sdd);
682 if (s3c64xx_spi_map_mssg(sdd, msg)) {
684 "Xfer: Unable to map message buffers!\n");
703 if (xfer->
len % (bpw / 8)) {
705 "Xfer length(%u) not a multiple of word size(%u)\n",
714 s3c64xx_spi_config(sdd);
729 enable_datapath(sdd, spi, xfer, use_dma);
737 spin_unlock_irqrestore(&sdd->
lock, flags);
739 status = wait_for_xfer(sdd, xfer, use_dma);
747 "rx-%d tx-%d res:rx-%c tx-%c len-%d\n",
775 disable_cs(sdd, spi);
784 if (!cs_toggle || status)
785 disable_cs(sdd, spi);
789 s3c64xx_spi_unmap_mssg(sdd, msg);
798 static int s3c64xx_spi_prepare_transfer(
struct spi_master *spi)
803 while (!acquire_dma(sdd))
806 pm_runtime_get_sync(&sdd->
pdev->dev);
811 static int s3c64xx_spi_unprepare_transfer(
struct spi_master *spi)
816 sdd->
ops->release(sdd->
rx_dma.ch, &s3c64xx_spi_dma_client);
817 sdd->
ops->release(sdd->
tx_dma.ch, &s3c64xx_spi_dma_client);
819 pm_runtime_put(&sdd->
pdev->dev);
832 slave_np = spi->
dev.of_node;
834 dev_err(&spi->
dev,
"device node not found\n");
840 dev_err(&spi->
dev,
"child node 'controller-data' not found\n");
846 dev_err(&spi->
dev,
"could not allocate memory for controller"
848 of_node_put(data_np);
852 cs->
line = of_get_named_gpio(data_np,
"cs-gpio", 0);
853 if (!gpio_is_valid(cs->
line)) {
854 dev_err(&spi->
dev,
"chip select gpio is not specified or "
857 of_node_put(data_np);
861 of_property_read_u32(data_np,
"samsung,spi-feedback-delay", &fb_delay);
863 of_node_put(data_np);
873 static int s3c64xx_spi_setup(
struct spi_device *spi)
882 sdd = spi_master_get_devdata(spi->
master);
883 if (!cs && spi->
dev.of_node) {
884 cs = s3c64xx_get_slave_ctrldata(sdd, spi);
888 if (IS_ERR_OR_NULL(cs)) {
893 if (!spi_get_ctldata(spi)) {
895 dev_name(&spi->
dev));
898 "Failed to get /CS gpio [%d]: %d\n",
902 spi_set_ctldata(spi, cs);
911 if (msg->
spi == spi) {
913 "setup: attempt while mssg in queue!\n");
914 spin_unlock_irqrestore(&sdd->
lock, flags);
920 spin_unlock_irqrestore(&sdd->
lock, flags);
925 dev_err(&spi->
dev,
"setup: %dbits/wrd not supported!\n",
931 pm_runtime_get_sync(&sdd->
pdev->dev);
967 pm_runtime_put(&sdd->
pdev->dev);
968 disable_cs(sdd, spi);
973 disable_cs(sdd, spi);
977 spi_set_ctldata(spi,
NULL);
980 if (spi->
dev.of_node)
986 static void s3c64xx_spi_cleanup(
struct spi_device *spi)
992 if (spi->
dev.of_node)
995 spi_set_ctldata(spi,
NULL);
998 static irqreturn_t s3c64xx_spi_irq(
int irq,
void *data)
1059 static int __devinit s3c64xx_spi_get_dmares(
1066 char prop_name[15], *chan_str;
1078 if (!sdd->
pdev->dev.of_node) {
1081 dev_err(&pdev->
dev,
"Unable to get SPI-%s dma "
1082 "resource\n", chan_str);
1089 sprintf(prop_name,
"%s-dma-channel", chan_str);
1092 dev_err(&pdev->
dev,
"%s dma channel property not specified\n",
1109 for (idx = 0; idx < 3; idx++) {
1110 gpio = of_get_gpio(dev->
of_node, idx);
1111 if (!gpio_is_valid(gpio)) {
1112 dev_err(dev,
"invalid gpio[%d]: %d\n", idx, gpio);
1118 dev_err(dev,
"gpio [%d] request failed: %d\n",
1134 for (idx = 0; idx < 3; idx++)
1146 dev_err(dev,
"memory allocation for spi_info failed\n");
1150 if (of_property_read_u32(dev->
of_node,
"samsung,spi-src-clk", &temp)) {
1151 dev_warn(dev,
"spi bus clock parent not specified, using "
1152 "clock at index 0 as parent\n");
1158 if (of_property_read_u32(dev->
of_node,
"num-cs", &temp)) {
1159 dev_warn(dev,
"number of chip select lines not specified, "
1160 "assuming 1 chip select line\n");
1184 static const struct of_device_id s3c64xx_spi_dt_match[];
1190 if (pdev->
dev.of_node) {
1209 if (!sci && pdev->
dev.of_node) {
1210 sci = s3c64xx_spi_parse_dt(&pdev->
dev);
1212 return PTR_ERR(sci);
1216 dev_err(&pdev->
dev,
"platform_data missing!\n");
1221 if (mem_res ==
NULL) {
1222 dev_err(&pdev->
dev,
"Unable to get SPI MEM resource\n");
1228 dev_warn(&pdev->
dev,
"Failed to get IRQ: %d\n", irq);
1234 if (master ==
NULL) {
1235 dev_err(&pdev->
dev,
"Unable to allocate SPI Master\n");
1239 platform_set_drvdata(pdev, master);
1241 sdd = spi_master_get_devdata(master);
1242 sdd->
port_conf = s3c64xx_spi_get_port_config(pdev);
1247 if (pdev->
dev.of_node) {
1250 dev_err(&pdev->
dev,
"failed to get alias id, "
1261 ret = s3c64xx_spi_get_dmares(sdd,
true);
1265 ret = s3c64xx_spi_get_dmares(sdd,
false);
1269 master->
dev.of_node = pdev->
dev.of_node;
1271 master->
setup = s3c64xx_spi_setup;
1272 master->
cleanup = s3c64xx_spi_cleanup;
1289 if (s3c64xx_spi_parse_dt_gpio(sdd))
1292 dev_err(&pdev->
dev,
"Unable to config gpio\n");
1299 if (IS_ERR(sdd->
clk)) {
1300 dev_err(&pdev->
dev,
"Unable to acquire clock 'spi'\n");
1301 ret = PTR_ERR(sdd->
clk);
1306 dev_err(&pdev->
dev,
"Couldn't enable clock 'spi'\n");
1315 "Unable to acquire clock '%s'\n", clk_name);
1321 dev_err(&pdev->
dev,
"Couldn't enable clock '%s'\n", clk_name);
1327 s3c64xx_spi_hwinit(sdd, sdd->
port_id);
1331 INIT_LIST_HEAD(&sdd->
queue);
1333 ret =
request_irq(irq, s3c64xx_spi_irq, 0,
"spi-s3c64xx", sdd);
1335 dev_err(&pdev->
dev,
"Failed to request IRQ %d: %d\n",
1345 dev_err(&pdev->
dev,
"cannot register SPI master\n");
1350 dev_dbg(&pdev->
dev,
"Samsung SoC SPI Driver loaded for Bus SPI-%d "
1351 "with %d Slaves attached\n",
1353 dev_dbg(&pdev->
dev,
"\tIOmem=[0x%x-0x%x]\tDMA=[Rx-%d, Tx-%d]\n",
1373 s3c64xx_spi_dt_gpio_free(sdd);
1377 platform_set_drvdata(pdev,
NULL);
1378 spi_master_put(master);
1385 struct spi_master *master = spi_master_get(platform_get_drvdata(pdev));
1388 pm_runtime_disable(&pdev->
dev);
1403 s3c64xx_spi_dt_gpio_free(sdd);
1405 platform_set_drvdata(pdev,
NULL);
1406 spi_master_put(master);
1412 static int s3c64xx_spi_suspend(
struct device *dev)
1424 s3c64xx_spi_dt_gpio_free(sdd);
1431 static int s3c64xx_spi_resume(
struct device *dev)
1438 s3c64xx_spi_parse_dt_gpio(sdd);
1446 s3c64xx_spi_hwinit(sdd, sdd->
port_id);
1454 #ifdef CONFIG_PM_RUNTIME
1455 static int s3c64xx_spi_runtime_suspend(
struct device *dev)
1466 static int s3c64xx_spi_runtime_resume(
struct device *dev)
1478 static const struct dev_pm_ops s3c64xx_spi_pm = {
1481 s3c64xx_spi_runtime_resume,
NULL)
1486 .rx_lvl_offset = 13,
1492 .fifo_lvl_mask = { 0x7f, 0x7F },
1493 .rx_lvl_offset = 13,
1498 .fifo_lvl_mask = { 0x1ff, 0x7F },
1499 .rx_lvl_offset = 15,
1504 .fifo_lvl_mask = { 0x7f, 0x7F },
1505 .rx_lvl_offset = 13,
1511 .fifo_lvl_mask = { 0x1ff, 0x7F },
1512 .rx_lvl_offset = 15,
1518 .fifo_lvl_mask = { 0x1ff, 0x7F, 0x7F },
1519 .rx_lvl_offset = 15,
1522 .clk_from_cmu =
true,
1527 .name =
"s3c2443-spi",
1530 .name =
"s3c6410-spi",
1533 .name =
"s5p64x0-spi",
1536 .name =
"s5pc100-spi",
1539 .name =
"s5pv210-spi",
1542 .name =
"exynos4210-spi",
1549 static const struct of_device_id s3c64xx_spi_dt_match[] = {
1550 { .compatible =
"samsung,exynos4210-spi",
1551 .data = (
void *)&exynos4_spi_port_config,
1560 .name =
"s3c64xx-spi",
1562 .pm = &s3c64xx_spi_pm,
1565 .remove = s3c64xx_spi_remove,
1566 .id_table = s3c64xx_spi_driver_ids,
1570 static int __init s3c64xx_spi_init(
void)
1576 static void __exit s3c64xx_spi_exit(
void)