[ALSA] version 1.0.14rc3
[firefly-linux-kernel-4.4.55.git] / arch / mips / momentum / ocelot_c / platform.c
1 #include <linux/delay.h>
2 #include <linux/if_ether.h>
3 #include <linux/ioport.h>
4 #include <linux/mv643xx.h>
5 #include <linux/platform_device.h>
6
7 #include "ocelot_c_fpga.h"
8
9 #if defined(CONFIG_MV643XX_ETH) || defined(CONFIG_MV643XX_ETH_MODULE)
10
11 static struct resource mv643xx_eth_shared_resources[] = {
12         [0] = {
13                 .name   = "ethernet shared base",
14                 .start  = 0xf1000000 + MV643XX_ETH_SHARED_REGS,
15                 .end    = 0xf1000000 + MV643XX_ETH_SHARED_REGS +
16                                        MV643XX_ETH_SHARED_REGS_SIZE - 1,
17                 .flags  = IORESOURCE_MEM,
18         },
19 };
20
21 static struct platform_device mv643xx_eth_shared_device = {
22         .name           = MV643XX_ETH_SHARED_NAME,
23         .id             = 0,
24         .num_resources  = ARRAY_SIZE(mv643xx_eth_shared_resources),
25         .resource       = mv643xx_eth_shared_resources,
26 };
27
28 #define MV_SRAM_BASE                    0xfe000000UL
29 #define MV_SRAM_SIZE                    (256 * 1024)
30
31 #define MV_SRAM_RXRING_SIZE             (MV_SRAM_SIZE / 4)
32 #define MV_SRAM_TXRING_SIZE             (MV_SRAM_SIZE / 4)
33
34 #define MV_SRAM_BASE_ETH0               MV_SRAM_BASE
35 #define MV_SRAM_BASE_ETH1               (MV_SRAM_BASE + (MV_SRAM_SIZE / 2))
36
37 #define MV64x60_IRQ_ETH_0 48
38 #define MV64x60_IRQ_ETH_1 49
39
40 static struct resource mv64x60_eth0_resources[] = {
41         [0] = {
42                 .name   = "eth0 irq",
43                 .start  = MV64x60_IRQ_ETH_0,
44                 .end    = MV64x60_IRQ_ETH_0,
45                 .flags  = IORESOURCE_IRQ,
46         },
47 };
48
49 static struct mv643xx_eth_platform_data eth0_pd = {
50         .tx_sram_addr   = MV_SRAM_BASE_ETH0,
51         .tx_sram_size   = MV_SRAM_TXRING_SIZE,
52         .tx_queue_size  = MV_SRAM_TXRING_SIZE / 16,
53
54         .rx_sram_addr   = MV_SRAM_BASE_ETH0 + MV_SRAM_TXRING_SIZE,
55         .rx_sram_size   = MV_SRAM_RXRING_SIZE,
56         .rx_queue_size  = MV_SRAM_RXRING_SIZE / 16,
57 };
58
59 static struct platform_device eth0_device = {
60         .name           = MV643XX_ETH_NAME,
61         .id             = 0,
62         .num_resources  = ARRAY_SIZE(mv64x60_eth0_resources),
63         .resource       = mv64x60_eth0_resources,
64         .dev = {
65                 .platform_data = &eth0_pd,
66         },
67 };
68
69 static struct resource mv64x60_eth1_resources[] = {
70         [0] = {
71                 .name   = "eth1 irq",
72                 .start  = MV64x60_IRQ_ETH_1,
73                 .end    = MV64x60_IRQ_ETH_1,
74                 .flags  = IORESOURCE_IRQ,
75         },
76 };
77
78 static struct mv643xx_eth_platform_data eth1_pd = {
79         .tx_sram_addr   = MV_SRAM_BASE_ETH1,
80         .tx_sram_size   = MV_SRAM_TXRING_SIZE,
81         .tx_queue_size  = MV_SRAM_TXRING_SIZE / 16,
82
83         .rx_sram_addr   = MV_SRAM_BASE_ETH1 + MV_SRAM_TXRING_SIZE,
84         .rx_sram_size   = MV_SRAM_RXRING_SIZE,
85         .rx_queue_size  = MV_SRAM_RXRING_SIZE / 16,
86 };
87
88 static struct platform_device eth1_device = {
89         .name           = MV643XX_ETH_NAME,
90         .id             = 1,
91         .num_resources  = ARRAY_SIZE(mv64x60_eth1_resources),
92         .resource       = mv64x60_eth1_resources,
93         .dev = {
94                 .platform_data = &eth1_pd,
95         },
96 };
97
98 static struct platform_device *mv643xx_eth_pd_devs[] __initdata = {
99         &mv643xx_eth_shared_device,
100         &eth0_device,
101         &eth1_device,
102         /* The third port is not wired up on the Ocelot C */
103 };
104
105 static u8 __init exchange_bit(u8 val, u8 cs)
106 {
107         /* place the data */
108         OCELOT_FPGA_WRITE((val << 2) | cs, EEPROM_MODE);
109         udelay(1);
110
111         /* turn the clock on */
112         OCELOT_FPGA_WRITE((val << 2) | cs | 0x2, EEPROM_MODE);
113         udelay(1);
114
115         /* turn the clock off and read-strobe */
116         OCELOT_FPGA_WRITE((val << 2) | cs | 0x10, EEPROM_MODE);
117
118         /* return the data */
119         return (OCELOT_FPGA_READ(EEPROM_MODE) >> 3) & 0x1;
120 }
121
122 static void __init get_mac(char dest[6])
123 {
124         u8 read_opcode[12] = {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
125         int i,j;
126
127         for (i = 0; i < 12; i++)
128                 exchange_bit(read_opcode[i], 1);
129
130         for (j = 0; j < 6; j++) {
131                 dest[j] = 0;
132                 for (i = 0; i < 8; i++) {
133                         dest[j] <<= 1;
134                         dest[j] |= exchange_bit(0, 1);
135                 }
136         }
137
138         /* turn off CS */
139         exchange_bit(0,0);
140 }
141
142 /*
143  * Copy and increment ethernet MAC address by a small value.
144  *
145  * This is useful for systems where the only one MAC address is stored in
146  * non-volatile memory for multiple ports.
147  */
148 static inline void eth_mac_add(unsigned char *dst, unsigned char *src,
149         unsigned int add)
150 {
151         int i;
152
153         BUG_ON(add >= 256);
154
155         for (i = ETH_ALEN; i >= 0; i--) {
156                 dst[i] = src[i] + add;
157                 add = dst[i] < src[i];          /* compute carry */
158         }
159
160         WARN_ON(add);
161 }
162
163 static int __init mv643xx_eth_add_pds(void)
164 {
165         unsigned char mac[ETH_ALEN];
166         int ret;
167
168         get_mac(mac);
169         eth_mac_add(eth0_pd.mac_addr, mac, 0);
170         eth_mac_add(eth1_pd.mac_addr, mac, 1);
171         ret = platform_add_devices(mv643xx_eth_pd_devs,
172                         ARRAY_SIZE(mv643xx_eth_pd_devs));
173
174         return ret;
175 }
176
177 device_initcall(mv643xx_eth_add_pds);
178
179 #endif /* defined(CONFIG_MV643XX_ETH) || defined(CONFIG_MV643XX_ETH_MODULE) */