1 // SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
3 * Copyright (c) 2011, 2012, Qualcomm Atheros Communications Inc.
4 * Copyright (c) 2017, I2SE GmbH
7 /* This module implements the Qualcomm Atheros UART protocol for
8 * kernel-based UART device; it is essentially an Ethernet-to-UART
12 #include <linux/device.h>
13 #include <linux/errno.h>
14 #include <linux/etherdevice.h>
15 #include <linux/if_arp.h>
16 #include <linux/if_ether.h>
17 #include <linux/jiffies.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/netdevice.h>
22 #include <linux/of_net.h>
23 #include <linux/sched.h>
24 #include <linux/serdev.h>
25 #include <linux/skbuff.h>
26 #include <linux/types.h>
28 #include "qca_7k_common.h"
30 #define QCAUART_DRV_VERSION "0.1.0"
31 #define QCAUART_DRV_NAME "qcauart"
32 #define QCAUART_TX_TIMEOUT (1 * HZ)
35 struct net_device *net_dev;
36 spinlock_t lock; /* transmit lock */
37 struct work_struct tx_work; /* Flushes transmit buffer */
39 struct serdev_device *serdev;
40 struct qcafrm_handle frm_handle;
41 struct sk_buff *rx_skb;
43 unsigned char *tx_head; /* pointer to next XMIT byte */
44 int tx_left; /* bytes left in XMIT queue */
45 unsigned char *tx_buffer;
49 qca_tty_receive(struct serdev_device *serdev, const u8 *data, size_t count)
51 struct qcauart *qca = serdev_device_get_drvdata(serdev);
52 struct net_device *netdev = qca->net_dev;
53 struct net_device_stats *n_stats = &netdev->stats;
57 qca->rx_skb = netdev_alloc_skb_ip_align(netdev,
62 n_stats->rx_dropped++;
67 for (i = 0; i < count; i++) {
70 retcode = qcafrm_fsm_decode(&qca->frm_handle,
72 skb_tailroom(qca->rx_skb),
80 netdev_dbg(netdev, "recv: no RX tail\n");
82 n_stats->rx_dropped++;
85 netdev_dbg(netdev, "recv: invalid RX length\n");
87 n_stats->rx_dropped++;
90 n_stats->rx_packets++;
91 n_stats->rx_bytes += retcode;
92 skb_put(qca->rx_skb, retcode);
93 qca->rx_skb->protocol = eth_type_trans(
94 qca->rx_skb, qca->rx_skb->dev);
95 skb_checksum_none_assert(qca->rx_skb);
96 netif_rx(qca->rx_skb);
97 qca->rx_skb = netdev_alloc_skb_ip_align(netdev,
101 netdev_dbg(netdev, "recv: out of RX resources\n");
102 n_stats->rx_errors++;
111 /* Write out any remaining transmit buffer. Scheduled when tty is writable */
112 static void qcauart_transmit(struct work_struct *work)
114 struct qcauart *qca = container_of(work, struct qcauart, tx_work);
115 struct net_device_stats *n_stats = &qca->net_dev->stats;
118 spin_lock_bh(&qca->lock);
120 /* First make sure we're connected. */
121 if (!netif_running(qca->net_dev)) {
122 spin_unlock_bh(&qca->lock);
126 if (qca->tx_left <= 0) {
127 /* Now serial buffer is almost free & we can start
128 * transmission of another packet
130 n_stats->tx_packets++;
131 spin_unlock_bh(&qca->lock);
132 netif_wake_queue(qca->net_dev);
136 written = serdev_device_write_buf(qca->serdev, qca->tx_head,
139 qca->tx_left -= written;
140 qca->tx_head += written;
142 spin_unlock_bh(&qca->lock);
145 /* Called by the driver when there's room for more data.
146 * Schedule the transmit.
148 static void qca_tty_wakeup(struct serdev_device *serdev)
150 struct qcauart *qca = serdev_device_get_drvdata(serdev);
152 schedule_work(&qca->tx_work);
155 static const struct serdev_device_ops qca_serdev_ops = {
156 .receive_buf = qca_tty_receive,
157 .write_wakeup = qca_tty_wakeup,
160 static int qcauart_netdev_open(struct net_device *dev)
162 struct qcauart *qca = netdev_priv(dev);
164 netif_start_queue(qca->net_dev);
169 static int qcauart_netdev_close(struct net_device *dev)
171 struct qcauart *qca = netdev_priv(dev);
173 netif_stop_queue(dev);
174 flush_work(&qca->tx_work);
176 spin_lock_bh(&qca->lock);
178 spin_unlock_bh(&qca->lock);
184 qcauart_netdev_xmit(struct sk_buff *skb, struct net_device *dev)
186 struct net_device_stats *n_stats = &dev->stats;
187 struct qcauart *qca = netdev_priv(dev);
192 spin_lock(&qca->lock);
194 WARN_ON(qca->tx_left);
196 if (!netif_running(dev)) {
197 spin_unlock(&qca->lock);
198 netdev_warn(qca->net_dev, "xmit: iface is down\n");
202 pos = qca->tx_buffer;
204 if (skb->len < QCAFRM_MIN_LEN)
205 pad_len = QCAFRM_MIN_LEN - skb->len;
207 pos += qcafrm_create_header(pos, skb->len + pad_len);
209 memcpy(pos, skb->data, skb->len);
213 memset(pos, 0, pad_len);
217 pos += qcafrm_create_footer(pos);
219 netif_stop_queue(qca->net_dev);
221 written = serdev_device_write_buf(qca->serdev, qca->tx_buffer,
222 pos - qca->tx_buffer);
224 qca->tx_left = (pos - qca->tx_buffer) - written;
225 qca->tx_head = qca->tx_buffer + written;
226 n_stats->tx_bytes += written;
228 spin_unlock(&qca->lock);
230 netif_trans_update(dev);
232 dev_kfree_skb_any(skb);
236 static void qcauart_netdev_tx_timeout(struct net_device *dev, unsigned int txqueue)
238 struct qcauart *qca = netdev_priv(dev);
240 netdev_info(qca->net_dev, "Transmit timeout at %ld, latency %ld\n",
241 jiffies, dev_trans_start(dev));
242 dev->stats.tx_errors++;
243 dev->stats.tx_dropped++;
246 static int qcauart_netdev_init(struct net_device *dev)
248 struct qcauart *qca = netdev_priv(dev);
251 /* Finish setting up the device info. */
252 dev->mtu = QCAFRM_MAX_MTU;
253 dev->type = ARPHRD_ETHER;
255 len = QCAFRM_HEADER_LEN + QCAFRM_MAX_LEN + QCAFRM_FOOTER_LEN;
256 qca->tx_buffer = devm_kmalloc(&qca->serdev->dev, len, GFP_KERNEL);
260 qca->rx_skb = netdev_alloc_skb_ip_align(qca->net_dev,
269 static void qcauart_netdev_uninit(struct net_device *dev)
271 struct qcauart *qca = netdev_priv(dev);
273 dev_kfree_skb(qca->rx_skb);
276 static const struct net_device_ops qcauart_netdev_ops = {
277 .ndo_init = qcauart_netdev_init,
278 .ndo_uninit = qcauart_netdev_uninit,
279 .ndo_open = qcauart_netdev_open,
280 .ndo_stop = qcauart_netdev_close,
281 .ndo_start_xmit = qcauart_netdev_xmit,
282 .ndo_set_mac_address = eth_mac_addr,
283 .ndo_tx_timeout = qcauart_netdev_tx_timeout,
284 .ndo_validate_addr = eth_validate_addr,
287 static void qcauart_netdev_setup(struct net_device *dev)
289 dev->netdev_ops = &qcauart_netdev_ops;
290 dev->watchdog_timeo = QCAUART_TX_TIMEOUT;
291 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
292 dev->tx_queue_len = 100;
294 /* MTU range: 46 - 1500 */
295 dev->min_mtu = QCAFRM_MIN_MTU;
296 dev->max_mtu = QCAFRM_MAX_MTU;
299 static const struct of_device_id qca_uart_of_match[] = {
301 .compatible = "qca,qca7000",
305 MODULE_DEVICE_TABLE(of, qca_uart_of_match);
307 static int qca_uart_probe(struct serdev_device *serdev)
309 struct net_device *qcauart_dev = alloc_etherdev(sizeof(struct qcauart));
317 qcauart_netdev_setup(qcauart_dev);
318 SET_NETDEV_DEV(qcauart_dev, &serdev->dev);
320 qca = netdev_priv(qcauart_dev);
322 pr_err("qca_uart: Fail to retrieve private structure\n");
326 qca->net_dev = qcauart_dev;
327 qca->serdev = serdev;
328 qcafrm_fsm_init_uart(&qca->frm_handle);
330 spin_lock_init(&qca->lock);
331 INIT_WORK(&qca->tx_work, qcauart_transmit);
333 of_property_read_u32(serdev->dev.of_node, "current-speed", &speed);
335 ret = of_get_ethdev_address(serdev->dev.of_node, qca->net_dev);
337 eth_hw_addr_random(qca->net_dev);
338 dev_info(&serdev->dev, "Using random MAC address: %pM\n",
339 qca->net_dev->dev_addr);
342 netif_carrier_on(qca->net_dev);
343 serdev_device_set_drvdata(serdev, qca);
344 serdev_device_set_client_ops(serdev, &qca_serdev_ops);
346 ret = serdev_device_open(serdev);
348 dev_err(&serdev->dev, "Unable to open device %s\n",
353 speed = serdev_device_set_baudrate(serdev, speed);
354 dev_info(&serdev->dev, "Using baudrate: %u\n", speed);
356 serdev_device_set_flow_control(serdev, false);
358 ret = register_netdev(qcauart_dev);
360 dev_err(&serdev->dev, "Unable to register net device %s\n",
362 serdev_device_close(serdev);
363 cancel_work_sync(&qca->tx_work);
370 free_netdev(qcauart_dev);
374 static void qca_uart_remove(struct serdev_device *serdev)
376 struct qcauart *qca = serdev_device_get_drvdata(serdev);
378 unregister_netdev(qca->net_dev);
380 /* Flush any pending characters in the driver. */
381 serdev_device_close(serdev);
382 cancel_work_sync(&qca->tx_work);
384 free_netdev(qca->net_dev);
387 static struct serdev_device_driver qca_uart_driver = {
388 .probe = qca_uart_probe,
389 .remove = qca_uart_remove,
391 .name = QCAUART_DRV_NAME,
392 .of_match_table = qca_uart_of_match,
396 module_serdev_device_driver(qca_uart_driver);
398 MODULE_DESCRIPTION("Qualcomm Atheros QCA7000 UART Driver");
399 MODULE_AUTHOR("Qualcomm Atheros Communications");
400 MODULE_AUTHOR("Stefan Wahren <wahrenst@gmx.net>");
401 MODULE_LICENSE("Dual BSD/GPL");
402 MODULE_VERSION(QCAUART_DRV_VERSION);