bpf: add bpf_jit_limit knob to restrict unpriv allocations
[linux/fpc-iii.git] / drivers / spi / spi-jcore.c
blobf8117b80fa22e7ac8d5108f403166e178858cdb5
1 /*
2 * J-Core SPI controller driver
4 * Copyright (C) 2012-2016 Smart Energy Instruments, Inc.
6 * Current version by Rich Felker
7 * Based loosely on initial version by Oleksandr G Zhadan
9 */
10 #include <linux/init.h>
11 #include <linux/interrupt.h>
12 #include <linux/errno.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/spi/spi.h>
16 #include <linux/clk.h>
17 #include <linux/err.h>
18 #include <linux/io.h>
19 #include <linux/of.h>
20 #include <linux/delay.h>
22 #define DRV_NAME "jcore_spi"
24 #define CTRL_REG 0x0
25 #define DATA_REG 0x4
27 #define JCORE_SPI_CTRL_XMIT 0x02
28 #define JCORE_SPI_STAT_BUSY 0x02
29 #define JCORE_SPI_CTRL_LOOP 0x08
30 #define JCORE_SPI_CTRL_CS_BITS 0x15
32 #define JCORE_SPI_WAIT_RDY_MAX_LOOP 2000000
34 struct jcore_spi {
35 struct spi_master *master;
36 void __iomem *base;
37 unsigned int cs_reg;
38 unsigned int speed_reg;
39 unsigned int speed_hz;
40 unsigned int clock_freq;
43 static int jcore_spi_wait(void __iomem *ctrl_reg)
45 unsigned timeout = JCORE_SPI_WAIT_RDY_MAX_LOOP;
47 do {
48 if (!(readl(ctrl_reg) & JCORE_SPI_STAT_BUSY))
49 return 0;
50 cpu_relax();
51 } while (--timeout);
53 return -EBUSY;
56 static void jcore_spi_program(struct jcore_spi *hw)
58 void __iomem *ctrl_reg = hw->base + CTRL_REG;
60 if (jcore_spi_wait(ctrl_reg))
61 dev_err(hw->master->dev.parent,
62 "timeout waiting to program ctrl reg.\n");
64 writel(hw->cs_reg | hw->speed_reg, ctrl_reg);
67 static void jcore_spi_chipsel(struct spi_device *spi, bool value)
69 struct jcore_spi *hw = spi_master_get_devdata(spi->master);
70 u32 csbit = 1U << (2 * spi->chip_select);
72 dev_dbg(hw->master->dev.parent, "chipselect %d\n", spi->chip_select);
74 if (value)
75 hw->cs_reg |= csbit;
76 else
77 hw->cs_reg &= ~csbit;
79 jcore_spi_program(hw);
82 static void jcore_spi_baudrate(struct jcore_spi *hw, int speed)
84 if (speed == hw->speed_hz) return;
85 hw->speed_hz = speed;
86 if (speed >= hw->clock_freq / 2)
87 hw->speed_reg = 0;
88 else
89 hw->speed_reg = ((hw->clock_freq / 2 / speed) - 1) << 27;
90 jcore_spi_program(hw);
91 dev_dbg(hw->master->dev.parent, "speed=%d reg=0x%x\n",
92 speed, hw->speed_reg);
95 static int jcore_spi_txrx(struct spi_master *master, struct spi_device *spi,
96 struct spi_transfer *t)
98 struct jcore_spi *hw = spi_master_get_devdata(master);
100 void __iomem *ctrl_reg = hw->base + CTRL_REG;
101 void __iomem *data_reg = hw->base + DATA_REG;
102 u32 xmit;
104 /* data buffers */
105 const unsigned char *tx;
106 unsigned char *rx;
107 unsigned int len;
108 unsigned int count;
110 jcore_spi_baudrate(hw, t->speed_hz);
112 xmit = hw->cs_reg | hw->speed_reg | JCORE_SPI_CTRL_XMIT;
113 tx = t->tx_buf;
114 rx = t->rx_buf;
115 len = t->len;
117 for (count = 0; count < len; count++) {
118 if (jcore_spi_wait(ctrl_reg))
119 break;
121 writel(tx ? *tx++ : 0, data_reg);
122 writel(xmit, ctrl_reg);
124 if (jcore_spi_wait(ctrl_reg))
125 break;
127 if (rx)
128 *rx++ = readl(data_reg);
131 spi_finalize_current_transfer(master);
133 if (count < len)
134 return -EREMOTEIO;
136 return 0;
139 static int jcore_spi_probe(struct platform_device *pdev)
141 struct device_node *node = pdev->dev.of_node;
142 struct jcore_spi *hw;
143 struct spi_master *master;
144 struct resource *res;
145 u32 clock_freq;
146 struct clk *clk;
147 int err = -ENODEV;
149 master = spi_alloc_master(&pdev->dev, sizeof(struct jcore_spi));
150 if (!master)
151 return err;
153 /* Setup the master state. */
154 master->num_chipselect = 3;
155 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
156 master->transfer_one = jcore_spi_txrx;
157 master->set_cs = jcore_spi_chipsel;
158 master->dev.of_node = node;
159 master->bus_num = pdev->id;
161 hw = spi_master_get_devdata(master);
162 hw->master = master;
163 platform_set_drvdata(pdev, hw);
165 /* Find and map our resources */
166 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
167 if (!res)
168 goto exit_busy;
169 if (!devm_request_mem_region(&pdev->dev, res->start,
170 resource_size(res), pdev->name))
171 goto exit_busy;
172 hw->base = devm_ioremap_nocache(&pdev->dev, res->start,
173 resource_size(res));
174 if (!hw->base)
175 goto exit_busy;
178 * The SPI clock rate controlled via a configurable clock divider
179 * which is applied to the reference clock. A 50 MHz reference is
180 * most suitable for obtaining standard SPI clock rates, but some
181 * designs may have a different reference clock, and the DT must
182 * make the driver aware so that it can properly program the
183 * requested rate. If the clock is omitted, 50 MHz is assumed.
185 clock_freq = 50000000;
186 clk = devm_clk_get(&pdev->dev, "ref_clk");
187 if (!IS_ERR_OR_NULL(clk)) {
188 if (clk_enable(clk) == 0)
189 clock_freq = clk_get_rate(clk);
190 else
191 dev_warn(&pdev->dev, "could not enable ref_clk\n");
193 hw->clock_freq = clock_freq;
195 /* Initialize all CS bits to high. */
196 hw->cs_reg = JCORE_SPI_CTRL_CS_BITS;
197 jcore_spi_baudrate(hw, 400000);
199 /* Register our spi controller */
200 err = devm_spi_register_master(&pdev->dev, master);
201 if (err)
202 goto exit;
204 return 0;
206 exit_busy:
207 err = -EBUSY;
208 exit:
209 spi_master_put(master);
210 return err;
213 static const struct of_device_id jcore_spi_of_match[] = {
214 { .compatible = "jcore,spi2" },
218 static struct platform_driver jcore_spi_driver = {
219 .probe = jcore_spi_probe,
220 .driver = {
221 .name = DRV_NAME,
222 .of_match_table = jcore_spi_of_match,
226 module_platform_driver(jcore_spi_driver);
228 MODULE_DESCRIPTION("J-Core SPI driver");
229 MODULE_AUTHOR("Rich Felker <dalias@libc.org>");
230 MODULE_LICENSE("GPL");
231 MODULE_ALIAS("platform:" DRV_NAME);