1 // SPDX-License-Identifier: GPL-2.0
2 // CAN bus driver for Bosch M_CAN controller
3 // Copyright (C) 2014 Freescale Semiconductor, Inc.
4 // Dong Aisheng <b29396@freescale.com>
5 // Copyright (C) 2018-19 Texas Instruments Incorporated - http://www.ti.com/
6
7 /* Bosch M_CAN user manual can be obtained from:
8 * https://github.com/linux-can/can-doc/tree/master/m_can
9 */
10
11 #include <linux/bitfield.h>
12 #include <linux/interrupt.h>
13 #include <linux/io.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/netdevice.h>
17 #include <linux/of.h>
18 #include <linux/of_device.h>
19 #include <linux/platform_device.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/iopoll.h>
22 #include <linux/can/dev.h>
23 #include <linux/pinctrl/consumer.h>
24 #include <linux/phy/phy.h>
25
26 #include "m_can.h"
27
28 /* registers definition */
29 enum m_can_reg {
30 M_CAN_CREL = 0x0,
31 M_CAN_ENDN = 0x4,
32 M_CAN_CUST = 0x8,
33 M_CAN_DBTP = 0xc,
34 M_CAN_TEST = 0x10,
35 M_CAN_RWD = 0x14,
36 M_CAN_CCCR = 0x18,
37 M_CAN_NBTP = 0x1c,
38 M_CAN_TSCC = 0x20,
39 M_CAN_TSCV = 0x24,
40 M_CAN_TOCC = 0x28,
41 M_CAN_TOCV = 0x2c,
42 M_CAN_ECR = 0x40,
43 M_CAN_PSR = 0x44,
44 /* TDCR Register only available for version >=3.1.x */
45 M_CAN_TDCR = 0x48,
46 M_CAN_IR = 0x50,
47 M_CAN_IE = 0x54,
48 M_CAN_ILS = 0x58,
49 M_CAN_ILE = 0x5c,
50 M_CAN_GFC = 0x80,
51 M_CAN_SIDFC = 0x84,
52 M_CAN_XIDFC = 0x88,
53 M_CAN_XIDAM = 0x90,
54 M_CAN_HPMS = 0x94,
55 M_CAN_NDAT1 = 0x98,
56 M_CAN_NDAT2 = 0x9c,
57 M_CAN_RXF0C = 0xa0,
58 M_CAN_RXF0S = 0xa4,
59 M_CAN_RXF0A = 0xa8,
60 M_CAN_RXBC = 0xac,
61 M_CAN_RXF1C = 0xb0,
62 M_CAN_RXF1S = 0xb4,
63 M_CAN_RXF1A = 0xb8,
64 M_CAN_RXESC = 0xbc,
65 M_CAN_TXBC = 0xc0,
66 M_CAN_TXFQS = 0xc4,
67 M_CAN_TXESC = 0xc8,
68 M_CAN_TXBRP = 0xcc,
69 M_CAN_TXBAR = 0xd0,
70 M_CAN_TXBCR = 0xd4,
71 M_CAN_TXBTO = 0xd8,
72 M_CAN_TXBCF = 0xdc,
73 M_CAN_TXBTIE = 0xe0,
74 M_CAN_TXBCIE = 0xe4,
75 M_CAN_TXEFC = 0xf0,
76 M_CAN_TXEFS = 0xf4,
77 M_CAN_TXEFA = 0xf8,
78 };
79
80 /* napi related */
81 #define M_CAN_NAPI_WEIGHT 64
82
83 /* message ram configuration data length */
84 #define MRAM_CFG_LEN 8
85
86 /* Core Release Register (CREL) */
87 #define CREL_REL_MASK GENMASK(31, 28)
88 #define CREL_STEP_MASK GENMASK(27, 24)
89 #define CREL_SUBSTEP_MASK GENMASK(23, 20)
90
91 /* Data Bit Timing & Prescaler Register (DBTP) */
92 #define DBTP_TDC BIT(23)
93 #define DBTP_DBRP_MASK GENMASK(20, 16)
94 #define DBTP_DTSEG1_MASK GENMASK(12, 8)
95 #define DBTP_DTSEG2_MASK GENMASK(7, 4)
96 #define DBTP_DSJW_MASK GENMASK(3, 0)
97
98 /* Transmitter Delay Compensation Register (TDCR) */
99 #define TDCR_TDCO_MASK GENMASK(14, 8)
100 #define TDCR_TDCF_MASK GENMASK(6, 0)
101
102 /* Test Register (TEST) */
103 #define TEST_LBCK BIT(4)
104
105 /* CC Control Register (CCCR) */
106 #define CCCR_TXP BIT(14)
107 #define CCCR_TEST BIT(7)
108 #define CCCR_DAR BIT(6)
109 #define CCCR_MON BIT(5)
110 #define CCCR_CSR BIT(4)
111 #define CCCR_CSA BIT(3)
112 #define CCCR_ASM BIT(2)
113 #define CCCR_CCE BIT(1)
114 #define CCCR_INIT BIT(0)
115 /* for version 3.0.x */
116 #define CCCR_CMR_MASK GENMASK(11, 10)
117 #define CCCR_CMR_CANFD 0x1
118 #define CCCR_CMR_CANFD_BRS 0x2
119 #define CCCR_CMR_CAN 0x3
120 #define CCCR_CME_MASK GENMASK(9, 8)
121 #define CCCR_CME_CAN 0
122 #define CCCR_CME_CANFD 0x1
123 #define CCCR_CME_CANFD_BRS 0x2
124 /* for version >=3.1.x */
125 #define CCCR_EFBI BIT(13)
126 #define CCCR_PXHD BIT(12)
127 #define CCCR_BRSE BIT(9)
128 #define CCCR_FDOE BIT(8)
129 /* for version >=3.2.x */
130 #define CCCR_NISO BIT(15)
131 /* for version >=3.3.x */
132 #define CCCR_WMM BIT(11)
133 #define CCCR_UTSU BIT(10)
134
135 /* Nominal Bit Timing & Prescaler Register (NBTP) */
136 #define NBTP_NSJW_MASK GENMASK(31, 25)
137 #define NBTP_NBRP_MASK GENMASK(24, 16)
138 #define NBTP_NTSEG1_MASK GENMASK(15, 8)
139 #define NBTP_NTSEG2_MASK GENMASK(6, 0)
140
141 /* Timestamp Counter Configuration Register (TSCC) */
142 #define TSCC_TCP_MASK GENMASK(19, 16)
143 #define TSCC_TSS_MASK GENMASK(1, 0)
144 #define TSCC_TSS_DISABLE 0x0
145 #define TSCC_TSS_INTERNAL 0x1
146 #define TSCC_TSS_EXTERNAL 0x2
147
148 /* Timestamp Counter Value Register (TSCV) */
149 #define TSCV_TSC_MASK GENMASK(15, 0)
150
151 /* Error Counter Register (ECR) */
152 #define ECR_RP BIT(15)
153 #define ECR_REC_MASK GENMASK(14, 8)
154 #define ECR_TEC_MASK GENMASK(7, 0)
155
156 /* Protocol Status Register (PSR) */
157 #define PSR_BO BIT(7)
158 #define PSR_EW BIT(6)
159 #define PSR_EP BIT(5)
160 #define PSR_LEC_MASK GENMASK(2, 0)
161
162 /* Interrupt Register (IR) */
163 #define IR_ALL_INT 0xffffffff
164
165 /* Renamed bits for versions > 3.1.x */
166 #define IR_ARA BIT(29)
167 #define IR_PED BIT(28)
168 #define IR_PEA BIT(27)
169
170 /* Bits for version 3.0.x */
171 #define IR_STE BIT(31)
172 #define IR_FOE BIT(30)
173 #define IR_ACKE BIT(29)
174 #define IR_BE BIT(28)
175 #define IR_CRCE BIT(27)
176 #define IR_WDI BIT(26)
177 #define IR_BO BIT(25)
178 #define IR_EW BIT(24)
179 #define IR_EP BIT(23)
180 #define IR_ELO BIT(22)
181 #define IR_BEU BIT(21)
182 #define IR_BEC BIT(20)
183 #define IR_DRX BIT(19)
184 #define IR_TOO BIT(18)
185 #define IR_MRAF BIT(17)
186 #define IR_TSW BIT(16)
187 #define IR_TEFL BIT(15)
188 #define IR_TEFF BIT(14)
189 #define IR_TEFW BIT(13)
190 #define IR_TEFN BIT(12)
191 #define IR_TFE BIT(11)
192 #define IR_TCF BIT(10)
193 #define IR_TC BIT(9)
194 #define IR_HPM BIT(8)
195 #define IR_RF1L BIT(7)
196 #define IR_RF1F BIT(6)
197 #define IR_RF1W BIT(5)
198 #define IR_RF1N BIT(4)
199 #define IR_RF0L BIT(3)
200 #define IR_RF0F BIT(2)
201 #define IR_RF0W BIT(1)
202 #define IR_RF0N BIT(0)
203 #define IR_ERR_STATE (IR_BO | IR_EW | IR_EP)
204
205 /* Interrupts for version 3.0.x */
206 #define IR_ERR_LEC_30X (IR_STE | IR_FOE | IR_ACKE | IR_BE | IR_CRCE)
207 #define IR_ERR_BUS_30X (IR_ERR_LEC_30X | IR_WDI | IR_BEU | IR_BEC | \
208 IR_TOO | IR_MRAF | IR_TSW | IR_TEFL | IR_RF1L | \
209 IR_RF0L)
210 #define IR_ERR_ALL_30X (IR_ERR_STATE | IR_ERR_BUS_30X)
211
212 /* Interrupts for version >= 3.1.x */
213 #define IR_ERR_LEC_31X (IR_PED | IR_PEA)
214 #define IR_ERR_BUS_31X (IR_ERR_LEC_31X | IR_WDI | IR_BEU | IR_BEC | \
215 IR_TOO | IR_MRAF | IR_TSW | IR_TEFL | IR_RF1L | \
216 IR_RF0L)
217 #define IR_ERR_ALL_31X (IR_ERR_STATE | IR_ERR_BUS_31X)
218
219 /* Interrupt Line Select (ILS) */
220 #define ILS_ALL_INT0 0x0
221 #define ILS_ALL_INT1 0xFFFFFFFF
222
223 /* Interrupt Line Enable (ILE) */
224 #define ILE_EINT1 BIT(1)
225 #define ILE_EINT0 BIT(0)
226
227 /* Rx FIFO 0/1 Configuration (RXF0C/RXF1C) */
228 #define RXFC_FWM_MASK GENMASK(30, 24)
229 #define RXFC_FS_MASK GENMASK(22, 16)
230
231 /* Rx FIFO 0/1 Status (RXF0S/RXF1S) */
232 #define RXFS_RFL BIT(25)
233 #define RXFS_FF BIT(24)
234 #define RXFS_FPI_MASK GENMASK(21, 16)
235 #define RXFS_FGI_MASK GENMASK(13, 8)
236 #define RXFS_FFL_MASK GENMASK(6, 0)
237
238 /* Rx Buffer / FIFO Element Size Configuration (RXESC) */
239 #define RXESC_RBDS_MASK GENMASK(10, 8)
240 #define RXESC_F1DS_MASK GENMASK(6, 4)
241 #define RXESC_F0DS_MASK GENMASK(2, 0)
242 #define RXESC_64B 0x7
243
244 /* Tx Buffer Configuration (TXBC) */
245 #define TXBC_TFQS_MASK GENMASK(29, 24)
246 #define TXBC_NDTB_MASK GENMASK(21, 16)
247
248 /* Tx FIFO/Queue Status (TXFQS) */
249 #define TXFQS_TFQF BIT(21)
250 #define TXFQS_TFQPI_MASK GENMASK(20, 16)
251 #define TXFQS_TFGI_MASK GENMASK(12, 8)
252 #define TXFQS_TFFL_MASK GENMASK(5, 0)
253
254 /* Tx Buffer Element Size Configuration (TXESC) */
255 #define TXESC_TBDS_MASK GENMASK(2, 0)
256 #define TXESC_TBDS_64B 0x7
257
258 /* Tx Event FIFO Configuration (TXEFC) */
259 #define TXEFC_EFS_MASK GENMASK(21, 16)
260
261 /* Tx Event FIFO Status (TXEFS) */
262 #define TXEFS_TEFL BIT(25)
263 #define TXEFS_EFF BIT(24)
264 #define TXEFS_EFGI_MASK GENMASK(12, 8)
265 #define TXEFS_EFFL_MASK GENMASK(5, 0)
266
267 /* Tx Event FIFO Acknowledge (TXEFA) */
268 #define TXEFA_EFAI_MASK GENMASK(4, 0)
269
270 /* Message RAM Configuration (in bytes) */
271 #define SIDF_ELEMENT_SIZE 4
272 #define XIDF_ELEMENT_SIZE 8
273 #define RXF0_ELEMENT_SIZE 72
274 #define RXF1_ELEMENT_SIZE 72
275 #define RXB_ELEMENT_SIZE 72
276 #define TXE_ELEMENT_SIZE 8
277 #define TXB_ELEMENT_SIZE 72
278
279 /* Message RAM Elements */
280 #define M_CAN_FIFO_ID 0x0
281 #define M_CAN_FIFO_DLC 0x4
282 #define M_CAN_FIFO_DATA 0x8
283
284 /* Rx Buffer Element */
285 /* R0 */
286 #define RX_BUF_ESI BIT(31)
287 #define RX_BUF_XTD BIT(30)
288 #define RX_BUF_RTR BIT(29)
289 /* R1 */
290 #define RX_BUF_ANMF BIT(31)
291 #define RX_BUF_FDF BIT(21)
292 #define RX_BUF_BRS BIT(20)
293 #define RX_BUF_RXTS_MASK GENMASK(15, 0)
294
295 /* Tx Buffer Element */
296 /* T0 */
297 #define TX_BUF_ESI BIT(31)
298 #define TX_BUF_XTD BIT(30)
299 #define TX_BUF_RTR BIT(29)
300 /* T1 */
301 #define TX_BUF_EFC BIT(23)
302 #define TX_BUF_FDF BIT(21)
303 #define TX_BUF_BRS BIT(20)
304 #define TX_BUF_MM_MASK GENMASK(31, 24)
305 #define TX_BUF_DLC_MASK GENMASK(19, 16)
306
307 /* Tx event FIFO Element */
308 /* E1 */
309 #define TX_EVENT_MM_MASK GENMASK(31, 24)
310 #define TX_EVENT_TXTS_MASK GENMASK(15, 0)
311
312 /* The ID and DLC registers are adjacent in M_CAN FIFO memory,
313 * and we can save a (potentially slow) bus round trip by combining
314 * reads and writes to them.
315 */
316 struct id_and_dlc {
317 u32 id;
318 u32 dlc;
319 };
320
m_can_read(struct m_can_classdev * cdev,enum m_can_reg reg)321 static inline u32 m_can_read(struct m_can_classdev *cdev, enum m_can_reg reg)
322 {
323 return cdev->ops->read_reg(cdev, reg);
324 }
325
m_can_write(struct m_can_classdev * cdev,enum m_can_reg reg,u32 val)326 static inline void m_can_write(struct m_can_classdev *cdev, enum m_can_reg reg,
327 u32 val)
328 {
329 cdev->ops->write_reg(cdev, reg, val);
330 }
331
332 static int
m_can_fifo_read(struct m_can_classdev * cdev,u32 fgi,unsigned int offset,void * val,size_t val_count)333 m_can_fifo_read(struct m_can_classdev *cdev,
334 u32 fgi, unsigned int offset, void *val, size_t val_count)
335 {
336 u32 addr_offset = cdev->mcfg[MRAM_RXF0].off + fgi * RXF0_ELEMENT_SIZE +
337 offset;
338
339 if (val_count == 0)
340 return 0;
341
342 return cdev->ops->read_fifo(cdev, addr_offset, val, val_count);
343 }
344
345 static int
m_can_fifo_write(struct m_can_classdev * cdev,u32 fpi,unsigned int offset,const void * val,size_t val_count)346 m_can_fifo_write(struct m_can_classdev *cdev,
347 u32 fpi, unsigned int offset, const void *val, size_t val_count)
348 {
349 u32 addr_offset = cdev->mcfg[MRAM_TXB].off + fpi * TXB_ELEMENT_SIZE +
350 offset;
351
352 if (val_count == 0)
353 return 0;
354
355 return cdev->ops->write_fifo(cdev, addr_offset, val, val_count);
356 }
357
m_can_fifo_write_no_off(struct m_can_classdev * cdev,u32 fpi,u32 val)358 static inline int m_can_fifo_write_no_off(struct m_can_classdev *cdev,
359 u32 fpi, u32 val)
360 {
361 return cdev->ops->write_fifo(cdev, fpi, &val, 1);
362 }
363
364 static int
m_can_txe_fifo_read(struct m_can_classdev * cdev,u32 fgi,u32 offset,u32 * val)365 m_can_txe_fifo_read(struct m_can_classdev *cdev, u32 fgi, u32 offset, u32 *val)
366 {
367 u32 addr_offset = cdev->mcfg[MRAM_TXE].off + fgi * TXE_ELEMENT_SIZE +
368 offset;
369
370 return cdev->ops->read_fifo(cdev, addr_offset, val, 1);
371 }
372
m_can_tx_fifo_full(struct m_can_classdev * cdev)373 static inline bool m_can_tx_fifo_full(struct m_can_classdev *cdev)
374 {
375 return !!(m_can_read(cdev, M_CAN_TXFQS) & TXFQS_TFQF);
376 }
377
m_can_config_endisable(struct m_can_classdev * cdev,bool enable)378 static void m_can_config_endisable(struct m_can_classdev *cdev, bool enable)
379 {
380 u32 cccr = m_can_read(cdev, M_CAN_CCCR);
381 u32 timeout = 10;
382 u32 val = 0;
383
384 /* Clear the Clock stop request if it was set */
385 if (cccr & CCCR_CSR)
386 cccr &= ~CCCR_CSR;
387
388 if (enable) {
389 /* enable m_can configuration */
390 m_can_write(cdev, M_CAN_CCCR, cccr | CCCR_INIT);
391 udelay(5);
392 /* CCCR.CCE can only be set/reset while CCCR.INIT = '1' */
393 m_can_write(cdev, M_CAN_CCCR, cccr | CCCR_INIT | CCCR_CCE);
394 } else {
395 m_can_write(cdev, M_CAN_CCCR, cccr & ~(CCCR_INIT | CCCR_CCE));
396 }
397
398 /* there's a delay for module initialization */
399 if (enable)
400 val = CCCR_INIT | CCCR_CCE;
401
402 while ((m_can_read(cdev, M_CAN_CCCR) & (CCCR_INIT | CCCR_CCE)) != val) {
403 if (timeout == 0) {
404 netdev_warn(cdev->net, "Failed to init module\n");
405 return;
406 }
407 timeout--;
408 udelay(1);
409 }
410 }
411
m_can_enable_all_interrupts(struct m_can_classdev * cdev)412 static inline void m_can_enable_all_interrupts(struct m_can_classdev *cdev)
413 {
414 /* Only interrupt line 0 is used in this driver */
415 m_can_write(cdev, M_CAN_ILE, ILE_EINT0);
416 }
417
m_can_disable_all_interrupts(struct m_can_classdev * cdev)418 static inline void m_can_disable_all_interrupts(struct m_can_classdev *cdev)
419 {
420 m_can_write(cdev, M_CAN_ILE, 0x0);
421 }
422
423 /* Retrieve internal timestamp counter from TSCV.TSC, and shift it to 32-bit
424 * width.
425 */
m_can_get_timestamp(struct m_can_classdev * cdev)426 static u32 m_can_get_timestamp(struct m_can_classdev *cdev)
427 {
428 u32 tscv;
429 u32 tsc;
430
431 tscv = m_can_read(cdev, M_CAN_TSCV);
432 tsc = FIELD_GET(TSCV_TSC_MASK, tscv);
433
434 return (tsc << 16);
435 }
436
m_can_clean(struct net_device * net)437 static void m_can_clean(struct net_device *net)
438 {
439 struct m_can_classdev *cdev = netdev_priv(net);
440
441 if (cdev->tx_skb) {
442 int putidx = 0;
443
444 net->stats.tx_errors++;
445 if (cdev->version > 30)
446 putidx = FIELD_GET(TXFQS_TFQPI_MASK,
447 m_can_read(cdev, M_CAN_TXFQS));
448
449 can_free_echo_skb(cdev->net, putidx, NULL);
450 cdev->tx_skb = NULL;
451 }
452 }
453
454 /* For peripherals, pass skb to rx-offload, which will push skb from
455 * napi. For non-peripherals, RX is done in napi already, so push
456 * directly. timestamp is used to ensure good skb ordering in
457 * rx-offload and is ignored for non-peripherals.
458 */
m_can_receive_skb(struct m_can_classdev * cdev,struct sk_buff * skb,u32 timestamp)459 static void m_can_receive_skb(struct m_can_classdev *cdev,
460 struct sk_buff *skb,
461 u32 timestamp)
462 {
463 if (cdev->is_peripheral) {
464 struct net_device_stats *stats = &cdev->net->stats;
465 int err;
466
467 err = can_rx_offload_queue_sorted(&cdev->offload, skb,
468 timestamp);
469 if (err)
470 stats->rx_fifo_errors++;
471 } else {
472 netif_receive_skb(skb);
473 }
474 }
475
m_can_read_fifo(struct net_device * dev,u32 rxfs)476 static int m_can_read_fifo(struct net_device *dev, u32 rxfs)
477 {
478 struct net_device_stats *stats = &dev->stats;
479 struct m_can_classdev *cdev = netdev_priv(dev);
480 struct canfd_frame *cf;
481 struct sk_buff *skb;
482 struct id_and_dlc fifo_header;
483 u32 fgi;
484 u32 timestamp = 0;
485 int err;
486
487 /* calculate the fifo get index for where to read data */
488 fgi = FIELD_GET(RXFS_FGI_MASK, rxfs);
489 err = m_can_fifo_read(cdev, fgi, M_CAN_FIFO_ID, &fifo_header, 2);
490 if (err)
491 goto out_fail;
492
493 if (fifo_header.dlc & RX_BUF_FDF)
494 skb = alloc_canfd_skb(dev, &cf);
495 else
496 skb = alloc_can_skb(dev, (struct can_frame **)&cf);
497 if (!skb) {
498 stats->rx_dropped++;
499 return 0;
500 }
501
502 if (fifo_header.dlc & RX_BUF_FDF)
503 cf->len = can_fd_dlc2len((fifo_header.dlc >> 16) & 0x0F);
504 else
505 cf->len = can_cc_dlc2len((fifo_header.dlc >> 16) & 0x0F);
506
507 if (fifo_header.id & RX_BUF_XTD)
508 cf->can_id = (fifo_header.id & CAN_EFF_MASK) | CAN_EFF_FLAG;
509 else
510 cf->can_id = (fifo_header.id >> 18) & CAN_SFF_MASK;
511
512 if (fifo_header.id & RX_BUF_ESI) {
513 cf->flags |= CANFD_ESI;
514 netdev_dbg(dev, "ESI Error\n");
515 }
516
517 if (!(fifo_header.dlc & RX_BUF_FDF) && (fifo_header.id & RX_BUF_RTR)) {
518 cf->can_id |= CAN_RTR_FLAG;
519 } else {
520 if (fifo_header.dlc & RX_BUF_BRS)
521 cf->flags |= CANFD_BRS;
522
523 err = m_can_fifo_read(cdev, fgi, M_CAN_FIFO_DATA,
524 cf->data, DIV_ROUND_UP(cf->len, 4));
525 if (err)
526 goto out_free_skb;
527 }
528
529 /* acknowledge rx fifo 0 */
530 m_can_write(cdev, M_CAN_RXF0A, fgi);
531
532 stats->rx_packets++;
533 stats->rx_bytes += cf->len;
534
535 timestamp = FIELD_GET(RX_BUF_RXTS_MASK, fifo_header.dlc) << 16;
536
537 m_can_receive_skb(cdev, skb, timestamp);
538
539 return 0;
540
541 out_free_skb:
542 kfree_skb(skb);
543 out_fail:
544 netdev_err(dev, "FIFO read returned %d\n", err);
545 return err;
546 }
547
m_can_do_rx_poll(struct net_device * dev,int quota)548 static int m_can_do_rx_poll(struct net_device *dev, int quota)
549 {
550 struct m_can_classdev *cdev = netdev_priv(dev);
551 u32 pkts = 0;
552 u32 rxfs;
553 int err;
554
555 rxfs = m_can_read(cdev, M_CAN_RXF0S);
556 if (!(rxfs & RXFS_FFL_MASK)) {
557 netdev_dbg(dev, "no messages in fifo0\n");
558 return 0;
559 }
560
561 while ((rxfs & RXFS_FFL_MASK) && (quota > 0)) {
562 err = m_can_read_fifo(dev, rxfs);
563 if (err)
564 return err;
565
566 quota--;
567 pkts++;
568 rxfs = m_can_read(cdev, M_CAN_RXF0S);
569 }
570
571 if (pkts)
572 can_led_event(dev, CAN_LED_EVENT_RX);
573
574 return pkts;
575 }
576
m_can_handle_lost_msg(struct net_device * dev)577 static int m_can_handle_lost_msg(struct net_device *dev)
578 {
579 struct m_can_classdev *cdev = netdev_priv(dev);
580 struct net_device_stats *stats = &dev->stats;
581 struct sk_buff *skb;
582 struct can_frame *frame;
583 u32 timestamp = 0;
584
585 netdev_err(dev, "msg lost in rxf0\n");
586
587 stats->rx_errors++;
588 stats->rx_over_errors++;
589
590 skb = alloc_can_err_skb(dev, &frame);
591 if (unlikely(!skb))
592 return 0;
593
594 frame->can_id |= CAN_ERR_CRTL;
595 frame->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
596
597 if (cdev->is_peripheral)
598 timestamp = m_can_get_timestamp(cdev);
599
600 m_can_receive_skb(cdev, skb, timestamp);
601
602 return 1;
603 }
604
m_can_handle_lec_err(struct net_device * dev,enum m_can_lec_type lec_type)605 static int m_can_handle_lec_err(struct net_device *dev,
606 enum m_can_lec_type lec_type)
607 {
608 struct m_can_classdev *cdev = netdev_priv(dev);
609 struct net_device_stats *stats = &dev->stats;
610 struct can_frame *cf;
611 struct sk_buff *skb;
612 u32 timestamp = 0;
613
614 cdev->can.can_stats.bus_error++;
615 stats->rx_errors++;
616
617 /* propagate the error condition to the CAN stack */
618 skb = alloc_can_err_skb(dev, &cf);
619 if (unlikely(!skb))
620 return 0;
621
622 /* check for 'last error code' which tells us the
623 * type of the last error to occur on the CAN bus
624 */
625 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
626
627 switch (lec_type) {
628 case LEC_STUFF_ERROR:
629 netdev_dbg(dev, "stuff error\n");
630 cf->data[2] |= CAN_ERR_PROT_STUFF;
631 break;
632 case LEC_FORM_ERROR:
633 netdev_dbg(dev, "form error\n");
634 cf->data[2] |= CAN_ERR_PROT_FORM;
635 break;
636 case LEC_ACK_ERROR:
637 netdev_dbg(dev, "ack error\n");
638 cf->data[3] = CAN_ERR_PROT_LOC_ACK;
639 break;
640 case LEC_BIT1_ERROR:
641 netdev_dbg(dev, "bit1 error\n");
642 cf->data[2] |= CAN_ERR_PROT_BIT1;
643 break;
644 case LEC_BIT0_ERROR:
645 netdev_dbg(dev, "bit0 error\n");
646 cf->data[2] |= CAN_ERR_PROT_BIT0;
647 break;
648 case LEC_CRC_ERROR:
649 netdev_dbg(dev, "CRC error\n");
650 cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
651 break;
652 default:
653 break;
654 }
655
656 stats->rx_packets++;
657 stats->rx_bytes += cf->len;
658
659 if (cdev->is_peripheral)
660 timestamp = m_can_get_timestamp(cdev);
661
662 m_can_receive_skb(cdev, skb, timestamp);
663
664 return 1;
665 }
666
__m_can_get_berr_counter(const struct net_device * dev,struct can_berr_counter * bec)667 static int __m_can_get_berr_counter(const struct net_device *dev,
668 struct can_berr_counter *bec)
669 {
670 struct m_can_classdev *cdev = netdev_priv(dev);
671 unsigned int ecr;
672
673 ecr = m_can_read(cdev, M_CAN_ECR);
674 bec->rxerr = FIELD_GET(ECR_REC_MASK, ecr);
675 bec->txerr = FIELD_GET(ECR_TEC_MASK, ecr);
676
677 return 0;
678 }
679
m_can_clk_start(struct m_can_classdev * cdev)680 static int m_can_clk_start(struct m_can_classdev *cdev)
681 {
682 if (cdev->pm_clock_support == 0)
683 return 0;
684
685 return pm_runtime_resume_and_get(cdev->dev);
686 }
687
m_can_clk_stop(struct m_can_classdev * cdev)688 static void m_can_clk_stop(struct m_can_classdev *cdev)
689 {
690 if (cdev->pm_clock_support)
691 pm_runtime_put_sync(cdev->dev);
692 }
693
m_can_get_berr_counter(const struct net_device * dev,struct can_berr_counter * bec)694 static int m_can_get_berr_counter(const struct net_device *dev,
695 struct can_berr_counter *bec)
696 {
697 struct m_can_classdev *cdev = netdev_priv(dev);
698 int err;
699
700 err = m_can_clk_start(cdev);
701 if (err)
702 return err;
703
704 __m_can_get_berr_counter(dev, bec);
705
706 m_can_clk_stop(cdev);
707
708 return 0;
709 }
710
m_can_handle_state_change(struct net_device * dev,enum can_state new_state)711 static int m_can_handle_state_change(struct net_device *dev,
712 enum can_state new_state)
713 {
714 struct m_can_classdev *cdev = netdev_priv(dev);
715 struct net_device_stats *stats = &dev->stats;
716 struct can_frame *cf;
717 struct sk_buff *skb;
718 struct can_berr_counter bec;
719 unsigned int ecr;
720 u32 timestamp = 0;
721
722 switch (new_state) {
723 case CAN_STATE_ERROR_WARNING:
724 /* error warning state */
725 cdev->can.can_stats.error_warning++;
726 cdev->can.state = CAN_STATE_ERROR_WARNING;
727 break;
728 case CAN_STATE_ERROR_PASSIVE:
729 /* error passive state */
730 cdev->can.can_stats.error_passive++;
731 cdev->can.state = CAN_STATE_ERROR_PASSIVE;
732 break;
733 case CAN_STATE_BUS_OFF:
734 /* bus-off state */
735 cdev->can.state = CAN_STATE_BUS_OFF;
736 m_can_disable_all_interrupts(cdev);
737 cdev->can.can_stats.bus_off++;
738 can_bus_off(dev);
739 break;
740 default:
741 break;
742 }
743
744 /* propagate the error condition to the CAN stack */
745 skb = alloc_can_err_skb(dev, &cf);
746 if (unlikely(!skb))
747 return 0;
748
749 __m_can_get_berr_counter(dev, &bec);
750
751 switch (new_state) {
752 case CAN_STATE_ERROR_WARNING:
753 /* error warning state */
754 cf->can_id |= CAN_ERR_CRTL;
755 cf->data[1] = (bec.txerr > bec.rxerr) ?
756 CAN_ERR_CRTL_TX_WARNING :
757 CAN_ERR_CRTL_RX_WARNING;
758 cf->data[6] = bec.txerr;
759 cf->data[7] = bec.rxerr;
760 break;
761 case CAN_STATE_ERROR_PASSIVE:
762 /* error passive state */
763 cf->can_id |= CAN_ERR_CRTL;
764 ecr = m_can_read(cdev, M_CAN_ECR);
765 if (ecr & ECR_RP)
766 cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
767 if (bec.txerr > 127)
768 cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE;
769 cf->data[6] = bec.txerr;
770 cf->data[7] = bec.rxerr;
771 break;
772 case CAN_STATE_BUS_OFF:
773 /* bus-off state */
774 cf->can_id |= CAN_ERR_BUSOFF;
775 break;
776 default:
777 break;
778 }
779
780 stats->rx_packets++;
781 stats->rx_bytes += cf->len;
782
783 if (cdev->is_peripheral)
784 timestamp = m_can_get_timestamp(cdev);
785
786 m_can_receive_skb(cdev, skb, timestamp);
787
788 return 1;
789 }
790
m_can_handle_state_errors(struct net_device * dev,u32 psr)791 static int m_can_handle_state_errors(struct net_device *dev, u32 psr)
792 {
793 struct m_can_classdev *cdev = netdev_priv(dev);
794 int work_done = 0;
795
796 if (psr & PSR_EW && cdev->can.state != CAN_STATE_ERROR_WARNING) {
797 netdev_dbg(dev, "entered error warning state\n");
798 work_done += m_can_handle_state_change(dev,
799 CAN_STATE_ERROR_WARNING);
800 }
801
802 if (psr & PSR_EP && cdev->can.state != CAN_STATE_ERROR_PASSIVE) {
803 netdev_dbg(dev, "entered error passive state\n");
804 work_done += m_can_handle_state_change(dev,
805 CAN_STATE_ERROR_PASSIVE);
806 }
807
808 if (psr & PSR_BO && cdev->can.state != CAN_STATE_BUS_OFF) {
809 netdev_dbg(dev, "entered error bus off state\n");
810 work_done += m_can_handle_state_change(dev,
811 CAN_STATE_BUS_OFF);
812 }
813
814 return work_done;
815 }
816
m_can_handle_other_err(struct net_device * dev,u32 irqstatus)817 static void m_can_handle_other_err(struct net_device *dev, u32 irqstatus)
818 {
819 if (irqstatus & IR_WDI)
820 netdev_err(dev, "Message RAM Watchdog event due to missing READY\n");
821 if (irqstatus & IR_BEU)
822 netdev_err(dev, "Bit Error Uncorrected\n");
823 if (irqstatus & IR_BEC)
824 netdev_err(dev, "Bit Error Corrected\n");
825 if (irqstatus & IR_TOO)
826 netdev_err(dev, "Timeout reached\n");
827 if (irqstatus & IR_MRAF)
828 netdev_err(dev, "Message RAM access failure occurred\n");
829 }
830
is_lec_err(u32 psr)831 static inline bool is_lec_err(u32 psr)
832 {
833 psr &= LEC_UNUSED;
834
835 return psr && (psr != LEC_UNUSED);
836 }
837
m_can_is_protocol_err(u32 irqstatus)838 static inline bool m_can_is_protocol_err(u32 irqstatus)
839 {
840 return irqstatus & IR_ERR_LEC_31X;
841 }
842
m_can_handle_protocol_error(struct net_device * dev,u32 irqstatus)843 static int m_can_handle_protocol_error(struct net_device *dev, u32 irqstatus)
844 {
845 struct net_device_stats *stats = &dev->stats;
846 struct m_can_classdev *cdev = netdev_priv(dev);
847 struct can_frame *cf;
848 struct sk_buff *skb;
849 u32 timestamp = 0;
850
851 /* propagate the error condition to the CAN stack */
852 skb = alloc_can_err_skb(dev, &cf);
853
854 /* update tx error stats since there is protocol error */
855 stats->tx_errors++;
856
857 /* update arbitration lost status */
858 if (cdev->version >= 31 && (irqstatus & IR_PEA)) {
859 netdev_dbg(dev, "Protocol error in Arbitration fail\n");
860 cdev->can.can_stats.arbitration_lost++;
861 if (skb) {
862 cf->can_id |= CAN_ERR_LOSTARB;
863 cf->data[0] |= CAN_ERR_LOSTARB_UNSPEC;
864 }
865 }
866
867 if (unlikely(!skb)) {
868 netdev_dbg(dev, "allocation of skb failed\n");
869 return 0;
870 }
871
872 if (cdev->is_peripheral)
873 timestamp = m_can_get_timestamp(cdev);
874
875 m_can_receive_skb(cdev, skb, timestamp);
876
877 return 1;
878 }
879
m_can_handle_bus_errors(struct net_device * dev,u32 irqstatus,u32 psr)880 static int m_can_handle_bus_errors(struct net_device *dev, u32 irqstatus,
881 u32 psr)
882 {
883 struct m_can_classdev *cdev = netdev_priv(dev);
884 int work_done = 0;
885
886 if (irqstatus & IR_RF0L)
887 work_done += m_can_handle_lost_msg(dev);
888
889 /* handle lec errors on the bus */
890 if ((cdev->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) &&
891 is_lec_err(psr))
892 work_done += m_can_handle_lec_err(dev, psr & LEC_UNUSED);
893
894 /* handle protocol errors in arbitration phase */
895 if ((cdev->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) &&
896 m_can_is_protocol_err(irqstatus))
897 work_done += m_can_handle_protocol_error(dev, irqstatus);
898
899 /* other unproccessed error interrupts */
900 m_can_handle_other_err(dev, irqstatus);
901
902 return work_done;
903 }
904
m_can_rx_handler(struct net_device * dev,int quota)905 static int m_can_rx_handler(struct net_device *dev, int quota)
906 {
907 struct m_can_classdev *cdev = netdev_priv(dev);
908 int rx_work_or_err;
909 int work_done = 0;
910 u32 irqstatus, psr;
911
912 irqstatus = cdev->irqstatus | m_can_read(cdev, M_CAN_IR);
913 if (!irqstatus)
914 goto end;
915
916 /* Errata workaround for issue "Needless activation of MRAF irq"
917 * During frame reception while the MCAN is in Error Passive state
918 * and the Receive Error Counter has the value MCAN_ECR.REC = 127,
919 * it may happen that MCAN_IR.MRAF is set although there was no
920 * Message RAM access failure.
921 * If MCAN_IR.MRAF is enabled, an interrupt to the Host CPU is generated
922 * The Message RAM Access Failure interrupt routine needs to check
923 * whether MCAN_ECR.RP = ’1’ and MCAN_ECR.REC = 127.
924 * In this case, reset MCAN_IR.MRAF. No further action is required.
925 */
926 if (cdev->version <= 31 && irqstatus & IR_MRAF &&
927 m_can_read(cdev, M_CAN_ECR) & ECR_RP) {
928 struct can_berr_counter bec;
929
930 __m_can_get_berr_counter(dev, &bec);
931 if (bec.rxerr == 127) {
932 m_can_write(cdev, M_CAN_IR, IR_MRAF);
933 irqstatus &= ~IR_MRAF;
934 }
935 }
936
937 psr = m_can_read(cdev, M_CAN_PSR);
938
939 if (irqstatus & IR_ERR_STATE)
940 work_done += m_can_handle_state_errors(dev, psr);
941
942 if (irqstatus & IR_ERR_BUS_30X)
943 work_done += m_can_handle_bus_errors(dev, irqstatus, psr);
944
945 if (irqstatus & IR_RF0N) {
946 rx_work_or_err = m_can_do_rx_poll(dev, (quota - work_done));
947 if (rx_work_or_err < 0)
948 return rx_work_or_err;
949
950 work_done += rx_work_or_err;
951 }
952 end:
953 return work_done;
954 }
955
m_can_rx_peripheral(struct net_device * dev)956 static int m_can_rx_peripheral(struct net_device *dev)
957 {
958 struct m_can_classdev *cdev = netdev_priv(dev);
959 int work_done;
960
961 work_done = m_can_rx_handler(dev, M_CAN_NAPI_WEIGHT);
962
963 /* Don't re-enable interrupts if the driver had a fatal error
964 * (e.g., FIFO read failure).
965 */
966 if (work_done >= 0)
967 m_can_enable_all_interrupts(cdev);
968
969 return work_done;
970 }
971
m_can_poll(struct napi_struct * napi,int quota)972 static int m_can_poll(struct napi_struct *napi, int quota)
973 {
974 struct net_device *dev = napi->dev;
975 struct m_can_classdev *cdev = netdev_priv(dev);
976 int work_done;
977
978 work_done = m_can_rx_handler(dev, quota);
979
980 /* Don't re-enable interrupts if the driver had a fatal error
981 * (e.g., FIFO read failure).
982 */
983 if (work_done >= 0 && work_done < quota) {
984 napi_complete_done(napi, work_done);
985 m_can_enable_all_interrupts(cdev);
986 }
987
988 return work_done;
989 }
990
991 /* Echo tx skb and update net stats. Peripherals use rx-offload for
992 * echo. timestamp is used for peripherals to ensure correct ordering
993 * by rx-offload, and is ignored for non-peripherals.
994 */
m_can_tx_update_stats(struct m_can_classdev * cdev,unsigned int msg_mark,u32 timestamp)995 static void m_can_tx_update_stats(struct m_can_classdev *cdev,
996 unsigned int msg_mark,
997 u32 timestamp)
998 {
999 struct net_device *dev = cdev->net;
1000 struct net_device_stats *stats = &dev->stats;
1001
1002 if (cdev->is_peripheral)
1003 stats->tx_bytes +=
1004 can_rx_offload_get_echo_skb(&cdev->offload,
1005 msg_mark,
1006 timestamp,
1007 NULL);
1008 else
1009 stats->tx_bytes += can_get_echo_skb(dev, msg_mark, NULL);
1010
1011 stats->tx_packets++;
1012 }
1013
m_can_echo_tx_event(struct net_device * dev)1014 static int m_can_echo_tx_event(struct net_device *dev)
1015 {
1016 u32 txe_count = 0;
1017 u32 m_can_txefs;
1018 u32 fgi = 0;
1019 int i = 0;
1020 unsigned int msg_mark;
1021
1022 struct m_can_classdev *cdev = netdev_priv(dev);
1023
1024 /* read tx event fifo status */
1025 m_can_txefs = m_can_read(cdev, M_CAN_TXEFS);
1026
1027 /* Get Tx Event fifo element count */
1028 txe_count = FIELD_GET(TXEFS_EFFL_MASK, m_can_txefs);
1029
1030 /* Get and process all sent elements */
1031 for (i = 0; i < txe_count; i++) {
1032 u32 txe, timestamp = 0;
1033 int err;
1034
1035 /* retrieve get index */
1036 fgi = FIELD_GET(TXEFS_EFGI_MASK, m_can_read(cdev, M_CAN_TXEFS));
1037
1038 /* get message marker, timestamp */
1039 err = m_can_txe_fifo_read(cdev, fgi, 4, &txe);
1040 if (err) {
1041 netdev_err(dev, "TXE FIFO read returned %d\n", err);
1042 return err;
1043 }
1044
1045 msg_mark = FIELD_GET(TX_EVENT_MM_MASK, txe);
1046 timestamp = FIELD_GET(TX_EVENT_TXTS_MASK, txe) << 16;
1047
1048 /* ack txe element */
1049 m_can_write(cdev, M_CAN_TXEFA, FIELD_PREP(TXEFA_EFAI_MASK,
1050 fgi));
1051
1052 /* update stats */
1053 m_can_tx_update_stats(cdev, msg_mark, timestamp);
1054 }
1055
1056 return 0;
1057 }
1058
m_can_isr(int irq,void * dev_id)1059 static irqreturn_t m_can_isr(int irq, void *dev_id)
1060 {
1061 struct net_device *dev = (struct net_device *)dev_id;
1062 struct m_can_classdev *cdev = netdev_priv(dev);
1063 u32 ir;
1064
1065 if (pm_runtime_suspended(cdev->dev))
1066 return IRQ_NONE;
1067 ir = m_can_read(cdev, M_CAN_IR);
1068 if (!ir)
1069 return IRQ_NONE;
1070
1071 /* ACK all irqs */
1072 if (ir & IR_ALL_INT)
1073 m_can_write(cdev, M_CAN_IR, ir);
1074
1075 if (cdev->ops->clear_interrupts)
1076 cdev->ops->clear_interrupts(cdev);
1077
1078 /* schedule NAPI in case of
1079 * - rx IRQ
1080 * - state change IRQ
1081 * - bus error IRQ and bus error reporting
1082 */
1083 if ((ir & IR_RF0N) || (ir & IR_ERR_ALL_30X)) {
1084 cdev->irqstatus = ir;
1085 m_can_disable_all_interrupts(cdev);
1086 if (!cdev->is_peripheral)
1087 napi_schedule(&cdev->napi);
1088 else if (m_can_rx_peripheral(dev) < 0)
1089 goto out_fail;
1090 }
1091
1092 if (cdev->version == 30) {
1093 if (ir & IR_TC) {
1094 /* Transmission Complete Interrupt*/
1095 u32 timestamp = 0;
1096
1097 if (cdev->is_peripheral)
1098 timestamp = m_can_get_timestamp(cdev);
1099 m_can_tx_update_stats(cdev, 0, timestamp);
1100
1101 can_led_event(dev, CAN_LED_EVENT_TX);
1102 netif_wake_queue(dev);
1103 }
1104 } else {
1105 if (ir & IR_TEFN) {
1106 /* New TX FIFO Element arrived */
1107 if (m_can_echo_tx_event(dev) != 0)
1108 goto out_fail;
1109
1110 can_led_event(dev, CAN_LED_EVENT_TX);
1111 if (netif_queue_stopped(dev) &&
1112 !m_can_tx_fifo_full(cdev))
1113 netif_wake_queue(dev);
1114 }
1115 }
1116
1117 if (cdev->is_peripheral)
1118 can_rx_offload_threaded_irq_finish(&cdev->offload);
1119
1120 return IRQ_HANDLED;
1121
1122 out_fail:
1123 m_can_disable_all_interrupts(cdev);
1124 return IRQ_HANDLED;
1125 }
1126
1127 static const struct can_bittiming_const m_can_bittiming_const_30X = {
1128 .name = KBUILD_MODNAME,
1129 .tseg1_min = 2, /* Time segment 1 = prop_seg + phase_seg1 */
1130 .tseg1_max = 64,
1131 .tseg2_min = 1, /* Time segment 2 = phase_seg2 */
1132 .tseg2_max = 16,
1133 .sjw_max = 16,
1134 .brp_min = 1,
1135 .brp_max = 1024,
1136 .brp_inc = 1,
1137 };
1138
1139 static const struct can_bittiming_const m_can_data_bittiming_const_30X = {
1140 .name = KBUILD_MODNAME,
1141 .tseg1_min = 2, /* Time segment 1 = prop_seg + phase_seg1 */
1142 .tseg1_max = 16,
1143 .tseg2_min = 1, /* Time segment 2 = phase_seg2 */
1144 .tseg2_max = 8,
1145 .sjw_max = 4,
1146 .brp_min = 1,
1147 .brp_max = 32,
1148 .brp_inc = 1,
1149 };
1150
1151 static const struct can_bittiming_const m_can_bittiming_const_31X = {
1152 .name = KBUILD_MODNAME,
1153 .tseg1_min = 2, /* Time segment 1 = prop_seg + phase_seg1 */
1154 .tseg1_max = 256,
1155 .tseg2_min = 2, /* Time segment 2 = phase_seg2 */
1156 .tseg2_max = 128,
1157 .sjw_max = 128,
1158 .brp_min = 1,
1159 .brp_max = 512,
1160 .brp_inc = 1,
1161 };
1162
1163 static const struct can_bittiming_const m_can_data_bittiming_const_31X = {
1164 .name = KBUILD_MODNAME,
1165 .tseg1_min = 1, /* Time segment 1 = prop_seg + phase_seg1 */
1166 .tseg1_max = 32,
1167 .tseg2_min = 1, /* Time segment 2 = phase_seg2 */
1168 .tseg2_max = 16,
1169 .sjw_max = 16,
1170 .brp_min = 1,
1171 .brp_max = 32,
1172 .brp_inc = 1,
1173 };
1174
m_can_set_bittiming(struct net_device * dev)1175 static int m_can_set_bittiming(struct net_device *dev)
1176 {
1177 struct m_can_classdev *cdev = netdev_priv(dev);
1178 const struct can_bittiming *bt = &cdev->can.bittiming;
1179 const struct can_bittiming *dbt = &cdev->can.data_bittiming;
1180 u16 brp, sjw, tseg1, tseg2;
1181 u32 reg_btp;
1182
1183 brp = bt->brp - 1;
1184 sjw = bt->sjw - 1;
1185 tseg1 = bt->prop_seg + bt->phase_seg1 - 1;
1186 tseg2 = bt->phase_seg2 - 1;
1187 reg_btp = FIELD_PREP(NBTP_NBRP_MASK, brp) |
1188 FIELD_PREP(NBTP_NSJW_MASK, sjw) |
1189 FIELD_PREP(NBTP_NTSEG1_MASK, tseg1) |
1190 FIELD_PREP(NBTP_NTSEG2_MASK, tseg2);
1191 m_can_write(cdev, M_CAN_NBTP, reg_btp);
1192
1193 if (cdev->can.ctrlmode & CAN_CTRLMODE_FD) {
1194 reg_btp = 0;
1195 brp = dbt->brp - 1;
1196 sjw = dbt->sjw - 1;
1197 tseg1 = dbt->prop_seg + dbt->phase_seg1 - 1;
1198 tseg2 = dbt->phase_seg2 - 1;
1199
1200 /* TDC is only needed for bitrates beyond 2.5 MBit/s.
1201 * This is mentioned in the "Bit Time Requirements for CAN FD"
1202 * paper presented at the International CAN Conference 2013
1203 */
1204 if (dbt->bitrate > 2500000) {
1205 u32 tdco, ssp;
1206
1207 /* Use the same value of secondary sampling point
1208 * as the data sampling point
1209 */
1210 ssp = dbt->sample_point;
1211
1212 /* Equation based on Bosch's M_CAN User Manual's
1213 * Transmitter Delay Compensation Section
1214 */
1215 tdco = (cdev->can.clock.freq / 1000) *
1216 ssp / dbt->bitrate;
1217
1218 /* Max valid TDCO value is 127 */
1219 if (tdco > 127) {
1220 netdev_warn(dev, "TDCO value of %u is beyond maximum. Using maximum possible value\n",
1221 tdco);
1222 tdco = 127;
1223 }
1224
1225 reg_btp |= DBTP_TDC;
1226 m_can_write(cdev, M_CAN_TDCR,
1227 FIELD_PREP(TDCR_TDCO_MASK, tdco));
1228 }
1229
1230 reg_btp |= FIELD_PREP(DBTP_DBRP_MASK, brp) |
1231 FIELD_PREP(DBTP_DSJW_MASK, sjw) |
1232 FIELD_PREP(DBTP_DTSEG1_MASK, tseg1) |
1233 FIELD_PREP(DBTP_DTSEG2_MASK, tseg2);
1234
1235 m_can_write(cdev, M_CAN_DBTP, reg_btp);
1236 }
1237
1238 return 0;
1239 }
1240
1241 /* Configure M_CAN chip:
1242 * - set rx buffer/fifo element size
1243 * - configure rx fifo
1244 * - accept non-matching frame into fifo 0
1245 * - configure tx buffer
1246 * - >= v3.1.x: TX FIFO is used
1247 * - configure mode
1248 * - setup bittiming
1249 * - configure timestamp generation
1250 */
m_can_chip_config(struct net_device * dev)1251 static int m_can_chip_config(struct net_device *dev)
1252 {
1253 struct m_can_classdev *cdev = netdev_priv(dev);
1254 u32 cccr, test;
1255 int err;
1256
1257 err = m_can_init_ram(cdev);
1258 if (err) {
1259 dev_err(cdev->dev, "Message RAM configuration failed\n");
1260 return err;
1261 }
1262
1263 m_can_config_endisable(cdev, true);
1264
1265 /* RX Buffer/FIFO Element Size 64 bytes data field */
1266 m_can_write(cdev, M_CAN_RXESC,
1267 FIELD_PREP(RXESC_RBDS_MASK, RXESC_64B) |
1268 FIELD_PREP(RXESC_F1DS_MASK, RXESC_64B) |
1269 FIELD_PREP(RXESC_F0DS_MASK, RXESC_64B));
1270
1271 /* Accept Non-matching Frames Into FIFO 0 */
1272 m_can_write(cdev, M_CAN_GFC, 0x0);
1273
1274 if (cdev->version == 30) {
1275 /* only support one Tx Buffer currently */
1276 m_can_write(cdev, M_CAN_TXBC, FIELD_PREP(TXBC_NDTB_MASK, 1) |
1277 cdev->mcfg[MRAM_TXB].off);
1278 } else {
1279 /* TX FIFO is used for newer IP Core versions */
1280 m_can_write(cdev, M_CAN_TXBC,
1281 FIELD_PREP(TXBC_TFQS_MASK,
1282 cdev->mcfg[MRAM_TXB].num) |
1283 cdev->mcfg[MRAM_TXB].off);
1284 }
1285
1286 /* support 64 bytes payload */
1287 m_can_write(cdev, M_CAN_TXESC,
1288 FIELD_PREP(TXESC_TBDS_MASK, TXESC_TBDS_64B));
1289
1290 /* TX Event FIFO */
1291 if (cdev->version == 30) {
1292 m_can_write(cdev, M_CAN_TXEFC,
1293 FIELD_PREP(TXEFC_EFS_MASK, 1) |
1294 cdev->mcfg[MRAM_TXE].off);
1295 } else {
1296 /* Full TX Event FIFO is used */
1297 m_can_write(cdev, M_CAN_TXEFC,
1298 FIELD_PREP(TXEFC_EFS_MASK,
1299 cdev->mcfg[MRAM_TXE].num) |
1300 cdev->mcfg[MRAM_TXE].off);
1301 }
1302
1303 /* rx fifo configuration, blocking mode, fifo size 1 */
1304 m_can_write(cdev, M_CAN_RXF0C,
1305 FIELD_PREP(RXFC_FS_MASK, cdev->mcfg[MRAM_RXF0].num) |
1306 cdev->mcfg[MRAM_RXF0].off);
1307
1308 m_can_write(cdev, M_CAN_RXF1C,
1309 FIELD_PREP(RXFC_FS_MASK, cdev->mcfg[MRAM_RXF1].num) |
1310 cdev->mcfg[MRAM_RXF1].off);
1311
1312 cccr = m_can_read(cdev, M_CAN_CCCR);
1313 test = m_can_read(cdev, M_CAN_TEST);
1314 test &= ~TEST_LBCK;
1315 if (cdev->version == 30) {
1316 /* Version 3.0.x */
1317
1318 cccr &= ~(CCCR_TEST | CCCR_MON | CCCR_DAR |
1319 FIELD_PREP(CCCR_CMR_MASK, FIELD_MAX(CCCR_CMR_MASK)) |
1320 FIELD_PREP(CCCR_CME_MASK, FIELD_MAX(CCCR_CME_MASK)));
1321
1322 if (cdev->can.ctrlmode & CAN_CTRLMODE_FD)
1323 cccr |= FIELD_PREP(CCCR_CME_MASK, CCCR_CME_CANFD_BRS);
1324
1325 } else {
1326 /* Version 3.1.x or 3.2.x */
1327 cccr &= ~(CCCR_TEST | CCCR_MON | CCCR_BRSE | CCCR_FDOE |
1328 CCCR_NISO | CCCR_DAR);
1329
1330 /* Only 3.2.x has NISO Bit implemented */
1331 if (cdev->can.ctrlmode & CAN_CTRLMODE_FD_NON_ISO)
1332 cccr |= CCCR_NISO;
1333
1334 if (cdev->can.ctrlmode & CAN_CTRLMODE_FD)
1335 cccr |= (CCCR_BRSE | CCCR_FDOE);
1336 }
1337
1338 /* Loopback Mode */
1339 if (cdev->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) {
1340 cccr |= CCCR_TEST | CCCR_MON;
1341 test |= TEST_LBCK;
1342 }
1343
1344 /* Enable Monitoring (all versions) */
1345 if (cdev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
1346 cccr |= CCCR_MON;
1347
1348 /* Disable Auto Retransmission (all versions) */
1349 if (cdev->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT)
1350 cccr |= CCCR_DAR;
1351
1352 /* Write config */
1353 m_can_write(cdev, M_CAN_CCCR, cccr);
1354 m_can_write(cdev, M_CAN_TEST, test);
1355
1356 /* Enable interrupts */
1357 m_can_write(cdev, M_CAN_IR, IR_ALL_INT);
1358 if (!(cdev->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING))
1359 if (cdev->version == 30)
1360 m_can_write(cdev, M_CAN_IE, IR_ALL_INT &
1361 ~(IR_ERR_LEC_30X));
1362 else
1363 m_can_write(cdev, M_CAN_IE, IR_ALL_INT &
1364 ~(IR_ERR_LEC_31X));
1365 else
1366 m_can_write(cdev, M_CAN_IE, IR_ALL_INT);
1367
1368 /* route all interrupts to INT0 */
1369 m_can_write(cdev, M_CAN_ILS, ILS_ALL_INT0);
1370
1371 /* set bittiming params */
1372 m_can_set_bittiming(dev);
1373
1374 /* enable internal timestamp generation, with a prescalar of 16. The
1375 * prescalar is applied to the nominal bit timing
1376 */
1377 m_can_write(cdev, M_CAN_TSCC,
1378 FIELD_PREP(TSCC_TCP_MASK, 0xf) |
1379 FIELD_PREP(TSCC_TSS_MASK, TSCC_TSS_INTERNAL));
1380
1381 m_can_config_endisable(cdev, false);
1382
1383 if (cdev->ops->init)
1384 cdev->ops->init(cdev);
1385
1386 return 0;
1387 }
1388
m_can_start(struct net_device * dev)1389 static int m_can_start(struct net_device *dev)
1390 {
1391 struct m_can_classdev *cdev = netdev_priv(dev);
1392 int ret;
1393
1394 /* basic m_can configuration */
1395 ret = m_can_chip_config(dev);
1396 if (ret)
1397 return ret;
1398
1399 cdev->can.state = CAN_STATE_ERROR_ACTIVE;
1400
1401 m_can_enable_all_interrupts(cdev);
1402
1403 return 0;
1404 }
1405
m_can_set_mode(struct net_device * dev,enum can_mode mode)1406 static int m_can_set_mode(struct net_device *dev, enum can_mode mode)
1407 {
1408 switch (mode) {
1409 case CAN_MODE_START:
1410 m_can_clean(dev);
1411 m_can_start(dev);
1412 netif_wake_queue(dev);
1413 break;
1414 default:
1415 return -EOPNOTSUPP;
1416 }
1417
1418 return 0;
1419 }
1420
1421 /* Checks core release number of M_CAN
1422 * returns 0 if an unsupported device is detected
1423 * else it returns the release and step coded as:
1424 * return value = 10 * <release> + 1 * <step>
1425 */
m_can_check_core_release(struct m_can_classdev * cdev)1426 static int m_can_check_core_release(struct m_can_classdev *cdev)
1427 {
1428 u32 crel_reg;
1429 u8 rel;
1430 u8 step;
1431 int res;
1432
1433 /* Read Core Release Version and split into version number
1434 * Example: Version 3.2.1 => rel = 3; step = 2; substep = 1;
1435 */
1436 crel_reg = m_can_read(cdev, M_CAN_CREL);
1437 rel = (u8)FIELD_GET(CREL_REL_MASK, crel_reg);
1438 step = (u8)FIELD_GET(CREL_STEP_MASK, crel_reg);
1439
1440 if (rel == 3) {
1441 /* M_CAN v3.x.y: create return value */
1442 res = 30 + step;
1443 } else {
1444 /* Unsupported M_CAN version */
1445 res = 0;
1446 }
1447
1448 return res;
1449 }
1450
1451 /* Selectable Non ISO support only in version 3.2.x
1452 * This function checks if the bit is writable.
1453 */
m_can_niso_supported(struct m_can_classdev * cdev)1454 static bool m_can_niso_supported(struct m_can_classdev *cdev)
1455 {
1456 u32 cccr_reg, cccr_poll = 0;
1457 int niso_timeout = -ETIMEDOUT;
1458 int i;
1459
1460 m_can_config_endisable(cdev, true);
1461 cccr_reg = m_can_read(cdev, M_CAN_CCCR);
1462 cccr_reg |= CCCR_NISO;
1463 m_can_write(cdev, M_CAN_CCCR, cccr_reg);
1464
1465 for (i = 0; i <= 10; i++) {
1466 cccr_poll = m_can_read(cdev, M_CAN_CCCR);
1467 if (cccr_poll == cccr_reg) {
1468 niso_timeout = 0;
1469 break;
1470 }
1471
1472 usleep_range(1, 5);
1473 }
1474
1475 /* Clear NISO */
1476 cccr_reg &= ~(CCCR_NISO);
1477 m_can_write(cdev, M_CAN_CCCR, cccr_reg);
1478
1479 m_can_config_endisable(cdev, false);
1480
1481 /* return false if time out (-ETIMEDOUT), else return true */
1482 return !niso_timeout;
1483 }
1484
m_can_dev_setup(struct m_can_classdev * cdev)1485 static int m_can_dev_setup(struct m_can_classdev *cdev)
1486 {
1487 struct net_device *dev = cdev->net;
1488 int m_can_version;
1489
1490 m_can_version = m_can_check_core_release(cdev);
1491 /* return if unsupported version */
1492 if (!m_can_version) {
1493 dev_err(cdev->dev, "Unsupported version number: %2d",
1494 m_can_version);
1495 return -EINVAL;
1496 }
1497
1498 if (!cdev->is_peripheral)
1499 netif_napi_add(dev, &cdev->napi,
1500 m_can_poll, M_CAN_NAPI_WEIGHT);
1501
1502 /* Shared properties of all M_CAN versions */
1503 cdev->version = m_can_version;
1504 cdev->can.do_set_mode = m_can_set_mode;
1505 cdev->can.do_get_berr_counter = m_can_get_berr_counter;
1506
1507 /* Set M_CAN supported operations */
1508 cdev->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
1509 CAN_CTRLMODE_LISTENONLY |
1510 CAN_CTRLMODE_BERR_REPORTING |
1511 CAN_CTRLMODE_FD |
1512 CAN_CTRLMODE_ONE_SHOT;
1513
1514 /* Set properties depending on M_CAN version */
1515 switch (cdev->version) {
1516 case 30:
1517 /* CAN_CTRLMODE_FD_NON_ISO is fixed with M_CAN IP v3.0.x */
1518 can_set_static_ctrlmode(dev, CAN_CTRLMODE_FD_NON_ISO);
1519 cdev->can.bittiming_const = cdev->bit_timing ?
1520 cdev->bit_timing : &m_can_bittiming_const_30X;
1521
1522 cdev->can.data_bittiming_const = cdev->data_timing ?
1523 cdev->data_timing :
1524 &m_can_data_bittiming_const_30X;
1525 break;
1526 case 31:
1527 /* CAN_CTRLMODE_FD_NON_ISO is fixed with M_CAN IP v3.1.x */
1528 can_set_static_ctrlmode(dev, CAN_CTRLMODE_FD_NON_ISO);
1529 cdev->can.bittiming_const = cdev->bit_timing ?
1530 cdev->bit_timing : &m_can_bittiming_const_31X;
1531
1532 cdev->can.data_bittiming_const = cdev->data_timing ?
1533 cdev->data_timing :
1534 &m_can_data_bittiming_const_31X;
1535 break;
1536 case 32:
1537 case 33:
1538 /* Support both MCAN version v3.2.x and v3.3.0 */
1539 cdev->can.bittiming_const = cdev->bit_timing ?
1540 cdev->bit_timing : &m_can_bittiming_const_31X;
1541
1542 cdev->can.data_bittiming_const = cdev->data_timing ?
1543 cdev->data_timing :
1544 &m_can_data_bittiming_const_31X;
1545
1546 cdev->can.ctrlmode_supported |=
1547 (m_can_niso_supported(cdev) ?
1548 CAN_CTRLMODE_FD_NON_ISO : 0);
1549 break;
1550 default:
1551 dev_err(cdev->dev, "Unsupported version number: %2d",
1552 cdev->version);
1553 return -EINVAL;
1554 }
1555
1556 if (cdev->ops->init)
1557 cdev->ops->init(cdev);
1558
1559 return 0;
1560 }
1561
m_can_stop(struct net_device * dev)1562 static void m_can_stop(struct net_device *dev)
1563 {
1564 struct m_can_classdev *cdev = netdev_priv(dev);
1565
1566 /* disable all interrupts */
1567 m_can_disable_all_interrupts(cdev);
1568
1569 /* Set init mode to disengage from the network */
1570 m_can_config_endisable(cdev, true);
1571
1572 /* set the state as STOPPED */
1573 cdev->can.state = CAN_STATE_STOPPED;
1574 }
1575
m_can_close(struct net_device * dev)1576 static int m_can_close(struct net_device *dev)
1577 {
1578 struct m_can_classdev *cdev = netdev_priv(dev);
1579
1580 netif_stop_queue(dev);
1581
1582 if (!cdev->is_peripheral)
1583 napi_disable(&cdev->napi);
1584
1585 m_can_stop(dev);
1586 m_can_clk_stop(cdev);
1587 free_irq(dev->irq, dev);
1588
1589 if (cdev->is_peripheral) {
1590 cdev->tx_skb = NULL;
1591 destroy_workqueue(cdev->tx_wq);
1592 cdev->tx_wq = NULL;
1593 }
1594
1595 if (cdev->is_peripheral)
1596 can_rx_offload_disable(&cdev->offload);
1597
1598 close_candev(dev);
1599 can_led_event(dev, CAN_LED_EVENT_STOP);
1600
1601 phy_power_off(cdev->transceiver);
1602
1603 return 0;
1604 }
1605
m_can_next_echo_skb_occupied(struct net_device * dev,int putidx)1606 static int m_can_next_echo_skb_occupied(struct net_device *dev, int putidx)
1607 {
1608 struct m_can_classdev *cdev = netdev_priv(dev);
1609 /*get wrap around for loopback skb index */
1610 unsigned int wrap = cdev->can.echo_skb_max;
1611 int next_idx;
1612
1613 /* calculate next index */
1614 next_idx = (++putidx >= wrap ? 0 : putidx);
1615
1616 /* check if occupied */
1617 return !!cdev->can.echo_skb[next_idx];
1618 }
1619
m_can_tx_handler(struct m_can_classdev * cdev)1620 static netdev_tx_t m_can_tx_handler(struct m_can_classdev *cdev)
1621 {
1622 struct canfd_frame *cf = (struct canfd_frame *)cdev->tx_skb->data;
1623 struct net_device *dev = cdev->net;
1624 struct sk_buff *skb = cdev->tx_skb;
1625 struct id_and_dlc fifo_header;
1626 u32 cccr, fdflags;
1627 int err;
1628 int putidx;
1629
1630 cdev->tx_skb = NULL;
1631
1632 /* Generate ID field for TX buffer Element */
1633 /* Common to all supported M_CAN versions */
1634 if (cf->can_id & CAN_EFF_FLAG) {
1635 fifo_header.id = cf->can_id & CAN_EFF_MASK;
1636 fifo_header.id |= TX_BUF_XTD;
1637 } else {
1638 fifo_header.id = ((cf->can_id & CAN_SFF_MASK) << 18);
1639 }
1640
1641 if (cf->can_id & CAN_RTR_FLAG)
1642 fifo_header.id |= TX_BUF_RTR;
1643
1644 if (cdev->version == 30) {
1645 netif_stop_queue(dev);
1646
1647 fifo_header.dlc = can_fd_len2dlc(cf->len) << 16;
1648
1649 /* Write the frame ID, DLC, and payload to the FIFO element. */
1650 err = m_can_fifo_write(cdev, 0, M_CAN_FIFO_ID, &fifo_header, 2);
1651 if (err)
1652 goto out_fail;
1653
1654 err = m_can_fifo_write(cdev, 0, M_CAN_FIFO_DATA,
1655 cf->data, DIV_ROUND_UP(cf->len, 4));
1656 if (err)
1657 goto out_fail;
1658
1659 if (cdev->can.ctrlmode & CAN_CTRLMODE_FD) {
1660 cccr = m_can_read(cdev, M_CAN_CCCR);
1661 cccr &= ~CCCR_CMR_MASK;
1662 if (can_is_canfd_skb(skb)) {
1663 if (cf->flags & CANFD_BRS)
1664 cccr |= FIELD_PREP(CCCR_CMR_MASK,
1665 CCCR_CMR_CANFD_BRS);
1666 else
1667 cccr |= FIELD_PREP(CCCR_CMR_MASK,
1668 CCCR_CMR_CANFD);
1669 } else {
1670 cccr |= FIELD_PREP(CCCR_CMR_MASK, CCCR_CMR_CAN);
1671 }
1672 m_can_write(cdev, M_CAN_CCCR, cccr);
1673 }
1674 m_can_write(cdev, M_CAN_TXBTIE, 0x1);
1675
1676 can_put_echo_skb(skb, dev, 0, 0);
1677
1678 m_can_write(cdev, M_CAN_TXBAR, 0x1);
1679 /* End of xmit function for version 3.0.x */
1680 } else {
1681 /* Transmit routine for version >= v3.1.x */
1682
1683 /* Check if FIFO full */
1684 if (m_can_tx_fifo_full(cdev)) {
1685 /* This shouldn't happen */
1686 netif_stop_queue(dev);
1687 netdev_warn(dev,
1688 "TX queue active although FIFO is full.");
1689
1690 if (cdev->is_peripheral) {
1691 kfree_skb(skb);
1692 dev->stats.tx_dropped++;
1693 return NETDEV_TX_OK;
1694 } else {
1695 return NETDEV_TX_BUSY;
1696 }
1697 }
1698
1699 /* get put index for frame */
1700 putidx = FIELD_GET(TXFQS_TFQPI_MASK,
1701 m_can_read(cdev, M_CAN_TXFQS));
1702
1703 /* Construct DLC Field, with CAN-FD configuration.
1704 * Use the put index of the fifo as the message marker,
1705 * used in the TX interrupt for sending the correct echo frame.
1706 */
1707
1708 /* get CAN FD configuration of frame */
1709 fdflags = 0;
1710 if (can_is_canfd_skb(skb)) {
1711 fdflags |= TX_BUF_FDF;
1712 if (cf->flags & CANFD_BRS)
1713 fdflags |= TX_BUF_BRS;
1714 }
1715
1716 fifo_header.dlc = FIELD_PREP(TX_BUF_MM_MASK, putidx) |
1717 FIELD_PREP(TX_BUF_DLC_MASK, can_fd_len2dlc(cf->len)) |
1718 fdflags | TX_BUF_EFC;
1719 err = m_can_fifo_write(cdev, putidx, M_CAN_FIFO_ID, &fifo_header, 2);
1720 if (err)
1721 goto out_fail;
1722
1723 err = m_can_fifo_write(cdev, putidx, M_CAN_FIFO_DATA,
1724 cf->data, DIV_ROUND_UP(cf->len, 4));
1725 if (err)
1726 goto out_fail;
1727
1728 /* Push loopback echo.
1729 * Will be looped back on TX interrupt based on message marker
1730 */
1731 can_put_echo_skb(skb, dev, putidx, 0);
1732
1733 /* Enable TX FIFO element to start transfer */
1734 m_can_write(cdev, M_CAN_TXBAR, (1 << putidx));
1735
1736 /* stop network queue if fifo full */
1737 if (m_can_tx_fifo_full(cdev) ||
1738 m_can_next_echo_skb_occupied(dev, putidx))
1739 netif_stop_queue(dev);
1740 }
1741
1742 return NETDEV_TX_OK;
1743
1744 out_fail:
1745 netdev_err(dev, "FIFO write returned %d\n", err);
1746 m_can_disable_all_interrupts(cdev);
1747 return NETDEV_TX_BUSY;
1748 }
1749
m_can_tx_work_queue(struct work_struct * ws)1750 static void m_can_tx_work_queue(struct work_struct *ws)
1751 {
1752 struct m_can_classdev *cdev = container_of(ws, struct m_can_classdev,
1753 tx_work);
1754
1755 m_can_tx_handler(cdev);
1756 }
1757
m_can_start_xmit(struct sk_buff * skb,struct net_device * dev)1758 static netdev_tx_t m_can_start_xmit(struct sk_buff *skb,
1759 struct net_device *dev)
1760 {
1761 struct m_can_classdev *cdev = netdev_priv(dev);
1762
1763 if (can_dropped_invalid_skb(dev, skb))
1764 return NETDEV_TX_OK;
1765
1766 if (cdev->is_peripheral) {
1767 if (cdev->tx_skb) {
1768 netdev_err(dev, "hard_xmit called while tx busy\n");
1769 return NETDEV_TX_BUSY;
1770 }
1771
1772 if (cdev->can.state == CAN_STATE_BUS_OFF) {
1773 m_can_clean(dev);
1774 } else {
1775 /* Need to stop the queue to avoid numerous requests
1776 * from being sent. Suggested improvement is to create
1777 * a queueing mechanism that will queue the skbs and
1778 * process them in order.
1779 */
1780 cdev->tx_skb = skb;
1781 netif_stop_queue(cdev->net);
1782 queue_work(cdev->tx_wq, &cdev->tx_work);
1783 }
1784 } else {
1785 cdev->tx_skb = skb;
1786 return m_can_tx_handler(cdev);
1787 }
1788
1789 return NETDEV_TX_OK;
1790 }
1791
m_can_open(struct net_device * dev)1792 static int m_can_open(struct net_device *dev)
1793 {
1794 struct m_can_classdev *cdev = netdev_priv(dev);
1795 int err;
1796
1797 err = phy_power_on(cdev->transceiver);
1798 if (err)
1799 return err;
1800
1801 err = m_can_clk_start(cdev);
1802 if (err)
1803 goto out_phy_power_off;
1804
1805 /* open the can device */
1806 err = open_candev(dev);
1807 if (err) {
1808 netdev_err(dev, "failed to open can device\n");
1809 goto exit_disable_clks;
1810 }
1811
1812 if (cdev->is_peripheral)
1813 can_rx_offload_enable(&cdev->offload);
1814
1815 /* register interrupt handler */
1816 if (cdev->is_peripheral) {
1817 cdev->tx_skb = NULL;
1818 cdev->tx_wq = alloc_workqueue("mcan_wq",
1819 WQ_FREEZABLE | WQ_MEM_RECLAIM, 0);
1820 if (!cdev->tx_wq) {
1821 err = -ENOMEM;
1822 goto out_wq_fail;
1823 }
1824
1825 INIT_WORK(&cdev->tx_work, m_can_tx_work_queue);
1826
1827 err = request_threaded_irq(dev->irq, NULL, m_can_isr,
1828 IRQF_ONESHOT,
1829 dev->name, dev);
1830 } else {
1831 err = request_irq(dev->irq, m_can_isr, IRQF_SHARED, dev->name,
1832 dev);
1833 }
1834
1835 if (err < 0) {
1836 netdev_err(dev, "failed to request interrupt\n");
1837 goto exit_irq_fail;
1838 }
1839
1840 /* start the m_can controller */
1841 err = m_can_start(dev);
1842 if (err)
1843 goto exit_irq_fail;
1844
1845 can_led_event(dev, CAN_LED_EVENT_OPEN);
1846
1847 if (!cdev->is_peripheral)
1848 napi_enable(&cdev->napi);
1849
1850 netif_start_queue(dev);
1851
1852 return 0;
1853
1854 exit_irq_fail:
1855 if (cdev->is_peripheral)
1856 destroy_workqueue(cdev->tx_wq);
1857 out_wq_fail:
1858 if (cdev->is_peripheral)
1859 can_rx_offload_disable(&cdev->offload);
1860 close_candev(dev);
1861 exit_disable_clks:
1862 m_can_clk_stop(cdev);
1863 out_phy_power_off:
1864 phy_power_off(cdev->transceiver);
1865 return err;
1866 }
1867
1868 static const struct net_device_ops m_can_netdev_ops = {
1869 .ndo_open = m_can_open,
1870 .ndo_stop = m_can_close,
1871 .ndo_start_xmit = m_can_start_xmit,
1872 .ndo_change_mtu = can_change_mtu,
1873 };
1874
register_m_can_dev(struct net_device * dev)1875 static int register_m_can_dev(struct net_device *dev)
1876 {
1877 dev->flags |= IFF_ECHO; /* we support local echo */
1878 dev->netdev_ops = &m_can_netdev_ops;
1879
1880 return register_candev(dev);
1881 }
1882
m_can_of_parse_mram(struct m_can_classdev * cdev,const u32 * mram_config_vals)1883 static void m_can_of_parse_mram(struct m_can_classdev *cdev,
1884 const u32 *mram_config_vals)
1885 {
1886 cdev->mcfg[MRAM_SIDF].off = mram_config_vals[0];
1887 cdev->mcfg[MRAM_SIDF].num = mram_config_vals[1];
1888 cdev->mcfg[MRAM_XIDF].off = cdev->mcfg[MRAM_SIDF].off +
1889 cdev->mcfg[MRAM_SIDF].num * SIDF_ELEMENT_SIZE;
1890 cdev->mcfg[MRAM_XIDF].num = mram_config_vals[2];
1891 cdev->mcfg[MRAM_RXF0].off = cdev->mcfg[MRAM_XIDF].off +
1892 cdev->mcfg[MRAM_XIDF].num * XIDF_ELEMENT_SIZE;
1893 cdev->mcfg[MRAM_RXF0].num = mram_config_vals[3] &
1894 FIELD_MAX(RXFC_FS_MASK);
1895 cdev->mcfg[MRAM_RXF1].off = cdev->mcfg[MRAM_RXF0].off +
1896 cdev->mcfg[MRAM_RXF0].num * RXF0_ELEMENT_SIZE;
1897 cdev->mcfg[MRAM_RXF1].num = mram_config_vals[4] &
1898 FIELD_MAX(RXFC_FS_MASK);
1899 cdev->mcfg[MRAM_RXB].off = cdev->mcfg[MRAM_RXF1].off +
1900 cdev->mcfg[MRAM_RXF1].num * RXF1_ELEMENT_SIZE;
1901 cdev->mcfg[MRAM_RXB].num = mram_config_vals[5];
1902 cdev->mcfg[MRAM_TXE].off = cdev->mcfg[MRAM_RXB].off +
1903 cdev->mcfg[MRAM_RXB].num * RXB_ELEMENT_SIZE;
1904 cdev->mcfg[MRAM_TXE].num = mram_config_vals[6];
1905 cdev->mcfg[MRAM_TXB].off = cdev->mcfg[MRAM_TXE].off +
1906 cdev->mcfg[MRAM_TXE].num * TXE_ELEMENT_SIZE;
1907 cdev->mcfg[MRAM_TXB].num = mram_config_vals[7] &
1908 FIELD_MAX(TXBC_NDTB_MASK);
1909
1910 dev_dbg(cdev->dev,
1911 "sidf 0x%x %d xidf 0x%x %d rxf0 0x%x %d rxf1 0x%x %d rxb 0x%x %d txe 0x%x %d txb 0x%x %d\n",
1912 cdev->mcfg[MRAM_SIDF].off, cdev->mcfg[MRAM_SIDF].num,
1913 cdev->mcfg[MRAM_XIDF].off, cdev->mcfg[MRAM_XIDF].num,
1914 cdev->mcfg[MRAM_RXF0].off, cdev->mcfg[MRAM_RXF0].num,
1915 cdev->mcfg[MRAM_RXF1].off, cdev->mcfg[MRAM_RXF1].num,
1916 cdev->mcfg[MRAM_RXB].off, cdev->mcfg[MRAM_RXB].num,
1917 cdev->mcfg[MRAM_TXE].off, cdev->mcfg[MRAM_TXE].num,
1918 cdev->mcfg[MRAM_TXB].off, cdev->mcfg[MRAM_TXB].num);
1919 }
1920
m_can_init_ram(struct m_can_classdev * cdev)1921 int m_can_init_ram(struct m_can_classdev *cdev)
1922 {
1923 int end, i, start;
1924 int err = 0;
1925
1926 /* initialize the entire Message RAM in use to avoid possible
1927 * ECC/parity checksum errors when reading an uninitialized buffer
1928 */
1929 start = cdev->mcfg[MRAM_SIDF].off;
1930 end = cdev->mcfg[MRAM_TXB].off +
1931 cdev->mcfg[MRAM_TXB].num * TXB_ELEMENT_SIZE;
1932
1933 for (i = start; i < end; i += 4) {
1934 err = m_can_fifo_write_no_off(cdev, i, 0x0);
1935 if (err)
1936 break;
1937 }
1938
1939 return err;
1940 }
1941 EXPORT_SYMBOL_GPL(m_can_init_ram);
1942
m_can_class_get_clocks(struct m_can_classdev * cdev)1943 int m_can_class_get_clocks(struct m_can_classdev *cdev)
1944 {
1945 int ret = 0;
1946
1947 cdev->hclk = devm_clk_get(cdev->dev, "hclk");
1948 cdev->cclk = devm_clk_get(cdev->dev, "cclk");
1949
1950 if (IS_ERR(cdev->hclk) || IS_ERR(cdev->cclk)) {
1951 dev_err(cdev->dev, "no clock found\n");
1952 ret = -ENODEV;
1953 }
1954
1955 return ret;
1956 }
1957 EXPORT_SYMBOL_GPL(m_can_class_get_clocks);
1958
m_can_class_allocate_dev(struct device * dev,int sizeof_priv)1959 struct m_can_classdev *m_can_class_allocate_dev(struct device *dev,
1960 int sizeof_priv)
1961 {
1962 struct m_can_classdev *class_dev = NULL;
1963 u32 mram_config_vals[MRAM_CFG_LEN];
1964 struct net_device *net_dev;
1965 u32 tx_fifo_size;
1966 int ret;
1967
1968 ret = fwnode_property_read_u32_array(dev_fwnode(dev),
1969 "bosch,mram-cfg",
1970 mram_config_vals,
1971 sizeof(mram_config_vals) / 4);
1972 if (ret) {
1973 dev_err(dev, "Could not get Message RAM configuration.");
1974 goto out;
1975 }
1976
1977 /* Get TX FIFO size
1978 * Defines the total amount of echo buffers for loopback
1979 */
1980 tx_fifo_size = mram_config_vals[7];
1981
1982 /* allocate the m_can device */
1983 net_dev = alloc_candev(sizeof_priv, tx_fifo_size);
1984 if (!net_dev) {
1985 dev_err(dev, "Failed to allocate CAN device");
1986 goto out;
1987 }
1988
1989 class_dev = netdev_priv(net_dev);
1990 class_dev->net = net_dev;
1991 class_dev->dev = dev;
1992 SET_NETDEV_DEV(net_dev, dev);
1993
1994 m_can_of_parse_mram(class_dev, mram_config_vals);
1995 out:
1996 return class_dev;
1997 }
1998 EXPORT_SYMBOL_GPL(m_can_class_allocate_dev);
1999
m_can_class_free_dev(struct net_device * net)2000 void m_can_class_free_dev(struct net_device *net)
2001 {
2002 free_candev(net);
2003 }
2004 EXPORT_SYMBOL_GPL(m_can_class_free_dev);
2005
m_can_class_register(struct m_can_classdev * cdev)2006 int m_can_class_register(struct m_can_classdev *cdev)
2007 {
2008 int ret;
2009
2010 if (cdev->pm_clock_support) {
2011 ret = m_can_clk_start(cdev);
2012 if (ret)
2013 return ret;
2014 }
2015
2016 if (cdev->is_peripheral) {
2017 ret = can_rx_offload_add_manual(cdev->net, &cdev->offload,
2018 M_CAN_NAPI_WEIGHT);
2019 if (ret)
2020 goto clk_disable;
2021 }
2022
2023 ret = m_can_dev_setup(cdev);
2024 if (ret)
2025 goto rx_offload_del;
2026
2027 ret = register_m_can_dev(cdev->net);
2028 if (ret) {
2029 dev_err(cdev->dev, "registering %s failed (err=%d)\n",
2030 cdev->net->name, ret);
2031 goto rx_offload_del;
2032 }
2033
2034 devm_can_led_init(cdev->net);
2035
2036 of_can_transceiver(cdev->net);
2037
2038 dev_info(cdev->dev, "%s device registered (irq=%d, version=%d)\n",
2039 KBUILD_MODNAME, cdev->net->irq, cdev->version);
2040
2041 /* Probe finished
2042 * Stop clocks. They will be reactivated once the M_CAN device is opened
2043 */
2044 m_can_clk_stop(cdev);
2045
2046 return 0;
2047
2048 rx_offload_del:
2049 if (cdev->is_peripheral)
2050 can_rx_offload_del(&cdev->offload);
2051 clk_disable:
2052 m_can_clk_stop(cdev);
2053
2054 return ret;
2055 }
2056 EXPORT_SYMBOL_GPL(m_can_class_register);
2057
m_can_class_unregister(struct m_can_classdev * cdev)2058 void m_can_class_unregister(struct m_can_classdev *cdev)
2059 {
2060 if (cdev->is_peripheral)
2061 can_rx_offload_del(&cdev->offload);
2062 unregister_candev(cdev->net);
2063 }
2064 EXPORT_SYMBOL_GPL(m_can_class_unregister);
2065
m_can_class_suspend(struct device * dev)2066 int m_can_class_suspend(struct device *dev)
2067 {
2068 struct m_can_classdev *cdev = dev_get_drvdata(dev);
2069 struct net_device *ndev = cdev->net;
2070
2071 if (netif_running(ndev)) {
2072 netif_stop_queue(ndev);
2073 netif_device_detach(ndev);
2074 m_can_stop(ndev);
2075 m_can_clk_stop(cdev);
2076 }
2077
2078 pinctrl_pm_select_sleep_state(dev);
2079
2080 cdev->can.state = CAN_STATE_SLEEPING;
2081
2082 return 0;
2083 }
2084 EXPORT_SYMBOL_GPL(m_can_class_suspend);
2085
m_can_class_resume(struct device * dev)2086 int m_can_class_resume(struct device *dev)
2087 {
2088 struct m_can_classdev *cdev = dev_get_drvdata(dev);
2089 struct net_device *ndev = cdev->net;
2090
2091 pinctrl_pm_select_default_state(dev);
2092
2093 cdev->can.state = CAN_STATE_ERROR_ACTIVE;
2094
2095 if (netif_running(ndev)) {
2096 int ret;
2097
2098 ret = m_can_clk_start(cdev);
2099 if (ret)
2100 return ret;
2101 ret = m_can_start(ndev);
2102 if (ret) {
2103 m_can_clk_stop(cdev);
2104
2105 return ret;
2106 }
2107
2108 netif_device_attach(ndev);
2109 netif_start_queue(ndev);
2110 }
2111
2112 return 0;
2113 }
2114 EXPORT_SYMBOL_GPL(m_can_class_resume);
2115
2116 MODULE_AUTHOR("Dong Aisheng <b29396@freescale.com>");
2117 MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
2118 MODULE_LICENSE("GPL v2");
2119 MODULE_DESCRIPTION("CAN bus driver for Bosch M_CAN controller");
2120