bfin_mac.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  1. /*
  2. * File: drivers/net/bfin_mac.c
  3. * Based on:
  4. * Maintainer:
  5. * Bryan Wu <bryan.wu@analog.com>
  6. *
  7. * Original author:
  8. * Luke Yang <luke.yang@analog.com>
  9. *
  10. * Created:
  11. * Description:
  12. *
  13. * Modified:
  14. * Copyright 2004-2006 Analog Devices Inc.
  15. *
  16. * Bugs: Enter bugs at http://blackfin.uclinux.org/
  17. *
  18. * This program is free software ; you can redistribute it and/or modify
  19. * it under the terms of the GNU General Public License as published by
  20. * the Free Software Foundation ; either version 2, or (at your option)
  21. * any later version.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY ; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU General Public License
  29. * along with this program ; see the file COPYING.
  30. * If not, write to the Free Software Foundation,
  31. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  32. */
  33. #include <linux/init.h>
  34. #include <linux/module.h>
  35. #include <linux/kernel.h>
  36. #include <linux/sched.h>
  37. #include <linux/slab.h>
  38. #include <linux/delay.h>
  39. #include <linux/timer.h>
  40. #include <linux/errno.h>
  41. #include <linux/irq.h>
  42. #include <linux/io.h>
  43. #include <linux/ioport.h>
  44. #include <linux/crc32.h>
  45. #include <linux/device.h>
  46. #include <linux/spinlock.h>
  47. #include <linux/ethtool.h>
  48. #include <linux/mii.h>
  49. #include <linux/netdevice.h>
  50. #include <linux/etherdevice.h>
  51. #include <linux/skbuff.h>
  52. #include <linux/platform_device.h>
  53. #include <asm/dma.h>
  54. #include <linux/dma-mapping.h>
  55. #include <asm/blackfin.h>
  56. #include <asm/cacheflush.h>
  57. #include <asm/portmux.h>
  58. #include "bfin_mac.h"
  59. #define DRV_NAME "bfin_mac"
  60. #define DRV_VERSION "1.1"
  61. #define DRV_AUTHOR "Bryan Wu, Luke Yang"
  62. #define DRV_DESC "Blackfin BF53[67] on-chip Ethernet MAC driver"
  63. MODULE_AUTHOR(DRV_AUTHOR);
  64. MODULE_LICENSE("GPL");
  65. MODULE_DESCRIPTION(DRV_DESC);
  66. #if defined(CONFIG_BFIN_MAC_USE_L1)
  67. # define bfin_mac_alloc(dma_handle, size) l1_data_sram_zalloc(size)
  68. # define bfin_mac_free(dma_handle, ptr) l1_data_sram_free(ptr)
  69. #else
  70. # define bfin_mac_alloc(dma_handle, size) \
  71. dma_alloc_coherent(NULL, size, dma_handle, GFP_KERNEL)
  72. # define bfin_mac_free(dma_handle, ptr) \
  73. dma_free_coherent(NULL, sizeof(*ptr), ptr, dma_handle)
  74. #endif
  75. #define PKT_BUF_SZ 1580
  76. #define MAX_TIMEOUT_CNT 500
  77. /* pointers to maintain transmit list */
  78. static struct net_dma_desc_tx *tx_list_head;
  79. static struct net_dma_desc_tx *tx_list_tail;
  80. static struct net_dma_desc_rx *rx_list_head;
  81. static struct net_dma_desc_rx *rx_list_tail;
  82. static struct net_dma_desc_rx *current_rx_ptr;
  83. static struct net_dma_desc_tx *current_tx_ptr;
  84. static struct net_dma_desc_tx *tx_desc;
  85. static struct net_dma_desc_rx *rx_desc;
  86. static void desc_list_free(void)
  87. {
  88. struct net_dma_desc_rx *r;
  89. struct net_dma_desc_tx *t;
  90. int i;
  91. #if !defined(CONFIG_BFIN_MAC_USE_L1)
  92. dma_addr_t dma_handle = 0;
  93. #endif
  94. if (tx_desc) {
  95. t = tx_list_head;
  96. for (i = 0; i < CONFIG_BFIN_TX_DESC_NUM; i++) {
  97. if (t) {
  98. if (t->skb) {
  99. dev_kfree_skb(t->skb);
  100. t->skb = NULL;
  101. }
  102. t = t->next;
  103. }
  104. }
  105. bfin_mac_free(dma_handle, tx_desc);
  106. }
  107. if (rx_desc) {
  108. r = rx_list_head;
  109. for (i = 0; i < CONFIG_BFIN_RX_DESC_NUM; i++) {
  110. if (r) {
  111. if (r->skb) {
  112. dev_kfree_skb(r->skb);
  113. r->skb = NULL;
  114. }
  115. r = r->next;
  116. }
  117. }
  118. bfin_mac_free(dma_handle, rx_desc);
  119. }
  120. }
  121. static int desc_list_init(void)
  122. {
  123. int i;
  124. struct sk_buff *new_skb;
  125. #if !defined(CONFIG_BFIN_MAC_USE_L1)
  126. /*
  127. * This dma_handle is useless in Blackfin dma_alloc_coherent().
  128. * The real dma handler is the return value of dma_alloc_coherent().
  129. */
  130. dma_addr_t dma_handle;
  131. #endif
  132. tx_desc = bfin_mac_alloc(&dma_handle,
  133. sizeof(struct net_dma_desc_tx) *
  134. CONFIG_BFIN_TX_DESC_NUM);
  135. if (tx_desc == NULL)
  136. goto init_error;
  137. rx_desc = bfin_mac_alloc(&dma_handle,
  138. sizeof(struct net_dma_desc_rx) *
  139. CONFIG_BFIN_RX_DESC_NUM);
  140. if (rx_desc == NULL)
  141. goto init_error;
  142. /* init tx_list */
  143. tx_list_head = tx_list_tail = tx_desc;
  144. for (i = 0; i < CONFIG_BFIN_TX_DESC_NUM; i++) {
  145. struct net_dma_desc_tx *t = tx_desc + i;
  146. struct dma_descriptor *a = &(t->desc_a);
  147. struct dma_descriptor *b = &(t->desc_b);
  148. /*
  149. * disable DMA
  150. * read from memory WNR = 0
  151. * wordsize is 32 bits
  152. * 6 half words is desc size
  153. * large desc flow
  154. */
  155. a->config = WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE;
  156. a->start_addr = (unsigned long)t->packet;
  157. a->x_count = 0;
  158. a->next_dma_desc = b;
  159. /*
  160. * enabled DMA
  161. * write to memory WNR = 1
  162. * wordsize is 32 bits
  163. * disable interrupt
  164. * 6 half words is desc size
  165. * large desc flow
  166. */
  167. b->config = DMAEN | WNR | WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE;
  168. b->start_addr = (unsigned long)(&(t->status));
  169. b->x_count = 0;
  170. t->skb = NULL;
  171. tx_list_tail->desc_b.next_dma_desc = a;
  172. tx_list_tail->next = t;
  173. tx_list_tail = t;
  174. }
  175. tx_list_tail->next = tx_list_head; /* tx_list is a circle */
  176. tx_list_tail->desc_b.next_dma_desc = &(tx_list_head->desc_a);
  177. current_tx_ptr = tx_list_head;
  178. /* init rx_list */
  179. rx_list_head = rx_list_tail = rx_desc;
  180. for (i = 0; i < CONFIG_BFIN_RX_DESC_NUM; i++) {
  181. struct net_dma_desc_rx *r = rx_desc + i;
  182. struct dma_descriptor *a = &(r->desc_a);
  183. struct dma_descriptor *b = &(r->desc_b);
  184. /* allocate a new skb for next time receive */
  185. new_skb = dev_alloc_skb(PKT_BUF_SZ + 2);
  186. if (!new_skb) {
  187. printk(KERN_NOTICE DRV_NAME
  188. ": init: low on mem - packet dropped\n");
  189. goto init_error;
  190. }
  191. skb_reserve(new_skb, 2);
  192. r->skb = new_skb;
  193. /*
  194. * enabled DMA
  195. * write to memory WNR = 1
  196. * wordsize is 32 bits
  197. * disable interrupt
  198. * 6 half words is desc size
  199. * large desc flow
  200. */
  201. a->config = DMAEN | WNR | WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE;
  202. /* since RXDWA is enabled */
  203. a->start_addr = (unsigned long)new_skb->data - 2;
  204. a->x_count = 0;
  205. a->next_dma_desc = b;
  206. /*
  207. * enabled DMA
  208. * write to memory WNR = 1
  209. * wordsize is 32 bits
  210. * enable interrupt
  211. * 6 half words is desc size
  212. * large desc flow
  213. */
  214. b->config = DMAEN | WNR | WDSIZE_32 | DI_EN |
  215. NDSIZE_6 | DMAFLOW_LARGE;
  216. b->start_addr = (unsigned long)(&(r->status));
  217. b->x_count = 0;
  218. rx_list_tail->desc_b.next_dma_desc = a;
  219. rx_list_tail->next = r;
  220. rx_list_tail = r;
  221. }
  222. rx_list_tail->next = rx_list_head; /* rx_list is a circle */
  223. rx_list_tail->desc_b.next_dma_desc = &(rx_list_head->desc_a);
  224. current_rx_ptr = rx_list_head;
  225. return 0;
  226. init_error:
  227. desc_list_free();
  228. printk(KERN_ERR DRV_NAME ": kmalloc failed\n");
  229. return -ENOMEM;
  230. }
  231. /*---PHY CONTROL AND CONFIGURATION-----------------------------------------*/
  232. /* Set FER regs to MUX in Ethernet pins */
  233. static int setup_pin_mux(int action)
  234. {
  235. #if defined(CONFIG_BFIN_MAC_RMII)
  236. u16 pin_req[] = P_RMII0;
  237. #else
  238. u16 pin_req[] = P_MII0;
  239. #endif
  240. if (action) {
  241. if (peripheral_request_list(pin_req, DRV_NAME)) {
  242. printk(KERN_ERR DRV_NAME
  243. ": Requesting Peripherals failed\n");
  244. return -EFAULT;
  245. }
  246. } else
  247. peripheral_free_list(pin_req);
  248. return 0;
  249. }
  250. /* Wait until the previous MDC/MDIO transaction has completed */
  251. static void poll_mdc_done(void)
  252. {
  253. int timeout_cnt = MAX_TIMEOUT_CNT;
  254. /* poll the STABUSY bit */
  255. while ((bfin_read_EMAC_STAADD()) & STABUSY) {
  256. mdelay(10);
  257. if (timeout_cnt-- < 0) {
  258. printk(KERN_ERR DRV_NAME
  259. ": wait MDC/MDIO transaction to complete timeout\n");
  260. break;
  261. }
  262. }
  263. }
  264. /* Read an off-chip register in a PHY through the MDC/MDIO port */
  265. static u16 read_phy_reg(u16 PHYAddr, u16 RegAddr)
  266. {
  267. poll_mdc_done();
  268. /* read mode */
  269. bfin_write_EMAC_STAADD(SET_PHYAD(PHYAddr) |
  270. SET_REGAD(RegAddr) |
  271. STABUSY);
  272. poll_mdc_done();
  273. return (u16) bfin_read_EMAC_STADAT();
  274. }
  275. /* Write an off-chip register in a PHY through the MDC/MDIO port */
  276. static void raw_write_phy_reg(u16 PHYAddr, u16 RegAddr, u32 Data)
  277. {
  278. bfin_write_EMAC_STADAT(Data);
  279. /* write mode */
  280. bfin_write_EMAC_STAADD(SET_PHYAD(PHYAddr) |
  281. SET_REGAD(RegAddr) |
  282. STAOP |
  283. STABUSY);
  284. poll_mdc_done();
  285. }
  286. static void write_phy_reg(u16 PHYAddr, u16 RegAddr, u32 Data)
  287. {
  288. poll_mdc_done();
  289. raw_write_phy_reg(PHYAddr, RegAddr, Data);
  290. }
  291. /* set up the phy */
  292. static void bf537mac_setphy(struct net_device *dev)
  293. {
  294. u16 phydat;
  295. struct bf537mac_local *lp = netdev_priv(dev);
  296. /* Program PHY registers */
  297. pr_debug("start setting up phy\n");
  298. /* issue a reset */
  299. raw_write_phy_reg(lp->PhyAddr, PHYREG_MODECTL, 0x8000);
  300. /* wait half a second */
  301. msleep(500);
  302. phydat = read_phy_reg(lp->PhyAddr, PHYREG_MODECTL);
  303. /* advertise flow control supported */
  304. phydat = read_phy_reg(lp->PhyAddr, PHYREG_ANAR);
  305. phydat |= (1 << 10);
  306. write_phy_reg(lp->PhyAddr, PHYREG_ANAR, phydat);
  307. phydat = 0;
  308. if (lp->Negotiate)
  309. phydat |= 0x1000; /* enable auto negotiation */
  310. else {
  311. if (lp->FullDuplex)
  312. phydat |= (1 << 8); /* full duplex */
  313. else
  314. phydat &= (~(1 << 8)); /* half duplex */
  315. if (!lp->Port10)
  316. phydat |= (1 << 13); /* 100 Mbps */
  317. else
  318. phydat &= (~(1 << 13)); /* 10 Mbps */
  319. }
  320. if (lp->Loopback)
  321. phydat |= (1 << 14); /* enable TX->RX loopback */
  322. write_phy_reg(lp->PhyAddr, PHYREG_MODECTL, phydat);
  323. msleep(500);
  324. phydat = read_phy_reg(lp->PhyAddr, PHYREG_MODECTL);
  325. /* check for SMSC PHY */
  326. if ((read_phy_reg(lp->PhyAddr, PHYREG_PHYID1) == 0x7) &&
  327. ((read_phy_reg(lp->PhyAddr, PHYREG_PHYID2) & 0xfff0) == 0xC0A0)) {
  328. /*
  329. * we have SMSC PHY so reqest interrupt
  330. * on link down condition
  331. */
  332. /* enable interrupts */
  333. write_phy_reg(lp->PhyAddr, 30, 0x0ff);
  334. }
  335. }
  336. /**************************************************************************/
  337. void setup_system_regs(struct net_device *dev)
  338. {
  339. int phyaddr;
  340. unsigned short sysctl, phydat;
  341. u32 opmode;
  342. struct bf537mac_local *lp = netdev_priv(dev);
  343. int count = 0;
  344. phyaddr = lp->PhyAddr;
  345. /* Enable PHY output */
  346. if (!(bfin_read_VR_CTL() & PHYCLKOE))
  347. bfin_write_VR_CTL(bfin_read_VR_CTL() | PHYCLKOE);
  348. /* MDC = 2.5 MHz */
  349. sysctl = SET_MDCDIV(24);
  350. /* Odd word alignment for Receive Frame DMA word */
  351. /* Configure checksum support and rcve frame word alignment */
  352. #if defined(BFIN_MAC_CSUM_OFFLOAD)
  353. sysctl |= RXDWA | RXCKS;
  354. #else
  355. sysctl |= RXDWA;
  356. #endif
  357. bfin_write_EMAC_SYSCTL(sysctl);
  358. /* auto negotiation on */
  359. /* full duplex */
  360. /* 100 Mbps */
  361. phydat = PHY_ANEG_EN | PHY_DUPLEX | PHY_SPD_SET;
  362. write_phy_reg(phyaddr, PHYREG_MODECTL, phydat);
  363. /* test if full duplex supported */
  364. do {
  365. msleep(100);
  366. phydat = read_phy_reg(phyaddr, PHYREG_MODESTAT);
  367. if (count > 30) {
  368. printk(KERN_NOTICE DRV_NAME ": Link is down\n");
  369. printk(KERN_NOTICE DRV_NAME
  370. "please check your network connection\n");
  371. break;
  372. }
  373. count++;
  374. } while (!(phydat & 0x0004));
  375. phydat = read_phy_reg(phyaddr, PHYREG_ANLPAR);
  376. if ((phydat & 0x0100) || (phydat & 0x0040)) {
  377. opmode = FDMODE;
  378. } else {
  379. opmode = 0;
  380. printk(KERN_INFO DRV_NAME
  381. ": Network is set to half duplex\n");
  382. }
  383. #if defined(CONFIG_BFIN_MAC_RMII)
  384. opmode |= RMII; /* For Now only 100MBit are supported */
  385. #endif
  386. bfin_write_EMAC_OPMODE(opmode);
  387. bfin_write_EMAC_MMC_CTL(RSTC | CROLL);
  388. /* Initialize the TX DMA channel registers */
  389. bfin_write_DMA2_X_COUNT(0);
  390. bfin_write_DMA2_X_MODIFY(4);
  391. bfin_write_DMA2_Y_COUNT(0);
  392. bfin_write_DMA2_Y_MODIFY(0);
  393. /* Initialize the RX DMA channel registers */
  394. bfin_write_DMA1_X_COUNT(0);
  395. bfin_write_DMA1_X_MODIFY(4);
  396. bfin_write_DMA1_Y_COUNT(0);
  397. bfin_write_DMA1_Y_MODIFY(0);
  398. }
  399. void setup_mac_addr(u8 * mac_addr)
  400. {
  401. u32 addr_low = le32_to_cpu(*(__le32 *) & mac_addr[0]);
  402. u16 addr_hi = le16_to_cpu(*(__le16 *) & mac_addr[4]);
  403. /* this depends on a little-endian machine */
  404. bfin_write_EMAC_ADDRLO(addr_low);
  405. bfin_write_EMAC_ADDRHI(addr_hi);
  406. }
  407. static void adjust_tx_list(void)
  408. {
  409. int timeout_cnt = MAX_TIMEOUT_CNT;
  410. if (tx_list_head->status.status_word != 0
  411. && current_tx_ptr != tx_list_head) {
  412. goto adjust_head; /* released something, just return; */
  413. }
  414. /*
  415. * if nothing released, check wait condition
  416. * current's next can not be the head,
  417. * otherwise the dma will not stop as we want
  418. */
  419. if (current_tx_ptr->next->next == tx_list_head) {
  420. while (tx_list_head->status.status_word == 0) {
  421. mdelay(10);
  422. if (tx_list_head->status.status_word != 0
  423. || !(bfin_read_DMA2_IRQ_STATUS() & 0x08)) {
  424. goto adjust_head;
  425. }
  426. if (timeout_cnt-- < 0) {
  427. printk(KERN_ERR DRV_NAME
  428. ": wait for adjust tx list head timeout\n");
  429. break;
  430. }
  431. }
  432. if (tx_list_head->status.status_word != 0) {
  433. goto adjust_head;
  434. }
  435. }
  436. return;
  437. adjust_head:
  438. do {
  439. tx_list_head->desc_a.config &= ~DMAEN;
  440. tx_list_head->status.status_word = 0;
  441. if (tx_list_head->skb) {
  442. dev_kfree_skb(tx_list_head->skb);
  443. tx_list_head->skb = NULL;
  444. } else {
  445. printk(KERN_ERR DRV_NAME
  446. ": no sk_buff in a transmitted frame!\n");
  447. }
  448. tx_list_head = tx_list_head->next;
  449. } while (tx_list_head->status.status_word != 0
  450. && current_tx_ptr != tx_list_head);
  451. return;
  452. }
  453. static int bf537mac_hard_start_xmit(struct sk_buff *skb,
  454. struct net_device *dev)
  455. {
  456. struct bf537mac_local *lp = netdev_priv(dev);
  457. unsigned int data;
  458. current_tx_ptr->skb = skb;
  459. /*
  460. * Is skb->data always 16-bit aligned?
  461. * Do we need to memcpy((char *)(tail->packet + 2), skb->data, len)?
  462. */
  463. if ((((unsigned int)(skb->data)) & 0x02) == 2) {
  464. /* move skb->data to current_tx_ptr payload */
  465. data = (unsigned int)(skb->data) - 2;
  466. *((unsigned short *)data) = (unsigned short)(skb->len);
  467. current_tx_ptr->desc_a.start_addr = (unsigned long)data;
  468. /* this is important! */
  469. blackfin_dcache_flush_range(data, (data + (skb->len)) + 2);
  470. } else {
  471. *((unsigned short *)(current_tx_ptr->packet)) =
  472. (unsigned short)(skb->len);
  473. memcpy((char *)(current_tx_ptr->packet + 2), skb->data,
  474. (skb->len));
  475. current_tx_ptr->desc_a.start_addr =
  476. (unsigned long)current_tx_ptr->packet;
  477. if (current_tx_ptr->status.status_word != 0)
  478. current_tx_ptr->status.status_word = 0;
  479. blackfin_dcache_flush_range((unsigned int)current_tx_ptr->
  480. packet,
  481. (unsigned int)(current_tx_ptr->
  482. packet + skb->len) +
  483. 2);
  484. }
  485. /* enable this packet's dma */
  486. current_tx_ptr->desc_a.config |= DMAEN;
  487. /* tx dma is running, just return */
  488. if (bfin_read_DMA2_IRQ_STATUS() & 0x08)
  489. goto out;
  490. /* tx dma is not running */
  491. bfin_write_DMA2_NEXT_DESC_PTR(&(current_tx_ptr->desc_a));
  492. /* dma enabled, read from memory, size is 6 */
  493. bfin_write_DMA2_CONFIG(current_tx_ptr->desc_a.config);
  494. /* Turn on the EMAC tx */
  495. bfin_write_EMAC_OPMODE(bfin_read_EMAC_OPMODE() | TE);
  496. out:
  497. adjust_tx_list();
  498. current_tx_ptr = current_tx_ptr->next;
  499. dev->trans_start = jiffies;
  500. dev->stats.tx_packets++;
  501. dev->stats.tx_bytes += (skb->len);
  502. return 0;
  503. }
  504. static void bf537mac_rx(struct net_device *dev)
  505. {
  506. struct sk_buff *skb, *new_skb;
  507. struct bf537mac_local *lp = netdev_priv(dev);
  508. unsigned short len;
  509. /* allocate a new skb for next time receive */
  510. skb = current_rx_ptr->skb;
  511. new_skb = dev_alloc_skb(PKT_BUF_SZ + 2);
  512. if (!new_skb) {
  513. printk(KERN_NOTICE DRV_NAME
  514. ": rx: low on mem - packet dropped\n");
  515. dev->stats.rx_dropped++;
  516. goto out;
  517. }
  518. /* reserve 2 bytes for RXDWA padding */
  519. skb_reserve(new_skb, 2);
  520. current_rx_ptr->skb = new_skb;
  521. current_rx_ptr->desc_a.start_addr = (unsigned long)new_skb->data - 2;
  522. len = (unsigned short)((current_rx_ptr->status.status_word) & RX_FRLEN);
  523. skb_put(skb, len);
  524. blackfin_dcache_invalidate_range((unsigned long)skb->head,
  525. (unsigned long)skb->tail);
  526. dev->last_rx = jiffies;
  527. skb->dev = dev;
  528. skb->protocol = eth_type_trans(skb, dev);
  529. #if defined(BFIN_MAC_CSUM_OFFLOAD)
  530. skb->csum = current_rx_ptr->status.ip_payload_csum;
  531. skb->ip_summed = CHECKSUM_PARTIAL;
  532. #endif
  533. netif_rx(skb);
  534. dev->stats.rx_packets++;
  535. dev->stats.rx_bytes += len;
  536. current_rx_ptr->status.status_word = 0x00000000;
  537. current_rx_ptr = current_rx_ptr->next;
  538. out:
  539. return;
  540. }
  541. /* interrupt routine to handle rx and error signal */
  542. static irqreturn_t bf537mac_interrupt(int irq, void *dev_id)
  543. {
  544. struct net_device *dev = dev_id;
  545. int number = 0;
  546. get_one_packet:
  547. if (current_rx_ptr->status.status_word == 0) {
  548. /* no more new packet received */
  549. if (number == 0) {
  550. if (current_rx_ptr->next->status.status_word != 0) {
  551. current_rx_ptr = current_rx_ptr->next;
  552. goto real_rx;
  553. }
  554. }
  555. bfin_write_DMA1_IRQ_STATUS(bfin_read_DMA1_IRQ_STATUS() |
  556. DMA_DONE | DMA_ERR);
  557. return IRQ_HANDLED;
  558. }
  559. real_rx:
  560. bf537mac_rx(dev);
  561. number++;
  562. goto get_one_packet;
  563. }
  564. #ifdef CONFIG_NET_POLL_CONTROLLER
  565. static void bf537mac_poll(struct net_device *dev)
  566. {
  567. disable_irq(IRQ_MAC_RX);
  568. bf537mac_interrupt(IRQ_MAC_RX, dev);
  569. enable_irq(IRQ_MAC_RX);
  570. }
  571. #endif /* CONFIG_NET_POLL_CONTROLLER */
  572. static void bf537mac_reset(void)
  573. {
  574. unsigned int opmode;
  575. opmode = bfin_read_EMAC_OPMODE();
  576. opmode &= (~RE);
  577. opmode &= (~TE);
  578. /* Turn off the EMAC */
  579. bfin_write_EMAC_OPMODE(opmode);
  580. }
  581. /*
  582. * Enable Interrupts, Receive, and Transmit
  583. */
  584. static int bf537mac_enable(struct net_device *dev)
  585. {
  586. u32 opmode;
  587. pr_debug("%s: %s\n", dev->name, __FUNCTION__);
  588. /* Set RX DMA */
  589. bfin_write_DMA1_NEXT_DESC_PTR(&(rx_list_head->desc_a));
  590. bfin_write_DMA1_CONFIG(rx_list_head->desc_a.config);
  591. /* Wait MII done */
  592. poll_mdc_done();
  593. /* We enable only RX here */
  594. /* ASTP : Enable Automatic Pad Stripping
  595. PR : Promiscuous Mode for test
  596. PSF : Receive frames with total length less than 64 bytes.
  597. FDMODE : Full Duplex Mode
  598. LB : Internal Loopback for test
  599. RE : Receiver Enable */
  600. opmode = bfin_read_EMAC_OPMODE();
  601. if (opmode & FDMODE)
  602. opmode |= PSF;
  603. else
  604. opmode |= DRO | DC | PSF;
  605. opmode |= RE;
  606. #if defined(CONFIG_BFIN_MAC_RMII)
  607. opmode |= RMII; /* For Now only 100MBit are supported */
  608. #ifdef CONFIG_BF_REV_0_2
  609. opmode |= TE;
  610. #endif
  611. #endif
  612. /* Turn on the EMAC rx */
  613. bfin_write_EMAC_OPMODE(opmode);
  614. return 0;
  615. }
  616. /* Our watchdog timed out. Called by the networking layer */
  617. static void bf537mac_timeout(struct net_device *dev)
  618. {
  619. pr_debug("%s: %s\n", dev->name, __FUNCTION__);
  620. bf537mac_reset();
  621. /* reset tx queue */
  622. tx_list_tail = tx_list_head->next;
  623. bf537mac_enable(dev);
  624. /* We can accept TX packets again */
  625. dev->trans_start = jiffies;
  626. netif_wake_queue(dev);
  627. }
  628. /*
  629. * This routine will, depending on the values passed to it,
  630. * either make it accept multicast packets, go into
  631. * promiscuous mode (for TCPDUMP and cousins) or accept
  632. * a select set of multicast packets
  633. */
  634. static void bf537mac_set_multicast_list(struct net_device *dev)
  635. {
  636. u32 sysctl;
  637. if (dev->flags & IFF_PROMISC) {
  638. printk(KERN_INFO "%s: set to promisc mode\n", dev->name);
  639. sysctl = bfin_read_EMAC_OPMODE();
  640. sysctl |= RAF;
  641. bfin_write_EMAC_OPMODE(sysctl);
  642. } else if (dev->flags & IFF_ALLMULTI || dev->mc_count) {
  643. /* accept all multicast */
  644. sysctl = bfin_read_EMAC_OPMODE();
  645. sysctl |= PAM;
  646. bfin_write_EMAC_OPMODE(sysctl);
  647. } else {
  648. /* clear promisc or multicast mode */
  649. sysctl = bfin_read_EMAC_OPMODE();
  650. sysctl &= ~(RAF | PAM);
  651. bfin_write_EMAC_OPMODE(sysctl);
  652. }
  653. }
  654. /*
  655. * this puts the device in an inactive state
  656. */
  657. static void bf537mac_shutdown(struct net_device *dev)
  658. {
  659. /* Turn off the EMAC */
  660. bfin_write_EMAC_OPMODE(0x00000000);
  661. /* Turn off the EMAC RX DMA */
  662. bfin_write_DMA1_CONFIG(0x0000);
  663. bfin_write_DMA2_CONFIG(0x0000);
  664. }
  665. /*
  666. * Open and Initialize the interface
  667. *
  668. * Set up everything, reset the card, etc..
  669. */
  670. static int bf537mac_open(struct net_device *dev)
  671. {
  672. int retval;
  673. pr_debug("%s: %s\n", dev->name, __FUNCTION__);
  674. /*
  675. * Check that the address is valid. If its not, refuse
  676. * to bring the device up. The user must specify an
  677. * address using ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx
  678. */
  679. if (!is_valid_ether_addr(dev->dev_addr)) {
  680. printk(KERN_WARNING DRV_NAME ": no valid ethernet hw addr\n");
  681. return -EINVAL;
  682. }
  683. /* initial rx and tx list */
  684. retval = desc_list_init();
  685. if (retval)
  686. return retval;
  687. bf537mac_setphy(dev);
  688. setup_system_regs(dev);
  689. bf537mac_reset();
  690. bf537mac_enable(dev);
  691. pr_debug("hardware init finished\n");
  692. netif_start_queue(dev);
  693. netif_carrier_on(dev);
  694. return 0;
  695. }
  696. /*
  697. *
  698. * this makes the board clean up everything that it can
  699. * and not talk to the outside world. Caused by
  700. * an 'ifconfig ethX down'
  701. */
  702. static int bf537mac_close(struct net_device *dev)
  703. {
  704. pr_debug("%s: %s\n", dev->name, __FUNCTION__);
  705. netif_stop_queue(dev);
  706. netif_carrier_off(dev);
  707. /* clear everything */
  708. bf537mac_shutdown(dev);
  709. /* free the rx/tx buffers */
  710. desc_list_free();
  711. return 0;
  712. }
  713. static int __init bf537mac_probe(struct net_device *dev)
  714. {
  715. struct bf537mac_local *lp = netdev_priv(dev);
  716. int retval;
  717. /* Grab the MAC address in the MAC */
  718. *(__le32 *) (&(dev->dev_addr[0])) = cpu_to_le32(bfin_read_EMAC_ADDRLO());
  719. *(__le16 *) (&(dev->dev_addr[4])) = cpu_to_le16((u16) bfin_read_EMAC_ADDRHI());
  720. /* probe mac */
  721. /*todo: how to proble? which is revision_register */
  722. bfin_write_EMAC_ADDRLO(0x12345678);
  723. if (bfin_read_EMAC_ADDRLO() != 0x12345678) {
  724. pr_debug("can't detect bf537 mac!\n");
  725. retval = -ENODEV;
  726. goto err_out;
  727. }
  728. /* set the GPIO pins to Ethernet mode */
  729. retval = setup_pin_mux(1);
  730. if (retval)
  731. return retval;
  732. /*Is it valid? (Did bootloader initialize it?) */
  733. if (!is_valid_ether_addr(dev->dev_addr)) {
  734. /* Grab the MAC from the board somehow - this is done in the
  735. arch/blackfin/mach-bf537/boards/eth_mac.c */
  736. get_bf537_ether_addr(dev->dev_addr);
  737. }
  738. /* If still not valid, get a random one */
  739. if (!is_valid_ether_addr(dev->dev_addr)) {
  740. random_ether_addr(dev->dev_addr);
  741. }
  742. setup_mac_addr(dev->dev_addr);
  743. /* Fill in the fields of the device structure with ethernet values. */
  744. ether_setup(dev);
  745. dev->open = bf537mac_open;
  746. dev->stop = bf537mac_close;
  747. dev->hard_start_xmit = bf537mac_hard_start_xmit;
  748. dev->tx_timeout = bf537mac_timeout;
  749. dev->set_multicast_list = bf537mac_set_multicast_list;
  750. #ifdef CONFIG_NET_POLL_CONTROLLER
  751. dev->poll_controller = bf537mac_poll;
  752. #endif
  753. /* fill in some of the fields */
  754. lp->version = 1;
  755. lp->PhyAddr = 0x01;
  756. lp->CLKIN = 25;
  757. lp->FullDuplex = 0;
  758. lp->Negotiate = 1;
  759. lp->FlowControl = 0;
  760. spin_lock_init(&lp->lock);
  761. /* now, enable interrupts */
  762. /* register irq handler */
  763. if (request_irq
  764. (IRQ_MAC_RX, bf537mac_interrupt, IRQF_DISABLED | IRQF_SHARED,
  765. "BFIN537_MAC_RX", dev)) {
  766. printk(KERN_WARNING DRV_NAME
  767. ": Unable to attach BlackFin MAC RX interrupt\n");
  768. return -EBUSY;
  769. }
  770. /* Enable PHY output early */
  771. if (!(bfin_read_VR_CTL() & PHYCLKOE))
  772. bfin_write_VR_CTL(bfin_read_VR_CTL() | PHYCLKOE);
  773. retval = register_netdev(dev);
  774. if (retval == 0) {
  775. /* now, print out the card info, in a short format.. */
  776. printk(KERN_INFO "%s: Version %s, %s\n",
  777. DRV_NAME, DRV_VERSION, DRV_DESC);
  778. }
  779. err_out:
  780. return retval;
  781. }
  782. static int bfin_mac_probe(struct platform_device *pdev)
  783. {
  784. struct net_device *ndev;
  785. ndev = alloc_etherdev(sizeof(struct bf537mac_local));
  786. if (!ndev) {
  787. printk(KERN_WARNING DRV_NAME ": could not allocate device\n");
  788. return -ENOMEM;
  789. }
  790. SET_NETDEV_DEV(ndev, &pdev->dev);
  791. platform_set_drvdata(pdev, ndev);
  792. if (bf537mac_probe(ndev) != 0) {
  793. platform_set_drvdata(pdev, NULL);
  794. free_netdev(ndev);
  795. printk(KERN_WARNING DRV_NAME ": not found\n");
  796. return -ENODEV;
  797. }
  798. return 0;
  799. }
  800. static int bfin_mac_remove(struct platform_device *pdev)
  801. {
  802. struct net_device *ndev = platform_get_drvdata(pdev);
  803. platform_set_drvdata(pdev, NULL);
  804. unregister_netdev(ndev);
  805. free_irq(IRQ_MAC_RX, ndev);
  806. free_netdev(ndev);
  807. setup_pin_mux(0);
  808. return 0;
  809. }
  810. static int bfin_mac_suspend(struct platform_device *pdev, pm_message_t state)
  811. {
  812. return 0;
  813. }
  814. static int bfin_mac_resume(struct platform_device *pdev)
  815. {
  816. return 0;
  817. }
  818. static struct platform_driver bfin_mac_driver = {
  819. .probe = bfin_mac_probe,
  820. .remove = bfin_mac_remove,
  821. .resume = bfin_mac_resume,
  822. .suspend = bfin_mac_suspend,
  823. .driver = {
  824. .name = DRV_NAME,
  825. },
  826. };
  827. static int __init bfin_mac_init(void)
  828. {
  829. return platform_driver_register(&bfin_mac_driver);
  830. }
  831. module_init(bfin_mac_init);
  832. static void __exit bfin_mac_cleanup(void)
  833. {
  834. platform_driver_unregister(&bfin_mac_driver);
  835. }
  836. module_exit(bfin_mac_cleanup);