altera_tse_main.c 44 KB

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