stmmac_platform.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  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/acpi.h>
  16. #include <linux/clk-provider.h>
  17. #include <linux/clkdev.h>
  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_axi_setup - parse DT parameters for programming the AXI register
  85. * @pdev: platform device
  86. * Description:
  87. * if required, from device-tree the AXI internal register can be tuned
  88. * by using platform parameters.
  89. */
  90. static struct stmmac_axi *stmmac_axi_setup(struct platform_device *pdev)
  91. {
  92. struct device_node *np;
  93. struct stmmac_axi *axi;
  94. np = of_parse_phandle(pdev->dev.of_node, "snps,axi-config", 0);
  95. if (!np)
  96. return NULL;
  97. axi = devm_kzalloc(&pdev->dev, sizeof(*axi), GFP_KERNEL);
  98. if (!axi) {
  99. of_node_put(np);
  100. return ERR_PTR(-ENOMEM);
  101. }
  102. axi->axi_lpi_en = of_property_read_bool(np, "snps,lpi_en");
  103. axi->axi_xit_frm = of_property_read_bool(np, "snps,xit_frm");
  104. axi->axi_kbbe = of_property_read_bool(np, "snps,axi_kbbe");
  105. axi->axi_fb = of_property_read_bool(np, "snps,axi_fb");
  106. axi->axi_mb = of_property_read_bool(np, "snps,axi_mb");
  107. axi->axi_rb = of_property_read_bool(np, "snps,axi_rb");
  108. if (of_property_read_u32(np, "snps,wr_osr_lmt", &axi->axi_wr_osr_lmt))
  109. axi->axi_wr_osr_lmt = 1;
  110. if (of_property_read_u32(np, "snps,rd_osr_lmt", &axi->axi_rd_osr_lmt))
  111. axi->axi_rd_osr_lmt = 1;
  112. of_property_read_u32_array(np, "snps,blen", axi->axi_blen, AXI_BLEN);
  113. of_node_put(np);
  114. return axi;
  115. }
  116. /**
  117. * stmmac_mtl_setup - parse DT parameters for multiple queues configuration
  118. * @pdev: platform device
  119. */
  120. static int stmmac_mtl_setup(struct platform_device *pdev,
  121. struct plat_stmmacenet_data *plat)
  122. {
  123. struct device_node *q_node;
  124. struct device_node *rx_node;
  125. struct device_node *tx_node;
  126. u8 queue = 0;
  127. int ret = 0;
  128. /* For backwards-compatibility with device trees that don't have any
  129. * snps,mtl-rx-config or snps,mtl-tx-config properties, we fall back
  130. * to one RX and TX queues each.
  131. */
  132. plat->rx_queues_to_use = 1;
  133. plat->tx_queues_to_use = 1;
  134. /* First Queue must always be in DCB mode. As MTL_QUEUE_DCB = 1 we need
  135. * to always set this, otherwise Queue will be classified as AVB
  136. * (because MTL_QUEUE_AVB = 0).
  137. */
  138. plat->rx_queues_cfg[0].mode_to_use = MTL_QUEUE_DCB;
  139. plat->tx_queues_cfg[0].mode_to_use = MTL_QUEUE_DCB;
  140. rx_node = of_parse_phandle(pdev->dev.of_node, "snps,mtl-rx-config", 0);
  141. if (!rx_node)
  142. return ret;
  143. tx_node = of_parse_phandle(pdev->dev.of_node, "snps,mtl-tx-config", 0);
  144. if (!tx_node) {
  145. of_node_put(rx_node);
  146. return ret;
  147. }
  148. /* Processing RX queues common config */
  149. if (of_property_read_u32(rx_node, "snps,rx-queues-to-use",
  150. &plat->rx_queues_to_use))
  151. plat->rx_queues_to_use = 1;
  152. if (of_property_read_bool(rx_node, "snps,rx-sched-sp"))
  153. plat->rx_sched_algorithm = MTL_RX_ALGORITHM_SP;
  154. else if (of_property_read_bool(rx_node, "snps,rx-sched-wsp"))
  155. plat->rx_sched_algorithm = MTL_RX_ALGORITHM_WSP;
  156. else
  157. plat->rx_sched_algorithm = MTL_RX_ALGORITHM_SP;
  158. /* Processing individual RX queue config */
  159. for_each_child_of_node(rx_node, q_node) {
  160. if (queue >= plat->rx_queues_to_use)
  161. break;
  162. if (of_property_read_bool(q_node, "snps,dcb-algorithm"))
  163. plat->rx_queues_cfg[queue].mode_to_use = MTL_QUEUE_DCB;
  164. else if (of_property_read_bool(q_node, "snps,avb-algorithm"))
  165. plat->rx_queues_cfg[queue].mode_to_use = MTL_QUEUE_AVB;
  166. else
  167. plat->rx_queues_cfg[queue].mode_to_use = MTL_QUEUE_DCB;
  168. if (of_property_read_u32(q_node, "snps,map-to-dma-channel",
  169. &plat->rx_queues_cfg[queue].chan))
  170. plat->rx_queues_cfg[queue].chan = queue;
  171. /* TODO: Dynamic mapping to be included in the future */
  172. if (of_property_read_u32(q_node, "snps,priority",
  173. &plat->rx_queues_cfg[queue].prio)) {
  174. plat->rx_queues_cfg[queue].prio = 0;
  175. plat->rx_queues_cfg[queue].use_prio = false;
  176. } else {
  177. plat->rx_queues_cfg[queue].use_prio = true;
  178. }
  179. /* RX queue specific packet type routing */
  180. if (of_property_read_bool(q_node, "snps,route-avcp"))
  181. plat->rx_queues_cfg[queue].pkt_route = PACKET_AVCPQ;
  182. else if (of_property_read_bool(q_node, "snps,route-ptp"))
  183. plat->rx_queues_cfg[queue].pkt_route = PACKET_PTPQ;
  184. else if (of_property_read_bool(q_node, "snps,route-dcbcp"))
  185. plat->rx_queues_cfg[queue].pkt_route = PACKET_DCBCPQ;
  186. else if (of_property_read_bool(q_node, "snps,route-up"))
  187. plat->rx_queues_cfg[queue].pkt_route = PACKET_UPQ;
  188. else if (of_property_read_bool(q_node, "snps,route-multi-broad"))
  189. plat->rx_queues_cfg[queue].pkt_route = PACKET_MCBCQ;
  190. else
  191. plat->rx_queues_cfg[queue].pkt_route = 0x0;
  192. queue++;
  193. }
  194. if (queue != plat->rx_queues_to_use) {
  195. ret = -EINVAL;
  196. dev_err(&pdev->dev, "Not all RX queues were configured\n");
  197. goto out;
  198. }
  199. /* Processing TX queues common config */
  200. if (of_property_read_u32(tx_node, "snps,tx-queues-to-use",
  201. &plat->tx_queues_to_use))
  202. plat->tx_queues_to_use = 1;
  203. if (of_property_read_bool(tx_node, "snps,tx-sched-wrr"))
  204. plat->tx_sched_algorithm = MTL_TX_ALGORITHM_WRR;
  205. else if (of_property_read_bool(tx_node, "snps,tx-sched-wfq"))
  206. plat->tx_sched_algorithm = MTL_TX_ALGORITHM_WFQ;
  207. else if (of_property_read_bool(tx_node, "snps,tx-sched-dwrr"))
  208. plat->tx_sched_algorithm = MTL_TX_ALGORITHM_DWRR;
  209. else if (of_property_read_bool(tx_node, "snps,tx-sched-sp"))
  210. plat->tx_sched_algorithm = MTL_TX_ALGORITHM_SP;
  211. else
  212. plat->tx_sched_algorithm = MTL_TX_ALGORITHM_SP;
  213. queue = 0;
  214. /* Processing individual TX queue config */
  215. for_each_child_of_node(tx_node, q_node) {
  216. if (queue >= plat->tx_queues_to_use)
  217. break;
  218. if (of_property_read_u32(q_node, "snps,weight",
  219. &plat->tx_queues_cfg[queue].weight))
  220. plat->tx_queues_cfg[queue].weight = 0x10 + queue;
  221. if (of_property_read_bool(q_node, "snps,dcb-algorithm")) {
  222. plat->tx_queues_cfg[queue].mode_to_use = MTL_QUEUE_DCB;
  223. } else if (of_property_read_bool(q_node,
  224. "snps,avb-algorithm")) {
  225. plat->tx_queues_cfg[queue].mode_to_use = MTL_QUEUE_AVB;
  226. /* Credit Base Shaper parameters used by AVB */
  227. if (of_property_read_u32(q_node, "snps,send_slope",
  228. &plat->tx_queues_cfg[queue].send_slope))
  229. plat->tx_queues_cfg[queue].send_slope = 0x0;
  230. if (of_property_read_u32(q_node, "snps,idle_slope",
  231. &plat->tx_queues_cfg[queue].idle_slope))
  232. plat->tx_queues_cfg[queue].idle_slope = 0x0;
  233. if (of_property_read_u32(q_node, "snps,high_credit",
  234. &plat->tx_queues_cfg[queue].high_credit))
  235. plat->tx_queues_cfg[queue].high_credit = 0x0;
  236. if (of_property_read_u32(q_node, "snps,low_credit",
  237. &plat->tx_queues_cfg[queue].low_credit))
  238. plat->tx_queues_cfg[queue].low_credit = 0x0;
  239. } else {
  240. plat->tx_queues_cfg[queue].mode_to_use = MTL_QUEUE_DCB;
  241. }
  242. if (of_property_read_u32(q_node, "snps,priority",
  243. &plat->tx_queues_cfg[queue].prio)) {
  244. plat->tx_queues_cfg[queue].prio = 0;
  245. plat->tx_queues_cfg[queue].use_prio = false;
  246. } else {
  247. plat->tx_queues_cfg[queue].use_prio = true;
  248. }
  249. queue++;
  250. }
  251. if (queue != plat->tx_queues_to_use) {
  252. ret = -EINVAL;
  253. dev_err(&pdev->dev, "Not all TX queues were configured\n");
  254. goto out;
  255. }
  256. out:
  257. of_node_put(rx_node);
  258. of_node_put(tx_node);
  259. of_node_put(q_node);
  260. return ret;
  261. }
  262. /**
  263. * stmmac_dt_phy - parse device-tree driver parameters to allocate PHY resources
  264. * @plat: driver data platform structure
  265. * @np: device tree node
  266. * @dev: device pointer
  267. * Description:
  268. * The mdio bus will be allocated in case of a phy transceiver is on board;
  269. * it will be NULL if the fixed-link is configured.
  270. * If there is the "snps,dwmac-mdio" sub-node the mdio will be allocated
  271. * in any case (for DSA, mdio must be registered even if fixed-link).
  272. * The table below sums the supported configurations:
  273. * -------------------------------
  274. * snps,phy-addr | Y
  275. * -------------------------------
  276. * phy-handle | Y
  277. * -------------------------------
  278. * fixed-link | N
  279. * -------------------------------
  280. * snps,dwmac-mdio |
  281. * even if | Y
  282. * fixed-link |
  283. * -------------------------------
  284. *
  285. * It returns 0 in case of success otherwise -ENODEV.
  286. */
  287. static int stmmac_dt_phy(struct plat_stmmacenet_data *plat,
  288. struct device_node *np, struct device *dev)
  289. {
  290. bool mdio = true;
  291. static const struct of_device_id need_mdio_ids[] = {
  292. { .compatible = "snps,dwc-qos-ethernet-4.10" },
  293. {},
  294. };
  295. /* If phy-handle property is passed from DT, use it as the PHY */
  296. plat->phy_node = of_parse_phandle(np, "phy-handle", 0);
  297. if (plat->phy_node)
  298. dev_dbg(dev, "Found phy-handle subnode\n");
  299. /* If phy-handle is not specified, check if we have a fixed-phy */
  300. if (!plat->phy_node && of_phy_is_fixed_link(np)) {
  301. if ((of_phy_register_fixed_link(np) < 0))
  302. return -ENODEV;
  303. dev_dbg(dev, "Found fixed-link subnode\n");
  304. plat->phy_node = of_node_get(np);
  305. mdio = false;
  306. }
  307. if (of_match_node(need_mdio_ids, np)) {
  308. plat->mdio_node = of_get_child_by_name(np, "mdio");
  309. } else {
  310. /**
  311. * If snps,dwmac-mdio is passed from DT, always register
  312. * the MDIO
  313. */
  314. for_each_child_of_node(np, plat->mdio_node) {
  315. if (of_device_is_compatible(plat->mdio_node,
  316. "snps,dwmac-mdio"))
  317. break;
  318. }
  319. }
  320. if (plat->mdio_node) {
  321. dev_dbg(dev, "Found MDIO subnode\n");
  322. mdio = true;
  323. }
  324. if (mdio)
  325. plat->mdio_bus_data =
  326. devm_kzalloc(dev, sizeof(struct stmmac_mdio_bus_data),
  327. GFP_KERNEL);
  328. return 0;
  329. }
  330. /**
  331. * stmmac_probe_config_dt - parse device-tree driver parameters
  332. * @pdev: platform_device structure
  333. * @mac: MAC address to use
  334. * Description:
  335. * this function is to read the driver parameters from device-tree and
  336. * set some private fields that will be used by the main at runtime.
  337. */
  338. struct plat_stmmacenet_data *
  339. stmmac_probe_config_dt(struct platform_device *pdev, const char **mac)
  340. {
  341. struct device_node *np = pdev->dev.of_node;
  342. struct plat_stmmacenet_data *plat;
  343. struct stmmac_dma_cfg *dma_cfg;
  344. int rc;
  345. plat = devm_kzalloc(&pdev->dev, sizeof(*plat), GFP_KERNEL);
  346. if (!plat)
  347. return ERR_PTR(-ENOMEM);
  348. *mac = of_get_mac_address(np);
  349. plat->interface = of_get_phy_mode(np);
  350. /* Get max speed of operation from device tree */
  351. if (of_property_read_u32(np, "max-speed", &plat->max_speed))
  352. plat->max_speed = -1;
  353. plat->bus_id = of_alias_get_id(np, "ethernet");
  354. if (plat->bus_id < 0)
  355. plat->bus_id = 0;
  356. /* Default to phy auto-detection */
  357. plat->phy_addr = -1;
  358. /* "snps,phy-addr" is not a standard property. Mark it as deprecated
  359. * and warn of its use. Remove this when phy node support is added.
  360. */
  361. if (of_property_read_u32(np, "snps,phy-addr", &plat->phy_addr) == 0)
  362. dev_warn(&pdev->dev, "snps,phy-addr property is deprecated\n");
  363. /* To Configure PHY by using all device-tree supported properties */
  364. rc = stmmac_dt_phy(plat, np, &pdev->dev);
  365. if (rc)
  366. return ERR_PTR(rc);
  367. of_property_read_u32(np, "tx-fifo-depth", &plat->tx_fifo_size);
  368. of_property_read_u32(np, "rx-fifo-depth", &plat->rx_fifo_size);
  369. plat->force_sf_dma_mode =
  370. of_property_read_bool(np, "snps,force_sf_dma_mode");
  371. plat->en_tx_lpi_clockgating =
  372. of_property_read_bool(np, "snps,en-tx-lpi-clockgating");
  373. /* Set the maxmtu to a default of JUMBO_LEN in case the
  374. * parameter is not present in the device tree.
  375. */
  376. plat->maxmtu = JUMBO_LEN;
  377. /* Set default value for multicast hash bins */
  378. plat->multicast_filter_bins = HASH_TABLE_SIZE;
  379. /* Set default value for unicast filter entries */
  380. plat->unicast_filter_entries = 1;
  381. /*
  382. * Currently only the properties needed on SPEAr600
  383. * are provided. All other properties should be added
  384. * once needed on other platforms.
  385. */
  386. if (of_device_is_compatible(np, "st,spear600-gmac") ||
  387. of_device_is_compatible(np, "snps,dwmac-3.50a") ||
  388. of_device_is_compatible(np, "snps,dwmac-3.70a") ||
  389. of_device_is_compatible(np, "snps,dwmac")) {
  390. /* Note that the max-frame-size parameter as defined in the
  391. * ePAPR v1.1 spec is defined as max-frame-size, it's
  392. * actually used as the IEEE definition of MAC Client
  393. * data, or MTU. The ePAPR specification is confusing as
  394. * the definition is max-frame-size, but usage examples
  395. * are clearly MTUs
  396. */
  397. of_property_read_u32(np, "max-frame-size", &plat->maxmtu);
  398. of_property_read_u32(np, "snps,multicast-filter-bins",
  399. &plat->multicast_filter_bins);
  400. of_property_read_u32(np, "snps,perfect-filter-entries",
  401. &plat->unicast_filter_entries);
  402. plat->unicast_filter_entries = dwmac1000_validate_ucast_entries(
  403. plat->unicast_filter_entries);
  404. plat->multicast_filter_bins = dwmac1000_validate_mcast_bins(
  405. plat->multicast_filter_bins);
  406. plat->has_gmac = 1;
  407. plat->pmt = 1;
  408. }
  409. if (of_device_is_compatible(np, "snps,dwmac-4.00") ||
  410. of_device_is_compatible(np, "snps,dwmac-4.10a") ||
  411. of_device_is_compatible(np, "snps,dwmac-4.20a")) {
  412. plat->has_gmac4 = 1;
  413. plat->has_gmac = 0;
  414. plat->pmt = 1;
  415. plat->tso_en = of_property_read_bool(np, "snps,tso");
  416. }
  417. if (of_device_is_compatible(np, "snps,dwmac-3.610") ||
  418. of_device_is_compatible(np, "snps,dwmac-3.710")) {
  419. plat->enh_desc = 1;
  420. plat->bugged_jumbo = 1;
  421. plat->force_sf_dma_mode = 1;
  422. }
  423. if (of_device_is_compatible(np, "snps,dwxgmac")) {
  424. plat->has_xgmac = 1;
  425. plat->pmt = 1;
  426. plat->tso_en = of_property_read_bool(np, "snps,tso");
  427. }
  428. dma_cfg = devm_kzalloc(&pdev->dev, sizeof(*dma_cfg),
  429. GFP_KERNEL);
  430. if (!dma_cfg) {
  431. stmmac_remove_config_dt(pdev, plat);
  432. return ERR_PTR(-ENOMEM);
  433. }
  434. plat->dma_cfg = dma_cfg;
  435. of_property_read_u32(np, "snps,pbl", &dma_cfg->pbl);
  436. if (!dma_cfg->pbl)
  437. dma_cfg->pbl = DEFAULT_DMA_PBL;
  438. of_property_read_u32(np, "snps,txpbl", &dma_cfg->txpbl);
  439. of_property_read_u32(np, "snps,rxpbl", &dma_cfg->rxpbl);
  440. dma_cfg->pblx8 = !of_property_read_bool(np, "snps,no-pbl-x8");
  441. dma_cfg->aal = of_property_read_bool(np, "snps,aal");
  442. dma_cfg->fixed_burst = of_property_read_bool(np, "snps,fixed-burst");
  443. dma_cfg->mixed_burst = of_property_read_bool(np, "snps,mixed-burst");
  444. plat->force_thresh_dma_mode = of_property_read_bool(np, "snps,force_thresh_dma_mode");
  445. if (plat->force_thresh_dma_mode) {
  446. plat->force_sf_dma_mode = 0;
  447. pr_warn("force_sf_dma_mode is ignored if force_thresh_dma_mode is set.");
  448. }
  449. of_property_read_u32(np, "snps,ps-speed", &plat->mac_port_sel_speed);
  450. plat->axi = stmmac_axi_setup(pdev);
  451. rc = stmmac_mtl_setup(pdev, plat);
  452. if (rc) {
  453. stmmac_remove_config_dt(pdev, plat);
  454. return ERR_PTR(rc);
  455. }
  456. /* clock setup */
  457. plat->stmmac_clk = devm_clk_get(&pdev->dev,
  458. STMMAC_RESOURCE_NAME);
  459. if (IS_ERR(plat->stmmac_clk)) {
  460. dev_warn(&pdev->dev, "Cannot get CSR clock\n");
  461. plat->stmmac_clk = NULL;
  462. }
  463. clk_prepare_enable(plat->stmmac_clk);
  464. plat->pclk = devm_clk_get(&pdev->dev, "pclk");
  465. if (IS_ERR(plat->pclk)) {
  466. if (PTR_ERR(plat->pclk) == -EPROBE_DEFER)
  467. goto error_pclk_get;
  468. plat->pclk = NULL;
  469. }
  470. clk_prepare_enable(plat->pclk);
  471. /* Fall-back to main clock in case of no PTP ref is passed */
  472. plat->clk_ptp_ref = devm_clk_get(&pdev->dev, "ptp_ref");
  473. if (IS_ERR(plat->clk_ptp_ref)) {
  474. plat->clk_ptp_rate = clk_get_rate(plat->stmmac_clk);
  475. plat->clk_ptp_ref = NULL;
  476. dev_warn(&pdev->dev, "PTP uses main clock\n");
  477. } else {
  478. plat->clk_ptp_rate = clk_get_rate(plat->clk_ptp_ref);
  479. dev_dbg(&pdev->dev, "PTP rate %d\n", plat->clk_ptp_rate);
  480. }
  481. plat->stmmac_rst = devm_reset_control_get(&pdev->dev,
  482. STMMAC_RESOURCE_NAME);
  483. if (IS_ERR(plat->stmmac_rst)) {
  484. if (PTR_ERR(plat->stmmac_rst) == -EPROBE_DEFER)
  485. goto error_hw_init;
  486. dev_info(&pdev->dev, "no reset control found\n");
  487. plat->stmmac_rst = NULL;
  488. }
  489. return plat;
  490. error_hw_init:
  491. clk_disable_unprepare(plat->pclk);
  492. error_pclk_get:
  493. clk_disable_unprepare(plat->stmmac_clk);
  494. return ERR_PTR(-EPROBE_DEFER);
  495. }
  496. /**
  497. * stmmac_remove_config_dt - undo the effects of stmmac_probe_config_dt()
  498. * @pdev: platform_device structure
  499. * @plat: driver data platform structure
  500. *
  501. * Release resources claimed by stmmac_probe_config_dt().
  502. */
  503. void stmmac_remove_config_dt(struct platform_device *pdev,
  504. struct plat_stmmacenet_data *plat)
  505. {
  506. struct device_node *np = pdev->dev.of_node;
  507. if (of_phy_is_fixed_link(np))
  508. of_phy_deregister_fixed_link(np);
  509. of_node_put(plat->phy_node);
  510. of_node_put(plat->mdio_node);
  511. }
  512. #else
  513. struct plat_stmmacenet_data *
  514. stmmac_probe_config_dt(struct platform_device *pdev, const char **mac)
  515. {
  516. return ERR_PTR(-EINVAL);
  517. }
  518. void stmmac_remove_config_dt(struct platform_device *pdev,
  519. struct plat_stmmacenet_data *plat)
  520. {
  521. }
  522. #endif /* CONFIG_OF */
  523. EXPORT_SYMBOL_GPL(stmmac_probe_config_dt);
  524. EXPORT_SYMBOL_GPL(stmmac_remove_config_dt);
  525. #ifdef CONFIG_ACPI
  526. /*
  527. * Parse ACPI _DSD to setup AXI register
  528. */
  529. static struct stmmac_axi * stmmac_axi_setup_acpi(struct platform_device *pdev)
  530. {
  531. struct fwnode_handle *np = dev_fwnode(&(pdev->dev));
  532. struct stmmac_axi * axi;
  533. axi = devm_kzalloc(&pdev->dev, sizeof(*axi), GFP_KERNEL);
  534. if (!axi)
  535. return ERR_PTR(-ENOMEM);
  536. axi->axi_lpi_en = fwnode_property_read_bool(np, "snps,lpi_en");
  537. axi->axi_xit_frm = fwnode_property_read_bool(np, "snps,xit_frm");
  538. axi->axi_kbbe = fwnode_property_read_bool(np, "snps,axi_kbbe");
  539. axi->axi_fb = fwnode_property_read_bool(np, "snps,axi_fb");
  540. axi->axi_mb = fwnode_property_read_bool(np, "snps,axi_mb");
  541. axi->axi_rb = fwnode_property_read_bool(np, "snps,axi_rb");
  542. if (fwnode_property_read_u32(np, "snps,wr_osr_lmt", &axi->axi_wr_osr_lmt))
  543. axi->axi_wr_osr_lmt = 1;
  544. if (fwnode_property_read_u32(np, "snps,rd_osr_lmt", &axi->axi_rd_osr_lmt))
  545. axi->axi_rd_osr_lmt = 1;
  546. fwnode_property_read_u32_array(np, "snps,blen", axi->axi_blen, AXI_BLEN);
  547. return axi;
  548. }
  549. /**
  550. * Parse ACPI _DSD parameters for multiple queues configuration
  551. */
  552. static void stmmac_mtl_setup_acpi(struct platform_device *pdev,
  553. struct plat_stmmacenet_data *plat)
  554. {
  555. plat->rx_queues_to_use = 1;
  556. plat->tx_queues_to_use = 1;
  557. /**
  558. * First Queue must always be in DCB mode. As MTL_QUEUE_DCB=1 we need
  559. * to always set this, otherwise Queue will be classified as AVB
  560. * (because MTL_QUEUE_AVB = 0).
  561. */
  562. plat->rx_queues_cfg[0].mode_to_use = MTL_QUEUE_DCB;
  563. plat->tx_queues_cfg[0].mode_to_use = MTL_QUEUE_DCB;
  564. plat->rx_queues_cfg[0].use_prio = true;
  565. plat->rx_queues_cfg[0].pkt_route = 0x0;
  566. plat->rx_sched_algorithm = MTL_RX_ALGORITHM_SP;
  567. plat->tx_sched_algorithm = MTL_TX_ALGORITHM_SP;
  568. plat->tx_queues_cfg[0].use_prio = true;
  569. }
  570. static int stmmac_acpi_phy(struct plat_stmmacenet_data *plat,
  571. struct fwnode_handle *np, struct device *dev)
  572. {
  573. plat->mdio_bus_data = devm_kzalloc(dev,
  574. sizeof(struct stmmac_mdio_bus_data),
  575. GFP_KERNEL);
  576. return 0;
  577. }
  578. int fw_get_phy_mode(struct fwnode_handle *np)
  579. {
  580. const char *pm;
  581. int err, i;
  582. err = fwnode_property_read_string(np, "phy-mode", &pm);
  583. if (err < 0)
  584. err = fwnode_property_read_string(np, "phy-connection-mode", &pm);
  585. if (err < 0)
  586. return err;
  587. for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++) {
  588. if (!strcasecmp(pm, phy_modes(i)))
  589. return i;
  590. }
  591. return -ENODEV;
  592. }
  593. int stmmac_acpi_clock_setup(struct plat_stmmacenet_data *plat,
  594. struct platform_device *pdev)
  595. {
  596. struct fwnode_handle *np = dev_fwnode(&(pdev->dev));
  597. struct device * dev = &pdev->dev;
  598. struct clk *clk = ERR_PTR(-ENODEV);
  599. u64 clk_freq = 0;
  600. int err;
  601. err = fwnode_property_read_u64(np, "clock-frequency", &clk_freq);
  602. if (err < 0)
  603. clk_freq = 125000000; /* default to 125MHz */
  604. plat->stmmac_clk = devm_clk_get(dev, dev_name(dev));
  605. if (IS_ERR(plat->stmmac_clk)) {
  606. clk = clk_register_fixed_rate(dev, dev_name(dev), NULL, 0, clk_freq);
  607. if (IS_ERR(clk))
  608. return -1;
  609. if (clk_register_clkdev(clk, dev_name(dev), dev_name(dev)))
  610. return -1;
  611. plat->stmmac_clk = clk;
  612. }
  613. clk_prepare_enable(plat->stmmac_clk);
  614. plat->pclk = devm_clk_get(dev, "pclk");
  615. if (IS_ERR(plat->pclk))
  616. plat->pclk = NULL;
  617. clk_prepare_enable(plat->pclk);
  618. plat->clk_ptp_ref = devm_clk_get(dev, "ptp_ref");
  619. if (IS_ERR(plat->clk_ptp_ref)) {
  620. plat->clk_ptp_rate = clk_get_rate(plat->stmmac_clk);
  621. plat->clk_ptp_ref = NULL;
  622. }
  623. plat->stmmac_rst = devm_reset_control_get(dev,STMMAC_RESOURCE_NAME);
  624. if (IS_ERR(plat->stmmac_rst)) {
  625. dev_info(dev, "no reset control found\n");
  626. plat->stmmac_rst = NULL;
  627. }
  628. return 0;
  629. }
  630. /**
  631. * Parse ACPI driver parameters
  632. */
  633. struct plat_stmmacenet_data *
  634. stmmac_probe_config_acpi(struct platform_device *pdev, const char **mac)
  635. {
  636. struct fwnode_handle *np;
  637. struct plat_stmmacenet_data *plat;
  638. struct stmmac_dma_cfg *dma_cfg;
  639. plat = devm_kzalloc(&pdev->dev, sizeof(*plat), GFP_KERNEL);
  640. if (!plat)
  641. return ERR_PTR(-ENOMEM);
  642. np = dev_fwnode(&(pdev->dev));
  643. plat->interface = fw_get_phy_mode(np);
  644. /* Get max speed of operation from device tree */
  645. if (fwnode_property_read_u32(np, "max-speed", &plat->max_speed))
  646. plat->max_speed = -1;
  647. if (fwnode_property_read_u32(np, "bus_id", &plat->bus_id))
  648. plat->bus_id = 2;
  649. /* Default to PHY auto-detection */
  650. plat->phy_addr = -1;
  651. /* "snps,phy-addr" is not a standard property. Mark it as deprecated
  652. * and warn of its use. Remove this when PHY node support is added.
  653. */
  654. if (fwnode_property_read_u32(np, "snps,phy-addr", &plat->phy_addr) == 0)
  655. dev_warn(&pdev->dev, "snps,phy-addr property is deprecated\n");
  656. if (stmmac_acpi_phy(plat, np, &pdev->dev))
  657. return ERR_PTR(-ENODEV);
  658. fwnode_property_read_u32(np, "tx-fifo-depth", &plat->tx_fifo_size);
  659. fwnode_property_read_u32(np, "rx-fifo-depth", &plat->rx_fifo_size);
  660. if (plat->tx_fifo_size == 0)
  661. plat->tx_fifo_size = 0x10000;
  662. if (plat->rx_fifo_size == 0)
  663. plat->rx_fifo_size = 0x10000;
  664. plat->force_sf_dma_mode =
  665. fwnode_property_read_bool(np, "snps,force_sf_dma_mode");
  666. plat->en_tx_lpi_clockgating =
  667. fwnode_property_read_bool(np, "snps,en-tx-lpi-clockgating");
  668. /* Set the maxmtu to a default of JUMBO_LEN in case the
  669. * parameter is not present.
  670. */
  671. plat->maxmtu = JUMBO_LEN;
  672. /* Set default value for multicast hash bins */
  673. plat->multicast_filter_bins = HASH_TABLE_SIZE;
  674. /* Set default value for unicast filter entries */
  675. plat->unicast_filter_entries = 1;
  676. /* Only to "snps,dwmac" */
  677. fwnode_property_read_u32(np, "max-frame-size", &plat->maxmtu);
  678. fwnode_property_read_u32(np, "snps,multicast-filter-bins",
  679. &plat->multicast_filter_bins);
  680. fwnode_property_read_u32(np, "snps,perfect-filter-entries",
  681. &plat->unicast_filter_entries);
  682. plat->unicast_filter_entries = dwmac1000_validate_ucast_entries(
  683. plat->unicast_filter_entries);
  684. plat->multicast_filter_bins = dwmac1000_validate_mcast_bins(
  685. plat->multicast_filter_bins);
  686. plat->has_gmac = 1;
  687. plat->pmt = 1;
  688. dma_cfg = devm_kzalloc(&pdev->dev, sizeof(*dma_cfg), GFP_KERNEL);
  689. if (!dma_cfg)
  690. return ERR_PTR(-ENOMEM);
  691. plat->dma_cfg = dma_cfg;
  692. fwnode_property_read_u32(np, "snps,pbl", &dma_cfg->pbl);
  693. if (!dma_cfg->pbl)
  694. dma_cfg->pbl = DEFAULT_DMA_PBL;
  695. fwnode_property_read_u32(np, "snps,txpbl", &dma_cfg->txpbl);
  696. fwnode_property_read_u32(np, "snps,rxpbl", &dma_cfg->rxpbl);
  697. dma_cfg->pblx8 = !fwnode_property_read_bool(np, "snps,no-pbl-x8");
  698. dma_cfg->aal = fwnode_property_read_bool(np, "snps,aal");
  699. dma_cfg->fixed_burst = fwnode_property_read_bool(np, "snps,fixed-burst");
  700. dma_cfg->mixed_burst = fwnode_property_read_bool(np, "snps,mixed-burst");
  701. plat->force_thresh_dma_mode = fwnode_property_read_bool(np, "snps,force_thresh_dma_mode");
  702. if (plat->force_thresh_dma_mode)
  703. plat->force_sf_dma_mode = 0;
  704. fwnode_property_read_u32(np, "snps,ps-speed", &plat->mac_port_sel_speed);
  705. plat->axi = stmmac_axi_setup_acpi(pdev);
  706. stmmac_mtl_setup_acpi(pdev, plat);
  707. stmmac_acpi_clock_setup(plat,pdev);
  708. return plat;
  709. }
  710. #else
  711. struct plat_stmmacenet_data *
  712. stmmac_probe_config_acpi(struct platform_device *pdev, const char **mac)
  713. {
  714. return ERR_PTR(-EINVAL);
  715. }
  716. #endif /* CONFIG_ACPI */
  717. EXPORT_SYMBOL_GPL(stmmac_probe_config_acpi);
  718. int stmmac_get_platform_resources(struct platform_device *pdev,
  719. struct stmmac_resources *stmmac_res)
  720. {
  721. struct resource *res;
  722. memset(stmmac_res, 0, sizeof(*stmmac_res));
  723. /* Get IRQ information early to have an ability to ask for deferred
  724. * probe if needed before we went too far with resource allocation.
  725. */
  726. if (pdev->dev.of_node) {
  727. stmmac_res->irq = platform_get_irq_byname(pdev, "macirq");
  728. if (stmmac_res->irq < 0) {
  729. if (stmmac_res->irq != -EPROBE_DEFER) {
  730. dev_err(&pdev->dev,
  731. "MAC IRQ configuration information not found\n");
  732. }
  733. return stmmac_res->irq;
  734. }
  735. /* On some platforms e.g. SPEAr the wake up irq differs from the mac irq
  736. * The external wake up irq can be passed through the platform code
  737. * named as "eth_wake_irq"
  738. *
  739. * In case the wake up interrupt is not passed from the platform
  740. * so the driver will continue to use the mac irq (ndev->irq)
  741. */
  742. stmmac_res->wol_irq = platform_get_irq_byname(pdev, "eth_wake_irq");
  743. if (stmmac_res->wol_irq < 0) {
  744. if (stmmac_res->wol_irq == -EPROBE_DEFER)
  745. return -EPROBE_DEFER;
  746. stmmac_res->wol_irq = stmmac_res->irq;
  747. }
  748. stmmac_res->lpi_irq = platform_get_irq_byname(pdev, "eth_lpi");
  749. if (stmmac_res->lpi_irq == -EPROBE_DEFER)
  750. return -EPROBE_DEFER;
  751. } else if (has_acpi_companion(&pdev->dev)) {
  752. stmmac_res->irq = platform_get_irq(pdev, 0);
  753. if (stmmac_res->irq < 0)
  754. dev_err(&pdev->dev,
  755. "MAC IRQ configuration information not found\n");
  756. stmmac_res->wol_irq = stmmac_res->irq;
  757. stmmac_res->lpi_irq = -1;
  758. }
  759. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  760. stmmac_res->addr = devm_ioremap_resource(&pdev->dev, res);
  761. return PTR_ERR_OR_ZERO(stmmac_res->addr);
  762. }
  763. EXPORT_SYMBOL_GPL(stmmac_get_platform_resources);
  764. /**
  765. * stmmac_pltfr_remove
  766. * @pdev: platform device pointer
  767. * Description: this function calls the main to free the net resources
  768. * and calls the platforms hook and release the resources (e.g. mem).
  769. */
  770. int stmmac_pltfr_remove(struct platform_device *pdev)
  771. {
  772. struct net_device *ndev = platform_get_drvdata(pdev);
  773. struct stmmac_priv *priv = netdev_priv(ndev);
  774. struct plat_stmmacenet_data *plat = priv->plat;
  775. int ret = stmmac_dvr_remove(&pdev->dev);
  776. if (plat->exit)
  777. plat->exit(pdev, plat->bsp_priv);
  778. stmmac_remove_config_dt(pdev, plat);
  779. return ret;
  780. }
  781. EXPORT_SYMBOL_GPL(stmmac_pltfr_remove);
  782. #ifdef CONFIG_PM_SLEEP
  783. /**
  784. * stmmac_pltfr_suspend
  785. * @dev: device pointer
  786. * Description: this function is invoked when suspend the driver and it direcly
  787. * call the main suspend function and then, if required, on some platform, it
  788. * can call an exit helper.
  789. */
  790. static int stmmac_pltfr_suspend(struct device *dev)
  791. {
  792. int ret;
  793. struct net_device *ndev = dev_get_drvdata(dev);
  794. struct stmmac_priv *priv = netdev_priv(ndev);
  795. struct platform_device *pdev = to_platform_device(dev);
  796. ret = stmmac_suspend(dev);
  797. if (priv->plat->exit)
  798. priv->plat->exit(pdev, priv->plat->bsp_priv);
  799. return ret;
  800. }
  801. /**
  802. * stmmac_pltfr_resume
  803. * @dev: device pointer
  804. * Description: this function is invoked when resume the driver before calling
  805. * the main resume function, on some platforms, it can call own init helper
  806. * if required.
  807. */
  808. static int stmmac_pltfr_resume(struct device *dev)
  809. {
  810. struct net_device *ndev = dev_get_drvdata(dev);
  811. struct stmmac_priv *priv = netdev_priv(ndev);
  812. struct platform_device *pdev = to_platform_device(dev);
  813. if (priv->plat->init)
  814. priv->plat->init(pdev, priv->plat->bsp_priv);
  815. return stmmac_resume(dev);
  816. }
  817. #endif /* CONFIG_PM_SLEEP */
  818. SIMPLE_DEV_PM_OPS(stmmac_pltfr_pm_ops, stmmac_pltfr_suspend,
  819. stmmac_pltfr_resume);
  820. EXPORT_SYMBOL_GPL(stmmac_pltfr_pm_ops);
  821. MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet platform support");
  822. MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
  823. MODULE_LICENSE("GPL");