sb1250-mac.c 71 KB

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