cortina.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright 2017 NXP
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * CORTINA is a registered trademark of Cortina Systems, Inc.
  15. *
  16. */
  17. #include <linux/module.h>
  18. #include <linux/phy.h>
  19. #define PHY_ID_CS4340 0x13e51002
  20. #define VILLA_GLOBAL_CHIP_ID_LSB 0x0
  21. #define VILLA_GLOBAL_CHIP_ID_MSB 0x1
  22. #define VILLA_GLOBAL_GPIO_1_INTS 0x017
  23. static int cortina_read_reg(struct phy_device *phydev, u16 regnum)
  24. {
  25. return mdiobus_read(phydev->mdio.bus, phydev->mdio.addr,
  26. MII_ADDR_C45 | regnum);
  27. }
  28. static int cortina_config_aneg(struct phy_device *phydev)
  29. {
  30. phydev->supported = SUPPORTED_10000baseT_Full;
  31. phydev->advertising = SUPPORTED_10000baseT_Full;
  32. return 0;
  33. }
  34. static int cortina_read_status(struct phy_device *phydev)
  35. {
  36. int gpio_int_status, ret = 0;
  37. gpio_int_status = cortina_read_reg(phydev, VILLA_GLOBAL_GPIO_1_INTS);
  38. if (gpio_int_status < 0) {
  39. ret = gpio_int_status;
  40. goto err;
  41. }
  42. if (gpio_int_status & 0x8) {
  43. /* up when edc_convergedS set */
  44. phydev->speed = SPEED_10000;
  45. phydev->duplex = DUPLEX_FULL;
  46. phydev->link = 1;
  47. } else {
  48. phydev->link = 0;
  49. }
  50. err:
  51. return ret;
  52. }
  53. static int cortina_soft_reset(struct phy_device *phydev)
  54. {
  55. return 0;
  56. }
  57. static int cortina_probe(struct phy_device *phydev)
  58. {
  59. u32 phy_id = 0;
  60. int id_lsb = 0, id_msb = 0;
  61. /* Read device id from phy registers. */
  62. id_lsb = cortina_read_reg(phydev, VILLA_GLOBAL_CHIP_ID_LSB);
  63. if (id_lsb < 0)
  64. return -ENXIO;
  65. phy_id = id_lsb << 16;
  66. id_msb = cortina_read_reg(phydev, VILLA_GLOBAL_CHIP_ID_MSB);
  67. if (id_msb < 0)
  68. return -ENXIO;
  69. phy_id |= id_msb;
  70. /* Make sure the device tree binding matched the driver with the
  71. * right device.
  72. */
  73. if (phy_id != phydev->drv->phy_id) {
  74. phydev_err(phydev, "Error matching phy with %s driver\n",
  75. phydev->drv->name);
  76. return -ENODEV;
  77. }
  78. return 0;
  79. }
  80. static struct phy_driver cortina_driver[] = {
  81. {
  82. .phy_id = PHY_ID_CS4340,
  83. .phy_id_mask = 0xffffffff,
  84. .name = "Cortina CS4340",
  85. .config_aneg = cortina_config_aneg,
  86. .read_status = cortina_read_status,
  87. .soft_reset = cortina_soft_reset,
  88. .probe = cortina_probe,
  89. },
  90. };
  91. module_phy_driver(cortina_driver);
  92. static struct mdio_device_id __maybe_unused cortina_tbl[] = {
  93. { PHY_ID_CS4340, 0xffffffff},
  94. {},
  95. };
  96. MODULE_DEVICE_TABLE(mdio, cortina_tbl);