stmmac_platform.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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. The full GNU General Public License is included in this distribution in
  12. the file called "COPYING".
  13. Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
  14. *******************************************************************************/
  15. #include <linux/platform_device.h>
  16. #include <linux/module.h>
  17. #include <linux/io.h>
  18. #include <linux/of.h>
  19. #include <linux/of_net.h>
  20. #include <linux/of_device.h>
  21. #include <linux/of_mdio.h>
  22. #include "stmmac.h"
  23. #include "stmmac_platform.h"
  24. #ifdef CONFIG_OF
  25. /**
  26. * dwmac1000_validate_mcast_bins - validates the number of Multicast filter bins
  27. * @mcast_bins: Multicast filtering bins
  28. * Description:
  29. * this function validates the number of Multicast filtering bins specified
  30. * by the configuration through the device tree. The Synopsys GMAC supports
  31. * 64 bins, 128 bins, or 256 bins. "bins" refer to the division of CRC
  32. * number space. 64 bins correspond to 6 bits of the CRC, 128 corresponds
  33. * to 7 bits, and 256 refers to 8 bits of the CRC. Any other setting is
  34. * invalid and will cause the filtering algorithm to use Multicast
  35. * promiscuous mode.
  36. */
  37. static int dwmac1000_validate_mcast_bins(int mcast_bins)
  38. {
  39. int x = mcast_bins;
  40. switch (x) {
  41. case HASH_TABLE_SIZE:
  42. case 128:
  43. case 256:
  44. break;
  45. default:
  46. x = 0;
  47. pr_info("Hash table entries set to unexpected value %d",
  48. mcast_bins);
  49. break;
  50. }
  51. return x;
  52. }
  53. /**
  54. * dwmac1000_validate_ucast_entries - validate the Unicast address entries
  55. * @ucast_entries: number of Unicast address entries
  56. * Description:
  57. * This function validates the number of Unicast address entries supported
  58. * by a particular Synopsys 10/100/1000 controller. The Synopsys controller
  59. * supports 1, 32, 64, or 128 Unicast filter entries for it's Unicast filter
  60. * logic. This function validates a valid, supported configuration is
  61. * selected, and defaults to 1 Unicast address if an unsupported
  62. * configuration is selected.
  63. */
  64. static int dwmac1000_validate_ucast_entries(int ucast_entries)
  65. {
  66. int x = ucast_entries;
  67. switch (x) {
  68. case 1:
  69. case 32:
  70. case 64:
  71. case 128:
  72. break;
  73. default:
  74. x = 1;
  75. pr_info("Unicast table entries set to unexpected value %d\n",
  76. ucast_entries);
  77. break;
  78. }
  79. return x;
  80. }
  81. /**
  82. * stmmac_axi_setup - parse DT parameters for programming the AXI register
  83. * @pdev: platform device
  84. * @priv: driver private struct.
  85. * Description:
  86. * if required, from device-tree the AXI internal register can be tuned
  87. * by using platform parameters.
  88. */
  89. static struct stmmac_axi *stmmac_axi_setup(struct platform_device *pdev)
  90. {
  91. struct device_node *np;
  92. struct stmmac_axi *axi;
  93. np = of_parse_phandle(pdev->dev.of_node, "snps,axi-config", 0);
  94. if (!np)
  95. return NULL;
  96. axi = kzalloc(sizeof(*axi), GFP_KERNEL);
  97. if (!axi) {
  98. of_node_put(np);
  99. return ERR_PTR(-ENOMEM);
  100. }
  101. axi->axi_lpi_en = of_property_read_bool(np, "snps,lpi_en");
  102. axi->axi_xit_frm = of_property_read_bool(np, "snps,xit_frm");
  103. axi->axi_kbbe = of_property_read_bool(np, "snps,axi_kbbe");
  104. axi->axi_fb = of_property_read_bool(np, "snps,axi_fb");
  105. axi->axi_mb = of_property_read_bool(np, "snps,axi_mb");
  106. axi->axi_rb = of_property_read_bool(np, "snps,axi_rb");
  107. if (of_property_read_u32(np, "snps,wr_osr_lmt", &axi->axi_wr_osr_lmt))
  108. axi->axi_wr_osr_lmt = 1;
  109. if (of_property_read_u32(np, "snps,rd_osr_lmt", &axi->axi_rd_osr_lmt))
  110. axi->axi_rd_osr_lmt = 1;
  111. of_property_read_u32_array(np, "snps,blen", axi->axi_blen, AXI_BLEN);
  112. of_node_put(np);
  113. return axi;
  114. }
  115. /**
  116. * stmmac_dt_phy - parse device-tree driver parameters to allocate PHY resources
  117. * @plat: driver data platform structure
  118. * @np: device tree node
  119. * @dev: device pointer
  120. * Description:
  121. * The mdio bus will be allocated in case of a phy transceiver is on board;
  122. * it will be NULL if the fixed-link is configured.
  123. * If there is the "snps,dwmac-mdio" sub-node the mdio will be allocated
  124. * in any case (for DSA, mdio must be registered even if fixed-link).
  125. * The table below sums the supported configurations:
  126. * -------------------------------
  127. * snps,phy-addr | Y
  128. * -------------------------------
  129. * phy-handle | Y
  130. * -------------------------------
  131. * fixed-link | N
  132. * -------------------------------
  133. * snps,dwmac-mdio |
  134. * even if | Y
  135. * fixed-link |
  136. * -------------------------------
  137. *
  138. * It returns 0 in case of success otherwise -ENODEV.
  139. */
  140. static int stmmac_dt_phy(struct plat_stmmacenet_data *plat,
  141. struct device_node *np, struct device *dev)
  142. {
  143. bool mdio = true;
  144. /* If phy-handle property is passed from DT, use it as the PHY */
  145. plat->phy_node = of_parse_phandle(np, "phy-handle", 0);
  146. if (plat->phy_node)
  147. dev_dbg(dev, "Found phy-handle subnode\n");
  148. /* If phy-handle is not specified, check if we have a fixed-phy */
  149. if (!plat->phy_node && of_phy_is_fixed_link(np)) {
  150. if ((of_phy_register_fixed_link(np) < 0))
  151. return -ENODEV;
  152. dev_dbg(dev, "Found fixed-link subnode\n");
  153. plat->phy_node = of_node_get(np);
  154. mdio = false;
  155. }
  156. /* exception for dwmac-dwc-qos-eth glue logic */
  157. if (of_device_is_compatible(np, "snps,dwc-qos-ethernet-4.10")) {
  158. plat->mdio_node = of_get_child_by_name(np, "mdio");
  159. } else {
  160. /**
  161. * If snps,dwmac-mdio is passed from DT, always register
  162. * the MDIO
  163. */
  164. for_each_child_of_node(np, plat->mdio_node) {
  165. if (of_device_is_compatible(plat->mdio_node,
  166. "snps,dwmac-mdio"))
  167. break;
  168. }
  169. }
  170. if (plat->mdio_node) {
  171. dev_dbg(dev, "Found MDIO subnode\n");
  172. mdio = true;
  173. }
  174. if (mdio)
  175. plat->mdio_bus_data =
  176. devm_kzalloc(dev, sizeof(struct stmmac_mdio_bus_data),
  177. GFP_KERNEL);
  178. return 0;
  179. }
  180. /**
  181. * stmmac_probe_config_dt - parse device-tree driver parameters
  182. * @pdev: platform_device structure
  183. * @mac: MAC address to use
  184. * Description:
  185. * this function is to read the driver parameters from device-tree and
  186. * set some private fields that will be used by the main at runtime.
  187. */
  188. struct plat_stmmacenet_data *
  189. stmmac_probe_config_dt(struct platform_device *pdev, const char **mac)
  190. {
  191. struct device_node *np = pdev->dev.of_node;
  192. struct plat_stmmacenet_data *plat;
  193. struct stmmac_dma_cfg *dma_cfg;
  194. plat = devm_kzalloc(&pdev->dev, sizeof(*plat), GFP_KERNEL);
  195. if (!plat)
  196. return ERR_PTR(-ENOMEM);
  197. *mac = of_get_mac_address(np);
  198. plat->interface = of_get_phy_mode(np);
  199. /* Get max speed of operation from device tree */
  200. if (of_property_read_u32(np, "max-speed", &plat->max_speed))
  201. plat->max_speed = -1;
  202. plat->bus_id = of_alias_get_id(np, "ethernet");
  203. if (plat->bus_id < 0)
  204. plat->bus_id = 0;
  205. /* Default to phy auto-detection */
  206. plat->phy_addr = -1;
  207. /* "snps,phy-addr" is not a standard property. Mark it as deprecated
  208. * and warn of its use. Remove this when phy node support is added.
  209. */
  210. if (of_property_read_u32(np, "snps,phy-addr", &plat->phy_addr) == 0)
  211. dev_warn(&pdev->dev, "snps,phy-addr property is deprecated\n");
  212. /* To Configure PHY by using all device-tree supported properties */
  213. if (stmmac_dt_phy(plat, np, &pdev->dev))
  214. return ERR_PTR(-ENODEV);
  215. of_property_read_u32(np, "tx-fifo-depth", &plat->tx_fifo_size);
  216. of_property_read_u32(np, "rx-fifo-depth", &plat->rx_fifo_size);
  217. plat->force_sf_dma_mode =
  218. of_property_read_bool(np, "snps,force_sf_dma_mode");
  219. plat->en_tx_lpi_clockgating =
  220. of_property_read_bool(np, "snps,en-tx-lpi-clockgating");
  221. /* Set the maxmtu to a default of JUMBO_LEN in case the
  222. * parameter is not present in the device tree.
  223. */
  224. plat->maxmtu = JUMBO_LEN;
  225. /* Set default value for multicast hash bins */
  226. plat->multicast_filter_bins = HASH_TABLE_SIZE;
  227. /* Set default value for unicast filter entries */
  228. plat->unicast_filter_entries = 1;
  229. /*
  230. * Currently only the properties needed on SPEAr600
  231. * are provided. All other properties should be added
  232. * once needed on other platforms.
  233. */
  234. if (of_device_is_compatible(np, "st,spear600-gmac") ||
  235. of_device_is_compatible(np, "snps,dwmac-3.50a") ||
  236. of_device_is_compatible(np, "snps,dwmac-3.70a") ||
  237. of_device_is_compatible(np, "snps,dwmac")) {
  238. /* Note that the max-frame-size parameter as defined in the
  239. * ePAPR v1.1 spec is defined as max-frame-size, it's
  240. * actually used as the IEEE definition of MAC Client
  241. * data, or MTU. The ePAPR specification is confusing as
  242. * the definition is max-frame-size, but usage examples
  243. * are clearly MTUs
  244. */
  245. of_property_read_u32(np, "max-frame-size", &plat->maxmtu);
  246. of_property_read_u32(np, "snps,multicast-filter-bins",
  247. &plat->multicast_filter_bins);
  248. of_property_read_u32(np, "snps,perfect-filter-entries",
  249. &plat->unicast_filter_entries);
  250. plat->unicast_filter_entries = dwmac1000_validate_ucast_entries(
  251. plat->unicast_filter_entries);
  252. plat->multicast_filter_bins = dwmac1000_validate_mcast_bins(
  253. plat->multicast_filter_bins);
  254. plat->has_gmac = 1;
  255. plat->pmt = 1;
  256. }
  257. if (of_device_is_compatible(np, "snps,dwmac-4.00") ||
  258. of_device_is_compatible(np, "snps,dwmac-4.10a")) {
  259. plat->has_gmac4 = 1;
  260. plat->has_gmac = 0;
  261. plat->pmt = 1;
  262. plat->tso_en = of_property_read_bool(np, "snps,tso");
  263. }
  264. if (of_device_is_compatible(np, "snps,dwmac-3.610") ||
  265. of_device_is_compatible(np, "snps,dwmac-3.710")) {
  266. plat->enh_desc = 1;
  267. plat->bugged_jumbo = 1;
  268. plat->force_sf_dma_mode = 1;
  269. }
  270. dma_cfg = devm_kzalloc(&pdev->dev, sizeof(*dma_cfg),
  271. GFP_KERNEL);
  272. if (!dma_cfg) {
  273. stmmac_remove_config_dt(pdev, plat);
  274. return ERR_PTR(-ENOMEM);
  275. }
  276. plat->dma_cfg = dma_cfg;
  277. of_property_read_u32(np, "snps,pbl", &dma_cfg->pbl);
  278. if (!dma_cfg->pbl)
  279. dma_cfg->pbl = DEFAULT_DMA_PBL;
  280. of_property_read_u32(np, "snps,txpbl", &dma_cfg->txpbl);
  281. of_property_read_u32(np, "snps,rxpbl", &dma_cfg->rxpbl);
  282. dma_cfg->pblx8 = !of_property_read_bool(np, "snps,no-pbl-x8");
  283. dma_cfg->aal = of_property_read_bool(np, "snps,aal");
  284. dma_cfg->fixed_burst = of_property_read_bool(np, "snps,fixed-burst");
  285. dma_cfg->mixed_burst = of_property_read_bool(np, "snps,mixed-burst");
  286. plat->force_thresh_dma_mode = of_property_read_bool(np, "snps,force_thresh_dma_mode");
  287. if (plat->force_thresh_dma_mode) {
  288. plat->force_sf_dma_mode = 0;
  289. pr_warn("force_sf_dma_mode is ignored if force_thresh_dma_mode is set.");
  290. }
  291. of_property_read_u32(np, "snps,ps-speed", &plat->mac_port_sel_speed);
  292. plat->axi = stmmac_axi_setup(pdev);
  293. /* clock setup */
  294. plat->stmmac_clk = devm_clk_get(&pdev->dev,
  295. STMMAC_RESOURCE_NAME);
  296. if (IS_ERR(plat->stmmac_clk)) {
  297. dev_warn(&pdev->dev, "Cannot get CSR clock\n");
  298. plat->stmmac_clk = NULL;
  299. }
  300. clk_prepare_enable(plat->stmmac_clk);
  301. plat->pclk = devm_clk_get(&pdev->dev, "pclk");
  302. if (IS_ERR(plat->pclk)) {
  303. if (PTR_ERR(plat->pclk) == -EPROBE_DEFER)
  304. goto error_pclk_get;
  305. plat->pclk = NULL;
  306. }
  307. clk_prepare_enable(plat->pclk);
  308. /* Fall-back to main clock in case of no PTP ref is passed */
  309. plat->clk_ptp_ref = devm_clk_get(&pdev->dev, "clk_ptp_ref");
  310. if (IS_ERR(plat->clk_ptp_ref)) {
  311. plat->clk_ptp_rate = clk_get_rate(plat->stmmac_clk);
  312. plat->clk_ptp_ref = NULL;
  313. dev_warn(&pdev->dev, "PTP uses main clock\n");
  314. } else {
  315. clk_prepare_enable(plat->clk_ptp_ref);
  316. plat->clk_ptp_rate = clk_get_rate(plat->clk_ptp_ref);
  317. dev_dbg(&pdev->dev, "PTP rate %d\n", plat->clk_ptp_rate);
  318. }
  319. plat->stmmac_rst = devm_reset_control_get(&pdev->dev,
  320. STMMAC_RESOURCE_NAME);
  321. if (IS_ERR(plat->stmmac_rst)) {
  322. if (PTR_ERR(plat->stmmac_rst) == -EPROBE_DEFER)
  323. goto error_hw_init;
  324. dev_info(&pdev->dev, "no reset control found\n");
  325. plat->stmmac_rst = NULL;
  326. }
  327. return plat;
  328. error_hw_init:
  329. clk_disable_unprepare(plat->pclk);
  330. error_pclk_get:
  331. clk_disable_unprepare(plat->stmmac_clk);
  332. return ERR_PTR(-EPROBE_DEFER);
  333. }
  334. /**
  335. * stmmac_remove_config_dt - undo the effects of stmmac_probe_config_dt()
  336. * @pdev: platform_device structure
  337. * @plat: driver data platform structure
  338. *
  339. * Release resources claimed by stmmac_probe_config_dt().
  340. */
  341. void stmmac_remove_config_dt(struct platform_device *pdev,
  342. struct plat_stmmacenet_data *plat)
  343. {
  344. struct device_node *np = pdev->dev.of_node;
  345. if (of_phy_is_fixed_link(np))
  346. of_phy_deregister_fixed_link(np);
  347. of_node_put(plat->phy_node);
  348. of_node_put(plat->mdio_node);
  349. }
  350. #else
  351. struct plat_stmmacenet_data *
  352. stmmac_probe_config_dt(struct platform_device *pdev, const char **mac)
  353. {
  354. return ERR_PTR(-EINVAL);
  355. }
  356. void stmmac_remove_config_dt(struct platform_device *pdev,
  357. struct plat_stmmacenet_data *plat)
  358. {
  359. }
  360. #endif /* CONFIG_OF */
  361. EXPORT_SYMBOL_GPL(stmmac_probe_config_dt);
  362. EXPORT_SYMBOL_GPL(stmmac_remove_config_dt);
  363. int stmmac_get_platform_resources(struct platform_device *pdev,
  364. struct stmmac_resources *stmmac_res)
  365. {
  366. struct resource *res;
  367. memset(stmmac_res, 0, sizeof(*stmmac_res));
  368. /* Get IRQ information early to have an ability to ask for deferred
  369. * probe if needed before we went too far with resource allocation.
  370. */
  371. stmmac_res->irq = platform_get_irq_byname(pdev, "macirq");
  372. if (stmmac_res->irq < 0) {
  373. if (stmmac_res->irq != -EPROBE_DEFER) {
  374. dev_err(&pdev->dev,
  375. "MAC IRQ configuration information not found\n");
  376. }
  377. return stmmac_res->irq;
  378. }
  379. /* On some platforms e.g. SPEAr the wake up irq differs from the mac irq
  380. * The external wake up irq can be passed through the platform code
  381. * named as "eth_wake_irq"
  382. *
  383. * In case the wake up interrupt is not passed from the platform
  384. * so the driver will continue to use the mac irq (ndev->irq)
  385. */
  386. stmmac_res->wol_irq = platform_get_irq_byname(pdev, "eth_wake_irq");
  387. if (stmmac_res->wol_irq < 0) {
  388. if (stmmac_res->wol_irq == -EPROBE_DEFER)
  389. return -EPROBE_DEFER;
  390. stmmac_res->wol_irq = stmmac_res->irq;
  391. }
  392. stmmac_res->lpi_irq = platform_get_irq_byname(pdev, "eth_lpi");
  393. if (stmmac_res->lpi_irq == -EPROBE_DEFER)
  394. return -EPROBE_DEFER;
  395. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  396. stmmac_res->addr = devm_ioremap_resource(&pdev->dev, res);
  397. return PTR_ERR_OR_ZERO(stmmac_res->addr);
  398. }
  399. EXPORT_SYMBOL_GPL(stmmac_get_platform_resources);
  400. /**
  401. * stmmac_pltfr_remove
  402. * @pdev: platform device pointer
  403. * Description: this function calls the main to free the net resources
  404. * and calls the platforms hook and release the resources (e.g. mem).
  405. */
  406. int stmmac_pltfr_remove(struct platform_device *pdev)
  407. {
  408. struct net_device *ndev = platform_get_drvdata(pdev);
  409. struct stmmac_priv *priv = netdev_priv(ndev);
  410. struct plat_stmmacenet_data *plat = priv->plat;
  411. int ret = stmmac_dvr_remove(&pdev->dev);
  412. if (plat->exit)
  413. plat->exit(pdev, plat->bsp_priv);
  414. stmmac_remove_config_dt(pdev, plat);
  415. return ret;
  416. }
  417. EXPORT_SYMBOL_GPL(stmmac_pltfr_remove);
  418. #ifdef CONFIG_PM_SLEEP
  419. /**
  420. * stmmac_pltfr_suspend
  421. * @dev: device pointer
  422. * Description: this function is invoked when suspend the driver and it direcly
  423. * call the main suspend function and then, if required, on some platform, it
  424. * can call an exit helper.
  425. */
  426. static int stmmac_pltfr_suspend(struct device *dev)
  427. {
  428. int ret;
  429. struct net_device *ndev = dev_get_drvdata(dev);
  430. struct stmmac_priv *priv = netdev_priv(ndev);
  431. struct platform_device *pdev = to_platform_device(dev);
  432. ret = stmmac_suspend(dev);
  433. if (priv->plat->exit)
  434. priv->plat->exit(pdev, priv->plat->bsp_priv);
  435. return ret;
  436. }
  437. /**
  438. * stmmac_pltfr_resume
  439. * @dev: device pointer
  440. * Description: this function is invoked when resume the driver before calling
  441. * the main resume function, on some platforms, it can call own init helper
  442. * if required.
  443. */
  444. static int stmmac_pltfr_resume(struct device *dev)
  445. {
  446. struct net_device *ndev = dev_get_drvdata(dev);
  447. struct stmmac_priv *priv = netdev_priv(ndev);
  448. struct platform_device *pdev = to_platform_device(dev);
  449. if (priv->plat->init)
  450. priv->plat->init(pdev, priv->plat->bsp_priv);
  451. return stmmac_resume(dev);
  452. }
  453. #endif /* CONFIG_PM_SLEEP */
  454. SIMPLE_DEV_PM_OPS(stmmac_pltfr_pm_ops, stmmac_pltfr_suspend,
  455. stmmac_pltfr_resume);
  456. EXPORT_SYMBOL_GPL(stmmac_pltfr_pm_ops);
  457. MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet platform support");
  458. MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
  459. MODULE_LICENSE("GPL");