blob: f2dd8d432affce67662c4cea827e04fefd471354 [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
Lucas Tanure356b02f2021-09-10 12:15:27 +0100106static int amd_spi_busy_wait(struct amd_spi *amd_spi)
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500107{
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500108 int timeout = 100000;
109
110 /* poll for SPI bus to become idle */
Lucas Tanure356b02f2021-09-10 12:15:27 +0100111 while (amd_spi_readreg32(amd_spi, AMD_SPI_CTRL0_REG) & AMD_SPI_BUSY) {
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500112 usleep_range(10, 20);
113 if (timeout-- < 0)
114 return -ETIMEDOUT;
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500115 }
116
117 return 0;
118}
119
Lucas Tanureca8e8a12021-09-10 12:15:26 +0100120static void amd_spi_execute_opcode(struct amd_spi *amd_spi)
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500121{
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500122 /* Set ExecuteOpCode bit in the CTRL0 register */
Lucas Tanureca8e8a12021-09-10 12:15:26 +0100123 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 -0500124 amd_spi_busy_wait(amd_spi);
125}
126
127static int amd_spi_master_setup(struct spi_device *spi)
128{
Lucas Tanureca8e8a12021-09-10 12:15:26 +0100129 struct amd_spi *amd_spi = spi_master_get_devdata(spi->master);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500130
Lucas Tanureca8e8a12021-09-10 12:15:26 +0100131 amd_spi_clear_fifo_ptr(amd_spi);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500132
133 return 0;
134}
135
136static inline int amd_spi_fifo_xfer(struct amd_spi *amd_spi,
Lukas Wunner36c72a52020-05-04 13:12:05 +0200137 struct spi_master *master,
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500138 struct spi_message *message)
139{
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500140 struct spi_transfer *xfer = NULL;
Sanjay R Mehta68d047c2020-04-27 23:56:41 -0500141 u8 cmd_opcode;
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500142 u8 *buf = NULL;
143 u32 m_cmd = 0;
144 u32 i = 0;
145 u32 tx_len = 0, rx_len = 0;
146
147 list_for_each_entry(xfer, &message->transfers,
148 transfer_list) {
149 if (xfer->rx_buf)
150 m_cmd = AMD_SPI_XFER_RX;
151 if (xfer->tx_buf)
152 m_cmd = AMD_SPI_XFER_TX;
153
154 if (m_cmd & AMD_SPI_XFER_TX) {
155 buf = (u8 *)xfer->tx_buf;
156 tx_len = xfer->len - 1;
157 cmd_opcode = *(u8 *)xfer->tx_buf;
158 buf++;
Lucas Tanureca8e8a12021-09-10 12:15:26 +0100159 amd_spi_set_opcode(amd_spi, cmd_opcode);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500160
161 /* Write data into the FIFO. */
162 for (i = 0; i < tx_len; i++) {
Lucas Tanureca8e8a12021-09-10 12:15:26 +0100163 iowrite8(buf[i], ((u8 __iomem *)amd_spi->io_remap_addr +
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500164 AMD_SPI_FIFO_BASE + i));
165 }
166
Lucas Tanureca8e8a12021-09-10 12:15:26 +0100167 amd_spi_set_tx_count(amd_spi, tx_len);
168 amd_spi_clear_fifo_ptr(amd_spi);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500169 /* Execute command */
Lucas Tanureca8e8a12021-09-10 12:15:26 +0100170 amd_spi_execute_opcode(amd_spi);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500171 }
172 if (m_cmd & AMD_SPI_XFER_RX) {
173 /*
174 * Store no. of bytes to be received from
175 * FIFO
176 */
177 rx_len = xfer->len;
178 buf = (u8 *)xfer->rx_buf;
Lucas Tanureca8e8a12021-09-10 12:15:26 +0100179 amd_spi_set_rx_count(amd_spi, rx_len);
180 amd_spi_clear_fifo_ptr(amd_spi);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500181 /* Execute command */
Lucas Tanureca8e8a12021-09-10 12:15:26 +0100182 amd_spi_execute_opcode(amd_spi);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500183 /* Read data from FIFO to receive buffer */
184 for (i = 0; i < rx_len; i++)
Lucas Tanureca8e8a12021-09-10 12:15:26 +0100185 buf[i] = amd_spi_readreg8(amd_spi, AMD_SPI_FIFO_BASE + tx_len + i);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500186 }
187 }
188
189 /* Update statistics */
190 message->actual_length = tx_len + rx_len + 1;
191 /* complete the transaction */
192 message->status = 0;
193 spi_finalize_current_message(master);
194
195 return 0;
196}
197
198static int amd_spi_master_transfer(struct spi_master *master,
199 struct spi_message *msg)
200{
201 struct amd_spi *amd_spi = spi_master_get_devdata(master);
202 struct spi_device *spi = msg->spi;
203
204 amd_spi->chip_select = spi->chip_select;
Lucas Tanureca8e8a12021-09-10 12:15:26 +0100205 amd_spi_select_chip(amd_spi);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500206
207 /*
208 * Extract spi_transfers from the spi message and
209 * program the controller.
210 */
Lukas Wunner36c72a52020-05-04 13:12:05 +0200211 amd_spi_fifo_xfer(amd_spi, master, msg);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500212
213 return 0;
214}
215
216static int amd_spi_probe(struct platform_device *pdev)
217{
218 struct device *dev = &pdev->dev;
219 struct spi_master *master;
220 struct amd_spi *amd_spi;
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500221 int err = 0;
222
223 /* Allocate storage for spi_master and driver private data */
224 master = spi_alloc_master(dev, sizeof(struct amd_spi));
225 if (!master) {
226 dev_err(dev, "Error allocating SPI master\n");
227 return -ENOMEM;
228 }
229
230 amd_spi = spi_master_get_devdata(master);
Qing Zhang2ed6e3b2020-11-21 11:43:51 +0800231 amd_spi->io_remap_addr = devm_platform_ioremap_resource(pdev, 0);
Wei Yongjunf84b6042020-04-29 02:54:26 +0000232 if (IS_ERR(amd_spi->io_remap_addr)) {
233 err = PTR_ERR(amd_spi->io_remap_addr);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500234 dev_err(dev, "error %d ioremap of SPI registers failed\n", err);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500235 goto err_free_master;
236 }
237 dev_dbg(dev, "io_remap_address: %p\n", amd_spi->io_remap_addr);
238
239 /* Initialize the spi_master fields */
240 master->bus_num = 0;
241 master->num_chipselect = 4;
242 master->mode_bits = 0;
243 master->flags = SPI_MASTER_HALF_DUPLEX;
244 master->setup = amd_spi_master_setup;
245 master->transfer_one_message = amd_spi_master_transfer;
246
247 /* Register the controller with SPI framework */
Lukas Wunner7b9c94b2020-05-04 13:12:04 +0200248 err = devm_spi_register_master(dev, master);
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500249 if (err) {
250 dev_err(dev, "error %d registering SPI controller\n", err);
Lukas Wunner2b60c492020-05-04 13:12:01 +0200251 goto err_free_master;
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500252 }
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500253
254 return 0;
255
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500256err_free_master:
257 spi_master_put(master);
258
Lukas Wunnercc17fbe2020-05-04 13:12:02 +0200259 return err;
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500260}
261
Lee Jones85ed0f62020-07-17 14:54:24 +0100262#ifdef CONFIG_ACPI
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500263static const struct acpi_device_id spi_acpi_match[] = {
264 { "AMDI0061", 0 },
265 {},
266};
267MODULE_DEVICE_TABLE(acpi, spi_acpi_match);
Lee Jones85ed0f62020-07-17 14:54:24 +0100268#endif
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500269
270static struct platform_driver amd_spi_driver = {
271 .driver = {
272 .name = "amd_spi",
273 .acpi_match_table = ACPI_PTR(spi_acpi_match),
274 },
275 .probe = amd_spi_probe,
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500276};
277
278module_platform_driver(amd_spi_driver);
279
280MODULE_LICENSE("Dual BSD/GPL");
281MODULE_AUTHOR("Sanjay Mehta <sanju.mehta@amd.com>");
282MODULE_DESCRIPTION("AMD SPI Master Controller Driver");