blob: 9651679ee7f7330a7f68ca469fb7d43226dfb515 [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
8#include <linux/interrupt.h>
9#include <linux/pci.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
Andy Shevchenkoc95791b2014-08-29 12:41:42 +030018struct spi_pci_desc {
19 int (*setup)(struct dw_spi *);
Andy Shevchenkod58cf5f2015-01-07 17:15:00 +020020 u16 num_cs;
21 u16 bus_num;
Andy Shevchenkoc95791b2014-08-29 12:41:42 +030022};
23
Andy Shevchenkod58cf5f2015-01-07 17:15:00 +020024static struct spi_pci_desc spi_pci_mid_desc_1 = {
Andy Shevchenkoc95791b2014-08-29 12:41:42 +030025 .setup = dw_spi_mid_init,
Andy Shevchenko307ed832015-02-23 17:55:54 +020026 .num_cs = 5,
Andy Shevchenkod58cf5f2015-01-07 17:15:00 +020027 .bus_num = 0,
28};
29
30static struct spi_pci_desc spi_pci_mid_desc_2 = {
31 .setup = dw_spi_mid_init,
Andy Shevchenko307ed832015-02-23 17:55:54 +020032 .num_cs = 2,
Andy Shevchenkod58cf5f2015-01-07 17:15:00 +020033 .bus_num = 1,
Andy Shevchenkoc95791b2014-08-29 12:41:42 +030034};
35
36static int spi_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Feng Tange24c7452009-12-14 14:20:22 -080037{
Feng Tange24c7452009-12-14 14:20:22 -080038 struct dw_spi *dws;
Andy Shevchenkoc95791b2014-08-29 12:41:42 +030039 struct spi_pci_desc *desc = (struct spi_pci_desc *)ent->driver_data;
Feng Tange24c7452009-12-14 14:20:22 -080040 int pci_bar = 0;
41 int ret;
42
Baruch Siach04f421e2013-12-30 20:30:44 +020043 ret = pcim_enable_device(pdev);
Feng Tange24c7452009-12-14 14:20:22 -080044 if (ret)
45 return ret;
46
Andy Shevchenko1c2df962015-10-14 23:12:24 +030047 dws = devm_kzalloc(&pdev->dev, sizeof(*dws), GFP_KERNEL);
48 if (!dws)
Baruch Siach04f421e2013-12-30 20:30:44 +020049 return -ENOMEM;
Feng Tange24c7452009-12-14 14:20:22 -080050
Feng Tange24c7452009-12-14 14:20:22 -080051 /* Get basic io resource and map it */
52 dws->paddr = pci_resource_start(pdev, pci_bar);
Feng Tange24c7452009-12-14 14:20:22 -080053
Andy Shevchenkoceb86de2014-08-29 12:41:40 +030054 ret = pcim_iomap_regions(pdev, 1 << pci_bar, pci_name(pdev));
Feng Tange24c7452009-12-14 14:20:22 -080055 if (ret)
Baruch Siach04f421e2013-12-30 20:30:44 +020056 return ret;
Feng Tange24c7452009-12-14 14:20:22 -080057
Andy Shevchenkoc9d5d6fe2014-08-27 16:21:12 +030058 dws->regs = pcim_iomap_table(pdev)[pci_bar];
Feng Tange24c7452009-12-14 14:20:22 -080059 dws->irq = pdev->irq;
Feng Tang7063c0d2010-12-24 13:59:11 +080060
61 /*
Geert Uytterhoeven3208a1c2016-05-10 20:59:58 +020062 * Specific handling for platforms, like dma setup,
Feng Tang7063c0d2010-12-24 13:59:11 +080063 * clock rate, FIFO depth.
64 */
Andy Shevchenkod58cf5f2015-01-07 17:15:00 +020065 if (desc) {
Andy Shevchenkod9c14742015-01-22 17:59:34 +020066 dws->num_cs = desc->num_cs;
67 dws->bus_num = desc->bus_num;
68
Andy Shevchenkod58cf5f2015-01-07 17:15:00 +020069 if (desc->setup) {
70 ret = desc->setup(dws);
71 if (ret)
72 return ret;
73 }
Andy Shevchenkod58cf5f2015-01-07 17:15:00 +020074 } else {
75 return -ENODEV;
Feng Tang7063c0d2010-12-24 13:59:11 +080076 }
Feng Tange24c7452009-12-14 14:20:22 -080077
Baruch Siach04f421e2013-12-30 20:30:44 +020078 ret = dw_spi_add_host(&pdev->dev, dws);
Feng Tange24c7452009-12-14 14:20:22 -080079 if (ret)
Baruch Siach04f421e2013-12-30 20:30:44 +020080 return ret;
Feng Tange24c7452009-12-14 14:20:22 -080081
82 /* PCI hook and SPI hook use the same drv data */
Andy Shevchenko1c2df962015-10-14 23:12:24 +030083 pci_set_drvdata(pdev, dws);
Feng Tange24c7452009-12-14 14:20:22 -080084
Andy Shevchenkofcf0af42014-08-29 12:41:39 +030085 dev_info(&pdev->dev, "found PCI SPI controller(ID: %04x:%04x)\n",
86 pdev->vendor, pdev->device);
87
Baruch Siach04f421e2013-12-30 20:30:44 +020088 return 0;
Feng Tange24c7452009-12-14 14:20:22 -080089}
90
Grant Likelyfd4a3192012-12-07 16:57:14 +000091static void spi_pci_remove(struct pci_dev *pdev)
Feng Tange24c7452009-12-14 14:20:22 -080092{
Andy Shevchenko1c2df962015-10-14 23:12:24 +030093 struct dw_spi *dws = pci_get_drvdata(pdev);
Feng Tange24c7452009-12-14 14:20:22 -080094
Andy Shevchenko1c2df962015-10-14 23:12:24 +030095 dw_spi_remove_host(dws);
Feng Tange24c7452009-12-14 14:20:22 -080096}
97
Andy Shevchenko35f2d412014-08-29 12:41:41 +030098#ifdef CONFIG_PM_SLEEP
99static int spi_suspend(struct device *dev)
Feng Tange24c7452009-12-14 14:20:22 -0800100{
Andy Shevchenko35f2d412014-08-29 12:41:41 +0300101 struct pci_dev *pdev = to_pci_dev(dev);
Andy Shevchenko1c2df962015-10-14 23:12:24 +0300102 struct dw_spi *dws = pci_get_drvdata(pdev);
Feng Tange24c7452009-12-14 14:20:22 -0800103
Andy Shevchenko1c2df962015-10-14 23:12:24 +0300104 return dw_spi_suspend_host(dws);
Feng Tange24c7452009-12-14 14:20:22 -0800105}
106
Andy Shevchenko35f2d412014-08-29 12:41:41 +0300107static int spi_resume(struct device *dev)
Feng Tange24c7452009-12-14 14:20:22 -0800108{
Andy Shevchenko35f2d412014-08-29 12:41:41 +0300109 struct pci_dev *pdev = to_pci_dev(dev);
Andy Shevchenko1c2df962015-10-14 23:12:24 +0300110 struct dw_spi *dws = pci_get_drvdata(pdev);
Feng Tange24c7452009-12-14 14:20:22 -0800111
Andy Shevchenko1c2df962015-10-14 23:12:24 +0300112 return dw_spi_resume_host(dws);
Feng Tange24c7452009-12-14 14:20:22 -0800113}
Feng Tange24c7452009-12-14 14:20:22 -0800114#endif
115
Andy Shevchenko35f2d412014-08-29 12:41:41 +0300116static SIMPLE_DEV_PM_OPS(dw_spi_pm_ops, spi_suspend, spi_resume);
117
Jingoo Han9a21e472013-12-03 08:25:19 +0900118static const struct pci_device_id pci_ids[] = {
Feng Tang7063c0d2010-12-24 13:59:11 +0800119 /* Intel MID platform SPI controller 0 */
Andy Shevchenkod58cf5f2015-01-07 17:15:00 +0200120 /*
121 * The access to the device 8086:0801 is disabled by HW, since it's
122 * exclusively used by SCU to communicate with MSIC.
123 */
124 /* Intel MID platform SPI controller 1 */
125 { PCI_VDEVICE(INTEL, 0x0800), (kernel_ulong_t)&spi_pci_mid_desc_1},
126 /* Intel MID platform SPI controller 2 */
127 { PCI_VDEVICE(INTEL, 0x0812), (kernel_ulong_t)&spi_pci_mid_desc_2},
Feng Tange24c7452009-12-14 14:20:22 -0800128 {},
129};
130
131static struct pci_driver dw_spi_driver = {
132 .name = DRIVER_NAME,
133 .id_table = pci_ids,
134 .probe = spi_pci_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +0000135 .remove = spi_pci_remove,
Andy Shevchenko35f2d412014-08-29 12:41:41 +0300136 .driver = {
137 .pm = &dw_spi_pm_ops,
138 },
Feng Tange24c7452009-12-14 14:20:22 -0800139};
140
Axel Lin8ebb35f2012-04-04 22:29:32 +0800141module_pci_driver(dw_spi_driver);
Feng Tange24c7452009-12-14 14:20:22 -0800142
143MODULE_AUTHOR("Feng Tang <feng.tang@intel.com>");
144MODULE_DESCRIPTION("PCI interface driver for DW SPI Core");
145MODULE_LICENSE("GPL v2");