2 * Linux CAN-bus device driver.
3 * Written by Arnaud Westenberg email:arnaud@wanadoo.nl
4 * Rewritten for new CAN queues by Pavel Pisa - OCERA team member
5 * email:pisa@cmp.felk.cvut.cz
6 * This software is released under the GPL-License.
7 * Version lincan-0.3 17 Jun 2004
9 * The support for TS-CAN1 and TS-7KV provided by Ronald Gomes
10 * from Technologic Systems <http://www.embeddedarm.com/>
13 #include "../include/can.h"
14 #include "../include/can_sysdep.h"
15 #include "../include/main.h"
16 #include "../include/pcm3680.h"
17 #include "../include/sja1000p.h"
19 #include <linux/module.h>
20 #include "../include/modparms.h"
21 #include "../include/devcommon.h"
23 #include "../include/tscan1.h"
25 short clock[MAX_HW_CARDS]={-1,-1,-1,-1,-1,-1,-1,-1};
26 MODULE_PARM(clock, "1-" __MODULE_STRING(MAX_HW_CARDS)"i");
27 MODULE_PARM_DESC(clock,"clock frequency for each board in step of 1kHz");
29 long canio[MAX_HW_CARDS]={-1,-1,-1,-1,-1,-1,-1,-1};
30 MODULE_PARM(canio, "1-" __MODULE_STRING(MAX_HW_CARDS)"i");
31 MODULE_PARM_DESC(canio,"CAN controller IO address for each board");
33 long remap_io_addr[MAX_HW_CARDS]={-1,-1,-1,-1,-1,-1,-1,-1};
34 long remap_can_addr[MAX_HW_CARDS]={-1,-1,-1,-1,-1,-1,-1,-1};
36 unsigned short isused = 0x0;
39 * tscan1_request_io: - reserve io or memory range for can board
40 * @candev: pointer to candevice/board which asks for io. Field @io_addr
41 * of @candev is used in most cases to define start of the range
43 * The function tscan1_request_io() is used to reserve the io-memory. If your
44 * hardware uses a dedicated memory range as hardware control registers you
45 * will have to add the code to reserve this memory as well.
46 * %IO_RANGE is the io-memory range that gets reserved, please adjust according
47 * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
48 * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
49 * Return Value: The function returns zero on success or %-ENODEV on failure
52 int tscan1_request_io(struct candevice_t *candev)
54 unsigned long remap_can_io_addr;
56 remap_io_addr[candev->candev_idx] = tscan1_getmappedaddr(candev->io_addr);
57 remap_can_io_addr = tscan1_getmappedaddr(canio[candev->candev_idx]);
59 candev->dev_base_addr = remap_can_io_addr;
60 candev->chip[0]->chip_base_addr = remap_can_io_addr;
62 remap_can_addr[candev->candev_idx] = remap_can_io_addr;
64 DEBUGMSG("IO-mem for %s: 0x%lx - 0x%lx mapped to 0x%lx\n",
65 candev->hwname, candev->io_addr,
66 candev->io_addr + TSXXX_IO_RANGE - 1, remap_io_addr[candev->candev_idx]);
67 DEBUGMSG("CAN-IO-mem for %s: 0x%lx - 0x%lx mapped to 0x%lx\n",
68 candev->hwname, canio[candev->candev_idx],
69 canio[candev->candev_idx] + TSXXX_CAN_RANGE - 1, candev->dev_base_addr);
75 * tscan1_release_io - free reserved io memory range
76 * @candev: pointer to candevice/board which releases io
78 * The function tscan1_release_io() is used to free reserved io-memory.
79 * In case you have reserved more io memory, don't forget to free it here.
80 * IO_RANGE is the io-memory range that gets released, please adjust according
81 * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
82 * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
83 * Return Value: The function always returns zero
86 int tscan1_release_io(struct candevice_t *candev)
92 * tscan1_reset - hardware reset routine
93 * @candev: Pointer to candevice/board structure
95 * The function tscan1_reset() is used to give a hardware reset. This is
96 * rather hardware specific so I haven't included example code. Don't forget to
97 * check the reset status of the chip before returning.
98 * Return Value: The function returns zero on success or %-ENODEV on failure
101 int tscan1_reset(struct candevice_t *candev)
103 unsigned short i=0, chipnr;
104 struct canchip_t *chip;
106 DEBUGMSG("Resetting %s hardware ...\n", candev->hwname);
108 for(chipnr=0;chipnr<candev->nr_sja1000_chips;chipnr++) {
109 chip=candev->chip[chipnr];
110 tscan1_write_register(sjaMOD_RM, chip->chip_base_addr+SJAMOD);
112 tscan1_write_register(0x00, chip->chip_base_addr + SJAIER);
115 while (tscan1_read_register(chip->chip_base_addr+SJAMOD)&sjaMOD_RM){
116 if(!i--) return -ENODEV;
118 tscan1_write_register(0, chip->chip_base_addr+SJAMOD);
121 tscan1_write_register(sjaCDR_PELICAN, chip->chip_base_addr+SJACDR);
122 tscan1_write_register(0x00, chip->chip_base_addr + SJAIER);
128 #define RESET_ADDR 0x100
133 * tscan1_init_hw_data - Initialize hardware cards
134 * @candev: Pointer to candevice/board structure
136 * The function tscan1_init_hw_data() is used to initialize the hardware
137 * structure containing information about the installed CAN-board.
138 * %RESET_ADDR represents the io-address of the hardware reset register.
139 * %NR_82527 represents the number of intel 82527 chips on the board.
140 * %NR_SJA1000 represents the number of philips sja1000 chips on the board.
141 * The flags entry can currently only be %CANDEV_PROGRAMMABLE_IRQ to indicate that
142 * the hardware uses programmable interrupts.
143 * Return Value: The function always returns zero
147 int tscan1_init_hw_data(struct candevice_t *candev)
150 unsigned long tsxxx_base_io = 0, io_mapped =0;
153 candev->io_addr = -1;
154 if (!strcmp(hw[candev->candev_idx],"tscan1"))
156 DEBUGMSG("Scanning bus for TS-CAN1 boards...\n");
159 tsxxx_base_io = TSCAN1_BASE_IO + i*TSXXX_IO_RANGE;
160 io_mapped = tscan1_getmappedaddr(TSXXX_BASE_IO + tsxxx_base_io);
161 if (inb(io_mapped+TSXXX_ID0_REG)==TSCAN1_ID0 &&
162 inb(io_mapped+TSXXX_ID1_REG)==TSCAN1_ID1)
166 //if (check_mem_region(tsxxx_base_io, TSXXX_IO_RANGE)) continue;
167 if (io[candev->candev_idx]!=-1 && io[candev->candev_idx]!=tsxxx_base_io)
169 io[candev->candev_idx] = tsxxx_base_io;
170 candev->io_addr = tsxxx_base_io;
171 candev->hwname="tscan1";
172 DEBUGMSG("TS-CAN1 board was found at 0x%lx for driver %d/%s\n", tsxxx_base_io,
173 candev->candev_idx, candev->hwname);
179 if (!strcmp(hw[candev->candev_idx],"ts7kv"))
181 DEBUGMSG("Scanning bus for TS-7KV boards...\n");
184 tsxxx_base_io = TS7KV_BASE_IO + (i-4)*TSXXX_IO_RANGE;
185 io_mapped = tscan1_getmappedaddr(TSXXX_BASE_IO + tsxxx_base_io);
186 if (inb(io_mapped+TSXXX_ID0_REG)==TS7KV_ID0 &&
187 inb(io_mapped+TSXXX_ID1_REG)==TS7KV_ID1)
191 //if (check_mem_region(tsxxx_base_io, TSXXX_IO_RANGE)) continue;
192 if (io[candev->candev_idx]!=-1 && io[candev->candev_idx]!=tsxxx_base_io)
194 io[candev->candev_idx] = tsxxx_base_io;
195 candev->io_addr = tsxxx_base_io;
196 candev->hwname="ts7kv";
197 DEBUGMSG("TS-7KV board was found at 0x%lx for driver %d/%s\n",
198 tsxxx_base_io, candev->candev_idx, candev->hwname);
205 if (candev->io_addr==-1) {
206 DEBUGMSG("No board was found for driver %d/%s.\n", candev->candev_idx, candev->hwname);
210 if (!strcmp(candev->hwname,"tscan1"))
212 if (clock[candev->candev_idx] == -1)
213 clock[candev->candev_idx] = 16000;
214 if (irq[candev->candev_idx] == -1)
216 jmp = inb(io_mapped+TSCAN1_JMP_REG);
217 irq[candev->candev_idx]=0;
218 if (jmp&0x10 && jmp&0x20) irq[candev->candev_idx]=TSXXX_IRQ5;
219 else if (jmp&0x10) irq[candev->candev_idx]=TSXXX_IRQ6;
220 else if (jmp&0x20) irq[candev->candev_idx]=TSXXX_IRQ7;
222 if (canio[candev->candev_idx] == -1)
224 switch(candev->io_addr) {
225 case (TSCAN1_BASE_IO+0*TSXXX_IO_RANGE):
226 canio[candev->candev_idx] = 0x180; break;
227 case (TSCAN1_BASE_IO+1*TSXXX_IO_RANGE):
228 canio[candev->candev_idx] = 0x1A0; break;
229 case (TSCAN1_BASE_IO+2*TSXXX_IO_RANGE):
230 canio[candev->candev_idx] = 0x240; break;
231 case (TSCAN1_BASE_IO+3*TSXXX_IO_RANGE):
232 canio[candev->candev_idx] = 0x280; break;
237 switch(canio[candev->candev_idx]) {
238 case 0x100: mode=0x60; break;
239 case 0x120: mode=0x61; break;
240 case 0x180: mode=0x62; break;
241 case 0x1A0: mode=0x63; break;
242 case 0x200: mode=0x64; break;
243 case 0x240: mode=0x65; break;
244 case 0x280: mode=0x66; break;
245 case 0x320: mode=0x67; break;
246 default: mode=0x60; break;
248 outb(0x00, io_mapped+TSCAN1_WIN_REG);
249 outb(mode, io_mapped+TSCAN1_MOD_REG);
252 else if (!strcmp(candev->hwname,"ts7kv"))
254 if (clock[candev->candev_idx] == -1)
255 clock[candev->candev_idx] = 24000;
256 if (irq[candev->candev_idx] == -1)
258 jmp = inb(io_mapped+TS7KV_JMP_REG);
259 irq[candev->candev_idx]=0;
260 if (jmp&0x10) irq[candev->candev_idx]=TSXXX_IRQ6;
261 if (jmp&0x20) irq[candev->candev_idx]=TSXXX_IRQ7;
263 canio[candev->candev_idx] = ((candev->io_addr>>3)&0x03)*0x20;
266 if (baudrate[candev->candev_idx] == -1)
267 baudrate[candev->candev_idx] = 1000;
269 io[candev->candev_idx] += TSXXX_BASE_IO;
270 candev->io_addr = io[candev->candev_idx];
271 canio[candev->candev_idx] += TSXXX_BASE_IO;
273 DEBUGMSG("Memory region at 0x%lx assigned to sja1000 of driver %d/%s\n",
274 canio[candev->candev_idx], candev->candev_idx, candev->hwname);
276 candev->res_addr=RESET_ADDR;
277 candev->nr_82527_chips=NR_82527;
278 candev->nr_sja1000_chips=NR_SJA1000;
279 candev->nr_all_chips=NR_82527+NR_SJA1000;
280 candev->flags &= ~CANDEV_PROGRAMMABLE_IRQ;
286 * tscan1_init_chip_data - Initialize chips
287 * @candev: Pointer to candevice/board structure
288 * @chipnr: Number of the CAN chip on the hardware card
290 * The function tscan1_init_chip_data() is used to initialize the hardware
291 * structure containing information about the CAN chips.
292 * %CHIP_TYPE represents the type of CAN chip. %CHIP_TYPE can be "i82527" or
294 * The @chip_base_addr entry represents the start of the 'official' memory map
295 * of the installed chip. It's likely that this is the same as the @io_addr
296 * argument supplied at module loading time.
297 * The @clock entry holds the chip clock value in Hz.
298 * The entry @sja_cdr_reg holds hardware specific options for the Clock Divider
299 * register. Options defined in the %sja1000.h file:
300 * %sjaCDR_CLKOUT_MASK, %sjaCDR_CLK_OFF, %sjaCDR_RXINPEN, %sjaCDR_CBP, %sjaCDR_PELICAN
301 * The entry @sja_ocr_reg holds hardware specific options for the Output Control
302 * register. Options defined in the %sja1000.h file:
303 * %sjaOCR_MODE_BIPHASE, %sjaOCR_MODE_TEST, %sjaOCR_MODE_NORMAL, %sjaOCR_MODE_CLOCK,
304 * %sjaOCR_TX0_LH, %sjaOCR_TX1_ZZ.
305 * The entry @int_clk_reg holds hardware specific options for the Clock Out
306 * register. Options defined in the %i82527.h file:
307 * %iCLK_CD0, %iCLK_CD1, %iCLK_CD2, %iCLK_CD3, %iCLK_SL0, %iCLK_SL1.
308 * The entry @int_bus_reg holds hardware specific options for the Bus
309 * Configuration register. Options defined in the %i82527.h file:
310 * %iBUS_DR0, %iBUS_DR1, %iBUS_DT1, %iBUS_POL, %iBUS_CBY.
311 * Return Value: The function always returns zero
314 int tscan1_init_chip_data(struct candevice_t *candev, int chipnr)
316 sja1000p_fill_chipspecops(candev->chip[chipnr]);
318 candev->chip[chipnr]->clock = clock[candev->candev_idx]*1000;
319 candev->chip[chipnr]->int_clk_reg = 0x0;
320 candev->chip[chipnr]->int_bus_reg = 0x0;
321 candev->chip[chipnr]->sja_cdr_reg = sjaCDR_CBP | sjaCDR_CLK_OFF;
322 candev->chip[chipnr]->sja_ocr_reg = sjaOCR_MODE_NORMAL | sjaOCR_TX0_LH;
323 candev->chip[chipnr]->chip_base_addr = candev->io_addr;
329 * tscan1_init_obj_data - Initialize message buffers
330 * @chip: Pointer to chip specific structure
331 * @objnr: Number of the message buffer
333 * The function tscan1_init_obj_data() is used to initialize the hardware
334 * structure containing information about the different message objects on the
335 * CAN chip. In case of the sja1000 there's only one message object but on the
336 * i82527 chip there are 15.
337 * The code below is for a i82527 chip and initializes the object base addresses
338 * The entry @obj_base_addr represents the first memory address of the message
339 * object. In case of the sja1000 @obj_base_addr is taken the same as the chips
341 * Unless the hardware uses a segmented memory map, flags can be set zero.
342 * Return Value: The function always returns zero
345 int tscan1_init_obj_data(struct canchip_t *chip, int objnr)
347 chip->msgobj[objnr]->obj_base_addr = chip->chip_base_addr;
352 * tscan1_program_irq - program interrupts
353 * @candev: Pointer to candevice/board structure
355 * The function tscan1_program_irq() is used for hardware that uses
356 * programmable interrupts. If your hardware doesn't use programmable interrupts
357 * you should not set the @candevices_t->flags entry to %CANDEV_PROGRAMMABLE_IRQ and
358 * leave this function unedited. Again this function is hardware specific so
359 * there's no example code.
360 * Return value: The function returns zero on success or %-ENODEV on failure
363 int tscan1_program_irq(struct candevice_t *candev)
369 * tscan1_write_register - Low level write register routine
370 * @data: data to be written
371 * @address: memory address to write to
373 * The function tscan1_write_register() is used to write to hardware registers
374 * on the CAN chip. You should only have to edit this function if your hardware
375 * uses some specific write process.
376 * Return Value: The function does not return a value
379 void tscan1_write_register(unsigned data, unsigned long address)
381 signed short nwinbak=-1;
382 unsigned long winaddr;
383 address = tscan1_setpage_getaddr(address, &nwinbak, &winaddr);
384 DEBUGMSG("outb(0x%x,0x%lx)\n", data, address);
387 DEBUGMSG("restore win outb(0x%x,0x%lx) \n", nwinbak, winaddr);
388 outb(nwinbak, winaddr);
393 * tscan1_read_register - Low level read register routine
394 * @address: memory address to read from
396 * The function tscan1_read_register() is used to read from hardware registers
397 * on the CAN chip. You should only have to edit this function if your hardware
398 * uses some specific read process.
399 * Return Value: The function returns the value stored in @address
402 unsigned tscan1_read_register(unsigned long address)
405 signed short nwinbak=-1;
406 unsigned long winaddr;
407 address = tscan1_setpage_getaddr(address, &nwinbak, &winaddr);
409 DEBUGMSG("inb(0x%lx) = 0x%x\n", address, data);
411 DEBUGMSG("restore win outb(0x%x,0x%lx) \n", nwinbak, winaddr);
412 outb(nwinbak, winaddr);
417 /* !!! Don't change this function !!! */
418 int tscan1_register(struct hwspecops_t *hwspecops)
420 hwspecops->request_io = tscan1_request_io;
421 hwspecops->release_io = tscan1_release_io;
422 hwspecops->reset = tscan1_reset;
423 hwspecops->init_hw_data = tscan1_init_hw_data;
424 hwspecops->init_chip_data = tscan1_init_chip_data;
425 hwspecops->init_obj_data = tscan1_init_obj_data;
426 hwspecops->write_register = tscan1_write_register;
427 hwspecops->read_register = tscan1_read_register;
428 hwspecops->program_irq = tscan1_program_irq;
432 unsigned long tscan1_getmappedaddr(unsigned long address)
434 return (unsigned long)TS7XXX_IO8_BASE + address;
437 unsigned short tscan1_getcandevidx(unsigned long address)
439 ///?FIXME Consider addresses beyond 32bytes range
441 for (i=0;i<MAX_HW_CARDS;i++)
442 if ( remap_can_addr[i]!=-1 &&
443 (address >= remap_can_addr[i]) &&
444 (address < remap_can_addr[i]+TSXXX_CAN_RANGE) )
449 unsigned long tscan1_setpage_getaddr(unsigned long address,
450 signed short *nwinbak, unsigned long *winaddr)
452 unsigned long offset_addr;
453 unsigned short candev_idx;
456 if ((candev_idx = tscan1_getcandevidx(address)) < 0)
459 offset_addr = address;
462 if (!strcmp(hardware_p->candevice[candev_idx]->hwname,"tscan1")) {
463 while ((offset_addr - remap_can_addr[candev_idx]) > 0x1f) {
467 DEBUGMSG("%s win outb(0x%x,0x%lx) of address 0x%lx\n",
468 hardware_p->candevice[candev_idx]->hwname,
469 nwin, remap_io_addr[candev_idx]|TSCAN1_WIN_REG, address);
470 outb(nwin, remap_io_addr[candev_idx]|TSCAN1_WIN_REG);
473 else if (!strcmp(hardware_p->candevice[candev_idx]->hwname,"ts7kv")) {
474 while ((offset_addr - remap_can_addr[candev_idx]) > 0x1d) {
479 *winaddr = (unsigned long)remap_can_addr[candev_idx]|TS7KV_WIN_REG;
480 *nwinbak = inb(*winaddr);
481 DEBUGMSG("inb(0x%lx)=0x%x bak\n", *winaddr, *nwinbak);
482 if (*nwinbak != (nwin|0x10)) {
483 DEBUGMSG("%s win outb(0x%x,0x%lx) of address 0x%lx\n",
484 hardware_p->candevice[candev_idx]->hwname,
485 nwin|0x10, *winaddr, address);
486 outb(nwin|0x10, *winaddr);
488 if ( 0x10 == ((*nwinbak)&0xF0) ) *nwinbak = -1;