plb2800_eth.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. * PLB2800 internal switch ethernet driver.
  3. *
  4. * (C) Copyright 2003
  5. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  6. *
  7. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. #include <common.h>
  26. #include <malloc.h>
  27. #include <net.h>
  28. #include <netdev.h>
  29. #include <asm/addrspace.h>
  30. #define NUM_RX_DESC PKTBUFSRX
  31. #define TOUT_LOOP 1000000
  32. #define LONG_REF(addr) (*((volatile unsigned long*)addr))
  33. #define CMAC_CRX_CTRL LONG_REF(0xb800c870)
  34. #define CMAC_CTX_CTRL LONG_REF(0xb800c874)
  35. #define SYS_MAC_ADDR_0 LONG_REF(0xb800c878)
  36. #define SYS_MAC_ADDR_1 LONG_REF(0xb800c87c)
  37. #define MIPS_H_MASK LONG_REF(0xB800C810)
  38. #define MA_LEARN LONG_REF(0xb8008004)
  39. #define DA_LOOKUP LONG_REF(0xb8008008)
  40. #define CMAC_CRX_CTRL_PD 0x00000001
  41. #define CMAC_CRX_CTRL_CG 0x00000002
  42. #define CMAC_CRX_CTRL_PL_SHIFT 2
  43. #define CMAC_CRIT 0x0
  44. #define CMAC_NON_CRIT 0x1
  45. #define MBOX_STAT_ID_SHF 28
  46. #define MBOX_STAT_CP 0x80000000
  47. #define MBOX_STAT_MB 0x00000001
  48. #define EN_MA_LEARN 0x02000000
  49. #define EN_DA_LKUP 0x01000000
  50. #define MA_DEST_SHF 11
  51. #define DA_DEST_SHF 11
  52. #define DA_STATE_SHF 19
  53. #define TSTAMP_MS 0x00000000
  54. #define SW_H_MBOX4_MASK 0x08000000
  55. #define SW_H_MBOX3_MASK 0x04000000
  56. #define SW_H_MBOX2_MASK 0x02000000
  57. #define SW_H_MBOX1_MASK 0x01000000
  58. typedef volatile struct {
  59. unsigned int stat;
  60. unsigned int cmd;
  61. unsigned int cnt;
  62. unsigned int adr;
  63. } mailbox_t;
  64. #define MBOX_REG(mb) ((mailbox_t*)(0xb800c830+(mb<<4)))
  65. typedef volatile struct {
  66. unsigned int word0;
  67. unsigned int word1;
  68. unsigned int word2;
  69. } mbhdr_t;
  70. #define MBOX_MEM(mb) ((void*)(0xb800a000+((3-mb)<<11)))
  71. static int plb2800_eth_init(struct eth_device *dev, bd_t * bis);
  72. static int plb2800_eth_send(struct eth_device *dev, void *packet, int length);
  73. static int plb2800_eth_recv(struct eth_device *dev);
  74. static void plb2800_eth_halt(struct eth_device *dev);
  75. static void plb2800_set_mac_addr(struct eth_device *dev, unsigned char * addr);
  76. static unsigned char * plb2800_get_mac_addr(void);
  77. static int rx_new;
  78. static int mac_addr_set = 0;
  79. int plb2800_eth_initialize(bd_t * bis)
  80. {
  81. struct eth_device *dev;
  82. ulong temp;
  83. #ifdef DEBUG
  84. printf("Entered plb2800_eth_initialize()\n");
  85. #endif
  86. if (!(dev = (struct eth_device *) malloc (sizeof *dev)))
  87. {
  88. printf("Failed to allocate memory\n");
  89. return -1;
  90. }
  91. memset(dev, 0, sizeof(*dev));
  92. sprintf(dev->name, "PLB2800 Switch");
  93. dev->init = plb2800_eth_init;
  94. dev->halt = plb2800_eth_halt;
  95. dev->send = plb2800_eth_send;
  96. dev->recv = plb2800_eth_recv;
  97. eth_register(dev);
  98. /* bug fix */
  99. *(ulong *)0xb800e800 = 0x838;
  100. /* Set MBOX ownership */
  101. temp = CMAC_CRIT << MBOX_STAT_ID_SHF;
  102. MBOX_REG(0)->stat = temp;
  103. MBOX_REG(1)->stat = temp;
  104. temp = CMAC_NON_CRIT << MBOX_STAT_ID_SHF;
  105. MBOX_REG(2)->stat = temp;
  106. MBOX_REG(3)->stat = temp;
  107. plb2800_set_mac_addr(dev, plb2800_get_mac_addr());
  108. /* Disable all Mbox interrupt */
  109. temp = MIPS_H_MASK;
  110. temp &= ~ (SW_H_MBOX1_MASK | SW_H_MBOX2_MASK | SW_H_MBOX3_MASK | SW_H_MBOX4_MASK) ;
  111. MIPS_H_MASK = temp;
  112. #ifdef DEBUG
  113. printf("Leaving plb2800_eth_initialize()\n");
  114. #endif
  115. return 0;
  116. }
  117. static int plb2800_eth_init(struct eth_device *dev, bd_t * bis)
  118. {
  119. #ifdef DEBUG
  120. printf("Entering plb2800_eth_init()\n");
  121. #endif
  122. plb2800_set_mac_addr(dev, dev->enetaddr);
  123. rx_new = 0;
  124. #ifdef DEBUG
  125. printf("Leaving plb2800_eth_init()\n");
  126. #endif
  127. return 0;
  128. }
  129. static int plb2800_eth_send(struct eth_device *dev, void *packet, int length)
  130. {
  131. int i;
  132. int res = -1;
  133. u32 temp;
  134. mailbox_t * mb = MBOX_REG(0);
  135. char * mem = MBOX_MEM(0);
  136. #ifdef DEBUG
  137. printf("Entered plb2800_eth_send()\n");
  138. #endif
  139. if (length <= 0)
  140. {
  141. printf ("%s: bad packet size: %d\n", dev->name, length);
  142. goto Done;
  143. }
  144. if (length < 64)
  145. {
  146. length = 64;
  147. }
  148. temp = CMAC_CRX_CTRL_CG | ((length + 4) << CMAC_CRX_CTRL_PL_SHIFT);
  149. #ifdef DEBUG
  150. printf("0 mb->stat = 0x%x\n", mb->stat);
  151. #endif
  152. for(i = 0; mb->stat & (MBOX_STAT_CP | MBOX_STAT_MB); i++)
  153. {
  154. if (i >= TOUT_LOOP)
  155. {
  156. printf("%s: tx buffer not ready\n", dev->name);
  157. printf("1 mb->stat = 0x%x\n", mb->stat);
  158. goto Done;
  159. }
  160. }
  161. /* For some strange reason, memcpy doesn't work, here!
  162. */
  163. do
  164. {
  165. int words = (length >> 2) + 1;
  166. unsigned int* dst = (unsigned int*)(mem);
  167. unsigned int* src = (unsigned int*)(packet);
  168. for (i = 0; i < words; i++)
  169. {
  170. *dst = *src;
  171. dst++;
  172. src++;
  173. };
  174. } while(0);
  175. CMAC_CRX_CTRL = temp;
  176. mb->cmd = MBOX_STAT_CP;
  177. #ifdef DEBUG
  178. printf("2 mb->stat = 0x%x\n", mb->stat);
  179. #endif
  180. res = length;
  181. Done:
  182. #ifdef DEBUG
  183. printf("Leaving plb2800_eth_send()\n");
  184. #endif
  185. return res;
  186. }
  187. static int plb2800_eth_recv(struct eth_device *dev)
  188. {
  189. int length = 0;
  190. mailbox_t * mbox = MBOX_REG(3);
  191. unsigned char * hdr = MBOX_MEM(3);
  192. unsigned int stat;
  193. #ifdef DEBUG
  194. printf("Entered plb2800_eth_recv()\n");
  195. #endif
  196. for (;;)
  197. {
  198. stat = mbox->stat;
  199. if (!(stat & MBOX_STAT_CP))
  200. {
  201. break;
  202. }
  203. length = ((*(hdr + 6) & 0x3f) << 8) + *(hdr + 7);
  204. memcpy((void *)NetRxPackets[rx_new], hdr + 12, length);
  205. stat &= ~MBOX_STAT_CP;
  206. mbox->stat = stat;
  207. #ifdef DEBUG
  208. {
  209. int i;
  210. for (i=0;i<length - 4;i++)
  211. {
  212. if (i % 16 == 0) printf("\n%04x: ", i);
  213. printf("%02X ", NetRxPackets[rx_new][i]);
  214. }
  215. printf("\n");
  216. }
  217. #endif
  218. if (length)
  219. {
  220. #ifdef DEBUG
  221. printf("Received %d bytes\n", length);
  222. #endif
  223. NetReceive((void*)(NetRxPackets[rx_new]),
  224. length - 4);
  225. }
  226. else
  227. {
  228. #if 1
  229. printf("Zero length!!!\n");
  230. #endif
  231. }
  232. rx_new = (rx_new + 1) % NUM_RX_DESC;
  233. }
  234. #ifdef DEBUG
  235. printf("Leaving plb2800_eth_recv()\n");
  236. #endif
  237. return length;
  238. }
  239. static void plb2800_eth_halt(struct eth_device *dev)
  240. {
  241. #ifdef DEBUG
  242. printf("Entered plb2800_eth_halt()\n");
  243. #endif
  244. #ifdef DEBUG
  245. printf("Leaving plb2800_eth_halt()\n");
  246. #endif
  247. }
  248. static void plb2800_set_mac_addr(struct eth_device *dev, unsigned char * addr)
  249. {
  250. char packet[60];
  251. ulong temp;
  252. int ix;
  253. if (mac_addr_set ||
  254. NULL == addr || memcmp(addr, "\0\0\0\0\0\0", 6) == 0)
  255. {
  256. return;
  257. }
  258. /* send one packet through CPU port
  259. * in order to learn system MAC address
  260. */
  261. /* Set DA_LOOKUP register */
  262. temp = EN_MA_LEARN | (0 << DA_STATE_SHF) | (63 << DA_DEST_SHF);
  263. DA_LOOKUP = temp;
  264. /* Set MA_LEARN register */
  265. temp = 50 << MA_DEST_SHF; /* static entry */
  266. MA_LEARN = temp;
  267. /* set destination address */
  268. for (ix=0;ix<6;ix++)
  269. packet[ix] = 0xff;
  270. /* set source address = system MAC address */
  271. for (ix=0;ix<6;ix++)
  272. packet[6+ix] = addr[ix];
  273. /* set type field */
  274. packet[12]=0xaa;
  275. packet[13]=0x55;
  276. /* set data field */
  277. for(ix=14;ix<60;ix++)
  278. packet[ix] = 0x00;
  279. #ifdef DEBUG
  280. for (ix=0;ix<6;ix++)
  281. printf("mac_addr[%d]=%02X\n", ix, (unsigned char)packet[6+ix]);
  282. #endif
  283. /* set one packet */
  284. plb2800_eth_send(dev, packet, sizeof(packet));
  285. /* delay for a while */
  286. for(ix=0;ix<65535;ix++)
  287. temp = ~temp;
  288. /* Set CMAC_CTX_CTRL register */
  289. temp = TSTAMP_MS; /* no autocast */
  290. CMAC_CTX_CTRL = temp;
  291. /* Set DA_LOOKUP register */
  292. temp = EN_DA_LKUP;
  293. DA_LOOKUP = temp;
  294. mac_addr_set = 1;
  295. }
  296. static unsigned char * plb2800_get_mac_addr(void)
  297. {
  298. static unsigned char addr[6];
  299. char *tmp, *end;
  300. int i;
  301. tmp = getenv ("ethaddr");
  302. if (NULL == tmp) return NULL;
  303. for (i=0; i<6; i++) {
  304. addr[i] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
  305. if (tmp)
  306. tmp = (*end) ? end+1 : end;
  307. }
  308. return addr;
  309. }