platform.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. #include "ocelot_3_fpga.h"
  7. #if defined(CONFIG_MV643XX_ETH) || defined(CONFIG_MV643XX_ETH_MODULE)
  8. static struct resource mv643xx_eth_shared_resources[] = {
  9. [0] = {
  10. .name = "ethernet shared base",
  11. .start = 0xf1000000 + MV643XX_ETH_SHARED_REGS,
  12. .end = 0xf1000000 + MV643XX_ETH_SHARED_REGS +
  13. MV643XX_ETH_SHARED_REGS_SIZE - 1,
  14. .flags = IORESOURCE_MEM,
  15. },
  16. };
  17. static struct platform_device mv643xx_eth_shared_device = {
  18. .name = MV643XX_ETH_SHARED_NAME,
  19. .id = 0,
  20. .num_resources = ARRAY_SIZE(mv643xx_eth_shared_resources),
  21. .resource = mv643xx_eth_shared_resources,
  22. };
  23. #define MV_SRAM_BASE 0xfe000000UL
  24. #define MV_SRAM_SIZE (256 * 1024)
  25. #define MV_SRAM_RXRING_SIZE (MV_SRAM_SIZE / 4)
  26. #define MV_SRAM_TXRING_SIZE (MV_SRAM_SIZE / 4)
  27. #define MV_SRAM_BASE_ETH0 MV_SRAM_BASE
  28. #define MV_SRAM_BASE_ETH1 (MV_SRAM_BASE + (MV_SRAM_SIZE / 2))
  29. #define MV64x60_IRQ_ETH_0 48
  30. #define MV64x60_IRQ_ETH_1 49
  31. #define MV64x60_IRQ_ETH_2 50
  32. static struct resource mv64x60_eth0_resources[] = {
  33. [0] = {
  34. .name = "eth0 irq",
  35. .start = MV64x60_IRQ_ETH_0,
  36. .end = MV64x60_IRQ_ETH_0,
  37. .flags = IORESOURCE_IRQ,
  38. },
  39. };
  40. static char eth0_mac_addr[ETH_ALEN];
  41. static struct mv643xx_eth_platform_data eth0_pd = {
  42. .mac_addr = eth0_mac_addr,
  43. .tx_sram_addr = MV_SRAM_BASE_ETH0,
  44. .tx_sram_size = MV_SRAM_TXRING_SIZE,
  45. .tx_queue_size = MV_SRAM_TXRING_SIZE / 16,
  46. .rx_sram_addr = MV_SRAM_BASE_ETH0 + MV_SRAM_TXRING_SIZE,
  47. .rx_sram_size = MV_SRAM_RXRING_SIZE,
  48. .rx_queue_size = MV_SRAM_RXRING_SIZE / 16,
  49. };
  50. static struct platform_device eth0_device = {
  51. .name = MV643XX_ETH_NAME,
  52. .id = 0,
  53. .num_resources = ARRAY_SIZE(mv64x60_eth0_resources),
  54. .resource = mv64x60_eth0_resources,
  55. .dev = {
  56. .platform_data = &eth0_pd,
  57. },
  58. };
  59. static struct resource mv64x60_eth1_resources[] = {
  60. [0] = {
  61. .name = "eth1 irq",
  62. .start = MV64x60_IRQ_ETH_1,
  63. .end = MV64x60_IRQ_ETH_1,
  64. .flags = IORESOURCE_IRQ,
  65. },
  66. };
  67. static char eth1_mac_addr[ETH_ALEN];
  68. static struct mv643xx_eth_platform_data eth1_pd = {
  69. .mac_addr = eth1_mac_addr,
  70. .tx_sram_addr = MV_SRAM_BASE_ETH1,
  71. .tx_sram_size = MV_SRAM_TXRING_SIZE,
  72. .tx_queue_size = MV_SRAM_TXRING_SIZE / 16,
  73. .rx_sram_addr = MV_SRAM_BASE_ETH1 + MV_SRAM_TXRING_SIZE,
  74. .rx_sram_size = MV_SRAM_RXRING_SIZE,
  75. .rx_queue_size = MV_SRAM_RXRING_SIZE / 16,
  76. };
  77. static struct platform_device eth1_device = {
  78. .name = MV643XX_ETH_NAME,
  79. .id = 1,
  80. .num_resources = ARRAY_SIZE(mv64x60_eth1_resources),
  81. .resource = mv64x60_eth1_resources,
  82. .dev = {
  83. .platform_data = &eth1_pd,
  84. },
  85. };
  86. static struct resource mv64x60_eth2_resources[] = {
  87. [0] = {
  88. .name = "eth2 irq",
  89. .start = MV64x60_IRQ_ETH_2,
  90. .end = MV64x60_IRQ_ETH_2,
  91. .flags = IORESOURCE_IRQ,
  92. },
  93. };
  94. static char eth2_mac_addr[ETH_ALEN];
  95. static struct mv643xx_eth_platform_data eth2_pd = {
  96. .mac_addr = eth2_mac_addr,
  97. };
  98. static struct platform_device eth2_device = {
  99. .name = MV643XX_ETH_NAME,
  100. .id = 2,
  101. .num_resources = ARRAY_SIZE(mv64x60_eth2_resources),
  102. .resource = mv64x60_eth2_resources,
  103. .dev = {
  104. .platform_data = &eth2_pd,
  105. },
  106. };
  107. static struct platform_device *mv643xx_eth_pd_devs[] __initdata = {
  108. &mv643xx_eth_shared_device,
  109. &eth0_device,
  110. &eth1_device,
  111. &eth2_device,
  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. /* turn the clock on */
  119. OCELOT_FPGA_WRITE((val << 2) | cs | 0x2, EEPROM_MODE);
  120. udelay(1);
  121. /* turn the clock off and read-strobe */
  122. OCELOT_FPGA_WRITE((val << 2) | cs | 0x10, EEPROM_MODE);
  123. /* return the data */
  124. return (OCELOT_FPGA_READ(EEPROM_MODE) >> 3) & 0x1;
  125. }
  126. static void __init get_mac(char dest[6])
  127. {
  128. u8 read_opcode[12] = {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  129. int i,j;
  130. for (i = 0; i < 12; i++)
  131. exchange_bit(read_opcode[i], 1);
  132. for (j = 0; j < 6; j++) {
  133. dest[j] = 0;
  134. for (i = 0; i < 8; i++) {
  135. dest[j] <<= 1;
  136. dest[j] |= exchange_bit(0, 1);
  137. }
  138. }
  139. /* turn off CS */
  140. exchange_bit(0,0);
  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. BUG_ON(add >= 256);
  153. for (i = ETH_ALEN; i >= 0; i--) {
  154. dst[i] = src[i] + add;
  155. add = dst[i] < src[i]; /* compute carry */
  156. }
  157. WARN_ON(add);
  158. }
  159. static int __init mv643xx_eth_add_pds(void)
  160. {
  161. unsigned char mac[ETH_ALEN];
  162. int ret;
  163. get_mac(mac);
  164. eth_mac_add(eth1_mac_addr, mac, 0);
  165. eth_mac_add(eth1_mac_addr, mac, 1);
  166. eth_mac_add(eth2_mac_addr, mac, 2);
  167. ret = platform_add_devices(mv643xx_eth_pd_devs,
  168. ARRAY_SIZE(mv643xx_eth_pd_devs));
  169. return ret;
  170. }
  171. device_initcall(mv643xx_eth_add_pds);
  172. #endif /* defined(CONFIG_MV643XX_ETH) || defined(CONFIG_MV643XX_ETH_MODULE) */