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 register 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, addr);
100 	if (!priv) {
101 		pr_err("%s: main driver probe failed", __func__);
102 		goto err_out;
103 	}
104 	priv->dev->irq = pdev->irq;
105 	priv->wol_irq = pdev->irq;
106 
107 	pci_set_drvdata(pdev, priv->dev);
108 
109 	pr_debug("STMMAC platform driver registration completed");
110 
111 	return 0;
112 
113 err_out:
114 	pci_clear_master(pdev);
115 err_out_map_failed:
116 	pci_release_regions(pdev);
117 err_out_req_reg_failed:
118 	pci_disable_device(pdev);
119 
120 	return ret;
121 }
122 
123 /**
124  * stmmac_dvr_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 __devexit stmmac_pci_remove(struct pci_dev *pdev)
131 {
132 	struct net_device *ndev = pci_get_drvdata(pdev);
133 	struct stmmac_priv *priv = netdev_priv(ndev);
134 
135 	stmmac_dvr_remove(ndev);
136 
137 	pci_set_drvdata(pdev, NULL);
138 	pci_iounmap(pdev, priv->ioaddr);
139 	pci_release_regions(pdev);
140 	pci_disable_device(pdev);
141 }
142 
143 #ifdef CONFIG_PM
144 static int stmmac_pci_suspend(struct pci_dev *pdev, pm_message_t state)
145 {
146 	struct net_device *ndev = pci_get_drvdata(pdev);
147 	int ret;
148 
149 	ret = stmmac_suspend(ndev);
150 	pci_save_state(pdev);
151 	pci_set_power_state(pdev, pci_choose_state(pdev, state));
152 
153 	return ret;
154 }
155 
156 static int stmmac_pci_resume(struct pci_dev *pdev)
157 {
158 	struct net_device *ndev = pci_get_drvdata(pdev);
159 
160 	pci_set_power_state(pdev, PCI_D0);
161 	pci_restore_state(pdev);
162 
163 	return stmmac_resume(ndev);
164 }
165 #endif
166 
167 #define STMMAC_VENDOR_ID 0x700
168 #define STMMAC_DEVICE_ID 0x1108
169 
170 static DEFINE_PCI_DEVICE_TABLE(stmmac_id_table) = {
171 	{PCI_DEVICE(STMMAC_VENDOR_ID, STMMAC_DEVICE_ID)},
172 	{PCI_DEVICE(PCI_VENDOR_ID_STMICRO, PCI_DEVICE_ID_STMICRO_MAC)},
173 	{}
174 };
175 
176 MODULE_DEVICE_TABLE(pci, stmmac_id_table);
177 
178 static struct pci_driver stmmac_driver = {
179 	.name = STMMAC_RESOURCE_NAME,
180 	.id_table = stmmac_id_table,
181 	.probe = stmmac_pci_probe,
182 	.remove = __devexit_p(stmmac_pci_remove),
183 #ifdef CONFIG_PM
184 	.suspend = stmmac_pci_suspend,
185 	.resume = stmmac_pci_resume,
186 #endif
187 };
188 
189 /**
190  * stmmac_init_module - Entry point for the driver
191  * Description: This function is the entry point for the driver.
192  */
193 static int __init stmmac_init_module(void)
194 {
195 	int ret;
196 
197 	ret = pci_register_driver(&stmmac_driver);
198 	if (ret < 0)
199 		pr_err("%s: ERROR: driver registration failed\n", __func__);
200 
201 	return ret;
202 }
203 
204 /**
205  * stmmac_cleanup_module - Cleanup routine for the driver
206  * Description: This function is the cleanup routine for the driver.
207  */
208 static void __exit stmmac_cleanup_module(void)
209 {
210 	pci_unregister_driver(&stmmac_driver);
211 }
212 
213 module_init(stmmac_init_module);
214 module_exit(stmmac_cleanup_module);
215 
216 MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet PCI driver");
217 MODULE_AUTHOR("Rayagond Kokatanur <rayagond.kokatanur@vayavyalabs.com>");
218 MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
219 MODULE_LICENSE("GPL");
220