tsec.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*
  2. * tsec.c
  3. * Motorola Three Speed Ethernet Controller driver
  4. *
  5. * This software may be used and distributed according to the
  6. * terms of the GNU Public License, Version 2, incorporated
  7. * herein by reference.
  8. *
  9. * (C) Copyright 2003, Motorola, Inc.
  10. * maintained by Xianghua Xiao (x.xiao@motorola.com)
  11. * author Andy Fleming
  12. *
  13. */
  14. #include <config.h>
  15. #include <mpc85xx.h>
  16. #include <common.h>
  17. #include <malloc.h>
  18. #include <net.h>
  19. #include <command.h>
  20. #if defined(CONFIG_TSEC_ENET)
  21. #include "tsec.h"
  22. #define TX_BUF_CNT 2
  23. #undef TSEC_DEBUG
  24. #ifdef TSEC_DEBUG
  25. #define DBGPRINT(x) printf(x)
  26. #else
  27. #define DBGPRINT(x)
  28. #endif
  29. static uint rxIdx; /* index of the current RX buffer */
  30. static uint txIdx; /* index of the current TX buffer */
  31. typedef volatile struct rtxbd {
  32. txbd8_t txbd[TX_BUF_CNT];
  33. rxbd8_t rxbd[PKTBUFSRX];
  34. } RTXBD;
  35. #ifdef __GNUC__
  36. static RTXBD rtx __attribute__ ((aligned(8)));
  37. #else
  38. #error "rtx must be 64-bit aligned"
  39. #endif
  40. static int tsec_send(struct eth_device* dev, volatile void *packet, int length);
  41. static int tsec_recv(struct eth_device* dev);
  42. static int tsec_init(struct eth_device* dev, bd_t * bd);
  43. static void tsec_halt(struct eth_device* dev);
  44. static void init_registers(volatile tsec_t *regs);
  45. static void startup_tsec(volatile tsec_t *regs);
  46. static void init_phy(volatile tsec_t *regs);
  47. /* Initialize device structure. returns 0 on failure, 1 on
  48. * success */
  49. int tsec_initialize(bd_t *bis)
  50. {
  51. struct eth_device* dev;
  52. int i;
  53. dev = (struct eth_device*) malloc(sizeof *dev);
  54. if(dev == NULL)
  55. return 0;
  56. memset(dev, 0, sizeof *dev);
  57. sprintf(dev->name, "MOTOROLA ETHERNET");
  58. dev->iobase = 0;
  59. dev->priv = 0;
  60. dev->init = tsec_init;
  61. dev->halt = tsec_halt;
  62. dev->send = tsec_send;
  63. dev->recv = tsec_recv;
  64. /* Tell u-boot to get the addr from the env */
  65. for(i=0;i<6;i++)
  66. dev->enetaddr[i] = 0;
  67. eth_register(dev);
  68. return 1;
  69. }
  70. /* Initializes data structures and registers for the controller,
  71. * and brings the interface up */
  72. int tsec_init(struct eth_device* dev, bd_t * bd)
  73. {
  74. volatile tsec_t *regs;
  75. uint tempval;
  76. char tmpbuf[MAC_ADDR_LEN];
  77. int i;
  78. regs = (volatile tsec_t *)(TSEC_BASE_ADDR);
  79. /* Make sure the controller is stopped */
  80. tsec_halt(dev);
  81. /* Reset the MAC */
  82. regs->maccfg1 |= MACCFG1_SOFT_RESET;
  83. /* Clear MACCFG1[Soft_Reset] */
  84. regs->maccfg1 &= ~(MACCFG1_SOFT_RESET);
  85. /* Init MACCFG2. Defaults to GMII/MII */
  86. regs->maccfg2 = MACCFG2_INIT_SETTINGS;
  87. /* Init ECNTRL */
  88. regs->ecntrl = ECNTRL_INIT_SETTINGS;
  89. /* Copy the station address into the address registers.
  90. * Backwards, because little endian MACS are dumb */
  91. for(i=0;i<MAC_ADDR_LEN;i++) {
  92. tmpbuf[MAC_ADDR_LEN - 1 - i] = bd->bi_enetaddr[i];
  93. }
  94. (uint)(regs->macstnaddr1) = *((uint *)(tmpbuf));
  95. tempval = *((uint *)(tmpbuf +4));
  96. (uint)(regs->macstnaddr2) = tempval;
  97. /* Initialize the PHY */
  98. init_phy(regs);
  99. /* reset the indices to zero */
  100. rxIdx = 0;
  101. txIdx = 0;
  102. /* Clear out (for the most part) the other registers */
  103. init_registers(regs);
  104. /* Ready the device for tx/rx */
  105. startup_tsec(regs);
  106. return 1;
  107. }
  108. /* Reads from the register at offset in the PHY at phyid, */
  109. /* using the register set defined in regbase. It waits until the */
  110. /* bits in the miimstat are valid (miimind notvalid bit cleared), */
  111. /* and then passes those bits on to the variable specified in */
  112. /* value */
  113. /* Before it does the read, it needs to clear the command field */
  114. uint read_phy_reg(volatile tsec_t *regbase, uint phyid, uint offset)
  115. {
  116. uint value;
  117. /* Put the address of the phy, and the register number into
  118. * MIIMADD
  119. */
  120. regbase->miimadd = (phyid << 8) | offset;
  121. /* Clear the command register, and wait */
  122. regbase->miimcom = 0;
  123. asm("msync");
  124. /* Initiate a read command, and wait */
  125. regbase->miimcom = MIIM_READ_COMMAND;
  126. asm("msync");
  127. /* Wait for the the indication that the read is done */
  128. while((regbase->miimind & (MIIMIND_NOTVALID | MIIMIND_BUSY)));
  129. /* Grab the value read from the PHY */
  130. value = regbase->miimstat;
  131. return value;
  132. }
  133. /* Setup the PHY */
  134. static void init_phy(volatile tsec_t *regs)
  135. {
  136. uint testval;
  137. unsigned int timeout = TSEC_TIMEOUT;
  138. /* Assign a Physical address to the TBI */
  139. regs->tbipa=TBIPA_VALUE;
  140. /* reset the management interface */
  141. regs->miimcfg=MIIMCFG_RESET;
  142. regs->miimcfg=MIIMCFG_INIT_VALUE;
  143. /* Wait until the bus is free */
  144. while(regs->miimind & MIIMIND_BUSY);
  145. #ifdef CONFIG_PHY_CIS8201
  146. /* override PHY config settings */
  147. write_phy_reg(regs, 0, MIIM_AUX_CONSTAT, MIIM_AUXCONSTAT_INIT);
  148. /* Set up interface mode */
  149. write_phy_reg(regs, 0, MIIM_EXT_CON1, MIIM_EXTCON1_INIT);
  150. #endif
  151. /* Set the PHY to gigabit, full duplex, Auto-negotiate */
  152. write_phy_reg(regs, 0, MIIM_CONTROL, MIIM_CONTROL_INIT);
  153. /* Wait until TBI_STATUS indicates AN is done */
  154. DBGPRINT("Waiting for Auto-negotiation to complete\n");
  155. testval=read_phy_reg(regs, 0, MIIM_TBI_STATUS);
  156. while((!(testval & MIIM_TBI_STATUS_AN_DONE))&& timeout--) {
  157. testval=read_phy_reg(regs, 0, MIIM_TBI_STATUS);
  158. }
  159. if(testval & MIIM_TBI_STATUS_AN_DONE)
  160. DBGPRINT("Auto-negotiation done\n");
  161. else
  162. DBGPRINT("Auto-negotiation timed-out.\n");
  163. #ifdef CONFIG_PHY_CIS8201
  164. /* Find out what duplexity (duplicity?) we have */
  165. /* Read it twice to make sure */
  166. testval=read_phy_reg(regs, 0, MIIM_AUX_CONSTAT);
  167. if(testval & MIIM_AUXCONSTAT_DUPLEX) {
  168. DBGPRINT("Enet starting in full duplex\n");
  169. regs->maccfg2 |= MACCFG2_FULL_DUPLEX;
  170. } else {
  171. DBGPRINT("Enet starting in half duplex\n");
  172. regs->maccfg2 &= ~MACCFG2_FULL_DUPLEX;
  173. }
  174. /* Also, we look to see what speed we are at
  175. * if Gigabit, MACCFG2 goes in GMII, otherwise,
  176. * MII mode.
  177. */
  178. if((testval & MIIM_AUXCONSTAT_SPEED) != MIIM_AUXCONSTAT_GBIT) {
  179. if((testval & MIIM_AUXCONSTAT_SPEED) == MIIM_AUXCONSTAT_100)
  180. DBGPRINT("Enet starting in 100BT\n");
  181. else
  182. DBGPRINT("Enet starting in 10BT\n");
  183. /* mark the mode in MACCFG2 */
  184. regs->maccfg2 = ((regs->maccfg2&~(MACCFG2_IF)) | MACCFG2_MII);
  185. } else {
  186. DBGPRINT("Enet starting in 1000BT\n");
  187. }
  188. #endif
  189. #ifdef CONFIG_PHY_M88E1011
  190. /* Read the PHY to see what speed and duplex we are */
  191. testval=read_phy_reg(regs, 0, MIIM_PHY_STATUS);
  192. timeout = TSEC_TIMEOUT;
  193. while((!(testval & MIIM_PHYSTAT_SPDDONE)) && timeout--) {
  194. testval = read_phy_reg(regs,0,MIIM_PHY_STATUS);
  195. }
  196. if(!(testval & MIIM_PHYSTAT_SPDDONE))
  197. DBGPRINT("Enet: Speed not resolved\n");
  198. testval=read_phy_reg(regs, 0, MIIM_PHY_STATUS);
  199. if(testval & MIIM_PHYSTAT_DUPLEX) {
  200. DBGPRINT("Enet starting in Full Duplex\n");
  201. regs->maccfg2 |= MACCFG2_FULL_DUPLEX;
  202. } else {
  203. DBGPRINT("Enet starting in Half Duplex\n");
  204. regs->maccfg2 &= ~MACCFG2_FULL_DUPLEX;
  205. }
  206. if(!((testval&MIIM_PHYSTAT_SPEED) == MIIM_PHYSTAT_GBIT)) {
  207. if((testval & MIIM_PHYSTAT_SPEED) == MIIM_PHYSTAT_100)
  208. DBGPRINT("Enet starting in 100BT\n");
  209. else
  210. DBGPRINT("Enet starting in 10BT\n");
  211. regs->maccfg2 = ((regs->maccfg2&~(MACCFG2_IF)) | MACCFG2_MII);
  212. } else {
  213. DBGPRINT("Enet starting in 1000BT\n");
  214. }
  215. #endif
  216. }
  217. static void init_registers(volatile tsec_t *regs)
  218. {
  219. /* Clear IEVENT */
  220. regs->ievent = IEVENT_INIT_CLEAR;
  221. regs->imask = IMASK_INIT_CLEAR;
  222. regs->hash.iaddr0 = 0;
  223. regs->hash.iaddr1 = 0;
  224. regs->hash.iaddr2 = 0;
  225. regs->hash.iaddr3 = 0;
  226. regs->hash.iaddr4 = 0;
  227. regs->hash.iaddr5 = 0;
  228. regs->hash.iaddr6 = 0;
  229. regs->hash.iaddr7 = 0;
  230. regs->hash.gaddr0 = 0;
  231. regs->hash.gaddr1 = 0;
  232. regs->hash.gaddr2 = 0;
  233. regs->hash.gaddr3 = 0;
  234. regs->hash.gaddr4 = 0;
  235. regs->hash.gaddr5 = 0;
  236. regs->hash.gaddr6 = 0;
  237. regs->hash.gaddr7 = 0;
  238. regs->rctrl = 0x00000000;
  239. /* Init RMON mib registers */
  240. memset((void *)&(regs->rmon), 0, sizeof(rmon_mib_t));
  241. regs->rmon.cam1 = 0xffffffff;
  242. regs->rmon.cam2 = 0xffffffff;
  243. regs->mrblr = MRBLR_INIT_SETTINGS;
  244. regs->minflr = MINFLR_INIT_SETTINGS;
  245. regs->attr = ATTR_INIT_SETTINGS;
  246. regs->attreli = ATTRELI_INIT_SETTINGS;
  247. }
  248. static void startup_tsec(volatile tsec_t *regs)
  249. {
  250. int i;
  251. /* Point to the buffer descriptors */
  252. regs->tbase = (unsigned int)(&rtx.txbd[txIdx]);
  253. regs->rbase = (unsigned int)(&rtx.rxbd[rxIdx]);
  254. /* Initialize the Rx Buffer descriptors */
  255. for (i = 0; i < PKTBUFSRX; i++) {
  256. rtx.rxbd[i].status = RXBD_EMPTY;
  257. rtx.rxbd[i].length = 0;
  258. rtx.rxbd[i].bufPtr = (uint)NetRxPackets[i];
  259. }
  260. rtx.rxbd[PKTBUFSRX -1].status |= RXBD_WRAP;
  261. /* Initialize the TX Buffer Descriptors */
  262. for(i=0; i<TX_BUF_CNT; i++) {
  263. rtx.txbd[i].status = 0;
  264. rtx.txbd[i].length = 0;
  265. rtx.txbd[i].bufPtr = 0;
  266. }
  267. rtx.txbd[TX_BUF_CNT -1].status |= TXBD_WRAP;
  268. /* Enable Transmit and Receive */
  269. regs->maccfg1 |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
  270. /* Tell the DMA it is clear to go */
  271. regs->dmactrl |= DMACTRL_INIT_SETTINGS;
  272. regs->tstat = TSTAT_CLEAR_THALT;
  273. regs->dmactrl &= ~(DMACTRL_GRS | DMACTRL_GTS);
  274. }
  275. /* This returns the status bits of the device. The return value
  276. * is never checked, and this is what the 8260 driver did, so we
  277. * do the same. Presumably, this would be zero if there were no
  278. * errors */
  279. static int tsec_send(struct eth_device* dev, volatile void *packet, int length)
  280. {
  281. int i;
  282. int result = 0;
  283. volatile tsec_t * regs = (volatile tsec_t *)(TSEC_BASE_ADDR);
  284. /* Find an empty buffer descriptor */
  285. for(i=0; rtx.txbd[txIdx].status & TXBD_READY; i++) {
  286. if (i >= TOUT_LOOP) {
  287. DBGPRINT("tsec: tx buffers full\n");
  288. return result;
  289. }
  290. }
  291. rtx.txbd[txIdx].bufPtr = (uint)packet;
  292. rtx.txbd[txIdx].length = length;
  293. rtx.txbd[txIdx].status |= (TXBD_READY | TXBD_LAST | TXBD_CRC | TXBD_INTERRUPT);
  294. /* Tell the DMA to go */
  295. regs->tstat = TSTAT_CLEAR_THALT;
  296. /* Wait for buffer to be transmitted */
  297. for(i=0; rtx.txbd[txIdx].status & TXBD_READY; i++) {
  298. if (i >= TOUT_LOOP) {
  299. DBGPRINT("tsec: tx error\n");
  300. return result;
  301. }
  302. }
  303. txIdx = (txIdx + 1) % TX_BUF_CNT;
  304. result = rtx.txbd[txIdx].status & TXBD_STATS;
  305. return result;
  306. }
  307. static int tsec_recv(struct eth_device* dev)
  308. {
  309. int length;
  310. volatile tsec_t *regs = (volatile tsec_t *)(TSEC_BASE_ADDR);
  311. while(!(rtx.rxbd[rxIdx].status & RXBD_EMPTY)) {
  312. length = rtx.rxbd[rxIdx].length;
  313. /* Send the packet up if there were no errors */
  314. if (!(rtx.rxbd[rxIdx].status & RXBD_STATS)) {
  315. NetReceive(NetRxPackets[rxIdx], length - 4);
  316. }
  317. rtx.rxbd[rxIdx].length = 0;
  318. /* Set the wrap bit if this is the last element in the list */
  319. rtx.rxbd[rxIdx].status = RXBD_EMPTY | (((rxIdx + 1) == PKTBUFSRX) ? RXBD_WRAP : 0);
  320. rxIdx = (rxIdx + 1) % PKTBUFSRX;
  321. }
  322. if(regs->ievent&IEVENT_BSY) {
  323. regs->ievent = IEVENT_BSY;
  324. regs->rstat = RSTAT_CLEAR_RHALT;
  325. }
  326. return -1;
  327. }
  328. static void tsec_halt(struct eth_device* dev)
  329. {
  330. volatile tsec_t *regs = (volatile tsec_t *)(TSEC_BASE_ADDR);
  331. regs->dmactrl &= ~(DMACTRL_GRS | DMACTRL_GTS);
  332. regs->dmactrl |= (DMACTRL_GRS | DMACTRL_GTS);
  333. while(!(regs->ievent & (IEVENT_GRSC | IEVENT_GTSC)));
  334. regs->maccfg1 &= ~(MACCFG1_TX_EN | MACCFG1_RX_EN);
  335. }
  336. #endif /* CONFIG_TSEC_ENET */