bfin_mac.c 46 KB

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