ravb_main.c 55 KB

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