stmmac_platform.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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 = devm_kzalloc(&pdev->dev, 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_mtl_setup - parse DT parameters for multiple queues configuration
  117. * @pdev: platform device
  118. */
  119. static int stmmac_mtl_setup(struct platform_device *pdev,
  120. struct plat_stmmacenet_data *plat)
  121. {
  122. struct device_node *q_node;
  123. struct device_node *rx_node;
  124. struct device_node *tx_node;
  125. u8 queue = 0;
  126. int ret = 0;
  127. /* For backwards-compatibility with device trees that don't have any
  128. * snps,mtl-rx-config or snps,mtl-tx-config properties, we fall back
  129. * to one RX and TX queues each.
  130. */
  131. plat->rx_queues_to_use = 1;
  132. plat->tx_queues_to_use = 1;
  133. /* First Queue must always be in DCB mode. As MTL_QUEUE_DCB = 1 we need
  134. * to always set this, otherwise Queue will be classified as AVB
  135. * (because MTL_QUEUE_AVB = 0).
  136. */
  137. plat->rx_queues_cfg[0].mode_to_use = MTL_QUEUE_DCB;
  138. plat->tx_queues_cfg[0].mode_to_use = MTL_QUEUE_DCB;
  139. rx_node = of_parse_phandle(pdev->dev.of_node, "snps,mtl-rx-config", 0);
  140. if (!rx_node)
  141. return ret;
  142. tx_node = of_parse_phandle(pdev->dev.of_node, "snps,mtl-tx-config", 0);
  143. if (!tx_node) {
  144. of_node_put(rx_node);
  145. return ret;
  146. }
  147. /* Processing RX queues common config */
  148. if (of_property_read_u32(rx_node, "snps,rx-queues-to-use",
  149. &plat->rx_queues_to_use))
  150. plat->rx_queues_to_use = 1;
  151. if (of_property_read_bool(rx_node, "snps,rx-sched-sp"))
  152. plat->rx_sched_algorithm = MTL_RX_ALGORITHM_SP;
  153. else if (of_property_read_bool(rx_node, "snps,rx-sched-wsp"))
  154. plat->rx_sched_algorithm = MTL_RX_ALGORITHM_WSP;
  155. else
  156. plat->rx_sched_algorithm = MTL_RX_ALGORITHM_SP;
  157. /* Processing individual RX queue config */
  158. for_each_child_of_node(rx_node, q_node) {
  159. if (queue >= plat->rx_queues_to_use)
  160. break;
  161. if (of_property_read_bool(q_node, "snps,dcb-algorithm"))
  162. plat->rx_queues_cfg[queue].mode_to_use = MTL_QUEUE_DCB;
  163. else if (of_property_read_bool(q_node, "snps,avb-algorithm"))
  164. plat->rx_queues_cfg[queue].mode_to_use = MTL_QUEUE_AVB;
  165. else
  166. plat->rx_queues_cfg[queue].mode_to_use = MTL_QUEUE_DCB;
  167. if (of_property_read_u32(q_node, "snps,map-to-dma-channel",
  168. &plat->rx_queues_cfg[queue].chan))
  169. plat->rx_queues_cfg[queue].chan = queue;
  170. /* TODO: Dynamic mapping to be included in the future */
  171. if (of_property_read_u32(q_node, "snps,priority",
  172. &plat->rx_queues_cfg[queue].prio)) {
  173. plat->rx_queues_cfg[queue].prio = 0;
  174. plat->rx_queues_cfg[queue].use_prio = false;
  175. } else {
  176. plat->rx_queues_cfg[queue].use_prio = true;
  177. }
  178. /* RX queue specific packet type routing */
  179. if (of_property_read_bool(q_node, "snps,route-avcp"))
  180. plat->rx_queues_cfg[queue].pkt_route = PACKET_AVCPQ;
  181. else if (of_property_read_bool(q_node, "snps,route-ptp"))
  182. plat->rx_queues_cfg[queue].pkt_route = PACKET_PTPQ;
  183. else if (of_property_read_bool(q_node, "snps,route-dcbcp"))
  184. plat->rx_queues_cfg[queue].pkt_route = PACKET_DCBCPQ;
  185. else if (of_property_read_bool(q_node, "snps,route-up"))
  186. plat->rx_queues_cfg[queue].pkt_route = PACKET_UPQ;
  187. else if (of_property_read_bool(q_node, "snps,route-multi-broad"))
  188. plat->rx_queues_cfg[queue].pkt_route = PACKET_MCBCQ;
  189. else
  190. plat->rx_queues_cfg[queue].pkt_route = 0x0;
  191. queue++;
  192. }
  193. if (queue != plat->rx_queues_to_use) {
  194. ret = -EINVAL;
  195. dev_err(&pdev->dev, "Not all RX queues were configured\n");
  196. goto out;
  197. }
  198. /* Processing TX queues common config */
  199. if (of_property_read_u32(tx_node, "snps,tx-queues-to-use",
  200. &plat->tx_queues_to_use))
  201. plat->tx_queues_to_use = 1;
  202. if (of_property_read_bool(tx_node, "snps,tx-sched-wrr"))
  203. plat->tx_sched_algorithm = MTL_TX_ALGORITHM_WRR;
  204. else if (of_property_read_bool(tx_node, "snps,tx-sched-wfq"))
  205. plat->tx_sched_algorithm = MTL_TX_ALGORITHM_WFQ;
  206. else if (of_property_read_bool(tx_node, "snps,tx-sched-dwrr"))
  207. plat->tx_sched_algorithm = MTL_TX_ALGORITHM_DWRR;
  208. else if (of_property_read_bool(tx_node, "snps,tx-sched-sp"))
  209. plat->tx_sched_algorithm = MTL_TX_ALGORITHM_SP;
  210. else
  211. plat->tx_sched_algorithm = MTL_TX_ALGORITHM_SP;
  212. queue = 0;
  213. /* Processing individual TX queue config */
  214. for_each_child_of_node(tx_node, q_node) {
  215. if (queue >= plat->tx_queues_to_use)
  216. break;
  217. if (of_property_read_u32(q_node, "snps,weight",
  218. &plat->tx_queues_cfg[queue].weight))
  219. plat->tx_queues_cfg[queue].weight = 0x10 + queue;
  220. if (of_property_read_bool(q_node, "snps,dcb-algorithm")) {
  221. plat->tx_queues_cfg[queue].mode_to_use = MTL_QUEUE_DCB;
  222. } else if (of_property_read_bool(q_node,
  223. "snps,avb-algorithm")) {
  224. plat->tx_queues_cfg[queue].mode_to_use = MTL_QUEUE_AVB;
  225. /* Credit Base Shaper parameters used by AVB */
  226. if (of_property_read_u32(q_node, "snps,send_slope",
  227. &plat->tx_queues_cfg[queue].send_slope))
  228. plat->tx_queues_cfg[queue].send_slope = 0x0;
  229. if (of_property_read_u32(q_node, "snps,idle_slope",
  230. &plat->tx_queues_cfg[queue].idle_slope))
  231. plat->tx_queues_cfg[queue].idle_slope = 0x0;
  232. if (of_property_read_u32(q_node, "snps,high_credit",
  233. &plat->tx_queues_cfg[queue].high_credit))
  234. plat->tx_queues_cfg[queue].high_credit = 0x0;
  235. if (of_property_read_u32(q_node, "snps,low_credit",
  236. &plat->tx_queues_cfg[queue].low_credit))
  237. plat->tx_queues_cfg[queue].low_credit = 0x0;
  238. } else {
  239. plat->tx_queues_cfg[queue].mode_to_use = MTL_QUEUE_DCB;
  240. }
  241. if (of_property_read_u32(q_node, "snps,priority",
  242. &plat->tx_queues_cfg[queue].prio)) {
  243. plat->tx_queues_cfg[queue].prio = 0;
  244. plat->tx_queues_cfg[queue].use_prio = false;
  245. } else {
  246. plat->tx_queues_cfg[queue].use_prio = true;
  247. }
  248. queue++;
  249. }
  250. if (queue != plat->tx_queues_to_use) {
  251. ret = -EINVAL;
  252. dev_err(&pdev->dev, "Not all TX queues were configured\n");
  253. goto out;
  254. }
  255. out:
  256. of_node_put(rx_node);
  257. of_node_put(tx_node);
  258. of_node_put(q_node);
  259. return ret;
  260. }
  261. /**
  262. * stmmac_dt_phy - parse device-tree driver parameters to allocate PHY resources
  263. * @plat: driver data platform structure
  264. * @np: device tree node
  265. * @dev: device pointer
  266. * Description:
  267. * The mdio bus will be allocated in case of a phy transceiver is on board;
  268. * it will be NULL if the fixed-link is configured.
  269. * If there is the "snps,dwmac-mdio" sub-node the mdio will be allocated
  270. * in any case (for DSA, mdio must be registered even if fixed-link).
  271. * The table below sums the supported configurations:
  272. * -------------------------------
  273. * snps,phy-addr | Y
  274. * -------------------------------
  275. * phy-handle | Y
  276. * -------------------------------
  277. * fixed-link | N
  278. * -------------------------------
  279. * snps,dwmac-mdio |
  280. * even if | Y
  281. * fixed-link |
  282. * -------------------------------
  283. *
  284. * It returns 0 in case of success otherwise -ENODEV.
  285. */
  286. static int stmmac_dt_phy(struct plat_stmmacenet_data *plat,
  287. struct device_node *np, struct device *dev)
  288. {
  289. bool mdio = true;
  290. static const struct of_device_id need_mdio_ids[] = {
  291. { .compatible = "snps,dwc-qos-ethernet-4.10" },
  292. {},
  293. };
  294. /* If phy-handle property is passed from DT, use it as the PHY */
  295. plat->phy_node = of_parse_phandle(np, "phy-handle", 0);
  296. if (plat->phy_node)
  297. dev_dbg(dev, "Found phy-handle subnode\n");
  298. /* If phy-handle is not specified, check if we have a fixed-phy */
  299. if (!plat->phy_node && of_phy_is_fixed_link(np)) {
  300. if ((of_phy_register_fixed_link(np) < 0))
  301. return -ENODEV;
  302. dev_dbg(dev, "Found fixed-link subnode\n");
  303. plat->phy_node = of_node_get(np);
  304. mdio = false;
  305. }
  306. if (of_match_node(need_mdio_ids, np)) {
  307. plat->mdio_node = of_get_child_by_name(np, "mdio");
  308. } else {
  309. /**
  310. * If snps,dwmac-mdio is passed from DT, always register
  311. * the MDIO
  312. */
  313. for_each_child_of_node(np, plat->mdio_node) {
  314. if (of_device_is_compatible(plat->mdio_node,
  315. "snps,dwmac-mdio"))
  316. break;
  317. }
  318. }
  319. if (plat->mdio_node) {
  320. dev_dbg(dev, "Found MDIO subnode\n");
  321. mdio = true;
  322. }
  323. if (mdio)
  324. plat->mdio_bus_data =
  325. devm_kzalloc(dev, sizeof(struct stmmac_mdio_bus_data),
  326. GFP_KERNEL);
  327. return 0;
  328. }
  329. /**
  330. * stmmac_probe_config_dt - parse device-tree driver parameters
  331. * @pdev: platform_device structure
  332. * @mac: MAC address to use
  333. * Description:
  334. * this function is to read the driver parameters from device-tree and
  335. * set some private fields that will be used by the main at runtime.
  336. */
  337. struct plat_stmmacenet_data *
  338. stmmac_probe_config_dt(struct platform_device *pdev, const char **mac)
  339. {
  340. struct device_node *np = pdev->dev.of_node;
  341. struct plat_stmmacenet_data *plat;
  342. struct stmmac_dma_cfg *dma_cfg;
  343. int rc;
  344. plat = devm_kzalloc(&pdev->dev, sizeof(*plat), GFP_KERNEL);
  345. if (!plat)
  346. return ERR_PTR(-ENOMEM);
  347. *mac = of_get_mac_address(np);
  348. plat->interface = of_get_phy_mode(np);
  349. /* Get max speed of operation from device tree */
  350. if (of_property_read_u32(np, "max-speed", &plat->max_speed))
  351. plat->max_speed = -1;
  352. plat->bus_id = of_alias_get_id(np, "ethernet");
  353. if (plat->bus_id < 0)
  354. plat->bus_id = 0;
  355. /* Default to phy auto-detection */
  356. plat->phy_addr = -1;
  357. /* "snps,phy-addr" is not a standard property. Mark it as deprecated
  358. * and warn of its use. Remove this when phy node support is added.
  359. */
  360. if (of_property_read_u32(np, "snps,phy-addr", &plat->phy_addr) == 0)
  361. dev_warn(&pdev->dev, "snps,phy-addr property is deprecated\n");
  362. /* To Configure PHY by using all device-tree supported properties */
  363. rc = stmmac_dt_phy(plat, np, &pdev->dev);
  364. if (rc)
  365. return ERR_PTR(rc);
  366. of_property_read_u32(np, "tx-fifo-depth", &plat->tx_fifo_size);
  367. of_property_read_u32(np, "rx-fifo-depth", &plat->rx_fifo_size);
  368. plat->force_sf_dma_mode =
  369. of_property_read_bool(np, "snps,force_sf_dma_mode");
  370. plat->en_tx_lpi_clockgating =
  371. of_property_read_bool(np, "snps,en-tx-lpi-clockgating");
  372. /* Set the maxmtu to a default of JUMBO_LEN in case the
  373. * parameter is not present in the device tree.
  374. */
  375. plat->maxmtu = JUMBO_LEN;
  376. /* Set default value for multicast hash bins */
  377. plat->multicast_filter_bins = HASH_TABLE_SIZE;
  378. /* Set default value for unicast filter entries */
  379. plat->unicast_filter_entries = 1;
  380. /*
  381. * Currently only the properties needed on SPEAr600
  382. * are provided. All other properties should be added
  383. * once needed on other platforms.
  384. */
  385. if (of_device_is_compatible(np, "st,spear600-gmac") ||
  386. of_device_is_compatible(np, "snps,dwmac-3.50a") ||
  387. of_device_is_compatible(np, "snps,dwmac-3.70a") ||
  388. of_device_is_compatible(np, "snps,dwmac")) {
  389. /* Note that the max-frame-size parameter as defined in the
  390. * ePAPR v1.1 spec is defined as max-frame-size, it's
  391. * actually used as the IEEE definition of MAC Client
  392. * data, or MTU. The ePAPR specification is confusing as
  393. * the definition is max-frame-size, but usage examples
  394. * are clearly MTUs
  395. */
  396. of_property_read_u32(np, "max-frame-size", &plat->maxmtu);
  397. of_property_read_u32(np, "snps,multicast-filter-bins",
  398. &plat->multicast_filter_bins);
  399. of_property_read_u32(np, "snps,perfect-filter-entries",
  400. &plat->unicast_filter_entries);
  401. plat->unicast_filter_entries = dwmac1000_validate_ucast_entries(
  402. plat->unicast_filter_entries);
  403. plat->multicast_filter_bins = dwmac1000_validate_mcast_bins(
  404. plat->multicast_filter_bins);
  405. plat->has_gmac = 1;
  406. plat->pmt = 1;
  407. }
  408. if (of_device_is_compatible(np, "snps,dwmac-4.00") ||
  409. of_device_is_compatible(np, "snps,dwmac-4.10a") ||
  410. of_device_is_compatible(np, "snps,dwmac-4.20a")) {
  411. plat->has_gmac4 = 1;
  412. plat->has_gmac = 0;
  413. plat->pmt = 1;
  414. plat->tso_en = of_property_read_bool(np, "snps,tso");
  415. }
  416. if (of_device_is_compatible(np, "snps,dwmac-3.610") ||
  417. of_device_is_compatible(np, "snps,dwmac-3.710")) {
  418. plat->enh_desc = 1;
  419. plat->bugged_jumbo = 1;
  420. plat->force_sf_dma_mode = 1;
  421. }
  422. dma_cfg = devm_kzalloc(&pdev->dev, sizeof(*dma_cfg),
  423. GFP_KERNEL);
  424. if (!dma_cfg) {
  425. stmmac_remove_config_dt(pdev, plat);
  426. return ERR_PTR(-ENOMEM);
  427. }
  428. plat->dma_cfg = dma_cfg;
  429. of_property_read_u32(np, "snps,pbl", &dma_cfg->pbl);
  430. if (!dma_cfg->pbl)
  431. dma_cfg->pbl = DEFAULT_DMA_PBL;
  432. of_property_read_u32(np, "snps,txpbl", &dma_cfg->txpbl);
  433. of_property_read_u32(np, "snps,rxpbl", &dma_cfg->rxpbl);
  434. dma_cfg->pblx8 = !of_property_read_bool(np, "snps,no-pbl-x8");
  435. dma_cfg->aal = of_property_read_bool(np, "snps,aal");
  436. dma_cfg->fixed_burst = of_property_read_bool(np, "snps,fixed-burst");
  437. dma_cfg->mixed_burst = of_property_read_bool(np, "snps,mixed-burst");
  438. plat->force_thresh_dma_mode = of_property_read_bool(np, "snps,force_thresh_dma_mode");
  439. if (plat->force_thresh_dma_mode) {
  440. plat->force_sf_dma_mode = 0;
  441. pr_warn("force_sf_dma_mode is ignored if force_thresh_dma_mode is set.");
  442. }
  443. of_property_read_u32(np, "snps,ps-speed", &plat->mac_port_sel_speed);
  444. plat->axi = stmmac_axi_setup(pdev);
  445. rc = stmmac_mtl_setup(pdev, plat);
  446. if (rc) {
  447. stmmac_remove_config_dt(pdev, plat);
  448. return ERR_PTR(rc);
  449. }
  450. /* clock setup */
  451. plat->stmmac_clk = devm_clk_get(&pdev->dev,
  452. STMMAC_RESOURCE_NAME);
  453. if (IS_ERR(plat->stmmac_clk)) {
  454. dev_warn(&pdev->dev, "Cannot get CSR clock\n");
  455. plat->stmmac_clk = NULL;
  456. }
  457. clk_prepare_enable(plat->stmmac_clk);
  458. plat->pclk = devm_clk_get(&pdev->dev, "pclk");
  459. if (IS_ERR(plat->pclk)) {
  460. if (PTR_ERR(plat->pclk) == -EPROBE_DEFER)
  461. goto error_pclk_get;
  462. plat->pclk = NULL;
  463. }
  464. clk_prepare_enable(plat->pclk);
  465. /* Fall-back to main clock in case of no PTP ref is passed */
  466. plat->clk_ptp_ref = devm_clk_get(&pdev->dev, "ptp_ref");
  467. if (IS_ERR(plat->clk_ptp_ref)) {
  468. plat->clk_ptp_rate = clk_get_rate(plat->stmmac_clk);
  469. plat->clk_ptp_ref = NULL;
  470. dev_warn(&pdev->dev, "PTP uses main clock\n");
  471. } else {
  472. plat->clk_ptp_rate = clk_get_rate(plat->clk_ptp_ref);
  473. dev_dbg(&pdev->dev, "PTP rate %d\n", plat->clk_ptp_rate);
  474. }
  475. plat->stmmac_rst = devm_reset_control_get(&pdev->dev,
  476. STMMAC_RESOURCE_NAME);
  477. if (IS_ERR(plat->stmmac_rst)) {
  478. if (PTR_ERR(plat->stmmac_rst) == -EPROBE_DEFER)
  479. goto error_hw_init;
  480. dev_info(&pdev->dev, "no reset control found\n");
  481. plat->stmmac_rst = NULL;
  482. }
  483. return plat;
  484. error_hw_init:
  485. clk_disable_unprepare(plat->pclk);
  486. error_pclk_get:
  487. clk_disable_unprepare(plat->stmmac_clk);
  488. return ERR_PTR(-EPROBE_DEFER);
  489. }
  490. /**
  491. * stmmac_remove_config_dt - undo the effects of stmmac_probe_config_dt()
  492. * @pdev: platform_device structure
  493. * @plat: driver data platform structure
  494. *
  495. * Release resources claimed by stmmac_probe_config_dt().
  496. */
  497. void stmmac_remove_config_dt(struct platform_device *pdev,
  498. struct plat_stmmacenet_data *plat)
  499. {
  500. struct device_node *np = pdev->dev.of_node;
  501. if (of_phy_is_fixed_link(np))
  502. of_phy_deregister_fixed_link(np);
  503. of_node_put(plat->phy_node);
  504. of_node_put(plat->mdio_node);
  505. }
  506. #else
  507. struct plat_stmmacenet_data *
  508. stmmac_probe_config_dt(struct platform_device *pdev, const char **mac)
  509. {
  510. return ERR_PTR(-EINVAL);
  511. }
  512. void stmmac_remove_config_dt(struct platform_device *pdev,
  513. struct plat_stmmacenet_data *plat)
  514. {
  515. }
  516. #endif /* CONFIG_OF */
  517. EXPORT_SYMBOL_GPL(stmmac_probe_config_dt);
  518. EXPORT_SYMBOL_GPL(stmmac_remove_config_dt);
  519. int stmmac_get_platform_resources(struct platform_device *pdev,
  520. struct stmmac_resources *stmmac_res)
  521. {
  522. struct resource *res;
  523. memset(stmmac_res, 0, sizeof(*stmmac_res));
  524. /* Get IRQ information early to have an ability to ask for deferred
  525. * probe if needed before we went too far with resource allocation.
  526. */
  527. stmmac_res->irq = platform_get_irq_byname(pdev, "macirq");
  528. if (stmmac_res->irq < 0) {
  529. if (stmmac_res->irq != -EPROBE_DEFER) {
  530. dev_err(&pdev->dev,
  531. "MAC IRQ configuration information not found\n");
  532. }
  533. return stmmac_res->irq;
  534. }
  535. /* On some platforms e.g. SPEAr the wake up irq differs from the mac irq
  536. * The external wake up irq can be passed through the platform code
  537. * named as "eth_wake_irq"
  538. *
  539. * In case the wake up interrupt is not passed from the platform
  540. * so the driver will continue to use the mac irq (ndev->irq)
  541. */
  542. stmmac_res->wol_irq = platform_get_irq_byname(pdev, "eth_wake_irq");
  543. if (stmmac_res->wol_irq < 0) {
  544. if (stmmac_res->wol_irq == -EPROBE_DEFER)
  545. return -EPROBE_DEFER;
  546. stmmac_res->wol_irq = stmmac_res->irq;
  547. }
  548. stmmac_res->lpi_irq = platform_get_irq_byname(pdev, "eth_lpi");
  549. if (stmmac_res->lpi_irq == -EPROBE_DEFER)
  550. return -EPROBE_DEFER;
  551. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  552. stmmac_res->addr = devm_ioremap_resource(&pdev->dev, res);
  553. return PTR_ERR_OR_ZERO(stmmac_res->addr);
  554. }
  555. EXPORT_SYMBOL_GPL(stmmac_get_platform_resources);
  556. /**
  557. * stmmac_pltfr_remove
  558. * @pdev: platform device pointer
  559. * Description: this function calls the main to free the net resources
  560. * and calls the platforms hook and release the resources (e.g. mem).
  561. */
  562. int stmmac_pltfr_remove(struct platform_device *pdev)
  563. {
  564. struct net_device *ndev = platform_get_drvdata(pdev);
  565. struct stmmac_priv *priv = netdev_priv(ndev);
  566. struct plat_stmmacenet_data *plat = priv->plat;
  567. int ret = stmmac_dvr_remove(&pdev->dev);
  568. if (plat->exit)
  569. plat->exit(pdev, plat->bsp_priv);
  570. stmmac_remove_config_dt(pdev, plat);
  571. return ret;
  572. }
  573. EXPORT_SYMBOL_GPL(stmmac_pltfr_remove);
  574. #ifdef CONFIG_PM_SLEEP
  575. /**
  576. * stmmac_pltfr_suspend
  577. * @dev: device pointer
  578. * Description: this function is invoked when suspend the driver and it direcly
  579. * call the main suspend function and then, if required, on some platform, it
  580. * can call an exit helper.
  581. */
  582. static int stmmac_pltfr_suspend(struct device *dev)
  583. {
  584. int ret;
  585. struct net_device *ndev = dev_get_drvdata(dev);
  586. struct stmmac_priv *priv = netdev_priv(ndev);
  587. struct platform_device *pdev = to_platform_device(dev);
  588. ret = stmmac_suspend(dev);
  589. if (priv->plat->exit)
  590. priv->plat->exit(pdev, priv->plat->bsp_priv);
  591. return ret;
  592. }
  593. /**
  594. * stmmac_pltfr_resume
  595. * @dev: device pointer
  596. * Description: this function is invoked when resume the driver before calling
  597. * the main resume function, on some platforms, it can call own init helper
  598. * if required.
  599. */
  600. static int stmmac_pltfr_resume(struct device *dev)
  601. {
  602. struct net_device *ndev = dev_get_drvdata(dev);
  603. struct stmmac_priv *priv = netdev_priv(ndev);
  604. struct platform_device *pdev = to_platform_device(dev);
  605. if (priv->plat->init)
  606. priv->plat->init(pdev, priv->plat->bsp_priv);
  607. return stmmac_resume(dev);
  608. }
  609. #endif /* CONFIG_PM_SLEEP */
  610. SIMPLE_DEV_PM_OPS(stmmac_pltfr_pm_ops, stmmac_pltfr_suspend,
  611. stmmac_pltfr_resume);
  612. EXPORT_SYMBOL_GPL(stmmac_pltfr_pm_ops);
  613. MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet platform support");
  614. MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
  615. MODULE_LICENSE("GPL");