sun4i_can.c 25 KB

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