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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  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. return 1;
  659. }
  660. static struct pci_id mtd80x_nics[] =
  661. {
  662. PCI_ROM(0x1516, 0x0800, "MTD800", "Myson MTD800"),
  663. PCI_ROM(0x1516, 0x0803, "MTD803", "Surecom EP-320X"),
  664. PCI_ROM(0x1516, 0x0891, "MTD891", "Myson MTD891"),
  665. };
  666. /**************************************************************************/
  667. static void set_rx_mode(struct nic *nic)
  668. {
  669. u32 mc_filter[2]; /* Multicast hash filter */
  670. u32 rx_mode;
  671. /* Too many to match, or accept all multicasts. */
  672. mc_filter[1] = mc_filter[0] = ~0;
  673. rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
  674. outl(mc_filter[0], mtdx.ioaddr + MAR0);
  675. outl(mc_filter[1], mtdx.ioaddr + MAR1);
  676. mtdx.crvalue = ( mtdx.crvalue & ~RxModeMask ) | rx_mode;
  677. outb( mtdx.crvalue, mtdx.ioaddr + TCRRCR);
  678. }
  679. /**************************************************************************/
  680. static unsigned int m80x_read_tick(void)
  681. /* function: Reads the Timer tick count register which decrements by 2 from */
  682. /* 65536 to 0 every 1/36.414 of a second. Each 2 decrements of the */
  683. /* count represents 838 nsec's. */
  684. /* input : none. */
  685. /* output : none. */
  686. {
  687. unsigned char tmp;
  688. int value;
  689. outb((char) 0x06, 0x43); // Command 8254 to latch T0's count
  690. // now read the count.
  691. tmp = (unsigned char) inb(0x40);
  692. value = ((int) tmp) << 8;
  693. tmp = (unsigned char) inb(0x40);
  694. value |= (((int) tmp) & 0xff);
  695. return (value);
  696. }
  697. static void m80x_delay(unsigned int interval)
  698. /* function: to wait for a specified time. */
  699. /* input : interval ... the specified time. */
  700. /* output : none. */
  701. {
  702. unsigned int interval1, interval2, i = 0;
  703. interval1 = m80x_read_tick(); // get initial value
  704. do
  705. {
  706. interval2 = m80x_read_tick();
  707. if (interval1 < interval2)
  708. interval1 += 65536;
  709. ++i;
  710. } while (((interval1 - interval2) < (u16) interval) && (i < 65535));
  711. }
  712. static u32 m80x_send_cmd_to_phy(long miiport, int opcode, int phyad, int regad)
  713. {
  714. u32 miir;
  715. int i;
  716. unsigned int mask, data;
  717. /* enable MII output */
  718. miir = (u32) inl(miiport);
  719. miir &= 0xfffffff0;
  720. miir |= MASK_MIIR_MII_WRITE + MASK_MIIR_MII_MDO;
  721. /* send 32 1's preamble */
  722. for (i = 0; i < 32; i++) {
  723. /* low MDC; MDO is already high (miir) */
  724. miir &= ~MASK_MIIR_MII_MDC;
  725. outl(miir, miiport);
  726. /* high MDC */
  727. miir |= MASK_MIIR_MII_MDC;
  728. outl(miir, miiport);
  729. }
  730. /* calculate ST+OP+PHYAD+REGAD+TA */
  731. data = opcode | (phyad << 7) | (regad << 2);
  732. /* sent out */
  733. mask = 0x8000;
  734. while (mask) {
  735. /* low MDC, prepare MDO */
  736. miir &= ~(MASK_MIIR_MII_MDC + MASK_MIIR_MII_MDO);
  737. if (mask & data)
  738. miir |= MASK_MIIR_MII_MDO;
  739. outl(miir, miiport);
  740. /* high MDC */
  741. miir |= MASK_MIIR_MII_MDC;
  742. outl(miir, miiport);
  743. m80x_delay(30);
  744. /* next */
  745. mask >>= 1;
  746. if (mask == 0x2 && opcode == OP_READ)
  747. miir &= ~MASK_MIIR_MII_WRITE;
  748. }
  749. return miir;
  750. }
  751. static int mdio_read(struct nic *nic, int phyad, int regad)
  752. {
  753. long miiport = mtdx.ioaddr + MANAGEMENT;
  754. u32 miir;
  755. unsigned int mask, data;
  756. miir = m80x_send_cmd_to_phy(miiport, OP_READ, phyad, regad);
  757. /* read data */
  758. mask = 0x8000;
  759. data = 0;
  760. while (mask)
  761. {
  762. /* low MDC */
  763. miir &= ~MASK_MIIR_MII_MDC;
  764. outl(miir, miiport);
  765. /* read MDI */
  766. miir = inl(miiport);
  767. if (miir & MASK_MIIR_MII_MDI)
  768. data |= mask;
  769. /* high MDC, and wait */
  770. miir |= MASK_MIIR_MII_MDC;
  771. outl(miir, miiport);
  772. m80x_delay((int) 30);
  773. /* next */
  774. mask >>= 1;
  775. }
  776. /* low MDC */
  777. miir &= ~MASK_MIIR_MII_MDC;
  778. outl(miir, miiport);
  779. return data & 0xffff;
  780. }
  781. static void mdio_write(struct nic *nic, int phyad, int regad, int data)
  782. {
  783. long miiport = mtdx.ioaddr + MANAGEMENT;
  784. u32 miir;
  785. unsigned int mask;
  786. miir = m80x_send_cmd_to_phy(miiport, OP_WRITE, phyad, regad);
  787. /* write data */
  788. mask = 0x8000;
  789. while (mask)
  790. {
  791. /* low MDC, prepare MDO */
  792. miir &= ~(MASK_MIIR_MII_MDC + MASK_MIIR_MII_MDO);
  793. if (mask & data)
  794. miir |= MASK_MIIR_MII_MDO;
  795. outl(miir, miiport);
  796. /* high MDC */
  797. miir |= MASK_MIIR_MII_MDC;
  798. outl(miir, miiport);
  799. /* next */
  800. mask >>= 1;
  801. }
  802. /* low MDC */
  803. miir &= ~MASK_MIIR_MII_MDC;
  804. outl(miir, miiport);
  805. return;
  806. }
  807. static void getlinkstatus(struct nic *nic)
  808. /* function: Routine will read MII Status Register to get link status. */
  809. /* input : dev... pointer to the adapter block. */
  810. /* output : none. */
  811. {
  812. unsigned int i, DelayTime = 0x1000;
  813. mtdx.linkok = 0;
  814. if (mtdx.PHYType == MysonPHY)
  815. {
  816. for (i = 0; i < DelayTime; ++i) {
  817. if (inl(mtdx.ioaddr + BMCRSR) & LinkIsUp2) {
  818. mtdx.linkok = 1;
  819. return;
  820. }
  821. // delay
  822. m80x_delay(100);
  823. }
  824. } else
  825. {
  826. for (i = 0; i < DelayTime; ++i) {
  827. if (mdio_read(nic, mtdx.phys[0], MII_BMSR) & BMSR_LSTATUS) {
  828. mtdx.linkok = 1;
  829. return;
  830. }
  831. // delay
  832. m80x_delay(100);
  833. }
  834. }
  835. }
  836. static void getlinktype(struct nic *dev)
  837. {
  838. if (mtdx.PHYType == MysonPHY)
  839. { /* 3-in-1 case */
  840. if (inl(mtdx.ioaddr + TCRRCR) & FD)
  841. mtdx.duplexmode = 2; /* full duplex */
  842. else
  843. mtdx.duplexmode = 1; /* half duplex */
  844. if (inl(mtdx.ioaddr + TCRRCR) & PS10)
  845. mtdx.line_speed = 1; /* 10M */
  846. else
  847. mtdx.line_speed = 2; /* 100M */
  848. } else
  849. {
  850. if (mtdx.PHYType == SeeqPHY) { /* this PHY is SEEQ 80225 */
  851. unsigned int data;
  852. data = mdio_read(dev, mtdx.phys[0], MIIRegister18);
  853. if (data & SPD_DET_100)
  854. mtdx.line_speed = 2; /* 100M */
  855. else
  856. mtdx.line_speed = 1; /* 10M */
  857. if (data & DPLX_DET_FULL)
  858. mtdx.duplexmode = 2; /* full duplex mode */
  859. else
  860. mtdx.duplexmode = 1; /* half duplex mode */
  861. } else if (mtdx.PHYType == AhdocPHY) {
  862. unsigned int data;
  863. data = mdio_read(dev, mtdx.phys[0], DiagnosticReg);
  864. if (data & Speed_100)
  865. mtdx.line_speed = 2; /* 100M */
  866. else
  867. mtdx.line_speed = 1; /* 10M */
  868. if (data & DPLX_FULL)
  869. mtdx.duplexmode = 2; /* full duplex mode */
  870. else
  871. mtdx.duplexmode = 1; /* half duplex mode */
  872. }
  873. /* 89/6/13 add, (begin) */
  874. else if (mtdx.PHYType == MarvellPHY) {
  875. unsigned int data;
  876. data = mdio_read(dev, mtdx.phys[0], SpecificReg);
  877. if (data & Full_Duplex)
  878. mtdx.duplexmode = 2; /* full duplex mode */
  879. else
  880. mtdx.duplexmode = 1; /* half duplex mode */
  881. data &= SpeedMask;
  882. if (data == Speed_1000M)
  883. mtdx.line_speed = 3; /* 1000M */
  884. else if (data == Speed_100M)
  885. mtdx.line_speed = 2; /* 100M */
  886. else
  887. mtdx.line_speed = 1; /* 10M */
  888. }
  889. /* 89/6/13 add, (end) */
  890. /* 89/7/27 add, (begin) */
  891. else if (mtdx.PHYType == Myson981) {
  892. unsigned int data;
  893. data = mdio_read(dev, mtdx.phys[0], StatusRegister);
  894. if (data & SPEED100)
  895. mtdx.line_speed = 2;
  896. else
  897. mtdx.line_speed = 1;
  898. if (data & FULLMODE)
  899. mtdx.duplexmode = 2;
  900. else
  901. mtdx.duplexmode = 1;
  902. }
  903. /* 89/7/27 add, (end) */
  904. /* 89/12/29 add */
  905. else if (mtdx.PHYType == LevelOnePHY) {
  906. unsigned int data;
  907. data = mdio_read(dev, mtdx.phys[0], SpecificReg);
  908. if (data & LXT1000_Full)
  909. mtdx.duplexmode = 2; /* full duplex mode */
  910. else
  911. mtdx.duplexmode = 1; /* half duplex mode */
  912. data &= SpeedMask;
  913. if (data == LXT1000_1000M)
  914. mtdx.line_speed = 3; /* 1000M */
  915. else if (data == LXT1000_100M)
  916. mtdx.line_speed = 2; /* 100M */
  917. else
  918. mtdx.line_speed = 1; /* 10M */
  919. }
  920. // chage crvalue
  921. // mtdx.crvalue&=(~PS10)&(~FD);
  922. mtdx.crvalue &= (~PS10) & (~FD) & (~PS1000);
  923. if (mtdx.line_speed == 1)
  924. mtdx.crvalue |= PS10;
  925. else if (mtdx.line_speed == 3)
  926. mtdx.crvalue |= PS1000;
  927. if (mtdx.duplexmode == 2)
  928. mtdx.crvalue |= FD;
  929. }
  930. }
  931. static struct pci_driver mtd80x_driver =
  932. PCI_DRIVER ( "MTD80X", mtd80x_nics, PCI_NO_CLASS );
  933. BOOT_DRIVER ( "MTD80X", mtd_probe );