hd64572.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Hitachi (now Renesas) SCA-II HD64572 driver for Linux
  4. *
  5. * Copyright (C) 1998-2008 Krzysztof Halasa <khc@pm.waw.pl>
  6. *
  7. * Source of information: HD64572 SCA-II User's Manual
  8. *
  9. * We use the following SCA memory map:
  10. *
  11. * Packet buffer descriptor rings - starting from card->rambase:
  12. * rx_ring_buffers * sizeof(pkt_desc) = logical channel #0 RX ring
  13. * tx_ring_buffers * sizeof(pkt_desc) = logical channel #0 TX ring
  14. * rx_ring_buffers * sizeof(pkt_desc) = logical channel #1 RX ring (if used)
  15. * tx_ring_buffers * sizeof(pkt_desc) = logical channel #1 TX ring (if used)
  16. *
  17. * Packet data buffers - starting from card->rambase + buff_offset:
  18. * rx_ring_buffers * HDLC_MAX_MRU = logical channel #0 RX buffers
  19. * tx_ring_buffers * HDLC_MAX_MRU = logical channel #0 TX buffers
  20. * rx_ring_buffers * HDLC_MAX_MRU = logical channel #0 RX buffers (if used)
  21. * tx_ring_buffers * HDLC_MAX_MRU = logical channel #0 TX buffers (if used)
  22. */
  23. #include <linux/bitops.h>
  24. #include <linux/errno.h>
  25. #include <linux/fcntl.h>
  26. #include <linux/hdlc.h>
  27. #include <linux/in.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/ioport.h>
  30. #include <linux/jiffies.h>
  31. #include <linux/kernel.h>
  32. #include <linux/module.h>
  33. #include <linux/netdevice.h>
  34. #include <linux/skbuff.h>
  35. #include <linux/string.h>
  36. #include <linux/types.h>
  37. #include <asm/io.h>
  38. #include <linux/uaccess.h>
  39. #include "hd64572.h"
  40. #define NAPI_WEIGHT 16
  41. #define get_msci(port) (port->chan ? MSCI1_OFFSET : MSCI0_OFFSET)
  42. #define get_dmac_rx(port) (port->chan ? DMAC1RX_OFFSET : DMAC0RX_OFFSET)
  43. #define get_dmac_tx(port) (port->chan ? DMAC1TX_OFFSET : DMAC0TX_OFFSET)
  44. #define sca_in(reg, card) readb(card->scabase + (reg))
  45. #define sca_out(value, reg, card) writeb(value, card->scabase + (reg))
  46. #define sca_inw(reg, card) readw(card->scabase + (reg))
  47. #define sca_outw(value, reg, card) writew(value, card->scabase + (reg))
  48. #define sca_inl(reg, card) readl(card->scabase + (reg))
  49. #define sca_outl(value, reg, card) writel(value, card->scabase + (reg))
  50. static int sca_poll(struct napi_struct *napi, int budget);
  51. static inline port_t* dev_to_port(struct net_device *dev)
  52. {
  53. return dev_to_hdlc(dev)->priv;
  54. }
  55. static inline void enable_intr(port_t *port)
  56. {
  57. /* enable DMIB and MSCI RXINTA interrupts */
  58. sca_outl(sca_inl(IER0, port->card) |
  59. (port->chan ? 0x08002200 : 0x00080022), IER0, port->card);
  60. }
  61. static inline void disable_intr(port_t *port)
  62. {
  63. sca_outl(sca_inl(IER0, port->card) &
  64. (port->chan ? 0x00FF00FF : 0xFF00FF00), IER0, port->card);
  65. }
  66. static inline u16 desc_abs_number(port_t *port, u16 desc, int transmit)
  67. {
  68. u16 rx_buffs = port->card->rx_ring_buffers;
  69. u16 tx_buffs = port->card->tx_ring_buffers;
  70. desc %= (transmit ? tx_buffs : rx_buffs); // called with "X + 1" etc.
  71. return port->chan * (rx_buffs + tx_buffs) + transmit * rx_buffs + desc;
  72. }
  73. static inline u16 desc_offset(port_t *port, u16 desc, int transmit)
  74. {
  75. /* Descriptor offset always fits in 16 bits */
  76. return desc_abs_number(port, desc, transmit) * sizeof(pkt_desc);
  77. }
  78. static inline pkt_desc __iomem *desc_address(port_t *port, u16 desc,
  79. int transmit)
  80. {
  81. return (pkt_desc __iomem *)(port->card->rambase +
  82. desc_offset(port, desc, transmit));
  83. }
  84. static inline u32 buffer_offset(port_t *port, u16 desc, int transmit)
  85. {
  86. return port->card->buff_offset +
  87. desc_abs_number(port, desc, transmit) * (u32)HDLC_MAX_MRU;
  88. }
  89. static inline void sca_set_carrier(port_t *port)
  90. {
  91. if (!(sca_in(get_msci(port) + ST3, port->card) & ST3_DCD)) {
  92. #ifdef DEBUG_LINK
  93. printk(KERN_DEBUG "%s: sca_set_carrier on\n",
  94. port->netdev.name);
  95. #endif
  96. netif_carrier_on(port->netdev);
  97. } else {
  98. #ifdef DEBUG_LINK
  99. printk(KERN_DEBUG "%s: sca_set_carrier off\n",
  100. port->netdev.name);
  101. #endif
  102. netif_carrier_off(port->netdev);
  103. }
  104. }
  105. static void sca_init_port(port_t *port)
  106. {
  107. card_t *card = port->card;
  108. u16 dmac_rx = get_dmac_rx(port), dmac_tx = get_dmac_tx(port);
  109. int transmit, i;
  110. port->rxin = 0;
  111. port->txin = 0;
  112. port->txlast = 0;
  113. for (transmit = 0; transmit < 2; transmit++) {
  114. u16 buffs = transmit ? card->tx_ring_buffers
  115. : card->rx_ring_buffers;
  116. for (i = 0; i < buffs; i++) {
  117. pkt_desc __iomem *desc = desc_address(port, i, transmit);
  118. u16 chain_off = desc_offset(port, i + 1, transmit);
  119. u32 buff_off = buffer_offset(port, i, transmit);
  120. writel(chain_off, &desc->cp);
  121. writel(buff_off, &desc->bp);
  122. writew(0, &desc->len);
  123. writeb(0, &desc->stat);
  124. }
  125. }
  126. /* DMA disable - to halt state */
  127. sca_out(0, DSR_RX(port->chan), card);
  128. sca_out(0, DSR_TX(port->chan), card);
  129. /* software ABORT - to initial state */
  130. sca_out(DCR_ABORT, DCR_RX(port->chan), card);
  131. sca_out(DCR_ABORT, DCR_TX(port->chan), card);
  132. /* current desc addr */
  133. sca_outl(desc_offset(port, 0, 0), dmac_rx + CDAL, card);
  134. sca_outl(desc_offset(port, card->tx_ring_buffers - 1, 0),
  135. dmac_rx + EDAL, card);
  136. sca_outl(desc_offset(port, 0, 1), dmac_tx + CDAL, card);
  137. sca_outl(desc_offset(port, 0, 1), dmac_tx + EDAL, card);
  138. /* clear frame end interrupt counter */
  139. sca_out(DCR_CLEAR_EOF, DCR_RX(port->chan), card);
  140. sca_out(DCR_CLEAR_EOF, DCR_TX(port->chan), card);
  141. /* Receive */
  142. sca_outw(HDLC_MAX_MRU, dmac_rx + BFLL, card); /* set buffer length */
  143. sca_out(0x14, DMR_RX(port->chan), card); /* Chain mode, Multi-frame */
  144. sca_out(DIR_EOME, DIR_RX(port->chan), card); /* enable interrupts */
  145. sca_out(DSR_DE, DSR_RX(port->chan), card); /* DMA enable */
  146. /* Transmit */
  147. sca_out(0x14, DMR_TX(port->chan), card); /* Chain mode, Multi-frame */
  148. sca_out(DIR_EOME, DIR_TX(port->chan), card); /* enable interrupts */
  149. sca_set_carrier(port);
  150. netif_napi_add(port->netdev, &port->napi, sca_poll, NAPI_WEIGHT);
  151. }
  152. /* MSCI interrupt service */
  153. static inline void sca_msci_intr(port_t *port)
  154. {
  155. u16 msci = get_msci(port);
  156. card_t* card = port->card;
  157. if (sca_in(msci + ST1, card) & ST1_CDCD) {
  158. /* Reset MSCI CDCD status bit */
  159. sca_out(ST1_CDCD, msci + ST1, card);
  160. sca_set_carrier(port);
  161. }
  162. }
  163. static inline void sca_rx(card_t *card, port_t *port, pkt_desc __iomem *desc,
  164. u16 rxin)
  165. {
  166. struct net_device *dev = port->netdev;
  167. struct sk_buff *skb;
  168. u16 len;
  169. u32 buff;
  170. len = readw(&desc->len);
  171. skb = dev_alloc_skb(len);
  172. if (!skb) {
  173. dev->stats.rx_dropped++;
  174. return;
  175. }
  176. buff = buffer_offset(port, rxin, 0);
  177. memcpy_fromio(skb->data, card->rambase + buff, len);
  178. skb_put(skb, len);
  179. #ifdef DEBUG_PKT
  180. printk(KERN_DEBUG "%s RX(%i):", dev->name, skb->len);
  181. debug_frame(skb);
  182. #endif
  183. dev->stats.rx_packets++;
  184. dev->stats.rx_bytes += skb->len;
  185. skb->protocol = hdlc_type_trans(skb, dev);
  186. netif_receive_skb(skb);
  187. }
  188. /* Receive DMA service */
  189. static inline int sca_rx_done(port_t *port, int budget)
  190. {
  191. struct net_device *dev = port->netdev;
  192. u16 dmac = get_dmac_rx(port);
  193. card_t *card = port->card;
  194. u8 stat = sca_in(DSR_RX(port->chan), card); /* read DMA Status */
  195. int received = 0;
  196. /* Reset DSR status bits */
  197. sca_out((stat & (DSR_EOT | DSR_EOM | DSR_BOF | DSR_COF)) | DSR_DWE,
  198. DSR_RX(port->chan), card);
  199. if (stat & DSR_BOF)
  200. /* Dropped one or more frames */
  201. dev->stats.rx_over_errors++;
  202. while (received < budget) {
  203. u32 desc_off = desc_offset(port, port->rxin, 0);
  204. pkt_desc __iomem *desc;
  205. u32 cda = sca_inl(dmac + CDAL, card);
  206. if ((cda >= desc_off) && (cda < desc_off + sizeof(pkt_desc)))
  207. break; /* No frame received */
  208. desc = desc_address(port, port->rxin, 0);
  209. stat = readb(&desc->stat);
  210. if (!(stat & ST_RX_EOM))
  211. port->rxpart = 1; /* partial frame received */
  212. else if ((stat & ST_ERROR_MASK) || port->rxpart) {
  213. dev->stats.rx_errors++;
  214. if (stat & ST_RX_OVERRUN)
  215. dev->stats.rx_fifo_errors++;
  216. else if ((stat & (ST_RX_SHORT | ST_RX_ABORT |
  217. ST_RX_RESBIT)) || port->rxpart)
  218. dev->stats.rx_frame_errors++;
  219. else if (stat & ST_RX_CRC)
  220. dev->stats.rx_crc_errors++;
  221. if (stat & ST_RX_EOM)
  222. port->rxpart = 0; /* received last fragment */
  223. } else {
  224. sca_rx(card, port, desc, port->rxin);
  225. received++;
  226. }
  227. /* Set new error descriptor address */
  228. sca_outl(desc_off, dmac + EDAL, card);
  229. port->rxin = (port->rxin + 1) % card->rx_ring_buffers;
  230. }
  231. /* make sure RX DMA is enabled */
  232. sca_out(DSR_DE, DSR_RX(port->chan), card);
  233. return received;
  234. }
  235. /* Transmit DMA service */
  236. static inline void sca_tx_done(port_t *port)
  237. {
  238. struct net_device *dev = port->netdev;
  239. card_t* card = port->card;
  240. u8 stat;
  241. unsigned count = 0;
  242. spin_lock(&port->lock);
  243. stat = sca_in(DSR_TX(port->chan), card); /* read DMA Status */
  244. /* Reset DSR status bits */
  245. sca_out((stat & (DSR_EOT | DSR_EOM | DSR_BOF | DSR_COF)) | DSR_DWE,
  246. DSR_TX(port->chan), card);
  247. while (1) {
  248. pkt_desc __iomem *desc = desc_address(port, port->txlast, 1);
  249. u8 stat = readb(&desc->stat);
  250. if (!(stat & ST_TX_OWNRSHP))
  251. break; /* not yet transmitted */
  252. if (stat & ST_TX_UNDRRUN) {
  253. dev->stats.tx_errors++;
  254. dev->stats.tx_fifo_errors++;
  255. } else {
  256. dev->stats.tx_packets++;
  257. dev->stats.tx_bytes += readw(&desc->len);
  258. }
  259. writeb(0, &desc->stat); /* Free descriptor */
  260. count++;
  261. port->txlast = (port->txlast + 1) % card->tx_ring_buffers;
  262. }
  263. if (count)
  264. netif_wake_queue(dev);
  265. spin_unlock(&port->lock);
  266. }
  267. static int sca_poll(struct napi_struct *napi, int budget)
  268. {
  269. port_t *port = container_of(napi, port_t, napi);
  270. u32 isr0 = sca_inl(ISR0, port->card);
  271. int received = 0;
  272. if (isr0 & (port->chan ? 0x08000000 : 0x00080000))
  273. sca_msci_intr(port);
  274. if (isr0 & (port->chan ? 0x00002000 : 0x00000020))
  275. sca_tx_done(port);
  276. if (isr0 & (port->chan ? 0x00000200 : 0x00000002))
  277. received = sca_rx_done(port, budget);
  278. if (received < budget) {
  279. napi_complete_done(napi, received);
  280. enable_intr(port);
  281. }
  282. return received;
  283. }
  284. static irqreturn_t sca_intr(int irq, void *dev_id)
  285. {
  286. card_t *card = dev_id;
  287. u32 isr0 = sca_inl(ISR0, card);
  288. int i, handled = 0;
  289. for (i = 0; i < 2; i++) {
  290. port_t *port = get_port(card, i);
  291. if (port && (isr0 & (i ? 0x08002200 : 0x00080022))) {
  292. handled = 1;
  293. disable_intr(port);
  294. napi_schedule(&port->napi);
  295. }
  296. }
  297. return IRQ_RETVAL(handled);
  298. }
  299. static void sca_set_port(port_t *port)
  300. {
  301. card_t* card = port->card;
  302. u16 msci = get_msci(port);
  303. u8 md2 = sca_in(msci + MD2, card);
  304. unsigned int tmc, br = 10, brv = 1024;
  305. if (port->settings.clock_rate > 0) {
  306. /* Try lower br for better accuracy*/
  307. do {
  308. br--;
  309. brv >>= 1; /* brv = 2^9 = 512 max in specs */
  310. /* Baud Rate = CLOCK_BASE / TMC / 2^BR */
  311. tmc = CLOCK_BASE / brv / port->settings.clock_rate;
  312. }while (br > 1 && tmc <= 128);
  313. if (tmc < 1) {
  314. tmc = 1;
  315. br = 0; /* For baud=CLOCK_BASE we use tmc=1 br=0 */
  316. brv = 1;
  317. } else if (tmc > 255)
  318. tmc = 256; /* tmc=0 means 256 - low baud rates */
  319. port->settings.clock_rate = CLOCK_BASE / brv / tmc;
  320. } else {
  321. br = 9; /* Minimum clock rate */
  322. tmc = 256; /* 8bit = 0 */
  323. port->settings.clock_rate = CLOCK_BASE / (256 * 512);
  324. }
  325. port->rxs = (port->rxs & ~CLK_BRG_MASK) | br;
  326. port->txs = (port->txs & ~CLK_BRG_MASK) | br;
  327. port->tmc = tmc;
  328. /* baud divisor - time constant*/
  329. sca_out(port->tmc, msci + TMCR, card);
  330. sca_out(port->tmc, msci + TMCT, card);
  331. /* Set BRG bits */
  332. sca_out(port->rxs, msci + RXS, card);
  333. sca_out(port->txs, msci + TXS, card);
  334. if (port->settings.loopback)
  335. md2 |= MD2_LOOPBACK;
  336. else
  337. md2 &= ~MD2_LOOPBACK;
  338. sca_out(md2, msci + MD2, card);
  339. }
  340. static void sca_open(struct net_device *dev)
  341. {
  342. port_t *port = dev_to_port(dev);
  343. card_t* card = port->card;
  344. u16 msci = get_msci(port);
  345. u8 md0, md2;
  346. switch(port->encoding) {
  347. case ENCODING_NRZ: md2 = MD2_NRZ; break;
  348. case ENCODING_NRZI: md2 = MD2_NRZI; break;
  349. case ENCODING_FM_MARK: md2 = MD2_FM_MARK; break;
  350. case ENCODING_FM_SPACE: md2 = MD2_FM_SPACE; break;
  351. default: md2 = MD2_MANCHESTER;
  352. }
  353. if (port->settings.loopback)
  354. md2 |= MD2_LOOPBACK;
  355. switch(port->parity) {
  356. case PARITY_CRC16_PR0: md0 = MD0_HDLC | MD0_CRC_16_0; break;
  357. case PARITY_CRC16_PR1: md0 = MD0_HDLC | MD0_CRC_16; break;
  358. case PARITY_CRC32_PR1_CCITT: md0 = MD0_HDLC | MD0_CRC_ITU32; break;
  359. case PARITY_CRC16_PR1_CCITT: md0 = MD0_HDLC | MD0_CRC_ITU; break;
  360. default: md0 = MD0_HDLC | MD0_CRC_NONE;
  361. }
  362. sca_out(CMD_RESET, msci + CMD, card);
  363. sca_out(md0, msci + MD0, card);
  364. sca_out(0x00, msci + MD1, card); /* no address field check */
  365. sca_out(md2, msci + MD2, card);
  366. sca_out(0x7E, msci + IDL, card); /* flag character 0x7E */
  367. /* Skip the rest of underrun frame */
  368. sca_out(CTL_IDLE | CTL_URCT | CTL_URSKP, msci + CTL, card);
  369. sca_out(0x0F, msci + RNR, card); /* +1=RX DMA activation condition */
  370. sca_out(0x3C, msci + TFS, card); /* +1 = TX start */
  371. sca_out(0x38, msci + TCR, card); /* =Critical TX DMA activ condition */
  372. sca_out(0x38, msci + TNR0, card); /* =TX DMA activation condition */
  373. sca_out(0x3F, msci + TNR1, card); /* +1=TX DMA deactivation condition*/
  374. /* We're using the following interrupts:
  375. - RXINTA (DCD changes only)
  376. - DMIB (EOM - single frame transfer complete)
  377. */
  378. sca_outl(IE0_RXINTA | IE0_CDCD, msci + IE0, card);
  379. sca_out(port->tmc, msci + TMCR, card);
  380. sca_out(port->tmc, msci + TMCT, card);
  381. sca_out(port->rxs, msci + RXS, card);
  382. sca_out(port->txs, msci + TXS, card);
  383. sca_out(CMD_TX_ENABLE, msci + CMD, card);
  384. sca_out(CMD_RX_ENABLE, msci + CMD, card);
  385. sca_set_carrier(port);
  386. enable_intr(port);
  387. napi_enable(&port->napi);
  388. netif_start_queue(dev);
  389. }
  390. static void sca_close(struct net_device *dev)
  391. {
  392. port_t *port = dev_to_port(dev);
  393. /* reset channel */
  394. sca_out(CMD_RESET, get_msci(port) + CMD, port->card);
  395. disable_intr(port);
  396. napi_disable(&port->napi);
  397. netif_stop_queue(dev);
  398. }
  399. static int sca_attach(struct net_device *dev, unsigned short encoding,
  400. unsigned short parity)
  401. {
  402. if (encoding != ENCODING_NRZ &&
  403. encoding != ENCODING_NRZI &&
  404. encoding != ENCODING_FM_MARK &&
  405. encoding != ENCODING_FM_SPACE &&
  406. encoding != ENCODING_MANCHESTER)
  407. return -EINVAL;
  408. if (parity != PARITY_NONE &&
  409. parity != PARITY_CRC16_PR0 &&
  410. parity != PARITY_CRC16_PR1 &&
  411. parity != PARITY_CRC32_PR1_CCITT &&
  412. parity != PARITY_CRC16_PR1_CCITT)
  413. return -EINVAL;
  414. dev_to_port(dev)->encoding = encoding;
  415. dev_to_port(dev)->parity = parity;
  416. return 0;
  417. }
  418. #ifdef DEBUG_RINGS
  419. static void sca_dump_rings(struct net_device *dev)
  420. {
  421. port_t *port = dev_to_port(dev);
  422. card_t *card = port->card;
  423. u16 cnt;
  424. printk(KERN_DEBUG "RX ring: CDA=%u EDA=%u DSR=%02X in=%u %sactive",
  425. sca_inl(get_dmac_rx(port) + CDAL, card),
  426. sca_inl(get_dmac_rx(port) + EDAL, card),
  427. sca_in(DSR_RX(port->chan), card), port->rxin,
  428. sca_in(DSR_RX(port->chan), card) & DSR_DE ? "" : "in");
  429. for (cnt = 0; cnt < port->card->rx_ring_buffers; cnt++)
  430. pr_cont(" %02X", readb(&(desc_address(port, cnt, 0)->stat)));
  431. pr_cont("\n");
  432. printk(KERN_DEBUG "TX ring: CDA=%u EDA=%u DSR=%02X in=%u "
  433. "last=%u %sactive",
  434. sca_inl(get_dmac_tx(port) + CDAL, card),
  435. sca_inl(get_dmac_tx(port) + EDAL, card),
  436. sca_in(DSR_TX(port->chan), card), port->txin, port->txlast,
  437. sca_in(DSR_TX(port->chan), card) & DSR_DE ? "" : "in");
  438. for (cnt = 0; cnt < port->card->tx_ring_buffers; cnt++)
  439. pr_cont(" %02X", readb(&(desc_address(port, cnt, 1)->stat)));
  440. pr_cont("\n");
  441. printk(KERN_DEBUG "MSCI: MD: %02x %02x %02x,"
  442. " ST: %02x %02x %02x %02x %02x, FST: %02x CST: %02x %02x\n",
  443. sca_in(get_msci(port) + MD0, card),
  444. sca_in(get_msci(port) + MD1, card),
  445. sca_in(get_msci(port) + MD2, card),
  446. sca_in(get_msci(port) + ST0, card),
  447. sca_in(get_msci(port) + ST1, card),
  448. sca_in(get_msci(port) + ST2, card),
  449. sca_in(get_msci(port) + ST3, card),
  450. sca_in(get_msci(port) + ST4, card),
  451. sca_in(get_msci(port) + FST, card),
  452. sca_in(get_msci(port) + CST0, card),
  453. sca_in(get_msci(port) + CST1, card));
  454. printk(KERN_DEBUG "ILAR: %02x ISR: %08x %08x\n", sca_in(ILAR, card),
  455. sca_inl(ISR0, card), sca_inl(ISR1, card));
  456. }
  457. #endif /* DEBUG_RINGS */
  458. static netdev_tx_t sca_xmit(struct sk_buff *skb, struct net_device *dev)
  459. {
  460. port_t *port = dev_to_port(dev);
  461. card_t *card = port->card;
  462. pkt_desc __iomem *desc;
  463. u32 buff, len;
  464. spin_lock_irq(&port->lock);
  465. desc = desc_address(port, port->txin + 1, 1);
  466. BUG_ON(readb(&desc->stat)); /* previous xmit should stop queue */
  467. #ifdef DEBUG_PKT
  468. printk(KERN_DEBUG "%s TX(%i):", dev->name, skb->len);
  469. debug_frame(skb);
  470. #endif
  471. desc = desc_address(port, port->txin, 1);
  472. buff = buffer_offset(port, port->txin, 1);
  473. len = skb->len;
  474. memcpy_toio(card->rambase + buff, skb->data, len);
  475. writew(len, &desc->len);
  476. writeb(ST_TX_EOM, &desc->stat);
  477. port->txin = (port->txin + 1) % card->tx_ring_buffers;
  478. sca_outl(desc_offset(port, port->txin, 1),
  479. get_dmac_tx(port) + EDAL, card);
  480. sca_out(DSR_DE, DSR_TX(port->chan), card); /* Enable TX DMA */
  481. desc = desc_address(port, port->txin + 1, 1);
  482. if (readb(&desc->stat)) /* allow 1 packet gap */
  483. netif_stop_queue(dev);
  484. spin_unlock_irq(&port->lock);
  485. dev_kfree_skb(skb);
  486. return NETDEV_TX_OK;
  487. }
  488. static u32 sca_detect_ram(card_t *card, u8 __iomem *rambase, u32 ramsize)
  489. {
  490. /* Round RAM size to 32 bits, fill from end to start */
  491. u32 i = ramsize &= ~3;
  492. do {
  493. i -= 4;
  494. writel(i ^ 0x12345678, rambase + i);
  495. } while (i > 0);
  496. for (i = 0; i < ramsize ; i += 4) {
  497. if (readl(rambase + i) != (i ^ 0x12345678))
  498. break;
  499. }
  500. return i;
  501. }
  502. static void sca_init(card_t *card, int wait_states)
  503. {
  504. sca_out(wait_states, WCRL, card); /* Wait Control */
  505. sca_out(wait_states, WCRM, card);
  506. sca_out(wait_states, WCRH, card);
  507. sca_out(0, DMER, card); /* DMA Master disable */
  508. sca_out(0x03, PCR, card); /* DMA priority */
  509. sca_out(0, DSR_RX(0), card); /* DMA disable - to halt state */
  510. sca_out(0, DSR_TX(0), card);
  511. sca_out(0, DSR_RX(1), card);
  512. sca_out(0, DSR_TX(1), card);
  513. sca_out(DMER_DME, DMER, card); /* DMA Master enable */
  514. }