bgmac.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625
  1. /*
  2. * Driver for (BCM4706)? GBit MAC core on BCMA bus.
  3. *
  4. * Copyright (C) 2012 Rafał Miłecki <zajec5@gmail.com>
  5. *
  6. * Licensed under the GNU/GPL. See COPYING for details.
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/bcma/bcma.h>
  10. #include <linux/etherdevice.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/bcm47xx_nvram.h>
  13. #include <linux/phy.h>
  14. #include <linux/phy_fixed.h>
  15. #include <net/dsa.h>
  16. #include "bgmac.h"
  17. static bool bgmac_wait_value(struct bgmac *bgmac, u16 reg, u32 mask,
  18. u32 value, int timeout)
  19. {
  20. u32 val;
  21. int i;
  22. for (i = 0; i < timeout / 10; i++) {
  23. val = bgmac_read(bgmac, reg);
  24. if ((val & mask) == value)
  25. return true;
  26. udelay(10);
  27. }
  28. dev_err(bgmac->dev, "Timeout waiting for reg 0x%X\n", reg);
  29. return false;
  30. }
  31. /**************************************************
  32. * DMA
  33. **************************************************/
  34. static void bgmac_dma_tx_reset(struct bgmac *bgmac, struct bgmac_dma_ring *ring)
  35. {
  36. u32 val;
  37. int i;
  38. if (!ring->mmio_base)
  39. return;
  40. /* Suspend DMA TX ring first.
  41. * bgmac_wait_value doesn't support waiting for any of few values, so
  42. * implement whole loop here.
  43. */
  44. bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL,
  45. BGMAC_DMA_TX_SUSPEND);
  46. for (i = 0; i < 10000 / 10; i++) {
  47. val = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_STATUS);
  48. val &= BGMAC_DMA_TX_STAT;
  49. if (val == BGMAC_DMA_TX_STAT_DISABLED ||
  50. val == BGMAC_DMA_TX_STAT_IDLEWAIT ||
  51. val == BGMAC_DMA_TX_STAT_STOPPED) {
  52. i = 0;
  53. break;
  54. }
  55. udelay(10);
  56. }
  57. if (i)
  58. dev_err(bgmac->dev, "Timeout suspending DMA TX ring 0x%X (BGMAC_DMA_TX_STAT: 0x%08X)\n",
  59. ring->mmio_base, val);
  60. /* Remove SUSPEND bit */
  61. bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL, 0);
  62. if (!bgmac_wait_value(bgmac,
  63. ring->mmio_base + BGMAC_DMA_TX_STATUS,
  64. BGMAC_DMA_TX_STAT, BGMAC_DMA_TX_STAT_DISABLED,
  65. 10000)) {
  66. dev_warn(bgmac->dev, "DMA TX ring 0x%X wasn't disabled on time, waiting additional 300us\n",
  67. ring->mmio_base);
  68. udelay(300);
  69. val = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_STATUS);
  70. if ((val & BGMAC_DMA_TX_STAT) != BGMAC_DMA_TX_STAT_DISABLED)
  71. dev_err(bgmac->dev, "Reset of DMA TX ring 0x%X failed\n",
  72. ring->mmio_base);
  73. }
  74. }
  75. static void bgmac_dma_tx_enable(struct bgmac *bgmac,
  76. struct bgmac_dma_ring *ring)
  77. {
  78. u32 ctl;
  79. ctl = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL);
  80. if (bgmac->feature_flags & BGMAC_FEAT_TX_MASK_SETUP) {
  81. ctl &= ~BGMAC_DMA_TX_BL_MASK;
  82. ctl |= BGMAC_DMA_TX_BL_128 << BGMAC_DMA_TX_BL_SHIFT;
  83. ctl &= ~BGMAC_DMA_TX_MR_MASK;
  84. ctl |= BGMAC_DMA_TX_MR_2 << BGMAC_DMA_TX_MR_SHIFT;
  85. ctl &= ~BGMAC_DMA_TX_PC_MASK;
  86. ctl |= BGMAC_DMA_TX_PC_16 << BGMAC_DMA_TX_PC_SHIFT;
  87. ctl &= ~BGMAC_DMA_TX_PT_MASK;
  88. ctl |= BGMAC_DMA_TX_PT_8 << BGMAC_DMA_TX_PT_SHIFT;
  89. }
  90. ctl |= BGMAC_DMA_TX_ENABLE;
  91. ctl |= BGMAC_DMA_TX_PARITY_DISABLE;
  92. bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL, ctl);
  93. }
  94. static void
  95. bgmac_dma_tx_add_buf(struct bgmac *bgmac, struct bgmac_dma_ring *ring,
  96. int i, int len, u32 ctl0)
  97. {
  98. struct bgmac_slot_info *slot;
  99. struct bgmac_dma_desc *dma_desc;
  100. u32 ctl1;
  101. if (i == BGMAC_TX_RING_SLOTS - 1)
  102. ctl0 |= BGMAC_DESC_CTL0_EOT;
  103. ctl1 = len & BGMAC_DESC_CTL1_LEN;
  104. slot = &ring->slots[i];
  105. dma_desc = &ring->cpu_base[i];
  106. dma_desc->addr_low = cpu_to_le32(lower_32_bits(slot->dma_addr));
  107. dma_desc->addr_high = cpu_to_le32(upper_32_bits(slot->dma_addr));
  108. dma_desc->ctl0 = cpu_to_le32(ctl0);
  109. dma_desc->ctl1 = cpu_to_le32(ctl1);
  110. }
  111. static netdev_tx_t bgmac_dma_tx_add(struct bgmac *bgmac,
  112. struct bgmac_dma_ring *ring,
  113. struct sk_buff *skb)
  114. {
  115. struct device *dma_dev = bgmac->dma_dev;
  116. struct net_device *net_dev = bgmac->net_dev;
  117. int index = ring->end % BGMAC_TX_RING_SLOTS;
  118. struct bgmac_slot_info *slot = &ring->slots[index];
  119. int nr_frags;
  120. u32 flags;
  121. int i;
  122. if (skb->len > BGMAC_DESC_CTL1_LEN) {
  123. netdev_err(bgmac->net_dev, "Too long skb (%d)\n", skb->len);
  124. goto err_drop;
  125. }
  126. if (skb->ip_summed == CHECKSUM_PARTIAL)
  127. skb_checksum_help(skb);
  128. nr_frags = skb_shinfo(skb)->nr_frags;
  129. /* ring->end - ring->start will return the number of valid slots,
  130. * even when ring->end overflows
  131. */
  132. if (ring->end - ring->start + nr_frags + 1 >= BGMAC_TX_RING_SLOTS) {
  133. netdev_err(bgmac->net_dev, "TX ring is full, queue should be stopped!\n");
  134. netif_stop_queue(net_dev);
  135. return NETDEV_TX_BUSY;
  136. }
  137. slot->dma_addr = dma_map_single(dma_dev, skb->data, skb_headlen(skb),
  138. DMA_TO_DEVICE);
  139. if (unlikely(dma_mapping_error(dma_dev, slot->dma_addr)))
  140. goto err_dma_head;
  141. flags = BGMAC_DESC_CTL0_SOF;
  142. if (!nr_frags)
  143. flags |= BGMAC_DESC_CTL0_EOF | BGMAC_DESC_CTL0_IOC;
  144. bgmac_dma_tx_add_buf(bgmac, ring, index, skb_headlen(skb), flags);
  145. flags = 0;
  146. for (i = 0; i < nr_frags; i++) {
  147. struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i];
  148. int len = skb_frag_size(frag);
  149. index = (index + 1) % BGMAC_TX_RING_SLOTS;
  150. slot = &ring->slots[index];
  151. slot->dma_addr = skb_frag_dma_map(dma_dev, frag, 0,
  152. len, DMA_TO_DEVICE);
  153. if (unlikely(dma_mapping_error(dma_dev, slot->dma_addr)))
  154. goto err_dma;
  155. if (i == nr_frags - 1)
  156. flags |= BGMAC_DESC_CTL0_EOF | BGMAC_DESC_CTL0_IOC;
  157. bgmac_dma_tx_add_buf(bgmac, ring, index, len, flags);
  158. }
  159. slot->skb = skb;
  160. ring->end += nr_frags + 1;
  161. netdev_sent_queue(net_dev, skb->len);
  162. wmb();
  163. /* Increase ring->end to point empty slot. We tell hardware the first
  164. * slot it should *not* read.
  165. */
  166. bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_INDEX,
  167. ring->index_base +
  168. (ring->end % BGMAC_TX_RING_SLOTS) *
  169. sizeof(struct bgmac_dma_desc));
  170. if (ring->end - ring->start >= BGMAC_TX_RING_SLOTS - 8)
  171. netif_stop_queue(net_dev);
  172. return NETDEV_TX_OK;
  173. err_dma:
  174. dma_unmap_single(dma_dev, slot->dma_addr, skb_headlen(skb),
  175. DMA_TO_DEVICE);
  176. while (i-- > 0) {
  177. int index = (ring->end + i) % BGMAC_TX_RING_SLOTS;
  178. struct bgmac_slot_info *slot = &ring->slots[index];
  179. u32 ctl1 = le32_to_cpu(ring->cpu_base[index].ctl1);
  180. int len = ctl1 & BGMAC_DESC_CTL1_LEN;
  181. dma_unmap_page(dma_dev, slot->dma_addr, len, DMA_TO_DEVICE);
  182. }
  183. err_dma_head:
  184. netdev_err(bgmac->net_dev, "Mapping error of skb on ring 0x%X\n",
  185. ring->mmio_base);
  186. err_drop:
  187. dev_kfree_skb(skb);
  188. net_dev->stats.tx_dropped++;
  189. net_dev->stats.tx_errors++;
  190. return NETDEV_TX_OK;
  191. }
  192. /* Free transmitted packets */
  193. static void bgmac_dma_tx_free(struct bgmac *bgmac, struct bgmac_dma_ring *ring)
  194. {
  195. struct device *dma_dev = bgmac->dma_dev;
  196. int empty_slot;
  197. unsigned bytes_compl = 0, pkts_compl = 0;
  198. /* The last slot that hardware didn't consume yet */
  199. empty_slot = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_STATUS);
  200. empty_slot &= BGMAC_DMA_TX_STATDPTR;
  201. empty_slot -= ring->index_base;
  202. empty_slot &= BGMAC_DMA_TX_STATDPTR;
  203. empty_slot /= sizeof(struct bgmac_dma_desc);
  204. while (ring->start != ring->end) {
  205. int slot_idx = ring->start % BGMAC_TX_RING_SLOTS;
  206. struct bgmac_slot_info *slot = &ring->slots[slot_idx];
  207. u32 ctl0, ctl1;
  208. int len;
  209. if (slot_idx == empty_slot)
  210. break;
  211. ctl0 = le32_to_cpu(ring->cpu_base[slot_idx].ctl0);
  212. ctl1 = le32_to_cpu(ring->cpu_base[slot_idx].ctl1);
  213. len = ctl1 & BGMAC_DESC_CTL1_LEN;
  214. if (ctl0 & BGMAC_DESC_CTL0_SOF)
  215. /* Unmap no longer used buffer */
  216. dma_unmap_single(dma_dev, slot->dma_addr, len,
  217. DMA_TO_DEVICE);
  218. else
  219. dma_unmap_page(dma_dev, slot->dma_addr, len,
  220. DMA_TO_DEVICE);
  221. if (slot->skb) {
  222. bgmac->net_dev->stats.tx_bytes += slot->skb->len;
  223. bgmac->net_dev->stats.tx_packets++;
  224. bytes_compl += slot->skb->len;
  225. pkts_compl++;
  226. /* Free memory! :) */
  227. dev_kfree_skb(slot->skb);
  228. slot->skb = NULL;
  229. }
  230. slot->dma_addr = 0;
  231. ring->start++;
  232. }
  233. if (!pkts_compl)
  234. return;
  235. netdev_completed_queue(bgmac->net_dev, pkts_compl, bytes_compl);
  236. if (netif_queue_stopped(bgmac->net_dev))
  237. netif_wake_queue(bgmac->net_dev);
  238. }
  239. static void bgmac_dma_rx_reset(struct bgmac *bgmac, struct bgmac_dma_ring *ring)
  240. {
  241. if (!ring->mmio_base)
  242. return;
  243. bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL, 0);
  244. if (!bgmac_wait_value(bgmac,
  245. ring->mmio_base + BGMAC_DMA_RX_STATUS,
  246. BGMAC_DMA_RX_STAT, BGMAC_DMA_RX_STAT_DISABLED,
  247. 10000))
  248. dev_err(bgmac->dev, "Reset of ring 0x%X RX failed\n",
  249. ring->mmio_base);
  250. }
  251. static void bgmac_dma_rx_enable(struct bgmac *bgmac,
  252. struct bgmac_dma_ring *ring)
  253. {
  254. u32 ctl;
  255. ctl = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL);
  256. /* preserve ONLY bits 16-17 from current hardware value */
  257. ctl &= BGMAC_DMA_RX_ADDREXT_MASK;
  258. if (bgmac->feature_flags & BGMAC_FEAT_RX_MASK_SETUP) {
  259. ctl &= ~BGMAC_DMA_RX_BL_MASK;
  260. ctl |= BGMAC_DMA_RX_BL_128 << BGMAC_DMA_RX_BL_SHIFT;
  261. ctl &= ~BGMAC_DMA_RX_PC_MASK;
  262. ctl |= BGMAC_DMA_RX_PC_8 << BGMAC_DMA_RX_PC_SHIFT;
  263. ctl &= ~BGMAC_DMA_RX_PT_MASK;
  264. ctl |= BGMAC_DMA_RX_PT_1 << BGMAC_DMA_RX_PT_SHIFT;
  265. }
  266. ctl |= BGMAC_DMA_RX_ENABLE;
  267. ctl |= BGMAC_DMA_RX_PARITY_DISABLE;
  268. ctl |= BGMAC_DMA_RX_OVERFLOW_CONT;
  269. ctl |= BGMAC_RX_FRAME_OFFSET << BGMAC_DMA_RX_FRAME_OFFSET_SHIFT;
  270. bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL, ctl);
  271. }
  272. static int bgmac_dma_rx_skb_for_slot(struct bgmac *bgmac,
  273. struct bgmac_slot_info *slot)
  274. {
  275. struct device *dma_dev = bgmac->dma_dev;
  276. dma_addr_t dma_addr;
  277. struct bgmac_rx_header *rx;
  278. void *buf;
  279. /* Alloc skb */
  280. buf = netdev_alloc_frag(BGMAC_RX_ALLOC_SIZE);
  281. if (!buf)
  282. return -ENOMEM;
  283. /* Poison - if everything goes fine, hardware will overwrite it */
  284. rx = buf + BGMAC_RX_BUF_OFFSET;
  285. rx->len = cpu_to_le16(0xdead);
  286. rx->flags = cpu_to_le16(0xbeef);
  287. /* Map skb for the DMA */
  288. dma_addr = dma_map_single(dma_dev, buf + BGMAC_RX_BUF_OFFSET,
  289. BGMAC_RX_BUF_SIZE, DMA_FROM_DEVICE);
  290. if (dma_mapping_error(dma_dev, dma_addr)) {
  291. netdev_err(bgmac->net_dev, "DMA mapping error\n");
  292. put_page(virt_to_head_page(buf));
  293. return -ENOMEM;
  294. }
  295. /* Update the slot */
  296. slot->buf = buf;
  297. slot->dma_addr = dma_addr;
  298. return 0;
  299. }
  300. static void bgmac_dma_rx_update_index(struct bgmac *bgmac,
  301. struct bgmac_dma_ring *ring)
  302. {
  303. dma_wmb();
  304. bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_INDEX,
  305. ring->index_base +
  306. ring->end * sizeof(struct bgmac_dma_desc));
  307. }
  308. static void bgmac_dma_rx_setup_desc(struct bgmac *bgmac,
  309. struct bgmac_dma_ring *ring, int desc_idx)
  310. {
  311. struct bgmac_dma_desc *dma_desc = ring->cpu_base + desc_idx;
  312. u32 ctl0 = 0, ctl1 = 0;
  313. if (desc_idx == BGMAC_RX_RING_SLOTS - 1)
  314. ctl0 |= BGMAC_DESC_CTL0_EOT;
  315. ctl1 |= BGMAC_RX_BUF_SIZE & BGMAC_DESC_CTL1_LEN;
  316. /* Is there any BGMAC device that requires extension? */
  317. /* ctl1 |= (addrext << B43_DMA64_DCTL1_ADDREXT_SHIFT) &
  318. * B43_DMA64_DCTL1_ADDREXT_MASK;
  319. */
  320. dma_desc->addr_low = cpu_to_le32(lower_32_bits(ring->slots[desc_idx].dma_addr));
  321. dma_desc->addr_high = cpu_to_le32(upper_32_bits(ring->slots[desc_idx].dma_addr));
  322. dma_desc->ctl0 = cpu_to_le32(ctl0);
  323. dma_desc->ctl1 = cpu_to_le32(ctl1);
  324. ring->end = desc_idx;
  325. }
  326. static void bgmac_dma_rx_poison_buf(struct device *dma_dev,
  327. struct bgmac_slot_info *slot)
  328. {
  329. struct bgmac_rx_header *rx = slot->buf + BGMAC_RX_BUF_OFFSET;
  330. dma_sync_single_for_cpu(dma_dev, slot->dma_addr, BGMAC_RX_BUF_SIZE,
  331. DMA_FROM_DEVICE);
  332. rx->len = cpu_to_le16(0xdead);
  333. rx->flags = cpu_to_le16(0xbeef);
  334. dma_sync_single_for_device(dma_dev, slot->dma_addr, BGMAC_RX_BUF_SIZE,
  335. DMA_FROM_DEVICE);
  336. }
  337. static int bgmac_dma_rx_read(struct bgmac *bgmac, struct bgmac_dma_ring *ring,
  338. int weight)
  339. {
  340. u32 end_slot;
  341. int handled = 0;
  342. end_slot = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_STATUS);
  343. end_slot &= BGMAC_DMA_RX_STATDPTR;
  344. end_slot -= ring->index_base;
  345. end_slot &= BGMAC_DMA_RX_STATDPTR;
  346. end_slot /= sizeof(struct bgmac_dma_desc);
  347. while (ring->start != end_slot) {
  348. struct device *dma_dev = bgmac->dma_dev;
  349. struct bgmac_slot_info *slot = &ring->slots[ring->start];
  350. struct bgmac_rx_header *rx = slot->buf + BGMAC_RX_BUF_OFFSET;
  351. struct sk_buff *skb;
  352. void *buf = slot->buf;
  353. dma_addr_t dma_addr = slot->dma_addr;
  354. u16 len, flags;
  355. do {
  356. /* Prepare new skb as replacement */
  357. if (bgmac_dma_rx_skb_for_slot(bgmac, slot)) {
  358. bgmac_dma_rx_poison_buf(dma_dev, slot);
  359. break;
  360. }
  361. /* Unmap buffer to make it accessible to the CPU */
  362. dma_unmap_single(dma_dev, dma_addr,
  363. BGMAC_RX_BUF_SIZE, DMA_FROM_DEVICE);
  364. /* Get info from the header */
  365. len = le16_to_cpu(rx->len);
  366. flags = le16_to_cpu(rx->flags);
  367. /* Check for poison and drop or pass the packet */
  368. if (len == 0xdead && flags == 0xbeef) {
  369. netdev_err(bgmac->net_dev, "Found poisoned packet at slot %d, DMA issue!\n",
  370. ring->start);
  371. put_page(virt_to_head_page(buf));
  372. bgmac->net_dev->stats.rx_errors++;
  373. break;
  374. }
  375. if (len > BGMAC_RX_ALLOC_SIZE) {
  376. netdev_err(bgmac->net_dev, "Found oversized packet at slot %d, DMA issue!\n",
  377. ring->start);
  378. put_page(virt_to_head_page(buf));
  379. bgmac->net_dev->stats.rx_length_errors++;
  380. bgmac->net_dev->stats.rx_errors++;
  381. break;
  382. }
  383. /* Omit CRC. */
  384. len -= ETH_FCS_LEN;
  385. skb = build_skb(buf, BGMAC_RX_ALLOC_SIZE);
  386. if (unlikely(!skb)) {
  387. netdev_err(bgmac->net_dev, "build_skb failed\n");
  388. put_page(virt_to_head_page(buf));
  389. bgmac->net_dev->stats.rx_errors++;
  390. break;
  391. }
  392. skb_put(skb, BGMAC_RX_FRAME_OFFSET +
  393. BGMAC_RX_BUF_OFFSET + len);
  394. skb_pull(skb, BGMAC_RX_FRAME_OFFSET +
  395. BGMAC_RX_BUF_OFFSET);
  396. skb_checksum_none_assert(skb);
  397. skb->protocol = eth_type_trans(skb, bgmac->net_dev);
  398. bgmac->net_dev->stats.rx_bytes += len;
  399. bgmac->net_dev->stats.rx_packets++;
  400. napi_gro_receive(&bgmac->napi, skb);
  401. handled++;
  402. } while (0);
  403. bgmac_dma_rx_setup_desc(bgmac, ring, ring->start);
  404. if (++ring->start >= BGMAC_RX_RING_SLOTS)
  405. ring->start = 0;
  406. if (handled >= weight) /* Should never be greater */
  407. break;
  408. }
  409. bgmac_dma_rx_update_index(bgmac, ring);
  410. return handled;
  411. }
  412. /* Does ring support unaligned addressing? */
  413. static bool bgmac_dma_unaligned(struct bgmac *bgmac,
  414. struct bgmac_dma_ring *ring,
  415. enum bgmac_dma_ring_type ring_type)
  416. {
  417. switch (ring_type) {
  418. case BGMAC_DMA_RING_TX:
  419. bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO,
  420. 0xff0);
  421. if (bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO))
  422. return true;
  423. break;
  424. case BGMAC_DMA_RING_RX:
  425. bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO,
  426. 0xff0);
  427. if (bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO))
  428. return true;
  429. break;
  430. }
  431. return false;
  432. }
  433. static void bgmac_dma_tx_ring_free(struct bgmac *bgmac,
  434. struct bgmac_dma_ring *ring)
  435. {
  436. struct device *dma_dev = bgmac->dma_dev;
  437. struct bgmac_dma_desc *dma_desc = ring->cpu_base;
  438. struct bgmac_slot_info *slot;
  439. int i;
  440. for (i = 0; i < BGMAC_TX_RING_SLOTS; i++) {
  441. u32 ctl1 = le32_to_cpu(dma_desc[i].ctl1);
  442. unsigned int len = ctl1 & BGMAC_DESC_CTL1_LEN;
  443. slot = &ring->slots[i];
  444. dev_kfree_skb(slot->skb);
  445. if (!slot->dma_addr)
  446. continue;
  447. if (slot->skb)
  448. dma_unmap_single(dma_dev, slot->dma_addr,
  449. len, DMA_TO_DEVICE);
  450. else
  451. dma_unmap_page(dma_dev, slot->dma_addr,
  452. len, DMA_TO_DEVICE);
  453. }
  454. }
  455. static void bgmac_dma_rx_ring_free(struct bgmac *bgmac,
  456. struct bgmac_dma_ring *ring)
  457. {
  458. struct device *dma_dev = bgmac->dma_dev;
  459. struct bgmac_slot_info *slot;
  460. int i;
  461. for (i = 0; i < BGMAC_RX_RING_SLOTS; i++) {
  462. slot = &ring->slots[i];
  463. if (!slot->dma_addr)
  464. continue;
  465. dma_unmap_single(dma_dev, slot->dma_addr,
  466. BGMAC_RX_BUF_SIZE,
  467. DMA_FROM_DEVICE);
  468. put_page(virt_to_head_page(slot->buf));
  469. slot->dma_addr = 0;
  470. }
  471. }
  472. static void bgmac_dma_ring_desc_free(struct bgmac *bgmac,
  473. struct bgmac_dma_ring *ring,
  474. int num_slots)
  475. {
  476. struct device *dma_dev = bgmac->dma_dev;
  477. int size;
  478. if (!ring->cpu_base)
  479. return;
  480. /* Free ring of descriptors */
  481. size = num_slots * sizeof(struct bgmac_dma_desc);
  482. dma_free_coherent(dma_dev, size, ring->cpu_base,
  483. ring->dma_base);
  484. }
  485. static void bgmac_dma_cleanup(struct bgmac *bgmac)
  486. {
  487. int i;
  488. for (i = 0; i < BGMAC_MAX_TX_RINGS; i++)
  489. bgmac_dma_tx_ring_free(bgmac, &bgmac->tx_ring[i]);
  490. for (i = 0; i < BGMAC_MAX_RX_RINGS; i++)
  491. bgmac_dma_rx_ring_free(bgmac, &bgmac->rx_ring[i]);
  492. }
  493. static void bgmac_dma_free(struct bgmac *bgmac)
  494. {
  495. int i;
  496. for (i = 0; i < BGMAC_MAX_TX_RINGS; i++)
  497. bgmac_dma_ring_desc_free(bgmac, &bgmac->tx_ring[i],
  498. BGMAC_TX_RING_SLOTS);
  499. for (i = 0; i < BGMAC_MAX_RX_RINGS; i++)
  500. bgmac_dma_ring_desc_free(bgmac, &bgmac->rx_ring[i],
  501. BGMAC_RX_RING_SLOTS);
  502. }
  503. static int bgmac_dma_alloc(struct bgmac *bgmac)
  504. {
  505. struct device *dma_dev = bgmac->dma_dev;
  506. struct bgmac_dma_ring *ring;
  507. static const u16 ring_base[] = { BGMAC_DMA_BASE0, BGMAC_DMA_BASE1,
  508. BGMAC_DMA_BASE2, BGMAC_DMA_BASE3, };
  509. int size; /* ring size: different for Tx and Rx */
  510. int err;
  511. int i;
  512. BUILD_BUG_ON(BGMAC_MAX_TX_RINGS > ARRAY_SIZE(ring_base));
  513. BUILD_BUG_ON(BGMAC_MAX_RX_RINGS > ARRAY_SIZE(ring_base));
  514. if (!(bgmac->feature_flags & BGMAC_FEAT_IDM_MASK)) {
  515. if (!(bgmac_idm_read(bgmac, BCMA_IOST) & BCMA_IOST_DMA64)) {
  516. dev_err(bgmac->dev, "Core does not report 64-bit DMA\n");
  517. return -ENOTSUPP;
  518. }
  519. }
  520. for (i = 0; i < BGMAC_MAX_TX_RINGS; i++) {
  521. ring = &bgmac->tx_ring[i];
  522. ring->mmio_base = ring_base[i];
  523. /* Alloc ring of descriptors */
  524. size = BGMAC_TX_RING_SLOTS * sizeof(struct bgmac_dma_desc);
  525. ring->cpu_base = dma_zalloc_coherent(dma_dev, size,
  526. &ring->dma_base,
  527. GFP_KERNEL);
  528. if (!ring->cpu_base) {
  529. dev_err(bgmac->dev, "Allocation of TX ring 0x%X failed\n",
  530. ring->mmio_base);
  531. goto err_dma_free;
  532. }
  533. ring->unaligned = bgmac_dma_unaligned(bgmac, ring,
  534. BGMAC_DMA_RING_TX);
  535. if (ring->unaligned)
  536. ring->index_base = lower_32_bits(ring->dma_base);
  537. else
  538. ring->index_base = 0;
  539. /* No need to alloc TX slots yet */
  540. }
  541. for (i = 0; i < BGMAC_MAX_RX_RINGS; i++) {
  542. ring = &bgmac->rx_ring[i];
  543. ring->mmio_base = ring_base[i];
  544. /* Alloc ring of descriptors */
  545. size = BGMAC_RX_RING_SLOTS * sizeof(struct bgmac_dma_desc);
  546. ring->cpu_base = dma_zalloc_coherent(dma_dev, size,
  547. &ring->dma_base,
  548. GFP_KERNEL);
  549. if (!ring->cpu_base) {
  550. dev_err(bgmac->dev, "Allocation of RX ring 0x%X failed\n",
  551. ring->mmio_base);
  552. err = -ENOMEM;
  553. goto err_dma_free;
  554. }
  555. ring->unaligned = bgmac_dma_unaligned(bgmac, ring,
  556. BGMAC_DMA_RING_RX);
  557. if (ring->unaligned)
  558. ring->index_base = lower_32_bits(ring->dma_base);
  559. else
  560. ring->index_base = 0;
  561. }
  562. return 0;
  563. err_dma_free:
  564. bgmac_dma_free(bgmac);
  565. return -ENOMEM;
  566. }
  567. static int bgmac_dma_init(struct bgmac *bgmac)
  568. {
  569. struct bgmac_dma_ring *ring;
  570. int i, err;
  571. for (i = 0; i < BGMAC_MAX_TX_RINGS; i++) {
  572. ring = &bgmac->tx_ring[i];
  573. if (!ring->unaligned)
  574. bgmac_dma_tx_enable(bgmac, ring);
  575. bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO,
  576. lower_32_bits(ring->dma_base));
  577. bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGHI,
  578. upper_32_bits(ring->dma_base));
  579. if (ring->unaligned)
  580. bgmac_dma_tx_enable(bgmac, ring);
  581. ring->start = 0;
  582. ring->end = 0; /* Points the slot that should *not* be read */
  583. }
  584. for (i = 0; i < BGMAC_MAX_RX_RINGS; i++) {
  585. int j;
  586. ring = &bgmac->rx_ring[i];
  587. if (!ring->unaligned)
  588. bgmac_dma_rx_enable(bgmac, ring);
  589. bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO,
  590. lower_32_bits(ring->dma_base));
  591. bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGHI,
  592. upper_32_bits(ring->dma_base));
  593. if (ring->unaligned)
  594. bgmac_dma_rx_enable(bgmac, ring);
  595. ring->start = 0;
  596. ring->end = 0;
  597. for (j = 0; j < BGMAC_RX_RING_SLOTS; j++) {
  598. err = bgmac_dma_rx_skb_for_slot(bgmac, &ring->slots[j]);
  599. if (err)
  600. goto error;
  601. bgmac_dma_rx_setup_desc(bgmac, ring, j);
  602. }
  603. bgmac_dma_rx_update_index(bgmac, ring);
  604. }
  605. return 0;
  606. error:
  607. bgmac_dma_cleanup(bgmac);
  608. return err;
  609. }
  610. /**************************************************
  611. * Chip ops
  612. **************************************************/
  613. /* TODO: can we just drop @force? Can we don't reset MAC at all if there is
  614. * nothing to change? Try if after stabilizng driver.
  615. */
  616. static void bgmac_cmdcfg_maskset(struct bgmac *bgmac, u32 mask, u32 set,
  617. bool force)
  618. {
  619. u32 cmdcfg = bgmac_read(bgmac, BGMAC_CMDCFG);
  620. u32 new_val = (cmdcfg & mask) | set;
  621. u32 cmdcfg_sr;
  622. if (bgmac->feature_flags & BGMAC_FEAT_CMDCFG_SR_REV4)
  623. cmdcfg_sr = BGMAC_CMDCFG_SR_REV4;
  624. else
  625. cmdcfg_sr = BGMAC_CMDCFG_SR_REV0;
  626. bgmac_set(bgmac, BGMAC_CMDCFG, cmdcfg_sr);
  627. udelay(2);
  628. if (new_val != cmdcfg || force)
  629. bgmac_write(bgmac, BGMAC_CMDCFG, new_val);
  630. bgmac_mask(bgmac, BGMAC_CMDCFG, ~cmdcfg_sr);
  631. udelay(2);
  632. }
  633. static void bgmac_write_mac_address(struct bgmac *bgmac, u8 *addr)
  634. {
  635. u32 tmp;
  636. tmp = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3];
  637. bgmac_write(bgmac, BGMAC_MACADDR_HIGH, tmp);
  638. tmp = (addr[4] << 8) | addr[5];
  639. bgmac_write(bgmac, BGMAC_MACADDR_LOW, tmp);
  640. }
  641. static void bgmac_set_rx_mode(struct net_device *net_dev)
  642. {
  643. struct bgmac *bgmac = netdev_priv(net_dev);
  644. if (net_dev->flags & IFF_PROMISC)
  645. bgmac_cmdcfg_maskset(bgmac, ~0, BGMAC_CMDCFG_PROM, true);
  646. else
  647. bgmac_cmdcfg_maskset(bgmac, ~BGMAC_CMDCFG_PROM, 0, true);
  648. }
  649. #if 0 /* We don't use that regs yet */
  650. static void bgmac_chip_stats_update(struct bgmac *bgmac)
  651. {
  652. int i;
  653. if (!(bgmac->feature_flags & BGMAC_FEAT_NO_CLR_MIB)) {
  654. for (i = 0; i < BGMAC_NUM_MIB_TX_REGS; i++)
  655. bgmac->mib_tx_regs[i] =
  656. bgmac_read(bgmac,
  657. BGMAC_TX_GOOD_OCTETS + (i * 4));
  658. for (i = 0; i < BGMAC_NUM_MIB_RX_REGS; i++)
  659. bgmac->mib_rx_regs[i] =
  660. bgmac_read(bgmac,
  661. BGMAC_RX_GOOD_OCTETS + (i * 4));
  662. }
  663. /* TODO: what else? how to handle BCM4706? Specs are needed */
  664. }
  665. #endif
  666. static void bgmac_clear_mib(struct bgmac *bgmac)
  667. {
  668. int i;
  669. if (bgmac->feature_flags & BGMAC_FEAT_NO_CLR_MIB)
  670. return;
  671. bgmac_set(bgmac, BGMAC_DEV_CTL, BGMAC_DC_MROR);
  672. for (i = 0; i < BGMAC_NUM_MIB_TX_REGS; i++)
  673. bgmac_read(bgmac, BGMAC_TX_GOOD_OCTETS + (i * 4));
  674. for (i = 0; i < BGMAC_NUM_MIB_RX_REGS; i++)
  675. bgmac_read(bgmac, BGMAC_RX_GOOD_OCTETS + (i * 4));
  676. }
  677. /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_speed */
  678. static void bgmac_mac_speed(struct bgmac *bgmac)
  679. {
  680. u32 mask = ~(BGMAC_CMDCFG_ES_MASK | BGMAC_CMDCFG_HD);
  681. u32 set = 0;
  682. switch (bgmac->mac_speed) {
  683. case SPEED_10:
  684. set |= BGMAC_CMDCFG_ES_10;
  685. break;
  686. case SPEED_100:
  687. set |= BGMAC_CMDCFG_ES_100;
  688. break;
  689. case SPEED_1000:
  690. set |= BGMAC_CMDCFG_ES_1000;
  691. break;
  692. case SPEED_2500:
  693. set |= BGMAC_CMDCFG_ES_2500;
  694. break;
  695. default:
  696. dev_err(bgmac->dev, "Unsupported speed: %d\n",
  697. bgmac->mac_speed);
  698. }
  699. if (bgmac->mac_duplex == DUPLEX_HALF)
  700. set |= BGMAC_CMDCFG_HD;
  701. bgmac_cmdcfg_maskset(bgmac, mask, set, true);
  702. }
  703. static void bgmac_miiconfig(struct bgmac *bgmac)
  704. {
  705. if (bgmac->feature_flags & BGMAC_FEAT_FORCE_SPEED_2500) {
  706. if (!(bgmac->feature_flags & BGMAC_FEAT_IDM_MASK)) {
  707. bgmac_idm_write(bgmac, BCMA_IOCTL,
  708. bgmac_idm_read(bgmac, BCMA_IOCTL) |
  709. 0x40 | BGMAC_BCMA_IOCTL_SW_CLKEN);
  710. }
  711. bgmac->mac_speed = SPEED_2500;
  712. bgmac->mac_duplex = DUPLEX_FULL;
  713. bgmac_mac_speed(bgmac);
  714. } else {
  715. u8 imode;
  716. imode = (bgmac_read(bgmac, BGMAC_DEV_STATUS) &
  717. BGMAC_DS_MM_MASK) >> BGMAC_DS_MM_SHIFT;
  718. if (imode == 0 || imode == 1) {
  719. bgmac->mac_speed = SPEED_100;
  720. bgmac->mac_duplex = DUPLEX_FULL;
  721. bgmac_mac_speed(bgmac);
  722. }
  723. }
  724. }
  725. static void bgmac_chip_reset_idm_config(struct bgmac *bgmac)
  726. {
  727. u32 iost;
  728. iost = bgmac_idm_read(bgmac, BCMA_IOST);
  729. if (bgmac->feature_flags & BGMAC_FEAT_IOST_ATTACHED)
  730. iost &= ~BGMAC_BCMA_IOST_ATTACHED;
  731. /* 3GMAC: for BCM4707 & BCM47094, only do core reset at bgmac_probe() */
  732. if (!(bgmac->feature_flags & BGMAC_FEAT_NO_RESET)) {
  733. u32 flags = 0;
  734. if (iost & BGMAC_BCMA_IOST_ATTACHED) {
  735. flags = BGMAC_BCMA_IOCTL_SW_CLKEN;
  736. if (!bgmac->has_robosw)
  737. flags |= BGMAC_BCMA_IOCTL_SW_RESET;
  738. }
  739. bgmac_clk_enable(bgmac, flags);
  740. }
  741. if (iost & BGMAC_BCMA_IOST_ATTACHED && !bgmac->has_robosw)
  742. bgmac_idm_write(bgmac, BCMA_IOCTL,
  743. bgmac_idm_read(bgmac, BCMA_IOCTL) &
  744. ~BGMAC_BCMA_IOCTL_SW_RESET);
  745. }
  746. /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipreset */
  747. static void bgmac_chip_reset(struct bgmac *bgmac)
  748. {
  749. u32 cmdcfg_sr;
  750. int i;
  751. if (bgmac_clk_enabled(bgmac)) {
  752. if (!bgmac->stats_grabbed) {
  753. /* bgmac_chip_stats_update(bgmac); */
  754. bgmac->stats_grabbed = true;
  755. }
  756. for (i = 0; i < BGMAC_MAX_TX_RINGS; i++)
  757. bgmac_dma_tx_reset(bgmac, &bgmac->tx_ring[i]);
  758. bgmac_cmdcfg_maskset(bgmac, ~0, BGMAC_CMDCFG_ML, false);
  759. udelay(1);
  760. for (i = 0; i < BGMAC_MAX_RX_RINGS; i++)
  761. bgmac_dma_rx_reset(bgmac, &bgmac->rx_ring[i]);
  762. /* TODO: Clear software multicast filter list */
  763. }
  764. if (!(bgmac->feature_flags & BGMAC_FEAT_IDM_MASK))
  765. bgmac_chip_reset_idm_config(bgmac);
  766. /* Request Misc PLL for corerev > 2 */
  767. if (bgmac->feature_flags & BGMAC_FEAT_MISC_PLL_REQ) {
  768. bgmac_set(bgmac, BCMA_CLKCTLST,
  769. BGMAC_BCMA_CLKCTLST_MISC_PLL_REQ);
  770. bgmac_wait_value(bgmac, BCMA_CLKCTLST,
  771. BGMAC_BCMA_CLKCTLST_MISC_PLL_ST,
  772. BGMAC_BCMA_CLKCTLST_MISC_PLL_ST,
  773. 1000);
  774. }
  775. if (bgmac->feature_flags & BGMAC_FEAT_SW_TYPE_PHY) {
  776. u8 et_swtype = 0;
  777. u8 sw_type = BGMAC_CHIPCTL_1_SW_TYPE_EPHY |
  778. BGMAC_CHIPCTL_1_IF_TYPE_MII;
  779. char buf[4];
  780. if (bcm47xx_nvram_getenv("et_swtype", buf, sizeof(buf)) > 0) {
  781. if (kstrtou8(buf, 0, &et_swtype))
  782. dev_err(bgmac->dev, "Failed to parse et_swtype (%s)\n",
  783. buf);
  784. et_swtype &= 0x0f;
  785. et_swtype <<= 4;
  786. sw_type = et_swtype;
  787. } else if (bgmac->feature_flags & BGMAC_FEAT_SW_TYPE_EPHYRMII) {
  788. sw_type = BGMAC_CHIPCTL_1_IF_TYPE_RMII |
  789. BGMAC_CHIPCTL_1_SW_TYPE_EPHYRMII;
  790. } else if (bgmac->feature_flags & BGMAC_FEAT_SW_TYPE_RGMII) {
  791. sw_type = BGMAC_CHIPCTL_1_IF_TYPE_RGMII |
  792. BGMAC_CHIPCTL_1_SW_TYPE_RGMII;
  793. }
  794. bgmac_cco_ctl_maskset(bgmac, 1, ~(BGMAC_CHIPCTL_1_IF_TYPE_MASK |
  795. BGMAC_CHIPCTL_1_SW_TYPE_MASK),
  796. sw_type);
  797. } else if (bgmac->feature_flags & BGMAC_FEAT_CC4_IF_SW_TYPE) {
  798. u32 sw_type = BGMAC_CHIPCTL_4_IF_TYPE_MII |
  799. BGMAC_CHIPCTL_4_SW_TYPE_EPHY;
  800. u8 et_swtype = 0;
  801. char buf[4];
  802. if (bcm47xx_nvram_getenv("et_swtype", buf, sizeof(buf)) > 0) {
  803. if (kstrtou8(buf, 0, &et_swtype))
  804. dev_err(bgmac->dev, "Failed to parse et_swtype (%s)\n",
  805. buf);
  806. sw_type = (et_swtype & 0x0f) << 12;
  807. } else if (bgmac->feature_flags & BGMAC_FEAT_CC4_IF_SW_TYPE_RGMII) {
  808. sw_type = BGMAC_CHIPCTL_4_IF_TYPE_RGMII |
  809. BGMAC_CHIPCTL_4_SW_TYPE_RGMII;
  810. }
  811. bgmac_cco_ctl_maskset(bgmac, 4, ~(BGMAC_CHIPCTL_4_IF_TYPE_MASK |
  812. BGMAC_CHIPCTL_4_SW_TYPE_MASK),
  813. sw_type);
  814. } else if (bgmac->feature_flags & BGMAC_FEAT_CC7_IF_TYPE_RGMII) {
  815. bgmac_cco_ctl_maskset(bgmac, 7, ~BGMAC_CHIPCTL_7_IF_TYPE_MASK,
  816. BGMAC_CHIPCTL_7_IF_TYPE_RGMII);
  817. }
  818. /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_reset
  819. * Specs don't say about using BGMAC_CMDCFG_SR, but in this routine
  820. * BGMAC_CMDCFG is read _after_ putting chip in a reset. So it has to
  821. * be keps until taking MAC out of the reset.
  822. */
  823. if (bgmac->feature_flags & BGMAC_FEAT_CMDCFG_SR_REV4)
  824. cmdcfg_sr = BGMAC_CMDCFG_SR_REV4;
  825. else
  826. cmdcfg_sr = BGMAC_CMDCFG_SR_REV0;
  827. bgmac_cmdcfg_maskset(bgmac,
  828. ~(BGMAC_CMDCFG_TE |
  829. BGMAC_CMDCFG_RE |
  830. BGMAC_CMDCFG_RPI |
  831. BGMAC_CMDCFG_TAI |
  832. BGMAC_CMDCFG_HD |
  833. BGMAC_CMDCFG_ML |
  834. BGMAC_CMDCFG_CFE |
  835. BGMAC_CMDCFG_RL |
  836. BGMAC_CMDCFG_RED |
  837. BGMAC_CMDCFG_PE |
  838. BGMAC_CMDCFG_TPI |
  839. BGMAC_CMDCFG_PAD_EN |
  840. BGMAC_CMDCFG_PF),
  841. BGMAC_CMDCFG_PROM |
  842. BGMAC_CMDCFG_NLC |
  843. BGMAC_CMDCFG_CFE |
  844. cmdcfg_sr,
  845. false);
  846. bgmac->mac_speed = SPEED_UNKNOWN;
  847. bgmac->mac_duplex = DUPLEX_UNKNOWN;
  848. bgmac_clear_mib(bgmac);
  849. if (bgmac->feature_flags & BGMAC_FEAT_CMN_PHY_CTL)
  850. bgmac_cmn_maskset32(bgmac, BCMA_GMAC_CMN_PHY_CTL, ~0,
  851. BCMA_GMAC_CMN_PC_MTE);
  852. else
  853. bgmac_set(bgmac, BGMAC_PHY_CNTL, BGMAC_PC_MTE);
  854. bgmac_miiconfig(bgmac);
  855. if (bgmac->mii_bus)
  856. bgmac->mii_bus->reset(bgmac->mii_bus);
  857. netdev_reset_queue(bgmac->net_dev);
  858. }
  859. static void bgmac_chip_intrs_on(struct bgmac *bgmac)
  860. {
  861. bgmac_write(bgmac, BGMAC_INT_MASK, bgmac->int_mask);
  862. }
  863. static void bgmac_chip_intrs_off(struct bgmac *bgmac)
  864. {
  865. bgmac_write(bgmac, BGMAC_INT_MASK, 0);
  866. bgmac_read(bgmac, BGMAC_INT_MASK);
  867. }
  868. /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_enable */
  869. static void bgmac_enable(struct bgmac *bgmac)
  870. {
  871. u32 cmdcfg_sr;
  872. u32 cmdcfg;
  873. u32 mode;
  874. if (bgmac->feature_flags & BGMAC_FEAT_CMDCFG_SR_REV4)
  875. cmdcfg_sr = BGMAC_CMDCFG_SR_REV4;
  876. else
  877. cmdcfg_sr = BGMAC_CMDCFG_SR_REV0;
  878. cmdcfg = bgmac_read(bgmac, BGMAC_CMDCFG);
  879. bgmac_cmdcfg_maskset(bgmac, ~(BGMAC_CMDCFG_TE | BGMAC_CMDCFG_RE),
  880. cmdcfg_sr, true);
  881. udelay(2);
  882. cmdcfg |= BGMAC_CMDCFG_TE | BGMAC_CMDCFG_RE;
  883. bgmac_write(bgmac, BGMAC_CMDCFG, cmdcfg);
  884. mode = (bgmac_read(bgmac, BGMAC_DEV_STATUS) & BGMAC_DS_MM_MASK) >>
  885. BGMAC_DS_MM_SHIFT;
  886. if (bgmac->feature_flags & BGMAC_FEAT_CLKCTLST || mode != 0)
  887. bgmac_set(bgmac, BCMA_CLKCTLST, BCMA_CLKCTLST_FORCEHT);
  888. if (!(bgmac->feature_flags & BGMAC_FEAT_CLKCTLST) && mode == 2)
  889. bgmac_cco_ctl_maskset(bgmac, 1, ~0,
  890. BGMAC_CHIPCTL_1_RXC_DLL_BYPASS);
  891. if (bgmac->feature_flags & (BGMAC_FEAT_FLW_CTRL1 |
  892. BGMAC_FEAT_FLW_CTRL2)) {
  893. u32 fl_ctl;
  894. if (bgmac->feature_flags & BGMAC_FEAT_FLW_CTRL1)
  895. fl_ctl = 0x2300e1;
  896. else
  897. fl_ctl = 0x03cb04cb;
  898. bgmac_write(bgmac, BGMAC_FLOW_CTL_THRESH, fl_ctl);
  899. bgmac_write(bgmac, BGMAC_PAUSE_CTL, 0x27fff);
  900. }
  901. if (bgmac->feature_flags & BGMAC_FEAT_SET_RXQ_CLK) {
  902. u32 rxq_ctl;
  903. u16 bp_clk;
  904. u8 mdp;
  905. rxq_ctl = bgmac_read(bgmac, BGMAC_RXQ_CTL);
  906. rxq_ctl &= ~BGMAC_RXQ_CTL_MDP_MASK;
  907. bp_clk = bgmac_get_bus_clock(bgmac) / 1000000;
  908. mdp = (bp_clk * 128 / 1000) - 3;
  909. rxq_ctl |= (mdp << BGMAC_RXQ_CTL_MDP_SHIFT);
  910. bgmac_write(bgmac, BGMAC_RXQ_CTL, rxq_ctl);
  911. }
  912. }
  913. /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipinit */
  914. static void bgmac_chip_init(struct bgmac *bgmac)
  915. {
  916. /* Clear any erroneously pending interrupts */
  917. bgmac_write(bgmac, BGMAC_INT_STATUS, ~0);
  918. /* 1 interrupt per received frame */
  919. bgmac_write(bgmac, BGMAC_INT_RECV_LAZY, 1 << BGMAC_IRL_FC_SHIFT);
  920. /* Enable 802.3x tx flow control (honor received PAUSE frames) */
  921. bgmac_cmdcfg_maskset(bgmac, ~BGMAC_CMDCFG_RPI, 0, true);
  922. bgmac_set_rx_mode(bgmac->net_dev);
  923. bgmac_write_mac_address(bgmac, bgmac->net_dev->dev_addr);
  924. if (bgmac->loopback)
  925. bgmac_cmdcfg_maskset(bgmac, ~0, BGMAC_CMDCFG_ML, false);
  926. else
  927. bgmac_cmdcfg_maskset(bgmac, ~BGMAC_CMDCFG_ML, 0, false);
  928. bgmac_write(bgmac, BGMAC_RXMAX_LENGTH, 32 + ETHER_MAX_LEN);
  929. bgmac_chip_intrs_on(bgmac);
  930. bgmac_enable(bgmac);
  931. }
  932. static irqreturn_t bgmac_interrupt(int irq, void *dev_id)
  933. {
  934. struct bgmac *bgmac = netdev_priv(dev_id);
  935. u32 int_status = bgmac_read(bgmac, BGMAC_INT_STATUS);
  936. int_status &= bgmac->int_mask;
  937. if (!int_status)
  938. return IRQ_NONE;
  939. int_status &= ~(BGMAC_IS_TX0 | BGMAC_IS_RX);
  940. if (int_status)
  941. dev_err(bgmac->dev, "Unknown IRQs: 0x%08X\n", int_status);
  942. /* Disable new interrupts until handling existing ones */
  943. bgmac_chip_intrs_off(bgmac);
  944. napi_schedule(&bgmac->napi);
  945. return IRQ_HANDLED;
  946. }
  947. static int bgmac_poll(struct napi_struct *napi, int weight)
  948. {
  949. struct bgmac *bgmac = container_of(napi, struct bgmac, napi);
  950. int handled = 0;
  951. /* Ack */
  952. bgmac_write(bgmac, BGMAC_INT_STATUS, ~0);
  953. bgmac_dma_tx_free(bgmac, &bgmac->tx_ring[0]);
  954. handled += bgmac_dma_rx_read(bgmac, &bgmac->rx_ring[0], weight);
  955. /* Poll again if more events arrived in the meantime */
  956. if (bgmac_read(bgmac, BGMAC_INT_STATUS) & (BGMAC_IS_TX0 | BGMAC_IS_RX))
  957. return weight;
  958. if (handled < weight) {
  959. napi_complete_done(napi, handled);
  960. bgmac_chip_intrs_on(bgmac);
  961. }
  962. return handled;
  963. }
  964. /**************************************************
  965. * net_device_ops
  966. **************************************************/
  967. static int bgmac_open(struct net_device *net_dev)
  968. {
  969. struct bgmac *bgmac = netdev_priv(net_dev);
  970. int err = 0;
  971. bgmac_chip_reset(bgmac);
  972. err = bgmac_dma_init(bgmac);
  973. if (err)
  974. return err;
  975. /* Specs say about reclaiming rings here, but we do that in DMA init */
  976. bgmac_chip_init(bgmac);
  977. err = request_irq(bgmac->irq, bgmac_interrupt, IRQF_SHARED,
  978. net_dev->name, net_dev);
  979. if (err < 0) {
  980. dev_err(bgmac->dev, "IRQ request error: %d!\n", err);
  981. bgmac_dma_cleanup(bgmac);
  982. return err;
  983. }
  984. napi_enable(&bgmac->napi);
  985. phy_start(net_dev->phydev);
  986. netif_start_queue(net_dev);
  987. return 0;
  988. }
  989. static int bgmac_stop(struct net_device *net_dev)
  990. {
  991. struct bgmac *bgmac = netdev_priv(net_dev);
  992. netif_carrier_off(net_dev);
  993. phy_stop(net_dev->phydev);
  994. napi_disable(&bgmac->napi);
  995. bgmac_chip_intrs_off(bgmac);
  996. free_irq(bgmac->irq, net_dev);
  997. bgmac_chip_reset(bgmac);
  998. bgmac_dma_cleanup(bgmac);
  999. return 0;
  1000. }
  1001. static netdev_tx_t bgmac_start_xmit(struct sk_buff *skb,
  1002. struct net_device *net_dev)
  1003. {
  1004. struct bgmac *bgmac = netdev_priv(net_dev);
  1005. struct bgmac_dma_ring *ring;
  1006. /* No QOS support yet */
  1007. ring = &bgmac->tx_ring[0];
  1008. return bgmac_dma_tx_add(bgmac, ring, skb);
  1009. }
  1010. static int bgmac_set_mac_address(struct net_device *net_dev, void *addr)
  1011. {
  1012. struct bgmac *bgmac = netdev_priv(net_dev);
  1013. struct sockaddr *sa = addr;
  1014. int ret;
  1015. ret = eth_prepare_mac_addr_change(net_dev, addr);
  1016. if (ret < 0)
  1017. return ret;
  1018. ether_addr_copy(net_dev->dev_addr, sa->sa_data);
  1019. bgmac_write_mac_address(bgmac, net_dev->dev_addr);
  1020. eth_commit_mac_addr_change(net_dev, addr);
  1021. return 0;
  1022. }
  1023. static int bgmac_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
  1024. {
  1025. if (!netif_running(net_dev))
  1026. return -EINVAL;
  1027. return phy_mii_ioctl(net_dev->phydev, ifr, cmd);
  1028. }
  1029. static const struct net_device_ops bgmac_netdev_ops = {
  1030. .ndo_open = bgmac_open,
  1031. .ndo_stop = bgmac_stop,
  1032. .ndo_start_xmit = bgmac_start_xmit,
  1033. .ndo_set_rx_mode = bgmac_set_rx_mode,
  1034. .ndo_set_mac_address = bgmac_set_mac_address,
  1035. .ndo_validate_addr = eth_validate_addr,
  1036. .ndo_do_ioctl = bgmac_ioctl,
  1037. };
  1038. /**************************************************
  1039. * ethtool_ops
  1040. **************************************************/
  1041. struct bgmac_stat {
  1042. u8 size;
  1043. u32 offset;
  1044. const char *name;
  1045. };
  1046. static struct bgmac_stat bgmac_get_strings_stats[] = {
  1047. { 8, BGMAC_TX_GOOD_OCTETS, "tx_good_octets" },
  1048. { 4, BGMAC_TX_GOOD_PKTS, "tx_good" },
  1049. { 8, BGMAC_TX_OCTETS, "tx_octets" },
  1050. { 4, BGMAC_TX_PKTS, "tx_pkts" },
  1051. { 4, BGMAC_TX_BROADCAST_PKTS, "tx_broadcast" },
  1052. { 4, BGMAC_TX_MULTICAST_PKTS, "tx_multicast" },
  1053. { 4, BGMAC_TX_LEN_64, "tx_64" },
  1054. { 4, BGMAC_TX_LEN_65_TO_127, "tx_65_127" },
  1055. { 4, BGMAC_TX_LEN_128_TO_255, "tx_128_255" },
  1056. { 4, BGMAC_TX_LEN_256_TO_511, "tx_256_511" },
  1057. { 4, BGMAC_TX_LEN_512_TO_1023, "tx_512_1023" },
  1058. { 4, BGMAC_TX_LEN_1024_TO_1522, "tx_1024_1522" },
  1059. { 4, BGMAC_TX_LEN_1523_TO_2047, "tx_1523_2047" },
  1060. { 4, BGMAC_TX_LEN_2048_TO_4095, "tx_2048_4095" },
  1061. { 4, BGMAC_TX_LEN_4096_TO_8191, "tx_4096_8191" },
  1062. { 4, BGMAC_TX_LEN_8192_TO_MAX, "tx_8192_max" },
  1063. { 4, BGMAC_TX_JABBER_PKTS, "tx_jabber" },
  1064. { 4, BGMAC_TX_OVERSIZE_PKTS, "tx_oversize" },
  1065. { 4, BGMAC_TX_FRAGMENT_PKTS, "tx_fragment" },
  1066. { 4, BGMAC_TX_UNDERRUNS, "tx_underruns" },
  1067. { 4, BGMAC_TX_TOTAL_COLS, "tx_total_cols" },
  1068. { 4, BGMAC_TX_SINGLE_COLS, "tx_single_cols" },
  1069. { 4, BGMAC_TX_MULTIPLE_COLS, "tx_multiple_cols" },
  1070. { 4, BGMAC_TX_EXCESSIVE_COLS, "tx_excessive_cols" },
  1071. { 4, BGMAC_TX_LATE_COLS, "tx_late_cols" },
  1072. { 4, BGMAC_TX_DEFERED, "tx_defered" },
  1073. { 4, BGMAC_TX_CARRIER_LOST, "tx_carrier_lost" },
  1074. { 4, BGMAC_TX_PAUSE_PKTS, "tx_pause" },
  1075. { 4, BGMAC_TX_UNI_PKTS, "tx_unicast" },
  1076. { 4, BGMAC_TX_Q0_PKTS, "tx_q0" },
  1077. { 8, BGMAC_TX_Q0_OCTETS, "tx_q0_octets" },
  1078. { 4, BGMAC_TX_Q1_PKTS, "tx_q1" },
  1079. { 8, BGMAC_TX_Q1_OCTETS, "tx_q1_octets" },
  1080. { 4, BGMAC_TX_Q2_PKTS, "tx_q2" },
  1081. { 8, BGMAC_TX_Q2_OCTETS, "tx_q2_octets" },
  1082. { 4, BGMAC_TX_Q3_PKTS, "tx_q3" },
  1083. { 8, BGMAC_TX_Q3_OCTETS, "tx_q3_octets" },
  1084. { 8, BGMAC_RX_GOOD_OCTETS, "rx_good_octets" },
  1085. { 4, BGMAC_RX_GOOD_PKTS, "rx_good" },
  1086. { 8, BGMAC_RX_OCTETS, "rx_octets" },
  1087. { 4, BGMAC_RX_PKTS, "rx_pkts" },
  1088. { 4, BGMAC_RX_BROADCAST_PKTS, "rx_broadcast" },
  1089. { 4, BGMAC_RX_MULTICAST_PKTS, "rx_multicast" },
  1090. { 4, BGMAC_RX_LEN_64, "rx_64" },
  1091. { 4, BGMAC_RX_LEN_65_TO_127, "rx_65_127" },
  1092. { 4, BGMAC_RX_LEN_128_TO_255, "rx_128_255" },
  1093. { 4, BGMAC_RX_LEN_256_TO_511, "rx_256_511" },
  1094. { 4, BGMAC_RX_LEN_512_TO_1023, "rx_512_1023" },
  1095. { 4, BGMAC_RX_LEN_1024_TO_1522, "rx_1024_1522" },
  1096. { 4, BGMAC_RX_LEN_1523_TO_2047, "rx_1523_2047" },
  1097. { 4, BGMAC_RX_LEN_2048_TO_4095, "rx_2048_4095" },
  1098. { 4, BGMAC_RX_LEN_4096_TO_8191, "rx_4096_8191" },
  1099. { 4, BGMAC_RX_LEN_8192_TO_MAX, "rx_8192_max" },
  1100. { 4, BGMAC_RX_JABBER_PKTS, "rx_jabber" },
  1101. { 4, BGMAC_RX_OVERSIZE_PKTS, "rx_oversize" },
  1102. { 4, BGMAC_RX_FRAGMENT_PKTS, "rx_fragment" },
  1103. { 4, BGMAC_RX_MISSED_PKTS, "rx_missed" },
  1104. { 4, BGMAC_RX_CRC_ALIGN_ERRS, "rx_crc_align" },
  1105. { 4, BGMAC_RX_UNDERSIZE, "rx_undersize" },
  1106. { 4, BGMAC_RX_CRC_ERRS, "rx_crc" },
  1107. { 4, BGMAC_RX_ALIGN_ERRS, "rx_align" },
  1108. { 4, BGMAC_RX_SYMBOL_ERRS, "rx_symbol" },
  1109. { 4, BGMAC_RX_PAUSE_PKTS, "rx_pause" },
  1110. { 4, BGMAC_RX_NONPAUSE_PKTS, "rx_nonpause" },
  1111. { 4, BGMAC_RX_SACHANGES, "rx_sa_changes" },
  1112. { 4, BGMAC_RX_UNI_PKTS, "rx_unicast" },
  1113. };
  1114. #define BGMAC_STATS_LEN ARRAY_SIZE(bgmac_get_strings_stats)
  1115. static int bgmac_get_sset_count(struct net_device *dev, int string_set)
  1116. {
  1117. switch (string_set) {
  1118. case ETH_SS_STATS:
  1119. return BGMAC_STATS_LEN;
  1120. }
  1121. return -EOPNOTSUPP;
  1122. }
  1123. static void bgmac_get_strings(struct net_device *dev, u32 stringset,
  1124. u8 *data)
  1125. {
  1126. int i;
  1127. if (stringset != ETH_SS_STATS)
  1128. return;
  1129. for (i = 0; i < BGMAC_STATS_LEN; i++)
  1130. strlcpy(data + i * ETH_GSTRING_LEN,
  1131. bgmac_get_strings_stats[i].name, ETH_GSTRING_LEN);
  1132. }
  1133. static void bgmac_get_ethtool_stats(struct net_device *dev,
  1134. struct ethtool_stats *ss, uint64_t *data)
  1135. {
  1136. struct bgmac *bgmac = netdev_priv(dev);
  1137. const struct bgmac_stat *s;
  1138. unsigned int i;
  1139. u64 val;
  1140. if (!netif_running(dev))
  1141. return;
  1142. for (i = 0; i < BGMAC_STATS_LEN; i++) {
  1143. s = &bgmac_get_strings_stats[i];
  1144. val = 0;
  1145. if (s->size == 8)
  1146. val = (u64)bgmac_read(bgmac, s->offset + 4) << 32;
  1147. val |= bgmac_read(bgmac, s->offset);
  1148. data[i] = val;
  1149. }
  1150. }
  1151. static void bgmac_get_drvinfo(struct net_device *net_dev,
  1152. struct ethtool_drvinfo *info)
  1153. {
  1154. strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
  1155. strlcpy(info->bus_info, "AXI", sizeof(info->bus_info));
  1156. }
  1157. static const struct ethtool_ops bgmac_ethtool_ops = {
  1158. .get_strings = bgmac_get_strings,
  1159. .get_sset_count = bgmac_get_sset_count,
  1160. .get_ethtool_stats = bgmac_get_ethtool_stats,
  1161. .get_drvinfo = bgmac_get_drvinfo,
  1162. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  1163. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  1164. };
  1165. /**************************************************
  1166. * MII
  1167. **************************************************/
  1168. void bgmac_adjust_link(struct net_device *net_dev)
  1169. {
  1170. struct bgmac *bgmac = netdev_priv(net_dev);
  1171. struct phy_device *phy_dev = net_dev->phydev;
  1172. bool update = false;
  1173. if (phy_dev->link) {
  1174. if (phy_dev->speed != bgmac->mac_speed) {
  1175. bgmac->mac_speed = phy_dev->speed;
  1176. update = true;
  1177. }
  1178. if (phy_dev->duplex != bgmac->mac_duplex) {
  1179. bgmac->mac_duplex = phy_dev->duplex;
  1180. update = true;
  1181. }
  1182. }
  1183. if (update) {
  1184. bgmac_mac_speed(bgmac);
  1185. phy_print_status(phy_dev);
  1186. }
  1187. }
  1188. EXPORT_SYMBOL_GPL(bgmac_adjust_link);
  1189. int bgmac_phy_connect_direct(struct bgmac *bgmac)
  1190. {
  1191. struct fixed_phy_status fphy_status = {
  1192. .link = 1,
  1193. .speed = SPEED_1000,
  1194. .duplex = DUPLEX_FULL,
  1195. };
  1196. struct phy_device *phy_dev;
  1197. int err;
  1198. phy_dev = fixed_phy_register(PHY_POLL, &fphy_status, -1, NULL);
  1199. if (!phy_dev || IS_ERR(phy_dev)) {
  1200. dev_err(bgmac->dev, "Failed to register fixed PHY device\n");
  1201. return -ENODEV;
  1202. }
  1203. err = phy_connect_direct(bgmac->net_dev, phy_dev, bgmac_adjust_link,
  1204. PHY_INTERFACE_MODE_MII);
  1205. if (err) {
  1206. dev_err(bgmac->dev, "Connecting PHY failed\n");
  1207. return err;
  1208. }
  1209. return err;
  1210. }
  1211. EXPORT_SYMBOL_GPL(bgmac_phy_connect_direct);
  1212. struct bgmac *bgmac_alloc(struct device *dev)
  1213. {
  1214. struct net_device *net_dev;
  1215. struct bgmac *bgmac;
  1216. /* Allocation and references */
  1217. net_dev = devm_alloc_etherdev(dev, sizeof(*bgmac));
  1218. if (!net_dev)
  1219. return NULL;
  1220. net_dev->netdev_ops = &bgmac_netdev_ops;
  1221. net_dev->ethtool_ops = &bgmac_ethtool_ops;
  1222. bgmac = netdev_priv(net_dev);
  1223. bgmac->dev = dev;
  1224. bgmac->net_dev = net_dev;
  1225. return bgmac;
  1226. }
  1227. EXPORT_SYMBOL_GPL(bgmac_alloc);
  1228. int bgmac_enet_probe(struct bgmac *bgmac)
  1229. {
  1230. struct net_device *net_dev = bgmac->net_dev;
  1231. int err;
  1232. bgmac_chip_intrs_off(bgmac);
  1233. net_dev->irq = bgmac->irq;
  1234. SET_NETDEV_DEV(net_dev, bgmac->dev);
  1235. dev_set_drvdata(bgmac->dev, bgmac);
  1236. if (!is_valid_ether_addr(net_dev->dev_addr)) {
  1237. dev_err(bgmac->dev, "Invalid MAC addr: %pM\n",
  1238. net_dev->dev_addr);
  1239. eth_hw_addr_random(net_dev);
  1240. dev_warn(bgmac->dev, "Using random MAC: %pM\n",
  1241. net_dev->dev_addr);
  1242. }
  1243. /* This (reset &) enable is not preset in specs or reference driver but
  1244. * Broadcom does it in arch PCI code when enabling fake PCI device.
  1245. */
  1246. bgmac_clk_enable(bgmac, 0);
  1247. /* This seems to be fixing IRQ by assigning OOB #6 to the core */
  1248. if (!(bgmac->feature_flags & BGMAC_FEAT_IDM_MASK)) {
  1249. if (bgmac->feature_flags & BGMAC_FEAT_IRQ_ID_OOB_6)
  1250. bgmac_idm_write(bgmac, BCMA_OOB_SEL_OUT_A30, 0x86);
  1251. }
  1252. bgmac_chip_reset(bgmac);
  1253. err = bgmac_dma_alloc(bgmac);
  1254. if (err) {
  1255. dev_err(bgmac->dev, "Unable to alloc memory for DMA\n");
  1256. goto err_out;
  1257. }
  1258. bgmac->int_mask = BGMAC_IS_ERRMASK | BGMAC_IS_RX | BGMAC_IS_TX_MASK;
  1259. if (bcm47xx_nvram_getenv("et0_no_txint", NULL, 0) == 0)
  1260. bgmac->int_mask &= ~BGMAC_IS_TX_MASK;
  1261. netif_napi_add(net_dev, &bgmac->napi, bgmac_poll, BGMAC_WEIGHT);
  1262. err = bgmac_phy_connect(bgmac);
  1263. if (err) {
  1264. dev_err(bgmac->dev, "Cannot connect to phy\n");
  1265. goto err_dma_free;
  1266. }
  1267. net_dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
  1268. net_dev->hw_features = net_dev->features;
  1269. net_dev->vlan_features = net_dev->features;
  1270. err = register_netdev(bgmac->net_dev);
  1271. if (err) {
  1272. dev_err(bgmac->dev, "Cannot register net device\n");
  1273. goto err_phy_disconnect;
  1274. }
  1275. netif_carrier_off(net_dev);
  1276. return 0;
  1277. err_phy_disconnect:
  1278. phy_disconnect(net_dev->phydev);
  1279. err_dma_free:
  1280. bgmac_dma_free(bgmac);
  1281. err_out:
  1282. return err;
  1283. }
  1284. EXPORT_SYMBOL_GPL(bgmac_enet_probe);
  1285. void bgmac_enet_remove(struct bgmac *bgmac)
  1286. {
  1287. unregister_netdev(bgmac->net_dev);
  1288. phy_disconnect(bgmac->net_dev->phydev);
  1289. netif_napi_del(&bgmac->napi);
  1290. bgmac_dma_free(bgmac);
  1291. free_netdev(bgmac->net_dev);
  1292. }
  1293. EXPORT_SYMBOL_GPL(bgmac_enet_remove);
  1294. int bgmac_enet_suspend(struct bgmac *bgmac)
  1295. {
  1296. if (!netif_running(bgmac->net_dev))
  1297. return 0;
  1298. phy_stop(bgmac->net_dev->phydev);
  1299. netif_stop_queue(bgmac->net_dev);
  1300. napi_disable(&bgmac->napi);
  1301. netif_tx_lock(bgmac->net_dev);
  1302. netif_device_detach(bgmac->net_dev);
  1303. netif_tx_unlock(bgmac->net_dev);
  1304. bgmac_chip_intrs_off(bgmac);
  1305. bgmac_chip_reset(bgmac);
  1306. bgmac_dma_cleanup(bgmac);
  1307. return 0;
  1308. }
  1309. EXPORT_SYMBOL_GPL(bgmac_enet_suspend);
  1310. int bgmac_enet_resume(struct bgmac *bgmac)
  1311. {
  1312. int rc;
  1313. if (!netif_running(bgmac->net_dev))
  1314. return 0;
  1315. rc = bgmac_dma_init(bgmac);
  1316. if (rc)
  1317. return rc;
  1318. bgmac_chip_init(bgmac);
  1319. napi_enable(&bgmac->napi);
  1320. netif_tx_lock(bgmac->net_dev);
  1321. netif_device_attach(bgmac->net_dev);
  1322. netif_tx_unlock(bgmac->net_dev);
  1323. netif_start_queue(bgmac->net_dev);
  1324. phy_start(bgmac->net_dev->phydev);
  1325. return 0;
  1326. }
  1327. EXPORT_SYMBOL_GPL(bgmac_enet_resume);
  1328. MODULE_AUTHOR("Rafał Miłecki");
  1329. MODULE_LICENSE("GPL");