Commit 6325b7780dad8be26ba6fc25ef88ba338c50205b

Authored by Grzegorz Bernacki
Committed by Wolfgang Denk
1 parent 5ec5529b82
Exists in master and in 55 other branches 8qm-imx_v2020.04_5.4.70_2.3.0, emb_lf_v2022.04, emb_lf_v2023.04, imx_v2015.04_4.1.15_1.0.0_ga, pitx_8mp_lf_v2020.04, smarc-8m-android-10.0.0_2.6.0, smarc-8m-android-11.0.0_2.0.0, smarc-8mp-android-11.0.0_2.0.0, smarc-emmc-imx_v2014.04_3.10.53_1.1.0_ga, smarc-emmc-imx_v2014.04_3.14.28_1.0.0_ga, smarc-imx-l5.0.0_1.0.0-ga, smarc-imx6_v2018.03_4.14.98_2.0.0_ga, smarc-imx7_v2017.03_4.9.11_1.0.0_ga, smarc-imx7_v2018.03_4.14.98_2.0.0_ga, smarc-imx_v2014.04_3.14.28_1.0.0_ga, smarc-imx_v2015.04_4.1.15_1.0.0_ga, smarc-imx_v2017.03_4.9.11_1.0.0_ga, smarc-imx_v2017.03_4.9.88_2.0.0_ga, smarc-imx_v2017.03_o8.1.0_1.3.0_8m, smarc-imx_v2018.03_4.14.78_1.0.0_ga, smarc-m6.0.1_2.1.0-ga, smarc-n7.1.2_2.0.0-ga, smarc-rel_imx_4.1.15_2.0.0_ga, smarc_8m-imx_v2018.03_4.14.98_2.0.0_ga, smarc_8m-imx_v2019.04_4.19.35_1.1.0, smarc_8m_00d0-imx_v2018.03_4.14.98_2.0.0_ga, smarc_8mm-imx_v2018.03_4.14.98_2.0.0_ga, smarc_8mm-imx_v2019.04_4.19.35_1.1.0, smarc_8mm-imx_v2020.04_5.4.24_2.1.0, smarc_8mp_lf_v2020.04, smarc_8mq-imx_v2020.04_5.4.24_2.1.0, smarc_8mq_lf_v2020.04, ti-u-boot-2015.07, u-boot-2013.01.y, v2013.10, v2013.10-smarct33, v2013.10-smartmen, v2014.01, v2014.04, v2014.04-smarct33, v2014.04-smarct33-emmc, v2014.04-smartmen, v2014.07, v2014.07-smarct33, v2014.07-smartmen, v2015.07-smarct33, v2015.07-smarct33-emmc, v2015.07-smarct4x, v2016.05-dlt, v2016.05-smarct3x, v2016.05-smarct3x-emmc, v2016.05-smarct4x, v2017.01-smarct3x, v2017.01-smarct3x-emmc, v2017.01-smarct4x

mpc52xx: Add SPI driver.

Signed-off-by: Grzegorz Bernacki <gjb@semihalf.com>

Showing 3 changed files with 128 additions and 0 deletions Side-by-side Diff

drivers/spi/Makefile
... ... @@ -28,6 +28,7 @@
28 28 COBJS-$(CONFIG_ATMEL_DATAFLASH_SPI) += atmel_dataflash_spi.o
29 29 COBJS-$(CONFIG_ATMEL_SPI) += atmel_spi.o
30 30 COBJS-$(CONFIG_BFIN_SPI) += bfin_spi.o
  31 +COBJS-$(CONFIG_MPC52XX_SPI) += mpc52xx_spi.o
31 32 COBJS-$(CONFIG_MPC8XXX_SPI) += mpc8xxx_spi.o
32 33 COBJS-$(CONFIG_MXC_SPI) += mxc_spi.o
33 34 COBJS-$(CONFIG_SOFT_SPI) += soft_spi.o
drivers/spi/mpc52xx_spi.c
  1 +/*
  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 +
  31 +void 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 +
  46 +struct 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 +
  51 + slave = malloc(sizeof(struct spi_slave));
  52 + if (!slave)
  53 + return NULL;
  54 +
  55 + slave->bus = bus;
  56 + slave->cs = cs;
  57 +
  58 + return slave;
  59 +}
  60 +
  61 +void spi_free_slave(struct spi_slave *slave)
  62 +{
  63 + free(slave);
  64 +}
  65 +
  66 +int spi_claim_bus(struct spi_slave *slave)
  67 +{
  68 + return 0;
  69 +}
  70 +
  71 +void spi_release_bus(struct spi_slave *slave)
  72 +{
  73 + return;
  74 +}
  75 +
  76 +int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
  77 + void *din, unsigned long flags)
  78 +{
  79 + struct mpc5xxx_spi *spi = (struct mpc5xxx_spi *)MPC5XXX_SPI;
  80 + int i, iter = bitlen >> 3;
  81 + const uchar *txp = dout;
  82 + uchar *rxp = din;
  83 +
  84 + debug("spi_xfer: slave %u:%u dout %08X din %08X bitlen %u\n",
  85 + slave->bus, slave->cs, *(uint *) dout, *(uint *) din, bitlen);
  86 +
  87 + if (flags & SPI_XFER_BEGIN)
  88 + setbits_8(&spi->pdr, SPI_PDR_SS);
  89 +
  90 + for (i = 0; i < iter; i++) {
  91 + udelay(1000);
  92 + debug("spi_xfer: sending %x\n", txp[i]);
  93 + out_8(&spi->dr, txp[i]);
  94 + while (!(in_8(&spi->sr) & SPI_SR_SPIF)) {
  95 + udelay(1000);
  96 + if (in_8(&spi->sr) & SPI_SR_WCOL) {
  97 + rxp[i] = in_8(&spi->dr);
  98 + puts("spi_xfer: write collision\n");
  99 + return -1;
  100 + }
  101 + }
  102 + rxp[i] = in_8(&spi->dr);
  103 + debug("spi_xfer: received %x\n", rxp[i]);
  104 + }
  105 + if (flags & SPI_XFER_END)
  106 + clrbits_8(&spi->pdr, SPI_PDR_SS);
  107 +
  108 + return 0;
  109 +}
... ... @@ -392,6 +392,24 @@
392 392 #define I2C_IF 0x02
393 393 #define I2C_RXAK 0x01
394 394  
  395 +/* SPI control register 1 bits */
  396 +#define SPI_CR_LSBFE 0x01
  397 +#define SPI_CR_SSOE 0x02
  398 +#define SPI_CR_CPHA 0x04
  399 +#define SPI_CR_CPOL 0x08
  400 +#define SPI_CR_MSTR 0x10
  401 +#define SPI_CR_SWOM 0x20
  402 +#define SPI_CR_SPE 0x40
  403 +#define SPI_CR_SPIE 0x80
  404 +
  405 +/* SPI status register bits */
  406 +#define SPI_SR_MODF 0x10
  407 +#define SPI_SR_WCOL 0x40
  408 +#define SPI_SR_SPIF 0x80
  409 +
  410 +/* SPI port data register bits */
  411 +#define SPI_PDR_SS 0x08
  412 +
395 413 /* Programmable Serial Controller (PSC) status register bits */
396 414 #define PSC_SR_CDE 0x0080
397 415 #define PSC_SR_RXRDY 0x0100