arm: vf610: fix double iomux configuration for vf610twr board
[u-boot/qq2440-u-boot.git] / drivers / spi / mpc52xx_spi.c
blob4613bec2aa5893ee8fc95be92fa152a597197cfb
1 /*
2 * (C) Copyright 2009
3 * Frank Bodammer <frank.bodammer@gcd-solutions.de>
4 * (C) Copyright 2009 Semihalf, Grzegorz Bernacki
6 * SPDX-License-Identifier: GPL-2.0+
7 */
9 #include <common.h>
10 #include <asm/io.h>
11 #include <malloc.h>
12 #include <spi.h>
13 #include <mpc5xxx.h>
15 void spi_init(void)
17 struct mpc5xxx_spi *spi = (struct mpc5xxx_spi *)MPC5XXX_SPI;
19 * Its important to use the correct order when initializing the
20 * registers
22 out_8(&spi->ddr, 0x0F); /* set all SPI pins as output */
23 out_8(&spi->pdr, 0x00); /* set SS low */
24 /* SPI is master, SS is general purpose output */
25 out_8(&spi->cr1, SPI_CR_MSTR | SPI_CR_SPE);
26 out_8(&spi->cr2, 0x00); /* normal operation */
27 out_8(&spi->brr, 0x77); /* baud rate: IPB clock / 2048 */
30 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
31 unsigned int max_hz, unsigned int mode)
33 struct spi_slave *slave;
35 slave = spi_alloc_slave_base(bus, cs);
36 if (!slave)
37 return NULL;
39 return slave;
42 void spi_free_slave(struct spi_slave *slave)
44 free(slave);
47 int spi_claim_bus(struct spi_slave *slave)
49 return 0;
52 void spi_release_bus(struct spi_slave *slave)
54 return;
57 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
58 void *din, unsigned long flags)
60 struct mpc5xxx_spi *spi = (struct mpc5xxx_spi *)MPC5XXX_SPI;
61 int i, iter = bitlen >> 3;
62 const uchar *txp = dout;
63 uchar *rxp = din;
65 debug("spi_xfer: slave %u:%u dout %08X din %08X bitlen %u\n",
66 slave->bus, slave->cs, *(uint *) dout, *(uint *) din, bitlen);
68 if (flags & SPI_XFER_BEGIN)
69 setbits_8(&spi->pdr, SPI_PDR_SS);
71 for (i = 0; i < iter; i++) {
72 udelay(1000);
73 debug("spi_xfer: sending %x\n", txp[i]);
74 out_8(&spi->dr, txp[i]);
75 while (!(in_8(&spi->sr) & SPI_SR_SPIF)) {
76 udelay(1000);
77 if (in_8(&spi->sr) & SPI_SR_WCOL) {
78 rxp[i] = in_8(&spi->dr);
79 puts("spi_xfer: write collision\n");
80 return -1;
83 rxp[i] = in_8(&spi->dr);
84 debug("spi_xfer: received %x\n", rxp[i]);
86 if (flags & SPI_XFER_END)
87 clrbits_8(&spi->pdr, SPI_PDR_SS);
89 return 0;