blob: 4b50bca880a8bdc9420d4163b0913bfe9f1a019d [file] [log] [blame]
Grzegorz Bernackid309f922009-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 *
6 * See file CREDITS for list of people who contributed to this
7 * project.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22 * MA 02111-1307 USA
23 */
24
25#include <common.h>
26#include <asm/io.h>
27#include <malloc.h>
28#include <spi.h>
29#include <mpc5xxx.h>
30
31void spi_init(void)
32{
33 struct mpc5xxx_spi *spi = (struct mpc5xxx_spi *)MPC5XXX_SPI;
34 /*
35 * Its important to use the correct order when initializing the
36 * registers
37 */
38 out_8(&spi->ddr, 0x0F); /* set all SPI pins as output */
39 out_8(&spi->pdr, 0x00); /* set SS low */
40 /* SPI is master, SS is general purpose output */
41 out_8(&spi->cr1, SPI_CR_MSTR | SPI_CR_SPE);
42 out_8(&spi->cr2, 0x00); /* normal operation */
43 out_8(&spi->brr, 0x77); /* baud rate: IPB clock / 2048 */
44}
45
46struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
47 unsigned int max_hz, unsigned int mode)
48{
49 struct spi_slave *slave;
50
Simon Glassd034a952013-03-18 19:23:40 +000051 slave = spi_alloc_slave_base(bus, cs);
Grzegorz Bernackid309f922009-06-12 11:33:52 +020052 if (!slave)
53 return NULL;
54
Grzegorz Bernackid309f922009-06-12 11:33:52 +020055 return slave;
56}
57
58void spi_free_slave(struct spi_slave *slave)
59{
60 free(slave);
61}
62
63int spi_claim_bus(struct spi_slave *slave)
64{
65 return 0;
66}
67
68void spi_release_bus(struct spi_slave *slave)
69{
70 return;
71}
72
73int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
74 void *din, unsigned long flags)
75{
76 struct mpc5xxx_spi *spi = (struct mpc5xxx_spi *)MPC5XXX_SPI;
77 int i, iter = bitlen >> 3;
78 const uchar *txp = dout;
79 uchar *rxp = din;
80
81 debug("spi_xfer: slave %u:%u dout %08X din %08X bitlen %u\n",
82 slave->bus, slave->cs, *(uint *) dout, *(uint *) din, bitlen);
83
84 if (flags & SPI_XFER_BEGIN)
85 setbits_8(&spi->pdr, SPI_PDR_SS);
86
87 for (i = 0; i < iter; i++) {
88 udelay(1000);
89 debug("spi_xfer: sending %x\n", txp[i]);
90 out_8(&spi->dr, txp[i]);
91 while (!(in_8(&spi->sr) & SPI_SR_SPIF)) {
92 udelay(1000);
93 if (in_8(&spi->sr) & SPI_SR_WCOL) {
94 rxp[i] = in_8(&spi->dr);
95 puts("spi_xfer: write collision\n");
96 return -1;
97 }
98 }
99 rxp[i] = in_8(&spi->dr);
100 debug("spi_xfer: received %x\n", rxp[i]);
101 }
102 if (flags & SPI_XFER_END)
103 clrbits_8(&spi->pdr, SPI_PDR_SS);
104
105 return 0;
106}