bfin_mac.c 42 KB

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