sunbmac.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* sunbmac.c: Driver for Sparc BigMAC 100baseT ethernet adapters.
  3. *
  4. * Copyright (C) 1997, 1998, 1999, 2003, 2008 David S. Miller ([email protected])
  5. */
  6. #include <linux/module.h>
  7. #include <linux/pgtable.h>
  8. #include <linux/kernel.h>
  9. #include <linux/types.h>
  10. #include <linux/fcntl.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/ioport.h>
  13. #include <linux/in.h>
  14. #include <linux/string.h>
  15. #include <linux/delay.h>
  16. #include <linux/crc32.h>
  17. #include <linux/errno.h>
  18. #include <linux/ethtool.h>
  19. #include <linux/mii.h>
  20. #include <linux/netdevice.h>
  21. #include <linux/etherdevice.h>
  22. #include <linux/skbuff.h>
  23. #include <linux/bitops.h>
  24. #include <linux/dma-mapping.h>
  25. #include <linux/of.h>
  26. #include <linux/of_device.h>
  27. #include <linux/gfp.h>
  28. #include <asm/auxio.h>
  29. #include <asm/byteorder.h>
  30. #include <asm/dma.h>
  31. #include <asm/idprom.h>
  32. #include <asm/io.h>
  33. #include <asm/openprom.h>
  34. #include <asm/oplib.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 ([email protected])"
  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->dev->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, bool non_blocking)
  175. {
  176. struct bmac_init_block *bb = bp->bmac_block;
  177. int i;
  178. gfp_t gfp_flags = GFP_KERNEL;
  179. if (non_blocking)
  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 *, bool);
  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(struct timer_list *t)
  436. {
  437. struct bigmac *bp = from_timer(bp, t, bigmac_timer);
  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, true);
  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. add_timer(&bp->bigmac_timer);
  516. }
  517. static int bigmac_init_hw(struct bigmac *bp, bool non_blocking)
  518. {
  519. void __iomem *gregs = bp->gregs;
  520. void __iomem *cregs = bp->creg;
  521. void __iomem *bregs = bp->bregs;
  522. __u32 bblk_dvma = (__u32)bp->bblock_dvma;
  523. const unsigned char *e = &bp->dev->dev_addr[0];
  524. /* Latch current counters into statistics. */
  525. bigmac_get_counters(bp, bregs);
  526. /* Reset QEC. */
  527. qec_global_reset(gregs);
  528. /* Init QEC. */
  529. qec_init(bp);
  530. /* Alloc and reset the tx/rx descriptor chains. */
  531. bigmac_init_rings(bp, non_blocking);
  532. /* Initialize the PHY. */
  533. bigmac_tcvr_init(bp);
  534. /* Stop transmitter and receiver. */
  535. bigmac_stop(bp);
  536. /* Set hardware ethernet address. */
  537. sbus_writel(((e[4] << 8) | e[5]), bregs + BMAC_MACADDR2);
  538. sbus_writel(((e[2] << 8) | e[3]), bregs + BMAC_MACADDR1);
  539. sbus_writel(((e[0] << 8) | e[1]), bregs + BMAC_MACADDR0);
  540. /* Clear the hash table until mc upload occurs. */
  541. sbus_writel(0, bregs + BMAC_HTABLE3);
  542. sbus_writel(0, bregs + BMAC_HTABLE2);
  543. sbus_writel(0, bregs + BMAC_HTABLE1);
  544. sbus_writel(0, bregs + BMAC_HTABLE0);
  545. /* Enable Big Mac hash table filter. */
  546. sbus_writel(BIGMAC_RXCFG_HENABLE | BIGMAC_RXCFG_FIFO,
  547. bregs + BMAC_RXCFG);
  548. udelay(20);
  549. /* Ok, configure the Big Mac transmitter. */
  550. sbus_writel(BIGMAC_TXCFG_FIFO, bregs + BMAC_TXCFG);
  551. /* The HME docs recommend to use the 10LSB of our MAC here. */
  552. sbus_writel(((e[5] | e[4] << 8) & 0x3ff),
  553. bregs + BMAC_RSEED);
  554. /* Enable the output drivers no matter what. */
  555. sbus_writel(BIGMAC_XCFG_ODENABLE | BIGMAC_XCFG_RESV,
  556. bregs + BMAC_XIFCFG);
  557. /* Tell the QEC where the ring descriptors are. */
  558. sbus_writel(bblk_dvma + bib_offset(be_rxd, 0),
  559. cregs + CREG_RXDS);
  560. sbus_writel(bblk_dvma + bib_offset(be_txd, 0),
  561. cregs + CREG_TXDS);
  562. /* Setup the FIFO pointers into QEC local memory. */
  563. sbus_writel(0, cregs + CREG_RXRBUFPTR);
  564. sbus_writel(0, cregs + CREG_RXWBUFPTR);
  565. sbus_writel(sbus_readl(gregs + GLOB_RSIZE),
  566. cregs + CREG_TXRBUFPTR);
  567. sbus_writel(sbus_readl(gregs + GLOB_RSIZE),
  568. cregs + CREG_TXWBUFPTR);
  569. /* Tell bigmac what interrupts we don't want to hear about. */
  570. sbus_writel(BIGMAC_IMASK_GOTFRAME | BIGMAC_IMASK_SENTFRAME,
  571. bregs + BMAC_IMASK);
  572. /* Enable the various other irq's. */
  573. sbus_writel(0, cregs + CREG_RIMASK);
  574. sbus_writel(0, cregs + CREG_TIMASK);
  575. sbus_writel(0, cregs + CREG_QMASK);
  576. sbus_writel(0, cregs + CREG_BMASK);
  577. /* Set jam size to a reasonable default. */
  578. sbus_writel(DEFAULT_JAMSIZE, bregs + BMAC_JSIZE);
  579. /* Clear collision counter. */
  580. sbus_writel(0, cregs + CREG_CCNT);
  581. /* Enable transmitter and receiver. */
  582. sbus_writel(sbus_readl(bregs + BMAC_TXCFG) | BIGMAC_TXCFG_ENABLE,
  583. bregs + BMAC_TXCFG);
  584. sbus_writel(sbus_readl(bregs + BMAC_RXCFG) | BIGMAC_RXCFG_ENABLE,
  585. bregs + BMAC_RXCFG);
  586. /* Ok, start detecting link speed/duplex. */
  587. bigmac_begin_auto_negotiation(bp);
  588. /* Success. */
  589. return 0;
  590. }
  591. /* Error interrupts get sent here. */
  592. static void bigmac_is_medium_rare(struct bigmac *bp, u32 qec_status, u32 bmac_status)
  593. {
  594. printk(KERN_ERR "bigmac_is_medium_rare: ");
  595. if (qec_status & (GLOB_STAT_ER | GLOB_STAT_BM)) {
  596. if (qec_status & GLOB_STAT_ER)
  597. printk("QEC_ERROR, ");
  598. if (qec_status & GLOB_STAT_BM)
  599. printk("QEC_BMAC_ERROR, ");
  600. }
  601. if (bmac_status & CREG_STAT_ERRORS) {
  602. if (bmac_status & CREG_STAT_BERROR)
  603. printk("BMAC_ERROR, ");
  604. if (bmac_status & CREG_STAT_TXDERROR)
  605. printk("TXD_ERROR, ");
  606. if (bmac_status & CREG_STAT_TXLERR)
  607. printk("TX_LATE_ERROR, ");
  608. if (bmac_status & CREG_STAT_TXPERR)
  609. printk("TX_PARITY_ERROR, ");
  610. if (bmac_status & CREG_STAT_TXSERR)
  611. printk("TX_SBUS_ERROR, ");
  612. if (bmac_status & CREG_STAT_RXDROP)
  613. printk("RX_DROP_ERROR, ");
  614. if (bmac_status & CREG_STAT_RXSMALL)
  615. printk("RX_SMALL_ERROR, ");
  616. if (bmac_status & CREG_STAT_RXLERR)
  617. printk("RX_LATE_ERROR, ");
  618. if (bmac_status & CREG_STAT_RXPERR)
  619. printk("RX_PARITY_ERROR, ");
  620. if (bmac_status & CREG_STAT_RXSERR)
  621. printk("RX_SBUS_ERROR, ");
  622. }
  623. printk(" RESET\n");
  624. bigmac_init_hw(bp, true);
  625. }
  626. /* BigMAC transmit complete service routines. */
  627. static void bigmac_tx(struct bigmac *bp)
  628. {
  629. struct be_txd *txbase = &bp->bmac_block->be_txd[0];
  630. struct net_device *dev = bp->dev;
  631. int elem;
  632. spin_lock(&bp->lock);
  633. elem = bp->tx_old;
  634. DTX(("bigmac_tx: tx_old[%d] ", elem));
  635. while (elem != bp->tx_new) {
  636. struct sk_buff *skb;
  637. struct be_txd *this = &txbase[elem];
  638. DTX(("this(%p) [flags(%08x)addr(%08x)]",
  639. this, this->tx_flags, this->tx_addr));
  640. if (this->tx_flags & TXD_OWN)
  641. break;
  642. skb = bp->tx_skbs[elem];
  643. dev->stats.tx_packets++;
  644. dev->stats.tx_bytes += skb->len;
  645. dma_unmap_single(&bp->bigmac_op->dev,
  646. this->tx_addr, skb->len,
  647. DMA_TO_DEVICE);
  648. DTX(("skb(%p) ", skb));
  649. bp->tx_skbs[elem] = NULL;
  650. dev_consume_skb_irq(skb);
  651. elem = NEXT_TX(elem);
  652. }
  653. DTX((" DONE, tx_old=%d\n", elem));
  654. bp->tx_old = elem;
  655. if (netif_queue_stopped(dev) &&
  656. TX_BUFFS_AVAIL(bp) > 0)
  657. netif_wake_queue(bp->dev);
  658. spin_unlock(&bp->lock);
  659. }
  660. /* BigMAC receive complete service routines. */
  661. static void bigmac_rx(struct bigmac *bp)
  662. {
  663. struct be_rxd *rxbase = &bp->bmac_block->be_rxd[0];
  664. struct be_rxd *this;
  665. int elem = bp->rx_new, drops = 0;
  666. u32 flags;
  667. this = &rxbase[elem];
  668. while (!((flags = this->rx_flags) & RXD_OWN)) {
  669. struct sk_buff *skb;
  670. int len = (flags & RXD_LENGTH); /* FCS not included */
  671. /* Check for errors. */
  672. if (len < ETH_ZLEN) {
  673. bp->dev->stats.rx_errors++;
  674. bp->dev->stats.rx_length_errors++;
  675. drop_it:
  676. /* Return it to the BigMAC. */
  677. bp->dev->stats.rx_dropped++;
  678. this->rx_flags =
  679. (RXD_OWN | ((RX_BUF_ALLOC_SIZE - 34) & RXD_LENGTH));
  680. goto next;
  681. }
  682. skb = bp->rx_skbs[elem];
  683. if (len > RX_COPY_THRESHOLD) {
  684. struct sk_buff *new_skb;
  685. /* Now refill the entry, if we can. */
  686. new_skb = big_mac_alloc_skb(RX_BUF_ALLOC_SIZE, GFP_ATOMIC);
  687. if (new_skb == NULL) {
  688. drops++;
  689. goto drop_it;
  690. }
  691. dma_unmap_single(&bp->bigmac_op->dev,
  692. this->rx_addr,
  693. RX_BUF_ALLOC_SIZE - 34,
  694. DMA_FROM_DEVICE);
  695. bp->rx_skbs[elem] = new_skb;
  696. skb_put(new_skb, ETH_FRAME_LEN);
  697. skb_reserve(new_skb, 34);
  698. this->rx_addr =
  699. dma_map_single(&bp->bigmac_op->dev,
  700. new_skb->data,
  701. RX_BUF_ALLOC_SIZE - 34,
  702. DMA_FROM_DEVICE);
  703. this->rx_flags =
  704. (RXD_OWN | ((RX_BUF_ALLOC_SIZE - 34) & RXD_LENGTH));
  705. /* Trim the original skb for the netif. */
  706. skb_trim(skb, len);
  707. } else {
  708. struct sk_buff *copy_skb = netdev_alloc_skb(bp->dev, len + 2);
  709. if (copy_skb == NULL) {
  710. drops++;
  711. goto drop_it;
  712. }
  713. skb_reserve(copy_skb, 2);
  714. skb_put(copy_skb, len);
  715. dma_sync_single_for_cpu(&bp->bigmac_op->dev,
  716. this->rx_addr, len,
  717. DMA_FROM_DEVICE);
  718. skb_copy_to_linear_data(copy_skb, (unsigned char *)skb->data, len);
  719. dma_sync_single_for_device(&bp->bigmac_op->dev,
  720. this->rx_addr, len,
  721. DMA_FROM_DEVICE);
  722. /* Reuse original ring buffer. */
  723. this->rx_flags =
  724. (RXD_OWN | ((RX_BUF_ALLOC_SIZE - 34) & RXD_LENGTH));
  725. skb = copy_skb;
  726. }
  727. /* No checksums done by the BigMAC ;-( */
  728. skb->protocol = eth_type_trans(skb, bp->dev);
  729. netif_rx(skb);
  730. bp->dev->stats.rx_packets++;
  731. bp->dev->stats.rx_bytes += len;
  732. next:
  733. elem = NEXT_RX(elem);
  734. this = &rxbase[elem];
  735. }
  736. bp->rx_new = elem;
  737. if (drops)
  738. printk(KERN_NOTICE "%s: Memory squeeze, deferring packet.\n", bp->dev->name);
  739. }
  740. static irqreturn_t bigmac_interrupt(int irq, void *dev_id)
  741. {
  742. struct bigmac *bp = (struct bigmac *) dev_id;
  743. u32 qec_status, bmac_status;
  744. DIRQ(("bigmac_interrupt: "));
  745. /* Latch status registers now. */
  746. bmac_status = sbus_readl(bp->creg + CREG_STAT);
  747. qec_status = sbus_readl(bp->gregs + GLOB_STAT);
  748. DIRQ(("qec_status=%08x bmac_status=%08x\n", qec_status, bmac_status));
  749. if ((qec_status & (GLOB_STAT_ER | GLOB_STAT_BM)) ||
  750. (bmac_status & CREG_STAT_ERRORS))
  751. bigmac_is_medium_rare(bp, qec_status, bmac_status);
  752. if (bmac_status & CREG_STAT_TXIRQ)
  753. bigmac_tx(bp);
  754. if (bmac_status & CREG_STAT_RXIRQ)
  755. bigmac_rx(bp);
  756. return IRQ_HANDLED;
  757. }
  758. static int bigmac_open(struct net_device *dev)
  759. {
  760. struct bigmac *bp = netdev_priv(dev);
  761. int ret;
  762. ret = request_irq(dev->irq, bigmac_interrupt, IRQF_SHARED, dev->name, bp);
  763. if (ret) {
  764. printk(KERN_ERR "BIGMAC: Can't order irq %d to go.\n", dev->irq);
  765. return ret;
  766. }
  767. timer_setup(&bp->bigmac_timer, bigmac_timer, 0);
  768. ret = bigmac_init_hw(bp, false);
  769. if (ret)
  770. free_irq(dev->irq, bp);
  771. return ret;
  772. }
  773. static int bigmac_close(struct net_device *dev)
  774. {
  775. struct bigmac *bp = netdev_priv(dev);
  776. del_timer(&bp->bigmac_timer);
  777. bp->timer_state = asleep;
  778. bp->timer_ticks = 0;
  779. bigmac_stop(bp);
  780. bigmac_clean_rings(bp);
  781. free_irq(dev->irq, bp);
  782. return 0;
  783. }
  784. static void bigmac_tx_timeout(struct net_device *dev, unsigned int txqueue)
  785. {
  786. struct bigmac *bp = netdev_priv(dev);
  787. bigmac_init_hw(bp, true);
  788. netif_wake_queue(dev);
  789. }
  790. /* Put a packet on the wire. */
  791. static netdev_tx_t
  792. 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 &dev->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. u32 tmp, crc;
  829. /* Disable the receiver. The bit self-clears when
  830. * the operation is complete.
  831. */
  832. tmp = sbus_readl(bregs + BMAC_RXCFG);
  833. tmp &= ~(BIGMAC_RXCFG_ENABLE);
  834. sbus_writel(tmp, bregs + BMAC_RXCFG);
  835. while ((sbus_readl(bregs + BMAC_RXCFG) & BIGMAC_RXCFG_ENABLE) != 0)
  836. udelay(20);
  837. if ((dev->flags & IFF_ALLMULTI) || (netdev_mc_count(dev) > 64)) {
  838. sbus_writel(0xffff, bregs + BMAC_HTABLE0);
  839. sbus_writel(0xffff, bregs + BMAC_HTABLE1);
  840. sbus_writel(0xffff, bregs + BMAC_HTABLE2);
  841. sbus_writel(0xffff, bregs + BMAC_HTABLE3);
  842. } else if (dev->flags & IFF_PROMISC) {
  843. tmp = sbus_readl(bregs + BMAC_RXCFG);
  844. tmp |= BIGMAC_RXCFG_PMISC;
  845. sbus_writel(tmp, bregs + BMAC_RXCFG);
  846. } else {
  847. u16 hash_table[4] = { 0 };
  848. netdev_for_each_mc_addr(ha, dev) {
  849. crc = ether_crc_le(6, ha->addr);
  850. crc >>= 26;
  851. hash_table[crc >> 4] |= 1 << (crc & 0xf);
  852. }
  853. sbus_writel(hash_table[0], bregs + BMAC_HTABLE0);
  854. sbus_writel(hash_table[1], bregs + BMAC_HTABLE1);
  855. sbus_writel(hash_table[2], bregs + BMAC_HTABLE2);
  856. sbus_writel(hash_table[3], bregs + BMAC_HTABLE3);
  857. }
  858. /* Re-enable the receiver. */
  859. tmp = sbus_readl(bregs + BMAC_RXCFG);
  860. tmp |= BIGMAC_RXCFG_ENABLE;
  861. sbus_writel(tmp, bregs + BMAC_RXCFG);
  862. }
  863. /* Ethtool support... */
  864. static void bigmac_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  865. {
  866. strscpy(info->driver, "sunbmac", sizeof(info->driver));
  867. strscpy(info->version, "2.0", sizeof(info->version));
  868. }
  869. static u32 bigmac_get_link(struct net_device *dev)
  870. {
  871. struct bigmac *bp = netdev_priv(dev);
  872. spin_lock_irq(&bp->lock);
  873. bp->sw_bmsr = bigmac_tcvr_read(bp, bp->tregs, MII_BMSR);
  874. spin_unlock_irq(&bp->lock);
  875. return (bp->sw_bmsr & BMSR_LSTATUS);
  876. }
  877. static const struct ethtool_ops bigmac_ethtool_ops = {
  878. .get_drvinfo = bigmac_get_drvinfo,
  879. .get_link = bigmac_get_link,
  880. };
  881. static const struct net_device_ops bigmac_ops = {
  882. .ndo_open = bigmac_open,
  883. .ndo_stop = bigmac_close,
  884. .ndo_start_xmit = bigmac_start_xmit,
  885. .ndo_get_stats = bigmac_get_stats,
  886. .ndo_set_rx_mode = bigmac_set_multicast,
  887. .ndo_tx_timeout = bigmac_tx_timeout,
  888. .ndo_set_mac_address = eth_mac_addr,
  889. .ndo_validate_addr = eth_validate_addr,
  890. };
  891. static int bigmac_ether_init(struct platform_device *op,
  892. struct platform_device *qec_op)
  893. {
  894. static int version_printed;
  895. struct net_device *dev;
  896. u8 bsizes, bsizes_more;
  897. struct bigmac *bp;
  898. /* Get a new device struct for this interface. */
  899. dev = alloc_etherdev(sizeof(struct bigmac));
  900. if (!dev)
  901. return -ENOMEM;
  902. if (version_printed++ == 0)
  903. printk(KERN_INFO "%s", version);
  904. eth_hw_addr_set(dev, idprom->id_ethaddr);
  905. /* Setup softc, with backpointers to QEC and BigMAC SBUS device structs. */
  906. bp = netdev_priv(dev);
  907. bp->qec_op = qec_op;
  908. bp->bigmac_op = op;
  909. SET_NETDEV_DEV(dev, &op->dev);
  910. spin_lock_init(&bp->lock);
  911. /* Map in QEC global control registers. */
  912. bp->gregs = of_ioremap(&qec_op->resource[0], 0,
  913. GLOB_REG_SIZE, "BigMAC QEC GLobal Regs");
  914. if (!bp->gregs) {
  915. printk(KERN_ERR "BIGMAC: Cannot map QEC global registers.\n");
  916. goto fail_and_cleanup;
  917. }
  918. /* Make sure QEC is in BigMAC mode. */
  919. if ((sbus_readl(bp->gregs + GLOB_CTRL) & 0xf0000000) != GLOB_CTRL_BMODE) {
  920. printk(KERN_ERR "BigMAC: AIEEE, QEC is not in BigMAC mode!\n");
  921. goto fail_and_cleanup;
  922. }
  923. /* Reset the QEC. */
  924. if (qec_global_reset(bp->gregs))
  925. goto fail_and_cleanup;
  926. /* Get supported SBUS burst sizes. */
  927. bsizes = of_getintprop_default(qec_op->dev.of_node, "burst-sizes", 0xff);
  928. bsizes_more = of_getintprop_default(qec_op->dev.of_node, "burst-sizes", 0xff);
  929. bsizes &= 0xff;
  930. if (bsizes_more != 0xff)
  931. bsizes &= bsizes_more;
  932. if (bsizes == 0xff || (bsizes & DMA_BURST16) == 0 ||
  933. (bsizes & DMA_BURST32) == 0)
  934. bsizes = (DMA_BURST32 - 1);
  935. bp->bigmac_bursts = bsizes;
  936. /* Perform QEC initialization. */
  937. qec_init(bp);
  938. /* Map in the BigMAC channel registers. */
  939. bp->creg = of_ioremap(&op->resource[0], 0,
  940. CREG_REG_SIZE, "BigMAC QEC Channel Regs");
  941. if (!bp->creg) {
  942. printk(KERN_ERR "BIGMAC: Cannot map QEC channel registers.\n");
  943. goto fail_and_cleanup;
  944. }
  945. /* Map in the BigMAC control registers. */
  946. bp->bregs = of_ioremap(&op->resource[1], 0,
  947. BMAC_REG_SIZE, "BigMAC Primary Regs");
  948. if (!bp->bregs) {
  949. printk(KERN_ERR "BIGMAC: Cannot map BigMAC primary registers.\n");
  950. goto fail_and_cleanup;
  951. }
  952. /* Map in the BigMAC transceiver registers, this is how you poke at
  953. * the BigMAC's PHY.
  954. */
  955. bp->tregs = of_ioremap(&op->resource[2], 0,
  956. TCVR_REG_SIZE, "BigMAC Transceiver Regs");
  957. if (!bp->tregs) {
  958. printk(KERN_ERR "BIGMAC: Cannot map BigMAC transceiver registers.\n");
  959. goto fail_and_cleanup;
  960. }
  961. /* Stop the BigMAC. */
  962. bigmac_stop(bp);
  963. /* Allocate transmit/receive descriptor DVMA block. */
  964. bp->bmac_block = dma_alloc_coherent(&bp->bigmac_op->dev,
  965. PAGE_SIZE,
  966. &bp->bblock_dvma, GFP_ATOMIC);
  967. if (bp->bmac_block == NULL || bp->bblock_dvma == 0)
  968. goto fail_and_cleanup;
  969. /* Get the board revision of this BigMAC. */
  970. bp->board_rev = of_getintprop_default(bp->bigmac_op->dev.of_node,
  971. "board-version", 1);
  972. /* Init auto-negotiation timer state. */
  973. timer_setup(&bp->bigmac_timer, bigmac_timer, 0);
  974. bp->timer_state = asleep;
  975. bp->timer_ticks = 0;
  976. /* Backlink to generic net device struct. */
  977. bp->dev = dev;
  978. /* Set links to our BigMAC open and close routines. */
  979. dev->ethtool_ops = &bigmac_ethtool_ops;
  980. dev->netdev_ops = &bigmac_ops;
  981. dev->watchdog_timeo = 5*HZ;
  982. /* Finish net device registration. */
  983. dev->irq = bp->bigmac_op->archdata.irqs[0];
  984. dev->dma = 0;
  985. if (register_netdev(dev)) {
  986. printk(KERN_ERR "BIGMAC: Cannot register device.\n");
  987. goto fail_and_cleanup;
  988. }
  989. dev_set_drvdata(&bp->bigmac_op->dev, bp);
  990. printk(KERN_INFO "%s: BigMAC 100baseT Ethernet %pM\n",
  991. dev->name, dev->dev_addr);
  992. return 0;
  993. fail_and_cleanup:
  994. /* Something went wrong, undo whatever we did so far. */
  995. /* Free register mappings if any. */
  996. if (bp->gregs)
  997. of_iounmap(&qec_op->resource[0], bp->gregs, GLOB_REG_SIZE);
  998. if (bp->creg)
  999. of_iounmap(&op->resource[0], bp->creg, CREG_REG_SIZE);
  1000. if (bp->bregs)
  1001. of_iounmap(&op->resource[1], bp->bregs, BMAC_REG_SIZE);
  1002. if (bp->tregs)
  1003. of_iounmap(&op->resource[2], bp->tregs, TCVR_REG_SIZE);
  1004. if (bp->bmac_block)
  1005. dma_free_coherent(&bp->bigmac_op->dev,
  1006. PAGE_SIZE,
  1007. bp->bmac_block,
  1008. bp->bblock_dvma);
  1009. /* This also frees the co-located private data */
  1010. free_netdev(dev);
  1011. return -ENODEV;
  1012. }
  1013. /* QEC can be the parent of either QuadEthernet or a BigMAC. We want
  1014. * the latter.
  1015. */
  1016. static int bigmac_sbus_probe(struct platform_device *op)
  1017. {
  1018. struct device *parent = op->dev.parent;
  1019. struct platform_device *qec_op;
  1020. qec_op = to_platform_device(parent);
  1021. return bigmac_ether_init(op, qec_op);
  1022. }
  1023. static int bigmac_sbus_remove(struct platform_device *op)
  1024. {
  1025. struct bigmac *bp = platform_get_drvdata(op);
  1026. struct device *parent = op->dev.parent;
  1027. struct net_device *net_dev = bp->dev;
  1028. struct platform_device *qec_op;
  1029. qec_op = to_platform_device(parent);
  1030. unregister_netdev(net_dev);
  1031. of_iounmap(&qec_op->resource[0], bp->gregs, GLOB_REG_SIZE);
  1032. of_iounmap(&op->resource[0], bp->creg, CREG_REG_SIZE);
  1033. of_iounmap(&op->resource[1], bp->bregs, BMAC_REG_SIZE);
  1034. of_iounmap(&op->resource[2], bp->tregs, TCVR_REG_SIZE);
  1035. dma_free_coherent(&op->dev,
  1036. PAGE_SIZE,
  1037. bp->bmac_block,
  1038. bp->bblock_dvma);
  1039. free_netdev(net_dev);
  1040. return 0;
  1041. }
  1042. static const struct of_device_id bigmac_sbus_match[] = {
  1043. {
  1044. .name = "be",
  1045. },
  1046. {},
  1047. };
  1048. MODULE_DEVICE_TABLE(of, bigmac_sbus_match);
  1049. static struct platform_driver bigmac_sbus_driver = {
  1050. .driver = {
  1051. .name = "sunbmac",
  1052. .of_match_table = bigmac_sbus_match,
  1053. },
  1054. .probe = bigmac_sbus_probe,
  1055. .remove = bigmac_sbus_remove,
  1056. };
  1057. module_platform_driver(bigmac_sbus_driver);