sb1250-mac.c 70 KB

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