blob: c5a16dd44fa3bc9d1d8740574dc2da0d810537f5 [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;
41 struct spi_master *master;
42 u8 chip_select;
43};
44
45static inline u8 amd_spi_readreg8(struct spi_master *master, int idx)
46{
47 struct amd_spi *amd_spi = spi_master_get_devdata(master);
48
49 return ioread8((u8 __iomem *)amd_spi->io_remap_addr + idx);
50}
51
52static inline void amd_spi_writereg8(struct spi_master *master, int idx,
53 u8 val)
54{
55 struct amd_spi *amd_spi = spi_master_get_devdata(master);
56
57 iowrite8(val, ((u8 __iomem *)amd_spi->io_remap_addr + idx));
58}
59
60static inline void amd_spi_setclear_reg8(struct spi_master *master, int idx,
61 u8 set, u8 clear)
62{
63 u8 tmp = amd_spi_readreg8(master, idx);
64
65 tmp = (tmp & ~clear) | set;
66 amd_spi_writereg8(master, idx, tmp);
67}
68
69static inline u32 amd_spi_readreg32(struct spi_master *master, int idx)
70{
71 struct amd_spi *amd_spi = spi_master_get_devdata(master);
72
73 return ioread32((u8 __iomem *)amd_spi->io_remap_addr + idx);
74}
75
76static inline void amd_spi_writereg32(struct spi_master *master, int idx,
77 u32 val)
78{
79 struct amd_spi *amd_spi = spi_master_get_devdata(master);
80
81 iowrite32(val, ((u8 __iomem *)amd_spi->io_remap_addr + idx));
82}
83
84static inline void amd_spi_setclear_reg32(struct spi_master *master, int idx,
85 u32 set, u32 clear)
86{
87 u32 tmp = amd_spi_readreg32(master, idx);
88
89 tmp = (tmp & ~clear) | set;
90 amd_spi_writereg32(master, idx, tmp);
91}
92
93static void amd_spi_select_chip(struct spi_master *master)
94{
95 struct amd_spi *amd_spi = spi_master_get_devdata(master);
96 u8 chip_select = amd_spi->chip_select;
97
98 amd_spi_setclear_reg8(master, AMD_SPI_ALT_CS_REG, chip_select,
99 AMD_SPI_ALT_CS_MASK);
100}
101
102static void amd_spi_clear_fifo_ptr(struct spi_master *master)
103{
104 amd_spi_setclear_reg32(master, AMD_SPI_CTRL0_REG, AMD_SPI_FIFO_CLEAR,
105 AMD_SPI_FIFO_CLEAR);
106}
107
108static void amd_spi_set_opcode(struct spi_master *master, u8 cmd_opcode)
109{
110 amd_spi_setclear_reg32(master, AMD_SPI_CTRL0_REG, cmd_opcode,
111 AMD_SPI_OPCODE_MASK);
112}
113
114static inline void amd_spi_set_rx_count(struct spi_master *master,
115 u8 rx_count)
116{
117 amd_spi_setclear_reg8(master, AMD_SPI_RX_COUNT_REG, rx_count, 0xff);
118}
119
120static inline void amd_spi_set_tx_count(struct spi_master *master,
121 u8 tx_count)
122{
123 amd_spi_setclear_reg8(master, AMD_SPI_TX_COUNT_REG, tx_count, 0xff);
124}
125
126static inline int amd_spi_busy_wait(struct amd_spi *amd_spi)
127{
128 bool spi_busy;
129 int timeout = 100000;
130
131 /* poll for SPI bus to become idle */
132 spi_busy = (ioread32((u8 __iomem *)amd_spi->io_remap_addr +
133 AMD_SPI_CTRL0_REG) & AMD_SPI_BUSY) == AMD_SPI_BUSY;
134 while (spi_busy) {
135 usleep_range(10, 20);
136 if (timeout-- < 0)
137 return -ETIMEDOUT;
138
139 spi_busy = (ioread32((u8 __iomem *)amd_spi->io_remap_addr +
140 AMD_SPI_CTRL0_REG) & AMD_SPI_BUSY) == AMD_SPI_BUSY;
141 }
142
143 return 0;
144}
145
146static void amd_spi_execute_opcode(struct spi_master *master)
147{
148 struct amd_spi *amd_spi = spi_master_get_devdata(master);
149
150 /* Set ExecuteOpCode bit in the CTRL0 register */
151 amd_spi_setclear_reg32(master, AMD_SPI_CTRL0_REG, AMD_SPI_EXEC_CMD,
152 AMD_SPI_EXEC_CMD);
153
154 amd_spi_busy_wait(amd_spi);
155}
156
157static int amd_spi_master_setup(struct spi_device *spi)
158{
159 struct spi_master *master = spi->master;
160
161 amd_spi_clear_fifo_ptr(master);
162
163 return 0;
164}
165
166static inline int amd_spi_fifo_xfer(struct amd_spi *amd_spi,
167 struct spi_message *message)
168{
169 struct spi_master *master = amd_spi->master;
170 struct spi_transfer *xfer = NULL;
Sanjay R Mehta68d047c2020-04-27 23:56:41 -0500171 u8 cmd_opcode;
Sanjay R Mehtabbb336f2020-04-25 14:59:48 -0500172 u8 *buf = NULL;
173 u32 m_cmd = 0;
174 u32 i = 0;
175 u32 tx_len = 0, rx_len = 0;
176
177 list_for_each_entry(xfer, &message->transfers,
178 transfer_list) {
179 if (xfer->rx_buf)
180 m_cmd = AMD_SPI_XFER_RX;
181 if (xfer->tx_buf)
182 m_cmd = AMD_SPI_XFER_TX;
183
184 if (m_cmd & AMD_SPI_XFER_TX) {
185 buf = (u8 *)xfer->tx_buf;
186 tx_len = xfer->len - 1;
187 cmd_opcode = *(u8 *)xfer->tx_buf;
188 buf++;
189 amd_spi_set_opcode(master, cmd_opcode);
190
191 /* Write data into the FIFO. */
192 for (i = 0; i < tx_len; i++) {
193 iowrite8(buf[i],
194 ((u8 __iomem *)amd_spi->io_remap_addr +
195 AMD_SPI_FIFO_BASE + i));
196 }
197
198 amd_spi_set_tx_count(master, tx_len);
199 amd_spi_clear_fifo_ptr(master);
200 /* Execute command */
201 amd_spi_execute_opcode(master);
202 }
203 if (m_cmd & AMD_SPI_XFER_RX) {
204 /*
205 * Store no. of bytes to be received from
206 * FIFO
207 */
208 rx_len = xfer->len;
209 buf = (u8 *)xfer->rx_buf;
210 amd_spi_set_rx_count(master, rx_len);
211 amd_spi_clear_fifo_ptr(master);
212 /* Execute command */
213 amd_spi_execute_opcode(master);
214 /* Read data from FIFO to receive buffer */
215 for (i = 0; i < rx_len; i++)
216 buf[i] = amd_spi_readreg8(master,
217 AMD_SPI_FIFO_BASE +
218 tx_len + i);
219 }
220 }
221
222 /* Update statistics */
223 message->actual_length = tx_len + rx_len + 1;
224 /* complete the transaction */
225 message->status = 0;
226 spi_finalize_current_message(master);
227
228 return 0;
229}
230
231static int amd_spi_master_transfer(struct spi_master *master,
232 struct spi_message *msg)
233{
234 struct amd_spi *amd_spi = spi_master_get_devdata(master);
235 struct spi_device *spi = msg->spi;
236
237 amd_spi->chip_select = spi->chip_select;
238 amd_spi_select_chip(master);
239
240 /*
241 * Extract spi_transfers from the spi message and
242 * program the controller.
243 */
244 amd_spi_fifo_xfer(amd_spi, msg);
245
246 return 0;
247}
248
249static int amd_spi_probe(struct platform_device *pdev)
250{
251 struct device *dev = &pdev->dev;
252 struct spi_master *master;
253 struct amd_spi *amd_spi;
254 struct resource *res;
255 int err = 0;
256
257 /* Allocate storage for spi_master and driver private data */
258 master = spi_alloc_master(dev, sizeof(struct amd_spi));
259 if (!master) {
260 dev_err(dev, "Error allocating SPI master\n");
261 return -ENOMEM;
262 }
263
264 amd_spi = spi_master_get_devdata(master);
265 amd_spi->master = master;
266
267 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
268 amd_spi->io_remap_addr = devm_ioremap_resource(&pdev->dev, res);
269
270 if (!amd_spi->io_remap_addr) {
271 dev_err(dev, "error %d ioremap of SPI registers failed\n", err);
272 err = -ENOMEM;
273 goto err_free_master;
274 }
275 dev_dbg(dev, "io_remap_address: %p\n", amd_spi->io_remap_addr);
276
277 /* Initialize the spi_master fields */
278 master->bus_num = 0;
279 master->num_chipselect = 4;
280 master->mode_bits = 0;
281 master->flags = SPI_MASTER_HALF_DUPLEX;
282 master->setup = amd_spi_master_setup;
283 master->transfer_one_message = amd_spi_master_transfer;
284
285 /* Register the controller with SPI framework */
286 err = spi_register_master(master);
287 if (err) {
288 dev_err(dev, "error %d registering SPI controller\n", err);
289 goto err_iounmap;
290 }
291 platform_set_drvdata(pdev, amd_spi);
292
293 return 0;
294
295err_iounmap:
296 iounmap(amd_spi->io_remap_addr);
297err_free_master:
298 spi_master_put(master);
299
300 return 0;
301}
302
303static int amd_spi_remove(struct platform_device *pdev)
304{
305 struct amd_spi *amd_spi = platform_get_drvdata(pdev);
306
307 spi_unregister_master(amd_spi->master);
308 spi_master_put(amd_spi->master);
309 platform_set_drvdata(pdev, NULL);
310
311 return 0;
312}
313
314static const struct acpi_device_id spi_acpi_match[] = {
315 { "AMDI0061", 0 },
316 {},
317};
318MODULE_DEVICE_TABLE(acpi, spi_acpi_match);
319
320static struct platform_driver amd_spi_driver = {
321 .driver = {
322 .name = "amd_spi",
323 .acpi_match_table = ACPI_PTR(spi_acpi_match),
324 },
325 .probe = amd_spi_probe,
326 .remove = amd_spi_remove,
327};
328
329module_platform_driver(amd_spi_driver);
330
331MODULE_LICENSE("Dual BSD/GPL");
332MODULE_AUTHOR("Sanjay Mehta <sanju.mehta@amd.com>");
333MODULE_DESCRIPTION("AMD SPI Master Controller Driver");