1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2008, Guennadi Liakhovetski <lg@denx.de>
10 #include <linux/errno.h>
13 #include <asm/arch/imx-regs.h>
14 #include <asm/arch/clock.h>
15 #include <asm/mach-imx/spi.h>
17 DECLARE_GLOBAL_DATA_PTR;
20 /* i.MX27 has a completely wrong register layout and register definitions in the
21 * datasheet, the correct one is in the Freescale's Linux driver */
23 #error "i.MX27 CSPI not supported due to drastic differences in register definitions" \
24 "See linux mxc_spi driver from Freescale for details."
27 __weak int board_spi_cs_gpio(unsigned bus, unsigned cs)
32 #define OUT MXC_GPIO_DIRECTION_OUT
34 #define reg_read readl
35 #define reg_write(a, v) writel(v, a)
37 #if !defined(CONFIG_SYS_SPI_MXC_WAIT)
38 #define CONFIG_SYS_SPI_MXC_WAIT (CONFIG_SYS_HZ/100) /* 10 ms */
41 struct mxc_spi_slave {
42 struct spi_slave slave;
45 #if defined(MXC_ECSPI)
55 static inline struct mxc_spi_slave *to_mxc_spi_slave(struct spi_slave *slave)
57 return container_of(slave, struct mxc_spi_slave, slave);
60 static void mxc_spi_cs_activate(struct mxc_spi_slave *mxcs)
62 if (CONFIG_IS_ENABLED(DM_SPI)) {
63 dm_gpio_set_value(&mxcs->ss, 1);
66 gpio_set_value(mxcs->gpio, mxcs->ss_pol);
70 static void mxc_spi_cs_deactivate(struct mxc_spi_slave *mxcs)
72 if (CONFIG_IS_ENABLED(DM_SPI)) {
73 dm_gpio_set_value(&mxcs->ss, 0);
76 gpio_set_value(mxcs->gpio, !(mxcs->ss_pol));
80 u32 get_cspi_div(u32 div)
84 for (i = 0; i < 8; i++) {
92 static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs)
94 unsigned int ctrl_reg;
97 unsigned int max_hz = mxcs->max_hz;
98 unsigned int mode = mxcs->mode;
100 clk_src = mxc_get_clock(MXC_CSPI_CLK);
102 div = DIV_ROUND_UP(clk_src, max_hz);
103 div = get_cspi_div(div);
105 debug("clk %d Hz, div %d, real clk %d Hz\n",
106 max_hz, div, clk_src / (4 << div));
108 ctrl_reg = MXC_CSPICTRL_CHIPSELECT(cs) |
109 MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS) |
110 MXC_CSPICTRL_DATARATE(div) |
118 ctrl_reg |= MXC_CSPICTRL_PHA;
120 ctrl_reg |= MXC_CSPICTRL_POL;
121 if (mode & SPI_CS_HIGH)
122 ctrl_reg |= MXC_CSPICTRL_SSPOL;
123 mxcs->ctrl_reg = ctrl_reg;
130 static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs)
132 u32 clk_src = mxc_get_clock(MXC_CSPI_CLK);
133 s32 reg_ctrl, reg_config;
134 u32 ss_pol = 0, sclkpol = 0, sclkpha = 0, sclkctl = 0;
135 u32 pre_div = 0, post_div = 0;
136 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
137 unsigned int max_hz = mxcs->max_hz;
138 unsigned int mode = mxcs->mode;
141 * Reset SPI and set all CSs to master mode, if toggling
142 * between slave and master mode we might see a glitch
145 reg_ctrl = MXC_CSPICTRL_MODE_MASK;
146 reg_write(®s->ctrl, reg_ctrl);
147 reg_ctrl |= MXC_CSPICTRL_EN;
148 reg_write(®s->ctrl, reg_ctrl);
150 if (clk_src > max_hz) {
151 pre_div = (clk_src - 1) / max_hz;
152 /* fls(1) = 1, fls(0x80000000) = 32, fls(16) = 5 */
153 post_div = fls(pre_div);
156 if (post_div >= 16) {
157 printf("Error: no divider for the freq: %d\n",
161 pre_div >>= post_div;
167 debug("pre_div = %d, post_div=%d\n", pre_div, post_div);
168 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_SELCHAN(3)) |
169 MXC_CSPICTRL_SELCHAN(cs);
170 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_PREDIV(0x0F)) |
171 MXC_CSPICTRL_PREDIV(pre_div);
172 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_POSTDIV(0x0F)) |
173 MXC_CSPICTRL_POSTDIV(post_div);
175 if (mode & SPI_CS_HIGH)
178 if (mode & SPI_CPOL) {
186 reg_config = reg_read(®s->cfg);
189 * Configuration register setup
190 * The MX51 supports different setup for each SS
192 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_SSPOL))) |
193 (ss_pol << (cs + MXC_CSPICON_SSPOL));
194 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_POL))) |
195 (sclkpol << (cs + MXC_CSPICON_POL));
196 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_CTL))) |
197 (sclkctl << (cs + MXC_CSPICON_CTL));
198 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_PHA))) |
199 (sclkpha << (cs + MXC_CSPICON_PHA));
201 debug("reg_ctrl = 0x%x\n", reg_ctrl);
202 reg_write(®s->ctrl, reg_ctrl);
203 debug("reg_config = 0x%x\n", reg_config);
204 reg_write(®s->cfg, reg_config);
206 /* save config register and control register */
207 mxcs->ctrl_reg = reg_ctrl;
208 mxcs->cfg_reg = reg_config;
210 /* clear interrupt reg */
211 reg_write(®s->intr, 0);
212 reg_write(®s->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
218 int spi_xchg_single(struct mxc_spi_slave *mxcs, unsigned int bitlen,
219 const u8 *dout, u8 *din, unsigned long flags)
221 int nbytes = DIV_ROUND_UP(bitlen, 8);
223 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
227 debug("%s: bitlen %d dout 0x%x din 0x%x\n",
228 __func__, bitlen, (u32)dout, (u32)din);
230 mxcs->ctrl_reg = (mxcs->ctrl_reg &
231 ~MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS)) |
232 MXC_CSPICTRL_BITCOUNT(bitlen - 1);
234 reg_write(®s->ctrl, mxcs->ctrl_reg | MXC_CSPICTRL_EN);
236 reg_write(®s->cfg, mxcs->cfg_reg);
239 /* Clear interrupt register */
240 reg_write(®s->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
243 * The SPI controller works only with words,
244 * check if less than a word is sent.
245 * Access to the FIFO is only 32 bit
249 cnt = (bitlen % 32) / 8;
251 for (i = 0; i < cnt; i++) {
252 data = (data << 8) | (*dout++ & 0xFF);
255 debug("Sending SPI 0x%x\n", data);
257 reg_write(®s->txdata, data);
266 /* Buffer is not 32-bit aligned */
267 if ((unsigned long)dout & 0x03) {
269 for (i = 0; i < 4; i++)
270 data = (data << 8) | (*dout++ & 0xFF);
273 data = cpu_to_be32(data);
277 debug("Sending SPI 0x%x\n", data);
278 reg_write(®s->txdata, data);
282 /* FIFO is written, now starts the transfer setting the XCH bit */
283 reg_write(®s->ctrl, mxcs->ctrl_reg |
284 MXC_CSPICTRL_EN | MXC_CSPICTRL_XCH);
287 status = reg_read(®s->stat);
288 /* Wait until the TC (Transfer completed) bit is set */
289 while ((status & MXC_CSPICTRL_TC) == 0) {
290 if (get_timer(ts) > CONFIG_SYS_SPI_MXC_WAIT) {
291 printf("spi_xchg_single: Timeout!\n");
294 status = reg_read(®s->stat);
297 /* Transfer completed, clear any pending request */
298 reg_write(®s->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
300 nbytes = DIV_ROUND_UP(bitlen, 8);
305 data = reg_read(®s->rxdata);
306 cnt = (bitlen % 32) / 8;
307 data = cpu_to_be32(data) >> ((sizeof(data) - cnt) * 8);
308 debug("SPI Rx unaligned: 0x%x\n", data);
310 memcpy(din, &data, cnt);
318 tmp = reg_read(®s->rxdata);
319 data = cpu_to_be32(tmp);
320 debug("SPI Rx: 0x%x 0x%x\n", tmp, data);
321 cnt = min_t(u32, nbytes, sizeof(data));
323 memcpy(din, &data, cnt);
333 static int mxc_spi_xfer_internal(struct mxc_spi_slave *mxcs,
334 unsigned int bitlen, const void *dout,
335 void *din, unsigned long flags)
337 int n_bytes = DIV_ROUND_UP(bitlen, 8);
341 u8 *p_outbuf = (u8 *)dout;
342 u8 *p_inbuf = (u8 *)din;
347 if (flags & SPI_XFER_BEGIN)
348 mxc_spi_cs_activate(mxcs);
350 while (n_bytes > 0) {
351 if (n_bytes < MAX_SPI_BYTES)
354 blk_size = MAX_SPI_BYTES;
356 n_bits = blk_size * 8;
358 ret = spi_xchg_single(mxcs, n_bits, p_outbuf, p_inbuf, 0);
363 p_outbuf += blk_size;
369 if (flags & SPI_XFER_END) {
370 mxc_spi_cs_deactivate(mxcs);
376 static int mxc_spi_claim_bus_internal(struct mxc_spi_slave *mxcs, int cs)
378 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
381 reg_write(®s->rxdata, 1);
383 ret = spi_cfg_mxc(mxcs, cs);
385 printf("mxc_spi: cannot setup SPI controller\n");
388 reg_write(®s->period, MXC_CSPIPERIOD_32KHZ);
389 reg_write(®s->intr, 0);
394 #ifndef CONFIG_DM_SPI
395 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
396 void *din, unsigned long flags)
398 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
400 return mxc_spi_xfer_internal(mxcs, bitlen, dout, din, flags);
404 * Some SPI devices require active chip-select over multiple
405 * transactions, we achieve this using a GPIO. Still, the SPI
406 * controller has to be configured to use one of its own chipselects.
407 * To use this feature you have to implement board_spi_cs_gpio() to assign
408 * a gpio value for each cs (-1 if cs doesn't need to use gpio).
409 * You must use some unused on this SPI controller cs between 0 and 3.
411 static int setup_cs_gpio(struct mxc_spi_slave *mxcs,
412 unsigned int bus, unsigned int cs)
416 mxcs->gpio = board_spi_cs_gpio(bus, cs);
417 if (mxcs->gpio == -1)
420 gpio_request(mxcs->gpio, "spi-cs");
421 ret = gpio_direction_output(mxcs->gpio, !(mxcs->ss_pol));
423 printf("mxc_spi: cannot setup gpio %d\n", mxcs->gpio);
430 static unsigned long spi_bases[] = {
431 MXC_SPI_BASE_ADDRESSES
434 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
435 unsigned int max_hz, unsigned int mode)
437 struct mxc_spi_slave *mxcs;
440 if (bus >= ARRAY_SIZE(spi_bases))
444 printf("Error: desired clock is 0\n");
448 mxcs = spi_alloc_slave(struct mxc_spi_slave, bus, cs);
450 puts("mxc_spi: SPI Slave not allocated !\n");
454 mxcs->ss_pol = (mode & SPI_CS_HIGH) ? 1 : 0;
456 ret = setup_cs_gpio(mxcs, bus, cs);
462 mxcs->base = spi_bases[bus];
463 mxcs->max_hz = max_hz;
469 void spi_free_slave(struct spi_slave *slave)
471 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
476 int spi_claim_bus(struct spi_slave *slave)
478 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
480 return mxc_spi_claim_bus_internal(mxcs, slave->cs);
483 void spi_release_bus(struct spi_slave *slave)
485 /* TODO: Shut the controller down */
489 static int mxc_spi_probe(struct udevice *bus)
491 struct mxc_spi_slave *plat = bus->platdata;
492 struct mxc_spi_slave *mxcs = dev_get_platdata(bus);
493 int node = dev_of_offset(bus);
494 const void *blob = gd->fdt_blob;
497 if (gpio_request_by_name(bus, "cs-gpios", 0, &plat->ss,
499 dev_err(bus, "No cs-gpios property\n");
503 plat->base = devfdt_get_addr(bus);
504 if (plat->base == FDT_ADDR_T_NONE)
507 ret = dm_gpio_set_value(&plat->ss, 0);
509 dev_err(bus, "Setting cs error\n");
513 mxcs->max_hz = fdtdec_get_int(blob, node, "spi-max-frequency",
519 static int mxc_spi_xfer(struct udevice *dev, unsigned int bitlen,
520 const void *dout, void *din, unsigned long flags)
522 struct mxc_spi_slave *mxcs = dev_get_platdata(dev->parent);
525 return mxc_spi_xfer_internal(mxcs, bitlen, dout, din, flags);
528 static int mxc_spi_claim_bus(struct udevice *dev)
530 struct mxc_spi_slave *mxcs = dev_get_platdata(dev->parent);
531 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
533 return mxc_spi_claim_bus_internal(mxcs, slave_plat->cs);
536 static int mxc_spi_release_bus(struct udevice *dev)
541 static int mxc_spi_set_speed(struct udevice *bus, uint speed)
547 static int mxc_spi_set_mode(struct udevice *bus, uint mode)
549 struct mxc_spi_slave *mxcs = dev_get_platdata(bus);
552 mxcs->ss_pol = (mode & SPI_CS_HIGH) ? 1 : 0;
557 static const struct dm_spi_ops mxc_spi_ops = {
558 .claim_bus = mxc_spi_claim_bus,
559 .release_bus = mxc_spi_release_bus,
560 .xfer = mxc_spi_xfer,
561 .set_speed = mxc_spi_set_speed,
562 .set_mode = mxc_spi_set_mode,
565 static const struct udevice_id mxc_spi_ids[] = {
566 { .compatible = "fsl,imx51-ecspi" },
570 U_BOOT_DRIVER(mxc_spi) = {
573 .of_match = mxc_spi_ids,
575 .platdata_auto_alloc_size = sizeof(struct mxc_spi_slave),
576 .probe = mxc_spi_probe,