xref: /openbmc/linux/drivers/net/can/sja1000/tscan1.c (revision 20c72589)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * tscan1.c: driver for Technologic Systems TS-CAN1 PC104 boards
4  *
5  * Copyright 2010 Andre B. Oliveira
6  */
7 
8 /* References:
9  * - Getting started with TS-CAN1, Technologic Systems, Feb 2022
10  *	https://docs.embeddedts.com/TS-CAN1
11  */
12 
13 #include <linux/init.h>
14 #include <linux/io.h>
15 #include <linux/ioport.h>
16 #include <linux/isa.h>
17 #include <linux/module.h>
18 #include <linux/netdevice.h>
19 #include "sja1000.h"
20 
21 MODULE_DESCRIPTION("Driver for Technologic Systems TS-CAN1 PC104 boards");
22 MODULE_AUTHOR("Andre B. Oliveira <anbadeol@gmail.com>");
23 MODULE_LICENSE("GPL");
24 
25 /* Maximum number of boards (one in each JP1:JP2 setting of IO address) */
26 #define TSCAN1_MAXDEV 4
27 
28 /* PLD registers address offsets */
29 #define TSCAN1_ID1	0
30 #define TSCAN1_ID2	1
31 #define TSCAN1_VERSION	2
32 #define TSCAN1_LED	3
33 #define TSCAN1_PAGE	4
34 #define TSCAN1_MODE	5
35 #define TSCAN1_JUMPERS	6
36 
37 /* PLD board identifier registers magic values */
38 #define TSCAN1_ID1_VALUE 0xf6
39 #define TSCAN1_ID2_VALUE 0xb9
40 
41 /* PLD mode register SJA1000 IO enable bit */
42 #define TSCAN1_MODE_ENABLE 0x40
43 
44 /* PLD jumpers register bits */
45 #define TSCAN1_JP4 0x10
46 #define TSCAN1_JP5 0x20
47 
48 /* PLD IO base addresses start */
49 #define TSCAN1_PLD_ADDRESS 0x150
50 
51 /* PLD register space size */
52 #define TSCAN1_PLD_SIZE 8
53 
54 /* SJA1000 register space size */
55 #define TSCAN1_SJA1000_SIZE 32
56 
57 /* SJA1000 crystal frequency (16MHz) */
58 #define TSCAN1_SJA1000_XTAL 16000000
59 
60 /* SJA1000 IO base addresses */
61 static const unsigned short tscan1_sja1000_addresses[] = {
62 	0x100, 0x120, 0x180, 0x1a0, 0x200, 0x240, 0x280, 0x320
63 };
64 
65 /* Read SJA1000 register */
tscan1_read(const struct sja1000_priv * priv,int reg)66 static u8 tscan1_read(const struct sja1000_priv *priv, int reg)
67 {
68 	return inb((unsigned long)priv->reg_base + reg);
69 }
70 
71 /* Write SJA1000 register */
tscan1_write(const struct sja1000_priv * priv,int reg,u8 val)72 static void tscan1_write(const struct sja1000_priv *priv, int reg, u8 val)
73 {
74 	outb(val, (unsigned long)priv->reg_base + reg);
75 }
76 
77 /* Probe for a TS-CAN1 board with JP2:JP1 jumper setting ID */
tscan1_probe(struct device * dev,unsigned id)78 static int tscan1_probe(struct device *dev, unsigned id)
79 {
80 	struct net_device *netdev;
81 	struct sja1000_priv *priv;
82 	unsigned long pld_base, sja1000_base;
83 	int irq, i;
84 
85 	pld_base = TSCAN1_PLD_ADDRESS + id * TSCAN1_PLD_SIZE;
86 	if (!request_region(pld_base, TSCAN1_PLD_SIZE, dev_name(dev)))
87 		return -EBUSY;
88 
89 	if (inb(pld_base + TSCAN1_ID1) != TSCAN1_ID1_VALUE ||
90 	    inb(pld_base + TSCAN1_ID2) != TSCAN1_ID2_VALUE) {
91 		release_region(pld_base, TSCAN1_PLD_SIZE);
92 		return -ENODEV;
93 	}
94 
95 	switch (inb(pld_base + TSCAN1_JUMPERS) & (TSCAN1_JP4 | TSCAN1_JP5)) {
96 	case TSCAN1_JP4:
97 		irq = 6;
98 		break;
99 	case TSCAN1_JP5:
100 		irq = 7;
101 		break;
102 	case TSCAN1_JP4 | TSCAN1_JP5:
103 		irq = 5;
104 		break;
105 	default:
106 		dev_err(dev, "invalid JP4:JP5 setting (no IRQ)\n");
107 		release_region(pld_base, TSCAN1_PLD_SIZE);
108 		return -EINVAL;
109 	}
110 
111 	netdev = alloc_sja1000dev(0);
112 	if (!netdev) {
113 		release_region(pld_base, TSCAN1_PLD_SIZE);
114 		return -ENOMEM;
115 	}
116 
117 	dev_set_drvdata(dev, netdev);
118 	SET_NETDEV_DEV(netdev, dev);
119 
120 	netdev->base_addr = pld_base;
121 	netdev->irq = irq;
122 
123 	priv = netdev_priv(netdev);
124 	priv->read_reg = tscan1_read;
125 	priv->write_reg = tscan1_write;
126 	priv->can.clock.freq = TSCAN1_SJA1000_XTAL / 2;
127 	priv->cdr = CDR_CBP | CDR_CLK_OFF;
128 	priv->ocr = OCR_TX0_PUSHPULL;
129 
130 	/* Select the first SJA1000 IO address that is free and that works */
131 	for (i = 0; i < ARRAY_SIZE(tscan1_sja1000_addresses); i++) {
132 		sja1000_base = tscan1_sja1000_addresses[i];
133 		if (!request_region(sja1000_base, TSCAN1_SJA1000_SIZE,
134 								dev_name(dev)))
135 			continue;
136 
137 		/* Set SJA1000 IO base address and enable it */
138 		outb(TSCAN1_MODE_ENABLE | i, pld_base + TSCAN1_MODE);
139 
140 		priv->reg_base = (void __iomem *)sja1000_base;
141 		if (!register_sja1000dev(netdev)) {
142 			/* SJA1000 probe succeeded; turn LED off and return */
143 			outb(0, pld_base + TSCAN1_LED);
144 			netdev_info(netdev, "TS-CAN1 at 0x%lx 0x%lx irq %d\n",
145 						pld_base, sja1000_base, irq);
146 			return 0;
147 		}
148 
149 		/* SJA1000 probe failed; release and try next address */
150 		outb(0, pld_base + TSCAN1_MODE);
151 		release_region(sja1000_base, TSCAN1_SJA1000_SIZE);
152 	}
153 
154 	dev_err(dev, "failed to assign SJA1000 IO address\n");
155 	dev_set_drvdata(dev, NULL);
156 	free_sja1000dev(netdev);
157 	release_region(pld_base, TSCAN1_PLD_SIZE);
158 	return -ENXIO;
159 }
160 
tscan1_remove(struct device * dev,unsigned id)161 static void tscan1_remove(struct device *dev, unsigned id /*unused*/)
162 {
163 	struct net_device *netdev;
164 	struct sja1000_priv *priv;
165 	unsigned long pld_base, sja1000_base;
166 
167 	netdev = dev_get_drvdata(dev);
168 	unregister_sja1000dev(netdev);
169 	dev_set_drvdata(dev, NULL);
170 
171 	priv = netdev_priv(netdev);
172 	pld_base = netdev->base_addr;
173 	sja1000_base = (unsigned long)priv->reg_base;
174 
175 	outb(0, pld_base + TSCAN1_MODE);	/* disable SJA1000 IO space */
176 
177 	release_region(sja1000_base, TSCAN1_SJA1000_SIZE);
178 	release_region(pld_base, TSCAN1_PLD_SIZE);
179 
180 	free_sja1000dev(netdev);
181 }
182 
183 static struct isa_driver tscan1_isa_driver = {
184 	.probe = tscan1_probe,
185 	.remove = tscan1_remove,
186 	.driver = {
187 		.name = "tscan1",
188 	},
189 };
190 
191 module_isa_driver(tscan1_isa_driver, TSCAN1_MAXDEV);
192