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/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:
  72. case 32:
  73. case 64:
  74. case 128:
  75. break;
  76. default:
  77. x = 1;
  78. pr_info("Unicast table entries set to unexpected value %d\n",
  79. ucast_entries);
  80. break;
  81. }
  82. return x;
  83. }
  84. /**
  85. * stmmac_probe_config_dt - parse device-tree driver parameters
  86. * @pdev: platform_device structure
  87. * @plat: driver data platform structure
  88. * @mac: MAC address to use
  89. * Description:
  90. * this function is to read the driver parameters from device-tree and
  91. * set some private fields that will be used by the main at runtime.
  92. */
  93. static int stmmac_probe_config_dt(struct platform_device *pdev,
  94. struct plat_stmmacenet_data *plat,
  95. const char **mac)
  96. {
  97. struct device_node *np = pdev->dev.of_node;
  98. struct stmmac_dma_cfg *dma_cfg;
  99. const struct of_device_id *device;
  100. struct device *dev = &pdev->dev;
  101. device = of_match_device(dev->driver->of_match_table, dev);
  102. if (device->data) {
  103. const struct stmmac_of_data *data = device->data;
  104. plat->has_gmac = data->has_gmac;
  105. plat->enh_desc = data->enh_desc;
  106. plat->tx_coe = data->tx_coe;
  107. plat->rx_coe = data->rx_coe;
  108. plat->bugged_jumbo = data->bugged_jumbo;
  109. plat->pmt = data->pmt;
  110. plat->riwt_off = data->riwt_off;
  111. plat->fix_mac_speed = data->fix_mac_speed;
  112. plat->bus_setup = data->bus_setup;
  113. plat->setup = data->setup;
  114. plat->free = data->free;
  115. plat->init = data->init;
  116. plat->exit = data->exit;
  117. }
  118. *mac = of_get_mac_address(np);
  119. plat->interface = of_get_phy_mode(np);
  120. /* Get max speed of operation from device tree */
  121. if (of_property_read_u32(np, "max-speed", &plat->max_speed))
  122. plat->max_speed = -1;
  123. plat->bus_id = of_alias_get_id(np, "ethernet");
  124. if (plat->bus_id < 0)
  125. plat->bus_id = 0;
  126. /* Default to phy auto-detection */
  127. plat->phy_addr = -1;
  128. /* If we find a phy-handle property, use it as the PHY */
  129. plat->phy_node = of_parse_phandle(np, "phy-handle", 0);
  130. /* If phy-handle is not specified, check if we have a fixed-phy */
  131. if (!plat->phy_node && of_phy_is_fixed_link(np)) {
  132. if ((of_phy_register_fixed_link(np) < 0))
  133. return -ENODEV;
  134. plat->phy_node = of_node_get(np);
  135. }
  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_node || 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. of_property_read_u32(np, "tx-fifo-depth", &plat->tx_fifo_size);
  149. of_property_read_u32(np, "rx-fifo-depth", &plat->rx_fifo_size);
  150. plat->force_sf_dma_mode =
  151. of_property_read_bool(np, "snps,force_sf_dma_mode");
  152. /* Set the maxmtu to a default of JUMBO_LEN in case the
  153. * parameter is not present in the device tree.
  154. */
  155. plat->maxmtu = JUMBO_LEN;
  156. /*
  157. * Currently only the properties needed on SPEAr600
  158. * are provided. All other properties should be added
  159. * once needed on other platforms.
  160. */
  161. if (of_device_is_compatible(np, "st,spear600-gmac") ||
  162. of_device_is_compatible(np, "snps,dwmac-3.70a") ||
  163. of_device_is_compatible(np, "snps,dwmac")) {
  164. /* Note that the max-frame-size parameter as defined in the
  165. * ePAPR v1.1 spec is defined as max-frame-size, it's
  166. * actually used as the IEEE definition of MAC Client
  167. * data, or MTU. The ePAPR specification is confusing as
  168. * the definition is max-frame-size, but usage examples
  169. * are clearly MTUs
  170. */
  171. of_property_read_u32(np, "max-frame-size", &plat->maxmtu);
  172. of_property_read_u32(np, "snps,multicast-filter-bins",
  173. &plat->multicast_filter_bins);
  174. of_property_read_u32(np, "snps,perfect-filter-entries",
  175. &plat->unicast_filter_entries);
  176. plat->unicast_filter_entries = dwmac1000_validate_ucast_entries(
  177. plat->unicast_filter_entries);
  178. plat->multicast_filter_bins = dwmac1000_validate_mcast_bins(
  179. plat->multicast_filter_bins);
  180. plat->has_gmac = 1;
  181. plat->pmt = 1;
  182. }
  183. if (of_device_is_compatible(np, "snps,dwmac-3.610") ||
  184. of_device_is_compatible(np, "snps,dwmac-3.710")) {
  185. plat->enh_desc = 1;
  186. plat->bugged_jumbo = 1;
  187. plat->force_sf_dma_mode = 1;
  188. }
  189. if (of_find_property(np, "snps,pbl", NULL)) {
  190. dma_cfg = devm_kzalloc(&pdev->dev, sizeof(*dma_cfg),
  191. GFP_KERNEL);
  192. if (!dma_cfg) {
  193. of_node_put(np);
  194. return -ENOMEM;
  195. }
  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. of_property_read_u32(np, "snps,burst_len", &dma_cfg->burst_len);
  203. if (dma_cfg->burst_len < 0 || dma_cfg->burst_len > 256)
  204. dma_cfg->burst_len = 0;
  205. }
  206. plat->force_thresh_dma_mode = of_property_read_bool(np, "snps,force_thresh_dma_mode");
  207. if (plat->force_thresh_dma_mode) {
  208. plat->force_sf_dma_mode = 0;
  209. pr_warn("force_sf_dma_mode is ignored if force_thresh_dma_mode is set.");
  210. }
  211. return 0;
  212. }
  213. #else
  214. static int stmmac_probe_config_dt(struct platform_device *pdev,
  215. struct plat_stmmacenet_data *plat,
  216. const char **mac)
  217. {
  218. return -ENOSYS;
  219. }
  220. #endif /* CONFIG_OF */
  221. /**
  222. * stmmac_pltfr_probe - platform driver probe.
  223. * @pdev: platform device pointer
  224. * Description: platform_device probe function. It is to allocate
  225. * the necessary platform resources, invoke custom helper (if required) and
  226. * invoke the main probe function.
  227. */
  228. int stmmac_pltfr_probe(struct platform_device *pdev)
  229. {
  230. struct stmmac_resources stmmac_res;
  231. int ret = 0;
  232. struct resource *res;
  233. struct device *dev = &pdev->dev;
  234. struct plat_stmmacenet_data *plat_dat = NULL;
  235. memset(&stmmac_res, 0, sizeof(stmmac_res));
  236. /* Get IRQ information early to have an ability to ask for deferred
  237. * probe if needed before we went too far with resource allocation.
  238. */
  239. stmmac_res.irq = platform_get_irq_byname(pdev, "macirq");
  240. if (stmmac_res.irq < 0) {
  241. if (stmmac_res.irq != -EPROBE_DEFER) {
  242. dev_err(dev,
  243. "MAC IRQ configuration information not found\n");
  244. }
  245. return stmmac_res.irq;
  246. }
  247. /* On some platforms e.g. SPEAr the wake up irq differs from the mac irq
  248. * The external wake up irq can be passed through the platform code
  249. * named as "eth_wake_irq"
  250. *
  251. * In case the wake up interrupt is not passed from the platform
  252. * so the driver will continue to use the mac irq (ndev->irq)
  253. */
  254. stmmac_res.wol_irq = platform_get_irq_byname(pdev, "eth_wake_irq");
  255. if (stmmac_res.wol_irq < 0) {
  256. if (stmmac_res.wol_irq == -EPROBE_DEFER)
  257. return -EPROBE_DEFER;
  258. stmmac_res.wol_irq = stmmac_res.irq;
  259. }
  260. stmmac_res.lpi_irq = platform_get_irq_byname(pdev, "eth_lpi");
  261. if (stmmac_res.lpi_irq == -EPROBE_DEFER)
  262. return -EPROBE_DEFER;
  263. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  264. stmmac_res.addr = devm_ioremap_resource(dev, res);
  265. if (IS_ERR(stmmac_res.addr))
  266. return PTR_ERR(stmmac_res.addr);
  267. plat_dat = dev_get_platdata(&pdev->dev);
  268. if (!plat_dat)
  269. plat_dat = devm_kzalloc(&pdev->dev,
  270. sizeof(struct plat_stmmacenet_data),
  271. GFP_KERNEL);
  272. if (!plat_dat) {
  273. pr_err("%s: ERROR: no memory", __func__);
  274. return -ENOMEM;
  275. }
  276. /* Set default value for multicast hash bins */
  277. plat_dat->multicast_filter_bins = HASH_TABLE_SIZE;
  278. /* Set default value for unicast filter entries */
  279. plat_dat->unicast_filter_entries = 1;
  280. if (pdev->dev.of_node) {
  281. ret = stmmac_probe_config_dt(pdev, plat_dat, &stmmac_res.mac);
  282. if (ret) {
  283. pr_err("%s: main dt probe failed", __func__);
  284. return ret;
  285. }
  286. }
  287. /* Custom setup (if needed) */
  288. if (plat_dat->setup) {
  289. plat_dat->bsp_priv = plat_dat->setup(pdev);
  290. if (IS_ERR(plat_dat->bsp_priv))
  291. return PTR_ERR(plat_dat->bsp_priv);
  292. }
  293. /* Custom initialisation (if needed)*/
  294. if (plat_dat->init) {
  295. ret = plat_dat->init(pdev, plat_dat->bsp_priv);
  296. if (unlikely(ret))
  297. return ret;
  298. }
  299. return stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res);
  300. }
  301. EXPORT_SYMBOL_GPL(stmmac_pltfr_probe);
  302. /**
  303. * stmmac_pltfr_remove
  304. * @pdev: platform device pointer
  305. * Description: this function calls the main to free the net resources
  306. * and calls the platforms hook and release the resources (e.g. mem).
  307. */
  308. int stmmac_pltfr_remove(struct platform_device *pdev)
  309. {
  310. struct net_device *ndev = platform_get_drvdata(pdev);
  311. struct stmmac_priv *priv = netdev_priv(ndev);
  312. int ret = stmmac_dvr_remove(ndev);
  313. if (priv->plat->exit)
  314. priv->plat->exit(pdev, priv->plat->bsp_priv);
  315. if (priv->plat->free)
  316. priv->plat->free(pdev, priv->plat->bsp_priv);
  317. return ret;
  318. }
  319. EXPORT_SYMBOL_GPL(stmmac_pltfr_remove);
  320. #ifdef CONFIG_PM_SLEEP
  321. /**
  322. * stmmac_pltfr_suspend
  323. * @dev: device pointer
  324. * Description: this function is invoked when suspend the driver and it direcly
  325. * call the main suspend function and then, if required, on some platform, it
  326. * can call an exit helper.
  327. */
  328. static int stmmac_pltfr_suspend(struct device *dev)
  329. {
  330. int ret;
  331. struct net_device *ndev = dev_get_drvdata(dev);
  332. struct stmmac_priv *priv = netdev_priv(ndev);
  333. struct platform_device *pdev = to_platform_device(dev);
  334. ret = stmmac_suspend(ndev);
  335. if (priv->plat->exit)
  336. priv->plat->exit(pdev, priv->plat->bsp_priv);
  337. return ret;
  338. }
  339. /**
  340. * stmmac_pltfr_resume
  341. * @dev: device pointer
  342. * Description: this function is invoked when resume the driver before calling
  343. * the main resume function, on some platforms, it can call own init helper
  344. * if required.
  345. */
  346. static int stmmac_pltfr_resume(struct device *dev)
  347. {
  348. struct net_device *ndev = dev_get_drvdata(dev);
  349. struct stmmac_priv *priv = netdev_priv(ndev);
  350. struct platform_device *pdev = to_platform_device(dev);
  351. if (priv->plat->init)
  352. priv->plat->init(pdev, priv->plat->bsp_priv);
  353. return stmmac_resume(ndev);
  354. }
  355. #endif /* CONFIG_PM_SLEEP */
  356. SIMPLE_DEV_PM_OPS(stmmac_pltfr_pm_ops, stmmac_pltfr_suspend,
  357. stmmac_pltfr_resume);
  358. EXPORT_SYMBOL_GPL(stmmac_pltfr_pm_ops);