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