sunbmac.c 33 KB

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