bfin_mac.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*
  2. * Driver for Blackfin On-Chip MAC device
  3. *
  4. * Copyright (c) 2005-2008 Analog Device, Inc.
  5. *
  6. * Licensed under the GPL-2 or later.
  7. */
  8. #include <common.h>
  9. #include <config.h>
  10. #include <net.h>
  11. #include <netdev.h>
  12. #include <command.h>
  13. #include <malloc.h>
  14. #include <miiphy.h>
  15. #include <linux/mii.h>
  16. #include <asm/blackfin.h>
  17. #include <asm/portmux.h>
  18. #include <asm/mach-common/bits/dma.h>
  19. #include <asm/mach-common/bits/emac.h>
  20. #include <asm/mach-common/bits/pll.h>
  21. #include "bfin_mac.h"
  22. #ifndef CONFIG_PHY_ADDR
  23. # define CONFIG_PHY_ADDR 1
  24. #endif
  25. #ifndef CONFIG_PHY_CLOCK_FREQ
  26. # define CONFIG_PHY_CLOCK_FREQ 2500000
  27. #endif
  28. #ifdef CONFIG_POST
  29. #include <post.h>
  30. #endif
  31. #define RXBUF_BASE_ADDR 0xFF900000
  32. #define TXBUF_BASE_ADDR 0xFF800000
  33. #define TX_BUF_CNT 1
  34. #define TOUT_LOOP 1000000
  35. static ADI_ETHER_BUFFER *txbuf[TX_BUF_CNT];
  36. static ADI_ETHER_BUFFER *rxbuf[PKTBUFSRX];
  37. static u16 txIdx; /* index of the current RX buffer */
  38. static u16 rxIdx; /* index of the current TX buffer */
  39. /* DMAx_CONFIG values at DMA Restart */
  40. static const union {
  41. u16 data;
  42. ADI_DMA_CONFIG_REG reg;
  43. } txdmacfg = {
  44. .reg = {
  45. .b_DMA_EN = 1, /* enabled */
  46. .b_WNR = 0, /* read from memory */
  47. .b_WDSIZE = 2, /* wordsize is 32 bits */
  48. .b_DMA2D = 0,
  49. .b_RESTART = 0,
  50. .b_DI_SEL = 0,
  51. .b_DI_EN = 0, /* no interrupt */
  52. .b_NDSIZE = 5, /* 5 half words is desc size */
  53. .b_FLOW = 7 /* large desc flow */
  54. },
  55. };
  56. static int bfin_miiphy_wait(void)
  57. {
  58. /* poll the STABUSY bit */
  59. while (bfin_read_EMAC_STAADD() & STABUSY)
  60. continue;
  61. return 0;
  62. }
  63. static int bfin_miiphy_read(const char *devname, uchar addr, uchar reg, ushort *val)
  64. {
  65. if (bfin_miiphy_wait())
  66. return 1;
  67. bfin_write_EMAC_STAADD(SET_PHYAD(addr) | SET_REGAD(reg) | STABUSY);
  68. if (bfin_miiphy_wait())
  69. return 1;
  70. *val = bfin_read_EMAC_STADAT();
  71. return 0;
  72. }
  73. static int bfin_miiphy_write(const char *devname, uchar addr, uchar reg, ushort val)
  74. {
  75. if (bfin_miiphy_wait())
  76. return 1;
  77. bfin_write_EMAC_STADAT(val);
  78. bfin_write_EMAC_STAADD(SET_PHYAD(addr) | SET_REGAD(reg) | STAOP | STABUSY);
  79. return 0;
  80. }
  81. int bfin_EMAC_initialize(bd_t *bis)
  82. {
  83. struct eth_device *dev;
  84. dev = malloc(sizeof(*dev));
  85. if (dev == NULL)
  86. hang();
  87. memset(dev, 0, sizeof(*dev));
  88. strcpy(dev->name, "bfin_mac");
  89. dev->iobase = 0;
  90. dev->priv = 0;
  91. dev->init = bfin_EMAC_init;
  92. dev->halt = bfin_EMAC_halt;
  93. dev->send = bfin_EMAC_send;
  94. dev->recv = bfin_EMAC_recv;
  95. dev->write_hwaddr = bfin_EMAC_setup_addr;
  96. eth_register(dev);
  97. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  98. miiphy_register(dev->name, bfin_miiphy_read, bfin_miiphy_write);
  99. #endif
  100. return 0;
  101. }
  102. static int bfin_EMAC_send(struct eth_device *dev, volatile void *packet,
  103. int length)
  104. {
  105. int i;
  106. int result = 0;
  107. unsigned int *buf;
  108. buf = (unsigned int *)packet;
  109. if (length <= 0) {
  110. printf("Ethernet: bad packet size: %d\n", length);
  111. goto out;
  112. }
  113. if (bfin_read_DMA2_IRQ_STATUS() & DMA_ERR) {
  114. printf("Ethernet: tx DMA error\n");
  115. goto out;
  116. }
  117. for (i = 0; (bfin_read_DMA2_IRQ_STATUS() & DMA_RUN); ++i) {
  118. if (i > TOUT_LOOP) {
  119. puts("Ethernet: tx time out\n");
  120. goto out;
  121. }
  122. }
  123. txbuf[txIdx]->FrmData->NoBytes = length;
  124. memcpy(txbuf[txIdx]->FrmData->Dest, (void *)packet, length);
  125. txbuf[txIdx]->Dma[0].START_ADDR = (u32) txbuf[txIdx]->FrmData;
  126. bfin_write_DMA2_NEXT_DESC_PTR(txbuf[txIdx]->Dma);
  127. bfin_write_DMA2_CONFIG(txdmacfg.data);
  128. bfin_write_EMAC_OPMODE(bfin_read_EMAC_OPMODE() | TE);
  129. for (i = 0; (txbuf[txIdx]->StatusWord & TX_COMP) == 0; i++) {
  130. if (i > TOUT_LOOP) {
  131. puts("Ethernet: tx error\n");
  132. goto out;
  133. }
  134. }
  135. result = txbuf[txIdx]->StatusWord;
  136. txbuf[txIdx]->StatusWord = 0;
  137. if ((txIdx + 1) >= TX_BUF_CNT)
  138. txIdx = 0;
  139. else
  140. txIdx++;
  141. out:
  142. debug("BFIN EMAC send: length = %d\n", length);
  143. return result;
  144. }
  145. static int bfin_EMAC_recv(struct eth_device *dev)
  146. {
  147. int length = 0;
  148. for (;;) {
  149. if ((rxbuf[rxIdx]->StatusWord & RX_COMP) == 0) {
  150. length = -1;
  151. break;
  152. }
  153. if ((rxbuf[rxIdx]->StatusWord & RX_DMAO) != 0) {
  154. printf("Ethernet: rx dma overrun\n");
  155. break;
  156. }
  157. if ((rxbuf[rxIdx]->StatusWord & RX_OK) == 0) {
  158. printf("Ethernet: rx error\n");
  159. break;
  160. }
  161. length = rxbuf[rxIdx]->StatusWord & 0x000007FF;
  162. if (length <= 4) {
  163. printf("Ethernet: bad frame\n");
  164. break;
  165. }
  166. debug("%s: len = %d\n", __func__, length - 4);
  167. NetRxPackets[rxIdx] =
  168. (volatile uchar *)(rxbuf[rxIdx]->FrmData->Dest);
  169. NetReceive(NetRxPackets[rxIdx], length - 4);
  170. bfin_write_DMA1_IRQ_STATUS(DMA_DONE | DMA_ERR);
  171. rxbuf[rxIdx]->StatusWord = 0x00000000;
  172. if ((rxIdx + 1) >= PKTBUFSRX)
  173. rxIdx = 0;
  174. else
  175. rxIdx++;
  176. }
  177. return length;
  178. }
  179. /**************************************************************
  180. *
  181. * Ethernet Initialization Routine
  182. *
  183. *************************************************************/
  184. /* MDC = SCLK / MDC_freq / 2 - 1 */
  185. #define MDC_FREQ_TO_DIV(mdc_freq) (get_sclk() / (mdc_freq) / 2 - 1)
  186. #ifndef CONFIG_BFIN_MAC_PINS
  187. # ifdef CONFIG_RMII
  188. # define CONFIG_BFIN_MAC_PINS P_RMII0
  189. # else
  190. # define CONFIG_BFIN_MAC_PINS P_MII0
  191. # endif
  192. #endif
  193. static int bfin_miiphy_init(struct eth_device *dev, int *opmode)
  194. {
  195. const unsigned short pins[] = CONFIG_BFIN_MAC_PINS;
  196. u16 phydat;
  197. size_t count;
  198. /* Enable PHY output */
  199. bfin_write_VR_CTL(bfin_read_VR_CTL() | CLKBUFOE);
  200. /* Set all the pins to peripheral mode */
  201. peripheral_request_list(pins, "bfin_mac");
  202. /* Odd word alignment for Receive Frame DMA word */
  203. /* Configure checksum support and rcve frame word alignment */
  204. bfin_write_EMAC_SYSCTL(RXDWA | RXCKS | SET_MDCDIV(MDC_FREQ_TO_DIV(CONFIG_PHY_CLOCK_FREQ)));
  205. /* turn on auto-negotiation and wait for link to come up */
  206. bfin_miiphy_write(dev->name, CONFIG_PHY_ADDR, MII_BMCR, BMCR_ANENABLE);
  207. count = 0;
  208. while (1) {
  209. ++count;
  210. if (bfin_miiphy_read(dev->name, CONFIG_PHY_ADDR, MII_BMSR, &phydat))
  211. return -1;
  212. if (phydat & BMSR_LSTATUS)
  213. break;
  214. if (count > 30000) {
  215. printf("%s: link down, check cable\n", dev->name);
  216. return -1;
  217. }
  218. udelay(100);
  219. }
  220. /* see what kind of link we have */
  221. if (bfin_miiphy_read(dev->name, CONFIG_PHY_ADDR, MII_LPA, &phydat))
  222. return -1;
  223. if (phydat & LPA_DUPLEX)
  224. *opmode = FDMODE;
  225. else
  226. *opmode = 0;
  227. bfin_write_EMAC_MMC_CTL(RSTC | CROLL);
  228. /* Initialize the TX DMA channel registers */
  229. bfin_write_DMA2_X_COUNT(0);
  230. bfin_write_DMA2_X_MODIFY(4);
  231. bfin_write_DMA2_Y_COUNT(0);
  232. bfin_write_DMA2_Y_MODIFY(0);
  233. /* Initialize the RX DMA channel registers */
  234. bfin_write_DMA1_X_COUNT(0);
  235. bfin_write_DMA1_X_MODIFY(4);
  236. bfin_write_DMA1_Y_COUNT(0);
  237. bfin_write_DMA1_Y_MODIFY(0);
  238. return 0;
  239. }
  240. static int bfin_EMAC_setup_addr(struct eth_device *dev)
  241. {
  242. bfin_write_EMAC_ADDRLO(
  243. dev->enetaddr[0] |
  244. dev->enetaddr[1] << 8 |
  245. dev->enetaddr[2] << 16 |
  246. dev->enetaddr[3] << 24
  247. );
  248. bfin_write_EMAC_ADDRHI(
  249. dev->enetaddr[4] |
  250. dev->enetaddr[5] << 8
  251. );
  252. return 0;
  253. }
  254. static int bfin_EMAC_init(struct eth_device *dev, bd_t *bd)
  255. {
  256. u32 opmode;
  257. int dat;
  258. int i;
  259. debug("Eth_init: ......\n");
  260. txIdx = 0;
  261. rxIdx = 0;
  262. /* Initialize System Register */
  263. if (bfin_miiphy_init(dev, &dat) < 0)
  264. return -1;
  265. /* Initialize EMAC address */
  266. bfin_EMAC_setup_addr(dev);
  267. /* Initialize TX and RX buffer */
  268. for (i = 0; i < PKTBUFSRX; i++) {
  269. rxbuf[i] = SetupRxBuffer(i);
  270. if (i > 0) {
  271. rxbuf[i - 1]->Dma[1].NEXT_DESC_PTR = rxbuf[i]->Dma;
  272. if (i == (PKTBUFSRX - 1))
  273. rxbuf[i]->Dma[1].NEXT_DESC_PTR = rxbuf[0]->Dma;
  274. }
  275. }
  276. for (i = 0; i < TX_BUF_CNT; i++) {
  277. txbuf[i] = SetupTxBuffer(i);
  278. if (i > 0) {
  279. txbuf[i - 1]->Dma[1].NEXT_DESC_PTR = txbuf[i]->Dma;
  280. if (i == (TX_BUF_CNT - 1))
  281. txbuf[i]->Dma[1].NEXT_DESC_PTR = txbuf[0]->Dma;
  282. }
  283. }
  284. /* Set RX DMA */
  285. bfin_write_DMA1_NEXT_DESC_PTR(rxbuf[0]->Dma);
  286. bfin_write_DMA1_CONFIG(rxbuf[0]->Dma[0].CONFIG_DATA);
  287. /* Wait MII done */
  288. bfin_miiphy_wait();
  289. /* We enable only RX here */
  290. /* ASTP : Enable Automatic Pad Stripping
  291. PR : Promiscuous Mode for test
  292. PSF : Receive frames with total length less than 64 bytes.
  293. FDMODE : Full Duplex Mode
  294. LB : Internal Loopback for test
  295. RE : Receiver Enable */
  296. if (dat == FDMODE)
  297. opmode = ASTP | FDMODE | PSF;
  298. else
  299. opmode = ASTP | PSF;
  300. opmode |= RE;
  301. #ifdef CONFIG_RMII
  302. opmode |= TE | RMII;
  303. #endif
  304. /* Turn on the EMAC */
  305. bfin_write_EMAC_OPMODE(opmode);
  306. return 0;
  307. }
  308. static void bfin_EMAC_halt(struct eth_device *dev)
  309. {
  310. debug("Eth_halt: ......\n");
  311. /* Turn off the EMAC */
  312. bfin_write_EMAC_OPMODE(0);
  313. /* Turn off the EMAC RX DMA */
  314. bfin_write_DMA1_CONFIG(0);
  315. bfin_write_DMA2_CONFIG(0);
  316. }
  317. ADI_ETHER_BUFFER *SetupRxBuffer(int no)
  318. {
  319. ADI_ETHER_FRAME_BUFFER *frmbuf;
  320. ADI_ETHER_BUFFER *buf;
  321. int nobytes_buffer = sizeof(ADI_ETHER_BUFFER[2]) / 2; /* ensure a multi. of 4 */
  322. int total_size = nobytes_buffer + RECV_BUFSIZE;
  323. buf = (void *) (RXBUF_BASE_ADDR + no * total_size);
  324. frmbuf = (void *) (RXBUF_BASE_ADDR + no * total_size + nobytes_buffer);
  325. memset(buf, 0x00, nobytes_buffer);
  326. buf->FrmData = frmbuf;
  327. memset(frmbuf, 0xfe, RECV_BUFSIZE);
  328. /* set up first desc to point to receive frame buffer */
  329. buf->Dma[0].NEXT_DESC_PTR = &(buf->Dma[1]);
  330. buf->Dma[0].START_ADDR = (u32) buf->FrmData;
  331. buf->Dma[0].CONFIG.b_DMA_EN = 1; /* enabled */
  332. buf->Dma[0].CONFIG.b_WNR = 1; /* Write to memory */
  333. buf->Dma[0].CONFIG.b_WDSIZE = 2; /* wordsize is 32 bits */
  334. buf->Dma[0].CONFIG.b_NDSIZE = 5; /* 5 half words is desc size. */
  335. buf->Dma[0].CONFIG.b_FLOW = 7; /* large desc flow */
  336. /* set up second desc to point to status word */
  337. buf->Dma[1].NEXT_DESC_PTR = buf->Dma;
  338. buf->Dma[1].START_ADDR = (u32) & buf->IPHdrChksum;
  339. buf->Dma[1].CONFIG.b_DMA_EN = 1; /* enabled */
  340. buf->Dma[1].CONFIG.b_WNR = 1; /* Write to memory */
  341. buf->Dma[1].CONFIG.b_WDSIZE = 2; /* wordsize is 32 bits */
  342. buf->Dma[1].CONFIG.b_DI_EN = 1; /* enable interrupt */
  343. buf->Dma[1].CONFIG.b_NDSIZE = 5; /* must be 0 when FLOW is 0 */
  344. buf->Dma[1].CONFIG.b_FLOW = 7; /* stop */
  345. return buf;
  346. }
  347. ADI_ETHER_BUFFER *SetupTxBuffer(int no)
  348. {
  349. ADI_ETHER_FRAME_BUFFER *frmbuf;
  350. ADI_ETHER_BUFFER *buf;
  351. int nobytes_buffer = sizeof(ADI_ETHER_BUFFER[2]) / 2; /* ensure a multi. of 4 */
  352. int total_size = nobytes_buffer + RECV_BUFSIZE;
  353. buf = (void *) (TXBUF_BASE_ADDR + no * total_size);
  354. frmbuf = (void *) (TXBUF_BASE_ADDR + no * total_size + nobytes_buffer);
  355. memset(buf, 0x00, nobytes_buffer);
  356. buf->FrmData = frmbuf;
  357. memset(frmbuf, 0x00, RECV_BUFSIZE);
  358. /* set up first desc to point to receive frame buffer */
  359. buf->Dma[0].NEXT_DESC_PTR = &(buf->Dma[1]);
  360. buf->Dma[0].START_ADDR = (u32) buf->FrmData;
  361. buf->Dma[0].CONFIG.b_DMA_EN = 1; /* enabled */
  362. buf->Dma[0].CONFIG.b_WNR = 0; /* Read to memory */
  363. buf->Dma[0].CONFIG.b_WDSIZE = 2; /* wordsize is 32 bits */
  364. buf->Dma[0].CONFIG.b_NDSIZE = 5; /* 5 half words is desc size. */
  365. buf->Dma[0].CONFIG.b_FLOW = 7; /* large desc flow */
  366. /* set up second desc to point to status word */
  367. buf->Dma[1].NEXT_DESC_PTR = &(buf->Dma[0]);
  368. buf->Dma[1].START_ADDR = (u32) & buf->StatusWord;
  369. buf->Dma[1].CONFIG.b_DMA_EN = 1; /* enabled */
  370. buf->Dma[1].CONFIG.b_WNR = 1; /* Write to memory */
  371. buf->Dma[1].CONFIG.b_WDSIZE = 2; /* wordsize is 32 bits */
  372. buf->Dma[1].CONFIG.b_DI_EN = 1; /* enable interrupt */
  373. buf->Dma[1].CONFIG.b_NDSIZE = 0; /* must be 0 when FLOW is 0 */
  374. buf->Dma[1].CONFIG.b_FLOW = 0; /* stop */
  375. return buf;
  376. }
  377. #if defined(CONFIG_POST) && defined(CONFIG_SYS_POST_ETHER)
  378. int ether_post_test(int flags)
  379. {
  380. uchar buf[64];
  381. int i, value = 0;
  382. int length;
  383. uint addr;
  384. printf("\n--------");
  385. bfin_EMAC_init(NULL, NULL);
  386. /* construct the package */
  387. addr = bfin_read_EMAC_ADDRLO();
  388. buf[0] = buf[6] = addr;
  389. buf[1] = buf[7] = addr >> 8;
  390. buf[2] = buf[8] = addr >> 16;
  391. buf[3] = buf[9] = addr >> 24;
  392. addr = bfin_read_EMAC_ADDRHI();
  393. buf[4] = buf[10] = addr;
  394. buf[5] = buf[11] = addr >> 8;
  395. buf[12] = 0x08; /* Type: ARP */
  396. buf[13] = 0x06;
  397. buf[14] = 0x00; /* Hardware type: Ethernet */
  398. buf[15] = 0x01;
  399. buf[16] = 0x08; /* Protocal type: IP */
  400. buf[17] = 0x00;
  401. buf[18] = 0x06; /* Hardware size */
  402. buf[19] = 0x04; /* Protocol size */
  403. buf[20] = 0x00; /* Opcode: request */
  404. buf[21] = 0x01;
  405. for (i = 0; i < 42; i++)
  406. buf[i + 22] = i;
  407. printf("--------Send 64 bytes......\n");
  408. bfin_EMAC_send(NULL, (volatile void *)buf, 64);
  409. for (i = 0; i < 100; i++) {
  410. udelay(10000);
  411. if ((rxbuf[rxIdx]->StatusWord & RX_COMP) != 0) {
  412. value = 1;
  413. break;
  414. }
  415. }
  416. if (value == 0) {
  417. printf("--------EMAC can't receive any data\n");
  418. eth_halt();
  419. return -1;
  420. }
  421. length = rxbuf[rxIdx]->StatusWord & 0x000007FF - 4;
  422. for (i = 0; i < length; i++) {
  423. if (rxbuf[rxIdx]->FrmData->Dest[i] != buf[i]) {
  424. printf("--------EMAC receive error data!\n");
  425. eth_halt();
  426. return -1;
  427. }
  428. }
  429. printf("--------receive %d bytes, matched\n", length);
  430. bfin_EMAC_halt(NULL);
  431. return 0;
  432. }
  433. #endif