sunbmac.c 33 KB

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