bfin_mac.c 13 KB

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