sb1250-mac.c 70 KB

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