1 // SPDX-License-Identifier: GPL-2.0
3 * HMS Anybus-S Host Driver
5 * Copyright (C) 2018 Arcx Inc
9 * Architecture Overview
10 * =====================
11 * This driver (running on the CPU/SoC) and the Anybus-S card communicate
12 * by reading and writing data to/from the Anybus-S Dual-Port RAM (dpram).
13 * This is memory connected to both the SoC and Anybus-S card, which both sides
14 * can access freely and concurrently.
16 * Synchronization happens by means of two registers located in the dpram:
17 * IND_AB: written exclusively by the Anybus card; and
18 * IND_AP: written exclusively by this driver.
20 * Communication happens using one of the following mechanisms:
21 * 1. reserve, read/write, release dpram memory areas:
22 * using an IND_AB/IND_AP protocol, the driver is able to reserve certain
23 * memory areas. no dpram memory can be read or written except if reserved.
24 * (with a few limited exceptions)
25 * 2. send and receive data structures via a shared mailbox:
26 * using an IND_AB/IND_AP protocol, the driver and Anybus card are able to
27 * exchange commands and responses using a shared mailbox.
28 * 3. receive software interrupts:
29 * using an IND_AB/IND_AP protocol, the Anybus card is able to notify the
30 * driver of certain events such as: bus online/offline, data available.
31 * note that software interrupt event bits are located in a memory area
32 * which must be reserved before it can be accessed.
34 * The manual[1] is silent on whether these mechanisms can happen concurrently,
35 * or how they should be synchronized. However, section 13 (Driver Example)
36 * provides the following suggestion for developing a driver:
37 * a) an interrupt handler which updates global variables;
38 * b) a continuously-running task handling area requests (1 above)
39 * c) a continuously-running task handling mailbox requests (2 above)
40 * The example conspicuously leaves out software interrupts (3 above), which
41 * is the thorniest issue to get right (see below).
43 * The naive, straightforward way to implement this would be:
44 * - create an isr which updates shared variables;
45 * - create a work_struct which handles software interrupts on a queue;
46 * - create a function which does reserve/update/unlock in a loop;
47 * - create a function which does mailbox send/receive in a loop;
48 * - call the above functions from the driver's read/write/ioctl;
49 * - synchronize using mutexes/spinlocks:
50 * + only one area request at a time
51 * + only one mailbox request at a time
52 * + protect AB_IND, AB_IND against data hazards (e.g. read-after-write)
54 * Unfortunately, the presence of the software interrupt causes subtle yet
55 * considerable synchronization issues; especially problematic is the
56 * requirement to reserve/release the area which contains the status bits.
58 * The driver architecture presented here sidesteps these synchronization issues
59 * by accessing the dpram from a single kernel thread only. User-space throws
60 * "tasks" (i.e. 1, 2 above) into a task queue, waits for their completion,
61 * and the kernel thread runs them to completion.
63 * Each task has a task_function, which is called/run by the queue thread.
64 * That function communicates with the Anybus card, and returns either
65 * 0 (OK), a negative error code (error), or -EINPROGRESS (waiting).
66 * On OK or error, the queue thread completes and dequeues the task,
67 * which also releases the user space thread which may still be waiting for it.
68 * On -EINPROGRESS (waiting), the queue thread will leave the task on the queue,
69 * and revisit (call again) whenever an interrupt event comes in.
71 * Each task has a state machine, which is run by calling its task_function.
72 * It ensures that the task will go through its various stages over time,
73 * returning -EINPROGRESS if it wants to wait for an event to happen.
75 * Note that according to the manual's driver example, the following operations
76 * may run independent of each other:
77 * - area reserve/read/write/release (point 1 above)
78 * - mailbox operations (point 2 above)
79 * - switching power on/off
81 * To allow them to run independently, each operation class gets its own queue.
83 * Userspace processes A, B, C, D post tasks to the appropriate queue,
84 * and wait for task completion:
89 * |<----- ========================================
91 * | v v v-------<-------+
92 * | +--------------------------------------+ |
93 * | | power q | mbox q | area q | |
94 * | |------------|------------|------------| |
95 * | | task | task | task | |
96 * | | task | task | task | |
97 * | | task wait | task wait | task wait | |
98 * | +--------------------------------------+ |
101 * | +--------------------------------------+ |
102 * | | queue thread | |
103 * | |--------------------------------------| |
104 * | | single-threaded: | |
106 * v | for each queue: | |
107 * | | run task state machine | |
108 * | | if task waiting: | |
109 * | | leave on queue | |
110 * | | if task done: | |
111 * | | complete task, remove from q | |
112 * | | if software irq event bits set: | |
113 * | | notify userspace | |
114 * | | post clear event bits task------>|>-------+
115 * | | wait for IND_AB changed event OR |
116 * | | task added event OR |
119 * | +--------------------------------------+
121 * | +--------------------------------------+
124 * +-------->------- |
126 * +--------------------------------------+
127 * | interrupt service routine |
128 * |--------------------------------------|
129 * | wake up queue thread on IND_AB change|
130 * +--------------------------------------+
132 * Note that the Anybus interrupt is dual-purpose:
133 * - after a reset, triggered when the card becomes ready;
134 * - during normal operation, triggered when AB_IND changes.
135 * This is why the interrupt service routine doesn't just wake up the
136 * queue thread, but also completes the card_boot completion.
138 * [1] https://www.anybus.com/docs/librariesprovider7/default-document-library/
139 * manuals-design-guides/hms-hmsi-27-275.pdf
142 #include <linux/kernel.h>
143 #include <linux/module.h>
144 #include <linux/init.h>
145 #include <linux/slab.h>
146 #include <linux/interrupt.h>
147 #include <linux/atomic.h>
148 #include <linux/kthread.h>
149 #include <linux/kfifo.h>
150 #include <linux/spinlock.h>
151 #include <linux/uaccess.h>
152 #include <linux/regmap.h>
153 #include <linux/of.h>
154 #include <linux/random.h>
155 #include <linux/kref.h>
156 #include <linux/of_address.h>
158 /* move to <linux/anybuss-*.h> when taking this out of staging */
159 #include "anybuss-client.h"
160 #include "anybuss-controller.h"
162 #define DPRAM_SIZE 0x800
163 #define MAX_MBOX_MSG_SZ 0x0FF
164 #define TIMEOUT (HZ * 2)
165 #define MAX_DATA_AREA_SZ 0x200
166 #define MAX_FBCTRL_AREA_SZ 0x1BE
168 #define REG_BOOTLOADER_V 0x7C0
169 #define REG_API_V 0x7C2
170 #define REG_FIELDBUS_V 0x7C4
171 #define REG_SERIAL_NO 0x7C6
172 #define REG_FIELDBUS_TYPE 0x7CC
173 #define REG_MODULE_SW_V 0x7CE
174 #define REG_IND_AB 0x7FF
175 #define REG_IND_AP 0x7FE
176 #define REG_EVENT_CAUSE 0x7ED
177 #define MBOX_IN_AREA 0x400
178 #define MBOX_OUT_AREA 0x520
179 #define DATA_IN_AREA 0x000
180 #define DATA_OUT_AREA 0x200
181 #define FBCTRL_AREA 0x640
183 #define EVENT_CAUSE_DC 0x01
184 #define EVENT_CAUSE_FBOF 0x02
185 #define EVENT_CAUSE_FBON 0x04
187 #define IND_AB_UPDATED 0x08
188 #define IND_AX_MIN 0x80
189 #define IND_AX_MOUT 0x40
190 #define IND_AX_IN 0x04
191 #define IND_AX_OUT 0x02
192 #define IND_AX_FBCTRL 0x01
193 #define IND_AP_LOCK 0x08
194 #define IND_AP_ACTION 0x10
195 #define IND_AX_EVNT 0x20
196 #define IND_AP_ABITS (IND_AX_IN | IND_AX_OUT | \
198 IND_AP_ACTION | IND_AP_LOCK)
200 #define INFO_TYPE_FB 0x0002
201 #define INFO_TYPE_APP 0x0001
202 #define INFO_COMMAND 0x4000
204 #define OP_MODE_FBFC 0x0002
205 #define OP_MODE_FBS 0x0004
206 #define OP_MODE_CD 0x0200
208 #define CMD_START_INIT 0x0001
209 #define CMD_ANYBUS_INIT 0x0002
210 #define CMD_END_INIT 0x0003
213 * ---------------------------------------------------------------
214 * Anybus mailbox messages - definitions
215 * ---------------------------------------------------------------
216 * note that we're depending on the layout of these structures being
217 * exactly as advertised.
220 struct anybus_mbox_hdr {
232 struct msg_anybus_init {
234 __be16 input_dpram_len;
235 __be16 input_total_len;
236 __be16 output_io_len;
237 __be16 output_dpram_len;
238 __be16 output_total_len;
244 /* ------------- ref counted tasks ------------- */
247 typedef int (*ab_task_fn_t)(struct anybuss_host *cd,
249 typedef void (*ab_done_fn_t)(struct anybuss_host *cd);
256 u8 buf[MAX_DATA_AREA_SZ];
260 struct anybus_mbox_hdr hdr;
263 u8 msg[MAX_MBOX_MSG_SZ];
267 struct kmem_cache *cache;
268 struct kref refcount;
269 ab_task_fn_t task_fn;
270 ab_done_fn_t done_fn;
272 struct completion done;
273 unsigned long start_jiffies;
275 struct area_priv area_pd;
276 struct mbox_priv mbox_pd;
280 static struct ab_task *ab_task_create_get(struct kmem_cache *cache,
281 ab_task_fn_t task_fn)
285 t = kmem_cache_alloc(cache, GFP_KERNEL);
289 kref_init(&t->refcount);
290 t->task_fn = task_fn;
293 init_completion(&t->done);
297 static void __ab_task_destroy(struct kref *refcount)
299 struct ab_task *t = container_of(refcount, struct ab_task, refcount);
300 struct kmem_cache *cache = t->cache;
302 kmem_cache_free(cache, t);
305 static void ab_task_put(struct ab_task *t)
307 kref_put(&t->refcount, __ab_task_destroy);
310 static struct ab_task *__ab_task_get(struct ab_task *t)
312 kref_get(&t->refcount);
316 static void __ab_task_finish(struct ab_task *t, struct anybuss_host *cd)
324 ab_task_dequeue_finish_put(struct kfifo *q, struct anybuss_host *cd)
329 ret = kfifo_out(q, &t, sizeof(t));
331 __ab_task_finish(t, cd);
336 ab_task_enqueue(struct ab_task *t, struct kfifo *q, spinlock_t *slock,
337 wait_queue_head_t *wq)
341 t->start_jiffies = jiffies;
343 ret = kfifo_in_spinlocked(q, &t, sizeof(t), slock);
353 ab_task_enqueue_wait(struct ab_task *t, struct kfifo *q, spinlock_t *slock,
354 wait_queue_head_t *wq)
358 ret = ab_task_enqueue(t, q, slock, wq);
361 ret = wait_for_completion_interruptible(&t->done);
367 /* ------------------------ anybus hardware ------------------------ */
369 struct anybuss_host {
371 struct anybuss_client *client;
372 void (*reset)(struct device *dev, bool assert);
373 struct regmap *regmap;
376 struct task_struct *qthread;
377 wait_queue_head_t wq;
378 struct completion card_boot;
380 spinlock_t qlock; /* protects IN side of powerq, mboxq, areaq */
381 struct kmem_cache *qcache;
383 struct kfifo *powerq;
387 bool softint_pending;
390 static void reset_assert(struct anybuss_host *cd)
392 cd->reset(cd->dev, true);
395 static void reset_deassert(struct anybuss_host *cd)
397 cd->reset(cd->dev, false);
400 static int test_dpram(struct regmap *regmap)
405 for (i = 0; i < DPRAM_SIZE; i++)
406 regmap_write(regmap, i, (u8)i);
407 for (i = 0; i < DPRAM_SIZE; i++) {
408 regmap_read(regmap, i, &val);
409 if ((u8)val != (u8)i)
415 static int read_ind_ab(struct regmap *regmap)
417 unsigned long timeout = jiffies + HZ / 2;
418 unsigned int a, b, i = 0;
420 while (time_before_eq(jiffies, timeout)) {
421 regmap_read(regmap, REG_IND_AB, &a);
422 regmap_read(regmap, REG_IND_AB, &b);
429 usleep_range(500, 1000);
432 WARN(1, "IND_AB register not stable");
436 static int write_ind_ap(struct regmap *regmap, unsigned int ind_ap)
438 unsigned long timeout = jiffies + HZ / 2;
439 unsigned int v, i = 0;
441 while (time_before_eq(jiffies, timeout)) {
442 regmap_write(regmap, REG_IND_AP, ind_ap);
443 regmap_read(regmap, REG_IND_AP, &v);
444 if (likely(ind_ap == v))
450 usleep_range(500, 1000);
453 WARN(1, "IND_AP register not stable");
457 static irqreturn_t irq_handler(int irq, void *data)
459 struct anybuss_host *cd = data;
463 * irq handler needs exclusive access to the IND_AB register,
464 * because the act of reading the register acks the interrupt.
466 * store the register value in cd->ind_ab (an atomic_t), so that the
467 * queue thread is able to read it without causing an interrupt ack
468 * side-effect (and without spuriously acking an interrupt).
470 ind_ab = read_ind_ab(cd->regmap);
473 atomic_set(&cd->ind_ab, ind_ab);
474 complete(&cd->card_boot);
479 /* ------------------------ power on/off tasks --------------------- */
481 static int task_fn_power_off(struct anybuss_host *cd,
484 struct anybuss_client *client = cd->client;
488 disable_irq(cd->irq);
490 atomic_set(&cd->ind_ab, IND_AB_UPDATED);
491 if (client->on_online_changed)
492 client->on_online_changed(client, false);
493 cd->power_on = false;
497 static int task_fn_power_on_2(struct anybuss_host *cd,
500 if (completion_done(&cd->card_boot)) {
504 if (time_after(jiffies, t->start_jiffies + TIMEOUT)) {
505 disable_irq(cd->irq);
507 dev_err(cd->dev, "power on timed out");
513 static int task_fn_power_on(struct anybuss_host *cd,
521 * anybus docs: prevent false 'init done' interrupt by
522 * doing a dummy read of IND_AB register while in reset.
524 regmap_read(cd->regmap, REG_IND_AB, &dummy);
525 reinit_completion(&cd->card_boot);
528 t->task_fn = task_fn_power_on_2;
532 int anybuss_set_power(struct anybuss_client *client, bool power_on)
534 struct anybuss_host *cd = client->host;
538 t = ab_task_create_get(cd->qcache, power_on ?
539 task_fn_power_on : task_fn_power_off);
542 err = ab_task_enqueue_wait(t, cd->powerq, &cd->qlock, &cd->wq);
546 EXPORT_SYMBOL_GPL(anybuss_set_power);
548 /* ---------------------------- area tasks ------------------------ */
550 static int task_fn_area_3(struct anybuss_host *cd, struct ab_task *t)
552 struct area_priv *pd = &t->area_pd;
556 if (atomic_read(&cd->ind_ab) & pd->flags) {
557 /* area not released yet */
558 if (time_after(jiffies, t->start_jiffies + TIMEOUT))
565 static int task_fn_area_2(struct anybuss_host *cd, struct ab_task *t)
567 struct area_priv *pd = &t->area_pd;
573 regmap_read(cd->regmap, REG_IND_AP, &ind_ap);
574 if (!(atomic_read(&cd->ind_ab) & pd->flags)) {
575 /* we don't own the area yet */
576 if (time_after(jiffies, t->start_jiffies + TIMEOUT)) {
577 dev_warn(cd->dev, "timeout waiting for area");
583 /* we own the area, do what we're here to do */
585 regmap_bulk_write(cd->regmap, pd->addr, pd->buf,
588 regmap_bulk_read(cd->regmap, pd->addr, pd->buf,
590 /* ask to release the area, must use unlocked release */
591 ind_ap &= ~IND_AP_ABITS;
593 ret = write_ind_ap(cd->regmap, ind_ap);
596 t->task_fn = task_fn_area_3;
600 static int task_fn_area(struct anybuss_host *cd, struct ab_task *t)
602 struct area_priv *pd = &t->area_pd;
608 regmap_read(cd->regmap, REG_IND_AP, &ind_ap);
609 /* ask to take the area */
610 ind_ap &= ~IND_AP_ABITS;
611 ind_ap |= pd->flags | IND_AP_ACTION | IND_AP_LOCK;
612 ret = write_ind_ap(cd->regmap, ind_ap);
615 t->task_fn = task_fn_area_2;
619 static struct ab_task *
620 create_area_reader(struct kmem_cache *qcache, u16 flags, u16 addr,
624 struct area_priv *ap;
626 t = ab_task_create_get(qcache, task_fn_area);
632 ap->is_write = false;
637 static struct ab_task *
638 create_area_writer(struct kmem_cache *qcache, u16 flags, u16 addr,
639 const void *buf, size_t count)
642 struct area_priv *ap;
644 t = ab_task_create_get(qcache, task_fn_area);
652 memcpy(ap->buf, buf, count);
656 static struct ab_task *
657 create_area_user_writer(struct kmem_cache *qcache, u16 flags, u16 addr,
658 const void __user *buf, size_t count)
661 struct area_priv *ap;
663 t = ab_task_create_get(qcache, task_fn_area);
665 return ERR_PTR(-ENOMEM);
671 if (copy_from_user(ap->buf, buf, count)) {
673 return ERR_PTR(-EFAULT);
678 static bool area_range_ok(u16 addr, size_t count, u16 area_start,
681 u16 area_end_ex = area_start + area_sz;
684 if (addr < area_start)
686 if (addr >= area_end_ex)
688 addr_end_ex = addr + count;
689 if (addr_end_ex > area_end_ex)
694 /* -------------------------- mailbox tasks ----------------------- */
696 static int task_fn_mbox_2(struct anybuss_host *cd, struct ab_task *t)
698 struct mbox_priv *pd = &t->mbox_pd;
703 regmap_read(cd->regmap, REG_IND_AP, &ind_ap);
704 if (((atomic_read(&cd->ind_ab) ^ ind_ap) & IND_AX_MOUT) == 0) {
705 /* output message not here */
706 if (time_after(jiffies, t->start_jiffies + TIMEOUT))
710 /* grab the returned header and msg */
711 regmap_bulk_read(cd->regmap, MBOX_OUT_AREA, &pd->hdr,
713 regmap_bulk_read(cd->regmap, MBOX_OUT_AREA + sizeof(pd->hdr),
714 pd->msg, pd->msg_in_sz);
715 /* tell anybus we've consumed the message */
716 ind_ap ^= IND_AX_MOUT;
717 return write_ind_ap(cd->regmap, ind_ap);
720 static int task_fn_mbox(struct anybuss_host *cd, struct ab_task *t)
722 struct mbox_priv *pd = &t->mbox_pd;
728 regmap_read(cd->regmap, REG_IND_AP, &ind_ap);
729 if ((atomic_read(&cd->ind_ab) ^ ind_ap) & IND_AX_MIN) {
730 /* mbox input area busy */
731 if (time_after(jiffies, t->start_jiffies + TIMEOUT))
735 /* write the header and msg to input area */
736 regmap_bulk_write(cd->regmap, MBOX_IN_AREA, &pd->hdr,
738 regmap_bulk_write(cd->regmap, MBOX_IN_AREA + sizeof(pd->hdr),
739 pd->msg, pd->msg_out_sz);
740 /* tell anybus we gave it a message */
741 ind_ap ^= IND_AX_MIN;
742 ret = write_ind_ap(cd->regmap, ind_ap);
745 t->start_jiffies = jiffies;
746 t->task_fn = task_fn_mbox_2;
750 static void log_invalid_other(struct device *dev,
751 struct anybus_mbox_hdr *hdr)
753 size_t ext_offs = ARRAY_SIZE(hdr->extended) - 1;
754 u16 code = be16_to_cpu(hdr->extended[ext_offs]);
756 dev_err(dev, " Invalid other: [0x%02X]", code);
759 static const char * const EMSGS[] = {
760 "Invalid Message ID",
761 "Invalid Message Type",
764 "Message Header Malformed (offset 008h)",
765 "Message Header Malformed (offset 00Ah)",
766 "Message Header Malformed (offset 00Ch - 00Dh)",
769 "Flash Config Error",
772 static int mbox_cmd_err(struct device *dev, struct mbox_priv *mpriv)
776 struct anybus_mbox_hdr *hdr = &mpriv->hdr;
777 u16 info = be16_to_cpu(hdr->info);
778 u8 *phdr = (u8 *)hdr;
779 u8 *pmsg = mpriv->msg;
781 if (!(info & 0x8000))
783 ecode = (info >> 8) & 0x0F;
784 dev_err(dev, "mailbox command failed:");
786 log_invalid_other(dev, hdr);
787 else if (ecode < ARRAY_SIZE(EMSGS))
788 dev_err(dev, " Error code: %s (0x%02X)",
789 EMSGS[ecode], ecode);
791 dev_err(dev, " Error code: 0x%02X\n", ecode);
792 dev_err(dev, "Failed command:");
793 dev_err(dev, "Message Header:");
794 for (i = 0; i < sizeof(mpriv->hdr); i += 2)
795 dev_err(dev, "%02X%02X", phdr[i], phdr[i + 1]);
796 dev_err(dev, "Message Data:");
797 for (i = 0; i < mpriv->msg_in_sz; i += 2)
798 dev_err(dev, "%02X%02X", pmsg[i], pmsg[i + 1]);
799 dev_err(dev, "Stack dump:");
804 static int _anybus_mbox_cmd(struct anybuss_host *cd,
805 u16 cmd_num, bool is_fb_cmd,
806 const void *msg_out, size_t msg_out_sz,
807 void *msg_in, size_t msg_in_sz,
808 const void *ext, size_t ext_sz)
811 struct mbox_priv *pd;
812 struct anybus_mbox_hdr *h;
813 size_t msg_sz = max(msg_in_sz, msg_out_sz);
817 if (msg_sz > MAX_MBOX_MSG_SZ)
819 if (ext && ext_sz > sizeof(h->extended))
821 t = ab_task_create_get(cd->qcache, task_fn_mbox);
826 info = is_fb_cmd ? INFO_TYPE_FB : INFO_TYPE_APP;
828 * prevent uninitialized memory in the header from being sent
831 memset(h, 0, sizeof(*h));
832 h->info = cpu_to_be16(info | INFO_COMMAND);
833 h->cmd_num = cpu_to_be16(cmd_num);
834 h->data_size = cpu_to_be16(msg_out_sz);
835 h->frame_count = cpu_to_be16(1);
836 h->frame_num = cpu_to_be16(1);
837 h->offset_high = cpu_to_be16(0);
838 h->offset_low = cpu_to_be16(0);
840 memcpy(h->extended, ext, ext_sz);
841 memcpy(pd->msg, msg_out, msg_out_sz);
842 pd->msg_out_sz = msg_out_sz;
843 pd->msg_in_sz = msg_in_sz;
844 err = ab_task_enqueue_wait(t, cd->powerq, &cd->qlock, &cd->wq);
848 * mailbox mechanism worked ok, but maybe the mbox response
849 * contains an error ?
851 err = mbox_cmd_err(cd->dev, pd);
854 memcpy(msg_in, pd->msg, msg_in_sz);
860 /* ------------------------ anybus queues ------------------------ */
862 static void process_q(struct anybuss_host *cd, struct kfifo *q)
867 ret = kfifo_out_peek(q, &t, sizeof(t));
870 t->result = t->task_fn(cd, t);
871 if (t->result != -EINPROGRESS)
872 ab_task_dequeue_finish_put(q, cd);
875 static bool qs_have_work(struct kfifo *qs, size_t num)
881 for (i = 0; i < num; i++, qs++) {
882 ret = kfifo_out_peek(qs, &t, sizeof(t));
883 if (ret && (t->result != -EINPROGRESS))
889 static void process_qs(struct anybuss_host *cd)
892 struct kfifo *qs = cd->qs;
893 size_t nqs = ARRAY_SIZE(cd->qs);
895 for (i = 0; i < nqs; i++, qs++)
899 static void softint_ack(struct anybuss_host *cd)
903 cd->softint_pending = false;
906 regmap_read(cd->regmap, REG_IND_AP, &ind_ap);
907 ind_ap &= ~IND_AX_EVNT;
908 ind_ap |= atomic_read(&cd->ind_ab) & IND_AX_EVNT;
909 write_ind_ap(cd->regmap, ind_ap);
912 static void process_softint(struct anybuss_host *cd)
914 struct anybuss_client *client = cd->client;
915 static const u8 zero;
917 unsigned int ind_ap, ev;
922 if (cd->softint_pending)
924 regmap_read(cd->regmap, REG_IND_AP, &ind_ap);
925 if (!((atomic_read(&cd->ind_ab) ^ ind_ap) & IND_AX_EVNT))
927 /* process software interrupt */
928 regmap_read(cd->regmap, REG_EVENT_CAUSE, &ev);
929 if (ev & EVENT_CAUSE_FBON) {
930 if (client->on_online_changed)
931 client->on_online_changed(client, true);
932 dev_dbg(cd->dev, "Fieldbus ON");
934 if (ev & EVENT_CAUSE_FBOF) {
935 if (client->on_online_changed)
936 client->on_online_changed(client, false);
937 dev_dbg(cd->dev, "Fieldbus OFF");
939 if (ev & EVENT_CAUSE_DC) {
940 if (client->on_area_updated)
941 client->on_area_updated(client);
942 dev_dbg(cd->dev, "Fieldbus data changed");
945 * reset the event cause bits.
946 * this must be done while owning the fbctrl area, so we'll
947 * enqueue a task to do that.
949 t = create_area_writer(cd->qcache, IND_AX_FBCTRL,
950 REG_EVENT_CAUSE, &zero, sizeof(zero));
955 t->done_fn = softint_ack;
956 ret = ab_task_enqueue(t, cd->powerq, &cd->qlock, &cd->wq);
958 cd->softint_pending = true;
965 static int qthread_fn(void *data)
967 struct anybuss_host *cd = data;
968 struct kfifo *qs = cd->qs;
969 size_t nqs = ARRAY_SIZE(cd->qs);
973 * this kernel thread has exclusive access to the anybus's memory.
974 * only exception: the IND_AB register, which is accessed exclusively
975 * by the interrupt service routine (ISR). This thread must not touch
976 * the IND_AB register, but it does require access to its value.
978 * the interrupt service routine stores the register's value in
979 * cd->ind_ab (an atomic_t), where we may safely access it, with the
980 * understanding that it can be modified by the ISR at any time.
983 while (!kthread_should_stop()) {
985 * make a local copy of IND_AB, so we can go around the loop
986 * again in case it changed while processing queues and softint.
988 ind_ab = atomic_read(&cd->ind_ab);
991 wait_event_timeout(cd->wq,
992 (atomic_read(&cd->ind_ab) != ind_ab) ||
993 qs_have_work(qs, nqs) ||
994 kthread_should_stop(),
997 * time out so even 'stuck' tasks will run eventually,
1005 /* ------------------------ anybus exports ------------------------ */
1007 int anybuss_start_init(struct anybuss_client *client,
1008 const struct anybuss_memcfg *cfg)
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),
1024 switch (cfg->offl_mode) {
1025 case FIELDBUS_DEV_OFFL_MODE_CLEAR:
1028 case FIELDBUS_DEV_OFFL_MODE_FREEZE:
1029 op_mode = OP_MODE_FBFC;
1031 case FIELDBUS_DEV_OFFL_MODE_SET:
1032 op_mode = OP_MODE_FBS;
1037 msg.op_mode = cpu_to_be16(op_mode | OP_MODE_CD);
1038 ret = _anybus_mbox_cmd(cd, CMD_START_INIT, false, NULL, 0,
1042 return _anybus_mbox_cmd(cd, CMD_ANYBUS_INIT, false,
1043 &msg, sizeof(msg), NULL, 0, NULL, 0);
1045 EXPORT_SYMBOL_GPL(anybuss_start_init);
1047 int anybuss_finish_init(struct anybuss_client *client)
1049 struct anybuss_host *cd = client->host;
1051 return _anybus_mbox_cmd(cd, CMD_END_INIT, false, NULL, 0,
1054 EXPORT_SYMBOL_GPL(anybuss_finish_init);
1056 int anybuss_read_fbctrl(struct anybuss_client *client, u16 addr,
1057 void *buf, size_t count)
1059 struct anybuss_host *cd = client->host;
1065 if (!area_range_ok(addr, count, FBCTRL_AREA,
1066 MAX_FBCTRL_AREA_SZ))
1068 t = create_area_reader(cd->qcache, IND_AX_FBCTRL, addr, count);
1071 ret = ab_task_enqueue_wait(t, cd->powerq, &cd->qlock, &cd->wq);
1074 memcpy(buf, t->area_pd.buf, count);
1079 EXPORT_SYMBOL_GPL(anybuss_read_fbctrl);
1081 int anybuss_write_input(struct anybuss_client *client,
1082 const char __user *buf, size_t size,
1085 ssize_t len = min_t(loff_t, MAX_DATA_AREA_SZ - *offset, size);
1086 struct anybuss_host *cd = client->host;
1092 t = create_area_user_writer(cd->qcache, IND_AX_IN,
1093 DATA_IN_AREA + *offset, buf, len);
1096 ret = ab_task_enqueue_wait(t, cd->powerq, &cd->qlock, &cd->wq);
1104 EXPORT_SYMBOL_GPL(anybuss_write_input);
1106 int anybuss_read_output(struct anybuss_client *client,
1107 char __user *buf, size_t size,
1110 ssize_t len = min_t(loff_t, MAX_DATA_AREA_SZ - *offset, size);
1111 struct anybuss_host *cd = client->host;
1117 t = create_area_reader(cd->qcache, IND_AX_OUT,
1118 DATA_OUT_AREA + *offset, len);
1121 ret = ab_task_enqueue_wait(t, cd->powerq, &cd->qlock, &cd->wq);
1124 if (copy_to_user(buf, t->area_pd.buf, len))
1134 EXPORT_SYMBOL_GPL(anybuss_read_output);
1136 int anybuss_send_msg(struct anybuss_client *client, u16 cmd_num,
1137 const void *buf, size_t count)
1139 struct anybuss_host *cd = client->host;
1141 return _anybus_mbox_cmd(cd, cmd_num, true, buf, count, NULL, 0,
1144 EXPORT_SYMBOL_GPL(anybuss_send_msg);
1146 int anybuss_send_ext(struct anybuss_client *client, u16 cmd_num,
1147 const void *buf, size_t count)
1149 struct anybuss_host *cd = client->host;
1151 return _anybus_mbox_cmd(cd, cmd_num, true, NULL, 0, NULL, 0,
1154 EXPORT_SYMBOL_GPL(anybuss_send_ext);
1156 int anybuss_recv_msg(struct anybuss_client *client, u16 cmd_num,
1157 void *buf, size_t count)
1159 struct anybuss_host *cd = client->host;
1161 return _anybus_mbox_cmd(cd, cmd_num, true, NULL, 0, buf, count,
1164 EXPORT_SYMBOL_GPL(anybuss_recv_msg);
1166 /* ------------------------ bus functions ------------------------ */
1168 static int anybus_bus_match(struct device *dev,
1169 struct device_driver *drv)
1171 struct anybuss_client_driver *adrv =
1172 to_anybuss_client_driver(drv);
1173 struct anybuss_client *adev =
1174 to_anybuss_client(dev);
1176 return adrv->anybus_id == be16_to_cpu(adev->anybus_id);
1179 static int anybus_bus_probe(struct device *dev)
1181 struct anybuss_client_driver *adrv =
1182 to_anybuss_client_driver(dev->driver);
1183 struct anybuss_client *adev =
1184 to_anybuss_client(dev);
1188 return adrv->probe(adev);
1191 static int anybus_bus_remove(struct device *dev)
1193 struct anybuss_client_driver *adrv =
1194 to_anybuss_client_driver(dev->driver);
1197 return adrv->remove(to_anybuss_client(dev));
1201 static struct bus_type anybus_bus = {
1203 .match = anybus_bus_match,
1204 .probe = anybus_bus_probe,
1205 .remove = anybus_bus_remove,
1208 int anybuss_client_driver_register(struct anybuss_client_driver *drv)
1210 drv->driver.bus = &anybus_bus;
1211 return driver_register(&drv->driver);
1213 EXPORT_SYMBOL_GPL(anybuss_client_driver_register);
1215 void anybuss_client_driver_unregister(struct anybuss_client_driver *drv)
1217 return driver_unregister(&drv->driver);
1219 EXPORT_SYMBOL_GPL(anybuss_client_driver_unregister);
1221 static void client_device_release(struct device *dev)
1223 kfree(to_anybuss_client(dev));
1226 static int taskq_alloc(struct device *dev, struct kfifo *q)
1229 size_t size = 64 * sizeof(struct ab_task *);
1231 buf = devm_kzalloc(dev, size, GFP_KERNEL);
1234 return kfifo_init(q, buf, size);
1237 static int anybus_of_get_host_idx(struct device_node *np)
1239 const __be32 *host_idx;
1241 host_idx = of_get_address(np, 0, NULL, NULL);
1244 return __be32_to_cpu(*host_idx);
1247 static struct device_node *
1248 anybus_of_find_child_device(struct device *dev, int host_idx)
1250 struct device_node *node;
1252 if (!dev || !dev->of_node)
1254 for_each_child_of_node(dev->of_node, node) {
1255 if (anybus_of_get_host_idx(node) == host_idx)
1261 struct anybuss_host * __must_check
1262 anybuss_host_common_probe(struct device *dev,
1263 const struct anybuss_ops *ops)
1267 __be16 fieldbus_type;
1268 struct anybuss_host *cd;
1270 cd = devm_kzalloc(dev, sizeof(*cd), GFP_KERNEL);
1272 return ERR_PTR(-ENOMEM);
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]);
1280 return ERR_PTR(ret);
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;
1289 return ERR_PTR(-EINVAL);
1290 cd->regmap = ops->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);
1297 return ERR_PTR(-ENOMEM);
1304 * use a dpram test to check if a card is present, this is only
1305 * possible while in reset.
1308 if (test_dpram(cd->regmap)) {
1309 dev_err(dev, "no Anybus-S card in slot");
1313 ret = devm_request_threaded_irq(dev, cd->irq, NULL, irq_handler,
1314 IRQF_ONESHOT, dev_name(dev), cd);
1316 dev_err(dev, "could not request irq");
1321 * perform dummy IND_AB read to prevent false 'init done' irq
1322 * (already done by test_dpram() above)
1324 * wait for first interrupt
1325 * interrupt came in: ready to go !
1328 if (!wait_for_completion_timeout(&cd->card_boot, TIMEOUT)) {
1333 * according to the anybus docs, we're allowed to read these
1334 * without handshaking / reserving the area
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",
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",
1354 /* put card back reset until a client driver releases it */
1355 disable_irq(cd->irq);
1357 atomic_set(&cd->ind_ab, IND_AB_UPDATED);
1358 /* fire up the queue thread */
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);
1366 * now advertise that we've detected a client device (card).
1367 * the bus infrastructure will match it to a client driver.
1369 cd->client = kzalloc(sizeof(*cd->client), GFP_KERNEL);
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);
1387 device_unregister(&cd->client->dev);
1389 kthread_stop(cd->qthread);
1393 kmem_cache_destroy(cd->qcache);
1394 return ERR_PTR(ret);
1396 EXPORT_SYMBOL_GPL(anybuss_host_common_probe);
1398 void anybuss_host_common_remove(struct anybuss_host *host)
1400 struct anybuss_host *cd = host;
1402 device_unregister(&cd->client->dev);
1403 kthread_stop(cd->qthread);
1405 kmem_cache_destroy(cd->qcache);
1407 EXPORT_SYMBOL_GPL(anybuss_host_common_remove);
1409 static void host_release(struct device *dev, void *res)
1411 struct anybuss_host **dr = res;
1413 anybuss_host_common_remove(*dr);
1416 struct anybuss_host * __must_check
1417 devm_anybuss_host_common_probe(struct device *dev,
1418 const struct anybuss_ops *ops)
1420 struct anybuss_host **dr;
1421 struct anybuss_host *host;
1423 dr = devres_alloc(host_release, sizeof(struct anybuss_host *),
1426 return ERR_PTR(-ENOMEM);
1428 host = anybuss_host_common_probe(dev, ops);
1434 devres_add(dev, dr);
1437 EXPORT_SYMBOL_GPL(devm_anybuss_host_common_probe);
1439 static int __init anybus_init(void)
1443 ret = bus_register(&anybus_bus);
1445 pr_err("could not register Anybus-S bus: %d\n", ret);
1448 module_init(anybus_init);
1450 static void __exit anybus_exit(void)
1452 bus_unregister(&anybus_bus);
1454 module_exit(anybus_exit);
1456 MODULE_DESCRIPTION("HMS Anybus-S Host Driver");
1458 MODULE_LICENSE("GPL v2");