rcar_can.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. /* Renesas R-Car CAN device driver
  2. *
  3. * Copyright (C) 2013 Cogent Embedded, Inc. <source@cogentembedded.com>
  4. * Copyright (C) 2013 Renesas Solutions Corp.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/types.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/errno.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/can/led.h>
  19. #include <linux/can/dev.h>
  20. #include <linux/clk.h>
  21. #include <linux/can/platform/rcar_can.h>
  22. #include <linux/of.h>
  23. #define RCAR_CAN_DRV_NAME "rcar_can"
  24. #define RCAR_SUPPORTED_CLOCKS (BIT(CLKR_CLKP1) | BIT(CLKR_CLKP2) | \
  25. BIT(CLKR_CLKEXT))
  26. /* Mailbox configuration:
  27. * mailbox 60 - 63 - Rx FIFO mailboxes
  28. * mailbox 56 - 59 - Tx FIFO mailboxes
  29. * non-FIFO mailboxes are not used
  30. */
  31. #define RCAR_CAN_N_MBX 64 /* Number of mailboxes in non-FIFO mode */
  32. #define RCAR_CAN_RX_FIFO_MBX 60 /* Mailbox - window to Rx FIFO */
  33. #define RCAR_CAN_TX_FIFO_MBX 56 /* Mailbox - window to Tx FIFO */
  34. #define RCAR_CAN_FIFO_DEPTH 4
  35. /* Mailbox registers structure */
  36. struct rcar_can_mbox_regs {
  37. u32 id; /* IDE and RTR bits, SID and EID */
  38. u8 stub; /* Not used */
  39. u8 dlc; /* Data Length Code - bits [0..3] */
  40. u8 data[8]; /* Data Bytes */
  41. u8 tsh; /* Time Stamp Higher Byte */
  42. u8 tsl; /* Time Stamp Lower Byte */
  43. };
  44. struct rcar_can_regs {
  45. struct rcar_can_mbox_regs mb[RCAR_CAN_N_MBX]; /* Mailbox registers */
  46. u32 mkr_2_9[8]; /* Mask Registers 2-9 */
  47. u32 fidcr[2]; /* FIFO Received ID Compare Register */
  48. u32 mkivlr1; /* Mask Invalid Register 1 */
  49. u32 mier1; /* Mailbox Interrupt Enable Register 1 */
  50. u32 mkr_0_1[2]; /* Mask Registers 0-1 */
  51. u32 mkivlr0; /* Mask Invalid Register 0*/
  52. u32 mier0; /* Mailbox Interrupt Enable Register 0 */
  53. u8 pad_440[0x3c0];
  54. u8 mctl[64]; /* Message Control Registers */
  55. u16 ctlr; /* Control Register */
  56. u16 str; /* Status register */
  57. u8 bcr[3]; /* Bit Configuration Register */
  58. u8 clkr; /* Clock Select Register */
  59. u8 rfcr; /* Receive FIFO Control Register */
  60. u8 rfpcr; /* Receive FIFO Pointer Control Register */
  61. u8 tfcr; /* Transmit FIFO Control Register */
  62. u8 tfpcr; /* Transmit FIFO Pointer Control Register */
  63. u8 eier; /* Error Interrupt Enable Register */
  64. u8 eifr; /* Error Interrupt Factor Judge Register */
  65. u8 recr; /* Receive Error Count Register */
  66. u8 tecr; /* Transmit Error Count Register */
  67. u8 ecsr; /* Error Code Store Register */
  68. u8 cssr; /* Channel Search Support Register */
  69. u8 mssr; /* Mailbox Search Status Register */
  70. u8 msmr; /* Mailbox Search Mode Register */
  71. u16 tsr; /* Time Stamp Register */
  72. u8 afsr; /* Acceptance Filter Support Register */
  73. u8 pad_857;
  74. u8 tcr; /* Test Control Register */
  75. u8 pad_859[7];
  76. u8 ier; /* Interrupt Enable Register */
  77. u8 isr; /* Interrupt Status Register */
  78. u8 pad_862;
  79. u8 mbsmr; /* Mailbox Search Mask Register */
  80. };
  81. struct rcar_can_priv {
  82. struct can_priv can; /* Must be the first member! */
  83. struct net_device *ndev;
  84. struct napi_struct napi;
  85. struct rcar_can_regs __iomem *regs;
  86. struct clk *clk;
  87. struct clk *can_clk;
  88. u8 tx_dlc[RCAR_CAN_FIFO_DEPTH];
  89. u32 tx_head;
  90. u32 tx_tail;
  91. u8 clock_select;
  92. u8 ier;
  93. };
  94. static const struct can_bittiming_const rcar_can_bittiming_const = {
  95. .name = RCAR_CAN_DRV_NAME,
  96. .tseg1_min = 4,
  97. .tseg1_max = 16,
  98. .tseg2_min = 2,
  99. .tseg2_max = 8,
  100. .sjw_max = 4,
  101. .brp_min = 1,
  102. .brp_max = 1024,
  103. .brp_inc = 1,
  104. };
  105. /* Control Register bits */
  106. #define RCAR_CAN_CTLR_BOM (3 << 11) /* Bus-Off Recovery Mode Bits */
  107. #define RCAR_CAN_CTLR_BOM_ENT (1 << 11) /* Entry to halt mode */
  108. /* at bus-off entry */
  109. #define RCAR_CAN_CTLR_SLPM (1 << 10)
  110. #define RCAR_CAN_CTLR_CANM (3 << 8) /* Operating Mode Select Bit */
  111. #define RCAR_CAN_CTLR_CANM_HALT (1 << 9)
  112. #define RCAR_CAN_CTLR_CANM_RESET (1 << 8)
  113. #define RCAR_CAN_CTLR_CANM_FORCE_RESET (3 << 8)
  114. #define RCAR_CAN_CTLR_MLM (1 << 3) /* Message Lost Mode Select */
  115. #define RCAR_CAN_CTLR_IDFM (3 << 1) /* ID Format Mode Select Bits */
  116. #define RCAR_CAN_CTLR_IDFM_MIXED (1 << 2) /* Mixed ID mode */
  117. #define RCAR_CAN_CTLR_MBM (1 << 0) /* Mailbox Mode select */
  118. /* Status Register bits */
  119. #define RCAR_CAN_STR_RSTST (1 << 8) /* Reset Status Bit */
  120. /* FIFO Received ID Compare Registers 0 and 1 bits */
  121. #define RCAR_CAN_FIDCR_IDE (1 << 31) /* ID Extension Bit */
  122. #define RCAR_CAN_FIDCR_RTR (1 << 30) /* Remote Transmission Request Bit */
  123. /* Receive FIFO Control Register bits */
  124. #define RCAR_CAN_RFCR_RFEST (1 << 7) /* Receive FIFO Empty Status Flag */
  125. #define RCAR_CAN_RFCR_RFE (1 << 0) /* Receive FIFO Enable */
  126. /* Transmit FIFO Control Register bits */
  127. #define RCAR_CAN_TFCR_TFUST (7 << 1) /* Transmit FIFO Unsent Message */
  128. /* Number Status Bits */
  129. #define RCAR_CAN_TFCR_TFUST_SHIFT 1 /* Offset of Transmit FIFO Unsent */
  130. /* Message Number Status Bits */
  131. #define RCAR_CAN_TFCR_TFE (1 << 0) /* Transmit FIFO Enable */
  132. #define RCAR_CAN_N_RX_MKREGS1 2 /* Number of mask registers */
  133. /* for Rx mailboxes 0-31 */
  134. #define RCAR_CAN_N_RX_MKREGS2 8
  135. /* Bit Configuration Register settings */
  136. #define RCAR_CAN_BCR_TSEG1(x) (((x) & 0x0f) << 20)
  137. #define RCAR_CAN_BCR_BPR(x) (((x) & 0x3ff) << 8)
  138. #define RCAR_CAN_BCR_SJW(x) (((x) & 0x3) << 4)
  139. #define RCAR_CAN_BCR_TSEG2(x) ((x) & 0x07)
  140. /* Mailbox and Mask Registers bits */
  141. #define RCAR_CAN_IDE (1 << 31)
  142. #define RCAR_CAN_RTR (1 << 30)
  143. #define RCAR_CAN_SID_SHIFT 18
  144. /* Mailbox Interrupt Enable Register 1 bits */
  145. #define RCAR_CAN_MIER1_RXFIE (1 << 28) /* Receive FIFO Interrupt Enable */
  146. #define RCAR_CAN_MIER1_TXFIE (1 << 24) /* Transmit FIFO Interrupt Enable */
  147. /* Interrupt Enable Register bits */
  148. #define RCAR_CAN_IER_ERSIE (1 << 5) /* Error (ERS) Interrupt Enable Bit */
  149. #define RCAR_CAN_IER_RXFIE (1 << 4) /* Reception FIFO Interrupt */
  150. /* Enable Bit */
  151. #define RCAR_CAN_IER_TXFIE (1 << 3) /* Transmission FIFO Interrupt */
  152. /* Enable Bit */
  153. /* Interrupt Status Register bits */
  154. #define RCAR_CAN_ISR_ERSF (1 << 5) /* Error (ERS) Interrupt Status Bit */
  155. #define RCAR_CAN_ISR_RXFF (1 << 4) /* Reception FIFO Interrupt */
  156. /* Status Bit */
  157. #define RCAR_CAN_ISR_TXFF (1 << 3) /* Transmission FIFO Interrupt */
  158. /* Status Bit */
  159. /* Error Interrupt Enable Register bits */
  160. #define RCAR_CAN_EIER_BLIE (1 << 7) /* Bus Lock Interrupt Enable */
  161. #define RCAR_CAN_EIER_OLIE (1 << 6) /* Overload Frame Transmit */
  162. /* Interrupt Enable */
  163. #define RCAR_CAN_EIER_ORIE (1 << 5) /* Receive Overrun Interrupt Enable */
  164. #define RCAR_CAN_EIER_BORIE (1 << 4) /* Bus-Off Recovery Interrupt Enable */
  165. #define RCAR_CAN_EIER_BOEIE (1 << 3) /* Bus-Off Entry Interrupt Enable */
  166. #define RCAR_CAN_EIER_EPIE (1 << 2) /* Error Passive Interrupt Enable */
  167. #define RCAR_CAN_EIER_EWIE (1 << 1) /* Error Warning Interrupt Enable */
  168. #define RCAR_CAN_EIER_BEIE (1 << 0) /* Bus Error Interrupt Enable */
  169. /* Error Interrupt Factor Judge Register bits */
  170. #define RCAR_CAN_EIFR_BLIF (1 << 7) /* Bus Lock Detect Flag */
  171. #define RCAR_CAN_EIFR_OLIF (1 << 6) /* Overload Frame Transmission */
  172. /* Detect Flag */
  173. #define RCAR_CAN_EIFR_ORIF (1 << 5) /* Receive Overrun Detect Flag */
  174. #define RCAR_CAN_EIFR_BORIF (1 << 4) /* Bus-Off Recovery Detect Flag */
  175. #define RCAR_CAN_EIFR_BOEIF (1 << 3) /* Bus-Off Entry Detect Flag */
  176. #define RCAR_CAN_EIFR_EPIF (1 << 2) /* Error Passive Detect Flag */
  177. #define RCAR_CAN_EIFR_EWIF (1 << 1) /* Error Warning Detect Flag */
  178. #define RCAR_CAN_EIFR_BEIF (1 << 0) /* Bus Error Detect Flag */
  179. /* Error Code Store Register bits */
  180. #define RCAR_CAN_ECSR_EDPM (1 << 7) /* Error Display Mode Select Bit */
  181. #define RCAR_CAN_ECSR_ADEF (1 << 6) /* ACK Delimiter Error Flag */
  182. #define RCAR_CAN_ECSR_BE0F (1 << 5) /* Bit Error (dominant) Flag */
  183. #define RCAR_CAN_ECSR_BE1F (1 << 4) /* Bit Error (recessive) Flag */
  184. #define RCAR_CAN_ECSR_CEF (1 << 3) /* CRC Error Flag */
  185. #define RCAR_CAN_ECSR_AEF (1 << 2) /* ACK Error Flag */
  186. #define RCAR_CAN_ECSR_FEF (1 << 1) /* Form Error Flag */
  187. #define RCAR_CAN_ECSR_SEF (1 << 0) /* Stuff Error Flag */
  188. #define RCAR_CAN_NAPI_WEIGHT 4
  189. #define MAX_STR_READS 0x100
  190. static void tx_failure_cleanup(struct net_device *ndev)
  191. {
  192. int i;
  193. for (i = 0; i < RCAR_CAN_FIFO_DEPTH; i++)
  194. can_free_echo_skb(ndev, i);
  195. }
  196. static void rcar_can_error(struct net_device *ndev)
  197. {
  198. struct rcar_can_priv *priv = netdev_priv(ndev);
  199. struct net_device_stats *stats = &ndev->stats;
  200. struct can_frame *cf;
  201. struct sk_buff *skb;
  202. u8 eifr, txerr = 0, rxerr = 0;
  203. /* Propagate the error condition to the CAN stack */
  204. skb = alloc_can_err_skb(ndev, &cf);
  205. eifr = readb(&priv->regs->eifr);
  206. if (eifr & (RCAR_CAN_EIFR_EWIF | RCAR_CAN_EIFR_EPIF)) {
  207. txerr = readb(&priv->regs->tecr);
  208. rxerr = readb(&priv->regs->recr);
  209. if (skb) {
  210. cf->can_id |= CAN_ERR_CRTL;
  211. cf->data[6] = txerr;
  212. cf->data[7] = rxerr;
  213. }
  214. }
  215. if (eifr & RCAR_CAN_EIFR_BEIF) {
  216. int rx_errors = 0, tx_errors = 0;
  217. u8 ecsr;
  218. netdev_dbg(priv->ndev, "Bus error interrupt:\n");
  219. if (skb)
  220. cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT;
  221. ecsr = readb(&priv->regs->ecsr);
  222. if (ecsr & RCAR_CAN_ECSR_ADEF) {
  223. netdev_dbg(priv->ndev, "ACK Delimiter Error\n");
  224. tx_errors++;
  225. writeb(~RCAR_CAN_ECSR_ADEF, &priv->regs->ecsr);
  226. if (skb)
  227. cf->data[3] = CAN_ERR_PROT_LOC_ACK_DEL;
  228. }
  229. if (ecsr & RCAR_CAN_ECSR_BE0F) {
  230. netdev_dbg(priv->ndev, "Bit Error (dominant)\n");
  231. tx_errors++;
  232. writeb(~RCAR_CAN_ECSR_BE0F, &priv->regs->ecsr);
  233. if (skb)
  234. cf->data[2] |= CAN_ERR_PROT_BIT0;
  235. }
  236. if (ecsr & RCAR_CAN_ECSR_BE1F) {
  237. netdev_dbg(priv->ndev, "Bit Error (recessive)\n");
  238. tx_errors++;
  239. writeb(~RCAR_CAN_ECSR_BE1F, &priv->regs->ecsr);
  240. if (skb)
  241. cf->data[2] |= CAN_ERR_PROT_BIT1;
  242. }
  243. if (ecsr & RCAR_CAN_ECSR_CEF) {
  244. netdev_dbg(priv->ndev, "CRC Error\n");
  245. rx_errors++;
  246. writeb(~RCAR_CAN_ECSR_CEF, &priv->regs->ecsr);
  247. if (skb)
  248. cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
  249. }
  250. if (ecsr & RCAR_CAN_ECSR_AEF) {
  251. netdev_dbg(priv->ndev, "ACK Error\n");
  252. tx_errors++;
  253. writeb(~RCAR_CAN_ECSR_AEF, &priv->regs->ecsr);
  254. if (skb) {
  255. cf->can_id |= CAN_ERR_ACK;
  256. cf->data[3] = CAN_ERR_PROT_LOC_ACK;
  257. }
  258. }
  259. if (ecsr & RCAR_CAN_ECSR_FEF) {
  260. netdev_dbg(priv->ndev, "Form Error\n");
  261. rx_errors++;
  262. writeb(~RCAR_CAN_ECSR_FEF, &priv->regs->ecsr);
  263. if (skb)
  264. cf->data[2] |= CAN_ERR_PROT_FORM;
  265. }
  266. if (ecsr & RCAR_CAN_ECSR_SEF) {
  267. netdev_dbg(priv->ndev, "Stuff Error\n");
  268. rx_errors++;
  269. writeb(~RCAR_CAN_ECSR_SEF, &priv->regs->ecsr);
  270. if (skb)
  271. cf->data[2] |= CAN_ERR_PROT_STUFF;
  272. }
  273. priv->can.can_stats.bus_error++;
  274. ndev->stats.rx_errors += rx_errors;
  275. ndev->stats.tx_errors += tx_errors;
  276. writeb(~RCAR_CAN_EIFR_BEIF, &priv->regs->eifr);
  277. }
  278. if (eifr & RCAR_CAN_EIFR_EWIF) {
  279. netdev_dbg(priv->ndev, "Error warning interrupt\n");
  280. priv->can.state = CAN_STATE_ERROR_WARNING;
  281. priv->can.can_stats.error_warning++;
  282. /* Clear interrupt condition */
  283. writeb(~RCAR_CAN_EIFR_EWIF, &priv->regs->eifr);
  284. if (skb)
  285. cf->data[1] = txerr > rxerr ? CAN_ERR_CRTL_TX_WARNING :
  286. CAN_ERR_CRTL_RX_WARNING;
  287. }
  288. if (eifr & RCAR_CAN_EIFR_EPIF) {
  289. netdev_dbg(priv->ndev, "Error passive interrupt\n");
  290. priv->can.state = CAN_STATE_ERROR_PASSIVE;
  291. priv->can.can_stats.error_passive++;
  292. /* Clear interrupt condition */
  293. writeb(~RCAR_CAN_EIFR_EPIF, &priv->regs->eifr);
  294. if (skb)
  295. cf->data[1] = txerr > rxerr ? CAN_ERR_CRTL_TX_PASSIVE :
  296. CAN_ERR_CRTL_RX_PASSIVE;
  297. }
  298. if (eifr & RCAR_CAN_EIFR_BOEIF) {
  299. netdev_dbg(priv->ndev, "Bus-off entry interrupt\n");
  300. tx_failure_cleanup(ndev);
  301. priv->ier = RCAR_CAN_IER_ERSIE;
  302. writeb(priv->ier, &priv->regs->ier);
  303. priv->can.state = CAN_STATE_BUS_OFF;
  304. /* Clear interrupt condition */
  305. writeb(~RCAR_CAN_EIFR_BOEIF, &priv->regs->eifr);
  306. priv->can.can_stats.bus_off++;
  307. can_bus_off(ndev);
  308. if (skb)
  309. cf->can_id |= CAN_ERR_BUSOFF;
  310. }
  311. if (eifr & RCAR_CAN_EIFR_ORIF) {
  312. netdev_dbg(priv->ndev, "Receive overrun error interrupt\n");
  313. ndev->stats.rx_over_errors++;
  314. ndev->stats.rx_errors++;
  315. writeb(~RCAR_CAN_EIFR_ORIF, &priv->regs->eifr);
  316. if (skb) {
  317. cf->can_id |= CAN_ERR_CRTL;
  318. cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
  319. }
  320. }
  321. if (eifr & RCAR_CAN_EIFR_OLIF) {
  322. netdev_dbg(priv->ndev,
  323. "Overload Frame Transmission error interrupt\n");
  324. ndev->stats.rx_over_errors++;
  325. ndev->stats.rx_errors++;
  326. writeb(~RCAR_CAN_EIFR_OLIF, &priv->regs->eifr);
  327. if (skb) {
  328. cf->can_id |= CAN_ERR_PROT;
  329. cf->data[2] |= CAN_ERR_PROT_OVERLOAD;
  330. }
  331. }
  332. if (skb) {
  333. stats->rx_packets++;
  334. stats->rx_bytes += cf->can_dlc;
  335. netif_rx(skb);
  336. }
  337. }
  338. static void rcar_can_tx_done(struct net_device *ndev)
  339. {
  340. struct rcar_can_priv *priv = netdev_priv(ndev);
  341. struct net_device_stats *stats = &ndev->stats;
  342. u8 isr;
  343. while (1) {
  344. u8 unsent = readb(&priv->regs->tfcr);
  345. unsent = (unsent & RCAR_CAN_TFCR_TFUST) >>
  346. RCAR_CAN_TFCR_TFUST_SHIFT;
  347. if (priv->tx_head - priv->tx_tail <= unsent)
  348. break;
  349. stats->tx_packets++;
  350. stats->tx_bytes += priv->tx_dlc[priv->tx_tail %
  351. RCAR_CAN_FIFO_DEPTH];
  352. priv->tx_dlc[priv->tx_tail % RCAR_CAN_FIFO_DEPTH] = 0;
  353. can_get_echo_skb(ndev, priv->tx_tail % RCAR_CAN_FIFO_DEPTH);
  354. priv->tx_tail++;
  355. netif_wake_queue(ndev);
  356. }
  357. /* Clear interrupt */
  358. isr = readb(&priv->regs->isr);
  359. writeb(isr & ~RCAR_CAN_ISR_TXFF, &priv->regs->isr);
  360. can_led_event(ndev, CAN_LED_EVENT_TX);
  361. }
  362. static irqreturn_t rcar_can_interrupt(int irq, void *dev_id)
  363. {
  364. struct net_device *ndev = dev_id;
  365. struct rcar_can_priv *priv = netdev_priv(ndev);
  366. u8 isr;
  367. isr = readb(&priv->regs->isr);
  368. if (!(isr & priv->ier))
  369. return IRQ_NONE;
  370. if (isr & RCAR_CAN_ISR_ERSF)
  371. rcar_can_error(ndev);
  372. if (isr & RCAR_CAN_ISR_TXFF)
  373. rcar_can_tx_done(ndev);
  374. if (isr & RCAR_CAN_ISR_RXFF) {
  375. if (napi_schedule_prep(&priv->napi)) {
  376. /* Disable Rx FIFO interrupts */
  377. priv->ier &= ~RCAR_CAN_IER_RXFIE;
  378. writeb(priv->ier, &priv->regs->ier);
  379. __napi_schedule(&priv->napi);
  380. }
  381. }
  382. return IRQ_HANDLED;
  383. }
  384. static void rcar_can_set_bittiming(struct net_device *dev)
  385. {
  386. struct rcar_can_priv *priv = netdev_priv(dev);
  387. struct can_bittiming *bt = &priv->can.bittiming;
  388. u32 bcr;
  389. bcr = RCAR_CAN_BCR_TSEG1(bt->phase_seg1 + bt->prop_seg - 1) |
  390. RCAR_CAN_BCR_BPR(bt->brp - 1) | RCAR_CAN_BCR_SJW(bt->sjw - 1) |
  391. RCAR_CAN_BCR_TSEG2(bt->phase_seg2 - 1);
  392. /* Don't overwrite CLKR with 32-bit BCR access; CLKR has 8-bit access.
  393. * All the registers are big-endian but they get byte-swapped on 32-bit
  394. * read/write (but not on 8-bit, contrary to the manuals)...
  395. */
  396. writel((bcr << 8) | priv->clock_select, &priv->regs->bcr);
  397. }
  398. static void rcar_can_start(struct net_device *ndev)
  399. {
  400. struct rcar_can_priv *priv = netdev_priv(ndev);
  401. u16 ctlr;
  402. int i;
  403. /* Set controller to known mode:
  404. * - FIFO mailbox mode
  405. * - accept all messages
  406. * - overrun mode
  407. * CAN is in sleep mode after MCU hardware or software reset.
  408. */
  409. ctlr = readw(&priv->regs->ctlr);
  410. ctlr &= ~RCAR_CAN_CTLR_SLPM;
  411. writew(ctlr, &priv->regs->ctlr);
  412. /* Go to reset mode */
  413. ctlr |= RCAR_CAN_CTLR_CANM_FORCE_RESET;
  414. writew(ctlr, &priv->regs->ctlr);
  415. for (i = 0; i < MAX_STR_READS; i++) {
  416. if (readw(&priv->regs->str) & RCAR_CAN_STR_RSTST)
  417. break;
  418. }
  419. rcar_can_set_bittiming(ndev);
  420. ctlr |= RCAR_CAN_CTLR_IDFM_MIXED; /* Select mixed ID mode */
  421. ctlr |= RCAR_CAN_CTLR_BOM_ENT; /* Entry to halt mode automatically */
  422. /* at bus-off */
  423. ctlr |= RCAR_CAN_CTLR_MBM; /* Select FIFO mailbox mode */
  424. ctlr |= RCAR_CAN_CTLR_MLM; /* Overrun mode */
  425. writew(ctlr, &priv->regs->ctlr);
  426. /* Accept all SID and EID */
  427. writel(0, &priv->regs->mkr_2_9[6]);
  428. writel(0, &priv->regs->mkr_2_9[7]);
  429. /* In FIFO mailbox mode, write "0" to bits 24 to 31 */
  430. writel(0, &priv->regs->mkivlr1);
  431. /* Accept all frames */
  432. writel(0, &priv->regs->fidcr[0]);
  433. writel(RCAR_CAN_FIDCR_IDE | RCAR_CAN_FIDCR_RTR, &priv->regs->fidcr[1]);
  434. /* Enable and configure FIFO mailbox interrupts */
  435. writel(RCAR_CAN_MIER1_RXFIE | RCAR_CAN_MIER1_TXFIE, &priv->regs->mier1);
  436. priv->ier = RCAR_CAN_IER_ERSIE | RCAR_CAN_IER_RXFIE |
  437. RCAR_CAN_IER_TXFIE;
  438. writeb(priv->ier, &priv->regs->ier);
  439. /* Accumulate error codes */
  440. writeb(RCAR_CAN_ECSR_EDPM, &priv->regs->ecsr);
  441. /* Enable error interrupts */
  442. writeb(RCAR_CAN_EIER_EWIE | RCAR_CAN_EIER_EPIE | RCAR_CAN_EIER_BOEIE |
  443. (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING ?
  444. RCAR_CAN_EIER_BEIE : 0) | RCAR_CAN_EIER_ORIE |
  445. RCAR_CAN_EIER_OLIE, &priv->regs->eier);
  446. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  447. /* Go to operation mode */
  448. writew(ctlr & ~RCAR_CAN_CTLR_CANM, &priv->regs->ctlr);
  449. for (i = 0; i < MAX_STR_READS; i++) {
  450. if (!(readw(&priv->regs->str) & RCAR_CAN_STR_RSTST))
  451. break;
  452. }
  453. /* Enable Rx and Tx FIFO */
  454. writeb(RCAR_CAN_RFCR_RFE, &priv->regs->rfcr);
  455. writeb(RCAR_CAN_TFCR_TFE, &priv->regs->tfcr);
  456. }
  457. static int rcar_can_open(struct net_device *ndev)
  458. {
  459. struct rcar_can_priv *priv = netdev_priv(ndev);
  460. int err;
  461. err = clk_prepare_enable(priv->clk);
  462. if (err) {
  463. netdev_err(ndev,
  464. "failed to enable peripheral clock, error %d\n",
  465. err);
  466. goto out;
  467. }
  468. err = clk_prepare_enable(priv->can_clk);
  469. if (err) {
  470. netdev_err(ndev, "failed to enable CAN clock, error %d\n",
  471. err);
  472. goto out_clock;
  473. }
  474. err = open_candev(ndev);
  475. if (err) {
  476. netdev_err(ndev, "open_candev() failed, error %d\n", err);
  477. goto out_can_clock;
  478. }
  479. napi_enable(&priv->napi);
  480. err = request_irq(ndev->irq, rcar_can_interrupt, 0, ndev->name, ndev);
  481. if (err) {
  482. netdev_err(ndev, "request_irq(%d) failed, error %d\n",
  483. ndev->irq, err);
  484. goto out_close;
  485. }
  486. can_led_event(ndev, CAN_LED_EVENT_OPEN);
  487. rcar_can_start(ndev);
  488. netif_start_queue(ndev);
  489. return 0;
  490. out_close:
  491. napi_disable(&priv->napi);
  492. close_candev(ndev);
  493. out_can_clock:
  494. clk_disable_unprepare(priv->can_clk);
  495. out_clock:
  496. clk_disable_unprepare(priv->clk);
  497. out:
  498. return err;
  499. }
  500. static void rcar_can_stop(struct net_device *ndev)
  501. {
  502. struct rcar_can_priv *priv = netdev_priv(ndev);
  503. u16 ctlr;
  504. int i;
  505. /* Go to (force) reset mode */
  506. ctlr = readw(&priv->regs->ctlr);
  507. ctlr |= RCAR_CAN_CTLR_CANM_FORCE_RESET;
  508. writew(ctlr, &priv->regs->ctlr);
  509. for (i = 0; i < MAX_STR_READS; i++) {
  510. if (readw(&priv->regs->str) & RCAR_CAN_STR_RSTST)
  511. break;
  512. }
  513. writel(0, &priv->regs->mier0);
  514. writel(0, &priv->regs->mier1);
  515. writeb(0, &priv->regs->ier);
  516. writeb(0, &priv->regs->eier);
  517. /* Go to sleep mode */
  518. ctlr |= RCAR_CAN_CTLR_SLPM;
  519. writew(ctlr, &priv->regs->ctlr);
  520. priv->can.state = CAN_STATE_STOPPED;
  521. }
  522. static int rcar_can_close(struct net_device *ndev)
  523. {
  524. struct rcar_can_priv *priv = netdev_priv(ndev);
  525. netif_stop_queue(ndev);
  526. rcar_can_stop(ndev);
  527. free_irq(ndev->irq, ndev);
  528. napi_disable(&priv->napi);
  529. clk_disable_unprepare(priv->can_clk);
  530. clk_disable_unprepare(priv->clk);
  531. close_candev(ndev);
  532. can_led_event(ndev, CAN_LED_EVENT_STOP);
  533. return 0;
  534. }
  535. static netdev_tx_t rcar_can_start_xmit(struct sk_buff *skb,
  536. struct net_device *ndev)
  537. {
  538. struct rcar_can_priv *priv = netdev_priv(ndev);
  539. struct can_frame *cf = (struct can_frame *)skb->data;
  540. u32 data, i;
  541. if (can_dropped_invalid_skb(ndev, skb))
  542. return NETDEV_TX_OK;
  543. if (cf->can_id & CAN_EFF_FLAG) /* Extended frame format */
  544. data = (cf->can_id & CAN_EFF_MASK) | RCAR_CAN_IDE;
  545. else /* Standard frame format */
  546. data = (cf->can_id & CAN_SFF_MASK) << RCAR_CAN_SID_SHIFT;
  547. if (cf->can_id & CAN_RTR_FLAG) { /* Remote transmission request */
  548. data |= RCAR_CAN_RTR;
  549. } else {
  550. for (i = 0; i < cf->can_dlc; i++)
  551. writeb(cf->data[i],
  552. &priv->regs->mb[RCAR_CAN_TX_FIFO_MBX].data[i]);
  553. }
  554. writel(data, &priv->regs->mb[RCAR_CAN_TX_FIFO_MBX].id);
  555. writeb(cf->can_dlc, &priv->regs->mb[RCAR_CAN_TX_FIFO_MBX].dlc);
  556. priv->tx_dlc[priv->tx_head % RCAR_CAN_FIFO_DEPTH] = cf->can_dlc;
  557. can_put_echo_skb(skb, ndev, priv->tx_head % RCAR_CAN_FIFO_DEPTH);
  558. priv->tx_head++;
  559. /* Start Tx: write 0xff to the TFPCR register to increment
  560. * the CPU-side pointer for the transmit FIFO to the next
  561. * mailbox location
  562. */
  563. writeb(0xff, &priv->regs->tfpcr);
  564. /* Stop the queue if we've filled all FIFO entries */
  565. if (priv->tx_head - priv->tx_tail >= RCAR_CAN_FIFO_DEPTH)
  566. netif_stop_queue(ndev);
  567. return NETDEV_TX_OK;
  568. }
  569. static const struct net_device_ops rcar_can_netdev_ops = {
  570. .ndo_open = rcar_can_open,
  571. .ndo_stop = rcar_can_close,
  572. .ndo_start_xmit = rcar_can_start_xmit,
  573. .ndo_change_mtu = can_change_mtu,
  574. };
  575. static void rcar_can_rx_pkt(struct rcar_can_priv *priv)
  576. {
  577. struct net_device_stats *stats = &priv->ndev->stats;
  578. struct can_frame *cf;
  579. struct sk_buff *skb;
  580. u32 data;
  581. u8 dlc;
  582. skb = alloc_can_skb(priv->ndev, &cf);
  583. if (!skb) {
  584. stats->rx_dropped++;
  585. return;
  586. }
  587. data = readl(&priv->regs->mb[RCAR_CAN_RX_FIFO_MBX].id);
  588. if (data & RCAR_CAN_IDE)
  589. cf->can_id = (data & CAN_EFF_MASK) | CAN_EFF_FLAG;
  590. else
  591. cf->can_id = (data >> RCAR_CAN_SID_SHIFT) & CAN_SFF_MASK;
  592. dlc = readb(&priv->regs->mb[RCAR_CAN_RX_FIFO_MBX].dlc);
  593. cf->can_dlc = get_can_dlc(dlc);
  594. if (data & RCAR_CAN_RTR) {
  595. cf->can_id |= CAN_RTR_FLAG;
  596. } else {
  597. for (dlc = 0; dlc < cf->can_dlc; dlc++)
  598. cf->data[dlc] =
  599. readb(&priv->regs->mb[RCAR_CAN_RX_FIFO_MBX].data[dlc]);
  600. }
  601. can_led_event(priv->ndev, CAN_LED_EVENT_RX);
  602. stats->rx_bytes += cf->can_dlc;
  603. stats->rx_packets++;
  604. netif_receive_skb(skb);
  605. }
  606. static int rcar_can_rx_poll(struct napi_struct *napi, int quota)
  607. {
  608. struct rcar_can_priv *priv = container_of(napi,
  609. struct rcar_can_priv, napi);
  610. int num_pkts;
  611. for (num_pkts = 0; num_pkts < quota; num_pkts++) {
  612. u8 rfcr, isr;
  613. isr = readb(&priv->regs->isr);
  614. /* Clear interrupt bit */
  615. if (isr & RCAR_CAN_ISR_RXFF)
  616. writeb(isr & ~RCAR_CAN_ISR_RXFF, &priv->regs->isr);
  617. rfcr = readb(&priv->regs->rfcr);
  618. if (rfcr & RCAR_CAN_RFCR_RFEST)
  619. break;
  620. rcar_can_rx_pkt(priv);
  621. /* Write 0xff to the RFPCR register to increment
  622. * the CPU-side pointer for the receive FIFO
  623. * to the next mailbox location
  624. */
  625. writeb(0xff, &priv->regs->rfpcr);
  626. }
  627. /* All packets processed */
  628. if (num_pkts < quota) {
  629. napi_complete(napi);
  630. priv->ier |= RCAR_CAN_IER_RXFIE;
  631. writeb(priv->ier, &priv->regs->ier);
  632. }
  633. return num_pkts;
  634. }
  635. static int rcar_can_do_set_mode(struct net_device *ndev, enum can_mode mode)
  636. {
  637. switch (mode) {
  638. case CAN_MODE_START:
  639. rcar_can_start(ndev);
  640. netif_wake_queue(ndev);
  641. return 0;
  642. default:
  643. return -EOPNOTSUPP;
  644. }
  645. }
  646. static int rcar_can_get_berr_counter(const struct net_device *dev,
  647. struct can_berr_counter *bec)
  648. {
  649. struct rcar_can_priv *priv = netdev_priv(dev);
  650. int err;
  651. err = clk_prepare_enable(priv->clk);
  652. if (err)
  653. return err;
  654. bec->txerr = readb(&priv->regs->tecr);
  655. bec->rxerr = readb(&priv->regs->recr);
  656. clk_disable_unprepare(priv->clk);
  657. return 0;
  658. }
  659. static const char * const clock_names[] = {
  660. [CLKR_CLKP1] = "clkp1",
  661. [CLKR_CLKP2] = "clkp2",
  662. [CLKR_CLKEXT] = "can_clk",
  663. };
  664. static int rcar_can_probe(struct platform_device *pdev)
  665. {
  666. struct rcar_can_platform_data *pdata;
  667. struct rcar_can_priv *priv;
  668. struct net_device *ndev;
  669. struct resource *mem;
  670. void __iomem *addr;
  671. u32 clock_select = CLKR_CLKP1;
  672. int err = -ENODEV;
  673. int irq;
  674. if (pdev->dev.of_node) {
  675. of_property_read_u32(pdev->dev.of_node,
  676. "renesas,can-clock-select", &clock_select);
  677. } else {
  678. pdata = dev_get_platdata(&pdev->dev);
  679. if (!pdata) {
  680. dev_err(&pdev->dev, "No platform data provided!\n");
  681. goto fail;
  682. }
  683. clock_select = pdata->clock_select;
  684. }
  685. irq = platform_get_irq(pdev, 0);
  686. if (irq < 0) {
  687. dev_err(&pdev->dev, "No IRQ resource\n");
  688. err = irq;
  689. goto fail;
  690. }
  691. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  692. addr = devm_ioremap_resource(&pdev->dev, mem);
  693. if (IS_ERR(addr)) {
  694. err = PTR_ERR(addr);
  695. goto fail;
  696. }
  697. ndev = alloc_candev(sizeof(struct rcar_can_priv), RCAR_CAN_FIFO_DEPTH);
  698. if (!ndev) {
  699. dev_err(&pdev->dev, "alloc_candev() failed\n");
  700. err = -ENOMEM;
  701. goto fail;
  702. }
  703. priv = netdev_priv(ndev);
  704. priv->clk = devm_clk_get(&pdev->dev, "clkp1");
  705. if (IS_ERR(priv->clk)) {
  706. err = PTR_ERR(priv->clk);
  707. dev_err(&pdev->dev, "cannot get peripheral clock, error %d\n",
  708. err);
  709. goto fail_clk;
  710. }
  711. if (!(BIT(clock_select) & RCAR_SUPPORTED_CLOCKS)) {
  712. err = -EINVAL;
  713. dev_err(&pdev->dev, "invalid CAN clock selected\n");
  714. goto fail_clk;
  715. }
  716. priv->can_clk = devm_clk_get(&pdev->dev, clock_names[clock_select]);
  717. if (IS_ERR(priv->can_clk)) {
  718. err = PTR_ERR(priv->can_clk);
  719. dev_err(&pdev->dev, "cannot get CAN clock, error %d\n", err);
  720. goto fail_clk;
  721. }
  722. ndev->netdev_ops = &rcar_can_netdev_ops;
  723. ndev->irq = irq;
  724. ndev->flags |= IFF_ECHO;
  725. priv->ndev = ndev;
  726. priv->regs = addr;
  727. priv->clock_select = clock_select;
  728. priv->can.clock.freq = clk_get_rate(priv->can_clk);
  729. priv->can.bittiming_const = &rcar_can_bittiming_const;
  730. priv->can.do_set_mode = rcar_can_do_set_mode;
  731. priv->can.do_get_berr_counter = rcar_can_get_berr_counter;
  732. priv->can.ctrlmode_supported = CAN_CTRLMODE_BERR_REPORTING;
  733. platform_set_drvdata(pdev, ndev);
  734. SET_NETDEV_DEV(ndev, &pdev->dev);
  735. netif_napi_add(ndev, &priv->napi, rcar_can_rx_poll,
  736. RCAR_CAN_NAPI_WEIGHT);
  737. err = register_candev(ndev);
  738. if (err) {
  739. dev_err(&pdev->dev, "register_candev() failed, error %d\n",
  740. err);
  741. goto fail_candev;
  742. }
  743. devm_can_led_init(ndev);
  744. dev_info(&pdev->dev, "device registered (regs @ %p, IRQ%d)\n",
  745. priv->regs, ndev->irq);
  746. return 0;
  747. fail_candev:
  748. netif_napi_del(&priv->napi);
  749. fail_clk:
  750. free_candev(ndev);
  751. fail:
  752. return err;
  753. }
  754. static int rcar_can_remove(struct platform_device *pdev)
  755. {
  756. struct net_device *ndev = platform_get_drvdata(pdev);
  757. struct rcar_can_priv *priv = netdev_priv(ndev);
  758. unregister_candev(ndev);
  759. netif_napi_del(&priv->napi);
  760. free_candev(ndev);
  761. return 0;
  762. }
  763. static int __maybe_unused rcar_can_suspend(struct device *dev)
  764. {
  765. struct net_device *ndev = dev_get_drvdata(dev);
  766. struct rcar_can_priv *priv = netdev_priv(ndev);
  767. u16 ctlr;
  768. if (netif_running(ndev)) {
  769. netif_stop_queue(ndev);
  770. netif_device_detach(ndev);
  771. }
  772. ctlr = readw(&priv->regs->ctlr);
  773. ctlr |= RCAR_CAN_CTLR_CANM_HALT;
  774. writew(ctlr, &priv->regs->ctlr);
  775. ctlr |= RCAR_CAN_CTLR_SLPM;
  776. writew(ctlr, &priv->regs->ctlr);
  777. priv->can.state = CAN_STATE_SLEEPING;
  778. clk_disable(priv->clk);
  779. return 0;
  780. }
  781. static int __maybe_unused rcar_can_resume(struct device *dev)
  782. {
  783. struct net_device *ndev = dev_get_drvdata(dev);
  784. struct rcar_can_priv *priv = netdev_priv(ndev);
  785. u16 ctlr;
  786. int err;
  787. err = clk_enable(priv->clk);
  788. if (err) {
  789. netdev_err(ndev, "clk_enable() failed, error %d\n", err);
  790. return err;
  791. }
  792. ctlr = readw(&priv->regs->ctlr);
  793. ctlr &= ~RCAR_CAN_CTLR_SLPM;
  794. writew(ctlr, &priv->regs->ctlr);
  795. ctlr &= ~RCAR_CAN_CTLR_CANM;
  796. writew(ctlr, &priv->regs->ctlr);
  797. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  798. if (netif_running(ndev)) {
  799. netif_device_attach(ndev);
  800. netif_start_queue(ndev);
  801. }
  802. return 0;
  803. }
  804. static SIMPLE_DEV_PM_OPS(rcar_can_pm_ops, rcar_can_suspend, rcar_can_resume);
  805. static const struct of_device_id rcar_can_of_table[] __maybe_unused = {
  806. { .compatible = "renesas,can-r8a7778" },
  807. { .compatible = "renesas,can-r8a7779" },
  808. { .compatible = "renesas,can-r8a7790" },
  809. { .compatible = "renesas,can-r8a7791" },
  810. { }
  811. };
  812. MODULE_DEVICE_TABLE(of, rcar_can_of_table);
  813. static struct platform_driver rcar_can_driver = {
  814. .driver = {
  815. .name = RCAR_CAN_DRV_NAME,
  816. .of_match_table = of_match_ptr(rcar_can_of_table),
  817. .pm = &rcar_can_pm_ops,
  818. },
  819. .probe = rcar_can_probe,
  820. .remove = rcar_can_remove,
  821. };
  822. module_platform_driver(rcar_can_driver);
  823. MODULE_AUTHOR("Cogent Embedded, Inc.");
  824. MODULE_LICENSE("GPL");
  825. MODULE_DESCRIPTION("CAN driver for Renesas R-Car SoC");
  826. MODULE_ALIAS("platform:" RCAR_CAN_DRV_NAME);