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

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