18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Driver for Atmel QSPI Controller
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2015 Atmel Corporation
68c2ecf20Sopenharmony_ci * Copyright (C) 2018 Cryptera A/S
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Author: Cyrille Pitchen <cyrille.pitchen@atmel.com>
98c2ecf20Sopenharmony_ci * Author: Piotr Bugalski <bugalski.piotr@gmail.com>
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * This driver is based on drivers/mtd/spi-nor/fsl-quadspi.c from Freescale.
128c2ecf20Sopenharmony_ci */
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <linux/clk.h>
158c2ecf20Sopenharmony_ci#include <linux/delay.h>
168c2ecf20Sopenharmony_ci#include <linux/err.h>
178c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
188c2ecf20Sopenharmony_ci#include <linux/io.h>
198c2ecf20Sopenharmony_ci#include <linux/kernel.h>
208c2ecf20Sopenharmony_ci#include <linux/module.h>
218c2ecf20Sopenharmony_ci#include <linux/of.h>
228c2ecf20Sopenharmony_ci#include <linux/of_platform.h>
238c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
248c2ecf20Sopenharmony_ci#include <linux/spi/spi-mem.h>
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci/* QSPI register offsets */
278c2ecf20Sopenharmony_ci#define QSPI_CR      0x0000  /* Control Register */
288c2ecf20Sopenharmony_ci#define QSPI_MR      0x0004  /* Mode Register */
298c2ecf20Sopenharmony_ci#define QSPI_RD      0x0008  /* Receive Data Register */
308c2ecf20Sopenharmony_ci#define QSPI_TD      0x000c  /* Transmit Data Register */
318c2ecf20Sopenharmony_ci#define QSPI_SR      0x0010  /* Status Register */
328c2ecf20Sopenharmony_ci#define QSPI_IER     0x0014  /* Interrupt Enable Register */
338c2ecf20Sopenharmony_ci#define QSPI_IDR     0x0018  /* Interrupt Disable Register */
348c2ecf20Sopenharmony_ci#define QSPI_IMR     0x001c  /* Interrupt Mask Register */
358c2ecf20Sopenharmony_ci#define QSPI_SCR     0x0020  /* Serial Clock Register */
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#define QSPI_IAR     0x0030  /* Instruction Address Register */
388c2ecf20Sopenharmony_ci#define QSPI_ICR     0x0034  /* Instruction Code Register */
398c2ecf20Sopenharmony_ci#define QSPI_WICR    0x0034  /* Write Instruction Code Register */
408c2ecf20Sopenharmony_ci#define QSPI_IFR     0x0038  /* Instruction Frame Register */
418c2ecf20Sopenharmony_ci#define QSPI_RICR    0x003C  /* Read Instruction Code Register */
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci#define QSPI_SMR     0x0040  /* Scrambling Mode Register */
448c2ecf20Sopenharmony_ci#define QSPI_SKR     0x0044  /* Scrambling Key Register */
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci#define QSPI_WPMR    0x00E4  /* Write Protection Mode Register */
478c2ecf20Sopenharmony_ci#define QSPI_WPSR    0x00E8  /* Write Protection Status Register */
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci#define QSPI_VERSION 0x00FC  /* Version Register */
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci/* Bitfields in QSPI_CR (Control Register) */
538c2ecf20Sopenharmony_ci#define QSPI_CR_QSPIEN                  BIT(0)
548c2ecf20Sopenharmony_ci#define QSPI_CR_QSPIDIS                 BIT(1)
558c2ecf20Sopenharmony_ci#define QSPI_CR_SWRST                   BIT(7)
568c2ecf20Sopenharmony_ci#define QSPI_CR_LASTXFER                BIT(24)
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci/* Bitfields in QSPI_MR (Mode Register) */
598c2ecf20Sopenharmony_ci#define QSPI_MR_SMM                     BIT(0)
608c2ecf20Sopenharmony_ci#define QSPI_MR_LLB                     BIT(1)
618c2ecf20Sopenharmony_ci#define QSPI_MR_WDRBT                   BIT(2)
628c2ecf20Sopenharmony_ci#define QSPI_MR_SMRM                    BIT(3)
638c2ecf20Sopenharmony_ci#define QSPI_MR_CSMODE_MASK             GENMASK(5, 4)
648c2ecf20Sopenharmony_ci#define QSPI_MR_CSMODE_NOT_RELOADED     (0 << 4)
658c2ecf20Sopenharmony_ci#define QSPI_MR_CSMODE_LASTXFER         (1 << 4)
668c2ecf20Sopenharmony_ci#define QSPI_MR_CSMODE_SYSTEMATICALLY   (2 << 4)
678c2ecf20Sopenharmony_ci#define QSPI_MR_NBBITS_MASK             GENMASK(11, 8)
688c2ecf20Sopenharmony_ci#define QSPI_MR_NBBITS(n)               ((((n) - 8) << 8) & QSPI_MR_NBBITS_MASK)
698c2ecf20Sopenharmony_ci#define QSPI_MR_DLYBCT_MASK             GENMASK(23, 16)
708c2ecf20Sopenharmony_ci#define QSPI_MR_DLYBCT(n)               (((n) << 16) & QSPI_MR_DLYBCT_MASK)
718c2ecf20Sopenharmony_ci#define QSPI_MR_DLYCS_MASK              GENMASK(31, 24)
728c2ecf20Sopenharmony_ci#define QSPI_MR_DLYCS(n)                (((n) << 24) & QSPI_MR_DLYCS_MASK)
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci/* Bitfields in QSPI_SR/QSPI_IER/QSPI_IDR/QSPI_IMR  */
758c2ecf20Sopenharmony_ci#define QSPI_SR_RDRF                    BIT(0)
768c2ecf20Sopenharmony_ci#define QSPI_SR_TDRE                    BIT(1)
778c2ecf20Sopenharmony_ci#define QSPI_SR_TXEMPTY                 BIT(2)
788c2ecf20Sopenharmony_ci#define QSPI_SR_OVRES                   BIT(3)
798c2ecf20Sopenharmony_ci#define QSPI_SR_CSR                     BIT(8)
808c2ecf20Sopenharmony_ci#define QSPI_SR_CSS                     BIT(9)
818c2ecf20Sopenharmony_ci#define QSPI_SR_INSTRE                  BIT(10)
828c2ecf20Sopenharmony_ci#define QSPI_SR_QSPIENS                 BIT(24)
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci#define QSPI_SR_CMD_COMPLETED	(QSPI_SR_INSTRE | QSPI_SR_CSR)
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci/* Bitfields in QSPI_SCR (Serial Clock Register) */
878c2ecf20Sopenharmony_ci#define QSPI_SCR_CPOL                   BIT(0)
888c2ecf20Sopenharmony_ci#define QSPI_SCR_CPHA                   BIT(1)
898c2ecf20Sopenharmony_ci#define QSPI_SCR_SCBR_MASK              GENMASK(15, 8)
908c2ecf20Sopenharmony_ci#define QSPI_SCR_SCBR(n)                (((n) << 8) & QSPI_SCR_SCBR_MASK)
918c2ecf20Sopenharmony_ci#define QSPI_SCR_DLYBS_MASK             GENMASK(23, 16)
928c2ecf20Sopenharmony_ci#define QSPI_SCR_DLYBS(n)               (((n) << 16) & QSPI_SCR_DLYBS_MASK)
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci/* Bitfields in QSPI_ICR (Read/Write Instruction Code Register) */
958c2ecf20Sopenharmony_ci#define QSPI_ICR_INST_MASK              GENMASK(7, 0)
968c2ecf20Sopenharmony_ci#define QSPI_ICR_INST(inst)             (((inst) << 0) & QSPI_ICR_INST_MASK)
978c2ecf20Sopenharmony_ci#define QSPI_ICR_OPT_MASK               GENMASK(23, 16)
988c2ecf20Sopenharmony_ci#define QSPI_ICR_OPT(opt)               (((opt) << 16) & QSPI_ICR_OPT_MASK)
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci/* Bitfields in QSPI_IFR (Instruction Frame Register) */
1018c2ecf20Sopenharmony_ci#define QSPI_IFR_WIDTH_MASK             GENMASK(2, 0)
1028c2ecf20Sopenharmony_ci#define QSPI_IFR_WIDTH_SINGLE_BIT_SPI   (0 << 0)
1038c2ecf20Sopenharmony_ci#define QSPI_IFR_WIDTH_DUAL_OUTPUT      (1 << 0)
1048c2ecf20Sopenharmony_ci#define QSPI_IFR_WIDTH_QUAD_OUTPUT      (2 << 0)
1058c2ecf20Sopenharmony_ci#define QSPI_IFR_WIDTH_DUAL_IO          (3 << 0)
1068c2ecf20Sopenharmony_ci#define QSPI_IFR_WIDTH_QUAD_IO          (4 << 0)
1078c2ecf20Sopenharmony_ci#define QSPI_IFR_WIDTH_DUAL_CMD         (5 << 0)
1088c2ecf20Sopenharmony_ci#define QSPI_IFR_WIDTH_QUAD_CMD         (6 << 0)
1098c2ecf20Sopenharmony_ci#define QSPI_IFR_INSTEN                 BIT(4)
1108c2ecf20Sopenharmony_ci#define QSPI_IFR_ADDREN                 BIT(5)
1118c2ecf20Sopenharmony_ci#define QSPI_IFR_OPTEN                  BIT(6)
1128c2ecf20Sopenharmony_ci#define QSPI_IFR_DATAEN                 BIT(7)
1138c2ecf20Sopenharmony_ci#define QSPI_IFR_OPTL_MASK              GENMASK(9, 8)
1148c2ecf20Sopenharmony_ci#define QSPI_IFR_OPTL_1BIT              (0 << 8)
1158c2ecf20Sopenharmony_ci#define QSPI_IFR_OPTL_2BIT              (1 << 8)
1168c2ecf20Sopenharmony_ci#define QSPI_IFR_OPTL_4BIT              (2 << 8)
1178c2ecf20Sopenharmony_ci#define QSPI_IFR_OPTL_8BIT              (3 << 8)
1188c2ecf20Sopenharmony_ci#define QSPI_IFR_ADDRL                  BIT(10)
1198c2ecf20Sopenharmony_ci#define QSPI_IFR_TFRTYP_MEM		BIT(12)
1208c2ecf20Sopenharmony_ci#define QSPI_IFR_SAMA5D2_WRITE_TRSFR	BIT(13)
1218c2ecf20Sopenharmony_ci#define QSPI_IFR_CRM                    BIT(14)
1228c2ecf20Sopenharmony_ci#define QSPI_IFR_NBDUM_MASK             GENMASK(20, 16)
1238c2ecf20Sopenharmony_ci#define QSPI_IFR_NBDUM(n)               (((n) << 16) & QSPI_IFR_NBDUM_MASK)
1248c2ecf20Sopenharmony_ci#define QSPI_IFR_APBTFRTYP_READ		BIT(24)	/* Defined in SAM9X60 */
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci/* Bitfields in QSPI_SMR (Scrambling Mode Register) */
1278c2ecf20Sopenharmony_ci#define QSPI_SMR_SCREN                  BIT(0)
1288c2ecf20Sopenharmony_ci#define QSPI_SMR_RVDIS                  BIT(1)
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci/* Bitfields in QSPI_WPMR (Write Protection Mode Register) */
1318c2ecf20Sopenharmony_ci#define QSPI_WPMR_WPEN                  BIT(0)
1328c2ecf20Sopenharmony_ci#define QSPI_WPMR_WPKEY_MASK            GENMASK(31, 8)
1338c2ecf20Sopenharmony_ci#define QSPI_WPMR_WPKEY(wpkey)          (((wpkey) << 8) & QSPI_WPMR_WPKEY_MASK)
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci/* Bitfields in QSPI_WPSR (Write Protection Status Register) */
1368c2ecf20Sopenharmony_ci#define QSPI_WPSR_WPVS                  BIT(0)
1378c2ecf20Sopenharmony_ci#define QSPI_WPSR_WPVSRC_MASK           GENMASK(15, 8)
1388c2ecf20Sopenharmony_ci#define QSPI_WPSR_WPVSRC(src)           (((src) << 8) & QSPI_WPSR_WPVSRC)
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_cistruct atmel_qspi_caps {
1418c2ecf20Sopenharmony_ci	bool has_qspick;
1428c2ecf20Sopenharmony_ci	bool has_ricr;
1438c2ecf20Sopenharmony_ci};
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_cistruct atmel_qspi {
1468c2ecf20Sopenharmony_ci	void __iomem		*regs;
1478c2ecf20Sopenharmony_ci	void __iomem		*mem;
1488c2ecf20Sopenharmony_ci	struct clk		*pclk;
1498c2ecf20Sopenharmony_ci	struct clk		*qspick;
1508c2ecf20Sopenharmony_ci	struct platform_device	*pdev;
1518c2ecf20Sopenharmony_ci	const struct atmel_qspi_caps *caps;
1528c2ecf20Sopenharmony_ci	resource_size_t		mmap_size;
1538c2ecf20Sopenharmony_ci	u32			pending;
1548c2ecf20Sopenharmony_ci	u32			mr;
1558c2ecf20Sopenharmony_ci	u32			scr;
1568c2ecf20Sopenharmony_ci	struct completion	cmd_completion;
1578c2ecf20Sopenharmony_ci};
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_cistruct atmel_qspi_mode {
1608c2ecf20Sopenharmony_ci	u8 cmd_buswidth;
1618c2ecf20Sopenharmony_ci	u8 addr_buswidth;
1628c2ecf20Sopenharmony_ci	u8 data_buswidth;
1638c2ecf20Sopenharmony_ci	u32 config;
1648c2ecf20Sopenharmony_ci};
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_cistatic const struct atmel_qspi_mode atmel_qspi_modes[] = {
1678c2ecf20Sopenharmony_ci	{ 1, 1, 1, QSPI_IFR_WIDTH_SINGLE_BIT_SPI },
1688c2ecf20Sopenharmony_ci	{ 1, 1, 2, QSPI_IFR_WIDTH_DUAL_OUTPUT },
1698c2ecf20Sopenharmony_ci	{ 1, 1, 4, QSPI_IFR_WIDTH_QUAD_OUTPUT },
1708c2ecf20Sopenharmony_ci	{ 1, 2, 2, QSPI_IFR_WIDTH_DUAL_IO },
1718c2ecf20Sopenharmony_ci	{ 1, 4, 4, QSPI_IFR_WIDTH_QUAD_IO },
1728c2ecf20Sopenharmony_ci	{ 2, 2, 2, QSPI_IFR_WIDTH_DUAL_CMD },
1738c2ecf20Sopenharmony_ci	{ 4, 4, 4, QSPI_IFR_WIDTH_QUAD_CMD },
1748c2ecf20Sopenharmony_ci};
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci#ifdef VERBOSE_DEBUG
1778c2ecf20Sopenharmony_cistatic const char *atmel_qspi_reg_name(u32 offset, char *tmp, size_t sz)
1788c2ecf20Sopenharmony_ci{
1798c2ecf20Sopenharmony_ci	switch (offset) {
1808c2ecf20Sopenharmony_ci	case QSPI_CR:
1818c2ecf20Sopenharmony_ci		return "CR";
1828c2ecf20Sopenharmony_ci	case QSPI_MR:
1838c2ecf20Sopenharmony_ci		return "MR";
1848c2ecf20Sopenharmony_ci	case QSPI_RD:
1858c2ecf20Sopenharmony_ci		return "MR";
1868c2ecf20Sopenharmony_ci	case QSPI_TD:
1878c2ecf20Sopenharmony_ci		return "TD";
1888c2ecf20Sopenharmony_ci	case QSPI_SR:
1898c2ecf20Sopenharmony_ci		return "SR";
1908c2ecf20Sopenharmony_ci	case QSPI_IER:
1918c2ecf20Sopenharmony_ci		return "IER";
1928c2ecf20Sopenharmony_ci	case QSPI_IDR:
1938c2ecf20Sopenharmony_ci		return "IDR";
1948c2ecf20Sopenharmony_ci	case QSPI_IMR:
1958c2ecf20Sopenharmony_ci		return "IMR";
1968c2ecf20Sopenharmony_ci	case QSPI_SCR:
1978c2ecf20Sopenharmony_ci		return "SCR";
1988c2ecf20Sopenharmony_ci	case QSPI_IAR:
1998c2ecf20Sopenharmony_ci		return "IAR";
2008c2ecf20Sopenharmony_ci	case QSPI_ICR:
2018c2ecf20Sopenharmony_ci		return "ICR/WICR";
2028c2ecf20Sopenharmony_ci	case QSPI_IFR:
2038c2ecf20Sopenharmony_ci		return "IFR";
2048c2ecf20Sopenharmony_ci	case QSPI_RICR:
2058c2ecf20Sopenharmony_ci		return "RICR";
2068c2ecf20Sopenharmony_ci	case QSPI_SMR:
2078c2ecf20Sopenharmony_ci		return "SMR";
2088c2ecf20Sopenharmony_ci	case QSPI_SKR:
2098c2ecf20Sopenharmony_ci		return "SKR";
2108c2ecf20Sopenharmony_ci	case QSPI_WPMR:
2118c2ecf20Sopenharmony_ci		return "WPMR";
2128c2ecf20Sopenharmony_ci	case QSPI_WPSR:
2138c2ecf20Sopenharmony_ci		return "WPSR";
2148c2ecf20Sopenharmony_ci	case QSPI_VERSION:
2158c2ecf20Sopenharmony_ci		return "VERSION";
2168c2ecf20Sopenharmony_ci	default:
2178c2ecf20Sopenharmony_ci		snprintf(tmp, sz, "0x%02x", offset);
2188c2ecf20Sopenharmony_ci		break;
2198c2ecf20Sopenharmony_ci	}
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	return tmp;
2228c2ecf20Sopenharmony_ci}
2238c2ecf20Sopenharmony_ci#endif /* VERBOSE_DEBUG */
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_cistatic u32 atmel_qspi_read(struct atmel_qspi *aq, u32 offset)
2268c2ecf20Sopenharmony_ci{
2278c2ecf20Sopenharmony_ci	u32 value = readl_relaxed(aq->regs + offset);
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci#ifdef VERBOSE_DEBUG
2308c2ecf20Sopenharmony_ci	char tmp[8];
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	dev_vdbg(&aq->pdev->dev, "read 0x%08x from %s\n", value,
2338c2ecf20Sopenharmony_ci		 atmel_qspi_reg_name(offset, tmp, sizeof(tmp)));
2348c2ecf20Sopenharmony_ci#endif /* VERBOSE_DEBUG */
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	return value;
2378c2ecf20Sopenharmony_ci}
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_cistatic void atmel_qspi_write(u32 value, struct atmel_qspi *aq, u32 offset)
2408c2ecf20Sopenharmony_ci{
2418c2ecf20Sopenharmony_ci#ifdef VERBOSE_DEBUG
2428c2ecf20Sopenharmony_ci	char tmp[8];
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	dev_vdbg(&aq->pdev->dev, "write 0x%08x into %s\n", value,
2458c2ecf20Sopenharmony_ci		 atmel_qspi_reg_name(offset, tmp, sizeof(tmp)));
2468c2ecf20Sopenharmony_ci#endif /* VERBOSE_DEBUG */
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci	writel_relaxed(value, aq->regs + offset);
2498c2ecf20Sopenharmony_ci}
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_cistatic inline bool atmel_qspi_is_compatible(const struct spi_mem_op *op,
2528c2ecf20Sopenharmony_ci					    const struct atmel_qspi_mode *mode)
2538c2ecf20Sopenharmony_ci{
2548c2ecf20Sopenharmony_ci	if (op->cmd.buswidth != mode->cmd_buswidth)
2558c2ecf20Sopenharmony_ci		return false;
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	if (op->addr.nbytes && op->addr.buswidth != mode->addr_buswidth)
2588c2ecf20Sopenharmony_ci		return false;
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci	if (op->data.nbytes && op->data.buswidth != mode->data_buswidth)
2618c2ecf20Sopenharmony_ci		return false;
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	return true;
2648c2ecf20Sopenharmony_ci}
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_cistatic int atmel_qspi_find_mode(const struct spi_mem_op *op)
2678c2ecf20Sopenharmony_ci{
2688c2ecf20Sopenharmony_ci	u32 i;
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(atmel_qspi_modes); i++)
2718c2ecf20Sopenharmony_ci		if (atmel_qspi_is_compatible(op, &atmel_qspi_modes[i]))
2728c2ecf20Sopenharmony_ci			return i;
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	return -ENOTSUPP;
2758c2ecf20Sopenharmony_ci}
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_cistatic bool atmel_qspi_supports_op(struct spi_mem *mem,
2788c2ecf20Sopenharmony_ci				   const struct spi_mem_op *op)
2798c2ecf20Sopenharmony_ci{
2808c2ecf20Sopenharmony_ci	if (!spi_mem_default_supports_op(mem, op))
2818c2ecf20Sopenharmony_ci		return false;
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci	if (atmel_qspi_find_mode(op) < 0)
2848c2ecf20Sopenharmony_ci		return false;
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	/* special case not supported by hardware */
2878c2ecf20Sopenharmony_ci	if (op->addr.nbytes == 2 && op->cmd.buswidth != op->addr.buswidth &&
2888c2ecf20Sopenharmony_ci		op->dummy.nbytes == 0)
2898c2ecf20Sopenharmony_ci		return false;
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci	/* DTR ops not supported. */
2928c2ecf20Sopenharmony_ci	if (op->cmd.dtr || op->addr.dtr || op->dummy.dtr || op->data.dtr)
2938c2ecf20Sopenharmony_ci		return false;
2948c2ecf20Sopenharmony_ci	if (op->cmd.nbytes != 1)
2958c2ecf20Sopenharmony_ci		return false;
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	return true;
2988c2ecf20Sopenharmony_ci}
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_cistatic int atmel_qspi_set_cfg(struct atmel_qspi *aq,
3018c2ecf20Sopenharmony_ci			      const struct spi_mem_op *op, u32 *offset)
3028c2ecf20Sopenharmony_ci{
3038c2ecf20Sopenharmony_ci	u32 iar, icr, ifr;
3048c2ecf20Sopenharmony_ci	u32 dummy_cycles = 0;
3058c2ecf20Sopenharmony_ci	int mode;
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	iar = 0;
3088c2ecf20Sopenharmony_ci	icr = QSPI_ICR_INST(op->cmd.opcode);
3098c2ecf20Sopenharmony_ci	ifr = QSPI_IFR_INSTEN;
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	mode = atmel_qspi_find_mode(op);
3128c2ecf20Sopenharmony_ci	if (mode < 0)
3138c2ecf20Sopenharmony_ci		return mode;
3148c2ecf20Sopenharmony_ci	ifr |= atmel_qspi_modes[mode].config;
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci	if (op->dummy.buswidth && op->dummy.nbytes)
3178c2ecf20Sopenharmony_ci		dummy_cycles = op->dummy.nbytes * 8 / op->dummy.buswidth;
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	/*
3208c2ecf20Sopenharmony_ci	 * The controller allows 24 and 32-bit addressing while NAND-flash
3218c2ecf20Sopenharmony_ci	 * requires 16-bit long. Handling 8-bit long addresses is done using
3228c2ecf20Sopenharmony_ci	 * the option field. For the 16-bit addresses, the workaround depends
3238c2ecf20Sopenharmony_ci	 * of the number of requested dummy bits. If there are 8 or more dummy
3248c2ecf20Sopenharmony_ci	 * cycles, the address is shifted and sent with the first dummy byte.
3258c2ecf20Sopenharmony_ci	 * Otherwise opcode is disabled and the first byte of the address
3268c2ecf20Sopenharmony_ci	 * contains the command opcode (works only if the opcode and address
3278c2ecf20Sopenharmony_ci	 * use the same buswidth). The limitation is when the 16-bit address is
3288c2ecf20Sopenharmony_ci	 * used without enough dummy cycles and the opcode is using a different
3298c2ecf20Sopenharmony_ci	 * buswidth than the address.
3308c2ecf20Sopenharmony_ci	 */
3318c2ecf20Sopenharmony_ci	if (op->addr.buswidth) {
3328c2ecf20Sopenharmony_ci		switch (op->addr.nbytes) {
3338c2ecf20Sopenharmony_ci		case 0:
3348c2ecf20Sopenharmony_ci			break;
3358c2ecf20Sopenharmony_ci		case 1:
3368c2ecf20Sopenharmony_ci			ifr |= QSPI_IFR_OPTEN | QSPI_IFR_OPTL_8BIT;
3378c2ecf20Sopenharmony_ci			icr |= QSPI_ICR_OPT(op->addr.val & 0xff);
3388c2ecf20Sopenharmony_ci			break;
3398c2ecf20Sopenharmony_ci		case 2:
3408c2ecf20Sopenharmony_ci			if (dummy_cycles < 8 / op->addr.buswidth) {
3418c2ecf20Sopenharmony_ci				ifr &= ~QSPI_IFR_INSTEN;
3428c2ecf20Sopenharmony_ci				ifr |= QSPI_IFR_ADDREN;
3438c2ecf20Sopenharmony_ci				iar = (op->cmd.opcode << 16) |
3448c2ecf20Sopenharmony_ci					(op->addr.val & 0xffff);
3458c2ecf20Sopenharmony_ci			} else {
3468c2ecf20Sopenharmony_ci				ifr |= QSPI_IFR_ADDREN;
3478c2ecf20Sopenharmony_ci				iar = (op->addr.val << 8) & 0xffffff;
3488c2ecf20Sopenharmony_ci				dummy_cycles -= 8 / op->addr.buswidth;
3498c2ecf20Sopenharmony_ci			}
3508c2ecf20Sopenharmony_ci			break;
3518c2ecf20Sopenharmony_ci		case 3:
3528c2ecf20Sopenharmony_ci			ifr |= QSPI_IFR_ADDREN;
3538c2ecf20Sopenharmony_ci			iar = op->addr.val & 0xffffff;
3548c2ecf20Sopenharmony_ci			break;
3558c2ecf20Sopenharmony_ci		case 4:
3568c2ecf20Sopenharmony_ci			ifr |= QSPI_IFR_ADDREN | QSPI_IFR_ADDRL;
3578c2ecf20Sopenharmony_ci			iar = op->addr.val & 0x7ffffff;
3588c2ecf20Sopenharmony_ci			break;
3598c2ecf20Sopenharmony_ci		default:
3608c2ecf20Sopenharmony_ci			return -ENOTSUPP;
3618c2ecf20Sopenharmony_ci		}
3628c2ecf20Sopenharmony_ci	}
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci	/* offset of the data access in the QSPI memory space */
3658c2ecf20Sopenharmony_ci	*offset = iar;
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci	/* Set number of dummy cycles */
3688c2ecf20Sopenharmony_ci	if (dummy_cycles)
3698c2ecf20Sopenharmony_ci		ifr |= QSPI_IFR_NBDUM(dummy_cycles);
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci	/* Set data enable and data transfer type. */
3728c2ecf20Sopenharmony_ci	if (op->data.nbytes) {
3738c2ecf20Sopenharmony_ci		ifr |= QSPI_IFR_DATAEN;
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci		if (op->addr.nbytes)
3768c2ecf20Sopenharmony_ci			ifr |= QSPI_IFR_TFRTYP_MEM;
3778c2ecf20Sopenharmony_ci	}
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	/*
3808c2ecf20Sopenharmony_ci	 * If the QSPI controller is set in regular SPI mode, set it in
3818c2ecf20Sopenharmony_ci	 * Serial Memory Mode (SMM).
3828c2ecf20Sopenharmony_ci	 */
3838c2ecf20Sopenharmony_ci	if (aq->mr != QSPI_MR_SMM) {
3848c2ecf20Sopenharmony_ci		atmel_qspi_write(QSPI_MR_SMM, aq, QSPI_MR);
3858c2ecf20Sopenharmony_ci		aq->mr = QSPI_MR_SMM;
3868c2ecf20Sopenharmony_ci	}
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	/* Clear pending interrupts */
3898c2ecf20Sopenharmony_ci	(void)atmel_qspi_read(aq, QSPI_SR);
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci	if (aq->caps->has_ricr) {
3928c2ecf20Sopenharmony_ci		if (!op->addr.nbytes && op->data.dir == SPI_MEM_DATA_IN)
3938c2ecf20Sopenharmony_ci			ifr |= QSPI_IFR_APBTFRTYP_READ;
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci		/* Set QSPI Instruction Frame registers */
3968c2ecf20Sopenharmony_ci		atmel_qspi_write(iar, aq, QSPI_IAR);
3978c2ecf20Sopenharmony_ci		if (op->data.dir == SPI_MEM_DATA_IN)
3988c2ecf20Sopenharmony_ci			atmel_qspi_write(icr, aq, QSPI_RICR);
3998c2ecf20Sopenharmony_ci		else
4008c2ecf20Sopenharmony_ci			atmel_qspi_write(icr, aq, QSPI_WICR);
4018c2ecf20Sopenharmony_ci		atmel_qspi_write(ifr, aq, QSPI_IFR);
4028c2ecf20Sopenharmony_ci	} else {
4038c2ecf20Sopenharmony_ci		if (op->data.nbytes && op->data.dir == SPI_MEM_DATA_OUT)
4048c2ecf20Sopenharmony_ci			ifr |= QSPI_IFR_SAMA5D2_WRITE_TRSFR;
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci		/* Set QSPI Instruction Frame registers */
4078c2ecf20Sopenharmony_ci		atmel_qspi_write(iar, aq, QSPI_IAR);
4088c2ecf20Sopenharmony_ci		atmel_qspi_write(icr, aq, QSPI_ICR);
4098c2ecf20Sopenharmony_ci		atmel_qspi_write(ifr, aq, QSPI_IFR);
4108c2ecf20Sopenharmony_ci	}
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci	return 0;
4138c2ecf20Sopenharmony_ci}
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_cistatic int atmel_qspi_exec_op(struct spi_mem *mem, const struct spi_mem_op *op)
4168c2ecf20Sopenharmony_ci{
4178c2ecf20Sopenharmony_ci	struct atmel_qspi *aq = spi_controller_get_devdata(mem->spi->master);
4188c2ecf20Sopenharmony_ci	u32 sr, offset;
4198c2ecf20Sopenharmony_ci	int err;
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci	/*
4228c2ecf20Sopenharmony_ci	 * Check if the address exceeds the MMIO window size. An improvement
4238c2ecf20Sopenharmony_ci	 * would be to add support for regular SPI mode and fall back to it
4248c2ecf20Sopenharmony_ci	 * when the flash memories overrun the controller's memory space.
4258c2ecf20Sopenharmony_ci	 */
4268c2ecf20Sopenharmony_ci	if (op->addr.val + op->data.nbytes > aq->mmap_size)
4278c2ecf20Sopenharmony_ci		return -ENOTSUPP;
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ci	err = atmel_qspi_set_cfg(aq, op, &offset);
4308c2ecf20Sopenharmony_ci	if (err)
4318c2ecf20Sopenharmony_ci		return err;
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci	/* Skip to the final steps if there is no data */
4348c2ecf20Sopenharmony_ci	if (op->data.nbytes) {
4358c2ecf20Sopenharmony_ci		/* Dummy read of QSPI_IFR to synchronize APB and AHB accesses */
4368c2ecf20Sopenharmony_ci		(void)atmel_qspi_read(aq, QSPI_IFR);
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_ci		/* Send/Receive data */
4398c2ecf20Sopenharmony_ci		if (op->data.dir == SPI_MEM_DATA_IN)
4408c2ecf20Sopenharmony_ci			memcpy_fromio(op->data.buf.in, aq->mem + offset,
4418c2ecf20Sopenharmony_ci				      op->data.nbytes);
4428c2ecf20Sopenharmony_ci		else
4438c2ecf20Sopenharmony_ci			memcpy_toio(aq->mem + offset, op->data.buf.out,
4448c2ecf20Sopenharmony_ci				    op->data.nbytes);
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci		/* Release the chip-select */
4478c2ecf20Sopenharmony_ci		atmel_qspi_write(QSPI_CR_LASTXFER, aq, QSPI_CR);
4488c2ecf20Sopenharmony_ci	}
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci	/* Poll INSTRuction End status */
4518c2ecf20Sopenharmony_ci	sr = atmel_qspi_read(aq, QSPI_SR);
4528c2ecf20Sopenharmony_ci	if ((sr & QSPI_SR_CMD_COMPLETED) == QSPI_SR_CMD_COMPLETED)
4538c2ecf20Sopenharmony_ci		return err;
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci	/* Wait for INSTRuction End interrupt */
4568c2ecf20Sopenharmony_ci	reinit_completion(&aq->cmd_completion);
4578c2ecf20Sopenharmony_ci	aq->pending = sr & QSPI_SR_CMD_COMPLETED;
4588c2ecf20Sopenharmony_ci	atmel_qspi_write(QSPI_SR_CMD_COMPLETED, aq, QSPI_IER);
4598c2ecf20Sopenharmony_ci	if (!wait_for_completion_timeout(&aq->cmd_completion,
4608c2ecf20Sopenharmony_ci					 msecs_to_jiffies(1000)))
4618c2ecf20Sopenharmony_ci		err = -ETIMEDOUT;
4628c2ecf20Sopenharmony_ci	atmel_qspi_write(QSPI_SR_CMD_COMPLETED, aq, QSPI_IDR);
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ci	return err;
4658c2ecf20Sopenharmony_ci}
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_cistatic const char *atmel_qspi_get_name(struct spi_mem *spimem)
4688c2ecf20Sopenharmony_ci{
4698c2ecf20Sopenharmony_ci	return dev_name(spimem->spi->dev.parent);
4708c2ecf20Sopenharmony_ci}
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_cistatic const struct spi_controller_mem_ops atmel_qspi_mem_ops = {
4738c2ecf20Sopenharmony_ci	.supports_op = atmel_qspi_supports_op,
4748c2ecf20Sopenharmony_ci	.exec_op = atmel_qspi_exec_op,
4758c2ecf20Sopenharmony_ci	.get_name = atmel_qspi_get_name
4768c2ecf20Sopenharmony_ci};
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_cistatic int atmel_qspi_setup(struct spi_device *spi)
4798c2ecf20Sopenharmony_ci{
4808c2ecf20Sopenharmony_ci	struct spi_controller *ctrl = spi->master;
4818c2ecf20Sopenharmony_ci	struct atmel_qspi *aq = spi_controller_get_devdata(ctrl);
4828c2ecf20Sopenharmony_ci	unsigned long src_rate;
4838c2ecf20Sopenharmony_ci	u32 scbr;
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ci	if (ctrl->busy)
4868c2ecf20Sopenharmony_ci		return -EBUSY;
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ci	if (!spi->max_speed_hz)
4898c2ecf20Sopenharmony_ci		return -EINVAL;
4908c2ecf20Sopenharmony_ci
4918c2ecf20Sopenharmony_ci	src_rate = clk_get_rate(aq->pclk);
4928c2ecf20Sopenharmony_ci	if (!src_rate)
4938c2ecf20Sopenharmony_ci		return -EINVAL;
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci	/* Compute the QSPI baudrate */
4968c2ecf20Sopenharmony_ci	scbr = DIV_ROUND_UP(src_rate, spi->max_speed_hz);
4978c2ecf20Sopenharmony_ci	if (scbr > 0)
4988c2ecf20Sopenharmony_ci		scbr--;
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci	aq->scr = QSPI_SCR_SCBR(scbr);
5018c2ecf20Sopenharmony_ci	atmel_qspi_write(aq->scr, aq, QSPI_SCR);
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci	return 0;
5048c2ecf20Sopenharmony_ci}
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_cistatic void atmel_qspi_init(struct atmel_qspi *aq)
5078c2ecf20Sopenharmony_ci{
5088c2ecf20Sopenharmony_ci	/* Reset the QSPI controller */
5098c2ecf20Sopenharmony_ci	atmel_qspi_write(QSPI_CR_SWRST, aq, QSPI_CR);
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ci	/* Set the QSPI controller by default in Serial Memory Mode */
5128c2ecf20Sopenharmony_ci	atmel_qspi_write(QSPI_MR_SMM, aq, QSPI_MR);
5138c2ecf20Sopenharmony_ci	aq->mr = QSPI_MR_SMM;
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_ci	/* Enable the QSPI controller */
5168c2ecf20Sopenharmony_ci	atmel_qspi_write(QSPI_CR_QSPIEN, aq, QSPI_CR);
5178c2ecf20Sopenharmony_ci}
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_cistatic irqreturn_t atmel_qspi_interrupt(int irq, void *dev_id)
5208c2ecf20Sopenharmony_ci{
5218c2ecf20Sopenharmony_ci	struct atmel_qspi *aq = dev_id;
5228c2ecf20Sopenharmony_ci	u32 status, mask, pending;
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci	status = atmel_qspi_read(aq, QSPI_SR);
5258c2ecf20Sopenharmony_ci	mask = atmel_qspi_read(aq, QSPI_IMR);
5268c2ecf20Sopenharmony_ci	pending = status & mask;
5278c2ecf20Sopenharmony_ci
5288c2ecf20Sopenharmony_ci	if (!pending)
5298c2ecf20Sopenharmony_ci		return IRQ_NONE;
5308c2ecf20Sopenharmony_ci
5318c2ecf20Sopenharmony_ci	aq->pending |= pending;
5328c2ecf20Sopenharmony_ci	if ((aq->pending & QSPI_SR_CMD_COMPLETED) == QSPI_SR_CMD_COMPLETED)
5338c2ecf20Sopenharmony_ci		complete(&aq->cmd_completion);
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
5368c2ecf20Sopenharmony_ci}
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_cistatic int atmel_qspi_probe(struct platform_device *pdev)
5398c2ecf20Sopenharmony_ci{
5408c2ecf20Sopenharmony_ci	struct spi_controller *ctrl;
5418c2ecf20Sopenharmony_ci	struct atmel_qspi *aq;
5428c2ecf20Sopenharmony_ci	struct resource *res;
5438c2ecf20Sopenharmony_ci	int irq, err = 0;
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_ci	ctrl = devm_spi_alloc_master(&pdev->dev, sizeof(*aq));
5468c2ecf20Sopenharmony_ci	if (!ctrl)
5478c2ecf20Sopenharmony_ci		return -ENOMEM;
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ci	ctrl->mode_bits = SPI_RX_DUAL | SPI_RX_QUAD | SPI_TX_DUAL | SPI_TX_QUAD;
5508c2ecf20Sopenharmony_ci	ctrl->setup = atmel_qspi_setup;
5518c2ecf20Sopenharmony_ci	ctrl->bus_num = -1;
5528c2ecf20Sopenharmony_ci	ctrl->mem_ops = &atmel_qspi_mem_ops;
5538c2ecf20Sopenharmony_ci	ctrl->num_chipselect = 1;
5548c2ecf20Sopenharmony_ci	ctrl->dev.of_node = pdev->dev.of_node;
5558c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, ctrl);
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_ci	aq = spi_controller_get_devdata(ctrl);
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ci	init_completion(&aq->cmd_completion);
5608c2ecf20Sopenharmony_ci	aq->pdev = pdev;
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_ci	/* Map the registers */
5638c2ecf20Sopenharmony_ci	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "qspi_base");
5648c2ecf20Sopenharmony_ci	aq->regs = devm_ioremap_resource(&pdev->dev, res);
5658c2ecf20Sopenharmony_ci	if (IS_ERR(aq->regs)) {
5668c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "missing registers\n");
5678c2ecf20Sopenharmony_ci		return PTR_ERR(aq->regs);
5688c2ecf20Sopenharmony_ci	}
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ci	/* Map the AHB memory */
5718c2ecf20Sopenharmony_ci	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "qspi_mmap");
5728c2ecf20Sopenharmony_ci	aq->mem = devm_ioremap_resource(&pdev->dev, res);
5738c2ecf20Sopenharmony_ci	if (IS_ERR(aq->mem)) {
5748c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "missing AHB memory\n");
5758c2ecf20Sopenharmony_ci		return PTR_ERR(aq->mem);
5768c2ecf20Sopenharmony_ci	}
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_ci	aq->mmap_size = resource_size(res);
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ci	/* Get the peripheral clock */
5818c2ecf20Sopenharmony_ci	aq->pclk = devm_clk_get(&pdev->dev, "pclk");
5828c2ecf20Sopenharmony_ci	if (IS_ERR(aq->pclk))
5838c2ecf20Sopenharmony_ci		aq->pclk = devm_clk_get(&pdev->dev, NULL);
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ci	if (IS_ERR(aq->pclk)) {
5868c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "missing peripheral clock\n");
5878c2ecf20Sopenharmony_ci		return PTR_ERR(aq->pclk);
5888c2ecf20Sopenharmony_ci	}
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci	/* Enable the peripheral clock */
5918c2ecf20Sopenharmony_ci	err = clk_prepare_enable(aq->pclk);
5928c2ecf20Sopenharmony_ci	if (err) {
5938c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "failed to enable the peripheral clock\n");
5948c2ecf20Sopenharmony_ci		return err;
5958c2ecf20Sopenharmony_ci	}
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_ci	aq->caps = of_device_get_match_data(&pdev->dev);
5988c2ecf20Sopenharmony_ci	if (!aq->caps) {
5998c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Could not retrieve QSPI caps\n");
6008c2ecf20Sopenharmony_ci		err = -EINVAL;
6018c2ecf20Sopenharmony_ci		goto disable_pclk;
6028c2ecf20Sopenharmony_ci	}
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_ci	if (aq->caps->has_qspick) {
6058c2ecf20Sopenharmony_ci		/* Get the QSPI system clock */
6068c2ecf20Sopenharmony_ci		aq->qspick = devm_clk_get(&pdev->dev, "qspick");
6078c2ecf20Sopenharmony_ci		if (IS_ERR(aq->qspick)) {
6088c2ecf20Sopenharmony_ci			dev_err(&pdev->dev, "missing system clock\n");
6098c2ecf20Sopenharmony_ci			err = PTR_ERR(aq->qspick);
6108c2ecf20Sopenharmony_ci			goto disable_pclk;
6118c2ecf20Sopenharmony_ci		}
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_ci		/* Enable the QSPI system clock */
6148c2ecf20Sopenharmony_ci		err = clk_prepare_enable(aq->qspick);
6158c2ecf20Sopenharmony_ci		if (err) {
6168c2ecf20Sopenharmony_ci			dev_err(&pdev->dev,
6178c2ecf20Sopenharmony_ci				"failed to enable the QSPI system clock\n");
6188c2ecf20Sopenharmony_ci			goto disable_pclk;
6198c2ecf20Sopenharmony_ci		}
6208c2ecf20Sopenharmony_ci	}
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_ci	/* Request the IRQ */
6238c2ecf20Sopenharmony_ci	irq = platform_get_irq(pdev, 0);
6248c2ecf20Sopenharmony_ci	if (irq < 0) {
6258c2ecf20Sopenharmony_ci		err = irq;
6268c2ecf20Sopenharmony_ci		goto disable_qspick;
6278c2ecf20Sopenharmony_ci	}
6288c2ecf20Sopenharmony_ci	err = devm_request_irq(&pdev->dev, irq, atmel_qspi_interrupt,
6298c2ecf20Sopenharmony_ci			       0, dev_name(&pdev->dev), aq);
6308c2ecf20Sopenharmony_ci	if (err)
6318c2ecf20Sopenharmony_ci		goto disable_qspick;
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_ci	atmel_qspi_init(aq);
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci	err = spi_register_controller(ctrl);
6368c2ecf20Sopenharmony_ci	if (err)
6378c2ecf20Sopenharmony_ci		goto disable_qspick;
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_ci	return 0;
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_cidisable_qspick:
6428c2ecf20Sopenharmony_ci	clk_disable_unprepare(aq->qspick);
6438c2ecf20Sopenharmony_cidisable_pclk:
6448c2ecf20Sopenharmony_ci	clk_disable_unprepare(aq->pclk);
6458c2ecf20Sopenharmony_ci
6468c2ecf20Sopenharmony_ci	return err;
6478c2ecf20Sopenharmony_ci}
6488c2ecf20Sopenharmony_ci
6498c2ecf20Sopenharmony_cistatic int atmel_qspi_remove(struct platform_device *pdev)
6508c2ecf20Sopenharmony_ci{
6518c2ecf20Sopenharmony_ci	struct spi_controller *ctrl = platform_get_drvdata(pdev);
6528c2ecf20Sopenharmony_ci	struct atmel_qspi *aq = spi_controller_get_devdata(ctrl);
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_ci	spi_unregister_controller(ctrl);
6558c2ecf20Sopenharmony_ci	atmel_qspi_write(QSPI_CR_QSPIDIS, aq, QSPI_CR);
6568c2ecf20Sopenharmony_ci	clk_disable_unprepare(aq->qspick);
6578c2ecf20Sopenharmony_ci	clk_disable_unprepare(aq->pclk);
6588c2ecf20Sopenharmony_ci	return 0;
6598c2ecf20Sopenharmony_ci}
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_cistatic int __maybe_unused atmel_qspi_suspend(struct device *dev)
6628c2ecf20Sopenharmony_ci{
6638c2ecf20Sopenharmony_ci	struct spi_controller *ctrl = dev_get_drvdata(dev);
6648c2ecf20Sopenharmony_ci	struct atmel_qspi *aq = spi_controller_get_devdata(ctrl);
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_ci	clk_disable_unprepare(aq->qspick);
6678c2ecf20Sopenharmony_ci	clk_disable_unprepare(aq->pclk);
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ci	return 0;
6708c2ecf20Sopenharmony_ci}
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_cistatic int __maybe_unused atmel_qspi_resume(struct device *dev)
6738c2ecf20Sopenharmony_ci{
6748c2ecf20Sopenharmony_ci	struct spi_controller *ctrl = dev_get_drvdata(dev);
6758c2ecf20Sopenharmony_ci	struct atmel_qspi *aq = spi_controller_get_devdata(ctrl);
6768c2ecf20Sopenharmony_ci
6778c2ecf20Sopenharmony_ci	clk_prepare_enable(aq->pclk);
6788c2ecf20Sopenharmony_ci	clk_prepare_enable(aq->qspick);
6798c2ecf20Sopenharmony_ci
6808c2ecf20Sopenharmony_ci	atmel_qspi_init(aq);
6818c2ecf20Sopenharmony_ci
6828c2ecf20Sopenharmony_ci	atmel_qspi_write(aq->scr, aq, QSPI_SCR);
6838c2ecf20Sopenharmony_ci
6848c2ecf20Sopenharmony_ci	return 0;
6858c2ecf20Sopenharmony_ci}
6868c2ecf20Sopenharmony_ci
6878c2ecf20Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(atmel_qspi_pm_ops, atmel_qspi_suspend,
6888c2ecf20Sopenharmony_ci			 atmel_qspi_resume);
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_cistatic const struct atmel_qspi_caps atmel_sama5d2_qspi_caps = {};
6918c2ecf20Sopenharmony_ci
6928c2ecf20Sopenharmony_cistatic const struct atmel_qspi_caps atmel_sam9x60_qspi_caps = {
6938c2ecf20Sopenharmony_ci	.has_qspick = true,
6948c2ecf20Sopenharmony_ci	.has_ricr = true,
6958c2ecf20Sopenharmony_ci};
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_cistatic const struct of_device_id atmel_qspi_dt_ids[] = {
6988c2ecf20Sopenharmony_ci	{
6998c2ecf20Sopenharmony_ci		.compatible = "atmel,sama5d2-qspi",
7008c2ecf20Sopenharmony_ci		.data = &atmel_sama5d2_qspi_caps,
7018c2ecf20Sopenharmony_ci	},
7028c2ecf20Sopenharmony_ci	{
7038c2ecf20Sopenharmony_ci		.compatible = "microchip,sam9x60-qspi",
7048c2ecf20Sopenharmony_ci		.data = &atmel_sam9x60_qspi_caps,
7058c2ecf20Sopenharmony_ci	},
7068c2ecf20Sopenharmony_ci	{ /* sentinel */ }
7078c2ecf20Sopenharmony_ci};
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, atmel_qspi_dt_ids);
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_cistatic struct platform_driver atmel_qspi_driver = {
7128c2ecf20Sopenharmony_ci	.driver = {
7138c2ecf20Sopenharmony_ci		.name	= "atmel_qspi",
7148c2ecf20Sopenharmony_ci		.of_match_table	= atmel_qspi_dt_ids,
7158c2ecf20Sopenharmony_ci		.pm	= &atmel_qspi_pm_ops,
7168c2ecf20Sopenharmony_ci	},
7178c2ecf20Sopenharmony_ci	.probe		= atmel_qspi_probe,
7188c2ecf20Sopenharmony_ci	.remove		= atmel_qspi_remove,
7198c2ecf20Sopenharmony_ci};
7208c2ecf20Sopenharmony_cimodule_platform_driver(atmel_qspi_driver);
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_ciMODULE_AUTHOR("Cyrille Pitchen <cyrille.pitchen@atmel.com>");
7238c2ecf20Sopenharmony_ciMODULE_AUTHOR("Piotr Bugalski <bugalski.piotr@gmail.com");
7248c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Atmel QSPI Controller driver");
7258c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
726