tlan.c 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258
  1. /*******************************************************************************
  2. *
  3. * Linux ThunderLAN Driver
  4. *
  5. * tlan.c
  6. * by James Banks
  7. *
  8. * (C) 1997-1998 Caldera, Inc.
  9. * (C) 1998 James Banks
  10. * (C) 1999-2001 Torben Mathiasen
  11. * (C) 2002 Samuel Chessman
  12. *
  13. * This software may be used and distributed according to the terms
  14. * of the GNU General Public License, incorporated herein by reference.
  15. *
  16. ** Useful (if not required) reading:
  17. *
  18. * Texas Instruments, ThunderLAN Programmer's Guide,
  19. * TI Literature Number SPWU013A
  20. * available in PDF format from www.ti.com
  21. * Level One, LXT901 and LXT970 Data Sheets
  22. * available in PDF format from www.level1.com
  23. * National Semiconductor, DP83840A Data Sheet
  24. * available in PDF format from www.national.com
  25. * Microchip Technology, 24C01A/02A/04A Data Sheet
  26. * available in PDF format from www.microchip.com
  27. *
  28. ******************************************************************************/
  29. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  30. #include <linux/hardirq.h>
  31. #include <linux/module.h>
  32. #include <linux/init.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/ioport.h>
  35. #include <linux/eisa.h>
  36. #include <linux/pci.h>
  37. #include <linux/dma-mapping.h>
  38. #include <linux/netdevice.h>
  39. #include <linux/etherdevice.h>
  40. #include <linux/delay.h>
  41. #include <linux/spinlock.h>
  42. #include <linux/workqueue.h>
  43. #include <linux/mii.h>
  44. #include "tlan.h"
  45. /* For removing EISA devices */
  46. static struct net_device *tlan_eisa_devices;
  47. static int tlan_devices_installed;
  48. /* Set speed, duplex and aui settings */
  49. static int aui[MAX_TLAN_BOARDS];
  50. static int duplex[MAX_TLAN_BOARDS];
  51. static int speed[MAX_TLAN_BOARDS];
  52. static int boards_found;
  53. module_param_array(aui, int, NULL, 0);
  54. module_param_array(duplex, int, NULL, 0);
  55. module_param_array(speed, int, NULL, 0);
  56. MODULE_PARM_DESC(aui, "ThunderLAN use AUI port(s) (0-1)");
  57. MODULE_PARM_DESC(duplex,
  58. "ThunderLAN duplex setting(s) (0-default, 1-half, 2-full)");
  59. MODULE_PARM_DESC(speed, "ThunderLAN port speed setting(s) (0,10,100)");
  60. MODULE_AUTHOR("Maintainer: Samuel Chessman <chessman@tux.org>");
  61. MODULE_DESCRIPTION("Driver for TI ThunderLAN based ethernet PCI adapters");
  62. MODULE_LICENSE("GPL");
  63. /* Define this to enable Link beat monitoring */
  64. #undef MONITOR
  65. /* Turn on debugging. See Documentation/networking/tlan.txt for details */
  66. static int debug;
  67. module_param(debug, int, 0);
  68. MODULE_PARM_DESC(debug, "ThunderLAN debug mask");
  69. static const char tlan_signature[] = "TLAN";
  70. static const char tlan_banner[] = "ThunderLAN driver v1.17\n";
  71. static int tlan_have_pci;
  72. static int tlan_have_eisa;
  73. static const char * const media[] = {
  74. "10BaseT-HD", "10BaseT-FD", "100baseTx-HD",
  75. "100BaseTx-FD", "100BaseT4", NULL
  76. };
  77. static struct board {
  78. const char *device_label;
  79. u32 flags;
  80. u16 addr_ofs;
  81. } board_info[] = {
  82. { "Compaq Netelligent 10 T PCI UTP", TLAN_ADAPTER_ACTIVITY_LED, 0x83 },
  83. { "Compaq Netelligent 10/100 TX PCI UTP",
  84. TLAN_ADAPTER_ACTIVITY_LED, 0x83 },
  85. { "Compaq Integrated NetFlex-3/P", TLAN_ADAPTER_NONE, 0x83 },
  86. { "Compaq NetFlex-3/P",
  87. TLAN_ADAPTER_UNMANAGED_PHY | TLAN_ADAPTER_BIT_RATE_PHY, 0x83 },
  88. { "Compaq NetFlex-3/P", TLAN_ADAPTER_NONE, 0x83 },
  89. { "Compaq Netelligent Integrated 10/100 TX UTP",
  90. TLAN_ADAPTER_ACTIVITY_LED, 0x83 },
  91. { "Compaq Netelligent Dual 10/100 TX PCI UTP",
  92. TLAN_ADAPTER_NONE, 0x83 },
  93. { "Compaq Netelligent 10/100 TX Embedded UTP",
  94. TLAN_ADAPTER_NONE, 0x83 },
  95. { "Olicom OC-2183/2185", TLAN_ADAPTER_USE_INTERN_10, 0x83 },
  96. { "Olicom OC-2325", TLAN_ADAPTER_UNMANAGED_PHY, 0xf8 },
  97. { "Olicom OC-2326", TLAN_ADAPTER_USE_INTERN_10, 0xf8 },
  98. { "Compaq Netelligent 10/100 TX UTP", TLAN_ADAPTER_ACTIVITY_LED, 0x83 },
  99. { "Compaq Netelligent 10 T/2 PCI UTP/coax", TLAN_ADAPTER_NONE, 0x83 },
  100. { "Compaq NetFlex-3/E",
  101. TLAN_ADAPTER_ACTIVITY_LED | /* EISA card */
  102. TLAN_ADAPTER_UNMANAGED_PHY | TLAN_ADAPTER_BIT_RATE_PHY, 0x83 },
  103. { "Compaq NetFlex-3/E",
  104. TLAN_ADAPTER_ACTIVITY_LED, 0x83 }, /* EISA card */
  105. };
  106. static DEFINE_PCI_DEVICE_TABLE(tlan_pci_tbl) = {
  107. { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_NETEL10,
  108. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  109. { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_NETEL100,
  110. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1 },
  111. { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_NETFLEX3I,
  112. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 2 },
  113. { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_THUNDER,
  114. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 3 },
  115. { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_NETFLEX3B,
  116. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 4 },
  117. { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_NETEL100PI,
  118. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 5 },
  119. { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_NETEL100D,
  120. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 6 },
  121. { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_NETEL100I,
  122. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 7 },
  123. { PCI_VENDOR_ID_OLICOM, PCI_DEVICE_ID_OLICOM_OC2183,
  124. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 8 },
  125. { PCI_VENDOR_ID_OLICOM, PCI_DEVICE_ID_OLICOM_OC2325,
  126. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 9 },
  127. { PCI_VENDOR_ID_OLICOM, PCI_DEVICE_ID_OLICOM_OC2326,
  128. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 10 },
  129. { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_NETELLIGENT_10_100_WS_5100,
  130. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 11 },
  131. { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_NETELLIGENT_10_T2,
  132. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 12 },
  133. { 0,}
  134. };
  135. MODULE_DEVICE_TABLE(pci, tlan_pci_tbl);
  136. static void tlan_eisa_probe(void);
  137. static void tlan_eisa_cleanup(void);
  138. static int tlan_init(struct net_device *);
  139. static int tlan_open(struct net_device *dev);
  140. static netdev_tx_t tlan_start_tx(struct sk_buff *, struct net_device *);
  141. static irqreturn_t tlan_handle_interrupt(int, void *);
  142. static int tlan_close(struct net_device *);
  143. static struct net_device_stats *tlan_get_stats(struct net_device *);
  144. static void tlan_set_multicast_list(struct net_device *);
  145. static int tlan_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
  146. static int tlan_probe1(struct pci_dev *pdev, long ioaddr,
  147. int irq, int rev, const struct pci_device_id *ent);
  148. static void tlan_tx_timeout(struct net_device *dev);
  149. static void tlan_tx_timeout_work(struct work_struct *work);
  150. static int tlan_init_one(struct pci_dev *pdev,
  151. const struct pci_device_id *ent);
  152. static u32 tlan_handle_tx_eof(struct net_device *, u16);
  153. static u32 tlan_handle_stat_overflow(struct net_device *, u16);
  154. static u32 tlan_handle_rx_eof(struct net_device *, u16);
  155. static u32 tlan_handle_dummy(struct net_device *, u16);
  156. static u32 tlan_handle_tx_eoc(struct net_device *, u16);
  157. static u32 tlan_handle_status_check(struct net_device *, u16);
  158. static u32 tlan_handle_rx_eoc(struct net_device *, u16);
  159. static void tlan_timer(unsigned long);
  160. static void tlan_reset_lists(struct net_device *);
  161. static void tlan_free_lists(struct net_device *);
  162. static void tlan_print_dio(u16);
  163. static void tlan_print_list(struct tlan_list *, char *, int);
  164. static void tlan_read_and_clear_stats(struct net_device *, int);
  165. static void tlan_reset_adapter(struct net_device *);
  166. static void tlan_finish_reset(struct net_device *);
  167. static void tlan_set_mac(struct net_device *, int areg, char *mac);
  168. static void tlan_phy_print(struct net_device *);
  169. static void tlan_phy_detect(struct net_device *);
  170. static void tlan_phy_power_down(struct net_device *);
  171. static void tlan_phy_power_up(struct net_device *);
  172. static void tlan_phy_reset(struct net_device *);
  173. static void tlan_phy_start_link(struct net_device *);
  174. static void tlan_phy_finish_auto_neg(struct net_device *);
  175. #ifdef MONITOR
  176. static void tlan_phy_monitor(struct net_device *);
  177. #endif
  178. /*
  179. static int tlan_phy_nop(struct net_device *);
  180. static int tlan_phy_internal_check(struct net_device *);
  181. static int tlan_phy_internal_service(struct net_device *);
  182. static int tlan_phy_dp83840a_check(struct net_device *);
  183. */
  184. static bool tlan_mii_read_reg(struct net_device *, u16, u16, u16 *);
  185. static void tlan_mii_send_data(u16, u32, unsigned);
  186. static void tlan_mii_sync(u16);
  187. static void tlan_mii_write_reg(struct net_device *, u16, u16, u16);
  188. static void tlan_ee_send_start(u16);
  189. static int tlan_ee_send_byte(u16, u8, int);
  190. static void tlan_ee_receive_byte(u16, u8 *, int);
  191. static int tlan_ee_read_byte(struct net_device *, u8, u8 *);
  192. static inline void
  193. tlan_store_skb(struct tlan_list *tag, struct sk_buff *skb)
  194. {
  195. unsigned long addr = (unsigned long)skb;
  196. tag->buffer[9].address = addr;
  197. tag->buffer[8].address = upper_32_bits(addr);
  198. }
  199. static inline struct sk_buff *
  200. tlan_get_skb(const struct tlan_list *tag)
  201. {
  202. unsigned long addr;
  203. addr = tag->buffer[9].address;
  204. addr |= (tag->buffer[8].address << 16) << 16;
  205. return (struct sk_buff *) addr;
  206. }
  207. static u32
  208. (*tlan_int_vector[TLAN_INT_NUMBER_OF_INTS])(struct net_device *, u16) = {
  209. NULL,
  210. tlan_handle_tx_eof,
  211. tlan_handle_stat_overflow,
  212. tlan_handle_rx_eof,
  213. tlan_handle_dummy,
  214. tlan_handle_tx_eoc,
  215. tlan_handle_status_check,
  216. tlan_handle_rx_eoc
  217. };
  218. static inline void
  219. tlan_set_timer(struct net_device *dev, u32 ticks, u32 type)
  220. {
  221. struct tlan_priv *priv = netdev_priv(dev);
  222. unsigned long flags = 0;
  223. if (!in_irq())
  224. spin_lock_irqsave(&priv->lock, flags);
  225. if (priv->timer.function != NULL &&
  226. priv->timer_type != TLAN_TIMER_ACTIVITY) {
  227. if (!in_irq())
  228. spin_unlock_irqrestore(&priv->lock, flags);
  229. return;
  230. }
  231. priv->timer.function = tlan_timer;
  232. if (!in_irq())
  233. spin_unlock_irqrestore(&priv->lock, flags);
  234. priv->timer.data = (unsigned long) dev;
  235. priv->timer_set_at = jiffies;
  236. priv->timer_type = type;
  237. mod_timer(&priv->timer, jiffies + ticks);
  238. }
  239. /*****************************************************************************
  240. ******************************************************************************
  241. ThunderLAN driver primary functions
  242. these functions are more or less common to all linux network drivers.
  243. ******************************************************************************
  244. *****************************************************************************/
  245. /***************************************************************
  246. * tlan_remove_one
  247. *
  248. * Returns:
  249. * Nothing
  250. * Parms:
  251. * None
  252. *
  253. * Goes through the TLanDevices list and frees the device
  254. * structs and memory associated with each device (lists
  255. * and buffers). It also ureserves the IO port regions
  256. * associated with this device.
  257. *
  258. **************************************************************/
  259. static void __devexit tlan_remove_one(struct pci_dev *pdev)
  260. {
  261. struct net_device *dev = pci_get_drvdata(pdev);
  262. struct tlan_priv *priv = netdev_priv(dev);
  263. unregister_netdev(dev);
  264. if (priv->dma_storage) {
  265. pci_free_consistent(priv->pci_dev,
  266. priv->dma_size, priv->dma_storage,
  267. priv->dma_storage_dma);
  268. }
  269. #ifdef CONFIG_PCI
  270. pci_release_regions(pdev);
  271. #endif
  272. free_netdev(dev);
  273. pci_set_drvdata(pdev, NULL);
  274. }
  275. static void tlan_start(struct net_device *dev)
  276. {
  277. tlan_reset_lists(dev);
  278. /* NOTE: It might not be necessary to read the stats before a
  279. reset if you don't care what the values are.
  280. */
  281. tlan_read_and_clear_stats(dev, TLAN_IGNORE);
  282. tlan_reset_adapter(dev);
  283. netif_wake_queue(dev);
  284. }
  285. static void tlan_stop(struct net_device *dev)
  286. {
  287. struct tlan_priv *priv = netdev_priv(dev);
  288. tlan_read_and_clear_stats(dev, TLAN_RECORD);
  289. outl(TLAN_HC_AD_RST, dev->base_addr + TLAN_HOST_CMD);
  290. /* Reset and power down phy */
  291. tlan_reset_adapter(dev);
  292. if (priv->timer.function != NULL) {
  293. del_timer_sync(&priv->timer);
  294. priv->timer.function = NULL;
  295. }
  296. }
  297. #ifdef CONFIG_PM
  298. static int tlan_suspend(struct pci_dev *pdev, pm_message_t state)
  299. {
  300. struct net_device *dev = pci_get_drvdata(pdev);
  301. if (netif_running(dev))
  302. tlan_stop(dev);
  303. netif_device_detach(dev);
  304. pci_save_state(pdev);
  305. pci_disable_device(pdev);
  306. pci_wake_from_d3(pdev, false);
  307. pci_set_power_state(pdev, PCI_D3hot);
  308. return 0;
  309. }
  310. static int tlan_resume(struct pci_dev *pdev)
  311. {
  312. struct net_device *dev = pci_get_drvdata(pdev);
  313. pci_set_power_state(pdev, PCI_D0);
  314. pci_restore_state(pdev);
  315. pci_enable_wake(pdev, 0, 0);
  316. netif_device_attach(dev);
  317. if (netif_running(dev))
  318. tlan_start(dev);
  319. return 0;
  320. }
  321. #else /* CONFIG_PM */
  322. #define tlan_suspend NULL
  323. #define tlan_resume NULL
  324. #endif /* CONFIG_PM */
  325. static struct pci_driver tlan_driver = {
  326. .name = "tlan",
  327. .id_table = tlan_pci_tbl,
  328. .probe = tlan_init_one,
  329. .remove = __devexit_p(tlan_remove_one),
  330. .suspend = tlan_suspend,
  331. .resume = tlan_resume,
  332. };
  333. static int __init tlan_probe(void)
  334. {
  335. int rc = -ENODEV;
  336. pr_info("%s", tlan_banner);
  337. TLAN_DBG(TLAN_DEBUG_PROBE, "Starting PCI Probe....\n");
  338. /* Use new style PCI probing. Now the kernel will
  339. do most of this for us */
  340. rc = pci_register_driver(&tlan_driver);
  341. if (rc != 0) {
  342. pr_err("Could not register pci driver\n");
  343. goto err_out_pci_free;
  344. }
  345. TLAN_DBG(TLAN_DEBUG_PROBE, "Starting EISA Probe....\n");
  346. tlan_eisa_probe();
  347. pr_info("%d device%s installed, PCI: %d EISA: %d\n",
  348. tlan_devices_installed, tlan_devices_installed == 1 ? "" : "s",
  349. tlan_have_pci, tlan_have_eisa);
  350. if (tlan_devices_installed == 0) {
  351. rc = -ENODEV;
  352. goto err_out_pci_unreg;
  353. }
  354. return 0;
  355. err_out_pci_unreg:
  356. pci_unregister_driver(&tlan_driver);
  357. err_out_pci_free:
  358. return rc;
  359. }
  360. static int __devinit tlan_init_one(struct pci_dev *pdev,
  361. const struct pci_device_id *ent)
  362. {
  363. return tlan_probe1(pdev, -1, -1, 0, ent);
  364. }
  365. /*
  366. ***************************************************************
  367. * tlan_probe1
  368. *
  369. * Returns:
  370. * 0 on success, error code on error
  371. * Parms:
  372. * none
  373. *
  374. * The name is lower case to fit in with all the rest of
  375. * the netcard_probe names. This function looks for
  376. * another TLan based adapter, setting it up with the
  377. * allocated device struct if one is found.
  378. * tlan_probe has been ported to the new net API and
  379. * now allocates its own device structure. This function
  380. * is also used by modules.
  381. *
  382. **************************************************************/
  383. static int __devinit tlan_probe1(struct pci_dev *pdev,
  384. long ioaddr, int irq, int rev,
  385. const struct pci_device_id *ent)
  386. {
  387. struct net_device *dev;
  388. struct tlan_priv *priv;
  389. u16 device_id;
  390. int reg, rc = -ENODEV;
  391. #ifdef CONFIG_PCI
  392. if (pdev) {
  393. rc = pci_enable_device(pdev);
  394. if (rc)
  395. return rc;
  396. rc = pci_request_regions(pdev, tlan_signature);
  397. if (rc) {
  398. pr_err("Could not reserve IO regions\n");
  399. goto err_out;
  400. }
  401. }
  402. #endif /* CONFIG_PCI */
  403. dev = alloc_etherdev(sizeof(struct tlan_priv));
  404. if (dev == NULL) {
  405. pr_err("Could not allocate memory for device\n");
  406. rc = -ENOMEM;
  407. goto err_out_regions;
  408. }
  409. SET_NETDEV_DEV(dev, &pdev->dev);
  410. priv = netdev_priv(dev);
  411. priv->pci_dev = pdev;
  412. priv->dev = dev;
  413. /* Is this a PCI device? */
  414. if (pdev) {
  415. u32 pci_io_base = 0;
  416. priv->adapter = &board_info[ent->driver_data];
  417. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  418. if (rc) {
  419. pr_err("No suitable PCI mapping available\n");
  420. goto err_out_free_dev;
  421. }
  422. for (reg = 0; reg <= 5; reg++) {
  423. if (pci_resource_flags(pdev, reg) & IORESOURCE_IO) {
  424. pci_io_base = pci_resource_start(pdev, reg);
  425. TLAN_DBG(TLAN_DEBUG_GNRL,
  426. "IO mapping is available at %x.\n",
  427. pci_io_base);
  428. break;
  429. }
  430. }
  431. if (!pci_io_base) {
  432. pr_err("No IO mappings available\n");
  433. rc = -EIO;
  434. goto err_out_free_dev;
  435. }
  436. dev->base_addr = pci_io_base;
  437. dev->irq = pdev->irq;
  438. priv->adapter_rev = pdev->revision;
  439. pci_set_master(pdev);
  440. pci_set_drvdata(pdev, dev);
  441. } else { /* EISA card */
  442. /* This is a hack. We need to know which board structure
  443. * is suited for this adapter */
  444. device_id = inw(ioaddr + EISA_ID2);
  445. priv->is_eisa = 1;
  446. if (device_id == 0x20F1) {
  447. priv->adapter = &board_info[13]; /* NetFlex-3/E */
  448. priv->adapter_rev = 23; /* TLAN 2.3 */
  449. } else {
  450. priv->adapter = &board_info[14];
  451. priv->adapter_rev = 10; /* TLAN 1.0 */
  452. }
  453. dev->base_addr = ioaddr;
  454. dev->irq = irq;
  455. }
  456. /* Kernel parameters */
  457. if (dev->mem_start) {
  458. priv->aui = dev->mem_start & 0x01;
  459. priv->duplex = ((dev->mem_start & 0x06) == 0x06) ? 0
  460. : (dev->mem_start & 0x06) >> 1;
  461. priv->speed = ((dev->mem_start & 0x18) == 0x18) ? 0
  462. : (dev->mem_start & 0x18) >> 3;
  463. if (priv->speed == 0x1)
  464. priv->speed = TLAN_SPEED_10;
  465. else if (priv->speed == 0x2)
  466. priv->speed = TLAN_SPEED_100;
  467. debug = priv->debug = dev->mem_end;
  468. } else {
  469. priv->aui = aui[boards_found];
  470. priv->speed = speed[boards_found];
  471. priv->duplex = duplex[boards_found];
  472. priv->debug = debug;
  473. }
  474. /* This will be used when we get an adapter error from
  475. * within our irq handler */
  476. INIT_WORK(&priv->tlan_tqueue, tlan_tx_timeout_work);
  477. spin_lock_init(&priv->lock);
  478. rc = tlan_init(dev);
  479. if (rc) {
  480. pr_err("Could not set up device\n");
  481. goto err_out_free_dev;
  482. }
  483. rc = register_netdev(dev);
  484. if (rc) {
  485. pr_err("Could not register device\n");
  486. goto err_out_uninit;
  487. }
  488. tlan_devices_installed++;
  489. boards_found++;
  490. /* pdev is NULL if this is an EISA device */
  491. if (pdev)
  492. tlan_have_pci++;
  493. else {
  494. priv->next_device = tlan_eisa_devices;
  495. tlan_eisa_devices = dev;
  496. tlan_have_eisa++;
  497. }
  498. netdev_info(dev, "irq=%2d, io=%04x, %s, Rev. %d\n",
  499. (int)dev->irq,
  500. (int)dev->base_addr,
  501. priv->adapter->device_label,
  502. priv->adapter_rev);
  503. return 0;
  504. err_out_uninit:
  505. pci_free_consistent(priv->pci_dev, priv->dma_size, priv->dma_storage,
  506. priv->dma_storage_dma);
  507. err_out_free_dev:
  508. free_netdev(dev);
  509. err_out_regions:
  510. #ifdef CONFIG_PCI
  511. if (pdev)
  512. pci_release_regions(pdev);
  513. #endif
  514. err_out:
  515. if (pdev)
  516. pci_disable_device(pdev);
  517. return rc;
  518. }
  519. static void tlan_eisa_cleanup(void)
  520. {
  521. struct net_device *dev;
  522. struct tlan_priv *priv;
  523. while (tlan_have_eisa) {
  524. dev = tlan_eisa_devices;
  525. priv = netdev_priv(dev);
  526. if (priv->dma_storage) {
  527. pci_free_consistent(priv->pci_dev, priv->dma_size,
  528. priv->dma_storage,
  529. priv->dma_storage_dma);
  530. }
  531. release_region(dev->base_addr, 0x10);
  532. unregister_netdev(dev);
  533. tlan_eisa_devices = priv->next_device;
  534. free_netdev(dev);
  535. tlan_have_eisa--;
  536. }
  537. }
  538. static void __exit tlan_exit(void)
  539. {
  540. pci_unregister_driver(&tlan_driver);
  541. if (tlan_have_eisa)
  542. tlan_eisa_cleanup();
  543. }
  544. /* Module loading/unloading */
  545. module_init(tlan_probe);
  546. module_exit(tlan_exit);
  547. /**************************************************************
  548. * tlan_eisa_probe
  549. *
  550. * Returns: 0 on success, 1 otherwise
  551. *
  552. * Parms: None
  553. *
  554. *
  555. * This functions probes for EISA devices and calls
  556. * TLan_probe1 when one is found.
  557. *
  558. *************************************************************/
  559. static void __init tlan_eisa_probe(void)
  560. {
  561. long ioaddr;
  562. int rc = -ENODEV;
  563. int irq;
  564. u16 device_id;
  565. if (!EISA_bus) {
  566. TLAN_DBG(TLAN_DEBUG_PROBE, "No EISA bus present\n");
  567. return;
  568. }
  569. /* Loop through all slots of the EISA bus */
  570. for (ioaddr = 0x1000; ioaddr < 0x9000; ioaddr += 0x1000) {
  571. TLAN_DBG(TLAN_DEBUG_PROBE, "EISA_ID 0x%4x: 0x%4x\n",
  572. (int) ioaddr + 0xc80, inw(ioaddr + EISA_ID));
  573. TLAN_DBG(TLAN_DEBUG_PROBE, "EISA_ID 0x%4x: 0x%4x\n",
  574. (int) ioaddr + 0xc82, inw(ioaddr + EISA_ID2));
  575. TLAN_DBG(TLAN_DEBUG_PROBE,
  576. "Probing for EISA adapter at IO: 0x%4x : ",
  577. (int) ioaddr);
  578. if (request_region(ioaddr, 0x10, tlan_signature) == NULL)
  579. goto out;
  580. if (inw(ioaddr + EISA_ID) != 0x110E) {
  581. release_region(ioaddr, 0x10);
  582. goto out;
  583. }
  584. device_id = inw(ioaddr + EISA_ID2);
  585. if (device_id != 0x20F1 && device_id != 0x40F1) {
  586. release_region(ioaddr, 0x10);
  587. goto out;
  588. }
  589. /* check if adapter is enabled */
  590. if (inb(ioaddr + EISA_CR) != 0x1) {
  591. release_region(ioaddr, 0x10);
  592. goto out2;
  593. }
  594. if (debug == 0x10)
  595. pr_info("Found one\n");
  596. /* Get irq from board */
  597. switch (inb(ioaddr + 0xcc0)) {
  598. case(0x10):
  599. irq = 5;
  600. break;
  601. case(0x20):
  602. irq = 9;
  603. break;
  604. case(0x40):
  605. irq = 10;
  606. break;
  607. case(0x80):
  608. irq = 11;
  609. break;
  610. default:
  611. goto out;
  612. }
  613. /* Setup the newly found eisa adapter */
  614. rc = tlan_probe1(NULL, ioaddr, irq,
  615. 12, NULL);
  616. continue;
  617. out:
  618. if (debug == 0x10)
  619. pr_info("None found\n");
  620. continue;
  621. out2:
  622. if (debug == 0x10)
  623. pr_info("Card found but it is not enabled, skipping\n");
  624. continue;
  625. }
  626. }
  627. #ifdef CONFIG_NET_POLL_CONTROLLER
  628. static void tlan_poll(struct net_device *dev)
  629. {
  630. disable_irq(dev->irq);
  631. tlan_handle_interrupt(dev->irq, dev);
  632. enable_irq(dev->irq);
  633. }
  634. #endif
  635. static const struct net_device_ops tlan_netdev_ops = {
  636. .ndo_open = tlan_open,
  637. .ndo_stop = tlan_close,
  638. .ndo_start_xmit = tlan_start_tx,
  639. .ndo_tx_timeout = tlan_tx_timeout,
  640. .ndo_get_stats = tlan_get_stats,
  641. .ndo_set_multicast_list = tlan_set_multicast_list,
  642. .ndo_do_ioctl = tlan_ioctl,
  643. .ndo_change_mtu = eth_change_mtu,
  644. .ndo_set_mac_address = eth_mac_addr,
  645. .ndo_validate_addr = eth_validate_addr,
  646. #ifdef CONFIG_NET_POLL_CONTROLLER
  647. .ndo_poll_controller = tlan_poll,
  648. #endif
  649. };
  650. /***************************************************************
  651. * tlan_init
  652. *
  653. * Returns:
  654. * 0 on success, error code otherwise.
  655. * Parms:
  656. * dev The structure of the device to be
  657. * init'ed.
  658. *
  659. * This function completes the initialization of the
  660. * device structure and driver. It reserves the IO
  661. * addresses, allocates memory for the lists and bounce
  662. * buffers, retrieves the MAC address from the eeprom
  663. * and assignes the device's methods.
  664. *
  665. **************************************************************/
  666. static int tlan_init(struct net_device *dev)
  667. {
  668. int dma_size;
  669. int err;
  670. int i;
  671. struct tlan_priv *priv;
  672. priv = netdev_priv(dev);
  673. dma_size = (TLAN_NUM_RX_LISTS + TLAN_NUM_TX_LISTS)
  674. * (sizeof(struct tlan_list));
  675. priv->dma_storage = pci_alloc_consistent(priv->pci_dev,
  676. dma_size,
  677. &priv->dma_storage_dma);
  678. priv->dma_size = dma_size;
  679. if (priv->dma_storage == NULL) {
  680. pr_err("Could not allocate lists and buffers for %s\n",
  681. dev->name);
  682. return -ENOMEM;
  683. }
  684. memset(priv->dma_storage, 0, dma_size);
  685. priv->rx_list = (struct tlan_list *)
  686. ALIGN((unsigned long)priv->dma_storage, 8);
  687. priv->rx_list_dma = ALIGN(priv->dma_storage_dma, 8);
  688. priv->tx_list = priv->rx_list + TLAN_NUM_RX_LISTS;
  689. priv->tx_list_dma =
  690. priv->rx_list_dma + sizeof(struct tlan_list)*TLAN_NUM_RX_LISTS;
  691. err = 0;
  692. for (i = 0; i < 6 ; i++)
  693. err |= tlan_ee_read_byte(dev,
  694. (u8) priv->adapter->addr_ofs + i,
  695. (u8 *) &dev->dev_addr[i]);
  696. if (err) {
  697. pr_err("%s: Error reading MAC from eeprom: %d\n",
  698. dev->name, err);
  699. }
  700. dev->addr_len = 6;
  701. netif_carrier_off(dev);
  702. /* Device methods */
  703. dev->netdev_ops = &tlan_netdev_ops;
  704. dev->watchdog_timeo = TX_TIMEOUT;
  705. return 0;
  706. }
  707. /***************************************************************
  708. * tlan_open
  709. *
  710. * Returns:
  711. * 0 on success, error code otherwise.
  712. * Parms:
  713. * dev Structure of device to be opened.
  714. *
  715. * This routine puts the driver and TLAN adapter in a
  716. * state where it is ready to send and receive packets.
  717. * It allocates the IRQ, resets and brings the adapter
  718. * out of reset, and allows interrupts. It also delays
  719. * the startup for autonegotiation or sends a Rx GO
  720. * command to the adapter, as appropriate.
  721. *
  722. **************************************************************/
  723. static int tlan_open(struct net_device *dev)
  724. {
  725. struct tlan_priv *priv = netdev_priv(dev);
  726. int err;
  727. priv->tlan_rev = tlan_dio_read8(dev->base_addr, TLAN_DEF_REVISION);
  728. err = request_irq(dev->irq, tlan_handle_interrupt, IRQF_SHARED,
  729. dev->name, dev);
  730. if (err) {
  731. netdev_err(dev, "Cannot open because IRQ %d is already in use\n",
  732. dev->irq);
  733. return err;
  734. }
  735. init_timer(&priv->timer);
  736. tlan_start(dev);
  737. TLAN_DBG(TLAN_DEBUG_GNRL, "%s: Opened. TLAN Chip Rev: %x\n",
  738. dev->name, priv->tlan_rev);
  739. return 0;
  740. }
  741. /**************************************************************
  742. * tlan_ioctl
  743. *
  744. * Returns:
  745. * 0 on success, error code otherwise
  746. * Params:
  747. * dev structure of device to receive ioctl.
  748. *
  749. * rq ifreq structure to hold userspace data.
  750. *
  751. * cmd ioctl command.
  752. *
  753. *
  754. *************************************************************/
  755. static int tlan_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  756. {
  757. struct tlan_priv *priv = netdev_priv(dev);
  758. struct mii_ioctl_data *data = if_mii(rq);
  759. u32 phy = priv->phy[priv->phy_num];
  760. if (!priv->phy_online)
  761. return -EAGAIN;
  762. switch (cmd) {
  763. case SIOCGMIIPHY: /* get address of MII PHY in use. */
  764. data->phy_id = phy;
  765. case SIOCGMIIREG: /* read MII PHY register. */
  766. tlan_mii_read_reg(dev, data->phy_id & 0x1f,
  767. data->reg_num & 0x1f, &data->val_out);
  768. return 0;
  769. case SIOCSMIIREG: /* write MII PHY register. */
  770. tlan_mii_write_reg(dev, data->phy_id & 0x1f,
  771. data->reg_num & 0x1f, data->val_in);
  772. return 0;
  773. default:
  774. return -EOPNOTSUPP;
  775. }
  776. }
  777. /***************************************************************
  778. * tlan_tx_timeout
  779. *
  780. * Returns: nothing
  781. *
  782. * Params:
  783. * dev structure of device which timed out
  784. * during transmit.
  785. *
  786. **************************************************************/
  787. static void tlan_tx_timeout(struct net_device *dev)
  788. {
  789. TLAN_DBG(TLAN_DEBUG_GNRL, "%s: Transmit timed out.\n", dev->name);
  790. /* Ok so we timed out, lets see what we can do about it...*/
  791. tlan_free_lists(dev);
  792. tlan_reset_lists(dev);
  793. tlan_read_and_clear_stats(dev, TLAN_IGNORE);
  794. tlan_reset_adapter(dev);
  795. dev->trans_start = jiffies; /* prevent tx timeout */
  796. netif_wake_queue(dev);
  797. }
  798. /***************************************************************
  799. * tlan_tx_timeout_work
  800. *
  801. * Returns: nothing
  802. *
  803. * Params:
  804. * work work item of device which timed out
  805. *
  806. **************************************************************/
  807. static void tlan_tx_timeout_work(struct work_struct *work)
  808. {
  809. struct tlan_priv *priv =
  810. container_of(work, struct tlan_priv, tlan_tqueue);
  811. tlan_tx_timeout(priv->dev);
  812. }
  813. /***************************************************************
  814. * tlan_start_tx
  815. *
  816. * Returns:
  817. * 0 on success, non-zero on failure.
  818. * Parms:
  819. * skb A pointer to the sk_buff containing the
  820. * frame to be sent.
  821. * dev The device to send the data on.
  822. *
  823. * This function adds a frame to the Tx list to be sent
  824. * ASAP. First it verifies that the adapter is ready and
  825. * there is room in the queue. Then it sets up the next
  826. * available list, copies the frame to the corresponding
  827. * buffer. If the adapter Tx channel is idle, it gives
  828. * the adapter a Tx Go command on the list, otherwise it
  829. * sets the forward address of the previous list to point
  830. * to this one. Then it frees the sk_buff.
  831. *
  832. **************************************************************/
  833. static netdev_tx_t tlan_start_tx(struct sk_buff *skb, struct net_device *dev)
  834. {
  835. struct tlan_priv *priv = netdev_priv(dev);
  836. dma_addr_t tail_list_phys;
  837. struct tlan_list *tail_list;
  838. unsigned long flags;
  839. unsigned int txlen;
  840. if (!priv->phy_online) {
  841. TLAN_DBG(TLAN_DEBUG_TX, "TRANSMIT: %s PHY is not ready\n",
  842. dev->name);
  843. dev_kfree_skb_any(skb);
  844. return NETDEV_TX_OK;
  845. }
  846. if (skb_padto(skb, TLAN_MIN_FRAME_SIZE))
  847. return NETDEV_TX_OK;
  848. txlen = max(skb->len, (unsigned int)TLAN_MIN_FRAME_SIZE);
  849. tail_list = priv->tx_list + priv->tx_tail;
  850. tail_list_phys =
  851. priv->tx_list_dma + sizeof(struct tlan_list)*priv->tx_tail;
  852. if (tail_list->c_stat != TLAN_CSTAT_UNUSED) {
  853. TLAN_DBG(TLAN_DEBUG_TX,
  854. "TRANSMIT: %s is busy (Head=%d Tail=%d)\n",
  855. dev->name, priv->tx_head, priv->tx_tail);
  856. netif_stop_queue(dev);
  857. priv->tx_busy_count++;
  858. return NETDEV_TX_BUSY;
  859. }
  860. tail_list->forward = 0;
  861. tail_list->buffer[0].address = pci_map_single(priv->pci_dev,
  862. skb->data, txlen,
  863. PCI_DMA_TODEVICE);
  864. tlan_store_skb(tail_list, skb);
  865. tail_list->frame_size = (u16) txlen;
  866. tail_list->buffer[0].count = TLAN_LAST_BUFFER | (u32) txlen;
  867. tail_list->buffer[1].count = 0;
  868. tail_list->buffer[1].address = 0;
  869. spin_lock_irqsave(&priv->lock, flags);
  870. tail_list->c_stat = TLAN_CSTAT_READY;
  871. if (!priv->tx_in_progress) {
  872. priv->tx_in_progress = 1;
  873. TLAN_DBG(TLAN_DEBUG_TX,
  874. "TRANSMIT: Starting TX on buffer %d\n",
  875. priv->tx_tail);
  876. outl(tail_list_phys, dev->base_addr + TLAN_CH_PARM);
  877. outl(TLAN_HC_GO, dev->base_addr + TLAN_HOST_CMD);
  878. } else {
  879. TLAN_DBG(TLAN_DEBUG_TX,
  880. "TRANSMIT: Adding buffer %d to TX channel\n",
  881. priv->tx_tail);
  882. if (priv->tx_tail == 0) {
  883. (priv->tx_list + (TLAN_NUM_TX_LISTS - 1))->forward
  884. = tail_list_phys;
  885. } else {
  886. (priv->tx_list + (priv->tx_tail - 1))->forward
  887. = tail_list_phys;
  888. }
  889. }
  890. spin_unlock_irqrestore(&priv->lock, flags);
  891. CIRC_INC(priv->tx_tail, TLAN_NUM_TX_LISTS);
  892. return NETDEV_TX_OK;
  893. }
  894. /***************************************************************
  895. * tlan_handle_interrupt
  896. *
  897. * Returns:
  898. * Nothing
  899. * Parms:
  900. * irq The line on which the interrupt
  901. * occurred.
  902. * dev_id A pointer to the device assigned to
  903. * this irq line.
  904. *
  905. * This function handles an interrupt generated by its
  906. * assigned TLAN adapter. The function deactivates
  907. * interrupts on its adapter, records the type of
  908. * interrupt, executes the appropriate subhandler, and
  909. * acknowdges the interrupt to the adapter (thus
  910. * re-enabling adapter interrupts.
  911. *
  912. **************************************************************/
  913. static irqreturn_t tlan_handle_interrupt(int irq, void *dev_id)
  914. {
  915. struct net_device *dev = dev_id;
  916. struct tlan_priv *priv = netdev_priv(dev);
  917. u16 host_int;
  918. u16 type;
  919. spin_lock(&priv->lock);
  920. host_int = inw(dev->base_addr + TLAN_HOST_INT);
  921. type = (host_int & TLAN_HI_IT_MASK) >> 2;
  922. if (type) {
  923. u32 ack;
  924. u32 host_cmd;
  925. outw(host_int, dev->base_addr + TLAN_HOST_INT);
  926. ack = tlan_int_vector[type](dev, host_int);
  927. if (ack) {
  928. host_cmd = TLAN_HC_ACK | ack | (type << 18);
  929. outl(host_cmd, dev->base_addr + TLAN_HOST_CMD);
  930. }
  931. }
  932. spin_unlock(&priv->lock);
  933. return IRQ_RETVAL(type);
  934. }
  935. /***************************************************************
  936. * tlan_close
  937. *
  938. * Returns:
  939. * An error code.
  940. * Parms:
  941. * dev The device structure of the device to
  942. * close.
  943. *
  944. * This function shuts down the adapter. It records any
  945. * stats, puts the adapter into reset state, deactivates
  946. * its time as needed, and frees the irq it is using.
  947. *
  948. **************************************************************/
  949. static int tlan_close(struct net_device *dev)
  950. {
  951. struct tlan_priv *priv = netdev_priv(dev);
  952. priv->neg_be_verbose = 0;
  953. tlan_stop(dev);
  954. free_irq(dev->irq, dev);
  955. tlan_free_lists(dev);
  956. TLAN_DBG(TLAN_DEBUG_GNRL, "Device %s closed.\n", dev->name);
  957. return 0;
  958. }
  959. /***************************************************************
  960. * tlan_get_stats
  961. *
  962. * Returns:
  963. * A pointer to the device's statistics structure.
  964. * Parms:
  965. * dev The device structure to return the
  966. * stats for.
  967. *
  968. * This function updates the devices statistics by reading
  969. * the TLAN chip's onboard registers. Then it returns the
  970. * address of the statistics structure.
  971. *
  972. **************************************************************/
  973. static struct net_device_stats *tlan_get_stats(struct net_device *dev)
  974. {
  975. struct tlan_priv *priv = netdev_priv(dev);
  976. int i;
  977. /* Should only read stats if open ? */
  978. tlan_read_and_clear_stats(dev, TLAN_RECORD);
  979. TLAN_DBG(TLAN_DEBUG_RX, "RECEIVE: %s EOC count = %d\n", dev->name,
  980. priv->rx_eoc_count);
  981. TLAN_DBG(TLAN_DEBUG_TX, "TRANSMIT: %s Busy count = %d\n", dev->name,
  982. priv->tx_busy_count);
  983. if (debug & TLAN_DEBUG_GNRL) {
  984. tlan_print_dio(dev->base_addr);
  985. tlan_phy_print(dev);
  986. }
  987. if (debug & TLAN_DEBUG_LIST) {
  988. for (i = 0; i < TLAN_NUM_RX_LISTS; i++)
  989. tlan_print_list(priv->rx_list + i, "RX", i);
  990. for (i = 0; i < TLAN_NUM_TX_LISTS; i++)
  991. tlan_print_list(priv->tx_list + i, "TX", i);
  992. }
  993. return &dev->stats;
  994. }
  995. /***************************************************************
  996. * tlan_set_multicast_list
  997. *
  998. * Returns:
  999. * Nothing
  1000. * Parms:
  1001. * dev The device structure to set the
  1002. * multicast list for.
  1003. *
  1004. * This function sets the TLAN adaptor to various receive
  1005. * modes. If the IFF_PROMISC flag is set, promiscuous
  1006. * mode is acitviated. Otherwise, promiscuous mode is
  1007. * turned off. If the IFF_ALLMULTI flag is set, then
  1008. * the hash table is set to receive all group addresses.
  1009. * Otherwise, the first three multicast addresses are
  1010. * stored in AREG_1-3, and the rest are selected via the
  1011. * hash table, as necessary.
  1012. *
  1013. **************************************************************/
  1014. static void tlan_set_multicast_list(struct net_device *dev)
  1015. {
  1016. struct netdev_hw_addr *ha;
  1017. u32 hash1 = 0;
  1018. u32 hash2 = 0;
  1019. int i;
  1020. u32 offset;
  1021. u8 tmp;
  1022. if (dev->flags & IFF_PROMISC) {
  1023. tmp = tlan_dio_read8(dev->base_addr, TLAN_NET_CMD);
  1024. tlan_dio_write8(dev->base_addr,
  1025. TLAN_NET_CMD, tmp | TLAN_NET_CMD_CAF);
  1026. } else {
  1027. tmp = tlan_dio_read8(dev->base_addr, TLAN_NET_CMD);
  1028. tlan_dio_write8(dev->base_addr,
  1029. TLAN_NET_CMD, tmp & ~TLAN_NET_CMD_CAF);
  1030. if (dev->flags & IFF_ALLMULTI) {
  1031. for (i = 0; i < 3; i++)
  1032. tlan_set_mac(dev, i + 1, NULL);
  1033. tlan_dio_write32(dev->base_addr, TLAN_HASH_1,
  1034. 0xffffffff);
  1035. tlan_dio_write32(dev->base_addr, TLAN_HASH_2,
  1036. 0xffffffff);
  1037. } else {
  1038. i = 0;
  1039. netdev_for_each_mc_addr(ha, dev) {
  1040. if (i < 3) {
  1041. tlan_set_mac(dev, i + 1,
  1042. (char *) &ha->addr);
  1043. } else {
  1044. offset =
  1045. tlan_hash_func((u8 *)&ha->addr);
  1046. if (offset < 32)
  1047. hash1 |= (1 << offset);
  1048. else
  1049. hash2 |= (1 << (offset - 32));
  1050. }
  1051. i++;
  1052. }
  1053. for ( ; i < 3; i++)
  1054. tlan_set_mac(dev, i + 1, NULL);
  1055. tlan_dio_write32(dev->base_addr, TLAN_HASH_1, hash1);
  1056. tlan_dio_write32(dev->base_addr, TLAN_HASH_2, hash2);
  1057. }
  1058. }
  1059. }
  1060. /*****************************************************************************
  1061. ******************************************************************************
  1062. ThunderLAN driver interrupt vectors and table
  1063. please see chap. 4, "Interrupt Handling" of the "ThunderLAN
  1064. Programmer's Guide" for more informations on handling interrupts
  1065. generated by TLAN based adapters.
  1066. ******************************************************************************
  1067. *****************************************************************************/
  1068. /***************************************************************
  1069. * tlan_handle_tx_eof
  1070. *
  1071. * Returns:
  1072. * 1
  1073. * Parms:
  1074. * dev Device assigned the IRQ that was
  1075. * raised.
  1076. * host_int The contents of the HOST_INT
  1077. * port.
  1078. *
  1079. * This function handles Tx EOF interrupts which are raised
  1080. * by the adapter when it has completed sending the
  1081. * contents of a buffer. If detemines which list/buffer
  1082. * was completed and resets it. If the buffer was the last
  1083. * in the channel (EOC), then the function checks to see if
  1084. * another buffer is ready to send, and if so, sends a Tx
  1085. * Go command. Finally, the driver activates/continues the
  1086. * activity LED.
  1087. *
  1088. **************************************************************/
  1089. static u32 tlan_handle_tx_eof(struct net_device *dev, u16 host_int)
  1090. {
  1091. struct tlan_priv *priv = netdev_priv(dev);
  1092. int eoc = 0;
  1093. struct tlan_list *head_list;
  1094. dma_addr_t head_list_phys;
  1095. u32 ack = 0;
  1096. u16 tmp_c_stat;
  1097. TLAN_DBG(TLAN_DEBUG_TX,
  1098. "TRANSMIT: Handling TX EOF (Head=%d Tail=%d)\n",
  1099. priv->tx_head, priv->tx_tail);
  1100. head_list = priv->tx_list + priv->tx_head;
  1101. while (((tmp_c_stat = head_list->c_stat) & TLAN_CSTAT_FRM_CMP)
  1102. && (ack < 255)) {
  1103. struct sk_buff *skb = tlan_get_skb(head_list);
  1104. ack++;
  1105. pci_unmap_single(priv->pci_dev, head_list->buffer[0].address,
  1106. max(skb->len,
  1107. (unsigned int)TLAN_MIN_FRAME_SIZE),
  1108. PCI_DMA_TODEVICE);
  1109. dev_kfree_skb_any(skb);
  1110. head_list->buffer[8].address = 0;
  1111. head_list->buffer[9].address = 0;
  1112. if (tmp_c_stat & TLAN_CSTAT_EOC)
  1113. eoc = 1;
  1114. dev->stats.tx_bytes += head_list->frame_size;
  1115. head_list->c_stat = TLAN_CSTAT_UNUSED;
  1116. netif_start_queue(dev);
  1117. CIRC_INC(priv->tx_head, TLAN_NUM_TX_LISTS);
  1118. head_list = priv->tx_list + priv->tx_head;
  1119. }
  1120. if (!ack)
  1121. netdev_info(dev,
  1122. "Received interrupt for uncompleted TX frame\n");
  1123. if (eoc) {
  1124. TLAN_DBG(TLAN_DEBUG_TX,
  1125. "TRANSMIT: handling TX EOC (Head=%d Tail=%d)\n",
  1126. priv->tx_head, priv->tx_tail);
  1127. head_list = priv->tx_list + priv->tx_head;
  1128. head_list_phys = priv->tx_list_dma
  1129. + sizeof(struct tlan_list)*priv->tx_head;
  1130. if ((head_list->c_stat & TLAN_CSTAT_READY)
  1131. == TLAN_CSTAT_READY) {
  1132. outl(head_list_phys, dev->base_addr + TLAN_CH_PARM);
  1133. ack |= TLAN_HC_GO;
  1134. } else {
  1135. priv->tx_in_progress = 0;
  1136. }
  1137. }
  1138. if (priv->adapter->flags & TLAN_ADAPTER_ACTIVITY_LED) {
  1139. tlan_dio_write8(dev->base_addr,
  1140. TLAN_LED_REG, TLAN_LED_LINK | TLAN_LED_ACT);
  1141. if (priv->timer.function == NULL) {
  1142. priv->timer.function = tlan_timer;
  1143. priv->timer.data = (unsigned long) dev;
  1144. priv->timer.expires = jiffies + TLAN_TIMER_ACT_DELAY;
  1145. priv->timer_set_at = jiffies;
  1146. priv->timer_type = TLAN_TIMER_ACTIVITY;
  1147. add_timer(&priv->timer);
  1148. } else if (priv->timer_type == TLAN_TIMER_ACTIVITY) {
  1149. priv->timer_set_at = jiffies;
  1150. }
  1151. }
  1152. return ack;
  1153. }
  1154. /***************************************************************
  1155. * TLan_HandleStatOverflow
  1156. *
  1157. * Returns:
  1158. * 1
  1159. * Parms:
  1160. * dev Device assigned the IRQ that was
  1161. * raised.
  1162. * host_int The contents of the HOST_INT
  1163. * port.
  1164. *
  1165. * This function handles the Statistics Overflow interrupt
  1166. * which means that one or more of the TLAN statistics
  1167. * registers has reached 1/2 capacity and needs to be read.
  1168. *
  1169. **************************************************************/
  1170. static u32 tlan_handle_stat_overflow(struct net_device *dev, u16 host_int)
  1171. {
  1172. tlan_read_and_clear_stats(dev, TLAN_RECORD);
  1173. return 1;
  1174. }
  1175. /***************************************************************
  1176. * TLan_HandleRxEOF
  1177. *
  1178. * Returns:
  1179. * 1
  1180. * Parms:
  1181. * dev Device assigned the IRQ that was
  1182. * raised.
  1183. * host_int The contents of the HOST_INT
  1184. * port.
  1185. *
  1186. * This function handles the Rx EOF interrupt which
  1187. * indicates a frame has been received by the adapter from
  1188. * the net and the frame has been transferred to memory.
  1189. * The function determines the bounce buffer the frame has
  1190. * been loaded into, creates a new sk_buff big enough to
  1191. * hold the frame, and sends it to protocol stack. It
  1192. * then resets the used buffer and appends it to the end
  1193. * of the list. If the frame was the last in the Rx
  1194. * channel (EOC), the function restarts the receive channel
  1195. * by sending an Rx Go command to the adapter. Then it
  1196. * activates/continues the activity LED.
  1197. *
  1198. **************************************************************/
  1199. static u32 tlan_handle_rx_eof(struct net_device *dev, u16 host_int)
  1200. {
  1201. struct tlan_priv *priv = netdev_priv(dev);
  1202. u32 ack = 0;
  1203. int eoc = 0;
  1204. struct tlan_list *head_list;
  1205. struct sk_buff *skb;
  1206. struct tlan_list *tail_list;
  1207. u16 tmp_c_stat;
  1208. dma_addr_t head_list_phys;
  1209. TLAN_DBG(TLAN_DEBUG_RX, "RECEIVE: handling RX EOF (Head=%d Tail=%d)\n",
  1210. priv->rx_head, priv->rx_tail);
  1211. head_list = priv->rx_list + priv->rx_head;
  1212. head_list_phys =
  1213. priv->rx_list_dma + sizeof(struct tlan_list)*priv->rx_head;
  1214. while (((tmp_c_stat = head_list->c_stat) & TLAN_CSTAT_FRM_CMP)
  1215. && (ack < 255)) {
  1216. dma_addr_t frame_dma = head_list->buffer[0].address;
  1217. u32 frame_size = head_list->frame_size;
  1218. struct sk_buff *new_skb;
  1219. ack++;
  1220. if (tmp_c_stat & TLAN_CSTAT_EOC)
  1221. eoc = 1;
  1222. new_skb = netdev_alloc_skb_ip_align(dev,
  1223. TLAN_MAX_FRAME_SIZE + 5);
  1224. if (!new_skb)
  1225. goto drop_and_reuse;
  1226. skb = tlan_get_skb(head_list);
  1227. pci_unmap_single(priv->pci_dev, frame_dma,
  1228. TLAN_MAX_FRAME_SIZE, PCI_DMA_FROMDEVICE);
  1229. skb_put(skb, frame_size);
  1230. dev->stats.rx_bytes += frame_size;
  1231. skb->protocol = eth_type_trans(skb, dev);
  1232. netif_rx(skb);
  1233. head_list->buffer[0].address =
  1234. pci_map_single(priv->pci_dev, new_skb->data,
  1235. TLAN_MAX_FRAME_SIZE, PCI_DMA_FROMDEVICE);
  1236. tlan_store_skb(head_list, new_skb);
  1237. drop_and_reuse:
  1238. head_list->forward = 0;
  1239. head_list->c_stat = 0;
  1240. tail_list = priv->rx_list + priv->rx_tail;
  1241. tail_list->forward = head_list_phys;
  1242. CIRC_INC(priv->rx_head, TLAN_NUM_RX_LISTS);
  1243. CIRC_INC(priv->rx_tail, TLAN_NUM_RX_LISTS);
  1244. head_list = priv->rx_list + priv->rx_head;
  1245. head_list_phys = priv->rx_list_dma
  1246. + sizeof(struct tlan_list)*priv->rx_head;
  1247. }
  1248. if (!ack)
  1249. netdev_info(dev,
  1250. "Received interrupt for uncompleted RX frame\n");
  1251. if (eoc) {
  1252. TLAN_DBG(TLAN_DEBUG_RX,
  1253. "RECEIVE: handling RX EOC (Head=%d Tail=%d)\n",
  1254. priv->rx_head, priv->rx_tail);
  1255. head_list = priv->rx_list + priv->rx_head;
  1256. head_list_phys = priv->rx_list_dma
  1257. + sizeof(struct tlan_list)*priv->rx_head;
  1258. outl(head_list_phys, dev->base_addr + TLAN_CH_PARM);
  1259. ack |= TLAN_HC_GO | TLAN_HC_RT;
  1260. priv->rx_eoc_count++;
  1261. }
  1262. if (priv->adapter->flags & TLAN_ADAPTER_ACTIVITY_LED) {
  1263. tlan_dio_write8(dev->base_addr,
  1264. TLAN_LED_REG, TLAN_LED_LINK | TLAN_LED_ACT);
  1265. if (priv->timer.function == NULL) {
  1266. priv->timer.function = tlan_timer;
  1267. priv->timer.data = (unsigned long) dev;
  1268. priv->timer.expires = jiffies + TLAN_TIMER_ACT_DELAY;
  1269. priv->timer_set_at = jiffies;
  1270. priv->timer_type = TLAN_TIMER_ACTIVITY;
  1271. add_timer(&priv->timer);
  1272. } else if (priv->timer_type == TLAN_TIMER_ACTIVITY) {
  1273. priv->timer_set_at = jiffies;
  1274. }
  1275. }
  1276. return ack;
  1277. }
  1278. /***************************************************************
  1279. * tlan_handle_dummy
  1280. *
  1281. * Returns:
  1282. * 1
  1283. * Parms:
  1284. * dev Device assigned the IRQ that was
  1285. * raised.
  1286. * host_int The contents of the HOST_INT
  1287. * port.
  1288. *
  1289. * This function handles the Dummy interrupt, which is
  1290. * raised whenever a test interrupt is generated by setting
  1291. * the Req_Int bit of HOST_CMD to 1.
  1292. *
  1293. **************************************************************/
  1294. static u32 tlan_handle_dummy(struct net_device *dev, u16 host_int)
  1295. {
  1296. netdev_info(dev, "Test interrupt\n");
  1297. return 1;
  1298. }
  1299. /***************************************************************
  1300. * tlan_handle_tx_eoc
  1301. *
  1302. * Returns:
  1303. * 1
  1304. * Parms:
  1305. * dev Device assigned the IRQ that was
  1306. * raised.
  1307. * host_int The contents of the HOST_INT
  1308. * port.
  1309. *
  1310. * This driver is structured to determine EOC occurrences by
  1311. * reading the CSTAT member of the list structure. Tx EOC
  1312. * interrupts are disabled via the DIO INTDIS register.
  1313. * However, TLAN chips before revision 3.0 didn't have this
  1314. * functionality, so process EOC events if this is the
  1315. * case.
  1316. *
  1317. **************************************************************/
  1318. static u32 tlan_handle_tx_eoc(struct net_device *dev, u16 host_int)
  1319. {
  1320. struct tlan_priv *priv = netdev_priv(dev);
  1321. struct tlan_list *head_list;
  1322. dma_addr_t head_list_phys;
  1323. u32 ack = 1;
  1324. host_int = 0;
  1325. if (priv->tlan_rev < 0x30) {
  1326. TLAN_DBG(TLAN_DEBUG_TX,
  1327. "TRANSMIT: handling TX EOC (Head=%d Tail=%d) -- IRQ\n",
  1328. priv->tx_head, priv->tx_tail);
  1329. head_list = priv->tx_list + priv->tx_head;
  1330. head_list_phys = priv->tx_list_dma
  1331. + sizeof(struct tlan_list)*priv->tx_head;
  1332. if ((head_list->c_stat & TLAN_CSTAT_READY)
  1333. == TLAN_CSTAT_READY) {
  1334. netif_stop_queue(dev);
  1335. outl(head_list_phys, dev->base_addr + TLAN_CH_PARM);
  1336. ack |= TLAN_HC_GO;
  1337. } else {
  1338. priv->tx_in_progress = 0;
  1339. }
  1340. }
  1341. return ack;
  1342. }
  1343. /***************************************************************
  1344. * tlan_handle_status_check
  1345. *
  1346. * Returns:
  1347. * 0 if Adapter check, 1 if Network Status check.
  1348. * Parms:
  1349. * dev Device assigned the IRQ that was
  1350. * raised.
  1351. * host_int The contents of the HOST_INT
  1352. * port.
  1353. *
  1354. * This function handles Adapter Check/Network Status
  1355. * interrupts generated by the adapter. It checks the
  1356. * vector in the HOST_INT register to determine if it is
  1357. * an Adapter Check interrupt. If so, it resets the
  1358. * adapter. Otherwise it clears the status registers
  1359. * and services the PHY.
  1360. *
  1361. **************************************************************/
  1362. static u32 tlan_handle_status_check(struct net_device *dev, u16 host_int)
  1363. {
  1364. struct tlan_priv *priv = netdev_priv(dev);
  1365. u32 ack;
  1366. u32 error;
  1367. u8 net_sts;
  1368. u32 phy;
  1369. u16 tlphy_ctl;
  1370. u16 tlphy_sts;
  1371. ack = 1;
  1372. if (host_int & TLAN_HI_IV_MASK) {
  1373. netif_stop_queue(dev);
  1374. error = inl(dev->base_addr + TLAN_CH_PARM);
  1375. netdev_info(dev, "Adaptor Error = 0x%x\n", error);
  1376. tlan_read_and_clear_stats(dev, TLAN_RECORD);
  1377. outl(TLAN_HC_AD_RST, dev->base_addr + TLAN_HOST_CMD);
  1378. schedule_work(&priv->tlan_tqueue);
  1379. netif_wake_queue(dev);
  1380. ack = 0;
  1381. } else {
  1382. TLAN_DBG(TLAN_DEBUG_GNRL, "%s: Status Check\n", dev->name);
  1383. phy = priv->phy[priv->phy_num];
  1384. net_sts = tlan_dio_read8(dev->base_addr, TLAN_NET_STS);
  1385. if (net_sts) {
  1386. tlan_dio_write8(dev->base_addr, TLAN_NET_STS, net_sts);
  1387. TLAN_DBG(TLAN_DEBUG_GNRL, "%s: Net_Sts = %x\n",
  1388. dev->name, (unsigned) net_sts);
  1389. }
  1390. if ((net_sts & TLAN_NET_STS_MIRQ) && (priv->phy_num == 0)) {
  1391. tlan_mii_read_reg(dev, phy, TLAN_TLPHY_STS, &tlphy_sts);
  1392. tlan_mii_read_reg(dev, phy, TLAN_TLPHY_CTL, &tlphy_ctl);
  1393. if (!(tlphy_sts & TLAN_TS_POLOK) &&
  1394. !(tlphy_ctl & TLAN_TC_SWAPOL)) {
  1395. tlphy_ctl |= TLAN_TC_SWAPOL;
  1396. tlan_mii_write_reg(dev, phy, TLAN_TLPHY_CTL,
  1397. tlphy_ctl);
  1398. } else if ((tlphy_sts & TLAN_TS_POLOK) &&
  1399. (tlphy_ctl & TLAN_TC_SWAPOL)) {
  1400. tlphy_ctl &= ~TLAN_TC_SWAPOL;
  1401. tlan_mii_write_reg(dev, phy, TLAN_TLPHY_CTL,
  1402. tlphy_ctl);
  1403. }
  1404. if (debug)
  1405. tlan_phy_print(dev);
  1406. }
  1407. }
  1408. return ack;
  1409. }
  1410. /***************************************************************
  1411. * tlan_handle_rx_eoc
  1412. *
  1413. * Returns:
  1414. * 1
  1415. * Parms:
  1416. * dev Device assigned the IRQ that was
  1417. * raised.
  1418. * host_int The contents of the HOST_INT
  1419. * port.
  1420. *
  1421. * This driver is structured to determine EOC occurrences by
  1422. * reading the CSTAT member of the list structure. Rx EOC
  1423. * interrupts are disabled via the DIO INTDIS register.
  1424. * However, TLAN chips before revision 3.0 didn't have this
  1425. * CSTAT member or a INTDIS register, so if this chip is
  1426. * pre-3.0, process EOC interrupts normally.
  1427. *
  1428. **************************************************************/
  1429. static u32 tlan_handle_rx_eoc(struct net_device *dev, u16 host_int)
  1430. {
  1431. struct tlan_priv *priv = netdev_priv(dev);
  1432. dma_addr_t head_list_phys;
  1433. u32 ack = 1;
  1434. if (priv->tlan_rev < 0x30) {
  1435. TLAN_DBG(TLAN_DEBUG_RX,
  1436. "RECEIVE: Handling RX EOC (head=%d tail=%d) -- IRQ\n",
  1437. priv->rx_head, priv->rx_tail);
  1438. head_list_phys = priv->rx_list_dma
  1439. + sizeof(struct tlan_list)*priv->rx_head;
  1440. outl(head_list_phys, dev->base_addr + TLAN_CH_PARM);
  1441. ack |= TLAN_HC_GO | TLAN_HC_RT;
  1442. priv->rx_eoc_count++;
  1443. }
  1444. return ack;
  1445. }
  1446. /*****************************************************************************
  1447. ******************************************************************************
  1448. ThunderLAN driver timer function
  1449. ******************************************************************************
  1450. *****************************************************************************/
  1451. /***************************************************************
  1452. * tlan_timer
  1453. *
  1454. * Returns:
  1455. * Nothing
  1456. * Parms:
  1457. * data A value given to add timer when
  1458. * add_timer was called.
  1459. *
  1460. * This function handles timed functionality for the
  1461. * TLAN driver. The two current timer uses are for
  1462. * delaying for autonegotionation and driving the ACT LED.
  1463. * - Autonegotiation requires being allowed about
  1464. * 2 1/2 seconds before attempting to transmit a
  1465. * packet. It would be a very bad thing to hang
  1466. * the kernel this long, so the driver doesn't
  1467. * allow transmission 'til after this time, for
  1468. * certain PHYs. It would be much nicer if all
  1469. * PHYs were interrupt-capable like the internal
  1470. * PHY.
  1471. * - The ACT LED, which shows adapter activity, is
  1472. * driven by the driver, and so must be left on
  1473. * for a short period to power up the LED so it
  1474. * can be seen. This delay can be changed by
  1475. * changing the TLAN_TIMER_ACT_DELAY in tlan.h,
  1476. * if desired. 100 ms produces a slightly
  1477. * sluggish response.
  1478. *
  1479. **************************************************************/
  1480. static void tlan_timer(unsigned long data)
  1481. {
  1482. struct net_device *dev = (struct net_device *) data;
  1483. struct tlan_priv *priv = netdev_priv(dev);
  1484. u32 elapsed;
  1485. unsigned long flags = 0;
  1486. priv->timer.function = NULL;
  1487. switch (priv->timer_type) {
  1488. #ifdef MONITOR
  1489. case TLAN_TIMER_LINK_BEAT:
  1490. tlan_phy_monitor(dev);
  1491. break;
  1492. #endif
  1493. case TLAN_TIMER_PHY_PDOWN:
  1494. tlan_phy_power_down(dev);
  1495. break;
  1496. case TLAN_TIMER_PHY_PUP:
  1497. tlan_phy_power_up(dev);
  1498. break;
  1499. case TLAN_TIMER_PHY_RESET:
  1500. tlan_phy_reset(dev);
  1501. break;
  1502. case TLAN_TIMER_PHY_START_LINK:
  1503. tlan_phy_start_link(dev);
  1504. break;
  1505. case TLAN_TIMER_PHY_FINISH_AN:
  1506. tlan_phy_finish_auto_neg(dev);
  1507. break;
  1508. case TLAN_TIMER_FINISH_RESET:
  1509. tlan_finish_reset(dev);
  1510. break;
  1511. case TLAN_TIMER_ACTIVITY:
  1512. spin_lock_irqsave(&priv->lock, flags);
  1513. if (priv->timer.function == NULL) {
  1514. elapsed = jiffies - priv->timer_set_at;
  1515. if (elapsed >= TLAN_TIMER_ACT_DELAY) {
  1516. tlan_dio_write8(dev->base_addr,
  1517. TLAN_LED_REG, TLAN_LED_LINK);
  1518. } else {
  1519. priv->timer.function = tlan_timer;
  1520. priv->timer.expires = priv->timer_set_at
  1521. + TLAN_TIMER_ACT_DELAY;
  1522. spin_unlock_irqrestore(&priv->lock, flags);
  1523. add_timer(&priv->timer);
  1524. break;
  1525. }
  1526. }
  1527. spin_unlock_irqrestore(&priv->lock, flags);
  1528. break;
  1529. default:
  1530. break;
  1531. }
  1532. }
  1533. /*****************************************************************************
  1534. ******************************************************************************
  1535. ThunderLAN driver adapter related routines
  1536. ******************************************************************************
  1537. *****************************************************************************/
  1538. /***************************************************************
  1539. * tlan_reset_lists
  1540. *
  1541. * Returns:
  1542. * Nothing
  1543. * Parms:
  1544. * dev The device structure with the list
  1545. * stuctures to be reset.
  1546. *
  1547. * This routine sets the variables associated with managing
  1548. * the TLAN lists to their initial values.
  1549. *
  1550. **************************************************************/
  1551. static void tlan_reset_lists(struct net_device *dev)
  1552. {
  1553. struct tlan_priv *priv = netdev_priv(dev);
  1554. int i;
  1555. struct tlan_list *list;
  1556. dma_addr_t list_phys;
  1557. struct sk_buff *skb;
  1558. priv->tx_head = 0;
  1559. priv->tx_tail = 0;
  1560. for (i = 0; i < TLAN_NUM_TX_LISTS; i++) {
  1561. list = priv->tx_list + i;
  1562. list->c_stat = TLAN_CSTAT_UNUSED;
  1563. list->buffer[0].address = 0;
  1564. list->buffer[2].count = 0;
  1565. list->buffer[2].address = 0;
  1566. list->buffer[8].address = 0;
  1567. list->buffer[9].address = 0;
  1568. }
  1569. priv->rx_head = 0;
  1570. priv->rx_tail = TLAN_NUM_RX_LISTS - 1;
  1571. for (i = 0; i < TLAN_NUM_RX_LISTS; i++) {
  1572. list = priv->rx_list + i;
  1573. list_phys = priv->rx_list_dma + sizeof(struct tlan_list)*i;
  1574. list->c_stat = TLAN_CSTAT_READY;
  1575. list->frame_size = TLAN_MAX_FRAME_SIZE;
  1576. list->buffer[0].count = TLAN_MAX_FRAME_SIZE | TLAN_LAST_BUFFER;
  1577. skb = netdev_alloc_skb_ip_align(dev, TLAN_MAX_FRAME_SIZE + 5);
  1578. if (!skb) {
  1579. netdev_err(dev, "Out of memory for received data\n");
  1580. break;
  1581. }
  1582. list->buffer[0].address = pci_map_single(priv->pci_dev,
  1583. skb->data,
  1584. TLAN_MAX_FRAME_SIZE,
  1585. PCI_DMA_FROMDEVICE);
  1586. tlan_store_skb(list, skb);
  1587. list->buffer[1].count = 0;
  1588. list->buffer[1].address = 0;
  1589. list->forward = list_phys + sizeof(struct tlan_list);
  1590. }
  1591. /* in case ran out of memory early, clear bits */
  1592. while (i < TLAN_NUM_RX_LISTS) {
  1593. tlan_store_skb(priv->rx_list + i, NULL);
  1594. ++i;
  1595. }
  1596. list->forward = 0;
  1597. }
  1598. static void tlan_free_lists(struct net_device *dev)
  1599. {
  1600. struct tlan_priv *priv = netdev_priv(dev);
  1601. int i;
  1602. struct tlan_list *list;
  1603. struct sk_buff *skb;
  1604. for (i = 0; i < TLAN_NUM_TX_LISTS; i++) {
  1605. list = priv->tx_list + i;
  1606. skb = tlan_get_skb(list);
  1607. if (skb) {
  1608. pci_unmap_single(
  1609. priv->pci_dev,
  1610. list->buffer[0].address,
  1611. max(skb->len,
  1612. (unsigned int)TLAN_MIN_FRAME_SIZE),
  1613. PCI_DMA_TODEVICE);
  1614. dev_kfree_skb_any(skb);
  1615. list->buffer[8].address = 0;
  1616. list->buffer[9].address = 0;
  1617. }
  1618. }
  1619. for (i = 0; i < TLAN_NUM_RX_LISTS; i++) {
  1620. list = priv->rx_list + i;
  1621. skb = tlan_get_skb(list);
  1622. if (skb) {
  1623. pci_unmap_single(priv->pci_dev,
  1624. list->buffer[0].address,
  1625. TLAN_MAX_FRAME_SIZE,
  1626. PCI_DMA_FROMDEVICE);
  1627. dev_kfree_skb_any(skb);
  1628. list->buffer[8].address = 0;
  1629. list->buffer[9].address = 0;
  1630. }
  1631. }
  1632. }
  1633. /***************************************************************
  1634. * tlan_print_dio
  1635. *
  1636. * Returns:
  1637. * Nothing
  1638. * Parms:
  1639. * io_base Base IO port of the device of
  1640. * which to print DIO registers.
  1641. *
  1642. * This function prints out all the internal (DIO)
  1643. * registers of a TLAN chip.
  1644. *
  1645. **************************************************************/
  1646. static void tlan_print_dio(u16 io_base)
  1647. {
  1648. u32 data0, data1;
  1649. int i;
  1650. pr_info("Contents of internal registers for io base 0x%04hx\n",
  1651. io_base);
  1652. pr_info("Off. +0 +4\n");
  1653. for (i = 0; i < 0x4C; i += 8) {
  1654. data0 = tlan_dio_read32(io_base, i);
  1655. data1 = tlan_dio_read32(io_base, i + 0x4);
  1656. pr_info("0x%02x 0x%08x 0x%08x\n", i, data0, data1);
  1657. }
  1658. }
  1659. /***************************************************************
  1660. * TLan_PrintList
  1661. *
  1662. * Returns:
  1663. * Nothing
  1664. * Parms:
  1665. * list A pointer to the struct tlan_list structure to
  1666. * be printed.
  1667. * type A string to designate type of list,
  1668. * "Rx" or "Tx".
  1669. * num The index of the list.
  1670. *
  1671. * This function prints out the contents of the list
  1672. * pointed to by the list parameter.
  1673. *
  1674. **************************************************************/
  1675. static void tlan_print_list(struct tlan_list *list, char *type, int num)
  1676. {
  1677. int i;
  1678. pr_info("%s List %d at %p\n", type, num, list);
  1679. pr_info(" Forward = 0x%08x\n", list->forward);
  1680. pr_info(" CSTAT = 0x%04hx\n", list->c_stat);
  1681. pr_info(" Frame Size = 0x%04hx\n", list->frame_size);
  1682. /* for (i = 0; i < 10; i++) { */
  1683. for (i = 0; i < 2; i++) {
  1684. pr_info(" Buffer[%d].count, addr = 0x%08x, 0x%08x\n",
  1685. i, list->buffer[i].count, list->buffer[i].address);
  1686. }
  1687. }
  1688. /***************************************************************
  1689. * tlan_read_and_clear_stats
  1690. *
  1691. * Returns:
  1692. * Nothing
  1693. * Parms:
  1694. * dev Pointer to device structure of adapter
  1695. * to which to read stats.
  1696. * record Flag indicating whether to add
  1697. *
  1698. * This functions reads all the internal status registers
  1699. * of the TLAN chip, which clears them as a side effect.
  1700. * It then either adds the values to the device's status
  1701. * struct, or discards them, depending on whether record
  1702. * is TLAN_RECORD (!=0) or TLAN_IGNORE (==0).
  1703. *
  1704. **************************************************************/
  1705. static void tlan_read_and_clear_stats(struct net_device *dev, int record)
  1706. {
  1707. u32 tx_good, tx_under;
  1708. u32 rx_good, rx_over;
  1709. u32 def_tx, crc, code;
  1710. u32 multi_col, single_col;
  1711. u32 excess_col, late_col, loss;
  1712. outw(TLAN_GOOD_TX_FRMS, dev->base_addr + TLAN_DIO_ADR);
  1713. tx_good = inb(dev->base_addr + TLAN_DIO_DATA);
  1714. tx_good += inb(dev->base_addr + TLAN_DIO_DATA + 1) << 8;
  1715. tx_good += inb(dev->base_addr + TLAN_DIO_DATA + 2) << 16;
  1716. tx_under = inb(dev->base_addr + TLAN_DIO_DATA + 3);
  1717. outw(TLAN_GOOD_RX_FRMS, dev->base_addr + TLAN_DIO_ADR);
  1718. rx_good = inb(dev->base_addr + TLAN_DIO_DATA);
  1719. rx_good += inb(dev->base_addr + TLAN_DIO_DATA + 1) << 8;
  1720. rx_good += inb(dev->base_addr + TLAN_DIO_DATA + 2) << 16;
  1721. rx_over = inb(dev->base_addr + TLAN_DIO_DATA + 3);
  1722. outw(TLAN_DEFERRED_TX, dev->base_addr + TLAN_DIO_ADR);
  1723. def_tx = inb(dev->base_addr + TLAN_DIO_DATA);
  1724. def_tx += inb(dev->base_addr + TLAN_DIO_DATA + 1) << 8;
  1725. crc = inb(dev->base_addr + TLAN_DIO_DATA + 2);
  1726. code = inb(dev->base_addr + TLAN_DIO_DATA + 3);
  1727. outw(TLAN_MULTICOL_FRMS, dev->base_addr + TLAN_DIO_ADR);
  1728. multi_col = inb(dev->base_addr + TLAN_DIO_DATA);
  1729. multi_col += inb(dev->base_addr + TLAN_DIO_DATA + 1) << 8;
  1730. single_col = inb(dev->base_addr + TLAN_DIO_DATA + 2);
  1731. single_col += inb(dev->base_addr + TLAN_DIO_DATA + 3) << 8;
  1732. outw(TLAN_EXCESSCOL_FRMS, dev->base_addr + TLAN_DIO_ADR);
  1733. excess_col = inb(dev->base_addr + TLAN_DIO_DATA);
  1734. late_col = inb(dev->base_addr + TLAN_DIO_DATA + 1);
  1735. loss = inb(dev->base_addr + TLAN_DIO_DATA + 2);
  1736. if (record) {
  1737. dev->stats.rx_packets += rx_good;
  1738. dev->stats.rx_errors += rx_over + crc + code;
  1739. dev->stats.tx_packets += tx_good;
  1740. dev->stats.tx_errors += tx_under + loss;
  1741. dev->stats.collisions += multi_col
  1742. + single_col + excess_col + late_col;
  1743. dev->stats.rx_over_errors += rx_over;
  1744. dev->stats.rx_crc_errors += crc;
  1745. dev->stats.rx_frame_errors += code;
  1746. dev->stats.tx_aborted_errors += tx_under;
  1747. dev->stats.tx_carrier_errors += loss;
  1748. }
  1749. }
  1750. /***************************************************************
  1751. * TLan_Reset
  1752. *
  1753. * Returns:
  1754. * 0
  1755. * Parms:
  1756. * dev Pointer to device structure of adapter
  1757. * to be reset.
  1758. *
  1759. * This function resets the adapter and it's physical
  1760. * device. See Chap. 3, pp. 9-10 of the "ThunderLAN
  1761. * Programmer's Guide" for details. The routine tries to
  1762. * implement what is detailed there, though adjustments
  1763. * have been made.
  1764. *
  1765. **************************************************************/
  1766. static void
  1767. tlan_reset_adapter(struct net_device *dev)
  1768. {
  1769. struct tlan_priv *priv = netdev_priv(dev);
  1770. int i;
  1771. u32 addr;
  1772. u32 data;
  1773. u8 data8;
  1774. priv->tlan_full_duplex = false;
  1775. priv->phy_online = 0;
  1776. netif_carrier_off(dev);
  1777. /* 1. Assert reset bit. */
  1778. data = inl(dev->base_addr + TLAN_HOST_CMD);
  1779. data |= TLAN_HC_AD_RST;
  1780. outl(data, dev->base_addr + TLAN_HOST_CMD);
  1781. udelay(1000);
  1782. /* 2. Turn off interrupts. (Probably isn't necessary) */
  1783. data = inl(dev->base_addr + TLAN_HOST_CMD);
  1784. data |= TLAN_HC_INT_OFF;
  1785. outl(data, dev->base_addr + TLAN_HOST_CMD);
  1786. /* 3. Clear AREGs and HASHs. */
  1787. for (i = TLAN_AREG_0; i <= TLAN_HASH_2; i += 4)
  1788. tlan_dio_write32(dev->base_addr, (u16) i, 0);
  1789. /* 4. Setup NetConfig register. */
  1790. data = TLAN_NET_CFG_1FRAG | TLAN_NET_CFG_1CHAN | TLAN_NET_CFG_PHY_EN;
  1791. tlan_dio_write16(dev->base_addr, TLAN_NET_CONFIG, (u16) data);
  1792. /* 5. Load Ld_Tmr and Ld_Thr in HOST_CMD. */
  1793. outl(TLAN_HC_LD_TMR | 0x3f, dev->base_addr + TLAN_HOST_CMD);
  1794. outl(TLAN_HC_LD_THR | 0x9, dev->base_addr + TLAN_HOST_CMD);
  1795. /* 6. Unreset the MII by setting NMRST (in NetSio) to 1. */
  1796. outw(TLAN_NET_SIO, dev->base_addr + TLAN_DIO_ADR);
  1797. addr = dev->base_addr + TLAN_DIO_DATA + TLAN_NET_SIO;
  1798. tlan_set_bit(TLAN_NET_SIO_NMRST, addr);
  1799. /* 7. Setup the remaining registers. */
  1800. if (priv->tlan_rev >= 0x30) {
  1801. data8 = TLAN_ID_TX_EOC | TLAN_ID_RX_EOC;
  1802. tlan_dio_write8(dev->base_addr, TLAN_INT_DIS, data8);
  1803. }
  1804. tlan_phy_detect(dev);
  1805. data = TLAN_NET_CFG_1FRAG | TLAN_NET_CFG_1CHAN;
  1806. if (priv->adapter->flags & TLAN_ADAPTER_BIT_RATE_PHY) {
  1807. data |= TLAN_NET_CFG_BIT;
  1808. if (priv->aui == 1) {
  1809. tlan_dio_write8(dev->base_addr, TLAN_ACOMMIT, 0x0a);
  1810. } else if (priv->duplex == TLAN_DUPLEX_FULL) {
  1811. tlan_dio_write8(dev->base_addr, TLAN_ACOMMIT, 0x00);
  1812. priv->tlan_full_duplex = true;
  1813. } else {
  1814. tlan_dio_write8(dev->base_addr, TLAN_ACOMMIT, 0x08);
  1815. }
  1816. }
  1817. if (priv->phy_num == 0)
  1818. data |= TLAN_NET_CFG_PHY_EN;
  1819. tlan_dio_write16(dev->base_addr, TLAN_NET_CONFIG, (u16) data);
  1820. if (priv->adapter->flags & TLAN_ADAPTER_UNMANAGED_PHY)
  1821. tlan_finish_reset(dev);
  1822. else
  1823. tlan_phy_power_down(dev);
  1824. }
  1825. static void
  1826. tlan_finish_reset(struct net_device *dev)
  1827. {
  1828. struct tlan_priv *priv = netdev_priv(dev);
  1829. u8 data;
  1830. u32 phy;
  1831. u8 sio;
  1832. u16 status;
  1833. u16 partner;
  1834. u16 tlphy_ctl;
  1835. u16 tlphy_par;
  1836. u16 tlphy_id1, tlphy_id2;
  1837. int i;
  1838. phy = priv->phy[priv->phy_num];
  1839. data = TLAN_NET_CMD_NRESET | TLAN_NET_CMD_NWRAP;
  1840. if (priv->tlan_full_duplex)
  1841. data |= TLAN_NET_CMD_DUPLEX;
  1842. tlan_dio_write8(dev->base_addr, TLAN_NET_CMD, data);
  1843. data = TLAN_NET_MASK_MASK4 | TLAN_NET_MASK_MASK5;
  1844. if (priv->phy_num == 0)
  1845. data |= TLAN_NET_MASK_MASK7;
  1846. tlan_dio_write8(dev->base_addr, TLAN_NET_MASK, data);
  1847. tlan_dio_write16(dev->base_addr, TLAN_MAX_RX, ((1536)+7)&~7);
  1848. tlan_mii_read_reg(dev, phy, MII_GEN_ID_HI, &tlphy_id1);
  1849. tlan_mii_read_reg(dev, phy, MII_GEN_ID_LO, &tlphy_id2);
  1850. if ((priv->adapter->flags & TLAN_ADAPTER_UNMANAGED_PHY) ||
  1851. (priv->aui)) {
  1852. status = MII_GS_LINK;
  1853. netdev_info(dev, "Link forced\n");
  1854. } else {
  1855. tlan_mii_read_reg(dev, phy, MII_GEN_STS, &status);
  1856. udelay(1000);
  1857. tlan_mii_read_reg(dev, phy, MII_GEN_STS, &status);
  1858. if ((status & MII_GS_LINK) &&
  1859. /* We only support link info on Nat.Sem. PHY's */
  1860. (tlphy_id1 == NAT_SEM_ID1) &&
  1861. (tlphy_id2 == NAT_SEM_ID2)) {
  1862. tlan_mii_read_reg(dev, phy, MII_AN_LPA, &partner);
  1863. tlan_mii_read_reg(dev, phy, TLAN_TLPHY_PAR, &tlphy_par);
  1864. netdev_info(dev,
  1865. "Link active with %s %uMbps %s-Duplex\n",
  1866. !(tlphy_par & TLAN_PHY_AN_EN_STAT)
  1867. ? "forced" : "Autonegotiation enabled,",
  1868. tlphy_par & TLAN_PHY_SPEED_100
  1869. ? 100 : 10,
  1870. tlphy_par & TLAN_PHY_DUPLEX_FULL
  1871. ? "Full" : "Half");
  1872. if (tlphy_par & TLAN_PHY_AN_EN_STAT) {
  1873. netdev_info(dev, "Partner capability:");
  1874. for (i = 5; i < 10; i++)
  1875. if (partner & (1 << i))
  1876. pr_cont(" %s", media[i-5]);
  1877. pr_cont("\n");
  1878. }
  1879. tlan_dio_write8(dev->base_addr, TLAN_LED_REG,
  1880. TLAN_LED_LINK);
  1881. #ifdef MONITOR
  1882. /* We have link beat..for now anyway */
  1883. priv->link = 1;
  1884. /*Enabling link beat monitoring */
  1885. tlan_set_timer(dev, (10*HZ), TLAN_TIMER_LINK_BEAT);
  1886. #endif
  1887. } else if (status & MII_GS_LINK) {
  1888. netdev_info(dev, "Link active\n");
  1889. tlan_dio_write8(dev->base_addr, TLAN_LED_REG,
  1890. TLAN_LED_LINK);
  1891. }
  1892. }
  1893. if (priv->phy_num == 0) {
  1894. tlan_mii_read_reg(dev, phy, TLAN_TLPHY_CTL, &tlphy_ctl);
  1895. tlphy_ctl |= TLAN_TC_INTEN;
  1896. tlan_mii_write_reg(dev, phy, TLAN_TLPHY_CTL, tlphy_ctl);
  1897. sio = tlan_dio_read8(dev->base_addr, TLAN_NET_SIO);
  1898. sio |= TLAN_NET_SIO_MINTEN;
  1899. tlan_dio_write8(dev->base_addr, TLAN_NET_SIO, sio);
  1900. }
  1901. if (status & MII_GS_LINK) {
  1902. tlan_set_mac(dev, 0, dev->dev_addr);
  1903. priv->phy_online = 1;
  1904. outb((TLAN_HC_INT_ON >> 8), dev->base_addr + TLAN_HOST_CMD + 1);
  1905. if (debug >= 1 && debug != TLAN_DEBUG_PROBE)
  1906. outb((TLAN_HC_REQ_INT >> 8),
  1907. dev->base_addr + TLAN_HOST_CMD + 1);
  1908. outl(priv->rx_list_dma, dev->base_addr + TLAN_CH_PARM);
  1909. outl(TLAN_HC_GO | TLAN_HC_RT, dev->base_addr + TLAN_HOST_CMD);
  1910. netif_carrier_on(dev);
  1911. } else {
  1912. netdev_info(dev, "Link inactive, will retry in 10 secs...\n");
  1913. tlan_set_timer(dev, (10*HZ), TLAN_TIMER_FINISH_RESET);
  1914. return;
  1915. }
  1916. tlan_set_multicast_list(dev);
  1917. }
  1918. /***************************************************************
  1919. * tlan_set_mac
  1920. *
  1921. * Returns:
  1922. * Nothing
  1923. * Parms:
  1924. * dev Pointer to device structure of adapter
  1925. * on which to change the AREG.
  1926. * areg The AREG to set the address in (0 - 3).
  1927. * mac A pointer to an array of chars. Each
  1928. * element stores one byte of the address.
  1929. * IE, it isn't in ascii.
  1930. *
  1931. * This function transfers a MAC address to one of the
  1932. * TLAN AREGs (address registers). The TLAN chip locks
  1933. * the register on writing to offset 0 and unlocks the
  1934. * register after writing to offset 5. If NULL is passed
  1935. * in mac, then the AREG is filled with 0's.
  1936. *
  1937. **************************************************************/
  1938. static void tlan_set_mac(struct net_device *dev, int areg, char *mac)
  1939. {
  1940. int i;
  1941. areg *= 6;
  1942. if (mac != NULL) {
  1943. for (i = 0; i < 6; i++)
  1944. tlan_dio_write8(dev->base_addr,
  1945. TLAN_AREG_0 + areg + i, mac[i]);
  1946. } else {
  1947. for (i = 0; i < 6; i++)
  1948. tlan_dio_write8(dev->base_addr,
  1949. TLAN_AREG_0 + areg + i, 0);
  1950. }
  1951. }
  1952. /*****************************************************************************
  1953. ******************************************************************************
  1954. ThunderLAN driver PHY layer routines
  1955. ******************************************************************************
  1956. *****************************************************************************/
  1957. /*********************************************************************
  1958. * tlan_phy_print
  1959. *
  1960. * Returns:
  1961. * Nothing
  1962. * Parms:
  1963. * dev A pointer to the device structure of the
  1964. * TLAN device having the PHYs to be detailed.
  1965. *
  1966. * This function prints the registers a PHY (aka transceiver).
  1967. *
  1968. ********************************************************************/
  1969. static void tlan_phy_print(struct net_device *dev)
  1970. {
  1971. struct tlan_priv *priv = netdev_priv(dev);
  1972. u16 i, data0, data1, data2, data3, phy;
  1973. phy = priv->phy[priv->phy_num];
  1974. if (priv->adapter->flags & TLAN_ADAPTER_UNMANAGED_PHY) {
  1975. netdev_info(dev, "Unmanaged PHY\n");
  1976. } else if (phy <= TLAN_PHY_MAX_ADDR) {
  1977. netdev_info(dev, "PHY 0x%02x\n", phy);
  1978. pr_info(" Off. +0 +1 +2 +3\n");
  1979. for (i = 0; i < 0x20; i += 4) {
  1980. tlan_mii_read_reg(dev, phy, i, &data0);
  1981. tlan_mii_read_reg(dev, phy, i + 1, &data1);
  1982. tlan_mii_read_reg(dev, phy, i + 2, &data2);
  1983. tlan_mii_read_reg(dev, phy, i + 3, &data3);
  1984. pr_info(" 0x%02x 0x%04hx 0x%04hx 0x%04hx 0x%04hx\n",
  1985. i, data0, data1, data2, data3);
  1986. }
  1987. } else {
  1988. netdev_info(dev, "Invalid PHY\n");
  1989. }
  1990. }
  1991. /*********************************************************************
  1992. * tlan_phy_detect
  1993. *
  1994. * Returns:
  1995. * Nothing
  1996. * Parms:
  1997. * dev A pointer to the device structure of the adapter
  1998. * for which the PHY needs determined.
  1999. *
  2000. * So far I've found that adapters which have external PHYs
  2001. * may also use the internal PHY for part of the functionality.
  2002. * (eg, AUI/Thinnet). This function finds out if this TLAN
  2003. * chip has an internal PHY, and then finds the first external
  2004. * PHY (starting from address 0) if it exists).
  2005. *
  2006. ********************************************************************/
  2007. static void tlan_phy_detect(struct net_device *dev)
  2008. {
  2009. struct tlan_priv *priv = netdev_priv(dev);
  2010. u16 control;
  2011. u16 hi;
  2012. u16 lo;
  2013. u32 phy;
  2014. if (priv->adapter->flags & TLAN_ADAPTER_UNMANAGED_PHY) {
  2015. priv->phy_num = 0xffff;
  2016. return;
  2017. }
  2018. tlan_mii_read_reg(dev, TLAN_PHY_MAX_ADDR, MII_GEN_ID_HI, &hi);
  2019. if (hi != 0xffff)
  2020. priv->phy[0] = TLAN_PHY_MAX_ADDR;
  2021. else
  2022. priv->phy[0] = TLAN_PHY_NONE;
  2023. priv->phy[1] = TLAN_PHY_NONE;
  2024. for (phy = 0; phy <= TLAN_PHY_MAX_ADDR; phy++) {
  2025. tlan_mii_read_reg(dev, phy, MII_GEN_CTL, &control);
  2026. tlan_mii_read_reg(dev, phy, MII_GEN_ID_HI, &hi);
  2027. tlan_mii_read_reg(dev, phy, MII_GEN_ID_LO, &lo);
  2028. if ((control != 0xffff) ||
  2029. (hi != 0xffff) || (lo != 0xffff)) {
  2030. TLAN_DBG(TLAN_DEBUG_GNRL,
  2031. "PHY found at %02x %04x %04x %04x\n",
  2032. phy, control, hi, lo);
  2033. if ((priv->phy[1] == TLAN_PHY_NONE) &&
  2034. (phy != TLAN_PHY_MAX_ADDR)) {
  2035. priv->phy[1] = phy;
  2036. }
  2037. }
  2038. }
  2039. if (priv->phy[1] != TLAN_PHY_NONE)
  2040. priv->phy_num = 1;
  2041. else if (priv->phy[0] != TLAN_PHY_NONE)
  2042. priv->phy_num = 0;
  2043. else
  2044. netdev_info(dev, "Cannot initialize device, no PHY was found!\n");
  2045. }
  2046. static void tlan_phy_power_down(struct net_device *dev)
  2047. {
  2048. struct tlan_priv *priv = netdev_priv(dev);
  2049. u16 value;
  2050. TLAN_DBG(TLAN_DEBUG_GNRL, "%s: Powering down PHY(s).\n", dev->name);
  2051. value = MII_GC_PDOWN | MII_GC_LOOPBK | MII_GC_ISOLATE;
  2052. tlan_mii_sync(dev->base_addr);
  2053. tlan_mii_write_reg(dev, priv->phy[priv->phy_num], MII_GEN_CTL, value);
  2054. if ((priv->phy_num == 0) &&
  2055. (priv->phy[1] != TLAN_PHY_NONE) &&
  2056. (!(priv->adapter->flags & TLAN_ADAPTER_USE_INTERN_10))) {
  2057. tlan_mii_sync(dev->base_addr);
  2058. tlan_mii_write_reg(dev, priv->phy[1], MII_GEN_CTL, value);
  2059. }
  2060. /* Wait for 50 ms and powerup
  2061. * This is abitrary. It is intended to make sure the
  2062. * transceiver settles.
  2063. */
  2064. tlan_set_timer(dev, (HZ/20), TLAN_TIMER_PHY_PUP);
  2065. }
  2066. static void tlan_phy_power_up(struct net_device *dev)
  2067. {
  2068. struct tlan_priv *priv = netdev_priv(dev);
  2069. u16 value;
  2070. TLAN_DBG(TLAN_DEBUG_GNRL, "%s: Powering up PHY.\n", dev->name);
  2071. tlan_mii_sync(dev->base_addr);
  2072. value = MII_GC_LOOPBK;
  2073. tlan_mii_write_reg(dev, priv->phy[priv->phy_num], MII_GEN_CTL, value);
  2074. tlan_mii_sync(dev->base_addr);
  2075. /* Wait for 500 ms and reset the
  2076. * transceiver. The TLAN docs say both 50 ms and
  2077. * 500 ms, so do the longer, just in case.
  2078. */
  2079. tlan_set_timer(dev, (HZ/20), TLAN_TIMER_PHY_RESET);
  2080. }
  2081. static void tlan_phy_reset(struct net_device *dev)
  2082. {
  2083. struct tlan_priv *priv = netdev_priv(dev);
  2084. u16 phy;
  2085. u16 value;
  2086. phy = priv->phy[priv->phy_num];
  2087. TLAN_DBG(TLAN_DEBUG_GNRL, "%s: Reseting PHY.\n", dev->name);
  2088. tlan_mii_sync(dev->base_addr);
  2089. value = MII_GC_LOOPBK | MII_GC_RESET;
  2090. tlan_mii_write_reg(dev, phy, MII_GEN_CTL, value);
  2091. tlan_mii_read_reg(dev, phy, MII_GEN_CTL, &value);
  2092. while (value & MII_GC_RESET)
  2093. tlan_mii_read_reg(dev, phy, MII_GEN_CTL, &value);
  2094. /* Wait for 500 ms and initialize.
  2095. * I don't remember why I wait this long.
  2096. * I've changed this to 50ms, as it seems long enough.
  2097. */
  2098. tlan_set_timer(dev, (HZ/20), TLAN_TIMER_PHY_START_LINK);
  2099. }
  2100. static void tlan_phy_start_link(struct net_device *dev)
  2101. {
  2102. struct tlan_priv *priv = netdev_priv(dev);
  2103. u16 ability;
  2104. u16 control;
  2105. u16 data;
  2106. u16 phy;
  2107. u16 status;
  2108. u16 tctl;
  2109. phy = priv->phy[priv->phy_num];
  2110. TLAN_DBG(TLAN_DEBUG_GNRL, "%s: Trying to activate link.\n", dev->name);
  2111. tlan_mii_read_reg(dev, phy, MII_GEN_STS, &status);
  2112. tlan_mii_read_reg(dev, phy, MII_GEN_STS, &ability);
  2113. if ((status & MII_GS_AUTONEG) &&
  2114. (!priv->aui)) {
  2115. ability = status >> 11;
  2116. if (priv->speed == TLAN_SPEED_10 &&
  2117. priv->duplex == TLAN_DUPLEX_HALF) {
  2118. tlan_mii_write_reg(dev, phy, MII_GEN_CTL, 0x0000);
  2119. } else if (priv->speed == TLAN_SPEED_10 &&
  2120. priv->duplex == TLAN_DUPLEX_FULL) {
  2121. priv->tlan_full_duplex = true;
  2122. tlan_mii_write_reg(dev, phy, MII_GEN_CTL, 0x0100);
  2123. } else if (priv->speed == TLAN_SPEED_100 &&
  2124. priv->duplex == TLAN_DUPLEX_HALF) {
  2125. tlan_mii_write_reg(dev, phy, MII_GEN_CTL, 0x2000);
  2126. } else if (priv->speed == TLAN_SPEED_100 &&
  2127. priv->duplex == TLAN_DUPLEX_FULL) {
  2128. priv->tlan_full_duplex = true;
  2129. tlan_mii_write_reg(dev, phy, MII_GEN_CTL, 0x2100);
  2130. } else {
  2131. /* Set Auto-Neg advertisement */
  2132. tlan_mii_write_reg(dev, phy, MII_AN_ADV,
  2133. (ability << 5) | 1);
  2134. /* Enablee Auto-Neg */
  2135. tlan_mii_write_reg(dev, phy, MII_GEN_CTL, 0x1000);
  2136. /* Restart Auto-Neg */
  2137. tlan_mii_write_reg(dev, phy, MII_GEN_CTL, 0x1200);
  2138. /* Wait for 4 sec for autonegotiation
  2139. * to complete. The max spec time is less than this
  2140. * but the card need additional time to start AN.
  2141. * .5 sec should be plenty extra.
  2142. */
  2143. netdev_info(dev, "Starting autonegotiation\n");
  2144. tlan_set_timer(dev, (2*HZ), TLAN_TIMER_PHY_FINISH_AN);
  2145. return;
  2146. }
  2147. }
  2148. if ((priv->aui) && (priv->phy_num != 0)) {
  2149. priv->phy_num = 0;
  2150. data = TLAN_NET_CFG_1FRAG | TLAN_NET_CFG_1CHAN
  2151. | TLAN_NET_CFG_PHY_EN;
  2152. tlan_dio_write16(dev->base_addr, TLAN_NET_CONFIG, data);
  2153. tlan_set_timer(dev, (40*HZ/1000), TLAN_TIMER_PHY_PDOWN);
  2154. return;
  2155. } else if (priv->phy_num == 0) {
  2156. control = 0;
  2157. tlan_mii_read_reg(dev, phy, TLAN_TLPHY_CTL, &tctl);
  2158. if (priv->aui) {
  2159. tctl |= TLAN_TC_AUISEL;
  2160. } else {
  2161. tctl &= ~TLAN_TC_AUISEL;
  2162. if (priv->duplex == TLAN_DUPLEX_FULL) {
  2163. control |= MII_GC_DUPLEX;
  2164. priv->tlan_full_duplex = true;
  2165. }
  2166. if (priv->speed == TLAN_SPEED_100)
  2167. control |= MII_GC_SPEEDSEL;
  2168. }
  2169. tlan_mii_write_reg(dev, phy, MII_GEN_CTL, control);
  2170. tlan_mii_write_reg(dev, phy, TLAN_TLPHY_CTL, tctl);
  2171. }
  2172. /* Wait for 2 sec to give the transceiver time
  2173. * to establish link.
  2174. */
  2175. tlan_set_timer(dev, (4*HZ), TLAN_TIMER_FINISH_RESET);
  2176. }
  2177. static void tlan_phy_finish_auto_neg(struct net_device *dev)
  2178. {
  2179. struct tlan_priv *priv = netdev_priv(dev);
  2180. u16 an_adv;
  2181. u16 an_lpa;
  2182. u16 data;
  2183. u16 mode;
  2184. u16 phy;
  2185. u16 status;
  2186. phy = priv->phy[priv->phy_num];
  2187. tlan_mii_read_reg(dev, phy, MII_GEN_STS, &status);
  2188. udelay(1000);
  2189. tlan_mii_read_reg(dev, phy, MII_GEN_STS, &status);
  2190. if (!(status & MII_GS_AUTOCMPLT)) {
  2191. /* Wait for 8 sec to give the process
  2192. * more time. Perhaps we should fail after a while.
  2193. */
  2194. if (!priv->neg_be_verbose++) {
  2195. pr_info("Giving autonegotiation more time.\n");
  2196. pr_info("Please check that your adapter has\n");
  2197. pr_info("been properly connected to a HUB or Switch.\n");
  2198. pr_info("Trying to establish link in the background...\n");
  2199. }
  2200. tlan_set_timer(dev, (8*HZ), TLAN_TIMER_PHY_FINISH_AN);
  2201. return;
  2202. }
  2203. netdev_info(dev, "Autonegotiation complete\n");
  2204. tlan_mii_read_reg(dev, phy, MII_AN_ADV, &an_adv);
  2205. tlan_mii_read_reg(dev, phy, MII_AN_LPA, &an_lpa);
  2206. mode = an_adv & an_lpa & 0x03E0;
  2207. if (mode & 0x0100)
  2208. priv->tlan_full_duplex = true;
  2209. else if (!(mode & 0x0080) && (mode & 0x0040))
  2210. priv->tlan_full_duplex = true;
  2211. if ((!(mode & 0x0180)) &&
  2212. (priv->adapter->flags & TLAN_ADAPTER_USE_INTERN_10) &&
  2213. (priv->phy_num != 0)) {
  2214. priv->phy_num = 0;
  2215. data = TLAN_NET_CFG_1FRAG | TLAN_NET_CFG_1CHAN
  2216. | TLAN_NET_CFG_PHY_EN;
  2217. tlan_dio_write16(dev->base_addr, TLAN_NET_CONFIG, data);
  2218. tlan_set_timer(dev, (400*HZ/1000), TLAN_TIMER_PHY_PDOWN);
  2219. return;
  2220. }
  2221. if (priv->phy_num == 0) {
  2222. if ((priv->duplex == TLAN_DUPLEX_FULL) ||
  2223. (an_adv & an_lpa & 0x0040)) {
  2224. tlan_mii_write_reg(dev, phy, MII_GEN_CTL,
  2225. MII_GC_AUTOENB | MII_GC_DUPLEX);
  2226. netdev_info(dev, "Starting internal PHY with FULL-DUPLEX\n");
  2227. } else {
  2228. tlan_mii_write_reg(dev, phy, MII_GEN_CTL,
  2229. MII_GC_AUTOENB);
  2230. netdev_info(dev, "Starting internal PHY with HALF-DUPLEX\n");
  2231. }
  2232. }
  2233. /* Wait for 100 ms. No reason in partiticular.
  2234. */
  2235. tlan_set_timer(dev, (HZ/10), TLAN_TIMER_FINISH_RESET);
  2236. }
  2237. #ifdef MONITOR
  2238. /*********************************************************************
  2239. *
  2240. * tlan_phy_monitor
  2241. *
  2242. * Returns:
  2243. * None
  2244. *
  2245. * Params:
  2246. * dev The device structure of this device.
  2247. *
  2248. *
  2249. * This function monitors PHY condition by reading the status
  2250. * register via the MII bus. This can be used to give info
  2251. * about link changes (up/down), and possible switch to alternate
  2252. * media.
  2253. *
  2254. *******************************************************************/
  2255. void tlan_phy_monitor(struct net_device *dev)
  2256. {
  2257. struct tlan_priv *priv = netdev_priv(dev);
  2258. u16 phy;
  2259. u16 phy_status;
  2260. phy = priv->phy[priv->phy_num];
  2261. /* Get PHY status register */
  2262. tlan_mii_read_reg(dev, phy, MII_GEN_STS, &phy_status);
  2263. /* Check if link has been lost */
  2264. if (!(phy_status & MII_GS_LINK)) {
  2265. if (priv->link) {
  2266. priv->link = 0;
  2267. printk(KERN_DEBUG "TLAN: %s has lost link\n",
  2268. dev->name);
  2269. netif_carrier_off(dev);
  2270. tlan_set_timer(dev, (2*HZ), TLAN_TIMER_LINK_BEAT);
  2271. return;
  2272. }
  2273. }
  2274. /* Link restablished? */
  2275. if ((phy_status & MII_GS_LINK) && !priv->link) {
  2276. priv->link = 1;
  2277. printk(KERN_DEBUG "TLAN: %s has reestablished link\n",
  2278. dev->name);
  2279. netif_carrier_on(dev);
  2280. }
  2281. /* Setup a new monitor */
  2282. tlan_set_timer(dev, (2*HZ), TLAN_TIMER_LINK_BEAT);
  2283. }
  2284. #endif /* MONITOR */
  2285. /*****************************************************************************
  2286. ******************************************************************************
  2287. ThunderLAN driver MII routines
  2288. these routines are based on the information in chap. 2 of the
  2289. "ThunderLAN Programmer's Guide", pp. 15-24.
  2290. ******************************************************************************
  2291. *****************************************************************************/
  2292. /***************************************************************
  2293. * tlan_mii_read_reg
  2294. *
  2295. * Returns:
  2296. * false if ack received ok
  2297. * true if no ack received or other error
  2298. *
  2299. * Parms:
  2300. * dev The device structure containing
  2301. * The io address and interrupt count
  2302. * for this device.
  2303. * phy The address of the PHY to be queried.
  2304. * reg The register whose contents are to be
  2305. * retrieved.
  2306. * val A pointer to a variable to store the
  2307. * retrieved value.
  2308. *
  2309. * This function uses the TLAN's MII bus to retrieve the contents
  2310. * of a given register on a PHY. It sends the appropriate info
  2311. * and then reads the 16-bit register value from the MII bus via
  2312. * the TLAN SIO register.
  2313. *
  2314. **************************************************************/
  2315. static bool
  2316. tlan_mii_read_reg(struct net_device *dev, u16 phy, u16 reg, u16 *val)
  2317. {
  2318. u8 nack;
  2319. u16 sio, tmp;
  2320. u32 i;
  2321. bool err;
  2322. int minten;
  2323. struct tlan_priv *priv = netdev_priv(dev);
  2324. unsigned long flags = 0;
  2325. err = false;
  2326. outw(TLAN_NET_SIO, dev->base_addr + TLAN_DIO_ADR);
  2327. sio = dev->base_addr + TLAN_DIO_DATA + TLAN_NET_SIO;
  2328. if (!in_irq())
  2329. spin_lock_irqsave(&priv->lock, flags);
  2330. tlan_mii_sync(dev->base_addr);
  2331. minten = tlan_get_bit(TLAN_NET_SIO_MINTEN, sio);
  2332. if (minten)
  2333. tlan_clear_bit(TLAN_NET_SIO_MINTEN, sio);
  2334. tlan_mii_send_data(dev->base_addr, 0x1, 2); /* start (01b) */
  2335. tlan_mii_send_data(dev->base_addr, 0x2, 2); /* read (10b) */
  2336. tlan_mii_send_data(dev->base_addr, phy, 5); /* device # */
  2337. tlan_mii_send_data(dev->base_addr, reg, 5); /* register # */
  2338. tlan_clear_bit(TLAN_NET_SIO_MTXEN, sio); /* change direction */
  2339. tlan_clear_bit(TLAN_NET_SIO_MCLK, sio); /* clock idle bit */
  2340. tlan_set_bit(TLAN_NET_SIO_MCLK, sio);
  2341. tlan_clear_bit(TLAN_NET_SIO_MCLK, sio); /* wait 300ns */
  2342. nack = tlan_get_bit(TLAN_NET_SIO_MDATA, sio); /* check for ACK */
  2343. tlan_set_bit(TLAN_NET_SIO_MCLK, sio); /* finish ACK */
  2344. if (nack) { /* no ACK, so fake it */
  2345. for (i = 0; i < 16; i++) {
  2346. tlan_clear_bit(TLAN_NET_SIO_MCLK, sio);
  2347. tlan_set_bit(TLAN_NET_SIO_MCLK, sio);
  2348. }
  2349. tmp = 0xffff;
  2350. err = true;
  2351. } else { /* ACK, so read data */
  2352. for (tmp = 0, i = 0x8000; i; i >>= 1) {
  2353. tlan_clear_bit(TLAN_NET_SIO_MCLK, sio);
  2354. if (tlan_get_bit(TLAN_NET_SIO_MDATA, sio))
  2355. tmp |= i;
  2356. tlan_set_bit(TLAN_NET_SIO_MCLK, sio);
  2357. }
  2358. }
  2359. tlan_clear_bit(TLAN_NET_SIO_MCLK, sio); /* idle cycle */
  2360. tlan_set_bit(TLAN_NET_SIO_MCLK, sio);
  2361. if (minten)
  2362. tlan_set_bit(TLAN_NET_SIO_MINTEN, sio);
  2363. *val = tmp;
  2364. if (!in_irq())
  2365. spin_unlock_irqrestore(&priv->lock, flags);
  2366. return err;
  2367. }
  2368. /***************************************************************
  2369. * tlan_mii_send_data
  2370. *
  2371. * Returns:
  2372. * Nothing
  2373. * Parms:
  2374. * base_port The base IO port of the adapter in
  2375. * question.
  2376. * dev The address of the PHY to be queried.
  2377. * data The value to be placed on the MII bus.
  2378. * num_bits The number of bits in data that are to
  2379. * be placed on the MII bus.
  2380. *
  2381. * This function sends on sequence of bits on the MII
  2382. * configuration bus.
  2383. *
  2384. **************************************************************/
  2385. static void tlan_mii_send_data(u16 base_port, u32 data, unsigned num_bits)
  2386. {
  2387. u16 sio;
  2388. u32 i;
  2389. if (num_bits == 0)
  2390. return;
  2391. outw(TLAN_NET_SIO, base_port + TLAN_DIO_ADR);
  2392. sio = base_port + TLAN_DIO_DATA + TLAN_NET_SIO;
  2393. tlan_set_bit(TLAN_NET_SIO_MTXEN, sio);
  2394. for (i = (0x1 << (num_bits - 1)); i; i >>= 1) {
  2395. tlan_clear_bit(TLAN_NET_SIO_MCLK, sio);
  2396. (void) tlan_get_bit(TLAN_NET_SIO_MCLK, sio);
  2397. if (data & i)
  2398. tlan_set_bit(TLAN_NET_SIO_MDATA, sio);
  2399. else
  2400. tlan_clear_bit(TLAN_NET_SIO_MDATA, sio);
  2401. tlan_set_bit(TLAN_NET_SIO_MCLK, sio);
  2402. (void) tlan_get_bit(TLAN_NET_SIO_MCLK, sio);
  2403. }
  2404. }
  2405. /***************************************************************
  2406. * TLan_MiiSync
  2407. *
  2408. * Returns:
  2409. * Nothing
  2410. * Parms:
  2411. * base_port The base IO port of the adapter in
  2412. * question.
  2413. *
  2414. * This functions syncs all PHYs in terms of the MII configuration
  2415. * bus.
  2416. *
  2417. **************************************************************/
  2418. static void tlan_mii_sync(u16 base_port)
  2419. {
  2420. int i;
  2421. u16 sio;
  2422. outw(TLAN_NET_SIO, base_port + TLAN_DIO_ADR);
  2423. sio = base_port + TLAN_DIO_DATA + TLAN_NET_SIO;
  2424. tlan_clear_bit(TLAN_NET_SIO_MTXEN, sio);
  2425. for (i = 0; i < 32; i++) {
  2426. tlan_clear_bit(TLAN_NET_SIO_MCLK, sio);
  2427. tlan_set_bit(TLAN_NET_SIO_MCLK, sio);
  2428. }
  2429. }
  2430. /***************************************************************
  2431. * tlan_mii_write_reg
  2432. *
  2433. * Returns:
  2434. * Nothing
  2435. * Parms:
  2436. * dev The device structure for the device
  2437. * to write to.
  2438. * phy The address of the PHY to be written to.
  2439. * reg The register whose contents are to be
  2440. * written.
  2441. * val The value to be written to the register.
  2442. *
  2443. * This function uses the TLAN's MII bus to write the contents of a
  2444. * given register on a PHY. It sends the appropriate info and then
  2445. * writes the 16-bit register value from the MII configuration bus
  2446. * via the TLAN SIO register.
  2447. *
  2448. **************************************************************/
  2449. static void
  2450. tlan_mii_write_reg(struct net_device *dev, u16 phy, u16 reg, u16 val)
  2451. {
  2452. u16 sio;
  2453. int minten;
  2454. unsigned long flags = 0;
  2455. struct tlan_priv *priv = netdev_priv(dev);
  2456. outw(TLAN_NET_SIO, dev->base_addr + TLAN_DIO_ADR);
  2457. sio = dev->base_addr + TLAN_DIO_DATA + TLAN_NET_SIO;
  2458. if (!in_irq())
  2459. spin_lock_irqsave(&priv->lock, flags);
  2460. tlan_mii_sync(dev->base_addr);
  2461. minten = tlan_get_bit(TLAN_NET_SIO_MINTEN, sio);
  2462. if (minten)
  2463. tlan_clear_bit(TLAN_NET_SIO_MINTEN, sio);
  2464. tlan_mii_send_data(dev->base_addr, 0x1, 2); /* start (01b) */
  2465. tlan_mii_send_data(dev->base_addr, 0x1, 2); /* write (01b) */
  2466. tlan_mii_send_data(dev->base_addr, phy, 5); /* device # */
  2467. tlan_mii_send_data(dev->base_addr, reg, 5); /* register # */
  2468. tlan_mii_send_data(dev->base_addr, 0x2, 2); /* send ACK */
  2469. tlan_mii_send_data(dev->base_addr, val, 16); /* send data */
  2470. tlan_clear_bit(TLAN_NET_SIO_MCLK, sio); /* idle cycle */
  2471. tlan_set_bit(TLAN_NET_SIO_MCLK, sio);
  2472. if (minten)
  2473. tlan_set_bit(TLAN_NET_SIO_MINTEN, sio);
  2474. if (!in_irq())
  2475. spin_unlock_irqrestore(&priv->lock, flags);
  2476. }
  2477. /*****************************************************************************
  2478. ******************************************************************************
  2479. ThunderLAN driver eeprom routines
  2480. the Compaq netelligent 10 and 10/100 cards use a microchip 24C02A
  2481. EEPROM. these functions are based on information in microchip's
  2482. data sheet. I don't know how well this functions will work with
  2483. other Eeproms.
  2484. ******************************************************************************
  2485. *****************************************************************************/
  2486. /***************************************************************
  2487. * tlan_ee_send_start
  2488. *
  2489. * Returns:
  2490. * Nothing
  2491. * Parms:
  2492. * io_base The IO port base address for the
  2493. * TLAN device with the EEPROM to
  2494. * use.
  2495. *
  2496. * This function sends a start cycle to an EEPROM attached
  2497. * to a TLAN chip.
  2498. *
  2499. **************************************************************/
  2500. static void tlan_ee_send_start(u16 io_base)
  2501. {
  2502. u16 sio;
  2503. outw(TLAN_NET_SIO, io_base + TLAN_DIO_ADR);
  2504. sio = io_base + TLAN_DIO_DATA + TLAN_NET_SIO;
  2505. tlan_set_bit(TLAN_NET_SIO_ECLOK, sio);
  2506. tlan_set_bit(TLAN_NET_SIO_EDATA, sio);
  2507. tlan_set_bit(TLAN_NET_SIO_ETXEN, sio);
  2508. tlan_clear_bit(TLAN_NET_SIO_EDATA, sio);
  2509. tlan_clear_bit(TLAN_NET_SIO_ECLOK, sio);
  2510. }
  2511. /***************************************************************
  2512. * tlan_ee_send_byte
  2513. *
  2514. * Returns:
  2515. * If the correct ack was received, 0, otherwise 1
  2516. * Parms: io_base The IO port base address for the
  2517. * TLAN device with the EEPROM to
  2518. * use.
  2519. * data The 8 bits of information to
  2520. * send to the EEPROM.
  2521. * stop If TLAN_EEPROM_STOP is passed, a
  2522. * stop cycle is sent after the
  2523. * byte is sent after the ack is
  2524. * read.
  2525. *
  2526. * This function sends a byte on the serial EEPROM line,
  2527. * driving the clock to send each bit. The function then
  2528. * reverses transmission direction and reads an acknowledge
  2529. * bit.
  2530. *
  2531. **************************************************************/
  2532. static int tlan_ee_send_byte(u16 io_base, u8 data, int stop)
  2533. {
  2534. int err;
  2535. u8 place;
  2536. u16 sio;
  2537. outw(TLAN_NET_SIO, io_base + TLAN_DIO_ADR);
  2538. sio = io_base + TLAN_DIO_DATA + TLAN_NET_SIO;
  2539. /* Assume clock is low, tx is enabled; */
  2540. for (place = 0x80; place != 0; place >>= 1) {
  2541. if (place & data)
  2542. tlan_set_bit(TLAN_NET_SIO_EDATA, sio);
  2543. else
  2544. tlan_clear_bit(TLAN_NET_SIO_EDATA, sio);
  2545. tlan_set_bit(TLAN_NET_SIO_ECLOK, sio);
  2546. tlan_clear_bit(TLAN_NET_SIO_ECLOK, sio);
  2547. }
  2548. tlan_clear_bit(TLAN_NET_SIO_ETXEN, sio);
  2549. tlan_set_bit(TLAN_NET_SIO_ECLOK, sio);
  2550. err = tlan_get_bit(TLAN_NET_SIO_EDATA, sio);
  2551. tlan_clear_bit(TLAN_NET_SIO_ECLOK, sio);
  2552. tlan_set_bit(TLAN_NET_SIO_ETXEN, sio);
  2553. if ((!err) && stop) {
  2554. /* STOP, raise data while clock is high */
  2555. tlan_clear_bit(TLAN_NET_SIO_EDATA, sio);
  2556. tlan_set_bit(TLAN_NET_SIO_ECLOK, sio);
  2557. tlan_set_bit(TLAN_NET_SIO_EDATA, sio);
  2558. }
  2559. return err;
  2560. }
  2561. /***************************************************************
  2562. * tlan_ee_receive_byte
  2563. *
  2564. * Returns:
  2565. * Nothing
  2566. * Parms:
  2567. * io_base The IO port base address for the
  2568. * TLAN device with the EEPROM to
  2569. * use.
  2570. * data An address to a char to hold the
  2571. * data sent from the EEPROM.
  2572. * stop If TLAN_EEPROM_STOP is passed, a
  2573. * stop cycle is sent after the
  2574. * byte is received, and no ack is
  2575. * sent.
  2576. *
  2577. * This function receives 8 bits of data from the EEPROM
  2578. * over the serial link. It then sends and ack bit, or no
  2579. * ack and a stop bit. This function is used to retrieve
  2580. * data after the address of a byte in the EEPROM has been
  2581. * sent.
  2582. *
  2583. **************************************************************/
  2584. static void tlan_ee_receive_byte(u16 io_base, u8 *data, int stop)
  2585. {
  2586. u8 place;
  2587. u16 sio;
  2588. outw(TLAN_NET_SIO, io_base + TLAN_DIO_ADR);
  2589. sio = io_base + TLAN_DIO_DATA + TLAN_NET_SIO;
  2590. *data = 0;
  2591. /* Assume clock is low, tx is enabled; */
  2592. tlan_clear_bit(TLAN_NET_SIO_ETXEN, sio);
  2593. for (place = 0x80; place; place >>= 1) {
  2594. tlan_set_bit(TLAN_NET_SIO_ECLOK, sio);
  2595. if (tlan_get_bit(TLAN_NET_SIO_EDATA, sio))
  2596. *data |= place;
  2597. tlan_clear_bit(TLAN_NET_SIO_ECLOK, sio);
  2598. }
  2599. tlan_set_bit(TLAN_NET_SIO_ETXEN, sio);
  2600. if (!stop) {
  2601. tlan_clear_bit(TLAN_NET_SIO_EDATA, sio); /* ack = 0 */
  2602. tlan_set_bit(TLAN_NET_SIO_ECLOK, sio);
  2603. tlan_clear_bit(TLAN_NET_SIO_ECLOK, sio);
  2604. } else {
  2605. tlan_set_bit(TLAN_NET_SIO_EDATA, sio); /* no ack = 1 (?) */
  2606. tlan_set_bit(TLAN_NET_SIO_ECLOK, sio);
  2607. tlan_clear_bit(TLAN_NET_SIO_ECLOK, sio);
  2608. /* STOP, raise data while clock is high */
  2609. tlan_clear_bit(TLAN_NET_SIO_EDATA, sio);
  2610. tlan_set_bit(TLAN_NET_SIO_ECLOK, sio);
  2611. tlan_set_bit(TLAN_NET_SIO_EDATA, sio);
  2612. }
  2613. }
  2614. /***************************************************************
  2615. * tlan_ee_read_byte
  2616. *
  2617. * Returns:
  2618. * No error = 0, else, the stage at which the error
  2619. * occurred.
  2620. * Parms:
  2621. * io_base The IO port base address for the
  2622. * TLAN device with the EEPROM to
  2623. * use.
  2624. * ee_addr The address of the byte in the
  2625. * EEPROM whose contents are to be
  2626. * retrieved.
  2627. * data An address to a char to hold the
  2628. * data obtained from the EEPROM.
  2629. *
  2630. * This function reads a byte of information from an byte
  2631. * cell in the EEPROM.
  2632. *
  2633. **************************************************************/
  2634. static int tlan_ee_read_byte(struct net_device *dev, u8 ee_addr, u8 *data)
  2635. {
  2636. int err;
  2637. struct tlan_priv *priv = netdev_priv(dev);
  2638. unsigned long flags = 0;
  2639. int ret = 0;
  2640. spin_lock_irqsave(&priv->lock, flags);
  2641. tlan_ee_send_start(dev->base_addr);
  2642. err = tlan_ee_send_byte(dev->base_addr, 0xa0, TLAN_EEPROM_ACK);
  2643. if (err) {
  2644. ret = 1;
  2645. goto fail;
  2646. }
  2647. err = tlan_ee_send_byte(dev->base_addr, ee_addr, TLAN_EEPROM_ACK);
  2648. if (err) {
  2649. ret = 2;
  2650. goto fail;
  2651. }
  2652. tlan_ee_send_start(dev->base_addr);
  2653. err = tlan_ee_send_byte(dev->base_addr, 0xa1, TLAN_EEPROM_ACK);
  2654. if (err) {
  2655. ret = 3;
  2656. goto fail;
  2657. }
  2658. tlan_ee_receive_byte(dev->base_addr, data, TLAN_EEPROM_STOP);
  2659. fail:
  2660. spin_unlock_irqrestore(&priv->lock, flags);
  2661. return ret;
  2662. }