tlan.c 86 KB

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