sxgbe_platform.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /* 10G controller driver for Samsung SoCs
  2. *
  3. * Copyright (C) 2013 Samsung Electronics Co., Ltd.
  4. * http://www.samsung.com
  5. *
  6. * Author: Siva Reddy Kallam <siva.kallam@samsung.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/etherdevice.h>
  14. #include <linux/io.h>
  15. #include <linux/module.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/of.h>
  18. #include <linux/of_irq.h>
  19. #include <linux/of_net.h>
  20. #include <linux/phy.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/sxgbe_platform.h>
  23. #include "sxgbe_common.h"
  24. #include "sxgbe_reg.h"
  25. #ifdef CONFIG_OF
  26. static int sxgbe_probe_config_dt(struct platform_device *pdev,
  27. struct sxgbe_plat_data *plat,
  28. const char **mac)
  29. {
  30. struct device_node *np = pdev->dev.of_node;
  31. struct sxgbe_dma_cfg *dma_cfg;
  32. if (!np)
  33. return -ENODEV;
  34. *mac = of_get_mac_address(np);
  35. plat->interface = of_get_phy_mode(np);
  36. plat->bus_id = of_alias_get_id(np, "ethernet");
  37. if (plat->bus_id < 0)
  38. plat->bus_id = 0;
  39. plat->mdio_bus_data = devm_kzalloc(&pdev->dev,
  40. sizeof(*plat->mdio_bus_data),
  41. GFP_KERNEL);
  42. dma_cfg = devm_kzalloc(&pdev->dev, sizeof(*dma_cfg), GFP_KERNEL);
  43. if (!dma_cfg)
  44. return -ENOMEM;
  45. plat->dma_cfg = dma_cfg;
  46. of_property_read_u32(np, "samsung,pbl", &dma_cfg->pbl);
  47. if (of_property_read_u32(np, "samsung,burst-map", &dma_cfg->burst_map) == 0)
  48. dma_cfg->fixed_burst = true;
  49. return 0;
  50. }
  51. #else
  52. static int sxgbe_probe_config_dt(struct platform_device *pdev,
  53. struct sxgbe_plat_data *plat,
  54. const char **mac)
  55. {
  56. return -ENOSYS;
  57. }
  58. #endif /* CONFIG_OF */
  59. /**
  60. * sxgbe_platform_probe
  61. * @pdev: platform device pointer
  62. * Description: platform_device probe function. It allocates
  63. * the necessary resources and invokes the main to init
  64. * the net device, register the mdio bus etc.
  65. */
  66. static int sxgbe_platform_probe(struct platform_device *pdev)
  67. {
  68. int ret;
  69. int i, chan;
  70. struct resource *res;
  71. struct device *dev = &pdev->dev;
  72. void __iomem *addr;
  73. struct sxgbe_priv_data *priv = NULL;
  74. struct sxgbe_plat_data *plat_dat = NULL;
  75. const char *mac = NULL;
  76. struct net_device *ndev = platform_get_drvdata(pdev);
  77. struct device_node *node = dev->of_node;
  78. /* Get memory resource */
  79. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  80. if (!res)
  81. goto err_out;
  82. addr = devm_ioremap_resource(dev, res);
  83. if (IS_ERR(addr))
  84. return PTR_ERR(addr);
  85. if (pdev->dev.of_node) {
  86. plat_dat = devm_kzalloc(&pdev->dev,
  87. sizeof(struct sxgbe_plat_data),
  88. GFP_KERNEL);
  89. if (!plat_dat)
  90. return -ENOMEM;
  91. ret = sxgbe_probe_config_dt(pdev, plat_dat, &mac);
  92. if (ret) {
  93. pr_err("%s: main dt probe failed\n", __func__);
  94. return ret;
  95. }
  96. }
  97. /* Get MAC address if available (DT) */
  98. if (mac)
  99. ether_addr_copy(priv->dev->dev_addr, mac);
  100. priv = sxgbe_drv_probe(&(pdev->dev), plat_dat, addr);
  101. if (!priv) {
  102. pr_err("%s: main driver probe failed\n", __func__);
  103. goto err_out;
  104. }
  105. /* Get the SXGBE common INT information */
  106. priv->irq = irq_of_parse_and_map(node, 0);
  107. if (priv->irq <= 0) {
  108. dev_err(dev, "sxgbe common irq parsing failed\n");
  109. goto err_drv_remove;
  110. }
  111. /* Get the TX/RX IRQ numbers */
  112. for (i = 0, chan = 1; i < SXGBE_TX_QUEUES; i++) {
  113. priv->txq[i]->irq_no = irq_of_parse_and_map(node, chan++);
  114. if (priv->txq[i]->irq_no <= 0) {
  115. dev_err(dev, "sxgbe tx irq parsing failed\n");
  116. goto err_tx_irq_unmap;
  117. }
  118. }
  119. for (i = 0; i < SXGBE_RX_QUEUES; i++) {
  120. priv->rxq[i]->irq_no = irq_of_parse_and_map(node, chan++);
  121. if (priv->rxq[i]->irq_no <= 0) {
  122. dev_err(dev, "sxgbe rx irq parsing failed\n");
  123. goto err_rx_irq_unmap;
  124. }
  125. }
  126. priv->lpi_irq = irq_of_parse_and_map(node, chan);
  127. if (priv->lpi_irq <= 0) {
  128. dev_err(dev, "sxgbe lpi irq parsing failed\n");
  129. goto err_rx_irq_unmap;
  130. }
  131. platform_set_drvdata(pdev, priv->dev);
  132. pr_debug("platform driver registration completed\n");
  133. return 0;
  134. err_rx_irq_unmap:
  135. while (--i)
  136. irq_dispose_mapping(priv->rxq[i]->irq_no);
  137. i = SXGBE_TX_QUEUES;
  138. err_tx_irq_unmap:
  139. while (--i)
  140. irq_dispose_mapping(priv->txq[i]->irq_no);
  141. irq_dispose_mapping(priv->irq);
  142. err_drv_remove:
  143. sxgbe_drv_remove(ndev);
  144. err_out:
  145. return -ENODEV;
  146. }
  147. /**
  148. * sxgbe_platform_remove
  149. * @pdev: platform device pointer
  150. * Description: this function calls the main to free the net resources
  151. * and calls the platforms hook and release the resources (e.g. mem).
  152. */
  153. static int sxgbe_platform_remove(struct platform_device *pdev)
  154. {
  155. struct net_device *ndev = platform_get_drvdata(pdev);
  156. int ret = sxgbe_drv_remove(ndev);
  157. return ret;
  158. }
  159. #ifdef CONFIG_PM
  160. static int sxgbe_platform_suspend(struct device *dev)
  161. {
  162. struct net_device *ndev = dev_get_drvdata(dev);
  163. return sxgbe_suspend(ndev);
  164. }
  165. static int sxgbe_platform_resume(struct device *dev)
  166. {
  167. struct net_device *ndev = dev_get_drvdata(dev);
  168. return sxgbe_resume(ndev);
  169. }
  170. static int sxgbe_platform_freeze(struct device *dev)
  171. {
  172. struct net_device *ndev = dev_get_drvdata(dev);
  173. return sxgbe_freeze(ndev);
  174. }
  175. static int sxgbe_platform_restore(struct device *dev)
  176. {
  177. struct net_device *ndev = dev_get_drvdata(dev);
  178. return sxgbe_restore(ndev);
  179. }
  180. static const struct dev_pm_ops sxgbe_platform_pm_ops = {
  181. .suspend = sxgbe_platform_suspend,
  182. .resume = sxgbe_platform_resume,
  183. .freeze = sxgbe_platform_freeze,
  184. .thaw = sxgbe_platform_restore,
  185. .restore = sxgbe_platform_restore,
  186. };
  187. #else
  188. static const struct dev_pm_ops sxgbe_platform_pm_ops;
  189. #endif /* CONFIG_PM */
  190. static const struct of_device_id sxgbe_dt_ids[] = {
  191. { .compatible = "samsung,sxgbe-v2.0a"},
  192. { /* sentinel */ }
  193. };
  194. MODULE_DEVICE_TABLE(of, sxgbe_dt_ids);
  195. static struct platform_driver sxgbe_platform_driver = {
  196. .probe = sxgbe_platform_probe,
  197. .remove = sxgbe_platform_remove,
  198. .driver = {
  199. .name = SXGBE_RESOURCE_NAME,
  200. .owner = THIS_MODULE,
  201. .pm = &sxgbe_platform_pm_ops,
  202. .of_match_table = of_match_ptr(sxgbe_dt_ids),
  203. },
  204. };
  205. int sxgbe_register_platform(void)
  206. {
  207. int err;
  208. err = platform_driver_register(&sxgbe_platform_driver);
  209. if (err)
  210. pr_err("failed to register the platform driver\n");
  211. return err;
  212. }
  213. void sxgbe_unregister_platform(void)
  214. {
  215. platform_driver_unregister(&sxgbe_platform_driver);
  216. }