mscan.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. /*
  2. * CAN bus driver for the alone generic (as possible as) MSCAN controller.
  3. *
  4. * Copyright (C) 2005-2006 Andrey Volkov <avolkov@varma-el.com>,
  5. * Varma Electronics Oy
  6. * Copyright (C) 2008-2009 Wolfgang Grandegger <wg@grandegger.com>
  7. * Copyright (C) 2008-2009 Pengutronix <kernel@pengutronix.de>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the version 2 of the GNU General Public License
  11. * as published by the Free Software Foundation
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/delay.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/if_arp.h>
  27. #include <linux/if_ether.h>
  28. #include <linux/list.h>
  29. #include <linux/can/dev.h>
  30. #include <linux/can/error.h>
  31. #include <linux/io.h>
  32. #include "mscan.h"
  33. static const struct can_bittiming_const mscan_bittiming_const = {
  34. .name = "mscan",
  35. .tseg1_min = 4,
  36. .tseg1_max = 16,
  37. .tseg2_min = 2,
  38. .tseg2_max = 8,
  39. .sjw_max = 4,
  40. .brp_min = 1,
  41. .brp_max = 64,
  42. .brp_inc = 1,
  43. };
  44. struct mscan_state {
  45. u8 mode;
  46. u8 canrier;
  47. u8 cantier;
  48. };
  49. static enum can_state state_map[] = {
  50. CAN_STATE_ERROR_ACTIVE,
  51. CAN_STATE_ERROR_WARNING,
  52. CAN_STATE_ERROR_PASSIVE,
  53. CAN_STATE_BUS_OFF
  54. };
  55. static int mscan_set_mode(struct net_device *dev, u8 mode)
  56. {
  57. struct mscan_priv *priv = netdev_priv(dev);
  58. struct mscan_regs __iomem *regs = priv->reg_base;
  59. int ret = 0;
  60. int i;
  61. u8 canctl1;
  62. if (mode != MSCAN_NORMAL_MODE) {
  63. if (priv->tx_active) {
  64. /* Abort transfers before going to sleep */#
  65. out_8(&regs->cantarq, priv->tx_active);
  66. /* Suppress TX done interrupts */
  67. out_8(&regs->cantier, 0);
  68. }
  69. canctl1 = in_8(&regs->canctl1);
  70. if ((mode & MSCAN_SLPRQ) && !(canctl1 & MSCAN_SLPAK)) {
  71. setbits8(&regs->canctl0, MSCAN_SLPRQ);
  72. for (i = 0; i < MSCAN_SET_MODE_RETRIES; i++) {
  73. if (in_8(&regs->canctl1) & MSCAN_SLPAK)
  74. break;
  75. udelay(100);
  76. }
  77. /*
  78. * The mscan controller will fail to enter sleep mode,
  79. * while there are irregular activities on bus, like
  80. * somebody keeps retransmitting. This behavior is
  81. * undocumented and seems to differ between mscan built
  82. * in mpc5200b and mpc5200. We proceed in that case,
  83. * since otherwise the slprq will be kept set and the
  84. * controller will get stuck. NOTE: INITRQ or CSWAI
  85. * will abort all active transmit actions, if still
  86. * any, at once.
  87. */
  88. if (i >= MSCAN_SET_MODE_RETRIES)
  89. netdev_dbg(dev,
  90. "device failed to enter sleep mode. "
  91. "We proceed anyhow.\n");
  92. else
  93. priv->can.state = CAN_STATE_SLEEPING;
  94. }
  95. if ((mode & MSCAN_INITRQ) && !(canctl1 & MSCAN_INITAK)) {
  96. setbits8(&regs->canctl0, MSCAN_INITRQ);
  97. for (i = 0; i < MSCAN_SET_MODE_RETRIES; i++) {
  98. if (in_8(&regs->canctl1) & MSCAN_INITAK)
  99. break;
  100. }
  101. if (i >= MSCAN_SET_MODE_RETRIES)
  102. ret = -ENODEV;
  103. }
  104. if (!ret)
  105. priv->can.state = CAN_STATE_STOPPED;
  106. if (mode & MSCAN_CSWAI)
  107. setbits8(&regs->canctl0, MSCAN_CSWAI);
  108. } else {
  109. canctl1 = in_8(&regs->canctl1);
  110. if (canctl1 & (MSCAN_SLPAK | MSCAN_INITAK)) {
  111. clrbits8(&regs->canctl0, MSCAN_SLPRQ | MSCAN_INITRQ);
  112. for (i = 0; i < MSCAN_SET_MODE_RETRIES; i++) {
  113. canctl1 = in_8(&regs->canctl1);
  114. if (!(canctl1 & (MSCAN_INITAK | MSCAN_SLPAK)))
  115. break;
  116. }
  117. if (i >= MSCAN_SET_MODE_RETRIES)
  118. ret = -ENODEV;
  119. else
  120. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  121. }
  122. }
  123. return ret;
  124. }
  125. static int mscan_start(struct net_device *dev)
  126. {
  127. struct mscan_priv *priv = netdev_priv(dev);
  128. struct mscan_regs __iomem *regs = priv->reg_base;
  129. u8 canrflg;
  130. int err;
  131. out_8(&regs->canrier, 0);
  132. INIT_LIST_HEAD(&priv->tx_head);
  133. priv->prev_buf_id = 0;
  134. priv->cur_pri = 0;
  135. priv->tx_active = 0;
  136. priv->shadow_canrier = 0;
  137. priv->flags = 0;
  138. if (priv->type == MSCAN_TYPE_MPC5121) {
  139. /* Clear pending bus-off condition */
  140. if (in_8(&regs->canmisc) & MSCAN_BOHOLD)
  141. out_8(&regs->canmisc, MSCAN_BOHOLD);
  142. }
  143. err = mscan_set_mode(dev, MSCAN_NORMAL_MODE);
  144. if (err)
  145. return err;
  146. canrflg = in_8(&regs->canrflg);
  147. priv->shadow_statflg = canrflg & MSCAN_STAT_MSK;
  148. priv->can.state = state_map[max(MSCAN_STATE_RX(canrflg),
  149. MSCAN_STATE_TX(canrflg))];
  150. out_8(&regs->cantier, 0);
  151. /* Enable receive interrupts. */
  152. out_8(&regs->canrier, MSCAN_RX_INTS_ENABLE);
  153. return 0;
  154. }
  155. static int mscan_restart(struct net_device *dev)
  156. {
  157. struct mscan_priv *priv = netdev_priv(dev);
  158. if (priv->type == MSCAN_TYPE_MPC5121) {
  159. struct mscan_regs __iomem *regs = priv->reg_base;
  160. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  161. WARN(!(in_8(&regs->canmisc) & MSCAN_BOHOLD),
  162. "bus-off state expected\n");
  163. out_8(&regs->canmisc, MSCAN_BOHOLD);
  164. /* Re-enable receive interrupts. */
  165. out_8(&regs->canrier, MSCAN_RX_INTS_ENABLE);
  166. } else {
  167. if (priv->can.state <= CAN_STATE_BUS_OFF)
  168. mscan_set_mode(dev, MSCAN_INIT_MODE);
  169. return mscan_start(dev);
  170. }
  171. return 0;
  172. }
  173. static netdev_tx_t mscan_start_xmit(struct sk_buff *skb, struct net_device *dev)
  174. {
  175. struct can_frame *frame = (struct can_frame *)skb->data;
  176. struct mscan_priv *priv = netdev_priv(dev);
  177. struct mscan_regs __iomem *regs = priv->reg_base;
  178. int i, rtr, buf_id;
  179. u32 can_id;
  180. if (can_dropped_invalid_skb(dev, skb))
  181. return NETDEV_TX_OK;
  182. out_8(&regs->cantier, 0);
  183. i = ~priv->tx_active & MSCAN_TXE;
  184. buf_id = ffs(i) - 1;
  185. switch (hweight8(i)) {
  186. case 0:
  187. netif_stop_queue(dev);
  188. netdev_err(dev, "Tx Ring full when queue awake!\n");
  189. return NETDEV_TX_BUSY;
  190. case 1:
  191. /*
  192. * if buf_id < 3, then current frame will be send out of order,
  193. * since buffer with lower id have higher priority (hell..)
  194. */
  195. netif_stop_queue(dev);
  196. case 2:
  197. if (buf_id < priv->prev_buf_id) {
  198. priv->cur_pri++;
  199. if (priv->cur_pri == 0xff) {
  200. set_bit(F_TX_WAIT_ALL, &priv->flags);
  201. netif_stop_queue(dev);
  202. }
  203. }
  204. set_bit(F_TX_PROGRESS, &priv->flags);
  205. break;
  206. }
  207. priv->prev_buf_id = buf_id;
  208. out_8(&regs->cantbsel, i);
  209. rtr = frame->can_id & CAN_RTR_FLAG;
  210. /* RTR is always the lowest bit of interest, then IDs follow */
  211. if (frame->can_id & CAN_EFF_FLAG) {
  212. can_id = (frame->can_id & CAN_EFF_MASK)
  213. << (MSCAN_EFF_RTR_SHIFT + 1);
  214. if (rtr)
  215. can_id |= 1 << MSCAN_EFF_RTR_SHIFT;
  216. out_be16(&regs->tx.idr3_2, can_id);
  217. can_id >>= 16;
  218. /* EFF_FLAGS are between the IDs :( */
  219. can_id = (can_id & 0x7) | ((can_id << 2) & 0xffe0)
  220. | MSCAN_EFF_FLAGS;
  221. } else {
  222. can_id = (frame->can_id & CAN_SFF_MASK)
  223. << (MSCAN_SFF_RTR_SHIFT + 1);
  224. if (rtr)
  225. can_id |= 1 << MSCAN_SFF_RTR_SHIFT;
  226. }
  227. out_be16(&regs->tx.idr1_0, can_id);
  228. if (!rtr) {
  229. void __iomem *data = &regs->tx.dsr1_0;
  230. u16 *payload = (u16 *)frame->data;
  231. for (i = 0; i < frame->can_dlc / 2; i++) {
  232. out_be16(data, *payload++);
  233. data += 2 + _MSCAN_RESERVED_DSR_SIZE;
  234. }
  235. /* write remaining byte if necessary */
  236. if (frame->can_dlc & 1)
  237. out_8(data, frame->data[frame->can_dlc - 1]);
  238. }
  239. out_8(&regs->tx.dlr, frame->can_dlc);
  240. out_8(&regs->tx.tbpr, priv->cur_pri);
  241. /* Start transmission. */
  242. out_8(&regs->cantflg, 1 << buf_id);
  243. if (!test_bit(F_TX_PROGRESS, &priv->flags))
  244. dev->trans_start = jiffies;
  245. list_add_tail(&priv->tx_queue[buf_id].list, &priv->tx_head);
  246. can_put_echo_skb(skb, dev, buf_id);
  247. /* Enable interrupt. */
  248. priv->tx_active |= 1 << buf_id;
  249. out_8(&regs->cantier, priv->tx_active);
  250. return NETDEV_TX_OK;
  251. }
  252. static enum can_state get_new_state(struct net_device *dev, u8 canrflg)
  253. {
  254. struct mscan_priv *priv = netdev_priv(dev);
  255. if (unlikely(canrflg & MSCAN_CSCIF))
  256. return state_map[max(MSCAN_STATE_RX(canrflg),
  257. MSCAN_STATE_TX(canrflg))];
  258. return priv->can.state;
  259. }
  260. static void mscan_get_rx_frame(struct net_device *dev, struct can_frame *frame)
  261. {
  262. struct mscan_priv *priv = netdev_priv(dev);
  263. struct mscan_regs __iomem *regs = priv->reg_base;
  264. u32 can_id;
  265. int i;
  266. can_id = in_be16(&regs->rx.idr1_0);
  267. if (can_id & (1 << 3)) {
  268. frame->can_id = CAN_EFF_FLAG;
  269. can_id = ((can_id << 16) | in_be16(&regs->rx.idr3_2));
  270. can_id = ((can_id & 0xffe00000) |
  271. ((can_id & 0x7ffff) << 2)) >> 2;
  272. } else {
  273. can_id >>= 4;
  274. frame->can_id = 0;
  275. }
  276. frame->can_id |= can_id >> 1;
  277. if (can_id & 1)
  278. frame->can_id |= CAN_RTR_FLAG;
  279. frame->can_dlc = get_can_dlc(in_8(&regs->rx.dlr) & 0xf);
  280. if (!(frame->can_id & CAN_RTR_FLAG)) {
  281. void __iomem *data = &regs->rx.dsr1_0;
  282. u16 *payload = (u16 *)frame->data;
  283. for (i = 0; i < frame->can_dlc / 2; i++) {
  284. *payload++ = in_be16(data);
  285. data += 2 + _MSCAN_RESERVED_DSR_SIZE;
  286. }
  287. /* read remaining byte if necessary */
  288. if (frame->can_dlc & 1)
  289. frame->data[frame->can_dlc - 1] = in_8(data);
  290. }
  291. out_8(&regs->canrflg, MSCAN_RXF);
  292. }
  293. static void mscan_get_err_frame(struct net_device *dev, struct can_frame *frame,
  294. u8 canrflg)
  295. {
  296. struct mscan_priv *priv = netdev_priv(dev);
  297. struct mscan_regs __iomem *regs = priv->reg_base;
  298. struct net_device_stats *stats = &dev->stats;
  299. enum can_state new_state;
  300. netdev_dbg(dev, "error interrupt (canrflg=%#x)\n", canrflg);
  301. frame->can_id = CAN_ERR_FLAG;
  302. if (canrflg & MSCAN_OVRIF) {
  303. frame->can_id |= CAN_ERR_CRTL;
  304. frame->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
  305. stats->rx_over_errors++;
  306. stats->rx_errors++;
  307. } else {
  308. frame->data[1] = 0;
  309. }
  310. new_state = get_new_state(dev, canrflg);
  311. if (new_state != priv->can.state) {
  312. can_change_state(dev, frame,
  313. state_map[MSCAN_STATE_TX(canrflg)],
  314. state_map[MSCAN_STATE_RX(canrflg)]);
  315. if (priv->can.state == CAN_STATE_BUS_OFF) {
  316. /*
  317. * The MSCAN on the MPC5200 does recover from bus-off
  318. * automatically. To avoid that we stop the chip doing
  319. * a light-weight stop (we are in irq-context).
  320. */
  321. if (priv->type != MSCAN_TYPE_MPC5121) {
  322. out_8(&regs->cantier, 0);
  323. out_8(&regs->canrier, 0);
  324. setbits8(&regs->canctl0,
  325. MSCAN_SLPRQ | MSCAN_INITRQ);
  326. }
  327. can_bus_off(dev);
  328. }
  329. }
  330. priv->shadow_statflg = canrflg & MSCAN_STAT_MSK;
  331. frame->can_dlc = CAN_ERR_DLC;
  332. out_8(&regs->canrflg, MSCAN_ERR_IF);
  333. }
  334. static int mscan_rx_poll(struct napi_struct *napi, int quota)
  335. {
  336. struct mscan_priv *priv = container_of(napi, struct mscan_priv, napi);
  337. struct net_device *dev = napi->dev;
  338. struct mscan_regs __iomem *regs = priv->reg_base;
  339. struct net_device_stats *stats = &dev->stats;
  340. int npackets = 0;
  341. int ret = 1;
  342. struct sk_buff *skb;
  343. struct can_frame *frame;
  344. u8 canrflg;
  345. while (npackets < quota) {
  346. canrflg = in_8(&regs->canrflg);
  347. if (!(canrflg & (MSCAN_RXF | MSCAN_ERR_IF)))
  348. break;
  349. skb = alloc_can_skb(dev, &frame);
  350. if (!skb) {
  351. if (printk_ratelimit())
  352. netdev_notice(dev, "packet dropped\n");
  353. stats->rx_dropped++;
  354. out_8(&regs->canrflg, canrflg);
  355. continue;
  356. }
  357. if (canrflg & MSCAN_RXF)
  358. mscan_get_rx_frame(dev, frame);
  359. else if (canrflg & MSCAN_ERR_IF)
  360. mscan_get_err_frame(dev, frame, canrflg);
  361. stats->rx_packets++;
  362. stats->rx_bytes += frame->can_dlc;
  363. npackets++;
  364. netif_receive_skb(skb);
  365. }
  366. if (!(in_8(&regs->canrflg) & (MSCAN_RXF | MSCAN_ERR_IF))) {
  367. napi_complete(&priv->napi);
  368. clear_bit(F_RX_PROGRESS, &priv->flags);
  369. if (priv->can.state < CAN_STATE_BUS_OFF)
  370. out_8(&regs->canrier, priv->shadow_canrier);
  371. ret = 0;
  372. }
  373. return ret;
  374. }
  375. static irqreturn_t mscan_isr(int irq, void *dev_id)
  376. {
  377. struct net_device *dev = (struct net_device *)dev_id;
  378. struct mscan_priv *priv = netdev_priv(dev);
  379. struct mscan_regs __iomem *regs = priv->reg_base;
  380. struct net_device_stats *stats = &dev->stats;
  381. u8 cantier, cantflg, canrflg;
  382. irqreturn_t ret = IRQ_NONE;
  383. cantier = in_8(&regs->cantier) & MSCAN_TXE;
  384. cantflg = in_8(&regs->cantflg) & cantier;
  385. if (cantier && cantflg) {
  386. struct list_head *tmp, *pos;
  387. list_for_each_safe(pos, tmp, &priv->tx_head) {
  388. struct tx_queue_entry *entry =
  389. list_entry(pos, struct tx_queue_entry, list);
  390. u8 mask = entry->mask;
  391. if (!(cantflg & mask))
  392. continue;
  393. out_8(&regs->cantbsel, mask);
  394. stats->tx_bytes += in_8(&regs->tx.dlr);
  395. stats->tx_packets++;
  396. can_get_echo_skb(dev, entry->id);
  397. priv->tx_active &= ~mask;
  398. list_del(pos);
  399. }
  400. if (list_empty(&priv->tx_head)) {
  401. clear_bit(F_TX_WAIT_ALL, &priv->flags);
  402. clear_bit(F_TX_PROGRESS, &priv->flags);
  403. priv->cur_pri = 0;
  404. } else {
  405. dev->trans_start = jiffies;
  406. }
  407. if (!test_bit(F_TX_WAIT_ALL, &priv->flags))
  408. netif_wake_queue(dev);
  409. out_8(&regs->cantier, priv->tx_active);
  410. ret = IRQ_HANDLED;
  411. }
  412. canrflg = in_8(&regs->canrflg);
  413. if ((canrflg & ~MSCAN_STAT_MSK) &&
  414. !test_and_set_bit(F_RX_PROGRESS, &priv->flags)) {
  415. if (canrflg & ~MSCAN_STAT_MSK) {
  416. priv->shadow_canrier = in_8(&regs->canrier);
  417. out_8(&regs->canrier, 0);
  418. napi_schedule(&priv->napi);
  419. ret = IRQ_HANDLED;
  420. } else {
  421. clear_bit(F_RX_PROGRESS, &priv->flags);
  422. }
  423. }
  424. return ret;
  425. }
  426. static int mscan_do_set_mode(struct net_device *dev, enum can_mode mode)
  427. {
  428. int ret = 0;
  429. switch (mode) {
  430. case CAN_MODE_START:
  431. ret = mscan_restart(dev);
  432. if (ret)
  433. break;
  434. if (netif_queue_stopped(dev))
  435. netif_wake_queue(dev);
  436. break;
  437. default:
  438. ret = -EOPNOTSUPP;
  439. break;
  440. }
  441. return ret;
  442. }
  443. static int mscan_do_set_bittiming(struct net_device *dev)
  444. {
  445. struct mscan_priv *priv = netdev_priv(dev);
  446. struct mscan_regs __iomem *regs = priv->reg_base;
  447. struct can_bittiming *bt = &priv->can.bittiming;
  448. u8 btr0, btr1;
  449. btr0 = BTR0_SET_BRP(bt->brp) | BTR0_SET_SJW(bt->sjw);
  450. btr1 = (BTR1_SET_TSEG1(bt->prop_seg + bt->phase_seg1) |
  451. BTR1_SET_TSEG2(bt->phase_seg2) |
  452. BTR1_SET_SAM(priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES));
  453. netdev_info(dev, "setting BTR0=0x%02x BTR1=0x%02x\n", btr0, btr1);
  454. out_8(&regs->canbtr0, btr0);
  455. out_8(&regs->canbtr1, btr1);
  456. return 0;
  457. }
  458. static int mscan_get_berr_counter(const struct net_device *dev,
  459. struct can_berr_counter *bec)
  460. {
  461. struct mscan_priv *priv = netdev_priv(dev);
  462. struct mscan_regs __iomem *regs = priv->reg_base;
  463. bec->txerr = in_8(&regs->cantxerr);
  464. bec->rxerr = in_8(&regs->canrxerr);
  465. return 0;
  466. }
  467. static int mscan_open(struct net_device *dev)
  468. {
  469. int ret;
  470. struct mscan_priv *priv = netdev_priv(dev);
  471. struct mscan_regs __iomem *regs = priv->reg_base;
  472. if (priv->clk_ipg) {
  473. ret = clk_prepare_enable(priv->clk_ipg);
  474. if (ret)
  475. goto exit_retcode;
  476. }
  477. if (priv->clk_can) {
  478. ret = clk_prepare_enable(priv->clk_can);
  479. if (ret)
  480. goto exit_dis_ipg_clock;
  481. }
  482. /* common open */
  483. ret = open_candev(dev);
  484. if (ret)
  485. goto exit_dis_can_clock;
  486. napi_enable(&priv->napi);
  487. ret = request_irq(dev->irq, mscan_isr, 0, dev->name, dev);
  488. if (ret < 0) {
  489. netdev_err(dev, "failed to attach interrupt\n");
  490. goto exit_napi_disable;
  491. }
  492. if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
  493. setbits8(&regs->canctl1, MSCAN_LISTEN);
  494. else
  495. clrbits8(&regs->canctl1, MSCAN_LISTEN);
  496. ret = mscan_start(dev);
  497. if (ret)
  498. goto exit_free_irq;
  499. netif_start_queue(dev);
  500. return 0;
  501. exit_free_irq:
  502. free_irq(dev->irq, dev);
  503. exit_napi_disable:
  504. napi_disable(&priv->napi);
  505. close_candev(dev);
  506. exit_dis_can_clock:
  507. if (priv->clk_can)
  508. clk_disable_unprepare(priv->clk_can);
  509. exit_dis_ipg_clock:
  510. if (priv->clk_ipg)
  511. clk_disable_unprepare(priv->clk_ipg);
  512. exit_retcode:
  513. return ret;
  514. }
  515. static int mscan_close(struct net_device *dev)
  516. {
  517. struct mscan_priv *priv = netdev_priv(dev);
  518. struct mscan_regs __iomem *regs = priv->reg_base;
  519. netif_stop_queue(dev);
  520. napi_disable(&priv->napi);
  521. out_8(&regs->cantier, 0);
  522. out_8(&regs->canrier, 0);
  523. mscan_set_mode(dev, MSCAN_INIT_MODE);
  524. close_candev(dev);
  525. free_irq(dev->irq, dev);
  526. if (priv->clk_can)
  527. clk_disable_unprepare(priv->clk_can);
  528. if (priv->clk_ipg)
  529. clk_disable_unprepare(priv->clk_ipg);
  530. return 0;
  531. }
  532. static const struct net_device_ops mscan_netdev_ops = {
  533. .ndo_open = mscan_open,
  534. .ndo_stop = mscan_close,
  535. .ndo_start_xmit = mscan_start_xmit,
  536. .ndo_change_mtu = can_change_mtu,
  537. };
  538. int register_mscandev(struct net_device *dev, int mscan_clksrc)
  539. {
  540. struct mscan_priv *priv = netdev_priv(dev);
  541. struct mscan_regs __iomem *regs = priv->reg_base;
  542. u8 ctl1;
  543. ctl1 = in_8(&regs->canctl1);
  544. if (mscan_clksrc)
  545. ctl1 |= MSCAN_CLKSRC;
  546. else
  547. ctl1 &= ~MSCAN_CLKSRC;
  548. if (priv->type == MSCAN_TYPE_MPC5121) {
  549. priv->can.do_get_berr_counter = mscan_get_berr_counter;
  550. ctl1 |= MSCAN_BORM; /* bus-off recovery upon request */
  551. }
  552. ctl1 |= MSCAN_CANE;
  553. out_8(&regs->canctl1, ctl1);
  554. udelay(100);
  555. /* acceptance mask/acceptance code (accept everything) */
  556. out_be16(&regs->canidar1_0, 0);
  557. out_be16(&regs->canidar3_2, 0);
  558. out_be16(&regs->canidar5_4, 0);
  559. out_be16(&regs->canidar7_6, 0);
  560. out_be16(&regs->canidmr1_0, 0xffff);
  561. out_be16(&regs->canidmr3_2, 0xffff);
  562. out_be16(&regs->canidmr5_4, 0xffff);
  563. out_be16(&regs->canidmr7_6, 0xffff);
  564. /* Two 32 bit Acceptance Filters */
  565. out_8(&regs->canidac, MSCAN_AF_32BIT);
  566. mscan_set_mode(dev, MSCAN_INIT_MODE);
  567. return register_candev(dev);
  568. }
  569. void unregister_mscandev(struct net_device *dev)
  570. {
  571. struct mscan_priv *priv = netdev_priv(dev);
  572. struct mscan_regs __iomem *regs = priv->reg_base;
  573. mscan_set_mode(dev, MSCAN_INIT_MODE);
  574. clrbits8(&regs->canctl1, MSCAN_CANE);
  575. unregister_candev(dev);
  576. }
  577. struct net_device *alloc_mscandev(void)
  578. {
  579. struct net_device *dev;
  580. struct mscan_priv *priv;
  581. int i;
  582. dev = alloc_candev(sizeof(struct mscan_priv), MSCAN_ECHO_SKB_MAX);
  583. if (!dev)
  584. return NULL;
  585. priv = netdev_priv(dev);
  586. dev->netdev_ops = &mscan_netdev_ops;
  587. dev->flags |= IFF_ECHO; /* we support local echo */
  588. netif_napi_add(dev, &priv->napi, mscan_rx_poll, 8);
  589. priv->can.bittiming_const = &mscan_bittiming_const;
  590. priv->can.do_set_bittiming = mscan_do_set_bittiming;
  591. priv->can.do_set_mode = mscan_do_set_mode;
  592. priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES |
  593. CAN_CTRLMODE_LISTENONLY;
  594. for (i = 0; i < TX_QUEUE_SIZE; i++) {
  595. priv->tx_queue[i].id = i;
  596. priv->tx_queue[i].mask = 1 << i;
  597. }
  598. return dev;
  599. }
  600. MODULE_AUTHOR("Andrey Volkov <avolkov@varma-el.com>");
  601. MODULE_LICENSE("GPL v2");
  602. MODULE_DESCRIPTION("CAN port driver for a MSCAN based chips");