1 /*******************************************************************************
2   This contains the functions to handle the platform driver.
3 
4   Copyright (C) 2007-2011  STMicroelectronics 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: Giuseppe Cavallaro <peppe.cavallaro@st.com>
23 *******************************************************************************/
24 
25 #include <linux/platform_device.h>
26 #include <linux/io.h>
27 #include "stmmac.h"
28 
29 /**
30  * stmmac_pltfr_probe
31  * @pdev: platform device pointer
32  * Description: platform_device probe function. It allocates
33  * the necessary resources and invokes the main to init
34  * the net device, register the mdio bus etc.
35  */
stmmac_pltfr_probe(struct platform_device * pdev)36 static int stmmac_pltfr_probe(struct platform_device *pdev)
37 {
38 	int ret = 0;
39 	struct resource *res;
40 	void __iomem *addr = NULL;
41 	struct stmmac_priv *priv = NULL;
42 	struct plat_stmmacenet_data *plat_dat;
43 
44 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
45 	if (!res)
46 		return -ENODEV;
47 
48 	if (!request_mem_region(res->start, resource_size(res), pdev->name)) {
49 		pr_err("%s: ERROR: memory allocation failed"
50 		       "cannot get the I/O addr 0x%x\n",
51 		       __func__, (unsigned int)res->start);
52 		return -EBUSY;
53 	}
54 
55 	addr = ioremap(res->start, resource_size(res));
56 	if (!addr) {
57 		pr_err("%s: ERROR: memory mapping failed", __func__);
58 		ret = -ENOMEM;
59 		goto out_release_region;
60 	}
61 	plat_dat = pdev->dev.platform_data;
62 
63 	/* Custom initialisation (if needed)*/
64 	if (plat_dat->init) {
65 		ret = plat_dat->init(pdev);
66 		if (unlikely(ret))
67 			goto out_unmap;
68 	}
69 
70 	priv = stmmac_dvr_probe(&(pdev->dev), plat_dat, addr);
71 	if (!priv) {
72 		pr_err("%s: main driver probe failed", __func__);
73 		goto out_unmap;
74 	}
75 
76 	/* Get the MAC information */
77 	priv->dev->irq = platform_get_irq_byname(pdev, "macirq");
78 	if (priv->dev->irq == -ENXIO) {
79 		pr_err("%s: ERROR: MAC IRQ configuration "
80 		       "information not found\n", __func__);
81 		ret = -ENXIO;
82 		goto out_unmap;
83 	}
84 
85 	/*
86 	 * On some platforms e.g. SPEAr the wake up irq differs from the mac irq
87 	 * The external wake up irq can be passed through the platform code
88 	 * named as "eth_wake_irq"
89 	 *
90 	 * In case the wake up interrupt is not passed from the platform
91 	 * so the driver will continue to use the mac irq (ndev->irq)
92 	 */
93 	priv->wol_irq = platform_get_irq_byname(pdev, "eth_wake_irq");
94 	if (priv->wol_irq == -ENXIO)
95 		priv->wol_irq = priv->dev->irq;
96 
97 	platform_set_drvdata(pdev, priv->dev);
98 
99 	pr_debug("STMMAC platform driver registration completed");
100 
101 	return 0;
102 
103 out_unmap:
104 	iounmap(addr);
105 	platform_set_drvdata(pdev, NULL);
106 
107 out_release_region:
108 	release_mem_region(res->start, resource_size(res));
109 
110 	return ret;
111 }
112 
113 /**
114  * stmmac_pltfr_remove
115  * @pdev: platform device pointer
116  * Description: this function calls the main to free the net resources
117  * and calls the platforms hook and release the resources (e.g. mem).
118  */
stmmac_pltfr_remove(struct platform_device * pdev)119 static int stmmac_pltfr_remove(struct platform_device *pdev)
120 {
121 	struct net_device *ndev = platform_get_drvdata(pdev);
122 	struct stmmac_priv *priv = netdev_priv(ndev);
123 	struct resource *res;
124 	int ret = stmmac_dvr_remove(ndev);
125 
126 	if (priv->plat->exit)
127 		priv->plat->exit(pdev);
128 
129 	if (priv->plat->exit)
130 		priv->plat->exit(pdev);
131 
132 	platform_set_drvdata(pdev, NULL);
133 
134 	iounmap((void *)priv->ioaddr);
135 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
136 	release_mem_region(res->start, resource_size(res));
137 
138 	return ret;
139 }
140 
141 #ifdef CONFIG_PM
stmmac_pltfr_suspend(struct device * dev)142 static int stmmac_pltfr_suspend(struct device *dev)
143 {
144 	struct net_device *ndev = dev_get_drvdata(dev);
145 
146 	return stmmac_suspend(ndev);
147 }
148 
stmmac_pltfr_resume(struct device * dev)149 static int stmmac_pltfr_resume(struct device *dev)
150 {
151 	struct net_device *ndev = dev_get_drvdata(dev);
152 
153 	return stmmac_resume(ndev);
154 }
155 
stmmac_pltfr_freeze(struct device * dev)156 int stmmac_pltfr_freeze(struct device *dev)
157 {
158 	struct net_device *ndev = dev_get_drvdata(dev);
159 
160 	return stmmac_freeze(ndev);
161 }
162 
stmmac_pltfr_restore(struct device * dev)163 int stmmac_pltfr_restore(struct device *dev)
164 {
165 	struct net_device *ndev = dev_get_drvdata(dev);
166 
167 	return stmmac_restore(ndev);
168 }
169 
170 static const struct dev_pm_ops stmmac_pltfr_pm_ops = {
171 	.suspend = stmmac_pltfr_suspend,
172 	.resume = stmmac_pltfr_resume,
173 	.freeze = stmmac_pltfr_freeze,
174 	.thaw = stmmac_pltfr_restore,
175 	.restore = stmmac_pltfr_restore,
176 };
177 #else
178 static const struct dev_pm_ops stmmac_pltfr_pm_ops;
179 #endif /* CONFIG_PM */
180 
181 static struct platform_driver stmmac_driver = {
182 	.probe = stmmac_pltfr_probe,
183 	.remove = stmmac_pltfr_remove,
184 	.driver = {
185 		   .name = STMMAC_RESOURCE_NAME,
186 		   .owner = THIS_MODULE,
187 		   .pm = &stmmac_pltfr_pm_ops,
188 		   },
189 };
190 
191 module_platform_driver(stmmac_driver);
192 
193 MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet PLATFORM driver");
194 MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
195 MODULE_LICENSE("GPL");
196