stmmac_platform.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*******************************************************************************
  2. This contains the functions to handle the platform driver.
  3. Copyright (C) 2007-2011 STMicroelectronics Ltd
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms and conditions of the GNU General Public License,
  6. version 2, as published by the Free Software Foundation.
  7. This program is distributed in the hope it will be useful, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  9. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  10. more details.
  11. You should have received a copy of the GNU General Public License along with
  12. this program; if not, write to the Free Software Foundation, Inc.,
  13. 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  14. The full GNU General Public License is included in this distribution in
  15. the file called "COPYING".
  16. Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
  17. *******************************************************************************/
  18. #include <linux/platform_device.h>
  19. #include <linux/io.h>
  20. #include <linux/of.h>
  21. #include <linux/of_net.h>
  22. #include <linux/of_device.h>
  23. #include "stmmac.h"
  24. static const struct of_device_id stmmac_dt_ids[] = {
  25. #ifdef CONFIG_DWMAC_SUNXI
  26. { .compatible = "allwinner,sun7i-a20-gmac", .data = &sun7i_gmac_data},
  27. #endif
  28. #ifdef CONFIG_DWMAC_STI
  29. { .compatible = "st,stih415-dwmac", .data = &sti_gmac_data},
  30. { .compatible = "st,stih416-dwmac", .data = &sti_gmac_data},
  31. { .compatible = "st,stid127-dwmac", .data = &sti_gmac_data},
  32. #endif
  33. #ifdef CONFIG_DWMAC_SOCFPGA
  34. { .compatible = "altr,socfpga-stmmac", .data = &socfpga_gmac_data },
  35. #endif
  36. /* SoC specific glue layers should come before generic bindings */
  37. { .compatible = "st,spear600-gmac"},
  38. { .compatible = "snps,dwmac-3.610"},
  39. { .compatible = "snps,dwmac-3.70a"},
  40. { .compatible = "snps,dwmac-3.710"},
  41. { .compatible = "snps,dwmac"},
  42. { /* sentinel */ }
  43. };
  44. MODULE_DEVICE_TABLE(of, stmmac_dt_ids);
  45. #ifdef CONFIG_OF
  46. static int stmmac_probe_config_dt(struct platform_device *pdev,
  47. struct plat_stmmacenet_data *plat,
  48. const char **mac)
  49. {
  50. struct device_node *np = pdev->dev.of_node;
  51. struct stmmac_dma_cfg *dma_cfg;
  52. const struct of_device_id *device;
  53. if (!np)
  54. return -ENODEV;
  55. device = of_match_device(stmmac_dt_ids, &pdev->dev);
  56. if (!device)
  57. return -ENODEV;
  58. if (device->data) {
  59. const struct stmmac_of_data *data = device->data;
  60. plat->has_gmac = data->has_gmac;
  61. plat->enh_desc = data->enh_desc;
  62. plat->tx_coe = data->tx_coe;
  63. plat->rx_coe = data->rx_coe;
  64. plat->bugged_jumbo = data->bugged_jumbo;
  65. plat->pmt = data->pmt;
  66. plat->riwt_off = data->riwt_off;
  67. plat->fix_mac_speed = data->fix_mac_speed;
  68. plat->bus_setup = data->bus_setup;
  69. plat->setup = data->setup;
  70. plat->free = data->free;
  71. plat->init = data->init;
  72. plat->exit = data->exit;
  73. }
  74. *mac = of_get_mac_address(np);
  75. plat->interface = of_get_phy_mode(np);
  76. /* Get max speed of operation from device tree */
  77. if (of_property_read_u32(np, "max-speed", &plat->max_speed))
  78. plat->max_speed = -1;
  79. plat->bus_id = of_alias_get_id(np, "ethernet");
  80. if (plat->bus_id < 0)
  81. plat->bus_id = 0;
  82. /* Default to phy auto-detection */
  83. plat->phy_addr = -1;
  84. /* "snps,phy-addr" is not a standard property. Mark it as deprecated
  85. * and warn of its use. Remove this when phy node support is added.
  86. */
  87. if (of_property_read_u32(np, "snps,phy-addr", &plat->phy_addr) == 0)
  88. dev_warn(&pdev->dev, "snps,phy-addr property is deprecated\n");
  89. plat->mdio_bus_data = devm_kzalloc(&pdev->dev,
  90. sizeof(struct stmmac_mdio_bus_data),
  91. GFP_KERNEL);
  92. plat->force_sf_dma_mode = of_property_read_bool(np, "snps,force_sf_dma_mode");
  93. /* Set the maxmtu to a default of JUMBO_LEN in case the
  94. * parameter is not present in the device tree.
  95. */
  96. plat->maxmtu = JUMBO_LEN;
  97. /*
  98. * Currently only the properties needed on SPEAr600
  99. * are provided. All other properties should be added
  100. * once needed on other platforms.
  101. */
  102. if (of_device_is_compatible(np, "st,spear600-gmac") ||
  103. of_device_is_compatible(np, "snps,dwmac-3.70a") ||
  104. of_device_is_compatible(np, "snps,dwmac")) {
  105. /* Note that the max-frame-size parameter as defined in the
  106. * ePAPR v1.1 spec is defined as max-frame-size, it's
  107. * actually used as the IEEE definition of MAC Client
  108. * data, or MTU. The ePAPR specification is confusing as
  109. * the definition is max-frame-size, but usage examples
  110. * are clearly MTUs
  111. */
  112. of_property_read_u32(np, "max-frame-size", &plat->maxmtu);
  113. plat->has_gmac = 1;
  114. plat->pmt = 1;
  115. }
  116. if (of_device_is_compatible(np, "snps,dwmac-3.610") ||
  117. of_device_is_compatible(np, "snps,dwmac-3.710")) {
  118. plat->enh_desc = 1;
  119. plat->bugged_jumbo = 1;
  120. plat->force_sf_dma_mode = 1;
  121. }
  122. if (of_find_property(np, "snps,pbl", NULL)) {
  123. dma_cfg = devm_kzalloc(&pdev->dev, sizeof(*dma_cfg),
  124. GFP_KERNEL);
  125. if (!dma_cfg)
  126. return -ENOMEM;
  127. plat->dma_cfg = dma_cfg;
  128. of_property_read_u32(np, "snps,pbl", &dma_cfg->pbl);
  129. dma_cfg->fixed_burst =
  130. of_property_read_bool(np, "snps,fixed-burst");
  131. dma_cfg->mixed_burst =
  132. of_property_read_bool(np, "snps,mixed-burst");
  133. }
  134. plat->force_thresh_dma_mode = of_property_read_bool(np, "snps,force_thresh_dma_mode");
  135. if (plat->force_thresh_dma_mode) {
  136. plat->force_sf_dma_mode = 0;
  137. pr_warn("force_sf_dma_mode is ignored if force_thresh_dma_mode is set.");
  138. }
  139. return 0;
  140. }
  141. #else
  142. static int stmmac_probe_config_dt(struct platform_device *pdev,
  143. struct plat_stmmacenet_data *plat,
  144. const char **mac)
  145. {
  146. return -ENOSYS;
  147. }
  148. #endif /* CONFIG_OF */
  149. /**
  150. * stmmac_pltfr_probe
  151. * @pdev: platform device pointer
  152. * Description: platform_device probe function. It allocates
  153. * the necessary resources and invokes the main to init
  154. * the net device, register the mdio bus etc.
  155. */
  156. static int stmmac_pltfr_probe(struct platform_device *pdev)
  157. {
  158. int ret = 0;
  159. struct resource *res;
  160. struct device *dev = &pdev->dev;
  161. void __iomem *addr = NULL;
  162. struct stmmac_priv *priv = NULL;
  163. struct plat_stmmacenet_data *plat_dat = NULL;
  164. const char *mac = NULL;
  165. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  166. addr = devm_ioremap_resource(dev, res);
  167. if (IS_ERR(addr))
  168. return PTR_ERR(addr);
  169. plat_dat = dev_get_platdata(&pdev->dev);
  170. if (pdev->dev.of_node) {
  171. if (!plat_dat)
  172. plat_dat = devm_kzalloc(&pdev->dev,
  173. sizeof(struct plat_stmmacenet_data),
  174. GFP_KERNEL);
  175. if (!plat_dat) {
  176. pr_err("%s: ERROR: no memory", __func__);
  177. return -ENOMEM;
  178. }
  179. ret = stmmac_probe_config_dt(pdev, plat_dat, &mac);
  180. if (ret) {
  181. pr_err("%s: main dt probe failed", __func__);
  182. return ret;
  183. }
  184. }
  185. /* Custom setup (if needed) */
  186. if (plat_dat->setup) {
  187. plat_dat->bsp_priv = plat_dat->setup(pdev);
  188. if (IS_ERR(plat_dat->bsp_priv))
  189. return PTR_ERR(plat_dat->bsp_priv);
  190. }
  191. /* Custom initialisation (if needed)*/
  192. if (plat_dat->init) {
  193. ret = plat_dat->init(pdev, plat_dat->bsp_priv);
  194. if (unlikely(ret))
  195. return ret;
  196. }
  197. priv = stmmac_dvr_probe(&(pdev->dev), plat_dat, addr);
  198. if (IS_ERR(priv)) {
  199. pr_err("%s: main driver probe failed", __func__);
  200. return PTR_ERR(priv);
  201. }
  202. /* Get MAC address if available (DT) */
  203. if (mac)
  204. memcpy(priv->dev->dev_addr, mac, ETH_ALEN);
  205. /* Get the MAC information */
  206. priv->dev->irq = platform_get_irq_byname(pdev, "macirq");
  207. if (priv->dev->irq < 0) {
  208. if (priv->dev->irq != -EPROBE_DEFER) {
  209. netdev_err(priv->dev,
  210. "MAC IRQ configuration information not found\n");
  211. }
  212. return priv->dev->irq;
  213. }
  214. /*
  215. * On some platforms e.g. SPEAr the wake up irq differs from the mac irq
  216. * The external wake up irq can be passed through the platform code
  217. * named as "eth_wake_irq"
  218. *
  219. * In case the wake up interrupt is not passed from the platform
  220. * so the driver will continue to use the mac irq (ndev->irq)
  221. */
  222. priv->wol_irq = platform_get_irq_byname(pdev, "eth_wake_irq");
  223. if (priv->wol_irq < 0) {
  224. if (priv->wol_irq == -EPROBE_DEFER)
  225. return -EPROBE_DEFER;
  226. priv->wol_irq = priv->dev->irq;
  227. }
  228. priv->lpi_irq = platform_get_irq_byname(pdev, "eth_lpi");
  229. if (priv->lpi_irq == -EPROBE_DEFER)
  230. return -EPROBE_DEFER;
  231. platform_set_drvdata(pdev, priv->dev);
  232. pr_debug("STMMAC platform driver registration completed");
  233. return 0;
  234. }
  235. /**
  236. * stmmac_pltfr_remove
  237. * @pdev: platform device pointer
  238. * Description: this function calls the main to free the net resources
  239. * and calls the platforms hook and release the resources (e.g. mem).
  240. */
  241. static int stmmac_pltfr_remove(struct platform_device *pdev)
  242. {
  243. struct net_device *ndev = platform_get_drvdata(pdev);
  244. struct stmmac_priv *priv = netdev_priv(ndev);
  245. int ret = stmmac_dvr_remove(ndev);
  246. if (priv->plat->exit)
  247. priv->plat->exit(pdev, priv->plat->bsp_priv);
  248. if (priv->plat->free)
  249. priv->plat->free(pdev, priv->plat->bsp_priv);
  250. return ret;
  251. }
  252. #ifdef CONFIG_PM
  253. static int stmmac_pltfr_suspend(struct device *dev)
  254. {
  255. int ret;
  256. struct net_device *ndev = dev_get_drvdata(dev);
  257. struct stmmac_priv *priv = netdev_priv(ndev);
  258. struct platform_device *pdev = to_platform_device(dev);
  259. ret = stmmac_suspend(ndev);
  260. if (priv->plat->exit)
  261. priv->plat->exit(pdev, priv->plat->bsp_priv);
  262. return ret;
  263. }
  264. static int stmmac_pltfr_resume(struct device *dev)
  265. {
  266. struct net_device *ndev = dev_get_drvdata(dev);
  267. struct stmmac_priv *priv = netdev_priv(ndev);
  268. struct platform_device *pdev = to_platform_device(dev);
  269. if (priv->plat->init)
  270. priv->plat->init(pdev, priv->plat->bsp_priv);
  271. return stmmac_resume(ndev);
  272. }
  273. #endif /* CONFIG_PM */
  274. static SIMPLE_DEV_PM_OPS(stmmac_pltfr_pm_ops,
  275. stmmac_pltfr_suspend, stmmac_pltfr_resume);
  276. struct platform_driver stmmac_pltfr_driver = {
  277. .probe = stmmac_pltfr_probe,
  278. .remove = stmmac_pltfr_remove,
  279. .driver = {
  280. .name = STMMAC_RESOURCE_NAME,
  281. .owner = THIS_MODULE,
  282. .pm = &stmmac_pltfr_pm_ops,
  283. .of_match_table = of_match_ptr(stmmac_dt_ids),
  284. },
  285. };
  286. MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet PLATFORM driver");
  287. MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
  288. MODULE_LICENSE("GPL");