bfin_mac.c 12 KB

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