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/sja1000p.h"
18 #include <linux/module.h>
19 #include "../include/modparms.h"
20 #include "../include/devcommon.h"
22 #include "../include/tscan1.h"
24 unsigned long tscanio[MAX_HW_CARDS]={-1,-1,-1,-1,-1,-1,-1,-1};
25 unsigned int tscanio_specified;
27 #if defined(TSXXX_BASE_IO)
28 unsigned long tsxxx_base=TSXXX_BASE_IO;
30 unsigned long tsxxx_base=0;
33 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,12))
34 MODULE_PARM(tscanio, "1-" __MODULE_STRING(MAX_HW_CARDS)"i");
35 MODULE_PARM(tsxxx_base, "1i");
36 #else /* LINUX_VERSION_CODE >= 2,6,12 */
37 module_param_array(tscanio, ulong, &tscanio_specified, 0);
38 module_param(tsxxx_base, ulong, 0);
39 #endif /* LINUX_VERSION_CODE >= 2,6,12 */
41 MODULE_PARM_DESC(tscanio,"TSCAN CAN controller IO address for each board");
42 MODULE_PARM_DESC(tsxxx_base,"The base of the ISA/8-bit IO space for TSxxx CAN peripherals in the system");
44 static unsigned long tscan1_tscanio_ranges[] =
45 { 0x100, 0x120, 0x180, 0x1A0, 0x200, 0x240, 0x280, 0x320};
47 static inline unsigned tscan1_read_pld_register(struct candevice_t *candev, can_ioptr_t address)
49 return candev->hwspecops->read_register(address);
52 static inline void tscan1_write_pld_register(struct candevice_t *candev, unsigned data, can_ioptr_t address)
54 candev->hwspecops->write_register(data, address);
58 * tscan1_request_io: - reserve io or memory range for can board
59 * @candev: pointer to candevice/board which asks for io. Field @io_addr
60 * of @candev is used in most cases to define start of the range
62 * The function tscan1_request_io() is used to reserve the io-memory. If your
63 * hardware uses a dedicated memory range as hardware control registers you
64 * will have to add the code to reserve this memory as well.
65 * %IO_RANGE is the io-memory range that gets reserved, please adjust according
66 * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
67 * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
68 * Return Value: The function returns zero on success or %-ENODEV on failure
71 int tscan1_request_io(struct candevice_t *candev)
73 unsigned long can_io_addr;
74 unsigned long rebased_can_io_addr;
75 can_ioptr_t remap_can_io_addr;
76 can_ioptr_t remap_pld_io_addr;
81 if (!can_request_io_region(candev->io_addr, TSCAN1_IO_RANGE, "tscan1-base")) {
82 CANMSG("Unable to request base IO port: 0x%lx\n", candev->io_addr);
85 DEBUGMSG("Registered base IO port: 0x%lx - 0x%lx\n",
86 candev->io_addr, candev->io_addr+TSCAN1_IO_RANGE-1);
89 can_io_addr = tscanio[candev->candev_idx];
91 if(can_io_addr && (can_io_addr != (unsigned long)-1)) {
92 rebased_can_io_addr = tsxxx_base + can_io_addr;
94 if (!can_request_io_region(rebased_can_io_addr, TSCAN1_CAN_RANGE, "tscan1-can")) {
95 CANMSG("Unable to request CAN IO port: 0x%lx\n", rebased_can_io_addr);
96 can_release_io_region(candev->io_addr, TSCAN1_IO_RANGE);
99 DEBUGMSG("Registered CAN IO port: 0x%lx - 0x%lx\n",
100 rebased_can_io_addr, rebased_can_io_addr+TSCAN1_CAN_RANGE-1);
106 CANMSG("Unable find range for CAN IO port\n");
107 can_release_io_region(candev->io_addr, TSCAN1_IO_RANGE);
111 can_io_addr = tscan1_tscanio_ranges[i];
112 for (j = 0; j < MAX_HW_CARDS; j++) {
113 if(tscanio[j] == can_io_addr) {
121 rebased_can_io_addr = tsxxx_base + can_io_addr;
123 if (can_request_io_region(rebased_can_io_addr, TSCAN1_CAN_RANGE, "tscan1-can"))
127 tscanio[candev->candev_idx] = can_io_addr;
129 DEBUGMSG("Found free range and registered CAN IO port: 0x%lx - 0x%lx\n",
130 rebased_can_io_addr, rebased_can_io_addr+TSCAN1_CAN_RANGE-1);
133 remap_can_io_addr = can_ulong2ioptr(rebased_can_io_addr);
135 candev->chip[0]->chip_base_addr = remap_can_io_addr;
136 candev->chip[0]->msgobj[0]->obj_base_addr = remap_can_io_addr;
139 for(i = 0; i < 8; i++) {
140 if (can_io_addr == tscan1_tscanio_ranges[i]) {
146 remap_pld_io_addr = can_ulong2ioptr(candev->io_addr);
148 tscan1_write_pld_register(candev, 0x00, remap_pld_io_addr+TSCAN1_WIN_REG);
149 tscan1_write_pld_register(candev, mode, remap_pld_io_addr+TSCAN1_MOD_REG);
155 * tscan1_release_io - free reserved io memory range
156 * @candev: pointer to candevice/board which releases io
158 * The function tscan1_release_io() is used to free reserved io-memory.
159 * In case you have reserved more io memory, don't forget to free it here.
160 * IO_RANGE is the io-memory range that gets released, please adjust according
161 * your hardware. Example: #define IO_RANGE 0x100 for i82527 chips or
162 * #define IO_RANGE 0x20 for sja1000 chips in basic CAN mode.
163 * Return Value: The function always returns zero
166 int tscan1_release_io(struct candevice_t *candev)
168 unsigned long rebased_can_io_addr;
169 can_ioptr_t remap_pld_io_addr = can_ulong2ioptr(candev->io_addr);
172 rebased_can_io_addr = candev->chip[0]->chip_base_addr;
173 if(rebased_can_io_addr != (unsigned long)-1)
174 can_release_io_region(rebased_can_io_addr, TSCAN1_CAN_RANGE);
177 tscan1_write_pld_register(candev, 0x20, remap_pld_io_addr+TSCAN1_MOD_REG);
179 can_release_io_region(candev->io_addr, TSCAN1_IO_RANGE);
184 * tscan1_reset - hardware reset routine
185 * @candev: Pointer to candevice/board structure
187 * The function tscan1_reset() is used to give a hardware reset. This is
188 * rather hardware specific so I haven't included example code. Don't forget to
189 * check the reset status of the chip before returning.
190 * Return Value: The function returns zero on success or %-ENODEV on failure
193 int tscan1_reset(struct candevice_t *candev)
195 unsigned short i=0, chipnr;
196 struct canchip_t *chip;
198 DEBUGMSG("Resetting %s hardware ...\n", candev->hwname);
200 for(chipnr=0;chipnr<candev->nr_sja1000_chips;chipnr++) {
201 chip=candev->chip[chipnr];
202 can_write_reg(chip, sjaMOD_RM, SJAMOD);
204 can_write_reg(chip, 0x00, SJAIER);
207 while (can_read_reg(chip, SJAMOD)&sjaMOD_RM){
208 if(!i--) return -ENODEV;
210 can_write_reg(chip, 0, SJAMOD);
213 can_write_reg(chip, sjaCDR_PELICAN, SJACDR);
214 can_write_reg(chip, 0x00, SJAIER);
220 #define RESET_ADDR 0x100
224 int tscan1_check_presence(struct candevice_t *candev, unsigned long rebased_pld_io_addr, int *pjmp)
226 can_ioptr_t remap_pld_io_addr;
227 int result = -ENODEV;
229 if (!can_request_io_region(rebased_pld_io_addr, TSCAN1_IO_RANGE, "tscan1-probe"))
233 remap_pld_io_addr = can_ulong2ioptr(rebased_pld_io_addr);
235 if (tscan1_read_pld_register(candev, remap_pld_io_addr+TSCAN1_ID0_REG)!=TSCAN1_ID0 ||
236 tscan1_read_pld_register(candev, remap_pld_io_addr+TSCAN1_ID1_REG)!=TSCAN1_ID1)
239 tscan1_write_pld_register(candev, 0x00, remap_pld_io_addr+TSCAN1_WIN_REG);
240 tscan1_write_pld_register(candev, 0x20, remap_pld_io_addr+TSCAN1_MOD_REG);
243 *pjmp = tscan1_read_pld_register(candev, remap_pld_io_addr+TSCAN1_JMP_REG);
248 can_release_io_region(rebased_pld_io_addr, TSCAN1_IO_RANGE);
255 * tscan1_init_hw_data - Initialize hardware cards
256 * @candev: Pointer to candevice/board structure
258 * The function tscan1_init_hw_data() is used to initialize the hardware
259 * structure containing information about the installed CAN-board.
260 * %RESET_ADDR represents the io-address of the hardware reset register.
261 * %NR_82527 represents the number of intel 82527 chips on the board.
262 * %NR_SJA1000 represents the number of philips sja1000 chips on the board.
263 * The flags entry can currently only be %CANDEV_PROGRAMMABLE_IRQ to indicate that
264 * the hardware uses programmable interrupts.
265 * Return Value: The function always returns zero
269 int tscan1_init_hw_data(struct candevice_t *candev)
272 unsigned long io_addr;
273 unsigned long rebased_pld_io_addr;
275 io_addr = candev->io_addr;
277 if(io_addr && (io_addr != (unsigned long)-1)) {
278 rebased_pld_io_addr = io_addr + tsxxx_base;
280 if(tscan1_check_presence(candev, rebased_pld_io_addr, &jmp)){
281 CANMSG("No TSCAN1 card found at address 0x%lx\n",
282 rebased_pld_io_addr);
286 DEBUGMSG("Scanning bus for TS-CAN1 boards...\n");
291 CANMSG("No TS-CAN1 boards found for slot %d\n", candev->candev_idx);
295 io_addr = TSCAN1_BASE_IO + i*TSCAN1_IO_RANGE;
296 rebased_pld_io_addr = io_addr + tsxxx_base;
298 for (j = 0; j < MAX_HW_CARDS; j++) {
299 if(io[j] == io_addr){
307 if(!tscan1_check_presence(candev, rebased_pld_io_addr, &jmp))
311 DEBUGMSG("TS-CAN1 board was found at 0x%lx for driver slot %d\n",
312 io_addr, candev->candev_idx);
314 io[candev->candev_idx] = io_addr;
317 candev->io_addr = rebased_pld_io_addr;
318 /* unused reset address is used to store jumper setting */
319 candev->res_addr = jmp;
321 candev->nr_82527_chips=NR_82527;
322 candev->nr_sja1000_chips=NR_SJA1000;
323 candev->nr_all_chips=NR_82527+NR_SJA1000;
324 candev->flags &= ~CANDEV_PROGRAMMABLE_IRQ;
326 DEBUGMSG("Memory region at 0x%lx assigned to sja1000 of driver %d/%s\n",
327 candev->io_addr, candev->candev_idx, candev->hwname);
334 * tscan1_init_chip_data - Initialize chips
335 * @candev: Pointer to candevice/board structure
336 * @chipnr: Number of the CAN chip on the hardware card
338 * The function tscan1_init_chip_data() is used to initialize the hardware
339 * structure containing information about the CAN chips.
340 * %CHIP_TYPE represents the type of CAN chip. %CHIP_TYPE can be "i82527" or
342 * The @chip_base_addr entry represents the start of the 'official' memory map
343 * of the installed chip. It's likely that this is the same as the @io_addr
344 * argument supplied at module loading time.
345 * The @clock entry holds the chip clock value in Hz.
346 * The entry @sja_cdr_reg holds hardware specific options for the Clock Divider
347 * register. Options defined in the %sja1000.h file:
348 * %sjaCDR_CLKOUT_MASK, %sjaCDR_CLK_OFF, %sjaCDR_RXINPEN, %sjaCDR_CBP, %sjaCDR_PELICAN
349 * The entry @sja_ocr_reg holds hardware specific options for the Output Control
350 * register. Options defined in the %sja1000.h file:
351 * %sjaOCR_MODE_BIPHASE, %sjaOCR_MODE_TEST, %sjaOCR_MODE_NORMAL, %sjaOCR_MODE_CLOCK,
352 * %sjaOCR_TX0_LH, %sjaOCR_TX1_ZZ.
353 * The entry @int_clk_reg holds hardware specific options for the Clock Out
354 * register. Options defined in the %i82527.h file:
355 * %iCLK_CD0, %iCLK_CD1, %iCLK_CD2, %iCLK_CD3, %iCLK_SL0, %iCLK_SL1.
356 * The entry @int_bus_reg holds hardware specific options for the Bus
357 * Configuration register. Options defined in the %i82527.h file:
358 * %iBUS_DR0, %iBUS_DR1, %iBUS_DT1, %iBUS_POL, %iBUS_CBY.
359 * Return Value: The function always returns zero
362 int tscan1_init_chip_data(struct candevice_t *candev, int chipnr)
364 unsigned long default_clk = 16000 * 1000;
368 /* unused reset address is used to store jumper setting */
369 jmp = candev->res_addr;
371 if (jmp&0x10 && jmp&0x20) irq=TSXXX_IRQ5;
372 else if (jmp&0x10) irq=TSXXX_IRQ6;
373 else if (jmp&0x20) irq=TSXXX_IRQ7;
376 CANMSG("Jumpers select no IRQ for TSCAN1 at 0x%lx of driver %d/%s\n",
377 candev->io_addr, candev->candev_idx, candev->hwname);
380 candev->chip[chipnr]->chip_irq = irq;
382 sja1000p_fill_chipspecops(candev->chip[chipnr]);
384 if(candev->chip[chipnr]->clock <= 0)
385 candev->chip[chipnr]->clock = default_clk;
386 candev->chip[chipnr]->int_clk_reg = 0x0;
387 candev->chip[chipnr]->int_bus_reg = 0x0;
388 candev->chip[chipnr]->sja_cdr_reg = sjaCDR_CBP | sjaCDR_CLK_OFF;
389 candev->chip[chipnr]->sja_ocr_reg = sjaOCR_MODE_NORMAL | sjaOCR_TX0_LH;
391 * The address is assigned during tscan1_request_io()
392 * according to found free ranges or tscanio option
394 candev->chip[chipnr]->chip_base_addr = (unsigned long)-1;
400 * tscan1_init_obj_data - Initialize message buffers
401 * @chip: Pointer to chip specific structure
402 * @objnr: Number of the message buffer
404 * The function tscan1_init_obj_data() is used to initialize the hardware
405 * structure containing information about the different message objects on the
406 * CAN chip. In case of the sja1000 there's only one message object but on the
407 * i82527 chip there are 15.
408 * The code below is for a i82527 chip and initializes the object base addresses
409 * The entry @obj_base_addr represents the first memory address of the message
410 * object. In case of the sja1000 @obj_base_addr is taken the same as the chips
412 * Unless the hardware uses a segmented memory map, flags can be set zero.
413 * Return Value: The function always returns zero
416 int tscan1_init_obj_data(struct canchip_t *chip, int objnr)
418 chip->msgobj[objnr]->obj_base_addr = chip->chip_base_addr;
423 * tscan1_program_irq - program interrupts
424 * @candev: Pointer to candevice/board structure
426 * The function tscan1_program_irq() is used for hardware that uses
427 * programmable interrupts. If your hardware doesn't use programmable interrupts
428 * you should not set the @candevices_t->flags entry to %CANDEV_PROGRAMMABLE_IRQ and
429 * leave this function unedited. Again this function is hardware specific so
430 * there's no example code.
431 * Return value: The function returns zero on success or %-ENODEV on failure
434 int tscan1_program_irq(struct candevice_t *candev)
440 * tscan1_write_register - Low level write register routine
441 * @data: data to be written
442 * @address: memory address to write to
444 * The function tscan1_write_register() is used to write to hardware registers
445 * on the CAN chip. You should only have to edit this function if your hardware
446 * uses some specific write process.
447 * Return Value: The function does not return a value
450 void tscan1_write_register(unsigned data, can_ioptr_t address)
452 can_outb(data, address);
455 void tscan1mmio_write_register(unsigned data, can_ioptr_t address)
457 can_writeb(data, address);
462 * tscan1_read_register - Low level read register routine
463 * @address: memory address to read from
465 * The function tscan1_read_register() is used to read from hardware registers
466 * on the CAN chip. You should only have to edit this function if your hardware
467 * uses some specific read process.
468 * Return Value: The function returns the value stored in @address
471 unsigned tscan1_read_register(can_ioptr_t address)
473 return can_inb(address);
476 unsigned tscan1mmio_read_register(can_ioptr_t address)
478 return can_readb(address);
481 int tscan1_register(struct hwspecops_t *hwspecops)
483 hwspecops->request_io = tscan1_request_io;
484 hwspecops->release_io = tscan1_release_io;
485 hwspecops->reset = tscan1_reset;
486 hwspecops->init_hw_data = tscan1_init_hw_data;
487 hwspecops->init_chip_data = tscan1_init_chip_data;
488 hwspecops->init_obj_data = tscan1_init_obj_data;
489 hwspecops->write_register = tscan1_write_register;
490 hwspecops->read_register = tscan1_read_register;
491 hwspecops->program_irq = tscan1_program_irq;
495 int tscan1mmio_register(struct hwspecops_t *hwspecops)
497 hwspecops->request_io = tscan1_request_io;
498 hwspecops->release_io = tscan1_release_io;
499 hwspecops->reset = tscan1_reset;
500 hwspecops->init_hw_data = tscan1_init_hw_data;
501 hwspecops->init_chip_data = tscan1_init_chip_data;
502 hwspecops->init_obj_data = tscan1_init_obj_data;
503 hwspecops->write_register = tscan1mmio_write_register;
504 hwspecops->read_register = tscan1mmio_read_register;
505 hwspecops->program_irq = tscan1_program_irq;