blob: 373ce30a0324e8f5f42869bdac2bf2fb8a5d4197 [file] [log] [blame]
Thomas Chou661ba142010-04-30 11:34:16 +08001/*
2 * Altera SPI driver
3 *
4 * based on bfin_spi.c
5 * Copyright (c) 2005-2008 Analog Devices Inc.
6 * Copyright (C) 2010 Thomas Chou <thomas@wytron.com.tw>
7 *
Jagannadha Sutradharudu Tekie7b1e452013-10-14 13:31:24 +05308 * SPDX-License-Identifier: GPL-2.0+
Thomas Chou661ba142010-04-30 11:34:16 +08009 */
10#include <common.h>
11#include <asm/io.h>
12#include <malloc.h>
13#include <spi.h>
14
Marek Vasuteef67022014-10-22 21:55:58 +020015struct altera_spi_regs {
16 u32 rxdata;
17 u32 txdata;
18 u32 status;
19 u32 control;
20 u32 _reserved;
21 u32 slave_sel;
22};
Thomas Chou661ba142010-04-30 11:34:16 +080023
Marek Vasut2e13da12014-10-22 21:55:59 +020024#define ALTERA_SPI_STATUS_ROE_MSK (1 << 3)
25#define ALTERA_SPI_STATUS_TOE_MSK (1 << 4)
26#define ALTERA_SPI_STATUS_TMT_MSK (1 << 5)
27#define ALTERA_SPI_STATUS_TRDY_MSK (1 << 6)
28#define ALTERA_SPI_STATUS_RRDY_MSK (1 << 7)
29#define ALTERA_SPI_STATUS_E_MSK (1 << 8)
Thomas Chou661ba142010-04-30 11:34:16 +080030
Marek Vasut2e13da12014-10-22 21:55:59 +020031#define ALTERA_SPI_CONTROL_IROE_MSK (1 << 3)
32#define ALTERA_SPI_CONTROL_ITOE_MSK (1 << 4)
33#define ALTERA_SPI_CONTROL_ITRDY_MSK (1 << 6)
34#define ALTERA_SPI_CONTROL_IRRDY_MSK (1 << 7)
35#define ALTERA_SPI_CONTROL_IE_MSK (1 << 8)
36#define ALTERA_SPI_CONTROL_SSO_MSK (1 << 10)
Thomas Chou661ba142010-04-30 11:34:16 +080037
38#ifndef CONFIG_SYS_ALTERA_SPI_LIST
39#define CONFIG_SYS_ALTERA_SPI_LIST { CONFIG_SYS_SPI_BASE }
40#endif
41
42static ulong altera_spi_base_list[] = CONFIG_SYS_ALTERA_SPI_LIST;
43
44struct altera_spi_slave {
Marek Vasuteef67022014-10-22 21:55:58 +020045 struct spi_slave slave;
46 struct altera_spi_regs *regs;
Thomas Chou661ba142010-04-30 11:34:16 +080047};
48#define to_altera_spi_slave(s) container_of(s, struct altera_spi_slave, slave)
49
Marek Vasut37dcc132014-10-22 21:56:00 +020050__weak int spi_cs_is_valid(unsigned int bus, unsigned int cs)
Thomas Chou661ba142010-04-30 11:34:16 +080051{
52 return bus < ARRAY_SIZE(altera_spi_base_list) && cs < 32;
53}
54
Marek Vasut37dcc132014-10-22 21:56:00 +020055__weak void spi_cs_activate(struct spi_slave *slave)
Thomas Chou661ba142010-04-30 11:34:16 +080056{
57 struct altera_spi_slave *altspi = to_altera_spi_slave(slave);
Marek Vasuteef67022014-10-22 21:55:58 +020058 writel(1 << slave->cs, &altspi->regs->slave_sel);
59 writel(ALTERA_SPI_CONTROL_SSO_MSK, &altspi->regs->control);
Thomas Chou661ba142010-04-30 11:34:16 +080060}
61
Marek Vasut37dcc132014-10-22 21:56:00 +020062__weak void spi_cs_deactivate(struct spi_slave *slave)
Thomas Chou661ba142010-04-30 11:34:16 +080063{
64 struct altera_spi_slave *altspi = to_altera_spi_slave(slave);
Marek Vasuteef67022014-10-22 21:55:58 +020065 writel(0, &altspi->regs->control);
66 writel(0, &altspi->regs->slave_sel);
Thomas Chou661ba142010-04-30 11:34:16 +080067}
68
69void spi_init(void)
70{
71}
72
Thomas Choudf8f1252010-12-27 09:30:17 +080073void spi_set_speed(struct spi_slave *slave, uint hz)
74{
75 /* altera spi core does not support programmable speed */
76}
77
Thomas Chou661ba142010-04-30 11:34:16 +080078struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
79 unsigned int max_hz, unsigned int mode)
80{
81 struct altera_spi_slave *altspi;
82
83 if (!spi_cs_is_valid(bus, cs))
84 return NULL;
85
Simon Glassd3504fe2013-03-18 19:23:40 +000086 altspi = spi_alloc_slave(struct altera_spi_slave, bus, cs);
Thomas Chou661ba142010-04-30 11:34:16 +080087 if (!altspi)
88 return NULL;
89
Marek Vasuteef67022014-10-22 21:55:58 +020090 altspi->regs = (struct altera_spi_regs *)altera_spi_base_list[bus];
Marek Vasut37dcc132014-10-22 21:56:00 +020091 debug("%s: bus:%i cs:%i base:%p\n", __func__, bus, cs, altspi->regs);
Thomas Chou661ba142010-04-30 11:34:16 +080092
93 return &altspi->slave;
94}
95
96void spi_free_slave(struct spi_slave *slave)
97{
98 struct altera_spi_slave *altspi = to_altera_spi_slave(slave);
99 free(altspi);
100}
101
102int spi_claim_bus(struct spi_slave *slave)
103{
104 struct altera_spi_slave *altspi = to_altera_spi_slave(slave);
105
106 debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
Marek Vasuteef67022014-10-22 21:55:58 +0200107 writel(0, &altspi->regs->control);
108 writel(0, &altspi->regs->slave_sel);
Thomas Chou661ba142010-04-30 11:34:16 +0800109 return 0;
110}
111
112void spi_release_bus(struct spi_slave *slave)
113{
114 struct altera_spi_slave *altspi = to_altera_spi_slave(slave);
115
116 debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
Marek Vasuteef67022014-10-22 21:55:58 +0200117 writel(0, &altspi->regs->slave_sel);
Thomas Chou661ba142010-04-30 11:34:16 +0800118}
119
120#ifndef CONFIG_ALTERA_SPI_IDLE_VAL
121# define CONFIG_ALTERA_SPI_IDLE_VAL 0xff
122#endif
123
124int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
125 void *din, unsigned long flags)
126{
127 struct altera_spi_slave *altspi = to_altera_spi_slave(slave);
128 /* assume spi core configured to do 8 bit transfers */
129 uint bytes = bitlen / 8;
130 const uchar *txp = dout;
131 uchar *rxp = din;
132
133 debug("%s: bus:%i cs:%i bitlen:%i bytes:%i flags:%lx\n", __func__,
Marek Vasut37dcc132014-10-22 21:56:00 +0200134 slave->bus, slave->cs, bitlen, bytes, flags);
135
Thomas Chou661ba142010-04-30 11:34:16 +0800136 if (bitlen == 0)
137 goto done;
138
139 if (bitlen % 8) {
140 flags |= SPI_XFER_END;
141 goto done;
142 }
143
144 /* empty read buffer */
Marek Vasuteef67022014-10-22 21:55:58 +0200145 if (readl(&altspi->regs->status) & ALTERA_SPI_STATUS_RRDY_MSK)
146 readl(&altspi->regs->rxdata);
Marek Vasut37dcc132014-10-22 21:56:00 +0200147
Thomas Chou661ba142010-04-30 11:34:16 +0800148 if (flags & SPI_XFER_BEGIN)
149 spi_cs_activate(slave);
150
151 while (bytes--) {
152 uchar d = txp ? *txp++ : CONFIG_ALTERA_SPI_IDLE_VAL;
Marek Vasut37dcc132014-10-22 21:56:00 +0200153
Thomas Chou661ba142010-04-30 11:34:16 +0800154 debug("%s: tx:%x ", __func__, d);
Marek Vasuteef67022014-10-22 21:55:58 +0200155 writel(d, &altspi->regs->txdata);
Marek Vasut37dcc132014-10-22 21:56:00 +0200156
Marek Vasuteef67022014-10-22 21:55:58 +0200157 while (!(readl(&altspi->regs->status) & ALTERA_SPI_STATUS_RRDY_MSK))
Thomas Chou661ba142010-04-30 11:34:16 +0800158 ;
Marek Vasut37dcc132014-10-22 21:56:00 +0200159
Marek Vasuteef67022014-10-22 21:55:58 +0200160 d = readl(&altspi->regs->rxdata);
Thomas Chou661ba142010-04-30 11:34:16 +0800161 if (rxp)
162 *rxp++ = d;
Marek Vasut37dcc132014-10-22 21:56:00 +0200163
Thomas Chou661ba142010-04-30 11:34:16 +0800164 debug("rx:%x\n", d);
165 }
Marek Vasut37dcc132014-10-22 21:56:00 +0200166
167done:
Thomas Chou661ba142010-04-30 11:34:16 +0800168 if (flags & SPI_XFER_END)
169 spi_cs_deactivate(slave);
170
171 return 0;
172}