]> Git Repo - J-u-boot.git/blobdiff - drivers/spi/davinci_spi.c
spi: davinci_spi: Move header code to driver
[J-u-boot.git] / drivers / spi / davinci_spi.c
index 13aca52c7e2add329fb2c4334f9d3bb4d1752684..e0ecf995cecee945cff41650e73ce252e509fe74 100644 (file)
  *
  * Copyright (C) 2007 Atmel Corporation
  *
- * See file CREDITS for list of people who contributed to this
- * project.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
- * MA 02111-1307 USA
+ * SPDX-License-Identifier:    GPL-2.0+
  */
 #include <common.h>
 #include <spi.h>
 #include <malloc.h>
 #include <asm/io.h>
 #include <asm/arch/hardware.h>
-#include "davinci_spi.h"
+
+struct davinci_spi_regs {
+       dv_reg  gcr0;           /* 0x00 */
+       dv_reg  gcr1;           /* 0x04 */
+       dv_reg  int0;           /* 0x08 */
+       dv_reg  lvl;            /* 0x0c */
+       dv_reg  flg;            /* 0x10 */
+       dv_reg  pc0;            /* 0x14 */
+       dv_reg  pc1;            /* 0x18 */
+       dv_reg  pc2;            /* 0x1c */
+       dv_reg  pc3;            /* 0x20 */
+       dv_reg  pc4;            /* 0x24 */
+       dv_reg  pc5;            /* 0x28 */
+       dv_reg  rsvd[3];
+       dv_reg  dat0;           /* 0x38 */
+       dv_reg  dat1;           /* 0x3c */
+       dv_reg  buf;            /* 0x40 */
+       dv_reg  emu;            /* 0x44 */
+       dv_reg  delay;          /* 0x48 */
+       dv_reg  def;            /* 0x4c */
+       dv_reg  fmt0;           /* 0x50 */
+       dv_reg  fmt1;           /* 0x54 */
+       dv_reg  fmt2;           /* 0x58 */
+       dv_reg  fmt3;           /* 0x5c */
+       dv_reg  intvec0;        /* 0x60 */
+       dv_reg  intvec1;        /* 0x64 */
+};
+
+#define BIT(x)                 (1 << (x))
+
+/* SPIGCR0 */
+#define SPIGCR0_SPIENA_MASK    0x1
+#define SPIGCR0_SPIRST_MASK    0x0
+
+/* SPIGCR0 */
+#define SPIGCR1_CLKMOD_MASK    BIT(1)
+#define SPIGCR1_MASTER_MASK    BIT(0)
+#define SPIGCR1_SPIENA_MASK    BIT(24)
+
+/* SPIPC0 */
+#define SPIPC0_DIFUN_MASK      BIT(11)         /* SIMO */
+#define SPIPC0_DOFUN_MASK      BIT(10)         /* SOMI */
+#define SPIPC0_CLKFUN_MASK     BIT(9)          /* CLK */
+#define SPIPC0_EN0FUN_MASK     BIT(0)
+
+/* SPIFMT0 */
+#define SPIFMT_SHIFTDIR_SHIFT  20
+#define SPIFMT_POLARITY_SHIFT  17
+#define SPIFMT_PHASE_SHIFT     16
+#define SPIFMT_PRESCALE_SHIFT  8
+
+/* SPIDAT1 */
+#define SPIDAT1_CSHOLD_SHIFT   28
+#define SPIDAT1_CSNR_SHIFT     16
+
+/* SPIDELAY */
+#define SPI_C2TDELAY_SHIFT     24
+#define SPI_T2CDELAY_SHIFT     16
+
+/* SPIBUF */
+#define SPIBUF_RXEMPTY_MASK    BIT(31)
+#define SPIBUF_TXFULL_MASK     BIT(29)
+
+/* SPIDEF */
+#define SPIDEF_CSDEF0_MASK     BIT(0)
+
+#define SPI0_BUS               0
+#define SPI0_BASE              CONFIG_SYS_SPI_BASE
+/*
+ * Define default SPI0_NUM_CS as 1 for existing platforms that uses this
+ * driver. Platform can configure number of CS using CONFIG_SYS_SPI0_NUM_CS
+ * if more than one CS is supported and by defining CONFIG_SYS_SPI0.
+ */
+#ifndef CONFIG_SYS_SPI0
+#define SPI0_NUM_CS            1
+#else
+#define SPI0_NUM_CS            CONFIG_SYS_SPI0_NUM_CS
+#endif
+
+/*
+ * define CONFIG_SYS_SPI1 when platform has spi-1 device (bus #1) and
+ * CONFIG_SYS_SPI1_NUM_CS defines number of CS on this bus
+ */
+#ifdef CONFIG_SYS_SPI1
+#define SPI1_BUS               1
+#define SPI1_NUM_CS            CONFIG_SYS_SPI1_NUM_CS
+#define SPI1_BASE              CONFIG_SYS_SPI1_BASE
+#endif
+
+/*
+ * define CONFIG_SYS_SPI2 when platform has spi-2 device (bus #2) and
+ * CONFIG_SYS_SPI2_NUM_CS defines number of CS on this bus
+ */
+#ifdef CONFIG_SYS_SPI2
+#define SPI2_BUS               2
+#define SPI2_NUM_CS            CONFIG_SYS_SPI2_NUM_CS
+#define SPI2_BASE              CONFIG_SYS_SPI2_BASE
+#endif
+
+struct davinci_spi_slave {
+       struct spi_slave slave;
+       struct davinci_spi_regs *regs;
+       unsigned int freq;
+};
+
+static inline struct davinci_spi_slave *to_davinci_spi(struct spi_slave *slave)
+{
+       return container_of(slave, struct davinci_spi_slave, slave);
+}
 
 void spi_init()
 {
@@ -44,13 +136,28 @@ struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
        if (!spi_cs_is_valid(bus, cs))
                return NULL;
 
-       ds = malloc(sizeof(*ds));
+       ds = spi_alloc_slave(struct davinci_spi_slave, bus, cs);
        if (!ds)
                return NULL;
 
-       ds->slave.bus = bus;
-       ds->slave.cs = cs;
-       ds->regs = (struct davinci_spi_regs *)CONFIG_SYS_SPI_BASE;
+       switch (bus) {
+       case SPI0_BUS:
+               ds->regs = (struct davinci_spi_regs *)SPI0_BASE;
+               break;
+#ifdef CONFIG_SYS_SPI1
+       case SPI1_BUS:
+               ds->regs = (struct davinci_spi_regs *)SPI1_BASE;
+               break;
+#endif
+#ifdef CONFIG_SYS_SPI2
+       case SPI2_BUS:
+               ds->regs = (struct davinci_spi_regs *)SPI2_BASE;
+               break;
+#endif
+       default: /* Invalid bus number */
+               return NULL;
+       }
+
        ds->freq = max_hz;
 
        return &ds->slave;
@@ -77,7 +184,7 @@ int spi_claim_bus(struct spi_slave *slave)
        writel(SPIGCR1_MASTER_MASK | SPIGCR1_CLKMOD_MASK, &ds->regs->gcr1);
 
        /* CS, CLK, SIMO and SOMI are functional pins */
-       writel((SPIPC0_EN0FUN_MASK | SPIPC0_CLKFUN_MASK |
+       writel(((1 << slave->cs) | SPIPC0_CLKFUN_MASK |
                SPIPC0_DOFUN_MASK | SPIPC0_DIFUN_MASK), &ds->regs->pc0);
 
        /* setup format */
@@ -282,7 +389,30 @@ out:
 
 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
 {
-       return bus == 0 && cs == 0;
+       int ret = 0;
+
+       switch (bus) {
+       case SPI0_BUS:
+               if (cs < SPI0_NUM_CS)
+                       ret = 1;
+               break;
+#ifdef CONFIG_SYS_SPI1
+       case SPI1_BUS:
+               if (cs < SPI1_NUM_CS)
+                       ret = 1;
+               break;
+#endif
+#ifdef CONFIG_SYS_SPI2
+       case SPI2_BUS:
+               if (cs < SPI2_NUM_CS)
+                       ret = 1;
+               break;
+#endif
+       default:
+               /* Invalid bus number. Do nothing */
+               break;
+       }
+       return ret;
 }
 
 void spi_cs_activate(struct spi_slave *slave)
This page took 0.034063 seconds and 4 git commands to generate.