ravb_main.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325
  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. netif_tx_stop_all_queues(ndev);
  1237. /* Stop PTP Clock driver */
  1238. if (priv->chip_id == RCAR_GEN2)
  1239. ravb_ptp_stop(ndev);
  1240. /* Wait for DMA stopping */
  1241. ravb_stop_dma(ndev);
  1242. ravb_ring_free(ndev, RAVB_BE);
  1243. ravb_ring_free(ndev, RAVB_NC);
  1244. /* Device init */
  1245. ravb_dmac_init(ndev);
  1246. ravb_emac_init(ndev);
  1247. /* Initialise PTP Clock driver */
  1248. if (priv->chip_id == RCAR_GEN2)
  1249. ravb_ptp_init(ndev, priv->pdev);
  1250. netif_tx_start_all_queues(ndev);
  1251. }
  1252. /* Packet transmit function for Ethernet AVB */
  1253. static netdev_tx_t ravb_start_xmit(struct sk_buff *skb, struct net_device *ndev)
  1254. {
  1255. struct ravb_private *priv = netdev_priv(ndev);
  1256. u16 q = skb_get_queue_mapping(skb);
  1257. struct ravb_tstamp_skb *ts_skb;
  1258. struct ravb_tx_desc *desc;
  1259. unsigned long flags;
  1260. u32 dma_addr;
  1261. void *buffer;
  1262. u32 entry;
  1263. u32 len;
  1264. spin_lock_irqsave(&priv->lock, flags);
  1265. if (priv->cur_tx[q] - priv->dirty_tx[q] > (priv->num_tx_ring[q] - 1) *
  1266. NUM_TX_DESC) {
  1267. netif_err(priv, tx_queued, ndev,
  1268. "still transmitting with the full ring!\n");
  1269. netif_stop_subqueue(ndev, q);
  1270. spin_unlock_irqrestore(&priv->lock, flags);
  1271. return NETDEV_TX_BUSY;
  1272. }
  1273. if (skb_put_padto(skb, ETH_ZLEN))
  1274. goto exit;
  1275. entry = priv->cur_tx[q] % (priv->num_tx_ring[q] * NUM_TX_DESC);
  1276. priv->tx_skb[q][entry / NUM_TX_DESC] = skb;
  1277. buffer = PTR_ALIGN(priv->tx_align[q], DPTR_ALIGN) +
  1278. entry / NUM_TX_DESC * DPTR_ALIGN;
  1279. len = PTR_ALIGN(skb->data, DPTR_ALIGN) - skb->data;
  1280. /* Zero length DMA descriptors are problematic as they seem to
  1281. * terminate DMA transfers. Avoid them by simply using a length of
  1282. * DPTR_ALIGN (4) when skb data is aligned to DPTR_ALIGN.
  1283. *
  1284. * As skb is guaranteed to have at least ETH_ZLEN (60) bytes of
  1285. * data by the call to skb_put_padto() above this is safe with
  1286. * respect to both the length of the first DMA descriptor (len)
  1287. * overflowing the available data and the length of the second DMA
  1288. * descriptor (skb->len - len) being negative.
  1289. */
  1290. if (len == 0)
  1291. len = DPTR_ALIGN;
  1292. memcpy(buffer, skb->data, len);
  1293. dma_addr = dma_map_single(ndev->dev.parent, buffer, len, DMA_TO_DEVICE);
  1294. if (dma_mapping_error(ndev->dev.parent, dma_addr))
  1295. goto drop;
  1296. desc = &priv->tx_ring[q][entry];
  1297. desc->ds_tagl = cpu_to_le16(len);
  1298. desc->dptr = cpu_to_le32(dma_addr);
  1299. buffer = skb->data + len;
  1300. len = skb->len - len;
  1301. dma_addr = dma_map_single(ndev->dev.parent, buffer, len, DMA_TO_DEVICE);
  1302. if (dma_mapping_error(ndev->dev.parent, dma_addr))
  1303. goto unmap;
  1304. desc++;
  1305. desc->ds_tagl = cpu_to_le16(len);
  1306. desc->dptr = cpu_to_le32(dma_addr);
  1307. /* TX timestamp required */
  1308. if (q == RAVB_NC) {
  1309. ts_skb = kmalloc(sizeof(*ts_skb), GFP_ATOMIC);
  1310. if (!ts_skb) {
  1311. desc--;
  1312. dma_unmap_single(ndev->dev.parent, dma_addr, len,
  1313. DMA_TO_DEVICE);
  1314. goto unmap;
  1315. }
  1316. ts_skb->skb = skb_get(skb);
  1317. ts_skb->tag = priv->ts_skb_tag++;
  1318. priv->ts_skb_tag &= 0x3ff;
  1319. list_add_tail(&ts_skb->list, &priv->ts_skb_list);
  1320. /* TAG and timestamp required flag */
  1321. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  1322. desc->tagh_tsr = (ts_skb->tag >> 4) | TX_TSR;
  1323. desc->ds_tagl |= cpu_to_le16(ts_skb->tag << 12);
  1324. }
  1325. skb_tx_timestamp(skb);
  1326. /* Descriptor type must be set after all the above writes */
  1327. dma_wmb();
  1328. desc->die_dt = DT_FEND;
  1329. desc--;
  1330. desc->die_dt = DT_FSTART;
  1331. ravb_modify(ndev, TCCR, TCCR_TSRQ0 << q, TCCR_TSRQ0 << q);
  1332. priv->cur_tx[q] += NUM_TX_DESC;
  1333. if (priv->cur_tx[q] - priv->dirty_tx[q] >
  1334. (priv->num_tx_ring[q] - 1) * NUM_TX_DESC &&
  1335. !ravb_tx_free(ndev, q, true))
  1336. netif_stop_subqueue(ndev, q);
  1337. exit:
  1338. mmiowb();
  1339. spin_unlock_irqrestore(&priv->lock, flags);
  1340. return NETDEV_TX_OK;
  1341. unmap:
  1342. dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr),
  1343. le16_to_cpu(desc->ds_tagl), DMA_TO_DEVICE);
  1344. drop:
  1345. dev_kfree_skb_any(skb);
  1346. priv->tx_skb[q][entry / NUM_TX_DESC] = NULL;
  1347. goto exit;
  1348. }
  1349. static u16 ravb_select_queue(struct net_device *ndev, struct sk_buff *skb,
  1350. struct net_device *sb_dev,
  1351. select_queue_fallback_t fallback)
  1352. {
  1353. /* If skb needs TX timestamp, it is handled in network control queue */
  1354. return (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) ? RAVB_NC :
  1355. RAVB_BE;
  1356. }
  1357. static struct net_device_stats *ravb_get_stats(struct net_device *ndev)
  1358. {
  1359. struct ravb_private *priv = netdev_priv(ndev);
  1360. struct net_device_stats *nstats, *stats0, *stats1;
  1361. nstats = &ndev->stats;
  1362. stats0 = &priv->stats[RAVB_BE];
  1363. stats1 = &priv->stats[RAVB_NC];
  1364. nstats->tx_dropped += ravb_read(ndev, TROCR);
  1365. ravb_write(ndev, 0, TROCR); /* (write clear) */
  1366. nstats->collisions += ravb_read(ndev, CDCR);
  1367. ravb_write(ndev, 0, CDCR); /* (write clear) */
  1368. nstats->tx_carrier_errors += ravb_read(ndev, LCCR);
  1369. ravb_write(ndev, 0, LCCR); /* (write clear) */
  1370. nstats->tx_carrier_errors += ravb_read(ndev, CERCR);
  1371. ravb_write(ndev, 0, CERCR); /* (write clear) */
  1372. nstats->tx_carrier_errors += ravb_read(ndev, CEECR);
  1373. ravb_write(ndev, 0, CEECR); /* (write clear) */
  1374. nstats->rx_packets = stats0->rx_packets + stats1->rx_packets;
  1375. nstats->tx_packets = stats0->tx_packets + stats1->tx_packets;
  1376. nstats->rx_bytes = stats0->rx_bytes + stats1->rx_bytes;
  1377. nstats->tx_bytes = stats0->tx_bytes + stats1->tx_bytes;
  1378. nstats->multicast = stats0->multicast + stats1->multicast;
  1379. nstats->rx_errors = stats0->rx_errors + stats1->rx_errors;
  1380. nstats->rx_crc_errors = stats0->rx_crc_errors + stats1->rx_crc_errors;
  1381. nstats->rx_frame_errors =
  1382. stats0->rx_frame_errors + stats1->rx_frame_errors;
  1383. nstats->rx_length_errors =
  1384. stats0->rx_length_errors + stats1->rx_length_errors;
  1385. nstats->rx_missed_errors =
  1386. stats0->rx_missed_errors + stats1->rx_missed_errors;
  1387. nstats->rx_over_errors =
  1388. stats0->rx_over_errors + stats1->rx_over_errors;
  1389. return nstats;
  1390. }
  1391. /* Update promiscuous bit */
  1392. static void ravb_set_rx_mode(struct net_device *ndev)
  1393. {
  1394. struct ravb_private *priv = netdev_priv(ndev);
  1395. unsigned long flags;
  1396. spin_lock_irqsave(&priv->lock, flags);
  1397. ravb_modify(ndev, ECMR, ECMR_PRM,
  1398. ndev->flags & IFF_PROMISC ? ECMR_PRM : 0);
  1399. mmiowb();
  1400. spin_unlock_irqrestore(&priv->lock, flags);
  1401. }
  1402. /* Device close function for Ethernet AVB */
  1403. static int ravb_close(struct net_device *ndev)
  1404. {
  1405. struct device_node *np = ndev->dev.parent->of_node;
  1406. struct ravb_private *priv = netdev_priv(ndev);
  1407. struct ravb_tstamp_skb *ts_skb, *ts_skb2;
  1408. netif_tx_stop_all_queues(ndev);
  1409. /* Disable interrupts by clearing the interrupt masks. */
  1410. ravb_write(ndev, 0, RIC0);
  1411. ravb_write(ndev, 0, RIC2);
  1412. ravb_write(ndev, 0, TIC);
  1413. /* Stop PTP Clock driver */
  1414. if (priv->chip_id == RCAR_GEN2)
  1415. ravb_ptp_stop(ndev);
  1416. /* Set the config mode to stop the AVB-DMAC's processes */
  1417. if (ravb_stop_dma(ndev) < 0)
  1418. netdev_err(ndev,
  1419. "device will be stopped after h/w processes are done.\n");
  1420. /* Clear the timestamp list */
  1421. list_for_each_entry_safe(ts_skb, ts_skb2, &priv->ts_skb_list, list) {
  1422. list_del(&ts_skb->list);
  1423. kfree_skb(ts_skb->skb);
  1424. kfree(ts_skb);
  1425. }
  1426. /* PHY disconnect */
  1427. if (ndev->phydev) {
  1428. phy_stop(ndev->phydev);
  1429. phy_disconnect(ndev->phydev);
  1430. if (of_phy_is_fixed_link(np))
  1431. of_phy_deregister_fixed_link(np);
  1432. }
  1433. if (priv->chip_id != RCAR_GEN2) {
  1434. free_irq(priv->tx_irqs[RAVB_NC], ndev);
  1435. free_irq(priv->rx_irqs[RAVB_NC], ndev);
  1436. free_irq(priv->tx_irqs[RAVB_BE], ndev);
  1437. free_irq(priv->rx_irqs[RAVB_BE], ndev);
  1438. free_irq(priv->emac_irq, ndev);
  1439. }
  1440. free_irq(ndev->irq, ndev);
  1441. napi_disable(&priv->napi[RAVB_NC]);
  1442. napi_disable(&priv->napi[RAVB_BE]);
  1443. /* Free all the skb's in the RX queue and the DMA buffers. */
  1444. ravb_ring_free(ndev, RAVB_BE);
  1445. ravb_ring_free(ndev, RAVB_NC);
  1446. return 0;
  1447. }
  1448. static int ravb_hwtstamp_get(struct net_device *ndev, struct ifreq *req)
  1449. {
  1450. struct ravb_private *priv = netdev_priv(ndev);
  1451. struct hwtstamp_config config;
  1452. config.flags = 0;
  1453. config.tx_type = priv->tstamp_tx_ctrl ? HWTSTAMP_TX_ON :
  1454. HWTSTAMP_TX_OFF;
  1455. if (priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE_V2_L2_EVENT)
  1456. config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
  1457. else if (priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE_ALL)
  1458. config.rx_filter = HWTSTAMP_FILTER_ALL;
  1459. else
  1460. config.rx_filter = HWTSTAMP_FILTER_NONE;
  1461. return copy_to_user(req->ifr_data, &config, sizeof(config)) ?
  1462. -EFAULT : 0;
  1463. }
  1464. /* Control hardware time stamping */
  1465. static int ravb_hwtstamp_set(struct net_device *ndev, struct ifreq *req)
  1466. {
  1467. struct ravb_private *priv = netdev_priv(ndev);
  1468. struct hwtstamp_config config;
  1469. u32 tstamp_rx_ctrl = RAVB_RXTSTAMP_ENABLED;
  1470. u32 tstamp_tx_ctrl;
  1471. if (copy_from_user(&config, req->ifr_data, sizeof(config)))
  1472. return -EFAULT;
  1473. /* Reserved for future extensions */
  1474. if (config.flags)
  1475. return -EINVAL;
  1476. switch (config.tx_type) {
  1477. case HWTSTAMP_TX_OFF:
  1478. tstamp_tx_ctrl = 0;
  1479. break;
  1480. case HWTSTAMP_TX_ON:
  1481. tstamp_tx_ctrl = RAVB_TXTSTAMP_ENABLED;
  1482. break;
  1483. default:
  1484. return -ERANGE;
  1485. }
  1486. switch (config.rx_filter) {
  1487. case HWTSTAMP_FILTER_NONE:
  1488. tstamp_rx_ctrl = 0;
  1489. break;
  1490. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  1491. tstamp_rx_ctrl |= RAVB_RXTSTAMP_TYPE_V2_L2_EVENT;
  1492. break;
  1493. default:
  1494. config.rx_filter = HWTSTAMP_FILTER_ALL;
  1495. tstamp_rx_ctrl |= RAVB_RXTSTAMP_TYPE_ALL;
  1496. }
  1497. priv->tstamp_tx_ctrl = tstamp_tx_ctrl;
  1498. priv->tstamp_rx_ctrl = tstamp_rx_ctrl;
  1499. return copy_to_user(req->ifr_data, &config, sizeof(config)) ?
  1500. -EFAULT : 0;
  1501. }
  1502. /* ioctl to device function */
  1503. static int ravb_do_ioctl(struct net_device *ndev, struct ifreq *req, int cmd)
  1504. {
  1505. struct phy_device *phydev = ndev->phydev;
  1506. if (!netif_running(ndev))
  1507. return -EINVAL;
  1508. if (!phydev)
  1509. return -ENODEV;
  1510. switch (cmd) {
  1511. case SIOCGHWTSTAMP:
  1512. return ravb_hwtstamp_get(ndev, req);
  1513. case SIOCSHWTSTAMP:
  1514. return ravb_hwtstamp_set(ndev, req);
  1515. }
  1516. return phy_mii_ioctl(phydev, req, cmd);
  1517. }
  1518. static int ravb_change_mtu(struct net_device *ndev, int new_mtu)
  1519. {
  1520. if (netif_running(ndev))
  1521. return -EBUSY;
  1522. ndev->mtu = new_mtu;
  1523. netdev_update_features(ndev);
  1524. return 0;
  1525. }
  1526. static void ravb_set_rx_csum(struct net_device *ndev, bool enable)
  1527. {
  1528. struct ravb_private *priv = netdev_priv(ndev);
  1529. unsigned long flags;
  1530. spin_lock_irqsave(&priv->lock, flags);
  1531. /* Disable TX and RX */
  1532. ravb_rcv_snd_disable(ndev);
  1533. /* Modify RX Checksum setting */
  1534. ravb_modify(ndev, ECMR, ECMR_RCSC, enable ? ECMR_RCSC : 0);
  1535. /* Enable TX and RX */
  1536. ravb_rcv_snd_enable(ndev);
  1537. spin_unlock_irqrestore(&priv->lock, flags);
  1538. }
  1539. static int ravb_set_features(struct net_device *ndev,
  1540. netdev_features_t features)
  1541. {
  1542. netdev_features_t changed = ndev->features ^ features;
  1543. if (changed & NETIF_F_RXCSUM)
  1544. ravb_set_rx_csum(ndev, features & NETIF_F_RXCSUM);
  1545. ndev->features = features;
  1546. return 0;
  1547. }
  1548. static const struct net_device_ops ravb_netdev_ops = {
  1549. .ndo_open = ravb_open,
  1550. .ndo_stop = ravb_close,
  1551. .ndo_start_xmit = ravb_start_xmit,
  1552. .ndo_select_queue = ravb_select_queue,
  1553. .ndo_get_stats = ravb_get_stats,
  1554. .ndo_set_rx_mode = ravb_set_rx_mode,
  1555. .ndo_tx_timeout = ravb_tx_timeout,
  1556. .ndo_do_ioctl = ravb_do_ioctl,
  1557. .ndo_change_mtu = ravb_change_mtu,
  1558. .ndo_validate_addr = eth_validate_addr,
  1559. .ndo_set_mac_address = eth_mac_addr,
  1560. .ndo_set_features = ravb_set_features,
  1561. };
  1562. /* MDIO bus init function */
  1563. static int ravb_mdio_init(struct ravb_private *priv)
  1564. {
  1565. struct platform_device *pdev = priv->pdev;
  1566. struct device *dev = &pdev->dev;
  1567. int error;
  1568. /* Bitbang init */
  1569. priv->mdiobb.ops = &bb_ops;
  1570. /* MII controller setting */
  1571. priv->mii_bus = alloc_mdio_bitbang(&priv->mdiobb);
  1572. if (!priv->mii_bus)
  1573. return -ENOMEM;
  1574. /* Hook up MII support for ethtool */
  1575. priv->mii_bus->name = "ravb_mii";
  1576. priv->mii_bus->parent = dev;
  1577. snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
  1578. pdev->name, pdev->id);
  1579. /* Register MDIO bus */
  1580. error = of_mdiobus_register(priv->mii_bus, dev->of_node);
  1581. if (error)
  1582. goto out_free_bus;
  1583. return 0;
  1584. out_free_bus:
  1585. free_mdio_bitbang(priv->mii_bus);
  1586. return error;
  1587. }
  1588. /* MDIO bus release function */
  1589. static int ravb_mdio_release(struct ravb_private *priv)
  1590. {
  1591. /* Unregister mdio bus */
  1592. mdiobus_unregister(priv->mii_bus);
  1593. /* Free bitbang info */
  1594. free_mdio_bitbang(priv->mii_bus);
  1595. return 0;
  1596. }
  1597. static const struct of_device_id ravb_match_table[] = {
  1598. { .compatible = "renesas,etheravb-r8a7790", .data = (void *)RCAR_GEN2 },
  1599. { .compatible = "renesas,etheravb-r8a7794", .data = (void *)RCAR_GEN2 },
  1600. { .compatible = "renesas,etheravb-rcar-gen2", .data = (void *)RCAR_GEN2 },
  1601. { .compatible = "renesas,etheravb-r8a7795", .data = (void *)RCAR_GEN3 },
  1602. { .compatible = "renesas,etheravb-rcar-gen3", .data = (void *)RCAR_GEN3 },
  1603. { }
  1604. };
  1605. MODULE_DEVICE_TABLE(of, ravb_match_table);
  1606. static int ravb_set_gti(struct net_device *ndev)
  1607. {
  1608. struct ravb_private *priv = netdev_priv(ndev);
  1609. struct device *dev = ndev->dev.parent;
  1610. unsigned long rate;
  1611. uint64_t inc;
  1612. rate = clk_get_rate(priv->clk);
  1613. if (!rate)
  1614. return -EINVAL;
  1615. inc = 1000000000ULL << 20;
  1616. do_div(inc, rate);
  1617. if (inc < GTI_TIV_MIN || inc > GTI_TIV_MAX) {
  1618. dev_err(dev, "gti.tiv increment 0x%llx is outside the range 0x%x - 0x%x\n",
  1619. inc, GTI_TIV_MIN, GTI_TIV_MAX);
  1620. return -EINVAL;
  1621. }
  1622. ravb_write(ndev, inc, GTI);
  1623. return 0;
  1624. }
  1625. static void ravb_set_config_mode(struct net_device *ndev)
  1626. {
  1627. struct ravb_private *priv = netdev_priv(ndev);
  1628. if (priv->chip_id == RCAR_GEN2) {
  1629. ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_CONFIG);
  1630. /* Set CSEL value */
  1631. ravb_modify(ndev, CCC, CCC_CSEL, CCC_CSEL_HPB);
  1632. } else {
  1633. ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_CONFIG |
  1634. CCC_GAC | CCC_CSEL_HPB);
  1635. }
  1636. }
  1637. /* Set tx and rx clock internal delay modes */
  1638. static void ravb_set_delay_mode(struct net_device *ndev)
  1639. {
  1640. struct ravb_private *priv = netdev_priv(ndev);
  1641. int set = 0;
  1642. if (priv->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
  1643. priv->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID)
  1644. set |= APSR_DM_RDM;
  1645. if (priv->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
  1646. priv->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID)
  1647. set |= APSR_DM_TDM;
  1648. ravb_modify(ndev, APSR, APSR_DM, set);
  1649. }
  1650. static int ravb_probe(struct platform_device *pdev)
  1651. {
  1652. struct device_node *np = pdev->dev.of_node;
  1653. struct ravb_private *priv;
  1654. enum ravb_chip_id chip_id;
  1655. struct net_device *ndev;
  1656. int error, irq, q;
  1657. struct resource *res;
  1658. int i;
  1659. if (!np) {
  1660. dev_err(&pdev->dev,
  1661. "this driver is required to be instantiated from device tree\n");
  1662. return -EINVAL;
  1663. }
  1664. /* Get base address */
  1665. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1666. if (!res) {
  1667. dev_err(&pdev->dev, "invalid resource\n");
  1668. return -EINVAL;
  1669. }
  1670. ndev = alloc_etherdev_mqs(sizeof(struct ravb_private),
  1671. NUM_TX_QUEUE, NUM_RX_QUEUE);
  1672. if (!ndev)
  1673. return -ENOMEM;
  1674. ndev->features = NETIF_F_RXCSUM;
  1675. ndev->hw_features = NETIF_F_RXCSUM;
  1676. pm_runtime_enable(&pdev->dev);
  1677. pm_runtime_get_sync(&pdev->dev);
  1678. /* The Ether-specific entries in the device structure. */
  1679. ndev->base_addr = res->start;
  1680. chip_id = (enum ravb_chip_id)of_device_get_match_data(&pdev->dev);
  1681. if (chip_id == RCAR_GEN3)
  1682. irq = platform_get_irq_byname(pdev, "ch22");
  1683. else
  1684. irq = platform_get_irq(pdev, 0);
  1685. if (irq < 0) {
  1686. error = irq;
  1687. goto out_release;
  1688. }
  1689. ndev->irq = irq;
  1690. SET_NETDEV_DEV(ndev, &pdev->dev);
  1691. priv = netdev_priv(ndev);
  1692. priv->ndev = ndev;
  1693. priv->pdev = pdev;
  1694. priv->num_tx_ring[RAVB_BE] = BE_TX_RING_SIZE;
  1695. priv->num_rx_ring[RAVB_BE] = BE_RX_RING_SIZE;
  1696. priv->num_tx_ring[RAVB_NC] = NC_TX_RING_SIZE;
  1697. priv->num_rx_ring[RAVB_NC] = NC_RX_RING_SIZE;
  1698. priv->addr = devm_ioremap_resource(&pdev->dev, res);
  1699. if (IS_ERR(priv->addr)) {
  1700. error = PTR_ERR(priv->addr);
  1701. goto out_release;
  1702. }
  1703. spin_lock_init(&priv->lock);
  1704. INIT_WORK(&priv->work, ravb_tx_timeout_work);
  1705. priv->phy_interface = of_get_phy_mode(np);
  1706. priv->no_avb_link = of_property_read_bool(np, "renesas,no-ether-link");
  1707. priv->avb_link_active_low =
  1708. of_property_read_bool(np, "renesas,ether-link-active-low");
  1709. if (chip_id == RCAR_GEN3) {
  1710. irq = platform_get_irq_byname(pdev, "ch24");
  1711. if (irq < 0) {
  1712. error = irq;
  1713. goto out_release;
  1714. }
  1715. priv->emac_irq = irq;
  1716. for (i = 0; i < NUM_RX_QUEUE; i++) {
  1717. irq = platform_get_irq_byname(pdev, ravb_rx_irqs[i]);
  1718. if (irq < 0) {
  1719. error = irq;
  1720. goto out_release;
  1721. }
  1722. priv->rx_irqs[i] = irq;
  1723. }
  1724. for (i = 0; i < NUM_TX_QUEUE; i++) {
  1725. irq = platform_get_irq_byname(pdev, ravb_tx_irqs[i]);
  1726. if (irq < 0) {
  1727. error = irq;
  1728. goto out_release;
  1729. }
  1730. priv->tx_irqs[i] = irq;
  1731. }
  1732. }
  1733. priv->chip_id = chip_id;
  1734. priv->clk = devm_clk_get(&pdev->dev, NULL);
  1735. if (IS_ERR(priv->clk)) {
  1736. error = PTR_ERR(priv->clk);
  1737. goto out_release;
  1738. }
  1739. ndev->max_mtu = 2048 - (ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN);
  1740. ndev->min_mtu = ETH_MIN_MTU;
  1741. /* Set function */
  1742. ndev->netdev_ops = &ravb_netdev_ops;
  1743. ndev->ethtool_ops = &ravb_ethtool_ops;
  1744. /* Set AVB config mode */
  1745. ravb_set_config_mode(ndev);
  1746. /* Set GTI value */
  1747. error = ravb_set_gti(ndev);
  1748. if (error)
  1749. goto out_release;
  1750. /* Request GTI loading */
  1751. ravb_modify(ndev, GCCR, GCCR_LTI, GCCR_LTI);
  1752. if (priv->chip_id != RCAR_GEN2)
  1753. ravb_set_delay_mode(ndev);
  1754. /* Allocate descriptor base address table */
  1755. priv->desc_bat_size = sizeof(struct ravb_desc) * DBAT_ENTRY_NUM;
  1756. priv->desc_bat = dma_alloc_coherent(ndev->dev.parent, priv->desc_bat_size,
  1757. &priv->desc_bat_dma, GFP_KERNEL);
  1758. if (!priv->desc_bat) {
  1759. dev_err(&pdev->dev,
  1760. "Cannot allocate desc base address table (size %d bytes)\n",
  1761. priv->desc_bat_size);
  1762. error = -ENOMEM;
  1763. goto out_release;
  1764. }
  1765. for (q = RAVB_BE; q < DBAT_ENTRY_NUM; q++)
  1766. priv->desc_bat[q].die_dt = DT_EOS;
  1767. ravb_write(ndev, priv->desc_bat_dma, DBAT);
  1768. /* Initialise HW timestamp list */
  1769. INIT_LIST_HEAD(&priv->ts_skb_list);
  1770. /* Initialise PTP Clock driver */
  1771. if (chip_id != RCAR_GEN2)
  1772. ravb_ptp_init(ndev, pdev);
  1773. /* Debug message level */
  1774. priv->msg_enable = RAVB_DEF_MSG_ENABLE;
  1775. /* Read and set MAC address */
  1776. ravb_read_mac_address(ndev, of_get_mac_address(np));
  1777. if (!is_valid_ether_addr(ndev->dev_addr)) {
  1778. dev_warn(&pdev->dev,
  1779. "no valid MAC address supplied, using a random one\n");
  1780. eth_hw_addr_random(ndev);
  1781. }
  1782. /* MDIO bus init */
  1783. error = ravb_mdio_init(priv);
  1784. if (error) {
  1785. dev_err(&pdev->dev, "failed to initialize MDIO\n");
  1786. goto out_dma_free;
  1787. }
  1788. netif_napi_add(ndev, &priv->napi[RAVB_BE], ravb_poll, 64);
  1789. netif_napi_add(ndev, &priv->napi[RAVB_NC], ravb_poll, 64);
  1790. /* Network device register */
  1791. error = register_netdev(ndev);
  1792. if (error)
  1793. goto out_napi_del;
  1794. device_set_wakeup_capable(&pdev->dev, 1);
  1795. /* Print device information */
  1796. netdev_info(ndev, "Base address at %#x, %pM, IRQ %d.\n",
  1797. (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
  1798. platform_set_drvdata(pdev, ndev);
  1799. return 0;
  1800. out_napi_del:
  1801. netif_napi_del(&priv->napi[RAVB_NC]);
  1802. netif_napi_del(&priv->napi[RAVB_BE]);
  1803. ravb_mdio_release(priv);
  1804. out_dma_free:
  1805. dma_free_coherent(ndev->dev.parent, priv->desc_bat_size, priv->desc_bat,
  1806. priv->desc_bat_dma);
  1807. /* Stop PTP Clock driver */
  1808. if (chip_id != RCAR_GEN2)
  1809. ravb_ptp_stop(ndev);
  1810. out_release:
  1811. free_netdev(ndev);
  1812. pm_runtime_put(&pdev->dev);
  1813. pm_runtime_disable(&pdev->dev);
  1814. return error;
  1815. }
  1816. static int ravb_remove(struct platform_device *pdev)
  1817. {
  1818. struct net_device *ndev = platform_get_drvdata(pdev);
  1819. struct ravb_private *priv = netdev_priv(ndev);
  1820. /* Stop PTP Clock driver */
  1821. if (priv->chip_id != RCAR_GEN2)
  1822. ravb_ptp_stop(ndev);
  1823. dma_free_coherent(ndev->dev.parent, priv->desc_bat_size, priv->desc_bat,
  1824. priv->desc_bat_dma);
  1825. /* Set reset mode */
  1826. ravb_write(ndev, CCC_OPC_RESET, CCC);
  1827. pm_runtime_put_sync(&pdev->dev);
  1828. unregister_netdev(ndev);
  1829. netif_napi_del(&priv->napi[RAVB_NC]);
  1830. netif_napi_del(&priv->napi[RAVB_BE]);
  1831. ravb_mdio_release(priv);
  1832. pm_runtime_disable(&pdev->dev);
  1833. free_netdev(ndev);
  1834. platform_set_drvdata(pdev, NULL);
  1835. return 0;
  1836. }
  1837. static int ravb_wol_setup(struct net_device *ndev)
  1838. {
  1839. struct ravb_private *priv = netdev_priv(ndev);
  1840. /* Disable interrupts by clearing the interrupt masks. */
  1841. ravb_write(ndev, 0, RIC0);
  1842. ravb_write(ndev, 0, RIC2);
  1843. ravb_write(ndev, 0, TIC);
  1844. /* Only allow ECI interrupts */
  1845. synchronize_irq(priv->emac_irq);
  1846. napi_disable(&priv->napi[RAVB_NC]);
  1847. napi_disable(&priv->napi[RAVB_BE]);
  1848. ravb_write(ndev, ECSIPR_MPDIP, ECSIPR);
  1849. /* Enable MagicPacket */
  1850. ravb_modify(ndev, ECMR, ECMR_MPDE, ECMR_MPDE);
  1851. return enable_irq_wake(priv->emac_irq);
  1852. }
  1853. static int ravb_wol_restore(struct net_device *ndev)
  1854. {
  1855. struct ravb_private *priv = netdev_priv(ndev);
  1856. int ret;
  1857. napi_enable(&priv->napi[RAVB_NC]);
  1858. napi_enable(&priv->napi[RAVB_BE]);
  1859. /* Disable MagicPacket */
  1860. ravb_modify(ndev, ECMR, ECMR_MPDE, 0);
  1861. ret = ravb_close(ndev);
  1862. if (ret < 0)
  1863. return ret;
  1864. return disable_irq_wake(priv->emac_irq);
  1865. }
  1866. static int __maybe_unused ravb_suspend(struct device *dev)
  1867. {
  1868. struct net_device *ndev = dev_get_drvdata(dev);
  1869. struct ravb_private *priv = netdev_priv(ndev);
  1870. int ret;
  1871. if (!netif_running(ndev))
  1872. return 0;
  1873. netif_device_detach(ndev);
  1874. if (priv->wol_enabled)
  1875. ret = ravb_wol_setup(ndev);
  1876. else
  1877. ret = ravb_close(ndev);
  1878. return ret;
  1879. }
  1880. static int __maybe_unused ravb_resume(struct device *dev)
  1881. {
  1882. struct net_device *ndev = dev_get_drvdata(dev);
  1883. struct ravb_private *priv = netdev_priv(ndev);
  1884. int ret = 0;
  1885. /* If WoL is enabled set reset mode to rearm the WoL logic */
  1886. if (priv->wol_enabled)
  1887. ravb_write(ndev, CCC_OPC_RESET, CCC);
  1888. /* All register have been reset to default values.
  1889. * Restore all registers which where setup at probe time and
  1890. * reopen device if it was running before system suspended.
  1891. */
  1892. /* Set AVB config mode */
  1893. ravb_set_config_mode(ndev);
  1894. /* Set GTI value */
  1895. ret = ravb_set_gti(ndev);
  1896. if (ret)
  1897. return ret;
  1898. /* Request GTI loading */
  1899. ravb_modify(ndev, GCCR, GCCR_LTI, GCCR_LTI);
  1900. if (priv->chip_id != RCAR_GEN2)
  1901. ravb_set_delay_mode(ndev);
  1902. /* Restore descriptor base address table */
  1903. ravb_write(ndev, priv->desc_bat_dma, DBAT);
  1904. if (netif_running(ndev)) {
  1905. if (priv->wol_enabled) {
  1906. ret = ravb_wol_restore(ndev);
  1907. if (ret)
  1908. return ret;
  1909. }
  1910. ret = ravb_open(ndev);
  1911. if (ret < 0)
  1912. return ret;
  1913. netif_device_attach(ndev);
  1914. }
  1915. return ret;
  1916. }
  1917. static int __maybe_unused ravb_runtime_nop(struct device *dev)
  1918. {
  1919. /* Runtime PM callback shared between ->runtime_suspend()
  1920. * and ->runtime_resume(). Simply returns success.
  1921. *
  1922. * This driver re-initializes all registers after
  1923. * pm_runtime_get_sync() anyway so there is no need
  1924. * to save and restore registers here.
  1925. */
  1926. return 0;
  1927. }
  1928. static const struct dev_pm_ops ravb_dev_pm_ops = {
  1929. SET_SYSTEM_SLEEP_PM_OPS(ravb_suspend, ravb_resume)
  1930. SET_RUNTIME_PM_OPS(ravb_runtime_nop, ravb_runtime_nop, NULL)
  1931. };
  1932. static struct platform_driver ravb_driver = {
  1933. .probe = ravb_probe,
  1934. .remove = ravb_remove,
  1935. .driver = {
  1936. .name = "ravb",
  1937. .pm = &ravb_dev_pm_ops,
  1938. .of_match_table = ravb_match_table,
  1939. },
  1940. };
  1941. module_platform_driver(ravb_driver);
  1942. MODULE_AUTHOR("Mitsuhiro Kimura, Masaru Nagai");
  1943. MODULE_DESCRIPTION("Renesas Ethernet AVB driver");
  1944. MODULE_LICENSE("GPL v2");