sunhme.c 92 KB

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