sunbmac.c 33 KB

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