cpsw-common.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * CPSW common - libs used across TI ethernet devices.
  3. *
  4. * Copyright (C) 2016, Texas Instruments, Incorporated
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <dm.h>
  10. #include <environment.h>
  11. #include <fdt_support.h>
  12. #include <asm/io.h>
  13. #include <cpsw.h>
  14. DECLARE_GLOBAL_DATA_PTR;
  15. #define CTRL_MAC_REG(offset, id) ((offset) + 0x8 * (id))
  16. static int davinci_emac_3517_get_macid(struct udevice *dev, u16 offset,
  17. int slave, u8 *mac_addr)
  18. {
  19. void *fdt = (void *)gd->fdt_blob;
  20. int node = dev_of_offset(dev);
  21. u32 macid_lsb;
  22. u32 macid_msb;
  23. fdt32_t gmii = 0;
  24. int syscon;
  25. u32 addr;
  26. syscon = fdtdec_lookup_phandle(fdt, node, "syscon");
  27. if (syscon < 0) {
  28. pr_err("Syscon offset not found\n");
  29. return -ENOENT;
  30. }
  31. addr = (u32)map_physmem(fdt_translate_address(fdt, syscon, &gmii),
  32. sizeof(u32), MAP_NOCACHE);
  33. if (addr == FDT_ADDR_T_NONE) {
  34. pr_err("Not able to get syscon address to get mac efuse address\n");
  35. return -ENOENT;
  36. }
  37. addr += CTRL_MAC_REG(offset, slave);
  38. /* try reading mac address from efuse */
  39. macid_lsb = readl(addr);
  40. macid_msb = readl(addr + 4);
  41. mac_addr[0] = (macid_msb >> 16) & 0xff;
  42. mac_addr[1] = (macid_msb >> 8) & 0xff;
  43. mac_addr[2] = macid_msb & 0xff;
  44. mac_addr[3] = (macid_lsb >> 16) & 0xff;
  45. mac_addr[4] = (macid_lsb >> 8) & 0xff;
  46. mac_addr[5] = macid_lsb & 0xff;
  47. return 0;
  48. }
  49. static int cpsw_am33xx_cm_get_macid(struct udevice *dev, u16 offset, int slave,
  50. u8 *mac_addr)
  51. {
  52. void *fdt = (void *)gd->fdt_blob;
  53. int node = dev_of_offset(dev);
  54. u32 macid_lo;
  55. u32 macid_hi;
  56. fdt32_t gmii = 0;
  57. int syscon;
  58. u32 addr;
  59. syscon = fdtdec_lookup_phandle(fdt, node, "syscon");
  60. if (syscon < 0) {
  61. pr_err("Syscon offset not found\n");
  62. return -ENOENT;
  63. }
  64. addr = (u32)map_physmem(fdt_translate_address(fdt, syscon, &gmii),
  65. sizeof(u32), MAP_NOCACHE);
  66. if (addr == FDT_ADDR_T_NONE) {
  67. pr_err("Not able to get syscon address to get mac efuse address\n");
  68. return -ENOENT;
  69. }
  70. addr += CTRL_MAC_REG(offset, slave);
  71. /* try reading mac address from efuse */
  72. macid_lo = readl(addr);
  73. macid_hi = readl(addr + 4);
  74. mac_addr[5] = (macid_lo >> 8) & 0xff;
  75. mac_addr[4] = macid_lo & 0xff;
  76. mac_addr[3] = (macid_hi >> 24) & 0xff;
  77. mac_addr[2] = (macid_hi >> 16) & 0xff;
  78. mac_addr[1] = (macid_hi >> 8) & 0xff;
  79. mac_addr[0] = macid_hi & 0xff;
  80. return 0;
  81. }
  82. int ti_cm_get_macid(struct udevice *dev, int slave, u8 *mac_addr)
  83. {
  84. if (of_machine_is_compatible("ti,dm8148"))
  85. return cpsw_am33xx_cm_get_macid(dev, 0x630, slave, mac_addr);
  86. if (of_machine_is_compatible("ti,am33xx"))
  87. return cpsw_am33xx_cm_get_macid(dev, 0x630, slave, mac_addr);
  88. if (device_is_compatible(dev, "ti,am3517-emac"))
  89. return davinci_emac_3517_get_macid(dev, 0x110, slave, mac_addr);
  90. if (device_is_compatible(dev, "ti,dm816-emac"))
  91. return cpsw_am33xx_cm_get_macid(dev, 0x30, slave, mac_addr);
  92. if (of_machine_is_compatible("ti,am43"))
  93. return cpsw_am33xx_cm_get_macid(dev, 0x630, slave, mac_addr);
  94. if (of_machine_is_compatible("ti,dra7"))
  95. return davinci_emac_3517_get_macid(dev, 0x514, slave, mac_addr);
  96. dev_err(dev, "incompatible machine/device type for reading mac address\n");
  97. return -ENOENT;
  98. }