stmmac_platform.c 14 KB

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