1 // SPDX-License-Identifier: GPL-2.0+
2 // Cadence XSPI flash controller driver
3 // Copyright (C) 2020-21 Cadence
5 #include <linux/completion.h>
6 #include <linux/delay.h>
8 #include <linux/errno.h>
9 #include <linux/interrupt.h>
11 #include <linux/iopoll.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/of_device.h>
16 #include <linux/platform_device.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/spi/spi.h>
19 #include <linux/spi/spi-mem.h>
20 #include <linux/bitfield.h>
21 #include <linux/limits.h>
22 #include <linux/log2.h>
24 #define CDNS_XSPI_MAGIC_NUM_VALUE 0x6522
25 #define CDNS_XSPI_MAX_BANKS 8
26 #define CDNS_XSPI_NAME "cadence-xspi"
29 * Note: below are additional auxiliary registers to
30 * configure XSPI controller pin-strap settings
33 /* PHY DQ timing register */
34 #define CDNS_XSPI_CCP_PHY_DQ_TIMING 0x0000
36 /* PHY DQS timing register */
37 #define CDNS_XSPI_CCP_PHY_DQS_TIMING 0x0004
39 /* PHY gate loopback control register */
40 #define CDNS_XSPI_CCP_PHY_GATE_LPBCK_CTRL 0x0008
42 /* PHY DLL slave control register */
43 #define CDNS_XSPI_CCP_PHY_DLL_SLAVE_CTRL 0x0010
45 /* DLL PHY control register */
46 #define CDNS_XSPI_DLL_PHY_CTRL 0x1034
48 /* Command registers */
49 #define CDNS_XSPI_CMD_REG_0 0x0000
50 #define CDNS_XSPI_CMD_REG_1 0x0004
51 #define CDNS_XSPI_CMD_REG_2 0x0008
52 #define CDNS_XSPI_CMD_REG_3 0x000C
53 #define CDNS_XSPI_CMD_REG_4 0x0010
54 #define CDNS_XSPI_CMD_REG_5 0x0014
56 /* Command status registers */
57 #define CDNS_XSPI_CMD_STATUS_REG 0x0044
59 /* Controller status register */
60 #define CDNS_XSPI_CTRL_STATUS_REG 0x0100
61 #define CDNS_XSPI_INIT_COMPLETED BIT(16)
62 #define CDNS_XSPI_INIT_LEGACY BIT(9)
63 #define CDNS_XSPI_INIT_FAIL BIT(8)
64 #define CDNS_XSPI_CTRL_BUSY BIT(7)
66 /* Controller interrupt status register */
67 #define CDNS_XSPI_INTR_STATUS_REG 0x0110
68 #define CDNS_XSPI_STIG_DONE BIT(23)
69 #define CDNS_XSPI_SDMA_ERROR BIT(22)
70 #define CDNS_XSPI_SDMA_TRIGGER BIT(21)
71 #define CDNS_XSPI_CMD_IGNRD_EN BIT(20)
72 #define CDNS_XSPI_DDMA_TERR_EN BIT(18)
73 #define CDNS_XSPI_CDMA_TREE_EN BIT(17)
74 #define CDNS_XSPI_CTRL_IDLE_EN BIT(16)
76 #define CDNS_XSPI_TRD_COMP_INTR_STATUS 0x0120
77 #define CDNS_XSPI_TRD_ERR_INTR_STATUS 0x0130
78 #define CDNS_XSPI_TRD_ERR_INTR_EN 0x0134
80 /* Controller interrupt enable register */
81 #define CDNS_XSPI_INTR_ENABLE_REG 0x0114
82 #define CDNS_XSPI_INTR_EN BIT(31)
83 #define CDNS_XSPI_STIG_DONE_EN BIT(23)
84 #define CDNS_XSPI_SDMA_ERROR_EN BIT(22)
85 #define CDNS_XSPI_SDMA_TRIGGER_EN BIT(21)
87 #define CDNS_XSPI_INTR_MASK (CDNS_XSPI_INTR_EN | \
88 CDNS_XSPI_STIG_DONE_EN | \
89 CDNS_XSPI_SDMA_ERROR_EN | \
90 CDNS_XSPI_SDMA_TRIGGER_EN)
92 /* Controller config register */
93 #define CDNS_XSPI_CTRL_CONFIG_REG 0x0230
94 #define CDNS_XSPI_CTRL_WORK_MODE GENMASK(6, 5)
96 #define CDNS_XSPI_WORK_MODE_DIRECT 0
97 #define CDNS_XSPI_WORK_MODE_STIG 1
98 #define CDNS_XSPI_WORK_MODE_ACMD 3
100 /* SDMA trigger transaction registers */
101 #define CDNS_XSPI_SDMA_SIZE_REG 0x0240
102 #define CDNS_XSPI_SDMA_TRD_INFO_REG 0x0244
103 #define CDNS_XSPI_SDMA_DIR BIT(8)
105 /* Controller features register */
106 #define CDNS_XSPI_CTRL_FEATURES_REG 0x0F04
107 #define CDNS_XSPI_NUM_BANKS GENMASK(25, 24)
108 #define CDNS_XSPI_DMA_DATA_WIDTH BIT(21)
109 #define CDNS_XSPI_NUM_THREADS GENMASK(3, 0)
111 /* Controller version register */
112 #define CDNS_XSPI_CTRL_VERSION_REG 0x0F00
113 #define CDNS_XSPI_MAGIC_NUM GENMASK(31, 16)
114 #define CDNS_XSPI_CTRL_REV GENMASK(7, 0)
116 /* STIG Profile 1.0 instruction fields (split into registers) */
117 #define CDNS_XSPI_CMD_INSTR_TYPE GENMASK(6, 0)
118 #define CDNS_XSPI_CMD_P1_R1_ADDR0 GENMASK(31, 24)
119 #define CDNS_XSPI_CMD_P1_R2_ADDR1 GENMASK(7, 0)
120 #define CDNS_XSPI_CMD_P1_R2_ADDR2 GENMASK(15, 8)
121 #define CDNS_XSPI_CMD_P1_R2_ADDR3 GENMASK(23, 16)
122 #define CDNS_XSPI_CMD_P1_R2_ADDR4 GENMASK(31, 24)
123 #define CDNS_XSPI_CMD_P1_R3_ADDR5 GENMASK(7, 0)
124 #define CDNS_XSPI_CMD_P1_R3_CMD GENMASK(23, 16)
125 #define CDNS_XSPI_CMD_P1_R3_NUM_ADDR_BYTES GENMASK(30, 28)
126 #define CDNS_XSPI_CMD_P1_R4_ADDR_IOS GENMASK(1, 0)
127 #define CDNS_XSPI_CMD_P1_R4_CMD_IOS GENMASK(9, 8)
128 #define CDNS_XSPI_CMD_P1_R4_BANK GENMASK(14, 12)
130 /* STIG data sequence instruction fields (split into registers) */
131 #define CDNS_XSPI_CMD_DSEQ_R2_DCNT_L GENMASK(31, 16)
132 #define CDNS_XSPI_CMD_DSEQ_R3_DCNT_H GENMASK(15, 0)
133 #define CDNS_XSPI_CMD_DSEQ_R3_NUM_OF_DUMMY GENMASK(25, 20)
134 #define CDNS_XSPI_CMD_DSEQ_R4_BANK GENMASK(14, 12)
135 #define CDNS_XSPI_CMD_DSEQ_R4_DATA_IOS GENMASK(9, 8)
136 #define CDNS_XSPI_CMD_DSEQ_R4_DIR BIT(4)
138 /* STIG command status fields */
139 #define CDNS_XSPI_CMD_STATUS_COMPLETED BIT(15)
140 #define CDNS_XSPI_CMD_STATUS_FAILED BIT(14)
141 #define CDNS_XSPI_CMD_STATUS_DQS_ERROR BIT(3)
142 #define CDNS_XSPI_CMD_STATUS_CRC_ERROR BIT(2)
143 #define CDNS_XSPI_CMD_STATUS_BUS_ERROR BIT(1)
144 #define CDNS_XSPI_CMD_STATUS_INV_SEQ_ERROR BIT(0)
146 #define CDNS_XSPI_STIG_DONE_FLAG BIT(0)
147 #define CDNS_XSPI_TRD_STATUS 0x0104
149 /* Helper macros for filling command registers */
150 #define CDNS_XSPI_CMD_FLD_P1_INSTR_CMD_1(op, data_phase) ( \
151 FIELD_PREP(CDNS_XSPI_CMD_INSTR_TYPE, (data_phase) ? \
152 CDNS_XSPI_STIG_INSTR_TYPE_1 : CDNS_XSPI_STIG_INSTR_TYPE_0) | \
153 FIELD_PREP(CDNS_XSPI_CMD_P1_R1_ADDR0, (op)->addr.val & 0xff))
155 #define CDNS_XSPI_CMD_FLD_P1_INSTR_CMD_2(op) ( \
156 FIELD_PREP(CDNS_XSPI_CMD_P1_R2_ADDR1, ((op)->addr.val >> 8) & 0xFF) | \
157 FIELD_PREP(CDNS_XSPI_CMD_P1_R2_ADDR2, ((op)->addr.val >> 16) & 0xFF) | \
158 FIELD_PREP(CDNS_XSPI_CMD_P1_R2_ADDR3, ((op)->addr.val >> 24) & 0xFF) | \
159 FIELD_PREP(CDNS_XSPI_CMD_P1_R2_ADDR4, ((op)->addr.val >> 32) & 0xFF))
161 #define CDNS_XSPI_CMD_FLD_P1_INSTR_CMD_3(op) ( \
162 FIELD_PREP(CDNS_XSPI_CMD_P1_R3_ADDR5, ((op)->addr.val >> 40) & 0xFF) | \
163 FIELD_PREP(CDNS_XSPI_CMD_P1_R3_CMD, (op)->cmd.opcode) | \
164 FIELD_PREP(CDNS_XSPI_CMD_P1_R3_NUM_ADDR_BYTES, (op)->addr.nbytes))
166 #define CDNS_XSPI_CMD_FLD_P1_INSTR_CMD_4(op, chipsel) ( \
167 FIELD_PREP(CDNS_XSPI_CMD_P1_R4_ADDR_IOS, ilog2((op)->addr.buswidth)) | \
168 FIELD_PREP(CDNS_XSPI_CMD_P1_R4_CMD_IOS, ilog2((op)->cmd.buswidth)) | \
169 FIELD_PREP(CDNS_XSPI_CMD_P1_R4_BANK, chipsel))
171 #define CDNS_XSPI_CMD_FLD_DSEQ_CMD_1(op) \
172 FIELD_PREP(CDNS_XSPI_CMD_INSTR_TYPE, CDNS_XSPI_STIG_INSTR_TYPE_DATA_SEQ)
174 #define CDNS_XSPI_CMD_FLD_DSEQ_CMD_2(op) \
175 FIELD_PREP(CDNS_XSPI_CMD_DSEQ_R2_DCNT_L, (op)->data.nbytes & 0xFFFF)
177 #define CDNS_XSPI_CMD_FLD_DSEQ_CMD_3(op) ( \
178 FIELD_PREP(CDNS_XSPI_CMD_DSEQ_R3_DCNT_H, \
179 ((op)->data.nbytes >> 16) & 0xffff) | \
180 FIELD_PREP(CDNS_XSPI_CMD_DSEQ_R3_NUM_OF_DUMMY, \
181 (op)->dummy.buswidth != 0 ? \
182 (((op)->dummy.nbytes * 8) / (op)->dummy.buswidth) : \
185 #define CDNS_XSPI_CMD_FLD_DSEQ_CMD_4(op, chipsel) ( \
186 FIELD_PREP(CDNS_XSPI_CMD_DSEQ_R4_BANK, chipsel) | \
187 FIELD_PREP(CDNS_XSPI_CMD_DSEQ_R4_DATA_IOS, \
188 ilog2((op)->data.buswidth)) | \
189 FIELD_PREP(CDNS_XSPI_CMD_DSEQ_R4_DIR, \
190 ((op)->data.dir == SPI_MEM_DATA_IN) ? \
191 CDNS_XSPI_STIG_CMD_DIR_READ : CDNS_XSPI_STIG_CMD_DIR_WRITE))
193 enum cdns_xspi_stig_instr_type {
194 CDNS_XSPI_STIG_INSTR_TYPE_0,
195 CDNS_XSPI_STIG_INSTR_TYPE_1,
196 CDNS_XSPI_STIG_INSTR_TYPE_DATA_SEQ = 127,
199 enum cdns_xspi_sdma_dir {
200 CDNS_XSPI_SDMA_DIR_READ,
201 CDNS_XSPI_SDMA_DIR_WRITE,
204 enum cdns_xspi_stig_cmd_dir {
205 CDNS_XSPI_STIG_CMD_DIR_READ,
206 CDNS_XSPI_STIG_CMD_DIR_WRITE,
209 struct cdns_xspi_dev {
210 struct platform_device *pdev;
213 void __iomem *iobase;
214 void __iomem *auxbase;
215 void __iomem *sdmabase;
219 unsigned int sdmasize;
221 struct completion cmd_complete;
222 struct completion auto_cmd_complete;
223 struct completion sdma_complete;
227 const void *out_buffer;
232 static int cdns_xspi_wait_for_controller_idle(struct cdns_xspi_dev *cdns_xspi)
236 return readl_relaxed_poll_timeout(cdns_xspi->iobase +
237 CDNS_XSPI_CTRL_STATUS_REG,
240 CDNS_XSPI_CTRL_BUSY) == 0),
244 static void cdns_xspi_trigger_command(struct cdns_xspi_dev *cdns_xspi,
247 writel(cmd_regs[5], cdns_xspi->iobase + CDNS_XSPI_CMD_REG_5);
248 writel(cmd_regs[4], cdns_xspi->iobase + CDNS_XSPI_CMD_REG_4);
249 writel(cmd_regs[3], cdns_xspi->iobase + CDNS_XSPI_CMD_REG_3);
250 writel(cmd_regs[2], cdns_xspi->iobase + CDNS_XSPI_CMD_REG_2);
251 writel(cmd_regs[1], cdns_xspi->iobase + CDNS_XSPI_CMD_REG_1);
252 writel(cmd_regs[0], cdns_xspi->iobase + CDNS_XSPI_CMD_REG_0);
255 static int cdns_xspi_check_command_status(struct cdns_xspi_dev *cdns_xspi)
258 u32 cmd_status = readl(cdns_xspi->iobase + CDNS_XSPI_CMD_STATUS_REG);
260 if (cmd_status & CDNS_XSPI_CMD_STATUS_COMPLETED) {
261 if ((cmd_status & CDNS_XSPI_CMD_STATUS_FAILED) != 0) {
262 if (cmd_status & CDNS_XSPI_CMD_STATUS_DQS_ERROR) {
263 dev_err(cdns_xspi->dev,
264 "Incorrect DQS pulses detected\n");
267 if (cmd_status & CDNS_XSPI_CMD_STATUS_CRC_ERROR) {
268 dev_err(cdns_xspi->dev,
269 "CRC error received\n");
272 if (cmd_status & CDNS_XSPI_CMD_STATUS_BUS_ERROR) {
273 dev_err(cdns_xspi->dev,
274 "Error resp on system DMA interface\n");
277 if (cmd_status & CDNS_XSPI_CMD_STATUS_INV_SEQ_ERROR) {
278 dev_err(cdns_xspi->dev,
279 "Invalid command sequence detected\n");
284 dev_err(cdns_xspi->dev, "Fatal err - command not completed\n");
291 static void cdns_xspi_set_interrupts(struct cdns_xspi_dev *cdns_xspi,
296 intr_enable = readl(cdns_xspi->iobase + CDNS_XSPI_INTR_ENABLE_REG);
298 intr_enable |= CDNS_XSPI_INTR_MASK;
300 intr_enable &= ~CDNS_XSPI_INTR_MASK;
301 writel(intr_enable, cdns_xspi->iobase + CDNS_XSPI_INTR_ENABLE_REG);
304 static int cdns_xspi_controller_init(struct cdns_xspi_dev *cdns_xspi)
310 ctrl_ver = readl(cdns_xspi->iobase + CDNS_XSPI_CTRL_VERSION_REG);
311 hw_magic_num = FIELD_GET(CDNS_XSPI_MAGIC_NUM, ctrl_ver);
312 if (hw_magic_num != CDNS_XSPI_MAGIC_NUM_VALUE) {
313 dev_err(cdns_xspi->dev,
314 "Incorrect XSPI magic number: %x, expected: %x\n",
315 hw_magic_num, CDNS_XSPI_MAGIC_NUM_VALUE);
319 ctrl_features = readl(cdns_xspi->iobase + CDNS_XSPI_CTRL_FEATURES_REG);
320 cdns_xspi->hw_num_banks = FIELD_GET(CDNS_XSPI_NUM_BANKS, ctrl_features);
321 cdns_xspi_set_interrupts(cdns_xspi, false);
326 static void cdns_xspi_sdma_handle(struct cdns_xspi_dev *cdns_xspi)
328 u32 sdma_size, sdma_trd_info;
331 sdma_size = readl(cdns_xspi->iobase + CDNS_XSPI_SDMA_SIZE_REG);
332 sdma_trd_info = readl(cdns_xspi->iobase + CDNS_XSPI_SDMA_TRD_INFO_REG);
333 sdma_dir = FIELD_GET(CDNS_XSPI_SDMA_DIR, sdma_trd_info);
336 case CDNS_XSPI_SDMA_DIR_READ:
337 ioread8_rep(cdns_xspi->sdmabase,
338 cdns_xspi->in_buffer, sdma_size);
341 case CDNS_XSPI_SDMA_DIR_WRITE:
342 iowrite8_rep(cdns_xspi->sdmabase,
343 cdns_xspi->out_buffer, sdma_size);
348 static int cdns_xspi_send_stig_command(struct cdns_xspi_dev *cdns_xspi,
349 const struct spi_mem_op *op,
356 ret = cdns_xspi_wait_for_controller_idle(cdns_xspi);
360 writel(FIELD_PREP(CDNS_XSPI_CTRL_WORK_MODE, CDNS_XSPI_WORK_MODE_STIG),
361 cdns_xspi->iobase + CDNS_XSPI_CTRL_CONFIG_REG);
363 cdns_xspi_set_interrupts(cdns_xspi, true);
364 cdns_xspi->sdma_error = false;
366 memset(cmd_regs, 0, sizeof(cmd_regs));
367 cmd_regs[1] = CDNS_XSPI_CMD_FLD_P1_INSTR_CMD_1(op, data_phase);
368 cmd_regs[2] = CDNS_XSPI_CMD_FLD_P1_INSTR_CMD_2(op);
369 cmd_regs[3] = CDNS_XSPI_CMD_FLD_P1_INSTR_CMD_3(op);
370 cmd_regs[4] = CDNS_XSPI_CMD_FLD_P1_INSTR_CMD_4(op,
373 cdns_xspi_trigger_command(cdns_xspi, cmd_regs);
376 cmd_regs[0] = CDNS_XSPI_STIG_DONE_FLAG;
377 cmd_regs[1] = CDNS_XSPI_CMD_FLD_DSEQ_CMD_1(op);
378 cmd_regs[2] = CDNS_XSPI_CMD_FLD_DSEQ_CMD_2(op);
379 cmd_regs[3] = CDNS_XSPI_CMD_FLD_DSEQ_CMD_3(op);
380 cmd_regs[4] = CDNS_XSPI_CMD_FLD_DSEQ_CMD_4(op,
383 cdns_xspi->in_buffer = op->data.buf.in;
384 cdns_xspi->out_buffer = op->data.buf.out;
386 cdns_xspi_trigger_command(cdns_xspi, cmd_regs);
388 wait_for_completion(&cdns_xspi->sdma_complete);
389 if (cdns_xspi->sdma_error) {
390 cdns_xspi_set_interrupts(cdns_xspi, false);
393 cdns_xspi_sdma_handle(cdns_xspi);
396 wait_for_completion(&cdns_xspi->cmd_complete);
397 cdns_xspi_set_interrupts(cdns_xspi, false);
399 cmd_status = cdns_xspi_check_command_status(cdns_xspi);
406 static int cdns_xspi_mem_op(struct cdns_xspi_dev *cdns_xspi,
408 const struct spi_mem_op *op)
410 enum spi_mem_data_dir dir = op->data.dir;
412 if (cdns_xspi->cur_cs != spi_get_chipselect(mem->spi, 0))
413 cdns_xspi->cur_cs = spi_get_chipselect(mem->spi, 0);
415 return cdns_xspi_send_stig_command(cdns_xspi, op,
416 (dir != SPI_MEM_NO_DATA));
419 static int cdns_xspi_mem_op_execute(struct spi_mem *mem,
420 const struct spi_mem_op *op)
422 struct cdns_xspi_dev *cdns_xspi =
423 spi_master_get_devdata(mem->spi->master);
426 ret = cdns_xspi_mem_op(cdns_xspi, mem, op);
431 static int cdns_xspi_adjust_mem_op_size(struct spi_mem *mem, struct spi_mem_op *op)
433 struct cdns_xspi_dev *cdns_xspi =
434 spi_master_get_devdata(mem->spi->master);
436 op->data.nbytes = clamp_val(op->data.nbytes, 0, cdns_xspi->sdmasize);
441 static const struct spi_controller_mem_ops cadence_xspi_mem_ops = {
442 .exec_op = cdns_xspi_mem_op_execute,
443 .adjust_op_size = cdns_xspi_adjust_mem_op_size,
446 static irqreturn_t cdns_xspi_irq_handler(int this_irq, void *dev)
448 struct cdns_xspi_dev *cdns_xspi = dev;
450 irqreturn_t result = IRQ_NONE;
452 irq_status = readl(cdns_xspi->iobase + CDNS_XSPI_INTR_STATUS_REG);
453 writel(irq_status, cdns_xspi->iobase + CDNS_XSPI_INTR_STATUS_REG);
456 (CDNS_XSPI_SDMA_ERROR | CDNS_XSPI_SDMA_TRIGGER |
457 CDNS_XSPI_STIG_DONE)) {
458 if (irq_status & CDNS_XSPI_SDMA_ERROR) {
459 dev_err(cdns_xspi->dev,
460 "Slave DMA transaction error\n");
461 cdns_xspi->sdma_error = true;
462 complete(&cdns_xspi->sdma_complete);
465 if (irq_status & CDNS_XSPI_SDMA_TRIGGER)
466 complete(&cdns_xspi->sdma_complete);
468 if (irq_status & CDNS_XSPI_STIG_DONE)
469 complete(&cdns_xspi->cmd_complete);
471 result = IRQ_HANDLED;
474 irq_status = readl(cdns_xspi->iobase + CDNS_XSPI_TRD_COMP_INTR_STATUS);
477 cdns_xspi->iobase + CDNS_XSPI_TRD_COMP_INTR_STATUS);
479 complete(&cdns_xspi->auto_cmd_complete);
481 result = IRQ_HANDLED;
487 static int cdns_xspi_of_get_plat_data(struct platform_device *pdev)
489 struct device_node *node_prop = pdev->dev.of_node;
490 struct device_node *node_child;
493 for_each_child_of_node(node_prop, node_child) {
494 if (!of_device_is_available(node_child))
497 if (of_property_read_u32(node_child, "reg", &cs)) {
498 dev_err(&pdev->dev, "Couldn't get memory chip select\n");
499 of_node_put(node_child);
501 } else if (cs >= CDNS_XSPI_MAX_BANKS) {
502 dev_err(&pdev->dev, "reg (cs) parameter value too large\n");
503 of_node_put(node_child);
511 static void cdns_xspi_print_phy_config(struct cdns_xspi_dev *cdns_xspi)
513 struct device *dev = cdns_xspi->dev;
515 dev_info(dev, "PHY configuration\n");
516 dev_info(dev, " * xspi_dll_phy_ctrl: %08x\n",
517 readl(cdns_xspi->iobase + CDNS_XSPI_DLL_PHY_CTRL));
518 dev_info(dev, " * phy_dq_timing: %08x\n",
519 readl(cdns_xspi->auxbase + CDNS_XSPI_CCP_PHY_DQ_TIMING));
520 dev_info(dev, " * phy_dqs_timing: %08x\n",
521 readl(cdns_xspi->auxbase + CDNS_XSPI_CCP_PHY_DQS_TIMING));
522 dev_info(dev, " * phy_gate_loopback_ctrl: %08x\n",
523 readl(cdns_xspi->auxbase + CDNS_XSPI_CCP_PHY_GATE_LPBCK_CTRL));
524 dev_info(dev, " * phy_dll_slave_ctrl: %08x\n",
525 readl(cdns_xspi->auxbase + CDNS_XSPI_CCP_PHY_DLL_SLAVE_CTRL));
528 static int cdns_xspi_probe(struct platform_device *pdev)
530 struct device *dev = &pdev->dev;
531 struct spi_master *master = NULL;
532 struct cdns_xspi_dev *cdns_xspi = NULL;
533 struct resource *res;
536 master = devm_spi_alloc_master(dev, sizeof(*cdns_xspi));
540 master->mode_bits = SPI_3WIRE | SPI_TX_DUAL | SPI_TX_QUAD |
541 SPI_RX_DUAL | SPI_RX_QUAD | SPI_TX_OCTAL | SPI_RX_OCTAL |
542 SPI_MODE_0 | SPI_MODE_3;
544 master->mem_ops = &cadence_xspi_mem_ops;
545 master->dev.of_node = pdev->dev.of_node;
546 master->bus_num = -1;
548 platform_set_drvdata(pdev, master);
550 cdns_xspi = spi_master_get_devdata(master);
551 cdns_xspi->pdev = pdev;
552 cdns_xspi->dev = &pdev->dev;
553 cdns_xspi->cur_cs = 0;
555 init_completion(&cdns_xspi->cmd_complete);
556 init_completion(&cdns_xspi->auto_cmd_complete);
557 init_completion(&cdns_xspi->sdma_complete);
559 ret = cdns_xspi_of_get_plat_data(pdev);
563 cdns_xspi->iobase = devm_platform_ioremap_resource_byname(pdev, "io");
564 if (IS_ERR(cdns_xspi->iobase)) {
565 dev_err(dev, "Failed to remap controller base address\n");
566 return PTR_ERR(cdns_xspi->iobase);
569 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sdma");
570 cdns_xspi->sdmabase = devm_ioremap_resource(dev, res);
571 if (IS_ERR(cdns_xspi->sdmabase))
572 return PTR_ERR(cdns_xspi->sdmabase);
573 cdns_xspi->sdmasize = resource_size(res);
575 cdns_xspi->auxbase = devm_platform_ioremap_resource_byname(pdev, "aux");
576 if (IS_ERR(cdns_xspi->auxbase)) {
577 dev_err(dev, "Failed to remap AUX address\n");
578 return PTR_ERR(cdns_xspi->auxbase);
581 cdns_xspi->irq = platform_get_irq(pdev, 0);
582 if (cdns_xspi->irq < 0)
585 ret = devm_request_irq(dev, cdns_xspi->irq, cdns_xspi_irq_handler,
586 IRQF_SHARED, pdev->name, cdns_xspi);
588 dev_err(dev, "Failed to request IRQ: %d\n", cdns_xspi->irq);
592 cdns_xspi_print_phy_config(cdns_xspi);
594 ret = cdns_xspi_controller_init(cdns_xspi);
596 dev_err(dev, "Failed to initialize controller\n");
600 master->num_chipselect = 1 << cdns_xspi->hw_num_banks;
602 ret = devm_spi_register_master(dev, master);
604 dev_err(dev, "Failed to register SPI master\n");
608 dev_info(dev, "Successfully registered SPI master\n");
613 static const struct of_device_id cdns_xspi_of_match[] = {
615 .compatible = "cdns,xspi-nor",
617 { /* end of table */}
619 MODULE_DEVICE_TABLE(of, cdns_xspi_of_match);
621 static struct platform_driver cdns_xspi_platform_driver = {
622 .probe = cdns_xspi_probe,
625 .name = CDNS_XSPI_NAME,
626 .of_match_table = cdns_xspi_of_match,
630 module_platform_driver(cdns_xspi_platform_driver);
632 MODULE_DESCRIPTION("Cadence XSPI Controller Driver");
633 MODULE_LICENSE("GPL v2");
634 MODULE_ALIAS("platform:" CDNS_XSPI_NAME);
635 MODULE_AUTHOR("Konrad Kociolek <konrad@cadence.com>");
636 MODULE_AUTHOR("Jayshri Pawar <jpawar@cadence.com>");
637 MODULE_AUTHOR("Parshuram Thombare <pthombar@cadence.com>");