You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

b44.c 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  1. /*
  2. * Copyright (c) 2008 Stefan Hajnoczi <stefanha@gmail.com>
  3. * Copyright (c) 2008 Pantelis Koukousoulas <pktoss@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of the
  8. * License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301, USA.
  19. *
  20. * This driver is a port of the b44 linux driver version 1.01
  21. *
  22. * Copyright (c) 2002 David S. Miller <davem@redhat.com>
  23. * Copyright (c) Pekka Pietikainen <pp@ee.oulu.fi>
  24. * Copyright (C) 2006 Broadcom Corporation.
  25. *
  26. * Some ssb bits copied from version 2.0 of the b44 driver
  27. * Copyright (c) Michael Buesch
  28. *
  29. * Copyright (c) a lot of people too. Please respect their work.
  30. */
  31. FILE_LICENCE ( GPL2_OR_LATER );
  32. #include <errno.h>
  33. #include <assert.h>
  34. #include <stdio.h>
  35. #include <unistd.h>
  36. #include <byteswap.h>
  37. #include <ipxe/io.h>
  38. #include <mii.h>
  39. #include <ipxe/iobuf.h>
  40. #include <ipxe/malloc.h>
  41. #include <ipxe/pci.h>
  42. #include <ipxe/netdevice.h>
  43. #include <ipxe/ethernet.h>
  44. #include <ipxe/if_ether.h>
  45. #include "b44.h"
  46. static inline int ring_next(int index)
  47. {
  48. /* B44_RING_SIZE is a power of 2 :) */
  49. return (index + 1) & (B44_RING_SIZE - 1);
  50. }
  51. /* Memory-mapped I/O wrappers */
  52. static inline u32 br32(const struct b44_private *bp, u32 reg)
  53. {
  54. return readl(bp->regs + reg);
  55. }
  56. static inline void bw32(const struct b44_private *bp, u32 reg, u32 val)
  57. {
  58. writel(val, bp->regs + reg);
  59. }
  60. static inline void bflush(const struct b44_private *bp, u32 reg, u32 timeout)
  61. {
  62. readl(bp->regs + reg);
  63. udelay(timeout);
  64. }
  65. #define VIRT_TO_B44(addr) ( virt_to_bus(addr) + SB_PCI_DMA )
  66. /**
  67. * Check if card can access address
  68. *
  69. * @v address Virtual address
  70. * @v address_ok Card can access address
  71. */
  72. static inline __attribute__ (( always_inline )) int
  73. b44_address_ok ( void *address ) {
  74. /* Card can address anything with a 30-bit address */
  75. if ( ( virt_to_bus ( address ) & ~B44_30BIT_DMA_MASK ) == 0 )
  76. return 1;
  77. return 0;
  78. }
  79. /**
  80. * Ring cells waiting to be processed are between 'tx_cur' and 'pending'
  81. * indexes in the ring.
  82. */
  83. static u32 pending_tx_index(struct b44_private *bp)
  84. {
  85. u32 pending = br32(bp, B44_DMATX_STAT);
  86. pending &= DMATX_STAT_CDMASK;
  87. pending /= sizeof(struct dma_desc);
  88. return pending & (B44_RING_SIZE - 1);
  89. }
  90. /**
  91. * Ring cells waiting to be processed are between 'rx_cur' and 'pending'
  92. * indexes in the ring.
  93. */
  94. static u32 pending_rx_index(struct b44_private *bp)
  95. {
  96. u32 pending = br32(bp, B44_DMARX_STAT);
  97. pending &= DMARX_STAT_CDMASK;
  98. pending /= sizeof(struct dma_desc);
  99. return pending & (B44_RING_SIZE - 1);
  100. }
  101. /**
  102. * Wait until the given bit is set/cleared.
  103. */
  104. static int b44_wait_bit(struct b44_private *bp, unsigned long reg, u32 bit,
  105. unsigned long timeout, const int clear)
  106. {
  107. unsigned long i;
  108. for (i = 0; i < timeout; i++) {
  109. u32 val = br32(bp, reg);
  110. if (clear && !(val & bit))
  111. break;
  112. if (!clear && (val & bit))
  113. break;
  114. udelay(10);
  115. }
  116. if (i == timeout) {
  117. return -ENODEV;
  118. }
  119. return 0;
  120. }
  121. /*
  122. * Sonics Silicon Backplane support. SSB is a mini-bus interconnecting
  123. * so-called IP Cores. One of those cores implements the Fast Ethernet
  124. * functionality and another one the PCI engine.
  125. *
  126. * You need to switch to the core you want to talk to before actually
  127. * sending commands.
  128. *
  129. * See: http://bcm-v4.sipsolutions.net/Backplane for (reverse-engineered)
  130. * specs.
  131. */
  132. static inline u32 ssb_get_core_rev(struct b44_private *bp)
  133. {
  134. return (br32(bp, B44_SBIDHIGH) & SBIDHIGH_RC_MASK);
  135. }
  136. static inline int ssb_is_core_up(struct b44_private *bp)
  137. {
  138. return ((br32(bp, B44_SBTMSLOW) & (SSB_CORE_DOWN | SBTMSLOW_CLOCK))
  139. == SBTMSLOW_CLOCK);
  140. }
  141. static u32 ssb_pci_setup(struct b44_private *bp, u32 cores)
  142. {
  143. u32 bar_orig, pci_rev, val;
  144. pci_read_config_dword(bp->pci, SSB_BAR0_WIN, &bar_orig);
  145. pci_write_config_dword(bp->pci, SSB_BAR0_WIN,
  146. BCM4400_PCI_CORE_ADDR);
  147. pci_rev = ssb_get_core_rev(bp);
  148. val = br32(bp, B44_SBINTVEC);
  149. val |= cores;
  150. bw32(bp, B44_SBINTVEC, val);
  151. val = br32(bp, SSB_PCI_TRANS_2);
  152. val |= SSB_PCI_PREF | SSB_PCI_BURST;
  153. bw32(bp, SSB_PCI_TRANS_2, val);
  154. pci_write_config_dword(bp->pci, SSB_BAR0_WIN, bar_orig);
  155. return pci_rev;
  156. }
  157. static void ssb_core_disable(struct b44_private *bp)
  158. {
  159. if (br32(bp, B44_SBTMSLOW) & SBTMSLOW_RESET)
  160. return;
  161. bw32(bp, B44_SBTMSLOW, (SBTMSLOW_REJECT | SBTMSLOW_CLOCK));
  162. b44_wait_bit(bp, B44_SBTMSLOW, SBTMSLOW_REJECT, 100000, 0);
  163. b44_wait_bit(bp, B44_SBTMSHIGH, SBTMSHIGH_BUSY, 100000, 1);
  164. bw32(bp, B44_SBTMSLOW, (SBTMSLOW_FGC | SBTMSLOW_CLOCK |
  165. SSB_CORE_DOWN));
  166. bflush(bp, B44_SBTMSLOW, 1);
  167. bw32(bp, B44_SBTMSLOW, SSB_CORE_DOWN);
  168. bflush(bp, B44_SBTMSLOW, 1);
  169. }
  170. static void ssb_core_reset(struct b44_private *bp)
  171. {
  172. u32 val;
  173. const u32 mask = (SBTMSLOW_CLOCK | SBTMSLOW_FGC | SBTMSLOW_RESET);
  174. ssb_core_disable(bp);
  175. bw32(bp, B44_SBTMSLOW, mask);
  176. bflush(bp, B44_SBTMSLOW, 1);
  177. /* Clear SERR if set, this is a hw bug workaround. */
  178. if (br32(bp, B44_SBTMSHIGH) & SBTMSHIGH_SERR)
  179. bw32(bp, B44_SBTMSHIGH, 0);
  180. val = br32(bp, B44_SBIMSTATE);
  181. if (val & (SBIMSTATE_BAD)) {
  182. bw32(bp, B44_SBIMSTATE, val & ~SBIMSTATE_BAD);
  183. }
  184. bw32(bp, B44_SBTMSLOW, (SBTMSLOW_CLOCK | SBTMSLOW_FGC));
  185. bflush(bp, B44_SBTMSLOW, 1);
  186. bw32(bp, B44_SBTMSLOW, (SBTMSLOW_CLOCK));
  187. bflush(bp, B44_SBTMSLOW, 1);
  188. }
  189. /*
  190. * Driver helper functions
  191. */
  192. /*
  193. * Chip reset provides power to the b44 MAC & PCI cores, which
  194. * is necessary for MAC register access. We only do a partial
  195. * reset in case of transmit/receive errors (ISTAT_ERRORS) to
  196. * avoid the chip being hung for an unnecessary long time in
  197. * this case.
  198. *
  199. * Called-by: b44_close, b44_halt, b44_inithw(b44_open), b44_probe
  200. */
  201. static void b44_chip_reset(struct b44_private *bp, int reset_kind)
  202. {
  203. if (ssb_is_core_up(bp)) {
  204. bw32(bp, B44_RCV_LAZY, 0);
  205. bw32(bp, B44_ENET_CTRL, ENET_CTRL_DISABLE);
  206. b44_wait_bit(bp, B44_ENET_CTRL, ENET_CTRL_DISABLE, 200, 1);
  207. bw32(bp, B44_DMATX_CTRL, 0);
  208. bp->tx_dirty = bp->tx_cur = 0;
  209. if (br32(bp, B44_DMARX_STAT) & DMARX_STAT_EMASK)
  210. b44_wait_bit(bp, B44_DMARX_STAT, DMARX_STAT_SIDLE,
  211. 100, 0);
  212. bw32(bp, B44_DMARX_CTRL, 0);
  213. bp->rx_cur = 0;
  214. } else {
  215. ssb_pci_setup(bp, SBINTVEC_ENET0);
  216. }
  217. ssb_core_reset(bp);
  218. /* Don't enable PHY if we are only doing a partial reset. */
  219. if (reset_kind == B44_CHIP_RESET_PARTIAL)
  220. return;
  221. /* Make PHY accessible. */
  222. bw32(bp, B44_MDIO_CTRL,
  223. (MDIO_CTRL_PREAMBLE | (0x0d & MDIO_CTRL_MAXF_MASK)));
  224. bflush(bp, B44_MDIO_CTRL, 1);
  225. /* Enable internal or external PHY */
  226. if (!(br32(bp, B44_DEVCTRL) & DEVCTRL_IPP)) {
  227. bw32(bp, B44_ENET_CTRL, ENET_CTRL_EPSEL);
  228. bflush(bp, B44_ENET_CTRL, 1);
  229. } else {
  230. u32 val = br32(bp, B44_DEVCTRL);
  231. if (val & DEVCTRL_EPR) {
  232. bw32(bp, B44_DEVCTRL, (val & ~DEVCTRL_EPR));
  233. bflush(bp, B44_DEVCTRL, 100);
  234. }
  235. }
  236. }
  237. /**
  238. * called by b44_poll in the error path
  239. */
  240. static void b44_halt(struct b44_private *bp)
  241. {
  242. /* disable ints */
  243. bw32(bp, B44_IMASK, 0);
  244. bflush(bp, B44_IMASK, 1);
  245. DBG("b44: powering down PHY\n");
  246. bw32(bp, B44_MAC_CTRL, MAC_CTRL_PHY_PDOWN);
  247. /*
  248. * Now reset the chip, but without enabling
  249. * the MAC&PHY part of it.
  250. * This has to be done _after_ we shut down the PHY
  251. */
  252. b44_chip_reset(bp, B44_CHIP_RESET_PARTIAL);
  253. }
  254. /*
  255. * Called at device open time to get the chip ready for
  256. * packet processing.
  257. *
  258. * Called-by: b44_open
  259. */
  260. static void b44_init_hw(struct b44_private *bp, int reset_kind)
  261. {
  262. u32 val;
  263. #define CTRL_MASK (DMARX_CTRL_ENABLE | (RX_PKT_OFFSET << DMARX_CTRL_ROSHIFT))
  264. b44_chip_reset(bp, B44_CHIP_RESET_FULL);
  265. if (reset_kind == B44_FULL_RESET) {
  266. b44_phy_reset(bp);
  267. }
  268. /* Enable CRC32, set proper LED modes and power on PHY */
  269. bw32(bp, B44_MAC_CTRL, MAC_CTRL_CRC32_ENAB | MAC_CTRL_PHY_LEDCTRL);
  270. bw32(bp, B44_RCV_LAZY, (1 << RCV_LAZY_FC_SHIFT));
  271. /* This sets the MAC address too. */
  272. b44_set_rx_mode(bp->netdev);
  273. /* MTU + eth header + possible VLAN tag + struct rx_header */
  274. bw32(bp, B44_RXMAXLEN, B44_MAX_MTU + ETH_HLEN + 8 + RX_HEADER_LEN);
  275. bw32(bp, B44_TXMAXLEN, B44_MAX_MTU + ETH_HLEN + 8 + RX_HEADER_LEN);
  276. bw32(bp, B44_TX_HIWMARK, TX_HIWMARK_DEFLT);
  277. if (reset_kind == B44_PARTIAL_RESET) {
  278. bw32(bp, B44_DMARX_CTRL, CTRL_MASK);
  279. } else {
  280. bw32(bp, B44_DMATX_CTRL, DMATX_CTRL_ENABLE);
  281. bw32(bp, B44_DMATX_ADDR, VIRT_TO_B44(bp->tx));
  282. bw32(bp, B44_DMARX_CTRL, CTRL_MASK);
  283. bw32(bp, B44_DMARX_ADDR, VIRT_TO_B44(bp->rx));
  284. bw32(bp, B44_DMARX_PTR, B44_RX_RING_LEN_BYTES);
  285. bw32(bp, B44_MIB_CTRL, MIB_CTRL_CLR_ON_READ);
  286. }
  287. val = br32(bp, B44_ENET_CTRL);
  288. bw32(bp, B44_ENET_CTRL, (val | ENET_CTRL_ENABLE));
  289. #undef CTRL_MASK
  290. }
  291. /*** Management of ring descriptors ***/
  292. static void b44_populate_rx_descriptor(struct b44_private *bp, u32 idx)
  293. {
  294. struct rx_header *rh;
  295. u32 ctrl, addr;
  296. rh = bp->rx_iobuf[idx]->data;
  297. rh->len = 0;
  298. rh->flags = 0;
  299. ctrl = DESC_CTRL_LEN & (RX_PKT_BUF_SZ - RX_PKT_OFFSET);
  300. if (idx == B44_RING_LAST) {
  301. ctrl |= DESC_CTRL_EOT;
  302. }
  303. addr = VIRT_TO_B44(bp->rx_iobuf[idx]->data);
  304. bp->rx[idx].ctrl = cpu_to_le32(ctrl);
  305. bp->rx[idx].addr = cpu_to_le32(addr);
  306. bw32(bp, B44_DMARX_PTR, idx * sizeof(struct dma_desc));
  307. }
  308. /*
  309. * Refill RX ring descriptors with buffers. This is needed
  310. * because during rx we are passing ownership of descriptor
  311. * buffers to the network stack.
  312. */
  313. static void b44_rx_refill(struct b44_private *bp, u32 pending)
  314. {
  315. struct io_buffer *iobuf;
  316. u32 i;
  317. // skip pending
  318. for (i = pending + 1; i != bp->rx_cur; i = ring_next(i)) {
  319. if (bp->rx_iobuf[i] != NULL)
  320. continue;
  321. iobuf = alloc_iob(RX_PKT_BUF_SZ);
  322. if (!iobuf) {
  323. DBG("Refill rx ring failed!!\n");
  324. break;
  325. }
  326. if (!b44_address_ok(iobuf->data)) {
  327. DBG("Refill rx ring bad address!!\n");
  328. free_iob(iobuf);
  329. break;
  330. }
  331. bp->rx_iobuf[i] = iobuf;
  332. b44_populate_rx_descriptor(bp, i);
  333. }
  334. }
  335. static void b44_free_rx_ring(struct b44_private *bp)
  336. {
  337. u32 i;
  338. if (bp->rx) {
  339. for (i = 0; i < B44_RING_SIZE; i++) {
  340. free_iob(bp->rx_iobuf[i]);
  341. bp->rx_iobuf[i] = NULL;
  342. }
  343. free_dma(bp->rx, B44_RX_RING_LEN_BYTES);
  344. bp->rx = NULL;
  345. }
  346. }
  347. static int b44_init_rx_ring(struct b44_private *bp)
  348. {
  349. b44_free_rx_ring(bp);
  350. bp->rx = malloc_dma(B44_RX_RING_LEN_BYTES, B44_DMA_ALIGNMENT);
  351. if (!bp->rx)
  352. return -ENOMEM;
  353. if (!b44_address_ok(bp->rx)) {
  354. free_dma(bp->rx, B44_RX_RING_LEN_BYTES);
  355. return -ENOTSUP;
  356. }
  357. memset(bp->rx_iobuf, 0, sizeof(bp->rx_iobuf));
  358. bp->rx_iobuf[0] = alloc_iob(RX_PKT_BUF_SZ);
  359. b44_populate_rx_descriptor(bp, 0);
  360. b44_rx_refill(bp, 0);
  361. DBG("Init RX rings: rx=0x%08lx\n", VIRT_TO_B44(bp->rx));
  362. return 0;
  363. }
  364. static void b44_free_tx_ring(struct b44_private *bp)
  365. {
  366. if (bp->tx) {
  367. free_dma(bp->tx, B44_TX_RING_LEN_BYTES);
  368. bp->tx = NULL;
  369. }
  370. }
  371. static int b44_init_tx_ring(struct b44_private *bp)
  372. {
  373. b44_free_tx_ring(bp);
  374. bp->tx = malloc_dma(B44_TX_RING_LEN_BYTES, B44_DMA_ALIGNMENT);
  375. if (!bp->tx)
  376. return -ENOMEM;
  377. if (!b44_address_ok(bp->tx)) {
  378. free_dma(bp->tx, B44_TX_RING_LEN_BYTES);
  379. return -ENOTSUP;
  380. }
  381. memset(bp->tx, 0, B44_TX_RING_LEN_BYTES);
  382. memset(bp->tx_iobuf, 0, sizeof(bp->tx_iobuf));
  383. DBG("Init TX rings: tx=0x%08lx\n", VIRT_TO_B44(bp->tx));
  384. return 0;
  385. }
  386. /*** Interaction with the PHY ***/
  387. static int b44_phy_read(struct b44_private *bp, int reg, u32 * val)
  388. {
  389. int err;
  390. u32 arg1 = (MDIO_OP_READ << MDIO_DATA_OP_SHIFT);
  391. u32 arg2 = (bp->phy_addr << MDIO_DATA_PMD_SHIFT);
  392. u32 arg3 = (reg << MDIO_DATA_RA_SHIFT);
  393. u32 arg4 = (MDIO_TA_VALID << MDIO_DATA_TA_SHIFT);
  394. u32 argv = arg1 | arg2 | arg3 | arg4;
  395. bw32(bp, B44_EMAC_ISTAT, EMAC_INT_MII);
  396. bw32(bp, B44_MDIO_DATA, (MDIO_DATA_SB_START | argv));
  397. err = b44_wait_bit(bp, B44_EMAC_ISTAT, EMAC_INT_MII, 100, 0);
  398. *val = br32(bp, B44_MDIO_DATA) & MDIO_DATA_DATA;
  399. return err;
  400. }
  401. static int b44_phy_write(struct b44_private *bp, int reg, u32 val)
  402. {
  403. u32 arg1 = (MDIO_OP_WRITE << MDIO_DATA_OP_SHIFT);
  404. u32 arg2 = (bp->phy_addr << MDIO_DATA_PMD_SHIFT);
  405. u32 arg3 = (reg << MDIO_DATA_RA_SHIFT);
  406. u32 arg4 = (MDIO_TA_VALID << MDIO_DATA_TA_SHIFT);
  407. u32 arg5 = (val & MDIO_DATA_DATA);
  408. u32 argv = arg1 | arg2 | arg3 | arg4 | arg5;
  409. bw32(bp, B44_EMAC_ISTAT, EMAC_INT_MII);
  410. bw32(bp, B44_MDIO_DATA, (MDIO_DATA_SB_START | argv));
  411. return b44_wait_bit(bp, B44_EMAC_ISTAT, EMAC_INT_MII, 100, 0);
  412. }
  413. static int b44_phy_reset(struct b44_private *bp)
  414. {
  415. u32 val;
  416. int err;
  417. err = b44_phy_write(bp, MII_BMCR, BMCR_RESET);
  418. if (err)
  419. return err;
  420. udelay(100);
  421. err = b44_phy_read(bp, MII_BMCR, &val);
  422. if (!err) {
  423. if (val & BMCR_RESET) {
  424. return -ENODEV;
  425. }
  426. }
  427. return 0;
  428. }
  429. /*
  430. * The BCM44xx CAM (Content Addressable Memory) stores the MAC
  431. * and PHY address.
  432. */
  433. static void b44_cam_write(struct b44_private *bp, unsigned char *data,
  434. int index)
  435. {
  436. u32 val;
  437. val = ((u32) data[2]) << 24;
  438. val |= ((u32) data[3]) << 16;
  439. val |= ((u32) data[4]) << 8;
  440. val |= ((u32) data[5]) << 0;
  441. bw32(bp, B44_CAM_DATA_LO, val);
  442. val = (CAM_DATA_HI_VALID |
  443. (((u32) data[0]) << 8) | (((u32) data[1]) << 0));
  444. bw32(bp, B44_CAM_DATA_HI, val);
  445. val = CAM_CTRL_WRITE | (index << CAM_CTRL_INDEX_SHIFT);
  446. bw32(bp, B44_CAM_CTRL, val);
  447. b44_wait_bit(bp, B44_CAM_CTRL, CAM_CTRL_BUSY, 100, 1);
  448. }
  449. static void b44_set_mac_addr(struct b44_private *bp)
  450. {
  451. u32 val;
  452. bw32(bp, B44_CAM_CTRL, 0);
  453. b44_cam_write(bp, bp->netdev->ll_addr, 0);
  454. val = br32(bp, B44_CAM_CTRL);
  455. bw32(bp, B44_CAM_CTRL, val | CAM_CTRL_ENABLE);
  456. }
  457. /* Read 128-bytes of EEPROM. */
  458. static void b44_read_eeprom(struct b44_private *bp, u8 * data)
  459. {
  460. long i;
  461. u16 *ptr = (u16 *) data;
  462. for (i = 0; i < 128; i += 2)
  463. ptr[i / 2] = cpu_to_le16(readw(bp->regs + 4096 + i));
  464. }
  465. static void b44_load_mac_and_phy_addr(struct b44_private *bp)
  466. {
  467. u8 eeprom[128];
  468. /* Load MAC address, note byteswapping */
  469. b44_read_eeprom(bp, &eeprom[0]);
  470. bp->netdev->hw_addr[0] = eeprom[79];
  471. bp->netdev->hw_addr[1] = eeprom[78];
  472. bp->netdev->hw_addr[2] = eeprom[81];
  473. bp->netdev->hw_addr[3] = eeprom[80];
  474. bp->netdev->hw_addr[4] = eeprom[83];
  475. bp->netdev->hw_addr[5] = eeprom[82];
  476. /* Load PHY address */
  477. bp->phy_addr = eeprom[90] & 0x1f;
  478. }
  479. static void b44_set_rx_mode(struct net_device *netdev)
  480. {
  481. struct b44_private *bp = netdev_priv(netdev);
  482. unsigned char zero[6] = { 0, 0, 0, 0, 0, 0 };
  483. u32 val;
  484. int i;
  485. val = br32(bp, B44_RXCONFIG);
  486. val &= ~RXCONFIG_PROMISC;
  487. val |= RXCONFIG_ALLMULTI;
  488. b44_set_mac_addr(bp);
  489. for (i = 1; i < 64; i++)
  490. b44_cam_write(bp, zero, i);
  491. bw32(bp, B44_RXCONFIG, val);
  492. val = br32(bp, B44_CAM_CTRL);
  493. bw32(bp, B44_CAM_CTRL, val | CAM_CTRL_ENABLE);
  494. }
  495. /*** Implementation of iPXE driver callbacks ***/
  496. /**
  497. * Probe device
  498. *
  499. * @v pci PCI device
  500. * @v id Matching entry in ID table
  501. * @ret rc Return status code
  502. */
  503. static int b44_probe(struct pci_device *pci)
  504. {
  505. struct net_device *netdev;
  506. struct b44_private *bp;
  507. int rc;
  508. /* Set up netdev */
  509. netdev = alloc_etherdev(sizeof(*bp));
  510. if (!netdev)
  511. return -ENOMEM;
  512. netdev_init(netdev, &b44_operations);
  513. pci_set_drvdata(pci, netdev);
  514. netdev->dev = &pci->dev;
  515. /* Set up private data */
  516. bp = netdev_priv(netdev);
  517. memset(bp, 0, sizeof(*bp));
  518. bp->netdev = netdev;
  519. bp->pci = pci;
  520. /* Map device registers */
  521. bp->regs = ioremap(pci->membase, B44_REGS_SIZE);
  522. if (!bp->regs) {
  523. netdev_put(netdev);
  524. return -ENOMEM;
  525. }
  526. /* Enable PCI bus mastering */
  527. adjust_pci_device(pci);
  528. b44_load_mac_and_phy_addr(bp);
  529. rc = register_netdev(netdev);
  530. if (rc != 0) {
  531. iounmap(bp->regs);
  532. netdev_put(netdev);
  533. return rc;
  534. }
  535. /* Link management currently not implemented */
  536. netdev_link_up(netdev);
  537. b44_chip_reset(bp, B44_CHIP_RESET_FULL);
  538. DBG("b44 %s (%04x:%04x) regs=%p MAC=%s\n", pci->id->name,
  539. pci->id->vendor, pci->id->device, bp->regs,
  540. eth_ntoa(netdev->ll_addr));
  541. return 0;
  542. }
  543. /**
  544. * Remove device
  545. *
  546. * @v pci PCI device
  547. */
  548. static void b44_remove(struct pci_device *pci)
  549. {
  550. struct net_device *netdev = pci_get_drvdata(pci);
  551. struct b44_private *bp = netdev_priv(netdev);
  552. ssb_core_disable(bp);
  553. unregister_netdev(netdev);
  554. iounmap(bp->regs);
  555. netdev_nullify(netdev);
  556. netdev_put(netdev);
  557. }
  558. /** Enable or disable interrupts
  559. *
  560. * @v netdev Network device
  561. * @v enable Interrupts should be enabled
  562. */
  563. static void b44_irq(struct net_device *netdev, int enable)
  564. {
  565. struct b44_private *bp = netdev_priv(netdev);
  566. /* Interrupt mask specifies which events generate interrupts */
  567. bw32(bp, B44_IMASK, enable ? IMASK_DEF : IMASK_DISABLE);
  568. }
  569. /** Open network device
  570. *
  571. * @v netdev Network device
  572. * @ret rc Return status code
  573. */
  574. static int b44_open(struct net_device *netdev)
  575. {
  576. struct b44_private *bp = netdev_priv(netdev);
  577. int rc;
  578. rc = b44_init_tx_ring(bp);
  579. if (rc != 0)
  580. return rc;
  581. rc = b44_init_rx_ring(bp);
  582. if (rc != 0)
  583. return rc;
  584. b44_init_hw(bp, B44_FULL_RESET);
  585. /* Disable interrupts */
  586. b44_irq(netdev, 0);
  587. return 0;
  588. }
  589. /** Close network device
  590. *
  591. * @v netdev Network device
  592. */
  593. static void b44_close(struct net_device *netdev)
  594. {
  595. struct b44_private *bp = netdev_priv(netdev);
  596. b44_chip_reset(bp, B44_FULL_RESET);
  597. b44_free_tx_ring(bp);
  598. b44_free_rx_ring(bp);
  599. }
  600. /** Transmit packet
  601. *
  602. * @v netdev Network device
  603. * @v iobuf I/O buffer
  604. * @ret rc Return status code
  605. */
  606. static int b44_transmit(struct net_device *netdev, struct io_buffer *iobuf)
  607. {
  608. struct b44_private *bp = netdev_priv(netdev);
  609. u32 cur = bp->tx_cur;
  610. u32 ctrl;
  611. /* Check for TX ring overflow */
  612. if (bp->tx[cur].ctrl) {
  613. DBG("tx overflow\n");
  614. return -ENOBUFS;
  615. }
  616. /* Check for addressability */
  617. if (!b44_address_ok(iobuf->data))
  618. return -ENOTSUP;
  619. /* Will call netdev_tx_complete() on the iobuf later */
  620. bp->tx_iobuf[cur] = iobuf;
  621. /* Set up TX descriptor */
  622. ctrl = (iob_len(iobuf) & DESC_CTRL_LEN) |
  623. DESC_CTRL_IOC | DESC_CTRL_SOF | DESC_CTRL_EOF;
  624. if (cur == B44_RING_LAST)
  625. ctrl |= DESC_CTRL_EOT;
  626. bp->tx[cur].ctrl = cpu_to_le32(ctrl);
  627. bp->tx[cur].addr = cpu_to_le32(VIRT_TO_B44(iobuf->data));
  628. /* Update next available descriptor index */
  629. cur = ring_next(cur);
  630. bp->tx_cur = cur;
  631. wmb();
  632. /* Tell card that a new TX descriptor is ready */
  633. bw32(bp, B44_DMATX_PTR, cur * sizeof(struct dma_desc));
  634. return 0;
  635. }
  636. /** Recycles sent TX descriptors and notifies network stack
  637. *
  638. * @v bp Driver state
  639. */
  640. static void b44_tx_complete(struct b44_private *bp)
  641. {
  642. u32 cur, i;
  643. cur = pending_tx_index(bp);
  644. for (i = bp->tx_dirty; i != cur; i = ring_next(i)) {
  645. /* Free finished frame */
  646. netdev_tx_complete(bp->netdev, bp->tx_iobuf[i]);
  647. bp->tx_iobuf[i] = NULL;
  648. /* Clear TX descriptor */
  649. bp->tx[i].ctrl = 0;
  650. bp->tx[i].addr = 0;
  651. }
  652. bp->tx_dirty = cur;
  653. }
  654. static void b44_process_rx_packets(struct b44_private *bp)
  655. {
  656. struct io_buffer *iob; /* received data */
  657. struct rx_header *rh;
  658. u32 pending, i;
  659. u16 len;
  660. pending = pending_rx_index(bp);
  661. for (i = bp->rx_cur; i != pending; i = ring_next(i)) {
  662. iob = bp->rx_iobuf[i];
  663. if (iob == NULL)
  664. break;
  665. rh = iob->data;
  666. len = le16_to_cpu(rh->len);
  667. /*
  668. * Guard against incompletely written RX descriptors.
  669. * Without this, things can get really slow!
  670. */
  671. if (len == 0)
  672. break;
  673. /* Discard CRC that is generated by the card */
  674. len -= 4;
  675. /* Check for invalid packets and errors */
  676. if (len > RX_PKT_BUF_SZ - RX_PKT_OFFSET ||
  677. (rh->flags & cpu_to_le16(RX_FLAG_ERRORS))) {
  678. DBG("rx error len=%d flags=%04x\n", len,
  679. cpu_to_le16(rh->flags));
  680. rh->len = 0;
  681. rh->flags = 0;
  682. netdev_rx_err(bp->netdev, iob, -EINVAL);
  683. continue;
  684. }
  685. /* Clear RX descriptor */
  686. rh->len = 0;
  687. rh->flags = 0;
  688. bp->rx_iobuf[i] = NULL;
  689. /* Hand off the IO buffer to the network stack */
  690. iob_reserve(iob, RX_PKT_OFFSET);
  691. iob_put(iob, len);
  692. netdev_rx(bp->netdev, iob);
  693. }
  694. bp->rx_cur = i;
  695. b44_rx_refill(bp, pending_rx_index(bp));
  696. }
  697. /** Poll for completed and received packets
  698. *
  699. * @v netdev Network device
  700. */
  701. static void b44_poll(struct net_device *netdev)
  702. {
  703. struct b44_private *bp = netdev_priv(netdev);
  704. u32 istat;
  705. /* Interrupt status */
  706. istat = br32(bp, B44_ISTAT);
  707. istat &= IMASK_DEF; /* only the events we care about */
  708. if (!istat)
  709. return;
  710. if (istat & ISTAT_TX)
  711. b44_tx_complete(bp);
  712. if (istat & ISTAT_RX)
  713. b44_process_rx_packets(bp);
  714. if (istat & ISTAT_ERRORS) {
  715. DBG("b44 error istat=0x%08x\n", istat);
  716. /* Reset B44 core partially to avoid long waits */
  717. b44_irq(bp->netdev, 0);
  718. b44_halt(bp);
  719. b44_init_tx_ring(bp);
  720. b44_init_rx_ring(bp);
  721. b44_init_hw(bp, B44_FULL_RESET_SKIP_PHY);
  722. }
  723. /* Acknowledge interrupt */
  724. bw32(bp, B44_ISTAT, 0);
  725. bflush(bp, B44_ISTAT, 1);
  726. }
  727. static struct net_device_operations b44_operations = {
  728. .open = b44_open,
  729. .close = b44_close,
  730. .transmit = b44_transmit,
  731. .poll = b44_poll,
  732. .irq = b44_irq,
  733. };
  734. static struct pci_device_id b44_nics[] = {
  735. PCI_ROM(0x14e4, 0x4401, "BCM4401", "BCM4401", 0),
  736. PCI_ROM(0x14e4, 0x170c, "BCM4401-B0", "BCM4401-B0", 0),
  737. PCI_ROM(0x14e4, 0x4402, "BCM4401-B1", "BCM4401-B1", 0),
  738. };
  739. struct pci_driver b44_driver __pci_driver = {
  740. .ids = b44_nics,
  741. .id_count = sizeof b44_nics / sizeof b44_nics[0],
  742. .probe = b44_probe,
  743. .remove = b44_remove,
  744. };