blob: fc3577b07a1ee0955cad0df9ce3871bd113b0e0b [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
Talel Shenharf2d70472018-10-11 14:20:07 +0300152 return 0;
153}
154
Wan Ahmad Zainiee539f432020-05-05 21:06:14 +0800155static int dw_spi_dwc_ssi_init(struct platform_device *pdev,
156 struct dw_spi_mmio *dwsmmio)
157{
158 /* Register hook to configure CTRLR0 */
159 dwsmmio->dws.update_cr0 = dw_spi_update_cr0_v1_01a;
160
161 return 0;
162}
163
Wan Ahmad Zainief4237792020-05-05 21:06:16 +0800164static u32 dw_spi_update_cr0_keembay(struct spi_controller *master,
165 struct spi_device *spi,
166 struct spi_transfer *transfer)
167{
168 u32 cr0 = dw_spi_update_cr0_v1_01a(master, spi, transfer);
169
170 return cr0 | KEEMBAY_CTRLR0_SSIC_IS_MST;
171}
172
173static int dw_spi_keembay_init(struct platform_device *pdev,
174 struct dw_spi_mmio *dwsmmio)
175{
176 /* Register hook to configure CTRLR0 */
177 dwsmmio->dws.update_cr0 = dw_spi_update_cr0_keembay;
178
179 return 0;
180}
181
Grant Likelyfd4a3192012-12-07 16:57:14 +0000182static int dw_spi_mmio_probe(struct platform_device *pdev)
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700183{
Alexandre Bellonic2c25cc2018-07-27 21:53:56 +0200184 int (*init_func)(struct platform_device *pdev,
185 struct dw_spi_mmio *dwsmmio);
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700186 struct dw_spi_mmio *dwsmmio;
187 struct dw_spi *dws;
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700188 int ret;
Steffen Trumtrar22dae172014-06-13 15:36:18 +0200189 int num_cs;
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700190
Baruch Siach04f421e2013-12-30 20:30:44 +0200191 dwsmmio = devm_kzalloc(&pdev->dev, sizeof(struct dw_spi_mmio),
192 GFP_KERNEL);
193 if (!dwsmmio)
194 return -ENOMEM;
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700195
196 dws = &dwsmmio->dws;
197
198 /* Get basic io resource and map it */
Andy Shevchenko05210502019-07-10 14:42:30 +0300199 dws->regs = devm_platform_ioremap_resource(pdev, 0);
Baruch Siach04f421e2013-12-30 20:30:44 +0200200 if (IS_ERR(dws->regs)) {
201 dev_err(&pdev->dev, "SPI region map failed\n");
202 return PTR_ERR(dws->regs);
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700203 }
204
205 dws->irq = platform_get_irq(pdev, 0);
Stephen Boyd6b8ac102019-07-30 11:15:41 -0700206 if (dws->irq < 0)
Baruch Siach04f421e2013-12-30 20:30:44 +0200207 return dws->irq; /* -ENXIO */
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700208
Baruch Siach04f421e2013-12-30 20:30:44 +0200209 dwsmmio->clk = devm_clk_get(&pdev->dev, NULL);
210 if (IS_ERR(dwsmmio->clk))
211 return PTR_ERR(dwsmmio->clk);
Baruch Siach020fe3f2013-12-30 20:30:45 +0200212 ret = clk_prepare_enable(dwsmmio->clk);
Baruch Siach04f421e2013-12-30 20:30:44 +0200213 if (ret)
214 return ret;
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700215
Phil Edworthy560ee7e2019-03-19 15:52:07 +0000216 /* Optional clock needed to access the registers */
217 dwsmmio->pclk = devm_clk_get_optional(&pdev->dev, "pclk");
Andy Shevchenko3da98342019-07-10 14:42:43 +0300218 if (IS_ERR(dwsmmio->pclk)) {
219 ret = PTR_ERR(dwsmmio->pclk);
220 goto out_clk;
221 }
Phil Edworthy560ee7e2019-03-19 15:52:07 +0000222 ret = clk_prepare_enable(dwsmmio->pclk);
223 if (ret)
224 goto out_clk;
225
Baruch Siach2418991e2014-01-26 10:14:32 +0200226 dws->bus_num = pdev->id;
Steffen Trumtrar22dae172014-06-13 15:36:18 +0200227
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700228 dws->max_freq = clk_get_rate(dwsmmio->clk);
229
Andy Shevchenko98999952015-10-14 23:12:25 +0300230 device_property_read_u32(&pdev->dev, "reg-io-width", &dws->reg_io_width);
Michael van der Westhuizenc4fe57f2015-08-18 22:21:53 +0200231
Steffen Trumtrar22dae172014-06-13 15:36:18 +0200232 num_cs = 4;
233
Andy Shevchenko98999952015-10-14 23:12:25 +0300234 device_property_read_u32(&pdev->dev, "num-cs", &num_cs);
Steffen Trumtrar22dae172014-06-13 15:36:18 +0200235
236 dws->num_cs = num_cs;
237
Alexandre Bellonic2c25cc2018-07-27 21:53:56 +0200238 init_func = device_get_match_data(&pdev->dev);
239 if (init_func) {
240 ret = init_func(pdev, dwsmmio);
241 if (ret)
242 goto out;
243 }
244
Jarkko Nikulab9fc2d22019-10-18 16:21:29 +0300245 pm_runtime_enable(&pdev->dev);
246
Baruch Siach04f421e2013-12-30 20:30:44 +0200247 ret = dw_spi_add_host(&pdev->dev, dws);
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700248 if (ret)
Baruch Siach04f421e2013-12-30 20:30:44 +0200249 goto out;
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700250
251 platform_set_drvdata(pdev, dwsmmio);
252 return 0;
253
Baruch Siach04f421e2013-12-30 20:30:44 +0200254out:
Jarkko Nikulab9fc2d22019-10-18 16:21:29 +0300255 pm_runtime_disable(&pdev->dev);
Phil Edworthy560ee7e2019-03-19 15:52:07 +0000256 clk_disable_unprepare(dwsmmio->pclk);
257out_clk:
Baruch Siach020fe3f2013-12-30 20:30:45 +0200258 clk_disable_unprepare(dwsmmio->clk);
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700259 return ret;
260}
261
Grant Likelyfd4a3192012-12-07 16:57:14 +0000262static int dw_spi_mmio_remove(struct platform_device *pdev)
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700263{
264 struct dw_spi_mmio *dwsmmio = platform_get_drvdata(pdev);
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700265
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700266 dw_spi_remove_host(&dwsmmio->dws);
Jarkko Nikulab9fc2d22019-10-18 16:21:29 +0300267 pm_runtime_disable(&pdev->dev);
Phil Edworthy560ee7e2019-03-19 15:52:07 +0000268 clk_disable_unprepare(dwsmmio->pclk);
Marek Vasut400c18e2017-04-18 20:09:06 +0200269 clk_disable_unprepare(dwsmmio->clk);
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700270
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700271 return 0;
272}
273
Steffen Trumtrar22dae172014-06-13 15:36:18 +0200274static const struct of_device_id dw_spi_mmio_of_match[] = {
Wan Ahmad Zainiec4eadee2020-05-05 21:06:13 +0800275 { .compatible = "snps,dw-apb-ssi", .data = dw_spi_dw_apb_init},
Alexandre Bellonibe17ee02018-08-29 14:45:48 +0200276 { .compatible = "mscc,ocelot-spi", .data = dw_spi_mscc_ocelot_init},
277 { .compatible = "mscc,jaguar2-spi", .data = dw_spi_mscc_jaguar2_init},
Talel Shenharf2d70472018-10-11 14:20:07 +0300278 { .compatible = "amazon,alpine-dw-apb-ssi", .data = dw_spi_alpine_init},
Wan Ahmad Zainiec4eadee2020-05-05 21:06:13 +0800279 { .compatible = "renesas,rzn1-spi", .data = dw_spi_dw_apb_init},
Wan Ahmad Zainiee539f432020-05-05 21:06:14 +0800280 { .compatible = "snps,dwc-ssi-1.01a", .data = dw_spi_dwc_ssi_init},
Wan Ahmad Zainief4237792020-05-05 21:06:16 +0800281 { .compatible = "intel,keembay-ssi", .data = dw_spi_keembay_init},
Steffen Trumtrar22dae172014-06-13 15:36:18 +0200282 { /* end of table */}
283};
284MODULE_DEVICE_TABLE(of, dw_spi_mmio_of_match);
285
Jay Fang32215a62018-12-03 11:15:50 +0800286static const struct acpi_device_id dw_spi_mmio_acpi_match[] = {
Wan Ahmad Zainiec4eadee2020-05-05 21:06:13 +0800287 {"HISI0173", (kernel_ulong_t)dw_spi_dw_apb_init},
Jay Fang32215a62018-12-03 11:15:50 +0800288 {},
289};
290MODULE_DEVICE_TABLE(acpi, dw_spi_mmio_acpi_match);
291
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700292static struct platform_driver dw_spi_mmio_driver = {
Grant Likely940ab882011-10-05 11:29:49 -0600293 .probe = dw_spi_mmio_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +0000294 .remove = dw_spi_mmio_remove,
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700295 .driver = {
296 .name = DRIVER_NAME,
Steffen Trumtrar22dae172014-06-13 15:36:18 +0200297 .of_match_table = dw_spi_mmio_of_match,
Jay Fang32215a62018-12-03 11:15:50 +0800298 .acpi_match_table = ACPI_PTR(dw_spi_mmio_acpi_match),
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700299 },
300};
Grant Likely940ab882011-10-05 11:29:49 -0600301module_platform_driver(dw_spi_mmio_driver);
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700302
303MODULE_AUTHOR("Jean-Hugues Deschenes <jean-hugues.deschenes@octasic.com>");
304MODULE_DESCRIPTION("Memory-mapped I/O interface driver for DW SPI Core");
305MODULE_LICENSE("GPL v2");