blob: ba68da12cdf090e11b1d013bcc48315c74c44013 [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 *);
33};
34
35static struct spi_pci_desc spi_pci_mid_desc = {
36 .setup = dw_spi_mid_init,
37};
38
39static int spi_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Feng Tange24c7452009-12-14 14:20:22 -080040{
41 struct dw_spi_pci *dwpci;
42 struct dw_spi *dws;
Andy Shevchenkoc95791b2014-08-29 12:41:42 +030043 struct spi_pci_desc *desc = (struct spi_pci_desc *)ent->driver_data;
Feng Tange24c7452009-12-14 14:20:22 -080044 int pci_bar = 0;
45 int ret;
46
Baruch Siach04f421e2013-12-30 20:30:44 +020047 ret = pcim_enable_device(pdev);
Feng Tange24c7452009-12-14 14:20:22 -080048 if (ret)
49 return ret;
50
Baruch Siachfa4934a2014-01-06 09:21:33 +020051 dwpci = devm_kzalloc(&pdev->dev, sizeof(struct dw_spi_pci),
52 GFP_KERNEL);
Baruch Siach04f421e2013-12-30 20:30:44 +020053 if (!dwpci)
54 return -ENOMEM;
Feng Tange24c7452009-12-14 14:20:22 -080055
56 dwpci->pdev = pdev;
57 dws = &dwpci->dws;
58
59 /* Get basic io resource and map it */
60 dws->paddr = pci_resource_start(pdev, pci_bar);
Feng Tange24c7452009-12-14 14:20:22 -080061
Andy Shevchenkoceb86de2014-08-29 12:41:40 +030062 ret = pcim_iomap_regions(pdev, 1 << pci_bar, pci_name(pdev));
Feng Tange24c7452009-12-14 14:20:22 -080063 if (ret)
Baruch Siach04f421e2013-12-30 20:30:44 +020064 return ret;
Feng Tange24c7452009-12-14 14:20:22 -080065
Andy Shevchenkoc9d5d6fe2014-08-27 16:21:12 +030066 dws->regs = pcim_iomap_table(pdev)[pci_bar];
67
Feng Tange24c7452009-12-14 14:20:22 -080068 dws->bus_num = 0;
69 dws->num_cs = 4;
Feng Tange24c7452009-12-14 14:20:22 -080070 dws->irq = pdev->irq;
Feng Tang7063c0d2010-12-24 13:59:11 +080071
72 /*
Andy Shevchenkoc95791b2014-08-29 12:41:42 +030073 * Specific handling for paltforms, like dma setup,
Feng Tang7063c0d2010-12-24 13:59:11 +080074 * clock rate, FIFO depth.
75 */
Andy Shevchenkoc95791b2014-08-29 12:41:42 +030076 if (desc && desc->setup) {
77 ret = desc->setup(dws);
Feng Tang7063c0d2010-12-24 13:59:11 +080078 if (ret)
Baruch Siach04f421e2013-12-30 20:30:44 +020079 return ret;
Feng Tang7063c0d2010-12-24 13:59:11 +080080 }
Feng Tange24c7452009-12-14 14:20:22 -080081
Baruch Siach04f421e2013-12-30 20:30:44 +020082 ret = dw_spi_add_host(&pdev->dev, dws);
Feng Tange24c7452009-12-14 14:20:22 -080083 if (ret)
Baruch Siach04f421e2013-12-30 20:30:44 +020084 return ret;
Feng Tange24c7452009-12-14 14:20:22 -080085
86 /* PCI hook and SPI hook use the same drv data */
87 pci_set_drvdata(pdev, dwpci);
Feng Tange24c7452009-12-14 14:20:22 -080088
Andy Shevchenkofcf0af42014-08-29 12:41:39 +030089 dev_info(&pdev->dev, "found PCI SPI controller(ID: %04x:%04x)\n",
90 pdev->vendor, pdev->device);
91
Baruch Siach04f421e2013-12-30 20:30:44 +020092 return 0;
Feng Tange24c7452009-12-14 14:20:22 -080093}
94
Grant Likelyfd4a3192012-12-07 16:57:14 +000095static void spi_pci_remove(struct pci_dev *pdev)
Feng Tange24c7452009-12-14 14:20:22 -080096{
97 struct dw_spi_pci *dwpci = pci_get_drvdata(pdev);
98
Feng Tang51f921c2010-01-20 13:49:45 -070099 dw_spi_remove_host(&dwpci->dws);
Feng Tange24c7452009-12-14 14:20:22 -0800100}
101
Andy Shevchenko35f2d412014-08-29 12:41:41 +0300102#ifdef CONFIG_PM_SLEEP
103static int spi_suspend(struct device *dev)
Feng Tange24c7452009-12-14 14:20:22 -0800104{
Andy Shevchenko35f2d412014-08-29 12:41:41 +0300105 struct pci_dev *pdev = to_pci_dev(dev);
Feng Tange24c7452009-12-14 14:20:22 -0800106 struct dw_spi_pci *dwpci = pci_get_drvdata(pdev);
Feng Tange24c7452009-12-14 14:20:22 -0800107
Andy Shevchenko35f2d412014-08-29 12:41:41 +0300108 return dw_spi_suspend_host(&dwpci->dws);
Feng Tange24c7452009-12-14 14:20:22 -0800109}
110
Andy Shevchenko35f2d412014-08-29 12:41:41 +0300111static int spi_resume(struct device *dev)
Feng Tange24c7452009-12-14 14:20:22 -0800112{
Andy Shevchenko35f2d412014-08-29 12:41:41 +0300113 struct pci_dev *pdev = to_pci_dev(dev);
Feng Tange24c7452009-12-14 14:20:22 -0800114 struct dw_spi_pci *dwpci = pci_get_drvdata(pdev);
Feng Tange24c7452009-12-14 14:20:22 -0800115
Feng Tange24c7452009-12-14 14:20:22 -0800116 return dw_spi_resume_host(&dwpci->dws);
117}
Feng Tange24c7452009-12-14 14:20:22 -0800118#endif
119
Andy Shevchenko35f2d412014-08-29 12:41:41 +0300120static SIMPLE_DEV_PM_OPS(dw_spi_pm_ops, spi_suspend, spi_resume);
121
Jingoo Han9a21e472013-12-03 08:25:19 +0900122static const struct pci_device_id pci_ids[] = {
Feng Tang7063c0d2010-12-24 13:59:11 +0800123 /* Intel MID platform SPI controller 0 */
Andy Shevchenkoc95791b2014-08-29 12:41:42 +0300124 { PCI_VDEVICE(INTEL, 0x0800), (kernel_ulong_t)&spi_pci_mid_desc},
Feng Tange24c7452009-12-14 14:20:22 -0800125 {},
126};
127
128static struct pci_driver dw_spi_driver = {
129 .name = DRIVER_NAME,
130 .id_table = pci_ids,
131 .probe = spi_pci_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +0000132 .remove = spi_pci_remove,
Andy Shevchenko35f2d412014-08-29 12:41:41 +0300133 .driver = {
134 .pm = &dw_spi_pm_ops,
135 },
Feng Tange24c7452009-12-14 14:20:22 -0800136};
137
Axel Lin8ebb35f2012-04-04 22:29:32 +0800138module_pci_driver(dw_spi_driver);
Feng Tange24c7452009-12-14 14:20:22 -0800139
140MODULE_AUTHOR("Feng Tang <feng.tang@intel.com>");
141MODULE_DESCRIPTION("PCI interface driver for DW SPI Core");
142MODULE_LICENSE("GPL v2");