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

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