macb.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. /*
  2. * File : macb.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006, RT-Thread Develop Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2011-03-18 weety first version
  13. */
  14. #include <rtthread.h>
  15. #include <netif/ethernetif.h>
  16. #include "lwipopts.h"
  17. #include <at91sam926x.h>
  18. #include "macb.h"
  19. #define CONFIG_RMII
  20. #define MACB_RX_BUFFER_SIZE 4096*4
  21. #define MACB_RX_RING_SIZE (MACB_RX_BUFFER_SIZE / 128)
  22. #define MACB_TX_RING_SIZE 16
  23. #define MACB_TX_TIMEOUT 1000
  24. #define MACB_AUTONEG_TIMEOUT 5000000
  25. #define MACB_LINK_TIMEOUT 500000
  26. struct macb_dma_desc {
  27. rt_uint32_t addr;
  28. rt_uint32_t ctrl;
  29. };
  30. #define RXADDR_USED 0x00000001
  31. #define RXADDR_WRAP 0x00000002
  32. #define RXBUF_FRMLEN_MASK 0x00000fff
  33. #define RXBUF_FRAME_START 0x00004000
  34. #define RXBUF_FRAME_END 0x00008000
  35. #define RXBUF_TYPEID_MATCH 0x00400000
  36. #define RXBUF_ADDR4_MATCH 0x00800000
  37. #define RXBUF_ADDR3_MATCH 0x01000000
  38. #define RXBUF_ADDR2_MATCH 0x02000000
  39. #define RXBUF_ADDR1_MATCH 0x04000000
  40. #define RXBUF_BROADCAST 0x80000000
  41. #define TXBUF_FRMLEN_MASK 0x000007ff
  42. #define TXBUF_FRAME_END 0x00008000
  43. #define TXBUF_NOCRC 0x00010000
  44. #define TXBUF_EXHAUSTED 0x08000000
  45. #define TXBUF_UNDERRUN 0x10000000
  46. #define TXBUF_MAXRETRY 0x20000000
  47. #define TXBUF_WRAP 0x40000000
  48. #define TXBUF_USED 0x80000000
  49. #define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(RXUBR) \
  50. | MACB_BIT(ISR_ROVR))
  51. #define MAX_ADDR_LEN 6
  52. struct rt_macb_eth
  53. {
  54. /* inherit from ethernet device */
  55. struct eth_device parent;
  56. void *regs;
  57. unsigned int rx_tail;
  58. unsigned int tx_head;
  59. unsigned int tx_tail;
  60. void *rx_buffer;
  61. void *tx_buffer;
  62. struct macb_dma_desc *rx_ring;
  63. struct macb_dma_desc *tx_ring;
  64. unsigned long rx_buffer_dma;
  65. unsigned long rx_ring_dma;
  66. unsigned long tx_ring_dma;
  67. /* interface address info. */
  68. rt_uint8_t dev_addr[MAX_ADDR_LEN]; /* hw address */
  69. unsigned short phy_addr;
  70. };
  71. static struct rt_macb_eth macb_device;
  72. static struct rt_semaphore sem_ack, sem_lock;
  73. static void udelay(rt_uint32_t us)
  74. {
  75. rt_uint32_t len;
  76. for (;us > 0; us --)
  77. for (len = 0; len < 10; len++ );
  78. }
  79. static void rt_macb_isr(int irq)
  80. {
  81. struct rt_macb_eth *macb = &macb_device;
  82. rt_device_t dev = &(macb->parent.parent);
  83. rt_uint32_t status, rsr, tsr;
  84. //rt_kprintf("%s:irq enter\n", __func__);
  85. status = macb_readl(macb, ISR);
  86. while (status) {
  87. if (status & MACB_RX_INT_FLAGS) {
  88. //rt_kprintf("macb recv isr\n");
  89. //macb_writel(macb, IDR, MACB_RX_INT_FLAGS);
  90. rsr = macb_readl(macb, RSR);
  91. macb_writel(macb, RSR, rsr);
  92. /* a frame has been received */
  93. eth_device_ready(&(macb_device.parent));
  94. }
  95. if (status & (MACB_BIT(TCOMP) | MACB_BIT(ISR_TUND) |
  96. MACB_BIT(ISR_RLE)))
  97. {
  98. //rt_kprintf("macb tx complete\n");
  99. tsr = macb_readl(macb, TSR);
  100. macb_writel(macb, TSR, tsr);
  101. /* One packet sent complete */
  102. rt_sem_release(&sem_ack);
  103. }
  104. //macb_tx(bp);
  105. /*
  106. * Link change detection isn't possible with RMII, so we'll
  107. * add that if/when we get our hands on a full-blown MII PHY.
  108. */
  109. if (status & MACB_BIT(HRESP)) {
  110. /*
  111. * TODO: Reset the hardware, and maybe move the printk
  112. * to a lower-priority context as well (work queue?)
  113. */
  114. rt_kprintf("%s: DMA bus error: HRESP not OK\n",
  115. dev->parent.name);
  116. }
  117. status = macb_readl(macb, ISR);
  118. }
  119. }
  120. static void macb_mdio_write(struct rt_macb_eth *macb, rt_uint8_t reg, rt_uint16_t value)
  121. {
  122. unsigned long netctl;
  123. unsigned long netstat;
  124. unsigned long frame;
  125. netctl = macb_readl(macb, NCR);
  126. netctl |= MACB_BIT(MPE);
  127. macb_writel(macb, NCR, netctl);
  128. frame = (MACB_BF(SOF, 1)
  129. | MACB_BF(RW, 1)
  130. | MACB_BF(PHYA, macb->phy_addr)
  131. | MACB_BF(REGA, reg)
  132. | MACB_BF(CODE, 2)
  133. | MACB_BF(DATA, value));
  134. macb_writel(macb, MAN, frame);
  135. do {
  136. netstat = macb_readl(macb, NSR);
  137. } while (!(netstat & MACB_BIT(IDLE)));
  138. netctl = macb_readl(macb, NCR);
  139. netctl &= ~MACB_BIT(MPE);
  140. macb_writel(macb, NCR, netctl);
  141. }
  142. static rt_uint16_t macb_mdio_read(struct rt_macb_eth *macb, rt_uint8_t reg)
  143. {
  144. unsigned long netctl;
  145. unsigned long netstat;
  146. unsigned long frame;
  147. netctl = macb_readl(macb, NCR);
  148. netctl |= MACB_BIT(MPE);
  149. macb_writel(macb, NCR, netctl);
  150. frame = (MACB_BF(SOF, 1)
  151. | MACB_BF(RW, 2)
  152. | MACB_BF(PHYA, macb->phy_addr)
  153. | MACB_BF(REGA, reg)
  154. | MACB_BF(CODE, 2));
  155. macb_writel(macb, MAN, frame);
  156. do {
  157. netstat = macb_readl(macb, NSR);
  158. } while (!(netstat & MACB_BIT(IDLE)));
  159. frame = macb_readl(macb, MAN);
  160. netctl = macb_readl(macb, NCR);
  161. netctl &= ~MACB_BIT(MPE);
  162. macb_writel(macb, NCR, netctl);
  163. return MACB_BFEXT(DATA, frame);
  164. }
  165. static void macb_phy_reset(rt_device_t dev)
  166. {
  167. struct rt_macb_eth *macb = dev->user_data;;
  168. int i;
  169. rt_uint16_t status, adv;
  170. adv = ADVERTISE_CSMA | ADVERTISE_ALL;
  171. macb_mdio_write(macb, MII_ADVERTISE, adv);
  172. rt_kprintf("%s: Starting autonegotiation...\n", dev->parent.name);
  173. macb_mdio_write(macb, MII_BMCR, (BMCR_ANENABLE
  174. | BMCR_ANRESTART));
  175. for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
  176. status = macb_mdio_read(macb, MII_BMSR);
  177. if (status & BMSR_ANEGCOMPLETE)
  178. break;
  179. udelay(100);
  180. }
  181. if (status & BMSR_ANEGCOMPLETE)
  182. rt_kprintf("%s: Autonegotiation complete\n", dev->parent.name);
  183. else
  184. rt_kprintf("%s: Autonegotiation timed out (status=0x%04x)\n",
  185. dev->parent.name, status);
  186. }
  187. static int macb_phy_init(rt_device_t dev)
  188. {
  189. //struct eth_device *netdev = &macb->netdev;
  190. struct rt_macb_eth *macb = dev->user_data;
  191. rt_uint32_t ncfgr;
  192. rt_uint16_t phy_id, status, adv, lpa;
  193. int media, speed, duplex;
  194. int i;
  195. /* Check if the PHY is up to snuff... */
  196. phy_id = macb_mdio_read(macb, MII_PHYSID1);
  197. if (phy_id == 0xffff) {
  198. rt_kprintf("%s: No PHY present\n", dev->parent.name);
  199. return 0;
  200. }
  201. status = macb_mdio_read(macb, MII_BMSR);
  202. if (!(status & BMSR_LSTATUS)) {
  203. /* Try to re-negotiate if we don't have link already. */
  204. macb_phy_reset(dev);
  205. for (i = 0; i < MACB_LINK_TIMEOUT / 100; i++) { //modified by luohui@2009-11-27 CFG_MACB_AUTONEG_TIMEOUT
  206. status = macb_mdio_read(macb, MII_BMSR);
  207. if (status & BMSR_LSTATUS)
  208. break;
  209. udelay(100);
  210. }
  211. }
  212. if (!(status & BMSR_LSTATUS)) {
  213. rt_kprintf("%s: link down (status: 0x%04x)\n",
  214. dev->parent.name, status);
  215. return 0;
  216. } else {
  217. adv = macb_mdio_read(macb, MII_ADVERTISE);
  218. lpa = macb_mdio_read(macb, MII_LPA);
  219. media = mii_nway_result(lpa & adv);
  220. speed = (media & (ADVERTISE_100FULL | ADVERTISE_100HALF)
  221. ? 1 : 0);
  222. duplex = (media & ADVERTISE_FULL) ? 1 : 0;
  223. rt_kprintf("%s: link up, %sMbps %s-duplex (lpa: 0x%04x)\n",
  224. dev->parent.name,
  225. speed ? "100" : "10",
  226. duplex ? "full" : "half",
  227. lpa);
  228. ncfgr = macb_readl(macb, NCFGR);
  229. ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
  230. if (speed)
  231. ncfgr |= MACB_BIT(SPD);
  232. if (duplex)
  233. ncfgr |= MACB_BIT(FD);
  234. macb_writel(macb, NCFGR, ncfgr);
  235. return 1;
  236. }
  237. }
  238. /* RT-Thread Device Interface */
  239. /* initialize the interface */
  240. static rt_err_t rt_macb_init(rt_device_t dev)
  241. {
  242. struct rt_macb_eth *macb = dev->user_data;
  243. unsigned long paddr;
  244. rt_uint32_t hwaddr_bottom;
  245. rt_uint16_t hwaddr_top;
  246. int i;
  247. /*
  248. * macb_halt should have been called at some point before now,
  249. * so we'll assume the controller is idle.
  250. */
  251. /* initialize DMA descriptors */
  252. paddr = macb->rx_buffer_dma;
  253. for (i = 0; i < MACB_RX_RING_SIZE; i++) {
  254. if (i == (MACB_RX_RING_SIZE - 1))
  255. paddr |= RXADDR_WRAP;
  256. macb->rx_ring[i].addr = paddr;
  257. macb->rx_ring[i].ctrl = 0;
  258. paddr += 128;
  259. }
  260. for (i = 0; i < MACB_TX_RING_SIZE; i++) {
  261. macb->tx_ring[i].addr = 0;
  262. if (i == (MACB_TX_RING_SIZE - 1))
  263. macb->tx_ring[i].ctrl = TXBUF_USED | TXBUF_WRAP;
  264. else
  265. macb->tx_ring[i].ctrl = TXBUF_USED;
  266. }
  267. macb->rx_tail = macb->tx_head = macb->tx_tail = 0;
  268. macb_writel(macb, RBQP, macb->rx_ring_dma);
  269. macb_writel(macb, TBQP, macb->tx_ring_dma);
  270. /* set hardware address */
  271. //hwaddr_bottom = cpu_to_le32(*((u32 *)netdev->enetaddr));
  272. hwaddr_bottom = (*((rt_uint32_t *)macb->dev_addr));
  273. macb_writel(macb, SA1B, hwaddr_bottom);
  274. //hwaddr_top = cpu_to_le16(*((u16 *)(netdev->enetaddr + 4)));
  275. hwaddr_top = (*((rt_uint16_t *)(macb->dev_addr + 4)));
  276. macb_writel(macb, SA1T, hwaddr_top);
  277. /* choose RMII or MII mode. This depends on the board */
  278. #ifdef CONFIG_RMII
  279. #if defined(CONFIG_AVR32)
  280. macb_writel(macb, USRIO, 0);
  281. #else
  282. macb_writel(macb, USRIO, MACB_BIT(RMII) | MACB_BIT(CLKEN));
  283. #endif
  284. #else
  285. #if defined(CONFIG_AVR32)
  286. macb_writel(macb, USRIO, MACB_BIT(MII));
  287. #else
  288. macb_writel(macb, USRIO, MACB_BIT(CLKEN));
  289. #endif
  290. #endif /* CONFIG_RMII */
  291. if (!macb_phy_init(dev))
  292. return -RT_ERROR;
  293. /* Enable TX and RX */
  294. macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE) | MACB_BIT(MPE));
  295. /* Enable interrupts */
  296. macb_writel(macb, IER, (MACB_BIT(RCOMP)
  297. | MACB_BIT(RXUBR)
  298. | MACB_BIT(ISR_TUND)
  299. | MACB_BIT(ISR_RLE)
  300. | MACB_BIT(TXERR)
  301. | MACB_BIT(TCOMP)
  302. | MACB_BIT(ISR_ROVR)
  303. | MACB_BIT(HRESP)));
  304. /* instal interrupt */
  305. rt_hw_interrupt_install(AT91SAM9260_ID_EMAC, rt_macb_isr, RT_NULL);
  306. rt_hw_interrupt_umask(AT91SAM9260_ID_EMAC);
  307. return RT_EOK;
  308. }
  309. static rt_err_t rt_macb_open(rt_device_t dev, rt_uint16_t oflag)
  310. {
  311. return RT_EOK;
  312. }
  313. static rt_err_t rt_macb_close(rt_device_t dev)
  314. {
  315. return RT_EOK;
  316. }
  317. static rt_size_t rt_macb_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  318. {
  319. rt_set_errno(-RT_ENOSYS);
  320. return 0;
  321. }
  322. static rt_size_t rt_macb_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  323. {
  324. rt_set_errno(-RT_ENOSYS);
  325. return 0;
  326. }
  327. static rt_err_t rt_macb_control(rt_device_t dev, rt_uint8_t cmd, void *args)
  328. {
  329. switch(cmd)
  330. {
  331. case NIOCTL_GADDR:
  332. /* get mac address */
  333. if(args) rt_memcpy(args, macb_device.dev_addr, 6);
  334. else return -RT_ERROR;
  335. break;
  336. default :
  337. break;
  338. }
  339. return RT_EOK;
  340. }
  341. /* ethernet device interface */
  342. /* transmit packet. */
  343. rt_err_t rt_macb_tx( rt_device_t dev, struct pbuf* p)
  344. {
  345. struct rt_macb_eth *macb = dev->user_data;
  346. struct pbuf* q;
  347. rt_uint32_t len;
  348. rt_uint8_t* bufptr, *buf = RT_NULL;
  349. unsigned long paddr, ctrl;
  350. unsigned int tx_head = macb->tx_head;
  351. int i;
  352. /* lock macb device */
  353. rt_sem_take(&sem_lock, RT_WAITING_FOREVER);
  354. //rt_kprintf("macb tx enter, packetlen=%d\n", p->tot_len);
  355. buf = rt_malloc(p->tot_len);
  356. if (!buf) {
  357. rt_kprintf("%s:alloc buf failed\n", __func__);
  358. return NULL;
  359. }
  360. bufptr = buf;
  361. for (q = p; q != NULL; q = q->next)
  362. {
  363. //len = q->len;
  364. //paddr = (unsigned long)q->payload;
  365. memcpy(bufptr, q->payload, q->len);
  366. bufptr += q->len;
  367. /* write data to device */
  368. /*ctrl = len & TXBUF_FRMLEN_MASK;
  369. ctrl |= TXBUF_FRAME_END;
  370. if (tx_head == (MACB_TX_RING_SIZE - 1)) {
  371. ctrl |= TXBUF_WRAP;
  372. macb->tx_head = 0;
  373. } else
  374. macb->tx_head++;
  375. macb->tx_ring[tx_head].ctrl = ctrl;
  376. macb->tx_ring[tx_head].addr = paddr;
  377. macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE) | MACB_BIT(TSTART));*/
  378. /*
  379. * I guess this is necessary because the networking core may
  380. * re-use the transmit buffer as soon as we return...
  381. */
  382. }
  383. ctrl = p->tot_len & TXBUF_FRMLEN_MASK;
  384. ctrl |= TXBUF_FRAME_END;
  385. if (tx_head == (MACB_TX_RING_SIZE - 1)) {
  386. ctrl |= TXBUF_WRAP;
  387. macb->tx_head = 0;
  388. } else
  389. macb->tx_head++;
  390. macb->tx_ring[tx_head].ctrl = ctrl;
  391. macb->tx_ring[tx_head].addr = (rt_uint32_t)buf;
  392. macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE) | MACB_BIT(TSTART));
  393. //rt_uint32_t netcr = macb_readl(macb, NCR); //e0: DMA bus error: HRESP not OK
  394. //rt_kprintf("NCR = 0x%08x\n");
  395. //macb_writel(macb, NCR, netcr | MACB_BIT(TE) | MACB_BIT(RE) | MACB_BIT(TSTART));
  396. /* unlock macb device */
  397. rt_sem_release(&sem_lock);
  398. /* wait ack */
  399. rt_sem_take(&sem_ack, RT_WAITING_FOREVER);
  400. rt_free(buf);
  401. buf == RT_NULL;
  402. return RT_EOK;
  403. }
  404. static void reclaim_rx_buffers(struct rt_macb_eth *macb,
  405. unsigned int new_tail)
  406. {
  407. unsigned int i;
  408. i = macb->rx_tail;
  409. while (i > new_tail) {
  410. macb->rx_ring[i].addr &= ~RXADDR_USED;
  411. i++;
  412. if (i > MACB_RX_RING_SIZE)
  413. i = 0;
  414. }
  415. while (i < new_tail) {
  416. macb->rx_ring[i].addr &= ~RXADDR_USED;
  417. i++;
  418. }
  419. //barrier();
  420. macb->rx_tail = new_tail;
  421. }
  422. /* reception packet. */
  423. struct pbuf *rt_macb_rx(rt_device_t dev)
  424. {
  425. struct rt_macb_eth *macb = dev->user_data;
  426. struct pbuf* p = RT_NULL;
  427. rt_uint32_t len;
  428. unsigned int rx_tail = macb->rx_tail;
  429. void *buffer;
  430. //int length;
  431. int wrapped = 0;
  432. rt_uint32_t status;
  433. //rt_uint8_t* data;
  434. struct pbuf* q;
  435. rt_uint8_t *buf = RT_NULL;
  436. //rt_kprintf("macb rx enter\n");
  437. /* lock macb device */
  438. rt_sem_take(&sem_lock, RT_WAITING_FOREVER);
  439. //rt_kprintf("macb rx enter2\n");
  440. for (;;) {
  441. if (!(macb->rx_ring[rx_tail].addr & RXADDR_USED))
  442. break;
  443. status = macb->rx_ring[rx_tail].ctrl;
  444. if (status & RXBUF_FRAME_START) {
  445. if (rx_tail != macb->rx_tail)
  446. reclaim_rx_buffers(macb, rx_tail);
  447. wrapped = 0;
  448. }
  449. if (status & RXBUF_FRAME_END) {
  450. buffer = macb->rx_buffer + 128 * macb->rx_tail;
  451. len = status & RXBUF_FRMLEN_MASK;
  452. //rt_kprintf("%s:recv %d bytes\n", __func__, len);
  453. p = pbuf_alloc(PBUF_LINK, len, PBUF_RAM);
  454. if (!p)
  455. {
  456. rt_kprintf("alloc pbuf failed\n");
  457. break;
  458. }
  459. if (wrapped) {
  460. unsigned int headlen, taillen;
  461. buf = rt_malloc(len);
  462. if (!buf)
  463. {
  464. rt_kprintf("%s:alloc memory failed\n", __func__);
  465. pbuf_free(p);
  466. p = RT_NULL;
  467. break;
  468. }
  469. headlen = 128 * (MACB_RX_RING_SIZE
  470. - macb->rx_tail);
  471. taillen = len - headlen;
  472. memcpy((void *)buf,
  473. buffer, headlen);
  474. memcpy((void *)buf + headlen,
  475. macb->rx_buffer, taillen);
  476. buffer = (void *)buf;
  477. for (q = p; q != RT_NULL; q= q->next)
  478. {
  479. /* read data from device */
  480. memcpy((void *)q->payload, buffer, q->len);
  481. buffer += q->len;
  482. }
  483. rt_free(buf);
  484. buf = RT_NULL;
  485. } else {
  486. for (q = p; q != RT_NULL; q= q->next)
  487. {
  488. /* read data from device */
  489. memcpy((void *)q->payload, buffer, q->len);
  490. buffer += q->len;
  491. }
  492. }
  493. //NetReceive(buffer, length);
  494. /* allocate buffer */
  495. if (++rx_tail >= MACB_RX_RING_SIZE)
  496. rx_tail = 0;
  497. reclaim_rx_buffers(macb, rx_tail);
  498. break;
  499. } else {
  500. if (++rx_tail >= MACB_RX_RING_SIZE) {
  501. wrapped = 1;
  502. rx_tail = 0;
  503. }
  504. }
  505. }
  506. /* unlock macb device */
  507. rt_sem_release(&sem_lock);
  508. return p;
  509. }
  510. void macb_gpio_init()
  511. {
  512. /* Pins used for MII and RMII */
  513. at91_sys_write(AT91_PIOA + PIO_PDR, (1 << 19)|(1 << 17)|(1 << 14)|(1 << 15)|(1 << 18)|(1 << 16)|(1 << 12)|(1 << 13)|(1 << 21)|(1 << 20));
  514. at91_sys_write(AT91_PIOA + PIO_ASR, (1 << 19)|(1 << 17)|(1 << 14)|(1 << 15)|(1 << 18)|(1 << 16)|(1 << 12)|(1 << 13)|(1 << 21)|(1 << 20));
  515. }
  516. void macb_initialize()
  517. {
  518. struct rt_macb_eth *macb = &macb_device;
  519. unsigned long macb_hz;
  520. rt_uint32_t ncfgr;
  521. macb->rx_buffer = rt_malloc(MACB_RX_BUFFER_SIZE);
  522. macb->rx_ring = rt_malloc(MACB_RX_RING_SIZE * sizeof(struct macb_dma_desc));
  523. macb->tx_ring = rt_malloc(MACB_TX_RING_SIZE * sizeof(struct macb_dma_desc));
  524. macb->rx_buffer_dma = (unsigned long)macb->rx_buffer;
  525. macb->rx_ring_dma = (unsigned long)macb->rx_ring;
  526. macb->tx_ring_dma = (unsigned long)macb->tx_ring;
  527. macb->regs = (void *)AT91SAM9260_BASE_EMAC;
  528. macb->phy_addr = 0x00;
  529. /*
  530. * Do some basic initialization so that we at least can talk
  531. * to the PHY
  532. */
  533. macb_hz = clk_get_rate(clk_get("mck"));
  534. if (macb_hz < 20000000)
  535. ncfgr = MACB_BF(CLK, MACB_CLK_DIV8);
  536. else if (macb_hz < 40000000)
  537. ncfgr = MACB_BF(CLK, MACB_CLK_DIV16);
  538. else if (macb_hz < 80000000)
  539. ncfgr = MACB_BF(CLK, MACB_CLK_DIV32);
  540. else
  541. ncfgr = MACB_BF(CLK, MACB_CLK_DIV64);
  542. macb_writel(macb, NCFGR, ncfgr);
  543. }
  544. void rt_hw_macb_init()
  545. {
  546. at91_sys_write(AT91_PMC + AT91_PMC_PCER, 1 << AT91SAM9260_ID_EMAC); //enable macb clock
  547. macb_gpio_init();
  548. macb_initialize();
  549. rt_sem_init(&sem_ack, "tx_ack", 1, RT_IPC_FLAG_FIFO);
  550. rt_sem_init(&sem_lock, "eth_lock", 1, RT_IPC_FLAG_FIFO);
  551. macb_device.dev_addr[0] = 0x00;
  552. macb_device.dev_addr[1] = 0x60;
  553. macb_device.dev_addr[2] = 0x6E;
  554. macb_device.dev_addr[3] = 0x11;
  555. macb_device.dev_addr[4] = 0x22;
  556. macb_device.dev_addr[5] = 0x33;
  557. macb_device.parent.parent.init = rt_macb_init;
  558. macb_device.parent.parent.open = rt_macb_open;
  559. macb_device.parent.parent.close = rt_macb_close;
  560. macb_device.parent.parent.read = rt_macb_read;
  561. macb_device.parent.parent.write = rt_macb_write;
  562. macb_device.parent.parent.control = rt_macb_control;
  563. macb_device.parent.parent.user_data = &macb_device;
  564. macb_device.parent.eth_rx = rt_macb_rx;
  565. macb_device.parent.eth_tx = rt_macb_tx;
  566. eth_device_init(&(macb_device.parent), "e0");
  567. //eth_system_device_init();
  568. //set_if("192.168.1.30", "192.168.1.1", "255.255.255.0");
  569. /* instal interrupt */
  570. //rt_hw_interrupt_install(AT91SAM9260_ID_EMAC, rt_macb_isr, RT_NULL);
  571. //rt_hw_interrupt_umask(AT91SAM9260_ID_EMAC);
  572. }