Thomas Gleixner | 2025cf9 | 2019-05-29 07:18:02 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 2 | /* |
Grant Likely | ca632f5 | 2011-06-06 01:16:30 -0600 | [diff] [blame] | 3 | * PCI interface driver for DW SPI Core |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 4 | * |
Andy Shevchenko | 5dc23c4 | 2014-08-29 12:41:43 +0300 | [diff] [blame] | 5 | * Copyright (c) 2009, 2014 Intel Corporation. |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 6 | */ |
| 7 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 8 | #include <linux/pci.h> |
Raymond Tan | c816958 | 2019-10-18 16:21:30 +0300 | [diff] [blame] | 9 | #include <linux/pm_runtime.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 10 | #include <linux/slab.h> |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 11 | #include <linux/spi/spi.h> |
Paul Gortmaker | d7614de | 2011-07-03 15:44:29 -0400 | [diff] [blame] | 12 | #include <linux/module.h> |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 13 | |
Grant Likely | ca632f5 | 2011-06-06 01:16:30 -0600 | [diff] [blame] | 14 | #include "spi-dw.h" |
Grant Likely | 568a60e | 2011-02-28 12:47:12 -0700 | [diff] [blame] | 15 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 16 | #define DRIVER_NAME "dw_spi_pci" |
| 17 | |
Serge Semin | 6c710c0 | 2020-05-29 16:11:59 +0300 | [diff] [blame] | 18 | /* 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 Semin | 725b0e3 | 2021-11-15 21:19:13 +0300 | [diff] [blame] | 27 | struct dw_spi_pci_desc { |
Andy Shevchenko | c95791b | 2014-08-29 12:41:42 +0300 | [diff] [blame] | 28 | int (*setup)(struct dw_spi *); |
Andy Shevchenko | d58cf5f | 2015-01-07 17:15:00 +0200 | [diff] [blame] | 29 | u16 num_cs; |
| 30 | u16 bus_num; |
Jarkko Nikula | 5271890 | 2019-08-12 13:13:44 +0300 | [diff] [blame] | 31 | u32 max_freq; |
Andy Shevchenko | c95791b | 2014-08-29 12:41:42 +0300 | [diff] [blame] | 32 | }; |
| 33 | |
Serge Semin | 725b0e3 | 2021-11-15 21:19:13 +0300 | [diff] [blame] | 34 | static int dw_spi_pci_mid_init(struct dw_spi *dws) |
Serge Semin | 6c710c0 | 2020-05-29 16:11:59 +0300 | [diff] [blame] | 35 | { |
| 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 Semin | 5778441 | 2020-05-29 16:12:02 +0300 | [diff] [blame] | 51 | dw_spi_dma_setup_mfld(dws); |
Serge Semin | 6c710c0 | 2020-05-29 16:11:59 +0300 | [diff] [blame] | 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
Serge Semin | 725b0e3 | 2021-11-15 21:19:13 +0300 | [diff] [blame] | 56 | static int dw_spi_pci_generic_init(struct dw_spi *dws) |
Serge Semin | 6c710c0 | 2020-05-29 16:11:59 +0300 | [diff] [blame] | 57 | { |
Serge Semin | 5778441 | 2020-05-29 16:12:02 +0300 | [diff] [blame] | 58 | dw_spi_dma_setup_generic(dws); |
Serge Semin | 6c710c0 | 2020-05-29 16:11:59 +0300 | [diff] [blame] | 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |
Serge Semin | 725b0e3 | 2021-11-15 21:19:13 +0300 | [diff] [blame] | 63 | static struct dw_spi_pci_desc dw_spi_pci_mid_desc_1 = { |
| 64 | .setup = dw_spi_pci_mid_init, |
Andy Shevchenko | 307ed83 | 2015-02-23 17:55:54 +0200 | [diff] [blame] | 65 | .num_cs = 5, |
Andy Shevchenko | d58cf5f | 2015-01-07 17:15:00 +0200 | [diff] [blame] | 66 | .bus_num = 0, |
| 67 | }; |
| 68 | |
Serge Semin | 725b0e3 | 2021-11-15 21:19:13 +0300 | [diff] [blame] | 69 | static struct dw_spi_pci_desc dw_spi_pci_mid_desc_2 = { |
| 70 | .setup = dw_spi_pci_mid_init, |
Andy Shevchenko | 307ed83 | 2015-02-23 17:55:54 +0200 | [diff] [blame] | 71 | .num_cs = 2, |
Andy Shevchenko | d58cf5f | 2015-01-07 17:15:00 +0200 | [diff] [blame] | 72 | .bus_num = 1, |
Andy Shevchenko | c95791b | 2014-08-29 12:41:42 +0300 | [diff] [blame] | 73 | }; |
| 74 | |
Serge Semin | 725b0e3 | 2021-11-15 21:19:13 +0300 | [diff] [blame] | 75 | static struct dw_spi_pci_desc dw_spi_pci_ehl_desc = { |
| 76 | .setup = dw_spi_pci_generic_init, |
Jarkko Nikula | c97905c | 2019-10-18 16:21:31 +0300 | [diff] [blame] | 77 | .num_cs = 2, |
Jarkko Nikula | 5271890 | 2019-08-12 13:13:44 +0300 | [diff] [blame] | 78 | .bus_num = -1, |
| 79 | .max_freq = 100000000, |
| 80 | }; |
| 81 | |
Serge Semin | 725b0e3 | 2021-11-15 21:19:13 +0300 | [diff] [blame] | 82 | static int dw_spi_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 83 | { |
Serge Semin | 725b0e3 | 2021-11-15 21:19:13 +0300 | [diff] [blame] | 84 | struct dw_spi_pci_desc *desc = (struct dw_spi_pci_desc *)ent->driver_data; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 85 | struct dw_spi *dws; |
| 86 | int pci_bar = 0; |
| 87 | int ret; |
| 88 | |
Baruch Siach | 04f421e | 2013-12-30 20:30:44 +0200 | [diff] [blame] | 89 | ret = pcim_enable_device(pdev); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 90 | if (ret) |
| 91 | return ret; |
| 92 | |
Andy Shevchenko | 1c2df96 | 2015-10-14 23:12:24 +0300 | [diff] [blame] | 93 | dws = devm_kzalloc(&pdev->dev, sizeof(*dws), GFP_KERNEL); |
| 94 | if (!dws) |
Baruch Siach | 04f421e | 2013-12-30 20:30:44 +0200 | [diff] [blame] | 95 | return -ENOMEM; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 96 | |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 97 | /* Get basic io resource and map it */ |
| 98 | dws->paddr = pci_resource_start(pdev, pci_bar); |
Felipe Balbi | 8f5c285 | 2019-10-01 11:14:05 +0300 | [diff] [blame] | 99 | pci_set_master(pdev); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 100 | |
Andy Shevchenko | ceb86de | 2014-08-29 12:41:40 +0300 | [diff] [blame] | 101 | ret = pcim_iomap_regions(pdev, 1 << pci_bar, pci_name(pdev)); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 102 | if (ret) |
Baruch Siach | 04f421e | 2013-12-30 20:30:44 +0200 | [diff] [blame] | 103 | return ret; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 104 | |
Felipe Balbi | 8f5c285 | 2019-10-01 11:14:05 +0300 | [diff] [blame] | 105 | ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES); |
| 106 | if (ret < 0) |
| 107 | return ret; |
| 108 | |
Andy Shevchenko | c9d5d6fe | 2014-08-27 16:21:12 +0300 | [diff] [blame] | 109 | dws->regs = pcim_iomap_table(pdev)[pci_bar]; |
Felipe Balbi | 8f5c285 | 2019-10-01 11:14:05 +0300 | [diff] [blame] | 110 | dws->irq = pci_irq_vector(pdev, 0); |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 111 | |
| 112 | /* |
Geert Uytterhoeven | 3208a1c | 2016-05-10 20:59:58 +0200 | [diff] [blame] | 113 | * Specific handling for platforms, like dma setup, |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 114 | * clock rate, FIFO depth. |
| 115 | */ |
Andy Shevchenko | d58cf5f | 2015-01-07 17:15:00 +0200 | [diff] [blame] | 116 | if (desc) { |
Andy Shevchenko | d9c1474 | 2015-01-22 17:59:34 +0200 | [diff] [blame] | 117 | dws->num_cs = desc->num_cs; |
| 118 | dws->bus_num = desc->bus_num; |
Jarkko Nikula | 5271890 | 2019-08-12 13:13:44 +0300 | [diff] [blame] | 119 | dws->max_freq = desc->max_freq; |
Andy Shevchenko | d9c1474 | 2015-01-22 17:59:34 +0200 | [diff] [blame] | 120 | |
Andy Shevchenko | d58cf5f | 2015-01-07 17:15:00 +0200 | [diff] [blame] | 121 | if (desc->setup) { |
| 122 | ret = desc->setup(dws); |
| 123 | if (ret) |
Jay Fang | 9599f34 | 2020-09-15 09:22:49 +0800 | [diff] [blame] | 124 | goto err_free_irq_vectors; |
Andy Shevchenko | d58cf5f | 2015-01-07 17:15:00 +0200 | [diff] [blame] | 125 | } |
Andy Shevchenko | d58cf5f | 2015-01-07 17:15:00 +0200 | [diff] [blame] | 126 | } else { |
Jay Fang | 9599f34 | 2020-09-15 09:22:49 +0800 | [diff] [blame] | 127 | ret = -ENODEV; |
| 128 | goto err_free_irq_vectors; |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 129 | } |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 130 | |
Baruch Siach | 04f421e | 2013-12-30 20:30:44 +0200 | [diff] [blame] | 131 | ret = dw_spi_add_host(&pdev->dev, dws); |
Jay Fang | 9599f34 | 2020-09-15 09:22:49 +0800 | [diff] [blame] | 132 | if (ret) |
| 133 | goto err_free_irq_vectors; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 134 | |
| 135 | /* PCI hook and SPI hook use the same drv data */ |
Andy Shevchenko | 1c2df96 | 2015-10-14 23:12:24 +0300 | [diff] [blame] | 136 | pci_set_drvdata(pdev, dws); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 137 | |
Andy Shevchenko | fcf0af4 | 2014-08-29 12:41:39 +0300 | [diff] [blame] | 138 | dev_info(&pdev->dev, "found PCI SPI controller(ID: %04x:%04x)\n", |
| 139 | pdev->vendor, pdev->device); |
| 140 | |
Raymond Tan | c816958 | 2019-10-18 16:21:30 +0300 | [diff] [blame] | 141 | 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 Siach | 04f421e | 2013-12-30 20:30:44 +0200 | [diff] [blame] | 146 | return 0; |
Jay Fang | 9599f34 | 2020-09-15 09:22:49 +0800 | [diff] [blame] | 147 | |
| 148 | err_free_irq_vectors: |
| 149 | pci_free_irq_vectors(pdev); |
| 150 | return ret; |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Serge Semin | 725b0e3 | 2021-11-15 21:19:13 +0300 | [diff] [blame] | 153 | static void dw_spi_pci_remove(struct pci_dev *pdev) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 154 | { |
Andy Shevchenko | 1c2df96 | 2015-10-14 23:12:24 +0300 | [diff] [blame] | 155 | struct dw_spi *dws = pci_get_drvdata(pdev); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 156 | |
Raymond Tan | c816958 | 2019-10-18 16:21:30 +0300 | [diff] [blame] | 157 | pm_runtime_forbid(&pdev->dev); |
| 158 | pm_runtime_get_noresume(&pdev->dev); |
| 159 | |
Andy Shevchenko | 1c2df96 | 2015-10-14 23:12:24 +0300 | [diff] [blame] | 160 | dw_spi_remove_host(dws); |
Felipe Balbi | 8f5c285 | 2019-10-01 11:14:05 +0300 | [diff] [blame] | 161 | pci_free_irq_vectors(pdev); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Andy Shevchenko | 35f2d41 | 2014-08-29 12:41:41 +0300 | [diff] [blame] | 164 | #ifdef CONFIG_PM_SLEEP |
Serge Semin | 725b0e3 | 2021-11-15 21:19:13 +0300 | [diff] [blame] | 165 | static int dw_spi_pci_suspend(struct device *dev) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 166 | { |
Chuhong Yuan | 2a3b6f7 | 2019-07-24 20:23:31 +0800 | [diff] [blame] | 167 | struct dw_spi *dws = dev_get_drvdata(dev); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 168 | |
Andy Shevchenko | 1c2df96 | 2015-10-14 23:12:24 +0300 | [diff] [blame] | 169 | return dw_spi_suspend_host(dws); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Serge Semin | 725b0e3 | 2021-11-15 21:19:13 +0300 | [diff] [blame] | 172 | static int dw_spi_pci_resume(struct device *dev) |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 173 | { |
Chuhong Yuan | 2a3b6f7 | 2019-07-24 20:23:31 +0800 | [diff] [blame] | 174 | struct dw_spi *dws = dev_get_drvdata(dev); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 175 | |
Andy Shevchenko | 1c2df96 | 2015-10-14 23:12:24 +0300 | [diff] [blame] | 176 | return dw_spi_resume_host(dws); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 177 | } |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 178 | #endif |
| 179 | |
Serge Semin | 725b0e3 | 2021-11-15 21:19:13 +0300 | [diff] [blame] | 180 | static SIMPLE_DEV_PM_OPS(dw_spi_pci_pm_ops, dw_spi_pci_suspend, dw_spi_pci_resume); |
Andy Shevchenko | 35f2d41 | 2014-08-29 12:41:41 +0300 | [diff] [blame] | 181 | |
Serge Semin | 725b0e3 | 2021-11-15 21:19:13 +0300 | [diff] [blame] | 182 | static const struct pci_device_id dw_spi_pci_ids[] = { |
Feng Tang | 7063c0d | 2010-12-24 13:59:11 +0800 | [diff] [blame] | 183 | /* Intel MID platform SPI controller 0 */ |
Andy Shevchenko | d58cf5f | 2015-01-07 17:15:00 +0200 | [diff] [blame] | 184 | /* |
| 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 Semin | 725b0e3 | 2021-11-15 21:19:13 +0300 | [diff] [blame] | 189 | { PCI_VDEVICE(INTEL, 0x0800), (kernel_ulong_t)&dw_spi_pci_mid_desc_1}, |
Andy Shevchenko | d58cf5f | 2015-01-07 17:15:00 +0200 | [diff] [blame] | 190 | /* Intel MID platform SPI controller 2 */ |
Serge Semin | 725b0e3 | 2021-11-15 21:19:13 +0300 | [diff] [blame] | 191 | { PCI_VDEVICE(INTEL, 0x0812), (kernel_ulong_t)&dw_spi_pci_mid_desc_2}, |
Jarkko Nikula | 5271890 | 2019-08-12 13:13:44 +0300 | [diff] [blame] | 192 | /* Intel Elkhart Lake PSE SPI controllers */ |
Serge Semin | 725b0e3 | 2021-11-15 21:19:13 +0300 | [diff] [blame] | 193 | { 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 Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 197 | {}, |
| 198 | }; |
Serge Semin | 725b0e3 | 2021-11-15 21:19:13 +0300 | [diff] [blame] | 199 | MODULE_DEVICE_TABLE(pci, dw_spi_pci_ids); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 200 | |
Serge Semin | 725b0e3 | 2021-11-15 21:19:13 +0300 | [diff] [blame] | 201 | static struct pci_driver dw_spi_pci_driver = { |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 202 | .name = DRIVER_NAME, |
Serge Semin | 725b0e3 | 2021-11-15 21:19:13 +0300 | [diff] [blame] | 203 | .id_table = dw_spi_pci_ids, |
| 204 | .probe = dw_spi_pci_probe, |
| 205 | .remove = dw_spi_pci_remove, |
Andy Shevchenko | 35f2d41 | 2014-08-29 12:41:41 +0300 | [diff] [blame] | 206 | .driver = { |
Serge Semin | 725b0e3 | 2021-11-15 21:19:13 +0300 | [diff] [blame] | 207 | .pm = &dw_spi_pci_pm_ops, |
Andy Shevchenko | 35f2d41 | 2014-08-29 12:41:41 +0300 | [diff] [blame] | 208 | }, |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 209 | }; |
Serge Semin | 725b0e3 | 2021-11-15 21:19:13 +0300 | [diff] [blame] | 210 | module_pci_driver(dw_spi_pci_driver); |
Feng Tang | e24c745 | 2009-12-14 14:20:22 -0800 | [diff] [blame] | 211 | |
| 212 | MODULE_AUTHOR("Feng Tang <feng.tang@intel.com>"); |
| 213 | MODULE_DESCRIPTION("PCI interface driver for DW SPI Core"); |
| 214 | MODULE_LICENSE("GPL v2"); |
Serge Semin | a62bacb | 2021-11-15 21:19:11 +0300 | [diff] [blame] | 215 | MODULE_IMPORT_NS(SPI_DW_CORE); |