xref: /linux/drivers/spi/spi-amd.c (revision f16d85cd3de9d0c761b74155b4cbe2f20d3dcf80)
1 // 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/delay.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/init.h>
13 #include <linux/io-64-nonatomic-lo-hi.h>
14 #include <linux/iopoll.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/spi/spi.h>
18 #include <linux/spi/spi-mem.h>
19 
20 #include "spi-amd.h"
21 
22 #define AMD_SPI_CTRL0_REG	0x00
23 #define AMD_SPI_EXEC_CMD	BIT(16)
24 #define AMD_SPI_FIFO_CLEAR	BIT(20)
25 #define AMD_SPI_BUSY		BIT(31)
26 
27 #define AMD_SPI_OPCODE_REG	0x45
28 #define AMD_SPI_CMD_TRIGGER_REG	0x47
29 #define AMD_SPI_TRIGGER_CMD	BIT(7)
30 
31 #define AMD_SPI_OPCODE_MASK	0xFF
32 
33 #define AMD_SPI_ALT_CS_REG	0x1D
34 #define AMD_SPI_ALT_CS_MASK	0x3
35 
36 #define AMD_SPI_FIFO_BASE	0x80
37 #define AMD_SPI_TX_COUNT_REG	0x48
38 #define AMD_SPI_RX_COUNT_REG	0x4B
39 #define AMD_SPI_STATUS_REG	0x4C
40 #define AMD_SPI_ADDR32CTRL_REG	0x50
41 
42 #define AMD_SPI_FIFO_SIZE	70
43 #define AMD_SPI_MEM_SIZE	200
44 #define AMD_SPI_MAX_DATA	64
45 #define AMD_SPI_HID2_DMA_SIZE   4096
46 
47 #define AMD_SPI_ENA_REG		0x20
48 #define AMD_SPI_ALT_SPD_SHIFT	20
49 #define AMD_SPI_ALT_SPD_MASK	GENMASK(23, AMD_SPI_ALT_SPD_SHIFT)
50 #define AMD_SPI_SPI100_SHIFT	0
51 #define AMD_SPI_SPI100_MASK	GENMASK(AMD_SPI_SPI100_SHIFT, AMD_SPI_SPI100_SHIFT)
52 #define AMD_SPI_SPEED_REG	0x6C
53 #define AMD_SPI_SPD7_SHIFT	8
54 #define AMD_SPI_SPD7_MASK	GENMASK(13, AMD_SPI_SPD7_SHIFT)
55 
56 #define AMD_SPI_HID2_INPUT_RING_BUF0	0X100
57 #define AMD_SPI_HID2_OUTPUT_BUF0	0x140
58 #define AMD_SPI_HID2_CNTRL		0x150
59 #define AMD_SPI_HID2_INT_STATUS		0x154
60 #define AMD_SPI_HID2_CMD_START		0x156
61 #define AMD_SPI_HID2_INT_MASK		0x158
62 #define AMD_SPI_HID2_WRITE_CNTRL0	0x160
63 #define AMD_SPI_HID2_WRITE_CNTRL1	0x164
64 #define AMD_SPI_HID2_READ_CNTRL0	0x170
65 #define AMD_SPI_HID2_READ_CNTRL1	0x174
66 #define AMD_SPI_HID2_READ_CNTRL2	0x180
67 
68 #define AMD_SPI_MAX_HZ		100000000
69 #define AMD_SPI_MIN_HZ		800000
70 
71 #define AMD_SPI_IO_SLEEP_US	20
72 #define AMD_SPI_IO_TIMEOUT_US	2000000
73 
74 /* SPI read command opcodes */
75 #define AMD_SPI_OP_READ          0x03	/* Read data bytes (low frequency) */
76 #define AMD_SPI_OP_READ_FAST     0x0b	/* Read data bytes (high frequency) */
77 #define AMD_SPI_OP_READ_1_1_2    0x3b	/* Read data bytes (Dual Output SPI) */
78 #define AMD_SPI_OP_READ_1_2_2    0xbb	/* Read data bytes (Dual I/O SPI) */
79 #define AMD_SPI_OP_READ_1_1_4    0x6b	/* Read data bytes (Quad Output SPI) */
80 #define AMD_SPI_OP_READ_1_4_4    0xeb	/* Read data bytes (Quad I/O SPI) */
81 
82 /* SPI read command opcodes - 4B address */
83 #define AMD_SPI_OP_READ_FAST_4B		0x0c    /* Read data bytes (high frequency) */
84 #define AMD_SPI_OP_READ_1_1_2_4B	0x3c    /* Read data bytes (Dual Output SPI) */
85 #define AMD_SPI_OP_READ_1_2_2_4B	0xbc    /* Read data bytes (Dual I/O SPI) */
86 #define AMD_SPI_OP_READ_1_1_4_4B	0x6c    /* Read data bytes (Quad Output SPI) */
87 #define AMD_SPI_OP_READ_1_4_4_4B	0xec    /* Read data bytes (Quad I/O SPI) */
88 
89 /* SPINAND write command opcodes */
90 #define AMD_SPI_OP_PP			0x02	/* Page program */
91 #define AMD_SPI_OP_PP_RANDOM		0x84	/* Page program */
92 
93 enum amd_spi_speed {
94 	F_66_66MHz,
95 	F_33_33MHz,
96 	F_22_22MHz,
97 	F_16_66MHz,
98 	F_100MHz,
99 	F_800KHz,
100 	SPI_SPD7 = 0x7,
101 	F_50MHz = 0x4,
102 	F_4MHz = 0x32,
103 	F_3_17MHz = 0x3F
104 };
105 
106 /**
107  * struct amd_spi_freq - Matches device speed with values to write in regs
108  * @speed_hz: Device frequency
109  * @enable_val: Value to be written to "enable register"
110  * @spd7_val: Some frequencies requires to have a value written at SPISPEED register
111  */
112 struct amd_spi_freq {
113 	u32 speed_hz;
114 	u32 enable_val;
115 	u32 spd7_val;
116 };
117 
118 static inline u8 amd_spi_readreg8(struct amd_spi *amd_spi, int idx)
119 {
120 	return readb((u8 __iomem *)amd_spi->io_remap_addr + idx);
121 }
122 
123 static inline void amd_spi_writereg8(struct amd_spi *amd_spi, int idx, u8 val)
124 {
125 	writeb(val, ((u8 __iomem *)amd_spi->io_remap_addr + idx));
126 }
127 
128 static void amd_spi_setclear_reg8(struct amd_spi *amd_spi, int idx, u8 set, u8 clear)
129 {
130 	u8 tmp = amd_spi_readreg8(amd_spi, idx);
131 
132 	tmp = (tmp & ~clear) | set;
133 	amd_spi_writereg8(amd_spi, idx, tmp);
134 }
135 
136 static inline u16 amd_spi_readreg16(struct amd_spi *amd_spi, int idx)
137 {
138 	return readw((u8 __iomem *)amd_spi->io_remap_addr + idx);
139 }
140 
141 static inline void amd_spi_writereg16(struct amd_spi *amd_spi, int idx, u16 val)
142 {
143 	writew(val, ((u8 __iomem *)amd_spi->io_remap_addr + idx));
144 }
145 
146 static inline u32 amd_spi_readreg32(struct amd_spi *amd_spi, int idx)
147 {
148 	return readl((u8 __iomem *)amd_spi->io_remap_addr + idx);
149 }
150 
151 static inline void amd_spi_writereg32(struct amd_spi *amd_spi, int idx, u32 val)
152 {
153 	writel(val, ((u8 __iomem *)amd_spi->io_remap_addr + idx));
154 }
155 
156 static inline u64 amd_spi_readreg64(struct amd_spi *amd_spi, int idx)
157 {
158 	return readq((u8 __iomem *)amd_spi->io_remap_addr + idx);
159 }
160 
161 static inline void amd_spi_writereg64(struct amd_spi *amd_spi, int idx, u64 val)
162 {
163 	writeq(val, ((u8 __iomem *)amd_spi->io_remap_addr + idx));
164 }
165 
166 static inline void amd_spi_setclear_reg32(struct amd_spi *amd_spi, int idx, u32 set, u32 clear)
167 {
168 	u32 tmp = amd_spi_readreg32(amd_spi, idx);
169 
170 	tmp = (tmp & ~clear) | set;
171 	amd_spi_writereg32(amd_spi, idx, tmp);
172 }
173 
174 static void amd_spi_select_chip(struct amd_spi *amd_spi, u8 cs)
175 {
176 	amd_spi_setclear_reg8(amd_spi, AMD_SPI_ALT_CS_REG, cs, AMD_SPI_ALT_CS_MASK);
177 }
178 
179 static inline void amd_spi_clear_chip(struct amd_spi *amd_spi, u8 chip_select)
180 {
181 	amd_spi_writereg8(amd_spi, AMD_SPI_ALT_CS_REG, chip_select & ~AMD_SPI_ALT_CS_MASK);
182 }
183 
184 static void amd_spi_clear_fifo_ptr(struct amd_spi *amd_spi)
185 {
186 	amd_spi_setclear_reg32(amd_spi, AMD_SPI_CTRL0_REG, AMD_SPI_FIFO_CLEAR, AMD_SPI_FIFO_CLEAR);
187 }
188 
189 static int amd_spi_set_opcode(struct amd_spi *amd_spi, u8 cmd_opcode)
190 {
191 	switch (amd_spi->version) {
192 	case AMD_SPI_V1:
193 		amd_spi_setclear_reg32(amd_spi, AMD_SPI_CTRL0_REG, cmd_opcode,
194 				       AMD_SPI_OPCODE_MASK);
195 		return 0;
196 	case AMD_SPI_V2:
197 	case AMD_HID2_SPI:
198 		amd_spi_writereg8(amd_spi, AMD_SPI_OPCODE_REG, cmd_opcode);
199 		return 0;
200 	default:
201 		return -ENODEV;
202 	}
203 }
204 
205 static inline void amd_spi_set_rx_count(struct amd_spi *amd_spi, u8 rx_count)
206 {
207 	amd_spi_writereg8(amd_spi, AMD_SPI_RX_COUNT_REG, rx_count);
208 }
209 
210 static inline void amd_spi_set_tx_count(struct amd_spi *amd_spi, u8 tx_count)
211 {
212 	amd_spi_writereg8(amd_spi, AMD_SPI_TX_COUNT_REG, tx_count);
213 }
214 
215 static int amd_spi_busy_wait(struct amd_spi *amd_spi)
216 {
217 	u32 val;
218 	int reg;
219 
220 	switch (amd_spi->version) {
221 	case AMD_SPI_V1:
222 		reg = AMD_SPI_CTRL0_REG;
223 		break;
224 	case AMD_SPI_V2:
225 	case AMD_HID2_SPI:
226 		reg = AMD_SPI_STATUS_REG;
227 		break;
228 	default:
229 		return -ENODEV;
230 	}
231 
232 	return readl_poll_timeout(amd_spi->io_remap_addr + reg, val,
233 				  !(val & AMD_SPI_BUSY), 20, 2000000);
234 }
235 
236 static int amd_spi_execute_opcode(struct amd_spi *amd_spi)
237 {
238 	int ret;
239 
240 	ret = amd_spi_busy_wait(amd_spi);
241 	if (ret)
242 		return ret;
243 
244 	switch (amd_spi->version) {
245 	case AMD_SPI_V1:
246 		/* Set ExecuteOpCode bit in the CTRL0 register */
247 		amd_spi_setclear_reg32(amd_spi, AMD_SPI_CTRL0_REG, AMD_SPI_EXEC_CMD,
248 				       AMD_SPI_EXEC_CMD);
249 		return 0;
250 	case AMD_SPI_V2:
251 	case AMD_HID2_SPI:
252 		/* Trigger the command execution */
253 		amd_spi_setclear_reg8(amd_spi, AMD_SPI_CMD_TRIGGER_REG,
254 				      AMD_SPI_TRIGGER_CMD, AMD_SPI_TRIGGER_CMD);
255 		return 0;
256 	default:
257 		return -ENODEV;
258 	}
259 }
260 
261 static int amd_spi_host_setup(struct spi_device *spi)
262 {
263 	struct amd_spi *amd_spi = spi_controller_get_devdata(spi->controller);
264 
265 	amd_spi_clear_fifo_ptr(amd_spi);
266 
267 	return 0;
268 }
269 
270 static const struct amd_spi_freq amd_spi_freq[] = {
271 	{ AMD_SPI_MAX_HZ,   F_100MHz,         0},
272 	{       66660000, F_66_66MHz,         0},
273 	{       50000000,   SPI_SPD7,   F_50MHz},
274 	{       33330000, F_33_33MHz,         0},
275 	{       22220000, F_22_22MHz,         0},
276 	{       16660000, F_16_66MHz,         0},
277 	{        4000000,   SPI_SPD7,    F_4MHz},
278 	{        3170000,   SPI_SPD7, F_3_17MHz},
279 	{ AMD_SPI_MIN_HZ,   F_800KHz,         0},
280 };
281 
282 static void amd_set_spi_freq(struct amd_spi *amd_spi, u32 speed_hz)
283 {
284 	unsigned int i, spd7_val, alt_spd;
285 
286 	for (i = 0; i < ARRAY_SIZE(amd_spi_freq)-1; i++)
287 		if (speed_hz >= amd_spi_freq[i].speed_hz)
288 			break;
289 
290 	if (amd_spi->speed_hz == amd_spi_freq[i].speed_hz)
291 		return;
292 
293 	amd_spi->speed_hz = amd_spi_freq[i].speed_hz;
294 
295 	alt_spd = (amd_spi_freq[i].enable_val << AMD_SPI_ALT_SPD_SHIFT)
296 		   & AMD_SPI_ALT_SPD_MASK;
297 	amd_spi_setclear_reg32(amd_spi, AMD_SPI_ENA_REG, alt_spd,
298 			       AMD_SPI_ALT_SPD_MASK);
299 
300 	if (amd_spi->speed_hz == AMD_SPI_MAX_HZ)
301 		amd_spi_setclear_reg32(amd_spi, AMD_SPI_ENA_REG, 1,
302 				       AMD_SPI_SPI100_MASK);
303 
304 	if (amd_spi_freq[i].spd7_val) {
305 		spd7_val = (amd_spi_freq[i].spd7_val << AMD_SPI_SPD7_SHIFT)
306 			    & AMD_SPI_SPD7_MASK;
307 		amd_spi_setclear_reg32(amd_spi, AMD_SPI_SPEED_REG, spd7_val,
308 				       AMD_SPI_SPD7_MASK);
309 	}
310 }
311 
312 static inline int amd_spi_fifo_xfer(struct amd_spi *amd_spi,
313 				    struct spi_controller *host,
314 				    struct spi_message *message)
315 {
316 	struct spi_transfer *xfer = NULL;
317 	struct spi_device *spi = message->spi;
318 	u8 cmd_opcode = 0, fifo_pos = AMD_SPI_FIFO_BASE;
319 	u8 *buf = NULL;
320 	u32 i = 0;
321 	u32 tx_len = 0, rx_len = 0;
322 
323 	list_for_each_entry(xfer, &message->transfers,
324 			    transfer_list) {
325 		/* Skip zero-length transfers to avoid len underflow below */
326 		if (!xfer->len)
327 			continue;
328 
329 		if (xfer->speed_hz)
330 			amd_set_spi_freq(amd_spi, xfer->speed_hz);
331 		else
332 			amd_set_spi_freq(amd_spi, spi->max_speed_hz);
333 
334 		if (xfer->tx_buf) {
335 			buf = (u8 *)xfer->tx_buf;
336 			if (!tx_len) {
337 				cmd_opcode = *(u8 *)xfer->tx_buf;
338 				buf++;
339 				xfer->len--;
340 			}
341 			tx_len += xfer->len;
342 			if (tx_len + rx_len > AMD_SPI_FIFO_SIZE) {
343 				message->status = -EMSGSIZE;
344 				goto fin_msg;
345 			}
346 
347 			/* Write data into the FIFO. */
348 			for (i = 0; i < xfer->len; i++)
349 				amd_spi_writereg8(amd_spi, fifo_pos + i, buf[i]);
350 
351 			fifo_pos += xfer->len;
352 		}
353 
354 		/* Store no. of bytes to be received from FIFO */
355 		if (xfer->rx_buf) {
356 			rx_len += xfer->len;
357 			if (tx_len + rx_len > AMD_SPI_FIFO_SIZE) {
358 				message->status = -EMSGSIZE;
359 				goto fin_msg;
360 			}
361 		}
362 	}
363 
364 	if (!buf) {
365 		message->status = -EINVAL;
366 		goto fin_msg;
367 	}
368 
369 	amd_spi_set_opcode(amd_spi, cmd_opcode);
370 	amd_spi_set_tx_count(amd_spi, tx_len);
371 	amd_spi_set_rx_count(amd_spi, rx_len);
372 
373 	/* Execute command */
374 	message->status = amd_spi_execute_opcode(amd_spi);
375 	if (message->status)
376 		goto fin_msg;
377 
378 	if (rx_len) {
379 		message->status = amd_spi_busy_wait(amd_spi);
380 		if (message->status)
381 			goto fin_msg;
382 
383 		list_for_each_entry(xfer, &message->transfers, transfer_list)
384 			if (xfer->rx_buf) {
385 				buf = (u8 *)xfer->rx_buf;
386 				/* Read data from FIFO to receive buffer */
387 				for (i = 0; i < xfer->len; i++)
388 					buf[i] = amd_spi_readreg8(amd_spi, fifo_pos + i);
389 				fifo_pos += xfer->len;
390 			}
391 	}
392 
393 	/* Update statistics */
394 	message->actual_length = tx_len + rx_len + 1;
395 
396 fin_msg:
397 	switch (amd_spi->version) {
398 	case AMD_SPI_V1:
399 		break;
400 	case AMD_SPI_V2:
401 	case AMD_HID2_SPI:
402 		amd_spi_clear_chip(amd_spi, spi_get_chipselect(message->spi, 0));
403 		break;
404 	default:
405 		return -ENODEV;
406 	}
407 
408 	spi_finalize_current_message(host);
409 
410 	return message->status;
411 }
412 
413 static inline bool amd_is_spi_read_cmd_4b(const u16 op)
414 {
415 	switch (op) {
416 	case AMD_SPI_OP_READ_FAST_4B:
417 	case AMD_SPI_OP_READ_1_1_2_4B:
418 	case AMD_SPI_OP_READ_1_2_2_4B:
419 	case AMD_SPI_OP_READ_1_1_4_4B:
420 	case AMD_SPI_OP_READ_1_4_4_4B:
421 		return true;
422 	default:
423 		return false;
424 	}
425 }
426 
427 static inline bool amd_is_spi_read_cmd(const u16 op)
428 {
429 	switch (op) {
430 	case AMD_SPI_OP_READ:
431 	case AMD_SPI_OP_READ_FAST:
432 	case AMD_SPI_OP_READ_1_1_2:
433 	case AMD_SPI_OP_READ_1_2_2:
434 	case AMD_SPI_OP_READ_1_1_4:
435 	case AMD_SPI_OP_READ_1_4_4:
436 		return true;
437 	default:
438 		return amd_is_spi_read_cmd_4b(op);
439 	}
440 }
441 
442 static inline bool amd_is_spi_write_cmd(const u16 op)
443 {
444 	switch (op) {
445 	case AMD_SPI_OP_PP:
446 	case AMD_SPI_OP_PP_RANDOM:
447 		return true;
448 	default:
449 		return false;
450 	}
451 }
452 
453 static bool amd_spi_supports_op(struct spi_mem *mem,
454 				const struct spi_mem_op *op)
455 {
456 	struct amd_spi *amd_spi = spi_controller_get_devdata(mem->spi->controller);
457 
458 	/* bus width is number of IO lines used to transmit */
459 	if (op->cmd.buswidth > 1 || op->addr.buswidth > 4)
460 		return false;
461 
462 	/* AMD SPI controllers support quad mode only for read operations */
463 	if (amd_is_spi_read_cmd(op->cmd.opcode) || amd_is_spi_write_cmd(op->cmd.opcode)) {
464 		if (op->data.buswidth > 4)
465 			return false;
466 
467 		/*
468 		 * HID2 SPI controller supports DMA read up to 4K bytes and
469 		 * doesn't support 4-byte address commands.
470 		 */
471 		if (amd_spi->version == AMD_HID2_SPI) {
472 			if ((amd_is_spi_read_cmd_4b(op->cmd.opcode) ||
473 			     amd_is_spi_write_cmd(op->cmd.opcode)) &&
474 			    op->data.nbytes > AMD_SPI_HID2_DMA_SIZE)
475 				return false;
476 		} else if (op->data.nbytes > AMD_SPI_MAX_DATA) {
477 			return false;
478 		}
479 	} else if (op->data.buswidth > 1 || op->data.nbytes > AMD_SPI_MAX_DATA) {
480 		return false;
481 	}
482 
483 	if (op->max_freq < mem->spi->controller->min_speed_hz)
484 		return false;
485 
486 	return spi_mem_default_supports_op(mem, op);
487 }
488 
489 static int amd_spi_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op)
490 {
491 	struct amd_spi *amd_spi = spi_controller_get_devdata(mem->spi->controller);
492 
493 	/*
494 	 * HID2 SPI controller DMA read mode supports reading up to 4k
495 	 * bytes in single transaction, where as SPI0 and HID2 SPI
496 	 * controller index mode supports maximum of 64 bytes in a single
497 	 * transaction.
498 	 */
499 	if (amd_spi->version == AMD_HID2_SPI && (amd_is_spi_read_cmd(op->cmd.opcode) ||
500 						 amd_is_spi_write_cmd(op->cmd.opcode)))
501 		op->data.nbytes = clamp_val(op->data.nbytes, 0, AMD_SPI_HID2_DMA_SIZE);
502 	else
503 		op->data.nbytes = clamp_val(op->data.nbytes, 0, AMD_SPI_MAX_DATA);
504 
505 	return 0;
506 }
507 
508 static void amd_spi_set_addr(struct amd_spi *amd_spi,
509 			     const struct spi_mem_op *op)
510 {
511 	u8 nbytes = op->addr.nbytes;
512 	u64 addr_val = op->addr.val;
513 	int base_addr, i;
514 
515 	base_addr = AMD_SPI_FIFO_BASE + nbytes;
516 
517 	for (i = 0; i < nbytes; i++) {
518 		amd_spi_writereg8(amd_spi, base_addr - i - 1, addr_val &
519 				  GENMASK(7, 0));
520 		addr_val >>= 8;
521 	}
522 }
523 
524 static void amd_spi_hiddma_write(struct amd_spi *amd_spi, const struct spi_mem_op *op)
525 {
526 	u16 hid_cmd_start, val;
527 	u32 hid_regval;
528 
529 	/*
530 	 * Program the HID2 output Buffer0. 4k aligned buf_memory_addr[31:12],
531 	 * buf_size[2:0].
532 	 */
533 	hid_regval = amd_spi->phy_dma_buf | BIT(0);
534 	amd_spi_writereg32(amd_spi, AMD_SPI_HID2_OUTPUT_BUF0, hid_regval);
535 
536 	/* Program max write length in hid2_write_control1 register */
537 	hid_regval = amd_spi_readreg32(amd_spi, AMD_SPI_HID2_WRITE_CNTRL1);
538 	hid_regval = (hid_regval & ~GENMASK(15, 0)) | ((op->data.nbytes) + 3);
539 	amd_spi_writereg32(amd_spi, AMD_SPI_HID2_WRITE_CNTRL1, hid_regval);
540 
541 	/* Set cmd start bit in hid2_cmd_start register to trigger HID basic write operation */
542 	hid_cmd_start = amd_spi_readreg16(amd_spi, AMD_SPI_HID2_CMD_START);
543 	amd_spi_writereg16(amd_spi, AMD_SPI_HID2_CMD_START, (hid_cmd_start | BIT(2)));
544 
545 	/* Check interrupt status of HIDDMA basic write operation in hid2_int_status register */
546 	readw_poll_timeout(amd_spi->io_remap_addr + AMD_SPI_HID2_INT_STATUS, val,
547 			   (val & BIT(2)), AMD_SPI_IO_SLEEP_US, AMD_SPI_IO_TIMEOUT_US);
548 
549 	/* Clear the interrupts by writing to hid2_int_status register */
550 	val = amd_spi_readreg16(amd_spi, AMD_SPI_HID2_INT_STATUS);
551 	amd_spi_writereg16(amd_spi, AMD_SPI_HID2_INT_STATUS, val);
552 }
553 
554 static void amd_spi_mem_data_out(struct amd_spi *amd_spi,
555 				 const struct spi_mem_op *op)
556 {
557 	int base_addr = AMD_SPI_FIFO_BASE + op->addr.nbytes;
558 	u64 *buf_64 = (u64 *)op->data.buf.out;
559 	u64 addr_val = op->addr.val;
560 	u32 nbytes = op->data.nbytes;
561 	u32 left_data = nbytes;
562 	u8 *buf;
563 	int i;
564 
565 	/*
566 	 * Condition for using HID write mode. Only for writing complete page data, use HID write.
567 	 * Use index mode otherwise.
568 	 */
569 	if (amd_spi->version == AMD_HID2_SPI && amd_is_spi_write_cmd(op->cmd.opcode)) {
570 		u64 *dma_buf64 = (u64 *)(amd_spi->dma_virt_addr + op->addr.nbytes + op->cmd.nbytes);
571 		u8 *dma_buf = (u8 *)amd_spi->dma_virt_addr;
572 
573 		/* Copy opcode and address to DMA buffer */
574 		*dma_buf = op->cmd.opcode;
575 
576 		dma_buf = (u8 *)dma_buf64;
577 		for (i = 0; i < op->addr.nbytes; i++) {
578 			*--dma_buf = addr_val & GENMASK(7, 0);
579 			addr_val >>= 8;
580 		}
581 
582 		/* Copy data to DMA buffer */
583 		while (left_data >= 8) {
584 			*dma_buf64++ = *buf_64++;
585 			left_data -= 8;
586 		}
587 
588 		buf = (u8 *)buf_64;
589 		dma_buf = (u8 *)dma_buf64;
590 		while (left_data--)
591 			*dma_buf++ = *buf++;
592 
593 		amd_spi_hiddma_write(amd_spi, op);
594 	} else {
595 		amd_spi_set_opcode(amd_spi, op->cmd.opcode);
596 		amd_spi_set_addr(amd_spi, op);
597 
598 		for (i = 0; left_data >= 8; i++, left_data -= 8)
599 			amd_spi_writereg64(amd_spi, base_addr + op->dummy.nbytes + (i * 8),
600 					   *buf_64++);
601 
602 		buf = (u8 *)buf_64;
603 		for (i = 0; i < left_data; i++) {
604 			amd_spi_writereg8(amd_spi,
605 					  base_addr + op->dummy.nbytes + nbytes + i - left_data,
606 					  buf[i]);
607 		}
608 
609 		amd_spi_set_tx_count(amd_spi, op->addr.nbytes + op->data.nbytes);
610 		amd_spi_set_rx_count(amd_spi, 0);
611 		amd_spi_clear_fifo_ptr(amd_spi);
612 		amd_spi_execute_opcode(amd_spi);
613 	}
614 }
615 
616 static void amd_spi_hiddma_read(struct amd_spi *amd_spi, const struct spi_mem_op *op)
617 {
618 	u16 hid_cmd_start, val;
619 	u32 hid_regval;
620 
621 	/* Set the opcode in hid2_read_control0 register */
622 	hid_regval = amd_spi_readreg32(amd_spi, AMD_SPI_HID2_READ_CNTRL0);
623 	hid_regval = (hid_regval & ~GENMASK(7, 0)) | op->cmd.opcode;
624 
625 	/*
626 	 * Program the address in the hid2_read_control0 register [8:31]. The address should
627 	 * be written starting from the 8th bit of the register, requiring an 8-bit shift.
628 	 * Additionally, to convert a 2-byte spinand address to a 3-byte address, another
629 	 * 8-bit shift is needed. Therefore, a total shift of 16 bits is required.
630 	 */
631 	hid_regval = (hid_regval & ~GENMASK(31, 8)) | (op->addr.val << 16);
632 	amd_spi_writereg32(amd_spi, AMD_SPI_HID2_READ_CNTRL0, hid_regval);
633 
634 	/* Configure dummy clock cycles for fast read, dual, quad I/O commands */
635 	hid_regval = amd_spi_readreg32(amd_spi, AMD_SPI_HID2_READ_CNTRL2);
636 	/* Fast read dummy cycle */
637 	hid_regval &= ~GENMASK(4, 0);
638 
639 	/* Fast read Dual I/O dummy cycle */
640 	hid_regval &= ~GENMASK(12, 8);
641 
642 	/* Fast read Quad I/O dummy cycle */
643 	hid_regval = (hid_regval & ~GENMASK(20, 16)) | BIT(17);
644 
645 	/* Set no of preamble bytecount */
646 	hid_regval &= ~GENMASK(27, 24);
647 	amd_spi_writereg32(amd_spi, AMD_SPI_HID2_READ_CNTRL2, hid_regval);
648 
649 	/*
650 	 * Program the HID2 Input Ring Buffer0. 4k aligned buf_memory_addr[31:12],
651 	 * buf_size[4:0], end_input_ring[5].
652 	 */
653 	hid_regval = amd_spi->phy_dma_buf | BIT(5) | BIT(0);
654 	amd_spi_writereg32(amd_spi, AMD_SPI_HID2_INPUT_RING_BUF0, hid_regval);
655 
656 	/* Program max read length(no of DWs) in hid2_read_control1 register */
657 	hid_regval = amd_spi_readreg32(amd_spi, AMD_SPI_HID2_READ_CNTRL1);
658 	hid_regval = (hid_regval & ~GENMASK(15, 0)) | ((op->data.nbytes / 4) - 1);
659 	amd_spi_writereg32(amd_spi, AMD_SPI_HID2_READ_CNTRL1, hid_regval);
660 
661 	/* Set cmd start bit in hid2_cmd_start register to trigger HID basic read operation */
662 	hid_cmd_start = amd_spi_readreg16(amd_spi, AMD_SPI_HID2_CMD_START);
663 	amd_spi_writereg16(amd_spi, AMD_SPI_HID2_CMD_START, (hid_cmd_start | BIT(3)));
664 
665 	/* Check interrupt status of HIDDMA basic read operation in hid2_int_status register */
666 	readw_poll_timeout(amd_spi->io_remap_addr + AMD_SPI_HID2_INT_STATUS, val,
667 			   (val & BIT(3)), AMD_SPI_IO_SLEEP_US, AMD_SPI_IO_TIMEOUT_US);
668 
669 	/* Clear the interrupts by writing to hid2_int_status register */
670 	val = amd_spi_readreg16(amd_spi, AMD_SPI_HID2_INT_STATUS);
671 	amd_spi_writereg16(amd_spi, AMD_SPI_HID2_INT_STATUS, val);
672 }
673 
674 static void amd_spi_mem_data_in(struct amd_spi *amd_spi,
675 				const struct spi_mem_op *op)
676 {
677 	int base_addr = AMD_SPI_FIFO_BASE + op->addr.nbytes;
678 	u64 *buf_64 = (u64 *)op->data.buf.in;
679 	u32 nbytes = op->data.nbytes;
680 	u32 left_data = nbytes;
681 	u32 data;
682 	u8 *buf;
683 	int i;
684 
685 	/*
686 	 * Condition for using HID read mode. Only for reading complete page data, use HID read.
687 	 * Use index mode otherwise.
688 	 */
689 	if (amd_spi->version == AMD_HID2_SPI && amd_is_spi_read_cmd(op->cmd.opcode)) {
690 		u64 *dma_buf64 = (u64 *)amd_spi->dma_virt_addr;
691 		u8 *dma_buf;
692 
693 		amd_spi_hiddma_read(amd_spi, op);
694 
695 		/* Copy data from DMA buffer */
696 		while (left_data >= 8) {
697 			*buf_64++ = *dma_buf64++;
698 			left_data -= 8;
699 		}
700 
701 		buf = (u8 *)buf_64;
702 		dma_buf = (u8 *)dma_buf64;
703 		while (left_data--)
704 			*buf++ = *dma_buf++;
705 
706 		/* Reset HID RX memory logic */
707 		data = amd_spi_readreg32(amd_spi, AMD_SPI_HID2_CNTRL);
708 		amd_spi_writereg32(amd_spi, AMD_SPI_HID2_CNTRL, data | BIT(5));
709 	} else {
710 		/* Index mode */
711 		amd_spi_set_opcode(amd_spi, op->cmd.opcode);
712 		amd_spi_set_addr(amd_spi, op);
713 		amd_spi_set_tx_count(amd_spi, op->addr.nbytes + op->dummy.nbytes);
714 
715 		for (i = 0; i < op->dummy.nbytes; i++)
716 			amd_spi_writereg8(amd_spi, (base_addr + i), 0xff);
717 
718 		amd_spi_set_rx_count(amd_spi, op->data.nbytes);
719 		amd_spi_clear_fifo_ptr(amd_spi);
720 		amd_spi_execute_opcode(amd_spi);
721 		amd_spi_busy_wait(amd_spi);
722 
723 		for (i = 0; left_data >= 8; i++, left_data -= 8)
724 			*buf_64++ = amd_spi_readreg64(amd_spi, base_addr + op->dummy.nbytes +
725 						      (i * 8));
726 
727 		buf = (u8 *)buf_64;
728 		for (i = 0; i < left_data; i++)
729 			buf[i] = amd_spi_readreg8(amd_spi, base_addr + op->dummy.nbytes +
730 						  nbytes + i - left_data);
731 	}
732 
733 }
734 
735 static void amd_set_spi_addr_mode(struct amd_spi *amd_spi,
736 				  const struct spi_mem_op *op)
737 {
738 	u32 val = amd_spi_readreg32(amd_spi, AMD_SPI_ADDR32CTRL_REG);
739 
740 	if (amd_is_spi_read_cmd_4b(op->cmd.opcode))
741 		amd_spi_writereg32(amd_spi, AMD_SPI_ADDR32CTRL_REG, val | BIT(0));
742 	else
743 		amd_spi_writereg32(amd_spi, AMD_SPI_ADDR32CTRL_REG, val & ~BIT(0));
744 }
745 
746 static int amd_spi_exec_mem_op(struct spi_mem *mem,
747 			       const struct spi_mem_op *op)
748 {
749 	struct amd_spi *amd_spi;
750 
751 	amd_spi = spi_controller_get_devdata(mem->spi->controller);
752 
753 	amd_set_spi_freq(amd_spi, op->max_freq);
754 
755 	if (amd_spi->version == AMD_SPI_V2)
756 		amd_set_spi_addr_mode(amd_spi, op);
757 
758 	switch (op->data.dir) {
759 	case SPI_MEM_DATA_IN:
760 		amd_spi_mem_data_in(amd_spi, op);
761 		break;
762 	case SPI_MEM_DATA_OUT:
763 		fallthrough;
764 	case SPI_MEM_NO_DATA:
765 		amd_spi_mem_data_out(amd_spi, op);
766 		break;
767 	default:
768 		return -EOPNOTSUPP;
769 	}
770 
771 	return 0;
772 }
773 
774 static const struct spi_controller_mem_ops amd_spi_mem_ops = {
775 	.exec_op = amd_spi_exec_mem_op,
776 	.adjust_op_size = amd_spi_adjust_op_size,
777 	.supports_op = amd_spi_supports_op,
778 };
779 
780 static const struct spi_controller_mem_caps amd_spi_mem_caps = {
781 	.per_op_freq = true,
782 };
783 
784 static int amd_spi_host_transfer(struct spi_controller *host,
785 				   struct spi_message *msg)
786 {
787 	struct amd_spi *amd_spi = spi_controller_get_devdata(host);
788 	struct spi_device *spi = msg->spi;
789 
790 	amd_spi_select_chip(amd_spi, spi_get_chipselect(spi, 0));
791 
792 	/*
793 	 * Extract spi_transfers from the spi message and
794 	 * program the controller.
795 	 */
796 	return amd_spi_fifo_xfer(amd_spi, host, msg);
797 }
798 
799 static size_t amd_spi_max_transfer_size(struct spi_device *spi)
800 {
801 	return AMD_SPI_FIFO_SIZE;
802 }
803 
804 static int amd_spi_setup_hiddma(struct amd_spi *amd_spi, struct device *dev)
805 {
806 	u32 hid_regval;
807 
808 	/* Allocate DMA buffer to use for HID basic read and write operations. For write
809 	 * operations, the DMA buffer should include the opcode, address bytes and dummy
810 	 * bytes(if any) in addition to the data bytes. Additionally, the hardware requires
811 	 * that the buffer address be 4K aligned. So, allocate DMA buffer of size
812 	 * 2 * AMD_SPI_HID2_DMA_SIZE.
813 	 */
814 	amd_spi->dma_virt_addr = dmam_alloc_coherent(dev, AMD_SPI_HID2_DMA_SIZE * 2,
815 						     &amd_spi->phy_dma_buf, GFP_KERNEL);
816 	if (!amd_spi->dma_virt_addr)
817 		return -ENOMEM;
818 
819 	/*
820 	 * Enable interrupts and set mask bits in hid2_int_mask register to generate interrupt
821 	 * properly for HIDDMA basic read and write operations.
822 	 */
823 	hid_regval = amd_spi_readreg32(amd_spi, AMD_SPI_HID2_INT_MASK);
824 	hid_regval = (hid_regval & GENMASK(31, 8)) | BIT(18) | BIT(19);
825 	amd_spi_writereg32(amd_spi, AMD_SPI_HID2_INT_MASK, hid_regval);
826 
827 	/* Configure buffer unit(4k) and write threshold in hid2_control register */
828 	hid_regval = amd_spi_readreg32(amd_spi, AMD_SPI_HID2_CNTRL);
829 	amd_spi_writereg32(amd_spi, AMD_SPI_HID2_CNTRL, (hid_regval | GENMASK(13, 12)) & ~BIT(3));
830 
831 	return 0;
832 }
833 
834 int amd_spi_probe_common(struct device *dev, struct spi_controller *host)
835 {
836 	struct amd_spi *amd_spi = spi_controller_get_devdata(host);
837 	int err;
838 
839 	/* Initialize the spi_controller fields */
840 	host->num_chipselect = 4;
841 	host->mode_bits = SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD;
842 	host->flags = SPI_CONTROLLER_HALF_DUPLEX;
843 	host->max_speed_hz = AMD_SPI_MAX_HZ;
844 	host->min_speed_hz = AMD_SPI_MIN_HZ;
845 	host->setup = amd_spi_host_setup;
846 	host->transfer_one_message = amd_spi_host_transfer;
847 	host->mem_ops = &amd_spi_mem_ops;
848 	host->mem_caps = &amd_spi_mem_caps;
849 	host->max_transfer_size = amd_spi_max_transfer_size;
850 	host->max_message_size = amd_spi_max_transfer_size;
851 
852 	/* Register the controller with SPI framework */
853 	err = devm_spi_register_controller(dev, host);
854 	if (err)
855 		return dev_err_probe(dev, err, "error registering SPI controller\n");
856 
857 	if (amd_spi->version == AMD_HID2_SPI)
858 		err = amd_spi_setup_hiddma(amd_spi, dev);
859 
860 	return err;
861 }
862 EXPORT_SYMBOL_GPL(amd_spi_probe_common);
863 
864 static int amd_spi_probe(struct platform_device *pdev)
865 {
866 	struct device *dev = &pdev->dev;
867 	struct spi_controller *host;
868 	struct amd_spi *amd_spi;
869 
870 	/* Allocate storage for host and driver private data */
871 	host = devm_spi_alloc_host(dev, sizeof(struct amd_spi));
872 	if (!host)
873 		return -ENOMEM;
874 
875 	amd_spi = spi_controller_get_devdata(host);
876 	amd_spi->io_remap_addr = devm_platform_ioremap_resource(pdev, 0);
877 	if (IS_ERR(amd_spi->io_remap_addr))
878 		return dev_err_probe(dev, PTR_ERR(amd_spi->io_remap_addr),
879 				     "ioremap of SPI registers failed\n");
880 
881 	dev_dbg(dev, "io_remap_address: %p\n", amd_spi->io_remap_addr);
882 
883 	amd_spi->version = (uintptr_t)device_get_match_data(dev);
884 	host->bus_num = (amd_spi->version == AMD_HID2_SPI) ? 2 : 0;
885 
886 	return amd_spi_probe_common(dev, host);
887 }
888 
889 #ifdef CONFIG_ACPI
890 static const struct acpi_device_id spi_acpi_match[] = {
891 	{ "AMDI0061", AMD_SPI_V1 },
892 	{ "AMDI0062", AMD_SPI_V2 },
893 	{ "AMDI0063", AMD_HID2_SPI },
894 	{},
895 };
896 MODULE_DEVICE_TABLE(acpi, spi_acpi_match);
897 #endif
898 
899 static struct platform_driver amd_spi_driver = {
900 	.driver = {
901 		.name = "amd_spi",
902 		.acpi_match_table = ACPI_PTR(spi_acpi_match),
903 	},
904 	.probe = amd_spi_probe,
905 };
906 
907 module_platform_driver(amd_spi_driver);
908 
909 MODULE_LICENSE("Dual BSD/GPL");
910 MODULE_AUTHOR("Sanjay Mehta <sanju.mehta@amd.com>");
911 MODULE_DESCRIPTION("AMD SPI Master Controller Driver");
912