sni_ave.c 48 KB

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