ravb_main.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Renesas Ethernet AVB device driver
  3. *
  4. * Copyright (C) 2014-2019 Renesas Electronics Corporation
  5. * Copyright (C) 2015 Renesas Solutions Corp.
  6. * Copyright (C) 2015-2016 Cogent Embedded, Inc. <source@cogentembedded.com>
  7. *
  8. * Based on the SuperH Ethernet driver
  9. */
  10. #include <linux/cache.h>
  11. #include <linux/clk.h>
  12. #include <linux/delay.h>
  13. #include <linux/dma-mapping.h>
  14. #include <linux/err.h>
  15. #include <linux/etherdevice.h>
  16. #include <linux/ethtool.h>
  17. #include <linux/if_vlan.h>
  18. #include <linux/kernel.h>
  19. #include <linux/list.h>
  20. #include <linux/module.h>
  21. #include <linux/net_tstamp.h>
  22. #include <linux/of.h>
  23. #include <linux/of_device.h>
  24. #include <linux/of_irq.h>
  25. #include <linux/of_mdio.h>
  26. #include <linux/of_net.h>
  27. #include <linux/pm_runtime.h>
  28. #include <linux/slab.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/sys_soc.h>
  31. #include <asm/div64.h>
  32. #include "ravb.h"
  33. #define RAVB_DEF_MSG_ENABLE \
  34. (NETIF_MSG_LINK | \
  35. NETIF_MSG_TIMER | \
  36. NETIF_MSG_RX_ERR | \
  37. NETIF_MSG_TX_ERR)
  38. static const char *ravb_rx_irqs[NUM_RX_QUEUE] = {
  39. "ch0", /* RAVB_BE */
  40. "ch1", /* RAVB_NC */
  41. };
  42. static const char *ravb_tx_irqs[NUM_TX_QUEUE] = {
  43. "ch18", /* RAVB_BE */
  44. "ch19", /* RAVB_NC */
  45. };
  46. void ravb_modify(struct net_device *ndev, enum ravb_reg reg, u32 clear,
  47. u32 set)
  48. {
  49. ravb_write(ndev, (ravb_read(ndev, reg) & ~clear) | set, reg);
  50. }
  51. int ravb_wait(struct net_device *ndev, enum ravb_reg reg, u32 mask, u32 value)
  52. {
  53. int i;
  54. for (i = 0; i < 10000; i++) {
  55. if ((ravb_read(ndev, reg) & mask) == value)
  56. return 0;
  57. udelay(10);
  58. }
  59. return -ETIMEDOUT;
  60. }
  61. static int ravb_config(struct net_device *ndev)
  62. {
  63. int error;
  64. /* Set config mode */
  65. ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_CONFIG);
  66. /* Check if the operating mode is changed to the config mode */
  67. error = ravb_wait(ndev, CSR, CSR_OPS, CSR_OPS_CONFIG);
  68. if (error)
  69. netdev_err(ndev, "failed to switch device to config mode\n");
  70. return error;
  71. }
  72. static void ravb_set_rate(struct net_device *ndev)
  73. {
  74. struct ravb_private *priv = netdev_priv(ndev);
  75. switch (priv->speed) {
  76. case 100: /* 100BASE */
  77. ravb_write(ndev, GECMR_SPEED_100, GECMR);
  78. break;
  79. case 1000: /* 1000BASE */
  80. ravb_write(ndev, GECMR_SPEED_1000, GECMR);
  81. break;
  82. }
  83. }
  84. static void ravb_set_buffer_align(struct sk_buff *skb)
  85. {
  86. u32 reserve = (unsigned long)skb->data & (RAVB_ALIGN - 1);
  87. if (reserve)
  88. skb_reserve(skb, RAVB_ALIGN - reserve);
  89. }
  90. /* Get MAC address from the MAC address registers
  91. *
  92. * Ethernet AVB device doesn't have ROM for MAC address.
  93. * This function gets the MAC address that was used by a bootloader.
  94. */
  95. static void ravb_read_mac_address(struct net_device *ndev, const u8 *mac)
  96. {
  97. if (mac) {
  98. ether_addr_copy(ndev->dev_addr, mac);
  99. } else {
  100. u32 mahr = ravb_read(ndev, MAHR);
  101. u32 malr = ravb_read(ndev, MALR);
  102. ndev->dev_addr[0] = (mahr >> 24) & 0xFF;
  103. ndev->dev_addr[1] = (mahr >> 16) & 0xFF;
  104. ndev->dev_addr[2] = (mahr >> 8) & 0xFF;
  105. ndev->dev_addr[3] = (mahr >> 0) & 0xFF;
  106. ndev->dev_addr[4] = (malr >> 8) & 0xFF;
  107. ndev->dev_addr[5] = (malr >> 0) & 0xFF;
  108. }
  109. }
  110. static void ravb_mdio_ctrl(struct mdiobb_ctrl *ctrl, u32 mask, int set)
  111. {
  112. struct ravb_private *priv = container_of(ctrl, struct ravb_private,
  113. mdiobb);
  114. ravb_modify(priv->ndev, PIR, mask, set ? mask : 0);
  115. }
  116. /* MDC pin control */
  117. static void ravb_set_mdc(struct mdiobb_ctrl *ctrl, int level)
  118. {
  119. ravb_mdio_ctrl(ctrl, PIR_MDC, level);
  120. }
  121. /* Data I/O pin control */
  122. static void ravb_set_mdio_dir(struct mdiobb_ctrl *ctrl, int output)
  123. {
  124. ravb_mdio_ctrl(ctrl, PIR_MMD, output);
  125. }
  126. /* Set data bit */
  127. static void ravb_set_mdio_data(struct mdiobb_ctrl *ctrl, int value)
  128. {
  129. ravb_mdio_ctrl(ctrl, PIR_MDO, value);
  130. }
  131. /* Get data bit */
  132. static int ravb_get_mdio_data(struct mdiobb_ctrl *ctrl)
  133. {
  134. struct ravb_private *priv = container_of(ctrl, struct ravb_private,
  135. mdiobb);
  136. return (ravb_read(priv->ndev, PIR) & PIR_MDI) != 0;
  137. }
  138. /* MDIO bus control struct */
  139. static struct mdiobb_ops bb_ops = {
  140. .owner = THIS_MODULE,
  141. .set_mdc = ravb_set_mdc,
  142. .set_mdio_dir = ravb_set_mdio_dir,
  143. .set_mdio_data = ravb_set_mdio_data,
  144. .get_mdio_data = ravb_get_mdio_data,
  145. };
  146. /* Free TX skb function for AVB-IP */
  147. static int ravb_tx_free(struct net_device *ndev, int q, bool free_txed_only)
  148. {
  149. struct ravb_private *priv = netdev_priv(ndev);
  150. struct net_device_stats *stats = &priv->stats[q];
  151. struct ravb_tx_desc *desc;
  152. int free_num = 0;
  153. int entry;
  154. u32 size;
  155. for (; priv->cur_tx[q] - priv->dirty_tx[q] > 0; priv->dirty_tx[q]++) {
  156. bool txed;
  157. entry = priv->dirty_tx[q] % (priv->num_tx_ring[q] *
  158. NUM_TX_DESC);
  159. desc = &priv->tx_ring[q][entry];
  160. txed = desc->die_dt == DT_FEMPTY;
  161. if (free_txed_only && !txed)
  162. break;
  163. /* Descriptor type must be checked before all other reads */
  164. dma_rmb();
  165. size = le16_to_cpu(desc->ds_tagl) & TX_DS;
  166. /* Free the original skb. */
  167. if (priv->tx_skb[q][entry / NUM_TX_DESC]) {
  168. dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr),
  169. size, DMA_TO_DEVICE);
  170. /* Last packet descriptor? */
  171. if (entry % NUM_TX_DESC == NUM_TX_DESC - 1) {
  172. entry /= NUM_TX_DESC;
  173. dev_kfree_skb_any(priv->tx_skb[q][entry]);
  174. priv->tx_skb[q][entry] = NULL;
  175. if (txed)
  176. stats->tx_packets++;
  177. }
  178. free_num++;
  179. }
  180. if (txed)
  181. stats->tx_bytes += size;
  182. desc->die_dt = DT_EEMPTY;
  183. }
  184. return free_num;
  185. }
  186. /* Free skb's and DMA buffers for Ethernet AVB */
  187. static void ravb_ring_free(struct net_device *ndev, int q)
  188. {
  189. struct ravb_private *priv = netdev_priv(ndev);
  190. int ring_size;
  191. int i;
  192. if (priv->rx_ring[q]) {
  193. for (i = 0; i < priv->num_rx_ring[q]; i++) {
  194. struct ravb_ex_rx_desc *desc = &priv->rx_ring[q][i];
  195. if (!dma_mapping_error(ndev->dev.parent,
  196. le32_to_cpu(desc->dptr)))
  197. dma_unmap_single(ndev->dev.parent,
  198. le32_to_cpu(desc->dptr),
  199. priv->rx_buf_sz,
  200. DMA_FROM_DEVICE);
  201. }
  202. ring_size = sizeof(struct ravb_ex_rx_desc) *
  203. (priv->num_rx_ring[q] + 1);
  204. dma_free_coherent(ndev->dev.parent, ring_size, priv->rx_ring[q],
  205. priv->rx_desc_dma[q]);
  206. priv->rx_ring[q] = NULL;
  207. }
  208. if (priv->tx_ring[q]) {
  209. ravb_tx_free(ndev, q, false);
  210. ring_size = sizeof(struct ravb_tx_desc) *
  211. (priv->num_tx_ring[q] * NUM_TX_DESC + 1);
  212. dma_free_coherent(ndev->dev.parent, ring_size, priv->tx_ring[q],
  213. priv->tx_desc_dma[q]);
  214. priv->tx_ring[q] = NULL;
  215. }
  216. /* Free RX skb ringbuffer */
  217. if (priv->rx_skb[q]) {
  218. for (i = 0; i < priv->num_rx_ring[q]; i++)
  219. dev_kfree_skb(priv->rx_skb[q][i]);
  220. }
  221. kfree(priv->rx_skb[q]);
  222. priv->rx_skb[q] = NULL;
  223. /* Free aligned TX buffers */
  224. kfree(priv->tx_align[q]);
  225. priv->tx_align[q] = NULL;
  226. /* Free TX skb ringbuffer.
  227. * SKBs are freed by ravb_tx_free() call above.
  228. */
  229. kfree(priv->tx_skb[q]);
  230. priv->tx_skb[q] = NULL;
  231. }
  232. /* Format skb and descriptor buffer for Ethernet AVB */
  233. static void ravb_ring_format(struct net_device *ndev, int q)
  234. {
  235. struct ravb_private *priv = netdev_priv(ndev);
  236. struct ravb_ex_rx_desc *rx_desc;
  237. struct ravb_tx_desc *tx_desc;
  238. struct ravb_desc *desc;
  239. int rx_ring_size = sizeof(*rx_desc) * priv->num_rx_ring[q];
  240. int tx_ring_size = sizeof(*tx_desc) * priv->num_tx_ring[q] *
  241. NUM_TX_DESC;
  242. dma_addr_t dma_addr;
  243. int i;
  244. priv->cur_rx[q] = 0;
  245. priv->cur_tx[q] = 0;
  246. priv->dirty_rx[q] = 0;
  247. priv->dirty_tx[q] = 0;
  248. memset(priv->rx_ring[q], 0, rx_ring_size);
  249. /* Build RX ring buffer */
  250. for (i = 0; i < priv->num_rx_ring[q]; i++) {
  251. /* RX descriptor */
  252. rx_desc = &priv->rx_ring[q][i];
  253. rx_desc->ds_cc = cpu_to_le16(priv->rx_buf_sz);
  254. dma_addr = dma_map_single(ndev->dev.parent, priv->rx_skb[q][i]->data,
  255. priv->rx_buf_sz,
  256. DMA_FROM_DEVICE);
  257. /* We just set the data size to 0 for a failed mapping which
  258. * should prevent DMA from happening...
  259. */
  260. if (dma_mapping_error(ndev->dev.parent, dma_addr))
  261. rx_desc->ds_cc = cpu_to_le16(0);
  262. rx_desc->dptr = cpu_to_le32(dma_addr);
  263. rx_desc->die_dt = DT_FEMPTY;
  264. }
  265. rx_desc = &priv->rx_ring[q][i];
  266. rx_desc->dptr = cpu_to_le32((u32)priv->rx_desc_dma[q]);
  267. rx_desc->die_dt = DT_LINKFIX; /* type */
  268. memset(priv->tx_ring[q], 0, tx_ring_size);
  269. /* Build TX ring buffer */
  270. for (i = 0, tx_desc = priv->tx_ring[q]; i < priv->num_tx_ring[q];
  271. i++, tx_desc++) {
  272. tx_desc->die_dt = DT_EEMPTY;
  273. tx_desc++;
  274. tx_desc->die_dt = DT_EEMPTY;
  275. }
  276. tx_desc->dptr = cpu_to_le32((u32)priv->tx_desc_dma[q]);
  277. tx_desc->die_dt = DT_LINKFIX; /* type */
  278. /* RX descriptor base address for best effort */
  279. desc = &priv->desc_bat[RX_QUEUE_OFFSET + q];
  280. desc->die_dt = DT_LINKFIX; /* type */
  281. desc->dptr = cpu_to_le32((u32)priv->rx_desc_dma[q]);
  282. /* TX descriptor base address for best effort */
  283. desc = &priv->desc_bat[q];
  284. desc->die_dt = DT_LINKFIX; /* type */
  285. desc->dptr = cpu_to_le32((u32)priv->tx_desc_dma[q]);
  286. }
  287. /* Init skb and descriptor buffer for Ethernet AVB */
  288. static int ravb_ring_init(struct net_device *ndev, int q)
  289. {
  290. struct ravb_private *priv = netdev_priv(ndev);
  291. struct sk_buff *skb;
  292. int ring_size;
  293. int i;
  294. priv->rx_buf_sz = (ndev->mtu <= 1492 ? PKT_BUF_SZ : ndev->mtu) +
  295. ETH_HLEN + VLAN_HLEN + sizeof(__sum16);
  296. /* Allocate RX and TX skb rings */
  297. priv->rx_skb[q] = kcalloc(priv->num_rx_ring[q],
  298. sizeof(*priv->rx_skb[q]), GFP_KERNEL);
  299. priv->tx_skb[q] = kcalloc(priv->num_tx_ring[q],
  300. sizeof(*priv->tx_skb[q]), GFP_KERNEL);
  301. if (!priv->rx_skb[q] || !priv->tx_skb[q])
  302. goto error;
  303. for (i = 0; i < priv->num_rx_ring[q]; i++) {
  304. skb = netdev_alloc_skb(ndev, priv->rx_buf_sz + RAVB_ALIGN - 1);
  305. if (!skb)
  306. goto error;
  307. ravb_set_buffer_align(skb);
  308. priv->rx_skb[q][i] = skb;
  309. }
  310. /* Allocate rings for the aligned buffers */
  311. priv->tx_align[q] = kmalloc(DPTR_ALIGN * priv->num_tx_ring[q] +
  312. DPTR_ALIGN - 1, GFP_KERNEL);
  313. if (!priv->tx_align[q])
  314. goto error;
  315. /* Allocate all RX descriptors. */
  316. ring_size = sizeof(struct ravb_ex_rx_desc) * (priv->num_rx_ring[q] + 1);
  317. priv->rx_ring[q] = dma_alloc_coherent(ndev->dev.parent, ring_size,
  318. &priv->rx_desc_dma[q],
  319. GFP_KERNEL);
  320. if (!priv->rx_ring[q])
  321. goto error;
  322. priv->dirty_rx[q] = 0;
  323. /* Allocate all TX descriptors. */
  324. ring_size = sizeof(struct ravb_tx_desc) *
  325. (priv->num_tx_ring[q] * NUM_TX_DESC + 1);
  326. priv->tx_ring[q] = dma_alloc_coherent(ndev->dev.parent, ring_size,
  327. &priv->tx_desc_dma[q],
  328. GFP_KERNEL);
  329. if (!priv->tx_ring[q])
  330. goto error;
  331. return 0;
  332. error:
  333. ravb_ring_free(ndev, q);
  334. return -ENOMEM;
  335. }
  336. /* E-MAC init function */
  337. static void ravb_emac_init(struct net_device *ndev)
  338. {
  339. /* Receive frame limit set register */
  340. ravb_write(ndev, ndev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN, RFLR);
  341. /* EMAC Mode: PAUSE prohibition; Duplex; RX Checksum; TX; RX */
  342. ravb_write(ndev, ECMR_ZPF | ECMR_DM |
  343. (ndev->features & NETIF_F_RXCSUM ? ECMR_RCSC : 0) |
  344. ECMR_TE | ECMR_RE, ECMR);
  345. ravb_set_rate(ndev);
  346. /* Set MAC address */
  347. ravb_write(ndev,
  348. (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) |
  349. (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR);
  350. ravb_write(ndev,
  351. (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR);
  352. /* E-MAC status register clear */
  353. ravb_write(ndev, ECSR_ICD | ECSR_MPD, ECSR);
  354. /* E-MAC interrupt enable register */
  355. ravb_write(ndev, ECSIPR_ICDIP | ECSIPR_MPDIP | ECSIPR_LCHNGIP, ECSIPR);
  356. }
  357. /* Device init function for Ethernet AVB */
  358. static int ravb_dmac_init(struct net_device *ndev)
  359. {
  360. struct ravb_private *priv = netdev_priv(ndev);
  361. int error;
  362. /* Set CONFIG mode */
  363. error = ravb_config(ndev);
  364. if (error)
  365. return error;
  366. error = ravb_ring_init(ndev, RAVB_BE);
  367. if (error)
  368. return error;
  369. error = ravb_ring_init(ndev, RAVB_NC);
  370. if (error) {
  371. ravb_ring_free(ndev, RAVB_BE);
  372. return error;
  373. }
  374. /* Descriptor format */
  375. ravb_ring_format(ndev, RAVB_BE);
  376. ravb_ring_format(ndev, RAVB_NC);
  377. #if defined(__LITTLE_ENDIAN)
  378. ravb_modify(ndev, CCC, CCC_BOC, 0);
  379. #else
  380. ravb_modify(ndev, CCC, CCC_BOC, CCC_BOC);
  381. #endif
  382. /* Set AVB RX */
  383. ravb_write(ndev,
  384. RCR_EFFS | RCR_ENCF | RCR_ETS0 | RCR_ESF | 0x18000000, RCR);
  385. /* Set FIFO size */
  386. ravb_write(ndev, TGC_TQP_AVBMODE1 | 0x00112200, TGC);
  387. /* Timestamp enable */
  388. ravb_write(ndev, TCCR_TFEN, TCCR);
  389. /* Interrupt init: */
  390. if (priv->chip_id == RCAR_GEN3) {
  391. /* Clear DIL.DPLx */
  392. ravb_write(ndev, 0, DIL);
  393. /* Set queue specific interrupt */
  394. ravb_write(ndev, CIE_CRIE | CIE_CTIE | CIE_CL0M, CIE);
  395. }
  396. /* Frame receive */
  397. ravb_write(ndev, RIC0_FRE0 | RIC0_FRE1, RIC0);
  398. /* Disable FIFO full warning */
  399. ravb_write(ndev, 0, RIC1);
  400. /* Receive FIFO full error, descriptor empty */
  401. ravb_write(ndev, RIC2_QFE0 | RIC2_QFE1 | RIC2_RFFE, RIC2);
  402. /* Frame transmitted, timestamp FIFO updated */
  403. ravb_write(ndev, TIC_FTE0 | TIC_FTE1 | TIC_TFUE, TIC);
  404. /* Setting the control will start the AVB-DMAC process. */
  405. ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_OPERATION);
  406. return 0;
  407. }
  408. static void ravb_get_tx_tstamp(struct net_device *ndev)
  409. {
  410. struct ravb_private *priv = netdev_priv(ndev);
  411. struct ravb_tstamp_skb *ts_skb, *ts_skb2;
  412. struct skb_shared_hwtstamps shhwtstamps;
  413. struct sk_buff *skb;
  414. struct timespec64 ts;
  415. u16 tag, tfa_tag;
  416. int count;
  417. u32 tfa2;
  418. count = (ravb_read(ndev, TSR) & TSR_TFFL) >> 8;
  419. while (count--) {
  420. tfa2 = ravb_read(ndev, TFA2);
  421. tfa_tag = (tfa2 & TFA2_TST) >> 16;
  422. ts.tv_nsec = (u64)ravb_read(ndev, TFA0);
  423. ts.tv_sec = ((u64)(tfa2 & TFA2_TSV) << 32) |
  424. ravb_read(ndev, TFA1);
  425. memset(&shhwtstamps, 0, sizeof(shhwtstamps));
  426. shhwtstamps.hwtstamp = timespec64_to_ktime(ts);
  427. list_for_each_entry_safe(ts_skb, ts_skb2, &priv->ts_skb_list,
  428. list) {
  429. skb = ts_skb->skb;
  430. tag = ts_skb->tag;
  431. list_del(&ts_skb->list);
  432. kfree(ts_skb);
  433. if (tag == tfa_tag) {
  434. skb_tstamp_tx(skb, &shhwtstamps);
  435. dev_consume_skb_any(skb);
  436. break;
  437. } else {
  438. dev_kfree_skb_any(skb);
  439. }
  440. }
  441. ravb_modify(ndev, TCCR, TCCR_TFR, TCCR_TFR);
  442. }
  443. }
  444. static void ravb_rx_csum(struct sk_buff *skb)
  445. {
  446. u8 *hw_csum;
  447. /* The hardware checksum is contained in sizeof(__sum16) (2) bytes
  448. * appended to packet data
  449. */
  450. if (unlikely(skb->len < sizeof(__sum16)))
  451. return;
  452. hw_csum = skb_tail_pointer(skb) - sizeof(__sum16);
  453. skb->csum = csum_unfold((__force __sum16)get_unaligned_le16(hw_csum));
  454. skb->ip_summed = CHECKSUM_COMPLETE;
  455. skb_trim(skb, skb->len - sizeof(__sum16));
  456. }
  457. /* Packet receive function for Ethernet AVB */
  458. static bool ravb_rx(struct net_device *ndev, int *quota, int q)
  459. {
  460. struct ravb_private *priv = netdev_priv(ndev);
  461. int entry = priv->cur_rx[q] % priv->num_rx_ring[q];
  462. int boguscnt = (priv->dirty_rx[q] + priv->num_rx_ring[q]) -
  463. priv->cur_rx[q];
  464. struct net_device_stats *stats = &priv->stats[q];
  465. struct ravb_ex_rx_desc *desc;
  466. struct sk_buff *skb;
  467. dma_addr_t dma_addr;
  468. struct timespec64 ts;
  469. u8 desc_status;
  470. u16 pkt_len;
  471. int limit;
  472. boguscnt = min(boguscnt, *quota);
  473. limit = boguscnt;
  474. desc = &priv->rx_ring[q][entry];
  475. while (desc->die_dt != DT_FEMPTY) {
  476. /* Descriptor type must be checked before all other reads */
  477. dma_rmb();
  478. desc_status = desc->msc;
  479. pkt_len = le16_to_cpu(desc->ds_cc) & RX_DS;
  480. if (--boguscnt < 0)
  481. break;
  482. /* We use 0-byte descriptors to mark the DMA mapping errors */
  483. if (!pkt_len)
  484. continue;
  485. if (desc_status & MSC_MC)
  486. stats->multicast++;
  487. if (desc_status & (MSC_CRC | MSC_RFE | MSC_RTSF | MSC_RTLF |
  488. MSC_CEEF)) {
  489. stats->rx_errors++;
  490. if (desc_status & MSC_CRC)
  491. stats->rx_crc_errors++;
  492. if (desc_status & MSC_RFE)
  493. stats->rx_frame_errors++;
  494. if (desc_status & (MSC_RTLF | MSC_RTSF))
  495. stats->rx_length_errors++;
  496. if (desc_status & MSC_CEEF)
  497. stats->rx_missed_errors++;
  498. } else {
  499. u32 get_ts = priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE;
  500. skb = priv->rx_skb[q][entry];
  501. priv->rx_skb[q][entry] = NULL;
  502. dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr),
  503. priv->rx_buf_sz,
  504. DMA_FROM_DEVICE);
  505. get_ts &= (q == RAVB_NC) ?
  506. RAVB_RXTSTAMP_TYPE_V2_L2_EVENT :
  507. ~RAVB_RXTSTAMP_TYPE_V2_L2_EVENT;
  508. if (get_ts) {
  509. struct skb_shared_hwtstamps *shhwtstamps;
  510. shhwtstamps = skb_hwtstamps(skb);
  511. memset(shhwtstamps, 0, sizeof(*shhwtstamps));
  512. ts.tv_sec = ((u64) le16_to_cpu(desc->ts_sh) <<
  513. 32) | le32_to_cpu(desc->ts_sl);
  514. ts.tv_nsec = le32_to_cpu(desc->ts_n);
  515. shhwtstamps->hwtstamp = timespec64_to_ktime(ts);
  516. }
  517. skb_put(skb, pkt_len);
  518. skb->protocol = eth_type_trans(skb, ndev);
  519. if (ndev->features & NETIF_F_RXCSUM)
  520. ravb_rx_csum(skb);
  521. napi_gro_receive(&priv->napi[q], skb);
  522. stats->rx_packets++;
  523. stats->rx_bytes += pkt_len;
  524. }
  525. entry = (++priv->cur_rx[q]) % priv->num_rx_ring[q];
  526. desc = &priv->rx_ring[q][entry];
  527. }
  528. /* Refill the RX ring buffers. */
  529. for (; priv->cur_rx[q] - priv->dirty_rx[q] > 0; priv->dirty_rx[q]++) {
  530. entry = priv->dirty_rx[q] % priv->num_rx_ring[q];
  531. desc = &priv->rx_ring[q][entry];
  532. desc->ds_cc = cpu_to_le16(priv->rx_buf_sz);
  533. if (!priv->rx_skb[q][entry]) {
  534. skb = netdev_alloc_skb(ndev,
  535. priv->rx_buf_sz +
  536. RAVB_ALIGN - 1);
  537. if (!skb)
  538. break; /* Better luck next round. */
  539. ravb_set_buffer_align(skb);
  540. dma_addr = dma_map_single(ndev->dev.parent, skb->data,
  541. le16_to_cpu(desc->ds_cc),
  542. DMA_FROM_DEVICE);
  543. skb_checksum_none_assert(skb);
  544. /* We just set the data size to 0 for a failed mapping
  545. * which should prevent DMA from happening...
  546. */
  547. if (dma_mapping_error(ndev->dev.parent, dma_addr))
  548. desc->ds_cc = cpu_to_le16(0);
  549. desc->dptr = cpu_to_le32(dma_addr);
  550. priv->rx_skb[q][entry] = skb;
  551. }
  552. /* Descriptor type must be set after all the above writes */
  553. dma_wmb();
  554. desc->die_dt = DT_FEMPTY;
  555. }
  556. *quota -= limit - (++boguscnt);
  557. return boguscnt <= 0;
  558. }
  559. static void ravb_rcv_snd_disable(struct net_device *ndev)
  560. {
  561. /* Disable TX and RX */
  562. ravb_modify(ndev, ECMR, ECMR_RE | ECMR_TE, 0);
  563. }
  564. static void ravb_rcv_snd_enable(struct net_device *ndev)
  565. {
  566. /* Enable TX and RX */
  567. ravb_modify(ndev, ECMR, ECMR_RE | ECMR_TE, ECMR_RE | ECMR_TE);
  568. }
  569. /* function for waiting dma process finished */
  570. static int ravb_stop_dma(struct net_device *ndev)
  571. {
  572. int error;
  573. /* Wait for stopping the hardware TX process */
  574. error = ravb_wait(ndev, TCCR,
  575. TCCR_TSRQ0 | TCCR_TSRQ1 | TCCR_TSRQ2 | TCCR_TSRQ3, 0);
  576. if (error)
  577. return error;
  578. error = ravb_wait(ndev, CSR, CSR_TPO0 | CSR_TPO1 | CSR_TPO2 | CSR_TPO3,
  579. 0);
  580. if (error)
  581. return error;
  582. /* Stop the E-MAC's RX/TX processes. */
  583. ravb_rcv_snd_disable(ndev);
  584. /* Wait for stopping the RX DMA process */
  585. error = ravb_wait(ndev, CSR, CSR_RPO, 0);
  586. if (error)
  587. return error;
  588. /* Stop AVB-DMAC process */
  589. return ravb_config(ndev);
  590. }
  591. /* E-MAC interrupt handler */
  592. static void ravb_emac_interrupt_unlocked(struct net_device *ndev)
  593. {
  594. struct ravb_private *priv = netdev_priv(ndev);
  595. u32 ecsr, psr;
  596. ecsr = ravb_read(ndev, ECSR);
  597. ravb_write(ndev, ecsr, ECSR); /* clear interrupt */
  598. if (ecsr & ECSR_MPD)
  599. pm_wakeup_event(&priv->pdev->dev, 0);
  600. if (ecsr & ECSR_ICD)
  601. ndev->stats.tx_carrier_errors++;
  602. if (ecsr & ECSR_LCHNG) {
  603. /* Link changed */
  604. if (priv->no_avb_link)
  605. return;
  606. psr = ravb_read(ndev, PSR);
  607. if (priv->avb_link_active_low)
  608. psr ^= PSR_LMON;
  609. if (!(psr & PSR_LMON)) {
  610. /* DIsable RX and TX */
  611. ravb_rcv_snd_disable(ndev);
  612. } else {
  613. /* Enable RX and TX */
  614. ravb_rcv_snd_enable(ndev);
  615. }
  616. }
  617. }
  618. static irqreturn_t ravb_emac_interrupt(int irq, void *dev_id)
  619. {
  620. struct net_device *ndev = dev_id;
  621. struct ravb_private *priv = netdev_priv(ndev);
  622. spin_lock(&priv->lock);
  623. ravb_emac_interrupt_unlocked(ndev);
  624. mmiowb();
  625. spin_unlock(&priv->lock);
  626. return IRQ_HANDLED;
  627. }
  628. /* Error interrupt handler */
  629. static void ravb_error_interrupt(struct net_device *ndev)
  630. {
  631. struct ravb_private *priv = netdev_priv(ndev);
  632. u32 eis, ris2;
  633. eis = ravb_read(ndev, EIS);
  634. ravb_write(ndev, ~(EIS_QFS | EIS_RESERVED), EIS);
  635. if (eis & EIS_QFS) {
  636. ris2 = ravb_read(ndev, RIS2);
  637. ravb_write(ndev, ~(RIS2_QFF0 | RIS2_RFFF | RIS2_RESERVED),
  638. RIS2);
  639. /* Receive Descriptor Empty int */
  640. if (ris2 & RIS2_QFF0)
  641. priv->stats[RAVB_BE].rx_over_errors++;
  642. /* Receive Descriptor Empty int */
  643. if (ris2 & RIS2_QFF1)
  644. priv->stats[RAVB_NC].rx_over_errors++;
  645. /* Receive FIFO Overflow int */
  646. if (ris2 & RIS2_RFFF)
  647. priv->rx_fifo_errors++;
  648. }
  649. }
  650. static bool ravb_queue_interrupt(struct net_device *ndev, int q)
  651. {
  652. struct ravb_private *priv = netdev_priv(ndev);
  653. u32 ris0 = ravb_read(ndev, RIS0);
  654. u32 ric0 = ravb_read(ndev, RIC0);
  655. u32 tis = ravb_read(ndev, TIS);
  656. u32 tic = ravb_read(ndev, TIC);
  657. if (((ris0 & ric0) & BIT(q)) || ((tis & tic) & BIT(q))) {
  658. if (napi_schedule_prep(&priv->napi[q])) {
  659. /* Mask RX and TX interrupts */
  660. if (priv->chip_id == RCAR_GEN2) {
  661. ravb_write(ndev, ric0 & ~BIT(q), RIC0);
  662. ravb_write(ndev, tic & ~BIT(q), TIC);
  663. } else {
  664. ravb_write(ndev, BIT(q), RID0);
  665. ravb_write(ndev, BIT(q), TID);
  666. }
  667. __napi_schedule(&priv->napi[q]);
  668. } else {
  669. netdev_warn(ndev,
  670. "ignoring interrupt, rx status 0x%08x, rx mask 0x%08x,\n",
  671. ris0, ric0);
  672. netdev_warn(ndev,
  673. " tx status 0x%08x, tx mask 0x%08x.\n",
  674. tis, tic);
  675. }
  676. return true;
  677. }
  678. return false;
  679. }
  680. static bool ravb_timestamp_interrupt(struct net_device *ndev)
  681. {
  682. u32 tis = ravb_read(ndev, TIS);
  683. if (tis & TIS_TFUF) {
  684. ravb_write(ndev, ~(TIS_TFUF | TIS_RESERVED), TIS);
  685. ravb_get_tx_tstamp(ndev);
  686. return true;
  687. }
  688. return false;
  689. }
  690. static irqreturn_t ravb_interrupt(int irq, void *dev_id)
  691. {
  692. struct net_device *ndev = dev_id;
  693. struct ravb_private *priv = netdev_priv(ndev);
  694. irqreturn_t result = IRQ_NONE;
  695. u32 iss;
  696. spin_lock(&priv->lock);
  697. /* Get interrupt status */
  698. iss = ravb_read(ndev, ISS);
  699. /* Received and transmitted interrupts */
  700. if (iss & (ISS_FRS | ISS_FTS | ISS_TFUS)) {
  701. int q;
  702. /* Timestamp updated */
  703. if (ravb_timestamp_interrupt(ndev))
  704. result = IRQ_HANDLED;
  705. /* Network control and best effort queue RX/TX */
  706. for (q = RAVB_NC; q >= RAVB_BE; q--) {
  707. if (ravb_queue_interrupt(ndev, q))
  708. result = IRQ_HANDLED;
  709. }
  710. }
  711. /* E-MAC status summary */
  712. if (iss & ISS_MS) {
  713. ravb_emac_interrupt_unlocked(ndev);
  714. result = IRQ_HANDLED;
  715. }
  716. /* Error status summary */
  717. if (iss & ISS_ES) {
  718. ravb_error_interrupt(ndev);
  719. result = IRQ_HANDLED;
  720. }
  721. /* gPTP interrupt status summary */
  722. if (iss & ISS_CGIS) {
  723. ravb_ptp_interrupt(ndev);
  724. result = IRQ_HANDLED;
  725. }
  726. mmiowb();
  727. spin_unlock(&priv->lock);
  728. return result;
  729. }
  730. /* Timestamp/Error/gPTP interrupt handler */
  731. static irqreturn_t ravb_multi_interrupt(int irq, void *dev_id)
  732. {
  733. struct net_device *ndev = dev_id;
  734. struct ravb_private *priv = netdev_priv(ndev);
  735. irqreturn_t result = IRQ_NONE;
  736. u32 iss;
  737. spin_lock(&priv->lock);
  738. /* Get interrupt status */
  739. iss = ravb_read(ndev, ISS);
  740. /* Timestamp updated */
  741. if ((iss & ISS_TFUS) && ravb_timestamp_interrupt(ndev))
  742. result = IRQ_HANDLED;
  743. /* Error status summary */
  744. if (iss & ISS_ES) {
  745. ravb_error_interrupt(ndev);
  746. result = IRQ_HANDLED;
  747. }
  748. /* gPTP interrupt status summary */
  749. if (iss & ISS_CGIS) {
  750. ravb_ptp_interrupt(ndev);
  751. result = IRQ_HANDLED;
  752. }
  753. mmiowb();
  754. spin_unlock(&priv->lock);
  755. return result;
  756. }
  757. static irqreturn_t ravb_dma_interrupt(int irq, void *dev_id, int q)
  758. {
  759. struct net_device *ndev = dev_id;
  760. struct ravb_private *priv = netdev_priv(ndev);
  761. irqreturn_t result = IRQ_NONE;
  762. spin_lock(&priv->lock);
  763. /* Network control/Best effort queue RX/TX */
  764. if (ravb_queue_interrupt(ndev, q))
  765. result = IRQ_HANDLED;
  766. mmiowb();
  767. spin_unlock(&priv->lock);
  768. return result;
  769. }
  770. static irqreturn_t ravb_be_interrupt(int irq, void *dev_id)
  771. {
  772. return ravb_dma_interrupt(irq, dev_id, RAVB_BE);
  773. }
  774. static irqreturn_t ravb_nc_interrupt(int irq, void *dev_id)
  775. {
  776. return ravb_dma_interrupt(irq, dev_id, RAVB_NC);
  777. }
  778. static int ravb_poll(struct napi_struct *napi, int budget)
  779. {
  780. struct net_device *ndev = napi->dev;
  781. struct ravb_private *priv = netdev_priv(ndev);
  782. unsigned long flags;
  783. int q = napi - priv->napi;
  784. int mask = BIT(q);
  785. int quota = budget;
  786. u32 ris0, tis;
  787. for (;;) {
  788. tis = ravb_read(ndev, TIS);
  789. ris0 = ravb_read(ndev, RIS0);
  790. if (!((ris0 & mask) || (tis & mask)))
  791. break;
  792. /* Processing RX Descriptor Ring */
  793. if (ris0 & mask) {
  794. /* Clear RX interrupt */
  795. ravb_write(ndev, ~(mask | RIS0_RESERVED), RIS0);
  796. if (ravb_rx(ndev, &quota, q))
  797. goto out;
  798. }
  799. /* Processing TX Descriptor Ring */
  800. if (tis & mask) {
  801. spin_lock_irqsave(&priv->lock, flags);
  802. /* Clear TX interrupt */
  803. ravb_write(ndev, ~(mask | TIS_RESERVED), TIS);
  804. ravb_tx_free(ndev, q, true);
  805. netif_wake_subqueue(ndev, q);
  806. mmiowb();
  807. spin_unlock_irqrestore(&priv->lock, flags);
  808. }
  809. }
  810. napi_complete(napi);
  811. /* Re-enable RX/TX interrupts */
  812. spin_lock_irqsave(&priv->lock, flags);
  813. if (priv->chip_id == RCAR_GEN2) {
  814. ravb_modify(ndev, RIC0, mask, mask);
  815. ravb_modify(ndev, TIC, mask, mask);
  816. } else {
  817. ravb_write(ndev, mask, RIE0);
  818. ravb_write(ndev, mask, TIE);
  819. }
  820. mmiowb();
  821. spin_unlock_irqrestore(&priv->lock, flags);
  822. /* Receive error message handling */
  823. priv->rx_over_errors = priv->stats[RAVB_BE].rx_over_errors;
  824. priv->rx_over_errors += priv->stats[RAVB_NC].rx_over_errors;
  825. if (priv->rx_over_errors != ndev->stats.rx_over_errors)
  826. ndev->stats.rx_over_errors = priv->rx_over_errors;
  827. if (priv->rx_fifo_errors != ndev->stats.rx_fifo_errors)
  828. ndev->stats.rx_fifo_errors = priv->rx_fifo_errors;
  829. out:
  830. return budget - quota;
  831. }
  832. /* PHY state control function */
  833. static void ravb_adjust_link(struct net_device *ndev)
  834. {
  835. struct ravb_private *priv = netdev_priv(ndev);
  836. struct phy_device *phydev = ndev->phydev;
  837. bool new_state = false;
  838. unsigned long flags;
  839. spin_lock_irqsave(&priv->lock, flags);
  840. /* Disable TX and RX right over here, if E-MAC change is ignored */
  841. if (priv->no_avb_link)
  842. ravb_rcv_snd_disable(ndev);
  843. if (phydev->link) {
  844. if (phydev->speed != priv->speed) {
  845. new_state = true;
  846. priv->speed = phydev->speed;
  847. ravb_set_rate(ndev);
  848. }
  849. if (!priv->link) {
  850. ravb_modify(ndev, ECMR, ECMR_TXF, 0);
  851. new_state = true;
  852. priv->link = phydev->link;
  853. }
  854. } else if (priv->link) {
  855. new_state = true;
  856. priv->link = 0;
  857. priv->speed = 0;
  858. }
  859. /* Enable TX and RX right over here, if E-MAC change is ignored */
  860. if (priv->no_avb_link && phydev->link)
  861. ravb_rcv_snd_enable(ndev);
  862. mmiowb();
  863. spin_unlock_irqrestore(&priv->lock, flags);
  864. if (new_state && netif_msg_link(priv))
  865. phy_print_status(phydev);
  866. }
  867. static const struct soc_device_attribute r8a7795es10[] = {
  868. { .soc_id = "r8a7795", .revision = "ES1.0", },
  869. { /* sentinel */ }
  870. };
  871. /* PHY init function */
  872. static int ravb_phy_init(struct net_device *ndev)
  873. {
  874. struct device_node *np = ndev->dev.parent->of_node;
  875. struct ravb_private *priv = netdev_priv(ndev);
  876. struct phy_device *phydev;
  877. struct device_node *pn;
  878. int err;
  879. priv->link = 0;
  880. priv->speed = 0;
  881. /* Try connecting to PHY */
  882. pn = of_parse_phandle(np, "phy-handle", 0);
  883. if (!pn) {
  884. /* In the case of a fixed PHY, the DT node associated
  885. * to the PHY is the Ethernet MAC DT node.
  886. */
  887. if (of_phy_is_fixed_link(np)) {
  888. err = of_phy_register_fixed_link(np);
  889. if (err)
  890. return err;
  891. }
  892. pn = of_node_get(np);
  893. }
  894. phydev = of_phy_connect(ndev, pn, ravb_adjust_link, 0,
  895. priv->phy_interface);
  896. of_node_put(pn);
  897. if (!phydev) {
  898. netdev_err(ndev, "failed to connect PHY\n");
  899. err = -ENOENT;
  900. goto err_deregister_fixed_link;
  901. }
  902. /* This driver only support 10/100Mbit speeds on R-Car H3 ES1.0
  903. * at this time.
  904. */
  905. if (soc_device_match(r8a7795es10)) {
  906. err = phy_set_max_speed(phydev, SPEED_100);
  907. if (err) {
  908. netdev_err(ndev, "failed to limit PHY to 100Mbit/s\n");
  909. goto err_phy_disconnect;
  910. }
  911. netdev_info(ndev, "limited PHY to 100Mbit/s\n");
  912. }
  913. /* 10BASE is not supported */
  914. phydev->supported &= ~PHY_10BT_FEATURES;
  915. phy_attached_info(phydev);
  916. return 0;
  917. err_phy_disconnect:
  918. phy_disconnect(phydev);
  919. err_deregister_fixed_link:
  920. if (of_phy_is_fixed_link(np))
  921. of_phy_deregister_fixed_link(np);
  922. return err;
  923. }
  924. /* PHY control start function */
  925. static int ravb_phy_start(struct net_device *ndev)
  926. {
  927. int error;
  928. error = ravb_phy_init(ndev);
  929. if (error)
  930. return error;
  931. phy_start(ndev->phydev);
  932. return 0;
  933. }
  934. static u32 ravb_get_msglevel(struct net_device *ndev)
  935. {
  936. struct ravb_private *priv = netdev_priv(ndev);
  937. return priv->msg_enable;
  938. }
  939. static void ravb_set_msglevel(struct net_device *ndev, u32 value)
  940. {
  941. struct ravb_private *priv = netdev_priv(ndev);
  942. priv->msg_enable = value;
  943. }
  944. static const char ravb_gstrings_stats[][ETH_GSTRING_LEN] = {
  945. "rx_queue_0_current",
  946. "tx_queue_0_current",
  947. "rx_queue_0_dirty",
  948. "tx_queue_0_dirty",
  949. "rx_queue_0_packets",
  950. "tx_queue_0_packets",
  951. "rx_queue_0_bytes",
  952. "tx_queue_0_bytes",
  953. "rx_queue_0_mcast_packets",
  954. "rx_queue_0_errors",
  955. "rx_queue_0_crc_errors",
  956. "rx_queue_0_frame_errors",
  957. "rx_queue_0_length_errors",
  958. "rx_queue_0_missed_errors",
  959. "rx_queue_0_over_errors",
  960. "rx_queue_1_current",
  961. "tx_queue_1_current",
  962. "rx_queue_1_dirty",
  963. "tx_queue_1_dirty",
  964. "rx_queue_1_packets",
  965. "tx_queue_1_packets",
  966. "rx_queue_1_bytes",
  967. "tx_queue_1_bytes",
  968. "rx_queue_1_mcast_packets",
  969. "rx_queue_1_errors",
  970. "rx_queue_1_crc_errors",
  971. "rx_queue_1_frame_errors",
  972. "rx_queue_1_length_errors",
  973. "rx_queue_1_missed_errors",
  974. "rx_queue_1_over_errors",
  975. };
  976. #define RAVB_STATS_LEN ARRAY_SIZE(ravb_gstrings_stats)
  977. static int ravb_get_sset_count(struct net_device *netdev, int sset)
  978. {
  979. switch (sset) {
  980. case ETH_SS_STATS:
  981. return RAVB_STATS_LEN;
  982. default:
  983. return -EOPNOTSUPP;
  984. }
  985. }
  986. static void ravb_get_ethtool_stats(struct net_device *ndev,
  987. struct ethtool_stats *estats, u64 *data)
  988. {
  989. struct ravb_private *priv = netdev_priv(ndev);
  990. int i = 0;
  991. int q;
  992. /* Device-specific stats */
  993. for (q = RAVB_BE; q < NUM_RX_QUEUE; q++) {
  994. struct net_device_stats *stats = &priv->stats[q];
  995. data[i++] = priv->cur_rx[q];
  996. data[i++] = priv->cur_tx[q];
  997. data[i++] = priv->dirty_rx[q];
  998. data[i++] = priv->dirty_tx[q];
  999. data[i++] = stats->rx_packets;
  1000. data[i++] = stats->tx_packets;
  1001. data[i++] = stats->rx_bytes;
  1002. data[i++] = stats->tx_bytes;
  1003. data[i++] = stats->multicast;
  1004. data[i++] = stats->rx_errors;
  1005. data[i++] = stats->rx_crc_errors;
  1006. data[i++] = stats->rx_frame_errors;
  1007. data[i++] = stats->rx_length_errors;
  1008. data[i++] = stats->rx_missed_errors;
  1009. data[i++] = stats->rx_over_errors;
  1010. }
  1011. }
  1012. static void ravb_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
  1013. {
  1014. switch (stringset) {
  1015. case ETH_SS_STATS:
  1016. memcpy(data, ravb_gstrings_stats, sizeof(ravb_gstrings_stats));
  1017. break;
  1018. }
  1019. }
  1020. static void ravb_get_ringparam(struct net_device *ndev,
  1021. struct ethtool_ringparam *ring)
  1022. {
  1023. struct ravb_private *priv = netdev_priv(ndev);
  1024. ring->rx_max_pending = BE_RX_RING_MAX;
  1025. ring->tx_max_pending = BE_TX_RING_MAX;
  1026. ring->rx_pending = priv->num_rx_ring[RAVB_BE];
  1027. ring->tx_pending = priv->num_tx_ring[RAVB_BE];
  1028. }
  1029. static int ravb_set_ringparam(struct net_device *ndev,
  1030. struct ethtool_ringparam *ring)
  1031. {
  1032. struct ravb_private *priv = netdev_priv(ndev);
  1033. int error;
  1034. if (ring->tx_pending > BE_TX_RING_MAX ||
  1035. ring->rx_pending > BE_RX_RING_MAX ||
  1036. ring->tx_pending < BE_TX_RING_MIN ||
  1037. ring->rx_pending < BE_RX_RING_MIN)
  1038. return -EINVAL;
  1039. if (ring->rx_mini_pending || ring->rx_jumbo_pending)
  1040. return -EINVAL;
  1041. if (netif_running(ndev)) {
  1042. netif_device_detach(ndev);
  1043. /* Stop PTP Clock driver */
  1044. if (priv->chip_id == RCAR_GEN2)
  1045. ravb_ptp_stop(ndev);
  1046. /* Wait for DMA stopping */
  1047. error = ravb_stop_dma(ndev);
  1048. if (error) {
  1049. netdev_err(ndev,
  1050. "cannot set ringparam! Any AVB processes are still running?\n");
  1051. return error;
  1052. }
  1053. synchronize_irq(ndev->irq);
  1054. /* Free all the skb's in the RX queue and the DMA buffers. */
  1055. ravb_ring_free(ndev, RAVB_BE);
  1056. ravb_ring_free(ndev, RAVB_NC);
  1057. }
  1058. /* Set new parameters */
  1059. priv->num_rx_ring[RAVB_BE] = ring->rx_pending;
  1060. priv->num_tx_ring[RAVB_BE] = ring->tx_pending;
  1061. if (netif_running(ndev)) {
  1062. error = ravb_dmac_init(ndev);
  1063. if (error) {
  1064. netdev_err(ndev,
  1065. "%s: ravb_dmac_init() failed, error %d\n",
  1066. __func__, error);
  1067. return error;
  1068. }
  1069. ravb_emac_init(ndev);
  1070. /* Initialise PTP Clock driver */
  1071. if (priv->chip_id == RCAR_GEN2)
  1072. ravb_ptp_init(ndev, priv->pdev);
  1073. netif_device_attach(ndev);
  1074. }
  1075. return 0;
  1076. }
  1077. static int ravb_get_ts_info(struct net_device *ndev,
  1078. struct ethtool_ts_info *info)
  1079. {
  1080. struct ravb_private *priv = netdev_priv(ndev);
  1081. info->so_timestamping =
  1082. SOF_TIMESTAMPING_TX_SOFTWARE |
  1083. SOF_TIMESTAMPING_RX_SOFTWARE |
  1084. SOF_TIMESTAMPING_SOFTWARE |
  1085. SOF_TIMESTAMPING_TX_HARDWARE |
  1086. SOF_TIMESTAMPING_RX_HARDWARE |
  1087. SOF_TIMESTAMPING_RAW_HARDWARE;
  1088. info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
  1089. info->rx_filters =
  1090. (1 << HWTSTAMP_FILTER_NONE) |
  1091. (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
  1092. (1 << HWTSTAMP_FILTER_ALL);
  1093. info->phc_index = ptp_clock_index(priv->ptp.clock);
  1094. return 0;
  1095. }
  1096. static void ravb_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
  1097. {
  1098. struct ravb_private *priv = netdev_priv(ndev);
  1099. wol->supported = WAKE_MAGIC;
  1100. wol->wolopts = priv->wol_enabled ? WAKE_MAGIC : 0;
  1101. }
  1102. static int ravb_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
  1103. {
  1104. struct ravb_private *priv = netdev_priv(ndev);
  1105. if (wol->wolopts & ~WAKE_MAGIC)
  1106. return -EOPNOTSUPP;
  1107. priv->wol_enabled = !!(wol->wolopts & WAKE_MAGIC);
  1108. device_set_wakeup_enable(&priv->pdev->dev, priv->wol_enabled);
  1109. return 0;
  1110. }
  1111. static const struct ethtool_ops ravb_ethtool_ops = {
  1112. .nway_reset = phy_ethtool_nway_reset,
  1113. .get_msglevel = ravb_get_msglevel,
  1114. .set_msglevel = ravb_set_msglevel,
  1115. .get_link = ethtool_op_get_link,
  1116. .get_strings = ravb_get_strings,
  1117. .get_ethtool_stats = ravb_get_ethtool_stats,
  1118. .get_sset_count = ravb_get_sset_count,
  1119. .get_ringparam = ravb_get_ringparam,
  1120. .set_ringparam = ravb_set_ringparam,
  1121. .get_ts_info = ravb_get_ts_info,
  1122. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  1123. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  1124. .get_wol = ravb_get_wol,
  1125. .set_wol = ravb_set_wol,
  1126. };
  1127. static inline int ravb_hook_irq(unsigned int irq, irq_handler_t handler,
  1128. struct net_device *ndev, struct device *dev,
  1129. const char *ch)
  1130. {
  1131. char *name;
  1132. int error;
  1133. name = devm_kasprintf(dev, GFP_KERNEL, "%s:%s", ndev->name, ch);
  1134. if (!name)
  1135. return -ENOMEM;
  1136. error = request_irq(irq, handler, 0, name, ndev);
  1137. if (error)
  1138. netdev_err(ndev, "cannot request IRQ %s\n", name);
  1139. return error;
  1140. }
  1141. /* Network device open function for Ethernet AVB */
  1142. static int ravb_open(struct net_device *ndev)
  1143. {
  1144. struct ravb_private *priv = netdev_priv(ndev);
  1145. struct platform_device *pdev = priv->pdev;
  1146. struct device *dev = &pdev->dev;
  1147. int error;
  1148. napi_enable(&priv->napi[RAVB_BE]);
  1149. napi_enable(&priv->napi[RAVB_NC]);
  1150. if (priv->chip_id == RCAR_GEN2) {
  1151. error = request_irq(ndev->irq, ravb_interrupt, IRQF_SHARED,
  1152. ndev->name, ndev);
  1153. if (error) {
  1154. netdev_err(ndev, "cannot request IRQ\n");
  1155. goto out_napi_off;
  1156. }
  1157. } else {
  1158. error = ravb_hook_irq(ndev->irq, ravb_multi_interrupt, ndev,
  1159. dev, "ch22:multi");
  1160. if (error)
  1161. goto out_napi_off;
  1162. error = ravb_hook_irq(priv->emac_irq, ravb_emac_interrupt, ndev,
  1163. dev, "ch24:emac");
  1164. if (error)
  1165. goto out_free_irq;
  1166. error = ravb_hook_irq(priv->rx_irqs[RAVB_BE], ravb_be_interrupt,
  1167. ndev, dev, "ch0:rx_be");
  1168. if (error)
  1169. goto out_free_irq_emac;
  1170. error = ravb_hook_irq(priv->tx_irqs[RAVB_BE], ravb_be_interrupt,
  1171. ndev, dev, "ch18:tx_be");
  1172. if (error)
  1173. goto out_free_irq_be_rx;
  1174. error = ravb_hook_irq(priv->rx_irqs[RAVB_NC], ravb_nc_interrupt,
  1175. ndev, dev, "ch1:rx_nc");
  1176. if (error)
  1177. goto out_free_irq_be_tx;
  1178. error = ravb_hook_irq(priv->tx_irqs[RAVB_NC], ravb_nc_interrupt,
  1179. ndev, dev, "ch19:tx_nc");
  1180. if (error)
  1181. goto out_free_irq_nc_rx;
  1182. }
  1183. /* Device init */
  1184. error = ravb_dmac_init(ndev);
  1185. if (error)
  1186. goto out_free_irq_nc_tx;
  1187. ravb_emac_init(ndev);
  1188. /* Initialise PTP Clock driver */
  1189. if (priv->chip_id == RCAR_GEN2)
  1190. ravb_ptp_init(ndev, priv->pdev);
  1191. netif_tx_start_all_queues(ndev);
  1192. /* PHY control start */
  1193. error = ravb_phy_start(ndev);
  1194. if (error)
  1195. goto out_ptp_stop;
  1196. return 0;
  1197. out_ptp_stop:
  1198. /* Stop PTP Clock driver */
  1199. if (priv->chip_id == RCAR_GEN2)
  1200. ravb_ptp_stop(ndev);
  1201. out_free_irq_nc_tx:
  1202. if (priv->chip_id == RCAR_GEN2)
  1203. goto out_free_irq;
  1204. free_irq(priv->tx_irqs[RAVB_NC], ndev);
  1205. out_free_irq_nc_rx:
  1206. free_irq(priv->rx_irqs[RAVB_NC], ndev);
  1207. out_free_irq_be_tx:
  1208. free_irq(priv->tx_irqs[RAVB_BE], ndev);
  1209. out_free_irq_be_rx:
  1210. free_irq(priv->rx_irqs[RAVB_BE], ndev);
  1211. out_free_irq_emac:
  1212. free_irq(priv->emac_irq, ndev);
  1213. out_free_irq:
  1214. free_irq(ndev->irq, ndev);
  1215. out_napi_off:
  1216. napi_disable(&priv->napi[RAVB_NC]);
  1217. napi_disable(&priv->napi[RAVB_BE]);
  1218. return error;
  1219. }
  1220. /* Timeout function for Ethernet AVB */
  1221. static void ravb_tx_timeout(struct net_device *ndev)
  1222. {
  1223. struct ravb_private *priv = netdev_priv(ndev);
  1224. netif_err(priv, tx_err, ndev,
  1225. "transmit timed out, status %08x, resetting...\n",
  1226. ravb_read(ndev, ISS));
  1227. /* tx_errors count up */
  1228. ndev->stats.tx_errors++;
  1229. schedule_work(&priv->work);
  1230. }
  1231. static void ravb_tx_timeout_work(struct work_struct *work)
  1232. {
  1233. struct ravb_private *priv = container_of(work, struct ravb_private,
  1234. work);
  1235. struct net_device *ndev = priv->ndev;
  1236. int error;
  1237. netif_tx_stop_all_queues(ndev);
  1238. /* Stop PTP Clock driver */
  1239. if (priv->chip_id == RCAR_GEN2)
  1240. ravb_ptp_stop(ndev);
  1241. /* Wait for DMA stopping */
  1242. if (ravb_stop_dma(ndev)) {
  1243. /* If ravb_stop_dma() fails, the hardware is still operating
  1244. * for TX and/or RX. So, this should not call the following
  1245. * functions because ravb_dmac_init() is possible to fail too.
  1246. * Also, this should not retry ravb_stop_dma() again and again
  1247. * here because it's possible to wait forever. So, this just
  1248. * re-enables the TX and RX and skip the following
  1249. * re-initialization procedure.
  1250. */
  1251. ravb_rcv_snd_enable(ndev);
  1252. goto out;
  1253. }
  1254. ravb_ring_free(ndev, RAVB_BE);
  1255. ravb_ring_free(ndev, RAVB_NC);
  1256. /* Device init */
  1257. error = ravb_dmac_init(ndev);
  1258. if (error) {
  1259. /* If ravb_dmac_init() fails, descriptors are freed. So, this
  1260. * should return here to avoid re-enabling the TX and RX in
  1261. * ravb_emac_init().
  1262. */
  1263. netdev_err(ndev, "%s: ravb_dmac_init() failed, error %d\n",
  1264. __func__, error);
  1265. return;
  1266. }
  1267. ravb_emac_init(ndev);
  1268. out:
  1269. /* Initialise PTP Clock driver */
  1270. if (priv->chip_id == RCAR_GEN2)
  1271. ravb_ptp_init(ndev, priv->pdev);
  1272. netif_tx_start_all_queues(ndev);
  1273. }
  1274. /* Packet transmit function for Ethernet AVB */
  1275. static netdev_tx_t ravb_start_xmit(struct sk_buff *skb, struct net_device *ndev)
  1276. {
  1277. struct ravb_private *priv = netdev_priv(ndev);
  1278. u16 q = skb_get_queue_mapping(skb);
  1279. struct ravb_tstamp_skb *ts_skb;
  1280. struct ravb_tx_desc *desc;
  1281. unsigned long flags;
  1282. u32 dma_addr;
  1283. void *buffer;
  1284. u32 entry;
  1285. u32 len;
  1286. spin_lock_irqsave(&priv->lock, flags);
  1287. if (priv->cur_tx[q] - priv->dirty_tx[q] > (priv->num_tx_ring[q] - 1) *
  1288. NUM_TX_DESC) {
  1289. netif_err(priv, tx_queued, ndev,
  1290. "still transmitting with the full ring!\n");
  1291. netif_stop_subqueue(ndev, q);
  1292. spin_unlock_irqrestore(&priv->lock, flags);
  1293. return NETDEV_TX_BUSY;
  1294. }
  1295. if (skb_put_padto(skb, ETH_ZLEN))
  1296. goto exit;
  1297. entry = priv->cur_tx[q] % (priv->num_tx_ring[q] * NUM_TX_DESC);
  1298. priv->tx_skb[q][entry / NUM_TX_DESC] = skb;
  1299. buffer = PTR_ALIGN(priv->tx_align[q], DPTR_ALIGN) +
  1300. entry / NUM_TX_DESC * DPTR_ALIGN;
  1301. len = PTR_ALIGN(skb->data, DPTR_ALIGN) - skb->data;
  1302. /* Zero length DMA descriptors are problematic as they seem to
  1303. * terminate DMA transfers. Avoid them by simply using a length of
  1304. * DPTR_ALIGN (4) when skb data is aligned to DPTR_ALIGN.
  1305. *
  1306. * As skb is guaranteed to have at least ETH_ZLEN (60) bytes of
  1307. * data by the call to skb_put_padto() above this is safe with
  1308. * respect to both the length of the first DMA descriptor (len)
  1309. * overflowing the available data and the length of the second DMA
  1310. * descriptor (skb->len - len) being negative.
  1311. */
  1312. if (len == 0)
  1313. len = DPTR_ALIGN;
  1314. memcpy(buffer, skb->data, len);
  1315. dma_addr = dma_map_single(ndev->dev.parent, buffer, len, DMA_TO_DEVICE);
  1316. if (dma_mapping_error(ndev->dev.parent, dma_addr))
  1317. goto drop;
  1318. desc = &priv->tx_ring[q][entry];
  1319. desc->ds_tagl = cpu_to_le16(len);
  1320. desc->dptr = cpu_to_le32(dma_addr);
  1321. buffer = skb->data + len;
  1322. len = skb->len - len;
  1323. dma_addr = dma_map_single(ndev->dev.parent, buffer, len, DMA_TO_DEVICE);
  1324. if (dma_mapping_error(ndev->dev.parent, dma_addr))
  1325. goto unmap;
  1326. desc++;
  1327. desc->ds_tagl = cpu_to_le16(len);
  1328. desc->dptr = cpu_to_le32(dma_addr);
  1329. /* TX timestamp required */
  1330. if (q == RAVB_NC) {
  1331. ts_skb = kmalloc(sizeof(*ts_skb), GFP_ATOMIC);
  1332. if (!ts_skb) {
  1333. desc--;
  1334. dma_unmap_single(ndev->dev.parent, dma_addr, len,
  1335. DMA_TO_DEVICE);
  1336. goto unmap;
  1337. }
  1338. ts_skb->skb = skb_get(skb);
  1339. ts_skb->tag = priv->ts_skb_tag++;
  1340. priv->ts_skb_tag &= 0x3ff;
  1341. list_add_tail(&ts_skb->list, &priv->ts_skb_list);
  1342. /* TAG and timestamp required flag */
  1343. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  1344. desc->tagh_tsr = (ts_skb->tag >> 4) | TX_TSR;
  1345. desc->ds_tagl |= cpu_to_le16(ts_skb->tag << 12);
  1346. }
  1347. skb_tx_timestamp(skb);
  1348. /* Descriptor type must be set after all the above writes */
  1349. dma_wmb();
  1350. desc->die_dt = DT_FEND;
  1351. desc--;
  1352. desc->die_dt = DT_FSTART;
  1353. ravb_modify(ndev, TCCR, TCCR_TSRQ0 << q, TCCR_TSRQ0 << q);
  1354. priv->cur_tx[q] += NUM_TX_DESC;
  1355. if (priv->cur_tx[q] - priv->dirty_tx[q] >
  1356. (priv->num_tx_ring[q] - 1) * NUM_TX_DESC &&
  1357. !ravb_tx_free(ndev, q, true))
  1358. netif_stop_subqueue(ndev, q);
  1359. exit:
  1360. mmiowb();
  1361. spin_unlock_irqrestore(&priv->lock, flags);
  1362. return NETDEV_TX_OK;
  1363. unmap:
  1364. dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr),
  1365. le16_to_cpu(desc->ds_tagl), DMA_TO_DEVICE);
  1366. drop:
  1367. dev_kfree_skb_any(skb);
  1368. priv->tx_skb[q][entry / NUM_TX_DESC] = NULL;
  1369. goto exit;
  1370. }
  1371. static u16 ravb_select_queue(struct net_device *ndev, struct sk_buff *skb,
  1372. struct net_device *sb_dev,
  1373. select_queue_fallback_t fallback)
  1374. {
  1375. /* If skb needs TX timestamp, it is handled in network control queue */
  1376. return (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) ? RAVB_NC :
  1377. RAVB_BE;
  1378. }
  1379. static struct net_device_stats *ravb_get_stats(struct net_device *ndev)
  1380. {
  1381. struct ravb_private *priv = netdev_priv(ndev);
  1382. struct net_device_stats *nstats, *stats0, *stats1;
  1383. nstats = &ndev->stats;
  1384. stats0 = &priv->stats[RAVB_BE];
  1385. stats1 = &priv->stats[RAVB_NC];
  1386. nstats->tx_dropped += ravb_read(ndev, TROCR);
  1387. ravb_write(ndev, 0, TROCR); /* (write clear) */
  1388. nstats->collisions += ravb_read(ndev, CDCR);
  1389. ravb_write(ndev, 0, CDCR); /* (write clear) */
  1390. nstats->tx_carrier_errors += ravb_read(ndev, LCCR);
  1391. ravb_write(ndev, 0, LCCR); /* (write clear) */
  1392. nstats->tx_carrier_errors += ravb_read(ndev, CERCR);
  1393. ravb_write(ndev, 0, CERCR); /* (write clear) */
  1394. nstats->tx_carrier_errors += ravb_read(ndev, CEECR);
  1395. ravb_write(ndev, 0, CEECR); /* (write clear) */
  1396. nstats->rx_packets = stats0->rx_packets + stats1->rx_packets;
  1397. nstats->tx_packets = stats0->tx_packets + stats1->tx_packets;
  1398. nstats->rx_bytes = stats0->rx_bytes + stats1->rx_bytes;
  1399. nstats->tx_bytes = stats0->tx_bytes + stats1->tx_bytes;
  1400. nstats->multicast = stats0->multicast + stats1->multicast;
  1401. nstats->rx_errors = stats0->rx_errors + stats1->rx_errors;
  1402. nstats->rx_crc_errors = stats0->rx_crc_errors + stats1->rx_crc_errors;
  1403. nstats->rx_frame_errors =
  1404. stats0->rx_frame_errors + stats1->rx_frame_errors;
  1405. nstats->rx_length_errors =
  1406. stats0->rx_length_errors + stats1->rx_length_errors;
  1407. nstats->rx_missed_errors =
  1408. stats0->rx_missed_errors + stats1->rx_missed_errors;
  1409. nstats->rx_over_errors =
  1410. stats0->rx_over_errors + stats1->rx_over_errors;
  1411. return nstats;
  1412. }
  1413. /* Update promiscuous bit */
  1414. static void ravb_set_rx_mode(struct net_device *ndev)
  1415. {
  1416. struct ravb_private *priv = netdev_priv(ndev);
  1417. unsigned long flags;
  1418. spin_lock_irqsave(&priv->lock, flags);
  1419. ravb_modify(ndev, ECMR, ECMR_PRM,
  1420. ndev->flags & IFF_PROMISC ? ECMR_PRM : 0);
  1421. mmiowb();
  1422. spin_unlock_irqrestore(&priv->lock, flags);
  1423. }
  1424. /* Device close function for Ethernet AVB */
  1425. static int ravb_close(struct net_device *ndev)
  1426. {
  1427. struct device_node *np = ndev->dev.parent->of_node;
  1428. struct ravb_private *priv = netdev_priv(ndev);
  1429. struct ravb_tstamp_skb *ts_skb, *ts_skb2;
  1430. netif_tx_stop_all_queues(ndev);
  1431. /* Disable interrupts by clearing the interrupt masks. */
  1432. ravb_write(ndev, 0, RIC0);
  1433. ravb_write(ndev, 0, RIC2);
  1434. ravb_write(ndev, 0, TIC);
  1435. /* Stop PTP Clock driver */
  1436. if (priv->chip_id == RCAR_GEN2)
  1437. ravb_ptp_stop(ndev);
  1438. /* Set the config mode to stop the AVB-DMAC's processes */
  1439. if (ravb_stop_dma(ndev) < 0)
  1440. netdev_err(ndev,
  1441. "device will be stopped after h/w processes are done.\n");
  1442. /* Clear the timestamp list */
  1443. list_for_each_entry_safe(ts_skb, ts_skb2, &priv->ts_skb_list, list) {
  1444. list_del(&ts_skb->list);
  1445. kfree_skb(ts_skb->skb);
  1446. kfree(ts_skb);
  1447. }
  1448. /* PHY disconnect */
  1449. if (ndev->phydev) {
  1450. phy_stop(ndev->phydev);
  1451. phy_disconnect(ndev->phydev);
  1452. if (of_phy_is_fixed_link(np))
  1453. of_phy_deregister_fixed_link(np);
  1454. }
  1455. if (priv->chip_id != RCAR_GEN2) {
  1456. free_irq(priv->tx_irqs[RAVB_NC], ndev);
  1457. free_irq(priv->rx_irqs[RAVB_NC], ndev);
  1458. free_irq(priv->tx_irqs[RAVB_BE], ndev);
  1459. free_irq(priv->rx_irqs[RAVB_BE], ndev);
  1460. free_irq(priv->emac_irq, ndev);
  1461. }
  1462. free_irq(ndev->irq, ndev);
  1463. napi_disable(&priv->napi[RAVB_NC]);
  1464. napi_disable(&priv->napi[RAVB_BE]);
  1465. /* Free all the skb's in the RX queue and the DMA buffers. */
  1466. ravb_ring_free(ndev, RAVB_BE);
  1467. ravb_ring_free(ndev, RAVB_NC);
  1468. return 0;
  1469. }
  1470. static int ravb_hwtstamp_get(struct net_device *ndev, struct ifreq *req)
  1471. {
  1472. struct ravb_private *priv = netdev_priv(ndev);
  1473. struct hwtstamp_config config;
  1474. config.flags = 0;
  1475. config.tx_type = priv->tstamp_tx_ctrl ? HWTSTAMP_TX_ON :
  1476. HWTSTAMP_TX_OFF;
  1477. switch (priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE) {
  1478. case RAVB_RXTSTAMP_TYPE_V2_L2_EVENT:
  1479. config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
  1480. break;
  1481. case RAVB_RXTSTAMP_TYPE_ALL:
  1482. config.rx_filter = HWTSTAMP_FILTER_ALL;
  1483. break;
  1484. default:
  1485. config.rx_filter = HWTSTAMP_FILTER_NONE;
  1486. }
  1487. return copy_to_user(req->ifr_data, &config, sizeof(config)) ?
  1488. -EFAULT : 0;
  1489. }
  1490. /* Control hardware time stamping */
  1491. static int ravb_hwtstamp_set(struct net_device *ndev, struct ifreq *req)
  1492. {
  1493. struct ravb_private *priv = netdev_priv(ndev);
  1494. struct hwtstamp_config config;
  1495. u32 tstamp_rx_ctrl = RAVB_RXTSTAMP_ENABLED;
  1496. u32 tstamp_tx_ctrl;
  1497. if (copy_from_user(&config, req->ifr_data, sizeof(config)))
  1498. return -EFAULT;
  1499. /* Reserved for future extensions */
  1500. if (config.flags)
  1501. return -EINVAL;
  1502. switch (config.tx_type) {
  1503. case HWTSTAMP_TX_OFF:
  1504. tstamp_tx_ctrl = 0;
  1505. break;
  1506. case HWTSTAMP_TX_ON:
  1507. tstamp_tx_ctrl = RAVB_TXTSTAMP_ENABLED;
  1508. break;
  1509. default:
  1510. return -ERANGE;
  1511. }
  1512. switch (config.rx_filter) {
  1513. case HWTSTAMP_FILTER_NONE:
  1514. tstamp_rx_ctrl = 0;
  1515. break;
  1516. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  1517. tstamp_rx_ctrl |= RAVB_RXTSTAMP_TYPE_V2_L2_EVENT;
  1518. break;
  1519. default:
  1520. config.rx_filter = HWTSTAMP_FILTER_ALL;
  1521. tstamp_rx_ctrl |= RAVB_RXTSTAMP_TYPE_ALL;
  1522. }
  1523. priv->tstamp_tx_ctrl = tstamp_tx_ctrl;
  1524. priv->tstamp_rx_ctrl = tstamp_rx_ctrl;
  1525. return copy_to_user(req->ifr_data, &config, sizeof(config)) ?
  1526. -EFAULT : 0;
  1527. }
  1528. /* ioctl to device function */
  1529. static int ravb_do_ioctl(struct net_device *ndev, struct ifreq *req, int cmd)
  1530. {
  1531. struct phy_device *phydev = ndev->phydev;
  1532. if (!netif_running(ndev))
  1533. return -EINVAL;
  1534. if (!phydev)
  1535. return -ENODEV;
  1536. switch (cmd) {
  1537. case SIOCGHWTSTAMP:
  1538. return ravb_hwtstamp_get(ndev, req);
  1539. case SIOCSHWTSTAMP:
  1540. return ravb_hwtstamp_set(ndev, req);
  1541. }
  1542. return phy_mii_ioctl(phydev, req, cmd);
  1543. }
  1544. static int ravb_change_mtu(struct net_device *ndev, int new_mtu)
  1545. {
  1546. if (netif_running(ndev))
  1547. return -EBUSY;
  1548. ndev->mtu = new_mtu;
  1549. netdev_update_features(ndev);
  1550. return 0;
  1551. }
  1552. static void ravb_set_rx_csum(struct net_device *ndev, bool enable)
  1553. {
  1554. struct ravb_private *priv = netdev_priv(ndev);
  1555. unsigned long flags;
  1556. spin_lock_irqsave(&priv->lock, flags);
  1557. /* Disable TX and RX */
  1558. ravb_rcv_snd_disable(ndev);
  1559. /* Modify RX Checksum setting */
  1560. ravb_modify(ndev, ECMR, ECMR_RCSC, enable ? ECMR_RCSC : 0);
  1561. /* Enable TX and RX */
  1562. ravb_rcv_snd_enable(ndev);
  1563. spin_unlock_irqrestore(&priv->lock, flags);
  1564. }
  1565. static int ravb_set_features(struct net_device *ndev,
  1566. netdev_features_t features)
  1567. {
  1568. netdev_features_t changed = ndev->features ^ features;
  1569. if (changed & NETIF_F_RXCSUM)
  1570. ravb_set_rx_csum(ndev, features & NETIF_F_RXCSUM);
  1571. ndev->features = features;
  1572. return 0;
  1573. }
  1574. static const struct net_device_ops ravb_netdev_ops = {
  1575. .ndo_open = ravb_open,
  1576. .ndo_stop = ravb_close,
  1577. .ndo_start_xmit = ravb_start_xmit,
  1578. .ndo_select_queue = ravb_select_queue,
  1579. .ndo_get_stats = ravb_get_stats,
  1580. .ndo_set_rx_mode = ravb_set_rx_mode,
  1581. .ndo_tx_timeout = ravb_tx_timeout,
  1582. .ndo_do_ioctl = ravb_do_ioctl,
  1583. .ndo_change_mtu = ravb_change_mtu,
  1584. .ndo_validate_addr = eth_validate_addr,
  1585. .ndo_set_mac_address = eth_mac_addr,
  1586. .ndo_set_features = ravb_set_features,
  1587. };
  1588. /* MDIO bus init function */
  1589. static int ravb_mdio_init(struct ravb_private *priv)
  1590. {
  1591. struct platform_device *pdev = priv->pdev;
  1592. struct device *dev = &pdev->dev;
  1593. int error;
  1594. /* Bitbang init */
  1595. priv->mdiobb.ops = &bb_ops;
  1596. /* MII controller setting */
  1597. priv->mii_bus = alloc_mdio_bitbang(&priv->mdiobb);
  1598. if (!priv->mii_bus)
  1599. return -ENOMEM;
  1600. /* Hook up MII support for ethtool */
  1601. priv->mii_bus->name = "ravb_mii";
  1602. priv->mii_bus->parent = dev;
  1603. snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
  1604. pdev->name, pdev->id);
  1605. /* Register MDIO bus */
  1606. error = of_mdiobus_register(priv->mii_bus, dev->of_node);
  1607. if (error)
  1608. goto out_free_bus;
  1609. return 0;
  1610. out_free_bus:
  1611. free_mdio_bitbang(priv->mii_bus);
  1612. return error;
  1613. }
  1614. /* MDIO bus release function */
  1615. static int ravb_mdio_release(struct ravb_private *priv)
  1616. {
  1617. /* Unregister mdio bus */
  1618. mdiobus_unregister(priv->mii_bus);
  1619. /* Free bitbang info */
  1620. free_mdio_bitbang(priv->mii_bus);
  1621. return 0;
  1622. }
  1623. static const struct of_device_id ravb_match_table[] = {
  1624. { .compatible = "renesas,etheravb-r8a7790", .data = (void *)RCAR_GEN2 },
  1625. { .compatible = "renesas,etheravb-r8a7794", .data = (void *)RCAR_GEN2 },
  1626. { .compatible = "renesas,etheravb-rcar-gen2", .data = (void *)RCAR_GEN2 },
  1627. { .compatible = "renesas,etheravb-r8a7795", .data = (void *)RCAR_GEN3 },
  1628. { .compatible = "renesas,etheravb-rcar-gen3", .data = (void *)RCAR_GEN3 },
  1629. { }
  1630. };
  1631. MODULE_DEVICE_TABLE(of, ravb_match_table);
  1632. static int ravb_set_gti(struct net_device *ndev)
  1633. {
  1634. struct ravb_private *priv = netdev_priv(ndev);
  1635. struct device *dev = ndev->dev.parent;
  1636. unsigned long rate;
  1637. uint64_t inc;
  1638. rate = clk_get_rate(priv->clk);
  1639. if (!rate)
  1640. return -EINVAL;
  1641. inc = 1000000000ULL << 20;
  1642. do_div(inc, rate);
  1643. if (inc < GTI_TIV_MIN || inc > GTI_TIV_MAX) {
  1644. dev_err(dev, "gti.tiv increment 0x%llx is outside the range 0x%x - 0x%x\n",
  1645. inc, GTI_TIV_MIN, GTI_TIV_MAX);
  1646. return -EINVAL;
  1647. }
  1648. ravb_write(ndev, inc, GTI);
  1649. return 0;
  1650. }
  1651. static void ravb_set_config_mode(struct net_device *ndev)
  1652. {
  1653. struct ravb_private *priv = netdev_priv(ndev);
  1654. if (priv->chip_id == RCAR_GEN2) {
  1655. ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_CONFIG);
  1656. /* Set CSEL value */
  1657. ravb_modify(ndev, CCC, CCC_CSEL, CCC_CSEL_HPB);
  1658. } else {
  1659. ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_CONFIG |
  1660. CCC_GAC | CCC_CSEL_HPB);
  1661. }
  1662. }
  1663. /* Set tx and rx clock internal delay modes */
  1664. static void ravb_set_delay_mode(struct net_device *ndev)
  1665. {
  1666. struct ravb_private *priv = netdev_priv(ndev);
  1667. int set = 0;
  1668. if (priv->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
  1669. priv->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID)
  1670. set |= APSR_DM_RDM;
  1671. if (priv->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
  1672. priv->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID)
  1673. set |= APSR_DM_TDM;
  1674. ravb_modify(ndev, APSR, APSR_DM, set);
  1675. }
  1676. static int ravb_probe(struct platform_device *pdev)
  1677. {
  1678. struct device_node *np = pdev->dev.of_node;
  1679. struct ravb_private *priv;
  1680. enum ravb_chip_id chip_id;
  1681. struct net_device *ndev;
  1682. int error, irq, q;
  1683. struct resource *res;
  1684. int i;
  1685. if (!np) {
  1686. dev_err(&pdev->dev,
  1687. "this driver is required to be instantiated from device tree\n");
  1688. return -EINVAL;
  1689. }
  1690. /* Get base address */
  1691. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1692. if (!res) {
  1693. dev_err(&pdev->dev, "invalid resource\n");
  1694. return -EINVAL;
  1695. }
  1696. ndev = alloc_etherdev_mqs(sizeof(struct ravb_private),
  1697. NUM_TX_QUEUE, NUM_RX_QUEUE);
  1698. if (!ndev)
  1699. return -ENOMEM;
  1700. ndev->features = NETIF_F_RXCSUM;
  1701. ndev->hw_features = NETIF_F_RXCSUM;
  1702. pm_runtime_enable(&pdev->dev);
  1703. pm_runtime_get_sync(&pdev->dev);
  1704. /* The Ether-specific entries in the device structure. */
  1705. ndev->base_addr = res->start;
  1706. chip_id = (enum ravb_chip_id)of_device_get_match_data(&pdev->dev);
  1707. if (chip_id == RCAR_GEN3)
  1708. irq = platform_get_irq_byname(pdev, "ch22");
  1709. else
  1710. irq = platform_get_irq(pdev, 0);
  1711. if (irq < 0) {
  1712. error = irq;
  1713. goto out_release;
  1714. }
  1715. ndev->irq = irq;
  1716. SET_NETDEV_DEV(ndev, &pdev->dev);
  1717. priv = netdev_priv(ndev);
  1718. priv->ndev = ndev;
  1719. priv->pdev = pdev;
  1720. priv->num_tx_ring[RAVB_BE] = BE_TX_RING_SIZE;
  1721. priv->num_rx_ring[RAVB_BE] = BE_RX_RING_SIZE;
  1722. priv->num_tx_ring[RAVB_NC] = NC_TX_RING_SIZE;
  1723. priv->num_rx_ring[RAVB_NC] = NC_RX_RING_SIZE;
  1724. priv->addr = devm_ioremap_resource(&pdev->dev, res);
  1725. if (IS_ERR(priv->addr)) {
  1726. error = PTR_ERR(priv->addr);
  1727. goto out_release;
  1728. }
  1729. spin_lock_init(&priv->lock);
  1730. INIT_WORK(&priv->work, ravb_tx_timeout_work);
  1731. priv->phy_interface = of_get_phy_mode(np);
  1732. priv->no_avb_link = of_property_read_bool(np, "renesas,no-ether-link");
  1733. priv->avb_link_active_low =
  1734. of_property_read_bool(np, "renesas,ether-link-active-low");
  1735. if (chip_id == RCAR_GEN3) {
  1736. irq = platform_get_irq_byname(pdev, "ch24");
  1737. if (irq < 0) {
  1738. error = irq;
  1739. goto out_release;
  1740. }
  1741. priv->emac_irq = irq;
  1742. for (i = 0; i < NUM_RX_QUEUE; i++) {
  1743. irq = platform_get_irq_byname(pdev, ravb_rx_irqs[i]);
  1744. if (irq < 0) {
  1745. error = irq;
  1746. goto out_release;
  1747. }
  1748. priv->rx_irqs[i] = irq;
  1749. }
  1750. for (i = 0; i < NUM_TX_QUEUE; i++) {
  1751. irq = platform_get_irq_byname(pdev, ravb_tx_irqs[i]);
  1752. if (irq < 0) {
  1753. error = irq;
  1754. goto out_release;
  1755. }
  1756. priv->tx_irqs[i] = irq;
  1757. }
  1758. }
  1759. priv->chip_id = chip_id;
  1760. priv->clk = devm_clk_get(&pdev->dev, NULL);
  1761. if (IS_ERR(priv->clk)) {
  1762. error = PTR_ERR(priv->clk);
  1763. goto out_release;
  1764. }
  1765. ndev->max_mtu = 2048 - (ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN);
  1766. ndev->min_mtu = ETH_MIN_MTU;
  1767. /* Set function */
  1768. ndev->netdev_ops = &ravb_netdev_ops;
  1769. ndev->ethtool_ops = &ravb_ethtool_ops;
  1770. /* Set AVB config mode */
  1771. ravb_set_config_mode(ndev);
  1772. /* Set GTI value */
  1773. error = ravb_set_gti(ndev);
  1774. if (error)
  1775. goto out_release;
  1776. /* Request GTI loading */
  1777. ravb_modify(ndev, GCCR, GCCR_LTI, GCCR_LTI);
  1778. if (priv->chip_id != RCAR_GEN2)
  1779. ravb_set_delay_mode(ndev);
  1780. /* Allocate descriptor base address table */
  1781. priv->desc_bat_size = sizeof(struct ravb_desc) * DBAT_ENTRY_NUM;
  1782. priv->desc_bat = dma_alloc_coherent(ndev->dev.parent, priv->desc_bat_size,
  1783. &priv->desc_bat_dma, GFP_KERNEL);
  1784. if (!priv->desc_bat) {
  1785. dev_err(&pdev->dev,
  1786. "Cannot allocate desc base address table (size %d bytes)\n",
  1787. priv->desc_bat_size);
  1788. error = -ENOMEM;
  1789. goto out_release;
  1790. }
  1791. for (q = RAVB_BE; q < DBAT_ENTRY_NUM; q++)
  1792. priv->desc_bat[q].die_dt = DT_EOS;
  1793. ravb_write(ndev, priv->desc_bat_dma, DBAT);
  1794. /* Initialise HW timestamp list */
  1795. INIT_LIST_HEAD(&priv->ts_skb_list);
  1796. /* Initialise PTP Clock driver */
  1797. if (chip_id != RCAR_GEN2)
  1798. ravb_ptp_init(ndev, pdev);
  1799. /* Debug message level */
  1800. priv->msg_enable = RAVB_DEF_MSG_ENABLE;
  1801. /* Read and set MAC address */
  1802. ravb_read_mac_address(ndev, of_get_mac_address(np));
  1803. if (!is_valid_ether_addr(ndev->dev_addr)) {
  1804. dev_warn(&pdev->dev,
  1805. "no valid MAC address supplied, using a random one\n");
  1806. eth_hw_addr_random(ndev);
  1807. }
  1808. /* MDIO bus init */
  1809. error = ravb_mdio_init(priv);
  1810. if (error) {
  1811. dev_err(&pdev->dev, "failed to initialize MDIO\n");
  1812. goto out_dma_free;
  1813. }
  1814. netif_napi_add(ndev, &priv->napi[RAVB_BE], ravb_poll, 64);
  1815. netif_napi_add(ndev, &priv->napi[RAVB_NC], ravb_poll, 64);
  1816. /* Network device register */
  1817. error = register_netdev(ndev);
  1818. if (error)
  1819. goto out_napi_del;
  1820. device_set_wakeup_capable(&pdev->dev, 1);
  1821. /* Print device information */
  1822. netdev_info(ndev, "Base address at %#x, %pM, IRQ %d.\n",
  1823. (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
  1824. platform_set_drvdata(pdev, ndev);
  1825. return 0;
  1826. out_napi_del:
  1827. netif_napi_del(&priv->napi[RAVB_NC]);
  1828. netif_napi_del(&priv->napi[RAVB_BE]);
  1829. ravb_mdio_release(priv);
  1830. out_dma_free:
  1831. dma_free_coherent(ndev->dev.parent, priv->desc_bat_size, priv->desc_bat,
  1832. priv->desc_bat_dma);
  1833. /* Stop PTP Clock driver */
  1834. if (chip_id != RCAR_GEN2)
  1835. ravb_ptp_stop(ndev);
  1836. out_release:
  1837. free_netdev(ndev);
  1838. pm_runtime_put(&pdev->dev);
  1839. pm_runtime_disable(&pdev->dev);
  1840. return error;
  1841. }
  1842. static int ravb_remove(struct platform_device *pdev)
  1843. {
  1844. struct net_device *ndev = platform_get_drvdata(pdev);
  1845. struct ravb_private *priv = netdev_priv(ndev);
  1846. /* Stop PTP Clock driver */
  1847. if (priv->chip_id != RCAR_GEN2)
  1848. ravb_ptp_stop(ndev);
  1849. dma_free_coherent(ndev->dev.parent, priv->desc_bat_size, priv->desc_bat,
  1850. priv->desc_bat_dma);
  1851. /* Set reset mode */
  1852. ravb_write(ndev, CCC_OPC_RESET, CCC);
  1853. pm_runtime_put_sync(&pdev->dev);
  1854. unregister_netdev(ndev);
  1855. netif_napi_del(&priv->napi[RAVB_NC]);
  1856. netif_napi_del(&priv->napi[RAVB_BE]);
  1857. ravb_mdio_release(priv);
  1858. pm_runtime_disable(&pdev->dev);
  1859. free_netdev(ndev);
  1860. platform_set_drvdata(pdev, NULL);
  1861. return 0;
  1862. }
  1863. static int ravb_wol_setup(struct net_device *ndev)
  1864. {
  1865. struct ravb_private *priv = netdev_priv(ndev);
  1866. /* Disable interrupts by clearing the interrupt masks. */
  1867. ravb_write(ndev, 0, RIC0);
  1868. ravb_write(ndev, 0, RIC2);
  1869. ravb_write(ndev, 0, TIC);
  1870. /* Only allow ECI interrupts */
  1871. synchronize_irq(priv->emac_irq);
  1872. napi_disable(&priv->napi[RAVB_NC]);
  1873. napi_disable(&priv->napi[RAVB_BE]);
  1874. ravb_write(ndev, ECSIPR_MPDIP, ECSIPR);
  1875. /* Enable MagicPacket */
  1876. ravb_modify(ndev, ECMR, ECMR_MPDE, ECMR_MPDE);
  1877. return enable_irq_wake(priv->emac_irq);
  1878. }
  1879. static int ravb_wol_restore(struct net_device *ndev)
  1880. {
  1881. struct ravb_private *priv = netdev_priv(ndev);
  1882. int ret;
  1883. napi_enable(&priv->napi[RAVB_NC]);
  1884. napi_enable(&priv->napi[RAVB_BE]);
  1885. /* Disable MagicPacket */
  1886. ravb_modify(ndev, ECMR, ECMR_MPDE, 0);
  1887. ret = ravb_close(ndev);
  1888. if (ret < 0)
  1889. return ret;
  1890. return disable_irq_wake(priv->emac_irq);
  1891. }
  1892. static int __maybe_unused ravb_suspend(struct device *dev)
  1893. {
  1894. struct net_device *ndev = dev_get_drvdata(dev);
  1895. struct ravb_private *priv = netdev_priv(ndev);
  1896. int ret;
  1897. if (!netif_running(ndev))
  1898. return 0;
  1899. netif_device_detach(ndev);
  1900. if (priv->wol_enabled)
  1901. ret = ravb_wol_setup(ndev);
  1902. else
  1903. ret = ravb_close(ndev);
  1904. return ret;
  1905. }
  1906. static int __maybe_unused ravb_resume(struct device *dev)
  1907. {
  1908. struct net_device *ndev = dev_get_drvdata(dev);
  1909. struct ravb_private *priv = netdev_priv(ndev);
  1910. int ret = 0;
  1911. /* If WoL is enabled set reset mode to rearm the WoL logic */
  1912. if (priv->wol_enabled)
  1913. ravb_write(ndev, CCC_OPC_RESET, CCC);
  1914. /* All register have been reset to default values.
  1915. * Restore all registers which where setup at probe time and
  1916. * reopen device if it was running before system suspended.
  1917. */
  1918. /* Set AVB config mode */
  1919. ravb_set_config_mode(ndev);
  1920. /* Set GTI value */
  1921. ret = ravb_set_gti(ndev);
  1922. if (ret)
  1923. return ret;
  1924. /* Request GTI loading */
  1925. ravb_modify(ndev, GCCR, GCCR_LTI, GCCR_LTI);
  1926. if (priv->chip_id != RCAR_GEN2)
  1927. ravb_set_delay_mode(ndev);
  1928. /* Restore descriptor base address table */
  1929. ravb_write(ndev, priv->desc_bat_dma, DBAT);
  1930. if (netif_running(ndev)) {
  1931. if (priv->wol_enabled) {
  1932. ret = ravb_wol_restore(ndev);
  1933. if (ret)
  1934. return ret;
  1935. }
  1936. ret = ravb_open(ndev);
  1937. if (ret < 0)
  1938. return ret;
  1939. netif_device_attach(ndev);
  1940. }
  1941. return ret;
  1942. }
  1943. static int __maybe_unused ravb_runtime_nop(struct device *dev)
  1944. {
  1945. /* Runtime PM callback shared between ->runtime_suspend()
  1946. * and ->runtime_resume(). Simply returns success.
  1947. *
  1948. * This driver re-initializes all registers after
  1949. * pm_runtime_get_sync() anyway so there is no need
  1950. * to save and restore registers here.
  1951. */
  1952. return 0;
  1953. }
  1954. static const struct dev_pm_ops ravb_dev_pm_ops = {
  1955. SET_SYSTEM_SLEEP_PM_OPS(ravb_suspend, ravb_resume)
  1956. SET_RUNTIME_PM_OPS(ravb_runtime_nop, ravb_runtime_nop, NULL)
  1957. };
  1958. static struct platform_driver ravb_driver = {
  1959. .probe = ravb_probe,
  1960. .remove = ravb_remove,
  1961. .driver = {
  1962. .name = "ravb",
  1963. .pm = &ravb_dev_pm_ops,
  1964. .of_match_table = ravb_match_table,
  1965. },
  1966. };
  1967. module_platform_driver(ravb_driver);
  1968. MODULE_AUTHOR("Mitsuhiro Kimura, Masaru Nagai");
  1969. MODULE_DESCRIPTION("Renesas Ethernet AVB driver");
  1970. MODULE_LICENSE("GPL v2");