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