sunbmac.c 33 KB

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