dwmac-dwc-qos-eth.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /*
  2. * Synopsys DWC Ethernet Quality-of-Service v4.10a linux driver
  3. *
  4. * Copyright (C) 2016 Joao Pinto <jpinto@synopsys.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * You should have received a copy of the GNU General Public License
  11. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/clk-provider.h>
  15. #include <linux/device.h>
  16. #include <linux/gpio/consumer.h>
  17. #include <linux/ethtool.h>
  18. #include <linux/io.h>
  19. #include <linux/iopoll.h>
  20. #include <linux/ioport.h>
  21. #include <linux/module.h>
  22. #include <linux/of_device.h>
  23. #include <linux/of_net.h>
  24. #include <linux/mfd/syscon.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/reset.h>
  27. #include <linux/stmmac.h>
  28. #include "stmmac_platform.h"
  29. #include "dwmac4.h"
  30. struct tegra_eqos {
  31. struct device *dev;
  32. void __iomem *regs;
  33. struct reset_control *rst;
  34. struct clk *clk_master;
  35. struct clk *clk_slave;
  36. struct clk *clk_tx;
  37. struct clk *clk_rx;
  38. struct gpio_desc *reset;
  39. };
  40. static int dwc_eth_dwmac_config_dt(struct platform_device *pdev,
  41. struct plat_stmmacenet_data *plat_dat)
  42. {
  43. struct device_node *np = pdev->dev.of_node;
  44. u32 burst_map = 0;
  45. u32 bit_index = 0;
  46. u32 a_index = 0;
  47. if (!plat_dat->axi) {
  48. plat_dat->axi = kzalloc(sizeof(struct stmmac_axi), GFP_KERNEL);
  49. if (!plat_dat->axi)
  50. return -ENOMEM;
  51. }
  52. plat_dat->axi->axi_lpi_en = of_property_read_bool(np, "snps,en-lpi");
  53. if (of_property_read_u32(np, "snps,write-requests",
  54. &plat_dat->axi->axi_wr_osr_lmt)) {
  55. /**
  56. * Since the register has a reset value of 1, if property
  57. * is missing, default to 1.
  58. */
  59. plat_dat->axi->axi_wr_osr_lmt = 1;
  60. } else {
  61. /**
  62. * If property exists, to keep the behavior from dwc_eth_qos,
  63. * subtract one after parsing.
  64. */
  65. plat_dat->axi->axi_wr_osr_lmt--;
  66. }
  67. if (of_property_read_u32(np, "snps,read-requests",
  68. &plat_dat->axi->axi_rd_osr_lmt)) {
  69. /**
  70. * Since the register has a reset value of 1, if property
  71. * is missing, default to 1.
  72. */
  73. plat_dat->axi->axi_rd_osr_lmt = 1;
  74. } else {
  75. /**
  76. * If property exists, to keep the behavior from dwc_eth_qos,
  77. * subtract one after parsing.
  78. */
  79. plat_dat->axi->axi_rd_osr_lmt--;
  80. }
  81. of_property_read_u32(np, "snps,burst-map", &burst_map);
  82. /* converts burst-map bitmask to burst array */
  83. for (bit_index = 0; bit_index < 7; bit_index++) {
  84. if (burst_map & (1 << bit_index)) {
  85. switch (bit_index) {
  86. case 0:
  87. plat_dat->axi->axi_blen[a_index] = 4; break;
  88. case 1:
  89. plat_dat->axi->axi_blen[a_index] = 8; break;
  90. case 2:
  91. plat_dat->axi->axi_blen[a_index] = 16; break;
  92. case 3:
  93. plat_dat->axi->axi_blen[a_index] = 32; break;
  94. case 4:
  95. plat_dat->axi->axi_blen[a_index] = 64; break;
  96. case 5:
  97. plat_dat->axi->axi_blen[a_index] = 128; break;
  98. case 6:
  99. plat_dat->axi->axi_blen[a_index] = 256; break;
  100. default:
  101. break;
  102. }
  103. a_index++;
  104. }
  105. }
  106. /* dwc-qos needs GMAC4, AAL, TSO and PMT */
  107. plat_dat->has_gmac4 = 1;
  108. plat_dat->dma_cfg->aal = 1;
  109. plat_dat->tso_en = 1;
  110. plat_dat->pmt = 1;
  111. return 0;
  112. }
  113. static void *dwc_qos_probe(struct platform_device *pdev,
  114. struct plat_stmmacenet_data *plat_dat,
  115. struct stmmac_resources *stmmac_res)
  116. {
  117. int err;
  118. plat_dat->stmmac_clk = devm_clk_get(&pdev->dev, "apb_pclk");
  119. if (IS_ERR(plat_dat->stmmac_clk)) {
  120. dev_err(&pdev->dev, "apb_pclk clock not found.\n");
  121. return ERR_CAST(plat_dat->stmmac_clk);
  122. }
  123. err = clk_prepare_enable(plat_dat->stmmac_clk);
  124. if (err < 0) {
  125. dev_err(&pdev->dev, "failed to enable apb_pclk clock: %d\n",
  126. err);
  127. return ERR_PTR(err);
  128. }
  129. plat_dat->pclk = devm_clk_get(&pdev->dev, "phy_ref_clk");
  130. if (IS_ERR(plat_dat->pclk)) {
  131. dev_err(&pdev->dev, "phy_ref_clk clock not found.\n");
  132. err = PTR_ERR(plat_dat->pclk);
  133. goto disable;
  134. }
  135. err = clk_prepare_enable(plat_dat->pclk);
  136. if (err < 0) {
  137. dev_err(&pdev->dev, "failed to enable phy_ref clock: %d\n",
  138. err);
  139. goto disable;
  140. }
  141. return NULL;
  142. disable:
  143. clk_disable_unprepare(plat_dat->stmmac_clk);
  144. return ERR_PTR(err);
  145. }
  146. static int dwc_qos_remove(struct platform_device *pdev)
  147. {
  148. struct net_device *ndev = platform_get_drvdata(pdev);
  149. struct stmmac_priv *priv = netdev_priv(ndev);
  150. clk_disable_unprepare(priv->plat->pclk);
  151. clk_disable_unprepare(priv->plat->stmmac_clk);
  152. return 0;
  153. }
  154. #define SDMEMCOMPPADCTRL 0x8800
  155. #define SDMEMCOMPPADCTRL_PAD_E_INPUT_OR_E_PWRD BIT(31)
  156. #define AUTO_CAL_CONFIG 0x8804
  157. #define AUTO_CAL_CONFIG_START BIT(31)
  158. #define AUTO_CAL_CONFIG_ENABLE BIT(29)
  159. #define AUTO_CAL_STATUS 0x880c
  160. #define AUTO_CAL_STATUS_ACTIVE BIT(31)
  161. static void tegra_eqos_fix_speed(void *priv, unsigned int speed)
  162. {
  163. struct tegra_eqos *eqos = priv;
  164. unsigned long rate = 125000000;
  165. bool needs_calibration = false;
  166. u32 value;
  167. int err;
  168. switch (speed) {
  169. case SPEED_1000:
  170. needs_calibration = true;
  171. rate = 125000000;
  172. break;
  173. case SPEED_100:
  174. needs_calibration = true;
  175. rate = 25000000;
  176. break;
  177. case SPEED_10:
  178. rate = 2500000;
  179. break;
  180. default:
  181. dev_err(eqos->dev, "invalid speed %u\n", speed);
  182. break;
  183. }
  184. if (needs_calibration) {
  185. /* calibrate */
  186. value = readl(eqos->regs + SDMEMCOMPPADCTRL);
  187. value |= SDMEMCOMPPADCTRL_PAD_E_INPUT_OR_E_PWRD;
  188. writel(value, eqos->regs + SDMEMCOMPPADCTRL);
  189. udelay(1);
  190. value = readl(eqos->regs + AUTO_CAL_CONFIG);
  191. value |= AUTO_CAL_CONFIG_START | AUTO_CAL_CONFIG_ENABLE;
  192. writel(value, eqos->regs + AUTO_CAL_CONFIG);
  193. err = readl_poll_timeout_atomic(eqos->regs + AUTO_CAL_STATUS,
  194. value,
  195. value & AUTO_CAL_STATUS_ACTIVE,
  196. 1, 10);
  197. if (err < 0) {
  198. dev_err(eqos->dev, "calibration did not start\n");
  199. goto failed;
  200. }
  201. err = readl_poll_timeout_atomic(eqos->regs + AUTO_CAL_STATUS,
  202. value,
  203. (value & AUTO_CAL_STATUS_ACTIVE) == 0,
  204. 20, 200);
  205. if (err < 0) {
  206. dev_err(eqos->dev, "calibration didn't finish\n");
  207. goto failed;
  208. }
  209. failed:
  210. value = readl(eqos->regs + SDMEMCOMPPADCTRL);
  211. value &= ~SDMEMCOMPPADCTRL_PAD_E_INPUT_OR_E_PWRD;
  212. writel(value, eqos->regs + SDMEMCOMPPADCTRL);
  213. } else {
  214. value = readl(eqos->regs + AUTO_CAL_CONFIG);
  215. value &= ~AUTO_CAL_CONFIG_ENABLE;
  216. writel(value, eqos->regs + AUTO_CAL_CONFIG);
  217. }
  218. err = clk_set_rate(eqos->clk_tx, rate);
  219. if (err < 0)
  220. dev_err(eqos->dev, "failed to set TX rate: %d\n", err);
  221. }
  222. static int tegra_eqos_init(struct platform_device *pdev, void *priv)
  223. {
  224. struct tegra_eqos *eqos = priv;
  225. unsigned long rate;
  226. u32 value;
  227. rate = clk_get_rate(eqos->clk_slave);
  228. value = (rate / 1000000) - 1;
  229. writel(value, eqos->regs + GMAC_1US_TIC_COUNTER);
  230. return 0;
  231. }
  232. static void *tegra_eqos_probe(struct platform_device *pdev,
  233. struct plat_stmmacenet_data *data,
  234. struct stmmac_resources *res)
  235. {
  236. struct tegra_eqos *eqos;
  237. int err;
  238. eqos = devm_kzalloc(&pdev->dev, sizeof(*eqos), GFP_KERNEL);
  239. if (!eqos) {
  240. err = -ENOMEM;
  241. goto error;
  242. }
  243. eqos->dev = &pdev->dev;
  244. eqos->regs = res->addr;
  245. eqos->clk_master = devm_clk_get(&pdev->dev, "master_bus");
  246. if (IS_ERR(eqos->clk_master)) {
  247. err = PTR_ERR(eqos->clk_master);
  248. goto error;
  249. }
  250. err = clk_prepare_enable(eqos->clk_master);
  251. if (err < 0)
  252. goto error;
  253. eqos->clk_slave = devm_clk_get(&pdev->dev, "slave_bus");
  254. if (IS_ERR(eqos->clk_slave)) {
  255. err = PTR_ERR(eqos->clk_slave);
  256. goto disable_master;
  257. }
  258. data->stmmac_clk = eqos->clk_slave;
  259. err = clk_prepare_enable(eqos->clk_slave);
  260. if (err < 0)
  261. goto disable_master;
  262. eqos->clk_rx = devm_clk_get(&pdev->dev, "rx");
  263. if (IS_ERR(eqos->clk_rx)) {
  264. err = PTR_ERR(eqos->clk_rx);
  265. goto disable_slave;
  266. }
  267. err = clk_prepare_enable(eqos->clk_rx);
  268. if (err < 0)
  269. goto disable_slave;
  270. eqos->clk_tx = devm_clk_get(&pdev->dev, "tx");
  271. if (IS_ERR(eqos->clk_tx)) {
  272. err = PTR_ERR(eqos->clk_tx);
  273. goto disable_rx;
  274. }
  275. err = clk_prepare_enable(eqos->clk_tx);
  276. if (err < 0)
  277. goto disable_rx;
  278. eqos->reset = devm_gpiod_get(&pdev->dev, "phy-reset", GPIOD_OUT_HIGH);
  279. if (IS_ERR(eqos->reset)) {
  280. err = PTR_ERR(eqos->reset);
  281. goto disable_tx;
  282. }
  283. usleep_range(2000, 4000);
  284. gpiod_set_value(eqos->reset, 0);
  285. eqos->rst = devm_reset_control_get(&pdev->dev, "eqos");
  286. if (IS_ERR(eqos->rst)) {
  287. err = PTR_ERR(eqos->rst);
  288. goto reset_phy;
  289. }
  290. err = reset_control_assert(eqos->rst);
  291. if (err < 0)
  292. goto reset_phy;
  293. usleep_range(2000, 4000);
  294. err = reset_control_deassert(eqos->rst);
  295. if (err < 0)
  296. goto reset_phy;
  297. usleep_range(2000, 4000);
  298. data->fix_mac_speed = tegra_eqos_fix_speed;
  299. data->init = tegra_eqos_init;
  300. data->bsp_priv = eqos;
  301. err = tegra_eqos_init(pdev, eqos);
  302. if (err < 0)
  303. goto reset;
  304. out:
  305. return eqos;
  306. reset:
  307. reset_control_assert(eqos->rst);
  308. reset_phy:
  309. gpiod_set_value(eqos->reset, 1);
  310. disable_tx:
  311. clk_disable_unprepare(eqos->clk_tx);
  312. disable_rx:
  313. clk_disable_unprepare(eqos->clk_rx);
  314. disable_slave:
  315. clk_disable_unprepare(eqos->clk_slave);
  316. disable_master:
  317. clk_disable_unprepare(eqos->clk_master);
  318. error:
  319. eqos = ERR_PTR(err);
  320. goto out;
  321. }
  322. static int tegra_eqos_remove(struct platform_device *pdev)
  323. {
  324. struct tegra_eqos *eqos = get_stmmac_bsp_priv(&pdev->dev);
  325. reset_control_assert(eqos->rst);
  326. gpiod_set_value(eqos->reset, 1);
  327. clk_disable_unprepare(eqos->clk_tx);
  328. clk_disable_unprepare(eqos->clk_rx);
  329. clk_disable_unprepare(eqos->clk_slave);
  330. clk_disable_unprepare(eqos->clk_master);
  331. return 0;
  332. }
  333. struct dwc_eth_dwmac_data {
  334. void *(*probe)(struct platform_device *pdev,
  335. struct plat_stmmacenet_data *data,
  336. struct stmmac_resources *res);
  337. int (*remove)(struct platform_device *pdev);
  338. };
  339. static const struct dwc_eth_dwmac_data dwc_qos_data = {
  340. .probe = dwc_qos_probe,
  341. .remove = dwc_qos_remove,
  342. };
  343. static const struct dwc_eth_dwmac_data tegra_eqos_data = {
  344. .probe = tegra_eqos_probe,
  345. .remove = tegra_eqos_remove,
  346. };
  347. static int dwc_eth_dwmac_probe(struct platform_device *pdev)
  348. {
  349. const struct dwc_eth_dwmac_data *data;
  350. struct plat_stmmacenet_data *plat_dat;
  351. struct stmmac_resources stmmac_res;
  352. struct resource *res;
  353. void *priv;
  354. int ret;
  355. data = of_device_get_match_data(&pdev->dev);
  356. memset(&stmmac_res, 0, sizeof(struct stmmac_resources));
  357. /**
  358. * Since stmmac_platform supports name IRQ only, basic platform
  359. * resource initialization is done in the glue logic.
  360. */
  361. stmmac_res.irq = platform_get_irq(pdev, 0);
  362. if (stmmac_res.irq < 0) {
  363. if (stmmac_res.irq != -EPROBE_DEFER)
  364. dev_err(&pdev->dev,
  365. "IRQ configuration information not found\n");
  366. return stmmac_res.irq;
  367. }
  368. stmmac_res.wol_irq = stmmac_res.irq;
  369. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  370. stmmac_res.addr = devm_ioremap_resource(&pdev->dev, res);
  371. if (IS_ERR(stmmac_res.addr))
  372. return PTR_ERR(stmmac_res.addr);
  373. plat_dat = stmmac_probe_config_dt(pdev, &stmmac_res.mac);
  374. if (IS_ERR(plat_dat))
  375. return PTR_ERR(plat_dat);
  376. priv = data->probe(pdev, plat_dat, &stmmac_res);
  377. if (IS_ERR(priv)) {
  378. ret = PTR_ERR(priv);
  379. dev_err(&pdev->dev, "failed to probe subdriver: %d\n", ret);
  380. goto remove_config;
  381. }
  382. ret = dwc_eth_dwmac_config_dt(pdev, plat_dat);
  383. if (ret)
  384. goto remove;
  385. ret = stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res);
  386. if (ret)
  387. goto remove;
  388. return ret;
  389. remove:
  390. data->remove(pdev);
  391. remove_config:
  392. stmmac_remove_config_dt(pdev, plat_dat);
  393. return ret;
  394. }
  395. static int dwc_eth_dwmac_remove(struct platform_device *pdev)
  396. {
  397. struct net_device *ndev = platform_get_drvdata(pdev);
  398. struct stmmac_priv *priv = netdev_priv(ndev);
  399. const struct dwc_eth_dwmac_data *data;
  400. int err;
  401. data = of_device_get_match_data(&pdev->dev);
  402. err = stmmac_dvr_remove(&pdev->dev);
  403. if (err < 0)
  404. dev_err(&pdev->dev, "failed to remove platform: %d\n", err);
  405. err = data->remove(pdev);
  406. if (err < 0)
  407. dev_err(&pdev->dev, "failed to remove subdriver: %d\n", err);
  408. stmmac_remove_config_dt(pdev, priv->plat);
  409. return err;
  410. }
  411. static const struct of_device_id dwc_eth_dwmac_match[] = {
  412. { .compatible = "snps,dwc-qos-ethernet-4.10", .data = &dwc_qos_data },
  413. { .compatible = "nvidia,tegra186-eqos", .data = &tegra_eqos_data },
  414. { }
  415. };
  416. MODULE_DEVICE_TABLE(of, dwc_eth_dwmac_match);
  417. static struct platform_driver dwc_eth_dwmac_driver = {
  418. .probe = dwc_eth_dwmac_probe,
  419. .remove = dwc_eth_dwmac_remove,
  420. .driver = {
  421. .name = "dwc-eth-dwmac",
  422. .pm = &stmmac_pltfr_pm_ops,
  423. .of_match_table = dwc_eth_dwmac_match,
  424. },
  425. };
  426. module_platform_driver(dwc_eth_dwmac_driver);
  427. MODULE_AUTHOR("Joao Pinto <jpinto@synopsys.com>");
  428. MODULE_DESCRIPTION("Synopsys DWC Ethernet Quality-of-Service v4.10a driver");
  429. MODULE_LICENSE("GPL v2");