mdio_mux_sandbox.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2019
  4. * Alex Marginean, NXP
  5. */
  6. #include <dm.h>
  7. #include <errno.h>
  8. #include <miiphy.h>
  9. /* macros copied over from mdio_sandbox.c */
  10. #define SANDBOX_PHY_ADDR 5
  11. #define SANDBOX_PHY_REG_CNT 2
  12. struct mdio_mux_sandbox_priv {
  13. int enabled;
  14. int sel;
  15. };
  16. static int mdio_mux_sandbox_mark_selection(struct udevice *dev, int sel)
  17. {
  18. struct udevice *mdio;
  19. struct mdio_ops *ops;
  20. int err;
  21. /*
  22. * find the sandbox parent mdio and write a register on the PHY there
  23. * so the mux test can verify selection.
  24. */
  25. err = uclass_get_device_by_name(UCLASS_MDIO, "mdio-test", &mdio);
  26. if (err)
  27. return err;
  28. ops = mdio_get_ops(mdio);
  29. return ops->write(mdio, SANDBOX_PHY_ADDR, MDIO_DEVAD_NONE,
  30. SANDBOX_PHY_REG_CNT - 1, (u16)sel);
  31. }
  32. static int mdio_mux_sandbox_select(struct udevice *dev, int cur, int sel)
  33. {
  34. struct mdio_mux_sandbox_priv *priv = dev_get_priv(dev);
  35. if (!priv->enabled)
  36. return -ENODEV;
  37. if (cur != priv->sel)
  38. return -EINVAL;
  39. priv->sel = sel;
  40. mdio_mux_sandbox_mark_selection(dev, priv->sel);
  41. return 0;
  42. }
  43. static int mdio_mux_sandbox_deselect(struct udevice *dev, int sel)
  44. {
  45. struct mdio_mux_sandbox_priv *priv = dev_get_priv(dev);
  46. if (!priv->enabled)
  47. return -ENODEV;
  48. if (sel != priv->sel)
  49. return -EINVAL;
  50. priv->sel = -1;
  51. mdio_mux_sandbox_mark_selection(dev, priv->sel);
  52. return 0;
  53. }
  54. static const struct mdio_mux_ops mdio_mux_sandbox_ops = {
  55. .select = mdio_mux_sandbox_select,
  56. .deselect = mdio_mux_sandbox_deselect,
  57. };
  58. static int mdio_mux_sandbox_probe(struct udevice *dev)
  59. {
  60. struct mdio_mux_sandbox_priv *priv = dev_get_priv(dev);
  61. priv->enabled = 1;
  62. priv->sel = -1;
  63. return 0;
  64. }
  65. static const struct udevice_id mdio_mux_sandbox_ids[] = {
  66. { .compatible = "sandbox,mdio-mux" },
  67. { }
  68. };
  69. U_BOOT_DRIVER(mdio_mux_sandbox) = {
  70. .name = "mdio_mux_sandbox",
  71. .id = UCLASS_MDIO_MUX,
  72. .of_match = mdio_mux_sandbox_ids,
  73. .probe = mdio_mux_sandbox_probe,
  74. .ops = &mdio_mux_sandbox_ops,
  75. .priv_auto = sizeof(struct mdio_mux_sandbox_priv),
  76. };