sunhme.c 92 KB

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