tlan.c 84 KB

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