sb1250-mac.c 72 KB

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