sunbmac.c 33 KB

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