]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - drivers/spi/kirkwood_spi.c
Merge branch 'master' of git://git.denx.de/u-boot-spi
[karo-tx-uboot.git] / drivers / spi / kirkwood_spi.c
index db4bb0ae6151b23c95b56a43a7d0b1c7c6e74067..942a208c2cfcb6349a6af684945ce04dbace310f 100644 (file)
@@ -5,23 +5,7 @@
  *
  * Derived from drivers/spi/mpc8xxx_spi.c
  *
- * 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., 51 Franklin Street, Fifth Floor, Boston,
- * MA 02110-1301 USA
+ * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #include <common.h>
@@ -41,23 +25,24 @@ struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
 {
        struct spi_slave *slave;
        u32 data;
-       u32 kwspi_mpp_config[] = { 0, 0 };
+       static const u32 kwspi_mpp_config[2][2] = {
+               { MPP0_SPI_SCn, 0 }, /* if cs == 0 */
+               { MPP7_SPI_SCn, 0 } /* if cs != 0 */
+       };
 
        if (!spi_cs_is_valid(bus, cs))
                return NULL;
 
-       slave = malloc(sizeof(struct spi_slave));
+       slave = spi_alloc_slave_base(bus, cs);
        if (!slave)
                return NULL;
 
-       slave->bus = bus;
-       slave->cs = cs;
-
        writel(~KWSPI_CSN_ACT | KWSPI_SMEMRDY, &spireg->ctrl);
 
        /* calculate spi clock prescaller using max_hz */
-       data = ((CONFIG_SYS_TCLK / 2) / max_hz) & KWSPI_CLKPRESCL_MASK;
-       data |= 0x10;
+       data = ((CONFIG_SYS_TCLK / 2) / max_hz) + 0x10;
+       data = data < KWSPI_CLKPRESCL_MIN ? KWSPI_CLKPRESCL_MIN : data;
+       data = data > KWSPI_CLKPRESCL_MASK ? KWSPI_CLKPRESCL_MASK : data;
 
        /* program spi clock prescaller using max_hz */
        writel(KWSPI_ADRLEN_3BYTE | data, &spireg->cfg);
@@ -67,12 +52,7 @@ struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
        writel(KWSPI_IRQMASK, &spireg->irq_mask);
 
        /* program mpp registers to select  SPI_CSn */
-       if (cs) {
-               kwspi_mpp_config[0] = MPP7_SPI_SCn;
-       } else {
-               kwspi_mpp_config[0] = MPP0_SPI_SCn;
-       }
-       kirkwood_mpp_conf(kwspi_mpp_config, cs_spi_mpp_back);
+       kirkwood_mpp_conf(kwspi_mpp_config[cs ? 1 : 0], cs_spi_mpp_back);
 
        return slave;
 }
@@ -87,6 +67,11 @@ void spi_free_slave(struct spi_slave *slave)
 u32 spi_mpp_backup[4];
 #endif
 
+__attribute__((weak)) int board_spi_claim_bus(struct spi_slave *slave)
+{
+       return 0;
+}
+
 int spi_claim_bus(struct spi_slave *slave)
 {
 #if defined(CONFIG_SYS_KW_SPI_MPP)
@@ -118,7 +103,11 @@ int spi_claim_bus(struct spi_slave *slave)
 
 #endif
 
-       return 0;
+       return board_spi_claim_bus(slave);
+}
+
+__attribute__((weak)) void board_spi_release_bus(struct spi_slave *slave)
+{
 }
 
 void spi_release_bus(struct spi_slave *slave)
@@ -126,6 +115,8 @@ void spi_release_bus(struct spi_slave *slave)
 #if defined(CONFIG_SYS_KW_SPI_MPP)
        kirkwood_mpp_conf(spi_mpp_backup, NULL);
 #endif
+
+       board_spi_release_bus(slave);
 }
 
 #ifndef CONFIG_SPI_CS_IS_VALID