blob: 6d331e0db33122b23cfa17f6602618a510efcde1 [file] [log] [blame]
Feng Tange24c7452009-12-14 14:20:22 -08001/*
Grant Likelyca632f52011-06-06 01:16:30 -06002 * PCI interface driver for DW SPI Core
Feng Tange24c7452009-12-14 14:20:22 -08003 *
Andy Shevchenko5dc23c42014-08-29 12:41:43 +03004 * Copyright (c) 2009, 2014 Intel Corporation.
Feng Tange24c7452009-12-14 14:20:22 -08005 *
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 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
Feng Tange24c7452009-12-14 14:20:22 -080014 */
15
16#include <linux/interrupt.h>
17#include <linux/pci.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Feng Tange24c7452009-12-14 14:20:22 -080019#include <linux/spi/spi.h>
Paul Gortmakerd7614de2011-07-03 15:44:29 -040020#include <linux/module.h>
Feng Tange24c7452009-12-14 14:20:22 -080021
Grant Likelyca632f52011-06-06 01:16:30 -060022#include "spi-dw.h"
Grant Likely568a60e2011-02-28 12:47:12 -070023
Feng Tange24c7452009-12-14 14:20:22 -080024#define DRIVER_NAME "dw_spi_pci"
25
26struct dw_spi_pci {
Feng Tang7063c0d2010-12-24 13:59:11 +080027 struct pci_dev *pdev;
28 struct dw_spi dws;
Feng Tange24c7452009-12-14 14:20:22 -080029};
30
Andy Shevchenkoc95791b2014-08-29 12:41:42 +030031struct spi_pci_desc {
32 int (*setup)(struct dw_spi *);
Andy Shevchenkod58cf5f2015-01-07 17:15:00 +020033 u16 num_cs;
34 u16 bus_num;
Andy Shevchenkoc95791b2014-08-29 12:41:42 +030035};
36
Andy Shevchenkod58cf5f2015-01-07 17:15:00 +020037static struct spi_pci_desc spi_pci_mid_desc_1 = {
Andy Shevchenkoc95791b2014-08-29 12:41:42 +030038 .setup = dw_spi_mid_init,
Andy Shevchenko307ed832015-02-23 17:55:54 +020039 .num_cs = 5,
Andy Shevchenkod58cf5f2015-01-07 17:15:00 +020040 .bus_num = 0,
41};
42
43static struct spi_pci_desc spi_pci_mid_desc_2 = {
44 .setup = dw_spi_mid_init,
Andy Shevchenko307ed832015-02-23 17:55:54 +020045 .num_cs = 2,
Andy Shevchenkod58cf5f2015-01-07 17:15:00 +020046 .bus_num = 1,
Andy Shevchenkoc95791b2014-08-29 12:41:42 +030047};
48
49static int spi_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Feng Tange24c7452009-12-14 14:20:22 -080050{
51 struct dw_spi_pci *dwpci;
52 struct dw_spi *dws;
Andy Shevchenkoc95791b2014-08-29 12:41:42 +030053 struct spi_pci_desc *desc = (struct spi_pci_desc *)ent->driver_data;
Feng Tange24c7452009-12-14 14:20:22 -080054 int pci_bar = 0;
55 int ret;
56
Baruch Siach04f421e2013-12-30 20:30:44 +020057 ret = pcim_enable_device(pdev);
Feng Tange24c7452009-12-14 14:20:22 -080058 if (ret)
59 return ret;
60
Baruch Siachfa4934a2014-01-06 09:21:33 +020061 dwpci = devm_kzalloc(&pdev->dev, sizeof(struct dw_spi_pci),
62 GFP_KERNEL);
Baruch Siach04f421e2013-12-30 20:30:44 +020063 if (!dwpci)
64 return -ENOMEM;
Feng Tange24c7452009-12-14 14:20:22 -080065
66 dwpci->pdev = pdev;
67 dws = &dwpci->dws;
68
69 /* Get basic io resource and map it */
70 dws->paddr = pci_resource_start(pdev, pci_bar);
Feng Tange24c7452009-12-14 14:20:22 -080071
Andy Shevchenkoceb86de2014-08-29 12:41:40 +030072 ret = pcim_iomap_regions(pdev, 1 << pci_bar, pci_name(pdev));
Feng Tange24c7452009-12-14 14:20:22 -080073 if (ret)
Baruch Siach04f421e2013-12-30 20:30:44 +020074 return ret;
Feng Tange24c7452009-12-14 14:20:22 -080075
Andy Shevchenkoc9d5d6fe2014-08-27 16:21:12 +030076 dws->regs = pcim_iomap_table(pdev)[pci_bar];
77
Feng Tange24c7452009-12-14 14:20:22 -080078 dws->irq = pdev->irq;
Feng Tang7063c0d2010-12-24 13:59:11 +080079
80 /*
Andy Shevchenkoc95791b2014-08-29 12:41:42 +030081 * Specific handling for paltforms, like dma setup,
Feng Tang7063c0d2010-12-24 13:59:11 +080082 * clock rate, FIFO depth.
83 */
Andy Shevchenkod58cf5f2015-01-07 17:15:00 +020084 if (desc) {
Andy Shevchenkod9c14742015-01-22 17:59:34 +020085 dws->num_cs = desc->num_cs;
86 dws->bus_num = desc->bus_num;
87
Andy Shevchenkod58cf5f2015-01-07 17:15:00 +020088 if (desc->setup) {
89 ret = desc->setup(dws);
90 if (ret)
91 return ret;
92 }
Andy Shevchenkod58cf5f2015-01-07 17:15:00 +020093 } else {
94 return -ENODEV;
Feng Tang7063c0d2010-12-24 13:59:11 +080095 }
Feng Tange24c7452009-12-14 14:20:22 -080096
Baruch Siach04f421e2013-12-30 20:30:44 +020097 ret = dw_spi_add_host(&pdev->dev, dws);
Feng Tange24c7452009-12-14 14:20:22 -080098 if (ret)
Baruch Siach04f421e2013-12-30 20:30:44 +020099 return ret;
Feng Tange24c7452009-12-14 14:20:22 -0800100
101 /* PCI hook and SPI hook use the same drv data */
102 pci_set_drvdata(pdev, dwpci);
Feng Tange24c7452009-12-14 14:20:22 -0800103
Andy Shevchenkofcf0af42014-08-29 12:41:39 +0300104 dev_info(&pdev->dev, "found PCI SPI controller(ID: %04x:%04x)\n",
105 pdev->vendor, pdev->device);
106
Baruch Siach04f421e2013-12-30 20:30:44 +0200107 return 0;
Feng Tange24c7452009-12-14 14:20:22 -0800108}
109
Grant Likelyfd4a3192012-12-07 16:57:14 +0000110static void spi_pci_remove(struct pci_dev *pdev)
Feng Tange24c7452009-12-14 14:20:22 -0800111{
112 struct dw_spi_pci *dwpci = pci_get_drvdata(pdev);
113
Feng Tang51f921c2010-01-20 13:49:45 -0700114 dw_spi_remove_host(&dwpci->dws);
Feng Tange24c7452009-12-14 14:20:22 -0800115}
116
Andy Shevchenko35f2d412014-08-29 12:41:41 +0300117#ifdef CONFIG_PM_SLEEP
118static int spi_suspend(struct device *dev)
Feng Tange24c7452009-12-14 14:20:22 -0800119{
Andy Shevchenko35f2d412014-08-29 12:41:41 +0300120 struct pci_dev *pdev = to_pci_dev(dev);
Feng Tange24c7452009-12-14 14:20:22 -0800121 struct dw_spi_pci *dwpci = pci_get_drvdata(pdev);
Feng Tange24c7452009-12-14 14:20:22 -0800122
Andy Shevchenko35f2d412014-08-29 12:41:41 +0300123 return dw_spi_suspend_host(&dwpci->dws);
Feng Tange24c7452009-12-14 14:20:22 -0800124}
125
Andy Shevchenko35f2d412014-08-29 12:41:41 +0300126static int spi_resume(struct device *dev)
Feng Tange24c7452009-12-14 14:20:22 -0800127{
Andy Shevchenko35f2d412014-08-29 12:41:41 +0300128 struct pci_dev *pdev = to_pci_dev(dev);
Feng Tange24c7452009-12-14 14:20:22 -0800129 struct dw_spi_pci *dwpci = pci_get_drvdata(pdev);
Feng Tange24c7452009-12-14 14:20:22 -0800130
Feng Tange24c7452009-12-14 14:20:22 -0800131 return dw_spi_resume_host(&dwpci->dws);
132}
Feng Tange24c7452009-12-14 14:20:22 -0800133#endif
134
Andy Shevchenko35f2d412014-08-29 12:41:41 +0300135static SIMPLE_DEV_PM_OPS(dw_spi_pm_ops, spi_suspend, spi_resume);
136
Jingoo Han9a21e472013-12-03 08:25:19 +0900137static const struct pci_device_id pci_ids[] = {
Feng Tang7063c0d2010-12-24 13:59:11 +0800138 /* Intel MID platform SPI controller 0 */
Andy Shevchenkod58cf5f2015-01-07 17:15:00 +0200139 /*
140 * The access to the device 8086:0801 is disabled by HW, since it's
141 * exclusively used by SCU to communicate with MSIC.
142 */
143 /* Intel MID platform SPI controller 1 */
144 { PCI_VDEVICE(INTEL, 0x0800), (kernel_ulong_t)&spi_pci_mid_desc_1},
145 /* Intel MID platform SPI controller 2 */
146 { PCI_VDEVICE(INTEL, 0x0812), (kernel_ulong_t)&spi_pci_mid_desc_2},
Feng Tange24c7452009-12-14 14:20:22 -0800147 {},
148};
149
150static struct pci_driver dw_spi_driver = {
151 .name = DRIVER_NAME,
152 .id_table = pci_ids,
153 .probe = spi_pci_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +0000154 .remove = spi_pci_remove,
Andy Shevchenko35f2d412014-08-29 12:41:41 +0300155 .driver = {
156 .pm = &dw_spi_pm_ops,
157 },
Feng Tange24c7452009-12-14 14:20:22 -0800158};
159
Axel Lin8ebb35f2012-04-04 22:29:32 +0800160module_pci_driver(dw_spi_driver);
Feng Tange24c7452009-12-14 14:20:22 -0800161
162MODULE_AUTHOR("Feng Tang <feng.tang@intel.com>");
163MODULE_DESCRIPTION("PCI interface driver for DW SPI Core");
164MODULE_LICENSE("GPL v2");