1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2008, Guennadi Liakhovetski <lg@denx.de>
4 */
5
6 #include <common.h>
7 #include <dm.h>
8 #include <malloc.h>
9 #include <spi.h>
10 #include <linux/errno.h>
11 #include <asm/io.h>
12 #include <asm/gpio.h>
13 #include <asm/arch/imx-regs.h>
14 #include <asm/arch/clock.h>
15 #include <asm/mach-imx/spi.h>
16
17 DECLARE_GLOBAL_DATA_PTR;
18
19 #ifdef CONFIG_MX27
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 */
22
23 #error "i.MX27 CSPI not supported due to drastic differences in register definitions" \
24 "See linux mxc_spi driver from Freescale for details."
25 #endif
26
board_spi_cs_gpio(unsigned bus,unsigned cs)27 __weak int board_spi_cs_gpio(unsigned bus, unsigned cs)
28 {
29 return -1;
30 }
31
32 #define OUT MXC_GPIO_DIRECTION_OUT
33
34 #define reg_read readl
35 #define reg_write(a, v) writel(v, a)
36
37 #if !defined(CONFIG_SYS_SPI_MXC_WAIT)
38 #define CONFIG_SYS_SPI_MXC_WAIT (CONFIG_SYS_HZ/100) /* 10 ms */
39 #endif
40
41 #define MAX_CS_COUNT 4
42
43 struct mxc_spi_slave {
44 struct spi_slave slave;
45 unsigned long base;
46 u32 ctrl_reg;
47 #if defined(MXC_ECSPI)
48 u32 cfg_reg;
49 #endif
50 int gpio;
51 int ss_pol;
52 unsigned int max_hz;
53 unsigned int mode;
54 struct gpio_desc ss;
55 struct gpio_desc cs_gpios[MAX_CS_COUNT];
56 struct udevice *dev;
57 };
58
to_mxc_spi_slave(struct spi_slave * slave)59 static inline struct mxc_spi_slave *to_mxc_spi_slave(struct spi_slave *slave)
60 {
61 return container_of(slave, struct mxc_spi_slave, slave);
62 }
63
mxc_spi_cs_activate(struct mxc_spi_slave * mxcs)64 static void mxc_spi_cs_activate(struct mxc_spi_slave *mxcs)
65 {
66 #if defined(CONFIG_DM_SPI)
67 struct udevice *dev = mxcs->dev;
68 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
69
70 u32 cs = slave_plat->cs;
71
72 if (!dm_gpio_is_valid(&mxcs->cs_gpios[cs]))
73 return;
74
75 dm_gpio_set_value(&mxcs->cs_gpios[cs], 1);
76 #else
77 if (mxcs->gpio > 0)
78 gpio_set_value(mxcs->gpio, mxcs->ss_pol);
79 #endif
80 }
81
mxc_spi_cs_deactivate(struct mxc_spi_slave * mxcs)82 static void mxc_spi_cs_deactivate(struct mxc_spi_slave *mxcs)
83 {
84 #if defined(CONFIG_DM_SPI)
85 struct udevice *dev = mxcs->dev;
86 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
87
88 u32 cs = slave_plat->cs;
89
90 if (!dm_gpio_is_valid(&mxcs->cs_gpios[cs]))
91 return;
92
93 dm_gpio_set_value(&mxcs->cs_gpios[cs], 0);
94 #else
95 if (mxcs->gpio > 0)
96 gpio_set_value(mxcs->gpio, !(mxcs->ss_pol));
97 #endif
98 }
99
get_cspi_div(u32 div)100 u32 get_cspi_div(u32 div)
101 {
102 int i;
103
104 for (i = 0; i < 8; i++) {
105 if (div <= (4 << i))
106 return i;
107 }
108 return i;
109 }
110
111 #ifdef MXC_CSPI
spi_cfg_mxc(struct mxc_spi_slave * mxcs,unsigned int cs)112 static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs)
113 {
114 unsigned int ctrl_reg;
115 u32 clk_src;
116 u32 div;
117 unsigned int max_hz = mxcs->max_hz;
118 unsigned int mode = mxcs->mode;
119
120 clk_src = mxc_get_clock(MXC_CSPI_CLK);
121
122 div = DIV_ROUND_UP(clk_src, max_hz);
123 div = get_cspi_div(div);
124
125 debug("clk %d Hz, div %d, real clk %d Hz\n",
126 max_hz, div, clk_src / (4 << div));
127
128 ctrl_reg = MXC_CSPICTRL_CHIPSELECT(cs) |
129 MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS) |
130 MXC_CSPICTRL_DATARATE(div) |
131 MXC_CSPICTRL_EN |
132 #ifdef CONFIG_MX35
133 MXC_CSPICTRL_SSCTL |
134 #endif
135 MXC_CSPICTRL_MODE;
136
137 if (mode & SPI_CPHA)
138 ctrl_reg |= MXC_CSPICTRL_PHA;
139 if (mode & SPI_CPOL)
140 ctrl_reg |= MXC_CSPICTRL_POL;
141 if (mode & SPI_CS_HIGH)
142 ctrl_reg |= MXC_CSPICTRL_SSPOL;
143 mxcs->ctrl_reg = ctrl_reg;
144
145 return 0;
146 }
147 #endif
148
149 #ifdef MXC_ECSPI
spi_cfg_mxc(struct mxc_spi_slave * mxcs,unsigned int cs)150 static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs)
151 {
152 u32 clk_src = mxc_get_clock(MXC_CSPI_CLK);
153 s32 reg_ctrl, reg_config;
154 u32 ss_pol = 0, sclkpol = 0, sclkpha = 0, sclkctl = 0;
155 u32 pre_div = 0, post_div = 0;
156 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
157 unsigned int max_hz = mxcs->max_hz;
158 unsigned int mode = mxcs->mode;
159
160 /*
161 * Reset SPI and set all CSs to master mode, if toggling
162 * between slave and master mode we might see a glitch
163 * on the clock line
164 */
165 reg_ctrl = MXC_CSPICTRL_MODE_MASK;
166 reg_write(®s->ctrl, reg_ctrl);
167 reg_ctrl |= MXC_CSPICTRL_EN;
168 reg_write(®s->ctrl, reg_ctrl);
169
170 if (clk_src > max_hz) {
171 pre_div = (clk_src - 1) / max_hz;
172 /* fls(1) = 1, fls(0x80000000) = 32, fls(16) = 5 */
173 post_div = fls(pre_div);
174 if (post_div > 4) {
175 post_div -= 4;
176 if (post_div >= 16) {
177 printf("Error: no divider for the freq: %d\n",
178 max_hz);
179 return -1;
180 }
181 pre_div >>= post_div;
182 } else {
183 post_div = 0;
184 }
185 }
186
187 debug("pre_div = %d, post_div=%d\n", pre_div, post_div);
188 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_SELCHAN(3)) |
189 MXC_CSPICTRL_SELCHAN(cs);
190 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_PREDIV(0x0F)) |
191 MXC_CSPICTRL_PREDIV(pre_div);
192 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_POSTDIV(0x0F)) |
193 MXC_CSPICTRL_POSTDIV(post_div);
194
195 if (mode & SPI_CS_HIGH)
196 ss_pol = 1;
197
198 if (mode & SPI_CPOL) {
199 sclkpol = 1;
200 sclkctl = 1;
201 }
202
203 if (mode & SPI_CPHA)
204 sclkpha = 1;
205
206 reg_config = reg_read(®s->cfg);
207
208 /*
209 * Configuration register setup
210 * The MX51 supports different setup for each SS
211 */
212 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_SSPOL))) |
213 (ss_pol << (cs + MXC_CSPICON_SSPOL));
214 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_POL))) |
215 (sclkpol << (cs + MXC_CSPICON_POL));
216 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_CTL))) |
217 (sclkctl << (cs + MXC_CSPICON_CTL));
218 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_PHA))) |
219 (sclkpha << (cs + MXC_CSPICON_PHA));
220
221 debug("reg_ctrl = 0x%x\n", reg_ctrl);
222 reg_write(®s->ctrl, reg_ctrl);
223 debug("reg_config = 0x%x\n", reg_config);
224 reg_write(®s->cfg, reg_config);
225
226 /* save config register and control register */
227 mxcs->ctrl_reg = reg_ctrl;
228 mxcs->cfg_reg = reg_config;
229
230 /* clear interrupt reg */
231 reg_write(®s->intr, 0);
232 reg_write(®s->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
233
234 return 0;
235 }
236 #endif
237
spi_xchg_single(struct mxc_spi_slave * mxcs,unsigned int bitlen,const u8 * dout,u8 * din,unsigned long flags)238 int spi_xchg_single(struct mxc_spi_slave *mxcs, unsigned int bitlen,
239 const u8 *dout, u8 *din, unsigned long flags)
240 {
241 int nbytes = DIV_ROUND_UP(bitlen, 8);
242 u32 data, cnt, i;
243 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
244 u32 ts;
245 int status;
246
247 debug("%s: bitlen %d dout 0x%lx din 0x%lx\n",
248 __func__, bitlen, (ulong)dout, (ulong)din);
249
250 mxcs->ctrl_reg = (mxcs->ctrl_reg &
251 ~MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS)) |
252 MXC_CSPICTRL_BITCOUNT(bitlen - 1);
253
254 reg_write(®s->ctrl, mxcs->ctrl_reg | MXC_CSPICTRL_EN);
255 #ifdef MXC_ECSPI
256 reg_write(®s->cfg, mxcs->cfg_reg);
257 #endif
258
259 /* Clear interrupt register */
260 reg_write(®s->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
261
262 /*
263 * The SPI controller works only with words,
264 * check if less than a word is sent.
265 * Access to the FIFO is only 32 bit
266 */
267 if (bitlen % 32) {
268 data = 0;
269 cnt = (bitlen % 32) / 8;
270 if (dout) {
271 for (i = 0; i < cnt; i++) {
272 data = (data << 8) | (*dout++ & 0xFF);
273 }
274 }
275 debug("Sending SPI 0x%x\n", data);
276
277 reg_write(®s->txdata, data);
278 nbytes -= cnt;
279 }
280
281 data = 0;
282
283 while (nbytes > 0) {
284 data = 0;
285 if (dout) {
286 /* Buffer is not 32-bit aligned */
287 if ((unsigned long)dout & 0x03) {
288 data = 0;
289 for (i = 0; i < 4; i++)
290 data = (data << 8) | (*dout++ & 0xFF);
291 } else {
292 data = *(u32 *)dout;
293 data = cpu_to_be32(data);
294 dout += 4;
295 }
296 }
297 debug("Sending SPI 0x%x\n", data);
298 reg_write(®s->txdata, data);
299 nbytes -= 4;
300 }
301
302 /* FIFO is written, now starts the transfer setting the XCH bit */
303 reg_write(®s->ctrl, mxcs->ctrl_reg |
304 MXC_CSPICTRL_EN | MXC_CSPICTRL_XCH);
305
306 ts = get_timer(0);
307 status = reg_read(®s->stat);
308 /* Wait until the TC (Transfer completed) bit is set */
309 while ((status & MXC_CSPICTRL_TC) == 0) {
310 if (get_timer(ts) > CONFIG_SYS_SPI_MXC_WAIT) {
311 printf("spi_xchg_single: Timeout!\n");
312 return -1;
313 }
314 status = reg_read(®s->stat);
315 }
316
317 /* Transfer completed, clear any pending request */
318 reg_write(®s->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
319
320 nbytes = DIV_ROUND_UP(bitlen, 8);
321
322 cnt = nbytes % 32;
323
324 if (bitlen % 32) {
325 data = reg_read(®s->rxdata);
326 cnt = (bitlen % 32) / 8;
327 data = cpu_to_be32(data) >> ((sizeof(data) - cnt) * 8);
328 debug("SPI Rx unaligned: 0x%x\n", data);
329 if (din) {
330 memcpy(din, &data, cnt);
331 din += cnt;
332 }
333 nbytes -= cnt;
334 }
335
336 while (nbytes > 0) {
337 u32 tmp;
338 tmp = reg_read(®s->rxdata);
339 data = cpu_to_be32(tmp);
340 debug("SPI Rx: 0x%x 0x%x\n", tmp, data);
341 cnt = min_t(u32, nbytes, sizeof(data));
342 if (din) {
343 memcpy(din, &data, cnt);
344 din += cnt;
345 }
346 nbytes -= cnt;
347 }
348
349 return 0;
350
351 }
352
mxc_spi_xfer_internal(struct mxc_spi_slave * mxcs,unsigned int bitlen,const void * dout,void * din,unsigned long flags)353 static int mxc_spi_xfer_internal(struct mxc_spi_slave *mxcs,
354 unsigned int bitlen, const void *dout,
355 void *din, unsigned long flags)
356 {
357 int n_bytes = DIV_ROUND_UP(bitlen, 8);
358 int n_bits;
359 int ret;
360 u32 blk_size;
361 u8 *p_outbuf = (u8 *)dout;
362 u8 *p_inbuf = (u8 *)din;
363
364 if (!mxcs)
365 return -EINVAL;
366
367 if (flags & SPI_XFER_BEGIN)
368 mxc_spi_cs_activate(mxcs);
369
370 while (n_bytes > 0) {
371 if (n_bytes < MAX_SPI_BYTES)
372 blk_size = n_bytes;
373 else
374 blk_size = MAX_SPI_BYTES;
375
376 n_bits = blk_size * 8;
377
378 ret = spi_xchg_single(mxcs, n_bits, p_outbuf, p_inbuf, 0);
379
380 if (ret)
381 return ret;
382 if (dout)
383 p_outbuf += blk_size;
384 if (din)
385 p_inbuf += blk_size;
386 n_bytes -= blk_size;
387 }
388
389 if (flags & SPI_XFER_END) {
390 mxc_spi_cs_deactivate(mxcs);
391 }
392
393 return 0;
394 }
395
mxc_spi_claim_bus_internal(struct mxc_spi_slave * mxcs,int cs)396 static int mxc_spi_claim_bus_internal(struct mxc_spi_slave *mxcs, int cs)
397 {
398 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
399 int ret;
400
401 reg_write(®s->rxdata, 1);
402 udelay(1);
403 ret = spi_cfg_mxc(mxcs, cs);
404 if (ret) {
405 printf("mxc_spi: cannot setup SPI controller\n");
406 return ret;
407 }
408 reg_write(®s->period, MXC_CSPIPERIOD_32KHZ);
409 reg_write(®s->intr, 0);
410
411 return 0;
412 }
413
414 #ifndef CONFIG_DM_SPI
spi_xfer(struct spi_slave * slave,unsigned int bitlen,const void * dout,void * din,unsigned long flags)415 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
416 void *din, unsigned long flags)
417 {
418 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
419
420 return mxc_spi_xfer_internal(mxcs, bitlen, dout, din, flags);
421 }
422
423 /*
424 * Some SPI devices require active chip-select over multiple
425 * transactions, we achieve this using a GPIO. Still, the SPI
426 * controller has to be configured to use one of its own chipselects.
427 * To use this feature you have to implement board_spi_cs_gpio() to assign
428 * a gpio value for each cs (-1 if cs doesn't need to use gpio).
429 * You must use some unused on this SPI controller cs between 0 and 3.
430 */
setup_cs_gpio(struct mxc_spi_slave * mxcs,unsigned int bus,unsigned int cs)431 static int setup_cs_gpio(struct mxc_spi_slave *mxcs,
432 unsigned int bus, unsigned int cs)
433 {
434 int ret;
435
436 mxcs->gpio = board_spi_cs_gpio(bus, cs);
437 if (mxcs->gpio == -1)
438 return 0;
439
440 gpio_request(mxcs->gpio, "spi-cs");
441 ret = gpio_direction_output(mxcs->gpio, !(mxcs->ss_pol));
442 if (ret) {
443 printf("mxc_spi: cannot setup gpio %d\n", mxcs->gpio);
444 return -EINVAL;
445 }
446
447 return 0;
448 }
449
450 static unsigned long spi_bases[] = {
451 MXC_SPI_BASE_ADDRESSES
452 };
453
spi_setup_slave(unsigned int bus,unsigned int cs,unsigned int max_hz,unsigned int mode)454 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
455 unsigned int max_hz, unsigned int mode)
456 {
457 struct mxc_spi_slave *mxcs;
458 int ret;
459
460 if (bus >= ARRAY_SIZE(spi_bases))
461 return NULL;
462
463 if (max_hz == 0) {
464 printf("Error: desired clock is 0\n");
465 return NULL;
466 }
467
468 mxcs = spi_alloc_slave(struct mxc_spi_slave, bus, cs);
469 if (!mxcs) {
470 puts("mxc_spi: SPI Slave not allocated !\n");
471 return NULL;
472 }
473
474 mxcs->ss_pol = (mode & SPI_CS_HIGH) ? 1 : 0;
475
476 ret = setup_cs_gpio(mxcs, bus, cs);
477 if (ret < 0) {
478 free(mxcs);
479 return NULL;
480 }
481
482 mxcs->base = spi_bases[bus];
483 mxcs->max_hz = max_hz;
484 mxcs->mode = mode;
485
486 return &mxcs->slave;
487 }
488
spi_free_slave(struct spi_slave * slave)489 void spi_free_slave(struct spi_slave *slave)
490 {
491 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
492
493 free(mxcs);
494 }
495
spi_claim_bus(struct spi_slave * slave)496 int spi_claim_bus(struct spi_slave *slave)
497 {
498 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
499
500 return mxc_spi_claim_bus_internal(mxcs, slave->cs);
501 }
502
spi_release_bus(struct spi_slave * slave)503 void spi_release_bus(struct spi_slave *slave)
504 {
505 /* TODO: Shut the controller down */
506 }
507 #else
508
mxc_spi_probe(struct udevice * bus)509 static int mxc_spi_probe(struct udevice *bus)
510 {
511 struct mxc_spi_slave *mxcs = dev_get_platdata(bus);
512 int node = dev_of_offset(bus);
513 const void *blob = gd->fdt_blob;
514 int ret;
515 int i;
516
517 ret = gpio_request_list_by_name(bus, "cs-gpios", mxcs->cs_gpios,
518 ARRAY_SIZE(mxcs->cs_gpios), 0);
519 if (ret < 0) {
520 pr_err("Can't get %s gpios! Error: %d", bus->name, ret);
521 return ret;
522 }
523
524 for (i = 0; i < ARRAY_SIZE(mxcs->cs_gpios); i++) {
525 if (!dm_gpio_is_valid(&mxcs->cs_gpios[i]))
526 continue;
527
528 ret = dm_gpio_set_dir_flags(&mxcs->cs_gpios[i],
529 GPIOD_IS_OUT | GPIOD_ACTIVE_LOW);
530 if (ret) {
531 dev_err(bus, "Setting cs %d error\n", i);
532 return ret;
533 }
534 }
535
536 mxcs->base = devfdt_get_addr(bus);
537 if (mxcs->base == FDT_ADDR_T_NONE)
538 return -ENODEV;
539
540 mxcs->max_hz = fdtdec_get_int(blob, node, "spi-max-frequency",
541 20000000);
542
543 return 0;
544 }
545
mxc_spi_xfer(struct udevice * dev,unsigned int bitlen,const void * dout,void * din,unsigned long flags)546 static int mxc_spi_xfer(struct udevice *dev, unsigned int bitlen,
547 const void *dout, void *din, unsigned long flags)
548 {
549 struct mxc_spi_slave *mxcs = dev_get_platdata(dev->parent);
550
551
552 return mxc_spi_xfer_internal(mxcs, bitlen, dout, din, flags);
553 }
554
mxc_spi_claim_bus(struct udevice * dev)555 static int mxc_spi_claim_bus(struct udevice *dev)
556 {
557 struct mxc_spi_slave *mxcs = dev_get_platdata(dev->parent);
558 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
559
560 mxcs->dev = dev;
561
562 return mxc_spi_claim_bus_internal(mxcs, slave_plat->cs);
563 }
564
mxc_spi_release_bus(struct udevice * dev)565 static int mxc_spi_release_bus(struct udevice *dev)
566 {
567 return 0;
568 }
569
mxc_spi_set_speed(struct udevice * bus,uint speed)570 static int mxc_spi_set_speed(struct udevice *bus, uint speed)
571 {
572 /* Nothing to do */
573 return 0;
574 }
575
mxc_spi_set_mode(struct udevice * bus,uint mode)576 static int mxc_spi_set_mode(struct udevice *bus, uint mode)
577 {
578 struct mxc_spi_slave *mxcs = dev_get_platdata(bus);
579
580 mxcs->mode = mode;
581 mxcs->ss_pol = (mode & SPI_CS_HIGH) ? 1 : 0;
582
583 return 0;
584 }
585
586 static const struct dm_spi_ops mxc_spi_ops = {
587 .claim_bus = mxc_spi_claim_bus,
588 .release_bus = mxc_spi_release_bus,
589 .xfer = mxc_spi_xfer,
590 .set_speed = mxc_spi_set_speed,
591 .set_mode = mxc_spi_set_mode,
592 };
593
594 static const struct udevice_id mxc_spi_ids[] = {
595 { .compatible = "fsl,imx51-ecspi" },
596 { }
597 };
598
599 U_BOOT_DRIVER(mxc_spi) = {
600 .name = "mxc_spi",
601 .id = UCLASS_SPI,
602 .of_match = mxc_spi_ids,
603 .ops = &mxc_spi_ops,
604 .platdata_auto_alloc_size = sizeof(struct mxc_spi_slave),
605 .probe = mxc_spi_probe,
606 };
607 #endif
608