2 * Linux CAN-bus device driver.
3 * Written for new CAN driver version by Pavel Pisa - OCERA team member
4 * email:pisa@cmp.felk.cvut.cz
5 * This software is released under the GPL-License.
6 * Version lincan-0.2 9 Jul 2003
9 #include "../include/can.h"
10 #include "../include/can_sysdep.h"
11 #include "../include/main.h"
12 #include "../include/unican_cl2.h"
15 long unican_bus_latency(struct msgobj_t *obj)
18 latency=obj->hostchip->baudrate;
20 latency=(long)HZ*1000/latency;
26 /* * * unican Chip Functionality * * */
28 int unican_enable_configuration(struct chip_t *chip)
33 int unican_disable_configuration(struct chip_t *chip)
39 * unican_chip_config: - can chip configuration
40 * @chip: pointer to chip state structure
42 * Return Value: negative value reports error.
45 int unican_chip_config(struct chip_t *chip)
51 * unican_extended_mask: - setup of extended mask for message filtering
52 * @chip: pointer to chip state structure
53 * @code: can message acceptance code
54 * @mask: can message acceptance mask
56 * Return Value: negative value reports error.
59 int unican_extended_mask(struct chip_t *chip, unsigned long code, unsigned long mask)
65 * unican_baud_rate: - set communication parameters.
66 * @chip: pointer to chip state structure
67 * @rate: baud rate in Hz
68 * @clock: frequency of sja1000 clock in Hz (ISA osc is 14318000)
69 * @sjw: synchronization jump width (0-3) prescaled clock cycles
70 * @sampl_pt: sample point in % (0-100) sets (TSEG1+1)/(TSEG1+TSEG2+2) ratio
71 * @flags: fields %BTR1_SAM, %OCMODE, %OCPOL, %OCTP, %OCTN, %CLK_OFF, %CBP
73 * Return Value: negative value reports error.
76 int unican_baud_rate(struct chip_t *chip, int rate, int clock, int sjw,
77 int sampl_pt, int flags)
83 * unican_read: - reads and distributes one or more received messages
84 * @chip: pointer to chip state structure
85 * @obj: pinter to CAN message queue information
89 void unican_read(struct chip_t *chip, struct msgobj_t *obj) {
94 * unican_pre_read_config: - prepares message object for message reception
95 * @chip: pointer to chip state structure
96 * @obj: pointer to message object state structure
98 * Return Value: negative value reports error.
99 * Positive value indicates immediate reception of message.
102 int unican_pre_read_config(struct chip_t *chip, struct msgobj_t *obj)
107 #define MAX_TRANSMIT_WAIT_LOOPS 10
109 * unican_pre_write_config: - prepares message object for message transmission
110 * @chip: pointer to chip state structure
111 * @obj: pointer to message object state structure
112 * @msg: pointer to CAN message
114 * Return Value: negative value reports error.
117 int unican_pre_write_config(struct chip_t *chip, struct msgobj_t *obj,
118 struct canmsg_t *msg)
124 * unican_send_msg: - initiate message transmission
125 * @chip: pointer to chip state structure
126 * @obj: pointer to message object state structure
127 * @msg: pointer to CAN message
129 * This function is called after unican_pre_write_config() function,
130 * which prepares data in chip buffer.
131 * Return Value: negative value reports error.
134 int unican_send_msg(struct chip_t *chip, struct msgobj_t *obj,
135 struct canmsg_t *msg)
141 * unican_check_tx_stat: - checks state of transmission engine
142 * @chip: pointer to chip state structure
144 * Return Value: negative value reports error.
145 * Positive return value indicates transmission under way status.
146 * Zero value indicates finishing of all issued transmission requests.
149 int unican_check_tx_stat(struct chip_t *chip)
155 * unican_set_btregs: - configures bitrate registers
156 * @chip: pointer to chip state structure
157 * @btr0: bitrate register 0
158 * @btr1: bitrate register 1
160 * Return Value: negative value reports error.
163 int unican_set_btregs(struct chip_t *chip, unsigned short btr0,
170 * unican_stop_chip: - starts chip message processing
171 * @chip: pointer to chip state structure
173 * Return Value: negative value reports error.
176 int unican_start_chip(struct chip_t *chip)
182 * unican_stop_chip: - stops chip message processing
183 * @chip: pointer to chip state structure
185 * Return Value: negative value reports error.
188 int unican_stop_chip(struct chip_t *chip)
195 * unican_remote_request: - configures message object and asks for RTR message
196 * @chip: pointer to chip state structure
197 * @obj: pointer to message object structure
199 * Return Value: negative value reports error.
202 int unican_remote_request(struct chip_t *chip, struct msgobj_t *obj)
204 CANMSG("unican_remote_request not implemented\n");
209 * unican_standard_mask: - setup of mask for message filtering
210 * @chip: pointer to chip state structure
211 * @code: can message acceptance code
212 * @mask: can message acceptance mask
214 * Return Value: negative value reports error.
217 int unican_standard_mask(struct chip_t *chip, unsigned short code,
220 CANMSG("unican_standard_mask not implemented\n");
225 * unican_clear_objects: - clears state of all message object residing in chip
226 * @chip: pointer to chip state structure
228 * Return Value: negative value reports error.
231 int unican_clear_objects(struct chip_t *chip)
233 CANMSG("unican_clear_objects not implemented\n");
238 * unican_config_irqs: - tunes chip hardware interrupt delivery
239 * @chip: pointer to chip state structure
240 * @irqs: requested chip IRQ configuration
242 * Return Value: negative value reports error.
245 int unican_config_irqs(struct chip_t *chip, short irqs)
247 CANMSG("unican_config_irqs not implemented\n");
252 * unican_irq_write_handler: - part of ISR code responsible for transmit events
253 * @chip: pointer to chip state structure
254 * @obj: pointer to attached queue description
256 * The main purpose of this function is to read message from attached queues
257 * and transfer message contents into CAN controller chip.
258 * This subroutine is called by
259 * unican_irq_write_handler() for transmit events.
262 void unican_irq_write_handler(struct chip_t *chip, struct msgobj_t *obj)
270 * unican_irq_handler: - interrupt service routine
271 * @irq: interrupt vector number, this value is system specific
272 * @dev_id: driver private pointer registered at time of request_irq() call.
273 * The CAN driver uses this pointer to store relationship of interrupt
274 * to chip state structure - @struct chip_t
275 * @regs: system dependent value pointing to registers stored in exception frame
277 * Interrupt handler is activated when state of CAN controller chip changes,
278 * there is message to be read or there is more space for new messages or
279 * error occurs. The receive events results in reading of the message from
280 * CAN controller chip and distribution of message through attached
284 can_irqreturn_t unican_irq_handler(int irq, void *dev_id, struct pt_regs *regs)
286 return CAN_IRQ_HANDLED;
290 void unican_schedule_next(struct msgobj_t *obj)
294 can_preempt_disable();
296 can_msgobj_set_fl(obj,TX_REQUEST);
298 while(!can_msgobj_test_and_set_fl(obj,TX_LOCK)){
300 can_msgobj_clear_fl(obj,TX_REQUEST);
302 cmd=canque_test_outslot(obj->qends, &obj->tx_qedge, &obj->tx_slot);
304 mod_timer(&obj->tx_timeout,
305 jiffies+unican_bus_latency(obj));
306 DEBUGMSG("unican: scheduled delivery\n");
309 can_msgobj_clear_fl(obj,TX_LOCK);
311 if(!can_msgobj_test_fl(obj,TX_REQUEST)) break;
312 DEBUGMSG("TX looping in unican_schedule_next\n");
315 can_preempt_enable();
319 void unican_do_tx_timeout(unsigned long data)
321 struct msgobj_t *obj=(struct msgobj_t *)data;
324 /* Deliver message to edges */
325 canque_filter_msg2edges(obj->qends, &obj->tx_slot->msg);
326 /* Free transmitted slot */
327 canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
329 DEBUGMSG("unican: delayed delivery\n");
331 can_msgobj_clear_fl(obj,TX_LOCK);
333 unican_schedule_next(obj);
337 * unican_wakeup_tx: - wakeups TX processing
338 * @chip: pointer to chip state structure
339 * @obj: pointer to message object structure
341 * Return Value: negative value reports error.
344 int unican_wakeup_tx(struct chip_t *chip, struct msgobj_t *obj)
346 /* can_msgobj_set_fl(obj,TX_REQUEST); */
348 struct canque_edge_t *qedge;
349 struct canque_slot_t *slot;
352 can_msgobj_clear_fl(obj,TX_REQUEST);
355 if(!unican_bus_latency(obj)) {
356 #endif /*CAN_WITH_RTL*/
357 /* Ensure delivery of all ready slots */
358 while((cmd=canque_test_outslot(obj->qends, &qedge, &slot)) >= 0){
360 canque_filter_msg2edges(obj->qends, &slot->msg);
361 DEBUGMSG("unican: direct delivery\n");
363 canque_free_outslot(obj->qends, qedge, slot);
367 unican_schedule_next(obj);
369 #endif /*CAN_WITH_RTL*/
375 /* * * unican Board Functionality * * */
378 * unican_request_io: - reserve io or memory range for can board
379 * @candev: pointer to candevice/board which asks for io. Field @io_addr
380 * of @candev is used in most cases to define start of the range
382 * Return Value: The function returns zero on success or %-ENODEV on failure
385 int unican_request_io(struct candevice_t *candev)
391 * unican_elease_io - free reserved io memory range
392 * @candev: pointer to candevice/board which releases io
394 * Return Value: The function always returns zero
397 int unican_release_io(struct candevice_t *candev)
403 * unican_reset - hardware reset routine
404 * @candev: Pointer to candevice/board structure
406 * Return Value: The function returns zero on success or %-ENODEV on failure
409 int unican_reset(struct candevice_t *candev)
415 * unican_init_hw_data - Initialize hardware cards
416 * @candev: Pointer to candevice/board structure
418 * Return Value: The function always returns zero
421 int unican_init_hw_data(struct candevice_t *candev)
424 candev->nr_82527_chips=0;
425 candev->nr_sja1000_chips=0;
426 candev->nr_all_chips=1;
427 candev->flags |= CANDEV_PROGRAMMABLE_IRQ*0;
432 #define CHIP_TYPE "unican"
435 * unican_init_chip_data - Initialize chips
436 * @candev: Pointer to candevice/board structure
437 * @chipnr: Number of the CAN chip on the hardware card
439 * Return Value: The function always returns zero
442 int unican_init_chip_data(struct candevice_t *candev, int chipnr)
444 struct chip_t *chip = candev->chip[chipnr];
445 chip->chip_type = CHIP_TYPE;
446 chip->chip_base_addr = 0;
447 chip->clock = 10000000;
448 chip->int_clk_reg = 0x0;
449 chip->int_bus_reg = 0x0;
450 chip->max_objects = 1;
452 CANMSG("initializing unican chip operations\n");
453 chip->chipspecops->chip_config=unican_chip_config;
454 chip->chipspecops->baud_rate=unican_baud_rate;
455 chip->chipspecops->standard_mask=unican_standard_mask;
456 chip->chipspecops->extended_mask=unican_extended_mask;
457 chip->chipspecops->message15_mask=unican_extended_mask;
458 chip->chipspecops->clear_objects=unican_clear_objects;
459 chip->chipspecops->config_irqs=unican_config_irqs;
460 chip->chipspecops->pre_read_config=unican_pre_read_config;
461 chip->chipspecops->pre_write_config=unican_pre_write_config;
462 chip->chipspecops->send_msg=unican_send_msg;
463 chip->chipspecops->check_tx_stat=unican_check_tx_stat;
464 chip->chipspecops->wakeup_tx=unican_wakeup_tx;
465 chip->chipspecops->remote_request=unican_remote_request;
466 chip->chipspecops->enable_configuration=unican_enable_configuration;
467 chip->chipspecops->disable_configuration=unican_disable_configuration;
468 chip->chipspecops->set_btregs=unican_set_btregs;
469 chip->chipspecops->start_chip=unican_start_chip;
470 chip->chipspecops->stop_chip=unican_stop_chip;
471 chip->chipspecops->irq_handler=NULL;
477 * unican_init_obj_data - Initialize message buffers
478 * @chip: Pointer to chip specific structure
479 * @objnr: Number of the message buffer
481 * Return Value: The function always returns zero
484 int unican_init_obj_data(struct chip_t *chip, int objnr)
486 struct msgobj_t *obj=chip->msgobj[objnr];
487 obj->obj_base_addr=chip->chip_base_addr;
488 obj->tx_timeout.function=unican_do_tx_timeout;
489 obj->tx_timeout.data=(unsigned long)obj;
494 * unican_program_irq - program interrupts
495 * @candev: Pointer to candevice/board structure
497 * Return value: The function returns zero on success or %-ENODEV on failure
500 int unican_program_irq(struct candevice_t *candev)
505 int unican_register(struct hwspecops_t *hwspecops)
507 hwspecops->request_io = unican_request_io;
508 hwspecops->release_io = unican_release_io;
509 hwspecops->reset = unican_reset;
510 hwspecops->init_hw_data = unican_init_hw_data;
511 hwspecops->init_chip_data = unican_init_chip_data;
512 hwspecops->init_obj_data = unican_init_obj_data;
513 hwspecops->write_register = NULL;
514 hwspecops->read_register = NULL;
515 hwspecops->program_irq = unican_program_irq;