blob: 2fe9d5cf18b4de97d6ea6a8f390b49f6f1653c0e [file] [log] [blame]
Angel Pons1ddb8942020-04-04 18:51:26 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Gabe Black607c0b62013-05-16 05:45:57 -07002
Kyösti Mälkki13f66502019-03-03 08:01:05 +02003#include <device/mmio.h>
Gabe Black607c0b62013-05-16 05:45:57 -07004#include <assert.h>
Julius Werner9b1f3cc2020-12-30 17:30:12 -08005#include <cbfs.h>
Julius Werner80af4422014-10-20 13:18:56 -07006#include <console/console.h>
7#include <soc/cpu.h>
8#include <soc/spi.h>
Furquan Shaikhc28984d2016-11-20 21:04:00 -08009#include <spi-generic.h>
Elyes HAOUAS7884c222020-07-27 08:12:59 +020010#include <stddef.h>
11#include <stdint.h>
Gabe Black967058f2014-03-21 21:32:12 -070012#include <string.h>
Aaron Durbinc6588c52015-05-15 13:15:34 -050013#include <symbols.h>
Hung-Te Lin45d524d22013-06-26 20:03:47 +080014
Hung-Te Lin86442072013-06-26 20:29:06 +080015#define EXYNOS_SPI_MAX_TRANSFER_BYTES (65535)
16
Gabe Black607c0b62013-05-16 05:45:57 -070017#if defined(CONFIG_DEBUG_SPI) && CONFIG_DEBUG_SPI
18# define DEBUG_SPI(x,...) printk(BIOS_DEBUG, "EXYNOS_SPI: " x)
19#else
20# define DEBUG_SPI(x,...)
21#endif
22
Hung-Te Linffa5bad2013-06-26 20:16:13 +080023struct exynos_spi_slave {
24 struct spi_slave slave;
25 struct exynos_spi *regs;
Gabe Black967058f2014-03-21 21:32:12 -070026 int initialized;
Hung-Te Linffa5bad2013-06-26 20:16:13 +080027};
28
Hung-Te Lined1742c2013-06-26 20:17:42 +080029/* TODO(hungte) Move the SPI param list to per-board configuration, probably
30 * Kconfig or mainboard.c */
31static struct exynos_spi_slave exynos_spi_slaves[3] = {
32 // SPI 0
33 {
34 .slave = { .bus = 0, },
Julius Wernerfa938c72013-08-29 14:17:36 -070035 .regs = (void *)EXYNOS5_SPI0_BASE,
Hung-Te Lined1742c2013-06-26 20:17:42 +080036 },
37 // SPI 1
38 {
Furquan Shaikhc28984d2016-11-20 21:04:00 -080039 .slave = { .bus = 1, },
Julius Wernerfa938c72013-08-29 14:17:36 -070040 .regs = (void *)EXYNOS5_SPI1_BASE,
Hung-Te Lined1742c2013-06-26 20:17:42 +080041 },
42 // SPI 2
43 {
Furquan Shaikhc28984d2016-11-20 21:04:00 -080044 .slave = { .bus = 2, },
Julius Wernerfa938c72013-08-29 14:17:36 -070045 .regs = (void *)EXYNOS5_SPI2_BASE,
Hung-Te Lined1742c2013-06-26 20:17:42 +080046 },
47};
48
Furquan Shaikh0dba0252016-11-30 04:34:22 -080049static inline struct exynos_spi_slave *to_exynos_spi(const struct spi_slave *slave)
Hung-Te Linffa5bad2013-06-26 20:16:13 +080050{
Furquan Shaikh36b81af2016-12-01 01:02:44 -080051 return &exynos_spi_slaves[slave->bus];
Hung-Te Linffa5bad2013-06-26 20:16:13 +080052}
53
Gabe Black967058f2014-03-21 21:32:12 -070054static void spi_sw_reset(struct exynos_spi *regs, int word)
55{
Julius Werner2f37bd62015-02-19 14:51:15 -080056 const uint32_t orig_mode_cfg = read32(&regs->mode_cfg);
Gabe Black967058f2014-03-21 21:32:12 -070057 uint32_t mode_cfg = orig_mode_cfg;
Julius Werner2f37bd62015-02-19 14:51:15 -080058 const uint32_t orig_swap_cfg = read32(&regs->swap_cfg);
Gabe Black967058f2014-03-21 21:32:12 -070059 uint32_t swap_cfg = orig_swap_cfg;
60
61 mode_cfg &= ~(SPI_MODE_CH_WIDTH_MASK | SPI_MODE_BUS_WIDTH_MASK);
62 if (word) {
63 mode_cfg |= SPI_MODE_CH_WIDTH_WORD | SPI_MODE_BUS_WIDTH_WORD;
64 swap_cfg |= SPI_RX_SWAP_EN |
65 SPI_RX_BYTE_SWAP |
66 SPI_RX_HWORD_SWAP |
67 SPI_TX_SWAP_EN |
68 SPI_TX_BYTE_SWAP |
69 SPI_TX_HWORD_SWAP;
70 } else {
71 mode_cfg |= SPI_MODE_CH_WIDTH_BYTE | SPI_MODE_BUS_WIDTH_BYTE;
72 swap_cfg = 0;
73 }
74
75 if (mode_cfg != orig_mode_cfg)
Julius Werner2f37bd62015-02-19 14:51:15 -080076 write32(&regs->mode_cfg, mode_cfg);
Gabe Black967058f2014-03-21 21:32:12 -070077 if (swap_cfg != orig_swap_cfg)
Julius Werner2f37bd62015-02-19 14:51:15 -080078 write32(&regs->swap_cfg, swap_cfg);
Gabe Black967058f2014-03-21 21:32:12 -070079
Julius Werner55009af2019-12-02 22:03:27 -080080 clrbits32(&regs->ch_cfg, SPI_RX_CH_ON | SPI_TX_CH_ON);
81 setbits32(&regs->ch_cfg, SPI_CH_RST);
82 clrbits32(&regs->ch_cfg, SPI_CH_RST);
83 setbits32(&regs->ch_cfg, SPI_RX_CH_ON | SPI_TX_CH_ON);
Gabe Black967058f2014-03-21 21:32:12 -070084}
85
Gabe Black967058f2014-03-21 21:32:12 -070086static void exynos_spi_init(struct exynos_spi *regs)
87{
88 // Set FB_CLK_SEL.
Julius Werner2f37bd62015-02-19 14:51:15 -080089 write32(&regs->fb_clk, SPI_FB_DELAY_180);
Gabe Black967058f2014-03-21 21:32:12 -070090 // CPOL: Active high.
Julius Werner55009af2019-12-02 22:03:27 -080091 clrbits32(&regs->ch_cfg, SPI_CH_CPOL_L);
Gabe Black967058f2014-03-21 21:32:12 -070092
Elyes HAOUAS1b296ee2020-02-19 20:48:29 +010093 // Clear rx and tx channel if set previously.
Julius Werner55009af2019-12-02 22:03:27 -080094 clrbits32(&regs->ch_cfg, SPI_RX_CH_ON | SPI_TX_CH_ON);
Gabe Black967058f2014-03-21 21:32:12 -070095
Julius Werner55009af2019-12-02 22:03:27 -080096 setbits32(&regs->swap_cfg,
Gabe Black967058f2014-03-21 21:32:12 -070097 SPI_RX_SWAP_EN | SPI_RX_BYTE_SWAP | SPI_RX_HWORD_SWAP);
Julius Werner55009af2019-12-02 22:03:27 -080098 clrbits32(&regs->ch_cfg, SPI_CH_HS_EN);
Gabe Black967058f2014-03-21 21:32:12 -070099
100 // Do a soft reset, which will also enable both channels.
101 spi_sw_reset(regs, 1);
Hung-Te Linffa5bad2013-06-26 20:16:13 +0800102}
103
Furquan Shaikh94f86992016-12-01 07:12:32 -0800104static int spi_ctrlr_claim_bus(const struct spi_slave *slave)
Hung-Te Linffa5bad2013-06-26 20:16:13 +0800105{
106 struct exynos_spi *regs = to_exynos_spi(slave)->regs;
107 // TODO(hungte) Add some delay if too many transactions happen at once.
Julius Werner55009af2019-12-02 22:03:27 -0800108 clrbits32(&regs->cs_reg, SPI_SLAVE_SIG_INACT);
Gabe Black607c0b62013-05-16 05:45:57 -0700109 return 0;
110}
111
Gabe Black967058f2014-03-21 21:32:12 -0700112static void spi_transfer(struct exynos_spi *regs, void *in, const void *out,
Furquan Shaikh0dba0252016-11-30 04:34:22 -0800113 size_t size)
Gabe Black607c0b62013-05-16 05:45:57 -0700114{
Gabe Black967058f2014-03-21 21:32:12 -0700115 u8 *inb = in;
116 const u8 *outb = out;
Hung-Te Lina965a372013-06-26 20:31:22 +0800117
Furquan Shaikh0dba0252016-11-30 04:34:22 -0800118 size_t width = (size % 4) ? 1 : 4;
Hung-Te Lina965a372013-06-26 20:31:22 +0800119
Gabe Black967058f2014-03-21 21:32:12 -0700120 while (size) {
Furquan Shaikh0dba0252016-11-30 04:34:22 -0800121 size_t packets = size / width;
Gabe Black967058f2014-03-21 21:32:12 -0700122 // The packet count field is 16 bits wide.
123 packets = MIN(packets, (1 << 16) - 1);
124
Furquan Shaikh0dba0252016-11-30 04:34:22 -0800125 size_t out_bytes, in_bytes;
Gabe Black967058f2014-03-21 21:32:12 -0700126 out_bytes = in_bytes = packets * width;
127
128 spi_sw_reset(regs, width == 4);
Julius Werner2f37bd62015-02-19 14:51:15 -0800129 write32(&regs->pkt_cnt, packets | SPI_PACKET_CNT_EN);
Gabe Black967058f2014-03-21 21:32:12 -0700130
131 while (out_bytes || in_bytes) {
Julius Werner2f37bd62015-02-19 14:51:15 -0800132 uint32_t spi_sts = read32(&regs->spi_sts);
Gabe Black967058f2014-03-21 21:32:12 -0700133 int rx_lvl = ((spi_sts >> 15) & 0x1ff);
134 int tx_lvl = ((spi_sts >> 6) & 0x1ff);
135
136 if (tx_lvl < 32 && tx_lvl < out_bytes) {
137 uint32_t data = 0xffffffff;
138
139 if (outb) {
140 memcpy(&data, outb, width);
141 outb += width;
142 }
Julius Werner2f37bd62015-02-19 14:51:15 -0800143 write32(&regs->tx_data, data);
Gabe Black967058f2014-03-21 21:32:12 -0700144
145 out_bytes -= width;
146 }
147
148 if (rx_lvl >= width) {
Julius Werner2f37bd62015-02-19 14:51:15 -0800149 uint32_t data = read32(&regs->rx_data);
Gabe Black967058f2014-03-21 21:32:12 -0700150
151 if (inb) {
152 memcpy(inb, &data, width);
153 inb += width;
154 }
155
156 in_bytes -= width;
157 }
Hung-Te Lina965a372013-06-26 20:31:22 +0800158 }
Gabe Black967058f2014-03-21 21:32:12 -0700159
160 size -= packets * width;
Hung-Te Lina965a372013-06-26 20:31:22 +0800161 }
Gabe Black967058f2014-03-21 21:32:12 -0700162}
Hung-Te Lina965a372013-06-26 20:31:22 +0800163
Furquan Shaikh94f86992016-12-01 07:12:32 -0800164static int spi_ctrlr_xfer(const struct spi_slave *slave, const void *dout, size_t bytes_out,
Furquan Shaikh0dba0252016-11-30 04:34:22 -0800165 void *din, size_t bytes_in)
Gabe Black967058f2014-03-21 21:32:12 -0700166{
167 struct exynos_spi *regs = to_exynos_spi(slave)->regs;
168
169 if (bytes_out && bytes_in) {
Furquan Shaikh0dba0252016-11-30 04:34:22 -0800170 size_t min_size = MIN(bytes_out, bytes_in);
Gabe Black967058f2014-03-21 21:32:12 -0700171
172 spi_transfer(regs, din, dout, min_size);
173
174 bytes_out -= min_size;
175 bytes_in -= min_size;
176
177 din = (uint8_t *)din + min_size;
178 dout = (const uint8_t *)dout + min_size;
179 }
180
181 if (bytes_in)
182 spi_transfer(regs, din, NULL, bytes_in);
183 else if (bytes_out)
184 spi_transfer(regs, NULL, dout, bytes_out);
185
186 return 0;
187}
188
Furquan Shaikh94f86992016-12-01 07:12:32 -0800189static void spi_ctrlr_release_bus(const struct spi_slave *slave)
Gabe Black967058f2014-03-21 21:32:12 -0700190{
Gabe Blackec9293f2014-03-27 21:26:46 -0700191 struct exynos_spi *regs = to_exynos_spi(slave)->regs;
Julius Werner55009af2019-12-02 22:03:27 -0800192 setbits32(&regs->cs_reg, SPI_SLAVE_SIG_INACT);
Hung-Te Linffa5bad2013-06-26 20:16:13 +0800193}
194
Furquan Shaikhf8662ca2017-05-18 14:44:04 -0700195static int spi_ctrlr_setup(const struct spi_slave *slave)
Furquan Shaikh94f86992016-12-01 07:12:32 -0800196{
Julius Werner7c712bb2019-05-01 16:51:20 -0700197 ASSERT(slave->bus < 3);
Furquan Shaikh94f86992016-12-01 07:12:32 -0800198 struct exynos_spi_slave *eslave;
199
Furquan Shaikh94f86992016-12-01 07:12:32 -0800200 eslave = to_exynos_spi(slave);
201 if (!eslave->initialized) {
202 exynos_spi_init(eslave->regs);
203 eslave->initialized = 1;
204 }
205 return 0;
206}
207
Furquan Shaikhf8662ca2017-05-18 14:44:04 -0700208static const struct spi_ctrlr spi_ctrlr = {
209 .setup = spi_ctrlr_setup,
210 .claim_bus = spi_ctrlr_claim_bus,
211 .release_bus = spi_ctrlr_release_bus,
212 .xfer = spi_ctrlr_xfer,
213 .max_xfer_size = SPI_CTRLR_DEFAULT_MAX_XFER_SIZE,
214};
215
216const struct spi_ctrlr_buses spi_ctrlr_bus_map[] = {
217 {
218 .ctrlr = &spi_ctrlr,
219 .bus_start = 0,
220 .bus_end = 2,
221 },
222};
223
224const size_t spi_ctrlr_bus_map_count = ARRAY_SIZE(spi_ctrlr_bus_map);
225
Hung-Te Linffa5bad2013-06-26 20:16:13 +0800226static int exynos_spi_read(struct spi_slave *slave, void *dest, uint32_t len,
227 uint32_t off)
228{
229 struct exynos_spi *regs = to_exynos_spi(slave)->regs;
Gabe Black967058f2014-03-21 21:32:12 -0700230 u32 command;
231 spi_claim_bus(slave);
Hung-Te Line42030d2013-06-23 08:14:30 +0800232
Gabe Black967058f2014-03-21 21:32:12 -0700233 // Send address.
Hung-Te Lina965a372013-06-26 20:31:22 +0800234 ASSERT(off < (1 << 24));
Gabe Black967058f2014-03-21 21:32:12 -0700235 command = htonl(SF_READ_DATA_CMD << 24 | off);
236 spi_transfer(regs, NULL, &command, sizeof(command));
Gabe Black607c0b62013-05-16 05:45:57 -0700237
Gabe Black967058f2014-03-21 21:32:12 -0700238 // Read the data.
239 spi_transfer(regs, dest, NULL, len);
240 spi_release_bus(slave);
Gabe Black607c0b62013-05-16 05:45:57 -0700241
Gabe Black967058f2014-03-21 21:32:12 -0700242 return len;
Gabe Black607c0b62013-05-16 05:45:57 -0700243}
244
Aaron Durbinc6588c52015-05-15 13:15:34 -0500245static struct exynos_spi_slave *boot_slave;
Gabe Black607c0b62013-05-16 05:45:57 -0700246
Aaron Durbinc6588c52015-05-15 13:15:34 -0500247static ssize_t exynos_spi_readat(const struct region_device *rdev, void *dest,
248 size_t offset, size_t count)
Stefan Reinauerff0df2b2013-09-17 14:08:29 -0700249{
Gabe Black607c0b62013-05-16 05:45:57 -0700250 DEBUG_SPI("exynos_spi_cbfs_read(%u)\n", count);
Aaron Durbinc6588c52015-05-15 13:15:34 -0500251 return exynos_spi_read(&boot_slave->slave, dest, count, offset);
Gabe Black607c0b62013-05-16 05:45:57 -0700252}
253
Aaron Durbinc6588c52015-05-15 13:15:34 -0500254static void *exynos_spi_map(const struct region_device *rdev,
255 size_t offset, size_t count)
Stefan Reinauerff0df2b2013-09-17 14:08:29 -0700256{
Gabe Black607c0b62013-05-16 05:45:57 -0700257 DEBUG_SPI("exynos_spi_cbfs_map\n");
Hung-Te Lina965a372013-06-26 20:31:22 +0800258 // exynos: spi_rx_tx may work in 4 byte-width-transmission mode and
259 // requires buffer memory address to be aligned.
Gabe Black607c0b62013-05-16 05:45:57 -0700260 if (count % 4)
261 count += 4 - (count % 4);
Aaron Durbinc6588c52015-05-15 13:15:34 -0500262 return mmap_helper_rdev_mmap(rdev, offset, count);
Gabe Black607c0b62013-05-16 05:45:57 -0700263}
264
Aaron Durbinc6588c52015-05-15 13:15:34 -0500265static const struct region_device_ops exynos_spi_ops = {
266 .mmap = exynos_spi_map,
267 .munmap = mmap_helper_rdev_munmap,
268 .readat = exynos_spi_readat,
269};
270
271static struct mmap_helper_region_device mdev =
Julius Werner9b1f3cc2020-12-30 17:30:12 -0800272 MMAP_HELPER_DEV_INIT(&exynos_spi_ops, 0, CONFIG_ROM_SIZE, &cbfs_cache);
Aaron Durbinc6588c52015-05-15 13:15:34 -0500273
274void exynos_init_spi_boot_device(void)
Stefan Reinauerff0df2b2013-09-17 14:08:29 -0700275{
Aaron Durbinc6588c52015-05-15 13:15:34 -0500276 boot_slave = &exynos_spi_slaves[1];
Gabe Black607c0b62013-05-16 05:45:57 -0700277}
278
Aaron Durbinc6588c52015-05-15 13:15:34 -0500279const struct region_device *exynos_spi_boot_device(void)
Stefan Reinauerff0df2b2013-09-17 14:08:29 -0700280{
Aaron Durbinc6588c52015-05-15 13:15:34 -0500281 return &mdev.rdev;
Gabe Black607c0b62013-05-16 05:45:57 -0700282}