xref: /linux/drivers/net/ethernet/stmicro/stmmac/stmmac_pci.c (revision 4949009eb8d40a441dcddcd96e101e77d31cf1b2)
1 /*******************************************************************************
2   This contains the functions to handle the pci driver.
3 
4   Copyright (C) 2011-2012  Vayavya Labs Pvt Ltd
5 
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.
14 
15   You should have received a copy of the GNU General Public License along with
16   this program; if not, write to the Free Software Foundation, Inc.,
17   51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 
19   The full GNU General Public License is included in this distribution in
20   the file called "COPYING".
21 
22   Author: Rayagond Kokatanur <rayagond@vayavyalabs.com>
23   Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
24 *******************************************************************************/
25 
26 #include <linux/pci.h>
27 #include "stmmac.h"
28 
29 static void stmmac_default_data(struct plat_stmmacenet_data *plat)
30 {
31 	plat->bus_id = 1;
32 	plat->phy_addr = 0;
33 	plat->interface = PHY_INTERFACE_MODE_GMII;
34 	plat->clk_csr = 2;	/* clk_csr_i = 20-35MHz & MDC = clk_csr_i/16 */
35 	plat->has_gmac = 1;
36 	plat->force_sf_dma_mode = 1;
37 
38 	plat->mdio_bus_data->phy_reset = NULL;
39 	plat->mdio_bus_data->phy_mask = 0;
40 
41 	plat->dma_cfg->pbl = 32;
42 	plat->dma_cfg->burst_len = DMA_AXI_BLEN_256;
43 
44 	/* Set default value for multicast hash bins */
45 	plat->multicast_filter_bins = HASH_TABLE_SIZE;
46 
47 	/* Set default value for unicast filter entries */
48 	plat->unicast_filter_entries = 1;
49 }
50 
51 /**
52  * stmmac_pci_probe
53  *
54  * @pdev: pci device pointer
55  * @id: pointer to table of device id/id's.
56  *
57  * Description: This probing function gets called for all PCI devices which
58  * match the ID table and are not "owned" by other driver yet. This function
59  * gets passed a "struct pci_dev *" for each device whose entry in the ID table
60  * matches the device. The probe functions returns zero when the driver choose
61  * to take "ownership" of the device or an error code(-ve no) otherwise.
62  */
63 static int stmmac_pci_probe(struct pci_dev *pdev,
64 			    const struct pci_device_id *id)
65 {
66 	struct plat_stmmacenet_data *plat;
67 	struct stmmac_priv *priv;
68 	int i;
69 	int ret;
70 
71 	plat = devm_kzalloc(&pdev->dev, sizeof(*plat), GFP_KERNEL);
72 	if (!plat)
73 		return -ENOMEM;
74 
75 	plat->mdio_bus_data = devm_kzalloc(&pdev->dev,
76 					   sizeof(*plat->mdio_bus_data),
77 					   GFP_KERNEL);
78 	if (!plat->mdio_bus_data)
79 		return -ENOMEM;
80 
81 	plat->dma_cfg = devm_kzalloc(&pdev->dev, sizeof(*plat->dma_cfg),
82 				     GFP_KERNEL);
83 	if (!plat->dma_cfg)
84 		return -ENOMEM;
85 
86 	/* Enable pci device */
87 	ret = pcim_enable_device(pdev);
88 	if (ret) {
89 		dev_err(&pdev->dev, "%s: ERROR: failed to enable device\n",
90 			__func__);
91 		return ret;
92 	}
93 
94 	/* Get the base address of device */
95 	for (i = 0; i <= PCI_STD_RESOURCE_END; i++) {
96 		if (pci_resource_len(pdev, i) == 0)
97 			continue;
98 		ret = pcim_iomap_regions(pdev, BIT(i), pci_name(pdev));
99 		if (ret)
100 			return ret;
101 		break;
102 	}
103 
104 	pci_set_master(pdev);
105 
106 	stmmac_default_data(plat);
107 
108 	priv = stmmac_dvr_probe(&pdev->dev, plat, pcim_iomap_table(pdev)[i]);
109 	if (IS_ERR(priv)) {
110 		dev_err(&pdev->dev, "%s: main driver probe failed\n", __func__);
111 		return PTR_ERR(priv);
112 	}
113 	priv->dev->irq = pdev->irq;
114 	priv->wol_irq = pdev->irq;
115 
116 	pci_set_drvdata(pdev, priv->dev);
117 
118 	dev_dbg(&pdev->dev, "STMMAC PCI driver registration completed\n");
119 
120 	return 0;
121 }
122 
123 /**
124  * stmmac_pci_remove
125  *
126  * @pdev: platform device pointer
127  * Description: this function calls the main to free the net resources
128  * and releases the PCI resources.
129  */
130 static void stmmac_pci_remove(struct pci_dev *pdev)
131 {
132 	struct net_device *ndev = pci_get_drvdata(pdev);
133 
134 	stmmac_dvr_remove(ndev);
135 }
136 
137 #ifdef CONFIG_PM_SLEEP
138 static int stmmac_pci_suspend(struct device *dev)
139 {
140 	struct pci_dev *pdev = to_pci_dev(dev);
141 	struct net_device *ndev = pci_get_drvdata(pdev);
142 
143 	return stmmac_suspend(ndev);
144 }
145 
146 static int stmmac_pci_resume(struct device *dev)
147 {
148 	struct pci_dev *pdev = to_pci_dev(dev);
149 	struct net_device *ndev = pci_get_drvdata(pdev);
150 
151 	return stmmac_resume(ndev);
152 }
153 #endif
154 
155 static SIMPLE_DEV_PM_OPS(stmmac_pm_ops, stmmac_pci_suspend, stmmac_pci_resume);
156 
157 #define STMMAC_VENDOR_ID 0x700
158 #define STMMAC_DEVICE_ID 0x1108
159 
160 static const struct pci_device_id stmmac_id_table[] = {
161 	{PCI_DEVICE(STMMAC_VENDOR_ID, STMMAC_DEVICE_ID)},
162 	{PCI_DEVICE(PCI_VENDOR_ID_STMICRO, PCI_DEVICE_ID_STMICRO_MAC)},
163 	{}
164 };
165 
166 MODULE_DEVICE_TABLE(pci, stmmac_id_table);
167 
168 static struct pci_driver stmmac_pci_driver = {
169 	.name = STMMAC_RESOURCE_NAME,
170 	.id_table = stmmac_id_table,
171 	.probe = stmmac_pci_probe,
172 	.remove = stmmac_pci_remove,
173 	.driver         = {
174 		.pm     = &stmmac_pm_ops,
175 	},
176 };
177 
178 module_pci_driver(stmmac_pci_driver);
179 
180 MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet PCI driver");
181 MODULE_AUTHOR("Rayagond Kokatanur <rayagond.kokatanur@vayavyalabs.com>");
182 MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
183 MODULE_LICENSE("GPL");
184