sni_ave.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982
  1. // SPDX-License-Identifier: GPL-2.0
  2. /**
  3. * sni_ave.c - Socionext UniPhier AVE ethernet driver
  4. * Copyright 2014 Panasonic Corporation
  5. * Copyright 2015-2017 Socionext Inc.
  6. */
  7. #include <linux/bitops.h>
  8. #include <linux/clk.h>
  9. #include <linux/etherdevice.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/io.h>
  12. #include <linux/iopoll.h>
  13. #include <linux/mfd/syscon.h>
  14. #include <linux/mii.h>
  15. #include <linux/module.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/of_net.h>
  18. #include <linux/of_mdio.h>
  19. #include <linux/of_platform.h>
  20. #include <linux/phy.h>
  21. #include <linux/regmap.h>
  22. #include <linux/reset.h>
  23. #include <linux/types.h>
  24. #include <linux/u64_stats_sync.h>
  25. /* General Register Group */
  26. #define AVE_IDR 0x000 /* ID */
  27. #define AVE_VR 0x004 /* Version */
  28. #define AVE_GRR 0x008 /* Global Reset */
  29. #define AVE_CFGR 0x00c /* Configuration */
  30. /* Interrupt Register Group */
  31. #define AVE_GIMR 0x100 /* Global Interrupt Mask */
  32. #define AVE_GISR 0x104 /* Global Interrupt Status */
  33. /* MAC Register Group */
  34. #define AVE_TXCR 0x200 /* TX Setup */
  35. #define AVE_RXCR 0x204 /* RX Setup */
  36. #define AVE_RXMAC1R 0x208 /* MAC address (lower) */
  37. #define AVE_RXMAC2R 0x20c /* MAC address (upper) */
  38. #define AVE_MDIOCTR 0x214 /* MDIO Control */
  39. #define AVE_MDIOAR 0x218 /* MDIO Address */
  40. #define AVE_MDIOWDR 0x21c /* MDIO Data */
  41. #define AVE_MDIOSR 0x220 /* MDIO Status */
  42. #define AVE_MDIORDR 0x224 /* MDIO Rd Data */
  43. /* Descriptor Control Register Group */
  44. #define AVE_DESCC 0x300 /* Descriptor Control */
  45. #define AVE_TXDC 0x304 /* TX Descriptor Configuration */
  46. #define AVE_RXDC0 0x308 /* RX Descriptor Ring0 Configuration */
  47. #define AVE_IIRQC 0x34c /* Interval IRQ Control */
  48. /* Packet Filter Register Group */
  49. #define AVE_PKTF_BASE 0x800 /* PF Base Address */
  50. #define AVE_PFMBYTE_BASE 0xd00 /* PF Mask Byte Base Address */
  51. #define AVE_PFMBIT_BASE 0xe00 /* PF Mask Bit Base Address */
  52. #define AVE_PFSEL_BASE 0xf00 /* PF Selector Base Address */
  53. #define AVE_PFEN 0xffc /* Packet Filter Enable */
  54. #define AVE_PKTF(ent) (AVE_PKTF_BASE + (ent) * 0x40)
  55. #define AVE_PFMBYTE(ent) (AVE_PFMBYTE_BASE + (ent) * 8)
  56. #define AVE_PFMBIT(ent) (AVE_PFMBIT_BASE + (ent) * 4)
  57. #define AVE_PFSEL(ent) (AVE_PFSEL_BASE + (ent) * 4)
  58. /* 64bit descriptor memory */
  59. #define AVE_DESC_SIZE_64 12 /* Descriptor Size */
  60. #define AVE_TXDM_64 0x1000 /* Tx Descriptor Memory */
  61. #define AVE_RXDM_64 0x1c00 /* Rx Descriptor Memory */
  62. #define AVE_TXDM_SIZE_64 0x0ba0 /* Tx Descriptor Memory Size 3KB */
  63. #define AVE_RXDM_SIZE_64 0x6000 /* Rx Descriptor Memory Size 24KB */
  64. /* 32bit descriptor memory */
  65. #define AVE_DESC_SIZE_32 8 /* Descriptor Size */
  66. #define AVE_TXDM_32 0x1000 /* Tx Descriptor Memory */
  67. #define AVE_RXDM_32 0x1800 /* Rx Descriptor Memory */
  68. #define AVE_TXDM_SIZE_32 0x07c0 /* Tx Descriptor Memory Size 2KB */
  69. #define AVE_RXDM_SIZE_32 0x4000 /* Rx Descriptor Memory Size 16KB */
  70. /* RMII Bridge Register Group */
  71. #define AVE_RSTCTRL 0x8028 /* Reset control */
  72. #define AVE_RSTCTRL_RMIIRST BIT(16)
  73. #define AVE_LINKSEL 0x8034 /* Link speed setting */
  74. #define AVE_LINKSEL_100M BIT(0)
  75. /* AVE_GRR */
  76. #define AVE_GRR_RXFFR BIT(5) /* Reset RxFIFO */
  77. #define AVE_GRR_PHYRST BIT(4) /* Reset external PHY */
  78. #define AVE_GRR_GRST BIT(0) /* Reset all MAC */
  79. /* AVE_CFGR */
  80. #define AVE_CFGR_FLE BIT(31) /* Filter Function */
  81. #define AVE_CFGR_CHE BIT(30) /* Checksum Function */
  82. #define AVE_CFGR_MII BIT(27) /* Func mode (1:MII/RMII, 0:RGMII) */
  83. #define AVE_CFGR_IPFCEN BIT(24) /* IP fragment sum Enable */
  84. /* AVE_GISR (common with GIMR) */
  85. #define AVE_GI_PHY BIT(24) /* PHY interrupt */
  86. #define AVE_GI_TX BIT(16) /* Tx complete */
  87. #define AVE_GI_RXERR BIT(8) /* Receive frame more than max size */
  88. #define AVE_GI_RXOVF BIT(7) /* Overflow at the RxFIFO */
  89. #define AVE_GI_RXDROP BIT(6) /* Drop packet */
  90. #define AVE_GI_RXIINT BIT(5) /* Interval interrupt */
  91. /* AVE_TXCR */
  92. #define AVE_TXCR_FLOCTR BIT(18) /* Flow control */
  93. #define AVE_TXCR_TXSPD_1G BIT(17)
  94. #define AVE_TXCR_TXSPD_100 BIT(16)
  95. /* AVE_RXCR */
  96. #define AVE_RXCR_RXEN BIT(30) /* Rx enable */
  97. #define AVE_RXCR_FDUPEN BIT(22) /* Interface mode */
  98. #define AVE_RXCR_FLOCTR BIT(21) /* Flow control */
  99. #define AVE_RXCR_AFEN BIT(19) /* MAC address filter */
  100. #define AVE_RXCR_DRPEN BIT(18) /* Drop pause frame */
  101. #define AVE_RXCR_MPSIZ_MASK GENMASK(10, 0)
  102. /* AVE_MDIOCTR */
  103. #define AVE_MDIOCTR_RREQ BIT(3) /* Read request */
  104. #define AVE_MDIOCTR_WREQ BIT(2) /* Write request */
  105. /* AVE_MDIOSR */
  106. #define AVE_MDIOSR_STS BIT(0) /* access status */
  107. /* AVE_DESCC */
  108. #define AVE_DESCC_STATUS_MASK GENMASK(31, 16)
  109. #define AVE_DESCC_RD0 BIT(8) /* Enable Rx descriptor Ring0 */
  110. #define AVE_DESCC_RDSTP BIT(4) /* Pause Rx descriptor */
  111. #define AVE_DESCC_TD BIT(0) /* Enable Tx descriptor */
  112. /* AVE_TXDC */
  113. #define AVE_TXDC_SIZE GENMASK(27, 16) /* Size of Tx descriptor */
  114. #define AVE_TXDC_ADDR GENMASK(11, 0) /* Start address */
  115. #define AVE_TXDC_ADDR_START 0
  116. /* AVE_RXDC0 */
  117. #define AVE_RXDC0_SIZE GENMASK(30, 16) /* Size of Rx descriptor */
  118. #define AVE_RXDC0_ADDR GENMASK(14, 0) /* Start address */
  119. #define AVE_RXDC0_ADDR_START 0
  120. /* AVE_IIRQC */
  121. #define AVE_IIRQC_EN0 BIT(27) /* Enable interval interrupt Ring0 */
  122. #define AVE_IIRQC_BSCK GENMASK(15, 0) /* Interval count unit */
  123. /* Command status for descriptor */
  124. #define AVE_STS_OWN BIT(31) /* Descriptor ownership */
  125. #define AVE_STS_INTR BIT(29) /* Request for interrupt */
  126. #define AVE_STS_OK BIT(27) /* Normal transmit */
  127. /* TX */
  128. #define AVE_STS_NOCSUM BIT(28) /* No use HW checksum */
  129. #define AVE_STS_1ST BIT(26) /* Head of buffer chain */
  130. #define AVE_STS_LAST BIT(25) /* Tail of buffer chain */
  131. #define AVE_STS_OWC BIT(21) /* Out of window,Late Collision */
  132. #define AVE_STS_EC BIT(20) /* Excess collision occurred */
  133. #define AVE_STS_PKTLEN_TX_MASK GENMASK(15, 0)
  134. /* RX */
  135. #define AVE_STS_CSSV BIT(21) /* Checksum check performed */
  136. #define AVE_STS_CSER BIT(20) /* Checksum error detected */
  137. #define AVE_STS_PKTLEN_RX_MASK GENMASK(10, 0)
  138. /* Packet filter */
  139. #define AVE_PFMBYTE_MASK0 (GENMASK(31, 8) | GENMASK(5, 0))
  140. #define AVE_PFMBYTE_MASK1 GENMASK(25, 0)
  141. #define AVE_PFMBIT_MASK GENMASK(15, 0)
  142. #define AVE_PF_SIZE 17 /* Number of all packet filter */
  143. #define AVE_PF_MULTICAST_SIZE 7 /* Number of multicast filter */
  144. #define AVE_PFNUM_FILTER 0 /* No.0 */
  145. #define AVE_PFNUM_UNICAST 1 /* No.1 */
  146. #define AVE_PFNUM_BROADCAST 2 /* No.2 */
  147. #define AVE_PFNUM_MULTICAST 11 /* No.11-17 */
  148. /* NETIF Message control */
  149. #define AVE_DEFAULT_MSG_ENABLE (NETIF_MSG_DRV | \
  150. NETIF_MSG_PROBE | \
  151. NETIF_MSG_LINK | \
  152. NETIF_MSG_TIMER | \
  153. NETIF_MSG_IFDOWN | \
  154. NETIF_MSG_IFUP | \
  155. NETIF_MSG_RX_ERR | \
  156. NETIF_MSG_TX_ERR)
  157. /* Parameter for descriptor */
  158. #define AVE_NR_TXDESC 64 /* Tx descriptor */
  159. #define AVE_NR_RXDESC 256 /* Rx descriptor */
  160. #define AVE_DESC_OFS_CMDSTS 0
  161. #define AVE_DESC_OFS_ADDRL 4
  162. #define AVE_DESC_OFS_ADDRU 8
  163. /* Parameter for ethernet frame */
  164. #define AVE_MAX_ETHFRAME 1518
  165. #define AVE_FRAME_HEADROOM 2
  166. /* Parameter for interrupt */
  167. #define AVE_INTM_COUNT 20
  168. #define AVE_FORCE_TXINTCNT 1
  169. /* SG */
  170. #define SG_ETPINMODE 0x540
  171. #define SG_ETPINMODE_EXTPHY BIT(1) /* for LD11 */
  172. #define SG_ETPINMODE_RMII(ins) BIT(ins)
  173. #define IS_DESC_64BIT(p) ((p)->data->is_desc_64bit)
  174. #define AVE_MAX_CLKS 4
  175. #define AVE_MAX_RSTS 2
  176. enum desc_id {
  177. AVE_DESCID_RX,
  178. AVE_DESCID_TX,
  179. };
  180. enum desc_state {
  181. AVE_DESC_RX_PERMIT,
  182. AVE_DESC_RX_SUSPEND,
  183. AVE_DESC_START,
  184. AVE_DESC_STOP,
  185. };
  186. struct ave_desc {
  187. struct sk_buff *skbs;
  188. dma_addr_t skbs_dma;
  189. size_t skbs_dmalen;
  190. };
  191. struct ave_desc_info {
  192. u32 ndesc; /* number of descriptor */
  193. u32 daddr; /* start address of descriptor */
  194. u32 proc_idx; /* index of processing packet */
  195. u32 done_idx; /* index of processed packet */
  196. struct ave_desc *desc; /* skb info related descriptor */
  197. };
  198. struct ave_stats {
  199. struct u64_stats_sync syncp;
  200. u64 packets;
  201. u64 bytes;
  202. u64 errors;
  203. u64 dropped;
  204. u64 collisions;
  205. u64 fifo_errors;
  206. };
  207. struct ave_private {
  208. void __iomem *base;
  209. int irq;
  210. int phy_id;
  211. unsigned int desc_size;
  212. u32 msg_enable;
  213. int nclks;
  214. struct clk *clk[AVE_MAX_CLKS];
  215. int nrsts;
  216. struct reset_control *rst[AVE_MAX_RSTS];
  217. phy_interface_t phy_mode;
  218. struct phy_device *phydev;
  219. struct mii_bus *mdio;
  220. struct regmap *regmap;
  221. unsigned int pinmode_mask;
  222. unsigned int pinmode_val;
  223. u32 wolopts;
  224. /* stats */
  225. struct ave_stats stats_rx;
  226. struct ave_stats stats_tx;
  227. /* NAPI support */
  228. struct net_device *ndev;
  229. struct napi_struct napi_rx;
  230. struct napi_struct napi_tx;
  231. /* descriptor */
  232. struct ave_desc_info rx;
  233. struct ave_desc_info tx;
  234. /* flow control */
  235. int pause_auto;
  236. int pause_rx;
  237. int pause_tx;
  238. const struct ave_soc_data *data;
  239. };
  240. struct ave_soc_data {
  241. bool is_desc_64bit;
  242. const char *clock_names[AVE_MAX_CLKS];
  243. const char *reset_names[AVE_MAX_RSTS];
  244. int (*get_pinmode)(struct ave_private *priv,
  245. phy_interface_t phy_mode, u32 arg);
  246. };
  247. static u32 ave_desc_read(struct net_device *ndev, enum desc_id id, int entry,
  248. int offset)
  249. {
  250. struct ave_private *priv = netdev_priv(ndev);
  251. u32 addr;
  252. addr = ((id == AVE_DESCID_TX) ? priv->tx.daddr : priv->rx.daddr)
  253. + entry * priv->desc_size + offset;
  254. return readl(priv->base + addr);
  255. }
  256. static u32 ave_desc_read_cmdsts(struct net_device *ndev, enum desc_id id,
  257. int entry)
  258. {
  259. return ave_desc_read(ndev, id, entry, AVE_DESC_OFS_CMDSTS);
  260. }
  261. static void ave_desc_write(struct net_device *ndev, enum desc_id id,
  262. int entry, int offset, u32 val)
  263. {
  264. struct ave_private *priv = netdev_priv(ndev);
  265. u32 addr;
  266. addr = ((id == AVE_DESCID_TX) ? priv->tx.daddr : priv->rx.daddr)
  267. + entry * priv->desc_size + offset;
  268. writel(val, priv->base + addr);
  269. }
  270. static void ave_desc_write_cmdsts(struct net_device *ndev, enum desc_id id,
  271. int entry, u32 val)
  272. {
  273. ave_desc_write(ndev, id, entry, AVE_DESC_OFS_CMDSTS, val);
  274. }
  275. static void ave_desc_write_addr(struct net_device *ndev, enum desc_id id,
  276. int entry, dma_addr_t paddr)
  277. {
  278. struct ave_private *priv = netdev_priv(ndev);
  279. ave_desc_write(ndev, id, entry, AVE_DESC_OFS_ADDRL,
  280. lower_32_bits(paddr));
  281. if (IS_DESC_64BIT(priv))
  282. ave_desc_write(ndev, id,
  283. entry, AVE_DESC_OFS_ADDRU,
  284. upper_32_bits(paddr));
  285. }
  286. static u32 ave_irq_disable_all(struct net_device *ndev)
  287. {
  288. struct ave_private *priv = netdev_priv(ndev);
  289. u32 ret;
  290. ret = readl(priv->base + AVE_GIMR);
  291. writel(0, priv->base + AVE_GIMR);
  292. return ret;
  293. }
  294. static void ave_irq_restore(struct net_device *ndev, u32 val)
  295. {
  296. struct ave_private *priv = netdev_priv(ndev);
  297. writel(val, priv->base + AVE_GIMR);
  298. }
  299. static void ave_irq_enable(struct net_device *ndev, u32 bitflag)
  300. {
  301. struct ave_private *priv = netdev_priv(ndev);
  302. writel(readl(priv->base + AVE_GIMR) | bitflag, priv->base + AVE_GIMR);
  303. writel(bitflag, priv->base + AVE_GISR);
  304. }
  305. static void ave_hw_write_macaddr(struct net_device *ndev,
  306. const unsigned char *mac_addr,
  307. int reg1, int reg2)
  308. {
  309. struct ave_private *priv = netdev_priv(ndev);
  310. writel(mac_addr[0] | mac_addr[1] << 8 |
  311. mac_addr[2] << 16 | mac_addr[3] << 24, priv->base + reg1);
  312. writel(mac_addr[4] | mac_addr[5] << 8, priv->base + reg2);
  313. }
  314. static void ave_hw_read_version(struct net_device *ndev, char *buf, int len)
  315. {
  316. struct ave_private *priv = netdev_priv(ndev);
  317. u32 major, minor, vr;
  318. vr = readl(priv->base + AVE_VR);
  319. major = (vr & GENMASK(15, 8)) >> 8;
  320. minor = (vr & GENMASK(7, 0));
  321. snprintf(buf, len, "v%u.%u", major, minor);
  322. }
  323. static void ave_ethtool_get_drvinfo(struct net_device *ndev,
  324. struct ethtool_drvinfo *info)
  325. {
  326. struct device *dev = ndev->dev.parent;
  327. strlcpy(info->driver, dev->driver->name, sizeof(info->driver));
  328. strlcpy(info->bus_info, dev_name(dev), sizeof(info->bus_info));
  329. ave_hw_read_version(ndev, info->fw_version, sizeof(info->fw_version));
  330. }
  331. static u32 ave_ethtool_get_msglevel(struct net_device *ndev)
  332. {
  333. struct ave_private *priv = netdev_priv(ndev);
  334. return priv->msg_enable;
  335. }
  336. static void ave_ethtool_set_msglevel(struct net_device *ndev, u32 val)
  337. {
  338. struct ave_private *priv = netdev_priv(ndev);
  339. priv->msg_enable = val;
  340. }
  341. static void ave_ethtool_get_wol(struct net_device *ndev,
  342. struct ethtool_wolinfo *wol)
  343. {
  344. wol->supported = 0;
  345. wol->wolopts = 0;
  346. if (ndev->phydev)
  347. phy_ethtool_get_wol(ndev->phydev, wol);
  348. }
  349. static int __ave_ethtool_set_wol(struct net_device *ndev,
  350. struct ethtool_wolinfo *wol)
  351. {
  352. if (!ndev->phydev ||
  353. (wol->wolopts & (WAKE_ARP | WAKE_MAGICSECURE)))
  354. return -EOPNOTSUPP;
  355. return phy_ethtool_set_wol(ndev->phydev, wol);
  356. }
  357. static int ave_ethtool_set_wol(struct net_device *ndev,
  358. struct ethtool_wolinfo *wol)
  359. {
  360. int ret;
  361. ret = __ave_ethtool_set_wol(ndev, wol);
  362. if (!ret)
  363. device_set_wakeup_enable(&ndev->dev, !!wol->wolopts);
  364. return ret;
  365. }
  366. static void ave_ethtool_get_pauseparam(struct net_device *ndev,
  367. struct ethtool_pauseparam *pause)
  368. {
  369. struct ave_private *priv = netdev_priv(ndev);
  370. pause->autoneg = priv->pause_auto;
  371. pause->rx_pause = priv->pause_rx;
  372. pause->tx_pause = priv->pause_tx;
  373. }
  374. static int ave_ethtool_set_pauseparam(struct net_device *ndev,
  375. struct ethtool_pauseparam *pause)
  376. {
  377. struct ave_private *priv = netdev_priv(ndev);
  378. struct phy_device *phydev = ndev->phydev;
  379. if (!phydev)
  380. return -EINVAL;
  381. priv->pause_auto = pause->autoneg;
  382. priv->pause_rx = pause->rx_pause;
  383. priv->pause_tx = pause->tx_pause;
  384. phy_set_asym_pause(phydev, pause->rx_pause, pause->tx_pause);
  385. return 0;
  386. }
  387. static const struct ethtool_ops ave_ethtool_ops = {
  388. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  389. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  390. .get_drvinfo = ave_ethtool_get_drvinfo,
  391. .nway_reset = phy_ethtool_nway_reset,
  392. .get_link = ethtool_op_get_link,
  393. .get_msglevel = ave_ethtool_get_msglevel,
  394. .set_msglevel = ave_ethtool_set_msglevel,
  395. .get_wol = ave_ethtool_get_wol,
  396. .set_wol = ave_ethtool_set_wol,
  397. .get_pauseparam = ave_ethtool_get_pauseparam,
  398. .set_pauseparam = ave_ethtool_set_pauseparam,
  399. };
  400. static int ave_mdiobus_read(struct mii_bus *bus, int phyid, int regnum)
  401. {
  402. struct net_device *ndev = bus->priv;
  403. struct ave_private *priv;
  404. u32 mdioctl, mdiosr;
  405. int ret;
  406. priv = netdev_priv(ndev);
  407. /* write address */
  408. writel((phyid << 8) | regnum, priv->base + AVE_MDIOAR);
  409. /* read request */
  410. mdioctl = readl(priv->base + AVE_MDIOCTR);
  411. writel((mdioctl | AVE_MDIOCTR_RREQ) & ~AVE_MDIOCTR_WREQ,
  412. priv->base + AVE_MDIOCTR);
  413. ret = readl_poll_timeout(priv->base + AVE_MDIOSR, mdiosr,
  414. !(mdiosr & AVE_MDIOSR_STS), 20, 2000);
  415. if (ret) {
  416. netdev_err(ndev, "failed to read (phy:%d reg:%x)\n",
  417. phyid, regnum);
  418. return ret;
  419. }
  420. return readl(priv->base + AVE_MDIORDR) & GENMASK(15, 0);
  421. }
  422. static int ave_mdiobus_write(struct mii_bus *bus, int phyid, int regnum,
  423. u16 val)
  424. {
  425. struct net_device *ndev = bus->priv;
  426. struct ave_private *priv;
  427. u32 mdioctl, mdiosr;
  428. int ret;
  429. priv = netdev_priv(ndev);
  430. /* write address */
  431. writel((phyid << 8) | regnum, priv->base + AVE_MDIOAR);
  432. /* write data */
  433. writel(val, priv->base + AVE_MDIOWDR);
  434. /* write request */
  435. mdioctl = readl(priv->base + AVE_MDIOCTR);
  436. writel((mdioctl | AVE_MDIOCTR_WREQ) & ~AVE_MDIOCTR_RREQ,
  437. priv->base + AVE_MDIOCTR);
  438. ret = readl_poll_timeout(priv->base + AVE_MDIOSR, mdiosr,
  439. !(mdiosr & AVE_MDIOSR_STS), 20, 2000);
  440. if (ret)
  441. netdev_err(ndev, "failed to write (phy:%d reg:%x)\n",
  442. phyid, regnum);
  443. return ret;
  444. }
  445. static int ave_dma_map(struct net_device *ndev, struct ave_desc *desc,
  446. void *ptr, size_t len, enum dma_data_direction dir,
  447. dma_addr_t *paddr)
  448. {
  449. dma_addr_t map_addr;
  450. map_addr = dma_map_single(ndev->dev.parent, ptr, len, dir);
  451. if (unlikely(dma_mapping_error(ndev->dev.parent, map_addr)))
  452. return -ENOMEM;
  453. desc->skbs_dma = map_addr;
  454. desc->skbs_dmalen = len;
  455. *paddr = map_addr;
  456. return 0;
  457. }
  458. static void ave_dma_unmap(struct net_device *ndev, struct ave_desc *desc,
  459. enum dma_data_direction dir)
  460. {
  461. if (!desc->skbs_dma)
  462. return;
  463. dma_unmap_single(ndev->dev.parent,
  464. desc->skbs_dma, desc->skbs_dmalen, dir);
  465. desc->skbs_dma = 0;
  466. }
  467. /* Prepare Rx descriptor and memory */
  468. static int ave_rxdesc_prepare(struct net_device *ndev, int entry)
  469. {
  470. struct ave_private *priv = netdev_priv(ndev);
  471. struct sk_buff *skb;
  472. dma_addr_t paddr;
  473. int ret;
  474. skb = priv->rx.desc[entry].skbs;
  475. if (!skb) {
  476. skb = netdev_alloc_skb(ndev, AVE_MAX_ETHFRAME);
  477. if (!skb) {
  478. netdev_err(ndev, "can't allocate skb for Rx\n");
  479. return -ENOMEM;
  480. }
  481. skb->data += AVE_FRAME_HEADROOM;
  482. skb->tail += AVE_FRAME_HEADROOM;
  483. }
  484. /* set disable to cmdsts */
  485. ave_desc_write_cmdsts(ndev, AVE_DESCID_RX, entry,
  486. AVE_STS_INTR | AVE_STS_OWN);
  487. /* map Rx buffer
  488. * Rx buffer set to the Rx descriptor has two restrictions:
  489. * - Rx buffer address is 4 byte aligned.
  490. * - Rx buffer begins with 2 byte headroom, and data will be put from
  491. * (buffer + 2).
  492. * To satisfy this, specify the address to put back the buffer
  493. * pointer advanced by AVE_FRAME_HEADROOM, and expand the map size
  494. * by AVE_FRAME_HEADROOM.
  495. */
  496. ret = ave_dma_map(ndev, &priv->rx.desc[entry],
  497. skb->data - AVE_FRAME_HEADROOM,
  498. AVE_MAX_ETHFRAME + AVE_FRAME_HEADROOM,
  499. DMA_FROM_DEVICE, &paddr);
  500. if (ret) {
  501. netdev_err(ndev, "can't map skb for Rx\n");
  502. dev_kfree_skb_any(skb);
  503. return ret;
  504. }
  505. priv->rx.desc[entry].skbs = skb;
  506. /* set buffer pointer */
  507. ave_desc_write_addr(ndev, AVE_DESCID_RX, entry, paddr);
  508. /* set enable to cmdsts */
  509. ave_desc_write_cmdsts(ndev, AVE_DESCID_RX, entry,
  510. AVE_STS_INTR | AVE_MAX_ETHFRAME);
  511. return ret;
  512. }
  513. /* Switch state of descriptor */
  514. static int ave_desc_switch(struct net_device *ndev, enum desc_state state)
  515. {
  516. struct ave_private *priv = netdev_priv(ndev);
  517. int ret = 0;
  518. u32 val;
  519. switch (state) {
  520. case AVE_DESC_START:
  521. writel(AVE_DESCC_TD | AVE_DESCC_RD0, priv->base + AVE_DESCC);
  522. break;
  523. case AVE_DESC_STOP:
  524. writel(0, priv->base + AVE_DESCC);
  525. if (readl_poll_timeout(priv->base + AVE_DESCC, val, !val,
  526. 150, 15000)) {
  527. netdev_err(ndev, "can't stop descriptor\n");
  528. ret = -EBUSY;
  529. }
  530. break;
  531. case AVE_DESC_RX_SUSPEND:
  532. val = readl(priv->base + AVE_DESCC);
  533. val |= AVE_DESCC_RDSTP;
  534. val &= ~AVE_DESCC_STATUS_MASK;
  535. writel(val, priv->base + AVE_DESCC);
  536. if (readl_poll_timeout(priv->base + AVE_DESCC, val,
  537. val & (AVE_DESCC_RDSTP << 16),
  538. 150, 150000)) {
  539. netdev_err(ndev, "can't suspend descriptor\n");
  540. ret = -EBUSY;
  541. }
  542. break;
  543. case AVE_DESC_RX_PERMIT:
  544. val = readl(priv->base + AVE_DESCC);
  545. val &= ~AVE_DESCC_RDSTP;
  546. val &= ~AVE_DESCC_STATUS_MASK;
  547. writel(val, priv->base + AVE_DESCC);
  548. break;
  549. default:
  550. ret = -EINVAL;
  551. break;
  552. }
  553. return ret;
  554. }
  555. static int ave_tx_complete(struct net_device *ndev)
  556. {
  557. struct ave_private *priv = netdev_priv(ndev);
  558. u32 proc_idx, done_idx, ndesc, cmdsts;
  559. unsigned int nr_freebuf = 0;
  560. unsigned int tx_packets = 0;
  561. unsigned int tx_bytes = 0;
  562. proc_idx = priv->tx.proc_idx;
  563. done_idx = priv->tx.done_idx;
  564. ndesc = priv->tx.ndesc;
  565. /* free pre-stored skb from done_idx to proc_idx */
  566. while (proc_idx != done_idx) {
  567. cmdsts = ave_desc_read_cmdsts(ndev, AVE_DESCID_TX, done_idx);
  568. /* do nothing if owner is HW (==1 for Tx) */
  569. if (cmdsts & AVE_STS_OWN)
  570. break;
  571. /* check Tx status and updates statistics */
  572. if (cmdsts & AVE_STS_OK) {
  573. tx_bytes += cmdsts & AVE_STS_PKTLEN_TX_MASK;
  574. /* success */
  575. if (cmdsts & AVE_STS_LAST)
  576. tx_packets++;
  577. } else {
  578. /* error */
  579. if (cmdsts & AVE_STS_LAST) {
  580. priv->stats_tx.errors++;
  581. if (cmdsts & (AVE_STS_OWC | AVE_STS_EC))
  582. priv->stats_tx.collisions++;
  583. }
  584. }
  585. /* release skb */
  586. if (priv->tx.desc[done_idx].skbs) {
  587. ave_dma_unmap(ndev, &priv->tx.desc[done_idx],
  588. DMA_TO_DEVICE);
  589. dev_consume_skb_any(priv->tx.desc[done_idx].skbs);
  590. priv->tx.desc[done_idx].skbs = NULL;
  591. nr_freebuf++;
  592. }
  593. done_idx = (done_idx + 1) % ndesc;
  594. }
  595. priv->tx.done_idx = done_idx;
  596. /* update stats */
  597. u64_stats_update_begin(&priv->stats_tx.syncp);
  598. priv->stats_tx.packets += tx_packets;
  599. priv->stats_tx.bytes += tx_bytes;
  600. u64_stats_update_end(&priv->stats_tx.syncp);
  601. /* wake queue for freeing buffer */
  602. if (unlikely(netif_queue_stopped(ndev)) && nr_freebuf)
  603. netif_wake_queue(ndev);
  604. return nr_freebuf;
  605. }
  606. static int ave_rx_receive(struct net_device *ndev, int num)
  607. {
  608. struct ave_private *priv = netdev_priv(ndev);
  609. unsigned int rx_packets = 0;
  610. unsigned int rx_bytes = 0;
  611. u32 proc_idx, done_idx;
  612. struct sk_buff *skb;
  613. unsigned int pktlen;
  614. int restpkt, npkts;
  615. u32 ndesc, cmdsts;
  616. proc_idx = priv->rx.proc_idx;
  617. done_idx = priv->rx.done_idx;
  618. ndesc = priv->rx.ndesc;
  619. restpkt = ((proc_idx + ndesc - 1) - done_idx) % ndesc;
  620. for (npkts = 0; npkts < num; npkts++) {
  621. /* we can't receive more packet, so fill desc quickly */
  622. if (--restpkt < 0)
  623. break;
  624. cmdsts = ave_desc_read_cmdsts(ndev, AVE_DESCID_RX, proc_idx);
  625. /* do nothing if owner is HW (==0 for Rx) */
  626. if (!(cmdsts & AVE_STS_OWN))
  627. break;
  628. if (!(cmdsts & AVE_STS_OK)) {
  629. priv->stats_rx.errors++;
  630. proc_idx = (proc_idx + 1) % ndesc;
  631. continue;
  632. }
  633. pktlen = cmdsts & AVE_STS_PKTLEN_RX_MASK;
  634. /* get skbuff for rx */
  635. skb = priv->rx.desc[proc_idx].skbs;
  636. priv->rx.desc[proc_idx].skbs = NULL;
  637. ave_dma_unmap(ndev, &priv->rx.desc[proc_idx], DMA_FROM_DEVICE);
  638. skb->dev = ndev;
  639. skb_put(skb, pktlen);
  640. skb->protocol = eth_type_trans(skb, ndev);
  641. if ((cmdsts & AVE_STS_CSSV) && (!(cmdsts & AVE_STS_CSER)))
  642. skb->ip_summed = CHECKSUM_UNNECESSARY;
  643. rx_packets++;
  644. rx_bytes += pktlen;
  645. netif_receive_skb(skb);
  646. proc_idx = (proc_idx + 1) % ndesc;
  647. }
  648. priv->rx.proc_idx = proc_idx;
  649. /* update stats */
  650. u64_stats_update_begin(&priv->stats_rx.syncp);
  651. priv->stats_rx.packets += rx_packets;
  652. priv->stats_rx.bytes += rx_bytes;
  653. u64_stats_update_end(&priv->stats_rx.syncp);
  654. /* refill the Rx buffers */
  655. while (proc_idx != done_idx) {
  656. if (ave_rxdesc_prepare(ndev, done_idx))
  657. break;
  658. done_idx = (done_idx + 1) % ndesc;
  659. }
  660. priv->rx.done_idx = done_idx;
  661. return npkts;
  662. }
  663. static int ave_napi_poll_rx(struct napi_struct *napi, int budget)
  664. {
  665. struct ave_private *priv;
  666. struct net_device *ndev;
  667. int num;
  668. priv = container_of(napi, struct ave_private, napi_rx);
  669. ndev = priv->ndev;
  670. num = ave_rx_receive(ndev, budget);
  671. if (num < budget) {
  672. napi_complete_done(napi, num);
  673. /* enable Rx interrupt when NAPI finishes */
  674. ave_irq_enable(ndev, AVE_GI_RXIINT);
  675. }
  676. return num;
  677. }
  678. static int ave_napi_poll_tx(struct napi_struct *napi, int budget)
  679. {
  680. struct ave_private *priv;
  681. struct net_device *ndev;
  682. int num;
  683. priv = container_of(napi, struct ave_private, napi_tx);
  684. ndev = priv->ndev;
  685. num = ave_tx_complete(ndev);
  686. napi_complete(napi);
  687. /* enable Tx interrupt when NAPI finishes */
  688. ave_irq_enable(ndev, AVE_GI_TX);
  689. return num;
  690. }
  691. static void ave_global_reset(struct net_device *ndev)
  692. {
  693. struct ave_private *priv = netdev_priv(ndev);
  694. u32 val;
  695. /* set config register */
  696. val = AVE_CFGR_FLE | AVE_CFGR_IPFCEN | AVE_CFGR_CHE;
  697. if (!phy_interface_mode_is_rgmii(priv->phy_mode))
  698. val |= AVE_CFGR_MII;
  699. writel(val, priv->base + AVE_CFGR);
  700. /* reset RMII register */
  701. val = readl(priv->base + AVE_RSTCTRL);
  702. val &= ~AVE_RSTCTRL_RMIIRST;
  703. writel(val, priv->base + AVE_RSTCTRL);
  704. /* assert reset */
  705. writel(AVE_GRR_GRST | AVE_GRR_PHYRST, priv->base + AVE_GRR);
  706. msleep(20);
  707. /* 1st, negate PHY reset only */
  708. writel(AVE_GRR_GRST, priv->base + AVE_GRR);
  709. msleep(40);
  710. /* negate reset */
  711. writel(0, priv->base + AVE_GRR);
  712. msleep(40);
  713. /* negate RMII register */
  714. val = readl(priv->base + AVE_RSTCTRL);
  715. val |= AVE_RSTCTRL_RMIIRST;
  716. writel(val, priv->base + AVE_RSTCTRL);
  717. ave_irq_disable_all(ndev);
  718. }
  719. static void ave_rxfifo_reset(struct net_device *ndev)
  720. {
  721. struct ave_private *priv = netdev_priv(ndev);
  722. u32 rxcr_org;
  723. /* save and disable MAC receive op */
  724. rxcr_org = readl(priv->base + AVE_RXCR);
  725. writel(rxcr_org & (~AVE_RXCR_RXEN), priv->base + AVE_RXCR);
  726. /* suspend Rx descriptor */
  727. ave_desc_switch(ndev, AVE_DESC_RX_SUSPEND);
  728. /* receive all packets before descriptor starts */
  729. ave_rx_receive(ndev, priv->rx.ndesc);
  730. /* assert reset */
  731. writel(AVE_GRR_RXFFR, priv->base + AVE_GRR);
  732. udelay(50);
  733. /* negate reset */
  734. writel(0, priv->base + AVE_GRR);
  735. udelay(20);
  736. /* negate interrupt status */
  737. writel(AVE_GI_RXOVF, priv->base + AVE_GISR);
  738. /* permit descriptor */
  739. ave_desc_switch(ndev, AVE_DESC_RX_PERMIT);
  740. /* restore MAC reccieve op */
  741. writel(rxcr_org, priv->base + AVE_RXCR);
  742. }
  743. static irqreturn_t ave_irq_handler(int irq, void *netdev)
  744. {
  745. struct net_device *ndev = (struct net_device *)netdev;
  746. struct ave_private *priv = netdev_priv(ndev);
  747. u32 gimr_val, gisr_val;
  748. gimr_val = ave_irq_disable_all(ndev);
  749. /* get interrupt status */
  750. gisr_val = readl(priv->base + AVE_GISR);
  751. /* PHY */
  752. if (gisr_val & AVE_GI_PHY)
  753. writel(AVE_GI_PHY, priv->base + AVE_GISR);
  754. /* check exceeding packet */
  755. if (gisr_val & AVE_GI_RXERR) {
  756. writel(AVE_GI_RXERR, priv->base + AVE_GISR);
  757. netdev_err(ndev, "receive a packet exceeding frame buffer\n");
  758. }
  759. gisr_val &= gimr_val;
  760. if (!gisr_val)
  761. goto exit_isr;
  762. /* RxFIFO overflow */
  763. if (gisr_val & AVE_GI_RXOVF) {
  764. priv->stats_rx.fifo_errors++;
  765. ave_rxfifo_reset(ndev);
  766. goto exit_isr;
  767. }
  768. /* Rx drop */
  769. if (gisr_val & AVE_GI_RXDROP) {
  770. priv->stats_rx.dropped++;
  771. writel(AVE_GI_RXDROP, priv->base + AVE_GISR);
  772. }
  773. /* Rx interval */
  774. if (gisr_val & AVE_GI_RXIINT) {
  775. napi_schedule(&priv->napi_rx);
  776. /* still force to disable Rx interrupt until NAPI finishes */
  777. gimr_val &= ~AVE_GI_RXIINT;
  778. }
  779. /* Tx completed */
  780. if (gisr_val & AVE_GI_TX) {
  781. napi_schedule(&priv->napi_tx);
  782. /* still force to disable Tx interrupt until NAPI finishes */
  783. gimr_val &= ~AVE_GI_TX;
  784. }
  785. exit_isr:
  786. ave_irq_restore(ndev, gimr_val);
  787. return IRQ_HANDLED;
  788. }
  789. static int ave_pfsel_start(struct net_device *ndev, unsigned int entry)
  790. {
  791. struct ave_private *priv = netdev_priv(ndev);
  792. u32 val;
  793. if (WARN_ON(entry > AVE_PF_SIZE))
  794. return -EINVAL;
  795. val = readl(priv->base + AVE_PFEN);
  796. writel(val | BIT(entry), priv->base + AVE_PFEN);
  797. return 0;
  798. }
  799. static int ave_pfsel_stop(struct net_device *ndev, unsigned int entry)
  800. {
  801. struct ave_private *priv = netdev_priv(ndev);
  802. u32 val;
  803. if (WARN_ON(entry > AVE_PF_SIZE))
  804. return -EINVAL;
  805. val = readl(priv->base + AVE_PFEN);
  806. writel(val & ~BIT(entry), priv->base + AVE_PFEN);
  807. return 0;
  808. }
  809. static int ave_pfsel_set_macaddr(struct net_device *ndev,
  810. unsigned int entry,
  811. const unsigned char *mac_addr,
  812. unsigned int set_size)
  813. {
  814. struct ave_private *priv = netdev_priv(ndev);
  815. if (WARN_ON(entry > AVE_PF_SIZE))
  816. return -EINVAL;
  817. if (WARN_ON(set_size > 6))
  818. return -EINVAL;
  819. ave_pfsel_stop(ndev, entry);
  820. /* set MAC address for the filter */
  821. ave_hw_write_macaddr(ndev, mac_addr,
  822. AVE_PKTF(entry), AVE_PKTF(entry) + 4);
  823. /* set byte mask */
  824. writel(GENMASK(31, set_size) & AVE_PFMBYTE_MASK0,
  825. priv->base + AVE_PFMBYTE(entry));
  826. writel(AVE_PFMBYTE_MASK1, priv->base + AVE_PFMBYTE(entry) + 4);
  827. /* set bit mask filter */
  828. writel(AVE_PFMBIT_MASK, priv->base + AVE_PFMBIT(entry));
  829. /* set selector to ring 0 */
  830. writel(0, priv->base + AVE_PFSEL(entry));
  831. /* restart filter */
  832. ave_pfsel_start(ndev, entry);
  833. return 0;
  834. }
  835. static void ave_pfsel_set_promisc(struct net_device *ndev,
  836. unsigned int entry, u32 rxring)
  837. {
  838. struct ave_private *priv = netdev_priv(ndev);
  839. if (WARN_ON(entry > AVE_PF_SIZE))
  840. return;
  841. ave_pfsel_stop(ndev, entry);
  842. /* set byte mask */
  843. writel(AVE_PFMBYTE_MASK0, priv->base + AVE_PFMBYTE(entry));
  844. writel(AVE_PFMBYTE_MASK1, priv->base + AVE_PFMBYTE(entry) + 4);
  845. /* set bit mask filter */
  846. writel(AVE_PFMBIT_MASK, priv->base + AVE_PFMBIT(entry));
  847. /* set selector to rxring */
  848. writel(rxring, priv->base + AVE_PFSEL(entry));
  849. ave_pfsel_start(ndev, entry);
  850. }
  851. static void ave_pfsel_init(struct net_device *ndev)
  852. {
  853. unsigned char bcast_mac[ETH_ALEN];
  854. int i;
  855. eth_broadcast_addr(bcast_mac);
  856. for (i = 0; i < AVE_PF_SIZE; i++)
  857. ave_pfsel_stop(ndev, i);
  858. /* promiscious entry, select ring 0 */
  859. ave_pfsel_set_promisc(ndev, AVE_PFNUM_FILTER, 0);
  860. /* unicast entry */
  861. ave_pfsel_set_macaddr(ndev, AVE_PFNUM_UNICAST, ndev->dev_addr, 6);
  862. /* broadcast entry */
  863. ave_pfsel_set_macaddr(ndev, AVE_PFNUM_BROADCAST, bcast_mac, 6);
  864. }
  865. static void ave_phy_adjust_link(struct net_device *ndev)
  866. {
  867. struct ave_private *priv = netdev_priv(ndev);
  868. struct phy_device *phydev = ndev->phydev;
  869. u32 val, txcr, rxcr, rxcr_org;
  870. u16 rmt_adv = 0, lcl_adv = 0;
  871. u8 cap;
  872. /* set RGMII speed */
  873. val = readl(priv->base + AVE_TXCR);
  874. val &= ~(AVE_TXCR_TXSPD_100 | AVE_TXCR_TXSPD_1G);
  875. if (phy_interface_is_rgmii(phydev) && phydev->speed == SPEED_1000)
  876. val |= AVE_TXCR_TXSPD_1G;
  877. else if (phydev->speed == SPEED_100)
  878. val |= AVE_TXCR_TXSPD_100;
  879. writel(val, priv->base + AVE_TXCR);
  880. /* set RMII speed (100M/10M only) */
  881. if (!phy_interface_is_rgmii(phydev)) {
  882. val = readl(priv->base + AVE_LINKSEL);
  883. if (phydev->speed == SPEED_10)
  884. val &= ~AVE_LINKSEL_100M;
  885. else
  886. val |= AVE_LINKSEL_100M;
  887. writel(val, priv->base + AVE_LINKSEL);
  888. }
  889. /* check current RXCR/TXCR */
  890. rxcr = readl(priv->base + AVE_RXCR);
  891. txcr = readl(priv->base + AVE_TXCR);
  892. rxcr_org = rxcr;
  893. if (phydev->duplex) {
  894. rxcr |= AVE_RXCR_FDUPEN;
  895. if (phydev->pause)
  896. rmt_adv |= LPA_PAUSE_CAP;
  897. if (phydev->asym_pause)
  898. rmt_adv |= LPA_PAUSE_ASYM;
  899. lcl_adv = linkmode_adv_to_lcl_adv_t(phydev->advertising);
  900. cap = mii_resolve_flowctrl_fdx(lcl_adv, rmt_adv);
  901. if (cap & FLOW_CTRL_TX)
  902. txcr |= AVE_TXCR_FLOCTR;
  903. else
  904. txcr &= ~AVE_TXCR_FLOCTR;
  905. if (cap & FLOW_CTRL_RX)
  906. rxcr |= AVE_RXCR_FLOCTR;
  907. else
  908. rxcr &= ~AVE_RXCR_FLOCTR;
  909. } else {
  910. rxcr &= ~AVE_RXCR_FDUPEN;
  911. rxcr &= ~AVE_RXCR_FLOCTR;
  912. txcr &= ~AVE_TXCR_FLOCTR;
  913. }
  914. if (rxcr_org != rxcr) {
  915. /* disable Rx mac */
  916. writel(rxcr & ~AVE_RXCR_RXEN, priv->base + AVE_RXCR);
  917. /* change and enable TX/Rx mac */
  918. writel(txcr, priv->base + AVE_TXCR);
  919. writel(rxcr, priv->base + AVE_RXCR);
  920. }
  921. phy_print_status(phydev);
  922. }
  923. static void ave_macaddr_init(struct net_device *ndev)
  924. {
  925. ave_hw_write_macaddr(ndev, ndev->dev_addr, AVE_RXMAC1R, AVE_RXMAC2R);
  926. /* pfsel unicast entry */
  927. ave_pfsel_set_macaddr(ndev, AVE_PFNUM_UNICAST, ndev->dev_addr, 6);
  928. }
  929. static int ave_init(struct net_device *ndev)
  930. {
  931. struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
  932. struct ave_private *priv = netdev_priv(ndev);
  933. struct device *dev = ndev->dev.parent;
  934. struct device_node *np = dev->of_node;
  935. struct device_node *mdio_np;
  936. struct phy_device *phydev;
  937. int nc, nr, ret;
  938. /* enable clk because of hw access until ndo_open */
  939. for (nc = 0; nc < priv->nclks; nc++) {
  940. ret = clk_prepare_enable(priv->clk[nc]);
  941. if (ret) {
  942. dev_err(dev, "can't enable clock\n");
  943. goto out_clk_disable;
  944. }
  945. }
  946. for (nr = 0; nr < priv->nrsts; nr++) {
  947. ret = reset_control_deassert(priv->rst[nr]);
  948. if (ret) {
  949. dev_err(dev, "can't deassert reset\n");
  950. goto out_reset_assert;
  951. }
  952. }
  953. ret = regmap_update_bits(priv->regmap, SG_ETPINMODE,
  954. priv->pinmode_mask, priv->pinmode_val);
  955. if (ret)
  956. goto out_reset_assert;
  957. ave_global_reset(ndev);
  958. mdio_np = of_get_child_by_name(np, "mdio");
  959. if (!mdio_np) {
  960. dev_err(dev, "mdio node not found\n");
  961. ret = -EINVAL;
  962. goto out_reset_assert;
  963. }
  964. ret = of_mdiobus_register(priv->mdio, mdio_np);
  965. of_node_put(mdio_np);
  966. if (ret) {
  967. dev_err(dev, "failed to register mdiobus\n");
  968. goto out_reset_assert;
  969. }
  970. phydev = of_phy_get_and_connect(ndev, np, ave_phy_adjust_link);
  971. if (!phydev) {
  972. dev_err(dev, "could not attach to PHY\n");
  973. ret = -ENODEV;
  974. goto out_mdio_unregister;
  975. }
  976. priv->phydev = phydev;
  977. ave_ethtool_get_wol(ndev, &wol);
  978. device_set_wakeup_capable(&ndev->dev, !!wol.supported);
  979. /* set wol initial state disabled */
  980. wol.wolopts = 0;
  981. __ave_ethtool_set_wol(ndev, &wol);
  982. if (!phy_interface_is_rgmii(phydev))
  983. phy_set_max_speed(phydev, SPEED_100);
  984. phy_support_asym_pause(phydev);
  985. phy_attached_info(phydev);
  986. return 0;
  987. out_mdio_unregister:
  988. mdiobus_unregister(priv->mdio);
  989. out_reset_assert:
  990. while (--nr >= 0)
  991. reset_control_assert(priv->rst[nr]);
  992. out_clk_disable:
  993. while (--nc >= 0)
  994. clk_disable_unprepare(priv->clk[nc]);
  995. return ret;
  996. }
  997. static void ave_uninit(struct net_device *ndev)
  998. {
  999. struct ave_private *priv = netdev_priv(ndev);
  1000. int i;
  1001. phy_disconnect(priv->phydev);
  1002. mdiobus_unregister(priv->mdio);
  1003. /* disable clk because of hw access after ndo_stop */
  1004. for (i = 0; i < priv->nrsts; i++)
  1005. reset_control_assert(priv->rst[i]);
  1006. for (i = 0; i < priv->nclks; i++)
  1007. clk_disable_unprepare(priv->clk[i]);
  1008. }
  1009. static int ave_open(struct net_device *ndev)
  1010. {
  1011. struct ave_private *priv = netdev_priv(ndev);
  1012. int entry;
  1013. int ret;
  1014. u32 val;
  1015. ret = request_irq(priv->irq, ave_irq_handler, IRQF_SHARED, ndev->name,
  1016. ndev);
  1017. if (ret)
  1018. return ret;
  1019. priv->tx.desc = kcalloc(priv->tx.ndesc, sizeof(*priv->tx.desc),
  1020. GFP_KERNEL);
  1021. if (!priv->tx.desc) {
  1022. ret = -ENOMEM;
  1023. goto out_free_irq;
  1024. }
  1025. priv->rx.desc = kcalloc(priv->rx.ndesc, sizeof(*priv->rx.desc),
  1026. GFP_KERNEL);
  1027. if (!priv->rx.desc) {
  1028. kfree(priv->tx.desc);
  1029. ret = -ENOMEM;
  1030. goto out_free_irq;
  1031. }
  1032. /* initialize Tx work and descriptor */
  1033. priv->tx.proc_idx = 0;
  1034. priv->tx.done_idx = 0;
  1035. for (entry = 0; entry < priv->tx.ndesc; entry++) {
  1036. ave_desc_write_cmdsts(ndev, AVE_DESCID_TX, entry, 0);
  1037. ave_desc_write_addr(ndev, AVE_DESCID_TX, entry, 0);
  1038. }
  1039. writel(AVE_TXDC_ADDR_START |
  1040. (((priv->tx.ndesc * priv->desc_size) << 16) & AVE_TXDC_SIZE),
  1041. priv->base + AVE_TXDC);
  1042. /* initialize Rx work and descriptor */
  1043. priv->rx.proc_idx = 0;
  1044. priv->rx.done_idx = 0;
  1045. for (entry = 0; entry < priv->rx.ndesc; entry++) {
  1046. if (ave_rxdesc_prepare(ndev, entry))
  1047. break;
  1048. }
  1049. writel(AVE_RXDC0_ADDR_START |
  1050. (((priv->rx.ndesc * priv->desc_size) << 16) & AVE_RXDC0_SIZE),
  1051. priv->base + AVE_RXDC0);
  1052. ave_desc_switch(ndev, AVE_DESC_START);
  1053. ave_pfsel_init(ndev);
  1054. ave_macaddr_init(ndev);
  1055. /* set Rx configuration */
  1056. /* full duplex, enable pause drop, enalbe flow control */
  1057. val = AVE_RXCR_RXEN | AVE_RXCR_FDUPEN | AVE_RXCR_DRPEN |
  1058. AVE_RXCR_FLOCTR | (AVE_MAX_ETHFRAME & AVE_RXCR_MPSIZ_MASK);
  1059. writel(val, priv->base + AVE_RXCR);
  1060. /* set Tx configuration */
  1061. /* enable flow control, disable loopback */
  1062. writel(AVE_TXCR_FLOCTR, priv->base + AVE_TXCR);
  1063. /* enable timer, clear EN,INTM, and mask interval unit(BSCK) */
  1064. val = readl(priv->base + AVE_IIRQC) & AVE_IIRQC_BSCK;
  1065. val |= AVE_IIRQC_EN0 | (AVE_INTM_COUNT << 16);
  1066. writel(val, priv->base + AVE_IIRQC);
  1067. val = AVE_GI_RXIINT | AVE_GI_RXOVF | AVE_GI_TX | AVE_GI_RXDROP;
  1068. ave_irq_restore(ndev, val);
  1069. napi_enable(&priv->napi_rx);
  1070. napi_enable(&priv->napi_tx);
  1071. phy_start(ndev->phydev);
  1072. phy_start_aneg(ndev->phydev);
  1073. netif_start_queue(ndev);
  1074. return 0;
  1075. out_free_irq:
  1076. disable_irq(priv->irq);
  1077. free_irq(priv->irq, ndev);
  1078. return ret;
  1079. }
  1080. static int ave_stop(struct net_device *ndev)
  1081. {
  1082. struct ave_private *priv = netdev_priv(ndev);
  1083. int entry;
  1084. ave_irq_disable_all(ndev);
  1085. disable_irq(priv->irq);
  1086. free_irq(priv->irq, ndev);
  1087. netif_tx_disable(ndev);
  1088. phy_stop(ndev->phydev);
  1089. napi_disable(&priv->napi_tx);
  1090. napi_disable(&priv->napi_rx);
  1091. ave_desc_switch(ndev, AVE_DESC_STOP);
  1092. /* free Tx buffer */
  1093. for (entry = 0; entry < priv->tx.ndesc; entry++) {
  1094. if (!priv->tx.desc[entry].skbs)
  1095. continue;
  1096. ave_dma_unmap(ndev, &priv->tx.desc[entry], DMA_TO_DEVICE);
  1097. dev_kfree_skb_any(priv->tx.desc[entry].skbs);
  1098. priv->tx.desc[entry].skbs = NULL;
  1099. }
  1100. priv->tx.proc_idx = 0;
  1101. priv->tx.done_idx = 0;
  1102. /* free Rx buffer */
  1103. for (entry = 0; entry < priv->rx.ndesc; entry++) {
  1104. if (!priv->rx.desc[entry].skbs)
  1105. continue;
  1106. ave_dma_unmap(ndev, &priv->rx.desc[entry], DMA_FROM_DEVICE);
  1107. dev_kfree_skb_any(priv->rx.desc[entry].skbs);
  1108. priv->rx.desc[entry].skbs = NULL;
  1109. }
  1110. priv->rx.proc_idx = 0;
  1111. priv->rx.done_idx = 0;
  1112. kfree(priv->tx.desc);
  1113. kfree(priv->rx.desc);
  1114. return 0;
  1115. }
  1116. static int ave_start_xmit(struct sk_buff *skb, struct net_device *ndev)
  1117. {
  1118. struct ave_private *priv = netdev_priv(ndev);
  1119. u32 proc_idx, done_idx, ndesc, cmdsts;
  1120. int ret, freepkt;
  1121. dma_addr_t paddr;
  1122. proc_idx = priv->tx.proc_idx;
  1123. done_idx = priv->tx.done_idx;
  1124. ndesc = priv->tx.ndesc;
  1125. freepkt = ((done_idx + ndesc - 1) - proc_idx) % ndesc;
  1126. /* stop queue when not enough entry */
  1127. if (unlikely(freepkt < 1)) {
  1128. netif_stop_queue(ndev);
  1129. return NETDEV_TX_BUSY;
  1130. }
  1131. /* add padding for short packet */
  1132. if (skb_put_padto(skb, ETH_ZLEN)) {
  1133. priv->stats_tx.dropped++;
  1134. return NETDEV_TX_OK;
  1135. }
  1136. /* map Tx buffer
  1137. * Tx buffer set to the Tx descriptor doesn't have any restriction.
  1138. */
  1139. ret = ave_dma_map(ndev, &priv->tx.desc[proc_idx],
  1140. skb->data, skb->len, DMA_TO_DEVICE, &paddr);
  1141. if (ret) {
  1142. dev_kfree_skb_any(skb);
  1143. priv->stats_tx.dropped++;
  1144. return NETDEV_TX_OK;
  1145. }
  1146. priv->tx.desc[proc_idx].skbs = skb;
  1147. ave_desc_write_addr(ndev, AVE_DESCID_TX, proc_idx, paddr);
  1148. cmdsts = AVE_STS_OWN | AVE_STS_1ST | AVE_STS_LAST |
  1149. (skb->len & AVE_STS_PKTLEN_TX_MASK);
  1150. /* set interrupt per AVE_FORCE_TXINTCNT or when queue is stopped */
  1151. if (!(proc_idx % AVE_FORCE_TXINTCNT) || netif_queue_stopped(ndev))
  1152. cmdsts |= AVE_STS_INTR;
  1153. /* disable checksum calculation when skb doesn't calurate checksum */
  1154. if (skb->ip_summed == CHECKSUM_NONE ||
  1155. skb->ip_summed == CHECKSUM_UNNECESSARY)
  1156. cmdsts |= AVE_STS_NOCSUM;
  1157. ave_desc_write_cmdsts(ndev, AVE_DESCID_TX, proc_idx, cmdsts);
  1158. priv->tx.proc_idx = (proc_idx + 1) % ndesc;
  1159. return NETDEV_TX_OK;
  1160. }
  1161. static int ave_ioctl(struct net_device *ndev, struct ifreq *ifr, int cmd)
  1162. {
  1163. return phy_mii_ioctl(ndev->phydev, ifr, cmd);
  1164. }
  1165. static const u8 v4multi_macadr[] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 };
  1166. static const u8 v6multi_macadr[] = { 0x33, 0x00, 0x00, 0x00, 0x00, 0x00 };
  1167. static void ave_set_rx_mode(struct net_device *ndev)
  1168. {
  1169. struct ave_private *priv = netdev_priv(ndev);
  1170. struct netdev_hw_addr *hw_adr;
  1171. int count, mc_cnt;
  1172. u32 val;
  1173. /* MAC addr filter enable for promiscious mode */
  1174. mc_cnt = netdev_mc_count(ndev);
  1175. val = readl(priv->base + AVE_RXCR);
  1176. if (ndev->flags & IFF_PROMISC || !mc_cnt)
  1177. val &= ~AVE_RXCR_AFEN;
  1178. else
  1179. val |= AVE_RXCR_AFEN;
  1180. writel(val, priv->base + AVE_RXCR);
  1181. /* set all multicast address */
  1182. if ((ndev->flags & IFF_ALLMULTI) || mc_cnt > AVE_PF_MULTICAST_SIZE) {
  1183. ave_pfsel_set_macaddr(ndev, AVE_PFNUM_MULTICAST,
  1184. v4multi_macadr, 1);
  1185. ave_pfsel_set_macaddr(ndev, AVE_PFNUM_MULTICAST + 1,
  1186. v6multi_macadr, 1);
  1187. } else {
  1188. /* stop all multicast filter */
  1189. for (count = 0; count < AVE_PF_MULTICAST_SIZE; count++)
  1190. ave_pfsel_stop(ndev, AVE_PFNUM_MULTICAST + count);
  1191. /* set multicast addresses */
  1192. count = 0;
  1193. netdev_for_each_mc_addr(hw_adr, ndev) {
  1194. if (count == mc_cnt)
  1195. break;
  1196. ave_pfsel_set_macaddr(ndev, AVE_PFNUM_MULTICAST + count,
  1197. hw_adr->addr, 6);
  1198. count++;
  1199. }
  1200. }
  1201. }
  1202. static void ave_get_stats64(struct net_device *ndev,
  1203. struct rtnl_link_stats64 *stats)
  1204. {
  1205. struct ave_private *priv = netdev_priv(ndev);
  1206. unsigned int start;
  1207. do {
  1208. start = u64_stats_fetch_begin_irq(&priv->stats_rx.syncp);
  1209. stats->rx_packets = priv->stats_rx.packets;
  1210. stats->rx_bytes = priv->stats_rx.bytes;
  1211. } while (u64_stats_fetch_retry_irq(&priv->stats_rx.syncp, start));
  1212. do {
  1213. start = u64_stats_fetch_begin_irq(&priv->stats_tx.syncp);
  1214. stats->tx_packets = priv->stats_tx.packets;
  1215. stats->tx_bytes = priv->stats_tx.bytes;
  1216. } while (u64_stats_fetch_retry_irq(&priv->stats_tx.syncp, start));
  1217. stats->rx_errors = priv->stats_rx.errors;
  1218. stats->tx_errors = priv->stats_tx.errors;
  1219. stats->rx_dropped = priv->stats_rx.dropped;
  1220. stats->tx_dropped = priv->stats_tx.dropped;
  1221. stats->rx_fifo_errors = priv->stats_rx.fifo_errors;
  1222. stats->collisions = priv->stats_tx.collisions;
  1223. }
  1224. static int ave_set_mac_address(struct net_device *ndev, void *p)
  1225. {
  1226. int ret = eth_mac_addr(ndev, p);
  1227. if (ret)
  1228. return ret;
  1229. ave_macaddr_init(ndev);
  1230. return 0;
  1231. }
  1232. static const struct net_device_ops ave_netdev_ops = {
  1233. .ndo_init = ave_init,
  1234. .ndo_uninit = ave_uninit,
  1235. .ndo_open = ave_open,
  1236. .ndo_stop = ave_stop,
  1237. .ndo_start_xmit = ave_start_xmit,
  1238. .ndo_do_ioctl = ave_ioctl,
  1239. .ndo_set_rx_mode = ave_set_rx_mode,
  1240. .ndo_get_stats64 = ave_get_stats64,
  1241. .ndo_set_mac_address = ave_set_mac_address,
  1242. };
  1243. static int ave_probe(struct platform_device *pdev)
  1244. {
  1245. const struct ave_soc_data *data;
  1246. struct device *dev = &pdev->dev;
  1247. char buf[ETHTOOL_FWVERS_LEN];
  1248. struct of_phandle_args args;
  1249. phy_interface_t phy_mode;
  1250. struct ave_private *priv;
  1251. struct net_device *ndev;
  1252. struct device_node *np;
  1253. const void *mac_addr;
  1254. void __iomem *base;
  1255. const char *name;
  1256. int i, irq, ret;
  1257. u64 dma_mask;
  1258. u32 ave_id;
  1259. data = of_device_get_match_data(dev);
  1260. if (WARN_ON(!data))
  1261. return -EINVAL;
  1262. np = dev->of_node;
  1263. phy_mode = of_get_phy_mode(np);
  1264. if ((int)phy_mode < 0) {
  1265. dev_err(dev, "phy-mode not found\n");
  1266. return -EINVAL;
  1267. }
  1268. irq = platform_get_irq(pdev, 0);
  1269. if (irq < 0)
  1270. return irq;
  1271. base = devm_platform_ioremap_resource(pdev, 0);
  1272. if (IS_ERR(base))
  1273. return PTR_ERR(base);
  1274. ndev = alloc_etherdev(sizeof(struct ave_private));
  1275. if (!ndev) {
  1276. dev_err(dev, "can't allocate ethernet device\n");
  1277. return -ENOMEM;
  1278. }
  1279. ndev->netdev_ops = &ave_netdev_ops;
  1280. ndev->ethtool_ops = &ave_ethtool_ops;
  1281. SET_NETDEV_DEV(ndev, dev);
  1282. ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_RXCSUM);
  1283. ndev->hw_features |= (NETIF_F_IP_CSUM | NETIF_F_RXCSUM);
  1284. ndev->max_mtu = AVE_MAX_ETHFRAME - (ETH_HLEN + ETH_FCS_LEN);
  1285. mac_addr = of_get_mac_address(np);
  1286. if (!IS_ERR(mac_addr))
  1287. ether_addr_copy(ndev->dev_addr, mac_addr);
  1288. /* if the mac address is invalid, use random mac address */
  1289. if (!is_valid_ether_addr(ndev->dev_addr)) {
  1290. eth_hw_addr_random(ndev);
  1291. dev_warn(dev, "Using random MAC address: %pM\n",
  1292. ndev->dev_addr);
  1293. }
  1294. priv = netdev_priv(ndev);
  1295. priv->base = base;
  1296. priv->irq = irq;
  1297. priv->ndev = ndev;
  1298. priv->msg_enable = netif_msg_init(-1, AVE_DEFAULT_MSG_ENABLE);
  1299. priv->phy_mode = phy_mode;
  1300. priv->data = data;
  1301. if (IS_DESC_64BIT(priv)) {
  1302. priv->desc_size = AVE_DESC_SIZE_64;
  1303. priv->tx.daddr = AVE_TXDM_64;
  1304. priv->rx.daddr = AVE_RXDM_64;
  1305. dma_mask = DMA_BIT_MASK(64);
  1306. } else {
  1307. priv->desc_size = AVE_DESC_SIZE_32;
  1308. priv->tx.daddr = AVE_TXDM_32;
  1309. priv->rx.daddr = AVE_RXDM_32;
  1310. dma_mask = DMA_BIT_MASK(32);
  1311. }
  1312. ret = dma_set_mask(dev, dma_mask);
  1313. if (ret)
  1314. goto out_free_netdev;
  1315. priv->tx.ndesc = AVE_NR_TXDESC;
  1316. priv->rx.ndesc = AVE_NR_RXDESC;
  1317. u64_stats_init(&priv->stats_tx.syncp);
  1318. u64_stats_init(&priv->stats_rx.syncp);
  1319. for (i = 0; i < AVE_MAX_CLKS; i++) {
  1320. name = priv->data->clock_names[i];
  1321. if (!name)
  1322. break;
  1323. priv->clk[i] = devm_clk_get(dev, name);
  1324. if (IS_ERR(priv->clk[i])) {
  1325. ret = PTR_ERR(priv->clk[i]);
  1326. goto out_free_netdev;
  1327. }
  1328. priv->nclks++;
  1329. }
  1330. for (i = 0; i < AVE_MAX_RSTS; i++) {
  1331. name = priv->data->reset_names[i];
  1332. if (!name)
  1333. break;
  1334. priv->rst[i] = devm_reset_control_get_shared(dev, name);
  1335. if (IS_ERR(priv->rst[i])) {
  1336. ret = PTR_ERR(priv->rst[i]);
  1337. goto out_free_netdev;
  1338. }
  1339. priv->nrsts++;
  1340. }
  1341. ret = of_parse_phandle_with_fixed_args(np,
  1342. "socionext,syscon-phy-mode",
  1343. 1, 0, &args);
  1344. if (ret) {
  1345. dev_err(dev, "can't get syscon-phy-mode property\n");
  1346. goto out_free_netdev;
  1347. }
  1348. priv->regmap = syscon_node_to_regmap(args.np);
  1349. of_node_put(args.np);
  1350. if (IS_ERR(priv->regmap)) {
  1351. dev_err(dev, "can't map syscon-phy-mode\n");
  1352. ret = PTR_ERR(priv->regmap);
  1353. goto out_free_netdev;
  1354. }
  1355. ret = priv->data->get_pinmode(priv, phy_mode, args.args[0]);
  1356. if (ret) {
  1357. dev_err(dev, "invalid phy-mode setting\n");
  1358. goto out_free_netdev;
  1359. }
  1360. priv->mdio = devm_mdiobus_alloc(dev);
  1361. if (!priv->mdio) {
  1362. ret = -ENOMEM;
  1363. goto out_free_netdev;
  1364. }
  1365. priv->mdio->priv = ndev;
  1366. priv->mdio->parent = dev;
  1367. priv->mdio->read = ave_mdiobus_read;
  1368. priv->mdio->write = ave_mdiobus_write;
  1369. priv->mdio->name = "uniphier-mdio";
  1370. snprintf(priv->mdio->id, MII_BUS_ID_SIZE, "%s-%x",
  1371. pdev->name, pdev->id);
  1372. /* Register as a NAPI supported driver */
  1373. netif_napi_add(ndev, &priv->napi_rx, ave_napi_poll_rx,
  1374. NAPI_POLL_WEIGHT);
  1375. netif_tx_napi_add(ndev, &priv->napi_tx, ave_napi_poll_tx,
  1376. NAPI_POLL_WEIGHT);
  1377. platform_set_drvdata(pdev, ndev);
  1378. ret = register_netdev(ndev);
  1379. if (ret) {
  1380. dev_err(dev, "failed to register netdevice\n");
  1381. goto out_del_napi;
  1382. }
  1383. /* get ID and version */
  1384. ave_id = readl(priv->base + AVE_IDR);
  1385. ave_hw_read_version(ndev, buf, sizeof(buf));
  1386. dev_info(dev, "Socionext %c%c%c%c Ethernet IP %s (irq=%d, phy=%s)\n",
  1387. (ave_id >> 24) & 0xff, (ave_id >> 16) & 0xff,
  1388. (ave_id >> 8) & 0xff, (ave_id >> 0) & 0xff,
  1389. buf, priv->irq, phy_modes(phy_mode));
  1390. return 0;
  1391. out_del_napi:
  1392. netif_napi_del(&priv->napi_rx);
  1393. netif_napi_del(&priv->napi_tx);
  1394. out_free_netdev:
  1395. free_netdev(ndev);
  1396. return ret;
  1397. }
  1398. static int ave_remove(struct platform_device *pdev)
  1399. {
  1400. struct net_device *ndev = platform_get_drvdata(pdev);
  1401. struct ave_private *priv = netdev_priv(ndev);
  1402. unregister_netdev(ndev);
  1403. netif_napi_del(&priv->napi_rx);
  1404. netif_napi_del(&priv->napi_tx);
  1405. free_netdev(ndev);
  1406. return 0;
  1407. }
  1408. #ifdef CONFIG_PM_SLEEP
  1409. static int ave_suspend(struct device *dev)
  1410. {
  1411. struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
  1412. struct net_device *ndev = dev_get_drvdata(dev);
  1413. struct ave_private *priv = netdev_priv(ndev);
  1414. int ret = 0;
  1415. if (netif_running(ndev)) {
  1416. ret = ave_stop(ndev);
  1417. netif_device_detach(ndev);
  1418. }
  1419. ave_ethtool_get_wol(ndev, &wol);
  1420. priv->wolopts = wol.wolopts;
  1421. return ret;
  1422. }
  1423. static int ave_resume(struct device *dev)
  1424. {
  1425. struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
  1426. struct net_device *ndev = dev_get_drvdata(dev);
  1427. struct ave_private *priv = netdev_priv(ndev);
  1428. int ret = 0;
  1429. ave_global_reset(ndev);
  1430. ave_ethtool_get_wol(ndev, &wol);
  1431. wol.wolopts = priv->wolopts;
  1432. __ave_ethtool_set_wol(ndev, &wol);
  1433. if (ndev->phydev) {
  1434. ret = phy_resume(ndev->phydev);
  1435. if (ret)
  1436. return ret;
  1437. }
  1438. if (netif_running(ndev)) {
  1439. ret = ave_open(ndev);
  1440. netif_device_attach(ndev);
  1441. }
  1442. return ret;
  1443. }
  1444. static SIMPLE_DEV_PM_OPS(ave_pm_ops, ave_suspend, ave_resume);
  1445. #define AVE_PM_OPS (&ave_pm_ops)
  1446. #else
  1447. #define AVE_PM_OPS NULL
  1448. #endif
  1449. static int ave_pro4_get_pinmode(struct ave_private *priv,
  1450. phy_interface_t phy_mode, u32 arg)
  1451. {
  1452. if (arg > 0)
  1453. return -EINVAL;
  1454. priv->pinmode_mask = SG_ETPINMODE_RMII(0);
  1455. switch (phy_mode) {
  1456. case PHY_INTERFACE_MODE_RMII:
  1457. priv->pinmode_val = SG_ETPINMODE_RMII(0);
  1458. break;
  1459. case PHY_INTERFACE_MODE_MII:
  1460. case PHY_INTERFACE_MODE_RGMII:
  1461. priv->pinmode_val = 0;
  1462. break;
  1463. default:
  1464. return -EINVAL;
  1465. }
  1466. return 0;
  1467. }
  1468. static int ave_ld11_get_pinmode(struct ave_private *priv,
  1469. phy_interface_t phy_mode, u32 arg)
  1470. {
  1471. if (arg > 0)
  1472. return -EINVAL;
  1473. priv->pinmode_mask = SG_ETPINMODE_EXTPHY | SG_ETPINMODE_RMII(0);
  1474. switch (phy_mode) {
  1475. case PHY_INTERFACE_MODE_INTERNAL:
  1476. priv->pinmode_val = 0;
  1477. break;
  1478. case PHY_INTERFACE_MODE_RMII:
  1479. priv->pinmode_val = SG_ETPINMODE_EXTPHY | SG_ETPINMODE_RMII(0);
  1480. break;
  1481. default:
  1482. return -EINVAL;
  1483. }
  1484. return 0;
  1485. }
  1486. static int ave_ld20_get_pinmode(struct ave_private *priv,
  1487. phy_interface_t phy_mode, u32 arg)
  1488. {
  1489. if (arg > 0)
  1490. return -EINVAL;
  1491. priv->pinmode_mask = SG_ETPINMODE_RMII(0);
  1492. switch (phy_mode) {
  1493. case PHY_INTERFACE_MODE_RMII:
  1494. priv->pinmode_val = SG_ETPINMODE_RMII(0);
  1495. break;
  1496. case PHY_INTERFACE_MODE_RGMII:
  1497. priv->pinmode_val = 0;
  1498. break;
  1499. default:
  1500. return -EINVAL;
  1501. }
  1502. return 0;
  1503. }
  1504. static int ave_pxs3_get_pinmode(struct ave_private *priv,
  1505. phy_interface_t phy_mode, u32 arg)
  1506. {
  1507. if (arg > 1)
  1508. return -EINVAL;
  1509. priv->pinmode_mask = SG_ETPINMODE_RMII(arg);
  1510. switch (phy_mode) {
  1511. case PHY_INTERFACE_MODE_RMII:
  1512. priv->pinmode_val = SG_ETPINMODE_RMII(arg);
  1513. break;
  1514. case PHY_INTERFACE_MODE_RGMII:
  1515. priv->pinmode_val = 0;
  1516. break;
  1517. default:
  1518. return -EINVAL;
  1519. }
  1520. return 0;
  1521. }
  1522. static const struct ave_soc_data ave_pro4_data = {
  1523. .is_desc_64bit = false,
  1524. .clock_names = {
  1525. "gio", "ether", "ether-gb", "ether-phy",
  1526. },
  1527. .reset_names = {
  1528. "gio", "ether",
  1529. },
  1530. .get_pinmode = ave_pro4_get_pinmode,
  1531. };
  1532. static const struct ave_soc_data ave_pxs2_data = {
  1533. .is_desc_64bit = false,
  1534. .clock_names = {
  1535. "ether",
  1536. },
  1537. .reset_names = {
  1538. "ether",
  1539. },
  1540. .get_pinmode = ave_pro4_get_pinmode,
  1541. };
  1542. static const struct ave_soc_data ave_ld11_data = {
  1543. .is_desc_64bit = false,
  1544. .clock_names = {
  1545. "ether",
  1546. },
  1547. .reset_names = {
  1548. "ether",
  1549. },
  1550. .get_pinmode = ave_ld11_get_pinmode,
  1551. };
  1552. static const struct ave_soc_data ave_ld20_data = {
  1553. .is_desc_64bit = true,
  1554. .clock_names = {
  1555. "ether",
  1556. },
  1557. .reset_names = {
  1558. "ether",
  1559. },
  1560. .get_pinmode = ave_ld20_get_pinmode,
  1561. };
  1562. static const struct ave_soc_data ave_pxs3_data = {
  1563. .is_desc_64bit = false,
  1564. .clock_names = {
  1565. "ether",
  1566. },
  1567. .reset_names = {
  1568. "ether",
  1569. },
  1570. .get_pinmode = ave_pxs3_get_pinmode,
  1571. };
  1572. static const struct of_device_id of_ave_match[] = {
  1573. {
  1574. .compatible = "socionext,uniphier-pro4-ave4",
  1575. .data = &ave_pro4_data,
  1576. },
  1577. {
  1578. .compatible = "socionext,uniphier-pxs2-ave4",
  1579. .data = &ave_pxs2_data,
  1580. },
  1581. {
  1582. .compatible = "socionext,uniphier-ld11-ave4",
  1583. .data = &ave_ld11_data,
  1584. },
  1585. {
  1586. .compatible = "socionext,uniphier-ld20-ave4",
  1587. .data = &ave_ld20_data,
  1588. },
  1589. {
  1590. .compatible = "socionext,uniphier-pxs3-ave4",
  1591. .data = &ave_pxs3_data,
  1592. },
  1593. { /* Sentinel */ }
  1594. };
  1595. MODULE_DEVICE_TABLE(of, of_ave_match);
  1596. static struct platform_driver ave_driver = {
  1597. .probe = ave_probe,
  1598. .remove = ave_remove,
  1599. .driver = {
  1600. .name = "ave",
  1601. .pm = AVE_PM_OPS,
  1602. .of_match_table = of_ave_match,
  1603. },
  1604. };
  1605. module_platform_driver(ave_driver);
  1606. MODULE_AUTHOR("Kunihiko Hayashi <hayashi.kunihiko@socionext.com>");
  1607. MODULE_DESCRIPTION("Socionext UniPhier AVE ethernet driver");
  1608. MODULE_LICENSE("GPL v2");