plb2800_eth.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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, volatile void *packet,
  73. int length);
  74. static int plb2800_eth_recv(struct eth_device *dev);
  75. static void plb2800_eth_halt(struct eth_device *dev);
  76. static void plb2800_set_mac_addr(struct eth_device *dev, unsigned char * addr);
  77. static unsigned char * plb2800_get_mac_addr(void);
  78. static int rx_new;
  79. static int mac_addr_set = 0;
  80. int plb2800_eth_initialize(bd_t * bis)
  81. {
  82. struct eth_device *dev;
  83. ulong temp;
  84. #ifdef DEBUG
  85. printf("Entered plb2800_eth_initialize()\n");
  86. #endif
  87. if (!(dev = (struct eth_device *) malloc (sizeof *dev)))
  88. {
  89. printf("Failed to allocate memory\n");
  90. return -1;
  91. }
  92. memset(dev, 0, sizeof(*dev));
  93. sprintf(dev->name, "PLB2800 Switch");
  94. dev->init = plb2800_eth_init;
  95. dev->halt = plb2800_eth_halt;
  96. dev->send = plb2800_eth_send;
  97. dev->recv = plb2800_eth_recv;
  98. eth_register(dev);
  99. /* bug fix */
  100. *(ulong *)0xb800e800 = 0x838;
  101. /* Set MBOX ownership */
  102. temp = CMAC_CRIT << MBOX_STAT_ID_SHF;
  103. MBOX_REG(0)->stat = temp;
  104. MBOX_REG(1)->stat = temp;
  105. temp = CMAC_NON_CRIT << MBOX_STAT_ID_SHF;
  106. MBOX_REG(2)->stat = temp;
  107. MBOX_REG(3)->stat = temp;
  108. plb2800_set_mac_addr(dev, plb2800_get_mac_addr());
  109. /* Disable all Mbox interrupt */
  110. temp = MIPS_H_MASK;
  111. temp &= ~ (SW_H_MBOX1_MASK | SW_H_MBOX2_MASK | SW_H_MBOX3_MASK | SW_H_MBOX4_MASK) ;
  112. MIPS_H_MASK = temp;
  113. #ifdef DEBUG
  114. printf("Leaving plb2800_eth_initialize()\n");
  115. #endif
  116. return 0;
  117. }
  118. static int plb2800_eth_init(struct eth_device *dev, bd_t * bis)
  119. {
  120. #ifdef DEBUG
  121. printf("Entering plb2800_eth_init()\n");
  122. #endif
  123. plb2800_set_mac_addr(dev, dev->enetaddr);
  124. rx_new = 0;
  125. #ifdef DEBUG
  126. printf("Leaving plb2800_eth_init()\n");
  127. #endif
  128. return 0;
  129. }
  130. static int plb2800_eth_send(struct eth_device *dev, volatile void *packet,
  131. int length)
  132. {
  133. int i;
  134. int res = -1;
  135. u32 temp;
  136. mailbox_t * mb = MBOX_REG(0);
  137. char * mem = MBOX_MEM(0);
  138. #ifdef DEBUG
  139. printf("Entered plb2800_eth_send()\n");
  140. #endif
  141. if (length <= 0)
  142. {
  143. printf ("%s: bad packet size: %d\n", dev->name, length);
  144. goto Done;
  145. }
  146. if (length < 64)
  147. {
  148. length = 64;
  149. }
  150. temp = CMAC_CRX_CTRL_CG | ((length + 4) << CMAC_CRX_CTRL_PL_SHIFT);
  151. #ifdef DEBUG
  152. printf("0 mb->stat = 0x%x\n", mb->stat);
  153. #endif
  154. for(i = 0; mb->stat & (MBOX_STAT_CP | MBOX_STAT_MB); i++)
  155. {
  156. if (i >= TOUT_LOOP)
  157. {
  158. printf("%s: tx buffer not ready\n", dev->name);
  159. printf("1 mb->stat = 0x%x\n", mb->stat);
  160. goto Done;
  161. }
  162. }
  163. /* For some strange reason, memcpy doesn't work, here!
  164. */
  165. do
  166. {
  167. int words = (length >> 2) + 1;
  168. unsigned int* dst = (unsigned int*)(mem);
  169. unsigned int* src = (unsigned int*)(packet);
  170. for (i = 0; i < words; i++)
  171. {
  172. *dst = *src;
  173. dst++;
  174. src++;
  175. };
  176. } while(0);
  177. CMAC_CRX_CTRL = temp;
  178. mb->cmd = MBOX_STAT_CP;
  179. #ifdef DEBUG
  180. printf("2 mb->stat = 0x%x\n", mb->stat);
  181. #endif
  182. res = length;
  183. Done:
  184. #ifdef DEBUG
  185. printf("Leaving plb2800_eth_send()\n");
  186. #endif
  187. return res;
  188. }
  189. static int plb2800_eth_recv(struct eth_device *dev)
  190. {
  191. int length = 0;
  192. mailbox_t * mbox = MBOX_REG(3);
  193. unsigned char * hdr = MBOX_MEM(3);
  194. unsigned int stat;
  195. #ifdef DEBUG
  196. printf("Entered plb2800_eth_recv()\n");
  197. #endif
  198. for (;;)
  199. {
  200. stat = mbox->stat;
  201. if (!(stat & MBOX_STAT_CP))
  202. {
  203. break;
  204. }
  205. length = ((*(hdr + 6) & 0x3f) << 8) + *(hdr + 7);
  206. memcpy((void *)NetRxPackets[rx_new], hdr + 12, length);
  207. stat &= ~MBOX_STAT_CP;
  208. mbox->stat = stat;
  209. #ifdef DEBUG
  210. {
  211. int i;
  212. for (i=0;i<length - 4;i++)
  213. {
  214. if (i % 16 == 0) printf("\n%04x: ", i);
  215. printf("%02X ", NetRxPackets[rx_new][i]);
  216. }
  217. printf("\n");
  218. }
  219. #endif
  220. if (length)
  221. {
  222. #ifdef DEBUG
  223. printf("Received %d bytes\n", length);
  224. #endif
  225. NetReceive((void*)(NetRxPackets[rx_new]),
  226. length - 4);
  227. }
  228. else
  229. {
  230. #if 1
  231. printf("Zero length!!!\n");
  232. #endif
  233. }
  234. rx_new = (rx_new + 1) % NUM_RX_DESC;
  235. }
  236. #ifdef DEBUG
  237. printf("Leaving plb2800_eth_recv()\n");
  238. #endif
  239. return length;
  240. }
  241. static void plb2800_eth_halt(struct eth_device *dev)
  242. {
  243. #ifdef DEBUG
  244. printf("Entered plb2800_eth_halt()\n");
  245. #endif
  246. #ifdef DEBUG
  247. printf("Leaving plb2800_eth_halt()\n");
  248. #endif
  249. }
  250. static void plb2800_set_mac_addr(struct eth_device *dev, unsigned char * addr)
  251. {
  252. char packet[60];
  253. ulong temp;
  254. int ix;
  255. if (mac_addr_set ||
  256. NULL == addr || memcmp(addr, "\0\0\0\0\0\0", 6) == 0)
  257. {
  258. return;
  259. }
  260. /* send one packet through CPU port
  261. * in order to learn system MAC address
  262. */
  263. /* Set DA_LOOKUP register */
  264. temp = EN_MA_LEARN | (0 << DA_STATE_SHF) | (63 << DA_DEST_SHF);
  265. DA_LOOKUP = temp;
  266. /* Set MA_LEARN register */
  267. temp = 50 << MA_DEST_SHF; /* static entry */
  268. MA_LEARN = temp;
  269. /* set destination address */
  270. for (ix=0;ix<6;ix++)
  271. packet[ix] = 0xff;
  272. /* set source address = system MAC address */
  273. for (ix=0;ix<6;ix++)
  274. packet[6+ix] = addr[ix];
  275. /* set type field */
  276. packet[12]=0xaa;
  277. packet[13]=0x55;
  278. /* set data field */
  279. for(ix=14;ix<60;ix++)
  280. packet[ix] = 0x00;
  281. #ifdef DEBUG
  282. for (ix=0;ix<6;ix++)
  283. printf("mac_addr[%d]=%02X\n", ix, (unsigned char)packet[6+ix]);
  284. #endif
  285. /* set one packet */
  286. plb2800_eth_send(dev, packet, sizeof(packet));
  287. /* delay for a while */
  288. for(ix=0;ix<65535;ix++)
  289. temp = ~temp;
  290. /* Set CMAC_CTX_CTRL register */
  291. temp = TSTAMP_MS; /* no autocast */
  292. CMAC_CTX_CTRL = temp;
  293. /* Set DA_LOOKUP register */
  294. temp = EN_DA_LKUP;
  295. DA_LOOKUP = temp;
  296. mac_addr_set = 1;
  297. }
  298. static unsigned char * plb2800_get_mac_addr(void)
  299. {
  300. static unsigned char addr[6];
  301. char *tmp, *end;
  302. int i;
  303. tmp = getenv ("ethaddr");
  304. if (NULL == tmp) return NULL;
  305. for (i=0; i<6; i++) {
  306. addr[i] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
  307. if (tmp)
  308. tmp = (*end) ? end+1 : end;
  309. }
  310. return addr;
  311. }