2 * Linux CAN-bus device driver.
3 * Written by Arnaud Westenberg email:arnaud@wanadoo.nl
4 * This software is released under the GPL-License.
5 * Version 0.7 6 Aug 2001
9 #include <linux/module.h>
11 #include <linux/autoconf.h>
12 #if defined (CONFIG_MODVERSIONS) && !defined (MODVERSIONS)
16 #if defined (MODVERSIONS)
17 #include <linux/modversions.h>
22 #include "../include/main.h"
23 #include "../include/i82527.h"
29 int i82527_enable_configuration(struct chip_t *chip)
31 unsigned short flags=0;
33 flags = can_read_reg(chip, iCTL) & (iCTL_IE|iCTL_SIE|iCTL_EIE);
34 can_write_reg(chip, flags|iCTL_CCE, iCTL);
39 int i82527_disable_configuration(struct chip_t *chip)
41 unsigned short flags=0;
43 flags = can_read_reg(chip, iCTL) & (iCTL_IE|iCTL_SIE|iCTL_EIE);
44 can_write_reg(chip, flags, iCTL);
49 int i82527_chip_config(struct chip_t *chip)
51 can_write_reg(chip,chip->int_cpu_reg,iCPU); // Configure cpu interface
52 can_write_reg(chip,(iCTL_CCE|iCTL_INI),iCTL); // Enable configuration
53 can_write_reg(chip,chip->int_clk_reg,iCLK); // Set clock out slew rates
54 can_write_reg(chip,chip->int_bus_reg,iBUS); /* Bus configuration */
55 can_write_reg(chip,0x00,iSTAT); /* Clear error status register */
57 /* Check if we can at least read back some arbitrary data from the
58 * card. If we can not, the card is not properly configured!
60 can_write_reg(chip,0x25,MSG_OFFSET(1)+iMSGDAT1);
61 can_write_reg(chip,0x52,MSG_OFFSET(2)+iMSGDAT3);
62 can_write_reg(chip,0xc3,MSG_OFFSET(10)+iMSGDAT6);
63 if ( (can_read_reg(chip,MSG_OFFSET(1)+iMSGDAT1) != 0x25) ||
64 (can_read_reg(chip,MSG_OFFSET(2)+iMSGDAT3) != 0x52) ||
65 (can_read_reg(chip,MSG_OFFSET(10)+iMSGDAT6) != 0xc3) ) {
66 CANMSG("Could not read back from the hardware.\n");
67 CANMSG("This probably means that your hardware is not correctly configured!\n");
71 DEBUGMSG("Could read back, hardware is probably configured correctly\n");
76 if (i82527_baud_rate(chip,baudrate*1000,chip->clock,0,75,0)) {
77 CANMSG("Error configuring baud rate\n");
80 if (i82527_standard_mask(chip,0x0000,stdmask)) {
81 CANMSG("Error configuring standard mask\n");
84 if (i82527_extended_mask(chip,0x00000000,extmask)) {
85 CANMSG("Error configuring extended mask\n");
88 if (i82527_message15_mask(chip,0x00000000,mo15mask)) {
89 CANMSG("Error configuring message 15 mask\n");
92 if (i82527_clear_objects(chip)) {
93 CANMSG("Error clearing message objects\n");
96 if (i82527_config_irqs(chip,0x0a)) {
97 CANMSG("Error configuring interrupts\n");
104 /* Set communication parameters.
105 * param rate baud rate in Hz
106 * param clock frequency of i82527 clock in Hz (ISA osc is 14318000)
107 * param sjw synchronization jump width (0-3) prescaled clock cycles
108 * param sampl_pt sample point in % (0-100) sets (TSEG1+2)/(TSEG1+TSEG2+3) ratio
109 * param flags fields BTR1_SAM, OCMODE, OCPOL, OCTP, OCTN, CLK_OFF, CBP
111 int i82527_baud_rate(struct chip_t *chip, int rate, int clock, int sjw,
112 int sampl_pt, int flags)
114 int best_error = 1000000000, error;
115 int best_tseg=0, best_brp=0, best_rate=0, brp=0;
116 int tseg=0, tseg1=0, tseg2=0;
118 if (i82527_enable_configuration(chip))
123 /* tseg even = round down, odd = round up */
124 for (tseg=(0+0+2)*2; tseg<=(MAX_TSEG2+MAX_TSEG1+2)*2+1; tseg++) {
125 brp = clock/((1+tseg/2)*rate)+tseg%2;
126 if (brp == 0 || brp > 64)
128 error = rate - clock/(brp*(1+tseg/2));
131 if (error <= best_error) {
135 best_rate = clock/(brp*(1+tseg/2));
138 if (best_error && (rate/best_error < 10)) {
139 CANMSG("baud rate %d is not possible with %d Hz clock\n",
141 CANMSG("%d bps. brp=%d, best_tseg=%d, tseg1=%d, tseg2=%d\n",
142 best_rate, best_brp, best_tseg, tseg1, tseg2);
145 tseg2 = best_tseg-(sampl_pt*(best_tseg+1))/100;
148 if (tseg2 > MAX_TSEG2)
151 tseg1 = best_tseg-tseg2-2;
152 if (tseg1>MAX_TSEG1) {
154 tseg2 = best_tseg-tseg1-2;
157 DEBUGMSG("Setting %d bps.\n", best_rate);
158 DEBUGMSG("brp=%d, best_tseg=%d, tseg1=%d, tseg2=%d, sampl_pt=%d\n",
159 best_brp, best_tseg, tseg1, tseg2,
160 (100*(best_tseg-tseg2)/(best_tseg+1)));
163 can_write_reg(chip, sjw<<6 | best_brp, iBT0);
164 can_write_reg(chip, ((flags & BTR1_SAM) != 0)<<7 | tseg2<<4 | tseg1,
166 DEBUGMSG("Writing 0x%x to iBT0\n",(sjw<<6 | best_brp));
167 DEBUGMSG("Writing 0x%x to iBT1\n",((flags & BTR1_SAM) != 0)<<7 |
170 i82527_disable_configuration(chip);
175 int i82527_standard_mask(struct chip_t *chip, unsigned short code, unsigned short mask)
177 unsigned char mask0, mask1;
179 mask0 = (unsigned char) (mask >> 3);
180 mask1 = (unsigned char) (mask << 5);
182 can_write_reg(chip,mask0,iSGM0);
183 can_write_reg(chip,mask1,iSGM1);
185 DEBUGMSG("Setting standard mask to 0x%lx\n",(unsigned long)mask);
190 int i82527_extended_mask(struct chip_t *chip, unsigned long code, unsigned long mask)
192 unsigned char mask0, mask1, mask2, mask3;
194 mask0 = (unsigned char) (mask >> 21);
195 mask1 = (unsigned char) (mask >> 13);
196 mask2 = (unsigned char) (mask >> 5);
197 mask3 = (unsigned char) (mask << 3);
199 can_write_reg(chip,mask0,iEGM0);
200 can_write_reg(chip,mask1,iEGM1);
201 can_write_reg(chip,mask2,iEGM2);
202 can_write_reg(chip,mask3,iEGM3);
204 DEBUGMSG("Setting extended mask to 0x%lx\n",(unsigned long)mask);
209 int i82527_message15_mask(struct chip_t *chip, unsigned long code, unsigned long mask)
211 unsigned char mask0, mask1, mask2, mask3;
213 mask0 = (unsigned char) (mask >> 21);
214 mask1 = (unsigned char) (mask >> 13);
215 mask2 = (unsigned char) (mask >> 5);
216 mask3 = (unsigned char) (mask << 3);
218 can_write_reg(chip,mask0,i15M0);
219 can_write_reg(chip,mask1,i15M1);
220 can_write_reg(chip,mask2,i15M2);
221 can_write_reg(chip,mask3,i15M3);
223 DEBUGMSG("Setting message 15 mask to 0x%lx\n",mask);
230 int i82527_clear_objects(struct chip_t *chip)
234 DEBUGMSG("Cleared all message objects on chip\n");
236 for (i=1; i<=15; i++) {
237 can_write_reg(chip,(INTPD_RES|RXIE_RES|TXIE_RES|MVAL_RES) ,
238 MSG_OFFSET(i)+iMSGCTL0);
239 can_write_reg(chip,(NEWD_RES|MLST_RES|TXRQ_RES|RMPD_RES) ,
240 MSG_OFFSET(i)+iMSGCTL1);
241 for (data=0x07; data<0x0f; data++)
242 can_write_reg(chip,0x00,MSG_OFFSET(i)+data);
243 for (id=2; id<6; id++) {
244 can_write_reg(chip,0x00,MSG_OFFSET(i)+id);
247 can_write_reg(chip,0x00,MSG_OFFSET(i)+iMSGCFG);
250 can_write_reg(chip,MCFG_XTD,MSG_OFFSET(i)+iMSGCFG);
254 DEBUGMSG("All message ID's set to standard\n");
256 DEBUGMSG("All message ID's set to extended\n");
261 int i82527_config_irqs(struct chip_t *chip, short irqs)
263 can_write_reg(chip,irqs,iCTL);
264 DEBUGMSG("Configured hardware interrupt delivery\n");
268 int i82527_pre_read_config(struct chip_t *chip, struct msgobj_t *obj)
271 can_write_reg(chip,MCFG_XTD,MSG_OFFSET(obj->object)+iMSGCFG);
274 can_write_reg(chip,0x00,MSG_OFFSET(obj->object)+iMSGCFG);
276 can_write_reg(chip ,(NEWD_RES|MLST_RES|TXRQ_RES|RMPD_RES),
277 MSG_OFFSET(obj->object)+iMSGCTL1);
278 can_write_reg(chip ,(MVAL_SET|TXIE_RES|RXIE_SET|INTPD_RES),
279 MSG_OFFSET(obj->object)+iMSGCTL0);
284 int i82527_pre_write_config(struct chip_t *chip, struct msgobj_t *obj,
285 struct canmsg_t *msg)
287 int i=0,id0=0,id1=0,id2=0,id3=0;
289 can_write_reg(chip,(RMPD_RES|TXRQ_RES|CPUU_SET|NEWD_RES),
290 MSG_OFFSET(obj->object)+iMSGCTL1);
291 can_write_reg(chip,(MVAL_SET|TXIE_SET|RXIE_RES|INTPD_RES),
292 MSG_OFFSET(obj->object)+iMSGCTL0);
294 can_write_reg(chip,(msg->length<<4)+(MCFG_DIR|MCFG_XTD),
295 MSG_OFFSET(obj->object)+iMSGCFG);
298 can_write_reg(chip,(msg->length<<4)+MCFG_DIR,
299 MSG_OFFSET(obj->object)+iMSGCFG);
302 id0 = (unsigned char) (msg->id<<3);
303 id1 = (unsigned char) (msg->id>>5);
304 id2 = (unsigned char) (msg->id>>13);
305 id3 = (unsigned char) (msg->id>>21);
306 can_write_reg(chip,id0,MSG_OFFSET(obj->object)+iMSGID3);
307 can_write_reg(chip,id1,MSG_OFFSET(obj->object)+iMSGID2);
308 can_write_reg(chip,id2,MSG_OFFSET(obj->object)+iMSGID1);
309 can_write_reg(chip,id3,MSG_OFFSET(obj->object)+iMSGID0);
312 id1 = (unsigned char) (msg->id<<5);
313 id0 = (unsigned char) (msg->id>>3);
314 can_write_reg(chip,id1,MSG_OFFSET(obj->object)+iMSGID1);
315 can_write_reg(chip,id0,MSG_OFFSET(obj->object)+iMSGID0);
317 can_write_reg(chip,0xfa,MSG_OFFSET(obj->object)+iMSGCTL1);
318 for (i=0; i<msg->length; i++) {
319 can_write_reg(chip,msg->data[i],MSG_OFFSET(obj->object)+
326 int i82527_send_msg(struct chip_t *chip, struct msgobj_t *obj,
327 struct canmsg_t *msg)
329 if (msg->flags & MSG_RTR) {
330 can_write_reg(chip,(RMPD_RES|TXRQ_RES|CPUU_RES|NEWD_SET),
331 MSG_OFFSET(obj->object)+iMSGCTL1);
334 can_write_reg(chip,(RMPD_RES|TXRQ_SET|CPUU_RES|NEWD_SET),
335 MSG_OFFSET(obj->object)+iMSGCTL1);
341 int i82527_check_tx_stat(struct chip_t *chip)
343 if (can_read_reg(chip,iSTAT) & iSTAT_TXOK) {
344 can_write_reg(chip,0x0,iSTAT);
348 can_write_reg(chip,0x0,iSTAT);
353 int i82527_remote_request(struct chip_t *chip, struct msgobj_t *obj)
355 can_write_reg(chip, (MVAL_SET|TXIE_RES|RXIE_SET|INTPD_RES),
356 MSG_OFFSET(obj->object)+iMSGCTL0);
357 can_write_reg(chip, (RMPD_RES|TXRQ_SET|MLST_RES|NEWD_RES),
358 MSG_OFFSET(obj->object)+iMSGCTL1);
363 int i82527_set_btregs(struct chip_t *chip, unsigned short btr0,
366 if (i82527_enable_configuration(chip))
369 can_write_reg(chip, btr0, iBT0);
370 can_write_reg(chip, btr1, iBT1);
372 i82527_disable_configuration(chip);
377 int i82527_start_chip(struct chip_t *chip)
379 unsigned short flags = 0;
381 flags = can_read_reg(chip, iCTL) & (iCTL_IE|iCTL_SIE|iCTL_EIE);
382 can_write_reg(chip, flags, iCTL);
387 int i82527_stop_chip(struct chip_t *chip)
389 unsigned short flags = 0;
391 flags = can_read_reg(chip, iCTL) & (iCTL_IE|iCTL_SIE|iCTL_EIE);
392 can_write_reg(chip, flags|(iCTL_CCE|iCTL_INI), iCTL);
397 int i82527_register(struct chipspecops_t *chipspecops)
399 chipspecops->chip_config = i82527_chip_config;
400 chipspecops->baud_rate = i82527_baud_rate;
401 chipspecops->standard_mask = i82527_standard_mask;
402 chipspecops->extended_mask = i82527_extended_mask;
403 chipspecops->message15_mask = i82527_message15_mask;
404 chipspecops->clear_objects = i82527_clear_objects;
405 chipspecops->config_irqs = i82527_config_irqs;
406 chipspecops->pre_read_config = i82527_pre_read_config;
407 chipspecops->pre_write_config = i82527_pre_write_config;
408 chipspecops->send_msg = i82527_send_msg;
409 chipspecops->check_tx_stat = i82527_check_tx_stat;
410 chipspecops->remote_request = i82527_remote_request;
411 chipspecops->enable_configuration = i82527_enable_configuration;
412 chipspecops->disable_configuration = i82527_disable_configuration;
413 chipspecops->set_btregs = i82527_set_btregs;
414 chipspecops->start_chip = i82527_start_chip;
415 chipspecops->stop_chip = i82527_stop_chip;