stmmac_platform.c 13 KB

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