sunhme.c 92 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408
  1. /* sunhme.c: Sparc HME/BigMac 10/100baseT half/full duplex auto switching,
  2. * auto carrier detecting ethernet driver. Also known as the
  3. * "Happy Meal Ethernet" found on SunSwift SBUS cards.
  4. *
  5. * Copyright (C) 1996, 1998, 1999, 2002, 2003,
  6. 2006 David S. Miller (davem@davemloft.net)
  7. *
  8. * Changes :
  9. * 2000/11/11 Willy Tarreau <willy AT meta-x.org>
  10. * - port to non-sparc architectures. Tested only on x86 and
  11. * only currently works with QFE PCI cards.
  12. * - ability to specify the MAC address at module load time by passing this
  13. * argument : macaddr=0x00,0x10,0x20,0x30,0x40,0x50
  14. */
  15. #include <linux/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/types.h>
  18. #include <linux/fcntl.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/ioport.h>
  21. #include <linux/in.h>
  22. #include <linux/slab.h>
  23. #include <linux/string.h>
  24. #include <linux/delay.h>
  25. #include <linux/init.h>
  26. #include <linux/ethtool.h>
  27. #include <linux/mii.h>
  28. #include <linux/crc32.h>
  29. #include <linux/random.h>
  30. #include <linux/errno.h>
  31. #include <linux/netdevice.h>
  32. #include <linux/etherdevice.h>
  33. #include <linux/skbuff.h>
  34. #include <linux/mm.h>
  35. #include <linux/bitops.h>
  36. #include <asm/system.h>
  37. #include <asm/io.h>
  38. #include <asm/dma.h>
  39. #include <asm/byteorder.h>
  40. #ifdef CONFIG_SPARC
  41. #include <asm/idprom.h>
  42. #include <asm/sbus.h>
  43. #include <asm/openprom.h>
  44. #include <asm/oplib.h>
  45. #include <asm/prom.h>
  46. #include <asm/auxio.h>
  47. #endif
  48. #include <asm/uaccess.h>
  49. #include <asm/pgtable.h>
  50. #include <asm/irq.h>
  51. #ifdef CONFIG_PCI
  52. #include <linux/pci.h>
  53. #endif
  54. #include "sunhme.h"
  55. #define DRV_NAME "sunhme"
  56. #define DRV_VERSION "3.00"
  57. #define DRV_RELDATE "June 23, 2006"
  58. #define DRV_AUTHOR "David S. Miller (davem@davemloft.net)"
  59. static char version[] =
  60. DRV_NAME ".c:v" DRV_VERSION " " DRV_RELDATE " " DRV_AUTHOR "\n";
  61. MODULE_VERSION(DRV_VERSION);
  62. MODULE_AUTHOR(DRV_AUTHOR);
  63. MODULE_DESCRIPTION("Sun HappyMealEthernet(HME) 10/100baseT ethernet driver");
  64. MODULE_LICENSE("GPL");
  65. static int macaddr[6];
  66. /* accept MAC address of the form macaddr=0x08,0x00,0x20,0x30,0x40,0x50 */
  67. module_param_array(macaddr, int, NULL, 0);
  68. MODULE_PARM_DESC(macaddr, "Happy Meal MAC address to set");
  69. #ifdef CONFIG_SBUS
  70. static struct quattro *qfe_sbus_list;
  71. #endif
  72. #ifdef CONFIG_PCI
  73. static struct quattro *qfe_pci_list;
  74. #endif
  75. #undef HMEDEBUG
  76. #undef SXDEBUG
  77. #undef RXDEBUG
  78. #undef TXDEBUG
  79. #undef TXLOGGING
  80. #ifdef TXLOGGING
  81. struct hme_tx_logent {
  82. unsigned int tstamp;
  83. int tx_new, tx_old;
  84. unsigned int action;
  85. #define TXLOG_ACTION_IRQ 0x01
  86. #define TXLOG_ACTION_TXMIT 0x02
  87. #define TXLOG_ACTION_TBUSY 0x04
  88. #define TXLOG_ACTION_NBUFS 0x08
  89. unsigned int status;
  90. };
  91. #define TX_LOG_LEN 128
  92. static struct hme_tx_logent tx_log[TX_LOG_LEN];
  93. static int txlog_cur_entry;
  94. static __inline__ void tx_add_log(struct happy_meal *hp, unsigned int a, unsigned int s)
  95. {
  96. struct hme_tx_logent *tlp;
  97. unsigned long flags;
  98. save_and_cli(flags);
  99. tlp = &tx_log[txlog_cur_entry];
  100. tlp->tstamp = (unsigned int)jiffies;
  101. tlp->tx_new = hp->tx_new;
  102. tlp->tx_old = hp->tx_old;
  103. tlp->action = a;
  104. tlp->status = s;
  105. txlog_cur_entry = (txlog_cur_entry + 1) & (TX_LOG_LEN - 1);
  106. restore_flags(flags);
  107. }
  108. static __inline__ void tx_dump_log(void)
  109. {
  110. int i, this;
  111. this = txlog_cur_entry;
  112. for (i = 0; i < TX_LOG_LEN; i++) {
  113. printk("TXLOG[%d]: j[%08x] tx[N(%d)O(%d)] action[%08x] stat[%08x]\n", i,
  114. tx_log[this].tstamp,
  115. tx_log[this].tx_new, tx_log[this].tx_old,
  116. tx_log[this].action, tx_log[this].status);
  117. this = (this + 1) & (TX_LOG_LEN - 1);
  118. }
  119. }
  120. static __inline__ void tx_dump_ring(struct happy_meal *hp)
  121. {
  122. struct hmeal_init_block *hb = hp->happy_block;
  123. struct happy_meal_txd *tp = &hb->happy_meal_txd[0];
  124. int i;
  125. for (i = 0; i < TX_RING_SIZE; i+=4) {
  126. printk("TXD[%d..%d]: [%08x:%08x] [%08x:%08x] [%08x:%08x] [%08x:%08x]\n",
  127. i, i + 4,
  128. le32_to_cpu(tp[i].tx_flags), le32_to_cpu(tp[i].tx_addr),
  129. le32_to_cpu(tp[i + 1].tx_flags), le32_to_cpu(tp[i + 1].tx_addr),
  130. le32_to_cpu(tp[i + 2].tx_flags), le32_to_cpu(tp[i + 2].tx_addr),
  131. le32_to_cpu(tp[i + 3].tx_flags), le32_to_cpu(tp[i + 3].tx_addr));
  132. }
  133. }
  134. #else
  135. #define tx_add_log(hp, a, s) do { } while(0)
  136. #define tx_dump_log() do { } while(0)
  137. #define tx_dump_ring(hp) do { } while(0)
  138. #endif
  139. #ifdef HMEDEBUG
  140. #define HMD(x) printk x
  141. #else
  142. #define HMD(x)
  143. #endif
  144. /* #define AUTO_SWITCH_DEBUG */
  145. #ifdef AUTO_SWITCH_DEBUG
  146. #define ASD(x) printk x
  147. #else
  148. #define ASD(x)
  149. #endif
  150. #define DEFAULT_IPG0 16 /* For lance-mode only */
  151. #define DEFAULT_IPG1 8 /* For all modes */
  152. #define DEFAULT_IPG2 4 /* For all modes */
  153. #define DEFAULT_JAMSIZE 4 /* Toe jam */
  154. /* NOTE: In the descriptor writes one _must_ write the address
  155. * member _first_. The card must not be allowed to see
  156. * the updated descriptor flags until the address is
  157. * correct. I've added a write memory barrier between
  158. * the two stores so that I can sleep well at night... -DaveM
  159. */
  160. #if defined(CONFIG_SBUS) && defined(CONFIG_PCI)
  161. static void sbus_hme_write32(void __iomem *reg, u32 val)
  162. {
  163. sbus_writel(val, reg);
  164. }
  165. static u32 sbus_hme_read32(void __iomem *reg)
  166. {
  167. return sbus_readl(reg);
  168. }
  169. static void sbus_hme_write_rxd(struct happy_meal_rxd *rxd, u32 flags, u32 addr)
  170. {
  171. rxd->rx_addr = (__force hme32)addr;
  172. wmb();
  173. rxd->rx_flags = (__force hme32)flags;
  174. }
  175. static void sbus_hme_write_txd(struct happy_meal_txd *txd, u32 flags, u32 addr)
  176. {
  177. txd->tx_addr = (__force hme32)addr;
  178. wmb();
  179. txd->tx_flags = (__force hme32)flags;
  180. }
  181. static u32 sbus_hme_read_desc32(hme32 *p)
  182. {
  183. return (__force u32)*p;
  184. }
  185. static void pci_hme_write32(void __iomem *reg, u32 val)
  186. {
  187. writel(val, reg);
  188. }
  189. static u32 pci_hme_read32(void __iomem *reg)
  190. {
  191. return readl(reg);
  192. }
  193. static void pci_hme_write_rxd(struct happy_meal_rxd *rxd, u32 flags, u32 addr)
  194. {
  195. rxd->rx_addr = (__force hme32)cpu_to_le32(addr);
  196. wmb();
  197. rxd->rx_flags = (__force hme32)cpu_to_le32(flags);
  198. }
  199. static void pci_hme_write_txd(struct happy_meal_txd *txd, u32 flags, u32 addr)
  200. {
  201. txd->tx_addr = (__force hme32)cpu_to_le32(addr);
  202. wmb();
  203. txd->tx_flags = (__force hme32)cpu_to_le32(flags);
  204. }
  205. static u32 pci_hme_read_desc32(hme32 *p)
  206. {
  207. return le32_to_cpup((__le32 *)p);
  208. }
  209. #define hme_write32(__hp, __reg, __val) \
  210. ((__hp)->write32((__reg), (__val)))
  211. #define hme_read32(__hp, __reg) \
  212. ((__hp)->read32(__reg))
  213. #define hme_write_rxd(__hp, __rxd, __flags, __addr) \
  214. ((__hp)->write_rxd((__rxd), (__flags), (__addr)))
  215. #define hme_write_txd(__hp, __txd, __flags, __addr) \
  216. ((__hp)->write_txd((__txd), (__flags), (__addr)))
  217. #define hme_read_desc32(__hp, __p) \
  218. ((__hp)->read_desc32(__p))
  219. #define hme_dma_map(__hp, __ptr, __size, __dir) \
  220. ((__hp)->dma_map((__hp)->happy_dev, (__ptr), (__size), (__dir)))
  221. #define hme_dma_unmap(__hp, __addr, __size, __dir) \
  222. ((__hp)->dma_unmap((__hp)->happy_dev, (__addr), (__size), (__dir)))
  223. #define hme_dma_sync_for_cpu(__hp, __addr, __size, __dir) \
  224. ((__hp)->dma_sync_for_cpu((__hp)->happy_dev, (__addr), (__size), (__dir)))
  225. #define hme_dma_sync_for_device(__hp, __addr, __size, __dir) \
  226. ((__hp)->dma_sync_for_device((__hp)->happy_dev, (__addr), (__size), (__dir)))
  227. #else
  228. #ifdef CONFIG_SBUS
  229. /* SBUS only compilation */
  230. #define hme_write32(__hp, __reg, __val) \
  231. sbus_writel((__val), (__reg))
  232. #define hme_read32(__hp, __reg) \
  233. sbus_readl(__reg)
  234. #define hme_write_rxd(__hp, __rxd, __flags, __addr) \
  235. do { (__rxd)->rx_addr = (__force hme32)(u32)(__addr); \
  236. wmb(); \
  237. (__rxd)->rx_flags = (__force hme32)(u32)(__flags); \
  238. } while(0)
  239. #define hme_write_txd(__hp, __txd, __flags, __addr) \
  240. do { (__txd)->tx_addr = (__force hme32)(u32)(__addr); \
  241. wmb(); \
  242. (__txd)->tx_flags = (__force hme32)(u32)(__flags); \
  243. } while(0)
  244. #define hme_read_desc32(__hp, __p) ((__force u32)(hme32)*(__p))
  245. #define hme_dma_map(__hp, __ptr, __size, __dir) \
  246. sbus_map_single((__hp)->happy_dev, (__ptr), (__size), (__dir))
  247. #define hme_dma_unmap(__hp, __addr, __size, __dir) \
  248. sbus_unmap_single((__hp)->happy_dev, (__addr), (__size), (__dir))
  249. #define hme_dma_sync_for_cpu(__hp, __addr, __size, __dir) \
  250. sbus_dma_sync_single_for_cpu((__hp)->happy_dev, (__addr), (__size), (__dir))
  251. #define hme_dma_sync_for_device(__hp, __addr, __size, __dir) \
  252. sbus_dma_sync_single_for_device((__hp)->happy_dev, (__addr), (__size), (__dir))
  253. #else
  254. /* PCI only compilation */
  255. #define hme_write32(__hp, __reg, __val) \
  256. writel((__val), (__reg))
  257. #define hme_read32(__hp, __reg) \
  258. readl(__reg)
  259. #define hme_write_rxd(__hp, __rxd, __flags, __addr) \
  260. do { (__rxd)->rx_addr = (__force hme32)cpu_to_le32(__addr); \
  261. wmb(); \
  262. (__rxd)->rx_flags = (__force hme32)cpu_to_le32(__flags); \
  263. } while(0)
  264. #define hme_write_txd(__hp, __txd, __flags, __addr) \
  265. do { (__txd)->tx_addr = (__force hme32)cpu_to_le32(__addr); \
  266. wmb(); \
  267. (__txd)->tx_flags = (__force hme32)cpu_to_le32(__flags); \
  268. } while(0)
  269. static inline u32 hme_read_desc32(struct happy_meal *hp, hme32 *p)
  270. {
  271. return le32_to_cpup((__le32 *)p);
  272. }
  273. #define hme_dma_map(__hp, __ptr, __size, __dir) \
  274. pci_map_single((__hp)->happy_dev, (__ptr), (__size), (__dir))
  275. #define hme_dma_unmap(__hp, __addr, __size, __dir) \
  276. pci_unmap_single((__hp)->happy_dev, (__addr), (__size), (__dir))
  277. #define hme_dma_sync_for_cpu(__hp, __addr, __size, __dir) \
  278. pci_dma_sync_single_for_cpu((__hp)->happy_dev, (__addr), (__size), (__dir))
  279. #define hme_dma_sync_for_device(__hp, __addr, __size, __dir) \
  280. pci_dma_sync_single_for_device((__hp)->happy_dev, (__addr), (__size), (__dir))
  281. #endif
  282. #endif
  283. #ifdef SBUS_DMA_BIDIRECTIONAL
  284. # define DMA_BIDIRECTIONAL SBUS_DMA_BIDIRECTIONAL
  285. #else
  286. # define DMA_BIDIRECTIONAL 0
  287. #endif
  288. #ifdef SBUS_DMA_FROMDEVICE
  289. # define DMA_FROMDEVICE SBUS_DMA_FROMDEVICE
  290. #else
  291. # define DMA_TODEVICE 1
  292. #endif
  293. #ifdef SBUS_DMA_TODEVICE
  294. # define DMA_TODEVICE SBUS_DMA_TODEVICE
  295. #else
  296. # define DMA_FROMDEVICE 2
  297. #endif
  298. /* Oh yes, the MIF BitBang is mighty fun to program. BitBucket is more like it. */
  299. static void BB_PUT_BIT(struct happy_meal *hp, void __iomem *tregs, int bit)
  300. {
  301. hme_write32(hp, tregs + TCVR_BBDATA, bit);
  302. hme_write32(hp, tregs + TCVR_BBCLOCK, 0);
  303. hme_write32(hp, tregs + TCVR_BBCLOCK, 1);
  304. }
  305. #if 0
  306. static u32 BB_GET_BIT(struct happy_meal *hp, void __iomem *tregs, int internal)
  307. {
  308. u32 ret;
  309. hme_write32(hp, tregs + TCVR_BBCLOCK, 0);
  310. hme_write32(hp, tregs + TCVR_BBCLOCK, 1);
  311. ret = hme_read32(hp, tregs + TCVR_CFG);
  312. if (internal)
  313. ret &= TCV_CFG_MDIO0;
  314. else
  315. ret &= TCV_CFG_MDIO1;
  316. return ret;
  317. }
  318. #endif
  319. static u32 BB_GET_BIT2(struct happy_meal *hp, void __iomem *tregs, int internal)
  320. {
  321. u32 retval;
  322. hme_write32(hp, tregs + TCVR_BBCLOCK, 0);
  323. udelay(1);
  324. retval = hme_read32(hp, tregs + TCVR_CFG);
  325. if (internal)
  326. retval &= TCV_CFG_MDIO0;
  327. else
  328. retval &= TCV_CFG_MDIO1;
  329. hme_write32(hp, tregs + TCVR_BBCLOCK, 1);
  330. return retval;
  331. }
  332. #define TCVR_FAILURE 0x80000000 /* Impossible MIF read value */
  333. static int happy_meal_bb_read(struct happy_meal *hp,
  334. void __iomem *tregs, int reg)
  335. {
  336. u32 tmp;
  337. int retval = 0;
  338. int i;
  339. ASD(("happy_meal_bb_read: reg=%d ", reg));
  340. /* Enable the MIF BitBang outputs. */
  341. hme_write32(hp, tregs + TCVR_BBOENAB, 1);
  342. /* Force BitBang into the idle state. */
  343. for (i = 0; i < 32; i++)
  344. BB_PUT_BIT(hp, tregs, 1);
  345. /* Give it the read sequence. */
  346. BB_PUT_BIT(hp, tregs, 0);
  347. BB_PUT_BIT(hp, tregs, 1);
  348. BB_PUT_BIT(hp, tregs, 1);
  349. BB_PUT_BIT(hp, tregs, 0);
  350. /* Give it the PHY address. */
  351. tmp = hp->paddr & 0xff;
  352. for (i = 4; i >= 0; i--)
  353. BB_PUT_BIT(hp, tregs, ((tmp >> i) & 1));
  354. /* Tell it what register we want to read. */
  355. tmp = (reg & 0xff);
  356. for (i = 4; i >= 0; i--)
  357. BB_PUT_BIT(hp, tregs, ((tmp >> i) & 1));
  358. /* Close down the MIF BitBang outputs. */
  359. hme_write32(hp, tregs + TCVR_BBOENAB, 0);
  360. /* Now read in the value. */
  361. (void) BB_GET_BIT2(hp, tregs, (hp->tcvr_type == internal));
  362. for (i = 15; i >= 0; i--)
  363. retval |= BB_GET_BIT2(hp, tregs, (hp->tcvr_type == internal));
  364. (void) BB_GET_BIT2(hp, tregs, (hp->tcvr_type == internal));
  365. (void) BB_GET_BIT2(hp, tregs, (hp->tcvr_type == internal));
  366. (void) BB_GET_BIT2(hp, tregs, (hp->tcvr_type == internal));
  367. ASD(("value=%x\n", retval));
  368. return retval;
  369. }
  370. static void happy_meal_bb_write(struct happy_meal *hp,
  371. void __iomem *tregs, int reg,
  372. unsigned short value)
  373. {
  374. u32 tmp;
  375. int i;
  376. ASD(("happy_meal_bb_write: reg=%d value=%x\n", reg, value));
  377. /* Enable the MIF BitBang outputs. */
  378. hme_write32(hp, tregs + TCVR_BBOENAB, 1);
  379. /* Force BitBang into the idle state. */
  380. for (i = 0; i < 32; i++)
  381. BB_PUT_BIT(hp, tregs, 1);
  382. /* Give it write sequence. */
  383. BB_PUT_BIT(hp, tregs, 0);
  384. BB_PUT_BIT(hp, tregs, 1);
  385. BB_PUT_BIT(hp, tregs, 0);
  386. BB_PUT_BIT(hp, tregs, 1);
  387. /* Give it the PHY address. */
  388. tmp = (hp->paddr & 0xff);
  389. for (i = 4; i >= 0; i--)
  390. BB_PUT_BIT(hp, tregs, ((tmp >> i) & 1));
  391. /* Tell it what register we will be writing. */
  392. tmp = (reg & 0xff);
  393. for (i = 4; i >= 0; i--)
  394. BB_PUT_BIT(hp, tregs, ((tmp >> i) & 1));
  395. /* Tell it to become ready for the bits. */
  396. BB_PUT_BIT(hp, tregs, 1);
  397. BB_PUT_BIT(hp, tregs, 0);
  398. for (i = 15; i >= 0; i--)
  399. BB_PUT_BIT(hp, tregs, ((value >> i) & 1));
  400. /* Close down the MIF BitBang outputs. */
  401. hme_write32(hp, tregs + TCVR_BBOENAB, 0);
  402. }
  403. #define TCVR_READ_TRIES 16
  404. static int happy_meal_tcvr_read(struct happy_meal *hp,
  405. void __iomem *tregs, int reg)
  406. {
  407. int tries = TCVR_READ_TRIES;
  408. int retval;
  409. ASD(("happy_meal_tcvr_read: reg=0x%02x ", reg));
  410. if (hp->tcvr_type == none) {
  411. ASD(("no transceiver, value=TCVR_FAILURE\n"));
  412. return TCVR_FAILURE;
  413. }
  414. if (!(hp->happy_flags & HFLAG_FENABLE)) {
  415. ASD(("doing bit bang\n"));
  416. return happy_meal_bb_read(hp, tregs, reg);
  417. }
  418. hme_write32(hp, tregs + TCVR_FRAME,
  419. (FRAME_READ | (hp->paddr << 23) | ((reg & 0xff) << 18)));
  420. while (!(hme_read32(hp, tregs + TCVR_FRAME) & 0x10000) && --tries)
  421. udelay(20);
  422. if (!tries) {
  423. printk(KERN_ERR "happy meal: Aieee, transceiver MIF read bolixed\n");
  424. return TCVR_FAILURE;
  425. }
  426. retval = hme_read32(hp, tregs + TCVR_FRAME) & 0xffff;
  427. ASD(("value=%04x\n", retval));
  428. return retval;
  429. }
  430. #define TCVR_WRITE_TRIES 16
  431. static void happy_meal_tcvr_write(struct happy_meal *hp,
  432. void __iomem *tregs, int reg,
  433. unsigned short value)
  434. {
  435. int tries = TCVR_WRITE_TRIES;
  436. ASD(("happy_meal_tcvr_write: reg=0x%02x value=%04x\n", reg, value));
  437. /* Welcome to Sun Microsystems, can I take your order please? */
  438. if (!(hp->happy_flags & HFLAG_FENABLE)) {
  439. happy_meal_bb_write(hp, tregs, reg, value);
  440. return;
  441. }
  442. /* Would you like fries with that? */
  443. hme_write32(hp, tregs + TCVR_FRAME,
  444. (FRAME_WRITE | (hp->paddr << 23) |
  445. ((reg & 0xff) << 18) | (value & 0xffff)));
  446. while (!(hme_read32(hp, tregs + TCVR_FRAME) & 0x10000) && --tries)
  447. udelay(20);
  448. /* Anything else? */
  449. if (!tries)
  450. printk(KERN_ERR "happy meal: Aieee, transceiver MIF write bolixed\n");
  451. /* Fifty-two cents is your change, have a nice day. */
  452. }
  453. /* Auto negotiation. The scheme is very simple. We have a timer routine
  454. * that keeps watching the auto negotiation process as it progresses.
  455. * The DP83840 is first told to start doing it's thing, we set up the time
  456. * and place the timer state machine in it's initial state.
  457. *
  458. * Here the timer peeks at the DP83840 status registers at each click to see
  459. * if the auto negotiation has completed, we assume here that the DP83840 PHY
  460. * will time out at some point and just tell us what (didn't) happen. For
  461. * complete coverage we only allow so many of the ticks at this level to run,
  462. * when this has expired we print a warning message and try another strategy.
  463. * This "other" strategy is to force the interface into various speed/duplex
  464. * configurations and we stop when we see a link-up condition before the
  465. * maximum number of "peek" ticks have occurred.
  466. *
  467. * Once a valid link status has been detected we configure the BigMAC and
  468. * the rest of the Happy Meal to speak the most efficient protocol we could
  469. * get a clean link for. The priority for link configurations, highest first
  470. * is:
  471. * 100 Base-T Full Duplex
  472. * 100 Base-T Half Duplex
  473. * 10 Base-T Full Duplex
  474. * 10 Base-T Half Duplex
  475. *
  476. * We start a new timer now, after a successful auto negotiation status has
  477. * been detected. This timer just waits for the link-up bit to get set in
  478. * the BMCR of the DP83840. When this occurs we print a kernel log message
  479. * describing the link type in use and the fact that it is up.
  480. *
  481. * If a fatal error of some sort is signalled and detected in the interrupt
  482. * service routine, and the chip is reset, or the link is ifconfig'd down
  483. * and then back up, this entire process repeats itself all over again.
  484. */
  485. static int try_next_permutation(struct happy_meal *hp, void __iomem *tregs)
  486. {
  487. hp->sw_bmcr = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
  488. /* Downgrade from full to half duplex. Only possible
  489. * via ethtool.
  490. */
  491. if (hp->sw_bmcr & BMCR_FULLDPLX) {
  492. hp->sw_bmcr &= ~(BMCR_FULLDPLX);
  493. happy_meal_tcvr_write(hp, tregs, MII_BMCR, hp->sw_bmcr);
  494. return 0;
  495. }
  496. /* Downgrade from 100 to 10. */
  497. if (hp->sw_bmcr & BMCR_SPEED100) {
  498. hp->sw_bmcr &= ~(BMCR_SPEED100);
  499. happy_meal_tcvr_write(hp, tregs, MII_BMCR, hp->sw_bmcr);
  500. return 0;
  501. }
  502. /* We've tried everything. */
  503. return -1;
  504. }
  505. static void display_link_mode(struct happy_meal *hp, void __iomem *tregs)
  506. {
  507. printk(KERN_INFO "%s: Link is up using ", hp->dev->name);
  508. if (hp->tcvr_type == external)
  509. printk("external ");
  510. else
  511. printk("internal ");
  512. printk("transceiver at ");
  513. hp->sw_lpa = happy_meal_tcvr_read(hp, tregs, MII_LPA);
  514. if (hp->sw_lpa & (LPA_100HALF | LPA_100FULL)) {
  515. if (hp->sw_lpa & LPA_100FULL)
  516. printk("100Mb/s, Full Duplex.\n");
  517. else
  518. printk("100Mb/s, Half Duplex.\n");
  519. } else {
  520. if (hp->sw_lpa & LPA_10FULL)
  521. printk("10Mb/s, Full Duplex.\n");
  522. else
  523. printk("10Mb/s, Half Duplex.\n");
  524. }
  525. }
  526. static void display_forced_link_mode(struct happy_meal *hp, void __iomem *tregs)
  527. {
  528. printk(KERN_INFO "%s: Link has been forced up using ", hp->dev->name);
  529. if (hp->tcvr_type == external)
  530. printk("external ");
  531. else
  532. printk("internal ");
  533. printk("transceiver at ");
  534. hp->sw_bmcr = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
  535. if (hp->sw_bmcr & BMCR_SPEED100)
  536. printk("100Mb/s, ");
  537. else
  538. printk("10Mb/s, ");
  539. if (hp->sw_bmcr & BMCR_FULLDPLX)
  540. printk("Full Duplex.\n");
  541. else
  542. printk("Half Duplex.\n");
  543. }
  544. static int set_happy_link_modes(struct happy_meal *hp, void __iomem *tregs)
  545. {
  546. int full;
  547. /* All we care about is making sure the bigmac tx_cfg has a
  548. * proper duplex setting.
  549. */
  550. if (hp->timer_state == arbwait) {
  551. hp->sw_lpa = happy_meal_tcvr_read(hp, tregs, MII_LPA);
  552. if (!(hp->sw_lpa & (LPA_10HALF | LPA_10FULL | LPA_100HALF | LPA_100FULL)))
  553. goto no_response;
  554. if (hp->sw_lpa & LPA_100FULL)
  555. full = 1;
  556. else if (hp->sw_lpa & LPA_100HALF)
  557. full = 0;
  558. else if (hp->sw_lpa & LPA_10FULL)
  559. full = 1;
  560. else
  561. full = 0;
  562. } else {
  563. /* Forcing a link mode. */
  564. hp->sw_bmcr = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
  565. if (hp->sw_bmcr & BMCR_FULLDPLX)
  566. full = 1;
  567. else
  568. full = 0;
  569. }
  570. /* Before changing other bits in the tx_cfg register, and in
  571. * general any of other the TX config registers too, you
  572. * must:
  573. * 1) Clear Enable
  574. * 2) Poll with reads until that bit reads back as zero
  575. * 3) Make TX configuration changes
  576. * 4) Set Enable once more
  577. */
  578. hme_write32(hp, hp->bigmacregs + BMAC_TXCFG,
  579. hme_read32(hp, hp->bigmacregs + BMAC_TXCFG) &
  580. ~(BIGMAC_TXCFG_ENABLE));
  581. while (hme_read32(hp, hp->bigmacregs + BMAC_TXCFG) & BIGMAC_TXCFG_ENABLE)
  582. barrier();
  583. if (full) {
  584. hp->happy_flags |= HFLAG_FULL;
  585. hme_write32(hp, hp->bigmacregs + BMAC_TXCFG,
  586. hme_read32(hp, hp->bigmacregs + BMAC_TXCFG) |
  587. BIGMAC_TXCFG_FULLDPLX);
  588. } else {
  589. hp->happy_flags &= ~(HFLAG_FULL);
  590. hme_write32(hp, hp->bigmacregs + BMAC_TXCFG,
  591. hme_read32(hp, hp->bigmacregs + BMAC_TXCFG) &
  592. ~(BIGMAC_TXCFG_FULLDPLX));
  593. }
  594. hme_write32(hp, hp->bigmacregs + BMAC_TXCFG,
  595. hme_read32(hp, hp->bigmacregs + BMAC_TXCFG) |
  596. BIGMAC_TXCFG_ENABLE);
  597. return 0;
  598. no_response:
  599. return 1;
  600. }
  601. static int happy_meal_init(struct happy_meal *hp);
  602. static int is_lucent_phy(struct happy_meal *hp)
  603. {
  604. void __iomem *tregs = hp->tcvregs;
  605. unsigned short mr2, mr3;
  606. int ret = 0;
  607. mr2 = happy_meal_tcvr_read(hp, tregs, 2);
  608. mr3 = happy_meal_tcvr_read(hp, tregs, 3);
  609. if ((mr2 & 0xffff) == 0x0180 &&
  610. ((mr3 & 0xffff) >> 10) == 0x1d)
  611. ret = 1;
  612. return ret;
  613. }
  614. static void happy_meal_timer(unsigned long data)
  615. {
  616. struct happy_meal *hp = (struct happy_meal *) data;
  617. void __iomem *tregs = hp->tcvregs;
  618. int restart_timer = 0;
  619. spin_lock_irq(&hp->happy_lock);
  620. hp->timer_ticks++;
  621. switch(hp->timer_state) {
  622. case arbwait:
  623. /* Only allow for 5 ticks, thats 10 seconds and much too
  624. * long to wait for arbitration to complete.
  625. */
  626. if (hp->timer_ticks >= 10) {
  627. /* Enter force mode. */
  628. do_force_mode:
  629. hp->sw_bmcr = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
  630. printk(KERN_NOTICE "%s: Auto-Negotiation unsuccessful, trying force link mode\n",
  631. hp->dev->name);
  632. hp->sw_bmcr = BMCR_SPEED100;
  633. happy_meal_tcvr_write(hp, tregs, MII_BMCR, hp->sw_bmcr);
  634. if (!is_lucent_phy(hp)) {
  635. /* OK, seems we need do disable the transceiver for the first
  636. * tick to make sure we get an accurate link state at the
  637. * second tick.
  638. */
  639. hp->sw_csconfig = happy_meal_tcvr_read(hp, tregs, DP83840_CSCONFIG);
  640. hp->sw_csconfig &= ~(CSCONFIG_TCVDISAB);
  641. happy_meal_tcvr_write(hp, tregs, DP83840_CSCONFIG, hp->sw_csconfig);
  642. }
  643. hp->timer_state = ltrywait;
  644. hp->timer_ticks = 0;
  645. restart_timer = 1;
  646. } else {
  647. /* Anything interesting happen? */
  648. hp->sw_bmsr = happy_meal_tcvr_read(hp, tregs, MII_BMSR);
  649. if (hp->sw_bmsr & BMSR_ANEGCOMPLETE) {
  650. int ret;
  651. /* Just what we've been waiting for... */
  652. ret = set_happy_link_modes(hp, tregs);
  653. if (ret) {
  654. /* Ooops, something bad happened, go to force
  655. * mode.
  656. *
  657. * XXX Broken hubs which don't support 802.3u
  658. * XXX auto-negotiation make this happen as well.
  659. */
  660. goto do_force_mode;
  661. }
  662. /* Success, at least so far, advance our state engine. */
  663. hp->timer_state = lupwait;
  664. restart_timer = 1;
  665. } else {
  666. restart_timer = 1;
  667. }
  668. }
  669. break;
  670. case lupwait:
  671. /* Auto negotiation was successful and we are awaiting a
  672. * link up status. I have decided to let this timer run
  673. * forever until some sort of error is signalled, reporting
  674. * a message to the user at 10 second intervals.
  675. */
  676. hp->sw_bmsr = happy_meal_tcvr_read(hp, tregs, MII_BMSR);
  677. if (hp->sw_bmsr & BMSR_LSTATUS) {
  678. /* Wheee, it's up, display the link mode in use and put
  679. * the timer to sleep.
  680. */
  681. display_link_mode(hp, tregs);
  682. hp->timer_state = asleep;
  683. restart_timer = 0;
  684. } else {
  685. if (hp->timer_ticks >= 10) {
  686. printk(KERN_NOTICE "%s: Auto negotiation successful, link still "
  687. "not completely up.\n", hp->dev->name);
  688. hp->timer_ticks = 0;
  689. restart_timer = 1;
  690. } else {
  691. restart_timer = 1;
  692. }
  693. }
  694. break;
  695. case ltrywait:
  696. /* Making the timeout here too long can make it take
  697. * annoyingly long to attempt all of the link mode
  698. * permutations, but then again this is essentially
  699. * error recovery code for the most part.
  700. */
  701. hp->sw_bmsr = happy_meal_tcvr_read(hp, tregs, MII_BMSR);
  702. hp->sw_csconfig = happy_meal_tcvr_read(hp, tregs, DP83840_CSCONFIG);
  703. if (hp->timer_ticks == 1) {
  704. if (!is_lucent_phy(hp)) {
  705. /* Re-enable transceiver, we'll re-enable the transceiver next
  706. * tick, then check link state on the following tick.
  707. */
  708. hp->sw_csconfig |= CSCONFIG_TCVDISAB;
  709. happy_meal_tcvr_write(hp, tregs,
  710. DP83840_CSCONFIG, hp->sw_csconfig);
  711. }
  712. restart_timer = 1;
  713. break;
  714. }
  715. if (hp->timer_ticks == 2) {
  716. if (!is_lucent_phy(hp)) {
  717. hp->sw_csconfig &= ~(CSCONFIG_TCVDISAB);
  718. happy_meal_tcvr_write(hp, tregs,
  719. DP83840_CSCONFIG, hp->sw_csconfig);
  720. }
  721. restart_timer = 1;
  722. break;
  723. }
  724. if (hp->sw_bmsr & BMSR_LSTATUS) {
  725. /* Force mode selection success. */
  726. display_forced_link_mode(hp, tregs);
  727. set_happy_link_modes(hp, tregs); /* XXX error? then what? */
  728. hp->timer_state = asleep;
  729. restart_timer = 0;
  730. } else {
  731. if (hp->timer_ticks >= 4) { /* 6 seconds or so... */
  732. int ret;
  733. ret = try_next_permutation(hp, tregs);
  734. if (ret == -1) {
  735. /* Aieee, tried them all, reset the
  736. * chip and try all over again.
  737. */
  738. /* Let the user know... */
  739. printk(KERN_NOTICE "%s: Link down, cable problem?\n",
  740. hp->dev->name);
  741. ret = happy_meal_init(hp);
  742. if (ret) {
  743. /* ho hum... */
  744. printk(KERN_ERR "%s: Error, cannot re-init the "
  745. "Happy Meal.\n", hp->dev->name);
  746. }
  747. goto out;
  748. }
  749. if (!is_lucent_phy(hp)) {
  750. hp->sw_csconfig = happy_meal_tcvr_read(hp, tregs,
  751. DP83840_CSCONFIG);
  752. hp->sw_csconfig |= CSCONFIG_TCVDISAB;
  753. happy_meal_tcvr_write(hp, tregs,
  754. DP83840_CSCONFIG, hp->sw_csconfig);
  755. }
  756. hp->timer_ticks = 0;
  757. restart_timer = 1;
  758. } else {
  759. restart_timer = 1;
  760. }
  761. }
  762. break;
  763. case asleep:
  764. default:
  765. /* Can't happens.... */
  766. printk(KERN_ERR "%s: Aieee, link timer is asleep but we got one anyways!\n",
  767. hp->dev->name);
  768. restart_timer = 0;
  769. hp->timer_ticks = 0;
  770. hp->timer_state = asleep; /* foo on you */
  771. break;
  772. };
  773. if (restart_timer) {
  774. hp->happy_timer.expires = jiffies + ((12 * HZ)/10); /* 1.2 sec. */
  775. add_timer(&hp->happy_timer);
  776. }
  777. out:
  778. spin_unlock_irq(&hp->happy_lock);
  779. }
  780. #define TX_RESET_TRIES 32
  781. #define RX_RESET_TRIES 32
  782. /* hp->happy_lock must be held */
  783. static void happy_meal_tx_reset(struct happy_meal *hp, void __iomem *bregs)
  784. {
  785. int tries = TX_RESET_TRIES;
  786. HMD(("happy_meal_tx_reset: reset, "));
  787. /* Would you like to try our SMCC Delux? */
  788. hme_write32(hp, bregs + BMAC_TXSWRESET, 0);
  789. while ((hme_read32(hp, bregs + BMAC_TXSWRESET) & 1) && --tries)
  790. udelay(20);
  791. /* Lettuce, tomato, buggy hardware (no extra charge)? */
  792. if (!tries)
  793. printk(KERN_ERR "happy meal: Transceiver BigMac ATTACK!");
  794. /* Take care. */
  795. HMD(("done\n"));
  796. }
  797. /* hp->happy_lock must be held */
  798. static void happy_meal_rx_reset(struct happy_meal *hp, void __iomem *bregs)
  799. {
  800. int tries = RX_RESET_TRIES;
  801. HMD(("happy_meal_rx_reset: reset, "));
  802. /* We have a special on GNU/Viking hardware bugs today. */
  803. hme_write32(hp, bregs + BMAC_RXSWRESET, 0);
  804. while ((hme_read32(hp, bregs + BMAC_RXSWRESET) & 1) && --tries)
  805. udelay(20);
  806. /* Will that be all? */
  807. if (!tries)
  808. printk(KERN_ERR "happy meal: Receiver BigMac ATTACK!");
  809. /* Don't forget your vik_1137125_wa. Have a nice day. */
  810. HMD(("done\n"));
  811. }
  812. #define STOP_TRIES 16
  813. /* hp->happy_lock must be held */
  814. static void happy_meal_stop(struct happy_meal *hp, void __iomem *gregs)
  815. {
  816. int tries = STOP_TRIES;
  817. HMD(("happy_meal_stop: reset, "));
  818. /* We're consolidating our STB products, it's your lucky day. */
  819. hme_write32(hp, gregs + GREG_SWRESET, GREG_RESET_ALL);
  820. while (hme_read32(hp, gregs + GREG_SWRESET) && --tries)
  821. udelay(20);
  822. /* Come back next week when we are "Sun Microelectronics". */
  823. if (!tries)
  824. printk(KERN_ERR "happy meal: Fry guys.");
  825. /* Remember: "Different name, same old buggy as shit hardware." */
  826. HMD(("done\n"));
  827. }
  828. /* hp->happy_lock must be held */
  829. static void happy_meal_get_counters(struct happy_meal *hp, void __iomem *bregs)
  830. {
  831. struct net_device_stats *stats = &hp->net_stats;
  832. stats->rx_crc_errors += hme_read32(hp, bregs + BMAC_RCRCECTR);
  833. hme_write32(hp, bregs + BMAC_RCRCECTR, 0);
  834. stats->rx_frame_errors += hme_read32(hp, bregs + BMAC_UNALECTR);
  835. hme_write32(hp, bregs + BMAC_UNALECTR, 0);
  836. stats->rx_length_errors += hme_read32(hp, bregs + BMAC_GLECTR);
  837. hme_write32(hp, bregs + BMAC_GLECTR, 0);
  838. stats->tx_aborted_errors += hme_read32(hp, bregs + BMAC_EXCTR);
  839. stats->collisions +=
  840. (hme_read32(hp, bregs + BMAC_EXCTR) +
  841. hme_read32(hp, bregs + BMAC_LTCTR));
  842. hme_write32(hp, bregs + BMAC_EXCTR, 0);
  843. hme_write32(hp, bregs + BMAC_LTCTR, 0);
  844. }
  845. /* hp->happy_lock must be held */
  846. static void happy_meal_poll_stop(struct happy_meal *hp, void __iomem *tregs)
  847. {
  848. ASD(("happy_meal_poll_stop: "));
  849. /* If polling disabled or not polling already, nothing to do. */
  850. if ((hp->happy_flags & (HFLAG_POLLENABLE | HFLAG_POLL)) !=
  851. (HFLAG_POLLENABLE | HFLAG_POLL)) {
  852. HMD(("not polling, return\n"));
  853. return;
  854. }
  855. /* Shut up the MIF. */
  856. ASD(("were polling, mif ints off, "));
  857. hme_write32(hp, tregs + TCVR_IMASK, 0xffff);
  858. /* Turn off polling. */
  859. ASD(("polling off, "));
  860. hme_write32(hp, tregs + TCVR_CFG,
  861. hme_read32(hp, tregs + TCVR_CFG) & ~(TCV_CFG_PENABLE));
  862. /* We are no longer polling. */
  863. hp->happy_flags &= ~(HFLAG_POLL);
  864. /* Let the bits set. */
  865. udelay(200);
  866. ASD(("done\n"));
  867. }
  868. /* Only Sun can take such nice parts and fuck up the programming interface
  869. * like this. Good job guys...
  870. */
  871. #define TCVR_RESET_TRIES 16 /* It should reset quickly */
  872. #define TCVR_UNISOLATE_TRIES 32 /* Dis-isolation can take longer. */
  873. /* hp->happy_lock must be held */
  874. static int happy_meal_tcvr_reset(struct happy_meal *hp, void __iomem *tregs)
  875. {
  876. u32 tconfig;
  877. int result, tries = TCVR_RESET_TRIES;
  878. tconfig = hme_read32(hp, tregs + TCVR_CFG);
  879. ASD(("happy_meal_tcvr_reset: tcfg<%08lx> ", tconfig));
  880. if (hp->tcvr_type == external) {
  881. ASD(("external<"));
  882. hme_write32(hp, tregs + TCVR_CFG, tconfig & ~(TCV_CFG_PSELECT));
  883. hp->tcvr_type = internal;
  884. hp->paddr = TCV_PADDR_ITX;
  885. ASD(("ISOLATE,"));
  886. happy_meal_tcvr_write(hp, tregs, MII_BMCR,
  887. (BMCR_LOOPBACK|BMCR_PDOWN|BMCR_ISOLATE));
  888. result = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
  889. if (result == TCVR_FAILURE) {
  890. ASD(("phyread_fail>\n"));
  891. return -1;
  892. }
  893. ASD(("phyread_ok,PSELECT>"));
  894. hme_write32(hp, tregs + TCVR_CFG, tconfig | TCV_CFG_PSELECT);
  895. hp->tcvr_type = external;
  896. hp->paddr = TCV_PADDR_ETX;
  897. } else {
  898. if (tconfig & TCV_CFG_MDIO1) {
  899. ASD(("internal<PSELECT,"));
  900. hme_write32(hp, tregs + TCVR_CFG, (tconfig | TCV_CFG_PSELECT));
  901. ASD(("ISOLATE,"));
  902. happy_meal_tcvr_write(hp, tregs, MII_BMCR,
  903. (BMCR_LOOPBACK|BMCR_PDOWN|BMCR_ISOLATE));
  904. result = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
  905. if (result == TCVR_FAILURE) {
  906. ASD(("phyread_fail>\n"));
  907. return -1;
  908. }
  909. ASD(("phyread_ok,~PSELECT>"));
  910. hme_write32(hp, tregs + TCVR_CFG, (tconfig & ~(TCV_CFG_PSELECT)));
  911. hp->tcvr_type = internal;
  912. hp->paddr = TCV_PADDR_ITX;
  913. }
  914. }
  915. ASD(("BMCR_RESET "));
  916. happy_meal_tcvr_write(hp, tregs, MII_BMCR, BMCR_RESET);
  917. while (--tries) {
  918. result = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
  919. if (result == TCVR_FAILURE)
  920. return -1;
  921. hp->sw_bmcr = result;
  922. if (!(result & BMCR_RESET))
  923. break;
  924. udelay(20);
  925. }
  926. if (!tries) {
  927. ASD(("BMCR RESET FAILED!\n"));
  928. return -1;
  929. }
  930. ASD(("RESET_OK\n"));
  931. /* Get fresh copies of the PHY registers. */
  932. hp->sw_bmsr = happy_meal_tcvr_read(hp, tregs, MII_BMSR);
  933. hp->sw_physid1 = happy_meal_tcvr_read(hp, tregs, MII_PHYSID1);
  934. hp->sw_physid2 = happy_meal_tcvr_read(hp, tregs, MII_PHYSID2);
  935. hp->sw_advertise = happy_meal_tcvr_read(hp, tregs, MII_ADVERTISE);
  936. ASD(("UNISOLATE"));
  937. hp->sw_bmcr &= ~(BMCR_ISOLATE);
  938. happy_meal_tcvr_write(hp, tregs, MII_BMCR, hp->sw_bmcr);
  939. tries = TCVR_UNISOLATE_TRIES;
  940. while (--tries) {
  941. result = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
  942. if (result == TCVR_FAILURE)
  943. return -1;
  944. if (!(result & BMCR_ISOLATE))
  945. break;
  946. udelay(20);
  947. }
  948. if (!tries) {
  949. ASD((" FAILED!\n"));
  950. return -1;
  951. }
  952. ASD((" SUCCESS and CSCONFIG_DFBYPASS\n"));
  953. if (!is_lucent_phy(hp)) {
  954. result = happy_meal_tcvr_read(hp, tregs,
  955. DP83840_CSCONFIG);
  956. happy_meal_tcvr_write(hp, tregs,
  957. DP83840_CSCONFIG, (result | CSCONFIG_DFBYPASS));
  958. }
  959. return 0;
  960. }
  961. /* Figure out whether we have an internal or external transceiver.
  962. *
  963. * hp->happy_lock must be held
  964. */
  965. static void happy_meal_transceiver_check(struct happy_meal *hp, void __iomem *tregs)
  966. {
  967. unsigned long tconfig = hme_read32(hp, tregs + TCVR_CFG);
  968. ASD(("happy_meal_transceiver_check: tcfg=%08lx ", tconfig));
  969. if (hp->happy_flags & HFLAG_POLL) {
  970. /* If we are polling, we must stop to get the transceiver type. */
  971. ASD(("<polling> "));
  972. if (hp->tcvr_type == internal) {
  973. if (tconfig & TCV_CFG_MDIO1) {
  974. ASD(("<internal> <poll stop> "));
  975. happy_meal_poll_stop(hp, tregs);
  976. hp->paddr = TCV_PADDR_ETX;
  977. hp->tcvr_type = external;
  978. ASD(("<external>\n"));
  979. tconfig &= ~(TCV_CFG_PENABLE);
  980. tconfig |= TCV_CFG_PSELECT;
  981. hme_write32(hp, tregs + TCVR_CFG, tconfig);
  982. }
  983. } else {
  984. if (hp->tcvr_type == external) {
  985. ASD(("<external> "));
  986. if (!(hme_read32(hp, tregs + TCVR_STATUS) >> 16)) {
  987. ASD(("<poll stop> "));
  988. happy_meal_poll_stop(hp, tregs);
  989. hp->paddr = TCV_PADDR_ITX;
  990. hp->tcvr_type = internal;
  991. ASD(("<internal>\n"));
  992. hme_write32(hp, tregs + TCVR_CFG,
  993. hme_read32(hp, tregs + TCVR_CFG) &
  994. ~(TCV_CFG_PSELECT));
  995. }
  996. ASD(("\n"));
  997. } else {
  998. ASD(("<none>\n"));
  999. }
  1000. }
  1001. } else {
  1002. u32 reread = hme_read32(hp, tregs + TCVR_CFG);
  1003. /* Else we can just work off of the MDIO bits. */
  1004. ASD(("<not polling> "));
  1005. if (reread & TCV_CFG_MDIO1) {
  1006. hme_write32(hp, tregs + TCVR_CFG, tconfig | TCV_CFG_PSELECT);
  1007. hp->paddr = TCV_PADDR_ETX;
  1008. hp->tcvr_type = external;
  1009. ASD(("<external>\n"));
  1010. } else {
  1011. if (reread & TCV_CFG_MDIO0) {
  1012. hme_write32(hp, tregs + TCVR_CFG,
  1013. tconfig & ~(TCV_CFG_PSELECT));
  1014. hp->paddr = TCV_PADDR_ITX;
  1015. hp->tcvr_type = internal;
  1016. ASD(("<internal>\n"));
  1017. } else {
  1018. printk(KERN_ERR "happy meal: Transceiver and a coke please.");
  1019. hp->tcvr_type = none; /* Grrr... */
  1020. ASD(("<none>\n"));
  1021. }
  1022. }
  1023. }
  1024. }
  1025. /* The receive ring buffers are a bit tricky to get right. Here goes...
  1026. *
  1027. * The buffers we dma into must be 64 byte aligned. So we use a special
  1028. * alloc_skb() routine for the happy meal to allocate 64 bytes more than
  1029. * we really need.
  1030. *
  1031. * We use skb_reserve() to align the data block we get in the skb. We
  1032. * also program the etxregs->cfg register to use an offset of 2. This
  1033. * imperical constant plus the ethernet header size will always leave
  1034. * us with a nicely aligned ip header once we pass things up to the
  1035. * protocol layers.
  1036. *
  1037. * The numbers work out to:
  1038. *
  1039. * Max ethernet frame size 1518
  1040. * Ethernet header size 14
  1041. * Happy Meal base offset 2
  1042. *
  1043. * Say a skb data area is at 0xf001b010, and its size alloced is
  1044. * (ETH_FRAME_LEN + 64 + 2) = (1514 + 64 + 2) = 1580 bytes.
  1045. *
  1046. * First our alloc_skb() routine aligns the data base to a 64 byte
  1047. * boundary. We now have 0xf001b040 as our skb data address. We
  1048. * plug this into the receive descriptor address.
  1049. *
  1050. * Next, we skb_reserve() 2 bytes to account for the Happy Meal offset.
  1051. * So now the data we will end up looking at starts at 0xf001b042. When
  1052. * the packet arrives, we will check out the size received and subtract
  1053. * this from the skb->length. Then we just pass the packet up to the
  1054. * protocols as is, and allocate a new skb to replace this slot we have
  1055. * just received from.
  1056. *
  1057. * The ethernet layer will strip the ether header from the front of the
  1058. * skb we just sent to it, this leaves us with the ip header sitting
  1059. * nicely aligned at 0xf001b050. Also, for tcp and udp packets the
  1060. * Happy Meal has even checksummed the tcp/udp data for us. The 16
  1061. * bit checksum is obtained from the low bits of the receive descriptor
  1062. * flags, thus:
  1063. *
  1064. * skb->csum = rxd->rx_flags & 0xffff;
  1065. * skb->ip_summed = CHECKSUM_COMPLETE;
  1066. *
  1067. * before sending off the skb to the protocols, and we are good as gold.
  1068. */
  1069. static void happy_meal_clean_rings(struct happy_meal *hp)
  1070. {
  1071. int i;
  1072. for (i = 0; i < RX_RING_SIZE; i++) {
  1073. if (hp->rx_skbs[i] != NULL) {
  1074. struct sk_buff *skb = hp->rx_skbs[i];
  1075. struct happy_meal_rxd *rxd;
  1076. u32 dma_addr;
  1077. rxd = &hp->happy_block->happy_meal_rxd[i];
  1078. dma_addr = hme_read_desc32(hp, &rxd->rx_addr);
  1079. hme_dma_unmap(hp, dma_addr, RX_BUF_ALLOC_SIZE, DMA_FROMDEVICE);
  1080. dev_kfree_skb_any(skb);
  1081. hp->rx_skbs[i] = NULL;
  1082. }
  1083. }
  1084. for (i = 0; i < TX_RING_SIZE; i++) {
  1085. if (hp->tx_skbs[i] != NULL) {
  1086. struct sk_buff *skb = hp->tx_skbs[i];
  1087. struct happy_meal_txd *txd;
  1088. u32 dma_addr;
  1089. int frag;
  1090. hp->tx_skbs[i] = NULL;
  1091. for (frag = 0; frag <= skb_shinfo(skb)->nr_frags; frag++) {
  1092. txd = &hp->happy_block->happy_meal_txd[i];
  1093. dma_addr = hme_read_desc32(hp, &txd->tx_addr);
  1094. hme_dma_unmap(hp, dma_addr,
  1095. (hme_read_desc32(hp, &txd->tx_flags)
  1096. & TXFLAG_SIZE),
  1097. DMA_TODEVICE);
  1098. if (frag != skb_shinfo(skb)->nr_frags)
  1099. i++;
  1100. }
  1101. dev_kfree_skb_any(skb);
  1102. }
  1103. }
  1104. }
  1105. /* hp->happy_lock must be held */
  1106. static void happy_meal_init_rings(struct happy_meal *hp)
  1107. {
  1108. struct hmeal_init_block *hb = hp->happy_block;
  1109. struct net_device *dev = hp->dev;
  1110. int i;
  1111. HMD(("happy_meal_init_rings: counters to zero, "));
  1112. hp->rx_new = hp->rx_old = hp->tx_new = hp->tx_old = 0;
  1113. /* Free any skippy bufs left around in the rings. */
  1114. HMD(("clean, "));
  1115. happy_meal_clean_rings(hp);
  1116. /* Now get new skippy bufs for the receive ring. */
  1117. HMD(("init rxring, "));
  1118. for (i = 0; i < RX_RING_SIZE; i++) {
  1119. struct sk_buff *skb;
  1120. skb = happy_meal_alloc_skb(RX_BUF_ALLOC_SIZE, GFP_ATOMIC);
  1121. if (!skb) {
  1122. hme_write_rxd(hp, &hb->happy_meal_rxd[i], 0, 0);
  1123. continue;
  1124. }
  1125. hp->rx_skbs[i] = skb;
  1126. skb->dev = dev;
  1127. /* Because we reserve afterwards. */
  1128. skb_put(skb, (ETH_FRAME_LEN + RX_OFFSET + 4));
  1129. hme_write_rxd(hp, &hb->happy_meal_rxd[i],
  1130. (RXFLAG_OWN | ((RX_BUF_ALLOC_SIZE - RX_OFFSET) << 16)),
  1131. hme_dma_map(hp, skb->data, RX_BUF_ALLOC_SIZE, DMA_FROMDEVICE));
  1132. skb_reserve(skb, RX_OFFSET);
  1133. }
  1134. HMD(("init txring, "));
  1135. for (i = 0; i < TX_RING_SIZE; i++)
  1136. hme_write_txd(hp, &hb->happy_meal_txd[i], 0, 0);
  1137. HMD(("done\n"));
  1138. }
  1139. /* hp->happy_lock must be held */
  1140. static void happy_meal_begin_auto_negotiation(struct happy_meal *hp,
  1141. void __iomem *tregs,
  1142. struct ethtool_cmd *ep)
  1143. {
  1144. int timeout;
  1145. /* Read all of the registers we are interested in now. */
  1146. hp->sw_bmsr = happy_meal_tcvr_read(hp, tregs, MII_BMSR);
  1147. hp->sw_bmcr = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
  1148. hp->sw_physid1 = happy_meal_tcvr_read(hp, tregs, MII_PHYSID1);
  1149. hp->sw_physid2 = happy_meal_tcvr_read(hp, tregs, MII_PHYSID2);
  1150. /* XXX Check BMSR_ANEGCAPABLE, should not be necessary though. */
  1151. hp->sw_advertise = happy_meal_tcvr_read(hp, tregs, MII_ADVERTISE);
  1152. if (ep == NULL || ep->autoneg == AUTONEG_ENABLE) {
  1153. /* Advertise everything we can support. */
  1154. if (hp->sw_bmsr & BMSR_10HALF)
  1155. hp->sw_advertise |= (ADVERTISE_10HALF);
  1156. else
  1157. hp->sw_advertise &= ~(ADVERTISE_10HALF);
  1158. if (hp->sw_bmsr & BMSR_10FULL)
  1159. hp->sw_advertise |= (ADVERTISE_10FULL);
  1160. else
  1161. hp->sw_advertise &= ~(ADVERTISE_10FULL);
  1162. if (hp->sw_bmsr & BMSR_100HALF)
  1163. hp->sw_advertise |= (ADVERTISE_100HALF);
  1164. else
  1165. hp->sw_advertise &= ~(ADVERTISE_100HALF);
  1166. if (hp->sw_bmsr & BMSR_100FULL)
  1167. hp->sw_advertise |= (ADVERTISE_100FULL);
  1168. else
  1169. hp->sw_advertise &= ~(ADVERTISE_100FULL);
  1170. happy_meal_tcvr_write(hp, tregs, MII_ADVERTISE, hp->sw_advertise);
  1171. /* XXX Currently no Happy Meal cards I know off support 100BaseT4,
  1172. * XXX and this is because the DP83840 does not support it, changes
  1173. * XXX would need to be made to the tx/rx logic in the driver as well
  1174. * XXX so I completely skip checking for it in the BMSR for now.
  1175. */
  1176. #ifdef AUTO_SWITCH_DEBUG
  1177. ASD(("%s: Advertising [ ", hp->dev->name));
  1178. if (hp->sw_advertise & ADVERTISE_10HALF)
  1179. ASD(("10H "));
  1180. if (hp->sw_advertise & ADVERTISE_10FULL)
  1181. ASD(("10F "));
  1182. if (hp->sw_advertise & ADVERTISE_100HALF)
  1183. ASD(("100H "));
  1184. if (hp->sw_advertise & ADVERTISE_100FULL)
  1185. ASD(("100F "));
  1186. #endif
  1187. /* Enable Auto-Negotiation, this is usually on already... */
  1188. hp->sw_bmcr |= BMCR_ANENABLE;
  1189. happy_meal_tcvr_write(hp, tregs, MII_BMCR, hp->sw_bmcr);
  1190. /* Restart it to make sure it is going. */
  1191. hp->sw_bmcr |= BMCR_ANRESTART;
  1192. happy_meal_tcvr_write(hp, tregs, MII_BMCR, hp->sw_bmcr);
  1193. /* BMCR_ANRESTART self clears when the process has begun. */
  1194. timeout = 64; /* More than enough. */
  1195. while (--timeout) {
  1196. hp->sw_bmcr = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
  1197. if (!(hp->sw_bmcr & BMCR_ANRESTART))
  1198. break; /* got it. */
  1199. udelay(10);
  1200. }
  1201. if (!timeout) {
  1202. printk(KERN_ERR "%s: Happy Meal would not start auto negotiation "
  1203. "BMCR=0x%04x\n", hp->dev->name, hp->sw_bmcr);
  1204. printk(KERN_NOTICE "%s: Performing force link detection.\n",
  1205. hp->dev->name);
  1206. goto force_link;
  1207. } else {
  1208. hp->timer_state = arbwait;
  1209. }
  1210. } else {
  1211. force_link:
  1212. /* Force the link up, trying first a particular mode.
  1213. * Either we are here at the request of ethtool or
  1214. * because the Happy Meal would not start to autoneg.
  1215. */
  1216. /* Disable auto-negotiation in BMCR, enable the duplex and
  1217. * speed setting, init the timer state machine, and fire it off.
  1218. */
  1219. if (ep == NULL || ep->autoneg == AUTONEG_ENABLE) {
  1220. hp->sw_bmcr = BMCR_SPEED100;
  1221. } else {
  1222. if (ep->speed == SPEED_100)
  1223. hp->sw_bmcr = BMCR_SPEED100;
  1224. else
  1225. hp->sw_bmcr = 0;
  1226. if (ep->duplex == DUPLEX_FULL)
  1227. hp->sw_bmcr |= BMCR_FULLDPLX;
  1228. }
  1229. happy_meal_tcvr_write(hp, tregs, MII_BMCR, hp->sw_bmcr);
  1230. if (!is_lucent_phy(hp)) {
  1231. /* OK, seems we need do disable the transceiver for the first
  1232. * tick to make sure we get an accurate link state at the
  1233. * second tick.
  1234. */
  1235. hp->sw_csconfig = happy_meal_tcvr_read(hp, tregs,
  1236. DP83840_CSCONFIG);
  1237. hp->sw_csconfig &= ~(CSCONFIG_TCVDISAB);
  1238. happy_meal_tcvr_write(hp, tregs, DP83840_CSCONFIG,
  1239. hp->sw_csconfig);
  1240. }
  1241. hp->timer_state = ltrywait;
  1242. }
  1243. hp->timer_ticks = 0;
  1244. hp->happy_timer.expires = jiffies + (12 * HZ)/10; /* 1.2 sec. */
  1245. hp->happy_timer.data = (unsigned long) hp;
  1246. hp->happy_timer.function = &happy_meal_timer;
  1247. add_timer(&hp->happy_timer);
  1248. }
  1249. /* hp->happy_lock must be held */
  1250. static int happy_meal_init(struct happy_meal *hp)
  1251. {
  1252. void __iomem *gregs = hp->gregs;
  1253. void __iomem *etxregs = hp->etxregs;
  1254. void __iomem *erxregs = hp->erxregs;
  1255. void __iomem *bregs = hp->bigmacregs;
  1256. void __iomem *tregs = hp->tcvregs;
  1257. u32 regtmp, rxcfg;
  1258. unsigned char *e = &hp->dev->dev_addr[0];
  1259. /* If auto-negotiation timer is running, kill it. */
  1260. del_timer(&hp->happy_timer);
  1261. HMD(("happy_meal_init: happy_flags[%08x] ",
  1262. hp->happy_flags));
  1263. if (!(hp->happy_flags & HFLAG_INIT)) {
  1264. HMD(("set HFLAG_INIT, "));
  1265. hp->happy_flags |= HFLAG_INIT;
  1266. happy_meal_get_counters(hp, bregs);
  1267. }
  1268. /* Stop polling. */
  1269. HMD(("to happy_meal_poll_stop\n"));
  1270. happy_meal_poll_stop(hp, tregs);
  1271. /* Stop transmitter and receiver. */
  1272. HMD(("happy_meal_init: to happy_meal_stop\n"));
  1273. happy_meal_stop(hp, gregs);
  1274. /* Alloc and reset the tx/rx descriptor chains. */
  1275. HMD(("happy_meal_init: to happy_meal_init_rings\n"));
  1276. happy_meal_init_rings(hp);
  1277. /* Shut up the MIF. */
  1278. HMD(("happy_meal_init: Disable all MIF irqs (old[%08x]), ",
  1279. hme_read32(hp, tregs + TCVR_IMASK)));
  1280. hme_write32(hp, tregs + TCVR_IMASK, 0xffff);
  1281. /* See if we can enable the MIF frame on this card to speak to the DP83840. */
  1282. if (hp->happy_flags & HFLAG_FENABLE) {
  1283. HMD(("use frame old[%08x], ",
  1284. hme_read32(hp, tregs + TCVR_CFG)));
  1285. hme_write32(hp, tregs + TCVR_CFG,
  1286. hme_read32(hp, tregs + TCVR_CFG) & ~(TCV_CFG_BENABLE));
  1287. } else {
  1288. HMD(("use bitbang old[%08x], ",
  1289. hme_read32(hp, tregs + TCVR_CFG)));
  1290. hme_write32(hp, tregs + TCVR_CFG,
  1291. hme_read32(hp, tregs + TCVR_CFG) | TCV_CFG_BENABLE);
  1292. }
  1293. /* Check the state of the transceiver. */
  1294. HMD(("to happy_meal_transceiver_check\n"));
  1295. happy_meal_transceiver_check(hp, tregs);
  1296. /* Put the Big Mac into a sane state. */
  1297. HMD(("happy_meal_init: "));
  1298. switch(hp->tcvr_type) {
  1299. case none:
  1300. /* Cannot operate if we don't know the transceiver type! */
  1301. HMD(("AAIEEE no transceiver type, EAGAIN"));
  1302. return -EAGAIN;
  1303. case internal:
  1304. /* Using the MII buffers. */
  1305. HMD(("internal, using MII, "));
  1306. hme_write32(hp, bregs + BMAC_XIFCFG, 0);
  1307. break;
  1308. case external:
  1309. /* Not using the MII, disable it. */
  1310. HMD(("external, disable MII, "));
  1311. hme_write32(hp, bregs + BMAC_XIFCFG, BIGMAC_XCFG_MIIDISAB);
  1312. break;
  1313. };
  1314. if (happy_meal_tcvr_reset(hp, tregs))
  1315. return -EAGAIN;
  1316. /* Reset the Happy Meal Big Mac transceiver and the receiver. */
  1317. HMD(("tx/rx reset, "));
  1318. happy_meal_tx_reset(hp, bregs);
  1319. happy_meal_rx_reset(hp, bregs);
  1320. /* Set jam size and inter-packet gaps to reasonable defaults. */
  1321. HMD(("jsize/ipg1/ipg2, "));
  1322. hme_write32(hp, bregs + BMAC_JSIZE, DEFAULT_JAMSIZE);
  1323. hme_write32(hp, bregs + BMAC_IGAP1, DEFAULT_IPG1);
  1324. hme_write32(hp, bregs + BMAC_IGAP2, DEFAULT_IPG2);
  1325. /* Load up the MAC address and random seed. */
  1326. HMD(("rseed/macaddr, "));
  1327. /* The docs recommend to use the 10LSB of our MAC here. */
  1328. hme_write32(hp, bregs + BMAC_RSEED, ((e[5] | e[4]<<8)&0x3ff));
  1329. hme_write32(hp, bregs + BMAC_MACADDR2, ((e[4] << 8) | e[5]));
  1330. hme_write32(hp, bregs + BMAC_MACADDR1, ((e[2] << 8) | e[3]));
  1331. hme_write32(hp, bregs + BMAC_MACADDR0, ((e[0] << 8) | e[1]));
  1332. HMD(("htable, "));
  1333. if ((hp->dev->flags & IFF_ALLMULTI) ||
  1334. (hp->dev->mc_count > 64)) {
  1335. hme_write32(hp, bregs + BMAC_HTABLE0, 0xffff);
  1336. hme_write32(hp, bregs + BMAC_HTABLE1, 0xffff);
  1337. hme_write32(hp, bregs + BMAC_HTABLE2, 0xffff);
  1338. hme_write32(hp, bregs + BMAC_HTABLE3, 0xffff);
  1339. } else if ((hp->dev->flags & IFF_PROMISC) == 0) {
  1340. u16 hash_table[4];
  1341. struct dev_mc_list *dmi = hp->dev->mc_list;
  1342. char *addrs;
  1343. int i;
  1344. u32 crc;
  1345. for (i = 0; i < 4; i++)
  1346. hash_table[i] = 0;
  1347. for (i = 0; i < hp->dev->mc_count; i++) {
  1348. addrs = dmi->dmi_addr;
  1349. dmi = dmi->next;
  1350. if (!(*addrs & 1))
  1351. continue;
  1352. crc = ether_crc_le(6, addrs);
  1353. crc >>= 26;
  1354. hash_table[crc >> 4] |= 1 << (crc & 0xf);
  1355. }
  1356. hme_write32(hp, bregs + BMAC_HTABLE0, hash_table[0]);
  1357. hme_write32(hp, bregs + BMAC_HTABLE1, hash_table[1]);
  1358. hme_write32(hp, bregs + BMAC_HTABLE2, hash_table[2]);
  1359. hme_write32(hp, bregs + BMAC_HTABLE3, hash_table[3]);
  1360. } else {
  1361. hme_write32(hp, bregs + BMAC_HTABLE3, 0);
  1362. hme_write32(hp, bregs + BMAC_HTABLE2, 0);
  1363. hme_write32(hp, bregs + BMAC_HTABLE1, 0);
  1364. hme_write32(hp, bregs + BMAC_HTABLE0, 0);
  1365. }
  1366. /* Set the RX and TX ring ptrs. */
  1367. HMD(("ring ptrs rxr[%08x] txr[%08x]\n",
  1368. ((__u32)hp->hblock_dvma + hblock_offset(happy_meal_rxd, 0)),
  1369. ((__u32)hp->hblock_dvma + hblock_offset(happy_meal_txd, 0))));
  1370. hme_write32(hp, erxregs + ERX_RING,
  1371. ((__u32)hp->hblock_dvma + hblock_offset(happy_meal_rxd, 0)));
  1372. hme_write32(hp, etxregs + ETX_RING,
  1373. ((__u32)hp->hblock_dvma + hblock_offset(happy_meal_txd, 0)));
  1374. /* Parity issues in the ERX unit of some HME revisions can cause some
  1375. * registers to not be written unless their parity is even. Detect such
  1376. * lost writes and simply rewrite with a low bit set (which will be ignored
  1377. * since the rxring needs to be 2K aligned).
  1378. */
  1379. if (hme_read32(hp, erxregs + ERX_RING) !=
  1380. ((__u32)hp->hblock_dvma + hblock_offset(happy_meal_rxd, 0)))
  1381. hme_write32(hp, erxregs + ERX_RING,
  1382. ((__u32)hp->hblock_dvma + hblock_offset(happy_meal_rxd, 0))
  1383. | 0x4);
  1384. /* Set the supported burst sizes. */
  1385. HMD(("happy_meal_init: old[%08x] bursts<",
  1386. hme_read32(hp, gregs + GREG_CFG)));
  1387. #ifndef CONFIG_SPARC
  1388. /* It is always PCI and can handle 64byte bursts. */
  1389. hme_write32(hp, gregs + GREG_CFG, GREG_CFG_BURST64);
  1390. #else
  1391. if ((hp->happy_bursts & DMA_BURST64) &&
  1392. ((hp->happy_flags & HFLAG_PCI) != 0
  1393. #ifdef CONFIG_SBUS
  1394. || sbus_can_burst64(hp->happy_dev)
  1395. #endif
  1396. || 0)) {
  1397. u32 gcfg = GREG_CFG_BURST64;
  1398. /* I have no idea if I should set the extended
  1399. * transfer mode bit for Cheerio, so for now I
  1400. * do not. -DaveM
  1401. */
  1402. #ifdef CONFIG_SBUS
  1403. if ((hp->happy_flags & HFLAG_PCI) == 0 &&
  1404. sbus_can_dma_64bit(hp->happy_dev)) {
  1405. sbus_set_sbus64(hp->happy_dev,
  1406. hp->happy_bursts);
  1407. gcfg |= GREG_CFG_64BIT;
  1408. }
  1409. #endif
  1410. HMD(("64>"));
  1411. hme_write32(hp, gregs + GREG_CFG, gcfg);
  1412. } else if (hp->happy_bursts & DMA_BURST32) {
  1413. HMD(("32>"));
  1414. hme_write32(hp, gregs + GREG_CFG, GREG_CFG_BURST32);
  1415. } else if (hp->happy_bursts & DMA_BURST16) {
  1416. HMD(("16>"));
  1417. hme_write32(hp, gregs + GREG_CFG, GREG_CFG_BURST16);
  1418. } else {
  1419. HMD(("XXX>"));
  1420. hme_write32(hp, gregs + GREG_CFG, 0);
  1421. }
  1422. #endif /* CONFIG_SPARC */
  1423. /* Turn off interrupts we do not want to hear. */
  1424. HMD((", enable global interrupts, "));
  1425. hme_write32(hp, gregs + GREG_IMASK,
  1426. (GREG_IMASK_GOTFRAME | GREG_IMASK_RCNTEXP |
  1427. GREG_IMASK_SENTFRAME | GREG_IMASK_TXPERR));
  1428. /* Set the transmit ring buffer size. */
  1429. HMD(("tx rsize=%d oreg[%08x], ", (int)TX_RING_SIZE,
  1430. hme_read32(hp, etxregs + ETX_RSIZE)));
  1431. hme_write32(hp, etxregs + ETX_RSIZE, (TX_RING_SIZE >> ETX_RSIZE_SHIFT) - 1);
  1432. /* Enable transmitter DVMA. */
  1433. HMD(("tx dma enable old[%08x], ",
  1434. hme_read32(hp, etxregs + ETX_CFG)));
  1435. hme_write32(hp, etxregs + ETX_CFG,
  1436. hme_read32(hp, etxregs + ETX_CFG) | ETX_CFG_DMAENABLE);
  1437. /* This chip really rots, for the receiver sometimes when you
  1438. * write to its control registers not all the bits get there
  1439. * properly. I cannot think of a sane way to provide complete
  1440. * coverage for this hardware bug yet.
  1441. */
  1442. HMD(("erx regs bug old[%08x]\n",
  1443. hme_read32(hp, erxregs + ERX_CFG)));
  1444. hme_write32(hp, erxregs + ERX_CFG, ERX_CFG_DEFAULT(RX_OFFSET));
  1445. regtmp = hme_read32(hp, erxregs + ERX_CFG);
  1446. hme_write32(hp, erxregs + ERX_CFG, ERX_CFG_DEFAULT(RX_OFFSET));
  1447. if (hme_read32(hp, erxregs + ERX_CFG) != ERX_CFG_DEFAULT(RX_OFFSET)) {
  1448. printk(KERN_ERR "happy meal: Eieee, rx config register gets greasy fries.\n");
  1449. printk(KERN_ERR "happy meal: Trying to set %08x, reread gives %08x\n",
  1450. ERX_CFG_DEFAULT(RX_OFFSET), regtmp);
  1451. /* XXX Should return failure here... */
  1452. }
  1453. /* Enable Big Mac hash table filter. */
  1454. HMD(("happy_meal_init: enable hash rx_cfg_old[%08x], ",
  1455. hme_read32(hp, bregs + BMAC_RXCFG)));
  1456. rxcfg = BIGMAC_RXCFG_HENABLE | BIGMAC_RXCFG_REJME;
  1457. if (hp->dev->flags & IFF_PROMISC)
  1458. rxcfg |= BIGMAC_RXCFG_PMISC;
  1459. hme_write32(hp, bregs + BMAC_RXCFG, rxcfg);
  1460. /* Let the bits settle in the chip. */
  1461. udelay(10);
  1462. /* Ok, configure the Big Mac transmitter. */
  1463. HMD(("BIGMAC init, "));
  1464. regtmp = 0;
  1465. if (hp->happy_flags & HFLAG_FULL)
  1466. regtmp |= BIGMAC_TXCFG_FULLDPLX;
  1467. /* Don't turn on the "don't give up" bit for now. It could cause hme
  1468. * to deadlock with the PHY if a Jabber occurs.
  1469. */
  1470. hme_write32(hp, bregs + BMAC_TXCFG, regtmp /*| BIGMAC_TXCFG_DGIVEUP*/);
  1471. /* Give up after 16 TX attempts. */
  1472. hme_write32(hp, bregs + BMAC_ALIMIT, 16);
  1473. /* Enable the output drivers no matter what. */
  1474. regtmp = BIGMAC_XCFG_ODENABLE;
  1475. /* If card can do lance mode, enable it. */
  1476. if (hp->happy_flags & HFLAG_LANCE)
  1477. regtmp |= (DEFAULT_IPG0 << 5) | BIGMAC_XCFG_LANCE;
  1478. /* Disable the MII buffers if using external transceiver. */
  1479. if (hp->tcvr_type == external)
  1480. regtmp |= BIGMAC_XCFG_MIIDISAB;
  1481. HMD(("XIF config old[%08x], ",
  1482. hme_read32(hp, bregs + BMAC_XIFCFG)));
  1483. hme_write32(hp, bregs + BMAC_XIFCFG, regtmp);
  1484. /* Start things up. */
  1485. HMD(("tx old[%08x] and rx [%08x] ON!\n",
  1486. hme_read32(hp, bregs + BMAC_TXCFG),
  1487. hme_read32(hp, bregs + BMAC_RXCFG)));
  1488. /* Set larger TX/RX size to allow for 802.1q */
  1489. hme_write32(hp, bregs + BMAC_TXMAX, ETH_FRAME_LEN + 8);
  1490. hme_write32(hp, bregs + BMAC_RXMAX, ETH_FRAME_LEN + 8);
  1491. hme_write32(hp, bregs + BMAC_TXCFG,
  1492. hme_read32(hp, bregs + BMAC_TXCFG) | BIGMAC_TXCFG_ENABLE);
  1493. hme_write32(hp, bregs + BMAC_RXCFG,
  1494. hme_read32(hp, bregs + BMAC_RXCFG) | BIGMAC_RXCFG_ENABLE);
  1495. /* Get the autonegotiation started, and the watch timer ticking. */
  1496. happy_meal_begin_auto_negotiation(hp, tregs, NULL);
  1497. /* Success. */
  1498. return 0;
  1499. }
  1500. /* hp->happy_lock must be held */
  1501. static void happy_meal_set_initial_advertisement(struct happy_meal *hp)
  1502. {
  1503. void __iomem *tregs = hp->tcvregs;
  1504. void __iomem *bregs = hp->bigmacregs;
  1505. void __iomem *gregs = hp->gregs;
  1506. happy_meal_stop(hp, gregs);
  1507. hme_write32(hp, tregs + TCVR_IMASK, 0xffff);
  1508. if (hp->happy_flags & HFLAG_FENABLE)
  1509. hme_write32(hp, tregs + TCVR_CFG,
  1510. hme_read32(hp, tregs + TCVR_CFG) & ~(TCV_CFG_BENABLE));
  1511. else
  1512. hme_write32(hp, tregs + TCVR_CFG,
  1513. hme_read32(hp, tregs + TCVR_CFG) | TCV_CFG_BENABLE);
  1514. happy_meal_transceiver_check(hp, tregs);
  1515. switch(hp->tcvr_type) {
  1516. case none:
  1517. return;
  1518. case internal:
  1519. hme_write32(hp, bregs + BMAC_XIFCFG, 0);
  1520. break;
  1521. case external:
  1522. hme_write32(hp, bregs + BMAC_XIFCFG, BIGMAC_XCFG_MIIDISAB);
  1523. break;
  1524. };
  1525. if (happy_meal_tcvr_reset(hp, tregs))
  1526. return;
  1527. /* Latch PHY registers as of now. */
  1528. hp->sw_bmsr = happy_meal_tcvr_read(hp, tregs, MII_BMSR);
  1529. hp->sw_advertise = happy_meal_tcvr_read(hp, tregs, MII_ADVERTISE);
  1530. /* Advertise everything we can support. */
  1531. if (hp->sw_bmsr & BMSR_10HALF)
  1532. hp->sw_advertise |= (ADVERTISE_10HALF);
  1533. else
  1534. hp->sw_advertise &= ~(ADVERTISE_10HALF);
  1535. if (hp->sw_bmsr & BMSR_10FULL)
  1536. hp->sw_advertise |= (ADVERTISE_10FULL);
  1537. else
  1538. hp->sw_advertise &= ~(ADVERTISE_10FULL);
  1539. if (hp->sw_bmsr & BMSR_100HALF)
  1540. hp->sw_advertise |= (ADVERTISE_100HALF);
  1541. else
  1542. hp->sw_advertise &= ~(ADVERTISE_100HALF);
  1543. if (hp->sw_bmsr & BMSR_100FULL)
  1544. hp->sw_advertise |= (ADVERTISE_100FULL);
  1545. else
  1546. hp->sw_advertise &= ~(ADVERTISE_100FULL);
  1547. /* Update the PHY advertisement register. */
  1548. happy_meal_tcvr_write(hp, tregs, MII_ADVERTISE, hp->sw_advertise);
  1549. }
  1550. /* Once status is latched (by happy_meal_interrupt) it is cleared by
  1551. * the hardware, so we cannot re-read it and get a correct value.
  1552. *
  1553. * hp->happy_lock must be held
  1554. */
  1555. static int happy_meal_is_not_so_happy(struct happy_meal *hp, u32 status)
  1556. {
  1557. int reset = 0;
  1558. /* Only print messages for non-counter related interrupts. */
  1559. if (status & (GREG_STAT_STSTERR | GREG_STAT_TFIFO_UND |
  1560. GREG_STAT_MAXPKTERR | GREG_STAT_RXERR |
  1561. GREG_STAT_RXPERR | GREG_STAT_RXTERR | GREG_STAT_EOPERR |
  1562. GREG_STAT_MIFIRQ | GREG_STAT_TXEACK | GREG_STAT_TXLERR |
  1563. GREG_STAT_TXPERR | GREG_STAT_TXTERR | GREG_STAT_SLVERR |
  1564. GREG_STAT_SLVPERR))
  1565. printk(KERN_ERR "%s: Error interrupt for happy meal, status = %08x\n",
  1566. hp->dev->name, status);
  1567. if (status & GREG_STAT_RFIFOVF) {
  1568. /* Receive FIFO overflow is harmless and the hardware will take
  1569. care of it, just some packets are lost. Who cares. */
  1570. printk(KERN_DEBUG "%s: Happy Meal receive FIFO overflow.\n", hp->dev->name);
  1571. }
  1572. if (status & GREG_STAT_STSTERR) {
  1573. /* BigMAC SQE link test failed. */
  1574. printk(KERN_ERR "%s: Happy Meal BigMAC SQE test failed.\n", hp->dev->name);
  1575. reset = 1;
  1576. }
  1577. if (status & GREG_STAT_TFIFO_UND) {
  1578. /* Transmit FIFO underrun, again DMA error likely. */
  1579. printk(KERN_ERR "%s: Happy Meal transmitter FIFO underrun, DMA error.\n",
  1580. hp->dev->name);
  1581. reset = 1;
  1582. }
  1583. if (status & GREG_STAT_MAXPKTERR) {
  1584. /* Driver error, tried to transmit something larger
  1585. * than ethernet max mtu.
  1586. */
  1587. printk(KERN_ERR "%s: Happy Meal MAX Packet size error.\n", hp->dev->name);
  1588. reset = 1;
  1589. }
  1590. if (status & GREG_STAT_NORXD) {
  1591. /* This is harmless, it just means the system is
  1592. * quite loaded and the incoming packet rate was
  1593. * faster than the interrupt handler could keep up
  1594. * with.
  1595. */
  1596. printk(KERN_INFO "%s: Happy Meal out of receive "
  1597. "descriptors, packet dropped.\n",
  1598. hp->dev->name);
  1599. }
  1600. if (status & (GREG_STAT_RXERR|GREG_STAT_RXPERR|GREG_STAT_RXTERR)) {
  1601. /* All sorts of DMA receive errors. */
  1602. printk(KERN_ERR "%s: Happy Meal rx DMA errors [ ", hp->dev->name);
  1603. if (status & GREG_STAT_RXERR)
  1604. printk("GenericError ");
  1605. if (status & GREG_STAT_RXPERR)
  1606. printk("ParityError ");
  1607. if (status & GREG_STAT_RXTERR)
  1608. printk("RxTagBotch ");
  1609. printk("]\n");
  1610. reset = 1;
  1611. }
  1612. if (status & GREG_STAT_EOPERR) {
  1613. /* Driver bug, didn't set EOP bit in tx descriptor given
  1614. * to the happy meal.
  1615. */
  1616. printk(KERN_ERR "%s: EOP not set in happy meal transmit descriptor!\n",
  1617. hp->dev->name);
  1618. reset = 1;
  1619. }
  1620. if (status & GREG_STAT_MIFIRQ) {
  1621. /* MIF signalled an interrupt, were we polling it? */
  1622. printk(KERN_ERR "%s: Happy Meal MIF interrupt.\n", hp->dev->name);
  1623. }
  1624. if (status &
  1625. (GREG_STAT_TXEACK|GREG_STAT_TXLERR|GREG_STAT_TXPERR|GREG_STAT_TXTERR)) {
  1626. /* All sorts of transmit DMA errors. */
  1627. printk(KERN_ERR "%s: Happy Meal tx DMA errors [ ", hp->dev->name);
  1628. if (status & GREG_STAT_TXEACK)
  1629. printk("GenericError ");
  1630. if (status & GREG_STAT_TXLERR)
  1631. printk("LateError ");
  1632. if (status & GREG_STAT_TXPERR)
  1633. printk("ParityErro ");
  1634. if (status & GREG_STAT_TXTERR)
  1635. printk("TagBotch ");
  1636. printk("]\n");
  1637. reset = 1;
  1638. }
  1639. if (status & (GREG_STAT_SLVERR|GREG_STAT_SLVPERR)) {
  1640. /* Bus or parity error when cpu accessed happy meal registers
  1641. * or it's internal FIFO's. Should never see this.
  1642. */
  1643. printk(KERN_ERR "%s: Happy Meal register access SBUS slave (%s) error.\n",
  1644. hp->dev->name,
  1645. (status & GREG_STAT_SLVPERR) ? "parity" : "generic");
  1646. reset = 1;
  1647. }
  1648. if (reset) {
  1649. printk(KERN_NOTICE "%s: Resetting...\n", hp->dev->name);
  1650. happy_meal_init(hp);
  1651. return 1;
  1652. }
  1653. return 0;
  1654. }
  1655. /* hp->happy_lock must be held */
  1656. static void happy_meal_mif_interrupt(struct happy_meal *hp)
  1657. {
  1658. void __iomem *tregs = hp->tcvregs;
  1659. printk(KERN_INFO "%s: Link status change.\n", hp->dev->name);
  1660. hp->sw_bmcr = happy_meal_tcvr_read(hp, tregs, MII_BMCR);
  1661. hp->sw_lpa = happy_meal_tcvr_read(hp, tregs, MII_LPA);
  1662. /* Use the fastest transmission protocol possible. */
  1663. if (hp->sw_lpa & LPA_100FULL) {
  1664. printk(KERN_INFO "%s: Switching to 100Mbps at full duplex.", hp->dev->name);
  1665. hp->sw_bmcr |= (BMCR_FULLDPLX | BMCR_SPEED100);
  1666. } else if (hp->sw_lpa & LPA_100HALF) {
  1667. printk(KERN_INFO "%s: Switching to 100MBps at half duplex.", hp->dev->name);
  1668. hp->sw_bmcr |= BMCR_SPEED100;
  1669. } else if (hp->sw_lpa & LPA_10FULL) {
  1670. printk(KERN_INFO "%s: Switching to 10MBps at full duplex.", hp->dev->name);
  1671. hp->sw_bmcr |= BMCR_FULLDPLX;
  1672. } else {
  1673. printk(KERN_INFO "%s: Using 10Mbps at half duplex.", hp->dev->name);
  1674. }
  1675. happy_meal_tcvr_write(hp, tregs, MII_BMCR, hp->sw_bmcr);
  1676. /* Finally stop polling and shut up the MIF. */
  1677. happy_meal_poll_stop(hp, tregs);
  1678. }
  1679. #ifdef TXDEBUG
  1680. #define TXD(x) printk x
  1681. #else
  1682. #define TXD(x)
  1683. #endif
  1684. /* hp->happy_lock must be held */
  1685. static void happy_meal_tx(struct happy_meal *hp)
  1686. {
  1687. struct happy_meal_txd *txbase = &hp->happy_block->happy_meal_txd[0];
  1688. struct happy_meal_txd *this;
  1689. struct net_device *dev = hp->dev;
  1690. int elem;
  1691. elem = hp->tx_old;
  1692. TXD(("TX<"));
  1693. while (elem != hp->tx_new) {
  1694. struct sk_buff *skb;
  1695. u32 flags, dma_addr, dma_len;
  1696. int frag;
  1697. TXD(("[%d]", elem));
  1698. this = &txbase[elem];
  1699. flags = hme_read_desc32(hp, &this->tx_flags);
  1700. if (flags & TXFLAG_OWN)
  1701. break;
  1702. skb = hp->tx_skbs[elem];
  1703. if (skb_shinfo(skb)->nr_frags) {
  1704. int last;
  1705. last = elem + skb_shinfo(skb)->nr_frags;
  1706. last &= (TX_RING_SIZE - 1);
  1707. flags = hme_read_desc32(hp, &txbase[last].tx_flags);
  1708. if (flags & TXFLAG_OWN)
  1709. break;
  1710. }
  1711. hp->tx_skbs[elem] = NULL;
  1712. hp->net_stats.tx_bytes += skb->len;
  1713. for (frag = 0; frag <= skb_shinfo(skb)->nr_frags; frag++) {
  1714. dma_addr = hme_read_desc32(hp, &this->tx_addr);
  1715. dma_len = hme_read_desc32(hp, &this->tx_flags);
  1716. dma_len &= TXFLAG_SIZE;
  1717. hme_dma_unmap(hp, dma_addr, dma_len, DMA_TODEVICE);
  1718. elem = NEXT_TX(elem);
  1719. this = &txbase[elem];
  1720. }
  1721. dev_kfree_skb_irq(skb);
  1722. hp->net_stats.tx_packets++;
  1723. }
  1724. hp->tx_old = elem;
  1725. TXD((">"));
  1726. if (netif_queue_stopped(dev) &&
  1727. TX_BUFFS_AVAIL(hp) > (MAX_SKB_FRAGS + 1))
  1728. netif_wake_queue(dev);
  1729. }
  1730. #ifdef RXDEBUG
  1731. #define RXD(x) printk x
  1732. #else
  1733. #define RXD(x)
  1734. #endif
  1735. /* Originally I used to handle the allocation failure by just giving back just
  1736. * that one ring buffer to the happy meal. Problem is that usually when that
  1737. * condition is triggered, the happy meal expects you to do something reasonable
  1738. * with all of the packets it has DMA'd in. So now I just drop the entire
  1739. * ring when we cannot get a new skb and give them all back to the happy meal,
  1740. * maybe things will be "happier" now.
  1741. *
  1742. * hp->happy_lock must be held
  1743. */
  1744. static void happy_meal_rx(struct happy_meal *hp, struct net_device *dev)
  1745. {
  1746. struct happy_meal_rxd *rxbase = &hp->happy_block->happy_meal_rxd[0];
  1747. struct happy_meal_rxd *this;
  1748. int elem = hp->rx_new, drops = 0;
  1749. u32 flags;
  1750. RXD(("RX<"));
  1751. this = &rxbase[elem];
  1752. while (!((flags = hme_read_desc32(hp, &this->rx_flags)) & RXFLAG_OWN)) {
  1753. struct sk_buff *skb;
  1754. int len = flags >> 16;
  1755. u16 csum = flags & RXFLAG_CSUM;
  1756. u32 dma_addr = hme_read_desc32(hp, &this->rx_addr);
  1757. RXD(("[%d ", elem));
  1758. /* Check for errors. */
  1759. if ((len < ETH_ZLEN) || (flags & RXFLAG_OVERFLOW)) {
  1760. RXD(("ERR(%08x)]", flags));
  1761. hp->net_stats.rx_errors++;
  1762. if (len < ETH_ZLEN)
  1763. hp->net_stats.rx_length_errors++;
  1764. if (len & (RXFLAG_OVERFLOW >> 16)) {
  1765. hp->net_stats.rx_over_errors++;
  1766. hp->net_stats.rx_fifo_errors++;
  1767. }
  1768. /* Return it to the Happy meal. */
  1769. drop_it:
  1770. hp->net_stats.rx_dropped++;
  1771. hme_write_rxd(hp, this,
  1772. (RXFLAG_OWN|((RX_BUF_ALLOC_SIZE-RX_OFFSET)<<16)),
  1773. dma_addr);
  1774. goto next;
  1775. }
  1776. skb = hp->rx_skbs[elem];
  1777. if (len > RX_COPY_THRESHOLD) {
  1778. struct sk_buff *new_skb;
  1779. /* Now refill the entry, if we can. */
  1780. new_skb = happy_meal_alloc_skb(RX_BUF_ALLOC_SIZE, GFP_ATOMIC);
  1781. if (new_skb == NULL) {
  1782. drops++;
  1783. goto drop_it;
  1784. }
  1785. hme_dma_unmap(hp, dma_addr, RX_BUF_ALLOC_SIZE, DMA_FROMDEVICE);
  1786. hp->rx_skbs[elem] = new_skb;
  1787. new_skb->dev = dev;
  1788. skb_put(new_skb, (ETH_FRAME_LEN + RX_OFFSET + 4));
  1789. hme_write_rxd(hp, this,
  1790. (RXFLAG_OWN|((RX_BUF_ALLOC_SIZE-RX_OFFSET)<<16)),
  1791. hme_dma_map(hp, new_skb->data, RX_BUF_ALLOC_SIZE, DMA_FROMDEVICE));
  1792. skb_reserve(new_skb, RX_OFFSET);
  1793. /* Trim the original skb for the netif. */
  1794. skb_trim(skb, len);
  1795. } else {
  1796. struct sk_buff *copy_skb = dev_alloc_skb(len + 2);
  1797. if (copy_skb == NULL) {
  1798. drops++;
  1799. goto drop_it;
  1800. }
  1801. skb_reserve(copy_skb, 2);
  1802. skb_put(copy_skb, len);
  1803. hme_dma_sync_for_cpu(hp, dma_addr, len, DMA_FROMDEVICE);
  1804. skb_copy_from_linear_data(skb, copy_skb->data, len);
  1805. hme_dma_sync_for_device(hp, dma_addr, len, DMA_FROMDEVICE);
  1806. /* Reuse original ring buffer. */
  1807. hme_write_rxd(hp, this,
  1808. (RXFLAG_OWN|((RX_BUF_ALLOC_SIZE-RX_OFFSET)<<16)),
  1809. dma_addr);
  1810. skb = copy_skb;
  1811. }
  1812. /* This card is _fucking_ hot... */
  1813. skb->csum = csum_unfold(~(__force __sum16)htons(csum));
  1814. skb->ip_summed = CHECKSUM_COMPLETE;
  1815. RXD(("len=%d csum=%4x]", len, csum));
  1816. skb->protocol = eth_type_trans(skb, dev);
  1817. netif_rx(skb);
  1818. dev->last_rx = jiffies;
  1819. hp->net_stats.rx_packets++;
  1820. hp->net_stats.rx_bytes += len;
  1821. next:
  1822. elem = NEXT_RX(elem);
  1823. this = &rxbase[elem];
  1824. }
  1825. hp->rx_new = elem;
  1826. if (drops)
  1827. printk(KERN_INFO "%s: Memory squeeze, deferring packet.\n", hp->dev->name);
  1828. RXD((">"));
  1829. }
  1830. static irqreturn_t happy_meal_interrupt(int irq, void *dev_id)
  1831. {
  1832. struct net_device *dev = dev_id;
  1833. struct happy_meal *hp = netdev_priv(dev);
  1834. u32 happy_status = hme_read32(hp, hp->gregs + GREG_STAT);
  1835. HMD(("happy_meal_interrupt: status=%08x ", happy_status));
  1836. spin_lock(&hp->happy_lock);
  1837. if (happy_status & GREG_STAT_ERRORS) {
  1838. HMD(("ERRORS "));
  1839. if (happy_meal_is_not_so_happy(hp, /* un- */ happy_status))
  1840. goto out;
  1841. }
  1842. if (happy_status & GREG_STAT_MIFIRQ) {
  1843. HMD(("MIFIRQ "));
  1844. happy_meal_mif_interrupt(hp);
  1845. }
  1846. if (happy_status & GREG_STAT_TXALL) {
  1847. HMD(("TXALL "));
  1848. happy_meal_tx(hp);
  1849. }
  1850. if (happy_status & GREG_STAT_RXTOHOST) {
  1851. HMD(("RXTOHOST "));
  1852. happy_meal_rx(hp, dev);
  1853. }
  1854. HMD(("done\n"));
  1855. out:
  1856. spin_unlock(&hp->happy_lock);
  1857. return IRQ_HANDLED;
  1858. }
  1859. #ifdef CONFIG_SBUS
  1860. static irqreturn_t quattro_sbus_interrupt(int irq, void *cookie)
  1861. {
  1862. struct quattro *qp = (struct quattro *) cookie;
  1863. int i;
  1864. for (i = 0; i < 4; i++) {
  1865. struct net_device *dev = qp->happy_meals[i];
  1866. struct happy_meal *hp = dev->priv;
  1867. u32 happy_status = hme_read32(hp, hp->gregs + GREG_STAT);
  1868. HMD(("quattro_interrupt: status=%08x ", happy_status));
  1869. if (!(happy_status & (GREG_STAT_ERRORS |
  1870. GREG_STAT_MIFIRQ |
  1871. GREG_STAT_TXALL |
  1872. GREG_STAT_RXTOHOST)))
  1873. continue;
  1874. spin_lock(&hp->happy_lock);
  1875. if (happy_status & GREG_STAT_ERRORS) {
  1876. HMD(("ERRORS "));
  1877. if (happy_meal_is_not_so_happy(hp, happy_status))
  1878. goto next;
  1879. }
  1880. if (happy_status & GREG_STAT_MIFIRQ) {
  1881. HMD(("MIFIRQ "));
  1882. happy_meal_mif_interrupt(hp);
  1883. }
  1884. if (happy_status & GREG_STAT_TXALL) {
  1885. HMD(("TXALL "));
  1886. happy_meal_tx(hp);
  1887. }
  1888. if (happy_status & GREG_STAT_RXTOHOST) {
  1889. HMD(("RXTOHOST "));
  1890. happy_meal_rx(hp, dev);
  1891. }
  1892. next:
  1893. spin_unlock(&hp->happy_lock);
  1894. }
  1895. HMD(("done\n"));
  1896. return IRQ_HANDLED;
  1897. }
  1898. #endif
  1899. static int happy_meal_open(struct net_device *dev)
  1900. {
  1901. struct happy_meal *hp = dev->priv;
  1902. int res;
  1903. HMD(("happy_meal_open: "));
  1904. /* On SBUS Quattro QFE cards, all hme interrupts are concentrated
  1905. * into a single source which we register handling at probe time.
  1906. */
  1907. if ((hp->happy_flags & (HFLAG_QUATTRO|HFLAG_PCI)) != HFLAG_QUATTRO) {
  1908. if (request_irq(dev->irq, &happy_meal_interrupt,
  1909. IRQF_SHARED, dev->name, (void *)dev)) {
  1910. HMD(("EAGAIN\n"));
  1911. printk(KERN_ERR "happy_meal(SBUS): Can't order irq %d to go.\n",
  1912. dev->irq);
  1913. return -EAGAIN;
  1914. }
  1915. }
  1916. HMD(("to happy_meal_init\n"));
  1917. spin_lock_irq(&hp->happy_lock);
  1918. res = happy_meal_init(hp);
  1919. spin_unlock_irq(&hp->happy_lock);
  1920. if (res && ((hp->happy_flags & (HFLAG_QUATTRO|HFLAG_PCI)) != HFLAG_QUATTRO))
  1921. free_irq(dev->irq, dev);
  1922. return res;
  1923. }
  1924. static int happy_meal_close(struct net_device *dev)
  1925. {
  1926. struct happy_meal *hp = dev->priv;
  1927. spin_lock_irq(&hp->happy_lock);
  1928. happy_meal_stop(hp, hp->gregs);
  1929. happy_meal_clean_rings(hp);
  1930. /* If auto-negotiation timer is running, kill it. */
  1931. del_timer(&hp->happy_timer);
  1932. spin_unlock_irq(&hp->happy_lock);
  1933. /* On Quattro QFE cards, all hme interrupts are concentrated
  1934. * into a single source which we register handling at probe
  1935. * time and never unregister.
  1936. */
  1937. if ((hp->happy_flags & (HFLAG_QUATTRO|HFLAG_PCI)) != HFLAG_QUATTRO)
  1938. free_irq(dev->irq, dev);
  1939. return 0;
  1940. }
  1941. #ifdef SXDEBUG
  1942. #define SXD(x) printk x
  1943. #else
  1944. #define SXD(x)
  1945. #endif
  1946. static void happy_meal_tx_timeout(struct net_device *dev)
  1947. {
  1948. struct happy_meal *hp = dev->priv;
  1949. printk (KERN_ERR "%s: transmit timed out, resetting\n", dev->name);
  1950. tx_dump_log();
  1951. printk (KERN_ERR "%s: Happy Status %08x TX[%08x:%08x]\n", dev->name,
  1952. hme_read32(hp, hp->gregs + GREG_STAT),
  1953. hme_read32(hp, hp->etxregs + ETX_CFG),
  1954. hme_read32(hp, hp->bigmacregs + BMAC_TXCFG));
  1955. spin_lock_irq(&hp->happy_lock);
  1956. happy_meal_init(hp);
  1957. spin_unlock_irq(&hp->happy_lock);
  1958. netif_wake_queue(dev);
  1959. }
  1960. static int happy_meal_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1961. {
  1962. struct happy_meal *hp = dev->priv;
  1963. int entry;
  1964. u32 tx_flags;
  1965. tx_flags = TXFLAG_OWN;
  1966. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1967. const u32 csum_start_off = skb_transport_offset(skb);
  1968. const u32 csum_stuff_off = csum_start_off + skb->csum_offset;
  1969. tx_flags = (TXFLAG_OWN | TXFLAG_CSENABLE |
  1970. ((csum_start_off << 14) & TXFLAG_CSBUFBEGIN) |
  1971. ((csum_stuff_off << 20) & TXFLAG_CSLOCATION));
  1972. }
  1973. spin_lock_irq(&hp->happy_lock);
  1974. if (TX_BUFFS_AVAIL(hp) <= (skb_shinfo(skb)->nr_frags + 1)) {
  1975. netif_stop_queue(dev);
  1976. spin_unlock_irq(&hp->happy_lock);
  1977. printk(KERN_ERR "%s: BUG! Tx Ring full when queue awake!\n",
  1978. dev->name);
  1979. return 1;
  1980. }
  1981. entry = hp->tx_new;
  1982. SXD(("SX<l[%d]e[%d]>", len, entry));
  1983. hp->tx_skbs[entry] = skb;
  1984. if (skb_shinfo(skb)->nr_frags == 0) {
  1985. u32 mapping, len;
  1986. len = skb->len;
  1987. mapping = hme_dma_map(hp, skb->data, len, DMA_TODEVICE);
  1988. tx_flags |= (TXFLAG_SOP | TXFLAG_EOP);
  1989. hme_write_txd(hp, &hp->happy_block->happy_meal_txd[entry],
  1990. (tx_flags | (len & TXFLAG_SIZE)),
  1991. mapping);
  1992. entry = NEXT_TX(entry);
  1993. } else {
  1994. u32 first_len, first_mapping;
  1995. int frag, first_entry = entry;
  1996. /* We must give this initial chunk to the device last.
  1997. * Otherwise we could race with the device.
  1998. */
  1999. first_len = skb_headlen(skb);
  2000. first_mapping = hme_dma_map(hp, skb->data, first_len, DMA_TODEVICE);
  2001. entry = NEXT_TX(entry);
  2002. for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
  2003. skb_frag_t *this_frag = &skb_shinfo(skb)->frags[frag];
  2004. u32 len, mapping, this_txflags;
  2005. len = this_frag->size;
  2006. mapping = hme_dma_map(hp,
  2007. ((void *) page_address(this_frag->page) +
  2008. this_frag->page_offset),
  2009. len, DMA_TODEVICE);
  2010. this_txflags = tx_flags;
  2011. if (frag == skb_shinfo(skb)->nr_frags - 1)
  2012. this_txflags |= TXFLAG_EOP;
  2013. hme_write_txd(hp, &hp->happy_block->happy_meal_txd[entry],
  2014. (this_txflags | (len & TXFLAG_SIZE)),
  2015. mapping);
  2016. entry = NEXT_TX(entry);
  2017. }
  2018. hme_write_txd(hp, &hp->happy_block->happy_meal_txd[first_entry],
  2019. (tx_flags | TXFLAG_SOP | (first_len & TXFLAG_SIZE)),
  2020. first_mapping);
  2021. }
  2022. hp->tx_new = entry;
  2023. if (TX_BUFFS_AVAIL(hp) <= (MAX_SKB_FRAGS + 1))
  2024. netif_stop_queue(dev);
  2025. /* Get it going. */
  2026. hme_write32(hp, hp->etxregs + ETX_PENDING, ETX_TP_DMAWAKEUP);
  2027. spin_unlock_irq(&hp->happy_lock);
  2028. dev->trans_start = jiffies;
  2029. tx_add_log(hp, TXLOG_ACTION_TXMIT, 0);
  2030. return 0;
  2031. }
  2032. static struct net_device_stats *happy_meal_get_stats(struct net_device *dev)
  2033. {
  2034. struct happy_meal *hp = dev->priv;
  2035. spin_lock_irq(&hp->happy_lock);
  2036. happy_meal_get_counters(hp, hp->bigmacregs);
  2037. spin_unlock_irq(&hp->happy_lock);
  2038. return &hp->net_stats;
  2039. }
  2040. static void happy_meal_set_multicast(struct net_device *dev)
  2041. {
  2042. struct happy_meal *hp = dev->priv;
  2043. void __iomem *bregs = hp->bigmacregs;
  2044. struct dev_mc_list *dmi = dev->mc_list;
  2045. char *addrs;
  2046. int i;
  2047. u32 crc;
  2048. spin_lock_irq(&hp->happy_lock);
  2049. netif_stop_queue(dev);
  2050. if ((dev->flags & IFF_ALLMULTI) || (dev->mc_count > 64)) {
  2051. hme_write32(hp, bregs + BMAC_HTABLE0, 0xffff);
  2052. hme_write32(hp, bregs + BMAC_HTABLE1, 0xffff);
  2053. hme_write32(hp, bregs + BMAC_HTABLE2, 0xffff);
  2054. hme_write32(hp, bregs + BMAC_HTABLE3, 0xffff);
  2055. } else if (dev->flags & IFF_PROMISC) {
  2056. hme_write32(hp, bregs + BMAC_RXCFG,
  2057. hme_read32(hp, bregs + BMAC_RXCFG) | BIGMAC_RXCFG_PMISC);
  2058. } else {
  2059. u16 hash_table[4];
  2060. for (i = 0; i < 4; i++)
  2061. hash_table[i] = 0;
  2062. for (i = 0; i < dev->mc_count; i++) {
  2063. addrs = dmi->dmi_addr;
  2064. dmi = dmi->next;
  2065. if (!(*addrs & 1))
  2066. continue;
  2067. crc = ether_crc_le(6, addrs);
  2068. crc >>= 26;
  2069. hash_table[crc >> 4] |= 1 << (crc & 0xf);
  2070. }
  2071. hme_write32(hp, bregs + BMAC_HTABLE0, hash_table[0]);
  2072. hme_write32(hp, bregs + BMAC_HTABLE1, hash_table[1]);
  2073. hme_write32(hp, bregs + BMAC_HTABLE2, hash_table[2]);
  2074. hme_write32(hp, bregs + BMAC_HTABLE3, hash_table[3]);
  2075. }
  2076. netif_wake_queue(dev);
  2077. spin_unlock_irq(&hp->happy_lock);
  2078. }
  2079. /* Ethtool support... */
  2080. static int hme_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  2081. {
  2082. struct happy_meal *hp = dev->priv;
  2083. cmd->supported =
  2084. (SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
  2085. SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
  2086. SUPPORTED_Autoneg | SUPPORTED_TP | SUPPORTED_MII);
  2087. /* XXX hardcoded stuff for now */
  2088. cmd->port = PORT_TP; /* XXX no MII support */
  2089. cmd->transceiver = XCVR_INTERNAL; /* XXX no external xcvr support */
  2090. cmd->phy_address = 0; /* XXX fixed PHYAD */
  2091. /* Record PHY settings. */
  2092. spin_lock_irq(&hp->happy_lock);
  2093. hp->sw_bmcr = happy_meal_tcvr_read(hp, hp->tcvregs, MII_BMCR);
  2094. hp->sw_lpa = happy_meal_tcvr_read(hp, hp->tcvregs, MII_LPA);
  2095. spin_unlock_irq(&hp->happy_lock);
  2096. if (hp->sw_bmcr & BMCR_ANENABLE) {
  2097. cmd->autoneg = AUTONEG_ENABLE;
  2098. cmd->speed =
  2099. (hp->sw_lpa & (LPA_100HALF | LPA_100FULL)) ?
  2100. SPEED_100 : SPEED_10;
  2101. if (cmd->speed == SPEED_100)
  2102. cmd->duplex =
  2103. (hp->sw_lpa & (LPA_100FULL)) ?
  2104. DUPLEX_FULL : DUPLEX_HALF;
  2105. else
  2106. cmd->duplex =
  2107. (hp->sw_lpa & (LPA_10FULL)) ?
  2108. DUPLEX_FULL : DUPLEX_HALF;
  2109. } else {
  2110. cmd->autoneg = AUTONEG_DISABLE;
  2111. cmd->speed =
  2112. (hp->sw_bmcr & BMCR_SPEED100) ?
  2113. SPEED_100 : SPEED_10;
  2114. cmd->duplex =
  2115. (hp->sw_bmcr & BMCR_FULLDPLX) ?
  2116. DUPLEX_FULL : DUPLEX_HALF;
  2117. }
  2118. return 0;
  2119. }
  2120. static int hme_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  2121. {
  2122. struct happy_meal *hp = dev->priv;
  2123. /* Verify the settings we care about. */
  2124. if (cmd->autoneg != AUTONEG_ENABLE &&
  2125. cmd->autoneg != AUTONEG_DISABLE)
  2126. return -EINVAL;
  2127. if (cmd->autoneg == AUTONEG_DISABLE &&
  2128. ((cmd->speed != SPEED_100 &&
  2129. cmd->speed != SPEED_10) ||
  2130. (cmd->duplex != DUPLEX_HALF &&
  2131. cmd->duplex != DUPLEX_FULL)))
  2132. return -EINVAL;
  2133. /* Ok, do it to it. */
  2134. spin_lock_irq(&hp->happy_lock);
  2135. del_timer(&hp->happy_timer);
  2136. happy_meal_begin_auto_negotiation(hp, hp->tcvregs, cmd);
  2137. spin_unlock_irq(&hp->happy_lock);
  2138. return 0;
  2139. }
  2140. static void hme_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  2141. {
  2142. struct happy_meal *hp = dev->priv;
  2143. strcpy(info->driver, "sunhme");
  2144. strcpy(info->version, "2.02");
  2145. if (hp->happy_flags & HFLAG_PCI) {
  2146. struct pci_dev *pdev = hp->happy_dev;
  2147. strcpy(info->bus_info, pci_name(pdev));
  2148. }
  2149. #ifdef CONFIG_SBUS
  2150. else {
  2151. struct sbus_dev *sdev = hp->happy_dev;
  2152. sprintf(info->bus_info, "SBUS:%d",
  2153. sdev->slot);
  2154. }
  2155. #endif
  2156. }
  2157. static u32 hme_get_link(struct net_device *dev)
  2158. {
  2159. struct happy_meal *hp = dev->priv;
  2160. spin_lock_irq(&hp->happy_lock);
  2161. hp->sw_bmcr = happy_meal_tcvr_read(hp, hp->tcvregs, MII_BMCR);
  2162. spin_unlock_irq(&hp->happy_lock);
  2163. return (hp->sw_bmsr & BMSR_LSTATUS);
  2164. }
  2165. static const struct ethtool_ops hme_ethtool_ops = {
  2166. .get_settings = hme_get_settings,
  2167. .set_settings = hme_set_settings,
  2168. .get_drvinfo = hme_get_drvinfo,
  2169. .get_link = hme_get_link,
  2170. };
  2171. static int hme_version_printed;
  2172. #ifdef CONFIG_SBUS
  2173. void __devinit quattro_get_ranges(struct quattro *qp)
  2174. {
  2175. struct sbus_dev *sdev = qp->quattro_dev;
  2176. int err;
  2177. err = prom_getproperty(sdev->prom_node,
  2178. "ranges",
  2179. (char *)&qp->ranges[0],
  2180. sizeof(qp->ranges));
  2181. if (err == 0 || err == -1) {
  2182. qp->nranges = 0;
  2183. return;
  2184. }
  2185. qp->nranges = (err / sizeof(struct linux_prom_ranges));
  2186. }
  2187. static void __devinit quattro_apply_ranges(struct quattro *qp, struct happy_meal *hp)
  2188. {
  2189. struct sbus_dev *sdev = hp->happy_dev;
  2190. int rng;
  2191. for (rng = 0; rng < qp->nranges; rng++) {
  2192. struct linux_prom_ranges *rngp = &qp->ranges[rng];
  2193. int reg;
  2194. for (reg = 0; reg < 5; reg++) {
  2195. if (sdev->reg_addrs[reg].which_io ==
  2196. rngp->ot_child_space)
  2197. break;
  2198. }
  2199. if (reg == 5)
  2200. continue;
  2201. sdev->reg_addrs[reg].which_io = rngp->ot_parent_space;
  2202. sdev->reg_addrs[reg].phys_addr += rngp->ot_parent_base;
  2203. }
  2204. }
  2205. /* Given a happy meal sbus device, find it's quattro parent.
  2206. * If none exist, allocate and return a new one.
  2207. *
  2208. * Return NULL on failure.
  2209. */
  2210. static struct quattro * __devinit quattro_sbus_find(struct sbus_dev *goal_sdev)
  2211. {
  2212. struct sbus_dev *sdev;
  2213. struct quattro *qp;
  2214. int i;
  2215. for (qp = qfe_sbus_list; qp != NULL; qp = qp->next) {
  2216. for (i = 0, sdev = qp->quattro_dev;
  2217. (sdev != NULL) && (i < 4);
  2218. sdev = sdev->next, i++) {
  2219. if (sdev == goal_sdev)
  2220. return qp;
  2221. }
  2222. }
  2223. qp = kmalloc(sizeof(struct quattro), GFP_KERNEL);
  2224. if (qp != NULL) {
  2225. int i;
  2226. for (i = 0; i < 4; i++)
  2227. qp->happy_meals[i] = NULL;
  2228. qp->quattro_dev = goal_sdev;
  2229. qp->next = qfe_sbus_list;
  2230. qfe_sbus_list = qp;
  2231. quattro_get_ranges(qp);
  2232. }
  2233. return qp;
  2234. }
  2235. /* After all quattro cards have been probed, we call these functions
  2236. * to register the IRQ handlers.
  2237. */
  2238. static void __init quattro_sbus_register_irqs(void)
  2239. {
  2240. struct quattro *qp;
  2241. for (qp = qfe_sbus_list; qp != NULL; qp = qp->next) {
  2242. struct sbus_dev *sdev = qp->quattro_dev;
  2243. int err;
  2244. err = request_irq(sdev->irqs[0],
  2245. quattro_sbus_interrupt,
  2246. IRQF_SHARED, "Quattro",
  2247. qp);
  2248. if (err != 0) {
  2249. printk(KERN_ERR "Quattro: Fatal IRQ registery error %d.\n", err);
  2250. panic("QFE request irq");
  2251. }
  2252. }
  2253. }
  2254. static void quattro_sbus_free_irqs(void)
  2255. {
  2256. struct quattro *qp;
  2257. for (qp = qfe_sbus_list; qp != NULL; qp = qp->next) {
  2258. struct sbus_dev *sdev = qp->quattro_dev;
  2259. free_irq(sdev->irqs[0], qp);
  2260. }
  2261. }
  2262. #endif /* CONFIG_SBUS */
  2263. #ifdef CONFIG_PCI
  2264. static struct quattro * __devinit quattro_pci_find(struct pci_dev *pdev)
  2265. {
  2266. struct pci_dev *bdev = pdev->bus->self;
  2267. struct quattro *qp;
  2268. if (!bdev) return NULL;
  2269. for (qp = qfe_pci_list; qp != NULL; qp = qp->next) {
  2270. struct pci_dev *qpdev = qp->quattro_dev;
  2271. if (qpdev == bdev)
  2272. return qp;
  2273. }
  2274. qp = kmalloc(sizeof(struct quattro), GFP_KERNEL);
  2275. if (qp != NULL) {
  2276. int i;
  2277. for (i = 0; i < 4; i++)
  2278. qp->happy_meals[i] = NULL;
  2279. qp->quattro_dev = bdev;
  2280. qp->next = qfe_pci_list;
  2281. qfe_pci_list = qp;
  2282. /* No range tricks necessary on PCI. */
  2283. qp->nranges = 0;
  2284. }
  2285. return qp;
  2286. }
  2287. #endif /* CONFIG_PCI */
  2288. #ifdef CONFIG_SBUS
  2289. static int __devinit happy_meal_sbus_probe_one(struct sbus_dev *sdev, int is_qfe)
  2290. {
  2291. struct device_node *dp = sdev->ofdev.node;
  2292. struct quattro *qp = NULL;
  2293. struct happy_meal *hp;
  2294. struct net_device *dev;
  2295. int i, qfe_slot = -1;
  2296. int err = -ENODEV;
  2297. DECLARE_MAC_BUF(mac);
  2298. if (is_qfe) {
  2299. qp = quattro_sbus_find(sdev);
  2300. if (qp == NULL)
  2301. goto err_out;
  2302. for (qfe_slot = 0; qfe_slot < 4; qfe_slot++)
  2303. if (qp->happy_meals[qfe_slot] == NULL)
  2304. break;
  2305. if (qfe_slot == 4)
  2306. goto err_out;
  2307. }
  2308. err = -ENOMEM;
  2309. dev = alloc_etherdev(sizeof(struct happy_meal));
  2310. if (!dev)
  2311. goto err_out;
  2312. SET_NETDEV_DEV(dev, &sdev->ofdev.dev);
  2313. if (hme_version_printed++ == 0)
  2314. printk(KERN_INFO "%s", version);
  2315. /* If user did not specify a MAC address specifically, use
  2316. * the Quattro local-mac-address property...
  2317. */
  2318. for (i = 0; i < 6; i++) {
  2319. if (macaddr[i] != 0)
  2320. break;
  2321. }
  2322. if (i < 6) { /* a mac address was given */
  2323. for (i = 0; i < 6; i++)
  2324. dev->dev_addr[i] = macaddr[i];
  2325. macaddr[5]++;
  2326. } else {
  2327. const unsigned char *addr;
  2328. int len;
  2329. addr = of_get_property(dp, "local-mac-address", &len);
  2330. if (qfe_slot != -1 && addr && len == 6)
  2331. memcpy(dev->dev_addr, addr, 6);
  2332. else
  2333. memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
  2334. }
  2335. hp = dev->priv;
  2336. hp->happy_dev = sdev;
  2337. spin_lock_init(&hp->happy_lock);
  2338. err = -ENODEV;
  2339. if (sdev->num_registers != 5) {
  2340. printk(KERN_ERR "happymeal: Device needs 5 regs, has %d.\n",
  2341. sdev->num_registers);
  2342. goto err_out_free_netdev;
  2343. }
  2344. if (qp != NULL) {
  2345. hp->qfe_parent = qp;
  2346. hp->qfe_ent = qfe_slot;
  2347. qp->happy_meals[qfe_slot] = dev;
  2348. quattro_apply_ranges(qp, hp);
  2349. }
  2350. hp->gregs = sbus_ioremap(&sdev->resource[0], 0,
  2351. GREG_REG_SIZE, "HME Global Regs");
  2352. if (!hp->gregs) {
  2353. printk(KERN_ERR "happymeal: Cannot map global registers.\n");
  2354. goto err_out_free_netdev;
  2355. }
  2356. hp->etxregs = sbus_ioremap(&sdev->resource[1], 0,
  2357. ETX_REG_SIZE, "HME TX Regs");
  2358. if (!hp->etxregs) {
  2359. printk(KERN_ERR "happymeal: Cannot map MAC TX registers.\n");
  2360. goto err_out_iounmap;
  2361. }
  2362. hp->erxregs = sbus_ioremap(&sdev->resource[2], 0,
  2363. ERX_REG_SIZE, "HME RX Regs");
  2364. if (!hp->erxregs) {
  2365. printk(KERN_ERR "happymeal: Cannot map MAC RX registers.\n");
  2366. goto err_out_iounmap;
  2367. }
  2368. hp->bigmacregs = sbus_ioremap(&sdev->resource[3], 0,
  2369. BMAC_REG_SIZE, "HME BIGMAC Regs");
  2370. if (!hp->bigmacregs) {
  2371. printk(KERN_ERR "happymeal: Cannot map BIGMAC registers.\n");
  2372. goto err_out_iounmap;
  2373. }
  2374. hp->tcvregs = sbus_ioremap(&sdev->resource[4], 0,
  2375. TCVR_REG_SIZE, "HME Tranceiver Regs");
  2376. if (!hp->tcvregs) {
  2377. printk(KERN_ERR "happymeal: Cannot map TCVR registers.\n");
  2378. goto err_out_iounmap;
  2379. }
  2380. hp->hm_revision = of_getintprop_default(dp, "hm-rev", 0xff);
  2381. if (hp->hm_revision == 0xff)
  2382. hp->hm_revision = 0xa0;
  2383. /* Now enable the feature flags we can. */
  2384. if (hp->hm_revision == 0x20 || hp->hm_revision == 0x21)
  2385. hp->happy_flags = HFLAG_20_21;
  2386. else if (hp->hm_revision != 0xa0)
  2387. hp->happy_flags = HFLAG_NOT_A0;
  2388. if (qp != NULL)
  2389. hp->happy_flags |= HFLAG_QUATTRO;
  2390. /* Get the supported DVMA burst sizes from our Happy SBUS. */
  2391. hp->happy_bursts = of_getintprop_default(sdev->bus->ofdev.node,
  2392. "burst-sizes", 0x00);
  2393. hp->happy_block = sbus_alloc_consistent(hp->happy_dev,
  2394. PAGE_SIZE,
  2395. &hp->hblock_dvma);
  2396. err = -ENOMEM;
  2397. if (!hp->happy_block) {
  2398. printk(KERN_ERR "happymeal: Cannot allocate descriptors.\n");
  2399. goto err_out_iounmap;
  2400. }
  2401. /* Force check of the link first time we are brought up. */
  2402. hp->linkcheck = 0;
  2403. /* Force timer state to 'asleep' with count of zero. */
  2404. hp->timer_state = asleep;
  2405. hp->timer_ticks = 0;
  2406. init_timer(&hp->happy_timer);
  2407. hp->dev = dev;
  2408. dev->open = &happy_meal_open;
  2409. dev->stop = &happy_meal_close;
  2410. dev->hard_start_xmit = &happy_meal_start_xmit;
  2411. dev->get_stats = &happy_meal_get_stats;
  2412. dev->set_multicast_list = &happy_meal_set_multicast;
  2413. dev->tx_timeout = &happy_meal_tx_timeout;
  2414. dev->watchdog_timeo = 5*HZ;
  2415. dev->ethtool_ops = &hme_ethtool_ops;
  2416. /* Happy Meal can do it all... */
  2417. dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
  2418. dev->irq = sdev->irqs[0];
  2419. #if defined(CONFIG_SBUS) && defined(CONFIG_PCI)
  2420. /* Hook up PCI register/dma accessors. */
  2421. hp->read_desc32 = sbus_hme_read_desc32;
  2422. hp->write_txd = sbus_hme_write_txd;
  2423. hp->write_rxd = sbus_hme_write_rxd;
  2424. hp->dma_map = (u32 (*)(void *, void *, long, int))sbus_map_single;
  2425. hp->dma_unmap = (void (*)(void *, u32, long, int))sbus_unmap_single;
  2426. hp->dma_sync_for_cpu = (void (*)(void *, u32, long, int))
  2427. sbus_dma_sync_single_for_cpu;
  2428. hp->dma_sync_for_device = (void (*)(void *, u32, long, int))
  2429. sbus_dma_sync_single_for_device;
  2430. hp->read32 = sbus_hme_read32;
  2431. hp->write32 = sbus_hme_write32;
  2432. #endif
  2433. /* Grrr, Happy Meal comes up by default not advertising
  2434. * full duplex 100baseT capabilities, fix this.
  2435. */
  2436. spin_lock_irq(&hp->happy_lock);
  2437. happy_meal_set_initial_advertisement(hp);
  2438. spin_unlock_irq(&hp->happy_lock);
  2439. if (register_netdev(hp->dev)) {
  2440. printk(KERN_ERR "happymeal: Cannot register net device, "
  2441. "aborting.\n");
  2442. goto err_out_free_consistent;
  2443. }
  2444. dev_set_drvdata(&sdev->ofdev.dev, hp);
  2445. if (qfe_slot != -1)
  2446. printk(KERN_INFO "%s: Quattro HME slot %d (SBUS) 10/100baseT Ethernet ",
  2447. dev->name, qfe_slot);
  2448. else
  2449. printk(KERN_INFO "%s: HAPPY MEAL (SBUS) 10/100baseT Ethernet ",
  2450. dev->name);
  2451. printk("%s\n", print_mac(mac, dev->dev_addr));
  2452. return 0;
  2453. err_out_free_consistent:
  2454. sbus_free_consistent(hp->happy_dev,
  2455. PAGE_SIZE,
  2456. hp->happy_block,
  2457. hp->hblock_dvma);
  2458. err_out_iounmap:
  2459. if (hp->gregs)
  2460. sbus_iounmap(hp->gregs, GREG_REG_SIZE);
  2461. if (hp->etxregs)
  2462. sbus_iounmap(hp->etxregs, ETX_REG_SIZE);
  2463. if (hp->erxregs)
  2464. sbus_iounmap(hp->erxregs, ERX_REG_SIZE);
  2465. if (hp->bigmacregs)
  2466. sbus_iounmap(hp->bigmacregs, BMAC_REG_SIZE);
  2467. if (hp->tcvregs)
  2468. sbus_iounmap(hp->tcvregs, TCVR_REG_SIZE);
  2469. err_out_free_netdev:
  2470. free_netdev(dev);
  2471. err_out:
  2472. return err;
  2473. }
  2474. #endif
  2475. #ifdef CONFIG_PCI
  2476. #ifndef CONFIG_SPARC
  2477. static int is_quattro_p(struct pci_dev *pdev)
  2478. {
  2479. struct pci_dev *busdev = pdev->bus->self;
  2480. struct list_head *tmp;
  2481. int n_hmes;
  2482. if (busdev == NULL ||
  2483. busdev->vendor != PCI_VENDOR_ID_DEC ||
  2484. busdev->device != PCI_DEVICE_ID_DEC_21153)
  2485. return 0;
  2486. n_hmes = 0;
  2487. tmp = pdev->bus->devices.next;
  2488. while (tmp != &pdev->bus->devices) {
  2489. struct pci_dev *this_pdev = pci_dev_b(tmp);
  2490. if (this_pdev->vendor == PCI_VENDOR_ID_SUN &&
  2491. this_pdev->device == PCI_DEVICE_ID_SUN_HAPPYMEAL)
  2492. n_hmes++;
  2493. tmp = tmp->next;
  2494. }
  2495. if (n_hmes != 4)
  2496. return 0;
  2497. return 1;
  2498. }
  2499. /* Fetch MAC address from vital product data of PCI ROM. */
  2500. static int find_eth_addr_in_vpd(void __iomem *rom_base, int len, int index, unsigned char *dev_addr)
  2501. {
  2502. int this_offset;
  2503. for (this_offset = 0x20; this_offset < len; this_offset++) {
  2504. void __iomem *p = rom_base + this_offset;
  2505. if (readb(p + 0) != 0x90 ||
  2506. readb(p + 1) != 0x00 ||
  2507. readb(p + 2) != 0x09 ||
  2508. readb(p + 3) != 0x4e ||
  2509. readb(p + 4) != 0x41 ||
  2510. readb(p + 5) != 0x06)
  2511. continue;
  2512. this_offset += 6;
  2513. p += 6;
  2514. if (index == 0) {
  2515. int i;
  2516. for (i = 0; i < 6; i++)
  2517. dev_addr[i] = readb(p + i);
  2518. return 1;
  2519. }
  2520. index--;
  2521. }
  2522. return 0;
  2523. }
  2524. static void get_hme_mac_nonsparc(struct pci_dev *pdev, unsigned char *dev_addr)
  2525. {
  2526. size_t size;
  2527. void __iomem *p = pci_map_rom(pdev, &size);
  2528. if (p) {
  2529. int index = 0;
  2530. int found;
  2531. if (is_quattro_p(pdev))
  2532. index = PCI_SLOT(pdev->devfn);
  2533. found = readb(p) == 0x55 &&
  2534. readb(p + 1) == 0xaa &&
  2535. find_eth_addr_in_vpd(p, (64 * 1024), index, dev_addr);
  2536. pci_unmap_rom(pdev, p);
  2537. if (found)
  2538. return;
  2539. }
  2540. /* Sun MAC prefix then 3 random bytes. */
  2541. dev_addr[0] = 0x08;
  2542. dev_addr[1] = 0x00;
  2543. dev_addr[2] = 0x20;
  2544. get_random_bytes(&dev_addr[3], 3);
  2545. return;
  2546. }
  2547. #endif /* !(CONFIG_SPARC) */
  2548. static int __devinit happy_meal_pci_probe(struct pci_dev *pdev,
  2549. const struct pci_device_id *ent)
  2550. {
  2551. struct quattro *qp = NULL;
  2552. #ifdef CONFIG_SPARC
  2553. struct device_node *dp;
  2554. #endif
  2555. struct happy_meal *hp;
  2556. struct net_device *dev;
  2557. void __iomem *hpreg_base;
  2558. unsigned long hpreg_res;
  2559. int i, qfe_slot = -1;
  2560. char prom_name[64];
  2561. int err;
  2562. DECLARE_MAC_BUF(mac);
  2563. /* Now make sure pci_dev cookie is there. */
  2564. #ifdef CONFIG_SPARC
  2565. dp = pci_device_to_OF_node(pdev);
  2566. strcpy(prom_name, dp->name);
  2567. #else
  2568. if (is_quattro_p(pdev))
  2569. strcpy(prom_name, "SUNW,qfe");
  2570. else
  2571. strcpy(prom_name, "SUNW,hme");
  2572. #endif
  2573. err = -ENODEV;
  2574. if (pci_enable_device(pdev))
  2575. goto err_out;
  2576. pci_set_master(pdev);
  2577. if (!strcmp(prom_name, "SUNW,qfe") || !strcmp(prom_name, "qfe")) {
  2578. qp = quattro_pci_find(pdev);
  2579. if (qp == NULL)
  2580. goto err_out;
  2581. for (qfe_slot = 0; qfe_slot < 4; qfe_slot++)
  2582. if (qp->happy_meals[qfe_slot] == NULL)
  2583. break;
  2584. if (qfe_slot == 4)
  2585. goto err_out;
  2586. }
  2587. dev = alloc_etherdev(sizeof(struct happy_meal));
  2588. err = -ENOMEM;
  2589. if (!dev)
  2590. goto err_out;
  2591. SET_NETDEV_DEV(dev, &pdev->dev);
  2592. if (hme_version_printed++ == 0)
  2593. printk(KERN_INFO "%s", version);
  2594. dev->base_addr = (long) pdev;
  2595. hp = (struct happy_meal *)dev->priv;
  2596. memset(hp, 0, sizeof(*hp));
  2597. hp->happy_dev = pdev;
  2598. spin_lock_init(&hp->happy_lock);
  2599. if (qp != NULL) {
  2600. hp->qfe_parent = qp;
  2601. hp->qfe_ent = qfe_slot;
  2602. qp->happy_meals[qfe_slot] = dev;
  2603. }
  2604. hpreg_res = pci_resource_start(pdev, 0);
  2605. err = -ENODEV;
  2606. if ((pci_resource_flags(pdev, 0) & IORESOURCE_IO) != 0) {
  2607. printk(KERN_ERR "happymeal(PCI): Cannot find proper PCI device base address.\n");
  2608. goto err_out_clear_quattro;
  2609. }
  2610. if (pci_request_regions(pdev, DRV_NAME)) {
  2611. printk(KERN_ERR "happymeal(PCI): Cannot obtain PCI resources, "
  2612. "aborting.\n");
  2613. goto err_out_clear_quattro;
  2614. }
  2615. if ((hpreg_base = ioremap(hpreg_res, 0x8000)) == NULL) {
  2616. printk(KERN_ERR "happymeal(PCI): Unable to remap card memory.\n");
  2617. goto err_out_free_res;
  2618. }
  2619. for (i = 0; i < 6; i++) {
  2620. if (macaddr[i] != 0)
  2621. break;
  2622. }
  2623. if (i < 6) { /* a mac address was given */
  2624. for (i = 0; i < 6; i++)
  2625. dev->dev_addr[i] = macaddr[i];
  2626. macaddr[5]++;
  2627. } else {
  2628. #ifdef CONFIG_SPARC
  2629. const unsigned char *addr;
  2630. int len;
  2631. if (qfe_slot != -1 &&
  2632. (addr = of_get_property(dp,
  2633. "local-mac-address", &len)) != NULL
  2634. && len == 6) {
  2635. memcpy(dev->dev_addr, addr, 6);
  2636. } else {
  2637. memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
  2638. }
  2639. #else
  2640. get_hme_mac_nonsparc(pdev, &dev->dev_addr[0]);
  2641. #endif
  2642. }
  2643. /* Layout registers. */
  2644. hp->gregs = (hpreg_base + 0x0000UL);
  2645. hp->etxregs = (hpreg_base + 0x2000UL);
  2646. hp->erxregs = (hpreg_base + 0x4000UL);
  2647. hp->bigmacregs = (hpreg_base + 0x6000UL);
  2648. hp->tcvregs = (hpreg_base + 0x7000UL);
  2649. #ifdef CONFIG_SPARC
  2650. hp->hm_revision = of_getintprop_default(dp, "hm-rev", 0xff);
  2651. if (hp->hm_revision == 0xff)
  2652. hp->hm_revision = 0xc0 | (pdev->revision & 0x0f);
  2653. #else
  2654. /* works with this on non-sparc hosts */
  2655. hp->hm_revision = 0x20;
  2656. #endif
  2657. /* Now enable the feature flags we can. */
  2658. if (hp->hm_revision == 0x20 || hp->hm_revision == 0x21)
  2659. hp->happy_flags = HFLAG_20_21;
  2660. else if (hp->hm_revision != 0xa0 && hp->hm_revision != 0xc0)
  2661. hp->happy_flags = HFLAG_NOT_A0;
  2662. if (qp != NULL)
  2663. hp->happy_flags |= HFLAG_QUATTRO;
  2664. /* And of course, indicate this is PCI. */
  2665. hp->happy_flags |= HFLAG_PCI;
  2666. #ifdef CONFIG_SPARC
  2667. /* Assume PCI happy meals can handle all burst sizes. */
  2668. hp->happy_bursts = DMA_BURSTBITS;
  2669. #endif
  2670. hp->happy_block = (struct hmeal_init_block *)
  2671. pci_alloc_consistent(pdev, PAGE_SIZE, &hp->hblock_dvma);
  2672. err = -ENODEV;
  2673. if (!hp->happy_block) {
  2674. printk(KERN_ERR "happymeal(PCI): Cannot get hme init block.\n");
  2675. goto err_out_iounmap;
  2676. }
  2677. hp->linkcheck = 0;
  2678. hp->timer_state = asleep;
  2679. hp->timer_ticks = 0;
  2680. init_timer(&hp->happy_timer);
  2681. hp->dev = dev;
  2682. dev->open = &happy_meal_open;
  2683. dev->stop = &happy_meal_close;
  2684. dev->hard_start_xmit = &happy_meal_start_xmit;
  2685. dev->get_stats = &happy_meal_get_stats;
  2686. dev->set_multicast_list = &happy_meal_set_multicast;
  2687. dev->tx_timeout = &happy_meal_tx_timeout;
  2688. dev->watchdog_timeo = 5*HZ;
  2689. dev->ethtool_ops = &hme_ethtool_ops;
  2690. dev->irq = pdev->irq;
  2691. dev->dma = 0;
  2692. /* Happy Meal can do it all... */
  2693. dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
  2694. #if defined(CONFIG_SBUS) && defined(CONFIG_PCI)
  2695. /* Hook up PCI register/dma accessors. */
  2696. hp->read_desc32 = pci_hme_read_desc32;
  2697. hp->write_txd = pci_hme_write_txd;
  2698. hp->write_rxd = pci_hme_write_rxd;
  2699. hp->dma_map = (u32 (*)(void *, void *, long, int))pci_map_single;
  2700. hp->dma_unmap = (void (*)(void *, u32, long, int))pci_unmap_single;
  2701. hp->dma_sync_for_cpu = (void (*)(void *, u32, long, int))
  2702. pci_dma_sync_single_for_cpu;
  2703. hp->dma_sync_for_device = (void (*)(void *, u32, long, int))
  2704. pci_dma_sync_single_for_device;
  2705. hp->read32 = pci_hme_read32;
  2706. hp->write32 = pci_hme_write32;
  2707. #endif
  2708. /* Grrr, Happy Meal comes up by default not advertising
  2709. * full duplex 100baseT capabilities, fix this.
  2710. */
  2711. spin_lock_irq(&hp->happy_lock);
  2712. happy_meal_set_initial_advertisement(hp);
  2713. spin_unlock_irq(&hp->happy_lock);
  2714. if (register_netdev(hp->dev)) {
  2715. printk(KERN_ERR "happymeal(PCI): Cannot register net device, "
  2716. "aborting.\n");
  2717. goto err_out_iounmap;
  2718. }
  2719. dev_set_drvdata(&pdev->dev, hp);
  2720. if (!qfe_slot) {
  2721. struct pci_dev *qpdev = qp->quattro_dev;
  2722. prom_name[0] = 0;
  2723. if (!strncmp(dev->name, "eth", 3)) {
  2724. int i = simple_strtoul(dev->name + 3, NULL, 10);
  2725. sprintf(prom_name, "-%d", i + 3);
  2726. }
  2727. printk(KERN_INFO "%s%s: Quattro HME (PCI/CheerIO) 10/100baseT Ethernet ", dev->name, prom_name);
  2728. if (qpdev->vendor == PCI_VENDOR_ID_DEC &&
  2729. qpdev->device == PCI_DEVICE_ID_DEC_21153)
  2730. printk("DEC 21153 PCI Bridge\n");
  2731. else
  2732. printk("unknown bridge %04x.%04x\n",
  2733. qpdev->vendor, qpdev->device);
  2734. }
  2735. if (qfe_slot != -1)
  2736. printk(KERN_INFO "%s: Quattro HME slot %d (PCI/CheerIO) 10/100baseT Ethernet ",
  2737. dev->name, qfe_slot);
  2738. else
  2739. printk(KERN_INFO "%s: HAPPY MEAL (PCI/CheerIO) 10/100BaseT Ethernet ",
  2740. dev->name);
  2741. printk("%s\n", print_mac(mac, dev->dev_addr));
  2742. return 0;
  2743. err_out_iounmap:
  2744. iounmap(hp->gregs);
  2745. err_out_free_res:
  2746. pci_release_regions(pdev);
  2747. err_out_clear_quattro:
  2748. if (qp != NULL)
  2749. qp->happy_meals[qfe_slot] = NULL;
  2750. free_netdev(dev);
  2751. err_out:
  2752. return err;
  2753. }
  2754. static void __devexit happy_meal_pci_remove(struct pci_dev *pdev)
  2755. {
  2756. struct happy_meal *hp = dev_get_drvdata(&pdev->dev);
  2757. struct net_device *net_dev = hp->dev;
  2758. unregister_netdev(net_dev);
  2759. pci_free_consistent(hp->happy_dev,
  2760. PAGE_SIZE,
  2761. hp->happy_block,
  2762. hp->hblock_dvma);
  2763. iounmap(hp->gregs);
  2764. pci_release_regions(hp->happy_dev);
  2765. free_netdev(net_dev);
  2766. dev_set_drvdata(&pdev->dev, NULL);
  2767. }
  2768. static struct pci_device_id happymeal_pci_ids[] = {
  2769. { PCI_DEVICE(PCI_VENDOR_ID_SUN, PCI_DEVICE_ID_SUN_HAPPYMEAL) },
  2770. { } /* Terminating entry */
  2771. };
  2772. MODULE_DEVICE_TABLE(pci, happymeal_pci_ids);
  2773. static struct pci_driver hme_pci_driver = {
  2774. .name = "hme",
  2775. .id_table = happymeal_pci_ids,
  2776. .probe = happy_meal_pci_probe,
  2777. .remove = __devexit_p(happy_meal_pci_remove),
  2778. };
  2779. static int __init happy_meal_pci_init(void)
  2780. {
  2781. return pci_register_driver(&hme_pci_driver);
  2782. }
  2783. static void happy_meal_pci_exit(void)
  2784. {
  2785. pci_unregister_driver(&hme_pci_driver);
  2786. while (qfe_pci_list) {
  2787. struct quattro *qfe = qfe_pci_list;
  2788. struct quattro *next = qfe->next;
  2789. kfree(qfe);
  2790. qfe_pci_list = next;
  2791. }
  2792. }
  2793. #endif
  2794. #ifdef CONFIG_SBUS
  2795. static int __devinit hme_sbus_probe(struct of_device *dev, const struct of_device_id *match)
  2796. {
  2797. struct sbus_dev *sdev = to_sbus_device(&dev->dev);
  2798. struct device_node *dp = dev->node;
  2799. const char *model = of_get_property(dp, "model", NULL);
  2800. int is_qfe = (match->data != NULL);
  2801. if (!is_qfe && model && !strcmp(model, "SUNW,sbus-qfe"))
  2802. is_qfe = 1;
  2803. return happy_meal_sbus_probe_one(sdev, is_qfe);
  2804. }
  2805. static int __devexit hme_sbus_remove(struct of_device *dev)
  2806. {
  2807. struct happy_meal *hp = dev_get_drvdata(&dev->dev);
  2808. struct net_device *net_dev = hp->dev;
  2809. unregister_netdev(net_dev);
  2810. /* XXX qfe parent interrupt... */
  2811. sbus_iounmap(hp->gregs, GREG_REG_SIZE);
  2812. sbus_iounmap(hp->etxregs, ETX_REG_SIZE);
  2813. sbus_iounmap(hp->erxregs, ERX_REG_SIZE);
  2814. sbus_iounmap(hp->bigmacregs, BMAC_REG_SIZE);
  2815. sbus_iounmap(hp->tcvregs, TCVR_REG_SIZE);
  2816. sbus_free_consistent(hp->happy_dev,
  2817. PAGE_SIZE,
  2818. hp->happy_block,
  2819. hp->hblock_dvma);
  2820. free_netdev(net_dev);
  2821. dev_set_drvdata(&dev->dev, NULL);
  2822. return 0;
  2823. }
  2824. static struct of_device_id hme_sbus_match[] = {
  2825. {
  2826. .name = "SUNW,hme",
  2827. },
  2828. {
  2829. .name = "SUNW,qfe",
  2830. .data = (void *) 1,
  2831. },
  2832. {
  2833. .name = "qfe",
  2834. .data = (void *) 1,
  2835. },
  2836. {},
  2837. };
  2838. MODULE_DEVICE_TABLE(of, hme_sbus_match);
  2839. static struct of_platform_driver hme_sbus_driver = {
  2840. .name = "hme",
  2841. .match_table = hme_sbus_match,
  2842. .probe = hme_sbus_probe,
  2843. .remove = __devexit_p(hme_sbus_remove),
  2844. };
  2845. static int __init happy_meal_sbus_init(void)
  2846. {
  2847. int err;
  2848. err = of_register_driver(&hme_sbus_driver, &sbus_bus_type);
  2849. if (!err)
  2850. quattro_sbus_register_irqs();
  2851. return err;
  2852. }
  2853. static void happy_meal_sbus_exit(void)
  2854. {
  2855. of_unregister_driver(&hme_sbus_driver);
  2856. quattro_sbus_free_irqs();
  2857. while (qfe_sbus_list) {
  2858. struct quattro *qfe = qfe_sbus_list;
  2859. struct quattro *next = qfe->next;
  2860. kfree(qfe);
  2861. qfe_sbus_list = next;
  2862. }
  2863. }
  2864. #endif
  2865. static int __init happy_meal_probe(void)
  2866. {
  2867. int err = 0;
  2868. #ifdef CONFIG_SBUS
  2869. err = happy_meal_sbus_init();
  2870. #endif
  2871. #ifdef CONFIG_PCI
  2872. if (!err) {
  2873. err = happy_meal_pci_init();
  2874. #ifdef CONFIG_SBUS
  2875. if (err)
  2876. happy_meal_sbus_exit();
  2877. #endif
  2878. }
  2879. #endif
  2880. return err;
  2881. }
  2882. static void __exit happy_meal_exit(void)
  2883. {
  2884. #ifdef CONFIG_SBUS
  2885. happy_meal_sbus_exit();
  2886. #endif
  2887. #ifdef CONFIG_PCI
  2888. happy_meal_pci_exit();
  2889. #endif
  2890. }
  2891. module_init(happy_meal_probe);
  2892. module_exit(happy_meal_exit);