From fa1423e707b30b3eb5251e976560ed5decfd2e3c Mon Sep 17 00:00:00 2001 From: Thomas Chou Date: Fri, 24 Dec 2010 15:16:07 +0800 Subject: spi: add spi_set_speed func This func helps mmc_spi driver set correct speed for mmc/sd, as mmc card needs 400KHz clock for spi mode initialization. Signed-off-by: Thomas Chou Signed-off-by: Mike Frysinger --- include/spi.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/include/spi.h b/include/spi.h index 320e50e..7887d0f 100644 --- a/include/spi.h +++ b/include/spi.h @@ -176,6 +176,14 @@ void spi_cs_activate(struct spi_slave *slave); void spi_cs_deactivate(struct spi_slave *slave); /*----------------------------------------------------------------------- + * Set transfer speed. + * This sets a new speed to be applied for next spi_xfer(). + * slave: The SPI slave + * hz: The transfer speed + */ +void spi_set_speed(struct spi_slave *slave, uint hz); + +/*----------------------------------------------------------------------- * Write 8 bits, then read 8 bits. * slave: The SPI slave we're communicating with * byte: Byte to be written -- cgit v1.1 From 6c3eb43a0f60ee50ca86db3544988ff09163ddbd Mon Sep 17 00:00:00 2001 From: Thomas Chou Date: Fri, 24 Dec 2010 15:16:08 +0800 Subject: bfin_spi: add spi_set_speed The new speed will be applied by spi_claim_bus. Signed-off-by: Thomas Chou Signed-off-by: Mike Frysinger --- drivers/spi/bfin_spi.c | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/drivers/spi/bfin_spi.c b/drivers/spi/bfin_spi.c index e8a4de5..213e792 100644 --- a/drivers/spi/bfin_spi.c +++ b/drivers/spi/bfin_spi.c @@ -138,13 +138,29 @@ static const unsigned short cs_pins[][7] = { #endif }; +void spi_set_speed(struct spi_slave *slave, uint hz) +{ + struct bfin_spi_slave *bss = to_bfin_spi_slave(slave); + ulong sclk; + u32 baud; + + sclk = get_sclk(); + baud = sclk / (2 * hz); + /* baud should be rounded up */ + if (sclk % (2 * hz)) + baud += 1; + if (baud < 2) + baud = 2; + else if (baud > (u16)-1) + baud = -1; + bss->baud = baud; +} + struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs, unsigned int max_hz, unsigned int mode) { struct bfin_spi_slave *bss; - ulong sclk; u32 mmr_base; - u32 baud; if (!spi_cs_is_valid(bus, cs)) return NULL; @@ -166,16 +182,6 @@ struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs, default: return NULL; } - sclk = get_sclk(); - baud = sclk / (2 * max_hz); - /* baud should be rounded up */ - if (sclk % (2 * max_hz)) - baud += 1; - if (baud < 2) - baud = 2; - else if (baud > (u16)-1) - baud = -1; - bss = malloc(sizeof(*bss)); if (!bss) return NULL; @@ -187,8 +193,8 @@ struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs, if (mode & SPI_CPHA) bss->ctl |= CPHA; if (mode & SPI_CPOL) bss->ctl |= CPOL; if (mode & SPI_LSB_FIRST) bss->ctl |= LSBF; - bss->baud = baud; bss->flg = mode & SPI_CS_HIGH ? 1 : 0; + spi_set_speed(&bss->slave, max_hz); debug("%s: bus:%i cs:%i mmr:%x ctl:%x baud:%i flg:%i\n", __func__, bus, cs, mmr_base, bss->ctl, baud, bss->flg); -- cgit v1.1