sunbmac.c 33 KB

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