bfin_mac.c 46 KB

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