cpsw-common.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // SPDX-License-Identifier: GPL-2.0+
  2. #include <linux/kernel.h>
  3. #include <linux/module.h>
  4. #include <linux/of.h>
  5. #include <linux/regmap.h>
  6. #include <linux/mfd/syscon.h>
  7. #include "cpsw.h"
  8. #define CTRL_MAC_LO_REG(offset, id) ((offset) + 0x8 * (id))
  9. #define CTRL_MAC_HI_REG(offset, id) ((offset) + 0x8 * (id) + 0x4)
  10. static int davinci_emac_3517_get_macid(struct device *dev, u16 offset,
  11. int slave, u8 *mac_addr)
  12. {
  13. u32 macid_lsb;
  14. u32 macid_msb;
  15. struct regmap *syscon;
  16. syscon = syscon_regmap_lookup_by_phandle(dev->of_node, "syscon");
  17. if (IS_ERR(syscon)) {
  18. if (PTR_ERR(syscon) == -ENODEV)
  19. return 0;
  20. return PTR_ERR(syscon);
  21. }
  22. regmap_read(syscon, CTRL_MAC_LO_REG(offset, slave), &macid_lsb);
  23. regmap_read(syscon, CTRL_MAC_HI_REG(offset, slave), &macid_msb);
  24. mac_addr[0] = (macid_msb >> 16) & 0xff;
  25. mac_addr[1] = (macid_msb >> 8) & 0xff;
  26. mac_addr[2] = macid_msb & 0xff;
  27. mac_addr[3] = (macid_lsb >> 16) & 0xff;
  28. mac_addr[4] = (macid_lsb >> 8) & 0xff;
  29. mac_addr[5] = macid_lsb & 0xff;
  30. return 0;
  31. }
  32. static int cpsw_am33xx_cm_get_macid(struct device *dev, u16 offset, int slave,
  33. u8 *mac_addr)
  34. {
  35. u32 macid_lo;
  36. u32 macid_hi;
  37. struct regmap *syscon;
  38. syscon = syscon_regmap_lookup_by_phandle(dev->of_node, "syscon");
  39. if (IS_ERR(syscon)) {
  40. if (PTR_ERR(syscon) == -ENODEV)
  41. return 0;
  42. return PTR_ERR(syscon);
  43. }
  44. regmap_read(syscon, CTRL_MAC_LO_REG(offset, slave), &macid_lo);
  45. regmap_read(syscon, CTRL_MAC_HI_REG(offset, slave), &macid_hi);
  46. mac_addr[5] = (macid_lo >> 8) & 0xff;
  47. mac_addr[4] = macid_lo & 0xff;
  48. mac_addr[3] = (macid_hi >> 24) & 0xff;
  49. mac_addr[2] = (macid_hi >> 16) & 0xff;
  50. mac_addr[1] = (macid_hi >> 8) & 0xff;
  51. mac_addr[0] = macid_hi & 0xff;
  52. return 0;
  53. }
  54. int ti_cm_get_macid(struct device *dev, int slave, u8 *mac_addr)
  55. {
  56. if (of_machine_is_compatible("ti,dm8148"))
  57. return cpsw_am33xx_cm_get_macid(dev, 0x630, slave, mac_addr);
  58. if (of_machine_is_compatible("ti,am33xx"))
  59. return cpsw_am33xx_cm_get_macid(dev, 0x630, slave, mac_addr);
  60. if (of_device_is_compatible(dev->of_node, "ti,am3517-emac"))
  61. return davinci_emac_3517_get_macid(dev, 0x110, slave, mac_addr);
  62. if (of_device_is_compatible(dev->of_node, "ti,dm816-emac"))
  63. return cpsw_am33xx_cm_get_macid(dev, 0x30, slave, mac_addr);
  64. if (of_machine_is_compatible("ti,am43"))
  65. return cpsw_am33xx_cm_get_macid(dev, 0x630, slave, mac_addr);
  66. if (of_machine_is_compatible("ti,dra7"))
  67. return davinci_emac_3517_get_macid(dev, 0x514, slave, mac_addr);
  68. dev_info(dev, "incompatible machine/device type for reading mac address\n");
  69. return -ENOENT;
  70. }
  71. EXPORT_SYMBOL_GPL(ti_cm_get_macid);
  72. MODULE_DESCRIPTION("TI CPSW Switch common module");
  73. MODULE_LICENSE("GPL");