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
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142 #include <linux/kernel.h>
0143 #include <linux/module.h>
0144 #include <linux/init.h>
0145 #include <linux/slab.h>
0146 #include <linux/interrupt.h>
0147 #include <linux/atomic.h>
0148 #include <linux/kthread.h>
0149 #include <linux/kfifo.h>
0150 #include <linux/spinlock.h>
0151 #include <linux/uaccess.h>
0152 #include <linux/regmap.h>
0153 #include <linux/of.h>
0154 #include <linux/random.h>
0155 #include <linux/kref.h>
0156 #include <linux/of_address.h>
0157
0158
0159 #include "anybuss-client.h"
0160 #include "anybuss-controller.h"
0161
0162 #define DPRAM_SIZE 0x800
0163 #define MAX_MBOX_MSG_SZ 0x0FF
0164 #define TIMEOUT (HZ * 2)
0165 #define MAX_DATA_AREA_SZ 0x200
0166 #define MAX_FBCTRL_AREA_SZ 0x1BE
0167
0168 #define REG_BOOTLOADER_V 0x7C0
0169 #define REG_API_V 0x7C2
0170 #define REG_FIELDBUS_V 0x7C4
0171 #define REG_SERIAL_NO 0x7C6
0172 #define REG_FIELDBUS_TYPE 0x7CC
0173 #define REG_MODULE_SW_V 0x7CE
0174 #define REG_IND_AB 0x7FF
0175 #define REG_IND_AP 0x7FE
0176 #define REG_EVENT_CAUSE 0x7ED
0177 #define MBOX_IN_AREA 0x400
0178 #define MBOX_OUT_AREA 0x520
0179 #define DATA_IN_AREA 0x000
0180 #define DATA_OUT_AREA 0x200
0181 #define FBCTRL_AREA 0x640
0182
0183 #define EVENT_CAUSE_DC 0x01
0184 #define EVENT_CAUSE_FBOF 0x02
0185 #define EVENT_CAUSE_FBON 0x04
0186
0187 #define IND_AB_UPDATED 0x08
0188 #define IND_AX_MIN 0x80
0189 #define IND_AX_MOUT 0x40
0190 #define IND_AX_IN 0x04
0191 #define IND_AX_OUT 0x02
0192 #define IND_AX_FBCTRL 0x01
0193 #define IND_AP_LOCK 0x08
0194 #define IND_AP_ACTION 0x10
0195 #define IND_AX_EVNT 0x20
0196 #define IND_AP_ABITS (IND_AX_IN | IND_AX_OUT | \
0197 IND_AX_FBCTRL | \
0198 IND_AP_ACTION | IND_AP_LOCK)
0199
0200 #define INFO_TYPE_FB 0x0002
0201 #define INFO_TYPE_APP 0x0001
0202 #define INFO_COMMAND 0x4000
0203
0204 #define OP_MODE_FBFC 0x0002
0205 #define OP_MODE_FBS 0x0004
0206 #define OP_MODE_CD 0x0200
0207
0208 #define CMD_START_INIT 0x0001
0209 #define CMD_ANYBUS_INIT 0x0002
0210 #define CMD_END_INIT 0x0003
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220 struct anybus_mbox_hdr {
0221 __be16 id;
0222 __be16 info;
0223 __be16 cmd_num;
0224 __be16 data_size;
0225 __be16 frame_count;
0226 __be16 frame_num;
0227 __be16 offset_high;
0228 __be16 offset_low;
0229 __be16 extended[8];
0230 };
0231
0232 struct msg_anybus_init {
0233 __be16 input_io_len;
0234 __be16 input_dpram_len;
0235 __be16 input_total_len;
0236 __be16 output_io_len;
0237 __be16 output_dpram_len;
0238 __be16 output_total_len;
0239 __be16 op_mode;
0240 __be16 notif_config;
0241 __be16 wd_val;
0242 };
0243
0244
0245
0246 struct ab_task;
0247 typedef int (*ab_task_fn_t)(struct anybuss_host *cd,
0248 struct ab_task *t);
0249 typedef void (*ab_done_fn_t)(struct anybuss_host *cd);
0250
0251 struct area_priv {
0252 bool is_write;
0253 u16 flags;
0254 u16 addr;
0255 size_t count;
0256 u8 buf[MAX_DATA_AREA_SZ];
0257 };
0258
0259 struct mbox_priv {
0260 struct anybus_mbox_hdr hdr;
0261 size_t msg_out_sz;
0262 size_t msg_in_sz;
0263 u8 msg[MAX_MBOX_MSG_SZ];
0264 };
0265
0266 struct ab_task {
0267 struct kmem_cache *cache;
0268 struct kref refcount;
0269 ab_task_fn_t task_fn;
0270 ab_done_fn_t done_fn;
0271 int result;
0272 struct completion done;
0273 unsigned long start_jiffies;
0274 union {
0275 struct area_priv area_pd;
0276 struct mbox_priv mbox_pd;
0277 };
0278 };
0279
0280 static struct ab_task *ab_task_create_get(struct kmem_cache *cache,
0281 ab_task_fn_t task_fn)
0282 {
0283 struct ab_task *t;
0284
0285 t = kmem_cache_alloc(cache, GFP_KERNEL);
0286 if (!t)
0287 return NULL;
0288 t->cache = cache;
0289 kref_init(&t->refcount);
0290 t->task_fn = task_fn;
0291 t->done_fn = NULL;
0292 t->result = 0;
0293 init_completion(&t->done);
0294 return t;
0295 }
0296
0297 static void __ab_task_destroy(struct kref *refcount)
0298 {
0299 struct ab_task *t = container_of(refcount, struct ab_task, refcount);
0300 struct kmem_cache *cache = t->cache;
0301
0302 kmem_cache_free(cache, t);
0303 }
0304
0305 static void ab_task_put(struct ab_task *t)
0306 {
0307 kref_put(&t->refcount, __ab_task_destroy);
0308 }
0309
0310 static struct ab_task *__ab_task_get(struct ab_task *t)
0311 {
0312 kref_get(&t->refcount);
0313 return t;
0314 }
0315
0316 static void __ab_task_finish(struct ab_task *t, struct anybuss_host *cd)
0317 {
0318 if (t->done_fn)
0319 t->done_fn(cd);
0320 complete(&t->done);
0321 }
0322
0323 static void
0324 ab_task_dequeue_finish_put(struct kfifo *q, struct anybuss_host *cd)
0325 {
0326 int ret;
0327 struct ab_task *t;
0328
0329 ret = kfifo_out(q, &t, sizeof(t));
0330 WARN_ON(!ret);
0331 __ab_task_finish(t, cd);
0332 ab_task_put(t);
0333 }
0334
0335 static int
0336 ab_task_enqueue(struct ab_task *t, struct kfifo *q, spinlock_t *slock,
0337 wait_queue_head_t *wq)
0338 {
0339 int ret;
0340
0341 t->start_jiffies = jiffies;
0342 __ab_task_get(t);
0343 ret = kfifo_in_spinlocked(q, &t, sizeof(t), slock);
0344 if (!ret) {
0345 ab_task_put(t);
0346 return -ENOMEM;
0347 }
0348 wake_up(wq);
0349 return 0;
0350 }
0351
0352 static int
0353 ab_task_enqueue_wait(struct ab_task *t, struct kfifo *q, spinlock_t *slock,
0354 wait_queue_head_t *wq)
0355 {
0356 int ret;
0357
0358 ret = ab_task_enqueue(t, q, slock, wq);
0359 if (ret)
0360 return ret;
0361 ret = wait_for_completion_interruptible(&t->done);
0362 if (ret)
0363 return ret;
0364 return t->result;
0365 }
0366
0367
0368
0369 struct anybuss_host {
0370 struct device *dev;
0371 struct anybuss_client *client;
0372 void (*reset)(struct device *dev, bool assert);
0373 struct regmap *regmap;
0374 int irq;
0375 int host_idx;
0376 struct task_struct *qthread;
0377 wait_queue_head_t wq;
0378 struct completion card_boot;
0379 atomic_t ind_ab;
0380 spinlock_t qlock;
0381 struct kmem_cache *qcache;
0382 struct kfifo qs[3];
0383 struct kfifo *powerq;
0384 struct kfifo *mboxq;
0385 struct kfifo *areaq;
0386 bool power_on;
0387 bool softint_pending;
0388 };
0389
0390 static void reset_assert(struct anybuss_host *cd)
0391 {
0392 cd->reset(cd->dev, true);
0393 }
0394
0395 static void reset_deassert(struct anybuss_host *cd)
0396 {
0397 cd->reset(cd->dev, false);
0398 }
0399
0400 static int test_dpram(struct regmap *regmap)
0401 {
0402 int i;
0403 unsigned int val;
0404
0405 for (i = 0; i < DPRAM_SIZE; i++)
0406 regmap_write(regmap, i, (u8)i);
0407 for (i = 0; i < DPRAM_SIZE; i++) {
0408 regmap_read(regmap, i, &val);
0409 if ((u8)val != (u8)i)
0410 return -EIO;
0411 }
0412 return 0;
0413 }
0414
0415 static int read_ind_ab(struct regmap *regmap)
0416 {
0417 unsigned long timeout = jiffies + HZ / 2;
0418 unsigned int a, b, i = 0;
0419
0420 while (time_before_eq(jiffies, timeout)) {
0421 regmap_read(regmap, REG_IND_AB, &a);
0422 regmap_read(regmap, REG_IND_AB, &b);
0423 if (likely(a == b))
0424 return (int)a;
0425 if (i < 10) {
0426 cpu_relax();
0427 i++;
0428 } else {
0429 usleep_range(500, 1000);
0430 }
0431 }
0432 WARN(1, "IND_AB register not stable");
0433 return -ETIMEDOUT;
0434 }
0435
0436 static int write_ind_ap(struct regmap *regmap, unsigned int ind_ap)
0437 {
0438 unsigned long timeout = jiffies + HZ / 2;
0439 unsigned int v, i = 0;
0440
0441 while (time_before_eq(jiffies, timeout)) {
0442 regmap_write(regmap, REG_IND_AP, ind_ap);
0443 regmap_read(regmap, REG_IND_AP, &v);
0444 if (likely(ind_ap == v))
0445 return 0;
0446 if (i < 10) {
0447 cpu_relax();
0448 i++;
0449 } else {
0450 usleep_range(500, 1000);
0451 }
0452 }
0453 WARN(1, "IND_AP register not stable");
0454 return -ETIMEDOUT;
0455 }
0456
0457 static irqreturn_t irq_handler(int irq, void *data)
0458 {
0459 struct anybuss_host *cd = data;
0460 int ind_ab;
0461
0462
0463
0464
0465
0466
0467
0468
0469
0470 ind_ab = read_ind_ab(cd->regmap);
0471 if (ind_ab < 0)
0472 return IRQ_NONE;
0473 atomic_set(&cd->ind_ab, ind_ab);
0474 complete(&cd->card_boot);
0475 wake_up(&cd->wq);
0476 return IRQ_HANDLED;
0477 }
0478
0479
0480
0481 static int task_fn_power_off(struct anybuss_host *cd,
0482 struct ab_task *t)
0483 {
0484 struct anybuss_client *client = cd->client;
0485
0486 if (!cd->power_on)
0487 return 0;
0488 disable_irq(cd->irq);
0489 reset_assert(cd);
0490 atomic_set(&cd->ind_ab, IND_AB_UPDATED);
0491 if (client->on_online_changed)
0492 client->on_online_changed(client, false);
0493 cd->power_on = false;
0494 return 0;
0495 }
0496
0497 static int task_fn_power_on_2(struct anybuss_host *cd,
0498 struct ab_task *t)
0499 {
0500 if (completion_done(&cd->card_boot)) {
0501 cd->power_on = true;
0502 return 0;
0503 }
0504 if (time_after(jiffies, t->start_jiffies + TIMEOUT)) {
0505 disable_irq(cd->irq);
0506 reset_assert(cd);
0507 dev_err(cd->dev, "power on timed out");
0508 return -ETIMEDOUT;
0509 }
0510 return -EINPROGRESS;
0511 }
0512
0513 static int task_fn_power_on(struct anybuss_host *cd,
0514 struct ab_task *t)
0515 {
0516 unsigned int dummy;
0517
0518 if (cd->power_on)
0519 return 0;
0520
0521
0522
0523
0524 regmap_read(cd->regmap, REG_IND_AB, &dummy);
0525 reinit_completion(&cd->card_boot);
0526 enable_irq(cd->irq);
0527 reset_deassert(cd);
0528 t->task_fn = task_fn_power_on_2;
0529 return -EINPROGRESS;
0530 }
0531
0532 int anybuss_set_power(struct anybuss_client *client, bool power_on)
0533 {
0534 struct anybuss_host *cd = client->host;
0535 struct ab_task *t;
0536 int err;
0537
0538 t = ab_task_create_get(cd->qcache, power_on ?
0539 task_fn_power_on : task_fn_power_off);
0540 if (!t)
0541 return -ENOMEM;
0542 err = ab_task_enqueue_wait(t, cd->powerq, &cd->qlock, &cd->wq);
0543 ab_task_put(t);
0544 return err;
0545 }
0546 EXPORT_SYMBOL_GPL(anybuss_set_power);
0547
0548
0549
0550 static int task_fn_area_3(struct anybuss_host *cd, struct ab_task *t)
0551 {
0552 struct area_priv *pd = &t->area_pd;
0553
0554 if (!cd->power_on)
0555 return -EIO;
0556 if (atomic_read(&cd->ind_ab) & pd->flags) {
0557
0558 if (time_after(jiffies, t->start_jiffies + TIMEOUT))
0559 return -ETIMEDOUT;
0560 return -EINPROGRESS;
0561 }
0562 return 0;
0563 }
0564
0565 static int task_fn_area_2(struct anybuss_host *cd, struct ab_task *t)
0566 {
0567 struct area_priv *pd = &t->area_pd;
0568 unsigned int ind_ap;
0569 int ret;
0570
0571 if (!cd->power_on)
0572 return -EIO;
0573 regmap_read(cd->regmap, REG_IND_AP, &ind_ap);
0574 if (!(atomic_read(&cd->ind_ab) & pd->flags)) {
0575
0576 if (time_after(jiffies, t->start_jiffies + TIMEOUT)) {
0577 dev_warn(cd->dev, "timeout waiting for area");
0578 dump_stack();
0579 return -ETIMEDOUT;
0580 }
0581 return -EINPROGRESS;
0582 }
0583
0584 if (pd->is_write)
0585 regmap_bulk_write(cd->regmap, pd->addr, pd->buf,
0586 pd->count);
0587 else
0588 regmap_bulk_read(cd->regmap, pd->addr, pd->buf,
0589 pd->count);
0590
0591 ind_ap &= ~IND_AP_ABITS;
0592 ind_ap |= pd->flags;
0593 ret = write_ind_ap(cd->regmap, ind_ap);
0594 if (ret)
0595 return ret;
0596 t->task_fn = task_fn_area_3;
0597 return -EINPROGRESS;
0598 }
0599
0600 static int task_fn_area(struct anybuss_host *cd, struct ab_task *t)
0601 {
0602 struct area_priv *pd = &t->area_pd;
0603 unsigned int ind_ap;
0604 int ret;
0605
0606 if (!cd->power_on)
0607 return -EIO;
0608 regmap_read(cd->regmap, REG_IND_AP, &ind_ap);
0609
0610 ind_ap &= ~IND_AP_ABITS;
0611 ind_ap |= pd->flags | IND_AP_ACTION | IND_AP_LOCK;
0612 ret = write_ind_ap(cd->regmap, ind_ap);
0613 if (ret)
0614 return ret;
0615 t->task_fn = task_fn_area_2;
0616 return -EINPROGRESS;
0617 }
0618
0619 static struct ab_task *
0620 create_area_reader(struct kmem_cache *qcache, u16 flags, u16 addr,
0621 size_t count)
0622 {
0623 struct ab_task *t;
0624 struct area_priv *ap;
0625
0626 t = ab_task_create_get(qcache, task_fn_area);
0627 if (!t)
0628 return NULL;
0629 ap = &t->area_pd;
0630 ap->flags = flags;
0631 ap->addr = addr;
0632 ap->is_write = false;
0633 ap->count = count;
0634 return t;
0635 }
0636
0637 static struct ab_task *
0638 create_area_writer(struct kmem_cache *qcache, u16 flags, u16 addr,
0639 const void *buf, size_t count)
0640 {
0641 struct ab_task *t;
0642 struct area_priv *ap;
0643
0644 t = ab_task_create_get(qcache, task_fn_area);
0645 if (!t)
0646 return NULL;
0647 ap = &t->area_pd;
0648 ap->flags = flags;
0649 ap->addr = addr;
0650 ap->is_write = true;
0651 ap->count = count;
0652 memcpy(ap->buf, buf, count);
0653 return t;
0654 }
0655
0656 static struct ab_task *
0657 create_area_user_writer(struct kmem_cache *qcache, u16 flags, u16 addr,
0658 const void __user *buf, size_t count)
0659 {
0660 struct ab_task *t;
0661 struct area_priv *ap;
0662
0663 t = ab_task_create_get(qcache, task_fn_area);
0664 if (!t)
0665 return ERR_PTR(-ENOMEM);
0666 ap = &t->area_pd;
0667 ap->flags = flags;
0668 ap->addr = addr;
0669 ap->is_write = true;
0670 ap->count = count;
0671 if (copy_from_user(ap->buf, buf, count)) {
0672 ab_task_put(t);
0673 return ERR_PTR(-EFAULT);
0674 }
0675 return t;
0676 }
0677
0678 static bool area_range_ok(u16 addr, size_t count, u16 area_start,
0679 size_t area_sz)
0680 {
0681 u16 area_end_ex = area_start + area_sz;
0682 u16 addr_end_ex;
0683
0684 if (addr < area_start)
0685 return false;
0686 if (addr >= area_end_ex)
0687 return false;
0688 addr_end_ex = addr + count;
0689 if (addr_end_ex > area_end_ex)
0690 return false;
0691 return true;
0692 }
0693
0694
0695
0696 static int task_fn_mbox_2(struct anybuss_host *cd, struct ab_task *t)
0697 {
0698 struct mbox_priv *pd = &t->mbox_pd;
0699 unsigned int ind_ap;
0700
0701 if (!cd->power_on)
0702 return -EIO;
0703 regmap_read(cd->regmap, REG_IND_AP, &ind_ap);
0704 if (((atomic_read(&cd->ind_ab) ^ ind_ap) & IND_AX_MOUT) == 0) {
0705
0706 if (time_after(jiffies, t->start_jiffies + TIMEOUT))
0707 return -ETIMEDOUT;
0708 return -EINPROGRESS;
0709 }
0710
0711 regmap_bulk_read(cd->regmap, MBOX_OUT_AREA, &pd->hdr,
0712 sizeof(pd->hdr));
0713 regmap_bulk_read(cd->regmap, MBOX_OUT_AREA + sizeof(pd->hdr),
0714 pd->msg, pd->msg_in_sz);
0715
0716 ind_ap ^= IND_AX_MOUT;
0717 return write_ind_ap(cd->regmap, ind_ap);
0718 }
0719
0720 static int task_fn_mbox(struct anybuss_host *cd, struct ab_task *t)
0721 {
0722 struct mbox_priv *pd = &t->mbox_pd;
0723 unsigned int ind_ap;
0724 int ret;
0725
0726 if (!cd->power_on)
0727 return -EIO;
0728 regmap_read(cd->regmap, REG_IND_AP, &ind_ap);
0729 if ((atomic_read(&cd->ind_ab) ^ ind_ap) & IND_AX_MIN) {
0730
0731 if (time_after(jiffies, t->start_jiffies + TIMEOUT))
0732 return -ETIMEDOUT;
0733 return -EINPROGRESS;
0734 }
0735
0736 regmap_bulk_write(cd->regmap, MBOX_IN_AREA, &pd->hdr,
0737 sizeof(pd->hdr));
0738 regmap_bulk_write(cd->regmap, MBOX_IN_AREA + sizeof(pd->hdr),
0739 pd->msg, pd->msg_out_sz);
0740
0741 ind_ap ^= IND_AX_MIN;
0742 ret = write_ind_ap(cd->regmap, ind_ap);
0743 if (ret)
0744 return ret;
0745 t->start_jiffies = jiffies;
0746 t->task_fn = task_fn_mbox_2;
0747 return -EINPROGRESS;
0748 }
0749
0750 static void log_invalid_other(struct device *dev,
0751 struct anybus_mbox_hdr *hdr)
0752 {
0753 size_t ext_offs = ARRAY_SIZE(hdr->extended) - 1;
0754 u16 code = be16_to_cpu(hdr->extended[ext_offs]);
0755
0756 dev_err(dev, " Invalid other: [0x%02X]", code);
0757 }
0758
0759 static const char * const EMSGS[] = {
0760 "Invalid Message ID",
0761 "Invalid Message Type",
0762 "Invalid Command",
0763 "Invalid Data Size",
0764 "Message Header Malformed (offset 008h)",
0765 "Message Header Malformed (offset 00Ah)",
0766 "Message Header Malformed (offset 00Ch - 00Dh)",
0767 "Invalid Address",
0768 "Invalid Response",
0769 "Flash Config Error",
0770 };
0771
0772 static int mbox_cmd_err(struct device *dev, struct mbox_priv *mpriv)
0773 {
0774 int i;
0775 u8 ecode;
0776 struct anybus_mbox_hdr *hdr = &mpriv->hdr;
0777 u16 info = be16_to_cpu(hdr->info);
0778 u8 *phdr = (u8 *)hdr;
0779 u8 *pmsg = mpriv->msg;
0780
0781 if (!(info & 0x8000))
0782 return 0;
0783 ecode = (info >> 8) & 0x0F;
0784 dev_err(dev, "mailbox command failed:");
0785 if (ecode == 0x0F)
0786 log_invalid_other(dev, hdr);
0787 else if (ecode < ARRAY_SIZE(EMSGS))
0788 dev_err(dev, " Error code: %s (0x%02X)",
0789 EMSGS[ecode], ecode);
0790 else
0791 dev_err(dev, " Error code: 0x%02X\n", ecode);
0792 dev_err(dev, "Failed command:");
0793 dev_err(dev, "Message Header:");
0794 for (i = 0; i < sizeof(mpriv->hdr); i += 2)
0795 dev_err(dev, "%02X%02X", phdr[i], phdr[i + 1]);
0796 dev_err(dev, "Message Data:");
0797 for (i = 0; i < mpriv->msg_in_sz; i += 2)
0798 dev_err(dev, "%02X%02X", pmsg[i], pmsg[i + 1]);
0799 dev_err(dev, "Stack dump:");
0800 dump_stack();
0801 return -EIO;
0802 }
0803
0804 static int _anybus_mbox_cmd(struct anybuss_host *cd,
0805 u16 cmd_num, bool is_fb_cmd,
0806 const void *msg_out, size_t msg_out_sz,
0807 void *msg_in, size_t msg_in_sz,
0808 const void *ext, size_t ext_sz)
0809 {
0810 struct ab_task *t;
0811 struct mbox_priv *pd;
0812 struct anybus_mbox_hdr *h;
0813 size_t msg_sz = max(msg_in_sz, msg_out_sz);
0814 u16 info;
0815 int err;
0816
0817 if (msg_sz > MAX_MBOX_MSG_SZ)
0818 return -EINVAL;
0819 if (ext && ext_sz > sizeof(h->extended))
0820 return -EINVAL;
0821 t = ab_task_create_get(cd->qcache, task_fn_mbox);
0822 if (!t)
0823 return -ENOMEM;
0824 pd = &t->mbox_pd;
0825 h = &pd->hdr;
0826 info = is_fb_cmd ? INFO_TYPE_FB : INFO_TYPE_APP;
0827
0828
0829
0830
0831 memset(h, 0, sizeof(*h));
0832 h->info = cpu_to_be16(info | INFO_COMMAND);
0833 h->cmd_num = cpu_to_be16(cmd_num);
0834 h->data_size = cpu_to_be16(msg_out_sz);
0835 h->frame_count = cpu_to_be16(1);
0836 h->frame_num = cpu_to_be16(1);
0837 h->offset_high = cpu_to_be16(0);
0838 h->offset_low = cpu_to_be16(0);
0839 if (ext)
0840 memcpy(h->extended, ext, ext_sz);
0841 memcpy(pd->msg, msg_out, msg_out_sz);
0842 pd->msg_out_sz = msg_out_sz;
0843 pd->msg_in_sz = msg_in_sz;
0844 err = ab_task_enqueue_wait(t, cd->powerq, &cd->qlock, &cd->wq);
0845 if (err)
0846 goto out;
0847
0848
0849
0850
0851 err = mbox_cmd_err(cd->dev, pd);
0852 if (err)
0853 goto out;
0854 memcpy(msg_in, pd->msg, msg_in_sz);
0855 out:
0856 ab_task_put(t);
0857 return err;
0858 }
0859
0860
0861
0862 static void process_q(struct anybuss_host *cd, struct kfifo *q)
0863 {
0864 struct ab_task *t;
0865 int ret;
0866
0867 ret = kfifo_out_peek(q, &t, sizeof(t));
0868 if (!ret)
0869 return;
0870 t->result = t->task_fn(cd, t);
0871 if (t->result != -EINPROGRESS)
0872 ab_task_dequeue_finish_put(q, cd);
0873 }
0874
0875 static bool qs_have_work(struct kfifo *qs, size_t num)
0876 {
0877 size_t i;
0878 struct ab_task *t;
0879 int ret;
0880
0881 for (i = 0; i < num; i++, qs++) {
0882 ret = kfifo_out_peek(qs, &t, sizeof(t));
0883 if (ret && (t->result != -EINPROGRESS))
0884 return true;
0885 }
0886 return false;
0887 }
0888
0889 static void process_qs(struct anybuss_host *cd)
0890 {
0891 size_t i;
0892 struct kfifo *qs = cd->qs;
0893 size_t nqs = ARRAY_SIZE(cd->qs);
0894
0895 for (i = 0; i < nqs; i++, qs++)
0896 process_q(cd, qs);
0897 }
0898
0899 static void softint_ack(struct anybuss_host *cd)
0900 {
0901 unsigned int ind_ap;
0902
0903 cd->softint_pending = false;
0904 if (!cd->power_on)
0905 return;
0906 regmap_read(cd->regmap, REG_IND_AP, &ind_ap);
0907 ind_ap &= ~IND_AX_EVNT;
0908 ind_ap |= atomic_read(&cd->ind_ab) & IND_AX_EVNT;
0909 write_ind_ap(cd->regmap, ind_ap);
0910 }
0911
0912 static void process_softint(struct anybuss_host *cd)
0913 {
0914 struct anybuss_client *client = cd->client;
0915 static const u8 zero;
0916 int ret;
0917 unsigned int ind_ap, ev;
0918 struct ab_task *t;
0919
0920 if (!cd->power_on)
0921 return;
0922 if (cd->softint_pending)
0923 return;
0924 regmap_read(cd->regmap, REG_IND_AP, &ind_ap);
0925 if (!((atomic_read(&cd->ind_ab) ^ ind_ap) & IND_AX_EVNT))
0926 return;
0927
0928 regmap_read(cd->regmap, REG_EVENT_CAUSE, &ev);
0929 if (ev & EVENT_CAUSE_FBON) {
0930 if (client->on_online_changed)
0931 client->on_online_changed(client, true);
0932 dev_dbg(cd->dev, "Fieldbus ON");
0933 }
0934 if (ev & EVENT_CAUSE_FBOF) {
0935 if (client->on_online_changed)
0936 client->on_online_changed(client, false);
0937 dev_dbg(cd->dev, "Fieldbus OFF");
0938 }
0939 if (ev & EVENT_CAUSE_DC) {
0940 if (client->on_area_updated)
0941 client->on_area_updated(client);
0942 dev_dbg(cd->dev, "Fieldbus data changed");
0943 }
0944
0945
0946
0947
0948
0949 t = create_area_writer(cd->qcache, IND_AX_FBCTRL,
0950 REG_EVENT_CAUSE, &zero, sizeof(zero));
0951 if (!t) {
0952 ret = -ENOMEM;
0953 goto out;
0954 }
0955 t->done_fn = softint_ack;
0956 ret = ab_task_enqueue(t, cd->powerq, &cd->qlock, &cd->wq);
0957 ab_task_put(t);
0958 cd->softint_pending = true;
0959 out:
0960 WARN_ON(ret);
0961 if (ret)
0962 softint_ack(cd);
0963 }
0964
0965 static int qthread_fn(void *data)
0966 {
0967 struct anybuss_host *cd = data;
0968 struct kfifo *qs = cd->qs;
0969 size_t nqs = ARRAY_SIZE(cd->qs);
0970 unsigned int ind_ab;
0971
0972
0973
0974
0975
0976
0977
0978
0979
0980
0981
0982
0983 while (!kthread_should_stop()) {
0984
0985
0986
0987
0988 ind_ab = atomic_read(&cd->ind_ab);
0989 process_qs(cd);
0990 process_softint(cd);
0991 wait_event_timeout(cd->wq,
0992 (atomic_read(&cd->ind_ab) != ind_ab) ||
0993 qs_have_work(qs, nqs) ||
0994 kthread_should_stop(),
0995 HZ);
0996
0997
0998
0999
1000 }
1001
1002 return 0;
1003 }
1004
1005
1006
1007 int anybuss_start_init(struct anybuss_client *client,
1008 const struct anybuss_memcfg *cfg)
1009 {
1010 int ret;
1011 u16 op_mode;
1012 struct anybuss_host *cd = client->host;
1013 struct msg_anybus_init msg = {
1014 .input_io_len = cpu_to_be16(cfg->input_io),
1015 .input_dpram_len = cpu_to_be16(cfg->input_dpram),
1016 .input_total_len = cpu_to_be16(cfg->input_total),
1017 .output_io_len = cpu_to_be16(cfg->output_io),
1018 .output_dpram_len = cpu_to_be16(cfg->output_dpram),
1019 .output_total_len = cpu_to_be16(cfg->output_total),
1020 .notif_config = cpu_to_be16(0x000F),
1021 .wd_val = cpu_to_be16(0),
1022 };
1023
1024 switch (cfg->offl_mode) {
1025 case FIELDBUS_DEV_OFFL_MODE_CLEAR:
1026 op_mode = 0;
1027 break;
1028 case FIELDBUS_DEV_OFFL_MODE_FREEZE:
1029 op_mode = OP_MODE_FBFC;
1030 break;
1031 case FIELDBUS_DEV_OFFL_MODE_SET:
1032 op_mode = OP_MODE_FBS;
1033 break;
1034 default:
1035 return -EINVAL;
1036 }
1037 msg.op_mode = cpu_to_be16(op_mode | OP_MODE_CD);
1038 ret = _anybus_mbox_cmd(cd, CMD_START_INIT, false, NULL, 0,
1039 NULL, 0, NULL, 0);
1040 if (ret)
1041 return ret;
1042 return _anybus_mbox_cmd(cd, CMD_ANYBUS_INIT, false,
1043 &msg, sizeof(msg), NULL, 0, NULL, 0);
1044 }
1045 EXPORT_SYMBOL_GPL(anybuss_start_init);
1046
1047 int anybuss_finish_init(struct anybuss_client *client)
1048 {
1049 struct anybuss_host *cd = client->host;
1050
1051 return _anybus_mbox_cmd(cd, CMD_END_INIT, false, NULL, 0,
1052 NULL, 0, NULL, 0);
1053 }
1054 EXPORT_SYMBOL_GPL(anybuss_finish_init);
1055
1056 int anybuss_read_fbctrl(struct anybuss_client *client, u16 addr,
1057 void *buf, size_t count)
1058 {
1059 struct anybuss_host *cd = client->host;
1060 struct ab_task *t;
1061 int ret;
1062
1063 if (count == 0)
1064 return 0;
1065 if (!area_range_ok(addr, count, FBCTRL_AREA,
1066 MAX_FBCTRL_AREA_SZ))
1067 return -EFAULT;
1068 t = create_area_reader(cd->qcache, IND_AX_FBCTRL, addr, count);
1069 if (!t)
1070 return -ENOMEM;
1071 ret = ab_task_enqueue_wait(t, cd->powerq, &cd->qlock, &cd->wq);
1072 if (ret)
1073 goto out;
1074 memcpy(buf, t->area_pd.buf, count);
1075 out:
1076 ab_task_put(t);
1077 return ret;
1078 }
1079 EXPORT_SYMBOL_GPL(anybuss_read_fbctrl);
1080
1081 int anybuss_write_input(struct anybuss_client *client,
1082 const char __user *buf, size_t size,
1083 loff_t *offset)
1084 {
1085 ssize_t len = min_t(loff_t, MAX_DATA_AREA_SZ - *offset, size);
1086 struct anybuss_host *cd = client->host;
1087 struct ab_task *t;
1088 int ret;
1089
1090 if (len <= 0)
1091 return 0;
1092 t = create_area_user_writer(cd->qcache, IND_AX_IN,
1093 DATA_IN_AREA + *offset, buf, len);
1094 if (IS_ERR(t))
1095 return PTR_ERR(t);
1096 ret = ab_task_enqueue_wait(t, cd->powerq, &cd->qlock, &cd->wq);
1097 ab_task_put(t);
1098 if (ret)
1099 return ret;
1100
1101 *offset += len;
1102 return len;
1103 }
1104 EXPORT_SYMBOL_GPL(anybuss_write_input);
1105
1106 int anybuss_read_output(struct anybuss_client *client,
1107 char __user *buf, size_t size,
1108 loff_t *offset)
1109 {
1110 ssize_t len = min_t(loff_t, MAX_DATA_AREA_SZ - *offset, size);
1111 struct anybuss_host *cd = client->host;
1112 struct ab_task *t;
1113 int ret;
1114
1115 if (len <= 0)
1116 return 0;
1117 t = create_area_reader(cd->qcache, IND_AX_OUT,
1118 DATA_OUT_AREA + *offset, len);
1119 if (!t)
1120 return -ENOMEM;
1121 ret = ab_task_enqueue_wait(t, cd->powerq, &cd->qlock, &cd->wq);
1122 if (ret)
1123 goto out;
1124 if (copy_to_user(buf, t->area_pd.buf, len))
1125 ret = -EFAULT;
1126 out:
1127 ab_task_put(t);
1128 if (ret)
1129 return ret;
1130
1131 *offset += len;
1132 return len;
1133 }
1134 EXPORT_SYMBOL_GPL(anybuss_read_output);
1135
1136 int anybuss_send_msg(struct anybuss_client *client, u16 cmd_num,
1137 const void *buf, size_t count)
1138 {
1139 struct anybuss_host *cd = client->host;
1140
1141 return _anybus_mbox_cmd(cd, cmd_num, true, buf, count, NULL, 0,
1142 NULL, 0);
1143 }
1144 EXPORT_SYMBOL_GPL(anybuss_send_msg);
1145
1146 int anybuss_send_ext(struct anybuss_client *client, u16 cmd_num,
1147 const void *buf, size_t count)
1148 {
1149 struct anybuss_host *cd = client->host;
1150
1151 return _anybus_mbox_cmd(cd, cmd_num, true, NULL, 0, NULL, 0,
1152 buf, count);
1153 }
1154 EXPORT_SYMBOL_GPL(anybuss_send_ext);
1155
1156 int anybuss_recv_msg(struct anybuss_client *client, u16 cmd_num,
1157 void *buf, size_t count)
1158 {
1159 struct anybuss_host *cd = client->host;
1160
1161 return _anybus_mbox_cmd(cd, cmd_num, true, NULL, 0, buf, count,
1162 NULL, 0);
1163 }
1164 EXPORT_SYMBOL_GPL(anybuss_recv_msg);
1165
1166
1167
1168 static int anybus_bus_match(struct device *dev,
1169 struct device_driver *drv)
1170 {
1171 struct anybuss_client_driver *adrv =
1172 to_anybuss_client_driver(drv);
1173 struct anybuss_client *adev =
1174 to_anybuss_client(dev);
1175
1176 return adrv->anybus_id == be16_to_cpu(adev->anybus_id);
1177 }
1178
1179 static int anybus_bus_probe(struct device *dev)
1180 {
1181 struct anybuss_client_driver *adrv =
1182 to_anybuss_client_driver(dev->driver);
1183 struct anybuss_client *adev =
1184 to_anybuss_client(dev);
1185
1186 return adrv->probe(adev);
1187 }
1188
1189 static void anybus_bus_remove(struct device *dev)
1190 {
1191 struct anybuss_client_driver *adrv =
1192 to_anybuss_client_driver(dev->driver);
1193
1194 if (adrv->remove)
1195 adrv->remove(to_anybuss_client(dev));
1196 }
1197
1198 static struct bus_type anybus_bus = {
1199 .name = "anybuss",
1200 .match = anybus_bus_match,
1201 .probe = anybus_bus_probe,
1202 .remove = anybus_bus_remove,
1203 };
1204
1205 int anybuss_client_driver_register(struct anybuss_client_driver *drv)
1206 {
1207 if (!drv->probe)
1208 return -ENODEV;
1209
1210 drv->driver.bus = &anybus_bus;
1211 return driver_register(&drv->driver);
1212 }
1213 EXPORT_SYMBOL_GPL(anybuss_client_driver_register);
1214
1215 void anybuss_client_driver_unregister(struct anybuss_client_driver *drv)
1216 {
1217 return driver_unregister(&drv->driver);
1218 }
1219 EXPORT_SYMBOL_GPL(anybuss_client_driver_unregister);
1220
1221 static void client_device_release(struct device *dev)
1222 {
1223 kfree(to_anybuss_client(dev));
1224 }
1225
1226 static int taskq_alloc(struct device *dev, struct kfifo *q)
1227 {
1228 void *buf;
1229 size_t size = 64 * sizeof(struct ab_task *);
1230
1231 buf = devm_kzalloc(dev, size, GFP_KERNEL);
1232 if (!buf)
1233 return -EIO;
1234 return kfifo_init(q, buf, size);
1235 }
1236
1237 static int anybus_of_get_host_idx(struct device_node *np)
1238 {
1239 const __be32 *host_idx;
1240
1241 host_idx = of_get_address(np, 0, NULL, NULL);
1242 if (!host_idx)
1243 return -ENOENT;
1244 return __be32_to_cpu(*host_idx);
1245 }
1246
1247 static struct device_node *
1248 anybus_of_find_child_device(struct device *dev, int host_idx)
1249 {
1250 struct device_node *node;
1251
1252 if (!dev || !dev->of_node)
1253 return NULL;
1254 for_each_child_of_node(dev->of_node, node) {
1255 if (anybus_of_get_host_idx(node) == host_idx)
1256 return node;
1257 }
1258 return NULL;
1259 }
1260
1261 struct anybuss_host * __must_check
1262 anybuss_host_common_probe(struct device *dev,
1263 const struct anybuss_ops *ops)
1264 {
1265 int ret, i;
1266 u8 val[4];
1267 __be16 fieldbus_type;
1268 struct anybuss_host *cd;
1269
1270 cd = devm_kzalloc(dev, sizeof(*cd), GFP_KERNEL);
1271 if (!cd)
1272 return ERR_PTR(-ENOMEM);
1273 cd->dev = dev;
1274 cd->host_idx = ops->host_idx;
1275 init_completion(&cd->card_boot);
1276 init_waitqueue_head(&cd->wq);
1277 for (i = 0; i < ARRAY_SIZE(cd->qs); i++) {
1278 ret = taskq_alloc(dev, &cd->qs[i]);
1279 if (ret)
1280 return ERR_PTR(ret);
1281 }
1282 if (WARN_ON(ARRAY_SIZE(cd->qs) < 3))
1283 return ERR_PTR(-EINVAL);
1284 cd->powerq = &cd->qs[0];
1285 cd->mboxq = &cd->qs[1];
1286 cd->areaq = &cd->qs[2];
1287 cd->reset = ops->reset;
1288 if (!cd->reset)
1289 return ERR_PTR(-EINVAL);
1290 cd->regmap = ops->regmap;
1291 if (!cd->regmap)
1292 return ERR_PTR(-EINVAL);
1293 spin_lock_init(&cd->qlock);
1294 cd->qcache = kmem_cache_create(dev_name(dev),
1295 sizeof(struct ab_task), 0, 0, NULL);
1296 if (!cd->qcache)
1297 return ERR_PTR(-ENOMEM);
1298 cd->irq = ops->irq;
1299 if (cd->irq <= 0) {
1300 ret = -EINVAL;
1301 goto err_qcache;
1302 }
1303
1304
1305
1306
1307 reset_assert(cd);
1308 if (test_dpram(cd->regmap)) {
1309 dev_err(dev, "no Anybus-S card in slot");
1310 ret = -ENODEV;
1311 goto err_qcache;
1312 }
1313 ret = devm_request_threaded_irq(dev, cd->irq, NULL, irq_handler,
1314 IRQF_ONESHOT, dev_name(dev), cd);
1315 if (ret) {
1316 dev_err(dev, "could not request irq");
1317 goto err_qcache;
1318 }
1319
1320
1321
1322
1323
1324
1325
1326
1327 reset_deassert(cd);
1328 if (!wait_for_completion_timeout(&cd->card_boot, TIMEOUT)) {
1329 ret = -ETIMEDOUT;
1330 goto err_reset;
1331 }
1332
1333
1334
1335
1336 dev_info(dev, "Anybus-S card detected");
1337 regmap_bulk_read(cd->regmap, REG_BOOTLOADER_V, val, 2);
1338 dev_info(dev, "Bootloader version: %02X%02X",
1339 val[0], val[1]);
1340 regmap_bulk_read(cd->regmap, REG_API_V, val, 2);
1341 dev_info(dev, "API version: %02X%02X", val[0], val[1]);
1342 regmap_bulk_read(cd->regmap, REG_FIELDBUS_V, val, 2);
1343 dev_info(dev, "Fieldbus version: %02X%02X", val[0], val[1]);
1344 regmap_bulk_read(cd->regmap, REG_SERIAL_NO, val, 4);
1345 dev_info(dev, "Serial number: %02X%02X%02X%02X",
1346 val[0], val[1], val[2], val[3]);
1347 add_device_randomness(&val, 4);
1348 regmap_bulk_read(cd->regmap, REG_FIELDBUS_TYPE, &fieldbus_type,
1349 sizeof(fieldbus_type));
1350 dev_info(dev, "Fieldbus type: %04X", be16_to_cpu(fieldbus_type));
1351 regmap_bulk_read(cd->regmap, REG_MODULE_SW_V, val, 2);
1352 dev_info(dev, "Module SW version: %02X%02X",
1353 val[0], val[1]);
1354
1355 disable_irq(cd->irq);
1356 reset_assert(cd);
1357 atomic_set(&cd->ind_ab, IND_AB_UPDATED);
1358
1359 cd->qthread = kthread_run(qthread_fn, cd, dev_name(dev));
1360 if (IS_ERR(cd->qthread)) {
1361 dev_err(dev, "could not create kthread");
1362 ret = PTR_ERR(cd->qthread);
1363 goto err_reset;
1364 }
1365
1366
1367
1368
1369 cd->client = kzalloc(sizeof(*cd->client), GFP_KERNEL);
1370 if (!cd->client) {
1371 ret = -ENOMEM;
1372 goto err_kthread;
1373 }
1374 cd->client->anybus_id = fieldbus_type;
1375 cd->client->host = cd;
1376 cd->client->dev.bus = &anybus_bus;
1377 cd->client->dev.parent = dev;
1378 cd->client->dev.release = client_device_release;
1379 cd->client->dev.of_node =
1380 anybus_of_find_child_device(dev, cd->host_idx);
1381 dev_set_name(&cd->client->dev, "anybuss.card%d", cd->host_idx);
1382 ret = device_register(&cd->client->dev);
1383 if (ret)
1384 goto err_device;
1385 return cd;
1386 err_device:
1387 put_device(&cd->client->dev);
1388 err_kthread:
1389 kthread_stop(cd->qthread);
1390 err_reset:
1391 reset_assert(cd);
1392 err_qcache:
1393 kmem_cache_destroy(cd->qcache);
1394 return ERR_PTR(ret);
1395 }
1396 EXPORT_SYMBOL_GPL(anybuss_host_common_probe);
1397
1398 void anybuss_host_common_remove(struct anybuss_host *host)
1399 {
1400 struct anybuss_host *cd = host;
1401
1402 device_unregister(&cd->client->dev);
1403 kthread_stop(cd->qthread);
1404 reset_assert(cd);
1405 kmem_cache_destroy(cd->qcache);
1406 }
1407 EXPORT_SYMBOL_GPL(anybuss_host_common_remove);
1408
1409 static void host_release(void *res)
1410 {
1411 anybuss_host_common_remove(res);
1412 }
1413
1414 struct anybuss_host * __must_check
1415 devm_anybuss_host_common_probe(struct device *dev,
1416 const struct anybuss_ops *ops)
1417 {
1418 struct anybuss_host *host;
1419 int ret;
1420
1421 host = anybuss_host_common_probe(dev, ops);
1422 if (IS_ERR(host))
1423 return host;
1424
1425 ret = devm_add_action_or_reset(dev, host_release, host);
1426 if (ret)
1427 return ERR_PTR(ret);
1428
1429 return host;
1430 }
1431 EXPORT_SYMBOL_GPL(devm_anybuss_host_common_probe);
1432
1433 static int __init anybus_init(void)
1434 {
1435 int ret;
1436
1437 ret = bus_register(&anybus_bus);
1438 if (ret)
1439 pr_err("could not register Anybus-S bus: %d\n", ret);
1440 return ret;
1441 }
1442 module_init(anybus_init);
1443
1444 static void __exit anybus_exit(void)
1445 {
1446 bus_unregister(&anybus_bus);
1447 }
1448 module_exit(anybus_exit);
1449
1450 MODULE_DESCRIPTION("HMS Anybus-S Host Driver");
1451 MODULE_AUTHOR("Sven Van Asbroeck <TheSven73@gmail.com>");
1452 MODULE_LICENSE("GPL v2");