選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ns83820.c 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  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. #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. 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, status, fail);
  548. else if (timed_out)
  549. printf("run_bist %s timed out! (%hX)\n", name, status);
  550. dprintf(("done %s in %d loops\n", name, loops));
  551. }
  552. /*************************************
  553. Check Link
  554. *************************************/
  555. static void ns83820_check_intr(struct nic *nic) {
  556. int i;
  557. u32 isr = readl(ns->base + ISR);
  558. if(ISR_PHY & isr)
  559. phy_intr(nic);
  560. if(( ISR_RXIDLE | ISR_RXDESC | ISR_RXERR) & isr)
  561. kick_rx();
  562. for (i = 0; i < NR_RX_DESC; i++) {
  563. if (rx_ring[i].cmdsts == CMDSTS_OWN) {
  564. // rx_ring[i].link = virt_to_le32desc(&rx_ring[i + 1]);
  565. rx_ring[i].cmdsts = cpu_to_le32(REAL_RX_BUF_SIZE);
  566. }
  567. }
  568. }
  569. /**************************************************************************
  570. POLL - Wait for a frame
  571. ***************************************************************************/
  572. static int ns83820_poll(struct nic *nic, int retrieve)
  573. {
  574. /* return true if there's an ethernet packet ready to read */
  575. /* nic->packet should contain data on return */
  576. /* nic->packetlen should contain length of data */
  577. u32 cmdsts;
  578. int entry = ns->cur_rx;
  579. ns83820_check_intr(nic);
  580. cmdsts = le32_to_cpu(rx_ring[entry].cmdsts);
  581. if ( ! ( (CMDSTS_OWN & (cmdsts)) && (cmdsts != (CMDSTS_OWN)) ) )
  582. return 0;
  583. if ( ! retrieve ) return 1;
  584. if (! (CMDSTS_OK & cmdsts) )
  585. return 0;
  586. nic->packetlen = cmdsts & 0xffff;
  587. memcpy(nic->packet,
  588. rxb + (entry * REAL_RX_BUF_SIZE),
  589. nic->packetlen);
  590. // rx_ring[entry].link = 0;
  591. rx_ring[entry].cmdsts = cpu_to_le32(CMDSTS_OWN);
  592. ns->cur_rx = ++ns->cur_rx % NR_RX_DESC;
  593. if (ns->cur_rx == 0) /* We have wrapped the ring */
  594. kick_rx();
  595. return 1;
  596. }
  597. static inline void kick_tx(struct nic *nic __unused)
  598. {
  599. dprintf(("kick_tx\n"));
  600. writel(CR_TXE, ns->base + CR);
  601. }
  602. /**************************************************************************
  603. TRANSMIT - Transmit a frame
  604. ***************************************************************************/
  605. static void ns83820_transmit(struct nic *nic, const char *d, /* Destination */
  606. unsigned int t, /* Type */
  607. unsigned int s, /* size */
  608. const char *p)
  609. { /* Packet */
  610. /* send the packet to destination */
  611. u16 nstype;
  612. u32 cmdsts, extsts;
  613. int cur_tx = 0;
  614. u32 isr = readl(ns->base + ISR);
  615. if (ISR_TXIDLE & isr)
  616. kick_tx(nic);
  617. /* point to the current txb incase multiple tx_rings are used */
  618. memcpy(txb, d, ETH_ALEN);
  619. memcpy(txb + ETH_ALEN, nic->node_addr, ETH_ALEN);
  620. nstype = htons((u16) t);
  621. memcpy(txb + 2 * ETH_ALEN, (u8 *) & nstype, 2);
  622. memcpy(txb + ETH_HLEN, p, s);
  623. s += ETH_HLEN;
  624. s &= 0x0FFF;
  625. while (s < ETH_ZLEN)
  626. txb[s++] = '\0';
  627. /* Setup the transmit descriptor */
  628. extsts = 0;
  629. extsts |= EXTSTS_UDPPKT;
  630. tx_ring[cur_tx].bufptr = virt_to_le32desc(&txb);
  631. tx_ring[cur_tx].extsts = cpu_to_le32(extsts);
  632. cmdsts = cpu_to_le32(0);
  633. cmdsts |= cpu_to_le32(CMDSTS_OWN | s);
  634. tx_ring[cur_tx].cmdsts = cpu_to_le32(cmdsts);
  635. writel(virt_to_le32desc(&tx_ring[0]), ns->base + TXDP);
  636. kick_tx(nic);
  637. }
  638. /**************************************************************************
  639. DISABLE - Turn off ethernet interface
  640. ***************************************************************************/
  641. static void ns83820_disable ( struct nic *nic, struct pci_device *pci __unused ) {
  642. nic_disable ( 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_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. int sz;
  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->name, pci->vendor_id, pci->device_id);
  706. /* point to private storage */
  707. ns = &nsx;
  708. adjust_pci_device(pci);
  709. addr = pci_bar_start(pci, PCI_BASE_ADDRESS_1);
  710. sz = pci_bar_size(pci, PCI_BASE_ADDRESS_1);
  711. ns->base = ioremap(addr, (1UL << 12));
  712. // ns->base = ioremap(addr, sz);
  713. if (!ns->base)
  714. return 0;
  715. nic->irqno = 0;
  716. pci_fill_nic ( nic, pci );
  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. nic->nic_op = &ns83820_operations;
  851. return 1;
  852. }
  853. DRIVER ( "NS83820/PCI", nic_driver, pci_driver, ns83820_driver,
  854. ns83820_probe, ns83820_disable );