stmmac_platform.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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_axi_setup - parse DT parameters for programming the AXI register
  86. * @pdev: platform device
  87. * @priv: driver private struct.
  88. * Description:
  89. * if required, from device-tree the AXI internal register can be tuned
  90. * by using platform parameters.
  91. */
  92. static struct stmmac_axi *stmmac_axi_setup(struct platform_device *pdev)
  93. {
  94. struct device_node *np;
  95. struct stmmac_axi *axi;
  96. np = of_parse_phandle(pdev->dev.of_node, "snps,axi-config", 0);
  97. if (!np)
  98. return NULL;
  99. axi = kzalloc(sizeof(*axi), GFP_KERNEL);
  100. if (!axi) {
  101. of_node_put(np);
  102. return ERR_PTR(-ENOMEM);
  103. }
  104. axi->axi_lpi_en = of_property_read_bool(np, "snps,lpi_en");
  105. axi->axi_xit_frm = of_property_read_bool(np, "snps,xit_frm");
  106. axi->axi_kbbe = of_property_read_bool(np, "snps,axi_kbbe");
  107. axi->axi_axi_all = of_property_read_bool(np, "snps,axi_all");
  108. axi->axi_fb = of_property_read_bool(np, "snps,axi_fb");
  109. axi->axi_mb = of_property_read_bool(np, "snps,axi_mb");
  110. axi->axi_rb = of_property_read_bool(np, "snps,axi_rb");
  111. if (of_property_read_u32(np, "snps,wr_osr_lmt", &axi->axi_wr_osr_lmt))
  112. axi->axi_wr_osr_lmt = 1;
  113. if (of_property_read_u32(np, "snps,rd_osr_lmt", &axi->axi_rd_osr_lmt))
  114. axi->axi_rd_osr_lmt = 1;
  115. of_property_read_u32_array(np, "snps,blen", axi->axi_blen, AXI_BLEN);
  116. of_node_put(np);
  117. return axi;
  118. }
  119. /**
  120. * stmmac_dt_phy - parse device-tree driver parameters to allocate PHY resources
  121. * @plat: driver data platform structure
  122. * @np: device tree node
  123. * @dev: device pointer
  124. * Description:
  125. * The mdio bus will be allocated in case of a phy transceiver is on board;
  126. * it will be NULL if the fixed-link is configured.
  127. * If there is the "snps,dwmac-mdio" sub-node the mdio will be allocated
  128. * in any case (for DSA, mdio must be registered even if fixed-link).
  129. * The table below sums the supported configurations:
  130. * -------------------------------
  131. * snps,phy-addr | Y
  132. * -------------------------------
  133. * phy-handle | Y
  134. * -------------------------------
  135. * fixed-link | N
  136. * -------------------------------
  137. * snps,dwmac-mdio |
  138. * even if | Y
  139. * fixed-link |
  140. * -------------------------------
  141. *
  142. * It returns 0 in case of success otherwise -ENODEV.
  143. */
  144. static int stmmac_dt_phy(struct plat_stmmacenet_data *plat,
  145. struct device_node *np, struct device *dev)
  146. {
  147. bool mdio = true;
  148. /* If phy-handle property is passed from DT, use it as the PHY */
  149. plat->phy_node = of_parse_phandle(np, "phy-handle", 0);
  150. if (plat->phy_node)
  151. dev_dbg(dev, "Found phy-handle subnode\n");
  152. /* If phy-handle is not specified, check if we have a fixed-phy */
  153. if (!plat->phy_node && of_phy_is_fixed_link(np)) {
  154. if ((of_phy_register_fixed_link(np) < 0))
  155. return -ENODEV;
  156. dev_dbg(dev, "Found fixed-link subnode\n");
  157. plat->phy_node = of_node_get(np);
  158. mdio = false;
  159. }
  160. /* If snps,dwmac-mdio is passed from DT, always register the MDIO */
  161. for_each_child_of_node(np, plat->mdio_node) {
  162. if (of_device_is_compatible(plat->mdio_node, "snps,dwmac-mdio"))
  163. break;
  164. }
  165. if (plat->mdio_node) {
  166. dev_dbg(dev, "Found MDIO subnode\n");
  167. mdio = true;
  168. }
  169. if (mdio)
  170. plat->mdio_bus_data =
  171. devm_kzalloc(dev, sizeof(struct stmmac_mdio_bus_data),
  172. GFP_KERNEL);
  173. return 0;
  174. }
  175. /**
  176. * stmmac_probe_config_dt - parse device-tree driver parameters
  177. * @pdev: platform_device structure
  178. * @mac: MAC address to use
  179. * Description:
  180. * this function is to read the driver parameters from device-tree and
  181. * set some private fields that will be used by the main at runtime.
  182. */
  183. struct plat_stmmacenet_data *
  184. stmmac_probe_config_dt(struct platform_device *pdev, const char **mac)
  185. {
  186. struct device_node *np = pdev->dev.of_node;
  187. struct plat_stmmacenet_data *plat;
  188. struct stmmac_dma_cfg *dma_cfg;
  189. plat = devm_kzalloc(&pdev->dev, sizeof(*plat), GFP_KERNEL);
  190. if (!plat)
  191. return ERR_PTR(-ENOMEM);
  192. *mac = of_get_mac_address(np);
  193. plat->interface = of_get_phy_mode(np);
  194. /* Get max speed of operation from device tree */
  195. if (of_property_read_u32(np, "max-speed", &plat->max_speed))
  196. plat->max_speed = -1;
  197. plat->bus_id = of_alias_get_id(np, "ethernet");
  198. if (plat->bus_id < 0)
  199. plat->bus_id = 0;
  200. /* Default to phy auto-detection */
  201. plat->phy_addr = -1;
  202. /* "snps,phy-addr" is not a standard property. Mark it as deprecated
  203. * and warn of its use. Remove this when phy node support is added.
  204. */
  205. if (of_property_read_u32(np, "snps,phy-addr", &plat->phy_addr) == 0)
  206. dev_warn(&pdev->dev, "snps,phy-addr property is deprecated\n");
  207. /* To Configure PHY by using all device-tree supported properties */
  208. if (stmmac_dt_phy(plat, np, &pdev->dev))
  209. return ERR_PTR(-ENODEV);
  210. of_property_read_u32(np, "tx-fifo-depth", &plat->tx_fifo_size);
  211. of_property_read_u32(np, "rx-fifo-depth", &plat->rx_fifo_size);
  212. plat->force_sf_dma_mode =
  213. of_property_read_bool(np, "snps,force_sf_dma_mode");
  214. /* Set the maxmtu to a default of JUMBO_LEN in case the
  215. * parameter is not present in the device tree.
  216. */
  217. plat->maxmtu = JUMBO_LEN;
  218. /* Set default value for multicast hash bins */
  219. plat->multicast_filter_bins = HASH_TABLE_SIZE;
  220. /* Set default value for unicast filter entries */
  221. plat->unicast_filter_entries = 1;
  222. /*
  223. * Currently only the properties needed on SPEAr600
  224. * are provided. All other properties should be added
  225. * once needed on other platforms.
  226. */
  227. if (of_device_is_compatible(np, "st,spear600-gmac") ||
  228. of_device_is_compatible(np, "snps,dwmac-3.50a") ||
  229. of_device_is_compatible(np, "snps,dwmac-3.70a") ||
  230. of_device_is_compatible(np, "snps,dwmac")) {
  231. /* Note that the max-frame-size parameter as defined in the
  232. * ePAPR v1.1 spec is defined as max-frame-size, it's
  233. * actually used as the IEEE definition of MAC Client
  234. * data, or MTU. The ePAPR specification is confusing as
  235. * the definition is max-frame-size, but usage examples
  236. * are clearly MTUs
  237. */
  238. of_property_read_u32(np, "max-frame-size", &plat->maxmtu);
  239. of_property_read_u32(np, "snps,multicast-filter-bins",
  240. &plat->multicast_filter_bins);
  241. of_property_read_u32(np, "snps,perfect-filter-entries",
  242. &plat->unicast_filter_entries);
  243. plat->unicast_filter_entries = dwmac1000_validate_ucast_entries(
  244. plat->unicast_filter_entries);
  245. plat->multicast_filter_bins = dwmac1000_validate_mcast_bins(
  246. plat->multicast_filter_bins);
  247. plat->has_gmac = 1;
  248. plat->pmt = 1;
  249. }
  250. if (of_device_is_compatible(np, "snps,dwmac-4.00") ||
  251. of_device_is_compatible(np, "snps,dwmac-4.10a")) {
  252. plat->has_gmac4 = 1;
  253. plat->pmt = 1;
  254. plat->tso_en = of_property_read_bool(np, "snps,tso");
  255. }
  256. if (of_device_is_compatible(np, "snps,dwmac-3.610") ||
  257. of_device_is_compatible(np, "snps,dwmac-3.710")) {
  258. plat->enh_desc = 1;
  259. plat->bugged_jumbo = 1;
  260. plat->force_sf_dma_mode = 1;
  261. }
  262. if (of_find_property(np, "snps,pbl", NULL)) {
  263. dma_cfg = devm_kzalloc(&pdev->dev, sizeof(*dma_cfg),
  264. GFP_KERNEL);
  265. if (!dma_cfg) {
  266. stmmac_remove_config_dt(pdev, plat);
  267. return ERR_PTR(-ENOMEM);
  268. }
  269. plat->dma_cfg = dma_cfg;
  270. of_property_read_u32(np, "snps,pbl", &dma_cfg->pbl);
  271. dma_cfg->aal = of_property_read_bool(np, "snps,aal");
  272. dma_cfg->fixed_burst =
  273. of_property_read_bool(np, "snps,fixed-burst");
  274. dma_cfg->mixed_burst =
  275. of_property_read_bool(np, "snps,mixed-burst");
  276. }
  277. plat->force_thresh_dma_mode = of_property_read_bool(np, "snps,force_thresh_dma_mode");
  278. if (plat->force_thresh_dma_mode) {
  279. plat->force_sf_dma_mode = 0;
  280. pr_warn("force_sf_dma_mode is ignored if force_thresh_dma_mode is set.");
  281. }
  282. of_property_read_u32(np, "snps,ps-speed", &plat->mac_port_sel_speed);
  283. plat->axi = stmmac_axi_setup(pdev);
  284. return plat;
  285. }
  286. /**
  287. * stmmac_remove_config_dt - undo the effects of stmmac_probe_config_dt()
  288. * @pdev: platform_device structure
  289. * @plat: driver data platform structure
  290. *
  291. * Release resources claimed by stmmac_probe_config_dt().
  292. */
  293. void stmmac_remove_config_dt(struct platform_device *pdev,
  294. struct plat_stmmacenet_data *plat)
  295. {
  296. struct device_node *np = pdev->dev.of_node;
  297. if (of_phy_is_fixed_link(np))
  298. of_phy_deregister_fixed_link(np);
  299. of_node_put(plat->phy_node);
  300. of_node_put(plat->mdio_node);
  301. }
  302. #else
  303. struct plat_stmmacenet_data *
  304. stmmac_probe_config_dt(struct platform_device *pdev, const char **mac)
  305. {
  306. return ERR_PTR(-ENOSYS);
  307. }
  308. void stmmac_remove_config_dt(struct platform_device *pdev,
  309. struct plat_stmmacenet_data *plat)
  310. {
  311. }
  312. #endif /* CONFIG_OF */
  313. EXPORT_SYMBOL_GPL(stmmac_probe_config_dt);
  314. EXPORT_SYMBOL_GPL(stmmac_remove_config_dt);
  315. int stmmac_get_platform_resources(struct platform_device *pdev,
  316. struct stmmac_resources *stmmac_res)
  317. {
  318. struct resource *res;
  319. memset(stmmac_res, 0, sizeof(*stmmac_res));
  320. /* Get IRQ information early to have an ability to ask for deferred
  321. * probe if needed before we went too far with resource allocation.
  322. */
  323. stmmac_res->irq = platform_get_irq_byname(pdev, "macirq");
  324. if (stmmac_res->irq < 0) {
  325. if (stmmac_res->irq != -EPROBE_DEFER) {
  326. dev_err(&pdev->dev,
  327. "MAC IRQ configuration information not found\n");
  328. }
  329. return stmmac_res->irq;
  330. }
  331. /* On some platforms e.g. SPEAr the wake up irq differs from the mac irq
  332. * The external wake up irq can be passed through the platform code
  333. * named as "eth_wake_irq"
  334. *
  335. * In case the wake up interrupt is not passed from the platform
  336. * so the driver will continue to use the mac irq (ndev->irq)
  337. */
  338. stmmac_res->wol_irq = platform_get_irq_byname(pdev, "eth_wake_irq");
  339. if (stmmac_res->wol_irq < 0) {
  340. if (stmmac_res->wol_irq == -EPROBE_DEFER)
  341. return -EPROBE_DEFER;
  342. stmmac_res->wol_irq = stmmac_res->irq;
  343. }
  344. stmmac_res->lpi_irq = platform_get_irq_byname(pdev, "eth_lpi");
  345. if (stmmac_res->lpi_irq == -EPROBE_DEFER)
  346. return -EPROBE_DEFER;
  347. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  348. stmmac_res->addr = devm_ioremap_resource(&pdev->dev, res);
  349. return PTR_ERR_OR_ZERO(stmmac_res->addr);
  350. }
  351. EXPORT_SYMBOL_GPL(stmmac_get_platform_resources);
  352. /**
  353. * stmmac_pltfr_remove
  354. * @pdev: platform device pointer
  355. * Description: this function calls the main to free the net resources
  356. * and calls the platforms hook and release the resources (e.g. mem).
  357. */
  358. int stmmac_pltfr_remove(struct platform_device *pdev)
  359. {
  360. struct net_device *ndev = platform_get_drvdata(pdev);
  361. struct stmmac_priv *priv = netdev_priv(ndev);
  362. struct plat_stmmacenet_data *plat = priv->plat;
  363. int ret = stmmac_dvr_remove(&pdev->dev);
  364. if (plat->exit)
  365. plat->exit(pdev, plat->bsp_priv);
  366. stmmac_remove_config_dt(pdev, plat);
  367. return ret;
  368. }
  369. EXPORT_SYMBOL_GPL(stmmac_pltfr_remove);
  370. #ifdef CONFIG_PM_SLEEP
  371. /**
  372. * stmmac_pltfr_suspend
  373. * @dev: device pointer
  374. * Description: this function is invoked when suspend the driver and it direcly
  375. * call the main suspend function and then, if required, on some platform, it
  376. * can call an exit helper.
  377. */
  378. static int stmmac_pltfr_suspend(struct device *dev)
  379. {
  380. int ret;
  381. struct net_device *ndev = dev_get_drvdata(dev);
  382. struct stmmac_priv *priv = netdev_priv(ndev);
  383. struct platform_device *pdev = to_platform_device(dev);
  384. ret = stmmac_suspend(dev);
  385. if (priv->plat->suspend)
  386. priv->plat->suspend(pdev, priv->plat->bsp_priv);
  387. else if (priv->plat->exit)
  388. priv->plat->exit(pdev, priv->plat->bsp_priv);
  389. return ret;
  390. }
  391. /**
  392. * stmmac_pltfr_resume
  393. * @dev: device pointer
  394. * Description: this function is invoked when resume the driver before calling
  395. * the main resume function, on some platforms, it can call own init helper
  396. * if required.
  397. */
  398. static int stmmac_pltfr_resume(struct device *dev)
  399. {
  400. struct net_device *ndev = dev_get_drvdata(dev);
  401. struct stmmac_priv *priv = netdev_priv(ndev);
  402. struct platform_device *pdev = to_platform_device(dev);
  403. if (priv->plat->resume)
  404. priv->plat->resume(pdev, priv->plat->bsp_priv);
  405. else if (priv->plat->init)
  406. priv->plat->init(pdev, priv->plat->bsp_priv);
  407. return stmmac_resume(dev);
  408. }
  409. #endif /* CONFIG_PM_SLEEP */
  410. SIMPLE_DEV_PM_OPS(stmmac_pltfr_pm_ops, stmmac_pltfr_suspend,
  411. stmmac_pltfr_resume);
  412. EXPORT_SYMBOL_GPL(stmmac_pltfr_pm_ops);
  413. MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet platform support");
  414. MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
  415. MODULE_LICENSE("GPL");