blob: 7c8279d13f31941b238d87454450037e38bde4ee [file] [log] [blame]
Thomas Gleixner2025cf92019-05-29 07:18:02 -07001// SPDX-License-Identifier: GPL-2.0-only
Feng Tange24c7452009-12-14 14:20:22 -08002/*
Grant Likelyca632f52011-06-06 01:16:30 -06003 * PCI interface driver for DW SPI Core
Feng Tange24c7452009-12-14 14:20:22 -08004 *
Andy Shevchenko5dc23c42014-08-29 12:41:43 +03005 * Copyright (c) 2009, 2014 Intel Corporation.
Feng Tange24c7452009-12-14 14:20:22 -08006 */
7
Feng Tange24c7452009-12-14 14:20:22 -08008#include <linux/pci.h>
Raymond Tanc8169582019-10-18 16:21:30 +03009#include <linux/pm_runtime.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Feng Tange24c7452009-12-14 14:20:22 -080011#include <linux/spi/spi.h>
Paul Gortmakerd7614de2011-07-03 15:44:29 -040012#include <linux/module.h>
Feng Tange24c7452009-12-14 14:20:22 -080013
Grant Likelyca632f52011-06-06 01:16:30 -060014#include "spi-dw.h"
Grant Likely568a60e2011-02-28 12:47:12 -070015
Feng Tange24c7452009-12-14 14:20:22 -080016#define DRIVER_NAME "dw_spi_pci"
17
Serge Semin6c710c02020-05-29 16:11:59 +030018/* HW info for MRST Clk Control Unit, 32b reg per controller */
19#define MRST_SPI_CLK_BASE 100000000 /* 100m */
20#define MRST_CLK_SPI_REG 0xff11d86c
21#define CLK_SPI_BDIV_OFFSET 0
22#define CLK_SPI_BDIV_MASK 0x00000007
23#define CLK_SPI_CDIV_OFFSET 9
24#define CLK_SPI_CDIV_MASK 0x00000e00
25#define CLK_SPI_DISABLE_OFFSET 8
26
Serge Semin725b0e32021-11-15 21:19:13 +030027struct dw_spi_pci_desc {
Andy Shevchenkoc95791b2014-08-29 12:41:42 +030028 int (*setup)(struct dw_spi *);
Andy Shevchenkod58cf5f2015-01-07 17:15:00 +020029 u16 num_cs;
30 u16 bus_num;
Jarkko Nikula52718902019-08-12 13:13:44 +030031 u32 max_freq;
Andy Shevchenkoc95791b2014-08-29 12:41:42 +030032};
33
Serge Semin725b0e32021-11-15 21:19:13 +030034static int dw_spi_pci_mid_init(struct dw_spi *dws)
Serge Semin6c710c02020-05-29 16:11:59 +030035{
36 void __iomem *clk_reg;
37 u32 clk_cdiv;
38
39 clk_reg = ioremap(MRST_CLK_SPI_REG, 16);
40 if (!clk_reg)
41 return -ENOMEM;
42
43 /* Get SPI controller operating freq info */
44 clk_cdiv = readl(clk_reg + dws->bus_num * sizeof(u32));
45 clk_cdiv &= CLK_SPI_CDIV_MASK;
46 clk_cdiv >>= CLK_SPI_CDIV_OFFSET;
47 dws->max_freq = MRST_SPI_CLK_BASE / (clk_cdiv + 1);
48
49 iounmap(clk_reg);
50
Serge Semin57784412020-05-29 16:12:02 +030051 dw_spi_dma_setup_mfld(dws);
Serge Semin6c710c02020-05-29 16:11:59 +030052
53 return 0;
54}
55
Serge Semin725b0e32021-11-15 21:19:13 +030056static int dw_spi_pci_generic_init(struct dw_spi *dws)
Serge Semin6c710c02020-05-29 16:11:59 +030057{
Serge Semin57784412020-05-29 16:12:02 +030058 dw_spi_dma_setup_generic(dws);
Serge Semin6c710c02020-05-29 16:11:59 +030059
60 return 0;
61}
62
Serge Semin725b0e32021-11-15 21:19:13 +030063static struct dw_spi_pci_desc dw_spi_pci_mid_desc_1 = {
64 .setup = dw_spi_pci_mid_init,
Andy Shevchenko307ed832015-02-23 17:55:54 +020065 .num_cs = 5,
Andy Shevchenkod58cf5f2015-01-07 17:15:00 +020066 .bus_num = 0,
67};
68
Serge Semin725b0e32021-11-15 21:19:13 +030069static struct dw_spi_pci_desc dw_spi_pci_mid_desc_2 = {
70 .setup = dw_spi_pci_mid_init,
Andy Shevchenko307ed832015-02-23 17:55:54 +020071 .num_cs = 2,
Andy Shevchenkod58cf5f2015-01-07 17:15:00 +020072 .bus_num = 1,
Andy Shevchenkoc95791b2014-08-29 12:41:42 +030073};
74
Serge Semin725b0e32021-11-15 21:19:13 +030075static struct dw_spi_pci_desc dw_spi_pci_ehl_desc = {
76 .setup = dw_spi_pci_generic_init,
Jarkko Nikulac97905c2019-10-18 16:21:31 +030077 .num_cs = 2,
Jarkko Nikula52718902019-08-12 13:13:44 +030078 .bus_num = -1,
79 .max_freq = 100000000,
80};
81
Serge Semin725b0e32021-11-15 21:19:13 +030082static int dw_spi_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Feng Tange24c7452009-12-14 14:20:22 -080083{
Serge Semin725b0e32021-11-15 21:19:13 +030084 struct dw_spi_pci_desc *desc = (struct dw_spi_pci_desc *)ent->driver_data;
Feng Tange24c7452009-12-14 14:20:22 -080085 struct dw_spi *dws;
86 int pci_bar = 0;
87 int ret;
88
Baruch Siach04f421e2013-12-30 20:30:44 +020089 ret = pcim_enable_device(pdev);
Feng Tange24c7452009-12-14 14:20:22 -080090 if (ret)
91 return ret;
92
Andy Shevchenko1c2df962015-10-14 23:12:24 +030093 dws = devm_kzalloc(&pdev->dev, sizeof(*dws), GFP_KERNEL);
94 if (!dws)
Baruch Siach04f421e2013-12-30 20:30:44 +020095 return -ENOMEM;
Feng Tange24c7452009-12-14 14:20:22 -080096
Feng Tange24c7452009-12-14 14:20:22 -080097 /* Get basic io resource and map it */
98 dws->paddr = pci_resource_start(pdev, pci_bar);
Felipe Balbi8f5c2852019-10-01 11:14:05 +030099 pci_set_master(pdev);
Feng Tange24c7452009-12-14 14:20:22 -0800100
Andy Shevchenkoceb86de2014-08-29 12:41:40 +0300101 ret = pcim_iomap_regions(pdev, 1 << pci_bar, pci_name(pdev));
Feng Tange24c7452009-12-14 14:20:22 -0800102 if (ret)
Baruch Siach04f421e2013-12-30 20:30:44 +0200103 return ret;
Feng Tange24c7452009-12-14 14:20:22 -0800104
Felipe Balbi8f5c2852019-10-01 11:14:05 +0300105 ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES);
106 if (ret < 0)
107 return ret;
108
Andy Shevchenkoc9d5d6fe2014-08-27 16:21:12 +0300109 dws->regs = pcim_iomap_table(pdev)[pci_bar];
Felipe Balbi8f5c2852019-10-01 11:14:05 +0300110 dws->irq = pci_irq_vector(pdev, 0);
Feng Tang7063c0d2010-12-24 13:59:11 +0800111
112 /*
Geert Uytterhoeven3208a1c2016-05-10 20:59:58 +0200113 * Specific handling for platforms, like dma setup,
Feng Tang7063c0d2010-12-24 13:59:11 +0800114 * clock rate, FIFO depth.
115 */
Andy Shevchenkod58cf5f2015-01-07 17:15:00 +0200116 if (desc) {
Andy Shevchenkod9c14742015-01-22 17:59:34 +0200117 dws->num_cs = desc->num_cs;
118 dws->bus_num = desc->bus_num;
Jarkko Nikula52718902019-08-12 13:13:44 +0300119 dws->max_freq = desc->max_freq;
Andy Shevchenkod9c14742015-01-22 17:59:34 +0200120
Andy Shevchenkod58cf5f2015-01-07 17:15:00 +0200121 if (desc->setup) {
122 ret = desc->setup(dws);
123 if (ret)
Jay Fang9599f342020-09-15 09:22:49 +0800124 goto err_free_irq_vectors;
Andy Shevchenkod58cf5f2015-01-07 17:15:00 +0200125 }
Andy Shevchenkod58cf5f2015-01-07 17:15:00 +0200126 } else {
Jay Fang9599f342020-09-15 09:22:49 +0800127 ret = -ENODEV;
128 goto err_free_irq_vectors;
Feng Tang7063c0d2010-12-24 13:59:11 +0800129 }
Feng Tange24c7452009-12-14 14:20:22 -0800130
Baruch Siach04f421e2013-12-30 20:30:44 +0200131 ret = dw_spi_add_host(&pdev->dev, dws);
Jay Fang9599f342020-09-15 09:22:49 +0800132 if (ret)
133 goto err_free_irq_vectors;
Feng Tange24c7452009-12-14 14:20:22 -0800134
135 /* PCI hook and SPI hook use the same drv data */
Andy Shevchenko1c2df962015-10-14 23:12:24 +0300136 pci_set_drvdata(pdev, dws);
Feng Tange24c7452009-12-14 14:20:22 -0800137
Andy Shevchenkofcf0af42014-08-29 12:41:39 +0300138 dev_info(&pdev->dev, "found PCI SPI controller(ID: %04x:%04x)\n",
139 pdev->vendor, pdev->device);
140
Raymond Tanc8169582019-10-18 16:21:30 +0300141 pm_runtime_set_autosuspend_delay(&pdev->dev, 1000);
142 pm_runtime_use_autosuspend(&pdev->dev);
143 pm_runtime_put_autosuspend(&pdev->dev);
144 pm_runtime_allow(&pdev->dev);
145
Baruch Siach04f421e2013-12-30 20:30:44 +0200146 return 0;
Jay Fang9599f342020-09-15 09:22:49 +0800147
148err_free_irq_vectors:
149 pci_free_irq_vectors(pdev);
150 return ret;
Feng Tange24c7452009-12-14 14:20:22 -0800151}
152
Serge Semin725b0e32021-11-15 21:19:13 +0300153static void dw_spi_pci_remove(struct pci_dev *pdev)
Feng Tange24c7452009-12-14 14:20:22 -0800154{
Andy Shevchenko1c2df962015-10-14 23:12:24 +0300155 struct dw_spi *dws = pci_get_drvdata(pdev);
Feng Tange24c7452009-12-14 14:20:22 -0800156
Raymond Tanc8169582019-10-18 16:21:30 +0300157 pm_runtime_forbid(&pdev->dev);
158 pm_runtime_get_noresume(&pdev->dev);
159
Andy Shevchenko1c2df962015-10-14 23:12:24 +0300160 dw_spi_remove_host(dws);
Felipe Balbi8f5c2852019-10-01 11:14:05 +0300161 pci_free_irq_vectors(pdev);
Feng Tange24c7452009-12-14 14:20:22 -0800162}
163
Andy Shevchenko35f2d412014-08-29 12:41:41 +0300164#ifdef CONFIG_PM_SLEEP
Serge Semin725b0e32021-11-15 21:19:13 +0300165static int dw_spi_pci_suspend(struct device *dev)
Feng Tange24c7452009-12-14 14:20:22 -0800166{
Chuhong Yuan2a3b6f72019-07-24 20:23:31 +0800167 struct dw_spi *dws = dev_get_drvdata(dev);
Feng Tange24c7452009-12-14 14:20:22 -0800168
Andy Shevchenko1c2df962015-10-14 23:12:24 +0300169 return dw_spi_suspend_host(dws);
Feng Tange24c7452009-12-14 14:20:22 -0800170}
171
Serge Semin725b0e32021-11-15 21:19:13 +0300172static int dw_spi_pci_resume(struct device *dev)
Feng Tange24c7452009-12-14 14:20:22 -0800173{
Chuhong Yuan2a3b6f72019-07-24 20:23:31 +0800174 struct dw_spi *dws = dev_get_drvdata(dev);
Feng Tange24c7452009-12-14 14:20:22 -0800175
Andy Shevchenko1c2df962015-10-14 23:12:24 +0300176 return dw_spi_resume_host(dws);
Feng Tange24c7452009-12-14 14:20:22 -0800177}
Feng Tange24c7452009-12-14 14:20:22 -0800178#endif
179
Serge Semin725b0e32021-11-15 21:19:13 +0300180static SIMPLE_DEV_PM_OPS(dw_spi_pci_pm_ops, dw_spi_pci_suspend, dw_spi_pci_resume);
Andy Shevchenko35f2d412014-08-29 12:41:41 +0300181
Serge Semin725b0e32021-11-15 21:19:13 +0300182static const struct pci_device_id dw_spi_pci_ids[] = {
Feng Tang7063c0d2010-12-24 13:59:11 +0800183 /* Intel MID platform SPI controller 0 */
Andy Shevchenkod58cf5f2015-01-07 17:15:00 +0200184 /*
185 * The access to the device 8086:0801 is disabled by HW, since it's
186 * exclusively used by SCU to communicate with MSIC.
187 */
188 /* Intel MID platform SPI controller 1 */
Serge Semin725b0e32021-11-15 21:19:13 +0300189 { PCI_VDEVICE(INTEL, 0x0800), (kernel_ulong_t)&dw_spi_pci_mid_desc_1},
Andy Shevchenkod58cf5f2015-01-07 17:15:00 +0200190 /* Intel MID platform SPI controller 2 */
Serge Semin725b0e32021-11-15 21:19:13 +0300191 { PCI_VDEVICE(INTEL, 0x0812), (kernel_ulong_t)&dw_spi_pci_mid_desc_2},
Jarkko Nikula52718902019-08-12 13:13:44 +0300192 /* Intel Elkhart Lake PSE SPI controllers */
Serge Semin725b0e32021-11-15 21:19:13 +0300193 { PCI_VDEVICE(INTEL, 0x4b84), (kernel_ulong_t)&dw_spi_pci_ehl_desc},
194 { PCI_VDEVICE(INTEL, 0x4b85), (kernel_ulong_t)&dw_spi_pci_ehl_desc},
195 { PCI_VDEVICE(INTEL, 0x4b86), (kernel_ulong_t)&dw_spi_pci_ehl_desc},
196 { PCI_VDEVICE(INTEL, 0x4b87), (kernel_ulong_t)&dw_spi_pci_ehl_desc},
Feng Tange24c7452009-12-14 14:20:22 -0800197 {},
198};
Serge Semin725b0e32021-11-15 21:19:13 +0300199MODULE_DEVICE_TABLE(pci, dw_spi_pci_ids);
Feng Tange24c7452009-12-14 14:20:22 -0800200
Serge Semin725b0e32021-11-15 21:19:13 +0300201static struct pci_driver dw_spi_pci_driver = {
Feng Tange24c7452009-12-14 14:20:22 -0800202 .name = DRIVER_NAME,
Serge Semin725b0e32021-11-15 21:19:13 +0300203 .id_table = dw_spi_pci_ids,
204 .probe = dw_spi_pci_probe,
205 .remove = dw_spi_pci_remove,
Andy Shevchenko35f2d412014-08-29 12:41:41 +0300206 .driver = {
Serge Semin725b0e32021-11-15 21:19:13 +0300207 .pm = &dw_spi_pci_pm_ops,
Andy Shevchenko35f2d412014-08-29 12:41:41 +0300208 },
Feng Tange24c7452009-12-14 14:20:22 -0800209};
Serge Semin725b0e32021-11-15 21:19:13 +0300210module_pci_driver(dw_spi_pci_driver);
Feng Tange24c7452009-12-14 14:20:22 -0800211
212MODULE_AUTHOR("Feng Tang <feng.tang@intel.com>");
213MODULE_DESCRIPTION("PCI interface driver for DW SPI Core");
214MODULE_LICENSE("GPL v2");
Serge Semina62bacb2021-11-15 21:19:11 +0300215MODULE_IMPORT_NS(SPI_DW_CORE);