cpsw-common.c 3.1 KB

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