sunhme.c 91 KB

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