mscan.c 18 KB

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