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

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