#include <linux/delay.h>
#include "apalis-tk1-k20-ezp.h"
-
+#define CONFIG_EXPERIMENTAL_K20_HSMODE
+#define APALIS_TK1_K20_MAX_MSG 4
static const struct spi_device_id apalis_tk1_k20_device_ids[] = {
{
.name = "apalis-tk1-k20",
struct device *dev = context;
struct spi_device *spi = to_spi_device(dev);
uint8_t out_data[APALIS_TK1_K20_MAX_BULK];
+ uint8_t in_data[APALIS_TK1_K20_MAX_BULK];
int ret;
#ifdef CONFIG_EXPERIMENTAL_K20_HSMODE
- struct spi_device *spi = to_spi_device(dev);
+ struct spi_message m;
struct spi_transfer t = {
.tx_buf = out_data,
- .rx_buf = NULL,
+ .rx_buf = in_data,
.cs_change = 0,
- .delay_usecs = 2,
+ .delay_usecs = 0,
};
- struct spi_transfer ts[APALIS_TK1_K20_MAX_BULK / APALIS_TK1_K20_MAX_MSG];
+ struct spi_transfer ts[(APALIS_TK1_K20_MAX_BULK /
+ APALIS_TK1_K20_MAX_MSG) + 1];
int i = 0;
#endif
out_data[2] = ((uint8_t *)data)[1];
ret = spi_write(spi, out_data, 3);
#ifdef CONFIG_EXPERIMENTAL_K20_HSMODE
- } else if (count == 2) {
- out_data[0] = APALIS_TK1_K20_BULK_WRITE_INST;
- out_data[1] = count - 1;
- memcpy(&out_data[2], data, count);
- ret = spi_write(spi, out_data, 4);
} else if ( (count > 2 ) && (count < APALIS_TK1_K20_MAX_BULK)) {
spi_message_init(&m);
ts[i].len = (count - 2 - (4 * i) >= 4) ?
4 : (count - 2 - (4 * i));
ts[i].cs_change = 0;
- ts[i].delay_usecs = 2;
+ ts[i].delay_usecs = 0;
spi_message_add_tail(&ts[i], &m);
}
ret = spi_sync(spi, &m);
- }
#endif
} else {
dev_err(dev, "Apalis TK1 K20 MFD Invalid write count = %d\n",
#include <linux/slab.h>
#include <linux/uaccess.h>
-#define GET_BYTE(val, byte) \
- (((val) >> ((byte) * 8)) & 0xff)
-#define SET_BYTE(val, byte) \
- (((val) & 0xff) << ((byte) * 8))
-
/* Buffer size required for the largest transfer (i.e., reading a
* frame)
*/
{
/* TODO: Implement multiple TX buffer handling */
struct apalis_tk1_k20_priv *priv = netdev_priv(net);
-
apalis_tk1_k20_lock(priv->apalis_tk1_k20);
-
apalis_tk1_k20_reg_write_bulk(priv->apalis_tk1_k20,
APALIS_TK1_K20_CAN_OUT_BUF
+ APALIS_TK1_K20_CAN_DEV_OFFSET(priv->pdata->id), buf,
memcpy(buf + MB_EID_OFF, &frame->can_id, MB_EID_LEN);
memcpy(buf + CAN_HEADER_MAX_LEN, frame->data, frame->can_dlc);
- apalis_tk1_k20_can_hw_tx_frame(net, buf, frame->can_dlc, tx_buf_idx);
+ apalis_tk1_k20_can_hw_tx_frame(net, buf, frame->can_dlc
+ + CAN_HEADER_MAX_LEN, tx_buf_idx);
}
static void apalis_tk1_k20_can_hw_rx_frame(struct net_device *net, u8 *buf,
int buf_idx)
{
- /* TODO: Implement multiple RX buffer handling */
struct apalis_tk1_k20_priv *priv = netdev_priv(net);
apalis_tk1_k20_lock(priv->apalis_tk1_k20);
apalis_tk1_k20_unlock(priv->apalis_tk1_k20);
}
+static u32 apalis_tk1_k20_can_available_rx_frames (struct net_device *net)
+{
+ u32 frame_cnt = 0;
+ struct apalis_tk1_k20_priv *priv = netdev_priv(net);
+ int ret;
+ apalis_tk1_k20_lock(priv->apalis_tk1_k20);
+
+ ret = apalis_tk1_k20_reg_read(priv->apalis_tk1_k20,
+ APALIS_TK1_K20_CAN_IN_BUF_CNT
+ + APALIS_TK1_K20_CAN_DEV_OFFSET(priv->pdata->id),
+ frame_cnt);
+ apalis_tk1_k20_unlock(priv->apalis_tk1_k20);
+ return (ret == 0) ? frame_cnt : 0;
+}
+
static void apalis_tk1_k20_can_hw_rx(struct net_device *net, int buf_idx)
{
struct apalis_tk1_k20_priv *priv = netdev_priv(net);
struct sk_buff *skb;
struct can_frame *frame;
+ u32 available_frames = 0;
u8 buf[CAN_TRANSFER_BUF_LEN];
skb = alloc_can_skb(priv->net, &frame);
return;
}
- apalis_tk1_k20_can_hw_rx_frame(net, buf, buf_idx);
- memcpy(&frame->can_id, buf + MB_EID_OFF, MB_EID_LEN);
- /* Data length */
- frame->can_dlc = get_can_dlc(buf[MB_DLC_OFF]);
- memcpy(frame->data, buf + CAN_HEADER_MAX_LEN, frame->can_dlc);
+ available_frames = apalis_tk1_k20_can_available_rx_frames(net);
- priv->net->stats.rx_packets++;
- priv->net->stats.rx_bytes += frame->can_dlc;
+ while ((available_frames > 0)) {
+ apalis_tk1_k20_can_hw_rx_frame(net, buf, buf_idx);
+ memcpy(&frame->can_id, buf + MB_EID_OFF, MB_EID_LEN);
+ /* Data length */
+ frame->can_dlc = get_can_dlc(buf[MB_DLC_OFF]);
+ memcpy(frame->data, buf + CAN_HEADER_MAX_LEN, frame->can_dlc);
- can_led_event(priv->net, CAN_LED_EVENT_RX);
+ priv->net->stats.rx_packets++;
+ priv->net->stats.rx_bytes += frame->can_dlc;
- netif_rx_ni(skb);
+ can_led_event(priv->net, CAN_LED_EVENT_RX);
+
+ netif_rx_ni(skb);
+ available_frames--;
+ }
}
static netdev_tx_t apalis_tk1_k20_can_hard_start_xmit(struct sk_buff *skb,
struct apalis_tk1_k20_priv *priv = netdev_priv(net);
struct can_bittiming *bt = &priv->can.bittiming;
+ if ((bt->bitrate / APALIS_TK1_CAN_CLK_UNIT) > 0xFF)
+ return -EINVAL;
+
apalis_tk1_k20_lock(priv->apalis_tk1_k20);
apalis_tk1_k20_reg_write(priv->apalis_tk1_k20,
APALIS_TK1_K20_CAN_BAUD_REG
+ APALIS_TK1_K20_CAN_DEV_OFFSET(priv->pdata->id),
- bt->bitrate);
+ (bt->bitrate / APALIS_TK1_CAN_CLK_UNIT) && 0xFF);
apalis_tk1_k20_reg_write(priv->apalis_tk1_k20,
APALIS_TK1_K20_CAN_BIT_1
+ APALIS_TK1_K20_CAN_DEV_OFFSET(priv->pdata->id),
((bt->sjw & 0x3) << 6) | ((bt->phase_seg2 & 0x7) << 3)
- | (bt->phase_seg2 & 0x7));
+ | (bt->phase_seg1 & 0x7));
apalis_tk1_k20_reg_write(priv->apalis_tk1_k20,
APALIS_TK1_K20_CAN_BIT_2
+ APALIS_TK1_K20_CAN_DEV_OFFSET(priv->pdata->id),
priv->wq = NULL;
mutex_lock(&priv->apalis_tk1_k20_can_lock);
+ apalis_tk1_k20_lock(priv->apalis_tk1_k20);
if (pdata->id == 0)
apalis_tk1_k20_irq_mask(priv->apalis_tk1_k20,
APALIS_TK1_K20_CAN0_IRQ);
APALIS_TK1_K20_CAN1_IRQ);
/* Disable and clear pending interrupts */
priv->can.state = CAN_STATE_STOPPED;
-
+ apalis_tk1_k20_unlock(priv->apalis_tk1_k20);
mutex_unlock(&priv->apalis_tk1_k20_can_lock);
can_led_event(net, CAN_LED_EVENT_STOP);
priv->force_quit = 1;
mutex_lock(&priv->apalis_tk1_k20_can_lock);
+ apalis_tk1_k20_lock(priv->apalis_tk1_k20);
if (pdata->id == 0)
apalis_tk1_k20_irq_mask(priv->apalis_tk1_k20,
APALIS_TK1_K20_CAN0_IRQ);
apalis_tk1_k20_irq_mask(priv->apalis_tk1_k20,
APALIS_TK1_K20_CAN1_IRQ);
/* Disable interrupts */
-
+ apalis_tk1_k20_unlock(priv->apalis_tk1_k20);
mutex_unlock(&priv->apalis_tk1_k20_can_lock);
/*
* Note: at this point neither IST nor workqueues are running.
priv->force_quit = 0;
mutex_lock(&priv->apalis_tk1_k20_can_lock);
+ apalis_tk1_k20_lock(priv->apalis_tk1_k20);
if (pdata->id == 0)
apalis_tk1_k20_irq_unmask(priv->apalis_tk1_k20,
APALIS_TK1_K20_CAN0_IRQ);
APALIS_TK1_K20_CAN1_IRQ);
/* Enable interrupts */
priv->can.state = CAN_STATE_STOPPED;
-
+ apalis_tk1_k20_unlock(priv->apalis_tk1_k20);
mutex_unlock(&priv->apalis_tk1_k20_can_lock);
return 0;
}
priv->force_quit = 0;
priv->tx_skb = NULL;
priv->tx_len = 0;
-
apalis_tk1_k20_lock(priv->apalis_tk1_k20);
-
if (pdata->id == 0)
ret = apalis_tk1_k20_irq_request(priv->apalis_tk1_k20,
APALIS_TK1_K20_CAN0_IRQ, apalis_tk1_k20_can_ist,
ret = apalis_tk1_k20_irq_request(priv->apalis_tk1_k20,
APALIS_TK1_K20_CAN1_IRQ, apalis_tk1_k20_can_ist,
DEVICE_NAME, priv);
-
+ apalis_tk1_k20_unlock(priv->apalis_tk1_k20);
if (ret) {
dev_err(&net->dev, "failed to acquire IRQ\n");
close_candev(net);
apalis_tk1_k20_can_open_clean(net);
goto open_unlock;
}
+
ret = apalis_tk1_k20_can_setup(net, priv);
if (ret) {
apalis_tk1_k20_can_open_clean(net);
goto open_unlock;
}
+
ret = apalis_tk1_k20_can_set_normal_mode(net);
if (ret) {
apalis_tk1_k20_can_open_clean(net);
netif_wake_queue(net);
open_unlock:
- apalis_tk1_k20_unlock(priv->apalis_tk1_k20);
mutex_unlock(&priv->apalis_tk1_k20_can_lock);
return ret;
}
priv = netdev_priv(net);
priv->can.bittiming_const = &apalis_tk1_k20_can_bittiming_const;
priv->can.do_set_mode = apalis_tk1_k20_can_do_set_mode;
+ priv->can.clock.freq = 8000000;
priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES |
CAN_CTRLMODE_LOOPBACK | CAN_CTRLMODE_LISTENONLY;
priv->net = net;
priv->pdata = pdata;
+ priv->apalis_tk1_k20 = dev_get_drvdata(pdev->dev.parent);
mutex_init(&priv->apalis_tk1_k20_can_lock);