1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
3 // AMD SPI controller driver
5 // Copyright (c) 2020, Advanced Micro Devices, Inc.
7 // Author: Sanjay R Mehta <sanju.mehta@amd.com>
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>
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)
21 #define AMD_SPI_OPCODE_MASK 0xFF
23 #define AMD_SPI_ALT_CS_REG 0x1D
24 #define AMD_SPI_ALT_CS_MASK 0x3
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
31 #define AMD_SPI_MEM_SIZE 200
33 /* M_CMD OP codes for SPI */
34 #define AMD_SPI_XFER_TX 1
35 #define AMD_SPI_XFER_RX 2
38 void __iomem *io_remap_addr;
39 unsigned long io_base_addr;
44 static inline u8 amd_spi_readreg8(struct spi_master *master, int idx)
46 struct amd_spi *amd_spi = spi_master_get_devdata(master);
48 return ioread8((u8 __iomem *)amd_spi->io_remap_addr + idx);
51 static inline void amd_spi_writereg8(struct spi_master *master, int idx,
54 struct amd_spi *amd_spi = spi_master_get_devdata(master);
56 iowrite8(val, ((u8 __iomem *)amd_spi->io_remap_addr + idx));
59 static inline void amd_spi_setclear_reg8(struct spi_master *master, int idx,
62 u8 tmp = amd_spi_readreg8(master, idx);
64 tmp = (tmp & ~clear) | set;
65 amd_spi_writereg8(master, idx, tmp);
68 static inline u32 amd_spi_readreg32(struct spi_master *master, int idx)
70 struct amd_spi *amd_spi = spi_master_get_devdata(master);
72 return ioread32((u8 __iomem *)amd_spi->io_remap_addr + idx);
75 static inline void amd_spi_writereg32(struct spi_master *master, int idx,
78 struct amd_spi *amd_spi = spi_master_get_devdata(master);
80 iowrite32(val, ((u8 __iomem *)amd_spi->io_remap_addr + idx));
83 static inline void amd_spi_setclear_reg32(struct spi_master *master, int idx,
86 u32 tmp = amd_spi_readreg32(master, idx);
88 tmp = (tmp & ~clear) | set;
89 amd_spi_writereg32(master, idx, tmp);
92 static void amd_spi_select_chip(struct spi_master *master)
94 struct amd_spi *amd_spi = spi_master_get_devdata(master);
95 u8 chip_select = amd_spi->chip_select;
97 amd_spi_setclear_reg8(master, AMD_SPI_ALT_CS_REG, chip_select,
101 static void amd_spi_clear_fifo_ptr(struct spi_master *master)
103 amd_spi_setclear_reg32(master, AMD_SPI_CTRL0_REG, AMD_SPI_FIFO_CLEAR,
107 static void amd_spi_set_opcode(struct spi_master *master, u8 cmd_opcode)
109 amd_spi_setclear_reg32(master, AMD_SPI_CTRL0_REG, cmd_opcode,
110 AMD_SPI_OPCODE_MASK);
113 static inline void amd_spi_set_rx_count(struct spi_master *master,
116 amd_spi_setclear_reg8(master, AMD_SPI_RX_COUNT_REG, rx_count, 0xff);
119 static inline void amd_spi_set_tx_count(struct spi_master *master,
122 amd_spi_setclear_reg8(master, AMD_SPI_TX_COUNT_REG, tx_count, 0xff);
125 static inline int amd_spi_busy_wait(struct amd_spi *amd_spi)
128 int timeout = 100000;
130 /* poll for SPI bus to become idle */
131 spi_busy = (ioread32((u8 __iomem *)amd_spi->io_remap_addr +
132 AMD_SPI_CTRL0_REG) & AMD_SPI_BUSY) == AMD_SPI_BUSY;
134 usleep_range(10, 20);
138 spi_busy = (ioread32((u8 __iomem *)amd_spi->io_remap_addr +
139 AMD_SPI_CTRL0_REG) & AMD_SPI_BUSY) == AMD_SPI_BUSY;
145 static void amd_spi_execute_opcode(struct spi_master *master)
147 struct amd_spi *amd_spi = spi_master_get_devdata(master);
149 /* Set ExecuteOpCode bit in the CTRL0 register */
150 amd_spi_setclear_reg32(master, AMD_SPI_CTRL0_REG, AMD_SPI_EXEC_CMD,
153 amd_spi_busy_wait(amd_spi);
156 static int amd_spi_master_setup(struct spi_device *spi)
158 struct spi_master *master = spi->master;
160 amd_spi_clear_fifo_ptr(master);
165 static inline int amd_spi_fifo_xfer(struct amd_spi *amd_spi,
166 struct spi_master *master,
167 struct spi_message *message)
169 struct spi_transfer *xfer = NULL;
174 u32 tx_len = 0, rx_len = 0;
176 list_for_each_entry(xfer, &message->transfers,
179 m_cmd = AMD_SPI_XFER_RX;
181 m_cmd = AMD_SPI_XFER_TX;
183 if (m_cmd & AMD_SPI_XFER_TX) {
184 buf = (u8 *)xfer->tx_buf;
185 tx_len = xfer->len - 1;
186 cmd_opcode = *(u8 *)xfer->tx_buf;
188 amd_spi_set_opcode(master, cmd_opcode);
190 /* Write data into the FIFO. */
191 for (i = 0; i < tx_len; i++) {
193 ((u8 __iomem *)amd_spi->io_remap_addr +
194 AMD_SPI_FIFO_BASE + i));
197 amd_spi_set_tx_count(master, tx_len);
198 amd_spi_clear_fifo_ptr(master);
199 /* Execute command */
200 amd_spi_execute_opcode(master);
202 if (m_cmd & AMD_SPI_XFER_RX) {
204 * Store no. of bytes to be received from
208 buf = (u8 *)xfer->rx_buf;
209 amd_spi_set_rx_count(master, rx_len);
210 amd_spi_clear_fifo_ptr(master);
211 /* Execute command */
212 amd_spi_execute_opcode(master);
213 /* Read data from FIFO to receive buffer */
214 for (i = 0; i < rx_len; i++)
215 buf[i] = amd_spi_readreg8(master,
221 /* Update statistics */
222 message->actual_length = tx_len + rx_len + 1;
223 /* complete the transaction */
225 spi_finalize_current_message(master);
230 static int amd_spi_master_transfer(struct spi_master *master,
231 struct spi_message *msg)
233 struct amd_spi *amd_spi = spi_master_get_devdata(master);
234 struct spi_device *spi = msg->spi;
236 amd_spi->chip_select = spi->chip_select;
237 amd_spi_select_chip(master);
240 * Extract spi_transfers from the spi message and
241 * program the controller.
243 amd_spi_fifo_xfer(amd_spi, master, msg);
248 static int amd_spi_probe(struct platform_device *pdev)
250 struct device *dev = &pdev->dev;
251 struct spi_master *master;
252 struct amd_spi *amd_spi;
253 struct resource *res;
256 /* Allocate storage for spi_master and driver private data */
257 master = spi_alloc_master(dev, sizeof(struct amd_spi));
259 dev_err(dev, "Error allocating SPI master\n");
263 amd_spi = spi_master_get_devdata(master);
265 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
266 amd_spi->io_remap_addr = devm_ioremap_resource(&pdev->dev, res);
267 if (IS_ERR(amd_spi->io_remap_addr)) {
268 err = PTR_ERR(amd_spi->io_remap_addr);
269 dev_err(dev, "error %d ioremap of SPI registers failed\n", err);
270 goto err_free_master;
272 dev_dbg(dev, "io_remap_address: %p\n", amd_spi->io_remap_addr);
274 /* Initialize the spi_master fields */
276 master->num_chipselect = 4;
277 master->mode_bits = 0;
278 master->flags = SPI_MASTER_HALF_DUPLEX;
279 master->setup = amd_spi_master_setup;
280 master->transfer_one_message = amd_spi_master_transfer;
282 /* Register the controller with SPI framework */
283 err = devm_spi_register_master(dev, master);
285 dev_err(dev, "error %d registering SPI controller\n", err);
286 goto err_free_master;
292 spi_master_put(master);
298 static const struct acpi_device_id spi_acpi_match[] = {
302 MODULE_DEVICE_TABLE(acpi, spi_acpi_match);
305 static struct platform_driver amd_spi_driver = {
308 .acpi_match_table = ACPI_PTR(spi_acpi_match),
310 .probe = amd_spi_probe,
313 module_platform_driver(amd_spi_driver);
315 MODULE_LICENSE("Dual BSD/GPL");
316 MODULE_AUTHOR("Sanjay Mehta <sanju.mehta@amd.com>");
317 MODULE_DESCRIPTION("AMD SPI Master Controller Driver");