blob: 4b3ac7aceaf62ed39e0b8f1eb5ef516610570b57 [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};
42
Lucas Tanureca8e8a12021-09-10 12:15:26 +010043static inline u8 amd_spi_readreg8(struct amd_spi *amd_spi, int idx)
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -050044{
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -050045 return ioread8((u8 __iomem *)amd_spi->io_remap_addr + idx);
46}
47
Lucas Tanureca8e8a12021-09-10 12:15:26 +010048static inline void amd_spi_writereg8(struct amd_spi *amd_spi, int idx, u8 val)
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -050049{
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -050050 iowrite8(val, ((u8 __iomem *)amd_spi->io_remap_addr + idx));
51}
52
Lucas Tanureca8e8a12021-09-10 12:15:26 +010053static void amd_spi_setclear_reg8(struct amd_spi *amd_spi, int idx, u8 set, u8 clear)
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -050054{
Lucas Tanureca8e8a12021-09-10 12:15:26 +010055 u8 tmp = amd_spi_readreg8(amd_spi, idx);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -050056
57 tmp = (tmp & ~clear) | set;
Lucas Tanureca8e8a12021-09-10 12:15:26 +010058 amd_spi_writereg8(amd_spi, idx, tmp);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -050059}
60
Lucas Tanureca8e8a12021-09-10 12:15:26 +010061static inline u32 amd_spi_readreg32(struct amd_spi *amd_spi, int idx)
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -050062{
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -050063 return ioread32((u8 __iomem *)amd_spi->io_remap_addr + idx);
64}
65
Lucas Tanureca8e8a12021-09-10 12:15:26 +010066static inline void amd_spi_writereg32(struct amd_spi *amd_spi, int idx, u32 val)
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -050067{
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -050068 iowrite32(val, ((u8 __iomem *)amd_spi->io_remap_addr + idx));
69}
70
Lucas Tanureca8e8a12021-09-10 12:15:26 +010071static 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 -050072{
Lucas Tanureca8e8a12021-09-10 12:15:26 +010073 u32 tmp = amd_spi_readreg32(amd_spi, idx);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -050074
75 tmp = (tmp & ~clear) | set;
Lucas Tanureca8e8a12021-09-10 12:15:26 +010076 amd_spi_writereg32(amd_spi, idx, tmp);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -050077}
78
Lucas Tanure3b02d282021-09-10 12:15:28 +010079static void amd_spi_select_chip(struct amd_spi *amd_spi, u8 cs)
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -050080{
Lucas Tanure3b02d282021-09-10 12:15:28 +010081 amd_spi_setclear_reg8(amd_spi, AMD_SPI_ALT_CS_REG, cs, AMD_SPI_ALT_CS_MASK);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -050082}
83
Lucas Tanureca8e8a12021-09-10 12:15:26 +010084static void amd_spi_clear_fifo_ptr(struct amd_spi *amd_spi)
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -050085{
Lucas Tanureca8e8a12021-09-10 12:15:26 +010086 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 -050087}
88
Lucas Tanureca8e8a12021-09-10 12:15:26 +010089static void amd_spi_set_opcode(struct amd_spi *amd_spi, u8 cmd_opcode)
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -050090{
Lucas Tanureca8e8a12021-09-10 12:15:26 +010091 amd_spi_setclear_reg32(amd_spi, AMD_SPI_CTRL0_REG, cmd_opcode, AMD_SPI_OPCODE_MASK);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -050092}
93
Lucas Tanureca8e8a12021-09-10 12:15:26 +010094static inline void amd_spi_set_rx_count(struct amd_spi *amd_spi, u8 rx_count)
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -050095{
Lucas Tanureca8e8a12021-09-10 12:15:26 +010096 amd_spi_setclear_reg8(amd_spi, AMD_SPI_RX_COUNT_REG, rx_count, 0xff);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -050097}
98
Lucas Tanureca8e8a12021-09-10 12:15:26 +010099static inline void amd_spi_set_tx_count(struct amd_spi *amd_spi, u8 tx_count)
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500100{
Lucas Tanureca8e8a12021-09-10 12:15:26 +0100101 amd_spi_setclear_reg8(amd_spi, AMD_SPI_TX_COUNT_REG, tx_count, 0xff);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500102}
103
Lucas Tanure356b02f2021-09-10 12:15:27 +0100104static int amd_spi_busy_wait(struct amd_spi *amd_spi)
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500105{
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500106 int timeout = 100000;
107
108 /* poll for SPI bus to become idle */
Lucas Tanure356b02f2021-09-10 12:15:27 +0100109 while (amd_spi_readreg32(amd_spi, AMD_SPI_CTRL0_REG) & AMD_SPI_BUSY) {
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500110 usleep_range(10, 20);
111 if (timeout-- < 0)
112 return -ETIMEDOUT;
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500113 }
114
115 return 0;
116}
117
Lucas Tanure777a2cb2021-09-10 12:15:29 +0100118static int amd_spi_execute_opcode(struct amd_spi *amd_spi)
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500119{
Lucas Tanure777a2cb2021-09-10 12:15:29 +0100120 int ret;
121
122 ret = amd_spi_busy_wait(amd_spi);
123 if (ret)
124 return ret;
125
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500126 /* Set ExecuteOpCode bit in the CTRL0 register */
Lucas Tanureca8e8a12021-09-10 12:15:26 +0100127 amd_spi_setclear_reg32(amd_spi, AMD_SPI_CTRL0_REG, AMD_SPI_EXEC_CMD, AMD_SPI_EXEC_CMD);
Lucas Tanure777a2cb2021-09-10 12:15:29 +0100128
129 return 0;
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500130}
131
132static int amd_spi_master_setup(struct spi_device *spi)
133{
Lucas Tanureca8e8a12021-09-10 12:15:26 +0100134 struct amd_spi *amd_spi = spi_master_get_devdata(spi->master);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500135
Lucas Tanureca8e8a12021-09-10 12:15:26 +0100136 amd_spi_clear_fifo_ptr(amd_spi);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500137
138 return 0;
139}
140
141static inline int amd_spi_fifo_xfer(struct amd_spi *amd_spi,
Lukas Wunner36c72a52020-05-04 13:12:05 +0200142 struct spi_master *master,
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500143 struct spi_message *message)
144{
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500145 struct spi_transfer *xfer = NULL;
Sanjay R Mehta68d047c2020-04-27 23:56:41 -0500146 u8 cmd_opcode;
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500147 u8 *buf = NULL;
148 u32 m_cmd = 0;
149 u32 i = 0;
150 u32 tx_len = 0, rx_len = 0;
151
152 list_for_each_entry(xfer, &message->transfers,
153 transfer_list) {
154 if (xfer->rx_buf)
155 m_cmd = AMD_SPI_XFER_RX;
156 if (xfer->tx_buf)
157 m_cmd = AMD_SPI_XFER_TX;
158
159 if (m_cmd & AMD_SPI_XFER_TX) {
160 buf = (u8 *)xfer->tx_buf;
161 tx_len = xfer->len - 1;
162 cmd_opcode = *(u8 *)xfer->tx_buf;
163 buf++;
Lucas Tanureca8e8a12021-09-10 12:15:26 +0100164 amd_spi_set_opcode(amd_spi, cmd_opcode);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500165
166 /* Write data into the FIFO. */
167 for (i = 0; i < tx_len; i++) {
Lucas Tanureca8e8a12021-09-10 12:15:26 +0100168 iowrite8(buf[i], ((u8 __iomem *)amd_spi->io_remap_addr +
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500169 AMD_SPI_FIFO_BASE + i));
170 }
171
Lucas Tanureca8e8a12021-09-10 12:15:26 +0100172 amd_spi_set_tx_count(amd_spi, tx_len);
173 amd_spi_clear_fifo_ptr(amd_spi);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500174 /* Execute command */
Lucas Tanureca8e8a12021-09-10 12:15:26 +0100175 amd_spi_execute_opcode(amd_spi);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500176 }
177 if (m_cmd & AMD_SPI_XFER_RX) {
178 /*
179 * Store no. of bytes to be received from
180 * FIFO
181 */
182 rx_len = xfer->len;
183 buf = (u8 *)xfer->rx_buf;
Lucas Tanureca8e8a12021-09-10 12:15:26 +0100184 amd_spi_set_rx_count(amd_spi, rx_len);
185 amd_spi_clear_fifo_ptr(amd_spi);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500186 /* Execute command */
Lucas Tanureca8e8a12021-09-10 12:15:26 +0100187 amd_spi_execute_opcode(amd_spi);
Lucas Tanure777a2cb2021-09-10 12:15:29 +0100188 amd_spi_busy_wait(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
Lucas Tanure3b02d282021-09-10 12:15:28 +0100210 amd_spi_select_chip(amd_spi, spi->chip_select);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500211
212 /*
213 * Extract spi_transfers from the spi message and
214 * program the controller.
215 */
Lukas Wunner36c72a52020-05-04 13:12:05 +0200216 amd_spi_fifo_xfer(amd_spi, master, msg);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500217
218 return 0;
219}
220
221static int amd_spi_probe(struct platform_device *pdev)
222{
223 struct device *dev = &pdev->dev;
224 struct spi_master *master;
225 struct amd_spi *amd_spi;
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500226 int err = 0;
227
228 /* Allocate storage for spi_master and driver private data */
229 master = spi_alloc_master(dev, sizeof(struct amd_spi));
230 if (!master) {
231 dev_err(dev, "Error allocating SPI master\n");
232 return -ENOMEM;
233 }
234
235 amd_spi = spi_master_get_devdata(master);
Qing Zhang2ed6e3b2020-11-21 11:43:51 +0800236 amd_spi->io_remap_addr = devm_platform_ioremap_resource(pdev, 0);
Wei Yongjunf84b6042020-04-29 02:54:26 +0000237 if (IS_ERR(amd_spi->io_remap_addr)) {
238 err = PTR_ERR(amd_spi->io_remap_addr);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500239 dev_err(dev, "error %d ioremap of SPI registers failed\n", err);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500240 goto err_free_master;
241 }
242 dev_dbg(dev, "io_remap_address: %p\n", amd_spi->io_remap_addr);
243
244 /* Initialize the spi_master fields */
245 master->bus_num = 0;
246 master->num_chipselect = 4;
247 master->mode_bits = 0;
248 master->flags = SPI_MASTER_HALF_DUPLEX;
249 master->setup = amd_spi_master_setup;
250 master->transfer_one_message = amd_spi_master_transfer;
251
252 /* Register the controller with SPI framework */
Lukas Wunner7b9c94b2020-05-04 13:12:04 +0200253 err = devm_spi_register_master(dev, master);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500254 if (err) {
255 dev_err(dev, "error %d registering SPI controller\n", err);
Lukas Wunner2b60c492020-05-04 13:12:01 +0200256 goto err_free_master;
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500257 }
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500258
259 return 0;
260
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500261err_free_master:
262 spi_master_put(master);
263
Lukas Wunnercc17fbe2020-05-04 13:12:02 +0200264 return err;
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500265}
266
Lee Jones85ed0f62020-07-17 14:54:24 +0100267#ifdef CONFIG_ACPI
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500268static const struct acpi_device_id spi_acpi_match[] = {
269 { "AMDI0061", 0 },
270 {},
271};
272MODULE_DEVICE_TABLE(acpi, spi_acpi_match);
Lee Jones85ed0f62020-07-17 14:54:24 +0100273#endif
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500274
275static struct platform_driver amd_spi_driver = {
276 .driver = {
277 .name = "amd_spi",
278 .acpi_match_table = ACPI_PTR(spi_acpi_match),
279 },
280 .probe = amd_spi_probe,
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500281};
282
283module_platform_driver(amd_spi_driver);
284
285MODULE_LICENSE("Dual BSD/GPL");
286MODULE_AUTHOR("Sanjay Mehta <sanju.mehta@amd.com>");
287MODULE_DESCRIPTION("AMD SPI Master Controller Driver");