2 * Copyright (C) 2008, Guennadi Liakhovetski <lg@denx.de>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24 #include <asm/errno.h>
27 #include <asm/arch/imx-regs.h>
28 #include <asm/arch/clock.h>
31 /* i.MX27 has a completely wrong register layout and register definitions in the
32 * datasheet, the correct one is in the Freescale's Linux driver */
34 #error "i.MX27 CSPI not supported due to drastic differences in register definitions" \
35 "See linux mxc_spi driver from Freescale for details."
38 static unsigned long spi_bases[] = {
39 MXC_SPI_BASE_ADDRESSES
42 #define OUT MXC_GPIO_DIRECTION_OUT
44 #define reg_read readl
45 #define reg_write(a, v) writel(v, a)
47 struct mxc_spi_slave {
48 struct spi_slave slave;
51 #if defined(MXC_ECSPI)
58 static inline struct mxc_spi_slave *to_mxc_spi_slave(struct spi_slave *slave)
60 return container_of(slave, struct mxc_spi_slave, slave);
63 void spi_cs_activate(struct spi_slave *slave)
65 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
67 gpio_set_value(mxcs->gpio, mxcs->ss_pol);
70 void spi_cs_deactivate(struct spi_slave *slave)
72 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
74 gpio_set_value(mxcs->gpio,
78 u32 get_cspi_div(u32 div)
82 for (i = 0; i < 8; i++) {
90 static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs,
91 unsigned int max_hz, unsigned int mode)
93 unsigned int ctrl_reg;
97 clk_src = mxc_get_clock(MXC_CSPI_CLK);
99 div = DIV_ROUND_UP(clk_src, max_hz);
100 div = get_cspi_div(div);
102 debug("clk %d Hz, div %d, real clk %d Hz\n",
103 max_hz, div, clk_src / (4 << div));
105 ctrl_reg = MXC_CSPICTRL_CHIPSELECT(cs) |
106 MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS) |
107 MXC_CSPICTRL_DATARATE(div) |
115 ctrl_reg |= MXC_CSPICTRL_PHA;
117 ctrl_reg |= MXC_CSPICTRL_POL;
118 if (mode & SPI_CS_HIGH)
119 ctrl_reg |= MXC_CSPICTRL_SSPOL;
120 mxcs->ctrl_reg = ctrl_reg;
127 static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs,
128 unsigned int max_hz, unsigned int mode)
130 u32 clk_src = mxc_get_clock(MXC_CSPI_CLK);
131 s32 reg_ctrl, reg_config;
132 u32 ss_pol = 0, sclkpol = 0, sclkpha = 0, pre_div = 0, post_div = 0;
133 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
136 printf("Error: desired clock is 0\n");
141 * Reset SPI and set all CSs to master mode, if toggling
142 * between slave and master mode we might see a glitch
145 reg_ctrl = MXC_CSPICTRL_MODE_MASK;
146 reg_write(®s->ctrl, reg_ctrl);
147 reg_ctrl |= MXC_CSPICTRL_EN;
148 reg_write(®s->ctrl, reg_ctrl);
150 if (clk_src > max_hz) {
151 pre_div = (clk_src - 1) / max_hz;
152 /* fls(1) = 1, fls(0x80000000) = 32, fls(16) = 5 */
153 post_div = fls(pre_div);
156 if (post_div >= 16) {
157 printf("Error: no divider for the freq: %d\n",
161 pre_div >>= post_div;
167 debug("pre_div = %d, post_div=%d\n", pre_div, post_div);
168 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_SELCHAN(3)) |
169 MXC_CSPICTRL_SELCHAN(cs);
170 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_PREDIV(0x0F)) |
171 MXC_CSPICTRL_PREDIV(pre_div);
172 reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_POSTDIV(0x0F)) |
173 MXC_CSPICTRL_POSTDIV(post_div);
175 /* We need to disable SPI before changing registers */
176 reg_ctrl &= ~MXC_CSPICTRL_EN;
178 if (mode & SPI_CS_HIGH)
187 reg_config = reg_read(®s->cfg);
190 * Configuration register setup
191 * The MX51 supports different setup for each SS
193 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_SSPOL))) |
194 (ss_pol << (cs + MXC_CSPICON_SSPOL));
195 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_POL))) |
196 (sclkpol << (cs + MXC_CSPICON_POL));
197 reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_PHA))) |
198 (sclkpha << (cs + MXC_CSPICON_PHA));
200 debug("reg_ctrl = 0x%x\n", reg_ctrl);
201 reg_write(®s->ctrl, reg_ctrl);
202 debug("reg_config = 0x%x\n", reg_config);
203 reg_write(®s->cfg, reg_config);
205 /* save config register and control register */
206 mxcs->ctrl_reg = reg_ctrl;
207 mxcs->cfg_reg = reg_config;
209 /* clear interrupt reg */
210 reg_write(®s->intr, 0);
211 reg_write(®s->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
217 int spi_xchg_single(struct spi_slave *slave, unsigned int bitlen,
218 const u8 *dout, u8 *din, unsigned long flags)
220 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
221 int nbytes = (bitlen + 7) / 8;
223 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
225 debug("%s: bitlen %d dout 0x%x din 0x%x\n",
226 __func__, bitlen, (u32)dout, (u32)din);
228 mxcs->ctrl_reg = (mxcs->ctrl_reg &
229 ~MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS)) |
230 MXC_CSPICTRL_BITCOUNT(bitlen - 1);
232 reg_write(®s->ctrl, mxcs->ctrl_reg | MXC_CSPICTRL_EN);
234 reg_write(®s->cfg, mxcs->cfg_reg);
237 /* Clear interrupt register */
238 reg_write(®s->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
241 * The SPI controller works only with words,
242 * check if less than a word is sent.
243 * Access to the FIFO is only 32 bit
247 cnt = (bitlen % 32) / 8;
249 for (i = 0; i < cnt; i++) {
250 data = (data << 8) | (*dout++ & 0xFF);
253 debug("Sending SPI 0x%x\n", data);
255 reg_write(®s->txdata, data);
264 /* Buffer is not 32-bit aligned */
265 if ((unsigned long)dout & 0x03) {
267 for (i = 0; i < 4; i++)
268 data = (data << 8) | (*dout++ & 0xFF);
271 data = cpu_to_be32(data);
275 debug("Sending SPI 0x%x\n", data);
276 reg_write(®s->txdata, data);
280 /* FIFO is written, now starts the transfer setting the XCH bit */
281 reg_write(®s->ctrl, mxcs->ctrl_reg |
282 MXC_CSPICTRL_EN | MXC_CSPICTRL_XCH);
284 /* Wait until the TC (Transfer completed) bit is set */
285 while ((reg_read(®s->stat) & MXC_CSPICTRL_TC) == 0)
288 /* Transfer completed, clear any pending request */
289 reg_write(®s->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
291 nbytes = (bitlen + 7) / 8;
296 data = reg_read(®s->rxdata);
297 cnt = (bitlen % 32) / 8;
298 data = cpu_to_be32(data) >> ((sizeof(data) - cnt) * 8);
299 debug("SPI Rx unaligned: 0x%x\n", data);
301 memcpy(din, &data, cnt);
309 tmp = reg_read(®s->rxdata);
310 data = cpu_to_be32(tmp);
311 debug("SPI Rx: 0x%x 0x%x\n", tmp, data);
312 cnt = min(nbytes, sizeof(data));
314 memcpy(din, &data, cnt);
324 int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
325 void *din, unsigned long flags)
327 int n_bytes = (bitlen + 7) / 8;
331 u8 *p_outbuf = (u8 *)dout;
332 u8 *p_inbuf = (u8 *)din;
337 if (flags & SPI_XFER_BEGIN)
338 spi_cs_activate(slave);
340 while (n_bytes > 0) {
341 if (n_bytes < MAX_SPI_BYTES)
344 blk_size = MAX_SPI_BYTES;
346 n_bits = blk_size * 8;
348 ret = spi_xchg_single(slave, n_bits, p_outbuf, p_inbuf, 0);
353 p_outbuf += blk_size;
359 if (flags & SPI_XFER_END) {
360 spi_cs_deactivate(slave);
370 static int decode_cs(struct mxc_spi_slave *mxcs, unsigned int cs)
375 * Some SPI devices require active chip-select over multiple
376 * transactions, we achieve this using a GPIO. Still, the SPI
377 * controller has to be configured to use one of its own chipselects.
378 * To use this feature you have to call spi_setup_slave() with
379 * cs = internal_cs | (gpio << 8), and you have to use some unused
380 * on this SPI controller cs between 0 and 3.
383 mxcs->gpio = cs >> 8;
385 ret = gpio_direction_output(mxcs->gpio, !(mxcs->ss_pol));
387 printf("mxc_spi: cannot setup gpio %d\n", mxcs->gpio);
397 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
398 unsigned int max_hz, unsigned int mode)
400 struct mxc_spi_slave *mxcs;
403 if (bus >= ARRAY_SIZE(spi_bases))
406 mxcs = spi_alloc_slave(struct mxc_spi_slave, bus, cs);
408 puts("mxc_spi: SPI Slave not allocated !\n");
412 mxcs->ss_pol = (mode & SPI_CS_HIGH) ? 1 : 0;
414 ret = decode_cs(mxcs, cs);
422 mxcs->base = spi_bases[bus];
424 ret = spi_cfg_mxc(mxcs, cs, max_hz, mode);
426 printf("mxc_spi: cannot setup SPI controller\n");
433 void spi_free_slave(struct spi_slave *slave)
435 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
440 int spi_claim_bus(struct spi_slave *slave)
442 struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
443 struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
445 reg_write(®s->rxdata, 1);
447 reg_write(®s->ctrl, mxcs->ctrl_reg);
448 reg_write(®s->period, MXC_CSPIPERIOD_32KHZ);
449 reg_write(®s->intr, 0);
454 void spi_release_bus(struct spi_slave *slave)
456 /* TODO: Shut the controller down */