bfin_can.c 17 KB

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