sb1250-mac.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954
  1. /*
  2. * Copyright (C) 2001,2002,2003,2004 Broadcom Corporation
  3. * Copyright (c) 2006, 2007 Maciej W. Rozycki
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. *
  19. *
  20. * This driver is designed for the Broadcom SiByte SOC built-in
  21. * Ethernet controllers. Written by Mitch Lichtenberg at Broadcom Corp.
  22. *
  23. * Updated to the driver model and the PHY abstraction layer
  24. * by Maciej W. Rozycki.
  25. */
  26. #include <linux/bug.h>
  27. #include <linux/module.h>
  28. #include <linux/kernel.h>
  29. #include <linux/string.h>
  30. #include <linux/timer.h>
  31. #include <linux/errno.h>
  32. #include <linux/ioport.h>
  33. #include <linux/slab.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/netdevice.h>
  36. #include <linux/etherdevice.h>
  37. #include <linux/skbuff.h>
  38. #include <linux/init.h>
  39. #include <linux/bitops.h>
  40. #include <linux/err.h>
  41. #include <linux/ethtool.h>
  42. #include <linux/mii.h>
  43. #include <linux/phy.h>
  44. #include <linux/platform_device.h>
  45. #include <asm/cache.h>
  46. #include <asm/io.h>
  47. #include <asm/processor.h> /* Processor type for cache alignment. */
  48. /* This is only here until the firmware is ready. In that case,
  49. the firmware leaves the ethernet address in the register for us. */
  50. #ifdef CONFIG_SIBYTE_STANDALONE
  51. #define SBMAC_ETH0_HWADDR "40:00:00:00:01:00"
  52. #define SBMAC_ETH1_HWADDR "40:00:00:00:01:01"
  53. #define SBMAC_ETH2_HWADDR "40:00:00:00:01:02"
  54. #define SBMAC_ETH3_HWADDR "40:00:00:00:01:03"
  55. #endif
  56. /* These identify the driver base version and may not be removed. */
  57. #if 0
  58. static char version1[] __initdata =
  59. "sb1250-mac.c:1.00 1/11/2001 Written by Mitch Lichtenberg\n";
  60. #endif
  61. /* Operational parameters that usually are not changed. */
  62. #define CONFIG_SBMAC_COALESCE
  63. /* Time in jiffies before concluding the transmitter is hung. */
  64. #define TX_TIMEOUT (2*HZ)
  65. MODULE_AUTHOR("Mitch Lichtenberg (Broadcom Corp.)");
  66. MODULE_DESCRIPTION("Broadcom SiByte SOC GB Ethernet driver");
  67. /* A few user-configurable values which may be modified when a driver
  68. module is loaded. */
  69. /* 1 normal messages, 0 quiet .. 7 verbose. */
  70. static int debug = 1;
  71. module_param(debug, int, S_IRUGO);
  72. MODULE_PARM_DESC(debug, "Debug messages");
  73. #ifdef CONFIG_SBMAC_COALESCE
  74. static int int_pktcnt_tx = 255;
  75. module_param(int_pktcnt_tx, int, S_IRUGO);
  76. MODULE_PARM_DESC(int_pktcnt_tx, "TX packet count");
  77. static int int_timeout_tx = 255;
  78. module_param(int_timeout_tx, int, S_IRUGO);
  79. MODULE_PARM_DESC(int_timeout_tx, "TX timeout value");
  80. static int int_pktcnt_rx = 64;
  81. module_param(int_pktcnt_rx, int, S_IRUGO);
  82. MODULE_PARM_DESC(int_pktcnt_rx, "RX packet count");
  83. static int int_timeout_rx = 64;
  84. module_param(int_timeout_rx, int, S_IRUGO);
  85. MODULE_PARM_DESC(int_timeout_rx, "RX timeout value");
  86. #endif
  87. #include <asm/sibyte/board.h>
  88. #include <asm/sibyte/sb1250.h>
  89. #if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
  90. #include <asm/sibyte/bcm1480_regs.h>
  91. #include <asm/sibyte/bcm1480_int.h>
  92. #define R_MAC_DMA_OODPKTLOST_RX R_MAC_DMA_OODPKTLOST
  93. #elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
  94. #include <asm/sibyte/sb1250_regs.h>
  95. #include <asm/sibyte/sb1250_int.h>
  96. #else
  97. #error invalid SiByte MAC configuation
  98. #endif
  99. #include <asm/sibyte/sb1250_scd.h>
  100. #include <asm/sibyte/sb1250_mac.h>
  101. #include <asm/sibyte/sb1250_dma.h>
  102. #if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
  103. #define UNIT_INT(n) (K_BCM1480_INT_MAC_0 + ((n) * 2))
  104. #elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
  105. #define UNIT_INT(n) (K_INT_MAC_0 + (n))
  106. #else
  107. #error invalid SiByte MAC configuation
  108. #endif
  109. #ifdef K_INT_PHY
  110. #define SBMAC_PHY_INT K_INT_PHY
  111. #else
  112. #define SBMAC_PHY_INT PHY_POLL
  113. #endif
  114. /**********************************************************************
  115. * Simple types
  116. ********************************************************************* */
  117. enum sbmac_speed {
  118. sbmac_speed_none = 0,
  119. sbmac_speed_10 = SPEED_10,
  120. sbmac_speed_100 = SPEED_100,
  121. sbmac_speed_1000 = SPEED_1000,
  122. };
  123. enum sbmac_duplex {
  124. sbmac_duplex_none = -1,
  125. sbmac_duplex_half = DUPLEX_HALF,
  126. sbmac_duplex_full = DUPLEX_FULL,
  127. };
  128. enum sbmac_fc {
  129. sbmac_fc_none,
  130. sbmac_fc_disabled,
  131. sbmac_fc_frame,
  132. sbmac_fc_collision,
  133. sbmac_fc_carrier,
  134. };
  135. enum sbmac_state {
  136. sbmac_state_uninit,
  137. sbmac_state_off,
  138. sbmac_state_on,
  139. sbmac_state_broken,
  140. };
  141. /**********************************************************************
  142. * Macros
  143. ********************************************************************* */
  144. #define SBDMA_NEXTBUF(d,f) ((((d)->f+1) == (d)->sbdma_dscrtable_end) ? \
  145. (d)->sbdma_dscrtable : (d)->f+1)
  146. #define NUMCACHEBLKS(x) (((x)+SMP_CACHE_BYTES-1)/SMP_CACHE_BYTES)
  147. #define SBMAC_MAX_TXDESCR 256
  148. #define SBMAC_MAX_RXDESCR 256
  149. #define ETHER_ADDR_LEN 6
  150. #define ENET_PACKET_SIZE 1518
  151. /*#define ENET_PACKET_SIZE 9216 */
  152. /**********************************************************************
  153. * DMA Descriptor structure
  154. ********************************************************************* */
  155. struct sbdmadscr {
  156. uint64_t dscr_a;
  157. uint64_t dscr_b;
  158. };
  159. /**********************************************************************
  160. * DMA Controller structure
  161. ********************************************************************* */
  162. struct sbmacdma {
  163. /*
  164. * This stuff is used to identify the channel and the registers
  165. * associated with it.
  166. */
  167. struct sbmac_softc *sbdma_eth; /* back pointer to associated
  168. MAC */
  169. int sbdma_channel; /* channel number */
  170. int sbdma_txdir; /* direction (1=transmit) */
  171. int sbdma_maxdescr; /* total # of descriptors
  172. in ring */
  173. #ifdef CONFIG_SBMAC_COALESCE
  174. int sbdma_int_pktcnt;
  175. /* # descriptors rx/tx
  176. before interrupt */
  177. int sbdma_int_timeout;
  178. /* # usec rx/tx interrupt */
  179. #endif
  180. void __iomem *sbdma_config0; /* DMA config register 0 */
  181. void __iomem *sbdma_config1; /* DMA config register 1 */
  182. void __iomem *sbdma_dscrbase;
  183. /* descriptor base address */
  184. void __iomem *sbdma_dscrcnt; /* descriptor count register */
  185. void __iomem *sbdma_curdscr; /* current descriptor
  186. address */
  187. void __iomem *sbdma_oodpktlost;
  188. /* pkt drop (rx only) */
  189. /*
  190. * This stuff is for maintenance of the ring
  191. */
  192. void *sbdma_dscrtable_unaligned;
  193. struct sbdmadscr *sbdma_dscrtable;
  194. /* base of descriptor table */
  195. struct sbdmadscr *sbdma_dscrtable_end;
  196. /* end of descriptor table */
  197. struct sk_buff **sbdma_ctxtable;
  198. /* context table, one
  199. per descr */
  200. dma_addr_t sbdma_dscrtable_phys;
  201. /* and also the phys addr */
  202. struct sbdmadscr *sbdma_addptr; /* next dscr for sw to add */
  203. struct sbdmadscr *sbdma_remptr; /* next dscr for sw
  204. to remove */
  205. };
  206. /**********************************************************************
  207. * Ethernet softc structure
  208. ********************************************************************* */
  209. struct sbmac_softc {
  210. /*
  211. * Linux-specific things
  212. */
  213. struct net_device *sbm_dev; /* pointer to linux device */
  214. struct napi_struct napi;
  215. struct phy_device *phy_dev; /* the associated PHY device */
  216. struct mii_bus *mii_bus; /* the MII bus */
  217. int phy_irq[PHY_MAX_ADDR];
  218. spinlock_t sbm_lock; /* spin lock */
  219. int sbm_devflags; /* current device flags */
  220. /*
  221. * Controller-specific things
  222. */
  223. void __iomem *sbm_base; /* MAC's base address */
  224. enum sbmac_state sbm_state; /* current state */
  225. void __iomem *sbm_macenable; /* MAC Enable Register */
  226. void __iomem *sbm_maccfg; /* MAC Config Register */
  227. void __iomem *sbm_fifocfg; /* FIFO Config Register */
  228. void __iomem *sbm_framecfg; /* Frame Config Register */
  229. void __iomem *sbm_rxfilter; /* Receive Filter Register */
  230. void __iomem *sbm_isr; /* Interrupt Status Register */
  231. void __iomem *sbm_imr; /* Interrupt Mask Register */
  232. void __iomem *sbm_mdio; /* MDIO Register */
  233. enum sbmac_speed sbm_speed; /* current speed */
  234. enum sbmac_duplex sbm_duplex; /* current duplex */
  235. enum sbmac_fc sbm_fc; /* cur. flow control setting */
  236. int sbm_pause; /* current pause setting */
  237. int sbm_link; /* current link state */
  238. unsigned char sbm_hwaddr[ETHER_ADDR_LEN];
  239. struct sbmacdma sbm_txdma; /* only channel 0 for now */
  240. struct sbmacdma sbm_rxdma;
  241. int rx_hw_checksum;
  242. int sbe_idx;
  243. };
  244. /**********************************************************************
  245. * Externs
  246. ********************************************************************* */
  247. /**********************************************************************
  248. * Prototypes
  249. ********************************************************************* */
  250. static void sbdma_initctx(struct sbmacdma *d, struct sbmac_softc *s, int chan,
  251. int txrx, int maxdescr);
  252. static void sbdma_channel_start(struct sbmacdma *d, int rxtx);
  253. static int sbdma_add_rcvbuffer(struct sbmac_softc *sc, struct sbmacdma *d,
  254. struct sk_buff *m);
  255. static int sbdma_add_txbuffer(struct sbmacdma *d, struct sk_buff *m);
  256. static void sbdma_emptyring(struct sbmacdma *d);
  257. static void sbdma_fillring(struct sbmac_softc *sc, struct sbmacdma *d);
  258. static int sbdma_rx_process(struct sbmac_softc *sc, struct sbmacdma *d,
  259. int work_to_do, int poll);
  260. static void sbdma_tx_process(struct sbmac_softc *sc, struct sbmacdma *d,
  261. int poll);
  262. static int sbmac_initctx(struct sbmac_softc *s);
  263. static void sbmac_channel_start(struct sbmac_softc *s);
  264. static void sbmac_channel_stop(struct sbmac_softc *s);
  265. static enum sbmac_state sbmac_set_channel_state(struct sbmac_softc *,
  266. enum sbmac_state);
  267. static void sbmac_promiscuous_mode(struct sbmac_softc *sc, int onoff);
  268. static uint64_t sbmac_addr2reg(unsigned char *ptr);
  269. static irqreturn_t sbmac_intr(int irq, void *dev_instance);
  270. static int sbmac_start_tx(struct sk_buff *skb, struct net_device *dev);
  271. static void sbmac_setmulti(struct sbmac_softc *sc);
  272. static int sbmac_init(struct platform_device *pldev, long long base);
  273. static int sbmac_set_speed(struct sbmac_softc *s, enum sbmac_speed speed);
  274. static int sbmac_set_duplex(struct sbmac_softc *s, enum sbmac_duplex duplex,
  275. enum sbmac_fc fc);
  276. static int sbmac_open(struct net_device *dev);
  277. static void sbmac_tx_timeout (struct net_device *dev);
  278. static void sbmac_set_rx_mode(struct net_device *dev);
  279. static int sbmac_mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
  280. static int sbmac_close(struct net_device *dev);
  281. static int sbmac_poll(struct napi_struct *napi, int budget);
  282. static void sbmac_mii_poll(struct net_device *dev);
  283. static int sbmac_mii_probe(struct net_device *dev);
  284. static void sbmac_mii_sync(void __iomem *sbm_mdio);
  285. static void sbmac_mii_senddata(void __iomem *sbm_mdio, unsigned int data,
  286. int bitcnt);
  287. static int sbmac_mii_read(struct mii_bus *bus, int phyaddr, int regidx);
  288. static int sbmac_mii_write(struct mii_bus *bus, int phyaddr, int regidx,
  289. u16 val);
  290. /**********************************************************************
  291. * Globals
  292. ********************************************************************* */
  293. static char sbmac_string[] = "sb1250-mac";
  294. static char sbmac_pretty[] = "SB1250 MAC";
  295. static char sbmac_mdio_string[] = "sb1250-mac-mdio";
  296. /**********************************************************************
  297. * MDIO constants
  298. ********************************************************************* */
  299. #define MII_COMMAND_START 0x01
  300. #define MII_COMMAND_READ 0x02
  301. #define MII_COMMAND_WRITE 0x01
  302. #define MII_COMMAND_ACK 0x02
  303. #define M_MAC_MDIO_DIR_OUTPUT 0 /* for clarity */
  304. #define ENABLE 1
  305. #define DISABLE 0
  306. /**********************************************************************
  307. * SBMAC_MII_SYNC(sbm_mdio)
  308. *
  309. * Synchronize with the MII - send a pattern of bits to the MII
  310. * that will guarantee that it is ready to accept a command.
  311. *
  312. * Input parameters:
  313. * sbm_mdio - address of the MAC's MDIO register
  314. *
  315. * Return value:
  316. * nothing
  317. ********************************************************************* */
  318. static void sbmac_mii_sync(void __iomem *sbm_mdio)
  319. {
  320. int cnt;
  321. uint64_t bits;
  322. int mac_mdio_genc;
  323. mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
  324. bits = M_MAC_MDIO_DIR_OUTPUT | M_MAC_MDIO_OUT;
  325. __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
  326. for (cnt = 0; cnt < 32; cnt++) {
  327. __raw_writeq(bits | M_MAC_MDC | mac_mdio_genc, sbm_mdio);
  328. __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
  329. }
  330. }
  331. /**********************************************************************
  332. * SBMAC_MII_SENDDATA(sbm_mdio, data, bitcnt)
  333. *
  334. * Send some bits to the MII. The bits to be sent are right-
  335. * justified in the 'data' parameter.
  336. *
  337. * Input parameters:
  338. * sbm_mdio - address of the MAC's MDIO register
  339. * data - data to send
  340. * bitcnt - number of bits to send
  341. ********************************************************************* */
  342. static void sbmac_mii_senddata(void __iomem *sbm_mdio, unsigned int data,
  343. int bitcnt)
  344. {
  345. int i;
  346. uint64_t bits;
  347. unsigned int curmask;
  348. int mac_mdio_genc;
  349. mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
  350. bits = M_MAC_MDIO_DIR_OUTPUT;
  351. __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
  352. curmask = 1 << (bitcnt - 1);
  353. for (i = 0; i < bitcnt; i++) {
  354. if (data & curmask)
  355. bits |= M_MAC_MDIO_OUT;
  356. else bits &= ~M_MAC_MDIO_OUT;
  357. __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
  358. __raw_writeq(bits | M_MAC_MDC | mac_mdio_genc, sbm_mdio);
  359. __raw_writeq(bits | mac_mdio_genc, sbm_mdio);
  360. curmask >>= 1;
  361. }
  362. }
  363. /**********************************************************************
  364. * SBMAC_MII_READ(bus, phyaddr, regidx)
  365. * Read a PHY register.
  366. *
  367. * Input parameters:
  368. * bus - MDIO bus handle
  369. * phyaddr - PHY's address
  370. * regnum - index of register to read
  371. *
  372. * Return value:
  373. * value read, or 0xffff if an error occurred.
  374. ********************************************************************* */
  375. static int sbmac_mii_read(struct mii_bus *bus, int phyaddr, int regidx)
  376. {
  377. struct sbmac_softc *sc = (struct sbmac_softc *)bus->priv;
  378. void __iomem *sbm_mdio = sc->sbm_mdio;
  379. int idx;
  380. int error;
  381. int regval;
  382. int mac_mdio_genc;
  383. /*
  384. * Synchronize ourselves so that the PHY knows the next
  385. * thing coming down is a command
  386. */
  387. sbmac_mii_sync(sbm_mdio);
  388. /*
  389. * Send the data to the PHY. The sequence is
  390. * a "start" command (2 bits)
  391. * a "read" command (2 bits)
  392. * the PHY addr (5 bits)
  393. * the register index (5 bits)
  394. */
  395. sbmac_mii_senddata(sbm_mdio, MII_COMMAND_START, 2);
  396. sbmac_mii_senddata(sbm_mdio, MII_COMMAND_READ, 2);
  397. sbmac_mii_senddata(sbm_mdio, phyaddr, 5);
  398. sbmac_mii_senddata(sbm_mdio, regidx, 5);
  399. mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
  400. /*
  401. * Switch the port around without a clock transition.
  402. */
  403. __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
  404. /*
  405. * Send out a clock pulse to signal we want the status
  406. */
  407. __raw_writeq(M_MAC_MDIO_DIR_INPUT | M_MAC_MDC | mac_mdio_genc,
  408. sbm_mdio);
  409. __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
  410. /*
  411. * If an error occurred, the PHY will signal '1' back
  412. */
  413. error = __raw_readq(sbm_mdio) & M_MAC_MDIO_IN;
  414. /*
  415. * Issue an 'idle' clock pulse, but keep the direction
  416. * the same.
  417. */
  418. __raw_writeq(M_MAC_MDIO_DIR_INPUT | M_MAC_MDC | mac_mdio_genc,
  419. sbm_mdio);
  420. __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
  421. regval = 0;
  422. for (idx = 0; idx < 16; idx++) {
  423. regval <<= 1;
  424. if (error == 0) {
  425. if (__raw_readq(sbm_mdio) & M_MAC_MDIO_IN)
  426. regval |= 1;
  427. }
  428. __raw_writeq(M_MAC_MDIO_DIR_INPUT | M_MAC_MDC | mac_mdio_genc,
  429. sbm_mdio);
  430. __raw_writeq(M_MAC_MDIO_DIR_INPUT | mac_mdio_genc, sbm_mdio);
  431. }
  432. /* Switch back to output */
  433. __raw_writeq(M_MAC_MDIO_DIR_OUTPUT | mac_mdio_genc, sbm_mdio);
  434. if (error == 0)
  435. return regval;
  436. return 0xffff;
  437. }
  438. /**********************************************************************
  439. * SBMAC_MII_WRITE(bus, phyaddr, regidx, regval)
  440. *
  441. * Write a value to a PHY register.
  442. *
  443. * Input parameters:
  444. * bus - MDIO bus handle
  445. * phyaddr - PHY to use
  446. * regidx - register within the PHY
  447. * regval - data to write to register
  448. *
  449. * Return value:
  450. * 0 for success
  451. ********************************************************************* */
  452. static int sbmac_mii_write(struct mii_bus *bus, int phyaddr, int regidx,
  453. u16 regval)
  454. {
  455. struct sbmac_softc *sc = (struct sbmac_softc *)bus->priv;
  456. void __iomem *sbm_mdio = sc->sbm_mdio;
  457. int mac_mdio_genc;
  458. sbmac_mii_sync(sbm_mdio);
  459. sbmac_mii_senddata(sbm_mdio, MII_COMMAND_START, 2);
  460. sbmac_mii_senddata(sbm_mdio, MII_COMMAND_WRITE, 2);
  461. sbmac_mii_senddata(sbm_mdio, phyaddr, 5);
  462. sbmac_mii_senddata(sbm_mdio, regidx, 5);
  463. sbmac_mii_senddata(sbm_mdio, MII_COMMAND_ACK, 2);
  464. sbmac_mii_senddata(sbm_mdio, regval, 16);
  465. mac_mdio_genc = __raw_readq(sbm_mdio) & M_MAC_GENC;
  466. __raw_writeq(M_MAC_MDIO_DIR_OUTPUT | mac_mdio_genc, sbm_mdio);
  467. return 0;
  468. }
  469. /**********************************************************************
  470. * SBDMA_INITCTX(d,s,chan,txrx,maxdescr)
  471. *
  472. * Initialize a DMA channel context. Since there are potentially
  473. * eight DMA channels per MAC, it's nice to do this in a standard
  474. * way.
  475. *
  476. * Input parameters:
  477. * d - struct sbmacdma (DMA channel context)
  478. * s - struct sbmac_softc (pointer to a MAC)
  479. * chan - channel number (0..1 right now)
  480. * txrx - Identifies DMA_TX or DMA_RX for channel direction
  481. * maxdescr - number of descriptors
  482. *
  483. * Return value:
  484. * nothing
  485. ********************************************************************* */
  486. static void sbdma_initctx(struct sbmacdma *d, struct sbmac_softc *s, int chan,
  487. int txrx, int maxdescr)
  488. {
  489. #ifdef CONFIG_SBMAC_COALESCE
  490. int int_pktcnt, int_timeout;
  491. #endif
  492. /*
  493. * Save away interesting stuff in the structure
  494. */
  495. d->sbdma_eth = s;
  496. d->sbdma_channel = chan;
  497. d->sbdma_txdir = txrx;
  498. #if 0
  499. /* RMON clearing */
  500. s->sbe_idx =(s->sbm_base - A_MAC_BASE_0)/MAC_SPACING;
  501. #endif
  502. __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_BYTES);
  503. __raw_writeq(0, s->sbm_base + R_MAC_RMON_COLLISIONS);
  504. __raw_writeq(0, s->sbm_base + R_MAC_RMON_LATE_COL);
  505. __raw_writeq(0, s->sbm_base + R_MAC_RMON_EX_COL);
  506. __raw_writeq(0, s->sbm_base + R_MAC_RMON_FCS_ERROR);
  507. __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_ABORT);
  508. __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_BAD);
  509. __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_GOOD);
  510. __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_RUNT);
  511. __raw_writeq(0, s->sbm_base + R_MAC_RMON_TX_OVERSIZE);
  512. __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_BYTES);
  513. __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_MCAST);
  514. __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_BCAST);
  515. __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_BAD);
  516. __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_GOOD);
  517. __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_RUNT);
  518. __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_OVERSIZE);
  519. __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_FCS_ERROR);
  520. __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_LENGTH_ERROR);
  521. __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_CODE_ERROR);
  522. __raw_writeq(0, s->sbm_base + R_MAC_RMON_RX_ALIGN_ERROR);
  523. /*
  524. * initialize register pointers
  525. */
  526. d->sbdma_config0 =
  527. s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CONFIG0);
  528. d->sbdma_config1 =
  529. s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CONFIG1);
  530. d->sbdma_dscrbase =
  531. s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_DSCR_BASE);
  532. d->sbdma_dscrcnt =
  533. s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_DSCR_CNT);
  534. d->sbdma_curdscr =
  535. s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_CUR_DSCRADDR);
  536. if (d->sbdma_txdir)
  537. d->sbdma_oodpktlost = NULL;
  538. else
  539. d->sbdma_oodpktlost =
  540. s->sbm_base + R_MAC_DMA_REGISTER(txrx,chan,R_MAC_DMA_OODPKTLOST_RX);
  541. /*
  542. * Allocate memory for the ring
  543. */
  544. d->sbdma_maxdescr = maxdescr;
  545. d->sbdma_dscrtable_unaligned = kcalloc(d->sbdma_maxdescr + 1,
  546. sizeof(*d->sbdma_dscrtable),
  547. GFP_KERNEL);
  548. /*
  549. * The descriptor table must be aligned to at least 16 bytes or the
  550. * MAC will corrupt it.
  551. */
  552. d->sbdma_dscrtable = (struct sbdmadscr *)
  553. ALIGN((unsigned long)d->sbdma_dscrtable_unaligned,
  554. sizeof(*d->sbdma_dscrtable));
  555. d->sbdma_dscrtable_end = d->sbdma_dscrtable + d->sbdma_maxdescr;
  556. d->sbdma_dscrtable_phys = virt_to_phys(d->sbdma_dscrtable);
  557. /*
  558. * And context table
  559. */
  560. d->sbdma_ctxtable = kcalloc(d->sbdma_maxdescr,
  561. sizeof(*d->sbdma_ctxtable), GFP_KERNEL);
  562. #ifdef CONFIG_SBMAC_COALESCE
  563. /*
  564. * Setup Rx/Tx DMA coalescing defaults
  565. */
  566. int_pktcnt = (txrx == DMA_TX) ? int_pktcnt_tx : int_pktcnt_rx;
  567. if ( int_pktcnt ) {
  568. d->sbdma_int_pktcnt = int_pktcnt;
  569. } else {
  570. d->sbdma_int_pktcnt = 1;
  571. }
  572. int_timeout = (txrx == DMA_TX) ? int_timeout_tx : int_timeout_rx;
  573. if ( int_timeout ) {
  574. d->sbdma_int_timeout = int_timeout;
  575. } else {
  576. d->sbdma_int_timeout = 0;
  577. }
  578. #endif
  579. }
  580. /**********************************************************************
  581. * SBDMA_CHANNEL_START(d)
  582. *
  583. * Initialize the hardware registers for a DMA channel.
  584. *
  585. * Input parameters:
  586. * d - DMA channel to init (context must be previously init'd
  587. * rxtx - DMA_RX or DMA_TX depending on what type of channel
  588. *
  589. * Return value:
  590. * nothing
  591. ********************************************************************* */
  592. static void sbdma_channel_start(struct sbmacdma *d, int rxtx)
  593. {
  594. /*
  595. * Turn on the DMA channel
  596. */
  597. #ifdef CONFIG_SBMAC_COALESCE
  598. __raw_writeq(V_DMA_INT_TIMEOUT(d->sbdma_int_timeout) |
  599. 0, d->sbdma_config1);
  600. __raw_writeq(M_DMA_EOP_INT_EN |
  601. V_DMA_RINGSZ(d->sbdma_maxdescr) |
  602. V_DMA_INT_PKTCNT(d->sbdma_int_pktcnt) |
  603. 0, d->sbdma_config0);
  604. #else
  605. __raw_writeq(0, d->sbdma_config1);
  606. __raw_writeq(V_DMA_RINGSZ(d->sbdma_maxdescr) |
  607. 0, d->sbdma_config0);
  608. #endif
  609. __raw_writeq(d->sbdma_dscrtable_phys, d->sbdma_dscrbase);
  610. /*
  611. * Initialize ring pointers
  612. */
  613. d->sbdma_addptr = d->sbdma_dscrtable;
  614. d->sbdma_remptr = d->sbdma_dscrtable;
  615. }
  616. /**********************************************************************
  617. * SBDMA_CHANNEL_STOP(d)
  618. *
  619. * Initialize the hardware registers for a DMA channel.
  620. *
  621. * Input parameters:
  622. * d - DMA channel to init (context must be previously init'd
  623. *
  624. * Return value:
  625. * nothing
  626. ********************************************************************* */
  627. static void sbdma_channel_stop(struct sbmacdma *d)
  628. {
  629. /*
  630. * Turn off the DMA channel
  631. */
  632. __raw_writeq(0, d->sbdma_config1);
  633. __raw_writeq(0, d->sbdma_dscrbase);
  634. __raw_writeq(0, d->sbdma_config0);
  635. /*
  636. * Zero ring pointers
  637. */
  638. d->sbdma_addptr = NULL;
  639. d->sbdma_remptr = NULL;
  640. }
  641. static inline void sbdma_align_skb(struct sk_buff *skb,
  642. unsigned int power2, unsigned int offset)
  643. {
  644. unsigned char *addr = skb->data;
  645. unsigned char *newaddr = PTR_ALIGN(addr, power2);
  646. skb_reserve(skb, newaddr - addr + offset);
  647. }
  648. /**********************************************************************
  649. * SBDMA_ADD_RCVBUFFER(d,sb)
  650. *
  651. * Add a buffer to the specified DMA channel. For receive channels,
  652. * this queues a buffer for inbound packets.
  653. *
  654. * Input parameters:
  655. * sc - softc structure
  656. * d - DMA channel descriptor
  657. * sb - sk_buff to add, or NULL if we should allocate one
  658. *
  659. * Return value:
  660. * 0 if buffer could not be added (ring is full)
  661. * 1 if buffer added successfully
  662. ********************************************************************* */
  663. static int sbdma_add_rcvbuffer(struct sbmac_softc *sc, struct sbmacdma *d,
  664. struct sk_buff *sb)
  665. {
  666. struct net_device *dev = sc->sbm_dev;
  667. struct sbdmadscr *dsc;
  668. struct sbdmadscr *nextdsc;
  669. struct sk_buff *sb_new = NULL;
  670. int pktsize = ENET_PACKET_SIZE;
  671. /* get pointer to our current place in the ring */
  672. dsc = d->sbdma_addptr;
  673. nextdsc = SBDMA_NEXTBUF(d,sbdma_addptr);
  674. /*
  675. * figure out if the ring is full - if the next descriptor
  676. * is the same as the one that we're going to remove from
  677. * the ring, the ring is full
  678. */
  679. if (nextdsc == d->sbdma_remptr) {
  680. return -ENOSPC;
  681. }
  682. /*
  683. * Allocate a sk_buff if we don't already have one.
  684. * If we do have an sk_buff, reset it so that it's empty.
  685. *
  686. * Note: sk_buffs don't seem to be guaranteed to have any sort
  687. * of alignment when they are allocated. Therefore, allocate enough
  688. * extra space to make sure that:
  689. *
  690. * 1. the data does not start in the middle of a cache line.
  691. * 2. The data does not end in the middle of a cache line
  692. * 3. The buffer can be aligned such that the IP addresses are
  693. * naturally aligned.
  694. *
  695. * Remember, the SOCs MAC writes whole cache lines at a time,
  696. * without reading the old contents first. So, if the sk_buff's
  697. * data portion starts in the middle of a cache line, the SOC
  698. * DMA will trash the beginning (and ending) portions.
  699. */
  700. if (sb == NULL) {
  701. sb_new = netdev_alloc_skb(dev, ENET_PACKET_SIZE +
  702. SMP_CACHE_BYTES * 2 +
  703. NET_IP_ALIGN);
  704. if (sb_new == NULL) {
  705. pr_info("%s: sk_buff allocation failed\n",
  706. d->sbdma_eth->sbm_dev->name);
  707. return -ENOBUFS;
  708. }
  709. sbdma_align_skb(sb_new, SMP_CACHE_BYTES, NET_IP_ALIGN);
  710. }
  711. else {
  712. sb_new = sb;
  713. /*
  714. * nothing special to reinit buffer, it's already aligned
  715. * and sb->data already points to a good place.
  716. */
  717. }
  718. /*
  719. * fill in the descriptor
  720. */
  721. #ifdef CONFIG_SBMAC_COALESCE
  722. /*
  723. * Do not interrupt per DMA transfer.
  724. */
  725. dsc->dscr_a = virt_to_phys(sb_new->data) |
  726. V_DMA_DSCRA_A_SIZE(NUMCACHEBLKS(pktsize + NET_IP_ALIGN)) | 0;
  727. #else
  728. dsc->dscr_a = virt_to_phys(sb_new->data) |
  729. V_DMA_DSCRA_A_SIZE(NUMCACHEBLKS(pktsize + NET_IP_ALIGN)) |
  730. M_DMA_DSCRA_INTERRUPT;
  731. #endif
  732. /* receiving: no options */
  733. dsc->dscr_b = 0;
  734. /*
  735. * fill in the context
  736. */
  737. d->sbdma_ctxtable[dsc-d->sbdma_dscrtable] = sb_new;
  738. /*
  739. * point at next packet
  740. */
  741. d->sbdma_addptr = nextdsc;
  742. /*
  743. * Give the buffer to the DMA engine.
  744. */
  745. __raw_writeq(1, d->sbdma_dscrcnt);
  746. return 0; /* we did it */
  747. }
  748. /**********************************************************************
  749. * SBDMA_ADD_TXBUFFER(d,sb)
  750. *
  751. * Add a transmit buffer to the specified DMA channel, causing a
  752. * transmit to start.
  753. *
  754. * Input parameters:
  755. * d - DMA channel descriptor
  756. * sb - sk_buff to add
  757. *
  758. * Return value:
  759. * 0 transmit queued successfully
  760. * otherwise error code
  761. ********************************************************************* */
  762. static int sbdma_add_txbuffer(struct sbmacdma *d, struct sk_buff *sb)
  763. {
  764. struct sbdmadscr *dsc;
  765. struct sbdmadscr *nextdsc;
  766. uint64_t phys;
  767. uint64_t ncb;
  768. int length;
  769. /* get pointer to our current place in the ring */
  770. dsc = d->sbdma_addptr;
  771. nextdsc = SBDMA_NEXTBUF(d,sbdma_addptr);
  772. /*
  773. * figure out if the ring is full - if the next descriptor
  774. * is the same as the one that we're going to remove from
  775. * the ring, the ring is full
  776. */
  777. if (nextdsc == d->sbdma_remptr) {
  778. return -ENOSPC;
  779. }
  780. /*
  781. * Under Linux, it's not necessary to copy/coalesce buffers
  782. * like it is on NetBSD. We think they're all contiguous,
  783. * but that may not be true for GBE.
  784. */
  785. length = sb->len;
  786. /*
  787. * fill in the descriptor. Note that the number of cache
  788. * blocks in the descriptor is the number of blocks
  789. * *spanned*, so we need to add in the offset (if any)
  790. * while doing the calculation.
  791. */
  792. phys = virt_to_phys(sb->data);
  793. ncb = NUMCACHEBLKS(length+(phys & (SMP_CACHE_BYTES - 1)));
  794. dsc->dscr_a = phys |
  795. V_DMA_DSCRA_A_SIZE(ncb) |
  796. #ifndef CONFIG_SBMAC_COALESCE
  797. M_DMA_DSCRA_INTERRUPT |
  798. #endif
  799. M_DMA_ETHTX_SOP;
  800. /* transmitting: set outbound options and length */
  801. dsc->dscr_b = V_DMA_DSCRB_OPTIONS(K_DMA_ETHTX_APPENDCRC_APPENDPAD) |
  802. V_DMA_DSCRB_PKT_SIZE(length);
  803. /*
  804. * fill in the context
  805. */
  806. d->sbdma_ctxtable[dsc-d->sbdma_dscrtable] = sb;
  807. /*
  808. * point at next packet
  809. */
  810. d->sbdma_addptr = nextdsc;
  811. /*
  812. * Give the buffer to the DMA engine.
  813. */
  814. __raw_writeq(1, d->sbdma_dscrcnt);
  815. return 0; /* we did it */
  816. }
  817. /**********************************************************************
  818. * SBDMA_EMPTYRING(d)
  819. *
  820. * Free all allocated sk_buffs on the specified DMA channel;
  821. *
  822. * Input parameters:
  823. * d - DMA channel
  824. *
  825. * Return value:
  826. * nothing
  827. ********************************************************************* */
  828. static void sbdma_emptyring(struct sbmacdma *d)
  829. {
  830. int idx;
  831. struct sk_buff *sb;
  832. for (idx = 0; idx < d->sbdma_maxdescr; idx++) {
  833. sb = d->sbdma_ctxtable[idx];
  834. if (sb) {
  835. dev_kfree_skb(sb);
  836. d->sbdma_ctxtable[idx] = NULL;
  837. }
  838. }
  839. }
  840. /**********************************************************************
  841. * SBDMA_FILLRING(d)
  842. *
  843. * Fill the specified DMA channel (must be receive channel)
  844. * with sk_buffs
  845. *
  846. * Input parameters:
  847. * sc - softc structure
  848. * d - DMA channel
  849. *
  850. * Return value:
  851. * nothing
  852. ********************************************************************* */
  853. static void sbdma_fillring(struct sbmac_softc *sc, struct sbmacdma *d)
  854. {
  855. int idx;
  856. for (idx = 0; idx < SBMAC_MAX_RXDESCR - 1; idx++) {
  857. if (sbdma_add_rcvbuffer(sc, d, NULL) != 0)
  858. break;
  859. }
  860. }
  861. #ifdef CONFIG_NET_POLL_CONTROLLER
  862. static void sbmac_netpoll(struct net_device *netdev)
  863. {
  864. struct sbmac_softc *sc = netdev_priv(netdev);
  865. int irq = sc->sbm_dev->irq;
  866. __raw_writeq(0, sc->sbm_imr);
  867. sbmac_intr(irq, netdev);
  868. #ifdef CONFIG_SBMAC_COALESCE
  869. __raw_writeq(((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_TX_CH0) |
  870. ((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_RX_CH0),
  871. sc->sbm_imr);
  872. #else
  873. __raw_writeq((M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
  874. (M_MAC_INT_CHANNEL << S_MAC_RX_CH0), sc->sbm_imr);
  875. #endif
  876. }
  877. #endif
  878. /**********************************************************************
  879. * SBDMA_RX_PROCESS(sc,d,work_to_do,poll)
  880. *
  881. * Process "completed" receive buffers on the specified DMA channel.
  882. *
  883. * Input parameters:
  884. * sc - softc structure
  885. * d - DMA channel context
  886. * work_to_do - no. of packets to process before enabling interrupt
  887. * again (for NAPI)
  888. * poll - 1: using polling (for NAPI)
  889. *
  890. * Return value:
  891. * nothing
  892. ********************************************************************* */
  893. static int sbdma_rx_process(struct sbmac_softc *sc, struct sbmacdma *d,
  894. int work_to_do, int poll)
  895. {
  896. struct net_device *dev = sc->sbm_dev;
  897. int curidx;
  898. int hwidx;
  899. struct sbdmadscr *dsc;
  900. struct sk_buff *sb;
  901. int len;
  902. int work_done = 0;
  903. int dropped = 0;
  904. prefetch(d);
  905. again:
  906. /* Check if the HW dropped any frames */
  907. dev->stats.rx_fifo_errors
  908. += __raw_readq(sc->sbm_rxdma.sbdma_oodpktlost) & 0xffff;
  909. __raw_writeq(0, sc->sbm_rxdma.sbdma_oodpktlost);
  910. while (work_to_do-- > 0) {
  911. /*
  912. * figure out where we are (as an index) and where
  913. * the hardware is (also as an index)
  914. *
  915. * This could be done faster if (for example) the
  916. * descriptor table was page-aligned and contiguous in
  917. * both virtual and physical memory -- you could then
  918. * just compare the low-order bits of the virtual address
  919. * (sbdma_remptr) and the physical address (sbdma_curdscr CSR)
  920. */
  921. dsc = d->sbdma_remptr;
  922. curidx = dsc - d->sbdma_dscrtable;
  923. prefetch(dsc);
  924. prefetch(&d->sbdma_ctxtable[curidx]);
  925. hwidx = ((__raw_readq(d->sbdma_curdscr) & M_DMA_CURDSCR_ADDR) -
  926. d->sbdma_dscrtable_phys) /
  927. sizeof(*d->sbdma_dscrtable);
  928. /*
  929. * If they're the same, that means we've processed all
  930. * of the descriptors up to (but not including) the one that
  931. * the hardware is working on right now.
  932. */
  933. if (curidx == hwidx)
  934. goto done;
  935. /*
  936. * Otherwise, get the packet's sk_buff ptr back
  937. */
  938. sb = d->sbdma_ctxtable[curidx];
  939. d->sbdma_ctxtable[curidx] = NULL;
  940. len = (int)G_DMA_DSCRB_PKT_SIZE(dsc->dscr_b) - 4;
  941. /*
  942. * Check packet status. If good, process it.
  943. * If not, silently drop it and put it back on the
  944. * receive ring.
  945. */
  946. if (likely (!(dsc->dscr_a & M_DMA_ETHRX_BAD))) {
  947. /*
  948. * Add a new buffer to replace the old one. If we fail
  949. * to allocate a buffer, we're going to drop this
  950. * packet and put it right back on the receive ring.
  951. */
  952. if (unlikely(sbdma_add_rcvbuffer(sc, d, NULL) ==
  953. -ENOBUFS)) {
  954. dev->stats.rx_dropped++;
  955. /* Re-add old buffer */
  956. sbdma_add_rcvbuffer(sc, d, sb);
  957. /* No point in continuing at the moment */
  958. printk(KERN_ERR "dropped packet (1)\n");
  959. d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
  960. goto done;
  961. } else {
  962. /*
  963. * Set length into the packet
  964. */
  965. skb_put(sb,len);
  966. /*
  967. * Buffer has been replaced on the
  968. * receive ring. Pass the buffer to
  969. * the kernel
  970. */
  971. sb->protocol = eth_type_trans(sb,d->sbdma_eth->sbm_dev);
  972. /* Check hw IPv4/TCP checksum if supported */
  973. if (sc->rx_hw_checksum == ENABLE) {
  974. if (!((dsc->dscr_a) & M_DMA_ETHRX_BADIP4CS) &&
  975. !((dsc->dscr_a) & M_DMA_ETHRX_BADTCPCS)) {
  976. sb->ip_summed = CHECKSUM_UNNECESSARY;
  977. /* don't need to set sb->csum */
  978. } else {
  979. sb->ip_summed = CHECKSUM_NONE;
  980. }
  981. }
  982. prefetch(sb->data);
  983. prefetch((const void *)(((char *)sb->data)+32));
  984. if (poll)
  985. dropped = netif_receive_skb(sb);
  986. else
  987. dropped = netif_rx(sb);
  988. if (dropped == NET_RX_DROP) {
  989. dev->stats.rx_dropped++;
  990. d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
  991. goto done;
  992. }
  993. else {
  994. dev->stats.rx_bytes += len;
  995. dev->stats.rx_packets++;
  996. }
  997. }
  998. } else {
  999. /*
  1000. * Packet was mangled somehow. Just drop it and
  1001. * put it back on the receive ring.
  1002. */
  1003. dev->stats.rx_errors++;
  1004. sbdma_add_rcvbuffer(sc, d, sb);
  1005. }
  1006. /*
  1007. * .. and advance to the next buffer.
  1008. */
  1009. d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
  1010. work_done++;
  1011. }
  1012. if (!poll) {
  1013. work_to_do = 32;
  1014. goto again; /* collect fifo drop statistics again */
  1015. }
  1016. done:
  1017. return work_done;
  1018. }
  1019. /**********************************************************************
  1020. * SBDMA_TX_PROCESS(sc,d)
  1021. *
  1022. * Process "completed" transmit buffers on the specified DMA channel.
  1023. * This is normally called within the interrupt service routine.
  1024. * Note that this isn't really ideal for priority channels, since
  1025. * it processes all of the packets on a given channel before
  1026. * returning.
  1027. *
  1028. * Input parameters:
  1029. * sc - softc structure
  1030. * d - DMA channel context
  1031. * poll - 1: using polling (for NAPI)
  1032. *
  1033. * Return value:
  1034. * nothing
  1035. ********************************************************************* */
  1036. static void sbdma_tx_process(struct sbmac_softc *sc, struct sbmacdma *d,
  1037. int poll)
  1038. {
  1039. struct net_device *dev = sc->sbm_dev;
  1040. int curidx;
  1041. int hwidx;
  1042. struct sbdmadscr *dsc;
  1043. struct sk_buff *sb;
  1044. unsigned long flags;
  1045. int packets_handled = 0;
  1046. spin_lock_irqsave(&(sc->sbm_lock), flags);
  1047. if (d->sbdma_remptr == d->sbdma_addptr)
  1048. goto end_unlock;
  1049. hwidx = ((__raw_readq(d->sbdma_curdscr) & M_DMA_CURDSCR_ADDR) -
  1050. d->sbdma_dscrtable_phys) / sizeof(*d->sbdma_dscrtable);
  1051. for (;;) {
  1052. /*
  1053. * figure out where we are (as an index) and where
  1054. * the hardware is (also as an index)
  1055. *
  1056. * This could be done faster if (for example) the
  1057. * descriptor table was page-aligned and contiguous in
  1058. * both virtual and physical memory -- you could then
  1059. * just compare the low-order bits of the virtual address
  1060. * (sbdma_remptr) and the physical address (sbdma_curdscr CSR)
  1061. */
  1062. curidx = d->sbdma_remptr - d->sbdma_dscrtable;
  1063. /*
  1064. * If they're the same, that means we've processed all
  1065. * of the descriptors up to (but not including) the one that
  1066. * the hardware is working on right now.
  1067. */
  1068. if (curidx == hwidx)
  1069. break;
  1070. /*
  1071. * Otherwise, get the packet's sk_buff ptr back
  1072. */
  1073. dsc = &(d->sbdma_dscrtable[curidx]);
  1074. sb = d->sbdma_ctxtable[curidx];
  1075. d->sbdma_ctxtable[curidx] = NULL;
  1076. /*
  1077. * Stats
  1078. */
  1079. dev->stats.tx_bytes += sb->len;
  1080. dev->stats.tx_packets++;
  1081. /*
  1082. * for transmits, we just free buffers.
  1083. */
  1084. dev_kfree_skb_irq(sb);
  1085. /*
  1086. * .. and advance to the next buffer.
  1087. */
  1088. d->sbdma_remptr = SBDMA_NEXTBUF(d,sbdma_remptr);
  1089. packets_handled++;
  1090. }
  1091. /*
  1092. * Decide if we should wake up the protocol or not.
  1093. * Other drivers seem to do this when we reach a low
  1094. * watermark on the transmit queue.
  1095. */
  1096. if (packets_handled)
  1097. netif_wake_queue(d->sbdma_eth->sbm_dev);
  1098. end_unlock:
  1099. spin_unlock_irqrestore(&(sc->sbm_lock), flags);
  1100. }
  1101. /**********************************************************************
  1102. * SBMAC_INITCTX(s)
  1103. *
  1104. * Initialize an Ethernet context structure - this is called
  1105. * once per MAC on the 1250. Memory is allocated here, so don't
  1106. * call it again from inside the ioctl routines that bring the
  1107. * interface up/down
  1108. *
  1109. * Input parameters:
  1110. * s - sbmac context structure
  1111. *
  1112. * Return value:
  1113. * 0
  1114. ********************************************************************* */
  1115. static int sbmac_initctx(struct sbmac_softc *s)
  1116. {
  1117. /*
  1118. * figure out the addresses of some ports
  1119. */
  1120. s->sbm_macenable = s->sbm_base + R_MAC_ENABLE;
  1121. s->sbm_maccfg = s->sbm_base + R_MAC_CFG;
  1122. s->sbm_fifocfg = s->sbm_base + R_MAC_THRSH_CFG;
  1123. s->sbm_framecfg = s->sbm_base + R_MAC_FRAMECFG;
  1124. s->sbm_rxfilter = s->sbm_base + R_MAC_ADFILTER_CFG;
  1125. s->sbm_isr = s->sbm_base + R_MAC_STATUS;
  1126. s->sbm_imr = s->sbm_base + R_MAC_INT_MASK;
  1127. s->sbm_mdio = s->sbm_base + R_MAC_MDIO;
  1128. /*
  1129. * Initialize the DMA channels. Right now, only one per MAC is used
  1130. * Note: Only do this _once_, as it allocates memory from the kernel!
  1131. */
  1132. sbdma_initctx(&(s->sbm_txdma),s,0,DMA_TX,SBMAC_MAX_TXDESCR);
  1133. sbdma_initctx(&(s->sbm_rxdma),s,0,DMA_RX,SBMAC_MAX_RXDESCR);
  1134. /*
  1135. * initial state is OFF
  1136. */
  1137. s->sbm_state = sbmac_state_off;
  1138. return 0;
  1139. }
  1140. static void sbdma_uninitctx(struct sbmacdma *d)
  1141. {
  1142. if (d->sbdma_dscrtable_unaligned) {
  1143. kfree(d->sbdma_dscrtable_unaligned);
  1144. d->sbdma_dscrtable_unaligned = d->sbdma_dscrtable = NULL;
  1145. }
  1146. if (d->sbdma_ctxtable) {
  1147. kfree(d->sbdma_ctxtable);
  1148. d->sbdma_ctxtable = NULL;
  1149. }
  1150. }
  1151. static void sbmac_uninitctx(struct sbmac_softc *sc)
  1152. {
  1153. sbdma_uninitctx(&(sc->sbm_txdma));
  1154. sbdma_uninitctx(&(sc->sbm_rxdma));
  1155. }
  1156. /**********************************************************************
  1157. * SBMAC_CHANNEL_START(s)
  1158. *
  1159. * Start packet processing on this MAC.
  1160. *
  1161. * Input parameters:
  1162. * s - sbmac structure
  1163. *
  1164. * Return value:
  1165. * nothing
  1166. ********************************************************************* */
  1167. static void sbmac_channel_start(struct sbmac_softc *s)
  1168. {
  1169. uint64_t reg;
  1170. void __iomem *port;
  1171. uint64_t cfg,fifo,framecfg;
  1172. int idx, th_value;
  1173. /*
  1174. * Don't do this if running
  1175. */
  1176. if (s->sbm_state == sbmac_state_on)
  1177. return;
  1178. /*
  1179. * Bring the controller out of reset, but leave it off.
  1180. */
  1181. __raw_writeq(0, s->sbm_macenable);
  1182. /*
  1183. * Ignore all received packets
  1184. */
  1185. __raw_writeq(0, s->sbm_rxfilter);
  1186. /*
  1187. * Calculate values for various control registers.
  1188. */
  1189. cfg = M_MAC_RETRY_EN |
  1190. M_MAC_TX_HOLD_SOP_EN |
  1191. V_MAC_TX_PAUSE_CNT_16K |
  1192. M_MAC_AP_STAT_EN |
  1193. M_MAC_FAST_SYNC |
  1194. M_MAC_SS_EN |
  1195. 0;
  1196. /*
  1197. * Be sure that RD_THRSH+WR_THRSH <= 32 for pass1 pars
  1198. * and make sure that RD_THRSH + WR_THRSH <=128 for pass2 and above
  1199. * Use a larger RD_THRSH for gigabit
  1200. */
  1201. if (soc_type == K_SYS_SOC_TYPE_BCM1250 && periph_rev < 2)
  1202. th_value = 28;
  1203. else
  1204. th_value = 64;
  1205. fifo = V_MAC_TX_WR_THRSH(4) | /* Must be '4' or '8' */
  1206. ((s->sbm_speed == sbmac_speed_1000)
  1207. ? V_MAC_TX_RD_THRSH(th_value) : V_MAC_TX_RD_THRSH(4)) |
  1208. V_MAC_TX_RL_THRSH(4) |
  1209. V_MAC_RX_PL_THRSH(4) |
  1210. V_MAC_RX_RD_THRSH(4) | /* Must be '4' */
  1211. V_MAC_RX_RL_THRSH(8) |
  1212. 0;
  1213. framecfg = V_MAC_MIN_FRAMESZ_DEFAULT |
  1214. V_MAC_MAX_FRAMESZ_DEFAULT |
  1215. V_MAC_BACKOFF_SEL(1);
  1216. /*
  1217. * Clear out the hash address map
  1218. */
  1219. port = s->sbm_base + R_MAC_HASH_BASE;
  1220. for (idx = 0; idx < MAC_HASH_COUNT; idx++) {
  1221. __raw_writeq(0, port);
  1222. port += sizeof(uint64_t);
  1223. }
  1224. /*
  1225. * Clear out the exact-match table
  1226. */
  1227. port = s->sbm_base + R_MAC_ADDR_BASE;
  1228. for (idx = 0; idx < MAC_ADDR_COUNT; idx++) {
  1229. __raw_writeq(0, port);
  1230. port += sizeof(uint64_t);
  1231. }
  1232. /*
  1233. * Clear out the DMA Channel mapping table registers
  1234. */
  1235. port = s->sbm_base + R_MAC_CHUP0_BASE;
  1236. for (idx = 0; idx < MAC_CHMAP_COUNT; idx++) {
  1237. __raw_writeq(0, port);
  1238. port += sizeof(uint64_t);
  1239. }
  1240. port = s->sbm_base + R_MAC_CHLO0_BASE;
  1241. for (idx = 0; idx < MAC_CHMAP_COUNT; idx++) {
  1242. __raw_writeq(0, port);
  1243. port += sizeof(uint64_t);
  1244. }
  1245. /*
  1246. * Program the hardware address. It goes into the hardware-address
  1247. * register as well as the first filter register.
  1248. */
  1249. reg = sbmac_addr2reg(s->sbm_hwaddr);
  1250. port = s->sbm_base + R_MAC_ADDR_BASE;
  1251. __raw_writeq(reg, port);
  1252. port = s->sbm_base + R_MAC_ETHERNET_ADDR;
  1253. #ifdef CONFIG_SB1_PASS_1_WORKAROUNDS
  1254. /*
  1255. * Pass1 SOCs do not receive packets addressed to the
  1256. * destination address in the R_MAC_ETHERNET_ADDR register.
  1257. * Set the value to zero.
  1258. */
  1259. __raw_writeq(0, port);
  1260. #else
  1261. __raw_writeq(reg, port);
  1262. #endif
  1263. /*
  1264. * Set the receive filter for no packets, and write values
  1265. * to the various config registers
  1266. */
  1267. __raw_writeq(0, s->sbm_rxfilter);
  1268. __raw_writeq(0, s->sbm_imr);
  1269. __raw_writeq(framecfg, s->sbm_framecfg);
  1270. __raw_writeq(fifo, s->sbm_fifocfg);
  1271. __raw_writeq(cfg, s->sbm_maccfg);
  1272. /*
  1273. * Initialize DMA channels (rings should be ok now)
  1274. */
  1275. sbdma_channel_start(&(s->sbm_rxdma), DMA_RX);
  1276. sbdma_channel_start(&(s->sbm_txdma), DMA_TX);
  1277. /*
  1278. * Configure the speed, duplex, and flow control
  1279. */
  1280. sbmac_set_speed(s,s->sbm_speed);
  1281. sbmac_set_duplex(s,s->sbm_duplex,s->sbm_fc);
  1282. /*
  1283. * Fill the receive ring
  1284. */
  1285. sbdma_fillring(s, &(s->sbm_rxdma));
  1286. /*
  1287. * Turn on the rest of the bits in the enable register
  1288. */
  1289. #if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
  1290. __raw_writeq(M_MAC_RXDMA_EN0 |
  1291. M_MAC_TXDMA_EN0, s->sbm_macenable);
  1292. #elif defined(CONFIG_SIBYTE_SB1250) || defined(CONFIG_SIBYTE_BCM112X)
  1293. __raw_writeq(M_MAC_RXDMA_EN0 |
  1294. M_MAC_TXDMA_EN0 |
  1295. M_MAC_RX_ENABLE |
  1296. M_MAC_TX_ENABLE, s->sbm_macenable);
  1297. #else
  1298. #error invalid SiByte MAC configuation
  1299. #endif
  1300. #ifdef CONFIG_SBMAC_COALESCE
  1301. __raw_writeq(((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_TX_CH0) |
  1302. ((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_RX_CH0), s->sbm_imr);
  1303. #else
  1304. __raw_writeq((M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
  1305. (M_MAC_INT_CHANNEL << S_MAC_RX_CH0), s->sbm_imr);
  1306. #endif
  1307. /*
  1308. * Enable receiving unicasts and broadcasts
  1309. */
  1310. __raw_writeq(M_MAC_UCAST_EN | M_MAC_BCAST_EN, s->sbm_rxfilter);
  1311. /*
  1312. * we're running now.
  1313. */
  1314. s->sbm_state = sbmac_state_on;
  1315. /*
  1316. * Program multicast addresses
  1317. */
  1318. sbmac_setmulti(s);
  1319. /*
  1320. * If channel was in promiscuous mode before, turn that on
  1321. */
  1322. if (s->sbm_devflags & IFF_PROMISC) {
  1323. sbmac_promiscuous_mode(s,1);
  1324. }
  1325. }
  1326. /**********************************************************************
  1327. * SBMAC_CHANNEL_STOP(s)
  1328. *
  1329. * Stop packet processing on this MAC.
  1330. *
  1331. * Input parameters:
  1332. * s - sbmac structure
  1333. *
  1334. * Return value:
  1335. * nothing
  1336. ********************************************************************* */
  1337. static void sbmac_channel_stop(struct sbmac_softc *s)
  1338. {
  1339. /* don't do this if already stopped */
  1340. if (s->sbm_state == sbmac_state_off)
  1341. return;
  1342. /* don't accept any packets, disable all interrupts */
  1343. __raw_writeq(0, s->sbm_rxfilter);
  1344. __raw_writeq(0, s->sbm_imr);
  1345. /* Turn off ticker */
  1346. /* XXX */
  1347. /* turn off receiver and transmitter */
  1348. __raw_writeq(0, s->sbm_macenable);
  1349. /* We're stopped now. */
  1350. s->sbm_state = sbmac_state_off;
  1351. /*
  1352. * Stop DMA channels (rings should be ok now)
  1353. */
  1354. sbdma_channel_stop(&(s->sbm_rxdma));
  1355. sbdma_channel_stop(&(s->sbm_txdma));
  1356. /* Empty the receive and transmit rings */
  1357. sbdma_emptyring(&(s->sbm_rxdma));
  1358. sbdma_emptyring(&(s->sbm_txdma));
  1359. }
  1360. /**********************************************************************
  1361. * SBMAC_SET_CHANNEL_STATE(state)
  1362. *
  1363. * Set the channel's state ON or OFF
  1364. *
  1365. * Input parameters:
  1366. * state - new state
  1367. *
  1368. * Return value:
  1369. * old state
  1370. ********************************************************************* */
  1371. static enum sbmac_state sbmac_set_channel_state(struct sbmac_softc *sc,
  1372. enum sbmac_state state)
  1373. {
  1374. enum sbmac_state oldstate = sc->sbm_state;
  1375. /*
  1376. * If same as previous state, return
  1377. */
  1378. if (state == oldstate) {
  1379. return oldstate;
  1380. }
  1381. /*
  1382. * If new state is ON, turn channel on
  1383. */
  1384. if (state == sbmac_state_on) {
  1385. sbmac_channel_start(sc);
  1386. }
  1387. else {
  1388. sbmac_channel_stop(sc);
  1389. }
  1390. /*
  1391. * Return previous state
  1392. */
  1393. return oldstate;
  1394. }
  1395. /**********************************************************************
  1396. * SBMAC_PROMISCUOUS_MODE(sc,onoff)
  1397. *
  1398. * Turn on or off promiscuous mode
  1399. *
  1400. * Input parameters:
  1401. * sc - softc
  1402. * onoff - 1 to turn on, 0 to turn off
  1403. *
  1404. * Return value:
  1405. * nothing
  1406. ********************************************************************* */
  1407. static void sbmac_promiscuous_mode(struct sbmac_softc *sc,int onoff)
  1408. {
  1409. uint64_t reg;
  1410. if (sc->sbm_state != sbmac_state_on)
  1411. return;
  1412. if (onoff) {
  1413. reg = __raw_readq(sc->sbm_rxfilter);
  1414. reg |= M_MAC_ALLPKT_EN;
  1415. __raw_writeq(reg, sc->sbm_rxfilter);
  1416. }
  1417. else {
  1418. reg = __raw_readq(sc->sbm_rxfilter);
  1419. reg &= ~M_MAC_ALLPKT_EN;
  1420. __raw_writeq(reg, sc->sbm_rxfilter);
  1421. }
  1422. }
  1423. /**********************************************************************
  1424. * SBMAC_SETIPHDR_OFFSET(sc,onoff)
  1425. *
  1426. * Set the iphdr offset as 15 assuming ethernet encapsulation
  1427. *
  1428. * Input parameters:
  1429. * sc - softc
  1430. *
  1431. * Return value:
  1432. * nothing
  1433. ********************************************************************* */
  1434. static void sbmac_set_iphdr_offset(struct sbmac_softc *sc)
  1435. {
  1436. uint64_t reg;
  1437. /* Hard code the off set to 15 for now */
  1438. reg = __raw_readq(sc->sbm_rxfilter);
  1439. reg &= ~M_MAC_IPHDR_OFFSET | V_MAC_IPHDR_OFFSET(15);
  1440. __raw_writeq(reg, sc->sbm_rxfilter);
  1441. /* BCM1250 pass1 didn't have hardware checksum. Everything
  1442. later does. */
  1443. if (soc_type == K_SYS_SOC_TYPE_BCM1250 && periph_rev < 2) {
  1444. sc->rx_hw_checksum = DISABLE;
  1445. } else {
  1446. sc->rx_hw_checksum = ENABLE;
  1447. }
  1448. }
  1449. /**********************************************************************
  1450. * SBMAC_ADDR2REG(ptr)
  1451. *
  1452. * Convert six bytes into the 64-bit register value that
  1453. * we typically write into the SBMAC's address/mcast registers
  1454. *
  1455. * Input parameters:
  1456. * ptr - pointer to 6 bytes
  1457. *
  1458. * Return value:
  1459. * register value
  1460. ********************************************************************* */
  1461. static uint64_t sbmac_addr2reg(unsigned char *ptr)
  1462. {
  1463. uint64_t reg = 0;
  1464. ptr += 6;
  1465. reg |= (uint64_t) *(--ptr);
  1466. reg <<= 8;
  1467. reg |= (uint64_t) *(--ptr);
  1468. reg <<= 8;
  1469. reg |= (uint64_t) *(--ptr);
  1470. reg <<= 8;
  1471. reg |= (uint64_t) *(--ptr);
  1472. reg <<= 8;
  1473. reg |= (uint64_t) *(--ptr);
  1474. reg <<= 8;
  1475. reg |= (uint64_t) *(--ptr);
  1476. return reg;
  1477. }
  1478. /**********************************************************************
  1479. * SBMAC_SET_SPEED(s,speed)
  1480. *
  1481. * Configure LAN speed for the specified MAC.
  1482. * Warning: must be called when MAC is off!
  1483. *
  1484. * Input parameters:
  1485. * s - sbmac structure
  1486. * speed - speed to set MAC to (see enum sbmac_speed)
  1487. *
  1488. * Return value:
  1489. * 1 if successful
  1490. * 0 indicates invalid parameters
  1491. ********************************************************************* */
  1492. static int sbmac_set_speed(struct sbmac_softc *s, enum sbmac_speed speed)
  1493. {
  1494. uint64_t cfg;
  1495. uint64_t framecfg;
  1496. /*
  1497. * Save new current values
  1498. */
  1499. s->sbm_speed = speed;
  1500. if (s->sbm_state == sbmac_state_on)
  1501. return 0; /* save for next restart */
  1502. /*
  1503. * Read current register values
  1504. */
  1505. cfg = __raw_readq(s->sbm_maccfg);
  1506. framecfg = __raw_readq(s->sbm_framecfg);
  1507. /*
  1508. * Mask out the stuff we want to change
  1509. */
  1510. cfg &= ~(M_MAC_BURST_EN | M_MAC_SPEED_SEL);
  1511. framecfg &= ~(M_MAC_IFG_RX | M_MAC_IFG_TX | M_MAC_IFG_THRSH |
  1512. M_MAC_SLOT_SIZE);
  1513. /*
  1514. * Now add in the new bits
  1515. */
  1516. switch (speed) {
  1517. case sbmac_speed_10:
  1518. framecfg |= V_MAC_IFG_RX_10 |
  1519. V_MAC_IFG_TX_10 |
  1520. K_MAC_IFG_THRSH_10 |
  1521. V_MAC_SLOT_SIZE_10;
  1522. cfg |= V_MAC_SPEED_SEL_10MBPS;
  1523. break;
  1524. case sbmac_speed_100:
  1525. framecfg |= V_MAC_IFG_RX_100 |
  1526. V_MAC_IFG_TX_100 |
  1527. V_MAC_IFG_THRSH_100 |
  1528. V_MAC_SLOT_SIZE_100;
  1529. cfg |= V_MAC_SPEED_SEL_100MBPS ;
  1530. break;
  1531. case sbmac_speed_1000:
  1532. framecfg |= V_MAC_IFG_RX_1000 |
  1533. V_MAC_IFG_TX_1000 |
  1534. V_MAC_IFG_THRSH_1000 |
  1535. V_MAC_SLOT_SIZE_1000;
  1536. cfg |= V_MAC_SPEED_SEL_1000MBPS | M_MAC_BURST_EN;
  1537. break;
  1538. default:
  1539. return 0;
  1540. }
  1541. /*
  1542. * Send the bits back to the hardware
  1543. */
  1544. __raw_writeq(framecfg, s->sbm_framecfg);
  1545. __raw_writeq(cfg, s->sbm_maccfg);
  1546. return 1;
  1547. }
  1548. /**********************************************************************
  1549. * SBMAC_SET_DUPLEX(s,duplex,fc)
  1550. *
  1551. * Set Ethernet duplex and flow control options for this MAC
  1552. * Warning: must be called when MAC is off!
  1553. *
  1554. * Input parameters:
  1555. * s - sbmac structure
  1556. * duplex - duplex setting (see enum sbmac_duplex)
  1557. * fc - flow control setting (see enum sbmac_fc)
  1558. *
  1559. * Return value:
  1560. * 1 if ok
  1561. * 0 if an invalid parameter combination was specified
  1562. ********************************************************************* */
  1563. static int sbmac_set_duplex(struct sbmac_softc *s, enum sbmac_duplex duplex,
  1564. enum sbmac_fc fc)
  1565. {
  1566. uint64_t cfg;
  1567. /*
  1568. * Save new current values
  1569. */
  1570. s->sbm_duplex = duplex;
  1571. s->sbm_fc = fc;
  1572. if (s->sbm_state == sbmac_state_on)
  1573. return 0; /* save for next restart */
  1574. /*
  1575. * Read current register values
  1576. */
  1577. cfg = __raw_readq(s->sbm_maccfg);
  1578. /*
  1579. * Mask off the stuff we're about to change
  1580. */
  1581. cfg &= ~(M_MAC_FC_SEL | M_MAC_FC_CMD | M_MAC_HDX_EN);
  1582. switch (duplex) {
  1583. case sbmac_duplex_half:
  1584. switch (fc) {
  1585. case sbmac_fc_disabled:
  1586. cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_DISABLED;
  1587. break;
  1588. case sbmac_fc_collision:
  1589. cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_ENABLED;
  1590. break;
  1591. case sbmac_fc_carrier:
  1592. cfg |= M_MAC_HDX_EN | V_MAC_FC_CMD_ENAB_FALSECARR;
  1593. break;
  1594. case sbmac_fc_frame: /* not valid in half duplex */
  1595. default: /* invalid selection */
  1596. return 0;
  1597. }
  1598. break;
  1599. case sbmac_duplex_full:
  1600. switch (fc) {
  1601. case sbmac_fc_disabled:
  1602. cfg |= V_MAC_FC_CMD_DISABLED;
  1603. break;
  1604. case sbmac_fc_frame:
  1605. cfg |= V_MAC_FC_CMD_ENABLED;
  1606. break;
  1607. case sbmac_fc_collision: /* not valid in full duplex */
  1608. case sbmac_fc_carrier: /* not valid in full duplex */
  1609. default:
  1610. return 0;
  1611. }
  1612. break;
  1613. default:
  1614. return 0;
  1615. }
  1616. /*
  1617. * Send the bits back to the hardware
  1618. */
  1619. __raw_writeq(cfg, s->sbm_maccfg);
  1620. return 1;
  1621. }
  1622. /**********************************************************************
  1623. * SBMAC_INTR()
  1624. *
  1625. * Interrupt handler for MAC interrupts
  1626. *
  1627. * Input parameters:
  1628. * MAC structure
  1629. *
  1630. * Return value:
  1631. * nothing
  1632. ********************************************************************* */
  1633. static irqreturn_t sbmac_intr(int irq,void *dev_instance)
  1634. {
  1635. struct net_device *dev = (struct net_device *) dev_instance;
  1636. struct sbmac_softc *sc = netdev_priv(dev);
  1637. uint64_t isr;
  1638. int handled = 0;
  1639. /*
  1640. * Read the ISR (this clears the bits in the real
  1641. * register, except for counter addr)
  1642. */
  1643. isr = __raw_readq(sc->sbm_isr) & ~M_MAC_COUNTER_ADDR;
  1644. if (isr == 0)
  1645. return IRQ_RETVAL(0);
  1646. handled = 1;
  1647. /*
  1648. * Transmits on channel 0
  1649. */
  1650. if (isr & (M_MAC_INT_CHANNEL << S_MAC_TX_CH0))
  1651. sbdma_tx_process(sc,&(sc->sbm_txdma), 0);
  1652. if (isr & (M_MAC_INT_CHANNEL << S_MAC_RX_CH0)) {
  1653. if (napi_schedule_prep(&sc->napi)) {
  1654. __raw_writeq(0, sc->sbm_imr);
  1655. __napi_schedule(&sc->napi);
  1656. /* Depend on the exit from poll to reenable intr */
  1657. }
  1658. else {
  1659. /* may leave some packets behind */
  1660. sbdma_rx_process(sc,&(sc->sbm_rxdma),
  1661. SBMAC_MAX_RXDESCR * 2, 0);
  1662. }
  1663. }
  1664. return IRQ_RETVAL(handled);
  1665. }
  1666. /**********************************************************************
  1667. * SBMAC_START_TX(skb,dev)
  1668. *
  1669. * Start output on the specified interface. Basically, we
  1670. * queue as many buffers as we can until the ring fills up, or
  1671. * we run off the end of the queue, whichever comes first.
  1672. *
  1673. * Input parameters:
  1674. *
  1675. *
  1676. * Return value:
  1677. * nothing
  1678. ********************************************************************* */
  1679. static int sbmac_start_tx(struct sk_buff *skb, struct net_device *dev)
  1680. {
  1681. struct sbmac_softc *sc = netdev_priv(dev);
  1682. unsigned long flags;
  1683. /* lock eth irq */
  1684. spin_lock_irqsave(&sc->sbm_lock, flags);
  1685. /*
  1686. * Put the buffer on the transmit ring. If we
  1687. * don't have room, stop the queue.
  1688. */
  1689. if (sbdma_add_txbuffer(&(sc->sbm_txdma),skb)) {
  1690. /* XXX save skb that we could not send */
  1691. netif_stop_queue(dev);
  1692. spin_unlock_irqrestore(&sc->sbm_lock, flags);
  1693. return NETDEV_TX_BUSY;
  1694. }
  1695. dev->trans_start = jiffies;
  1696. spin_unlock_irqrestore(&sc->sbm_lock, flags);
  1697. return NETDEV_TX_OK;
  1698. }
  1699. /**********************************************************************
  1700. * SBMAC_SETMULTI(sc)
  1701. *
  1702. * Reprogram the multicast table into the hardware, given
  1703. * the list of multicasts associated with the interface
  1704. * structure.
  1705. *
  1706. * Input parameters:
  1707. * sc - softc
  1708. *
  1709. * Return value:
  1710. * nothing
  1711. ********************************************************************* */
  1712. static void sbmac_setmulti(struct sbmac_softc *sc)
  1713. {
  1714. uint64_t reg;
  1715. void __iomem *port;
  1716. int idx;
  1717. struct dev_mc_list *mclist;
  1718. struct net_device *dev = sc->sbm_dev;
  1719. /*
  1720. * Clear out entire multicast table. We do this by nuking
  1721. * the entire hash table and all the direct matches except
  1722. * the first one, which is used for our station address
  1723. */
  1724. for (idx = 1; idx < MAC_ADDR_COUNT; idx++) {
  1725. port = sc->sbm_base + R_MAC_ADDR_BASE+(idx*sizeof(uint64_t));
  1726. __raw_writeq(0, port);
  1727. }
  1728. for (idx = 0; idx < MAC_HASH_COUNT; idx++) {
  1729. port = sc->sbm_base + R_MAC_HASH_BASE+(idx*sizeof(uint64_t));
  1730. __raw_writeq(0, port);
  1731. }
  1732. /*
  1733. * Clear the filter to say we don't want any multicasts.
  1734. */
  1735. reg = __raw_readq(sc->sbm_rxfilter);
  1736. reg &= ~(M_MAC_MCAST_INV | M_MAC_MCAST_EN);
  1737. __raw_writeq(reg, sc->sbm_rxfilter);
  1738. if (dev->flags & IFF_ALLMULTI) {
  1739. /*
  1740. * Enable ALL multicasts. Do this by inverting the
  1741. * multicast enable bit.
  1742. */
  1743. reg = __raw_readq(sc->sbm_rxfilter);
  1744. reg |= (M_MAC_MCAST_INV | M_MAC_MCAST_EN);
  1745. __raw_writeq(reg, sc->sbm_rxfilter);
  1746. return;
  1747. }
  1748. /*
  1749. * Progam new multicast entries. For now, only use the
  1750. * perfect filter. In the future we'll need to use the
  1751. * hash filter if the perfect filter overflows
  1752. */
  1753. /* XXX only using perfect filter for now, need to use hash
  1754. * XXX if the table overflows */
  1755. idx = 1; /* skip station address */
  1756. mclist = dev->mc_list;
  1757. while (mclist && (idx < MAC_ADDR_COUNT)) {
  1758. reg = sbmac_addr2reg(mclist->dmi_addr);
  1759. port = sc->sbm_base + R_MAC_ADDR_BASE+(idx * sizeof(uint64_t));
  1760. __raw_writeq(reg, port);
  1761. idx++;
  1762. mclist = mclist->next;
  1763. }
  1764. /*
  1765. * Enable the "accept multicast bits" if we programmed at least one
  1766. * multicast.
  1767. */
  1768. if (idx > 1) {
  1769. reg = __raw_readq(sc->sbm_rxfilter);
  1770. reg |= M_MAC_MCAST_EN;
  1771. __raw_writeq(reg, sc->sbm_rxfilter);
  1772. }
  1773. }
  1774. #if defined(SBMAC_ETH0_HWADDR) || defined(SBMAC_ETH1_HWADDR) || defined(SBMAC_ETH2_HWADDR) || defined(SBMAC_ETH3_HWADDR)
  1775. /**********************************************************************
  1776. * SBMAC_PARSE_XDIGIT(str)
  1777. *
  1778. * Parse a hex digit, returning its value
  1779. *
  1780. * Input parameters:
  1781. * str - character
  1782. *
  1783. * Return value:
  1784. * hex value, or -1 if invalid
  1785. ********************************************************************* */
  1786. static int sbmac_parse_xdigit(char str)
  1787. {
  1788. int digit;
  1789. if ((str >= '0') && (str <= '9'))
  1790. digit = str - '0';
  1791. else if ((str >= 'a') && (str <= 'f'))
  1792. digit = str - 'a' + 10;
  1793. else if ((str >= 'A') && (str <= 'F'))
  1794. digit = str - 'A' + 10;
  1795. else
  1796. return -1;
  1797. return digit;
  1798. }
  1799. /**********************************************************************
  1800. * SBMAC_PARSE_HWADDR(str,hwaddr)
  1801. *
  1802. * Convert a string in the form xx:xx:xx:xx:xx:xx into a 6-byte
  1803. * Ethernet address.
  1804. *
  1805. * Input parameters:
  1806. * str - string
  1807. * hwaddr - pointer to hardware address
  1808. *
  1809. * Return value:
  1810. * 0 if ok, else -1
  1811. ********************************************************************* */
  1812. static int sbmac_parse_hwaddr(char *str, unsigned char *hwaddr)
  1813. {
  1814. int digit1,digit2;
  1815. int idx = 6;
  1816. while (*str && (idx > 0)) {
  1817. digit1 = sbmac_parse_xdigit(*str);
  1818. if (digit1 < 0)
  1819. return -1;
  1820. str++;
  1821. if (!*str)
  1822. return -1;
  1823. if ((*str == ':') || (*str == '-')) {
  1824. digit2 = digit1;
  1825. digit1 = 0;
  1826. }
  1827. else {
  1828. digit2 = sbmac_parse_xdigit(*str);
  1829. if (digit2 < 0)
  1830. return -1;
  1831. str++;
  1832. }
  1833. *hwaddr++ = (digit1 << 4) | digit2;
  1834. idx--;
  1835. if (*str == '-')
  1836. str++;
  1837. if (*str == ':')
  1838. str++;
  1839. }
  1840. return 0;
  1841. }
  1842. #endif
  1843. static int sb1250_change_mtu(struct net_device *_dev, int new_mtu)
  1844. {
  1845. if (new_mtu > ENET_PACKET_SIZE)
  1846. return -EINVAL;
  1847. _dev->mtu = new_mtu;
  1848. pr_info("changing the mtu to %d\n", new_mtu);
  1849. return 0;
  1850. }
  1851. static const struct net_device_ops sbmac_netdev_ops = {
  1852. .ndo_open = sbmac_open,
  1853. .ndo_stop = sbmac_close,
  1854. .ndo_start_xmit = sbmac_start_tx,
  1855. .ndo_set_multicast_list = sbmac_set_rx_mode,
  1856. .ndo_tx_timeout = sbmac_tx_timeout,
  1857. .ndo_do_ioctl = sbmac_mii_ioctl,
  1858. .ndo_change_mtu = sb1250_change_mtu,
  1859. .ndo_validate_addr = eth_validate_addr,
  1860. .ndo_set_mac_address = eth_mac_addr,
  1861. #ifdef CONFIG_NET_POLL_CONTROLLER
  1862. .ndo_poll_controller = sbmac_netpoll,
  1863. #endif
  1864. };
  1865. /**********************************************************************
  1866. * SBMAC_INIT(dev)
  1867. *
  1868. * Attach routine - init hardware and hook ourselves into linux
  1869. *
  1870. * Input parameters:
  1871. * dev - net_device structure
  1872. *
  1873. * Return value:
  1874. * status
  1875. ********************************************************************* */
  1876. static int sbmac_init(struct platform_device *pldev, long long base)
  1877. {
  1878. struct net_device *dev = dev_get_drvdata(&pldev->dev);
  1879. int idx = pldev->id;
  1880. struct sbmac_softc *sc = netdev_priv(dev);
  1881. unsigned char *eaddr;
  1882. uint64_t ea_reg;
  1883. int i;
  1884. int err;
  1885. sc->sbm_dev = dev;
  1886. sc->sbe_idx = idx;
  1887. eaddr = sc->sbm_hwaddr;
  1888. /*
  1889. * Read the ethernet address. The firmware left this programmed
  1890. * for us in the ethernet address register for each mac.
  1891. */
  1892. ea_reg = __raw_readq(sc->sbm_base + R_MAC_ETHERNET_ADDR);
  1893. __raw_writeq(0, sc->sbm_base + R_MAC_ETHERNET_ADDR);
  1894. for (i = 0; i < 6; i++) {
  1895. eaddr[i] = (uint8_t) (ea_reg & 0xFF);
  1896. ea_reg >>= 8;
  1897. }
  1898. for (i = 0; i < 6; i++) {
  1899. dev->dev_addr[i] = eaddr[i];
  1900. }
  1901. /*
  1902. * Initialize context (get pointers to registers and stuff), then
  1903. * allocate the memory for the descriptor tables.
  1904. */
  1905. sbmac_initctx(sc);
  1906. /*
  1907. * Set up Linux device callins
  1908. */
  1909. spin_lock_init(&(sc->sbm_lock));
  1910. dev->netdev_ops = &sbmac_netdev_ops;
  1911. dev->watchdog_timeo = TX_TIMEOUT;
  1912. netif_napi_add(dev, &sc->napi, sbmac_poll, 16);
  1913. dev->irq = UNIT_INT(idx);
  1914. /* This is needed for PASS2 for Rx H/W checksum feature */
  1915. sbmac_set_iphdr_offset(sc);
  1916. sc->mii_bus = mdiobus_alloc();
  1917. if (sc->mii_bus == NULL) {
  1918. sbmac_uninitctx(sc);
  1919. return -ENOMEM;
  1920. }
  1921. err = register_netdev(dev);
  1922. if (err) {
  1923. printk(KERN_ERR "%s.%d: unable to register netdev\n",
  1924. sbmac_string, idx);
  1925. mdiobus_free(sc->mii_bus);
  1926. sbmac_uninitctx(sc);
  1927. return err;
  1928. }
  1929. pr_info("%s.%d: registered as %s\n", sbmac_string, idx, dev->name);
  1930. if (sc->rx_hw_checksum == ENABLE)
  1931. pr_info("%s: enabling TCP rcv checksum\n", dev->name);
  1932. /*
  1933. * Display Ethernet address (this is called during the config
  1934. * process so we need to finish off the config message that
  1935. * was being displayed)
  1936. */
  1937. pr_info("%s: SiByte Ethernet at 0x%08Lx, address: %pM\n",
  1938. dev->name, base, eaddr);
  1939. sc->mii_bus->name = sbmac_mdio_string;
  1940. snprintf(sc->mii_bus->id, MII_BUS_ID_SIZE, "%x", idx);
  1941. sc->mii_bus->priv = sc;
  1942. sc->mii_bus->read = sbmac_mii_read;
  1943. sc->mii_bus->write = sbmac_mii_write;
  1944. sc->mii_bus->irq = sc->phy_irq;
  1945. for (i = 0; i < PHY_MAX_ADDR; ++i)
  1946. sc->mii_bus->irq[i] = SBMAC_PHY_INT;
  1947. sc->mii_bus->parent = &pldev->dev;
  1948. dev_set_drvdata(&pldev->dev, sc->mii_bus);
  1949. return 0;
  1950. }
  1951. static int sbmac_open(struct net_device *dev)
  1952. {
  1953. struct sbmac_softc *sc = netdev_priv(dev);
  1954. int err;
  1955. if (debug > 1)
  1956. pr_debug("%s: sbmac_open() irq %d.\n", dev->name, dev->irq);
  1957. /*
  1958. * map/route interrupt (clear status first, in case something
  1959. * weird is pending; we haven't initialized the mac registers
  1960. * yet)
  1961. */
  1962. __raw_readq(sc->sbm_isr);
  1963. err = request_irq(dev->irq, sbmac_intr, IRQF_SHARED, dev->name, dev);
  1964. if (err) {
  1965. printk(KERN_ERR "%s: unable to get IRQ %d\n", dev->name,
  1966. dev->irq);
  1967. goto out_err;
  1968. }
  1969. /*
  1970. * Probe PHY address
  1971. */
  1972. err = mdiobus_register(sc->mii_bus);
  1973. if (err) {
  1974. printk(KERN_ERR "%s: unable to register MDIO bus\n",
  1975. dev->name);
  1976. goto out_unirq;
  1977. }
  1978. sc->sbm_speed = sbmac_speed_none;
  1979. sc->sbm_duplex = sbmac_duplex_none;
  1980. sc->sbm_fc = sbmac_fc_none;
  1981. sc->sbm_pause = -1;
  1982. sc->sbm_link = 0;
  1983. /*
  1984. * Attach to the PHY
  1985. */
  1986. err = sbmac_mii_probe(dev);
  1987. if (err)
  1988. goto out_unregister;
  1989. /*
  1990. * Turn on the channel
  1991. */
  1992. sbmac_set_channel_state(sc,sbmac_state_on);
  1993. netif_start_queue(dev);
  1994. sbmac_set_rx_mode(dev);
  1995. phy_start(sc->phy_dev);
  1996. napi_enable(&sc->napi);
  1997. return 0;
  1998. out_unregister:
  1999. mdiobus_unregister(sc->mii_bus);
  2000. out_unirq:
  2001. free_irq(dev->irq, dev);
  2002. out_err:
  2003. return err;
  2004. }
  2005. static int sbmac_mii_probe(struct net_device *dev)
  2006. {
  2007. struct sbmac_softc *sc = netdev_priv(dev);
  2008. struct phy_device *phy_dev;
  2009. int i;
  2010. for (i = 0; i < PHY_MAX_ADDR; i++) {
  2011. phy_dev = sc->mii_bus->phy_map[i];
  2012. if (phy_dev)
  2013. break;
  2014. }
  2015. if (!phy_dev) {
  2016. printk(KERN_ERR "%s: no PHY found\n", dev->name);
  2017. return -ENXIO;
  2018. }
  2019. phy_dev = phy_connect(dev, dev_name(&phy_dev->dev), &sbmac_mii_poll, 0,
  2020. PHY_INTERFACE_MODE_GMII);
  2021. if (IS_ERR(phy_dev)) {
  2022. printk(KERN_ERR "%s: could not attach to PHY\n", dev->name);
  2023. return PTR_ERR(phy_dev);
  2024. }
  2025. /* Remove any features not supported by the controller */
  2026. phy_dev->supported &= SUPPORTED_10baseT_Half |
  2027. SUPPORTED_10baseT_Full |
  2028. SUPPORTED_100baseT_Half |
  2029. SUPPORTED_100baseT_Full |
  2030. SUPPORTED_1000baseT_Half |
  2031. SUPPORTED_1000baseT_Full |
  2032. SUPPORTED_Autoneg |
  2033. SUPPORTED_MII |
  2034. SUPPORTED_Pause |
  2035. SUPPORTED_Asym_Pause;
  2036. phy_dev->advertising = phy_dev->supported;
  2037. pr_info("%s: attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
  2038. dev->name, phy_dev->drv->name,
  2039. dev_name(&phy_dev->dev), phy_dev->irq);
  2040. sc->phy_dev = phy_dev;
  2041. return 0;
  2042. }
  2043. static void sbmac_mii_poll(struct net_device *dev)
  2044. {
  2045. struct sbmac_softc *sc = netdev_priv(dev);
  2046. struct phy_device *phy_dev = sc->phy_dev;
  2047. unsigned long flags;
  2048. enum sbmac_fc fc;
  2049. int link_chg, speed_chg, duplex_chg, pause_chg, fc_chg;
  2050. link_chg = (sc->sbm_link != phy_dev->link);
  2051. speed_chg = (sc->sbm_speed != phy_dev->speed);
  2052. duplex_chg = (sc->sbm_duplex != phy_dev->duplex);
  2053. pause_chg = (sc->sbm_pause != phy_dev->pause);
  2054. if (!link_chg && !speed_chg && !duplex_chg && !pause_chg)
  2055. return; /* Hmmm... */
  2056. if (!phy_dev->link) {
  2057. if (link_chg) {
  2058. sc->sbm_link = phy_dev->link;
  2059. sc->sbm_speed = sbmac_speed_none;
  2060. sc->sbm_duplex = sbmac_duplex_none;
  2061. sc->sbm_fc = sbmac_fc_disabled;
  2062. sc->sbm_pause = -1;
  2063. pr_info("%s: link unavailable\n", dev->name);
  2064. }
  2065. return;
  2066. }
  2067. if (phy_dev->duplex == DUPLEX_FULL) {
  2068. if (phy_dev->pause)
  2069. fc = sbmac_fc_frame;
  2070. else
  2071. fc = sbmac_fc_disabled;
  2072. } else
  2073. fc = sbmac_fc_collision;
  2074. fc_chg = (sc->sbm_fc != fc);
  2075. pr_info("%s: link available: %dbase-%cD\n", dev->name, phy_dev->speed,
  2076. phy_dev->duplex == DUPLEX_FULL ? 'F' : 'H');
  2077. spin_lock_irqsave(&sc->sbm_lock, flags);
  2078. sc->sbm_speed = phy_dev->speed;
  2079. sc->sbm_duplex = phy_dev->duplex;
  2080. sc->sbm_fc = fc;
  2081. sc->sbm_pause = phy_dev->pause;
  2082. sc->sbm_link = phy_dev->link;
  2083. if ((speed_chg || duplex_chg || fc_chg) &&
  2084. sc->sbm_state != sbmac_state_off) {
  2085. /*
  2086. * something changed, restart the channel
  2087. */
  2088. if (debug > 1)
  2089. pr_debug("%s: restarting channel "
  2090. "because PHY state changed\n", dev->name);
  2091. sbmac_channel_stop(sc);
  2092. sbmac_channel_start(sc);
  2093. }
  2094. spin_unlock_irqrestore(&sc->sbm_lock, flags);
  2095. }
  2096. static void sbmac_tx_timeout (struct net_device *dev)
  2097. {
  2098. struct sbmac_softc *sc = netdev_priv(dev);
  2099. unsigned long flags;
  2100. spin_lock_irqsave(&sc->sbm_lock, flags);
  2101. dev->trans_start = jiffies;
  2102. dev->stats.tx_errors++;
  2103. spin_unlock_irqrestore(&sc->sbm_lock, flags);
  2104. printk (KERN_WARNING "%s: Transmit timed out\n",dev->name);
  2105. }
  2106. static void sbmac_set_rx_mode(struct net_device *dev)
  2107. {
  2108. unsigned long flags;
  2109. struct sbmac_softc *sc = netdev_priv(dev);
  2110. spin_lock_irqsave(&sc->sbm_lock, flags);
  2111. if ((dev->flags ^ sc->sbm_devflags) & IFF_PROMISC) {
  2112. /*
  2113. * Promiscuous changed.
  2114. */
  2115. if (dev->flags & IFF_PROMISC) {
  2116. sbmac_promiscuous_mode(sc,1);
  2117. }
  2118. else {
  2119. sbmac_promiscuous_mode(sc,0);
  2120. }
  2121. }
  2122. spin_unlock_irqrestore(&sc->sbm_lock, flags);
  2123. /*
  2124. * Program the multicasts. Do this every time.
  2125. */
  2126. sbmac_setmulti(sc);
  2127. }
  2128. static int sbmac_mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  2129. {
  2130. struct sbmac_softc *sc = netdev_priv(dev);
  2131. if (!netif_running(dev) || !sc->phy_dev)
  2132. return -EINVAL;
  2133. return phy_mii_ioctl(sc->phy_dev, if_mii(rq), cmd);
  2134. }
  2135. static int sbmac_close(struct net_device *dev)
  2136. {
  2137. struct sbmac_softc *sc = netdev_priv(dev);
  2138. napi_disable(&sc->napi);
  2139. phy_stop(sc->phy_dev);
  2140. sbmac_set_channel_state(sc, sbmac_state_off);
  2141. netif_stop_queue(dev);
  2142. if (debug > 1)
  2143. pr_debug("%s: Shutting down ethercard\n", dev->name);
  2144. phy_disconnect(sc->phy_dev);
  2145. sc->phy_dev = NULL;
  2146. mdiobus_unregister(sc->mii_bus);
  2147. free_irq(dev->irq, dev);
  2148. sbdma_emptyring(&(sc->sbm_txdma));
  2149. sbdma_emptyring(&(sc->sbm_rxdma));
  2150. return 0;
  2151. }
  2152. static int sbmac_poll(struct napi_struct *napi, int budget)
  2153. {
  2154. struct sbmac_softc *sc = container_of(napi, struct sbmac_softc, napi);
  2155. struct net_device *dev = sc->sbm_dev;
  2156. int work_done;
  2157. work_done = sbdma_rx_process(sc, &(sc->sbm_rxdma), budget, 1);
  2158. sbdma_tx_process(sc, &(sc->sbm_txdma), 1);
  2159. if (work_done < budget) {
  2160. napi_complete(napi);
  2161. #ifdef CONFIG_SBMAC_COALESCE
  2162. __raw_writeq(((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_TX_CH0) |
  2163. ((M_MAC_INT_EOP_COUNT | M_MAC_INT_EOP_TIMER) << S_MAC_RX_CH0),
  2164. sc->sbm_imr);
  2165. #else
  2166. __raw_writeq((M_MAC_INT_CHANNEL << S_MAC_TX_CH0) |
  2167. (M_MAC_INT_CHANNEL << S_MAC_RX_CH0), sc->sbm_imr);
  2168. #endif
  2169. }
  2170. return work_done;
  2171. }
  2172. static int __devinit sbmac_probe(struct platform_device *pldev)
  2173. {
  2174. struct net_device *dev;
  2175. struct sbmac_softc *sc;
  2176. void __iomem *sbm_base;
  2177. struct resource *res;
  2178. u64 sbmac_orig_hwaddr;
  2179. int err;
  2180. res = platform_get_resource(pldev, IORESOURCE_MEM, 0);
  2181. BUG_ON(!res);
  2182. sbm_base = ioremap_nocache(res->start, res->end - res->start + 1);
  2183. if (!sbm_base) {
  2184. printk(KERN_ERR "%s: unable to map device registers\n",
  2185. dev_name(&pldev->dev));
  2186. err = -ENOMEM;
  2187. goto out_out;
  2188. }
  2189. /*
  2190. * The R_MAC_ETHERNET_ADDR register will be set to some nonzero
  2191. * value for us by the firmware if we're going to use this MAC.
  2192. * If we find a zero, skip this MAC.
  2193. */
  2194. sbmac_orig_hwaddr = __raw_readq(sbm_base + R_MAC_ETHERNET_ADDR);
  2195. pr_debug("%s: %sconfiguring MAC at 0x%08Lx\n", dev_name(&pldev->dev),
  2196. sbmac_orig_hwaddr ? "" : "not ", (long long)res->start);
  2197. if (sbmac_orig_hwaddr == 0) {
  2198. err = 0;
  2199. goto out_unmap;
  2200. }
  2201. /*
  2202. * Okay, cool. Initialize this MAC.
  2203. */
  2204. dev = alloc_etherdev(sizeof(struct sbmac_softc));
  2205. if (!dev) {
  2206. printk(KERN_ERR "%s: unable to allocate etherdev\n",
  2207. dev_name(&pldev->dev));
  2208. err = -ENOMEM;
  2209. goto out_unmap;
  2210. }
  2211. dev_set_drvdata(&pldev->dev, dev);
  2212. SET_NETDEV_DEV(dev, &pldev->dev);
  2213. sc = netdev_priv(dev);
  2214. sc->sbm_base = sbm_base;
  2215. err = sbmac_init(pldev, res->start);
  2216. if (err)
  2217. goto out_kfree;
  2218. return 0;
  2219. out_kfree:
  2220. free_netdev(dev);
  2221. __raw_writeq(sbmac_orig_hwaddr, sbm_base + R_MAC_ETHERNET_ADDR);
  2222. out_unmap:
  2223. iounmap(sbm_base);
  2224. out_out:
  2225. return err;
  2226. }
  2227. static int __exit sbmac_remove(struct platform_device *pldev)
  2228. {
  2229. struct net_device *dev = dev_get_drvdata(&pldev->dev);
  2230. struct sbmac_softc *sc = netdev_priv(dev);
  2231. unregister_netdev(dev);
  2232. sbmac_uninitctx(sc);
  2233. mdiobus_free(sc->mii_bus);
  2234. iounmap(sc->sbm_base);
  2235. free_netdev(dev);
  2236. return 0;
  2237. }
  2238. static struct platform_device **sbmac_pldev;
  2239. static int sbmac_max_units;
  2240. #if defined(SBMAC_ETH0_HWADDR) || defined(SBMAC_ETH1_HWADDR) || defined(SBMAC_ETH2_HWADDR) || defined(SBMAC_ETH3_HWADDR)
  2241. static void __init sbmac_setup_hwaddr(int idx, char *addr)
  2242. {
  2243. void __iomem *sbm_base;
  2244. unsigned long start, end;
  2245. uint8_t eaddr[6];
  2246. uint64_t val;
  2247. if (idx >= sbmac_max_units)
  2248. return;
  2249. start = A_MAC_CHANNEL_BASE(idx);
  2250. end = A_MAC_CHANNEL_BASE(idx + 1) - 1;
  2251. sbm_base = ioremap_nocache(start, end - start + 1);
  2252. if (!sbm_base) {
  2253. printk(KERN_ERR "%s: unable to map device registers\n",
  2254. sbmac_string);
  2255. return;
  2256. }
  2257. sbmac_parse_hwaddr(addr, eaddr);
  2258. val = sbmac_addr2reg(eaddr);
  2259. __raw_writeq(val, sbm_base + R_MAC_ETHERNET_ADDR);
  2260. val = __raw_readq(sbm_base + R_MAC_ETHERNET_ADDR);
  2261. iounmap(sbm_base);
  2262. }
  2263. #endif
  2264. static int __init sbmac_platform_probe_one(int idx)
  2265. {
  2266. struct platform_device *pldev;
  2267. struct {
  2268. struct resource r;
  2269. char name[strlen(sbmac_pretty) + 4];
  2270. } *res;
  2271. int err;
  2272. res = kzalloc(sizeof(*res), GFP_KERNEL);
  2273. if (!res) {
  2274. printk(KERN_ERR "%s.%d: unable to allocate memory\n",
  2275. sbmac_string, idx);
  2276. err = -ENOMEM;
  2277. goto out_err;
  2278. }
  2279. /*
  2280. * This is the base address of the MAC.
  2281. */
  2282. snprintf(res->name, sizeof(res->name), "%s %d", sbmac_pretty, idx);
  2283. res->r.name = res->name;
  2284. res->r.flags = IORESOURCE_MEM;
  2285. res->r.start = A_MAC_CHANNEL_BASE(idx);
  2286. res->r.end = A_MAC_CHANNEL_BASE(idx + 1) - 1;
  2287. pldev = platform_device_register_simple(sbmac_string, idx, &res->r, 1);
  2288. if (IS_ERR(pldev)) {
  2289. printk(KERN_ERR "%s.%d: unable to register platform device\n",
  2290. sbmac_string, idx);
  2291. err = PTR_ERR(pldev);
  2292. goto out_kfree;
  2293. }
  2294. if (!pldev->dev.driver) {
  2295. err = 0; /* No hardware at this address. */
  2296. goto out_unregister;
  2297. }
  2298. sbmac_pldev[idx] = pldev;
  2299. return 0;
  2300. out_unregister:
  2301. platform_device_unregister(pldev);
  2302. out_kfree:
  2303. kfree(res);
  2304. out_err:
  2305. return err;
  2306. }
  2307. static void __init sbmac_platform_probe(void)
  2308. {
  2309. int i;
  2310. /* Set the number of available units based on the SOC type. */
  2311. switch (soc_type) {
  2312. case K_SYS_SOC_TYPE_BCM1250:
  2313. case K_SYS_SOC_TYPE_BCM1250_ALT:
  2314. sbmac_max_units = 3;
  2315. break;
  2316. case K_SYS_SOC_TYPE_BCM1120:
  2317. case K_SYS_SOC_TYPE_BCM1125:
  2318. case K_SYS_SOC_TYPE_BCM1125H:
  2319. case K_SYS_SOC_TYPE_BCM1250_ALT2: /* Hybrid */
  2320. sbmac_max_units = 2;
  2321. break;
  2322. case K_SYS_SOC_TYPE_BCM1x55:
  2323. case K_SYS_SOC_TYPE_BCM1x80:
  2324. sbmac_max_units = 4;
  2325. break;
  2326. default:
  2327. return; /* none */
  2328. }
  2329. /*
  2330. * For bringup when not using the firmware, we can pre-fill
  2331. * the MAC addresses using the environment variables
  2332. * specified in this file (or maybe from the config file?)
  2333. */
  2334. #ifdef SBMAC_ETH0_HWADDR
  2335. sbmac_setup_hwaddr(0, SBMAC_ETH0_HWADDR);
  2336. #endif
  2337. #ifdef SBMAC_ETH1_HWADDR
  2338. sbmac_setup_hwaddr(1, SBMAC_ETH1_HWADDR);
  2339. #endif
  2340. #ifdef SBMAC_ETH2_HWADDR
  2341. sbmac_setup_hwaddr(2, SBMAC_ETH2_HWADDR);
  2342. #endif
  2343. #ifdef SBMAC_ETH3_HWADDR
  2344. sbmac_setup_hwaddr(3, SBMAC_ETH3_HWADDR);
  2345. #endif
  2346. sbmac_pldev = kcalloc(sbmac_max_units, sizeof(*sbmac_pldev),
  2347. GFP_KERNEL);
  2348. if (!sbmac_pldev) {
  2349. printk(KERN_ERR "%s: unable to allocate memory\n",
  2350. sbmac_string);
  2351. return;
  2352. }
  2353. /*
  2354. * Walk through the Ethernet controllers and find
  2355. * those who have their MAC addresses set.
  2356. */
  2357. for (i = 0; i < sbmac_max_units; i++)
  2358. if (sbmac_platform_probe_one(i))
  2359. break;
  2360. }
  2361. static void __exit sbmac_platform_cleanup(void)
  2362. {
  2363. int i;
  2364. for (i = 0; i < sbmac_max_units; i++)
  2365. platform_device_unregister(sbmac_pldev[i]);
  2366. kfree(sbmac_pldev);
  2367. }
  2368. static struct platform_driver sbmac_driver = {
  2369. .probe = sbmac_probe,
  2370. .remove = __exit_p(sbmac_remove),
  2371. .driver = {
  2372. .name = sbmac_string,
  2373. },
  2374. };
  2375. static int __init sbmac_init_module(void)
  2376. {
  2377. int err;
  2378. err = platform_driver_register(&sbmac_driver);
  2379. if (err)
  2380. return err;
  2381. sbmac_platform_probe();
  2382. return err;
  2383. }
  2384. static void __exit sbmac_cleanup_module(void)
  2385. {
  2386. sbmac_platform_cleanup();
  2387. platform_driver_unregister(&sbmac_driver);
  2388. }
  2389. module_init(sbmac_init_module);
  2390. module_exit(sbmac_cleanup_module);