bfin_mac.c 43 KB

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