2 * Freescale i.MX28 SPI driver
4 * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
5 * on behalf of DENX Software Engineering GmbH
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22 * NOTE: This driver only supports the SPI-controller chipselects,
23 * GPIO driven chipselects are not supported.
29 #include <asm/errno.h>
31 #include <asm/arch/clock.h>
32 #include <asm/arch/imx-regs.h>
33 #include <asm/arch/sys_proto.h>
35 #define MXS_SPI_MAX_TIMEOUT 1000000
36 #define MXS_SPI_PORT_OFFSET 0x2000
38 struct mxs_spi_slave {
39 struct spi_slave slave;
42 struct mx28_ssp_regs *regs;
45 static inline struct mxs_spi_slave *to_mxs_slave(struct spi_slave *slave)
47 return container_of(slave, struct mxs_spi_slave, slave);
54 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
56 /* MXS SPI: 4 ports and 3 chip selects maximum */
57 if (bus > 3 || cs > 2)
63 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
64 unsigned int max_hz, unsigned int mode)
66 struct mxs_spi_slave *mxs_slave;
69 if (!spi_cs_is_valid(bus, cs)) {
70 printf("mxs_spi: invalid bus %d / chip select %d\n", bus, cs);
74 mxs_slave = malloc(sizeof(struct mxs_spi_slave));
78 addr = MXS_SSP0_BASE + (bus * MXS_SPI_PORT_OFFSET);
80 mxs_slave->slave.bus = bus;
81 mxs_slave->slave.cs = cs;
82 mxs_slave->max_khz = max_hz / 1000;
83 mxs_slave->mode = mode;
84 mxs_slave->regs = (struct mx28_ssp_regs *)addr;
86 return &mxs_slave->slave;
89 void spi_free_slave(struct spi_slave *slave)
91 struct mxs_spi_slave *mxs_slave = to_mxs_slave(slave);
95 int spi_claim_bus(struct spi_slave *slave)
97 struct mxs_spi_slave *mxs_slave = to_mxs_slave(slave);
98 struct mx28_ssp_regs *ssp_regs = mxs_slave->regs;
101 mx28_reset_block(&ssp_regs->hw_ssp_ctrl0_reg);
103 writel(SSP_CTRL0_BUS_WIDTH_ONE_BIT, &ssp_regs->hw_ssp_ctrl0);
105 reg = SSP_CTRL1_SSP_MODE_SPI | SSP_CTRL1_WORD_LENGTH_EIGHT_BITS;
106 reg |= (mxs_slave->mode & SPI_CPOL) ? SSP_CTRL1_POLARITY : 0;
107 reg |= (mxs_slave->mode & SPI_CPHA) ? SSP_CTRL1_PHASE : 0;
108 writel(reg, &ssp_regs->hw_ssp_ctrl1);
110 writel(0, &ssp_regs->hw_ssp_cmd0);
112 mx28_set_ssp_busclock(slave->bus, mxs_slave->max_khz);
117 void spi_release_bus(struct spi_slave *slave)
121 static void mxs_spi_start_xfer(struct mx28_ssp_regs *ssp_regs)
123 writel(SSP_CTRL0_LOCK_CS, &ssp_regs->hw_ssp_ctrl0_set);
124 writel(SSP_CTRL0_IGNORE_CRC, &ssp_regs->hw_ssp_ctrl0_clr);
127 static void mxs_spi_end_xfer(struct mx28_ssp_regs *ssp_regs)
129 writel(SSP_CTRL0_LOCK_CS, &ssp_regs->hw_ssp_ctrl0_clr);
130 writel(SSP_CTRL0_IGNORE_CRC, &ssp_regs->hw_ssp_ctrl0_set);
133 int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
134 const void *dout, void *din, unsigned long flags)
136 struct mxs_spi_slave *mxs_slave = to_mxs_slave(slave);
137 struct mx28_ssp_regs *ssp_regs = mxs_slave->regs;
138 int len = bitlen / 8;
139 const char *tx = dout;
144 if (flags & SPI_XFER_END) {
154 if (flags & SPI_XFER_BEGIN)
155 mxs_spi_start_xfer(ssp_regs);
158 /* We transfer 1 byte */
159 writel(1, &ssp_regs->hw_ssp_xfer_size);
161 if ((flags & SPI_XFER_END) && !len)
162 mxs_spi_end_xfer(ssp_regs);
165 writel(SSP_CTRL0_READ, &ssp_regs->hw_ssp_ctrl0_clr);
167 writel(SSP_CTRL0_READ, &ssp_regs->hw_ssp_ctrl0_set);
169 writel(SSP_CTRL0_RUN, &ssp_regs->hw_ssp_ctrl0_set);
171 if (mx28_wait_mask_set(&ssp_regs->hw_ssp_ctrl0_reg,
172 SSP_CTRL0_RUN, MXS_SPI_MAX_TIMEOUT)) {
173 printf("MXS SPI: Timeout waiting for start\n");
178 writel(*tx++, &ssp_regs->hw_ssp_data);
180 writel(SSP_CTRL0_DATA_XFER, &ssp_regs->hw_ssp_ctrl0_set);
183 if (mx28_wait_mask_clr(&ssp_regs->hw_ssp_status_reg,
184 SSP_STATUS_FIFO_EMPTY, MXS_SPI_MAX_TIMEOUT)) {
185 printf("MXS SPI: Timeout waiting for data\n");
189 *rx = readl(&ssp_regs->hw_ssp_data);
193 if (mx28_wait_mask_clr(&ssp_regs->hw_ssp_ctrl0_reg,
194 SSP_CTRL0_RUN, MXS_SPI_MAX_TIMEOUT)) {
195 printf("MXS SPI: Timeout waiting for finish\n");