bfin_can.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. /*
  2. * Blackfin On-Chip CAN Driver
  3. *
  4. * Copyright 2004-2009 Analog Devices Inc.
  5. *
  6. * Enter bugs at http://blackfin.uclinux.org/
  7. *
  8. * Licensed under the GPL-2 or later.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/kernel.h>
  13. #include <linux/bitops.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/errno.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/skbuff.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/can/dev.h>
  20. #include <linux/can/error.h>
  21. #include <asm/bfin_can.h>
  22. #include <asm/portmux.h>
  23. #define DRV_NAME "bfin_can"
  24. #define BFIN_CAN_TIMEOUT 100
  25. #define TX_ECHO_SKB_MAX 1
  26. /*
  27. * bfin can private data
  28. */
  29. struct bfin_can_priv {
  30. struct can_priv can; /* must be the first member */
  31. struct net_device *dev;
  32. void __iomem *membase;
  33. int rx_irq;
  34. int tx_irq;
  35. int err_irq;
  36. unsigned short *pin_list;
  37. };
  38. /*
  39. * bfin can timing parameters
  40. */
  41. static struct can_bittiming_const bfin_can_bittiming_const = {
  42. .name = DRV_NAME,
  43. .tseg1_min = 1,
  44. .tseg1_max = 16,
  45. .tseg2_min = 1,
  46. .tseg2_max = 8,
  47. .sjw_max = 4,
  48. /*
  49. * Although the BRP field can be set to any value, it is recommended
  50. * that the value be greater than or equal to 4, as restrictions
  51. * apply to the bit timing configuration when BRP is less than 4.
  52. */
  53. .brp_min = 4,
  54. .brp_max = 1024,
  55. .brp_inc = 1,
  56. };
  57. static int bfin_can_set_bittiming(struct net_device *dev)
  58. {
  59. struct bfin_can_priv *priv = netdev_priv(dev);
  60. struct bfin_can_regs __iomem *reg = priv->membase;
  61. struct can_bittiming *bt = &priv->can.bittiming;
  62. u16 clk, timing;
  63. clk = bt->brp - 1;
  64. timing = ((bt->sjw - 1) << 8) | (bt->prop_seg + bt->phase_seg1 - 1) |
  65. ((bt->phase_seg2 - 1) << 4);
  66. /*
  67. * If the SAM bit is set, the input signal is oversampled three times
  68. * at the SCLK rate.
  69. */
  70. if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
  71. timing |= SAM;
  72. bfin_write(&reg->clock, clk);
  73. bfin_write(&reg->timing, timing);
  74. netdev_info(dev, "setting CLOCK=0x%04x TIMING=0x%04x\n", clk, timing);
  75. return 0;
  76. }
  77. static void bfin_can_set_reset_mode(struct net_device *dev)
  78. {
  79. struct bfin_can_priv *priv = netdev_priv(dev);
  80. struct bfin_can_regs __iomem *reg = priv->membase;
  81. int timeout = BFIN_CAN_TIMEOUT;
  82. int i;
  83. /* disable interrupts */
  84. bfin_write(&reg->mbim1, 0);
  85. bfin_write(&reg->mbim2, 0);
  86. bfin_write(&reg->gim, 0);
  87. /* reset can and enter configuration mode */
  88. bfin_write(&reg->control, SRS | CCR);
  89. SSYNC();
  90. bfin_write(&reg->control, CCR);
  91. SSYNC();
  92. while (!(bfin_read(&reg->control) & CCA)) {
  93. udelay(10);
  94. if (--timeout == 0) {
  95. netdev_err(dev, "fail to enter configuration mode\n");
  96. BUG();
  97. }
  98. }
  99. /*
  100. * All mailbox configurations are marked as inactive
  101. * by writing to CAN Mailbox Configuration Registers 1 and 2
  102. * For all bits: 0 - Mailbox disabled, 1 - Mailbox enabled
  103. */
  104. bfin_write(&reg->mc1, 0);
  105. bfin_write(&reg->mc2, 0);
  106. /* Set Mailbox Direction */
  107. bfin_write(&reg->md1, 0xFFFF); /* mailbox 1-16 are RX */
  108. bfin_write(&reg->md2, 0); /* mailbox 17-32 are TX */
  109. /* RECEIVE_STD_CHL */
  110. for (i = 0; i < 2; i++) {
  111. bfin_write(&reg->chl[RECEIVE_STD_CHL + i].id0, 0);
  112. bfin_write(&reg->chl[RECEIVE_STD_CHL + i].id1, AME);
  113. bfin_write(&reg->chl[RECEIVE_STD_CHL + i].dlc, 0);
  114. bfin_write(&reg->msk[RECEIVE_STD_CHL + i].amh, 0x1FFF);
  115. bfin_write(&reg->msk[RECEIVE_STD_CHL + i].aml, 0xFFFF);
  116. }
  117. /* RECEIVE_EXT_CHL */
  118. for (i = 0; i < 2; i++) {
  119. bfin_write(&reg->chl[RECEIVE_EXT_CHL + i].id0, 0);
  120. bfin_write(&reg->chl[RECEIVE_EXT_CHL + i].id1, AME | IDE);
  121. bfin_write(&reg->chl[RECEIVE_EXT_CHL + i].dlc, 0);
  122. bfin_write(&reg->msk[RECEIVE_EXT_CHL + i].amh, 0x1FFF);
  123. bfin_write(&reg->msk[RECEIVE_EXT_CHL + i].aml, 0xFFFF);
  124. }
  125. bfin_write(&reg->mc2, BIT(TRANSMIT_CHL - 16));
  126. bfin_write(&reg->mc1, BIT(RECEIVE_STD_CHL) + BIT(RECEIVE_EXT_CHL));
  127. SSYNC();
  128. priv->can.state = CAN_STATE_STOPPED;
  129. }
  130. static void bfin_can_set_normal_mode(struct net_device *dev)
  131. {
  132. struct bfin_can_priv *priv = netdev_priv(dev);
  133. struct bfin_can_regs __iomem *reg = priv->membase;
  134. int timeout = BFIN_CAN_TIMEOUT;
  135. /*
  136. * leave configuration mode
  137. */
  138. bfin_write(&reg->control, bfin_read(&reg->control) & ~CCR);
  139. while (bfin_read(&reg->status) & CCA) {
  140. udelay(10);
  141. if (--timeout == 0) {
  142. netdev_err(dev, "fail to leave configuration mode\n");
  143. BUG();
  144. }
  145. }
  146. /*
  147. * clear _All_ tx and rx interrupts
  148. */
  149. bfin_write(&reg->mbtif1, 0xFFFF);
  150. bfin_write(&reg->mbtif2, 0xFFFF);
  151. bfin_write(&reg->mbrif1, 0xFFFF);
  152. bfin_write(&reg->mbrif2, 0xFFFF);
  153. /*
  154. * clear global interrupt status register
  155. */
  156. bfin_write(&reg->gis, 0x7FF); /* overwrites with '1' */
  157. /*
  158. * Initialize Interrupts
  159. * - set bits in the mailbox interrupt mask register
  160. * - global interrupt mask
  161. */
  162. bfin_write(&reg->mbim1, BIT(RECEIVE_STD_CHL) + BIT(RECEIVE_EXT_CHL));
  163. bfin_write(&reg->mbim2, BIT(TRANSMIT_CHL - 16));
  164. bfin_write(&reg->gim, EPIM | BOIM | RMLIM);
  165. SSYNC();
  166. }
  167. static void bfin_can_start(struct net_device *dev)
  168. {
  169. struct bfin_can_priv *priv = netdev_priv(dev);
  170. /* enter reset mode */
  171. if (priv->can.state != CAN_STATE_STOPPED)
  172. bfin_can_set_reset_mode(dev);
  173. /* leave reset mode */
  174. bfin_can_set_normal_mode(dev);
  175. }
  176. static int bfin_can_set_mode(struct net_device *dev, enum can_mode mode)
  177. {
  178. switch (mode) {
  179. case CAN_MODE_START:
  180. bfin_can_start(dev);
  181. if (netif_queue_stopped(dev))
  182. netif_wake_queue(dev);
  183. break;
  184. default:
  185. return -EOPNOTSUPP;
  186. }
  187. return 0;
  188. }
  189. static int bfin_can_get_berr_counter(const struct net_device *dev,
  190. struct can_berr_counter *bec)
  191. {
  192. struct bfin_can_priv *priv = netdev_priv(dev);
  193. struct bfin_can_regs __iomem *reg = priv->membase;
  194. u16 cec = bfin_read(&reg->cec);
  195. bec->txerr = cec >> 8;
  196. bec->rxerr = cec;
  197. return 0;
  198. }
  199. static int bfin_can_start_xmit(struct sk_buff *skb, struct net_device *dev)
  200. {
  201. struct bfin_can_priv *priv = netdev_priv(dev);
  202. struct bfin_can_regs __iomem *reg = priv->membase;
  203. struct can_frame *cf = (struct can_frame *)skb->data;
  204. u8 dlc = cf->can_dlc;
  205. canid_t id = cf->can_id;
  206. u8 *data = cf->data;
  207. u16 val;
  208. int i;
  209. if (can_dropped_invalid_skb(dev, skb))
  210. return NETDEV_TX_OK;
  211. netif_stop_queue(dev);
  212. /* fill id */
  213. if (id & CAN_EFF_FLAG) {
  214. bfin_write(&reg->chl[TRANSMIT_CHL].id0, id);
  215. val = ((id & 0x1FFF0000) >> 16) | IDE;
  216. } else
  217. val = (id << 2);
  218. if (id & CAN_RTR_FLAG)
  219. val |= RTR;
  220. bfin_write(&reg->chl[TRANSMIT_CHL].id1, val | AME);
  221. /* fill payload */
  222. for (i = 0; i < 8; i += 2) {
  223. val = ((7 - i) < dlc ? (data[7 - i]) : 0) +
  224. ((6 - i) < dlc ? (data[6 - i] << 8) : 0);
  225. bfin_write(&reg->chl[TRANSMIT_CHL].data[i], val);
  226. }
  227. /* fill data length code */
  228. bfin_write(&reg->chl[TRANSMIT_CHL].dlc, dlc);
  229. can_put_echo_skb(skb, dev, 0);
  230. /* set transmit request */
  231. bfin_write(&reg->trs2, BIT(TRANSMIT_CHL - 16));
  232. return 0;
  233. }
  234. static void bfin_can_rx(struct net_device *dev, u16 isrc)
  235. {
  236. struct bfin_can_priv *priv = netdev_priv(dev);
  237. struct net_device_stats *stats = &dev->stats;
  238. struct bfin_can_regs __iomem *reg = priv->membase;
  239. struct can_frame *cf;
  240. struct sk_buff *skb;
  241. int obj;
  242. int i;
  243. u16 val;
  244. skb = alloc_can_skb(dev, &cf);
  245. if (skb == NULL)
  246. return;
  247. /* get id */
  248. if (isrc & BIT(RECEIVE_EXT_CHL)) {
  249. /* extended frame format (EFF) */
  250. cf->can_id = ((bfin_read(&reg->chl[RECEIVE_EXT_CHL].id1)
  251. & 0x1FFF) << 16)
  252. + bfin_read(&reg->chl[RECEIVE_EXT_CHL].id0);
  253. cf->can_id |= CAN_EFF_FLAG;
  254. obj = RECEIVE_EXT_CHL;
  255. } else {
  256. /* standard frame format (SFF) */
  257. cf->can_id = (bfin_read(&reg->chl[RECEIVE_STD_CHL].id1)
  258. & 0x1ffc) >> 2;
  259. obj = RECEIVE_STD_CHL;
  260. }
  261. if (bfin_read(&reg->chl[obj].id1) & RTR)
  262. cf->can_id |= CAN_RTR_FLAG;
  263. /* get data length code */
  264. cf->can_dlc = get_can_dlc(bfin_read(&reg->chl[obj].dlc) & 0xF);
  265. /* get payload */
  266. for (i = 0; i < 8; i += 2) {
  267. val = bfin_read(&reg->chl[obj].data[i]);
  268. cf->data[7 - i] = (7 - i) < cf->can_dlc ? val : 0;
  269. cf->data[6 - i] = (6 - i) < cf->can_dlc ? (val >> 8) : 0;
  270. }
  271. netif_rx(skb);
  272. stats->rx_packets++;
  273. stats->rx_bytes += cf->can_dlc;
  274. }
  275. static int bfin_can_err(struct net_device *dev, u16 isrc, u16 status)
  276. {
  277. struct bfin_can_priv *priv = netdev_priv(dev);
  278. struct bfin_can_regs __iomem *reg = priv->membase;
  279. struct net_device_stats *stats = &dev->stats;
  280. struct can_frame *cf;
  281. struct sk_buff *skb;
  282. enum can_state state = priv->can.state;
  283. skb = alloc_can_err_skb(dev, &cf);
  284. if (skb == NULL)
  285. return -ENOMEM;
  286. if (isrc & RMLIS) {
  287. /* data overrun interrupt */
  288. netdev_dbg(dev, "data overrun interrupt\n");
  289. cf->can_id |= CAN_ERR_CRTL;
  290. cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
  291. stats->rx_over_errors++;
  292. stats->rx_errors++;
  293. }
  294. if (isrc & BOIS) {
  295. netdev_dbg(dev, "bus-off mode interrupt\n");
  296. state = CAN_STATE_BUS_OFF;
  297. cf->can_id |= CAN_ERR_BUSOFF;
  298. can_bus_off(dev);
  299. }
  300. if (isrc & EPIS) {
  301. /* error passive interrupt */
  302. netdev_dbg(dev, "error passive interrupt\n");
  303. state = CAN_STATE_ERROR_PASSIVE;
  304. }
  305. if ((isrc & EWTIS) || (isrc & EWRIS)) {
  306. netdev_dbg(dev, "Error Warning Transmit/Receive Interrupt\n");
  307. state = CAN_STATE_ERROR_WARNING;
  308. }
  309. if (state != priv->can.state && (state == CAN_STATE_ERROR_WARNING ||
  310. state == CAN_STATE_ERROR_PASSIVE)) {
  311. u16 cec = bfin_read(&reg->cec);
  312. u8 rxerr = cec;
  313. u8 txerr = cec >> 8;
  314. cf->can_id |= CAN_ERR_CRTL;
  315. if (state == CAN_STATE_ERROR_WARNING) {
  316. priv->can.can_stats.error_warning++;
  317. cf->data[1] = (txerr > rxerr) ?
  318. CAN_ERR_CRTL_TX_WARNING :
  319. CAN_ERR_CRTL_RX_WARNING;
  320. } else {
  321. priv->can.can_stats.error_passive++;
  322. cf->data[1] = (txerr > rxerr) ?
  323. CAN_ERR_CRTL_TX_PASSIVE :
  324. CAN_ERR_CRTL_RX_PASSIVE;
  325. }
  326. }
  327. if (status) {
  328. priv->can.can_stats.bus_error++;
  329. cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
  330. if (status & BEF)
  331. cf->data[2] |= CAN_ERR_PROT_BIT;
  332. else if (status & FER)
  333. cf->data[2] |= CAN_ERR_PROT_FORM;
  334. else if (status & SER)
  335. cf->data[2] |= CAN_ERR_PROT_STUFF;
  336. else
  337. cf->data[2] |= CAN_ERR_PROT_UNSPEC;
  338. }
  339. priv->can.state = state;
  340. netif_rx(skb);
  341. stats->rx_packets++;
  342. stats->rx_bytes += cf->can_dlc;
  343. return 0;
  344. }
  345. irqreturn_t bfin_can_interrupt(int irq, void *dev_id)
  346. {
  347. struct net_device *dev = dev_id;
  348. struct bfin_can_priv *priv = netdev_priv(dev);
  349. struct bfin_can_regs __iomem *reg = priv->membase;
  350. struct net_device_stats *stats = &dev->stats;
  351. u16 status, isrc;
  352. if ((irq == priv->tx_irq) && bfin_read(&reg->mbtif2)) {
  353. /* transmission complete interrupt */
  354. bfin_write(&reg->mbtif2, 0xFFFF);
  355. stats->tx_packets++;
  356. stats->tx_bytes += bfin_read(&reg->chl[TRANSMIT_CHL].dlc);
  357. can_get_echo_skb(dev, 0);
  358. netif_wake_queue(dev);
  359. } else if ((irq == priv->rx_irq) && bfin_read(&reg->mbrif1)) {
  360. /* receive interrupt */
  361. isrc = bfin_read(&reg->mbrif1);
  362. bfin_write(&reg->mbrif1, 0xFFFF);
  363. bfin_can_rx(dev, isrc);
  364. } else if ((irq == priv->err_irq) && bfin_read(&reg->gis)) {
  365. /* error interrupt */
  366. isrc = bfin_read(&reg->gis);
  367. status = bfin_read(&reg->esr);
  368. bfin_write(&reg->gis, 0x7FF);
  369. bfin_can_err(dev, isrc, status);
  370. } else {
  371. return IRQ_NONE;
  372. }
  373. return IRQ_HANDLED;
  374. }
  375. static int bfin_can_open(struct net_device *dev)
  376. {
  377. struct bfin_can_priv *priv = netdev_priv(dev);
  378. int err;
  379. /* set chip into reset mode */
  380. bfin_can_set_reset_mode(dev);
  381. /* common open */
  382. err = open_candev(dev);
  383. if (err)
  384. goto exit_open;
  385. /* register interrupt handler */
  386. err = request_irq(priv->rx_irq, &bfin_can_interrupt, 0,
  387. "bfin-can-rx", dev);
  388. if (err)
  389. goto exit_rx_irq;
  390. err = request_irq(priv->tx_irq, &bfin_can_interrupt, 0,
  391. "bfin-can-tx", dev);
  392. if (err)
  393. goto exit_tx_irq;
  394. err = request_irq(priv->err_irq, &bfin_can_interrupt, 0,
  395. "bfin-can-err", dev);
  396. if (err)
  397. goto exit_err_irq;
  398. bfin_can_start(dev);
  399. netif_start_queue(dev);
  400. return 0;
  401. exit_err_irq:
  402. free_irq(priv->tx_irq, dev);
  403. exit_tx_irq:
  404. free_irq(priv->rx_irq, dev);
  405. exit_rx_irq:
  406. close_candev(dev);
  407. exit_open:
  408. return err;
  409. }
  410. static int bfin_can_close(struct net_device *dev)
  411. {
  412. struct bfin_can_priv *priv = netdev_priv(dev);
  413. netif_stop_queue(dev);
  414. bfin_can_set_reset_mode(dev);
  415. close_candev(dev);
  416. free_irq(priv->rx_irq, dev);
  417. free_irq(priv->tx_irq, dev);
  418. free_irq(priv->err_irq, dev);
  419. return 0;
  420. }
  421. struct net_device *alloc_bfin_candev(void)
  422. {
  423. struct net_device *dev;
  424. struct bfin_can_priv *priv;
  425. dev = alloc_candev(sizeof(*priv), TX_ECHO_SKB_MAX);
  426. if (!dev)
  427. return NULL;
  428. priv = netdev_priv(dev);
  429. priv->dev = dev;
  430. priv->can.bittiming_const = &bfin_can_bittiming_const;
  431. priv->can.do_set_bittiming = bfin_can_set_bittiming;
  432. priv->can.do_set_mode = bfin_can_set_mode;
  433. priv->can.do_get_berr_counter = bfin_can_get_berr_counter;
  434. priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
  435. return dev;
  436. }
  437. static const struct net_device_ops bfin_can_netdev_ops = {
  438. .ndo_open = bfin_can_open,
  439. .ndo_stop = bfin_can_close,
  440. .ndo_start_xmit = bfin_can_start_xmit,
  441. };
  442. static int __devinit bfin_can_probe(struct platform_device *pdev)
  443. {
  444. int err;
  445. struct net_device *dev;
  446. struct bfin_can_priv *priv;
  447. struct resource *res_mem, *rx_irq, *tx_irq, *err_irq;
  448. unsigned short *pdata;
  449. pdata = pdev->dev.platform_data;
  450. if (!pdata) {
  451. dev_err(&pdev->dev, "No platform data provided!\n");
  452. err = -EINVAL;
  453. goto exit;
  454. }
  455. res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  456. rx_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  457. tx_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
  458. err_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 2);
  459. if (!res_mem || !rx_irq || !tx_irq || !err_irq) {
  460. err = -EINVAL;
  461. goto exit;
  462. }
  463. if (!request_mem_region(res_mem->start, resource_size(res_mem),
  464. dev_name(&pdev->dev))) {
  465. err = -EBUSY;
  466. goto exit;
  467. }
  468. /* request peripheral pins */
  469. err = peripheral_request_list(pdata, dev_name(&pdev->dev));
  470. if (err)
  471. goto exit_mem_release;
  472. dev = alloc_bfin_candev();
  473. if (!dev) {
  474. err = -ENOMEM;
  475. goto exit_peri_pin_free;
  476. }
  477. priv = netdev_priv(dev);
  478. priv->membase = (void __iomem *)res_mem->start;
  479. priv->rx_irq = rx_irq->start;
  480. priv->tx_irq = tx_irq->start;
  481. priv->err_irq = err_irq->start;
  482. priv->pin_list = pdata;
  483. priv->can.clock.freq = get_sclk();
  484. dev_set_drvdata(&pdev->dev, dev);
  485. SET_NETDEV_DEV(dev, &pdev->dev);
  486. dev->flags |= IFF_ECHO; /* we support local echo */
  487. dev->netdev_ops = &bfin_can_netdev_ops;
  488. bfin_can_set_reset_mode(dev);
  489. err = register_candev(dev);
  490. if (err) {
  491. dev_err(&pdev->dev, "registering failed (err=%d)\n", err);
  492. goto exit_candev_free;
  493. }
  494. dev_info(&pdev->dev,
  495. "%s device registered"
  496. "(&reg_base=%p, rx_irq=%d, tx_irq=%d, err_irq=%d, sclk=%d)\n",
  497. DRV_NAME, (void *)priv->membase, priv->rx_irq,
  498. priv->tx_irq, priv->err_irq, priv->can.clock.freq);
  499. return 0;
  500. exit_candev_free:
  501. free_candev(dev);
  502. exit_peri_pin_free:
  503. peripheral_free_list(pdata);
  504. exit_mem_release:
  505. release_mem_region(res_mem->start, resource_size(res_mem));
  506. exit:
  507. return err;
  508. }
  509. static int __devexit bfin_can_remove(struct platform_device *pdev)
  510. {
  511. struct net_device *dev = dev_get_drvdata(&pdev->dev);
  512. struct bfin_can_priv *priv = netdev_priv(dev);
  513. struct resource *res;
  514. bfin_can_set_reset_mode(dev);
  515. unregister_candev(dev);
  516. dev_set_drvdata(&pdev->dev, NULL);
  517. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  518. release_mem_region(res->start, resource_size(res));
  519. peripheral_free_list(priv->pin_list);
  520. free_candev(dev);
  521. return 0;
  522. }
  523. #ifdef CONFIG_PM
  524. static int bfin_can_suspend(struct platform_device *pdev, pm_message_t mesg)
  525. {
  526. struct net_device *dev = dev_get_drvdata(&pdev->dev);
  527. struct bfin_can_priv *priv = netdev_priv(dev);
  528. struct bfin_can_regs __iomem *reg = priv->membase;
  529. int timeout = BFIN_CAN_TIMEOUT;
  530. if (netif_running(dev)) {
  531. /* enter sleep mode */
  532. bfin_write(&reg->control, bfin_read(&reg->control) | SMR);
  533. SSYNC();
  534. while (!(bfin_read(&reg->intr) & SMACK)) {
  535. udelay(10);
  536. if (--timeout == 0) {
  537. netdev_err(dev, "fail to enter sleep mode\n");
  538. BUG();
  539. }
  540. }
  541. }
  542. return 0;
  543. }
  544. static int bfin_can_resume(struct platform_device *pdev)
  545. {
  546. struct net_device *dev = dev_get_drvdata(&pdev->dev);
  547. struct bfin_can_priv *priv = netdev_priv(dev);
  548. struct bfin_can_regs __iomem *reg = priv->membase;
  549. if (netif_running(dev)) {
  550. /* leave sleep mode */
  551. bfin_write(&reg->intr, 0);
  552. SSYNC();
  553. }
  554. return 0;
  555. }
  556. #else
  557. #define bfin_can_suspend NULL
  558. #define bfin_can_resume NULL
  559. #endif /* CONFIG_PM */
  560. static struct platform_driver bfin_can_driver = {
  561. .probe = bfin_can_probe,
  562. .remove = __devexit_p(bfin_can_remove),
  563. .suspend = bfin_can_suspend,
  564. .resume = bfin_can_resume,
  565. .driver = {
  566. .name = DRV_NAME,
  567. .owner = THIS_MODULE,
  568. },
  569. };
  570. module_platform_driver(bfin_can_driver);
  571. MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
  572. MODULE_LICENSE("GPL");
  573. MODULE_DESCRIPTION("Blackfin on-chip CAN netdevice driver");