Lines Matching refs:sc

66 #define RT2560_RSSI(sc, rssi)					\
67 ((rssi) > (RT2560_NOISE_FLOOR + (sc)->rssi_corr) ? \
68 ((rssi) - RT2560_NOISE_FLOOR - (sc)->rssi_corr) : 0)
72 #define DPRINTF(sc, fmt, ...) do { \
73 if (sc->sc_debug > 0) \
76 #define DPRINTFN(sc, n, fmt, ...) do { \
77 if (sc->sc_debug >= (n)) \
81 #define DPRINTF(sc, fmt, ...)
82 #define DPRINTFN(sc, n, fmt, ...)
204 struct rt2560_softc *sc = device_get_softc(dev);
205 struct ieee80211com *ic = &sc->sc_ic;
208 sc->sc_dev = dev;
210 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
213 callout_init_mtx(&sc->watchdog_ch, &sc->sc_mtx, 0);
214 mbufq_init(&sc->sc_snd, ifqmaxlen);
217 sc->asic_rev = RAL_READ(sc, RT2560_CSR0);
220 rt2560_read_config(sc);
223 sc->asic_rev, rt2560_get_rf(sc->rf_rev));
228 error = rt2560_alloc_tx_ring(sc, &sc->txq, RT2560_TX_RING_COUNT);
230 device_printf(sc->sc_dev, "could not allocate Tx ring\n");
234 error = rt2560_alloc_tx_ring(sc, &sc->atimq, RT2560_ATIM_RING_COUNT);
236 device_printf(sc->sc_dev, "could not allocate ATIM ring\n");
240 error = rt2560_alloc_tx_ring(sc, &sc->prioq, RT2560_PRIO_RING_COUNT);
242 device_printf(sc->sc_dev, "could not allocate Prio ring\n");
246 error = rt2560_alloc_tx_ring(sc, &sc->bcnq, RT2560_BEACON_RING_COUNT);
248 device_printf(sc->sc_dev, "could not allocate Beacon ring\n");
252 error = rt2560_alloc_rx_ring(sc, &sc->rxq, RT2560_RX_RING_COUNT);
254 device_printf(sc->sc_dev, "could not allocate Rx ring\n");
259 rt2560_get_macaddr(sc, ic->ic_macaddr);
261 ic->ic_softc = sc;
302 &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap),
304 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap),
313 "debug", CTLFLAG_RW, &sc->sc_debug, 0, "debug msgs");
317 "txantenna", CTLFLAG_RW, &sc->tx_ant, 0, "tx antenna (0=auto)");
321 "rxantenna", CTLFLAG_RW, &sc->rx_ant, 0, "rx antenna (0=auto)");
328 fail5: rt2560_free_tx_ring(sc, &sc->bcnq);
329 fail4: rt2560_free_tx_ring(sc, &sc->prioq);
330 fail3: rt2560_free_tx_ring(sc, &sc->atimq);
331 fail2: rt2560_free_tx_ring(sc, &sc->txq);
332 fail1: mtx_destroy(&sc->sc_mtx);
340 struct rt2560_softc *sc = xsc;
341 struct ieee80211com *ic = &sc->sc_ic;
343 rt2560_stop(sc);
346 mbufq_drain(&sc->sc_snd);
348 rt2560_free_tx_ring(sc, &sc->txq);
349 rt2560_free_tx_ring(sc, &sc->atimq);
350 rt2560_free_tx_ring(sc, &sc->prioq);
351 rt2560_free_tx_ring(sc, &sc->bcnq);
352 rt2560_free_rx_ring(sc, &sc->rxq);
354 mtx_destroy(&sc->sc_mtx);
365 struct rt2560_softc *sc = ic->ic_softc;
378 device_printf(sc->sc_dev, "only 1 vap supported\n");
387 device_printf(sc->sc_dev,
399 device_printf(sc->sc_dev, "unknown opmode %d\n", opmode);
433 struct rt2560_softc *sc = xsc;
435 if (sc->sc_ic.ic_nrunning > 0)
436 rt2560_init(sc);
451 rt2560_alloc_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring,
461 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 4, 0,
466 device_printf(sc->sc_dev, "could not create desc DMA tag\n");
473 device_printf(sc->sc_dev, "could not allocate DMA memory\n");
481 device_printf(sc->sc_dev, "could not load desc DMA map\n");
488 device_printf(sc->sc_dev, "could not allocate soft data\n");
493 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
498 device_printf(sc->sc_dev, "could not create data DMA tag\n");
506 device_printf(sc->sc_dev, "could not create DMA map\n");
513 fail: rt2560_free_tx_ring(sc, ring);
518 rt2560_reset_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring)
552 rt2560_free_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring)
593 rt2560_alloc_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring,
605 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 4, 0,
610 device_printf(sc->sc_dev, "could not create desc DMA tag\n");
617 device_printf(sc->sc_dev, "could not allocate DMA memory\n");
625 device_printf(sc->sc_dev, "could not load desc DMA map\n");
632 device_printf(sc->sc_dev, "could not allocate soft data\n");
640 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0,
644 device_printf(sc->sc_dev, "could not create data DMA tag\n");
649 desc = &sc->rxq.desc[i];
650 data = &sc->rxq.data[i];
654 device_printf(sc->sc_dev, "could not create DMA map\n");
660 device_printf(sc->sc_dev,
670 device_printf(sc->sc_dev,
683 fail: rt2560_free_rx_ring(sc, ring);
688 rt2560_reset_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring)
704 rt2560_free_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring)
745 struct rt2560_softc *sc = vap->iv_ic->ic_softc;
750 RAL_WRITE(sc, RT2560_CSR14, 0);
753 rt2560_update_led(sc, 0, 0);
763 rt2560_update_plcp(sc);
764 rt2560_set_basicrates(sc, &ni->ni_rates);
765 rt2560_set_bssid(sc, ni->ni_bssid);
773 device_printf(sc->sc_dev,
778 error = rt2560_tx_bcn(sc, m, ni);
784 rt2560_update_led(sc, 1, 0);
787 rt2560_enable_tsf_sync(sc);
789 rt2560_enable_tsf(sc);
799 rt2560_eeprom_read(struct rt2560_softc *sc, uint8_t addr)
806 RT2560_EEPROM_CTL(sc, 0);
808 RT2560_EEPROM_CTL(sc, RT2560_S);
809 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C);
810 RT2560_EEPROM_CTL(sc, RT2560_S);
813 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D);
814 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D | RT2560_C);
817 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D);
818 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D | RT2560_C);
819 RT2560_EEPROM_CTL(sc, RT2560_S);
820 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C);
823 n = (RAL_READ(sc, RT2560_CSR21) & RT2560_93C46) ? 5 : 7;
825 RT2560_EEPROM_CTL(sc, RT2560_S |
827 RT2560_EEPROM_CTL(sc, RT2560_S |
831 RT2560_EEPROM_CTL(sc, RT2560_S);
836 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C);
837 tmp = RAL_READ(sc, RT2560_CSR21);
839 RT2560_EEPROM_CTL(sc, RT2560_S);
842 RT2560_EEPROM_CTL(sc, 0);
845 RT2560_EEPROM_CTL(sc, RT2560_S);
846 RT2560_EEPROM_CTL(sc, 0);
847 RT2560_EEPROM_CTL(sc, RT2560_C);
857 rt2560_encryption_intr(struct rt2560_softc *sc)
863 hw = RAL_READ(sc, RT2560_SECCSR1) - sc->txq.physaddr;
866 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
869 while (sc->txq.next_encrypt != hw) {
870 if (sc->txq.next_encrypt == sc->txq.cur_encrypt) {
872 sc->txq.cur_encrypt);
876 desc = &sc->txq.desc[sc->txq.next_encrypt];
891 DPRINTFN(sc, 15, "encryption done idx=%u\n",
892 sc->txq.next_encrypt);
894 sc->txq.next_encrypt =
895 (sc->txq.next_encrypt + 1) % RT2560_TX_RING_COUNT;
898 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
902 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_TX);
906 rt2560_tx_intr(struct rt2560_softc *sc)
908 struct ieee80211_ratectl_tx_status *txs = &sc->sc_txs;
916 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
921 desc = &sc->txq.desc[sc->txq.next];
922 data = &sc->txq.data[sc->txq.next];
938 DPRINTFN(sc, 10, "%s\n", "data frame sent successfully");
948 DPRINTFN(sc, 9, "data frame sent after %u retries\n",
959 DPRINTFN(sc, 9, "data frame failed after %d retries\n",
969 device_printf(sc->sc_dev, "sending data frame failed "
974 bus_dmamap_sync(sc->txq.data_dmat, data->map,
976 bus_dmamap_unload(sc->txq.data_dmat, data->map);
985 DPRINTFN(sc, 15, "tx done idx=%u\n", sc->txq.next);
987 sc->txq.queued--;
988 sc->txq.next = (sc->txq.next + 1) % RT2560_TX_RING_COUNT;
991 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
994 if (sc->prioq.queued == 0 && sc->txq.queued == 0)
995 sc->sc_tx_timer = 0;
997 if (sc->txq.queued < RT2560_TX_RING_COUNT - 1)
998 rt2560_start(sc);
1002 rt2560_prio_intr(struct rt2560_softc *sc)
1010 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map,
1014 desc = &sc->prioq.desc[sc->prioq.next];
1015 data = &sc->prioq.data[sc->prioq.next];
1023 DPRINTFN(sc, 10, "%s\n", "mgt frame sent successfully");
1027 DPRINTFN(sc, 9, "mgt frame sent after %u retries\n",
1032 DPRINTFN(sc, 9, "%s\n",
1039 device_printf(sc->sc_dev, "sending mgt frame failed "
1044 bus_dmamap_sync(sc->prioq.data_dmat, data->map,
1046 bus_dmamap_unload(sc->prioq.data_dmat, data->map);
1056 DPRINTFN(sc, 15, "prio done idx=%u\n", sc->prioq.next);
1058 sc->prioq.queued--;
1059 sc->prioq.next = (sc->prioq.next + 1) % RT2560_PRIO_RING_COUNT;
1069 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map,
1072 if (sc->prioq.queued == 0 && sc->txq.queued == 0)
1073 sc->sc_tx_timer = 0;
1075 if (sc->prioq.queued < RT2560_PRIO_RING_COUNT)
1076 rt2560_start(sc);
1084 rt2560_decryption_intr(struct rt2560_softc *sc)
1086 struct ieee80211com *ic = &sc->sc_ic;
1097 hw = RAL_READ(sc, RT2560_SECCSR0) - sc->rxq.physaddr;
1100 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1103 for (; sc->rxq.cur_decrypt != hw;) {
1104 desc = &sc->rxq.desc[sc->rxq.cur_decrypt];
1105 data = &sc->rxq.data[sc->rxq.cur_decrypt];
1135 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
1137 bus_dmamap_unload(sc->rxq.data_dmat, data->map);
1139 error = bus_dmamap_load(sc->rxq.data_dmat, data->map,
1146 error = bus_dmamap_load(sc->rxq.data_dmat, data->map,
1152 device_get_name(sc->sc_dev));
1170 rssi = RT2560_RSSI(sc, desc->rssi);
1173 struct rt2560_rx_radiotap_header *tap = &sc->sc_rxtap;
1177 tsf_hi = RAL_READ(sc, RT2560_CSR17);
1178 tsf_lo = RAL_READ(sc, RT2560_CSR16);
1186 tap->wr_antenna = sc->rx_ant;
1191 sc->sc_flags |= RT2560_F_INPUT_RUNNING;
1192 RAL_UNLOCK(sc);
1202 RAL_LOCK(sc);
1203 sc->sc_flags &= ~RT2560_F_INPUT_RUNNING;
1206 DPRINTFN(sc, 15, "decryption done idx=%u\n", sc->rxq.cur_decrypt);
1208 sc->rxq.cur_decrypt =
1209 (sc->rxq.cur_decrypt + 1) % RT2560_RX_RING_COUNT;
1212 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1221 rt2560_rx_intr(struct rt2560_softc *sc)
1226 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1230 desc = &sc->rxq.desc[sc->rxq.cur];
1231 data = &sc->rxq.data[sc->rxq.cur];
1245 DPRINTFN(sc, 5, "PHY or CRC error flags 0x%08x\n",
1251 DPRINTFN(sc, 5, "%s\n", "bad length");
1258 DPRINTFN(sc, 15, "rx done idx=%u\n", sc->rxq.cur);
1260 sc->rxq.cur = (sc->rxq.cur + 1) % RT2560_RX_RING_COUNT;
1263 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1267 RAL_WRITE(sc, RT2560_SECCSR0, RT2560_KICK_DECRYPT);
1283 rt2560_beacon_expire(struct rt2560_softc *sc)
1285 struct ieee80211com *ic = &sc->sc_ic;
1293 data = &sc->bcnq.data[sc->bcnq.next];
1300 bus_dmamap_sync(sc->bcnq.data_dmat, data->map, BUS_DMASYNC_POSTWRITE);
1301 bus_dmamap_unload(sc->bcnq.data_dmat, data->map);
1306 rt2560_tx_bcn(sc, data->m, data->ni);
1308 DPRINTFN(sc, 15, "%s", "beacon expired\n");
1310 sc->bcnq.next = (sc->bcnq.next + 1) % RT2560_BEACON_RING_COUNT;
1315 rt2560_wakeup_expire(struct rt2560_softc *sc)
1317 DPRINTFN(sc, 2, "%s", "wakeup expired\n");
1323 struct rt2560_softc *sc = arg;
1326 RAL_LOCK(sc);
1329 RAL_WRITE(sc, RT2560_CSR8, 0xffffffff);
1332 if (!(sc->sc_flags & RT2560_F_RUNNING)) {
1333 RAL_UNLOCK(sc);
1337 r = RAL_READ(sc, RT2560_CSR7);
1338 RAL_WRITE(sc, RT2560_CSR7, r);
1341 rt2560_beacon_expire(sc);
1344 rt2560_wakeup_expire(sc);
1347 rt2560_encryption_intr(sc);
1350 rt2560_tx_intr(sc);
1353 rt2560_prio_intr(sc);
1356 rt2560_decryption_intr(sc);
1359 rt2560_rx_intr(sc);
1360 rt2560_encryption_intr(sc);
1364 RAL_WRITE(sc, RT2560_CSR8, RT2560_INTR_MASK);
1366 RAL_UNLOCK(sc);
1397 rt2560_setup_tx_desc(struct rt2560_softc *sc, struct rt2560_tx_desc *desc,
1400 struct ieee80211com *ic = &sc->sc_ic;
1445 rt2560_tx_bcn(struct rt2560_softc *sc, struct mbuf *m0,
1454 desc = &sc->bcnq.desc[sc->bcnq.cur];
1455 data = &sc->bcnq.data[sc->bcnq.cur];
1460 error = bus_dmamap_load_mbuf_sg(sc->bcnq.data_dmat, data->map, m0,
1463 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1470 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1474 tap->wt_antenna = sc->tx_ant;
1482 rt2560_setup_tx_desc(sc, desc, RT2560_TX_IFS_NEWBACKOFF |
1485 DPRINTFN(sc, 10, "sending beacon frame len=%u idx=%u rate=%u\n",
1486 m0->m_pkthdr.len, sc->bcnq.cur, rate);
1488 bus_dmamap_sync(sc->bcnq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1489 bus_dmamap_sync(sc->bcnq.desc_dmat, sc->bcnq.desc_map,
1492 sc->bcnq.cur = (sc->bcnq.cur + 1) % RT2560_BEACON_RING_COUNT;
1498 rt2560_tx_mgt(struct rt2560_softc *sc, struct mbuf *m0,
1512 desc = &sc->prioq.desc[sc->prioq.cur];
1513 data = &sc->prioq.data[sc->prioq.cur];
1527 error = bus_dmamap_load_mbuf_sg(sc->prioq.data_dmat, data->map, m0,
1530 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1537 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1541 tap->wt_antenna = sc->tx_ant;
1568 rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate, 0,
1571 bus_dmamap_sync(sc->prioq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1572 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map,
1575 DPRINTFN(sc, 10, "sending mgt frame len=%u idx=%u rate=%u\n",
1576 m0->m_pkthdr.len, sc->prioq.cur, rate);
1579 sc->prioq.queued++;
1580 sc->prioq.cur = (sc->prioq.cur + 1) % RT2560_PRIO_RING_COUNT;
1581 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_PRIO);
1587 rt2560_sendprot(struct rt2560_softc *sc,
1601 device_printf(sc->sc_dev,
1606 desc = &sc->txq.desc[sc->txq.cur_encrypt];
1607 data = &sc->txq.data[sc->txq.cur_encrypt];
1609 error = bus_dmamap_load_mbuf_sg(sc->txq.data_dmat, data->map,
1612 device_printf(sc->sc_dev,
1628 rt2560_setup_tx_desc(sc, desc, flags, mprot->m_pkthdr.len, protrate, 1,
1631 bus_dmamap_sync(sc->txq.data_dmat, data->map,
1634 sc->txq.queued++;
1635 sc->txq.cur_encrypt = (sc->txq.cur_encrypt + 1) % RT2560_TX_RING_COUNT;
1641 rt2560_tx_raw(struct rt2560_softc *sc, struct mbuf *m0,
1652 desc = &sc->prioq.desc[sc->prioq.cur];
1653 data = &sc->prioq.data[sc->prioq.cur];
1666 error = rt2560_sendprot(sc, m0, ni,
1677 error = bus_dmamap_load_mbuf_sg(sc->prioq.data_dmat, data->map, m0,
1680 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1687 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1691 tap->wt_antenna = sc->tx_ant;
1700 rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len,
1704 bus_dmamap_sync(sc->prioq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1705 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map,
1708 DPRINTFN(sc, 10, "sending raw frame len=%u idx=%u rate=%u\n",
1709 m0->m_pkthdr.len, sc->prioq.cur, rate);
1712 sc->prioq.queued++;
1713 sc->prioq.cur = (sc->prioq.cur + 1) % RT2560_PRIO_RING_COUNT;
1714 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_PRIO);
1720 rt2560_tx_data(struct rt2560_softc *sc, struct mbuf *m0,
1769 error = rt2560_sendprot(sc, m0, ni, prot, rate);
1778 data = &sc->txq.data[sc->txq.cur_encrypt];
1779 desc = &sc->txq.desc[sc->txq.cur_encrypt];
1781 error = bus_dmamap_load_mbuf_sg(sc->txq.data_dmat, data->map, m0,
1784 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1792 device_printf(sc->sc_dev,
1799 error = bus_dmamap_load_mbuf_sg(sc->txq.data_dmat, data->map,
1802 device_printf(sc->sc_dev,
1813 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1817 tap->wt_antenna = sc->tx_ant;
1841 rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate, 1,
1844 bus_dmamap_sync(sc->txq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1845 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
1848 DPRINTFN(sc, 10, "sending data frame len=%u idx=%u rate=%u\n",
1849 m0->m_pkthdr.len, sc->txq.cur_encrypt, rate);
1852 sc->txq.queued++;
1853 sc->txq.cur_encrypt = (sc->txq.cur_encrypt + 1) % RT2560_TX_RING_COUNT;
1854 RAL_WRITE(sc, RT2560_SECCSR1, RT2560_KICK_ENCRYPT);
1862 struct rt2560_softc *sc = ic->ic_softc;
1865 RAL_LOCK(sc);
1866 if ((sc->sc_flags & RT2560_F_RUNNING) == 0) {
1867 RAL_UNLOCK(sc);
1870 error = mbufq_enqueue(&sc->sc_snd, m);
1872 RAL_UNLOCK(sc);
1875 rt2560_start(sc);
1876 RAL_UNLOCK(sc);
1882 rt2560_start(struct rt2560_softc *sc)
1887 RAL_LOCK_ASSERT(sc);
1889 while (sc->txq.queued < RT2560_TX_RING_COUNT - 1 &&
1890 (m = mbufq_dequeue(&sc->sc_snd)) != NULL) {
1892 if (rt2560_tx_data(sc, m, ni) != 0) {
1898 sc->sc_tx_timer = 5;
1905 struct rt2560_softc *sc = arg;
1907 RAL_LOCK_ASSERT(sc);
1909 KASSERT(sc->sc_flags & RT2560_F_RUNNING, ("not running"));
1911 if (sc->sc_invalid) /* card ejected */
1914 rt2560_encryption_intr(sc);
1915 rt2560_tx_intr(sc);
1917 if (sc->sc_tx_timer > 0 && --sc->sc_tx_timer == 0) {
1918 device_printf(sc->sc_dev, "device timeout\n");
1919 rt2560_init_locked(sc);
1920 counter_u64_add(sc->sc_ic.ic_oerrors, 1);
1924 callout_reset(&sc->watchdog_ch, hz, rt2560_watchdog, sc);
1930 struct rt2560_softc *sc = ic->ic_softc;
1933 RAL_LOCK(sc);
1935 if ((sc->sc_flags & RT2560_F_RUNNING) == 0) {
1936 rt2560_init_locked(sc);
1940 } else if (sc->sc_flags & RT2560_F_RUNNING)
1941 rt2560_stop_locked(sc);
1942 RAL_UNLOCK(sc);
1948 rt2560_bbp_write(struct rt2560_softc *sc, uint8_t reg, uint8_t val)
1954 if (!(RAL_READ(sc, RT2560_BBPCSR) & RT2560_BBP_BUSY))
1959 device_printf(sc->sc_dev, "could not write to BBP\n");
1964 RAL_WRITE(sc, RT2560_BBPCSR, tmp);
1966 DPRINTFN(sc, 15, "BBP R%u <- 0x%02x\n", reg, val);
1970 rt2560_bbp_read(struct rt2560_softc *sc, uint8_t reg)
1976 if (!(RAL_READ(sc, RT2560_BBPCSR) & RT2560_BBP_BUSY))
1981 device_printf(sc->sc_dev, "could not read from BBP\n");
1986 RAL_WRITE(sc, RT2560_BBPCSR, val);
1989 val = RAL_READ(sc, RT2560_BBPCSR);
1995 device_printf(sc->sc_dev, "could not read from BBP\n");
2000 rt2560_rf_write(struct rt2560_softc *sc, uint8_t reg, uint32_t val)
2006 if (!(RAL_READ(sc, RT2560_RFCSR) & RT2560_RF_BUSY))
2011 device_printf(sc->sc_dev, "could not write to RF\n");
2017 RAL_WRITE(sc, RT2560_RFCSR, tmp);
2019 /* remember last written value in sc */
2020 sc->rf_regs[reg] = val;
2022 DPRINTFN(sc, 15, "RF R[%u] <- 0x%05x\n", reg & 0x3, val & 0xfffff);
2026 rt2560_set_chan(struct rt2560_softc *sc, struct ieee80211_channel *c)
2028 struct ieee80211com *ic = &sc->sc_ic;
2036 power = min(sc->txpow[chan - 1], 31);
2043 DPRINTFN(sc, 2, "setting channel to %u, txpower to %u\n", chan, power);
2045 switch (sc->rf_rev) {
2047 rt2560_rf_write(sc, RAL_RF1, 0x00814);
2048 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2522_r2[chan - 1]);
2049 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
2053 rt2560_rf_write(sc, RAL_RF1, 0x08804);
2054 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2523_r2[chan - 1]);
2055 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x38044);
2056 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2060 rt2560_rf_write(sc, RAL_RF1, 0x0c808);
2061 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2524_r2[chan - 1]);
2062 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
2063 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2067 rt2560_rf_write(sc, RAL_RF1, 0x08808);
2068 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525_hi_r2[chan - 1]);
2069 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2070 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2072 rt2560_rf_write(sc, RAL_RF1, 0x08808);
2073 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525_r2[chan - 1]);
2074 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2075 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2079 rt2560_rf_write(sc, RAL_RF1, 0x08808);
2080 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525e_r2[chan - 1]);
2081 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2082 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00286 : 0x00282);
2086 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2526_hi_r2[chan - 1]);
2087 rt2560_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
2088 rt2560_rf_write(sc, RAL_RF1, 0x08804);
2090 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2526_r2[chan - 1]);
2091 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2092 rt2560_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
2099 rt2560_rf_write(sc, RAL_RF1, rt2560_rf5222[i].r1);
2100 rt2560_rf_write(sc, RAL_RF2, rt2560_rf5222[i].r2);
2101 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
2102 rt2560_rf_write(sc, RAL_RF4, rt2560_rf5222[i].r4);
2105 printf("unknown ral rev=%d\n", sc->rf_rev);
2111 tmp = rt2560_bbp_read(sc, 70);
2117 rt2560_bbp_write(sc, 70, tmp);
2120 RAL_READ(sc, RT2560_CNT0);
2128 struct rt2560_softc *sc = ic->ic_softc;
2136 if (sc->rf_rev == RT2560_RF_5222) {
2146 struct rt2560_softc *sc = ic->ic_softc;
2148 RAL_LOCK(sc);
2149 rt2560_set_chan(sc, ic->ic_curchan);
2150 RAL_UNLOCK(sc);
2159 rt2560_disable_rf_tune(struct rt2560_softc *sc)
2163 if (sc->rf_rev != RT2560_RF_2523) {
2164 tmp = sc->rf_regs[RAL_RF1] & ~RAL_RF1_AUTOTUNE;
2165 rt2560_rf_write(sc, RAL_RF1, tmp);
2168 tmp = sc->rf_regs[RAL_RF3] & ~RAL_RF3_AUTOTUNE;
2169 rt2560_rf_write(sc, RAL_RF3, tmp);
2171 DPRINTFN(sc, 2, "%s", "disabling RF autotune\n");
2180 rt2560_enable_tsf_sync(struct rt2560_softc *sc)
2182 struct ieee80211com *ic = &sc->sc_ic;
2188 RAL_WRITE(sc, RT2560_CSR14, 0);
2191 RAL_WRITE(sc, RT2560_CSR12, tmp);
2193 RAL_WRITE(sc, RT2560_CSR13, 0);
2198 RAL_WRITE(sc, RT2560_BCNOCSR, tmp);
2207 RAL_WRITE(sc, RT2560_CSR14, tmp);
2209 DPRINTF(sc, "%s", "enabling TSF synchronization\n");
2213 rt2560_enable_tsf(struct rt2560_softc *sc)
2215 RAL_WRITE(sc, RT2560_CSR14, 0);
2216 RAL_WRITE(sc, RT2560_CSR14,
2221 rt2560_update_plcp(struct rt2560_softc *sc)
2223 struct ieee80211com *ic = &sc->sc_ic;
2226 RAL_WRITE(sc, RT2560_PLCP1MCSR, 0x00700400);
2230 RAL_WRITE(sc, RT2560_PLCP2MCSR, 0x00380401);
2231 RAL_WRITE(sc, RT2560_PLCP5p5MCSR, 0x00150402);
2232 RAL_WRITE(sc, RT2560_PLCP11MCSR, 0x000b8403);
2235 RAL_WRITE(sc, RT2560_PLCP2MCSR, 0x00380409);
2236 RAL_WRITE(sc, RT2560_PLCP5p5MCSR, 0x0015040a);
2237 RAL_WRITE(sc, RT2560_PLCP11MCSR, 0x000b840b);
2240 DPRINTF(sc, "updating PLCP for %s preamble\n",
2251 struct rt2560_softc *sc = ic->ic_softc;
2284 tmp = RAL_READ(sc, RT2560_CSR11);
2286 RAL_WRITE(sc, RT2560_CSR11, tmp);
2289 RAL_WRITE(sc, RT2560_CSR18, tmp);
2292 RAL_WRITE(sc, RT2560_CSR19, tmp);
2294 DPRINTF(sc, "setting slottime to %uus\n", slottime);
2298 rt2560_set_basicrates(struct rt2560_softc *sc,
2301 struct ieee80211com *ic = &sc->sc_ic;
2316 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, mask);
2318 DPRINTF(sc, "Setting basic rate mask to 0x%x\n", mask);
2322 rt2560_update_led(struct rt2560_softc *sc, int led1, int led2)
2328 RAL_WRITE(sc, RT2560_LEDCSR, tmp);
2332 rt2560_set_bssid(struct rt2560_softc *sc, const uint8_t *bssid)
2337 RAL_WRITE(sc, RT2560_CSR5, tmp);
2340 RAL_WRITE(sc, RT2560_CSR6, tmp);
2342 DPRINTF(sc, "setting BSSID to %6D\n", bssid, ":");
2346 rt2560_set_macaddr(struct rt2560_softc *sc, const uint8_t *addr)
2351 RAL_WRITE(sc, RT2560_CSR3, tmp);
2354 RAL_WRITE(sc, RT2560_CSR4, tmp);
2356 DPRINTF(sc, "setting MAC address to %6D\n", addr, ":");
2360 rt2560_get_macaddr(struct rt2560_softc *sc, uint8_t *addr)
2364 tmp = RAL_READ(sc, RT2560_CSR3);
2370 tmp = RAL_READ(sc, RT2560_CSR4);
2378 struct rt2560_softc *sc = ic->ic_softc;
2381 tmp = RAL_READ(sc, RT2560_RXCSR0);
2387 RAL_WRITE(sc, RT2560_RXCSR0, tmp);
2389 DPRINTF(sc, "%s promiscuous mode\n",
2409 rt2560_read_config(struct rt2560_softc *sc)
2414 val = rt2560_eeprom_read(sc, RT2560_EEPROM_CONFIG0);
2415 sc->rf_rev = (val >> 11) & 0x7;
2416 sc->hw_radio = (val >> 10) & 0x1;
2417 sc->led_mode = (val >> 6) & 0x7;
2418 sc->rx_ant = (val >> 4) & 0x3;
2419 sc->tx_ant = (val >> 2) & 0x3;
2420 sc->nb_ant = val & 0x3;
2424 val = rt2560_eeprom_read(sc, RT2560_EEPROM_BBP_BASE + i);
2428 sc->bbp_prom[i].reg = val >> 8;
2429 sc->bbp_prom[i].val = val & 0xff;
2434 val = rt2560_eeprom_read(sc, RT2560_EEPROM_TXPOWER + i);
2435 sc->txpow[i * 2] = val & 0xff;
2436 sc->txpow[i * 2 + 1] = val >> 8;
2439 if (sc->txpow[i] > 31)
2440 sc->txpow[i] = 24;
2443 val = rt2560_eeprom_read(sc, RT2560_EEPROM_CALIBRATE);
2445 sc->rssi_corr = RT2560_DEFAULT_RSSI_CORR;
2447 sc->rssi_corr = val & 0xff;
2448 DPRINTF(sc, "rssi correction %d, calibrate 0x%02x\n",
2449 sc->rssi_corr, val);
2455 struct rt2560_softc *sc = ic->ic_softc;
2458 RAL_WRITE(sc, RT2560_CSR14, 0);
2459 rt2560_set_bssid(sc, ieee80211broadcastaddr);
2465 struct rt2560_softc *sc = ic->ic_softc;
2468 rt2560_enable_tsf_sync(sc);
2470 rt2560_set_bssid(sc, vap->iv_bss->ni_bssid);
2474 rt2560_bbp_init(struct rt2560_softc *sc)
2480 if (rt2560_bbp_read(sc, RT2560_BBP_VERSION) != 0)
2485 device_printf(sc->sc_dev, "timeout waiting for BBP\n");
2491 rt2560_bbp_write(sc, rt2560_def_bbp[i].reg,
2497 if (sc->bbp_prom[i].reg == 0 && sc->bbp_prom[i].val == 0)
2499 rt2560_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
2501 rt2560_bbp_write(sc, 17, 0x48); /* XXX restore bbp17 */
2507 rt2560_set_txantenna(struct rt2560_softc *sc, int antenna)
2512 tx = rt2560_bbp_read(sc, RT2560_BBP_TX) & ~RT2560_BBP_ANTMASK;
2521 if (sc->rf_rev == RT2560_RF_2525E || sc->rf_rev == RT2560_RF_2526 ||
2522 sc->rf_rev == RT2560_RF_5222)
2525 rt2560_bbp_write(sc, RT2560_BBP_TX, tx);
2528 tmp = RAL_READ(sc, RT2560_BBPCSR1) & ~0x00070007;
2530 RAL_WRITE(sc, RT2560_BBPCSR1, tmp);
2534 rt2560_set_rxantenna(struct rt2560_softc *sc, int antenna)
2538 rx = rt2560_bbp_read(sc, RT2560_BBP_RX) & ~RT2560_BBP_ANTMASK;
2547 if (sc->rf_rev == RT2560_RF_2525E || sc->rf_rev == RT2560_RF_2526)
2550 rt2560_bbp_write(sc, RT2560_BBP_RX, rx);
2554 rt2560_init_locked(struct rt2560_softc *sc)
2556 struct ieee80211com *ic = &sc->sc_ic;
2561 RAL_LOCK_ASSERT(sc);
2563 rt2560_stop_locked(sc);
2572 RAL_WRITE(sc, RT2560_TXCSR2, tmp);
2573 RAL_WRITE(sc, RT2560_TXCSR3, sc->txq.physaddr);
2574 RAL_WRITE(sc, RT2560_TXCSR5, sc->prioq.physaddr);
2575 RAL_WRITE(sc, RT2560_TXCSR4, sc->atimq.physaddr);
2576 RAL_WRITE(sc, RT2560_TXCSR6, sc->bcnq.physaddr);
2581 RAL_WRITE(sc, RT2560_RXCSR1, tmp);
2582 RAL_WRITE(sc, RT2560_RXCSR2, sc->rxq.physaddr);
2586 RAL_WRITE(sc, rt2560_def_mac[i].reg, rt2560_def_mac[i].val);
2588 rt2560_set_macaddr(sc, vap ? vap->iv_myaddr : ic->ic_macaddr);
2591 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x153);
2594 rt2560_update_plcp(sc);
2595 rt2560_update_led(sc, 0, 0);
2597 RAL_WRITE(sc, RT2560_CSR1, RT2560_RESET_ASIC);
2598 RAL_WRITE(sc, RT2560_CSR1, RT2560_HOST_READY);
2600 if (rt2560_bbp_init(sc) != 0) {
2601 rt2560_stop_locked(sc);
2605 rt2560_set_txantenna(sc, sc->tx_ant);
2606 rt2560_set_rxantenna(sc, sc->rx_ant);
2609 rt2560_set_chan(sc, ic->ic_curchan);
2621 RAL_WRITE(sc, RT2560_RXCSR0, tmp);
2624 RAL_READ(sc, RT2560_CNT0);
2625 RAL_READ(sc, RT2560_CNT4);
2628 RAL_WRITE(sc, RT2560_CSR7, 0xffffffff);
2631 RAL_WRITE(sc, RT2560_CSR8, RT2560_INTR_MASK);
2633 sc->sc_flags |= RT2560_F_RUNNING;
2635 callout_reset(&sc->watchdog_ch, hz, rt2560_watchdog, sc);
2641 struct rt2560_softc *sc = priv;
2642 struct ieee80211com *ic = &sc->sc_ic;
2644 RAL_LOCK(sc);
2645 rt2560_init_locked(sc);
2646 RAL_UNLOCK(sc);
2648 if (sc->sc_flags & RT2560_F_RUNNING)
2653 rt2560_stop_locked(struct rt2560_softc *sc)
2655 volatile int *flags = &sc->sc_flags;
2657 RAL_LOCK_ASSERT(sc);
2660 msleep(sc, &sc->sc_mtx, 0, "ralrunning", hz/10);
2662 callout_stop(&sc->watchdog_ch);
2663 sc->sc_tx_timer = 0;
2665 if (sc->sc_flags & RT2560_F_RUNNING) {
2666 sc->sc_flags &= ~RT2560_F_RUNNING;
2669 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_ABORT_TX);
2672 RAL_WRITE(sc, RT2560_RXCSR0, RT2560_DISABLE_RX);
2675 RAL_WRITE(sc, RT2560_CSR1, RT2560_RESET_ASIC);
2676 RAL_WRITE(sc, RT2560_CSR1, 0);
2679 RAL_WRITE(sc, RT2560_CSR8, 0xffffffff);
2682 rt2560_reset_tx_ring(sc, &sc->txq);
2683 rt2560_reset_tx_ring(sc, &sc->atimq);
2684 rt2560_reset_tx_ring(sc, &sc->prioq);
2685 rt2560_reset_tx_ring(sc, &sc->bcnq);
2686 rt2560_reset_rx_ring(sc, &sc->rxq);
2693 struct rt2560_softc *sc = arg;
2695 RAL_LOCK(sc);
2696 rt2560_stop_locked(sc);
2697 RAL_UNLOCK(sc);
2705 struct rt2560_softc *sc = ic->ic_softc;
2707 RAL_LOCK(sc);
2710 if (!(sc->sc_flags & RT2560_F_RUNNING)) {
2711 RAL_UNLOCK(sc);
2715 if (sc->prioq.queued >= RT2560_PRIO_RING_COUNT) {
2716 RAL_UNLOCK(sc);
2726 if (rt2560_tx_mgt(sc, m, ni) != 0)
2733 if (rt2560_tx_raw(sc, m, ni, params))
2736 sc->sc_tx_timer = 5;
2738 RAL_UNLOCK(sc);
2742 RAL_UNLOCK(sc);