sunbmac.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  1. /* $Id: sunbmac.c,v 1.30 2002/01/15 06:48:55 davem Exp $
  2. * sunbmac.c: Driver for Sparc BigMAC 100baseT ethernet adapters.
  3. *
  4. * Copyright (C) 1997, 1998, 1999, 2003 David S. Miller (davem@redhat.com)
  5. */
  6. #include <linux/module.h>
  7. #include <linux/kernel.h>
  8. #include <linux/types.h>
  9. #include <linux/fcntl.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/ioport.h>
  12. #include <linux/in.h>
  13. #include <linux/slab.h>
  14. #include <linux/string.h>
  15. #include <linux/delay.h>
  16. #include <linux/init.h>
  17. #include <linux/crc32.h>
  18. #include <linux/errno.h>
  19. #include <linux/ethtool.h>
  20. #include <linux/netdevice.h>
  21. #include <linux/etherdevice.h>
  22. #include <linux/skbuff.h>
  23. #include <linux/bitops.h>
  24. #include <asm/auxio.h>
  25. #include <asm/byteorder.h>
  26. #include <asm/dma.h>
  27. #include <asm/idprom.h>
  28. #include <asm/io.h>
  29. #include <asm/openprom.h>
  30. #include <asm/oplib.h>
  31. #include <asm/pgtable.h>
  32. #include <asm/sbus.h>
  33. #include <asm/system.h>
  34. #include "sunbmac.h"
  35. #define DRV_NAME "sunbmac"
  36. #define DRV_VERSION "2.0"
  37. #define DRV_RELDATE "11/24/03"
  38. #define DRV_AUTHOR "David S. Miller (davem@redhat.com)"
  39. static char version[] __initdata =
  40. DRV_NAME ".c:v" DRV_VERSION " " DRV_RELDATE " " DRV_AUTHOR "\n";
  41. MODULE_VERSION(DRV_VERSION);
  42. MODULE_AUTHOR(DRV_AUTHOR);
  43. MODULE_DESCRIPTION("Sun BigMAC 100baseT ethernet driver");
  44. MODULE_LICENSE("GPL");
  45. #undef DEBUG_PROBE
  46. #undef DEBUG_TX
  47. #undef DEBUG_IRQ
  48. #ifdef DEBUG_PROBE
  49. #define DP(x) printk x
  50. #else
  51. #define DP(x)
  52. #endif
  53. #ifdef DEBUG_TX
  54. #define DTX(x) printk x
  55. #else
  56. #define DTX(x)
  57. #endif
  58. #ifdef DEBUG_IRQ
  59. #define DIRQ(x) printk x
  60. #else
  61. #define DIRQ(x)
  62. #endif
  63. static struct bigmac *root_bigmac_dev;
  64. #define DEFAULT_JAMSIZE 4 /* Toe jam */
  65. #define QEC_RESET_TRIES 200
  66. static int qec_global_reset(void __iomem *gregs)
  67. {
  68. int tries = QEC_RESET_TRIES;
  69. sbus_writel(GLOB_CTRL_RESET, gregs + GLOB_CTRL);
  70. while (--tries) {
  71. if (sbus_readl(gregs + GLOB_CTRL) & GLOB_CTRL_RESET) {
  72. udelay(20);
  73. continue;
  74. }
  75. break;
  76. }
  77. if (tries)
  78. return 0;
  79. printk(KERN_ERR "BigMAC: Cannot reset the QEC.\n");
  80. return -1;
  81. }
  82. static void qec_init(struct bigmac *bp)
  83. {
  84. void __iomem *gregs = bp->gregs;
  85. struct sbus_dev *qec_sdev = bp->qec_sdev;
  86. u8 bsizes = bp->bigmac_bursts;
  87. u32 regval;
  88. /* 64byte bursts do not work at the moment, do
  89. * not even try to enable them. -DaveM
  90. */
  91. if (bsizes & DMA_BURST32)
  92. regval = GLOB_CTRL_B32;
  93. else
  94. regval = GLOB_CTRL_B16;
  95. sbus_writel(regval | GLOB_CTRL_BMODE, gregs + GLOB_CTRL);
  96. sbus_writel(GLOB_PSIZE_2048, gregs + GLOB_PSIZE);
  97. /* All of memsize is given to bigmac. */
  98. sbus_writel(qec_sdev->reg_addrs[1].reg_size,
  99. gregs + GLOB_MSIZE);
  100. /* Half to the transmitter, half to the receiver. */
  101. sbus_writel(qec_sdev->reg_addrs[1].reg_size >> 1,
  102. gregs + GLOB_TSIZE);
  103. sbus_writel(qec_sdev->reg_addrs[1].reg_size >> 1,
  104. gregs + GLOB_RSIZE);
  105. }
  106. #define TX_RESET_TRIES 32
  107. #define RX_RESET_TRIES 32
  108. static void bigmac_tx_reset(void __iomem *bregs)
  109. {
  110. int tries = TX_RESET_TRIES;
  111. sbus_writel(0, bregs + BMAC_TXCFG);
  112. /* The fifo threshold bit is read-only and does
  113. * not clear. -DaveM
  114. */
  115. while ((sbus_readl(bregs + BMAC_TXCFG) & ~(BIGMAC_TXCFG_FIFO)) != 0 &&
  116. --tries != 0)
  117. udelay(20);
  118. if (!tries) {
  119. printk(KERN_ERR "BIGMAC: Transmitter will not reset.\n");
  120. printk(KERN_ERR "BIGMAC: tx_cfg is %08x\n",
  121. sbus_readl(bregs + BMAC_TXCFG));
  122. }
  123. }
  124. static void bigmac_rx_reset(void __iomem *bregs)
  125. {
  126. int tries = RX_RESET_TRIES;
  127. sbus_writel(0, bregs + BMAC_RXCFG);
  128. while (sbus_readl(bregs + BMAC_RXCFG) && --tries)
  129. udelay(20);
  130. if (!tries) {
  131. printk(KERN_ERR "BIGMAC: Receiver will not reset.\n");
  132. printk(KERN_ERR "BIGMAC: rx_cfg is %08x\n",
  133. sbus_readl(bregs + BMAC_RXCFG));
  134. }
  135. }
  136. /* Reset the transmitter and receiver. */
  137. static void bigmac_stop(struct bigmac *bp)
  138. {
  139. bigmac_tx_reset(bp->bregs);
  140. bigmac_rx_reset(bp->bregs);
  141. }
  142. static void bigmac_get_counters(struct bigmac *bp, void __iomem *bregs)
  143. {
  144. struct net_device_stats *stats = &bp->enet_stats;
  145. stats->rx_crc_errors += sbus_readl(bregs + BMAC_RCRCECTR);
  146. sbus_writel(0, bregs + BMAC_RCRCECTR);
  147. stats->rx_frame_errors += sbus_readl(bregs + BMAC_UNALECTR);
  148. sbus_writel(0, bregs + BMAC_UNALECTR);
  149. stats->rx_length_errors += sbus_readl(bregs + BMAC_GLECTR);
  150. sbus_writel(0, bregs + BMAC_GLECTR);
  151. stats->tx_aborted_errors += sbus_readl(bregs + BMAC_EXCTR);
  152. stats->collisions +=
  153. (sbus_readl(bregs + BMAC_EXCTR) +
  154. sbus_readl(bregs + BMAC_LTCTR));
  155. sbus_writel(0, bregs + BMAC_EXCTR);
  156. sbus_writel(0, bregs + BMAC_LTCTR);
  157. }
  158. static void bigmac_clean_rings(struct bigmac *bp)
  159. {
  160. int i;
  161. for (i = 0; i < RX_RING_SIZE; i++) {
  162. if (bp->rx_skbs[i] != NULL) {
  163. dev_kfree_skb_any(bp->rx_skbs[i]);
  164. bp->rx_skbs[i] = NULL;
  165. }
  166. }
  167. for (i = 0; i < TX_RING_SIZE; i++) {
  168. if (bp->tx_skbs[i] != NULL) {
  169. dev_kfree_skb_any(bp->tx_skbs[i]);
  170. bp->tx_skbs[i] = NULL;
  171. }
  172. }
  173. }
  174. static void bigmac_init_rings(struct bigmac *bp, int from_irq)
  175. {
  176. struct bmac_init_block *bb = bp->bmac_block;
  177. struct net_device *dev = bp->dev;
  178. int i;
  179. gfp_t gfp_flags = GFP_KERNEL;
  180. if (from_irq || in_interrupt())
  181. gfp_flags = GFP_ATOMIC;
  182. bp->rx_new = bp->rx_old = bp->tx_new = bp->tx_old = 0;
  183. /* Free any skippy bufs left around in the rings. */
  184. bigmac_clean_rings(bp);
  185. /* Now get new skbufs for the receive ring. */
  186. for (i = 0; i < RX_RING_SIZE; i++) {
  187. struct sk_buff *skb;
  188. skb = big_mac_alloc_skb(RX_BUF_ALLOC_SIZE, gfp_flags);
  189. if (!skb)
  190. continue;
  191. bp->rx_skbs[i] = skb;
  192. skb->dev = dev;
  193. /* Because we reserve afterwards. */
  194. skb_put(skb, ETH_FRAME_LEN);
  195. skb_reserve(skb, 34);
  196. bb->be_rxd[i].rx_addr =
  197. sbus_map_single(bp->bigmac_sdev, skb->data,
  198. RX_BUF_ALLOC_SIZE - 34,
  199. SBUS_DMA_FROMDEVICE);
  200. bb->be_rxd[i].rx_flags =
  201. (RXD_OWN | ((RX_BUF_ALLOC_SIZE - 34) & RXD_LENGTH));
  202. }
  203. for (i = 0; i < TX_RING_SIZE; i++)
  204. bb->be_txd[i].tx_flags = bb->be_txd[i].tx_addr = 0;
  205. }
  206. #define MGMT_CLKON (MGMT_PAL_INT_MDIO|MGMT_PAL_EXT_MDIO|MGMT_PAL_OENAB|MGMT_PAL_DCLOCK)
  207. #define MGMT_CLKOFF (MGMT_PAL_INT_MDIO|MGMT_PAL_EXT_MDIO|MGMT_PAL_OENAB)
  208. static void idle_transceiver(void __iomem *tregs)
  209. {
  210. int i = 20;
  211. while (i--) {
  212. sbus_writel(MGMT_CLKOFF, tregs + TCVR_MPAL);
  213. sbus_readl(tregs + TCVR_MPAL);
  214. sbus_writel(MGMT_CLKON, tregs + TCVR_MPAL);
  215. sbus_readl(tregs + TCVR_MPAL);
  216. }
  217. }
  218. static void write_tcvr_bit(struct bigmac *bp, void __iomem *tregs, int bit)
  219. {
  220. if (bp->tcvr_type == internal) {
  221. bit = (bit & 1) << 3;
  222. sbus_writel(bit | (MGMT_PAL_OENAB | MGMT_PAL_EXT_MDIO),
  223. tregs + TCVR_MPAL);
  224. sbus_readl(tregs + TCVR_MPAL);
  225. sbus_writel(bit | MGMT_PAL_OENAB | MGMT_PAL_EXT_MDIO | MGMT_PAL_DCLOCK,
  226. tregs + TCVR_MPAL);
  227. sbus_readl(tregs + TCVR_MPAL);
  228. } else if (bp->tcvr_type == external) {
  229. bit = (bit & 1) << 2;
  230. sbus_writel(bit | MGMT_PAL_INT_MDIO | MGMT_PAL_OENAB,
  231. tregs + TCVR_MPAL);
  232. sbus_readl(tregs + TCVR_MPAL);
  233. sbus_writel(bit | MGMT_PAL_INT_MDIO | MGMT_PAL_OENAB | MGMT_PAL_DCLOCK,
  234. tregs + TCVR_MPAL);
  235. sbus_readl(tregs + TCVR_MPAL);
  236. } else {
  237. printk(KERN_ERR "write_tcvr_bit: No transceiver type known!\n");
  238. }
  239. }
  240. static int read_tcvr_bit(struct bigmac *bp, void __iomem *tregs)
  241. {
  242. int retval = 0;
  243. if (bp->tcvr_type == internal) {
  244. sbus_writel(MGMT_PAL_EXT_MDIO, tregs + TCVR_MPAL);
  245. sbus_readl(tregs + TCVR_MPAL);
  246. sbus_writel(MGMT_PAL_EXT_MDIO | MGMT_PAL_DCLOCK,
  247. tregs + TCVR_MPAL);
  248. sbus_readl(tregs + TCVR_MPAL);
  249. retval = (sbus_readl(tregs + TCVR_MPAL) & MGMT_PAL_INT_MDIO) >> 3;
  250. } else if (bp->tcvr_type == external) {
  251. sbus_writel(MGMT_PAL_INT_MDIO, tregs + TCVR_MPAL);
  252. sbus_readl(tregs + TCVR_MPAL);
  253. sbus_writel(MGMT_PAL_INT_MDIO | MGMT_PAL_DCLOCK, tregs + TCVR_MPAL);
  254. sbus_readl(tregs + TCVR_MPAL);
  255. retval = (sbus_readl(tregs + TCVR_MPAL) & MGMT_PAL_EXT_MDIO) >> 2;
  256. } else {
  257. printk(KERN_ERR "read_tcvr_bit: No transceiver type known!\n");
  258. }
  259. return retval;
  260. }
  261. static int read_tcvr_bit2(struct bigmac *bp, void __iomem *tregs)
  262. {
  263. int retval = 0;
  264. if (bp->tcvr_type == internal) {
  265. sbus_writel(MGMT_PAL_EXT_MDIO, tregs + TCVR_MPAL);
  266. sbus_readl(tregs + TCVR_MPAL);
  267. retval = (sbus_readl(tregs + TCVR_MPAL) & MGMT_PAL_INT_MDIO) >> 3;
  268. sbus_writel(MGMT_PAL_EXT_MDIO | MGMT_PAL_DCLOCK, tregs + TCVR_MPAL);
  269. sbus_readl(tregs + TCVR_MPAL);
  270. } else if (bp->tcvr_type == external) {
  271. sbus_writel(MGMT_PAL_INT_MDIO, tregs + TCVR_MPAL);
  272. sbus_readl(tregs + TCVR_MPAL);
  273. retval = (sbus_readl(tregs + TCVR_MPAL) & MGMT_PAL_EXT_MDIO) >> 2;
  274. sbus_writel(MGMT_PAL_INT_MDIO | MGMT_PAL_DCLOCK, tregs + TCVR_MPAL);
  275. sbus_readl(tregs + TCVR_MPAL);
  276. } else {
  277. printk(KERN_ERR "read_tcvr_bit2: No transceiver type known!\n");
  278. }
  279. return retval;
  280. }
  281. static void put_tcvr_byte(struct bigmac *bp,
  282. void __iomem *tregs,
  283. unsigned int byte)
  284. {
  285. int shift = 4;
  286. do {
  287. write_tcvr_bit(bp, tregs, ((byte >> shift) & 1));
  288. shift -= 1;
  289. } while (shift >= 0);
  290. }
  291. static void bigmac_tcvr_write(struct bigmac *bp, void __iomem *tregs,
  292. int reg, unsigned short val)
  293. {
  294. int shift;
  295. reg &= 0xff;
  296. val &= 0xffff;
  297. switch(bp->tcvr_type) {
  298. case internal:
  299. case external:
  300. break;
  301. default:
  302. printk(KERN_ERR "bigmac_tcvr_read: Whoops, no known transceiver type.\n");
  303. return;
  304. };
  305. idle_transceiver(tregs);
  306. write_tcvr_bit(bp, tregs, 0);
  307. write_tcvr_bit(bp, tregs, 1);
  308. write_tcvr_bit(bp, tregs, 0);
  309. write_tcvr_bit(bp, tregs, 1);
  310. put_tcvr_byte(bp, tregs,
  311. ((bp->tcvr_type == internal) ?
  312. BIGMAC_PHY_INTERNAL : BIGMAC_PHY_EXTERNAL));
  313. put_tcvr_byte(bp, tregs, reg);
  314. write_tcvr_bit(bp, tregs, 1);
  315. write_tcvr_bit(bp, tregs, 0);
  316. shift = 15;
  317. do {
  318. write_tcvr_bit(bp, tregs, (val >> shift) & 1);
  319. shift -= 1;
  320. } while (shift >= 0);
  321. }
  322. static unsigned short bigmac_tcvr_read(struct bigmac *bp,
  323. void __iomem *tregs,
  324. int reg)
  325. {
  326. unsigned short retval = 0;
  327. reg &= 0xff;
  328. switch(bp->tcvr_type) {
  329. case internal:
  330. case external:
  331. break;
  332. default:
  333. printk(KERN_ERR "bigmac_tcvr_read: Whoops, no known transceiver type.\n");
  334. return 0xffff;
  335. };
  336. idle_transceiver(tregs);
  337. write_tcvr_bit(bp, tregs, 0);
  338. write_tcvr_bit(bp, tregs, 1);
  339. write_tcvr_bit(bp, tregs, 1);
  340. write_tcvr_bit(bp, tregs, 0);
  341. put_tcvr_byte(bp, tregs,
  342. ((bp->tcvr_type == internal) ?
  343. BIGMAC_PHY_INTERNAL : BIGMAC_PHY_EXTERNAL));
  344. put_tcvr_byte(bp, tregs, reg);
  345. if (bp->tcvr_type == external) {
  346. int shift = 15;
  347. (void) read_tcvr_bit2(bp, tregs);
  348. (void) read_tcvr_bit2(bp, tregs);
  349. do {
  350. int tmp;
  351. tmp = read_tcvr_bit2(bp, tregs);
  352. retval |= ((tmp & 1) << shift);
  353. shift -= 1;
  354. } while (shift >= 0);
  355. (void) read_tcvr_bit2(bp, tregs);
  356. (void) read_tcvr_bit2(bp, tregs);
  357. (void) read_tcvr_bit2(bp, tregs);
  358. } else {
  359. int shift = 15;
  360. (void) read_tcvr_bit(bp, tregs);
  361. (void) read_tcvr_bit(bp, tregs);
  362. do {
  363. int tmp;
  364. tmp = read_tcvr_bit(bp, tregs);
  365. retval |= ((tmp & 1) << shift);
  366. shift -= 1;
  367. } while (shift >= 0);
  368. (void) read_tcvr_bit(bp, tregs);
  369. (void) read_tcvr_bit(bp, tregs);
  370. (void) read_tcvr_bit(bp, tregs);
  371. }
  372. return retval;
  373. }
  374. static void bigmac_tcvr_init(struct bigmac *bp)
  375. {
  376. void __iomem *tregs = bp->tregs;
  377. u32 mpal;
  378. idle_transceiver(tregs);
  379. sbus_writel(MGMT_PAL_INT_MDIO | MGMT_PAL_EXT_MDIO | MGMT_PAL_DCLOCK,
  380. tregs + TCVR_MPAL);
  381. sbus_readl(tregs + TCVR_MPAL);
  382. /* Only the bit for the present transceiver (internal or
  383. * external) will stick, set them both and see what stays.
  384. */
  385. sbus_writel(MGMT_PAL_INT_MDIO | MGMT_PAL_EXT_MDIO, tregs + TCVR_MPAL);
  386. sbus_readl(tregs + TCVR_MPAL);
  387. udelay(20);
  388. mpal = sbus_readl(tregs + TCVR_MPAL);
  389. if (mpal & MGMT_PAL_EXT_MDIO) {
  390. bp->tcvr_type = external;
  391. sbus_writel(~(TCVR_PAL_EXTLBACK | TCVR_PAL_MSENSE | TCVR_PAL_LTENABLE),
  392. tregs + TCVR_TPAL);
  393. sbus_readl(tregs + TCVR_TPAL);
  394. } else if (mpal & MGMT_PAL_INT_MDIO) {
  395. bp->tcvr_type = internal;
  396. sbus_writel(~(TCVR_PAL_SERIAL | TCVR_PAL_EXTLBACK |
  397. TCVR_PAL_MSENSE | TCVR_PAL_LTENABLE),
  398. tregs + TCVR_TPAL);
  399. sbus_readl(tregs + TCVR_TPAL);
  400. } else {
  401. printk(KERN_ERR "BIGMAC: AIEEE, neither internal nor "
  402. "external MDIO available!\n");
  403. printk(KERN_ERR "BIGMAC: mgmt_pal[%08x] tcvr_pal[%08x]\n",
  404. sbus_readl(tregs + TCVR_MPAL),
  405. sbus_readl(tregs + TCVR_TPAL));
  406. }
  407. }
  408. static int bigmac_init(struct bigmac *, int);
  409. static int try_next_permutation(struct bigmac *bp, void __iomem *tregs)
  410. {
  411. if (bp->sw_bmcr & BMCR_SPEED100) {
  412. int timeout;
  413. /* Reset the PHY. */
  414. bp->sw_bmcr = (BMCR_ISOLATE | BMCR_PDOWN | BMCR_LOOPBACK);
  415. bigmac_tcvr_write(bp, tregs, BIGMAC_BMCR, bp->sw_bmcr);
  416. bp->sw_bmcr = (BMCR_RESET);
  417. bigmac_tcvr_write(bp, tregs, BIGMAC_BMCR, bp->sw_bmcr);
  418. timeout = 64;
  419. while (--timeout) {
  420. bp->sw_bmcr = bigmac_tcvr_read(bp, tregs, BIGMAC_BMCR);
  421. if ((bp->sw_bmcr & BMCR_RESET) == 0)
  422. break;
  423. udelay(20);
  424. }
  425. if (timeout == 0)
  426. printk(KERN_ERR "%s: PHY reset failed.\n", bp->dev->name);
  427. bp->sw_bmcr = bigmac_tcvr_read(bp, tregs, BIGMAC_BMCR);
  428. /* Now we try 10baseT. */
  429. bp->sw_bmcr &= ~(BMCR_SPEED100);
  430. bigmac_tcvr_write(bp, tregs, BIGMAC_BMCR, bp->sw_bmcr);
  431. return 0;
  432. }
  433. /* We've tried them all. */
  434. return -1;
  435. }
  436. static void bigmac_timer(unsigned long data)
  437. {
  438. struct bigmac *bp = (struct bigmac *) data;
  439. void __iomem *tregs = bp->tregs;
  440. int restart_timer = 0;
  441. bp->timer_ticks++;
  442. if (bp->timer_state == ltrywait) {
  443. bp->sw_bmsr = bigmac_tcvr_read(bp, tregs, BIGMAC_BMSR);
  444. bp->sw_bmcr = bigmac_tcvr_read(bp, tregs, BIGMAC_BMCR);
  445. if (bp->sw_bmsr & BMSR_LSTATUS) {
  446. printk(KERN_INFO "%s: Link is now up at %s.\n",
  447. bp->dev->name,
  448. (bp->sw_bmcr & BMCR_SPEED100) ?
  449. "100baseT" : "10baseT");
  450. bp->timer_state = asleep;
  451. restart_timer = 0;
  452. } else {
  453. if (bp->timer_ticks >= 4) {
  454. int ret;
  455. ret = try_next_permutation(bp, tregs);
  456. if (ret == -1) {
  457. printk(KERN_ERR "%s: Link down, cable problem?\n",
  458. bp->dev->name);
  459. ret = bigmac_init(bp, 0);
  460. if (ret) {
  461. printk(KERN_ERR "%s: Error, cannot re-init the "
  462. "BigMAC.\n", bp->dev->name);
  463. }
  464. return;
  465. }
  466. bp->timer_ticks = 0;
  467. restart_timer = 1;
  468. } else {
  469. restart_timer = 1;
  470. }
  471. }
  472. } else {
  473. /* Can't happens.... */
  474. printk(KERN_ERR "%s: Aieee, link timer is asleep but we got one anyways!\n",
  475. bp->dev->name);
  476. restart_timer = 0;
  477. bp->timer_ticks = 0;
  478. bp->timer_state = asleep; /* foo on you */
  479. }
  480. if (restart_timer != 0) {
  481. bp->bigmac_timer.expires = jiffies + ((12 * HZ)/10); /* 1.2 sec. */
  482. add_timer(&bp->bigmac_timer);
  483. }
  484. }
  485. /* Well, really we just force the chip into 100baseT then
  486. * 10baseT, each time checking for a link status.
  487. */
  488. static void bigmac_begin_auto_negotiation(struct bigmac *bp)
  489. {
  490. void __iomem *tregs = bp->tregs;
  491. int timeout;
  492. /* Grab new software copies of PHY registers. */
  493. bp->sw_bmsr = bigmac_tcvr_read(bp, tregs, BIGMAC_BMSR);
  494. bp->sw_bmcr = bigmac_tcvr_read(bp, tregs, BIGMAC_BMCR);
  495. /* Reset the PHY. */
  496. bp->sw_bmcr = (BMCR_ISOLATE | BMCR_PDOWN | BMCR_LOOPBACK);
  497. bigmac_tcvr_write(bp, tregs, BIGMAC_BMCR, bp->sw_bmcr);
  498. bp->sw_bmcr = (BMCR_RESET);
  499. bigmac_tcvr_write(bp, tregs, BIGMAC_BMCR, bp->sw_bmcr);
  500. timeout = 64;
  501. while (--timeout) {
  502. bp->sw_bmcr = bigmac_tcvr_read(bp, tregs, BIGMAC_BMCR);
  503. if ((bp->sw_bmcr & BMCR_RESET) == 0)
  504. break;
  505. udelay(20);
  506. }
  507. if (timeout == 0)
  508. printk(KERN_ERR "%s: PHY reset failed.\n", bp->dev->name);
  509. bp->sw_bmcr = bigmac_tcvr_read(bp, tregs, BIGMAC_BMCR);
  510. /* First we try 100baseT. */
  511. bp->sw_bmcr |= BMCR_SPEED100;
  512. bigmac_tcvr_write(bp, tregs, BIGMAC_BMCR, bp->sw_bmcr);
  513. bp->timer_state = ltrywait;
  514. bp->timer_ticks = 0;
  515. bp->bigmac_timer.expires = jiffies + (12 * HZ) / 10;
  516. bp->bigmac_timer.data = (unsigned long) bp;
  517. bp->bigmac_timer.function = &bigmac_timer;
  518. add_timer(&bp->bigmac_timer);
  519. }
  520. static int bigmac_init(struct bigmac *bp, int from_irq)
  521. {
  522. void __iomem *gregs = bp->gregs;
  523. void __iomem *cregs = bp->creg;
  524. void __iomem *bregs = bp->bregs;
  525. unsigned char *e = &bp->dev->dev_addr[0];
  526. /* Latch current counters into statistics. */
  527. bigmac_get_counters(bp, bregs);
  528. /* Reset QEC. */
  529. qec_global_reset(gregs);
  530. /* Init QEC. */
  531. qec_init(bp);
  532. /* Alloc and reset the tx/rx descriptor chains. */
  533. bigmac_init_rings(bp, from_irq);
  534. /* Initialize the PHY. */
  535. bigmac_tcvr_init(bp);
  536. /* Stop transmitter and receiver. */
  537. bigmac_stop(bp);
  538. /* Set hardware ethernet address. */
  539. sbus_writel(((e[4] << 8) | e[5]), bregs + BMAC_MACADDR2);
  540. sbus_writel(((e[2] << 8) | e[3]), bregs + BMAC_MACADDR1);
  541. sbus_writel(((e[0] << 8) | e[1]), bregs + BMAC_MACADDR0);
  542. /* Clear the hash table until mc upload occurs. */
  543. sbus_writel(0, bregs + BMAC_HTABLE3);
  544. sbus_writel(0, bregs + BMAC_HTABLE2);
  545. sbus_writel(0, bregs + BMAC_HTABLE1);
  546. sbus_writel(0, bregs + BMAC_HTABLE0);
  547. /* Enable Big Mac hash table filter. */
  548. sbus_writel(BIGMAC_RXCFG_HENABLE | BIGMAC_RXCFG_FIFO,
  549. bregs + BMAC_RXCFG);
  550. udelay(20);
  551. /* Ok, configure the Big Mac transmitter. */
  552. sbus_writel(BIGMAC_TXCFG_FIFO, bregs + BMAC_TXCFG);
  553. /* The HME docs recommend to use the 10LSB of our MAC here. */
  554. sbus_writel(((e[5] | e[4] << 8) & 0x3ff),
  555. bregs + BMAC_RSEED);
  556. /* Enable the output drivers no matter what. */
  557. sbus_writel(BIGMAC_XCFG_ODENABLE | BIGMAC_XCFG_RESV,
  558. bregs + BMAC_XIFCFG);
  559. /* Tell the QEC where the ring descriptors are. */
  560. sbus_writel(bp->bblock_dvma + bib_offset(be_rxd, 0),
  561. cregs + CREG_RXDS);
  562. sbus_writel(bp->bblock_dvma + bib_offset(be_txd, 0),
  563. cregs + CREG_TXDS);
  564. /* Setup the FIFO pointers into QEC local memory. */
  565. sbus_writel(0, cregs + CREG_RXRBUFPTR);
  566. sbus_writel(0, cregs + CREG_RXWBUFPTR);
  567. sbus_writel(sbus_readl(gregs + GLOB_RSIZE),
  568. cregs + CREG_TXRBUFPTR);
  569. sbus_writel(sbus_readl(gregs + GLOB_RSIZE),
  570. cregs + CREG_TXWBUFPTR);
  571. /* Tell bigmac what interrupts we don't want to hear about. */
  572. sbus_writel(BIGMAC_IMASK_GOTFRAME | BIGMAC_IMASK_SENTFRAME,
  573. bregs + BMAC_IMASK);
  574. /* Enable the various other irq's. */
  575. sbus_writel(0, cregs + CREG_RIMASK);
  576. sbus_writel(0, cregs + CREG_TIMASK);
  577. sbus_writel(0, cregs + CREG_QMASK);
  578. sbus_writel(0, cregs + CREG_BMASK);
  579. /* Set jam size to a reasonable default. */
  580. sbus_writel(DEFAULT_JAMSIZE, bregs + BMAC_JSIZE);
  581. /* Clear collision counter. */
  582. sbus_writel(0, cregs + CREG_CCNT);
  583. /* Enable transmitter and receiver. */
  584. sbus_writel(sbus_readl(bregs + BMAC_TXCFG) | BIGMAC_TXCFG_ENABLE,
  585. bregs + BMAC_TXCFG);
  586. sbus_writel(sbus_readl(bregs + BMAC_RXCFG) | BIGMAC_RXCFG_ENABLE,
  587. bregs + BMAC_RXCFG);
  588. /* Ok, start detecting link speed/duplex. */
  589. bigmac_begin_auto_negotiation(bp);
  590. /* Success. */
  591. return 0;
  592. }
  593. /* Error interrupts get sent here. */
  594. static void bigmac_is_medium_rare(struct bigmac *bp, u32 qec_status, u32 bmac_status)
  595. {
  596. printk(KERN_ERR "bigmac_is_medium_rare: ");
  597. if (qec_status & (GLOB_STAT_ER | GLOB_STAT_BM)) {
  598. if (qec_status & GLOB_STAT_ER)
  599. printk("QEC_ERROR, ");
  600. if (qec_status & GLOB_STAT_BM)
  601. printk("QEC_BMAC_ERROR, ");
  602. }
  603. if (bmac_status & CREG_STAT_ERRORS) {
  604. if (bmac_status & CREG_STAT_BERROR)
  605. printk("BMAC_ERROR, ");
  606. if (bmac_status & CREG_STAT_TXDERROR)
  607. printk("TXD_ERROR, ");
  608. if (bmac_status & CREG_STAT_TXLERR)
  609. printk("TX_LATE_ERROR, ");
  610. if (bmac_status & CREG_STAT_TXPERR)
  611. printk("TX_PARITY_ERROR, ");
  612. if (bmac_status & CREG_STAT_TXSERR)
  613. printk("TX_SBUS_ERROR, ");
  614. if (bmac_status & CREG_STAT_RXDROP)
  615. printk("RX_DROP_ERROR, ");
  616. if (bmac_status & CREG_STAT_RXSMALL)
  617. printk("RX_SMALL_ERROR, ");
  618. if (bmac_status & CREG_STAT_RXLERR)
  619. printk("RX_LATE_ERROR, ");
  620. if (bmac_status & CREG_STAT_RXPERR)
  621. printk("RX_PARITY_ERROR, ");
  622. if (bmac_status & CREG_STAT_RXSERR)
  623. printk("RX_SBUS_ERROR, ");
  624. }
  625. printk(" RESET\n");
  626. bigmac_init(bp, 1);
  627. }
  628. /* BigMAC transmit complete service routines. */
  629. static void bigmac_tx(struct bigmac *bp)
  630. {
  631. struct be_txd *txbase = &bp->bmac_block->be_txd[0];
  632. struct net_device *dev = bp->dev;
  633. int elem;
  634. spin_lock(&bp->lock);
  635. elem = bp->tx_old;
  636. DTX(("bigmac_tx: tx_old[%d] ", elem));
  637. while (elem != bp->tx_new) {
  638. struct sk_buff *skb;
  639. struct be_txd *this = &txbase[elem];
  640. DTX(("this(%p) [flags(%08x)addr(%08x)]",
  641. this, this->tx_flags, this->tx_addr));
  642. if (this->tx_flags & TXD_OWN)
  643. break;
  644. skb = bp->tx_skbs[elem];
  645. bp->enet_stats.tx_packets++;
  646. bp->enet_stats.tx_bytes += skb->len;
  647. sbus_unmap_single(bp->bigmac_sdev,
  648. this->tx_addr, skb->len,
  649. SBUS_DMA_TODEVICE);
  650. DTX(("skb(%p) ", skb));
  651. bp->tx_skbs[elem] = NULL;
  652. dev_kfree_skb_irq(skb);
  653. elem = NEXT_TX(elem);
  654. }
  655. DTX((" DONE, tx_old=%d\n", elem));
  656. bp->tx_old = elem;
  657. if (netif_queue_stopped(dev) &&
  658. TX_BUFFS_AVAIL(bp) > 0)
  659. netif_wake_queue(bp->dev);
  660. spin_unlock(&bp->lock);
  661. }
  662. /* BigMAC receive complete service routines. */
  663. static void bigmac_rx(struct bigmac *bp)
  664. {
  665. struct be_rxd *rxbase = &bp->bmac_block->be_rxd[0];
  666. struct be_rxd *this;
  667. int elem = bp->rx_new, drops = 0;
  668. u32 flags;
  669. this = &rxbase[elem];
  670. while (!((flags = this->rx_flags) & RXD_OWN)) {
  671. struct sk_buff *skb;
  672. int len = (flags & RXD_LENGTH); /* FCS not included */
  673. /* Check for errors. */
  674. if (len < ETH_ZLEN) {
  675. bp->enet_stats.rx_errors++;
  676. bp->enet_stats.rx_length_errors++;
  677. drop_it:
  678. /* Return it to the BigMAC. */
  679. bp->enet_stats.rx_dropped++;
  680. this->rx_flags =
  681. (RXD_OWN | ((RX_BUF_ALLOC_SIZE - 34) & RXD_LENGTH));
  682. goto next;
  683. }
  684. skb = bp->rx_skbs[elem];
  685. if (len > RX_COPY_THRESHOLD) {
  686. struct sk_buff *new_skb;
  687. /* Now refill the entry, if we can. */
  688. new_skb = big_mac_alloc_skb(RX_BUF_ALLOC_SIZE, GFP_ATOMIC);
  689. if (new_skb == NULL) {
  690. drops++;
  691. goto drop_it;
  692. }
  693. sbus_unmap_single(bp->bigmac_sdev,
  694. this->rx_addr,
  695. RX_BUF_ALLOC_SIZE - 34,
  696. SBUS_DMA_FROMDEVICE);
  697. bp->rx_skbs[elem] = new_skb;
  698. new_skb->dev = bp->dev;
  699. skb_put(new_skb, ETH_FRAME_LEN);
  700. skb_reserve(new_skb, 34);
  701. this->rx_addr = sbus_map_single(bp->bigmac_sdev,
  702. new_skb->data,
  703. RX_BUF_ALLOC_SIZE - 34,
  704. SBUS_DMA_FROMDEVICE);
  705. this->rx_flags =
  706. (RXD_OWN | ((RX_BUF_ALLOC_SIZE - 34) & RXD_LENGTH));
  707. /* Trim the original skb for the netif. */
  708. skb_trim(skb, len);
  709. } else {
  710. struct sk_buff *copy_skb = dev_alloc_skb(len + 2);
  711. if (copy_skb == NULL) {
  712. drops++;
  713. goto drop_it;
  714. }
  715. copy_skb->dev = bp->dev;
  716. skb_reserve(copy_skb, 2);
  717. skb_put(copy_skb, len);
  718. sbus_dma_sync_single_for_cpu(bp->bigmac_sdev,
  719. this->rx_addr, len,
  720. SBUS_DMA_FROMDEVICE);
  721. eth_copy_and_sum(copy_skb, (unsigned char *)skb->data, len, 0);
  722. sbus_dma_sync_single_for_device(bp->bigmac_sdev,
  723. this->rx_addr, len,
  724. SBUS_DMA_FROMDEVICE);
  725. /* Reuse original ring buffer. */
  726. this->rx_flags =
  727. (RXD_OWN | ((RX_BUF_ALLOC_SIZE - 34) & RXD_LENGTH));
  728. skb = copy_skb;
  729. }
  730. /* No checksums done by the BigMAC ;-( */
  731. skb->protocol = eth_type_trans(skb, bp->dev);
  732. netif_rx(skb);
  733. bp->dev->last_rx = jiffies;
  734. bp->enet_stats.rx_packets++;
  735. bp->enet_stats.rx_bytes += len;
  736. next:
  737. elem = NEXT_RX(elem);
  738. this = &rxbase[elem];
  739. }
  740. bp->rx_new = elem;
  741. if (drops)
  742. printk(KERN_NOTICE "%s: Memory squeeze, deferring packet.\n", bp->dev->name);
  743. }
  744. static irqreturn_t bigmac_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  745. {
  746. struct bigmac *bp = (struct bigmac *) dev_id;
  747. u32 qec_status, bmac_status;
  748. DIRQ(("bigmac_interrupt: "));
  749. /* Latch status registers now. */
  750. bmac_status = sbus_readl(bp->creg + CREG_STAT);
  751. qec_status = sbus_readl(bp->gregs + GLOB_STAT);
  752. DIRQ(("qec_status=%08x bmac_status=%08x\n", qec_status, bmac_status));
  753. if ((qec_status & (GLOB_STAT_ER | GLOB_STAT_BM)) ||
  754. (bmac_status & CREG_STAT_ERRORS))
  755. bigmac_is_medium_rare(bp, qec_status, bmac_status);
  756. if (bmac_status & CREG_STAT_TXIRQ)
  757. bigmac_tx(bp);
  758. if (bmac_status & CREG_STAT_RXIRQ)
  759. bigmac_rx(bp);
  760. return IRQ_HANDLED;
  761. }
  762. static int bigmac_open(struct net_device *dev)
  763. {
  764. struct bigmac *bp = (struct bigmac *) dev->priv;
  765. int ret;
  766. ret = request_irq(dev->irq, &bigmac_interrupt, SA_SHIRQ, dev->name, bp);
  767. if (ret) {
  768. printk(KERN_ERR "BIGMAC: Can't order irq %d to go.\n", dev->irq);
  769. return ret;
  770. }
  771. init_timer(&bp->bigmac_timer);
  772. ret = bigmac_init(bp, 0);
  773. if (ret)
  774. free_irq(dev->irq, bp);
  775. return ret;
  776. }
  777. static int bigmac_close(struct net_device *dev)
  778. {
  779. struct bigmac *bp = (struct bigmac *) dev->priv;
  780. del_timer(&bp->bigmac_timer);
  781. bp->timer_state = asleep;
  782. bp->timer_ticks = 0;
  783. bigmac_stop(bp);
  784. bigmac_clean_rings(bp);
  785. free_irq(dev->irq, bp);
  786. return 0;
  787. }
  788. static void bigmac_tx_timeout(struct net_device *dev)
  789. {
  790. struct bigmac *bp = (struct bigmac *) dev->priv;
  791. bigmac_init(bp, 0);
  792. netif_wake_queue(dev);
  793. }
  794. /* Put a packet on the wire. */
  795. static int bigmac_start_xmit(struct sk_buff *skb, struct net_device *dev)
  796. {
  797. struct bigmac *bp = (struct bigmac *) dev->priv;
  798. int len, entry;
  799. u32 mapping;
  800. len = skb->len;
  801. mapping = sbus_map_single(bp->bigmac_sdev, skb->data, len, SBUS_DMA_TODEVICE);
  802. /* Avoid a race... */
  803. spin_lock_irq(&bp->lock);
  804. entry = bp->tx_new;
  805. DTX(("bigmac_start_xmit: len(%d) entry(%d)\n", len, entry));
  806. bp->bmac_block->be_txd[entry].tx_flags = TXD_UPDATE;
  807. bp->tx_skbs[entry] = skb;
  808. bp->bmac_block->be_txd[entry].tx_addr = mapping;
  809. bp->bmac_block->be_txd[entry].tx_flags =
  810. (TXD_OWN | TXD_SOP | TXD_EOP | (len & TXD_LENGTH));
  811. bp->tx_new = NEXT_TX(entry);
  812. if (TX_BUFFS_AVAIL(bp) <= 0)
  813. netif_stop_queue(dev);
  814. spin_unlock_irq(&bp->lock);
  815. /* Get it going. */
  816. sbus_writel(CREG_CTRL_TWAKEUP, bp->creg + CREG_CTRL);
  817. dev->trans_start = jiffies;
  818. return 0;
  819. }
  820. static struct net_device_stats *bigmac_get_stats(struct net_device *dev)
  821. {
  822. struct bigmac *bp = (struct bigmac *) dev->priv;
  823. bigmac_get_counters(bp, bp->bregs);
  824. return &bp->enet_stats;
  825. }
  826. static void bigmac_set_multicast(struct net_device *dev)
  827. {
  828. struct bigmac *bp = (struct bigmac *) dev->priv;
  829. void __iomem *bregs = bp->bregs;
  830. struct dev_mc_list *dmi = dev->mc_list;
  831. char *addrs;
  832. int i;
  833. u32 tmp, crc;
  834. /* Disable the receiver. The bit self-clears when
  835. * the operation is complete.
  836. */
  837. tmp = sbus_readl(bregs + BMAC_RXCFG);
  838. tmp &= ~(BIGMAC_RXCFG_ENABLE);
  839. sbus_writel(tmp, bregs + BMAC_RXCFG);
  840. while ((sbus_readl(bregs + BMAC_RXCFG) & BIGMAC_RXCFG_ENABLE) != 0)
  841. udelay(20);
  842. if ((dev->flags & IFF_ALLMULTI) || (dev->mc_count > 64)) {
  843. sbus_writel(0xffff, bregs + BMAC_HTABLE0);
  844. sbus_writel(0xffff, bregs + BMAC_HTABLE1);
  845. sbus_writel(0xffff, bregs + BMAC_HTABLE2);
  846. sbus_writel(0xffff, bregs + BMAC_HTABLE3);
  847. } else if (dev->flags & IFF_PROMISC) {
  848. tmp = sbus_readl(bregs + BMAC_RXCFG);
  849. tmp |= BIGMAC_RXCFG_PMISC;
  850. sbus_writel(tmp, bregs + BMAC_RXCFG);
  851. } else {
  852. u16 hash_table[4];
  853. for (i = 0; i < 4; i++)
  854. hash_table[i] = 0;
  855. for (i = 0; i < dev->mc_count; i++) {
  856. addrs = dmi->dmi_addr;
  857. dmi = dmi->next;
  858. if (!(*addrs & 1))
  859. continue;
  860. crc = ether_crc_le(6, addrs);
  861. crc >>= 26;
  862. hash_table[crc >> 4] |= 1 << (crc & 0xf);
  863. }
  864. sbus_writel(hash_table[0], bregs + BMAC_HTABLE0);
  865. sbus_writel(hash_table[1], bregs + BMAC_HTABLE1);
  866. sbus_writel(hash_table[2], bregs + BMAC_HTABLE2);
  867. sbus_writel(hash_table[3], bregs + BMAC_HTABLE3);
  868. }
  869. /* Re-enable the receiver. */
  870. tmp = sbus_readl(bregs + BMAC_RXCFG);
  871. tmp |= BIGMAC_RXCFG_ENABLE;
  872. sbus_writel(tmp, bregs + BMAC_RXCFG);
  873. }
  874. /* Ethtool support... */
  875. static void bigmac_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  876. {
  877. struct bigmac *bp = dev->priv;
  878. strcpy(info->driver, "sunbmac");
  879. strcpy(info->version, "2.0");
  880. sprintf(info->bus_info, "SBUS:%d",
  881. bp->qec_sdev->slot);
  882. }
  883. static u32 bigmac_get_link(struct net_device *dev)
  884. {
  885. struct bigmac *bp = dev->priv;
  886. spin_lock_irq(&bp->lock);
  887. bp->sw_bmsr = bigmac_tcvr_read(bp, bp->tregs, BIGMAC_BMSR);
  888. spin_unlock_irq(&bp->lock);
  889. return (bp->sw_bmsr & BMSR_LSTATUS);
  890. }
  891. static struct ethtool_ops bigmac_ethtool_ops = {
  892. .get_drvinfo = bigmac_get_drvinfo,
  893. .get_link = bigmac_get_link,
  894. };
  895. static int __init bigmac_ether_init(struct sbus_dev *qec_sdev)
  896. {
  897. struct net_device *dev;
  898. static int version_printed;
  899. struct bigmac *bp;
  900. u8 bsizes, bsizes_more;
  901. int i;
  902. /* Get a new device struct for this interface. */
  903. dev = alloc_etherdev(sizeof(struct bigmac));
  904. if (!dev)
  905. return -ENOMEM;
  906. SET_MODULE_OWNER(dev);
  907. if (version_printed++ == 0)
  908. printk(KERN_INFO "%s", version);
  909. dev->base_addr = (long) qec_sdev;
  910. for (i = 0; i < 6; i++)
  911. dev->dev_addr[i] = idprom->id_ethaddr[i];
  912. /* Setup softc, with backpointers to QEC and BigMAC SBUS device structs. */
  913. bp = dev->priv;
  914. bp->qec_sdev = qec_sdev;
  915. bp->bigmac_sdev = qec_sdev->child;
  916. spin_lock_init(&bp->lock);
  917. /* Verify the registers we expect, are actually there. */
  918. if ((bp->bigmac_sdev->num_registers != 3) ||
  919. (bp->qec_sdev->num_registers != 2)) {
  920. printk(KERN_ERR "BIGMAC: Device does not have 2 and 3 regs, it has %d and %d.\n",
  921. bp->qec_sdev->num_registers,
  922. bp->bigmac_sdev->num_registers);
  923. printk(KERN_ERR "BIGMAC: Would you like that for here or to go?\n");
  924. goto fail_and_cleanup;
  925. }
  926. /* Map in QEC global control registers. */
  927. bp->gregs = sbus_ioremap(&bp->qec_sdev->resource[0], 0,
  928. GLOB_REG_SIZE, "BigMAC QEC GLobal Regs");
  929. if (!bp->gregs) {
  930. printk(KERN_ERR "BIGMAC: Cannot map QEC global registers.\n");
  931. goto fail_and_cleanup;
  932. }
  933. /* Make sure QEC is in BigMAC mode. */
  934. if ((sbus_readl(bp->gregs + GLOB_CTRL) & 0xf0000000) != GLOB_CTRL_BMODE) {
  935. printk(KERN_ERR "BigMAC: AIEEE, QEC is not in BigMAC mode!\n");
  936. goto fail_and_cleanup;
  937. }
  938. /* Reset the QEC. */
  939. if (qec_global_reset(bp->gregs))
  940. goto fail_and_cleanup;
  941. /* Get supported SBUS burst sizes. */
  942. bsizes = prom_getintdefault(bp->qec_sdev->prom_node,
  943. "burst-sizes",
  944. 0xff);
  945. bsizes_more = prom_getintdefault(bp->qec_sdev->bus->prom_node,
  946. "burst-sizes",
  947. 0xff);
  948. bsizes &= 0xff;
  949. if (bsizes_more != 0xff)
  950. bsizes &= bsizes_more;
  951. if (bsizes == 0xff || (bsizes & DMA_BURST16) == 0 ||
  952. (bsizes & DMA_BURST32) == 0)
  953. bsizes = (DMA_BURST32 - 1);
  954. bp->bigmac_bursts = bsizes;
  955. /* Perform QEC initialization. */
  956. qec_init(bp);
  957. /* Map in the BigMAC channel registers. */
  958. bp->creg = sbus_ioremap(&bp->bigmac_sdev->resource[0], 0,
  959. CREG_REG_SIZE, "BigMAC QEC Channel Regs");
  960. if (!bp->creg) {
  961. printk(KERN_ERR "BIGMAC: Cannot map QEC channel registers.\n");
  962. goto fail_and_cleanup;
  963. }
  964. /* Map in the BigMAC control registers. */
  965. bp->bregs = sbus_ioremap(&bp->bigmac_sdev->resource[1], 0,
  966. BMAC_REG_SIZE, "BigMAC Primary Regs");
  967. if (!bp->bregs) {
  968. printk(KERN_ERR "BIGMAC: Cannot map BigMAC primary registers.\n");
  969. goto fail_and_cleanup;
  970. }
  971. /* Map in the BigMAC transceiver registers, this is how you poke at
  972. * the BigMAC's PHY.
  973. */
  974. bp->tregs = sbus_ioremap(&bp->bigmac_sdev->resource[2], 0,
  975. TCVR_REG_SIZE, "BigMAC Transceiver Regs");
  976. if (!bp->tregs) {
  977. printk(KERN_ERR "BIGMAC: Cannot map BigMAC transceiver registers.\n");
  978. goto fail_and_cleanup;
  979. }
  980. /* Stop the BigMAC. */
  981. bigmac_stop(bp);
  982. /* Allocate transmit/receive descriptor DVMA block. */
  983. bp->bmac_block = sbus_alloc_consistent(bp->bigmac_sdev,
  984. PAGE_SIZE,
  985. &bp->bblock_dvma);
  986. if (bp->bmac_block == NULL || bp->bblock_dvma == 0) {
  987. printk(KERN_ERR "BIGMAC: Cannot allocate consistent DMA.\n");
  988. goto fail_and_cleanup;
  989. }
  990. /* Get the board revision of this BigMAC. */
  991. bp->board_rev = prom_getintdefault(bp->bigmac_sdev->prom_node,
  992. "board-version", 1);
  993. /* Init auto-negotiation timer state. */
  994. init_timer(&bp->bigmac_timer);
  995. bp->timer_state = asleep;
  996. bp->timer_ticks = 0;
  997. /* Backlink to generic net device struct. */
  998. bp->dev = dev;
  999. /* Set links to our BigMAC open and close routines. */
  1000. dev->open = &bigmac_open;
  1001. dev->stop = &bigmac_close;
  1002. dev->hard_start_xmit = &bigmac_start_xmit;
  1003. dev->ethtool_ops = &bigmac_ethtool_ops;
  1004. /* Set links to BigMAC statistic and multi-cast loading code. */
  1005. dev->get_stats = &bigmac_get_stats;
  1006. dev->set_multicast_list = &bigmac_set_multicast;
  1007. dev->tx_timeout = &bigmac_tx_timeout;
  1008. dev->watchdog_timeo = 5*HZ;
  1009. /* Finish net device registration. */
  1010. dev->irq = bp->bigmac_sdev->irqs[0];
  1011. dev->dma = 0;
  1012. if (register_netdev(dev)) {
  1013. printk(KERN_ERR "BIGMAC: Cannot register device.\n");
  1014. goto fail_and_cleanup;
  1015. }
  1016. /* Put us into the list of instances attached for later driver
  1017. * exit.
  1018. */
  1019. bp->next_module = root_bigmac_dev;
  1020. root_bigmac_dev = bp;
  1021. printk(KERN_INFO "%s: BigMAC 100baseT Ethernet ", dev->name);
  1022. for (i = 0; i < 6; i++)
  1023. printk("%2.2x%c", dev->dev_addr[i],
  1024. i == 5 ? ' ' : ':');
  1025. printk("\n");
  1026. return 0;
  1027. fail_and_cleanup:
  1028. /* Something went wrong, undo whatever we did so far. */
  1029. /* Free register mappings if any. */
  1030. if (bp->gregs)
  1031. sbus_iounmap(bp->gregs, GLOB_REG_SIZE);
  1032. if (bp->creg)
  1033. sbus_iounmap(bp->creg, CREG_REG_SIZE);
  1034. if (bp->bregs)
  1035. sbus_iounmap(bp->bregs, BMAC_REG_SIZE);
  1036. if (bp->tregs)
  1037. sbus_iounmap(bp->tregs, TCVR_REG_SIZE);
  1038. if (bp->bmac_block)
  1039. sbus_free_consistent(bp->bigmac_sdev,
  1040. PAGE_SIZE,
  1041. bp->bmac_block,
  1042. bp->bblock_dvma);
  1043. /* This also frees the co-located 'dev->priv' */
  1044. free_netdev(dev);
  1045. return -ENODEV;
  1046. }
  1047. /* QEC can be the parent of either QuadEthernet or
  1048. * a BigMAC. We want the latter.
  1049. */
  1050. static int __init bigmac_match(struct sbus_dev *sdev)
  1051. {
  1052. struct sbus_dev *child = sdev->child;
  1053. if (strcmp(sdev->prom_name, "qec") != 0)
  1054. return 0;
  1055. if (child == NULL)
  1056. return 0;
  1057. if (strcmp(child->prom_name, "be") != 0)
  1058. return 0;
  1059. return 1;
  1060. }
  1061. static int __init bigmac_probe(void)
  1062. {
  1063. struct sbus_bus *sbus;
  1064. struct sbus_dev *sdev = NULL;
  1065. static int called;
  1066. int cards = 0, v;
  1067. root_bigmac_dev = NULL;
  1068. if (called)
  1069. return -ENODEV;
  1070. called++;
  1071. for_each_sbus(sbus) {
  1072. for_each_sbusdev(sdev, sbus) {
  1073. if (bigmac_match(sdev)) {
  1074. cards++;
  1075. if ((v = bigmac_ether_init(sdev)))
  1076. return v;
  1077. }
  1078. }
  1079. }
  1080. if (!cards)
  1081. return -ENODEV;
  1082. return 0;
  1083. }
  1084. static void __exit bigmac_cleanup(void)
  1085. {
  1086. while (root_bigmac_dev) {
  1087. struct bigmac *bp = root_bigmac_dev;
  1088. struct bigmac *bp_nxt = root_bigmac_dev->next_module;
  1089. sbus_iounmap(bp->gregs, GLOB_REG_SIZE);
  1090. sbus_iounmap(bp->creg, CREG_REG_SIZE);
  1091. sbus_iounmap(bp->bregs, BMAC_REG_SIZE);
  1092. sbus_iounmap(bp->tregs, TCVR_REG_SIZE);
  1093. sbus_free_consistent(bp->bigmac_sdev,
  1094. PAGE_SIZE,
  1095. bp->bmac_block,
  1096. bp->bblock_dvma);
  1097. unregister_netdev(bp->dev);
  1098. free_netdev(bp->dev);
  1099. root_bigmac_dev = bp_nxt;
  1100. }
  1101. }
  1102. module_init(bigmac_probe);
  1103. module_exit(bigmac_cleanup);