blob: d0dd7814e997834793f3e1e49339bd28c73d15ee [file] [log] [blame]
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -07001/*
Grant Likelyca632f52011-06-06 01:16:30 -06002 * Memory-mapped interface driver for DW SPI Core
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -07003 *
4 * Copyright (c) 2010, Octasic semiconductor.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 */
10
11#include <linux/clk.h>
Jamie Iles50c01fc2011-01-11 12:43:52 +000012#include <linux/err.h>
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070013#include <linux/interrupt.h>
14#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070016#include <linux/spi/spi.h>
Grant Likely568a60e2011-02-28 12:47:12 -070017#include <linux/scatterlist.h>
Alexandre Bellonic2c25cc2018-07-27 21:53:56 +020018#include <linux/mfd/syscon.h>
Paul Gortmakerd7614de2011-07-03 15:44:29 -040019#include <linux/module.h>
Steffen Trumtrar22dae172014-06-13 15:36:18 +020020#include <linux/of.h>
Baruch Siachd9c73bb2014-01-31 12:07:47 +020021#include <linux/of_gpio.h>
Steffen Trumtrar22dae172014-06-13 15:36:18 +020022#include <linux/of_platform.h>
Jay Fang32215a62018-12-03 11:15:50 +080023#include <linux/acpi.h>
Andy Shevchenko98999952015-10-14 23:12:25 +030024#include <linux/property.h>
Alexandre Bellonic2c25cc2018-07-27 21:53:56 +020025#include <linux/regmap.h>
Grant Likely568a60e2011-02-28 12:47:12 -070026
Grant Likelyca632f52011-06-06 01:16:30 -060027#include "spi-dw.h"
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070028
29#define DRIVER_NAME "dw_spi_mmio"
30
31struct dw_spi_mmio {
Jean-Hugues Deschenes0a4c1d72010-01-21 09:55:42 -070032 struct dw_spi dws;
33 struct clk *clk;
Alexandre Bellonic2c25cc2018-07-27 21:53:56 +020034 void *priv;
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070035};
36
Alexandre Bellonic2c25cc2018-07-27 21:53:56 +020037#define MSCC_CPU_SYSTEM_CTRL_GENERAL_CTRL 0x24
Alexandre Bellonic2c25cc2018-07-27 21:53:56 +020038#define OCELOT_IF_SI_OWNER_OFFSET 4
Alexandre Bellonibe17ee02018-08-29 14:45:48 +020039#define JAGUAR2_IF_SI_OWNER_OFFSET 6
Alexandre Bellonic1d8b082018-08-31 13:40:46 +020040#define MSCC_IF_SI_OWNER_MASK GENMASK(1, 0)
Alexandre Bellonic2c25cc2018-07-27 21:53:56 +020041#define MSCC_IF_SI_OWNER_SISL 0
42#define MSCC_IF_SI_OWNER_SIBM 1
43#define MSCC_IF_SI_OWNER_SIMC 2
44
45#define MSCC_SPI_MST_SW_MODE 0x14
46#define MSCC_SPI_MST_SW_MODE_SW_PIN_CTRL_MODE BIT(13)
47#define MSCC_SPI_MST_SW_MODE_SW_SPI_CS(x) (x << 5)
48
49struct dw_spi_mscc {
50 struct regmap *syscon;
51 void __iomem *spi_mst;
52};
53
54/*
55 * The Designware SPI controller (referred to as master in the documentation)
56 * automatically deasserts chip select when the tx fifo is empty. The chip
57 * selects then needs to be either driven as GPIOs or, for the first 4 using the
58 * the SPI boot controller registers. the final chip select is an OR gate
59 * between the Designware SPI controller and the SPI boot controller.
60 */
61static void dw_spi_mscc_set_cs(struct spi_device *spi, bool enable)
62{
63 struct dw_spi *dws = spi_master_get_devdata(spi->master);
64 struct dw_spi_mmio *dwsmmio = container_of(dws, struct dw_spi_mmio, dws);
65 struct dw_spi_mscc *dwsmscc = dwsmmio->priv;
66 u32 cs = spi->chip_select;
67
68 if (cs < 4) {
69 u32 sw_mode = MSCC_SPI_MST_SW_MODE_SW_PIN_CTRL_MODE;
70
71 if (!enable)
72 sw_mode |= MSCC_SPI_MST_SW_MODE_SW_SPI_CS(BIT(cs));
73
74 writel(sw_mode, dwsmscc->spi_mst + MSCC_SPI_MST_SW_MODE);
75 }
76
77 dw_spi_set_cs(spi, enable);
78}
79
80static int dw_spi_mscc_init(struct platform_device *pdev,
Alexandre Bellonibe17ee02018-08-29 14:45:48 +020081 struct dw_spi_mmio *dwsmmio,
82 const char *cpu_syscon, u32 if_si_owner_offset)
Alexandre Bellonic2c25cc2018-07-27 21:53:56 +020083{
84 struct dw_spi_mscc *dwsmscc;
85 struct resource *res;
86
87 dwsmscc = devm_kzalloc(&pdev->dev, sizeof(*dwsmscc), GFP_KERNEL);
88 if (!dwsmscc)
89 return -ENOMEM;
90
91 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
92 dwsmscc->spi_mst = devm_ioremap_resource(&pdev->dev, res);
93 if (IS_ERR(dwsmscc->spi_mst)) {
94 dev_err(&pdev->dev, "SPI_MST region map failed\n");
95 return PTR_ERR(dwsmscc->spi_mst);
96 }
97
Alexandre Bellonibe17ee02018-08-29 14:45:48 +020098 dwsmscc->syscon = syscon_regmap_lookup_by_compatible(cpu_syscon);
Alexandre Bellonic2c25cc2018-07-27 21:53:56 +020099 if (IS_ERR(dwsmscc->syscon))
100 return PTR_ERR(dwsmscc->syscon);
101
102 /* Deassert all CS */
103 writel(0, dwsmscc->spi_mst + MSCC_SPI_MST_SW_MODE);
104
105 /* Select the owner of the SI interface */
106 regmap_update_bits(dwsmscc->syscon, MSCC_CPU_SYSTEM_CTRL_GENERAL_CTRL,
Alexandre Bellonic1d8b082018-08-31 13:40:46 +0200107 MSCC_IF_SI_OWNER_MASK << if_si_owner_offset,
Alexandre Bellonibe17ee02018-08-29 14:45:48 +0200108 MSCC_IF_SI_OWNER_SIMC << if_si_owner_offset);
Alexandre Bellonic2c25cc2018-07-27 21:53:56 +0200109
110 dwsmmio->dws.set_cs = dw_spi_mscc_set_cs;
111 dwsmmio->priv = dwsmscc;
112
113 return 0;
114}
115
Alexandre Bellonibe17ee02018-08-29 14:45:48 +0200116static int dw_spi_mscc_ocelot_init(struct platform_device *pdev,
117 struct dw_spi_mmio *dwsmmio)
118{
119 return dw_spi_mscc_init(pdev, dwsmmio, "mscc,ocelot-cpu-syscon",
120 OCELOT_IF_SI_OWNER_OFFSET);
121}
122
123static int dw_spi_mscc_jaguar2_init(struct platform_device *pdev,
124 struct dw_spi_mmio *dwsmmio)
125{
126 return dw_spi_mscc_init(pdev, dwsmmio, "mscc,jaguar2-cpu-syscon",
127 JAGUAR2_IF_SI_OWNER_OFFSET);
128}
129
Talel Shenharf2d70472018-10-11 14:20:07 +0300130static int dw_spi_alpine_init(struct platform_device *pdev,
131 struct dw_spi_mmio *dwsmmio)
132{
133 dwsmmio->dws.cs_override = 1;
134
135 return 0;
136}
137
Grant Likelyfd4a3192012-12-07 16:57:14 +0000138static int dw_spi_mmio_probe(struct platform_device *pdev)
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700139{
Alexandre Bellonic2c25cc2018-07-27 21:53:56 +0200140 int (*init_func)(struct platform_device *pdev,
141 struct dw_spi_mmio *dwsmmio);
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700142 struct dw_spi_mmio *dwsmmio;
143 struct dw_spi *dws;
Baruch Siach04f421e2013-12-30 20:30:44 +0200144 struct resource *mem;
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700145 int ret;
Steffen Trumtrar22dae172014-06-13 15:36:18 +0200146 int num_cs;
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700147
Baruch Siach04f421e2013-12-30 20:30:44 +0200148 dwsmmio = devm_kzalloc(&pdev->dev, sizeof(struct dw_spi_mmio),
149 GFP_KERNEL);
150 if (!dwsmmio)
151 return -ENOMEM;
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700152
153 dws = &dwsmmio->dws;
154
155 /* Get basic io resource and map it */
156 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Baruch Siach04f421e2013-12-30 20:30:44 +0200157 dws->regs = devm_ioremap_resource(&pdev->dev, mem);
158 if (IS_ERR(dws->regs)) {
159 dev_err(&pdev->dev, "SPI region map failed\n");
160 return PTR_ERR(dws->regs);
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700161 }
162
163 dws->irq = platform_get_irq(pdev, 0);
164 if (dws->irq < 0) {
165 dev_err(&pdev->dev, "no irq resource?\n");
Baruch Siach04f421e2013-12-30 20:30:44 +0200166 return dws->irq; /* -ENXIO */
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700167 }
168
Baruch Siach04f421e2013-12-30 20:30:44 +0200169 dwsmmio->clk = devm_clk_get(&pdev->dev, NULL);
170 if (IS_ERR(dwsmmio->clk))
171 return PTR_ERR(dwsmmio->clk);
Baruch Siach020fe3f2013-12-30 20:30:45 +0200172 ret = clk_prepare_enable(dwsmmio->clk);
Baruch Siach04f421e2013-12-30 20:30:44 +0200173 if (ret)
174 return ret;
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700175
Baruch Siach2418991e2014-01-26 10:14:32 +0200176 dws->bus_num = pdev->id;
Steffen Trumtrar22dae172014-06-13 15:36:18 +0200177
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700178 dws->max_freq = clk_get_rate(dwsmmio->clk);
179
Andy Shevchenko98999952015-10-14 23:12:25 +0300180 device_property_read_u32(&pdev->dev, "reg-io-width", &dws->reg_io_width);
Michael van der Westhuizenc4fe57f2015-08-18 22:21:53 +0200181
Steffen Trumtrar22dae172014-06-13 15:36:18 +0200182 num_cs = 4;
183
Andy Shevchenko98999952015-10-14 23:12:25 +0300184 device_property_read_u32(&pdev->dev, "num-cs", &num_cs);
Steffen Trumtrar22dae172014-06-13 15:36:18 +0200185
186 dws->num_cs = num_cs;
187
Baruch Siachd9c73bb2014-01-31 12:07:47 +0200188 if (pdev->dev.of_node) {
189 int i;
190
191 for (i = 0; i < dws->num_cs; i++) {
192 int cs_gpio = of_get_named_gpio(pdev->dev.of_node,
193 "cs-gpios", i);
194
195 if (cs_gpio == -EPROBE_DEFER) {
196 ret = cs_gpio;
197 goto out;
198 }
199
200 if (gpio_is_valid(cs_gpio)) {
201 ret = devm_gpio_request(&pdev->dev, cs_gpio,
202 dev_name(&pdev->dev));
203 if (ret)
204 goto out;
205 }
206 }
207 }
208
Alexandre Bellonic2c25cc2018-07-27 21:53:56 +0200209 init_func = device_get_match_data(&pdev->dev);
210 if (init_func) {
211 ret = init_func(pdev, dwsmmio);
212 if (ret)
213 goto out;
214 }
215
Baruch Siach04f421e2013-12-30 20:30:44 +0200216 ret = dw_spi_add_host(&pdev->dev, dws);
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700217 if (ret)
Baruch Siach04f421e2013-12-30 20:30:44 +0200218 goto out;
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700219
220 platform_set_drvdata(pdev, dwsmmio);
221 return 0;
222
Baruch Siach04f421e2013-12-30 20:30:44 +0200223out:
Baruch Siach020fe3f2013-12-30 20:30:45 +0200224 clk_disable_unprepare(dwsmmio->clk);
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700225 return ret;
226}
227
Grant Likelyfd4a3192012-12-07 16:57:14 +0000228static int dw_spi_mmio_remove(struct platform_device *pdev)
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700229{
230 struct dw_spi_mmio *dwsmmio = platform_get_drvdata(pdev);
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700231
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700232 dw_spi_remove_host(&dwsmmio->dws);
Marek Vasut400c18e2017-04-18 20:09:06 +0200233 clk_disable_unprepare(dwsmmio->clk);
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700234
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700235 return 0;
236}
237
Steffen Trumtrar22dae172014-06-13 15:36:18 +0200238static const struct of_device_id dw_spi_mmio_of_match[] = {
239 { .compatible = "snps,dw-apb-ssi", },
Alexandre Bellonibe17ee02018-08-29 14:45:48 +0200240 { .compatible = "mscc,ocelot-spi", .data = dw_spi_mscc_ocelot_init},
241 { .compatible = "mscc,jaguar2-spi", .data = dw_spi_mscc_jaguar2_init},
Talel Shenharf2d70472018-10-11 14:20:07 +0300242 { .compatible = "amazon,alpine-dw-apb-ssi", .data = dw_spi_alpine_init},
Steffen Trumtrar22dae172014-06-13 15:36:18 +0200243 { /* end of table */}
244};
245MODULE_DEVICE_TABLE(of, dw_spi_mmio_of_match);
246
Jay Fang32215a62018-12-03 11:15:50 +0800247static const struct acpi_device_id dw_spi_mmio_acpi_match[] = {
248 {"HISI0173", 0},
249 {},
250};
251MODULE_DEVICE_TABLE(acpi, dw_spi_mmio_acpi_match);
252
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700253static struct platform_driver dw_spi_mmio_driver = {
Grant Likely940ab882011-10-05 11:29:49 -0600254 .probe = dw_spi_mmio_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +0000255 .remove = dw_spi_mmio_remove,
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700256 .driver = {
257 .name = DRIVER_NAME,
Steffen Trumtrar22dae172014-06-13 15:36:18 +0200258 .of_match_table = dw_spi_mmio_of_match,
Jay Fang32215a62018-12-03 11:15:50 +0800259 .acpi_match_table = ACPI_PTR(dw_spi_mmio_acpi_match),
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700260 },
261};
Grant Likely940ab882011-10-05 11:29:49 -0600262module_platform_driver(dw_spi_mmio_driver);
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700263
264MODULE_AUTHOR("Jean-Hugues Deschenes <jean-hugues.deschenes@octasic.com>");
265MODULE_DESCRIPTION("Memory-mapped I/O interface driver for DW SPI Core");
266MODULE_LICENSE("GPL v2");