bfin_mac.c 24 KB

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