0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #include <linux/can/core.h>
0018 #include <linux/can/dev.h>
0019 #include <linux/clk.h>
0020 #include <linux/completion.h>
0021 #include <linux/delay.h>
0022 #include <linux/device.h>
0023 #include <linux/ethtool.h>
0024 #include <linux/freezer.h>
0025 #include <linux/interrupt.h>
0026 #include <linux/io.h>
0027 #include <linux/kernel.h>
0028 #include <linux/mod_devicetable.h>
0029 #include <linux/module.h>
0030 #include <linux/netdevice.h>
0031 #include <linux/platform_device.h>
0032 #include <linux/property.h>
0033 #include <linux/regulator/consumer.h>
0034 #include <linux/slab.h>
0035 #include <linux/spi/spi.h>
0036 #include <linux/uaccess.h>
0037
0038 #define HI3110_MASTER_RESET 0x56
0039 #define HI3110_READ_CTRL0 0xD2
0040 #define HI3110_READ_CTRL1 0xD4
0041 #define HI3110_READ_STATF 0xE2
0042 #define HI3110_WRITE_CTRL0 0x14
0043 #define HI3110_WRITE_CTRL1 0x16
0044 #define HI3110_WRITE_INTE 0x1C
0045 #define HI3110_WRITE_BTR0 0x18
0046 #define HI3110_WRITE_BTR1 0x1A
0047 #define HI3110_READ_BTR0 0xD6
0048 #define HI3110_READ_BTR1 0xD8
0049 #define HI3110_READ_INTF 0xDE
0050 #define HI3110_READ_ERR 0xDC
0051 #define HI3110_READ_FIFO_WOTIME 0x48
0052 #define HI3110_WRITE_FIFO 0x12
0053 #define HI3110_READ_MESSTAT 0xDA
0054 #define HI3110_READ_REC 0xEA
0055 #define HI3110_READ_TEC 0xEC
0056
0057 #define HI3110_CTRL0_MODE_MASK (7 << 5)
0058 #define HI3110_CTRL0_NORMAL_MODE (0 << 5)
0059 #define HI3110_CTRL0_LOOPBACK_MODE (1 << 5)
0060 #define HI3110_CTRL0_MONITOR_MODE (2 << 5)
0061 #define HI3110_CTRL0_SLEEP_MODE (3 << 5)
0062 #define HI3110_CTRL0_INIT_MODE (4 << 5)
0063
0064 #define HI3110_CTRL1_TXEN BIT(7)
0065
0066 #define HI3110_INT_RXTMP BIT(7)
0067 #define HI3110_INT_RXFIFO BIT(6)
0068 #define HI3110_INT_TXCPLT BIT(5)
0069 #define HI3110_INT_BUSERR BIT(4)
0070 #define HI3110_INT_MCHG BIT(3)
0071 #define HI3110_INT_WAKEUP BIT(2)
0072 #define HI3110_INT_F1MESS BIT(1)
0073 #define HI3110_INT_F0MESS BIT(0)
0074
0075 #define HI3110_ERR_BUSOFF BIT(7)
0076 #define HI3110_ERR_TXERRP BIT(6)
0077 #define HI3110_ERR_RXERRP BIT(5)
0078 #define HI3110_ERR_BITERR BIT(4)
0079 #define HI3110_ERR_FRMERR BIT(3)
0080 #define HI3110_ERR_CRCERR BIT(2)
0081 #define HI3110_ERR_ACKERR BIT(1)
0082 #define HI3110_ERR_STUFERR BIT(0)
0083 #define HI3110_ERR_PROTOCOL_MASK (0x1F)
0084 #define HI3110_ERR_PASSIVE_MASK (0x60)
0085
0086 #define HI3110_STAT_RXFMTY BIT(1)
0087 #define HI3110_STAT_BUSOFF BIT(2)
0088 #define HI3110_STAT_ERRP BIT(3)
0089 #define HI3110_STAT_ERRW BIT(4)
0090 #define HI3110_STAT_TXMTY BIT(7)
0091
0092 #define HI3110_BTR0_SJW_SHIFT 6
0093 #define HI3110_BTR0_BRP_SHIFT 0
0094
0095 #define HI3110_BTR1_SAMP_3PERBIT (1 << 7)
0096 #define HI3110_BTR1_SAMP_1PERBIT (0 << 7)
0097 #define HI3110_BTR1_TSEG2_SHIFT 4
0098 #define HI3110_BTR1_TSEG1_SHIFT 0
0099
0100 #define HI3110_FIFO_WOTIME_TAG_OFF 0
0101 #define HI3110_FIFO_WOTIME_ID_OFF 1
0102 #define HI3110_FIFO_WOTIME_DLC_OFF 5
0103 #define HI3110_FIFO_WOTIME_DAT_OFF 6
0104
0105 #define HI3110_FIFO_WOTIME_TAG_IDE BIT(7)
0106 #define HI3110_FIFO_WOTIME_ID_RTR BIT(0)
0107
0108 #define HI3110_FIFO_TAG_OFF 0
0109 #define HI3110_FIFO_ID_OFF 1
0110 #define HI3110_FIFO_STD_DLC_OFF 3
0111 #define HI3110_FIFO_STD_DATA_OFF 4
0112 #define HI3110_FIFO_EXT_DLC_OFF 5
0113 #define HI3110_FIFO_EXT_DATA_OFF 6
0114
0115 #define HI3110_CAN_MAX_DATA_LEN 8
0116 #define HI3110_RX_BUF_LEN 15
0117 #define HI3110_TX_STD_BUF_LEN 12
0118 #define HI3110_TX_EXT_BUF_LEN 14
0119 #define HI3110_CAN_FRAME_MAX_BITS 128
0120 #define HI3110_EFF_FLAGS 0x18
0121
0122 #define HI3110_TX_ECHO_SKB_MAX 1
0123
0124 #define HI3110_OST_DELAY_MS (10)
0125
0126 #define DEVICE_NAME "hi3110"
0127
0128 static const struct can_bittiming_const hi3110_bittiming_const = {
0129 .name = DEVICE_NAME,
0130 .tseg1_min = 2,
0131 .tseg1_max = 16,
0132 .tseg2_min = 2,
0133 .tseg2_max = 8,
0134 .sjw_max = 4,
0135 .brp_min = 1,
0136 .brp_max = 64,
0137 .brp_inc = 1,
0138 };
0139
0140 enum hi3110_model {
0141 CAN_HI3110_HI3110 = 0x3110,
0142 };
0143
0144 struct hi3110_priv {
0145 struct can_priv can;
0146 struct net_device *net;
0147 struct spi_device *spi;
0148 enum hi3110_model model;
0149
0150 struct mutex hi3110_lock;
0151
0152 u8 *spi_tx_buf;
0153 u8 *spi_rx_buf;
0154
0155 struct sk_buff *tx_skb;
0156
0157 struct workqueue_struct *wq;
0158 struct work_struct tx_work;
0159 struct work_struct restart_work;
0160
0161 int force_quit;
0162 int after_suspend;
0163 #define HI3110_AFTER_SUSPEND_UP 1
0164 #define HI3110_AFTER_SUSPEND_DOWN 2
0165 #define HI3110_AFTER_SUSPEND_POWER 4
0166 #define HI3110_AFTER_SUSPEND_RESTART 8
0167 int restart_tx;
0168 bool tx_busy;
0169
0170 struct regulator *power;
0171 struct regulator *transceiver;
0172 struct clk *clk;
0173 };
0174
0175 static void hi3110_clean(struct net_device *net)
0176 {
0177 struct hi3110_priv *priv = netdev_priv(net);
0178
0179 if (priv->tx_skb || priv->tx_busy)
0180 net->stats.tx_errors++;
0181 dev_kfree_skb(priv->tx_skb);
0182 if (priv->tx_busy)
0183 can_free_echo_skb(priv->net, 0, NULL);
0184 priv->tx_skb = NULL;
0185 priv->tx_busy = false;
0186 }
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200 static int hi3110_spi_trans(struct spi_device *spi, int len)
0201 {
0202 struct hi3110_priv *priv = spi_get_drvdata(spi);
0203 struct spi_transfer t = {
0204 .tx_buf = priv->spi_tx_buf,
0205 .rx_buf = priv->spi_rx_buf,
0206 .len = len,
0207 .cs_change = 0,
0208 };
0209 struct spi_message m;
0210 int ret;
0211
0212 spi_message_init(&m);
0213 spi_message_add_tail(&t, &m);
0214
0215 ret = spi_sync(spi, &m);
0216
0217 if (ret)
0218 dev_err(&spi->dev, "spi transfer failed: ret = %d\n", ret);
0219 return ret;
0220 }
0221
0222 static int hi3110_cmd(struct spi_device *spi, u8 command)
0223 {
0224 struct hi3110_priv *priv = spi_get_drvdata(spi);
0225
0226 priv->spi_tx_buf[0] = command;
0227 dev_dbg(&spi->dev, "hi3110_cmd: %02X\n", command);
0228
0229 return hi3110_spi_trans(spi, 1);
0230 }
0231
0232 static u8 hi3110_read(struct spi_device *spi, u8 command)
0233 {
0234 struct hi3110_priv *priv = spi_get_drvdata(spi);
0235 u8 val = 0;
0236
0237 priv->spi_tx_buf[0] = command;
0238 hi3110_spi_trans(spi, 2);
0239 val = priv->spi_rx_buf[1];
0240
0241 return val;
0242 }
0243
0244 static void hi3110_write(struct spi_device *spi, u8 reg, u8 val)
0245 {
0246 struct hi3110_priv *priv = spi_get_drvdata(spi);
0247
0248 priv->spi_tx_buf[0] = reg;
0249 priv->spi_tx_buf[1] = val;
0250 hi3110_spi_trans(spi, 2);
0251 }
0252
0253 static void hi3110_hw_tx_frame(struct spi_device *spi, u8 *buf, int len)
0254 {
0255 struct hi3110_priv *priv = spi_get_drvdata(spi);
0256
0257 priv->spi_tx_buf[0] = HI3110_WRITE_FIFO;
0258 memcpy(priv->spi_tx_buf + 1, buf, len);
0259 hi3110_spi_trans(spi, len + 1);
0260 }
0261
0262 static void hi3110_hw_tx(struct spi_device *spi, struct can_frame *frame)
0263 {
0264 u8 buf[HI3110_TX_EXT_BUF_LEN];
0265
0266 buf[HI3110_FIFO_TAG_OFF] = 0;
0267
0268 if (frame->can_id & CAN_EFF_FLAG) {
0269
0270 buf[HI3110_FIFO_ID_OFF] = (frame->can_id & CAN_EFF_MASK) >> 21;
0271 buf[HI3110_FIFO_ID_OFF + 1] =
0272 (((frame->can_id & CAN_EFF_MASK) >> 13) & 0xe0) |
0273 HI3110_EFF_FLAGS |
0274 (((frame->can_id & CAN_EFF_MASK) >> 15) & 0x07);
0275 buf[HI3110_FIFO_ID_OFF + 2] =
0276 (frame->can_id & CAN_EFF_MASK) >> 7;
0277 buf[HI3110_FIFO_ID_OFF + 3] =
0278 ((frame->can_id & CAN_EFF_MASK) << 1) |
0279 ((frame->can_id & CAN_RTR_FLAG) ? 1 : 0);
0280
0281 buf[HI3110_FIFO_EXT_DLC_OFF] = frame->len;
0282
0283 memcpy(buf + HI3110_FIFO_EXT_DATA_OFF,
0284 frame->data, frame->len);
0285
0286 hi3110_hw_tx_frame(spi, buf, HI3110_TX_EXT_BUF_LEN -
0287 (HI3110_CAN_MAX_DATA_LEN - frame->len));
0288 } else {
0289
0290 buf[HI3110_FIFO_ID_OFF] = (frame->can_id & CAN_SFF_MASK) >> 3;
0291 buf[HI3110_FIFO_ID_OFF + 1] =
0292 ((frame->can_id & CAN_SFF_MASK) << 5) |
0293 ((frame->can_id & CAN_RTR_FLAG) ? (1 << 4) : 0);
0294
0295 buf[HI3110_FIFO_STD_DLC_OFF] = frame->len;
0296
0297 memcpy(buf + HI3110_FIFO_STD_DATA_OFF,
0298 frame->data, frame->len);
0299
0300 hi3110_hw_tx_frame(spi, buf, HI3110_TX_STD_BUF_LEN -
0301 (HI3110_CAN_MAX_DATA_LEN - frame->len));
0302 }
0303 }
0304
0305 static void hi3110_hw_rx_frame(struct spi_device *spi, u8 *buf)
0306 {
0307 struct hi3110_priv *priv = spi_get_drvdata(spi);
0308
0309 priv->spi_tx_buf[0] = HI3110_READ_FIFO_WOTIME;
0310 hi3110_spi_trans(spi, HI3110_RX_BUF_LEN);
0311 memcpy(buf, priv->spi_rx_buf + 1, HI3110_RX_BUF_LEN - 1);
0312 }
0313
0314 static void hi3110_hw_rx(struct spi_device *spi)
0315 {
0316 struct hi3110_priv *priv = spi_get_drvdata(spi);
0317 struct sk_buff *skb;
0318 struct can_frame *frame;
0319 u8 buf[HI3110_RX_BUF_LEN - 1];
0320
0321 skb = alloc_can_skb(priv->net, &frame);
0322 if (!skb) {
0323 priv->net->stats.rx_dropped++;
0324 return;
0325 }
0326
0327 hi3110_hw_rx_frame(spi, buf);
0328 if (buf[HI3110_FIFO_WOTIME_TAG_OFF] & HI3110_FIFO_WOTIME_TAG_IDE) {
0329
0330 frame->can_id = CAN_EFF_FLAG;
0331 frame->can_id |=
0332 (buf[HI3110_FIFO_WOTIME_ID_OFF] << 21) |
0333 (((buf[HI3110_FIFO_WOTIME_ID_OFF + 1] & 0xE0) >> 5) << 18) |
0334 ((buf[HI3110_FIFO_WOTIME_ID_OFF + 1] & 0x07) << 15) |
0335 (buf[HI3110_FIFO_WOTIME_ID_OFF + 2] << 7) |
0336 (buf[HI3110_FIFO_WOTIME_ID_OFF + 3] >> 1);
0337 } else {
0338
0339 frame->can_id =
0340 (buf[HI3110_FIFO_WOTIME_ID_OFF] << 3) |
0341 ((buf[HI3110_FIFO_WOTIME_ID_OFF + 1] & 0xE0) >> 5);
0342 }
0343
0344
0345 frame->len = can_cc_dlc2len(buf[HI3110_FIFO_WOTIME_DLC_OFF] & 0x0F);
0346
0347 if (buf[HI3110_FIFO_WOTIME_ID_OFF + 3] & HI3110_FIFO_WOTIME_ID_RTR) {
0348 frame->can_id |= CAN_RTR_FLAG;
0349 } else {
0350 memcpy(frame->data, buf + HI3110_FIFO_WOTIME_DAT_OFF,
0351 frame->len);
0352
0353 priv->net->stats.rx_bytes += frame->len;
0354 }
0355 priv->net->stats.rx_packets++;
0356
0357 netif_rx(skb);
0358 }
0359
0360 static void hi3110_hw_sleep(struct spi_device *spi)
0361 {
0362 hi3110_write(spi, HI3110_WRITE_CTRL0, HI3110_CTRL0_SLEEP_MODE);
0363 }
0364
0365 static netdev_tx_t hi3110_hard_start_xmit(struct sk_buff *skb,
0366 struct net_device *net)
0367 {
0368 struct hi3110_priv *priv = netdev_priv(net);
0369 struct spi_device *spi = priv->spi;
0370
0371 if (priv->tx_skb || priv->tx_busy) {
0372 dev_err(&spi->dev, "hard_xmit called while tx busy\n");
0373 return NETDEV_TX_BUSY;
0374 }
0375
0376 if (can_dropped_invalid_skb(net, skb))
0377 return NETDEV_TX_OK;
0378
0379 netif_stop_queue(net);
0380 priv->tx_skb = skb;
0381 queue_work(priv->wq, &priv->tx_work);
0382
0383 return NETDEV_TX_OK;
0384 }
0385
0386 static int hi3110_do_set_mode(struct net_device *net, enum can_mode mode)
0387 {
0388 struct hi3110_priv *priv = netdev_priv(net);
0389
0390 switch (mode) {
0391 case CAN_MODE_START:
0392 hi3110_clean(net);
0393
0394 priv->can.state = CAN_STATE_ERROR_ACTIVE;
0395 priv->restart_tx = 1;
0396 if (priv->can.restart_ms == 0)
0397 priv->after_suspend = HI3110_AFTER_SUSPEND_RESTART;
0398 queue_work(priv->wq, &priv->restart_work);
0399 break;
0400 default:
0401 return -EOPNOTSUPP;
0402 }
0403
0404 return 0;
0405 }
0406
0407 static int hi3110_get_berr_counter(const struct net_device *net,
0408 struct can_berr_counter *bec)
0409 {
0410 struct hi3110_priv *priv = netdev_priv(net);
0411 struct spi_device *spi = priv->spi;
0412
0413 mutex_lock(&priv->hi3110_lock);
0414 bec->txerr = hi3110_read(spi, HI3110_READ_TEC);
0415 bec->rxerr = hi3110_read(spi, HI3110_READ_REC);
0416 mutex_unlock(&priv->hi3110_lock);
0417
0418 return 0;
0419 }
0420
0421 static int hi3110_set_normal_mode(struct spi_device *spi)
0422 {
0423 struct hi3110_priv *priv = spi_get_drvdata(spi);
0424 u8 reg = 0;
0425
0426 hi3110_write(spi, HI3110_WRITE_INTE, HI3110_INT_BUSERR |
0427 HI3110_INT_RXFIFO | HI3110_INT_TXCPLT);
0428
0429
0430 hi3110_write(spi, HI3110_WRITE_CTRL1, HI3110_CTRL1_TXEN);
0431
0432 if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
0433 reg = HI3110_CTRL0_LOOPBACK_MODE;
0434 else if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
0435 reg = HI3110_CTRL0_MONITOR_MODE;
0436 else
0437 reg = HI3110_CTRL0_NORMAL_MODE;
0438
0439 hi3110_write(spi, HI3110_WRITE_CTRL0, reg);
0440
0441
0442 mdelay(HI3110_OST_DELAY_MS);
0443 reg = hi3110_read(spi, HI3110_READ_CTRL0);
0444 if ((reg & HI3110_CTRL0_MODE_MASK) != reg)
0445 return -EBUSY;
0446
0447 priv->can.state = CAN_STATE_ERROR_ACTIVE;
0448 return 0;
0449 }
0450
0451 static int hi3110_do_set_bittiming(struct net_device *net)
0452 {
0453 struct hi3110_priv *priv = netdev_priv(net);
0454 struct can_bittiming *bt = &priv->can.bittiming;
0455 struct spi_device *spi = priv->spi;
0456
0457 hi3110_write(spi, HI3110_WRITE_BTR0,
0458 ((bt->sjw - 1) << HI3110_BTR0_SJW_SHIFT) |
0459 ((bt->brp - 1) << HI3110_BTR0_BRP_SHIFT));
0460
0461 hi3110_write(spi, HI3110_WRITE_BTR1,
0462 (priv->can.ctrlmode &
0463 CAN_CTRLMODE_3_SAMPLES ?
0464 HI3110_BTR1_SAMP_3PERBIT : HI3110_BTR1_SAMP_1PERBIT) |
0465 ((bt->phase_seg1 + bt->prop_seg - 1)
0466 << HI3110_BTR1_TSEG1_SHIFT) |
0467 ((bt->phase_seg2 - 1) << HI3110_BTR1_TSEG2_SHIFT));
0468
0469 dev_dbg(&spi->dev, "BT: 0x%02x 0x%02x\n",
0470 hi3110_read(spi, HI3110_READ_BTR0),
0471 hi3110_read(spi, HI3110_READ_BTR1));
0472
0473 return 0;
0474 }
0475
0476 static int hi3110_setup(struct net_device *net)
0477 {
0478 hi3110_do_set_bittiming(net);
0479 return 0;
0480 }
0481
0482 static int hi3110_hw_reset(struct spi_device *spi)
0483 {
0484 u8 reg;
0485 int ret;
0486
0487
0488 mdelay(HI3110_OST_DELAY_MS);
0489
0490 ret = hi3110_cmd(spi, HI3110_MASTER_RESET);
0491 if (ret)
0492 return ret;
0493
0494
0495 mdelay(HI3110_OST_DELAY_MS);
0496
0497 reg = hi3110_read(spi, HI3110_READ_CTRL0);
0498 if ((reg & HI3110_CTRL0_MODE_MASK) != HI3110_CTRL0_INIT_MODE)
0499 return -ENODEV;
0500
0501
0502
0503
0504 hi3110_read(spi, HI3110_READ_ERR);
0505
0506 return 0;
0507 }
0508
0509 static int hi3110_hw_probe(struct spi_device *spi)
0510 {
0511 u8 statf;
0512
0513 hi3110_hw_reset(spi);
0514
0515
0516
0517
0518 statf = hi3110_read(spi, HI3110_READ_STATF);
0519
0520 dev_dbg(&spi->dev, "statf: %02X\n", statf);
0521
0522 if (statf != 0x82)
0523 return -ENODEV;
0524
0525 return 0;
0526 }
0527
0528 static int hi3110_power_enable(struct regulator *reg, int enable)
0529 {
0530 if (IS_ERR_OR_NULL(reg))
0531 return 0;
0532
0533 if (enable)
0534 return regulator_enable(reg);
0535 else
0536 return regulator_disable(reg);
0537 }
0538
0539 static int hi3110_stop(struct net_device *net)
0540 {
0541 struct hi3110_priv *priv = netdev_priv(net);
0542 struct spi_device *spi = priv->spi;
0543
0544 close_candev(net);
0545
0546 priv->force_quit = 1;
0547 free_irq(spi->irq, priv);
0548 destroy_workqueue(priv->wq);
0549 priv->wq = NULL;
0550
0551 mutex_lock(&priv->hi3110_lock);
0552
0553
0554 hi3110_write(spi, HI3110_WRITE_CTRL1, 0x0);
0555 hi3110_write(spi, HI3110_WRITE_INTE, 0x0);
0556 hi3110_read(spi, HI3110_READ_INTF);
0557
0558 hi3110_clean(net);
0559
0560 hi3110_hw_sleep(spi);
0561
0562 hi3110_power_enable(priv->transceiver, 0);
0563
0564 priv->can.state = CAN_STATE_STOPPED;
0565
0566 mutex_unlock(&priv->hi3110_lock);
0567
0568 return 0;
0569 }
0570
0571 static void hi3110_tx_work_handler(struct work_struct *ws)
0572 {
0573 struct hi3110_priv *priv = container_of(ws, struct hi3110_priv,
0574 tx_work);
0575 struct spi_device *spi = priv->spi;
0576 struct net_device *net = priv->net;
0577 struct can_frame *frame;
0578
0579 mutex_lock(&priv->hi3110_lock);
0580 if (priv->tx_skb) {
0581 if (priv->can.state == CAN_STATE_BUS_OFF) {
0582 hi3110_clean(net);
0583 } else {
0584 frame = (struct can_frame *)priv->tx_skb->data;
0585 hi3110_hw_tx(spi, frame);
0586 priv->tx_busy = true;
0587 can_put_echo_skb(priv->tx_skb, net, 0, 0);
0588 priv->tx_skb = NULL;
0589 }
0590 }
0591 mutex_unlock(&priv->hi3110_lock);
0592 }
0593
0594 static void hi3110_restart_work_handler(struct work_struct *ws)
0595 {
0596 struct hi3110_priv *priv = container_of(ws, struct hi3110_priv,
0597 restart_work);
0598 struct spi_device *spi = priv->spi;
0599 struct net_device *net = priv->net;
0600
0601 mutex_lock(&priv->hi3110_lock);
0602 if (priv->after_suspend) {
0603 hi3110_hw_reset(spi);
0604 hi3110_setup(net);
0605 if (priv->after_suspend & HI3110_AFTER_SUSPEND_RESTART) {
0606 hi3110_set_normal_mode(spi);
0607 } else if (priv->after_suspend & HI3110_AFTER_SUSPEND_UP) {
0608 netif_device_attach(net);
0609 hi3110_clean(net);
0610 hi3110_set_normal_mode(spi);
0611 netif_wake_queue(net);
0612 } else {
0613 hi3110_hw_sleep(spi);
0614 }
0615 priv->after_suspend = 0;
0616 priv->force_quit = 0;
0617 }
0618
0619 if (priv->restart_tx) {
0620 priv->restart_tx = 0;
0621 hi3110_hw_reset(spi);
0622 hi3110_setup(net);
0623 hi3110_clean(net);
0624 hi3110_set_normal_mode(spi);
0625 netif_wake_queue(net);
0626 }
0627 mutex_unlock(&priv->hi3110_lock);
0628 }
0629
0630 static irqreturn_t hi3110_can_ist(int irq, void *dev_id)
0631 {
0632 struct hi3110_priv *priv = dev_id;
0633 struct spi_device *spi = priv->spi;
0634 struct net_device *net = priv->net;
0635
0636 mutex_lock(&priv->hi3110_lock);
0637
0638 while (!priv->force_quit) {
0639 enum can_state new_state;
0640 u8 intf, eflag, statf;
0641
0642 while (!(HI3110_STAT_RXFMTY &
0643 (statf = hi3110_read(spi, HI3110_READ_STATF)))) {
0644 hi3110_hw_rx(spi);
0645 }
0646
0647 intf = hi3110_read(spi, HI3110_READ_INTF);
0648 eflag = hi3110_read(spi, HI3110_READ_ERR);
0649
0650 if (eflag & HI3110_ERR_BUSOFF)
0651 new_state = CAN_STATE_BUS_OFF;
0652 else if (eflag & HI3110_ERR_PASSIVE_MASK)
0653 new_state = CAN_STATE_ERROR_PASSIVE;
0654 else if (statf & HI3110_STAT_ERRW)
0655 new_state = CAN_STATE_ERROR_WARNING;
0656 else
0657 new_state = CAN_STATE_ERROR_ACTIVE;
0658
0659 if (new_state != priv->can.state) {
0660 struct can_frame *cf;
0661 struct sk_buff *skb;
0662 enum can_state rx_state, tx_state;
0663 u8 rxerr, txerr;
0664
0665 skb = alloc_can_err_skb(net, &cf);
0666 if (!skb)
0667 break;
0668
0669 txerr = hi3110_read(spi, HI3110_READ_TEC);
0670 rxerr = hi3110_read(spi, HI3110_READ_REC);
0671 tx_state = txerr >= rxerr ? new_state : 0;
0672 rx_state = txerr <= rxerr ? new_state : 0;
0673 can_change_state(net, cf, tx_state, rx_state);
0674 netif_rx(skb);
0675
0676 if (new_state == CAN_STATE_BUS_OFF) {
0677 can_bus_off(net);
0678 if (priv->can.restart_ms == 0) {
0679 priv->force_quit = 1;
0680 hi3110_hw_sleep(spi);
0681 break;
0682 }
0683 } else {
0684 cf->can_id |= CAN_ERR_CNT;
0685 cf->data[6] = txerr;
0686 cf->data[7] = rxerr;
0687 }
0688 }
0689
0690
0691 if ((intf & HI3110_INT_BUSERR) &&
0692 (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)) {
0693 struct can_frame *cf;
0694 struct sk_buff *skb;
0695
0696
0697 if (eflag & HI3110_ERR_PROTOCOL_MASK) {
0698 skb = alloc_can_err_skb(net, &cf);
0699 if (!skb)
0700 break;
0701
0702 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
0703 priv->can.can_stats.bus_error++;
0704 priv->net->stats.rx_errors++;
0705 if (eflag & HI3110_ERR_BITERR)
0706 cf->data[2] |= CAN_ERR_PROT_BIT;
0707 else if (eflag & HI3110_ERR_FRMERR)
0708 cf->data[2] |= CAN_ERR_PROT_FORM;
0709 else if (eflag & HI3110_ERR_STUFERR)
0710 cf->data[2] |= CAN_ERR_PROT_STUFF;
0711 else if (eflag & HI3110_ERR_CRCERR)
0712 cf->data[3] |= CAN_ERR_PROT_LOC_CRC_SEQ;
0713 else if (eflag & HI3110_ERR_ACKERR)
0714 cf->data[3] |= CAN_ERR_PROT_LOC_ACK;
0715
0716 cf->data[6] = hi3110_read(spi, HI3110_READ_TEC);
0717 cf->data[7] = hi3110_read(spi, HI3110_READ_REC);
0718 netdev_dbg(priv->net, "Bus Error\n");
0719 netif_rx(skb);
0720 }
0721 }
0722
0723 if (priv->tx_busy && statf & HI3110_STAT_TXMTY) {
0724 net->stats.tx_packets++;
0725 net->stats.tx_bytes += can_get_echo_skb(net, 0, NULL);
0726 priv->tx_busy = false;
0727 netif_wake_queue(net);
0728 }
0729
0730 if (intf == 0)
0731 break;
0732 }
0733 mutex_unlock(&priv->hi3110_lock);
0734 return IRQ_HANDLED;
0735 }
0736
0737 static int hi3110_open(struct net_device *net)
0738 {
0739 struct hi3110_priv *priv = netdev_priv(net);
0740 struct spi_device *spi = priv->spi;
0741 unsigned long flags = IRQF_ONESHOT | IRQF_TRIGGER_HIGH;
0742 int ret;
0743
0744 ret = open_candev(net);
0745 if (ret)
0746 return ret;
0747
0748 mutex_lock(&priv->hi3110_lock);
0749 hi3110_power_enable(priv->transceiver, 1);
0750
0751 priv->force_quit = 0;
0752 priv->tx_skb = NULL;
0753 priv->tx_busy = false;
0754
0755 ret = request_threaded_irq(spi->irq, NULL, hi3110_can_ist,
0756 flags, DEVICE_NAME, priv);
0757 if (ret) {
0758 dev_err(&spi->dev, "failed to acquire irq %d\n", spi->irq);
0759 goto out_close;
0760 }
0761
0762 priv->wq = alloc_workqueue("hi3110_wq", WQ_FREEZABLE | WQ_MEM_RECLAIM,
0763 0);
0764 if (!priv->wq) {
0765 ret = -ENOMEM;
0766 goto out_free_irq;
0767 }
0768 INIT_WORK(&priv->tx_work, hi3110_tx_work_handler);
0769 INIT_WORK(&priv->restart_work, hi3110_restart_work_handler);
0770
0771 ret = hi3110_hw_reset(spi);
0772 if (ret)
0773 goto out_free_wq;
0774
0775 ret = hi3110_setup(net);
0776 if (ret)
0777 goto out_free_wq;
0778
0779 ret = hi3110_set_normal_mode(spi);
0780 if (ret)
0781 goto out_free_wq;
0782
0783 netif_wake_queue(net);
0784 mutex_unlock(&priv->hi3110_lock);
0785
0786 return 0;
0787
0788 out_free_wq:
0789 destroy_workqueue(priv->wq);
0790 out_free_irq:
0791 free_irq(spi->irq, priv);
0792 hi3110_hw_sleep(spi);
0793 out_close:
0794 hi3110_power_enable(priv->transceiver, 0);
0795 close_candev(net);
0796 mutex_unlock(&priv->hi3110_lock);
0797 return ret;
0798 }
0799
0800 static const struct net_device_ops hi3110_netdev_ops = {
0801 .ndo_open = hi3110_open,
0802 .ndo_stop = hi3110_stop,
0803 .ndo_start_xmit = hi3110_hard_start_xmit,
0804 };
0805
0806 static const struct ethtool_ops hi3110_ethtool_ops = {
0807 .get_ts_info = ethtool_op_get_ts_info,
0808 };
0809
0810 static const struct of_device_id hi3110_of_match[] = {
0811 {
0812 .compatible = "holt,hi3110",
0813 .data = (void *)CAN_HI3110_HI3110,
0814 },
0815 { }
0816 };
0817 MODULE_DEVICE_TABLE(of, hi3110_of_match);
0818
0819 static const struct spi_device_id hi3110_id_table[] = {
0820 {
0821 .name = "hi3110",
0822 .driver_data = (kernel_ulong_t)CAN_HI3110_HI3110,
0823 },
0824 { }
0825 };
0826 MODULE_DEVICE_TABLE(spi, hi3110_id_table);
0827
0828 static int hi3110_can_probe(struct spi_device *spi)
0829 {
0830 struct device *dev = &spi->dev;
0831 struct net_device *net;
0832 struct hi3110_priv *priv;
0833 const void *match;
0834 struct clk *clk;
0835 u32 freq;
0836 int ret;
0837
0838 clk = devm_clk_get_optional(&spi->dev, NULL);
0839 if (IS_ERR(clk))
0840 return dev_err_probe(dev, PTR_ERR(clk), "no CAN clock source defined\n");
0841
0842 if (clk) {
0843 freq = clk_get_rate(clk);
0844 } else {
0845 ret = device_property_read_u32(dev, "clock-frequency", &freq);
0846 if (ret)
0847 return dev_err_probe(dev, ret, "Failed to get clock-frequency!\n");
0848 }
0849
0850
0851 if (freq > 40000000)
0852 return -ERANGE;
0853
0854
0855 net = alloc_candev(sizeof(struct hi3110_priv), HI3110_TX_ECHO_SKB_MAX);
0856 if (!net)
0857 return -ENOMEM;
0858
0859 ret = clk_prepare_enable(clk);
0860 if (ret)
0861 goto out_free;
0862
0863 net->netdev_ops = &hi3110_netdev_ops;
0864 net->ethtool_ops = &hi3110_ethtool_ops;
0865 net->flags |= IFF_ECHO;
0866
0867 priv = netdev_priv(net);
0868 priv->can.bittiming_const = &hi3110_bittiming_const;
0869 priv->can.do_set_mode = hi3110_do_set_mode;
0870 priv->can.do_get_berr_counter = hi3110_get_berr_counter;
0871 priv->can.clock.freq = freq / 2;
0872 priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES |
0873 CAN_CTRLMODE_LOOPBACK |
0874 CAN_CTRLMODE_LISTENONLY |
0875 CAN_CTRLMODE_BERR_REPORTING;
0876
0877 match = device_get_match_data(dev);
0878 if (match)
0879 priv->model = (enum hi3110_model)(uintptr_t)match;
0880 else
0881 priv->model = spi_get_device_id(spi)->driver_data;
0882 priv->net = net;
0883 priv->clk = clk;
0884
0885 spi_set_drvdata(spi, priv);
0886
0887
0888 spi->bits_per_word = 8;
0889 ret = spi_setup(spi);
0890 if (ret)
0891 goto out_clk;
0892
0893 priv->power = devm_regulator_get_optional(&spi->dev, "vdd");
0894 priv->transceiver = devm_regulator_get_optional(&spi->dev, "xceiver");
0895 if ((PTR_ERR(priv->power) == -EPROBE_DEFER) ||
0896 (PTR_ERR(priv->transceiver) == -EPROBE_DEFER)) {
0897 ret = -EPROBE_DEFER;
0898 goto out_clk;
0899 }
0900
0901 ret = hi3110_power_enable(priv->power, 1);
0902 if (ret)
0903 goto out_clk;
0904
0905 priv->spi = spi;
0906 mutex_init(&priv->hi3110_lock);
0907
0908 priv->spi_tx_buf = devm_kzalloc(&spi->dev, HI3110_RX_BUF_LEN,
0909 GFP_KERNEL);
0910 if (!priv->spi_tx_buf) {
0911 ret = -ENOMEM;
0912 goto error_probe;
0913 }
0914 priv->spi_rx_buf = devm_kzalloc(&spi->dev, HI3110_RX_BUF_LEN,
0915 GFP_KERNEL);
0916
0917 if (!priv->spi_rx_buf) {
0918 ret = -ENOMEM;
0919 goto error_probe;
0920 }
0921
0922 SET_NETDEV_DEV(net, &spi->dev);
0923
0924 ret = hi3110_hw_probe(spi);
0925 if (ret) {
0926 dev_err_probe(dev, ret, "Cannot initialize %x. Wrong wiring?\n", priv->model);
0927 goto error_probe;
0928 }
0929 hi3110_hw_sleep(spi);
0930
0931 ret = register_candev(net);
0932 if (ret)
0933 goto error_probe;
0934
0935 netdev_info(net, "%x successfully initialized.\n", priv->model);
0936
0937 return 0;
0938
0939 error_probe:
0940 hi3110_power_enable(priv->power, 0);
0941
0942 out_clk:
0943 clk_disable_unprepare(clk);
0944
0945 out_free:
0946 free_candev(net);
0947
0948 return dev_err_probe(dev, ret, "Probe failed\n");
0949 }
0950
0951 static void hi3110_can_remove(struct spi_device *spi)
0952 {
0953 struct hi3110_priv *priv = spi_get_drvdata(spi);
0954 struct net_device *net = priv->net;
0955
0956 unregister_candev(net);
0957
0958 hi3110_power_enable(priv->power, 0);
0959
0960 clk_disable_unprepare(priv->clk);
0961
0962 free_candev(net);
0963 }
0964
0965 static int __maybe_unused hi3110_can_suspend(struct device *dev)
0966 {
0967 struct spi_device *spi = to_spi_device(dev);
0968 struct hi3110_priv *priv = spi_get_drvdata(spi);
0969 struct net_device *net = priv->net;
0970
0971 priv->force_quit = 1;
0972 disable_irq(spi->irq);
0973
0974
0975
0976
0977 if (netif_running(net)) {
0978 netif_device_detach(net);
0979
0980 hi3110_hw_sleep(spi);
0981 hi3110_power_enable(priv->transceiver, 0);
0982 priv->after_suspend = HI3110_AFTER_SUSPEND_UP;
0983 } else {
0984 priv->after_suspend = HI3110_AFTER_SUSPEND_DOWN;
0985 }
0986
0987 if (!IS_ERR_OR_NULL(priv->power)) {
0988 regulator_disable(priv->power);
0989 priv->after_suspend |= HI3110_AFTER_SUSPEND_POWER;
0990 }
0991
0992 return 0;
0993 }
0994
0995 static int __maybe_unused hi3110_can_resume(struct device *dev)
0996 {
0997 struct spi_device *spi = to_spi_device(dev);
0998 struct hi3110_priv *priv = spi_get_drvdata(spi);
0999
1000 if (priv->after_suspend & HI3110_AFTER_SUSPEND_POWER)
1001 hi3110_power_enable(priv->power, 1);
1002
1003 if (priv->after_suspend & HI3110_AFTER_SUSPEND_UP) {
1004 hi3110_power_enable(priv->transceiver, 1);
1005 queue_work(priv->wq, &priv->restart_work);
1006 } else {
1007 priv->after_suspend = 0;
1008 }
1009
1010 priv->force_quit = 0;
1011 enable_irq(spi->irq);
1012 return 0;
1013 }
1014
1015 static SIMPLE_DEV_PM_OPS(hi3110_can_pm_ops, hi3110_can_suspend, hi3110_can_resume);
1016
1017 static struct spi_driver hi3110_can_driver = {
1018 .driver = {
1019 .name = DEVICE_NAME,
1020 .of_match_table = hi3110_of_match,
1021 .pm = &hi3110_can_pm_ops,
1022 },
1023 .id_table = hi3110_id_table,
1024 .probe = hi3110_can_probe,
1025 .remove = hi3110_can_remove,
1026 };
1027
1028 module_spi_driver(hi3110_can_driver);
1029
1030 MODULE_AUTHOR("Akshay Bhat <akshay.bhat@timesys.com>");
1031 MODULE_AUTHOR("Casey Fitzpatrick <casey.fitzpatrick@timesys.com>");
1032 MODULE_DESCRIPTION("Holt HI-3110 CAN driver");
1033 MODULE_LICENSE("GPL v2");