hc
2024-11-01 2f529f9b558ca1c1bd74be7437a84e4711743404
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
 * Copyright (C) 2006 Wolfgang Grandegger <wg@grandegger.com>
 *
 * Derived from the PCAN project file driver/src/pcan_pci.c:
 *
 * Copyright (C) 2001-2006  PEAK System-Technik GmbH
 *
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
 
#include <linux/module.h>
#include <linux/ioport.h>
#include <linux/delay.h>
#include <linux/pci.h>
#include <asm/io.h>
 
#include <rtdm/driver.h>
 
/* CAN device profile */
#include <rtdm/can.h>
#include <rtcan_dev.h>
#include <rtcan_raw.h>
#include <rtcan_sja1000.h>
#include <rtcan_sja1000_regs.h>
 
#define RTCAN_DEV_NAME    "rtcan%d"
#define RTCAN_DRV_NAME    "PEAK-PCI-CAN"
 
static char *peak_pci_board_name = "PEAK-PCI";
 
MODULE_AUTHOR("Wolfgang Grandegger <wg@grandegger.com>");
MODULE_DESCRIPTION("RTCAN board driver for PEAK-PCI cards");
MODULE_LICENSE("GPL");
 
struct rtcan_peak_pci
{
    struct pci_dev *pci_dev;
    struct rtcan_device *slave_dev;
    int channel;
    volatile void __iomem *base_addr;
    volatile void __iomem *conf_addr;
};
 
#define PEAK_PCI_CAN_SYS_CLOCK (16000000 / 2)
 
#define PELICAN_SINGLE  (SJA_CDR_CAN_MODE | SJA_CDR_CBP | 0x07 | SJA_CDR_CLK_OFF)
#define PELICAN_MASTER  (SJA_CDR_CAN_MODE | SJA_CDR_CBP | 0x07            )
#define PELICAN_DEFAULT (SJA_CDR_CAN_MODE                                 )
 
#define CHANNEL_SINGLE 0 /* this is a single channel device */
#define CHANNEL_MASTER 1 /* multi channel device, this device is master */
#define CHANNEL_SLAVE  2 /* multi channel device, this is slave */
 
// important PITA registers
#define PITA_ICR         0x00        // interrupt control register
#define PITA_GPIOICR     0x18        // general purpose IO interface control register
#define PITA_MISC        0x1C        // miscellanoes register
 
#define PEAK_PCI_VENDOR_ID      0x001C  // the PCI device and vendor IDs
#define PEAK_PCI_DEVICE_ID      0x0001  // Device ID for PCI and older PCIe cards
#define PEAK_PCIE_DEVICE_ID     0x0003  // Device ID for newer PCIe cards (IPEH-003027)
#define PEAK_CPCI_DEVICE_ID     0x0004  // for nextgen cPCI slot cards
#define PEAK_MPCI_DEVICE_ID     0x0005  // for nextgen miniPCI slot cards
#define PEAK_PC_104P_DEVICE_ID  0x0006  // PCAN-PC/104+ cards
#define PEAK_PCI_104E_DEVICE_ID 0x0007  // PCAN-PCI/104 Express cards
#define PEAK_MPCIE_DEVICE_ID    0x0008  // The miniPCIe slot cards
#define PEAK_PCIE_OEM_ID        0x0009  // PCAN-PCI Express OEM
 
#define PCI_CONFIG_PORT_SIZE 0x1000  // size of the config io-memory
#define PCI_PORT_SIZE        0x0400  // size of a channel io-memory
 
