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

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