bfin_mac.c 26 KB

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