11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/module.h>
14 #include <linux/bitops.h>
19 #include <linux/hdlc.h>
21 #include <linux/kernel.h>
23 #include <linux/poll.h>
24 #include <linux/slab.h>
31 #define DEBUG_PKT_BYTES 0
34 #define DRV_NAME "ixp4xx_hss"
36 #define PKT_EXTRA_FLAGS 0
37 #define PKT_NUM_PIPES 1
38 #define PKT_PIPE_FIFO_SIZEW 4
43 #define POOL_ALLOC_SIZE (sizeof(struct desc) * (RX_DESCS + TX_DESCS))
44 #define RX_SIZE (HDLC_MAX_MRU + 4)
45 #define MAX_CLOSE_WAIT 1000
47 #define FRAME_SIZE 256
48 #define FRAME_OFFSET 0
49 #define MAX_CHANNELS (FRAME_SIZE / 8)
51 #define NAPI_WEIGHT 16
54 #define HSS0_CHL_RXTRIG_QUEUE 12
55 #define HSS0_PKT_RX_QUEUE 13
56 #define HSS0_PKT_TX0_QUEUE 14
57 #define HSS0_PKT_TX1_QUEUE 15
58 #define HSS0_PKT_TX2_QUEUE 16
59 #define HSS0_PKT_TX3_QUEUE 17
60 #define HSS0_PKT_RXFREE0_QUEUE 18
61 #define HSS0_PKT_RXFREE1_QUEUE 19
62 #define HSS0_PKT_RXFREE2_QUEUE 20
63 #define HSS0_PKT_RXFREE3_QUEUE 21
64 #define HSS0_PKT_TXDONE_QUEUE 22
66 #define HSS1_CHL_RXTRIG_QUEUE 10
67 #define HSS1_PKT_RX_QUEUE 0
68 #define HSS1_PKT_TX0_QUEUE 5
69 #define HSS1_PKT_TX1_QUEUE 6
70 #define HSS1_PKT_TX2_QUEUE 7
71 #define HSS1_PKT_TX3_QUEUE 8
72 #define HSS1_PKT_RXFREE0_QUEUE 1
73 #define HSS1_PKT_RXFREE1_QUEUE 2
74 #define HSS1_PKT_RXFREE2_QUEUE 3
75 #define HSS1_PKT_RXFREE3_QUEUE 4
76 #define HSS1_PKT_TXDONE_QUEUE 9
78 #define NPE_PKT_MODE_HDLC 0
79 #define NPE_PKT_MODE_RAW 1
80 #define NPE_PKT_MODE_56KMODE 2
81 #define NPE_PKT_MODE_56KENDIAN_MSB 4
84 #define PKT_HDLC_IDLE_ONES 0x1
85 #define PKT_HDLC_CRC_32 0x2
86 #define PKT_HDLC_MSB_ENDIAN 0x4
91 #define PCR_FRM_SYNC_ACTIVE_HIGH 0x40000000
92 #define PCR_FRM_SYNC_FALLINGEDGE 0x80000000
93 #define PCR_FRM_SYNC_RISINGEDGE 0xC0000000
96 #define PCR_FRM_SYNC_OUTPUT_FALLING 0x20000000
97 #define PCR_FRM_SYNC_OUTPUT_RISING 0x30000000
100 #define PCR_FCLK_EDGE_RISING 0x08000000
101 #define PCR_DCLK_EDGE_RISING 0x04000000
104 #define PCR_SYNC_CLK_DIR_OUTPUT 0x02000000
107 #define PCR_FRM_PULSE_DISABLED 0x01000000
110 #define PCR_HALF_CLK_RATE 0x00200000
113 #define PCR_DATA_POLARITY_INVERT 0x00100000
116 #define PCR_MSB_ENDIAN 0x00080000
119 #define PCR_TX_PINS_OPEN_DRAIN 0x00040000
122 #define PCR_SOF_NO_FBIT 0x00020000
125 #define PCR_TX_DATA_ENABLE 0x00010000
128 #define PCR_TX_V56K_HIGH 0x00002000
129 #define PCR_TX_V56K_HIGH_IMP 0x00004000
132 #define PCR_TX_UNASS_HIGH 0x00000800
133 #define PCR_TX_UNASS_HIGH_IMP 0x00001000
136 #define PCR_TX_FB_HIGH_IMP 0x00000400
139 #define PCR_TX_56KE_BIT_0_UNUSED 0x00000200
142 #define PCR_TX_56KS_56K_DATA 0x00000100
146 #define CCR_NPE_HFIFO_2_HDLC 0x04000000
147 #define CCR_NPE_HFIFO_3_OR_4HDLC 0x08000000
150 #define CCR_LOOPBACK 0x02000000
153 #define CCR_SECOND_HSS 0x01000000
157 #define CLK42X_SPEED_EXP ((0x3FF << 22) | ( 2 << 12) | 15)
159 #define CLK42X_SPEED_512KHZ (( 130 << 22) | ( 2 << 12) | 15)
160 #define CLK42X_SPEED_1536KHZ (( 43 << 22) | ( 18 << 12) | 47)
161 #define CLK42X_SPEED_1544KHZ (( 43 << 22) | ( 33 << 12) | 192)
162 #define CLK42X_SPEED_2048KHZ (( 32 << 22) | ( 34 << 12) | 63)
163 #define CLK42X_SPEED_4096KHZ (( 16 << 22) | ( 34 << 12) | 127)
164 #define CLK42X_SPEED_8192KHZ (( 8 << 22) | ( 34 << 12) | 255)
166 #define CLK46X_SPEED_512KHZ (( 130 << 22) | ( 24 << 12) | 127)
167 #define CLK46X_SPEED_1536KHZ (( 43 << 22) | (152 << 12) | 383)
168 #define CLK46X_SPEED_1544KHZ (( 43 << 22) | ( 66 << 12) | 385)
169 #define CLK46X_SPEED_2048KHZ (( 32 << 22) | (280 << 12) | 511)
170 #define CLK46X_SPEED_4096KHZ (( 16 << 22) | (280 << 12) | 1023)
171 #define CLK46X_SPEED_8192KHZ (( 8 << 22) | (280 << 12) | 2047)
198 #define TDMMAP_UNASSIGNED 0
199 #define TDMMAP_HDLC 1
200 #define TDMMAP_VOICE56K 2
201 #define TDMMAP_VOICE64K 3
204 #define HSS_CONFIG_TX_PCR 0x00
205 #define HSS_CONFIG_RX_PCR 0x04
206 #define HSS_CONFIG_CORE_CR 0x08
207 #define HSS_CONFIG_CLOCK_CR 0x0C
208 #define HSS_CONFIG_TX_FCR 0x10
209 #define HSS_CONFIG_RX_FCR 0x14
210 #define HSS_CONFIG_TX_LUT 0x18
211 #define HSS_CONFIG_RX_LUT 0x38
216 #define PORT_CONFIG_WRITE 0x40
219 #define PORT_CONFIG_LOAD 0x41
222 #define PORT_ERROR_READ 0x42
226 #define PKT_PIPE_FLOW_ENABLE 0x50
227 #define PKT_PIPE_FLOW_DISABLE 0x51
228 #define PKT_NUM_PIPES_WRITE 0x52
229 #define PKT_PIPE_FIFO_SIZEW_WRITE 0x53
230 #define PKT_PIPE_HDLC_CFG_WRITE 0x54
231 #define PKT_PIPE_IDLE_PATTERN_WRITE 0x55
232 #define PKT_PIPE_RX_SIZE_WRITE 0x56
233 #define PKT_PIPE_MODE_WRITE 0x57
236 #define ERR_SHUTDOWN 1
237 #define ERR_HDLC_ALIGN 2
238 #define ERR_HDLC_FCS 3
239 #define ERR_RXFREE_Q_EMPTY 4
241 #define ERR_HDLC_TOO_LONG 5
242 #define ERR_HDLC_ABORT 6
243 #define ERR_DISCONNECTING 7
248 #define free_buffer dev_kfree_skb
249 #define free_buffer_irq dev_kfree_skb_irq
252 #define free_buffer kfree
253 #define free_buffer_irq kfree
314 #define rx_desc_phys(port, n) ((port)->desc_tab_phys + \
315 (n) * sizeof(struct desc))
316 #define rx_desc_ptr(port, n) (&(port)->desc_tab[n])
318 #define tx_desc_phys(port, n) ((port)->desc_tab_phys + \
319 ((n) + RX_DESCS) * sizeof(struct desc))
320 #define tx_desc_ptr(port, n) (&(port)->desc_tab[(n) + RX_DESCS])
326 static int ports_open;
330 static const struct {
344 return dev_to_hdlc(dev)->priv;
351 for (i = 0; i <
cnt; i++)
364 pr_crit(
"HSS-%i: unable to send command [%08X:%08X] to %s\n",
365 port->
id, val[0], val[1], npe_name(port->
npe));
370 static void hss_config_set_lut(
struct port *port)
375 memset(&msg, 0,
sizeof(msg));
385 hss_npe_send(port, &msg,
"HSS_SET_TX_LUT");
388 hss_npe_send(port, &msg,
"HSS_SET_RX_LUT");
393 static void hss_config(
struct port *port)
397 memset(&msg, 0,
sizeof(msg));
405 hss_npe_send(port, &msg,
"HSS_SET_TX_PCR");
409 hss_npe_send(port, &msg,
"HSS_SET_RX_PCR");
411 memset(&msg, 0,
sizeof(msg));
417 hss_npe_send(port, &msg,
"HSS_SET_CORE_CR");
419 memset(&msg, 0,
sizeof(msg));
424 hss_npe_send(port, &msg,
"HSS_SET_CLOCK_CR");
426 memset(&msg, 0,
sizeof(msg));
432 hss_npe_send(port, &msg,
"HSS_SET_TX_FCR");
434 memset(&msg, 0,
sizeof(msg));
440 hss_npe_send(port, &msg,
"HSS_SET_RX_FCR");
442 hss_config_set_lut(port);
444 memset(&msg, 0,
sizeof(msg));
447 hss_npe_send(port, &msg,
"HSS_LOAD_CONFIG");
452 pr_crit(
"HSS-%i: HSS_LOAD_CONFIG failed\n", port->
id);
460 static void hss_set_hdlc_cfg(
struct port *port)
464 memset(&msg, 0,
sizeof(msg));
469 hss_npe_send(port, &msg,
"HSS_SET_HDLC_CFG");
472 static u32 hss_get_status(
struct port *port)
476 memset(&msg, 0,
sizeof(msg));
479 hss_npe_send(port, &msg,
"PORT_ERROR_READ");
481 pr_crit(
"HSS-%i: unable to read HSS status\n", port->
id);
488 static void hss_start_hdlc(
struct port *port)
492 memset(&msg, 0,
sizeof(msg));
496 hss_npe_send(port, &msg,
"HSS_ENABLE_PKT_PIPE");
499 static void hss_stop_hdlc(
struct port *port)
503 memset(&msg, 0,
sizeof(msg));
506 hss_npe_send(port, &msg,
"HSS_DISABLE_PKT_PIPE");
507 hss_get_status(port);
510 static int hss_load_firmware(
struct port *port)
524 memset(&msg, 0,
sizeof(msg));
528 hss_npe_send(port, &msg,
"HSS_SET_PKT_PIPES");
532 hss_npe_send(port, &msg,
"HSS_SET_PKT_FIFO");
538 hss_npe_send(port, &msg,
"HSS_SET_PKT_MODE");
542 hss_npe_send(port, &msg,
"HSS_SET_PKT_RX_SIZE");
546 hss_npe_send(port, &msg,
"HSS_SET_PKT_IDLE");
563 for (i = 0; i < len; i++) {
566 printk(
"%s%02X", !(i % 4) ?
" " :
"", data[i]);
582 static inline int queue_get_desc(
unsigned int queue,
struct port *port,
588 if (!(phys = qmgr_get_entry(queue)))
594 n_desc = (phys - tab_phys) /
sizeof(
struct desc);
596 debug_desc(phys, &tab[n_desc]);
601 static inline void queue_put_desc(
unsigned int queue,
u32 phys,
604 debug_desc(phys, desc);
606 qmgr_put_entry(queue, phys);
612 static inline void dma_unmap_tx(
struct port *port,
struct desc *
desc)
625 static void hss_hdlc_set_carrier(
void *pdev,
int carrier)
639 spin_unlock_irqrestore(&npe_lock, flags);
642 static void hss_hdlc_rx_irq(
void *pdev)
651 napi_schedule(&port->
napi);
656 struct port *port =
container_of(napi,
struct port, napi);
658 unsigned int rxq = queue_ids[port->
id].rx;
659 unsigned int rxfreeq = queue_ids[port->
id].rxfree;
666 while (received < budget) {
675 if ((n = queue_get_desc(rxq, port, 0)) < 0) {
678 " napi_complete\n", dev->
name);
682 if (!qmgr_stat_empty(rxq) &&
683 napi_reschedule(napi)) {
686 " napi_reschedule succeeded\n",
710 if ((skb = netdev_alloc_skb(dev,
RX_SIZE)) !=
NULL) {
720 skb = netdev_alloc_skb(dev, desc->
pkt_len);
723 dev->
stats.rx_dropped++;
727 dev->
stats.rx_frame_errors++;
728 dev->
stats.rx_errors++;
731 dev->
stats.rx_crc_errors++;
732 dev->
stats.rx_errors++;
735 dev->
stats.rx_length_errors++;
736 dev->
stats.rx_errors++;
739 netdev_err(dev,
"hss_hdlc_poll: status 0x%02X errors %u\n",
741 dev->
stats.rx_errors++;
766 debug_pkt(dev,
"hss_hdlc_poll", skb->
data, skb->
len);
768 skb->
protocol = hdlc_type_trans(skb, dev);
769 dev->
stats.rx_packets++;
790 static void hss_hdlc_txdone_irq(
void *pdev)
799 while ((n_desc = queue_get_desc(queue_ids[port->
id].txdone,
806 dev->
stats.tx_packets++;
809 dma_unmap_tx(port, desc);
817 start = qmgr_stat_below_low_watermark(port->
plat->txreadyq);
818 queue_put_desc(port->
plat->txreadyq,
819 tx_desc_phys(port, n_desc), desc);
823 " ready\n", dev->
name);
825 netif_wake_queue(dev);
833 unsigned int txreadyq = port->
plat->txreadyq;
845 dev->
stats.tx_errors++;
849 debug_pkt(dev,
"hss_hdlc_xmit", skb->
data, skb->
len);
858 bytes =
ALIGN(offset + len, 4);
861 dev->
stats.tx_dropped++;
864 memcpy_swab32(mem, (
u32 *)((
int)skb->
data & ~3), bytes / 4);
875 dev->
stats.tx_dropped++;
879 n = queue_get_desc(txreadyq, port, 1);
892 queue_put_desc(queue_ids[port->
id].tx, tx_desc_phys(port, n), desc);
894 if (qmgr_stat_below_low_watermark(txreadyq)) {
898 netif_stop_queue(dev);
900 if (!qmgr_stat_below_low_watermark(txreadyq)) {
905 netif_wake_queue(dev);
916 static int request_hdlc_queues(
struct port *port)
921 "%s:RX-free", port->
netdev->name);
926 "%s:RX", port->
netdev->name);
931 "%s:TX", port->
netdev->name);
936 "%s:TX-ready", port->
netdev->name);
941 "%s:TX-done", port->
netdev->name);
959 static void release_hdlc_queues(
struct port *port)
968 static int init_hdlc_queues(
struct port *port)
1013 static void destroy_hdlc_queues(
struct port *port)
1032 dma_unmap_tx(port, desc);
1040 if (!ports_open && dma_pool) {
1046 static int hss_hdlc_open(
struct net_device *dev)
1049 unsigned long flags;
1055 if ((err = hss_load_firmware(port)))
1056 goto err_hdlc_close;
1058 if ((err = request_hdlc_queues(port)))
1059 goto err_hdlc_close;
1061 if ((err = init_hdlc_queues(port)))
1062 goto err_destroy_queues;
1065 if (port->
plat->open)
1066 if ((err = port->
plat->open(port->
id, dev,
1067 hss_hdlc_set_carrier)))
1069 spin_unlock_irqrestore(&npe_lock, flags);
1073 queue_put_desc(port->
plat->txreadyq,
1077 queue_put_desc(queue_ids[port->
id].rxfree,
1080 napi_enable(&port->
napi);
1081 netif_start_queue(dev);
1084 hss_hdlc_rx_irq, dev);
1087 hss_hdlc_txdone_irq, dev);
1092 hss_set_hdlc_cfg(port);
1095 hss_start_hdlc(port);
1098 napi_schedule(&port->
napi);
1102 spin_unlock_irqrestore(&npe_lock, flags);
1104 destroy_hdlc_queues(port);
1105 release_hdlc_queues(port);
1111 static int hss_hdlc_close(
struct net_device *dev)
1114 unsigned long flags;
1120 netif_stop_queue(dev);
1121 napi_disable(&port->
napi);
1123 hss_stop_hdlc(port);
1125 while (queue_get_desc(queue_ids[port->
id].rxfree, port, 0) >= 0)
1127 while (queue_get_desc(queue_ids[port->
id].rx, port, 0) >= 0)
1131 netdev_crit(dev,
"unable to drain RX queue, %i buffer(s) left in NPE\n",
1135 while (queue_get_desc(queue_ids[port->
id].tx, port, 1) >= 0)
1140 while (queue_get_desc(port->
plat->txreadyq, port, 1) >= 0)
1147 netdev_crit(dev,
"unable to drain TX queue, %i buffer(s) left in NPE\n",
1155 if (port->
plat->close)
1156 port->
plat->close(port->
id, dev);
1157 spin_unlock_irqrestore(&npe_lock, flags);
1159 destroy_hdlc_queues(port);
1160 release_hdlc_queues(port);
1166 static int hss_hdlc_attach(
struct net_device *dev,
unsigned short encoding,
1167 unsigned short parity)
1196 do_div(new_rate, a * (c + 1) + b + 1);
1197 new_diff =
abs((
u32)new_rate - rate);
1199 if (new_diff < *best_diff) {
1201 *best_diff = new_diff;
1202 *reg = (a << 22) | (b << 12) |
c;
1207 static void find_best_clock(
u32 rate,
u32 *best,
u32 *reg)
1209 u32 a,
b, diff = 0xFFFFFFFF;
1214 check_clock(rate, 0x3FF, 1, 1, best, &diff, reg);
1223 check_clock(rate, a - 1, 1, 1, best, &diff, reg);
1227 for (b = 0; b < 0x400; b++) {
1233 !check_clock(rate, a - 1, 1, 1, best, &diff, reg))
1235 check_clock(rate, a, b, 0xFFF, best, &diff, reg);
1238 if (!check_clock(rate, a, b, c, best, &diff, reg))
1240 if (!check_clock(rate, a, b, c + 1, best, &diff, reg))
1251 unsigned long flags;
1257 switch(ifr->ifr_settings.type) {
1260 if (ifr->ifr_settings.size < size) {
1261 ifr->ifr_settings.size =
size;
1264 memset(&new_line, 0,
sizeof(new_line));
1280 if (port->
plat->set_clock)
1281 clk = port->
plat->set_clock(port->
id, clk);
1308 spin_unlock_irqrestore(&npe_lock, flags);
1322 .ndo_open = hss_hdlc_open,
1323 .ndo_stop = hss_hdlc_close,
1326 .ndo_do_ioctl = hss_hdlc_ioctl,
1350 hdlc = dev_to_hdlc(dev);
1351 hdlc->
attach = hss_hdlc_attach;
1352 hdlc->
xmit = hss_hdlc_xmit;
1358 port->
id = pdev->
id;
1360 port->
plat = pdev->
dev.platform_data;
1364 goto err_free_netdev;
1366 platform_set_drvdata(pdev, port);
1368 netdev_info(dev,
"initialized\n");
1382 struct port *port = platform_get_drvdata(pdev);
1387 platform_set_drvdata(pdev,
NULL);
1394 .probe = hss_init_one,
1395 .remove = hss_remove_one,
1398 static int __init hss_init_module(
void)
1400 if ((ixp4xx_read_feature_bits() &
1410 static void __exit hss_cleanup_module(
void)