0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025 #include <linux/device.h>
0026 #include <linux/errno.h>
0027 #include <linux/etherdevice.h>
0028 #include <linux/if_arp.h>
0029 #include <linux/if_ether.h>
0030 #include <linux/jiffies.h>
0031 #include <linux/kernel.h>
0032 #include <linux/module.h>
0033 #include <linux/netdevice.h>
0034 #include <linux/of.h>
0035 #include <linux/of_device.h>
0036 #include <linux/of_net.h>
0037 #include <linux/sched.h>
0038 #include <linux/serdev.h>
0039 #include <linux/skbuff.h>
0040 #include <linux/types.h>
0041
0042 #include "qca_7k_common.h"
0043
0044 #define QCAUART_DRV_VERSION "0.1.0"
0045 #define QCAUART_DRV_NAME "qcauart"
0046 #define QCAUART_TX_TIMEOUT (1 * HZ)
0047
0048 struct qcauart {
0049 struct net_device *net_dev;
0050 spinlock_t lock;
0051 struct work_struct tx_work;
0052
0053 struct serdev_device *serdev;
0054 struct qcafrm_handle frm_handle;
0055 struct sk_buff *rx_skb;
0056
0057 unsigned char *tx_head;
0058 int tx_left;
0059 unsigned char *tx_buffer;
0060 };
0061
0062 static int
0063 qca_tty_receive(struct serdev_device *serdev, const unsigned char *data,
0064 size_t count)
0065 {
0066 struct qcauart *qca = serdev_device_get_drvdata(serdev);
0067 struct net_device *netdev = qca->net_dev;
0068 struct net_device_stats *n_stats = &netdev->stats;
0069 size_t i;
0070
0071 if (!qca->rx_skb) {
0072 qca->rx_skb = netdev_alloc_skb_ip_align(netdev,
0073 netdev->mtu +
0074 VLAN_ETH_HLEN);
0075 if (!qca->rx_skb) {
0076 n_stats->rx_errors++;
0077 n_stats->rx_dropped++;
0078 return 0;
0079 }
0080 }
0081
0082 for (i = 0; i < count; i++) {
0083 s32 retcode;
0084
0085 retcode = qcafrm_fsm_decode(&qca->frm_handle,
0086 qca->rx_skb->data,
0087 skb_tailroom(qca->rx_skb),
0088 data[i]);
0089
0090 switch (retcode) {
0091 case QCAFRM_GATHER:
0092 case QCAFRM_NOHEAD:
0093 break;
0094 case QCAFRM_NOTAIL:
0095 netdev_dbg(netdev, "recv: no RX tail\n");
0096 n_stats->rx_errors++;
0097 n_stats->rx_dropped++;
0098 break;
0099 case QCAFRM_INVLEN:
0100 netdev_dbg(netdev, "recv: invalid RX length\n");
0101 n_stats->rx_errors++;
0102 n_stats->rx_dropped++;
0103 break;
0104 default:
0105 n_stats->rx_packets++;
0106 n_stats->rx_bytes += retcode;
0107 skb_put(qca->rx_skb, retcode);
0108 qca->rx_skb->protocol = eth_type_trans(
0109 qca->rx_skb, qca->rx_skb->dev);
0110 skb_checksum_none_assert(qca->rx_skb);
0111 netif_rx(qca->rx_skb);
0112 qca->rx_skb = netdev_alloc_skb_ip_align(netdev,
0113 netdev->mtu +
0114 VLAN_ETH_HLEN);
0115 if (!qca->rx_skb) {
0116 netdev_dbg(netdev, "recv: out of RX resources\n");
0117 n_stats->rx_errors++;
0118 return i;
0119 }
0120 }
0121 }
0122
0123 return i;
0124 }
0125
0126
0127 static void qcauart_transmit(struct work_struct *work)
0128 {
0129 struct qcauart *qca = container_of(work, struct qcauart, tx_work);
0130 struct net_device_stats *n_stats = &qca->net_dev->stats;
0131 int written;
0132
0133 spin_lock_bh(&qca->lock);
0134
0135
0136 if (!netif_running(qca->net_dev)) {
0137 spin_unlock_bh(&qca->lock);
0138 return;
0139 }
0140
0141 if (qca->tx_left <= 0) {
0142
0143
0144
0145 n_stats->tx_packets++;
0146 spin_unlock_bh(&qca->lock);
0147 netif_wake_queue(qca->net_dev);
0148 return;
0149 }
0150
0151 written = serdev_device_write_buf(qca->serdev, qca->tx_head,
0152 qca->tx_left);
0153 if (written > 0) {
0154 qca->tx_left -= written;
0155 qca->tx_head += written;
0156 }
0157 spin_unlock_bh(&qca->lock);
0158 }
0159
0160
0161
0162
0163 static void qca_tty_wakeup(struct serdev_device *serdev)
0164 {
0165 struct qcauart *qca = serdev_device_get_drvdata(serdev);
0166
0167 schedule_work(&qca->tx_work);
0168 }
0169
0170 static const struct serdev_device_ops qca_serdev_ops = {
0171 .receive_buf = qca_tty_receive,
0172 .write_wakeup = qca_tty_wakeup,
0173 };
0174
0175 static int qcauart_netdev_open(struct net_device *dev)
0176 {
0177 struct qcauart *qca = netdev_priv(dev);
0178
0179 netif_start_queue(qca->net_dev);
0180
0181 return 0;
0182 }
0183
0184 static int qcauart_netdev_close(struct net_device *dev)
0185 {
0186 struct qcauart *qca = netdev_priv(dev);
0187
0188 netif_stop_queue(dev);
0189 flush_work(&qca->tx_work);
0190
0191 spin_lock_bh(&qca->lock);
0192 qca->tx_left = 0;
0193 spin_unlock_bh(&qca->lock);
0194
0195 return 0;
0196 }
0197
0198 static netdev_tx_t
0199 qcauart_netdev_xmit(struct sk_buff *skb, struct net_device *dev)
0200 {
0201 struct net_device_stats *n_stats = &dev->stats;
0202 struct qcauart *qca = netdev_priv(dev);
0203 u8 pad_len = 0;
0204 int written;
0205 u8 *pos;
0206
0207 spin_lock(&qca->lock);
0208
0209 WARN_ON(qca->tx_left);
0210
0211 if (!netif_running(dev)) {
0212 spin_unlock(&qca->lock);
0213 netdev_warn(qca->net_dev, "xmit: iface is down\n");
0214 goto out;
0215 }
0216
0217 pos = qca->tx_buffer;
0218
0219 if (skb->len < QCAFRM_MIN_LEN)
0220 pad_len = QCAFRM_MIN_LEN - skb->len;
0221
0222 pos += qcafrm_create_header(pos, skb->len + pad_len);
0223
0224 memcpy(pos, skb->data, skb->len);
0225 pos += skb->len;
0226
0227 if (pad_len) {
0228 memset(pos, 0, pad_len);
0229 pos += pad_len;
0230 }
0231
0232 pos += qcafrm_create_footer(pos);
0233
0234 netif_stop_queue(qca->net_dev);
0235
0236 written = serdev_device_write_buf(qca->serdev, qca->tx_buffer,
0237 pos - qca->tx_buffer);
0238 if (written > 0) {
0239 qca->tx_left = (pos - qca->tx_buffer) - written;
0240 qca->tx_head = qca->tx_buffer + written;
0241 n_stats->tx_bytes += written;
0242 }
0243 spin_unlock(&qca->lock);
0244
0245 netif_trans_update(dev);
0246 out:
0247 dev_kfree_skb_any(skb);
0248 return NETDEV_TX_OK;
0249 }
0250
0251 static void qcauart_netdev_tx_timeout(struct net_device *dev, unsigned int txqueue)
0252 {
0253 struct qcauart *qca = netdev_priv(dev);
0254
0255 netdev_info(qca->net_dev, "Transmit timeout at %ld, latency %ld\n",
0256 jiffies, dev_trans_start(dev));
0257 dev->stats.tx_errors++;
0258 dev->stats.tx_dropped++;
0259 }
0260
0261 static int qcauart_netdev_init(struct net_device *dev)
0262 {
0263 struct qcauart *qca = netdev_priv(dev);
0264 size_t len;
0265
0266
0267 dev->mtu = QCAFRM_MAX_MTU;
0268 dev->type = ARPHRD_ETHER;
0269
0270 len = QCAFRM_HEADER_LEN + QCAFRM_MAX_LEN + QCAFRM_FOOTER_LEN;
0271 qca->tx_buffer = devm_kmalloc(&qca->serdev->dev, len, GFP_KERNEL);
0272 if (!qca->tx_buffer)
0273 return -ENOMEM;
0274
0275 qca->rx_skb = netdev_alloc_skb_ip_align(qca->net_dev,
0276 qca->net_dev->mtu +
0277 VLAN_ETH_HLEN);
0278 if (!qca->rx_skb)
0279 return -ENOBUFS;
0280
0281 return 0;
0282 }
0283
0284 static void qcauart_netdev_uninit(struct net_device *dev)
0285 {
0286 struct qcauart *qca = netdev_priv(dev);
0287
0288 dev_kfree_skb(qca->rx_skb);
0289 }
0290
0291 static const struct net_device_ops qcauart_netdev_ops = {
0292 .ndo_init = qcauart_netdev_init,
0293 .ndo_uninit = qcauart_netdev_uninit,
0294 .ndo_open = qcauart_netdev_open,
0295 .ndo_stop = qcauart_netdev_close,
0296 .ndo_start_xmit = qcauart_netdev_xmit,
0297 .ndo_set_mac_address = eth_mac_addr,
0298 .ndo_tx_timeout = qcauart_netdev_tx_timeout,
0299 .ndo_validate_addr = eth_validate_addr,
0300 };
0301
0302 static void qcauart_netdev_setup(struct net_device *dev)
0303 {
0304 dev->netdev_ops = &qcauart_netdev_ops;
0305 dev->watchdog_timeo = QCAUART_TX_TIMEOUT;
0306 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
0307 dev->tx_queue_len = 100;
0308
0309
0310 dev->min_mtu = QCAFRM_MIN_MTU;
0311 dev->max_mtu = QCAFRM_MAX_MTU;
0312 }
0313
0314 static const struct of_device_id qca_uart_of_match[] = {
0315 {
0316 .compatible = "qca,qca7000",
0317 },
0318 {}
0319 };
0320 MODULE_DEVICE_TABLE(of, qca_uart_of_match);
0321
0322 static int qca_uart_probe(struct serdev_device *serdev)
0323 {
0324 struct net_device *qcauart_dev = alloc_etherdev(sizeof(struct qcauart));
0325 struct qcauart *qca;
0326 u32 speed = 115200;
0327 int ret;
0328
0329 if (!qcauart_dev)
0330 return -ENOMEM;
0331
0332 qcauart_netdev_setup(qcauart_dev);
0333 SET_NETDEV_DEV(qcauart_dev, &serdev->dev);
0334
0335 qca = netdev_priv(qcauart_dev);
0336 if (!qca) {
0337 pr_err("qca_uart: Fail to retrieve private structure\n");
0338 ret = -ENOMEM;
0339 goto free;
0340 }
0341 qca->net_dev = qcauart_dev;
0342 qca->serdev = serdev;
0343 qcafrm_fsm_init_uart(&qca->frm_handle);
0344
0345 spin_lock_init(&qca->lock);
0346 INIT_WORK(&qca->tx_work, qcauart_transmit);
0347
0348 of_property_read_u32(serdev->dev.of_node, "current-speed", &speed);
0349
0350 ret = of_get_ethdev_address(serdev->dev.of_node, qca->net_dev);
0351 if (ret) {
0352 eth_hw_addr_random(qca->net_dev);
0353 dev_info(&serdev->dev, "Using random MAC address: %pM\n",
0354 qca->net_dev->dev_addr);
0355 }
0356
0357 netif_carrier_on(qca->net_dev);
0358 serdev_device_set_drvdata(serdev, qca);
0359 serdev_device_set_client_ops(serdev, &qca_serdev_ops);
0360
0361 ret = serdev_device_open(serdev);
0362 if (ret) {
0363 dev_err(&serdev->dev, "Unable to open device %s\n",
0364 qcauart_dev->name);
0365 goto free;
0366 }
0367
0368 speed = serdev_device_set_baudrate(serdev, speed);
0369 dev_info(&serdev->dev, "Using baudrate: %u\n", speed);
0370
0371 serdev_device_set_flow_control(serdev, false);
0372
0373 ret = register_netdev(qcauart_dev);
0374 if (ret) {
0375 dev_err(&serdev->dev, "Unable to register net device %s\n",
0376 qcauart_dev->name);
0377 serdev_device_close(serdev);
0378 cancel_work_sync(&qca->tx_work);
0379 goto free;
0380 }
0381
0382 return 0;
0383
0384 free:
0385 free_netdev(qcauart_dev);
0386 return ret;
0387 }
0388
0389 static void qca_uart_remove(struct serdev_device *serdev)
0390 {
0391 struct qcauart *qca = serdev_device_get_drvdata(serdev);
0392
0393 unregister_netdev(qca->net_dev);
0394
0395
0396 serdev_device_close(serdev);
0397 cancel_work_sync(&qca->tx_work);
0398
0399 free_netdev(qca->net_dev);
0400 }
0401
0402 static struct serdev_device_driver qca_uart_driver = {
0403 .probe = qca_uart_probe,
0404 .remove = qca_uart_remove,
0405 .driver = {
0406 .name = QCAUART_DRV_NAME,
0407 .of_match_table = of_match_ptr(qca_uart_of_match),
0408 },
0409 };
0410
0411 module_serdev_device_driver(qca_uart_driver);
0412
0413 MODULE_DESCRIPTION("Qualcomm Atheros QCA7000 UART Driver");
0414 MODULE_AUTHOR("Qualcomm Atheros Communications");
0415 MODULE_AUTHOR("Stefan Wahren <stefan.wahren@i2se.com>");
0416 MODULE_LICENSE("Dual BSD/GPL");
0417 MODULE_VERSION(QCAUART_DRV_VERSION);