sunhme.c 79 KB

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