sb1250-mac.c 70 KB

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