1 // SPDX-License-Identifier: GPL-2.0+
3 * Microchip PIC32 SPI controller driver.
5 * Copyright (c) 2015, Microchip Technology Inc.
13 #include <linux/bitops.h>
14 #include <linux/compat.h>
18 #include <asm/types.h>
21 #include <dt-bindings/clock/microchip,clock.h>
22 #include <mach/pic32.h>
24 DECLARE_GLOBAL_DATA_PTR;
26 /* PIC32 SPI controller registers */
27 struct pic32_reg_spi {
28 struct pic32_reg_atomic ctrl;
29 struct pic32_reg_atomic status;
30 struct pic32_reg_atomic buf;
31 struct pic32_reg_atomic baud;
32 struct pic32_reg_atomic ctrl2;
35 /* Bit fields in SPI Control Register */
36 #define PIC32_SPI_CTRL_MSTEN BIT(5) /* Enable SPI Master */
37 #define PIC32_SPI_CTRL_CKP BIT(6) /* active low */
38 #define PIC32_SPI_CTRL_CKE BIT(8) /* Tx on falling edge */
39 #define PIC32_SPI_CTRL_SMP BIT(9) /* Rx at middle or end of tx */
40 #define PIC32_SPI_CTRL_BPW_MASK 0x03 /* Bits per word */
41 #define PIC32_SPI_CTRL_BPW_8 0x0
42 #define PIC32_SPI_CTRL_BPW_16 0x1
43 #define PIC32_SPI_CTRL_BPW_32 0x2
44 #define PIC32_SPI_CTRL_BPW_SHIFT 10
45 #define PIC32_SPI_CTRL_ON BIT(15) /* Macro enable */
46 #define PIC32_SPI_CTRL_ENHBUF BIT(16) /* Enable enhanced buffering */
47 #define PIC32_SPI_CTRL_MCLKSEL BIT(23) /* Select SPI Clock src */
48 #define PIC32_SPI_CTRL_MSSEN BIT(28) /* SPI macro will drive SS */
49 #define PIC32_SPI_CTRL_FRMEN BIT(31) /* Enable framing mode */
51 /* Bit fields in SPI Status Register */
52 #define PIC32_SPI_STAT_RX_OV BIT(6) /* err, s/w needs to clear */
53 #define PIC32_SPI_STAT_TF_LVL_MASK 0x1f
54 #define PIC32_SPI_STAT_TF_LVL_SHIFT 16
55 #define PIC32_SPI_STAT_RF_LVL_MASK 0x1f
56 #define PIC32_SPI_STAT_RF_LVL_SHIFT 24
58 /* Bit fields in SPI Baud Register */
59 #define PIC32_SPI_BAUD_MASK 0x1ff
61 struct pic32_spi_priv {
62 struct pic32_reg_spi *regs;
63 u32 fifo_depth; /* FIFO depth in bytes */
64 u32 fifo_n_word; /* FIFO depth in words */
65 struct gpio_desc cs_gpio;
67 /* Current SPI slave specific */
69 u32 speed_hz; /* spi-clk rate */
72 /* Current message/transfer state */
79 /* SPI FiFo accessor */
80 void (*rx_fifo)(struct pic32_spi_priv *);
81 void (*tx_fifo)(struct pic32_spi_priv *);
84 static inline void pic32_spi_enable(struct pic32_spi_priv *priv)
86 writel(PIC32_SPI_CTRL_ON, &priv->regs->ctrl.set);
89 static inline void pic32_spi_disable(struct pic32_spi_priv *priv)
91 writel(PIC32_SPI_CTRL_ON, &priv->regs->ctrl.clr);
94 static inline u32 pic32_spi_rx_fifo_level(struct pic32_spi_priv *priv)
96 u32 sr = readl(&priv->regs->status.raw);
98 return (sr >> PIC32_SPI_STAT_RF_LVL_SHIFT) & PIC32_SPI_STAT_RF_LVL_MASK;
101 static inline u32 pic32_spi_tx_fifo_level(struct pic32_spi_priv *priv)
103 u32 sr = readl(&priv->regs->status.raw);
105 return (sr >> PIC32_SPI_STAT_TF_LVL_SHIFT) & PIC32_SPI_STAT_TF_LVL_MASK;
108 /* Return the max entries we can fill into tx fifo */
109 static u32 pic32_tx_max(struct pic32_spi_priv *priv, int n_bytes)
111 u32 tx_left, tx_room, rxtx_gap;
113 tx_left = (priv->tx_end - priv->tx) / n_bytes;
114 tx_room = priv->fifo_n_word - pic32_spi_tx_fifo_level(priv);
116 rxtx_gap = (priv->rx_end - priv->rx) - (priv->tx_end - priv->tx);
118 return min3(tx_left, tx_room, (u32)(priv->fifo_n_word - rxtx_gap));
121 /* Return the max entries we should read out of rx fifo */
122 static u32 pic32_rx_max(struct pic32_spi_priv *priv, int n_bytes)
124 u32 rx_left = (priv->rx_end - priv->rx) / n_bytes;
126 return min_t(u32, rx_left, pic32_spi_rx_fifo_level(priv));
129 #define BUILD_SPI_FIFO_RW(__name, __type, __bwl) \
130 static void pic32_spi_rx_##__name(struct pic32_spi_priv *priv) \
133 u32 mx = pic32_rx_max(priv, sizeof(__type)); \
136 val = read##__bwl(&priv->regs->buf.raw); \
137 if (priv->rx_end - priv->len) \
138 *(__type *)(priv->rx) = val; \
139 priv->rx += sizeof(__type); \
143 static void pic32_spi_tx_##__name(struct pic32_spi_priv *priv) \
146 u32 mx = pic32_tx_max(priv, sizeof(__type)); \
148 for (; mx ; mx--) { \
149 val = (__type) ~0U; \
150 if (priv->tx_end - priv->len) \
151 val = *(__type *)(priv->tx); \
152 write##__bwl(val, &priv->regs->buf.raw); \
153 priv->tx += sizeof(__type); \
156 BUILD_SPI_FIFO_RW(byte, u8, b);
157 BUILD_SPI_FIFO_RW(word, u16, w);
158 BUILD_SPI_FIFO_RW(dword, u32, l);
160 static int pic32_spi_set_word_size(struct pic32_spi_priv *priv,
161 unsigned int wordlen)
168 priv->rx_fifo = pic32_spi_rx_byte;
169 priv->tx_fifo = pic32_spi_tx_byte;
170 bits_per_word = PIC32_SPI_CTRL_BPW_8;
173 priv->rx_fifo = pic32_spi_rx_word;
174 priv->tx_fifo = pic32_spi_tx_word;
175 bits_per_word = PIC32_SPI_CTRL_BPW_16;
178 priv->rx_fifo = pic32_spi_rx_dword;
179 priv->tx_fifo = pic32_spi_tx_dword;
180 bits_per_word = PIC32_SPI_CTRL_BPW_32;
183 printf("pic32-spi: unsupported wordlen\n");
187 /* set bits-per-word */
188 val = readl(&priv->regs->ctrl.raw);
189 val &= ~(PIC32_SPI_CTRL_BPW_MASK << PIC32_SPI_CTRL_BPW_SHIFT);
190 val |= bits_per_word << PIC32_SPI_CTRL_BPW_SHIFT;
191 writel(val, &priv->regs->ctrl.raw);
193 /* calculate maximum number of words fifo can hold */
194 priv->fifo_n_word = DIV_ROUND_UP(priv->fifo_depth, wordlen / 8);
199 static int pic32_spi_claim_bus(struct udevice *slave)
201 struct pic32_spi_priv *priv = dev_get_priv(slave->parent);
204 pic32_spi_enable(priv);
209 static int pic32_spi_release_bus(struct udevice *slave)
211 struct pic32_spi_priv *priv = dev_get_priv(slave->parent);
214 pic32_spi_disable(priv);
219 static void spi_cs_activate(struct pic32_spi_priv *priv)
221 if (!dm_gpio_is_valid(&priv->cs_gpio))
224 dm_gpio_set_value(&priv->cs_gpio, 1);
227 static void spi_cs_deactivate(struct pic32_spi_priv *priv)
229 if (!dm_gpio_is_valid(&priv->cs_gpio))
232 dm_gpio_set_value(&priv->cs_gpio, 0);
235 static int pic32_spi_xfer(struct udevice *slave, unsigned int bitlen,
236 const void *tx_buf, void *rx_buf,
239 struct dm_spi_slave_platdata *slave_plat;
240 struct udevice *bus = slave->parent;
241 struct pic32_spi_priv *priv;
242 int len = bitlen / 8;
246 priv = dev_get_priv(bus);
247 slave_plat = dev_get_parent_platdata(slave);
249 debug("spi_xfer: bus:%i cs:%i flags:%lx\n",
250 bus->seq, slave_plat->cs, flags);
251 debug("msg tx %p, rx %p submitted of %d byte(s)\n",
252 tx_buf, rx_buf, len);
255 if (flags & SPI_XFER_BEGIN)
256 spi_cs_activate(priv);
258 /* set current transfer information */
261 priv->tx_end = priv->tx + len;
262 priv->rx_end = priv->rx + len;
265 /* transact by polling */
266 tbase = get_timer(0);
271 /* received sufficient data */
272 if (priv->rx >= priv->rx_end) {
277 if (get_timer(tbase) > 5 * CONFIG_SYS_HZ) {
278 printf("pic32_spi: error, xfer timedout.\n");
279 flags |= SPI_XFER_END;
286 if (flags & SPI_XFER_END)
287 spi_cs_deactivate(priv);
292 static int pic32_spi_set_speed(struct udevice *bus, uint speed)
294 struct pic32_spi_priv *priv = dev_get_priv(bus);
297 debug("%s: %s, speed %u\n", __func__, bus->name, speed);
299 /* div = [clk_in / (2 * spi_clk)] - 1 */
300 div = (priv->clk_rate / 2 / speed) - 1;
301 div &= PIC32_SPI_BAUD_MASK;
302 writel(div, &priv->regs->baud.raw);
304 priv->speed_hz = speed;
309 static int pic32_spi_set_mode(struct udevice *bus, uint mode)
311 struct pic32_spi_priv *priv = dev_get_priv(bus);
314 debug("%s: %s, mode %d\n", __func__, bus->name, mode);
316 /* set spi-clk mode */
317 val = readl(&priv->regs->ctrl.raw);
320 val |= PIC32_SPI_CTRL_CKP;
322 val &= ~PIC32_SPI_CTRL_CKP;
324 /* TX at idle-to-active clk transition */
326 val &= ~PIC32_SPI_CTRL_CKE;
328 val |= PIC32_SPI_CTRL_CKE;
330 /* RX at end of tx */
331 val |= PIC32_SPI_CTRL_SMP;
332 writel(val, &priv->regs->ctrl.raw);
339 static int pic32_spi_set_wordlen(struct udevice *slave, unsigned int wordlen)
341 struct pic32_spi_priv *priv = dev_get_priv(slave->parent);
343 return pic32_spi_set_word_size(priv, wordlen);
346 static void pic32_spi_hw_init(struct pic32_spi_priv *priv)
351 pic32_spi_disable(priv);
353 val = readl(&priv->regs->ctrl);
355 /* enable enhanced fifo of 128bit deep */
356 val |= PIC32_SPI_CTRL_ENHBUF;
357 priv->fifo_depth = 16;
359 /* disable framing mode */
360 val &= ~PIC32_SPI_CTRL_FRMEN;
362 /* enable master mode */
363 val |= PIC32_SPI_CTRL_MSTEN;
365 /* select clk source */
366 val &= ~PIC32_SPI_CTRL_MCLKSEL;
368 /* set manual /CS mode */
369 val &= ~PIC32_SPI_CTRL_MSSEN;
371 writel(val, &priv->regs->ctrl);
373 /* clear rx overflow indicator */
374 writel(PIC32_SPI_STAT_RX_OV, &priv->regs->status.clr);
377 static int pic32_spi_probe(struct udevice *bus)
379 struct pic32_spi_priv *priv = dev_get_priv(bus);
380 struct dm_spi_bus *dm_spi = dev_get_uclass_priv(bus);
381 int node = dev_of_offset(bus);
382 struct udevice *clkdev;
387 debug("%s: %d, bus: %i\n", __func__, __LINE__, bus->seq);
388 addr = fdtdec_get_addr_size(gd->fdt_blob, node, "reg", &size);
389 if (addr == FDT_ADDR_T_NONE)
392 priv->regs = ioremap(addr, size);
396 dm_spi->max_hz = fdtdec_get_int(gd->fdt_blob, node, "spi-max-frequency",
399 ret = clk_get_by_index(bus, 0, &clkdev);
401 printf("pic32-spi: error, clk not found\n");
404 priv->clk_rate = clk_get_periph_rate(clkdev, ret);
407 pic32_spi_hw_init(priv);
410 pic32_spi_set_word_size(priv, SPI_DEFAULT_WORDLEN);
412 /* PIC32 SPI controller can automatically drive /CS during transfer
413 * depending on fifo fill-level. /CS will stay asserted as long as
414 * TX fifo is non-empty, else will be deasserted confirming completion
415 * of the ongoing transfer. To avoid this sort of error we will drive
416 * /CS manually by toggling cs-gpio pins.
418 ret = gpio_request_by_name_nodev(offset_to_ofnode(node), "cs-gpios", 0,
419 &priv->cs_gpio, GPIOD_IS_OUT);
421 printf("pic32-spi: error, cs-gpios not found\n");
428 static const struct dm_spi_ops pic32_spi_ops = {
429 .claim_bus = pic32_spi_claim_bus,
430 .release_bus = pic32_spi_release_bus,
431 .xfer = pic32_spi_xfer,
432 .set_speed = pic32_spi_set_speed,
433 .set_mode = pic32_spi_set_mode,
434 .set_wordlen = pic32_spi_set_wordlen,
437 static const struct udevice_id pic32_spi_ids[] = {
438 { .compatible = "microchip,pic32mzda-spi" },
442 U_BOOT_DRIVER(pic32_spi) = {
445 .of_match = pic32_spi_ids,
446 .ops = &pic32_spi_ops,
447 .priv_auto_alloc_size = sizeof(struct pic32_spi_priv),
448 .probe = pic32_spi_probe,