ravb_main.c 59 KB

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