523-MIPS-ath79-OTP-support.patch 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. --- a/arch/mips/ath79/dev-wmac.c
  2. +++ b/arch/mips/ath79/dev-wmac.c
  3. @@ -167,6 +167,137 @@ static void qca955x_wmac_setup(void)
  4. ath79_wmac_data.is_clk_25mhz = true;
  5. }
  6. +static bool __init
  7. +ar93xx_wmac_otp_read_word(void __iomem *base, int addr, u32 *data)
  8. +{
  9. + int timeout = 1000;
  10. + u32 val;
  11. +
  12. + __raw_readl(base + AR9300_OTP_BASE + (4 * addr));
  13. + while (timeout--) {
  14. + val = __raw_readl(base + AR9300_OTP_STATUS);
  15. + if ((val & AR9300_OTP_STATUS_TYPE) == AR9300_OTP_STATUS_VALID)
  16. + break;
  17. +
  18. + udelay(10);
  19. + }
  20. +
  21. + if (!timeout)
  22. + return false;
  23. +
  24. + *data = __raw_readl(base + AR9300_OTP_READ_DATA);
  25. + return true;
  26. +}
  27. +
  28. +static bool __init
  29. +ar93xx_wmac_otp_read(void __iomem *base, int addr, u8 *dest, int len)
  30. +{
  31. + u32 data;
  32. + int i;
  33. +
  34. + for (i = 0; i < len; i++) {
  35. + int offset = 8 * ((addr - i) % 4);
  36. +
  37. + if (!ar93xx_wmac_otp_read_word(base, (addr - i) / 4, &data))
  38. + return false;
  39. +
  40. + dest[i] = (data >> offset) & 0xff;
  41. + }
  42. +
  43. + return true;
  44. +}
  45. +
  46. +static bool __init
  47. +ar93xx_wmac_otp_uncompress(void __iomem *base, int addr, int len, u8 *dest,
  48. + int dest_start, int dest_len)
  49. +{
  50. + int dest_bytes = 0;
  51. + int offset = 0;
  52. + int end = addr - len;
  53. + u8 hdr[2];
  54. +
  55. + while (addr > end) {
  56. + if (!ar93xx_wmac_otp_read(base, addr, hdr, 2))
  57. + return false;
  58. +
  59. + addr -= 2;
  60. + offset += hdr[0];
  61. +
  62. + if (offset <= dest_start + dest_len &&
  63. + offset + len >= dest_start) {
  64. + int data_offset = 0;
  65. + int dest_offset = 0;
  66. + int copy_len;
  67. +
  68. + if (offset < dest_start)
  69. + data_offset = dest_start - offset;
  70. + else
  71. + dest_offset = offset - dest_start;
  72. +
  73. + copy_len = len - data_offset;
  74. + if (copy_len > dest_len - dest_offset)
  75. + copy_len = dest_len - dest_offset;
  76. +
  77. + ar93xx_wmac_otp_read(base, addr - data_offset,
  78. + dest + dest_offset,
  79. + copy_len);
  80. +
  81. + dest_bytes += copy_len;
  82. + }
  83. + addr -= hdr[1];
  84. + }
  85. + return !!dest_bytes;
  86. +}
  87. +
  88. +bool __init ar93xx_wmac_read_mac_address(u8 *dest)
  89. +{
  90. + void __iomem *base;
  91. + bool ret = false;
  92. + int addr = 0x1ff;
  93. + unsigned int len;
  94. + u32 hdr_u32;
  95. + u8 *hdr = (u8 *) &hdr_u32;
  96. + u8 mac[6] = { 0x00, 0x02, 0x03, 0x04, 0x05, 0x06 };
  97. + int mac_start = 2, mac_end = 8;
  98. +
  99. + BUG_ON(!soc_is_ar933x() && !soc_is_ar934x());
  100. + base = ioremap_nocache(AR933X_WMAC_BASE, AR933X_WMAC_SIZE);
  101. + while (addr > sizeof(hdr)) {
  102. + if (!ar93xx_wmac_otp_read(base, addr, hdr, sizeof(hdr)))
  103. + break;
  104. +
  105. + if (hdr_u32 == 0 || hdr_u32 == ~0)
  106. + break;
  107. +
  108. + len = (hdr[1] << 4) | (hdr[2] >> 4);
  109. + addr -= 4;
  110. +
  111. + switch (hdr[0] >> 5) {
  112. + case 0:
  113. + if (len < mac_end)
  114. + break;
  115. +
  116. + ar93xx_wmac_otp_read(base, addr - mac_start, mac, 6);
  117. + ret = true;
  118. + break;
  119. + case 3:
  120. + ret |= ar93xx_wmac_otp_uncompress(base, addr, len, mac,
  121. + mac_start, 6);
  122. + break;
  123. + default:
  124. + break;
  125. + }
  126. +
  127. + addr -= len + 2;
  128. + }
  129. +
  130. + iounmap(base);
  131. + if (ret)
  132. + memcpy(dest, mac, 6);
  133. +
  134. + return ret;
  135. +}
  136. +
  137. void __init ath79_register_wmac(u8 *cal_data, u8 *mac_addr)
  138. {
  139. if (soc_is_ar913x())
  140. --- a/arch/mips/ath79/dev-wmac.h
  141. +++ b/arch/mips/ath79/dev-wmac.h
  142. @@ -14,5 +14,6 @@
  143. void ath79_register_wmac(u8 *cal_data, u8 *mac_addr);
  144. void ath79_register_wmac_simple(void);
  145. +bool ar93xx_wmac_read_mac_address(u8 *dest);
  146. #endif /* _ATH79_DEV_WMAC_H */
  147. --- a/arch/mips/include/asm/mach-ath79/ar71xx_regs.h
  148. +++ b/arch/mips/include/asm/mach-ath79/ar71xx_regs.h
  149. @@ -112,6 +112,14 @@
  150. #define QCA955X_EHCI1_BASE 0x1b400000
  151. #define QCA955X_EHCI_SIZE 0x1000
  152. +#define AR9300_OTP_BASE 0x14000
  153. +#define AR9300_OTP_STATUS 0x15f18
  154. +#define AR9300_OTP_STATUS_TYPE 0x7
  155. +#define AR9300_OTP_STATUS_VALID 0x4
  156. +#define AR9300_OTP_STATUS_ACCESS_BUSY 0x2
  157. +#define AR9300_OTP_STATUS_SM_BUSY 0x1
  158. +#define AR9300_OTP_READ_DATA 0x15f1c
  159. +
  160. /*
  161. * DDR_CTRL block
  162. */