Merge branch 'upstream-linus' of master.kernel.org:/pub/scm/linux/kernel/git/jgarzik...
[powerpc.git] / arch / mips / momentum / jaguar_atx / 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 "jaguar_atx_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 #define MV64x60_IRQ_ETH_2 50
40
41 static struct resource mv64x60_eth0_resources[] = {
42         [0] = {
43                 .name   = "eth0 irq",
44                 .start  = MV64x60_IRQ_ETH_0,
45                 .end    = MV64x60_IRQ_ETH_0,
46                 .flags  = IORESOURCE_IRQ,
47         },
48 };
49
50 static struct mv643xx_eth_platform_data eth0_pd = {
51         .tx_sram_addr   = MV_SRAM_BASE_ETH0,
52         .tx_sram_size   = MV_SRAM_TXRING_SIZE,
53         .tx_queue_size  = MV_SRAM_TXRING_SIZE / 16,
54
55         .rx_sram_addr   = MV_SRAM_BASE_ETH0 + MV_SRAM_TXRING_SIZE,
56         .rx_sram_size   = MV_SRAM_RXRING_SIZE,
57         .rx_queue_size  = MV_SRAM_RXRING_SIZE / 16,
58 };
59
60 static struct platform_device eth0_device = {
61         .name           = MV643XX_ETH_NAME,
62         .id             = 0,
63         .num_resources  = ARRAY_SIZE(mv64x60_eth0_resources),
64         .resource       = mv64x60_eth0_resources,
65         .dev = {
66                 .platform_data = &eth0_pd,
67         },
68 };
69
70 static struct resource mv64x60_eth1_resources[] = {
71         [0] = {
72                 .name   = "eth1 irq",
73                 .start  = MV64x60_IRQ_ETH_1,
74                 .end    = MV64x60_IRQ_ETH_1,
75                 .flags  = IORESOURCE_IRQ,
76         },
77 };
78
79 static struct mv643xx_eth_platform_data eth1_pd = {
80         .tx_sram_addr   = MV_SRAM_BASE_ETH1,
81         .tx_sram_size   = MV_SRAM_TXRING_SIZE,
82         .tx_queue_size  = MV_SRAM_TXRING_SIZE / 16,
83
84         .rx_sram_addr   = MV_SRAM_BASE_ETH1 + MV_SRAM_TXRING_SIZE,
85         .rx_sram_size   = MV_SRAM_RXRING_SIZE,
86         .rx_queue_size  = MV_SRAM_RXRING_SIZE / 16,
87 };
88
89 static struct platform_device eth1_device = {
90         .name           = MV643XX_ETH_NAME,
91         .id             = 1,
92         .num_resources  = ARRAY_SIZE(mv64x60_eth1_resources),
93         .resource       = mv64x60_eth1_resources,
94         .dev = {
95                 .platform_data = &eth1_pd,
96         },
97 };
98
99 static struct resource mv64x60_eth2_resources[] = {
100         [0] = {
101                 .name   = "eth2 irq",
102                 .start  = MV64x60_IRQ_ETH_2,
103                 .end    = MV64x60_IRQ_ETH_2,
104                 .flags  = IORESOURCE_IRQ,
105         },
106 };
107
108 static struct mv643xx_eth_platform_data eth2_pd;
109
110 static struct platform_device eth2_device = {
111         .name           = MV643XX_ETH_NAME,
112         .id             = 2,
113         .num_resources  = ARRAY_SIZE(mv64x60_eth2_resources),
114         .resource       = mv64x60_eth2_resources,
115         .dev = {
116                 .platform_data = &eth2_pd,
117         },
118 };
119
120 static struct platform_device *mv643xx_eth_pd_devs[] __initdata = {
121         &mv643xx_eth_shared_device,
122         &eth0_device,
123         &eth1_device,
124         &eth2_device,
125 };
126
127 static u8 __init exchange_bit(u8 val, u8 cs)
128 {
129         /* place the data */
130         JAGUAR_FPGA_WRITE((val << 2) | cs, EEPROM_MODE);
131         udelay(1);
132
133         /* turn the clock on */
134         JAGUAR_FPGA_WRITE((val << 2) | cs | 0x2, EEPROM_MODE);
135         udelay(1);
136
137         /* turn the clock off and read-strobe */
138         JAGUAR_FPGA_WRITE((val << 2) | cs | 0x10, EEPROM_MODE);
139
140         /* return the data */
141         return (JAGUAR_FPGA_READ(EEPROM_MODE) >> 3) & 0x1;
142 }
143
144 static void __init get_mac(char dest[6])
145 {
146         u8 read_opcode[12] = {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
147         int i,j;
148
149         for (i = 0; i < 12; i++)
150                 exchange_bit(read_opcode[i], 1);
151
152         for (j = 0; j < 6; j++) {
153                 dest[j] = 0;
154                 for (i = 0; i < 8; i++) {
155                         dest[j] <<= 1;
156                         dest[j] |= exchange_bit(0, 1);
157                 }
158         }
159
160         /* turn off CS */
161         exchange_bit(0,0);
162 }
163
164 /*
165  * Copy and increment ethernet MAC address by a small value.
166  *
167  * This is useful for systems where the only one MAC address is stored in
168  * non-volatile memory for multiple ports.
169  */
170 static inline void eth_mac_add(unsigned char *dst, unsigned char *src,
171         unsigned int add)
172 {
173         int i;
174
175         BUG_ON(add >= 256);
176
177         for (i = ETH_ALEN; i >= 0; i--) {
178                 dst[i] = src[i] + add;
179                 add = dst[i] < src[i];          /* compute carry */
180         }
181
182         WARN_ON(add);
183 }
184
185 static int __init mv643xx_eth_add_pds(void)
186 {
187         unsigned char mac[ETH_ALEN];
188         int ret;
189
190         get_mac(mac);
191         eth_mac_add(eth0_pd.mac_addr, mac, 0);
192         eth_mac_add(eth1_pd.mac_addr, mac, 1);
193         eth_mac_add(eth2_pd.mac_addr, mac, 2);
194         ret = platform_add_devices(mv643xx_eth_pd_devs,
195                         ARRAY_SIZE(mv643xx_eth_pd_devs));
196
197         return ret;
198 }
199
200 device_initcall(mv643xx_eth_add_pds);
201
202 #endif /* defined(CONFIG_MV643XX_ETH) || defined(CONFIG_MV643XX_ETH_MODULE) */