blob: 6269a09c736950c98d7df37ec4261d548d53f1c6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Krzysztof Hałasa30224392008-03-24 20:24:23 +01002 * Hitachi (now Renesas) SCA-II HD64572 driver for Linux
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Krzysztof Hałasaabc9d912008-07-09 16:49:37 +02004 * Copyright (C) 1998-2008 Krzysztof Halasa <khc@pm.waw.pl>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of version 2 of the GNU General Public License
8 * as published by the Free Software Foundation.
9 *
Krzysztof Hałasa30224392008-03-24 20:24:23 +010010 * Source of information: HD64572 SCA-II User's Manual
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
12 * We use the following SCA memory map:
13 *
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +020014 * Packet buffer descriptor rings - starting from card->rambase:
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * rx_ring_buffers * sizeof(pkt_desc) = logical channel #0 RX ring
16 * tx_ring_buffers * sizeof(pkt_desc) = logical channel #0 TX ring
17 * rx_ring_buffers * sizeof(pkt_desc) = logical channel #1 RX ring (if used)
18 * tx_ring_buffers * sizeof(pkt_desc) = logical channel #1 TX ring (if used)
19 *
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +020020 * Packet data buffers - starting from card->rambase + buff_offset:
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 * rx_ring_buffers * HDLC_MAX_MRU = logical channel #0 RX buffers
22 * tx_ring_buffers * HDLC_MAX_MRU = logical channel #0 TX buffers
23 * rx_ring_buffers * HDLC_MAX_MRU = logical channel #0 RX buffers (if used)
24 * tx_ring_buffers * HDLC_MAX_MRU = logical channel #0 TX buffers (if used)
25 */
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/bitops.h>
Krzysztof Hałasa30224392008-03-24 20:24:23 +010028#include <linux/errno.h>
29#include <linux/fcntl.h>
30#include <linux/hdlc.h>
31#include <linux/in.h>
32#include <linux/init.h>
33#include <linux/interrupt.h>
34#include <linux/ioport.h>
35#include <linux/jiffies.h>
36#include <linux/kernel.h>
37#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/netdevice.h>
39#include <linux/skbuff.h>
Krzysztof Hałasa30224392008-03-24 20:24:23 +010040#include <linux/string.h>
41#include <linux/types.h>
42#include <asm/io.h>
Krzysztof Hałasa30224392008-03-24 20:24:23 +010043#include <asm/uaccess.h>
44#include "hd64572.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Krzysztof Hałasaabc9d912008-07-09 16:49:37 +020046#define NAPI_WEIGHT 16
47
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +020048#define get_msci(port) (port->chan ? MSCI1_OFFSET : MSCI0_OFFSET)
49#define get_dmac_rx(port) (port->chan ? DMAC1RX_OFFSET : DMAC0RX_OFFSET)
50#define get_dmac_tx(port) (port->chan ? DMAC1TX_OFFSET : DMAC0TX_OFFSET)
51
52#define sca_in(reg, card) readb(card->scabase + (reg))
53#define sca_out(value, reg, card) writeb(value, card->scabase + (reg))
54#define sca_inw(reg, card) readw(card->scabase + (reg))
55#define sca_outw(value, reg, card) writew(value, card->scabase + (reg))
56#define sca_inl(reg, card) readl(card->scabase + (reg))
57#define sca_outl(value, reg, card) writel(value, card->scabase + (reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Krzysztof Hałasaabc9d912008-07-09 16:49:37 +020059static int sca_poll(struct napi_struct *napi, int budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static inline port_t* dev_to_port(struct net_device *dev)
62{
63 return dev_to_hdlc(dev)->priv;
64}
65
Krzysztof Hałasaabc9d912008-07-09 16:49:37 +020066static inline void enable_intr(port_t *port)
67{
Krzysztof Hałasa0446c3b2008-07-09 19:28:45 +020068 /* enable DMIB and MSCI RXINTA interrupts */
Krzysztof Hałasaabc9d912008-07-09 16:49:37 +020069 sca_outl(sca_inl(IER0, port->card) |
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +020070 (port->chan ? 0x08002200 : 0x00080022), IER0, port->card);
Krzysztof Hałasaabc9d912008-07-09 16:49:37 +020071}
72
73static inline void disable_intr(port_t *port)
74{
75 sca_outl(sca_inl(IER0, port->card) &
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +020076 (port->chan ? 0x00FF00FF : 0xFF00FF00), IER0, port->card);
Krzysztof Hałasaabc9d912008-07-09 16:49:37 +020077}
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static inline u16 desc_abs_number(port_t *port, u16 desc, int transmit)
80{
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +020081 u16 rx_buffs = port->card->rx_ring_buffers;
82 u16 tx_buffs = port->card->tx_ring_buffers;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84 desc %= (transmit ? tx_buffs : rx_buffs); // called with "X + 1" etc.
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +020085 return port->chan * (rx_buffs + tx_buffs) + transmit * rx_buffs + desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089static inline u16 desc_offset(port_t *port, u16 desc, int transmit)
90{
Krzysztof Hałasafcfe9ff2008-07-10 00:30:51 +020091 /* Descriptor offset always fits in 16 bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 return desc_abs_number(port, desc, transmit) * sizeof(pkt_desc);
93}
94
95
Krzysztof Hałasa30224392008-03-24 20:24:23 +010096static inline pkt_desc __iomem *desc_address(port_t *port, u16 desc,
97 int transmit)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +020099 return (pkt_desc __iomem *)(port->card->rambase +
100 desc_offset(port, desc, transmit));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104static inline u32 buffer_offset(port_t *port, u16 desc, int transmit)
105{
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +0200106 return port->card->buff_offset +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 desc_abs_number(port, desc, transmit) * (u32)HDLC_MAX_MRU;
108}
109
110
Krzysztof Halasac2ce9202006-07-12 13:46:12 -0700111static inline void sca_set_carrier(port_t *port)
112{
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +0200113 if (!(sca_in(get_msci(port) + ST3, port->card) & ST3_DCD)) {
Krzysztof Halasac2ce9202006-07-12 13:46:12 -0700114#ifdef DEBUG_LINK
115 printk(KERN_DEBUG "%s: sca_set_carrier on\n",
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +0200116 port->netdev.name);
Krzysztof Halasac2ce9202006-07-12 13:46:12 -0700117#endif
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +0200118 netif_carrier_on(port->netdev);
Krzysztof Halasac2ce9202006-07-12 13:46:12 -0700119 } else {
120#ifdef DEBUG_LINK
121 printk(KERN_DEBUG "%s: sca_set_carrier off\n",
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +0200122 port->netdev.name);
Krzysztof Halasac2ce9202006-07-12 13:46:12 -0700123#endif
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +0200124 netif_carrier_off(port->netdev);
Krzysztof Halasac2ce9202006-07-12 13:46:12 -0700125 }
126}
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Krzysztof Hałasa30224392008-03-24 20:24:23 +0100129static void sca_init_port(port_t *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +0200131 card_t *card = port->card;
Krzysztof Hałasae1f024e2008-07-11 00:13:09 +0200132 u16 dmac_rx = get_dmac_rx(port), dmac_tx = get_dmac_tx(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 int transmit, i;
134
135 port->rxin = 0;
136 port->txin = 0;
137 port->txlast = 0;
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 for (transmit = 0; transmit < 2; transmit++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 u16 buffs = transmit ? card->tx_ring_buffers
141 : card->rx_ring_buffers;
142
143 for (i = 0; i < buffs; i++) {
144 pkt_desc __iomem *desc = desc_address(port, i, transmit);
145 u16 chain_off = desc_offset(port, i + 1, transmit);
146 u32 buff_off = buffer_offset(port, i, transmit);
147
Krzysztof Hałasa30224392008-03-24 20:24:23 +0100148 writel(chain_off, &desc->cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 writel(buff_off, &desc->bp);
150 writew(0, &desc->len);
151 writeb(0, &desc->stat);
152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }
Krzysztof Hałasae1f024e2008-07-11 00:13:09 +0200154
155 /* DMA disable - to halt state */
156 sca_out(0, DSR_RX(port->chan), card);
157 sca_out(0, DSR_TX(port->chan), card);
158
159 /* software ABORT - to initial state */
160 sca_out(DCR_ABORT, DCR_RX(port->chan), card);
161 sca_out(DCR_ABORT, DCR_TX(port->chan), card);
162
163 /* current desc addr */
164 sca_outl(desc_offset(port, 0, 0), dmac_rx + CDAL, card);
165 sca_outl(desc_offset(port, card->tx_ring_buffers - 1, 0),
166 dmac_rx + EDAL, card);
167 sca_outl(desc_offset(port, 0, 1), dmac_tx + CDAL, card);
168 sca_outl(desc_offset(port, 0, 1), dmac_tx + EDAL, card);
169
170 /* clear frame end interrupt counter */
171 sca_out(DCR_CLEAR_EOF, DCR_RX(port->chan), card);
172 sca_out(DCR_CLEAR_EOF, DCR_TX(port->chan), card);
173
174 /* Receive */
175 sca_outw(HDLC_MAX_MRU, dmac_rx + BFLL, card); /* set buffer length */
176 sca_out(0x14, DMR_RX(port->chan), card); /* Chain mode, Multi-frame */
177 sca_out(DIR_EOME, DIR_RX(port->chan), card); /* enable interrupts */
178 sca_out(DSR_DE, DSR_RX(port->chan), card); /* DMA enable */
179
180 /* Transmit */
181 sca_out(0x14, DMR_TX(port->chan), card); /* Chain mode, Multi-frame */
182 sca_out(DIR_EOME, DIR_TX(port->chan), card); /* enable interrupts */
183
Krzysztof Halasac2ce9202006-07-12 13:46:12 -0700184 sca_set_carrier(port);
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +0200185 netif_napi_add(port->netdev, &port->napi, sca_poll, NAPI_WEIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/* MSCI interrupt service */
190static inline void sca_msci_intr(port_t *port)
191{
192 u16 msci = get_msci(port);
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +0200193 card_t* card = port->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Krzysztof Hałasab0942f72008-07-09 20:01:23 +0200195 if (sca_in(msci + ST1, card) & ST1_CDCD) {
196 /* Reset MSCI CDCD status bit */
197 sca_out(ST1_CDCD, msci + ST1, card);
Krzysztof Halasac2ce9202006-07-12 13:46:12 -0700198 sca_set_carrier(port);
Krzysztof Hałasab0942f72008-07-09 20:01:23 +0200199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202
Krzysztof Hałasa30224392008-03-24 20:24:23 +0100203static inline void sca_rx(card_t *card, port_t *port, pkt_desc __iomem *desc,
204 u16 rxin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +0200206 struct net_device *dev = port->netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 struct sk_buff *skb;
208 u16 len;
209 u32 buff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 len = readw(&desc->len);
212 skb = dev_alloc_skb(len);
213 if (!skb) {
Krzysztof Halasa198191c2008-06-30 23:26:53 +0200214 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return;
216 }
217
218 buff = buffer_offset(port, rxin, 0);
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +0200219 memcpy_fromio(skb->data, card->rambase + buff, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 skb_put(skb, len);
222#ifdef DEBUG_PKT
223 printk(KERN_DEBUG "%s RX(%i):", dev->name, skb->len);
224 debug_frame(skb);
225#endif
Krzysztof Halasa198191c2008-06-30 23:26:53 +0200226 dev->stats.rx_packets++;
227 dev->stats.rx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 skb->protocol = hdlc_type_trans(skb, dev);
Krzysztof Hałasaabc9d912008-07-09 16:49:37 +0200229 netif_receive_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
232
Krzysztof Hałasaabc9d912008-07-09 16:49:37 +0200233/* Receive DMA service */
234static inline int sca_rx_done(port_t *port, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +0200236 struct net_device *dev = port->netdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 u16 dmac = get_dmac_rx(port);
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +0200238 card_t *card = port->card;
239 u8 stat = sca_in(DSR_RX(port->chan), card); /* read DMA Status */
Krzysztof Hałasaabc9d912008-07-09 16:49:37 +0200240 int received = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 /* Reset DSR status bits */
243 sca_out((stat & (DSR_EOT | DSR_EOM | DSR_BOF | DSR_COF)) | DSR_DWE,
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +0200244 DSR_RX(port->chan), card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 if (stat & DSR_BOF)
Krzysztof Halasa198191c2008-06-30 23:26:53 +0200247 /* Dropped one or more frames */
248 dev->stats.rx_over_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Krzysztof Hałasaabc9d912008-07-09 16:49:37 +0200250 while (received < budget) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 u32 desc_off = desc_offset(port, port->rxin, 0);
252 pkt_desc __iomem *desc;
Krzysztof Hałasa30224392008-03-24 20:24:23 +0100253 u32 cda = sca_inl(dmac + CDAL, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 if ((cda >= desc_off) && (cda < desc_off + sizeof(pkt_desc)))
256 break; /* No frame received */
257
258 desc = desc_address(port, port->rxin, 0);
259 stat = readb(&desc->stat);
260 if (!(stat & ST_RX_EOM))
261 port->rxpart = 1; /* partial frame received */
262 else if ((stat & ST_ERROR_MASK) || port->rxpart) {
Krzysztof Halasa198191c2008-06-30 23:26:53 +0200263 dev->stats.rx_errors++;
264 if (stat & ST_RX_OVERRUN)
265 dev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 else if ((stat & (ST_RX_SHORT | ST_RX_ABORT |
267 ST_RX_RESBIT)) || port->rxpart)
Krzysztof Halasa198191c2008-06-30 23:26:53 +0200268 dev->stats.rx_frame_errors++;
269 else if (stat & ST_RX_CRC)
270 dev->stats.rx_crc_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if (stat & ST_RX_EOM)
272 port->rxpart = 0; /* received last fragment */
Krzysztof Hałasaabc9d912008-07-09 16:49:37 +0200273 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 sca_rx(card, port, desc, port->rxin);
Krzysztof Hałasaabc9d912008-07-09 16:49:37 +0200275 received++;
276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 /* Set new error descriptor address */
Krzysztof Hałasa30224392008-03-24 20:24:23 +0100279 sca_outl(desc_off, dmac + EDAL, card);
Krzysztof Hałasa0b59cef2008-07-09 23:39:12 +0200280 port->rxin = (port->rxin + 1) % card->rx_ring_buffers;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
282
283 /* make sure RX DMA is enabled */
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +0200284 sca_out(DSR_DE, DSR_RX(port->chan), card);
Krzysztof Hałasaabc9d912008-07-09 16:49:37 +0200285 return received;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
288
Krzysztof Hałasaabc9d912008-07-09 16:49:37 +0200289/* Transmit DMA service */
290static inline void sca_tx_done(port_t *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +0200292 struct net_device *dev = port->netdev;
293 card_t* card = port->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 u8 stat;
Krzysztof Halasaeaff9452010-12-12 12:06:47 +0000295 unsigned count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 spin_lock(&port->lock);
298
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +0200299 stat = sca_in(DSR_TX(port->chan), card); /* read DMA Status */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 /* Reset DSR status bits */
302 sca_out((stat & (DSR_EOT | DSR_EOM | DSR_BOF | DSR_COF)) | DSR_DWE,
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +0200303 DSR_TX(port->chan), card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 while (1) {
Krzysztof Hałasa09fd65a2008-07-09 19:47:05 +0200306 pkt_desc __iomem *desc = desc_address(port, port->txlast, 1);
Krzysztof Hałasab0942f72008-07-09 20:01:23 +0200307 u8 stat = readb(&desc->stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Krzysztof Hałasab0942f72008-07-09 20:01:23 +0200309 if (!(stat & ST_TX_OWNRSHP))
Krzysztof Hałasa09fd65a2008-07-09 19:47:05 +0200310 break; /* not yet transmitted */
Krzysztof Hałasab0942f72008-07-09 20:01:23 +0200311 if (stat & ST_TX_UNDRRUN) {
312 dev->stats.tx_errors++;
313 dev->stats.tx_fifo_errors++;
314 } else {
315 dev->stats.tx_packets++;
316 dev->stats.tx_bytes += readw(&desc->len);
317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 writeb(0, &desc->stat); /* Free descriptor */
Krzysztof Halasaeaff9452010-12-12 12:06:47 +0000319 count++;
Krzysztof Hałasa0b59cef2008-07-09 23:39:12 +0200320 port->txlast = (port->txlast + 1) % card->tx_ring_buffers;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
322
Krzysztof Halasaeaff9452010-12-12 12:06:47 +0000323 if (count)
324 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 spin_unlock(&port->lock);
326}
327
328
Krzysztof Hałasaabc9d912008-07-09 16:49:37 +0200329static int sca_poll(struct napi_struct *napi, int budget)
330{
331 port_t *port = container_of(napi, port_t, napi);
Krzysztof Hałasa0954ed82008-07-09 21:24:42 +0200332 u32 isr0 = sca_inl(ISR0, port->card);
Krzysztof Hałasaabc9d912008-07-09 16:49:37 +0200333 int received = 0;
334
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +0200335 if (isr0 & (port->chan ? 0x08000000 : 0x00080000))
Krzysztof Hałasaabc9d912008-07-09 16:49:37 +0200336 sca_msci_intr(port);
337
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +0200338 if (isr0 & (port->chan ? 0x00002000 : 0x00000020))
Krzysztof Hałasaabc9d912008-07-09 16:49:37 +0200339 sca_tx_done(port);
340
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +0200341 if (isr0 & (port->chan ? 0x00000200 : 0x00000002))
Krzysztof Hałasaabc9d912008-07-09 16:49:37 +0200342 received = sca_rx_done(port, budget);
343
344 if (received < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -0800345 napi_complete(napi);
Krzysztof Hałasaabc9d912008-07-09 16:49:37 +0200346 enable_intr(port);
347 }
348
349 return received;
350}
351
Krzysztof Hałasa0954ed82008-07-09 21:24:42 +0200352static irqreturn_t sca_intr(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
354 card_t *card = dev_id;
Krzysztof Hałasa0954ed82008-07-09 21:24:42 +0200355 u32 isr0 = sca_inl(ISR0, card);
356 int i, handled = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Krzysztof Hałasaabc9d912008-07-09 16:49:37 +0200358 for (i = 0; i < 2; i++) {
359 port_t *port = get_port(card, i);
Krzysztof Hałasa0954ed82008-07-09 21:24:42 +0200360 if (port && (isr0 & (i ? 0x08002200 : 0x00080022))) {
Krzysztof Hałasaabc9d912008-07-09 16:49:37 +0200361 handled = 1;
362 disable_intr(port);
Ben Hutchings288379f2009-01-19 16:43:59 -0800363 napi_schedule(&port->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
365 }
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return IRQ_RETVAL(handled);
368}
369
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371static void sca_set_port(port_t *port)
372{
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +0200373 card_t* card = port->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 u16 msci = get_msci(port);
375 u8 md2 = sca_in(msci + MD2, card);
376 unsigned int tmc, br = 10, brv = 1024;
377
378
379 if (port->settings.clock_rate > 0) {
380 /* Try lower br for better accuracy*/
381 do {
382 br--;
383 brv >>= 1; /* brv = 2^9 = 512 max in specs */
384
385 /* Baud Rate = CLOCK_BASE / TMC / 2^BR */
386 tmc = CLOCK_BASE / brv / port->settings.clock_rate;
387 }while (br > 1 && tmc <= 128);
388
389 if (tmc < 1) {
390 tmc = 1;
391 br = 0; /* For baud=CLOCK_BASE we use tmc=1 br=0 */
392 brv = 1;
393 } else if (tmc > 255)
394 tmc = 256; /* tmc=0 means 256 - low baud rates */
395
396 port->settings.clock_rate = CLOCK_BASE / brv / tmc;
397 } else {
398 br = 9; /* Minimum clock rate */
399 tmc = 256; /* 8bit = 0 */
400 port->settings.clock_rate = CLOCK_BASE / (256 * 512);
401 }
402
403 port->rxs = (port->rxs & ~CLK_BRG_MASK) | br;
404 port->txs = (port->txs & ~CLK_BRG_MASK) | br;
405 port->tmc = tmc;
406
407 /* baud divisor - time constant*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 sca_out(port->tmc, msci + TMCR, card);
409 sca_out(port->tmc, msci + TMCT, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 /* Set BRG bits */
412 sca_out(port->rxs, msci + RXS, card);
413 sca_out(port->txs, msci + TXS, card);
414
415 if (port->settings.loopback)
416 md2 |= MD2_LOOPBACK;
417 else
418 md2 &= ~MD2_LOOPBACK;
419
420 sca_out(md2, msci + MD2, card);
421
422}
423
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425static void sca_open(struct net_device *dev)
426{
427 port_t *port = dev_to_port(dev);
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +0200428 card_t* card = port->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 u16 msci = get_msci(port);
430 u8 md0, md2;
431
432 switch(port->encoding) {
433 case ENCODING_NRZ: md2 = MD2_NRZ; break;
434 case ENCODING_NRZI: md2 = MD2_NRZI; break;
435 case ENCODING_FM_MARK: md2 = MD2_FM_MARK; break;
436 case ENCODING_FM_SPACE: md2 = MD2_FM_SPACE; break;
437 default: md2 = MD2_MANCHESTER;
438 }
439
440 if (port->settings.loopback)
441 md2 |= MD2_LOOPBACK;
442
443 switch(port->parity) {
444 case PARITY_CRC16_PR0: md0 = MD0_HDLC | MD0_CRC_16_0; break;
445 case PARITY_CRC16_PR1: md0 = MD0_HDLC | MD0_CRC_16; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 case PARITY_CRC32_PR1_CCITT: md0 = MD0_HDLC | MD0_CRC_ITU32; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 case PARITY_CRC16_PR1_CCITT: md0 = MD0_HDLC | MD0_CRC_ITU; break;
448 default: md0 = MD0_HDLC | MD0_CRC_NONE;
449 }
450
451 sca_out(CMD_RESET, msci + CMD, card);
452 sca_out(md0, msci + MD0, card);
453 sca_out(0x00, msci + MD1, card); /* no address field check */
454 sca_out(md2, msci + MD2, card);
455 sca_out(0x7E, msci + IDL, card); /* flag character 0x7E */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 /* Skip the rest of underrun frame */
457 sca_out(CTL_IDLE | CTL_URCT | CTL_URSKP, msci + CTL, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 sca_out(0x0F, msci + RNR, card); /* +1=RX DMA activation condition */
459 sca_out(0x3C, msci + TFS, card); /* +1 = TX start */
460 sca_out(0x38, msci + TCR, card); /* =Critical TX DMA activ condition */
461 sca_out(0x38, msci + TNR0, card); /* =TX DMA activation condition */
462 sca_out(0x3F, msci + TNR1, card); /* +1=TX DMA deactivation condition*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464/* We're using the following interrupts:
Krzysztof Hałasa0446c3b2008-07-09 19:28:45 +0200465 - RXINTA (DCD changes only)
466 - DMIB (EOM - single frame transfer complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467*/
Krzysztof Hałasa0446c3b2008-07-09 19:28:45 +0200468 sca_outl(IE0_RXINTA | IE0_CDCD, msci + IE0, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 sca_out(port->tmc, msci + TMCR, card);
471 sca_out(port->tmc, msci + TMCT, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 sca_out(port->rxs, msci + RXS, card);
473 sca_out(port->txs, msci + TXS, card);
474 sca_out(CMD_TX_ENABLE, msci + CMD, card);
475 sca_out(CMD_RX_ENABLE, msci + CMD, card);
476
Krzysztof Hałasaabc9d912008-07-09 16:49:37 +0200477 sca_set_carrier(port);
478 enable_intr(port);
479 napi_enable(&port->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 netif_start_queue(dev);
481}
482
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484static void sca_close(struct net_device *dev)
485{
486 port_t *port = dev_to_port(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 /* reset channel */
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +0200489 sca_out(CMD_RESET, get_msci(port) + CMD, port->card);
Krzysztof Hałasaabc9d912008-07-09 16:49:37 +0200490 disable_intr(port);
491 napi_disable(&port->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 netif_stop_queue(dev);
493}
494
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496static int sca_attach(struct net_device *dev, unsigned short encoding,
497 unsigned short parity)
498{
499 if (encoding != ENCODING_NRZ &&
500 encoding != ENCODING_NRZI &&
501 encoding != ENCODING_FM_MARK &&
502 encoding != ENCODING_FM_SPACE &&
503 encoding != ENCODING_MANCHESTER)
504 return -EINVAL;
505
506 if (parity != PARITY_NONE &&
507 parity != PARITY_CRC16_PR0 &&
508 parity != PARITY_CRC16_PR1 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 parity != PARITY_CRC32_PR1_CCITT &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 parity != PARITY_CRC16_PR1_CCITT)
511 return -EINVAL;
512
513 dev_to_port(dev)->encoding = encoding;
514 dev_to_port(dev)->parity = parity;
515 return 0;
516}
517
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519#ifdef DEBUG_RINGS
520static void sca_dump_rings(struct net_device *dev)
521{
522 port_t *port = dev_to_port(dev);
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +0200523 card_t *card = port->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 u16 cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 printk(KERN_DEBUG "RX ring: CDA=%u EDA=%u DSR=%02X in=%u %sactive",
Krzysztof Hałasa30224392008-03-24 20:24:23 +0100527 sca_inl(get_dmac_rx(port) + CDAL, card),
528 sca_inl(get_dmac_rx(port) + EDAL, card),
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +0200529 sca_in(DSR_RX(port->chan), card), port->rxin,
530 sca_in(DSR_RX(port->chan), card) & DSR_DE ? "" : "in");
531 for (cnt = 0; cnt < port->card->rx_ring_buffers; cnt++)
Joe Perches12a3bfe2011-06-26 19:01:28 +0000532 pr_cont(" %02X", readb(&(desc_address(port, cnt, 0)->stat)));
533 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Joe Perchesad361c92009-07-06 13:05:40 -0700535 printk(KERN_DEBUG "TX ring: CDA=%u EDA=%u DSR=%02X in=%u "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 "last=%u %sactive",
Krzysztof Hałasa30224392008-03-24 20:24:23 +0100537 sca_inl(get_dmac_tx(port) + CDAL, card),
538 sca_inl(get_dmac_tx(port) + EDAL, card),
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +0200539 sca_in(DSR_TX(port->chan), card), port->txin, port->txlast,
540 sca_in(DSR_TX(port->chan), card) & DSR_DE ? "" : "in");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +0200542 for (cnt = 0; cnt < port->card->tx_ring_buffers; cnt++)
Joe Perches12a3bfe2011-06-26 19:01:28 +0000543 pr_cont(" %02X", readb(&(desc_address(port, cnt, 1)->stat)));
544 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Krzysztof Hałasa30224392008-03-24 20:24:23 +0100546 printk(KERN_DEBUG "MSCI: MD: %02x %02x %02x,"
547 " ST: %02x %02x %02x %02x %02x, FST: %02x CST: %02x %02x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 sca_in(get_msci(port) + MD0, card),
549 sca_in(get_msci(port) + MD1, card),
550 sca_in(get_msci(port) + MD2, card),
551 sca_in(get_msci(port) + ST0, card),
552 sca_in(get_msci(port) + ST1, card),
553 sca_in(get_msci(port) + ST2, card),
554 sca_in(get_msci(port) + ST3, card),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 sca_in(get_msci(port) + ST4, card),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 sca_in(get_msci(port) + FST, card),
557 sca_in(get_msci(port) + CST0, card),
558 sca_in(get_msci(port) + CST1, card));
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 printk(KERN_DEBUG "ILAR: %02x ISR: %08x %08x\n", sca_in(ILAR, card),
561 sca_inl(ISR0, card), sca_inl(ISR1, card));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563#endif /* DEBUG_RINGS */
564
565
Stephen Hemmingerd71a6742009-08-31 19:50:47 +0000566static netdev_tx_t sca_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
568 port_t *port = dev_to_port(dev);
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +0200569 card_t *card = port->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 pkt_desc __iomem *desc;
571 u32 buff, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 spin_lock_irq(&port->lock);
574
575 desc = desc_address(port, port->txin + 1, 1);
Krzysztof Hałasa30224392008-03-24 20:24:23 +0100576 BUG_ON(readb(&desc->stat)); /* previous xmit should stop queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578#ifdef DEBUG_PKT
579 printk(KERN_DEBUG "%s TX(%i):", dev->name, skb->len);
580 debug_frame(skb);
581#endif
582
583 desc = desc_address(port, port->txin, 1);
584 buff = buffer_offset(port, port->txin, 1);
585 len = skb->len;
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +0200586 memcpy_toio(card->rambase + buff, skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 writew(len, &desc->len);
589 writeb(ST_TX_EOM, &desc->stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Krzysztof Hałasa0b59cef2008-07-09 23:39:12 +0200591 port->txin = (port->txin + 1) % card->tx_ring_buffers;
Krzysztof Hałasa30224392008-03-24 20:24:23 +0100592 sca_outl(desc_offset(port, port->txin, 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 get_dmac_tx(port) + EDAL, card);
594
Krzysztof Hałasa61e0a6a2008-07-09 23:13:49 +0200595 sca_out(DSR_DE, DSR_TX(port->chan), card); /* Enable TX DMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 desc = desc_address(port, port->txin + 1, 1);
598 if (readb(&desc->stat)) /* allow 1 packet gap */
599 netif_stop_queue(dev);
600
601 spin_unlock_irq(&port->lock);
602
603 dev_kfree_skb(skb);
Stephen Hemmingerd71a6742009-08-31 19:50:47 +0000604 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
607
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +0000608static u32 sca_detect_ram(card_t *card, u8 __iomem *rambase, u32 ramsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
610 /* Round RAM size to 32 bits, fill from end to start */
611 u32 i = ramsize &= ~3;
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 do {
614 i -= 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 writel(i ^ 0x12345678, rambase + i);
Krzysztof Hałasa30224392008-03-24 20:24:23 +0100616 } while (i > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 for (i = 0; i < ramsize ; i += 4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 if (readl(rambase + i) != (i ^ 0x12345678))
620 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
622
623 return i;
624}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626
Bill Pembertonaeea6bb2012-12-03 09:24:19 -0500627static void sca_init(card_t *card, int wait_states)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
629 sca_out(wait_states, WCRL, card); /* Wait Control */
630 sca_out(wait_states, WCRM, card);
631 sca_out(wait_states, WCRH, card);
632
633 sca_out(0, DMER, card); /* DMA Master disable */
634 sca_out(0x03, PCR, card); /* DMA priority */
635 sca_out(0, DSR_RX(0), card); /* DMA disable - to halt state */
636 sca_out(0, DSR_TX(0), card);
637 sca_out(0, DSR_RX(1), card);
638 sca_out(0, DSR_TX(1), card);
639 sca_out(DMER_DME, DMER, card); /* DMA Master enable */
640}