sb1250-mac.c 74 KB

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