blob: 4613bec2aa5893ee8fc95be92fa152a597197cfb [file] [log] [blame]
Grzegorz Bernacki6325b772009-06-12 11:33:52 +02001/*
2 * (C) Copyright 2009
3 * Frank Bodammer <frank.bodammer@gcd-solutions.de>
4 * (C) Copyright 2009 Semihalf, Grzegorz Bernacki
5 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Grzegorz Bernacki6325b772009-06-12 11:33:52 +02007 */
8
9#include <common.h>
10#include <asm/io.h>
11#include <malloc.h>
12#include <spi.h>
13#include <mpc5xxx.h>
14
15void spi_init(void)
16{
17 struct mpc5xxx_spi *spi = (struct mpc5xxx_spi *)MPC5XXX_SPI;
18 /*
19 * Its important to use the correct order when initializing the
20 * registers
21 */
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 */
28}
29
30struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
31 unsigned int max_hz, unsigned int mode)
32{
33 struct spi_slave *slave;
34
Simon Glassd3504fe2013-03-18 19:23:40 +000035 slave = spi_alloc_slave_base(bus, cs);
Grzegorz Bernacki6325b772009-06-12 11:33:52 +020036 if (!slave)
37 return NULL;
38
Grzegorz Bernacki6325b772009-06-12 11:33:52 +020039 return slave;
40}
41
42void spi_free_slave(struct spi_slave *slave)
43{
44 free(slave);
45}
46
47int spi_claim_bus(struct spi_slave *slave)
48{
49 return 0;
50}
51
52void spi_release_bus(struct spi_slave *slave)
53{
54 return;
55}
56
57int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
58 void *din, unsigned long flags)
59{
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;
64
65 debug("spi_xfer: slave %u:%u dout %08X din %08X bitlen %u\n",
66 slave->bus, slave->cs, *(uint *) dout, *(uint *) din, bitlen);
67
68 if (flags & SPI_XFER_BEGIN)
69 setbits_8(&spi->pdr, SPI_PDR_SS);
70
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;
81 }
82 }
83 rxp[i] = in_8(&spi->dr);
84 debug("spi_xfer: received %x\n", rxp[i]);
85 }
86 if (flags & SPI_XFER_END)
87 clrbits_8(&spi->pdr, SPI_PDR_SS);
88
89 return 0;
90}