mdio_mux_sandbox.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. int err;
  20. /*
  21. * find the sandbox parent mdio and write a register on the PHY there
  22. * so the mux test can verify selection.
  23. */
  24. err = uclass_get_device_by_name(UCLASS_MDIO, "mdio-test", &mdio);
  25. if (err)
  26. return err;
  27. return dm_mdio_write(mdio, SANDBOX_PHY_ADDR, MDIO_DEVAD_NONE,
  28. SANDBOX_PHY_REG_CNT - 1, (u16)sel);
  29. }
  30. static int mdio_mux_sandbox_select(struct udevice *dev, int cur, int sel)
  31. {
  32. struct mdio_mux_sandbox_priv *priv = dev_get_priv(dev);
  33. if (!priv->enabled)
  34. return -ENODEV;
  35. if (cur != priv->sel)
  36. return -EINVAL;
  37. priv->sel = sel;
  38. mdio_mux_sandbox_mark_selection(dev, priv->sel);
  39. return 0;
  40. }
  41. static int mdio_mux_sandbox_deselect(struct udevice *dev, int sel)
  42. {
  43. struct mdio_mux_sandbox_priv *priv = dev_get_priv(dev);
  44. if (!priv->enabled)
  45. return -ENODEV;
  46. if (sel != priv->sel)
  47. return -EINVAL;
  48. priv->sel = -1;
  49. mdio_mux_sandbox_mark_selection(dev, priv->sel);
  50. return 0;
  51. }
  52. static const struct mdio_mux_ops mdio_mux_sandbox_ops = {
  53. .select = mdio_mux_sandbox_select,
  54. .deselect = mdio_mux_sandbox_deselect,
  55. };
  56. static int mdio_mux_sandbox_probe(struct udevice *dev)
  57. {
  58. struct mdio_mux_sandbox_priv *priv = dev_get_priv(dev);
  59. priv->enabled = 1;
  60. priv->sel = -1;
  61. return 0;
  62. }
  63. static const struct udevice_id mdio_mux_sandbox_ids[] = {
  64. { .compatible = "sandbox,mdio-mux" },
  65. { }
  66. };
  67. U_BOOT_DRIVER(mdio_mux_sandbox) = {
  68. .name = "mdio_mux_sandbox",
  69. .id = UCLASS_MDIO_MUX,
  70. .of_match = mdio_mux_sandbox_ids,
  71. .probe = mdio_mux_sandbox_probe,
  72. .ops = &mdio_mux_sandbox_ops,
  73. .priv_auto = sizeof(struct mdio_mux_sandbox_priv),
  74. };