stmmac_platform.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 <linux/of_mdio.h>
  25. #include "stmmac.h"
  26. #include "stmmac_platform.h"
  27. #ifdef CONFIG_OF
  28. /**
  29. * dwmac1000_validate_mcast_bins - validates the number of Multicast filter bins
  30. * @mcast_bins: Multicast filtering bins
  31. * Description:
  32. * this function validates the number of Multicast filtering bins specified
  33. * by the configuration through the device tree. The Synopsys GMAC supports
  34. * 64 bins, 128 bins, or 256 bins. "bins" refer to the division of CRC
  35. * number space. 64 bins correspond to 6 bits of the CRC, 128 corresponds
  36. * to 7 bits, and 256 refers to 8 bits of the CRC. Any other setting is
  37. * invalid and will cause the filtering algorithm to use Multicast
  38. * promiscuous mode.
  39. */
  40. static int dwmac1000_validate_mcast_bins(int mcast_bins)
  41. {
  42. int x = mcast_bins;
  43. switch (x) {
  44. case HASH_TABLE_SIZE:
  45. case 128:
  46. case 256:
  47. break;
  48. default:
  49. x = 0;
  50. pr_info("Hash table entries set to unexpected value %d",
  51. mcast_bins);
  52. break;
  53. }
  54. return x;
  55. }
  56. /**
  57. * dwmac1000_validate_ucast_entries - validate the Unicast address entries
  58. * @ucast_entries: number of Unicast address entries
  59. * Description:
  60. * This function validates the number of Unicast address entries supported
  61. * by a particular Synopsys 10/100/1000 controller. The Synopsys controller
  62. * supports 1..32, 64, or 128 Unicast filter entries for it's Unicast filter
  63. * logic. This function validates a valid, supported configuration is
  64. * selected, and defaults to 1 Unicast address if an unsupported
  65. * configuration is selected.
  66. */
  67. static int dwmac1000_validate_ucast_entries(int ucast_entries)
  68. {
  69. int x = ucast_entries;
  70. switch (x) {
  71. case 1 ... 32:
  72. case 64:
  73. case 128:
  74. break;
  75. default:
  76. x = 1;
  77. pr_info("Unicast table entries set to unexpected value %d\n",
  78. ucast_entries);
  79. break;
  80. }
  81. return x;
  82. }
  83. /**
  84. * stmmac_probe_config_dt - parse device-tree driver parameters
  85. * @pdev: platform_device structure
  86. * @plat: driver data platform structure
  87. * @mac: MAC address to use
  88. * Description:
  89. * this function is to read the driver parameters from device-tree and
  90. * set some private fields that will be used by the main at runtime.
  91. */
  92. struct plat_stmmacenet_data *
  93. stmmac_probe_config_dt(struct platform_device *pdev, const char **mac)
  94. {
  95. struct device_node *np = pdev->dev.of_node;
  96. struct plat_stmmacenet_data *plat;
  97. struct stmmac_dma_cfg *dma_cfg;
  98. plat = devm_kzalloc(&pdev->dev, sizeof(*plat), GFP_KERNEL);
  99. if (!plat)
  100. return ERR_PTR(-ENOMEM);
  101. *mac = of_get_mac_address(np);
  102. plat->interface = of_get_phy_mode(np);
  103. /* Get max speed of operation from device tree */
  104. if (of_property_read_u32(np, "max-speed", &plat->max_speed))
  105. plat->max_speed = -1;
  106. plat->bus_id = of_alias_get_id(np, "ethernet");
  107. if (plat->bus_id < 0)
  108. plat->bus_id = 0;
  109. /* Default to phy auto-detection */
  110. plat->phy_addr = -1;
  111. /* If we find a phy-handle property, use it as the PHY */
  112. plat->phy_node = of_parse_phandle(np, "phy-handle", 0);
  113. /* If phy-handle is not specified, check if we have a fixed-phy */
  114. if (!plat->phy_node && of_phy_is_fixed_link(np)) {
  115. if ((of_phy_register_fixed_link(np) < 0))
  116. return ERR_PTR(-ENODEV);
  117. plat->phy_node = of_node_get(np);
  118. }
  119. /* "snps,phy-addr" is not a standard property. Mark it as deprecated
  120. * and warn of its use. Remove this when phy node support is added.
  121. */
  122. if (of_property_read_u32(np, "snps,phy-addr", &plat->phy_addr) == 0)
  123. dev_warn(&pdev->dev, "snps,phy-addr property is deprecated\n");
  124. if (plat->phy_node || plat->phy_bus_name)
  125. plat->mdio_bus_data = NULL;
  126. else
  127. plat->mdio_bus_data =
  128. devm_kzalloc(&pdev->dev,
  129. sizeof(struct stmmac_mdio_bus_data),
  130. GFP_KERNEL);
  131. of_property_read_u32(np, "tx-fifo-depth", &plat->tx_fifo_size);
  132. of_property_read_u32(np, "rx-fifo-depth", &plat->rx_fifo_size);
  133. plat->force_sf_dma_mode =
  134. of_property_read_bool(np, "snps,force_sf_dma_mode");
  135. /* Set the maxmtu to a default of JUMBO_LEN in case the
  136. * parameter is not present in the device tree.
  137. */
  138. plat->maxmtu = JUMBO_LEN;
  139. /* Set default value for multicast hash bins */
  140. plat->multicast_filter_bins = HASH_TABLE_SIZE;
  141. /* Set default value for unicast filter entries */
  142. plat->unicast_filter_entries = 1;
  143. /*
  144. * Currently only the properties needed on SPEAr600
  145. * are provided. All other properties should be added
  146. * once needed on other platforms.
  147. */
  148. if (of_device_is_compatible(np, "st,spear600-gmac") ||
  149. of_device_is_compatible(np, "snps,dwmac-3.70a") ||
  150. of_device_is_compatible(np, "snps,dwmac")) {
  151. /* Note that the max-frame-size parameter as defined in the
  152. * ePAPR v1.1 spec is defined as max-frame-size, it's
  153. * actually used as the IEEE definition of MAC Client
  154. * data, or MTU. The ePAPR specification is confusing as
  155. * the definition is max-frame-size, but usage examples
  156. * are clearly MTUs
  157. */
  158. of_property_read_u32(np, "max-frame-size", &plat->maxmtu);
  159. of_property_read_u32(np, "snps,multicast-filter-bins",
  160. &plat->multicast_filter_bins);
  161. of_property_read_u32(np, "snps,perfect-filter-entries",
  162. &plat->unicast_filter_entries);
  163. plat->unicast_filter_entries = dwmac1000_validate_ucast_entries(
  164. plat->unicast_filter_entries);
  165. plat->multicast_filter_bins = dwmac1000_validate_mcast_bins(
  166. plat->multicast_filter_bins);
  167. plat->has_gmac = 1;
  168. plat->pmt = 1;
  169. }
  170. if (of_device_is_compatible(np, "snps,dwmac-3.610") ||
  171. of_device_is_compatible(np, "snps,dwmac-3.710")) {
  172. plat->enh_desc = 1;
  173. plat->bugged_jumbo = 1;
  174. plat->force_sf_dma_mode = 1;
  175. }
  176. if (of_find_property(np, "snps,pbl", NULL)) {
  177. dma_cfg = devm_kzalloc(&pdev->dev, sizeof(*dma_cfg),
  178. GFP_KERNEL);
  179. if (!dma_cfg) {
  180. of_node_put(np);
  181. return ERR_PTR(-ENOMEM);
  182. }
  183. plat->dma_cfg = dma_cfg;
  184. of_property_read_u32(np, "snps,pbl", &dma_cfg->pbl);
  185. dma_cfg->fixed_burst =
  186. of_property_read_bool(np, "snps,fixed-burst");
  187. dma_cfg->mixed_burst =
  188. of_property_read_bool(np, "snps,mixed-burst");
  189. of_property_read_u32(np, "snps,burst_len", &dma_cfg->burst_len);
  190. if (dma_cfg->burst_len < 0 || dma_cfg->burst_len > 256)
  191. dma_cfg->burst_len = 0;
  192. }
  193. plat->force_thresh_dma_mode = of_property_read_bool(np, "snps,force_thresh_dma_mode");
  194. if (plat->force_thresh_dma_mode) {
  195. plat->force_sf_dma_mode = 0;
  196. pr_warn("force_sf_dma_mode is ignored if force_thresh_dma_mode is set.");
  197. }
  198. return plat;
  199. }
  200. #else
  201. struct plat_stmmacenet_data *
  202. stmmac_probe_config_dt(struct platform_device *pdev, const char **mac)
  203. {
  204. return ERR_PTR(-ENOSYS);
  205. }
  206. #endif /* CONFIG_OF */
  207. EXPORT_SYMBOL_GPL(stmmac_probe_config_dt);
  208. int stmmac_get_platform_resources(struct platform_device *pdev,
  209. struct stmmac_resources *stmmac_res)
  210. {
  211. struct resource *res;
  212. memset(stmmac_res, 0, sizeof(*stmmac_res));
  213. /* Get IRQ information early to have an ability to ask for deferred
  214. * probe if needed before we went too far with resource allocation.
  215. */
  216. stmmac_res->irq = platform_get_irq_byname(pdev, "macirq");
  217. if (stmmac_res->irq < 0) {
  218. if (stmmac_res->irq != -EPROBE_DEFER) {
  219. dev_err(&pdev->dev,
  220. "MAC IRQ configuration information not found\n");
  221. }
  222. return stmmac_res->irq;
  223. }
  224. /* On some platforms e.g. SPEAr the wake up irq differs from the mac irq
  225. * The external wake up irq can be passed through the platform code
  226. * named as "eth_wake_irq"
  227. *
  228. * In case the wake up interrupt is not passed from the platform
  229. * so the driver will continue to use the mac irq (ndev->irq)
  230. */
  231. stmmac_res->wol_irq = platform_get_irq_byname(pdev, "eth_wake_irq");
  232. if (stmmac_res->wol_irq < 0) {
  233. if (stmmac_res->wol_irq == -EPROBE_DEFER)
  234. return -EPROBE_DEFER;
  235. stmmac_res->wol_irq = stmmac_res->irq;
  236. }
  237. stmmac_res->lpi_irq = platform_get_irq_byname(pdev, "eth_lpi");
  238. if (stmmac_res->lpi_irq == -EPROBE_DEFER)
  239. return -EPROBE_DEFER;
  240. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  241. stmmac_res->addr = devm_ioremap_resource(&pdev->dev, res);
  242. return PTR_ERR_OR_ZERO(stmmac_res->addr);
  243. }
  244. EXPORT_SYMBOL_GPL(stmmac_get_platform_resources);
  245. /**
  246. * stmmac_pltfr_remove
  247. * @pdev: platform device pointer
  248. * Description: this function calls the main to free the net resources
  249. * and calls the platforms hook and release the resources (e.g. mem).
  250. */
  251. int stmmac_pltfr_remove(struct platform_device *pdev)
  252. {
  253. struct net_device *ndev = platform_get_drvdata(pdev);
  254. struct stmmac_priv *priv = netdev_priv(ndev);
  255. int ret = stmmac_dvr_remove(ndev);
  256. if (priv->plat->exit)
  257. priv->plat->exit(pdev, priv->plat->bsp_priv);
  258. return ret;
  259. }
  260. EXPORT_SYMBOL_GPL(stmmac_pltfr_remove);
  261. #ifdef CONFIG_PM_SLEEP
  262. /**
  263. * stmmac_pltfr_suspend
  264. * @dev: device pointer
  265. * Description: this function is invoked when suspend the driver and it direcly
  266. * call the main suspend function and then, if required, on some platform, it
  267. * can call an exit helper.
  268. */
  269. static int stmmac_pltfr_suspend(struct device *dev)
  270. {
  271. int ret;
  272. struct net_device *ndev = dev_get_drvdata(dev);
  273. struct stmmac_priv *priv = netdev_priv(ndev);
  274. struct platform_device *pdev = to_platform_device(dev);
  275. ret = stmmac_suspend(ndev);
  276. if (priv->plat->exit)
  277. priv->plat->exit(pdev, priv->plat->bsp_priv);
  278. return ret;
  279. }
  280. /**
  281. * stmmac_pltfr_resume
  282. * @dev: device pointer
  283. * Description: this function is invoked when resume the driver before calling
  284. * the main resume function, on some platforms, it can call own init helper
  285. * if required.
  286. */
  287. static int stmmac_pltfr_resume(struct device *dev)
  288. {
  289. struct net_device *ndev = dev_get_drvdata(dev);
  290. struct stmmac_priv *priv = netdev_priv(ndev);
  291. struct platform_device *pdev = to_platform_device(dev);
  292. if (priv->plat->init)
  293. priv->plat->init(pdev, priv->plat->bsp_priv);
  294. return stmmac_resume(ndev);
  295. }
  296. #endif /* CONFIG_PM_SLEEP */
  297. SIMPLE_DEV_PM_OPS(stmmac_pltfr_pm_ops, stmmac_pltfr_suspend,
  298. stmmac_pltfr_resume);
  299. EXPORT_SYMBOL_GPL(stmmac_pltfr_pm_ops);
  300. MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet platform support");
  301. MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
  302. MODULE_LICENSE("GPL");