175a6faf6SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2ca632f55SGrant Likely /*
3ca632f55SGrant Likely * Memory-mapped interface driver for DW SPI Core
4ca632f55SGrant Likely *
5ca632f55SGrant Likely * Copyright (c) 2010, Octasic semiconductor.
6ca632f55SGrant Likely */
7ca632f55SGrant Likely
8ca632f55SGrant Likely #include <linux/clk.h>
9ca632f55SGrant Likely #include <linux/err.h>
10ca632f55SGrant Likely #include <linux/platform_device.h>
11b9fc2d20SJarkko Nikula #include <linux/pm_runtime.h>
12ca632f55SGrant Likely #include <linux/slab.h>
13ca632f55SGrant Likely #include <linux/spi/spi.h>
14ca632f55SGrant Likely #include <linux/scatterlist.h>
15c2c25cc3SAlexandre Belloni #include <linux/mfd/syscon.h>
16d7614de4SPaul Gortmaker #include <linux/module.h>
1722dae17eSSteffen Trumtrar #include <linux/of.h>
1822dae17eSSteffen Trumtrar #include <linux/of_platform.h>
1932215a6cSJay Fang #include <linux/acpi.h>
209899995eSAndy Shevchenko #include <linux/property.h>
21c2c25cc3SAlexandre Belloni #include <linux/regmap.h>
227830c0efSDinh Nguyen #include <linux/reset.h>
23ca632f55SGrant Likely
24ca632f55SGrant Likely #include "spi-dw.h"
25ca632f55SGrant Likely
26ca632f55SGrant Likely #define DRIVER_NAME "dw_spi_mmio"
27ca632f55SGrant Likely
28ca632f55SGrant Likely struct dw_spi_mmio {
29ca632f55SGrant Likely struct dw_spi dws;
30ca632f55SGrant Likely struct clk *clk;
31560ee7e9SPhil Edworthy struct clk *pclk;
32c2c25cc3SAlexandre Belloni void *priv;
337830c0efSDinh Nguyen struct reset_control *rstc;
34ca632f55SGrant Likely };
35ca632f55SGrant Likely
36c2c25cc3SAlexandre Belloni #define MSCC_CPU_SYSTEM_CTRL_GENERAL_CTRL 0x24
37c2c25cc3SAlexandre Belloni #define OCELOT_IF_SI_OWNER_OFFSET 4
38be17ee0dSAlexandre Belloni #define JAGUAR2_IF_SI_OWNER_OFFSET 6
39c1d8b082SAlexandre Belloni #define MSCC_IF_SI_OWNER_MASK GENMASK(1, 0)
40c2c25cc3SAlexandre Belloni #define MSCC_IF_SI_OWNER_SISL 0
41c2c25cc3SAlexandre Belloni #define MSCC_IF_SI_OWNER_SIBM 1
42c2c25cc3SAlexandre Belloni #define MSCC_IF_SI_OWNER_SIMC 2
43c2c25cc3SAlexandre Belloni
44c2c25cc3SAlexandre Belloni #define MSCC_SPI_MST_SW_MODE 0x14
45c2c25cc3SAlexandre Belloni #define MSCC_SPI_MST_SW_MODE_SW_PIN_CTRL_MODE BIT(13)
46c2c25cc3SAlexandre Belloni #define MSCC_SPI_MST_SW_MODE_SW_SPI_CS(x) (x << 5)
47c2c25cc3SAlexandre Belloni
4853a09635SLars Povlsen #define SPARX5_FORCE_ENA 0xa4
4953a09635SLars Povlsen #define SPARX5_FORCE_VAL 0xa8
5053a09635SLars Povlsen
51c2c25cc3SAlexandre Belloni struct dw_spi_mscc {
52c2c25cc3SAlexandre Belloni struct regmap *syscon;
5353a09635SLars Povlsen void __iomem *spi_mst; /* Not sparx5 */
54c2c25cc3SAlexandre Belloni };
55c2c25cc3SAlexandre Belloni
56c2c25cc3SAlexandre Belloni /*
572c860604SBrad Larson * Elba SoC does not use ssi, pin override is used for cs 0,1 and
582c860604SBrad Larson * gpios for cs 2,3 as defined in the device tree.
592c860604SBrad Larson *
602c860604SBrad Larson * cs: | 1 0
612c860604SBrad Larson * bit: |---3-------2-------1-------0
622c860604SBrad Larson * | cs1 cs1_ovr cs0 cs0_ovr
632c860604SBrad Larson */
642c860604SBrad Larson #define ELBA_SPICS_REG 0x2468
652c860604SBrad Larson #define ELBA_SPICS_OFFSET(cs) ((cs) << 1)
662c860604SBrad Larson #define ELBA_SPICS_MASK(cs) (GENMASK(1, 0) << ELBA_SPICS_OFFSET(cs))
672c860604SBrad Larson #define ELBA_SPICS_SET(cs, val) \
682c860604SBrad Larson ((((val) << 1) | BIT(0)) << ELBA_SPICS_OFFSET(cs))
692c860604SBrad Larson
702c860604SBrad Larson /*
71*ecd02b6dSYang Yingliang * The Designware SPI controller (referred to as master in the documentation)
72c2c25cc3SAlexandre Belloni * automatically deasserts chip select when the tx fifo is empty. The chip
73db56d030SJay Fang * selects then needs to be either driven as GPIOs or, for the first 4 using
74c2c25cc3SAlexandre Belloni * the SPI boot controller registers. the final chip select is an OR gate
75c2c25cc3SAlexandre Belloni * between the Designware SPI controller and the SPI boot controller.
76c2c25cc3SAlexandre Belloni */
dw_spi_mscc_set_cs(struct spi_device * spi,bool enable)77c2c25cc3SAlexandre Belloni static void dw_spi_mscc_set_cs(struct spi_device *spi, bool enable)
78c2c25cc3SAlexandre Belloni {
79eefc6c5cSYang Yingliang struct dw_spi *dws = spi_controller_get_devdata(spi->controller);
80c2c25cc3SAlexandre Belloni struct dw_spi_mmio *dwsmmio = container_of(dws, struct dw_spi_mmio, dws);
81c2c25cc3SAlexandre Belloni struct dw_spi_mscc *dwsmscc = dwsmmio->priv;
829e264f3fSAmit Kumar Mahapatra via Alsa-devel u32 cs = spi_get_chipselect(spi, 0);
83c2c25cc3SAlexandre Belloni
84c2c25cc3SAlexandre Belloni if (cs < 4) {
85c2c25cc3SAlexandre Belloni u32 sw_mode = MSCC_SPI_MST_SW_MODE_SW_PIN_CTRL_MODE;
86c2c25cc3SAlexandre Belloni
87c2c25cc3SAlexandre Belloni if (!enable)
88c2c25cc3SAlexandre Belloni sw_mode |= MSCC_SPI_MST_SW_MODE_SW_SPI_CS(BIT(cs));
89c2c25cc3SAlexandre Belloni
90c2c25cc3SAlexandre Belloni writel(sw_mode, dwsmscc->spi_mst + MSCC_SPI_MST_SW_MODE);
91c2c25cc3SAlexandre Belloni }
92c2c25cc3SAlexandre Belloni
93c2c25cc3SAlexandre Belloni dw_spi_set_cs(spi, enable);
94c2c25cc3SAlexandre Belloni }
95c2c25cc3SAlexandre Belloni
dw_spi_mscc_init(struct platform_device * pdev,struct dw_spi_mmio * dwsmmio,const char * cpu_syscon,u32 if_si_owner_offset)96c2c25cc3SAlexandre Belloni static int dw_spi_mscc_init(struct platform_device *pdev,
97be17ee0dSAlexandre Belloni struct dw_spi_mmio *dwsmmio,
98be17ee0dSAlexandre Belloni const char *cpu_syscon, u32 if_si_owner_offset)
99c2c25cc3SAlexandre Belloni {
100c2c25cc3SAlexandre Belloni struct dw_spi_mscc *dwsmscc;
101c2c25cc3SAlexandre Belloni
102c2c25cc3SAlexandre Belloni dwsmscc = devm_kzalloc(&pdev->dev, sizeof(*dwsmscc), GFP_KERNEL);
103c2c25cc3SAlexandre Belloni if (!dwsmscc)
104c2c25cc3SAlexandre Belloni return -ENOMEM;
105c2c25cc3SAlexandre Belloni
1065cc6fdccSYueHaibing dwsmscc->spi_mst = devm_platform_ioremap_resource(pdev, 1);
107c2c25cc3SAlexandre Belloni if (IS_ERR(dwsmscc->spi_mst)) {
108c2c25cc3SAlexandre Belloni dev_err(&pdev->dev, "SPI_MST region map failed\n");
109c2c25cc3SAlexandre Belloni return PTR_ERR(dwsmscc->spi_mst);
110c2c25cc3SAlexandre Belloni }
111c2c25cc3SAlexandre Belloni
112be17ee0dSAlexandre Belloni dwsmscc->syscon = syscon_regmap_lookup_by_compatible(cpu_syscon);
113c2c25cc3SAlexandre Belloni if (IS_ERR(dwsmscc->syscon))
114c2c25cc3SAlexandre Belloni return PTR_ERR(dwsmscc->syscon);
115c2c25cc3SAlexandre Belloni
116c2c25cc3SAlexandre Belloni /* Deassert all CS */
117c2c25cc3SAlexandre Belloni writel(0, dwsmscc->spi_mst + MSCC_SPI_MST_SW_MODE);
118c2c25cc3SAlexandre Belloni
119c2c25cc3SAlexandre Belloni /* Select the owner of the SI interface */
120c2c25cc3SAlexandre Belloni regmap_update_bits(dwsmscc->syscon, MSCC_CPU_SYSTEM_CTRL_GENERAL_CTRL,
121c1d8b082SAlexandre Belloni MSCC_IF_SI_OWNER_MASK << if_si_owner_offset,
122be17ee0dSAlexandre Belloni MSCC_IF_SI_OWNER_SIMC << if_si_owner_offset);
123c2c25cc3SAlexandre Belloni
124c2c25cc3SAlexandre Belloni dwsmmio->dws.set_cs = dw_spi_mscc_set_cs;
125c2c25cc3SAlexandre Belloni dwsmmio->priv = dwsmscc;
126c2c25cc3SAlexandre Belloni
127c2c25cc3SAlexandre Belloni return 0;
128c2c25cc3SAlexandre Belloni }
129c2c25cc3SAlexandre Belloni
dw_spi_mscc_ocelot_init(struct platform_device * pdev,struct dw_spi_mmio * dwsmmio)130be17ee0dSAlexandre Belloni static int dw_spi_mscc_ocelot_init(struct platform_device *pdev,
131be17ee0dSAlexandre Belloni struct dw_spi_mmio *dwsmmio)
132be17ee0dSAlexandre Belloni {
133be17ee0dSAlexandre Belloni return dw_spi_mscc_init(pdev, dwsmmio, "mscc,ocelot-cpu-syscon",
134be17ee0dSAlexandre Belloni OCELOT_IF_SI_OWNER_OFFSET);
135be17ee0dSAlexandre Belloni }
136be17ee0dSAlexandre Belloni
dw_spi_mscc_jaguar2_init(struct platform_device * pdev,struct dw_spi_mmio * dwsmmio)137be17ee0dSAlexandre Belloni static int dw_spi_mscc_jaguar2_init(struct platform_device *pdev,
138be17ee0dSAlexandre Belloni struct dw_spi_mmio *dwsmmio)
139be17ee0dSAlexandre Belloni {
140be17ee0dSAlexandre Belloni return dw_spi_mscc_init(pdev, dwsmmio, "mscc,jaguar2-cpu-syscon",
141be17ee0dSAlexandre Belloni JAGUAR2_IF_SI_OWNER_OFFSET);
142be17ee0dSAlexandre Belloni }
143be17ee0dSAlexandre Belloni
14453a09635SLars Povlsen /*
145*ecd02b6dSYang Yingliang * The Designware SPI controller (referred to as master in the
14653a09635SLars Povlsen * documentation) automatically deasserts chip select when the tx fifo
14753a09635SLars Povlsen * is empty. The chip selects then needs to be driven by a CS override
14853a09635SLars Povlsen * register. enable is an active low signal.
14953a09635SLars Povlsen */
dw_spi_sparx5_set_cs(struct spi_device * spi,bool enable)15053a09635SLars Povlsen static void dw_spi_sparx5_set_cs(struct spi_device *spi, bool enable)
15153a09635SLars Povlsen {
152eefc6c5cSYang Yingliang struct dw_spi *dws = spi_controller_get_devdata(spi->controller);
15353a09635SLars Povlsen struct dw_spi_mmio *dwsmmio = container_of(dws, struct dw_spi_mmio, dws);
15453a09635SLars Povlsen struct dw_spi_mscc *dwsmscc = dwsmmio->priv;
1559e264f3fSAmit Kumar Mahapatra via Alsa-devel u8 cs = spi_get_chipselect(spi, 0);
15653a09635SLars Povlsen
15753a09635SLars Povlsen if (!enable) {
15853a09635SLars Povlsen /* CS override drive enable */
15953a09635SLars Povlsen regmap_write(dwsmscc->syscon, SPARX5_FORCE_ENA, 1);
16053a09635SLars Povlsen /* Now set CSx enabled */
16153a09635SLars Povlsen regmap_write(dwsmscc->syscon, SPARX5_FORCE_VAL, ~BIT(cs));
16253a09635SLars Povlsen /* Allow settle */
16353a09635SLars Povlsen usleep_range(1, 5);
16453a09635SLars Povlsen } else {
16553a09635SLars Povlsen /* CS value */
16653a09635SLars Povlsen regmap_write(dwsmscc->syscon, SPARX5_FORCE_VAL, ~0);
16753a09635SLars Povlsen /* Allow settle */
16853a09635SLars Povlsen usleep_range(1, 5);
16953a09635SLars Povlsen /* CS override drive disable */
17053a09635SLars Povlsen regmap_write(dwsmscc->syscon, SPARX5_FORCE_ENA, 0);
17153a09635SLars Povlsen }
17253a09635SLars Povlsen
17353a09635SLars Povlsen dw_spi_set_cs(spi, enable);
17453a09635SLars Povlsen }
17553a09635SLars Povlsen
dw_spi_mscc_sparx5_init(struct platform_device * pdev,struct dw_spi_mmio * dwsmmio)17653a09635SLars Povlsen static int dw_spi_mscc_sparx5_init(struct platform_device *pdev,
17753a09635SLars Povlsen struct dw_spi_mmio *dwsmmio)
17853a09635SLars Povlsen {
17953a09635SLars Povlsen const char *syscon_name = "microchip,sparx5-cpu-syscon";
18053a09635SLars Povlsen struct device *dev = &pdev->dev;
18153a09635SLars Povlsen struct dw_spi_mscc *dwsmscc;
18253a09635SLars Povlsen
18353a09635SLars Povlsen if (!IS_ENABLED(CONFIG_SPI_MUX)) {
18453a09635SLars Povlsen dev_err(dev, "This driver needs CONFIG_SPI_MUX\n");
18553a09635SLars Povlsen return -EOPNOTSUPP;
18653a09635SLars Povlsen }
18753a09635SLars Povlsen
18853a09635SLars Povlsen dwsmscc = devm_kzalloc(dev, sizeof(*dwsmscc), GFP_KERNEL);
18953a09635SLars Povlsen if (!dwsmscc)
19053a09635SLars Povlsen return -ENOMEM;
19153a09635SLars Povlsen
19253a09635SLars Povlsen dwsmscc->syscon =
19353a09635SLars Povlsen syscon_regmap_lookup_by_compatible(syscon_name);
19453a09635SLars Povlsen if (IS_ERR(dwsmscc->syscon)) {
19553a09635SLars Povlsen dev_err(dev, "No syscon map %s\n", syscon_name);
19653a09635SLars Povlsen return PTR_ERR(dwsmscc->syscon);
19753a09635SLars Povlsen }
19853a09635SLars Povlsen
19953a09635SLars Povlsen dwsmmio->dws.set_cs = dw_spi_sparx5_set_cs;
20053a09635SLars Povlsen dwsmmio->priv = dwsmscc;
20153a09635SLars Povlsen
20253a09635SLars Povlsen return 0;
20353a09635SLars Povlsen }
20453a09635SLars Povlsen
dw_spi_alpine_init(struct platform_device * pdev,struct dw_spi_mmio * dwsmmio)205f2d70479STalel Shenhar static int dw_spi_alpine_init(struct platform_device *pdev,
206f2d70479STalel Shenhar struct dw_spi_mmio *dwsmmio)
207f2d70479STalel Shenhar {
208cc760f31SSerge Semin dwsmmio->dws.caps = DW_SPI_CAP_CS_OVERRIDE;
209f2d70479STalel Shenhar
210c4eadee2SWan Ahmad Zainie return 0;
211c4eadee2SWan Ahmad Zainie }
212c4eadee2SWan Ahmad Zainie
dw_spi_pssi_init(struct platform_device * pdev,struct dw_spi_mmio * dwsmmio)213725b0e3eSSerge Semin static int dw_spi_pssi_init(struct platform_device *pdev,
214c4eadee2SWan Ahmad Zainie struct dw_spi_mmio *dwsmmio)
215c4eadee2SWan Ahmad Zainie {
2160fdad596SSerge Semin dw_spi_dma_setup_generic(&dwsmmio->dws);
2170fdad596SSerge Semin
218f2d70479STalel Shenhar return 0;
219f2d70479STalel Shenhar }
220f2d70479STalel Shenhar
dw_spi_hssi_init(struct platform_device * pdev,struct dw_spi_mmio * dwsmmio)221725b0e3eSSerge Semin static int dw_spi_hssi_init(struct platform_device *pdev,
222e539f435SWan Ahmad Zainie struct dw_spi_mmio *dwsmmio)
223e539f435SWan Ahmad Zainie {
2242b8a47e0SSerge Semin dwsmmio->dws.ip = DW_HSSI_ID;
225e539f435SWan Ahmad Zainie
2260fdad596SSerge Semin dw_spi_dma_setup_generic(&dwsmmio->dws);
2270fdad596SSerge Semin
228e539f435SWan Ahmad Zainie return 0;
229e539f435SWan Ahmad Zainie }
230e539f435SWan Ahmad Zainie
dw_spi_intel_init(struct platform_device * pdev,struct dw_spi_mmio * dwsmmio)231dc4e6d9fSNandhini Srikandan static int dw_spi_intel_init(struct platform_device *pdev,
232f4237791SWan Ahmad Zainie struct dw_spi_mmio *dwsmmio)
233f4237791SWan Ahmad Zainie {
2342b8a47e0SSerge Semin dwsmmio->dws.ip = DW_HSSI_ID;
235f4237791SWan Ahmad Zainie
236f4237791SWan Ahmad Zainie return 0;
237f4237791SWan Ahmad Zainie }
238f4237791SWan Ahmad Zainie
2390760d5d0SAbe Kohandel /*
2405b6d0b91SAbe Kohandel * DMA-based mem ops are not configured for this device and are not tested.
2410760d5d0SAbe Kohandel */
dw_spi_mountevans_imc_init(struct platform_device * pdev,struct dw_spi_mmio * dwsmmio)2420760d5d0SAbe Kohandel static int dw_spi_mountevans_imc_init(struct platform_device *pdev,
2430760d5d0SAbe Kohandel struct dw_spi_mmio *dwsmmio)
2440760d5d0SAbe Kohandel {
2450760d5d0SAbe Kohandel /*
2460760d5d0SAbe Kohandel * The Intel Mount Evans SoC's Integrated Management Complex DW
2470760d5d0SAbe Kohandel * apb_ssi_v4.02a controller has an errata where a full TX FIFO can
2480760d5d0SAbe Kohandel * result in data corruption. The suggested workaround is to never
2490760d5d0SAbe Kohandel * completely fill the FIFO. The TX FIFO has a size of 32 so the
2500760d5d0SAbe Kohandel * fifo_len is set to 31.
2510760d5d0SAbe Kohandel */
2520760d5d0SAbe Kohandel dwsmmio->dws.fifo_len = 31;
2530760d5d0SAbe Kohandel
2540760d5d0SAbe Kohandel return 0;
2550760d5d0SAbe Kohandel }
2560760d5d0SAbe Kohandel
dw_spi_canaan_k210_init(struct platform_device * pdev,struct dw_spi_mmio * dwsmmio)257b0dfd948SDamien Le Moal static int dw_spi_canaan_k210_init(struct platform_device *pdev,
258b0dfd948SDamien Le Moal struct dw_spi_mmio *dwsmmio)
259b0dfd948SDamien Le Moal {
260b0dfd948SDamien Le Moal /*
261b0dfd948SDamien Le Moal * The Canaan Kendryte K210 SoC DW apb_ssi v4 spi controller is
262b0dfd948SDamien Le Moal * documented to have a 32 word deep TX and RX FIFO, which
263b0dfd948SDamien Le Moal * spi_hw_init() detects. However, when the RX FIFO is filled up to
264b0dfd948SDamien Le Moal * 32 entries (RXFLR = 32), an RX FIFO overrun error occurs. Avoid this
265b0dfd948SDamien Le Moal * problem by force setting fifo_len to 31.
266b0dfd948SDamien Le Moal */
267b0dfd948SDamien Le Moal dwsmmio->dws.fifo_len = 31;
268b0dfd948SDamien Le Moal
269b0dfd948SDamien Le Moal return 0;
270b0dfd948SDamien Le Moal }
271b0dfd948SDamien Le Moal
dw_spi_elba_override_cs(struct regmap * syscon,int cs,int enable)2722c860604SBrad Larson static void dw_spi_elba_override_cs(struct regmap *syscon, int cs, int enable)
2732c860604SBrad Larson {
2742c860604SBrad Larson regmap_update_bits(syscon, ELBA_SPICS_REG, ELBA_SPICS_MASK(cs),
2752c860604SBrad Larson ELBA_SPICS_SET(cs, enable));
2762c860604SBrad Larson }
2772c860604SBrad Larson
dw_spi_elba_set_cs(struct spi_device * spi,bool enable)2782c860604SBrad Larson static void dw_spi_elba_set_cs(struct spi_device *spi, bool enable)
2792c860604SBrad Larson {
280eefc6c5cSYang Yingliang struct dw_spi *dws = spi_controller_get_devdata(spi->controller);
2812c860604SBrad Larson struct dw_spi_mmio *dwsmmio = container_of(dws, struct dw_spi_mmio, dws);
2822c860604SBrad Larson struct regmap *syscon = dwsmmio->priv;
2832c860604SBrad Larson u8 cs;
2842c860604SBrad Larson
285445164e8SAmit Kumar Mahapatra cs = spi_get_chipselect(spi, 0);
2862c860604SBrad Larson if (cs < 2)
287445164e8SAmit Kumar Mahapatra dw_spi_elba_override_cs(syscon, spi_get_chipselect(spi, 0), enable);
2882c860604SBrad Larson
2892c860604SBrad Larson /*
2902c860604SBrad Larson * The DW SPI controller needs a native CS bit selected to start
2912c860604SBrad Larson * the serial engine.
2922c860604SBrad Larson */
293445164e8SAmit Kumar Mahapatra spi_set_chipselect(spi, 0, 0);
2942c860604SBrad Larson dw_spi_set_cs(spi, enable);
295eee43699SAbe Kohandel spi_set_chipselect(spi, 0, cs);
2962c860604SBrad Larson }
2972c860604SBrad Larson
dw_spi_elba_init(struct platform_device * pdev,struct dw_spi_mmio * dwsmmio)2982c860604SBrad Larson static int dw_spi_elba_init(struct platform_device *pdev,
2992c860604SBrad Larson struct dw_spi_mmio *dwsmmio)
3002c860604SBrad Larson {
3012c860604SBrad Larson struct regmap *syscon;
3022c860604SBrad Larson
3032c860604SBrad Larson syscon = syscon_regmap_lookup_by_phandle(dev_of_node(&pdev->dev),
3042c860604SBrad Larson "amd,pensando-elba-syscon");
3052c860604SBrad Larson if (IS_ERR(syscon))
3062c860604SBrad Larson return dev_err_probe(&pdev->dev, PTR_ERR(syscon),
3072c860604SBrad Larson "syscon regmap lookup failed\n");
3082c860604SBrad Larson
3092c860604SBrad Larson dwsmmio->priv = syscon;
3102c860604SBrad Larson dwsmmio->dws.set_cs = dw_spi_elba_set_cs;
3112c860604SBrad Larson
3122c860604SBrad Larson return 0;
3132c860604SBrad Larson }
3142c860604SBrad Larson
dw_spi_mmio_probe(struct platform_device * pdev)315fd4a319bSGrant Likely static int dw_spi_mmio_probe(struct platform_device *pdev)
316ca632f55SGrant Likely {
317c2c25cc3SAlexandre Belloni int (*init_func)(struct platform_device *pdev,
318c2c25cc3SAlexandre Belloni struct dw_spi_mmio *dwsmmio);
319ca632f55SGrant Likely struct dw_spi_mmio *dwsmmio;
32077810d48SSerge Semin struct resource *mem;
321ca632f55SGrant Likely struct dw_spi *dws;
322ca632f55SGrant Likely int ret;
32322dae17eSSteffen Trumtrar int num_cs;
324ca632f55SGrant Likely
32504f421e7SBaruch Siach dwsmmio = devm_kzalloc(&pdev->dev, sizeof(struct dw_spi_mmio),
32604f421e7SBaruch Siach GFP_KERNEL);
32704f421e7SBaruch Siach if (!dwsmmio)
32804f421e7SBaruch Siach return -ENOMEM;
329ca632f55SGrant Likely
330ca632f55SGrant Likely dws = &dwsmmio->dws;
331ca632f55SGrant Likely
332ca632f55SGrant Likely /* Get basic io resource and map it */
33377810d48SSerge Semin dws->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &mem);
334afb7f565SAndy Shevchenko if (IS_ERR(dws->regs))
33504f421e7SBaruch Siach return PTR_ERR(dws->regs);
336ca632f55SGrant Likely
33777810d48SSerge Semin dws->paddr = mem->start;
33877810d48SSerge Semin
339ca632f55SGrant Likely dws->irq = platform_get_irq(pdev, 0);
3406b8ac10eSStephen Boyd if (dws->irq < 0)
34104f421e7SBaruch Siach return dws->irq; /* -ENXIO */
342ca632f55SGrant Likely
34304f421e7SBaruch Siach dwsmmio->clk = devm_clk_get(&pdev->dev, NULL);
34404f421e7SBaruch Siach if (IS_ERR(dwsmmio->clk))
34504f421e7SBaruch Siach return PTR_ERR(dwsmmio->clk);
346020fe3feSBaruch Siach ret = clk_prepare_enable(dwsmmio->clk);
34704f421e7SBaruch Siach if (ret)
34804f421e7SBaruch Siach return ret;
349ca632f55SGrant Likely
350560ee7e9SPhil Edworthy /* Optional clock needed to access the registers */
351560ee7e9SPhil Edworthy dwsmmio->pclk = devm_clk_get_optional(&pdev->dev, "pclk");
3523da9834dSAndy Shevchenko if (IS_ERR(dwsmmio->pclk)) {
3533da9834dSAndy Shevchenko ret = PTR_ERR(dwsmmio->pclk);
3543da9834dSAndy Shevchenko goto out_clk;
3553da9834dSAndy Shevchenko }
356560ee7e9SPhil Edworthy ret = clk_prepare_enable(dwsmmio->pclk);
357560ee7e9SPhil Edworthy if (ret)
358560ee7e9SPhil Edworthy goto out_clk;
359560ee7e9SPhil Edworthy
3607830c0efSDinh Nguyen /* find an optional reset controller */
3617830c0efSDinh Nguyen dwsmmio->rstc = devm_reset_control_get_optional_exclusive(&pdev->dev, "spi");
3627830c0efSDinh Nguyen if (IS_ERR(dwsmmio->rstc)) {
3637830c0efSDinh Nguyen ret = PTR_ERR(dwsmmio->rstc);
3647830c0efSDinh Nguyen goto out_clk;
3657830c0efSDinh Nguyen }
3667830c0efSDinh Nguyen reset_control_deassert(dwsmmio->rstc);
3677830c0efSDinh Nguyen
3682418991eSBaruch Siach dws->bus_num = pdev->id;
36922dae17eSSteffen Trumtrar
370ca632f55SGrant Likely dws->max_freq = clk_get_rate(dwsmmio->clk);
371ca632f55SGrant Likely
3722ca03eccSKunihiko Hayashi if (device_property_read_u32(&pdev->dev, "reg-io-width",
3732ca03eccSKunihiko Hayashi &dws->reg_io_width))
3742ca03eccSKunihiko Hayashi dws->reg_io_width = 4;
375c4fe57f7SMichael van der Westhuizen
37622dae17eSSteffen Trumtrar num_cs = 4;
37722dae17eSSteffen Trumtrar
3789899995eSAndy Shevchenko device_property_read_u32(&pdev->dev, "num-cs", &num_cs);
37922dae17eSSteffen Trumtrar
38022dae17eSSteffen Trumtrar dws->num_cs = num_cs;
38122dae17eSSteffen Trumtrar
382c2c25cc3SAlexandre Belloni init_func = device_get_match_data(&pdev->dev);
383c2c25cc3SAlexandre Belloni if (init_func) {
384c2c25cc3SAlexandre Belloni ret = init_func(pdev, dwsmmio);
385c2c25cc3SAlexandre Belloni if (ret)
386c2c25cc3SAlexandre Belloni goto out;
387c2c25cc3SAlexandre Belloni }
388c2c25cc3SAlexandre Belloni
389b9fc2d20SJarkko Nikula pm_runtime_enable(&pdev->dev);
390b9fc2d20SJarkko Nikula
39104f421e7SBaruch Siach ret = dw_spi_add_host(&pdev->dev, dws);
392ca632f55SGrant Likely if (ret)
39304f421e7SBaruch Siach goto out;
394ca632f55SGrant Likely
395ca632f55SGrant Likely platform_set_drvdata(pdev, dwsmmio);
396ca632f55SGrant Likely return 0;
397ca632f55SGrant Likely
39804f421e7SBaruch Siach out:
399b9fc2d20SJarkko Nikula pm_runtime_disable(&pdev->dev);
400560ee7e9SPhil Edworthy clk_disable_unprepare(dwsmmio->pclk);
401560ee7e9SPhil Edworthy out_clk:
402020fe3feSBaruch Siach clk_disable_unprepare(dwsmmio->clk);
4037830c0efSDinh Nguyen reset_control_assert(dwsmmio->rstc);
4047830c0efSDinh Nguyen
405ca632f55SGrant Likely return ret;
406ca632f55SGrant Likely }
407ca632f55SGrant Likely
dw_spi_mmio_remove(struct platform_device * pdev)408f74abea2SUwe Kleine-König static void dw_spi_mmio_remove(struct platform_device *pdev)
409ca632f55SGrant Likely {
410ca632f55SGrant Likely struct dw_spi_mmio *dwsmmio = platform_get_drvdata(pdev);
411ca632f55SGrant Likely
412ca632f55SGrant Likely dw_spi_remove_host(&dwsmmio->dws);
413b9fc2d20SJarkko Nikula pm_runtime_disable(&pdev->dev);
414560ee7e9SPhil Edworthy clk_disable_unprepare(dwsmmio->pclk);
415400c18e3SMarek Vasut clk_disable_unprepare(dwsmmio->clk);
4167830c0efSDinh Nguyen reset_control_assert(dwsmmio->rstc);
417ca632f55SGrant Likely }
418ca632f55SGrant Likely
41922dae17eSSteffen Trumtrar static const struct of_device_id dw_spi_mmio_of_match[] = {
420725b0e3eSSerge Semin { .compatible = "snps,dw-apb-ssi", .data = dw_spi_pssi_init},
421be17ee0dSAlexandre Belloni { .compatible = "mscc,ocelot-spi", .data = dw_spi_mscc_ocelot_init},
422be17ee0dSAlexandre Belloni { .compatible = "mscc,jaguar2-spi", .data = dw_spi_mscc_jaguar2_init},
423f2d70479STalel Shenhar { .compatible = "amazon,alpine-dw-apb-ssi", .data = dw_spi_alpine_init},
424725b0e3eSSerge Semin { .compatible = "renesas,rzn1-spi", .data = dw_spi_pssi_init},
425725b0e3eSSerge Semin { .compatible = "snps,dwc-ssi-1.01a", .data = dw_spi_hssi_init},
426dc4e6d9fSNandhini Srikandan { .compatible = "intel,keembay-ssi", .data = dw_spi_intel_init},
427dc4e6d9fSNandhini Srikandan { .compatible = "intel,thunderbay-ssi", .data = dw_spi_intel_init},
4280760d5d0SAbe Kohandel {
4290760d5d0SAbe Kohandel .compatible = "intel,mountevans-imc-ssi",
4300760d5d0SAbe Kohandel .data = dw_spi_mountevans_imc_init,
4310760d5d0SAbe Kohandel },
43253a09635SLars Povlsen { .compatible = "microchip,sparx5-spi", dw_spi_mscc_sparx5_init},
433b0dfd948SDamien Le Moal { .compatible = "canaan,k210-spi", dw_spi_canaan_k210_init},
4342c860604SBrad Larson { .compatible = "amd,pensando-elba-spi", .data = dw_spi_elba_init},
43522dae17eSSteffen Trumtrar { /* end of table */}
43622dae17eSSteffen Trumtrar };
43722dae17eSSteffen Trumtrar MODULE_DEVICE_TABLE(of, dw_spi_mmio_of_match);
43822dae17eSSteffen Trumtrar
4394dd227a5SJay Fang #ifdef CONFIG_ACPI
44032215a6cSJay Fang static const struct acpi_device_id dw_spi_mmio_acpi_match[] = {
441725b0e3eSSerge Semin {"HISI0173", (kernel_ulong_t)dw_spi_pssi_init},
44232215a6cSJay Fang {},
44332215a6cSJay Fang };
44432215a6cSJay Fang MODULE_DEVICE_TABLE(acpi, dw_spi_mmio_acpi_match);
4454dd227a5SJay Fang #endif
44632215a6cSJay Fang
447ca632f55SGrant Likely static struct platform_driver dw_spi_mmio_driver = {
448940ab889SGrant Likely .probe = dw_spi_mmio_probe,
449f74abea2SUwe Kleine-König .remove_new = dw_spi_mmio_remove,
450ca632f55SGrant Likely .driver = {
451ca632f55SGrant Likely .name = DRIVER_NAME,
45222dae17eSSteffen Trumtrar .of_match_table = dw_spi_mmio_of_match,
45332215a6cSJay Fang .acpi_match_table = ACPI_PTR(dw_spi_mmio_acpi_match),
454ca632f55SGrant Likely },
455ca632f55SGrant Likely };
456940ab889SGrant Likely module_platform_driver(dw_spi_mmio_driver);
457ca632f55SGrant Likely
458ca632f55SGrant Likely MODULE_AUTHOR("Jean-Hugues Deschenes <jean-hugues.deschenes@octasic.com>");
459ca632f55SGrant Likely MODULE_DESCRIPTION("Memory-mapped I/O interface driver for DW SPI Core");
460ca632f55SGrant Likely MODULE_LICENSE("GPL v2");
461a62bacbaSSerge Semin MODULE_IMPORT_NS(SPI_DW_CORE);
462