bgmac.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727
  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. #include "bgmac.h"
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/delay.h>
  12. #include <linux/etherdevice.h>
  13. #include <linux/mii.h>
  14. #include <linux/phy.h>
  15. #include <linux/phy_fixed.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/dma-mapping.h>
  18. #include <linux/bcm47xx_nvram.h>
  19. static const struct bcma_device_id bgmac_bcma_tbl[] = {
  20. BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_4706_MAC_GBIT, BCMA_ANY_REV, BCMA_ANY_CLASS),
  21. BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_MAC_GBIT, BCMA_ANY_REV, BCMA_ANY_CLASS),
  22. {},
  23. };
  24. MODULE_DEVICE_TABLE(bcma, bgmac_bcma_tbl);
  25. static bool bgmac_wait_value(struct bcma_device *core, u16 reg, u32 mask,
  26. u32 value, int timeout)
  27. {
  28. u32 val;
  29. int i;
  30. for (i = 0; i < timeout / 10; i++) {
  31. val = bcma_read32(core, reg);
  32. if ((val & mask) == value)
  33. return true;
  34. udelay(10);
  35. }
  36. pr_err("Timeout waiting for reg 0x%X\n", reg);
  37. return false;
  38. }
  39. /**************************************************
  40. * DMA
  41. **************************************************/
  42. static void bgmac_dma_tx_reset(struct bgmac *bgmac, struct bgmac_dma_ring *ring)
  43. {
  44. u32 val;
  45. int i;
  46. if (!ring->mmio_base)
  47. return;
  48. /* Suspend DMA TX ring first.
  49. * bgmac_wait_value doesn't support waiting for any of few values, so
  50. * implement whole loop here.
  51. */
  52. bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL,
  53. BGMAC_DMA_TX_SUSPEND);
  54. for (i = 0; i < 10000 / 10; i++) {
  55. val = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_STATUS);
  56. val &= BGMAC_DMA_TX_STAT;
  57. if (val == BGMAC_DMA_TX_STAT_DISABLED ||
  58. val == BGMAC_DMA_TX_STAT_IDLEWAIT ||
  59. val == BGMAC_DMA_TX_STAT_STOPPED) {
  60. i = 0;
  61. break;
  62. }
  63. udelay(10);
  64. }
  65. if (i)
  66. bgmac_err(bgmac, "Timeout suspending DMA TX ring 0x%X (BGMAC_DMA_TX_STAT: 0x%08X)\n",
  67. ring->mmio_base, val);
  68. /* Remove SUSPEND bit */
  69. bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL, 0);
  70. if (!bgmac_wait_value(bgmac->core,
  71. ring->mmio_base + BGMAC_DMA_TX_STATUS,
  72. BGMAC_DMA_TX_STAT, BGMAC_DMA_TX_STAT_DISABLED,
  73. 10000)) {
  74. bgmac_warn(bgmac, "DMA TX ring 0x%X wasn't disabled on time, waiting additional 300us\n",
  75. ring->mmio_base);
  76. udelay(300);
  77. val = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_STATUS);
  78. if ((val & BGMAC_DMA_TX_STAT) != BGMAC_DMA_TX_STAT_DISABLED)
  79. bgmac_err(bgmac, "Reset of DMA TX ring 0x%X failed\n",
  80. ring->mmio_base);
  81. }
  82. }
  83. static void bgmac_dma_tx_enable(struct bgmac *bgmac,
  84. struct bgmac_dma_ring *ring)
  85. {
  86. u32 ctl;
  87. ctl = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL);
  88. if (bgmac->core->id.rev >= 4) {
  89. ctl &= ~BGMAC_DMA_TX_BL_MASK;
  90. ctl |= BGMAC_DMA_TX_BL_128 << BGMAC_DMA_TX_BL_SHIFT;
  91. ctl &= ~BGMAC_DMA_TX_MR_MASK;
  92. ctl |= BGMAC_DMA_TX_MR_2 << BGMAC_DMA_TX_MR_SHIFT;
  93. ctl &= ~BGMAC_DMA_TX_PC_MASK;
  94. ctl |= BGMAC_DMA_TX_PC_16 << BGMAC_DMA_TX_PC_SHIFT;
  95. ctl &= ~BGMAC_DMA_TX_PT_MASK;
  96. ctl |= BGMAC_DMA_TX_PT_8 << BGMAC_DMA_TX_PT_SHIFT;
  97. }
  98. ctl |= BGMAC_DMA_TX_ENABLE;
  99. ctl |= BGMAC_DMA_TX_PARITY_DISABLE;
  100. bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_CTL, ctl);
  101. }
  102. static void
  103. bgmac_dma_tx_add_buf(struct bgmac *bgmac, struct bgmac_dma_ring *ring,
  104. int i, int len, u32 ctl0)
  105. {
  106. struct bgmac_slot_info *slot;
  107. struct bgmac_dma_desc *dma_desc;
  108. u32 ctl1;
  109. if (i == BGMAC_TX_RING_SLOTS - 1)
  110. ctl0 |= BGMAC_DESC_CTL0_EOT;
  111. ctl1 = len & BGMAC_DESC_CTL1_LEN;
  112. slot = &ring->slots[i];
  113. dma_desc = &ring->cpu_base[i];
  114. dma_desc->addr_low = cpu_to_le32(lower_32_bits(slot->dma_addr));
  115. dma_desc->addr_high = cpu_to_le32(upper_32_bits(slot->dma_addr));
  116. dma_desc->ctl0 = cpu_to_le32(ctl0);
  117. dma_desc->ctl1 = cpu_to_le32(ctl1);
  118. }
  119. static netdev_tx_t bgmac_dma_tx_add(struct bgmac *bgmac,
  120. struct bgmac_dma_ring *ring,
  121. struct sk_buff *skb)
  122. {
  123. struct device *dma_dev = bgmac->core->dma_dev;
  124. struct net_device *net_dev = bgmac->net_dev;
  125. int index = ring->end % BGMAC_TX_RING_SLOTS;
  126. struct bgmac_slot_info *slot = &ring->slots[index];
  127. int nr_frags;
  128. u32 flags;
  129. int i;
  130. if (skb->len > BGMAC_DESC_CTL1_LEN) {
  131. bgmac_err(bgmac, "Too long skb (%d)\n", skb->len);
  132. goto err_drop;
  133. }
  134. if (skb->ip_summed == CHECKSUM_PARTIAL)
  135. skb_checksum_help(skb);
  136. nr_frags = skb_shinfo(skb)->nr_frags;
  137. /* ring->end - ring->start will return the number of valid slots,
  138. * even when ring->end overflows
  139. */
  140. if (ring->end - ring->start + nr_frags + 1 >= BGMAC_TX_RING_SLOTS) {
  141. bgmac_err(bgmac, "TX ring is full, queue should be stopped!\n");
  142. netif_stop_queue(net_dev);
  143. return NETDEV_TX_BUSY;
  144. }
  145. slot->dma_addr = dma_map_single(dma_dev, skb->data, skb_headlen(skb),
  146. DMA_TO_DEVICE);
  147. if (unlikely(dma_mapping_error(dma_dev, slot->dma_addr)))
  148. goto err_dma_head;
  149. flags = BGMAC_DESC_CTL0_SOF;
  150. if (!nr_frags)
  151. flags |= BGMAC_DESC_CTL0_EOF | BGMAC_DESC_CTL0_IOC;
  152. bgmac_dma_tx_add_buf(bgmac, ring, index, skb_headlen(skb), flags);
  153. flags = 0;
  154. for (i = 0; i < nr_frags; i++) {
  155. struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i];
  156. int len = skb_frag_size(frag);
  157. index = (index + 1) % BGMAC_TX_RING_SLOTS;
  158. slot = &ring->slots[index];
  159. slot->dma_addr = skb_frag_dma_map(dma_dev, frag, 0,
  160. len, DMA_TO_DEVICE);
  161. if (unlikely(dma_mapping_error(dma_dev, slot->dma_addr)))
  162. goto err_dma;
  163. if (i == nr_frags - 1)
  164. flags |= BGMAC_DESC_CTL0_EOF | BGMAC_DESC_CTL0_IOC;
  165. bgmac_dma_tx_add_buf(bgmac, ring, index, len, flags);
  166. }
  167. slot->skb = skb;
  168. ring->end += nr_frags + 1;
  169. netdev_sent_queue(net_dev, skb->len);
  170. wmb();
  171. /* Increase ring->end to point empty slot. We tell hardware the first
  172. * slot it should *not* read.
  173. */
  174. bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_INDEX,
  175. ring->index_base +
  176. (ring->end % BGMAC_TX_RING_SLOTS) *
  177. sizeof(struct bgmac_dma_desc));
  178. if (ring->end - ring->start >= BGMAC_TX_RING_SLOTS - 8)
  179. netif_stop_queue(net_dev);
  180. return NETDEV_TX_OK;
  181. err_dma:
  182. dma_unmap_single(dma_dev, slot->dma_addr, skb_headlen(skb),
  183. DMA_TO_DEVICE);
  184. while (i > 0) {
  185. int index = (ring->end + i) % BGMAC_TX_RING_SLOTS;
  186. struct bgmac_slot_info *slot = &ring->slots[index];
  187. u32 ctl1 = le32_to_cpu(ring->cpu_base[index].ctl1);
  188. int len = ctl1 & BGMAC_DESC_CTL1_LEN;
  189. dma_unmap_page(dma_dev, slot->dma_addr, len, DMA_TO_DEVICE);
  190. }
  191. err_dma_head:
  192. bgmac_err(bgmac, "Mapping error of skb on ring 0x%X\n",
  193. ring->mmio_base);
  194. err_drop:
  195. dev_kfree_skb(skb);
  196. return NETDEV_TX_OK;
  197. }
  198. /* Free transmitted packets */
  199. static void bgmac_dma_tx_free(struct bgmac *bgmac, struct bgmac_dma_ring *ring)
  200. {
  201. struct device *dma_dev = bgmac->core->dma_dev;
  202. int empty_slot;
  203. bool freed = false;
  204. unsigned bytes_compl = 0, pkts_compl = 0;
  205. /* The last slot that hardware didn't consume yet */
  206. empty_slot = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_STATUS);
  207. empty_slot &= BGMAC_DMA_TX_STATDPTR;
  208. empty_slot -= ring->index_base;
  209. empty_slot &= BGMAC_DMA_TX_STATDPTR;
  210. empty_slot /= sizeof(struct bgmac_dma_desc);
  211. while (ring->start != ring->end) {
  212. int slot_idx = ring->start % BGMAC_TX_RING_SLOTS;
  213. struct bgmac_slot_info *slot = &ring->slots[slot_idx];
  214. u32 ctl1;
  215. int len;
  216. if (slot_idx == empty_slot)
  217. break;
  218. ctl1 = le32_to_cpu(ring->cpu_base[slot_idx].ctl1);
  219. len = ctl1 & BGMAC_DESC_CTL1_LEN;
  220. if (ctl1 & BGMAC_DESC_CTL0_SOF)
  221. /* Unmap no longer used buffer */
  222. dma_unmap_single(dma_dev, slot->dma_addr, len,
  223. DMA_TO_DEVICE);
  224. else
  225. dma_unmap_page(dma_dev, slot->dma_addr, len,
  226. DMA_TO_DEVICE);
  227. if (slot->skb) {
  228. bytes_compl += slot->skb->len;
  229. pkts_compl++;
  230. /* Free memory! :) */
  231. dev_kfree_skb(slot->skb);
  232. slot->skb = NULL;
  233. }
  234. slot->dma_addr = 0;
  235. ring->start++;
  236. freed = true;
  237. }
  238. if (!pkts_compl)
  239. return;
  240. netdev_completed_queue(bgmac->net_dev, pkts_compl, bytes_compl);
  241. if (netif_queue_stopped(bgmac->net_dev))
  242. netif_wake_queue(bgmac->net_dev);
  243. }
  244. static void bgmac_dma_rx_reset(struct bgmac *bgmac, struct bgmac_dma_ring *ring)
  245. {
  246. if (!ring->mmio_base)
  247. return;
  248. bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL, 0);
  249. if (!bgmac_wait_value(bgmac->core,
  250. ring->mmio_base + BGMAC_DMA_RX_STATUS,
  251. BGMAC_DMA_RX_STAT, BGMAC_DMA_RX_STAT_DISABLED,
  252. 10000))
  253. bgmac_err(bgmac, "Reset of ring 0x%X RX failed\n",
  254. ring->mmio_base);
  255. }
  256. static void bgmac_dma_rx_enable(struct bgmac *bgmac,
  257. struct bgmac_dma_ring *ring)
  258. {
  259. u32 ctl;
  260. ctl = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL);
  261. if (bgmac->core->id.rev >= 4) {
  262. ctl &= ~BGMAC_DMA_RX_BL_MASK;
  263. ctl |= BGMAC_DMA_RX_BL_128 << BGMAC_DMA_RX_BL_SHIFT;
  264. ctl &= ~BGMAC_DMA_RX_PC_MASK;
  265. ctl |= BGMAC_DMA_RX_PC_8 << BGMAC_DMA_RX_PC_SHIFT;
  266. ctl &= ~BGMAC_DMA_RX_PT_MASK;
  267. ctl |= BGMAC_DMA_RX_PT_1 << BGMAC_DMA_RX_PT_SHIFT;
  268. }
  269. ctl &= BGMAC_DMA_RX_ADDREXT_MASK;
  270. ctl |= BGMAC_DMA_RX_ENABLE;
  271. ctl |= BGMAC_DMA_RX_PARITY_DISABLE;
  272. ctl |= BGMAC_DMA_RX_OVERFLOW_CONT;
  273. ctl |= BGMAC_RX_FRAME_OFFSET << BGMAC_DMA_RX_FRAME_OFFSET_SHIFT;
  274. bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_CTL, ctl);
  275. }
  276. static int bgmac_dma_rx_skb_for_slot(struct bgmac *bgmac,
  277. struct bgmac_slot_info *slot)
  278. {
  279. struct device *dma_dev = bgmac->core->dma_dev;
  280. dma_addr_t dma_addr;
  281. struct bgmac_rx_header *rx;
  282. void *buf;
  283. /* Alloc skb */
  284. buf = netdev_alloc_frag(BGMAC_RX_ALLOC_SIZE);
  285. if (!buf)
  286. return -ENOMEM;
  287. /* Poison - if everything goes fine, hardware will overwrite it */
  288. rx = buf + BGMAC_RX_BUF_OFFSET;
  289. rx->len = cpu_to_le16(0xdead);
  290. rx->flags = cpu_to_le16(0xbeef);
  291. /* Map skb for the DMA */
  292. dma_addr = dma_map_single(dma_dev, buf + BGMAC_RX_BUF_OFFSET,
  293. BGMAC_RX_BUF_SIZE, DMA_FROM_DEVICE);
  294. if (dma_mapping_error(dma_dev, dma_addr)) {
  295. bgmac_err(bgmac, "DMA mapping error\n");
  296. put_page(virt_to_head_page(buf));
  297. return -ENOMEM;
  298. }
  299. /* Update the slot */
  300. slot->buf = buf;
  301. slot->dma_addr = dma_addr;
  302. return 0;
  303. }
  304. static void bgmac_dma_rx_update_index(struct bgmac *bgmac,
  305. struct bgmac_dma_ring *ring)
  306. {
  307. dma_wmb();
  308. bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_INDEX,
  309. ring->index_base +
  310. ring->end * sizeof(struct bgmac_dma_desc));
  311. }
  312. static void bgmac_dma_rx_setup_desc(struct bgmac *bgmac,
  313. struct bgmac_dma_ring *ring, int desc_idx)
  314. {
  315. struct bgmac_dma_desc *dma_desc = ring->cpu_base + desc_idx;
  316. u32 ctl0 = 0, ctl1 = 0;
  317. if (desc_idx == BGMAC_RX_RING_SLOTS - 1)
  318. ctl0 |= BGMAC_DESC_CTL0_EOT;
  319. ctl1 |= BGMAC_RX_BUF_SIZE & BGMAC_DESC_CTL1_LEN;
  320. /* Is there any BGMAC device that requires extension? */
  321. /* ctl1 |= (addrext << B43_DMA64_DCTL1_ADDREXT_SHIFT) &
  322. * B43_DMA64_DCTL1_ADDREXT_MASK;
  323. */
  324. dma_desc->addr_low = cpu_to_le32(lower_32_bits(ring->slots[desc_idx].dma_addr));
  325. dma_desc->addr_high = cpu_to_le32(upper_32_bits(ring->slots[desc_idx].dma_addr));
  326. dma_desc->ctl0 = cpu_to_le32(ctl0);
  327. dma_desc->ctl1 = cpu_to_le32(ctl1);
  328. ring->end = desc_idx;
  329. }
  330. static void bgmac_dma_rx_poison_buf(struct device *dma_dev,
  331. struct bgmac_slot_info *slot)
  332. {
  333. struct bgmac_rx_header *rx = slot->buf + BGMAC_RX_BUF_OFFSET;
  334. dma_sync_single_for_cpu(dma_dev, slot->dma_addr, BGMAC_RX_BUF_SIZE,
  335. DMA_FROM_DEVICE);
  336. rx->len = cpu_to_le16(0xdead);
  337. rx->flags = cpu_to_le16(0xbeef);
  338. dma_sync_single_for_device(dma_dev, slot->dma_addr, BGMAC_RX_BUF_SIZE,
  339. DMA_FROM_DEVICE);
  340. }
  341. static int bgmac_dma_rx_read(struct bgmac *bgmac, struct bgmac_dma_ring *ring,
  342. int weight)
  343. {
  344. u32 end_slot;
  345. int handled = 0;
  346. end_slot = bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_STATUS);
  347. end_slot &= BGMAC_DMA_RX_STATDPTR;
  348. end_slot -= ring->index_base;
  349. end_slot &= BGMAC_DMA_RX_STATDPTR;
  350. end_slot /= sizeof(struct bgmac_dma_desc);
  351. while (ring->start != end_slot) {
  352. struct device *dma_dev = bgmac->core->dma_dev;
  353. struct bgmac_slot_info *slot = &ring->slots[ring->start];
  354. struct bgmac_rx_header *rx = slot->buf + BGMAC_RX_BUF_OFFSET;
  355. struct sk_buff *skb;
  356. void *buf = slot->buf;
  357. dma_addr_t dma_addr = slot->dma_addr;
  358. u16 len, flags;
  359. do {
  360. /* Prepare new skb as replacement */
  361. if (bgmac_dma_rx_skb_for_slot(bgmac, slot)) {
  362. bgmac_dma_rx_poison_buf(dma_dev, slot);
  363. break;
  364. }
  365. /* Unmap buffer to make it accessible to the CPU */
  366. dma_unmap_single(dma_dev, dma_addr,
  367. BGMAC_RX_BUF_SIZE, DMA_FROM_DEVICE);
  368. /* Get info from the header */
  369. len = le16_to_cpu(rx->len);
  370. flags = le16_to_cpu(rx->flags);
  371. /* Check for poison and drop or pass the packet */
  372. if (len == 0xdead && flags == 0xbeef) {
  373. bgmac_err(bgmac, "Found poisoned packet at slot %d, DMA issue!\n",
  374. ring->start);
  375. put_page(virt_to_head_page(buf));
  376. break;
  377. }
  378. if (len > BGMAC_RX_ALLOC_SIZE) {
  379. bgmac_err(bgmac, "Found oversized packet at slot %d, DMA issue!\n",
  380. ring->start);
  381. put_page(virt_to_head_page(buf));
  382. break;
  383. }
  384. /* Omit CRC. */
  385. len -= ETH_FCS_LEN;
  386. skb = build_skb(buf, BGMAC_RX_ALLOC_SIZE);
  387. skb_put(skb, BGMAC_RX_FRAME_OFFSET +
  388. BGMAC_RX_BUF_OFFSET + len);
  389. skb_pull(skb, BGMAC_RX_FRAME_OFFSET +
  390. BGMAC_RX_BUF_OFFSET);
  391. skb_checksum_none_assert(skb);
  392. skb->protocol = eth_type_trans(skb, bgmac->net_dev);
  393. napi_gro_receive(&bgmac->napi, skb);
  394. handled++;
  395. } while (0);
  396. bgmac_dma_rx_setup_desc(bgmac, ring, ring->start);
  397. if (++ring->start >= BGMAC_RX_RING_SLOTS)
  398. ring->start = 0;
  399. if (handled >= weight) /* Should never be greater */
  400. break;
  401. }
  402. bgmac_dma_rx_update_index(bgmac, ring);
  403. return handled;
  404. }
  405. /* Does ring support unaligned addressing? */
  406. static bool bgmac_dma_unaligned(struct bgmac *bgmac,
  407. struct bgmac_dma_ring *ring,
  408. enum bgmac_dma_ring_type ring_type)
  409. {
  410. switch (ring_type) {
  411. case BGMAC_DMA_RING_TX:
  412. bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO,
  413. 0xff0);
  414. if (bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO))
  415. return true;
  416. break;
  417. case BGMAC_DMA_RING_RX:
  418. bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO,
  419. 0xff0);
  420. if (bgmac_read(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO))
  421. return true;
  422. break;
  423. }
  424. return false;
  425. }
  426. static void bgmac_dma_tx_ring_free(struct bgmac *bgmac,
  427. struct bgmac_dma_ring *ring)
  428. {
  429. struct device *dma_dev = bgmac->core->dma_dev;
  430. struct bgmac_dma_desc *dma_desc = ring->cpu_base;
  431. struct bgmac_slot_info *slot;
  432. int i;
  433. for (i = 0; i < BGMAC_TX_RING_SLOTS; i++) {
  434. int len = dma_desc[i].ctl1 & BGMAC_DESC_CTL1_LEN;
  435. slot = &ring->slots[i];
  436. dev_kfree_skb(slot->skb);
  437. if (!slot->dma_addr)
  438. continue;
  439. if (slot->skb)
  440. dma_unmap_single(dma_dev, slot->dma_addr,
  441. len, DMA_TO_DEVICE);
  442. else
  443. dma_unmap_page(dma_dev, slot->dma_addr,
  444. len, DMA_TO_DEVICE);
  445. }
  446. }
  447. static void bgmac_dma_rx_ring_free(struct bgmac *bgmac,
  448. struct bgmac_dma_ring *ring)
  449. {
  450. struct device *dma_dev = bgmac->core->dma_dev;
  451. struct bgmac_slot_info *slot;
  452. int i;
  453. for (i = 0; i < BGMAC_RX_RING_SLOTS; i++) {
  454. slot = &ring->slots[i];
  455. if (!slot->dma_addr)
  456. continue;
  457. dma_unmap_single(dma_dev, slot->dma_addr,
  458. BGMAC_RX_BUF_SIZE,
  459. DMA_FROM_DEVICE);
  460. put_page(virt_to_head_page(slot->buf));
  461. slot->dma_addr = 0;
  462. }
  463. }
  464. static void bgmac_dma_ring_desc_free(struct bgmac *bgmac,
  465. struct bgmac_dma_ring *ring,
  466. int num_slots)
  467. {
  468. struct device *dma_dev = bgmac->core->dma_dev;
  469. int size;
  470. if (!ring->cpu_base)
  471. return;
  472. /* Free ring of descriptors */
  473. size = num_slots * sizeof(struct bgmac_dma_desc);
  474. dma_free_coherent(dma_dev, size, ring->cpu_base,
  475. ring->dma_base);
  476. }
  477. static void bgmac_dma_cleanup(struct bgmac *bgmac)
  478. {
  479. int i;
  480. for (i = 0; i < BGMAC_MAX_TX_RINGS; i++)
  481. bgmac_dma_tx_ring_free(bgmac, &bgmac->tx_ring[i]);
  482. for (i = 0; i < BGMAC_MAX_RX_RINGS; i++)
  483. bgmac_dma_rx_ring_free(bgmac, &bgmac->rx_ring[i]);
  484. }
  485. static void bgmac_dma_free(struct bgmac *bgmac)
  486. {
  487. int i;
  488. for (i = 0; i < BGMAC_MAX_TX_RINGS; i++)
  489. bgmac_dma_ring_desc_free(bgmac, &bgmac->tx_ring[i],
  490. BGMAC_TX_RING_SLOTS);
  491. for (i = 0; i < BGMAC_MAX_RX_RINGS; i++)
  492. bgmac_dma_ring_desc_free(bgmac, &bgmac->rx_ring[i],
  493. BGMAC_RX_RING_SLOTS);
  494. }
  495. static int bgmac_dma_alloc(struct bgmac *bgmac)
  496. {
  497. struct device *dma_dev = bgmac->core->dma_dev;
  498. struct bgmac_dma_ring *ring;
  499. static const u16 ring_base[] = { BGMAC_DMA_BASE0, BGMAC_DMA_BASE1,
  500. BGMAC_DMA_BASE2, BGMAC_DMA_BASE3, };
  501. int size; /* ring size: different for Tx and Rx */
  502. int err;
  503. int i;
  504. BUILD_BUG_ON(BGMAC_MAX_TX_RINGS > ARRAY_SIZE(ring_base));
  505. BUILD_BUG_ON(BGMAC_MAX_RX_RINGS > ARRAY_SIZE(ring_base));
  506. if (!(bcma_aread32(bgmac->core, BCMA_IOST) & BCMA_IOST_DMA64)) {
  507. bgmac_err(bgmac, "Core does not report 64-bit DMA\n");
  508. return -ENOTSUPP;
  509. }
  510. for (i = 0; i < BGMAC_MAX_TX_RINGS; i++) {
  511. ring = &bgmac->tx_ring[i];
  512. ring->mmio_base = ring_base[i];
  513. /* Alloc ring of descriptors */
  514. size = BGMAC_TX_RING_SLOTS * sizeof(struct bgmac_dma_desc);
  515. ring->cpu_base = dma_zalloc_coherent(dma_dev, size,
  516. &ring->dma_base,
  517. GFP_KERNEL);
  518. if (!ring->cpu_base) {
  519. bgmac_err(bgmac, "Allocation of TX ring 0x%X failed\n",
  520. ring->mmio_base);
  521. goto err_dma_free;
  522. }
  523. ring->unaligned = bgmac_dma_unaligned(bgmac, ring,
  524. BGMAC_DMA_RING_TX);
  525. if (ring->unaligned)
  526. ring->index_base = lower_32_bits(ring->dma_base);
  527. else
  528. ring->index_base = 0;
  529. /* No need to alloc TX slots yet */
  530. }
  531. for (i = 0; i < BGMAC_MAX_RX_RINGS; i++) {
  532. ring = &bgmac->rx_ring[i];
  533. ring->mmio_base = ring_base[i];
  534. /* Alloc ring of descriptors */
  535. size = BGMAC_RX_RING_SLOTS * sizeof(struct bgmac_dma_desc);
  536. ring->cpu_base = dma_zalloc_coherent(dma_dev, size,
  537. &ring->dma_base,
  538. GFP_KERNEL);
  539. if (!ring->cpu_base) {
  540. bgmac_err(bgmac, "Allocation of RX ring 0x%X failed\n",
  541. ring->mmio_base);
  542. err = -ENOMEM;
  543. goto err_dma_free;
  544. }
  545. ring->unaligned = bgmac_dma_unaligned(bgmac, ring,
  546. BGMAC_DMA_RING_RX);
  547. if (ring->unaligned)
  548. ring->index_base = lower_32_bits(ring->dma_base);
  549. else
  550. ring->index_base = 0;
  551. }
  552. return 0;
  553. err_dma_free:
  554. bgmac_dma_free(bgmac);
  555. return -ENOMEM;
  556. }
  557. static int bgmac_dma_init(struct bgmac *bgmac)
  558. {
  559. struct bgmac_dma_ring *ring;
  560. int i, err;
  561. for (i = 0; i < BGMAC_MAX_TX_RINGS; i++) {
  562. ring = &bgmac->tx_ring[i];
  563. if (!ring->unaligned)
  564. bgmac_dma_tx_enable(bgmac, ring);
  565. bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGLO,
  566. lower_32_bits(ring->dma_base));
  567. bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_TX_RINGHI,
  568. upper_32_bits(ring->dma_base));
  569. if (ring->unaligned)
  570. bgmac_dma_tx_enable(bgmac, ring);
  571. ring->start = 0;
  572. ring->end = 0; /* Points the slot that should *not* be read */
  573. }
  574. for (i = 0; i < BGMAC_MAX_RX_RINGS; i++) {
  575. int j;
  576. ring = &bgmac->rx_ring[i];
  577. if (!ring->unaligned)
  578. bgmac_dma_rx_enable(bgmac, ring);
  579. bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGLO,
  580. lower_32_bits(ring->dma_base));
  581. bgmac_write(bgmac, ring->mmio_base + BGMAC_DMA_RX_RINGHI,
  582. upper_32_bits(ring->dma_base));
  583. if (ring->unaligned)
  584. bgmac_dma_rx_enable(bgmac, ring);
  585. ring->start = 0;
  586. ring->end = 0;
  587. for (j = 0; j < BGMAC_RX_RING_SLOTS; j++) {
  588. err = bgmac_dma_rx_skb_for_slot(bgmac, &ring->slots[j]);
  589. if (err)
  590. goto error;
  591. bgmac_dma_rx_setup_desc(bgmac, ring, j);
  592. }
  593. bgmac_dma_rx_update_index(bgmac, ring);
  594. }
  595. return 0;
  596. error:
  597. bgmac_dma_cleanup(bgmac);
  598. return err;
  599. }
  600. /**************************************************
  601. * PHY ops
  602. **************************************************/
  603. static u16 bgmac_phy_read(struct bgmac *bgmac, u8 phyaddr, u8 reg)
  604. {
  605. struct bcma_device *core;
  606. u16 phy_access_addr;
  607. u16 phy_ctl_addr;
  608. u32 tmp;
  609. BUILD_BUG_ON(BGMAC_PA_DATA_MASK != BCMA_GMAC_CMN_PA_DATA_MASK);
  610. BUILD_BUG_ON(BGMAC_PA_ADDR_MASK != BCMA_GMAC_CMN_PA_ADDR_MASK);
  611. BUILD_BUG_ON(BGMAC_PA_ADDR_SHIFT != BCMA_GMAC_CMN_PA_ADDR_SHIFT);
  612. BUILD_BUG_ON(BGMAC_PA_REG_MASK != BCMA_GMAC_CMN_PA_REG_MASK);
  613. BUILD_BUG_ON(BGMAC_PA_REG_SHIFT != BCMA_GMAC_CMN_PA_REG_SHIFT);
  614. BUILD_BUG_ON(BGMAC_PA_WRITE != BCMA_GMAC_CMN_PA_WRITE);
  615. BUILD_BUG_ON(BGMAC_PA_START != BCMA_GMAC_CMN_PA_START);
  616. BUILD_BUG_ON(BGMAC_PC_EPA_MASK != BCMA_GMAC_CMN_PC_EPA_MASK);
  617. BUILD_BUG_ON(BGMAC_PC_MCT_MASK != BCMA_GMAC_CMN_PC_MCT_MASK);
  618. BUILD_BUG_ON(BGMAC_PC_MCT_SHIFT != BCMA_GMAC_CMN_PC_MCT_SHIFT);
  619. BUILD_BUG_ON(BGMAC_PC_MTE != BCMA_GMAC_CMN_PC_MTE);
  620. if (bgmac->core->id.id == BCMA_CORE_4706_MAC_GBIT) {
  621. core = bgmac->core->bus->drv_gmac_cmn.core;
  622. phy_access_addr = BCMA_GMAC_CMN_PHY_ACCESS;
  623. phy_ctl_addr = BCMA_GMAC_CMN_PHY_CTL;
  624. } else {
  625. core = bgmac->core;
  626. phy_access_addr = BGMAC_PHY_ACCESS;
  627. phy_ctl_addr = BGMAC_PHY_CNTL;
  628. }
  629. tmp = bcma_read32(core, phy_ctl_addr);
  630. tmp &= ~BGMAC_PC_EPA_MASK;
  631. tmp |= phyaddr;
  632. bcma_write32(core, phy_ctl_addr, tmp);
  633. tmp = BGMAC_PA_START;
  634. tmp |= phyaddr << BGMAC_PA_ADDR_SHIFT;
  635. tmp |= reg << BGMAC_PA_REG_SHIFT;
  636. bcma_write32(core, phy_access_addr, tmp);
  637. if (!bgmac_wait_value(core, phy_access_addr, BGMAC_PA_START, 0, 1000)) {
  638. bgmac_err(bgmac, "Reading PHY %d register 0x%X failed\n",
  639. phyaddr, reg);
  640. return 0xffff;
  641. }
  642. return bcma_read32(core, phy_access_addr) & BGMAC_PA_DATA_MASK;
  643. }
  644. /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipphywr */
  645. static int bgmac_phy_write(struct bgmac *bgmac, u8 phyaddr, u8 reg, u16 value)
  646. {
  647. struct bcma_device *core;
  648. u16 phy_access_addr;
  649. u16 phy_ctl_addr;
  650. u32 tmp;
  651. if (bgmac->core->id.id == BCMA_CORE_4706_MAC_GBIT) {
  652. core = bgmac->core->bus->drv_gmac_cmn.core;
  653. phy_access_addr = BCMA_GMAC_CMN_PHY_ACCESS;
  654. phy_ctl_addr = BCMA_GMAC_CMN_PHY_CTL;
  655. } else {
  656. core = bgmac->core;
  657. phy_access_addr = BGMAC_PHY_ACCESS;
  658. phy_ctl_addr = BGMAC_PHY_CNTL;
  659. }
  660. tmp = bcma_read32(core, phy_ctl_addr);
  661. tmp &= ~BGMAC_PC_EPA_MASK;
  662. tmp |= phyaddr;
  663. bcma_write32(core, phy_ctl_addr, tmp);
  664. bgmac_write(bgmac, BGMAC_INT_STATUS, BGMAC_IS_MDIO);
  665. if (bgmac_read(bgmac, BGMAC_INT_STATUS) & BGMAC_IS_MDIO)
  666. bgmac_warn(bgmac, "Error setting MDIO int\n");
  667. tmp = BGMAC_PA_START;
  668. tmp |= BGMAC_PA_WRITE;
  669. tmp |= phyaddr << BGMAC_PA_ADDR_SHIFT;
  670. tmp |= reg << BGMAC_PA_REG_SHIFT;
  671. tmp |= value;
  672. bcma_write32(core, phy_access_addr, tmp);
  673. if (!bgmac_wait_value(core, phy_access_addr, BGMAC_PA_START, 0, 1000)) {
  674. bgmac_err(bgmac, "Writing to PHY %d register 0x%X failed\n",
  675. phyaddr, reg);
  676. return -ETIMEDOUT;
  677. }
  678. return 0;
  679. }
  680. /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipphyinit */
  681. static void bgmac_phy_init(struct bgmac *bgmac)
  682. {
  683. struct bcma_chipinfo *ci = &bgmac->core->bus->chipinfo;
  684. struct bcma_drv_cc *cc = &bgmac->core->bus->drv_cc;
  685. u8 i;
  686. if (ci->id == BCMA_CHIP_ID_BCM5356) {
  687. for (i = 0; i < 5; i++) {
  688. bgmac_phy_write(bgmac, i, 0x1f, 0x008b);
  689. bgmac_phy_write(bgmac, i, 0x15, 0x0100);
  690. bgmac_phy_write(bgmac, i, 0x1f, 0x000f);
  691. bgmac_phy_write(bgmac, i, 0x12, 0x2aaa);
  692. bgmac_phy_write(bgmac, i, 0x1f, 0x000b);
  693. }
  694. }
  695. if ((ci->id == BCMA_CHIP_ID_BCM5357 && ci->pkg != 10) ||
  696. (ci->id == BCMA_CHIP_ID_BCM4749 && ci->pkg != 10) ||
  697. (ci->id == BCMA_CHIP_ID_BCM53572 && ci->pkg != 9)) {
  698. bcma_chipco_chipctl_maskset(cc, 2, ~0xc0000000, 0);
  699. bcma_chipco_chipctl_maskset(cc, 4, ~0x80000000, 0);
  700. for (i = 0; i < 5; i++) {
  701. bgmac_phy_write(bgmac, i, 0x1f, 0x000f);
  702. bgmac_phy_write(bgmac, i, 0x16, 0x5284);
  703. bgmac_phy_write(bgmac, i, 0x1f, 0x000b);
  704. bgmac_phy_write(bgmac, i, 0x17, 0x0010);
  705. bgmac_phy_write(bgmac, i, 0x1f, 0x000f);
  706. bgmac_phy_write(bgmac, i, 0x16, 0x5296);
  707. bgmac_phy_write(bgmac, i, 0x17, 0x1073);
  708. bgmac_phy_write(bgmac, i, 0x17, 0x9073);
  709. bgmac_phy_write(bgmac, i, 0x16, 0x52b6);
  710. bgmac_phy_write(bgmac, i, 0x17, 0x9273);
  711. bgmac_phy_write(bgmac, i, 0x1f, 0x000b);
  712. }
  713. }
  714. }
  715. /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipphyreset */
  716. static void bgmac_phy_reset(struct bgmac *bgmac)
  717. {
  718. if (bgmac->phyaddr == BGMAC_PHY_NOREGS)
  719. return;
  720. bgmac_phy_write(bgmac, bgmac->phyaddr, MII_BMCR, BMCR_RESET);
  721. udelay(100);
  722. if (bgmac_phy_read(bgmac, bgmac->phyaddr, MII_BMCR) & BMCR_RESET)
  723. bgmac_err(bgmac, "PHY reset failed\n");
  724. bgmac_phy_init(bgmac);
  725. }
  726. /**************************************************
  727. * Chip ops
  728. **************************************************/
  729. /* TODO: can we just drop @force? Can we don't reset MAC at all if there is
  730. * nothing to change? Try if after stabilizng driver.
  731. */
  732. static void bgmac_cmdcfg_maskset(struct bgmac *bgmac, u32 mask, u32 set,
  733. bool force)
  734. {
  735. u32 cmdcfg = bgmac_read(bgmac, BGMAC_CMDCFG);
  736. u32 new_val = (cmdcfg & mask) | set;
  737. bgmac_set(bgmac, BGMAC_CMDCFG, BGMAC_CMDCFG_SR(bgmac->core->id.rev));
  738. udelay(2);
  739. if (new_val != cmdcfg || force)
  740. bgmac_write(bgmac, BGMAC_CMDCFG, new_val);
  741. bgmac_mask(bgmac, BGMAC_CMDCFG, ~BGMAC_CMDCFG_SR(bgmac->core->id.rev));
  742. udelay(2);
  743. }
  744. static void bgmac_write_mac_address(struct bgmac *bgmac, u8 *addr)
  745. {
  746. u32 tmp;
  747. tmp = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3];
  748. bgmac_write(bgmac, BGMAC_MACADDR_HIGH, tmp);
  749. tmp = (addr[4] << 8) | addr[5];
  750. bgmac_write(bgmac, BGMAC_MACADDR_LOW, tmp);
  751. }
  752. static void bgmac_set_rx_mode(struct net_device *net_dev)
  753. {
  754. struct bgmac *bgmac = netdev_priv(net_dev);
  755. if (net_dev->flags & IFF_PROMISC)
  756. bgmac_cmdcfg_maskset(bgmac, ~0, BGMAC_CMDCFG_PROM, true);
  757. else
  758. bgmac_cmdcfg_maskset(bgmac, ~BGMAC_CMDCFG_PROM, 0, true);
  759. }
  760. #if 0 /* We don't use that regs yet */
  761. static void bgmac_chip_stats_update(struct bgmac *bgmac)
  762. {
  763. int i;
  764. if (bgmac->core->id.id != BCMA_CORE_4706_MAC_GBIT) {
  765. for (i = 0; i < BGMAC_NUM_MIB_TX_REGS; i++)
  766. bgmac->mib_tx_regs[i] =
  767. bgmac_read(bgmac,
  768. BGMAC_TX_GOOD_OCTETS + (i * 4));
  769. for (i = 0; i < BGMAC_NUM_MIB_RX_REGS; i++)
  770. bgmac->mib_rx_regs[i] =
  771. bgmac_read(bgmac,
  772. BGMAC_RX_GOOD_OCTETS + (i * 4));
  773. }
  774. /* TODO: what else? how to handle BCM4706? Specs are needed */
  775. }
  776. #endif
  777. static void bgmac_clear_mib(struct bgmac *bgmac)
  778. {
  779. int i;
  780. if (bgmac->core->id.id == BCMA_CORE_4706_MAC_GBIT)
  781. return;
  782. bgmac_set(bgmac, BGMAC_DEV_CTL, BGMAC_DC_MROR);
  783. for (i = 0; i < BGMAC_NUM_MIB_TX_REGS; i++)
  784. bgmac_read(bgmac, BGMAC_TX_GOOD_OCTETS + (i * 4));
  785. for (i = 0; i < BGMAC_NUM_MIB_RX_REGS; i++)
  786. bgmac_read(bgmac, BGMAC_RX_GOOD_OCTETS + (i * 4));
  787. }
  788. /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_speed */
  789. static void bgmac_mac_speed(struct bgmac *bgmac)
  790. {
  791. u32 mask = ~(BGMAC_CMDCFG_ES_MASK | BGMAC_CMDCFG_HD);
  792. u32 set = 0;
  793. switch (bgmac->mac_speed) {
  794. case SPEED_10:
  795. set |= BGMAC_CMDCFG_ES_10;
  796. break;
  797. case SPEED_100:
  798. set |= BGMAC_CMDCFG_ES_100;
  799. break;
  800. case SPEED_1000:
  801. set |= BGMAC_CMDCFG_ES_1000;
  802. break;
  803. case SPEED_2500:
  804. set |= BGMAC_CMDCFG_ES_2500;
  805. break;
  806. default:
  807. bgmac_err(bgmac, "Unsupported speed: %d\n", bgmac->mac_speed);
  808. }
  809. if (bgmac->mac_duplex == DUPLEX_HALF)
  810. set |= BGMAC_CMDCFG_HD;
  811. bgmac_cmdcfg_maskset(bgmac, mask, set, true);
  812. }
  813. static void bgmac_miiconfig(struct bgmac *bgmac)
  814. {
  815. struct bcma_device *core = bgmac->core;
  816. struct bcma_chipinfo *ci = &core->bus->chipinfo;
  817. u8 imode;
  818. if (ci->id == BCMA_CHIP_ID_BCM4707 ||
  819. ci->id == BCMA_CHIP_ID_BCM53018) {
  820. bcma_awrite32(core, BCMA_IOCTL,
  821. bcma_aread32(core, BCMA_IOCTL) | 0x40 |
  822. BGMAC_BCMA_IOCTL_SW_CLKEN);
  823. bgmac->mac_speed = SPEED_2500;
  824. bgmac->mac_duplex = DUPLEX_FULL;
  825. bgmac_mac_speed(bgmac);
  826. } else {
  827. imode = (bgmac_read(bgmac, BGMAC_DEV_STATUS) &
  828. BGMAC_DS_MM_MASK) >> BGMAC_DS_MM_SHIFT;
  829. if (imode == 0 || imode == 1) {
  830. bgmac->mac_speed = SPEED_100;
  831. bgmac->mac_duplex = DUPLEX_FULL;
  832. bgmac_mac_speed(bgmac);
  833. }
  834. }
  835. }
  836. /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipreset */
  837. static void bgmac_chip_reset(struct bgmac *bgmac)
  838. {
  839. struct bcma_device *core = bgmac->core;
  840. struct bcma_bus *bus = core->bus;
  841. struct bcma_chipinfo *ci = &bus->chipinfo;
  842. u32 flags;
  843. u32 iost;
  844. int i;
  845. if (bcma_core_is_enabled(core)) {
  846. if (!bgmac->stats_grabbed) {
  847. /* bgmac_chip_stats_update(bgmac); */
  848. bgmac->stats_grabbed = true;
  849. }
  850. for (i = 0; i < BGMAC_MAX_TX_RINGS; i++)
  851. bgmac_dma_tx_reset(bgmac, &bgmac->tx_ring[i]);
  852. bgmac_cmdcfg_maskset(bgmac, ~0, BGMAC_CMDCFG_ML, false);
  853. udelay(1);
  854. for (i = 0; i < BGMAC_MAX_RX_RINGS; i++)
  855. bgmac_dma_rx_reset(bgmac, &bgmac->rx_ring[i]);
  856. /* TODO: Clear software multicast filter list */
  857. }
  858. iost = bcma_aread32(core, BCMA_IOST);
  859. if ((ci->id == BCMA_CHIP_ID_BCM5357 && ci->pkg == BCMA_PKG_ID_BCM47186) ||
  860. (ci->id == BCMA_CHIP_ID_BCM4749 && ci->pkg == 10) ||
  861. (ci->id == BCMA_CHIP_ID_BCM53572 && ci->pkg == BCMA_PKG_ID_BCM47188))
  862. iost &= ~BGMAC_BCMA_IOST_ATTACHED;
  863. /* 3GMAC: for BCM4707, only do core reset at bgmac_probe() */
  864. if (ci->id != BCMA_CHIP_ID_BCM4707) {
  865. flags = 0;
  866. if (iost & BGMAC_BCMA_IOST_ATTACHED) {
  867. flags = BGMAC_BCMA_IOCTL_SW_CLKEN;
  868. if (!bgmac->has_robosw)
  869. flags |= BGMAC_BCMA_IOCTL_SW_RESET;
  870. }
  871. bcma_core_enable(core, flags);
  872. }
  873. /* Request Misc PLL for corerev > 2 */
  874. if (core->id.rev > 2 &&
  875. ci->id != BCMA_CHIP_ID_BCM4707 &&
  876. ci->id != BCMA_CHIP_ID_BCM53018) {
  877. bgmac_set(bgmac, BCMA_CLKCTLST,
  878. BGMAC_BCMA_CLKCTLST_MISC_PLL_REQ);
  879. bgmac_wait_value(bgmac->core, BCMA_CLKCTLST,
  880. BGMAC_BCMA_CLKCTLST_MISC_PLL_ST,
  881. BGMAC_BCMA_CLKCTLST_MISC_PLL_ST,
  882. 1000);
  883. }
  884. if (ci->id == BCMA_CHIP_ID_BCM5357 ||
  885. ci->id == BCMA_CHIP_ID_BCM4749 ||
  886. ci->id == BCMA_CHIP_ID_BCM53572) {
  887. struct bcma_drv_cc *cc = &bgmac->core->bus->drv_cc;
  888. u8 et_swtype = 0;
  889. u8 sw_type = BGMAC_CHIPCTL_1_SW_TYPE_EPHY |
  890. BGMAC_CHIPCTL_1_IF_TYPE_MII;
  891. char buf[4];
  892. if (bcm47xx_nvram_getenv("et_swtype", buf, sizeof(buf)) > 0) {
  893. if (kstrtou8(buf, 0, &et_swtype))
  894. bgmac_err(bgmac, "Failed to parse et_swtype (%s)\n",
  895. buf);
  896. et_swtype &= 0x0f;
  897. et_swtype <<= 4;
  898. sw_type = et_swtype;
  899. } else if (ci->id == BCMA_CHIP_ID_BCM5357 && ci->pkg == BCMA_PKG_ID_BCM5358) {
  900. sw_type = BGMAC_CHIPCTL_1_SW_TYPE_EPHYRMII;
  901. } else if ((ci->id == BCMA_CHIP_ID_BCM5357 && ci->pkg == BCMA_PKG_ID_BCM47186) ||
  902. (ci->id == BCMA_CHIP_ID_BCM4749 && ci->pkg == 10) ||
  903. (ci->id == BCMA_CHIP_ID_BCM53572 && ci->pkg == BCMA_PKG_ID_BCM47188)) {
  904. sw_type = BGMAC_CHIPCTL_1_IF_TYPE_RGMII |
  905. BGMAC_CHIPCTL_1_SW_TYPE_RGMII;
  906. }
  907. bcma_chipco_chipctl_maskset(cc, 1,
  908. ~(BGMAC_CHIPCTL_1_IF_TYPE_MASK |
  909. BGMAC_CHIPCTL_1_SW_TYPE_MASK),
  910. sw_type);
  911. }
  912. if (iost & BGMAC_BCMA_IOST_ATTACHED && !bgmac->has_robosw)
  913. bcma_awrite32(core, BCMA_IOCTL,
  914. bcma_aread32(core, BCMA_IOCTL) &
  915. ~BGMAC_BCMA_IOCTL_SW_RESET);
  916. /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_reset
  917. * Specs don't say about using BGMAC_CMDCFG_SR, but in this routine
  918. * BGMAC_CMDCFG is read _after_ putting chip in a reset. So it has to
  919. * be keps until taking MAC out of the reset.
  920. */
  921. bgmac_cmdcfg_maskset(bgmac,
  922. ~(BGMAC_CMDCFG_TE |
  923. BGMAC_CMDCFG_RE |
  924. BGMAC_CMDCFG_RPI |
  925. BGMAC_CMDCFG_TAI |
  926. BGMAC_CMDCFG_HD |
  927. BGMAC_CMDCFG_ML |
  928. BGMAC_CMDCFG_CFE |
  929. BGMAC_CMDCFG_RL |
  930. BGMAC_CMDCFG_RED |
  931. BGMAC_CMDCFG_PE |
  932. BGMAC_CMDCFG_TPI |
  933. BGMAC_CMDCFG_PAD_EN |
  934. BGMAC_CMDCFG_PF),
  935. BGMAC_CMDCFG_PROM |
  936. BGMAC_CMDCFG_NLC |
  937. BGMAC_CMDCFG_CFE |
  938. BGMAC_CMDCFG_SR(core->id.rev),
  939. false);
  940. bgmac->mac_speed = SPEED_UNKNOWN;
  941. bgmac->mac_duplex = DUPLEX_UNKNOWN;
  942. bgmac_clear_mib(bgmac);
  943. if (core->id.id == BCMA_CORE_4706_MAC_GBIT)
  944. bcma_maskset32(bgmac->cmn, BCMA_GMAC_CMN_PHY_CTL, ~0,
  945. BCMA_GMAC_CMN_PC_MTE);
  946. else
  947. bgmac_set(bgmac, BGMAC_PHY_CNTL, BGMAC_PC_MTE);
  948. bgmac_miiconfig(bgmac);
  949. bgmac_phy_init(bgmac);
  950. netdev_reset_queue(bgmac->net_dev);
  951. }
  952. static void bgmac_chip_intrs_on(struct bgmac *bgmac)
  953. {
  954. bgmac_write(bgmac, BGMAC_INT_MASK, bgmac->int_mask);
  955. }
  956. static void bgmac_chip_intrs_off(struct bgmac *bgmac)
  957. {
  958. bgmac_write(bgmac, BGMAC_INT_MASK, 0);
  959. bgmac_read(bgmac, BGMAC_INT_MASK);
  960. }
  961. /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/gmac_enable */
  962. static void bgmac_enable(struct bgmac *bgmac)
  963. {
  964. struct bcma_chipinfo *ci = &bgmac->core->bus->chipinfo;
  965. u32 cmdcfg;
  966. u32 mode;
  967. u32 rxq_ctl;
  968. u32 fl_ctl;
  969. u16 bp_clk;
  970. u8 mdp;
  971. cmdcfg = bgmac_read(bgmac, BGMAC_CMDCFG);
  972. bgmac_cmdcfg_maskset(bgmac, ~(BGMAC_CMDCFG_TE | BGMAC_CMDCFG_RE),
  973. BGMAC_CMDCFG_SR(bgmac->core->id.rev), true);
  974. udelay(2);
  975. cmdcfg |= BGMAC_CMDCFG_TE | BGMAC_CMDCFG_RE;
  976. bgmac_write(bgmac, BGMAC_CMDCFG, cmdcfg);
  977. mode = (bgmac_read(bgmac, BGMAC_DEV_STATUS) & BGMAC_DS_MM_MASK) >>
  978. BGMAC_DS_MM_SHIFT;
  979. if (ci->id != BCMA_CHIP_ID_BCM47162 || mode != 0)
  980. bgmac_set(bgmac, BCMA_CLKCTLST, BCMA_CLKCTLST_FORCEHT);
  981. if (ci->id == BCMA_CHIP_ID_BCM47162 && mode == 2)
  982. bcma_chipco_chipctl_maskset(&bgmac->core->bus->drv_cc, 1, ~0,
  983. BGMAC_CHIPCTL_1_RXC_DLL_BYPASS);
  984. switch (ci->id) {
  985. case BCMA_CHIP_ID_BCM5357:
  986. case BCMA_CHIP_ID_BCM4749:
  987. case BCMA_CHIP_ID_BCM53572:
  988. case BCMA_CHIP_ID_BCM4716:
  989. case BCMA_CHIP_ID_BCM47162:
  990. fl_ctl = 0x03cb04cb;
  991. if (ci->id == BCMA_CHIP_ID_BCM5357 ||
  992. ci->id == BCMA_CHIP_ID_BCM4749 ||
  993. ci->id == BCMA_CHIP_ID_BCM53572)
  994. fl_ctl = 0x2300e1;
  995. bgmac_write(bgmac, BGMAC_FLOW_CTL_THRESH, fl_ctl);
  996. bgmac_write(bgmac, BGMAC_PAUSE_CTL, 0x27fff);
  997. break;
  998. }
  999. if (ci->id != BCMA_CHIP_ID_BCM4707 &&
  1000. ci->id != BCMA_CHIP_ID_BCM53018) {
  1001. rxq_ctl = bgmac_read(bgmac, BGMAC_RXQ_CTL);
  1002. rxq_ctl &= ~BGMAC_RXQ_CTL_MDP_MASK;
  1003. bp_clk = bcma_pmu_get_bus_clock(&bgmac->core->bus->drv_cc) /
  1004. 1000000;
  1005. mdp = (bp_clk * 128 / 1000) - 3;
  1006. rxq_ctl |= (mdp << BGMAC_RXQ_CTL_MDP_SHIFT);
  1007. bgmac_write(bgmac, BGMAC_RXQ_CTL, rxq_ctl);
  1008. }
  1009. }
  1010. /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipinit */
  1011. static void bgmac_chip_init(struct bgmac *bgmac)
  1012. {
  1013. /* 1 interrupt per received frame */
  1014. bgmac_write(bgmac, BGMAC_INT_RECV_LAZY, 1 << BGMAC_IRL_FC_SHIFT);
  1015. /* Enable 802.3x tx flow control (honor received PAUSE frames) */
  1016. bgmac_cmdcfg_maskset(bgmac, ~BGMAC_CMDCFG_RPI, 0, true);
  1017. bgmac_set_rx_mode(bgmac->net_dev);
  1018. bgmac_write_mac_address(bgmac, bgmac->net_dev->dev_addr);
  1019. if (bgmac->loopback)
  1020. bgmac_cmdcfg_maskset(bgmac, ~0, BGMAC_CMDCFG_ML, false);
  1021. else
  1022. bgmac_cmdcfg_maskset(bgmac, ~BGMAC_CMDCFG_ML, 0, false);
  1023. bgmac_write(bgmac, BGMAC_RXMAX_LENGTH, 32 + ETHER_MAX_LEN);
  1024. bgmac_chip_intrs_on(bgmac);
  1025. bgmac_enable(bgmac);
  1026. }
  1027. static irqreturn_t bgmac_interrupt(int irq, void *dev_id)
  1028. {
  1029. struct bgmac *bgmac = netdev_priv(dev_id);
  1030. u32 int_status = bgmac_read(bgmac, BGMAC_INT_STATUS);
  1031. int_status &= bgmac->int_mask;
  1032. if (!int_status)
  1033. return IRQ_NONE;
  1034. int_status &= ~(BGMAC_IS_TX0 | BGMAC_IS_RX);
  1035. if (int_status)
  1036. bgmac_err(bgmac, "Unknown IRQs: 0x%08X\n", int_status);
  1037. /* Disable new interrupts until handling existing ones */
  1038. bgmac_chip_intrs_off(bgmac);
  1039. napi_schedule(&bgmac->napi);
  1040. return IRQ_HANDLED;
  1041. }
  1042. static int bgmac_poll(struct napi_struct *napi, int weight)
  1043. {
  1044. struct bgmac *bgmac = container_of(napi, struct bgmac, napi);
  1045. int handled = 0;
  1046. /* Ack */
  1047. bgmac_write(bgmac, BGMAC_INT_STATUS, ~0);
  1048. bgmac_dma_tx_free(bgmac, &bgmac->tx_ring[0]);
  1049. handled += bgmac_dma_rx_read(bgmac, &bgmac->rx_ring[0], weight);
  1050. /* Poll again if more events arrived in the meantime */
  1051. if (bgmac_read(bgmac, BGMAC_INT_STATUS) & (BGMAC_IS_TX0 | BGMAC_IS_RX))
  1052. return weight;
  1053. if (handled < weight) {
  1054. napi_complete(napi);
  1055. bgmac_chip_intrs_on(bgmac);
  1056. }
  1057. return handled;
  1058. }
  1059. /**************************************************
  1060. * net_device_ops
  1061. **************************************************/
  1062. static int bgmac_open(struct net_device *net_dev)
  1063. {
  1064. struct bgmac *bgmac = netdev_priv(net_dev);
  1065. int err = 0;
  1066. bgmac_chip_reset(bgmac);
  1067. err = bgmac_dma_init(bgmac);
  1068. if (err)
  1069. return err;
  1070. /* Specs say about reclaiming rings here, but we do that in DMA init */
  1071. bgmac_chip_init(bgmac);
  1072. err = request_irq(bgmac->core->irq, bgmac_interrupt, IRQF_SHARED,
  1073. KBUILD_MODNAME, net_dev);
  1074. if (err < 0) {
  1075. bgmac_err(bgmac, "IRQ request error: %d!\n", err);
  1076. bgmac_dma_cleanup(bgmac);
  1077. return err;
  1078. }
  1079. napi_enable(&bgmac->napi);
  1080. phy_start(bgmac->phy_dev);
  1081. netif_carrier_on(net_dev);
  1082. return 0;
  1083. }
  1084. static int bgmac_stop(struct net_device *net_dev)
  1085. {
  1086. struct bgmac *bgmac = netdev_priv(net_dev);
  1087. netif_carrier_off(net_dev);
  1088. phy_stop(bgmac->phy_dev);
  1089. napi_disable(&bgmac->napi);
  1090. bgmac_chip_intrs_off(bgmac);
  1091. free_irq(bgmac->core->irq, net_dev);
  1092. bgmac_chip_reset(bgmac);
  1093. bgmac_dma_cleanup(bgmac);
  1094. return 0;
  1095. }
  1096. static netdev_tx_t bgmac_start_xmit(struct sk_buff *skb,
  1097. struct net_device *net_dev)
  1098. {
  1099. struct bgmac *bgmac = netdev_priv(net_dev);
  1100. struct bgmac_dma_ring *ring;
  1101. /* No QOS support yet */
  1102. ring = &bgmac->tx_ring[0];
  1103. return bgmac_dma_tx_add(bgmac, ring, skb);
  1104. }
  1105. static int bgmac_set_mac_address(struct net_device *net_dev, void *addr)
  1106. {
  1107. struct bgmac *bgmac = netdev_priv(net_dev);
  1108. int ret;
  1109. ret = eth_prepare_mac_addr_change(net_dev, addr);
  1110. if (ret < 0)
  1111. return ret;
  1112. bgmac_write_mac_address(bgmac, (u8 *)addr);
  1113. eth_commit_mac_addr_change(net_dev, addr);
  1114. return 0;
  1115. }
  1116. static int bgmac_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
  1117. {
  1118. struct bgmac *bgmac = netdev_priv(net_dev);
  1119. if (!netif_running(net_dev))
  1120. return -EINVAL;
  1121. return phy_mii_ioctl(bgmac->phy_dev, ifr, cmd);
  1122. }
  1123. static const struct net_device_ops bgmac_netdev_ops = {
  1124. .ndo_open = bgmac_open,
  1125. .ndo_stop = bgmac_stop,
  1126. .ndo_start_xmit = bgmac_start_xmit,
  1127. .ndo_set_rx_mode = bgmac_set_rx_mode,
  1128. .ndo_set_mac_address = bgmac_set_mac_address,
  1129. .ndo_validate_addr = eth_validate_addr,
  1130. .ndo_do_ioctl = bgmac_ioctl,
  1131. };
  1132. /**************************************************
  1133. * ethtool_ops
  1134. **************************************************/
  1135. static int bgmac_get_settings(struct net_device *net_dev,
  1136. struct ethtool_cmd *cmd)
  1137. {
  1138. struct bgmac *bgmac = netdev_priv(net_dev);
  1139. return phy_ethtool_gset(bgmac->phy_dev, cmd);
  1140. }
  1141. static int bgmac_set_settings(struct net_device *net_dev,
  1142. struct ethtool_cmd *cmd)
  1143. {
  1144. struct bgmac *bgmac = netdev_priv(net_dev);
  1145. return phy_ethtool_sset(bgmac->phy_dev, cmd);
  1146. }
  1147. static void bgmac_get_drvinfo(struct net_device *net_dev,
  1148. struct ethtool_drvinfo *info)
  1149. {
  1150. strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
  1151. strlcpy(info->bus_info, "BCMA", sizeof(info->bus_info));
  1152. }
  1153. static const struct ethtool_ops bgmac_ethtool_ops = {
  1154. .get_settings = bgmac_get_settings,
  1155. .set_settings = bgmac_set_settings,
  1156. .get_drvinfo = bgmac_get_drvinfo,
  1157. };
  1158. /**************************************************
  1159. * MII
  1160. **************************************************/
  1161. static int bgmac_mii_read(struct mii_bus *bus, int mii_id, int regnum)
  1162. {
  1163. return bgmac_phy_read(bus->priv, mii_id, regnum);
  1164. }
  1165. static int bgmac_mii_write(struct mii_bus *bus, int mii_id, int regnum,
  1166. u16 value)
  1167. {
  1168. return bgmac_phy_write(bus->priv, mii_id, regnum, value);
  1169. }
  1170. static void bgmac_adjust_link(struct net_device *net_dev)
  1171. {
  1172. struct bgmac *bgmac = netdev_priv(net_dev);
  1173. struct phy_device *phy_dev = bgmac->phy_dev;
  1174. bool update = false;
  1175. if (phy_dev->link) {
  1176. if (phy_dev->speed != bgmac->mac_speed) {
  1177. bgmac->mac_speed = phy_dev->speed;
  1178. update = true;
  1179. }
  1180. if (phy_dev->duplex != bgmac->mac_duplex) {
  1181. bgmac->mac_duplex = phy_dev->duplex;
  1182. update = true;
  1183. }
  1184. }
  1185. if (update) {
  1186. bgmac_mac_speed(bgmac);
  1187. phy_print_status(phy_dev);
  1188. }
  1189. }
  1190. static int bgmac_fixed_phy_register(struct bgmac *bgmac)
  1191. {
  1192. struct fixed_phy_status fphy_status = {
  1193. .link = 1,
  1194. .speed = SPEED_1000,
  1195. .duplex = DUPLEX_FULL,
  1196. };
  1197. struct phy_device *phy_dev;
  1198. int err;
  1199. phy_dev = fixed_phy_register(PHY_POLL, &fphy_status, NULL);
  1200. if (!phy_dev || IS_ERR(phy_dev)) {
  1201. bgmac_err(bgmac, "Failed to register fixed PHY device\n");
  1202. return -ENODEV;
  1203. }
  1204. err = phy_connect_direct(bgmac->net_dev, phy_dev, bgmac_adjust_link,
  1205. PHY_INTERFACE_MODE_MII);
  1206. if (err) {
  1207. bgmac_err(bgmac, "Connecting PHY failed\n");
  1208. return err;
  1209. }
  1210. bgmac->phy_dev = phy_dev;
  1211. return err;
  1212. }
  1213. static int bgmac_mii_register(struct bgmac *bgmac)
  1214. {
  1215. struct bcma_chipinfo *ci = &bgmac->core->bus->chipinfo;
  1216. struct mii_bus *mii_bus;
  1217. struct phy_device *phy_dev;
  1218. char bus_id[MII_BUS_ID_SIZE + 3];
  1219. int i, err = 0;
  1220. if (ci->id == BCMA_CHIP_ID_BCM4707 ||
  1221. ci->id == BCMA_CHIP_ID_BCM53018)
  1222. return bgmac_fixed_phy_register(bgmac);
  1223. mii_bus = mdiobus_alloc();
  1224. if (!mii_bus)
  1225. return -ENOMEM;
  1226. mii_bus->name = "bgmac mii bus";
  1227. sprintf(mii_bus->id, "%s-%d-%d", "bgmac", bgmac->core->bus->num,
  1228. bgmac->core->core_unit);
  1229. mii_bus->priv = bgmac;
  1230. mii_bus->read = bgmac_mii_read;
  1231. mii_bus->write = bgmac_mii_write;
  1232. mii_bus->parent = &bgmac->core->dev;
  1233. mii_bus->phy_mask = ~(1 << bgmac->phyaddr);
  1234. mii_bus->irq = kmalloc_array(PHY_MAX_ADDR, sizeof(int), GFP_KERNEL);
  1235. if (!mii_bus->irq) {
  1236. err = -ENOMEM;
  1237. goto err_free_bus;
  1238. }
  1239. for (i = 0; i < PHY_MAX_ADDR; i++)
  1240. mii_bus->irq[i] = PHY_POLL;
  1241. err = mdiobus_register(mii_bus);
  1242. if (err) {
  1243. bgmac_err(bgmac, "Registration of mii bus failed\n");
  1244. goto err_free_irq;
  1245. }
  1246. bgmac->mii_bus = mii_bus;
  1247. /* Connect to the PHY */
  1248. snprintf(bus_id, sizeof(bus_id), PHY_ID_FMT, mii_bus->id,
  1249. bgmac->phyaddr);
  1250. phy_dev = phy_connect(bgmac->net_dev, bus_id, &bgmac_adjust_link,
  1251. PHY_INTERFACE_MODE_MII);
  1252. if (IS_ERR(phy_dev)) {
  1253. bgmac_err(bgmac, "PHY connecton failed\n");
  1254. err = PTR_ERR(phy_dev);
  1255. goto err_unregister_bus;
  1256. }
  1257. bgmac->phy_dev = phy_dev;
  1258. return err;
  1259. err_unregister_bus:
  1260. mdiobus_unregister(mii_bus);
  1261. err_free_irq:
  1262. kfree(mii_bus->irq);
  1263. err_free_bus:
  1264. mdiobus_free(mii_bus);
  1265. return err;
  1266. }
  1267. static void bgmac_mii_unregister(struct bgmac *bgmac)
  1268. {
  1269. struct mii_bus *mii_bus = bgmac->mii_bus;
  1270. mdiobus_unregister(mii_bus);
  1271. kfree(mii_bus->irq);
  1272. mdiobus_free(mii_bus);
  1273. }
  1274. /**************************************************
  1275. * BCMA bus ops
  1276. **************************************************/
  1277. /* http://bcm-v4.sipsolutions.net/mac-gbit/gmac/chipattach */
  1278. static int bgmac_probe(struct bcma_device *core)
  1279. {
  1280. struct bcma_chipinfo *ci = &core->bus->chipinfo;
  1281. struct net_device *net_dev;
  1282. struct bgmac *bgmac;
  1283. struct ssb_sprom *sprom = &core->bus->sprom;
  1284. u8 *mac = core->core_unit ? sprom->et1mac : sprom->et0mac;
  1285. int err;
  1286. /* We don't support 2nd, 3rd, ... units, SPROM has to be adjusted */
  1287. if (core->core_unit > 1) {
  1288. pr_err("Unsupported core_unit %d\n", core->core_unit);
  1289. return -ENOTSUPP;
  1290. }
  1291. if (!is_valid_ether_addr(mac)) {
  1292. dev_err(&core->dev, "Invalid MAC addr: %pM\n", mac);
  1293. eth_random_addr(mac);
  1294. dev_warn(&core->dev, "Using random MAC: %pM\n", mac);
  1295. }
  1296. /* Allocation and references */
  1297. net_dev = alloc_etherdev(sizeof(*bgmac));
  1298. if (!net_dev)
  1299. return -ENOMEM;
  1300. net_dev->netdev_ops = &bgmac_netdev_ops;
  1301. net_dev->irq = core->irq;
  1302. net_dev->ethtool_ops = &bgmac_ethtool_ops;
  1303. bgmac = netdev_priv(net_dev);
  1304. bgmac->net_dev = net_dev;
  1305. bgmac->core = core;
  1306. bcma_set_drvdata(core, bgmac);
  1307. /* Defaults */
  1308. memcpy(bgmac->net_dev->dev_addr, mac, ETH_ALEN);
  1309. /* On BCM4706 we need common core to access PHY */
  1310. if (core->id.id == BCMA_CORE_4706_MAC_GBIT &&
  1311. !core->bus->drv_gmac_cmn.core) {
  1312. bgmac_err(bgmac, "GMAC CMN core not found (required for BCM4706)\n");
  1313. err = -ENODEV;
  1314. goto err_netdev_free;
  1315. }
  1316. bgmac->cmn = core->bus->drv_gmac_cmn.core;
  1317. bgmac->phyaddr = core->core_unit ? sprom->et1phyaddr :
  1318. sprom->et0phyaddr;
  1319. bgmac->phyaddr &= BGMAC_PHY_MASK;
  1320. if (bgmac->phyaddr == BGMAC_PHY_MASK) {
  1321. bgmac_err(bgmac, "No PHY found\n");
  1322. err = -ENODEV;
  1323. goto err_netdev_free;
  1324. }
  1325. bgmac_info(bgmac, "Found PHY addr: %d%s\n", bgmac->phyaddr,
  1326. bgmac->phyaddr == BGMAC_PHY_NOREGS ? " (NOREGS)" : "");
  1327. if (core->bus->hosttype == BCMA_HOSTTYPE_PCI) {
  1328. bgmac_err(bgmac, "PCI setup not implemented\n");
  1329. err = -ENOTSUPP;
  1330. goto err_netdev_free;
  1331. }
  1332. bgmac_chip_reset(bgmac);
  1333. /* For Northstar, we have to take all GMAC core out of reset */
  1334. if (ci->id == BCMA_CHIP_ID_BCM4707 ||
  1335. ci->id == BCMA_CHIP_ID_BCM53018) {
  1336. struct bcma_device *ns_core;
  1337. int ns_gmac;
  1338. /* Northstar has 4 GMAC cores */
  1339. for (ns_gmac = 0; ns_gmac < 4; ns_gmac++) {
  1340. /* As Northstar requirement, we have to reset all GMACs
  1341. * before accessing one. bgmac_chip_reset() call
  1342. * bcma_core_enable() for this core. Then the other
  1343. * three GMACs didn't reset. We do it here.
  1344. */
  1345. ns_core = bcma_find_core_unit(core->bus,
  1346. BCMA_CORE_MAC_GBIT,
  1347. ns_gmac);
  1348. if (ns_core && !bcma_core_is_enabled(ns_core))
  1349. bcma_core_enable(ns_core, 0);
  1350. }
  1351. }
  1352. err = bgmac_dma_alloc(bgmac);
  1353. if (err) {
  1354. bgmac_err(bgmac, "Unable to alloc memory for DMA\n");
  1355. goto err_netdev_free;
  1356. }
  1357. bgmac->int_mask = BGMAC_IS_ERRMASK | BGMAC_IS_RX | BGMAC_IS_TX_MASK;
  1358. if (bcm47xx_nvram_getenv("et0_no_txint", NULL, 0) == 0)
  1359. bgmac->int_mask &= ~BGMAC_IS_TX_MASK;
  1360. /* TODO: reset the external phy. Specs are needed */
  1361. bgmac_phy_reset(bgmac);
  1362. bgmac->has_robosw = !!(core->bus->sprom.boardflags_lo &
  1363. BGMAC_BFL_ENETROBO);
  1364. if (bgmac->has_robosw)
  1365. bgmac_warn(bgmac, "Support for Roboswitch not implemented\n");
  1366. if (core->bus->sprom.boardflags_lo & BGMAC_BFL_ENETADM)
  1367. bgmac_warn(bgmac, "Support for ADMtek ethernet switch not implemented\n");
  1368. netif_napi_add(net_dev, &bgmac->napi, bgmac_poll, BGMAC_WEIGHT);
  1369. err = bgmac_mii_register(bgmac);
  1370. if (err) {
  1371. bgmac_err(bgmac, "Cannot register MDIO\n");
  1372. goto err_dma_free;
  1373. }
  1374. net_dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
  1375. net_dev->hw_features = net_dev->features;
  1376. net_dev->vlan_features = net_dev->features;
  1377. err = register_netdev(bgmac->net_dev);
  1378. if (err) {
  1379. bgmac_err(bgmac, "Cannot register net device\n");
  1380. goto err_mii_unregister;
  1381. }
  1382. netif_carrier_off(net_dev);
  1383. return 0;
  1384. err_mii_unregister:
  1385. bgmac_mii_unregister(bgmac);
  1386. err_dma_free:
  1387. bgmac_dma_free(bgmac);
  1388. err_netdev_free:
  1389. bcma_set_drvdata(core, NULL);
  1390. free_netdev(net_dev);
  1391. return err;
  1392. }
  1393. static void bgmac_remove(struct bcma_device *core)
  1394. {
  1395. struct bgmac *bgmac = bcma_get_drvdata(core);
  1396. unregister_netdev(bgmac->net_dev);
  1397. bgmac_mii_unregister(bgmac);
  1398. netif_napi_del(&bgmac->napi);
  1399. bgmac_dma_free(bgmac);
  1400. bcma_set_drvdata(core, NULL);
  1401. free_netdev(bgmac->net_dev);
  1402. }
  1403. static struct bcma_driver bgmac_bcma_driver = {
  1404. .name = KBUILD_MODNAME,
  1405. .id_table = bgmac_bcma_tbl,
  1406. .probe = bgmac_probe,
  1407. .remove = bgmac_remove,
  1408. };
  1409. static int __init bgmac_init(void)
  1410. {
  1411. int err;
  1412. err = bcma_driver_register(&bgmac_bcma_driver);
  1413. if (err)
  1414. return err;
  1415. pr_info("Broadcom 47xx GBit MAC driver loaded\n");
  1416. return 0;
  1417. }
  1418. static void __exit bgmac_exit(void)
  1419. {
  1420. bcma_driver_unregister(&bgmac_bcma_driver);
  1421. }
  1422. module_init(bgmac_init)
  1423. module_exit(bgmac_exit)
  1424. MODULE_AUTHOR("Rafał Miłecki");
  1425. MODULE_LICENSE("GPL");