sb1250-mac.c 63 KB

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