bfin_mac.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549
  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/div64.h>
  34. #include <asm/dpmc.h>
  35. #include <asm/blackfin.h>
  36. #include <asm/cacheflush.h>
  37. #include <asm/portmux.h>
  38. #include "bfin_mac.h"
  39. #define DRV_NAME "bfin_mac"
  40. #define DRV_VERSION "1.1"
  41. #define DRV_AUTHOR "Bryan Wu, Luke Yang"
  42. #define DRV_DESC "Blackfin on-chip Ethernet MAC driver"
  43. MODULE_AUTHOR(DRV_AUTHOR);
  44. MODULE_LICENSE("GPL");
  45. MODULE_DESCRIPTION(DRV_DESC);
  46. MODULE_ALIAS("platform:bfin_mac");
  47. #if defined(CONFIG_BFIN_MAC_USE_L1)
  48. # define bfin_mac_alloc(dma_handle, size) l1_data_sram_zalloc(size)
  49. # define bfin_mac_free(dma_handle, ptr) l1_data_sram_free(ptr)
  50. #else
  51. # define bfin_mac_alloc(dma_handle, size) \
  52. dma_alloc_coherent(NULL, size, dma_handle, GFP_KERNEL)
  53. # define bfin_mac_free(dma_handle, ptr) \
  54. dma_free_coherent(NULL, sizeof(*ptr), ptr, dma_handle)
  55. #endif
  56. #define PKT_BUF_SZ 1580
  57. #define MAX_TIMEOUT_CNT 500
  58. /* pointers to maintain transmit list */
  59. static struct net_dma_desc_tx *tx_list_head;
  60. static struct net_dma_desc_tx *tx_list_tail;
  61. static struct net_dma_desc_rx *rx_list_head;
  62. static struct net_dma_desc_rx *rx_list_tail;
  63. static struct net_dma_desc_rx *current_rx_ptr;
  64. static struct net_dma_desc_tx *current_tx_ptr;
  65. static struct net_dma_desc_tx *tx_desc;
  66. static struct net_dma_desc_rx *rx_desc;
  67. #if defined(CONFIG_BFIN_MAC_RMII)
  68. static u16 pin_req[] = P_RMII0;
  69. #else
  70. static u16 pin_req[] = P_MII0;
  71. #endif
  72. static void bfin_mac_disable(void);
  73. static void bfin_mac_enable(void);
  74. static void desc_list_free(void)
  75. {
  76. struct net_dma_desc_rx *r;
  77. struct net_dma_desc_tx *t;
  78. int i;
  79. #if !defined(CONFIG_BFIN_MAC_USE_L1)
  80. dma_addr_t dma_handle = 0;
  81. #endif
  82. if (tx_desc) {
  83. t = tx_list_head;
  84. for (i = 0; i < CONFIG_BFIN_TX_DESC_NUM; i++) {
  85. if (t) {
  86. if (t->skb) {
  87. dev_kfree_skb(t->skb);
  88. t->skb = NULL;
  89. }
  90. t = t->next;
  91. }
  92. }
  93. bfin_mac_free(dma_handle, tx_desc);
  94. }
  95. if (rx_desc) {
  96. r = rx_list_head;
  97. for (i = 0; i < CONFIG_BFIN_RX_DESC_NUM; i++) {
  98. if (r) {
  99. if (r->skb) {
  100. dev_kfree_skb(r->skb);
  101. r->skb = NULL;
  102. }
  103. r = r->next;
  104. }
  105. }
  106. bfin_mac_free(dma_handle, rx_desc);
  107. }
  108. }
  109. static int desc_list_init(void)
  110. {
  111. int i;
  112. struct sk_buff *new_skb;
  113. #if !defined(CONFIG_BFIN_MAC_USE_L1)
  114. /*
  115. * This dma_handle is useless in Blackfin dma_alloc_coherent().
  116. * The real dma handler is the return value of dma_alloc_coherent().
  117. */
  118. dma_addr_t dma_handle;
  119. #endif
  120. tx_desc = bfin_mac_alloc(&dma_handle,
  121. sizeof(struct net_dma_desc_tx) *
  122. CONFIG_BFIN_TX_DESC_NUM);
  123. if (tx_desc == NULL)
  124. goto init_error;
  125. rx_desc = bfin_mac_alloc(&dma_handle,
  126. sizeof(struct net_dma_desc_rx) *
  127. CONFIG_BFIN_RX_DESC_NUM);
  128. if (rx_desc == NULL)
  129. goto init_error;
  130. /* init tx_list */
  131. tx_list_head = tx_list_tail = tx_desc;
  132. for (i = 0; i < CONFIG_BFIN_TX_DESC_NUM; i++) {
  133. struct net_dma_desc_tx *t = tx_desc + i;
  134. struct dma_descriptor *a = &(t->desc_a);
  135. struct dma_descriptor *b = &(t->desc_b);
  136. /*
  137. * disable DMA
  138. * read from memory WNR = 0
  139. * wordsize is 32 bits
  140. * 6 half words is desc size
  141. * large desc flow
  142. */
  143. a->config = WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE;
  144. a->start_addr = (unsigned long)t->packet;
  145. a->x_count = 0;
  146. a->next_dma_desc = b;
  147. /*
  148. * enabled DMA
  149. * write to memory WNR = 1
  150. * wordsize is 32 bits
  151. * disable interrupt
  152. * 6 half words is desc size
  153. * large desc flow
  154. */
  155. b->config = DMAEN | WNR | WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE;
  156. b->start_addr = (unsigned long)(&(t->status));
  157. b->x_count = 0;
  158. t->skb = NULL;
  159. tx_list_tail->desc_b.next_dma_desc = a;
  160. tx_list_tail->next = t;
  161. tx_list_tail = t;
  162. }
  163. tx_list_tail->next = tx_list_head; /* tx_list is a circle */
  164. tx_list_tail->desc_b.next_dma_desc = &(tx_list_head->desc_a);
  165. current_tx_ptr = tx_list_head;
  166. /* init rx_list */
  167. rx_list_head = rx_list_tail = rx_desc;
  168. for (i = 0; i < CONFIG_BFIN_RX_DESC_NUM; i++) {
  169. struct net_dma_desc_rx *r = rx_desc + i;
  170. struct dma_descriptor *a = &(r->desc_a);
  171. struct dma_descriptor *b = &(r->desc_b);
  172. /* allocate a new skb for next time receive */
  173. new_skb = dev_alloc_skb(PKT_BUF_SZ + NET_IP_ALIGN);
  174. if (!new_skb) {
  175. printk(KERN_NOTICE DRV_NAME
  176. ": init: low on mem - packet dropped\n");
  177. goto init_error;
  178. }
  179. skb_reserve(new_skb, NET_IP_ALIGN);
  180. r->skb = new_skb;
  181. /*
  182. * enabled DMA
  183. * write to memory WNR = 1
  184. * wordsize is 32 bits
  185. * disable interrupt
  186. * 6 half words is desc size
  187. * large desc flow
  188. */
  189. a->config = DMAEN | WNR | WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE;
  190. /* since RXDWA is enabled */
  191. a->start_addr = (unsigned long)new_skb->data - 2;
  192. a->x_count = 0;
  193. a->next_dma_desc = b;
  194. /*
  195. * enabled DMA
  196. * write to memory WNR = 1
  197. * wordsize is 32 bits
  198. * enable interrupt
  199. * 6 half words is desc size
  200. * large desc flow
  201. */
  202. b->config = DMAEN | WNR | WDSIZE_32 | DI_EN |
  203. NDSIZE_6 | DMAFLOW_LARGE;
  204. b->start_addr = (unsigned long)(&(r->status));
  205. b->x_count = 0;
  206. rx_list_tail->desc_b.next_dma_desc = a;
  207. rx_list_tail->next = r;
  208. rx_list_tail = r;
  209. }
  210. rx_list_tail->next = rx_list_head; /* rx_list is a circle */
  211. rx_list_tail->desc_b.next_dma_desc = &(rx_list_head->desc_a);
  212. current_rx_ptr = rx_list_head;
  213. return 0;
  214. init_error:
  215. desc_list_free();
  216. printk(KERN_ERR DRV_NAME ": kmalloc failed\n");
  217. return -ENOMEM;
  218. }
  219. /*---PHY CONTROL AND CONFIGURATION-----------------------------------------*/
  220. /*
  221. * MII operations
  222. */
  223. /* Wait until the previous MDC/MDIO transaction has completed */
  224. static void bfin_mdio_poll(void)
  225. {
  226. int timeout_cnt = MAX_TIMEOUT_CNT;
  227. /* poll the STABUSY bit */
  228. while ((bfin_read_EMAC_STAADD()) & STABUSY) {
  229. udelay(1);
  230. if (timeout_cnt-- < 0) {
  231. printk(KERN_ERR DRV_NAME
  232. ": wait MDC/MDIO transaction to complete timeout\n");
  233. break;
  234. }
  235. }
  236. }
  237. /* Read an off-chip register in a PHY through the MDC/MDIO port */
  238. static int bfin_mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum)
  239. {
  240. bfin_mdio_poll();
  241. /* read mode */
  242. bfin_write_EMAC_STAADD(SET_PHYAD((u16) phy_addr) |
  243. SET_REGAD((u16) regnum) |
  244. STABUSY);
  245. bfin_mdio_poll();
  246. return (int) bfin_read_EMAC_STADAT();
  247. }
  248. /* Write an off-chip register in a PHY through the MDC/MDIO port */
  249. static int bfin_mdiobus_write(struct mii_bus *bus, int phy_addr, int regnum,
  250. u16 value)
  251. {
  252. bfin_mdio_poll();
  253. bfin_write_EMAC_STADAT((u32) value);
  254. /* write mode */
  255. bfin_write_EMAC_STAADD(SET_PHYAD((u16) phy_addr) |
  256. SET_REGAD((u16) regnum) |
  257. STAOP |
  258. STABUSY);
  259. bfin_mdio_poll();
  260. return 0;
  261. }
  262. static int bfin_mdiobus_reset(struct mii_bus *bus)
  263. {
  264. return 0;
  265. }
  266. static void bfin_mac_adjust_link(struct net_device *dev)
  267. {
  268. struct bfin_mac_local *lp = netdev_priv(dev);
  269. struct phy_device *phydev = lp->phydev;
  270. unsigned long flags;
  271. int new_state = 0;
  272. spin_lock_irqsave(&lp->lock, flags);
  273. if (phydev->link) {
  274. /* Now we make sure that we can be in full duplex mode.
  275. * If not, we operate in half-duplex mode. */
  276. if (phydev->duplex != lp->old_duplex) {
  277. u32 opmode = bfin_read_EMAC_OPMODE();
  278. new_state = 1;
  279. if (phydev->duplex)
  280. opmode |= FDMODE;
  281. else
  282. opmode &= ~(FDMODE);
  283. bfin_write_EMAC_OPMODE(opmode);
  284. lp->old_duplex = phydev->duplex;
  285. }
  286. if (phydev->speed != lp->old_speed) {
  287. #if defined(CONFIG_BFIN_MAC_RMII)
  288. u32 opmode = bfin_read_EMAC_OPMODE();
  289. switch (phydev->speed) {
  290. case 10:
  291. opmode |= RMII_10;
  292. break;
  293. case 100:
  294. opmode &= ~(RMII_10);
  295. break;
  296. default:
  297. printk(KERN_WARNING
  298. "%s: Ack! Speed (%d) is not 10/100!\n",
  299. DRV_NAME, phydev->speed);
  300. break;
  301. }
  302. bfin_write_EMAC_OPMODE(opmode);
  303. #endif
  304. new_state = 1;
  305. lp->old_speed = phydev->speed;
  306. }
  307. if (!lp->old_link) {
  308. new_state = 1;
  309. lp->old_link = 1;
  310. }
  311. } else if (lp->old_link) {
  312. new_state = 1;
  313. lp->old_link = 0;
  314. lp->old_speed = 0;
  315. lp->old_duplex = -1;
  316. }
  317. if (new_state) {
  318. u32 opmode = bfin_read_EMAC_OPMODE();
  319. phy_print_status(phydev);
  320. pr_debug("EMAC_OPMODE = 0x%08x\n", opmode);
  321. }
  322. spin_unlock_irqrestore(&lp->lock, flags);
  323. }
  324. /* MDC = 2.5 MHz */
  325. #define MDC_CLK 2500000
  326. static int mii_probe(struct net_device *dev)
  327. {
  328. struct bfin_mac_local *lp = netdev_priv(dev);
  329. struct phy_device *phydev = NULL;
  330. unsigned short sysctl;
  331. int i;
  332. u32 sclk, mdc_div;
  333. /* Enable PHY output early */
  334. if (!(bfin_read_VR_CTL() & CLKBUFOE))
  335. bfin_write_VR_CTL(bfin_read_VR_CTL() | CLKBUFOE);
  336. sclk = get_sclk();
  337. mdc_div = ((sclk / MDC_CLK) / 2) - 1;
  338. sysctl = bfin_read_EMAC_SYSCTL();
  339. sysctl = (sysctl & ~MDCDIV) | SET_MDCDIV(mdc_div);
  340. bfin_write_EMAC_SYSCTL(sysctl);
  341. /* search for connect PHY device */
  342. for (i = 0; i < PHY_MAX_ADDR; i++) {
  343. struct phy_device *const tmp_phydev = lp->mii_bus->phy_map[i];
  344. if (!tmp_phydev)
  345. continue; /* no PHY here... */
  346. phydev = tmp_phydev;
  347. break; /* found it */
  348. }
  349. /* now we are supposed to have a proper phydev, to attach to... */
  350. if (!phydev) {
  351. printk(KERN_INFO "%s: Don't found any phy device at all\n",
  352. dev->name);
  353. return -ENODEV;
  354. }
  355. #if defined(CONFIG_BFIN_MAC_RMII)
  356. phydev = phy_connect(dev, dev_name(&phydev->dev), &bfin_mac_adjust_link,
  357. 0, PHY_INTERFACE_MODE_RMII);
  358. #else
  359. phydev = phy_connect(dev, dev_name(&phydev->dev), &bfin_mac_adjust_link,
  360. 0, PHY_INTERFACE_MODE_MII);
  361. #endif
  362. if (IS_ERR(phydev)) {
  363. printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
  364. return PTR_ERR(phydev);
  365. }
  366. /* mask with MAC supported features */
  367. phydev->supported &= (SUPPORTED_10baseT_Half
  368. | SUPPORTED_10baseT_Full
  369. | SUPPORTED_100baseT_Half
  370. | SUPPORTED_100baseT_Full
  371. | SUPPORTED_Autoneg
  372. | SUPPORTED_Pause | SUPPORTED_Asym_Pause
  373. | SUPPORTED_MII
  374. | SUPPORTED_TP);
  375. phydev->advertising = phydev->supported;
  376. lp->old_link = 0;
  377. lp->old_speed = 0;
  378. lp->old_duplex = -1;
  379. lp->phydev = phydev;
  380. printk(KERN_INFO "%s: attached PHY driver [%s] "
  381. "(mii_bus:phy_addr=%s, irq=%d, mdc_clk=%dHz(mdc_div=%d)"
  382. "@sclk=%dMHz)\n",
  383. DRV_NAME, phydev->drv->name, dev_name(&phydev->dev), phydev->irq,
  384. MDC_CLK, mdc_div, sclk/1000000);
  385. return 0;
  386. }
  387. /*
  388. * Ethtool support
  389. */
  390. static int
  391. bfin_mac_ethtool_getsettings(struct net_device *dev, struct ethtool_cmd *cmd)
  392. {
  393. struct bfin_mac_local *lp = netdev_priv(dev);
  394. if (lp->phydev)
  395. return phy_ethtool_gset(lp->phydev, cmd);
  396. return -EINVAL;
  397. }
  398. static int
  399. bfin_mac_ethtool_setsettings(struct net_device *dev, struct ethtool_cmd *cmd)
  400. {
  401. struct bfin_mac_local *lp = netdev_priv(dev);
  402. if (!capable(CAP_NET_ADMIN))
  403. return -EPERM;
  404. if (lp->phydev)
  405. return phy_ethtool_sset(lp->phydev, cmd);
  406. return -EINVAL;
  407. }
  408. static void bfin_mac_ethtool_getdrvinfo(struct net_device *dev,
  409. struct ethtool_drvinfo *info)
  410. {
  411. strcpy(info->driver, DRV_NAME);
  412. strcpy(info->version, DRV_VERSION);
  413. strcpy(info->fw_version, "N/A");
  414. strcpy(info->bus_info, dev_name(&dev->dev));
  415. }
  416. static const struct ethtool_ops bfin_mac_ethtool_ops = {
  417. .get_settings = bfin_mac_ethtool_getsettings,
  418. .set_settings = bfin_mac_ethtool_setsettings,
  419. .get_link = ethtool_op_get_link,
  420. .get_drvinfo = bfin_mac_ethtool_getdrvinfo,
  421. };
  422. /**************************************************************************/
  423. void setup_system_regs(struct net_device *dev)
  424. {
  425. unsigned short sysctl;
  426. /*
  427. * Odd word alignment for Receive Frame DMA word
  428. * Configure checksum support and rcve frame word alignment
  429. */
  430. sysctl = bfin_read_EMAC_SYSCTL();
  431. #if defined(BFIN_MAC_CSUM_OFFLOAD)
  432. sysctl |= RXDWA | RXCKS;
  433. #else
  434. sysctl |= RXDWA;
  435. #endif
  436. bfin_write_EMAC_SYSCTL(sysctl);
  437. bfin_write_EMAC_MMC_CTL(RSTC | CROLL);
  438. /* Initialize the TX DMA channel registers */
  439. bfin_write_DMA2_X_COUNT(0);
  440. bfin_write_DMA2_X_MODIFY(4);
  441. bfin_write_DMA2_Y_COUNT(0);
  442. bfin_write_DMA2_Y_MODIFY(0);
  443. /* Initialize the RX DMA channel registers */
  444. bfin_write_DMA1_X_COUNT(0);
  445. bfin_write_DMA1_X_MODIFY(4);
  446. bfin_write_DMA1_Y_COUNT(0);
  447. bfin_write_DMA1_Y_MODIFY(0);
  448. }
  449. static void setup_mac_addr(u8 *mac_addr)
  450. {
  451. u32 addr_low = le32_to_cpu(*(__le32 *) & mac_addr[0]);
  452. u16 addr_hi = le16_to_cpu(*(__le16 *) & mac_addr[4]);
  453. /* this depends on a little-endian machine */
  454. bfin_write_EMAC_ADDRLO(addr_low);
  455. bfin_write_EMAC_ADDRHI(addr_hi);
  456. }
  457. static int bfin_mac_set_mac_address(struct net_device *dev, void *p)
  458. {
  459. struct sockaddr *addr = p;
  460. if (netif_running(dev))
  461. return -EBUSY;
  462. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  463. setup_mac_addr(dev->dev_addr);
  464. return 0;
  465. }
  466. #ifdef CONFIG_BFIN_MAC_USE_HWSTAMP
  467. #define bfin_mac_hwtstamp_is_none(cfg) ((cfg) == HWTSTAMP_FILTER_NONE)
  468. static int bfin_mac_hwtstamp_ioctl(struct net_device *netdev,
  469. struct ifreq *ifr, int cmd)
  470. {
  471. struct hwtstamp_config config;
  472. struct bfin_mac_local *lp = netdev_priv(netdev);
  473. u16 ptpctl;
  474. u32 ptpfv1, ptpfv2, ptpfv3, ptpfoff;
  475. if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
  476. return -EFAULT;
  477. pr_debug("%s config flag:0x%x, tx_type:0x%x, rx_filter:0x%x\n",
  478. __func__, config.flags, config.tx_type, config.rx_filter);
  479. /* reserved for future extensions */
  480. if (config.flags)
  481. return -EINVAL;
  482. if ((config.tx_type != HWTSTAMP_TX_OFF) &&
  483. (config.tx_type != HWTSTAMP_TX_ON))
  484. return -ERANGE;
  485. ptpctl = bfin_read_EMAC_PTP_CTL();
  486. switch (config.rx_filter) {
  487. case HWTSTAMP_FILTER_NONE:
  488. /*
  489. * Dont allow any timestamping
  490. */
  491. ptpfv3 = 0xFFFFFFFF;
  492. bfin_write_EMAC_PTP_FV3(ptpfv3);
  493. break;
  494. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  495. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  496. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  497. /*
  498. * Clear the five comparison mask bits (bits[12:8]) in EMAC_PTP_CTL)
  499. * to enable all the field matches.
  500. */
  501. ptpctl &= ~0x1F00;
  502. bfin_write_EMAC_PTP_CTL(ptpctl);
  503. /*
  504. * Keep the default values of the EMAC_PTP_FOFF register.
  505. */
  506. ptpfoff = 0x4A24170C;
  507. bfin_write_EMAC_PTP_FOFF(ptpfoff);
  508. /*
  509. * Keep the default values of the EMAC_PTP_FV1 and EMAC_PTP_FV2
  510. * registers.
  511. */
  512. ptpfv1 = 0x11040800;
  513. bfin_write_EMAC_PTP_FV1(ptpfv1);
  514. ptpfv2 = 0x0140013F;
  515. bfin_write_EMAC_PTP_FV2(ptpfv2);
  516. /*
  517. * The default value (0xFFFC) allows the timestamping of both
  518. * received Sync messages and Delay_Req messages.
  519. */
  520. ptpfv3 = 0xFFFFFFFC;
  521. bfin_write_EMAC_PTP_FV3(ptpfv3);
  522. config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
  523. break;
  524. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  525. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  526. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  527. /* Clear all five comparison mask bits (bits[12:8]) in the
  528. * EMAC_PTP_CTL register to enable all the field matches.
  529. */
  530. ptpctl &= ~0x1F00;
  531. bfin_write_EMAC_PTP_CTL(ptpctl);
  532. /*
  533. * Keep the default values of the EMAC_PTP_FOFF register, except set
  534. * the PTPCOF field to 0x2A.
  535. */
  536. ptpfoff = 0x2A24170C;
  537. bfin_write_EMAC_PTP_FOFF(ptpfoff);
  538. /*
  539. * Keep the default values of the EMAC_PTP_FV1 and EMAC_PTP_FV2
  540. * registers.
  541. */
  542. ptpfv1 = 0x11040800;
  543. bfin_write_EMAC_PTP_FV1(ptpfv1);
  544. ptpfv2 = 0x0140013F;
  545. bfin_write_EMAC_PTP_FV2(ptpfv2);
  546. /*
  547. * To allow the timestamping of Pdelay_Req and Pdelay_Resp, set
  548. * the value to 0xFFF0.
  549. */
  550. ptpfv3 = 0xFFFFFFF0;
  551. bfin_write_EMAC_PTP_FV3(ptpfv3);
  552. config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
  553. break;
  554. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  555. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  556. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  557. /*
  558. * Clear bits 8 and 12 of the EMAC_PTP_CTL register to enable only the
  559. * EFTM and PTPCM field comparison.
  560. */
  561. ptpctl &= ~0x1100;
  562. bfin_write_EMAC_PTP_CTL(ptpctl);
  563. /*
  564. * Keep the default values of all the fields of the EMAC_PTP_FOFF
  565. * register, except set the PTPCOF field to 0x0E.
  566. */
  567. ptpfoff = 0x0E24170C;
  568. bfin_write_EMAC_PTP_FOFF(ptpfoff);
  569. /*
  570. * Program bits [15:0] of the EMAC_PTP_FV1 register to 0x88F7, which
  571. * corresponds to PTP messages on the MAC layer.
  572. */
  573. ptpfv1 = 0x110488F7;
  574. bfin_write_EMAC_PTP_FV1(ptpfv1);
  575. ptpfv2 = 0x0140013F;
  576. bfin_write_EMAC_PTP_FV2(ptpfv2);
  577. /*
  578. * To allow the timestamping of Pdelay_Req and Pdelay_Resp
  579. * messages, set the value to 0xFFF0.
  580. */
  581. ptpfv3 = 0xFFFFFFF0;
  582. bfin_write_EMAC_PTP_FV3(ptpfv3);
  583. config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
  584. break;
  585. default:
  586. return -ERANGE;
  587. }
  588. if (config.tx_type == HWTSTAMP_TX_OFF &&
  589. bfin_mac_hwtstamp_is_none(config.rx_filter)) {
  590. ptpctl &= ~PTP_EN;
  591. bfin_write_EMAC_PTP_CTL(ptpctl);
  592. SSYNC();
  593. } else {
  594. ptpctl |= PTP_EN;
  595. bfin_write_EMAC_PTP_CTL(ptpctl);
  596. /*
  597. * clear any existing timestamp
  598. */
  599. bfin_read_EMAC_PTP_RXSNAPLO();
  600. bfin_read_EMAC_PTP_RXSNAPHI();
  601. bfin_read_EMAC_PTP_TXSNAPLO();
  602. bfin_read_EMAC_PTP_TXSNAPHI();
  603. /*
  604. * Set registers so that rollover occurs soon to test this.
  605. */
  606. bfin_write_EMAC_PTP_TIMELO(0x00000000);
  607. bfin_write_EMAC_PTP_TIMEHI(0xFF800000);
  608. SSYNC();
  609. lp->compare.last_update = 0;
  610. timecounter_init(&lp->clock,
  611. &lp->cycles,
  612. ktime_to_ns(ktime_get_real()));
  613. timecompare_update(&lp->compare, 0);
  614. }
  615. lp->stamp_cfg = config;
  616. return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
  617. -EFAULT : 0;
  618. }
  619. static void bfin_dump_hwtamp(char *s, ktime_t *hw, ktime_t *ts, struct timecompare *cmp)
  620. {
  621. ktime_t sys = ktime_get_real();
  622. pr_debug("%s %s hardware:%d,%d transform system:%d,%d system:%d,%d, cmp:%lld, %lld\n",
  623. __func__, s, hw->tv.sec, hw->tv.nsec, ts->tv.sec, ts->tv.nsec, sys.tv.sec,
  624. sys.tv.nsec, cmp->offset, cmp->skew);
  625. }
  626. static void bfin_tx_hwtstamp(struct net_device *netdev, struct sk_buff *skb)
  627. {
  628. struct bfin_mac_local *lp = netdev_priv(netdev);
  629. union skb_shared_tx *shtx = skb_tx(skb);
  630. if (shtx->hardware) {
  631. int timeout_cnt = MAX_TIMEOUT_CNT;
  632. /* When doing time stamping, keep the connection to the socket
  633. * a while longer
  634. */
  635. shtx->in_progress = 1;
  636. /*
  637. * The timestamping is done at the EMAC module's MII/RMII interface
  638. * when the module sees the Start of Frame of an event message packet. This
  639. * interface is the closest possible place to the physical Ethernet transmission
  640. * medium, providing the best timing accuracy.
  641. */
  642. while ((!(bfin_read_EMAC_PTP_ISTAT() & TXTL)) && (--timeout_cnt))
  643. udelay(1);
  644. if (timeout_cnt == 0)
  645. printk(KERN_ERR DRV_NAME
  646. ": fails to timestamp the TX packet\n");
  647. else {
  648. struct skb_shared_hwtstamps shhwtstamps;
  649. u64 ns;
  650. u64 regval;
  651. regval = bfin_read_EMAC_PTP_TXSNAPLO();
  652. regval |= (u64)bfin_read_EMAC_PTP_TXSNAPHI() << 32;
  653. memset(&shhwtstamps, 0, sizeof(shhwtstamps));
  654. ns = timecounter_cyc2time(&lp->clock,
  655. regval);
  656. timecompare_update(&lp->compare, ns);
  657. shhwtstamps.hwtstamp = ns_to_ktime(ns);
  658. shhwtstamps.syststamp =
  659. timecompare_transform(&lp->compare, ns);
  660. skb_tstamp_tx(skb, &shhwtstamps);
  661. bfin_dump_hwtamp("TX", &shhwtstamps.hwtstamp, &shhwtstamps.syststamp, &lp->compare);
  662. }
  663. }
  664. }
  665. static void bfin_rx_hwtstamp(struct net_device *netdev, struct sk_buff *skb)
  666. {
  667. struct bfin_mac_local *lp = netdev_priv(netdev);
  668. u32 valid;
  669. u64 regval, ns;
  670. struct skb_shared_hwtstamps *shhwtstamps;
  671. if (bfin_mac_hwtstamp_is_none(lp->stamp_cfg.rx_filter))
  672. return;
  673. valid = bfin_read_EMAC_PTP_ISTAT() & RXEL;
  674. if (!valid)
  675. return;
  676. shhwtstamps = skb_hwtstamps(skb);
  677. regval = bfin_read_EMAC_PTP_RXSNAPLO();
  678. regval |= (u64)bfin_read_EMAC_PTP_RXSNAPHI() << 32;
  679. ns = timecounter_cyc2time(&lp->clock, regval);
  680. timecompare_update(&lp->compare, ns);
  681. memset(shhwtstamps, 0, sizeof(*shhwtstamps));
  682. shhwtstamps->hwtstamp = ns_to_ktime(ns);
  683. shhwtstamps->syststamp = timecompare_transform(&lp->compare, ns);
  684. bfin_dump_hwtamp("RX", &shhwtstamps->hwtstamp, &shhwtstamps->syststamp, &lp->compare);
  685. }
  686. /*
  687. * bfin_read_clock - read raw cycle counter (to be used by time counter)
  688. */
  689. static cycle_t bfin_read_clock(const struct cyclecounter *tc)
  690. {
  691. u64 stamp;
  692. stamp = bfin_read_EMAC_PTP_TIMELO();
  693. stamp |= (u64)bfin_read_EMAC_PTP_TIMEHI() << 32ULL;
  694. return stamp;
  695. }
  696. #define PTP_CLK 25000000
  697. static void bfin_mac_hwtstamp_init(struct net_device *netdev)
  698. {
  699. struct bfin_mac_local *lp = netdev_priv(netdev);
  700. u64 append;
  701. /* Initialize hardware timer */
  702. append = PTP_CLK * (1ULL << 32);
  703. do_div(append, get_sclk());
  704. bfin_write_EMAC_PTP_ADDEND((u32)append);
  705. memset(&lp->cycles, 0, sizeof(lp->cycles));
  706. lp->cycles.read = bfin_read_clock;
  707. lp->cycles.mask = CLOCKSOURCE_MASK(64);
  708. lp->cycles.mult = 1000000000 / PTP_CLK;
  709. lp->cycles.shift = 0;
  710. /* Synchronize our NIC clock against system wall clock */
  711. memset(&lp->compare, 0, sizeof(lp->compare));
  712. lp->compare.source = &lp->clock;
  713. lp->compare.target = ktime_get_real;
  714. lp->compare.num_samples = 10;
  715. /* Initialize hwstamp config */
  716. lp->stamp_cfg.rx_filter = HWTSTAMP_FILTER_NONE;
  717. lp->stamp_cfg.tx_type = HWTSTAMP_TX_OFF;
  718. }
  719. #else
  720. # define bfin_mac_hwtstamp_is_none(cfg) 0
  721. # define bfin_mac_hwtstamp_init(dev)
  722. # define bfin_mac_hwtstamp_ioctl(dev, ifr, cmd) (-EOPNOTSUPP)
  723. # define bfin_rx_hwtstamp(dev, skb)
  724. # define bfin_tx_hwtstamp(dev, skb)
  725. #endif
  726. static void adjust_tx_list(void)
  727. {
  728. int timeout_cnt = MAX_TIMEOUT_CNT;
  729. if (tx_list_head->status.status_word != 0 &&
  730. current_tx_ptr != tx_list_head) {
  731. goto adjust_head; /* released something, just return; */
  732. }
  733. /*
  734. * if nothing released, check wait condition
  735. * current's next can not be the head,
  736. * otherwise the dma will not stop as we want
  737. */
  738. if (current_tx_ptr->next->next == tx_list_head) {
  739. while (tx_list_head->status.status_word == 0) {
  740. udelay(10);
  741. if (tx_list_head->status.status_word != 0 ||
  742. !(bfin_read_DMA2_IRQ_STATUS() & DMA_RUN)) {
  743. goto adjust_head;
  744. }
  745. if (timeout_cnt-- < 0) {
  746. printk(KERN_ERR DRV_NAME
  747. ": wait for adjust tx list head timeout\n");
  748. break;
  749. }
  750. }
  751. if (tx_list_head->status.status_word != 0) {
  752. goto adjust_head;
  753. }
  754. }
  755. return;
  756. adjust_head:
  757. do {
  758. tx_list_head->desc_a.config &= ~DMAEN;
  759. tx_list_head->status.status_word = 0;
  760. if (tx_list_head->skb) {
  761. dev_kfree_skb(tx_list_head->skb);
  762. tx_list_head->skb = NULL;
  763. } else {
  764. printk(KERN_ERR DRV_NAME
  765. ": no sk_buff in a transmitted frame!\n");
  766. }
  767. tx_list_head = tx_list_head->next;
  768. } while (tx_list_head->status.status_word != 0 &&
  769. current_tx_ptr != tx_list_head);
  770. return;
  771. }
  772. static int bfin_mac_hard_start_xmit(struct sk_buff *skb,
  773. struct net_device *dev)
  774. {
  775. u16 *data;
  776. u32 data_align = (unsigned long)(skb->data) & 0x3;
  777. union skb_shared_tx *shtx = skb_tx(skb);
  778. current_tx_ptr->skb = skb;
  779. if (data_align == 0x2) {
  780. /* move skb->data to current_tx_ptr payload */
  781. data = (u16 *)(skb->data) - 1;
  782. *data = (u16)(skb->len);
  783. /*
  784. * When transmitting an Ethernet packet, the PTP_TSYNC module requires
  785. * a DMA_Length_Word field associated with the packet. The lower 12 bits
  786. * of this field are the length of the packet payload in bytes and the higher
  787. * 4 bits are the timestamping enable field.
  788. */
  789. if (shtx->hardware)
  790. *data |= 0x1000;
  791. current_tx_ptr->desc_a.start_addr = (u32)data;
  792. /* this is important! */
  793. blackfin_dcache_flush_range((u32)data,
  794. (u32)((u8 *)data + skb->len + 4));
  795. } else {
  796. *((u16 *)(current_tx_ptr->packet)) = (u16)(skb->len);
  797. /* enable timestamping for the sent packet */
  798. if (shtx->hardware)
  799. *((u16 *)(current_tx_ptr->packet)) |= 0x1000;
  800. memcpy((u8 *)(current_tx_ptr->packet + 2), skb->data,
  801. skb->len);
  802. current_tx_ptr->desc_a.start_addr =
  803. (u32)current_tx_ptr->packet;
  804. if (current_tx_ptr->status.status_word != 0)
  805. current_tx_ptr->status.status_word = 0;
  806. blackfin_dcache_flush_range(
  807. (u32)current_tx_ptr->packet,
  808. (u32)(current_tx_ptr->packet + skb->len + 2));
  809. }
  810. /* make sure the internal data buffers in the core are drained
  811. * so that the DMA descriptors are completely written when the
  812. * DMA engine goes to fetch them below
  813. */
  814. SSYNC();
  815. /* enable this packet's dma */
  816. current_tx_ptr->desc_a.config |= DMAEN;
  817. /* tx dma is running, just return */
  818. if (bfin_read_DMA2_IRQ_STATUS() & DMA_RUN)
  819. goto out;
  820. /* tx dma is not running */
  821. bfin_write_DMA2_NEXT_DESC_PTR(&(current_tx_ptr->desc_a));
  822. /* dma enabled, read from memory, size is 6 */
  823. bfin_write_DMA2_CONFIG(current_tx_ptr->desc_a.config);
  824. /* Turn on the EMAC tx */
  825. bfin_write_EMAC_OPMODE(bfin_read_EMAC_OPMODE() | TE);
  826. out:
  827. adjust_tx_list();
  828. bfin_tx_hwtstamp(dev, skb);
  829. current_tx_ptr = current_tx_ptr->next;
  830. dev->stats.tx_packets++;
  831. dev->stats.tx_bytes += (skb->len);
  832. return NETDEV_TX_OK;
  833. }
  834. static void bfin_mac_rx(struct net_device *dev)
  835. {
  836. struct sk_buff *skb, *new_skb;
  837. unsigned short len;
  838. struct bfin_mac_local *lp __maybe_unused = netdev_priv(dev);
  839. /* allocate a new skb for next time receive */
  840. skb = current_rx_ptr->skb;
  841. new_skb = dev_alloc_skb(PKT_BUF_SZ + NET_IP_ALIGN);
  842. if (!new_skb) {
  843. printk(KERN_NOTICE DRV_NAME
  844. ": rx: low on mem - packet dropped\n");
  845. dev->stats.rx_dropped++;
  846. goto out;
  847. }
  848. /* reserve 2 bytes for RXDWA padding */
  849. skb_reserve(new_skb, NET_IP_ALIGN);
  850. current_rx_ptr->skb = new_skb;
  851. current_rx_ptr->desc_a.start_addr = (unsigned long)new_skb->data - 2;
  852. /* Invidate the data cache of skb->data range when it is write back
  853. * cache. It will prevent overwritting the new data from DMA
  854. */
  855. blackfin_dcache_invalidate_range((unsigned long)new_skb->head,
  856. (unsigned long)new_skb->end);
  857. len = (unsigned short)((current_rx_ptr->status.status_word) & RX_FRLEN);
  858. skb_put(skb, len);
  859. blackfin_dcache_invalidate_range((unsigned long)skb->head,
  860. (unsigned long)skb->tail);
  861. skb->protocol = eth_type_trans(skb, dev);
  862. bfin_rx_hwtstamp(dev, skb);
  863. #if defined(BFIN_MAC_CSUM_OFFLOAD)
  864. skb->csum = current_rx_ptr->status.ip_payload_csum;
  865. skb->ip_summed = CHECKSUM_COMPLETE;
  866. #endif
  867. netif_rx(skb);
  868. dev->stats.rx_packets++;
  869. dev->stats.rx_bytes += len;
  870. current_rx_ptr->status.status_word = 0x00000000;
  871. current_rx_ptr = current_rx_ptr->next;
  872. out:
  873. return;
  874. }
  875. /* interrupt routine to handle rx and error signal */
  876. static irqreturn_t bfin_mac_interrupt(int irq, void *dev_id)
  877. {
  878. struct net_device *dev = dev_id;
  879. int number = 0;
  880. get_one_packet:
  881. if (current_rx_ptr->status.status_word == 0) {
  882. /* no more new packet received */
  883. if (number == 0) {
  884. if (current_rx_ptr->next->status.status_word != 0) {
  885. current_rx_ptr = current_rx_ptr->next;
  886. goto real_rx;
  887. }
  888. }
  889. bfin_write_DMA1_IRQ_STATUS(bfin_read_DMA1_IRQ_STATUS() |
  890. DMA_DONE | DMA_ERR);
  891. return IRQ_HANDLED;
  892. }
  893. real_rx:
  894. bfin_mac_rx(dev);
  895. number++;
  896. goto get_one_packet;
  897. }
  898. #ifdef CONFIG_NET_POLL_CONTROLLER
  899. static void bfin_mac_poll(struct net_device *dev)
  900. {
  901. disable_irq(IRQ_MAC_RX);
  902. bfin_mac_interrupt(IRQ_MAC_RX, dev);
  903. enable_irq(IRQ_MAC_RX);
  904. }
  905. #endif /* CONFIG_NET_POLL_CONTROLLER */
  906. static void bfin_mac_disable(void)
  907. {
  908. unsigned int opmode;
  909. opmode = bfin_read_EMAC_OPMODE();
  910. opmode &= (~RE);
  911. opmode &= (~TE);
  912. /* Turn off the EMAC */
  913. bfin_write_EMAC_OPMODE(opmode);
  914. }
  915. /*
  916. * Enable Interrupts, Receive, and Transmit
  917. */
  918. static void bfin_mac_enable(void)
  919. {
  920. u32 opmode;
  921. pr_debug("%s: %s\n", DRV_NAME, __func__);
  922. /* Set RX DMA */
  923. bfin_write_DMA1_NEXT_DESC_PTR(&(rx_list_head->desc_a));
  924. bfin_write_DMA1_CONFIG(rx_list_head->desc_a.config);
  925. /* Wait MII done */
  926. bfin_mdio_poll();
  927. /* We enable only RX here */
  928. /* ASTP : Enable Automatic Pad Stripping
  929. PR : Promiscuous Mode for test
  930. PSF : Receive frames with total length less than 64 bytes.
  931. FDMODE : Full Duplex Mode
  932. LB : Internal Loopback for test
  933. RE : Receiver Enable */
  934. opmode = bfin_read_EMAC_OPMODE();
  935. if (opmode & FDMODE)
  936. opmode |= PSF;
  937. else
  938. opmode |= DRO | DC | PSF;
  939. opmode |= RE;
  940. #if defined(CONFIG_BFIN_MAC_RMII)
  941. opmode |= RMII; /* For Now only 100MBit are supported */
  942. #if (defined(CONFIG_BF537) || defined(CONFIG_BF536)) && CONFIG_BF_REV_0_2
  943. opmode |= TE;
  944. #endif
  945. #endif
  946. /* Turn on the EMAC rx */
  947. bfin_write_EMAC_OPMODE(opmode);
  948. }
  949. /* Our watchdog timed out. Called by the networking layer */
  950. static void bfin_mac_timeout(struct net_device *dev)
  951. {
  952. pr_debug("%s: %s\n", dev->name, __func__);
  953. bfin_mac_disable();
  954. /* reset tx queue */
  955. tx_list_tail = tx_list_head->next;
  956. bfin_mac_enable();
  957. /* We can accept TX packets again */
  958. dev->trans_start = jiffies; /* prevent tx timeout */
  959. netif_wake_queue(dev);
  960. }
  961. static void bfin_mac_multicast_hash(struct net_device *dev)
  962. {
  963. u32 emac_hashhi, emac_hashlo;
  964. struct netdev_hw_addr *ha;
  965. char *addrs;
  966. u32 crc;
  967. emac_hashhi = emac_hashlo = 0;
  968. netdev_for_each_mc_addr(ha, dev) {
  969. addrs = ha->addr;
  970. /* skip non-multicast addresses */
  971. if (!(*addrs & 1))
  972. continue;
  973. crc = ether_crc(ETH_ALEN, addrs);
  974. crc >>= 26;
  975. if (crc & 0x20)
  976. emac_hashhi |= 1 << (crc & 0x1f);
  977. else
  978. emac_hashlo |= 1 << (crc & 0x1f);
  979. }
  980. bfin_write_EMAC_HASHHI(emac_hashhi);
  981. bfin_write_EMAC_HASHLO(emac_hashlo);
  982. }
  983. /*
  984. * This routine will, depending on the values passed to it,
  985. * either make it accept multicast packets, go into
  986. * promiscuous mode (for TCPDUMP and cousins) or accept
  987. * a select set of multicast packets
  988. */
  989. static void bfin_mac_set_multicast_list(struct net_device *dev)
  990. {
  991. u32 sysctl;
  992. if (dev->flags & IFF_PROMISC) {
  993. printk(KERN_INFO "%s: set to promisc mode\n", dev->name);
  994. sysctl = bfin_read_EMAC_OPMODE();
  995. sysctl |= RAF;
  996. bfin_write_EMAC_OPMODE(sysctl);
  997. } else if (dev->flags & IFF_ALLMULTI) {
  998. /* accept all multicast */
  999. sysctl = bfin_read_EMAC_OPMODE();
  1000. sysctl |= PAM;
  1001. bfin_write_EMAC_OPMODE(sysctl);
  1002. } else if (!netdev_mc_empty(dev)) {
  1003. /* set up multicast hash table */
  1004. sysctl = bfin_read_EMAC_OPMODE();
  1005. sysctl |= HM;
  1006. bfin_write_EMAC_OPMODE(sysctl);
  1007. bfin_mac_multicast_hash(dev);
  1008. } else {
  1009. /* clear promisc or multicast mode */
  1010. sysctl = bfin_read_EMAC_OPMODE();
  1011. sysctl &= ~(RAF | PAM);
  1012. bfin_write_EMAC_OPMODE(sysctl);
  1013. }
  1014. }
  1015. static int bfin_mac_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  1016. {
  1017. switch (cmd) {
  1018. case SIOCSHWTSTAMP:
  1019. return bfin_mac_hwtstamp_ioctl(netdev, ifr, cmd);
  1020. default:
  1021. return -EOPNOTSUPP;
  1022. }
  1023. }
  1024. /*
  1025. * this puts the device in an inactive state
  1026. */
  1027. static void bfin_mac_shutdown(struct net_device *dev)
  1028. {
  1029. /* Turn off the EMAC */
  1030. bfin_write_EMAC_OPMODE(0x00000000);
  1031. /* Turn off the EMAC RX DMA */
  1032. bfin_write_DMA1_CONFIG(0x0000);
  1033. bfin_write_DMA2_CONFIG(0x0000);
  1034. }
  1035. /*
  1036. * Open and Initialize the interface
  1037. *
  1038. * Set up everything, reset the card, etc..
  1039. */
  1040. static int bfin_mac_open(struct net_device *dev)
  1041. {
  1042. struct bfin_mac_local *lp = netdev_priv(dev);
  1043. int retval;
  1044. pr_debug("%s: %s\n", dev->name, __func__);
  1045. /*
  1046. * Check that the address is valid. If its not, refuse
  1047. * to bring the device up. The user must specify an
  1048. * address using ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx
  1049. */
  1050. if (!is_valid_ether_addr(dev->dev_addr)) {
  1051. printk(KERN_WARNING DRV_NAME ": no valid ethernet hw addr\n");
  1052. return -EINVAL;
  1053. }
  1054. /* initial rx and tx list */
  1055. retval = desc_list_init();
  1056. if (retval)
  1057. return retval;
  1058. phy_start(lp->phydev);
  1059. phy_write(lp->phydev, MII_BMCR, BMCR_RESET);
  1060. setup_system_regs(dev);
  1061. setup_mac_addr(dev->dev_addr);
  1062. bfin_mac_disable();
  1063. bfin_mac_enable();
  1064. pr_debug("hardware init finished\n");
  1065. netif_start_queue(dev);
  1066. netif_carrier_on(dev);
  1067. return 0;
  1068. }
  1069. /*
  1070. * this makes the board clean up everything that it can
  1071. * and not talk to the outside world. Caused by
  1072. * an 'ifconfig ethX down'
  1073. */
  1074. static int bfin_mac_close(struct net_device *dev)
  1075. {
  1076. struct bfin_mac_local *lp = netdev_priv(dev);
  1077. pr_debug("%s: %s\n", dev->name, __func__);
  1078. netif_stop_queue(dev);
  1079. netif_carrier_off(dev);
  1080. phy_stop(lp->phydev);
  1081. phy_write(lp->phydev, MII_BMCR, BMCR_PDOWN);
  1082. /* clear everything */
  1083. bfin_mac_shutdown(dev);
  1084. /* free the rx/tx buffers */
  1085. desc_list_free();
  1086. return 0;
  1087. }
  1088. static const struct net_device_ops bfin_mac_netdev_ops = {
  1089. .ndo_open = bfin_mac_open,
  1090. .ndo_stop = bfin_mac_close,
  1091. .ndo_start_xmit = bfin_mac_hard_start_xmit,
  1092. .ndo_set_mac_address = bfin_mac_set_mac_address,
  1093. .ndo_tx_timeout = bfin_mac_timeout,
  1094. .ndo_set_multicast_list = bfin_mac_set_multicast_list,
  1095. .ndo_do_ioctl = bfin_mac_ioctl,
  1096. .ndo_validate_addr = eth_validate_addr,
  1097. .ndo_change_mtu = eth_change_mtu,
  1098. #ifdef CONFIG_NET_POLL_CONTROLLER
  1099. .ndo_poll_controller = bfin_mac_poll,
  1100. #endif
  1101. };
  1102. static int __devinit bfin_mac_probe(struct platform_device *pdev)
  1103. {
  1104. struct net_device *ndev;
  1105. struct bfin_mac_local *lp;
  1106. struct platform_device *pd;
  1107. int rc;
  1108. ndev = alloc_etherdev(sizeof(struct bfin_mac_local));
  1109. if (!ndev) {
  1110. dev_err(&pdev->dev, "Cannot allocate net device!\n");
  1111. return -ENOMEM;
  1112. }
  1113. SET_NETDEV_DEV(ndev, &pdev->dev);
  1114. platform_set_drvdata(pdev, ndev);
  1115. lp = netdev_priv(ndev);
  1116. /* Grab the MAC address in the MAC */
  1117. *(__le32 *) (&(ndev->dev_addr[0])) = cpu_to_le32(bfin_read_EMAC_ADDRLO());
  1118. *(__le16 *) (&(ndev->dev_addr[4])) = cpu_to_le16((u16) bfin_read_EMAC_ADDRHI());
  1119. /* probe mac */
  1120. /*todo: how to proble? which is revision_register */
  1121. bfin_write_EMAC_ADDRLO(0x12345678);
  1122. if (bfin_read_EMAC_ADDRLO() != 0x12345678) {
  1123. dev_err(&pdev->dev, "Cannot detect Blackfin on-chip ethernet MAC controller!\n");
  1124. rc = -ENODEV;
  1125. goto out_err_probe_mac;
  1126. }
  1127. /*
  1128. * Is it valid? (Did bootloader initialize it?)
  1129. * Grab the MAC from the board somehow
  1130. * this is done in the arch/blackfin/mach-bfxxx/boards/eth_mac.c
  1131. */
  1132. if (!is_valid_ether_addr(ndev->dev_addr))
  1133. bfin_get_ether_addr(ndev->dev_addr);
  1134. /* If still not valid, get a random one */
  1135. if (!is_valid_ether_addr(ndev->dev_addr))
  1136. random_ether_addr(ndev->dev_addr);
  1137. setup_mac_addr(ndev->dev_addr);
  1138. if (!pdev->dev.platform_data) {
  1139. dev_err(&pdev->dev, "Cannot get platform device bfin_mii_bus!\n");
  1140. rc = -ENODEV;
  1141. goto out_err_probe_mac;
  1142. }
  1143. pd = pdev->dev.platform_data;
  1144. lp->mii_bus = platform_get_drvdata(pd);
  1145. lp->mii_bus->priv = ndev;
  1146. rc = mii_probe(ndev);
  1147. if (rc) {
  1148. dev_err(&pdev->dev, "MII Probe failed!\n");
  1149. goto out_err_mii_probe;
  1150. }
  1151. /* Fill in the fields of the device structure with ethernet values. */
  1152. ether_setup(ndev);
  1153. ndev->netdev_ops = &bfin_mac_netdev_ops;
  1154. ndev->ethtool_ops = &bfin_mac_ethtool_ops;
  1155. spin_lock_init(&lp->lock);
  1156. /* now, enable interrupts */
  1157. /* register irq handler */
  1158. rc = request_irq(IRQ_MAC_RX, bfin_mac_interrupt,
  1159. IRQF_DISABLED, "EMAC_RX", ndev);
  1160. if (rc) {
  1161. dev_err(&pdev->dev, "Cannot request Blackfin MAC RX IRQ!\n");
  1162. rc = -EBUSY;
  1163. goto out_err_request_irq;
  1164. }
  1165. rc = register_netdev(ndev);
  1166. if (rc) {
  1167. dev_err(&pdev->dev, "Cannot register net device!\n");
  1168. goto out_err_reg_ndev;
  1169. }
  1170. bfin_mac_hwtstamp_init(ndev);
  1171. /* now, print out the card info, in a short format.. */
  1172. dev_info(&pdev->dev, "%s, Version %s\n", DRV_DESC, DRV_VERSION);
  1173. return 0;
  1174. out_err_reg_ndev:
  1175. free_irq(IRQ_MAC_RX, ndev);
  1176. out_err_request_irq:
  1177. out_err_mii_probe:
  1178. mdiobus_unregister(lp->mii_bus);
  1179. mdiobus_free(lp->mii_bus);
  1180. peripheral_free_list(pin_req);
  1181. out_err_probe_mac:
  1182. platform_set_drvdata(pdev, NULL);
  1183. free_netdev(ndev);
  1184. return rc;
  1185. }
  1186. static int __devexit bfin_mac_remove(struct platform_device *pdev)
  1187. {
  1188. struct net_device *ndev = platform_get_drvdata(pdev);
  1189. struct bfin_mac_local *lp = netdev_priv(ndev);
  1190. platform_set_drvdata(pdev, NULL);
  1191. lp->mii_bus->priv = NULL;
  1192. unregister_netdev(ndev);
  1193. free_irq(IRQ_MAC_RX, ndev);
  1194. free_netdev(ndev);
  1195. peripheral_free_list(pin_req);
  1196. return 0;
  1197. }
  1198. #ifdef CONFIG_PM
  1199. static int bfin_mac_suspend(struct platform_device *pdev, pm_message_t mesg)
  1200. {
  1201. struct net_device *net_dev = platform_get_drvdata(pdev);
  1202. if (netif_running(net_dev))
  1203. bfin_mac_close(net_dev);
  1204. return 0;
  1205. }
  1206. static int bfin_mac_resume(struct platform_device *pdev)
  1207. {
  1208. struct net_device *net_dev = platform_get_drvdata(pdev);
  1209. if (netif_running(net_dev))
  1210. bfin_mac_open(net_dev);
  1211. return 0;
  1212. }
  1213. #else
  1214. #define bfin_mac_suspend NULL
  1215. #define bfin_mac_resume NULL
  1216. #endif /* CONFIG_PM */
  1217. static int __devinit bfin_mii_bus_probe(struct platform_device *pdev)
  1218. {
  1219. struct mii_bus *miibus;
  1220. int rc, i;
  1221. /*
  1222. * We are setting up a network card,
  1223. * so set the GPIO pins to Ethernet mode
  1224. */
  1225. rc = peripheral_request_list(pin_req, DRV_NAME);
  1226. if (rc) {
  1227. dev_err(&pdev->dev, "Requesting peripherals failed!\n");
  1228. return rc;
  1229. }
  1230. rc = -ENOMEM;
  1231. miibus = mdiobus_alloc();
  1232. if (miibus == NULL)
  1233. goto out_err_alloc;
  1234. miibus->read = bfin_mdiobus_read;
  1235. miibus->write = bfin_mdiobus_write;
  1236. miibus->reset = bfin_mdiobus_reset;
  1237. miibus->parent = &pdev->dev;
  1238. miibus->name = "bfin_mii_bus";
  1239. snprintf(miibus->id, MII_BUS_ID_SIZE, "0");
  1240. miibus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
  1241. if (miibus->irq == NULL)
  1242. goto out_err_alloc;
  1243. for (i = 0; i < PHY_MAX_ADDR; ++i)
  1244. miibus->irq[i] = PHY_POLL;
  1245. rc = mdiobus_register(miibus);
  1246. if (rc) {
  1247. dev_err(&pdev->dev, "Cannot register MDIO bus!\n");
  1248. goto out_err_mdiobus_register;
  1249. }
  1250. platform_set_drvdata(pdev, miibus);
  1251. return 0;
  1252. out_err_mdiobus_register:
  1253. mdiobus_free(miibus);
  1254. out_err_alloc:
  1255. peripheral_free_list(pin_req);
  1256. return rc;
  1257. }
  1258. static int __devexit bfin_mii_bus_remove(struct platform_device *pdev)
  1259. {
  1260. struct mii_bus *miibus = platform_get_drvdata(pdev);
  1261. platform_set_drvdata(pdev, NULL);
  1262. mdiobus_unregister(miibus);
  1263. mdiobus_free(miibus);
  1264. peripheral_free_list(pin_req);
  1265. return 0;
  1266. }
  1267. static struct platform_driver bfin_mii_bus_driver = {
  1268. .probe = bfin_mii_bus_probe,
  1269. .remove = __devexit_p(bfin_mii_bus_remove),
  1270. .driver = {
  1271. .name = "bfin_mii_bus",
  1272. .owner = THIS_MODULE,
  1273. },
  1274. };
  1275. static struct platform_driver bfin_mac_driver = {
  1276. .probe = bfin_mac_probe,
  1277. .remove = __devexit_p(bfin_mac_remove),
  1278. .resume = bfin_mac_resume,
  1279. .suspend = bfin_mac_suspend,
  1280. .driver = {
  1281. .name = DRV_NAME,
  1282. .owner = THIS_MODULE,
  1283. },
  1284. };
  1285. static int __init bfin_mac_init(void)
  1286. {
  1287. int ret;
  1288. ret = platform_driver_register(&bfin_mii_bus_driver);
  1289. if (!ret)
  1290. return platform_driver_register(&bfin_mac_driver);
  1291. return -ENODEV;
  1292. }
  1293. module_init(bfin_mac_init);
  1294. static void __exit bfin_mac_cleanup(void)
  1295. {
  1296. platform_driver_unregister(&bfin_mac_driver);
  1297. platform_driver_unregister(&bfin_mii_bus_driver);
  1298. }
  1299. module_exit(bfin_mac_cleanup);