]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/tscan1.c
049fb8d3045d7e7a9cb059c5d997f34935c3b055
[lincan.git] / lincan / src / tscan1.c
1 /* tscan1.c
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
8  *
9  * The support for TS-CAN1 and TS-7KV provided by Ronald Gomes
10  * from Technologic Systems <http://www.embeddedarm.com/>
11  */
12
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"
18
19 #include <linux/module.h>
20 #include "../include/modparms.h"
21 #include "../include/devcommon.h"
22
23 #include "../include/tscan1.h"
24
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");
28
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");
32
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};
35
36 unsigned short isused = 0x0;
37
38 /**
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
42  *
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
50  * File: src/tscan1.c
51  */
52 int tscan1_request_io(struct candevice_t *candev)
53 {
54    unsigned long remap_can_io_addr;
55
56         remap_io_addr[candev->candev_idx] = tscan1_getmappedaddr(candev->io_addr);
57         remap_can_io_addr = tscan1_getmappedaddr(canio[candev->candev_idx]);
58
59         candev->dev_base_addr = remap_can_io_addr;
60         candev->chip[0]->chip_base_addr = remap_can_io_addr;
61
62         remap_can_addr[candev->candev_idx] = remap_can_io_addr;
63
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);
70
71         return 0;
72 }
73
74 /**
75  * tscan1_release_io - free reserved io memory range
76  * @candev: pointer to candevice/board which releases io
77  *
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
84  * File: src/tscan1.c
85  */
86 int tscan1_release_io(struct candevice_t *candev)
87 {
88         return 0;
89 }
90
91 /**
92  * tscan1_reset - hardware reset routine
93  * @candev: Pointer to candevice/board structure
94  *
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
99  * File: src/tscan1.c
100  */
101 int tscan1_reset(struct candevice_t *candev)
102 {
103         unsigned short i=0, chipnr;
104         struct canchip_t *chip;
105
106         DEBUGMSG("Resetting %s hardware ...\n", candev->hwname);
107
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);
111                 udelay(1000);
112                 tscan1_write_register(0x00, chip->chip_base_addr + SJAIER);
113                 udelay(1000);
114                 i=20;
115                 while (tscan1_read_register(chip->chip_base_addr+SJAMOD)&sjaMOD_RM){
116                         if(!i--) return -ENODEV;
117                         udelay(1000);
118                         tscan1_write_register(0, chip->chip_base_addr+SJAMOD);
119                 }
120                 udelay(1000);
121                 tscan1_write_register(sjaCDR_PELICAN, chip->chip_base_addr+SJACDR);
122                 tscan1_write_register(0x00, chip->chip_base_addr + SJAIER);
123         }
124
125         return 0;
126 }
127
128 #define RESET_ADDR 0x100
129 #define NR_82527 0
130 #define NR_SJA1000 1
131
132 /**
133  * tscan1_init_hw_data - Initialize hardware cards
134  * @candev: Pointer to candevice/board structure
135  *
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
144  * File: src/tscan1.c
145  */
146
147 int tscan1_init_hw_data(struct candevice_t *candev)
148 {
149         short i, jmp;
150         unsigned long tsxxx_base_io = 0, io_mapped =0;
151         unsigned short mode;
152
153         candev->io_addr = -1;
154         if (!strcmp(hw[candev->candev_idx],"tscan1"))
155         {
156                 DEBUGMSG("Scanning bus for TS-CAN1 boards...\n");
157                 for (i=0;i<4;i++)
158                 {
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)
163                         {
164                                 if (isused & 1<<i)
165                                         continue;
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)
168                                         continue;
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);
174                                 isused |= 1<<i;
175                                 break;
176                         }
177                 }
178         }
179         if (!strcmp(hw[candev->candev_idx],"ts7kv"))
180         {
181                 DEBUGMSG("Scanning bus for TS-7KV boards...\n");
182                 for (i=4;i<8;i++)
183                 {
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)
188                         {
189                                 if (isused & 1<<i)
190                                         continue;
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)
193                                         continue;
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);
199                                 isused |= 1<<i;
200                                 break;
201                         }
202                 }
203         }
204
205         if (candev->io_addr==-1) {
206                 DEBUGMSG("No board was found for driver %d/%s.\n", candev->candev_idx, candev->hwname);
207                 return -1;
208         }
209
210         if (!strcmp(candev->hwname,"tscan1"))
211         {
212                 if (clock[candev->candev_idx] == -1)
213                         clock[candev->candev_idx] = 16000;
214                 if (irq[candev->candev_idx] == -1)
215                 {
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;
221                 }
222                 if (canio[candev->candev_idx] == -1)
223                 {
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;
233                         }
234                 }
235
236                 //Reseting...
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;
247                 }
248                 outb(0x00, io_mapped+TSCAN1_WIN_REG);
249                 outb(mode, io_mapped+TSCAN1_MOD_REG);
250         }
251
252         else if (!strcmp(candev->hwname,"ts7kv"))
253         {
254                 if (clock[candev->candev_idx] == -1)
255                         clock[candev->candev_idx] = 24000;
256                 if (irq[candev->candev_idx] == -1)
257                 {
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;
262                 }
263                 canio[candev->candev_idx] = ((candev->io_addr>>3)&0x03)*0x20;
264         }
265
266         if (baudrate[candev->candev_idx] == -1)
267                 baudrate[candev->candev_idx] = 1000;
268
269         io[candev->candev_idx] += TSXXX_BASE_IO;
270         candev->io_addr = io[candev->candev_idx];
271         canio[candev->candev_idx] += TSXXX_BASE_IO;
272
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);
275
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;
281
282         return 0;
283 }
284
285 /**
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
289  *
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
293  * "sja1000".
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
312  * File: src/tscan1.c
313  */
314 int tscan1_init_chip_data(struct candevice_t *candev, int chipnr)
315 {
316         sja1000p_fill_chipspecops(candev->chip[chipnr]);
317
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;
324
325         return 0;
326 }
327
328 /**
329  * tscan1_init_obj_data - Initialize message buffers
330  * @chip: Pointer to chip specific structure
331  * @objnr: Number of the message buffer
332  *
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
340  * base address.
341  * Unless the hardware uses a segmented memory map, flags can be set zero.
342  * Return Value: The function always returns zero
343  * File: src/tscan1.c
344  */
345 int tscan1_init_obj_data(struct canchip_t *chip, int objnr)
346 {
347         chip->msgobj[objnr]->obj_base_addr = chip->chip_base_addr;
348         return 0;
349 }
350
351 /**
352  * tscan1_program_irq - program interrupts
353  * @candev: Pointer to candevice/board structure
354  *
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
361  * File: src/tscan1.c
362  */
363 int tscan1_program_irq(struct candevice_t *candev)
364 {
365         return 0;
366 }
367
368 /**
369  * tscan1_write_register - Low level write register routine
370  * @data: data to be written
371  * @address: memory address to write to
372  *
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
377  * File: src/tscan1.c
378  */
379 void tscan1_write_register(unsigned data, unsigned long address)
380 {
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);
385         outb(data, address);
386         if (nwinbak!=-1) {
387                 DEBUGMSG("restore win outb(0x%x,0x%lx) \n", nwinbak, winaddr);
388                 outb(nwinbak, winaddr);
389         }
390 }
391
392 /**
393  * tscan1_read_register - Low level read register routine
394  * @address: memory address to read from
395  *
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
400  * File: src/tscan1.c
401  */
402 unsigned tscan1_read_register(unsigned long address)
403 {
404         unsigned short data;
405         signed short nwinbak=-1;
406         unsigned long winaddr;
407         address = tscan1_setpage_getaddr(address, &nwinbak, &winaddr);
408         data = inb(address);
409         DEBUGMSG("inb(0x%lx) = 0x%x\n", address, data);
410         if (nwinbak!=-1) {
411                 DEBUGMSG("restore win outb(0x%x,0x%lx) \n", nwinbak, winaddr);
412                 outb(nwinbak, winaddr);
413         }
414         return data;
415 }
416
417 /* !!! Don't change this function !!! */
418 int tscan1_register(struct hwspecops_t *hwspecops)
419 {
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;
429         return 0;
430 }
431
432 unsigned long tscan1_getmappedaddr(unsigned long address)
433 {
434         return (unsigned long)TS7XXX_IO8_BASE + address;
435 }
436
437 unsigned short tscan1_getcandevidx(unsigned long address)
438 {
439         ///?FIXME Consider addresses beyond 32bytes range
440         unsigned short i;
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) )
445                         return i;
446         return -1;
447 }
448
449 unsigned long tscan1_setpage_getaddr(unsigned long address,
450         signed short *nwinbak, unsigned long *winaddr)
451 {
452         unsigned long offset_addr;
453         unsigned short candev_idx;
454         char nwin;
455
456         if ((candev_idx = tscan1_getcandevidx(address)) < 0)
457                 return address;
458
459         offset_addr = address;
460         nwin = 0x0;
461
462         if (!strcmp(hardware_p->candevice[candev_idx]->hwname,"tscan1")) {
463                 while ((offset_addr - remap_can_addr[candev_idx]) > 0x1f) {
464                         offset_addr -= 0x20;
465                         nwin += 0x1;
466                 }
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);
471         }
472
473         else if (!strcmp(hardware_p->candevice[candev_idx]->hwname,"ts7kv")) {
474                 while ((offset_addr - remap_can_addr[candev_idx]) > 0x1d) {
475                         offset_addr -= 0x10;
476                         nwin += 0x1;
477                 }
478
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);
487                         }
488                 if ( 0x10 == ((*nwinbak)&0xF0) )        *nwinbak = -1;
489         }
490
491         return offset_addr;
492 }