blob: a4d03d97cfcff6aff2d20718f40ec83090384545 [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 Vasutcdcdad82014-10-22 21:56:04 +020015#ifndef CONFIG_ALTERA_SPI_IDLE_VAL
16#define CONFIG_ALTERA_SPI_IDLE_VAL 0xff
17#endif
18
19#ifndef CONFIG_SYS_ALTERA_SPI_LIST
20#define CONFIG_SYS_ALTERA_SPI_LIST { CONFIG_SYS_SPI_BASE }
21#endif
22
Marek Vasuteef67022014-10-22 21:55:58 +020023struct altera_spi_regs {
24 u32 rxdata;
25 u32 txdata;
26 u32 status;
27 u32 control;
28 u32 _reserved;
29 u32 slave_sel;
30};
Thomas Chou661ba142010-04-30 11:34:16 +080031
Marek Vasut2e13da12014-10-22 21:55:59 +020032#define ALTERA_SPI_STATUS_ROE_MSK (1 << 3)
33#define ALTERA_SPI_STATUS_TOE_MSK (1 << 4)
34#define ALTERA_SPI_STATUS_TMT_MSK (1 << 5)
35#define ALTERA_SPI_STATUS_TRDY_MSK (1 << 6)
36#define ALTERA_SPI_STATUS_RRDY_MSK (1 << 7)
37#define ALTERA_SPI_STATUS_E_MSK (1 << 8)
Thomas Chou661ba142010-04-30 11:34:16 +080038
Marek Vasut2e13da12014-10-22 21:55:59 +020039#define ALTERA_SPI_CONTROL_IROE_MSK (1 << 3)
40#define ALTERA_SPI_CONTROL_ITOE_MSK (1 << 4)
41#define ALTERA_SPI_CONTROL_ITRDY_MSK (1 << 6)
42#define ALTERA_SPI_CONTROL_IRRDY_MSK (1 << 7)
43#define ALTERA_SPI_CONTROL_IE_MSK (1 << 8)
44#define ALTERA_SPI_CONTROL_SSO_MSK (1 << 10)
Thomas Chou661ba142010-04-30 11:34:16 +080045
Thomas Chou661ba142010-04-30 11:34:16 +080046static ulong altera_spi_base_list[] = CONFIG_SYS_ALTERA_SPI_LIST;
47
48struct altera_spi_slave {
Marek Vasuteef67022014-10-22 21:55:58 +020049 struct spi_slave slave;
50 struct altera_spi_regs *regs;
Thomas Chou661ba142010-04-30 11:34:16 +080051};
52#define to_altera_spi_slave(s) container_of(s, struct altera_spi_slave, slave)
53
Marek Vasut37dcc132014-10-22 21:56:00 +020054__weak int spi_cs_is_valid(unsigned int bus, unsigned int cs)
Thomas Chou661ba142010-04-30 11:34:16 +080055{
56 return bus < ARRAY_SIZE(altera_spi_base_list) && cs < 32;
57}
58
Marek Vasut37dcc132014-10-22 21:56:00 +020059__weak void spi_cs_activate(struct spi_slave *slave)
Thomas Chou661ba142010-04-30 11:34:16 +080060{
61 struct altera_spi_slave *altspi = to_altera_spi_slave(slave);
Marek Vasuteef67022014-10-22 21:55:58 +020062 writel(1 << slave->cs, &altspi->regs->slave_sel);
63 writel(ALTERA_SPI_CONTROL_SSO_MSK, &altspi->regs->control);
Thomas Chou661ba142010-04-30 11:34:16 +080064}
65
Marek Vasut37dcc132014-10-22 21:56:00 +020066__weak void spi_cs_deactivate(struct spi_slave *slave)
Thomas Chou661ba142010-04-30 11:34:16 +080067{
68 struct altera_spi_slave *altspi = to_altera_spi_slave(slave);
Marek Vasuteef67022014-10-22 21:55:58 +020069 writel(0, &altspi->regs->control);
70 writel(0, &altspi->regs->slave_sel);
Thomas Chou661ba142010-04-30 11:34:16 +080071}
72
73void spi_init(void)
74{
75}
76
Thomas Choudf8f1252010-12-27 09:30:17 +080077void spi_set_speed(struct spi_slave *slave, uint hz)
78{
79 /* altera spi core does not support programmable speed */
80}
81
Thomas Chou661ba142010-04-30 11:34:16 +080082struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
83 unsigned int max_hz, unsigned int mode)
84{
85 struct altera_spi_slave *altspi;
86
87 if (!spi_cs_is_valid(bus, cs))
88 return NULL;
89
Simon Glassd3504fe2013-03-18 19:23:40 +000090 altspi = spi_alloc_slave(struct altera_spi_slave, bus, cs);
Thomas Chou661ba142010-04-30 11:34:16 +080091 if (!altspi)
92 return NULL;
93
Marek Vasuteef67022014-10-22 21:55:58 +020094 altspi->regs = (struct altera_spi_regs *)altera_spi_base_list[bus];
Marek Vasut37dcc132014-10-22 21:56:00 +020095 debug("%s: bus:%i cs:%i base:%p\n", __func__, bus, cs, altspi->regs);
Thomas Chou661ba142010-04-30 11:34:16 +080096
97 return &altspi->slave;
98}
99
100void spi_free_slave(struct spi_slave *slave)
101{
102 struct altera_spi_slave *altspi = to_altera_spi_slave(slave);
103 free(altspi);
104}
105
106int spi_claim_bus(struct spi_slave *slave)
107{
108 struct altera_spi_slave *altspi = to_altera_spi_slave(slave);
109
110 debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
Marek Vasuteef67022014-10-22 21:55:58 +0200111 writel(0, &altspi->regs->control);
112 writel(0, &altspi->regs->slave_sel);
Thomas Chou661ba142010-04-30 11:34:16 +0800113 return 0;
114}
115
116void spi_release_bus(struct spi_slave *slave)
117{
118 struct altera_spi_slave *altspi = to_altera_spi_slave(slave);
119
120 debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
Marek Vasuteef67022014-10-22 21:55:58 +0200121 writel(0, &altspi->regs->slave_sel);
Thomas Chou661ba142010-04-30 11:34:16 +0800122}
123
Thomas Chou661ba142010-04-30 11:34:16 +0800124int 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 */
Marek Vasutbc76b822014-10-22 21:56:02 +0200129 unsigned int bytes = bitlen / 8;
130 const unsigned char *txp = dout;
131 unsigned char *rxp = din;
132 uint32_t reg, data, start;
Thomas Chou661ba142010-04-30 11:34:16 +0800133
134 debug("%s: bus:%i cs:%i bitlen:%i bytes:%i flags:%lx\n", __func__,
Marek Vasut37dcc132014-10-22 21:56:00 +0200135 slave->bus, slave->cs, bitlen, bytes, flags);
136
Thomas Chou661ba142010-04-30 11:34:16 +0800137 if (bitlen == 0)
138 goto done;
139
140 if (bitlen % 8) {
141 flags |= SPI_XFER_END;
142 goto done;
143 }
144
145 /* empty read buffer */
Marek Vasuteef67022014-10-22 21:55:58 +0200146 if (readl(&altspi->regs->status) & ALTERA_SPI_STATUS_RRDY_MSK)
147 readl(&altspi->regs->rxdata);
Marek Vasut37dcc132014-10-22 21:56:00 +0200148
Thomas Chou661ba142010-04-30 11:34:16 +0800149 if (flags & SPI_XFER_BEGIN)
150 spi_cs_activate(slave);
151
152 while (bytes--) {
Marek Vasutbc76b822014-10-22 21:56:02 +0200153 if (txp)
154 data = *txp++;
155 else
156 data = CONFIG_ALTERA_SPI_IDLE_VAL;
Marek Vasut37dcc132014-10-22 21:56:00 +0200157
Marek Vasutbc76b822014-10-22 21:56:02 +0200158 debug("%s: tx:%x ", __func__, data);
159 writel(data, &altspi->regs->txdata);
Marek Vasut37dcc132014-10-22 21:56:00 +0200160
Marek Vasut80d73332014-10-22 21:56:01 +0200161 start = get_timer(0);
162 while (1) {
163 reg = readl(&altspi->regs->status);
164 if (reg & ALTERA_SPI_STATUS_RRDY_MSK)
165 break;
166 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
167 printf("%s: Transmission timed out!\n", __func__);
168 goto done;
169 }
170 }
Marek Vasut37dcc132014-10-22 21:56:00 +0200171
Marek Vasutbc76b822014-10-22 21:56:02 +0200172 data = readl(&altspi->regs->rxdata);
Thomas Chou661ba142010-04-30 11:34:16 +0800173 if (rxp)
Marek Vasutbc76b822014-10-22 21:56:02 +0200174 *rxp++ = data & 0xff;
Marek Vasut37dcc132014-10-22 21:56:00 +0200175
Marek Vasutbc76b822014-10-22 21:56:02 +0200176 debug("rx:%x\n", data);
Thomas Chou661ba142010-04-30 11:34:16 +0800177 }
Marek Vasut37dcc132014-10-22 21:56:00 +0200178
179done:
Thomas Chou661ba142010-04-30 11:34:16 +0800180 if (flags & SPI_XFER_END)
181 spi_cs_deactivate(slave);
182
183 return 0;
184}