platform.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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_c_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. static struct resource mv64x60_eth0_resources[] = {
  32. [0] = {
  33. .name = "eth0 irq",
  34. .start = MV64x60_IRQ_ETH_0,
  35. .end = MV64x60_IRQ_ETH_0,
  36. .flags = IORESOURCE_IRQ,
  37. },
  38. };
  39. static struct mv643xx_eth_platform_data eth0_pd = {
  40. .tx_sram_addr = MV_SRAM_BASE_ETH0,
  41. .tx_sram_size = MV_SRAM_TXRING_SIZE,
  42. .tx_queue_size = MV_SRAM_TXRING_SIZE / 16,
  43. .rx_sram_addr = MV_SRAM_BASE_ETH0 + MV_SRAM_TXRING_SIZE,
  44. .rx_sram_size = MV_SRAM_RXRING_SIZE,
  45. .rx_queue_size = MV_SRAM_RXRING_SIZE / 16,
  46. };
  47. static struct platform_device eth0_device = {
  48. .name = MV643XX_ETH_NAME,
  49. .id = 0,
  50. .num_resources = ARRAY_SIZE(mv64x60_eth0_resources),
  51. .resource = mv64x60_eth0_resources,
  52. .dev = {
  53. .platform_data = &eth0_pd,
  54. },
  55. };
  56. static struct resource mv64x60_eth1_resources[] = {
  57. [0] = {
  58. .name = "eth1 irq",
  59. .start = MV64x60_IRQ_ETH_1,
  60. .end = MV64x60_IRQ_ETH_1,
  61. .flags = IORESOURCE_IRQ,
  62. },
  63. };
  64. static struct mv643xx_eth_platform_data eth1_pd = {
  65. .tx_sram_addr = MV_SRAM_BASE_ETH1,
  66. .tx_sram_size = MV_SRAM_TXRING_SIZE,
  67. .tx_queue_size = MV_SRAM_TXRING_SIZE / 16,
  68. .rx_sram_addr = MV_SRAM_BASE_ETH1 + MV_SRAM_TXRING_SIZE,
  69. .rx_sram_size = MV_SRAM_RXRING_SIZE,
  70. .rx_queue_size = MV_SRAM_RXRING_SIZE / 16,
  71. };
  72. static struct platform_device eth1_device = {
  73. .name = MV643XX_ETH_NAME,
  74. .id = 1,
  75. .num_resources = ARRAY_SIZE(mv64x60_eth1_resources),
  76. .resource = mv64x60_eth1_resources,
  77. .dev = {
  78. .platform_data = &eth1_pd,
  79. },
  80. };
  81. static struct platform_device *mv643xx_eth_pd_devs[] __initdata = {
  82. &mv643xx_eth_shared_device,
  83. &eth0_device,
  84. &eth1_device,
  85. /* The third port is not wired up on the Ocelot C */
  86. };
  87. static u8 __init exchange_bit(u8 val, u8 cs)
  88. {
  89. /* place the data */
  90. OCELOT_FPGA_WRITE((val << 2) | cs, EEPROM_MODE);
  91. udelay(1);
  92. /* turn the clock on */
  93. OCELOT_FPGA_WRITE((val << 2) | cs | 0x2, EEPROM_MODE);
  94. udelay(1);
  95. /* turn the clock off and read-strobe */
  96. OCELOT_FPGA_WRITE((val << 2) | cs | 0x10, EEPROM_MODE);
  97. /* return the data */
  98. return (OCELOT_FPGA_READ(EEPROM_MODE) >> 3) & 0x1;
  99. }
  100. static void __init get_mac(char dest[6])
  101. {
  102. u8 read_opcode[12] = {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  103. int i,j;
  104. for (i = 0; i < 12; i++)
  105. exchange_bit(read_opcode[i], 1);
  106. for (j = 0; j < 6; j++) {
  107. dest[j] = 0;
  108. for (i = 0; i < 8; i++) {
  109. dest[j] <<= 1;
  110. dest[j] |= exchange_bit(0, 1);
  111. }
  112. }
  113. /* turn off CS */
  114. exchange_bit(0,0);
  115. }
  116. /*
  117. * Copy and increment ethernet MAC address by a small value.
  118. *
  119. * This is useful for systems where the only one MAC address is stored in
  120. * non-volatile memory for multiple ports.
  121. */
  122. static inline void eth_mac_add(unsigned char *dst, unsigned char *src,
  123. unsigned int add)
  124. {
  125. int i;
  126. BUG_ON(add >= 256);
  127. for (i = ETH_ALEN; i >= 0; i--) {
  128. dst[i] = src[i] + add;
  129. add = dst[i] < src[i]; /* compute carry */
  130. }
  131. WARN_ON(add);
  132. }
  133. static int __init mv643xx_eth_add_pds(void)
  134. {
  135. unsigned char mac[ETH_ALEN];
  136. int ret;
  137. get_mac(mac);
  138. eth_mac_add(eth0_pd.mac_addr, mac, 0);
  139. eth_mac_add(eth1_pd.mac_addr, mac, 1);
  140. ret = platform_add_devices(mv643xx_eth_pd_devs,
  141. ARRAY_SIZE(mv643xx_eth_pd_devs));
  142. return ret;
  143. }
  144. device_initcall(mv643xx_eth_add_pds);
  145. #endif /* defined(CONFIG_MV643XX_ETH) || defined(CONFIG_MV643XX_ETH_MODULE) */