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.

ns83820.c 27KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. /**************************************************************************
  2. * ns83820.c: Etherboot device driver for the National Semiconductor 83820
  3. * Written 2004 by Timothy Legge <tlegge@rogers.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. *
  19. * Portions of this code based on:
  20. * ns83820.c by Benjamin LaHaise with contributions
  21. * for Linux kernel 2.4.x.
  22. *
  23. * Linux Driver Version 0.20, 20020610
  24. *
  25. * This development of this Etherboot driver was funded by:
  26. *
  27. * NXTV: http://www.nxtv.com/
  28. *
  29. * REVISION HISTORY:
  30. * ================
  31. *
  32. * v1.0 02-16-2004 timlegge Initial port of Linux driver
  33. * v1.1 02-19-2004 timlegge More rohbust transmit and poll
  34. *
  35. * Indent Options: indent -kr -i8
  36. ***************************************************************************/
  37. /* to get some global routines like printf */
  38. #include "etherboot.h"
  39. /* to get the interface to the body of the program */
  40. #include "nic.h"
  41. /* to get the PCI support functions, if this is a PCI NIC */
  42. #include <gpxe/pci.h>
  43. #if ARCH == ia64 /* Support 64-bit addressing */
  44. #define USE_64BIT_ADDR
  45. #endif
  46. //#define DDEBUG
  47. #ifdef DDEBUG
  48. #define dprintf(x) printf x
  49. #else
  50. #define dprintf(x)
  51. #endif
  52. #define HZ 100
  53. /* Condensed operations for readability. */
  54. #define virt_to_le32desc(addr) cpu_to_le32(virt_to_bus(addr))
  55. #define le32desc_to_virt(addr) bus_to_virt(le32_to_cpu(addr))
  56. /* NIC specific static variables go here */
  57. /* Global parameters. See MODULE_PARM near the bottom. */
  58. // static int ihr = 2;
  59. static int reset_phy = 0;
  60. static int lnksts = 0; /* CFG_LNKSTS bit polarity */
  61. #if defined(CONFIG_HIGHMEM64G) || defined(__ia64__)
  62. #define USE_64BIT_ADDR "+"
  63. #endif
  64. #if defined(USE_64BIT_ADDR)
  65. #define TRY_DAC 1
  66. #else
  67. #define TRY_DAC 0
  68. #endif
  69. /* tunables */
  70. #define RX_BUF_SIZE 1500 /* 8192 */
  71. /* Must not exceed ~65000. */
  72. #define NR_RX_DESC 64
  73. #define NR_TX_DESC 1
  74. /* not tunable *//* Extra 6 bytes for 64 bit alignment (divisable by 8) */
  75. #define REAL_RX_BUF_SIZE (RX_BUF_SIZE + 14 + 6) /* rx/tx mac addr + type */
  76. #define MIN_TX_DESC_FREE 8
  77. /* register defines */
  78. #define CFGCS 0x04
  79. #define CR_TXE 0x00000001
  80. #define CR_TXD 0x00000002
  81. /* Ramit : Here's a tip, don't do a RXD immediately followed by an RXE
  82. * The Receive engine skips one descriptor and moves
  83. * onto the next one!! */
  84. #define CR_RXE 0x00000004
  85. #define CR_RXD 0x00000008
  86. #define CR_TXR 0x00000010
  87. #define CR_RXR 0x00000020
  88. #define CR_SWI 0x00000080
  89. #define CR_RST 0x00000100
  90. #define PTSCR_EEBIST_FAIL 0x00000001
  91. #define PTSCR_EEBIST_EN 0x00000002
  92. #define PTSCR_EELOAD_EN 0x00000004
  93. #define PTSCR_RBIST_FAIL 0x000001b8
  94. #define PTSCR_RBIST_DONE 0x00000200
  95. #define PTSCR_RBIST_EN 0x00000400
  96. #define PTSCR_RBIST_RST 0x00002000
  97. #define MEAR_EEDI 0x00000001
  98. #define MEAR_EEDO 0x00000002
  99. #define MEAR_EECLK 0x00000004
  100. #define MEAR_EESEL 0x00000008
  101. #define MEAR_MDIO 0x00000010
  102. #define MEAR_MDDIR 0x00000020
  103. #define MEAR_MDC 0x00000040
  104. #define ISR_TXDESC3 0x40000000
  105. #define ISR_TXDESC2 0x20000000
  106. #define ISR_TXDESC1 0x10000000
  107. #define ISR_TXDESC0 0x08000000
  108. #define ISR_RXDESC3 0x04000000
  109. #define ISR_RXDESC2 0x02000000
  110. #define ISR_RXDESC1 0x01000000
  111. #define ISR_RXDESC0 0x00800000
  112. #define ISR_TXRCMP 0x00400000
  113. #define ISR_RXRCMP 0x00200000
  114. #define ISR_DPERR 0x00100000
  115. #define ISR_SSERR 0x00080000
  116. #define ISR_RMABT 0x00040000
  117. #define ISR_RTABT 0x00020000
  118. #define ISR_RXSOVR 0x00010000
  119. #define ISR_HIBINT 0x00008000
  120. #define ISR_PHY 0x00004000
  121. #define ISR_PME 0x00002000
  122. #define ISR_SWI 0x00001000
  123. #define ISR_MIB 0x00000800
  124. #define ISR_TXURN 0x00000400
  125. #define ISR_TXIDLE 0x00000200
  126. #define ISR_TXERR 0x00000100
  127. #define ISR_TXDESC 0x00000080
  128. #define ISR_TXOK 0x00000040
  129. #define ISR_RXORN 0x00000020
  130. #define ISR_RXIDLE 0x00000010
  131. #define ISR_RXEARLY 0x00000008
  132. #define ISR_RXERR 0x00000004
  133. #define ISR_RXDESC 0x00000002
  134. #define ISR_RXOK 0x00000001
  135. #define TXCFG_CSI 0x80000000
  136. #define TXCFG_HBI 0x40000000
  137. #define TXCFG_MLB 0x20000000
  138. #define TXCFG_ATP 0x10000000
  139. #define TXCFG_ECRETRY 0x00800000
  140. #define TXCFG_BRST_DIS 0x00080000
  141. #define TXCFG_MXDMA1024 0x00000000
  142. #define TXCFG_MXDMA512 0x00700000
  143. #define TXCFG_MXDMA256 0x00600000
  144. #define TXCFG_MXDMA128 0x00500000
  145. #define TXCFG_MXDMA64 0x00400000
  146. #define TXCFG_MXDMA32 0x00300000
  147. #define TXCFG_MXDMA16 0x00200000
  148. #define TXCFG_MXDMA8 0x00100000
  149. #define CFG_LNKSTS 0x80000000
  150. #define CFG_SPDSTS 0x60000000
  151. #define CFG_SPDSTS1 0x40000000
  152. #define CFG_SPDSTS0 0x20000000
  153. #define CFG_DUPSTS 0x10000000
  154. #define CFG_TBI_EN 0x01000000
  155. #define CFG_MODE_1000 0x00400000
  156. /* Ramit : Dont' ever use AUTO_1000, it never works and is buggy.
  157. * Read the Phy response and then configure the MAC accordingly */
  158. #define CFG_AUTO_1000 0x00200000
  159. #define CFG_PINT_CTL 0x001c0000
  160. #define CFG_PINT_DUPSTS 0x00100000
  161. #define CFG_PINT_LNKSTS 0x00080000
  162. #define CFG_PINT_SPDSTS 0x00040000
  163. #define CFG_TMRTEST 0x00020000
  164. #define CFG_MRM_DIS 0x00010000
  165. #define CFG_MWI_DIS 0x00008000
  166. #define CFG_T64ADDR 0x00004000
  167. #define CFG_PCI64_DET 0x00002000
  168. #define CFG_DATA64_EN 0x00001000
  169. #define CFG_M64ADDR 0x00000800
  170. #define CFG_PHY_RST 0x00000400
  171. #define CFG_PHY_DIS 0x00000200
  172. #define CFG_EXTSTS_EN 0x00000100
  173. #define CFG_REQALG 0x00000080
  174. #define CFG_SB 0x00000040
  175. #define CFG_POW 0x00000020
  176. #define CFG_EXD 0x00000010
  177. #define CFG_PESEL 0x00000008
  178. #define CFG_BROM_DIS 0x00000004
  179. #define CFG_EXT_125 0x00000002
  180. #define CFG_BEM 0x00000001
  181. #define EXTSTS_UDPPKT 0x00200000
  182. #define EXTSTS_TCPPKT 0x00080000
  183. #define EXTSTS_IPPKT 0x00020000
  184. #define SPDSTS_POLARITY (CFG_SPDSTS1 | CFG_SPDSTS0 | CFG_DUPSTS | (lnksts ? CFG_LNKSTS : 0))
  185. #define MIBC_MIBS 0x00000008
  186. #define MIBC_ACLR 0x00000004
  187. #define MIBC_FRZ 0x00000002
  188. #define MIBC_WRN 0x00000001
  189. #define PCR_PSEN (1 << 31)
  190. #define PCR_PS_MCAST (1 << 30)
  191. #define PCR_PS_DA (1 << 29)
  192. #define PCR_STHI_8 (3 << 23)
  193. #define PCR_STLO_4 (1 << 23)
  194. #define PCR_FFHI_8K (3 << 21)
  195. #define PCR_FFLO_4K (1 << 21)
  196. #define PCR_PAUSE_CNT 0xFFFE
  197. #define RXCFG_AEP 0x80000000
  198. #define RXCFG_ARP 0x40000000
  199. #define RXCFG_STRIPCRC 0x20000000
  200. #define RXCFG_RX_FD 0x10000000
  201. #define RXCFG_ALP 0x08000000
  202. #define RXCFG_AIRL 0x04000000
  203. #define RXCFG_MXDMA512 0x00700000
  204. #define RXCFG_DRTH 0x0000003e
  205. #define RXCFG_DRTH0 0x00000002
  206. #define RFCR_RFEN 0x80000000
  207. #define RFCR_AAB 0x40000000
  208. #define RFCR_AAM 0x20000000
  209. #define RFCR_AAU 0x10000000
  210. #define RFCR_APM 0x08000000
  211. #define RFCR_APAT 0x07800000
  212. #define RFCR_APAT3 0x04000000
  213. #define RFCR_APAT2 0x02000000
  214. #define RFCR_APAT1 0x01000000
  215. #define RFCR_APAT0 0x00800000
  216. #define RFCR_AARP 0x00400000
  217. #define RFCR_MHEN 0x00200000
  218. #define RFCR_UHEN 0x00100000
  219. #define RFCR_ULM 0x00080000
  220. #define VRCR_RUDPE 0x00000080
  221. #define VRCR_RTCPE 0x00000040
  222. #define VRCR_RIPE 0x00000020
  223. #define VRCR_IPEN 0x00000010
  224. #define VRCR_DUTF 0x00000008
  225. #define VRCR_DVTF 0x00000004
  226. #define VRCR_VTREN 0x00000002
  227. #define VRCR_VTDEN 0x00000001
  228. #define VTCR_PPCHK 0x00000008
  229. #define VTCR_GCHK 0x00000004
  230. #define VTCR_VPPTI 0x00000002
  231. #define VTCR_VGTI 0x00000001
  232. #define CR 0x00
  233. #define CFG 0x04
  234. #define MEAR 0x08
  235. #define PTSCR 0x0c
  236. #define ISR 0x10
  237. #define IMR 0x14
  238. #define IER 0x18
  239. #define IHR 0x1c
  240. #define TXDP 0x20
  241. #define TXDP_HI 0x24
  242. #define TXCFG 0x28
  243. #define GPIOR 0x2c
  244. #define RXDP 0x30
  245. #define RXDP_HI 0x34
  246. #define RXCFG 0x38
  247. #define PQCR 0x3c
  248. #define WCSR 0x40
  249. #define PCR 0x44
  250. #define RFCR 0x48
  251. #define RFDR 0x4c
  252. #define SRR 0x58
  253. #define VRCR 0xbc
  254. #define VTCR 0xc0
  255. #define VDR 0xc4
  256. #define CCSR 0xcc
  257. #define TBICR 0xe0
  258. #define TBISR 0xe4
  259. #define TANAR 0xe8
  260. #define TANLPAR 0xec
  261. #define TANER 0xf0
  262. #define TESR 0xf4
  263. #define TBICR_MR_AN_ENABLE 0x00001000
  264. #define TBICR_MR_RESTART_AN 0x00000200
  265. #define TBISR_MR_LINK_STATUS 0x00000020
  266. #define TBISR_MR_AN_COMPLETE 0x00000004
  267. #define TANAR_PS2 0x00000100
  268. #define TANAR_PS1 0x00000080
  269. #define TANAR_HALF_DUP 0x00000040
  270. #define TANAR_FULL_DUP 0x00000020
  271. #define GPIOR_GP5_OE 0x00000200
  272. #define GPIOR_GP4_OE 0x00000100
  273. #define GPIOR_GP3_OE 0x00000080
  274. #define GPIOR_GP2_OE 0x00000040
  275. #define GPIOR_GP1_OE 0x00000020
  276. #define GPIOR_GP3_OUT 0x00000004
  277. #define GPIOR_GP1_OUT 0x00000001
  278. #define LINK_AUTONEGOTIATE 0x01
  279. #define LINK_DOWN 0x02
  280. #define LINK_UP 0x04
  281. #define __kick_rx() writel(CR_RXE, ns->base + CR)
  282. #define kick_rx() do { \
  283. dprintf(("kick_rx: maybe kicking\n")); \
  284. writel(virt_to_le32desc(&rx_ring[ns->cur_rx]), ns->base + RXDP); \
  285. if (ns->next_rx == ns->next_empty) \
  286. printf("uh-oh: next_rx == next_empty???\n"); \
  287. __kick_rx(); \
  288. } while(0)
  289. #ifdef USE_64BIT_ADDR
  290. #define HW_ADDR_LEN 8
  291. #else
  292. #define HW_ADDR_LEN 4
  293. #endif
  294. #define CMDSTS_OWN 0x80000000
  295. #define CMDSTS_MORE 0x40000000
  296. #define CMDSTS_INTR 0x20000000
  297. #define CMDSTS_ERR 0x10000000
  298. #define CMDSTS_OK 0x08000000
  299. #define CMDSTS_LEN_MASK 0x0000ffff
  300. #define CMDSTS_DEST_MASK 0x01800000
  301. #define CMDSTS_DEST_SELF 0x00800000
  302. #define CMDSTS_DEST_MULTI 0x01000000
  303. #define DESC_SIZE 8 /* Should be cache line sized */
  304. #ifdef USE_64BIT_ADDR
  305. struct ring_desc {
  306. uint64_t link;
  307. uint64_t bufptr;
  308. u32 cmdsts;
  309. u32 extsts; /* Extended status field */
  310. };
  311. #else
  312. struct ring_desc {
  313. u32 link;
  314. u32 bufptr;
  315. u32 cmdsts;
  316. u32 extsts; /* Extended status field */
  317. };
  318. #endif
  319. /* Private Storage for the NIC */
  320. static struct ns83820_private {
  321. u8 *base;
  322. int up;
  323. long idle;
  324. u32 *next_rx_desc;
  325. u16 next_rx, next_empty;
  326. u32 cur_rx;
  327. u32 *descs;
  328. unsigned ihr;
  329. u32 CFG_cache;
  330. u32 MEAR_cache;
  331. u32 IMR_cache;
  332. int linkstate;
  333. u16 tx_done_idx;
  334. u16 tx_idx;
  335. u16 tx_intr_idx;
  336. u32 phy_descs;
  337. u32 *tx_descs;
  338. } nsx;
  339. static struct ns83820_private *ns;
  340. /* Define the TX and RX Descriptor and Buffers */
  341. struct {
  342. struct ring_desc tx_ring[NR_TX_DESC] __attribute__ ((aligned(8)));
  343. unsigned char txb[NR_TX_DESC * REAL_RX_BUF_SIZE];
  344. struct ring_desc rx_ring[NR_RX_DESC] __attribute__ ((aligned(8)));
  345. unsigned char rxb[NR_RX_DESC * REAL_RX_BUF_SIZE]
  346. __attribute__ ((aligned(8)));
  347. } ns83820_bufs __shared;
  348. #define tx_ring ns83820_bufs.tx_ring
  349. #define rx_ring ns83820_bufs.rx_ring
  350. #define txb ns83820_bufs.txb
  351. #define rxb ns83820_bufs.rxb
  352. static void phy_intr(struct nic *nic __unused)
  353. {
  354. static char *speeds[] =
  355. { "10", "100", "1000", "1000(?)", "1000F" };
  356. u32 cfg, new_cfg;
  357. u32 tbisr, tanar, tanlpar;
  358. int speed, fullduplex, newlinkstate;
  359. cfg = readl(ns->base + CFG) ^ SPDSTS_POLARITY;
  360. if (ns->CFG_cache & CFG_TBI_EN) {
  361. /* we have an optical transceiver */
  362. tbisr = readl(ns->base + TBISR);
  363. tanar = readl(ns->base + TANAR);
  364. tanlpar = readl(ns->base + TANLPAR);
  365. dprintf(("phy_intr: tbisr=%hX, tanar=%hX, tanlpar=%hX\n",
  366. tbisr, tanar, tanlpar));
  367. if ((fullduplex = (tanlpar & TANAR_FULL_DUP)
  368. && (tanar & TANAR_FULL_DUP))) {
  369. /* both of us are full duplex */
  370. writel(readl(ns->base + TXCFG)
  371. | TXCFG_CSI | TXCFG_HBI | TXCFG_ATP,
  372. ns->base + TXCFG);
  373. writel(readl(ns->base + RXCFG) | RXCFG_RX_FD,
  374. ns->base + RXCFG);
  375. /* Light up full duplex LED */
  376. writel(readl(ns->base + GPIOR) | GPIOR_GP1_OUT,
  377. ns->base + GPIOR);
  378. } else if (((tanlpar & TANAR_HALF_DUP)
  379. && (tanar & TANAR_HALF_DUP))
  380. || ((tanlpar & TANAR_FULL_DUP)
  381. && (tanar & TANAR_HALF_DUP))
  382. || ((tanlpar & TANAR_HALF_DUP)
  383. && (tanar & TANAR_FULL_DUP))) {
  384. /* one or both of us are half duplex */
  385. writel((readl(ns->base + TXCFG)
  386. & ~(TXCFG_CSI | TXCFG_HBI)) | TXCFG_ATP,
  387. ns->base + TXCFG);
  388. writel(readl(ns->base + RXCFG) & ~RXCFG_RX_FD,
  389. ns->base + RXCFG);
  390. /* Turn off full duplex LED */
  391. writel(readl(ns->base + GPIOR) & ~GPIOR_GP1_OUT,
  392. ns->base + GPIOR);
  393. }
  394. speed = 4; /* 1000F */
  395. } else {
  396. /* we have a copper transceiver */
  397. new_cfg =
  398. ns->CFG_cache & ~(CFG_SB | CFG_MODE_1000 | CFG_SPDSTS);
  399. if (cfg & CFG_SPDSTS1)
  400. new_cfg |= CFG_MODE_1000;
  401. else
  402. new_cfg &= ~CFG_MODE_1000;
  403. speed = ((cfg / CFG_SPDSTS0) & 3);
  404. fullduplex = (cfg & CFG_DUPSTS);
  405. if (fullduplex)
  406. new_cfg |= CFG_SB;
  407. if ((cfg & CFG_LNKSTS) &&
  408. ((new_cfg ^ ns->CFG_cache) & CFG_MODE_1000)) {
  409. writel(new_cfg, ns->base + CFG);
  410. ns->CFG_cache = new_cfg;
  411. }
  412. ns->CFG_cache &= ~CFG_SPDSTS;
  413. ns->CFG_cache |= cfg & CFG_SPDSTS;
  414. }
  415. newlinkstate = (cfg & CFG_LNKSTS) ? LINK_UP : LINK_DOWN;
  416. if (newlinkstate & LINK_UP && ns->linkstate != newlinkstate) {
  417. printf("link now %s mbps, %s duplex and up.\n",
  418. speeds[speed], fullduplex ? "full" : "half");
  419. } else if (newlinkstate & LINK_DOWN
  420. && ns->linkstate != newlinkstate) {
  421. printf("link now down.\n");
  422. }
  423. ns->linkstate = newlinkstate;
  424. }
  425. static void ns83820_set_multicast(struct nic *nic __unused);
  426. static void ns83820_setup_rx(struct nic *nic)
  427. {
  428. unsigned i;
  429. ns->idle = 1;
  430. ns->next_rx = 0;
  431. ns->next_rx_desc = ns->descs;
  432. ns->next_empty = 0;
  433. ns->cur_rx = 0;
  434. for (i = 0; i < NR_RX_DESC; i++) {
  435. rx_ring[i].link = virt_to_le32desc(&rx_ring[i + 1]);
  436. rx_ring[i].bufptr =
  437. virt_to_le32desc(&rxb[i * REAL_RX_BUF_SIZE]);
  438. rx_ring[i].cmdsts = cpu_to_le32(REAL_RX_BUF_SIZE);
  439. rx_ring[i].extsts = cpu_to_le32(0);
  440. }
  441. // No need to wrap the ring
  442. // rx_ring[i].link = virt_to_le32desc(&rx_ring[0]);
  443. writel(0, ns->base + RXDP_HI);
  444. writel(virt_to_le32desc(&rx_ring[0]), ns->base + RXDP);
  445. dprintf(("starting receiver\n"));
  446. writel(0x0001, ns->base + CCSR);
  447. writel(0, ns->base + RFCR);
  448. writel(0x7fc00000, ns->base + RFCR);
  449. writel(0xffc00000, ns->base + RFCR);
  450. ns->up = 1;
  451. phy_intr(nic);
  452. /* Okay, let it rip */
  453. ns->IMR_cache |= ISR_PHY;
  454. ns->IMR_cache |= ISR_RXRCMP;
  455. //dev->IMR_cache |= ISR_RXERR;
  456. //dev->IMR_cache |= ISR_RXOK;
  457. ns->IMR_cache |= ISR_RXORN;
  458. ns->IMR_cache |= ISR_RXSOVR;
  459. ns->IMR_cache |= ISR_RXDESC;
  460. ns->IMR_cache |= ISR_RXIDLE;
  461. ns->IMR_cache |= ISR_TXDESC;
  462. ns->IMR_cache |= ISR_TXIDLE;
  463. // No reason to enable interupts...
  464. // writel(ns->IMR_cache, ns->base + IMR);
  465. // writel(1, ns->base + IER);
  466. ns83820_set_multicast(nic);
  467. kick_rx();
  468. }
  469. static void ns83820_do_reset(struct nic *nic __unused, u32 which)
  470. {
  471. dprintf(("resetting chip...\n"));
  472. writel(which, ns->base + CR);
  473. do {
  474. } while (readl(ns->base + CR) & which);
  475. dprintf(("okay!\n"));
  476. }
  477. static void ns83820_reset(struct nic *nic)
  478. {
  479. unsigned i;
  480. dprintf(("ns83820_reset\n"));
  481. writel(0, ns->base + PQCR);
  482. ns83820_setup_rx(nic);
  483. for (i = 0; i < NR_TX_DESC; i++) {
  484. tx_ring[i].link = 0;
  485. tx_ring[i].bufptr = 0;
  486. tx_ring[i].cmdsts = cpu_to_le32(0);
  487. tx_ring[i].extsts = cpu_to_le32(0);
  488. }
  489. ns->tx_idx = 0;
  490. ns->tx_done_idx = 0;
  491. writel(0, ns->base + TXDP_HI);
  492. return;
  493. }
  494. static void ns83820_getmac(struct nic *nic __unused, u8 * mac)
  495. {
  496. unsigned i;
  497. for (i = 0; i < 3; i++) {
  498. u32 data;
  499. /* Read from the perfect match memory: this is loaded by
  500. * the chip from the EEPROM via the EELOAD self test.
  501. */
  502. writel(i * 2, ns->base + RFCR);
  503. data = readl(ns->base + RFDR);
  504. *mac++ = data;
  505. *mac++ = data >> 8;
  506. }
  507. }
  508. static void ns83820_set_multicast(struct nic *nic __unused)
  509. {
  510. u8 *rfcr = ns->base + RFCR;
  511. u32 and_mask = 0xffffffff;
  512. u32 or_mask = 0;
  513. u32 val;
  514. /* Support Multicast */
  515. and_mask &= ~(RFCR_AAU | RFCR_AAM);
  516. or_mask |= RFCR_AAM;
  517. val = (readl(rfcr) & and_mask) | or_mask;
  518. /* Ramit : RFCR Write Fix doc says RFEN must be 0 modify other bits */
  519. writel(val & ~RFCR_RFEN, rfcr);
  520. writel(val, rfcr);
  521. }
  522. static void ns83820_run_bist(struct nic *nic __unused, const char *name,
  523. u32 enable, u32 done, u32 fail)
  524. {
  525. int timed_out = 0;
  526. long start;
  527. u32 status;
  528. int loops = 0;
  529. dprintf(("start %s\n", name))
  530. start = currticks();
  531. writel(enable, ns->base + PTSCR);
  532. for (;;) {
  533. loops++;
  534. status = readl(ns->base + PTSCR);
  535. if (!(status & enable))
  536. break;
  537. if (status & done)
  538. break;
  539. if (status & fail)
  540. break;
  541. if ((currticks() - start) >= HZ) {
  542. timed_out = 1;
  543. break;
  544. }
  545. }
  546. if (status & fail)
  547. printf("%s failed! (0x%hX & 0x%hX)\n", name, (unsigned int) status,
  548. (unsigned int) fail);
  549. else if (timed_out)
  550. printf("run_bist %s timed out! (%hX)\n", name, (unsigned int) status);
  551. dprintf(("done %s in %d loops\n", name, loops));
  552. }
  553. /*************************************
  554. Check Link
  555. *************************************/
  556. static void ns83820_check_intr(struct nic *nic) {
  557. int i;
  558. u32 isr = readl(ns->base + ISR);
  559. if(ISR_PHY & isr)
  560. phy_intr(nic);
  561. if(( ISR_RXIDLE | ISR_RXDESC | ISR_RXERR) & isr)
  562. kick_rx();
  563. for (i = 0; i < NR_RX_DESC; i++) {
  564. if (rx_ring[i].cmdsts == CMDSTS_OWN) {
  565. // rx_ring[i].link = virt_to_le32desc(&rx_ring[i + 1]);
  566. rx_ring[i].cmdsts = cpu_to_le32(REAL_RX_BUF_SIZE);
  567. }
  568. }
  569. }
  570. /**************************************************************************
  571. POLL - Wait for a frame
  572. ***************************************************************************/
  573. static int ns83820_poll(struct nic *nic, int retrieve)
  574. {
  575. /* return true if there's an ethernet packet ready to read */
  576. /* nic->packet should contain data on return */
  577. /* nic->packetlen should contain length of data */
  578. u32 cmdsts;
  579. int entry = ns->cur_rx;
  580. ns83820_check_intr(nic);
  581. cmdsts = le32_to_cpu(rx_ring[entry].cmdsts);
  582. if ( ! ( (CMDSTS_OWN & (cmdsts)) && (cmdsts != (CMDSTS_OWN)) ) )
  583. return 0;
  584. if ( ! retrieve ) return 1;
  585. if (! (CMDSTS_OK & cmdsts) )
  586. return 0;
  587. nic->packetlen = cmdsts & 0xffff;
  588. memcpy(nic->packet,
  589. rxb + (entry * REAL_RX_BUF_SIZE),
  590. nic->packetlen);
  591. // rx_ring[entry].link = 0;
  592. rx_ring[entry].cmdsts = cpu_to_le32(CMDSTS_OWN);
  593. ns->cur_rx = ++ns->cur_rx % NR_RX_DESC;
  594. if (ns->cur_rx == 0) /* We have wrapped the ring */
  595. kick_rx();
  596. return 1;
  597. }
  598. static inline void kick_tx(struct nic *nic __unused)
  599. {
  600. dprintf(("kick_tx\n"));
  601. writel(CR_TXE, ns->base + CR);
  602. }
  603. /**************************************************************************
  604. TRANSMIT - Transmit a frame
  605. ***************************************************************************/
  606. static void ns83820_transmit(struct nic *nic, const char *d, /* Destination */
  607. unsigned int t, /* Type */
  608. unsigned int s, /* size */
  609. const char *p)
  610. { /* Packet */
  611. /* send the packet to destination */
  612. u16 nstype;
  613. u32 cmdsts, extsts;
  614. int cur_tx = 0;
  615. u32 isr = readl(ns->base + ISR);
  616. if (ISR_TXIDLE & isr)
  617. kick_tx(nic);
  618. /* point to the current txb incase multiple tx_rings are used */
  619. memcpy(txb, d, ETH_ALEN);
  620. memcpy(txb + ETH_ALEN, nic->node_addr, ETH_ALEN);
  621. nstype = htons((u16) t);
  622. memcpy(txb + 2 * ETH_ALEN, (u8 *) & nstype, 2);
  623. memcpy(txb + ETH_HLEN, p, s);
  624. s += ETH_HLEN;
  625. s &= 0x0FFF;
  626. while (s < ETH_ZLEN)
  627. txb[s++] = '\0';
  628. /* Setup the transmit descriptor */
  629. extsts = 0;
  630. extsts |= EXTSTS_UDPPKT;
  631. tx_ring[cur_tx].bufptr = virt_to_le32desc(&txb);
  632. tx_ring[cur_tx].extsts = cpu_to_le32(extsts);
  633. cmdsts = cpu_to_le32(0);
  634. cmdsts |= cpu_to_le32(CMDSTS_OWN | s);
  635. tx_ring[cur_tx].cmdsts = cpu_to_le32(cmdsts);
  636. writel(virt_to_le32desc(&tx_ring[0]), ns->base + TXDP);
  637. kick_tx(nic);
  638. }
  639. /**************************************************************************
  640. DISABLE - Turn off ethernet interface
  641. ***************************************************************************/
  642. static void ns83820_disable ( struct nic *nic ) {
  643. /* put the card in its initial state */
  644. /* This function serves 3 purposes.
  645. * This disables DMA and interrupts so we don't receive
  646. * unexpected packets or interrupts from the card after
  647. * etherboot has finished.
  648. * This frees resources so etherboot may use
  649. * this driver on another interface
  650. * This allows etherboot to reinitialize the interface
  651. * if something is something goes wrong.
  652. */
  653. /* disable interrupts */
  654. writel(0, ns->base + IMR);
  655. writel(0, ns->base + IER);
  656. readl(ns->base + IER);
  657. ns->up = 0;
  658. ns83820_do_reset(nic, CR_RST);
  659. ns->IMR_cache &=
  660. ~(ISR_RXOK | ISR_RXDESC | ISR_RXERR | ISR_RXEARLY |
  661. ISR_RXIDLE);
  662. writel(ns->IMR_cache, ns->base + IMR);
  663. /* touch the pci bus... */
  664. readl(ns->base + IMR);
  665. /* assumes the transmitter is already disabled and reset */
  666. writel(0, ns->base + RXDP_HI);
  667. writel(0, ns->base + RXDP);
  668. }
  669. /**************************************************************************
  670. IRQ - Enable, Disable, or Force interrupts
  671. ***************************************************************************/
  672. static void ns83820_irq(struct nic *nic __unused, irq_action_t action __unused)
  673. {
  674. switch ( action ) {
  675. case DISABLE :
  676. break;
  677. case ENABLE :
  678. break;
  679. case FORCE :
  680. break;
  681. }
  682. }
  683. static struct nic_operations ns83820_operations = {
  684. .connect = dummy_connect,
  685. .poll = ns83820_poll,
  686. .transmit = ns83820_transmit,
  687. .irq = ns83820_irq,
  688. };
  689. static struct pci_device_id ns83820_nics[] = {
  690. PCI_ROM(0x100b, 0x0022, "ns83820", "National Semiconductor 83820"),
  691. };
  692. PCI_DRIVER ( ns83820_driver, ns83820_nics, PCI_NO_CLASS );
  693. /**************************************************************************
  694. PROBE - Look for an adapter, this routine's visible to the outside
  695. ***************************************************************************/
  696. #define board_found 1
  697. #define valid_link 0
  698. static int ns83820_probe ( struct nic *nic, struct pci_device *pci ) {
  699. long addr;
  700. int using_dac = 0;
  701. if (pci->ioaddr == 0)
  702. return 0;
  703. printf("ns83820.c: Found %s, vendor=0x%hX, device=0x%hX\n",
  704. pci->driver_name, pci->vendor, pci->device);
  705. /* point to private storage */
  706. ns = &nsx;
  707. adjust_pci_device(pci);
  708. addr = pci_bar_start(pci, PCI_BASE_ADDRESS_1);
  709. ns->base = ioremap(addr, (1UL << 12));
  710. if (!ns->base)
  711. return 0;
  712. nic->irqno = 0;
  713. nic->ioaddr = pci->ioaddr & ~3;
  714. /* disable interrupts */
  715. writel(0, ns->base + IMR);
  716. writel(0, ns->base + IER);
  717. readl(ns->base + IER);
  718. ns->IMR_cache = 0;
  719. ns83820_do_reset(nic, CR_RST);
  720. /* Must reset the ram bist before running it */
  721. writel(PTSCR_RBIST_RST, ns->base + PTSCR);
  722. ns83820_run_bist(nic, "sram bist", PTSCR_RBIST_EN,
  723. PTSCR_RBIST_DONE, PTSCR_RBIST_FAIL);
  724. ns83820_run_bist(nic, "eeprom bist", PTSCR_EEBIST_EN, 0,
  725. PTSCR_EEBIST_FAIL);
  726. ns83820_run_bist(nic, "eeprom load", PTSCR_EELOAD_EN, 0, 0);
  727. /* I love config registers */
  728. ns->CFG_cache = readl(ns->base + CFG);
  729. if ((ns->CFG_cache & CFG_PCI64_DET)) {
  730. printf("%s: detected 64 bit PCI data bus.\n", pci->driver_name);
  731. /*dev->CFG_cache |= CFG_DATA64_EN; */
  732. if (!(ns->CFG_cache & CFG_DATA64_EN))
  733. printf
  734. ("%s: EEPROM did not enable 64 bit bus. Disabled.\n",
  735. pci->driver_name);
  736. } else
  737. ns->CFG_cache &= ~(CFG_DATA64_EN);
  738. ns->CFG_cache &= (CFG_TBI_EN | CFG_MRM_DIS | CFG_MWI_DIS |
  739. CFG_T64ADDR | CFG_DATA64_EN | CFG_EXT_125 |
  740. CFG_M64ADDR);
  741. ns->CFG_cache |=
  742. CFG_PINT_DUPSTS | CFG_PINT_LNKSTS | CFG_PINT_SPDSTS |
  743. CFG_EXTSTS_EN | CFG_EXD | CFG_PESEL;
  744. ns->CFG_cache |= CFG_REQALG;
  745. ns->CFG_cache |= CFG_POW;
  746. ns->CFG_cache |= CFG_TMRTEST;
  747. /* When compiled with 64 bit addressing, we must always enable
  748. * the 64 bit descriptor format.
  749. */
  750. #ifdef USE_64BIT_ADDR
  751. ns->CFG_cache |= CFG_M64ADDR;
  752. #endif
  753. //FIXME: Enable section on dac or remove this
  754. if (using_dac)
  755. ns->CFG_cache |= CFG_T64ADDR;
  756. /* Big endian mode does not seem to do what the docs suggest */
  757. ns->CFG_cache &= ~CFG_BEM;
  758. /* setup optical transceiver if we have one */
  759. if (ns->CFG_cache & CFG_TBI_EN) {
  760. dprintf(("%s: enabling optical transceiver\n", pci->driver_name));
  761. writel(readl(ns->base + GPIOR) | 0x3e8, ns->base + GPIOR);
  762. /* setup auto negotiation feature advertisement */
  763. writel(readl(ns->base + TANAR)
  764. | TANAR_HALF_DUP | TANAR_FULL_DUP,
  765. ns->base + TANAR);
  766. /* start auto negotiation */
  767. writel(TBICR_MR_AN_ENABLE | TBICR_MR_RESTART_AN,
  768. ns->base + TBICR);
  769. writel(TBICR_MR_AN_ENABLE, ns->base + TBICR);
  770. ns->linkstate = LINK_AUTONEGOTIATE;
  771. ns->CFG_cache |= CFG_MODE_1000;
  772. }
  773. writel(ns->CFG_cache, ns->base + CFG);
  774. dprintf(("CFG: %hX\n", ns->CFG_cache));
  775. /* FIXME: reset_phy is defaulted to 0, should we reset anyway? */
  776. if (reset_phy) {
  777. dprintf(("%s: resetting phy\n", pci->driver_name));
  778. writel(ns->CFG_cache | CFG_PHY_RST, ns->base + CFG);
  779. writel(ns->CFG_cache, ns->base + CFG);
  780. }
  781. #if 0 /* Huh? This sets the PCI latency register. Should be done via
  782. * the PCI layer. FIXME.
  783. */
  784. if (readl(dev->base + SRR))
  785. writel(readl(dev->base + 0x20c) | 0xfe00,
  786. dev->base + 0x20c);
  787. #endif
  788. /* Note! The DMA burst size interacts with packet
  789. * transmission, such that the largest packet that
  790. * can be transmitted is 8192 - FLTH - burst size.
  791. * If only the transmit fifo was larger...
  792. */
  793. /* Ramit : 1024 DMA is not a good idea, it ends up banging
  794. * some DELL and COMPAQ SMP systems */
  795. writel(TXCFG_CSI | TXCFG_HBI | TXCFG_ATP | TXCFG_MXDMA512
  796. | ((1600 / 32) * 0x100), ns->base + TXCFG);
  797. /* Set Rx to full duplex, don't accept runt, errored, long or length
  798. * range errored packets. Use 512 byte DMA.
  799. */
  800. /* Ramit : 1024 DMA is not a good idea, it ends up banging
  801. * some DELL and COMPAQ SMP systems
  802. * Turn on ALP, only we are accpeting Jumbo Packets */
  803. writel(RXCFG_AEP | RXCFG_ARP | RXCFG_AIRL | RXCFG_RX_FD
  804. | RXCFG_STRIPCRC
  805. //| RXCFG_ALP
  806. | (RXCFG_MXDMA512) | 0, ns->base + RXCFG);
  807. /* Disable priority queueing */
  808. writel(0, ns->base + PQCR);
  809. /* Enable IP checksum validation and detetion of VLAN headers.
  810. * Note: do not set the reject options as at least the 0x102
  811. * revision of the chip does not properly accept IP fragments
  812. * at least for UDP.
  813. */
  814. /* Ramit : Be sure to turn on RXCFG_ARP if VLAN's are enabled, since
  815. * the MAC it calculates the packetsize AFTER stripping the VLAN
  816. * header, and if a VLAN Tagged packet of 64 bytes is received (like
  817. * a ping with a VLAN header) then the card, strips the 4 byte VLAN
  818. * tag and then checks the packet size, so if RXCFG_ARP is not enabled,
  819. * it discrards it!. These guys......
  820. */
  821. writel(VRCR_IPEN | VRCR_VTDEN, ns->base + VRCR);
  822. /* Enable per-packet TCP/UDP/IP checksumming */
  823. writel(VTCR_PPCHK, ns->base + VTCR);
  824. /* Ramit : Enable async and sync pause frames */
  825. // writel(0, ns->base + PCR);
  826. writel((PCR_PS_MCAST | PCR_PS_DA | PCR_PSEN | PCR_FFLO_4K |
  827. PCR_FFHI_8K | PCR_STLO_4 | PCR_STHI_8 | PCR_PAUSE_CNT),
  828. ns->base + PCR);
  829. /* Disable Wake On Lan */
  830. writel(0, ns->base + WCSR);
  831. ns83820_getmac(nic, nic->node_addr);
  832. if (using_dac) {
  833. dprintf(("%s: using 64 bit addressing.\n", pci->driver_name));
  834. }
  835. dprintf(("%s: DP83820 %d.%d: %! io=0x%hX\n",
  836. pci->driver_name,
  837. (unsigned) readl(ns->base + SRR) >> 8,
  838. (unsigned) readl(ns->base + SRR) & 0xff,
  839. nic->node_addr, pci->ioaddr));
  840. #ifdef PHY_CODE_IS_FINISHED
  841. ns83820_probe_phy(dev);
  842. #endif
  843. ns83820_reset(nic);
  844. /* point to NIC specific routines */
  845. nic->nic_op = &ns83820_operations;
  846. return 1;
  847. }
  848. DRIVER ( "NS83820/PCI", nic_driver, pci_driver, ns83820_driver,
  849. ns83820_probe, ns83820_disable );
  850. /*
  851. * Local variables:
  852. * c-basic-offset: 8
  853. * c-indent-level: 8
  854. * tab-width: 8
  855. * End:
  856. */