cpsw-common.c 2.6 KB

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