You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mtd80x.c 36KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  1. /**************************************************************************
  2. *
  3. * mtd80x.c: Etherboot device driver for the mtd80x Ethernet chip.
  4. * Written 2004-2004 by Erdem Güven <zuencap@yahoo.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. *
  20. * Portions of this code based on:
  21. * fealnx.c: A Linux device driver for the mtd80x Ethernet chip
  22. * Written 1998-2000 by Donald Becker
  23. *
  24. ***************************************************************************/
  25. /* to get some global routines like printf */
  26. #include "etherboot.h"
  27. /* to get the interface to the body of the program */
  28. #include "nic.h"
  29. /* to get the PCI support functions, if this is a PCI NIC */
  30. #include "pci.h"
  31. #if 0
  32. #define DBGPRNT( x ) printf x
  33. #else
  34. #define DBGPRNT( x )
  35. #endif
  36. typedef unsigned char u8;
  37. typedef signed char s8;
  38. typedef unsigned short u16;
  39. typedef signed short s16;
  40. typedef unsigned int u32;
  41. typedef signed int s32;
  42. /* Condensed operations for readability. */
  43. #define virt_to_le32desc(addr) cpu_to_le32(virt_to_bus(addr))
  44. #define le32desc_to_virt(addr) bus_to_virt(le32_to_cpu(addr))
  45. #define get_unaligned(ptr) (*(ptr))
  46. /* Operational parameters that are set at compile time. */
  47. /* Keep the ring sizes a power of two for compile efficiency. */
  48. /* The compiler will convert <unsigned>'%'<2^N> into a bit mask. */
  49. /* Making the Tx ring too large decreases the effectiveness of channel */
  50. /* bonding and packet priority. */
  51. /* There are no ill effects from too-large receive rings. */
  52. #define TX_RING_SIZE 2
  53. #define TX_QUEUE_LEN 10 /* Limit ring entries actually used. */
  54. #define RX_RING_SIZE 4
  55. /* Operational parameters that usually are not changed. */
  56. /* Time in jiffies before concluding the transmitter is hung. */
  57. #define HZ 100
  58. #define TX_TIME_OUT (6*HZ)
  59. /* Allocation size of Rx buffers with normal sized Ethernet frames.
  60. Do not change this value without good reason. This is not a limit,
  61. but a way to keep a consistent allocation size among drivers.
  62. */
  63. #define PKT_BUF_SZ 1536
  64. /* Generic MII registers. */
  65. #define MII_BMCR 0x00 /* Basic mode control register */
  66. #define MII_BMSR 0x01 /* Basic mode status register */
  67. #define MII_PHYSID1 0x02 /* PHYS ID 1 */
  68. #define MII_PHYSID2 0x03 /* PHYS ID 2 */
  69. #define MII_ADVERTISE 0x04 /* Advertisement control reg */
  70. #define MII_LPA 0x05 /* Link partner ability reg */
  71. #define MII_EXPANSION 0x06 /* Expansion register */
  72. #define MII_DCOUNTER 0x12 /* Disconnect counter */
  73. #define MII_FCSCOUNTER 0x13 /* False carrier counter */
  74. #define MII_NWAYTEST 0x14 /* N-way auto-neg test reg */
  75. #define MII_RERRCOUNTER 0x15 /* Receive error counter */
  76. #define MII_SREVISION 0x16 /* Silicon revision */
  77. #define MII_RESV1 0x17 /* Reserved... */
  78. #define MII_LBRERROR 0x18 /* Lpback, rx, bypass error */
  79. #define MII_PHYADDR 0x19 /* PHY address */
  80. #define MII_RESV2 0x1a /* Reserved... */
  81. #define MII_TPISTATUS 0x1b /* TPI status for 10mbps */
  82. #define MII_NCONFIG 0x1c /* Network interface config */
  83. /* Basic mode control register. */
  84. #define BMCR_RESV 0x007f /* Unused... */
  85. #define BMCR_CTST 0x0080 /* Collision test */
  86. #define BMCR_FULLDPLX 0x0100 /* Full duplex */
  87. #define BMCR_ANRESTART 0x0200 /* Auto negotiation restart */
  88. #define BMCR_ISOLATE 0x0400 /* Disconnect DP83840 from MII */
  89. #define BMCR_PDOWN 0x0800 /* Powerdown the DP83840 */
  90. #define BMCR_ANENABLE 0x1000 /* Enable auto negotiation */
  91. #define BMCR_SPEED100 0x2000 /* Select 100Mbps */
  92. #define BMCR_LOOPBACK 0x4000 /* TXD loopback bits */
  93. #define BMCR_RESET 0x8000 /* Reset the DP83840 */
  94. /* Basic mode status register. */
  95. #define BMSR_ERCAP 0x0001 /* Ext-reg capability */
  96. #define BMSR_JCD 0x0002 /* Jabber detected */
  97. #define BMSR_LSTATUS 0x0004 /* Link status */
  98. #define BMSR_ANEGCAPABLE 0x0008 /* Able to do auto-negotiation */
  99. #define BMSR_RFAULT 0x0010 /* Remote fault detected */
  100. #define BMSR_ANEGCOMPLETE 0x0020 /* Auto-negotiation complete */
  101. #define BMSR_RESV 0x07c0 /* Unused... */
  102. #define BMSR_10HALF 0x0800 /* Can do 10mbps, half-duplex */
  103. #define BMSR_10FULL 0x1000 /* Can do 10mbps, full-duplex */
  104. #define BMSR_100HALF 0x2000 /* Can do 100mbps, half-duplex */
  105. #define BMSR_100FULL 0x4000 /* Can do 100mbps, full-duplex */
  106. #define BMSR_100BASE4 0x8000 /* Can do 100mbps, 4k packets */
  107. /* Advertisement control register. */
  108. #define ADVERTISE_SLCT 0x001f /* Selector bits */
  109. #define ADVERTISE_CSMA 0x0001 /* Only selector supported */
  110. #define ADVERTISE_10HALF 0x0020 /* Try for 10mbps half-duplex */
  111. #define ADVERTISE_10FULL 0x0040 /* Try for 10mbps full-duplex */
  112. #define ADVERTISE_100HALF 0x0080 /* Try for 100mbps half-duplex */
  113. #define ADVERTISE_100FULL 0x0100 /* Try for 100mbps full-duplex */
  114. #define ADVERTISE_100BASE4 0x0200 /* Try for 100mbps 4k packets */
  115. #define ADVERTISE_RESV 0x1c00 /* Unused... */
  116. #define ADVERTISE_RFAULT 0x2000 /* Say we can detect faults */
  117. #define ADVERTISE_LPACK 0x4000 /* Ack link partners response */
  118. #define ADVERTISE_NPAGE 0x8000 /* Next page bit */
  119. #define ADVERTISE_FULL (ADVERTISE_100FULL | ADVERTISE_10FULL | \
  120. ADVERTISE_CSMA)
  121. #define ADVERTISE_ALL (ADVERTISE_10HALF | ADVERTISE_10FULL | \
  122. ADVERTISE_100HALF | ADVERTISE_100FULL)
  123. /* for different PHY */
  124. enum phy_type_flags {
  125. MysonPHY = 1,
  126. AhdocPHY = 2,
  127. SeeqPHY = 3,
  128. MarvellPHY = 4,
  129. Myson981 = 5,
  130. LevelOnePHY = 6,
  131. OtherPHY = 10,
  132. };
  133. /* A chip capabilities table*/
  134. enum chip_capability_flags {
  135. HAS_MII_XCVR,
  136. HAS_CHIP_XCVR,
  137. };
  138. static
  139. struct chip_info
  140. {
  141. u16 dev_id;
  142. int flag;
  143. }
  144. mtd80x_chips[] = {
  145. {0x0800, HAS_MII_XCVR},
  146. {0x0803, HAS_CHIP_XCVR},
  147. {0x0891, HAS_MII_XCVR}
  148. };
  149. static int chip_cnt = sizeof( mtd80x_chips ) / sizeof( struct chip_info );
  150. /* Offsets to the Command and Status Registers. */
  151. enum mtd_offsets {
  152. PAR0 = 0x0, /* physical address 0-3 */
  153. PAR1 = 0x04, /* physical address 4-5 */
  154. MAR0 = 0x08, /* multicast address 0-3 */
  155. MAR1 = 0x0C, /* multicast address 4-7 */
  156. FAR0 = 0x10, /* flow-control address 0-3 */
  157. FAR1 = 0x14, /* flow-control address 4-5 */
  158. TCRRCR = 0x18, /* receive & transmit configuration */
  159. BCR = 0x1C, /* bus command */
  160. TXPDR = 0x20, /* transmit polling demand */
  161. RXPDR = 0x24, /* receive polling demand */
  162. RXCWP = 0x28, /* receive current word pointer */
  163. TXLBA = 0x2C, /* transmit list base address */
  164. RXLBA = 0x30, /* receive list base address */
  165. ISR = 0x34, /* interrupt status */
  166. IMR = 0x38, /* interrupt mask */
  167. FTH = 0x3C, /* flow control high/low threshold */
  168. MANAGEMENT = 0x40, /* bootrom/eeprom and mii management */
  169. TALLY = 0x44, /* tally counters for crc and mpa */
  170. TSR = 0x48, /* tally counter for transmit status */
  171. BMCRSR = 0x4c, /* basic mode control and status */
  172. PHYIDENTIFIER = 0x50, /* phy identifier */
  173. ANARANLPAR = 0x54, /* auto-negotiation advertisement and link
  174. partner ability */
  175. ANEROCR = 0x58, /* auto-negotiation expansion and pci conf. */
  176. BPREMRPSR = 0x5c, /* bypass & receive error mask and phy status */
  177. };
  178. /* Bits in the interrupt status/enable registers. */
  179. /* The bits in the Intr Status/Enable registers, mostly interrupt sources. */
  180. enum intr_status_bits {
  181. RFCON = 0x00020000, /* receive flow control xon packet */
  182. RFCOFF = 0x00010000, /* receive flow control xoff packet */
  183. LSCStatus = 0x00008000, /* link status change */
  184. ANCStatus = 0x00004000, /* autonegotiation completed */
  185. FBE = 0x00002000, /* fatal bus error */
  186. FBEMask = 0x00001800, /* mask bit12-11 */
  187. ParityErr = 0x00000000, /* parity error */
  188. TargetErr = 0x00001000, /* target abort */
  189. MasterErr = 0x00000800, /* master error */
  190. TUNF = 0x00000400, /* transmit underflow */
  191. ROVF = 0x00000200, /* receive overflow */
  192. ETI = 0x00000100, /* transmit early int */
  193. ERI = 0x00000080, /* receive early int */
  194. CNTOVF = 0x00000040, /* counter overflow */
  195. RBU = 0x00000020, /* receive buffer unavailable */
  196. TBU = 0x00000010, /* transmit buffer unavilable */
  197. TI = 0x00000008, /* transmit interrupt */
  198. RI = 0x00000004, /* receive interrupt */
  199. RxErr = 0x00000002, /* receive error */
  200. };
  201. /* Bits in the NetworkConfig register. */
  202. enum rx_mode_bits {
  203. RxModeMask = 0xe0,
  204. AcceptAllPhys = 0x80, /* promiscuous mode */
  205. AcceptBroadcast = 0x40, /* accept broadcast */
  206. AcceptMulticast = 0x20, /* accept mutlicast */
  207. AcceptRunt = 0x08, /* receive runt pkt */
  208. ALP = 0x04, /* receive long pkt */
  209. AcceptErr = 0x02, /* receive error pkt */
  210. AcceptMyPhys = 0x00000000,
  211. RxEnable = 0x00000001,
  212. RxFlowCtrl = 0x00002000,
  213. TxEnable = 0x00040000,
  214. TxModeFDX = 0x00100000,
  215. TxThreshold = 0x00e00000,
  216. PS1000 = 0x00010000,
  217. PS10 = 0x00080000,
  218. FD = 0x00100000,
  219. };
  220. /* Bits in network_desc.status */
  221. enum rx_desc_status_bits {
  222. RXOWN = 0x80000000, /* own bit */
  223. FLNGMASK = 0x0fff0000, /* frame length */
  224. FLNGShift = 16,
  225. MARSTATUS = 0x00004000, /* multicast address received */
  226. BARSTATUS = 0x00002000, /* broadcast address received */
  227. PHYSTATUS = 0x00001000, /* physical address received */
  228. RXFSD = 0x00000800, /* first descriptor */
  229. RXLSD = 0x00000400, /* last descriptor */
  230. ErrorSummary = 0x80, /* error summary */
  231. RUNT = 0x40, /* runt packet received */
  232. LONG = 0x20, /* long packet received */
  233. FAE = 0x10, /* frame align error */
  234. CRC = 0x08, /* crc error */
  235. RXER = 0x04, /* receive error */
  236. };
  237. enum rx_desc_control_bits {
  238. RXIC = 0x00800000, /* interrupt control */
  239. RBSShift = 0,
  240. };
  241. enum tx_desc_status_bits {
  242. TXOWN = 0x80000000, /* own bit */
  243. JABTO = 0x00004000, /* jabber timeout */
  244. CSL = 0x00002000, /* carrier sense lost */
  245. LC = 0x00001000, /* late collision */
  246. EC = 0x00000800, /* excessive collision */
  247. UDF = 0x00000400, /* fifo underflow */
  248. DFR = 0x00000200, /* deferred */
  249. HF = 0x00000100, /* heartbeat fail */
  250. NCRMask = 0x000000ff, /* collision retry count */
  251. NCRShift = 0,
  252. };
  253. enum tx_desc_control_bits {
  254. TXIC = 0x80000000, /* interrupt control */
  255. ETIControl = 0x40000000, /* early transmit interrupt */
  256. TXLD = 0x20000000, /* last descriptor */
  257. TXFD = 0x10000000, /* first descriptor */
  258. CRCEnable = 0x08000000, /* crc control */
  259. PADEnable = 0x04000000, /* padding control */
  260. RetryTxLC = 0x02000000, /* retry late collision */
  261. PKTSMask = 0x3ff800, /* packet size bit21-11 */
  262. PKTSShift = 11,
  263. TBSMask = 0x000007ff, /* transmit buffer bit 10-0 */
  264. TBSShift = 0,
  265. };
  266. /* BootROM/EEPROM/MII Management Register */
  267. #define MASK_MIIR_MII_READ 0x00000000
  268. #define MASK_MIIR_MII_WRITE 0x00000008
  269. #define MASK_MIIR_MII_MDO 0x00000004
  270. #define MASK_MIIR_MII_MDI 0x00000002
  271. #define MASK_MIIR_MII_MDC 0x00000001
  272. /* ST+OP+PHYAD+REGAD+TA */
  273. #define OP_READ 0x6000 /* ST:01+OP:10+PHYAD+REGAD+TA:Z0 */
  274. #define OP_WRITE 0x5002 /* ST:01+OP:01+PHYAD+REGAD+TA:10 */
  275. /* ------------------------------------------------------------------------- */
  276. /* Constants for Myson PHY */
  277. /* ------------------------------------------------------------------------- */
  278. #define MysonPHYID 0xd0000302
  279. /* 89-7-27 add, (begin) */
  280. #define MysonPHYID0 0x0302
  281. #define StatusRegister 18
  282. #define SPEED100 0x0400 // bit10
  283. #define FULLMODE 0x0800 // bit11
  284. /* 89-7-27 add, (end) */
  285. /* ------------------------------------------------------------------------- */
  286. /* Constants for Seeq 80225 PHY */
  287. /* ------------------------------------------------------------------------- */
  288. #define SeeqPHYID0 0x0016
  289. #define MIIRegister18 18
  290. #define SPD_DET_100 0x80
  291. #define DPLX_DET_FULL 0x40
  292. /* ------------------------------------------------------------------------- */
  293. /* Constants for Ahdoc 101 PHY */
  294. /* ------------------------------------------------------------------------- */
  295. #define AhdocPHYID0 0x0022
  296. #define DiagnosticReg 18
  297. #define DPLX_FULL 0x0800
  298. #define Speed_100 0x0400
  299. /* 89/6/13 add, */
  300. /* -------------------------------------------------------------------------- */
  301. /* Constants */
  302. /* -------------------------------------------------------------------------- */
  303. #define MarvellPHYID0 0x0141
  304. #define LevelOnePHYID0 0x0013
  305. #define MII1000BaseTControlReg 9
  306. #define MII1000BaseTStatusReg 10
  307. #define SpecificReg 17
  308. /* for 1000BaseT Control Register */
  309. #define PHYAbletoPerform1000FullDuplex 0x0200
  310. #define PHYAbletoPerform1000HalfDuplex 0x0100
  311. #define PHY1000AbilityMask 0x300
  312. // for phy specific status register, marvell phy.
  313. #define SpeedMask 0x0c000
  314. #define Speed_1000M 0x08000
  315. #define Speed_100M 0x4000
  316. #define Speed_10M 0
  317. #define Full_Duplex 0x2000
  318. // 89/12/29 add, for phy specific status register, levelone phy, (begin)
  319. #define LXT1000_100M 0x08000
  320. #define LXT1000_1000M 0x0c000
  321. #define LXT1000_Full 0x200
  322. // 89/12/29 add, for phy specific status register, levelone phy, (end)
  323. #if 0
  324. /* for 3-in-1 case */
  325. #define PS10 0x00080000
  326. #define FD 0x00100000
  327. #define PS1000 0x00010000
  328. #endif
  329. /* for PHY */
  330. #define LinkIsUp 0x0004
  331. #define LinkIsUp2 0x00040000
  332. /* Create a static buffer of size PKT_BUF_SZ for each
  333. TX Descriptor. All descriptors point to a
  334. part of this buffer */
  335. static u8 txb[PKT_BUF_SZ * TX_RING_SIZE]
  336. __attribute__ ((aligned(8)));
  337. /* Create a static buffer of size PKT_BUF_SZ for each
  338. RX Descriptor All descriptors point to a
  339. part of this buffer */
  340. static u8 rxb[PKT_BUF_SZ * RX_RING_SIZE]
  341. __attribute__ ((aligned(8)));
  342. /* The Tulip Rx and Tx buffer descriptors. */
  343. struct mtd_desc
  344. {
  345. s32 status;
  346. s32 control;
  347. u32 buffer;
  348. u32 next_desc;
  349. struct mtd_desc *next_desc_logical;
  350. u8* skbuff;
  351. u32 reserved1;
  352. u32 reserved2;
  353. };
  354. struct mtd_private
  355. {
  356. struct mtd_desc rx_ring[RX_RING_SIZE];
  357. struct mtd_desc tx_ring[TX_RING_SIZE];
  358. /* Frequently used values: keep some adjacent for cache effect. */
  359. int flags;
  360. struct pci_dev *pci_dev;
  361. unsigned long crvalue;
  362. unsigned long bcrvalue;
  363. /*unsigned long imrvalue;*/
  364. struct mtd_desc *cur_rx;
  365. struct mtd_desc *lack_rxbuf;
  366. int really_rx_count;
  367. struct mtd_desc *cur_tx;
  368. struct mtd_desc *cur_tx_copy;
  369. int really_tx_count;
  370. int free_tx_count;
  371. unsigned int rx_buf_sz; /* Based on MTU+slack. */
  372. /* These values are keep track of the transceiver/media in use. */
  373. unsigned int linkok;
  374. unsigned int line_speed;
  375. unsigned int duplexmode;
  376. unsigned int default_port:
  377. 4; /* Last dev->if_port value. */
  378. unsigned int PHYType;
  379. /* MII transceiver section. */
  380. int mii_cnt; /* MII device addresses. */
  381. unsigned char phys[1]; /* MII device addresses. */
  382. /*other*/
  383. const char *nic_name;
  384. int ioaddr;
  385. u16 dev_id;
  386. };
  387. static struct mtd_private mtdx;
  388. static int mdio_read(struct nic * , int phy_id, int location);
  389. static void mdio_write(struct nic * , int phy_id, int location, int value);
  390. static void getlinktype(struct nic * );
  391. static void getlinkstatus(struct nic * );
  392. static void set_rx_mode(struct nic *);
  393. /**************************************************************************
  394. * init_ring - setup the tx and rx descriptors
  395. *************************************************************************/
  396. static void init_ring(struct nic *nic)
  397. {
  398. int i;
  399. mtdx.cur_rx = &mtdx.rx_ring[0];
  400. mtdx.rx_buf_sz = PKT_BUF_SZ;
  401. /*mtdx.rx_head_desc = &mtdx.rx_ring[0];*/
  402. /* Initialize all Rx descriptors. */
  403. /* Fill in the Rx buffers. Handle allocation failure gracefully. */
  404. for (i = 0; i < RX_RING_SIZE; i++)
  405. {
  406. mtdx.rx_ring[i].status = RXOWN;
  407. mtdx.rx_ring[i].control = mtdx.rx_buf_sz << RBSShift;
  408. mtdx.rx_ring[i].next_desc = virt_to_le32desc(&mtdx.rx_ring[i+1]);
  409. mtdx.rx_ring[i].next_desc_logical = &mtdx.rx_ring[i+1];
  410. mtdx.rx_ring[i].buffer = virt_to_le32desc(&rxb[i * PKT_BUF_SZ]);
  411. mtdx.rx_ring[i].skbuff = &rxb[i * PKT_BUF_SZ];
  412. }
  413. /* Mark the last entry as wrapping the ring. */
  414. mtdx.rx_ring[i-1].next_desc = virt_to_le32desc(&mtdx.rx_ring[0]);
  415. mtdx.rx_ring[i-1].next_desc_logical = &mtdx.rx_ring[0];
  416. /* We only use one transmit buffer, but two
  417. * descriptors so transmit engines have somewhere
  418. * to point should they feel the need */
  419. mtdx.tx_ring[0].status = 0x00000000;
  420. mtdx.tx_ring[0].buffer = virt_to_bus(&txb[0]);
  421. mtdx.tx_ring[0].next_desc = virt_to_le32desc(&mtdx.tx_ring[1]);
  422. /* This descriptor is never used */
  423. mtdx.tx_ring[1].status = 0x00000000;
  424. mtdx.tx_ring[1].buffer = 0; /*virt_to_bus(&txb[1]); */
  425. mtdx.tx_ring[1].next_desc = virt_to_le32desc(&mtdx.tx_ring[0]);
  426. return;
  427. }
  428. /**************************************************************************
  429. RESET - Reset Adapter
  430. ***************************************************************************/
  431. static void mtd_reset(struct nic *nic)
  432. {
  433. /* Reset the chip to erase previous misconfiguration. */
  434. outl(0x00000001, mtdx.ioaddr + BCR);
  435. init_ring(nic);
  436. outl(virt_to_bus(mtdx.rx_ring), mtdx.ioaddr + RXLBA);
  437. outl(virt_to_bus(mtdx.tx_ring), mtdx.ioaddr + TXLBA);
  438. /* Initialize other registers. */
  439. /* Configure the PCI bus bursts and FIFO thresholds. */
  440. mtdx.bcrvalue = 0x10; /* little-endian, 8 burst length */
  441. mtdx.crvalue = 0xa00; /* rx 128 burst length */
  442. if ( mtdx.dev_id == 0x891 ) {
  443. mtdx.bcrvalue |= 0x200; /* set PROG bit */
  444. mtdx.crvalue |= 0x02000000; /* set enhanced bit */
  445. }
  446. outl( mtdx.bcrvalue, mtdx.ioaddr + BCR);
  447. /* Restart Rx engine if stopped. */
  448. outl(0, mtdx.ioaddr + RXPDR);
  449. getlinkstatus(nic);
  450. if (mtdx.linkok)
  451. {
  452. char* texts[]={"half","full","10","100","1000"};
  453. getlinktype(nic);
  454. DBGPRNT(("Link is OK : %s %s\n", texts[mtdx.duplexmode-1], texts[mtdx.line_speed+1] ));
  455. } else
  456. {
  457. DBGPRNT(("No link!!!\n"));
  458. }
  459. mtdx.crvalue |= /*TxEnable |*/ RxEnable | TxThreshold;
  460. set_rx_mode(nic);
  461. /* Clear interrupts by setting the interrupt mask. */
  462. outl(FBE | TUNF | CNTOVF | RBU | TI | RI, mtdx.ioaddr + ISR);
  463. outl( 0, mtdx.ioaddr + IMR);
  464. }
  465. /**************************************************************************
  466. POLL - Wait for a frame
  467. ***************************************************************************/
  468. static int mtd_poll(struct nic *nic)
  469. {
  470. s32 rx_status = mtdx.cur_rx->status;
  471. int retval = 0;
  472. if( ( rx_status & RXOWN ) != 0 )
  473. {
  474. return 0;
  475. }
  476. if (rx_status & ErrorSummary)
  477. { /* there was a fatal error */
  478. printf( "%s: Receive error, Rx status %8.8x, Error(s) %s%s%s\n",
  479. mtdx.nic_name, rx_status ,
  480. (rx_status & (LONG | RUNT)) ? "length_error ":"",
  481. (rx_status & RXER) ? "frame_error ":"",
  482. (rx_status & CRC) ? "crc_error ":"" );
  483. retval = 0;
  484. } else if( !((rx_status & RXFSD) && (rx_status & RXLSD)) )
  485. {
  486. /* this pkt is too long, over one rx buffer */
  487. printf("Pkt is too long, over one rx buffer.\n");
  488. retval = 0;
  489. } else
  490. { /* this received pkt is ok */
  491. /* Omit the four octet CRC from the length. */
  492. short pkt_len = ((rx_status & FLNGMASK) >> FLNGShift) - 4;
  493. DBGPRNT(( " netdev_rx() normal Rx pkt length %d"
  494. " status %x.\n", pkt_len, rx_status));
  495. nic->packetlen = pkt_len;
  496. memcpy(nic->packet, mtdx.cur_rx->skbuff, pkt_len);
  497. retval = 1;
  498. }
  499. while( ( mtdx.cur_rx->status & RXOWN ) == 0 )
  500. {
  501. mtdx.cur_rx->status = RXOWN;
  502. mtdx.cur_rx = mtdx.cur_rx->next_desc_logical;
  503. }
  504. /* Restart Rx engine if stopped. */
  505. outl(0, mtdx.ioaddr + RXPDR);
  506. return retval;
  507. }
  508. /**************************************************************************
  509. TRANSMIT - Transmit a frame
  510. ***************************************************************************/
  511. static void mtd_transmit(
  512. struct nic *nic,
  513. const char *dest, /* Destination */
  514. unsigned int type, /* Type */
  515. unsigned int size, /* size */
  516. const char *data) /* Packet */
  517. {
  518. u32 to;
  519. u32 tx_status;
  520. unsigned int nstype = htons ( type );
  521. memcpy( txb, dest, ETH_ALEN );
  522. memcpy( txb + ETH_ALEN, nic->node_addr, ETH_ALEN );
  523. memcpy( txb + 2 * ETH_ALEN, &nstype, 2 );
  524. memcpy( txb + ETH_HLEN, data, size );
  525. size += ETH_HLEN;
  526. size &= 0x0FFF;
  527. while( size < ETH_ZLEN )
  528. {
  529. txb[size++] = '\0';
  530. }
  531. mtdx.tx_ring[0].control = TXLD | TXFD | CRCEnable | PADEnable;
  532. mtdx.tx_ring[0].control |= (size << PKTSShift); /* pkt size */
  533. mtdx.tx_ring[0].control |= (size << TBSShift); /* buffer size */
  534. mtdx.tx_ring[0].status = TXOWN;
  535. /* Point to transmit descriptor */
  536. outl(virt_to_bus(mtdx.tx_ring), mtdx.ioaddr + TXLBA);
  537. /* Enable Tx */
  538. outl( mtdx.crvalue | TxEnable, mtdx.ioaddr + TCRRCR);
  539. /* Wake the potentially-idle transmit channel. */
  540. outl(0, mtdx.ioaddr + TXPDR);
  541. to = currticks() + TX_TIME_OUT;
  542. while(( mtdx.tx_ring[0].status & TXOWN) && (currticks() < to));
  543. /* Disable Tx */
  544. outl( mtdx.crvalue & (~TxEnable), mtdx.ioaddr + TCRRCR);
  545. tx_status = mtdx.tx_ring[0].status;
  546. if (currticks() >= to){
  547. DBGPRNT(("TX Time Out"));
  548. } else if( tx_status & (CSL | LC | EC | UDF | HF)){
  549. printf("Transmit error: %s %s %s %s %s.\n",
  550. tx_status,
  551. tx_status & EC ? "abort" : "",
  552. tx_status & CSL ? "carrier" : "",
  553. tx_status & LC ? "late" : "",
  554. tx_status & UDF ? "fifo" : "",
  555. tx_status & HF ? "heartbeat" : "" );
  556. }
  557. /*hex_dump( txb, size );*/
  558. /*pause();*/
  559. DBGPRNT(("TRANSMIT\n"));
  560. }
  561. /**************************************************************************
  562. DISABLE - Turn off ethernet interface
  563. ***************************************************************************/
  564. static void mtd_disable ( struct nic *nic ) {
  565. /* put the card in its initial state */
  566. /* Disable Tx Rx*/
  567. outl( mtdx.crvalue & (~TxEnable) & (~RxEnable), mtdx.ioaddr + TCRRCR);
  568. /* Reset the chip to erase previous misconfiguration. */
  569. mtd_reset((struct nic *) dev);
  570. DBGPRNT(("DISABLE\n"));
  571. }
  572. /**************************************************************************
  573. PROBE - Look for an adapter, this routine's visible to the outside
  574. ***************************************************************************/
  575. static int mtd_probe ( struct dev *dev ) {
  576. struct nic *nic = nic_device ( dev );
  577. struct pci_device *pci = pci_device ( dev );
  578. int i;
  579. if (pci->ioaddr == 0)
  580. {
  581. return 0;
  582. }
  583. printf(" - ");
  584. /* Mask the bit that says "this is an io addr" */
  585. mtdx.ioaddr = pci->ioaddr & ~3;
  586. adjust_pci_device(pci);
  587. mtdx.nic_name = pci->name;
  588. mtdx.dev_id = pci->dev_id;
  589. /* read ethernet id */
  590. for (i = 0; i < 6; ++i)
  591. {
  592. nic->node_addr[i] = inb(mtdx.ioaddr + PAR0 + i);
  593. }
  594. if (memcmp(nic->node_addr, "\0\0\0\0\0", 6) == 0)
  595. {
  596. return 0;
  597. }
  598. DBGPRNT(("%s : ioaddr %#hX, addr %!\n",mtdx.nic_name, mtdx.ioaddr, nic->node_addr));
  599. /* Reset the chip to erase previous misconfiguration. */
  600. outl(0x00000001, mtdx.ioaddr + BCR);
  601. /* find the connected MII xcvrs */
  602. if( mtdx.dev_id != 0x803 )
  603. {
  604. int phy, phy_idx = 0;
  605. for (phy = 1; phy < 32 && phy_idx < 1; phy++) {
  606. int mii_status = mdio_read(nic, phy, 1);
  607. if (mii_status != 0xffff && mii_status != 0x0000) {
  608. mtdx.phys[phy_idx] = phy;
  609. DBGPRNT(("%s: MII PHY found at address %d, status "
  610. "0x%4.4x.\n", mtdx.nic_name, phy, mii_status));
  611. /* get phy type */
  612. {
  613. unsigned int data;
  614. data = mdio_read(nic, mtdx.phys[phy_idx], 2);
  615. if (data == SeeqPHYID0)
  616. mtdx.PHYType = SeeqPHY;
  617. else if (data == AhdocPHYID0)
  618. mtdx.PHYType = AhdocPHY;
  619. else if (data == MarvellPHYID0)
  620. mtdx.PHYType = MarvellPHY;
  621. else if (data == MysonPHYID0)
  622. mtdx.PHYType = Myson981;
  623. else if (data == LevelOnePHYID0)
  624. mtdx.PHYType = LevelOnePHY;
  625. else
  626. mtdx.PHYType = OtherPHY;
  627. }
  628. phy_idx++;
  629. }
  630. }
  631. mtdx.mii_cnt = phy_idx;
  632. if (phy_idx == 0) {
  633. printf("%s: MII PHY not found -- this device may "
  634. "not operate correctly.\n", mtdx.nic_name);
  635. }
  636. } else {
  637. mtdx.phys[0] = 32;
  638. /* get phy type */
  639. if (inl(mtdx.ioaddr + PHYIDENTIFIER) == MysonPHYID ) {
  640. mtdx.PHYType = MysonPHY;
  641. DBGPRNT(("MysonPHY\n"));
  642. } else {
  643. mtdx.PHYType = OtherPHY;
  644. DBGPRNT(("OtherPHY\n"));
  645. }
  646. }
  647. getlinkstatus(nic);
  648. if( !mtdx.linkok )
  649. {
  650. printf("No link!!!\n");
  651. return 0;
  652. }
  653. mtd_reset( nic );
  654. /* point to NIC specific routines */
  655. dev->disable = mtd_disable;
  656. nic->poll = mtd_poll;
  657. nic->transmit = mtd_transmit;
  658. nic->irq = dummy_irq;
  659. return 1;
  660. }
  661. static struct pci_id mtd80x_nics[] =
  662. {
  663. PCI_ROM(0x1516, 0x0800, "MTD800", "Myson MTD800"),
  664. PCI_ROM(0x1516, 0x0803, "MTD803", "Surecom EP-320X"),
  665. PCI_ROM(0x1516, 0x0891, "MTD891", "Myson MTD891"),
  666. };
  667. /**************************************************************************/
  668. static void set_rx_mode(struct nic *nic)
  669. {
  670. u32 mc_filter[2]; /* Multicast hash filter */
  671. u32 rx_mode;
  672. /* Too many to match, or accept all multicasts. */
  673. mc_filter[1] = mc_filter[0] = ~0;
  674. rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
  675. outl(mc_filter[0], mtdx.ioaddr + MAR0);
  676. outl(mc_filter[1], mtdx.ioaddr + MAR1);
  677. mtdx.crvalue = ( mtdx.crvalue & ~RxModeMask ) | rx_mode;
  678. outb( mtdx.crvalue, mtdx.ioaddr + TCRRCR);
  679. }
  680. /**************************************************************************/
  681. static unsigned int m80x_read_tick(void)
  682. /* function: Reads the Timer tick count register which decrements by 2 from */
  683. /* 65536 to 0 every 1/36.414 of a second. Each 2 decrements of the */
  684. /* count represents 838 nsec's. */
  685. /* input : none. */
  686. /* output : none. */
  687. {
  688. unsigned char tmp;
  689. int value;
  690. outb((char) 0x06, 0x43); // Command 8254 to latch T0's count
  691. // now read the count.
  692. tmp = (unsigned char) inb(0x40);
  693. value = ((int) tmp) << 8;
  694. tmp = (unsigned char) inb(0x40);
  695. value |= (((int) tmp) & 0xff);
  696. return (value);
  697. }
  698. static void m80x_delay(unsigned int interval)
  699. /* function: to wait for a specified time. */
  700. /* input : interval ... the specified time. */
  701. /* output : none. */
  702. {
  703. unsigned int interval1, interval2, i = 0;
  704. interval1 = m80x_read_tick(); // get initial value
  705. do
  706. {
  707. interval2 = m80x_read_tick();
  708. if (interval1 < interval2)
  709. interval1 += 65536;
  710. ++i;
  711. } while (((interval1 - interval2) < (u16) interval) && (i < 65535));
  712. }
  713. static u32 m80x_send_cmd_to_phy(long miiport, int opcode, int phyad, int regad)
  714. {
  715. u32 miir;
  716. int i;
  717. unsigned int mask, data;
  718. /* enable MII output */
  719. miir = (u32) inl(miiport);
  720. miir &= 0xfffffff0;
  721. miir |= MASK_MIIR_MII_WRITE + MASK_MIIR_MII_MDO;
  722. /* send 32 1's preamble */
  723. for (i = 0; i < 32; i++) {
  724. /* low MDC; MDO is already high (miir) */
  725. miir &= ~MASK_MIIR_MII_MDC;
  726. outl(miir, miiport);
  727. /* high MDC */
  728. miir |= MASK_MIIR_MII_MDC;
  729. outl(miir, miiport);
  730. }
  731. /* calculate ST+OP+PHYAD+REGAD+TA */
  732. data = opcode | (phyad << 7) | (regad << 2);
  733. /* sent out */
  734. mask = 0x8000;
  735. while (mask) {
  736. /* low MDC, prepare MDO */
  737. miir &= ~(MASK_MIIR_MII_MDC + MASK_MIIR_MII_MDO);
  738. if (mask & data)
  739. miir |= MASK_MIIR_MII_MDO;
  740. outl(miir, miiport);
  741. /* high MDC */
  742. miir |= MASK_MIIR_MII_MDC;
  743. outl(miir, miiport);
  744. m80x_delay(30);
  745. /* next */
  746. mask >>= 1;
  747. if (mask == 0x2 && opcode == OP_READ)
  748. miir &= ~MASK_MIIR_MII_WRITE;
  749. }
  750. return miir;
  751. }
  752. static int mdio_read(struct nic *nic, int phyad, int regad)
  753. {
  754. long miiport = mtdx.ioaddr + MANAGEMENT;
  755. u32 miir;
  756. unsigned int mask, data;
  757. miir = m80x_send_cmd_to_phy(miiport, OP_READ, phyad, regad);
  758. /* read data */
  759. mask = 0x8000;
  760. data = 0;
  761. while (mask)
  762. {
  763. /* low MDC */
  764. miir &= ~MASK_MIIR_MII_MDC;
  765. outl(miir, miiport);
  766. /* read MDI */
  767. miir = inl(miiport);
  768. if (miir & MASK_MIIR_MII_MDI)
  769. data |= mask;
  770. /* high MDC, and wait */
  771. miir |= MASK_MIIR_MII_MDC;
  772. outl(miir, miiport);
  773. m80x_delay((int) 30);
  774. /* next */
  775. mask >>= 1;
  776. }
  777. /* low MDC */
  778. miir &= ~MASK_MIIR_MII_MDC;
  779. outl(miir, miiport);
  780. return data & 0xffff;
  781. }
  782. static void mdio_write(struct nic *nic, int phyad, int regad, int data)
  783. {
  784. long miiport = mtdx.ioaddr + MANAGEMENT;
  785. u32 miir;
  786. unsigned int mask;
  787. miir = m80x_send_cmd_to_phy(miiport, OP_WRITE, phyad, regad);
  788. /* write data */
  789. mask = 0x8000;
  790. while (mask)
  791. {
  792. /* low MDC, prepare MDO */
  793. miir &= ~(MASK_MIIR_MII_MDC + MASK_MIIR_MII_MDO);
  794. if (mask & data)
  795. miir |= MASK_MIIR_MII_MDO;
  796. outl(miir, miiport);
  797. /* high MDC */
  798. miir |= MASK_MIIR_MII_MDC;
  799. outl(miir, miiport);
  800. /* next */
  801. mask >>= 1;
  802. }
  803. /* low MDC */
  804. miir &= ~MASK_MIIR_MII_MDC;
  805. outl(miir, miiport);
  806. return;
  807. }
  808. static void getlinkstatus(struct nic *nic)
  809. /* function: Routine will read MII Status Register to get link status. */
  810. /* input : dev... pointer to the adapter block. */
  811. /* output : none. */
  812. {
  813. unsigned int i, DelayTime = 0x1000;
  814. mtdx.linkok = 0;
  815. if (mtdx.PHYType == MysonPHY)
  816. {
  817. for (i = 0; i < DelayTime; ++i) {
  818. if (inl(mtdx.ioaddr + BMCRSR) & LinkIsUp2) {
  819. mtdx.linkok = 1;
  820. return;
  821. }
  822. // delay
  823. m80x_delay(100);
  824. }
  825. } else
  826. {
  827. for (i = 0; i < DelayTime; ++i) {
  828. if (mdio_read(nic, mtdx.phys[0], MII_BMSR) & BMSR_LSTATUS) {
  829. mtdx.linkok = 1;
  830. return;
  831. }
  832. // delay
  833. m80x_delay(100);
  834. }
  835. }
  836. }
  837. static void getlinktype(struct nic *dev)
  838. {
  839. if (mtdx.PHYType == MysonPHY)
  840. { /* 3-in-1 case */
  841. if (inl(mtdx.ioaddr + TCRRCR) & FD)
  842. mtdx.duplexmode = 2; /* full duplex */
  843. else
  844. mtdx.duplexmode = 1; /* half duplex */
  845. if (inl(mtdx.ioaddr + TCRRCR) & PS10)
  846. mtdx.line_speed = 1; /* 10M */
  847. else
  848. mtdx.line_speed = 2; /* 100M */
  849. } else
  850. {
  851. if (mtdx.PHYType == SeeqPHY) { /* this PHY is SEEQ 80225 */
  852. unsigned int data;
  853. data = mdio_read(dev, mtdx.phys[0], MIIRegister18);
  854. if (data & SPD_DET_100)
  855. mtdx.line_speed = 2; /* 100M */
  856. else
  857. mtdx.line_speed = 1; /* 10M */
  858. if (data & DPLX_DET_FULL)
  859. mtdx.duplexmode = 2; /* full duplex mode */
  860. else
  861. mtdx.duplexmode = 1; /* half duplex mode */
  862. } else if (mtdx.PHYType == AhdocPHY) {
  863. unsigned int data;
  864. data = mdio_read(dev, mtdx.phys[0], DiagnosticReg);
  865. if (data & Speed_100)
  866. mtdx.line_speed = 2; /* 100M */
  867. else
  868. mtdx.line_speed = 1; /* 10M */
  869. if (data & DPLX_FULL)
  870. mtdx.duplexmode = 2; /* full duplex mode */
  871. else
  872. mtdx.duplexmode = 1; /* half duplex mode */
  873. }
  874. /* 89/6/13 add, (begin) */
  875. else if (mtdx.PHYType == MarvellPHY) {
  876. unsigned int data;
  877. data = mdio_read(dev, mtdx.phys[0], SpecificReg);
  878. if (data & Full_Duplex)
  879. mtdx.duplexmode = 2; /* full duplex mode */
  880. else
  881. mtdx.duplexmode = 1; /* half duplex mode */
  882. data &= SpeedMask;
  883. if (data == Speed_1000M)
  884. mtdx.line_speed = 3; /* 1000M */
  885. else if (data == Speed_100M)
  886. mtdx.line_speed = 2; /* 100M */
  887. else
  888. mtdx.line_speed = 1; /* 10M */
  889. }
  890. /* 89/6/13 add, (end) */
  891. /* 89/7/27 add, (begin) */
  892. else if (mtdx.PHYType == Myson981) {
  893. unsigned int data;
  894. data = mdio_read(dev, mtdx.phys[0], StatusRegister);
  895. if (data & SPEED100)
  896. mtdx.line_speed = 2;
  897. else
  898. mtdx.line_speed = 1;
  899. if (data & FULLMODE)
  900. mtdx.duplexmode = 2;
  901. else
  902. mtdx.duplexmode = 1;
  903. }
  904. /* 89/7/27 add, (end) */
  905. /* 89/12/29 add */
  906. else if (mtdx.PHYType == LevelOnePHY) {
  907. unsigned int data;
  908. data = mdio_read(dev, mtdx.phys[0], SpecificReg);
  909. if (data & LXT1000_Full)
  910. mtdx.duplexmode = 2; /* full duplex mode */
  911. else
  912. mtdx.duplexmode = 1; /* half duplex mode */
  913. data &= SpeedMask;
  914. if (data == LXT1000_1000M)
  915. mtdx.line_speed = 3; /* 1000M */
  916. else if (data == LXT1000_100M)
  917. mtdx.line_speed = 2; /* 100M */
  918. else
  919. mtdx.line_speed = 1; /* 10M */
  920. }
  921. // chage crvalue
  922. // mtdx.crvalue&=(~PS10)&(~FD);
  923. mtdx.crvalue &= (~PS10) & (~FD) & (~PS1000);
  924. if (mtdx.line_speed == 1)
  925. mtdx.crvalue |= PS10;
  926. else if (mtdx.line_speed == 3)
  927. mtdx.crvalue |= PS1000;
  928. if (mtdx.duplexmode == 2)
  929. mtdx.crvalue |= FD;
  930. }
  931. }
  932. static struct pci_driver mtd80x_driver =
  933. PCI_DRIVER ( "MTD80X", mtd80x_nics, PCI_NO_CLASS );
  934. BOOT_DRIVER ( "MTD80X", mtd_probe );