sun4i_can.c 24 KB

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