sunbmac.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311
  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->dev->last_rx = jiffies;
  735. bp->enet_stats.rx_packets++;
  736. bp->enet_stats.rx_bytes += len;
  737. next:
  738. elem = NEXT_RX(elem);
  739. this = &rxbase[elem];
  740. }
  741. bp->rx_new = elem;
  742. if (drops)
  743. printk(KERN_NOTICE "%s: Memory squeeze, deferring packet.\n", bp->dev->name);
  744. }
  745. static irqreturn_t bigmac_interrupt(int irq, void *dev_id)
  746. {
  747. struct bigmac *bp = (struct bigmac *) dev_id;
  748. u32 qec_status, bmac_status;
  749. DIRQ(("bigmac_interrupt: "));
  750. /* Latch status registers now. */
  751. bmac_status = sbus_readl(bp->creg + CREG_STAT);
  752. qec_status = sbus_readl(bp->gregs + GLOB_STAT);
  753. DIRQ(("qec_status=%08x bmac_status=%08x\n", qec_status, bmac_status));
  754. if ((qec_status & (GLOB_STAT_ER | GLOB_STAT_BM)) ||
  755. (bmac_status & CREG_STAT_ERRORS))
  756. bigmac_is_medium_rare(bp, qec_status, bmac_status);
  757. if (bmac_status & CREG_STAT_TXIRQ)
  758. bigmac_tx(bp);
  759. if (bmac_status & CREG_STAT_RXIRQ)
  760. bigmac_rx(bp);
  761. return IRQ_HANDLED;
  762. }
  763. static int bigmac_open(struct net_device *dev)
  764. {
  765. struct bigmac *bp = (struct bigmac *) dev->priv;
  766. int ret;
  767. ret = request_irq(dev->irq, &bigmac_interrupt, IRQF_SHARED, dev->name, bp);
  768. if (ret) {
  769. printk(KERN_ERR "BIGMAC: Can't order irq %d to go.\n", dev->irq);
  770. return ret;
  771. }
  772. init_timer(&bp->bigmac_timer);
  773. ret = bigmac_init_hw(bp, 0);
  774. if (ret)
  775. free_irq(dev->irq, bp);
  776. return ret;
  777. }
  778. static int bigmac_close(struct net_device *dev)
  779. {
  780. struct bigmac *bp = (struct bigmac *) dev->priv;
  781. del_timer(&bp->bigmac_timer);
  782. bp->timer_state = asleep;
  783. bp->timer_ticks = 0;
  784. bigmac_stop(bp);
  785. bigmac_clean_rings(bp);
  786. free_irq(dev->irq, bp);
  787. return 0;
  788. }
  789. static void bigmac_tx_timeout(struct net_device *dev)
  790. {
  791. struct bigmac *bp = (struct bigmac *) dev->priv;
  792. bigmac_init_hw(bp, 0);
  793. netif_wake_queue(dev);
  794. }
  795. /* Put a packet on the wire. */
  796. static int bigmac_start_xmit(struct sk_buff *skb, struct net_device *dev)
  797. {
  798. struct bigmac *bp = (struct bigmac *) dev->priv;
  799. int len, entry;
  800. u32 mapping;
  801. len = skb->len;
  802. mapping = dma_map_single(&bp->bigmac_op->dev, skb->data,
  803. len, DMA_TO_DEVICE);
  804. /* Avoid a race... */
  805. spin_lock_irq(&bp->lock);
  806. entry = bp->tx_new;
  807. DTX(("bigmac_start_xmit: len(%d) entry(%d)\n", len, entry));
  808. bp->bmac_block->be_txd[entry].tx_flags = TXD_UPDATE;
  809. bp->tx_skbs[entry] = skb;
  810. bp->bmac_block->be_txd[entry].tx_addr = mapping;
  811. bp->bmac_block->be_txd[entry].tx_flags =
  812. (TXD_OWN | TXD_SOP | TXD_EOP | (len & TXD_LENGTH));
  813. bp->tx_new = NEXT_TX(entry);
  814. if (TX_BUFFS_AVAIL(bp) <= 0)
  815. netif_stop_queue(dev);
  816. spin_unlock_irq(&bp->lock);
  817. /* Get it going. */
  818. sbus_writel(CREG_CTRL_TWAKEUP, bp->creg + CREG_CTRL);
  819. dev->trans_start = jiffies;
  820. return 0;
  821. }
  822. static struct net_device_stats *bigmac_get_stats(struct net_device *dev)
  823. {
  824. struct bigmac *bp = (struct bigmac *) dev->priv;
  825. bigmac_get_counters(bp, bp->bregs);
  826. return &bp->enet_stats;
  827. }
  828. static void bigmac_set_multicast(struct net_device *dev)
  829. {
  830. struct bigmac *bp = (struct bigmac *) dev->priv;
  831. void __iomem *bregs = bp->bregs;
  832. struct dev_mc_list *dmi = dev->mc_list;
  833. char *addrs;
  834. int i;
  835. u32 tmp, crc;
  836. /* Disable the receiver. The bit self-clears when
  837. * the operation is complete.
  838. */
  839. tmp = sbus_readl(bregs + BMAC_RXCFG);
  840. tmp &= ~(BIGMAC_RXCFG_ENABLE);
  841. sbus_writel(tmp, bregs + BMAC_RXCFG);
  842. while ((sbus_readl(bregs + BMAC_RXCFG) & BIGMAC_RXCFG_ENABLE) != 0)
  843. udelay(20);
  844. if ((dev->flags & IFF_ALLMULTI) || (dev->mc_count > 64)) {
  845. sbus_writel(0xffff, bregs + BMAC_HTABLE0);
  846. sbus_writel(0xffff, bregs + BMAC_HTABLE1);
  847. sbus_writel(0xffff, bregs + BMAC_HTABLE2);
  848. sbus_writel(0xffff, bregs + BMAC_HTABLE3);
  849. } else if (dev->flags & IFF_PROMISC) {
  850. tmp = sbus_readl(bregs + BMAC_RXCFG);
  851. tmp |= BIGMAC_RXCFG_PMISC;
  852. sbus_writel(tmp, bregs + BMAC_RXCFG);
  853. } else {
  854. u16 hash_table[4];
  855. for (i = 0; i < 4; i++)
  856. hash_table[i] = 0;
  857. for (i = 0; i < dev->mc_count; i++) {
  858. addrs = dmi->dmi_addr;
  859. dmi = dmi->next;
  860. if (!(*addrs & 1))
  861. continue;
  862. crc = ether_crc_le(6, addrs);
  863. crc >>= 26;
  864. hash_table[crc >> 4] |= 1 << (crc & 0xf);
  865. }
  866. sbus_writel(hash_table[0], bregs + BMAC_HTABLE0);
  867. sbus_writel(hash_table[1], bregs + BMAC_HTABLE1);
  868. sbus_writel(hash_table[2], bregs + BMAC_HTABLE2);
  869. sbus_writel(hash_table[3], bregs + BMAC_HTABLE3);
  870. }
  871. /* Re-enable the receiver. */
  872. tmp = sbus_readl(bregs + BMAC_RXCFG);
  873. tmp |= BIGMAC_RXCFG_ENABLE;
  874. sbus_writel(tmp, bregs + BMAC_RXCFG);
  875. }
  876. /* Ethtool support... */
  877. static void bigmac_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  878. {
  879. strcpy(info->driver, "sunbmac");
  880. strcpy(info->version, "2.0");
  881. }
  882. static u32 bigmac_get_link(struct net_device *dev)
  883. {
  884. struct bigmac *bp = dev->priv;
  885. spin_lock_irq(&bp->lock);
  886. bp->sw_bmsr = bigmac_tcvr_read(bp, bp->tregs, BIGMAC_BMSR);
  887. spin_unlock_irq(&bp->lock);
  888. return (bp->sw_bmsr & BMSR_LSTATUS);
  889. }
  890. static const struct ethtool_ops bigmac_ethtool_ops = {
  891. .get_drvinfo = bigmac_get_drvinfo,
  892. .get_link = bigmac_get_link,
  893. };
  894. static int __devinit bigmac_ether_init(struct of_device *op,
  895. struct of_device *qec_op)
  896. {
  897. static int version_printed;
  898. struct net_device *dev;
  899. u8 bsizes, bsizes_more;
  900. DECLARE_MAC_BUF(mac);
  901. struct bigmac *bp;
  902. int i;
  903. /* Get a new device struct for this interface. */
  904. dev = alloc_etherdev(sizeof(struct bigmac));
  905. if (!dev)
  906. return -ENOMEM;
  907. if (version_printed++ == 0)
  908. printk(KERN_INFO "%s", version);
  909. for (i = 0; i < 6; i++)
  910. dev->dev_addr[i] = idprom->id_ethaddr[i];
  911. /* Setup softc, with backpointers to QEC and BigMAC SBUS device structs. */
  912. bp = netdev_priv(dev);
  913. bp->qec_op = qec_op;
  914. bp->bigmac_op = op;
  915. SET_NETDEV_DEV(dev, &op->dev);
  916. spin_lock_init(&bp->lock);
  917. /* Map in QEC global control registers. */
  918. bp->gregs = of_ioremap(&qec_op->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 = of_getintprop_default(qec_op->node, "burst-sizes", 0xff);
  934. bsizes_more = of_getintprop_default(qec_op->node, "burst-sizes", 0xff);
  935. bsizes &= 0xff;
  936. if (bsizes_more != 0xff)
  937. bsizes &= bsizes_more;
  938. if (bsizes == 0xff || (bsizes & DMA_BURST16) == 0 ||
  939. (bsizes & DMA_BURST32) == 0)
  940. bsizes = (DMA_BURST32 - 1);
  941. bp->bigmac_bursts = bsizes;
  942. /* Perform QEC initialization. */
  943. qec_init(bp);
  944. /* Map in the BigMAC channel registers. */
  945. bp->creg = of_ioremap(&op->resource[0], 0,
  946. CREG_REG_SIZE, "BigMAC QEC Channel Regs");
  947. if (!bp->creg) {
  948. printk(KERN_ERR "BIGMAC: Cannot map QEC channel registers.\n");
  949. goto fail_and_cleanup;
  950. }
  951. /* Map in the BigMAC control registers. */
  952. bp->bregs = of_ioremap(&op->resource[1], 0,
  953. BMAC_REG_SIZE, "BigMAC Primary Regs");
  954. if (!bp->bregs) {
  955. printk(KERN_ERR "BIGMAC: Cannot map BigMAC primary registers.\n");
  956. goto fail_and_cleanup;
  957. }
  958. /* Map in the BigMAC transceiver registers, this is how you poke at
  959. * the BigMAC's PHY.
  960. */
  961. bp->tregs = of_ioremap(&op->resource[2], 0,
  962. TCVR_REG_SIZE, "BigMAC Transceiver Regs");
  963. if (!bp->tregs) {
  964. printk(KERN_ERR "BIGMAC: Cannot map BigMAC transceiver registers.\n");
  965. goto fail_and_cleanup;
  966. }
  967. /* Stop the BigMAC. */
  968. bigmac_stop(bp);
  969. /* Allocate transmit/receive descriptor DVMA block. */
  970. bp->bmac_block = dma_alloc_coherent(&bp->bigmac_op->dev,
  971. PAGE_SIZE,
  972. &bp->bblock_dvma, GFP_ATOMIC);
  973. if (bp->bmac_block == NULL || bp->bblock_dvma == 0) {
  974. printk(KERN_ERR "BIGMAC: Cannot allocate consistent DMA.\n");
  975. goto fail_and_cleanup;
  976. }
  977. /* Get the board revision of this BigMAC. */
  978. bp->board_rev = of_getintprop_default(bp->bigmac_op->node,
  979. "board-version", 1);
  980. /* Init auto-negotiation timer state. */
  981. init_timer(&bp->bigmac_timer);
  982. bp->timer_state = asleep;
  983. bp->timer_ticks = 0;
  984. /* Backlink to generic net device struct. */
  985. bp->dev = dev;
  986. /* Set links to our BigMAC open and close routines. */
  987. dev->open = &bigmac_open;
  988. dev->stop = &bigmac_close;
  989. dev->hard_start_xmit = &bigmac_start_xmit;
  990. dev->ethtool_ops = &bigmac_ethtool_ops;
  991. /* Set links to BigMAC statistic and multi-cast loading code. */
  992. dev->get_stats = &bigmac_get_stats;
  993. dev->set_multicast_list = &bigmac_set_multicast;
  994. dev->tx_timeout = &bigmac_tx_timeout;
  995. dev->watchdog_timeo = 5*HZ;
  996. /* Finish net device registration. */
  997. dev->irq = bp->bigmac_op->irqs[0];
  998. dev->dma = 0;
  999. if (register_netdev(dev)) {
  1000. printk(KERN_ERR "BIGMAC: Cannot register device.\n");
  1001. goto fail_and_cleanup;
  1002. }
  1003. dev_set_drvdata(&bp->bigmac_op->dev, bp);
  1004. printk(KERN_INFO "%s: BigMAC 100baseT Ethernet %s\n",
  1005. dev->name, print_mac(mac, dev->dev_addr));
  1006. return 0;
  1007. fail_and_cleanup:
  1008. /* Something went wrong, undo whatever we did so far. */
  1009. /* Free register mappings if any. */
  1010. if (bp->gregs)
  1011. of_iounmap(&qec_op->resource[0], bp->gregs, GLOB_REG_SIZE);
  1012. if (bp->creg)
  1013. of_iounmap(&op->resource[0], bp->creg, CREG_REG_SIZE);
  1014. if (bp->bregs)
  1015. of_iounmap(&op->resource[1], bp->bregs, BMAC_REG_SIZE);
  1016. if (bp->tregs)
  1017. of_iounmap(&op->resource[2], bp->tregs, TCVR_REG_SIZE);
  1018. if (bp->bmac_block)
  1019. dma_free_coherent(&bp->bigmac_op->dev,
  1020. PAGE_SIZE,
  1021. bp->bmac_block,
  1022. bp->bblock_dvma);
  1023. /* This also frees the co-located 'dev->priv' */
  1024. free_netdev(dev);
  1025. return -ENODEV;
  1026. }
  1027. /* QEC can be the parent of either QuadEthernet or a BigMAC. We want
  1028. * the latter.
  1029. */
  1030. static int __devinit bigmac_sbus_probe(struct of_device *op,
  1031. const struct of_device_id *match)
  1032. {
  1033. struct device *parent = op->dev.parent;
  1034. struct of_device *qec_op;
  1035. qec_op = to_of_device(parent);
  1036. return bigmac_ether_init(op, qec_op);
  1037. }
  1038. static int __devexit bigmac_sbus_remove(struct of_device *op)
  1039. {
  1040. struct bigmac *bp = dev_get_drvdata(&op->dev);
  1041. struct device *parent = op->dev.parent;
  1042. struct net_device *net_dev = bp->dev;
  1043. struct of_device *qec_op;
  1044. qec_op = to_of_device(parent);
  1045. unregister_netdev(net_dev);
  1046. of_iounmap(&qec_op->resource[0], bp->gregs, GLOB_REG_SIZE);
  1047. of_iounmap(&op->resource[0], bp->creg, CREG_REG_SIZE);
  1048. of_iounmap(&op->resource[1], bp->bregs, BMAC_REG_SIZE);
  1049. of_iounmap(&op->resource[2], bp->tregs, TCVR_REG_SIZE);
  1050. dma_free_coherent(&op->dev,
  1051. PAGE_SIZE,
  1052. bp->bmac_block,
  1053. bp->bblock_dvma);
  1054. free_netdev(net_dev);
  1055. dev_set_drvdata(&op->dev, NULL);
  1056. return 0;
  1057. }
  1058. static const struct of_device_id bigmac_sbus_match[] = {
  1059. {
  1060. .name = "be",
  1061. },
  1062. {},
  1063. };
  1064. MODULE_DEVICE_TABLE(of, bigmac_sbus_match);
  1065. static struct of_platform_driver bigmac_sbus_driver = {
  1066. .name = "sunbmac",
  1067. .match_table = bigmac_sbus_match,
  1068. .probe = bigmac_sbus_probe,
  1069. .remove = __devexit_p(bigmac_sbus_remove),
  1070. };
  1071. static int __init bigmac_init(void)
  1072. {
  1073. return of_register_driver(&bigmac_sbus_driver, &of_bus_type);
  1074. }
  1075. static void __exit bigmac_exit(void)
  1076. {
  1077. of_unregister_driver(&bigmac_sbus_driver);
  1078. }
  1079. module_init(bigmac_init);
  1080. module_exit(bigmac_exit);