altera_tse_main.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Altera Triple-Speed Ethernet MAC driver
  3. * Copyright (C) 2008-2014 Altera Corporation. All rights reserved
  4. *
  5. * Contributors:
  6. * Dalon Westergreen
  7. * Thomas Chou
  8. * Ian Abbott
  9. * Yuriy Kozlov
  10. * Tobias Klauser
  11. * Andriy Smolskyy
  12. * Roman Bulgakov
  13. * Dmytro Mytarchuk
  14. * Matthew Gerlach
  15. *
  16. * Original driver contributed by SLS.
  17. * Major updates contributed by GlobalLogic
  18. */
  19. #include <linux/atomic.h>
  20. #include <linux/delay.h>
  21. #include <linux/etherdevice.h>
  22. #include <linux/if_vlan.h>
  23. #include <linux/init.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/io.h>
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/mii.h>
  29. #include <linux/mdio/mdio-regmap.h>
  30. #include <linux/netdevice.h>
  31. #include <linux/of.h>
  32. #include <linux/of_mdio.h>
  33. #include <linux/of_net.h>
  34. #include <linux/pcs-lynx.h>
  35. #include <linux/phy.h>
  36. #include <linux/platform_device.h>
  37. #include <linux/property.h>
  38. #include <linux/regmap.h>
  39. #include <linux/skbuff.h>
  40. #include <asm/cacheflush.h>
  41. #include "altera_utils.h"
  42. #include "altera_tse.h"
  43. #include "altera_sgdma.h"
  44. #include "altera_msgdma.h"
  45. static atomic_t instance_count = ATOMIC_INIT(~0);
  46. /* Module parameters */
  47. static int debug = -1;
  48. module_param(debug, int, 0644);
  49. MODULE_PARM_DESC(debug, "Message Level (-1: default, 0: no output, 16: all)");
  50. static const u32 default_msg_level = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
  51. NETIF_MSG_LINK | NETIF_MSG_IFUP |
  52. NETIF_MSG_IFDOWN);
  53. #define RX_DESCRIPTORS 64
  54. static int dma_rx_num = RX_DESCRIPTORS;
  55. module_param(dma_rx_num, int, 0644);
  56. MODULE_PARM_DESC(dma_rx_num, "Number of descriptors in the RX list");
  57. #define TX_DESCRIPTORS 64
  58. static int dma_tx_num = TX_DESCRIPTORS;
  59. module_param(dma_tx_num, int, 0644);
  60. MODULE_PARM_DESC(dma_tx_num, "Number of descriptors in the TX list");
  61. #define POLL_PHY (-1)
  62. /* Make sure DMA buffer size is larger than the max frame size
  63. * plus some alignment offset and a VLAN header. If the max frame size is
  64. * 1518, a VLAN header would be additional 4 bytes and additional
  65. * headroom for alignment is 2 bytes, 2048 is just fine.
  66. */
  67. #define ALTERA_RXDMABUFFER_SIZE 2048
  68. /* Allow network stack to resume queuing packets after we've
  69. * finished transmitting at least 1/4 of the packets in the queue.
  70. */
  71. #define TSE_TX_THRESH(x) (x->tx_ring_size / 4)
  72. #define TXQUEUESTOP_THRESHHOLD 2
  73. static inline u32 tse_tx_avail(struct altera_tse_private *priv)
  74. {
  75. return priv->tx_cons + priv->tx_ring_size - priv->tx_prod - 1;
  76. }
  77. /* MDIO specific functions
  78. */
  79. static int altera_tse_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
  80. {
  81. struct net_device *ndev = bus->priv;
  82. struct altera_tse_private *priv = netdev_priv(ndev);
  83. /* set MDIO address */
  84. csrwr32((mii_id & 0x1f), priv->mac_dev,
  85. tse_csroffs(mdio_phy1_addr));
  86. /* get the data */
  87. return csrrd32(priv->mac_dev,
  88. tse_csroffs(mdio_phy1) + regnum * 4) & 0xffff;
  89. }
  90. static int altera_tse_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
  91. u16 value)
  92. {
  93. struct net_device *ndev = bus->priv;
  94. struct altera_tse_private *priv = netdev_priv(ndev);
  95. /* set MDIO address */
  96. csrwr32((mii_id & 0x1f), priv->mac_dev,
  97. tse_csroffs(mdio_phy1_addr));
  98. /* write the data */
  99. csrwr32(value, priv->mac_dev, tse_csroffs(mdio_phy1) + regnum * 4);
  100. return 0;
  101. }
  102. static int altera_tse_mdio_create(struct net_device *dev, unsigned int id)
  103. {
  104. struct altera_tse_private *priv = netdev_priv(dev);
  105. struct device_node *mdio_node = NULL;
  106. struct device_node *child_node = NULL;
  107. struct mii_bus *mdio = NULL;
  108. int ret;
  109. for_each_child_of_node(priv->device->of_node, child_node) {
  110. if (of_device_is_compatible(child_node, "altr,tse-mdio")) {
  111. mdio_node = child_node;
  112. break;
  113. }
  114. }
  115. if (mdio_node) {
  116. netdev_dbg(dev, "FOUND MDIO subnode\n");
  117. } else {
  118. netdev_dbg(dev, "NO MDIO subnode\n");
  119. return 0;
  120. }
  121. mdio = mdiobus_alloc();
  122. if (mdio == NULL) {
  123. netdev_err(dev, "Error allocating MDIO bus\n");
  124. ret = -ENOMEM;
  125. goto put_node;
  126. }
  127. mdio->name = ALTERA_TSE_RESOURCE_NAME;
  128. mdio->read = &altera_tse_mdio_read;
  129. mdio->write = &altera_tse_mdio_write;
  130. snprintf(mdio->id, MII_BUS_ID_SIZE, "%s-%u", mdio->name, id);
  131. mdio->priv = dev;
  132. mdio->parent = priv->device;
  133. ret = of_mdiobus_register(mdio, mdio_node);
  134. if (ret != 0) {
  135. netdev_err(dev, "Cannot register MDIO bus %s\n",
  136. mdio->id);
  137. goto out_free_mdio;
  138. }
  139. of_node_put(mdio_node);
  140. if (netif_msg_drv(priv))
  141. netdev_info(dev, "MDIO bus %s: created\n", mdio->id);
  142. priv->mdio = mdio;
  143. return 0;
  144. out_free_mdio:
  145. mdiobus_free(mdio);
  146. mdio = NULL;
  147. put_node:
  148. of_node_put(mdio_node);
  149. return ret;
  150. }
  151. static void altera_tse_mdio_destroy(struct net_device *dev)
  152. {
  153. struct altera_tse_private *priv = netdev_priv(dev);
  154. if (priv->mdio == NULL)
  155. return;
  156. if (netif_msg_drv(priv))
  157. netdev_info(dev, "MDIO bus %s: removed\n",
  158. priv->mdio->id);
  159. mdiobus_unregister(priv->mdio);
  160. mdiobus_free(priv->mdio);
  161. priv->mdio = NULL;
  162. }
  163. static int tse_init_rx_buffer(struct altera_tse_private *priv,
  164. struct tse_buffer *rxbuffer, int len)
  165. {
  166. rxbuffer->skb = netdev_alloc_skb_ip_align(priv->dev, len);
  167. if (!rxbuffer->skb)
  168. return -ENOMEM;
  169. rxbuffer->dma_addr = dma_map_single(priv->device, rxbuffer->skb->data,
  170. len,
  171. DMA_FROM_DEVICE);
  172. if (dma_mapping_error(priv->device, rxbuffer->dma_addr)) {
  173. netdev_err(priv->dev, "%s: DMA mapping error\n", __func__);
  174. dev_kfree_skb_any(rxbuffer->skb);
  175. return -EINVAL;
  176. }
  177. rxbuffer->dma_addr &= (dma_addr_t)~3;
  178. rxbuffer->len = len;
  179. return 0;
  180. }
  181. static void tse_free_rx_buffer(struct altera_tse_private *priv,
  182. struct tse_buffer *rxbuffer)
  183. {
  184. dma_addr_t dma_addr = rxbuffer->dma_addr;
  185. struct sk_buff *skb = rxbuffer->skb;
  186. if (skb != NULL) {
  187. if (dma_addr)
  188. dma_unmap_single(priv->device, dma_addr,
  189. rxbuffer->len,
  190. DMA_FROM_DEVICE);
  191. dev_kfree_skb_any(skb);
  192. rxbuffer->skb = NULL;
  193. rxbuffer->dma_addr = 0;
  194. }
  195. }
  196. /* Unmap and free Tx buffer resources
  197. */
  198. static void tse_free_tx_buffer(struct altera_tse_private *priv,
  199. struct tse_buffer *buffer)
  200. {
  201. if (buffer->dma_addr) {
  202. if (buffer->mapped_as_page)
  203. dma_unmap_page(priv->device, buffer->dma_addr,
  204. buffer->len, DMA_TO_DEVICE);
  205. else
  206. dma_unmap_single(priv->device, buffer->dma_addr,
  207. buffer->len, DMA_TO_DEVICE);
  208. buffer->dma_addr = 0;
  209. }
  210. if (buffer->skb) {
  211. dev_kfree_skb_any(buffer->skb);
  212. buffer->skb = NULL;
  213. }
  214. }
  215. static int alloc_init_skbufs(struct altera_tse_private *priv)
  216. {
  217. unsigned int rx_descs = priv->rx_ring_size;
  218. unsigned int tx_descs = priv->tx_ring_size;
  219. int ret = -ENOMEM;
  220. int i;
  221. /* Create Rx ring buffer */
  222. priv->rx_ring = kzalloc_objs(struct tse_buffer, rx_descs);
  223. if (!priv->rx_ring)
  224. goto err_rx_ring;
  225. /* Create Tx ring buffer */
  226. priv->tx_ring = kzalloc_objs(struct tse_buffer, tx_descs);
  227. if (!priv->tx_ring)
  228. goto err_tx_ring;
  229. priv->tx_cons = 0;
  230. priv->tx_prod = 0;
  231. /* Init Rx ring */
  232. for (i = 0; i < rx_descs; i++) {
  233. ret = tse_init_rx_buffer(priv, &priv->rx_ring[i],
  234. priv->rx_dma_buf_sz);
  235. if (ret)
  236. goto err_init_rx_buffers;
  237. }
  238. priv->rx_cons = 0;
  239. priv->rx_prod = 0;
  240. return 0;
  241. err_init_rx_buffers:
  242. while (--i >= 0)
  243. tse_free_rx_buffer(priv, &priv->rx_ring[i]);
  244. kfree(priv->tx_ring);
  245. err_tx_ring:
  246. kfree(priv->rx_ring);
  247. err_rx_ring:
  248. return ret;
  249. }
  250. static void free_skbufs(struct net_device *dev)
  251. {
  252. struct altera_tse_private *priv = netdev_priv(dev);
  253. unsigned int rx_descs = priv->rx_ring_size;
  254. unsigned int tx_descs = priv->tx_ring_size;
  255. int i;
  256. /* Release the DMA TX/RX socket buffers */
  257. for (i = 0; i < rx_descs; i++)
  258. tse_free_rx_buffer(priv, &priv->rx_ring[i]);
  259. for (i = 0; i < tx_descs; i++)
  260. tse_free_tx_buffer(priv, &priv->tx_ring[i]);
  261. kfree(priv->tx_ring);
  262. }
  263. /* Reallocate the skb for the reception process
  264. */
  265. static inline void tse_rx_refill(struct altera_tse_private *priv)
  266. {
  267. unsigned int rxsize = priv->rx_ring_size;
  268. unsigned int entry;
  269. int ret;
  270. for (; priv->rx_cons - priv->rx_prod > 0;
  271. priv->rx_prod++) {
  272. entry = priv->rx_prod % rxsize;
  273. if (likely(priv->rx_ring[entry].skb == NULL)) {
  274. ret = tse_init_rx_buffer(priv, &priv->rx_ring[entry],
  275. priv->rx_dma_buf_sz);
  276. if (unlikely(ret != 0))
  277. break;
  278. priv->dmaops->add_rx_desc(priv, &priv->rx_ring[entry]);
  279. }
  280. }
  281. }
  282. /* Pull out the VLAN tag and fix up the packet
  283. */
  284. static inline void tse_rx_vlan(struct net_device *dev, struct sk_buff *skb)
  285. {
  286. struct ethhdr *eth_hdr;
  287. u16 vid;
  288. if ((dev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
  289. !__vlan_get_tag(skb, &vid)) {
  290. eth_hdr = (struct ethhdr *)skb->data;
  291. memmove(skb->data + VLAN_HLEN, eth_hdr, ETH_ALEN * 2);
  292. skb_pull(skb, VLAN_HLEN);
  293. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
  294. }
  295. }
  296. /* Receive a packet: retrieve and pass over to upper levels
  297. */
  298. static int tse_rx(struct altera_tse_private *priv, int limit)
  299. {
  300. unsigned int entry = priv->rx_cons % priv->rx_ring_size;
  301. unsigned int next_entry;
  302. unsigned int count = 0;
  303. struct sk_buff *skb;
  304. u32 rxstatus;
  305. u16 pktlength;
  306. u16 pktstatus;
  307. /* Check for count < limit first as get_rx_status is changing
  308. * the response-fifo so we must process the next packet
  309. * after calling get_rx_status if a response is pending.
  310. * (reading the last byte of the response pops the value from the fifo.)
  311. */
  312. while ((count < limit) &&
  313. ((rxstatus = priv->dmaops->get_rx_status(priv)) != 0)) {
  314. pktstatus = rxstatus >> 16;
  315. pktlength = rxstatus & 0xffff;
  316. if ((pktstatus & 0xFF) || (pktlength == 0))
  317. netdev_err(priv->dev,
  318. "RCV pktstatus %08X pktlength %08X\n",
  319. pktstatus, pktlength);
  320. /* DMA transfer from TSE starts with 2 additional bytes for
  321. * IP payload alignment. Status returned by get_rx_status()
  322. * contains DMA transfer length. Packet is 2 bytes shorter.
  323. */
  324. pktlength -= 2;
  325. count++;
  326. next_entry = (++priv->rx_cons) % priv->rx_ring_size;
  327. skb = priv->rx_ring[entry].skb;
  328. if (unlikely(!skb)) {
  329. netdev_err(priv->dev,
  330. "%s: Inconsistent Rx descriptor chain\n",
  331. __func__);
  332. priv->dev->stats.rx_dropped++;
  333. break;
  334. }
  335. priv->rx_ring[entry].skb = NULL;
  336. skb_put(skb, pktlength);
  337. dma_unmap_single(priv->device, priv->rx_ring[entry].dma_addr,
  338. priv->rx_ring[entry].len, DMA_FROM_DEVICE);
  339. if (netif_msg_pktdata(priv)) {
  340. netdev_info(priv->dev, "frame received %d bytes\n",
  341. pktlength);
  342. print_hex_dump(KERN_ERR, "data: ", DUMP_PREFIX_OFFSET,
  343. 16, 1, skb->data, pktlength, true);
  344. }
  345. tse_rx_vlan(priv->dev, skb);
  346. skb->protocol = eth_type_trans(skb, priv->dev);
  347. skb_checksum_none_assert(skb);
  348. napi_gro_receive(&priv->napi, skb);
  349. priv->dev->stats.rx_packets++;
  350. priv->dev->stats.rx_bytes += pktlength;
  351. entry = next_entry;
  352. tse_rx_refill(priv);
  353. }
  354. return count;
  355. }
  356. /* Reclaim resources after transmission completes
  357. */
  358. static int tse_tx_complete(struct altera_tse_private *priv)
  359. {
  360. unsigned int txsize = priv->tx_ring_size;
  361. struct tse_buffer *tx_buff;
  362. unsigned int entry;
  363. int txcomplete = 0;
  364. u32 ready;
  365. spin_lock(&priv->tx_lock);
  366. ready = priv->dmaops->tx_completions(priv);
  367. /* Free sent buffers */
  368. while (ready && (priv->tx_cons != priv->tx_prod)) {
  369. entry = priv->tx_cons % txsize;
  370. tx_buff = &priv->tx_ring[entry];
  371. if (netif_msg_tx_done(priv))
  372. netdev_dbg(priv->dev, "%s: curr %d, dirty %d\n",
  373. __func__, priv->tx_prod, priv->tx_cons);
  374. if (likely(tx_buff->skb))
  375. priv->dev->stats.tx_packets++;
  376. tse_free_tx_buffer(priv, tx_buff);
  377. priv->tx_cons++;
  378. txcomplete++;
  379. ready--;
  380. }
  381. if (unlikely(netif_queue_stopped(priv->dev) &&
  382. tse_tx_avail(priv) > TSE_TX_THRESH(priv))) {
  383. if (netif_queue_stopped(priv->dev) &&
  384. tse_tx_avail(priv) > TSE_TX_THRESH(priv)) {
  385. if (netif_msg_tx_done(priv))
  386. netdev_dbg(priv->dev, "%s: restart transmit\n",
  387. __func__);
  388. netif_wake_queue(priv->dev);
  389. }
  390. }
  391. spin_unlock(&priv->tx_lock);
  392. return txcomplete;
  393. }
  394. /* NAPI polling function
  395. */
  396. static int tse_poll(struct napi_struct *napi, int budget)
  397. {
  398. struct altera_tse_private *priv =
  399. container_of(napi, struct altera_tse_private, napi);
  400. unsigned long int flags;
  401. int rxcomplete = 0;
  402. tse_tx_complete(priv);
  403. rxcomplete = tse_rx(priv, budget);
  404. if (rxcomplete < budget) {
  405. napi_complete_done(napi, rxcomplete);
  406. netdev_dbg(priv->dev,
  407. "NAPI Complete, did %d packets with budget %d\n",
  408. rxcomplete, budget);
  409. spin_lock_irqsave(&priv->rxdma_irq_lock, flags);
  410. priv->dmaops->enable_rxirq(priv);
  411. priv->dmaops->enable_txirq(priv);
  412. spin_unlock_irqrestore(&priv->rxdma_irq_lock, flags);
  413. }
  414. return rxcomplete;
  415. }
  416. /* DMA TX & RX FIFO interrupt routing
  417. */
  418. static irqreturn_t altera_isr(int irq, void *dev_id)
  419. {
  420. struct net_device *dev = dev_id;
  421. struct altera_tse_private *priv;
  422. if (unlikely(!dev)) {
  423. pr_err("%s: invalid dev pointer\n", __func__);
  424. return IRQ_NONE;
  425. }
  426. priv = netdev_priv(dev);
  427. spin_lock(&priv->rxdma_irq_lock);
  428. /* reset IRQs */
  429. priv->dmaops->clear_rxirq(priv);
  430. priv->dmaops->clear_txirq(priv);
  431. spin_unlock(&priv->rxdma_irq_lock);
  432. if (likely(napi_schedule_prep(&priv->napi))) {
  433. spin_lock(&priv->rxdma_irq_lock);
  434. priv->dmaops->disable_rxirq(priv);
  435. priv->dmaops->disable_txirq(priv);
  436. spin_unlock(&priv->rxdma_irq_lock);
  437. __napi_schedule(&priv->napi);
  438. }
  439. return IRQ_HANDLED;
  440. }
  441. /* Transmit a packet (called by the kernel). Dispatches
  442. * either the SGDMA method for transmitting or the
  443. * MSGDMA method, assumes no scatter/gather support,
  444. * implying an assumption that there's only one
  445. * physically contiguous fragment starting at
  446. * skb->data, for length of skb_headlen(skb).
  447. */
  448. static netdev_tx_t tse_start_xmit(struct sk_buff *skb, struct net_device *dev)
  449. {
  450. struct altera_tse_private *priv = netdev_priv(dev);
  451. unsigned int nopaged_len = skb_headlen(skb);
  452. unsigned int txsize = priv->tx_ring_size;
  453. int nfrags = skb_shinfo(skb)->nr_frags;
  454. struct tse_buffer *buffer = NULL;
  455. netdev_tx_t ret = NETDEV_TX_OK;
  456. dma_addr_t dma_addr;
  457. unsigned int entry;
  458. spin_lock_bh(&priv->tx_lock);
  459. if (unlikely(tse_tx_avail(priv) < nfrags + 1)) {
  460. if (!netif_queue_stopped(dev)) {
  461. netif_stop_queue(dev);
  462. /* This is a hard error, log it. */
  463. netdev_err(priv->dev,
  464. "%s: Tx list full when queue awake\n",
  465. __func__);
  466. }
  467. ret = NETDEV_TX_BUSY;
  468. goto out;
  469. }
  470. /* Map the first skb fragment */
  471. entry = priv->tx_prod % txsize;
  472. buffer = &priv->tx_ring[entry];
  473. dma_addr = dma_map_single(priv->device, skb->data, nopaged_len,
  474. DMA_TO_DEVICE);
  475. if (dma_mapping_error(priv->device, dma_addr)) {
  476. netdev_err(priv->dev, "%s: DMA mapping error\n", __func__);
  477. dev_kfree_skb_any(skb);
  478. ret = NETDEV_TX_OK;
  479. goto out;
  480. }
  481. buffer->skb = skb;
  482. buffer->dma_addr = dma_addr;
  483. buffer->len = nopaged_len;
  484. priv->dmaops->tx_buffer(priv, buffer);
  485. skb_tx_timestamp(skb);
  486. priv->tx_prod++;
  487. dev->stats.tx_bytes += skb->len;
  488. if (unlikely(tse_tx_avail(priv) <= TXQUEUESTOP_THRESHHOLD)) {
  489. if (netif_msg_hw(priv))
  490. netdev_dbg(priv->dev, "%s: stop transmitted packets\n",
  491. __func__);
  492. netif_stop_queue(dev);
  493. }
  494. out:
  495. spin_unlock_bh(&priv->tx_lock);
  496. return ret;
  497. }
  498. static int altera_tse_phy_get_addr_mdio_create(struct net_device *dev)
  499. {
  500. struct altera_tse_private *priv = netdev_priv(dev);
  501. struct device_node *np = priv->device->of_node;
  502. int ret;
  503. ret = of_get_phy_mode(np, &priv->phy_iface);
  504. /* Avoid get phy addr and create mdio if no phy is present */
  505. if (ret)
  506. return 0;
  507. /* try to get PHY address from device tree, use PHY autodetection if
  508. * no valid address is given
  509. */
  510. if (of_property_read_u32(priv->device->of_node, "phy-addr",
  511. &priv->phy_addr)) {
  512. priv->phy_addr = POLL_PHY;
  513. }
  514. if (!((priv->phy_addr == POLL_PHY) ||
  515. ((priv->phy_addr >= 0) && (priv->phy_addr < PHY_MAX_ADDR)))) {
  516. netdev_err(dev, "invalid phy-addr specified %d\n",
  517. priv->phy_addr);
  518. return -ENODEV;
  519. }
  520. /* Create/attach to MDIO bus */
  521. ret = altera_tse_mdio_create(dev,
  522. atomic_add_return(1, &instance_count));
  523. if (ret)
  524. return -ENODEV;
  525. return 0;
  526. }
  527. static void tse_update_mac_addr(struct altera_tse_private *priv, const u8 *addr)
  528. {
  529. u32 msb;
  530. u32 lsb;
  531. msb = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0];
  532. lsb = ((addr[5] << 8) | addr[4]) & 0xffff;
  533. /* Set primary MAC address */
  534. csrwr32(msb, priv->mac_dev, tse_csroffs(mac_addr_0));
  535. csrwr32(lsb, priv->mac_dev, tse_csroffs(mac_addr_1));
  536. }
  537. /* MAC software reset.
  538. * When reset is triggered, the MAC function completes the current
  539. * transmission or reception, and subsequently disables the transmit and
  540. * receive logic, flushes the receive FIFO buffer, and resets the statistics
  541. * counters.
  542. */
  543. static int reset_mac(struct altera_tse_private *priv)
  544. {
  545. int counter;
  546. u32 dat;
  547. dat = csrrd32(priv->mac_dev, tse_csroffs(command_config));
  548. dat &= ~(MAC_CMDCFG_TX_ENA | MAC_CMDCFG_RX_ENA);
  549. dat |= MAC_CMDCFG_SW_RESET | MAC_CMDCFG_CNT_RESET;
  550. csrwr32(dat, priv->mac_dev, tse_csroffs(command_config));
  551. counter = 0;
  552. while (counter++ < ALTERA_TSE_SW_RESET_WATCHDOG_CNTR) {
  553. if (tse_bit_is_clear(priv->mac_dev, tse_csroffs(command_config),
  554. MAC_CMDCFG_SW_RESET))
  555. break;
  556. udelay(1);
  557. }
  558. if (counter >= ALTERA_TSE_SW_RESET_WATCHDOG_CNTR) {
  559. dat = csrrd32(priv->mac_dev, tse_csroffs(command_config));
  560. dat &= ~MAC_CMDCFG_SW_RESET;
  561. csrwr32(dat, priv->mac_dev, tse_csroffs(command_config));
  562. return -1;
  563. }
  564. return 0;
  565. }
  566. /* Initialize MAC core registers
  567. */
  568. static int init_mac(struct altera_tse_private *priv)
  569. {
  570. unsigned int cmd = 0;
  571. u32 frm_length;
  572. /* Setup Rx FIFO */
  573. csrwr32(priv->rx_fifo_depth - ALTERA_TSE_RX_SECTION_EMPTY,
  574. priv->mac_dev, tse_csroffs(rx_section_empty));
  575. csrwr32(ALTERA_TSE_RX_SECTION_FULL, priv->mac_dev,
  576. tse_csroffs(rx_section_full));
  577. csrwr32(ALTERA_TSE_RX_ALMOST_EMPTY, priv->mac_dev,
  578. tse_csroffs(rx_almost_empty));
  579. csrwr32(ALTERA_TSE_RX_ALMOST_FULL, priv->mac_dev,
  580. tse_csroffs(rx_almost_full));
  581. /* Setup Tx FIFO */
  582. csrwr32(priv->tx_fifo_depth - ALTERA_TSE_TX_SECTION_EMPTY,
  583. priv->mac_dev, tse_csroffs(tx_section_empty));
  584. csrwr32(ALTERA_TSE_TX_SECTION_FULL, priv->mac_dev,
  585. tse_csroffs(tx_section_full));
  586. csrwr32(ALTERA_TSE_TX_ALMOST_EMPTY, priv->mac_dev,
  587. tse_csroffs(tx_almost_empty));
  588. csrwr32(ALTERA_TSE_TX_ALMOST_FULL, priv->mac_dev,
  589. tse_csroffs(tx_almost_full));
  590. /* MAC Address Configuration */
  591. tse_update_mac_addr(priv, priv->dev->dev_addr);
  592. /* MAC Function Configuration */
  593. frm_length = ETH_HLEN + priv->dev->mtu + ETH_FCS_LEN;
  594. csrwr32(frm_length, priv->mac_dev, tse_csroffs(frm_length));
  595. csrwr32(ALTERA_TSE_TX_IPG_LENGTH, priv->mac_dev,
  596. tse_csroffs(tx_ipg_length));
  597. /* Disable RX/TX shift 16 for alignment of all received frames on 16-bit
  598. * start address
  599. */
  600. tse_set_bit(priv->mac_dev, tse_csroffs(rx_cmd_stat),
  601. ALTERA_TSE_RX_CMD_STAT_RX_SHIFT16);
  602. tse_clear_bit(priv->mac_dev, tse_csroffs(tx_cmd_stat),
  603. ALTERA_TSE_TX_CMD_STAT_TX_SHIFT16 |
  604. ALTERA_TSE_TX_CMD_STAT_OMIT_CRC);
  605. /* Set the MAC options */
  606. cmd = csrrd32(priv->mac_dev, tse_csroffs(command_config));
  607. cmd &= ~MAC_CMDCFG_PAD_EN; /* No padding Removal on Receive */
  608. cmd &= ~MAC_CMDCFG_CRC_FWD; /* CRC Removal */
  609. cmd |= MAC_CMDCFG_RX_ERR_DISC; /* Automatically discard frames
  610. * with CRC errors
  611. */
  612. cmd |= MAC_CMDCFG_CNTL_FRM_ENA;
  613. cmd &= ~MAC_CMDCFG_TX_ENA;
  614. cmd &= ~MAC_CMDCFG_RX_ENA;
  615. /* Default speed and duplex setting, full/100 */
  616. cmd &= ~MAC_CMDCFG_HD_ENA;
  617. cmd &= ~MAC_CMDCFG_ETH_SPEED;
  618. cmd &= ~MAC_CMDCFG_ENA_10;
  619. csrwr32(cmd, priv->mac_dev, tse_csroffs(command_config));
  620. csrwr32(ALTERA_TSE_PAUSE_QUANTA, priv->mac_dev,
  621. tse_csroffs(pause_quanta));
  622. if (netif_msg_hw(priv))
  623. dev_dbg(priv->device,
  624. "MAC post-initialization: CMD_CONFIG = 0x%08x\n", cmd);
  625. return 0;
  626. }
  627. /* Start/stop MAC transmission logic
  628. */
  629. static void tse_set_mac(struct altera_tse_private *priv, bool enable)
  630. {
  631. u32 value = csrrd32(priv->mac_dev, tse_csroffs(command_config));
  632. if (enable)
  633. value |= MAC_CMDCFG_TX_ENA | MAC_CMDCFG_RX_ENA;
  634. else
  635. value &= ~(MAC_CMDCFG_TX_ENA | MAC_CMDCFG_RX_ENA);
  636. csrwr32(value, priv->mac_dev, tse_csroffs(command_config));
  637. }
  638. /* Change the MTU
  639. */
  640. static int tse_change_mtu(struct net_device *dev, int new_mtu)
  641. {
  642. if (netif_running(dev)) {
  643. netdev_err(dev, "must be stopped to change its MTU\n");
  644. return -EBUSY;
  645. }
  646. WRITE_ONCE(dev->mtu, new_mtu);
  647. netdev_update_features(dev);
  648. return 0;
  649. }
  650. static void altera_tse_set_mcfilter(struct net_device *dev)
  651. {
  652. struct altera_tse_private *priv = netdev_priv(dev);
  653. struct netdev_hw_addr *ha;
  654. int i;
  655. /* clear the hash filter */
  656. for (i = 0; i < 64; i++)
  657. csrwr32(0, priv->mac_dev, tse_csroffs(hash_table) + i * 4);
  658. netdev_for_each_mc_addr(ha, dev) {
  659. unsigned int hash = 0;
  660. int mac_octet;
  661. for (mac_octet = 5; mac_octet >= 0; mac_octet--) {
  662. unsigned char xor_bit = 0;
  663. unsigned char octet = ha->addr[mac_octet];
  664. unsigned int bitshift;
  665. for (bitshift = 0; bitshift < 8; bitshift++)
  666. xor_bit ^= ((octet >> bitshift) & 0x01);
  667. hash = (hash << 1) | xor_bit;
  668. }
  669. csrwr32(1, priv->mac_dev, tse_csroffs(hash_table) + hash * 4);
  670. }
  671. }
  672. static void altera_tse_set_mcfilterall(struct net_device *dev)
  673. {
  674. struct altera_tse_private *priv = netdev_priv(dev);
  675. int i;
  676. /* set the hash filter */
  677. for (i = 0; i < 64; i++)
  678. csrwr32(1, priv->mac_dev, tse_csroffs(hash_table) + i * 4);
  679. }
  680. /* Set or clear the multicast filter for this adapter
  681. */
  682. static void tse_set_rx_mode_hashfilter(struct net_device *dev)
  683. {
  684. struct altera_tse_private *priv = netdev_priv(dev);
  685. spin_lock(&priv->mac_cfg_lock);
  686. if (dev->flags & IFF_PROMISC)
  687. tse_set_bit(priv->mac_dev, tse_csroffs(command_config),
  688. MAC_CMDCFG_PROMIS_EN);
  689. if (dev->flags & IFF_ALLMULTI)
  690. altera_tse_set_mcfilterall(dev);
  691. else
  692. altera_tse_set_mcfilter(dev);
  693. spin_unlock(&priv->mac_cfg_lock);
  694. }
  695. /* Set or clear the multicast filter for this adapter
  696. */
  697. static void tse_set_rx_mode(struct net_device *dev)
  698. {
  699. struct altera_tse_private *priv = netdev_priv(dev);
  700. spin_lock(&priv->mac_cfg_lock);
  701. if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI) ||
  702. !netdev_mc_empty(dev) || !netdev_uc_empty(dev))
  703. tse_set_bit(priv->mac_dev, tse_csroffs(command_config),
  704. MAC_CMDCFG_PROMIS_EN);
  705. else
  706. tse_clear_bit(priv->mac_dev, tse_csroffs(command_config),
  707. MAC_CMDCFG_PROMIS_EN);
  708. spin_unlock(&priv->mac_cfg_lock);
  709. }
  710. /* Open and initialize the interface
  711. */
  712. static int tse_open(struct net_device *dev)
  713. {
  714. struct altera_tse_private *priv = netdev_priv(dev);
  715. unsigned long flags;
  716. int ret = 0;
  717. int i;
  718. /* Reset and configure TSE MAC and probe associated PHY */
  719. ret = priv->dmaops->init_dma(priv);
  720. if (ret != 0) {
  721. netdev_err(dev, "Cannot initialize DMA\n");
  722. goto phy_error;
  723. }
  724. if (netif_msg_ifup(priv))
  725. netdev_warn(dev, "device MAC address %pM\n",
  726. dev->dev_addr);
  727. spin_lock(&priv->mac_cfg_lock);
  728. ret = reset_mac(priv);
  729. /* Note that reset_mac will fail if the clocks are gated by the PHY
  730. * due to the PHY being put into isolation or power down mode.
  731. * This is not an error if reset fails due to no clock.
  732. */
  733. if (ret)
  734. netdev_dbg(dev, "Cannot reset MAC core (error: %d)\n", ret);
  735. ret = init_mac(priv);
  736. spin_unlock(&priv->mac_cfg_lock);
  737. if (ret) {
  738. netdev_err(dev, "Cannot init MAC core (error: %d)\n", ret);
  739. goto alloc_skbuf_error;
  740. }
  741. priv->dmaops->reset_dma(priv);
  742. /* Create and initialize the TX/RX descriptors chains. */
  743. priv->rx_ring_size = dma_rx_num;
  744. priv->tx_ring_size = dma_tx_num;
  745. ret = alloc_init_skbufs(priv);
  746. if (ret) {
  747. netdev_err(dev, "DMA descriptors initialization failed\n");
  748. goto alloc_skbuf_error;
  749. }
  750. /* Register RX interrupt */
  751. ret = request_irq(priv->rx_irq, altera_isr, IRQF_SHARED,
  752. dev->name, dev);
  753. if (ret) {
  754. netdev_err(dev, "Unable to register RX interrupt %d\n",
  755. priv->rx_irq);
  756. goto init_error;
  757. }
  758. /* Register TX interrupt */
  759. ret = request_irq(priv->tx_irq, altera_isr, IRQF_SHARED,
  760. dev->name, dev);
  761. if (ret) {
  762. netdev_err(dev, "Unable to register TX interrupt %d\n",
  763. priv->tx_irq);
  764. goto tx_request_irq_error;
  765. }
  766. /* Enable DMA interrupts */
  767. spin_lock_irqsave(&priv->rxdma_irq_lock, flags);
  768. priv->dmaops->enable_rxirq(priv);
  769. priv->dmaops->enable_txirq(priv);
  770. /* Setup RX descriptor chain */
  771. for (i = 0; i < priv->rx_ring_size; i++)
  772. priv->dmaops->add_rx_desc(priv, &priv->rx_ring[i]);
  773. spin_unlock_irqrestore(&priv->rxdma_irq_lock, flags);
  774. ret = phylink_of_phy_connect(priv->phylink, priv->device->of_node, 0);
  775. if (ret) {
  776. netdev_err(dev, "could not connect phylink (%d)\n", ret);
  777. goto tx_request_irq_error;
  778. }
  779. phylink_start(priv->phylink);
  780. napi_enable(&priv->napi);
  781. netif_start_queue(dev);
  782. priv->dmaops->start_rxdma(priv);
  783. /* Start MAC Rx/Tx */
  784. spin_lock(&priv->mac_cfg_lock);
  785. tse_set_mac(priv, true);
  786. spin_unlock(&priv->mac_cfg_lock);
  787. return 0;
  788. tx_request_irq_error:
  789. free_irq(priv->rx_irq, dev);
  790. init_error:
  791. free_skbufs(dev);
  792. alloc_skbuf_error:
  793. phy_error:
  794. return ret;
  795. }
  796. /* Stop TSE MAC interface and put the device in an inactive state
  797. */
  798. static int tse_shutdown(struct net_device *dev)
  799. {
  800. struct altera_tse_private *priv = netdev_priv(dev);
  801. unsigned long int flags;
  802. int ret;
  803. phylink_stop(priv->phylink);
  804. phylink_disconnect_phy(priv->phylink);
  805. netif_stop_queue(dev);
  806. napi_disable(&priv->napi);
  807. /* Disable DMA interrupts */
  808. spin_lock_irqsave(&priv->rxdma_irq_lock, flags);
  809. priv->dmaops->disable_rxirq(priv);
  810. priv->dmaops->disable_txirq(priv);
  811. spin_unlock_irqrestore(&priv->rxdma_irq_lock, flags);
  812. /* Free the IRQ lines */
  813. free_irq(priv->rx_irq, dev);
  814. free_irq(priv->tx_irq, dev);
  815. /* disable and reset the MAC, empties fifo */
  816. spin_lock(&priv->mac_cfg_lock);
  817. spin_lock(&priv->tx_lock);
  818. ret = reset_mac(priv);
  819. /* Note that reset_mac will fail if the clocks are gated by the PHY
  820. * due to the PHY being put into isolation or power down mode.
  821. * This is not an error if reset fails due to no clock.
  822. */
  823. if (ret)
  824. netdev_dbg(dev, "Cannot reset MAC core (error: %d)\n", ret);
  825. priv->dmaops->reset_dma(priv);
  826. free_skbufs(dev);
  827. spin_unlock(&priv->tx_lock);
  828. spin_unlock(&priv->mac_cfg_lock);
  829. priv->dmaops->uninit_dma(priv);
  830. return 0;
  831. }
  832. static struct net_device_ops altera_tse_netdev_ops = {
  833. .ndo_open = tse_open,
  834. .ndo_stop = tse_shutdown,
  835. .ndo_start_xmit = tse_start_xmit,
  836. .ndo_set_mac_address = eth_mac_addr,
  837. .ndo_set_rx_mode = tse_set_rx_mode,
  838. .ndo_change_mtu = tse_change_mtu,
  839. .ndo_validate_addr = eth_validate_addr,
  840. };
  841. static void alt_tse_mac_config(struct phylink_config *config, unsigned int mode,
  842. const struct phylink_link_state *state)
  843. {
  844. struct net_device *ndev = to_net_dev(config->dev);
  845. struct altera_tse_private *priv = netdev_priv(ndev);
  846. spin_lock(&priv->mac_cfg_lock);
  847. reset_mac(priv);
  848. tse_set_mac(priv, true);
  849. spin_unlock(&priv->mac_cfg_lock);
  850. }
  851. static void alt_tse_mac_link_down(struct phylink_config *config,
  852. unsigned int mode, phy_interface_t interface)
  853. {
  854. }
  855. static void alt_tse_mac_link_up(struct phylink_config *config,
  856. struct phy_device *phy, unsigned int mode,
  857. phy_interface_t interface, int speed,
  858. int duplex, bool tx_pause, bool rx_pause)
  859. {
  860. struct net_device *ndev = to_net_dev(config->dev);
  861. struct altera_tse_private *priv = netdev_priv(ndev);
  862. u32 ctrl;
  863. ctrl = csrrd32(priv->mac_dev, tse_csroffs(command_config));
  864. ctrl &= ~(MAC_CMDCFG_ENA_10 | MAC_CMDCFG_ETH_SPEED | MAC_CMDCFG_HD_ENA);
  865. if (duplex == DUPLEX_HALF)
  866. ctrl |= MAC_CMDCFG_HD_ENA;
  867. if (speed == SPEED_1000)
  868. ctrl |= MAC_CMDCFG_ETH_SPEED;
  869. else if (speed == SPEED_10)
  870. ctrl |= MAC_CMDCFG_ENA_10;
  871. spin_lock(&priv->mac_cfg_lock);
  872. csrwr32(ctrl, priv->mac_dev, tse_csroffs(command_config));
  873. spin_unlock(&priv->mac_cfg_lock);
  874. }
  875. static struct phylink_pcs *alt_tse_select_pcs(struct phylink_config *config,
  876. phy_interface_t interface)
  877. {
  878. struct net_device *ndev = to_net_dev(config->dev);
  879. struct altera_tse_private *priv = netdev_priv(ndev);
  880. if (interface == PHY_INTERFACE_MODE_SGMII ||
  881. interface == PHY_INTERFACE_MODE_1000BASEX)
  882. return priv->pcs;
  883. else
  884. return NULL;
  885. }
  886. static const struct phylink_mac_ops alt_tse_phylink_ops = {
  887. .mac_config = alt_tse_mac_config,
  888. .mac_link_down = alt_tse_mac_link_down,
  889. .mac_link_up = alt_tse_mac_link_up,
  890. .mac_select_pcs = alt_tse_select_pcs,
  891. };
  892. static int request_and_map(struct platform_device *pdev, const char *name,
  893. struct resource **res, void __iomem **ptr)
  894. {
  895. struct device *device = &pdev->dev;
  896. struct resource *region;
  897. *res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
  898. if (*res == NULL) {
  899. dev_err(device, "resource %s not defined\n", name);
  900. return -ENODEV;
  901. }
  902. region = devm_request_mem_region(device, (*res)->start,
  903. resource_size(*res), dev_name(device));
  904. if (region == NULL) {
  905. dev_err(device, "unable to request %s\n", name);
  906. return -EBUSY;
  907. }
  908. *ptr = devm_ioremap(device, region->start,
  909. resource_size(region));
  910. if (*ptr == NULL) {
  911. dev_err(device, "ioremap of %s failed!", name);
  912. return -ENOMEM;
  913. }
  914. return 0;
  915. }
  916. /* Probe Altera TSE MAC device
  917. */
  918. static int altera_tse_probe(struct platform_device *pdev)
  919. {
  920. struct regmap_config pcs_regmap_cfg;
  921. struct altera_tse_private *priv;
  922. struct mdio_regmap_config mrc;
  923. struct resource *control_port;
  924. struct regmap *pcs_regmap;
  925. struct resource *dma_res;
  926. struct resource *pcs_res;
  927. struct mii_bus *pcs_bus;
  928. struct net_device *ndev;
  929. void __iomem *descmap;
  930. int ret = -ENODEV;
  931. u32 revision;
  932. ndev = alloc_etherdev(sizeof(struct altera_tse_private));
  933. if (!ndev) {
  934. dev_err(&pdev->dev, "Could not allocate network device\n");
  935. return -ENODEV;
  936. }
  937. SET_NETDEV_DEV(ndev, &pdev->dev);
  938. platform_set_drvdata(pdev, ndev);
  939. priv = netdev_priv(ndev);
  940. priv->device = &pdev->dev;
  941. priv->dev = ndev;
  942. priv->msg_enable = netif_msg_init(debug, default_msg_level);
  943. priv->dmaops = device_get_match_data(&pdev->dev);
  944. if (priv->dmaops &&
  945. priv->dmaops->altera_dtype == ALTERA_DTYPE_SGDMA) {
  946. /* Get the mapped address to the SGDMA descriptor memory */
  947. ret = request_and_map(pdev, "s1", &dma_res, &descmap);
  948. if (ret)
  949. goto err_free_netdev;
  950. /* Start of that memory is for transmit descriptors */
  951. priv->tx_dma_desc = descmap;
  952. /* First half is for tx descriptors, other half for tx */
  953. priv->txdescmem = resource_size(dma_res)/2;
  954. priv->txdescmem_busaddr = (dma_addr_t)dma_res->start;
  955. priv->rx_dma_desc = (void __iomem *)((uintptr_t)(descmap +
  956. priv->txdescmem));
  957. priv->rxdescmem = resource_size(dma_res)/2;
  958. priv->rxdescmem_busaddr = dma_res->start;
  959. priv->rxdescmem_busaddr += priv->txdescmem;
  960. if (upper_32_bits(priv->rxdescmem_busaddr)) {
  961. dev_dbg(priv->device,
  962. "SGDMA bus addresses greater than 32-bits\n");
  963. ret = -EINVAL;
  964. goto err_free_netdev;
  965. }
  966. if (upper_32_bits(priv->txdescmem_busaddr)) {
  967. dev_dbg(priv->device,
  968. "SGDMA bus addresses greater than 32-bits\n");
  969. ret = -EINVAL;
  970. goto err_free_netdev;
  971. }
  972. } else if (priv->dmaops &&
  973. priv->dmaops->altera_dtype == ALTERA_DTYPE_MSGDMA) {
  974. ret = request_and_map(pdev, "rx_resp", &dma_res,
  975. &priv->rx_dma_resp);
  976. if (ret)
  977. goto err_free_netdev;
  978. ret = request_and_map(pdev, "tx_desc", &dma_res,
  979. &priv->tx_dma_desc);
  980. if (ret)
  981. goto err_free_netdev;
  982. priv->txdescmem = resource_size(dma_res);
  983. priv->txdescmem_busaddr = dma_res->start;
  984. ret = request_and_map(pdev, "rx_desc", &dma_res,
  985. &priv->rx_dma_desc);
  986. if (ret)
  987. goto err_free_netdev;
  988. priv->rxdescmem = resource_size(dma_res);
  989. priv->rxdescmem_busaddr = dma_res->start;
  990. } else {
  991. ret = -ENODEV;
  992. goto err_free_netdev;
  993. }
  994. if (!dma_set_mask(priv->device, DMA_BIT_MASK(priv->dmaops->dmamask))) {
  995. dma_set_coherent_mask(priv->device,
  996. DMA_BIT_MASK(priv->dmaops->dmamask));
  997. } else if (!dma_set_mask(priv->device, DMA_BIT_MASK(32))) {
  998. dma_set_coherent_mask(priv->device, DMA_BIT_MASK(32));
  999. } else {
  1000. ret = -EIO;
  1001. goto err_free_netdev;
  1002. }
  1003. /* MAC address space */
  1004. ret = request_and_map(pdev, "control_port", &control_port,
  1005. (void __iomem **)&priv->mac_dev);
  1006. if (ret)
  1007. goto err_free_netdev;
  1008. /* xSGDMA Rx Dispatcher address space */
  1009. ret = request_and_map(pdev, "rx_csr", &dma_res,
  1010. &priv->rx_dma_csr);
  1011. if (ret)
  1012. goto err_free_netdev;
  1013. /* xSGDMA Tx Dispatcher address space */
  1014. ret = request_and_map(pdev, "tx_csr", &dma_res,
  1015. &priv->tx_dma_csr);
  1016. if (ret)
  1017. goto err_free_netdev;
  1018. memset(&pcs_regmap_cfg, 0, sizeof(pcs_regmap_cfg));
  1019. memset(&mrc, 0, sizeof(mrc));
  1020. /* SGMII PCS address space. The location can vary depending on how the
  1021. * IP is integrated. We can have a resource dedicated to it at a specific
  1022. * address space, but if it's not the case, we fallback to the mdiophy0
  1023. * from the MAC's address space
  1024. */
  1025. ret = request_and_map(pdev, "pcs", &pcs_res, &priv->pcs_base);
  1026. if (ret) {
  1027. /* If we can't find a dedicated resource for the PCS, fallback
  1028. * to the internal PCS, that has a different address stride
  1029. */
  1030. priv->pcs_base = priv->mac_dev + tse_csroffs(mdio_phy0);
  1031. pcs_regmap_cfg.reg_bits = 32;
  1032. /* Values are MDIO-like values, on 16 bits */
  1033. pcs_regmap_cfg.val_bits = 16;
  1034. pcs_regmap_cfg.reg_shift = REGMAP_UPSHIFT(2);
  1035. } else {
  1036. pcs_regmap_cfg.reg_bits = 16;
  1037. pcs_regmap_cfg.val_bits = 16;
  1038. pcs_regmap_cfg.reg_shift = REGMAP_UPSHIFT(1);
  1039. }
  1040. /* Create a regmap for the PCS so that it can be used by the PCS driver */
  1041. pcs_regmap = devm_regmap_init_mmio(&pdev->dev, priv->pcs_base,
  1042. &pcs_regmap_cfg);
  1043. if (IS_ERR(pcs_regmap)) {
  1044. ret = PTR_ERR(pcs_regmap);
  1045. goto err_free_netdev;
  1046. }
  1047. mrc.regmap = pcs_regmap;
  1048. mrc.parent = &pdev->dev;
  1049. mrc.valid_addr = 0x0;
  1050. mrc.autoscan = false;
  1051. /* Rx IRQ */
  1052. priv->rx_irq = platform_get_irq_byname(pdev, "rx_irq");
  1053. if (priv->rx_irq == -ENXIO) {
  1054. dev_err(&pdev->dev, "cannot obtain Rx IRQ\n");
  1055. ret = -ENXIO;
  1056. goto err_free_netdev;
  1057. }
  1058. /* Tx IRQ */
  1059. priv->tx_irq = platform_get_irq_byname(pdev, "tx_irq");
  1060. if (priv->tx_irq == -ENXIO) {
  1061. dev_err(&pdev->dev, "cannot obtain Tx IRQ\n");
  1062. ret = -ENXIO;
  1063. goto err_free_netdev;
  1064. }
  1065. /* get FIFO depths from device tree */
  1066. if (of_property_read_u32(pdev->dev.of_node, "rx-fifo-depth",
  1067. &priv->rx_fifo_depth)) {
  1068. dev_err(&pdev->dev, "cannot obtain rx-fifo-depth\n");
  1069. ret = -ENXIO;
  1070. goto err_free_netdev;
  1071. }
  1072. if (of_property_read_u32(pdev->dev.of_node, "tx-fifo-depth",
  1073. &priv->tx_fifo_depth)) {
  1074. dev_err(&pdev->dev, "cannot obtain tx-fifo-depth\n");
  1075. ret = -ENXIO;
  1076. goto err_free_netdev;
  1077. }
  1078. /* get hash filter settings for this instance */
  1079. priv->hash_filter =
  1080. of_property_read_bool(pdev->dev.of_node,
  1081. "altr,has-hash-multicast-filter");
  1082. /* Set hash filter to not set for now until the
  1083. * multicast filter receive issue is debugged
  1084. */
  1085. priv->hash_filter = 0;
  1086. /* get supplemental address settings for this instance */
  1087. priv->added_unicast =
  1088. of_property_read_bool(pdev->dev.of_node,
  1089. "altr,has-supplementary-unicast");
  1090. priv->dev->min_mtu = ETH_ZLEN + ETH_FCS_LEN;
  1091. /* Max MTU is 1500, ETH_DATA_LEN */
  1092. priv->dev->max_mtu = ETH_DATA_LEN;
  1093. /* Get the max mtu from the device tree. Note that the
  1094. * "max-frame-size" parameter is actually max mtu. Definition
  1095. * in the ePAPR v1.1 spec and usage differ, so go with usage.
  1096. */
  1097. of_property_read_u32(pdev->dev.of_node, "max-frame-size",
  1098. &priv->dev->max_mtu);
  1099. /* The DMA buffer size already accounts for an alignment bias
  1100. * to avoid unaligned access exceptions for the NIOS processor,
  1101. */
  1102. priv->rx_dma_buf_sz = ALTERA_RXDMABUFFER_SIZE;
  1103. /* get default MAC address from device tree */
  1104. ret = of_get_ethdev_address(pdev->dev.of_node, ndev);
  1105. if (ret)
  1106. eth_hw_addr_random(ndev);
  1107. /* get phy addr and create mdio */
  1108. ret = altera_tse_phy_get_addr_mdio_create(ndev);
  1109. if (ret)
  1110. goto err_free_netdev;
  1111. /* initialize netdev */
  1112. ndev->mem_start = control_port->start;
  1113. ndev->mem_end = control_port->end;
  1114. ndev->netdev_ops = &altera_tse_netdev_ops;
  1115. altera_tse_set_ethtool_ops(ndev);
  1116. altera_tse_netdev_ops.ndo_set_rx_mode = tse_set_rx_mode;
  1117. if (priv->hash_filter)
  1118. altera_tse_netdev_ops.ndo_set_rx_mode =
  1119. tse_set_rx_mode_hashfilter;
  1120. /* Scatter/gather IO is not supported,
  1121. * so it is turned off
  1122. */
  1123. ndev->hw_features &= ~NETIF_F_SG;
  1124. ndev->features |= ndev->hw_features | NETIF_F_HIGHDMA;
  1125. /* VLAN offloading of tagging, stripping and filtering is not
  1126. * supported by hardware, but driver will accommodate the
  1127. * extra 4-byte VLAN tag for processing by upper layers
  1128. */
  1129. ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
  1130. /* setup NAPI interface */
  1131. netif_napi_add(ndev, &priv->napi, tse_poll);
  1132. spin_lock_init(&priv->mac_cfg_lock);
  1133. spin_lock_init(&priv->tx_lock);
  1134. spin_lock_init(&priv->rxdma_irq_lock);
  1135. snprintf(mrc.name, MII_BUS_ID_SIZE, "%s-pcs-mii", dev_name(&pdev->dev));
  1136. pcs_bus = devm_mdio_regmap_register(&pdev->dev, &mrc);
  1137. if (IS_ERR(pcs_bus)) {
  1138. ret = PTR_ERR(pcs_bus);
  1139. goto err_init_pcs;
  1140. }
  1141. priv->pcs = lynx_pcs_create_mdiodev(pcs_bus, 0);
  1142. if (IS_ERR(priv->pcs)) {
  1143. ret = PTR_ERR(priv->pcs);
  1144. goto err_init_pcs;
  1145. }
  1146. priv->phylink_config.dev = &ndev->dev;
  1147. priv->phylink_config.type = PHYLINK_NETDEV;
  1148. priv->phylink_config.mac_capabilities = MAC_SYM_PAUSE | MAC_10 |
  1149. MAC_100 | MAC_1000FD;
  1150. phy_interface_set_rgmii(priv->phylink_config.supported_interfaces);
  1151. __set_bit(PHY_INTERFACE_MODE_MII,
  1152. priv->phylink_config.supported_interfaces);
  1153. __set_bit(PHY_INTERFACE_MODE_GMII,
  1154. priv->phylink_config.supported_interfaces);
  1155. __set_bit(PHY_INTERFACE_MODE_SGMII,
  1156. priv->phylink_config.supported_interfaces);
  1157. __set_bit(PHY_INTERFACE_MODE_1000BASEX,
  1158. priv->phylink_config.supported_interfaces);
  1159. priv->phylink = phylink_create(&priv->phylink_config,
  1160. of_fwnode_handle(priv->device->of_node),
  1161. priv->phy_iface, &alt_tse_phylink_ops);
  1162. if (IS_ERR(priv->phylink)) {
  1163. dev_err(&pdev->dev, "failed to create phylink\n");
  1164. ret = PTR_ERR(priv->phylink);
  1165. goto err_init_phylink;
  1166. }
  1167. ret = register_netdev(ndev);
  1168. if (ret) {
  1169. dev_err(&pdev->dev, "failed to register TSE net device\n");
  1170. goto err_register_netdev;
  1171. }
  1172. revision = ioread32(&priv->mac_dev->megacore_revision);
  1173. if (revision < 0xd00 || revision > 0xe00)
  1174. netdev_warn(ndev, "TSE revision %x\n", revision);
  1175. if (netif_msg_probe(priv))
  1176. dev_info(&pdev->dev, "Altera TSE MAC version %d.%d at 0x%08lx irq %d/%d\n",
  1177. (revision >> 8) & 0xff, revision & 0xff,
  1178. (unsigned long)control_port->start, priv->rx_irq,
  1179. priv->tx_irq);
  1180. return 0;
  1181. err_register_netdev:
  1182. phylink_destroy(priv->phylink);
  1183. err_init_phylink:
  1184. lynx_pcs_destroy(priv->pcs);
  1185. err_init_pcs:
  1186. netif_napi_del(&priv->napi);
  1187. altera_tse_mdio_destroy(ndev);
  1188. err_free_netdev:
  1189. free_netdev(ndev);
  1190. return ret;
  1191. }
  1192. /* Remove Altera TSE MAC device
  1193. */
  1194. static void altera_tse_remove(struct platform_device *pdev)
  1195. {
  1196. struct net_device *ndev = platform_get_drvdata(pdev);
  1197. struct altera_tse_private *priv = netdev_priv(ndev);
  1198. platform_set_drvdata(pdev, NULL);
  1199. altera_tse_mdio_destroy(ndev);
  1200. unregister_netdev(ndev);
  1201. phylink_destroy(priv->phylink);
  1202. lynx_pcs_destroy(priv->pcs);
  1203. free_netdev(ndev);
  1204. }
  1205. static const struct altera_dmaops altera_dtype_sgdma = {
  1206. .altera_dtype = ALTERA_DTYPE_SGDMA,
  1207. .dmamask = 32,
  1208. .reset_dma = sgdma_reset,
  1209. .enable_txirq = sgdma_enable_txirq,
  1210. .enable_rxirq = sgdma_enable_rxirq,
  1211. .disable_txirq = sgdma_disable_txirq,
  1212. .disable_rxirq = sgdma_disable_rxirq,
  1213. .clear_txirq = sgdma_clear_txirq,
  1214. .clear_rxirq = sgdma_clear_rxirq,
  1215. .tx_buffer = sgdma_tx_buffer,
  1216. .tx_completions = sgdma_tx_completions,
  1217. .add_rx_desc = sgdma_add_rx_desc,
  1218. .get_rx_status = sgdma_rx_status,
  1219. .init_dma = sgdma_initialize,
  1220. .uninit_dma = sgdma_uninitialize,
  1221. .start_rxdma = sgdma_start_rxdma,
  1222. };
  1223. static const struct altera_dmaops altera_dtype_msgdma = {
  1224. .altera_dtype = ALTERA_DTYPE_MSGDMA,
  1225. .dmamask = 64,
  1226. .reset_dma = msgdma_reset,
  1227. .enable_txirq = msgdma_enable_txirq,
  1228. .enable_rxirq = msgdma_enable_rxirq,
  1229. .disable_txirq = msgdma_disable_txirq,
  1230. .disable_rxirq = msgdma_disable_rxirq,
  1231. .clear_txirq = msgdma_clear_txirq,
  1232. .clear_rxirq = msgdma_clear_rxirq,
  1233. .tx_buffer = msgdma_tx_buffer,
  1234. .tx_completions = msgdma_tx_completions,
  1235. .add_rx_desc = msgdma_add_rx_desc,
  1236. .get_rx_status = msgdma_rx_status,
  1237. .init_dma = msgdma_initialize,
  1238. .uninit_dma = msgdma_uninitialize,
  1239. .start_rxdma = msgdma_start_rxdma,
  1240. };
  1241. static const struct of_device_id altera_tse_ids[] = {
  1242. { .compatible = "altr,tse-msgdma-1.0", .data = &altera_dtype_msgdma, },
  1243. { .compatible = "altr,tse-1.0", .data = &altera_dtype_sgdma, },
  1244. { .compatible = "ALTR,tse-1.0", .data = &altera_dtype_sgdma, },
  1245. {},
  1246. };
  1247. MODULE_DEVICE_TABLE(of, altera_tse_ids);
  1248. static struct platform_driver altera_tse_driver = {
  1249. .probe = altera_tse_probe,
  1250. .remove = altera_tse_remove,
  1251. .suspend = NULL,
  1252. .resume = NULL,
  1253. .driver = {
  1254. .name = ALTERA_TSE_RESOURCE_NAME,
  1255. .of_match_table = altera_tse_ids,
  1256. },
  1257. };
  1258. module_platform_driver(altera_tse_driver);
  1259. MODULE_AUTHOR("Altera Corporation");
  1260. MODULE_DESCRIPTION("Altera Triple Speed Ethernet MAC driver");
  1261. MODULE_LICENSE("GPL v2");