altera_tse_main.c 41 KB

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