blob: ef7db75c92c13b34af62dd6c0377d3aa16bfcabd [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
Andy Shevchenkoc95791b2014-08-29 12:41:42 +030026struct spi_pci_desc {
27 int (*setup)(struct dw_spi *);
Andy Shevchenkod58cf5f2015-01-07 17:15:00 +020028 u16 num_cs;
29 u16 bus_num;
Andy Shevchenkoc95791b2014-08-29 12:41:42 +030030};
31
Andy Shevchenkod58cf5f2015-01-07 17:15:00 +020032static struct spi_pci_desc spi_pci_mid_desc_1 = {
Andy Shevchenkoc95791b2014-08-29 12:41:42 +030033 .setup = dw_spi_mid_init,
Andy Shevchenko307ed832015-02-23 17:55:54 +020034 .num_cs = 5,
Andy Shevchenkod58cf5f2015-01-07 17:15:00 +020035 .bus_num = 0,
36};
37
38static struct spi_pci_desc spi_pci_mid_desc_2 = {
39 .setup = dw_spi_mid_init,
Andy Shevchenko307ed832015-02-23 17:55:54 +020040 .num_cs = 2,
Andy Shevchenkod58cf5f2015-01-07 17:15:00 +020041 .bus_num = 1,
Andy Shevchenkoc95791b2014-08-29 12:41:42 +030042};
43
44static int spi_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Feng Tange24c7452009-12-14 14:20:22 -080045{
Feng Tange24c7452009-12-14 14:20:22 -080046 struct dw_spi *dws;
Andy Shevchenkoc95791b2014-08-29 12:41:42 +030047 struct spi_pci_desc *desc = (struct spi_pci_desc *)ent->driver_data;
Feng Tange24c7452009-12-14 14:20:22 -080048 int pci_bar = 0;
49 int ret;
50
Baruch Siach04f421e2013-12-30 20:30:44 +020051 ret = pcim_enable_device(pdev);
Feng Tange24c7452009-12-14 14:20:22 -080052 if (ret)
53 return ret;
54
Andy Shevchenko1c2df962015-10-14 23:12:24 +030055 dws = devm_kzalloc(&pdev->dev, sizeof(*dws), GFP_KERNEL);
56 if (!dws)
Baruch Siach04f421e2013-12-30 20:30:44 +020057 return -ENOMEM;
Feng Tange24c7452009-12-14 14:20:22 -080058
Feng Tange24c7452009-12-14 14:20:22 -080059 /* 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];
Feng Tange24c7452009-12-14 14:20:22 -080067 dws->irq = pdev->irq;
Feng Tang7063c0d2010-12-24 13:59:11 +080068
69 /*
Geert Uytterhoeven3208a1c2016-05-10 20:59:58 +020070 * Specific handling for platforms, like dma setup,
Feng Tang7063c0d2010-12-24 13:59:11 +080071 * clock rate, FIFO depth.
72 */
Andy Shevchenkod58cf5f2015-01-07 17:15:00 +020073 if (desc) {
Andy Shevchenkod9c14742015-01-22 17:59:34 +020074 dws->num_cs = desc->num_cs;
75 dws->bus_num = desc->bus_num;
76
Andy Shevchenkod58cf5f2015-01-07 17:15:00 +020077 if (desc->setup) {
78 ret = desc->setup(dws);
79 if (ret)
80 return ret;
81 }
Andy Shevchenkod58cf5f2015-01-07 17:15:00 +020082 } else {
83 return -ENODEV;
Feng Tang7063c0d2010-12-24 13:59:11 +080084 }
Feng Tange24c7452009-12-14 14:20:22 -080085
Baruch Siach04f421e2013-12-30 20:30:44 +020086 ret = dw_spi_add_host(&pdev->dev, dws);
Feng Tange24c7452009-12-14 14:20:22 -080087 if (ret)
Baruch Siach04f421e2013-12-30 20:30:44 +020088 return ret;
Feng Tange24c7452009-12-14 14:20:22 -080089
90 /* PCI hook and SPI hook use the same drv data */
Andy Shevchenko1c2df962015-10-14 23:12:24 +030091 pci_set_drvdata(pdev, dws);
Feng Tange24c7452009-12-14 14:20:22 -080092
Andy Shevchenkofcf0af42014-08-29 12:41:39 +030093 dev_info(&pdev->dev, "found PCI SPI controller(ID: %04x:%04x)\n",
94 pdev->vendor, pdev->device);
95
Baruch Siach04f421e2013-12-30 20:30:44 +020096 return 0;
Feng Tange24c7452009-12-14 14:20:22 -080097}
98
Grant Likelyfd4a3192012-12-07 16:57:14 +000099static void spi_pci_remove(struct pci_dev *pdev)
Feng Tange24c7452009-12-14 14:20:22 -0800100{
Andy Shevchenko1c2df962015-10-14 23:12:24 +0300101 struct dw_spi *dws = pci_get_drvdata(pdev);
Feng Tange24c7452009-12-14 14:20:22 -0800102
Andy Shevchenko1c2df962015-10-14 23:12:24 +0300103 dw_spi_remove_host(dws);
Feng Tange24c7452009-12-14 14:20:22 -0800104}
105
Andy Shevchenko35f2d412014-08-29 12:41:41 +0300106#ifdef CONFIG_PM_SLEEP
107static int spi_suspend(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_suspend_host(dws);
Feng Tange24c7452009-12-14 14:20:22 -0800113}
114
Andy Shevchenko35f2d412014-08-29 12:41:41 +0300115static int spi_resume(struct device *dev)
Feng Tange24c7452009-12-14 14:20:22 -0800116{
Andy Shevchenko35f2d412014-08-29 12:41:41 +0300117 struct pci_dev *pdev = to_pci_dev(dev);
Andy Shevchenko1c2df962015-10-14 23:12:24 +0300118 struct dw_spi *dws = pci_get_drvdata(pdev);
Feng Tange24c7452009-12-14 14:20:22 -0800119
Andy Shevchenko1c2df962015-10-14 23:12:24 +0300120 return dw_spi_resume_host(dws);
Feng Tange24c7452009-12-14 14:20:22 -0800121}
Feng Tange24c7452009-12-14 14:20:22 -0800122#endif
123
Andy Shevchenko35f2d412014-08-29 12:41:41 +0300124static SIMPLE_DEV_PM_OPS(dw_spi_pm_ops, spi_suspend, spi_resume);
125
Jingoo Han9a21e472013-12-03 08:25:19 +0900126static const struct pci_device_id pci_ids[] = {
Feng Tang7063c0d2010-12-24 13:59:11 +0800127 /* Intel MID platform SPI controller 0 */
Andy Shevchenkod58cf5f2015-01-07 17:15:00 +0200128 /*
129 * The access to the device 8086:0801 is disabled by HW, since it's
130 * exclusively used by SCU to communicate with MSIC.
131 */
132 /* Intel MID platform SPI controller 1 */
133 { PCI_VDEVICE(INTEL, 0x0800), (kernel_ulong_t)&spi_pci_mid_desc_1},
134 /* Intel MID platform SPI controller 2 */
135 { PCI_VDEVICE(INTEL, 0x0812), (kernel_ulong_t)&spi_pci_mid_desc_2},
Feng Tange24c7452009-12-14 14:20:22 -0800136 {},
137};
138
139static struct pci_driver dw_spi_driver = {
140 .name = DRIVER_NAME,
141 .id_table = pci_ids,
142 .probe = spi_pci_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +0000143 .remove = spi_pci_remove,
Andy Shevchenko35f2d412014-08-29 12:41:41 +0300144 .driver = {
145 .pm = &dw_spi_pm_ops,
146 },
Feng Tange24c7452009-12-14 14:20:22 -0800147};
148
Axel Lin8ebb35f2012-04-04 22:29:32 +0800149module_pci_driver(dw_spi_driver);
Feng Tange24c7452009-12-14 14:20:22 -0800150
151MODULE_AUTHOR("Feng Tang <feng.tang@intel.com>");
152MODULE_DESCRIPTION("PCI interface driver for DW SPI Core");
153MODULE_LICENSE("GPL v2");