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, (op)->dummy.nbytes * 8))
182 #define CDNS_XSPI_CMD_FLD_DSEQ_CMD_4(op, chipsel) ( \
183 FIELD_PREP(CDNS_XSPI_CMD_DSEQ_R4_BANK, chipsel) | \
184 FIELD_PREP(CDNS_XSPI_CMD_DSEQ_R4_DATA_IOS, \
185 ilog2((op)->data.buswidth)) | \
186 FIELD_PREP(CDNS_XSPI_CMD_DSEQ_R4_DIR, \
187 ((op)->data.dir == SPI_MEM_DATA_IN) ? \
188 CDNS_XSPI_STIG_CMD_DIR_READ : CDNS_XSPI_STIG_CMD_DIR_WRITE))
190 enum cdns_xspi_stig_instr_type {
191 CDNS_XSPI_STIG_INSTR_TYPE_0,
192 CDNS_XSPI_STIG_INSTR_TYPE_1,
193 CDNS_XSPI_STIG_INSTR_TYPE_DATA_SEQ = 127,
196 enum cdns_xspi_sdma_dir {
197 CDNS_XSPI_SDMA_DIR_READ,
198 CDNS_XSPI_SDMA_DIR_WRITE,
201 enum cdns_xspi_stig_cmd_dir {
202 CDNS_XSPI_STIG_CMD_DIR_READ,
203 CDNS_XSPI_STIG_CMD_DIR_WRITE,
206 struct cdns_xspi_dev {
207 struct platform_device *pdev;
210 void __iomem *iobase;
211 void __iomem *auxbase;
212 void __iomem *sdmabase;
216 unsigned int sdmasize;
218 struct completion cmd_complete;
219 struct completion auto_cmd_complete;
220 struct completion sdma_complete;
224 const void *out_buffer;
229 static int cdns_xspi_wait_for_controller_idle(struct cdns_xspi_dev *cdns_xspi)
233 return readl_relaxed_poll_timeout(cdns_xspi->iobase +
234 CDNS_XSPI_CTRL_STATUS_REG,
237 CDNS_XSPI_CTRL_BUSY) == 0),
241 static void cdns_xspi_trigger_command(struct cdns_xspi_dev *cdns_xspi,
244 writel(cmd_regs[5], cdns_xspi->iobase + CDNS_XSPI_CMD_REG_5);
245 writel(cmd_regs[4], cdns_xspi->iobase + CDNS_XSPI_CMD_REG_4);
246 writel(cmd_regs[3], cdns_xspi->iobase + CDNS_XSPI_CMD_REG_3);
247 writel(cmd_regs[2], cdns_xspi->iobase + CDNS_XSPI_CMD_REG_2);
248 writel(cmd_regs[1], cdns_xspi->iobase + CDNS_XSPI_CMD_REG_1);
249 writel(cmd_regs[0], cdns_xspi->iobase + CDNS_XSPI_CMD_REG_0);
252 static int cdns_xspi_check_command_status(struct cdns_xspi_dev *cdns_xspi)
255 u32 cmd_status = readl(cdns_xspi->iobase + CDNS_XSPI_CMD_STATUS_REG);
257 if (cmd_status & CDNS_XSPI_CMD_STATUS_COMPLETED) {
258 if ((cmd_status & CDNS_XSPI_CMD_STATUS_FAILED) != 0) {
259 if (cmd_status & CDNS_XSPI_CMD_STATUS_DQS_ERROR) {
260 dev_err(cdns_xspi->dev,
261 "Incorrect DQS pulses detected\n");
264 if (cmd_status & CDNS_XSPI_CMD_STATUS_CRC_ERROR) {
265 dev_err(cdns_xspi->dev,
266 "CRC error received\n");
269 if (cmd_status & CDNS_XSPI_CMD_STATUS_BUS_ERROR) {
270 dev_err(cdns_xspi->dev,
271 "Error resp on system DMA interface\n");
274 if (cmd_status & CDNS_XSPI_CMD_STATUS_INV_SEQ_ERROR) {
275 dev_err(cdns_xspi->dev,
276 "Invalid command sequence detected\n");
281 dev_err(cdns_xspi->dev, "Fatal err - command not completed\n");
288 static void cdns_xspi_set_interrupts(struct cdns_xspi_dev *cdns_xspi,
293 intr_enable = readl(cdns_xspi->iobase + CDNS_XSPI_INTR_ENABLE_REG);
295 intr_enable |= CDNS_XSPI_INTR_MASK;
297 intr_enable &= ~CDNS_XSPI_INTR_MASK;
298 writel(intr_enable, cdns_xspi->iobase + CDNS_XSPI_INTR_ENABLE_REG);
301 static int cdns_xspi_controller_init(struct cdns_xspi_dev *cdns_xspi)
307 ctrl_ver = readl(cdns_xspi->iobase + CDNS_XSPI_CTRL_VERSION_REG);
308 hw_magic_num = FIELD_GET(CDNS_XSPI_MAGIC_NUM, ctrl_ver);
309 if (hw_magic_num != CDNS_XSPI_MAGIC_NUM_VALUE) {
310 dev_err(cdns_xspi->dev,
311 "Incorrect XSPI magic number: %x, expected: %x\n",
312 hw_magic_num, CDNS_XSPI_MAGIC_NUM_VALUE);
316 ctrl_features = readl(cdns_xspi->iobase + CDNS_XSPI_CTRL_FEATURES_REG);
317 cdns_xspi->hw_num_banks = FIELD_GET(CDNS_XSPI_NUM_BANKS, ctrl_features);
318 cdns_xspi_set_interrupts(cdns_xspi, false);
323 static void cdns_xspi_sdma_handle(struct cdns_xspi_dev *cdns_xspi)
325 u32 sdma_size, sdma_trd_info;
328 sdma_size = readl(cdns_xspi->iobase + CDNS_XSPI_SDMA_SIZE_REG);
329 sdma_trd_info = readl(cdns_xspi->iobase + CDNS_XSPI_SDMA_TRD_INFO_REG);
330 sdma_dir = FIELD_GET(CDNS_XSPI_SDMA_DIR, sdma_trd_info);
333 case CDNS_XSPI_SDMA_DIR_READ:
334 ioread8_rep(cdns_xspi->sdmabase,
335 cdns_xspi->in_buffer, sdma_size);
338 case CDNS_XSPI_SDMA_DIR_WRITE:
339 iowrite8_rep(cdns_xspi->sdmabase,
340 cdns_xspi->out_buffer, sdma_size);
345 static int cdns_xspi_send_stig_command(struct cdns_xspi_dev *cdns_xspi,
346 const struct spi_mem_op *op,
353 ret = cdns_xspi_wait_for_controller_idle(cdns_xspi);
357 writel(FIELD_PREP(CDNS_XSPI_CTRL_WORK_MODE, CDNS_XSPI_WORK_MODE_STIG),
358 cdns_xspi->iobase + CDNS_XSPI_CTRL_CONFIG_REG);
360 cdns_xspi_set_interrupts(cdns_xspi, true);
361 cdns_xspi->sdma_error = false;
363 memset(cmd_regs, 0, sizeof(cmd_regs));
364 cmd_regs[1] = CDNS_XSPI_CMD_FLD_P1_INSTR_CMD_1(op, data_phase);
365 cmd_regs[2] = CDNS_XSPI_CMD_FLD_P1_INSTR_CMD_2(op);
366 cmd_regs[3] = CDNS_XSPI_CMD_FLD_P1_INSTR_CMD_3(op);
367 cmd_regs[4] = CDNS_XSPI_CMD_FLD_P1_INSTR_CMD_4(op,
370 cdns_xspi_trigger_command(cdns_xspi, cmd_regs);
373 cmd_regs[0] = CDNS_XSPI_STIG_DONE_FLAG;
374 cmd_regs[1] = CDNS_XSPI_CMD_FLD_DSEQ_CMD_1(op);
375 cmd_regs[2] = CDNS_XSPI_CMD_FLD_DSEQ_CMD_2(op);
376 cmd_regs[3] = CDNS_XSPI_CMD_FLD_DSEQ_CMD_3(op);
377 cmd_regs[4] = CDNS_XSPI_CMD_FLD_DSEQ_CMD_4(op,
380 cdns_xspi->in_buffer = op->data.buf.in;
381 cdns_xspi->out_buffer = op->data.buf.out;
383 cdns_xspi_trigger_command(cdns_xspi, cmd_regs);
385 wait_for_completion(&cdns_xspi->sdma_complete);
386 if (cdns_xspi->sdma_error) {
387 cdns_xspi_set_interrupts(cdns_xspi, false);
390 cdns_xspi_sdma_handle(cdns_xspi);
393 wait_for_completion(&cdns_xspi->cmd_complete);
394 cdns_xspi_set_interrupts(cdns_xspi, false);
396 cmd_status = cdns_xspi_check_command_status(cdns_xspi);
403 static int cdns_xspi_mem_op(struct cdns_xspi_dev *cdns_xspi,
405 const struct spi_mem_op *op)
407 enum spi_mem_data_dir dir = op->data.dir;
409 if (cdns_xspi->cur_cs != mem->spi->chip_select)
410 cdns_xspi->cur_cs = mem->spi->chip_select;
412 return cdns_xspi_send_stig_command(cdns_xspi, op,
413 (dir != SPI_MEM_NO_DATA));
416 static int cdns_xspi_mem_op_execute(struct spi_mem *mem,
417 const struct spi_mem_op *op)
419 struct cdns_xspi_dev *cdns_xspi =
420 spi_master_get_devdata(mem->spi->master);
423 ret = cdns_xspi_mem_op(cdns_xspi, mem, op);
428 static int cdns_xspi_adjust_mem_op_size(struct spi_mem *mem, struct spi_mem_op *op)
430 struct cdns_xspi_dev *cdns_xspi =
431 spi_master_get_devdata(mem->spi->master);
433 op->data.nbytes = clamp_val(op->data.nbytes, 0, cdns_xspi->sdmasize);
438 static const struct spi_controller_mem_ops cadence_xspi_mem_ops = {
439 .exec_op = cdns_xspi_mem_op_execute,
440 .adjust_op_size = cdns_xspi_adjust_mem_op_size,
443 static irqreturn_t cdns_xspi_irq_handler(int this_irq, void *dev)
445 struct cdns_xspi_dev *cdns_xspi = dev;
447 irqreturn_t result = IRQ_NONE;
449 irq_status = readl(cdns_xspi->iobase + CDNS_XSPI_INTR_STATUS_REG);
450 writel(irq_status, cdns_xspi->iobase + CDNS_XSPI_INTR_STATUS_REG);
453 (CDNS_XSPI_SDMA_ERROR | CDNS_XSPI_SDMA_TRIGGER |
454 CDNS_XSPI_STIG_DONE)) {
455 if (irq_status & CDNS_XSPI_SDMA_ERROR) {
456 dev_err(cdns_xspi->dev,
457 "Slave DMA transaction error\n");
458 cdns_xspi->sdma_error = true;
459 complete(&cdns_xspi->sdma_complete);
462 if (irq_status & CDNS_XSPI_SDMA_TRIGGER)
463 complete(&cdns_xspi->sdma_complete);
465 if (irq_status & CDNS_XSPI_STIG_DONE)
466 complete(&cdns_xspi->cmd_complete);
468 result = IRQ_HANDLED;
471 irq_status = readl(cdns_xspi->iobase + CDNS_XSPI_TRD_COMP_INTR_STATUS);
474 cdns_xspi->iobase + CDNS_XSPI_TRD_COMP_INTR_STATUS);
476 complete(&cdns_xspi->auto_cmd_complete);
478 result = IRQ_HANDLED;
484 static int cdns_xspi_of_get_plat_data(struct platform_device *pdev)
486 struct device_node *node_prop = pdev->dev.of_node;
487 struct device_node *node_child;
490 for_each_child_of_node(node_prop, node_child) {
491 if (!of_device_is_available(node_child))
494 if (of_property_read_u32(node_child, "reg", &cs)) {
495 dev_err(&pdev->dev, "Couldn't get memory chip select\n");
496 of_node_put(node_child);
498 } else if (cs >= CDNS_XSPI_MAX_BANKS) {
499 dev_err(&pdev->dev, "reg (cs) parameter value too large\n");
500 of_node_put(node_child);
508 static void cdns_xspi_print_phy_config(struct cdns_xspi_dev *cdns_xspi)
510 struct device *dev = cdns_xspi->dev;
512 dev_info(dev, "PHY configuration\n");
513 dev_info(dev, " * xspi_dll_phy_ctrl: %08x\n",
514 readl(cdns_xspi->iobase + CDNS_XSPI_DLL_PHY_CTRL));
515 dev_info(dev, " * phy_dq_timing: %08x\n",
516 readl(cdns_xspi->auxbase + CDNS_XSPI_CCP_PHY_DQ_TIMING));
517 dev_info(dev, " * phy_dqs_timing: %08x\n",
518 readl(cdns_xspi->auxbase + CDNS_XSPI_CCP_PHY_DQS_TIMING));
519 dev_info(dev, " * phy_gate_loopback_ctrl: %08x\n",
520 readl(cdns_xspi->auxbase + CDNS_XSPI_CCP_PHY_GATE_LPBCK_CTRL));
521 dev_info(dev, " * phy_dll_slave_ctrl: %08x\n",
522 readl(cdns_xspi->auxbase + CDNS_XSPI_CCP_PHY_DLL_SLAVE_CTRL));
525 static int cdns_xspi_probe(struct platform_device *pdev)
527 struct device *dev = &pdev->dev;
528 struct spi_master *master = NULL;
529 struct cdns_xspi_dev *cdns_xspi = NULL;
530 struct resource *res;
533 master = devm_spi_alloc_master(dev, sizeof(*cdns_xspi));
537 master->mode_bits = SPI_3WIRE | SPI_TX_DUAL | SPI_TX_QUAD |
538 SPI_RX_DUAL | SPI_RX_QUAD | SPI_TX_OCTAL | SPI_RX_OCTAL |
539 SPI_MODE_0 | SPI_MODE_3;
541 master->mem_ops = &cadence_xspi_mem_ops;
542 master->dev.of_node = pdev->dev.of_node;
543 master->bus_num = -1;
545 platform_set_drvdata(pdev, master);
547 cdns_xspi = spi_master_get_devdata(master);
548 cdns_xspi->pdev = pdev;
549 cdns_xspi->dev = &pdev->dev;
550 cdns_xspi->cur_cs = 0;
552 init_completion(&cdns_xspi->cmd_complete);
553 init_completion(&cdns_xspi->auto_cmd_complete);
554 init_completion(&cdns_xspi->sdma_complete);
556 ret = cdns_xspi_of_get_plat_data(pdev);
560 cdns_xspi->iobase = devm_platform_ioremap_resource_byname(pdev, "io");
561 if (IS_ERR(cdns_xspi->iobase)) {
562 dev_err(dev, "Failed to remap controller base address\n");
563 return PTR_ERR(cdns_xspi->iobase);
566 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sdma");
567 cdns_xspi->sdmabase = devm_ioremap_resource(dev, res);
568 if (IS_ERR(cdns_xspi->sdmabase))
569 return PTR_ERR(cdns_xspi->sdmabase);
570 cdns_xspi->sdmasize = resource_size(res);
572 cdns_xspi->auxbase = devm_platform_ioremap_resource_byname(pdev, "aux");
573 if (IS_ERR(cdns_xspi->auxbase)) {
574 dev_err(dev, "Failed to remap AUX address\n");
575 return PTR_ERR(cdns_xspi->auxbase);
578 cdns_xspi->irq = platform_get_irq(pdev, 0);
579 if (cdns_xspi->irq < 0)
582 ret = devm_request_irq(dev, cdns_xspi->irq, cdns_xspi_irq_handler,
583 IRQF_SHARED, pdev->name, cdns_xspi);
585 dev_err(dev, "Failed to request IRQ: %d\n", cdns_xspi->irq);
589 cdns_xspi_print_phy_config(cdns_xspi);
591 ret = cdns_xspi_controller_init(cdns_xspi);
593 dev_err(dev, "Failed to initialize controller\n");
597 master->num_chipselect = 1 << cdns_xspi->hw_num_banks;
599 ret = devm_spi_register_master(dev, master);
601 dev_err(dev, "Failed to register SPI master\n");
605 dev_info(dev, "Successfully registered SPI master\n");
611 static const struct of_device_id cdns_xspi_of_match[] = {
613 .compatible = "cdns,xspi-nor",
615 { /* end of table */}
617 MODULE_DEVICE_TABLE(of, cdns_xspi_of_match);
619 #define cdns_xspi_of_match NULL
620 #endif /* CONFIG_OF */
622 static struct platform_driver cdns_xspi_platform_driver = {
623 .probe = cdns_xspi_probe,
626 .name = CDNS_XSPI_NAME,
627 .of_match_table = cdns_xspi_of_match,
631 module_platform_driver(cdns_xspi_platform_driver);
633 MODULE_DESCRIPTION("Cadence XSPI Controller Driver");
634 MODULE_LICENSE("GPL v2");
635 MODULE_ALIAS("platform:" CDNS_XSPI_NAME);
636 MODULE_AUTHOR("Konrad Kociolek <konrad@cadence.com>");
637 MODULE_AUTHOR("Jayshri Pawar <jpawar@cadence.com>");
638 MODULE_AUTHOR("Parshuram Thombare <pthombar@cadence.com>");