sunhme.c 91 KB

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