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 struct plat_stmmacenet_data plat_dat;
30 struct stmmac_mdio_bus_data mdio_data;
31 
32 static void stmmac_default_data(void)
33 {
34 	memset(&plat_dat, 0, sizeof(struct plat_stmmacenet_data));
35 	plat_dat.bus_id = 1;
36 	plat_dat.phy_addr = 0;
37 	plat_dat.interface = PHY_INTERFACE_MODE_GMII;
38 	plat_dat.pbl = 32;
39 	plat_dat.clk_csr = 2;	/* clk_csr_i = 20-35MHz & MDC = clk_csr_i/16 */
40 	plat_dat.has_gmac = 1;
41 	plat_dat.force_sf_dma_mode = 1;
42 
43 	mdio_data.bus_id = 1;
44 	mdio_data.phy_reset = NULL;
45 	mdio_data.phy_mask = 0;
46 	plat_dat.mdio_bus_data = &mdio_data;
47 }
48 
49 /**
50  * stmmac_pci_probe
51  *
52  * @pdev: pci device pointer
53  * @id: pointer to table of device id/id's.
54  *
55  * Description: This probing function gets called for all PCI devices which
56  * match the ID table and are not "owned" by other driver yet. This function
57  * gets passed a "struct pci_dev *" for each device whose entry in the ID table
58  * matches the device. The probe functions returns zero when the driver choose
59  * to take "ownership" of the device or an error code(-ve no) otherwise.
60  */
61 static int __devinit stmmac_pci_probe(struct pci_dev *pdev,
62 				      const struct pci_device_id *id)
63 {
64 	int ret = 0;
65 	void __iomem *addr = NULL;
66 	struct stmmac_priv *priv = NULL;
67 	int i;
68 
69 	/* Enable pci device */
70 	ret = pci_enable_device(pdev);
71 	if (ret) {
72 		pr_err("%s : ERROR: failed to enable %s device\n", __func__,
73 		       pci_name(pdev));
74 		return ret;
75 	}
76 	if (pci_request_regions(pdev, STMMAC_RESOURCE_NAME)) {
77 		pr_err("%s: ERROR: failed to get PCI region\n", __func__);
78 		ret = -ENODEV;
79 		goto err_out_req_reg_failed;
80 	}
81 
82 	/* Get the base address of device */
83 	for (i = 0; i <= 5; i++) {
84 		if (pci_resource_len(pdev, i) == 0)
85 			continue;
86 		addr = pci_iomap(pdev, i, 0);
87 		if (addr == NULL) {
88 			pr_err("%s: ERROR: cannot map regiser memory, aborting",
89 			       __func__);
90 			ret = -EIO;
91 			goto err_out_map_failed;
92 		}
93 		break;
94 	}
95 	pci_set_master(pdev);
96 
97 	stmmac_default_data();
98 
99 	priv = stmmac_dvr_probe(&(pdev->dev), &plat_dat);
100 	if (!priv) {
101 		pr_err("%s: main drivr probe failed", __func__);
102 		goto err_out;
103 	}
104 	priv->ioaddr = addr;
105 	priv->dev->base_addr = (unsigned long)addr;
106 	priv->dev->irq = pdev->irq;
107 	priv->wol_irq = pdev->irq;
108 
109 	pci_set_drvdata(pdev, priv->dev);
110 
111 	pr_debug("STMMAC platform driver registration completed");
112 
113 	return 0;
114 
115 err_out:
116 	pci_clear_master(pdev);
117 err_out_map_failed:
118 	pci_release_regions(pdev);
119 err_out_req_reg_failed:
120 	pci_disable_device(pdev);
121 
122 	return ret;
123 }
124 
125 /**
126  * stmmac_dvr_remove
127  *
128  * @pdev: platform device pointer
129  * Description: this function calls the main to free the net resources
130  * and releases the PCI resources.
131  */
132 static void __devexit stmmac_pci_remove(struct pci_dev *pdev)
133 {
134 	struct net_device *ndev = pci_get_drvdata(pdev);
135 	struct stmmac_priv *priv = netdev_priv(ndev);
136 
137 	stmmac_dvr_remove(ndev);
138 
139 	pci_set_drvdata(pdev, NULL);
140 	pci_iounmap(pdev, priv->ioaddr);
141 	pci_release_regions(pdev);
142 	pci_disable_device(pdev);
143 }
144 
145 #ifdef CONFIG_PM
146 static int stmmac_pci_suspend(struct pci_dev *pdev, pm_message_t state)
147 {
148 	struct net_device *ndev = pci_get_drvdata(pdev);
149 	int ret;
150 
151 	ret = stmmac_suspend(ndev);
152 	pci_save_state(pdev);
153 	pci_set_power_state(pdev, pci_choose_state(pdev, state));
154 
155 	return ret;
156 }
157 
158 static int stmmac_pci_resume(struct pci_dev *pdev)
159 {
160 	struct net_device *ndev = pci_get_drvdata(pdev);
161 
162 	pci_set_power_state(pdev, PCI_D0);
163 	pci_restore_state(pdev);
164 
165 	return stmmac_resume(ndev);
166 }
167 #endif
168 
169 #define STMMAC_VENDOR_ID 0x700
170 #define STMMAC_DEVICE_ID 0x1108
171 
172 static DEFINE_PCI_DEVICE_TABLE(stmmac_id_table) = {
173 	{PCI_DEVICE(STMMAC_VENDOR_ID, STMMAC_DEVICE_ID)},
174 	{PCI_DEVICE(PCI_VENDOR_ID_STMICRO, PCI_DEVICE_ID_STMICRO_MAC)},
175 	{}
176 };
177 
178 MODULE_DEVICE_TABLE(pci, stmmac_id_table);
179 
180 static struct pci_driver stmmac_driver = {
181 	.name = STMMAC_RESOURCE_NAME,
182 	.id_table = stmmac_id_table,
183 	.probe = stmmac_pci_probe,
184 	.remove = __devexit_p(stmmac_pci_remove),
185 #ifdef CONFIG_PM
186 	.suspend = stmmac_pci_suspend,
187 	.resume = stmmac_pci_resume,
188 #endif
189 };
190 
191 /**
192  * stmmac_init_module - Entry point for the driver
193  * Description: This function is the entry point for the driver.
194  */
195 static int __init stmmac_init_module(void)
196 {
197 	int ret;
198 
199 	ret = pci_register_driver(&stmmac_driver);
200 	if (ret < 0)
201 		pr_err("%s: ERROR: driver registration failed\n", __func__);
202 
203 	return ret;
204 }
205 
206 /**
207  * stmmac_cleanup_module - Cleanup routine for the driver
208  * Description: This function is the cleanup routine for the driver.
209  */
210 static void __exit stmmac_cleanup_module(void)
211 {
212 	pci_unregister_driver(&stmmac_driver);
213 }
214 
215 module_init(stmmac_init_module);
216 module_exit(stmmac_cleanup_module);
217 
218 MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet PCI driver");
219 MODULE_AUTHOR("Rayagond Kokatanur <rayagond.kokatanur@vayavyalabs.com>");
220 MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
221 MODULE_LICENSE("GPL");
222