bfin_mac.c 13 KB

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