bfin_mac.c 12 KB

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