static struct pci_device_id peak_pci_tbl[] = {
   {PEAK_PCI_VENDOR_ID, PEAK_PCI_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
   {PEAK_PCI_VENDOR_ID, PEAK_PCIE_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
   {PEAK_PCI_VENDOR_ID, PEAK_MPCI_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
   {PEAK_PCI_VENDOR_ID, PEAK_MPCIE_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
   {PEAK_PCI_VENDOR_ID, PEAK_PC_104P_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
   {PEAK_PCI_VENDOR_ID, PEAK_PCI_104E_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
   {PEAK_PCI_VENDOR_ID, PEAK_CPCI_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
   {PEAK_PCI_VENDOR_ID, PEAK_PCIE_OEM_ID, PCI_ANY_ID, PCI_ANY_ID,},
   { }
};
MODULE_DEVICE_TABLE (pci, peak_pci_tbl);
 
 
static u8 rtcan_peak_pci_read_reg(struct rtcan_device *dev, int port)
{
    struct rtcan_peak_pci *board = (struct rtcan_peak_pci *)dev->board_priv;
    return readb(board->base_addr + ((unsigned long)port << 2));
}
 
static void rtcan_peak_pci_write_reg(struct rtcan_device *dev, int port, u8 data)
{
    struct rtcan_peak_pci *board = (struct rtcan_peak_pci *)dev->board_priv;
    writeb(data, board->base_addr + ((unsigned long)port << 2));
}
 
static void rtcan_peak_pci_irq_ack(struct rtcan_device *dev)
{
    struct rtcan_peak_pci *board = (struct rtcan_peak_pci *)dev->board_priv;
    u16 pita_icr_low;
 
    /* Select and clear in Pita stored interrupt */
    pita_icr_low = readw(board->conf_addr + PITA_ICR);
    if (board->channel == CHANNEL_SLAVE) {
   if (pita_icr_low & 0x0001)
       writew(0x0001, board->conf_addr + PITA_ICR);
    }
    else {
   if (pita_icr_low & 0x0002)
       writew(0x0002, board->conf_addr + PITA_ICR);
    }
}
 
static void rtcan_peak_pci_del_chan(struct rtcan_device *dev,
                   int init_step)
{
    struct rtcan_peak_pci *board;
    u16 pita_icr_high;
 
    if (!dev)
   return;
 
    board = (struct rtcan_peak_pci *)dev->board_priv;
 
    switch (init_step) {
    case 0:            /* Full cleanup */
   printk("Removing %s %s device %s\n",
          peak_pci_board_name, dev->ctrl_name, dev->name);
   rtcan_sja1000_unregister(dev);
   fallthrough;
    case 5:
   pita_icr_high = readw(board->conf_addr + PITA_ICR + 2);
   if (board->channel == CHANNEL_SLAVE) {
       pita_icr_high &= ~0x0001;
   } else {
       pita_icr_high &= ~0x0002;
   }
   writew(pita_icr_high, board->conf_addr + PITA_ICR + 2);
   fallthrough;
    case 4:
   iounmap((void *)board->base_addr);
   fallthrough;
    case 3:
   if (board->channel != CHANNEL_SLAVE)
       iounmap((void *)board->conf_addr);
   fallthrough;
    case 2:
   rtcan_dev_free(dev);
   fallthrough;
    case 1:
   break;
    }
 
}
 
static int rtcan_peak_pci_add_chan(struct pci_dev *pdev, int channel,
                  struct rtcan_device **master_dev)
{
    struct rtcan_device *dev;
    struct rtcan_sja1000 *chip;
    struct rtcan_peak_pci *board;
    u16 pita_icr_high;
    unsigned long addr;
    int ret, init_step = 1;
 
    dev = rtcan_dev_alloc(sizeof(struct rtcan_sja1000),
             sizeof(struct rtcan_peak_pci));
    if (dev == NULL)
   return -ENOMEM;
    init_step = 2;
 
    chip = (struct rtcan_sja1000 *)dev->priv;
    board = (struct rtcan_peak_pci *)dev->board_priv;
 
    board->pci_dev = pdev;
    board->channel = channel;
 
    if (channel != CHANNEL_SLAVE) {
 
   addr = pci_resource_start(pdev, 0);
   board->conf_addr = ioremap(addr, PCI_CONFIG_PORT_SIZE);
   if (board->conf_addr == 0) {
       ret = -ENODEV;
       goto failure;
   }
   init_step = 3;
 
   /* Set GPIO control register */
   writew(0x0005, board->conf_addr + PITA_GPIOICR + 2);
 
   if (channel == CHANNEL_MASTER)
       writeb(0x00, board->conf_addr + PITA_GPIOICR); /* enable both */
   else
       writeb(0x04, board->conf_addr + PITA_GPIOICR); /* enable single */
 
   writeb(0x05, board->conf_addr + PITA_MISC + 3);  /* toggle reset */
   mdelay(5);
   writeb(0x04, board->conf_addr + PITA_MISC + 3);  /* leave parport mux mode */
    } else {
   struct rtcan_peak_pci *master_board =
       (struct rtcan_peak_pci *)(*master_dev)->board_priv;
   master_board->slave_dev = dev;
   board->conf_addr = master_board->conf_addr;
    }
 
    addr = pci_resource_start(pdev, 1);
    if (channel == CHANNEL_SLAVE)
   addr += 0x400;
 
    board->base_addr = ioremap(addr, PCI_PORT_SIZE);
    if (board->base_addr == 0) {
   ret = -ENODEV;
   goto failure;
    }
    init_step = 4;
 
    dev->board_name = peak_pci_board_name;
 
    chip->read_reg = rtcan_peak_pci_read_reg;
    chip->write_reg = rtcan_peak_pci_write_reg;
    chip->irq_ack = rtcan_peak_pci_irq_ack;
 
    /* Clock frequency in Hz */
    dev->can_sys_clock = PEAK_PCI_CAN_SYS_CLOCK;
 
    /* Output control register */
    chip->ocr = SJA_OCR_MODE_NORMAL | SJA_OCR_TX0_PUSHPULL;
 
    /* Clock divider register */
    if (channel == CHANNEL_MASTER)
   chip->cdr = PELICAN_MASTER;
    else
   chip->cdr = PELICAN_SINGLE;
 
    strncpy(dev->name, RTCAN_DEV_NAME, IFNAMSIZ);
 
    /* Register and setup interrupt handling */
    chip->irq_flags = RTDM_IRQTYPE_SHARED;
    chip->irq_num = pdev->irq;
    pita_icr_high = readw(board->conf_addr + PITA_ICR + 2);
    if (channel == CHANNEL_SLAVE) {
   pita_icr_high |= 0x0001;
    } else {
   pita_icr_high |= 0x0002;
    }
    writew(pita_icr_high, board->conf_addr + PITA_ICR + 2);
    init_step = 5;
 
    printk("%s: base_addr=%p conf_addr=%p irq=%d\n", RTCAN_DRV_NAME,
      board->base_addr, board->conf_addr, chip->irq_num);
 
    /* Register SJA1000 device */
    ret = rtcan_sja1000_register(dev);
    if (ret) {
   printk(KERN_ERR
          "ERROR %d while trying to register SJA1000 device!\n", ret);
   goto failure;
    }
 
    if (channel != CHANNEL_SLAVE)
   *master_dev = dev;
 
    return 0;
 
 failure:
    rtcan_peak_pci_del_chan(dev, init_step);
    return ret;
}
 
static int peak_pci_init_one(struct pci_dev *pdev,
                const struct pci_device_id *ent)
{
    int ret;
    u16 sub_sys_id;
    struct rtcan_device *master_dev = NULL;
 
    if (!rtdm_available())
   return -ENODEV;
 
    printk("%s: initializing device %04x:%04x\n",
      RTCAN_DRV_NAME,  pdev->vendor, pdev->device);
 
    if ((ret = pci_enable_device (pdev)))
   goto failure;
 
    if ((ret = pci_request_regions(pdev, RTCAN_DRV_NAME)))
   goto failure;
 
    if ((ret = pci_read_config_word(pdev, 0x2e, &sub_sys_id)))
   goto failure_cleanup;
 
    /* Enable memory space */
    if ((ret = pci_write_config_word(pdev, 0x04, 2)))
   goto failure_cleanup;
 
    if ((ret = pci_write_config_word(pdev, 0x44, 0)))
   goto failure_cleanup;
 
    if (sub_sys_id > 3) {
   if ((ret = rtcan_peak_pci_add_chan(pdev, CHANNEL_MASTER,
                      &master_dev)))
       goto failure_cleanup;
   if ((ret = rtcan_peak_pci_add_chan(pdev, CHANNEL_SLAVE,
                      &master_dev)))
       goto failure_cleanup;
    } else {
   if ((ret = rtcan_peak_pci_add_chan(pdev, CHANNEL_SINGLE,
                      &master_dev)))
       goto failure_cleanup;
    }
 
    pci_set_drvdata(pdev, master_dev);
    return 0;
 
 failure_cleanup:
    if (master_dev)
   rtcan_peak_pci_del_chan(master_dev, 0);
 
    pci_release_regions(pdev);
 
 failure:
    return ret;
 
}
 
static void peak_pci_remove_one(struct pci_dev *pdev)
{
    struct rtcan_device *dev = pci_get_drvdata(pdev);
    struct rtcan_peak_pci *board = (struct rtcan_peak_pci *)dev->board_priv;
 
    if (board->slave_dev)
   rtcan_peak_pci_del_chan(board->slave_dev, 0);
    rtcan_peak_pci_del_chan(dev, 0);
 
    pci_release_regions(pdev);
    pci_disable_device(pdev);
    pci_set_drvdata(pdev, NULL);
}
 
static struct pci_driver rtcan_peak_pci_driver = {
   .name        = RTCAN_DRV_NAME,
   .id_table    = peak_pci_tbl,
   .probe        = peak_pci_init_one,
   .remove        = peak_pci_remove_one,
};
 
module_pci_driver(rtcan_peak_pci_driver);