blob: f729347ee24a3104c520b842c4ca5e7d09ee0313 [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>
Thomas Chou15a56f92015-10-14 08:33:34 +080011#include <dm.h>
12#include <errno.h>
Thomas Chou661ba142010-04-30 11:34:16 +080013#include <malloc.h>
Thomas Chou15a56f92015-10-14 08:33:34 +080014#include <fdtdec.h>
Jagan Tekibef87ad2015-10-27 23:11:11 +053015#include <spi.h>
Thomas Chou15a56f92015-10-14 08:33:34 +080016#include <asm/io.h>
17
Jagan Tekibef87ad2015-10-27 23:11:11 +053018#define ALTERA_SPI_STATUS_RRDY_MSK BIT(7)
19#define ALTERA_SPI_CONTROL_SSO_MSK BIT(10)
20
Marek Vasutcdcdad82014-10-22 21:56:04 +020021#ifndef CONFIG_ALTERA_SPI_IDLE_VAL
Jagan Tekibef87ad2015-10-27 23:11:11 +053022#define CONFIG_ALTERA_SPI_IDLE_VAL 0xff
Marek Vasutcdcdad82014-10-22 21:56:04 +020023#endif
24
Marek Vasuteef67022014-10-22 21:55:58 +020025struct altera_spi_regs {
26 u32 rxdata;
27 u32 txdata;
28 u32 status;
29 u32 control;
30 u32 _reserved;
31 u32 slave_sel;
32};
Thomas Chou661ba142010-04-30 11:34:16 +080033
Thomas Chou15a56f92015-10-14 08:33:34 +080034struct altera_spi_platdata {
35 struct altera_spi_regs *regs;
36};
Thomas Chou661ba142010-04-30 11:34:16 +080037
Thomas Chou15a56f92015-10-14 08:33:34 +080038struct altera_spi_priv {
39 struct altera_spi_regs *regs;
40};
41
Thomas Chou15a56f92015-10-14 08:33:34 +080042static void spi_cs_activate(struct udevice *dev, uint cs)
Thomas Chou661ba142010-04-30 11:34:16 +080043{
Thomas Chou15a56f92015-10-14 08:33:34 +080044 struct udevice *bus = dev->parent;
45 struct altera_spi_priv *priv = dev_get_priv(bus);
46 struct altera_spi_regs *const regs = priv->regs;
47
48 writel(1 << cs, &regs->slave_sel);
49 writel(ALTERA_SPI_CONTROL_SSO_MSK, &regs->control);
Thomas Chou661ba142010-04-30 11:34:16 +080050}
51
Thomas Chou15a56f92015-10-14 08:33:34 +080052static void spi_cs_deactivate(struct udevice *dev)
Thomas Chou661ba142010-04-30 11:34:16 +080053{
Thomas Chou15a56f92015-10-14 08:33:34 +080054 struct udevice *bus = dev->parent;
55 struct altera_spi_priv *priv = dev_get_priv(bus);
56 struct altera_spi_regs *const regs = priv->regs;
57
58 writel(0, &regs->control);
59 writel(0, &regs->slave_sel);
Thomas Chou661ba142010-04-30 11:34:16 +080060}
61
Thomas Chou15a56f92015-10-14 08:33:34 +080062static int altera_spi_claim_bus(struct udevice *dev)
Thomas Chou661ba142010-04-30 11:34:16 +080063{
Thomas Chou15a56f92015-10-14 08:33:34 +080064 struct udevice *bus = dev->parent;
65 struct altera_spi_priv *priv = dev_get_priv(bus);
66 struct altera_spi_regs *const regs = priv->regs;
Thomas Chou661ba142010-04-30 11:34:16 +080067
Thomas Chou15a56f92015-10-14 08:33:34 +080068 writel(0, &regs->control);
69 writel(0, &regs->slave_sel);
Thomas Chou661ba142010-04-30 11:34:16 +080070
Thomas Chou661ba142010-04-30 11:34:16 +080071 return 0;
72}
73
Thomas Chou15a56f92015-10-14 08:33:34 +080074static int altera_spi_release_bus(struct udevice *dev)
Thomas Chou661ba142010-04-30 11:34:16 +080075{
Thomas Chou15a56f92015-10-14 08:33:34 +080076 struct udevice *bus = dev->parent;
77 struct altera_spi_priv *priv = dev_get_priv(bus);
78 struct altera_spi_regs *const regs = priv->regs;
Thomas Chou661ba142010-04-30 11:34:16 +080079
Thomas Chou15a56f92015-10-14 08:33:34 +080080 writel(0, &regs->slave_sel);
81
82 return 0;
Thomas Chou661ba142010-04-30 11:34:16 +080083}
84
Thomas Chou15a56f92015-10-14 08:33:34 +080085static int altera_spi_xfer(struct udevice *dev, unsigned int bitlen,
86 const void *dout, void *din, unsigned long flags)
Thomas Chou661ba142010-04-30 11:34:16 +080087{
Thomas Chou15a56f92015-10-14 08:33:34 +080088 struct udevice *bus = dev->parent;
89 struct altera_spi_priv *priv = dev_get_priv(bus);
90 struct altera_spi_regs *const regs = priv->regs;
91 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
92
Thomas Chou661ba142010-04-30 11:34:16 +080093 /* assume spi core configured to do 8 bit transfers */
Marek Vasutbc76b822014-10-22 21:56:02 +020094 unsigned int bytes = bitlen / 8;
95 const unsigned char *txp = dout;
96 unsigned char *rxp = din;
97 uint32_t reg, data, start;
Thomas Chou661ba142010-04-30 11:34:16 +080098
99 debug("%s: bus:%i cs:%i bitlen:%i bytes:%i flags:%lx\n", __func__,
Thomas Chou15a56f92015-10-14 08:33:34 +0800100 bus->seq, slave_plat->cs, bitlen, bytes, flags);
Marek Vasut37dcc132014-10-22 21:56:00 +0200101
Thomas Chou661ba142010-04-30 11:34:16 +0800102 if (bitlen == 0)
103 goto done;
104
105 if (bitlen % 8) {
106 flags |= SPI_XFER_END;
107 goto done;
108 }
109
110 /* empty read buffer */
Thomas Chou15a56f92015-10-14 08:33:34 +0800111 if (readl(&regs->status) & ALTERA_SPI_STATUS_RRDY_MSK)
112 readl(&regs->rxdata);
Marek Vasut37dcc132014-10-22 21:56:00 +0200113
Thomas Chou661ba142010-04-30 11:34:16 +0800114 if (flags & SPI_XFER_BEGIN)
Thomas Chou15a56f92015-10-14 08:33:34 +0800115 spi_cs_activate(dev, slave_plat->cs);
Thomas Chou661ba142010-04-30 11:34:16 +0800116
117 while (bytes--) {
Marek Vasutbc76b822014-10-22 21:56:02 +0200118 if (txp)
119 data = *txp++;
120 else
121 data = CONFIG_ALTERA_SPI_IDLE_VAL;
Marek Vasut37dcc132014-10-22 21:56:00 +0200122
Marek Vasutbc76b822014-10-22 21:56:02 +0200123 debug("%s: tx:%x ", __func__, data);
Thomas Chou15a56f92015-10-14 08:33:34 +0800124 writel(data, &regs->txdata);
Marek Vasut37dcc132014-10-22 21:56:00 +0200125
Marek Vasut80d73332014-10-22 21:56:01 +0200126 start = get_timer(0);
127 while (1) {
Thomas Chou15a56f92015-10-14 08:33:34 +0800128 reg = readl(&regs->status);
Marek Vasut80d73332014-10-22 21:56:01 +0200129 if (reg & ALTERA_SPI_STATUS_RRDY_MSK)
130 break;
131 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
Thomas Chou15a56f92015-10-14 08:33:34 +0800132 debug("%s: Transmission timed out!\n", __func__);
133 return -1;
Marek Vasut80d73332014-10-22 21:56:01 +0200134 }
135 }
Marek Vasut37dcc132014-10-22 21:56:00 +0200136
Thomas Chou15a56f92015-10-14 08:33:34 +0800137 data = readl(&regs->rxdata);
Thomas Chou661ba142010-04-30 11:34:16 +0800138 if (rxp)
Marek Vasutbc76b822014-10-22 21:56:02 +0200139 *rxp++ = data & 0xff;
Marek Vasut37dcc132014-10-22 21:56:00 +0200140
Marek Vasutbc76b822014-10-22 21:56:02 +0200141 debug("rx:%x\n", data);
Thomas Chou661ba142010-04-30 11:34:16 +0800142 }
Marek Vasut37dcc132014-10-22 21:56:00 +0200143
144done:
Thomas Chou661ba142010-04-30 11:34:16 +0800145 if (flags & SPI_XFER_END)
Thomas Chou15a56f92015-10-14 08:33:34 +0800146 spi_cs_deactivate(dev);
Thomas Chou661ba142010-04-30 11:34:16 +0800147
148 return 0;
149}
Thomas Chou15a56f92015-10-14 08:33:34 +0800150
151static int altera_spi_set_speed(struct udevice *bus, uint speed)
152{
153 return 0;
154}
155
156static int altera_spi_set_mode(struct udevice *bus, uint mode)
157{
158 return 0;
159}
160
161static int altera_spi_probe(struct udevice *bus)
162{
163 struct altera_spi_platdata *plat = dev_get_platdata(bus);
164 struct altera_spi_priv *priv = dev_get_priv(bus);
165
166 priv->regs = plat->regs;
167
168 return 0;
169}
170
171static int altera_spi_ofdata_to_platdata(struct udevice *bus)
172{
173 struct altera_spi_platdata *plat = dev_get_platdata(bus);
174
Simon Glassa821c4a2017-05-17 17:18:05 -0600175 plat->regs = map_physmem(devfdt_get_addr(bus),
Thomas Chou7313e212015-11-14 11:17:25 +0800176 sizeof(struct altera_spi_regs),
177 MAP_NOCACHE);
Thomas Chou15a56f92015-10-14 08:33:34 +0800178
179 return 0;
180}
181
182static const struct dm_spi_ops altera_spi_ops = {
183 .claim_bus = altera_spi_claim_bus,
184 .release_bus = altera_spi_release_bus,
185 .xfer = altera_spi_xfer,
186 .set_speed = altera_spi_set_speed,
187 .set_mode = altera_spi_set_mode,
188 /*
189 * cs_info is not needed, since we require all chip selects to be
190 * in the device tree explicitly
191 */
192};
193
194static const struct udevice_id altera_spi_ids[] = {
Thomas Chouddf34c22015-10-31 20:55:48 +0800195 { .compatible = "altr,spi-1.0" },
196 {}
Thomas Chou15a56f92015-10-14 08:33:34 +0800197};
198
199U_BOOT_DRIVER(altera_spi) = {
200 .name = "altera_spi",
201 .id = UCLASS_SPI,
202 .of_match = altera_spi_ids,
203 .ops = &altera_spi_ops,
204 .ofdata_to_platdata = altera_spi_ofdata_to_platdata,
205 .platdata_auto_alloc_size = sizeof(struct altera_spi_platdata),
206 .priv_auto_alloc_size = sizeof(struct altera_spi_priv),
207 .probe = altera_spi_probe,
208};