blob: e23d0c53a66440f7297c24aff3616f1178bf4aa0 [file] [log] [blame]
Thomas Gleixner75a6faf2019-06-01 10:08:37 +02001// SPDX-License-Identifier: GPL-2.0-only
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -07002/*
Grant Likelyca632f52011-06-06 01:16:30 -06003 * Memory-mapped interface driver for DW SPI Core
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -07004 *
5 * Copyright (c) 2010, Octasic semiconductor.
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -07006 */
7
8#include <linux/clk.h>
Jamie Iles50c01fc2011-01-11 12:43:52 +00009#include <linux/err.h>
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070010#include <linux/platform_device.h>
Jarkko Nikulab9fc2d22019-10-18 16:21:29 +030011#include <linux/pm_runtime.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070013#include <linux/spi/spi.h>
Grant Likely568a60e2011-02-28 12:47:12 -070014#include <linux/scatterlist.h>
Alexandre Bellonic2c25cc2018-07-27 21:53:56 +020015#include <linux/mfd/syscon.h>
Paul Gortmakerd7614de2011-07-03 15:44:29 -040016#include <linux/module.h>
Steffen Trumtrar22dae172014-06-13 15:36:18 +020017#include <linux/of.h>
Steffen Trumtrar22dae172014-06-13 15:36:18 +020018#include <linux/of_platform.h>
Jay Fang32215a62018-12-03 11:15:50 +080019#include <linux/acpi.h>
Andy Shevchenko98999952015-10-14 23:12:25 +030020#include <linux/property.h>
Alexandre Bellonic2c25cc2018-07-27 21:53:56 +020021#include <linux/regmap.h>
Grant Likely568a60e2011-02-28 12:47:12 -070022
Grant Likelyca632f52011-06-06 01:16:30 -060023#include "spi-dw.h"
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070024
25#define DRIVER_NAME "dw_spi_mmio"
26
27struct dw_spi_mmio {
Jean-Hugues Deschenes0a4c1d72010-01-21 09:55:42 -070028 struct dw_spi dws;
29 struct clk *clk;
Phil Edworthy560ee7e2019-03-19 15:52:07 +000030 struct clk *pclk;
Alexandre Bellonic2c25cc2018-07-27 21:53:56 +020031 void *priv;
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070032};
33
Alexandre Bellonic2c25cc2018-07-27 21:53:56 +020034#define MSCC_CPU_SYSTEM_CTRL_GENERAL_CTRL 0x24
Alexandre Bellonic2c25cc2018-07-27 21:53:56 +020035#define OCELOT_IF_SI_OWNER_OFFSET 4
Alexandre Bellonibe17ee02018-08-29 14:45:48 +020036#define JAGUAR2_IF_SI_OWNER_OFFSET 6
Alexandre Bellonic1d8b082018-08-31 13:40:46 +020037#define MSCC_IF_SI_OWNER_MASK GENMASK(1, 0)
Alexandre Bellonic2c25cc2018-07-27 21:53:56 +020038#define MSCC_IF_SI_OWNER_SISL 0
39#define MSCC_IF_SI_OWNER_SIBM 1
40#define MSCC_IF_SI_OWNER_SIMC 2
41
42#define MSCC_SPI_MST_SW_MODE 0x14
43#define MSCC_SPI_MST_SW_MODE_SW_PIN_CTRL_MODE BIT(13)
44#define MSCC_SPI_MST_SW_MODE_SW_SPI_CS(x) (x << 5)
45
Wan Ahmad Zainief4237792020-05-05 21:06:16 +080046/*
47 * For Keem Bay, CTRLR0[31] is used to select controller mode.
48 * 0: SSI is slave
49 * 1: SSI is master
50 */
51#define KEEMBAY_CTRLR0_SSIC_IS_MST BIT(31)
52
Alexandre Bellonic2c25cc2018-07-27 21:53:56 +020053struct dw_spi_mscc {
54 struct regmap *syscon;
55 void __iomem *spi_mst;
56};
57
58/*
59 * The Designware SPI controller (referred to as master in the documentation)
60 * automatically deasserts chip select when the tx fifo is empty. The chip
61 * selects then needs to be either driven as GPIOs or, for the first 4 using the
62 * the SPI boot controller registers. the final chip select is an OR gate
63 * between the Designware SPI controller and the SPI boot controller.
64 */
65static void dw_spi_mscc_set_cs(struct spi_device *spi, bool enable)
66{
67 struct dw_spi *dws = spi_master_get_devdata(spi->master);
68 struct dw_spi_mmio *dwsmmio = container_of(dws, struct dw_spi_mmio, dws);
69 struct dw_spi_mscc *dwsmscc = dwsmmio->priv;
70 u32 cs = spi->chip_select;
71
72 if (cs < 4) {
73 u32 sw_mode = MSCC_SPI_MST_SW_MODE_SW_PIN_CTRL_MODE;
74
75 if (!enable)
76 sw_mode |= MSCC_SPI_MST_SW_MODE_SW_SPI_CS(BIT(cs));
77
78 writel(sw_mode, dwsmscc->spi_mst + MSCC_SPI_MST_SW_MODE);
79 }
80
81 dw_spi_set_cs(spi, enable);
82}
83
84static int dw_spi_mscc_init(struct platform_device *pdev,
Alexandre Bellonibe17ee02018-08-29 14:45:48 +020085 struct dw_spi_mmio *dwsmmio,
86 const char *cpu_syscon, u32 if_si_owner_offset)
Alexandre Bellonic2c25cc2018-07-27 21:53:56 +020087{
88 struct dw_spi_mscc *dwsmscc;
Alexandre Bellonic2c25cc2018-07-27 21:53:56 +020089
90 dwsmscc = devm_kzalloc(&pdev->dev, sizeof(*dwsmscc), GFP_KERNEL);
91 if (!dwsmscc)
92 return -ENOMEM;
93
YueHaibing5cc6fdc2019-09-04 21:58:54 +080094 dwsmscc->spi_mst = devm_platform_ioremap_resource(pdev, 1);
Alexandre Bellonic2c25cc2018-07-27 21:53:56 +020095 if (IS_ERR(dwsmscc->spi_mst)) {
96 dev_err(&pdev->dev, "SPI_MST region map failed\n");
97 return PTR_ERR(dwsmscc->spi_mst);
98 }
99
Alexandre Bellonibe17ee02018-08-29 14:45:48 +0200100 dwsmscc->syscon = syscon_regmap_lookup_by_compatible(cpu_syscon);
Alexandre Bellonic2c25cc2018-07-27 21:53:56 +0200101 if (IS_ERR(dwsmscc->syscon))
102 return PTR_ERR(dwsmscc->syscon);
103
104 /* Deassert all CS */
105 writel(0, dwsmscc->spi_mst + MSCC_SPI_MST_SW_MODE);
106
107 /* Select the owner of the SI interface */
108 regmap_update_bits(dwsmscc->syscon, MSCC_CPU_SYSTEM_CTRL_GENERAL_CTRL,
Alexandre Bellonic1d8b082018-08-31 13:40:46 +0200109 MSCC_IF_SI_OWNER_MASK << if_si_owner_offset,
Alexandre Bellonibe17ee02018-08-29 14:45:48 +0200110 MSCC_IF_SI_OWNER_SIMC << if_si_owner_offset);
Alexandre Bellonic2c25cc2018-07-27 21:53:56 +0200111
112 dwsmmio->dws.set_cs = dw_spi_mscc_set_cs;
113 dwsmmio->priv = dwsmscc;
114
Wan Ahmad Zainiec4eadee2020-05-05 21:06:13 +0800115 /* Register hook to configure CTRLR0 */
116 dwsmmio->dws.update_cr0 = dw_spi_update_cr0;
117
Alexandre Bellonic2c25cc2018-07-27 21:53:56 +0200118 return 0;
119}
120
Alexandre Bellonibe17ee02018-08-29 14:45:48 +0200121static int dw_spi_mscc_ocelot_init(struct platform_device *pdev,
122 struct dw_spi_mmio *dwsmmio)
123{
124 return dw_spi_mscc_init(pdev, dwsmmio, "mscc,ocelot-cpu-syscon",
125 OCELOT_IF_SI_OWNER_OFFSET);
126}
127
128static int dw_spi_mscc_jaguar2_init(struct platform_device *pdev,
129 struct dw_spi_mmio *dwsmmio)
130{
131 return dw_spi_mscc_init(pdev, dwsmmio, "mscc,jaguar2-cpu-syscon",
132 JAGUAR2_IF_SI_OWNER_OFFSET);
133}
134
Talel Shenharf2d70472018-10-11 14:20:07 +0300135static int dw_spi_alpine_init(struct platform_device *pdev,
136 struct dw_spi_mmio *dwsmmio)
137{
138 dwsmmio->dws.cs_override = 1;
139
Wan Ahmad Zainiec4eadee2020-05-05 21:06:13 +0800140 /* Register hook to configure CTRLR0 */
141 dwsmmio->dws.update_cr0 = dw_spi_update_cr0;
142
143 return 0;
144}
145
146static int dw_spi_dw_apb_init(struct platform_device *pdev,
147 struct dw_spi_mmio *dwsmmio)
148{
149 /* Register hook to configure CTRLR0 */
150 dwsmmio->dws.update_cr0 = dw_spi_update_cr0;
151
Serge Semin0fdad592020-05-29 16:12:03 +0300152 dw_spi_dma_setup_generic(&dwsmmio->dws);
153
Talel Shenharf2d70472018-10-11 14:20:07 +0300154 return 0;
155}
156
Wan Ahmad Zainiee539f432020-05-05 21:06:14 +0800157static int dw_spi_dwc_ssi_init(struct platform_device *pdev,
158 struct dw_spi_mmio *dwsmmio)
159{
160 /* Register hook to configure CTRLR0 */
161 dwsmmio->dws.update_cr0 = dw_spi_update_cr0_v1_01a;
162
Serge Semin0fdad592020-05-29 16:12:03 +0300163 dw_spi_dma_setup_generic(&dwsmmio->dws);
164
Wan Ahmad Zainiee539f432020-05-05 21:06:14 +0800165 return 0;
166}
167
Wan Ahmad Zainief4237792020-05-05 21:06:16 +0800168static u32 dw_spi_update_cr0_keembay(struct spi_controller *master,
169 struct spi_device *spi,
170 struct spi_transfer *transfer)
171{
172 u32 cr0 = dw_spi_update_cr0_v1_01a(master, spi, transfer);
173
174 return cr0 | KEEMBAY_CTRLR0_SSIC_IS_MST;
175}
176
177static int dw_spi_keembay_init(struct platform_device *pdev,
178 struct dw_spi_mmio *dwsmmio)
179{
180 /* Register hook to configure CTRLR0 */
181 dwsmmio->dws.update_cr0 = dw_spi_update_cr0_keembay;
182
183 return 0;
184}
185
Grant Likelyfd4a3192012-12-07 16:57:14 +0000186static int dw_spi_mmio_probe(struct platform_device *pdev)
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700187{
Alexandre Bellonic2c25cc2018-07-27 21:53:56 +0200188 int (*init_func)(struct platform_device *pdev,
189 struct dw_spi_mmio *dwsmmio);
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700190 struct dw_spi_mmio *dwsmmio;
Serge Semin77810d42020-05-15 13:47:50 +0300191 struct resource *mem;
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700192 struct dw_spi *dws;
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700193 int ret;
Steffen Trumtrar22dae172014-06-13 15:36:18 +0200194 int num_cs;
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700195
Baruch Siach04f421e2013-12-30 20:30:44 +0200196 dwsmmio = devm_kzalloc(&pdev->dev, sizeof(struct dw_spi_mmio),
197 GFP_KERNEL);
198 if (!dwsmmio)
199 return -ENOMEM;
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700200
201 dws = &dwsmmio->dws;
202
203 /* Get basic io resource and map it */
Serge Semin77810d42020-05-15 13:47:50 +0300204 dws->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &mem);
Andy Shevchenkoafb7f5652020-05-12 14:03:15 +0300205 if (IS_ERR(dws->regs))
Baruch Siach04f421e2013-12-30 20:30:44 +0200206 return PTR_ERR(dws->regs);
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700207
Serge Semin77810d42020-05-15 13:47:50 +0300208 dws->paddr = mem->start;
209
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700210 dws->irq = platform_get_irq(pdev, 0);
Stephen Boyd6b8ac102019-07-30 11:15:41 -0700211 if (dws->irq < 0)
Baruch Siach04f421e2013-12-30 20:30:44 +0200212 return dws->irq; /* -ENXIO */
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700213
Baruch Siach04f421e2013-12-30 20:30:44 +0200214 dwsmmio->clk = devm_clk_get(&pdev->dev, NULL);
215 if (IS_ERR(dwsmmio->clk))
216 return PTR_ERR(dwsmmio->clk);
Baruch Siach020fe3f2013-12-30 20:30:45 +0200217 ret = clk_prepare_enable(dwsmmio->clk);
Baruch Siach04f421e2013-12-30 20:30:44 +0200218 if (ret)
219 return ret;
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700220
Phil Edworthy560ee7e2019-03-19 15:52:07 +0000221 /* Optional clock needed to access the registers */
222 dwsmmio->pclk = devm_clk_get_optional(&pdev->dev, "pclk");
Andy Shevchenko3da98342019-07-10 14:42:43 +0300223 if (IS_ERR(dwsmmio->pclk)) {
224 ret = PTR_ERR(dwsmmio->pclk);
225 goto out_clk;
226 }
Phil Edworthy560ee7e2019-03-19 15:52:07 +0000227 ret = clk_prepare_enable(dwsmmio->pclk);
228 if (ret)
229 goto out_clk;
230
Baruch Siach2418991e2014-01-26 10:14:32 +0200231 dws->bus_num = pdev->id;
Steffen Trumtrar22dae172014-06-13 15:36:18 +0200232
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700233 dws->max_freq = clk_get_rate(dwsmmio->clk);
234
Andy Shevchenko98999952015-10-14 23:12:25 +0300235 device_property_read_u32(&pdev->dev, "reg-io-width", &dws->reg_io_width);
Michael van der Westhuizenc4fe57f2015-08-18 22:21:53 +0200236
Steffen Trumtrar22dae172014-06-13 15:36:18 +0200237 num_cs = 4;
238
Andy Shevchenko98999952015-10-14 23:12:25 +0300239 device_property_read_u32(&pdev->dev, "num-cs", &num_cs);
Steffen Trumtrar22dae172014-06-13 15:36:18 +0200240
241 dws->num_cs = num_cs;
242
Alexandre Bellonic2c25cc2018-07-27 21:53:56 +0200243 init_func = device_get_match_data(&pdev->dev);
244 if (init_func) {
245 ret = init_func(pdev, dwsmmio);
246 if (ret)
247 goto out;
248 }
249
Jarkko Nikulab9fc2d22019-10-18 16:21:29 +0300250 pm_runtime_enable(&pdev->dev);
251
Baruch Siach04f421e2013-12-30 20:30:44 +0200252 ret = dw_spi_add_host(&pdev->dev, dws);
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700253 if (ret)
Baruch Siach04f421e2013-12-30 20:30:44 +0200254 goto out;
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700255
256 platform_set_drvdata(pdev, dwsmmio);
257 return 0;
258
Baruch Siach04f421e2013-12-30 20:30:44 +0200259out:
Jarkko Nikulab9fc2d22019-10-18 16:21:29 +0300260 pm_runtime_disable(&pdev->dev);
Phil Edworthy560ee7e2019-03-19 15:52:07 +0000261 clk_disable_unprepare(dwsmmio->pclk);
262out_clk:
Baruch Siach020fe3f2013-12-30 20:30:45 +0200263 clk_disable_unprepare(dwsmmio->clk);
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700264 return ret;
265}
266
Grant Likelyfd4a3192012-12-07 16:57:14 +0000267static int dw_spi_mmio_remove(struct platform_device *pdev)
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700268{
269 struct dw_spi_mmio *dwsmmio = platform_get_drvdata(pdev);
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700270
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700271 dw_spi_remove_host(&dwsmmio->dws);
Jarkko Nikulab9fc2d22019-10-18 16:21:29 +0300272 pm_runtime_disable(&pdev->dev);
Phil Edworthy560ee7e2019-03-19 15:52:07 +0000273 clk_disable_unprepare(dwsmmio->pclk);
Marek Vasut400c18e2017-04-18 20:09:06 +0200274 clk_disable_unprepare(dwsmmio->clk);
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700275
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700276 return 0;
277}
278
Steffen Trumtrar22dae172014-06-13 15:36:18 +0200279static const struct of_device_id dw_spi_mmio_of_match[] = {
Wan Ahmad Zainiec4eadee2020-05-05 21:06:13 +0800280 { .compatible = "snps,dw-apb-ssi", .data = dw_spi_dw_apb_init},
Alexandre Bellonibe17ee02018-08-29 14:45:48 +0200281 { .compatible = "mscc,ocelot-spi", .data = dw_spi_mscc_ocelot_init},
282 { .compatible = "mscc,jaguar2-spi", .data = dw_spi_mscc_jaguar2_init},
Talel Shenharf2d70472018-10-11 14:20:07 +0300283 { .compatible = "amazon,alpine-dw-apb-ssi", .data = dw_spi_alpine_init},
Wan Ahmad Zainiec4eadee2020-05-05 21:06:13 +0800284 { .compatible = "renesas,rzn1-spi", .data = dw_spi_dw_apb_init},
Wan Ahmad Zainiee539f432020-05-05 21:06:14 +0800285 { .compatible = "snps,dwc-ssi-1.01a", .data = dw_spi_dwc_ssi_init},
Wan Ahmad Zainief4237792020-05-05 21:06:16 +0800286 { .compatible = "intel,keembay-ssi", .data = dw_spi_keembay_init},
Steffen Trumtrar22dae172014-06-13 15:36:18 +0200287 { /* end of table */}
288};
289MODULE_DEVICE_TABLE(of, dw_spi_mmio_of_match);
290
Jay Fang4dd227a2020-05-09 10:29:51 +0800291#ifdef CONFIG_ACPI
Jay Fang32215a62018-12-03 11:15:50 +0800292static const struct acpi_device_id dw_spi_mmio_acpi_match[] = {
Wan Ahmad Zainiec4eadee2020-05-05 21:06:13 +0800293 {"HISI0173", (kernel_ulong_t)dw_spi_dw_apb_init},
Jay Fang32215a62018-12-03 11:15:50 +0800294 {},
295};
296MODULE_DEVICE_TABLE(acpi, dw_spi_mmio_acpi_match);
Jay Fang4dd227a2020-05-09 10:29:51 +0800297#endif
Jay Fang32215a62018-12-03 11:15:50 +0800298
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700299static struct platform_driver dw_spi_mmio_driver = {
Grant Likely940ab882011-10-05 11:29:49 -0600300 .probe = dw_spi_mmio_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +0000301 .remove = dw_spi_mmio_remove,
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700302 .driver = {
303 .name = DRIVER_NAME,
Steffen Trumtrar22dae172014-06-13 15:36:18 +0200304 .of_match_table = dw_spi_mmio_of_match,
Jay Fang32215a62018-12-03 11:15:50 +0800305 .acpi_match_table = ACPI_PTR(dw_spi_mmio_acpi_match),
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700306 },
307};
Grant Likely940ab882011-10-05 11:29:49 -0600308module_platform_driver(dw_spi_mmio_driver);
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700309
310MODULE_AUTHOR("Jean-Hugues Deschenes <jean-hugues.deschenes@octasic.com>");
311MODULE_DESCRIPTION("Memory-mapped I/O interface driver for DW SPI Core");
312MODULE_LICENSE("GPL v2");