altera_tse_main.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701
  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, S_IRUGO | S_IWUSR);
  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, S_IRUGO | S_IWUSR);
  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, S_IRUGO | S_IWUSR);
  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(napi);
  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. } else {
  602. int ret;
  603. phydev = phy_find_first(priv->mdio);
  604. if (phydev == NULL) {
  605. netdev_err(dev, "No PHY found\n");
  606. return phydev;
  607. }
  608. ret = phy_connect_direct(dev, phydev, &altera_tse_adjust_link,
  609. priv->phy_iface);
  610. if (ret != 0) {
  611. netdev_err(dev, "Could not attach to PHY\n");
  612. phydev = NULL;
  613. }
  614. }
  615. return phydev;
  616. }
  617. static int altera_tse_phy_get_addr_mdio_create(struct net_device *dev)
  618. {
  619. struct altera_tse_private *priv = netdev_priv(dev);
  620. struct device_node *np = priv->device->of_node;
  621. int ret = 0;
  622. priv->phy_iface = of_get_phy_mode(np);
  623. /* Avoid get phy addr and create mdio if no phy is present */
  624. if (!priv->phy_iface)
  625. return 0;
  626. /* try to get PHY address from device tree, use PHY autodetection if
  627. * no valid address is given
  628. */
  629. if (of_property_read_u32(priv->device->of_node, "phy-addr",
  630. &priv->phy_addr)) {
  631. priv->phy_addr = POLL_PHY;
  632. }
  633. if (!((priv->phy_addr == POLL_PHY) ||
  634. ((priv->phy_addr >= 0) && (priv->phy_addr < PHY_MAX_ADDR)))) {
  635. netdev_err(dev, "invalid phy-addr specified %d\n",
  636. priv->phy_addr);
  637. return -ENODEV;
  638. }
  639. /* Create/attach to MDIO bus */
  640. ret = altera_tse_mdio_create(dev,
  641. atomic_add_return(1, &instance_count));
  642. if (ret)
  643. return -ENODEV;
  644. return 0;
  645. }
  646. /* Initialize driver's PHY state, and attach to the PHY
  647. */
  648. static int init_phy(struct net_device *dev)
  649. {
  650. struct altera_tse_private *priv = netdev_priv(dev);
  651. struct phy_device *phydev;
  652. struct device_node *phynode;
  653. bool fixed_link = false;
  654. int rc = 0;
  655. /* Avoid init phy in case of no phy present */
  656. if (!priv->phy_iface)
  657. return 0;
  658. priv->oldlink = 0;
  659. priv->oldspeed = 0;
  660. priv->oldduplex = -1;
  661. phynode = of_parse_phandle(priv->device->of_node, "phy-handle", 0);
  662. if (!phynode) {
  663. /* check if a fixed-link is defined in device-tree */
  664. if (of_phy_is_fixed_link(priv->device->of_node)) {
  665. rc = of_phy_register_fixed_link(priv->device->of_node);
  666. if (rc < 0) {
  667. netdev_err(dev, "cannot register fixed PHY\n");
  668. return rc;
  669. }
  670. /* In the case of a fixed PHY, the DT node associated
  671. * to the PHY is the Ethernet MAC DT node.
  672. */
  673. phynode = of_node_get(priv->device->of_node);
  674. fixed_link = true;
  675. netdev_dbg(dev, "fixed-link detected\n");
  676. phydev = of_phy_connect(dev, phynode,
  677. &altera_tse_adjust_link,
  678. 0, priv->phy_iface);
  679. } else {
  680. netdev_dbg(dev, "no phy-handle found\n");
  681. if (!priv->mdio) {
  682. netdev_err(dev, "No phy-handle nor local mdio specified\n");
  683. return -ENODEV;
  684. }
  685. phydev = connect_local_phy(dev);
  686. }
  687. } else {
  688. netdev_dbg(dev, "phy-handle found\n");
  689. phydev = of_phy_connect(dev, phynode,
  690. &altera_tse_adjust_link, 0, priv->phy_iface);
  691. }
  692. of_node_put(phynode);
  693. if (!phydev) {
  694. netdev_err(dev, "Could not find the PHY\n");
  695. if (fixed_link)
  696. of_phy_deregister_fixed_link(priv->device->of_node);
  697. return -ENODEV;
  698. }
  699. /* Stop Advertising 1000BASE Capability if interface is not GMII
  700. * Note: Checkpatch throws CHECKs for the camel case defines below,
  701. * it's ok to ignore.
  702. */
  703. if ((priv->phy_iface == PHY_INTERFACE_MODE_MII) ||
  704. (priv->phy_iface == PHY_INTERFACE_MODE_RMII))
  705. phydev->advertising &= ~(SUPPORTED_1000baseT_Half |
  706. SUPPORTED_1000baseT_Full);
  707. /* Broken HW is sometimes missing the pull-up resistor on the
  708. * MDIO line, which results in reads to non-existent devices returning
  709. * 0 rather than 0xffff. Catch this here and treat 0 as a non-existent
  710. * device as well. If a fixed-link is used the phy_id is always 0.
  711. * Note: phydev->phy_id is the result of reading the UID PHY registers.
  712. */
  713. if ((phydev->phy_id == 0) && !fixed_link) {
  714. netdev_err(dev, "Bad PHY UID 0x%08x\n", phydev->phy_id);
  715. phy_disconnect(phydev);
  716. return -ENODEV;
  717. }
  718. netdev_dbg(dev, "attached to PHY %d UID 0x%08x Link = %d\n",
  719. phydev->mdio.addr, phydev->phy_id, phydev->link);
  720. return 0;
  721. }
  722. static void tse_update_mac_addr(struct altera_tse_private *priv, u8 *addr)
  723. {
  724. u32 msb;
  725. u32 lsb;
  726. msb = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0];
  727. lsb = ((addr[5] << 8) | addr[4]) & 0xffff;
  728. /* Set primary MAC address */
  729. csrwr32(msb, priv->mac_dev, tse_csroffs(mac_addr_0));
  730. csrwr32(lsb, priv->mac_dev, tse_csroffs(mac_addr_1));
  731. }
  732. /* MAC software reset.
  733. * When reset is triggered, the MAC function completes the current
  734. * transmission or reception, and subsequently disables the transmit and
  735. * receive logic, flushes the receive FIFO buffer, and resets the statistics
  736. * counters.
  737. */
  738. static int reset_mac(struct altera_tse_private *priv)
  739. {
  740. int counter;
  741. u32 dat;
  742. dat = csrrd32(priv->mac_dev, tse_csroffs(command_config));
  743. dat &= ~(MAC_CMDCFG_TX_ENA | MAC_CMDCFG_RX_ENA);
  744. dat |= MAC_CMDCFG_SW_RESET | MAC_CMDCFG_CNT_RESET;
  745. csrwr32(dat, priv->mac_dev, tse_csroffs(command_config));
  746. counter = 0;
  747. while (counter++ < ALTERA_TSE_SW_RESET_WATCHDOG_CNTR) {
  748. if (tse_bit_is_clear(priv->mac_dev, tse_csroffs(command_config),
  749. MAC_CMDCFG_SW_RESET))
  750. break;
  751. udelay(1);
  752. }
  753. if (counter >= ALTERA_TSE_SW_RESET_WATCHDOG_CNTR) {
  754. dat = csrrd32(priv->mac_dev, tse_csroffs(command_config));
  755. dat &= ~MAC_CMDCFG_SW_RESET;
  756. csrwr32(dat, priv->mac_dev, tse_csroffs(command_config));
  757. return -1;
  758. }
  759. return 0;
  760. }
  761. /* Initialize MAC core registers
  762. */
  763. static int init_mac(struct altera_tse_private *priv)
  764. {
  765. unsigned int cmd = 0;
  766. u32 frm_length;
  767. /* Setup Rx FIFO */
  768. csrwr32(priv->rx_fifo_depth - ALTERA_TSE_RX_SECTION_EMPTY,
  769. priv->mac_dev, tse_csroffs(rx_section_empty));
  770. csrwr32(ALTERA_TSE_RX_SECTION_FULL, priv->mac_dev,
  771. tse_csroffs(rx_section_full));
  772. csrwr32(ALTERA_TSE_RX_ALMOST_EMPTY, priv->mac_dev,
  773. tse_csroffs(rx_almost_empty));
  774. csrwr32(ALTERA_TSE_RX_ALMOST_FULL, priv->mac_dev,
  775. tse_csroffs(rx_almost_full));
  776. /* Setup Tx FIFO */
  777. csrwr32(priv->tx_fifo_depth - ALTERA_TSE_TX_SECTION_EMPTY,
  778. priv->mac_dev, tse_csroffs(tx_section_empty));
  779. csrwr32(ALTERA_TSE_TX_SECTION_FULL, priv->mac_dev,
  780. tse_csroffs(tx_section_full));
  781. csrwr32(ALTERA_TSE_TX_ALMOST_EMPTY, priv->mac_dev,
  782. tse_csroffs(tx_almost_empty));
  783. csrwr32(ALTERA_TSE_TX_ALMOST_FULL, priv->mac_dev,
  784. tse_csroffs(tx_almost_full));
  785. /* MAC Address Configuration */
  786. tse_update_mac_addr(priv, priv->dev->dev_addr);
  787. /* MAC Function Configuration */
  788. frm_length = ETH_HLEN + priv->dev->mtu + ETH_FCS_LEN;
  789. csrwr32(frm_length, priv->mac_dev, tse_csroffs(frm_length));
  790. csrwr32(ALTERA_TSE_TX_IPG_LENGTH, priv->mac_dev,
  791. tse_csroffs(tx_ipg_length));
  792. /* Disable RX/TX shift 16 for alignment of all received frames on 16-bit
  793. * start address
  794. */
  795. tse_set_bit(priv->mac_dev, tse_csroffs(rx_cmd_stat),
  796. ALTERA_TSE_RX_CMD_STAT_RX_SHIFT16);
  797. tse_clear_bit(priv->mac_dev, tse_csroffs(tx_cmd_stat),
  798. ALTERA_TSE_TX_CMD_STAT_TX_SHIFT16 |
  799. ALTERA_TSE_TX_CMD_STAT_OMIT_CRC);
  800. /* Set the MAC options */
  801. cmd = csrrd32(priv->mac_dev, tse_csroffs(command_config));
  802. cmd &= ~MAC_CMDCFG_PAD_EN; /* No padding Removal on Receive */
  803. cmd &= ~MAC_CMDCFG_CRC_FWD; /* CRC Removal */
  804. cmd |= MAC_CMDCFG_RX_ERR_DISC; /* Automatically discard frames
  805. * with CRC errors
  806. */
  807. cmd |= MAC_CMDCFG_CNTL_FRM_ENA;
  808. cmd &= ~MAC_CMDCFG_TX_ENA;
  809. cmd &= ~MAC_CMDCFG_RX_ENA;
  810. /* Default speed and duplex setting, full/100 */
  811. cmd &= ~MAC_CMDCFG_HD_ENA;
  812. cmd &= ~MAC_CMDCFG_ETH_SPEED;
  813. cmd &= ~MAC_CMDCFG_ENA_10;
  814. csrwr32(cmd, priv->mac_dev, tse_csroffs(command_config));
  815. csrwr32(ALTERA_TSE_PAUSE_QUANTA, priv->mac_dev,
  816. tse_csroffs(pause_quanta));
  817. if (netif_msg_hw(priv))
  818. dev_dbg(priv->device,
  819. "MAC post-initialization: CMD_CONFIG = 0x%08x\n", cmd);
  820. return 0;
  821. }
  822. /* Start/stop MAC transmission logic
  823. */
  824. static void tse_set_mac(struct altera_tse_private *priv, bool enable)
  825. {
  826. u32 value = csrrd32(priv->mac_dev, tse_csroffs(command_config));
  827. if (enable)
  828. value |= MAC_CMDCFG_TX_ENA | MAC_CMDCFG_RX_ENA;
  829. else
  830. value &= ~(MAC_CMDCFG_TX_ENA | MAC_CMDCFG_RX_ENA);
  831. csrwr32(value, priv->mac_dev, tse_csroffs(command_config));
  832. }
  833. /* Change the MTU
  834. */
  835. static int tse_change_mtu(struct net_device *dev, int new_mtu)
  836. {
  837. if (netif_running(dev)) {
  838. netdev_err(dev, "must be stopped to change its MTU\n");
  839. return -EBUSY;
  840. }
  841. dev->mtu = new_mtu;
  842. netdev_update_features(dev);
  843. return 0;
  844. }
  845. static void altera_tse_set_mcfilter(struct net_device *dev)
  846. {
  847. struct altera_tse_private *priv = netdev_priv(dev);
  848. int i;
  849. struct netdev_hw_addr *ha;
  850. /* clear the hash filter */
  851. for (i = 0; i < 64; i++)
  852. csrwr32(0, priv->mac_dev, tse_csroffs(hash_table) + i * 4);
  853. netdev_for_each_mc_addr(ha, dev) {
  854. unsigned int hash = 0;
  855. int mac_octet;
  856. for (mac_octet = 5; mac_octet >= 0; mac_octet--) {
  857. unsigned char xor_bit = 0;
  858. unsigned char octet = ha->addr[mac_octet];
  859. unsigned int bitshift;
  860. for (bitshift = 0; bitshift < 8; bitshift++)
  861. xor_bit ^= ((octet >> bitshift) & 0x01);
  862. hash = (hash << 1) | xor_bit;
  863. }
  864. csrwr32(1, priv->mac_dev, tse_csroffs(hash_table) + hash * 4);
  865. }
  866. }
  867. static void altera_tse_set_mcfilterall(struct net_device *dev)
  868. {
  869. struct altera_tse_private *priv = netdev_priv(dev);
  870. int i;
  871. /* set the hash filter */
  872. for (i = 0; i < 64; i++)
  873. csrwr32(1, priv->mac_dev, tse_csroffs(hash_table) + i * 4);
  874. }
  875. /* Set or clear the multicast filter for this adaptor
  876. */
  877. static void tse_set_rx_mode_hashfilter(struct net_device *dev)
  878. {
  879. struct altera_tse_private *priv = netdev_priv(dev);
  880. spin_lock(&priv->mac_cfg_lock);
  881. if (dev->flags & IFF_PROMISC)
  882. tse_set_bit(priv->mac_dev, tse_csroffs(command_config),
  883. MAC_CMDCFG_PROMIS_EN);
  884. if (dev->flags & IFF_ALLMULTI)
  885. altera_tse_set_mcfilterall(dev);
  886. else
  887. altera_tse_set_mcfilter(dev);
  888. spin_unlock(&priv->mac_cfg_lock);
  889. }
  890. /* Set or clear the multicast filter for this adaptor
  891. */
  892. static void tse_set_rx_mode(struct net_device *dev)
  893. {
  894. struct altera_tse_private *priv = netdev_priv(dev);
  895. spin_lock(&priv->mac_cfg_lock);
  896. if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI) ||
  897. !netdev_mc_empty(dev) || !netdev_uc_empty(dev))
  898. tse_set_bit(priv->mac_dev, tse_csroffs(command_config),
  899. MAC_CMDCFG_PROMIS_EN);
  900. else
  901. tse_clear_bit(priv->mac_dev, tse_csroffs(command_config),
  902. MAC_CMDCFG_PROMIS_EN);
  903. spin_unlock(&priv->mac_cfg_lock);
  904. }
  905. /* Initialise (if necessary) the SGMII PCS component
  906. */
  907. static int init_sgmii_pcs(struct net_device *dev)
  908. {
  909. struct altera_tse_private *priv = netdev_priv(dev);
  910. int n;
  911. unsigned int tmp_reg = 0;
  912. if (priv->phy_iface != PHY_INTERFACE_MODE_SGMII)
  913. return 0; /* Nothing to do, not in SGMII mode */
  914. /* The TSE SGMII PCS block looks a little like a PHY, it is
  915. * mapped into the zeroth MDIO space of the MAC and it has
  916. * ID registers like a PHY would. Sadly this is often
  917. * configured to zeroes, so don't be surprised if it does
  918. * show 0x00000000.
  919. */
  920. if (sgmii_pcs_scratch_test(priv, 0x0000) &&
  921. sgmii_pcs_scratch_test(priv, 0xffff) &&
  922. sgmii_pcs_scratch_test(priv, 0xa5a5) &&
  923. sgmii_pcs_scratch_test(priv, 0x5a5a)) {
  924. netdev_info(dev, "PCS PHY ID: 0x%04x%04x\n",
  925. sgmii_pcs_read(priv, MII_PHYSID1),
  926. sgmii_pcs_read(priv, MII_PHYSID2));
  927. } else {
  928. netdev_err(dev, "SGMII PCS Scratch memory test failed.\n");
  929. return -ENOMEM;
  930. }
  931. /* Starting on page 5-29 of the MegaCore Function User Guide
  932. * Set SGMII Link timer to 1.6ms
  933. */
  934. sgmii_pcs_write(priv, SGMII_PCS_LINK_TIMER_0, 0x0D40);
  935. sgmii_pcs_write(priv, SGMII_PCS_LINK_TIMER_1, 0x03);
  936. /* Enable SGMII Interface and Enable SGMII Auto Negotiation */
  937. sgmii_pcs_write(priv, SGMII_PCS_IF_MODE, 0x3);
  938. /* Enable Autonegotiation */
  939. tmp_reg = sgmii_pcs_read(priv, MII_BMCR);
  940. tmp_reg |= (BMCR_SPEED1000 | BMCR_FULLDPLX | BMCR_ANENABLE);
  941. sgmii_pcs_write(priv, MII_BMCR, tmp_reg);
  942. /* Reset PCS block */
  943. tmp_reg |= BMCR_RESET;
  944. sgmii_pcs_write(priv, MII_BMCR, tmp_reg);
  945. for (n = 0; n < SGMII_PCS_SW_RESET_TIMEOUT; n++) {
  946. if (!(sgmii_pcs_read(priv, MII_BMCR) & BMCR_RESET)) {
  947. netdev_info(dev, "SGMII PCS block initialised OK\n");
  948. return 0;
  949. }
  950. udelay(1);
  951. }
  952. /* We failed to reset the block, return a timeout */
  953. netdev_err(dev, "SGMII PCS block reset failed.\n");
  954. return -ETIMEDOUT;
  955. }
  956. /* Open and initialize the interface
  957. */
  958. static int tse_open(struct net_device *dev)
  959. {
  960. struct altera_tse_private *priv = netdev_priv(dev);
  961. int ret = 0;
  962. int i;
  963. unsigned long int flags;
  964. /* Reset and configure TSE MAC and probe associated PHY */
  965. ret = priv->dmaops->init_dma(priv);
  966. if (ret != 0) {
  967. netdev_err(dev, "Cannot initialize DMA\n");
  968. goto phy_error;
  969. }
  970. if (netif_msg_ifup(priv))
  971. netdev_warn(dev, "device MAC address %pM\n",
  972. dev->dev_addr);
  973. if ((priv->revision < 0xd00) || (priv->revision > 0xe00))
  974. netdev_warn(dev, "TSE revision %x\n", priv->revision);
  975. spin_lock(&priv->mac_cfg_lock);
  976. /* no-op if MAC not operating in SGMII mode*/
  977. ret = init_sgmii_pcs(dev);
  978. if (ret) {
  979. netdev_err(dev,
  980. "Cannot init the SGMII PCS (error: %d)\n", ret);
  981. spin_unlock(&priv->mac_cfg_lock);
  982. goto phy_error;
  983. }
  984. ret = reset_mac(priv);
  985. /* Note that reset_mac will fail if the clocks are gated by the PHY
  986. * due to the PHY being put into isolation or power down mode.
  987. * This is not an error if reset fails due to no clock.
  988. */
  989. if (ret)
  990. netdev_dbg(dev, "Cannot reset MAC core (error: %d)\n", ret);
  991. ret = init_mac(priv);
  992. spin_unlock(&priv->mac_cfg_lock);
  993. if (ret) {
  994. netdev_err(dev, "Cannot init MAC core (error: %d)\n", ret);
  995. goto alloc_skbuf_error;
  996. }
  997. priv->dmaops->reset_dma(priv);
  998. /* Create and initialize the TX/RX descriptors chains. */
  999. priv->rx_ring_size = dma_rx_num;
  1000. priv->tx_ring_size = dma_tx_num;
  1001. ret = alloc_init_skbufs(priv);
  1002. if (ret) {
  1003. netdev_err(dev, "DMA descriptors initialization failed\n");
  1004. goto alloc_skbuf_error;
  1005. }
  1006. /* Register RX interrupt */
  1007. ret = request_irq(priv->rx_irq, altera_isr, IRQF_SHARED,
  1008. dev->name, dev);
  1009. if (ret) {
  1010. netdev_err(dev, "Unable to register RX interrupt %d\n",
  1011. priv->rx_irq);
  1012. goto init_error;
  1013. }
  1014. /* Register TX interrupt */
  1015. ret = request_irq(priv->tx_irq, altera_isr, IRQF_SHARED,
  1016. dev->name, dev);
  1017. if (ret) {
  1018. netdev_err(dev, "Unable to register TX interrupt %d\n",
  1019. priv->tx_irq);
  1020. goto tx_request_irq_error;
  1021. }
  1022. /* Enable DMA interrupts */
  1023. spin_lock_irqsave(&priv->rxdma_irq_lock, flags);
  1024. priv->dmaops->enable_rxirq(priv);
  1025. priv->dmaops->enable_txirq(priv);
  1026. /* Setup RX descriptor chain */
  1027. for (i = 0; i < priv->rx_ring_size; i++)
  1028. priv->dmaops->add_rx_desc(priv, &priv->rx_ring[i]);
  1029. spin_unlock_irqrestore(&priv->rxdma_irq_lock, flags);
  1030. if (dev->phydev)
  1031. phy_start(dev->phydev);
  1032. napi_enable(&priv->napi);
  1033. netif_start_queue(dev);
  1034. priv->dmaops->start_rxdma(priv);
  1035. /* Start MAC Rx/Tx */
  1036. spin_lock(&priv->mac_cfg_lock);
  1037. tse_set_mac(priv, true);
  1038. spin_unlock(&priv->mac_cfg_lock);
  1039. return 0;
  1040. tx_request_irq_error:
  1041. free_irq(priv->rx_irq, dev);
  1042. init_error:
  1043. free_skbufs(dev);
  1044. alloc_skbuf_error:
  1045. phy_error:
  1046. return ret;
  1047. }
  1048. /* Stop TSE MAC interface and put the device in an inactive state
  1049. */
  1050. static int tse_shutdown(struct net_device *dev)
  1051. {
  1052. struct altera_tse_private *priv = netdev_priv(dev);
  1053. int ret;
  1054. unsigned long int flags;
  1055. /* Stop the PHY */
  1056. if (dev->phydev)
  1057. phy_stop(dev->phydev);
  1058. netif_stop_queue(dev);
  1059. napi_disable(&priv->napi);
  1060. /* Disable DMA interrupts */
  1061. spin_lock_irqsave(&priv->rxdma_irq_lock, flags);
  1062. priv->dmaops->disable_rxirq(priv);
  1063. priv->dmaops->disable_txirq(priv);
  1064. spin_unlock_irqrestore(&priv->rxdma_irq_lock, flags);
  1065. /* Free the IRQ lines */
  1066. free_irq(priv->rx_irq, dev);
  1067. free_irq(priv->tx_irq, dev);
  1068. /* disable and reset the MAC, empties fifo */
  1069. spin_lock(&priv->mac_cfg_lock);
  1070. spin_lock(&priv->tx_lock);
  1071. ret = reset_mac(priv);
  1072. /* Note that reset_mac will fail if the clocks are gated by the PHY
  1073. * due to the PHY being put into isolation or power down mode.
  1074. * This is not an error if reset fails due to no clock.
  1075. */
  1076. if (ret)
  1077. netdev_dbg(dev, "Cannot reset MAC core (error: %d)\n", ret);
  1078. priv->dmaops->reset_dma(priv);
  1079. free_skbufs(dev);
  1080. spin_unlock(&priv->tx_lock);
  1081. spin_unlock(&priv->mac_cfg_lock);
  1082. priv->dmaops->uninit_dma(priv);
  1083. return 0;
  1084. }
  1085. static struct net_device_ops altera_tse_netdev_ops = {
  1086. .ndo_open = tse_open,
  1087. .ndo_stop = tse_shutdown,
  1088. .ndo_start_xmit = tse_start_xmit,
  1089. .ndo_set_mac_address = eth_mac_addr,
  1090. .ndo_set_rx_mode = tse_set_rx_mode,
  1091. .ndo_change_mtu = tse_change_mtu,
  1092. .ndo_validate_addr = eth_validate_addr,
  1093. };
  1094. static int request_and_map(struct platform_device *pdev, const char *name,
  1095. struct resource **res, void __iomem **ptr)
  1096. {
  1097. struct resource *region;
  1098. struct device *device = &pdev->dev;
  1099. *res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
  1100. if (*res == NULL) {
  1101. dev_err(device, "resource %s not defined\n", name);
  1102. return -ENODEV;
  1103. }
  1104. region = devm_request_mem_region(device, (*res)->start,
  1105. resource_size(*res), dev_name(device));
  1106. if (region == NULL) {
  1107. dev_err(device, "unable to request %s\n", name);
  1108. return -EBUSY;
  1109. }
  1110. *ptr = devm_ioremap_nocache(device, region->start,
  1111. resource_size(region));
  1112. if (*ptr == NULL) {
  1113. dev_err(device, "ioremap_nocache of %s failed!", name);
  1114. return -ENOMEM;
  1115. }
  1116. return 0;
  1117. }
  1118. /* Probe Altera TSE MAC device
  1119. */
  1120. static int altera_tse_probe(struct platform_device *pdev)
  1121. {
  1122. struct net_device *ndev;
  1123. int ret = -ENODEV;
  1124. struct resource *control_port;
  1125. struct resource *dma_res;
  1126. struct altera_tse_private *priv;
  1127. const unsigned char *macaddr;
  1128. void __iomem *descmap;
  1129. const struct of_device_id *of_id = NULL;
  1130. ndev = alloc_etherdev(sizeof(struct altera_tse_private));
  1131. if (!ndev) {
  1132. dev_err(&pdev->dev, "Could not allocate network device\n");
  1133. return -ENODEV;
  1134. }
  1135. SET_NETDEV_DEV(ndev, &pdev->dev);
  1136. priv = netdev_priv(ndev);
  1137. priv->device = &pdev->dev;
  1138. priv->dev = ndev;
  1139. priv->msg_enable = netif_msg_init(debug, default_msg_level);
  1140. of_id = of_match_device(altera_tse_ids, &pdev->dev);
  1141. if (of_id)
  1142. priv->dmaops = (struct altera_dmaops *)of_id->data;
  1143. if (priv->dmaops &&
  1144. priv->dmaops->altera_dtype == ALTERA_DTYPE_SGDMA) {
  1145. /* Get the mapped address to the SGDMA descriptor memory */
  1146. ret = request_and_map(pdev, "s1", &dma_res, &descmap);
  1147. if (ret)
  1148. goto err_free_netdev;
  1149. /* Start of that memory is for transmit descriptors */
  1150. priv->tx_dma_desc = descmap;
  1151. /* First half is for tx descriptors, other half for tx */
  1152. priv->txdescmem = resource_size(dma_res)/2;
  1153. priv->txdescmem_busaddr = (dma_addr_t)dma_res->start;
  1154. priv->rx_dma_desc = (void __iomem *)((uintptr_t)(descmap +
  1155. priv->txdescmem));
  1156. priv->rxdescmem = resource_size(dma_res)/2;
  1157. priv->rxdescmem_busaddr = dma_res->start;
  1158. priv->rxdescmem_busaddr += priv->txdescmem;
  1159. if (upper_32_bits(priv->rxdescmem_busaddr)) {
  1160. dev_dbg(priv->device,
  1161. "SGDMA bus addresses greater than 32-bits\n");
  1162. ret = -EINVAL;
  1163. goto err_free_netdev;
  1164. }
  1165. if (upper_32_bits(priv->txdescmem_busaddr)) {
  1166. dev_dbg(priv->device,
  1167. "SGDMA bus addresses greater than 32-bits\n");
  1168. ret = -EINVAL;
  1169. goto err_free_netdev;
  1170. }
  1171. } else if (priv->dmaops &&
  1172. priv->dmaops->altera_dtype == ALTERA_DTYPE_MSGDMA) {
  1173. ret = request_and_map(pdev, "rx_resp", &dma_res,
  1174. &priv->rx_dma_resp);
  1175. if (ret)
  1176. goto err_free_netdev;
  1177. ret = request_and_map(pdev, "tx_desc", &dma_res,
  1178. &priv->tx_dma_desc);
  1179. if (ret)
  1180. goto err_free_netdev;
  1181. priv->txdescmem = resource_size(dma_res);
  1182. priv->txdescmem_busaddr = dma_res->start;
  1183. ret = request_and_map(pdev, "rx_desc", &dma_res,
  1184. &priv->rx_dma_desc);
  1185. if (ret)
  1186. goto err_free_netdev;
  1187. priv->rxdescmem = resource_size(dma_res);
  1188. priv->rxdescmem_busaddr = dma_res->start;
  1189. } else {
  1190. goto err_free_netdev;
  1191. }
  1192. if (!dma_set_mask(priv->device, DMA_BIT_MASK(priv->dmaops->dmamask)))
  1193. dma_set_coherent_mask(priv->device,
  1194. DMA_BIT_MASK(priv->dmaops->dmamask));
  1195. else if (!dma_set_mask(priv->device, DMA_BIT_MASK(32)))
  1196. dma_set_coherent_mask(priv->device, DMA_BIT_MASK(32));
  1197. else
  1198. goto err_free_netdev;
  1199. /* MAC address space */
  1200. ret = request_and_map(pdev, "control_port", &control_port,
  1201. (void __iomem **)&priv->mac_dev);
  1202. if (ret)
  1203. goto err_free_netdev;
  1204. /* xSGDMA Rx Dispatcher address space */
  1205. ret = request_and_map(pdev, "rx_csr", &dma_res,
  1206. &priv->rx_dma_csr);
  1207. if (ret)
  1208. goto err_free_netdev;
  1209. /* xSGDMA Tx Dispatcher address space */
  1210. ret = request_and_map(pdev, "tx_csr", &dma_res,
  1211. &priv->tx_dma_csr);
  1212. if (ret)
  1213. goto err_free_netdev;
  1214. /* Rx IRQ */
  1215. priv->rx_irq = platform_get_irq_byname(pdev, "rx_irq");
  1216. if (priv->rx_irq == -ENXIO) {
  1217. dev_err(&pdev->dev, "cannot obtain Rx IRQ\n");
  1218. ret = -ENXIO;
  1219. goto err_free_netdev;
  1220. }
  1221. /* Tx IRQ */
  1222. priv->tx_irq = platform_get_irq_byname(pdev, "tx_irq");
  1223. if (priv->tx_irq == -ENXIO) {
  1224. dev_err(&pdev->dev, "cannot obtain Tx IRQ\n");
  1225. ret = -ENXIO;
  1226. goto err_free_netdev;
  1227. }
  1228. /* get FIFO depths from device tree */
  1229. if (of_property_read_u32(pdev->dev.of_node, "rx-fifo-depth",
  1230. &priv->rx_fifo_depth)) {
  1231. dev_err(&pdev->dev, "cannot obtain rx-fifo-depth\n");
  1232. ret = -ENXIO;
  1233. goto err_free_netdev;
  1234. }
  1235. if (of_property_read_u32(pdev->dev.of_node, "tx-fifo-depth",
  1236. &priv->tx_fifo_depth)) {
  1237. dev_err(&pdev->dev, "cannot obtain tx-fifo-depth\n");
  1238. ret = -ENXIO;
  1239. goto err_free_netdev;
  1240. }
  1241. /* get hash filter settings for this instance */
  1242. priv->hash_filter =
  1243. of_property_read_bool(pdev->dev.of_node,
  1244. "altr,has-hash-multicast-filter");
  1245. /* Set hash filter to not set for now until the
  1246. * multicast filter receive issue is debugged
  1247. */
  1248. priv->hash_filter = 0;
  1249. /* get supplemental address settings for this instance */
  1250. priv->added_unicast =
  1251. of_property_read_bool(pdev->dev.of_node,
  1252. "altr,has-supplementary-unicast");
  1253. priv->dev->min_mtu = ETH_ZLEN + ETH_FCS_LEN;
  1254. /* Max MTU is 1500, ETH_DATA_LEN */
  1255. priv->dev->max_mtu = ETH_DATA_LEN;
  1256. /* Get the max mtu from the device tree. Note that the
  1257. * "max-frame-size" parameter is actually max mtu. Definition
  1258. * in the ePAPR v1.1 spec and usage differ, so go with usage.
  1259. */
  1260. of_property_read_u32(pdev->dev.of_node, "max-frame-size",
  1261. &priv->dev->max_mtu);
  1262. /* The DMA buffer size already accounts for an alignment bias
  1263. * to avoid unaligned access exceptions for the NIOS processor,
  1264. */
  1265. priv->rx_dma_buf_sz = ALTERA_RXDMABUFFER_SIZE;
  1266. /* get default MAC address from device tree */
  1267. macaddr = of_get_mac_address(pdev->dev.of_node);
  1268. if (macaddr)
  1269. ether_addr_copy(ndev->dev_addr, macaddr);
  1270. else
  1271. eth_hw_addr_random(ndev);
  1272. /* get phy addr and create mdio */
  1273. ret = altera_tse_phy_get_addr_mdio_create(ndev);
  1274. if (ret)
  1275. goto err_free_netdev;
  1276. /* initialize netdev */
  1277. ndev->mem_start = control_port->start;
  1278. ndev->mem_end = control_port->end;
  1279. ndev->netdev_ops = &altera_tse_netdev_ops;
  1280. altera_tse_set_ethtool_ops(ndev);
  1281. altera_tse_netdev_ops.ndo_set_rx_mode = tse_set_rx_mode;
  1282. if (priv->hash_filter)
  1283. altera_tse_netdev_ops.ndo_set_rx_mode =
  1284. tse_set_rx_mode_hashfilter;
  1285. /* Scatter/gather IO is not supported,
  1286. * so it is turned off
  1287. */
  1288. ndev->hw_features &= ~NETIF_F_SG;
  1289. ndev->features |= ndev->hw_features | NETIF_F_HIGHDMA;
  1290. /* VLAN offloading of tagging, stripping and filtering is not
  1291. * supported by hardware, but driver will accommodate the
  1292. * extra 4-byte VLAN tag for processing by upper layers
  1293. */
  1294. ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
  1295. /* setup NAPI interface */
  1296. netif_napi_add(ndev, &priv->napi, tse_poll, NAPI_POLL_WEIGHT);
  1297. spin_lock_init(&priv->mac_cfg_lock);
  1298. spin_lock_init(&priv->tx_lock);
  1299. spin_lock_init(&priv->rxdma_irq_lock);
  1300. netif_carrier_off(ndev);
  1301. ret = register_netdev(ndev);
  1302. if (ret) {
  1303. dev_err(&pdev->dev, "failed to register TSE net device\n");
  1304. goto err_register_netdev;
  1305. }
  1306. platform_set_drvdata(pdev, ndev);
  1307. priv->revision = ioread32(&priv->mac_dev->megacore_revision);
  1308. if (netif_msg_probe(priv))
  1309. dev_info(&pdev->dev, "Altera TSE MAC version %d.%d at 0x%08lx irq %d/%d\n",
  1310. (priv->revision >> 8) & 0xff,
  1311. priv->revision & 0xff,
  1312. (unsigned long) control_port->start, priv->rx_irq,
  1313. priv->tx_irq);
  1314. ret = init_phy(ndev);
  1315. if (ret != 0) {
  1316. netdev_err(ndev, "Cannot attach to PHY (error: %d)\n", ret);
  1317. goto err_init_phy;
  1318. }
  1319. return 0;
  1320. err_init_phy:
  1321. unregister_netdev(ndev);
  1322. err_register_netdev:
  1323. netif_napi_del(&priv->napi);
  1324. altera_tse_mdio_destroy(ndev);
  1325. err_free_netdev:
  1326. free_netdev(ndev);
  1327. return ret;
  1328. }
  1329. /* Remove Altera TSE MAC device
  1330. */
  1331. static int altera_tse_remove(struct platform_device *pdev)
  1332. {
  1333. struct net_device *ndev = platform_get_drvdata(pdev);
  1334. struct altera_tse_private *priv = netdev_priv(ndev);
  1335. if (ndev->phydev) {
  1336. phy_disconnect(ndev->phydev);
  1337. if (of_phy_is_fixed_link(priv->device->of_node))
  1338. of_phy_deregister_fixed_link(priv->device->of_node);
  1339. }
  1340. platform_set_drvdata(pdev, NULL);
  1341. altera_tse_mdio_destroy(ndev);
  1342. unregister_netdev(ndev);
  1343. free_netdev(ndev);
  1344. return 0;
  1345. }
  1346. static const struct altera_dmaops altera_dtype_sgdma = {
  1347. .altera_dtype = ALTERA_DTYPE_SGDMA,
  1348. .dmamask = 32,
  1349. .reset_dma = sgdma_reset,
  1350. .enable_txirq = sgdma_enable_txirq,
  1351. .enable_rxirq = sgdma_enable_rxirq,
  1352. .disable_txirq = sgdma_disable_txirq,
  1353. .disable_rxirq = sgdma_disable_rxirq,
  1354. .clear_txirq = sgdma_clear_txirq,
  1355. .clear_rxirq = sgdma_clear_rxirq,
  1356. .tx_buffer = sgdma_tx_buffer,
  1357. .tx_completions = sgdma_tx_completions,
  1358. .add_rx_desc = sgdma_add_rx_desc,
  1359. .get_rx_status = sgdma_rx_status,
  1360. .init_dma = sgdma_initialize,
  1361. .uninit_dma = sgdma_uninitialize,
  1362. .start_rxdma = sgdma_start_rxdma,
  1363. };
  1364. static const struct altera_dmaops altera_dtype_msgdma = {
  1365. .altera_dtype = ALTERA_DTYPE_MSGDMA,
  1366. .dmamask = 64,
  1367. .reset_dma = msgdma_reset,
  1368. .enable_txirq = msgdma_enable_txirq,
  1369. .enable_rxirq = msgdma_enable_rxirq,
  1370. .disable_txirq = msgdma_disable_txirq,
  1371. .disable_rxirq = msgdma_disable_rxirq,
  1372. .clear_txirq = msgdma_clear_txirq,
  1373. .clear_rxirq = msgdma_clear_rxirq,
  1374. .tx_buffer = msgdma_tx_buffer,
  1375. .tx_completions = msgdma_tx_completions,
  1376. .add_rx_desc = msgdma_add_rx_desc,
  1377. .get_rx_status = msgdma_rx_status,
  1378. .init_dma = msgdma_initialize,
  1379. .uninit_dma = msgdma_uninitialize,
  1380. .start_rxdma = msgdma_start_rxdma,
  1381. };
  1382. static const struct of_device_id altera_tse_ids[] = {
  1383. { .compatible = "altr,tse-msgdma-1.0", .data = &altera_dtype_msgdma, },
  1384. { .compatible = "altr,tse-1.0", .data = &altera_dtype_sgdma, },
  1385. { .compatible = "ALTR,tse-1.0", .data = &altera_dtype_sgdma, },
  1386. {},
  1387. };
  1388. MODULE_DEVICE_TABLE(of, altera_tse_ids);
  1389. static struct platform_driver altera_tse_driver = {
  1390. .probe = altera_tse_probe,
  1391. .remove = altera_tse_remove,
  1392. .suspend = NULL,
  1393. .resume = NULL,
  1394. .driver = {
  1395. .name = ALTERA_TSE_RESOURCE_NAME,
  1396. .of_match_table = altera_tse_ids,
  1397. },
  1398. };
  1399. module_platform_driver(altera_tse_driver);
  1400. MODULE_AUTHOR("Altera Corporation");
  1401. MODULE_DESCRIPTION("Altera Triple Speed Ethernet MAC driver");
  1402. MODULE_LICENSE("GPL v2");