sun4i_can.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. /*
  2. * sun4i_can.c - CAN bus controller driver for Allwinner SUN4I&SUN7I based SoCs
  3. *
  4. * Copyright (C) 2013 Peter Chen
  5. * Copyright (C) 2015 Gerhard Bertelsmann
  6. * All rights reserved.
  7. *
  8. * Parts of this software are based on (derived from) the SJA1000 code by:
  9. * Copyright (C) 2014 Oliver Hartkopp <oliver.hartkopp@volkswagen.de>
  10. * Copyright (C) 2007 Wolfgang Grandegger <wg@grandegger.com>
  11. * Copyright (C) 2002-2007 Volkswagen Group Electronic Research
  12. * Copyright (C) 2003 Matthias Brukner, Trajet Gmbh, Rebenring 33,
  13. * 38106 Braunschweig, GERMANY
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions
  17. * are met:
  18. * 1. Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. * 2. Redistributions in binary form must reproduce the above copyright
  21. * notice, this list of conditions and the following disclaimer in the
  22. * documentation and/or other materials provided with the distribution.
  23. * 3. Neither the name of Volkswagen nor the names of its contributors
  24. * may be used to endorse or promote products derived from this software
  25. * without specific prior written permission.
  26. *
  27. * Alternatively, provided that this notice is retained in full, this
  28. * software may be distributed under the terms of the GNU General
  29. * Public License ("GPL") version 2, in which case the provisions of the
  30. * GPL apply INSTEAD OF those given above.
  31. *
  32. * The provided data structures and external interfaces from this code
  33. * are not restricted to be used by modules with a GPL compatible license.
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  36. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  37. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  38. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  39. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  42. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  43. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  44. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  45. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  46. * DAMAGE.
  47. *
  48. */
  49. #include <linux/netdevice.h>
  50. #include <linux/can.h>
  51. #include <linux/can/dev.h>
  52. #include <linux/can/error.h>
  53. #include <linux/clk.h>
  54. #include <linux/delay.h>
  55. #include <linux/ethtool.h>
  56. #include <linux/interrupt.h>
  57. #include <linux/init.h>
  58. #include <linux/io.h>
  59. #include <linux/module.h>
  60. #include <linux/of.h>
  61. #include <linux/platform_device.h>
  62. #include <linux/reset.h>
  63. #define DRV_NAME "sun4i_can"
  64. /* Registers address (physical base address 0x01C2BC00) */
  65. #define SUN4I_REG_MSEL_ADDR 0x0000 /* CAN Mode Select */
  66. #define SUN4I_REG_CMD_ADDR 0x0004 /* CAN Command */
  67. #define SUN4I_REG_STA_ADDR 0x0008 /* CAN Status */
  68. #define SUN4I_REG_INT_ADDR 0x000c /* CAN Interrupt Flag */
  69. #define SUN4I_REG_INTEN_ADDR 0x0010 /* CAN Interrupt Enable */
  70. #define SUN4I_REG_BTIME_ADDR 0x0014 /* CAN Bus Timing 0 */
  71. #define SUN4I_REG_TEWL_ADDR 0x0018 /* CAN Tx Error Warning Limit */
  72. #define SUN4I_REG_ERRC_ADDR 0x001c /* CAN Error Counter */
  73. #define SUN4I_REG_RMCNT_ADDR 0x0020 /* CAN Receive Message Counter */
  74. #define SUN4I_REG_RBUFSA_ADDR 0x0024 /* CAN Receive Buffer Start Address */
  75. #define SUN4I_REG_BUF0_ADDR 0x0040 /* CAN Tx/Rx Buffer 0 */
  76. #define SUN4I_REG_BUF1_ADDR 0x0044 /* CAN Tx/Rx Buffer 1 */
  77. #define SUN4I_REG_BUF2_ADDR 0x0048 /* CAN Tx/Rx Buffer 2 */
  78. #define SUN4I_REG_BUF3_ADDR 0x004c /* CAN Tx/Rx Buffer 3 */
  79. #define SUN4I_REG_BUF4_ADDR 0x0050 /* CAN Tx/Rx Buffer 4 */
  80. #define SUN4I_REG_BUF5_ADDR 0x0054 /* CAN Tx/Rx Buffer 5 */
  81. #define SUN4I_REG_BUF6_ADDR 0x0058 /* CAN Tx/Rx Buffer 6 */
  82. #define SUN4I_REG_BUF7_ADDR 0x005c /* CAN Tx/Rx Buffer 7 */
  83. #define SUN4I_REG_BUF8_ADDR 0x0060 /* CAN Tx/Rx Buffer 8 */
  84. #define SUN4I_REG_BUF9_ADDR 0x0064 /* CAN Tx/Rx Buffer 9 */
  85. #define SUN4I_REG_BUF10_ADDR 0x0068 /* CAN Tx/Rx Buffer 10 */
  86. #define SUN4I_REG_BUF11_ADDR 0x006c /* CAN Tx/Rx Buffer 11 */
  87. #define SUN4I_REG_BUF12_ADDR 0x0070 /* CAN Tx/Rx Buffer 12 */
  88. #define SUN4I_REG_ACPC_ADDR 0x0040 /* CAN Acceptance Code 0 */
  89. #define SUN4I_REG_ACPM_ADDR 0x0044 /* CAN Acceptance Mask 0 */
  90. #define SUN4I_REG_ACPC_ADDR_D1 0x0028 /* CAN Acceptance Code 0 on the D1 */
  91. #define SUN4I_REG_ACPM_ADDR_D1 0x002C /* CAN Acceptance Mask 0 on the D1 */
  92. #define SUN4I_REG_RBUF_RBACK_START_ADDR 0x0180 /* CAN transmit buffer start */
  93. #define SUN4I_REG_RBUF_RBACK_END_ADDR 0x01b0 /* CAN transmit buffer end */
  94. /* Controller Register Description */
  95. /* mode select register (r/w)
  96. * offset:0x0000 default:0x0000_0001
  97. */
  98. #define SUN4I_MSEL_SLEEP_MODE (0x01 << 4) /* write in reset mode */
  99. #define SUN4I_MSEL_WAKE_UP (0x00 << 4)
  100. #define SUN4I_MSEL_SINGLE_FILTER (0x01 << 3) /* write in reset mode */
  101. #define SUN4I_MSEL_DUAL_FILTERS (0x00 << 3)
  102. #define SUN4I_MSEL_LOOPBACK_MODE BIT(2)
  103. #define SUN4I_MSEL_LISTEN_ONLY_MODE BIT(1)
  104. #define SUN4I_MSEL_RESET_MODE BIT(0)
  105. /* command register (w)
  106. * offset:0x0004 default:0x0000_0000
  107. */
  108. #define SUN4I_CMD_BUS_OFF_REQ BIT(5)
  109. #define SUN4I_CMD_SELF_RCV_REQ BIT(4)
  110. #define SUN4I_CMD_CLEAR_OR_FLAG BIT(3)
  111. #define SUN4I_CMD_RELEASE_RBUF BIT(2)
  112. #define SUN4I_CMD_ABORT_REQ BIT(1)
  113. #define SUN4I_CMD_TRANS_REQ BIT(0)
  114. /* status register (r)
  115. * offset:0x0008 default:0x0000_003c
  116. */
  117. #define SUN4I_STA_BIT_ERR (0x00 << 22)
  118. #define SUN4I_STA_FORM_ERR (0x01 << 22)
  119. #define SUN4I_STA_STUFF_ERR (0x02 << 22)
  120. #define SUN4I_STA_OTHER_ERR (0x03 << 22)
  121. #define SUN4I_STA_MASK_ERR (0x03 << 22)
  122. #define SUN4I_STA_ERR_DIR BIT(21)
  123. #define SUN4I_STA_ERR_SEG_CODE (0x1f << 16)
  124. #define SUN4I_STA_START (0x03 << 16)
  125. #define SUN4I_STA_ID28_21 (0x02 << 16)
  126. #define SUN4I_STA_ID20_18 (0x06 << 16)
  127. #define SUN4I_STA_SRTR (0x04 << 16)
  128. #define SUN4I_STA_IDE (0x05 << 16)
  129. #define SUN4I_STA_ID17_13 (0x07 << 16)
  130. #define SUN4I_STA_ID12_5 (0x0f << 16)
  131. #define SUN4I_STA_ID4_0 (0x0e << 16)
  132. #define SUN4I_STA_RTR (0x0c << 16)
  133. #define SUN4I_STA_RB1 (0x0d << 16)
  134. #define SUN4I_STA_RB0 (0x09 << 16)
  135. #define SUN4I_STA_DLEN (0x0b << 16)
  136. #define SUN4I_STA_DATA_FIELD (0x0a << 16)
  137. #define SUN4I_STA_CRC_SEQUENCE (0x08 << 16)
  138. #define SUN4I_STA_CRC_DELIMITER (0x18 << 16)
  139. #define SUN4I_STA_ACK (0x19 << 16)
  140. #define SUN4I_STA_ACK_DELIMITER (0x1b << 16)
  141. #define SUN4I_STA_END (0x1a << 16)
  142. #define SUN4I_STA_INTERMISSION (0x12 << 16)
  143. #define SUN4I_STA_ACTIVE_ERROR (0x11 << 16)
  144. #define SUN4I_STA_PASSIVE_ERROR (0x16 << 16)
  145. #define SUN4I_STA_TOLERATE_DOMINANT_BITS (0x13 << 16)
  146. #define SUN4I_STA_ERROR_DELIMITER (0x17 << 16)
  147. #define SUN4I_STA_OVERLOAD (0x1c << 16)
  148. #define SUN4I_STA_BUS_OFF BIT(7)
  149. #define SUN4I_STA_ERR_STA BIT(6)
  150. #define SUN4I_STA_TRANS_BUSY BIT(5)
  151. #define SUN4I_STA_RCV_BUSY BIT(4)
  152. #define SUN4I_STA_TRANS_OVER BIT(3)
  153. #define SUN4I_STA_TBUF_RDY BIT(2)
  154. #define SUN4I_STA_DATA_ORUN BIT(1)
  155. #define SUN4I_STA_RBUF_RDY BIT(0)
  156. /* interrupt register (r)
  157. * offset:0x000c default:0x0000_0000
  158. */
  159. #define SUN4I_INT_BUS_ERR BIT(7)
  160. #define SUN4I_INT_ARB_LOST BIT(6)
  161. #define SUN4I_INT_ERR_PASSIVE BIT(5)
  162. #define SUN4I_INT_WAKEUP BIT(4)
  163. #define SUN4I_INT_DATA_OR BIT(3)
  164. #define SUN4I_INT_ERR_WRN BIT(2)
  165. #define SUN4I_INT_TBUF_VLD BIT(1)
  166. #define SUN4I_INT_RBUF_VLD BIT(0)
  167. /* interrupt enable register (r/w)
  168. * offset:0x0010 default:0x0000_0000
  169. */
  170. #define SUN4I_INTEN_BERR BIT(7)
  171. #define SUN4I_INTEN_ARB_LOST BIT(6)
  172. #define SUN4I_INTEN_ERR_PASSIVE BIT(5)
  173. #define SUN4I_INTEN_WAKEUP BIT(4)
  174. #define SUN4I_INTEN_OR BIT(3)
  175. #define SUN4I_INTEN_ERR_WRN BIT(2)
  176. #define SUN4I_INTEN_TX BIT(1)
  177. #define SUN4I_INTEN_RX BIT(0)
  178. /* error code */
  179. #define SUN4I_ERR_INRCV (0x1 << 5)
  180. #define SUN4I_ERR_INTRANS (0x0 << 5)
  181. /* filter mode */
  182. #define SUN4I_FILTER_CLOSE 0
  183. #define SUN4I_SINGLE_FLTER_MODE 1
  184. #define SUN4I_DUAL_FILTER_MODE 2
  185. /* message buffer flags */
  186. #define SUN4I_MSG_EFF_FLAG BIT(7)
  187. #define SUN4I_MSG_RTR_FLAG BIT(6)
  188. /* max. number of interrupts handled in ISR */
  189. #define SUN4I_CAN_MAX_IRQ 20
  190. #define SUN4I_MODE_MAX_RETRIES 100
  191. /**
  192. * struct sun4ican_quirks - Differences between SoC variants.
  193. *
  194. * @has_reset: SoC needs reset deasserted.
  195. * @acp_offset: Offset of ACPC and ACPM registers
  196. */
  197. struct sun4ican_quirks {
  198. bool has_reset;
  199. int acp_offset;
  200. };
  201. struct sun4ican_priv {
  202. struct can_priv can;
  203. void __iomem *base;
  204. struct clk *clk;
  205. struct reset_control *reset;
  206. spinlock_t cmdreg_lock; /* lock for concurrent cmd register writes */
  207. int acp_offset;
  208. };
  209. static const struct can_bittiming_const sun4ican_bittiming_const = {
  210. .name = DRV_NAME,
  211. .tseg1_min = 1,
  212. .tseg1_max = 16,
  213. .tseg2_min = 1,
  214. .tseg2_max = 8,
  215. .sjw_max = 4,
  216. .brp_min = 1,
  217. .brp_max = 64,
  218. .brp_inc = 1,
  219. };
  220. static void sun4i_can_write_cmdreg(struct sun4ican_priv *priv, u8 val)
  221. {
  222. unsigned long flags;
  223. spin_lock_irqsave(&priv->cmdreg_lock, flags);
  224. writel(val, priv->base + SUN4I_REG_CMD_ADDR);
  225. spin_unlock_irqrestore(&priv->cmdreg_lock, flags);
  226. }
  227. static int set_normal_mode(struct net_device *dev)
  228. {
  229. struct sun4ican_priv *priv = netdev_priv(dev);
  230. int retry = SUN4I_MODE_MAX_RETRIES;
  231. u32 mod_reg_val = 0;
  232. do {
  233. mod_reg_val = readl(priv->base + SUN4I_REG_MSEL_ADDR);
  234. mod_reg_val &= ~SUN4I_MSEL_RESET_MODE;
  235. writel(mod_reg_val, priv->base + SUN4I_REG_MSEL_ADDR);
  236. } while (retry-- && (mod_reg_val & SUN4I_MSEL_RESET_MODE));
  237. if (readl(priv->base + SUN4I_REG_MSEL_ADDR) & SUN4I_MSEL_RESET_MODE) {
  238. netdev_err(dev,
  239. "setting controller into normal mode failed!\n");
  240. return -ETIMEDOUT;
  241. }
  242. return 0;
  243. }
  244. static int set_reset_mode(struct net_device *dev)
  245. {
  246. struct sun4ican_priv *priv = netdev_priv(dev);
  247. int retry = SUN4I_MODE_MAX_RETRIES;
  248. u32 mod_reg_val = 0;
  249. do {
  250. mod_reg_val = readl(priv->base + SUN4I_REG_MSEL_ADDR);
  251. mod_reg_val |= SUN4I_MSEL_RESET_MODE;
  252. writel(mod_reg_val, priv->base + SUN4I_REG_MSEL_ADDR);
  253. } while (retry-- && !(mod_reg_val & SUN4I_MSEL_RESET_MODE));
  254. if (!(readl(priv->base + SUN4I_REG_MSEL_ADDR) &
  255. SUN4I_MSEL_RESET_MODE)) {
  256. netdev_err(dev, "setting controller into reset mode failed!\n");
  257. return -ETIMEDOUT;
  258. }
  259. return 0;
  260. }
  261. /* bittiming is called in reset_mode only */
  262. static int sun4ican_set_bittiming(struct net_device *dev)
  263. {
  264. struct sun4ican_priv *priv = netdev_priv(dev);
  265. struct can_bittiming *bt = &priv->can.bittiming;
  266. u32 cfg;
  267. cfg = ((bt->brp - 1) & 0x3FF) |
  268. (((bt->sjw - 1) & 0x3) << 14) |
  269. (((bt->prop_seg + bt->phase_seg1 - 1) & 0xf) << 16) |
  270. (((bt->phase_seg2 - 1) & 0x7) << 20);
  271. if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
  272. cfg |= 0x800000;
  273. netdev_dbg(dev, "setting BITTIMING=0x%08x\n", cfg);
  274. writel(cfg, priv->base + SUN4I_REG_BTIME_ADDR);
  275. return 0;
  276. }
  277. static int sun4ican_get_berr_counter(const struct net_device *dev,
  278. struct can_berr_counter *bec)
  279. {
  280. struct sun4ican_priv *priv = netdev_priv(dev);
  281. u32 errors;
  282. int err;
  283. err = clk_prepare_enable(priv->clk);
  284. if (err) {
  285. netdev_err(dev, "could not enable clock\n");
  286. return err;
  287. }
  288. errors = readl(priv->base + SUN4I_REG_ERRC_ADDR);
  289. bec->txerr = errors & 0xFF;
  290. bec->rxerr = (errors >> 16) & 0xFF;
  291. clk_disable_unprepare(priv->clk);
  292. return 0;
  293. }
  294. static int sun4i_can_start(struct net_device *dev)
  295. {
  296. struct sun4ican_priv *priv = netdev_priv(dev);
  297. int err;
  298. u32 mod_reg_val;
  299. /* we need to enter the reset mode */
  300. err = set_reset_mode(dev);
  301. if (err) {
  302. netdev_err(dev, "could not enter reset mode\n");
  303. return err;
  304. }
  305. /* set filters - we accept all */
  306. writel(0x00000000, priv->base + SUN4I_REG_ACPC_ADDR + priv->acp_offset);
  307. writel(0xFFFFFFFF, priv->base + SUN4I_REG_ACPM_ADDR + priv->acp_offset);
  308. /* clear error counters and error code capture */
  309. writel(0, priv->base + SUN4I_REG_ERRC_ADDR);
  310. /* enable interrupts */
  311. if (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
  312. writel(0xFF, priv->base + SUN4I_REG_INTEN_ADDR);
  313. else
  314. writel(0xFF & ~SUN4I_INTEN_BERR,
  315. priv->base + SUN4I_REG_INTEN_ADDR);
  316. /* enter the selected mode */
  317. mod_reg_val = readl(priv->base + SUN4I_REG_MSEL_ADDR);
  318. if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
  319. mod_reg_val |= SUN4I_MSEL_LOOPBACK_MODE;
  320. else if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
  321. mod_reg_val |= SUN4I_MSEL_LISTEN_ONLY_MODE;
  322. writel(mod_reg_val, priv->base + SUN4I_REG_MSEL_ADDR);
  323. err = sun4ican_set_bittiming(dev);
  324. if (err)
  325. return err;
  326. /* we are ready to enter the normal mode */
  327. err = set_normal_mode(dev);
  328. if (err) {
  329. netdev_err(dev, "could not enter normal mode\n");
  330. return err;
  331. }
  332. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  333. return 0;
  334. }
  335. static int sun4i_can_stop(struct net_device *dev)
  336. {
  337. struct sun4ican_priv *priv = netdev_priv(dev);
  338. int err;
  339. priv->can.state = CAN_STATE_STOPPED;
  340. /* we need to enter reset mode */
  341. err = set_reset_mode(dev);
  342. if (err) {
  343. netdev_err(dev, "could not enter reset mode\n");
  344. return err;
  345. }
  346. /* disable all interrupts */
  347. writel(0, priv->base + SUN4I_REG_INTEN_ADDR);
  348. return 0;
  349. }
  350. static int sun4ican_set_mode(struct net_device *dev, enum can_mode mode)
  351. {
  352. int err;
  353. switch (mode) {
  354. case CAN_MODE_START:
  355. err = sun4i_can_start(dev);
  356. if (err) {
  357. netdev_err(dev, "starting CAN controller failed!\n");
  358. return err;
  359. }
  360. if (netif_queue_stopped(dev))
  361. netif_wake_queue(dev);
  362. break;
  363. default:
  364. return -EOPNOTSUPP;
  365. }
  366. return 0;
  367. }
  368. /* transmit a CAN message
  369. * message layout in the sk_buff should be like this:
  370. * xx xx xx xx ff ll 00 11 22 33 44 55 66 77
  371. * [ can_id ] [flags] [len] [can data (up to 8 bytes]
  372. */
  373. static netdev_tx_t sun4ican_start_xmit(struct sk_buff *skb, struct net_device *dev)
  374. {
  375. struct sun4ican_priv *priv = netdev_priv(dev);
  376. struct can_frame *cf = (struct can_frame *)skb->data;
  377. u8 dlc;
  378. u32 dreg, msg_flag_n;
  379. canid_t id;
  380. int i;
  381. if (can_dev_dropped_skb(dev, skb))
  382. return NETDEV_TX_OK;
  383. netif_stop_queue(dev);
  384. id = cf->can_id;
  385. dlc = cf->len;
  386. msg_flag_n = dlc;
  387. if (id & CAN_RTR_FLAG)
  388. msg_flag_n |= SUN4I_MSG_RTR_FLAG;
  389. if (id & CAN_EFF_FLAG) {
  390. msg_flag_n |= SUN4I_MSG_EFF_FLAG;
  391. dreg = SUN4I_REG_BUF5_ADDR;
  392. writel((id >> 21) & 0xFF, priv->base + SUN4I_REG_BUF1_ADDR);
  393. writel((id >> 13) & 0xFF, priv->base + SUN4I_REG_BUF2_ADDR);
  394. writel((id >> 5) & 0xFF, priv->base + SUN4I_REG_BUF3_ADDR);
  395. writel((id << 3) & 0xF8, priv->base + SUN4I_REG_BUF4_ADDR);
  396. } else {
  397. dreg = SUN4I_REG_BUF3_ADDR;
  398. writel((id >> 3) & 0xFF, priv->base + SUN4I_REG_BUF1_ADDR);
  399. writel((id << 5) & 0xE0, priv->base + SUN4I_REG_BUF2_ADDR);
  400. }
  401. for (i = 0; i < dlc; i++)
  402. writel(cf->data[i], priv->base + (dreg + i * 4));
  403. writel(msg_flag_n, priv->base + SUN4I_REG_BUF0_ADDR);
  404. can_put_echo_skb(skb, dev, 0, 0);
  405. if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
  406. sun4i_can_write_cmdreg(priv, SUN4I_CMD_SELF_RCV_REQ);
  407. else
  408. sun4i_can_write_cmdreg(priv, SUN4I_CMD_TRANS_REQ);
  409. return NETDEV_TX_OK;
  410. }
  411. static void sun4i_can_rx(struct net_device *dev)
  412. {
  413. struct sun4ican_priv *priv = netdev_priv(dev);
  414. struct net_device_stats *stats = &dev->stats;
  415. struct can_frame *cf;
  416. struct sk_buff *skb;
  417. u8 fi;
  418. u32 dreg;
  419. canid_t id;
  420. int i;
  421. /* create zero'ed CAN frame buffer */
  422. skb = alloc_can_skb(dev, &cf);
  423. if (!skb)
  424. return;
  425. fi = readl(priv->base + SUN4I_REG_BUF0_ADDR);
  426. cf->len = can_cc_dlc2len(fi & 0x0F);
  427. if (fi & SUN4I_MSG_EFF_FLAG) {
  428. dreg = SUN4I_REG_BUF5_ADDR;
  429. id = (readl(priv->base + SUN4I_REG_BUF1_ADDR) << 21) |
  430. (readl(priv->base + SUN4I_REG_BUF2_ADDR) << 13) |
  431. (readl(priv->base + SUN4I_REG_BUF3_ADDR) << 5) |
  432. ((readl(priv->base + SUN4I_REG_BUF4_ADDR) >> 3) & 0x1f);
  433. id |= CAN_EFF_FLAG;
  434. } else {
  435. dreg = SUN4I_REG_BUF3_ADDR;
  436. id = (readl(priv->base + SUN4I_REG_BUF1_ADDR) << 3) |
  437. ((readl(priv->base + SUN4I_REG_BUF2_ADDR) >> 5) & 0x7);
  438. }
  439. /* remote frame ? */
  440. if (fi & SUN4I_MSG_RTR_FLAG) {
  441. id |= CAN_RTR_FLAG;
  442. } else {
  443. for (i = 0; i < cf->len; i++)
  444. cf->data[i] = readl(priv->base + dreg + i * 4);
  445. stats->rx_bytes += cf->len;
  446. }
  447. stats->rx_packets++;
  448. cf->can_id = id;
  449. sun4i_can_write_cmdreg(priv, SUN4I_CMD_RELEASE_RBUF);
  450. netif_rx(skb);
  451. }
  452. static int sun4i_can_err(struct net_device *dev, u8 isrc, u8 status)
  453. {
  454. struct sun4ican_priv *priv = netdev_priv(dev);
  455. struct net_device_stats *stats = &dev->stats;
  456. struct can_frame *cf;
  457. struct sk_buff *skb;
  458. enum can_state state = priv->can.state;
  459. enum can_state rx_state, tx_state;
  460. unsigned int rxerr, txerr, errc;
  461. u32 ecc, alc;
  462. /* we don't skip if alloc fails because we want the stats anyhow */
  463. skb = alloc_can_err_skb(dev, &cf);
  464. errc = readl(priv->base + SUN4I_REG_ERRC_ADDR);
  465. rxerr = (errc >> 16) & 0xFF;
  466. txerr = errc & 0xFF;
  467. if (isrc & SUN4I_INT_DATA_OR) {
  468. /* data overrun interrupt */
  469. netdev_dbg(dev, "data overrun interrupt\n");
  470. if (likely(skb)) {
  471. cf->can_id |= CAN_ERR_CRTL;
  472. cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
  473. }
  474. stats->rx_over_errors++;
  475. stats->rx_errors++;
  476. /* reset the CAN IP by entering reset mode
  477. * ignoring timeout error
  478. */
  479. set_reset_mode(dev);
  480. set_normal_mode(dev);
  481. /* clear bit */
  482. sun4i_can_write_cmdreg(priv, SUN4I_CMD_CLEAR_OR_FLAG);
  483. }
  484. if (isrc & SUN4I_INT_ERR_WRN) {
  485. /* error warning interrupt */
  486. netdev_dbg(dev, "error warning interrupt\n");
  487. if (status & SUN4I_STA_BUS_OFF)
  488. state = CAN_STATE_BUS_OFF;
  489. else if (status & SUN4I_STA_ERR_STA)
  490. state = CAN_STATE_ERROR_WARNING;
  491. else
  492. state = CAN_STATE_ERROR_ACTIVE;
  493. }
  494. if (likely(skb) && state != CAN_STATE_BUS_OFF) {
  495. cf->can_id |= CAN_ERR_CNT;
  496. cf->data[6] = txerr;
  497. cf->data[7] = rxerr;
  498. }
  499. if (isrc & SUN4I_INT_BUS_ERR) {
  500. /* bus error interrupt */
  501. netdev_dbg(dev, "bus error interrupt\n");
  502. priv->can.can_stats.bus_error++;
  503. ecc = readl(priv->base + SUN4I_REG_STA_ADDR);
  504. if (likely(skb)) {
  505. cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
  506. switch (ecc & SUN4I_STA_MASK_ERR) {
  507. case SUN4I_STA_BIT_ERR:
  508. cf->data[2] |= CAN_ERR_PROT_BIT;
  509. break;
  510. case SUN4I_STA_FORM_ERR:
  511. cf->data[2] |= CAN_ERR_PROT_FORM;
  512. break;
  513. case SUN4I_STA_STUFF_ERR:
  514. cf->data[2] |= CAN_ERR_PROT_STUFF;
  515. break;
  516. default:
  517. cf->data[3] = (ecc & SUN4I_STA_ERR_SEG_CODE)
  518. >> 16;
  519. break;
  520. }
  521. }
  522. /* error occurred during transmission? */
  523. if ((ecc & SUN4I_STA_ERR_DIR) == 0) {
  524. if (likely(skb))
  525. cf->data[2] |= CAN_ERR_PROT_TX;
  526. stats->tx_errors++;
  527. } else {
  528. stats->rx_errors++;
  529. }
  530. }
  531. if (isrc & SUN4I_INT_ERR_PASSIVE) {
  532. /* error passive interrupt */
  533. netdev_dbg(dev, "error passive interrupt\n");
  534. if (state == CAN_STATE_ERROR_PASSIVE)
  535. state = CAN_STATE_ERROR_WARNING;
  536. else
  537. state = CAN_STATE_ERROR_PASSIVE;
  538. }
  539. if (isrc & SUN4I_INT_ARB_LOST) {
  540. /* arbitration lost interrupt */
  541. netdev_dbg(dev, "arbitration lost interrupt\n");
  542. alc = readl(priv->base + SUN4I_REG_STA_ADDR);
  543. priv->can.can_stats.arbitration_lost++;
  544. if (likely(skb)) {
  545. cf->can_id |= CAN_ERR_LOSTARB;
  546. cf->data[0] = (alc >> 8) & 0x1f;
  547. }
  548. }
  549. if (state != priv->can.state) {
  550. tx_state = txerr >= rxerr ? state : 0;
  551. rx_state = txerr <= rxerr ? state : 0;
  552. /* The skb allocation might fail, but can_change_state()
  553. * handles cf == NULL.
  554. */
  555. can_change_state(dev, cf, tx_state, rx_state);
  556. if (state == CAN_STATE_BUS_OFF)
  557. can_bus_off(dev);
  558. }
  559. if (likely(skb))
  560. netif_rx(skb);
  561. else
  562. return -ENOMEM;
  563. return 0;
  564. }
  565. static irqreturn_t sun4i_can_interrupt(int irq, void *dev_id)
  566. {
  567. struct net_device *dev = (struct net_device *)dev_id;
  568. struct sun4ican_priv *priv = netdev_priv(dev);
  569. struct net_device_stats *stats = &dev->stats;
  570. u8 isrc, status;
  571. int n = 0;
  572. while ((n < SUN4I_CAN_MAX_IRQ) &&
  573. (isrc = readl(priv->base + SUN4I_REG_INT_ADDR))) {
  574. n++;
  575. status = readl(priv->base + SUN4I_REG_STA_ADDR);
  576. if (isrc & SUN4I_INT_WAKEUP)
  577. netdev_warn(dev, "wakeup interrupt\n");
  578. if (isrc & SUN4I_INT_TBUF_VLD) {
  579. /* transmission complete interrupt */
  580. stats->tx_bytes += can_get_echo_skb(dev, 0, NULL);
  581. stats->tx_packets++;
  582. netif_wake_queue(dev);
  583. }
  584. if ((isrc & SUN4I_INT_RBUF_VLD) &&
  585. !(isrc & SUN4I_INT_DATA_OR)) {
  586. /* receive interrupt - don't read if overrun occurred */
  587. while (status & SUN4I_STA_RBUF_RDY) {
  588. /* RX buffer is not empty */
  589. sun4i_can_rx(dev);
  590. status = readl(priv->base + SUN4I_REG_STA_ADDR);
  591. }
  592. }
  593. if (isrc &
  594. (SUN4I_INT_DATA_OR | SUN4I_INT_ERR_WRN | SUN4I_INT_BUS_ERR |
  595. SUN4I_INT_ERR_PASSIVE | SUN4I_INT_ARB_LOST)) {
  596. /* error interrupt */
  597. if (sun4i_can_err(dev, isrc, status))
  598. netdev_err(dev, "can't allocate buffer - clearing pending interrupts\n");
  599. }
  600. /* clear interrupts */
  601. writel(isrc, priv->base + SUN4I_REG_INT_ADDR);
  602. readl(priv->base + SUN4I_REG_INT_ADDR);
  603. }
  604. if (n >= SUN4I_CAN_MAX_IRQ)
  605. netdev_dbg(dev, "%d messages handled in ISR", n);
  606. return (n) ? IRQ_HANDLED : IRQ_NONE;
  607. }
  608. static int sun4ican_open(struct net_device *dev)
  609. {
  610. struct sun4ican_priv *priv = netdev_priv(dev);
  611. int err;
  612. /* common open */
  613. err = open_candev(dev);
  614. if (err)
  615. return err;
  616. /* register interrupt handler */
  617. err = request_irq(dev->irq, sun4i_can_interrupt, 0, dev->name, dev);
  618. if (err) {
  619. netdev_err(dev, "request_irq err: %d\n", err);
  620. goto exit_irq;
  621. }
  622. /* software reset deassert */
  623. err = reset_control_deassert(priv->reset);
  624. if (err) {
  625. netdev_err(dev, "could not deassert CAN reset\n");
  626. goto exit_soft_reset;
  627. }
  628. /* turn on clocking for CAN peripheral block */
  629. err = clk_prepare_enable(priv->clk);
  630. if (err) {
  631. netdev_err(dev, "could not enable CAN peripheral clock\n");
  632. goto exit_clock;
  633. }
  634. err = sun4i_can_start(dev);
  635. if (err) {
  636. netdev_err(dev, "could not start CAN peripheral\n");
  637. goto exit_can_start;
  638. }
  639. netif_start_queue(dev);
  640. return 0;
  641. exit_can_start:
  642. clk_disable_unprepare(priv->clk);
  643. exit_clock:
  644. reset_control_assert(priv->reset);
  645. exit_soft_reset:
  646. free_irq(dev->irq, dev);
  647. exit_irq:
  648. close_candev(dev);
  649. return err;
  650. }
  651. static int sun4ican_close(struct net_device *dev)
  652. {
  653. struct sun4ican_priv *priv = netdev_priv(dev);
  654. netif_stop_queue(dev);
  655. sun4i_can_stop(dev);
  656. clk_disable_unprepare(priv->clk);
  657. reset_control_assert(priv->reset);
  658. free_irq(dev->irq, dev);
  659. close_candev(dev);
  660. return 0;
  661. }
  662. static const struct net_device_ops sun4ican_netdev_ops = {
  663. .ndo_open = sun4ican_open,
  664. .ndo_stop = sun4ican_close,
  665. .ndo_start_xmit = sun4ican_start_xmit,
  666. };
  667. static const struct ethtool_ops sun4ican_ethtool_ops = {
  668. .get_ts_info = ethtool_op_get_ts_info,
  669. };
  670. static const struct sun4ican_quirks sun4ican_quirks_a10 = {
  671. .has_reset = false,
  672. .acp_offset = 0,
  673. };
  674. static const struct sun4ican_quirks sun4ican_quirks_r40 = {
  675. .has_reset = true,
  676. .acp_offset = 0,
  677. };
  678. static const struct sun4ican_quirks sun4ican_quirks_d1 = {
  679. .has_reset = true,
  680. .acp_offset = (SUN4I_REG_ACPC_ADDR_D1 - SUN4I_REG_ACPC_ADDR),
  681. };
  682. static const struct of_device_id sun4ican_of_match[] = {
  683. {
  684. .compatible = "allwinner,sun4i-a10-can",
  685. .data = &sun4ican_quirks_a10
  686. }, {
  687. .compatible = "allwinner,sun7i-a20-can",
  688. .data = &sun4ican_quirks_a10
  689. }, {
  690. .compatible = "allwinner,sun8i-r40-can",
  691. .data = &sun4ican_quirks_r40
  692. }, {
  693. .compatible = "allwinner,sun20i-d1-can",
  694. .data = &sun4ican_quirks_d1
  695. }, {
  696. /* sentinel */
  697. },
  698. };
  699. MODULE_DEVICE_TABLE(of, sun4ican_of_match);
  700. static void sun4ican_remove(struct platform_device *pdev)
  701. {
  702. struct net_device *dev = platform_get_drvdata(pdev);
  703. unregister_netdev(dev);
  704. free_candev(dev);
  705. }
  706. static int sun4ican_probe(struct platform_device *pdev)
  707. {
  708. struct device_node *np = pdev->dev.of_node;
  709. struct clk *clk;
  710. struct reset_control *reset = NULL;
  711. void __iomem *addr;
  712. int err, irq;
  713. struct net_device *dev;
  714. struct sun4ican_priv *priv;
  715. const struct sun4ican_quirks *quirks;
  716. quirks = of_device_get_match_data(&pdev->dev);
  717. if (!quirks) {
  718. dev_err(&pdev->dev, "failed to determine the quirks to use\n");
  719. err = -ENODEV;
  720. goto exit;
  721. }
  722. if (quirks->has_reset) {
  723. reset = devm_reset_control_get_exclusive(&pdev->dev, NULL);
  724. if (IS_ERR(reset)) {
  725. dev_err(&pdev->dev, "unable to request reset\n");
  726. err = PTR_ERR(reset);
  727. goto exit;
  728. }
  729. }
  730. clk = of_clk_get(np, 0);
  731. if (IS_ERR(clk)) {
  732. dev_err(&pdev->dev, "unable to request clock\n");
  733. err = -ENODEV;
  734. goto exit;
  735. }
  736. irq = platform_get_irq(pdev, 0);
  737. if (irq < 0) {
  738. err = -ENODEV;
  739. goto exit;
  740. }
  741. addr = devm_platform_ioremap_resource(pdev, 0);
  742. if (IS_ERR(addr)) {
  743. err = PTR_ERR(addr);
  744. goto exit;
  745. }
  746. dev = alloc_candev(sizeof(struct sun4ican_priv), 1);
  747. if (!dev) {
  748. dev_err(&pdev->dev,
  749. "could not allocate memory for CAN device\n");
  750. err = -ENOMEM;
  751. goto exit;
  752. }
  753. dev->netdev_ops = &sun4ican_netdev_ops;
  754. dev->ethtool_ops = &sun4ican_ethtool_ops;
  755. dev->irq = irq;
  756. dev->flags |= IFF_ECHO;
  757. priv = netdev_priv(dev);
  758. priv->can.clock.freq = clk_get_rate(clk);
  759. priv->can.bittiming_const = &sun4ican_bittiming_const;
  760. priv->can.do_set_mode = sun4ican_set_mode;
  761. priv->can.do_get_berr_counter = sun4ican_get_berr_counter;
  762. priv->can.ctrlmode_supported = CAN_CTRLMODE_BERR_REPORTING |
  763. CAN_CTRLMODE_LISTENONLY |
  764. CAN_CTRLMODE_LOOPBACK |
  765. CAN_CTRLMODE_3_SAMPLES;
  766. priv->base = addr;
  767. priv->clk = clk;
  768. priv->reset = reset;
  769. priv->acp_offset = quirks->acp_offset;
  770. spin_lock_init(&priv->cmdreg_lock);
  771. platform_set_drvdata(pdev, dev);
  772. SET_NETDEV_DEV(dev, &pdev->dev);
  773. err = register_candev(dev);
  774. if (err) {
  775. dev_err(&pdev->dev, "registering %s failed (err=%d)\n",
  776. DRV_NAME, err);
  777. goto exit_free;
  778. }
  779. dev_info(&pdev->dev, "device registered (base=%p, irq=%d)\n",
  780. priv->base, dev->irq);
  781. return 0;
  782. exit_free:
  783. free_candev(dev);
  784. exit:
  785. return err;
  786. }
  787. static struct platform_driver sun4i_can_driver = {
  788. .driver = {
  789. .name = DRV_NAME,
  790. .of_match_table = sun4ican_of_match,
  791. },
  792. .probe = sun4ican_probe,
  793. .remove = sun4ican_remove,
  794. };
  795. module_platform_driver(sun4i_can_driver);
  796. MODULE_AUTHOR("Peter Chen <xingkongcp@gmail.com>");
  797. MODULE_AUTHOR("Gerhard Bertelsmann <info@gerhard-bertelsmann.de>");
  798. MODULE_LICENSE("Dual BSD/GPL");
  799. MODULE_DESCRIPTION("CAN driver for Allwinner SoCs (A10/A20/D1)");