sunhme.c 91 KB

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