blob: f23467cf6acdac3bfcecb506a29bcf6a7e623352 [file] [log] [blame]
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -05001// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2//
3// AMD SPI controller driver
4//
5// Copyright (c) 2020, Advanced Micro Devices, Inc.
6//
7// Author: Sanjay R Mehta <sanju.mehta@amd.com>
8
9#include <linux/acpi.h>
10#include <linux/init.h>
11#include <linux/module.h>
12#include <linux/platform_device.h>
13#include <linux/delay.h>
14#include <linux/spi/spi.h>
15
16#define AMD_SPI_CTRL0_REG 0x00
17#define AMD_SPI_EXEC_CMD BIT(16)
18#define AMD_SPI_FIFO_CLEAR BIT(20)
19#define AMD_SPI_BUSY BIT(31)
20
21#define AMD_SPI_OPCODE_MASK 0xFF
22
23#define AMD_SPI_ALT_CS_REG 0x1D
24#define AMD_SPI_ALT_CS_MASK 0x3
25
26#define AMD_SPI_FIFO_BASE 0x80
27#define AMD_SPI_TX_COUNT_REG 0x48
28#define AMD_SPI_RX_COUNT_REG 0x4B
29#define AMD_SPI_STATUS_REG 0x4C
30
31#define AMD_SPI_MEM_SIZE 200
32
33/* M_CMD OP codes for SPI */
34#define AMD_SPI_XFER_TX 1
35#define AMD_SPI_XFER_RX 2
36
37struct amd_spi {
38 void __iomem *io_remap_addr;
39 unsigned long io_base_addr;
40 u32 rom_addr;
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -050041 u8 chip_select;
42};
43
Lucas Tanureca8e8a12021-09-10 12:15:26 +010044static inline u8 amd_spi_readreg8(struct amd_spi *amd_spi, int idx)
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -050045{
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -050046 return ioread8((u8 __iomem *)amd_spi->io_remap_addr + idx);
47}
48
Lucas Tanureca8e8a12021-09-10 12:15:26 +010049static inline void amd_spi_writereg8(struct amd_spi *amd_spi, int idx, u8 val)
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -050050{
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -050051 iowrite8(val, ((u8 __iomem *)amd_spi->io_remap_addr + idx));
52}
53
Lucas Tanureca8e8a12021-09-10 12:15:26 +010054static void amd_spi_setclear_reg8(struct amd_spi *amd_spi, int idx, u8 set, u8 clear)
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -050055{
Lucas Tanureca8e8a12021-09-10 12:15:26 +010056 u8 tmp = amd_spi_readreg8(amd_spi, idx);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -050057
58 tmp = (tmp & ~clear) | set;
Lucas Tanureca8e8a12021-09-10 12:15:26 +010059 amd_spi_writereg8(amd_spi, idx, tmp);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -050060}
61
Lucas Tanureca8e8a12021-09-10 12:15:26 +010062static inline u32 amd_spi_readreg32(struct amd_spi *amd_spi, int idx)
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -050063{
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -050064 return ioread32((u8 __iomem *)amd_spi->io_remap_addr + idx);
65}
66
Lucas Tanureca8e8a12021-09-10 12:15:26 +010067static inline void amd_spi_writereg32(struct amd_spi *amd_spi, int idx, u32 val)
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -050068{
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -050069 iowrite32(val, ((u8 __iomem *)amd_spi->io_remap_addr + idx));
70}
71
Lucas Tanureca8e8a12021-09-10 12:15:26 +010072static inline void amd_spi_setclear_reg32(struct amd_spi *amd_spi, int idx, u32 set, u32 clear)
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -050073{
Lucas Tanureca8e8a12021-09-10 12:15:26 +010074 u32 tmp = amd_spi_readreg32(amd_spi, idx);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -050075
76 tmp = (tmp & ~clear) | set;
Lucas Tanureca8e8a12021-09-10 12:15:26 +010077 amd_spi_writereg32(amd_spi, idx, tmp);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -050078}
79
Lucas Tanureca8e8a12021-09-10 12:15:26 +010080static void amd_spi_select_chip(struct amd_spi *amd_spi)
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -050081{
Lucas Tanureca8e8a12021-09-10 12:15:26 +010082 amd_spi_setclear_reg8(amd_spi, AMD_SPI_ALT_CS_REG, amd_spi->chip_select,
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -050083 AMD_SPI_ALT_CS_MASK);
84}
85
Lucas Tanureca8e8a12021-09-10 12:15:26 +010086static void amd_spi_clear_fifo_ptr(struct amd_spi *amd_spi)
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -050087{
Lucas Tanureca8e8a12021-09-10 12:15:26 +010088 amd_spi_setclear_reg32(amd_spi, AMD_SPI_CTRL0_REG, AMD_SPI_FIFO_CLEAR, AMD_SPI_FIFO_CLEAR);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -050089}
90
Lucas Tanureca8e8a12021-09-10 12:15:26 +010091static void amd_spi_set_opcode(struct amd_spi *amd_spi, u8 cmd_opcode)
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -050092{
Lucas Tanureca8e8a12021-09-10 12:15:26 +010093 amd_spi_setclear_reg32(amd_spi, AMD_SPI_CTRL0_REG, cmd_opcode, AMD_SPI_OPCODE_MASK);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -050094}
95
Lucas Tanureca8e8a12021-09-10 12:15:26 +010096static inline void amd_spi_set_rx_count(struct amd_spi *amd_spi, u8 rx_count)
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -050097{
Lucas Tanureca8e8a12021-09-10 12:15:26 +010098 amd_spi_setclear_reg8(amd_spi, AMD_SPI_RX_COUNT_REG, rx_count, 0xff);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -050099}
100
Lucas Tanureca8e8a12021-09-10 12:15:26 +0100101static inline void amd_spi_set_tx_count(struct amd_spi *amd_spi, u8 tx_count)
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500102{
Lucas Tanureca8e8a12021-09-10 12:15:26 +0100103 amd_spi_setclear_reg8(amd_spi, AMD_SPI_TX_COUNT_REG, tx_count, 0xff);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500104}
105
106static inline int amd_spi_busy_wait(struct amd_spi *amd_spi)
107{
108 bool spi_busy;
109 int timeout = 100000;
110
111 /* poll for SPI bus to become idle */
112 spi_busy = (ioread32((u8 __iomem *)amd_spi->io_remap_addr +
113 AMD_SPI_CTRL0_REG) & AMD_SPI_BUSY) == AMD_SPI_BUSY;
114 while (spi_busy) {
115 usleep_range(10, 20);
116 if (timeout-- < 0)
117 return -ETIMEDOUT;
118
119 spi_busy = (ioread32((u8 __iomem *)amd_spi->io_remap_addr +
120 AMD_SPI_CTRL0_REG) & AMD_SPI_BUSY) == AMD_SPI_BUSY;
121 }
122
123 return 0;
124}
125
Lucas Tanureca8e8a12021-09-10 12:15:26 +0100126static void amd_spi_execute_opcode(struct amd_spi *amd_spi)
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500127{
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500128 /* Set ExecuteOpCode bit in the CTRL0 register */
Lucas Tanureca8e8a12021-09-10 12:15:26 +0100129 amd_spi_setclear_reg32(amd_spi, AMD_SPI_CTRL0_REG, AMD_SPI_EXEC_CMD, AMD_SPI_EXEC_CMD);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500130 amd_spi_busy_wait(amd_spi);
131}
132
133static int amd_spi_master_setup(struct spi_device *spi)
134{
Lucas Tanureca8e8a12021-09-10 12:15:26 +0100135 struct amd_spi *amd_spi = spi_master_get_devdata(spi->master);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500136
Lucas Tanureca8e8a12021-09-10 12:15:26 +0100137 amd_spi_clear_fifo_ptr(amd_spi);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500138
139 return 0;
140}
141
142static inline int amd_spi_fifo_xfer(struct amd_spi *amd_spi,
Lukas Wunner36c72a52020-05-04 13:12:05 +0200143 struct spi_master *master,
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500144 struct spi_message *message)
145{
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500146 struct spi_transfer *xfer = NULL;
Sanjay R Mehta68d047c2020-04-27 23:56:41 -0500147 u8 cmd_opcode;
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500148 u8 *buf = NULL;
149 u32 m_cmd = 0;
150 u32 i = 0;
151 u32 tx_len = 0, rx_len = 0;
152
153 list_for_each_entry(xfer, &message->transfers,
154 transfer_list) {
155 if (xfer->rx_buf)
156 m_cmd = AMD_SPI_XFER_RX;
157 if (xfer->tx_buf)
158 m_cmd = AMD_SPI_XFER_TX;
159
160 if (m_cmd & AMD_SPI_XFER_TX) {
161 buf = (u8 *)xfer->tx_buf;
162 tx_len = xfer->len - 1;
163 cmd_opcode = *(u8 *)xfer->tx_buf;
164 buf++;
Lucas Tanureca8e8a12021-09-10 12:15:26 +0100165 amd_spi_set_opcode(amd_spi, cmd_opcode);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500166
167 /* Write data into the FIFO. */
168 for (i = 0; i < tx_len; i++) {
Lucas Tanureca8e8a12021-09-10 12:15:26 +0100169 iowrite8(buf[i], ((u8 __iomem *)amd_spi->io_remap_addr +
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500170 AMD_SPI_FIFO_BASE + i));
171 }
172
Lucas Tanureca8e8a12021-09-10 12:15:26 +0100173 amd_spi_set_tx_count(amd_spi, tx_len);
174 amd_spi_clear_fifo_ptr(amd_spi);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500175 /* Execute command */
Lucas Tanureca8e8a12021-09-10 12:15:26 +0100176 amd_spi_execute_opcode(amd_spi);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500177 }
178 if (m_cmd & AMD_SPI_XFER_RX) {
179 /*
180 * Store no. of bytes to be received from
181 * FIFO
182 */
183 rx_len = xfer->len;
184 buf = (u8 *)xfer->rx_buf;
Lucas Tanureca8e8a12021-09-10 12:15:26 +0100185 amd_spi_set_rx_count(amd_spi, rx_len);
186 amd_spi_clear_fifo_ptr(amd_spi);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500187 /* Execute command */
Lucas Tanureca8e8a12021-09-10 12:15:26 +0100188 amd_spi_execute_opcode(amd_spi);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500189 /* Read data from FIFO to receive buffer */
190 for (i = 0; i < rx_len; i++)
Lucas Tanureca8e8a12021-09-10 12:15:26 +0100191 buf[i] = amd_spi_readreg8(amd_spi, AMD_SPI_FIFO_BASE + tx_len + i);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500192 }
193 }
194
195 /* Update statistics */
196 message->actual_length = tx_len + rx_len + 1;
197 /* complete the transaction */
198 message->status = 0;
199 spi_finalize_current_message(master);
200
201 return 0;
202}
203
204static int amd_spi_master_transfer(struct spi_master *master,
205 struct spi_message *msg)
206{
207 struct amd_spi *amd_spi = spi_master_get_devdata(master);
208 struct spi_device *spi = msg->spi;
209
210 amd_spi->chip_select = spi->chip_select;
Lucas Tanureca8e8a12021-09-10 12:15:26 +0100211 amd_spi_select_chip(amd_spi);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500212
213 /*
214 * Extract spi_transfers from the spi message and
215 * program the controller.
216 */
Lukas Wunner36c72a52020-05-04 13:12:05 +0200217 amd_spi_fifo_xfer(amd_spi, master, msg);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500218
219 return 0;
220}
221
222static int amd_spi_probe(struct platform_device *pdev)
223{
224 struct device *dev = &pdev->dev;
225 struct spi_master *master;
226 struct amd_spi *amd_spi;
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500227 int err = 0;
228
229 /* Allocate storage for spi_master and driver private data */
230 master = spi_alloc_master(dev, sizeof(struct amd_spi));
231 if (!master) {
232 dev_err(dev, "Error allocating SPI master\n");
233 return -ENOMEM;
234 }
235
236 amd_spi = spi_master_get_devdata(master);
Qing Zhang2ed6e3b2020-11-21 11:43:51 +0800237 amd_spi->io_remap_addr = devm_platform_ioremap_resource(pdev, 0);
Wei Yongjunf84b6042020-04-29 02:54:26 +0000238 if (IS_ERR(amd_spi->io_remap_addr)) {
239 err = PTR_ERR(amd_spi->io_remap_addr);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500240 dev_err(dev, "error %d ioremap of SPI registers failed\n", err);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500241 goto err_free_master;
242 }
243 dev_dbg(dev, "io_remap_address: %p\n", amd_spi->io_remap_addr);
244
245 /* Initialize the spi_master fields */
246 master->bus_num = 0;
247 master->num_chipselect = 4;
248 master->mode_bits = 0;
249 master->flags = SPI_MASTER_HALF_DUPLEX;
250 master->setup = amd_spi_master_setup;
251 master->transfer_one_message = amd_spi_master_transfer;
252
253 /* Register the controller with SPI framework */
Lukas Wunner7b9c94b2020-05-04 13:12:04 +0200254 err = devm_spi_register_master(dev, master);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500255 if (err) {
256 dev_err(dev, "error %d registering SPI controller\n", err);
Lukas Wunner2b60c492020-05-04 13:12:01 +0200257 goto err_free_master;
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500258 }
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500259
260 return 0;
261
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500262err_free_master:
263 spi_master_put(master);
264
Lukas Wunnercc17fbe2020-05-04 13:12:02 +0200265 return err;
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500266}
267
Lee Jones85ed0f62020-07-17 14:54:24 +0100268#ifdef CONFIG_ACPI
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500269static const struct acpi_device_id spi_acpi_match[] = {
270 { "AMDI0061", 0 },
271 {},
272};
273MODULE_DEVICE_TABLE(acpi, spi_acpi_match);
Lee Jones85ed0f62020-07-17 14:54:24 +0100274#endif
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500275
276static struct platform_driver amd_spi_driver = {
277 .driver = {
278 .name = "amd_spi",
279 .acpi_match_table = ACPI_PTR(spi_acpi_match),
280 },
281 .probe = amd_spi_probe,
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500282};
283
284module_platform_driver(amd_spi_driver);
285
286MODULE_LICENSE("Dual BSD/GPL");
287MODULE_AUTHOR("Sanjay Mehta <sanju.mehta@amd.com>");
288MODULE_DESCRIPTION("AMD SPI Master Controller Driver");