sunbmac.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309
  1. /* sunbmac.c: Driver for Sparc BigMAC 100baseT ethernet adapters.
  2. *
  3. * Copyright (C) 1997, 1998, 1999, 2003, 2008 David S. Miller (davem@davemloft.net)
  4. */
  5. #include <linux/module.h>
  6. #include <linux/kernel.h>
  7. #include <linux/types.h>
  8. #include <linux/fcntl.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/ioport.h>
  11. #include <linux/in.h>
  12. #include <linux/slab.h>
  13. #include <linux/string.h>
  14. #include <linux/delay.h>
  15. #include <linux/init.h>
  16. #include <linux/crc32.h>
  17. #include <linux/errno.h>
  18. #include <linux/ethtool.h>
  19. #include <linux/netdevice.h>
  20. #include <linux/etherdevice.h>
  21. #include <linux/skbuff.h>
  22. #include <linux/bitops.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/of.h>
  25. #include <linux/of_device.h>
  26. #include <asm/auxio.h>
  27. #include <asm/byteorder.h>
  28. #include <asm/dma.h>
  29. #include <asm/idprom.h>
  30. #include <asm/io.h>
  31. #include <asm/openprom.h>
  32. #include <asm/oplib.h>
  33. #include <asm/pgtable.h>
  34. #include <asm/system.h>
  35. #include "sunbmac.h"
  36. #define DRV_NAME "sunbmac"
  37. #define DRV_VERSION "2.1"
  38. #define DRV_RELDATE "August 26, 2008"
  39. #define DRV_AUTHOR "David S. Miller (davem@davemloft.net)"
  40. static char version[] =
  41. DRV_NAME ".c:v" DRV_VERSION " " DRV_RELDATE " " DRV_AUTHOR "\n";
  42. MODULE_VERSION(DRV_VERSION);
  43. MODULE_AUTHOR(DRV_AUTHOR);
  44. MODULE_DESCRIPTION("Sun BigMAC 100baseT ethernet driver");
  45. MODULE_LICENSE("GPL");
  46. #undef DEBUG_PROBE
  47. #undef DEBUG_TX
  48. #undef DEBUG_IRQ
  49. #ifdef DEBUG_PROBE
  50. #define DP(x) printk x
  51. #else
  52. #define DP(x)
  53. #endif
  54. #ifdef DEBUG_TX
  55. #define DTX(x) printk x
  56. #else
  57. #define DTX(x)
  58. #endif
  59. #ifdef DEBUG_IRQ
  60. #define DIRQ(x) printk x
  61. #else
  62. #define DIRQ(x)
  63. #endif
  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. struct of_device *qec_op = bp->qec_op;
  85. void __iomem *gregs = bp->gregs;
  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(resource_size(&qec_op->resource[1]),
  99. gregs + GLOB_MSIZE);
  100. /* Half to the transmitter, half to the receiver. */
  101. sbus_writel(resource_size(&qec_op->resource[1]) >> 1,
  102. gregs + GLOB_TSIZE);
  103. sbus_writel(resource_size(&qec_op->resource[1]) >> 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. dma_map_single(&bp->bigmac_op->dev,
  198. skb->data,
  199. RX_BUF_ALLOC_SIZE - 34,
  200. DMA_FROM_DEVICE);
  201. bb->be_rxd[i].rx_flags =
  202. (RXD_OWN | ((RX_BUF_ALLOC_SIZE - 34) & RXD_LENGTH));
  203. }
  204. for (i = 0; i < TX_RING_SIZE; i++)
  205. bb->be_txd[i].tx_flags = bb->be_txd[i].tx_addr = 0;
  206. }
  207. #define MGMT_CLKON (MGMT_PAL_INT_MDIO|MGMT_PAL_EXT_MDIO|MGMT_PAL_OENAB|MGMT_PAL_DCLOCK)
  208. #define MGMT_CLKOFF (MGMT_PAL_INT_MDIO|MGMT_PAL_EXT_MDIO|MGMT_PAL_OENAB)
  209. static void idle_transceiver(void __iomem *tregs)
  210. {
  211. int i = 20;
  212. while (i--) {
  213. sbus_writel(MGMT_CLKOFF, tregs + TCVR_MPAL);
  214. sbus_readl(tregs + TCVR_MPAL);
  215. sbus_writel(MGMT_CLKON, tregs + TCVR_MPAL);
  216. sbus_readl(tregs + TCVR_MPAL);
  217. }
  218. }
  219. static void write_tcvr_bit(struct bigmac *bp, void __iomem *tregs, int bit)
  220. {
  221. if (bp->tcvr_type == internal) {
  222. bit = (bit & 1) << 3;
  223. sbus_writel(bit | (MGMT_PAL_OENAB | MGMT_PAL_EXT_MDIO),
  224. tregs + TCVR_MPAL);
  225. sbus_readl(tregs + TCVR_MPAL);
  226. sbus_writel(bit | MGMT_PAL_OENAB | MGMT_PAL_EXT_MDIO | MGMT_PAL_DCLOCK,
  227. tregs + TCVR_MPAL);
  228. sbus_readl(tregs + TCVR_MPAL);
  229. } else if (bp->tcvr_type == external) {
  230. bit = (bit & 1) << 2;
  231. sbus_writel(bit | MGMT_PAL_INT_MDIO | MGMT_PAL_OENAB,
  232. tregs + TCVR_MPAL);
  233. sbus_readl(tregs + TCVR_MPAL);
  234. sbus_writel(bit | MGMT_PAL_INT_MDIO | MGMT_PAL_OENAB | MGMT_PAL_DCLOCK,
  235. tregs + TCVR_MPAL);
  236. sbus_readl(tregs + TCVR_MPAL);
  237. } else {
  238. printk(KERN_ERR "write_tcvr_bit: No transceiver type known!\n");
  239. }
  240. }
  241. static int read_tcvr_bit(struct bigmac *bp, void __iomem *tregs)
  242. {
  243. int retval = 0;
  244. if (bp->tcvr_type == internal) {
  245. sbus_writel(MGMT_PAL_EXT_MDIO, tregs + TCVR_MPAL);
  246. sbus_readl(tregs + TCVR_MPAL);
  247. sbus_writel(MGMT_PAL_EXT_MDIO | MGMT_PAL_DCLOCK,
  248. tregs + TCVR_MPAL);
  249. sbus_readl(tregs + TCVR_MPAL);
  250. retval = (sbus_readl(tregs + TCVR_MPAL) & MGMT_PAL_INT_MDIO) >> 3;
  251. } else if (bp->tcvr_type == external) {
  252. sbus_writel(MGMT_PAL_INT_MDIO, tregs + TCVR_MPAL);
  253. sbus_readl(tregs + TCVR_MPAL);
  254. sbus_writel(MGMT_PAL_INT_MDIO | MGMT_PAL_DCLOCK, tregs + TCVR_MPAL);
  255. sbus_readl(tregs + TCVR_MPAL);
  256. retval = (sbus_readl(tregs + TCVR_MPAL) & MGMT_PAL_EXT_MDIO) >> 2;
  257. } else {
  258. printk(KERN_ERR "read_tcvr_bit: No transceiver type known!\n");
  259. }
  260. return retval;
  261. }
  262. static int read_tcvr_bit2(struct bigmac *bp, void __iomem *tregs)
  263. {
  264. int retval = 0;
  265. if (bp->tcvr_type == internal) {
  266. sbus_writel(MGMT_PAL_EXT_MDIO, tregs + TCVR_MPAL);
  267. sbus_readl(tregs + TCVR_MPAL);
  268. retval = (sbus_readl(tregs + TCVR_MPAL) & MGMT_PAL_INT_MDIO) >> 3;
  269. sbus_writel(MGMT_PAL_EXT_MDIO | MGMT_PAL_DCLOCK, tregs + TCVR_MPAL);
  270. sbus_readl(tregs + TCVR_MPAL);
  271. } else if (bp->tcvr_type == external) {
  272. sbus_writel(MGMT_PAL_INT_MDIO, tregs + TCVR_MPAL);
  273. sbus_readl(tregs + TCVR_MPAL);
  274. retval = (sbus_readl(tregs + TCVR_MPAL) & MGMT_PAL_EXT_MDIO) >> 2;
  275. sbus_writel(MGMT_PAL_INT_MDIO | MGMT_PAL_DCLOCK, tregs + TCVR_MPAL);
  276. sbus_readl(tregs + TCVR_MPAL);
  277. } else {
  278. printk(KERN_ERR "read_tcvr_bit2: No transceiver type known!\n");
  279. }
  280. return retval;
  281. }
  282. static void put_tcvr_byte(struct bigmac *bp,
  283. void __iomem *tregs,
  284. unsigned int byte)
  285. {
  286. int shift = 4;
  287. do {
  288. write_tcvr_bit(bp, tregs, ((byte >> shift) & 1));
  289. shift -= 1;
  290. } while (shift >= 0);
  291. }
  292. static void bigmac_tcvr_write(struct bigmac *bp, void __iomem *tregs,
  293. int reg, unsigned short val)
  294. {
  295. int shift;
  296. reg &= 0xff;
  297. val &= 0xffff;
  298. switch(bp->tcvr_type) {
  299. case internal:
  300. case external:
  301. break;
  302. default:
  303. printk(KERN_ERR "bigmac_tcvr_read: Whoops, no known transceiver type.\n");
  304. return;
  305. };
  306. idle_transceiver(tregs);
  307. write_tcvr_bit(bp, tregs, 0);
  308. write_tcvr_bit(bp, tregs, 1);
  309. write_tcvr_bit(bp, tregs, 0);
  310. write_tcvr_bit(bp, tregs, 1);
  311. put_tcvr_byte(bp, tregs,
  312. ((bp->tcvr_type == internal) ?
  313. BIGMAC_PHY_INTERNAL : BIGMAC_PHY_EXTERNAL));
  314. put_tcvr_byte(bp, tregs, reg);
  315. write_tcvr_bit(bp, tregs, 1);
  316. write_tcvr_bit(bp, tregs, 0);
  317. shift = 15;
  318. do {
  319. write_tcvr_bit(bp, tregs, (val >> shift) & 1);
  320. shift -= 1;
  321. } while (shift >= 0);
  322. }
  323. static unsigned short bigmac_tcvr_read(struct bigmac *bp,
  324. void __iomem *tregs,
  325. int reg)
  326. {
  327. unsigned short retval = 0;
  328. reg &= 0xff;
  329. switch(bp->tcvr_type) {
  330. case internal:
  331. case external:
  332. break;
  333. default:
  334. printk(KERN_ERR "bigmac_tcvr_read: Whoops, no known transceiver type.\n");
  335. return 0xffff;
  336. };
  337. idle_transceiver(tregs);
  338. write_tcvr_bit(bp, tregs, 0);
  339. write_tcvr_bit(bp, tregs, 1);
  340. write_tcvr_bit(bp, tregs, 1);
  341. write_tcvr_bit(bp, tregs, 0);
  342. put_tcvr_byte(bp, tregs,
  343. ((bp->tcvr_type == internal) ?
  344. BIGMAC_PHY_INTERNAL : BIGMAC_PHY_EXTERNAL));
  345. put_tcvr_byte(bp, tregs, reg);
  346. if (bp->tcvr_type == external) {
  347. int shift = 15;
  348. (void) read_tcvr_bit2(bp, tregs);
  349. (void) read_tcvr_bit2(bp, tregs);
  350. do {
  351. int tmp;
  352. tmp = read_tcvr_bit2(bp, tregs);
  353. retval |= ((tmp & 1) << shift);
  354. shift -= 1;
  355. } while (shift >= 0);
  356. (void) read_tcvr_bit2(bp, tregs);
  357. (void) read_tcvr_bit2(bp, tregs);
  358. (void) read_tcvr_bit2(bp, tregs);
  359. } else {
  360. int shift = 15;
  361. (void) read_tcvr_bit(bp, tregs);
  362. (void) read_tcvr_bit(bp, tregs);
  363. do {
  364. int tmp;
  365. tmp = read_tcvr_bit(bp, tregs);
  366. retval |= ((tmp & 1) << shift);
  367. shift -= 1;
  368. } while (shift >= 0);
  369. (void) read_tcvr_bit(bp, tregs);
  370. (void) read_tcvr_bit(bp, tregs);
  371. (void) read_tcvr_bit(bp, tregs);
  372. }
  373. return retval;
  374. }
  375. static void bigmac_tcvr_init(struct bigmac *bp)
  376. {
  377. void __iomem *tregs = bp->tregs;
  378. u32 mpal;
  379. idle_transceiver(tregs);
  380. sbus_writel(MGMT_PAL_INT_MDIO | MGMT_PAL_EXT_MDIO | MGMT_PAL_DCLOCK,
  381. tregs + TCVR_MPAL);
  382. sbus_readl(tregs + TCVR_MPAL);
  383. /* Only the bit for the present transceiver (internal or
  384. * external) will stick, set them both and see what stays.
  385. */
  386. sbus_writel(MGMT_PAL_INT_MDIO | MGMT_PAL_EXT_MDIO, tregs + TCVR_MPAL);
  387. sbus_readl(tregs + TCVR_MPAL);
  388. udelay(20);
  389. mpal = sbus_readl(tregs + TCVR_MPAL);
  390. if (mpal & MGMT_PAL_EXT_MDIO) {
  391. bp->tcvr_type = external;
  392. sbus_writel(~(TCVR_PAL_EXTLBACK | TCVR_PAL_MSENSE | TCVR_PAL_LTENABLE),
  393. tregs + TCVR_TPAL);
  394. sbus_readl(tregs + TCVR_TPAL);
  395. } else if (mpal & MGMT_PAL_INT_MDIO) {
  396. bp->tcvr_type = internal;
  397. sbus_writel(~(TCVR_PAL_SERIAL | TCVR_PAL_EXTLBACK |
  398. TCVR_PAL_MSENSE | TCVR_PAL_LTENABLE),
  399. tregs + TCVR_TPAL);
  400. sbus_readl(tregs + TCVR_TPAL);
  401. } else {
  402. printk(KERN_ERR "BIGMAC: AIEEE, neither internal nor "
  403. "external MDIO available!\n");
  404. printk(KERN_ERR "BIGMAC: mgmt_pal[%08x] tcvr_pal[%08x]\n",
  405. sbus_readl(tregs + TCVR_MPAL),
  406. sbus_readl(tregs + TCVR_TPAL));
  407. }
  408. }
  409. static int bigmac_init_hw(struct bigmac *, int);
  410. static int try_next_permutation(struct bigmac *bp, void __iomem *tregs)
  411. {
  412. if (bp->sw_bmcr & BMCR_SPEED100) {
  413. int timeout;
  414. /* Reset the PHY. */
  415. bp->sw_bmcr = (BMCR_ISOLATE | BMCR_PDOWN | BMCR_LOOPBACK);
  416. bigmac_tcvr_write(bp, tregs, BIGMAC_BMCR, bp->sw_bmcr);
  417. bp->sw_bmcr = (BMCR_RESET);
  418. bigmac_tcvr_write(bp, tregs, BIGMAC_BMCR, bp->sw_bmcr);
  419. timeout = 64;
  420. while (--timeout) {
  421. bp->sw_bmcr = bigmac_tcvr_read(bp, tregs, BIGMAC_BMCR);
  422. if ((bp->sw_bmcr & BMCR_RESET) == 0)
  423. break;
  424. udelay(20);
  425. }
  426. if (timeout == 0)
  427. printk(KERN_ERR "%s: PHY reset failed.\n", bp->dev->name);
  428. bp->sw_bmcr = bigmac_tcvr_read(bp, tregs, BIGMAC_BMCR);
  429. /* Now we try 10baseT. */
  430. bp->sw_bmcr &= ~(BMCR_SPEED100);
  431. bigmac_tcvr_write(bp, tregs, BIGMAC_BMCR, bp->sw_bmcr);
  432. return 0;
  433. }
  434. /* We've tried them all. */
  435. return -1;
  436. }
  437. static void bigmac_timer(unsigned long data)
  438. {
  439. struct bigmac *bp = (struct bigmac *) data;
  440. void __iomem *tregs = bp->tregs;
  441. int restart_timer = 0;
  442. bp->timer_ticks++;
  443. if (bp->timer_state == ltrywait) {
  444. bp->sw_bmsr = bigmac_tcvr_read(bp, tregs, BIGMAC_BMSR);
  445. bp->sw_bmcr = bigmac_tcvr_read(bp, tregs, BIGMAC_BMCR);
  446. if (bp->sw_bmsr & BMSR_LSTATUS) {
  447. printk(KERN_INFO "%s: Link is now up at %s.\n",
  448. bp->dev->name,
  449. (bp->sw_bmcr & BMCR_SPEED100) ?
  450. "100baseT" : "10baseT");
  451. bp->timer_state = asleep;
  452. restart_timer = 0;
  453. } else {
  454. if (bp->timer_ticks >= 4) {
  455. int ret;
  456. ret = try_next_permutation(bp, tregs);
  457. if (ret == -1) {
  458. printk(KERN_ERR "%s: Link down, cable problem?\n",
  459. bp->dev->name);
  460. ret = bigmac_init_hw(bp, 0);
  461. if (ret) {
  462. printk(KERN_ERR "%s: Error, cannot re-init the "
  463. "BigMAC.\n", bp->dev->name);
  464. }
  465. return;
  466. }
  467. bp->timer_ticks = 0;
  468. restart_timer = 1;
  469. } else {
  470. restart_timer = 1;
  471. }
  472. }
  473. } else {
  474. /* Can't happens.... */
  475. printk(KERN_ERR "%s: Aieee, link timer is asleep but we got one anyways!\n",
  476. bp->dev->name);
  477. restart_timer = 0;
  478. bp->timer_ticks = 0;
  479. bp->timer_state = asleep; /* foo on you */
  480. }
  481. if (restart_timer != 0) {
  482. bp->bigmac_timer.expires = jiffies + ((12 * HZ)/10); /* 1.2 sec. */
  483. add_timer(&bp->bigmac_timer);
  484. }
  485. }
  486. /* Well, really we just force the chip into 100baseT then
  487. * 10baseT, each time checking for a link status.
  488. */
  489. static void bigmac_begin_auto_negotiation(struct bigmac *bp)
  490. {
  491. void __iomem *tregs = bp->tregs;
  492. int timeout;
  493. /* Grab new software copies of PHY registers. */
  494. bp->sw_bmsr = bigmac_tcvr_read(bp, tregs, BIGMAC_BMSR);
  495. bp->sw_bmcr = bigmac_tcvr_read(bp, tregs, BIGMAC_BMCR);
  496. /* Reset the PHY. */
  497. bp->sw_bmcr = (BMCR_ISOLATE | BMCR_PDOWN | BMCR_LOOPBACK);
  498. bigmac_tcvr_write(bp, tregs, BIGMAC_BMCR, bp->sw_bmcr);
  499. bp->sw_bmcr = (BMCR_RESET);
  500. bigmac_tcvr_write(bp, tregs, BIGMAC_BMCR, bp->sw_bmcr);
  501. timeout = 64;
  502. while (--timeout) {
  503. bp->sw_bmcr = bigmac_tcvr_read(bp, tregs, BIGMAC_BMCR);
  504. if ((bp->sw_bmcr & BMCR_RESET) == 0)
  505. break;
  506. udelay(20);
  507. }
  508. if (timeout == 0)
  509. printk(KERN_ERR "%s: PHY reset failed.\n", bp->dev->name);
  510. bp->sw_bmcr = bigmac_tcvr_read(bp, tregs, BIGMAC_BMCR);
  511. /* First we try 100baseT. */
  512. bp->sw_bmcr |= BMCR_SPEED100;
  513. bigmac_tcvr_write(bp, tregs, BIGMAC_BMCR, bp->sw_bmcr);
  514. bp->timer_state = ltrywait;
  515. bp->timer_ticks = 0;
  516. bp->bigmac_timer.expires = jiffies + (12 * HZ) / 10;
  517. bp->bigmac_timer.data = (unsigned long) bp;
  518. bp->bigmac_timer.function = &bigmac_timer;
  519. add_timer(&bp->bigmac_timer);
  520. }
  521. static int bigmac_init_hw(struct bigmac *bp, int from_irq)
  522. {
  523. void __iomem *gregs = bp->gregs;
  524. void __iomem *cregs = bp->creg;
  525. void __iomem *bregs = bp->bregs;
  526. unsigned char *e = &bp->dev->dev_addr[0];
  527. /* Latch current counters into statistics. */
  528. bigmac_get_counters(bp, bregs);
  529. /* Reset QEC. */
  530. qec_global_reset(gregs);
  531. /* Init QEC. */
  532. qec_init(bp);
  533. /* Alloc and reset the tx/rx descriptor chains. */
  534. bigmac_init_rings(bp, from_irq);
  535. /* Initialize the PHY. */
  536. bigmac_tcvr_init(bp);
  537. /* Stop transmitter and receiver. */
  538. bigmac_stop(bp);
  539. /* Set hardware ethernet address. */
  540. sbus_writel(((e[4] << 8) | e[5]), bregs + BMAC_MACADDR2);
  541. sbus_writel(((e[2] << 8) | e[3]), bregs + BMAC_MACADDR1);
  542. sbus_writel(((e[0] << 8) | e[1]), bregs + BMAC_MACADDR0);
  543. /* Clear the hash table until mc upload occurs. */
  544. sbus_writel(0, bregs + BMAC_HTABLE3);
  545. sbus_writel(0, bregs + BMAC_HTABLE2);
  546. sbus_writel(0, bregs + BMAC_HTABLE1);
  547. sbus_writel(0, bregs + BMAC_HTABLE0);
  548. /* Enable Big Mac hash table filter. */
  549. sbus_writel(BIGMAC_RXCFG_HENABLE | BIGMAC_RXCFG_FIFO,
  550. bregs + BMAC_RXCFG);
  551. udelay(20);
  552. /* Ok, configure the Big Mac transmitter. */
  553. sbus_writel(BIGMAC_TXCFG_FIFO, bregs + BMAC_TXCFG);
  554. /* The HME docs recommend to use the 10LSB of our MAC here. */
  555. sbus_writel(((e[5] | e[4] << 8) & 0x3ff),
  556. bregs + BMAC_RSEED);
  557. /* Enable the output drivers no matter what. */
  558. sbus_writel(BIGMAC_XCFG_ODENABLE | BIGMAC_XCFG_RESV,
  559. bregs + BMAC_XIFCFG);
  560. /* Tell the QEC where the ring descriptors are. */
  561. sbus_writel(bp->bblock_dvma + bib_offset(be_rxd, 0),
  562. cregs + CREG_RXDS);
  563. sbus_writel(bp->bblock_dvma + bib_offset(be_txd, 0),
  564. cregs + CREG_TXDS);
  565. /* Setup the FIFO pointers into QEC local memory. */
  566. sbus_writel(0, cregs + CREG_RXRBUFPTR);
  567. sbus_writel(0, cregs + CREG_RXWBUFPTR);
  568. sbus_writel(sbus_readl(gregs + GLOB_RSIZE),
  569. cregs + CREG_TXRBUFPTR);
  570. sbus_writel(sbus_readl(gregs + GLOB_RSIZE),
  571. cregs + CREG_TXWBUFPTR);
  572. /* Tell bigmac what interrupts we don't want to hear about. */
  573. sbus_writel(BIGMAC_IMASK_GOTFRAME | BIGMAC_IMASK_SENTFRAME,
  574. bregs + BMAC_IMASK);
  575. /* Enable the various other irq's. */
  576. sbus_writel(0, cregs + CREG_RIMASK);
  577. sbus_writel(0, cregs + CREG_TIMASK);
  578. sbus_writel(0, cregs + CREG_QMASK);
  579. sbus_writel(0, cregs + CREG_BMASK);
  580. /* Set jam size to a reasonable default. */
  581. sbus_writel(DEFAULT_JAMSIZE, bregs + BMAC_JSIZE);
  582. /* Clear collision counter. */
  583. sbus_writel(0, cregs + CREG_CCNT);
  584. /* Enable transmitter and receiver. */
  585. sbus_writel(sbus_readl(bregs + BMAC_TXCFG) | BIGMAC_TXCFG_ENABLE,
  586. bregs + BMAC_TXCFG);
  587. sbus_writel(sbus_readl(bregs + BMAC_RXCFG) | BIGMAC_RXCFG_ENABLE,
  588. bregs + BMAC_RXCFG);
  589. /* Ok, start detecting link speed/duplex. */
  590. bigmac_begin_auto_negotiation(bp);
  591. /* Success. */
  592. return 0;
  593. }
  594. /* Error interrupts get sent here. */
  595. static void bigmac_is_medium_rare(struct bigmac *bp, u32 qec_status, u32 bmac_status)
  596. {
  597. printk(KERN_ERR "bigmac_is_medium_rare: ");
  598. if (qec_status & (GLOB_STAT_ER | GLOB_STAT_BM)) {
  599. if (qec_status & GLOB_STAT_ER)
  600. printk("QEC_ERROR, ");
  601. if (qec_status & GLOB_STAT_BM)
  602. printk("QEC_BMAC_ERROR, ");
  603. }
  604. if (bmac_status & CREG_STAT_ERRORS) {
  605. if (bmac_status & CREG_STAT_BERROR)
  606. printk("BMAC_ERROR, ");
  607. if (bmac_status & CREG_STAT_TXDERROR)
  608. printk("TXD_ERROR, ");
  609. if (bmac_status & CREG_STAT_TXLERR)
  610. printk("TX_LATE_ERROR, ");
  611. if (bmac_status & CREG_STAT_TXPERR)
  612. printk("TX_PARITY_ERROR, ");
  613. if (bmac_status & CREG_STAT_TXSERR)
  614. printk("TX_SBUS_ERROR, ");
  615. if (bmac_status & CREG_STAT_RXDROP)
  616. printk("RX_DROP_ERROR, ");
  617. if (bmac_status & CREG_STAT_RXSMALL)
  618. printk("RX_SMALL_ERROR, ");
  619. if (bmac_status & CREG_STAT_RXLERR)
  620. printk("RX_LATE_ERROR, ");
  621. if (bmac_status & CREG_STAT_RXPERR)
  622. printk("RX_PARITY_ERROR, ");
  623. if (bmac_status & CREG_STAT_RXSERR)
  624. printk("RX_SBUS_ERROR, ");
  625. }
  626. printk(" RESET\n");
  627. bigmac_init_hw(bp, 1);
  628. }
  629. /* BigMAC transmit complete service routines. */
  630. static void bigmac_tx(struct bigmac *bp)
  631. {
  632. struct be_txd *txbase = &bp->bmac_block->be_txd[0];
  633. struct net_device *dev = bp->dev;
  634. int elem;
  635. spin_lock(&bp->lock);
  636. elem = bp->tx_old;
  637. DTX(("bigmac_tx: tx_old[%d] ", elem));
  638. while (elem != bp->tx_new) {
  639. struct sk_buff *skb;
  640. struct be_txd *this = &txbase[elem];
  641. DTX(("this(%p) [flags(%08x)addr(%08x)]",
  642. this, this->tx_flags, this->tx_addr));
  643. if (this->tx_flags & TXD_OWN)
  644. break;
  645. skb = bp->tx_skbs[elem];
  646. bp->enet_stats.tx_packets++;
  647. bp->enet_stats.tx_bytes += skb->len;
  648. dma_unmap_single(&bp->bigmac_op->dev,
  649. this->tx_addr, skb->len,
  650. DMA_TO_DEVICE);
  651. DTX(("skb(%p) ", skb));
  652. bp->tx_skbs[elem] = NULL;
  653. dev_kfree_skb_irq(skb);
  654. elem = NEXT_TX(elem);
  655. }
  656. DTX((" DONE, tx_old=%d\n", elem));
  657. bp->tx_old = elem;
  658. if (netif_queue_stopped(dev) &&
  659. TX_BUFFS_AVAIL(bp) > 0)
  660. netif_wake_queue(bp->dev);
  661. spin_unlock(&bp->lock);
  662. }
  663. /* BigMAC receive complete service routines. */
  664. static void bigmac_rx(struct bigmac *bp)
  665. {
  666. struct be_rxd *rxbase = &bp->bmac_block->be_rxd[0];
  667. struct be_rxd *this;
  668. int elem = bp->rx_new, drops = 0;
  669. u32 flags;
  670. this = &rxbase[elem];
  671. while (!((flags = this->rx_flags) & RXD_OWN)) {
  672. struct sk_buff *skb;
  673. int len = (flags & RXD_LENGTH); /* FCS not included */
  674. /* Check for errors. */
  675. if (len < ETH_ZLEN) {
  676. bp->enet_stats.rx_errors++;
  677. bp->enet_stats.rx_length_errors++;
  678. drop_it:
  679. /* Return it to the BigMAC. */
  680. bp->enet_stats.rx_dropped++;
  681. this->rx_flags =
  682. (RXD_OWN | ((RX_BUF_ALLOC_SIZE - 34) & RXD_LENGTH));
  683. goto next;
  684. }
  685. skb = bp->rx_skbs[elem];
  686. if (len > RX_COPY_THRESHOLD) {
  687. struct sk_buff *new_skb;
  688. /* Now refill the entry, if we can. */
  689. new_skb = big_mac_alloc_skb(RX_BUF_ALLOC_SIZE, GFP_ATOMIC);
  690. if (new_skb == NULL) {
  691. drops++;
  692. goto drop_it;
  693. }
  694. dma_unmap_single(&bp->bigmac_op->dev,
  695. this->rx_addr,
  696. RX_BUF_ALLOC_SIZE - 34,
  697. DMA_FROM_DEVICE);
  698. bp->rx_skbs[elem] = new_skb;
  699. new_skb->dev = bp->dev;
  700. skb_put(new_skb, ETH_FRAME_LEN);
  701. skb_reserve(new_skb, 34);
  702. this->rx_addr =
  703. dma_map_single(&bp->bigmac_op->dev,
  704. new_skb->data,
  705. RX_BUF_ALLOC_SIZE - 34,
  706. DMA_FROM_DEVICE);
  707. this->rx_flags =
  708. (RXD_OWN | ((RX_BUF_ALLOC_SIZE - 34) & RXD_LENGTH));
  709. /* Trim the original skb for the netif. */
  710. skb_trim(skb, len);
  711. } else {
  712. struct sk_buff *copy_skb = dev_alloc_skb(len + 2);
  713. if (copy_skb == NULL) {
  714. drops++;
  715. goto drop_it;
  716. }
  717. skb_reserve(copy_skb, 2);
  718. skb_put(copy_skb, len);
  719. dma_sync_single_for_cpu(&bp->bigmac_op->dev,
  720. this->rx_addr, len,
  721. DMA_FROM_DEVICE);
  722. skb_copy_to_linear_data(copy_skb, (unsigned char *)skb->data, len);
  723. dma_sync_single_for_device(&bp->bigmac_op->dev,
  724. this->rx_addr, len,
  725. DMA_FROM_DEVICE);
  726. /* Reuse original ring buffer. */
  727. this->rx_flags =
  728. (RXD_OWN | ((RX_BUF_ALLOC_SIZE - 34) & RXD_LENGTH));
  729. skb = copy_skb;
  730. }
  731. /* No checksums done by the BigMAC ;-( */
  732. skb->protocol = eth_type_trans(skb, bp->dev);
  733. netif_rx(skb);
  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)
  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 = netdev_priv(dev);
  765. int ret;
  766. ret = request_irq(dev->irq, &bigmac_interrupt, IRQF_SHARED, 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_hw(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 = netdev_priv(dev);
  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 = netdev_priv(dev);
  791. bigmac_init_hw(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 = netdev_priv(dev);
  798. int len, entry;
  799. u32 mapping;
  800. len = skb->len;
  801. mapping = dma_map_single(&bp->bigmac_op->dev, skb->data,
  802. len, DMA_TO_DEVICE);
  803. /* Avoid a race... */
  804. spin_lock_irq(&bp->lock);
  805. entry = bp->tx_new;
  806. DTX(("bigmac_start_xmit: len(%d) entry(%d)\n", len, entry));
  807. bp->bmac_block->be_txd[entry].tx_flags = TXD_UPDATE;
  808. bp->tx_skbs[entry] = skb;
  809. bp->bmac_block->be_txd[entry].tx_addr = mapping;
  810. bp->bmac_block->be_txd[entry].tx_flags =
  811. (TXD_OWN | TXD_SOP | TXD_EOP | (len & TXD_LENGTH));
  812. bp->tx_new = NEXT_TX(entry);
  813. if (TX_BUFFS_AVAIL(bp) <= 0)
  814. netif_stop_queue(dev);
  815. spin_unlock_irq(&bp->lock);
  816. /* Get it going. */
  817. sbus_writel(CREG_CTRL_TWAKEUP, bp->creg + CREG_CTRL);
  818. dev->trans_start = jiffies;
  819. return 0;
  820. }
  821. static struct net_device_stats *bigmac_get_stats(struct net_device *dev)
  822. {
  823. struct bigmac *bp = netdev_priv(dev);
  824. bigmac_get_counters(bp, bp->bregs);
  825. return &bp->enet_stats;
  826. }
  827. static void bigmac_set_multicast(struct net_device *dev)
  828. {
  829. struct bigmac *bp = netdev_priv(dev);
  830. void __iomem *bregs = bp->bregs;
  831. struct dev_mc_list *dmi = dev->mc_list;
  832. char *addrs;
  833. int i;
  834. u32 tmp, crc;
  835. /* Disable the receiver. The bit self-clears when
  836. * the operation is complete.
  837. */
  838. tmp = sbus_readl(bregs + BMAC_RXCFG);
  839. tmp &= ~(BIGMAC_RXCFG_ENABLE);
  840. sbus_writel(tmp, bregs + BMAC_RXCFG);
  841. while ((sbus_readl(bregs + BMAC_RXCFG) & BIGMAC_RXCFG_ENABLE) != 0)
  842. udelay(20);
  843. if ((dev->flags & IFF_ALLMULTI) || (dev->mc_count > 64)) {
  844. sbus_writel(0xffff, bregs + BMAC_HTABLE0);
  845. sbus_writel(0xffff, bregs + BMAC_HTABLE1);
  846. sbus_writel(0xffff, bregs + BMAC_HTABLE2);
  847. sbus_writel(0xffff, bregs + BMAC_HTABLE3);
  848. } else if (dev->flags & IFF_PROMISC) {
  849. tmp = sbus_readl(bregs + BMAC_RXCFG);
  850. tmp |= BIGMAC_RXCFG_PMISC;
  851. sbus_writel(tmp, bregs + BMAC_RXCFG);
  852. } else {
  853. u16 hash_table[4];
  854. for (i = 0; i < 4; i++)
  855. hash_table[i] = 0;
  856. for (i = 0; i < dev->mc_count; i++) {
  857. addrs = dmi->dmi_addr;
  858. dmi = dmi->next;
  859. if (!(*addrs & 1))
  860. continue;
  861. crc = ether_crc_le(6, addrs);
  862. crc >>= 26;
  863. hash_table[crc >> 4] |= 1 << (crc & 0xf);
  864. }
  865. sbus_writel(hash_table[0], bregs + BMAC_HTABLE0);
  866. sbus_writel(hash_table[1], bregs + BMAC_HTABLE1);
  867. sbus_writel(hash_table[2], bregs + BMAC_HTABLE2);
  868. sbus_writel(hash_table[3], bregs + BMAC_HTABLE3);
  869. }
  870. /* Re-enable the receiver. */
  871. tmp = sbus_readl(bregs + BMAC_RXCFG);
  872. tmp |= BIGMAC_RXCFG_ENABLE;
  873. sbus_writel(tmp, bregs + BMAC_RXCFG);
  874. }
  875. /* Ethtool support... */
  876. static void bigmac_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  877. {
  878. strcpy(info->driver, "sunbmac");
  879. strcpy(info->version, "2.0");
  880. }
  881. static u32 bigmac_get_link(struct net_device *dev)
  882. {
  883. struct bigmac *bp = netdev_priv(dev);
  884. spin_lock_irq(&bp->lock);
  885. bp->sw_bmsr = bigmac_tcvr_read(bp, bp->tregs, BIGMAC_BMSR);
  886. spin_unlock_irq(&bp->lock);
  887. return (bp->sw_bmsr & BMSR_LSTATUS);
  888. }
  889. static const struct ethtool_ops bigmac_ethtool_ops = {
  890. .get_drvinfo = bigmac_get_drvinfo,
  891. .get_link = bigmac_get_link,
  892. };
  893. static int __devinit bigmac_ether_init(struct of_device *op,
  894. struct of_device *qec_op)
  895. {
  896. static int version_printed;
  897. struct net_device *dev;
  898. u8 bsizes, bsizes_more;
  899. struct bigmac *bp;
  900. int i;
  901. /* Get a new device struct for this interface. */
  902. dev = alloc_etherdev(sizeof(struct bigmac));
  903. if (!dev)
  904. return -ENOMEM;
  905. if (version_printed++ == 0)
  906. printk(KERN_INFO "%s", version);
  907. for (i = 0; i < 6; i++)
  908. dev->dev_addr[i] = idprom->id_ethaddr[i];
  909. /* Setup softc, with backpointers to QEC and BigMAC SBUS device structs. */
  910. bp = netdev_priv(dev);
  911. bp->qec_op = qec_op;
  912. bp->bigmac_op = op;
  913. SET_NETDEV_DEV(dev, &op->dev);
  914. spin_lock_init(&bp->lock);
  915. /* Map in QEC global control registers. */
  916. bp->gregs = of_ioremap(&qec_op->resource[0], 0,
  917. GLOB_REG_SIZE, "BigMAC QEC GLobal Regs");
  918. if (!bp->gregs) {
  919. printk(KERN_ERR "BIGMAC: Cannot map QEC global registers.\n");
  920. goto fail_and_cleanup;
  921. }
  922. /* Make sure QEC is in BigMAC mode. */
  923. if ((sbus_readl(bp->gregs + GLOB_CTRL) & 0xf0000000) != GLOB_CTRL_BMODE) {
  924. printk(KERN_ERR "BigMAC: AIEEE, QEC is not in BigMAC mode!\n");
  925. goto fail_and_cleanup;
  926. }
  927. /* Reset the QEC. */
  928. if (qec_global_reset(bp->gregs))
  929. goto fail_and_cleanup;
  930. /* Get supported SBUS burst sizes. */
  931. bsizes = of_getintprop_default(qec_op->node, "burst-sizes", 0xff);
  932. bsizes_more = of_getintprop_default(qec_op->node, "burst-sizes", 0xff);
  933. bsizes &= 0xff;
  934. if (bsizes_more != 0xff)
  935. bsizes &= bsizes_more;
  936. if (bsizes == 0xff || (bsizes & DMA_BURST16) == 0 ||
  937. (bsizes & DMA_BURST32) == 0)
  938. bsizes = (DMA_BURST32 - 1);
  939. bp->bigmac_bursts = bsizes;
  940. /* Perform QEC initialization. */
  941. qec_init(bp);
  942. /* Map in the BigMAC channel registers. */
  943. bp->creg = of_ioremap(&op->resource[0], 0,
  944. CREG_REG_SIZE, "BigMAC QEC Channel Regs");
  945. if (!bp->creg) {
  946. printk(KERN_ERR "BIGMAC: Cannot map QEC channel registers.\n");
  947. goto fail_and_cleanup;
  948. }
  949. /* Map in the BigMAC control registers. */
  950. bp->bregs = of_ioremap(&op->resource[1], 0,
  951. BMAC_REG_SIZE, "BigMAC Primary Regs");
  952. if (!bp->bregs) {
  953. printk(KERN_ERR "BIGMAC: Cannot map BigMAC primary registers.\n");
  954. goto fail_and_cleanup;
  955. }
  956. /* Map in the BigMAC transceiver registers, this is how you poke at
  957. * the BigMAC's PHY.
  958. */
  959. bp->tregs = of_ioremap(&op->resource[2], 0,
  960. TCVR_REG_SIZE, "BigMAC Transceiver Regs");
  961. if (!bp->tregs) {
  962. printk(KERN_ERR "BIGMAC: Cannot map BigMAC transceiver registers.\n");
  963. goto fail_and_cleanup;
  964. }
  965. /* Stop the BigMAC. */
  966. bigmac_stop(bp);
  967. /* Allocate transmit/receive descriptor DVMA block. */
  968. bp->bmac_block = dma_alloc_coherent(&bp->bigmac_op->dev,
  969. PAGE_SIZE,
  970. &bp->bblock_dvma, GFP_ATOMIC);
  971. if (bp->bmac_block == NULL || bp->bblock_dvma == 0) {
  972. printk(KERN_ERR "BIGMAC: Cannot allocate consistent DMA.\n");
  973. goto fail_and_cleanup;
  974. }
  975. /* Get the board revision of this BigMAC. */
  976. bp->board_rev = of_getintprop_default(bp->bigmac_op->node,
  977. "board-version", 1);
  978. /* Init auto-negotiation timer state. */
  979. init_timer(&bp->bigmac_timer);
  980. bp->timer_state = asleep;
  981. bp->timer_ticks = 0;
  982. /* Backlink to generic net device struct. */
  983. bp->dev = dev;
  984. /* Set links to our BigMAC open and close routines. */
  985. dev->open = &bigmac_open;
  986. dev->stop = &bigmac_close;
  987. dev->hard_start_xmit = &bigmac_start_xmit;
  988. dev->ethtool_ops = &bigmac_ethtool_ops;
  989. /* Set links to BigMAC statistic and multi-cast loading code. */
  990. dev->get_stats = &bigmac_get_stats;
  991. dev->set_multicast_list = &bigmac_set_multicast;
  992. dev->tx_timeout = &bigmac_tx_timeout;
  993. dev->watchdog_timeo = 5*HZ;
  994. /* Finish net device registration. */
  995. dev->irq = bp->bigmac_op->irqs[0];
  996. dev->dma = 0;
  997. if (register_netdev(dev)) {
  998. printk(KERN_ERR "BIGMAC: Cannot register device.\n");
  999. goto fail_and_cleanup;
  1000. }
  1001. dev_set_drvdata(&bp->bigmac_op->dev, bp);
  1002. printk(KERN_INFO "%s: BigMAC 100baseT Ethernet %pM\n",
  1003. dev->name, dev->dev_addr);
  1004. return 0;
  1005. fail_and_cleanup:
  1006. /* Something went wrong, undo whatever we did so far. */
  1007. /* Free register mappings if any. */
  1008. if (bp->gregs)
  1009. of_iounmap(&qec_op->resource[0], bp->gregs, GLOB_REG_SIZE);
  1010. if (bp->creg)
  1011. of_iounmap(&op->resource[0], bp->creg, CREG_REG_SIZE);
  1012. if (bp->bregs)
  1013. of_iounmap(&op->resource[1], bp->bregs, BMAC_REG_SIZE);
  1014. if (bp->tregs)
  1015. of_iounmap(&op->resource[2], bp->tregs, TCVR_REG_SIZE);
  1016. if (bp->bmac_block)
  1017. dma_free_coherent(&bp->bigmac_op->dev,
  1018. PAGE_SIZE,
  1019. bp->bmac_block,
  1020. bp->bblock_dvma);
  1021. /* This also frees the co-located private data */
  1022. free_netdev(dev);
  1023. return -ENODEV;
  1024. }
  1025. /* QEC can be the parent of either QuadEthernet or a BigMAC. We want
  1026. * the latter.
  1027. */
  1028. static int __devinit bigmac_sbus_probe(struct of_device *op,
  1029. const struct of_device_id *match)
  1030. {
  1031. struct device *parent = op->dev.parent;
  1032. struct of_device *qec_op;
  1033. qec_op = to_of_device(parent);
  1034. return bigmac_ether_init(op, qec_op);
  1035. }
  1036. static int __devexit bigmac_sbus_remove(struct of_device *op)
  1037. {
  1038. struct bigmac *bp = dev_get_drvdata(&op->dev);
  1039. struct device *parent = op->dev.parent;
  1040. struct net_device *net_dev = bp->dev;
  1041. struct of_device *qec_op;
  1042. qec_op = to_of_device(parent);
  1043. unregister_netdev(net_dev);
  1044. of_iounmap(&qec_op->resource[0], bp->gregs, GLOB_REG_SIZE);
  1045. of_iounmap(&op->resource[0], bp->creg, CREG_REG_SIZE);
  1046. of_iounmap(&op->resource[1], bp->bregs, BMAC_REG_SIZE);
  1047. of_iounmap(&op->resource[2], bp->tregs, TCVR_REG_SIZE);
  1048. dma_free_coherent(&op->dev,
  1049. PAGE_SIZE,
  1050. bp->bmac_block,
  1051. bp->bblock_dvma);
  1052. free_netdev(net_dev);
  1053. dev_set_drvdata(&op->dev, NULL);
  1054. return 0;
  1055. }
  1056. static const struct of_device_id bigmac_sbus_match[] = {
  1057. {
  1058. .name = "be",
  1059. },
  1060. {},
  1061. };
  1062. MODULE_DEVICE_TABLE(of, bigmac_sbus_match);
  1063. static struct of_platform_driver bigmac_sbus_driver = {
  1064. .name = "sunbmac",
  1065. .match_table = bigmac_sbus_match,
  1066. .probe = bigmac_sbus_probe,
  1067. .remove = __devexit_p(bigmac_sbus_remove),
  1068. };
  1069. static int __init bigmac_init(void)
  1070. {
  1071. return of_register_driver(&bigmac_sbus_driver, &of_bus_type);
  1072. }
  1073. static void __exit bigmac_exit(void)
  1074. {
  1075. of_unregister_driver(&bigmac_sbus_driver);
  1076. }
  1077. module_init(bigmac_init);
  1078. module_exit(bigmac_exit);