bfin_mac.c 41 KB

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