1 // SPDX-License-Identifier: GPL-2.0+
10 #include <dm/device_compat.h>
11 #include <linux/errno.h>
14 #include <asm/arch/imx-regs.h>
15 #include <asm/arch/clock.h>
16 #include <asm/mach-imx/spi.h>
18 DECLARE_GLOBAL_DATA_PTR;
21 /* i.MX27 has a completely wrong register layout and register definitions in the
22 * datasheet, the correct one is in the Freescale's Linux driver */
24 #error "i.MX27 CSPI not supported due to drastic differences in register definitions" \
25 "See linux mxc_spi driver from Freescale for details."
28 __weak int board_spi_cs_gpio(unsigned bus, unsigned cs)
33 #define OUT MXC_GPIO_DIRECTION_OUT
35 #define reg_read readl
36 #define reg_write(a, v) writel(v, a)
38 #if !defined(CONFIG_SYS_SPI_MXC_WAIT)
39 #define CONFIG_SYS_SPI_MXC_WAIT (CONFIG_SYS_HZ/100) /* 10 ms */
42 #define MAX_CS_COUNT 4
44 struct mxc_spi_slave {
45 struct spi_slave slave;
48 #if defined(MXC_ECSPI)
56 struct gpio_desc cs_gpios[MAX_CS_COUNT];
60 static inline struct mxc_spi_slave *to_mxc_spi_slave(struct spi_slave *slave)
62 return container_of(slave, struct mxc_spi_slave, slave);
65 static void mxc_spi_cs_activate(struct mxc_spi_slave *mxcs)
67 #if defined(CONFIG_DM_SPI)
68 struct udevice *dev = mxcs->dev;
69 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
71 u32 cs = slave_plat->cs;
73 if (!dm_gpio_is_valid(&mxcs->cs_gpios[cs]))
76 dm_gpio_set_value(&mxcs->cs_gpios[cs], 1);
79 gpio_set_value(mxcs->gpio, mxcs->ss_pol);
83 static void mxc_spi_cs_deactivate(struct mxc_spi_slave *mxcs)
85 #if defined(CONFIG_DM_SPI)
86 struct udevice *dev = mxcs->dev;
87 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
89 u32 cs = slave_plat->cs;
91 if (!dm_gpio_is_valid(&mxcs->cs_gpios[cs]))
94 dm_gpio_set_value(&mxcs->cs_gpios[cs], 0);
97 gpio_set_value(mxcs->gpio, !(mxcs->ss_pol));
101 u32 get_cspi_div(u32 div)
105 for (i = 0; i < 8; i++) {
113 static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs)
115 unsigned int ctrl_reg;
118 unsigned int max_hz = mxcs->max_hz;
119 unsigned int mode = mxcs->mode;
121 clk_src = mxc_get_clock(MXC_CSPI_CLK);
123 div = DIV_ROUND_UP(clk_src, max_hz);
124 div = get_cspi_div(div);
126 debug("clk %d Hz, div %d, real clk %d Hz\n",
127 max_hz, div, clk_src / (4 << div));
129 ctrl_reg = MXC_CSPICTRL_CHIPSELECT(cs) |
130 MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS) |
131 MXC_CSPICTRL_DATARATE(div) |
139 ctrl_reg |= MXC_CSPICTRL_PHA;
141 ctrl_reg |= MXC_CSPICTRL_POL;
142 if (mode & SPI_CS_HIGH)
143 ctrl_reg |= MXC_CSPICTRL_SSPOL;
144 mxcs->ctrl_reg = ctrl_reg;
151 static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs)
153 u32 clk_src = mxc_get_clock(MXC_CSPI_CLK);
154 s32 reg_ctrl, reg_config;
155 u32 ss_pol = 0, sclkpol = 0, sclkpha = 0, sclkctl = 0;
156 u32 pre_div = 0, post_div = 0;
157 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
158 unsigned int max_hz = mxcs->max_hz;
159 unsigned int mode = mxcs->mode;
162 * Reset SPI and set all CSs to master mode, if toggling
163 * between slave and master mode we might see a glitch
166 reg_ctrl = MXC_CSPICTRL_MODE_MASK;
167 reg_write(®s->ctrl, reg_ctrl);
168 reg_ctrl |= MXC_CSPICTRL_EN;
169 reg_write(®s->ctrl, reg_ctrl);
171 if (clk_src > max_hz) {
172 pre_div = (clk_src - 1) / max_hz;
173 /* fls(1) = 1, fls(0x80000000) = 32, fls(16) = 5 */
174 post_div = fls(pre_div);
177 if (post_div >= 16) {
178 printf("Error: no divider for the freq: %d\n",
182 pre_div >>= post_div;
188 debug("pre_div = %d, post_div=%d\n", pre_div, post_div);
189 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_SELCHAN(3)) |
190 MXC_CSPICTRL_SELCHAN(cs);
191 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_PREDIV(0x0F)) |
192 MXC_CSPICTRL_PREDIV(pre_div);
193 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_POSTDIV(0x0F)) |
194 MXC_CSPICTRL_POSTDIV(post_div);
196 if (mode & SPI_CS_HIGH)
199 if (mode & SPI_CPOL) {
207 reg_config = reg_read(®s->cfg);
210 * Configuration register setup
211 * The MX51 supports different setup for each SS
213 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_SSPOL))) |
214 (ss_pol << (cs + MXC_CSPICON_SSPOL));
215 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_POL))) |
216 (sclkpol << (cs + MXC_CSPICON_POL));
217 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_CTL))) |
218 (sclkctl << (cs + MXC_CSPICON_CTL));
219 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_PHA))) |
220 (sclkpha << (cs + MXC_CSPICON_PHA));
222 debug("reg_ctrl = 0x%x\n", reg_ctrl);
223 reg_write(®s->ctrl, reg_ctrl);
224 debug("reg_config = 0x%x\n", reg_config);
225 reg_write(®s->cfg, reg_config);
227 /* save config register and control register */
228 mxcs->ctrl_reg = reg_ctrl;
229 mxcs->cfg_reg = reg_config;
231 /* clear interrupt reg */
232 reg_write(®s->intr, 0);
233 reg_write(®s->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
239 int spi_xchg_single(struct mxc_spi_slave *mxcs, unsigned int bitlen,
240 const u8 *dout, u8 *din, unsigned long flags)
242 int nbytes = DIV_ROUND_UP(bitlen, 8);
244 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
248 debug("%s: bitlen %d dout 0x%lx din 0x%lx\n",
249 __func__, bitlen, (ulong)dout, (ulong)din);
251 mxcs->ctrl_reg = (mxcs->ctrl_reg &
252 ~MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS)) |
253 MXC_CSPICTRL_BITCOUNT(bitlen - 1);
255 reg_write(®s->ctrl, mxcs->ctrl_reg | MXC_CSPICTRL_EN);
257 reg_write(®s->cfg, mxcs->cfg_reg);
260 /* Clear interrupt register */
261 reg_write(®s->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
264 * The SPI controller works only with words,
265 * check if less than a word is sent.
266 * Access to the FIFO is only 32 bit
270 cnt = (bitlen % 32) / 8;
272 for (i = 0; i < cnt; i++) {
273 data = (data << 8) | (*dout++ & 0xFF);
276 debug("Sending SPI 0x%x\n", data);
278 reg_write(®s->txdata, data);
287 /* Buffer is not 32-bit aligned */
288 if ((unsigned long)dout & 0x03) {
290 for (i = 0; i < 4; i++)
291 data = (data << 8) | (*dout++ & 0xFF);
294 data = cpu_to_be32(data);
298 debug("Sending SPI 0x%x\n", data);
299 reg_write(®s->txdata, data);
303 /* FIFO is written, now starts the transfer setting the XCH bit */
304 reg_write(®s->ctrl, mxcs->ctrl_reg |
305 MXC_CSPICTRL_EN | MXC_CSPICTRL_XCH);
308 status = reg_read(®s->stat);
309 /* Wait until the TC (Transfer completed) bit is set */
310 while ((status & MXC_CSPICTRL_TC) == 0) {
311 if (get_timer(ts) > CONFIG_SYS_SPI_MXC_WAIT) {
312 printf("spi_xchg_single: Timeout!\n");
315 status = reg_read(®s->stat);
318 /* Transfer completed, clear any pending request */
319 reg_write(®s->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
321 nbytes = DIV_ROUND_UP(bitlen, 8);
326 data = reg_read(®s->rxdata);
327 cnt = (bitlen % 32) / 8;
328 data = cpu_to_be32(data) >> ((sizeof(data) - cnt) * 8);
329 debug("SPI Rx unaligned: 0x%x\n", data);
331 memcpy(din, &data, cnt);
339 tmp = reg_read(®s->rxdata);
340 data = cpu_to_be32(tmp);
341 debug("SPI Rx: 0x%x 0x%x\n", tmp, data);
342 cnt = min_t(u32, nbytes, sizeof(data));
344 memcpy(din, &data, cnt);
354 static int mxc_spi_xfer_internal(struct mxc_spi_slave *mxcs,
355 unsigned int bitlen, const void *dout,
356 void *din, unsigned long flags)
358 int n_bytes = DIV_ROUND_UP(bitlen, 8);
362 u8 *p_outbuf = (u8 *)dout;
363 u8 *p_inbuf = (u8 *)din;
368 if (flags & SPI_XFER_BEGIN)
369 mxc_spi_cs_activate(mxcs);
371 while (n_bytes > 0) {
372 if (n_bytes < MAX_SPI_BYTES)
375 blk_size = MAX_SPI_BYTES;
377 n_bits = blk_size * 8;
379 ret = spi_xchg_single(mxcs, n_bits, p_outbuf, p_inbuf, 0);
384 p_outbuf += blk_size;
390 if (flags & SPI_XFER_END) {
391 mxc_spi_cs_deactivate(mxcs);
397 static int mxc_spi_claim_bus_internal(struct mxc_spi_slave *mxcs, int cs)
399 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
402 reg_write(®s->rxdata, 1);
404 ret = spi_cfg_mxc(mxcs, cs);
406 printf("mxc_spi: cannot setup SPI controller\n");
409 reg_write(®s->period, MXC_CSPIPERIOD_32KHZ);
410 reg_write(®s->intr, 0);
415 #ifndef CONFIG_DM_SPI
416 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
417 void *din, unsigned long flags)
419 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
421 return mxc_spi_xfer_internal(mxcs, bitlen, dout, din, flags);
425 * Some SPI devices require active chip-select over multiple
426 * transactions, we achieve this using a GPIO. Still, the SPI
427 * controller has to be configured to use one of its own chipselects.
428 * To use this feature you have to implement board_spi_cs_gpio() to assign
429 * a gpio value for each cs (-1 if cs doesn't need to use gpio).
430 * You must use some unused on this SPI controller cs between 0 and 3.
432 static int setup_cs_gpio(struct mxc_spi_slave *mxcs,
433 unsigned int bus, unsigned int cs)
437 mxcs->gpio = board_spi_cs_gpio(bus, cs);
438 if (mxcs->gpio == -1)
441 gpio_request(mxcs->gpio, "spi-cs");
442 ret = gpio_direction_output(mxcs->gpio, !(mxcs->ss_pol));
444 printf("mxc_spi: cannot setup gpio %d\n", mxcs->gpio);
451 static unsigned long spi_bases[] = {
452 MXC_SPI_BASE_ADDRESSES
455 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
456 unsigned int max_hz, unsigned int mode)
458 struct mxc_spi_slave *mxcs;
461 if (bus >= ARRAY_SIZE(spi_bases))
465 printf("Error: desired clock is 0\n");
469 mxcs = spi_alloc_slave(struct mxc_spi_slave, bus, cs);
471 puts("mxc_spi: SPI Slave not allocated !\n");
475 mxcs->ss_pol = (mode & SPI_CS_HIGH) ? 1 : 0;
477 ret = setup_cs_gpio(mxcs, bus, cs);
483 mxcs->base = spi_bases[bus];
484 mxcs->max_hz = max_hz;
490 void spi_free_slave(struct spi_slave *slave)
492 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
497 int spi_claim_bus(struct spi_slave *slave)
499 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
501 return mxc_spi_claim_bus_internal(mxcs, slave->cs);
504 void spi_release_bus(struct spi_slave *slave)
506 /* TODO: Shut the controller down */
510 static int mxc_spi_probe(struct udevice *bus)
512 struct mxc_spi_slave *mxcs = dev_get_platdata(bus);
513 int node = dev_of_offset(bus);
514 const void *blob = gd->fdt_blob;
518 ret = gpio_request_list_by_name(bus, "cs-gpios", mxcs->cs_gpios,
519 ARRAY_SIZE(mxcs->cs_gpios), 0);
521 pr_err("Can't get %s gpios! Error: %d", bus->name, ret);
525 for (i = 0; i < ARRAY_SIZE(mxcs->cs_gpios); i++) {
526 if (!dm_gpio_is_valid(&mxcs->cs_gpios[i]))
529 ret = dm_gpio_set_dir_flags(&mxcs->cs_gpios[i],
530 GPIOD_IS_OUT | GPIOD_ACTIVE_LOW);
532 dev_err(bus, "Setting cs %d error\n", i);
537 mxcs->base = devfdt_get_addr(bus);
538 if (mxcs->base == FDT_ADDR_T_NONE)
541 mxcs->max_hz = fdtdec_get_int(blob, node, "spi-max-frequency",
547 static int mxc_spi_xfer(struct udevice *dev, unsigned int bitlen,
548 const void *dout, void *din, unsigned long flags)
550 struct mxc_spi_slave *mxcs = dev_get_platdata(dev->parent);
553 return mxc_spi_xfer_internal(mxcs, bitlen, dout, din, flags);
556 static int mxc_spi_claim_bus(struct udevice *dev)
558 struct mxc_spi_slave *mxcs = dev_get_platdata(dev->parent);
559 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
563 return mxc_spi_claim_bus_internal(mxcs, slave_plat->cs);
566 static int mxc_spi_release_bus(struct udevice *dev)
571 static int mxc_spi_set_speed(struct udevice *bus, uint speed)
577 static int mxc_spi_set_mode(struct udevice *bus, uint mode)
579 struct mxc_spi_slave *mxcs = dev_get_platdata(bus);
582 mxcs->ss_pol = (mode & SPI_CS_HIGH) ? 1 : 0;
587 static const struct dm_spi_ops mxc_spi_ops = {
588 .claim_bus = mxc_spi_claim_bus,
589 .release_bus = mxc_spi_release_bus,
590 .xfer = mxc_spi_xfer,
591 .set_speed = mxc_spi_set_speed,
592 .set_mode = mxc_spi_set_mode,
595 static const struct udevice_id mxc_spi_ids[] = {
596 { .compatible = "fsl,imx51-ecspi" },
600 U_BOOT_DRIVER(mxc_spi) = {
603 .of_match = mxc_spi_ids,
605 .platdata_auto_alloc_size = sizeof(struct mxc_spi_slave),
606 .probe = mxc_spi_probe,