platform.c 5.2 KB

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