stmmac_platform.c 12 KB

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