bfin_mac.c 46 KB

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