stmmac_platform.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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_MESON
  26. { .compatible = "amlogic,meson6-dwmac", .data = &meson6_dwmac_data},
  27. #endif
  28. #ifdef CONFIG_DWMAC_SUNXI
  29. { .compatible = "allwinner,sun7i-a20-gmac", .data = &sun7i_gmac_data},
  30. #endif
  31. #ifdef CONFIG_DWMAC_STI
  32. { .compatible = "st,stih415-dwmac", .data = &stih4xx_dwmac_data},
  33. { .compatible = "st,stih416-dwmac", .data = &stih4xx_dwmac_data},
  34. { .compatible = "st,stid127-dwmac", .data = &stid127_dwmac_data},
  35. { .compatible = "st,stih407-dwmac", .data = &stih4xx_dwmac_data},
  36. #endif
  37. #ifdef CONFIG_DWMAC_SOCFPGA
  38. { .compatible = "altr,socfpga-stmmac", .data = &socfpga_gmac_data },
  39. #endif
  40. /* SoC specific glue layers should come before generic bindings */
  41. { .compatible = "st,spear600-gmac"},
  42. { .compatible = "snps,dwmac-3.610"},
  43. { .compatible = "snps,dwmac-3.70a"},
  44. { .compatible = "snps,dwmac-3.710"},
  45. { .compatible = "snps,dwmac"},
  46. { /* sentinel */ }
  47. };
  48. MODULE_DEVICE_TABLE(of, stmmac_dt_ids);
  49. #ifdef CONFIG_OF
  50. /* This function validates the number of Multicast filtering bins specified
  51. * by the configuration through the device tree. The Synopsys GMAC supports
  52. * 64 bins, 128 bins, or 256 bins. "bins" refer to the division of CRC
  53. * number space. 64 bins correspond to 6 bits of the CRC, 128 corresponds
  54. * to 7 bits, and 256 refers to 8 bits of the CRC. Any other setting is
  55. * invalid and will cause the filtering algorithm to use Multicast
  56. * promiscuous mode.
  57. */
  58. static int dwmac1000_validate_mcast_bins(int mcast_bins)
  59. {
  60. int x = mcast_bins;
  61. switch (x) {
  62. case HASH_TABLE_SIZE:
  63. case 128:
  64. case 256:
  65. break;
  66. default:
  67. x = 0;
  68. pr_info("Hash table entries set to unexpected value %d",
  69. mcast_bins);
  70. break;
  71. }
  72. return x;
  73. }
  74. /* This function validates the number of Unicast address entries supported
  75. * by a particular Synopsys 10/100/1000 controller. The Synopsys controller
  76. * supports 1, 32, 64, or 128 Unicast filter entries for it's Unicast filter
  77. * logic. This function validates a valid, supported configuration is
  78. * selected, and defaults to 1 Unicast address if an unsupported
  79. * configuration is selected.
  80. */
  81. static int dwmac1000_validate_ucast_entries(int ucast_entries)
  82. {
  83. int x = ucast_entries;
  84. switch (x) {
  85. case 1:
  86. case 32:
  87. case 64:
  88. case 128:
  89. break;
  90. default:
  91. x = 1;
  92. pr_info("Unicast table entries set to unexpected value %d\n",
  93. ucast_entries);
  94. break;
  95. }
  96. return x;
  97. }
  98. static int stmmac_probe_config_dt(struct platform_device *pdev,
  99. struct plat_stmmacenet_data *plat,
  100. const char **mac)
  101. {
  102. struct device_node *np = pdev->dev.of_node;
  103. struct stmmac_dma_cfg *dma_cfg;
  104. const struct of_device_id *device;
  105. if (!np)
  106. return -ENODEV;
  107. device = of_match_device(stmmac_dt_ids, &pdev->dev);
  108. if (!device)
  109. return -ENODEV;
  110. if (device->data) {
  111. const struct stmmac_of_data *data = device->data;
  112. plat->has_gmac = data->has_gmac;
  113. plat->enh_desc = data->enh_desc;
  114. plat->tx_coe = data->tx_coe;
  115. plat->rx_coe = data->rx_coe;
  116. plat->bugged_jumbo = data->bugged_jumbo;
  117. plat->pmt = data->pmt;
  118. plat->riwt_off = data->riwt_off;
  119. plat->fix_mac_speed = data->fix_mac_speed;
  120. plat->bus_setup = data->bus_setup;
  121. plat->setup = data->setup;
  122. plat->free = data->free;
  123. plat->init = data->init;
  124. plat->exit = data->exit;
  125. }
  126. *mac = of_get_mac_address(np);
  127. plat->interface = of_get_phy_mode(np);
  128. /* Get max speed of operation from device tree */
  129. if (of_property_read_u32(np, "max-speed", &plat->max_speed))
  130. plat->max_speed = -1;
  131. plat->bus_id = of_alias_get_id(np, "ethernet");
  132. if (plat->bus_id < 0)
  133. plat->bus_id = 0;
  134. /* Default to phy auto-detection */
  135. plat->phy_addr = -1;
  136. /* "snps,phy-addr" is not a standard property. Mark it as deprecated
  137. * and warn of its use. Remove this when phy node support is added.
  138. */
  139. if (of_property_read_u32(np, "snps,phy-addr", &plat->phy_addr) == 0)
  140. dev_warn(&pdev->dev, "snps,phy-addr property is deprecated\n");
  141. if (plat->phy_bus_name)
  142. plat->mdio_bus_data = NULL;
  143. else
  144. plat->mdio_bus_data =
  145. devm_kzalloc(&pdev->dev,
  146. sizeof(struct stmmac_mdio_bus_data),
  147. GFP_KERNEL);
  148. plat->force_sf_dma_mode =
  149. of_property_read_bool(np, "snps,force_sf_dma_mode");
  150. /* Set the maxmtu to a default of JUMBO_LEN in case the
  151. * parameter is not present in the device tree.
  152. */
  153. plat->maxmtu = JUMBO_LEN;
  154. /* Set default value for multicast hash bins */
  155. plat->multicast_filter_bins = HASH_TABLE_SIZE;
  156. /* Set default value for unicast filter entries */
  157. plat->unicast_filter_entries = 1;
  158. /*
  159. * Currently only the properties needed on SPEAr600
  160. * are provided. All other properties should be added
  161. * once needed on other platforms.
  162. */
  163. if (of_device_is_compatible(np, "st,spear600-gmac") ||
  164. of_device_is_compatible(np, "snps,dwmac-3.70a") ||
  165. of_device_is_compatible(np, "snps,dwmac")) {
  166. /* Note that the max-frame-size parameter as defined in the
  167. * ePAPR v1.1 spec is defined as max-frame-size, it's
  168. * actually used as the IEEE definition of MAC Client
  169. * data, or MTU. The ePAPR specification is confusing as
  170. * the definition is max-frame-size, but usage examples
  171. * are clearly MTUs
  172. */
  173. of_property_read_u32(np, "max-frame-size", &plat->maxmtu);
  174. of_property_read_u32(np, "snps,multicast-filter-bins",
  175. &plat->multicast_filter_bins);
  176. of_property_read_u32(np, "snps,perfect-filter-entries",
  177. &plat->unicast_filter_entries);
  178. plat->unicast_filter_entries = dwmac1000_validate_ucast_entries(
  179. plat->unicast_filter_entries);
  180. plat->multicast_filter_bins = dwmac1000_validate_mcast_bins(
  181. plat->multicast_filter_bins);
  182. plat->has_gmac = 1;
  183. plat->pmt = 1;
  184. }
  185. if (of_device_is_compatible(np, "snps,dwmac-3.610") ||
  186. of_device_is_compatible(np, "snps,dwmac-3.710")) {
  187. plat->enh_desc = 1;
  188. plat->bugged_jumbo = 1;
  189. plat->force_sf_dma_mode = 1;
  190. }
  191. if (of_find_property(np, "snps,pbl", NULL)) {
  192. dma_cfg = devm_kzalloc(&pdev->dev, sizeof(*dma_cfg),
  193. GFP_KERNEL);
  194. if (!dma_cfg)
  195. return -ENOMEM;
  196. plat->dma_cfg = dma_cfg;
  197. of_property_read_u32(np, "snps,pbl", &dma_cfg->pbl);
  198. dma_cfg->fixed_burst =
  199. of_property_read_bool(np, "snps,fixed-burst");
  200. dma_cfg->mixed_burst =
  201. of_property_read_bool(np, "snps,mixed-burst");
  202. }
  203. plat->force_thresh_dma_mode = of_property_read_bool(np, "snps,force_thresh_dma_mode");
  204. if (plat->force_thresh_dma_mode) {
  205. plat->force_sf_dma_mode = 0;
  206. pr_warn("force_sf_dma_mode is ignored if force_thresh_dma_mode is set.");
  207. }
  208. return 0;
  209. }
  210. #else
  211. static int stmmac_probe_config_dt(struct platform_device *pdev,
  212. struct plat_stmmacenet_data *plat,
  213. const char **mac)
  214. {
  215. return -ENOSYS;
  216. }
  217. #endif /* CONFIG_OF */
  218. /**
  219. * stmmac_pltfr_probe
  220. * @pdev: platform device pointer
  221. * Description: platform_device probe function. It allocates
  222. * the necessary resources and invokes the main to init
  223. * the net device, register the mdio bus etc.
  224. */
  225. static int stmmac_pltfr_probe(struct platform_device *pdev)
  226. {
  227. int ret = 0;
  228. struct resource *res;
  229. struct device *dev = &pdev->dev;
  230. void __iomem *addr = NULL;
  231. struct stmmac_priv *priv = NULL;
  232. struct plat_stmmacenet_data *plat_dat = NULL;
  233. const char *mac = NULL;
  234. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  235. addr = devm_ioremap_resource(dev, res);
  236. if (IS_ERR(addr))
  237. return PTR_ERR(addr);
  238. plat_dat = dev_get_platdata(&pdev->dev);
  239. if (pdev->dev.of_node) {
  240. if (!plat_dat)
  241. plat_dat = devm_kzalloc(&pdev->dev,
  242. sizeof(struct plat_stmmacenet_data),
  243. GFP_KERNEL);
  244. if (!plat_dat) {
  245. pr_err("%s: ERROR: no memory", __func__);
  246. return -ENOMEM;
  247. }
  248. ret = stmmac_probe_config_dt(pdev, plat_dat, &mac);
  249. if (ret) {
  250. pr_err("%s: main dt probe failed", __func__);
  251. return ret;
  252. }
  253. }
  254. /* Custom setup (if needed) */
  255. if (plat_dat->setup) {
  256. plat_dat->bsp_priv = plat_dat->setup(pdev);
  257. if (IS_ERR(plat_dat->bsp_priv))
  258. return PTR_ERR(plat_dat->bsp_priv);
  259. }
  260. /* Custom initialisation (if needed)*/
  261. if (plat_dat->init) {
  262. ret = plat_dat->init(pdev, plat_dat->bsp_priv);
  263. if (unlikely(ret))
  264. return ret;
  265. }
  266. priv = stmmac_dvr_probe(&(pdev->dev), plat_dat, addr);
  267. if (IS_ERR(priv)) {
  268. pr_err("%s: main driver probe failed", __func__);
  269. return PTR_ERR(priv);
  270. }
  271. /* Get MAC address if available (DT) */
  272. if (mac)
  273. memcpy(priv->dev->dev_addr, mac, ETH_ALEN);
  274. /* Get the MAC information */
  275. priv->dev->irq = platform_get_irq_byname(pdev, "macirq");
  276. if (priv->dev->irq < 0) {
  277. if (priv->dev->irq != -EPROBE_DEFER) {
  278. netdev_err(priv->dev,
  279. "MAC IRQ configuration information not found\n");
  280. }
  281. return priv->dev->irq;
  282. }
  283. /*
  284. * On some platforms e.g. SPEAr the wake up irq differs from the mac irq
  285. * The external wake up irq can be passed through the platform code
  286. * named as "eth_wake_irq"
  287. *
  288. * In case the wake up interrupt is not passed from the platform
  289. * so the driver will continue to use the mac irq (ndev->irq)
  290. */
  291. priv->wol_irq = platform_get_irq_byname(pdev, "eth_wake_irq");
  292. if (priv->wol_irq < 0) {
  293. if (priv->wol_irq == -EPROBE_DEFER)
  294. return -EPROBE_DEFER;
  295. priv->wol_irq = priv->dev->irq;
  296. }
  297. priv->lpi_irq = platform_get_irq_byname(pdev, "eth_lpi");
  298. if (priv->lpi_irq == -EPROBE_DEFER)
  299. return -EPROBE_DEFER;
  300. platform_set_drvdata(pdev, priv->dev);
  301. pr_debug("STMMAC platform driver registration completed");
  302. return 0;
  303. }
  304. /**
  305. * stmmac_pltfr_remove
  306. * @pdev: platform device pointer
  307. * Description: this function calls the main to free the net resources
  308. * and calls the platforms hook and release the resources (e.g. mem).
  309. */
  310. static int stmmac_pltfr_remove(struct platform_device *pdev)
  311. {
  312. struct net_device *ndev = platform_get_drvdata(pdev);
  313. struct stmmac_priv *priv = netdev_priv(ndev);
  314. int ret = stmmac_dvr_remove(ndev);
  315. if (priv->plat->exit)
  316. priv->plat->exit(pdev, priv->plat->bsp_priv);
  317. if (priv->plat->free)
  318. priv->plat->free(pdev, priv->plat->bsp_priv);
  319. return ret;
  320. }
  321. #ifdef CONFIG_PM
  322. static int stmmac_pltfr_suspend(struct device *dev)
  323. {
  324. int ret;
  325. struct net_device *ndev = dev_get_drvdata(dev);
  326. struct stmmac_priv *priv = netdev_priv(ndev);
  327. struct platform_device *pdev = to_platform_device(dev);
  328. ret = stmmac_suspend(ndev);
  329. if (priv->plat->exit)
  330. priv->plat->exit(pdev, priv->plat->bsp_priv);
  331. return ret;
  332. }
  333. static int stmmac_pltfr_resume(struct device *dev)
  334. {
  335. struct net_device *ndev = dev_get_drvdata(dev);
  336. struct stmmac_priv *priv = netdev_priv(ndev);
  337. struct platform_device *pdev = to_platform_device(dev);
  338. if (priv->plat->init)
  339. priv->plat->init(pdev, priv->plat->bsp_priv);
  340. return stmmac_resume(ndev);
  341. }
  342. #endif /* CONFIG_PM */
  343. static SIMPLE_DEV_PM_OPS(stmmac_pltfr_pm_ops,
  344. stmmac_pltfr_suspend, stmmac_pltfr_resume);
  345. struct platform_driver stmmac_pltfr_driver = {
  346. .probe = stmmac_pltfr_probe,
  347. .remove = stmmac_pltfr_remove,
  348. .driver = {
  349. .name = STMMAC_RESOURCE_NAME,
  350. .owner = THIS_MODULE,
  351. .pm = &stmmac_pltfr_pm_ops,
  352. .of_match_table = of_match_ptr(stmmac_dt_ids),
  353. },
  354. };
  355. MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet PLATFORM driver");
  356. MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
  357. MODULE_LICENSE("GPL");