bfin_mac.c 42 KB

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