2008-04-15 12:14:25 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008, Guennadi Liakhovetski <lg@denx.de>
|
|
|
|
*
|
2013-07-08 07:37:19 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
2008-04-15 12:14:25 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
SPI API improvements
This patch gets rid of the spi_chipsel table and adds a handful of new
functions that makes the SPI layer cleaner and more flexible.
Instead of the spi_chipsel table, each board that wants to use SPI
gets to implement three hooks:
* spi_cs_activate(): Activates the chipselect for a given slave
* spi_cs_deactivate(): Deactivates the chipselect for a given slave
* spi_cs_is_valid(): Determines if the given bus/chipselect
combination can be activated.
Not all drivers may need those extra functions however. If that's the
case, the board code may just leave them out (assuming they know what
the driver needs) or rely on the linker to strip them out (assuming
--gc-sections is being used.)
To set up communication parameters for a given slave, the driver needs
to call spi_setup_slave(). This returns a pointer to an opaque
spi_slave struct which must be passed as a parameter to subsequent SPI
calls. This struct can be freed by calling spi_free_slave(), but most
driver probably don't want to do this.
Before starting one or more SPI transfers, the driver must call
spi_claim_bus() to gain exclusive access to the SPI bus and initialize
the hardware. When all transfers are done, the driver must call
spi_release_bus() to make the bus available to others, and possibly
shut down the SPI controller hardware.
spi_xfer() behaves mostly the same as before, but it now takes a
spi_slave parameter instead of a spi_chipsel function pointer. It also
got a new parameter, flags, which is used to specify chip select
behaviour. This may be extended with other flags in the future.
This patch has been build-tested on all powerpc and arm boards
involved. I have not tested NIOS since I don't have a toolchain for it
installed, so I expect some breakage there even though I've tried
fixing up everything I could find by visual inspection.
I have run-time tested this on AVR32 ATNGW100 using the atmel_spi and
DataFlash drivers posted as a follow-up. I'd like some help testing
other boards that use the existing SPI API.
But most of all, I'd like some comments on the new API. Is this stuff
usable for everyone? If not, why?
Changed in v4:
- Build fixes for various boards, drivers and commands
- Provide common struct spi_slave definition that can be extended by
drivers
- Pass a struct spi_slave * to spi_cs_activate and spi_cs_deactivate
- Make default bus and mode build-time configurable
- Override default SPI bus ID and mode on mx32ads and imx31_litekit.
Changed in v3:
- Add opaque struct spi_slave for controller-specific data associated
with a slave.
- Add spi_claim_bus() and spi_release_bus()
- Add spi_free_slave()
- spi_setup() is now called spi_setup_slave() and returns a
struct spi_slave
- soft_spi now supports four SPI modes (CPOL|CPHA)
- Add bus parameter to spi_setup_slave()
- Convert the new i.MX32 SPI driver
- Convert the new MC13783 RTC driver
Changed in v2:
- Convert the mpc8xxx_spi driver and the mpc8349emds board to the
new API.
Signed-off-by: Haavard Skinnemoen <hskinnemoen@atmel.com>
Tested-by: Guennadi Liakhovetski <lg@denx.de>
2008-05-16 09:10:31 +00:00
|
|
|
#include <malloc.h>
|
2008-04-15 12:14:25 +00:00
|
|
|
#include <spi.h>
|
2009-02-13 08:26:40 +00:00
|
|
|
#include <asm/errno.h>
|
2008-04-15 12:14:25 +00:00
|
|
|
#include <asm/io.h>
|
2011-08-21 08:45:44 +00:00
|
|
|
#include <asm/gpio.h>
|
2011-03-14 14:43:56 +00:00
|
|
|
#include <asm/arch/imx-regs.h>
|
|
|
|
#include <asm/arch/clock.h>
|
2008-04-15 12:14:25 +00:00
|
|
|
|
|
|
|
#ifdef CONFIG_MX27
|
|
|
|
/* i.MX27 has a completely wrong register layout and register definitions in the
|
|
|
|
* datasheet, the correct one is in the Freescale's Linux driver */
|
|
|
|
|
2011-06-15 01:45:45 +00:00
|
|
|
#error "i.MX27 CSPI not supported due to drastic differences in register definitions" \
|
2008-04-15 12:14:25 +00:00
|
|
|
"See linux mxc_spi driver from Freescale for details."
|
2012-01-31 07:52:03 +00:00
|
|
|
#endif
|
2011-01-19 22:46:30 +00:00
|
|
|
|
|
|
|
static unsigned long spi_bases[] = {
|
2012-01-31 07:52:03 +00:00
|
|
|
MXC_SPI_BASE_ADDRESSES
|
2011-01-19 22:46:30 +00:00
|
|
|
};
|
|
|
|
|
spi: mxc: fix sf probe when using mxc_spi
MXC SPI driver has a feature whereas a GPIO line can be used to force CS high
across multiple transactions. This is set up by embedding the GPIO information
in the CS value:
cs = (cs | gpio << 8)
This merge of cs and gpio data into one value breaks the sf probe command:
if the use of gpio is required, invoking "sf probe <cs>" will not work, because
the CS argument doesn't have the GPIO information in it. Instead, the user must
use "sf probe <cs | gpio << 8>". For example, if bank 2 gpio 30 is used to force
cs high on cs 0, bus 0, then instead of typing "sf probe 0" the user now must
type "sf probe 15872".
This is inconsistent with the description of the sf probe command, and forces
the user to be aware of implementaiton details.
Fix this by introducing a new board function: board_spi_cs_gpio(), which will
accept a naked CS value, and provide the driver with the relevant GPIO, if one
is necessary.
Cc: Eric Nelson <eric.nelson@boundarydevices.com>
Cc: Eric Benard <eric@eukrea.com>
Cc: Fabio Estevam <fabio.estevam@freescale.com>
Cc: Tim Harvey <tharvey@gateworks.com>
Cc: Stefano Babic <sbabic@denx.de>
Cc: Tom Rini <trini@ti.com>
Cc: Marek Vasut <marex@denx.de>
Reviewed-by: Marek Vasut <marex@denx.de>
Signed-off-by: Nikita Kiryanov <nikita@compulab.co.il>
Reviewed-by: Jagannadha Sutradharudu Teki <jaganna@xilinx.com>
2014-08-20 12:08:50 +00:00
|
|
|
__weak int board_spi_cs_gpio(unsigned bus, unsigned cs)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-07-06 15:05:06 +00:00
|
|
|
#define OUT MXC_GPIO_DIRECTION_OUT
|
|
|
|
|
2011-01-19 22:46:33 +00:00
|
|
|
#define reg_read readl
|
|
|
|
#define reg_write(a, v) writel(v, a)
|
|
|
|
|
2014-07-14 08:22:11 +00:00
|
|
|
#if !defined(CONFIG_SYS_SPI_MXC_WAIT)
|
|
|
|
#define CONFIG_SYS_SPI_MXC_WAIT (CONFIG_SYS_HZ/100) /* 10 ms */
|
|
|
|
#endif
|
|
|
|
|
SPI API improvements
This patch gets rid of the spi_chipsel table and adds a handful of new
functions that makes the SPI layer cleaner and more flexible.
Instead of the spi_chipsel table, each board that wants to use SPI
gets to implement three hooks:
* spi_cs_activate(): Activates the chipselect for a given slave
* spi_cs_deactivate(): Deactivates the chipselect for a given slave
* spi_cs_is_valid(): Determines if the given bus/chipselect
combination can be activated.
Not all drivers may need those extra functions however. If that's the
case, the board code may just leave them out (assuming they know what
the driver needs) or rely on the linker to strip them out (assuming
--gc-sections is being used.)
To set up communication parameters for a given slave, the driver needs
to call spi_setup_slave(). This returns a pointer to an opaque
spi_slave struct which must be passed as a parameter to subsequent SPI
calls. This struct can be freed by calling spi_free_slave(), but most
driver probably don't want to do this.
Before starting one or more SPI transfers, the driver must call
spi_claim_bus() to gain exclusive access to the SPI bus and initialize
the hardware. When all transfers are done, the driver must call
spi_release_bus() to make the bus available to others, and possibly
shut down the SPI controller hardware.
spi_xfer() behaves mostly the same as before, but it now takes a
spi_slave parameter instead of a spi_chipsel function pointer. It also
got a new parameter, flags, which is used to specify chip select
behaviour. This may be extended with other flags in the future.
This patch has been build-tested on all powerpc and arm boards
involved. I have not tested NIOS since I don't have a toolchain for it
installed, so I expect some breakage there even though I've tried
fixing up everything I could find by visual inspection.
I have run-time tested this on AVR32 ATNGW100 using the atmel_spi and
DataFlash drivers posted as a follow-up. I'd like some help testing
other boards that use the existing SPI API.
But most of all, I'd like some comments on the new API. Is this stuff
usable for everyone? If not, why?
Changed in v4:
- Build fixes for various boards, drivers and commands
- Provide common struct spi_slave definition that can be extended by
drivers
- Pass a struct spi_slave * to spi_cs_activate and spi_cs_deactivate
- Make default bus and mode build-time configurable
- Override default SPI bus ID and mode on mx32ads and imx31_litekit.
Changed in v3:
- Add opaque struct spi_slave for controller-specific data associated
with a slave.
- Add spi_claim_bus() and spi_release_bus()
- Add spi_free_slave()
- spi_setup() is now called spi_setup_slave() and returns a
struct spi_slave
- soft_spi now supports four SPI modes (CPOL|CPHA)
- Add bus parameter to spi_setup_slave()
- Convert the new i.MX32 SPI driver
- Convert the new MC13783 RTC driver
Changed in v2:
- Convert the mpc8xxx_spi driver and the mpc8349emds board to the
new API.
Signed-off-by: Haavard Skinnemoen <hskinnemoen@atmel.com>
Tested-by: Guennadi Liakhovetski <lg@denx.de>
2008-05-16 09:10:31 +00:00
|
|
|
struct mxc_spi_slave {
|
|
|
|
struct spi_slave slave;
|
|
|
|
unsigned long base;
|
|
|
|
u32 ctrl_reg;
|
2012-01-31 07:52:03 +00:00
|
|
|
#if defined(MXC_ECSPI)
|
2010-04-04 20:43:38 +00:00
|
|
|
u32 cfg_reg;
|
|
|
|
#endif
|
2009-02-13 08:26:40 +00:00
|
|
|
int gpio;
|
2010-07-06 15:05:06 +00:00
|
|
|
int ss_pol;
|
2008-04-15 12:14:25 +00:00
|
|
|
};
|
SPI API improvements
This patch gets rid of the spi_chipsel table and adds a handful of new
functions that makes the SPI layer cleaner and more flexible.
Instead of the spi_chipsel table, each board that wants to use SPI
gets to implement three hooks:
* spi_cs_activate(): Activates the chipselect for a given slave
* spi_cs_deactivate(): Deactivates the chipselect for a given slave
* spi_cs_is_valid(): Determines if the given bus/chipselect
combination can be activated.
Not all drivers may need those extra functions however. If that's the
case, the board code may just leave them out (assuming they know what
the driver needs) or rely on the linker to strip them out (assuming
--gc-sections is being used.)
To set up communication parameters for a given slave, the driver needs
to call spi_setup_slave(). This returns a pointer to an opaque
spi_slave struct which must be passed as a parameter to subsequent SPI
calls. This struct can be freed by calling spi_free_slave(), but most
driver probably don't want to do this.
Before starting one or more SPI transfers, the driver must call
spi_claim_bus() to gain exclusive access to the SPI bus and initialize
the hardware. When all transfers are done, the driver must call
spi_release_bus() to make the bus available to others, and possibly
shut down the SPI controller hardware.
spi_xfer() behaves mostly the same as before, but it now takes a
spi_slave parameter instead of a spi_chipsel function pointer. It also
got a new parameter, flags, which is used to specify chip select
behaviour. This may be extended with other flags in the future.
This patch has been build-tested on all powerpc and arm boards
involved. I have not tested NIOS since I don't have a toolchain for it
installed, so I expect some breakage there even though I've tried
fixing up everything I could find by visual inspection.
I have run-time tested this on AVR32 ATNGW100 using the atmel_spi and
DataFlash drivers posted as a follow-up. I'd like some help testing
other boards that use the existing SPI API.
But most of all, I'd like some comments on the new API. Is this stuff
usable for everyone? If not, why?
Changed in v4:
- Build fixes for various boards, drivers and commands
- Provide common struct spi_slave definition that can be extended by
drivers
- Pass a struct spi_slave * to spi_cs_activate and spi_cs_deactivate
- Make default bus and mode build-time configurable
- Override default SPI bus ID and mode on mx32ads and imx31_litekit.
Changed in v3:
- Add opaque struct spi_slave for controller-specific data associated
with a slave.
- Add spi_claim_bus() and spi_release_bus()
- Add spi_free_slave()
- spi_setup() is now called spi_setup_slave() and returns a
struct spi_slave
- soft_spi now supports four SPI modes (CPOL|CPHA)
- Add bus parameter to spi_setup_slave()
- Convert the new i.MX32 SPI driver
- Convert the new MC13783 RTC driver
Changed in v2:
- Convert the mpc8xxx_spi driver and the mpc8349emds board to the
new API.
Signed-off-by: Haavard Skinnemoen <hskinnemoen@atmel.com>
Tested-by: Guennadi Liakhovetski <lg@denx.de>
2008-05-16 09:10:31 +00:00
|
|
|
|
|
|
|
static inline struct mxc_spi_slave *to_mxc_spi_slave(struct spi_slave *slave)
|
|
|
|
{
|
|
|
|
return container_of(slave, struct mxc_spi_slave, slave);
|
|
|
|
}
|
2008-04-15 12:14:25 +00:00
|
|
|
|
2010-04-04 20:43:38 +00:00
|
|
|
void spi_cs_activate(struct spi_slave *slave)
|
|
|
|
{
|
|
|
|
struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
|
|
|
|
if (mxcs->gpio > 0)
|
2011-08-21 08:45:44 +00:00
|
|
|
gpio_set_value(mxcs->gpio, mxcs->ss_pol);
|
2010-04-04 20:43:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void spi_cs_deactivate(struct spi_slave *slave)
|
|
|
|
{
|
|
|
|
struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
|
|
|
|
if (mxcs->gpio > 0)
|
2011-08-21 08:45:44 +00:00
|
|
|
gpio_set_value(mxcs->gpio,
|
2010-07-06 15:05:06 +00:00
|
|
|
!(mxcs->ss_pol));
|
2010-04-04 20:43:38 +00:00
|
|
|
}
|
|
|
|
|
2011-01-19 22:46:32 +00:00
|
|
|
u32 get_cspi_div(u32 div)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 8; i++) {
|
|
|
|
if (div <= (4 << i))
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2012-01-31 07:52:03 +00:00
|
|
|
#ifdef MXC_CSPI
|
2011-01-19 22:46:30 +00:00
|
|
|
static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs,
|
|
|
|
unsigned int max_hz, unsigned int mode)
|
|
|
|
{
|
|
|
|
unsigned int ctrl_reg;
|
2011-01-19 22:46:32 +00:00
|
|
|
u32 clk_src;
|
|
|
|
u32 div;
|
|
|
|
|
|
|
|
clk_src = mxc_get_clock(MXC_CSPI_CLK);
|
|
|
|
|
2012-08-10 08:51:50 +00:00
|
|
|
div = DIV_ROUND_UP(clk_src, max_hz);
|
2011-01-19 22:46:32 +00:00
|
|
|
div = get_cspi_div(div);
|
|
|
|
|
|
|
|
debug("clk %d Hz, div %d, real clk %d Hz\n",
|
|
|
|
max_hz, div, clk_src / (4 << div));
|
2011-01-19 22:46:30 +00:00
|
|
|
|
|
|
|
ctrl_reg = MXC_CSPICTRL_CHIPSELECT(cs) |
|
|
|
|
MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS) |
|
2011-01-19 22:46:32 +00:00
|
|
|
MXC_CSPICTRL_DATARATE(div) |
|
2011-01-19 22:46:30 +00:00
|
|
|
MXC_CSPICTRL_EN |
|
|
|
|
#ifdef CONFIG_MX35
|
|
|
|
MXC_CSPICTRL_SSCTL |
|
|
|
|
#endif
|
|
|
|
MXC_CSPICTRL_MODE;
|
|
|
|
|
|
|
|
if (mode & SPI_CPHA)
|
|
|
|
ctrl_reg |= MXC_CSPICTRL_PHA;
|
|
|
|
if (mode & SPI_CPOL)
|
|
|
|
ctrl_reg |= MXC_CSPICTRL_POL;
|
|
|
|
if (mode & SPI_CS_HIGH)
|
|
|
|
ctrl_reg |= MXC_CSPICTRL_SSPOL;
|
|
|
|
mxcs->ctrl_reg = ctrl_reg;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-01-31 07:52:03 +00:00
|
|
|
#ifdef MXC_ECSPI
|
2011-01-19 22:46:30 +00:00
|
|
|
static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs,
|
2010-04-04 20:43:38 +00:00
|
|
|
unsigned int max_hz, unsigned int mode)
|
|
|
|
{
|
|
|
|
u32 clk_src = mxc_get_clock(MXC_CSPI_CLK);
|
2013-05-11 05:25:54 +00:00
|
|
|
s32 reg_ctrl, reg_config;
|
2014-02-17 16:33:17 +00:00
|
|
|
u32 ss_pol = 0, sclkpol = 0, sclkpha = 0, sclkctl = 0;
|
|
|
|
u32 pre_div = 0, post_div = 0;
|
2011-01-19 22:46:33 +00:00
|
|
|
struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
|
2010-04-04 20:43:38 +00:00
|
|
|
|
|
|
|
if (max_hz == 0) {
|
|
|
|
printf("Error: desired clock is 0\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-04-09 13:06:25 +00:00
|
|
|
/*
|
|
|
|
* Reset SPI and set all CSs to master mode, if toggling
|
|
|
|
* between slave and master mode we might see a glitch
|
|
|
|
* on the clock line
|
|
|
|
*/
|
|
|
|
reg_ctrl = MXC_CSPICTRL_MODE_MASK;
|
|
|
|
reg_write(®s->ctrl, reg_ctrl);
|
|
|
|
reg_ctrl |= MXC_CSPICTRL_EN;
|
|
|
|
reg_write(®s->ctrl, reg_ctrl);
|
2010-04-04 20:43:38 +00:00
|
|
|
|
|
|
|
if (clk_src > max_hz) {
|
2013-05-11 05:25:54 +00:00
|
|
|
pre_div = (clk_src - 1) / max_hz;
|
|
|
|
/* fls(1) = 1, fls(0x80000000) = 32, fls(16) = 5 */
|
|
|
|
post_div = fls(pre_div);
|
|
|
|
if (post_div > 4) {
|
|
|
|
post_div -= 4;
|
|
|
|
if (post_div >= 16) {
|
2010-04-04 20:43:38 +00:00
|
|
|
printf("Error: no divider for the freq: %d\n",
|
|
|
|
max_hz);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-05-11 05:25:54 +00:00
|
|
|
pre_div >>= post_div;
|
|
|
|
} else {
|
|
|
|
post_div = 0;
|
2010-04-04 20:43:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
debug("pre_div = %d, post_div=%d\n", pre_div, post_div);
|
|
|
|
reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_SELCHAN(3)) |
|
|
|
|
MXC_CSPICTRL_SELCHAN(cs);
|
|
|
|
reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_PREDIV(0x0F)) |
|
|
|
|
MXC_CSPICTRL_PREDIV(pre_div);
|
|
|
|
reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_POSTDIV(0x0F)) |
|
|
|
|
MXC_CSPICTRL_POSTDIV(post_div);
|
|
|
|
|
|
|
|
/* We need to disable SPI before changing registers */
|
|
|
|
reg_ctrl &= ~MXC_CSPICTRL_EN;
|
|
|
|
|
|
|
|
if (mode & SPI_CS_HIGH)
|
|
|
|
ss_pol = 1;
|
|
|
|
|
2014-02-17 16:33:17 +00:00
|
|
|
if (mode & SPI_CPOL) {
|
2010-04-04 20:43:38 +00:00
|
|
|
sclkpol = 1;
|
2014-02-17 16:33:17 +00:00
|
|
|
sclkctl = 1;
|
|
|
|
}
|
2010-04-04 20:43:38 +00:00
|
|
|
|
|
|
|
if (mode & SPI_CPHA)
|
|
|
|
sclkpha = 1;
|
|
|
|
|
2011-01-19 22:46:33 +00:00
|
|
|
reg_config = reg_read(®s->cfg);
|
2010-04-04 20:43:38 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Configuration register setup
|
2011-01-19 22:46:30 +00:00
|
|
|
* The MX51 supports different setup for each SS
|
2010-04-04 20:43:38 +00:00
|
|
|
*/
|
|
|
|
reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_SSPOL))) |
|
|
|
|
(ss_pol << (cs + MXC_CSPICON_SSPOL));
|
|
|
|
reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_POL))) |
|
|
|
|
(sclkpol << (cs + MXC_CSPICON_POL));
|
2014-02-17 16:33:17 +00:00
|
|
|
reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_CTL))) |
|
|
|
|
(sclkctl << (cs + MXC_CSPICON_CTL));
|
2010-04-04 20:43:38 +00:00
|
|
|
reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_PHA))) |
|
|
|
|
(sclkpha << (cs + MXC_CSPICON_PHA));
|
|
|
|
|
|
|
|
debug("reg_ctrl = 0x%x\n", reg_ctrl);
|
2011-01-19 22:46:33 +00:00
|
|
|
reg_write(®s->ctrl, reg_ctrl);
|
2010-04-04 20:43:38 +00:00
|
|
|
debug("reg_config = 0x%x\n", reg_config);
|
2011-01-19 22:46:33 +00:00
|
|
|
reg_write(®s->cfg, reg_config);
|
2010-04-04 20:43:38 +00:00
|
|
|
|
|
|
|
/* save config register and control register */
|
|
|
|
mxcs->ctrl_reg = reg_ctrl;
|
|
|
|
mxcs->cfg_reg = reg_config;
|
|
|
|
|
|
|
|
/* clear interrupt reg */
|
2011-01-19 22:46:33 +00:00
|
|
|
reg_write(®s->intr, 0);
|
|
|
|
reg_write(®s->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
|
2010-04-04 20:43:38 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-08-20 10:05:03 +00:00
|
|
|
int spi_xchg_single(struct spi_slave *slave, unsigned int bitlen,
|
|
|
|
const u8 *dout, u8 *din, unsigned long flags)
|
2008-04-15 12:14:25 +00:00
|
|
|
{
|
SPI API improvements
This patch gets rid of the spi_chipsel table and adds a handful of new
functions that makes the SPI layer cleaner and more flexible.
Instead of the spi_chipsel table, each board that wants to use SPI
gets to implement three hooks:
* spi_cs_activate(): Activates the chipselect for a given slave
* spi_cs_deactivate(): Deactivates the chipselect for a given slave
* spi_cs_is_valid(): Determines if the given bus/chipselect
combination can be activated.
Not all drivers may need those extra functions however. If that's the
case, the board code may just leave them out (assuming they know what
the driver needs) or rely on the linker to strip them out (assuming
--gc-sections is being used.)
To set up communication parameters for a given slave, the driver needs
to call spi_setup_slave(). This returns a pointer to an opaque
spi_slave struct which must be passed as a parameter to subsequent SPI
calls. This struct can be freed by calling spi_free_slave(), but most
driver probably don't want to do this.
Before starting one or more SPI transfers, the driver must call
spi_claim_bus() to gain exclusive access to the SPI bus and initialize
the hardware. When all transfers are done, the driver must call
spi_release_bus() to make the bus available to others, and possibly
shut down the SPI controller hardware.
spi_xfer() behaves mostly the same as before, but it now takes a
spi_slave parameter instead of a spi_chipsel function pointer. It also
got a new parameter, flags, which is used to specify chip select
behaviour. This may be extended with other flags in the future.
This patch has been build-tested on all powerpc and arm boards
involved. I have not tested NIOS since I don't have a toolchain for it
installed, so I expect some breakage there even though I've tried
fixing up everything I could find by visual inspection.
I have run-time tested this on AVR32 ATNGW100 using the atmel_spi and
DataFlash drivers posted as a follow-up. I'd like some help testing
other boards that use the existing SPI API.
But most of all, I'd like some comments on the new API. Is this stuff
usable for everyone? If not, why?
Changed in v4:
- Build fixes for various boards, drivers and commands
- Provide common struct spi_slave definition that can be extended by
drivers
- Pass a struct spi_slave * to spi_cs_activate and spi_cs_deactivate
- Make default bus and mode build-time configurable
- Override default SPI bus ID and mode on mx32ads and imx31_litekit.
Changed in v3:
- Add opaque struct spi_slave for controller-specific data associated
with a slave.
- Add spi_claim_bus() and spi_release_bus()
- Add spi_free_slave()
- spi_setup() is now called spi_setup_slave() and returns a
struct spi_slave
- soft_spi now supports four SPI modes (CPOL|CPHA)
- Add bus parameter to spi_setup_slave()
- Convert the new i.MX32 SPI driver
- Convert the new MC13783 RTC driver
Changed in v2:
- Convert the mpc8xxx_spi driver and the mpc8349emds board to the
new API.
Signed-off-by: Haavard Skinnemoen <hskinnemoen@atmel.com>
Tested-by: Guennadi Liakhovetski <lg@denx.de>
2008-05-16 09:10:31 +00:00
|
|
|
struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
|
2013-06-14 13:13:32 +00:00
|
|
|
int nbytes = DIV_ROUND_UP(bitlen, 8);
|
2010-08-20 10:05:03 +00:00
|
|
|
u32 data, cnt, i;
|
2011-01-19 22:46:33 +00:00
|
|
|
struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
|
2014-07-14 08:22:11 +00:00
|
|
|
u32 ts;
|
|
|
|
int status;
|
2008-04-15 12:14:25 +00:00
|
|
|
|
2010-08-20 10:05:03 +00:00
|
|
|
debug("%s: bitlen %d dout 0x%x din 0x%x\n",
|
|
|
|
__func__, bitlen, (u32)dout, (u32)din);
|
2010-04-04 20:43:38 +00:00
|
|
|
|
|
|
|
mxcs->ctrl_reg = (mxcs->ctrl_reg &
|
|
|
|
~MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS)) |
|
2009-02-06 23:09:12 +00:00
|
|
|
MXC_CSPICTRL_BITCOUNT(bitlen - 1);
|
2008-04-15 12:14:25 +00:00
|
|
|
|
2011-01-19 22:46:33 +00:00
|
|
|
reg_write(®s->ctrl, mxcs->ctrl_reg | MXC_CSPICTRL_EN);
|
2012-01-31 07:52:03 +00:00
|
|
|
#ifdef MXC_ECSPI
|
2011-01-19 22:46:33 +00:00
|
|
|
reg_write(®s->cfg, mxcs->cfg_reg);
|
2010-04-04 20:43:38 +00:00
|
|
|
#endif
|
2008-04-15 12:14:25 +00:00
|
|
|
|
2010-04-04 20:43:38 +00:00
|
|
|
/* Clear interrupt register */
|
2011-01-19 22:46:33 +00:00
|
|
|
reg_write(®s->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
|
2009-02-13 08:26:40 +00:00
|
|
|
|
2010-08-20 10:05:03 +00:00
|
|
|
/*
|
|
|
|
* The SPI controller works only with words,
|
|
|
|
* check if less than a word is sent.
|
|
|
|
* Access to the FIFO is only 32 bit
|
|
|
|
*/
|
|
|
|
if (bitlen % 32) {
|
|
|
|
data = 0;
|
|
|
|
cnt = (bitlen % 32) / 8;
|
|
|
|
if (dout) {
|
|
|
|
for (i = 0; i < cnt; i++) {
|
|
|
|
data = (data << 8) | (*dout++ & 0xFF);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debug("Sending SPI 0x%x\n", data);
|
|
|
|
|
2011-01-19 22:46:33 +00:00
|
|
|
reg_write(®s->txdata, data);
|
2010-08-20 10:05:03 +00:00
|
|
|
nbytes -= cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
data = 0;
|
|
|
|
|
|
|
|
while (nbytes > 0) {
|
|
|
|
data = 0;
|
|
|
|
if (dout) {
|
|
|
|
/* Buffer is not 32-bit aligned */
|
|
|
|
if ((unsigned long)dout & 0x03) {
|
|
|
|
data = 0;
|
2011-01-20 07:53:06 +00:00
|
|
|
for (i = 0; i < 4; i++)
|
2010-08-20 10:05:03 +00:00
|
|
|
data = (data << 8) | (*dout++ & 0xFF);
|
|
|
|
} else {
|
|
|
|
data = *(u32 *)dout;
|
|
|
|
data = cpu_to_be32(data);
|
2013-10-15 18:35:09 +00:00
|
|
|
dout += 4;
|
2010-08-20 10:05:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
debug("Sending SPI 0x%x\n", data);
|
2011-01-19 22:46:33 +00:00
|
|
|
reg_write(®s->txdata, data);
|
2010-08-20 10:05:03 +00:00
|
|
|
nbytes -= 4;
|
|
|
|
}
|
2008-04-15 12:14:25 +00:00
|
|
|
|
2010-04-04 20:43:38 +00:00
|
|
|
/* FIFO is written, now starts the transfer setting the XCH bit */
|
2011-01-19 22:46:33 +00:00
|
|
|
reg_write(®s->ctrl, mxcs->ctrl_reg |
|
2010-04-04 20:43:38 +00:00
|
|
|
MXC_CSPICTRL_EN | MXC_CSPICTRL_XCH);
|
2008-04-15 12:14:25 +00:00
|
|
|
|
2014-07-14 08:22:11 +00:00
|
|
|
ts = get_timer(0);
|
|
|
|
status = reg_read(®s->stat);
|
2010-04-04 20:43:38 +00:00
|
|
|
/* Wait until the TC (Transfer completed) bit is set */
|
2014-07-14 08:22:11 +00:00
|
|
|
while ((status & MXC_CSPICTRL_TC) == 0) {
|
|
|
|
if (get_timer(ts) > CONFIG_SYS_SPI_MXC_WAIT) {
|
|
|
|
printf("spi_xchg_single: Timeout!\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
status = reg_read(®s->stat);
|
|
|
|
}
|
2008-04-15 12:14:25 +00:00
|
|
|
|
2010-04-04 20:43:38 +00:00
|
|
|
/* Transfer completed, clear any pending request */
|
2011-01-19 22:46:33 +00:00
|
|
|
reg_write(®s->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
|
2010-04-04 20:43:38 +00:00
|
|
|
|
2013-06-14 13:13:32 +00:00
|
|
|
nbytes = DIV_ROUND_UP(bitlen, 8);
|
2010-04-04 20:43:38 +00:00
|
|
|
|
2010-08-20 10:05:03 +00:00
|
|
|
cnt = nbytes % 32;
|
2010-04-04 20:43:38 +00:00
|
|
|
|
2010-08-20 10:05:03 +00:00
|
|
|
if (bitlen % 32) {
|
2011-01-19 22:46:33 +00:00
|
|
|
data = reg_read(®s->rxdata);
|
2010-08-20 10:05:03 +00:00
|
|
|
cnt = (bitlen % 32) / 8;
|
2011-01-20 07:53:06 +00:00
|
|
|
data = cpu_to_be32(data) >> ((sizeof(data) - cnt) * 8);
|
2010-08-20 10:05:03 +00:00
|
|
|
debug("SPI Rx unaligned: 0x%x\n", data);
|
|
|
|
if (din) {
|
2011-01-20 07:53:06 +00:00
|
|
|
memcpy(din, &data, cnt);
|
|
|
|
din += cnt;
|
2010-08-20 10:05:03 +00:00
|
|
|
}
|
|
|
|
nbytes -= cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (nbytes > 0) {
|
|
|
|
u32 tmp;
|
2011-01-19 22:46:33 +00:00
|
|
|
tmp = reg_read(®s->rxdata);
|
2010-08-20 10:05:03 +00:00
|
|
|
data = cpu_to_be32(tmp);
|
|
|
|
debug("SPI Rx: 0x%x 0x%x\n", tmp, data);
|
|
|
|
cnt = min(nbytes, sizeof(data));
|
|
|
|
if (din) {
|
|
|
|
memcpy(din, &data, cnt);
|
|
|
|
din += cnt;
|
|
|
|
}
|
|
|
|
nbytes -= cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2009-02-13 08:26:40 +00:00
|
|
|
|
2008-04-15 12:14:25 +00:00
|
|
|
}
|
|
|
|
|
SPI API improvements
This patch gets rid of the spi_chipsel table and adds a handful of new
functions that makes the SPI layer cleaner and more flexible.
Instead of the spi_chipsel table, each board that wants to use SPI
gets to implement three hooks:
* spi_cs_activate(): Activates the chipselect for a given slave
* spi_cs_deactivate(): Deactivates the chipselect for a given slave
* spi_cs_is_valid(): Determines if the given bus/chipselect
combination can be activated.
Not all drivers may need those extra functions however. If that's the
case, the board code may just leave them out (assuming they know what
the driver needs) or rely on the linker to strip them out (assuming
--gc-sections is being used.)
To set up communication parameters for a given slave, the driver needs
to call spi_setup_slave(). This returns a pointer to an opaque
spi_slave struct which must be passed as a parameter to subsequent SPI
calls. This struct can be freed by calling spi_free_slave(), but most
driver probably don't want to do this.
Before starting one or more SPI transfers, the driver must call
spi_claim_bus() to gain exclusive access to the SPI bus and initialize
the hardware. When all transfers are done, the driver must call
spi_release_bus() to make the bus available to others, and possibly
shut down the SPI controller hardware.
spi_xfer() behaves mostly the same as before, but it now takes a
spi_slave parameter instead of a spi_chipsel function pointer. It also
got a new parameter, flags, which is used to specify chip select
behaviour. This may be extended with other flags in the future.
This patch has been build-tested on all powerpc and arm boards
involved. I have not tested NIOS since I don't have a toolchain for it
installed, so I expect some breakage there even though I've tried
fixing up everything I could find by visual inspection.
I have run-time tested this on AVR32 ATNGW100 using the atmel_spi and
DataFlash drivers posted as a follow-up. I'd like some help testing
other boards that use the existing SPI API.
But most of all, I'd like some comments on the new API. Is this stuff
usable for everyone? If not, why?
Changed in v4:
- Build fixes for various boards, drivers and commands
- Provide common struct spi_slave definition that can be extended by
drivers
- Pass a struct spi_slave * to spi_cs_activate and spi_cs_deactivate
- Make default bus and mode build-time configurable
- Override default SPI bus ID and mode on mx32ads and imx31_litekit.
Changed in v3:
- Add opaque struct spi_slave for controller-specific data associated
with a slave.
- Add spi_claim_bus() and spi_release_bus()
- Add spi_free_slave()
- spi_setup() is now called spi_setup_slave() and returns a
struct spi_slave
- soft_spi now supports four SPI modes (CPOL|CPHA)
- Add bus parameter to spi_setup_slave()
- Convert the new i.MX32 SPI driver
- Convert the new MC13783 RTC driver
Changed in v2:
- Convert the mpc8xxx_spi driver and the mpc8349emds board to the
new API.
Signed-off-by: Haavard Skinnemoen <hskinnemoen@atmel.com>
Tested-by: Guennadi Liakhovetski <lg@denx.de>
2008-05-16 09:10:31 +00:00
|
|
|
int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
|
|
|
|
void *din, unsigned long flags)
|
2008-04-15 12:14:25 +00:00
|
|
|
{
|
2013-06-14 13:13:32 +00:00
|
|
|
int n_bytes = DIV_ROUND_UP(bitlen, 8);
|
2010-08-20 10:05:03 +00:00
|
|
|
int n_bits;
|
|
|
|
int ret;
|
|
|
|
u32 blk_size;
|
|
|
|
u8 *p_outbuf = (u8 *)dout;
|
|
|
|
u8 *p_inbuf = (u8 *)din;
|
2008-04-15 12:14:25 +00:00
|
|
|
|
2010-08-20 10:05:03 +00:00
|
|
|
if (!slave)
|
|
|
|
return -1;
|
2008-04-15 12:14:25 +00:00
|
|
|
|
2010-08-20 10:05:03 +00:00
|
|
|
if (flags & SPI_XFER_BEGIN)
|
|
|
|
spi_cs_activate(slave);
|
|
|
|
|
|
|
|
while (n_bytes > 0) {
|
|
|
|
if (n_bytes < MAX_SPI_BYTES)
|
|
|
|
blk_size = n_bytes;
|
|
|
|
else
|
|
|
|
blk_size = MAX_SPI_BYTES;
|
|
|
|
|
|
|
|
n_bits = blk_size * 8;
|
|
|
|
|
|
|
|
ret = spi_xchg_single(slave, n_bits, p_outbuf, p_inbuf, 0);
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
if (dout)
|
|
|
|
p_outbuf += blk_size;
|
|
|
|
if (din)
|
|
|
|
p_inbuf += blk_size;
|
|
|
|
n_bytes -= blk_size;
|
2010-02-09 21:05:39 +00:00
|
|
|
}
|
|
|
|
|
2010-08-20 10:05:03 +00:00
|
|
|
if (flags & SPI_XFER_END) {
|
|
|
|
spi_cs_deactivate(slave);
|
2009-02-06 23:09:12 +00:00
|
|
|
}
|
2008-04-15 12:14:25 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void spi_init(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
spi: mxc: fix sf probe when using mxc_spi
MXC SPI driver has a feature whereas a GPIO line can be used to force CS high
across multiple transactions. This is set up by embedding the GPIO information
in the CS value:
cs = (cs | gpio << 8)
This merge of cs and gpio data into one value breaks the sf probe command:
if the use of gpio is required, invoking "sf probe <cs>" will not work, because
the CS argument doesn't have the GPIO information in it. Instead, the user must
use "sf probe <cs | gpio << 8>". For example, if bank 2 gpio 30 is used to force
cs high on cs 0, bus 0, then instead of typing "sf probe 0" the user now must
type "sf probe 15872".
This is inconsistent with the description of the sf probe command, and forces
the user to be aware of implementaiton details.
Fix this by introducing a new board function: board_spi_cs_gpio(), which will
accept a naked CS value, and provide the driver with the relevant GPIO, if one
is necessary.
Cc: Eric Nelson <eric.nelson@boundarydevices.com>
Cc: Eric Benard <eric@eukrea.com>
Cc: Fabio Estevam <fabio.estevam@freescale.com>
Cc: Tim Harvey <tharvey@gateworks.com>
Cc: Stefano Babic <sbabic@denx.de>
Cc: Tom Rini <trini@ti.com>
Cc: Marek Vasut <marex@denx.de>
Reviewed-by: Marek Vasut <marex@denx.de>
Signed-off-by: Nikita Kiryanov <nikita@compulab.co.il>
Reviewed-by: Jagannadha Sutradharudu Teki <jaganna@xilinx.com>
2014-08-20 12:08:50 +00:00
|
|
|
/*
|
|
|
|
* Some SPI devices require active chip-select over multiple
|
|
|
|
* transactions, we achieve this using a GPIO. Still, the SPI
|
|
|
|
* controller has to be configured to use one of its own chipselects.
|
|
|
|
* To use this feature you have to implement board_spi_cs_gpio() to assign
|
|
|
|
* a gpio value for each cs (-1 if cs doesn't need to use gpio).
|
|
|
|
* You must use some unused on this SPI controller cs between 0 and 3.
|
|
|
|
*/
|
|
|
|
static int setup_cs_gpio(struct mxc_spi_slave *mxcs,
|
|
|
|
unsigned int bus, unsigned int cs)
|
2009-02-13 08:26:40 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
spi: mxc: fix sf probe when using mxc_spi
MXC SPI driver has a feature whereas a GPIO line can be used to force CS high
across multiple transactions. This is set up by embedding the GPIO information
in the CS value:
cs = (cs | gpio << 8)
This merge of cs and gpio data into one value breaks the sf probe command:
if the use of gpio is required, invoking "sf probe <cs>" will not work, because
the CS argument doesn't have the GPIO information in it. Instead, the user must
use "sf probe <cs | gpio << 8>". For example, if bank 2 gpio 30 is used to force
cs high on cs 0, bus 0, then instead of typing "sf probe 0" the user now must
type "sf probe 15872".
This is inconsistent with the description of the sf probe command, and forces
the user to be aware of implementaiton details.
Fix this by introducing a new board function: board_spi_cs_gpio(), which will
accept a naked CS value, and provide the driver with the relevant GPIO, if one
is necessary.
Cc: Eric Nelson <eric.nelson@boundarydevices.com>
Cc: Eric Benard <eric@eukrea.com>
Cc: Fabio Estevam <fabio.estevam@freescale.com>
Cc: Tim Harvey <tharvey@gateworks.com>
Cc: Stefano Babic <sbabic@denx.de>
Cc: Tom Rini <trini@ti.com>
Cc: Marek Vasut <marex@denx.de>
Reviewed-by: Marek Vasut <marex@denx.de>
Signed-off-by: Nikita Kiryanov <nikita@compulab.co.il>
Reviewed-by: Jagannadha Sutradharudu Teki <jaganna@xilinx.com>
2014-08-20 12:08:50 +00:00
|
|
|
mxcs->gpio = board_spi_cs_gpio(bus, cs);
|
|
|
|
if (mxcs->gpio == -1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ret = gpio_direction_output(mxcs->gpio, !(mxcs->ss_pol));
|
|
|
|
if (ret) {
|
|
|
|
printf("mxc_spi: cannot setup gpio %d\n", mxcs->gpio);
|
|
|
|
return -EINVAL;
|
2009-02-13 08:26:40 +00:00
|
|
|
}
|
|
|
|
|
spi: mxc: fix sf probe when using mxc_spi
MXC SPI driver has a feature whereas a GPIO line can be used to force CS high
across multiple transactions. This is set up by embedding the GPIO information
in the CS value:
cs = (cs | gpio << 8)
This merge of cs and gpio data into one value breaks the sf probe command:
if the use of gpio is required, invoking "sf probe <cs>" will not work, because
the CS argument doesn't have the GPIO information in it. Instead, the user must
use "sf probe <cs | gpio << 8>". For example, if bank 2 gpio 30 is used to force
cs high on cs 0, bus 0, then instead of typing "sf probe 0" the user now must
type "sf probe 15872".
This is inconsistent with the description of the sf probe command, and forces
the user to be aware of implementaiton details.
Fix this by introducing a new board function: board_spi_cs_gpio(), which will
accept a naked CS value, and provide the driver with the relevant GPIO, if one
is necessary.
Cc: Eric Nelson <eric.nelson@boundarydevices.com>
Cc: Eric Benard <eric@eukrea.com>
Cc: Fabio Estevam <fabio.estevam@freescale.com>
Cc: Tim Harvey <tharvey@gateworks.com>
Cc: Stefano Babic <sbabic@denx.de>
Cc: Tom Rini <trini@ti.com>
Cc: Marek Vasut <marex@denx.de>
Reviewed-by: Marek Vasut <marex@denx.de>
Signed-off-by: Nikita Kiryanov <nikita@compulab.co.il>
Reviewed-by: Jagannadha Sutradharudu Teki <jaganna@xilinx.com>
2014-08-20 12:08:50 +00:00
|
|
|
return 0;
|
2009-02-13 08:26:40 +00:00
|
|
|
}
|
|
|
|
|
SPI API improvements
This patch gets rid of the spi_chipsel table and adds a handful of new
functions that makes the SPI layer cleaner and more flexible.
Instead of the spi_chipsel table, each board that wants to use SPI
gets to implement three hooks:
* spi_cs_activate(): Activates the chipselect for a given slave
* spi_cs_deactivate(): Deactivates the chipselect for a given slave
* spi_cs_is_valid(): Determines if the given bus/chipselect
combination can be activated.
Not all drivers may need those extra functions however. If that's the
case, the board code may just leave them out (assuming they know what
the driver needs) or rely on the linker to strip them out (assuming
--gc-sections is being used.)
To set up communication parameters for a given slave, the driver needs
to call spi_setup_slave(). This returns a pointer to an opaque
spi_slave struct which must be passed as a parameter to subsequent SPI
calls. This struct can be freed by calling spi_free_slave(), but most
driver probably don't want to do this.
Before starting one or more SPI transfers, the driver must call
spi_claim_bus() to gain exclusive access to the SPI bus and initialize
the hardware. When all transfers are done, the driver must call
spi_release_bus() to make the bus available to others, and possibly
shut down the SPI controller hardware.
spi_xfer() behaves mostly the same as before, but it now takes a
spi_slave parameter instead of a spi_chipsel function pointer. It also
got a new parameter, flags, which is used to specify chip select
behaviour. This may be extended with other flags in the future.
This patch has been build-tested on all powerpc and arm boards
involved. I have not tested NIOS since I don't have a toolchain for it
installed, so I expect some breakage there even though I've tried
fixing up everything I could find by visual inspection.
I have run-time tested this on AVR32 ATNGW100 using the atmel_spi and
DataFlash drivers posted as a follow-up. I'd like some help testing
other boards that use the existing SPI API.
But most of all, I'd like some comments on the new API. Is this stuff
usable for everyone? If not, why?
Changed in v4:
- Build fixes for various boards, drivers and commands
- Provide common struct spi_slave definition that can be extended by
drivers
- Pass a struct spi_slave * to spi_cs_activate and spi_cs_deactivate
- Make default bus and mode build-time configurable
- Override default SPI bus ID and mode on mx32ads and imx31_litekit.
Changed in v3:
- Add opaque struct spi_slave for controller-specific data associated
with a slave.
- Add spi_claim_bus() and spi_release_bus()
- Add spi_free_slave()
- spi_setup() is now called spi_setup_slave() and returns a
struct spi_slave
- soft_spi now supports four SPI modes (CPOL|CPHA)
- Add bus parameter to spi_setup_slave()
- Convert the new i.MX32 SPI driver
- Convert the new MC13783 RTC driver
Changed in v2:
- Convert the mpc8xxx_spi driver and the mpc8349emds board to the
new API.
Signed-off-by: Haavard Skinnemoen <hskinnemoen@atmel.com>
Tested-by: Guennadi Liakhovetski <lg@denx.de>
2008-05-16 09:10:31 +00:00
|
|
|
struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
|
|
|
|
unsigned int max_hz, unsigned int mode)
|
2008-04-15 12:14:25 +00:00
|
|
|
{
|
SPI API improvements
This patch gets rid of the spi_chipsel table and adds a handful of new
functions that makes the SPI layer cleaner and more flexible.
Instead of the spi_chipsel table, each board that wants to use SPI
gets to implement three hooks:
* spi_cs_activate(): Activates the chipselect for a given slave
* spi_cs_deactivate(): Deactivates the chipselect for a given slave
* spi_cs_is_valid(): Determines if the given bus/chipselect
combination can be activated.
Not all drivers may need those extra functions however. If that's the
case, the board code may just leave them out (assuming they know what
the driver needs) or rely on the linker to strip them out (assuming
--gc-sections is being used.)
To set up communication parameters for a given slave, the driver needs
to call spi_setup_slave(). This returns a pointer to an opaque
spi_slave struct which must be passed as a parameter to subsequent SPI
calls. This struct can be freed by calling spi_free_slave(), but most
driver probably don't want to do this.
Before starting one or more SPI transfers, the driver must call
spi_claim_bus() to gain exclusive access to the SPI bus and initialize
the hardware. When all transfers are done, the driver must call
spi_release_bus() to make the bus available to others, and possibly
shut down the SPI controller hardware.
spi_xfer() behaves mostly the same as before, but it now takes a
spi_slave parameter instead of a spi_chipsel function pointer. It also
got a new parameter, flags, which is used to specify chip select
behaviour. This may be extended with other flags in the future.
This patch has been build-tested on all powerpc and arm boards
involved. I have not tested NIOS since I don't have a toolchain for it
installed, so I expect some breakage there even though I've tried
fixing up everything I could find by visual inspection.
I have run-time tested this on AVR32 ATNGW100 using the atmel_spi and
DataFlash drivers posted as a follow-up. I'd like some help testing
other boards that use the existing SPI API.
But most of all, I'd like some comments on the new API. Is this stuff
usable for everyone? If not, why?
Changed in v4:
- Build fixes for various boards, drivers and commands
- Provide common struct spi_slave definition that can be extended by
drivers
- Pass a struct spi_slave * to spi_cs_activate and spi_cs_deactivate
- Make default bus and mode build-time configurable
- Override default SPI bus ID and mode on mx32ads and imx31_litekit.
Changed in v3:
- Add opaque struct spi_slave for controller-specific data associated
with a slave.
- Add spi_claim_bus() and spi_release_bus()
- Add spi_free_slave()
- spi_setup() is now called spi_setup_slave() and returns a
struct spi_slave
- soft_spi now supports four SPI modes (CPOL|CPHA)
- Add bus parameter to spi_setup_slave()
- Convert the new i.MX32 SPI driver
- Convert the new MC13783 RTC driver
Changed in v2:
- Convert the mpc8xxx_spi driver and the mpc8349emds board to the
new API.
Signed-off-by: Haavard Skinnemoen <hskinnemoen@atmel.com>
Tested-by: Guennadi Liakhovetski <lg@denx.de>
2008-05-16 09:10:31 +00:00
|
|
|
struct mxc_spi_slave *mxcs;
|
2009-02-13 08:26:40 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (bus >= ARRAY_SIZE(spi_bases))
|
|
|
|
return NULL;
|
|
|
|
|
2013-03-18 19:23:40 +00:00
|
|
|
mxcs = spi_alloc_slave(struct mxc_spi_slave, bus, cs);
|
2010-08-20 10:05:03 +00:00
|
|
|
if (!mxcs) {
|
|
|
|
puts("mxc_spi: SPI Slave not allocated !\n");
|
2009-02-13 08:26:40 +00:00
|
|
|
return NULL;
|
2010-08-20 10:05:03 +00:00
|
|
|
}
|
2008-04-15 12:14:25 +00:00
|
|
|
|
2012-11-15 11:23:23 +00:00
|
|
|
mxcs->ss_pol = (mode & SPI_CS_HIGH) ? 1 : 0;
|
|
|
|
|
spi: mxc: fix sf probe when using mxc_spi
MXC SPI driver has a feature whereas a GPIO line can be used to force CS high
across multiple transactions. This is set up by embedding the GPIO information
in the CS value:
cs = (cs | gpio << 8)
This merge of cs and gpio data into one value breaks the sf probe command:
if the use of gpio is required, invoking "sf probe <cs>" will not work, because
the CS argument doesn't have the GPIO information in it. Instead, the user must
use "sf probe <cs | gpio << 8>". For example, if bank 2 gpio 30 is used to force
cs high on cs 0, bus 0, then instead of typing "sf probe 0" the user now must
type "sf probe 15872".
This is inconsistent with the description of the sf probe command, and forces
the user to be aware of implementaiton details.
Fix this by introducing a new board function: board_spi_cs_gpio(), which will
accept a naked CS value, and provide the driver with the relevant GPIO, if one
is necessary.
Cc: Eric Nelson <eric.nelson@boundarydevices.com>
Cc: Eric Benard <eric@eukrea.com>
Cc: Fabio Estevam <fabio.estevam@freescale.com>
Cc: Tim Harvey <tharvey@gateworks.com>
Cc: Stefano Babic <sbabic@denx.de>
Cc: Tom Rini <trini@ti.com>
Cc: Marek Vasut <marex@denx.de>
Reviewed-by: Marek Vasut <marex@denx.de>
Signed-off-by: Nikita Kiryanov <nikita@compulab.co.il>
Reviewed-by: Jagannadha Sutradharudu Teki <jaganna@xilinx.com>
2014-08-20 12:08:50 +00:00
|
|
|
ret = setup_cs_gpio(mxcs, bus, cs);
|
2009-02-13 08:26:40 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
free(mxcs);
|
SPI API improvements
This patch gets rid of the spi_chipsel table and adds a handful of new
functions that makes the SPI layer cleaner and more flexible.
Instead of the spi_chipsel table, each board that wants to use SPI
gets to implement three hooks:
* spi_cs_activate(): Activates the chipselect for a given slave
* spi_cs_deactivate(): Deactivates the chipselect for a given slave
* spi_cs_is_valid(): Determines if the given bus/chipselect
combination can be activated.
Not all drivers may need those extra functions however. If that's the
case, the board code may just leave them out (assuming they know what
the driver needs) or rely on the linker to strip them out (assuming
--gc-sections is being used.)
To set up communication parameters for a given slave, the driver needs
to call spi_setup_slave(). This returns a pointer to an opaque
spi_slave struct which must be passed as a parameter to subsequent SPI
calls. This struct can be freed by calling spi_free_slave(), but most
driver probably don't want to do this.
Before starting one or more SPI transfers, the driver must call
spi_claim_bus() to gain exclusive access to the SPI bus and initialize
the hardware. When all transfers are done, the driver must call
spi_release_bus() to make the bus available to others, and possibly
shut down the SPI controller hardware.
spi_xfer() behaves mostly the same as before, but it now takes a
spi_slave parameter instead of a spi_chipsel function pointer. It also
got a new parameter, flags, which is used to specify chip select
behaviour. This may be extended with other flags in the future.
This patch has been build-tested on all powerpc and arm boards
involved. I have not tested NIOS since I don't have a toolchain for it
installed, so I expect some breakage there even though I've tried
fixing up everything I could find by visual inspection.
I have run-time tested this on AVR32 ATNGW100 using the atmel_spi and
DataFlash drivers posted as a follow-up. I'd like some help testing
other boards that use the existing SPI API.
But most of all, I'd like some comments on the new API. Is this stuff
usable for everyone? If not, why?
Changed in v4:
- Build fixes for various boards, drivers and commands
- Provide common struct spi_slave definition that can be extended by
drivers
- Pass a struct spi_slave * to spi_cs_activate and spi_cs_deactivate
- Make default bus and mode build-time configurable
- Override default SPI bus ID and mode on mx32ads and imx31_litekit.
Changed in v3:
- Add opaque struct spi_slave for controller-specific data associated
with a slave.
- Add spi_claim_bus() and spi_release_bus()
- Add spi_free_slave()
- spi_setup() is now called spi_setup_slave() and returns a
struct spi_slave
- soft_spi now supports four SPI modes (CPOL|CPHA)
- Add bus parameter to spi_setup_slave()
- Convert the new i.MX32 SPI driver
- Convert the new MC13783 RTC driver
Changed in v2:
- Convert the mpc8xxx_spi driver and the mpc8349emds board to the
new API.
Signed-off-by: Haavard Skinnemoen <hskinnemoen@atmel.com>
Tested-by: Guennadi Liakhovetski <lg@denx.de>
2008-05-16 09:10:31 +00:00
|
|
|
return NULL;
|
2009-02-13 08:26:40 +00:00
|
|
|
}
|
|
|
|
|
2010-04-04 20:43:38 +00:00
|
|
|
mxcs->base = spi_bases[bus];
|
|
|
|
|
2011-01-19 22:46:30 +00:00
|
|
|
ret = spi_cfg_mxc(mxcs, cs, max_hz, mode);
|
2010-04-04 20:43:38 +00:00
|
|
|
if (ret) {
|
|
|
|
printf("mxc_spi: cannot setup SPI controller\n");
|
|
|
|
free(mxcs);
|
|
|
|
return NULL;
|
|
|
|
}
|
SPI API improvements
This patch gets rid of the spi_chipsel table and adds a handful of new
functions that makes the SPI layer cleaner and more flexible.
Instead of the spi_chipsel table, each board that wants to use SPI
gets to implement three hooks:
* spi_cs_activate(): Activates the chipselect for a given slave
* spi_cs_deactivate(): Deactivates the chipselect for a given slave
* spi_cs_is_valid(): Determines if the given bus/chipselect
combination can be activated.
Not all drivers may need those extra functions however. If that's the
case, the board code may just leave them out (assuming they know what
the driver needs) or rely on the linker to strip them out (assuming
--gc-sections is being used.)
To set up communication parameters for a given slave, the driver needs
to call spi_setup_slave(). This returns a pointer to an opaque
spi_slave struct which must be passed as a parameter to subsequent SPI
calls. This struct can be freed by calling spi_free_slave(), but most
driver probably don't want to do this.
Before starting one or more SPI transfers, the driver must call
spi_claim_bus() to gain exclusive access to the SPI bus and initialize
the hardware. When all transfers are done, the driver must call
spi_release_bus() to make the bus available to others, and possibly
shut down the SPI controller hardware.
spi_xfer() behaves mostly the same as before, but it now takes a
spi_slave parameter instead of a spi_chipsel function pointer. It also
got a new parameter, flags, which is used to specify chip select
behaviour. This may be extended with other flags in the future.
This patch has been build-tested on all powerpc and arm boards
involved. I have not tested NIOS since I don't have a toolchain for it
installed, so I expect some breakage there even though I've tried
fixing up everything I could find by visual inspection.
I have run-time tested this on AVR32 ATNGW100 using the atmel_spi and
DataFlash drivers posted as a follow-up. I'd like some help testing
other boards that use the existing SPI API.
But most of all, I'd like some comments on the new API. Is this stuff
usable for everyone? If not, why?
Changed in v4:
- Build fixes for various boards, drivers and commands
- Provide common struct spi_slave definition that can be extended by
drivers
- Pass a struct spi_slave * to spi_cs_activate and spi_cs_deactivate
- Make default bus and mode build-time configurable
- Override default SPI bus ID and mode on mx32ads and imx31_litekit.
Changed in v3:
- Add opaque struct spi_slave for controller-specific data associated
with a slave.
- Add spi_claim_bus() and spi_release_bus()
- Add spi_free_slave()
- spi_setup() is now called spi_setup_slave() and returns a
struct spi_slave
- soft_spi now supports four SPI modes (CPOL|CPHA)
- Add bus parameter to spi_setup_slave()
- Convert the new i.MX32 SPI driver
- Convert the new MC13783 RTC driver
Changed in v2:
- Convert the mpc8xxx_spi driver and the mpc8349emds board to the
new API.
Signed-off-by: Haavard Skinnemoen <hskinnemoen@atmel.com>
Tested-by: Guennadi Liakhovetski <lg@denx.de>
2008-05-16 09:10:31 +00:00
|
|
|
return &mxcs->slave;
|
|
|
|
}
|
|
|
|
|
|
|
|
void spi_free_slave(struct spi_slave *slave)
|
|
|
|
{
|
2009-02-06 23:09:12 +00:00
|
|
|
struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
|
|
|
|
|
|
|
|
free(mxcs);
|
SPI API improvements
This patch gets rid of the spi_chipsel table and adds a handful of new
functions that makes the SPI layer cleaner and more flexible.
Instead of the spi_chipsel table, each board that wants to use SPI
gets to implement three hooks:
* spi_cs_activate(): Activates the chipselect for a given slave
* spi_cs_deactivate(): Deactivates the chipselect for a given slave
* spi_cs_is_valid(): Determines if the given bus/chipselect
combination can be activated.
Not all drivers may need those extra functions however. If that's the
case, the board code may just leave them out (assuming they know what
the driver needs) or rely on the linker to strip them out (assuming
--gc-sections is being used.)
To set up communication parameters for a given slave, the driver needs
to call spi_setup_slave(). This returns a pointer to an opaque
spi_slave struct which must be passed as a parameter to subsequent SPI
calls. This struct can be freed by calling spi_free_slave(), but most
driver probably don't want to do this.
Before starting one or more SPI transfers, the driver must call
spi_claim_bus() to gain exclusive access to the SPI bus and initialize
the hardware. When all transfers are done, the driver must call
spi_release_bus() to make the bus available to others, and possibly
shut down the SPI controller hardware.
spi_xfer() behaves mostly the same as before, but it now takes a
spi_slave parameter instead of a spi_chipsel function pointer. It also
got a new parameter, flags, which is used to specify chip select
behaviour. This may be extended with other flags in the future.
This patch has been build-tested on all powerpc and arm boards
involved. I have not tested NIOS since I don't have a toolchain for it
installed, so I expect some breakage there even though I've tried
fixing up everything I could find by visual inspection.
I have run-time tested this on AVR32 ATNGW100 using the atmel_spi and
DataFlash drivers posted as a follow-up. I'd like some help testing
other boards that use the existing SPI API.
But most of all, I'd like some comments on the new API. Is this stuff
usable for everyone? If not, why?
Changed in v4:
- Build fixes for various boards, drivers and commands
- Provide common struct spi_slave definition that can be extended by
drivers
- Pass a struct spi_slave * to spi_cs_activate and spi_cs_deactivate
- Make default bus and mode build-time configurable
- Override default SPI bus ID and mode on mx32ads and imx31_litekit.
Changed in v3:
- Add opaque struct spi_slave for controller-specific data associated
with a slave.
- Add spi_claim_bus() and spi_release_bus()
- Add spi_free_slave()
- spi_setup() is now called spi_setup_slave() and returns a
struct spi_slave
- soft_spi now supports four SPI modes (CPOL|CPHA)
- Add bus parameter to spi_setup_slave()
- Convert the new i.MX32 SPI driver
- Convert the new MC13783 RTC driver
Changed in v2:
- Convert the mpc8xxx_spi driver and the mpc8349emds board to the
new API.
Signed-off-by: Haavard Skinnemoen <hskinnemoen@atmel.com>
Tested-by: Guennadi Liakhovetski <lg@denx.de>
2008-05-16 09:10:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int spi_claim_bus(struct spi_slave *slave)
|
|
|
|
{
|
|
|
|
struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
|
2011-01-19 22:46:33 +00:00
|
|
|
struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
|
SPI API improvements
This patch gets rid of the spi_chipsel table and adds a handful of new
functions that makes the SPI layer cleaner and more flexible.
Instead of the spi_chipsel table, each board that wants to use SPI
gets to implement three hooks:
* spi_cs_activate(): Activates the chipselect for a given slave
* spi_cs_deactivate(): Deactivates the chipselect for a given slave
* spi_cs_is_valid(): Determines if the given bus/chipselect
combination can be activated.
Not all drivers may need those extra functions however. If that's the
case, the board code may just leave them out (assuming they know what
the driver needs) or rely on the linker to strip them out (assuming
--gc-sections is being used.)
To set up communication parameters for a given slave, the driver needs
to call spi_setup_slave(). This returns a pointer to an opaque
spi_slave struct which must be passed as a parameter to subsequent SPI
calls. This struct can be freed by calling spi_free_slave(), but most
driver probably don't want to do this.
Before starting one or more SPI transfers, the driver must call
spi_claim_bus() to gain exclusive access to the SPI bus and initialize
the hardware. When all transfers are done, the driver must call
spi_release_bus() to make the bus available to others, and possibly
shut down the SPI controller hardware.
spi_xfer() behaves mostly the same as before, but it now takes a
spi_slave parameter instead of a spi_chipsel function pointer. It also
got a new parameter, flags, which is used to specify chip select
behaviour. This may be extended with other flags in the future.
This patch has been build-tested on all powerpc and arm boards
involved. I have not tested NIOS since I don't have a toolchain for it
installed, so I expect some breakage there even though I've tried
fixing up everything I could find by visual inspection.
I have run-time tested this on AVR32 ATNGW100 using the atmel_spi and
DataFlash drivers posted as a follow-up. I'd like some help testing
other boards that use the existing SPI API.
But most of all, I'd like some comments on the new API. Is this stuff
usable for everyone? If not, why?
Changed in v4:
- Build fixes for various boards, drivers and commands
- Provide common struct spi_slave definition that can be extended by
drivers
- Pass a struct spi_slave * to spi_cs_activate and spi_cs_deactivate
- Make default bus and mode build-time configurable
- Override default SPI bus ID and mode on mx32ads and imx31_litekit.
Changed in v3:
- Add opaque struct spi_slave for controller-specific data associated
with a slave.
- Add spi_claim_bus() and spi_release_bus()
- Add spi_free_slave()
- spi_setup() is now called spi_setup_slave() and returns a
struct spi_slave
- soft_spi now supports four SPI modes (CPOL|CPHA)
- Add bus parameter to spi_setup_slave()
- Convert the new i.MX32 SPI driver
- Convert the new MC13783 RTC driver
Changed in v2:
- Convert the mpc8xxx_spi driver and the mpc8349emds board to the
new API.
Signed-off-by: Haavard Skinnemoen <hskinnemoen@atmel.com>
Tested-by: Guennadi Liakhovetski <lg@denx.de>
2008-05-16 09:10:31 +00:00
|
|
|
|
2011-01-19 22:46:33 +00:00
|
|
|
reg_write(®s->rxdata, 1);
|
2008-04-15 12:14:25 +00:00
|
|
|
udelay(1);
|
2011-01-19 22:46:33 +00:00
|
|
|
reg_write(®s->ctrl, mxcs->ctrl_reg);
|
|
|
|
reg_write(®s->period, MXC_CSPIPERIOD_32KHZ);
|
|
|
|
reg_write(®s->intr, 0);
|
2008-04-15 12:14:25 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
SPI API improvements
This patch gets rid of the spi_chipsel table and adds a handful of new
functions that makes the SPI layer cleaner and more flexible.
Instead of the spi_chipsel table, each board that wants to use SPI
gets to implement three hooks:
* spi_cs_activate(): Activates the chipselect for a given slave
* spi_cs_deactivate(): Deactivates the chipselect for a given slave
* spi_cs_is_valid(): Determines if the given bus/chipselect
combination can be activated.
Not all drivers may need those extra functions however. If that's the
case, the board code may just leave them out (assuming they know what
the driver needs) or rely on the linker to strip them out (assuming
--gc-sections is being used.)
To set up communication parameters for a given slave, the driver needs
to call spi_setup_slave(). This returns a pointer to an opaque
spi_slave struct which must be passed as a parameter to subsequent SPI
calls. This struct can be freed by calling spi_free_slave(), but most
driver probably don't want to do this.
Before starting one or more SPI transfers, the driver must call
spi_claim_bus() to gain exclusive access to the SPI bus and initialize
the hardware. When all transfers are done, the driver must call
spi_release_bus() to make the bus available to others, and possibly
shut down the SPI controller hardware.
spi_xfer() behaves mostly the same as before, but it now takes a
spi_slave parameter instead of a spi_chipsel function pointer. It also
got a new parameter, flags, which is used to specify chip select
behaviour. This may be extended with other flags in the future.
This patch has been build-tested on all powerpc and arm boards
involved. I have not tested NIOS since I don't have a toolchain for it
installed, so I expect some breakage there even though I've tried
fixing up everything I could find by visual inspection.
I have run-time tested this on AVR32 ATNGW100 using the atmel_spi and
DataFlash drivers posted as a follow-up. I'd like some help testing
other boards that use the existing SPI API.
But most of all, I'd like some comments on the new API. Is this stuff
usable for everyone? If not, why?
Changed in v4:
- Build fixes for various boards, drivers and commands
- Provide common struct spi_slave definition that can be extended by
drivers
- Pass a struct spi_slave * to spi_cs_activate and spi_cs_deactivate
- Make default bus and mode build-time configurable
- Override default SPI bus ID and mode on mx32ads and imx31_litekit.
Changed in v3:
- Add opaque struct spi_slave for controller-specific data associated
with a slave.
- Add spi_claim_bus() and spi_release_bus()
- Add spi_free_slave()
- spi_setup() is now called spi_setup_slave() and returns a
struct spi_slave
- soft_spi now supports four SPI modes (CPOL|CPHA)
- Add bus parameter to spi_setup_slave()
- Convert the new i.MX32 SPI driver
- Convert the new MC13783 RTC driver
Changed in v2:
- Convert the mpc8xxx_spi driver and the mpc8349emds board to the
new API.
Signed-off-by: Haavard Skinnemoen <hskinnemoen@atmel.com>
Tested-by: Guennadi Liakhovetski <lg@denx.de>
2008-05-16 09:10:31 +00:00
|
|
|
|
|
|
|
void spi_release_bus(struct spi_slave *slave)
|
|
|
|
{
|
|
|
|
/* TODO: Shut the controller down */
|
|
|
|
}
|