0012-net-switchlib-add-driver-for-Atheros-AR8216.patch 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. From 1a1d61a2faf0390033a3766559ce0e758e15894e Mon Sep 17 00:00:00 2001
  2. From: Luka Perkov <openwrt@lukaperkov.net>
  3. Date: Wed, 29 Aug 2012 22:08:16 +0200
  4. Subject: net: switchlib: add driver for Atheros AR8216
  5. Signed-off-by: Luka Perkov <openwrt@lukaperkov.net>
  6. Signed-off-by: Daniel Schwierzeck <daniel.schwierzeck@gmail.com>
  7. --- a/drivers/net/switch/Makefile
  8. +++ b/drivers/net/switch/Makefile
  9. @@ -12,6 +12,7 @@ LIB := $(obj)libswitch.o
  10. COBJS-$(CONFIG_SWITCH_MULTI) += switch.o
  11. COBJS-$(CONFIG_SWITCH_PSB697X) += psb697x.o
  12. COBJS-$(CONFIG_SWITCH_ADM6996I) += adm6996i.o
  13. +COBJS-$(CONFIG_SWITCH_AR8216) += ar8216.o
  14. COBJS := $(COBJS-y)
  15. SRCS := $(COBJS:.o=.c)
  16. --- /dev/null
  17. +++ b/drivers/net/switch/ar8216.c
  18. @@ -0,0 +1,114 @@
  19. +/*
  20. + * Copyright (C) 2012 Luka Perkov <luka@openwrt.org>
  21. + *
  22. + * SPDX-License-Identifier: GPL-2.0+
  23. + */
  24. +
  25. +#include <common.h>
  26. +#include <malloc.h>
  27. +#include <miiphy.h>
  28. +#include <switch.h>
  29. +#include <netdev.h>
  30. +
  31. +#define BITS(_s, _n) (((1UL << (_n)) - 1) << _s)
  32. +
  33. +#define AR8216_REG_CTRL 0x0000
  34. +#define AR8216_CTRL_REVISION BITS(0, 8)
  35. +#define AR8216_CTRL_VERSION BITS(8, 8)
  36. +
  37. +#define AR8216_PROBE_RETRIES 10
  38. +
  39. +static void split_addr(u32 regaddr, u16 *r1, u16 *r2, u16 *page)
  40. +{
  41. + regaddr >>= 1;
  42. + *r1 = regaddr & 0x1e;
  43. +
  44. + regaddr >>= 5;
  45. + *r2 = regaddr & 0x7;
  46. +
  47. + regaddr >>= 3;
  48. + *page = regaddr & 0x1ff;
  49. +}
  50. +
  51. +static int ar8216_mii_read(struct mii_dev *bus, u32 reg)
  52. +{
  53. + u16 r1, r2, page;
  54. + u16 lo, hi;
  55. +
  56. + split_addr(reg, &r1, &r2, &page);
  57. +
  58. + bus->write(bus, 0x18, MDIO_DEVAD_NONE, 0, page);
  59. + __udelay(1000);
  60. +
  61. + lo = bus->read(bus, 0x10 | r2, MDIO_DEVAD_NONE, r1);
  62. + hi = bus->read(bus, 0x10 | r2, MDIO_DEVAD_NONE, r1 + 1);
  63. +
  64. + return (hi << 16) | lo;
  65. +}
  66. +
  67. +static void ar8216_mii_write(struct mii_dev *bus, u16 reg, u32 val)
  68. +{
  69. + u16 r1, r2, r3;
  70. + u16 lo, hi;
  71. +
  72. + split_addr((u32) reg, &r1, &r2, &r3);
  73. +
  74. + bus->write(bus, 0x18, MDIO_DEVAD_NONE, 0, r3);
  75. + __udelay(1000);
  76. +
  77. + lo = val & 0xffff;
  78. + hi = (u16) (val >> 16);
  79. + bus->write(bus, 0x10 | r2, MDIO_DEVAD_NONE, r1 + 1, hi);
  80. + bus->write(bus, 0x10 | r2, MDIO_DEVAD_NONE, r1, lo);
  81. +}
  82. +
  83. +static int ar8216_probe(struct switch_device *dev)
  84. +{
  85. + struct mii_dev *bus = dev->bus;
  86. + u32 val;
  87. + u16 id;
  88. +
  89. + val = ar8216_mii_read(bus, AR8216_REG_CTRL);
  90. + if (val == ~0)
  91. + return 1;
  92. +
  93. + id = val & (AR8216_CTRL_REVISION | AR8216_CTRL_VERSION);
  94. +
  95. + switch (id) {
  96. + case 0x0101:
  97. + return 0;
  98. + default:
  99. + return 1;
  100. + }
  101. +}
  102. +
  103. +static void ar8216_setup(struct switch_device *dev)
  104. +{
  105. + struct mii_dev *bus = dev->bus;
  106. +
  107. + ar8216_mii_write(bus, 0x200, 0x200);
  108. + ar8216_mii_write(bus, 0x300, 0x200);
  109. + ar8216_mii_write(bus, 0x400, 0x200);
  110. + ar8216_mii_write(bus, 0x500, 0x200);
  111. + ar8216_mii_write(bus, 0x600, 0x7d);
  112. + ar8216_mii_write(bus, 0x38, 0xc000050e);
  113. + ar8216_mii_write(bus, 0x104, 0x4004);
  114. + ar8216_mii_write(bus, 0x60, 0xffffffff);
  115. + ar8216_mii_write(bus, 0x64, 0xaaaaaaaa);
  116. + ar8216_mii_write(bus, 0x68, 0x55555555);
  117. + ar8216_mii_write(bus, 0x6c, 0x0);
  118. + ar8216_mii_write(bus, 0x70, 0x41af);
  119. +}
  120. +
  121. +static struct switch_driver ar8216_drv = {
  122. + .name = "ar8216",
  123. +};
  124. +
  125. +void switch_ar8216_init(void)
  126. +{
  127. + /* for archs with manual relocation */
  128. + ar8216_drv.probe = ar8216_probe;
  129. + ar8216_drv.setup = ar8216_setup;
  130. +
  131. + switch_driver_register(&ar8216_drv);
  132. +}
  133. --- a/drivers/net/switch/switch.c
  134. +++ b/drivers/net/switch/switch.c
  135. @@ -23,6 +23,9 @@ void switch_init(void)
  136. #if defined(CONFIG_SWITCH_ADM6996I)
  137. switch_adm6996i_init();
  138. #endif
  139. +#if defined(CONFIG_SWITCH_AR8216)
  140. + switch_ar8216_init();
  141. +#endif
  142. board_switch_init();
  143. }
  144. --- a/include/switch.h
  145. +++ b/include/switch.h
  146. @@ -99,6 +99,7 @@ static inline void switch_setup(struct s
  147. /* Init functions for supported Switch drivers */
  148. extern void switch_psb697x_init(void);
  149. extern void switch_adm6996i_init(void);
  150. +extern void switch_ar8216_init(void);
  151. #endif /* __SWITCH_H */