bfin_mac.c 27 KB

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